blob: 2e2882daefbbd5985d233e3a011cce15d42c4695 [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 Carstense87bfe52006-09-20 15:59:15 +020022#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "airq.h"
24#include "cio.h"
25#include "css.h"
26#include "chsc.h"
27#include "ioasm.h"
28#include "blacklist.h"
29#include "cio_debug.h"
30
31debug_info_t *cio_debug_msg_id;
32debug_info_t *cio_debug_trace_id;
33debug_info_t *cio_debug_crw_id;
34
35int cio_show_msg;
36
37static int __init
38cio_setup (char *parm)
39{
40 if (!strcmp (parm, "yes"))
41 cio_show_msg = 1;
42 else if (!strcmp (parm, "no"))
43 cio_show_msg = 0;
44 else
45 printk (KERN_ERR "cio_setup : invalid cio_msg parameter '%s'",
46 parm);
47 return 1;
48}
49
50__setup ("cio_msg=", cio_setup);
51
52/*
53 * Function: cio_debug_init
54 * Initializes three debug logs (under /proc/s390dbf) for common I/O:
55 * - cio_msg logs the messages which are printk'ed when CONFIG_DEBUG_IO is on
56 * - cio_trace logs the calling of different functions
57 * - cio_crw logs the messages which are printk'ed when CONFIG_DEBUG_CRW is on
58 * debug levels depend on CONFIG_DEBUG_IO resp. CONFIG_DEBUG_CRW
59 */
60static int __init
61cio_debug_init (void)
62{
Michael Holzheu66a464d2005-06-25 14:55:33 -070063 cio_debug_msg_id = debug_register ("cio_msg", 16, 4, 16*sizeof (long));
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (!cio_debug_msg_id)
65 goto out_unregister;
66 debug_register_view (cio_debug_msg_id, &debug_sprintf_view);
67 debug_set_level (cio_debug_msg_id, 2);
Peter Oberparleiter06fbcb12006-04-10 22:53:46 -070068 cio_debug_trace_id = debug_register ("cio_trace", 16, 4, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (!cio_debug_trace_id)
70 goto out_unregister;
71 debug_register_view (cio_debug_trace_id, &debug_hex_ascii_view);
72 debug_set_level (cio_debug_trace_id, 2);
Michael Holzheu66a464d2005-06-25 14:55:33 -070073 cio_debug_crw_id = debug_register ("cio_crw", 4, 4, 16*sizeof (long));
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (!cio_debug_crw_id)
75 goto out_unregister;
76 debug_register_view (cio_debug_crw_id, &debug_sprintf_view);
77 debug_set_level (cio_debug_crw_id, 2);
78 pr_debug("debugging initialized\n");
79 return 0;
80
81out_unregister:
82 if (cio_debug_msg_id)
83 debug_unregister (cio_debug_msg_id);
84 if (cio_debug_trace_id)
85 debug_unregister (cio_debug_trace_id);
86 if (cio_debug_crw_id)
87 debug_unregister (cio_debug_crw_id);
88 pr_debug("could not initialize debugging\n");
89 return -1;
90}
91
92arch_initcall (cio_debug_init);
93
94int
95cio_set_options (struct subchannel *sch, int flags)
96{
97 sch->options.suspend = (flags & DOIO_ALLOW_SUSPEND) != 0;
98 sch->options.prefetch = (flags & DOIO_DENY_PREFETCH) != 0;
99 sch->options.inter = (flags & DOIO_SUPPRESS_INTER) != 0;
100 return 0;
101}
102
103/* FIXME: who wants to use this? */
104int
105cio_get_options (struct subchannel *sch)
106{
107 int flags;
108
109 flags = 0;
110 if (sch->options.suspend)
111 flags |= DOIO_ALLOW_SUSPEND;
112 if (sch->options.prefetch)
113 flags |= DOIO_DENY_PREFETCH;
114 if (sch->options.inter)
115 flags |= DOIO_SUPPRESS_INTER;
116 return flags;
117}
118
119/*
120 * Use tpi to get a pending interrupt, call the interrupt handler and
121 * return a pointer to the subchannel structure.
122 */
123static inline int
124cio_tpi(void)
125{
126 struct tpi_info *tpi_info;
127 struct subchannel *sch;
128 struct irb *irb;
129
130 tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
131 if (tpi (NULL) != 1)
132 return 0;
133 irb = (struct irb *) __LC_IRB;
134 /* Store interrupt response block to lowcore. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800135 if (tsch (tpi_info->schid, irb) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 /* Not status pending or not operational. */
137 return 1;
138 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
139 if (!sch)
140 return 1;
141 local_bh_disable();
142 irq_enter ();
143 spin_lock(&sch->lock);
144 memcpy (&sch->schib.scsw, &irb->scsw, sizeof (struct scsw));
145 if (sch->driver && sch->driver->irq)
146 sch->driver->irq(&sch->dev);
147 spin_unlock(&sch->lock);
148 irq_exit ();
Heiko Carstens1f194a42006-07-03 00:24:46 -0700149 _local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return 1;
151}
152
153static inline int
154cio_start_handle_notoper(struct subchannel *sch, __u8 lpm)
155{
156 char dbf_text[15];
157
158 if (lpm != 0)
159 sch->lpm &= ~lpm;
160 else
161 sch->lpm = 0;
162
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800163 stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 CIO_MSG_EVENT(0, "cio_start: 'not oper' status for "
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800166 "subchannel 0.%x.%04x!\n", sch->schid.ssid,
167 sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 sprintf(dbf_text, "no%s", sch->dev.bus_id);
169 CIO_TRACE_EVENT(0, dbf_text);
170 CIO_HEX_EVENT(0, &sch->schib, sizeof (struct schib));
171
172 return (sch->lpm ? -EACCES : -ENODEV);
173}
174
175int
176cio_start_key (struct subchannel *sch, /* subchannel structure */
177 struct ccw1 * cpa, /* logical channel prog addr */
178 __u8 lpm, /* logical path mask */
179 __u8 key) /* storage key */
180{
181 char dbf_txt[15];
182 int ccode;
183
184 CIO_TRACE_EVENT (4, "stIO");
185 CIO_TRACE_EVENT (4, sch->dev.bus_id);
186
187 /* sch is always under 2G. */
188 sch->orb.intparm = (__u32)(unsigned long)sch;
189 sch->orb.fmt = 1;
190
191 sch->orb.pfch = sch->options.prefetch == 0;
192 sch->orb.spnd = sch->options.suspend;
193 sch->orb.ssic = sch->options.suspend && sch->options.inter;
Peter Oberparleitere0e32c82006-09-20 15:59:57 +0200194 sch->orb.lpm = (lpm != 0) ? lpm : sch->lpm;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800195#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /*
197 * for 64 bit we always support 64 bit IDAWs with 4k page size only
198 */
199 sch->orb.c64 = 1;
200 sch->orb.i2k = 0;
201#endif
202 sch->orb.key = key >> 4;
203 /* issue "Start Subchannel" */
204 sch->orb.cpa = (__u32) __pa (cpa);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800205 ccode = ssch (sch->schid, &sch->orb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 /* process condition code */
208 sprintf (dbf_txt, "ccode:%d", ccode);
209 CIO_TRACE_EVENT (4, dbf_txt);
210
211 switch (ccode) {
212 case 0:
213 /*
214 * initialize device status information
215 */
216 sch->schib.scsw.actl |= SCSW_ACTL_START_PEND;
217 return 0;
218 case 1: /* status pending */
219 case 2: /* busy */
220 return -EBUSY;
221 default: /* device/path not operational */
222 return cio_start_handle_notoper(sch, lpm);
223 }
224}
225
226int
227cio_start (struct subchannel *sch, struct ccw1 *cpa, __u8 lpm)
228{
Peter Oberparleiter0b642ed2005-05-01 08:58:58 -0700229 return cio_start_key(sch, cpa, lpm, PAGE_DEFAULT_KEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232/*
233 * resume suspended I/O operation
234 */
235int
236cio_resume (struct subchannel *sch)
237{
238 char dbf_txt[15];
239 int ccode;
240
241 CIO_TRACE_EVENT (4, "resIO");
242 CIO_TRACE_EVENT (4, sch->dev.bus_id);
243
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800244 ccode = rsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 sprintf (dbf_txt, "ccode:%d", ccode);
247 CIO_TRACE_EVENT (4, dbf_txt);
248
249 switch (ccode) {
250 case 0:
251 sch->schib.scsw.actl |= SCSW_ACTL_RESUME_PEND;
252 return 0;
253 case 1:
254 return -EBUSY;
255 case 2:
256 return -EINVAL;
257 default:
258 /*
259 * useless to wait for request completion
260 * as device is no longer operational !
261 */
262 return -ENODEV;
263 }
264}
265
266/*
267 * halt I/O operation
268 */
269int
270cio_halt(struct subchannel *sch)
271{
272 char dbf_txt[15];
273 int ccode;
274
275 if (!sch)
276 return -ENODEV;
277
278 CIO_TRACE_EVENT (2, "haltIO");
279 CIO_TRACE_EVENT (2, sch->dev.bus_id);
280
281 /*
282 * Issue "Halt subchannel" and process condition code
283 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800284 ccode = hsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 sprintf (dbf_txt, "ccode:%d", ccode);
287 CIO_TRACE_EVENT (2, dbf_txt);
288
289 switch (ccode) {
290 case 0:
291 sch->schib.scsw.actl |= SCSW_ACTL_HALT_PEND;
292 return 0;
293 case 1: /* status pending */
294 case 2: /* busy */
295 return -EBUSY;
296 default: /* device not operational */
297 return -ENODEV;
298 }
299}
300
301/*
302 * Clear I/O operation
303 */
304int
305cio_clear(struct subchannel *sch)
306{
307 char dbf_txt[15];
308 int ccode;
309
310 if (!sch)
311 return -ENODEV;
312
313 CIO_TRACE_EVENT (2, "clearIO");
314 CIO_TRACE_EVENT (2, sch->dev.bus_id);
315
316 /*
317 * Issue "Clear subchannel" and process condition code
318 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800319 ccode = csch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 sprintf (dbf_txt, "ccode:%d", ccode);
322 CIO_TRACE_EVENT (2, dbf_txt);
323
324 switch (ccode) {
325 case 0:
326 sch->schib.scsw.actl |= SCSW_ACTL_CLEAR_PEND;
327 return 0;
328 default: /* device not operational */
329 return -ENODEV;
330 }
331}
332
333/*
334 * Function: cio_cancel
335 * Issues a "Cancel Subchannel" on the specified subchannel
336 * Note: We don't need any fancy intparms and flags here
337 * since xsch is executed synchronously.
338 * Only for common I/O internal use as for now.
339 */
340int
341cio_cancel (struct subchannel *sch)
342{
343 char dbf_txt[15];
344 int ccode;
345
346 if (!sch)
347 return -ENODEV;
348
349 CIO_TRACE_EVENT (2, "cancelIO");
350 CIO_TRACE_EVENT (2, sch->dev.bus_id);
351
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800352 ccode = xsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 sprintf (dbf_txt, "ccode:%d", ccode);
355 CIO_TRACE_EVENT (2, dbf_txt);
356
357 switch (ccode) {
358 case 0: /* success */
359 /* Update information in scsw. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800360 stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return 0;
362 case 1: /* status pending */
363 return -EBUSY;
364 case 2: /* not applicable */
365 return -EINVAL;
366 default: /* not oper */
367 return -ENODEV;
368 }
369}
370
371/*
372 * Function: cio_modify
373 * Issues a "Modify Subchannel" on the specified subchannel
374 */
375int
376cio_modify (struct subchannel *sch)
377{
378 int ccode, retry, ret;
379
380 ret = 0;
381 for (retry = 0; retry < 5; retry++) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800382 ccode = msch_err (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (ccode < 0) /* -EIO if msch gets a program check. */
384 return ccode;
385 switch (ccode) {
386 case 0: /* successfull */
387 return 0;
388 case 1: /* status pending */
389 return -EBUSY;
390 case 2: /* busy */
391 udelay (100); /* allow for recovery */
392 ret = -EBUSY;
393 break;
394 case 3: /* not operational */
395 return -ENODEV;
396 }
397 }
398 return ret;
399}
400
401/*
402 * Enable subchannel.
403 */
404int
405cio_enable_subchannel (struct subchannel *sch, unsigned int isc)
406{
407 char dbf_txt[15];
408 int ccode;
409 int retry;
410 int ret;
411
412 CIO_TRACE_EVENT (2, "ensch");
413 CIO_TRACE_EVENT (2, sch->dev.bus_id);
414
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800415 ccode = stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (ccode)
417 return -ENODEV;
418
419 for (retry = 5, ret = 0; retry > 0; retry--) {
420 sch->schib.pmcw.ena = 1;
421 sch->schib.pmcw.isc = isc;
422 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
423 ret = cio_modify(sch);
424 if (ret == -ENODEV)
425 break;
426 if (ret == -EIO)
427 /*
428 * Got a program check in cio_modify. Try without
429 * the concurrent sense bit the next time.
430 */
431 sch->schib.pmcw.csense = 0;
432 if (ret == 0) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800433 stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (sch->schib.pmcw.ena)
435 break;
436 }
437 if (ret == -EBUSY) {
438 struct irb irb;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800439 if (tsch(sch->schid, &irb) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 break;
441 }
442 }
443 sprintf (dbf_txt, "ret:%d", ret);
444 CIO_TRACE_EVENT (2, dbf_txt);
445 return ret;
446}
447
448/*
449 * Disable subchannel.
450 */
451int
452cio_disable_subchannel (struct subchannel *sch)
453{
454 char dbf_txt[15];
455 int ccode;
456 int retry;
457 int ret;
458
459 CIO_TRACE_EVENT (2, "dissch");
460 CIO_TRACE_EVENT (2, sch->dev.bus_id);
461
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800462 ccode = stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (ccode == 3) /* Not operational. */
464 return -ENODEV;
465
466 if (sch->schib.scsw.actl != 0)
467 /*
468 * the disable function must not be called while there are
469 * requests pending for completion !
470 */
471 return -EBUSY;
472
473 for (retry = 5, ret = 0; retry > 0; retry--) {
474 sch->schib.pmcw.ena = 0;
475 ret = cio_modify(sch);
476 if (ret == -ENODEV)
477 break;
478 if (ret == -EBUSY)
479 /*
480 * The subchannel is busy or status pending.
481 * We'll disable when the next interrupt was delivered
482 * via the state machine.
483 */
484 break;
485 if (ret == 0) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800486 stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (!sch->schib.pmcw.ena)
488 break;
489 }
490 }
491 sprintf (dbf_txt, "ret:%d", ret);
492 CIO_TRACE_EVENT (2, dbf_txt);
493 return ret;
494}
495
496/*
497 * cio_validate_subchannel()
498 *
499 * Find out subchannel type and initialize struct subchannel.
500 * Return codes:
501 * SUBCHANNEL_TYPE_IO for a normal io subchannel
502 * SUBCHANNEL_TYPE_CHSC for a chsc subchannel
503 * SUBCHANNEL_TYPE_MESSAGE for a messaging subchannel
504 * SUBCHANNEL_TYPE_ADM for a adm(?) subchannel
505 * -ENXIO for non-defined subchannels
506 * -ENODEV for subchannels with invalid device number or blacklisted devices
507 */
508int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800509cio_validate_subchannel (struct subchannel *sch, struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 char dbf_txt[15];
512 int ccode;
513
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800514 sprintf (dbf_txt, "valsch%x", schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 CIO_TRACE_EVENT (4, dbf_txt);
516
517 /* Nuke all fields. */
518 memset(sch, 0, sizeof(struct subchannel));
519
520 spin_lock_init(&sch->lock);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200521 mutex_init(&sch->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 /* Set a name for the subchannel */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800524 snprintf (sch->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x", schid.ssid,
525 schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 /*
528 * The first subchannel that is not-operational (ccode==3)
529 * indicates that there aren't any more devices available.
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800530 * If stsch gets an exception, it means the current subchannel set
531 * is not valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800533 ccode = stsch_err (schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (ccode)
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800535 return (ccode == 3) ? -ENXIO : ccode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800537 sch->schid = schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /* Copy subchannel type from path management control word. */
539 sch->st = sch->schib.pmcw.st;
540
541 /*
542 * ... just being curious we check for non I/O subchannels
543 */
544 if (sch->st != 0) {
545 CIO_DEBUG(KERN_INFO, 0,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800546 "Subchannel 0.%x.%04x reports "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 "non-I/O subchannel type %04X\n",
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800548 sch->schid.ssid, sch->schid.sch_no, sch->st);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /* We stop here for non-io subchannels. */
550 return sch->st;
551 }
552
553 /* Initialization for io subchannels. */
554 if (!sch->schib.pmcw.dnv)
555 /* io subchannel but device number is invalid. */
556 return -ENODEV;
557
558 /* Devno is valid. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800559 if (is_blacklisted (sch->schid.ssid, sch->schib.pmcw.dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /*
561 * This device must not be known to Linux. So we simply
562 * say that there is no device and return ENODEV.
563 */
564 CIO_MSG_EVENT(0, "Blacklisted device detected "
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800565 "at devno %04X, subchannel set %x\n",
566 sch->schib.pmcw.dev, sch->schid.ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return -ENODEV;
568 }
569 sch->opm = 0xff;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800570 if (!cio_is_console(sch->schid))
571 chsc_validate_chpids(sch);
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200572 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 CIO_DEBUG(KERN_INFO, 0,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800575 "Detected device %04x on subchannel 0.%x.%04X"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 " - PIM = %02X, PAM = %02X, POM = %02X\n",
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800577 sch->schib.pmcw.dev, sch->schid.ssid,
578 sch->schid.sch_no, sch->schib.pmcw.pim,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
580
581 /*
582 * We now have to initially ...
583 * ... set "interruption subclass"
584 * ... enable "concurrent sense"
585 * ... enable "multipath mode" if more than one
586 * CHPID is available. This is done regardless
587 * whether multiple paths are available for us.
588 */
589 sch->schib.pmcw.isc = 3; /* could be smth. else */
590 sch->schib.pmcw.csense = 1; /* concurrent sense */
591 sch->schib.pmcw.ena = 0;
592 if ((sch->lpm & (sch->lpm - 1)) != 0)
593 sch->schib.pmcw.mp = 1; /* multipath mode */
594 return 0;
595}
596
597/*
598 * do_IRQ() handles all normal I/O device IRQ's (the special
599 * SMP cross-CPU interrupts have their own specific
600 * handlers).
601 *
602 */
603void
604do_IRQ (struct pt_regs *regs)
605{
606 struct tpi_info *tpi_info;
607 struct subchannel *sch;
608 struct irb *irb;
609
610 irq_enter ();
611 asm volatile ("mc 0,0");
612 if (S390_lowcore.int_clock >= S390_lowcore.jiffy_timer)
613 /**
614 * Make sure that the i/o interrupt did not "overtake"
615 * the last HZ timer interrupt.
616 */
617 account_ticks(regs);
618 /*
619 * Get interrupt information from lowcore
620 */
621 tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
622 irb = (struct irb *) __LC_IRB;
623 do {
624 kstat_cpu(smp_processor_id()).irqs[IO_INTERRUPT]++;
625 /*
626 * Non I/O-subchannel thin interrupts are processed differently
627 */
628 if (tpi_info->adapter_IO == 1 &&
629 tpi_info->int_type == IO_INTERRUPT_TYPE) {
630 do_adapter_IO();
631 continue;
632 }
633 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
634 if (sch)
635 spin_lock(&sch->lock);
636 /* Store interrupt response block to lowcore. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800637 if (tsch (tpi_info->schid, irb) == 0 && sch) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /* Keep subchannel information word up to date. */
639 memcpy (&sch->schib.scsw, &irb->scsw,
640 sizeof (irb->scsw));
641 /* Call interrupt handler if there is one. */
642 if (sch->driver && sch->driver->irq)
643 sch->driver->irq(&sch->dev);
644 }
645 if (sch)
646 spin_unlock(&sch->lock);
647 /*
648 * Are more interrupts pending?
649 * If so, the tpi instruction will update the lowcore
650 * to hold the info for the next interrupt.
651 * We don't do this for VM because a tpi drops the cpu
652 * out of the sie which costs more cycles than it saves.
653 */
654 } while (!MACHINE_IS_VM && tpi (NULL) != 0);
655 irq_exit ();
656}
657
658#ifdef CONFIG_CCW_CONSOLE
659static struct subchannel console_subchannel;
660static int console_subchannel_in_use;
661
662/*
663 * busy wait for the next interrupt on the console
664 */
665void
666wait_cons_dev (void)
667{
668 unsigned long cr6 __attribute__ ((aligned (8)));
669 unsigned long save_cr6 __attribute__ ((aligned (8)));
670
671 /*
672 * before entering the spinlock we may already have
673 * processed the interrupt on a different CPU...
674 */
675 if (!console_subchannel_in_use)
676 return;
677
678 /* disable all but isc 7 (console device) */
679 __ctl_store (save_cr6, 6, 6);
680 cr6 = 0x01000000;
681 __ctl_load (cr6, 6, 6);
682
683 do {
684 spin_unlock(&console_subchannel.lock);
685 if (!cio_tpi())
686 cpu_relax();
687 spin_lock(&console_subchannel.lock);
688 } while (console_subchannel.schib.scsw.actl != 0);
689 /*
690 * restore previous isc value
691 */
692 __ctl_load (save_cr6, 6, 6);
693}
694
695static int
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800696cio_test_for_console(struct subchannel_id schid, void *data)
697{
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800698 if (stsch_err(schid, &console_subchannel.schib) != 0)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800699 return -ENXIO;
700 if (console_subchannel.schib.pmcw.dnv &&
701 console_subchannel.schib.pmcw.dev ==
702 console_devno) {
703 console_irq = schid.sch_no;
704 return 1; /* found */
705 }
706 return 0;
707}
708
709
710static int
711cio_get_console_sch_no(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800713 struct subchannel_id schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800715 init_subchannel_id(&schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (console_irq != -1) {
717 /* VM provided us with the irq number of the console. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800718 schid.sch_no = console_irq;
719 if (stsch(schid, &console_subchannel.schib) != 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 !console_subchannel.schib.pmcw.dnv)
721 return -1;
722 console_devno = console_subchannel.schib.pmcw.dev;
723 } else if (console_devno != -1) {
724 /* At least the console device number is known. */
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800725 for_each_subchannel(cio_test_for_console, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (console_irq == -1)
727 return -1;
728 } else {
729 /* unlike in 2.4, we cannot autoprobe here, since
730 * the channel subsystem is not fully initialized.
731 * With some luck, the HWC console can take over */
732 printk(KERN_WARNING "No ccw console found!\n");
733 return -1;
734 }
735 return console_irq;
736}
737
738struct subchannel *
739cio_probe_console(void)
740{
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800741 int sch_no, ret;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800742 struct subchannel_id schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 if (xchg(&console_subchannel_in_use, 1) != 0)
745 return ERR_PTR(-EBUSY);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800746 sch_no = cio_get_console_sch_no();
747 if (sch_no == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 console_subchannel_in_use = 0;
749 return ERR_PTR(-ENODEV);
750 }
751 memset(&console_subchannel, 0, sizeof(struct subchannel));
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800752 init_subchannel_id(&schid);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800753 schid.sch_no = sch_no;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800754 ret = cio_validate_subchannel(&console_subchannel, schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (ret) {
756 console_subchannel_in_use = 0;
757 return ERR_PTR(-ENODEV);
758 }
759
760 /*
761 * enable console I/O-interrupt subclass 7
762 */
763 ctl_set_bit(6, 24);
764 console_subchannel.schib.pmcw.isc = 7;
765 console_subchannel.schib.pmcw.intparm =
766 (__u32)(unsigned long)&console_subchannel;
767 ret = cio_modify(&console_subchannel);
768 if (ret) {
769 console_subchannel_in_use = 0;
770 return ERR_PTR(ret);
771 }
772 return &console_subchannel;
773}
774
775void
776cio_release_console(void)
777{
778 console_subchannel.schib.pmcw.intparm = 0;
779 cio_modify(&console_subchannel);
780 ctl_clear_bit(6, 24);
781 console_subchannel_in_use = 0;
782}
783
784/* Bah... hack to catch console special sausages. */
785int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800786cio_is_console(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 if (!console_subchannel_in_use)
789 return 0;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800790 return schid_equal(&schid, &console_subchannel.schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
793struct subchannel *
794cio_get_console_subchannel(void)
795{
796 if (!console_subchannel_in_use)
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200797 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return &console_subchannel;
799}
800
801#endif
802static inline int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800803__disable_subchannel_easy(struct subchannel_id schid, struct schib *schib)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 int retry, cc;
806
807 cc = 0;
808 for (retry=0;retry<3;retry++) {
809 schib->pmcw.ena = 0;
810 cc = msch(schid, schib);
811 if (cc)
812 return (cc==3?-ENODEV:-EBUSY);
813 stsch(schid, schib);
814 if (!schib->pmcw.ena)
815 return 0;
816 }
817 return -EBUSY; /* uhm... */
818}
819
820static inline int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800821__clear_subchannel_easy(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 int retry;
824
825 if (csch(schid))
826 return -ENODEV;
827 for (retry=0;retry<20;retry++) {
828 struct tpi_info ti;
829
830 if (tpi(&ti)) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800831 tsch(ti.schid, (struct irb *)__LC_IRB);
832 if (schid_equal(&ti.schid, &schid))
Cornelia Huck4c24da72005-09-03 15:58:01 -0700833 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835 udelay(100);
836 }
837 return -EBUSY;
838}
839
Michael Holzheuff6b8ea2006-09-20 15:58:49 +0200840struct sch_match_id {
841 struct subchannel_id schid;
842 struct ccw_dev_id devid;
843 int rc;
844};
845
846static int __shutdown_subchannel_easy_and_match(struct subchannel_id schid,
847 void *data)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800848{
849 struct schib schib;
Michael Holzheuff6b8ea2006-09-20 15:58:49 +0200850 struct sch_match_id *match_id = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800852 if (stsch_err(schid, &schib))
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800853 return -ENXIO;
Michael Holzheuff6b8ea2006-09-20 15:58:49 +0200854 if (match_id && schib.pmcw.dnv &&
855 (schib.pmcw.dev == match_id->devid.devno) &&
856 (schid.ssid == match_id->devid.ssid)) {
857 match_id->schid = schid;
858 match_id->rc = 0;
859 }
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800860 if (!schib.pmcw.ena)
861 return 0;
862 switch(__disable_subchannel_easy(schid, &schib)) {
863 case 0:
864 case -ENODEV:
865 break;
866 default: /* -EBUSY */
867 if (__clear_subchannel_easy(schid))
868 break; /* give up... */
869 stsch(schid, &schib);
870 __disable_subchannel_easy(schid, &schib);
871 }
872 return 0;
873}
874
Michael Holzheuff6b8ea2006-09-20 15:58:49 +0200875static int clear_all_subchannels_and_match(struct ccw_dev_id *devid,
876 struct subchannel_id *schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Michael Holzheuff6b8ea2006-09-20 15:58:49 +0200878 struct sch_match_id match_id;
879
880 match_id.devid = *devid;
881 match_id.rc = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 local_irq_disable();
Michael Holzheuff6b8ea2006-09-20 15:58:49 +0200883 for_each_subchannel(__shutdown_subchannel_easy_and_match, &match_id);
884 if (match_id.rc == 0)
885 *schid = match_id.schid;
886 return match_id.rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
Michael Holzheuff6b8ea2006-09-20 15:58:49 +0200889
890void clear_all_subchannels(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Michael Holzheuff6b8ea2006-09-20 15:58:49 +0200892 local_irq_disable();
893 for_each_subchannel(__shutdown_subchannel_easy_and_match, NULL);
894}
895
896extern void do_reipl_asm(__u32 schid);
897
898/* Make sure all subchannels are quiet before we re-ipl an lpar. */
899void reipl_ccw_dev(struct ccw_dev_id *devid)
900{
901 struct subchannel_id schid;
902
903 if (clear_all_subchannels_and_match(devid, &schid))
904 panic("IPL Device not found\n");
Cornelia Huck7e560812006-07-12 16:40:19 +0200905 cio_reset_channel_paths();
Michael Holzheuff6b8ea2006-09-20 15:58:49 +0200906 do_reipl_asm(*((__u32*)&schid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
Heiko Carstense87bfe52006-09-20 15:59:15 +0200908
909extern struct schib ipl_schib;
910
911/*
912 * ipl_save_parameters gets called very early. It is not allowed to access
913 * anything in the bss section at all. The bss section is not cleared yet,
914 * but may contain some ipl parameters written by the firmware.
915 * These parameters (if present) are copied to 0x2000.
916 * To avoid corruption of the ipl parameters, all variables used by this
917 * function must reside on the stack or in the data section.
918 */
919void ipl_save_parameters(void)
920{
921 struct subchannel_id schid;
922 unsigned int *ipl_ptr;
923 void *src, *dst;
924
925 schid = *(struct subchannel_id *)__LC_SUBCHANNEL_ID;
926 if (!schid.one)
927 return;
928 if (stsch(schid, &ipl_schib))
929 return;
930 if (!ipl_schib.pmcw.dnv)
931 return;
932 ipl_devno = ipl_schib.pmcw.dev;
933 ipl_flags |= IPL_DEVNO_VALID;
934 if (!ipl_schib.pmcw.qf)
935 return;
936 ipl_flags |= IPL_PARMBLOCK_VALID;
937 ipl_ptr = (unsigned int *)__LC_IPL_PARMBLOCK_PTR;
938 src = (void *)(unsigned long)*ipl_ptr;
939 dst = (void *)IPL_PARMBLOCK_ORIGIN;
940 memmove(dst, src, PAGE_SIZE);
941 *ipl_ptr = IPL_PARMBLOCK_ORIGIN;
942}