cio.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  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. tsch(tpi_info->schid, irb);
  548. continue;
  549. }
  550. spin_lock(sch->lock);
  551. /* Store interrupt response block to lowcore. */
  552. if (tsch(tpi_info->schid, irb) == 0) {
  553. /* Keep subchannel information word up to date. */
  554. memcpy (&sch->schib.scsw, &irb->scsw,
  555. sizeof (irb->scsw));
  556. /* Call interrupt handler if there is one. */
  557. if (sch->driver && sch->driver->irq)
  558. sch->driver->irq(sch);
  559. }
  560. spin_unlock(sch->lock);
  561. /*
  562. * Are more interrupts pending?
  563. * If so, the tpi instruction will update the lowcore
  564. * to hold the info for the next interrupt.
  565. * We don't do this for VM because a tpi drops the cpu
  566. * out of the sie which costs more cycles than it saves.
  567. */
  568. } while (MACHINE_IS_LPAR && tpi(NULL) != 0);
  569. irq_exit();
  570. set_irq_regs(old_regs);
  571. }
  572. #ifdef CONFIG_CCW_CONSOLE
  573. static struct subchannel console_subchannel;
  574. static struct io_subchannel_private console_priv;
  575. static int console_subchannel_in_use;
  576. /*
  577. * Use tpi to get a pending interrupt, call the interrupt handler and
  578. * return a pointer to the subchannel structure.
  579. */
  580. static int cio_tpi(void)
  581. {
  582. struct tpi_info *tpi_info;
  583. struct subchannel *sch;
  584. struct irb *irb;
  585. int irq_context;
  586. tpi_info = (struct tpi_info *)&S390_lowcore.subchannel_id;
  587. if (tpi(NULL) != 1)
  588. return 0;
  589. irb = (struct irb *)&S390_lowcore.irb;
  590. /* Store interrupt response block to lowcore. */
  591. if (tsch(tpi_info->schid, irb) != 0)
  592. /* Not status pending or not operational. */
  593. return 1;
  594. sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
  595. if (!sch)
  596. return 1;
  597. irq_context = in_interrupt();
  598. if (!irq_context)
  599. local_bh_disable();
  600. irq_enter();
  601. spin_lock(sch->lock);
  602. memcpy(&sch->schib.scsw, &irb->scsw, sizeof(union scsw));
  603. if (sch->driver && sch->driver->irq)
  604. sch->driver->irq(sch);
  605. spin_unlock(sch->lock);
  606. irq_exit();
  607. if (!irq_context)
  608. _local_bh_enable();
  609. return 1;
  610. }
  611. void *cio_get_console_priv(void)
  612. {
  613. return &console_priv;
  614. }
  615. /*
  616. * busy wait for the next interrupt on the console
  617. */
  618. void wait_cons_dev(void)
  619. __releases(console_subchannel.lock)
  620. __acquires(console_subchannel.lock)
  621. {
  622. unsigned long cr6 __attribute__ ((aligned (8)));
  623. unsigned long save_cr6 __attribute__ ((aligned (8)));
  624. /*
  625. * before entering the spinlock we may already have
  626. * processed the interrupt on a different CPU...
  627. */
  628. if (!console_subchannel_in_use)
  629. return;
  630. /* disable all but the console isc */
  631. __ctl_store (save_cr6, 6, 6);
  632. cr6 = 1UL << (31 - CONSOLE_ISC);
  633. __ctl_load (cr6, 6, 6);
  634. do {
  635. spin_unlock(console_subchannel.lock);
  636. if (!cio_tpi())
  637. cpu_relax();
  638. spin_lock(console_subchannel.lock);
  639. } while (console_subchannel.schib.scsw.cmd.actl != 0);
  640. /*
  641. * restore previous isc value
  642. */
  643. __ctl_load (save_cr6, 6, 6);
  644. }
  645. static int
  646. cio_test_for_console(struct subchannel_id schid, void *data)
  647. {
  648. if (stsch_err(schid, &console_subchannel.schib) != 0)
  649. return -ENXIO;
  650. if ((console_subchannel.schib.pmcw.st == SUBCHANNEL_TYPE_IO) &&
  651. console_subchannel.schib.pmcw.dnv &&
  652. (console_subchannel.schib.pmcw.dev == console_devno)) {
  653. console_irq = schid.sch_no;
  654. return 1; /* found */
  655. }
  656. return 0;
  657. }
  658. static int
  659. cio_get_console_sch_no(void)
  660. {
  661. struct subchannel_id schid;
  662. init_subchannel_id(&schid);
  663. if (console_irq != -1) {
  664. /* VM provided us with the irq number of the console. */
  665. schid.sch_no = console_irq;
  666. if (stsch_err(schid, &console_subchannel.schib) != 0 ||
  667. (console_subchannel.schib.pmcw.st != SUBCHANNEL_TYPE_IO) ||
  668. !console_subchannel.schib.pmcw.dnv)
  669. return -1;
  670. console_devno = console_subchannel.schib.pmcw.dev;
  671. } else if (console_devno != -1) {
  672. /* At least the console device number is known. */
  673. for_each_subchannel(cio_test_for_console, NULL);
  674. if (console_irq == -1)
  675. return -1;
  676. } else {
  677. /* unlike in 2.4, we cannot autoprobe here, since
  678. * the channel subsystem is not fully initialized.
  679. * With some luck, the HWC console can take over */
  680. return -1;
  681. }
  682. return console_irq;
  683. }
  684. struct subchannel *
  685. cio_probe_console(void)
  686. {
  687. int sch_no, ret;
  688. struct subchannel_id schid;
  689. if (xchg(&console_subchannel_in_use, 1) != 0)
  690. return ERR_PTR(-EBUSY);
  691. sch_no = cio_get_console_sch_no();
  692. if (sch_no == -1) {
  693. console_subchannel_in_use = 0;
  694. pr_warning("No CCW console was found\n");
  695. return ERR_PTR(-ENODEV);
  696. }
  697. memset(&console_subchannel, 0, sizeof(struct subchannel));
  698. init_subchannel_id(&schid);
  699. schid.sch_no = sch_no;
  700. ret = cio_validate_subchannel(&console_subchannel, schid);
  701. if (ret) {
  702. console_subchannel_in_use = 0;
  703. return ERR_PTR(-ENODEV);
  704. }
  705. /*
  706. * enable console I/O-interrupt subclass
  707. */
  708. isc_register(CONSOLE_ISC);
  709. console_subchannel.config.isc = CONSOLE_ISC;
  710. console_subchannel.config.intparm = (u32)(addr_t)&console_subchannel;
  711. ret = cio_commit_config(&console_subchannel);
  712. if (ret) {
  713. isc_unregister(CONSOLE_ISC);
  714. console_subchannel_in_use = 0;
  715. return ERR_PTR(ret);
  716. }
  717. return &console_subchannel;
  718. }
  719. void
  720. cio_release_console(void)
  721. {
  722. console_subchannel.config.intparm = 0;
  723. cio_commit_config(&console_subchannel);
  724. isc_unregister(CONSOLE_ISC);
  725. console_subchannel_in_use = 0;
  726. }
  727. /* Bah... hack to catch console special sausages. */
  728. int
  729. cio_is_console(struct subchannel_id schid)
  730. {
  731. if (!console_subchannel_in_use)
  732. return 0;
  733. return schid_equal(&schid, &console_subchannel.schid);
  734. }
  735. struct subchannel *
  736. cio_get_console_subchannel(void)
  737. {
  738. if (!console_subchannel_in_use)
  739. return NULL;
  740. return &console_subchannel;
  741. }
  742. #endif
  743. static int
  744. __disable_subchannel_easy(struct subchannel_id schid, struct schib *schib)
  745. {
  746. int retry, cc;
  747. cc = 0;
  748. for (retry=0;retry<3;retry++) {
  749. schib->pmcw.ena = 0;
  750. cc = msch_err(schid, schib);
  751. if (cc)
  752. return (cc==3?-ENODEV:-EBUSY);
  753. if (stsch_err(schid, schib) || !css_sch_is_valid(schib))
  754. return -ENODEV;
  755. if (!schib->pmcw.ena)
  756. return 0;
  757. }
  758. return -EBUSY; /* uhm... */
  759. }
  760. static int
  761. __clear_io_subchannel_easy(struct subchannel_id schid)
  762. {
  763. int retry;
  764. if (csch(schid))
  765. return -ENODEV;
  766. for (retry=0;retry<20;retry++) {
  767. struct tpi_info ti;
  768. if (tpi(&ti)) {
  769. tsch(ti.schid, (struct irb *)&S390_lowcore.irb);
  770. if (schid_equal(&ti.schid, &schid))
  771. return 0;
  772. }
  773. udelay_simple(100);
  774. }
  775. return -EBUSY;
  776. }
  777. static void __clear_chsc_subchannel_easy(void)
  778. {
  779. /* It seems we can only wait for a bit here :/ */
  780. udelay_simple(100);
  781. }
  782. static int pgm_check_occured;
  783. static void cio_reset_pgm_check_handler(void)
  784. {
  785. pgm_check_occured = 1;
  786. }
  787. static int stsch_reset(struct subchannel_id schid, struct schib *addr)
  788. {
  789. int rc;
  790. pgm_check_occured = 0;
  791. s390_base_pgm_handler_fn = cio_reset_pgm_check_handler;
  792. rc = stsch_err(schid, addr);
  793. s390_base_pgm_handler_fn = NULL;
  794. /* The program check handler could have changed pgm_check_occured. */
  795. barrier();
  796. if (pgm_check_occured)
  797. return -EIO;
  798. else
  799. return rc;
  800. }
  801. static int __shutdown_subchannel_easy(struct subchannel_id schid, void *data)
  802. {
  803. struct schib schib;
  804. if (stsch_reset(schid, &schib))
  805. return -ENXIO;
  806. if (!schib.pmcw.ena)
  807. return 0;
  808. switch(__disable_subchannel_easy(schid, &schib)) {
  809. case 0:
  810. case -ENODEV:
  811. break;
  812. default: /* -EBUSY */
  813. switch (schib.pmcw.st) {
  814. case SUBCHANNEL_TYPE_IO:
  815. if (__clear_io_subchannel_easy(schid))
  816. goto out; /* give up... */
  817. break;
  818. case SUBCHANNEL_TYPE_CHSC:
  819. __clear_chsc_subchannel_easy();
  820. break;
  821. default:
  822. /* No default clear strategy */
  823. break;
  824. }
  825. stsch_err(schid, &schib);
  826. __disable_subchannel_easy(schid, &schib);
  827. }
  828. out:
  829. return 0;
  830. }
  831. static atomic_t chpid_reset_count;
  832. static void s390_reset_chpids_mcck_handler(void)
  833. {
  834. struct crw crw;
  835. struct mci *mci;
  836. /* Check for pending channel report word. */
  837. mci = (struct mci *)&S390_lowcore.mcck_interruption_code;
  838. if (!mci->cp)
  839. return;
  840. /* Process channel report words. */
  841. while (stcrw(&crw) == 0) {
  842. /* Check for responses to RCHP. */
  843. if (crw.slct && crw.rsc == CRW_RSC_CPATH)
  844. atomic_dec(&chpid_reset_count);
  845. }
  846. }
  847. #define RCHP_TIMEOUT (30 * USEC_PER_SEC)
  848. static void css_reset(void)
  849. {
  850. int i, ret;
  851. unsigned long long timeout;
  852. struct chp_id chpid;
  853. /* Reset subchannels. */
  854. for_each_subchannel(__shutdown_subchannel_easy, NULL);
  855. /* Reset channel paths. */
  856. s390_base_mcck_handler_fn = s390_reset_chpids_mcck_handler;
  857. /* Enable channel report machine checks. */
  858. __ctl_set_bit(14, 28);
  859. /* Temporarily reenable machine checks. */
  860. local_mcck_enable();
  861. chp_id_init(&chpid);
  862. for (i = 0; i <= __MAX_CHPID; i++) {
  863. chpid.id = i;
  864. ret = rchp(chpid);
  865. if ((ret == 0) || (ret == 2))
  866. /*
  867. * rchp either succeeded, or another rchp is already
  868. * in progress. In either case, we'll get a crw.
  869. */
  870. atomic_inc(&chpid_reset_count);
  871. }
  872. /* Wait for machine check for all channel paths. */
  873. timeout = get_clock() + (RCHP_TIMEOUT << 12);
  874. while (atomic_read(&chpid_reset_count) != 0) {
  875. if (get_clock() > timeout)
  876. break;
  877. cpu_relax();
  878. }
  879. /* Disable machine checks again. */
  880. local_mcck_disable();
  881. /* Disable channel report machine checks. */
  882. __ctl_clear_bit(14, 28);
  883. s390_base_mcck_handler_fn = NULL;
  884. }
  885. static struct reset_call css_reset_call = {
  886. .fn = css_reset,
  887. };
  888. static int __init init_css_reset_call(void)
  889. {
  890. atomic_set(&chpid_reset_count, 0);
  891. register_reset_call(&css_reset_call);
  892. return 0;
  893. }
  894. arch_initcall(init_css_reset_call);
  895. struct sch_match_id {
  896. struct subchannel_id schid;
  897. struct ccw_dev_id devid;
  898. int rc;
  899. };
  900. static int __reipl_subchannel_match(struct subchannel_id schid, void *data)
  901. {
  902. struct schib schib;
  903. struct sch_match_id *match_id = data;
  904. if (stsch_reset(schid, &schib))
  905. return -ENXIO;
  906. if ((schib.pmcw.st == SUBCHANNEL_TYPE_IO) && schib.pmcw.dnv &&
  907. (schib.pmcw.dev == match_id->devid.devno) &&
  908. (schid.ssid == match_id->devid.ssid)) {
  909. match_id->schid = schid;
  910. match_id->rc = 0;
  911. return 1;
  912. }
  913. return 0;
  914. }
  915. static int reipl_find_schid(struct ccw_dev_id *devid,
  916. struct subchannel_id *schid)
  917. {
  918. struct sch_match_id match_id;
  919. match_id.devid = *devid;
  920. match_id.rc = -ENODEV;
  921. for_each_subchannel(__reipl_subchannel_match, &match_id);
  922. if (match_id.rc == 0)
  923. *schid = match_id.schid;
  924. return match_id.rc;
  925. }
  926. extern void do_reipl_asm(__u32 schid);
  927. /* Make sure all subchannels are quiet before we re-ipl an lpar. */
  928. void reipl_ccw_dev(struct ccw_dev_id *devid)
  929. {
  930. struct subchannel_id schid;
  931. s390_reset_system();
  932. if (reipl_find_schid(devid, &schid) != 0)
  933. panic("IPL Device not found\n");
  934. do_reipl_asm(*((__u32*)&schid));
  935. }
  936. int __init cio_get_iplinfo(struct cio_iplinfo *iplinfo)
  937. {
  938. struct subchannel_id schid;
  939. struct schib schib;
  940. schid = *(struct subchannel_id *)&S390_lowcore.subchannel_id;
  941. if (!schid.one)
  942. return -ENODEV;
  943. if (stsch_err(schid, &schib))
  944. return -ENODEV;
  945. if (schib.pmcw.st != SUBCHANNEL_TYPE_IO)
  946. return -ENODEV;
  947. if (!schib.pmcw.dnv)
  948. return -ENODEV;
  949. iplinfo->devno = schib.pmcw.dev;
  950. iplinfo->is_qdio = schib.pmcw.qf;
  951. return 0;
  952. }
  953. /**
  954. * cio_tm_start_key - perform start function
  955. * @sch: subchannel on which to perform the start function
  956. * @tcw: transport-command word to be started
  957. * @lpm: mask of paths to use
  958. * @key: storage key to use for storage access
  959. *
  960. * Start the tcw on the given subchannel. Return zero on success, non-zero
  961. * otherwise.
  962. */
  963. int cio_tm_start_key(struct subchannel *sch, struct tcw *tcw, u8 lpm, u8 key)
  964. {
  965. int cc;
  966. union orb *orb = &to_io_private(sch)->orb;
  967. memset(orb, 0, sizeof(union orb));
  968. orb->tm.intparm = (u32) (addr_t) sch;
  969. orb->tm.key = key >> 4;
  970. orb->tm.b = 1;
  971. orb->tm.lpm = lpm ? lpm : sch->lpm;
  972. orb->tm.tcw = (u32) (addr_t) tcw;
  973. cc = ssch(sch->schid, orb);
  974. switch (cc) {
  975. case 0:
  976. return 0;
  977. case 1:
  978. case 2:
  979. return -EBUSY;
  980. default:
  981. return cio_start_handle_notoper(sch, lpm);
  982. }
  983. }
  984. /**
  985. * cio_tm_intrg - perform interrogate function
  986. * @sch - subchannel on which to perform the interrogate function
  987. *
  988. * If the specified subchannel is running in transport-mode, perform the
  989. * interrogate function. Return zero on success, non-zero otherwie.
  990. */
  991. int cio_tm_intrg(struct subchannel *sch)
  992. {
  993. int cc;
  994. if (!to_io_private(sch)->orb.tm.b)
  995. return -EINVAL;
  996. cc = xsch(sch->schid);
  997. switch (cc) {
  998. case 0:
  999. case 2:
  1000. return 0;
  1001. case 1:
  1002. return -EBUSY;
  1003. default:
  1004. return -ENODEV;
  1005. }
  1006. }