cio.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. /*
  2. * drivers/s390/cio/cio.c
  3. * S/390 common I/O routines -- low level i/o calls
  4. *
  5. * Copyright IBM Corp. 1999,2008
  6. * Author(s): Ingo Adlung (adlung@de.ibm.com)
  7. * Cornelia Huck (cornelia.huck@de.ibm.com)
  8. * Arnd Bergmann (arndb@de.ibm.com)
  9. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  10. */
  11. #define KMSG_COMPONENT "cio"
  12. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  13. #include <linux/ftrace.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/slab.h>
  17. #include <linux/device.h>
  18. #include <linux/kernel_stat.h>
  19. #include <linux/interrupt.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 <asm/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. /*
  46. * Function: cio_debug_init
  47. * Initializes three debug logs for common I/O:
  48. * - cio_msg logs generic cio messages
  49. * - cio_trace logs the calling of different functions
  50. * - cio_crw logs machine check related cio messages
  51. */
  52. static int __init cio_debug_init(void)
  53. {
  54. cio_debug_msg_id = debug_register("cio_msg", 16, 1, 16 * sizeof(long));
  55. if (!cio_debug_msg_id)
  56. goto out_unregister;
  57. debug_register_view(cio_debug_msg_id, &debug_sprintf_view);
  58. debug_set_level(cio_debug_msg_id, 2);
  59. cio_debug_trace_id = debug_register("cio_trace", 16, 1, 16);
  60. if (!cio_debug_trace_id)
  61. goto out_unregister;
  62. debug_register_view(cio_debug_trace_id, &debug_hex_ascii_view);
  63. debug_set_level(cio_debug_trace_id, 2);
  64. cio_debug_crw_id = debug_register("cio_crw", 16, 1, 16 * sizeof(long));
  65. if (!cio_debug_crw_id)
  66. goto out_unregister;
  67. debug_register_view(cio_debug_crw_id, &debug_sprintf_view);
  68. debug_set_level(cio_debug_crw_id, 4);
  69. return 0;
  70. out_unregister:
  71. if (cio_debug_msg_id)
  72. debug_unregister(cio_debug_msg_id);
  73. if (cio_debug_trace_id)
  74. debug_unregister(cio_debug_trace_id);
  75. if (cio_debug_crw_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. #ifdef CONFIG_64BIT
  126. /*
  127. * for 64 bit we always support 64 bit IDAWs with 4k page size only
  128. */
  129. orb->cmd.c64 = 1;
  130. orb->cmd.i2k = 0;
  131. #endif
  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. struct schib schib;
  299. int ccode, retry, ret = 0;
  300. if (stsch_err(sch->schid, &schib) || !css_sch_is_valid(&schib))
  301. return -ENODEV;
  302. for (retry = 0; retry < 5; retry++) {
  303. /* copy desired changes to local schib */
  304. cio_apply_config(sch, &schib);
  305. ccode = msch_err(sch->schid, &schib);
  306. if (ccode < 0) /* -EIO if msch gets a program check. */
  307. return ccode;
  308. switch (ccode) {
  309. case 0: /* successful */
  310. if (stsch_err(sch->schid, &schib) ||
  311. !css_sch_is_valid(&schib))
  312. return -ENODEV;
  313. if (cio_check_config(sch, &schib)) {
  314. /* commit changes from local schib */
  315. memcpy(&sch->schib, &schib, sizeof(schib));
  316. return 0;
  317. }
  318. ret = -EAGAIN;
  319. break;
  320. case 1: /* status pending */
  321. return -EBUSY;
  322. case 2: /* busy */
  323. udelay(100); /* allow for recovery */
  324. ret = -EBUSY;
  325. break;
  326. case 3: /* not operational */
  327. return -ENODEV;
  328. }
  329. }
  330. return ret;
  331. }
  332. /**
  333. * cio_update_schib - Perform stsch and update schib if subchannel is valid.
  334. * @sch: subchannel on which to perform stsch
  335. * Return zero on success, -ENODEV otherwise.
  336. */
  337. int cio_update_schib(struct subchannel *sch)
  338. {
  339. struct schib schib;
  340. if (stsch_err(sch->schid, &schib) || !css_sch_is_valid(&schib))
  341. return -ENODEV;
  342. memcpy(&sch->schib, &schib, sizeof(schib));
  343. return 0;
  344. }
  345. EXPORT_SYMBOL_GPL(cio_update_schib);
  346. /**
  347. * cio_enable_subchannel - enable a subchannel.
  348. * @sch: subchannel to be enabled
  349. * @intparm: interruption parameter to set
  350. */
  351. int cio_enable_subchannel(struct subchannel *sch, u32 intparm)
  352. {
  353. int retry;
  354. int ret;
  355. CIO_TRACE_EVENT(2, "ensch");
  356. CIO_TRACE_EVENT(2, dev_name(&sch->dev));
  357. if (sch_is_pseudo_sch(sch))
  358. return -EINVAL;
  359. if (cio_update_schib(sch))
  360. return -ENODEV;
  361. sch->config.ena = 1;
  362. sch->config.isc = sch->isc;
  363. sch->config.intparm = intparm;
  364. for (retry = 0; retry < 3; retry++) {
  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. } else if (ret == -EBUSY) {
  373. struct irb irb;
  374. if (tsch(sch->schid, &irb) != 0)
  375. break;
  376. } else
  377. break;
  378. }
  379. CIO_HEX_EVENT(2, &ret, sizeof(ret));
  380. return ret;
  381. }
  382. EXPORT_SYMBOL_GPL(cio_enable_subchannel);
  383. /**
  384. * cio_disable_subchannel - disable a subchannel.
  385. * @sch: subchannel to disable
  386. */
  387. int cio_disable_subchannel(struct subchannel *sch)
  388. {
  389. int retry;
  390. int ret;
  391. CIO_TRACE_EVENT(2, "dissch");
  392. CIO_TRACE_EVENT(2, dev_name(&sch->dev));
  393. if (sch_is_pseudo_sch(sch))
  394. return 0;
  395. if (cio_update_schib(sch))
  396. return -ENODEV;
  397. sch->config.ena = 0;
  398. for (retry = 0; retry < 3; retry++) {
  399. ret = cio_commit_config(sch);
  400. if (ret == -EBUSY) {
  401. struct irb irb;
  402. if (tsch(sch->schid, &irb) != 0)
  403. break;
  404. } else
  405. break;
  406. }
  407. CIO_HEX_EVENT(2, &ret, sizeof(ret));
  408. return ret;
  409. }
  410. EXPORT_SYMBOL_GPL(cio_disable_subchannel);
  411. int cio_create_sch_lock(struct subchannel *sch)
  412. {
  413. sch->lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
  414. if (!sch->lock)
  415. return -ENOMEM;
  416. spin_lock_init(sch->lock);
  417. return 0;
  418. }
  419. static int cio_check_devno_blacklisted(struct subchannel *sch)
  420. {
  421. if (is_blacklisted(sch->schid.ssid, sch->schib.pmcw.dev)) {
  422. /*
  423. * This device must not be known to Linux. So we simply
  424. * say that there is no device and return ENODEV.
  425. */
  426. CIO_MSG_EVENT(6, "Blacklisted device detected "
  427. "at devno %04X, subchannel set %x\n",
  428. sch->schib.pmcw.dev, sch->schid.ssid);
  429. return -ENODEV;
  430. }
  431. return 0;
  432. }
  433. static int cio_validate_io_subchannel(struct subchannel *sch)
  434. {
  435. /* Initialization for io subchannels. */
  436. if (!css_sch_is_valid(&sch->schib))
  437. return -ENODEV;
  438. /* Devno is valid. */
  439. return cio_check_devno_blacklisted(sch);
  440. }
  441. static int cio_validate_msg_subchannel(struct subchannel *sch)
  442. {
  443. /* Initialization for message subchannels. */
  444. if (!css_sch_is_valid(&sch->schib))
  445. return -ENODEV;
  446. /* Devno is valid. */
  447. return cio_check_devno_blacklisted(sch);
  448. }
  449. /**
  450. * cio_validate_subchannel - basic validation of subchannel
  451. * @sch: subchannel structure to be filled out
  452. * @schid: subchannel id
  453. *
  454. * Find out subchannel type and initialize struct subchannel.
  455. * Return codes:
  456. * 0 on success
  457. * -ENXIO for non-defined subchannels
  458. * -ENODEV for invalid subchannels or blacklisted devices
  459. * -EIO for subchannels in an invalid subchannel set
  460. */
  461. int cio_validate_subchannel(struct subchannel *sch, struct subchannel_id schid)
  462. {
  463. char dbf_txt[15];
  464. int ccode;
  465. int err;
  466. sprintf(dbf_txt, "valsch%x", schid.sch_no);
  467. CIO_TRACE_EVENT(4, dbf_txt);
  468. /* Nuke all fields. */
  469. memset(sch, 0, sizeof(struct subchannel));
  470. sch->schid = schid;
  471. if (cio_is_console(schid)) {
  472. sch->lock = cio_get_console_lock();
  473. } else {
  474. err = cio_create_sch_lock(sch);
  475. if (err)
  476. goto out;
  477. }
  478. mutex_init(&sch->reg_mutex);
  479. /*
  480. * The first subchannel that is not-operational (ccode==3)
  481. * indicates that there aren't any more devices available.
  482. * If stsch gets an exception, it means the current subchannel set
  483. * is not valid.
  484. */
  485. ccode = stsch_err (schid, &sch->schib);
  486. if (ccode) {
  487. err = (ccode == 3) ? -ENXIO : ccode;
  488. goto out;
  489. }
  490. /* Copy subchannel type from path management control word. */
  491. sch->st = sch->schib.pmcw.st;
  492. switch (sch->st) {
  493. case SUBCHANNEL_TYPE_IO:
  494. err = cio_validate_io_subchannel(sch);
  495. break;
  496. case SUBCHANNEL_TYPE_MSG:
  497. err = cio_validate_msg_subchannel(sch);
  498. break;
  499. default:
  500. err = 0;
  501. }
  502. if (err)
  503. goto out;
  504. CIO_MSG_EVENT(4, "Subchannel 0.%x.%04x reports subchannel type %04X\n",
  505. sch->schid.ssid, sch->schid.sch_no, sch->st);
  506. return 0;
  507. out:
  508. if (!cio_is_console(schid))
  509. kfree(sch->lock);
  510. sch->lock = NULL;
  511. return err;
  512. }
  513. /*
  514. * do_IRQ() handles all normal I/O device IRQ's (the special
  515. * SMP cross-CPU interrupts have their own specific
  516. * handlers).
  517. *
  518. */
  519. void __irq_entry do_IRQ(struct pt_regs *regs)
  520. {
  521. struct tpi_info *tpi_info;
  522. struct subchannel *sch;
  523. struct irb *irb;
  524. struct pt_regs *old_regs;
  525. old_regs = set_irq_regs(regs);
  526. s390_idle_check(regs, S390_lowcore.int_clock,
  527. S390_lowcore.async_enter_timer);
  528. irq_enter();
  529. __this_cpu_write(s390_idle.nohz_delay, 1);
  530. if (S390_lowcore.int_clock >= S390_lowcore.clock_comparator)
  531. /* Serve timer interrupts first. */
  532. clock_comparator_work();
  533. /*
  534. * Get interrupt information from lowcore
  535. */
  536. tpi_info = (struct tpi_info *)&S390_lowcore.subchannel_id;
  537. irb = (struct irb *)&S390_lowcore.irb;
  538. do {
  539. kstat_cpu(smp_processor_id()).irqs[IO_INTERRUPT]++;
  540. if (tpi_info->adapter_IO) {
  541. do_adapter_IO(tpi_info->isc);
  542. continue;
  543. }
  544. sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
  545. if (!sch) {
  546. /* Clear pending interrupt condition. */
  547. kstat_cpu(smp_processor_id()).irqs[IOINT_CIO]++;
  548. tsch(tpi_info->schid, irb);
  549. continue;
  550. }
  551. spin_lock(sch->lock);
  552. /* Store interrupt response block to lowcore. */
  553. if (tsch(tpi_info->schid, irb) == 0) {
  554. /* Keep subchannel information word up to date. */
  555. memcpy (&sch->schib.scsw, &irb->scsw,
  556. sizeof (irb->scsw));
  557. /* Call interrupt handler if there is one. */
  558. if (sch->driver && sch->driver->irq)
  559. sch->driver->irq(sch);
  560. else
  561. kstat_cpu(smp_processor_id()).irqs[IOINT_CIO]++;
  562. } else
  563. kstat_cpu(smp_processor_id()).irqs[IOINT_CIO]++;
  564. spin_unlock(sch->lock);
  565. /*
  566. * Are more interrupts pending?
  567. * If so, the tpi instruction will update the lowcore
  568. * to hold the info for the next interrupt.
  569. * We don't do this for VM because a tpi drops the cpu
  570. * out of the sie which costs more cycles than it saves.
  571. */
  572. } while (MACHINE_IS_LPAR && tpi(NULL) != 0);
  573. irq_exit();
  574. set_irq_regs(old_regs);
  575. }
  576. #ifdef CONFIG_CCW_CONSOLE
  577. static struct subchannel console_subchannel;
  578. static struct io_subchannel_private console_priv;
  579. static int console_subchannel_in_use;
  580. /*
  581. * Use cio_tpi to get a pending interrupt and call the interrupt handler.
  582. * Return non-zero if an interrupt was processed, zero otherwise.
  583. */
  584. static int cio_tpi(void)
  585. {
  586. struct tpi_info *tpi_info;
  587. struct subchannel *sch;
  588. struct irb *irb;
  589. int irq_context;
  590. tpi_info = (struct tpi_info *)&S390_lowcore.subchannel_id;
  591. if (tpi(NULL) != 1)
  592. return 0;
  593. kstat_cpu(smp_processor_id()).irqs[IO_INTERRUPT]++;
  594. if (tpi_info->adapter_IO) {
  595. do_adapter_IO(tpi_info->isc);
  596. return 1;
  597. }
  598. irb = (struct irb *)&S390_lowcore.irb;
  599. /* Store interrupt response block to lowcore. */
  600. if (tsch(tpi_info->schid, irb) != 0) {
  601. /* Not status pending or not operational. */
  602. kstat_cpu(smp_processor_id()).irqs[IOINT_CIO]++;
  603. return 1;
  604. }
  605. sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
  606. if (!sch) {
  607. kstat_cpu(smp_processor_id()).irqs[IOINT_CIO]++;
  608. return 1;
  609. }
  610. irq_context = in_interrupt();
  611. if (!irq_context)
  612. local_bh_disable();
  613. irq_enter();
  614. spin_lock(sch->lock);
  615. memcpy(&sch->schib.scsw, &irb->scsw, sizeof(union scsw));
  616. if (sch->driver && sch->driver->irq)
  617. sch->driver->irq(sch);
  618. else
  619. kstat_cpu(smp_processor_id()).irqs[IOINT_CIO]++;
  620. spin_unlock(sch->lock);
  621. irq_exit();
  622. if (!irq_context)
  623. _local_bh_enable();
  624. return 1;
  625. }
  626. void *cio_get_console_priv(void)
  627. {
  628. return &console_priv;
  629. }
  630. /*
  631. * busy wait for the next interrupt on the console
  632. */
  633. void wait_cons_dev(void)
  634. __releases(console_subchannel.lock)
  635. __acquires(console_subchannel.lock)
  636. {
  637. unsigned long cr6 __attribute__ ((aligned (8)));
  638. unsigned long save_cr6 __attribute__ ((aligned (8)));
  639. /*
  640. * before entering the spinlock we may already have
  641. * processed the interrupt on a different CPU...
  642. */
  643. if (!console_subchannel_in_use)
  644. return;
  645. /* disable all but the console isc */
  646. __ctl_store (save_cr6, 6, 6);
  647. cr6 = 1UL << (31 - CONSOLE_ISC);
  648. __ctl_load (cr6, 6, 6);
  649. do {
  650. spin_unlock(console_subchannel.lock);
  651. if (!cio_tpi())
  652. cpu_relax();
  653. spin_lock(console_subchannel.lock);
  654. } while (console_subchannel.schib.scsw.cmd.actl != 0);
  655. /*
  656. * restore previous isc value
  657. */
  658. __ctl_load (save_cr6, 6, 6);
  659. }
  660. static int
  661. cio_test_for_console(struct subchannel_id schid, void *data)
  662. {
  663. if (stsch_err(schid, &console_subchannel.schib) != 0)
  664. return -ENXIO;
  665. if ((console_subchannel.schib.pmcw.st == SUBCHANNEL_TYPE_IO) &&
  666. console_subchannel.schib.pmcw.dnv &&
  667. (console_subchannel.schib.pmcw.dev == console_devno)) {
  668. console_irq = schid.sch_no;
  669. return 1; /* found */
  670. }
  671. return 0;
  672. }
  673. static int
  674. cio_get_console_sch_no(void)
  675. {
  676. struct subchannel_id schid;
  677. init_subchannel_id(&schid);
  678. if (console_irq != -1) {
  679. /* VM provided us with the irq number of the console. */
  680. schid.sch_no = console_irq;
  681. if (stsch_err(schid, &console_subchannel.schib) != 0 ||
  682. (console_subchannel.schib.pmcw.st != SUBCHANNEL_TYPE_IO) ||
  683. !console_subchannel.schib.pmcw.dnv)
  684. return -1;
  685. console_devno = console_subchannel.schib.pmcw.dev;
  686. } else if (console_devno != -1) {
  687. /* At least the console device number is known. */
  688. for_each_subchannel(cio_test_for_console, NULL);
  689. if (console_irq == -1)
  690. return -1;
  691. } else {
  692. /* unlike in 2.4, we cannot autoprobe here, since
  693. * the channel subsystem is not fully initialized.
  694. * With some luck, the HWC console can take over */
  695. return -1;
  696. }
  697. return console_irq;
  698. }
  699. struct subchannel *
  700. cio_probe_console(void)
  701. {
  702. int sch_no, ret;
  703. struct subchannel_id schid;
  704. if (xchg(&console_subchannel_in_use, 1) != 0)
  705. return ERR_PTR(-EBUSY);
  706. sch_no = cio_get_console_sch_no();
  707. if (sch_no == -1) {
  708. console_subchannel_in_use = 0;
  709. pr_warning("No CCW console was found\n");
  710. return ERR_PTR(-ENODEV);
  711. }
  712. memset(&console_subchannel, 0, sizeof(struct subchannel));
  713. init_subchannel_id(&schid);
  714. schid.sch_no = sch_no;
  715. ret = cio_validate_subchannel(&console_subchannel, schid);
  716. if (ret) {
  717. console_subchannel_in_use = 0;
  718. return ERR_PTR(-ENODEV);
  719. }
  720. /*
  721. * enable console I/O-interrupt subclass
  722. */
  723. isc_register(CONSOLE_ISC);
  724. console_subchannel.config.isc = CONSOLE_ISC;
  725. console_subchannel.config.intparm = (u32)(addr_t)&console_subchannel;
  726. ret = cio_commit_config(&console_subchannel);
  727. if (ret) {
  728. isc_unregister(CONSOLE_ISC);
  729. console_subchannel_in_use = 0;
  730. return ERR_PTR(ret);
  731. }
  732. return &console_subchannel;
  733. }
  734. void
  735. cio_release_console(void)
  736. {
  737. console_subchannel.config.intparm = 0;
  738. cio_commit_config(&console_subchannel);
  739. isc_unregister(CONSOLE_ISC);
  740. console_subchannel_in_use = 0;
  741. }
  742. /* Bah... hack to catch console special sausages. */
  743. int
  744. cio_is_console(struct subchannel_id schid)
  745. {
  746. if (!console_subchannel_in_use)
  747. return 0;
  748. return schid_equal(&schid, &console_subchannel.schid);
  749. }
  750. struct subchannel *
  751. cio_get_console_subchannel(void)
  752. {
  753. if (!console_subchannel_in_use)
  754. return NULL;
  755. return &console_subchannel;
  756. }
  757. #endif
  758. static int
  759. __disable_subchannel_easy(struct subchannel_id schid, struct schib *schib)
  760. {
  761. int retry, cc;
  762. cc = 0;
  763. for (retry=0;retry<3;retry++) {
  764. schib->pmcw.ena = 0;
  765. cc = msch_err(schid, schib);
  766. if (cc)
  767. return (cc==3?-ENODEV:-EBUSY);
  768. if (stsch_err(schid, schib) || !css_sch_is_valid(schib))
  769. return -ENODEV;
  770. if (!schib->pmcw.ena)
  771. return 0;
  772. }
  773. return -EBUSY; /* uhm... */
  774. }
  775. static int
  776. __clear_io_subchannel_easy(struct subchannel_id schid)
  777. {
  778. int retry;
  779. if (csch(schid))
  780. return -ENODEV;
  781. for (retry=0;retry<20;retry++) {
  782. struct tpi_info ti;
  783. if (tpi(&ti)) {
  784. tsch(ti.schid, (struct irb *)&S390_lowcore.irb);
  785. if (schid_equal(&ti.schid, &schid))
  786. return 0;
  787. }
  788. udelay_simple(100);
  789. }
  790. return -EBUSY;
  791. }
  792. static void __clear_chsc_subchannel_easy(void)
  793. {
  794. /* It seems we can only wait for a bit here :/ */
  795. udelay_simple(100);
  796. }
  797. static int pgm_check_occured;
  798. static void cio_reset_pgm_check_handler(void)
  799. {
  800. pgm_check_occured = 1;
  801. }
  802. static int stsch_reset(struct subchannel_id schid, struct schib *addr)
  803. {
  804. int rc;
  805. pgm_check_occured = 0;
  806. s390_base_pgm_handler_fn = cio_reset_pgm_check_handler;
  807. rc = stsch_err(schid, addr);
  808. s390_base_pgm_handler_fn = NULL;
  809. /* The program check handler could have changed pgm_check_occured. */
  810. barrier();
  811. if (pgm_check_occured)
  812. return -EIO;
  813. else
  814. return rc;
  815. }
  816. static int __shutdown_subchannel_easy(struct subchannel_id schid, void *data)
  817. {
  818. struct schib schib;
  819. if (stsch_reset(schid, &schib))
  820. return -ENXIO;
  821. if (!schib.pmcw.ena)
  822. return 0;
  823. switch(__disable_subchannel_easy(schid, &schib)) {
  824. case 0:
  825. case -ENODEV:
  826. break;
  827. default: /* -EBUSY */
  828. switch (schib.pmcw.st) {
  829. case SUBCHANNEL_TYPE_IO:
  830. if (__clear_io_subchannel_easy(schid))
  831. goto out; /* give up... */
  832. break;
  833. case SUBCHANNEL_TYPE_CHSC:
  834. __clear_chsc_subchannel_easy();
  835. break;
  836. default:
  837. /* No default clear strategy */
  838. break;
  839. }
  840. stsch_err(schid, &schib);
  841. __disable_subchannel_easy(schid, &schib);
  842. }
  843. out:
  844. return 0;
  845. }
  846. static atomic_t chpid_reset_count;
  847. static void s390_reset_chpids_mcck_handler(void)
  848. {
  849. struct crw crw;
  850. struct mci *mci;
  851. /* Check for pending channel report word. */
  852. mci = (struct mci *)&S390_lowcore.mcck_interruption_code;
  853. if (!mci->cp)
  854. return;
  855. /* Process channel report words. */
  856. while (stcrw(&crw) == 0) {
  857. /* Check for responses to RCHP. */
  858. if (crw.slct && crw.rsc == CRW_RSC_CPATH)
  859. atomic_dec(&chpid_reset_count);
  860. }
  861. }
  862. #define RCHP_TIMEOUT (30 * USEC_PER_SEC)
  863. static void css_reset(void)
  864. {
  865. int i, ret;
  866. unsigned long long timeout;
  867. struct chp_id chpid;
  868. /* Reset subchannels. */
  869. for_each_subchannel(__shutdown_subchannel_easy, NULL);
  870. /* Reset channel paths. */
  871. s390_base_mcck_handler_fn = s390_reset_chpids_mcck_handler;
  872. /* Enable channel report machine checks. */
  873. __ctl_set_bit(14, 28);
  874. /* Temporarily reenable machine checks. */
  875. local_mcck_enable();
  876. chp_id_init(&chpid);
  877. for (i = 0; i <= __MAX_CHPID; i++) {
  878. chpid.id = i;
  879. ret = rchp(chpid);
  880. if ((ret == 0) || (ret == 2))
  881. /*
  882. * rchp either succeeded, or another rchp is already
  883. * in progress. In either case, we'll get a crw.
  884. */
  885. atomic_inc(&chpid_reset_count);
  886. }
  887. /* Wait for machine check for all channel paths. */
  888. timeout = get_clock() + (RCHP_TIMEOUT << 12);
  889. while (atomic_read(&chpid_reset_count) != 0) {
  890. if (get_clock() > timeout)
  891. break;
  892. cpu_relax();
  893. }
  894. /* Disable machine checks again. */
  895. local_mcck_disable();
  896. /* Disable channel report machine checks. */
  897. __ctl_clear_bit(14, 28);
  898. s390_base_mcck_handler_fn = NULL;
  899. }
  900. static struct reset_call css_reset_call = {
  901. .fn = css_reset,
  902. };
  903. static int __init init_css_reset_call(void)
  904. {
  905. atomic_set(&chpid_reset_count, 0);
  906. register_reset_call(&css_reset_call);
  907. return 0;
  908. }
  909. arch_initcall(init_css_reset_call);
  910. struct sch_match_id {
  911. struct subchannel_id schid;
  912. struct ccw_dev_id devid;
  913. int rc;
  914. };
  915. static int __reipl_subchannel_match(struct subchannel_id schid, void *data)
  916. {
  917. struct schib schib;
  918. struct sch_match_id *match_id = data;
  919. if (stsch_reset(schid, &schib))
  920. return -ENXIO;
  921. if ((schib.pmcw.st == SUBCHANNEL_TYPE_IO) && schib.pmcw.dnv &&
  922. (schib.pmcw.dev == match_id->devid.devno) &&
  923. (schid.ssid == match_id->devid.ssid)) {
  924. match_id->schid = schid;
  925. match_id->rc = 0;
  926. return 1;
  927. }
  928. return 0;
  929. }
  930. static int reipl_find_schid(struct ccw_dev_id *devid,
  931. struct subchannel_id *schid)
  932. {
  933. struct sch_match_id match_id;
  934. match_id.devid = *devid;
  935. match_id.rc = -ENODEV;
  936. for_each_subchannel(__reipl_subchannel_match, &match_id);
  937. if (match_id.rc == 0)
  938. *schid = match_id.schid;
  939. return match_id.rc;
  940. }
  941. extern void do_reipl_asm(__u32 schid);
  942. /* Make sure all subchannels are quiet before we re-ipl an lpar. */
  943. void reipl_ccw_dev(struct ccw_dev_id *devid)
  944. {
  945. struct subchannel_id schid;
  946. s390_reset_system(NULL, NULL);
  947. if (reipl_find_schid(devid, &schid) != 0)
  948. panic("IPL Device not found\n");
  949. do_reipl_asm(*((__u32*)&schid));
  950. }
  951. int __init cio_get_iplinfo(struct cio_iplinfo *iplinfo)
  952. {
  953. struct subchannel_id schid;
  954. struct schib schib;
  955. schid = *(struct subchannel_id *)&S390_lowcore.subchannel_id;
  956. if (!schid.one)
  957. return -ENODEV;
  958. if (stsch_err(schid, &schib))
  959. return -ENODEV;
  960. if (schib.pmcw.st != SUBCHANNEL_TYPE_IO)
  961. return -ENODEV;
  962. if (!schib.pmcw.dnv)
  963. return -ENODEV;
  964. iplinfo->devno = schib.pmcw.dev;
  965. iplinfo->is_qdio = schib.pmcw.qf;
  966. return 0;
  967. }
  968. /**
  969. * cio_tm_start_key - perform start function
  970. * @sch: subchannel on which to perform the start function
  971. * @tcw: transport-command word to be started
  972. * @lpm: mask of paths to use
  973. * @key: storage key to use for storage access
  974. *
  975. * Start the tcw on the given subchannel. Return zero on success, non-zero
  976. * otherwise.
  977. */
  978. int cio_tm_start_key(struct subchannel *sch, struct tcw *tcw, u8 lpm, u8 key)
  979. {
  980. int cc;
  981. union orb *orb = &to_io_private(sch)->orb;
  982. memset(orb, 0, sizeof(union orb));
  983. orb->tm.intparm = (u32) (addr_t) sch;
  984. orb->tm.key = key >> 4;
  985. orb->tm.b = 1;
  986. orb->tm.lpm = lpm ? lpm : sch->lpm;
  987. orb->tm.tcw = (u32) (addr_t) tcw;
  988. cc = ssch(sch->schid, orb);
  989. switch (cc) {
  990. case 0:
  991. return 0;
  992. case 1:
  993. case 2:
  994. return -EBUSY;
  995. default:
  996. return cio_start_handle_notoper(sch, lpm);
  997. }
  998. }
  999. /**
  1000. * cio_tm_intrg - perform interrogate function
  1001. * @sch - subchannel on which to perform the interrogate function
  1002. *
  1003. * If the specified subchannel is running in transport-mode, perform the
  1004. * interrogate function. Return zero on success, non-zero otherwie.
  1005. */
  1006. int cio_tm_intrg(struct subchannel *sch)
  1007. {
  1008. int cc;
  1009. if (!to_io_private(sch)->orb.tm.b)
  1010. return -EINVAL;
  1011. cc = xsch(sch->schid);
  1012. switch (cc) {
  1013. case 0:
  1014. case 2:
  1015. return 0;
  1016. case 1:
  1017. return -EBUSY;
  1018. default:
  1019. return -ENODEV;
  1020. }
  1021. }