cio.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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/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. int
  153. cio_start (struct subchannel *sch, struct ccw1 *cpa, __u8 lpm)
  154. {
  155. return cio_start_key(sch, cpa, lpm, PAGE_DEFAULT_KEY);
  156. }
  157. /*
  158. * resume suspended I/O operation
  159. */
  160. int
  161. cio_resume (struct subchannel *sch)
  162. {
  163. int ccode;
  164. CIO_TRACE_EVENT(4, "resIO");
  165. CIO_TRACE_EVENT(4, dev_name(&sch->dev));
  166. ccode = rsch (sch->schid);
  167. CIO_HEX_EVENT(4, &ccode, sizeof(ccode));
  168. switch (ccode) {
  169. case 0:
  170. sch->schib.scsw.cmd.actl |= SCSW_ACTL_RESUME_PEND;
  171. return 0;
  172. case 1:
  173. return -EBUSY;
  174. case 2:
  175. return -EINVAL;
  176. default:
  177. /*
  178. * useless to wait for request completion
  179. * as device is no longer operational !
  180. */
  181. return -ENODEV;
  182. }
  183. }
  184. /*
  185. * halt I/O operation
  186. */
  187. int
  188. cio_halt(struct subchannel *sch)
  189. {
  190. int ccode;
  191. if (!sch)
  192. return -ENODEV;
  193. CIO_TRACE_EVENT(2, "haltIO");
  194. CIO_TRACE_EVENT(2, dev_name(&sch->dev));
  195. /*
  196. * Issue "Halt subchannel" and process condition code
  197. */
  198. ccode = hsch (sch->schid);
  199. CIO_HEX_EVENT(2, &ccode, sizeof(ccode));
  200. switch (ccode) {
  201. case 0:
  202. sch->schib.scsw.cmd.actl |= SCSW_ACTL_HALT_PEND;
  203. return 0;
  204. case 1: /* status pending */
  205. case 2: /* busy */
  206. return -EBUSY;
  207. default: /* device not operational */
  208. return -ENODEV;
  209. }
  210. }
  211. /*
  212. * Clear I/O operation
  213. */
  214. int
  215. cio_clear(struct subchannel *sch)
  216. {
  217. int ccode;
  218. if (!sch)
  219. return -ENODEV;
  220. CIO_TRACE_EVENT(2, "clearIO");
  221. CIO_TRACE_EVENT(2, dev_name(&sch->dev));
  222. /*
  223. * Issue "Clear subchannel" and process condition code
  224. */
  225. ccode = csch (sch->schid);
  226. CIO_HEX_EVENT(2, &ccode, sizeof(ccode));
  227. switch (ccode) {
  228. case 0:
  229. sch->schib.scsw.cmd.actl |= SCSW_ACTL_CLEAR_PEND;
  230. return 0;
  231. default: /* device not operational */
  232. return -ENODEV;
  233. }
  234. }
  235. /*
  236. * Function: cio_cancel
  237. * Issues a "Cancel Subchannel" on the specified subchannel
  238. * Note: We don't need any fancy intparms and flags here
  239. * since xsch is executed synchronously.
  240. * Only for common I/O internal use as for now.
  241. */
  242. int
  243. cio_cancel (struct subchannel *sch)
  244. {
  245. int ccode;
  246. if (!sch)
  247. return -ENODEV;
  248. CIO_TRACE_EVENT(2, "cancelIO");
  249. CIO_TRACE_EVENT(2, dev_name(&sch->dev));
  250. ccode = xsch (sch->schid);
  251. CIO_HEX_EVENT(2, &ccode, sizeof(ccode));
  252. switch (ccode) {
  253. case 0: /* success */
  254. /* Update information in scsw. */
  255. if (cio_update_schib(sch))
  256. return -ENODEV;
  257. return 0;
  258. case 1: /* status pending */
  259. return -EBUSY;
  260. case 2: /* not applicable */
  261. return -EINVAL;
  262. default: /* not oper */
  263. return -ENODEV;
  264. }
  265. }
  266. static void cio_apply_config(struct subchannel *sch, struct schib *schib)
  267. {
  268. schib->pmcw.intparm = sch->config.intparm;
  269. schib->pmcw.mbi = sch->config.mbi;
  270. schib->pmcw.isc = sch->config.isc;
  271. schib->pmcw.ena = sch->config.ena;
  272. schib->pmcw.mme = sch->config.mme;
  273. schib->pmcw.mp = sch->config.mp;
  274. schib->pmcw.csense = sch->config.csense;
  275. schib->pmcw.mbfc = sch->config.mbfc;
  276. if (sch->config.mbfc)
  277. schib->mba = sch->config.mba;
  278. }
  279. static int cio_check_config(struct subchannel *sch, struct schib *schib)
  280. {
  281. return (schib->pmcw.intparm == sch->config.intparm) &&
  282. (schib->pmcw.mbi == sch->config.mbi) &&
  283. (schib->pmcw.isc == sch->config.isc) &&
  284. (schib->pmcw.ena == sch->config.ena) &&
  285. (schib->pmcw.mme == sch->config.mme) &&
  286. (schib->pmcw.mp == sch->config.mp) &&
  287. (schib->pmcw.csense == sch->config.csense) &&
  288. (schib->pmcw.mbfc == sch->config.mbfc) &&
  289. (!sch->config.mbfc || (schib->mba == sch->config.mba));
  290. }
  291. /*
  292. * cio_commit_config - apply configuration to the subchannel
  293. */
  294. int cio_commit_config(struct subchannel *sch)
  295. {
  296. int ccode, retry, ret = 0;
  297. struct schib schib;
  298. struct irb irb;
  299. if (stsch(sch->schid, &schib) || !css_sch_is_valid(&schib))
  300. return -ENODEV;
  301. for (retry = 0; retry < 5; retry++) {
  302. /* copy desired changes to local schib */
  303. cio_apply_config(sch, &schib);
  304. ccode = msch(sch->schid, &schib);
  305. if (ccode < 0) /* -EIO if msch gets a program check. */
  306. return ccode;
  307. switch (ccode) {
  308. case 0: /* successful */
  309. if (stsch(sch->schid, &schib) ||
  310. !css_sch_is_valid(&schib))
  311. return -ENODEV;
  312. if (cio_check_config(sch, &schib)) {
  313. /* commit changes from local schib */
  314. memcpy(&sch->schib, &schib, sizeof(schib));
  315. return 0;
  316. }
  317. ret = -EAGAIN;
  318. break;
  319. case 1: /* status pending */
  320. ret = -EBUSY;
  321. if (tsch(sch->schid, &irb))
  322. return ret;
  323. break;
  324. case 2: /* busy */
  325. udelay(100); /* allow for recovery */
  326. ret = -EBUSY;
  327. break;
  328. case 3: /* not operational */
  329. return -ENODEV;
  330. }
  331. }
  332. return ret;
  333. }
  334. /**
  335. * cio_update_schib - Perform stsch and update schib if subchannel is valid.
  336. * @sch: subchannel on which to perform stsch
  337. * Return zero on success, -ENODEV otherwise.
  338. */
  339. int cio_update_schib(struct subchannel *sch)
  340. {
  341. struct schib schib;
  342. if (stsch(sch->schid, &schib) || !css_sch_is_valid(&schib))
  343. return -ENODEV;
  344. memcpy(&sch->schib, &schib, sizeof(schib));
  345. return 0;
  346. }
  347. EXPORT_SYMBOL_GPL(cio_update_schib);
  348. /**
  349. * cio_enable_subchannel - enable a subchannel.
  350. * @sch: subchannel to be enabled
  351. * @intparm: interruption parameter to set
  352. */
  353. int cio_enable_subchannel(struct subchannel *sch, u32 intparm)
  354. {
  355. int ret;
  356. CIO_TRACE_EVENT(2, "ensch");
  357. CIO_TRACE_EVENT(2, dev_name(&sch->dev));
  358. if (sch_is_pseudo_sch(sch))
  359. return -EINVAL;
  360. if (cio_update_schib(sch))
  361. return -ENODEV;
  362. sch->config.ena = 1;
  363. sch->config.isc = sch->isc;
  364. sch->config.intparm = intparm;
  365. ret = cio_commit_config(sch);
  366. if (ret == -EIO) {
  367. /*
  368. * Got a program check in msch. Try without
  369. * the concurrent sense bit the next time.
  370. */
  371. sch->config.csense = 0;
  372. ret = cio_commit_config(sch);
  373. }
  374. CIO_HEX_EVENT(2, &ret, sizeof(ret));
  375. return ret;
  376. }
  377. EXPORT_SYMBOL_GPL(cio_enable_subchannel);
  378. /**
  379. * cio_disable_subchannel - disable a subchannel.
  380. * @sch: subchannel to disable
  381. */
  382. int cio_disable_subchannel(struct subchannel *sch)
  383. {
  384. int ret;
  385. CIO_TRACE_EVENT(2, "dissch");
  386. CIO_TRACE_EVENT(2, dev_name(&sch->dev));
  387. if (sch_is_pseudo_sch(sch))
  388. return 0;
  389. if (cio_update_schib(sch))
  390. return -ENODEV;
  391. sch->config.ena = 0;
  392. ret = cio_commit_config(sch);
  393. CIO_HEX_EVENT(2, &ret, sizeof(ret));
  394. return ret;
  395. }
  396. EXPORT_SYMBOL_GPL(cio_disable_subchannel);
  397. static int cio_check_devno_blacklisted(struct subchannel *sch)
  398. {
  399. if (is_blacklisted(sch->schid.ssid, sch->schib.pmcw.dev)) {
  400. /*
  401. * This device must not be known to Linux. So we simply
  402. * say that there is no device and return ENODEV.
  403. */
  404. CIO_MSG_EVENT(6, "Blacklisted device detected "
  405. "at devno %04X, subchannel set %x\n",
  406. sch->schib.pmcw.dev, sch->schid.ssid);
  407. return -ENODEV;
  408. }
  409. return 0;
  410. }
  411. /**
  412. * cio_validate_subchannel - basic validation of subchannel
  413. * @sch: subchannel structure to be filled out
  414. * @schid: subchannel id
  415. *
  416. * Find out subchannel type and initialize struct subchannel.
  417. * Return codes:
  418. * 0 on success
  419. * -ENXIO for non-defined subchannels
  420. * -ENODEV for invalid subchannels or blacklisted devices
  421. * -EIO for subchannels in an invalid subchannel set
  422. */
  423. int cio_validate_subchannel(struct subchannel *sch, struct subchannel_id schid)
  424. {
  425. char dbf_txt[15];
  426. int ccode;
  427. int err;
  428. sprintf(dbf_txt, "valsch%x", schid.sch_no);
  429. CIO_TRACE_EVENT(4, dbf_txt);
  430. /*
  431. * The first subchannel that is not-operational (ccode==3)
  432. * indicates that there aren't any more devices available.
  433. * If stsch gets an exception, it means the current subchannel set
  434. * is not valid.
  435. */
  436. ccode = stsch(schid, &sch->schib);
  437. if (ccode) {
  438. err = (ccode == 3) ? -ENXIO : ccode;
  439. goto out;
  440. }
  441. sch->st = sch->schib.pmcw.st;
  442. sch->schid = schid;
  443. switch (sch->st) {
  444. case SUBCHANNEL_TYPE_IO:
  445. case SUBCHANNEL_TYPE_MSG:
  446. if (!css_sch_is_valid(&sch->schib))
  447. err = -ENODEV;
  448. else
  449. err = cio_check_devno_blacklisted(sch);
  450. break;
  451. default:
  452. err = 0;
  453. }
  454. if (err)
  455. goto out;
  456. CIO_MSG_EVENT(4, "Subchannel 0.%x.%04x reports subchannel type %04X\n",
  457. sch->schid.ssid, sch->schid.sch_no, sch->st);
  458. out:
  459. return err;
  460. }
  461. /*
  462. * do_cio_interrupt() handles all normal I/O device IRQ's
  463. */
  464. static irqreturn_t do_cio_interrupt(int irq, void *dummy)
  465. {
  466. struct tpi_info *tpi_info;
  467. struct subchannel *sch;
  468. struct irb *irb;
  469. set_cpu_flag(CIF_NOHZ_DELAY);
  470. tpi_info = (struct tpi_info *) &get_irq_regs()->int_code;
  471. trace_s390_cio_interrupt(tpi_info);
  472. irb = this_cpu_ptr(&cio_irb);
  473. sch = (struct subchannel *)(unsigned long) tpi_info->intparm;
  474. if (!sch) {
  475. /* Clear pending interrupt condition. */
  476. inc_irq_stat(IRQIO_CIO);
  477. tsch(tpi_info->schid, irb);
  478. return IRQ_HANDLED;
  479. }
  480. spin_lock(sch->lock);
  481. /* Store interrupt response block to lowcore. */
  482. if (tsch(tpi_info->schid, irb) == 0) {
  483. /* Keep subchannel information word up to date. */
  484. memcpy (&sch->schib.scsw, &irb->scsw, sizeof (irb->scsw));
  485. /* Call interrupt handler if there is one. */
  486. if (sch->driver && sch->driver->irq)
  487. sch->driver->irq(sch);
  488. else
  489. inc_irq_stat(IRQIO_CIO);
  490. } else
  491. inc_irq_stat(IRQIO_CIO);
  492. spin_unlock(sch->lock);
  493. return IRQ_HANDLED;
  494. }
  495. static struct irqaction io_interrupt = {
  496. .name = "IO",
  497. .handler = do_cio_interrupt,
  498. };
  499. void __init init_cio_interrupts(void)
  500. {
  501. irq_set_chip_and_handler(IO_INTERRUPT,
  502. &dummy_irq_chip, handle_percpu_irq);
  503. setup_irq(IO_INTERRUPT, &io_interrupt);
  504. }
  505. #ifdef CONFIG_CCW_CONSOLE
  506. static struct subchannel *console_sch;
  507. static struct lock_class_key console_sch_key;
  508. /*
  509. * Use cio_tsch to update the subchannel status and call the interrupt handler
  510. * if status had been pending. Called with the subchannel's lock held.
  511. */
  512. void cio_tsch(struct subchannel *sch)
  513. {
  514. struct irb *irb;
  515. int irq_context;
  516. irb = this_cpu_ptr(&cio_irb);
  517. /* Store interrupt response block to lowcore. */
  518. if (tsch(sch->schid, irb) != 0)
  519. /* Not status pending or not operational. */
  520. return;
  521. memcpy(&sch->schib.scsw, &irb->scsw, sizeof(union scsw));
  522. /* Call interrupt handler with updated status. */
  523. irq_context = in_interrupt();
  524. if (!irq_context) {
  525. local_bh_disable();
  526. irq_enter();
  527. }
  528. kstat_incr_irq_this_cpu(IO_INTERRUPT);
  529. if (sch->driver && sch->driver->irq)
  530. sch->driver->irq(sch);
  531. else
  532. inc_irq_stat(IRQIO_CIO);
  533. if (!irq_context) {
  534. irq_exit();
  535. _local_bh_enable();
  536. }
  537. }
  538. static int cio_test_for_console(struct subchannel_id schid, void *data)
  539. {
  540. struct schib schib;
  541. if (stsch(schid, &schib) != 0)
  542. return -ENXIO;
  543. if ((schib.pmcw.st == SUBCHANNEL_TYPE_IO) && schib.pmcw.dnv &&
  544. (schib.pmcw.dev == console_devno)) {
  545. console_irq = schid.sch_no;
  546. return 1; /* found */
  547. }
  548. return 0;
  549. }
  550. static int cio_get_console_sch_no(void)
  551. {
  552. struct subchannel_id schid;
  553. struct schib schib;
  554. init_subchannel_id(&schid);
  555. if (console_irq != -1) {
  556. /* VM provided us with the irq number of the console. */
  557. schid.sch_no = console_irq;
  558. if (stsch(schid, &schib) != 0 ||
  559. (schib.pmcw.st != SUBCHANNEL_TYPE_IO) || !schib.pmcw.dnv)
  560. return -1;
  561. console_devno = schib.pmcw.dev;
  562. } else if (console_devno != -1) {
  563. /* At least the console device number is known. */
  564. for_each_subchannel(cio_test_for_console, NULL);
  565. }
  566. return console_irq;
  567. }
  568. struct subchannel *cio_probe_console(void)
  569. {
  570. struct subchannel_id schid;
  571. struct subchannel *sch;
  572. int sch_no, ret;
  573. sch_no = cio_get_console_sch_no();
  574. if (sch_no == -1) {
  575. pr_warn("No CCW console was found\n");
  576. return ERR_PTR(-ENODEV);
  577. }
  578. init_subchannel_id(&schid);
  579. schid.sch_no = sch_no;
  580. sch = css_alloc_subchannel(schid);
  581. if (IS_ERR(sch))
  582. return sch;
  583. lockdep_set_class(sch->lock, &console_sch_key);
  584. isc_register(CONSOLE_ISC);
  585. sch->config.isc = CONSOLE_ISC;
  586. sch->config.intparm = (u32)(addr_t)sch;
  587. ret = cio_commit_config(sch);
  588. if (ret) {
  589. isc_unregister(CONSOLE_ISC);
  590. put_device(&sch->dev);
  591. return ERR_PTR(ret);
  592. }
  593. console_sch = sch;
  594. return sch;
  595. }
  596. int cio_is_console(struct subchannel_id schid)
  597. {
  598. if (!console_sch)
  599. return 0;
  600. return schid_equal(&schid, &console_sch->schid);
  601. }
  602. void cio_register_early_subchannels(void)
  603. {
  604. int ret;
  605. if (!console_sch)
  606. return;
  607. ret = css_register_subchannel(console_sch);
  608. if (ret)
  609. put_device(&console_sch->dev);
  610. }
  611. #endif /* CONFIG_CCW_CONSOLE */
  612. static int
  613. __disable_subchannel_easy(struct subchannel_id schid, struct schib *schib)
  614. {
  615. int retry, cc;
  616. cc = 0;
  617. for (retry=0;retry<3;retry++) {
  618. schib->pmcw.ena = 0;
  619. cc = msch(schid, schib);
  620. if (cc)
  621. return (cc==3?-ENODEV:-EBUSY);
  622. if (stsch(schid, schib) || !css_sch_is_valid(schib))
  623. return -ENODEV;
  624. if (!schib->pmcw.ena)
  625. return 0;
  626. }
  627. return -EBUSY; /* uhm... */
  628. }
  629. static int
  630. __clear_io_subchannel_easy(struct subchannel_id schid)
  631. {
  632. int retry;
  633. if (csch(schid))
  634. return -ENODEV;
  635. for (retry=0;retry<20;retry++) {
  636. struct tpi_info ti;
  637. if (tpi(&ti)) {
  638. tsch(ti.schid, this_cpu_ptr(&cio_irb));
  639. if (schid_equal(&ti.schid, &schid))
  640. return 0;
  641. }
  642. udelay_simple(100);
  643. }
  644. return -EBUSY;
  645. }
  646. static void __clear_chsc_subchannel_easy(void)
  647. {
  648. /* It seems we can only wait for a bit here :/ */
  649. udelay_simple(100);
  650. }
  651. static int pgm_check_occured;
  652. static void cio_reset_pgm_check_handler(void)
  653. {
  654. pgm_check_occured = 1;
  655. }
  656. static int stsch_reset(struct subchannel_id schid, struct schib *addr)
  657. {
  658. int rc;
  659. pgm_check_occured = 0;
  660. s390_base_pgm_handler_fn = cio_reset_pgm_check_handler;
  661. rc = stsch(schid, addr);
  662. s390_base_pgm_handler_fn = NULL;
  663. /* The program check handler could have changed pgm_check_occured. */
  664. barrier();
  665. if (pgm_check_occured)
  666. return -EIO;
  667. else
  668. return rc;
  669. }
  670. static int __shutdown_subchannel_easy(struct subchannel_id schid, void *data)
  671. {
  672. struct schib schib;
  673. if (stsch_reset(schid, &schib))
  674. return -ENXIO;
  675. if (!schib.pmcw.ena)
  676. return 0;
  677. switch(__disable_subchannel_easy(schid, &schib)) {
  678. case 0:
  679. case -ENODEV:
  680. break;
  681. default: /* -EBUSY */
  682. switch (schib.pmcw.st) {
  683. case SUBCHANNEL_TYPE_IO:
  684. if (__clear_io_subchannel_easy(schid))
  685. goto out; /* give up... */
  686. break;
  687. case SUBCHANNEL_TYPE_CHSC:
  688. __clear_chsc_subchannel_easy();
  689. break;
  690. default:
  691. /* No default clear strategy */
  692. break;
  693. }
  694. stsch(schid, &schib);
  695. __disable_subchannel_easy(schid, &schib);
  696. }
  697. out:
  698. return 0;
  699. }
  700. static atomic_t chpid_reset_count;
  701. static void s390_reset_chpids_mcck_handler(void)
  702. {
  703. struct crw crw;
  704. union mci mci;
  705. /* Check for pending channel report word. */
  706. mci.val = S390_lowcore.mcck_interruption_code;
  707. if (!mci.cp)
  708. return;
  709. /* Process channel report words. */
  710. while (stcrw(&crw) == 0) {
  711. /* Check for responses to RCHP. */
  712. if (crw.slct && crw.rsc == CRW_RSC_CPATH)
  713. atomic_dec(&chpid_reset_count);
  714. }
  715. }
  716. #define RCHP_TIMEOUT (30 * USEC_PER_SEC)
  717. static void css_reset(void)
  718. {
  719. int i, ret;
  720. unsigned long long timeout;
  721. struct chp_id chpid;
  722. /* Reset subchannels. */
  723. for_each_subchannel(__shutdown_subchannel_easy, NULL);
  724. /* Reset channel paths. */
  725. s390_base_mcck_handler_fn = s390_reset_chpids_mcck_handler;
  726. /* Enable channel report machine checks. */
  727. __ctl_set_bit(14, 28);
  728. /* Temporarily reenable machine checks. */
  729. local_mcck_enable();
  730. chp_id_init(&chpid);
  731. for (i = 0; i <= __MAX_CHPID; i++) {
  732. chpid.id = i;
  733. ret = rchp(chpid);
  734. if ((ret == 0) || (ret == 2))
  735. /*
  736. * rchp either succeeded, or another rchp is already
  737. * in progress. In either case, we'll get a crw.
  738. */
  739. atomic_inc(&chpid_reset_count);
  740. }
  741. /* Wait for machine check for all channel paths. */
  742. timeout = get_tod_clock_fast() + (RCHP_TIMEOUT << 12);
  743. while (atomic_read(&chpid_reset_count) != 0) {
  744. if (get_tod_clock_fast() > timeout)
  745. break;
  746. cpu_relax();
  747. }
  748. /* Disable machine checks again. */
  749. local_mcck_disable();
  750. /* Disable channel report machine checks. */
  751. __ctl_clear_bit(14, 28);
  752. s390_base_mcck_handler_fn = NULL;
  753. }
  754. static struct reset_call css_reset_call = {
  755. .fn = css_reset,
  756. };
  757. static int __init init_css_reset_call(void)
  758. {
  759. atomic_set(&chpid_reset_count, 0);
  760. register_reset_call(&css_reset_call);
  761. return 0;
  762. }
  763. arch_initcall(init_css_reset_call);
  764. struct sch_match_id {
  765. struct subchannel_id schid;
  766. struct ccw_dev_id devid;
  767. int rc;
  768. };
  769. static int __reipl_subchannel_match(struct subchannel_id schid, void *data)
  770. {
  771. struct schib schib;
  772. struct sch_match_id *match_id = data;
  773. if (stsch_reset(schid, &schib))
  774. return -ENXIO;
  775. if ((schib.pmcw.st == SUBCHANNEL_TYPE_IO) && schib.pmcw.dnv &&
  776. (schib.pmcw.dev == match_id->devid.devno) &&
  777. (schid.ssid == match_id->devid.ssid)) {
  778. match_id->schid = schid;
  779. match_id->rc = 0;
  780. return 1;
  781. }
  782. return 0;
  783. }
  784. static int reipl_find_schid(struct ccw_dev_id *devid,
  785. struct subchannel_id *schid)
  786. {
  787. struct sch_match_id match_id;
  788. match_id.devid = *devid;
  789. match_id.rc = -ENODEV;
  790. for_each_subchannel(__reipl_subchannel_match, &match_id);
  791. if (match_id.rc == 0)
  792. *schid = match_id.schid;
  793. return match_id.rc;
  794. }
  795. extern void do_reipl_asm(__u32 schid);
  796. /* Make sure all subchannels are quiet before we re-ipl an lpar. */
  797. void reipl_ccw_dev(struct ccw_dev_id *devid)
  798. {
  799. struct subchannel_id uninitialized_var(schid);
  800. s390_reset_system();
  801. if (reipl_find_schid(devid, &schid) != 0)
  802. panic("IPL Device not found\n");
  803. do_reipl_asm(*((__u32*)&schid));
  804. }
  805. int __init cio_get_iplinfo(struct cio_iplinfo *iplinfo)
  806. {
  807. static struct chsc_sda_area sda_area __initdata;
  808. struct subchannel_id schid;
  809. struct schib schib;
  810. schid = *(struct subchannel_id *)&S390_lowcore.subchannel_id;
  811. if (!schid.one)
  812. return -ENODEV;
  813. if (schid.ssid) {
  814. /*
  815. * Firmware should have already enabled MSS but whoever started
  816. * the kernel might have initiated a channel subsystem reset.
  817. * Ensure that MSS is enabled.
  818. */
  819. memset(&sda_area, 0, sizeof(sda_area));
  820. if (__chsc_enable_facility(&sda_area, CHSC_SDA_OC_MSS))
  821. return -ENODEV;
  822. }
  823. if (stsch(schid, &schib))
  824. return -ENODEV;
  825. if (schib.pmcw.st != SUBCHANNEL_TYPE_IO)
  826. return -ENODEV;
  827. if (!schib.pmcw.dnv)
  828. return -ENODEV;
  829. iplinfo->ssid = schid.ssid;
  830. iplinfo->devno = schib.pmcw.dev;
  831. iplinfo->is_qdio = schib.pmcw.qf;
  832. return 0;
  833. }
  834. /**
  835. * cio_tm_start_key - perform start function
  836. * @sch: subchannel on which to perform the start function
  837. * @tcw: transport-command word to be started
  838. * @lpm: mask of paths to use
  839. * @key: storage key to use for storage access
  840. *
  841. * Start the tcw on the given subchannel. Return zero on success, non-zero
  842. * otherwise.
  843. */
  844. int cio_tm_start_key(struct subchannel *sch, struct tcw *tcw, u8 lpm, u8 key)
  845. {
  846. int cc;
  847. union orb *orb = &to_io_private(sch)->orb;
  848. memset(orb, 0, sizeof(union orb));
  849. orb->tm.intparm = (u32) (addr_t) sch;
  850. orb->tm.key = key >> 4;
  851. orb->tm.b = 1;
  852. orb->tm.lpm = lpm ? lpm : sch->lpm;
  853. orb->tm.tcw = (u32) (addr_t) tcw;
  854. cc = ssch(sch->schid, orb);
  855. switch (cc) {
  856. case 0:
  857. return 0;
  858. case 1:
  859. case 2:
  860. return -EBUSY;
  861. default:
  862. return cio_start_handle_notoper(sch, lpm);
  863. }
  864. }
  865. /**
  866. * cio_tm_intrg - perform interrogate function
  867. * @sch - subchannel on which to perform the interrogate function
  868. *
  869. * If the specified subchannel is running in transport-mode, perform the
  870. * interrogate function. Return zero on success, non-zero otherwie.
  871. */
  872. int cio_tm_intrg(struct subchannel *sch)
  873. {
  874. int cc;
  875. if (!to_io_private(sch)->orb.tm.b)
  876. return -EINVAL;
  877. cc = xsch(sch->schid);
  878. switch (cc) {
  879. case 0:
  880. case 2:
  881. return 0;
  882. case 1:
  883. return -EBUSY;
  884. default:
  885. return -ENODEV;
  886. }
  887. }