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