cio.c 24 KB

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