blob: 5ec7789bd9d84a0ab171d75d66315f4bce8e4b9c [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
Michael Ernste6d5a422008-12-25 13:39:36 +010012#define KMSG_COMPONENT "cio"
13#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14
Heiko Carstens88dbd202009-06-12 10:26:46 +020015#include <linux/ftrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/init.h>
18#include <linux/slab.h>
19#include <linux/device.h>
20#include <linux/kernel_stat.h>
21#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/cio.h>
23#include <asm/delay.h>
24#include <asm/irq.h>
Heiko Carstens5a489b92006-10-06 16:38:35 +020025#include <asm/irq_regs.h>
Heiko Carstense87bfe52006-09-20 15:59:15 +020026#include <asm/setup.h>
Heiko Carstens15e9b582006-12-04 15:40:26 +010027#include <asm/reset.h>
Michael Holzheu46b05d22007-02-21 10:55:21 +010028#include <asm/ipl.h>
Peter Oberparleitere5854a52007-04-27 16:01:31 +020029#include <asm/chpid.h>
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010030#include <asm/airq.h>
Cornelia Huck3a3fc292008-07-14 09:58:58 +020031#include <asm/isc.h>
Martin Schwidefsky76d4e002009-06-12 10:26:21 +020032#include <asm/cputime.h>
Peter Oberparleiter83262d62008-07-14 09:58:51 +020033#include <asm/fcx.h>
Heiko Carstensf5daba12009-03-26 15:24:01 +010034#include <asm/nmi.h>
35#include <asm/crw.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "cio.h"
37#include "css.h"
38#include "chsc.h"
39#include "ioasm.h"
Cornelia Huckcd6b4f22008-01-26 14:10:43 +010040#include "io_sch.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "blacklist.h"
42#include "cio_debug.h"
Peter Oberparleitere6b6e102007-04-27 16:01:28 +020043#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45debug_info_t *cio_debug_msg_id;
46debug_info_t *cio_debug_trace_id;
47debug_info_t *cio_debug_crw_id;
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
50 * Function: cio_debug_init
Cornelia Huckbc698bc2008-01-26 14:10:42 +010051 * Initializes three debug logs for common I/O:
52 * - cio_msg logs generic cio messages
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * - cio_trace logs the calling of different functions
Cornelia Huckbc698bc2008-01-26 14:10:42 +010054 * - cio_crw logs machine check related cio messages
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 */
Cornelia Huckbc698bc2008-01-26 14:10:42 +010056static int __init cio_debug_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Peter Tiedemann361f4942008-01-26 14:11:30 +010058 cio_debug_msg_id = debug_register("cio_msg", 16, 1, 16 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (!cio_debug_msg_id)
60 goto out_unregister;
Cornelia Huckbc698bc2008-01-26 14:10:42 +010061 debug_register_view(cio_debug_msg_id, &debug_sprintf_view);
62 debug_set_level(cio_debug_msg_id, 2);
Peter Tiedemann361f4942008-01-26 14:11:30 +010063 cio_debug_trace_id = debug_register("cio_trace", 16, 1, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (!cio_debug_trace_id)
65 goto out_unregister;
Cornelia Huckbc698bc2008-01-26 14:10:42 +010066 debug_register_view(cio_debug_trace_id, &debug_hex_ascii_view);
67 debug_set_level(cio_debug_trace_id, 2);
Peter Tiedemann361f4942008-01-26 14:11:30 +010068 cio_debug_crw_id = debug_register("cio_crw", 16, 1, 16 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (!cio_debug_crw_id)
70 goto out_unregister;
Cornelia Huckbc698bc2008-01-26 14:10:42 +010071 debug_register_view(cio_debug_crw_id, &debug_sprintf_view);
72 debug_set_level(cio_debug_crw_id, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return 0;
74
75out_unregister:
76 if (cio_debug_msg_id)
Cornelia Huckbc698bc2008-01-26 14:10:42 +010077 debug_unregister(cio_debug_msg_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (cio_debug_trace_id)
Cornelia Huckbc698bc2008-01-26 14:10:42 +010079 debug_unregister(cio_debug_trace_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (cio_debug_crw_id)
Cornelia Huckbc698bc2008-01-26 14:10:42 +010081 debug_unregister(cio_debug_crw_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return -1;
83}
84
85arch_initcall (cio_debug_init);
86
87int
88cio_set_options (struct subchannel *sch, int flags)
89{
90 sch->options.suspend = (flags & DOIO_ALLOW_SUSPEND) != 0;
91 sch->options.prefetch = (flags & DOIO_DENY_PREFETCH) != 0;
92 sch->options.inter = (flags & DOIO_SUPPRESS_INTER) != 0;
93 return 0;
94}
95
96/* FIXME: who wants to use this? */
97int
98cio_get_options (struct subchannel *sch)
99{
100 int flags;
101
102 flags = 0;
103 if (sch->options.suspend)
104 flags |= DOIO_ALLOW_SUSPEND;
105 if (sch->options.prefetch)
106 flags |= DOIO_DENY_PREFETCH;
107 if (sch->options.inter)
108 flags |= DOIO_SUPPRESS_INTER;
109 return flags;
110}
111
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100112static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113cio_start_handle_notoper(struct subchannel *sch, __u8 lpm)
114{
115 char dbf_text[15];
116
117 if (lpm != 0)
118 sch->lpm &= ~lpm;
119 else
120 sch->lpm = 0;
121
Michael Ernst139b83d2008-05-07 09:22:54 +0200122 CIO_MSG_EVENT(2, "cio_start: 'not oper' status for "
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800123 "subchannel 0.%x.%04x!\n", sch->schid.ssid,
124 sch->schid.sch_no);
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100125
126 if (cio_update_schib(sch))
127 return -ENODEV;
128
Kay Sievers2a0217d2008-10-10 21:33:09 +0200129 sprintf(dbf_text, "no%s", dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 CIO_TRACE_EVENT(0, dbf_text);
131 CIO_HEX_EVENT(0, &sch->schib, sizeof (struct schib));
132
133 return (sch->lpm ? -EACCES : -ENODEV);
134}
135
136int
137cio_start_key (struct subchannel *sch, /* subchannel structure */
138 struct ccw1 * cpa, /* logical channel prog addr */
139 __u8 lpm, /* logical path mask */
140 __u8 key) /* storage key */
141{
142 char dbf_txt[15];
143 int ccode;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200144 union orb *orb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100146 CIO_TRACE_EVENT(4, "stIO");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200147 CIO_TRACE_EVENT(4, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100149 orb = &to_io_private(sch)->orb;
Stefan Weinhuber9adb8c12008-09-16 09:32:19 -0700150 memset(orb, 0, sizeof(union orb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 /* sch is always under 2G. */
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200152 orb->cmd.intparm = (u32)(addr_t)sch;
153 orb->cmd.fmt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200155 orb->cmd.pfch = sch->options.prefetch == 0;
156 orb->cmd.spnd = sch->options.suspend;
157 orb->cmd.ssic = sch->options.suspend && sch->options.inter;
158 orb->cmd.lpm = (lpm != 0) ? lpm : sch->lpm;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800159#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 /*
161 * for 64 bit we always support 64 bit IDAWs with 4k page size only
162 */
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200163 orb->cmd.c64 = 1;
164 orb->cmd.i2k = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#endif
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200166 orb->cmd.key = key >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 /* issue "Start Subchannel" */
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200168 orb->cmd.cpa = (__u32) __pa(cpa);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100169 ccode = ssch(sch->schid, orb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 /* process condition code */
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100172 sprintf(dbf_txt, "ccode:%d", ccode);
173 CIO_TRACE_EVENT(4, dbf_txt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 switch (ccode) {
176 case 0:
177 /*
178 * initialize device status information
179 */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200180 sch->schib.scsw.cmd.actl |= SCSW_ACTL_START_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return 0;
182 case 1: /* status pending */
183 case 2: /* busy */
184 return -EBUSY;
Cornelia Huckc91ebe42008-09-09 12:38:58 +0200185 case 3: /* device/path not operational */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return cio_start_handle_notoper(sch, lpm);
Cornelia Huckc91ebe42008-09-09 12:38:58 +0200187 default:
188 return ccode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190}
191
192int
193cio_start (struct subchannel *sch, struct ccw1 *cpa, __u8 lpm)
194{
Peter Oberparleiter0b642ed2005-05-01 08:58:58 -0700195 return cio_start_key(sch, cpa, lpm, PAGE_DEFAULT_KEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198/*
199 * resume suspended I/O operation
200 */
201int
202cio_resume (struct subchannel *sch)
203{
204 char dbf_txt[15];
205 int ccode;
206
207 CIO_TRACE_EVENT (4, "resIO");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200208 CIO_TRACE_EVENT(4, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800210 ccode = rsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 sprintf (dbf_txt, "ccode:%d", ccode);
213 CIO_TRACE_EVENT (4, dbf_txt);
214
215 switch (ccode) {
216 case 0:
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200217 sch->schib.scsw.cmd.actl |= SCSW_ACTL_RESUME_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return 0;
219 case 1:
220 return -EBUSY;
221 case 2:
222 return -EINVAL;
223 default:
224 /*
225 * useless to wait for request completion
226 * as device is no longer operational !
227 */
228 return -ENODEV;
229 }
230}
231
232/*
233 * halt I/O operation
234 */
235int
236cio_halt(struct subchannel *sch)
237{
238 char dbf_txt[15];
239 int ccode;
240
241 if (!sch)
242 return -ENODEV;
243
244 CIO_TRACE_EVENT (2, "haltIO");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200245 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 /*
248 * Issue "Halt subchannel" and process condition code
249 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800250 ccode = hsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 sprintf (dbf_txt, "ccode:%d", ccode);
253 CIO_TRACE_EVENT (2, dbf_txt);
254
255 switch (ccode) {
256 case 0:
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200257 sch->schib.scsw.cmd.actl |= SCSW_ACTL_HALT_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return 0;
259 case 1: /* status pending */
260 case 2: /* busy */
261 return -EBUSY;
262 default: /* device not operational */
263 return -ENODEV;
264 }
265}
266
267/*
268 * Clear I/O operation
269 */
270int
271cio_clear(struct subchannel *sch)
272{
273 char dbf_txt[15];
274 int ccode;
275
276 if (!sch)
277 return -ENODEV;
278
279 CIO_TRACE_EVENT (2, "clearIO");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200280 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 /*
283 * Issue "Clear subchannel" and process condition code
284 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800285 ccode = csch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 sprintf (dbf_txt, "ccode:%d", ccode);
288 CIO_TRACE_EVENT (2, dbf_txt);
289
290 switch (ccode) {
291 case 0:
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200292 sch->schib.scsw.cmd.actl |= SCSW_ACTL_CLEAR_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return 0;
294 default: /* device not operational */
295 return -ENODEV;
296 }
297}
298
299/*
300 * Function: cio_cancel
301 * Issues a "Cancel Subchannel" on the specified subchannel
302 * Note: We don't need any fancy intparms and flags here
303 * since xsch is executed synchronously.
304 * Only for common I/O internal use as for now.
305 */
306int
307cio_cancel (struct subchannel *sch)
308{
309 char dbf_txt[15];
310 int ccode;
311
312 if (!sch)
313 return -ENODEV;
314
315 CIO_TRACE_EVENT (2, "cancelIO");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200316 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800318 ccode = xsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 sprintf (dbf_txt, "ccode:%d", ccode);
321 CIO_TRACE_EVENT (2, dbf_txt);
322
323 switch (ccode) {
324 case 0: /* success */
325 /* Update information in scsw. */
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100326 if (cio_update_schib(sch))
327 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return 0;
329 case 1: /* status pending */
330 return -EBUSY;
331 case 2: /* not applicable */
332 return -EINVAL;
333 default: /* not oper */
334 return -ENODEV;
335 }
336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Sebastian Ott13952ec2008-12-25 13:39:13 +0100339static void cio_apply_config(struct subchannel *sch, struct schib *schib)
340{
341 schib->pmcw.intparm = sch->config.intparm;
342 schib->pmcw.mbi = sch->config.mbi;
343 schib->pmcw.isc = sch->config.isc;
344 schib->pmcw.ena = sch->config.ena;
345 schib->pmcw.mme = sch->config.mme;
346 schib->pmcw.mp = sch->config.mp;
347 schib->pmcw.csense = sch->config.csense;
348 schib->pmcw.mbfc = sch->config.mbfc;
349 if (sch->config.mbfc)
350 schib->mba = sch->config.mba;
351}
352
353static int cio_check_config(struct subchannel *sch, struct schib *schib)
354{
355 return (schib->pmcw.intparm == sch->config.intparm) &&
356 (schib->pmcw.mbi == sch->config.mbi) &&
357 (schib->pmcw.isc == sch->config.isc) &&
358 (schib->pmcw.ena == sch->config.ena) &&
359 (schib->pmcw.mme == sch->config.mme) &&
360 (schib->pmcw.mp == sch->config.mp) &&
361 (schib->pmcw.csense == sch->config.csense) &&
362 (schib->pmcw.mbfc == sch->config.mbfc) &&
363 (!sch->config.mbfc || (schib->mba == sch->config.mba));
364}
365
366/*
367 * cio_commit_config - apply configuration to the subchannel
368 */
369int cio_commit_config(struct subchannel *sch)
370{
371 struct schib schib;
372 int ccode, retry, ret = 0;
373
374 if (stsch(sch->schid, &schib) || !css_sch_is_valid(&schib))
375 return -ENODEV;
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 for (retry = 0; retry < 5; retry++) {
Sebastian Ott13952ec2008-12-25 13:39:13 +0100378 /* copy desired changes to local schib */
379 cio_apply_config(sch, &schib);
380 ccode = msch_err(sch->schid, &schib);
381 if (ccode < 0) /* -EIO if msch gets a program check. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return ccode;
383 switch (ccode) {
Coly Li73ac36e2009-01-07 18:09:16 -0800384 case 0: /* successful */
Sebastian Ott13952ec2008-12-25 13:39:13 +0100385 if (stsch(sch->schid, &schib) ||
386 !css_sch_is_valid(&schib))
387 return -ENODEV;
388 if (cio_check_config(sch, &schib)) {
389 /* commit changes from local schib */
390 memcpy(&sch->schib, &schib, sizeof(schib));
391 return 0;
392 }
393 ret = -EAGAIN;
394 break;
395 case 1: /* status pending */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return -EBUSY;
Sebastian Ott13952ec2008-12-25 13:39:13 +0100397 case 2: /* busy */
398 udelay(100); /* allow for recovery */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 ret = -EBUSY;
400 break;
Sebastian Ott13952ec2008-12-25 13:39:13 +0100401 case 3: /* not operational */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return -ENODEV;
403 }
404 }
405 return ret;
406}
407
Cornelia Huck44a1c192008-07-14 09:58:47 +0200408/**
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100409 * cio_update_schib - Perform stsch and update schib if subchannel is valid.
410 * @sch: subchannel on which to perform stsch
411 * Return zero on success, -ENODEV otherwise.
412 */
413int cio_update_schib(struct subchannel *sch)
414{
415 struct schib schib;
416
417 if (stsch(sch->schid, &schib) || !css_sch_is_valid(&schib))
418 return -ENODEV;
419
420 memcpy(&sch->schib, &schib, sizeof(schib));
421 return 0;
422}
423EXPORT_SYMBOL_GPL(cio_update_schib);
424
425/**
Cornelia Huck44a1c192008-07-14 09:58:47 +0200426 * cio_enable_subchannel - enable a subchannel.
427 * @sch: subchannel to be enabled
428 * @intparm: interruption parameter to set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 */
Cornelia Huckedf22092008-04-30 13:38:39 +0200430int cio_enable_subchannel(struct subchannel *sch, u32 intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 char dbf_txt[15];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 int retry;
434 int ret;
435
436 CIO_TRACE_EVENT (2, "ensch");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200437 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100439 if (sch_is_pseudo_sch(sch))
440 return -EINVAL;
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100441 if (cio_update_schib(sch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return -ENODEV;
443
Sebastian Ott13952ec2008-12-25 13:39:13 +0100444 sch->config.ena = 1;
445 sch->config.isc = sch->isc;
446 sch->config.intparm = intparm;
447
448 for (retry = 0; retry < 3; retry++) {
449 ret = cio_commit_config(sch);
450 if (ret == -EIO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /*
Sebastian Ott13952ec2008-12-25 13:39:13 +0100452 * Got a program check in msch. Try without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 * the concurrent sense bit the next time.
454 */
Sebastian Ott13952ec2008-12-25 13:39:13 +0100455 sch->config.csense = 0;
456 } else if (ret == -EBUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 struct irb irb;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800458 if (tsch(sch->schid, &irb) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
Sebastian Ott13952ec2008-12-25 13:39:13 +0100460 } else
461 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463 sprintf (dbf_txt, "ret:%d", ret);
464 CIO_TRACE_EVENT (2, dbf_txt);
465 return ret;
466}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200467EXPORT_SYMBOL_GPL(cio_enable_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Cornelia Huck44a1c192008-07-14 09:58:47 +0200469/**
470 * cio_disable_subchannel - disable a subchannel.
471 * @sch: subchannel to disable
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 */
Cornelia Huck44a1c192008-07-14 09:58:47 +0200473int cio_disable_subchannel(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 char dbf_txt[15];
Cornelia Hucked04b892009-03-26 15:24:06 +0100476 int retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 int ret;
478
479 CIO_TRACE_EVENT (2, "dissch");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200480 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100482 if (sch_is_pseudo_sch(sch))
483 return 0;
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100484 if (cio_update_schib(sch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return -ENODEV;
486
Sebastian Ott13952ec2008-12-25 13:39:13 +0100487 sch->config.ena = 0;
Sebastian Ott13952ec2008-12-25 13:39:13 +0100488
Cornelia Hucked04b892009-03-26 15:24:06 +0100489 for (retry = 0; retry < 3; retry++) {
490 ret = cio_commit_config(sch);
491 if (ret == -EBUSY) {
492 struct irb irb;
493 if (tsch(sch->schid, &irb) != 0)
494 break;
495 } else
496 break;
497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 sprintf (dbf_txt, "ret:%d", ret);
499 CIO_TRACE_EVENT (2, dbf_txt);
500 return ret;
501}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200502EXPORT_SYMBOL_GPL(cio_disable_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
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
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200513static int cio_check_devno_blacklisted(struct subchannel *sch)
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200514{
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200515 if (is_blacklisted(sch->schid.ssid, sch->schib.pmcw.dev)) {
516 /*
517 * This device must not be known to Linux. So we simply
518 * say that there is no device and return ENODEV.
519 */
520 CIO_MSG_EVENT(6, "Blacklisted device detected "
521 "at devno %04X, subchannel set %x\n",
522 sch->schib.pmcw.dev, sch->schid.ssid);
523 return -ENODEV;
524 }
525 return 0;
526}
527
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200528static int cio_validate_io_subchannel(struct subchannel *sch)
529{
530 /* Initialization for io 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
538static int cio_validate_msg_subchannel(struct subchannel *sch)
539{
540 /* Initialization for message subchannels. */
541 if (!css_sch_is_valid(&sch->schib))
542 return -ENODEV;
543
544 /* Devno is valid. */
545 return cio_check_devno_blacklisted(sch);
546}
547
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200548/**
549 * cio_validate_subchannel - basic validation of subchannel
550 * @sch: subchannel structure to be filled out
551 * @schid: subchannel id
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 *
553 * Find out subchannel type and initialize struct subchannel.
554 * Return codes:
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200555 * 0 on success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 * -ENXIO for non-defined subchannels
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200557 * -ENODEV for invalid subchannels or blacklisted devices
558 * -EIO for subchannels in an invalid subchannel set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200560int cio_validate_subchannel(struct subchannel *sch, struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 char dbf_txt[15];
563 int ccode;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100564 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200566 sprintf(dbf_txt, "valsch%x", schid.sch_no);
567 CIO_TRACE_EVENT(4, dbf_txt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /* Nuke all fields. */
570 memset(sch, 0, sizeof(struct subchannel));
571
Cornelia Huck2ec22982006-12-08 15:54:26 +0100572 sch->schid = schid;
573 if (cio_is_console(schid)) {
574 sch->lock = cio_get_console_lock();
575 } else {
576 err = cio_create_sch_lock(sch);
577 if (err)
578 goto out;
579 }
Cornelia Huck6ab48792006-07-12 16:39:50 +0200580 mutex_init(&sch->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* Set a name for the subchannel */
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +0200582 if (cio_is_console(schid))
583 sch->dev.init_name = cio_get_console_sch_name(schid);
584 else
585 dev_set_name(&sch->dev, "0.%x.%04x", schid.ssid, schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 /*
588 * The first subchannel that is not-operational (ccode==3)
589 * indicates that there aren't any more devices available.
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800590 * If stsch gets an exception, it means the current subchannel set
591 * is not valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800593 ccode = stsch_err (schid, &sch->schib);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100594 if (ccode) {
595 err = (ccode == 3) ? -ENXIO : ccode;
596 goto out;
597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /* Copy subchannel type from path management control word. */
599 sch->st = sch->schib.pmcw.st;
Cornelia Huckc820de32008-07-14 09:58:45 +0200600
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200601 switch (sch->st) {
602 case SUBCHANNEL_TYPE_IO:
603 err = cio_validate_io_subchannel(sch);
604 break;
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200605 case SUBCHANNEL_TYPE_MSG:
606 err = cio_validate_msg_subchannel(sch);
607 break;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200608 default:
609 err = 0;
610 }
611 if (err)
Cornelia Huck2ec22982006-12-08 15:54:26 +0100612 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200614 CIO_MSG_EVENT(4, "Subchannel 0.%x.%04x reports subchannel type %04X\n",
615 sch->schid.ssid, sch->schid.sch_no, sch->st);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return 0;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100617out:
618 if (!cio_is_console(schid))
619 kfree(sch->lock);
620 sch->lock = NULL;
621 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
624/*
625 * do_IRQ() handles all normal I/O device IRQ's (the special
626 * SMP cross-CPU interrupts have their own specific
627 * handlers).
628 *
629 */
Heiko Carstens88dbd202009-06-12 10:26:46 +0200630void __irq_entry do_IRQ(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 struct tpi_info *tpi_info;
633 struct subchannel *sch;
634 struct irb *irb;
Heiko Carstens5a489b92006-10-06 16:38:35 +0200635 struct pt_regs *old_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Heiko Carstens5a489b92006-10-06 16:38:35 +0200637 old_regs = set_irq_regs(regs);
Heiko Carstens43ca5c32008-04-17 07:46:23 +0200638 s390_idle_check();
Martin Schwidefsky9cfb9b32008-12-31 15:11:41 +0100639 irq_enter();
Heiko Carstens5a62b192008-04-17 07:46:25 +0200640 if (S390_lowcore.int_clock >= S390_lowcore.clock_comparator)
641 /* Serve timer interrupts first. */
642 clock_comparator_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /*
644 * Get interrupt information from lowcore
645 */
646 tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
647 irb = (struct irb *) __LC_IRB;
648 do {
649 kstat_cpu(smp_processor_id()).irqs[IO_INTERRUPT]++;
650 /*
651 * Non I/O-subchannel thin interrupts are processed differently
652 */
653 if (tpi_info->adapter_IO == 1 &&
654 tpi_info->int_type == IO_INTERRUPT_TYPE) {
Cornelia Huckda7c5af2008-07-14 09:58:59 +0200655 do_adapter_IO(tpi_info->isc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 continue;
657 }
658 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
Heiko Carstensa8061702008-04-17 07:46:26 +0200659 if (!sch) {
660 /* Clear pending interrupt condition. */
661 tsch(tpi_info->schid, irb);
662 continue;
663 }
664 spin_lock(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /* Store interrupt response block to lowcore. */
Heiko Carstensa8061702008-04-17 07:46:26 +0200666 if (tsch(tpi_info->schid, irb) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /* Keep subchannel information word up to date. */
668 memcpy (&sch->schib.scsw, &irb->scsw,
669 sizeof (irb->scsw));
670 /* Call interrupt handler if there is one. */
671 if (sch->driver && sch->driver->irq)
Cornelia Huck602b20f2008-01-26 14:10:39 +0100672 sch->driver->irq(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
Heiko Carstensa8061702008-04-17 07:46:26 +0200674 spin_unlock(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /*
676 * Are more interrupts pending?
677 * If so, the tpi instruction will update the lowcore
678 * to hold the info for the next interrupt.
679 * We don't do this for VM because a tpi drops the cpu
680 * out of the sie which costs more cycles than it saves.
681 */
682 } while (!MACHINE_IS_VM && tpi (NULL) != 0);
Heiko Carstens9d0a57c2006-10-11 15:31:26 +0200683 irq_exit();
Heiko Carstens5a489b92006-10-06 16:38:35 +0200684 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
687#ifdef CONFIG_CCW_CONSOLE
688static struct subchannel console_subchannel;
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +0200689static char console_sch_name[10] = "0.x.xxxx";
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100690static struct io_subchannel_private console_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691static int console_subchannel_in_use;
692
Heiko Carstens191fd442008-12-25 13:39:01 +0100693/*
694 * Use tpi to get a pending interrupt, call the interrupt handler and
695 * return a pointer to the subchannel structure.
696 */
697static int cio_tpi(void)
698{
699 struct tpi_info *tpi_info;
700 struct subchannel *sch;
701 struct irb *irb;
702 int irq_context;
703
704 tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
705 if (tpi(NULL) != 1)
706 return 0;
707 irb = (struct irb *) __LC_IRB;
708 /* Store interrupt response block to lowcore. */
709 if (tsch(tpi_info->schid, irb) != 0)
710 /* Not status pending or not operational. */
711 return 1;
712 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
713 if (!sch)
714 return 1;
715 irq_context = in_interrupt();
716 if (!irq_context)
717 local_bh_disable();
718 irq_enter();
719 spin_lock(sch->lock);
720 memcpy(&sch->schib.scsw, &irb->scsw, sizeof(union scsw));
721 if (sch->driver && sch->driver->irq)
722 sch->driver->irq(sch);
723 spin_unlock(sch->lock);
724 irq_exit();
725 if (!irq_context)
726 _local_bh_enable();
727 return 1;
728}
729
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100730void *cio_get_console_priv(void)
731{
732 return &console_priv;
733}
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735/*
736 * busy wait for the next interrupt on the console
737 */
Heiko Carstensa8061702008-04-17 07:46:26 +0200738void wait_cons_dev(void)
739 __releases(console_subchannel.lock)
740 __acquires(console_subchannel.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 unsigned long cr6 __attribute__ ((aligned (8)));
743 unsigned long save_cr6 __attribute__ ((aligned (8)));
744
745 /*
746 * before entering the spinlock we may already have
747 * processed the interrupt on a different CPU...
748 */
749 if (!console_subchannel_in_use)
750 return;
751
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200752 /* disable all but the console isc */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 __ctl_store (save_cr6, 6, 6);
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200754 cr6 = 1UL << (31 - CONSOLE_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 __ctl_load (cr6, 6, 6);
756
757 do {
Cornelia Huck2ec22982006-12-08 15:54:26 +0100758 spin_unlock(console_subchannel.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (!cio_tpi())
760 cpu_relax();
Cornelia Huck2ec22982006-12-08 15:54:26 +0100761 spin_lock(console_subchannel.lock);
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200762 } while (console_subchannel.schib.scsw.cmd.actl != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 /*
764 * restore previous isc value
765 */
766 __ctl_load (save_cr6, 6, 6);
767}
768
769static int
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800770cio_test_for_console(struct subchannel_id schid, void *data)
771{
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800772 if (stsch_err(schid, &console_subchannel.schib) != 0)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800773 return -ENXIO;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100774 if ((console_subchannel.schib.pmcw.st == SUBCHANNEL_TYPE_IO) &&
775 console_subchannel.schib.pmcw.dnv &&
776 (console_subchannel.schib.pmcw.dev == console_devno)) {
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800777 console_irq = schid.sch_no;
778 return 1; /* found */
779 }
780 return 0;
781}
782
783
784static int
785cio_get_console_sch_no(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800787 struct subchannel_id schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800789 init_subchannel_id(&schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (console_irq != -1) {
791 /* VM provided us with the irq number of the console. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800792 schid.sch_no = console_irq;
793 if (stsch(schid, &console_subchannel.schib) != 0 ||
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100794 (console_subchannel.schib.pmcw.st != SUBCHANNEL_TYPE_IO) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 !console_subchannel.schib.pmcw.dnv)
796 return -1;
797 console_devno = console_subchannel.schib.pmcw.dev;
798 } else if (console_devno != -1) {
799 /* At least the console device number is known. */
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800800 for_each_subchannel(cio_test_for_console, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (console_irq == -1)
802 return -1;
803 } else {
804 /* unlike in 2.4, we cannot autoprobe here, since
805 * the channel subsystem is not fully initialized.
806 * With some luck, the HWC console can take over */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return -1;
808 }
809 return console_irq;
810}
811
812struct subchannel *
813cio_probe_console(void)
814{
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800815 int sch_no, ret;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800816 struct subchannel_id schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 if (xchg(&console_subchannel_in_use, 1) != 0)
819 return ERR_PTR(-EBUSY);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800820 sch_no = cio_get_console_sch_no();
821 if (sch_no == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 console_subchannel_in_use = 0;
Michael Ernste6d5a422008-12-25 13:39:36 +0100823 pr_warning("No CCW console was found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return ERR_PTR(-ENODEV);
825 }
826 memset(&console_subchannel, 0, sizeof(struct subchannel));
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800827 init_subchannel_id(&schid);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800828 schid.sch_no = sch_no;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800829 ret = cio_validate_subchannel(&console_subchannel, schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (ret) {
831 console_subchannel_in_use = 0;
832 return ERR_PTR(-ENODEV);
833 }
834
835 /*
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200836 * enable console I/O-interrupt subclass
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 */
Cornelia Huck6ef556cc2008-07-14 09:59:01 +0200838 isc_register(CONSOLE_ISC);
Sebastian Ott13952ec2008-12-25 13:39:13 +0100839 console_subchannel.config.isc = CONSOLE_ISC;
840 console_subchannel.config.intparm = (u32)(addr_t)&console_subchannel;
841 ret = cio_commit_config(&console_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (ret) {
Cornelia Huck6ef556cc2008-07-14 09:59:01 +0200843 isc_unregister(CONSOLE_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 console_subchannel_in_use = 0;
845 return ERR_PTR(ret);
846 }
847 return &console_subchannel;
848}
849
850void
851cio_release_console(void)
852{
Sebastian Ott13952ec2008-12-25 13:39:13 +0100853 console_subchannel.config.intparm = 0;
854 cio_commit_config(&console_subchannel);
Cornelia Huck6ef556cc2008-07-14 09:59:01 +0200855 isc_unregister(CONSOLE_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 console_subchannel_in_use = 0;
857}
858
859/* Bah... hack to catch console special sausages. */
860int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800861cio_is_console(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 if (!console_subchannel_in_use)
864 return 0;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800865 return schid_equal(&schid, &console_subchannel.schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
868struct subchannel *
869cio_get_console_subchannel(void)
870{
871 if (!console_subchannel_in_use)
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200872 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return &console_subchannel;
874}
875
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +0200876const char *cio_get_console_sch_name(struct subchannel_id schid)
877{
878 snprintf(console_sch_name, 10, "0.%x.%04x", schid.ssid, schid.sch_no);
879 return (const char *)console_sch_name;
880}
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882#endif
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100883static int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800884__disable_subchannel_easy(struct subchannel_id schid, struct schib *schib)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 int retry, cc;
887
888 cc = 0;
889 for (retry=0;retry<3;retry++) {
890 schib->pmcw.ena = 0;
891 cc = msch(schid, schib);
892 if (cc)
893 return (cc==3?-ENODEV:-EBUSY);
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100894 if (stsch(schid, schib) || !css_sch_is_valid(schib))
895 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (!schib->pmcw.ena)
897 return 0;
898 }
899 return -EBUSY; /* uhm... */
900}
901
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100902static int
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200903__clear_io_subchannel_easy(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
905 int retry;
906
907 if (csch(schid))
908 return -ENODEV;
909 for (retry=0;retry<20;retry++) {
910 struct tpi_info ti;
911
912 if (tpi(&ti)) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800913 tsch(ti.schid, (struct irb *)__LC_IRB);
914 if (schid_equal(&ti.schid, &schid))
Cornelia Huck4c24da72005-09-03 15:58:01 -0700915 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200917 udelay_simple(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
919 return -EBUSY;
920}
921
Cornelia Huck9d92a7e2008-07-14 09:59:05 +0200922static void __clear_chsc_subchannel_easy(void)
923{
924 /* It seems we can only wait for a bit here :/ */
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200925 udelay_simple(100);
Cornelia Huck9d92a7e2008-07-14 09:59:05 +0200926}
927
Michael Holzheua45e1412006-12-15 17:18:22 +0100928static int pgm_check_occured;
929
930static void cio_reset_pgm_check_handler(void)
931{
932 pgm_check_occured = 1;
933}
934
Peter Oberparleiterf9c9fe32008-10-10 21:33:15 +0200935static int stsch_reset(struct subchannel_id schid, struct schib *addr)
Michael Holzheua45e1412006-12-15 17:18:22 +0100936{
937 int rc;
938
939 pgm_check_occured = 0;
Heiko Carstensab14de62007-02-05 21:18:37 +0100940 s390_base_pgm_handler_fn = cio_reset_pgm_check_handler;
Heiko Carstens6faf4442007-01-09 10:18:41 +0100941 rc = stsch(schid, addr);
Heiko Carstensab14de62007-02-05 21:18:37 +0100942 s390_base_pgm_handler_fn = NULL;
Heiko Carstens6faf4442007-01-09 10:18:41 +0100943
Heiko Carstensab14de62007-02-05 21:18:37 +0100944 /* The program check handler could have changed pgm_check_occured. */
Heiko Carstens6faf4442007-01-09 10:18:41 +0100945 barrier();
946
Michael Holzheua45e1412006-12-15 17:18:22 +0100947 if (pgm_check_occured)
948 return -EIO;
949 else
950 return rc;
951}
952
Heiko Carstens15e9b582006-12-04 15:40:26 +0100953static int __shutdown_subchannel_easy(struct subchannel_id schid, void *data)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800954{
955 struct schib schib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Michael Holzheua45e1412006-12-15 17:18:22 +0100957 if (stsch_reset(schid, &schib))
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800958 return -ENXIO;
959 if (!schib.pmcw.ena)
960 return 0;
961 switch(__disable_subchannel_easy(schid, &schib)) {
962 case 0:
963 case -ENODEV:
964 break;
965 default: /* -EBUSY */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200966 switch (schib.pmcw.st) {
967 case SUBCHANNEL_TYPE_IO:
968 if (__clear_io_subchannel_easy(schid))
969 goto out; /* give up... */
970 break;
Cornelia Huck9d92a7e2008-07-14 09:59:05 +0200971 case SUBCHANNEL_TYPE_CHSC:
972 __clear_chsc_subchannel_easy();
973 break;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200974 default:
975 /* No default clear strategy */
976 break;
977 }
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800978 stsch(schid, &schib);
979 __disable_subchannel_easy(schid, &schib);
980 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200981out:
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800982 return 0;
983}
984
Heiko Carstens15e9b582006-12-04 15:40:26 +0100985static atomic_t chpid_reset_count;
986
987static void s390_reset_chpids_mcck_handler(void)
988{
989 struct crw crw;
990 struct mci *mci;
991
992 /* Check for pending channel report word. */
993 mci = (struct mci *)&S390_lowcore.mcck_interruption_code;
994 if (!mci->cp)
995 return;
996 /* Process channel report words. */
997 while (stcrw(&crw) == 0) {
998 /* Check for responses to RCHP. */
999 if (crw.slct && crw.rsc == CRW_RSC_CPATH)
1000 atomic_dec(&chpid_reset_count);
1001 }
1002}
1003
1004#define RCHP_TIMEOUT (30 * USEC_PER_SEC)
1005static void css_reset(void)
1006{
1007 int i, ret;
1008 unsigned long long timeout;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +02001009 struct chp_id chpid;
Heiko Carstens15e9b582006-12-04 15:40:26 +01001010
1011 /* Reset subchannels. */
1012 for_each_subchannel(__shutdown_subchannel_easy, NULL);
1013 /* Reset channel paths. */
Heiko Carstensab14de62007-02-05 21:18:37 +01001014 s390_base_mcck_handler_fn = s390_reset_chpids_mcck_handler;
Heiko Carstens15e9b582006-12-04 15:40:26 +01001015 /* Enable channel report machine checks. */
1016 __ctl_set_bit(14, 28);
1017 /* Temporarily reenable machine checks. */
1018 local_mcck_enable();
Peter Oberparleiterf86635f2007-04-27 16:01:26 +02001019 chp_id_init(&chpid);
Heiko Carstens15e9b582006-12-04 15:40:26 +01001020 for (i = 0; i <= __MAX_CHPID; i++) {
Peter Oberparleiterf86635f2007-04-27 16:01:26 +02001021 chpid.id = i;
1022 ret = rchp(chpid);
Heiko Carstens15e9b582006-12-04 15:40:26 +01001023 if ((ret == 0) || (ret == 2))
1024 /*
1025 * rchp either succeeded, or another rchp is already
1026 * in progress. In either case, we'll get a crw.
1027 */
1028 atomic_inc(&chpid_reset_count);
1029 }
1030 /* Wait for machine check for all channel paths. */
1031 timeout = get_clock() + (RCHP_TIMEOUT << 12);
1032 while (atomic_read(&chpid_reset_count) != 0) {
1033 if (get_clock() > timeout)
1034 break;
1035 cpu_relax();
1036 }
1037 /* Disable machine checks again. */
1038 local_mcck_disable();
1039 /* Disable channel report machine checks. */
1040 __ctl_clear_bit(14, 28);
Heiko Carstensab14de62007-02-05 21:18:37 +01001041 s390_base_mcck_handler_fn = NULL;
Heiko Carstens15e9b582006-12-04 15:40:26 +01001042}
1043
1044static struct reset_call css_reset_call = {
1045 .fn = css_reset,
1046};
1047
1048static int __init init_css_reset_call(void)
1049{
1050 atomic_set(&chpid_reset_count, 0);
1051 register_reset_call(&css_reset_call);
1052 return 0;
1053}
1054
1055arch_initcall(init_css_reset_call);
1056
1057struct sch_match_id {
1058 struct subchannel_id schid;
1059 struct ccw_dev_id devid;
1060 int rc;
1061};
1062
1063static int __reipl_subchannel_match(struct subchannel_id schid, void *data)
1064{
1065 struct schib schib;
1066 struct sch_match_id *match_id = data;
1067
Michael Holzheua45e1412006-12-15 17:18:22 +01001068 if (stsch_reset(schid, &schib))
Heiko Carstens15e9b582006-12-04 15:40:26 +01001069 return -ENXIO;
Cornelia Huckb279a4f2008-01-26 14:10:45 +01001070 if ((schib.pmcw.st == SUBCHANNEL_TYPE_IO) && schib.pmcw.dnv &&
Heiko Carstens15e9b582006-12-04 15:40:26 +01001071 (schib.pmcw.dev == match_id->devid.devno) &&
1072 (schid.ssid == match_id->devid.ssid)) {
1073 match_id->schid = schid;
1074 match_id->rc = 0;
1075 return 1;
1076 }
1077 return 0;
1078}
1079
1080static int reipl_find_schid(struct ccw_dev_id *devid,
1081 struct subchannel_id *schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001083 struct sch_match_id match_id;
1084
1085 match_id.devid = *devid;
1086 match_id.rc = -ENODEV;
Heiko Carstens15e9b582006-12-04 15:40:26 +01001087 for_each_subchannel(__reipl_subchannel_match, &match_id);
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001088 if (match_id.rc == 0)
1089 *schid = match_id.schid;
1090 return match_id.rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001093extern void do_reipl_asm(__u32 schid);
1094
1095/* Make sure all subchannels are quiet before we re-ipl an lpar. */
1096void reipl_ccw_dev(struct ccw_dev_id *devid)
1097{
1098 struct subchannel_id schid;
1099
Heiko Carstens15e9b582006-12-04 15:40:26 +01001100 s390_reset_system();
1101 if (reipl_find_schid(devid, &schid) != 0)
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001102 panic("IPL Device not found\n");
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001103 do_reipl_asm(*((__u32*)&schid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
Heiko Carstense87bfe52006-09-20 15:59:15 +02001105
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001106int __init cio_get_iplinfo(struct cio_iplinfo *iplinfo)
Heiko Carstense87bfe52006-09-20 15:59:15 +02001107{
1108 struct subchannel_id schid;
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001109 struct schib schib;
Heiko Carstense87bfe52006-09-20 15:59:15 +02001110
1111 schid = *(struct subchannel_id *)__LC_SUBCHANNEL_ID;
1112 if (!schid.one)
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001113 return -ENODEV;
1114 if (stsch(schid, &schib))
1115 return -ENODEV;
Cornelia Huckb279a4f2008-01-26 14:10:45 +01001116 if (schib.pmcw.st != SUBCHANNEL_TYPE_IO)
1117 return -ENODEV;
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001118 if (!schib.pmcw.dnv)
1119 return -ENODEV;
1120 iplinfo->devno = schib.pmcw.dev;
1121 iplinfo->is_qdio = schib.pmcw.qf;
1122 return 0;
Heiko Carstense87bfe52006-09-20 15:59:15 +02001123}
Peter Oberparleiter83262d62008-07-14 09:58:51 +02001124
1125/**
1126 * cio_tm_start_key - perform start function
1127 * @sch: subchannel on which to perform the start function
1128 * @tcw: transport-command word to be started
1129 * @lpm: mask of paths to use
1130 * @key: storage key to use for storage access
1131 *
1132 * Start the tcw on the given subchannel. Return zero on success, non-zero
1133 * otherwise.
1134 */
1135int cio_tm_start_key(struct subchannel *sch, struct tcw *tcw, u8 lpm, u8 key)
1136{
1137 int cc;
1138 union orb *orb = &to_io_private(sch)->orb;
1139
1140 memset(orb, 0, sizeof(union orb));
1141 orb->tm.intparm = (u32) (addr_t) sch;
1142 orb->tm.key = key >> 4;
1143 orb->tm.b = 1;
1144 orb->tm.lpm = lpm ? lpm : sch->lpm;
1145 orb->tm.tcw = (u32) (addr_t) tcw;
1146 cc = ssch(sch->schid, orb);
1147 switch (cc) {
1148 case 0:
1149 return 0;
1150 case 1:
1151 case 2:
1152 return -EBUSY;
1153 default:
1154 return cio_start_handle_notoper(sch, lpm);
1155 }
1156}
1157
1158/**
1159 * cio_tm_intrg - perform interrogate function
1160 * @sch - subchannel on which to perform the interrogate function
1161 *
1162 * If the specified subchannel is running in transport-mode, perform the
1163 * interrogate function. Return zero on success, non-zero otherwie.
1164 */
1165int cio_tm_intrg(struct subchannel *sch)
1166{
1167 int cc;
1168
1169 if (!to_io_private(sch)->orb.tm.b)
1170 return -EINVAL;
1171 cc = xsch(sch->schid);
1172 switch (cc) {
1173 case 0:
1174 case 2:
1175 return 0;
1176 case 1:
1177 return -EBUSY;
1178 default:
1179 return -ENODEV;
1180 }
1181}