css.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. /*
  2. * drivers/s390/cio/css.c
  3. * driver for channel subsystem
  4. *
  5. * Copyright IBM Corp. 2002,2008
  6. * Author(s): Arnd Bergmann (arndb@de.ibm.com)
  7. * Cornelia Huck (cornelia.huck@de.ibm.com)
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/device.h>
  12. #include <linux/slab.h>
  13. #include <linux/errno.h>
  14. #include <linux/list.h>
  15. #include <linux/reboot.h>
  16. #include <asm/isc.h>
  17. #include "../s390mach.h"
  18. #include "css.h"
  19. #include "cio.h"
  20. #include "cio_debug.h"
  21. #include "ioasm.h"
  22. #include "chsc.h"
  23. #include "device.h"
  24. #include "idset.h"
  25. #include "chp.h"
  26. int css_init_done = 0;
  27. static int need_reprobe = 0;
  28. static int max_ssid = 0;
  29. struct channel_subsystem *channel_subsystems[__MAX_CSSID + 1];
  30. int
  31. for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
  32. {
  33. struct subchannel_id schid;
  34. int ret;
  35. init_subchannel_id(&schid);
  36. ret = -ENODEV;
  37. do {
  38. do {
  39. ret = fn(schid, data);
  40. if (ret)
  41. break;
  42. } while (schid.sch_no++ < __MAX_SUBCHANNEL);
  43. schid.sch_no = 0;
  44. } while (schid.ssid++ < max_ssid);
  45. return ret;
  46. }
  47. struct cb_data {
  48. void *data;
  49. struct idset *set;
  50. int (*fn_known_sch)(struct subchannel *, void *);
  51. int (*fn_unknown_sch)(struct subchannel_id, void *);
  52. };
  53. static int call_fn_known_sch(struct device *dev, void *data)
  54. {
  55. struct subchannel *sch = to_subchannel(dev);
  56. struct cb_data *cb = data;
  57. int rc = 0;
  58. idset_sch_del(cb->set, sch->schid);
  59. if (cb->fn_known_sch)
  60. rc = cb->fn_known_sch(sch, cb->data);
  61. return rc;
  62. }
  63. static int call_fn_unknown_sch(struct subchannel_id schid, void *data)
  64. {
  65. struct cb_data *cb = data;
  66. int rc = 0;
  67. if (idset_sch_contains(cb->set, schid))
  68. rc = cb->fn_unknown_sch(schid, cb->data);
  69. return rc;
  70. }
  71. int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *),
  72. int (*fn_unknown)(struct subchannel_id,
  73. void *), void *data)
  74. {
  75. struct cb_data cb;
  76. int rc;
  77. cb.set = idset_sch_new();
  78. if (!cb.set)
  79. return -ENOMEM;
  80. idset_fill(cb.set);
  81. cb.data = data;
  82. cb.fn_known_sch = fn_known;
  83. cb.fn_unknown_sch = fn_unknown;
  84. /* Process registered subchannels. */
  85. rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
  86. if (rc)
  87. goto out;
  88. /* Process unregistered subchannels. */
  89. if (fn_unknown)
  90. rc = for_each_subchannel(call_fn_unknown_sch, &cb);
  91. out:
  92. idset_free(cb.set);
  93. return rc;
  94. }
  95. static struct subchannel *
  96. css_alloc_subchannel(struct subchannel_id schid)
  97. {
  98. struct subchannel *sch;
  99. int ret;
  100. sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
  101. if (sch == NULL)
  102. return ERR_PTR(-ENOMEM);
  103. ret = cio_validate_subchannel (sch, schid);
  104. if (ret < 0) {
  105. kfree(sch);
  106. return ERR_PTR(ret);
  107. }
  108. return sch;
  109. }
  110. static void
  111. css_free_subchannel(struct subchannel *sch)
  112. {
  113. if (sch) {
  114. /* Reset intparm to zeroes. */
  115. sch->schib.pmcw.intparm = 0;
  116. cio_modify(sch);
  117. kfree(sch->lock);
  118. kfree(sch);
  119. }
  120. }
  121. static void
  122. css_subchannel_release(struct device *dev)
  123. {
  124. struct subchannel *sch;
  125. sch = to_subchannel(dev);
  126. if (!cio_is_console(sch->schid)) {
  127. kfree(sch->lock);
  128. kfree(sch);
  129. }
  130. }
  131. static int css_sch_device_register(struct subchannel *sch)
  132. {
  133. int ret;
  134. mutex_lock(&sch->reg_mutex);
  135. ret = device_register(&sch->dev);
  136. mutex_unlock(&sch->reg_mutex);
  137. return ret;
  138. }
  139. /**
  140. * css_sch_device_unregister - unregister a subchannel
  141. * @sch: subchannel to be unregistered
  142. */
  143. void css_sch_device_unregister(struct subchannel *sch)
  144. {
  145. mutex_lock(&sch->reg_mutex);
  146. if (device_is_registered(&sch->dev))
  147. device_unregister(&sch->dev);
  148. mutex_unlock(&sch->reg_mutex);
  149. }
  150. EXPORT_SYMBOL_GPL(css_sch_device_unregister);
  151. static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
  152. {
  153. int i;
  154. int mask;
  155. memset(ssd, 0, sizeof(struct chsc_ssd_info));
  156. ssd->path_mask = pmcw->pim;
  157. for (i = 0; i < 8; i++) {
  158. mask = 0x80 >> i;
  159. if (pmcw->pim & mask) {
  160. chp_id_init(&ssd->chpid[i]);
  161. ssd->chpid[i].id = pmcw->chpid[i];
  162. }
  163. }
  164. }
  165. static void ssd_register_chpids(struct chsc_ssd_info *ssd)
  166. {
  167. int i;
  168. int mask;
  169. for (i = 0; i < 8; i++) {
  170. mask = 0x80 >> i;
  171. if (ssd->path_mask & mask)
  172. if (!chp_is_registered(ssd->chpid[i]))
  173. chp_new(ssd->chpid[i]);
  174. }
  175. }
  176. void css_update_ssd_info(struct subchannel *sch)
  177. {
  178. int ret;
  179. if (cio_is_console(sch->schid)) {
  180. /* Console is initialized too early for functions requiring
  181. * memory allocation. */
  182. ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
  183. } else {
  184. ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
  185. if (ret)
  186. ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
  187. ssd_register_chpids(&sch->ssd_info);
  188. }
  189. }
  190. static ssize_t type_show(struct device *dev, struct device_attribute *attr,
  191. char *buf)
  192. {
  193. struct subchannel *sch = to_subchannel(dev);
  194. return sprintf(buf, "%01x\n", sch->st);
  195. }
  196. static DEVICE_ATTR(type, 0444, type_show, NULL);
  197. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  198. char *buf)
  199. {
  200. struct subchannel *sch = to_subchannel(dev);
  201. return sprintf(buf, "css:t%01X\n", sch->st);
  202. }
  203. static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
  204. static struct attribute *subch_attrs[] = {
  205. &dev_attr_type.attr,
  206. &dev_attr_modalias.attr,
  207. NULL,
  208. };
  209. static struct attribute_group subch_attr_group = {
  210. .attrs = subch_attrs,
  211. };
  212. static struct attribute_group *default_subch_attr_groups[] = {
  213. &subch_attr_group,
  214. NULL,
  215. };
  216. static int css_register_subchannel(struct subchannel *sch)
  217. {
  218. int ret;
  219. /* Initialize the subchannel structure */
  220. sch->dev.parent = &channel_subsystems[0]->device;
  221. sch->dev.bus = &css_bus_type;
  222. sch->dev.release = &css_subchannel_release;
  223. sch->dev.groups = default_subch_attr_groups;
  224. /*
  225. * We don't want to generate uevents for I/O subchannels that don't
  226. * have a working ccw device behind them since they will be
  227. * unregistered before they can be used anyway, so we delay the add
  228. * uevent until after device recognition was successful.
  229. * Note that we suppress the uevent for all subchannel types;
  230. * the subchannel driver can decide itself when it wants to inform
  231. * userspace of its existence.
  232. */
  233. sch->dev.uevent_suppress = 1;
  234. css_update_ssd_info(sch);
  235. /* make it known to the system */
  236. ret = css_sch_device_register(sch);
  237. if (ret) {
  238. CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
  239. sch->schid.ssid, sch->schid.sch_no, ret);
  240. return ret;
  241. }
  242. if (!sch->driver) {
  243. /*
  244. * No driver matched. Generate the uevent now so that
  245. * a fitting driver module may be loaded based on the
  246. * modalias.
  247. */
  248. sch->dev.uevent_suppress = 0;
  249. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  250. }
  251. return ret;
  252. }
  253. int css_probe_device(struct subchannel_id schid)
  254. {
  255. int ret;
  256. struct subchannel *sch;
  257. sch = css_alloc_subchannel(schid);
  258. if (IS_ERR(sch))
  259. return PTR_ERR(sch);
  260. ret = css_register_subchannel(sch);
  261. if (ret)
  262. css_free_subchannel(sch);
  263. return ret;
  264. }
  265. static int
  266. check_subchannel(struct device * dev, void * data)
  267. {
  268. struct subchannel *sch;
  269. struct subchannel_id *schid = data;
  270. sch = to_subchannel(dev);
  271. return schid_equal(&sch->schid, schid);
  272. }
  273. struct subchannel *
  274. get_subchannel_by_schid(struct subchannel_id schid)
  275. {
  276. struct device *dev;
  277. dev = bus_find_device(&css_bus_type, NULL,
  278. &schid, check_subchannel);
  279. return dev ? to_subchannel(dev) : NULL;
  280. }
  281. /**
  282. * css_sch_is_valid() - check if a subchannel is valid
  283. * @schib: subchannel information block for the subchannel
  284. */
  285. int css_sch_is_valid(struct schib *schib)
  286. {
  287. if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
  288. return 0;
  289. if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
  290. return 0;
  291. return 1;
  292. }
  293. EXPORT_SYMBOL_GPL(css_sch_is_valid);
  294. static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
  295. {
  296. struct schib schib;
  297. if (!slow) {
  298. /* Will be done on the slow path. */
  299. return -EAGAIN;
  300. }
  301. if (stsch_err(schid, &schib) || !css_sch_is_valid(&schib)) {
  302. /* Unusable - ignore. */
  303. return 0;
  304. }
  305. CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
  306. "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
  307. return css_probe_device(schid);
  308. }
  309. static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
  310. {
  311. int ret = 0;
  312. if (sch->driver) {
  313. if (sch->driver->sch_event)
  314. ret = sch->driver->sch_event(sch, slow);
  315. else
  316. dev_dbg(&sch->dev,
  317. "Got subchannel machine check but "
  318. "no sch_event handler provided.\n");
  319. }
  320. return ret;
  321. }
  322. static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
  323. {
  324. struct subchannel *sch;
  325. int ret;
  326. sch = get_subchannel_by_schid(schid);
  327. if (sch) {
  328. ret = css_evaluate_known_subchannel(sch, slow);
  329. put_device(&sch->dev);
  330. } else
  331. ret = css_evaluate_new_subchannel(schid, slow);
  332. if (ret == -EAGAIN)
  333. css_schedule_eval(schid);
  334. }
  335. static struct idset *slow_subchannel_set;
  336. static spinlock_t slow_subchannel_lock;
  337. static int __init slow_subchannel_init(void)
  338. {
  339. spin_lock_init(&slow_subchannel_lock);
  340. slow_subchannel_set = idset_sch_new();
  341. if (!slow_subchannel_set) {
  342. CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
  343. return -ENOMEM;
  344. }
  345. return 0;
  346. }
  347. static int slow_eval_known_fn(struct subchannel *sch, void *data)
  348. {
  349. int eval;
  350. int rc;
  351. spin_lock_irq(&slow_subchannel_lock);
  352. eval = idset_sch_contains(slow_subchannel_set, sch->schid);
  353. idset_sch_del(slow_subchannel_set, sch->schid);
  354. spin_unlock_irq(&slow_subchannel_lock);
  355. if (eval) {
  356. rc = css_evaluate_known_subchannel(sch, 1);
  357. if (rc == -EAGAIN)
  358. css_schedule_eval(sch->schid);
  359. }
  360. return 0;
  361. }
  362. static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
  363. {
  364. int eval;
  365. int rc = 0;
  366. spin_lock_irq(&slow_subchannel_lock);
  367. eval = idset_sch_contains(slow_subchannel_set, schid);
  368. idset_sch_del(slow_subchannel_set, schid);
  369. spin_unlock_irq(&slow_subchannel_lock);
  370. if (eval) {
  371. rc = css_evaluate_new_subchannel(schid, 1);
  372. switch (rc) {
  373. case -EAGAIN:
  374. css_schedule_eval(schid);
  375. rc = 0;
  376. break;
  377. case -ENXIO:
  378. case -ENOMEM:
  379. case -EIO:
  380. /* These should abort looping */
  381. break;
  382. default:
  383. rc = 0;
  384. }
  385. }
  386. return rc;
  387. }
  388. static void css_slow_path_func(struct work_struct *unused)
  389. {
  390. CIO_TRACE_EVENT(4, "slowpath");
  391. for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
  392. NULL);
  393. }
  394. static DECLARE_WORK(slow_path_work, css_slow_path_func);
  395. struct workqueue_struct *slow_path_wq;
  396. void css_schedule_eval(struct subchannel_id schid)
  397. {
  398. unsigned long flags;
  399. spin_lock_irqsave(&slow_subchannel_lock, flags);
  400. idset_sch_add(slow_subchannel_set, schid);
  401. queue_work(slow_path_wq, &slow_path_work);
  402. spin_unlock_irqrestore(&slow_subchannel_lock, flags);
  403. }
  404. void css_schedule_eval_all(void)
  405. {
  406. unsigned long flags;
  407. spin_lock_irqsave(&slow_subchannel_lock, flags);
  408. idset_fill(slow_subchannel_set);
  409. queue_work(slow_path_wq, &slow_path_work);
  410. spin_unlock_irqrestore(&slow_subchannel_lock, flags);
  411. }
  412. void css_wait_for_slow_path(void)
  413. {
  414. flush_workqueue(ccw_device_notify_work);
  415. flush_workqueue(slow_path_wq);
  416. }
  417. /* Reprobe subchannel if unregistered. */
  418. static int reprobe_subchannel(struct subchannel_id schid, void *data)
  419. {
  420. int ret;
  421. CIO_MSG_EVENT(6, "cio: reprobe 0.%x.%04x\n",
  422. schid.ssid, schid.sch_no);
  423. if (need_reprobe)
  424. return -EAGAIN;
  425. ret = css_probe_device(schid);
  426. switch (ret) {
  427. case 0:
  428. break;
  429. case -ENXIO:
  430. case -ENOMEM:
  431. case -EIO:
  432. /* These should abort looping */
  433. break;
  434. default:
  435. ret = 0;
  436. }
  437. return ret;
  438. }
  439. /* Work function used to reprobe all unregistered subchannels. */
  440. static void reprobe_all(struct work_struct *unused)
  441. {
  442. int ret;
  443. CIO_MSG_EVENT(4, "reprobe start\n");
  444. need_reprobe = 0;
  445. /* Make sure initial subchannel scan is done. */
  446. wait_event(ccw_device_init_wq,
  447. atomic_read(&ccw_device_init_count) == 0);
  448. ret = for_each_subchannel_staged(NULL, reprobe_subchannel, NULL);
  449. CIO_MSG_EVENT(4, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
  450. need_reprobe);
  451. }
  452. static DECLARE_WORK(css_reprobe_work, reprobe_all);
  453. /* Schedule reprobing of all unregistered subchannels. */
  454. void css_schedule_reprobe(void)
  455. {
  456. need_reprobe = 1;
  457. queue_work(slow_path_wq, &css_reprobe_work);
  458. }
  459. EXPORT_SYMBOL_GPL(css_schedule_reprobe);
  460. /*
  461. * Called from the machine check handler for subchannel report words.
  462. */
  463. static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
  464. {
  465. struct subchannel_id mchk_schid;
  466. if (overflow) {
  467. css_schedule_eval_all();
  468. return;
  469. }
  470. CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
  471. "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
  472. crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
  473. crw0->erc, crw0->rsid);
  474. if (crw1)
  475. CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
  476. "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
  477. crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
  478. crw1->anc, crw1->erc, crw1->rsid);
  479. init_subchannel_id(&mchk_schid);
  480. mchk_schid.sch_no = crw0->rsid;
  481. if (crw1)
  482. mchk_schid.ssid = (crw1->rsid >> 8) & 3;
  483. /*
  484. * Since we are always presented with IPI in the CRW, we have to
  485. * use stsch() to find out if the subchannel in question has come
  486. * or gone.
  487. */
  488. css_evaluate_subchannel(mchk_schid, 0);
  489. }
  490. static int __init
  491. __init_channel_subsystem(struct subchannel_id schid, void *data)
  492. {
  493. struct subchannel *sch;
  494. int ret;
  495. if (cio_is_console(schid))
  496. sch = cio_get_console_subchannel();
  497. else {
  498. sch = css_alloc_subchannel(schid);
  499. if (IS_ERR(sch))
  500. ret = PTR_ERR(sch);
  501. else
  502. ret = 0;
  503. switch (ret) {
  504. case 0:
  505. break;
  506. case -ENOMEM:
  507. panic("Out of memory in init_channel_subsystem\n");
  508. /* -ENXIO: no more subchannels. */
  509. case -ENXIO:
  510. return ret;
  511. /* -EIO: this subchannel set not supported. */
  512. case -EIO:
  513. return ret;
  514. default:
  515. return 0;
  516. }
  517. }
  518. /*
  519. * We register ALL valid subchannels in ioinfo, even those
  520. * that have been present before init_channel_subsystem.
  521. * These subchannels can't have been registered yet (kmalloc
  522. * not working) so we do it now. This is true e.g. for the
  523. * console subchannel.
  524. */
  525. css_register_subchannel(sch);
  526. return 0;
  527. }
  528. static void __init
  529. css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
  530. {
  531. if (css_general_characteristics.mcss) {
  532. css->global_pgid.pgid_high.ext_cssid.version = 0x80;
  533. css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
  534. } else {
  535. #ifdef CONFIG_SMP
  536. css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
  537. #else
  538. css->global_pgid.pgid_high.cpu_addr = 0;
  539. #endif
  540. }
  541. css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
  542. css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
  543. css->global_pgid.tod_high = tod_high;
  544. }
  545. static void
  546. channel_subsystem_release(struct device *dev)
  547. {
  548. struct channel_subsystem *css;
  549. css = to_css(dev);
  550. mutex_destroy(&css->mutex);
  551. kfree(css);
  552. }
  553. static ssize_t
  554. css_cm_enable_show(struct device *dev, struct device_attribute *attr,
  555. char *buf)
  556. {
  557. struct channel_subsystem *css = to_css(dev);
  558. int ret;
  559. if (!css)
  560. return 0;
  561. mutex_lock(&css->mutex);
  562. ret = sprintf(buf, "%x\n", css->cm_enabled);
  563. mutex_unlock(&css->mutex);
  564. return ret;
  565. }
  566. static ssize_t
  567. css_cm_enable_store(struct device *dev, struct device_attribute *attr,
  568. const char *buf, size_t count)
  569. {
  570. struct channel_subsystem *css = to_css(dev);
  571. int ret;
  572. unsigned long val;
  573. ret = strict_strtoul(buf, 16, &val);
  574. if (ret)
  575. return ret;
  576. mutex_lock(&css->mutex);
  577. switch (val) {
  578. case 0:
  579. ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
  580. break;
  581. case 1:
  582. ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
  583. break;
  584. default:
  585. ret = -EINVAL;
  586. }
  587. mutex_unlock(&css->mutex);
  588. return ret < 0 ? ret : count;
  589. }
  590. static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
  591. static int __init setup_css(int nr)
  592. {
  593. u32 tod_high;
  594. int ret;
  595. struct channel_subsystem *css;
  596. css = channel_subsystems[nr];
  597. memset(css, 0, sizeof(struct channel_subsystem));
  598. css->pseudo_subchannel =
  599. kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
  600. if (!css->pseudo_subchannel)
  601. return -ENOMEM;
  602. css->pseudo_subchannel->dev.parent = &css->device;
  603. css->pseudo_subchannel->dev.release = css_subchannel_release;
  604. sprintf(css->pseudo_subchannel->dev.bus_id, "defunct");
  605. ret = cio_create_sch_lock(css->pseudo_subchannel);
  606. if (ret) {
  607. kfree(css->pseudo_subchannel);
  608. return ret;
  609. }
  610. mutex_init(&css->mutex);
  611. css->valid = 1;
  612. css->cssid = nr;
  613. sprintf(css->device.bus_id, "css%x", nr);
  614. css->device.release = channel_subsystem_release;
  615. tod_high = (u32) (get_clock() >> 32);
  616. css_generate_pgid(css, tod_high);
  617. return 0;
  618. }
  619. static int css_reboot_event(struct notifier_block *this,
  620. unsigned long event,
  621. void *ptr)
  622. {
  623. int ret, i;
  624. ret = NOTIFY_DONE;
  625. for (i = 0; i <= __MAX_CSSID; i++) {
  626. struct channel_subsystem *css;
  627. css = channel_subsystems[i];
  628. mutex_lock(&css->mutex);
  629. if (css->cm_enabled)
  630. if (chsc_secm(css, 0))
  631. ret = NOTIFY_BAD;
  632. mutex_unlock(&css->mutex);
  633. }
  634. return ret;
  635. }
  636. static struct notifier_block css_reboot_notifier = {
  637. .notifier_call = css_reboot_event,
  638. };
  639. /*
  640. * Now that the driver core is running, we can setup our channel subsystem.
  641. * The struct subchannel's are created during probing (except for the
  642. * static console subchannel).
  643. */
  644. static int __init
  645. init_channel_subsystem (void)
  646. {
  647. int ret, i;
  648. ret = chsc_determine_css_characteristics();
  649. if (ret == -ENOMEM)
  650. goto out; /* No need to continue. */
  651. ret = chsc_alloc_sei_area();
  652. if (ret)
  653. goto out;
  654. ret = slow_subchannel_init();
  655. if (ret)
  656. goto out;
  657. ret = s390_register_crw_handler(CRW_RSC_SCH, css_process_crw);
  658. if (ret)
  659. goto out;
  660. if ((ret = bus_register(&css_bus_type)))
  661. goto out;
  662. /* Try to enable MSS. */
  663. ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
  664. switch (ret) {
  665. case 0: /* Success. */
  666. max_ssid = __MAX_SSID;
  667. break;
  668. case -ENOMEM:
  669. goto out_bus;
  670. default:
  671. max_ssid = 0;
  672. }
  673. /* Setup css structure. */
  674. for (i = 0; i <= __MAX_CSSID; i++) {
  675. struct channel_subsystem *css;
  676. css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
  677. if (!css) {
  678. ret = -ENOMEM;
  679. goto out_unregister;
  680. }
  681. channel_subsystems[i] = css;
  682. ret = setup_css(i);
  683. if (ret)
  684. goto out_free;
  685. ret = device_register(&css->device);
  686. if (ret)
  687. goto out_free_all;
  688. if (css_chsc_characteristics.secm) {
  689. ret = device_create_file(&css->device,
  690. &dev_attr_cm_enable);
  691. if (ret)
  692. goto out_device;
  693. }
  694. ret = device_register(&css->pseudo_subchannel->dev);
  695. if (ret)
  696. goto out_file;
  697. }
  698. ret = register_reboot_notifier(&css_reboot_notifier);
  699. if (ret)
  700. goto out_pseudo;
  701. css_init_done = 1;
  702. /* Enable default isc for I/O subchannels. */
  703. isc_register(IO_SCH_ISC);
  704. for_each_subchannel(__init_channel_subsystem, NULL);
  705. return 0;
  706. out_pseudo:
  707. device_unregister(&channel_subsystems[i]->pseudo_subchannel->dev);
  708. out_file:
  709. device_remove_file(&channel_subsystems[i]->device,
  710. &dev_attr_cm_enable);
  711. out_device:
  712. device_unregister(&channel_subsystems[i]->device);
  713. out_free_all:
  714. kfree(channel_subsystems[i]->pseudo_subchannel->lock);
  715. kfree(channel_subsystems[i]->pseudo_subchannel);
  716. out_free:
  717. kfree(channel_subsystems[i]);
  718. out_unregister:
  719. while (i > 0) {
  720. struct channel_subsystem *css;
  721. i--;
  722. css = channel_subsystems[i];
  723. device_unregister(&css->pseudo_subchannel->dev);
  724. if (css_chsc_characteristics.secm)
  725. device_remove_file(&css->device,
  726. &dev_attr_cm_enable);
  727. device_unregister(&css->device);
  728. }
  729. out_bus:
  730. bus_unregister(&css_bus_type);
  731. out:
  732. s390_unregister_crw_handler(CRW_RSC_CSS);
  733. chsc_free_sei_area();
  734. kfree(slow_subchannel_set);
  735. printk(KERN_WARNING"cio: failed to initialize css driver (%d)!\n",
  736. ret);
  737. return ret;
  738. }
  739. int sch_is_pseudo_sch(struct subchannel *sch)
  740. {
  741. return sch == to_css(sch->dev.parent)->pseudo_subchannel;
  742. }
  743. static int css_bus_match(struct device *dev, struct device_driver *drv)
  744. {
  745. struct subchannel *sch = to_subchannel(dev);
  746. struct css_driver *driver = to_cssdriver(drv);
  747. struct css_device_id *id;
  748. for (id = driver->subchannel_type; id->match_flags; id++) {
  749. if (sch->st == id->type)
  750. return 1;
  751. }
  752. return 0;
  753. }
  754. static int css_probe(struct device *dev)
  755. {
  756. struct subchannel *sch;
  757. int ret;
  758. sch = to_subchannel(dev);
  759. sch->driver = to_cssdriver(dev->driver);
  760. ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
  761. if (ret)
  762. sch->driver = NULL;
  763. return ret;
  764. }
  765. static int css_remove(struct device *dev)
  766. {
  767. struct subchannel *sch;
  768. int ret;
  769. sch = to_subchannel(dev);
  770. ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
  771. sch->driver = NULL;
  772. return ret;
  773. }
  774. static void css_shutdown(struct device *dev)
  775. {
  776. struct subchannel *sch;
  777. sch = to_subchannel(dev);
  778. if (sch->driver && sch->driver->shutdown)
  779. sch->driver->shutdown(sch);
  780. }
  781. static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
  782. {
  783. struct subchannel *sch = to_subchannel(dev);
  784. int ret;
  785. ret = add_uevent_var(env, "ST=%01X", sch->st);
  786. if (ret)
  787. return ret;
  788. ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
  789. return ret;
  790. }
  791. struct bus_type css_bus_type = {
  792. .name = "css",
  793. .match = css_bus_match,
  794. .probe = css_probe,
  795. .remove = css_remove,
  796. .shutdown = css_shutdown,
  797. .uevent = css_uevent,
  798. };
  799. /**
  800. * css_driver_register - register a css driver
  801. * @cdrv: css driver to register
  802. *
  803. * This is mainly a wrapper around driver_register that sets name
  804. * and bus_type in the embedded struct device_driver correctly.
  805. */
  806. int css_driver_register(struct css_driver *cdrv)
  807. {
  808. cdrv->drv.name = cdrv->name;
  809. cdrv->drv.bus = &css_bus_type;
  810. cdrv->drv.owner = cdrv->owner;
  811. return driver_register(&cdrv->drv);
  812. }
  813. EXPORT_SYMBOL_GPL(css_driver_register);
  814. /**
  815. * css_driver_unregister - unregister a css driver
  816. * @cdrv: css driver to unregister
  817. *
  818. * This is a wrapper around driver_unregister.
  819. */
  820. void css_driver_unregister(struct css_driver *cdrv)
  821. {
  822. driver_unregister(&cdrv->drv);
  823. }
  824. EXPORT_SYMBOL_GPL(css_driver_unregister);
  825. subsys_initcall(init_channel_subsystem);
  826. MODULE_LICENSE("GPL");
  827. EXPORT_SYMBOL(css_bus_type);