css.c 29 KB

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