css.c 26 KB

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