css.c 26 KB

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