css.c 29 KB

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