css.c 26 KB

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