css.c 29 KB

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