cio.c 23 KB

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