css.c 21 KB

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