css.c 29 KB

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