chp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 1999, 2010
  4. * Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
  5. * Arnd Bergmann (arndb@de.ibm.com)
  6. * Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
  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 wait_queue_head_t 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 = container_of(kobj, struct device, 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 = container_of(kobj, struct device, 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. if (!strncasecmp(cmd, "on", 2) || !strcmp(cmd, "1")) {
  221. mutex_lock(&cp->lock);
  222. error = s390_vary_chpid(cp->chpid, 1);
  223. mutex_unlock(&cp->lock);
  224. } else if (!strncasecmp(cmd, "off", 3) || !strcmp(cmd, "0")) {
  225. mutex_lock(&cp->lock);
  226. error = s390_vary_chpid(cp->chpid, 0);
  227. mutex_unlock(&cp->lock);
  228. } else
  229. error = -EINVAL;
  230. return error < 0 ? error : count;
  231. }
  232. static DEVICE_ATTR(status, 0644, chp_status_show, chp_status_write);
  233. static ssize_t chp_configure_show(struct device *dev,
  234. struct device_attribute *attr, char *buf)
  235. {
  236. struct channel_path *cp;
  237. int status;
  238. cp = to_channelpath(dev);
  239. status = chp_info_get_status(cp->chpid);
  240. if (status < 0)
  241. return status;
  242. return snprintf(buf, PAGE_SIZE, "%d\n", status);
  243. }
  244. static int cfg_wait_idle(void);
  245. static ssize_t chp_configure_write(struct device *dev,
  246. struct device_attribute *attr,
  247. const char *buf, size_t count)
  248. {
  249. struct channel_path *cp;
  250. int val;
  251. char delim;
  252. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  253. return -EINVAL;
  254. if (val != 0 && val != 1)
  255. return -EINVAL;
  256. cp = to_channelpath(dev);
  257. chp_cfg_schedule(cp->chpid, val);
  258. cfg_wait_idle();
  259. return count;
  260. }
  261. static DEVICE_ATTR(configure, 0644, chp_configure_show, chp_configure_write);
  262. static ssize_t chp_type_show(struct device *dev, struct device_attribute *attr,
  263. char *buf)
  264. {
  265. struct channel_path *chp = to_channelpath(dev);
  266. u8 type;
  267. mutex_lock(&chp->lock);
  268. type = chp->desc.desc;
  269. mutex_unlock(&chp->lock);
  270. return sprintf(buf, "%x\n", type);
  271. }
  272. static DEVICE_ATTR(type, 0444, chp_type_show, NULL);
  273. static ssize_t chp_cmg_show(struct device *dev, struct device_attribute *attr,
  274. char *buf)
  275. {
  276. struct channel_path *chp = to_channelpath(dev);
  277. if (!chp)
  278. return 0;
  279. if (chp->cmg == -1) /* channel measurements not available */
  280. return sprintf(buf, "unknown\n");
  281. return sprintf(buf, "%x\n", chp->cmg);
  282. }
  283. static DEVICE_ATTR(cmg, 0444, chp_cmg_show, NULL);
  284. static ssize_t chp_shared_show(struct device *dev,
  285. struct device_attribute *attr, char *buf)
  286. {
  287. struct channel_path *chp = to_channelpath(dev);
  288. if (!chp)
  289. return 0;
  290. if (chp->shared == -1) /* channel measurements not available */
  291. return sprintf(buf, "unknown\n");
  292. return sprintf(buf, "%x\n", chp->shared);
  293. }
  294. static DEVICE_ATTR(shared, 0444, chp_shared_show, NULL);
  295. static ssize_t chp_chid_show(struct device *dev, struct device_attribute *attr,
  296. char *buf)
  297. {
  298. struct channel_path *chp = to_channelpath(dev);
  299. ssize_t rc;
  300. mutex_lock(&chp->lock);
  301. if (chp->desc_fmt1.flags & 0x10)
  302. rc = sprintf(buf, "%04x\n", chp->desc_fmt1.chid);
  303. else
  304. rc = 0;
  305. mutex_unlock(&chp->lock);
  306. return rc;
  307. }
  308. static DEVICE_ATTR(chid, 0444, chp_chid_show, NULL);
  309. static ssize_t chp_chid_external_show(struct device *dev,
  310. struct device_attribute *attr, char *buf)
  311. {
  312. struct channel_path *chp = to_channelpath(dev);
  313. ssize_t rc;
  314. mutex_lock(&chp->lock);
  315. if (chp->desc_fmt1.flags & 0x10)
  316. rc = sprintf(buf, "%x\n", chp->desc_fmt1.flags & 0x8 ? 1 : 0);
  317. else
  318. rc = 0;
  319. mutex_unlock(&chp->lock);
  320. return rc;
  321. }
  322. static DEVICE_ATTR(chid_external, 0444, chp_chid_external_show, NULL);
  323. static ssize_t util_string_read(struct file *filp, struct kobject *kobj,
  324. struct bin_attribute *attr, char *buf,
  325. loff_t off, size_t count)
  326. {
  327. struct channel_path *chp = to_channelpath(kobj_to_dev(kobj));
  328. ssize_t rc;
  329. mutex_lock(&chp->lock);
  330. rc = memory_read_from_buffer(buf, count, &off, chp->desc_fmt3.util_str,
  331. sizeof(chp->desc_fmt3.util_str));
  332. mutex_unlock(&chp->lock);
  333. return rc;
  334. }
  335. static BIN_ATTR_RO(util_string,
  336. sizeof(((struct channel_path_desc_fmt3 *)0)->util_str));
  337. static struct bin_attribute *chp_bin_attrs[] = {
  338. &bin_attr_util_string,
  339. NULL,
  340. };
  341. static struct attribute *chp_attrs[] = {
  342. &dev_attr_status.attr,
  343. &dev_attr_configure.attr,
  344. &dev_attr_type.attr,
  345. &dev_attr_cmg.attr,
  346. &dev_attr_shared.attr,
  347. &dev_attr_chid.attr,
  348. &dev_attr_chid_external.attr,
  349. NULL,
  350. };
  351. static struct attribute_group chp_attr_group = {
  352. .attrs = chp_attrs,
  353. .bin_attrs = chp_bin_attrs,
  354. };
  355. static const struct attribute_group *chp_attr_groups[] = {
  356. &chp_attr_group,
  357. NULL,
  358. };
  359. static void chp_release(struct device *dev)
  360. {
  361. struct channel_path *cp;
  362. cp = to_channelpath(dev);
  363. kfree(cp);
  364. }
  365. /**
  366. * chp_update_desc - update channel-path description
  367. * @chp: channel-path
  368. *
  369. * Update the channel-path description of the specified channel-path
  370. * including channel measurement related information.
  371. * Return zero on success, non-zero otherwise.
  372. */
  373. int chp_update_desc(struct channel_path *chp)
  374. {
  375. int rc;
  376. rc = chsc_determine_fmt0_channel_path_desc(chp->chpid, &chp->desc);
  377. if (rc)
  378. return rc;
  379. /*
  380. * Fetching the following data is optional. Not all machines or
  381. * hypervisors implement the required chsc commands.
  382. */
  383. chsc_determine_fmt1_channel_path_desc(chp->chpid, &chp->desc_fmt1);
  384. chsc_determine_fmt3_channel_path_desc(chp->chpid, &chp->desc_fmt3);
  385. chsc_get_channel_measurement_chars(chp);
  386. return 0;
  387. }
  388. /**
  389. * chp_new - register a new channel-path
  390. * @chpid: channel-path ID
  391. *
  392. * Create and register data structure representing new channel-path. Return
  393. * zero on success, non-zero otherwise.
  394. */
  395. int chp_new(struct chp_id chpid)
  396. {
  397. struct channel_subsystem *css = css_by_id(chpid.cssid);
  398. struct channel_path *chp;
  399. int ret = 0;
  400. mutex_lock(&css->mutex);
  401. if (chp_is_registered(chpid))
  402. goto out;
  403. chp = kzalloc(sizeof(struct channel_path), GFP_KERNEL);
  404. if (!chp) {
  405. ret = -ENOMEM;
  406. goto out;
  407. }
  408. /* fill in status, etc. */
  409. chp->chpid = chpid;
  410. chp->state = 1;
  411. chp->dev.parent = &css->device;
  412. chp->dev.groups = chp_attr_groups;
  413. chp->dev.release = chp_release;
  414. mutex_init(&chp->lock);
  415. /* Obtain channel path description and fill it in. */
  416. ret = chp_update_desc(chp);
  417. if (ret)
  418. goto out_free;
  419. if ((chp->desc.flags & 0x80) == 0) {
  420. ret = -ENODEV;
  421. goto out_free;
  422. }
  423. dev_set_name(&chp->dev, "chp%x.%02x", chpid.cssid, chpid.id);
  424. /* make it known to the system */
  425. ret = device_register(&chp->dev);
  426. if (ret) {
  427. CIO_MSG_EVENT(0, "Could not register chp%x.%02x: %d\n",
  428. chpid.cssid, chpid.id, ret);
  429. put_device(&chp->dev);
  430. goto out;
  431. }
  432. if (css->cm_enabled) {
  433. ret = chp_add_cmg_attr(chp);
  434. if (ret) {
  435. device_unregister(&chp->dev);
  436. goto out;
  437. }
  438. }
  439. css->chps[chpid.id] = chp;
  440. goto out;
  441. out_free:
  442. kfree(chp);
  443. out:
  444. mutex_unlock(&css->mutex);
  445. return ret;
  446. }
  447. /**
  448. * chp_get_chp_desc - return newly allocated channel-path description
  449. * @chpid: channel-path ID
  450. *
  451. * On success return a newly allocated copy of the channel-path description
  452. * data associated with the given channel-path ID. Return %NULL on error.
  453. */
  454. struct channel_path_desc_fmt0 *chp_get_chp_desc(struct chp_id chpid)
  455. {
  456. struct channel_path *chp;
  457. struct channel_path_desc_fmt0 *desc;
  458. chp = chpid_to_chp(chpid);
  459. if (!chp)
  460. return NULL;
  461. desc = kmalloc(sizeof(*desc), GFP_KERNEL);
  462. if (!desc)
  463. return NULL;
  464. mutex_lock(&chp->lock);
  465. memcpy(desc, &chp->desc, sizeof(*desc));
  466. mutex_unlock(&chp->lock);
  467. return desc;
  468. }
  469. /**
  470. * chp_process_crw - process channel-path status change
  471. * @crw0: channel report-word to handler
  472. * @crw1: second channel-report word (always NULL)
  473. * @overflow: crw overflow indication
  474. *
  475. * Handle channel-report-words indicating that the status of a channel-path
  476. * has changed.
  477. */
  478. static void chp_process_crw(struct crw *crw0, struct crw *crw1,
  479. int overflow)
  480. {
  481. struct chp_id chpid;
  482. if (overflow) {
  483. css_schedule_eval_all();
  484. return;
  485. }
  486. CIO_CRW_EVENT(2, "CRW reports slct=%d, oflw=%d, "
  487. "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
  488. crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
  489. crw0->erc, crw0->rsid);
  490. /*
  491. * Check for solicited machine checks. These are
  492. * created by reset channel path and need not be
  493. * handled here.
  494. */
  495. if (crw0->slct) {
  496. CIO_CRW_EVENT(2, "solicited machine check for "
  497. "channel path %02X\n", crw0->rsid);
  498. return;
  499. }
  500. chp_id_init(&chpid);
  501. chpid.id = crw0->rsid;
  502. switch (crw0->erc) {
  503. case CRW_ERC_IPARM: /* Path has come. */
  504. case CRW_ERC_INIT:
  505. chp_new(chpid);
  506. chsc_chp_online(chpid);
  507. break;
  508. case CRW_ERC_PERRI: /* Path has gone. */
  509. case CRW_ERC_PERRN:
  510. chsc_chp_offline(chpid);
  511. break;
  512. default:
  513. CIO_CRW_EVENT(2, "Don't know how to handle erc=%x\n",
  514. crw0->erc);
  515. }
  516. }
  517. int chp_ssd_get_mask(struct chsc_ssd_info *ssd, struct chp_link *link)
  518. {
  519. int i;
  520. int mask;
  521. for (i = 0; i < 8; i++) {
  522. mask = 0x80 >> i;
  523. if (!(ssd->path_mask & mask))
  524. continue;
  525. if (!chp_id_is_equal(&ssd->chpid[i], &link->chpid))
  526. continue;
  527. if ((ssd->fla_valid_mask & mask) &&
  528. ((ssd->fla[i] & link->fla_mask) != link->fla))
  529. continue;
  530. return mask;
  531. }
  532. return 0;
  533. }
  534. EXPORT_SYMBOL_GPL(chp_ssd_get_mask);
  535. static inline int info_bit_num(struct chp_id id)
  536. {
  537. return id.id + id.cssid * (__MAX_CHPID + 1);
  538. }
  539. /* Force chp_info refresh on next call to info_validate(). */
  540. static void info_expire(void)
  541. {
  542. mutex_lock(&info_lock);
  543. chp_info_expires = jiffies - 1;
  544. mutex_unlock(&info_lock);
  545. }
  546. /* Ensure that chp_info is up-to-date. */
  547. static int info_update(void)
  548. {
  549. int rc;
  550. mutex_lock(&info_lock);
  551. rc = 0;
  552. if (time_after(jiffies, chp_info_expires)) {
  553. /* Data is too old, update. */
  554. rc = sclp_chp_read_info(&chp_info);
  555. chp_info_expires = jiffies + CHP_INFO_UPDATE_INTERVAL ;
  556. }
  557. mutex_unlock(&info_lock);
  558. return rc;
  559. }
  560. /**
  561. * chp_info_get_status - retrieve configure status of a channel-path
  562. * @chpid: channel-path ID
  563. *
  564. * On success, return 0 for standby, 1 for configured, 2 for reserved,
  565. * 3 for not recognized. Return negative error code on error.
  566. */
  567. int chp_info_get_status(struct chp_id chpid)
  568. {
  569. int rc;
  570. int bit;
  571. rc = info_update();
  572. if (rc)
  573. return rc;
  574. bit = info_bit_num(chpid);
  575. mutex_lock(&info_lock);
  576. if (!chp_test_bit(chp_info.recognized, bit))
  577. rc = CHP_STATUS_NOT_RECOGNIZED;
  578. else if (chp_test_bit(chp_info.configured, bit))
  579. rc = CHP_STATUS_CONFIGURED;
  580. else if (chp_test_bit(chp_info.standby, bit))
  581. rc = CHP_STATUS_STANDBY;
  582. else
  583. rc = CHP_STATUS_RESERVED;
  584. mutex_unlock(&info_lock);
  585. return rc;
  586. }
  587. /* Return configure task for chpid. */
  588. static enum cfg_task_t cfg_get_task(struct chp_id chpid)
  589. {
  590. return chp_cfg_task[chpid.cssid][chpid.id];
  591. }
  592. /* Set configure task for chpid. */
  593. static void cfg_set_task(struct chp_id chpid, enum cfg_task_t cfg)
  594. {
  595. chp_cfg_task[chpid.cssid][chpid.id] = cfg;
  596. }
  597. /* Fetch the first configure task. Set chpid accordingly. */
  598. static enum cfg_task_t chp_cfg_fetch_task(struct chp_id *chpid)
  599. {
  600. enum cfg_task_t t = cfg_none;
  601. chp_id_for_each(chpid) {
  602. t = cfg_get_task(*chpid);
  603. if (t != cfg_none)
  604. break;
  605. }
  606. return t;
  607. }
  608. /* Perform one configure/deconfigure request. Reschedule work function until
  609. * last request. */
  610. static void cfg_func(struct work_struct *work)
  611. {
  612. struct chp_id chpid;
  613. enum cfg_task_t t;
  614. int rc;
  615. spin_lock(&cfg_lock);
  616. t = chp_cfg_fetch_task(&chpid);
  617. spin_unlock(&cfg_lock);
  618. switch (t) {
  619. case cfg_configure:
  620. rc = sclp_chp_configure(chpid);
  621. if (rc)
  622. CIO_MSG_EVENT(2, "chp: sclp_chp_configure(%x.%02x)="
  623. "%d\n", chpid.cssid, chpid.id, rc);
  624. else {
  625. info_expire();
  626. chsc_chp_online(chpid);
  627. }
  628. break;
  629. case cfg_deconfigure:
  630. rc = sclp_chp_deconfigure(chpid);
  631. if (rc)
  632. CIO_MSG_EVENT(2, "chp: sclp_chp_deconfigure(%x.%02x)="
  633. "%d\n", chpid.cssid, chpid.id, rc);
  634. else {
  635. info_expire();
  636. chsc_chp_offline(chpid);
  637. }
  638. break;
  639. case cfg_none:
  640. /* Get updated information after last change. */
  641. info_update();
  642. wake_up_interruptible(&cfg_wait_queue);
  643. return;
  644. }
  645. spin_lock(&cfg_lock);
  646. if (t == cfg_get_task(chpid))
  647. cfg_set_task(chpid, cfg_none);
  648. spin_unlock(&cfg_lock);
  649. schedule_work(&cfg_work);
  650. }
  651. /**
  652. * chp_cfg_schedule - schedule chpid configuration request
  653. * @chpid: channel-path ID
  654. * @configure: Non-zero for configure, zero for deconfigure
  655. *
  656. * Schedule a channel-path configuration/deconfiguration request.
  657. */
  658. void chp_cfg_schedule(struct chp_id chpid, int configure)
  659. {
  660. CIO_MSG_EVENT(2, "chp_cfg_sched%x.%02x=%d\n", chpid.cssid, chpid.id,
  661. configure);
  662. spin_lock(&cfg_lock);
  663. cfg_set_task(chpid, configure ? cfg_configure : cfg_deconfigure);
  664. spin_unlock(&cfg_lock);
  665. schedule_work(&cfg_work);
  666. }
  667. /**
  668. * chp_cfg_cancel_deconfigure - cancel chpid deconfiguration request
  669. * @chpid: channel-path ID
  670. *
  671. * Cancel an active channel-path deconfiguration request if it has not yet
  672. * been performed.
  673. */
  674. void chp_cfg_cancel_deconfigure(struct chp_id chpid)
  675. {
  676. CIO_MSG_EVENT(2, "chp_cfg_cancel:%x.%02x\n", chpid.cssid, chpid.id);
  677. spin_lock(&cfg_lock);
  678. if (cfg_get_task(chpid) == cfg_deconfigure)
  679. cfg_set_task(chpid, cfg_none);
  680. spin_unlock(&cfg_lock);
  681. }
  682. static bool cfg_idle(void)
  683. {
  684. struct chp_id chpid;
  685. enum cfg_task_t t;
  686. spin_lock(&cfg_lock);
  687. t = chp_cfg_fetch_task(&chpid);
  688. spin_unlock(&cfg_lock);
  689. return t == cfg_none;
  690. }
  691. static int cfg_wait_idle(void)
  692. {
  693. if (wait_event_interruptible(cfg_wait_queue, cfg_idle()))
  694. return -ERESTARTSYS;
  695. return 0;
  696. }
  697. static int __init chp_init(void)
  698. {
  699. struct chp_id chpid;
  700. int state, ret;
  701. ret = crw_register_handler(CRW_RSC_CPATH, chp_process_crw);
  702. if (ret)
  703. return ret;
  704. INIT_WORK(&cfg_work, cfg_func);
  705. init_waitqueue_head(&cfg_wait_queue);
  706. if (info_update())
  707. return 0;
  708. /* Register available channel-paths. */
  709. chp_id_for_each(&chpid) {
  710. state = chp_info_get_status(chpid);
  711. if (state == CHP_STATUS_CONFIGURED ||
  712. state == CHP_STATUS_STANDBY)
  713. chp_new(chpid);
  714. }
  715. return 0;
  716. }
  717. subsys_initcall(chp_init);