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