chp.c 16 KB

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