blob: 9e058c4657a3fdef34a26f00f3e1fc3601d23841 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * S/390 common I/O routines -- low level i/o calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 1999, 2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Ingo Adlung (adlung@de.ibm.com)
Cornelia Huck4ce3b302006-01-14 13:21:04 -08006 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Arnd Bergmann (arndb@de.ibm.com)
8 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 */
10
Michael Ernste6d5a422008-12-25 13:39:36 +010011#define KMSG_COMPONENT "cio"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
Heiko Carstens88dbd202009-06-12 10:26:46 +020014#include <linux/ftrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/device.h>
19#include <linux/kernel_stat.h>
20#include <linux/interrupt.h>
Thomas Gleixner257ceab2014-03-04 23:57:29 +010021#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/cio.h>
23#include <asm/delay.h>
24#include <asm/irq.h>
Heiko Carstens5a489b982006-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>
Frederic Weisbeckerbfc3f022014-03-05 16:33:42 +010032#include <linux/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{
Sebastian Ottf7e1e652014-02-17 11:16:10 +010058 cio_debug_msg_id = debug_register("cio_msg", 16, 1, 11 * 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);
Sebastian Ottf7e1e652014-02-17 11:16:10 +010068 cio_debug_crw_id = debug_register("cio_crw", 8, 1, 8 * 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{
Sebastian Ott13952ec2008-12-25 13:39:13 +0100346 int ccode, retry, ret = 0;
Sebastian Ott1bc89272014-02-05 13:36:05 +0100347 struct schib schib;
348 struct irb irb;
Sebastian Ott13952ec2008-12-25 13:39:13 +0100349
Sebastian Ott8821d242010-04-22 17:17:05 +0200350 if (stsch_err(sch->schid, &schib) || !css_sch_is_valid(&schib))
Sebastian Ott13952ec2008-12-25 13:39:13 +0100351 return -ENODEV;
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 for (retry = 0; retry < 5; retry++) {
Sebastian Ott13952ec2008-12-25 13:39:13 +0100354 /* copy desired changes to local schib */
355 cio_apply_config(sch, &schib);
356 ccode = msch_err(sch->schid, &schib);
357 if (ccode < 0) /* -EIO if msch gets a program check. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return ccode;
359 switch (ccode) {
Coly Li73ac36e2009-01-07 18:09:16 -0800360 case 0: /* successful */
Sebastian Ott8821d242010-04-22 17:17:05 +0200361 if (stsch_err(sch->schid, &schib) ||
Sebastian Ott13952ec2008-12-25 13:39:13 +0100362 !css_sch_is_valid(&schib))
363 return -ENODEV;
364 if (cio_check_config(sch, &schib)) {
365 /* commit changes from local schib */
366 memcpy(&sch->schib, &schib, sizeof(schib));
367 return 0;
368 }
369 ret = -EAGAIN;
370 break;
371 case 1: /* status pending */
Sebastian Ott1bc89272014-02-05 13:36:05 +0100372 ret = -EBUSY;
373 if (tsch(sch->schid, &irb))
374 return ret;
375 break;
Sebastian Ott13952ec2008-12-25 13:39:13 +0100376 case 2: /* busy */
377 udelay(100); /* allow for recovery */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 ret = -EBUSY;
379 break;
Sebastian Ott13952ec2008-12-25 13:39:13 +0100380 case 3: /* not operational */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return -ENODEV;
382 }
383 }
384 return ret;
385}
386
Cornelia Huck44a1c192008-07-14 09:58:47 +0200387/**
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100388 * cio_update_schib - Perform stsch and update schib if subchannel is valid.
389 * @sch: subchannel on which to perform stsch
390 * Return zero on success, -ENODEV otherwise.
391 */
392int cio_update_schib(struct subchannel *sch)
393{
394 struct schib schib;
395
Sebastian Ott8821d242010-04-22 17:17:05 +0200396 if (stsch_err(sch->schid, &schib) || !css_sch_is_valid(&schib))
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100397 return -ENODEV;
398
399 memcpy(&sch->schib, &schib, sizeof(schib));
400 return 0;
401}
402EXPORT_SYMBOL_GPL(cio_update_schib);
403
404/**
Cornelia Huck44a1c192008-07-14 09:58:47 +0200405 * cio_enable_subchannel - enable a subchannel.
406 * @sch: subchannel to be enabled
407 * @intparm: interruption parameter to set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 */
Cornelia Huckedf22092008-04-30 13:38:39 +0200409int cio_enable_subchannel(struct subchannel *sch, u32 intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 int ret;
412
Sebastian Ottefd986d2009-09-11 10:28:18 +0200413 CIO_TRACE_EVENT(2, "ensch");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200414 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100416 if (sch_is_pseudo_sch(sch))
417 return -EINVAL;
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100418 if (cio_update_schib(sch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return -ENODEV;
420
Sebastian Ott13952ec2008-12-25 13:39:13 +0100421 sch->config.ena = 1;
422 sch->config.isc = sch->isc;
423 sch->config.intparm = intparm;
424
Sebastian Ott1bc89272014-02-05 13:36:05 +0100425 ret = cio_commit_config(sch);
426 if (ret == -EIO) {
427 /*
428 * Got a program check in msch. Try without
429 * the concurrent sense bit the next time.
430 */
431 sch->config.csense = 0;
Sebastian Ott13952ec2008-12-25 13:39:13 +0100432 ret = cio_commit_config(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
Sebastian Ottefd986d2009-09-11 10:28:18 +0200434 CIO_HEX_EVENT(2, &ret, sizeof(ret));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return ret;
436}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200437EXPORT_SYMBOL_GPL(cio_enable_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Cornelia Huck44a1c192008-07-14 09:58:47 +0200439/**
440 * cio_disable_subchannel - disable a subchannel.
441 * @sch: subchannel to disable
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 */
Cornelia Huck44a1c192008-07-14 09:58:47 +0200443int cio_disable_subchannel(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 int ret;
446
Sebastian Ottefd986d2009-09-11 10:28:18 +0200447 CIO_TRACE_EVENT(2, "dissch");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200448 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100450 if (sch_is_pseudo_sch(sch))
451 return 0;
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100452 if (cio_update_schib(sch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return -ENODEV;
454
Sebastian Ott13952ec2008-12-25 13:39:13 +0100455 sch->config.ena = 0;
Sebastian Ott1bc89272014-02-05 13:36:05 +0100456 ret = cio_commit_config(sch);
Sebastian Ott13952ec2008-12-25 13:39:13 +0100457
Sebastian Ottefd986d2009-09-11 10:28:18 +0200458 CIO_HEX_EVENT(2, &ret, sizeof(ret));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return ret;
460}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200461EXPORT_SYMBOL_GPL(cio_disable_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200463static int cio_check_devno_blacklisted(struct subchannel *sch)
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200464{
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200465 if (is_blacklisted(sch->schid.ssid, sch->schib.pmcw.dev)) {
466 /*
467 * This device must not be known to Linux. So we simply
468 * say that there is no device and return ENODEV.
469 */
470 CIO_MSG_EVENT(6, "Blacklisted device detected "
471 "at devno %04X, subchannel set %x\n",
472 sch->schib.pmcw.dev, sch->schid.ssid);
473 return -ENODEV;
474 }
475 return 0;
476}
477
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200478static int cio_validate_io_subchannel(struct subchannel *sch)
479{
480 /* Initialization for io subchannels. */
481 if (!css_sch_is_valid(&sch->schib))
482 return -ENODEV;
483
484 /* Devno is valid. */
485 return cio_check_devno_blacklisted(sch);
486}
487
488static int cio_validate_msg_subchannel(struct subchannel *sch)
489{
490 /* Initialization for message subchannels. */
491 if (!css_sch_is_valid(&sch->schib))
492 return -ENODEV;
493
494 /* Devno is valid. */
495 return cio_check_devno_blacklisted(sch);
496}
497
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200498/**
499 * cio_validate_subchannel - basic validation of subchannel
500 * @sch: subchannel structure to be filled out
501 * @schid: subchannel id
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 *
503 * Find out subchannel type and initialize struct subchannel.
504 * Return codes:
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200505 * 0 on success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 * -ENXIO for non-defined subchannels
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200507 * -ENODEV for invalid subchannels or blacklisted devices
508 * -EIO for subchannels in an invalid subchannel set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200510int cio_validate_subchannel(struct subchannel *sch, struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 char dbf_txt[15];
513 int ccode;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100514 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200516 sprintf(dbf_txt, "valsch%x", schid.sch_no);
517 CIO_TRACE_EVENT(4, dbf_txt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /*
520 * The first subchannel that is not-operational (ccode==3)
Sebastian Otte5dcf002013-04-13 13:08:01 +0200521 * indicates that there aren't any more devices available.
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800522 * If stsch gets an exception, it means the current subchannel set
Sebastian Otte5dcf002013-04-13 13:08:01 +0200523 * is not valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 */
Sebastian Otte5dcf002013-04-13 13:08:01 +0200525 ccode = stsch_err(schid, &sch->schib);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100526 if (ccode) {
527 err = (ccode == 3) ? -ENXIO : ccode;
528 goto out;
529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 sch->st = sch->schib.pmcw.st;
Sebastian Otte5dcf002013-04-13 13:08:01 +0200531 sch->schid = schid;
Cornelia Huckc820de32008-07-14 09:58:45 +0200532
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200533 switch (sch->st) {
534 case SUBCHANNEL_TYPE_IO:
535 err = cio_validate_io_subchannel(sch);
536 break;
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200537 case SUBCHANNEL_TYPE_MSG:
538 err = cio_validate_msg_subchannel(sch);
539 break;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200540 default:
541 err = 0;
542 }
543 if (err)
Cornelia Huck2ec22982006-12-08 15:54:26 +0100544 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200546 CIO_MSG_EVENT(4, "Subchannel 0.%x.%04x reports subchannel type %04X\n",
547 sch->schid.ssid, sch->schid.sch_no, sch->st);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100548out:
Cornelia Huck2ec22982006-12-08 15:54:26 +0100549 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
552/*
Martin Schwidefsky1f44a2252013-06-27 09:01:09 +0200553 * do_cio_interrupt() handles all normal I/O device IRQ's
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 */
Martin Schwidefsky1f44a2252013-06-27 09:01:09 +0200555static irqreturn_t do_cio_interrupt(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Martin Schwidefsky1f44a2252013-06-27 09:01:09 +0200557 struct tpi_info *tpi_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 struct subchannel *sch;
559 struct irb *irb;
560
Christoph Lameter4a6f4fe2010-12-06 11:16:24 -0600561 __this_cpu_write(s390_idle.nohz_delay, 1);
Martin Schwidefsky1f44a2252013-06-27 09:01:09 +0200562 tpi_info = (struct tpi_info *) &get_irq_regs()->int_code;
Martin Schwidefsky48f6b002013-06-17 14:54:02 +0200563 irb = (struct irb *) &S390_lowcore.irb;
Martin Schwidefsky48f6b002013-06-17 14:54:02 +0200564 sch = (struct subchannel *)(unsigned long) tpi_info->intparm;
565 if (!sch) {
566 /* Clear pending interrupt condition. */
567 inc_irq_stat(IRQIO_CIO);
568 tsch(tpi_info->schid, irb);
Martin Schwidefsky1f44a2252013-06-27 09:01:09 +0200569 return IRQ_HANDLED;
Martin Schwidefsky48f6b002013-06-17 14:54:02 +0200570 }
571 spin_lock(sch->lock);
572 /* Store interrupt response block to lowcore. */
573 if (tsch(tpi_info->schid, irb) == 0) {
574 /* Keep subchannel information word up to date. */
575 memcpy (&sch->schib.scsw, &irb->scsw, sizeof (irb->scsw));
576 /* Call interrupt handler if there is one. */
577 if (sch->driver && sch->driver->irq)
578 sch->driver->irq(sch);
579 else
Heiko Carstens420f42e2013-01-02 15:18:18 +0100580 inc_irq_stat(IRQIO_CIO);
Martin Schwidefsky48f6b002013-06-17 14:54:02 +0200581 } else
582 inc_irq_stat(IRQIO_CIO);
583 spin_unlock(sch->lock);
Martin Schwidefsky1f44a2252013-06-27 09:01:09 +0200584
585 return IRQ_HANDLED;
586}
587
Martin Schwidefsky1f44a2252013-06-27 09:01:09 +0200588static struct irqaction io_interrupt = {
589 .name = "IO",
590 .handler = do_cio_interrupt,
591};
592
593void __init init_cio_interrupts(void)
594{
595 irq_set_chip_and_handler(IO_INTERRUPT,
596 &dummy_irq_chip, handle_percpu_irq);
597 setup_irq(IO_INTERRUPT, &io_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
600#ifdef CONFIG_CCW_CONSOLE
Sebastian Ott863fc842013-04-13 13:01:50 +0200601static struct subchannel *console_sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Heiko Carstens191fd442008-12-25 13:39:01 +0100603/*
Martin Schwidefskyb603d252012-05-09 16:27:36 +0200604 * Use cio_tsch to update the subchannel status and call the interrupt handler
Sebastian Ott188561a2013-04-13 12:53:21 +0200605 * if status had been pending. Called with the subchannel's lock held.
Heiko Carstens191fd442008-12-25 13:39:01 +0100606 */
Sebastian Ott188561a2013-04-13 12:53:21 +0200607void cio_tsch(struct subchannel *sch)
Heiko Carstens191fd442008-12-25 13:39:01 +0100608{
Heiko Carstens191fd442008-12-25 13:39:01 +0100609 struct irb *irb;
610 int irq_context;
611
Heiko Carstenscbb870c2010-02-26 22:37:43 +0100612 irb = (struct irb *)&S390_lowcore.irb;
Heiko Carstens191fd442008-12-25 13:39:01 +0100613 /* Store interrupt response block to lowcore. */
Martin Schwidefskyb603d252012-05-09 16:27:36 +0200614 if (tsch(sch->schid, irb) != 0)
Heiko Carstens191fd442008-12-25 13:39:01 +0100615 /* Not status pending or not operational. */
Martin Schwidefskyb603d252012-05-09 16:27:36 +0200616 return;
Heiko Carstens191fd442008-12-25 13:39:01 +0100617 memcpy(&sch->schib.scsw, &irb->scsw, sizeof(union scsw));
Martin Schwidefskyb603d252012-05-09 16:27:36 +0200618 /* Call interrupt handler with updated status. */
619 irq_context = in_interrupt();
620 if (!irq_context) {
621 local_bh_disable();
622 irq_enter();
623 }
Thomas Gleixnerbc5dfcf2014-02-23 21:40:17 +0000624 kstat_incr_irq_this_cpu(IO_INTERRUPT);
Heiko Carstens191fd442008-12-25 13:39:01 +0100625 if (sch->driver && sch->driver->irq)
626 sch->driver->irq(sch);
Peter Oberparleiterde400d62011-10-30 15:16:04 +0100627 else
Heiko Carstens420f42e2013-01-02 15:18:18 +0100628 inc_irq_stat(IRQIO_CIO);
Martin Schwidefskyb603d252012-05-09 16:27:36 +0200629 if (!irq_context) {
630 irq_exit();
Heiko Carstens191fd442008-12-25 13:39:01 +0100631 _local_bh_enable();
Martin Schwidefskyb603d252012-05-09 16:27:36 +0200632 }
Heiko Carstens191fd442008-12-25 13:39:01 +0100633}
634
Sebastian Ott863fc842013-04-13 13:01:50 +0200635static int cio_test_for_console(struct subchannel_id schid, void *data)
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100636{
Sebastian Ott863fc842013-04-13 13:01:50 +0200637 struct schib schib;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100638
Sebastian Ott863fc842013-04-13 13:01:50 +0200639 if (stsch_err(schid, &schib) != 0)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800640 return -ENXIO;
Sebastian Ott863fc842013-04-13 13:01:50 +0200641 if ((schib.pmcw.st == SUBCHANNEL_TYPE_IO) && schib.pmcw.dnv &&
642 (schib.pmcw.dev == console_devno)) {
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800643 console_irq = schid.sch_no;
644 return 1; /* found */
645 }
646 return 0;
647}
648
Sebastian Ott863fc842013-04-13 13:01:50 +0200649static int cio_get_console_sch_no(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800651 struct subchannel_id schid;
Sebastian Ott863fc842013-04-13 13:01:50 +0200652 struct schib schib;
653
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800654 init_subchannel_id(&schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (console_irq != -1) {
656 /* VM provided us with the irq number of the console. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800657 schid.sch_no = console_irq;
Sebastian Ott863fc842013-04-13 13:01:50 +0200658 if (stsch_err(schid, &schib) != 0 ||
659 (schib.pmcw.st != SUBCHANNEL_TYPE_IO) || !schib.pmcw.dnv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return -1;
Sebastian Ott863fc842013-04-13 13:01:50 +0200661 console_devno = schib.pmcw.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 } else if (console_devno != -1) {
663 /* At least the console device number is known. */
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800664 for_each_subchannel(cio_test_for_console, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666 return console_irq;
667}
668
Sebastian Ott863fc842013-04-13 13:01:50 +0200669struct subchannel *cio_probe_console(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800671 struct subchannel_id schid;
Sebastian Ott863fc842013-04-13 13:01:50 +0200672 struct subchannel *sch;
673 int sch_no, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800675 sch_no = cio_get_console_sch_no();
676 if (sch_no == -1) {
Michael Ernste6d5a422008-12-25 13:39:36 +0100677 pr_warning("No CCW console was found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return ERR_PTR(-ENODEV);
679 }
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800680 init_subchannel_id(&schid);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800681 schid.sch_no = sch_no;
Sebastian Ott863fc842013-04-13 13:01:50 +0200682 sch = css_alloc_subchannel(schid);
683 if (IS_ERR(sch))
684 return sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Cornelia Huck6ef556c2008-07-14 09:59:01 +0200686 isc_register(CONSOLE_ISC);
Sebastian Ott863fc842013-04-13 13:01:50 +0200687 sch->config.isc = CONSOLE_ISC;
688 sch->config.intparm = (u32)(addr_t)sch;
689 ret = cio_commit_config(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (ret) {
Cornelia Huck6ef556c2008-07-14 09:59:01 +0200691 isc_unregister(CONSOLE_ISC);
Sebastian Ott863fc842013-04-13 13:01:50 +0200692 put_device(&sch->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return ERR_PTR(ret);
694 }
Sebastian Ott863fc842013-04-13 13:01:50 +0200695 console_sch = sch;
696 return sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Sebastian Ott863fc842013-04-13 13:01:50 +0200699int cio_is_console(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Sebastian Ott863fc842013-04-13 13:01:50 +0200701 if (!console_sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return 0;
Sebastian Ott863fc842013-04-13 13:01:50 +0200703 return schid_equal(&schid, &console_sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
Sebastian Ott14556b32013-04-13 13:03:54 +0200706void cio_register_early_subchannels(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Sebastian Ott14556b32013-04-13 13:03:54 +0200708 int ret;
709
710 if (!console_sch)
711 return;
712
713 ret = css_register_subchannel(console_sch);
714 if (ret)
715 put_device(&console_sch->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
Sebastian Ott863fc842013-04-13 13:01:50 +0200717#endif /* CONFIG_CCW_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100719static int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800720__disable_subchannel_easy(struct subchannel_id schid, struct schib *schib)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 int retry, cc;
723
724 cc = 0;
725 for (retry=0;retry<3;retry++) {
726 schib->pmcw.ena = 0;
Sebastian Ott8821d242010-04-22 17:17:05 +0200727 cc = msch_err(schid, schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (cc)
729 return (cc==3?-ENODEV:-EBUSY);
Sebastian Ott8821d242010-04-22 17:17:05 +0200730 if (stsch_err(schid, schib) || !css_sch_is_valid(schib))
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100731 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (!schib->pmcw.ena)
733 return 0;
734 }
735 return -EBUSY; /* uhm... */
736}
737
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100738static int
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200739__clear_io_subchannel_easy(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 int retry;
742
743 if (csch(schid))
744 return -ENODEV;
745 for (retry=0;retry<20;retry++) {
746 struct tpi_info ti;
747
748 if (tpi(&ti)) {
Heiko Carstenscbb870c2010-02-26 22:37:43 +0100749 tsch(ti.schid, (struct irb *)&S390_lowcore.irb);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800750 if (schid_equal(&ti.schid, &schid))
Cornelia Huck4c24da72005-09-03 15:58:01 -0700751 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200753 udelay_simple(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755 return -EBUSY;
756}
757
Cornelia Huck9d92a7e2008-07-14 09:59:05 +0200758static void __clear_chsc_subchannel_easy(void)
759{
760 /* It seems we can only wait for a bit here :/ */
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200761 udelay_simple(100);
Cornelia Huck9d92a7e2008-07-14 09:59:05 +0200762}
763
Michael Holzheua45e1412006-12-15 17:18:22 +0100764static int pgm_check_occured;
765
766static void cio_reset_pgm_check_handler(void)
767{
768 pgm_check_occured = 1;
769}
770
Peter Oberparleiterf9c9fe32008-10-10 21:33:15 +0200771static int stsch_reset(struct subchannel_id schid, struct schib *addr)
Michael Holzheua45e1412006-12-15 17:18:22 +0100772{
773 int rc;
774
775 pgm_check_occured = 0;
Heiko Carstensab14de62007-02-05 21:18:37 +0100776 s390_base_pgm_handler_fn = cio_reset_pgm_check_handler;
Sebastian Ott8821d242010-04-22 17:17:05 +0200777 rc = stsch_err(schid, addr);
Heiko Carstensab14de62007-02-05 21:18:37 +0100778 s390_base_pgm_handler_fn = NULL;
Heiko Carstens6faf4442007-01-09 10:18:41 +0100779
Heiko Carstensab14de62007-02-05 21:18:37 +0100780 /* The program check handler could have changed pgm_check_occured. */
Heiko Carstens6faf4442007-01-09 10:18:41 +0100781 barrier();
782
Michael Holzheua45e1412006-12-15 17:18:22 +0100783 if (pgm_check_occured)
784 return -EIO;
785 else
786 return rc;
787}
788
Heiko Carstens15e9b582006-12-04 15:40:26 +0100789static int __shutdown_subchannel_easy(struct subchannel_id schid, void *data)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800790{
791 struct schib schib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Michael Holzheua45e1412006-12-15 17:18:22 +0100793 if (stsch_reset(schid, &schib))
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800794 return -ENXIO;
795 if (!schib.pmcw.ena)
796 return 0;
797 switch(__disable_subchannel_easy(schid, &schib)) {
798 case 0:
799 case -ENODEV:
800 break;
801 default: /* -EBUSY */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200802 switch (schib.pmcw.st) {
803 case SUBCHANNEL_TYPE_IO:
804 if (__clear_io_subchannel_easy(schid))
805 goto out; /* give up... */
806 break;
Cornelia Huck9d92a7e2008-07-14 09:59:05 +0200807 case SUBCHANNEL_TYPE_CHSC:
808 __clear_chsc_subchannel_easy();
809 break;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200810 default:
811 /* No default clear strategy */
812 break;
813 }
Sebastian Ott8821d242010-04-22 17:17:05 +0200814 stsch_err(schid, &schib);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800815 __disable_subchannel_easy(schid, &schib);
816 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200817out:
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800818 return 0;
819}
820
Heiko Carstens15e9b582006-12-04 15:40:26 +0100821static atomic_t chpid_reset_count;
822
823static void s390_reset_chpids_mcck_handler(void)
824{
825 struct crw crw;
826 struct mci *mci;
827
828 /* Check for pending channel report word. */
829 mci = (struct mci *)&S390_lowcore.mcck_interruption_code;
830 if (!mci->cp)
831 return;
832 /* Process channel report words. */
833 while (stcrw(&crw) == 0) {
834 /* Check for responses to RCHP. */
835 if (crw.slct && crw.rsc == CRW_RSC_CPATH)
836 atomic_dec(&chpid_reset_count);
837 }
838}
839
840#define RCHP_TIMEOUT (30 * USEC_PER_SEC)
841static void css_reset(void)
842{
843 int i, ret;
844 unsigned long long timeout;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200845 struct chp_id chpid;
Heiko Carstens15e9b582006-12-04 15:40:26 +0100846
847 /* Reset subchannels. */
848 for_each_subchannel(__shutdown_subchannel_easy, NULL);
849 /* Reset channel paths. */
Heiko Carstensab14de62007-02-05 21:18:37 +0100850 s390_base_mcck_handler_fn = s390_reset_chpids_mcck_handler;
Heiko Carstens15e9b582006-12-04 15:40:26 +0100851 /* Enable channel report machine checks. */
852 __ctl_set_bit(14, 28);
853 /* Temporarily reenable machine checks. */
854 local_mcck_enable();
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200855 chp_id_init(&chpid);
Heiko Carstens15e9b582006-12-04 15:40:26 +0100856 for (i = 0; i <= __MAX_CHPID; i++) {
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200857 chpid.id = i;
858 ret = rchp(chpid);
Heiko Carstens15e9b582006-12-04 15:40:26 +0100859 if ((ret == 0) || (ret == 2))
860 /*
861 * rchp either succeeded, or another rchp is already
862 * in progress. In either case, we'll get a crw.
863 */
864 atomic_inc(&chpid_reset_count);
865 }
866 /* Wait for machine check for all channel paths. */
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200867 timeout = get_tod_clock_fast() + (RCHP_TIMEOUT << 12);
Heiko Carstens15e9b582006-12-04 15:40:26 +0100868 while (atomic_read(&chpid_reset_count) != 0) {
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200869 if (get_tod_clock_fast() > timeout)
Heiko Carstens15e9b582006-12-04 15:40:26 +0100870 break;
871 cpu_relax();
872 }
873 /* Disable machine checks again. */
874 local_mcck_disable();
875 /* Disable channel report machine checks. */
876 __ctl_clear_bit(14, 28);
Heiko Carstensab14de62007-02-05 21:18:37 +0100877 s390_base_mcck_handler_fn = NULL;
Heiko Carstens15e9b582006-12-04 15:40:26 +0100878}
879
880static struct reset_call css_reset_call = {
881 .fn = css_reset,
882};
883
884static int __init init_css_reset_call(void)
885{
886 atomic_set(&chpid_reset_count, 0);
887 register_reset_call(&css_reset_call);
888 return 0;
889}
890
891arch_initcall(init_css_reset_call);
892
893struct sch_match_id {
894 struct subchannel_id schid;
895 struct ccw_dev_id devid;
896 int rc;
897};
898
899static int __reipl_subchannel_match(struct subchannel_id schid, void *data)
900{
901 struct schib schib;
902 struct sch_match_id *match_id = data;
903
Michael Holzheua45e1412006-12-15 17:18:22 +0100904 if (stsch_reset(schid, &schib))
Heiko Carstens15e9b582006-12-04 15:40:26 +0100905 return -ENXIO;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100906 if ((schib.pmcw.st == SUBCHANNEL_TYPE_IO) && schib.pmcw.dnv &&
Heiko Carstens15e9b582006-12-04 15:40:26 +0100907 (schib.pmcw.dev == match_id->devid.devno) &&
908 (schid.ssid == match_id->devid.ssid)) {
909 match_id->schid = schid;
910 match_id->rc = 0;
911 return 1;
912 }
913 return 0;
914}
915
916static int reipl_find_schid(struct ccw_dev_id *devid,
917 struct subchannel_id *schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Michael Holzheuff6b8ea2006-09-20 15:58:49 +0200919 struct sch_match_id match_id;
920
921 match_id.devid = *devid;
922 match_id.rc = -ENODEV;
Heiko Carstens15e9b582006-12-04 15:40:26 +0100923 for_each_subchannel(__reipl_subchannel_match, &match_id);
Michael Holzheuff6b8ea2006-09-20 15:58:49 +0200924 if (match_id.rc == 0)
925 *schid = match_id.schid;
926 return match_id.rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
Michael Holzheuff6b8ea2006-09-20 15:58:49 +0200929extern void do_reipl_asm(__u32 schid);
930
931/* Make sure all subchannels are quiet before we re-ipl an lpar. */
932void reipl_ccw_dev(struct ccw_dev_id *devid)
933{
Heiko Carstens41459d32012-09-14 11:09:52 +0200934 struct subchannel_id uninitialized_var(schid);
Michael Holzheuff6b8ea2006-09-20 15:58:49 +0200935
Michael Holzheu60a0c682011-10-30 15:16:40 +0100936 s390_reset_system(NULL, NULL);
Heiko Carstens15e9b582006-12-04 15:40:26 +0100937 if (reipl_find_schid(devid, &schid) != 0)
Michael Holzheuff6b8ea2006-09-20 15:58:49 +0200938 panic("IPL Device not found\n");
Michael Holzheuff6b8ea2006-09-20 15:58:49 +0200939 do_reipl_asm(*((__u32*)&schid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
Heiko Carstense87bfe52006-09-20 15:59:15 +0200941
Heiko Carstens6fc321f2007-04-27 16:01:25 +0200942int __init cio_get_iplinfo(struct cio_iplinfo *iplinfo)
Heiko Carstense87bfe52006-09-20 15:59:15 +0200943{
944 struct subchannel_id schid;
Heiko Carstens6fc321f2007-04-27 16:01:25 +0200945 struct schib schib;
Heiko Carstense87bfe52006-09-20 15:59:15 +0200946
Heiko Carstenscbb870c2010-02-26 22:37:43 +0100947 schid = *(struct subchannel_id *)&S390_lowcore.subchannel_id;
Heiko Carstense87bfe52006-09-20 15:59:15 +0200948 if (!schid.one)
Heiko Carstens6fc321f2007-04-27 16:01:25 +0200949 return -ENODEV;
Sebastian Ott8821d242010-04-22 17:17:05 +0200950 if (stsch_err(schid, &schib))
Heiko Carstens6fc321f2007-04-27 16:01:25 +0200951 return -ENODEV;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100952 if (schib.pmcw.st != SUBCHANNEL_TYPE_IO)
953 return -ENODEV;
Heiko Carstens6fc321f2007-04-27 16:01:25 +0200954 if (!schib.pmcw.dnv)
955 return -ENODEV;
956 iplinfo->devno = schib.pmcw.dev;
957 iplinfo->is_qdio = schib.pmcw.qf;
958 return 0;
Heiko Carstense87bfe52006-09-20 15:59:15 +0200959}
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200960
961/**
962 * cio_tm_start_key - perform start function
963 * @sch: subchannel on which to perform the start function
964 * @tcw: transport-command word to be started
965 * @lpm: mask of paths to use
966 * @key: storage key to use for storage access
967 *
968 * Start the tcw on the given subchannel. Return zero on success, non-zero
969 * otherwise.
970 */
971int cio_tm_start_key(struct subchannel *sch, struct tcw *tcw, u8 lpm, u8 key)
972{
973 int cc;
974 union orb *orb = &to_io_private(sch)->orb;
975
976 memset(orb, 0, sizeof(union orb));
977 orb->tm.intparm = (u32) (addr_t) sch;
978 orb->tm.key = key >> 4;
979 orb->tm.b = 1;
980 orb->tm.lpm = lpm ? lpm : sch->lpm;
981 orb->tm.tcw = (u32) (addr_t) tcw;
982 cc = ssch(sch->schid, orb);
983 switch (cc) {
984 case 0:
985 return 0;
986 case 1:
987 case 2:
988 return -EBUSY;
989 default:
990 return cio_start_handle_notoper(sch, lpm);
991 }
992}
993
994/**
995 * cio_tm_intrg - perform interrogate function
996 * @sch - subchannel on which to perform the interrogate function
997 *
998 * If the specified subchannel is running in transport-mode, perform the
999 * interrogate function. Return zero on success, non-zero otherwie.
1000 */
1001int cio_tm_intrg(struct subchannel *sch)
1002{
1003 int cc;
1004
1005 if (!to_io_private(sch)->orb.tm.b)
1006 return -EINVAL;
1007 cc = xsch(sch->schid);
1008 switch (cc) {
1009 case 0:
1010 case 2:
1011 return 0;
1012 case 1:
1013 return -EBUSY;
1014 default:
1015 return -ENODEV;
1016 }
1017}