chp.c 20 KB

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