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. EXPORT_SYMBOL_GPL(css_sched_sch_todo);
  387. static void css_sch_todo(struct work_struct *work)
  388. {
  389. struct subchannel *sch;
  390. enum sch_todo todo;
  391. int ret;
  392. sch = container_of(work, struct subchannel, todo_work);
  393. /* Find out todo. */
  394. spin_lock_irq(sch->lock);
  395. todo = sch->todo;
  396. CIO_MSG_EVENT(4, "sch_todo: sch=0.%x.%04x, todo=%d\n", sch->schid.ssid,
  397. sch->schid.sch_no, todo);
  398. sch->todo = SCH_TODO_NOTHING;
  399. spin_unlock_irq(sch->lock);
  400. /* Perform todo. */
  401. switch (todo) {
  402. case SCH_TODO_NOTHING:
  403. break;
  404. case SCH_TODO_EVAL:
  405. ret = css_evaluate_known_subchannel(sch, 1);
  406. if (ret == -EAGAIN) {
  407. spin_lock_irq(sch->lock);
  408. css_sched_sch_todo(sch, todo);
  409. spin_unlock_irq(sch->lock);
  410. }
  411. break;
  412. case SCH_TODO_UNREG:
  413. css_sch_device_unregister(sch);
  414. break;
  415. }
  416. /* Release workqueue ref. */
  417. put_device(&sch->dev);
  418. }
  419. static struct idset *slow_subchannel_set;
  420. static spinlock_t slow_subchannel_lock;
  421. static wait_queue_head_t css_eval_wq;
  422. static atomic_t css_eval_scheduled;
  423. static int __init slow_subchannel_init(void)
  424. {
  425. spin_lock_init(&slow_subchannel_lock);
  426. atomic_set(&css_eval_scheduled, 0);
  427. init_waitqueue_head(&css_eval_wq);
  428. slow_subchannel_set = idset_sch_new();
  429. if (!slow_subchannel_set) {
  430. CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
  431. return -ENOMEM;
  432. }
  433. return 0;
  434. }
  435. static int slow_eval_known_fn(struct subchannel *sch, void *data)
  436. {
  437. int eval;
  438. int rc;
  439. spin_lock_irq(&slow_subchannel_lock);
  440. eval = idset_sch_contains(slow_subchannel_set, sch->schid);
  441. idset_sch_del(slow_subchannel_set, sch->schid);
  442. spin_unlock_irq(&slow_subchannel_lock);
  443. if (eval) {
  444. rc = css_evaluate_known_subchannel(sch, 1);
  445. if (rc == -EAGAIN)
  446. css_schedule_eval(sch->schid);
  447. }
  448. return 0;
  449. }
  450. static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
  451. {
  452. int eval;
  453. int rc = 0;
  454. spin_lock_irq(&slow_subchannel_lock);
  455. eval = idset_sch_contains(slow_subchannel_set, schid);
  456. idset_sch_del(slow_subchannel_set, schid);
  457. spin_unlock_irq(&slow_subchannel_lock);
  458. if (eval) {
  459. rc = css_evaluate_new_subchannel(schid, 1);
  460. switch (rc) {
  461. case -EAGAIN:
  462. css_schedule_eval(schid);
  463. rc = 0;
  464. break;
  465. case -ENXIO:
  466. case -ENOMEM:
  467. case -EIO:
  468. /* These should abort looping */
  469. break;
  470. default:
  471. rc = 0;
  472. }
  473. }
  474. return rc;
  475. }
  476. static void css_slow_path_func(struct work_struct *unused)
  477. {
  478. unsigned long flags;
  479. CIO_TRACE_EVENT(4, "slowpath");
  480. for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
  481. NULL);
  482. spin_lock_irqsave(&slow_subchannel_lock, flags);
  483. if (idset_is_empty(slow_subchannel_set)) {
  484. atomic_set(&css_eval_scheduled, 0);
  485. wake_up(&css_eval_wq);
  486. }
  487. spin_unlock_irqrestore(&slow_subchannel_lock, flags);
  488. }
  489. static DECLARE_WORK(slow_path_work, css_slow_path_func);
  490. struct workqueue_struct *cio_work_q;
  491. void css_schedule_eval(struct subchannel_id schid)
  492. {
  493. unsigned long flags;
  494. spin_lock_irqsave(&slow_subchannel_lock, flags);
  495. idset_sch_add(slow_subchannel_set, schid);
  496. atomic_set(&css_eval_scheduled, 1);
  497. queue_work(cio_work_q, &slow_path_work);
  498. spin_unlock_irqrestore(&slow_subchannel_lock, flags);
  499. }
  500. void css_schedule_eval_all(void)
  501. {
  502. unsigned long flags;
  503. spin_lock_irqsave(&slow_subchannel_lock, flags);
  504. idset_fill(slow_subchannel_set);
  505. atomic_set(&css_eval_scheduled, 1);
  506. queue_work(cio_work_q, &slow_path_work);
  507. spin_unlock_irqrestore(&slow_subchannel_lock, flags);
  508. }
  509. static int __unset_registered(struct device *dev, void *data)
  510. {
  511. struct idset *set = data;
  512. struct subchannel *sch = to_subchannel(dev);
  513. idset_sch_del(set, sch->schid);
  514. return 0;
  515. }
  516. static void css_schedule_eval_all_unreg(void)
  517. {
  518. unsigned long flags;
  519. struct idset *unreg_set;
  520. /* Find unregistered subchannels. */
  521. unreg_set = idset_sch_new();
  522. if (!unreg_set) {
  523. /* Fallback. */
  524. css_schedule_eval_all();
  525. return;
  526. }
  527. idset_fill(unreg_set);
  528. bus_for_each_dev(&css_bus_type, NULL, unreg_set, __unset_registered);
  529. /* Apply to slow_subchannel_set. */
  530. spin_lock_irqsave(&slow_subchannel_lock, flags);
  531. idset_add_set(slow_subchannel_set, unreg_set);
  532. atomic_set(&css_eval_scheduled, 1);
  533. queue_work(cio_work_q, &slow_path_work);
  534. spin_unlock_irqrestore(&slow_subchannel_lock, flags);
  535. idset_free(unreg_set);
  536. }
  537. void css_wait_for_slow_path(void)
  538. {
  539. flush_workqueue(cio_work_q);
  540. }
  541. /* Schedule reprobing of all unregistered subchannels. */
  542. void css_schedule_reprobe(void)
  543. {
  544. css_schedule_eval_all_unreg();
  545. }
  546. EXPORT_SYMBOL_GPL(css_schedule_reprobe);
  547. /*
  548. * Called from the machine check handler for subchannel report words.
  549. */
  550. static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
  551. {
  552. struct subchannel_id mchk_schid;
  553. struct subchannel *sch;
  554. if (overflow) {
  555. css_schedule_eval_all();
  556. return;
  557. }
  558. CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
  559. "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
  560. crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
  561. crw0->erc, crw0->rsid);
  562. if (crw1)
  563. CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
  564. "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
  565. crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
  566. crw1->anc, crw1->erc, crw1->rsid);
  567. init_subchannel_id(&mchk_schid);
  568. mchk_schid.sch_no = crw0->rsid;
  569. if (crw1)
  570. mchk_schid.ssid = (crw1->rsid >> 4) & 3;
  571. if (crw0->erc == CRW_ERC_PMOD) {
  572. sch = get_subchannel_by_schid(mchk_schid);
  573. if (sch) {
  574. css_update_ssd_info(sch);
  575. put_device(&sch->dev);
  576. }
  577. }
  578. /*
  579. * Since we are always presented with IPI in the CRW, we have to
  580. * use stsch() to find out if the subchannel in question has come
  581. * or gone.
  582. */
  583. css_evaluate_subchannel(mchk_schid, 0);
  584. }
  585. static void __init
  586. css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
  587. {
  588. struct cpuid cpu_id;
  589. if (css_general_characteristics.mcss) {
  590. css->global_pgid.pgid_high.ext_cssid.version = 0x80;
  591. css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
  592. } else {
  593. #ifdef CONFIG_SMP
  594. css->global_pgid.pgid_high.cpu_addr = stap();
  595. #else
  596. css->global_pgid.pgid_high.cpu_addr = 0;
  597. #endif
  598. }
  599. get_cpu_id(&cpu_id);
  600. css->global_pgid.cpu_id = cpu_id.ident;
  601. css->global_pgid.cpu_model = cpu_id.machine;
  602. css->global_pgid.tod_high = tod_high;
  603. }
  604. static void
  605. channel_subsystem_release(struct device *dev)
  606. {
  607. struct channel_subsystem *css;
  608. css = to_css(dev);
  609. mutex_destroy(&css->mutex);
  610. if (css->pseudo_subchannel) {
  611. /* Implies that it has been generated but never registered. */
  612. css_subchannel_release(&css->pseudo_subchannel->dev);
  613. css->pseudo_subchannel = NULL;
  614. }
  615. kfree(css);
  616. }
  617. static ssize_t
  618. css_cm_enable_show(struct device *dev, struct device_attribute *attr,
  619. char *buf)
  620. {
  621. struct channel_subsystem *css = to_css(dev);
  622. int ret;
  623. if (!css)
  624. return 0;
  625. mutex_lock(&css->mutex);
  626. ret = sprintf(buf, "%x\n", css->cm_enabled);
  627. mutex_unlock(&css->mutex);
  628. return ret;
  629. }
  630. static ssize_t
  631. css_cm_enable_store(struct device *dev, struct device_attribute *attr,
  632. const char *buf, size_t count)
  633. {
  634. struct channel_subsystem *css = to_css(dev);
  635. int ret;
  636. unsigned long val;
  637. ret = strict_strtoul(buf, 16, &val);
  638. if (ret)
  639. return ret;
  640. mutex_lock(&css->mutex);
  641. switch (val) {
  642. case 0:
  643. ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
  644. break;
  645. case 1:
  646. ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
  647. break;
  648. default:
  649. ret = -EINVAL;
  650. }
  651. mutex_unlock(&css->mutex);
  652. return ret < 0 ? ret : count;
  653. }
  654. static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
  655. static int __init setup_css(int nr)
  656. {
  657. u32 tod_high;
  658. int ret;
  659. struct channel_subsystem *css;
  660. css = channel_subsystems[nr];
  661. memset(css, 0, sizeof(struct channel_subsystem));
  662. css->pseudo_subchannel =
  663. kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
  664. if (!css->pseudo_subchannel)
  665. return -ENOMEM;
  666. css->pseudo_subchannel->dev.parent = &css->device;
  667. css->pseudo_subchannel->dev.release = css_subchannel_release;
  668. dev_set_name(&css->pseudo_subchannel->dev, "defunct");
  669. mutex_init(&css->pseudo_subchannel->reg_mutex);
  670. ret = cio_create_sch_lock(css->pseudo_subchannel);
  671. if (ret) {
  672. kfree(css->pseudo_subchannel);
  673. return ret;
  674. }
  675. mutex_init(&css->mutex);
  676. css->valid = 1;
  677. css->cssid = nr;
  678. dev_set_name(&css->device, "css%x", nr);
  679. css->device.release = channel_subsystem_release;
  680. tod_high = (u32) (get_clock() >> 32);
  681. css_generate_pgid(css, tod_high);
  682. return 0;
  683. }
  684. static int css_reboot_event(struct notifier_block *this,
  685. unsigned long event,
  686. void *ptr)
  687. {
  688. int ret, i;
  689. ret = NOTIFY_DONE;
  690. for (i = 0; i <= __MAX_CSSID; i++) {
  691. struct channel_subsystem *css;
  692. css = channel_subsystems[i];
  693. mutex_lock(&css->mutex);
  694. if (css->cm_enabled)
  695. if (chsc_secm(css, 0))
  696. ret = NOTIFY_BAD;
  697. mutex_unlock(&css->mutex);
  698. }
  699. return ret;
  700. }
  701. static struct notifier_block css_reboot_notifier = {
  702. .notifier_call = css_reboot_event,
  703. };
  704. /*
  705. * Since the css devices are neither on a bus nor have a class
  706. * nor have a special device type, we cannot stop/restart channel
  707. * path measurements via the normal suspend/resume callbacks, but have
  708. * to use notifiers.
  709. */
  710. static int css_power_event(struct notifier_block *this, unsigned long event,
  711. void *ptr)
  712. {
  713. int ret, i;
  714. switch (event) {
  715. case PM_HIBERNATION_PREPARE:
  716. case PM_SUSPEND_PREPARE:
  717. ret = NOTIFY_DONE;
  718. for (i = 0; i <= __MAX_CSSID; i++) {
  719. struct channel_subsystem *css;
  720. css = channel_subsystems[i];
  721. mutex_lock(&css->mutex);
  722. if (!css->cm_enabled) {
  723. mutex_unlock(&css->mutex);
  724. continue;
  725. }
  726. ret = __chsc_do_secm(css, 0);
  727. ret = notifier_from_errno(ret);
  728. mutex_unlock(&css->mutex);
  729. }
  730. break;
  731. case PM_POST_HIBERNATION:
  732. case PM_POST_SUSPEND:
  733. ret = NOTIFY_DONE;
  734. for (i = 0; i <= __MAX_CSSID; i++) {
  735. struct channel_subsystem *css;
  736. css = channel_subsystems[i];
  737. mutex_lock(&css->mutex);
  738. if (!css->cm_enabled) {
  739. mutex_unlock(&css->mutex);
  740. continue;
  741. }
  742. ret = __chsc_do_secm(css, 1);
  743. ret = notifier_from_errno(ret);
  744. mutex_unlock(&css->mutex);
  745. }
  746. /* search for subchannels, which appeared during hibernation */
  747. css_schedule_reprobe();
  748. break;
  749. default:
  750. ret = NOTIFY_DONE;
  751. }
  752. return ret;
  753. }
  754. static struct notifier_block css_power_notifier = {
  755. .notifier_call = css_power_event,
  756. };
  757. /*
  758. * Now that the driver core is running, we can setup our channel subsystem.
  759. * The struct subchannel's are created during probing (except for the
  760. * static console subchannel).
  761. */
  762. static int __init css_bus_init(void)
  763. {
  764. int ret, i;
  765. ret = chsc_init();
  766. if (ret)
  767. return ret;
  768. chsc_determine_css_characteristics();
  769. /* Try to enable MSS. */
  770. ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
  771. if (ret)
  772. max_ssid = 0;
  773. else /* Success. */
  774. max_ssid = __MAX_SSID;
  775. ret = slow_subchannel_init();
  776. if (ret)
  777. goto out;
  778. ret = crw_register_handler(CRW_RSC_SCH, css_process_crw);
  779. if (ret)
  780. goto out;
  781. if ((ret = bus_register(&css_bus_type)))
  782. goto out;
  783. /* Setup css structure. */
  784. for (i = 0; i <= __MAX_CSSID; i++) {
  785. struct channel_subsystem *css;
  786. css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
  787. if (!css) {
  788. ret = -ENOMEM;
  789. goto out_unregister;
  790. }
  791. channel_subsystems[i] = css;
  792. ret = setup_css(i);
  793. if (ret) {
  794. kfree(channel_subsystems[i]);
  795. goto out_unregister;
  796. }
  797. ret = device_register(&css->device);
  798. if (ret) {
  799. put_device(&css->device);
  800. goto out_unregister;
  801. }
  802. if (css_chsc_characteristics.secm) {
  803. ret = device_create_file(&css->device,
  804. &dev_attr_cm_enable);
  805. if (ret)
  806. goto out_device;
  807. }
  808. ret = device_register(&css->pseudo_subchannel->dev);
  809. if (ret) {
  810. put_device(&css->pseudo_subchannel->dev);
  811. goto out_file;
  812. }
  813. }
  814. ret = register_reboot_notifier(&css_reboot_notifier);
  815. if (ret)
  816. goto out_unregister;
  817. ret = register_pm_notifier(&css_power_notifier);
  818. if (ret) {
  819. unregister_reboot_notifier(&css_reboot_notifier);
  820. goto out_unregister;
  821. }
  822. css_init_done = 1;
  823. /* Enable default isc for I/O subchannels. */
  824. isc_register(IO_SCH_ISC);
  825. return 0;
  826. out_file:
  827. if (css_chsc_characteristics.secm)
  828. device_remove_file(&channel_subsystems[i]->device,
  829. &dev_attr_cm_enable);
  830. out_device:
  831. device_unregister(&channel_subsystems[i]->device);
  832. out_unregister:
  833. while (i > 0) {
  834. struct channel_subsystem *css;
  835. i--;
  836. css = channel_subsystems[i];
  837. device_unregister(&css->pseudo_subchannel->dev);
  838. css->pseudo_subchannel = NULL;
  839. if (css_chsc_characteristics.secm)
  840. device_remove_file(&css->device,
  841. &dev_attr_cm_enable);
  842. device_unregister(&css->device);
  843. }
  844. bus_unregister(&css_bus_type);
  845. out:
  846. crw_unregister_handler(CRW_RSC_SCH);
  847. idset_free(slow_subchannel_set);
  848. chsc_init_cleanup();
  849. pr_alert("The CSS device driver initialization failed with "
  850. "errno=%d\n", ret);
  851. return ret;
  852. }
  853. static void __init css_bus_cleanup(void)
  854. {
  855. struct channel_subsystem *css;
  856. int i;
  857. for (i = 0; i <= __MAX_CSSID; i++) {
  858. css = channel_subsystems[i];
  859. device_unregister(&css->pseudo_subchannel->dev);
  860. css->pseudo_subchannel = NULL;
  861. if (css_chsc_characteristics.secm)
  862. device_remove_file(&css->device, &dev_attr_cm_enable);
  863. device_unregister(&css->device);
  864. }
  865. bus_unregister(&css_bus_type);
  866. crw_unregister_handler(CRW_RSC_SCH);
  867. idset_free(slow_subchannel_set);
  868. chsc_init_cleanup();
  869. isc_unregister(IO_SCH_ISC);
  870. }
  871. static int __init channel_subsystem_init(void)
  872. {
  873. int ret;
  874. ret = css_bus_init();
  875. if (ret)
  876. return ret;
  877. cio_work_q = create_singlethread_workqueue("cio");
  878. if (!cio_work_q) {
  879. ret = -ENOMEM;
  880. goto out_bus;
  881. }
  882. ret = io_subchannel_init();
  883. if (ret)
  884. goto out_wq;
  885. return ret;
  886. out_wq:
  887. destroy_workqueue(cio_work_q);
  888. out_bus:
  889. css_bus_cleanup();
  890. return ret;
  891. }
  892. subsys_initcall(channel_subsystem_init);
  893. static int css_settle(struct device_driver *drv, void *unused)
  894. {
  895. struct css_driver *cssdrv = to_cssdriver(drv);
  896. if (cssdrv->settle)
  897. return cssdrv->settle();
  898. return 0;
  899. }
  900. int css_complete_work(void)
  901. {
  902. int ret;
  903. /* Wait for the evaluation of subchannels to finish. */
  904. ret = wait_event_interruptible(css_eval_wq,
  905. atomic_read(&css_eval_scheduled) == 0);
  906. if (ret)
  907. return -EINTR;
  908. flush_workqueue(cio_work_q);
  909. /* Wait for the subchannel type specific initialization to finish */
  910. return bus_for_each_drv(&css_bus_type, NULL, NULL, css_settle);
  911. }
  912. /*
  913. * Wait for the initialization of devices to finish, to make sure we are
  914. * done with our setup if the search for the root device starts.
  915. */
  916. static int __init channel_subsystem_init_sync(void)
  917. {
  918. /* Start initial subchannel evaluation. */
  919. css_schedule_eval_all();
  920. css_complete_work();
  921. return 0;
  922. }
  923. subsys_initcall_sync(channel_subsystem_init_sync);
  924. void channel_subsystem_reinit(void)
  925. {
  926. struct channel_path *chp;
  927. struct chp_id chpid;
  928. chsc_enable_facility(CHSC_SDA_OC_MSS);
  929. chp_id_for_each(&chpid) {
  930. chp = chpid_to_chp(chpid);
  931. if (!chp)
  932. continue;
  933. chsc_determine_base_channel_path_desc(chpid, &chp->desc);
  934. }
  935. }
  936. #ifdef CONFIG_PROC_FS
  937. static ssize_t cio_settle_write(struct file *file, const char __user *buf,
  938. size_t count, loff_t *ppos)
  939. {
  940. int ret;
  941. /* Handle pending CRW's. */
  942. crw_wait_for_channel_report();
  943. ret = css_complete_work();
  944. return ret ? ret : count;
  945. }
  946. static const struct file_operations cio_settle_proc_fops = {
  947. .open = nonseekable_open,
  948. .write = cio_settle_write,
  949. .llseek = no_llseek,
  950. };
  951. static int __init cio_settle_init(void)
  952. {
  953. struct proc_dir_entry *entry;
  954. entry = proc_create("cio_settle", S_IWUSR, NULL,
  955. &cio_settle_proc_fops);
  956. if (!entry)
  957. return -ENOMEM;
  958. return 0;
  959. }
  960. device_initcall(cio_settle_init);
  961. #endif /*CONFIG_PROC_FS*/
  962. int sch_is_pseudo_sch(struct subchannel *sch)
  963. {
  964. return sch == to_css(sch->dev.parent)->pseudo_subchannel;
  965. }
  966. static int css_bus_match(struct device *dev, struct device_driver *drv)
  967. {
  968. struct subchannel *sch = to_subchannel(dev);
  969. struct css_driver *driver = to_cssdriver(drv);
  970. struct css_device_id *id;
  971. for (id = driver->subchannel_type; id->match_flags; id++) {
  972. if (sch->st == id->type)
  973. return 1;
  974. }
  975. return 0;
  976. }
  977. static int css_probe(struct device *dev)
  978. {
  979. struct subchannel *sch;
  980. int ret;
  981. sch = to_subchannel(dev);
  982. sch->driver = to_cssdriver(dev->driver);
  983. ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
  984. if (ret)
  985. sch->driver = NULL;
  986. return ret;
  987. }
  988. static int css_remove(struct device *dev)
  989. {
  990. struct subchannel *sch;
  991. int ret;
  992. sch = to_subchannel(dev);
  993. ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
  994. sch->driver = NULL;
  995. return ret;
  996. }
  997. static void css_shutdown(struct device *dev)
  998. {
  999. struct subchannel *sch;
  1000. sch = to_subchannel(dev);
  1001. if (sch->driver && sch->driver->shutdown)
  1002. sch->driver->shutdown(sch);
  1003. }
  1004. static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
  1005. {
  1006. struct subchannel *sch = to_subchannel(dev);
  1007. int ret;
  1008. ret = add_uevent_var(env, "ST=%01X", sch->st);
  1009. if (ret)
  1010. return ret;
  1011. ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
  1012. return ret;
  1013. }
  1014. static int css_pm_prepare(struct device *dev)
  1015. {
  1016. struct subchannel *sch = to_subchannel(dev);
  1017. struct css_driver *drv;
  1018. if (mutex_is_locked(&sch->reg_mutex))
  1019. return -EAGAIN;
  1020. if (!sch->dev.driver)
  1021. return 0;
  1022. drv = to_cssdriver(sch->dev.driver);
  1023. /* Notify drivers that they may not register children. */
  1024. return drv->prepare ? drv->prepare(sch) : 0;
  1025. }
  1026. static void css_pm_complete(struct device *dev)
  1027. {
  1028. struct subchannel *sch = to_subchannel(dev);
  1029. struct css_driver *drv;
  1030. if (!sch->dev.driver)
  1031. return;
  1032. drv = to_cssdriver(sch->dev.driver);
  1033. if (drv->complete)
  1034. drv->complete(sch);
  1035. }
  1036. static int css_pm_freeze(struct device *dev)
  1037. {
  1038. struct subchannel *sch = to_subchannel(dev);
  1039. struct css_driver *drv;
  1040. if (!sch->dev.driver)
  1041. return 0;
  1042. drv = to_cssdriver(sch->dev.driver);
  1043. return drv->freeze ? drv->freeze(sch) : 0;
  1044. }
  1045. static int css_pm_thaw(struct device *dev)
  1046. {
  1047. struct subchannel *sch = to_subchannel(dev);
  1048. struct css_driver *drv;
  1049. if (!sch->dev.driver)
  1050. return 0;
  1051. drv = to_cssdriver(sch->dev.driver);
  1052. return drv->thaw ? drv->thaw(sch) : 0;
  1053. }
  1054. static int css_pm_restore(struct device *dev)
  1055. {
  1056. struct subchannel *sch = to_subchannel(dev);
  1057. struct css_driver *drv;
  1058. css_update_ssd_info(sch);
  1059. if (!sch->dev.driver)
  1060. return 0;
  1061. drv = to_cssdriver(sch->dev.driver);
  1062. return drv->restore ? drv->restore(sch) : 0;
  1063. }
  1064. static const struct dev_pm_ops css_pm_ops = {
  1065. .prepare = css_pm_prepare,
  1066. .complete = css_pm_complete,
  1067. .freeze = css_pm_freeze,
  1068. .thaw = css_pm_thaw,
  1069. .restore = css_pm_restore,
  1070. };
  1071. static struct bus_type css_bus_type = {
  1072. .name = "css",
  1073. .match = css_bus_match,
  1074. .probe = css_probe,
  1075. .remove = css_remove,
  1076. .shutdown = css_shutdown,
  1077. .uevent = css_uevent,
  1078. .pm = &css_pm_ops,
  1079. };
  1080. /**
  1081. * css_driver_register - register a css driver
  1082. * @cdrv: css driver to register
  1083. *
  1084. * This is mainly a wrapper around driver_register that sets name
  1085. * and bus_type in the embedded struct device_driver correctly.
  1086. */
  1087. int css_driver_register(struct css_driver *cdrv)
  1088. {
  1089. cdrv->drv.bus = &css_bus_type;
  1090. return driver_register(&cdrv->drv);
  1091. }
  1092. EXPORT_SYMBOL_GPL(css_driver_register);
  1093. /**
  1094. * css_driver_unregister - unregister a css driver
  1095. * @cdrv: css driver to unregister
  1096. *
  1097. * This is a wrapper around driver_unregister.
  1098. */
  1099. void css_driver_unregister(struct css_driver *cdrv)
  1100. {
  1101. driver_unregister(&cdrv->drv);
  1102. }
  1103. EXPORT_SYMBOL_GPL(css_driver_unregister);
  1104. MODULE_LICENSE("GPL");