css.c 21 KB

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