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