css.c 29 KB

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