css.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /*
  2. * drivers/s390/cio/css.c
  3. * driver for channel subsystem
  4. *
  5. * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
  6. * IBM Corporation
  7. * Author(s): Arnd Bergmann (arndb@de.ibm.com)
  8. * Cornelia Huck (cornelia.huck@de.ibm.com)
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/device.h>
  13. #include <linux/slab.h>
  14. #include <linux/errno.h>
  15. #include <linux/list.h>
  16. #include "css.h"
  17. #include "cio.h"
  18. #include "cio_debug.h"
  19. #include "ioasm.h"
  20. #include "chsc.h"
  21. int need_rescan = 0;
  22. int css_init_done = 0;
  23. static int max_ssid = 0;
  24. struct channel_subsystem *css[__MAX_CSSID + 1];
  25. int css_characteristics_avail = 0;
  26. inline int
  27. for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
  28. {
  29. struct subchannel_id schid;
  30. int ret;
  31. init_subchannel_id(&schid);
  32. ret = -ENODEV;
  33. do {
  34. do {
  35. ret = fn(schid, data);
  36. if (ret)
  37. break;
  38. } while (schid.sch_no++ < __MAX_SUBCHANNEL);
  39. schid.sch_no = 0;
  40. } while (schid.ssid++ < max_ssid);
  41. return ret;
  42. }
  43. static struct subchannel *
  44. css_alloc_subchannel(struct subchannel_id schid)
  45. {
  46. struct subchannel *sch;
  47. int ret;
  48. sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
  49. if (sch == NULL)
  50. return ERR_PTR(-ENOMEM);
  51. ret = cio_validate_subchannel (sch, schid);
  52. if (ret < 0) {
  53. kfree(sch);
  54. return ERR_PTR(ret);
  55. }
  56. if (sch->st != SUBCHANNEL_TYPE_IO) {
  57. /* For now we ignore all non-io subchannels. */
  58. kfree(sch);
  59. return ERR_PTR(-EINVAL);
  60. }
  61. /*
  62. * Set intparm to subchannel address.
  63. * This is fine even on 64bit since the subchannel is always located
  64. * under 2G.
  65. */
  66. sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
  67. ret = cio_modify(sch);
  68. if (ret) {
  69. kfree(sch);
  70. return ERR_PTR(ret);
  71. }
  72. return sch;
  73. }
  74. static void
  75. css_free_subchannel(struct subchannel *sch)
  76. {
  77. if (sch) {
  78. /* Reset intparm to zeroes. */
  79. sch->schib.pmcw.intparm = 0;
  80. cio_modify(sch);
  81. kfree(sch);
  82. }
  83. }
  84. static void
  85. css_subchannel_release(struct device *dev)
  86. {
  87. struct subchannel *sch;
  88. sch = to_subchannel(dev);
  89. if (!cio_is_console(sch->schid))
  90. kfree(sch);
  91. }
  92. extern int css_get_ssd_info(struct subchannel *sch);
  93. static int
  94. css_register_subchannel(struct subchannel *sch)
  95. {
  96. int ret;
  97. /* Initialize the subchannel structure */
  98. sch->dev.parent = &css[0]->device;
  99. sch->dev.bus = &css_bus_type;
  100. sch->dev.release = &css_subchannel_release;
  101. /* make it known to the system */
  102. ret = device_register(&sch->dev);
  103. if (ret)
  104. printk (KERN_WARNING "%s: could not register %s\n",
  105. __func__, sch->dev.bus_id);
  106. else
  107. css_get_ssd_info(sch);
  108. return ret;
  109. }
  110. int
  111. css_probe_device(struct subchannel_id schid)
  112. {
  113. int ret;
  114. struct subchannel *sch;
  115. sch = css_alloc_subchannel(schid);
  116. if (IS_ERR(sch))
  117. return PTR_ERR(sch);
  118. ret = css_register_subchannel(sch);
  119. if (ret)
  120. css_free_subchannel(sch);
  121. return ret;
  122. }
  123. static int
  124. check_subchannel(struct device * dev, void * data)
  125. {
  126. struct subchannel *sch;
  127. struct subchannel_id *schid = data;
  128. sch = to_subchannel(dev);
  129. return schid_equal(&sch->schid, schid);
  130. }
  131. struct subchannel *
  132. get_subchannel_by_schid(struct subchannel_id schid)
  133. {
  134. struct device *dev;
  135. dev = bus_find_device(&css_bus_type, NULL,
  136. (void *)&schid, check_subchannel);
  137. return dev ? to_subchannel(dev) : NULL;
  138. }
  139. static inline int
  140. css_get_subchannel_status(struct subchannel *sch, struct subchannel_id schid)
  141. {
  142. struct schib schib;
  143. int cc;
  144. cc = stsch(schid, &schib);
  145. if (cc)
  146. return CIO_GONE;
  147. if (!schib.pmcw.dnv)
  148. return CIO_GONE;
  149. if (sch && sch->schib.pmcw.dnv &&
  150. (schib.pmcw.dev != sch->schib.pmcw.dev))
  151. return CIO_REVALIDATE;
  152. if (sch && !sch->lpm)
  153. return CIO_NO_PATH;
  154. return CIO_OPER;
  155. }
  156. static int
  157. css_evaluate_subchannel(struct subchannel_id schid, int slow)
  158. {
  159. int event, ret, disc;
  160. struct subchannel *sch;
  161. unsigned long flags;
  162. sch = get_subchannel_by_schid(schid);
  163. disc = sch ? device_is_disconnected(sch) : 0;
  164. if (disc && slow) {
  165. if (sch)
  166. put_device(&sch->dev);
  167. return 0; /* Already processed. */
  168. }
  169. /*
  170. * We've got a machine check, so running I/O won't get an interrupt.
  171. * Kill any pending timers.
  172. */
  173. if (sch)
  174. device_kill_pending_timer(sch);
  175. if (!disc && !slow) {
  176. if (sch)
  177. put_device(&sch->dev);
  178. return -EAGAIN; /* Will be done on the slow path. */
  179. }
  180. event = css_get_subchannel_status(sch, schid);
  181. CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
  182. schid.ssid, schid.sch_no, event,
  183. sch?(disc?"disconnected":"normal"):"unknown",
  184. slow?"slow":"fast");
  185. switch (event) {
  186. case CIO_NO_PATH:
  187. case CIO_GONE:
  188. if (!sch) {
  189. /* Never used this subchannel. Ignore. */
  190. ret = 0;
  191. break;
  192. }
  193. if (disc && (event == CIO_NO_PATH)) {
  194. /*
  195. * Uargh, hack again. Because we don't get a machine
  196. * check on configure on, our path bookkeeping can
  197. * be out of date here (it's fine while we only do
  198. * logical varying or get chsc machine checks). We
  199. * need to force reprobing or we might miss devices
  200. * coming operational again. It won't do harm in real
  201. * no path situations.
  202. */
  203. spin_lock_irqsave(&sch->lock, flags);
  204. device_trigger_reprobe(sch);
  205. spin_unlock_irqrestore(&sch->lock, flags);
  206. ret = 0;
  207. break;
  208. }
  209. if (sch->driver && sch->driver->notify &&
  210. sch->driver->notify(&sch->dev, event)) {
  211. cio_disable_subchannel(sch);
  212. device_set_disconnected(sch);
  213. ret = 0;
  214. break;
  215. }
  216. /*
  217. * Unregister subchannel.
  218. * The device will be killed automatically.
  219. */
  220. cio_disable_subchannel(sch);
  221. device_unregister(&sch->dev);
  222. /* Reset intparm to zeroes. */
  223. sch->schib.pmcw.intparm = 0;
  224. cio_modify(sch);
  225. put_device(&sch->dev);
  226. ret = 0;
  227. break;
  228. case CIO_REVALIDATE:
  229. /*
  230. * Revalidation machine check. Sick.
  231. * We don't notify the driver since we have to throw the device
  232. * away in any case.
  233. */
  234. if (!disc) {
  235. device_unregister(&sch->dev);
  236. /* Reset intparm to zeroes. */
  237. sch->schib.pmcw.intparm = 0;
  238. cio_modify(sch);
  239. put_device(&sch->dev);
  240. ret = css_probe_device(schid);
  241. } else {
  242. /*
  243. * We can't immediately deregister the disconnected
  244. * device since it might block.
  245. */
  246. spin_lock_irqsave(&sch->lock, flags);
  247. device_trigger_reprobe(sch);
  248. spin_unlock_irqrestore(&sch->lock, flags);
  249. ret = 0;
  250. }
  251. break;
  252. case CIO_OPER:
  253. if (disc) {
  254. spin_lock_irqsave(&sch->lock, flags);
  255. /* Get device operational again. */
  256. device_trigger_reprobe(sch);
  257. spin_unlock_irqrestore(&sch->lock, flags);
  258. }
  259. ret = sch ? 0 : css_probe_device(schid);
  260. break;
  261. default:
  262. BUG();
  263. ret = 0;
  264. }
  265. return ret;
  266. }
  267. static int
  268. css_rescan_devices(struct subchannel_id schid, void *data)
  269. {
  270. return css_evaluate_subchannel(schid, 1);
  271. }
  272. struct slow_subchannel {
  273. struct list_head slow_list;
  274. struct subchannel_id schid;
  275. };
  276. static LIST_HEAD(slow_subchannels_head);
  277. static DEFINE_SPINLOCK(slow_subchannel_lock);
  278. static void
  279. css_trigger_slow_path(void)
  280. {
  281. CIO_TRACE_EVENT(4, "slowpath");
  282. if (need_rescan) {
  283. need_rescan = 0;
  284. for_each_subchannel(css_rescan_devices, NULL);
  285. return;
  286. }
  287. spin_lock_irq(&slow_subchannel_lock);
  288. while (!list_empty(&slow_subchannels_head)) {
  289. struct slow_subchannel *slow_sch =
  290. list_entry(slow_subchannels_head.next,
  291. struct slow_subchannel, slow_list);
  292. list_del_init(slow_subchannels_head.next);
  293. spin_unlock_irq(&slow_subchannel_lock);
  294. css_evaluate_subchannel(slow_sch->schid, 1);
  295. spin_lock_irq(&slow_subchannel_lock);
  296. kfree(slow_sch);
  297. }
  298. spin_unlock_irq(&slow_subchannel_lock);
  299. }
  300. typedef void (*workfunc)(void *);
  301. DECLARE_WORK(slow_path_work, (workfunc)css_trigger_slow_path, NULL);
  302. struct workqueue_struct *slow_path_wq;
  303. /*
  304. * Rescan for new devices. FIXME: This is slow.
  305. * This function is called when we have lost CRWs due to overflows and we have
  306. * to do subchannel housekeeping.
  307. */
  308. void
  309. css_reiterate_subchannels(void)
  310. {
  311. css_clear_subchannel_slow_list();
  312. need_rescan = 1;
  313. }
  314. /*
  315. * Called from the machine check handler for subchannel report words.
  316. */
  317. int
  318. css_process_crw(int rsid1, int rsid2)
  319. {
  320. int ret;
  321. struct subchannel_id mchk_schid;
  322. CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
  323. rsid1, rsid2);
  324. if (need_rescan)
  325. /* We need to iterate all subchannels anyway. */
  326. return -EAGAIN;
  327. init_subchannel_id(&mchk_schid);
  328. mchk_schid.sch_no = rsid1;
  329. if (rsid2 != 0)
  330. mchk_schid.ssid = (rsid2 >> 8) & 3;
  331. /*
  332. * Since we are always presented with IPI in the CRW, we have to
  333. * use stsch() to find out if the subchannel in question has come
  334. * or gone.
  335. */
  336. ret = css_evaluate_subchannel(mchk_schid, 0);
  337. if (ret == -EAGAIN) {
  338. if (css_enqueue_subchannel_slow(mchk_schid)) {
  339. css_clear_subchannel_slow_list();
  340. need_rescan = 1;
  341. }
  342. }
  343. return ret;
  344. }
  345. static int __init
  346. __init_channel_subsystem(struct subchannel_id schid, void *data)
  347. {
  348. struct subchannel *sch;
  349. int ret;
  350. if (cio_is_console(schid))
  351. sch = cio_get_console_subchannel();
  352. else {
  353. sch = css_alloc_subchannel(schid);
  354. if (IS_ERR(sch))
  355. ret = PTR_ERR(sch);
  356. else
  357. ret = 0;
  358. switch (ret) {
  359. case 0:
  360. break;
  361. case -ENOMEM:
  362. panic("Out of memory in init_channel_subsystem\n");
  363. /* -ENXIO: no more subchannels. */
  364. case -ENXIO:
  365. return ret;
  366. /* -EIO: this subchannel set not supported. */
  367. case -EIO:
  368. return ret;
  369. default:
  370. return 0;
  371. }
  372. }
  373. /*
  374. * We register ALL valid subchannels in ioinfo, even those
  375. * that have been present before init_channel_subsystem.
  376. * These subchannels can't have been registered yet (kmalloc
  377. * not working) so we do it now. This is true e.g. for the
  378. * console subchannel.
  379. */
  380. css_register_subchannel(sch);
  381. return 0;
  382. }
  383. static void __init
  384. css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
  385. {
  386. if (css_characteristics_avail && css_general_characteristics.mcss) {
  387. css->global_pgid.pgid_high.ext_cssid.version = 0x80;
  388. css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
  389. } else {
  390. #ifdef CONFIG_SMP
  391. css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
  392. #else
  393. css->global_pgid.pgid_high.cpu_addr = 0;
  394. #endif
  395. }
  396. css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
  397. css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
  398. css->global_pgid.tod_high = tod_high;
  399. }
  400. static void
  401. channel_subsystem_release(struct device *dev)
  402. {
  403. struct channel_subsystem *css;
  404. css = to_css(dev);
  405. mutex_destroy(&css->mutex);
  406. kfree(css);
  407. }
  408. static ssize_t
  409. css_cm_enable_show(struct device *dev, struct device_attribute *attr,
  410. char *buf)
  411. {
  412. struct channel_subsystem *css = to_css(dev);
  413. if (!css)
  414. return 0;
  415. return sprintf(buf, "%x\n", css->cm_enabled);
  416. }
  417. static ssize_t
  418. css_cm_enable_store(struct device *dev, struct device_attribute *attr,
  419. const char *buf, size_t count)
  420. {
  421. struct channel_subsystem *css = to_css(dev);
  422. int ret;
  423. switch (buf[0]) {
  424. case '0':
  425. ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
  426. break;
  427. case '1':
  428. ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
  429. break;
  430. default:
  431. ret = -EINVAL;
  432. }
  433. return ret < 0 ? ret : count;
  434. }
  435. static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
  436. static inline void __init
  437. setup_css(int nr)
  438. {
  439. u32 tod_high;
  440. memset(css[nr], 0, sizeof(struct channel_subsystem));
  441. mutex_init(&css[nr]->mutex);
  442. css[nr]->valid = 1;
  443. css[nr]->cssid = nr;
  444. sprintf(css[nr]->device.bus_id, "css%x", nr);
  445. css[nr]->device.release = channel_subsystem_release;
  446. tod_high = (u32) (get_clock() >> 32);
  447. css_generate_pgid(css[nr], tod_high);
  448. }
  449. /*
  450. * Now that the driver core is running, we can setup our channel subsystem.
  451. * The struct subchannel's are created during probing (except for the
  452. * static console subchannel).
  453. */
  454. static int __init
  455. init_channel_subsystem (void)
  456. {
  457. int ret, i;
  458. if (chsc_determine_css_characteristics() == 0)
  459. css_characteristics_avail = 1;
  460. if ((ret = bus_register(&css_bus_type)))
  461. goto out;
  462. /* Try to enable MSS. */
  463. ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
  464. switch (ret) {
  465. case 0: /* Success. */
  466. max_ssid = __MAX_SSID;
  467. break;
  468. case -ENOMEM:
  469. goto out_bus;
  470. default:
  471. max_ssid = 0;
  472. }
  473. /* Setup css structure. */
  474. for (i = 0; i <= __MAX_CSSID; i++) {
  475. css[i] = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
  476. if (!css[i]) {
  477. ret = -ENOMEM;
  478. goto out_unregister;
  479. }
  480. setup_css(i);
  481. ret = device_register(&css[i]->device);
  482. if (ret)
  483. goto out_free;
  484. if (css_characteristics_avail && css_chsc_characteristics.secm)
  485. device_create_file(&css[i]->device,
  486. &dev_attr_cm_enable);
  487. }
  488. css_init_done = 1;
  489. ctl_set_bit(6, 28);
  490. for_each_subchannel(__init_channel_subsystem, NULL);
  491. return 0;
  492. out_free:
  493. kfree(css[i]);
  494. out_unregister:
  495. while (i > 0) {
  496. i--;
  497. if (css_characteristics_avail && css_chsc_characteristics.secm)
  498. device_remove_file(&css[i]->device,
  499. &dev_attr_cm_enable);
  500. device_unregister(&css[i]->device);
  501. }
  502. out_bus:
  503. bus_unregister(&css_bus_type);
  504. out:
  505. return ret;
  506. }
  507. /*
  508. * find a driver for a subchannel. They identify by the subchannel
  509. * type with the exception that the console subchannel driver has its own
  510. * subchannel type although the device is an i/o subchannel
  511. */
  512. static int
  513. css_bus_match (struct device *dev, struct device_driver *drv)
  514. {
  515. struct subchannel *sch = container_of (dev, struct subchannel, dev);
  516. struct css_driver *driver = container_of (drv, struct css_driver, drv);
  517. if (sch->st == driver->subchannel_type)
  518. return 1;
  519. return 0;
  520. }
  521. static int
  522. css_probe (struct device *dev)
  523. {
  524. struct subchannel *sch;
  525. sch = to_subchannel(dev);
  526. sch->driver = container_of (dev->driver, struct css_driver, drv);
  527. return (sch->driver->probe ? sch->driver->probe(sch) : 0);
  528. }
  529. static int
  530. css_remove (struct device *dev)
  531. {
  532. struct subchannel *sch;
  533. sch = to_subchannel(dev);
  534. return (sch->driver->remove ? sch->driver->remove(sch) : 0);
  535. }
  536. static void
  537. css_shutdown (struct device *dev)
  538. {
  539. struct subchannel *sch;
  540. sch = to_subchannel(dev);
  541. if (sch->driver->shutdown)
  542. sch->driver->shutdown(sch);
  543. }
  544. struct bus_type css_bus_type = {
  545. .name = "css",
  546. .match = css_bus_match,
  547. .probe = css_probe,
  548. .remove = css_remove,
  549. .shutdown = css_shutdown,
  550. };
  551. subsys_initcall(init_channel_subsystem);
  552. int
  553. css_enqueue_subchannel_slow(struct subchannel_id schid)
  554. {
  555. struct slow_subchannel *new_slow_sch;
  556. unsigned long flags;
  557. new_slow_sch = kzalloc(sizeof(struct slow_subchannel), GFP_ATOMIC);
  558. if (!new_slow_sch)
  559. return -ENOMEM;
  560. new_slow_sch->schid = schid;
  561. spin_lock_irqsave(&slow_subchannel_lock, flags);
  562. list_add_tail(&new_slow_sch->slow_list, &slow_subchannels_head);
  563. spin_unlock_irqrestore(&slow_subchannel_lock, flags);
  564. return 0;
  565. }
  566. void
  567. css_clear_subchannel_slow_list(void)
  568. {
  569. unsigned long flags;
  570. spin_lock_irqsave(&slow_subchannel_lock, flags);
  571. while (!list_empty(&slow_subchannels_head)) {
  572. struct slow_subchannel *slow_sch =
  573. list_entry(slow_subchannels_head.next,
  574. struct slow_subchannel, slow_list);
  575. list_del_init(slow_subchannels_head.next);
  576. kfree(slow_sch);
  577. }
  578. spin_unlock_irqrestore(&slow_subchannel_lock, flags);
  579. }
  580. int
  581. css_slow_subchannels_exist(void)
  582. {
  583. return (!list_empty(&slow_subchannels_head));
  584. }
  585. MODULE_LICENSE("GPL");
  586. EXPORT_SYMBOL(css_bus_type);
  587. EXPORT_SYMBOL_GPL(css_characteristics_avail);