css.c 29 KB

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