css.c 29 KB

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