css.c 27 KB

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