css.c 26 KB

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