blob: 7dd0649c95d11c0bcb755aad0d13d047cf9235bc [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 *
Michael Holzheuaa770152006-12-28 00:35:36 +01005 * Copyright (C) IBM Corp. 1999,2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Author(s): Ingo Adlung (adlung@de.ibm.com)
Cornelia Huck4ce3b302006-01-14 13:21:04 -08007 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Arnd Bergmann (arndb@de.ibm.com)
9 * Martin Schwidefsky (schwidefsky@de.ibm.com)
10 */
11
12#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/init.h>
14#include <linux/slab.h>
15#include <linux/device.h>
16#include <linux/kernel_stat.h>
17#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/cio.h>
19#include <asm/delay.h>
20#include <asm/irq.h>
Heiko Carstens5a489b92006-10-06 16:38:35 +020021#include <asm/irq_regs.h>
Heiko Carstense87bfe52006-09-20 15:59:15 +020022#include <asm/setup.h>
Heiko Carstens15e9b582006-12-04 15:40:26 +010023#include <asm/reset.h>
Michael Holzheu46b05d22007-02-21 10:55:21 +010024#include <asm/ipl.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"
Peter Oberparleitere6b6e102007-04-27 16:01:28 +020032#include "chp.h"
Heiko Carstens15e9b582006-12-04 15:40:26 +010033#include "../s390mach.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35debug_info_t *cio_debug_msg_id;
36debug_info_t *cio_debug_trace_id;
37debug_info_t *cio_debug_crw_id;
38
39int cio_show_msg;
40
41static int __init
42cio_setup (char *parm)
43{
44 if (!strcmp (parm, "yes"))
45 cio_show_msg = 1;
46 else if (!strcmp (parm, "no"))
47 cio_show_msg = 0;
48 else
49 printk (KERN_ERR "cio_setup : invalid cio_msg parameter '%s'",
50 parm);
51 return 1;
52}
53
54__setup ("cio_msg=", cio_setup);
55
56/*
57 * Function: cio_debug_init
58 * Initializes three debug logs (under /proc/s390dbf) for common I/O:
59 * - cio_msg logs the messages which are printk'ed when CONFIG_DEBUG_IO is on
60 * - cio_trace logs the calling of different functions
61 * - cio_crw logs the messages which are printk'ed when CONFIG_DEBUG_CRW is on
62 * debug levels depend on CONFIG_DEBUG_IO resp. CONFIG_DEBUG_CRW
63 */
64static int __init
65cio_debug_init (void)
66{
Michael Holzheu66a464d2005-06-25 14:55:33 -070067 cio_debug_msg_id = debug_register ("cio_msg", 16, 4, 16*sizeof (long));
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (!cio_debug_msg_id)
69 goto out_unregister;
70 debug_register_view (cio_debug_msg_id, &debug_sprintf_view);
71 debug_set_level (cio_debug_msg_id, 2);
Peter Oberparleiter06fbcb12006-04-10 22:53:46 -070072 cio_debug_trace_id = debug_register ("cio_trace", 16, 4, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (!cio_debug_trace_id)
74 goto out_unregister;
75 debug_register_view (cio_debug_trace_id, &debug_hex_ascii_view);
76 debug_set_level (cio_debug_trace_id, 2);
Michael Holzheu66a464d2005-06-25 14:55:33 -070077 cio_debug_crw_id = debug_register ("cio_crw", 4, 4, 16*sizeof (long));
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (!cio_debug_crw_id)
79 goto out_unregister;
80 debug_register_view (cio_debug_crw_id, &debug_sprintf_view);
81 debug_set_level (cio_debug_crw_id, 2);
82 pr_debug("debugging initialized\n");
83 return 0;
84
85out_unregister:
86 if (cio_debug_msg_id)
87 debug_unregister (cio_debug_msg_id);
88 if (cio_debug_trace_id)
89 debug_unregister (cio_debug_trace_id);
90 if (cio_debug_crw_id)
91 debug_unregister (cio_debug_crw_id);
92 pr_debug("could not initialize debugging\n");
93 return -1;
94}
95
96arch_initcall (cio_debug_init);
97
98int
99cio_set_options (struct subchannel *sch, int flags)
100{
101 sch->options.suspend = (flags & DOIO_ALLOW_SUSPEND) != 0;
102 sch->options.prefetch = (flags & DOIO_DENY_PREFETCH) != 0;
103 sch->options.inter = (flags & DOIO_SUPPRESS_INTER) != 0;
104 return 0;
105}
106
107/* FIXME: who wants to use this? */
108int
109cio_get_options (struct subchannel *sch)
110{
111 int flags;
112
113 flags = 0;
114 if (sch->options.suspend)
115 flags |= DOIO_ALLOW_SUSPEND;
116 if (sch->options.prefetch)
117 flags |= DOIO_DENY_PREFETCH;
118 if (sch->options.inter)
119 flags |= DOIO_SUPPRESS_INTER;
120 return flags;
121}
122
123/*
124 * Use tpi to get a pending interrupt, call the interrupt handler and
125 * return a pointer to the subchannel structure.
126 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100127static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128cio_tpi(void)
129{
130 struct tpi_info *tpi_info;
131 struct subchannel *sch;
132 struct irb *irb;
133
134 tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
135 if (tpi (NULL) != 1)
136 return 0;
137 irb = (struct irb *) __LC_IRB;
138 /* Store interrupt response block to lowcore. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800139 if (tsch (tpi_info->schid, irb) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 /* Not status pending or not operational. */
141 return 1;
142 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
143 if (!sch)
144 return 1;
145 local_bh_disable();
146 irq_enter ();
Cornelia Huck2ec22982006-12-08 15:54:26 +0100147 spin_lock(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 memcpy (&sch->schib.scsw, &irb->scsw, sizeof (struct scsw));
149 if (sch->driver && sch->driver->irq)
150 sch->driver->irq(&sch->dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100151 spin_unlock(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 irq_exit ();
Heiko Carstens1f194a42006-07-03 00:24:46 -0700153 _local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return 1;
155}
156
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100157static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158cio_start_handle_notoper(struct subchannel *sch, __u8 lpm)
159{
160 char dbf_text[15];
161
162 if (lpm != 0)
163 sch->lpm &= ~lpm;
164 else
165 sch->lpm = 0;
166
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800167 stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 CIO_MSG_EVENT(0, "cio_start: 'not oper' status for "
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800170 "subchannel 0.%x.%04x!\n", sch->schid.ssid,
171 sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 sprintf(dbf_text, "no%s", sch->dev.bus_id);
173 CIO_TRACE_EVENT(0, dbf_text);
174 CIO_HEX_EVENT(0, &sch->schib, sizeof (struct schib));
175
176 return (sch->lpm ? -EACCES : -ENODEV);
177}
178
179int
180cio_start_key (struct subchannel *sch, /* subchannel structure */
181 struct ccw1 * cpa, /* logical channel prog addr */
182 __u8 lpm, /* logical path mask */
183 __u8 key) /* storage key */
184{
185 char dbf_txt[15];
186 int ccode;
187
188 CIO_TRACE_EVENT (4, "stIO");
189 CIO_TRACE_EVENT (4, sch->dev.bus_id);
190
191 /* sch is always under 2G. */
192 sch->orb.intparm = (__u32)(unsigned long)sch;
193 sch->orb.fmt = 1;
194
195 sch->orb.pfch = sch->options.prefetch == 0;
196 sch->orb.spnd = sch->options.suspend;
197 sch->orb.ssic = sch->options.suspend && sch->options.inter;
Peter Oberparleitere0e32c82006-09-20 15:59:57 +0200198 sch->orb.lpm = (lpm != 0) ? lpm : sch->lpm;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800199#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /*
201 * for 64 bit we always support 64 bit IDAWs with 4k page size only
202 */
203 sch->orb.c64 = 1;
204 sch->orb.i2k = 0;
205#endif
206 sch->orb.key = key >> 4;
207 /* issue "Start Subchannel" */
208 sch->orb.cpa = (__u32) __pa (cpa);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800209 ccode = ssch (sch->schid, &sch->orb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 /* process condition code */
212 sprintf (dbf_txt, "ccode:%d", ccode);
213 CIO_TRACE_EVENT (4, dbf_txt);
214
215 switch (ccode) {
216 case 0:
217 /*
218 * initialize device status information
219 */
220 sch->schib.scsw.actl |= SCSW_ACTL_START_PEND;
221 return 0;
222 case 1: /* status pending */
223 case 2: /* busy */
224 return -EBUSY;
225 default: /* device/path not operational */
226 return cio_start_handle_notoper(sch, lpm);
227 }
228}
229
230int
231cio_start (struct subchannel *sch, struct ccw1 *cpa, __u8 lpm)
232{
Peter Oberparleiter0b642ed2005-05-01 08:58:58 -0700233 return cio_start_key(sch, cpa, lpm, PAGE_DEFAULT_KEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236/*
237 * resume suspended I/O operation
238 */
239int
240cio_resume (struct subchannel *sch)
241{
242 char dbf_txt[15];
243 int ccode;
244
245 CIO_TRACE_EVENT (4, "resIO");
246 CIO_TRACE_EVENT (4, sch->dev.bus_id);
247
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800248 ccode = rsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 sprintf (dbf_txt, "ccode:%d", ccode);
251 CIO_TRACE_EVENT (4, dbf_txt);
252
253 switch (ccode) {
254 case 0:
255 sch->schib.scsw.actl |= SCSW_ACTL_RESUME_PEND;
256 return 0;
257 case 1:
258 return -EBUSY;
259 case 2:
260 return -EINVAL;
261 default:
262 /*
263 * useless to wait for request completion
264 * as device is no longer operational !
265 */
266 return -ENODEV;
267 }
268}
269
270/*
271 * halt I/O operation
272 */
273int
274cio_halt(struct subchannel *sch)
275{
276 char dbf_txt[15];
277 int ccode;
278
279 if (!sch)
280 return -ENODEV;
281
282 CIO_TRACE_EVENT (2, "haltIO");
283 CIO_TRACE_EVENT (2, sch->dev.bus_id);
284
285 /*
286 * Issue "Halt subchannel" and process condition code
287 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800288 ccode = hsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 sprintf (dbf_txt, "ccode:%d", ccode);
291 CIO_TRACE_EVENT (2, dbf_txt);
292
293 switch (ccode) {
294 case 0:
295 sch->schib.scsw.actl |= SCSW_ACTL_HALT_PEND;
296 return 0;
297 case 1: /* status pending */
298 case 2: /* busy */
299 return -EBUSY;
300 default: /* device not operational */
301 return -ENODEV;
302 }
303}
304
305/*
306 * Clear I/O operation
307 */
308int
309cio_clear(struct subchannel *sch)
310{
311 char dbf_txt[15];
312 int ccode;
313
314 if (!sch)
315 return -ENODEV;
316
317 CIO_TRACE_EVENT (2, "clearIO");
318 CIO_TRACE_EVENT (2, sch->dev.bus_id);
319
320 /*
321 * Issue "Clear subchannel" and process condition code
322 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800323 ccode = csch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 sprintf (dbf_txt, "ccode:%d", ccode);
326 CIO_TRACE_EVENT (2, dbf_txt);
327
328 switch (ccode) {
329 case 0:
330 sch->schib.scsw.actl |= SCSW_ACTL_CLEAR_PEND;
331 return 0;
332 default: /* device not operational */
333 return -ENODEV;
334 }
335}
336
337/*
338 * Function: cio_cancel
339 * Issues a "Cancel Subchannel" on the specified subchannel
340 * Note: We don't need any fancy intparms and flags here
341 * since xsch is executed synchronously.
342 * Only for common I/O internal use as for now.
343 */
344int
345cio_cancel (struct subchannel *sch)
346{
347 char dbf_txt[15];
348 int ccode;
349
350 if (!sch)
351 return -ENODEV;
352
353 CIO_TRACE_EVENT (2, "cancelIO");
354 CIO_TRACE_EVENT (2, sch->dev.bus_id);
355
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800356 ccode = xsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 sprintf (dbf_txt, "ccode:%d", ccode);
359 CIO_TRACE_EVENT (2, dbf_txt);
360
361 switch (ccode) {
362 case 0: /* success */
363 /* Update information in scsw. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800364 stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return 0;
366 case 1: /* status pending */
367 return -EBUSY;
368 case 2: /* not applicable */
369 return -EINVAL;
370 default: /* not oper */
371 return -ENODEV;
372 }
373}
374
375/*
376 * Function: cio_modify
377 * Issues a "Modify Subchannel" on the specified subchannel
378 */
379int
380cio_modify (struct subchannel *sch)
381{
382 int ccode, retry, ret;
383
384 ret = 0;
385 for (retry = 0; retry < 5; retry++) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800386 ccode = msch_err (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (ccode < 0) /* -EIO if msch gets a program check. */
388 return ccode;
389 switch (ccode) {
390 case 0: /* successfull */
391 return 0;
392 case 1: /* status pending */
393 return -EBUSY;
394 case 2: /* busy */
395 udelay (100); /* allow for recovery */
396 ret = -EBUSY;
397 break;
398 case 3: /* not operational */
399 return -ENODEV;
400 }
401 }
402 return ret;
403}
404
405/*
406 * Enable subchannel.
407 */
408int
409cio_enable_subchannel (struct subchannel *sch, unsigned int isc)
410{
411 char dbf_txt[15];
412 int ccode;
413 int retry;
414 int ret;
415
416 CIO_TRACE_EVENT (2, "ensch");
417 CIO_TRACE_EVENT (2, sch->dev.bus_id);
418
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100419 if (sch_is_pseudo_sch(sch))
420 return -EINVAL;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800421 ccode = stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (ccode)
423 return -ENODEV;
424
425 for (retry = 5, ret = 0; retry > 0; retry--) {
426 sch->schib.pmcw.ena = 1;
427 sch->schib.pmcw.isc = isc;
428 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
429 ret = cio_modify(sch);
430 if (ret == -ENODEV)
431 break;
432 if (ret == -EIO)
433 /*
434 * Got a program check in cio_modify. Try without
435 * the concurrent sense bit the next time.
436 */
437 sch->schib.pmcw.csense = 0;
438 if (ret == 0) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800439 stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (sch->schib.pmcw.ena)
441 break;
442 }
443 if (ret == -EBUSY) {
444 struct irb irb;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800445 if (tsch(sch->schid, &irb) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 break;
447 }
448 }
449 sprintf (dbf_txt, "ret:%d", ret);
450 CIO_TRACE_EVENT (2, dbf_txt);
451 return ret;
452}
453
454/*
455 * Disable subchannel.
456 */
457int
458cio_disable_subchannel (struct subchannel *sch)
459{
460 char dbf_txt[15];
461 int ccode;
462 int retry;
463 int ret;
464
465 CIO_TRACE_EVENT (2, "dissch");
466 CIO_TRACE_EVENT (2, sch->dev.bus_id);
467
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100468 if (sch_is_pseudo_sch(sch))
469 return 0;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800470 ccode = stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (ccode == 3) /* Not operational. */
472 return -ENODEV;
473
474 if (sch->schib.scsw.actl != 0)
475 /*
476 * the disable function must not be called while there are
477 * requests pending for completion !
478 */
479 return -EBUSY;
480
481 for (retry = 5, ret = 0; retry > 0; retry--) {
482 sch->schib.pmcw.ena = 0;
483 ret = cio_modify(sch);
484 if (ret == -ENODEV)
485 break;
486 if (ret == -EBUSY)
487 /*
488 * The subchannel is busy or status pending.
489 * We'll disable when the next interrupt was delivered
490 * via the state machine.
491 */
492 break;
493 if (ret == 0) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800494 stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (!sch->schib.pmcw.ena)
496 break;
497 }
498 }
499 sprintf (dbf_txt, "ret:%d", ret);
500 CIO_TRACE_EVENT (2, dbf_txt);
501 return ret;
502}
503
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100504int cio_create_sch_lock(struct subchannel *sch)
Cornelia Huck2ec22982006-12-08 15:54:26 +0100505{
506 sch->lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
507 if (!sch->lock)
508 return -ENOMEM;
509 spin_lock_init(sch->lock);
510 return 0;
511}
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513/*
514 * cio_validate_subchannel()
515 *
516 * Find out subchannel type and initialize struct subchannel.
517 * Return codes:
518 * SUBCHANNEL_TYPE_IO for a normal io subchannel
519 * SUBCHANNEL_TYPE_CHSC for a chsc subchannel
520 * SUBCHANNEL_TYPE_MESSAGE for a messaging subchannel
521 * SUBCHANNEL_TYPE_ADM for a adm(?) subchannel
522 * -ENXIO for non-defined subchannels
523 * -ENODEV for subchannels with invalid device number or blacklisted devices
524 */
525int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800526cio_validate_subchannel (struct subchannel *sch, struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 char dbf_txt[15];
529 int ccode;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100530 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800532 sprintf (dbf_txt, "valsch%x", schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 CIO_TRACE_EVENT (4, dbf_txt);
534
535 /* Nuke all fields. */
536 memset(sch, 0, sizeof(struct subchannel));
537
Cornelia Huck2ec22982006-12-08 15:54:26 +0100538 sch->schid = schid;
539 if (cio_is_console(schid)) {
540 sch->lock = cio_get_console_lock();
541 } else {
542 err = cio_create_sch_lock(sch);
543 if (err)
544 goto out;
545 }
Cornelia Huck6ab48792006-07-12 16:39:50 +0200546 mutex_init(&sch->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /* Set a name for the subchannel */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800548 snprintf (sch->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x", schid.ssid,
549 schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /*
552 * The first subchannel that is not-operational (ccode==3)
553 * indicates that there aren't any more devices available.
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800554 * If stsch gets an exception, it means the current subchannel set
555 * is not valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800557 ccode = stsch_err (schid, &sch->schib);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100558 if (ccode) {
559 err = (ccode == 3) ? -ENXIO : ccode;
560 goto out;
561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /* Copy subchannel type from path management control word. */
563 sch->st = sch->schib.pmcw.st;
564
565 /*
566 * ... just being curious we check for non I/O subchannels
567 */
568 if (sch->st != 0) {
569 CIO_DEBUG(KERN_INFO, 0,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800570 "Subchannel 0.%x.%04x reports "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 "non-I/O subchannel type %04X\n",
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800572 sch->schid.ssid, sch->schid.sch_no, sch->st);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /* We stop here for non-io subchannels. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100574 err = sch->st;
575 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
578 /* Initialization for io subchannels. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100579 if (!sch->schib.pmcw.dnv) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /* io subchannel but device number is invalid. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100581 err = -ENODEV;
582 goto out;
583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /* Devno is valid. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800585 if (is_blacklisted (sch->schid.ssid, sch->schib.pmcw.dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 /*
587 * This device must not be known to Linux. So we simply
588 * say that there is no device and return ENODEV.
589 */
Cornelia Huck347d59d2007-02-05 21:17:56 +0100590 CIO_MSG_EVENT(4, "Blacklisted device detected "
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800591 "at devno %04X, subchannel set %x\n",
592 sch->schib.pmcw.dev, sch->schid.ssid);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100593 err = -ENODEV;
594 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200596 if (cio_is_console(sch->schid))
597 sch->opm = 0xff;
598 else
599 sch->opm = chp_get_sch_opm(sch);
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200600 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 CIO_DEBUG(KERN_INFO, 0,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800603 "Detected device %04x on subchannel 0.%x.%04X"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 " - PIM = %02X, PAM = %02X, POM = %02X\n",
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800605 sch->schib.pmcw.dev, sch->schid.ssid,
606 sch->schid.sch_no, sch->schib.pmcw.pim,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
608
609 /*
610 * We now have to initially ...
611 * ... set "interruption subclass"
612 * ... enable "concurrent sense"
613 * ... enable "multipath mode" if more than one
614 * CHPID is available. This is done regardless
615 * whether multiple paths are available for us.
616 */
617 sch->schib.pmcw.isc = 3; /* could be smth. else */
618 sch->schib.pmcw.csense = 1; /* concurrent sense */
619 sch->schib.pmcw.ena = 0;
620 if ((sch->lpm & (sch->lpm - 1)) != 0)
621 sch->schib.pmcw.mp = 1; /* multipath mode */
622 return 0;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100623out:
624 if (!cio_is_console(schid))
625 kfree(sch->lock);
626 sch->lock = NULL;
627 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
630/*
631 * do_IRQ() handles all normal I/O device IRQ's (the special
632 * SMP cross-CPU interrupts have their own specific
633 * handlers).
634 *
635 */
636void
637do_IRQ (struct pt_regs *regs)
638{
639 struct tpi_info *tpi_info;
640 struct subchannel *sch;
641 struct irb *irb;
Heiko Carstens5a489b92006-10-06 16:38:35 +0200642 struct pt_regs *old_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Heiko Carstens5a489b92006-10-06 16:38:35 +0200644 old_regs = set_irq_regs(regs);
Heiko Carstens9d0a57c2006-10-11 15:31:26 +0200645 irq_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 asm volatile ("mc 0,0");
647 if (S390_lowcore.int_clock >= S390_lowcore.jiffy_timer)
648 /**
649 * Make sure that the i/o interrupt did not "overtake"
650 * the last HZ timer interrupt.
651 */
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100652 account_ticks(S390_lowcore.int_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 /*
654 * Get interrupt information from lowcore
655 */
656 tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
657 irb = (struct irb *) __LC_IRB;
658 do {
659 kstat_cpu(smp_processor_id()).irqs[IO_INTERRUPT]++;
660 /*
661 * Non I/O-subchannel thin interrupts are processed differently
662 */
663 if (tpi_info->adapter_IO == 1 &&
664 tpi_info->int_type == IO_INTERRUPT_TYPE) {
665 do_adapter_IO();
666 continue;
667 }
668 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
669 if (sch)
Cornelia Huck2ec22982006-12-08 15:54:26 +0100670 spin_lock(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /* Store interrupt response block to lowcore. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800672 if (tsch (tpi_info->schid, irb) == 0 && sch) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 /* Keep subchannel information word up to date. */
674 memcpy (&sch->schib.scsw, &irb->scsw,
675 sizeof (irb->scsw));
676 /* Call interrupt handler if there is one. */
677 if (sch->driver && sch->driver->irq)
678 sch->driver->irq(&sch->dev);
679 }
680 if (sch)
Cornelia Huck2ec22982006-12-08 15:54:26 +0100681 spin_unlock(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /*
683 * Are more interrupts pending?
684 * If so, the tpi instruction will update the lowcore
685 * to hold the info for the next interrupt.
686 * We don't do this for VM because a tpi drops the cpu
687 * out of the sie which costs more cycles than it saves.
688 */
689 } while (!MACHINE_IS_VM && tpi (NULL) != 0);
Heiko Carstens9d0a57c2006-10-11 15:31:26 +0200690 irq_exit();
Heiko Carstens5a489b92006-10-06 16:38:35 +0200691 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
694#ifdef CONFIG_CCW_CONSOLE
695static struct subchannel console_subchannel;
696static int console_subchannel_in_use;
697
698/*
699 * busy wait for the next interrupt on the console
700 */
701void
702wait_cons_dev (void)
703{
704 unsigned long cr6 __attribute__ ((aligned (8)));
705 unsigned long save_cr6 __attribute__ ((aligned (8)));
706
707 /*
708 * before entering the spinlock we may already have
709 * processed the interrupt on a different CPU...
710 */
711 if (!console_subchannel_in_use)
712 return;
713
714 /* disable all but isc 7 (console device) */
715 __ctl_store (save_cr6, 6, 6);
716 cr6 = 0x01000000;
717 __ctl_load (cr6, 6, 6);
718
719 do {
Cornelia Huck2ec22982006-12-08 15:54:26 +0100720 spin_unlock(console_subchannel.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (!cio_tpi())
722 cpu_relax();
Cornelia Huck2ec22982006-12-08 15:54:26 +0100723 spin_lock(console_subchannel.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 } while (console_subchannel.schib.scsw.actl != 0);
725 /*
726 * restore previous isc value
727 */
728 __ctl_load (save_cr6, 6, 6);
729}
730
731static int
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800732cio_test_for_console(struct subchannel_id schid, void *data)
733{
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800734 if (stsch_err(schid, &console_subchannel.schib) != 0)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800735 return -ENXIO;
736 if (console_subchannel.schib.pmcw.dnv &&
737 console_subchannel.schib.pmcw.dev ==
738 console_devno) {
739 console_irq = schid.sch_no;
740 return 1; /* found */
741 }
742 return 0;
743}
744
745
746static int
747cio_get_console_sch_no(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800749 struct subchannel_id schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800751 init_subchannel_id(&schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (console_irq != -1) {
753 /* VM provided us with the irq number of the console. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800754 schid.sch_no = console_irq;
755 if (stsch(schid, &console_subchannel.schib) != 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 !console_subchannel.schib.pmcw.dnv)
757 return -1;
758 console_devno = console_subchannel.schib.pmcw.dev;
759 } else if (console_devno != -1) {
760 /* At least the console device number is known. */
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800761 for_each_subchannel(cio_test_for_console, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (console_irq == -1)
763 return -1;
764 } else {
765 /* unlike in 2.4, we cannot autoprobe here, since
766 * the channel subsystem is not fully initialized.
767 * With some luck, the HWC console can take over */
768 printk(KERN_WARNING "No ccw console found!\n");
769 return -1;
770 }
771 return console_irq;
772}
773
774struct subchannel *
775cio_probe_console(void)
776{
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800777 int sch_no, ret;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800778 struct subchannel_id schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 if (xchg(&console_subchannel_in_use, 1) != 0)
781 return ERR_PTR(-EBUSY);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800782 sch_no = cio_get_console_sch_no();
783 if (sch_no == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 console_subchannel_in_use = 0;
785 return ERR_PTR(-ENODEV);
786 }
787 memset(&console_subchannel, 0, sizeof(struct subchannel));
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800788 init_subchannel_id(&schid);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800789 schid.sch_no = sch_no;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800790 ret = cio_validate_subchannel(&console_subchannel, schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (ret) {
792 console_subchannel_in_use = 0;
793 return ERR_PTR(-ENODEV);
794 }
795
796 /*
797 * enable console I/O-interrupt subclass 7
798 */
799 ctl_set_bit(6, 24);
800 console_subchannel.schib.pmcw.isc = 7;
801 console_subchannel.schib.pmcw.intparm =
802 (__u32)(unsigned long)&console_subchannel;
803 ret = cio_modify(&console_subchannel);
804 if (ret) {
805 console_subchannel_in_use = 0;
806 return ERR_PTR(ret);
807 }
808 return &console_subchannel;
809}
810
811void
812cio_release_console(void)
813{
814 console_subchannel.schib.pmcw.intparm = 0;
815 cio_modify(&console_subchannel);
816 ctl_clear_bit(6, 24);
817 console_subchannel_in_use = 0;
818}
819
820/* Bah... hack to catch console special sausages. */
821int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800822cio_is_console(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 if (!console_subchannel_in_use)
825 return 0;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800826 return schid_equal(&schid, &console_subchannel.schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
829struct subchannel *
830cio_get_console_subchannel(void)
831{
832 if (!console_subchannel_in_use)
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200833 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return &console_subchannel;
835}
836
837#endif
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100838static int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800839__disable_subchannel_easy(struct subchannel_id schid, struct schib *schib)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 int retry, cc;
842
843 cc = 0;
844 for (retry=0;retry<3;retry++) {
845 schib->pmcw.ena = 0;
846 cc = msch(schid, schib);
847 if (cc)
848 return (cc==3?-ENODEV:-EBUSY);
849 stsch(schid, schib);
850 if (!schib->pmcw.ena)
851 return 0;
852 }
853 return -EBUSY; /* uhm... */
854}
855
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100856/* we can't use the normal udelay here, since it enables external interrupts */
857
858static void udelay_reset(unsigned long usecs)
859{
860 uint64_t start_cc, end_cc;
861
862 asm volatile ("STCK %0" : "=m" (start_cc));
863 do {
864 cpu_relax();
865 asm volatile ("STCK %0" : "=m" (end_cc));
866 } while (((end_cc - start_cc)/4096) < usecs);
867}
868
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100869static int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800870__clear_subchannel_easy(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 int retry;
873
874 if (csch(schid))
875 return -ENODEV;
876 for (retry=0;retry<20;retry++) {
877 struct tpi_info ti;
878
879 if (tpi(&ti)) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800880 tsch(ti.schid, (struct irb *)__LC_IRB);
881 if (schid_equal(&ti.schid, &schid))
Cornelia Huck4c24da72005-09-03 15:58:01 -0700882 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100884 udelay_reset(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
886 return -EBUSY;
887}
888
Michael Holzheua45e1412006-12-15 17:18:22 +0100889static int pgm_check_occured;
890
891static void cio_reset_pgm_check_handler(void)
892{
893 pgm_check_occured = 1;
894}
895
896static int stsch_reset(struct subchannel_id schid, volatile struct schib *addr)
897{
898 int rc;
899
900 pgm_check_occured = 0;
Heiko Carstensab14de62007-02-05 21:18:37 +0100901 s390_base_pgm_handler_fn = cio_reset_pgm_check_handler;
Heiko Carstens6faf4442007-01-09 10:18:41 +0100902 rc = stsch(schid, addr);
Heiko Carstensab14de62007-02-05 21:18:37 +0100903 s390_base_pgm_handler_fn = NULL;
Heiko Carstens6faf4442007-01-09 10:18:41 +0100904
Heiko Carstensab14de62007-02-05 21:18:37 +0100905 /* The program check handler could have changed pgm_check_occured. */
Heiko Carstens6faf4442007-01-09 10:18:41 +0100906 barrier();
907
Michael Holzheua45e1412006-12-15 17:18:22 +0100908 if (pgm_check_occured)
909 return -EIO;
910 else
911 return rc;
912}
913
Heiko Carstens15e9b582006-12-04 15:40:26 +0100914static int __shutdown_subchannel_easy(struct subchannel_id schid, void *data)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800915{
916 struct schib schib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Michael Holzheua45e1412006-12-15 17:18:22 +0100918 if (stsch_reset(schid, &schib))
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800919 return -ENXIO;
920 if (!schib.pmcw.ena)
921 return 0;
922 switch(__disable_subchannel_easy(schid, &schib)) {
923 case 0:
924 case -ENODEV:
925 break;
926 default: /* -EBUSY */
927 if (__clear_subchannel_easy(schid))
928 break; /* give up... */
929 stsch(schid, &schib);
930 __disable_subchannel_easy(schid, &schib);
931 }
932 return 0;
933}
934
Heiko Carstens15e9b582006-12-04 15:40:26 +0100935static atomic_t chpid_reset_count;
936
937static void s390_reset_chpids_mcck_handler(void)
938{
939 struct crw crw;
940 struct mci *mci;
941
942 /* Check for pending channel report word. */
943 mci = (struct mci *)&S390_lowcore.mcck_interruption_code;
944 if (!mci->cp)
945 return;
946 /* Process channel report words. */
947 while (stcrw(&crw) == 0) {
948 /* Check for responses to RCHP. */
949 if (crw.slct && crw.rsc == CRW_RSC_CPATH)
950 atomic_dec(&chpid_reset_count);
951 }
952}
953
954#define RCHP_TIMEOUT (30 * USEC_PER_SEC)
955static void css_reset(void)
956{
957 int i, ret;
958 unsigned long long timeout;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200959 struct chp_id chpid;
Heiko Carstens15e9b582006-12-04 15:40:26 +0100960
961 /* Reset subchannels. */
962 for_each_subchannel(__shutdown_subchannel_easy, NULL);
963 /* Reset channel paths. */
Heiko Carstensab14de62007-02-05 21:18:37 +0100964 s390_base_mcck_handler_fn = s390_reset_chpids_mcck_handler;
Heiko Carstens15e9b582006-12-04 15:40:26 +0100965 /* Enable channel report machine checks. */
966 __ctl_set_bit(14, 28);
967 /* Temporarily reenable machine checks. */
968 local_mcck_enable();
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200969 chp_id_init(&chpid);
Heiko Carstens15e9b582006-12-04 15:40:26 +0100970 for (i = 0; i <= __MAX_CHPID; i++) {
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200971 chpid.id = i;
972 ret = rchp(chpid);
Heiko Carstens15e9b582006-12-04 15:40:26 +0100973 if ((ret == 0) || (ret == 2))
974 /*
975 * rchp either succeeded, or another rchp is already
976 * in progress. In either case, we'll get a crw.
977 */
978 atomic_inc(&chpid_reset_count);
979 }
980 /* Wait for machine check for all channel paths. */
981 timeout = get_clock() + (RCHP_TIMEOUT << 12);
982 while (atomic_read(&chpid_reset_count) != 0) {
983 if (get_clock() > timeout)
984 break;
985 cpu_relax();
986 }
987 /* Disable machine checks again. */
988 local_mcck_disable();
989 /* Disable channel report machine checks. */
990 __ctl_clear_bit(14, 28);
Heiko Carstensab14de62007-02-05 21:18:37 +0100991 s390_base_mcck_handler_fn = NULL;
Heiko Carstens15e9b582006-12-04 15:40:26 +0100992}
993
994static struct reset_call css_reset_call = {
995 .fn = css_reset,
996};
997
998static int __init init_css_reset_call(void)
999{
1000 atomic_set(&chpid_reset_count, 0);
1001 register_reset_call(&css_reset_call);
1002 return 0;
1003}
1004
1005arch_initcall(init_css_reset_call);
1006
1007struct sch_match_id {
1008 struct subchannel_id schid;
1009 struct ccw_dev_id devid;
1010 int rc;
1011};
1012
1013static int __reipl_subchannel_match(struct subchannel_id schid, void *data)
1014{
1015 struct schib schib;
1016 struct sch_match_id *match_id = data;
1017
Michael Holzheua45e1412006-12-15 17:18:22 +01001018 if (stsch_reset(schid, &schib))
Heiko Carstens15e9b582006-12-04 15:40:26 +01001019 return -ENXIO;
1020 if (schib.pmcw.dnv &&
1021 (schib.pmcw.dev == match_id->devid.devno) &&
1022 (schid.ssid == match_id->devid.ssid)) {
1023 match_id->schid = schid;
1024 match_id->rc = 0;
1025 return 1;
1026 }
1027 return 0;
1028}
1029
1030static int reipl_find_schid(struct ccw_dev_id *devid,
1031 struct subchannel_id *schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001033 struct sch_match_id match_id;
1034
1035 match_id.devid = *devid;
1036 match_id.rc = -ENODEV;
Heiko Carstens15e9b582006-12-04 15:40:26 +01001037 for_each_subchannel(__reipl_subchannel_match, &match_id);
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001038 if (match_id.rc == 0)
1039 *schid = match_id.schid;
1040 return match_id.rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001043extern void do_reipl_asm(__u32 schid);
1044
1045/* Make sure all subchannels are quiet before we re-ipl an lpar. */
1046void reipl_ccw_dev(struct ccw_dev_id *devid)
1047{
1048 struct subchannel_id schid;
1049
Heiko Carstens15e9b582006-12-04 15:40:26 +01001050 s390_reset_system();
1051 if (reipl_find_schid(devid, &schid) != 0)
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001052 panic("IPL Device not found\n");
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001053 do_reipl_asm(*((__u32*)&schid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
Heiko Carstense87bfe52006-09-20 15:59:15 +02001055
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001056int __init cio_get_iplinfo(struct cio_iplinfo *iplinfo)
Heiko Carstense87bfe52006-09-20 15:59:15 +02001057{
1058 struct subchannel_id schid;
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001059 struct schib schib;
Heiko Carstense87bfe52006-09-20 15:59:15 +02001060
1061 schid = *(struct subchannel_id *)__LC_SUBCHANNEL_ID;
1062 if (!schid.one)
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001063 return -ENODEV;
1064 if (stsch(schid, &schib))
1065 return -ENODEV;
1066 if (!schib.pmcw.dnv)
1067 return -ENODEV;
1068 iplinfo->devno = schib.pmcw.dev;
1069 iplinfo->is_qdio = schib.pmcw.qf;
1070 return 0;
Heiko Carstense87bfe52006-09-20 15:59:15 +02001071}