blob: 5954b905e3cab9723116646069356ad7928c31d8 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return -1;
78}
79
80arch_initcall (cio_debug_init);
81
82int
83cio_set_options (struct subchannel *sch, int flags)
84{
85 sch->options.suspend = (flags & DOIO_ALLOW_SUSPEND) != 0;
86 sch->options.prefetch = (flags & DOIO_DENY_PREFETCH) != 0;
87 sch->options.inter = (flags & DOIO_SUPPRESS_INTER) != 0;
88 return 0;
89}
90
91/* FIXME: who wants to use this? */
92int
93cio_get_options (struct subchannel *sch)
94{
95 int flags;
96
97 flags = 0;
98 if (sch->options.suspend)
99 flags |= DOIO_ALLOW_SUSPEND;
100 if (sch->options.prefetch)
101 flags |= DOIO_DENY_PREFETCH;
102 if (sch->options.inter)
103 flags |= DOIO_SUPPRESS_INTER;
104 return flags;
105}
106
107/*
108 * Use tpi to get a pending interrupt, call the interrupt handler and
109 * return a pointer to the subchannel structure.
110 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100111static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112cio_tpi(void)
113{
114 struct tpi_info *tpi_info;
115 struct subchannel *sch;
116 struct irb *irb;
117
118 tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
119 if (tpi (NULL) != 1)
120 return 0;
121 irb = (struct irb *) __LC_IRB;
122 /* Store interrupt response block to lowcore. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800123 if (tsch (tpi_info->schid, irb) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 /* Not status pending or not operational. */
125 return 1;
126 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
127 if (!sch)
128 return 1;
129 local_bh_disable();
130 irq_enter ();
Cornelia Huck2ec22982006-12-08 15:54:26 +0100131 spin_lock(sch->lock);
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200132 memcpy(&sch->schib.scsw, &irb->scsw, sizeof(union scsw));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (sch->driver && sch->driver->irq)
Cornelia Huck602b20f2008-01-26 14:10:39 +0100134 sch->driver->irq(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100135 spin_unlock(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 irq_exit ();
Heiko Carstens1f194a42006-07-03 00:24:46 -0700137 _local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return 1;
139}
140
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100141static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142cio_start_handle_notoper(struct subchannel *sch, __u8 lpm)
143{
144 char dbf_text[15];
145
146 if (lpm != 0)
147 sch->lpm &= ~lpm;
148 else
149 sch->lpm = 0;
150
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800151 stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Michael Ernst139b83d2008-05-07 09:22:54 +0200153 CIO_MSG_EVENT(2, "cio_start: 'not oper' status for "
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800154 "subchannel 0.%x.%04x!\n", sch->schid.ssid,
155 sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 sprintf(dbf_text, "no%s", sch->dev.bus_id);
157 CIO_TRACE_EVENT(0, dbf_text);
158 CIO_HEX_EVENT(0, &sch->schib, sizeof (struct schib));
159
160 return (sch->lpm ? -EACCES : -ENODEV);
161}
162
163int
164cio_start_key (struct subchannel *sch, /* subchannel structure */
165 struct ccw1 * cpa, /* logical channel prog addr */
166 __u8 lpm, /* logical path mask */
167 __u8 key) /* storage key */
168{
169 char dbf_txt[15];
170 int ccode;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200171 union orb *orb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100173 CIO_TRACE_EVENT(4, "stIO");
174 CIO_TRACE_EVENT(4, sch->dev.bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100176 orb = &to_io_private(sch)->orb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 /* sch is always under 2G. */
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200178 orb->cmd.intparm = (u32)(addr_t)sch;
179 orb->cmd.fmt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200181 orb->cmd.pfch = sch->options.prefetch == 0;
182 orb->cmd.spnd = sch->options.suspend;
183 orb->cmd.ssic = sch->options.suspend && sch->options.inter;
184 orb->cmd.lpm = (lpm != 0) ? lpm : sch->lpm;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800185#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 /*
187 * for 64 bit we always support 64 bit IDAWs with 4k page size only
188 */
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200189 orb->cmd.c64 = 1;
190 orb->cmd.i2k = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191#endif
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200192 orb->cmd.key = key >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /* issue "Start Subchannel" */
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200194 orb->cmd.cpa = (__u32) __pa(cpa);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100195 ccode = ssch(sch->schid, orb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 /* process condition code */
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100198 sprintf(dbf_txt, "ccode:%d", ccode);
199 CIO_TRACE_EVENT(4, dbf_txt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 switch (ccode) {
202 case 0:
203 /*
204 * initialize device status information
205 */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200206 sch->schib.scsw.cmd.actl |= SCSW_ACTL_START_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return 0;
208 case 1: /* status pending */
209 case 2: /* busy */
210 return -EBUSY;
Cornelia Huckc91ebe42008-09-09 12:38:58 +0200211 case 3: /* device/path not operational */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return cio_start_handle_notoper(sch, lpm);
Cornelia Huckc91ebe42008-09-09 12:38:58 +0200213 default:
214 return ccode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216}
217
218int
219cio_start (struct subchannel *sch, struct ccw1 *cpa, __u8 lpm)
220{
Peter Oberparleiter0b642ed2005-05-01 08:58:58 -0700221 return cio_start_key(sch, cpa, lpm, PAGE_DEFAULT_KEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224/*
225 * resume suspended I/O operation
226 */
227int
228cio_resume (struct subchannel *sch)
229{
230 char dbf_txt[15];
231 int ccode;
232
233 CIO_TRACE_EVENT (4, "resIO");
234 CIO_TRACE_EVENT (4, sch->dev.bus_id);
235
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800236 ccode = rsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 sprintf (dbf_txt, "ccode:%d", ccode);
239 CIO_TRACE_EVENT (4, dbf_txt);
240
241 switch (ccode) {
242 case 0:
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200243 sch->schib.scsw.cmd.actl |= SCSW_ACTL_RESUME_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return 0;
245 case 1:
246 return -EBUSY;
247 case 2:
248 return -EINVAL;
249 default:
250 /*
251 * useless to wait for request completion
252 * as device is no longer operational !
253 */
254 return -ENODEV;
255 }
256}
257
258/*
259 * halt I/O operation
260 */
261int
262cio_halt(struct subchannel *sch)
263{
264 char dbf_txt[15];
265 int ccode;
266
267 if (!sch)
268 return -ENODEV;
269
270 CIO_TRACE_EVENT (2, "haltIO");
271 CIO_TRACE_EVENT (2, sch->dev.bus_id);
272
273 /*
274 * Issue "Halt subchannel" and process condition code
275 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800276 ccode = hsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 sprintf (dbf_txt, "ccode:%d", ccode);
279 CIO_TRACE_EVENT (2, dbf_txt);
280
281 switch (ccode) {
282 case 0:
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200283 sch->schib.scsw.cmd.actl |= SCSW_ACTL_HALT_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return 0;
285 case 1: /* status pending */
286 case 2: /* busy */
287 return -EBUSY;
288 default: /* device not operational */
289 return -ENODEV;
290 }
291}
292
293/*
294 * Clear I/O operation
295 */
296int
297cio_clear(struct subchannel *sch)
298{
299 char dbf_txt[15];
300 int ccode;
301
302 if (!sch)
303 return -ENODEV;
304
305 CIO_TRACE_EVENT (2, "clearIO");
306 CIO_TRACE_EVENT (2, sch->dev.bus_id);
307
308 /*
309 * Issue "Clear subchannel" and process condition code
310 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800311 ccode = csch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 sprintf (dbf_txt, "ccode:%d", ccode);
314 CIO_TRACE_EVENT (2, dbf_txt);
315
316 switch (ccode) {
317 case 0:
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200318 sch->schib.scsw.cmd.actl |= SCSW_ACTL_CLEAR_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return 0;
320 default: /* device not operational */
321 return -ENODEV;
322 }
323}
324
325/*
326 * Function: cio_cancel
327 * Issues a "Cancel Subchannel" on the specified subchannel
328 * Note: We don't need any fancy intparms and flags here
329 * since xsch is executed synchronously.
330 * Only for common I/O internal use as for now.
331 */
332int
333cio_cancel (struct subchannel *sch)
334{
335 char dbf_txt[15];
336 int ccode;
337
338 if (!sch)
339 return -ENODEV;
340
341 CIO_TRACE_EVENT (2, "cancelIO");
342 CIO_TRACE_EVENT (2, sch->dev.bus_id);
343
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800344 ccode = xsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 sprintf (dbf_txt, "ccode:%d", ccode);
347 CIO_TRACE_EVENT (2, dbf_txt);
348
349 switch (ccode) {
350 case 0: /* success */
351 /* Update information in scsw. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800352 stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return 0;
354 case 1: /* status pending */
355 return -EBUSY;
356 case 2: /* not applicable */
357 return -EINVAL;
358 default: /* not oper */
359 return -ENODEV;
360 }
361}
362
363/*
364 * Function: cio_modify
365 * Issues a "Modify Subchannel" on the specified subchannel
366 */
367int
368cio_modify (struct subchannel *sch)
369{
370 int ccode, retry, ret;
371
372 ret = 0;
373 for (retry = 0; retry < 5; retry++) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800374 ccode = msch_err (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (ccode < 0) /* -EIO if msch gets a program check. */
376 return ccode;
377 switch (ccode) {
378 case 0: /* successfull */
379 return 0;
380 case 1: /* status pending */
381 return -EBUSY;
382 case 2: /* busy */
383 udelay (100); /* allow for recovery */
384 ret = -EBUSY;
385 break;
386 case 3: /* not operational */
387 return -ENODEV;
388 }
389 }
390 return ret;
391}
392
Cornelia Huck44a1c192008-07-14 09:58:47 +0200393/**
394 * cio_enable_subchannel - enable a subchannel.
395 * @sch: subchannel to be enabled
396 * @intparm: interruption parameter to set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 */
Cornelia Huckedf22092008-04-30 13:38:39 +0200398int cio_enable_subchannel(struct subchannel *sch, u32 intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 char dbf_txt[15];
401 int ccode;
402 int retry;
403 int ret;
404
405 CIO_TRACE_EVENT (2, "ensch");
406 CIO_TRACE_EVENT (2, sch->dev.bus_id);
407
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100408 if (sch_is_pseudo_sch(sch))
409 return -EINVAL;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800410 ccode = stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (ccode)
412 return -ENODEV;
413
414 for (retry = 5, ret = 0; retry > 0; retry--) {
415 sch->schib.pmcw.ena = 1;
Cornelia Huckedf22092008-04-30 13:38:39 +0200416 sch->schib.pmcw.isc = sch->isc;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100417 sch->schib.pmcw.intparm = intparm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 ret = cio_modify(sch);
419 if (ret == -ENODEV)
420 break;
421 if (ret == -EIO)
422 /*
423 * Got a program check in cio_modify. Try without
424 * the concurrent sense bit the next time.
425 */
426 sch->schib.pmcw.csense = 0;
427 if (ret == 0) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800428 stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (sch->schib.pmcw.ena)
430 break;
431 }
432 if (ret == -EBUSY) {
433 struct irb irb;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800434 if (tsch(sch->schid, &irb) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 break;
436 }
437 }
438 sprintf (dbf_txt, "ret:%d", ret);
439 CIO_TRACE_EVENT (2, dbf_txt);
440 return ret;
441}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200442EXPORT_SYMBOL_GPL(cio_enable_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Cornelia Huck44a1c192008-07-14 09:58:47 +0200444/**
445 * cio_disable_subchannel - disable a subchannel.
446 * @sch: subchannel to disable
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 */
Cornelia Huck44a1c192008-07-14 09:58:47 +0200448int cio_disable_subchannel(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 char dbf_txt[15];
451 int ccode;
452 int retry;
453 int ret;
454
455 CIO_TRACE_EVENT (2, "dissch");
456 CIO_TRACE_EVENT (2, sch->dev.bus_id);
457
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100458 if (sch_is_pseudo_sch(sch))
459 return 0;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800460 ccode = stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (ccode == 3) /* Not operational. */
462 return -ENODEV;
463
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200464 if (scsw_actl(&sch->schib.scsw) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /*
466 * the disable function must not be called while there are
467 * requests pending for completion !
468 */
469 return -EBUSY;
470
471 for (retry = 5, ret = 0; retry > 0; retry--) {
472 sch->schib.pmcw.ena = 0;
473 ret = cio_modify(sch);
474 if (ret == -ENODEV)
475 break;
476 if (ret == -EBUSY)
477 /*
478 * The subchannel is busy or status pending.
479 * We'll disable when the next interrupt was delivered
480 * via the state machine.
481 */
482 break;
483 if (ret == 0) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800484 stsch (sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (!sch->schib.pmcw.ena)
486 break;
487 }
488 }
489 sprintf (dbf_txt, "ret:%d", ret);
490 CIO_TRACE_EVENT (2, dbf_txt);
491 return ret;
492}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200493EXPORT_SYMBOL_GPL(cio_disable_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100495int cio_create_sch_lock(struct subchannel *sch)
Cornelia Huck2ec22982006-12-08 15:54:26 +0100496{
497 sch->lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
498 if (!sch->lock)
499 return -ENOMEM;
500 spin_lock_init(sch->lock);
501 return 0;
502}
503
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200504static int cio_check_devno_blacklisted(struct subchannel *sch)
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200505{
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200506 if (is_blacklisted(sch->schid.ssid, sch->schib.pmcw.dev)) {
507 /*
508 * This device must not be known to Linux. So we simply
509 * say that there is no device and return ENODEV.
510 */
511 CIO_MSG_EVENT(6, "Blacklisted device detected "
512 "at devno %04X, subchannel set %x\n",
513 sch->schib.pmcw.dev, sch->schid.ssid);
514 return -ENODEV;
515 }
516 return 0;
517}
518
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200519static int cio_validate_io_subchannel(struct subchannel *sch)
520{
521 /* Initialization for io subchannels. */
522 if (!css_sch_is_valid(&sch->schib))
523 return -ENODEV;
524
525 /* Devno is valid. */
526 return cio_check_devno_blacklisted(sch);
527}
528
529static int cio_validate_msg_subchannel(struct subchannel *sch)
530{
531 /* Initialization for message subchannels. */
532 if (!css_sch_is_valid(&sch->schib))
533 return -ENODEV;
534
535 /* Devno is valid. */
536 return cio_check_devno_blacklisted(sch);
537}
538
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200539/**
540 * cio_validate_subchannel - basic validation of subchannel
541 * @sch: subchannel structure to be filled out
542 * @schid: subchannel id
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 *
544 * Find out subchannel type and initialize struct subchannel.
545 * Return codes:
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200546 * 0 on success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 * -ENXIO for non-defined subchannels
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200548 * -ENODEV for invalid subchannels or blacklisted devices
549 * -EIO for subchannels in an invalid subchannel set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200551int cio_validate_subchannel(struct subchannel *sch, struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 char dbf_txt[15];
554 int ccode;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100555 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200557 sprintf(dbf_txt, "valsch%x", schid.sch_no);
558 CIO_TRACE_EVENT(4, dbf_txt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 /* Nuke all fields. */
561 memset(sch, 0, sizeof(struct subchannel));
562
Cornelia Huck2ec22982006-12-08 15:54:26 +0100563 sch->schid = schid;
564 if (cio_is_console(schid)) {
565 sch->lock = cio_get_console_lock();
566 } else {
567 err = cio_create_sch_lock(sch);
568 if (err)
569 goto out;
570 }
Cornelia Huck6ab48792006-07-12 16:39:50 +0200571 mutex_init(&sch->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /* Set a name for the subchannel */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800573 snprintf (sch->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x", schid.ssid,
574 schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 /*
577 * The first subchannel that is not-operational (ccode==3)
578 * indicates that there aren't any more devices available.
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800579 * If stsch gets an exception, it means the current subchannel set
580 * is not valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800582 ccode = stsch_err (schid, &sch->schib);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100583 if (ccode) {
584 err = (ccode == 3) ? -ENXIO : ccode;
585 goto out;
586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* Copy subchannel type from path management control word. */
588 sch->st = sch->schib.pmcw.st;
Cornelia Huckc820de32008-07-14 09:58:45 +0200589
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200590 switch (sch->st) {
591 case SUBCHANNEL_TYPE_IO:
592 err = cio_validate_io_subchannel(sch);
593 break;
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200594 case SUBCHANNEL_TYPE_MSG:
595 err = cio_validate_msg_subchannel(sch);
596 break;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200597 default:
598 err = 0;
599 }
600 if (err)
Cornelia Huck2ec22982006-12-08 15:54:26 +0100601 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200603 CIO_MSG_EVENT(4, "Subchannel 0.%x.%04x reports subchannel type %04X\n",
604 sch->schid.ssid, sch->schid.sch_no, sch->st);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return 0;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100606out:
607 if (!cio_is_console(schid))
608 kfree(sch->lock);
609 sch->lock = NULL;
610 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
613/*
614 * do_IRQ() handles all normal I/O device IRQ's (the special
615 * SMP cross-CPU interrupts have their own specific
616 * handlers).
617 *
618 */
619void
620do_IRQ (struct pt_regs *regs)
621{
622 struct tpi_info *tpi_info;
623 struct subchannel *sch;
624 struct irb *irb;
Heiko Carstens5a489b92006-10-06 16:38:35 +0200625 struct pt_regs *old_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Heiko Carstens5a489b92006-10-06 16:38:35 +0200627 old_regs = set_irq_regs(regs);
Heiko Carstens9d0a57c2006-10-11 15:31:26 +0200628 irq_enter();
Heiko Carstens43ca5c32008-04-17 07:46:23 +0200629 s390_idle_check();
Heiko Carstens5a62b192008-04-17 07:46:25 +0200630 if (S390_lowcore.int_clock >= S390_lowcore.clock_comparator)
631 /* Serve timer interrupts first. */
632 clock_comparator_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /*
634 * Get interrupt information from lowcore
635 */
636 tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
637 irb = (struct irb *) __LC_IRB;
638 do {
639 kstat_cpu(smp_processor_id()).irqs[IO_INTERRUPT]++;
640 /*
641 * Non I/O-subchannel thin interrupts are processed differently
642 */
643 if (tpi_info->adapter_IO == 1 &&
644 tpi_info->int_type == IO_INTERRUPT_TYPE) {
Cornelia Huckda7c5af2008-07-14 09:58:59 +0200645 do_adapter_IO(tpi_info->isc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 continue;
647 }
648 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
Heiko Carstensa8061702008-04-17 07:46:26 +0200649 if (!sch) {
650 /* Clear pending interrupt condition. */
651 tsch(tpi_info->schid, irb);
652 continue;
653 }
654 spin_lock(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 /* Store interrupt response block to lowcore. */
Heiko Carstensa8061702008-04-17 07:46:26 +0200656 if (tsch(tpi_info->schid, irb) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /* Keep subchannel information word up to date. */
658 memcpy (&sch->schib.scsw, &irb->scsw,
659 sizeof (irb->scsw));
660 /* Call interrupt handler if there is one. */
661 if (sch->driver && sch->driver->irq)
Cornelia Huck602b20f2008-01-26 14:10:39 +0100662 sch->driver->irq(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
Heiko Carstensa8061702008-04-17 07:46:26 +0200664 spin_unlock(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /*
666 * Are more interrupts pending?
667 * If so, the tpi instruction will update the lowcore
668 * to hold the info for the next interrupt.
669 * We don't do this for VM because a tpi drops the cpu
670 * out of the sie which costs more cycles than it saves.
671 */
672 } while (!MACHINE_IS_VM && tpi (NULL) != 0);
Heiko Carstens9d0a57c2006-10-11 15:31:26 +0200673 irq_exit();
Heiko Carstens5a489b92006-10-06 16:38:35 +0200674 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
677#ifdef CONFIG_CCW_CONSOLE
678static struct subchannel console_subchannel;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100679static struct io_subchannel_private console_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680static int console_subchannel_in_use;
681
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100682void *cio_get_console_priv(void)
683{
684 return &console_priv;
685}
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687/*
688 * busy wait for the next interrupt on the console
689 */
Heiko Carstensa8061702008-04-17 07:46:26 +0200690void wait_cons_dev(void)
691 __releases(console_subchannel.lock)
692 __acquires(console_subchannel.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 unsigned long cr6 __attribute__ ((aligned (8)));
695 unsigned long save_cr6 __attribute__ ((aligned (8)));
696
697 /*
698 * before entering the spinlock we may already have
699 * processed the interrupt on a different CPU...
700 */
701 if (!console_subchannel_in_use)
702 return;
703
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200704 /* disable all but the console isc */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 __ctl_store (save_cr6, 6, 6);
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200706 cr6 = 1UL << (31 - CONSOLE_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 __ctl_load (cr6, 6, 6);
708
709 do {
Cornelia Huck2ec22982006-12-08 15:54:26 +0100710 spin_unlock(console_subchannel.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (!cio_tpi())
712 cpu_relax();
Cornelia Huck2ec22982006-12-08 15:54:26 +0100713 spin_lock(console_subchannel.lock);
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200714 } while (console_subchannel.schib.scsw.cmd.actl != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 /*
716 * restore previous isc value
717 */
718 __ctl_load (save_cr6, 6, 6);
719}
720
721static int
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800722cio_test_for_console(struct subchannel_id schid, void *data)
723{
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800724 if (stsch_err(schid, &console_subchannel.schib) != 0)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800725 return -ENXIO;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100726 if ((console_subchannel.schib.pmcw.st == SUBCHANNEL_TYPE_IO) &&
727 console_subchannel.schib.pmcw.dnv &&
728 (console_subchannel.schib.pmcw.dev == console_devno)) {
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800729 console_irq = schid.sch_no;
730 return 1; /* found */
731 }
732 return 0;
733}
734
735
736static int
737cio_get_console_sch_no(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800739 struct subchannel_id schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800741 init_subchannel_id(&schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (console_irq != -1) {
743 /* VM provided us with the irq number of the console. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800744 schid.sch_no = console_irq;
745 if (stsch(schid, &console_subchannel.schib) != 0 ||
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100746 (console_subchannel.schib.pmcw.st != SUBCHANNEL_TYPE_IO) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 !console_subchannel.schib.pmcw.dnv)
748 return -1;
749 console_devno = console_subchannel.schib.pmcw.dev;
750 } else if (console_devno != -1) {
751 /* At least the console device number is known. */
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800752 for_each_subchannel(cio_test_for_console, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (console_irq == -1)
754 return -1;
755 } else {
756 /* unlike in 2.4, we cannot autoprobe here, since
757 * the channel subsystem is not fully initialized.
758 * With some luck, the HWC console can take over */
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;
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200775 printk(KERN_WARNING "cio: No ccw console found!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return ERR_PTR(-ENODEV);
777 }
778 memset(&console_subchannel, 0, sizeof(struct subchannel));
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800779 init_subchannel_id(&schid);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800780 schid.sch_no = sch_no;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800781 ret = cio_validate_subchannel(&console_subchannel, schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (ret) {
783 console_subchannel_in_use = 0;
784 return ERR_PTR(-ENODEV);
785 }
786
787 /*
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200788 * enable console I/O-interrupt subclass
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 */
Cornelia Huck6ef556cc2008-07-14 09:59:01 +0200790 isc_register(CONSOLE_ISC);
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200791 console_subchannel.schib.pmcw.isc = CONSOLE_ISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 console_subchannel.schib.pmcw.intparm =
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100793 (u32)(addr_t)&console_subchannel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 ret = cio_modify(&console_subchannel);
795 if (ret) {
Cornelia Huck6ef556cc2008-07-14 09:59:01 +0200796 isc_unregister(CONSOLE_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 console_subchannel_in_use = 0;
798 return ERR_PTR(ret);
799 }
800 return &console_subchannel;
801}
802
803void
804cio_release_console(void)
805{
806 console_subchannel.schib.pmcw.intparm = 0;
807 cio_modify(&console_subchannel);
Cornelia Huck6ef556cc2008-07-14 09:59:01 +0200808 isc_unregister(CONSOLE_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 console_subchannel_in_use = 0;
810}
811
812/* Bah... hack to catch console special sausages. */
813int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800814cio_is_console(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 if (!console_subchannel_in_use)
817 return 0;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800818 return schid_equal(&schid, &console_subchannel.schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
821struct subchannel *
822cio_get_console_subchannel(void)
823{
824 if (!console_subchannel_in_use)
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200825 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return &console_subchannel;
827}
828
829#endif
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100830static int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800831__disable_subchannel_easy(struct subchannel_id schid, struct schib *schib)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
833 int retry, cc;
834
835 cc = 0;
836 for (retry=0;retry<3;retry++) {
837 schib->pmcw.ena = 0;
838 cc = msch(schid, schib);
839 if (cc)
840 return (cc==3?-ENODEV:-EBUSY);
841 stsch(schid, schib);
842 if (!schib->pmcw.ena)
843 return 0;
844 }
845 return -EBUSY; /* uhm... */
846}
847
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100848/* we can't use the normal udelay here, since it enables external interrupts */
849
850static void udelay_reset(unsigned long usecs)
851{
852 uint64_t start_cc, end_cc;
853
854 asm volatile ("STCK %0" : "=m" (start_cc));
855 do {
856 cpu_relax();
857 asm volatile ("STCK %0" : "=m" (end_cc));
858 } while (((end_cc - start_cc)/4096) < usecs);
859}
860
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100861static int
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200862__clear_io_subchannel_easy(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864 int retry;
865
866 if (csch(schid))
867 return -ENODEV;
868 for (retry=0;retry<20;retry++) {
869 struct tpi_info ti;
870
871 if (tpi(&ti)) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800872 tsch(ti.schid, (struct irb *)__LC_IRB);
873 if (schid_equal(&ti.schid, &schid))
Cornelia Huck4c24da72005-09-03 15:58:01 -0700874 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100876 udelay_reset(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878 return -EBUSY;
879}
880
Cornelia Huck9d92a7e2008-07-14 09:59:05 +0200881static void __clear_chsc_subchannel_easy(void)
882{
883 /* It seems we can only wait for a bit here :/ */
884 udelay_reset(100);
885}
886
Michael Holzheua45e1412006-12-15 17:18:22 +0100887static int pgm_check_occured;
888
889static void cio_reset_pgm_check_handler(void)
890{
891 pgm_check_occured = 1;
892}
893
894static int stsch_reset(struct subchannel_id schid, volatile struct schib *addr)
895{
896 int rc;
897
898 pgm_check_occured = 0;
Heiko Carstensab14de62007-02-05 21:18:37 +0100899 s390_base_pgm_handler_fn = cio_reset_pgm_check_handler;
Heiko Carstens6faf4442007-01-09 10:18:41 +0100900 rc = stsch(schid, addr);
Heiko Carstensab14de62007-02-05 21:18:37 +0100901 s390_base_pgm_handler_fn = NULL;
Heiko Carstens6faf4442007-01-09 10:18:41 +0100902
Heiko Carstensab14de62007-02-05 21:18:37 +0100903 /* The program check handler could have changed pgm_check_occured. */
Heiko Carstens6faf4442007-01-09 10:18:41 +0100904 barrier();
905
Michael Holzheua45e1412006-12-15 17:18:22 +0100906 if (pgm_check_occured)
907 return -EIO;
908 else
909 return rc;
910}
911
Heiko Carstens15e9b582006-12-04 15:40:26 +0100912static int __shutdown_subchannel_easy(struct subchannel_id schid, void *data)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800913{
914 struct schib schib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Michael Holzheua45e1412006-12-15 17:18:22 +0100916 if (stsch_reset(schid, &schib))
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800917 return -ENXIO;
918 if (!schib.pmcw.ena)
919 return 0;
920 switch(__disable_subchannel_easy(schid, &schib)) {
921 case 0:
922 case -ENODEV:
923 break;
924 default: /* -EBUSY */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200925 switch (schib.pmcw.st) {
926 case SUBCHANNEL_TYPE_IO:
927 if (__clear_io_subchannel_easy(schid))
928 goto out; /* give up... */
929 break;
Cornelia Huck9d92a7e2008-07-14 09:59:05 +0200930 case SUBCHANNEL_TYPE_CHSC:
931 __clear_chsc_subchannel_easy();
932 break;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200933 default:
934 /* No default clear strategy */
935 break;
936 }
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800937 stsch(schid, &schib);
938 __disable_subchannel_easy(schid, &schib);
939 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200940out:
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800941 return 0;
942}
943
Heiko Carstens15e9b582006-12-04 15:40:26 +0100944static atomic_t chpid_reset_count;
945
946static void s390_reset_chpids_mcck_handler(void)
947{
948 struct crw crw;
949 struct mci *mci;
950
951 /* Check for pending channel report word. */
952 mci = (struct mci *)&S390_lowcore.mcck_interruption_code;
953 if (!mci->cp)
954 return;
955 /* Process channel report words. */
956 while (stcrw(&crw) == 0) {
957 /* Check for responses to RCHP. */
958 if (crw.slct && crw.rsc == CRW_RSC_CPATH)
959 atomic_dec(&chpid_reset_count);
960 }
961}
962
963#define RCHP_TIMEOUT (30 * USEC_PER_SEC)
964static void css_reset(void)
965{
966 int i, ret;
967 unsigned long long timeout;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200968 struct chp_id chpid;
Heiko Carstens15e9b582006-12-04 15:40:26 +0100969
970 /* Reset subchannels. */
971 for_each_subchannel(__shutdown_subchannel_easy, NULL);
972 /* Reset channel paths. */
Heiko Carstensab14de62007-02-05 21:18:37 +0100973 s390_base_mcck_handler_fn = s390_reset_chpids_mcck_handler;
Heiko Carstens15e9b582006-12-04 15:40:26 +0100974 /* Enable channel report machine checks. */
975 __ctl_set_bit(14, 28);
976 /* Temporarily reenable machine checks. */
977 local_mcck_enable();
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200978 chp_id_init(&chpid);
Heiko Carstens15e9b582006-12-04 15:40:26 +0100979 for (i = 0; i <= __MAX_CHPID; i++) {
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200980 chpid.id = i;
981 ret = rchp(chpid);
Heiko Carstens15e9b582006-12-04 15:40:26 +0100982 if ((ret == 0) || (ret == 2))
983 /*
984 * rchp either succeeded, or another rchp is already
985 * in progress. In either case, we'll get a crw.
986 */
987 atomic_inc(&chpid_reset_count);
988 }
989 /* Wait for machine check for all channel paths. */
990 timeout = get_clock() + (RCHP_TIMEOUT << 12);
991 while (atomic_read(&chpid_reset_count) != 0) {
992 if (get_clock() > timeout)
993 break;
994 cpu_relax();
995 }
996 /* Disable machine checks again. */
997 local_mcck_disable();
998 /* Disable channel report machine checks. */
999 __ctl_clear_bit(14, 28);
Heiko Carstensab14de62007-02-05 21:18:37 +01001000 s390_base_mcck_handler_fn = NULL;
Heiko Carstens15e9b582006-12-04 15:40:26 +01001001}
1002
1003static struct reset_call css_reset_call = {
1004 .fn = css_reset,
1005};
1006
1007static int __init init_css_reset_call(void)
1008{
1009 atomic_set(&chpid_reset_count, 0);
1010 register_reset_call(&css_reset_call);
1011 return 0;
1012}
1013
1014arch_initcall(init_css_reset_call);
1015
1016struct sch_match_id {
1017 struct subchannel_id schid;
1018 struct ccw_dev_id devid;
1019 int rc;
1020};
1021
1022static int __reipl_subchannel_match(struct subchannel_id schid, void *data)
1023{
1024 struct schib schib;
1025 struct sch_match_id *match_id = data;
1026
Michael Holzheua45e1412006-12-15 17:18:22 +01001027 if (stsch_reset(schid, &schib))
Heiko Carstens15e9b582006-12-04 15:40:26 +01001028 return -ENXIO;
Cornelia Huckb279a4f2008-01-26 14:10:45 +01001029 if ((schib.pmcw.st == SUBCHANNEL_TYPE_IO) && schib.pmcw.dnv &&
Heiko Carstens15e9b582006-12-04 15:40:26 +01001030 (schib.pmcw.dev == match_id->devid.devno) &&
1031 (schid.ssid == match_id->devid.ssid)) {
1032 match_id->schid = schid;
1033 match_id->rc = 0;
1034 return 1;
1035 }
1036 return 0;
1037}
1038
1039static int reipl_find_schid(struct ccw_dev_id *devid,
1040 struct subchannel_id *schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001042 struct sch_match_id match_id;
1043
1044 match_id.devid = *devid;
1045 match_id.rc = -ENODEV;
Heiko Carstens15e9b582006-12-04 15:40:26 +01001046 for_each_subchannel(__reipl_subchannel_match, &match_id);
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001047 if (match_id.rc == 0)
1048 *schid = match_id.schid;
1049 return match_id.rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001052extern void do_reipl_asm(__u32 schid);
1053
1054/* Make sure all subchannels are quiet before we re-ipl an lpar. */
1055void reipl_ccw_dev(struct ccw_dev_id *devid)
1056{
1057 struct subchannel_id schid;
1058
Heiko Carstens15e9b582006-12-04 15:40:26 +01001059 s390_reset_system();
1060 if (reipl_find_schid(devid, &schid) != 0)
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001061 panic("IPL Device not found\n");
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001062 do_reipl_asm(*((__u32*)&schid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
Heiko Carstense87bfe52006-09-20 15:59:15 +02001064
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001065int __init cio_get_iplinfo(struct cio_iplinfo *iplinfo)
Heiko Carstense87bfe52006-09-20 15:59:15 +02001066{
1067 struct subchannel_id schid;
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001068 struct schib schib;
Heiko Carstense87bfe52006-09-20 15:59:15 +02001069
1070 schid = *(struct subchannel_id *)__LC_SUBCHANNEL_ID;
1071 if (!schid.one)
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001072 return -ENODEV;
1073 if (stsch(schid, &schib))
1074 return -ENODEV;
Cornelia Huckb279a4f2008-01-26 14:10:45 +01001075 if (schib.pmcw.st != SUBCHANNEL_TYPE_IO)
1076 return -ENODEV;
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001077 if (!schib.pmcw.dnv)
1078 return -ENODEV;
1079 iplinfo->devno = schib.pmcw.dev;
1080 iplinfo->is_qdio = schib.pmcw.qf;
1081 return 0;
Heiko Carstense87bfe52006-09-20 15:59:15 +02001082}
Peter Oberparleiter83262d62008-07-14 09:58:51 +02001083
1084/**
1085 * cio_tm_start_key - perform start function
1086 * @sch: subchannel on which to perform the start function
1087 * @tcw: transport-command word to be started
1088 * @lpm: mask of paths to use
1089 * @key: storage key to use for storage access
1090 *
1091 * Start the tcw on the given subchannel. Return zero on success, non-zero
1092 * otherwise.
1093 */
1094int cio_tm_start_key(struct subchannel *sch, struct tcw *tcw, u8 lpm, u8 key)
1095{
1096 int cc;
1097 union orb *orb = &to_io_private(sch)->orb;
1098
1099 memset(orb, 0, sizeof(union orb));
1100 orb->tm.intparm = (u32) (addr_t) sch;
1101 orb->tm.key = key >> 4;
1102 orb->tm.b = 1;
1103 orb->tm.lpm = lpm ? lpm : sch->lpm;
1104 orb->tm.tcw = (u32) (addr_t) tcw;
1105 cc = ssch(sch->schid, orb);
1106 switch (cc) {
1107 case 0:
1108 return 0;
1109 case 1:
1110 case 2:
1111 return -EBUSY;
1112 default:
1113 return cio_start_handle_notoper(sch, lpm);
1114 }
1115}
1116
1117/**
1118 * cio_tm_intrg - perform interrogate function
1119 * @sch - subchannel on which to perform the interrogate function
1120 *
1121 * If the specified subchannel is running in transport-mode, perform the
1122 * interrogate function. Return zero on success, non-zero otherwie.
1123 */
1124int cio_tm_intrg(struct subchannel *sch)
1125{
1126 int cc;
1127
1128 if (!to_io_private(sch)->orb.tm.b)
1129 return -EINVAL;
1130 cc = xsch(sch->schid);
1131 switch (cc) {
1132 case 0:
1133 case 2:
1134 return 0;
1135 case 1:
1136 return -EBUSY;
1137 default:
1138 return -ENODEV;
1139 }
1140}