css.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. /*
  2. * driver for channel subsystem
  3. *
  4. * Copyright IBM Corp. 2002, 2010
  5. *
  6. * Author(s): Arnd Bergmann (arndb@de.ibm.com)
  7. * Cornelia Huck (cornelia.huck@de.ibm.com)
  8. */
  9. #define KMSG_COMPONENT "cio"
  10. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  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 <linux/reboot.h>
  18. #include <linux/suspend.h>
  19. #include <linux/proc_fs.h>
  20. #include <asm/isc.h>
  21. #include <asm/crw.h>
  22. #include "css.h"
  23. #include "cio.h"
  24. #include "cio_debug.h"
  25. #include "ioasm.h"
  26. #include "chsc.h"
  27. #include "device.h"
  28. #include "idset.h"
  29. #include "chp.h"
  30. int css_init_done = 0;
  31. int max_ssid;
  32. struct channel_subsystem *channel_subsystems[__MAX_CSSID + 1];
  33. static struct bus_type css_bus_type;
  34. int
  35. for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
  36. {
  37. struct subchannel_id schid;
  38. int ret;
  39. init_subchannel_id(&schid);
  40. ret = -ENODEV;
  41. do {
  42. do {
  43. ret = fn(schid, data);
  44. if (ret)
  45. break;
  46. } while (schid.sch_no++ < __MAX_SUBCHANNEL);
  47. schid.sch_no = 0;
  48. } while (schid.ssid++ < max_ssid);
  49. return ret;
  50. }
  51. struct cb_data {
  52. void *data;
  53. struct idset *set;
  54. int (*fn_known_sch)(struct subchannel *, void *);
  55. int (*fn_unknown_sch)(struct subchannel_id, void *);
  56. };
  57. static int call_fn_known_sch(struct device *dev, void *data)
  58. {
  59. struct subchannel *sch = to_subchannel(dev);
  60. struct cb_data *cb = data;
  61. int rc = 0;
  62. idset_sch_del(cb->set, sch->schid);
  63. if (cb->fn_known_sch)
  64. rc = cb->fn_known_sch(sch, cb->data);
  65. return rc;
  66. }
  67. static int call_fn_unknown_sch(struct subchannel_id schid, void *data)
  68. {
  69. struct cb_data *cb = data;
  70. int rc = 0;
  71. if (idset_sch_contains(cb->set, schid))
  72. rc = cb->fn_unknown_sch(schid, cb->data);
  73. return rc;
  74. }
  75. static int call_fn_all_sch(struct subchannel_id schid, void *data)
  76. {
  77. struct cb_data *cb = data;
  78. struct subchannel *sch;
  79. int rc = 0;
  80. sch = get_subchannel_by_schid(schid);
  81. if (sch) {
  82. if (cb->fn_known_sch)
  83. rc = cb->fn_known_sch(sch, cb->data);
  84. put_device(&sch->dev);
  85. } else {
  86. if (cb->fn_unknown_sch)
  87. rc = cb->fn_unknown_sch(schid, cb->data);
  88. }
  89. return rc;
  90. }
  91. int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *),
  92. int (*fn_unknown)(struct subchannel_id,
  93. void *), void *data)
  94. {
  95. struct cb_data cb;
  96. int rc;
  97. cb.data = data;
  98. cb.fn_known_sch = fn_known;
  99. cb.fn_unknown_sch = fn_unknown;
  100. cb.set = idset_sch_new();
  101. if (!cb.set)
  102. /* fall back to brute force scanning in case of oom */
  103. return for_each_subchannel(call_fn_all_sch, &cb);
  104. idset_fill(cb.set);
  105. /* Process registered subchannels. */
  106. rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
  107. if (rc)
  108. goto out;
  109. /* Process unregistered subchannels. */
  110. if (fn_unknown)
  111. rc = for_each_subchannel(call_fn_unknown_sch, &cb);
  112. out:
  113. idset_free(cb.set);
  114. return rc;
  115. }
  116. static void css_sch_todo(struct work_struct *work);
  117. static struct subchannel *
  118. css_alloc_subchannel(struct subchannel_id schid)
  119. {
  120. struct subchannel *sch;
  121. int ret;
  122. sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
  123. if (sch == NULL)
  124. return ERR_PTR(-ENOMEM);
  125. ret = cio_validate_subchannel (sch, schid);
  126. if (ret < 0) {
  127. kfree(sch);
  128. return ERR_PTR(ret);
  129. }
  130. INIT_WORK(&sch->todo_work, css_sch_todo);
  131. return sch;
  132. }
  133. static void
  134. css_subchannel_release(struct device *dev)
  135. {
  136. struct subchannel *sch;
  137. sch = to_subchannel(dev);
  138. if (!cio_is_console(sch->schid)) {
  139. /* Reset intparm to zeroes. */
  140. sch->config.intparm = 0;
  141. cio_commit_config(sch);
  142. kfree(sch->lock);
  143. kfree(sch);
  144. }
  145. }
  146. static int css_sch_device_register(struct subchannel *sch)
  147. {
  148. int ret;
  149. mutex_lock(&sch->reg_mutex);
  150. dev_set_name(&sch->dev, "0.%x.%04x", sch->schid.ssid,
  151. sch->schid.sch_no);
  152. ret = device_register(&sch->dev);
  153. mutex_unlock(&sch->reg_mutex);
  154. return ret;
  155. }
  156. /**
  157. * css_sch_device_unregister - unregister a subchannel
  158. * @sch: subchannel to be unregistered
  159. */
  160. void css_sch_device_unregister(struct subchannel *sch)
  161. {
  162. mutex_lock(&sch->reg_mutex);
  163. if (device_is_registered(&sch->dev))
  164. device_unregister(&sch->dev);
  165. mutex_unlock(&sch->reg_mutex);
  166. }
  167. EXPORT_SYMBOL_GPL(css_sch_device_unregister);
  168. static void css_sch_todo(struct work_struct *work)
  169. {
  170. struct subchannel *sch;
  171. enum sch_todo todo;
  172. sch = container_of(work, struct subchannel, todo_work);
  173. /* Find out todo. */
  174. spin_lock_irq(sch->lock);
  175. todo = sch->todo;
  176. CIO_MSG_EVENT(4, "sch_todo: sch=0.%x.%04x, todo=%d\n", sch->schid.ssid,
  177. sch->schid.sch_no, todo);
  178. sch->todo = SCH_TODO_NOTHING;
  179. spin_unlock_irq(sch->lock);
  180. /* Perform todo. */
  181. if (todo == SCH_TODO_UNREG)
  182. css_sch_device_unregister(sch);
  183. /* Release workqueue ref. */
  184. put_device(&sch->dev);
  185. }
  186. /**
  187. * css_sched_sch_todo - schedule a subchannel operation
  188. * @sch: subchannel
  189. * @todo: todo
  190. *
  191. * Schedule the operation identified by @todo to be performed on the slow path
  192. * workqueue. Do nothing if another operation with higher priority is already
  193. * scheduled. Needs to be called with subchannel lock held.
  194. */
  195. void css_sched_sch_todo(struct subchannel *sch, enum sch_todo todo)
  196. {
  197. CIO_MSG_EVENT(4, "sch_todo: sched sch=0.%x.%04x todo=%d\n",
  198. sch->schid.ssid, sch->schid.sch_no, todo);
  199. if (sch->todo >= todo)
  200. return;
  201. /* Get workqueue ref. */
  202. if (!get_device(&sch->dev))
  203. return;
  204. sch->todo = todo;
  205. if (!queue_work(cio_work_q, &sch->todo_work)) {
  206. /* Already queued, release workqueue ref. */
  207. put_device(&sch->dev);
  208. }
  209. }
  210. static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
  211. {
  212. int i;
  213. int mask;
  214. memset(ssd, 0, sizeof(struct chsc_ssd_info));
  215. ssd->path_mask = pmcw->pim;
  216. for (i = 0; i < 8; i++) {
  217. mask = 0x80 >> i;
  218. if (pmcw->pim & mask) {
  219. chp_id_init(&ssd->chpid[i]);
  220. ssd->chpid[i].id = pmcw->chpid[i];
  221. }
  222. }
  223. }
  224. static void ssd_register_chpids(struct chsc_ssd_info *ssd)
  225. {
  226. int i;
  227. int mask;
  228. for (i = 0; i < 8; i++) {
  229. mask = 0x80 >> i;
  230. if (ssd->path_mask & mask)
  231. if (!chp_is_registered(ssd->chpid[i]))
  232. chp_new(ssd->chpid[i]);
  233. }
  234. }
  235. void css_update_ssd_info(struct subchannel *sch)
  236. {
  237. int ret;
  238. if (cio_is_console(sch->schid)) {
  239. /* Console is initialized too early for functions requiring
  240. * memory allocation. */
  241. ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
  242. } else {
  243. ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
  244. if (ret)
  245. ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
  246. ssd_register_chpids(&sch->ssd_info);
  247. }
  248. }
  249. static ssize_t type_show(struct device *dev, struct device_attribute *attr,
  250. char *buf)
  251. {
  252. struct subchannel *sch = to_subchannel(dev);
  253. return sprintf(buf, "%01x\n", sch->st);
  254. }
  255. static DEVICE_ATTR(type, 0444, type_show, NULL);
  256. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  257. char *buf)
  258. {
  259. struct subchannel *sch = to_subchannel(dev);
  260. return sprintf(buf, "css:t%01X\n", sch->st);
  261. }
  262. static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
  263. static struct attribute *subch_attrs[] = {
  264. &dev_attr_type.attr,
  265. &dev_attr_modalias.attr,
  266. NULL,
  267. };
  268. static struct attribute_group subch_attr_group = {
  269. .attrs = subch_attrs,
  270. };
  271. static const struct attribute_group *default_subch_attr_groups[] = {
  272. &subch_attr_group,
  273. NULL,
  274. };
  275. static int css_register_subchannel(struct subchannel *sch)
  276. {
  277. int ret;
  278. /* Initialize the subchannel structure */
  279. sch->dev.parent = &channel_subsystems[0]->device;
  280. sch->dev.bus = &css_bus_type;
  281. sch->dev.release = &css_subchannel_release;
  282. sch->dev.groups = default_subch_attr_groups;
  283. /*
  284. * We don't want to generate uevents for I/O subchannels that don't
  285. * have a working ccw device behind them since they will be
  286. * unregistered before they can be used anyway, so we delay the add
  287. * uevent until after device recognition was successful.
  288. * Note that we suppress the uevent for all subchannel types;
  289. * the subchannel driver can decide itself when it wants to inform
  290. * userspace of its existence.
  291. */
  292. dev_set_uevent_suppress(&sch->dev, 1);
  293. css_update_ssd_info(sch);
  294. /* make it known to the system */
  295. ret = css_sch_device_register(sch);
  296. if (ret) {
  297. CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
  298. sch->schid.ssid, sch->schid.sch_no, ret);
  299. return ret;
  300. }
  301. if (!sch->driver) {
  302. /*
  303. * No driver matched. Generate the uevent now so that
  304. * a fitting driver module may be loaded based on the
  305. * modalias.
  306. */
  307. dev_set_uevent_suppress(&sch->dev, 0);
  308. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  309. }
  310. return ret;
  311. }
  312. int css_probe_device(struct subchannel_id schid)
  313. {
  314. int ret;
  315. struct subchannel *sch;
  316. if (cio_is_console(schid))
  317. sch = cio_get_console_subchannel();
  318. else {
  319. sch = css_alloc_subchannel(schid);
  320. if (IS_ERR(sch))
  321. return PTR_ERR(sch);
  322. }
  323. ret = css_register_subchannel(sch);
  324. if (ret) {
  325. if (!cio_is_console(schid))
  326. put_device(&sch->dev);
  327. }
  328. return ret;
  329. }
  330. static int
  331. check_subchannel(struct device * dev, void * data)
  332. {
  333. struct subchannel *sch;
  334. struct subchannel_id *schid = data;
  335. sch = to_subchannel(dev);
  336. return schid_equal(&sch->schid, schid);
  337. }
  338. struct subchannel *
  339. get_subchannel_by_schid(struct subchannel_id schid)
  340. {
  341. struct device *dev;
  342. dev = bus_find_device(&css_bus_type, NULL,
  343. &schid, check_subchannel);
  344. return dev ? to_subchannel(dev) : NULL;
  345. }
  346. /**
  347. * css_sch_is_valid() - check if a subchannel is valid
  348. * @schib: subchannel information block for the subchannel
  349. */
  350. int css_sch_is_valid(struct schib *schib)
  351. {
  352. if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
  353. return 0;
  354. if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
  355. return 0;
  356. return 1;
  357. }
  358. EXPORT_SYMBOL_GPL(css_sch_is_valid);
  359. static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
  360. {
  361. struct schib schib;
  362. if (!slow) {
  363. /* Will be done on the slow path. */
  364. return -EAGAIN;
  365. }
  366. if (stsch_err(schid, &schib) || !css_sch_is_valid(&schib)) {
  367. /* Unusable - ignore. */
  368. return 0;
  369. }
  370. CIO_MSG_EVENT(4, "event: sch 0.%x.%04x, new\n", schid.ssid,
  371. schid.sch_no);
  372. return css_probe_device(schid);
  373. }
  374. static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
  375. {
  376. int ret = 0;
  377. if (sch->driver) {
  378. if (sch->driver->sch_event)
  379. ret = sch->driver->sch_event(sch, slow);
  380. else
  381. dev_dbg(&sch->dev,
  382. "Got subchannel machine check but "
  383. "no sch_event handler provided.\n");
  384. }
  385. if (ret != 0 && ret != -EAGAIN) {
  386. CIO_MSG_EVENT(2, "eval: sch 0.%x.%04x, rc=%d\n",
  387. sch->schid.ssid, sch->schid.sch_no, ret);
  388. }
  389. return ret;
  390. }
  391. static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
  392. {
  393. struct subchannel *sch;
  394. int ret;
  395. sch = get_subchannel_by_schid(schid);
  396. if (sch) {
  397. ret = css_evaluate_known_subchannel(sch, slow);
  398. put_device(&sch->dev);
  399. } else
  400. ret = css_evaluate_new_subchannel(schid, slow);
  401. if (ret == -EAGAIN)
  402. css_schedule_eval(schid);
  403. }
  404. static struct idset *slow_subchannel_set;
  405. static spinlock_t slow_subchannel_lock;
  406. static wait_queue_head_t css_eval_wq;
  407. static atomic_t css_eval_scheduled;
  408. static int __init slow_subchannel_init(void)
  409. {
  410. spin_lock_init(&slow_subchannel_lock);
  411. atomic_set(&css_eval_scheduled, 0);
  412. init_waitqueue_head(&css_eval_wq);
  413. slow_subchannel_set = idset_sch_new();
  414. if (!slow_subchannel_set) {
  415. CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
  416. return -ENOMEM;
  417. }
  418. return 0;
  419. }
  420. static int slow_eval_known_fn(struct subchannel *sch, void *data)
  421. {
  422. int eval;
  423. int rc;
  424. spin_lock_irq(&slow_subchannel_lock);
  425. eval = idset_sch_contains(slow_subchannel_set, sch->schid);
  426. idset_sch_del(slow_subchannel_set, sch->schid);
  427. spin_unlock_irq(&slow_subchannel_lock);
  428. if (eval) {
  429. rc = css_evaluate_known_subchannel(sch, 1);
  430. if (rc == -EAGAIN)
  431. css_schedule_eval(sch->schid);
  432. }
  433. return 0;
  434. }
  435. static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
  436. {
  437. int eval;
  438. int rc = 0;
  439. spin_lock_irq(&slow_subchannel_lock);
  440. eval = idset_sch_contains(slow_subchannel_set, schid);
  441. idset_sch_del(slow_subchannel_set, schid);
  442. spin_unlock_irq(&slow_subchannel_lock);
  443. if (eval) {
  444. rc = css_evaluate_new_subchannel(schid, 1);
  445. switch (rc) {
  446. case -EAGAIN:
  447. css_schedule_eval(schid);
  448. rc = 0;
  449. break;
  450. case -ENXIO:
  451. case -ENOMEM:
  452. case -EIO:
  453. /* These should abort looping */
  454. break;
  455. default:
  456. rc = 0;
  457. }
  458. }
  459. return rc;
  460. }
  461. static void css_slow_path_func(struct work_struct *unused)
  462. {
  463. unsigned long flags;
  464. CIO_TRACE_EVENT(4, "slowpath");
  465. for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
  466. NULL);
  467. spin_lock_irqsave(&slow_subchannel_lock, flags);
  468. if (idset_is_empty(slow_subchannel_set)) {
  469. atomic_set(&css_eval_scheduled, 0);
  470. wake_up(&css_eval_wq);
  471. }
  472. spin_unlock_irqrestore(&slow_subchannel_lock, flags);
  473. }
  474. static DECLARE_WORK(slow_path_work, css_slow_path_func);
  475. struct workqueue_struct *cio_work_q;
  476. void css_schedule_eval(struct subchannel_id schid)
  477. {
  478. unsigned long flags;
  479. spin_lock_irqsave(&slow_subchannel_lock, flags);
  480. idset_sch_add(slow_subchannel_set, schid);
  481. atomic_set(&css_eval_scheduled, 1);
  482. queue_work(cio_work_q, &slow_path_work);
  483. spin_unlock_irqrestore(&slow_subchannel_lock, flags);
  484. }
  485. void css_schedule_eval_all(void)
  486. {
  487. unsigned long flags;
  488. spin_lock_irqsave(&slow_subchannel_lock, flags);
  489. idset_fill(slow_subchannel_set);
  490. atomic_set(&css_eval_scheduled, 1);
  491. queue_work(cio_work_q, &slow_path_work);
  492. spin_unlock_irqrestore(&slow_subchannel_lock, flags);
  493. }
  494. static int __unset_registered(struct device *dev, void *data)
  495. {
  496. struct idset *set = data;
  497. struct subchannel *sch = to_subchannel(dev);
  498. idset_sch_del(set, sch->schid);
  499. return 0;
  500. }
  501. static void css_schedule_eval_all_unreg(void)
  502. {
  503. unsigned long flags;
  504. struct idset *unreg_set;
  505. /* Find unregistered subchannels. */
  506. unreg_set = idset_sch_new();
  507. if (!unreg_set) {
  508. /* Fallback. */
  509. css_schedule_eval_all();
  510. return;
  511. }
  512. idset_fill(unreg_set);
  513. bus_for_each_dev(&css_bus_type, NULL, unreg_set, __unset_registered);
  514. /* Apply to slow_subchannel_set. */
  515. spin_lock_irqsave(&slow_subchannel_lock, flags);
  516. idset_add_set(slow_subchannel_set, unreg_set);
  517. atomic_set(&css_eval_scheduled, 1);
  518. queue_work(cio_work_q, &slow_path_work);
  519. spin_unlock_irqrestore(&slow_subchannel_lock, flags);
  520. idset_free(unreg_set);
  521. }
  522. void css_wait_for_slow_path(void)
  523. {
  524. flush_workqueue(cio_work_q);
  525. }
  526. /* Schedule reprobing of all unregistered subchannels. */
  527. void css_schedule_reprobe(void)
  528. {
  529. css_schedule_eval_all_unreg();
  530. }
  531. EXPORT_SYMBOL_GPL(css_schedule_reprobe);
  532. /*
  533. * Called from the machine check handler for subchannel report words.
  534. */
  535. static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
  536. {
  537. struct subchannel_id mchk_schid;
  538. struct subchannel *sch;
  539. if (overflow) {
  540. css_schedule_eval_all();
  541. return;
  542. }
  543. CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
  544. "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
  545. crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
  546. crw0->erc, crw0->rsid);
  547. if (crw1)
  548. CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
  549. "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
  550. crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
  551. crw1->anc, crw1->erc, crw1->rsid);
  552. init_subchannel_id(&mchk_schid);
  553. mchk_schid.sch_no = crw0->rsid;
  554. if (crw1)
  555. mchk_schid.ssid = (crw1->rsid >> 4) & 3;
  556. if (crw0->erc == CRW_ERC_PMOD) {
  557. sch = get_subchannel_by_schid(mchk_schid);
  558. if (sch) {
  559. css_update_ssd_info(sch);
  560. put_device(&sch->dev);
  561. }
  562. }
  563. /*
  564. * Since we are always presented with IPI in the CRW, we have to
  565. * use stsch() to find out if the subchannel in question has come
  566. * or gone.
  567. */
  568. css_evaluate_subchannel(mchk_schid, 0);
  569. }
  570. static void __init
  571. css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
  572. {
  573. struct cpuid cpu_id;
  574. if (css_general_characteristics.mcss) {
  575. css->global_pgid.pgid_high.ext_cssid.version = 0x80;
  576. css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
  577. } else {
  578. #ifdef CONFIG_SMP
  579. css->global_pgid.pgid_high.cpu_addr = stap();
  580. #else
  581. css->global_pgid.pgid_high.cpu_addr = 0;
  582. #endif
  583. }
  584. get_cpu_id(&cpu_id);
  585. css->global_pgid.cpu_id = cpu_id.ident;
  586. css->global_pgid.cpu_model = cpu_id.machine;
  587. css->global_pgid.tod_high = tod_high;
  588. }
  589. static void
  590. channel_subsystem_release(struct device *dev)
  591. {
  592. struct channel_subsystem *css;
  593. css = to_css(dev);
  594. mutex_destroy(&css->mutex);
  595. if (css->pseudo_subchannel) {
  596. /* Implies that it has been generated but never registered. */
  597. css_subchannel_release(&css->pseudo_subchannel->dev);
  598. css->pseudo_subchannel = NULL;
  599. }
  600. kfree(css);
  601. }
  602. static ssize_t
  603. css_cm_enable_show(struct device *dev, struct device_attribute *attr,
  604. char *buf)
  605. {
  606. struct channel_subsystem *css = to_css(dev);
  607. int ret;
  608. if (!css)
  609. return 0;
  610. mutex_lock(&css->mutex);
  611. ret = sprintf(buf, "%x\n", css->cm_enabled);
  612. mutex_unlock(&css->mutex);
  613. return ret;
  614. }
  615. static ssize_t
  616. css_cm_enable_store(struct device *dev, struct device_attribute *attr,
  617. const char *buf, size_t count)
  618. {
  619. struct channel_subsystem *css = to_css(dev);
  620. int ret;
  621. unsigned long val;
  622. ret = strict_strtoul(buf, 16, &val);
  623. if (ret)
  624. return ret;
  625. mutex_lock(&css->mutex);
  626. switch (val) {
  627. case 0:
  628. ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
  629. break;
  630. case 1:
  631. ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
  632. break;
  633. default:
  634. ret = -EINVAL;
  635. }
  636. mutex_unlock(&css->mutex);
  637. return ret < 0 ? ret : count;
  638. }
  639. static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
  640. static int __init setup_css(int nr)
  641. {
  642. u32 tod_high;
  643. int ret;
  644. struct channel_subsystem *css;
  645. css = channel_subsystems[nr];
  646. memset(css, 0, sizeof(struct channel_subsystem));
  647. css->pseudo_subchannel =
  648. kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
  649. if (!css->pseudo_subchannel)
  650. return -ENOMEM;
  651. css->pseudo_subchannel->dev.parent = &css->device;
  652. css->pseudo_subchannel->dev.release = css_subchannel_release;
  653. dev_set_name(&css->pseudo_subchannel->dev, "defunct");
  654. mutex_init(&css->pseudo_subchannel->reg_mutex);
  655. ret = cio_create_sch_lock(css->pseudo_subchannel);
  656. if (ret) {
  657. kfree(css->pseudo_subchannel);
  658. return ret;
  659. }
  660. mutex_init(&css->mutex);
  661. css->valid = 1;
  662. css->cssid = nr;
  663. dev_set_name(&css->device, "css%x", nr);
  664. css->device.release = channel_subsystem_release;
  665. tod_high = (u32) (get_clock() >> 32);
  666. css_generate_pgid(css, tod_high);
  667. return 0;
  668. }
  669. static int css_reboot_event(struct notifier_block *this,
  670. unsigned long event,
  671. void *ptr)
  672. {
  673. int ret, i;
  674. ret = NOTIFY_DONE;
  675. for (i = 0; i <= __MAX_CSSID; i++) {
  676. struct channel_subsystem *css;
  677. css = channel_subsystems[i];
  678. mutex_lock(&css->mutex);
  679. if (css->cm_enabled)
  680. if (chsc_secm(css, 0))
  681. ret = NOTIFY_BAD;
  682. mutex_unlock(&css->mutex);
  683. }
  684. return ret;
  685. }
  686. static struct notifier_block css_reboot_notifier = {
  687. .notifier_call = css_reboot_event,
  688. };
  689. /*
  690. * Since the css devices are neither on a bus nor have a class
  691. * nor have a special device type, we cannot stop/restart channel
  692. * path measurements via the normal suspend/resume callbacks, but have
  693. * to use notifiers.
  694. */
  695. static int css_power_event(struct notifier_block *this, unsigned long event,
  696. void *ptr)
  697. {
  698. int ret, i;
  699. switch (event) {
  700. case PM_HIBERNATION_PREPARE:
  701. case PM_SUSPEND_PREPARE:
  702. ret = NOTIFY_DONE;
  703. for (i = 0; i <= __MAX_CSSID; i++) {
  704. struct channel_subsystem *css;
  705. css = channel_subsystems[i];
  706. mutex_lock(&css->mutex);
  707. if (!css->cm_enabled) {
  708. mutex_unlock(&css->mutex);
  709. continue;
  710. }
  711. if (__chsc_do_secm(css, 0))
  712. ret = NOTIFY_BAD;
  713. mutex_unlock(&css->mutex);
  714. }
  715. break;
  716. case PM_POST_HIBERNATION:
  717. case PM_POST_SUSPEND:
  718. ret = NOTIFY_DONE;
  719. for (i = 0; i <= __MAX_CSSID; i++) {
  720. struct channel_subsystem *css;
  721. css = channel_subsystems[i];
  722. mutex_lock(&css->mutex);
  723. if (!css->cm_enabled) {
  724. mutex_unlock(&css->mutex);
  725. continue;
  726. }
  727. if (__chsc_do_secm(css, 1))
  728. ret = NOTIFY_BAD;
  729. mutex_unlock(&css->mutex);
  730. }
  731. /* search for subchannels, which appeared during hibernation */
  732. css_schedule_reprobe();
  733. break;
  734. default:
  735. ret = NOTIFY_DONE;
  736. }
  737. return ret;
  738. }
  739. static struct notifier_block css_power_notifier = {
  740. .notifier_call = css_power_event,
  741. };
  742. /*
  743. * Now that the driver core is running, we can setup our channel subsystem.
  744. * The struct subchannel's are created during probing (except for the
  745. * static console subchannel).
  746. */
  747. static int __init css_bus_init(void)
  748. {
  749. int ret, i;
  750. ret = chsc_init();
  751. if (ret)
  752. return ret;
  753. chsc_determine_css_characteristics();
  754. /* Try to enable MSS. */
  755. ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
  756. if (ret)
  757. max_ssid = 0;
  758. else /* Success. */
  759. max_ssid = __MAX_SSID;
  760. ret = slow_subchannel_init();
  761. if (ret)
  762. goto out;
  763. ret = crw_register_handler(CRW_RSC_SCH, css_process_crw);
  764. if (ret)
  765. goto out;
  766. if ((ret = bus_register(&css_bus_type)))
  767. goto out;
  768. /* Setup css structure. */
  769. for (i = 0; i <= __MAX_CSSID; i++) {
  770. struct channel_subsystem *css;
  771. css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
  772. if (!css) {
  773. ret = -ENOMEM;
  774. goto out_unregister;
  775. }
  776. channel_subsystems[i] = css;
  777. ret = setup_css(i);
  778. if (ret) {
  779. kfree(channel_subsystems[i]);
  780. goto out_unregister;
  781. }
  782. ret = device_register(&css->device);
  783. if (ret) {
  784. put_device(&css->device);
  785. goto out_unregister;
  786. }
  787. if (css_chsc_characteristics.secm) {
  788. ret = device_create_file(&css->device,
  789. &dev_attr_cm_enable);
  790. if (ret)
  791. goto out_device;
  792. }
  793. ret = device_register(&css->pseudo_subchannel->dev);
  794. if (ret) {
  795. put_device(&css->pseudo_subchannel->dev);
  796. goto out_file;
  797. }
  798. }
  799. ret = register_reboot_notifier(&css_reboot_notifier);
  800. if (ret)
  801. goto out_unregister;
  802. ret = register_pm_notifier(&css_power_notifier);
  803. if (ret) {
  804. unregister_reboot_notifier(&css_reboot_notifier);
  805. goto out_unregister;
  806. }
  807. css_init_done = 1;
  808. /* Enable default isc for I/O subchannels. */
  809. isc_register(IO_SCH_ISC);
  810. return 0;
  811. out_file:
  812. if (css_chsc_characteristics.secm)
  813. device_remove_file(&channel_subsystems[i]->device,
  814. &dev_attr_cm_enable);
  815. out_device:
  816. device_unregister(&channel_subsystems[i]->device);
  817. out_unregister:
  818. while (i > 0) {
  819. struct channel_subsystem *css;
  820. i--;
  821. css = channel_subsystems[i];
  822. device_unregister(&css->pseudo_subchannel->dev);
  823. css->pseudo_subchannel = NULL;
  824. if (css_chsc_characteristics.secm)
  825. device_remove_file(&css->device,
  826. &dev_attr_cm_enable);
  827. device_unregister(&css->device);
  828. }
  829. bus_unregister(&css_bus_type);
  830. out:
  831. crw_unregister_handler(CRW_RSC_SCH);
  832. idset_free(slow_subchannel_set);
  833. chsc_init_cleanup();
  834. pr_alert("The CSS device driver initialization failed with "
  835. "errno=%d\n", ret);
  836. return ret;
  837. }
  838. static void __init css_bus_cleanup(void)
  839. {
  840. struct channel_subsystem *css;
  841. int i;
  842. for (i = 0; i <= __MAX_CSSID; i++) {
  843. css = channel_subsystems[i];
  844. device_unregister(&css->pseudo_subchannel->dev);
  845. css->pseudo_subchannel = NULL;
  846. if (css_chsc_characteristics.secm)
  847. device_remove_file(&css->device, &dev_attr_cm_enable);
  848. device_unregister(&css->device);
  849. }
  850. bus_unregister(&css_bus_type);
  851. crw_unregister_handler(CRW_RSC_SCH);
  852. idset_free(slow_subchannel_set);
  853. chsc_init_cleanup();
  854. isc_unregister(IO_SCH_ISC);
  855. }
  856. static int __init channel_subsystem_init(void)
  857. {
  858. int ret;
  859. ret = css_bus_init();
  860. if (ret)
  861. return ret;
  862. cio_work_q = create_singlethread_workqueue("cio");
  863. if (!cio_work_q) {
  864. ret = -ENOMEM;
  865. goto out_bus;
  866. }
  867. ret = io_subchannel_init();
  868. if (ret)
  869. goto out_wq;
  870. return ret;
  871. out_wq:
  872. destroy_workqueue(cio_work_q);
  873. out_bus:
  874. css_bus_cleanup();
  875. return ret;
  876. }
  877. subsys_initcall(channel_subsystem_init);
  878. static int css_settle(struct device_driver *drv, void *unused)
  879. {
  880. struct css_driver *cssdrv = to_cssdriver(drv);
  881. if (cssdrv->settle)
  882. return cssdrv->settle();
  883. return 0;
  884. }
  885. int css_complete_work(void)
  886. {
  887. int ret;
  888. /* Wait for the evaluation of subchannels to finish. */
  889. ret = wait_event_interruptible(css_eval_wq,
  890. atomic_read(&css_eval_scheduled) == 0);
  891. if (ret)
  892. return -EINTR;
  893. flush_workqueue(cio_work_q);
  894. /* Wait for the subchannel type specific initialization to finish */
  895. return bus_for_each_drv(&css_bus_type, NULL, NULL, css_settle);
  896. }
  897. /*
  898. * Wait for the initialization of devices to finish, to make sure we are
  899. * done with our setup if the search for the root device starts.
  900. */
  901. static int __init channel_subsystem_init_sync(void)
  902. {
  903. /* Start initial subchannel evaluation. */
  904. css_schedule_eval_all();
  905. css_complete_work();
  906. return 0;
  907. }
  908. subsys_initcall_sync(channel_subsystem_init_sync);
  909. void channel_subsystem_reinit(void)
  910. {
  911. struct channel_path *chp;
  912. struct chp_id chpid;
  913. chsc_enable_facility(CHSC_SDA_OC_MSS);
  914. chp_id_for_each(&chpid) {
  915. chp = chpid_to_chp(chpid);
  916. if (!chp)
  917. continue;
  918. chsc_determine_base_channel_path_desc(chpid, &chp->desc);
  919. }
  920. }
  921. #ifdef CONFIG_PROC_FS
  922. static ssize_t cio_settle_write(struct file *file, const char __user *buf,
  923. size_t count, loff_t *ppos)
  924. {
  925. int ret;
  926. /* Handle pending CRW's. */
  927. crw_wait_for_channel_report();
  928. ret = css_complete_work();
  929. return ret ? ret : count;
  930. }
  931. static const struct file_operations cio_settle_proc_fops = {
  932. .open = nonseekable_open,
  933. .write = cio_settle_write,
  934. .llseek = no_llseek,
  935. };
  936. static int __init cio_settle_init(void)
  937. {
  938. struct proc_dir_entry *entry;
  939. entry = proc_create("cio_settle", S_IWUSR, NULL,
  940. &cio_settle_proc_fops);
  941. if (!entry)
  942. return -ENOMEM;
  943. return 0;
  944. }
  945. device_initcall(cio_settle_init);
  946. #endif /*CONFIG_PROC_FS*/
  947. int sch_is_pseudo_sch(struct subchannel *sch)
  948. {
  949. return sch == to_css(sch->dev.parent)->pseudo_subchannel;
  950. }
  951. static int css_bus_match(struct device *dev, struct device_driver *drv)
  952. {
  953. struct subchannel *sch = to_subchannel(dev);
  954. struct css_driver *driver = to_cssdriver(drv);
  955. struct css_device_id *id;
  956. for (id = driver->subchannel_type; id->match_flags; id++) {
  957. if (sch->st == id->type)
  958. return 1;
  959. }
  960. return 0;
  961. }
  962. static int css_probe(struct device *dev)
  963. {
  964. struct subchannel *sch;
  965. int ret;
  966. sch = to_subchannel(dev);
  967. sch->driver = to_cssdriver(dev->driver);
  968. ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
  969. if (ret)
  970. sch->driver = NULL;
  971. return ret;
  972. }
  973. static int css_remove(struct device *dev)
  974. {
  975. struct subchannel *sch;
  976. int ret;
  977. sch = to_subchannel(dev);
  978. ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
  979. sch->driver = NULL;
  980. return ret;
  981. }
  982. static void css_shutdown(struct device *dev)
  983. {
  984. struct subchannel *sch;
  985. sch = to_subchannel(dev);
  986. if (sch->driver && sch->driver->shutdown)
  987. sch->driver->shutdown(sch);
  988. }
  989. static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
  990. {
  991. struct subchannel *sch = to_subchannel(dev);
  992. int ret;
  993. ret = add_uevent_var(env, "ST=%01X", sch->st);
  994. if (ret)
  995. return ret;
  996. ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
  997. return ret;
  998. }
  999. static int css_pm_prepare(struct device *dev)
  1000. {
  1001. struct subchannel *sch = to_subchannel(dev);
  1002. struct css_driver *drv;
  1003. if (mutex_is_locked(&sch->reg_mutex))
  1004. return -EAGAIN;
  1005. if (!sch->dev.driver)
  1006. return 0;
  1007. drv = to_cssdriver(sch->dev.driver);
  1008. /* Notify drivers that they may not register children. */
  1009. return drv->prepare ? drv->prepare(sch) : 0;
  1010. }
  1011. static void css_pm_complete(struct device *dev)
  1012. {
  1013. struct subchannel *sch = to_subchannel(dev);
  1014. struct css_driver *drv;
  1015. if (!sch->dev.driver)
  1016. return;
  1017. drv = to_cssdriver(sch->dev.driver);
  1018. if (drv->complete)
  1019. drv->complete(sch);
  1020. }
  1021. static int css_pm_freeze(struct device *dev)
  1022. {
  1023. struct subchannel *sch = to_subchannel(dev);
  1024. struct css_driver *drv;
  1025. if (!sch->dev.driver)
  1026. return 0;
  1027. drv = to_cssdriver(sch->dev.driver);
  1028. return drv->freeze ? drv->freeze(sch) : 0;
  1029. }
  1030. static int css_pm_thaw(struct device *dev)
  1031. {
  1032. struct subchannel *sch = to_subchannel(dev);
  1033. struct css_driver *drv;
  1034. if (!sch->dev.driver)
  1035. return 0;
  1036. drv = to_cssdriver(sch->dev.driver);
  1037. return drv->thaw ? drv->thaw(sch) : 0;
  1038. }
  1039. static int css_pm_restore(struct device *dev)
  1040. {
  1041. struct subchannel *sch = to_subchannel(dev);
  1042. struct css_driver *drv;
  1043. css_update_ssd_info(sch);
  1044. if (!sch->dev.driver)
  1045. return 0;
  1046. drv = to_cssdriver(sch->dev.driver);
  1047. return drv->restore ? drv->restore(sch) : 0;
  1048. }
  1049. static const struct dev_pm_ops css_pm_ops = {
  1050. .prepare = css_pm_prepare,
  1051. .complete = css_pm_complete,
  1052. .freeze = css_pm_freeze,
  1053. .thaw = css_pm_thaw,
  1054. .restore = css_pm_restore,
  1055. };
  1056. static struct bus_type css_bus_type = {
  1057. .name = "css",
  1058. .match = css_bus_match,
  1059. .probe = css_probe,
  1060. .remove = css_remove,
  1061. .shutdown = css_shutdown,
  1062. .uevent = css_uevent,
  1063. .pm = &css_pm_ops,
  1064. };
  1065. /**
  1066. * css_driver_register - register a css driver
  1067. * @cdrv: css driver to register
  1068. *
  1069. * This is mainly a wrapper around driver_register that sets name
  1070. * and bus_type in the embedded struct device_driver correctly.
  1071. */
  1072. int css_driver_register(struct css_driver *cdrv)
  1073. {
  1074. cdrv->drv.bus = &css_bus_type;
  1075. return driver_register(&cdrv->drv);
  1076. }
  1077. EXPORT_SYMBOL_GPL(css_driver_register);
  1078. /**
  1079. * css_driver_unregister - unregister a css driver
  1080. * @cdrv: css driver to unregister
  1081. *
  1082. * This is a wrapper around driver_unregister.
  1083. */
  1084. void css_driver_unregister(struct css_driver *cdrv)
  1085. {
  1086. driver_unregister(&cdrv->drv);
  1087. }
  1088. EXPORT_SYMBOL_GPL(css_driver_unregister);
  1089. MODULE_LICENSE("GPL");