css.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. * drivers/s390/cio/css.c
  3. * driver for channel subsystem
  4. * $Revision: 1.85 $
  5. *
  6. * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
  7. * IBM Corporation
  8. * Author(s): Arnd Bergmann (arndb@de.ibm.com)
  9. * Cornelia Huck (cohuck@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. unsigned int highest_subchannel;
  23. int need_rescan = 0;
  24. int css_init_done = 0;
  25. struct pgid global_pgid;
  26. int css_characteristics_avail = 0;
  27. struct device css_bus_device = {
  28. .bus_id = "css0",
  29. };
  30. static struct subchannel *
  31. css_alloc_subchannel(int irq)
  32. {
  33. struct subchannel *sch;
  34. int ret;
  35. sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
  36. if (sch == NULL)
  37. return ERR_PTR(-ENOMEM);
  38. ret = cio_validate_subchannel (sch, irq);
  39. if (ret < 0) {
  40. kfree(sch);
  41. return ERR_PTR(ret);
  42. }
  43. if (irq > highest_subchannel)
  44. highest_subchannel = irq;
  45. if (sch->st != SUBCHANNEL_TYPE_IO) {
  46. /* For now we ignore all non-io subchannels. */
  47. kfree(sch);
  48. return ERR_PTR(-EINVAL);
  49. }
  50. /*
  51. * Set intparm to subchannel address.
  52. * This is fine even on 64bit since the subchannel is always located
  53. * under 2G.
  54. */
  55. sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
  56. ret = cio_modify(sch);
  57. if (ret) {
  58. kfree(sch);
  59. return ERR_PTR(ret);
  60. }
  61. return sch;
  62. }
  63. static void
  64. css_free_subchannel(struct subchannel *sch)
  65. {
  66. if (sch) {
  67. /* Reset intparm to zeroes. */
  68. sch->schib.pmcw.intparm = 0;
  69. cio_modify(sch);
  70. kfree(sch);
  71. }
  72. }
  73. static void
  74. css_subchannel_release(struct device *dev)
  75. {
  76. struct subchannel *sch;
  77. sch = to_subchannel(dev);
  78. if (!cio_is_console(sch->irq))
  79. kfree(sch);
  80. }
  81. extern int css_get_ssd_info(struct subchannel *sch);
  82. static int
  83. css_register_subchannel(struct subchannel *sch)
  84. {
  85. int ret;
  86. /* Initialize the subchannel structure */
  87. sch->dev.parent = &css_bus_device;
  88. sch->dev.bus = &css_bus_type;
  89. sch->dev.release = &css_subchannel_release;
  90. /* make it known to the system */
  91. ret = device_register(&sch->dev);
  92. if (ret)
  93. printk (KERN_WARNING "%s: could not register %s\n",
  94. __func__, sch->dev.bus_id);
  95. else
  96. css_get_ssd_info(sch);
  97. return ret;
  98. }
  99. int
  100. css_probe_device(int irq)
  101. {
  102. int ret;
  103. struct subchannel *sch;
  104. sch = css_alloc_subchannel(irq);
  105. if (IS_ERR(sch))
  106. return PTR_ERR(sch);
  107. ret = css_register_subchannel(sch);
  108. if (ret)
  109. css_free_subchannel(sch);
  110. return ret;
  111. }
  112. static int
  113. check_subchannel(struct device * dev, void * data)
  114. {
  115. struct subchannel *sch;
  116. int irq = (unsigned long)data;
  117. sch = to_subchannel(dev);
  118. return (sch->irq == irq);
  119. }
  120. struct subchannel *
  121. get_subchannel_by_schid(int irq)
  122. {
  123. struct device *dev;
  124. dev = bus_find_device(&css_bus_type, NULL,
  125. (void *)(unsigned long)irq, check_subchannel);
  126. return dev ? to_subchannel(dev) : NULL;
  127. }
  128. static inline int
  129. css_get_subchannel_status(struct subchannel *sch, int schid)
  130. {
  131. struct schib schib;
  132. int cc;
  133. cc = stsch(schid, &schib);
  134. if (cc)
  135. return CIO_GONE;
  136. if (!schib.pmcw.dnv)
  137. return CIO_GONE;
  138. if (sch && sch->schib.pmcw.dnv &&
  139. (schib.pmcw.dev != sch->schib.pmcw.dev))
  140. return CIO_REVALIDATE;
  141. if (sch && !sch->lpm)
  142. return CIO_NO_PATH;
  143. return CIO_OPER;
  144. }
  145. static int
  146. css_evaluate_subchannel(int irq, int slow)
  147. {
  148. int event, ret, disc;
  149. struct subchannel *sch;
  150. unsigned long flags;
  151. sch = get_subchannel_by_schid(irq);
  152. disc = sch ? device_is_disconnected(sch) : 0;
  153. if (disc && slow) {
  154. if (sch)
  155. put_device(&sch->dev);
  156. return 0; /* Already processed. */
  157. }
  158. /*
  159. * We've got a machine check, so running I/O won't get an interrupt.
  160. * Kill any pending timers.
  161. */
  162. if (sch)
  163. device_kill_pending_timer(sch);
  164. if (!disc && !slow) {
  165. if (sch)
  166. put_device(&sch->dev);
  167. return -EAGAIN; /* Will be done on the slow path. */
  168. }
  169. event = css_get_subchannel_status(sch, irq);
  170. CIO_MSG_EVENT(4, "Evaluating schid %04x, event %d, %s, %s path.\n",
  171. irq, event, sch?(disc?"disconnected":"normal"):"unknown",
  172. slow?"slow":"fast");
  173. switch (event) {
  174. case CIO_NO_PATH:
  175. case CIO_GONE:
  176. if (!sch) {
  177. /* Never used this subchannel. Ignore. */
  178. ret = 0;
  179. break;
  180. }
  181. if (disc && (event == CIO_NO_PATH)) {
  182. /*
  183. * Uargh, hack again. Because we don't get a machine
  184. * check on configure on, our path bookkeeping can
  185. * be out of date here (it's fine while we only do
  186. * logical varying or get chsc machine checks). We
  187. * need to force reprobing or we might miss devices
  188. * coming operational again. It won't do harm in real
  189. * no path situations.
  190. */
  191. spin_lock_irqsave(&sch->lock, flags);
  192. device_trigger_reprobe(sch);
  193. spin_unlock_irqrestore(&sch->lock, flags);
  194. ret = 0;
  195. break;
  196. }
  197. if (sch->driver && sch->driver->notify &&
  198. sch->driver->notify(&sch->dev, event)) {
  199. cio_disable_subchannel(sch);
  200. device_set_disconnected(sch);
  201. ret = 0;
  202. break;
  203. }
  204. /*
  205. * Unregister subchannel.
  206. * The device will be killed automatically.
  207. */
  208. cio_disable_subchannel(sch);
  209. device_unregister(&sch->dev);
  210. /* Reset intparm to zeroes. */
  211. sch->schib.pmcw.intparm = 0;
  212. cio_modify(sch);
  213. put_device(&sch->dev);
  214. ret = 0;
  215. break;
  216. case CIO_REVALIDATE:
  217. /*
  218. * Revalidation machine check. Sick.
  219. * We don't notify the driver since we have to throw the device
  220. * away in any case.
  221. */
  222. if (!disc) {
  223. device_unregister(&sch->dev);
  224. /* Reset intparm to zeroes. */
  225. sch->schib.pmcw.intparm = 0;
  226. cio_modify(sch);
  227. put_device(&sch->dev);
  228. ret = css_probe_device(irq);
  229. } else {
  230. /*
  231. * We can't immediately deregister the disconnected
  232. * device since it might block.
  233. */
  234. spin_lock_irqsave(&sch->lock, flags);
  235. device_trigger_reprobe(sch);
  236. spin_unlock_irqrestore(&sch->lock, flags);
  237. ret = 0;
  238. }
  239. break;
  240. case CIO_OPER:
  241. if (disc) {
  242. spin_lock_irqsave(&sch->lock, flags);
  243. /* Get device operational again. */
  244. device_trigger_reprobe(sch);
  245. spin_unlock_irqrestore(&sch->lock, flags);
  246. }
  247. ret = sch ? 0 : css_probe_device(irq);
  248. break;
  249. default:
  250. BUG();
  251. ret = 0;
  252. }
  253. return ret;
  254. }
  255. static void
  256. css_rescan_devices(void)
  257. {
  258. int irq, ret;
  259. for (irq = 0; irq < __MAX_SUBCHANNELS; irq++) {
  260. ret = css_evaluate_subchannel(irq, 1);
  261. /* No more memory. It doesn't make sense to continue. No
  262. * panic because this can happen in midflight and just
  263. * because we can't use a new device is no reason to crash
  264. * the system. */
  265. if (ret == -ENOMEM)
  266. break;
  267. /* -ENXIO indicates that there are no more subchannels. */
  268. if (ret == -ENXIO)
  269. break;
  270. }
  271. }
  272. struct slow_subchannel {
  273. struct list_head slow_list;
  274. unsigned long 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. css_rescan_devices();
  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 irq)
  319. {
  320. int ret;
  321. CIO_CRW_EVENT(2, "source is subchannel %04X\n", irq);
  322. if (need_rescan)
  323. /* We need to iterate all subchannels anyway. */
  324. return -EAGAIN;
  325. /*
  326. * Since we are always presented with IPI in the CRW, we have to
  327. * use stsch() to find out if the subchannel in question has come
  328. * or gone.
  329. */
  330. ret = css_evaluate_subchannel(irq, 0);
  331. if (ret == -EAGAIN) {
  332. if (css_enqueue_subchannel_slow(irq)) {
  333. css_clear_subchannel_slow_list();
  334. need_rescan = 1;
  335. }
  336. }
  337. return ret;
  338. }
  339. static void __init
  340. css_generate_pgid(void)
  341. {
  342. /* Let's build our path group ID here. */
  343. if (css_characteristics_avail && css_general_characteristics.mcss)
  344. global_pgid.cpu_addr = 0x8000;
  345. else {
  346. #ifdef CONFIG_SMP
  347. global_pgid.cpu_addr = hard_smp_processor_id();
  348. #else
  349. global_pgid.cpu_addr = 0;
  350. #endif
  351. }
  352. global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
  353. global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
  354. global_pgid.tod_high = (__u32) (get_clock() >> 32);
  355. }
  356. /*
  357. * Now that the driver core is running, we can setup our channel subsystem.
  358. * The struct subchannel's are created during probing (except for the
  359. * static console subchannel).
  360. */
  361. static int __init
  362. init_channel_subsystem (void)
  363. {
  364. int ret, irq;
  365. if (chsc_determine_css_characteristics() == 0)
  366. css_characteristics_avail = 1;
  367. css_generate_pgid();
  368. if ((ret = bus_register(&css_bus_type)))
  369. goto out;
  370. if ((ret = device_register (&css_bus_device)))
  371. goto out_bus;
  372. css_init_done = 1;
  373. ctl_set_bit(6, 28);
  374. for (irq = 0; irq < __MAX_SUBCHANNELS; irq++) {
  375. struct subchannel *sch;
  376. if (cio_is_console(irq))
  377. sch = cio_get_console_subchannel();
  378. else {
  379. sch = css_alloc_subchannel(irq);
  380. if (IS_ERR(sch))
  381. ret = PTR_ERR(sch);
  382. else
  383. ret = 0;
  384. if (ret == -ENOMEM)
  385. panic("Out of memory in "
  386. "init_channel_subsystem\n");
  387. /* -ENXIO: no more subchannels. */
  388. if (ret == -ENXIO)
  389. break;
  390. if (ret)
  391. continue;
  392. }
  393. /*
  394. * We register ALL valid subchannels in ioinfo, even those
  395. * that have been present before init_channel_subsystem.
  396. * These subchannels can't have been registered yet (kmalloc
  397. * not working) so we do it now. This is true e.g. for the
  398. * console subchannel.
  399. */
  400. css_register_subchannel(sch);
  401. }
  402. return 0;
  403. out_bus:
  404. bus_unregister(&css_bus_type);
  405. out:
  406. return ret;
  407. }
  408. /*
  409. * find a driver for a subchannel. They identify by the subchannel
  410. * type with the exception that the console subchannel driver has its own
  411. * subchannel type although the device is an i/o subchannel
  412. */
  413. static int
  414. css_bus_match (struct device *dev, struct device_driver *drv)
  415. {
  416. struct subchannel *sch = container_of (dev, struct subchannel, dev);
  417. struct css_driver *driver = container_of (drv, struct css_driver, drv);
  418. if (sch->st == driver->subchannel_type)
  419. return 1;
  420. return 0;
  421. }
  422. struct bus_type css_bus_type = {
  423. .name = "css",
  424. .match = &css_bus_match,
  425. };
  426. subsys_initcall(init_channel_subsystem);
  427. /*
  428. * Register root devices for some drivers. The release function must not be
  429. * in the device drivers, so we do it here.
  430. */
  431. static void
  432. s390_root_dev_release(struct device *dev)
  433. {
  434. kfree(dev);
  435. }
  436. struct device *
  437. s390_root_dev_register(const char *name)
  438. {
  439. struct device *dev;
  440. int ret;
  441. if (!strlen(name))
  442. return ERR_PTR(-EINVAL);
  443. dev = kmalloc(sizeof(struct device), GFP_KERNEL);
  444. if (!dev)
  445. return ERR_PTR(-ENOMEM);
  446. memset(dev, 0, sizeof(struct device));
  447. strncpy(dev->bus_id, name, min(strlen(name), (size_t)BUS_ID_SIZE));
  448. dev->release = s390_root_dev_release;
  449. ret = device_register(dev);
  450. if (ret) {
  451. kfree(dev);
  452. return ERR_PTR(ret);
  453. }
  454. return dev;
  455. }
  456. void
  457. s390_root_dev_unregister(struct device *dev)
  458. {
  459. if (dev)
  460. device_unregister(dev);
  461. }
  462. int
  463. css_enqueue_subchannel_slow(unsigned long schid)
  464. {
  465. struct slow_subchannel *new_slow_sch;
  466. unsigned long flags;
  467. new_slow_sch = kmalloc(sizeof(struct slow_subchannel), GFP_ATOMIC);
  468. if (!new_slow_sch)
  469. return -ENOMEM;
  470. memset(new_slow_sch, 0, sizeof(struct slow_subchannel));
  471. new_slow_sch->schid = schid;
  472. spin_lock_irqsave(&slow_subchannel_lock, flags);
  473. list_add_tail(&new_slow_sch->slow_list, &slow_subchannels_head);
  474. spin_unlock_irqrestore(&slow_subchannel_lock, flags);
  475. return 0;
  476. }
  477. void
  478. css_clear_subchannel_slow_list(void)
  479. {
  480. unsigned long flags;
  481. spin_lock_irqsave(&slow_subchannel_lock, flags);
  482. while (!list_empty(&slow_subchannels_head)) {
  483. struct slow_subchannel *slow_sch =
  484. list_entry(slow_subchannels_head.next,
  485. struct slow_subchannel, slow_list);
  486. list_del_init(slow_subchannels_head.next);
  487. kfree(slow_sch);
  488. }
  489. spin_unlock_irqrestore(&slow_subchannel_lock, flags);
  490. }
  491. int
  492. css_slow_subchannels_exist(void)
  493. {
  494. return (!list_empty(&slow_subchannels_head));
  495. }
  496. MODULE_LICENSE("GPL");
  497. EXPORT_SYMBOL(css_bus_type);
  498. EXPORT_SYMBOL(s390_root_dev_register);
  499. EXPORT_SYMBOL(s390_root_dev_unregister);
  500. EXPORT_SYMBOL_GPL(css_characteristics_avail);