css.c 29 KB

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