css.c 29 KB

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