css.c 22 KB

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