css.c 26 KB

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