chp.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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 (status < 0) {
  107. printk(KERN_ERR "Can't vary unknown chpid %x.%02x\n",
  108. chpid.cssid, chpid.id);
  109. return -EINVAL;
  110. }
  111. if (!on && !status) {
  112. printk(KERN_ERR "chpid %x.%02x is already offline\n",
  113. chpid.cssid, chpid.id);
  114. return -EINVAL;
  115. }
  116. set_chp_logically_online(chpid, on);
  117. chsc_chp_vary(chpid, on);
  118. return 0;
  119. }
  120. /*
  121. * Channel measurement related functions
  122. */
  123. static ssize_t chp_measurement_chars_read(struct kobject *kobj, char *buf,
  124. loff_t off, size_t count)
  125. {
  126. struct channel_path *chp;
  127. unsigned int size;
  128. chp = to_channelpath(container_of(kobj, struct device, kobj));
  129. if (!chp->cmg_chars)
  130. return 0;
  131. size = sizeof(struct cmg_chars);
  132. if (off > size)
  133. return 0;
  134. if (off + count > size)
  135. count = size - off;
  136. memcpy(buf, chp->cmg_chars + off, count);
  137. return count;
  138. }
  139. static struct bin_attribute chp_measurement_chars_attr = {
  140. .attr = {
  141. .name = "measurement_chars",
  142. .mode = S_IRUSR,
  143. .owner = THIS_MODULE,
  144. },
  145. .size = sizeof(struct cmg_chars),
  146. .read = chp_measurement_chars_read,
  147. };
  148. static void chp_measurement_copy_block(struct cmg_entry *buf,
  149. struct channel_subsystem *css,
  150. struct chp_id chpid)
  151. {
  152. void *area;
  153. struct cmg_entry *entry, reference_buf;
  154. int idx;
  155. if (chpid.id < 128) {
  156. area = css->cub_addr1;
  157. idx = chpid.id;
  158. } else {
  159. area = css->cub_addr2;
  160. idx = chpid.id - 128;
  161. }
  162. entry = area + (idx * sizeof(struct cmg_entry));
  163. do {
  164. memcpy(buf, entry, sizeof(*entry));
  165. memcpy(&reference_buf, entry, sizeof(*entry));
  166. } while (reference_buf.values[0] != buf->values[0]);
  167. }
  168. static ssize_t chp_measurement_read(struct kobject *kobj, char *buf,
  169. loff_t off, size_t count)
  170. {
  171. struct channel_path *chp;
  172. struct channel_subsystem *css;
  173. unsigned int size;
  174. chp = to_channelpath(container_of(kobj, struct device, kobj));
  175. css = to_css(chp->dev.parent);
  176. size = sizeof(struct cmg_entry);
  177. /* Only allow single reads. */
  178. if (off || count < size)
  179. return 0;
  180. chp_measurement_copy_block((struct cmg_entry *)buf, css, chp->chpid);
  181. count = size;
  182. return count;
  183. }
  184. static struct bin_attribute chp_measurement_attr = {
  185. .attr = {
  186. .name = "measurement",
  187. .mode = S_IRUSR,
  188. .owner = THIS_MODULE,
  189. },
  190. .size = sizeof(struct cmg_entry),
  191. .read = chp_measurement_read,
  192. };
  193. void chp_remove_cmg_attr(struct channel_path *chp)
  194. {
  195. device_remove_bin_file(&chp->dev, &chp_measurement_chars_attr);
  196. device_remove_bin_file(&chp->dev, &chp_measurement_attr);
  197. }
  198. int chp_add_cmg_attr(struct channel_path *chp)
  199. {
  200. int ret;
  201. ret = device_create_bin_file(&chp->dev, &chp_measurement_chars_attr);
  202. if (ret)
  203. return ret;
  204. ret = device_create_bin_file(&chp->dev, &chp_measurement_attr);
  205. if (ret)
  206. device_remove_bin_file(&chp->dev, &chp_measurement_chars_attr);
  207. return ret;
  208. }
  209. /*
  210. * Files for the channel path entries.
  211. */
  212. static ssize_t chp_status_show(struct device *dev,
  213. struct device_attribute *attr, char *buf)
  214. {
  215. struct channel_path *chp = container_of(dev, struct channel_path, dev);
  216. if (!chp)
  217. return 0;
  218. return (chp_get_status(chp->chpid) ? sprintf(buf, "online\n") :
  219. sprintf(buf, "offline\n"));
  220. }
  221. static ssize_t chp_status_write(struct device *dev,
  222. struct device_attribute *attr,
  223. const char *buf, size_t count)
  224. {
  225. struct channel_path *cp = container_of(dev, struct channel_path, dev);
  226. char cmd[10];
  227. int num_args;
  228. int error;
  229. num_args = sscanf(buf, "%5s", cmd);
  230. if (!num_args)
  231. return count;
  232. if (!strnicmp(cmd, "on", 2) || !strcmp(cmd, "1"))
  233. error = s390_vary_chpid(cp->chpid, 1);
  234. else if (!strnicmp(cmd, "off", 3) || !strcmp(cmd, "0"))
  235. error = s390_vary_chpid(cp->chpid, 0);
  236. else
  237. error = -EINVAL;
  238. return error < 0 ? error : count;
  239. }
  240. static DEVICE_ATTR(status, 0644, chp_status_show, chp_status_write);
  241. static ssize_t chp_configure_show(struct device *dev,
  242. struct device_attribute *attr, char *buf)
  243. {
  244. struct channel_path *cp;
  245. int status;
  246. cp = container_of(dev, struct channel_path, dev);
  247. status = chp_info_get_status(cp->chpid);
  248. if (status < 0)
  249. return status;
  250. return snprintf(buf, PAGE_SIZE, "%d\n", status);
  251. }
  252. static int cfg_wait_idle(void);
  253. static ssize_t chp_configure_write(struct device *dev,
  254. struct device_attribute *attr,
  255. const char *buf, size_t count)
  256. {
  257. struct channel_path *cp;
  258. int val;
  259. char delim;
  260. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  261. return -EINVAL;
  262. if (val != 0 && val != 1)
  263. return -EINVAL;
  264. cp = container_of(dev, struct channel_path, dev);
  265. chp_cfg_schedule(cp->chpid, val);
  266. cfg_wait_idle();
  267. return count;
  268. }
  269. static DEVICE_ATTR(configure, 0644, chp_configure_show, chp_configure_write);
  270. static ssize_t chp_type_show(struct device *dev, struct device_attribute *attr,
  271. char *buf)
  272. {
  273. struct channel_path *chp = container_of(dev, struct channel_path, dev);
  274. if (!chp)
  275. return 0;
  276. return sprintf(buf, "%x\n", chp->desc.desc);
  277. }
  278. static DEVICE_ATTR(type, 0444, chp_type_show, NULL);
  279. static ssize_t chp_cmg_show(struct device *dev, struct device_attribute *attr,
  280. char *buf)
  281. {
  282. struct channel_path *chp = to_channelpath(dev);
  283. if (!chp)
  284. return 0;
  285. if (chp->cmg == -1) /* channel measurements not available */
  286. return sprintf(buf, "unknown\n");
  287. return sprintf(buf, "%x\n", chp->cmg);
  288. }
  289. static DEVICE_ATTR(cmg, 0444, chp_cmg_show, NULL);
  290. static ssize_t chp_shared_show(struct device *dev,
  291. struct device_attribute *attr, char *buf)
  292. {
  293. struct channel_path *chp = to_channelpath(dev);
  294. if (!chp)
  295. return 0;
  296. if (chp->shared == -1) /* channel measurements not available */
  297. return sprintf(buf, "unknown\n");
  298. return sprintf(buf, "%x\n", chp->shared);
  299. }
  300. static DEVICE_ATTR(shared, 0444, chp_shared_show, NULL);
  301. static struct attribute * chp_attrs[] = {
  302. &dev_attr_status.attr,
  303. &dev_attr_configure.attr,
  304. &dev_attr_type.attr,
  305. &dev_attr_cmg.attr,
  306. &dev_attr_shared.attr,
  307. NULL,
  308. };
  309. static struct attribute_group chp_attr_group = {
  310. .attrs = chp_attrs,
  311. };
  312. static void chp_release(struct device *dev)
  313. {
  314. struct channel_path *cp;
  315. cp = container_of(dev, struct channel_path, dev);
  316. kfree(cp);
  317. }
  318. /**
  319. * chp_new - register a new channel-path
  320. * @chpid - channel-path ID
  321. *
  322. * Create and register data structure representing new channel-path. Return
  323. * zero on success, non-zero otherwise.
  324. */
  325. int chp_new(struct chp_id chpid)
  326. {
  327. struct channel_path *chp;
  328. int ret;
  329. if (chp_is_registered(chpid))
  330. return 0;
  331. chp = kzalloc(sizeof(struct channel_path), GFP_KERNEL);
  332. if (!chp)
  333. return -ENOMEM;
  334. /* fill in status, etc. */
  335. chp->chpid = chpid;
  336. chp->state = 1;
  337. chp->dev.parent = &css[chpid.cssid]->device;
  338. chp->dev.release = chp_release;
  339. snprintf(chp->dev.bus_id, BUS_ID_SIZE, "chp%x.%02x", chpid.cssid,
  340. chpid.id);
  341. /* Obtain channel path description and fill it in. */
  342. ret = chsc_determine_channel_path_description(chpid, &chp->desc);
  343. if (ret)
  344. goto out_free;
  345. if ((chp->desc.flags & 0x80) == 0) {
  346. ret = -ENODEV;
  347. goto out_free;
  348. }
  349. /* Get channel-measurement characteristics. */
  350. if (css_characteristics_avail && css_chsc_characteristics.scmc
  351. && css_chsc_characteristics.secm) {
  352. ret = chsc_get_channel_measurement_chars(chp);
  353. if (ret)
  354. goto out_free;
  355. } else {
  356. static int msg_done;
  357. if (!msg_done) {
  358. printk(KERN_WARNING "cio: Channel measurements not "
  359. "available, continuing.\n");
  360. msg_done = 1;
  361. }
  362. chp->cmg = -1;
  363. }
  364. /* make it known to the system */
  365. ret = device_register(&chp->dev);
  366. if (ret) {
  367. printk(KERN_WARNING "%s: could not register %x.%02x\n",
  368. __func__, chpid.cssid, chpid.id);
  369. goto out_free;
  370. }
  371. ret = sysfs_create_group(&chp->dev.kobj, &chp_attr_group);
  372. if (ret) {
  373. device_unregister(&chp->dev);
  374. goto out_free;
  375. }
  376. mutex_lock(&css[chpid.cssid]->mutex);
  377. if (css[chpid.cssid]->cm_enabled) {
  378. ret = chp_add_cmg_attr(chp);
  379. if (ret) {
  380. sysfs_remove_group(&chp->dev.kobj, &chp_attr_group);
  381. device_unregister(&chp->dev);
  382. mutex_unlock(&css[chpid.cssid]->mutex);
  383. goto out_free;
  384. }
  385. }
  386. css[chpid.cssid]->chps[chpid.id] = chp;
  387. mutex_unlock(&css[chpid.cssid]->mutex);
  388. return ret;
  389. out_free:
  390. kfree(chp);
  391. return ret;
  392. }
  393. /**
  394. * chp_get_chp_desc - return newly allocated channel-path description
  395. * @chpid: channel-path ID
  396. *
  397. * On success return a newly allocated copy of the channel-path description
  398. * data associated with the given channel-path ID. Return %NULL on error.
  399. */
  400. void *chp_get_chp_desc(struct chp_id chpid)
  401. {
  402. struct channel_path *chp;
  403. struct channel_path_desc *desc;
  404. chp = chpid_to_chp(chpid);
  405. if (!chp)
  406. return NULL;
  407. desc = kmalloc(sizeof(struct channel_path_desc), GFP_KERNEL);
  408. if (!desc)
  409. return NULL;
  410. memcpy(desc, &chp->desc, sizeof(struct channel_path_desc));
  411. return desc;
  412. }
  413. /**
  414. * chp_process_crw - process channel-path status change
  415. * @id: channel-path ID number
  416. * @status: non-zero if channel-path has become available, zero otherwise
  417. *
  418. * Handle channel-report-words indicating that the status of a channel-path
  419. * has changed.
  420. */
  421. void chp_process_crw(int id, int status)
  422. {
  423. struct chp_id chpid;
  424. chp_id_init(&chpid);
  425. chpid.id = id;
  426. if (status) {
  427. if (!chp_is_registered(chpid))
  428. chp_new(chpid);
  429. chsc_chp_online(chpid);
  430. } else
  431. chsc_chp_offline(chpid);
  432. }
  433. static inline int info_bit_num(struct chp_id id)
  434. {
  435. return id.id + id.cssid * (__MAX_CHPID + 1);
  436. }
  437. /* Force chp_info refresh on next call to info_validate(). */
  438. static void info_expire(void)
  439. {
  440. mutex_lock(&info_lock);
  441. chp_info_expires = jiffies - 1;
  442. mutex_unlock(&info_lock);
  443. }
  444. /* Ensure that chp_info is up-to-date. */
  445. static int info_update(void)
  446. {
  447. int rc;
  448. mutex_lock(&info_lock);
  449. rc = 0;
  450. if (time_after(jiffies, chp_info_expires)) {
  451. /* Data is too old, update. */
  452. rc = sclp_chp_read_info(&chp_info);
  453. chp_info_expires = jiffies + CHP_INFO_UPDATE_INTERVAL ;
  454. }
  455. mutex_unlock(&info_lock);
  456. return rc;
  457. }
  458. /**
  459. * chp_info_get_status - retrieve configure status of a channel-path
  460. * @chpid: channel-path ID
  461. *
  462. * On success, return 0 for standby, 1 for configured, 2 for reserved,
  463. * 3 for not recognized. Return negative error code on error.
  464. */
  465. int chp_info_get_status(struct chp_id chpid)
  466. {
  467. int rc;
  468. int bit;
  469. rc = info_update();
  470. if (rc)
  471. return rc;
  472. bit = info_bit_num(chpid);
  473. mutex_lock(&info_lock);
  474. if (!chp_test_bit(chp_info.recognized, bit))
  475. rc = CHP_STATUS_NOT_RECOGNIZED;
  476. else if (chp_test_bit(chp_info.configured, bit))
  477. rc = CHP_STATUS_CONFIGURED;
  478. else if (chp_test_bit(chp_info.standby, bit))
  479. rc = CHP_STATUS_STANDBY;
  480. else
  481. rc = CHP_STATUS_RESERVED;
  482. mutex_unlock(&info_lock);
  483. return rc;
  484. }
  485. /* Return configure task for chpid. */
  486. static enum cfg_task_t cfg_get_task(struct chp_id chpid)
  487. {
  488. return chp_cfg_task[chpid.cssid][chpid.id];
  489. }
  490. /* Set configure task for chpid. */
  491. static void cfg_set_task(struct chp_id chpid, enum cfg_task_t cfg)
  492. {
  493. chp_cfg_task[chpid.cssid][chpid.id] = cfg;
  494. }
  495. /* Perform one configure/deconfigure request. Reschedule work function until
  496. * last request. */
  497. static void cfg_func(struct work_struct *work)
  498. {
  499. struct chp_id chpid;
  500. enum cfg_task_t t;
  501. mutex_lock(&cfg_lock);
  502. t = cfg_none;
  503. chp_id_for_each(&chpid) {
  504. t = cfg_get_task(chpid);
  505. if (t != cfg_none) {
  506. cfg_set_task(chpid, cfg_none);
  507. break;
  508. }
  509. }
  510. mutex_unlock(&cfg_lock);
  511. switch (t) {
  512. case cfg_configure:
  513. sclp_chp_configure(chpid);
  514. info_expire();
  515. chsc_chp_online(chpid);
  516. break;
  517. case cfg_deconfigure:
  518. sclp_chp_deconfigure(chpid);
  519. info_expire();
  520. chsc_chp_offline(chpid);
  521. break;
  522. case cfg_none:
  523. /* Get updated information after last change. */
  524. info_update();
  525. mutex_lock(&cfg_lock);
  526. cfg_busy = 0;
  527. mutex_unlock(&cfg_lock);
  528. wake_up_interruptible(&cfg_wait_queue);
  529. return;
  530. }
  531. queue_work(chp_wq, &cfg_work);
  532. }
  533. /**
  534. * chp_cfg_schedule - schedule chpid configuration request
  535. * @chpid - channel-path ID
  536. * @configure - Non-zero for configure, zero for deconfigure
  537. *
  538. * Schedule a channel-path configuration/deconfiguration request.
  539. */
  540. void chp_cfg_schedule(struct chp_id chpid, int configure)
  541. {
  542. CIO_MSG_EVENT(2, "chp_cfg_sched%x.%02x=%d\n", chpid.cssid, chpid.id,
  543. configure);
  544. mutex_lock(&cfg_lock);
  545. cfg_set_task(chpid, configure ? cfg_configure : cfg_deconfigure);
  546. cfg_busy = 1;
  547. mutex_unlock(&cfg_lock);
  548. queue_work(chp_wq, &cfg_work);
  549. }
  550. /**
  551. * chp_cfg_cancel_deconfigure - cancel chpid deconfiguration request
  552. * @chpid - channel-path ID
  553. *
  554. * Cancel an active channel-path deconfiguration request if it has not yet
  555. * been performed.
  556. */
  557. void chp_cfg_cancel_deconfigure(struct chp_id chpid)
  558. {
  559. CIO_MSG_EVENT(2, "chp_cfg_cancel:%x.%02x\n", chpid.cssid, chpid.id);
  560. mutex_lock(&cfg_lock);
  561. if (cfg_get_task(chpid) == cfg_deconfigure)
  562. cfg_set_task(chpid, cfg_none);
  563. mutex_unlock(&cfg_lock);
  564. }
  565. static int cfg_wait_idle(void)
  566. {
  567. if (wait_event_interruptible(cfg_wait_queue, !cfg_busy))
  568. return -ERESTARTSYS;
  569. return 0;
  570. }
  571. static int __init chp_init(void)
  572. {
  573. struct chp_id chpid;
  574. chp_wq = create_singlethread_workqueue("cio_chp");
  575. if (!chp_wq)
  576. return -ENOMEM;
  577. INIT_WORK(&cfg_work, cfg_func);
  578. init_waitqueue_head(&cfg_wait_queue);
  579. if (info_update())
  580. return 0;
  581. /* Register available channel-paths. */
  582. chp_id_for_each(&chpid) {
  583. if (chp_info_get_status(chpid) != CHP_STATUS_NOT_RECOGNIZED)
  584. chp_new(chpid);
  585. }
  586. return 0;
  587. }
  588. subsys_initcall(chp_init);