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