css.c 29 KB

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