cio.c 26 KB

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