css.c 28 KB

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