css.c 29 KB

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