css.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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(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. if (css->pseudo_subchannel) {
  551. /* Implies that it has been generated but never registered. */
  552. css_subchannel_release(&css->pseudo_subchannel->dev);
  553. css->pseudo_subchannel = NULL;
  554. }
  555. kfree(css);
  556. }
  557. static ssize_t
  558. css_cm_enable_show(struct device *dev, struct device_attribute *attr,
  559. char *buf)
  560. {
  561. struct channel_subsystem *css = to_css(dev);
  562. int ret;
  563. if (!css)
  564. return 0;
  565. mutex_lock(&css->mutex);
  566. ret = sprintf(buf, "%x\n", css->cm_enabled);
  567. mutex_unlock(&css->mutex);
  568. return ret;
  569. }
  570. static ssize_t
  571. css_cm_enable_store(struct device *dev, struct device_attribute *attr,
  572. const char *buf, size_t count)
  573. {
  574. struct channel_subsystem *css = to_css(dev);
  575. int ret;
  576. unsigned long val;
  577. ret = strict_strtoul(buf, 16, &val);
  578. if (ret)
  579. return ret;
  580. mutex_lock(&css->mutex);
  581. switch (val) {
  582. case 0:
  583. ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
  584. break;
  585. case 1:
  586. ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
  587. break;
  588. default:
  589. ret = -EINVAL;
  590. }
  591. mutex_unlock(&css->mutex);
  592. return ret < 0 ? ret : count;
  593. }
  594. static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
  595. static int __init setup_css(int nr)
  596. {
  597. u32 tod_high;
  598. int ret;
  599. struct channel_subsystem *css;
  600. css = channel_subsystems[nr];
  601. memset(css, 0, sizeof(struct channel_subsystem));
  602. css->pseudo_subchannel =
  603. kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
  604. if (!css->pseudo_subchannel)
  605. return -ENOMEM;
  606. css->pseudo_subchannel->dev.parent = &css->device;
  607. css->pseudo_subchannel->dev.release = css_subchannel_release;
  608. dev_set_name(&css->pseudo_subchannel->dev, "defunct");
  609. ret = cio_create_sch_lock(css->pseudo_subchannel);
  610. if (ret) {
  611. kfree(css->pseudo_subchannel);
  612. return ret;
  613. }
  614. mutex_init(&css->mutex);
  615. css->valid = 1;
  616. css->cssid = nr;
  617. dev_set_name(&css->device, "css%x", nr);
  618. css->device.release = channel_subsystem_release;
  619. tod_high = (u32) (get_clock() >> 32);
  620. css_generate_pgid(css, tod_high);
  621. return 0;
  622. }
  623. static int css_reboot_event(struct notifier_block *this,
  624. unsigned long event,
  625. void *ptr)
  626. {
  627. int ret, i;
  628. ret = NOTIFY_DONE;
  629. for (i = 0; i <= __MAX_CSSID; i++) {
  630. struct channel_subsystem *css;
  631. css = channel_subsystems[i];
  632. mutex_lock(&css->mutex);
  633. if (css->cm_enabled)
  634. if (chsc_secm(css, 0))
  635. ret = NOTIFY_BAD;
  636. mutex_unlock(&css->mutex);
  637. }
  638. return ret;
  639. }
  640. static struct notifier_block css_reboot_notifier = {
  641. .notifier_call = css_reboot_event,
  642. };
  643. /*
  644. * Now that the driver core is running, we can setup our channel subsystem.
  645. * The struct subchannel's are created during probing (except for the
  646. * static console subchannel).
  647. */
  648. static int __init
  649. init_channel_subsystem (void)
  650. {
  651. int ret, i;
  652. ret = chsc_determine_css_characteristics();
  653. if (ret == -ENOMEM)
  654. goto out; /* No need to continue. */
  655. ret = chsc_alloc_sei_area();
  656. if (ret)
  657. goto out;
  658. ret = slow_subchannel_init();
  659. if (ret)
  660. goto out;
  661. ret = s390_register_crw_handler(CRW_RSC_SCH, css_process_crw);
  662. if (ret)
  663. goto out;
  664. if ((ret = bus_register(&css_bus_type)))
  665. goto out;
  666. /* Try to enable MSS. */
  667. ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
  668. switch (ret) {
  669. case 0: /* Success. */
  670. max_ssid = __MAX_SSID;
  671. break;
  672. case -ENOMEM:
  673. goto out_bus;
  674. default:
  675. max_ssid = 0;
  676. }
  677. /* Setup css structure. */
  678. for (i = 0; i <= __MAX_CSSID; i++) {
  679. struct channel_subsystem *css;
  680. css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
  681. if (!css) {
  682. ret = -ENOMEM;
  683. goto out_unregister;
  684. }
  685. channel_subsystems[i] = css;
  686. ret = setup_css(i);
  687. if (ret) {
  688. kfree(channel_subsystems[i]);
  689. goto out_unregister;
  690. }
  691. ret = device_register(&css->device);
  692. if (ret) {
  693. put_device(&css->device);
  694. goto out_unregister;
  695. }
  696. if (css_chsc_characteristics.secm) {
  697. ret = device_create_file(&css->device,
  698. &dev_attr_cm_enable);
  699. if (ret)
  700. goto out_device;
  701. }
  702. ret = device_register(&css->pseudo_subchannel->dev);
  703. if (ret)
  704. goto out_file;
  705. }
  706. ret = register_reboot_notifier(&css_reboot_notifier);
  707. if (ret)
  708. goto out_unregister;
  709. css_init_done = 1;
  710. /* Enable default isc for I/O subchannels. */
  711. isc_register(IO_SCH_ISC);
  712. for_each_subchannel(__init_channel_subsystem, NULL);
  713. return 0;
  714. out_file:
  715. if (css_chsc_characteristics.secm)
  716. device_remove_file(&channel_subsystems[i]->device,
  717. &dev_attr_cm_enable);
  718. out_device:
  719. device_unregister(&channel_subsystems[i]->device);
  720. out_unregister:
  721. while (i > 0) {
  722. struct channel_subsystem *css;
  723. i--;
  724. css = channel_subsystems[i];
  725. device_unregister(&css->pseudo_subchannel->dev);
  726. css->pseudo_subchannel = NULL;
  727. if (css_chsc_characteristics.secm)
  728. device_remove_file(&css->device,
  729. &dev_attr_cm_enable);
  730. device_unregister(&css->device);
  731. }
  732. out_bus:
  733. bus_unregister(&css_bus_type);
  734. out:
  735. s390_unregister_crw_handler(CRW_RSC_CSS);
  736. chsc_free_sei_area();
  737. kfree(slow_subchannel_set);
  738. printk(KERN_WARNING"cio: failed to initialize css driver (%d)!\n",
  739. ret);
  740. return ret;
  741. }
  742. int sch_is_pseudo_sch(struct subchannel *sch)
  743. {
  744. return sch == to_css(sch->dev.parent)->pseudo_subchannel;
  745. }
  746. static int css_bus_match(struct device *dev, struct device_driver *drv)
  747. {
  748. struct subchannel *sch = to_subchannel(dev);
  749. struct css_driver *driver = to_cssdriver(drv);
  750. struct css_device_id *id;
  751. for (id = driver->subchannel_type; id->match_flags; id++) {
  752. if (sch->st == id->type)
  753. return 1;
  754. }
  755. return 0;
  756. }
  757. static int css_probe(struct device *dev)
  758. {
  759. struct subchannel *sch;
  760. int ret;
  761. sch = to_subchannel(dev);
  762. sch->driver = to_cssdriver(dev->driver);
  763. ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
  764. if (ret)
  765. sch->driver = NULL;
  766. return ret;
  767. }
  768. static int css_remove(struct device *dev)
  769. {
  770. struct subchannel *sch;
  771. int ret;
  772. sch = to_subchannel(dev);
  773. ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
  774. sch->driver = NULL;
  775. return ret;
  776. }
  777. static void css_shutdown(struct device *dev)
  778. {
  779. struct subchannel *sch;
  780. sch = to_subchannel(dev);
  781. if (sch->driver && sch->driver->shutdown)
  782. sch->driver->shutdown(sch);
  783. }
  784. static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
  785. {
  786. struct subchannel *sch = to_subchannel(dev);
  787. int ret;
  788. ret = add_uevent_var(env, "ST=%01X", sch->st);
  789. if (ret)
  790. return ret;
  791. ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
  792. return ret;
  793. }
  794. struct bus_type css_bus_type = {
  795. .name = "css",
  796. .match = css_bus_match,
  797. .probe = css_probe,
  798. .remove = css_remove,
  799. .shutdown = css_shutdown,
  800. .uevent = css_uevent,
  801. };
  802. /**
  803. * css_driver_register - register a css driver
  804. * @cdrv: css driver to register
  805. *
  806. * This is mainly a wrapper around driver_register that sets name
  807. * and bus_type in the embedded struct device_driver correctly.
  808. */
  809. int css_driver_register(struct css_driver *cdrv)
  810. {
  811. cdrv->drv.name = cdrv->name;
  812. cdrv->drv.bus = &css_bus_type;
  813. cdrv->drv.owner = cdrv->owner;
  814. return driver_register(&cdrv->drv);
  815. }
  816. EXPORT_SYMBOL_GPL(css_driver_register);
  817. /**
  818. * css_driver_unregister - unregister a css driver
  819. * @cdrv: css driver to unregister
  820. *
  821. * This is a wrapper around driver_unregister.
  822. */
  823. void css_driver_unregister(struct css_driver *cdrv)
  824. {
  825. driver_unregister(&cdrv->drv);
  826. }
  827. EXPORT_SYMBOL_GPL(css_driver_unregister);
  828. subsys_initcall(init_channel_subsystem);
  829. MODULE_LICENSE("GPL");
  830. EXPORT_SYMBOL(css_bus_type);