cio.c 27 KB

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