cio.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. /*
  2. * S/390 common I/O routines -- low level i/o calls
  3. *
  4. * Copyright IBM Corp. 1999, 2008
  5. * Author(s): Ingo Adlung (adlung@de.ibm.com)
  6. * Cornelia Huck (cornelia.huck@de.ibm.com)
  7. * Arnd Bergmann (arndb@de.ibm.com)
  8. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  9. */
  10. #define KMSG_COMPONENT "cio"
  11. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  12. #include <linux/ftrace.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/device.h>
  17. #include <linux/kernel_stat.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/irq.h>
  20. #include <asm/cio.h>
  21. #include <asm/delay.h>
  22. #include <asm/irq.h>
  23. #include <asm/irq_regs.h>
  24. #include <asm/setup.h>
  25. #include <asm/reset.h>
  26. #include <asm/ipl.h>
  27. #include <asm/chpid.h>
  28. #include <asm/airq.h>
  29. #include <asm/isc.h>
  30. #include <linux/sched/cputime.h>
  31. #include <asm/fcx.h>
  32. #include <asm/nmi.h>
  33. #include <asm/crw.h>
  34. #include "cio.h"
  35. #include "css.h"
  36. #include "chsc.h"
  37. #include "ioasm.h"
  38. #include "io_sch.h"
  39. #include "blacklist.h"
  40. #include "cio_debug.h"
  41. #include "chp.h"
  42. #include "trace.h"
  43. debug_info_t *cio_debug_msg_id;
  44. debug_info_t *cio_debug_trace_id;
  45. debug_info_t *cio_debug_crw_id;
  46. DEFINE_PER_CPU_ALIGNED(struct irb, cio_irb);
  47. EXPORT_PER_CPU_SYMBOL(cio_irb);
  48. /*
  49. * Function: cio_debug_init
  50. * Initializes three debug logs for common I/O:
  51. * - cio_msg logs generic cio messages
  52. * - cio_trace logs the calling of different functions
  53. * - cio_crw logs machine check related cio messages
  54. */
  55. static int __init cio_debug_init(void)
  56. {
  57. cio_debug_msg_id = debug_register("cio_msg", 16, 1, 11 * sizeof(long));
  58. if (!cio_debug_msg_id)
  59. goto out_unregister;
  60. debug_register_view(cio_debug_msg_id, &debug_sprintf_view);
  61. debug_set_level(cio_debug_msg_id, 2);
  62. cio_debug_trace_id = debug_register("cio_trace", 16, 1, 16);
  63. if (!cio_debug_trace_id)
  64. goto out_unregister;
  65. debug_register_view(cio_debug_trace_id, &debug_hex_ascii_view);
  66. debug_set_level(cio_debug_trace_id, 2);
  67. cio_debug_crw_id = debug_register("cio_crw", 8, 1, 8 * sizeof(long));
  68. if (!cio_debug_crw_id)
  69. goto out_unregister;
  70. debug_register_view(cio_debug_crw_id, &debug_sprintf_view);
  71. debug_set_level(cio_debug_crw_id, 4);
  72. return 0;
  73. out_unregister:
  74. debug_unregister(cio_debug_msg_id);
  75. debug_unregister(cio_debug_trace_id);
  76. debug_unregister(cio_debug_crw_id);
  77. return -1;
  78. }
  79. arch_initcall (cio_debug_init);
  80. int cio_set_options(struct subchannel *sch, int flags)
  81. {
  82. struct io_subchannel_private *priv = to_io_private(sch);
  83. priv->options.suspend = (flags & DOIO_ALLOW_SUSPEND) != 0;
  84. priv->options.prefetch = (flags & DOIO_DENY_PREFETCH) != 0;
  85. priv->options.inter = (flags & DOIO_SUPPRESS_INTER) != 0;
  86. return 0;
  87. }
  88. static int
  89. cio_start_handle_notoper(struct subchannel *sch, __u8 lpm)
  90. {
  91. char dbf_text[15];
  92. if (lpm != 0)
  93. sch->lpm &= ~lpm;
  94. else
  95. sch->lpm = 0;
  96. CIO_MSG_EVENT(2, "cio_start: 'not oper' status for "
  97. "subchannel 0.%x.%04x!\n", sch->schid.ssid,
  98. sch->schid.sch_no);
  99. if (cio_update_schib(sch))
  100. return -ENODEV;
  101. sprintf(dbf_text, "no%s", dev_name(&sch->dev));
  102. CIO_TRACE_EVENT(0, dbf_text);
  103. CIO_HEX_EVENT(0, &sch->schib, sizeof (struct schib));
  104. return (sch->lpm ? -EACCES : -ENODEV);
  105. }
  106. int
  107. cio_start_key (struct subchannel *sch, /* subchannel structure */
  108. struct ccw1 * cpa, /* logical channel prog addr */
  109. __u8 lpm, /* logical path mask */
  110. __u8 key) /* storage key */
  111. {
  112. struct io_subchannel_private *priv = to_io_private(sch);
  113. union orb *orb = &priv->orb;
  114. int ccode;
  115. CIO_TRACE_EVENT(5, "stIO");
  116. CIO_TRACE_EVENT(5, dev_name(&sch->dev));
  117. memset(orb, 0, sizeof(union orb));
  118. /* sch is always under 2G. */
  119. orb->cmd.intparm = (u32)(addr_t)sch;
  120. orb->cmd.fmt = 1;
  121. orb->cmd.pfch = priv->options.prefetch == 0;
  122. orb->cmd.spnd = priv->options.suspend;
  123. orb->cmd.ssic = priv->options.suspend && priv->options.inter;
  124. orb->cmd.lpm = (lpm != 0) ? lpm : sch->lpm;
  125. /*
  126. * for 64 bit we always support 64 bit IDAWs with 4k page size only
  127. */
  128. orb->cmd.c64 = 1;
  129. orb->cmd.i2k = 0;
  130. orb->cmd.key = key >> 4;
  131. /* issue "Start Subchannel" */
  132. orb->cmd.cpa = (__u32) __pa(cpa);
  133. ccode = ssch(sch->schid, orb);
  134. /* process condition code */
  135. CIO_HEX_EVENT(5, &ccode, sizeof(ccode));
  136. switch (ccode) {
  137. case 0:
  138. /*
  139. * initialize device status information
  140. */
  141. sch->schib.scsw.cmd.actl |= SCSW_ACTL_START_PEND;
  142. return 0;
  143. case 1: /* status pending */
  144. case 2: /* busy */
  145. return -EBUSY;
  146. case 3: /* device/path not operational */
  147. return cio_start_handle_notoper(sch, lpm);
  148. default:
  149. return ccode;
  150. }
  151. }
  152. EXPORT_SYMBOL_GPL(cio_start_key);
  153. int
  154. cio_start (struct subchannel *sch, struct ccw1 *cpa, __u8 lpm)
  155. {
  156. return cio_start_key(sch, cpa, lpm, PAGE_DEFAULT_KEY);
  157. }
  158. EXPORT_SYMBOL_GPL(cio_start);
  159. /*
  160. * resume suspended I/O operation
  161. */
  162. int
  163. cio_resume (struct subchannel *sch)
  164. {
  165. int ccode;
  166. CIO_TRACE_EVENT(4, "resIO");
  167. CIO_TRACE_EVENT(4, dev_name(&sch->dev));
  168. ccode = rsch (sch->schid);
  169. CIO_HEX_EVENT(4, &ccode, sizeof(ccode));
  170. switch (ccode) {
  171. case 0:
  172. sch->schib.scsw.cmd.actl |= SCSW_ACTL_RESUME_PEND;
  173. return 0;
  174. case 1:
  175. return -EBUSY;
  176. case 2:
  177. return -EINVAL;
  178. default:
  179. /*
  180. * useless to wait for request completion
  181. * as device is no longer operational !
  182. */
  183. return -ENODEV;
  184. }
  185. }
  186. EXPORT_SYMBOL_GPL(cio_resume);
  187. /*
  188. * halt I/O operation
  189. */
  190. int
  191. cio_halt(struct subchannel *sch)
  192. {
  193. int ccode;
  194. if (!sch)
  195. return -ENODEV;
  196. CIO_TRACE_EVENT(2, "haltIO");
  197. CIO_TRACE_EVENT(2, dev_name(&sch->dev));
  198. /*
  199. * Issue "Halt subchannel" and process condition code
  200. */
  201. ccode = hsch (sch->schid);
  202. CIO_HEX_EVENT(2, &ccode, sizeof(ccode));
  203. switch (ccode) {
  204. case 0:
  205. sch->schib.scsw.cmd.actl |= SCSW_ACTL_HALT_PEND;
  206. return 0;
  207. case 1: /* status pending */
  208. case 2: /* busy */
  209. return -EBUSY;
  210. default: /* device not operational */
  211. return -ENODEV;
  212. }
  213. }
  214. EXPORT_SYMBOL_GPL(cio_halt);
  215. /*
  216. * Clear I/O operation
  217. */
  218. int
  219. cio_clear(struct subchannel *sch)
  220. {
  221. int ccode;
  222. if (!sch)
  223. return -ENODEV;
  224. CIO_TRACE_EVENT(2, "clearIO");
  225. CIO_TRACE_EVENT(2, dev_name(&sch->dev));
  226. /*
  227. * Issue "Clear subchannel" and process condition code
  228. */
  229. ccode = csch (sch->schid);
  230. CIO_HEX_EVENT(2, &ccode, sizeof(ccode));
  231. switch (ccode) {
  232. case 0:
  233. sch->schib.scsw.cmd.actl |= SCSW_ACTL_CLEAR_PEND;
  234. return 0;
  235. default: /* device not operational */
  236. return -ENODEV;
  237. }
  238. }
  239. EXPORT_SYMBOL_GPL(cio_clear);
  240. /*
  241. * Function: cio_cancel
  242. * Issues a "Cancel Subchannel" on the specified subchannel
  243. * Note: We don't need any fancy intparms and flags here
  244. * since xsch is executed synchronously.
  245. * Only for common I/O internal use as for now.
  246. */
  247. int
  248. cio_cancel (struct subchannel *sch)
  249. {
  250. int ccode;
  251. if (!sch)
  252. return -ENODEV;
  253. CIO_TRACE_EVENT(2, "cancelIO");
  254. CIO_TRACE_EVENT(2, dev_name(&sch->dev));
  255. ccode = xsch (sch->schid);
  256. CIO_HEX_EVENT(2, &ccode, sizeof(ccode));
  257. switch (ccode) {
  258. case 0: /* success */
  259. /* Update information in scsw. */
  260. if (cio_update_schib(sch))
  261. return -ENODEV;
  262. return 0;
  263. case 1: /* status pending */
  264. return -EBUSY;
  265. case 2: /* not applicable */
  266. return -EINVAL;
  267. default: /* not oper */
  268. return -ENODEV;
  269. }
  270. }
  271. EXPORT_SYMBOL_GPL(cio_cancel);
  272. /**
  273. * cio_cancel_halt_clear - Cancel running I/O by performing cancel, halt
  274. * and clear ordinally if subchannel is valid.
  275. * @sch: subchannel on which to perform the cancel_halt_clear operation
  276. * @iretry: the number of the times remained to retry the next operation
  277. *
  278. * This should be called repeatedly since halt/clear are asynchronous
  279. * operations. We do one try with cio_cancel, three tries with cio_halt,
  280. * 255 tries with cio_clear. The caller should initialize @iretry with
  281. * the value 255 for its first call to this, and keep using the same
  282. * @iretry in the subsequent calls until it gets a non -EBUSY return.
  283. *
  284. * Returns 0 if device now idle, -ENODEV for device not operational,
  285. * -EBUSY if an interrupt is expected (either from halt/clear or from a
  286. * status pending), and -EIO if out of retries.
  287. */
  288. int cio_cancel_halt_clear(struct subchannel *sch, int *iretry)
  289. {
  290. int ret;
  291. if (cio_update_schib(sch))
  292. return -ENODEV;
  293. if (!sch->schib.pmcw.ena)
  294. /* Not operational -> done. */
  295. return 0;
  296. /* Stage 1: cancel io. */
  297. if (!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_HALT_PEND) &&
  298. !(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_CLEAR_PEND)) {
  299. if (!scsw_is_tm(&sch->schib.scsw)) {
  300. ret = cio_cancel(sch);
  301. if (ret != -EINVAL)
  302. return ret;
  303. }
  304. /*
  305. * Cancel io unsuccessful or not applicable (transport mode).
  306. * Continue with asynchronous instructions.
  307. */
  308. *iretry = 3; /* 3 halt retries. */
  309. }
  310. /* Stage 2: halt io. */
  311. if (!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_CLEAR_PEND)) {
  312. if (*iretry) {
  313. *iretry -= 1;
  314. ret = cio_halt(sch);
  315. if (ret != -EBUSY)
  316. return (ret == 0) ? -EBUSY : ret;
  317. }
  318. /* Halt io unsuccessful. */
  319. *iretry = 255; /* 255 clear retries. */
  320. }
  321. /* Stage 3: clear io. */
  322. if (*iretry) {
  323. *iretry -= 1;
  324. ret = cio_clear(sch);
  325. return (ret == 0) ? -EBUSY : ret;
  326. }
  327. /* Function was unsuccessful */
  328. return -EIO;
  329. }
  330. EXPORT_SYMBOL_GPL(cio_cancel_halt_clear);
  331. static void cio_apply_config(struct subchannel *sch, struct schib *schib)
  332. {
  333. schib->pmcw.intparm = sch->config.intparm;
  334. schib->pmcw.mbi = sch->config.mbi;
  335. schib->pmcw.isc = sch->config.isc;
  336. schib->pmcw.ena = sch->config.ena;
  337. schib->pmcw.mme = sch->config.mme;
  338. schib->pmcw.mp = sch->config.mp;
  339. schib->pmcw.csense = sch->config.csense;
  340. schib->pmcw.mbfc = sch->config.mbfc;
  341. if (sch->config.mbfc)
  342. schib->mba = sch->config.mba;
  343. }
  344. static int cio_check_config(struct subchannel *sch, struct schib *schib)
  345. {
  346. return (schib->pmcw.intparm == sch->config.intparm) &&
  347. (schib->pmcw.mbi == sch->config.mbi) &&
  348. (schib->pmcw.isc == sch->config.isc) &&
  349. (schib->pmcw.ena == sch->config.ena) &&
  350. (schib->pmcw.mme == sch->config.mme) &&
  351. (schib->pmcw.mp == sch->config.mp) &&
  352. (schib->pmcw.csense == sch->config.csense) &&
  353. (schib->pmcw.mbfc == sch->config.mbfc) &&
  354. (!sch->config.mbfc || (schib->mba == sch->config.mba));
  355. }
  356. /*
  357. * cio_commit_config - apply configuration to the subchannel
  358. */
  359. int cio_commit_config(struct subchannel *sch)
  360. {
  361. int ccode, retry, ret = 0;
  362. struct schib schib;
  363. struct irb irb;
  364. if (stsch(sch->schid, &schib) || !css_sch_is_valid(&schib))
  365. return -ENODEV;
  366. for (retry = 0; retry < 5; retry++) {
  367. /* copy desired changes to local schib */
  368. cio_apply_config(sch, &schib);
  369. ccode = msch(sch->schid, &schib);
  370. if (ccode < 0) /* -EIO if msch gets a program check. */
  371. return ccode;
  372. switch (ccode) {
  373. case 0: /* successful */
  374. if (stsch(sch->schid, &schib) ||
  375. !css_sch_is_valid(&schib))
  376. return -ENODEV;
  377. if (cio_check_config(sch, &schib)) {
  378. /* commit changes from local schib */
  379. memcpy(&sch->schib, &schib, sizeof(schib));
  380. return 0;
  381. }
  382. ret = -EAGAIN;
  383. break;
  384. case 1: /* status pending */
  385. ret = -EBUSY;
  386. if (tsch(sch->schid, &irb))
  387. return ret;
  388. break;
  389. case 2: /* busy */
  390. udelay(100); /* allow for recovery */
  391. ret = -EBUSY;
  392. break;
  393. case 3: /* not operational */
  394. return -ENODEV;
  395. }
  396. }
  397. return ret;
  398. }
  399. EXPORT_SYMBOL_GPL(cio_commit_config);
  400. /**
  401. * cio_update_schib - Perform stsch and update schib if subchannel is valid.
  402. * @sch: subchannel on which to perform stsch
  403. * Return zero on success, -ENODEV otherwise.
  404. */
  405. int cio_update_schib(struct subchannel *sch)
  406. {
  407. struct schib schib;
  408. if (stsch(sch->schid, &schib) || !css_sch_is_valid(&schib))
  409. return -ENODEV;
  410. memcpy(&sch->schib, &schib, sizeof(schib));
  411. return 0;
  412. }
  413. EXPORT_SYMBOL_GPL(cio_update_schib);
  414. /**
  415. * cio_enable_subchannel - enable a subchannel.
  416. * @sch: subchannel to be enabled
  417. * @intparm: interruption parameter to set
  418. */
  419. int cio_enable_subchannel(struct subchannel *sch, u32 intparm)
  420. {
  421. int ret;
  422. CIO_TRACE_EVENT(2, "ensch");
  423. CIO_TRACE_EVENT(2, dev_name(&sch->dev));
  424. if (sch_is_pseudo_sch(sch))
  425. return -EINVAL;
  426. if (cio_update_schib(sch))
  427. return -ENODEV;
  428. sch->config.ena = 1;
  429. sch->config.isc = sch->isc;
  430. sch->config.intparm = intparm;
  431. ret = cio_commit_config(sch);
  432. if (ret == -EIO) {
  433. /*
  434. * Got a program check in msch. Try without
  435. * the concurrent sense bit the next time.
  436. */
  437. sch->config.csense = 0;
  438. ret = cio_commit_config(sch);
  439. }
  440. CIO_HEX_EVENT(2, &ret, sizeof(ret));
  441. return ret;
  442. }
  443. EXPORT_SYMBOL_GPL(cio_enable_subchannel);
  444. /**
  445. * cio_disable_subchannel - disable a subchannel.
  446. * @sch: subchannel to disable
  447. */
  448. int cio_disable_subchannel(struct subchannel *sch)
  449. {
  450. int ret;
  451. CIO_TRACE_EVENT(2, "dissch");
  452. CIO_TRACE_EVENT(2, dev_name(&sch->dev));
  453. if (sch_is_pseudo_sch(sch))
  454. return 0;
  455. if (cio_update_schib(sch))
  456. return -ENODEV;
  457. sch->config.ena = 0;
  458. ret = cio_commit_config(sch);
  459. CIO_HEX_EVENT(2, &ret, sizeof(ret));
  460. return ret;
  461. }
  462. EXPORT_SYMBOL_GPL(cio_disable_subchannel);
  463. static int cio_check_devno_blacklisted(struct subchannel *sch)
  464. {
  465. if (is_blacklisted(sch->schid.ssid, sch->schib.pmcw.dev)) {
  466. /*
  467. * This device must not be known to Linux. So we simply
  468. * say that there is no device and return ENODEV.
  469. */
  470. CIO_MSG_EVENT(6, "Blacklisted device detected "
  471. "at devno %04X, subchannel set %x\n",
  472. sch->schib.pmcw.dev, sch->schid.ssid);
  473. return -ENODEV;
  474. }
  475. return 0;
  476. }
  477. /**
  478. * cio_validate_subchannel - basic validation of subchannel
  479. * @sch: subchannel structure to be filled out
  480. * @schid: subchannel id
  481. *
  482. * Find out subchannel type and initialize struct subchannel.
  483. * Return codes:
  484. * 0 on success
  485. * -ENXIO for non-defined subchannels
  486. * -ENODEV for invalid subchannels or blacklisted devices
  487. * -EIO for subchannels in an invalid subchannel set
  488. */
  489. int cio_validate_subchannel(struct subchannel *sch, struct subchannel_id schid)
  490. {
  491. char dbf_txt[15];
  492. int ccode;
  493. int err;
  494. sprintf(dbf_txt, "valsch%x", schid.sch_no);
  495. CIO_TRACE_EVENT(4, dbf_txt);
  496. /*
  497. * The first subchannel that is not-operational (ccode==3)
  498. * indicates that there aren't any more devices available.
  499. * If stsch gets an exception, it means the current subchannel set
  500. * is not valid.
  501. */
  502. ccode = stsch(schid, &sch->schib);
  503. if (ccode) {
  504. err = (ccode == 3) ? -ENXIO : ccode;
  505. goto out;
  506. }
  507. sch->st = sch->schib.pmcw.st;
  508. sch->schid = schid;
  509. switch (sch->st) {
  510. case SUBCHANNEL_TYPE_IO:
  511. case SUBCHANNEL_TYPE_MSG:
  512. if (!css_sch_is_valid(&sch->schib))
  513. err = -ENODEV;
  514. else
  515. err = cio_check_devno_blacklisted(sch);
  516. break;
  517. default:
  518. err = 0;
  519. }
  520. if (err)
  521. goto out;
  522. CIO_MSG_EVENT(4, "Subchannel 0.%x.%04x reports subchannel type %04X\n",
  523. sch->schid.ssid, sch->schid.sch_no, sch->st);
  524. out:
  525. return err;
  526. }
  527. /*
  528. * do_cio_interrupt() handles all normal I/O device IRQ's
  529. */
  530. static irqreturn_t do_cio_interrupt(int irq, void *dummy)
  531. {
  532. struct tpi_info *tpi_info;
  533. struct subchannel *sch;
  534. struct irb *irb;
  535. set_cpu_flag(CIF_NOHZ_DELAY);
  536. tpi_info = (struct tpi_info *) &get_irq_regs()->int_code;
  537. trace_s390_cio_interrupt(tpi_info);
  538. irb = this_cpu_ptr(&cio_irb);
  539. sch = (struct subchannel *)(unsigned long) tpi_info->intparm;
  540. if (!sch) {
  541. /* Clear pending interrupt condition. */
  542. inc_irq_stat(IRQIO_CIO);
  543. tsch(tpi_info->schid, irb);
  544. return IRQ_HANDLED;
  545. }
  546. spin_lock(sch->lock);
  547. /* Store interrupt response block to lowcore. */
  548. if (tsch(tpi_info->schid, irb) == 0) {
  549. /* Keep subchannel information word up to date. */
  550. memcpy (&sch->schib.scsw, &irb->scsw, sizeof (irb->scsw));
  551. /* Call interrupt handler if there is one. */
  552. if (sch->driver && sch->driver->irq)
  553. sch->driver->irq(sch);
  554. else
  555. inc_irq_stat(IRQIO_CIO);
  556. } else
  557. inc_irq_stat(IRQIO_CIO);
  558. spin_unlock(sch->lock);
  559. return IRQ_HANDLED;
  560. }
  561. static struct irqaction io_interrupt = {
  562. .name = "IO",
  563. .handler = do_cio_interrupt,
  564. };
  565. void __init init_cio_interrupts(void)
  566. {
  567. irq_set_chip_and_handler(IO_INTERRUPT,
  568. &dummy_irq_chip, handle_percpu_irq);
  569. setup_irq(IO_INTERRUPT, &io_interrupt);
  570. }
  571. #ifdef CONFIG_CCW_CONSOLE
  572. static struct subchannel *console_sch;
  573. static struct lock_class_key console_sch_key;
  574. /*
  575. * Use cio_tsch to update the subchannel status and call the interrupt handler
  576. * if status had been pending. Called with the subchannel's lock held.
  577. */
  578. void cio_tsch(struct subchannel *sch)
  579. {
  580. struct irb *irb;
  581. int irq_context;
  582. irb = this_cpu_ptr(&cio_irb);
  583. /* Store interrupt response block to lowcore. */
  584. if (tsch(sch->schid, irb) != 0)
  585. /* Not status pending or not operational. */
  586. return;
  587. memcpy(&sch->schib.scsw, &irb->scsw, sizeof(union scsw));
  588. /* Call interrupt handler with updated status. */
  589. irq_context = in_interrupt();
  590. if (!irq_context) {
  591. local_bh_disable();
  592. irq_enter();
  593. }
  594. kstat_incr_irq_this_cpu(IO_INTERRUPT);
  595. if (sch->driver && sch->driver->irq)
  596. sch->driver->irq(sch);
  597. else
  598. inc_irq_stat(IRQIO_CIO);
  599. if (!irq_context) {
  600. irq_exit();
  601. _local_bh_enable();
  602. }
  603. }
  604. static int cio_test_for_console(struct subchannel_id schid, void *data)
  605. {
  606. struct schib schib;
  607. if (stsch(schid, &schib) != 0)
  608. return -ENXIO;
  609. if ((schib.pmcw.st == SUBCHANNEL_TYPE_IO) && schib.pmcw.dnv &&
  610. (schib.pmcw.dev == console_devno)) {
  611. console_irq = schid.sch_no;
  612. return 1; /* found */
  613. }
  614. return 0;
  615. }
  616. static int cio_get_console_sch_no(void)
  617. {
  618. struct subchannel_id schid;
  619. struct schib schib;
  620. init_subchannel_id(&schid);
  621. if (console_irq != -1) {
  622. /* VM provided us with the irq number of the console. */
  623. schid.sch_no = console_irq;
  624. if (stsch(schid, &schib) != 0 ||
  625. (schib.pmcw.st != SUBCHANNEL_TYPE_IO) || !schib.pmcw.dnv)
  626. return -1;
  627. console_devno = schib.pmcw.dev;
  628. } else if (console_devno != -1) {
  629. /* At least the console device number is known. */
  630. for_each_subchannel(cio_test_for_console, NULL);
  631. }
  632. return console_irq;
  633. }
  634. struct subchannel *cio_probe_console(void)
  635. {
  636. struct subchannel_id schid;
  637. struct subchannel *sch;
  638. int sch_no, ret;
  639. sch_no = cio_get_console_sch_no();
  640. if (sch_no == -1) {
  641. pr_warn("No CCW console was found\n");
  642. return ERR_PTR(-ENODEV);
  643. }
  644. init_subchannel_id(&schid);
  645. schid.sch_no = sch_no;
  646. sch = css_alloc_subchannel(schid);
  647. if (IS_ERR(sch))
  648. return sch;
  649. lockdep_set_class(sch->lock, &console_sch_key);
  650. isc_register(CONSOLE_ISC);
  651. sch->config.isc = CONSOLE_ISC;
  652. sch->config.intparm = (u32)(addr_t)sch;
  653. ret = cio_commit_config(sch);
  654. if (ret) {
  655. isc_unregister(CONSOLE_ISC);
  656. put_device(&sch->dev);
  657. return ERR_PTR(ret);
  658. }
  659. console_sch = sch;
  660. return sch;
  661. }
  662. int cio_is_console(struct subchannel_id schid)
  663. {
  664. if (!console_sch)
  665. return 0;
  666. return schid_equal(&schid, &console_sch->schid);
  667. }
  668. void cio_register_early_subchannels(void)
  669. {
  670. int ret;
  671. if (!console_sch)
  672. return;
  673. ret = css_register_subchannel(console_sch);
  674. if (ret)
  675. put_device(&console_sch->dev);
  676. }
  677. #endif /* CONFIG_CCW_CONSOLE */
  678. static int
  679. __disable_subchannel_easy(struct subchannel_id schid, struct schib *schib)
  680. {
  681. int retry, cc;
  682. cc = 0;
  683. for (retry=0;retry<3;retry++) {
  684. schib->pmcw.ena = 0;
  685. cc = msch(schid, schib);
  686. if (cc)
  687. return (cc==3?-ENODEV:-EBUSY);
  688. if (stsch(schid, schib) || !css_sch_is_valid(schib))
  689. return -ENODEV;
  690. if (!schib->pmcw.ena)
  691. return 0;
  692. }
  693. return -EBUSY; /* uhm... */
  694. }
  695. static int
  696. __clear_io_subchannel_easy(struct subchannel_id schid)
  697. {
  698. int retry;
  699. if (csch(schid))
  700. return -ENODEV;
  701. for (retry=0;retry<20;retry++) {
  702. struct tpi_info ti;
  703. if (tpi(&ti)) {
  704. tsch(ti.schid, this_cpu_ptr(&cio_irb));
  705. if (schid_equal(&ti.schid, &schid))
  706. return 0;
  707. }
  708. udelay_simple(100);
  709. }
  710. return -EBUSY;
  711. }
  712. static void __clear_chsc_subchannel_easy(void)
  713. {
  714. /* It seems we can only wait for a bit here :/ */
  715. udelay_simple(100);
  716. }
  717. static int pgm_check_occured;
  718. static void cio_reset_pgm_check_handler(void)
  719. {
  720. pgm_check_occured = 1;
  721. }
  722. static int stsch_reset(struct subchannel_id schid, struct schib *addr)
  723. {
  724. int rc;
  725. pgm_check_occured = 0;
  726. s390_base_pgm_handler_fn = cio_reset_pgm_check_handler;
  727. rc = stsch(schid, addr);
  728. s390_base_pgm_handler_fn = NULL;
  729. /* The program check handler could have changed pgm_check_occured. */
  730. barrier();
  731. if (pgm_check_occured)
  732. return -EIO;
  733. else
  734. return rc;
  735. }
  736. static int __shutdown_subchannel_easy(struct subchannel_id schid, void *data)
  737. {
  738. struct schib schib;
  739. if (stsch_reset(schid, &schib))
  740. return -ENXIO;
  741. if (!schib.pmcw.ena)
  742. return 0;
  743. switch(__disable_subchannel_easy(schid, &schib)) {
  744. case 0:
  745. case -ENODEV:
  746. break;
  747. default: /* -EBUSY */
  748. switch (schib.pmcw.st) {
  749. case SUBCHANNEL_TYPE_IO:
  750. if (__clear_io_subchannel_easy(schid))
  751. goto out; /* give up... */
  752. break;
  753. case SUBCHANNEL_TYPE_CHSC:
  754. __clear_chsc_subchannel_easy();
  755. break;
  756. default:
  757. /* No default clear strategy */
  758. break;
  759. }
  760. stsch(schid, &schib);
  761. __disable_subchannel_easy(schid, &schib);
  762. }
  763. out:
  764. return 0;
  765. }
  766. static atomic_t chpid_reset_count;
  767. static void s390_reset_chpids_mcck_handler(void)
  768. {
  769. struct crw crw;
  770. union mci mci;
  771. /* Check for pending channel report word. */
  772. mci.val = S390_lowcore.mcck_interruption_code;
  773. if (!mci.cp)
  774. return;
  775. /* Process channel report words. */
  776. while (stcrw(&crw) == 0) {
  777. /* Check for responses to RCHP. */
  778. if (crw.slct && crw.rsc == CRW_RSC_CPATH)
  779. atomic_dec(&chpid_reset_count);
  780. }
  781. }
  782. #define RCHP_TIMEOUT (30 * USEC_PER_SEC)
  783. static void css_reset(void)
  784. {
  785. int i, ret;
  786. unsigned long long timeout;
  787. struct chp_id chpid;
  788. /* Reset subchannels. */
  789. for_each_subchannel(__shutdown_subchannel_easy, NULL);
  790. /* Reset channel paths. */
  791. s390_base_mcck_handler_fn = s390_reset_chpids_mcck_handler;
  792. /* Enable channel report machine checks. */
  793. __ctl_set_bit(14, 28);
  794. /* Temporarily reenable machine checks. */
  795. local_mcck_enable();
  796. chp_id_init(&chpid);
  797. for (i = 0; i <= __MAX_CHPID; i++) {
  798. chpid.id = i;
  799. ret = rchp(chpid);
  800. if ((ret == 0) || (ret == 2))
  801. /*
  802. * rchp either succeeded, or another rchp is already
  803. * in progress. In either case, we'll get a crw.
  804. */
  805. atomic_inc(&chpid_reset_count);
  806. }
  807. /* Wait for machine check for all channel paths. */
  808. timeout = get_tod_clock_fast() + (RCHP_TIMEOUT << 12);
  809. while (atomic_read(&chpid_reset_count) != 0) {
  810. if (get_tod_clock_fast() > timeout)
  811. break;
  812. cpu_relax();
  813. }
  814. /* Disable machine checks again. */
  815. local_mcck_disable();
  816. /* Disable channel report machine checks. */
  817. __ctl_clear_bit(14, 28);
  818. s390_base_mcck_handler_fn = NULL;
  819. }
  820. static struct reset_call css_reset_call = {
  821. .fn = css_reset,
  822. };
  823. static int __init init_css_reset_call(void)
  824. {
  825. atomic_set(&chpid_reset_count, 0);
  826. register_reset_call(&css_reset_call);
  827. return 0;
  828. }
  829. arch_initcall(init_css_reset_call);
  830. struct sch_match_id {
  831. struct subchannel_id schid;
  832. struct ccw_dev_id devid;
  833. int rc;
  834. };
  835. static int __reipl_subchannel_match(struct subchannel_id schid, void *data)
  836. {
  837. struct schib schib;
  838. struct sch_match_id *match_id = data;
  839. if (stsch_reset(schid, &schib))
  840. return -ENXIO;
  841. if ((schib.pmcw.st == SUBCHANNEL_TYPE_IO) && schib.pmcw.dnv &&
  842. (schib.pmcw.dev == match_id->devid.devno) &&
  843. (schid.ssid == match_id->devid.ssid)) {
  844. match_id->schid = schid;
  845. match_id->rc = 0;
  846. return 1;
  847. }
  848. return 0;
  849. }
  850. static int reipl_find_schid(struct ccw_dev_id *devid,
  851. struct subchannel_id *schid)
  852. {
  853. struct sch_match_id match_id;
  854. match_id.devid = *devid;
  855. match_id.rc = -ENODEV;
  856. for_each_subchannel(__reipl_subchannel_match, &match_id);
  857. if (match_id.rc == 0)
  858. *schid = match_id.schid;
  859. return match_id.rc;
  860. }
  861. extern void do_reipl_asm(__u32 schid);
  862. /* Make sure all subchannels are quiet before we re-ipl an lpar. */
  863. void reipl_ccw_dev(struct ccw_dev_id *devid)
  864. {
  865. struct subchannel_id uninitialized_var(schid);
  866. s390_reset_system();
  867. if (reipl_find_schid(devid, &schid) != 0)
  868. panic("IPL Device not found\n");
  869. do_reipl_asm(*((__u32*)&schid));
  870. }
  871. int __init cio_get_iplinfo(struct cio_iplinfo *iplinfo)
  872. {
  873. static struct chsc_sda_area sda_area __initdata;
  874. struct subchannel_id schid;
  875. struct schib schib;
  876. schid = *(struct subchannel_id *)&S390_lowcore.subchannel_id;
  877. if (!schid.one)
  878. return -ENODEV;
  879. if (schid.ssid) {
  880. /*
  881. * Firmware should have already enabled MSS but whoever started
  882. * the kernel might have initiated a channel subsystem reset.
  883. * Ensure that MSS is enabled.
  884. */
  885. memset(&sda_area, 0, sizeof(sda_area));
  886. if (__chsc_enable_facility(&sda_area, CHSC_SDA_OC_MSS))
  887. return -ENODEV;
  888. }
  889. if (stsch(schid, &schib))
  890. return -ENODEV;
  891. if (schib.pmcw.st != SUBCHANNEL_TYPE_IO)
  892. return -ENODEV;
  893. if (!schib.pmcw.dnv)
  894. return -ENODEV;
  895. iplinfo->ssid = schid.ssid;
  896. iplinfo->devno = schib.pmcw.dev;
  897. iplinfo->is_qdio = schib.pmcw.qf;
  898. return 0;
  899. }
  900. /**
  901. * cio_tm_start_key - perform start function
  902. * @sch: subchannel on which to perform the start function
  903. * @tcw: transport-command word to be started
  904. * @lpm: mask of paths to use
  905. * @key: storage key to use for storage access
  906. *
  907. * Start the tcw on the given subchannel. Return zero on success, non-zero
  908. * otherwise.
  909. */
  910. int cio_tm_start_key(struct subchannel *sch, struct tcw *tcw, u8 lpm, u8 key)
  911. {
  912. int cc;
  913. union orb *orb = &to_io_private(sch)->orb;
  914. memset(orb, 0, sizeof(union orb));
  915. orb->tm.intparm = (u32) (addr_t) sch;
  916. orb->tm.key = key >> 4;
  917. orb->tm.b = 1;
  918. orb->tm.lpm = lpm ? lpm : sch->lpm;
  919. orb->tm.tcw = (u32) (addr_t) tcw;
  920. cc = ssch(sch->schid, orb);
  921. switch (cc) {
  922. case 0:
  923. return 0;
  924. case 1:
  925. case 2:
  926. return -EBUSY;
  927. default:
  928. return cio_start_handle_notoper(sch, lpm);
  929. }
  930. }
  931. EXPORT_SYMBOL_GPL(cio_tm_start_key);
  932. /**
  933. * cio_tm_intrg - perform interrogate function
  934. * @sch - subchannel on which to perform the interrogate function
  935. *
  936. * If the specified subchannel is running in transport-mode, perform the
  937. * interrogate function. Return zero on success, non-zero otherwie.
  938. */
  939. int cio_tm_intrg(struct subchannel *sch)
  940. {
  941. int cc;
  942. if (!to_io_private(sch)->orb.tm.b)
  943. return -EINVAL;
  944. cc = xsch(sch->schid);
  945. switch (cc) {
  946. case 0:
  947. case 2:
  948. return 0;
  949. case 1:
  950. return -EBUSY;
  951. default:
  952. return -ENODEV;
  953. }
  954. }
  955. EXPORT_SYMBOL_GPL(cio_tm_intrg);