blob: 390b4e96fd0d1d6085cac76e7d1b5857036c34e1 [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{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 int ccode;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200143 union orb *orb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Sebastian Ottefd986d2009-09-11 10:28:18 +0200145 CIO_TRACE_EVENT(5, "stIO");
146 CIO_TRACE_EVENT(5, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100148 orb = &to_io_private(sch)->orb;
Stefan Weinhuber9adb8c12008-09-16 09:32:19 -0700149 memset(orb, 0, sizeof(union orb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 /* sch is always under 2G. */
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200151 orb->cmd.intparm = (u32)(addr_t)sch;
152 orb->cmd.fmt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200154 orb->cmd.pfch = sch->options.prefetch == 0;
155 orb->cmd.spnd = sch->options.suspend;
156 orb->cmd.ssic = sch->options.suspend && sch->options.inter;
157 orb->cmd.lpm = (lpm != 0) ? lpm : sch->lpm;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800158#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /*
160 * for 64 bit we always support 64 bit IDAWs with 4k page size only
161 */
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200162 orb->cmd.c64 = 1;
163 orb->cmd.i2k = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#endif
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200165 orb->cmd.key = key >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /* issue "Start Subchannel" */
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200167 orb->cmd.cpa = (__u32) __pa(cpa);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100168 ccode = ssch(sch->schid, orb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 /* process condition code */
Sebastian Ottefd986d2009-09-11 10:28:18 +0200171 CIO_HEX_EVENT(5, &ccode, sizeof(ccode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 switch (ccode) {
174 case 0:
175 /*
176 * initialize device status information
177 */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200178 sch->schib.scsw.cmd.actl |= SCSW_ACTL_START_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return 0;
180 case 1: /* status pending */
181 case 2: /* busy */
182 return -EBUSY;
Cornelia Huckc91ebe42008-09-09 12:38:58 +0200183 case 3: /* device/path not operational */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return cio_start_handle_notoper(sch, lpm);
Cornelia Huckc91ebe42008-09-09 12:38:58 +0200185 default:
186 return ccode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188}
189
190int
191cio_start (struct subchannel *sch, struct ccw1 *cpa, __u8 lpm)
192{
Peter Oberparleiter0b642ed2005-05-01 08:58:58 -0700193 return cio_start_key(sch, cpa, lpm, PAGE_DEFAULT_KEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196/*
197 * resume suspended I/O operation
198 */
199int
200cio_resume (struct subchannel *sch)
201{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 int ccode;
203
Sebastian Ottefd986d2009-09-11 10:28:18 +0200204 CIO_TRACE_EVENT(4, "resIO");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200205 CIO_TRACE_EVENT(4, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800207 ccode = rsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Sebastian Ottefd986d2009-09-11 10:28:18 +0200209 CIO_HEX_EVENT(4, &ccode, sizeof(ccode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 switch (ccode) {
212 case 0:
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200213 sch->schib.scsw.cmd.actl |= SCSW_ACTL_RESUME_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return 0;
215 case 1:
216 return -EBUSY;
217 case 2:
218 return -EINVAL;
219 default:
220 /*
221 * useless to wait for request completion
222 * as device is no longer operational !
223 */
224 return -ENODEV;
225 }
226}
227
228/*
229 * halt I/O operation
230 */
231int
232cio_halt(struct subchannel *sch)
233{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 int ccode;
235
236 if (!sch)
237 return -ENODEV;
238
Sebastian Ottefd986d2009-09-11 10:28:18 +0200239 CIO_TRACE_EVENT(2, "haltIO");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200240 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /*
243 * Issue "Halt subchannel" and process condition code
244 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800245 ccode = hsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Sebastian Ottefd986d2009-09-11 10:28:18 +0200247 CIO_HEX_EVENT(2, &ccode, sizeof(ccode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 switch (ccode) {
250 case 0:
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200251 sch->schib.scsw.cmd.actl |= SCSW_ACTL_HALT_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return 0;
253 case 1: /* status pending */
254 case 2: /* busy */
255 return -EBUSY;
256 default: /* device not operational */
257 return -ENODEV;
258 }
259}
260
261/*
262 * Clear I/O operation
263 */
264int
265cio_clear(struct subchannel *sch)
266{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 int ccode;
268
269 if (!sch)
270 return -ENODEV;
271
Sebastian Ottefd986d2009-09-11 10:28:18 +0200272 CIO_TRACE_EVENT(2, "clearIO");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200273 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /*
276 * Issue "Clear subchannel" and process condition code
277 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800278 ccode = csch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Sebastian Ottefd986d2009-09-11 10:28:18 +0200280 CIO_HEX_EVENT(2, &ccode, sizeof(ccode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 switch (ccode) {
283 case 0:
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200284 sch->schib.scsw.cmd.actl |= SCSW_ACTL_CLEAR_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return 0;
286 default: /* device not operational */
287 return -ENODEV;
288 }
289}
290
291/*
292 * Function: cio_cancel
293 * Issues a "Cancel Subchannel" on the specified subchannel
294 * Note: We don't need any fancy intparms and flags here
295 * since xsch is executed synchronously.
296 * Only for common I/O internal use as for now.
297 */
298int
299cio_cancel (struct subchannel *sch)
300{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 int ccode;
302
303 if (!sch)
304 return -ENODEV;
305
Sebastian Ottefd986d2009-09-11 10:28:18 +0200306 CIO_TRACE_EVENT(2, "cancelIO");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200307 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800309 ccode = xsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Sebastian Ottefd986d2009-09-11 10:28:18 +0200311 CIO_HEX_EVENT(2, &ccode, sizeof(ccode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 switch (ccode) {
314 case 0: /* success */
315 /* Update information in scsw. */
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100316 if (cio_update_schib(sch))
317 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return 0;
319 case 1: /* status pending */
320 return -EBUSY;
321 case 2: /* not applicable */
322 return -EINVAL;
323 default: /* not oper */
324 return -ENODEV;
325 }
326}
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Sebastian Ott13952ec12008-12-25 13:39:13 +0100329static void cio_apply_config(struct subchannel *sch, struct schib *schib)
330{
331 schib->pmcw.intparm = sch->config.intparm;
332 schib->pmcw.mbi = sch->config.mbi;
333 schib->pmcw.isc = sch->config.isc;
334 schib->pmcw.ena = sch->config.ena;
335 schib->pmcw.mme = sch->config.mme;
336 schib->pmcw.mp = sch->config.mp;
337 schib->pmcw.csense = sch->config.csense;
338 schib->pmcw.mbfc = sch->config.mbfc;
339 if (sch->config.mbfc)
340 schib->mba = sch->config.mba;
341}
342
343static int cio_check_config(struct subchannel *sch, struct schib *schib)
344{
345 return (schib->pmcw.intparm == sch->config.intparm) &&
346 (schib->pmcw.mbi == sch->config.mbi) &&
347 (schib->pmcw.isc == sch->config.isc) &&
348 (schib->pmcw.ena == sch->config.ena) &&
349 (schib->pmcw.mme == sch->config.mme) &&
350 (schib->pmcw.mp == sch->config.mp) &&
351 (schib->pmcw.csense == sch->config.csense) &&
352 (schib->pmcw.mbfc == sch->config.mbfc) &&
353 (!sch->config.mbfc || (schib->mba == sch->config.mba));
354}
355
356/*
357 * cio_commit_config - apply configuration to the subchannel
358 */
359int cio_commit_config(struct subchannel *sch)
360{
361 struct schib schib;
362 int ccode, retry, ret = 0;
363
364 if (stsch(sch->schid, &schib) || !css_sch_is_valid(&schib))
365 return -ENODEV;
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 for (retry = 0; retry < 5; retry++) {
Sebastian Ott13952ec12008-12-25 13:39:13 +0100368 /* copy desired changes to local schib */
369 cio_apply_config(sch, &schib);
370 ccode = msch_err(sch->schid, &schib);
371 if (ccode < 0) /* -EIO if msch gets a program check. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return ccode;
373 switch (ccode) {
Coly Li73ac36e2009-01-07 18:09:16 -0800374 case 0: /* successful */
Sebastian Ott13952ec12008-12-25 13:39:13 +0100375 if (stsch(sch->schid, &schib) ||
376 !css_sch_is_valid(&schib))
377 return -ENODEV;
378 if (cio_check_config(sch, &schib)) {
379 /* commit changes from local schib */
380 memcpy(&sch->schib, &schib, sizeof(schib));
381 return 0;
382 }
383 ret = -EAGAIN;
384 break;
385 case 1: /* status pending */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return -EBUSY;
Sebastian Ott13952ec12008-12-25 13:39:13 +0100387 case 2: /* busy */
388 udelay(100); /* allow for recovery */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 ret = -EBUSY;
390 break;
Sebastian Ott13952ec12008-12-25 13:39:13 +0100391 case 3: /* not operational */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return -ENODEV;
393 }
394 }
395 return ret;
396}
397
Cornelia Huck44a1c192008-07-14 09:58:47 +0200398/**
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100399 * cio_update_schib - Perform stsch and update schib if subchannel is valid.
400 * @sch: subchannel on which to perform stsch
401 * Return zero on success, -ENODEV otherwise.
402 */
403int cio_update_schib(struct subchannel *sch)
404{
405 struct schib schib;
406
407 if (stsch(sch->schid, &schib) || !css_sch_is_valid(&schib))
408 return -ENODEV;
409
410 memcpy(&sch->schib, &schib, sizeof(schib));
411 return 0;
412}
413EXPORT_SYMBOL_GPL(cio_update_schib);
414
415/**
Cornelia Huck44a1c192008-07-14 09:58:47 +0200416 * cio_enable_subchannel - enable a subchannel.
417 * @sch: subchannel to be enabled
418 * @intparm: interruption parameter to set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 */
Cornelia Huckedf22092008-04-30 13:38:39 +0200420int cio_enable_subchannel(struct subchannel *sch, u32 intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 int retry;
423 int ret;
424
Sebastian Ottefd986d2009-09-11 10:28:18 +0200425 CIO_TRACE_EVENT(2, "ensch");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200426 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100428 if (sch_is_pseudo_sch(sch))
429 return -EINVAL;
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100430 if (cio_update_schib(sch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return -ENODEV;
432
Sebastian Ott13952ec12008-12-25 13:39:13 +0100433 sch->config.ena = 1;
434 sch->config.isc = sch->isc;
435 sch->config.intparm = intparm;
436
437 for (retry = 0; retry < 3; retry++) {
438 ret = cio_commit_config(sch);
439 if (ret == -EIO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /*
Sebastian Ott13952ec12008-12-25 13:39:13 +0100441 * Got a program check in msch. Try without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * the concurrent sense bit the next time.
443 */
Sebastian Ott13952ec12008-12-25 13:39:13 +0100444 sch->config.csense = 0;
445 } else if (ret == -EBUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct irb irb;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800447 if (tsch(sch->schid, &irb) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 break;
Sebastian Ott13952ec12008-12-25 13:39:13 +0100449 } else
450 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
Sebastian Ottefd986d2009-09-11 10:28:18 +0200452 CIO_HEX_EVENT(2, &ret, sizeof(ret));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return ret;
454}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200455EXPORT_SYMBOL_GPL(cio_enable_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Cornelia Huck44a1c192008-07-14 09:58:47 +0200457/**
458 * cio_disable_subchannel - disable a subchannel.
459 * @sch: subchannel to disable
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 */
Cornelia Huck44a1c192008-07-14 09:58:47 +0200461int cio_disable_subchannel(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Cornelia Hucked04b892009-03-26 15:24:06 +0100463 int retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 int ret;
465
Sebastian Ottefd986d2009-09-11 10:28:18 +0200466 CIO_TRACE_EVENT(2, "dissch");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200467 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100469 if (sch_is_pseudo_sch(sch))
470 return 0;
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100471 if (cio_update_schib(sch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return -ENODEV;
473
Sebastian Ott13952ec12008-12-25 13:39:13 +0100474 sch->config.ena = 0;
Sebastian Ott13952ec12008-12-25 13:39:13 +0100475
Cornelia Hucked04b892009-03-26 15:24:06 +0100476 for (retry = 0; retry < 3; retry++) {
477 ret = cio_commit_config(sch);
478 if (ret == -EBUSY) {
479 struct irb irb;
480 if (tsch(sch->schid, &irb) != 0)
481 break;
482 } else
483 break;
484 }
Sebastian Ottefd986d2009-09-11 10:28:18 +0200485 CIO_HEX_EVENT(2, &ret, sizeof(ret));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return ret;
487}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200488EXPORT_SYMBOL_GPL(cio_disable_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100490int cio_create_sch_lock(struct subchannel *sch)
Cornelia Huck2ec22982006-12-08 15:54:26 +0100491{
492 sch->lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
493 if (!sch->lock)
494 return -ENOMEM;
495 spin_lock_init(sch->lock);
496 return 0;
497}
498
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200499static int cio_check_devno_blacklisted(struct subchannel *sch)
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200500{
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200501 if (is_blacklisted(sch->schid.ssid, sch->schib.pmcw.dev)) {
502 /*
503 * This device must not be known to Linux. So we simply
504 * say that there is no device and return ENODEV.
505 */
506 CIO_MSG_EVENT(6, "Blacklisted device detected "
507 "at devno %04X, subchannel set %x\n",
508 sch->schib.pmcw.dev, sch->schid.ssid);
509 return -ENODEV;
510 }
511 return 0;
512}
513
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200514static int cio_validate_io_subchannel(struct subchannel *sch)
515{
516 /* Initialization for io subchannels. */
517 if (!css_sch_is_valid(&sch->schib))
518 return -ENODEV;
519
520 /* Devno is valid. */
521 return cio_check_devno_blacklisted(sch);
522}
523
524static int cio_validate_msg_subchannel(struct subchannel *sch)
525{
526 /* Initialization for message subchannels. */
527 if (!css_sch_is_valid(&sch->schib))
528 return -ENODEV;
529
530 /* Devno is valid. */
531 return cio_check_devno_blacklisted(sch);
532}
533
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200534/**
535 * cio_validate_subchannel - basic validation of subchannel
536 * @sch: subchannel structure to be filled out
537 * @schid: subchannel id
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 *
539 * Find out subchannel type and initialize struct subchannel.
540 * Return codes:
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200541 * 0 on success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 * -ENXIO for non-defined subchannels
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200543 * -ENODEV for invalid subchannels or blacklisted devices
544 * -EIO for subchannels in an invalid subchannel set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200546int cio_validate_subchannel(struct subchannel *sch, struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 char dbf_txt[15];
549 int ccode;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100550 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200552 sprintf(dbf_txt, "valsch%x", schid.sch_no);
553 CIO_TRACE_EVENT(4, dbf_txt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 /* Nuke all fields. */
556 memset(sch, 0, sizeof(struct subchannel));
557
Cornelia Huck2ec22982006-12-08 15:54:26 +0100558 sch->schid = schid;
559 if (cio_is_console(schid)) {
560 sch->lock = cio_get_console_lock();
561 } else {
562 err = cio_create_sch_lock(sch);
563 if (err)
564 goto out;
565 }
Cornelia Huck6ab48792006-07-12 16:39:50 +0200566 mutex_init(&sch->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /* Set a name for the subchannel */
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +0200568 if (cio_is_console(schid))
569 sch->dev.init_name = cio_get_console_sch_name(schid);
570 else
571 dev_set_name(&sch->dev, "0.%x.%04x", schid.ssid, schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 /*
574 * The first subchannel that is not-operational (ccode==3)
575 * indicates that there aren't any more devices available.
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800576 * If stsch gets an exception, it means the current subchannel set
577 * is not valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800579 ccode = stsch_err (schid, &sch->schib);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100580 if (ccode) {
581 err = (ccode == 3) ? -ENXIO : ccode;
582 goto out;
583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /* Copy subchannel type from path management control word. */
585 sch->st = sch->schib.pmcw.st;
Cornelia Huckc820de32008-07-14 09:58:45 +0200586
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200587 switch (sch->st) {
588 case SUBCHANNEL_TYPE_IO:
589 err = cio_validate_io_subchannel(sch);
590 break;
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200591 case SUBCHANNEL_TYPE_MSG:
592 err = cio_validate_msg_subchannel(sch);
593 break;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200594 default:
595 err = 0;
596 }
597 if (err)
Cornelia Huck2ec22982006-12-08 15:54:26 +0100598 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200600 CIO_MSG_EVENT(4, "Subchannel 0.%x.%04x reports subchannel type %04X\n",
601 sch->schid.ssid, sch->schid.sch_no, sch->st);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return 0;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100603out:
604 if (!cio_is_console(schid))
605 kfree(sch->lock);
606 sch->lock = NULL;
607 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
610/*
611 * do_IRQ() handles all normal I/O device IRQ's (the special
612 * SMP cross-CPU interrupts have their own specific
613 * handlers).
614 *
615 */
Heiko Carstens88dbd202009-06-12 10:26:46 +0200616void __irq_entry do_IRQ(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 struct tpi_info *tpi_info;
619 struct subchannel *sch;
620 struct irb *irb;
Heiko Carstens5a489b92006-10-06 16:38:35 +0200621 struct pt_regs *old_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Heiko Carstens5a489b92006-10-06 16:38:35 +0200623 old_regs = set_irq_regs(regs);
Heiko Carstens43ca5c32008-04-17 07:46:23 +0200624 s390_idle_check();
Martin Schwidefsky9cfb9b32008-12-31 15:11:41 +0100625 irq_enter();
Heiko Carstens5a62b192008-04-17 07:46:25 +0200626 if (S390_lowcore.int_clock >= S390_lowcore.clock_comparator)
627 /* Serve timer interrupts first. */
628 clock_comparator_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /*
630 * Get interrupt information from lowcore
631 */
632 tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
633 irb = (struct irb *) __LC_IRB;
634 do {
635 kstat_cpu(smp_processor_id()).irqs[IO_INTERRUPT]++;
636 /*
637 * Non I/O-subchannel thin interrupts are processed differently
638 */
639 if (tpi_info->adapter_IO == 1 &&
640 tpi_info->int_type == IO_INTERRUPT_TYPE) {
Cornelia Huckda7c5af2008-07-14 09:58:59 +0200641 do_adapter_IO(tpi_info->isc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 continue;
643 }
644 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
Heiko Carstensa8061702008-04-17 07:46:26 +0200645 if (!sch) {
646 /* Clear pending interrupt condition. */
647 tsch(tpi_info->schid, irb);
648 continue;
649 }
650 spin_lock(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 /* Store interrupt response block to lowcore. */
Heiko Carstensa8061702008-04-17 07:46:26 +0200652 if (tsch(tpi_info->schid, irb) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 /* Keep subchannel information word up to date. */
654 memcpy (&sch->schib.scsw, &irb->scsw,
655 sizeof (irb->scsw));
656 /* Call interrupt handler if there is one. */
657 if (sch->driver && sch->driver->irq)
Cornelia Huck602b20f2008-01-26 14:10:39 +0100658 sch->driver->irq(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
Heiko Carstensa8061702008-04-17 07:46:26 +0200660 spin_unlock(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /*
662 * Are more interrupts pending?
663 * If so, the tpi instruction will update the lowcore
664 * to hold the info for the next interrupt.
665 * We don't do this for VM because a tpi drops the cpu
666 * out of the sie which costs more cycles than it saves.
667 */
668 } while (!MACHINE_IS_VM && tpi (NULL) != 0);
Heiko Carstens9d0a57c2006-10-11 15:31:26 +0200669 irq_exit();
Heiko Carstens5a489b92006-10-06 16:38:35 +0200670 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
673#ifdef CONFIG_CCW_CONSOLE
674static struct subchannel console_subchannel;
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +0200675static char console_sch_name[10] = "0.x.xxxx";
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100676static struct io_subchannel_private console_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677static int console_subchannel_in_use;
678
Heiko Carstens191fd442008-12-25 13:39:01 +0100679/*
680 * Use tpi to get a pending interrupt, call the interrupt handler and
681 * return a pointer to the subchannel structure.
682 */
683static int cio_tpi(void)
684{
685 struct tpi_info *tpi_info;
686 struct subchannel *sch;
687 struct irb *irb;
688 int irq_context;
689
690 tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
691 if (tpi(NULL) != 1)
692 return 0;
693 irb = (struct irb *) __LC_IRB;
694 /* Store interrupt response block to lowcore. */
695 if (tsch(tpi_info->schid, irb) != 0)
696 /* Not status pending or not operational. */
697 return 1;
698 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
699 if (!sch)
700 return 1;
701 irq_context = in_interrupt();
702 if (!irq_context)
703 local_bh_disable();
704 irq_enter();
705 spin_lock(sch->lock);
706 memcpy(&sch->schib.scsw, &irb->scsw, sizeof(union scsw));
707 if (sch->driver && sch->driver->irq)
708 sch->driver->irq(sch);
709 spin_unlock(sch->lock);
710 irq_exit();
711 if (!irq_context)
712 _local_bh_enable();
713 return 1;
714}
715
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100716void *cio_get_console_priv(void)
717{
718 return &console_priv;
719}
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721/*
722 * busy wait for the next interrupt on the console
723 */
Heiko Carstensa8061702008-04-17 07:46:26 +0200724void wait_cons_dev(void)
725 __releases(console_subchannel.lock)
726 __acquires(console_subchannel.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 unsigned long cr6 __attribute__ ((aligned (8)));
729 unsigned long save_cr6 __attribute__ ((aligned (8)));
730
731 /*
732 * before entering the spinlock we may already have
733 * processed the interrupt on a different CPU...
734 */
735 if (!console_subchannel_in_use)
736 return;
737
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200738 /* disable all but the console isc */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 __ctl_store (save_cr6, 6, 6);
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200740 cr6 = 1UL << (31 - CONSOLE_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 __ctl_load (cr6, 6, 6);
742
743 do {
Cornelia Huck2ec22982006-12-08 15:54:26 +0100744 spin_unlock(console_subchannel.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (!cio_tpi())
746 cpu_relax();
Cornelia Huck2ec22982006-12-08 15:54:26 +0100747 spin_lock(console_subchannel.lock);
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200748 } while (console_subchannel.schib.scsw.cmd.actl != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 /*
750 * restore previous isc value
751 */
752 __ctl_load (save_cr6, 6, 6);
753}
754
755static int
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800756cio_test_for_console(struct subchannel_id schid, void *data)
757{
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800758 if (stsch_err(schid, &console_subchannel.schib) != 0)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800759 return -ENXIO;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100760 if ((console_subchannel.schib.pmcw.st == SUBCHANNEL_TYPE_IO) &&
761 console_subchannel.schib.pmcw.dnv &&
762 (console_subchannel.schib.pmcw.dev == console_devno)) {
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800763 console_irq = schid.sch_no;
764 return 1; /* found */
765 }
766 return 0;
767}
768
769
770static int
771cio_get_console_sch_no(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800773 struct subchannel_id schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800775 init_subchannel_id(&schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (console_irq != -1) {
777 /* VM provided us with the irq number of the console. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800778 schid.sch_no = console_irq;
779 if (stsch(schid, &console_subchannel.schib) != 0 ||
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100780 (console_subchannel.schib.pmcw.st != SUBCHANNEL_TYPE_IO) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 !console_subchannel.schib.pmcw.dnv)
782 return -1;
783 console_devno = console_subchannel.schib.pmcw.dev;
784 } else if (console_devno != -1) {
785 /* At least the console device number is known. */
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800786 for_each_subchannel(cio_test_for_console, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (console_irq == -1)
788 return -1;
789 } else {
790 /* unlike in 2.4, we cannot autoprobe here, since
791 * the channel subsystem is not fully initialized.
792 * With some luck, the HWC console can take over */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return -1;
794 }
795 return console_irq;
796}
797
798struct subchannel *
799cio_probe_console(void)
800{
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800801 int sch_no, ret;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800802 struct subchannel_id schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 if (xchg(&console_subchannel_in_use, 1) != 0)
805 return ERR_PTR(-EBUSY);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800806 sch_no = cio_get_console_sch_no();
807 if (sch_no == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 console_subchannel_in_use = 0;
Michael Ernste6d5a422008-12-25 13:39:36 +0100809 pr_warning("No CCW console was found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return ERR_PTR(-ENODEV);
811 }
812 memset(&console_subchannel, 0, sizeof(struct subchannel));
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800813 init_subchannel_id(&schid);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800814 schid.sch_no = sch_no;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800815 ret = cio_validate_subchannel(&console_subchannel, schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (ret) {
817 console_subchannel_in_use = 0;
818 return ERR_PTR(-ENODEV);
819 }
820
821 /*
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200822 * enable console I/O-interrupt subclass
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 */
Cornelia Huck6ef556c2008-07-14 09:59:01 +0200824 isc_register(CONSOLE_ISC);
Sebastian Ott13952ec12008-12-25 13:39:13 +0100825 console_subchannel.config.isc = CONSOLE_ISC;
826 console_subchannel.config.intparm = (u32)(addr_t)&console_subchannel;
827 ret = cio_commit_config(&console_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (ret) {
Cornelia Huck6ef556c2008-07-14 09:59:01 +0200829 isc_unregister(CONSOLE_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 console_subchannel_in_use = 0;
831 return ERR_PTR(ret);
832 }
833 return &console_subchannel;
834}
835
836void
837cio_release_console(void)
838{
Sebastian Ott13952ec12008-12-25 13:39:13 +0100839 console_subchannel.config.intparm = 0;
840 cio_commit_config(&console_subchannel);
Cornelia Huck6ef556c2008-07-14 09:59:01 +0200841 isc_unregister(CONSOLE_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 console_subchannel_in_use = 0;
843}
844
845/* Bah... hack to catch console special sausages. */
846int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800847cio_is_console(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 if (!console_subchannel_in_use)
850 return 0;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800851 return schid_equal(&schid, &console_subchannel.schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
854struct subchannel *
855cio_get_console_subchannel(void)
856{
857 if (!console_subchannel_in_use)
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200858 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return &console_subchannel;
860}
861
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +0200862const char *cio_get_console_sch_name(struct subchannel_id schid)
863{
864 snprintf(console_sch_name, 10, "0.%x.%04x", schid.ssid, schid.sch_no);
865 return (const char *)console_sch_name;
866}
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868#endif
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100869static int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800870__disable_subchannel_easy(struct subchannel_id schid, struct schib *schib)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 int retry, cc;
873
874 cc = 0;
875 for (retry=0;retry<3;retry++) {
876 schib->pmcw.ena = 0;
877 cc = msch(schid, schib);
878 if (cc)
879 return (cc==3?-ENODEV:-EBUSY);
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100880 if (stsch(schid, schib) || !css_sch_is_valid(schib))
881 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (!schib->pmcw.ena)
883 return 0;
884 }
885 return -EBUSY; /* uhm... */
886}
887
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100888static int
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200889__clear_io_subchannel_easy(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
891 int retry;
892
893 if (csch(schid))
894 return -ENODEV;
895 for (retry=0;retry<20;retry++) {
896 struct tpi_info ti;
897
898 if (tpi(&ti)) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800899 tsch(ti.schid, (struct irb *)__LC_IRB);
900 if (schid_equal(&ti.schid, &schid))
Cornelia Huck4c24da72005-09-03 15:58:01 -0700901 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200903 udelay_simple(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905 return -EBUSY;
906}
907
Cornelia Huck9d92a7e2008-07-14 09:59:05 +0200908static void __clear_chsc_subchannel_easy(void)
909{
910 /* It seems we can only wait for a bit here :/ */
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200911 udelay_simple(100);
Cornelia Huck9d92a7e2008-07-14 09:59:05 +0200912}
913
Michael Holzheua45e1412006-12-15 17:18:22 +0100914static int pgm_check_occured;
915
916static void cio_reset_pgm_check_handler(void)
917{
918 pgm_check_occured = 1;
919}
920
Peter Oberparleiterf9c9fe32008-10-10 21:33:15 +0200921static int stsch_reset(struct subchannel_id schid, struct schib *addr)
Michael Holzheua45e1412006-12-15 17:18:22 +0100922{
923 int rc;
924
925 pgm_check_occured = 0;
Heiko Carstensab14de62007-02-05 21:18:37 +0100926 s390_base_pgm_handler_fn = cio_reset_pgm_check_handler;
Heiko Carstens6faf4442007-01-09 10:18:41 +0100927 rc = stsch(schid, addr);
Heiko Carstensab14de62007-02-05 21:18:37 +0100928 s390_base_pgm_handler_fn = NULL;
Heiko Carstens6faf4442007-01-09 10:18:41 +0100929
Heiko Carstensab14de62007-02-05 21:18:37 +0100930 /* The program check handler could have changed pgm_check_occured. */
Heiko Carstens6faf4442007-01-09 10:18:41 +0100931 barrier();
932
Michael Holzheua45e1412006-12-15 17:18:22 +0100933 if (pgm_check_occured)
934 return -EIO;
935 else
936 return rc;
937}
938
Heiko Carstens15e9b582006-12-04 15:40:26 +0100939static int __shutdown_subchannel_easy(struct subchannel_id schid, void *data)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800940{
941 struct schib schib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Michael Holzheua45e1412006-12-15 17:18:22 +0100943 if (stsch_reset(schid, &schib))
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800944 return -ENXIO;
945 if (!schib.pmcw.ena)
946 return 0;
947 switch(__disable_subchannel_easy(schid, &schib)) {
948 case 0:
949 case -ENODEV:
950 break;
951 default: /* -EBUSY */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200952 switch (schib.pmcw.st) {
953 case SUBCHANNEL_TYPE_IO:
954 if (__clear_io_subchannel_easy(schid))
955 goto out; /* give up... */
956 break;
Cornelia Huck9d92a7e2008-07-14 09:59:05 +0200957 case SUBCHANNEL_TYPE_CHSC:
958 __clear_chsc_subchannel_easy();
959 break;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200960 default:
961 /* No default clear strategy */
962 break;
963 }
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800964 stsch(schid, &schib);
965 __disable_subchannel_easy(schid, &schib);
966 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200967out:
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800968 return 0;
969}
970
Heiko Carstens15e9b582006-12-04 15:40:26 +0100971static atomic_t chpid_reset_count;
972
973static void s390_reset_chpids_mcck_handler(void)
974{
975 struct crw crw;
976 struct mci *mci;
977
978 /* Check for pending channel report word. */
979 mci = (struct mci *)&S390_lowcore.mcck_interruption_code;
980 if (!mci->cp)
981 return;
982 /* Process channel report words. */
983 while (stcrw(&crw) == 0) {
984 /* Check for responses to RCHP. */
985 if (crw.slct && crw.rsc == CRW_RSC_CPATH)
986 atomic_dec(&chpid_reset_count);
987 }
988}
989
990#define RCHP_TIMEOUT (30 * USEC_PER_SEC)
991static void css_reset(void)
992{
993 int i, ret;
994 unsigned long long timeout;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200995 struct chp_id chpid;
Heiko Carstens15e9b582006-12-04 15:40:26 +0100996
997 /* Reset subchannels. */
998 for_each_subchannel(__shutdown_subchannel_easy, NULL);
999 /* Reset channel paths. */
Heiko Carstensab14de62007-02-05 21:18:37 +01001000 s390_base_mcck_handler_fn = s390_reset_chpids_mcck_handler;
Heiko Carstens15e9b582006-12-04 15:40:26 +01001001 /* Enable channel report machine checks. */
1002 __ctl_set_bit(14, 28);
1003 /* Temporarily reenable machine checks. */
1004 local_mcck_enable();
Peter Oberparleiterf86635f2007-04-27 16:01:26 +02001005 chp_id_init(&chpid);
Heiko Carstens15e9b582006-12-04 15:40:26 +01001006 for (i = 0; i <= __MAX_CHPID; i++) {
Peter Oberparleiterf86635f2007-04-27 16:01:26 +02001007 chpid.id = i;
1008 ret = rchp(chpid);
Heiko Carstens15e9b582006-12-04 15:40:26 +01001009 if ((ret == 0) || (ret == 2))
1010 /*
1011 * rchp either succeeded, or another rchp is already
1012 * in progress. In either case, we'll get a crw.
1013 */
1014 atomic_inc(&chpid_reset_count);
1015 }
1016 /* Wait for machine check for all channel paths. */
1017 timeout = get_clock() + (RCHP_TIMEOUT << 12);
1018 while (atomic_read(&chpid_reset_count) != 0) {
1019 if (get_clock() > timeout)
1020 break;
1021 cpu_relax();
1022 }
1023 /* Disable machine checks again. */
1024 local_mcck_disable();
1025 /* Disable channel report machine checks. */
1026 __ctl_clear_bit(14, 28);
Heiko Carstensab14de62007-02-05 21:18:37 +01001027 s390_base_mcck_handler_fn = NULL;
Heiko Carstens15e9b582006-12-04 15:40:26 +01001028}
1029
1030static struct reset_call css_reset_call = {
1031 .fn = css_reset,
1032};
1033
1034static int __init init_css_reset_call(void)
1035{
1036 atomic_set(&chpid_reset_count, 0);
1037 register_reset_call(&css_reset_call);
1038 return 0;
1039}
1040
1041arch_initcall(init_css_reset_call);
1042
1043struct sch_match_id {
1044 struct subchannel_id schid;
1045 struct ccw_dev_id devid;
1046 int rc;
1047};
1048
1049static int __reipl_subchannel_match(struct subchannel_id schid, void *data)
1050{
1051 struct schib schib;
1052 struct sch_match_id *match_id = data;
1053
Michael Holzheua45e1412006-12-15 17:18:22 +01001054 if (stsch_reset(schid, &schib))
Heiko Carstens15e9b582006-12-04 15:40:26 +01001055 return -ENXIO;
Cornelia Huckb279a4f2008-01-26 14:10:45 +01001056 if ((schib.pmcw.st == SUBCHANNEL_TYPE_IO) && schib.pmcw.dnv &&
Heiko Carstens15e9b582006-12-04 15:40:26 +01001057 (schib.pmcw.dev == match_id->devid.devno) &&
1058 (schid.ssid == match_id->devid.ssid)) {
1059 match_id->schid = schid;
1060 match_id->rc = 0;
1061 return 1;
1062 }
1063 return 0;
1064}
1065
1066static int reipl_find_schid(struct ccw_dev_id *devid,
1067 struct subchannel_id *schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001069 struct sch_match_id match_id;
1070
1071 match_id.devid = *devid;
1072 match_id.rc = -ENODEV;
Heiko Carstens15e9b582006-12-04 15:40:26 +01001073 for_each_subchannel(__reipl_subchannel_match, &match_id);
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001074 if (match_id.rc == 0)
1075 *schid = match_id.schid;
1076 return match_id.rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001079extern void do_reipl_asm(__u32 schid);
1080
1081/* Make sure all subchannels are quiet before we re-ipl an lpar. */
1082void reipl_ccw_dev(struct ccw_dev_id *devid)
1083{
1084 struct subchannel_id schid;
1085
Heiko Carstens15e9b582006-12-04 15:40:26 +01001086 s390_reset_system();
1087 if (reipl_find_schid(devid, &schid) != 0)
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001088 panic("IPL Device not found\n");
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001089 do_reipl_asm(*((__u32*)&schid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
Heiko Carstense87bfe52006-09-20 15:59:15 +02001091
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001092int __init cio_get_iplinfo(struct cio_iplinfo *iplinfo)
Heiko Carstense87bfe52006-09-20 15:59:15 +02001093{
1094 struct subchannel_id schid;
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001095 struct schib schib;
Heiko Carstense87bfe52006-09-20 15:59:15 +02001096
1097 schid = *(struct subchannel_id *)__LC_SUBCHANNEL_ID;
1098 if (!schid.one)
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001099 return -ENODEV;
1100 if (stsch(schid, &schib))
1101 return -ENODEV;
Cornelia Huckb279a4f2008-01-26 14:10:45 +01001102 if (schib.pmcw.st != SUBCHANNEL_TYPE_IO)
1103 return -ENODEV;
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001104 if (!schib.pmcw.dnv)
1105 return -ENODEV;
1106 iplinfo->devno = schib.pmcw.dev;
1107 iplinfo->is_qdio = schib.pmcw.qf;
1108 return 0;
Heiko Carstense87bfe52006-09-20 15:59:15 +02001109}
Peter Oberparleiter83262d62008-07-14 09:58:51 +02001110
1111/**
1112 * cio_tm_start_key - perform start function
1113 * @sch: subchannel on which to perform the start function
1114 * @tcw: transport-command word to be started
1115 * @lpm: mask of paths to use
1116 * @key: storage key to use for storage access
1117 *
1118 * Start the tcw on the given subchannel. Return zero on success, non-zero
1119 * otherwise.
1120 */
1121int cio_tm_start_key(struct subchannel *sch, struct tcw *tcw, u8 lpm, u8 key)
1122{
1123 int cc;
1124 union orb *orb = &to_io_private(sch)->orb;
1125
1126 memset(orb, 0, sizeof(union orb));
1127 orb->tm.intparm = (u32) (addr_t) sch;
1128 orb->tm.key = key >> 4;
1129 orb->tm.b = 1;
1130 orb->tm.lpm = lpm ? lpm : sch->lpm;
1131 orb->tm.tcw = (u32) (addr_t) tcw;
1132 cc = ssch(sch->schid, orb);
1133 switch (cc) {
1134 case 0:
1135 return 0;
1136 case 1:
1137 case 2:
1138 return -EBUSY;
1139 default:
1140 return cio_start_handle_notoper(sch, lpm);
1141 }
1142}
1143
1144/**
1145 * cio_tm_intrg - perform interrogate function
1146 * @sch - subchannel on which to perform the interrogate function
1147 *
1148 * If the specified subchannel is running in transport-mode, perform the
1149 * interrogate function. Return zero on success, non-zero otherwie.
1150 */
1151int cio_tm_intrg(struct subchannel *sch)
1152{
1153 int cc;
1154
1155 if (!to_io_private(sch)->orb.tm.b)
1156 return -EINVAL;
1157 cc = xsch(sch->schid);
1158 switch (cc) {
1159 case 0:
1160 case 2:
1161 return 0;
1162 case 1:
1163 return -EBUSY;
1164 default:
1165 return -ENODEV;
1166 }
1167}