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