cio.c 26 KB

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