blob: 34b38fb7c2f79546fd77dd7fe1bee18fa0bf245e [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 *
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02005 * Copyright IBM Corp. 1999,2008
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>
Peter Oberparleitere5854a52007-04-27 16:01:31 +020025#include <asm/chpid.h>
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010026#include <asm/airq.h>
Cornelia Huck3a3fc292008-07-14 09:58:58 +020027#include <asm/isc.h>
Heiko Carstens43ca5c32008-04-17 07:46:23 +020028#include <asm/cpu.h>
Peter Oberparleiter83262d62008-07-14 09:58:51 +020029#include <asm/fcx.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "cio.h"
31#include "css.h"
32#include "chsc.h"
33#include "ioasm.h"
Cornelia Huckcd6b4f22008-01-26 14:10:43 +010034#include "io_sch.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "blacklist.h"
36#include "cio_debug.h"
Peter Oberparleitere6b6e102007-04-27 16:01:28 +020037#include "chp.h"
Heiko Carstens15e9b582006-12-04 15:40:26 +010038#include "../s390mach.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40debug_info_t *cio_debug_msg_id;
41debug_info_t *cio_debug_trace_id;
42debug_info_t *cio_debug_crw_id;
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * Function: cio_debug_init
Cornelia Huckbc698bc2008-01-26 14:10:42 +010046 * Initializes three debug logs for common I/O:
47 * - cio_msg logs generic cio messages
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * - cio_trace logs the calling of different functions
Cornelia Huckbc698bc2008-01-26 14:10:42 +010049 * - cio_crw logs machine check related cio messages
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
Cornelia Huckbc698bc2008-01-26 14:10:42 +010051static int __init cio_debug_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Peter Tiedemann361f4942008-01-26 14:11:30 +010053 cio_debug_msg_id = debug_register("cio_msg", 16, 1, 16 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if (!cio_debug_msg_id)
55 goto out_unregister;
Cornelia Huckbc698bc2008-01-26 14:10:42 +010056 debug_register_view(cio_debug_msg_id, &debug_sprintf_view);
57 debug_set_level(cio_debug_msg_id, 2);
Peter Tiedemann361f4942008-01-26 14:11:30 +010058 cio_debug_trace_id = debug_register("cio_trace", 16, 1, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (!cio_debug_trace_id)
60 goto out_unregister;
Cornelia Huckbc698bc2008-01-26 14:10:42 +010061 debug_register_view(cio_debug_trace_id, &debug_hex_ascii_view);
62 debug_set_level(cio_debug_trace_id, 2);
Peter Tiedemann361f4942008-01-26 14:11:30 +010063 cio_debug_crw_id = debug_register("cio_crw", 16, 1, 16 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (!cio_debug_crw_id)
65 goto out_unregister;
Cornelia Huckbc698bc2008-01-26 14:10:42 +010066 debug_register_view(cio_debug_crw_id, &debug_sprintf_view);
67 debug_set_level(cio_debug_crw_id, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return 0;
69
70out_unregister:
71 if (cio_debug_msg_id)
Cornelia Huckbc698bc2008-01-26 14:10:42 +010072 debug_unregister(cio_debug_msg_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (cio_debug_trace_id)
Cornelia Huckbc698bc2008-01-26 14:10:42 +010074 debug_unregister(cio_debug_trace_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (cio_debug_crw_id)
Cornelia Huckbc698bc2008-01-26 14:10:42 +010076 debug_unregister(cio_debug_crw_id);
Cornelia Hucke556bbb2007-07-27 12:29:19 +020077 printk(KERN_WARNING"cio: could not initialize debugging\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return -1;
79}
80
81arch_initcall (cio_debug_init);
82
83int
84cio_set_options (struct subchannel *sch, int flags)
85{
86 sch->options.suspend = (flags & DOIO_ALLOW_SUSPEND) != 0;
87 sch->options.prefetch = (flags & DOIO_DENY_PREFETCH) != 0;
88 sch->options.inter = (flags & DOIO_SUPPRESS_INTER) != 0;
89 return 0;
90}
91
92/* FIXME: who wants to use this? */
93int
94cio_get_options (struct subchannel *sch)
95{
96 int flags;
97
98 flags = 0;
99 if (sch->options.suspend)
100 flags |= DOIO_ALLOW_SUSPEND;
101 if (sch->options.prefetch)
102 flags |= DOIO_DENY_PREFETCH;
103 if (sch->options.inter)
104 flags |= DOIO_SUPPRESS_INTER;
105 return flags;
106}
107
108/*
109 * Use tpi to get a pending interrupt, call the interrupt handler and
110 * return a pointer to the subchannel structure.
111 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100112static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113cio_tpi(void)
114{
115 struct tpi_info *tpi_info;
116 struct subchannel *sch;
117 struct irb *irb;
118
119 tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
120 if (tpi (NULL) != 1)
121 return 0;
122 irb = (struct irb *) __LC_IRB;
123 /* Store interrupt response block to lowcore. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800124 if (tsch (tpi_info->schid, irb) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 /* Not status pending or not operational. */
126 return 1;
127 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
128 if (!sch)
129 return 1;
130 local_bh_disable();
131 irq_enter ();
Cornelia Huck2ec22982006-12-08 15:54:26 +0100132 spin_lock(sch->lock);
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200133 memcpy(&sch->schib.scsw, &irb->scsw, sizeof(union scsw));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (sch->driver && sch->driver->irq)
Cornelia Huck602b20f2008-01-26 14:10:39 +0100135 sch->driver->irq(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100136 spin_unlock(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 irq_exit ();
Heiko Carstens1f194a42006-07-03 00:24:46 -0700138 _local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return 1;
140}
141
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100142static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143cio_start_handle_notoper(struct subchannel *sch, __u8 lpm)
144{
145 char dbf_text[15];
146
147 if (lpm != 0)
148 sch->lpm &= ~lpm;
149 else
150 sch->lpm = 0;
151
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800152 stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Michael Ernst139b83d2008-05-07 09:22:54 +0200154 CIO_MSG_EVENT(2, "cio_start: 'not oper' status for "
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800155 "subchannel 0.%x.%04x!\n", sch->schid.ssid,
156 sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 sprintf(dbf_text, "no%s", sch->dev.bus_id);
158 CIO_TRACE_EVENT(0, dbf_text);
159 CIO_HEX_EVENT(0, &sch->schib, sizeof (struct schib));
160
161 return (sch->lpm ? -EACCES : -ENODEV);
162}
163
164int
165cio_start_key (struct subchannel *sch, /* subchannel structure */
166 struct ccw1 * cpa, /* logical channel prog addr */
167 __u8 lpm, /* logical path mask */
168 __u8 key) /* storage key */
169{
170 char dbf_txt[15];
171 int ccode;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200172 union orb *orb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100174 CIO_TRACE_EVENT(4, "stIO");
175 CIO_TRACE_EVENT(4, sch->dev.bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100177 orb = &to_io_private(sch)->orb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 /* sch is always under 2G. */
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200179 orb->cmd.intparm = (u32)(addr_t)sch;
180 orb->cmd.fmt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200182 orb->cmd.pfch = sch->options.prefetch == 0;
183 orb->cmd.spnd = sch->options.suspend;
184 orb->cmd.ssic = sch->options.suspend && sch->options.inter;
185 orb->cmd.lpm = (lpm != 0) ? lpm : sch->lpm;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800186#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /*
188 * for 64 bit we always support 64 bit IDAWs with 4k page size only
189 */
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200190 orb->cmd.c64 = 1;
191 orb->cmd.i2k = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192#endif
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200193 orb->cmd.key = key >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /* issue "Start Subchannel" */
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200195 orb->cmd.cpa = (__u32) __pa(cpa);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100196 ccode = ssch(sch->schid, orb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 /* process condition code */
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100199 sprintf(dbf_txt, "ccode:%d", ccode);
200 CIO_TRACE_EVENT(4, dbf_txt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 switch (ccode) {
203 case 0:
204 /*
205 * initialize device status information
206 */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200207 sch->schib.scsw.cmd.actl |= SCSW_ACTL_START_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return 0;
209 case 1: /* status pending */
210 case 2: /* busy */
211 return -EBUSY;
212 default: /* device/path not operational */
213 return cio_start_handle_notoper(sch, lpm);
214 }
215}
216
217int
218cio_start (struct subchannel *sch, struct ccw1 *cpa, __u8 lpm)
219{
Peter Oberparleiter0b642ed2005-05-01 08:58:58 -0700220 return cio_start_key(sch, cpa, lpm, PAGE_DEFAULT_KEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223/*
224 * resume suspended I/O operation
225 */
226int
227cio_resume (struct subchannel *sch)
228{
229 char dbf_txt[15];
230 int ccode;
231
232 CIO_TRACE_EVENT (4, "resIO");
233 CIO_TRACE_EVENT (4, sch->dev.bus_id);
234
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800235 ccode = rsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 sprintf (dbf_txt, "ccode:%d", ccode);
238 CIO_TRACE_EVENT (4, dbf_txt);
239
240 switch (ccode) {
241 case 0:
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200242 sch->schib.scsw.cmd.actl |= SCSW_ACTL_RESUME_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return 0;
244 case 1:
245 return -EBUSY;
246 case 2:
247 return -EINVAL;
248 default:
249 /*
250 * useless to wait for request completion
251 * as device is no longer operational !
252 */
253 return -ENODEV;
254 }
255}
256
257/*
258 * halt I/O operation
259 */
260int
261cio_halt(struct subchannel *sch)
262{
263 char dbf_txt[15];
264 int ccode;
265
266 if (!sch)
267 return -ENODEV;
268
269 CIO_TRACE_EVENT (2, "haltIO");
270 CIO_TRACE_EVENT (2, sch->dev.bus_id);
271
272 /*
273 * Issue "Halt subchannel" and process condition code
274 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800275 ccode = hsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 sprintf (dbf_txt, "ccode:%d", ccode);
278 CIO_TRACE_EVENT (2, dbf_txt);
279
280 switch (ccode) {
281 case 0:
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200282 sch->schib.scsw.cmd.actl |= SCSW_ACTL_HALT_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return 0;
284 case 1: /* status pending */
285 case 2: /* busy */
286 return -EBUSY;
287 default: /* device not operational */
288 return -ENODEV;
289 }
290}
291
292/*
293 * Clear I/O operation
294 */
295int
296cio_clear(struct subchannel *sch)
297{
298 char dbf_txt[15];
299 int ccode;
300
301 if (!sch)
302 return -ENODEV;
303
304 CIO_TRACE_EVENT (2, "clearIO");
305 CIO_TRACE_EVENT (2, sch->dev.bus_id);
306
307 /*
308 * Issue "Clear subchannel" and process condition code
309 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800310 ccode = csch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 sprintf (dbf_txt, "ccode:%d", ccode);
313 CIO_TRACE_EVENT (2, dbf_txt);
314
315 switch (ccode) {
316 case 0:
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200317 sch->schib.scsw.cmd.actl |= SCSW_ACTL_CLEAR_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return 0;
319 default: /* device not operational */
320 return -ENODEV;
321 }
322}
323
324/*
325 * Function: cio_cancel
326 * Issues a "Cancel Subchannel" on the specified subchannel
327 * Note: We don't need any fancy intparms and flags here
328 * since xsch is executed synchronously.
329 * Only for common I/O internal use as for now.
330 */
331int
332cio_cancel (struct subchannel *sch)
333{
334 char dbf_txt[15];
335 int ccode;
336
337 if (!sch)
338 return -ENODEV;
339
340 CIO_TRACE_EVENT (2, "cancelIO");
341 CIO_TRACE_EVENT (2, sch->dev.bus_id);
342
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800343 ccode = xsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 sprintf (dbf_txt, "ccode:%d", ccode);
346 CIO_TRACE_EVENT (2, dbf_txt);
347
348 switch (ccode) {
349 case 0: /* success */
350 /* Update information in scsw. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800351 stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return 0;
353 case 1: /* status pending */
354 return -EBUSY;
355 case 2: /* not applicable */
356 return -EINVAL;
357 default: /* not oper */
358 return -ENODEV;
359 }
360}
361
362/*
363 * Function: cio_modify
364 * Issues a "Modify Subchannel" on the specified subchannel
365 */
366int
367cio_modify (struct subchannel *sch)
368{
369 int ccode, retry, ret;
370
371 ret = 0;
372 for (retry = 0; retry < 5; retry++) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800373 ccode = msch_err (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (ccode < 0) /* -EIO if msch gets a program check. */
375 return ccode;
376 switch (ccode) {
377 case 0: /* successfull */
378 return 0;
379 case 1: /* status pending */
380 return -EBUSY;
381 case 2: /* busy */
382 udelay (100); /* allow for recovery */
383 ret = -EBUSY;
384 break;
385 case 3: /* not operational */
386 return -ENODEV;
387 }
388 }
389 return ret;
390}
391
Cornelia Huck44a1c192008-07-14 09:58:47 +0200392/**
393 * cio_enable_subchannel - enable a subchannel.
394 * @sch: subchannel to be enabled
395 * @intparm: interruption parameter to set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 */
Cornelia Huckedf22092008-04-30 13:38:39 +0200397int cio_enable_subchannel(struct subchannel *sch, u32 intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 char dbf_txt[15];
400 int ccode;
401 int retry;
402 int ret;
403
404 CIO_TRACE_EVENT (2, "ensch");
405 CIO_TRACE_EVENT (2, sch->dev.bus_id);
406
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100407 if (sch_is_pseudo_sch(sch))
408 return -EINVAL;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800409 ccode = stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (ccode)
411 return -ENODEV;
412
413 for (retry = 5, ret = 0; retry > 0; retry--) {
414 sch->schib.pmcw.ena = 1;
Cornelia Huckedf22092008-04-30 13:38:39 +0200415 sch->schib.pmcw.isc = sch->isc;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100416 sch->schib.pmcw.intparm = intparm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 ret = cio_modify(sch);
418 if (ret == -ENODEV)
419 break;
420 if (ret == -EIO)
421 /*
422 * Got a program check in cio_modify. Try without
423 * the concurrent sense bit the next time.
424 */
425 sch->schib.pmcw.csense = 0;
426 if (ret == 0) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800427 stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (sch->schib.pmcw.ena)
429 break;
430 }
431 if (ret == -EBUSY) {
432 struct irb irb;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800433 if (tsch(sch->schid, &irb) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 break;
435 }
436 }
437 sprintf (dbf_txt, "ret:%d", ret);
438 CIO_TRACE_EVENT (2, dbf_txt);
439 return ret;
440}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200441EXPORT_SYMBOL_GPL(cio_enable_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Cornelia Huck44a1c192008-07-14 09:58:47 +0200443/**
444 * cio_disable_subchannel - disable a subchannel.
445 * @sch: subchannel to disable
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 */
Cornelia Huck44a1c192008-07-14 09:58:47 +0200447int cio_disable_subchannel(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 char dbf_txt[15];
450 int ccode;
451 int retry;
452 int ret;
453
454 CIO_TRACE_EVENT (2, "dissch");
455 CIO_TRACE_EVENT (2, sch->dev.bus_id);
456
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100457 if (sch_is_pseudo_sch(sch))
458 return 0;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800459 ccode = stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (ccode == 3) /* Not operational. */
461 return -ENODEV;
462
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200463 if (scsw_actl(&sch->schib.scsw) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /*
465 * the disable function must not be called while there are
466 * requests pending for completion !
467 */
468 return -EBUSY;
469
470 for (retry = 5, ret = 0; retry > 0; retry--) {
471 sch->schib.pmcw.ena = 0;
472 ret = cio_modify(sch);
473 if (ret == -ENODEV)
474 break;
475 if (ret == -EBUSY)
476 /*
477 * The subchannel is busy or status pending.
478 * We'll disable when the next interrupt was delivered
479 * via the state machine.
480 */
481 break;
482 if (ret == 0) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800483 stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (!sch->schib.pmcw.ena)
485 break;
486 }
487 }
488 sprintf (dbf_txt, "ret:%d", ret);
489 CIO_TRACE_EVENT (2, dbf_txt);
490 return ret;
491}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200492EXPORT_SYMBOL_GPL(cio_disable_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100494int cio_create_sch_lock(struct subchannel *sch)
Cornelia Huck2ec22982006-12-08 15:54:26 +0100495{
496 sch->lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
497 if (!sch->lock)
498 return -ENOMEM;
499 spin_lock_init(sch->lock);
500 return 0;
501}
502
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200503static int cio_check_devno_blacklisted(struct subchannel *sch)
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200504{
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200505 if (is_blacklisted(sch->schid.ssid, sch->schib.pmcw.dev)) {
506 /*
507 * This device must not be known to Linux. So we simply
508 * say that there is no device and return ENODEV.
509 */
510 CIO_MSG_EVENT(6, "Blacklisted device detected "
511 "at devno %04X, subchannel set %x\n",
512 sch->schib.pmcw.dev, sch->schid.ssid);
513 return -ENODEV;
514 }
515 return 0;
516}
517
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200518static int cio_validate_io_subchannel(struct subchannel *sch)
519{
520 /* Initialization for io subchannels. */
521 if (!css_sch_is_valid(&sch->schib))
522 return -ENODEV;
523
524 /* Devno is valid. */
525 return cio_check_devno_blacklisted(sch);
526}
527
528static int cio_validate_msg_subchannel(struct subchannel *sch)
529{
530 /* Initialization for message subchannels. */
531 if (!css_sch_is_valid(&sch->schib))
532 return -ENODEV;
533
534 /* Devno is valid. */
535 return cio_check_devno_blacklisted(sch);
536}
537
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200538/**
539 * cio_validate_subchannel - basic validation of subchannel
540 * @sch: subchannel structure to be filled out
541 * @schid: subchannel id
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 *
543 * Find out subchannel type and initialize struct subchannel.
544 * Return codes:
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200545 * 0 on success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 * -ENXIO for non-defined subchannels
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200547 * -ENODEV for invalid subchannels or blacklisted devices
548 * -EIO for subchannels in an invalid subchannel set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200550int cio_validate_subchannel(struct subchannel *sch, struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 char dbf_txt[15];
553 int ccode;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100554 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200556 sprintf(dbf_txt, "valsch%x", schid.sch_no);
557 CIO_TRACE_EVENT(4, dbf_txt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 /* Nuke all fields. */
560 memset(sch, 0, sizeof(struct subchannel));
561
Cornelia Huck2ec22982006-12-08 15:54:26 +0100562 sch->schid = schid;
563 if (cio_is_console(schid)) {
564 sch->lock = cio_get_console_lock();
565 } else {
566 err = cio_create_sch_lock(sch);
567 if (err)
568 goto out;
569 }
Cornelia Huck6ab48792006-07-12 16:39:50 +0200570 mutex_init(&sch->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* Set a name for the subchannel */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800572 snprintf (sch->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x", schid.ssid,
573 schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 /*
576 * The first subchannel that is not-operational (ccode==3)
577 * indicates that there aren't any more devices available.
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800578 * If stsch gets an exception, it means the current subchannel set
579 * is not valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800581 ccode = stsch_err (schid, &sch->schib);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100582 if (ccode) {
583 err = (ccode == 3) ? -ENXIO : ccode;
584 goto out;
585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 /* Copy subchannel type from path management control word. */
587 sch->st = sch->schib.pmcw.st;
Cornelia Huckc820de32008-07-14 09:58:45 +0200588
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200589 switch (sch->st) {
590 case SUBCHANNEL_TYPE_IO:
591 err = cio_validate_io_subchannel(sch);
592 break;
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200593 case SUBCHANNEL_TYPE_MSG:
594 err = cio_validate_msg_subchannel(sch);
595 break;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200596 default:
597 err = 0;
598 }
599 if (err)
Cornelia Huck2ec22982006-12-08 15:54:26 +0100600 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200602 CIO_MSG_EVENT(4, "Subchannel 0.%x.%04x reports subchannel type %04X\n",
603 sch->schid.ssid, sch->schid.sch_no, sch->st);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return 0;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100605out:
606 if (!cio_is_console(schid))
607 kfree(sch->lock);
608 sch->lock = NULL;
609 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
612/*
613 * do_IRQ() handles all normal I/O device IRQ's (the special
614 * SMP cross-CPU interrupts have their own specific
615 * handlers).
616 *
617 */
618void
619do_IRQ (struct pt_regs *regs)
620{
621 struct tpi_info *tpi_info;
622 struct subchannel *sch;
623 struct irb *irb;
Heiko Carstens5a489b92006-10-06 16:38:35 +0200624 struct pt_regs *old_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Heiko Carstens5a489b92006-10-06 16:38:35 +0200626 old_regs = set_irq_regs(regs);
Heiko Carstens9d0a57c2006-10-11 15:31:26 +0200627 irq_enter();
Heiko Carstens43ca5c32008-04-17 07:46:23 +0200628 s390_idle_check();
Heiko Carstens5a62b192008-04-17 07:46:25 +0200629 if (S390_lowcore.int_clock >= S390_lowcore.clock_comparator)
630 /* Serve timer interrupts first. */
631 clock_comparator_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /*
633 * Get interrupt information from lowcore
634 */
635 tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
636 irb = (struct irb *) __LC_IRB;
637 do {
638 kstat_cpu(smp_processor_id()).irqs[IO_INTERRUPT]++;
639 /*
640 * Non I/O-subchannel thin interrupts are processed differently
641 */
642 if (tpi_info->adapter_IO == 1 &&
643 tpi_info->int_type == IO_INTERRUPT_TYPE) {
Cornelia Huckda7c5af2008-07-14 09:58:59 +0200644 do_adapter_IO(tpi_info->isc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 continue;
646 }
647 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
Heiko Carstensa8061702008-04-17 07:46:26 +0200648 if (!sch) {
649 /* Clear pending interrupt condition. */
650 tsch(tpi_info->schid, irb);
651 continue;
652 }
653 spin_lock(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /* Store interrupt response block to lowcore. */
Heiko Carstensa8061702008-04-17 07:46:26 +0200655 if (tsch(tpi_info->schid, irb) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /* Keep subchannel information word up to date. */
657 memcpy (&sch->schib.scsw, &irb->scsw,
658 sizeof (irb->scsw));
659 /* Call interrupt handler if there is one. */
660 if (sch->driver && sch->driver->irq)
Cornelia Huck602b20f2008-01-26 14:10:39 +0100661 sch->driver->irq(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
Heiko Carstensa8061702008-04-17 07:46:26 +0200663 spin_unlock(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /*
665 * Are more interrupts pending?
666 * If so, the tpi instruction will update the lowcore
667 * to hold the info for the next interrupt.
668 * We don't do this for VM because a tpi drops the cpu
669 * out of the sie which costs more cycles than it saves.
670 */
671 } while (!MACHINE_IS_VM && tpi (NULL) != 0);
Heiko Carstens9d0a57c2006-10-11 15:31:26 +0200672 irq_exit();
Heiko Carstens5a489b92006-10-06 16:38:35 +0200673 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
676#ifdef CONFIG_CCW_CONSOLE
677static struct subchannel console_subchannel;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100678static struct io_subchannel_private console_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679static int console_subchannel_in_use;
680
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100681void *cio_get_console_priv(void)
682{
683 return &console_priv;
684}
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686/*
687 * busy wait for the next interrupt on the console
688 */
Heiko Carstensa8061702008-04-17 07:46:26 +0200689void wait_cons_dev(void)
690 __releases(console_subchannel.lock)
691 __acquires(console_subchannel.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 unsigned long cr6 __attribute__ ((aligned (8)));
694 unsigned long save_cr6 __attribute__ ((aligned (8)));
695
696 /*
697 * before entering the spinlock we may already have
698 * processed the interrupt on a different CPU...
699 */
700 if (!console_subchannel_in_use)
701 return;
702
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200703 /* disable all but the console isc */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 __ctl_store (save_cr6, 6, 6);
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200705 cr6 = 1UL << (31 - CONSOLE_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 __ctl_load (cr6, 6, 6);
707
708 do {
Cornelia Huck2ec22982006-12-08 15:54:26 +0100709 spin_unlock(console_subchannel.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (!cio_tpi())
711 cpu_relax();
Cornelia Huck2ec22982006-12-08 15:54:26 +0100712 spin_lock(console_subchannel.lock);
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200713 } while (console_subchannel.schib.scsw.cmd.actl != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /*
715 * restore previous isc value
716 */
717 __ctl_load (save_cr6, 6, 6);
718}
719
720static int
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800721cio_test_for_console(struct subchannel_id schid, void *data)
722{
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800723 if (stsch_err(schid, &console_subchannel.schib) != 0)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800724 return -ENXIO;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100725 if ((console_subchannel.schib.pmcw.st == SUBCHANNEL_TYPE_IO) &&
726 console_subchannel.schib.pmcw.dnv &&
727 (console_subchannel.schib.pmcw.dev == console_devno)) {
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800728 console_irq = schid.sch_no;
729 return 1; /* found */
730 }
731 return 0;
732}
733
734
735static int
736cio_get_console_sch_no(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800738 struct subchannel_id schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800740 init_subchannel_id(&schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (console_irq != -1) {
742 /* VM provided us with the irq number of the console. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800743 schid.sch_no = console_irq;
744 if (stsch(schid, &console_subchannel.schib) != 0 ||
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100745 (console_subchannel.schib.pmcw.st != SUBCHANNEL_TYPE_IO) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 !console_subchannel.schib.pmcw.dnv)
747 return -1;
748 console_devno = console_subchannel.schib.pmcw.dev;
749 } else if (console_devno != -1) {
750 /* At least the console device number is known. */
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800751 for_each_subchannel(cio_test_for_console, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (console_irq == -1)
753 return -1;
754 } else {
755 /* unlike in 2.4, we cannot autoprobe here, since
756 * the channel subsystem is not fully initialized.
757 * With some luck, the HWC console can take over */
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200758 printk(KERN_WARNING "cio: No ccw console found!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return -1;
760 }
761 return console_irq;
762}
763
764struct subchannel *
765cio_probe_console(void)
766{
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800767 int sch_no, ret;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800768 struct subchannel_id schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 if (xchg(&console_subchannel_in_use, 1) != 0)
771 return ERR_PTR(-EBUSY);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800772 sch_no = cio_get_console_sch_no();
773 if (sch_no == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 console_subchannel_in_use = 0;
775 return ERR_PTR(-ENODEV);
776 }
777 memset(&console_subchannel, 0, sizeof(struct subchannel));
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800778 init_subchannel_id(&schid);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800779 schid.sch_no = sch_no;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800780 ret = cio_validate_subchannel(&console_subchannel, schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (ret) {
782 console_subchannel_in_use = 0;
783 return ERR_PTR(-ENODEV);
784 }
785
786 /*
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200787 * enable console I/O-interrupt subclass
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 */
Cornelia Huck6ef556c2008-07-14 09:59:01 +0200789 isc_register(CONSOLE_ISC);
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200790 console_subchannel.schib.pmcw.isc = CONSOLE_ISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 console_subchannel.schib.pmcw.intparm =
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100792 (u32)(addr_t)&console_subchannel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 ret = cio_modify(&console_subchannel);
794 if (ret) {
Cornelia Huck6ef556c2008-07-14 09:59:01 +0200795 isc_unregister(CONSOLE_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 console_subchannel_in_use = 0;
797 return ERR_PTR(ret);
798 }
799 return &console_subchannel;
800}
801
802void
803cio_release_console(void)
804{
805 console_subchannel.schib.pmcw.intparm = 0;
806 cio_modify(&console_subchannel);
Cornelia Huck6ef556c2008-07-14 09:59:01 +0200807 isc_unregister(CONSOLE_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 console_subchannel_in_use = 0;
809}
810
811/* Bah... hack to catch console special sausages. */
812int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800813cio_is_console(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
815 if (!console_subchannel_in_use)
816 return 0;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800817 return schid_equal(&schid, &console_subchannel.schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
820struct subchannel *
821cio_get_console_subchannel(void)
822{
823 if (!console_subchannel_in_use)
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200824 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return &console_subchannel;
826}
827
828#endif
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100829static int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800830__disable_subchannel_easy(struct subchannel_id schid, struct schib *schib)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
832 int retry, cc;
833
834 cc = 0;
835 for (retry=0;retry<3;retry++) {
836 schib->pmcw.ena = 0;
837 cc = msch(schid, schib);
838 if (cc)
839 return (cc==3?-ENODEV:-EBUSY);
840 stsch(schid, schib);
841 if (!schib->pmcw.ena)
842 return 0;
843 }
844 return -EBUSY; /* uhm... */
845}
846
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100847/* we can't use the normal udelay here, since it enables external interrupts */
848
849static void udelay_reset(unsigned long usecs)
850{
851 uint64_t start_cc, end_cc;
852
853 asm volatile ("STCK %0" : "=m" (start_cc));
854 do {
855 cpu_relax();
856 asm volatile ("STCK %0" : "=m" (end_cc));
857 } while (((end_cc - start_cc)/4096) < usecs);
858}
859
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100860static int
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200861__clear_io_subchannel_easy(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 int retry;
864
865 if (csch(schid))
866 return -ENODEV;
867 for (retry=0;retry<20;retry++) {
868 struct tpi_info ti;
869
870 if (tpi(&ti)) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800871 tsch(ti.schid, (struct irb *)__LC_IRB);
872 if (schid_equal(&ti.schid, &schid))
Cornelia Huck4c24da72005-09-03 15:58:01 -0700873 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100875 udelay_reset(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877 return -EBUSY;
878}
879
Michael Holzheua45e1412006-12-15 17:18:22 +0100880static int pgm_check_occured;
881
882static void cio_reset_pgm_check_handler(void)
883{
884 pgm_check_occured = 1;
885}
886
887static int stsch_reset(struct subchannel_id schid, volatile struct schib *addr)
888{
889 int rc;
890
891 pgm_check_occured = 0;
Heiko Carstensab14de62007-02-05 21:18:37 +0100892 s390_base_pgm_handler_fn = cio_reset_pgm_check_handler;
Heiko Carstens6faf4442007-01-09 10:18:41 +0100893 rc = stsch(schid, addr);
Heiko Carstensab14de62007-02-05 21:18:37 +0100894 s390_base_pgm_handler_fn = NULL;
Heiko Carstens6faf4442007-01-09 10:18:41 +0100895
Heiko Carstensab14de62007-02-05 21:18:37 +0100896 /* The program check handler could have changed pgm_check_occured. */
Heiko Carstens6faf4442007-01-09 10:18:41 +0100897 barrier();
898
Michael Holzheua45e1412006-12-15 17:18:22 +0100899 if (pgm_check_occured)
900 return -EIO;
901 else
902 return rc;
903}
904
Heiko Carstens15e9b582006-12-04 15:40:26 +0100905static int __shutdown_subchannel_easy(struct subchannel_id schid, void *data)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800906{
907 struct schib schib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Michael Holzheua45e1412006-12-15 17:18:22 +0100909 if (stsch_reset(schid, &schib))
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800910 return -ENXIO;
911 if (!schib.pmcw.ena)
912 return 0;
913 switch(__disable_subchannel_easy(schid, &schib)) {
914 case 0:
915 case -ENODEV:
916 break;
917 default: /* -EBUSY */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200918 switch (schib.pmcw.st) {
919 case SUBCHANNEL_TYPE_IO:
920 if (__clear_io_subchannel_easy(schid))
921 goto out; /* give up... */
922 break;
923 default:
924 /* No default clear strategy */
925 break;
926 }
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800927 stsch(schid, &schib);
928 __disable_subchannel_easy(schid, &schib);
929 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200930out:
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800931 return 0;
932}
933
Heiko Carstens15e9b582006-12-04 15:40:26 +0100934static atomic_t chpid_reset_count;
935
936static void s390_reset_chpids_mcck_handler(void)
937{
938 struct crw crw;
939 struct mci *mci;
940
941 /* Check for pending channel report word. */
942 mci = (struct mci *)&S390_lowcore.mcck_interruption_code;
943 if (!mci->cp)
944 return;
945 /* Process channel report words. */
946 while (stcrw(&crw) == 0) {
947 /* Check for responses to RCHP. */
948 if (crw.slct && crw.rsc == CRW_RSC_CPATH)
949 atomic_dec(&chpid_reset_count);
950 }
951}
952
953#define RCHP_TIMEOUT (30 * USEC_PER_SEC)
954static void css_reset(void)
955{
956 int i, ret;
957 unsigned long long timeout;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200958 struct chp_id chpid;
Heiko Carstens15e9b582006-12-04 15:40:26 +0100959
960 /* Reset subchannels. */
961 for_each_subchannel(__shutdown_subchannel_easy, NULL);
962 /* Reset channel paths. */
Heiko Carstensab14de62007-02-05 21:18:37 +0100963 s390_base_mcck_handler_fn = s390_reset_chpids_mcck_handler;
Heiko Carstens15e9b582006-12-04 15:40:26 +0100964 /* Enable channel report machine checks. */
965 __ctl_set_bit(14, 28);
966 /* Temporarily reenable machine checks. */
967 local_mcck_enable();
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200968 chp_id_init(&chpid);
Heiko Carstens15e9b582006-12-04 15:40:26 +0100969 for (i = 0; i <= __MAX_CHPID; i++) {
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200970 chpid.id = i;
971 ret = rchp(chpid);
Heiko Carstens15e9b582006-12-04 15:40:26 +0100972 if ((ret == 0) || (ret == 2))
973 /*
974 * rchp either succeeded, or another rchp is already
975 * in progress. In either case, we'll get a crw.
976 */
977 atomic_inc(&chpid_reset_count);
978 }
979 /* Wait for machine check for all channel paths. */
980 timeout = get_clock() + (RCHP_TIMEOUT << 12);
981 while (atomic_read(&chpid_reset_count) != 0) {
982 if (get_clock() > timeout)
983 break;
984 cpu_relax();
985 }
986 /* Disable machine checks again. */
987 local_mcck_disable();
988 /* Disable channel report machine checks. */
989 __ctl_clear_bit(14, 28);
Heiko Carstensab14de62007-02-05 21:18:37 +0100990 s390_base_mcck_handler_fn = NULL;
Heiko Carstens15e9b582006-12-04 15:40:26 +0100991}
992
993static struct reset_call css_reset_call = {
994 .fn = css_reset,
995};
996
997static int __init init_css_reset_call(void)
998{
999 atomic_set(&chpid_reset_count, 0);
1000 register_reset_call(&css_reset_call);
1001 return 0;
1002}
1003
1004arch_initcall(init_css_reset_call);
1005
1006struct sch_match_id {
1007 struct subchannel_id schid;
1008 struct ccw_dev_id devid;
1009 int rc;
1010};
1011
1012static int __reipl_subchannel_match(struct subchannel_id schid, void *data)
1013{
1014 struct schib schib;
1015 struct sch_match_id *match_id = data;
1016
Michael Holzheua45e1412006-12-15 17:18:22 +01001017 if (stsch_reset(schid, &schib))
Heiko Carstens15e9b582006-12-04 15:40:26 +01001018 return -ENXIO;
Cornelia Huckb279a4f2008-01-26 14:10:45 +01001019 if ((schib.pmcw.st == SUBCHANNEL_TYPE_IO) && schib.pmcw.dnv &&
Heiko Carstens15e9b582006-12-04 15:40:26 +01001020 (schib.pmcw.dev == match_id->devid.devno) &&
1021 (schid.ssid == match_id->devid.ssid)) {
1022 match_id->schid = schid;
1023 match_id->rc = 0;
1024 return 1;
1025 }
1026 return 0;
1027}
1028
1029static int reipl_find_schid(struct ccw_dev_id *devid,
1030 struct subchannel_id *schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001032 struct sch_match_id match_id;
1033
1034 match_id.devid = *devid;
1035 match_id.rc = -ENODEV;
Heiko Carstens15e9b582006-12-04 15:40:26 +01001036 for_each_subchannel(__reipl_subchannel_match, &match_id);
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001037 if (match_id.rc == 0)
1038 *schid = match_id.schid;
1039 return match_id.rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001042extern void do_reipl_asm(__u32 schid);
1043
1044/* Make sure all subchannels are quiet before we re-ipl an lpar. */
1045void reipl_ccw_dev(struct ccw_dev_id *devid)
1046{
1047 struct subchannel_id schid;
1048
Heiko Carstens15e9b582006-12-04 15:40:26 +01001049 s390_reset_system();
1050 if (reipl_find_schid(devid, &schid) != 0)
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001051 panic("IPL Device not found\n");
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001052 do_reipl_asm(*((__u32*)&schid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
Heiko Carstense87bfe52006-09-20 15:59:15 +02001054
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001055int __init cio_get_iplinfo(struct cio_iplinfo *iplinfo)
Heiko Carstense87bfe52006-09-20 15:59:15 +02001056{
1057 struct subchannel_id schid;
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001058 struct schib schib;
Heiko Carstense87bfe52006-09-20 15:59:15 +02001059
1060 schid = *(struct subchannel_id *)__LC_SUBCHANNEL_ID;
1061 if (!schid.one)
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001062 return -ENODEV;
1063 if (stsch(schid, &schib))
1064 return -ENODEV;
Cornelia Huckb279a4f2008-01-26 14:10:45 +01001065 if (schib.pmcw.st != SUBCHANNEL_TYPE_IO)
1066 return -ENODEV;
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001067 if (!schib.pmcw.dnv)
1068 return -ENODEV;
1069 iplinfo->devno = schib.pmcw.dev;
1070 iplinfo->is_qdio = schib.pmcw.qf;
1071 return 0;
Heiko Carstense87bfe52006-09-20 15:59:15 +02001072}
Peter Oberparleiter83262d62008-07-14 09:58:51 +02001073
1074/**
1075 * cio_tm_start_key - perform start function
1076 * @sch: subchannel on which to perform the start function
1077 * @tcw: transport-command word to be started
1078 * @lpm: mask of paths to use
1079 * @key: storage key to use for storage access
1080 *
1081 * Start the tcw on the given subchannel. Return zero on success, non-zero
1082 * otherwise.
1083 */
1084int cio_tm_start_key(struct subchannel *sch, struct tcw *tcw, u8 lpm, u8 key)
1085{
1086 int cc;
1087 union orb *orb = &to_io_private(sch)->orb;
1088
1089 memset(orb, 0, sizeof(union orb));
1090 orb->tm.intparm = (u32) (addr_t) sch;
1091 orb->tm.key = key >> 4;
1092 orb->tm.b = 1;
1093 orb->tm.lpm = lpm ? lpm : sch->lpm;
1094 orb->tm.tcw = (u32) (addr_t) tcw;
1095 cc = ssch(sch->schid, orb);
1096 switch (cc) {
1097 case 0:
1098 return 0;
1099 case 1:
1100 case 2:
1101 return -EBUSY;
1102 default:
1103 return cio_start_handle_notoper(sch, lpm);
1104 }
1105}
1106
1107/**
1108 * cio_tm_intrg - perform interrogate function
1109 * @sch - subchannel on which to perform the interrogate function
1110 *
1111 * If the specified subchannel is running in transport-mode, perform the
1112 * interrogate function. Return zero on success, non-zero otherwie.
1113 */
1114int cio_tm_intrg(struct subchannel *sch)
1115{
1116 int cc;
1117
1118 if (!to_io_private(sch)->orb.tm.b)
1119 return -EINVAL;
1120 cc = xsch(sch->schid);
1121 switch (cc) {
1122 case 0:
1123 case 2:
1124 return 0;
1125 case 1:
1126 return -EBUSY;
1127 default:
1128 return -ENODEV;
1129 }
1130}