blob: eb3140ee821eaab07fe43a2eb079909c11f3e362 [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
Sebastian Ottc513d072011-03-15 17:08:25 +010087int cio_set_options(struct subchannel *sch, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Sebastian Ottc513d072011-03-15 17:08:25 +010089 struct io_subchannel_private *priv = to_io_private(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Sebastian Ottc513d072011-03-15 17:08:25 +010091 priv->options.suspend = (flags & DOIO_ALLOW_SUSPEND) != 0;
92 priv->options.prefetch = (flags & DOIO_DENY_PREFETCH) != 0;
93 priv->options.inter = (flags & DOIO_SUPPRESS_INTER) != 0;
94 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Heiko Carstens4d284ca2007-02-05 21:18:53 +010097static int
Linus Torvalds1da177e2005-04-16 15:20:36 -070098cio_start_handle_notoper(struct subchannel *sch, __u8 lpm)
99{
100 char dbf_text[15];
101
102 if (lpm != 0)
103 sch->lpm &= ~lpm;
104 else
105 sch->lpm = 0;
106
Michael Ernst139b83d2008-05-07 09:22:54 +0200107 CIO_MSG_EVENT(2, "cio_start: 'not oper' status for "
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800108 "subchannel 0.%x.%04x!\n", sch->schid.ssid,
109 sch->schid.sch_no);
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100110
111 if (cio_update_schib(sch))
112 return -ENODEV;
113
Kay Sievers2a0217d2008-10-10 21:33:09 +0200114 sprintf(dbf_text, "no%s", dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 CIO_TRACE_EVENT(0, dbf_text);
116 CIO_HEX_EVENT(0, &sch->schib, sizeof (struct schib));
117
118 return (sch->lpm ? -EACCES : -ENODEV);
119}
120
121int
122cio_start_key (struct subchannel *sch, /* subchannel structure */
123 struct ccw1 * cpa, /* logical channel prog addr */
124 __u8 lpm, /* logical path mask */
125 __u8 key) /* storage key */
126{
Sebastian Ottc513d072011-03-15 17:08:25 +0100127 struct io_subchannel_private *priv = to_io_private(sch);
128 union orb *orb = &priv->orb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 int ccode;
130
Sebastian Ottefd986d2009-09-11 10:28:18 +0200131 CIO_TRACE_EVENT(5, "stIO");
132 CIO_TRACE_EVENT(5, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Stefan Weinhuber9adb8c12008-09-16 09:32:19 -0700134 memset(orb, 0, sizeof(union orb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 /* sch is always under 2G. */
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200136 orb->cmd.intparm = (u32)(addr_t)sch;
137 orb->cmd.fmt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Sebastian Ottc513d072011-03-15 17:08:25 +0100139 orb->cmd.pfch = priv->options.prefetch == 0;
140 orb->cmd.spnd = priv->options.suspend;
141 orb->cmd.ssic = priv->options.suspend && priv->options.inter;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200142 orb->cmd.lpm = (lpm != 0) ? lpm : sch->lpm;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800143#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 /*
145 * for 64 bit we always support 64 bit IDAWs with 4k page size only
146 */
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200147 orb->cmd.c64 = 1;
148 orb->cmd.i2k = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#endif
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200150 orb->cmd.key = key >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 /* issue "Start Subchannel" */
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200152 orb->cmd.cpa = (__u32) __pa(cpa);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100153 ccode = ssch(sch->schid, orb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 /* process condition code */
Sebastian Ottefd986d2009-09-11 10:28:18 +0200156 CIO_HEX_EVENT(5, &ccode, sizeof(ccode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 switch (ccode) {
159 case 0:
160 /*
161 * initialize device status information
162 */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200163 sch->schib.scsw.cmd.actl |= SCSW_ACTL_START_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return 0;
165 case 1: /* status pending */
166 case 2: /* busy */
167 return -EBUSY;
Cornelia Huckc91ebe42008-09-09 12:38:58 +0200168 case 3: /* device/path not operational */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return cio_start_handle_notoper(sch, lpm);
Cornelia Huckc91ebe42008-09-09 12:38:58 +0200170 default:
171 return ccode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173}
174
175int
176cio_start (struct subchannel *sch, struct ccw1 *cpa, __u8 lpm)
177{
Peter Oberparleiter0b642ed2005-05-01 08:58:58 -0700178 return cio_start_key(sch, cpa, lpm, PAGE_DEFAULT_KEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181/*
182 * resume suspended I/O operation
183 */
184int
185cio_resume (struct subchannel *sch)
186{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 int ccode;
188
Sebastian Ottefd986d2009-09-11 10:28:18 +0200189 CIO_TRACE_EVENT(4, "resIO");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200190 CIO_TRACE_EVENT(4, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800192 ccode = rsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Sebastian Ottefd986d2009-09-11 10:28:18 +0200194 CIO_HEX_EVENT(4, &ccode, sizeof(ccode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 switch (ccode) {
197 case 0:
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200198 sch->schib.scsw.cmd.actl |= SCSW_ACTL_RESUME_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return 0;
200 case 1:
201 return -EBUSY;
202 case 2:
203 return -EINVAL;
204 default:
205 /*
206 * useless to wait for request completion
207 * as device is no longer operational !
208 */
209 return -ENODEV;
210 }
211}
212
213/*
214 * halt I/O operation
215 */
216int
217cio_halt(struct subchannel *sch)
218{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 int ccode;
220
221 if (!sch)
222 return -ENODEV;
223
Sebastian Ottefd986d2009-09-11 10:28:18 +0200224 CIO_TRACE_EVENT(2, "haltIO");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200225 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /*
228 * Issue "Halt subchannel" and process condition code
229 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800230 ccode = hsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Sebastian Ottefd986d2009-09-11 10:28:18 +0200232 CIO_HEX_EVENT(2, &ccode, sizeof(ccode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 switch (ccode) {
235 case 0:
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200236 sch->schib.scsw.cmd.actl |= SCSW_ACTL_HALT_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return 0;
238 case 1: /* status pending */
239 case 2: /* busy */
240 return -EBUSY;
241 default: /* device not operational */
242 return -ENODEV;
243 }
244}
245
246/*
247 * Clear I/O operation
248 */
249int
250cio_clear(struct subchannel *sch)
251{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 int ccode;
253
254 if (!sch)
255 return -ENODEV;
256
Sebastian Ottefd986d2009-09-11 10:28:18 +0200257 CIO_TRACE_EVENT(2, "clearIO");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200258 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 /*
261 * Issue "Clear subchannel" and process condition code
262 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800263 ccode = csch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Sebastian Ottefd986d2009-09-11 10:28:18 +0200265 CIO_HEX_EVENT(2, &ccode, sizeof(ccode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 switch (ccode) {
268 case 0:
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200269 sch->schib.scsw.cmd.actl |= SCSW_ACTL_CLEAR_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return 0;
271 default: /* device not operational */
272 return -ENODEV;
273 }
274}
275
276/*
277 * Function: cio_cancel
278 * Issues a "Cancel Subchannel" on the specified subchannel
279 * Note: We don't need any fancy intparms and flags here
280 * since xsch is executed synchronously.
281 * Only for common I/O internal use as for now.
282 */
283int
284cio_cancel (struct subchannel *sch)
285{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 int ccode;
287
288 if (!sch)
289 return -ENODEV;
290
Sebastian Ottefd986d2009-09-11 10:28:18 +0200291 CIO_TRACE_EVENT(2, "cancelIO");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200292 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800294 ccode = xsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Sebastian Ottefd986d2009-09-11 10:28:18 +0200296 CIO_HEX_EVENT(2, &ccode, sizeof(ccode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 switch (ccode) {
299 case 0: /* success */
300 /* Update information in scsw. */
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100301 if (cio_update_schib(sch))
302 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return 0;
304 case 1: /* status pending */
305 return -EBUSY;
306 case 2: /* not applicable */
307 return -EINVAL;
308 default: /* not oper */
309 return -ENODEV;
310 }
311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Sebastian Ott13952ec2008-12-25 13:39:13 +0100314static void cio_apply_config(struct subchannel *sch, struct schib *schib)
315{
316 schib->pmcw.intparm = sch->config.intparm;
317 schib->pmcw.mbi = sch->config.mbi;
318 schib->pmcw.isc = sch->config.isc;
319 schib->pmcw.ena = sch->config.ena;
320 schib->pmcw.mme = sch->config.mme;
321 schib->pmcw.mp = sch->config.mp;
322 schib->pmcw.csense = sch->config.csense;
323 schib->pmcw.mbfc = sch->config.mbfc;
324 if (sch->config.mbfc)
325 schib->mba = sch->config.mba;
326}
327
328static int cio_check_config(struct subchannel *sch, struct schib *schib)
329{
330 return (schib->pmcw.intparm == sch->config.intparm) &&
331 (schib->pmcw.mbi == sch->config.mbi) &&
332 (schib->pmcw.isc == sch->config.isc) &&
333 (schib->pmcw.ena == sch->config.ena) &&
334 (schib->pmcw.mme == sch->config.mme) &&
335 (schib->pmcw.mp == sch->config.mp) &&
336 (schib->pmcw.csense == sch->config.csense) &&
337 (schib->pmcw.mbfc == sch->config.mbfc) &&
338 (!sch->config.mbfc || (schib->mba == sch->config.mba));
339}
340
341/*
342 * cio_commit_config - apply configuration to the subchannel
343 */
344int cio_commit_config(struct subchannel *sch)
345{
346 struct schib schib;
347 int ccode, retry, ret = 0;
348
Sebastian Ott8821d242010-04-22 17:17:05 +0200349 if (stsch_err(sch->schid, &schib) || !css_sch_is_valid(&schib))
Sebastian Ott13952ec2008-12-25 13:39:13 +0100350 return -ENODEV;
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 for (retry = 0; retry < 5; retry++) {
Sebastian Ott13952ec2008-12-25 13:39:13 +0100353 /* copy desired changes to local schib */
354 cio_apply_config(sch, &schib);
355 ccode = msch_err(sch->schid, &schib);
356 if (ccode < 0) /* -EIO if msch gets a program check. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return ccode;
358 switch (ccode) {
Coly Li73ac36e2009-01-07 18:09:16 -0800359 case 0: /* successful */
Sebastian Ott8821d242010-04-22 17:17:05 +0200360 if (stsch_err(sch->schid, &schib) ||
Sebastian Ott13952ec2008-12-25 13:39:13 +0100361 !css_sch_is_valid(&schib))
362 return -ENODEV;
363 if (cio_check_config(sch, &schib)) {
364 /* commit changes from local schib */
365 memcpy(&sch->schib, &schib, sizeof(schib));
366 return 0;
367 }
368 ret = -EAGAIN;
369 break;
370 case 1: /* status pending */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return -EBUSY;
Sebastian Ott13952ec2008-12-25 13:39:13 +0100372 case 2: /* busy */
373 udelay(100); /* allow for recovery */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 ret = -EBUSY;
375 break;
Sebastian Ott13952ec2008-12-25 13:39:13 +0100376 case 3: /* not operational */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return -ENODEV;
378 }
379 }
380 return ret;
381}
382
Cornelia Huck44a1c192008-07-14 09:58:47 +0200383/**
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100384 * cio_update_schib - Perform stsch and update schib if subchannel is valid.
385 * @sch: subchannel on which to perform stsch
386 * Return zero on success, -ENODEV otherwise.
387 */
388int cio_update_schib(struct subchannel *sch)
389{
390 struct schib schib;
391
Sebastian Ott8821d242010-04-22 17:17:05 +0200392 if (stsch_err(sch->schid, &schib) || !css_sch_is_valid(&schib))
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100393 return -ENODEV;
394
395 memcpy(&sch->schib, &schib, sizeof(schib));
396 return 0;
397}
398EXPORT_SYMBOL_GPL(cio_update_schib);
399
400/**
Cornelia Huck44a1c192008-07-14 09:58:47 +0200401 * cio_enable_subchannel - enable a subchannel.
402 * @sch: subchannel to be enabled
403 * @intparm: interruption parameter to set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 */
Cornelia Huckedf22092008-04-30 13:38:39 +0200405int cio_enable_subchannel(struct subchannel *sch, u32 intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 int retry;
408 int ret;
409
Sebastian Ottefd986d2009-09-11 10:28:18 +0200410 CIO_TRACE_EVENT(2, "ensch");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200411 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100413 if (sch_is_pseudo_sch(sch))
414 return -EINVAL;
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100415 if (cio_update_schib(sch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return -ENODEV;
417
Sebastian Ott13952ec2008-12-25 13:39:13 +0100418 sch->config.ena = 1;
419 sch->config.isc = sch->isc;
420 sch->config.intparm = intparm;
421
422 for (retry = 0; retry < 3; retry++) {
423 ret = cio_commit_config(sch);
424 if (ret == -EIO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 /*
Sebastian Ott13952ec2008-12-25 13:39:13 +0100426 * Got a program check in msch. Try without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 * the concurrent sense bit the next time.
428 */
Sebastian Ott13952ec2008-12-25 13:39:13 +0100429 sch->config.csense = 0;
430 } else if (ret == -EBUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 struct irb irb;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800432 if (tsch(sch->schid, &irb) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 break;
Sebastian Ott13952ec2008-12-25 13:39:13 +0100434 } else
435 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
Sebastian Ottefd986d2009-09-11 10:28:18 +0200437 CIO_HEX_EVENT(2, &ret, sizeof(ret));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return ret;
439}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200440EXPORT_SYMBOL_GPL(cio_enable_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Cornelia Huck44a1c192008-07-14 09:58:47 +0200442/**
443 * cio_disable_subchannel - disable a subchannel.
444 * @sch: subchannel to disable
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 */
Cornelia Huck44a1c192008-07-14 09:58:47 +0200446int cio_disable_subchannel(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Cornelia Hucked04b892009-03-26 15:24:06 +0100448 int retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 int ret;
450
Sebastian Ottefd986d2009-09-11 10:28:18 +0200451 CIO_TRACE_EVENT(2, "dissch");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200452 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100454 if (sch_is_pseudo_sch(sch))
455 return 0;
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100456 if (cio_update_schib(sch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return -ENODEV;
458
Sebastian Ott13952ec2008-12-25 13:39:13 +0100459 sch->config.ena = 0;
Sebastian Ott13952ec2008-12-25 13:39:13 +0100460
Cornelia Hucked04b892009-03-26 15:24:06 +0100461 for (retry = 0; retry < 3; retry++) {
462 ret = cio_commit_config(sch);
463 if (ret == -EBUSY) {
464 struct irb irb;
465 if (tsch(sch->schid, &irb) != 0)
466 break;
467 } else
468 break;
469 }
Sebastian Ottefd986d2009-09-11 10:28:18 +0200470 CIO_HEX_EVENT(2, &ret, sizeof(ret));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return ret;
472}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200473EXPORT_SYMBOL_GPL(cio_disable_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100475int cio_create_sch_lock(struct subchannel *sch)
Cornelia Huck2ec22982006-12-08 15:54:26 +0100476{
477 sch->lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
478 if (!sch->lock)
479 return -ENOMEM;
480 spin_lock_init(sch->lock);
481 return 0;
482}
483
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200484static int cio_check_devno_blacklisted(struct subchannel *sch)
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200485{
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200486 if (is_blacklisted(sch->schid.ssid, sch->schib.pmcw.dev)) {
487 /*
488 * This device must not be known to Linux. So we simply
489 * say that there is no device and return ENODEV.
490 */
491 CIO_MSG_EVENT(6, "Blacklisted device detected "
492 "at devno %04X, subchannel set %x\n",
493 sch->schib.pmcw.dev, sch->schid.ssid);
494 return -ENODEV;
495 }
496 return 0;
497}
498
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200499static int cio_validate_io_subchannel(struct subchannel *sch)
500{
501 /* Initialization for io subchannels. */
502 if (!css_sch_is_valid(&sch->schib))
503 return -ENODEV;
504
505 /* Devno is valid. */
506 return cio_check_devno_blacklisted(sch);
507}
508
509static int cio_validate_msg_subchannel(struct subchannel *sch)
510{
511 /* Initialization for message subchannels. */
512 if (!css_sch_is_valid(&sch->schib))
513 return -ENODEV;
514
515 /* Devno is valid. */
516 return cio_check_devno_blacklisted(sch);
517}
518
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200519/**
520 * cio_validate_subchannel - basic validation of subchannel
521 * @sch: subchannel structure to be filled out
522 * @schid: subchannel id
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 *
524 * Find out subchannel type and initialize struct subchannel.
525 * Return codes:
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200526 * 0 on success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 * -ENXIO for non-defined subchannels
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200528 * -ENODEV for invalid subchannels or blacklisted devices
529 * -EIO for subchannels in an invalid subchannel set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200531int cio_validate_subchannel(struct subchannel *sch, struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 char dbf_txt[15];
534 int ccode;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100535 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200537 sprintf(dbf_txt, "valsch%x", schid.sch_no);
538 CIO_TRACE_EVENT(4, dbf_txt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 /* Nuke all fields. */
541 memset(sch, 0, sizeof(struct subchannel));
542
Cornelia Huck2ec22982006-12-08 15:54:26 +0100543 sch->schid = schid;
544 if (cio_is_console(schid)) {
545 sch->lock = cio_get_console_lock();
546 } else {
547 err = cio_create_sch_lock(sch);
548 if (err)
549 goto out;
550 }
Cornelia Huck6ab48792006-07-12 16:39:50 +0200551 mutex_init(&sch->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 /*
554 * The first subchannel that is not-operational (ccode==3)
555 * indicates that there aren't any more devices available.
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800556 * If stsch gets an exception, it means the current subchannel set
557 * is not valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800559 ccode = stsch_err (schid, &sch->schib);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100560 if (ccode) {
561 err = (ccode == 3) ? -ENXIO : ccode;
562 goto out;
563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /* Copy subchannel type from path management control word. */
565 sch->st = sch->schib.pmcw.st;
Cornelia Huckc820de32008-07-14 09:58:45 +0200566
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200567 switch (sch->st) {
568 case SUBCHANNEL_TYPE_IO:
569 err = cio_validate_io_subchannel(sch);
570 break;
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200571 case SUBCHANNEL_TYPE_MSG:
572 err = cio_validate_msg_subchannel(sch);
573 break;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200574 default:
575 err = 0;
576 }
577 if (err)
Cornelia Huck2ec22982006-12-08 15:54:26 +0100578 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200580 CIO_MSG_EVENT(4, "Subchannel 0.%x.%04x reports subchannel type %04X\n",
581 sch->schid.ssid, sch->schid.sch_no, sch->st);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return 0;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100583out:
584 if (!cio_is_console(schid))
585 kfree(sch->lock);
586 sch->lock = NULL;
587 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
590/*
591 * do_IRQ() handles all normal I/O device IRQ's (the special
592 * SMP cross-CPU interrupts have their own specific
593 * handlers).
594 *
595 */
Heiko Carstens88dbd202009-06-12 10:26:46 +0200596void __irq_entry do_IRQ(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
598 struct tpi_info *tpi_info;
599 struct subchannel *sch;
600 struct irb *irb;
Heiko Carstens5a489b92006-10-06 16:38:35 +0200601 struct pt_regs *old_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Heiko Carstens5a489b92006-10-06 16:38:35 +0200603 old_regs = set_irq_regs(regs);
Martin Schwidefsky63779812010-05-17 10:00:03 +0200604 s390_idle_check(regs, S390_lowcore.int_clock,
605 S390_lowcore.async_enter_timer);
Martin Schwidefsky9cfb9b32008-12-31 15:11:41 +0100606 irq_enter();
Christoph Lameter4a6f4fe2010-12-06 11:16:24 -0600607 __this_cpu_write(s390_idle.nohz_delay, 1);
Heiko Carstens5a62b192008-04-17 07:46:25 +0200608 if (S390_lowcore.int_clock >= S390_lowcore.clock_comparator)
609 /* Serve timer interrupts first. */
610 clock_comparator_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /*
612 * Get interrupt information from lowcore
613 */
Heiko Carstenscbb870c2010-02-26 22:37:43 +0100614 tpi_info = (struct tpi_info *)&S390_lowcore.subchannel_id;
615 irb = (struct irb *)&S390_lowcore.irb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 do {
617 kstat_cpu(smp_processor_id()).irqs[IO_INTERRUPT]++;
Sebastian Ottee5894f2011-03-15 17:08:20 +0100618 if (tpi_info->adapter_IO) {
Cornelia Huckda7c5af2008-07-14 09:58:59 +0200619 do_adapter_IO(tpi_info->isc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 continue;
621 }
622 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
Heiko Carstensa8061702008-04-17 07:46:26 +0200623 if (!sch) {
624 /* Clear pending interrupt condition. */
625 tsch(tpi_info->schid, irb);
626 continue;
627 }
628 spin_lock(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /* Store interrupt response block to lowcore. */
Heiko Carstensa8061702008-04-17 07:46:26 +0200630 if (tsch(tpi_info->schid, irb) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /* Keep subchannel information word up to date. */
632 memcpy (&sch->schib.scsw, &irb->scsw,
633 sizeof (irb->scsw));
634 /* Call interrupt handler if there is one. */
635 if (sch->driver && sch->driver->irq)
Cornelia Huck602b20f2008-01-26 14:10:39 +0100636 sch->driver->irq(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
Heiko Carstensa8061702008-04-17 07:46:26 +0200638 spin_unlock(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /*
640 * Are more interrupts pending?
641 * If so, the tpi instruction will update the lowcore
642 * to hold the info for the next interrupt.
643 * We don't do this for VM because a tpi drops the cpu
644 * out of the sie which costs more cycles than it saves.
645 */
Martin Schwidefsky27d71602010-02-26 22:37:38 +0100646 } while (MACHINE_IS_LPAR && tpi(NULL) != 0);
Heiko Carstens9d0a57c2006-10-11 15:31:26 +0200647 irq_exit();
Heiko Carstens5a489b92006-10-06 16:38:35 +0200648 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651#ifdef CONFIG_CCW_CONSOLE
652static struct subchannel console_subchannel;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100653static struct io_subchannel_private console_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654static int console_subchannel_in_use;
655
Heiko Carstens191fd442008-12-25 13:39:01 +0100656/*
Peter Oberparleitera6818872011-09-26 16:40:35 +0200657 * Use cio_tpi to get a pending interrupt and call the interrupt handler.
658 * Return non-zero if an interrupt was processed, zero otherwise.
Heiko Carstens191fd442008-12-25 13:39:01 +0100659 */
660static int cio_tpi(void)
661{
662 struct tpi_info *tpi_info;
663 struct subchannel *sch;
664 struct irb *irb;
665 int irq_context;
666
Heiko Carstenscbb870c2010-02-26 22:37:43 +0100667 tpi_info = (struct tpi_info *)&S390_lowcore.subchannel_id;
Heiko Carstens191fd442008-12-25 13:39:01 +0100668 if (tpi(NULL) != 1)
669 return 0;
Peter Oberparleitera6818872011-09-26 16:40:35 +0200670 if (tpi_info->adapter_IO) {
671 do_adapter_IO(tpi_info->isc);
672 return 1;
673 }
Heiko Carstenscbb870c2010-02-26 22:37:43 +0100674 irb = (struct irb *)&S390_lowcore.irb;
Heiko Carstens191fd442008-12-25 13:39:01 +0100675 /* Store interrupt response block to lowcore. */
676 if (tsch(tpi_info->schid, irb) != 0)
677 /* Not status pending or not operational. */
678 return 1;
679 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
680 if (!sch)
681 return 1;
682 irq_context = in_interrupt();
683 if (!irq_context)
684 local_bh_disable();
685 irq_enter();
686 spin_lock(sch->lock);
687 memcpy(&sch->schib.scsw, &irb->scsw, sizeof(union scsw));
688 if (sch->driver && sch->driver->irq)
689 sch->driver->irq(sch);
690 spin_unlock(sch->lock);
691 irq_exit();
692 if (!irq_context)
693 _local_bh_enable();
694 return 1;
695}
696
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100697void *cio_get_console_priv(void)
698{
699 return &console_priv;
700}
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702/*
703 * busy wait for the next interrupt on the console
704 */
Heiko Carstensa8061702008-04-17 07:46:26 +0200705void wait_cons_dev(void)
706 __releases(console_subchannel.lock)
707 __acquires(console_subchannel.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 unsigned long cr6 __attribute__ ((aligned (8)));
710 unsigned long save_cr6 __attribute__ ((aligned (8)));
711
712 /*
713 * before entering the spinlock we may already have
714 * processed the interrupt on a different CPU...
715 */
716 if (!console_subchannel_in_use)
717 return;
718
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200719 /* disable all but the console isc */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 __ctl_store (save_cr6, 6, 6);
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200721 cr6 = 1UL << (31 - CONSOLE_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 __ctl_load (cr6, 6, 6);
723
724 do {
Cornelia Huck2ec22982006-12-08 15:54:26 +0100725 spin_unlock(console_subchannel.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (!cio_tpi())
727 cpu_relax();
Cornelia Huck2ec22982006-12-08 15:54:26 +0100728 spin_lock(console_subchannel.lock);
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200729 } while (console_subchannel.schib.scsw.cmd.actl != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /*
731 * restore previous isc value
732 */
733 __ctl_load (save_cr6, 6, 6);
734}
735
736static int
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800737cio_test_for_console(struct subchannel_id schid, void *data)
738{
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800739 if (stsch_err(schid, &console_subchannel.schib) != 0)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800740 return -ENXIO;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100741 if ((console_subchannel.schib.pmcw.st == SUBCHANNEL_TYPE_IO) &&
742 console_subchannel.schib.pmcw.dnv &&
743 (console_subchannel.schib.pmcw.dev == console_devno)) {
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800744 console_irq = schid.sch_no;
745 return 1; /* found */
746 }
747 return 0;
748}
749
750
751static int
752cio_get_console_sch_no(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800754 struct subchannel_id schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800756 init_subchannel_id(&schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (console_irq != -1) {
758 /* VM provided us with the irq number of the console. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800759 schid.sch_no = console_irq;
Sebastian Ott8821d242010-04-22 17:17:05 +0200760 if (stsch_err(schid, &console_subchannel.schib) != 0 ||
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100761 (console_subchannel.schib.pmcw.st != SUBCHANNEL_TYPE_IO) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 !console_subchannel.schib.pmcw.dnv)
763 return -1;
764 console_devno = console_subchannel.schib.pmcw.dev;
765 } else if (console_devno != -1) {
766 /* At least the console device number is known. */
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800767 for_each_subchannel(cio_test_for_console, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (console_irq == -1)
769 return -1;
770 } else {
771 /* unlike in 2.4, we cannot autoprobe here, since
772 * the channel subsystem is not fully initialized.
773 * With some luck, the HWC console can take over */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return -1;
775 }
776 return console_irq;
777}
778
779struct subchannel *
780cio_probe_console(void)
781{
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800782 int sch_no, ret;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800783 struct subchannel_id schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 if (xchg(&console_subchannel_in_use, 1) != 0)
786 return ERR_PTR(-EBUSY);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800787 sch_no = cio_get_console_sch_no();
788 if (sch_no == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 console_subchannel_in_use = 0;
Michael Ernste6d5a422008-12-25 13:39:36 +0100790 pr_warning("No CCW console was found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return ERR_PTR(-ENODEV);
792 }
793 memset(&console_subchannel, 0, sizeof(struct subchannel));
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800794 init_subchannel_id(&schid);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800795 schid.sch_no = sch_no;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800796 ret = cio_validate_subchannel(&console_subchannel, schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (ret) {
798 console_subchannel_in_use = 0;
799 return ERR_PTR(-ENODEV);
800 }
801
802 /*
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200803 * enable console I/O-interrupt subclass
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 */
Cornelia Huck6ef556c2008-07-14 09:59:01 +0200805 isc_register(CONSOLE_ISC);
Sebastian Ott13952ec2008-12-25 13:39:13 +0100806 console_subchannel.config.isc = CONSOLE_ISC;
807 console_subchannel.config.intparm = (u32)(addr_t)&console_subchannel;
808 ret = cio_commit_config(&console_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (ret) {
Cornelia Huck6ef556c2008-07-14 09:59:01 +0200810 isc_unregister(CONSOLE_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 console_subchannel_in_use = 0;
812 return ERR_PTR(ret);
813 }
814 return &console_subchannel;
815}
816
817void
818cio_release_console(void)
819{
Sebastian Ott13952ec2008-12-25 13:39:13 +0100820 console_subchannel.config.intparm = 0;
821 cio_commit_config(&console_subchannel);
Cornelia Huck6ef556c2008-07-14 09:59:01 +0200822 isc_unregister(CONSOLE_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 console_subchannel_in_use = 0;
824}
825
826/* Bah... hack to catch console special sausages. */
827int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800828cio_is_console(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 if (!console_subchannel_in_use)
831 return 0;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800832 return schid_equal(&schid, &console_subchannel.schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
835struct subchannel *
836cio_get_console_subchannel(void)
837{
838 if (!console_subchannel_in_use)
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200839 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return &console_subchannel;
841}
842
843#endif
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100844static int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800845__disable_subchannel_easy(struct subchannel_id schid, struct schib *schib)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
847 int retry, cc;
848
849 cc = 0;
850 for (retry=0;retry<3;retry++) {
851 schib->pmcw.ena = 0;
Sebastian Ott8821d242010-04-22 17:17:05 +0200852 cc = msch_err(schid, schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (cc)
854 return (cc==3?-ENODEV:-EBUSY);
Sebastian Ott8821d242010-04-22 17:17:05 +0200855 if (stsch_err(schid, schib) || !css_sch_is_valid(schib))
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100856 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (!schib->pmcw.ena)
858 return 0;
859 }
860 return -EBUSY; /* uhm... */
861}
862
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100863static int
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200864__clear_io_subchannel_easy(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 int retry;
867
868 if (csch(schid))
869 return -ENODEV;
870 for (retry=0;retry<20;retry++) {
871 struct tpi_info ti;
872
873 if (tpi(&ti)) {
Heiko Carstenscbb870c2010-02-26 22:37:43 +0100874 tsch(ti.schid, (struct irb *)&S390_lowcore.irb);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800875 if (schid_equal(&ti.schid, &schid))
Cornelia Huck4c24da72005-09-03 15:58:01 -0700876 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200878 udelay_simple(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
880 return -EBUSY;
881}
882
Cornelia Huck9d92a7e2008-07-14 09:59:05 +0200883static void __clear_chsc_subchannel_easy(void)
884{
885 /* It seems we can only wait for a bit here :/ */
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200886 udelay_simple(100);
Cornelia Huck9d92a7e2008-07-14 09:59:05 +0200887}
888
Michael Holzheua45e1412006-12-15 17:18:22 +0100889static int pgm_check_occured;
890
891static void cio_reset_pgm_check_handler(void)
892{
893 pgm_check_occured = 1;
894}
895
Peter Oberparleiterf9c9fe32008-10-10 21:33:15 +0200896static int stsch_reset(struct subchannel_id schid, struct schib *addr)
Michael Holzheua45e1412006-12-15 17:18:22 +0100897{
898 int rc;
899
900 pgm_check_occured = 0;
Heiko Carstensab14de62007-02-05 21:18:37 +0100901 s390_base_pgm_handler_fn = cio_reset_pgm_check_handler;
Sebastian Ott8821d242010-04-22 17:17:05 +0200902 rc = stsch_err(schid, addr);
Heiko Carstensab14de62007-02-05 21:18:37 +0100903 s390_base_pgm_handler_fn = NULL;
Heiko Carstens6faf4442007-01-09 10:18:41 +0100904
Heiko Carstensab14de62007-02-05 21:18:37 +0100905 /* The program check handler could have changed pgm_check_occured. */
Heiko Carstens6faf4442007-01-09 10:18:41 +0100906 barrier();
907
Michael Holzheua45e1412006-12-15 17:18:22 +0100908 if (pgm_check_occured)
909 return -EIO;
910 else
911 return rc;
912}
913
Heiko Carstens15e9b582006-12-04 15:40:26 +0100914static int __shutdown_subchannel_easy(struct subchannel_id schid, void *data)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800915{
916 struct schib schib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Michael Holzheua45e1412006-12-15 17:18:22 +0100918 if (stsch_reset(schid, &schib))
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800919 return -ENXIO;
920 if (!schib.pmcw.ena)
921 return 0;
922 switch(__disable_subchannel_easy(schid, &schib)) {
923 case 0:
924 case -ENODEV:
925 break;
926 default: /* -EBUSY */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200927 switch (schib.pmcw.st) {
928 case SUBCHANNEL_TYPE_IO:
929 if (__clear_io_subchannel_easy(schid))
930 goto out; /* give up... */
931 break;
Cornelia Huck9d92a7e2008-07-14 09:59:05 +0200932 case SUBCHANNEL_TYPE_CHSC:
933 __clear_chsc_subchannel_easy();
934 break;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200935 default:
936 /* No default clear strategy */
937 break;
938 }
Sebastian Ott8821d242010-04-22 17:17:05 +0200939 stsch_err(schid, &schib);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800940 __disable_subchannel_easy(schid, &schib);
941 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200942out:
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800943 return 0;
944}
945
Heiko Carstens15e9b582006-12-04 15:40:26 +0100946static atomic_t chpid_reset_count;
947
948static void s390_reset_chpids_mcck_handler(void)
949{
950 struct crw crw;
951 struct mci *mci;
952
953 /* Check for pending channel report word. */
954 mci = (struct mci *)&S390_lowcore.mcck_interruption_code;
955 if (!mci->cp)
956 return;
957 /* Process channel report words. */
958 while (stcrw(&crw) == 0) {
959 /* Check for responses to RCHP. */
960 if (crw.slct && crw.rsc == CRW_RSC_CPATH)
961 atomic_dec(&chpid_reset_count);
962 }
963}
964
965#define RCHP_TIMEOUT (30 * USEC_PER_SEC)
966static void css_reset(void)
967{
968 int i, ret;
969 unsigned long long timeout;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200970 struct chp_id chpid;
Heiko Carstens15e9b582006-12-04 15:40:26 +0100971
972 /* Reset subchannels. */
973 for_each_subchannel(__shutdown_subchannel_easy, NULL);
974 /* Reset channel paths. */
Heiko Carstensab14de62007-02-05 21:18:37 +0100975 s390_base_mcck_handler_fn = s390_reset_chpids_mcck_handler;
Heiko Carstens15e9b582006-12-04 15:40:26 +0100976 /* Enable channel report machine checks. */
977 __ctl_set_bit(14, 28);
978 /* Temporarily reenable machine checks. */
979 local_mcck_enable();
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200980 chp_id_init(&chpid);
Heiko Carstens15e9b582006-12-04 15:40:26 +0100981 for (i = 0; i <= __MAX_CHPID; i++) {
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200982 chpid.id = i;
983 ret = rchp(chpid);
Heiko Carstens15e9b582006-12-04 15:40:26 +0100984 if ((ret == 0) || (ret == 2))
985 /*
986 * rchp either succeeded, or another rchp is already
987 * in progress. In either case, we'll get a crw.
988 */
989 atomic_inc(&chpid_reset_count);
990 }
991 /* Wait for machine check for all channel paths. */
992 timeout = get_clock() + (RCHP_TIMEOUT << 12);
993 while (atomic_read(&chpid_reset_count) != 0) {
994 if (get_clock() > timeout)
995 break;
996 cpu_relax();
997 }
998 /* Disable machine checks again. */
999 local_mcck_disable();
1000 /* Disable channel report machine checks. */
1001 __ctl_clear_bit(14, 28);
Heiko Carstensab14de62007-02-05 21:18:37 +01001002 s390_base_mcck_handler_fn = NULL;
Heiko Carstens15e9b582006-12-04 15:40:26 +01001003}
1004
1005static struct reset_call css_reset_call = {
1006 .fn = css_reset,
1007};
1008
1009static int __init init_css_reset_call(void)
1010{
1011 atomic_set(&chpid_reset_count, 0);
1012 register_reset_call(&css_reset_call);
1013 return 0;
1014}
1015
1016arch_initcall(init_css_reset_call);
1017
1018struct sch_match_id {
1019 struct subchannel_id schid;
1020 struct ccw_dev_id devid;
1021 int rc;
1022};
1023
1024static int __reipl_subchannel_match(struct subchannel_id schid, void *data)
1025{
1026 struct schib schib;
1027 struct sch_match_id *match_id = data;
1028
Michael Holzheua45e1412006-12-15 17:18:22 +01001029 if (stsch_reset(schid, &schib))
Heiko Carstens15e9b582006-12-04 15:40:26 +01001030 return -ENXIO;
Cornelia Huckb279a4f2008-01-26 14:10:45 +01001031 if ((schib.pmcw.st == SUBCHANNEL_TYPE_IO) && schib.pmcw.dnv &&
Heiko Carstens15e9b582006-12-04 15:40:26 +01001032 (schib.pmcw.dev == match_id->devid.devno) &&
1033 (schid.ssid == match_id->devid.ssid)) {
1034 match_id->schid = schid;
1035 match_id->rc = 0;
1036 return 1;
1037 }
1038 return 0;
1039}
1040
1041static int reipl_find_schid(struct ccw_dev_id *devid,
1042 struct subchannel_id *schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001044 struct sch_match_id match_id;
1045
1046 match_id.devid = *devid;
1047 match_id.rc = -ENODEV;
Heiko Carstens15e9b582006-12-04 15:40:26 +01001048 for_each_subchannel(__reipl_subchannel_match, &match_id);
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001049 if (match_id.rc == 0)
1050 *schid = match_id.schid;
1051 return match_id.rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001054extern void do_reipl_asm(__u32 schid);
1055
1056/* Make sure all subchannels are quiet before we re-ipl an lpar. */
1057void reipl_ccw_dev(struct ccw_dev_id *devid)
1058{
1059 struct subchannel_id schid;
1060
Heiko Carstens15e9b582006-12-04 15:40:26 +01001061 s390_reset_system();
1062 if (reipl_find_schid(devid, &schid) != 0)
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001063 panic("IPL Device not found\n");
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001064 do_reipl_asm(*((__u32*)&schid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
Heiko Carstense87bfe52006-09-20 15:59:15 +02001066
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001067int __init cio_get_iplinfo(struct cio_iplinfo *iplinfo)
Heiko Carstense87bfe52006-09-20 15:59:15 +02001068{
1069 struct subchannel_id schid;
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001070 struct schib schib;
Heiko Carstense87bfe52006-09-20 15:59:15 +02001071
Heiko Carstenscbb870c2010-02-26 22:37:43 +01001072 schid = *(struct subchannel_id *)&S390_lowcore.subchannel_id;
Heiko Carstense87bfe52006-09-20 15:59:15 +02001073 if (!schid.one)
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001074 return -ENODEV;
Sebastian Ott8821d242010-04-22 17:17:05 +02001075 if (stsch_err(schid, &schib))
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001076 return -ENODEV;
Cornelia Huckb279a4f2008-01-26 14:10:45 +01001077 if (schib.pmcw.st != SUBCHANNEL_TYPE_IO)
1078 return -ENODEV;
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001079 if (!schib.pmcw.dnv)
1080 return -ENODEV;
1081 iplinfo->devno = schib.pmcw.dev;
1082 iplinfo->is_qdio = schib.pmcw.qf;
1083 return 0;
Heiko Carstense87bfe52006-09-20 15:59:15 +02001084}
Peter Oberparleiter83262d62008-07-14 09:58:51 +02001085
1086/**
1087 * cio_tm_start_key - perform start function
1088 * @sch: subchannel on which to perform the start function
1089 * @tcw: transport-command word to be started
1090 * @lpm: mask of paths to use
1091 * @key: storage key to use for storage access
1092 *
1093 * Start the tcw on the given subchannel. Return zero on success, non-zero
1094 * otherwise.
1095 */
1096int cio_tm_start_key(struct subchannel *sch, struct tcw *tcw, u8 lpm, u8 key)
1097{
1098 int cc;
1099 union orb *orb = &to_io_private(sch)->orb;
1100
1101 memset(orb, 0, sizeof(union orb));
1102 orb->tm.intparm = (u32) (addr_t) sch;
1103 orb->tm.key = key >> 4;
1104 orb->tm.b = 1;
1105 orb->tm.lpm = lpm ? lpm : sch->lpm;
1106 orb->tm.tcw = (u32) (addr_t) tcw;
1107 cc = ssch(sch->schid, orb);
1108 switch (cc) {
1109 case 0:
1110 return 0;
1111 case 1:
1112 case 2:
1113 return -EBUSY;
1114 default:
1115 return cio_start_handle_notoper(sch, lpm);
1116 }
1117}
1118
1119/**
1120 * cio_tm_intrg - perform interrogate function
1121 * @sch - subchannel on which to perform the interrogate function
1122 *
1123 * If the specified subchannel is running in transport-mode, perform the
1124 * interrogate function. Return zero on success, non-zero otherwie.
1125 */
1126int cio_tm_intrg(struct subchannel *sch)
1127{
1128 int cc;
1129
1130 if (!to_io_private(sch)->orb.tm.b)
1131 return -EINVAL;
1132 cc = xsch(sch->schid);
1133 switch (cc) {
1134 case 0:
1135 case 2:
1136 return 0;
1137 case 1:
1138 return -EBUSY;
1139 default:
1140 return -ENODEV;
1141 }
1142}