cio.c 25 KB

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