css.c 26 KB

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