blob: e8d331493fd812c594696f0fcad8bc9ac62cf3f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/cio/cio.c
3 * S/390 common I/O routines -- low level i/o calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Copyright (C) 1999-2002 IBM Deutschland Entwicklung GmbH,
6 * IBM Corporation
7 * Author(s): Ingo Adlung (adlung@de.ibm.com)
Cornelia Huck4ce3b302006-01-14 13:21:04 -08008 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Arnd Bergmann (arndb@de.ibm.com)
10 * Martin Schwidefsky (schwidefsky@de.ibm.com)
11 */
12
13#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/init.h>
15#include <linux/slab.h>
16#include <linux/device.h>
17#include <linux/kernel_stat.h>
18#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/cio.h>
20#include <asm/delay.h>
21#include <asm/irq.h>
Heiko Carstens5a489b92006-10-06 16:38:35 +020022#include <asm/irq_regs.h>
Heiko Carstense87bfe52006-09-20 15:59:15 +020023#include <asm/setup.h>
Heiko Carstens15e9b582006-12-04 15:40:26 +010024#include <asm/reset.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "airq.h"
26#include "cio.h"
27#include "css.h"
28#include "chsc.h"
29#include "ioasm.h"
30#include "blacklist.h"
31#include "cio_debug.h"
Heiko Carstens15e9b582006-12-04 15:40:26 +010032#include "../s390mach.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34debug_info_t *cio_debug_msg_id;
35debug_info_t *cio_debug_trace_id;
36debug_info_t *cio_debug_crw_id;
37
38int cio_show_msg;
39
40static int __init
41cio_setup (char *parm)
42{
43 if (!strcmp (parm, "yes"))
44 cio_show_msg = 1;
45 else if (!strcmp (parm, "no"))
46 cio_show_msg = 0;
47 else
48 printk (KERN_ERR "cio_setup : invalid cio_msg parameter '%s'",
49 parm);
50 return 1;
51}
52
53__setup ("cio_msg=", cio_setup);
54
55/*
56 * Function: cio_debug_init
57 * Initializes three debug logs (under /proc/s390dbf) for common I/O:
58 * - cio_msg logs the messages which are printk'ed when CONFIG_DEBUG_IO is on
59 * - cio_trace logs the calling of different functions
60 * - cio_crw logs the messages which are printk'ed when CONFIG_DEBUG_CRW is on
61 * debug levels depend on CONFIG_DEBUG_IO resp. CONFIG_DEBUG_CRW
62 */
63static int __init
64cio_debug_init (void)
65{
Michael Holzheu66a464d2005-06-25 14:55:33 -070066 cio_debug_msg_id = debug_register ("cio_msg", 16, 4, 16*sizeof (long));
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 if (!cio_debug_msg_id)
68 goto out_unregister;
69 debug_register_view (cio_debug_msg_id, &debug_sprintf_view);
70 debug_set_level (cio_debug_msg_id, 2);
Peter Oberparleiter06fbcb12006-04-10 22:53:46 -070071 cio_debug_trace_id = debug_register ("cio_trace", 16, 4, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (!cio_debug_trace_id)
73 goto out_unregister;
74 debug_register_view (cio_debug_trace_id, &debug_hex_ascii_view);
75 debug_set_level (cio_debug_trace_id, 2);
Michael Holzheu66a464d2005-06-25 14:55:33 -070076 cio_debug_crw_id = debug_register ("cio_crw", 4, 4, 16*sizeof (long));
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (!cio_debug_crw_id)
78 goto out_unregister;
79 debug_register_view (cio_debug_crw_id, &debug_sprintf_view);
80 debug_set_level (cio_debug_crw_id, 2);
81 pr_debug("debugging initialized\n");
82 return 0;
83
84out_unregister:
85 if (cio_debug_msg_id)
86 debug_unregister (cio_debug_msg_id);
87 if (cio_debug_trace_id)
88 debug_unregister (cio_debug_trace_id);
89 if (cio_debug_crw_id)
90 debug_unregister (cio_debug_crw_id);
91 pr_debug("could not initialize debugging\n");
92 return -1;
93}
94
95arch_initcall (cio_debug_init);
96
97int
98cio_set_options (struct subchannel *sch, int flags)
99{
100 sch->options.suspend = (flags & DOIO_ALLOW_SUSPEND) != 0;
101 sch->options.prefetch = (flags & DOIO_DENY_PREFETCH) != 0;
102 sch->options.inter = (flags & DOIO_SUPPRESS_INTER) != 0;
103 return 0;
104}
105
106/* FIXME: who wants to use this? */
107int
108cio_get_options (struct subchannel *sch)
109{
110 int flags;
111
112 flags = 0;
113 if (sch->options.suspend)
114 flags |= DOIO_ALLOW_SUSPEND;
115 if (sch->options.prefetch)
116 flags |= DOIO_DENY_PREFETCH;
117 if (sch->options.inter)
118 flags |= DOIO_SUPPRESS_INTER;
119 return flags;
120}
121
122/*
123 * Use tpi to get a pending interrupt, call the interrupt handler and
124 * return a pointer to the subchannel structure.
125 */
126static inline int
127cio_tpi(void)
128{
129 struct tpi_info *tpi_info;
130 struct subchannel *sch;
131 struct irb *irb;
132
133 tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
134 if (tpi (NULL) != 1)
135 return 0;
136 irb = (struct irb *) __LC_IRB;
137 /* Store interrupt response block to lowcore. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800138 if (tsch (tpi_info->schid, irb) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 /* Not status pending or not operational. */
140 return 1;
141 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
142 if (!sch)
143 return 1;
144 local_bh_disable();
145 irq_enter ();
Cornelia Huck2ec22982006-12-08 15:54:26 +0100146 spin_lock(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 memcpy (&sch->schib.scsw, &irb->scsw, sizeof (struct scsw));
148 if (sch->driver && sch->driver->irq)
149 sch->driver->irq(&sch->dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100150 spin_unlock(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 irq_exit ();
Heiko Carstens1f194a42006-07-03 00:24:46 -0700152 _local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return 1;
154}
155
156static inline int
157cio_start_handle_notoper(struct subchannel *sch, __u8 lpm)
158{
159 char dbf_text[15];
160
161 if (lpm != 0)
162 sch->lpm &= ~lpm;
163 else
164 sch->lpm = 0;
165
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800166 stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 CIO_MSG_EVENT(0, "cio_start: 'not oper' status for "
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800169 "subchannel 0.%x.%04x!\n", sch->schid.ssid,
170 sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 sprintf(dbf_text, "no%s", sch->dev.bus_id);
172 CIO_TRACE_EVENT(0, dbf_text);
173 CIO_HEX_EVENT(0, &sch->schib, sizeof (struct schib));
174
175 return (sch->lpm ? -EACCES : -ENODEV);
176}
177
178int
179cio_start_key (struct subchannel *sch, /* subchannel structure */
180 struct ccw1 * cpa, /* logical channel prog addr */
181 __u8 lpm, /* logical path mask */
182 __u8 key) /* storage key */
183{
184 char dbf_txt[15];
185 int ccode;
186
187 CIO_TRACE_EVENT (4, "stIO");
188 CIO_TRACE_EVENT (4, sch->dev.bus_id);
189
190 /* sch is always under 2G. */
191 sch->orb.intparm = (__u32)(unsigned long)sch;
192 sch->orb.fmt = 1;
193
194 sch->orb.pfch = sch->options.prefetch == 0;
195 sch->orb.spnd = sch->options.suspend;
196 sch->orb.ssic = sch->options.suspend && sch->options.inter;
Peter Oberparleitere0e32c82006-09-20 15:59:57 +0200197 sch->orb.lpm = (lpm != 0) ? lpm : sch->lpm;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800198#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /*
200 * for 64 bit we always support 64 bit IDAWs with 4k page size only
201 */
202 sch->orb.c64 = 1;
203 sch->orb.i2k = 0;
204#endif
205 sch->orb.key = key >> 4;
206 /* issue "Start Subchannel" */
207 sch->orb.cpa = (__u32) __pa (cpa);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800208 ccode = ssch (sch->schid, &sch->orb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 /* process condition code */
211 sprintf (dbf_txt, "ccode:%d", ccode);
212 CIO_TRACE_EVENT (4, dbf_txt);
213
214 switch (ccode) {
215 case 0:
216 /*
217 * initialize device status information
218 */
219 sch->schib.scsw.actl |= SCSW_ACTL_START_PEND;
220 return 0;
221 case 1: /* status pending */
222 case 2: /* busy */
223 return -EBUSY;
224 default: /* device/path not operational */
225 return cio_start_handle_notoper(sch, lpm);
226 }
227}
228
229int
230cio_start (struct subchannel *sch, struct ccw1 *cpa, __u8 lpm)
231{
Peter Oberparleiter0b642ed2005-05-01 08:58:58 -0700232 return cio_start_key(sch, cpa, lpm, PAGE_DEFAULT_KEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235/*
236 * resume suspended I/O operation
237 */
238int
239cio_resume (struct subchannel *sch)
240{
241 char dbf_txt[15];
242 int ccode;
243
244 CIO_TRACE_EVENT (4, "resIO");
245 CIO_TRACE_EVENT (4, sch->dev.bus_id);
246
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800247 ccode = rsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 sprintf (dbf_txt, "ccode:%d", ccode);
250 CIO_TRACE_EVENT (4, dbf_txt);
251
252 switch (ccode) {
253 case 0:
254 sch->schib.scsw.actl |= SCSW_ACTL_RESUME_PEND;
255 return 0;
256 case 1:
257 return -EBUSY;
258 case 2:
259 return -EINVAL;
260 default:
261 /*
262 * useless to wait for request completion
263 * as device is no longer operational !
264 */
265 return -ENODEV;
266 }
267}
268
269/*
270 * halt I/O operation
271 */
272int
273cio_halt(struct subchannel *sch)
274{
275 char dbf_txt[15];
276 int ccode;
277
278 if (!sch)
279 return -ENODEV;
280
281 CIO_TRACE_EVENT (2, "haltIO");
282 CIO_TRACE_EVENT (2, sch->dev.bus_id);
283
284 /*
285 * Issue "Halt subchannel" and process condition code
286 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800287 ccode = hsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 sprintf (dbf_txt, "ccode:%d", ccode);
290 CIO_TRACE_EVENT (2, dbf_txt);
291
292 switch (ccode) {
293 case 0:
294 sch->schib.scsw.actl |= SCSW_ACTL_HALT_PEND;
295 return 0;
296 case 1: /* status pending */
297 case 2: /* busy */
298 return -EBUSY;
299 default: /* device not operational */
300 return -ENODEV;
301 }
302}
303
304/*
305 * Clear I/O operation
306 */
307int
308cio_clear(struct subchannel *sch)
309{
310 char dbf_txt[15];
311 int ccode;
312
313 if (!sch)
314 return -ENODEV;
315
316 CIO_TRACE_EVENT (2, "clearIO");
317 CIO_TRACE_EVENT (2, sch->dev.bus_id);
318
319 /*
320 * Issue "Clear subchannel" and process condition code
321 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800322 ccode = csch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 sprintf (dbf_txt, "ccode:%d", ccode);
325 CIO_TRACE_EVENT (2, dbf_txt);
326
327 switch (ccode) {
328 case 0:
329 sch->schib.scsw.actl |= SCSW_ACTL_CLEAR_PEND;
330 return 0;
331 default: /* device not operational */
332 return -ENODEV;
333 }
334}
335
336/*
337 * Function: cio_cancel
338 * Issues a "Cancel Subchannel" on the specified subchannel
339 * Note: We don't need any fancy intparms and flags here
340 * since xsch is executed synchronously.
341 * Only for common I/O internal use as for now.
342 */
343int
344cio_cancel (struct subchannel *sch)
345{
346 char dbf_txt[15];
347 int ccode;
348
349 if (!sch)
350 return -ENODEV;
351
352 CIO_TRACE_EVENT (2, "cancelIO");
353 CIO_TRACE_EVENT (2, sch->dev.bus_id);
354
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800355 ccode = xsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 sprintf (dbf_txt, "ccode:%d", ccode);
358 CIO_TRACE_EVENT (2, dbf_txt);
359
360 switch (ccode) {
361 case 0: /* success */
362 /* Update information in scsw. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800363 stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return 0;
365 case 1: /* status pending */
366 return -EBUSY;
367 case 2: /* not applicable */
368 return -EINVAL;
369 default: /* not oper */
370 return -ENODEV;
371 }
372}
373
374/*
375 * Function: cio_modify
376 * Issues a "Modify Subchannel" on the specified subchannel
377 */
378int
379cio_modify (struct subchannel *sch)
380{
381 int ccode, retry, ret;
382
383 ret = 0;
384 for (retry = 0; retry < 5; retry++) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800385 ccode = msch_err (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (ccode < 0) /* -EIO if msch gets a program check. */
387 return ccode;
388 switch (ccode) {
389 case 0: /* successfull */
390 return 0;
391 case 1: /* status pending */
392 return -EBUSY;
393 case 2: /* busy */
394 udelay (100); /* allow for recovery */
395 ret = -EBUSY;
396 break;
397 case 3: /* not operational */
398 return -ENODEV;
399 }
400 }
401 return ret;
402}
403
404/*
405 * Enable subchannel.
406 */
407int
408cio_enable_subchannel (struct subchannel *sch, unsigned int isc)
409{
410 char dbf_txt[15];
411 int ccode;
412 int retry;
413 int ret;
414
415 CIO_TRACE_EVENT (2, "ensch");
416 CIO_TRACE_EVENT (2, sch->dev.bus_id);
417
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800418 ccode = stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (ccode)
420 return -ENODEV;
421
422 for (retry = 5, ret = 0; retry > 0; retry--) {
423 sch->schib.pmcw.ena = 1;
424 sch->schib.pmcw.isc = isc;
425 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
426 ret = cio_modify(sch);
427 if (ret == -ENODEV)
428 break;
429 if (ret == -EIO)
430 /*
431 * Got a program check in cio_modify. Try without
432 * the concurrent sense bit the next time.
433 */
434 sch->schib.pmcw.csense = 0;
435 if (ret == 0) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800436 stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (sch->schib.pmcw.ena)
438 break;
439 }
440 if (ret == -EBUSY) {
441 struct irb irb;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800442 if (tsch(sch->schid, &irb) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 break;
444 }
445 }
446 sprintf (dbf_txt, "ret:%d", ret);
447 CIO_TRACE_EVENT (2, dbf_txt);
448 return ret;
449}
450
451/*
452 * Disable subchannel.
453 */
454int
455cio_disable_subchannel (struct subchannel *sch)
456{
457 char dbf_txt[15];
458 int ccode;
459 int retry;
460 int ret;
461
462 CIO_TRACE_EVENT (2, "dissch");
463 CIO_TRACE_EVENT (2, sch->dev.bus_id);
464
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800465 ccode = stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (ccode == 3) /* Not operational. */
467 return -ENODEV;
468
469 if (sch->schib.scsw.actl != 0)
470 /*
471 * the disable function must not be called while there are
472 * requests pending for completion !
473 */
474 return -EBUSY;
475
476 for (retry = 5, ret = 0; retry > 0; retry--) {
477 sch->schib.pmcw.ena = 0;
478 ret = cio_modify(sch);
479 if (ret == -ENODEV)
480 break;
481 if (ret == -EBUSY)
482 /*
483 * The subchannel is busy or status pending.
484 * We'll disable when the next interrupt was delivered
485 * via the state machine.
486 */
487 break;
488 if (ret == 0) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800489 stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (!sch->schib.pmcw.ena)
491 break;
492 }
493 }
494 sprintf (dbf_txt, "ret:%d", ret);
495 CIO_TRACE_EVENT (2, dbf_txt);
496 return ret;
497}
498
Cornelia Huck2ec22982006-12-08 15:54:26 +0100499static int cio_create_sch_lock(struct subchannel *sch)
500{
501 sch->lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
502 if (!sch->lock)
503 return -ENOMEM;
504 spin_lock_init(sch->lock);
505 return 0;
506}
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508/*
509 * cio_validate_subchannel()
510 *
511 * Find out subchannel type and initialize struct subchannel.
512 * Return codes:
513 * SUBCHANNEL_TYPE_IO for a normal io subchannel
514 * SUBCHANNEL_TYPE_CHSC for a chsc subchannel
515 * SUBCHANNEL_TYPE_MESSAGE for a messaging subchannel
516 * SUBCHANNEL_TYPE_ADM for a adm(?) subchannel
517 * -ENXIO for non-defined subchannels
518 * -ENODEV for subchannels with invalid device number or blacklisted devices
519 */
520int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800521cio_validate_subchannel (struct subchannel *sch, struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 char dbf_txt[15];
524 int ccode;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100525 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800527 sprintf (dbf_txt, "valsch%x", schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 CIO_TRACE_EVENT (4, dbf_txt);
529
530 /* Nuke all fields. */
531 memset(sch, 0, sizeof(struct subchannel));
532
Cornelia Huck2ec22982006-12-08 15:54:26 +0100533 sch->schid = schid;
534 if (cio_is_console(schid)) {
535 sch->lock = cio_get_console_lock();
536 } else {
537 err = cio_create_sch_lock(sch);
538 if (err)
539 goto out;
540 }
Cornelia Huck6ab48792006-07-12 16:39:50 +0200541 mutex_init(&sch->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 /* Set a name for the subchannel */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800543 snprintf (sch->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x", schid.ssid,
544 schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 /*
547 * The first subchannel that is not-operational (ccode==3)
548 * indicates that there aren't any more devices available.
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800549 * If stsch gets an exception, it means the current subchannel set
550 * is not valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800552 ccode = stsch_err (schid, &sch->schib);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100553 if (ccode) {
554 err = (ccode == 3) ? -ENXIO : ccode;
555 goto out;
556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 /* Copy subchannel type from path management control word. */
558 sch->st = sch->schib.pmcw.st;
559
560 /*
561 * ... just being curious we check for non I/O subchannels
562 */
563 if (sch->st != 0) {
564 CIO_DEBUG(KERN_INFO, 0,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800565 "Subchannel 0.%x.%04x reports "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 "non-I/O subchannel type %04X\n",
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800567 sch->schid.ssid, sch->schid.sch_no, sch->st);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* We stop here for non-io subchannels. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100569 err = sch->st;
570 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572
573 /* Initialization for io subchannels. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100574 if (!sch->schib.pmcw.dnv) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 /* io subchannel but device number is invalid. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100576 err = -ENODEV;
577 goto out;
578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* Devno is valid. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800580 if (is_blacklisted (sch->schid.ssid, sch->schib.pmcw.dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /*
582 * This device must not be known to Linux. So we simply
583 * say that there is no device and return ENODEV.
584 */
585 CIO_MSG_EVENT(0, "Blacklisted device detected "
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800586 "at devno %04X, subchannel set %x\n",
587 sch->schib.pmcw.dev, sch->schid.ssid);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100588 err = -ENODEV;
589 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591 sch->opm = 0xff;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800592 if (!cio_is_console(sch->schid))
593 chsc_validate_chpids(sch);
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200594 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 CIO_DEBUG(KERN_INFO, 0,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800597 "Detected device %04x on subchannel 0.%x.%04X"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 " - PIM = %02X, PAM = %02X, POM = %02X\n",
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800599 sch->schib.pmcw.dev, sch->schid.ssid,
600 sch->schid.sch_no, sch->schib.pmcw.pim,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
602
603 /*
604 * We now have to initially ...
605 * ... set "interruption subclass"
606 * ... enable "concurrent sense"
607 * ... enable "multipath mode" if more than one
608 * CHPID is available. This is done regardless
609 * whether multiple paths are available for us.
610 */
611 sch->schib.pmcw.isc = 3; /* could be smth. else */
612 sch->schib.pmcw.csense = 1; /* concurrent sense */
613 sch->schib.pmcw.ena = 0;
614 if ((sch->lpm & (sch->lpm - 1)) != 0)
615 sch->schib.pmcw.mp = 1; /* multipath mode */
616 return 0;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100617out:
618 if (!cio_is_console(schid))
619 kfree(sch->lock);
620 sch->lock = NULL;
621 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
624/*
625 * do_IRQ() handles all normal I/O device IRQ's (the special
626 * SMP cross-CPU interrupts have their own specific
627 * handlers).
628 *
629 */
630void
631do_IRQ (struct pt_regs *regs)
632{
633 struct tpi_info *tpi_info;
634 struct subchannel *sch;
635 struct irb *irb;
Heiko Carstens5a489b92006-10-06 16:38:35 +0200636 struct pt_regs *old_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Heiko Carstens5a489b92006-10-06 16:38:35 +0200638 old_regs = set_irq_regs(regs);
Heiko Carstens9d0a57c2006-10-11 15:31:26 +0200639 irq_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 asm volatile ("mc 0,0");
641 if (S390_lowcore.int_clock >= S390_lowcore.jiffy_timer)
642 /**
643 * Make sure that the i/o interrupt did not "overtake"
644 * the last HZ timer interrupt.
645 */
Heiko Carstens5a489b92006-10-06 16:38:35 +0200646 account_ticks();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /*
648 * Get interrupt information from lowcore
649 */
650 tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
651 irb = (struct irb *) __LC_IRB;
652 do {
653 kstat_cpu(smp_processor_id()).irqs[IO_INTERRUPT]++;
654 /*
655 * Non I/O-subchannel thin interrupts are processed differently
656 */
657 if (tpi_info->adapter_IO == 1 &&
658 tpi_info->int_type == IO_INTERRUPT_TYPE) {
659 do_adapter_IO();
660 continue;
661 }
662 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
663 if (sch)
Cornelia Huck2ec22982006-12-08 15:54:26 +0100664 spin_lock(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /* Store interrupt response block to lowcore. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800666 if (tsch (tpi_info->schid, irb) == 0 && sch) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /* Keep subchannel information word up to date. */
668 memcpy (&sch->schib.scsw, &irb->scsw,
669 sizeof (irb->scsw));
670 /* Call interrupt handler if there is one. */
671 if (sch->driver && sch->driver->irq)
672 sch->driver->irq(&sch->dev);
673 }
674 if (sch)
Cornelia Huck2ec22982006-12-08 15:54:26 +0100675 spin_unlock(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /*
677 * Are more interrupts pending?
678 * If so, the tpi instruction will update the lowcore
679 * to hold the info for the next interrupt.
680 * We don't do this for VM because a tpi drops the cpu
681 * out of the sie which costs more cycles than it saves.
682 */
683 } while (!MACHINE_IS_VM && tpi (NULL) != 0);
Heiko Carstens9d0a57c2006-10-11 15:31:26 +0200684 irq_exit();
Heiko Carstens5a489b92006-10-06 16:38:35 +0200685 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
688#ifdef CONFIG_CCW_CONSOLE
689static struct subchannel console_subchannel;
690static int console_subchannel_in_use;
691
692/*
693 * busy wait for the next interrupt on the console
694 */
695void
696wait_cons_dev (void)
697{
698 unsigned long cr6 __attribute__ ((aligned (8)));
699 unsigned long save_cr6 __attribute__ ((aligned (8)));
700
701 /*
702 * before entering the spinlock we may already have
703 * processed the interrupt on a different CPU...
704 */
705 if (!console_subchannel_in_use)
706 return;
707
708 /* disable all but isc 7 (console device) */
709 __ctl_store (save_cr6, 6, 6);
710 cr6 = 0x01000000;
711 __ctl_load (cr6, 6, 6);
712
713 do {
Cornelia Huck2ec22982006-12-08 15:54:26 +0100714 spin_unlock(console_subchannel.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (!cio_tpi())
716 cpu_relax();
Cornelia Huck2ec22982006-12-08 15:54:26 +0100717 spin_lock(console_subchannel.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 } while (console_subchannel.schib.scsw.actl != 0);
719 /*
720 * restore previous isc value
721 */
722 __ctl_load (save_cr6, 6, 6);
723}
724
725static int
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800726cio_test_for_console(struct subchannel_id schid, void *data)
727{
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800728 if (stsch_err(schid, &console_subchannel.schib) != 0)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800729 return -ENXIO;
730 if (console_subchannel.schib.pmcw.dnv &&
731 console_subchannel.schib.pmcw.dev ==
732 console_devno) {
733 console_irq = schid.sch_no;
734 return 1; /* found */
735 }
736 return 0;
737}
738
739
740static int
741cio_get_console_sch_no(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800743 struct subchannel_id schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800745 init_subchannel_id(&schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (console_irq != -1) {
747 /* VM provided us with the irq number of the console. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800748 schid.sch_no = console_irq;
749 if (stsch(schid, &console_subchannel.schib) != 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 !console_subchannel.schib.pmcw.dnv)
751 return -1;
752 console_devno = console_subchannel.schib.pmcw.dev;
753 } else if (console_devno != -1) {
754 /* At least the console device number is known. */
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800755 for_each_subchannel(cio_test_for_console, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (console_irq == -1)
757 return -1;
758 } else {
759 /* unlike in 2.4, we cannot autoprobe here, since
760 * the channel subsystem is not fully initialized.
761 * With some luck, the HWC console can take over */
762 printk(KERN_WARNING "No ccw console found!\n");
763 return -1;
764 }
765 return console_irq;
766}
767
768struct subchannel *
769cio_probe_console(void)
770{
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800771 int sch_no, ret;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800772 struct subchannel_id schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 if (xchg(&console_subchannel_in_use, 1) != 0)
775 return ERR_PTR(-EBUSY);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800776 sch_no = cio_get_console_sch_no();
777 if (sch_no == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 console_subchannel_in_use = 0;
779 return ERR_PTR(-ENODEV);
780 }
781 memset(&console_subchannel, 0, sizeof(struct subchannel));
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800782 init_subchannel_id(&schid);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800783 schid.sch_no = sch_no;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800784 ret = cio_validate_subchannel(&console_subchannel, schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (ret) {
786 console_subchannel_in_use = 0;
787 return ERR_PTR(-ENODEV);
788 }
789
790 /*
791 * enable console I/O-interrupt subclass 7
792 */
793 ctl_set_bit(6, 24);
794 console_subchannel.schib.pmcw.isc = 7;
795 console_subchannel.schib.pmcw.intparm =
796 (__u32)(unsigned long)&console_subchannel;
797 ret = cio_modify(&console_subchannel);
798 if (ret) {
799 console_subchannel_in_use = 0;
800 return ERR_PTR(ret);
801 }
802 return &console_subchannel;
803}
804
805void
806cio_release_console(void)
807{
808 console_subchannel.schib.pmcw.intparm = 0;
809 cio_modify(&console_subchannel);
810 ctl_clear_bit(6, 24);
811 console_subchannel_in_use = 0;
812}
813
814/* Bah... hack to catch console special sausages. */
815int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800816cio_is_console(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
818 if (!console_subchannel_in_use)
819 return 0;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800820 return schid_equal(&schid, &console_subchannel.schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
823struct subchannel *
824cio_get_console_subchannel(void)
825{
826 if (!console_subchannel_in_use)
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200827 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return &console_subchannel;
829}
830
831#endif
832static inline int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800833__disable_subchannel_easy(struct subchannel_id schid, struct schib *schib)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 int retry, cc;
836
837 cc = 0;
838 for (retry=0;retry<3;retry++) {
839 schib->pmcw.ena = 0;
840 cc = msch(schid, schib);
841 if (cc)
842 return (cc==3?-ENODEV:-EBUSY);
843 stsch(schid, schib);
844 if (!schib->pmcw.ena)
845 return 0;
846 }
847 return -EBUSY; /* uhm... */
848}
849
850static inline int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800851__clear_subchannel_easy(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 int retry;
854
855 if (csch(schid))
856 return -ENODEV;
857 for (retry=0;retry<20;retry++) {
858 struct tpi_info ti;
859
860 if (tpi(&ti)) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800861 tsch(ti.schid, (struct irb *)__LC_IRB);
862 if (schid_equal(&ti.schid, &schid))
Cornelia Huck4c24da72005-09-03 15:58:01 -0700863 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
865 udelay(100);
866 }
867 return -EBUSY;
868}
869
Heiko Carstens15e9b582006-12-04 15:40:26 +0100870static int __shutdown_subchannel_easy(struct subchannel_id schid, void *data)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800871{
872 struct schib schib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800874 if (stsch_err(schid, &schib))
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800875 return -ENXIO;
876 if (!schib.pmcw.ena)
877 return 0;
878 switch(__disable_subchannel_easy(schid, &schib)) {
879 case 0:
880 case -ENODEV:
881 break;
882 default: /* -EBUSY */
883 if (__clear_subchannel_easy(schid))
884 break; /* give up... */
885 stsch(schid, &schib);
886 __disable_subchannel_easy(schid, &schib);
887 }
888 return 0;
889}
890
Heiko Carstens15e9b582006-12-04 15:40:26 +0100891static atomic_t chpid_reset_count;
892
893static void s390_reset_chpids_mcck_handler(void)
894{
895 struct crw crw;
896 struct mci *mci;
897
898 /* Check for pending channel report word. */
899 mci = (struct mci *)&S390_lowcore.mcck_interruption_code;
900 if (!mci->cp)
901 return;
902 /* Process channel report words. */
903 while (stcrw(&crw) == 0) {
904 /* Check for responses to RCHP. */
905 if (crw.slct && crw.rsc == CRW_RSC_CPATH)
906 atomic_dec(&chpid_reset_count);
907 }
908}
909
910#define RCHP_TIMEOUT (30 * USEC_PER_SEC)
911static void css_reset(void)
912{
913 int i, ret;
914 unsigned long long timeout;
915
916 /* Reset subchannels. */
917 for_each_subchannel(__shutdown_subchannel_easy, NULL);
918 /* Reset channel paths. */
919 s390_reset_mcck_handler = s390_reset_chpids_mcck_handler;
920 /* Enable channel report machine checks. */
921 __ctl_set_bit(14, 28);
922 /* Temporarily reenable machine checks. */
923 local_mcck_enable();
924 for (i = 0; i <= __MAX_CHPID; i++) {
925 ret = rchp(i);
926 if ((ret == 0) || (ret == 2))
927 /*
928 * rchp either succeeded, or another rchp is already
929 * in progress. In either case, we'll get a crw.
930 */
931 atomic_inc(&chpid_reset_count);
932 }
933 /* Wait for machine check for all channel paths. */
934 timeout = get_clock() + (RCHP_TIMEOUT << 12);
935 while (atomic_read(&chpid_reset_count) != 0) {
936 if (get_clock() > timeout)
937 break;
938 cpu_relax();
939 }
940 /* Disable machine checks again. */
941 local_mcck_disable();
942 /* Disable channel report machine checks. */
943 __ctl_clear_bit(14, 28);
944 s390_reset_mcck_handler = NULL;
945}
946
947static struct reset_call css_reset_call = {
948 .fn = css_reset,
949};
950
951static int __init init_css_reset_call(void)
952{
953 atomic_set(&chpid_reset_count, 0);
954 register_reset_call(&css_reset_call);
955 return 0;
956}
957
958arch_initcall(init_css_reset_call);
959
960struct sch_match_id {
961 struct subchannel_id schid;
962 struct ccw_dev_id devid;
963 int rc;
964};
965
966static int __reipl_subchannel_match(struct subchannel_id schid, void *data)
967{
968 struct schib schib;
969 struct sch_match_id *match_id = data;
970
971 if (stsch_err(schid, &schib))
972 return -ENXIO;
973 if (schib.pmcw.dnv &&
974 (schib.pmcw.dev == match_id->devid.devno) &&
975 (schid.ssid == match_id->devid.ssid)) {
976 match_id->schid = schid;
977 match_id->rc = 0;
978 return 1;
979 }
980 return 0;
981}
982
983static int reipl_find_schid(struct ccw_dev_id *devid,
984 struct subchannel_id *schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Michael Holzheuff6b8ea2006-09-20 15:58:49 +0200986 struct sch_match_id match_id;
987
988 match_id.devid = *devid;
989 match_id.rc = -ENODEV;
Heiko Carstens15e9b582006-12-04 15:40:26 +0100990 for_each_subchannel(__reipl_subchannel_match, &match_id);
Michael Holzheuff6b8ea2006-09-20 15:58:49 +0200991 if (match_id.rc == 0)
992 *schid = match_id.schid;
993 return match_id.rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
Michael Holzheuff6b8ea2006-09-20 15:58:49 +0200996extern void do_reipl_asm(__u32 schid);
997
998/* Make sure all subchannels are quiet before we re-ipl an lpar. */
999void reipl_ccw_dev(struct ccw_dev_id *devid)
1000{
1001 struct subchannel_id schid;
1002
Heiko Carstens15e9b582006-12-04 15:40:26 +01001003 s390_reset_system();
1004 if (reipl_find_schid(devid, &schid) != 0)
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001005 panic("IPL Device not found\n");
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001006 do_reipl_asm(*((__u32*)&schid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
Heiko Carstense87bfe52006-09-20 15:59:15 +02001008
1009extern struct schib ipl_schib;
1010
1011/*
1012 * ipl_save_parameters gets called very early. It is not allowed to access
1013 * anything in the bss section at all. The bss section is not cleared yet,
1014 * but may contain some ipl parameters written by the firmware.
1015 * These parameters (if present) are copied to 0x2000.
1016 * To avoid corruption of the ipl parameters, all variables used by this
1017 * function must reside on the stack or in the data section.
1018 */
1019void ipl_save_parameters(void)
1020{
1021 struct subchannel_id schid;
1022 unsigned int *ipl_ptr;
1023 void *src, *dst;
1024
1025 schid = *(struct subchannel_id *)__LC_SUBCHANNEL_ID;
1026 if (!schid.one)
1027 return;
1028 if (stsch(schid, &ipl_schib))
1029 return;
1030 if (!ipl_schib.pmcw.dnv)
1031 return;
1032 ipl_devno = ipl_schib.pmcw.dev;
1033 ipl_flags |= IPL_DEVNO_VALID;
1034 if (!ipl_schib.pmcw.qf)
1035 return;
1036 ipl_flags |= IPL_PARMBLOCK_VALID;
1037 ipl_ptr = (unsigned int *)__LC_IPL_PARMBLOCK_PTR;
1038 src = (void *)(unsigned long)*ipl_ptr;
1039 dst = (void *)IPL_PARMBLOCK_ORIGIN;
1040 memmove(dst, src, PAGE_SIZE);
1041 *ipl_ptr = IPL_PARMBLOCK_ORIGIN;
1042}