css.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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. /**
  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. return 1;
  289. }
  290. EXPORT_SYMBOL_GPL(css_sch_is_valid);
  291. static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
  292. {
  293. struct schib schib;
  294. if (!slow) {
  295. /* Will be done on the slow path. */
  296. return -EAGAIN;
  297. }
  298. if (stsch_err(schid, &schib) || !css_sch_is_valid(&schib)) {
  299. /* Unusable - ignore. */
  300. return 0;
  301. }
  302. CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
  303. "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
  304. return css_probe_device(schid);
  305. }
  306. static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
  307. {
  308. int ret = 0;
  309. if (sch->driver) {
  310. if (sch->driver->sch_event)
  311. ret = sch->driver->sch_event(sch, slow);
  312. else
  313. dev_dbg(&sch->dev,
  314. "Got subchannel machine check but "
  315. "no sch_event handler provided.\n");
  316. }
  317. return ret;
  318. }
  319. static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
  320. {
  321. struct subchannel *sch;
  322. int ret;
  323. sch = get_subchannel_by_schid(schid);
  324. if (sch) {
  325. ret = css_evaluate_known_subchannel(sch, slow);
  326. put_device(&sch->dev);
  327. } else
  328. ret = css_evaluate_new_subchannel(schid, slow);
  329. if (ret == -EAGAIN)
  330. css_schedule_eval(schid);
  331. }
  332. static struct idset *slow_subchannel_set;
  333. static spinlock_t slow_subchannel_lock;
  334. static int __init slow_subchannel_init(void)
  335. {
  336. spin_lock_init(&slow_subchannel_lock);
  337. slow_subchannel_set = idset_sch_new();
  338. if (!slow_subchannel_set) {
  339. CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
  340. return -ENOMEM;
  341. }
  342. return 0;
  343. }
  344. static int slow_eval_known_fn(struct subchannel *sch, void *data)
  345. {
  346. int eval;
  347. int rc;
  348. spin_lock_irq(&slow_subchannel_lock);
  349. eval = idset_sch_contains(slow_subchannel_set, sch->schid);
  350. idset_sch_del(slow_subchannel_set, sch->schid);
  351. spin_unlock_irq(&slow_subchannel_lock);
  352. if (eval) {
  353. rc = css_evaluate_known_subchannel(sch, 1);
  354. if (rc == -EAGAIN)
  355. css_schedule_eval(sch->schid);
  356. }
  357. return 0;
  358. }
  359. static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
  360. {
  361. int eval;
  362. int rc = 0;
  363. spin_lock_irq(&slow_subchannel_lock);
  364. eval = idset_sch_contains(slow_subchannel_set, schid);
  365. idset_sch_del(slow_subchannel_set, schid);
  366. spin_unlock_irq(&slow_subchannel_lock);
  367. if (eval) {
  368. rc = css_evaluate_new_subchannel(schid, 1);
  369. switch (rc) {
  370. case -EAGAIN:
  371. css_schedule_eval(schid);
  372. rc = 0;
  373. break;
  374. case -ENXIO:
  375. case -ENOMEM:
  376. case -EIO:
  377. /* These should abort looping */
  378. break;
  379. default:
  380. rc = 0;
  381. }
  382. }
  383. return rc;
  384. }
  385. static void css_slow_path_func(struct work_struct *unused)
  386. {
  387. CIO_TRACE_EVENT(4, "slowpath");
  388. for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
  389. NULL);
  390. }
  391. static DECLARE_WORK(slow_path_work, css_slow_path_func);
  392. struct workqueue_struct *slow_path_wq;
  393. void css_schedule_eval(struct subchannel_id schid)
  394. {
  395. unsigned long flags;
  396. spin_lock_irqsave(&slow_subchannel_lock, flags);
  397. idset_sch_add(slow_subchannel_set, schid);
  398. queue_work(slow_path_wq, &slow_path_work);
  399. spin_unlock_irqrestore(&slow_subchannel_lock, flags);
  400. }
  401. void css_schedule_eval_all(void)
  402. {
  403. unsigned long flags;
  404. spin_lock_irqsave(&slow_subchannel_lock, flags);
  405. idset_fill(slow_subchannel_set);
  406. queue_work(slow_path_wq, &slow_path_work);
  407. spin_unlock_irqrestore(&slow_subchannel_lock, flags);
  408. }
  409. void css_wait_for_slow_path(void)
  410. {
  411. flush_workqueue(ccw_device_notify_work);
  412. flush_workqueue(slow_path_wq);
  413. }
  414. /* Reprobe subchannel if unregistered. */
  415. static int reprobe_subchannel(struct subchannel_id schid, void *data)
  416. {
  417. int ret;
  418. CIO_MSG_EVENT(6, "cio: reprobe 0.%x.%04x\n",
  419. schid.ssid, schid.sch_no);
  420. if (need_reprobe)
  421. return -EAGAIN;
  422. ret = css_probe_device(schid);
  423. switch (ret) {
  424. case 0:
  425. break;
  426. case -ENXIO:
  427. case -ENOMEM:
  428. case -EIO:
  429. /* These should abort looping */
  430. break;
  431. default:
  432. ret = 0;
  433. }
  434. return ret;
  435. }
  436. /* Work function used to reprobe all unregistered subchannels. */
  437. static void reprobe_all(struct work_struct *unused)
  438. {
  439. int ret;
  440. CIO_MSG_EVENT(4, "reprobe start\n");
  441. need_reprobe = 0;
  442. /* Make sure initial subchannel scan is done. */
  443. wait_event(ccw_device_init_wq,
  444. atomic_read(&ccw_device_init_count) == 0);
  445. ret = for_each_subchannel_staged(NULL, reprobe_subchannel, NULL);
  446. CIO_MSG_EVENT(4, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
  447. need_reprobe);
  448. }
  449. static DECLARE_WORK(css_reprobe_work, reprobe_all);
  450. /* Schedule reprobing of all unregistered subchannels. */
  451. void css_schedule_reprobe(void)
  452. {
  453. need_reprobe = 1;
  454. queue_work(slow_path_wq, &css_reprobe_work);
  455. }
  456. EXPORT_SYMBOL_GPL(css_schedule_reprobe);
  457. /*
  458. * Called from the machine check handler for subchannel report words.
  459. */
  460. static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
  461. {
  462. struct subchannel_id mchk_schid;
  463. if (overflow) {
  464. css_schedule_eval_all();
  465. return;
  466. }
  467. CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
  468. "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
  469. crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
  470. crw0->erc, crw0->rsid);
  471. if (crw1)
  472. CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
  473. "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
  474. crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
  475. crw1->anc, crw1->erc, crw1->rsid);
  476. init_subchannel_id(&mchk_schid);
  477. mchk_schid.sch_no = crw0->rsid;
  478. if (crw1)
  479. mchk_schid.ssid = (crw1->rsid >> 8) & 3;
  480. /*
  481. * Since we are always presented with IPI in the CRW, we have to
  482. * use stsch() to find out if the subchannel in question has come
  483. * or gone.
  484. */
  485. css_evaluate_subchannel(mchk_schid, 0);
  486. }
  487. static int __init
  488. __init_channel_subsystem(struct subchannel_id schid, void *data)
  489. {
  490. struct subchannel *sch;
  491. int ret;
  492. if (cio_is_console(schid))
  493. sch = cio_get_console_subchannel();
  494. else {
  495. sch = css_alloc_subchannel(schid);
  496. if (IS_ERR(sch))
  497. ret = PTR_ERR(sch);
  498. else
  499. ret = 0;
  500. switch (ret) {
  501. case 0:
  502. break;
  503. case -ENOMEM:
  504. panic("Out of memory in init_channel_subsystem\n");
  505. /* -ENXIO: no more subchannels. */
  506. case -ENXIO:
  507. return ret;
  508. /* -EIO: this subchannel set not supported. */
  509. case -EIO:
  510. return ret;
  511. default:
  512. return 0;
  513. }
  514. }
  515. /*
  516. * We register ALL valid subchannels in ioinfo, even those
  517. * that have been present before init_channel_subsystem.
  518. * These subchannels can't have been registered yet (kmalloc
  519. * not working) so we do it now. This is true e.g. for the
  520. * console subchannel.
  521. */
  522. css_register_subchannel(sch);
  523. return 0;
  524. }
  525. static void __init
  526. css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
  527. {
  528. if (css_characteristics_avail && css_general_characteristics.mcss) {
  529. css->global_pgid.pgid_high.ext_cssid.version = 0x80;
  530. css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
  531. } else {
  532. #ifdef CONFIG_SMP
  533. css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
  534. #else
  535. css->global_pgid.pgid_high.cpu_addr = 0;
  536. #endif
  537. }
  538. css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
  539. css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
  540. css->global_pgid.tod_high = tod_high;
  541. }
  542. static void
  543. channel_subsystem_release(struct device *dev)
  544. {
  545. struct channel_subsystem *css;
  546. css = to_css(dev);
  547. mutex_destroy(&css->mutex);
  548. kfree(css);
  549. }
  550. static ssize_t
  551. css_cm_enable_show(struct device *dev, struct device_attribute *attr,
  552. char *buf)
  553. {
  554. struct channel_subsystem *css = to_css(dev);
  555. int ret;
  556. if (!css)
  557. return 0;
  558. mutex_lock(&css->mutex);
  559. ret = sprintf(buf, "%x\n", css->cm_enabled);
  560. mutex_unlock(&css->mutex);
  561. return ret;
  562. }
  563. static ssize_t
  564. css_cm_enable_store(struct device *dev, struct device_attribute *attr,
  565. const char *buf, size_t count)
  566. {
  567. struct channel_subsystem *css = to_css(dev);
  568. int ret;
  569. unsigned long val;
  570. ret = strict_strtoul(buf, 16, &val);
  571. if (ret)
  572. return ret;
  573. mutex_lock(&css->mutex);
  574. switch (val) {
  575. case 0:
  576. ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
  577. break;
  578. case 1:
  579. ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
  580. break;
  581. default:
  582. ret = -EINVAL;
  583. }
  584. mutex_unlock(&css->mutex);
  585. return ret < 0 ? ret : count;
  586. }
  587. static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
  588. static int __init setup_css(int nr)
  589. {
  590. u32 tod_high;
  591. int ret;
  592. struct channel_subsystem *css;
  593. css = channel_subsystems[nr];
  594. memset(css, 0, sizeof(struct channel_subsystem));
  595. css->pseudo_subchannel =
  596. kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
  597. if (!css->pseudo_subchannel)
  598. return -ENOMEM;
  599. css->pseudo_subchannel->dev.parent = &css->device;
  600. css->pseudo_subchannel->dev.release = css_subchannel_release;
  601. sprintf(css->pseudo_subchannel->dev.bus_id, "defunct");
  602. ret = cio_create_sch_lock(css->pseudo_subchannel);
  603. if (ret) {
  604. kfree(css->pseudo_subchannel);
  605. return ret;
  606. }
  607. mutex_init(&css->mutex);
  608. css->valid = 1;
  609. css->cssid = nr;
  610. sprintf(css->device.bus_id, "css%x", nr);
  611. css->device.release = channel_subsystem_release;
  612. tod_high = (u32) (get_clock() >> 32);
  613. css_generate_pgid(css, tod_high);
  614. return 0;
  615. }
  616. static int css_reboot_event(struct notifier_block *this,
  617. unsigned long event,
  618. void *ptr)
  619. {
  620. int ret, i;
  621. ret = NOTIFY_DONE;
  622. for (i = 0; i <= __MAX_CSSID; i++) {
  623. struct channel_subsystem *css;
  624. css = channel_subsystems[i];
  625. mutex_lock(&css->mutex);
  626. if (css->cm_enabled)
  627. if (chsc_secm(css, 0))
  628. ret = NOTIFY_BAD;
  629. mutex_unlock(&css->mutex);
  630. }
  631. return ret;
  632. }
  633. static struct notifier_block css_reboot_notifier = {
  634. .notifier_call = css_reboot_event,
  635. };
  636. /*
  637. * Now that the driver core is running, we can setup our channel subsystem.
  638. * The struct subchannel's are created during probing (except for the
  639. * static console subchannel).
  640. */
  641. static int __init
  642. init_channel_subsystem (void)
  643. {
  644. int ret, i;
  645. ret = chsc_determine_css_characteristics();
  646. if (ret == -ENOMEM)
  647. goto out; /* No need to continue. */
  648. if (ret == 0)
  649. css_characteristics_avail = 1;
  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_characteristics_avail &&
  688. 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. ctl_set_bit(6, 28);
  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_characteristics_avail && 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);
  830. EXPORT_SYMBOL_GPL(css_characteristics_avail);