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