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