css.c 21 KB

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