css.c 14 KB

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