css.c 30 KB

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