chp.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /*
  2. * drivers/s390/cio/chp.c
  3. *
  4. * Copyright IBM Corp. 1999,2007
  5. * Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
  6. * Arnd Bergmann (arndb@de.ibm.com)
  7. * Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
  8. */
  9. #include <linux/bug.h>
  10. #include <linux/workqueue.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/init.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/wait.h>
  15. #include <linux/mutex.h>
  16. #include <asm/errno.h>
  17. #include <asm/chpid.h>
  18. #include <asm/sclp.h>
  19. #include "cio.h"
  20. #include "css.h"
  21. #include "ioasm.h"
  22. #include "cio_debug.h"
  23. #include "chp.h"
  24. #define to_channelpath(device) container_of(device, struct channel_path, dev)
  25. #define CHP_INFO_UPDATE_INTERVAL 1*HZ
  26. enum cfg_task_t {
  27. cfg_none,
  28. cfg_configure,
  29. cfg_deconfigure
  30. };
  31. /* Map for pending configure tasks. */
  32. static enum cfg_task_t chp_cfg_task[__MAX_CSSID + 1][__MAX_CHPID + 1];
  33. static DEFINE_MUTEX(cfg_lock);
  34. static int cfg_busy;
  35. /* Map for channel-path status. */
  36. static struct sclp_chp_info chp_info;
  37. static DEFINE_MUTEX(info_lock);
  38. /* Time after which channel-path status may be outdated. */
  39. static unsigned long chp_info_expires;
  40. /* Workqueue to perform pending configure tasks. */
  41. static struct workqueue_struct *chp_wq;
  42. static struct work_struct cfg_work;
  43. /* Wait queue for configure completion events. */
  44. static wait_queue_head_t cfg_wait_queue;
  45. /* Return channel_path struct for given chpid. */
  46. static inline struct channel_path *chpid_to_chp(struct chp_id chpid)
  47. {
  48. return css[chpid.cssid]->chps[chpid.id];
  49. }
  50. /* Set vary state for given chpid. */
  51. static void set_chp_logically_online(struct chp_id chpid, int onoff)
  52. {
  53. chpid_to_chp(chpid)->state = onoff;
  54. }
  55. /* On succes return 0 if channel-path is varied offline, 1 if it is varied
  56. * online. Return -ENODEV if channel-path is not registered. */
  57. int chp_get_status(struct chp_id chpid)
  58. {
  59. return (chpid_to_chp(chpid) ? chpid_to_chp(chpid)->state : -ENODEV);
  60. }
  61. /**
  62. * chp_get_sch_opm - return opm for subchannel
  63. * @sch: subchannel
  64. *
  65. * Calculate and return the operational path mask (opm) based on the chpids
  66. * used by the subchannel and the status of the associated channel-paths.
  67. */
  68. u8 chp_get_sch_opm(struct subchannel *sch)
  69. {
  70. struct chp_id chpid;
  71. int opm;
  72. int i;
  73. opm = 0;
  74. chp_id_init(&chpid);
  75. for (i=0; i < 8; i++) {
  76. opm <<= 1;
  77. chpid.id = sch->schib.pmcw.chpid[i];
  78. if (chp_get_status(chpid) != 0)
  79. opm |= 1;
  80. }
  81. return opm;
  82. }
  83. /**
  84. * chp_is_registered - check if a channel-path is registered
  85. * @chpid: channel-path ID
  86. *
  87. * Return non-zero if a channel-path with the given chpid is registered,
  88. * zero otherwise.
  89. */
  90. int chp_is_registered(struct chp_id chpid)
  91. {
  92. return chpid_to_chp(chpid) != NULL;
  93. }
  94. /*
  95. * Function: s390_vary_chpid
  96. * Varies the specified chpid online or offline
  97. */
  98. static int s390_vary_chpid(struct chp_id chpid, int on)
  99. {
  100. char dbf_text[15];
  101. int status;
  102. sprintf(dbf_text, on?"varyon%x.%02x":"varyoff%x.%02x", chpid.cssid,
  103. chpid.id);
  104. CIO_TRACE_EVENT( 2, dbf_text);
  105. status = chp_get_status(chpid);
  106. if (!on && !status) {
  107. printk(KERN_ERR "cio: chpid %x.%02x is already offline\n",
  108. chpid.cssid, chpid.id);
  109. return -EINVAL;
  110. }
  111. set_chp_logically_online(chpid, on);
  112. chsc_chp_vary(chpid, on);
  113. return 0;
  114. }
  115. /*
  116. * Channel measurement related functions
  117. */
  118. static ssize_t chp_measurement_chars_read(struct kobject *kobj,
  119. struct bin_attribute *bin_attr,
  120. char *buf, loff_t off, size_t count)
  121. {
  122. struct channel_path *chp;
  123. unsigned int size;
  124. chp = to_channelpath(container_of(kobj, struct device, kobj));
  125. if (!chp->cmg_chars)
  126. return 0;
  127. size = sizeof(struct cmg_chars);
  128. if (off > size)
  129. return 0;
  130. if (off + count > size)
  131. count = size - off;
  132. memcpy(buf, chp->cmg_chars + off, count);
  133. return count;
  134. }
  135. static struct bin_attribute chp_measurement_chars_attr = {
  136. .attr = {
  137. .name = "measurement_chars",
  138. .mode = S_IRUSR,
  139. },
  140. .size = sizeof(struct cmg_chars),
  141. .read = chp_measurement_chars_read,
  142. };
  143. static void chp_measurement_copy_block(struct cmg_entry *buf,
  144. struct channel_subsystem *css,
  145. struct chp_id chpid)
  146. {
  147. void *area;
  148. struct cmg_entry *entry, reference_buf;
  149. int idx;
  150. if (chpid.id < 128) {
  151. area = css->cub_addr1;
  152. idx = chpid.id;
  153. } else {
  154. area = css->cub_addr2;
  155. idx = chpid.id - 128;
  156. }
  157. entry = area + (idx * sizeof(struct cmg_entry));
  158. do {
  159. memcpy(buf, entry, sizeof(*entry));
  160. memcpy(&reference_buf, entry, sizeof(*entry));
  161. } while (reference_buf.values[0] != buf->values[0]);
  162. }
  163. static ssize_t chp_measurement_read(struct kobject *kobj,
  164. struct bin_attribute *bin_attr,
  165. char *buf, loff_t off, size_t count)
  166. {
  167. struct channel_path *chp;
  168. struct channel_subsystem *css;
  169. unsigned int size;
  170. chp = to_channelpath(container_of(kobj, struct device, kobj));
  171. css = to_css(chp->dev.parent);
  172. size = sizeof(struct cmg_entry);
  173. /* Only allow single reads. */
  174. if (off || count < size)
  175. return 0;
  176. chp_measurement_copy_block((struct cmg_entry *)buf, css, chp->chpid);
  177. count = size;
  178. return count;
  179. }
  180. static struct bin_attribute chp_measurement_attr = {
  181. .attr = {
  182. .name = "measurement",
  183. .mode = S_IRUSR,
  184. },
  185. .size = sizeof(struct cmg_entry),
  186. .read = chp_measurement_read,
  187. };
  188. void chp_remove_cmg_attr(struct channel_path *chp)
  189. {
  190. device_remove_bin_file(&chp->dev, &chp_measurement_chars_attr);
  191. device_remove_bin_file(&chp->dev, &chp_measurement_attr);
  192. }
  193. int chp_add_cmg_attr(struct channel_path *chp)
  194. {
  195. int ret;
  196. ret = device_create_bin_file(&chp->dev, &chp_measurement_chars_attr);
  197. if (ret)
  198. return ret;
  199. ret = device_create_bin_file(&chp->dev, &chp_measurement_attr);
  200. if (ret)
  201. device_remove_bin_file(&chp->dev, &chp_measurement_chars_attr);
  202. return ret;
  203. }
  204. /*
  205. * Files for the channel path entries.
  206. */
  207. static ssize_t chp_status_show(struct device *dev,
  208. struct device_attribute *attr, char *buf)
  209. {
  210. struct channel_path *chp = container_of(dev, struct channel_path, dev);
  211. if (!chp)
  212. return 0;
  213. return (chp_get_status(chp->chpid) ? sprintf(buf, "online\n") :
  214. sprintf(buf, "offline\n"));
  215. }
  216. static ssize_t chp_status_write(struct device *dev,
  217. struct device_attribute *attr,
  218. const char *buf, size_t count)
  219. {
  220. struct channel_path *cp = container_of(dev, struct channel_path, dev);
  221. char cmd[10];
  222. int num_args;
  223. int error;
  224. num_args = sscanf(buf, "%5s", cmd);
  225. if (!num_args)
  226. return count;
  227. if (!strnicmp(cmd, "on", 2) || !strcmp(cmd, "1"))
  228. error = s390_vary_chpid(cp->chpid, 1);
  229. else if (!strnicmp(cmd, "off", 3) || !strcmp(cmd, "0"))
  230. error = s390_vary_chpid(cp->chpid, 0);
  231. else
  232. error = -EINVAL;
  233. return error < 0 ? error : count;
  234. }
  235. static DEVICE_ATTR(status, 0644, chp_status_show, chp_status_write);
  236. static ssize_t chp_configure_show(struct device *dev,
  237. struct device_attribute *attr, char *buf)
  238. {
  239. struct channel_path *cp;
  240. int status;
  241. cp = container_of(dev, struct channel_path, dev);
  242. status = chp_info_get_status(cp->chpid);
  243. if (status < 0)
  244. return status;
  245. return snprintf(buf, PAGE_SIZE, "%d\n", status);
  246. }
  247. static int cfg_wait_idle(void);
  248. static ssize_t chp_configure_write(struct device *dev,
  249. struct device_attribute *attr,
  250. const char *buf, size_t count)
  251. {
  252. struct channel_path *cp;
  253. int val;
  254. char delim;
  255. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  256. return -EINVAL;
  257. if (val != 0 && val != 1)
  258. return -EINVAL;
  259. cp = container_of(dev, struct channel_path, dev);
  260. chp_cfg_schedule(cp->chpid, val);
  261. cfg_wait_idle();
  262. return count;
  263. }
  264. static DEVICE_ATTR(configure, 0644, chp_configure_show, chp_configure_write);
  265. static ssize_t chp_type_show(struct device *dev, struct device_attribute *attr,
  266. char *buf)
  267. {
  268. struct channel_path *chp = container_of(dev, struct channel_path, dev);
  269. if (!chp)
  270. return 0;
  271. return sprintf(buf, "%x\n", chp->desc.desc);
  272. }
  273. static DEVICE_ATTR(type, 0444, chp_type_show, NULL);
  274. static ssize_t chp_cmg_show(struct device *dev, struct device_attribute *attr,
  275. char *buf)
  276. {
  277. struct channel_path *chp = to_channelpath(dev);
  278. if (!chp)
  279. return 0;
  280. if (chp->cmg == -1) /* channel measurements not available */
  281. return sprintf(buf, "unknown\n");
  282. return sprintf(buf, "%x\n", chp->cmg);
  283. }
  284. static DEVICE_ATTR(cmg, 0444, chp_cmg_show, NULL);
  285. static ssize_t chp_shared_show(struct device *dev,
  286. struct device_attribute *attr, char *buf)
  287. {
  288. struct channel_path *chp = to_channelpath(dev);
  289. if (!chp)
  290. return 0;
  291. if (chp->shared == -1) /* channel measurements not available */
  292. return sprintf(buf, "unknown\n");
  293. return sprintf(buf, "%x\n", chp->shared);
  294. }
  295. static DEVICE_ATTR(shared, 0444, chp_shared_show, NULL);
  296. static struct attribute * chp_attrs[] = {
  297. &dev_attr_status.attr,
  298. &dev_attr_configure.attr,
  299. &dev_attr_type.attr,
  300. &dev_attr_cmg.attr,
  301. &dev_attr_shared.attr,
  302. NULL,
  303. };
  304. static struct attribute_group chp_attr_group = {
  305. .attrs = chp_attrs,
  306. };
  307. static void chp_release(struct device *dev)
  308. {
  309. struct channel_path *cp;
  310. cp = container_of(dev, struct channel_path, dev);
  311. kfree(cp);
  312. }
  313. /**
  314. * chp_new - register a new channel-path
  315. * @chpid - channel-path ID
  316. *
  317. * Create and register data structure representing new channel-path. Return
  318. * zero on success, non-zero otherwise.
  319. */
  320. int chp_new(struct chp_id chpid)
  321. {
  322. struct channel_path *chp;
  323. int ret;
  324. if (chp_is_registered(chpid))
  325. return 0;
  326. chp = kzalloc(sizeof(struct channel_path), GFP_KERNEL);
  327. if (!chp)
  328. return -ENOMEM;
  329. /* fill in status, etc. */
  330. chp->chpid = chpid;
  331. chp->state = 1;
  332. chp->dev.parent = &css[chpid.cssid]->device;
  333. chp->dev.release = chp_release;
  334. snprintf(chp->dev.bus_id, BUS_ID_SIZE, "chp%x.%02x", chpid.cssid,
  335. chpid.id);
  336. /* Obtain channel path description and fill it in. */
  337. ret = chsc_determine_channel_path_description(chpid, &chp->desc);
  338. if (ret)
  339. goto out_free;
  340. if ((chp->desc.flags & 0x80) == 0) {
  341. ret = -ENODEV;
  342. goto out_free;
  343. }
  344. /* Get channel-measurement characteristics. */
  345. if (css_characteristics_avail && css_chsc_characteristics.scmc
  346. && css_chsc_characteristics.secm) {
  347. ret = chsc_get_channel_measurement_chars(chp);
  348. if (ret)
  349. goto out_free;
  350. } else {
  351. chp->cmg = -1;
  352. }
  353. /* make it known to the system */
  354. ret = device_register(&chp->dev);
  355. if (ret) {
  356. CIO_MSG_EVENT(0, "Could not register chp%x.%02x: %d\n",
  357. chpid.cssid, chpid.id, ret);
  358. goto out_free;
  359. }
  360. ret = sysfs_create_group(&chp->dev.kobj, &chp_attr_group);
  361. if (ret) {
  362. device_unregister(&chp->dev);
  363. goto out_free;
  364. }
  365. mutex_lock(&css[chpid.cssid]->mutex);
  366. if (css[chpid.cssid]->cm_enabled) {
  367. ret = chp_add_cmg_attr(chp);
  368. if (ret) {
  369. sysfs_remove_group(&chp->dev.kobj, &chp_attr_group);
  370. device_unregister(&chp->dev);
  371. mutex_unlock(&css[chpid.cssid]->mutex);
  372. goto out_free;
  373. }
  374. }
  375. css[chpid.cssid]->chps[chpid.id] = chp;
  376. mutex_unlock(&css[chpid.cssid]->mutex);
  377. return ret;
  378. out_free:
  379. kfree(chp);
  380. return ret;
  381. }
  382. /**
  383. * chp_get_chp_desc - return newly allocated channel-path description
  384. * @chpid: channel-path ID
  385. *
  386. * On success return a newly allocated copy of the channel-path description
  387. * data associated with the given channel-path ID. Return %NULL on error.
  388. */
  389. void *chp_get_chp_desc(struct chp_id chpid)
  390. {
  391. struct channel_path *chp;
  392. struct channel_path_desc *desc;
  393. chp = chpid_to_chp(chpid);
  394. if (!chp)
  395. return NULL;
  396. desc = kmalloc(sizeof(struct channel_path_desc), GFP_KERNEL);
  397. if (!desc)
  398. return NULL;
  399. memcpy(desc, &chp->desc, sizeof(struct channel_path_desc));
  400. return desc;
  401. }
  402. /**
  403. * chp_process_crw - process channel-path status change
  404. * @id: channel-path ID number
  405. * @status: non-zero if channel-path has become available, zero otherwise
  406. *
  407. * Handle channel-report-words indicating that the status of a channel-path
  408. * has changed.
  409. */
  410. void chp_process_crw(int id, int status)
  411. {
  412. struct chp_id chpid;
  413. chp_id_init(&chpid);
  414. chpid.id = id;
  415. if (status) {
  416. if (!chp_is_registered(chpid))
  417. chp_new(chpid);
  418. chsc_chp_online(chpid);
  419. } else
  420. chsc_chp_offline(chpid);
  421. }
  422. static inline int info_bit_num(struct chp_id id)
  423. {
  424. return id.id + id.cssid * (__MAX_CHPID + 1);
  425. }
  426. /* Force chp_info refresh on next call to info_validate(). */
  427. static void info_expire(void)
  428. {
  429. mutex_lock(&info_lock);
  430. chp_info_expires = jiffies - 1;
  431. mutex_unlock(&info_lock);
  432. }
  433. /* Ensure that chp_info is up-to-date. */
  434. static int info_update(void)
  435. {
  436. int rc;
  437. mutex_lock(&info_lock);
  438. rc = 0;
  439. if (time_after(jiffies, chp_info_expires)) {
  440. /* Data is too old, update. */
  441. rc = sclp_chp_read_info(&chp_info);
  442. chp_info_expires = jiffies + CHP_INFO_UPDATE_INTERVAL ;
  443. }
  444. mutex_unlock(&info_lock);
  445. return rc;
  446. }
  447. /**
  448. * chp_info_get_status - retrieve configure status of a channel-path
  449. * @chpid: channel-path ID
  450. *
  451. * On success, return 0 for standby, 1 for configured, 2 for reserved,
  452. * 3 for not recognized. Return negative error code on error.
  453. */
  454. int chp_info_get_status(struct chp_id chpid)
  455. {
  456. int rc;
  457. int bit;
  458. rc = info_update();
  459. if (rc)
  460. return rc;
  461. bit = info_bit_num(chpid);
  462. mutex_lock(&info_lock);
  463. if (!chp_test_bit(chp_info.recognized, bit))
  464. rc = CHP_STATUS_NOT_RECOGNIZED;
  465. else if (chp_test_bit(chp_info.configured, bit))
  466. rc = CHP_STATUS_CONFIGURED;
  467. else if (chp_test_bit(chp_info.standby, bit))
  468. rc = CHP_STATUS_STANDBY;
  469. else
  470. rc = CHP_STATUS_RESERVED;
  471. mutex_unlock(&info_lock);
  472. return rc;
  473. }
  474. /* Return configure task for chpid. */
  475. static enum cfg_task_t cfg_get_task(struct chp_id chpid)
  476. {
  477. return chp_cfg_task[chpid.cssid][chpid.id];
  478. }
  479. /* Set configure task for chpid. */
  480. static void cfg_set_task(struct chp_id chpid, enum cfg_task_t cfg)
  481. {
  482. chp_cfg_task[chpid.cssid][chpid.id] = cfg;
  483. }
  484. /* Perform one configure/deconfigure request. Reschedule work function until
  485. * last request. */
  486. static void cfg_func(struct work_struct *work)
  487. {
  488. struct chp_id chpid;
  489. enum cfg_task_t t;
  490. mutex_lock(&cfg_lock);
  491. t = cfg_none;
  492. chp_id_for_each(&chpid) {
  493. t = cfg_get_task(chpid);
  494. if (t != cfg_none) {
  495. cfg_set_task(chpid, cfg_none);
  496. break;
  497. }
  498. }
  499. mutex_unlock(&cfg_lock);
  500. switch (t) {
  501. case cfg_configure:
  502. sclp_chp_configure(chpid);
  503. info_expire();
  504. chsc_chp_online(chpid);
  505. break;
  506. case cfg_deconfigure:
  507. sclp_chp_deconfigure(chpid);
  508. info_expire();
  509. chsc_chp_offline(chpid);
  510. break;
  511. case cfg_none:
  512. /* Get updated information after last change. */
  513. info_update();
  514. mutex_lock(&cfg_lock);
  515. cfg_busy = 0;
  516. mutex_unlock(&cfg_lock);
  517. wake_up_interruptible(&cfg_wait_queue);
  518. return;
  519. }
  520. queue_work(chp_wq, &cfg_work);
  521. }
  522. /**
  523. * chp_cfg_schedule - schedule chpid configuration request
  524. * @chpid - channel-path ID
  525. * @configure - Non-zero for configure, zero for deconfigure
  526. *
  527. * Schedule a channel-path configuration/deconfiguration request.
  528. */
  529. void chp_cfg_schedule(struct chp_id chpid, int configure)
  530. {
  531. CIO_MSG_EVENT(2, "chp_cfg_sched%x.%02x=%d\n", chpid.cssid, chpid.id,
  532. configure);
  533. mutex_lock(&cfg_lock);
  534. cfg_set_task(chpid, configure ? cfg_configure : cfg_deconfigure);
  535. cfg_busy = 1;
  536. mutex_unlock(&cfg_lock);
  537. queue_work(chp_wq, &cfg_work);
  538. }
  539. /**
  540. * chp_cfg_cancel_deconfigure - cancel chpid deconfiguration request
  541. * @chpid - channel-path ID
  542. *
  543. * Cancel an active channel-path deconfiguration request if it has not yet
  544. * been performed.
  545. */
  546. void chp_cfg_cancel_deconfigure(struct chp_id chpid)
  547. {
  548. CIO_MSG_EVENT(2, "chp_cfg_cancel:%x.%02x\n", chpid.cssid, chpid.id);
  549. mutex_lock(&cfg_lock);
  550. if (cfg_get_task(chpid) == cfg_deconfigure)
  551. cfg_set_task(chpid, cfg_none);
  552. mutex_unlock(&cfg_lock);
  553. }
  554. static int cfg_wait_idle(void)
  555. {
  556. if (wait_event_interruptible(cfg_wait_queue, !cfg_busy))
  557. return -ERESTARTSYS;
  558. return 0;
  559. }
  560. static int __init chp_init(void)
  561. {
  562. struct chp_id chpid;
  563. chp_wq = create_singlethread_workqueue("cio_chp");
  564. if (!chp_wq)
  565. return -ENOMEM;
  566. INIT_WORK(&cfg_work, cfg_func);
  567. init_waitqueue_head(&cfg_wait_queue);
  568. if (info_update())
  569. return 0;
  570. /* Register available channel-paths. */
  571. chp_id_for_each(&chpid) {
  572. if (chp_info_get_status(chpid) != CHP_STATUS_NOT_RECOGNIZED)
  573. chp_new(chpid);
  574. }
  575. return 0;
  576. }
  577. subsys_initcall(chp_init);