cio.c 26 KB

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