cio.c 26 KB

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