chp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 1999, 2010
  4. * Author(s): Cornelia Huck ([email protected])
  5. * Arnd Bergmann ([email protected])
  6. * Peter Oberparleiter <[email protected]>
  7. */
  8. #include <linux/bug.h>
  9. #include <linux/workqueue.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/export.h>
  12. #include <linux/sched.h>
  13. #include <linux/init.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/wait.h>
  16. #include <linux/mutex.h>
  17. #include <linux/errno.h>
  18. #include <linux/slab.h>
  19. #include <asm/chpid.h>
  20. #include <asm/sclp.h>
  21. #include <asm/crw.h>
  22. #include "cio.h"
  23. #include "css.h"
  24. #include "ioasm.h"
  25. #include "cio_debug.h"
  26. #include "chp.h"
  27. #define to_channelpath(device) container_of(device, struct channel_path, dev)
  28. #define CHP_INFO_UPDATE_INTERVAL 1*HZ
  29. enum cfg_task_t {
  30. cfg_none,
  31. cfg_configure,
  32. cfg_deconfigure
  33. };
  34. /* Map for pending configure tasks. */
  35. static enum cfg_task_t chp_cfg_task[__MAX_CSSID + 1][__MAX_CHPID + 1];
  36. static DEFINE_SPINLOCK(cfg_lock);
  37. /* Map for channel-path status. */
  38. static struct sclp_chp_info chp_info;
  39. static DEFINE_MUTEX(info_lock);
  40. /* Time after which channel-path status may be outdated. */
  41. static unsigned long chp_info_expires;
  42. static struct work_struct cfg_work;
  43. /* Wait queue for configure completion events. */
  44. static DECLARE_WAIT_QUEUE_HEAD(cfg_wait_queue);
  45. /* Set vary state for given chpid. */
  46. static void set_chp_logically_online(struct chp_id chpid, int onoff)
  47. {
  48. chpid_to_chp(chpid)->state = onoff;
  49. }
  50. /* On success return 0 if channel-path is varied offline, 1 if it is varied
  51. * online. Return -ENODEV if channel-path is not registered. */
  52. int chp_get_status(struct chp_id chpid)
  53. {
  54. return (chpid_to_chp(chpid) ? chpid_to_chp(chpid)->state : -ENODEV);
  55. }
  56. /**
  57. * chp_get_sch_opm - return opm for subchannel
  58. * @sch: subchannel
  59. *
  60. * Calculate and return the operational path mask (opm) based on the chpids
  61. * used by the subchannel and the status of the associated channel-paths.
  62. */
  63. u8 chp_get_sch_opm(struct subchannel *sch)
  64. {
  65. struct chp_id chpid;
  66. int opm;
  67. int i;
  68. opm = 0;
  69. chp_id_init(&chpid);
  70. for (i = 0; i < 8; i++) {
  71. opm <<= 1;
  72. chpid.id = sch->schib.pmcw.chpid[i];
  73. if (chp_get_status(chpid) != 0)
  74. opm |= 1;
  75. }
  76. return opm;
  77. }
  78. EXPORT_SYMBOL_GPL(chp_get_sch_opm);
  79. /**
  80. * chp_is_registered - check if a channel-path is registered
  81. * @chpid: channel-path ID
  82. *
  83. * Return non-zero if a channel-path with the given chpid is registered,
  84. * zero otherwise.
  85. */
  86. int chp_is_registered(struct chp_id chpid)
  87. {
  88. return chpid_to_chp(chpid) != NULL;
  89. }
  90. /*
  91. * Function: s390_vary_chpid
  92. * Varies the specified chpid online or offline
  93. */
  94. static int s390_vary_chpid(struct chp_id chpid, int on)
  95. {
  96. char dbf_text[15];
  97. int status;
  98. sprintf(dbf_text, on?"varyon%x.%02x":"varyoff%x.%02x", chpid.cssid,
  99. chpid.id);
  100. CIO_TRACE_EVENT(2, dbf_text);
  101. status = chp_get_status(chpid);
  102. if (!on && !status)
  103. return 0;
  104. set_chp_logically_online(chpid, on);
  105. chsc_chp_vary(chpid, on);
  106. return 0;
  107. }
  108. /*
  109. * Channel measurement related functions
  110. */
  111. static ssize_t chp_measurement_chars_read(struct file *filp,
  112. struct kobject *kobj,
  113. struct bin_attribute *bin_attr,
  114. char *buf, loff_t off, size_t count)
  115. {
  116. struct channel_path *chp;
  117. struct device *device;
  118. device = kobj_to_dev(kobj);
  119. chp = to_channelpath(device);
  120. if (chp->cmg == -1)
  121. return 0;
  122. return memory_read_from_buffer(buf, count, &off, &chp->cmg_chars,
  123. sizeof(chp->cmg_chars));
  124. }
  125. static const struct bin_attribute chp_measurement_chars_attr = {
  126. .attr = {
  127. .name = "measurement_chars",
  128. .mode = S_IRUSR,
  129. },
  130. .size = sizeof(struct cmg_chars),
  131. .read = chp_measurement_chars_read,
  132. };
  133. static void chp_measurement_copy_block(struct cmg_entry *buf,
  134. struct channel_subsystem *css,
  135. struct chp_id chpid)
  136. {
  137. void *area;
  138. struct cmg_entry *entry, reference_buf;
  139. int idx;
  140. if (chpid.id < 128) {
  141. area = css->cub_addr1;
  142. idx = chpid.id;
  143. } else {
  144. area = css->cub_addr2;
  145. idx = chpid.id - 128;
  146. }
  147. entry = area + (idx * sizeof(struct cmg_entry));
  148. do {
  149. memcpy(buf, entry, sizeof(*entry));
  150. memcpy(&reference_buf, entry, sizeof(*entry));
  151. } while (reference_buf.values[0] != buf->values[0]);
  152. }
  153. static ssize_t chp_measurement_read(struct file *filp, struct kobject *kobj,
  154. struct bin_attribute *bin_attr,
  155. char *buf, loff_t off, size_t count)
  156. {
  157. struct channel_path *chp;
  158. struct channel_subsystem *css;
  159. struct device *device;
  160. unsigned int size;
  161. device = kobj_to_dev(kobj);
  162. chp = to_channelpath(device);
  163. css = to_css(chp->dev.parent);
  164. size = sizeof(struct cmg_entry);
  165. /* Only allow single reads. */
  166. if (off || count < size)
  167. return 0;
  168. chp_measurement_copy_block((struct cmg_entry *)buf, css, chp->chpid);
  169. count = size;
  170. return count;
  171. }
  172. static const struct bin_attribute chp_measurement_attr = {
  173. .attr = {
  174. .name = "measurement",
  175. .mode = S_IRUSR,
  176. },
  177. .size = sizeof(struct cmg_entry),
  178. .read = chp_measurement_read,
  179. };
  180. void chp_remove_cmg_attr(struct channel_path *chp)
  181. {
  182. device_remove_bin_file(&chp->dev, &chp_measurement_chars_attr);
  183. device_remove_bin_file(&chp->dev, &chp_measurement_attr);
  184. }
  185. int chp_add_cmg_attr(struct channel_path *chp)
  186. {
  187. int ret;
  188. ret = device_create_bin_file(&chp->dev, &chp_measurement_chars_attr);
  189. if (ret)
  190. return ret;
  191. ret = device_create_bin_file(&chp->dev, &chp_measurement_attr);
  192. if (ret)
  193. device_remove_bin_file(&chp->dev, &chp_measurement_chars_attr);
  194. return ret;
  195. }
  196. /*
  197. * Files for the channel path entries.
  198. */
  199. static ssize_t chp_status_show(struct device *dev,
  200. struct device_attribute *attr, char *buf)
  201. {
  202. struct channel_path *chp = to_channelpath(dev);
  203. int status;
  204. mutex_lock(&chp->lock);
  205. status = chp->state;
  206. mutex_unlock(&chp->lock);
  207. return status ? sprintf(buf, "online\n") : sprintf(buf, "offline\n");
  208. }
  209. static ssize_t chp_status_write(struct device *dev,
  210. struct device_attribute *attr,
  211. const char *buf, size_t count)
  212. {
  213. struct channel_path *cp = to_channelpath(dev);
  214. char cmd[10];
  215. int num_args;
  216. int error;
  217. num_args = sscanf(buf, "%5s", cmd);
  218. if (!num_args)
  219. return count;
  220. /* Wait until previous actions have settled. */
  221. css_wait_for_slow_path();
  222. if (!strncasecmp(cmd, "on", 2) || !strcmp(cmd, "1")) {
  223. mutex_lock(&cp->lock);
  224. error = s390_vary_chpid(cp->chpid, 1);
  225. mutex_unlock(&cp->lock);
  226. } else if (!strncasecmp(cmd, "off", 3) || !strcmp(cmd, "0")) {
  227. mutex_lock(&cp->lock);
  228. error = s390_vary_chpid(cp->chpid, 0);
  229. mutex_unlock(&cp->lock);
  230. } else
  231. error = -EINVAL;
  232. return error < 0 ? error : count;
  233. }
  234. static DEVICE_ATTR(status, 0644, chp_status_show, chp_status_write);
  235. static ssize_t chp_configure_show(struct device *dev,
  236. struct device_attribute *attr, char *buf)
  237. {
  238. struct channel_path *cp;
  239. int status;
  240. cp = to_channelpath(dev);
  241. status = chp_info_get_status(cp->chpid);
  242. if (status < 0)
  243. return status;
  244. return sysfs_emit(buf, "%d\n", status);
  245. }
  246. static int cfg_wait_idle(void);
  247. static ssize_t chp_configure_write(struct device *dev,
  248. struct device_attribute *attr,
  249. const char *buf, size_t count)
  250. {
  251. struct channel_path *cp;
  252. int val;
  253. char delim;
  254. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  255. return -EINVAL;
  256. if (val != 0 && val != 1)
  257. return -EINVAL;
  258. cp = to_channelpath(dev);
  259. chp_cfg_schedule(cp->chpid, val);
  260. cfg_wait_idle();
  261. return count;
  262. }
  263. static DEVICE_ATTR(configure, 0644, chp_configure_show, chp_configure_write);
  264. static ssize_t chp_type_show(struct device *dev, struct device_attribute *attr,
  265. char *buf)
  266. {
  267. struct channel_path *chp = to_channelpath(dev);
  268. u8 type;
  269. mutex_lock(&chp->lock);
  270. type = chp->desc.desc;
  271. mutex_unlock(&chp->lock);
  272. return sprintf(buf, "%x\n", type);
  273. }
  274. static DEVICE_ATTR(type, 0444, chp_type_show, NULL);
  275. static ssize_t chp_cmg_show(struct device *dev, struct device_attribute *attr,
  276. char *buf)
  277. {
  278. struct channel_path *chp = to_channelpath(dev);
  279. if (!chp)
  280. return 0;
  281. if (chp->cmg == -1) /* channel measurements not available */
  282. return sprintf(buf, "unknown\n");
  283. return sprintf(buf, "%x\n", chp->cmg);
  284. }
  285. static DEVICE_ATTR(cmg, 0444, chp_cmg_show, NULL);
  286. static ssize_t chp_shared_show(struct device *dev,
  287. struct device_attribute *attr, char *buf)
  288. {
  289. struct channel_path *chp = to_channelpath(dev);
  290. if (!chp)
  291. return 0;
  292. if (chp->shared == -1) /* channel measurements not available */
  293. return sprintf(buf, "unknown\n");
  294. return sprintf(buf, "%x\n", chp->shared);
  295. }
  296. static DEVICE_ATTR(shared, 0444, chp_shared_show, NULL);
  297. static ssize_t chp_chid_show(struct device *dev, struct device_attribute *attr,
  298. char *buf)
  299. {
  300. struct channel_path *chp = to_channelpath(dev);
  301. ssize_t rc;
  302. mutex_lock(&chp->lock);
  303. if (chp->desc_fmt1.flags & 0x10)
  304. rc = sprintf(buf, "%04x\n", chp->desc_fmt1.chid);
  305. else
  306. rc = 0;
  307. mutex_unlock(&chp->lock);
  308. return rc;
  309. }
  310. static DEVICE_ATTR(chid, 0444, chp_chid_show, NULL);
  311. static ssize_t chp_chid_external_show(struct device *dev,
  312. struct device_attribute *attr, char *buf)
  313. {
  314. struct channel_path *chp = to_channelpath(dev);
  315. ssize_t rc;
  316. mutex_lock(&chp->lock);
  317. if (chp->desc_fmt1.flags & 0x10)
  318. rc = sprintf(buf, "%x\n", chp->desc_fmt1.flags & 0x8 ? 1 : 0);
  319. else
  320. rc = 0;
  321. mutex_unlock(&chp->lock);
  322. return rc;
  323. }
  324. static DEVICE_ATTR(chid_external, 0444, chp_chid_external_show, NULL);
  325. static ssize_t chp_esc_show(struct device *dev,
  326. struct device_attribute *attr, char *buf)
  327. {
  328. struct channel_path *chp = to_channelpath(dev);
  329. ssize_t rc;
  330. mutex_lock(&chp->lock);
  331. rc = sprintf(buf, "%x\n", chp->desc_fmt1.esc);
  332. mutex_unlock(&chp->lock);
  333. return rc;
  334. }
  335. static DEVICE_ATTR(esc, 0444, chp_esc_show, NULL);
  336. static ssize_t util_string_read(struct file *filp, struct kobject *kobj,
  337. struct bin_attribute *attr, char *buf,
  338. loff_t off, size_t count)
  339. {
  340. struct channel_path *chp = to_channelpath(kobj_to_dev(kobj));
  341. ssize_t rc;
  342. mutex_lock(&chp->lock);
  343. rc = memory_read_from_buffer(buf, count, &off, chp->desc_fmt3.util_str,
  344. sizeof(chp->desc_fmt3.util_str));
  345. mutex_unlock(&chp->lock);
  346. return rc;
  347. }
  348. static BIN_ATTR_RO(util_string,
  349. sizeof(((struct channel_path_desc_fmt3 *)0)->util_str));
  350. static struct bin_attribute *chp_bin_attrs[] = {
  351. &bin_attr_util_string,
  352. NULL,
  353. };
  354. static struct attribute *chp_attrs[] = {
  355. &dev_attr_status.attr,
  356. &dev_attr_configure.attr,
  357. &dev_attr_type.attr,
  358. &dev_attr_cmg.attr,
  359. &dev_attr_shared.attr,
  360. &dev_attr_chid.attr,
  361. &dev_attr_chid_external.attr,
  362. &dev_attr_esc.attr,
  363. NULL,
  364. };
  365. static struct attribute_group chp_attr_group = {
  366. .attrs = chp_attrs,
  367. .bin_attrs = chp_bin_attrs,
  368. };
  369. static const struct attribute_group *chp_attr_groups[] = {
  370. &chp_attr_group,
  371. NULL,
  372. };
  373. static void chp_release(struct device *dev)
  374. {
  375. struct channel_path *cp;
  376. cp = to_channelpath(dev);
  377. kfree(cp);
  378. }
  379. /**
  380. * chp_update_desc - update channel-path description
  381. * @chp: channel-path
  382. *
  383. * Update the channel-path description of the specified channel-path
  384. * including channel measurement related information.
  385. * Return zero on success, non-zero otherwise.
  386. */
  387. int chp_update_desc(struct channel_path *chp)
  388. {
  389. int rc;
  390. rc = chsc_determine_fmt0_channel_path_desc(chp->chpid, &chp->desc);
  391. if (rc)
  392. return rc;
  393. /*
  394. * Fetching the following data is optional. Not all machines or
  395. * hypervisors implement the required chsc commands.
  396. */
  397. chsc_determine_fmt1_channel_path_desc(chp->chpid, &chp->desc_fmt1);
  398. chsc_determine_fmt3_channel_path_desc(chp->chpid, &chp->desc_fmt3);
  399. chsc_get_channel_measurement_chars(chp);
  400. return 0;
  401. }
  402. /**
  403. * chp_new - register a new channel-path
  404. * @chpid: channel-path ID
  405. *
  406. * Create and register data structure representing new channel-path. Return
  407. * zero on success, non-zero otherwise.
  408. */
  409. int chp_new(struct chp_id chpid)
  410. {
  411. struct channel_subsystem *css = css_by_id(chpid.cssid);
  412. struct channel_path *chp;
  413. int ret = 0;
  414. mutex_lock(&css->mutex);
  415. if (chp_is_registered(chpid))
  416. goto out;
  417. chp = kzalloc(sizeof(struct channel_path), GFP_KERNEL);
  418. if (!chp) {
  419. ret = -ENOMEM;
  420. goto out;
  421. }
  422. /* fill in status, etc. */
  423. chp->chpid = chpid;
  424. chp->state = 1;
  425. chp->dev.parent = &css->device;
  426. chp->dev.groups = chp_attr_groups;
  427. chp->dev.release = chp_release;
  428. mutex_init(&chp->lock);
  429. /* Obtain channel path description and fill it in. */
  430. ret = chp_update_desc(chp);
  431. if (ret)
  432. goto out_free;
  433. if ((chp->desc.flags & 0x80) == 0) {
  434. ret = -ENODEV;
  435. goto out_free;
  436. }
  437. dev_set_name(&chp->dev, "chp%x.%02x", chpid.cssid, chpid.id);
  438. /* make it known to the system */
  439. ret = device_register(&chp->dev);
  440. if (ret) {
  441. CIO_MSG_EVENT(0, "Could not register chp%x.%02x: %d\n",
  442. chpid.cssid, chpid.id, ret);
  443. put_device(&chp->dev);
  444. goto out;
  445. }
  446. if (css->cm_enabled) {
  447. ret = chp_add_cmg_attr(chp);
  448. if (ret) {
  449. device_unregister(&chp->dev);
  450. goto out;
  451. }
  452. }
  453. css->chps[chpid.id] = chp;
  454. goto out;
  455. out_free:
  456. kfree(chp);
  457. out:
  458. mutex_unlock(&css->mutex);
  459. return ret;
  460. }
  461. /**
  462. * chp_get_chp_desc - return newly allocated channel-path description
  463. * @chpid: channel-path ID
  464. *
  465. * On success return a newly allocated copy of the channel-path description
  466. * data associated with the given channel-path ID. Return %NULL on error.
  467. */
  468. struct channel_path_desc_fmt0 *chp_get_chp_desc(struct chp_id chpid)
  469. {
  470. struct channel_path *chp;
  471. struct channel_path_desc_fmt0 *desc;
  472. chp = chpid_to_chp(chpid);
  473. if (!chp)
  474. return NULL;
  475. desc = kmalloc(sizeof(*desc), GFP_KERNEL);
  476. if (!desc)
  477. return NULL;
  478. mutex_lock(&chp->lock);
  479. memcpy(desc, &chp->desc, sizeof(*desc));
  480. mutex_unlock(&chp->lock);
  481. return desc;
  482. }
  483. /**
  484. * chp_process_crw - process channel-path status change
  485. * @crw0: channel report-word to handler
  486. * @crw1: second channel-report word (always NULL)
  487. * @overflow: crw overflow indication
  488. *
  489. * Handle channel-report-words indicating that the status of a channel-path
  490. * has changed.
  491. */
  492. static void chp_process_crw(struct crw *crw0, struct crw *crw1,
  493. int overflow)
  494. {
  495. struct chp_id chpid;
  496. if (overflow) {
  497. css_schedule_eval_all();
  498. return;
  499. }
  500. CIO_CRW_EVENT(2, "CRW reports slct=%d, oflw=%d, "
  501. "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
  502. crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
  503. crw0->erc, crw0->rsid);
  504. /*
  505. * Check for solicited machine checks. These are
  506. * created by reset channel path and need not be
  507. * handled here.
  508. */
  509. if (crw0->slct) {
  510. CIO_CRW_EVENT(2, "solicited machine check for "
  511. "channel path %02X\n", crw0->rsid);
  512. return;
  513. }
  514. chp_id_init(&chpid);
  515. chpid.id = crw0->rsid;
  516. switch (crw0->erc) {
  517. case CRW_ERC_IPARM: /* Path has come. */
  518. case CRW_ERC_INIT:
  519. chp_new(chpid);
  520. chsc_chp_online(chpid);
  521. break;
  522. case CRW_ERC_PERRI: /* Path has gone. */
  523. case CRW_ERC_PERRN:
  524. chsc_chp_offline(chpid);
  525. break;
  526. default:
  527. CIO_CRW_EVENT(2, "Don't know how to handle erc=%x\n",
  528. crw0->erc);
  529. }
  530. }
  531. int chp_ssd_get_mask(struct chsc_ssd_info *ssd, struct chp_link *link)
  532. {
  533. int i;
  534. int mask;
  535. for (i = 0; i < 8; i++) {
  536. mask = 0x80 >> i;
  537. if (!(ssd->path_mask & mask))
  538. continue;
  539. if (!chp_id_is_equal(&ssd->chpid[i], &link->chpid))
  540. continue;
  541. if ((ssd->fla_valid_mask & mask) &&
  542. ((ssd->fla[i] & link->fla_mask) != link->fla))
  543. continue;
  544. return mask;
  545. }
  546. return 0;
  547. }
  548. EXPORT_SYMBOL_GPL(chp_ssd_get_mask);
  549. static inline int info_bit_num(struct chp_id id)
  550. {
  551. return id.id + id.cssid * (__MAX_CHPID + 1);
  552. }
  553. /* Force chp_info refresh on next call to info_validate(). */
  554. static void info_expire(void)
  555. {
  556. mutex_lock(&info_lock);
  557. chp_info_expires = jiffies - 1;
  558. mutex_unlock(&info_lock);
  559. }
  560. /* Ensure that chp_info is up-to-date. */
  561. static int info_update(void)
  562. {
  563. int rc;
  564. mutex_lock(&info_lock);
  565. rc = 0;
  566. if (time_after(jiffies, chp_info_expires)) {
  567. /* Data is too old, update. */
  568. rc = sclp_chp_read_info(&chp_info);
  569. chp_info_expires = jiffies + CHP_INFO_UPDATE_INTERVAL ;
  570. }
  571. mutex_unlock(&info_lock);
  572. return rc;
  573. }
  574. /**
  575. * chp_info_get_status - retrieve configure status of a channel-path
  576. * @chpid: channel-path ID
  577. *
  578. * On success, return 0 for standby, 1 for configured, 2 for reserved,
  579. * 3 for not recognized. Return negative error code on error.
  580. */
  581. int chp_info_get_status(struct chp_id chpid)
  582. {
  583. int rc;
  584. int bit;
  585. rc = info_update();
  586. if (rc)
  587. return rc;
  588. bit = info_bit_num(chpid);
  589. mutex_lock(&info_lock);
  590. if (!chp_test_bit(chp_info.recognized, bit))
  591. rc = CHP_STATUS_NOT_RECOGNIZED;
  592. else if (chp_test_bit(chp_info.configured, bit))
  593. rc = CHP_STATUS_CONFIGURED;
  594. else if (chp_test_bit(chp_info.standby, bit))
  595. rc = CHP_STATUS_STANDBY;
  596. else
  597. rc = CHP_STATUS_RESERVED;
  598. mutex_unlock(&info_lock);
  599. return rc;
  600. }
  601. /* Return configure task for chpid. */
  602. static enum cfg_task_t cfg_get_task(struct chp_id chpid)
  603. {
  604. return chp_cfg_task[chpid.cssid][chpid.id];
  605. }
  606. /* Set configure task for chpid. */
  607. static void cfg_set_task(struct chp_id chpid, enum cfg_task_t cfg)
  608. {
  609. chp_cfg_task[chpid.cssid][chpid.id] = cfg;
  610. }
  611. /* Fetch the first configure task. Set chpid accordingly. */
  612. static enum cfg_task_t chp_cfg_fetch_task(struct chp_id *chpid)
  613. {
  614. enum cfg_task_t t = cfg_none;
  615. chp_id_for_each(chpid) {
  616. t = cfg_get_task(*chpid);
  617. if (t != cfg_none)
  618. break;
  619. }
  620. return t;
  621. }
  622. /* Perform one configure/deconfigure request. Reschedule work function until
  623. * last request. */
  624. static void cfg_func(struct work_struct *work)
  625. {
  626. struct chp_id chpid;
  627. enum cfg_task_t t;
  628. int rc;
  629. spin_lock(&cfg_lock);
  630. t = chp_cfg_fetch_task(&chpid);
  631. spin_unlock(&cfg_lock);
  632. switch (t) {
  633. case cfg_configure:
  634. rc = sclp_chp_configure(chpid);
  635. if (rc)
  636. CIO_MSG_EVENT(2, "chp: sclp_chp_configure(%x.%02x)="
  637. "%d\n", chpid.cssid, chpid.id, rc);
  638. else {
  639. info_expire();
  640. chsc_chp_online(chpid);
  641. }
  642. break;
  643. case cfg_deconfigure:
  644. rc = sclp_chp_deconfigure(chpid);
  645. if (rc)
  646. CIO_MSG_EVENT(2, "chp: sclp_chp_deconfigure(%x.%02x)="
  647. "%d\n", chpid.cssid, chpid.id, rc);
  648. else {
  649. info_expire();
  650. chsc_chp_offline(chpid);
  651. }
  652. break;
  653. case cfg_none:
  654. /* Get updated information after last change. */
  655. info_update();
  656. wake_up_interruptible(&cfg_wait_queue);
  657. return;
  658. }
  659. spin_lock(&cfg_lock);
  660. if (t == cfg_get_task(chpid))
  661. cfg_set_task(chpid, cfg_none);
  662. spin_unlock(&cfg_lock);
  663. schedule_work(&cfg_work);
  664. }
  665. /**
  666. * chp_cfg_schedule - schedule chpid configuration request
  667. * @chpid: channel-path ID
  668. * @configure: Non-zero for configure, zero for deconfigure
  669. *
  670. * Schedule a channel-path configuration/deconfiguration request.
  671. */
  672. void chp_cfg_schedule(struct chp_id chpid, int configure)
  673. {
  674. CIO_MSG_EVENT(2, "chp_cfg_sched%x.%02x=%d\n", chpid.cssid, chpid.id,
  675. configure);
  676. spin_lock(&cfg_lock);
  677. cfg_set_task(chpid, configure ? cfg_configure : cfg_deconfigure);
  678. spin_unlock(&cfg_lock);
  679. schedule_work(&cfg_work);
  680. }
  681. /**
  682. * chp_cfg_cancel_deconfigure - cancel chpid deconfiguration request
  683. * @chpid: channel-path ID
  684. *
  685. * Cancel an active channel-path deconfiguration request if it has not yet
  686. * been performed.
  687. */
  688. void chp_cfg_cancel_deconfigure(struct chp_id chpid)
  689. {
  690. CIO_MSG_EVENT(2, "chp_cfg_cancel:%x.%02x\n", chpid.cssid, chpid.id);
  691. spin_lock(&cfg_lock);
  692. if (cfg_get_task(chpid) == cfg_deconfigure)
  693. cfg_set_task(chpid, cfg_none);
  694. spin_unlock(&cfg_lock);
  695. }
  696. static bool cfg_idle(void)
  697. {
  698. struct chp_id chpid;
  699. enum cfg_task_t t;
  700. spin_lock(&cfg_lock);
  701. t = chp_cfg_fetch_task(&chpid);
  702. spin_unlock(&cfg_lock);
  703. return t == cfg_none;
  704. }
  705. static int cfg_wait_idle(void)
  706. {
  707. if (wait_event_interruptible(cfg_wait_queue, cfg_idle()))
  708. return -ERESTARTSYS;
  709. return 0;
  710. }
  711. static int __init chp_init(void)
  712. {
  713. struct chp_id chpid;
  714. int state, ret;
  715. ret = crw_register_handler(CRW_RSC_CPATH, chp_process_crw);
  716. if (ret)
  717. return ret;
  718. INIT_WORK(&cfg_work, cfg_func);
  719. if (info_update())
  720. return 0;
  721. /* Register available channel-paths. */
  722. chp_id_for_each(&chpid) {
  723. state = chp_info_get_status(chpid);
  724. if (state == CHP_STATUS_CONFIGURED ||
  725. state == CHP_STATUS_STANDBY)
  726. chp_new(chpid);
  727. }
  728. return 0;
  729. }
  730. subsys_initcall(chp_init);