blob: 73135c5e9dfbf8edf561b1a5224785a5f2629657 [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
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/cio.h>
22#include <asm/delay.h>
23#include <asm/irq.h>
Heiko Carstens5a489b982006-10-06 16:38:35 +020024#include <asm/irq_regs.h>
Heiko Carstense87bfe52006-09-20 15:59:15 +020025#include <asm/setup.h>
Heiko Carstens15e9b582006-12-04 15:40:26 +010026#include <asm/reset.h>
Michael Holzheu46b05d22007-02-21 10:55:21 +010027#include <asm/ipl.h>
Peter Oberparleitere5854a52007-04-27 16:01:31 +020028#include <asm/chpid.h>
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010029#include <asm/airq.h>
Cornelia Huck3a3fc292008-07-14 09:58:58 +020030#include <asm/isc.h>
Heiko Carstens43ca5c32008-04-17 07:46:23 +020031#include <asm/cpu.h>
Peter Oberparleiter83262d62008-07-14 09:58:51 +020032#include <asm/fcx.h>
Heiko Carstensf5daba12009-03-26 15:24:01 +010033#include <asm/nmi.h>
34#include <asm/crw.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "cio.h"
36#include "css.h"
37#include "chsc.h"
38#include "ioasm.h"
Cornelia Huckcd6b4f22008-01-26 14:10:43 +010039#include "io_sch.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "blacklist.h"
41#include "cio_debug.h"
Peter Oberparleitere6b6e102007-04-27 16:01:28 +020042#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44debug_info_t *cio_debug_msg_id;
45debug_info_t *cio_debug_trace_id;
46debug_info_t *cio_debug_crw_id;
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*
49 * Function: cio_debug_init
Cornelia Huckbc698bc2008-01-26 14:10:42 +010050 * Initializes three debug logs for common I/O:
51 * - cio_msg logs generic cio messages
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * - cio_trace logs the calling of different functions
Cornelia Huckbc698bc2008-01-26 14:10:42 +010053 * - cio_crw logs machine check related cio messages
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 */
Cornelia Huckbc698bc2008-01-26 14:10:42 +010055static int __init cio_debug_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Peter Tiedemann361f4942008-01-26 14:11:30 +010057 cio_debug_msg_id = debug_register("cio_msg", 16, 1, 16 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 if (!cio_debug_msg_id)
59 goto out_unregister;
Cornelia Huckbc698bc2008-01-26 14:10:42 +010060 debug_register_view(cio_debug_msg_id, &debug_sprintf_view);
61 debug_set_level(cio_debug_msg_id, 2);
Peter Tiedemann361f4942008-01-26 14:11:30 +010062 cio_debug_trace_id = debug_register("cio_trace", 16, 1, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (!cio_debug_trace_id)
64 goto out_unregister;
Cornelia Huckbc698bc2008-01-26 14:10:42 +010065 debug_register_view(cio_debug_trace_id, &debug_hex_ascii_view);
66 debug_set_level(cio_debug_trace_id, 2);
Peter Tiedemann361f4942008-01-26 14:11:30 +010067 cio_debug_crw_id = debug_register("cio_crw", 16, 1, 16 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (!cio_debug_crw_id)
69 goto out_unregister;
Cornelia Huckbc698bc2008-01-26 14:10:42 +010070 debug_register_view(cio_debug_crw_id, &debug_sprintf_view);
71 debug_set_level(cio_debug_crw_id, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return 0;
73
74out_unregister:
75 if (cio_debug_msg_id)
Cornelia Huckbc698bc2008-01-26 14:10:42 +010076 debug_unregister(cio_debug_msg_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (cio_debug_trace_id)
Cornelia Huckbc698bc2008-01-26 14:10:42 +010078 debug_unregister(cio_debug_trace_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (cio_debug_crw_id)
Cornelia Huckbc698bc2008-01-26 14:10:42 +010080 debug_unregister(cio_debug_crw_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return -1;
82}
83
84arch_initcall (cio_debug_init);
85
86int
87cio_set_options (struct subchannel *sch, int flags)
88{
89 sch->options.suspend = (flags & DOIO_ALLOW_SUSPEND) != 0;
90 sch->options.prefetch = (flags & DOIO_DENY_PREFETCH) != 0;
91 sch->options.inter = (flags & DOIO_SUPPRESS_INTER) != 0;
92 return 0;
93}
94
95/* FIXME: who wants to use this? */
96int
97cio_get_options (struct subchannel *sch)
98{
99 int flags;
100
101 flags = 0;
102 if (sch->options.suspend)
103 flags |= DOIO_ALLOW_SUSPEND;
104 if (sch->options.prefetch)
105 flags |= DOIO_DENY_PREFETCH;
106 if (sch->options.inter)
107 flags |= DOIO_SUPPRESS_INTER;
108 return flags;
109}
110
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100111static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112cio_start_handle_notoper(struct subchannel *sch, __u8 lpm)
113{
114 char dbf_text[15];
115
116 if (lpm != 0)
117 sch->lpm &= ~lpm;
118 else
119 sch->lpm = 0;
120
Michael Ernst139b83dd2008-05-07 09:22:54 +0200121 CIO_MSG_EVENT(2, "cio_start: 'not oper' status for "
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800122 "subchannel 0.%x.%04x!\n", sch->schid.ssid,
123 sch->schid.sch_no);
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100124
125 if (cio_update_schib(sch))
126 return -ENODEV;
127
Kay Sievers2a0217d2008-10-10 21:33:09 +0200128 sprintf(dbf_text, "no%s", dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 CIO_TRACE_EVENT(0, dbf_text);
130 CIO_HEX_EVENT(0, &sch->schib, sizeof (struct schib));
131
132 return (sch->lpm ? -EACCES : -ENODEV);
133}
134
135int
136cio_start_key (struct subchannel *sch, /* subchannel structure */
137 struct ccw1 * cpa, /* logical channel prog addr */
138 __u8 lpm, /* logical path mask */
139 __u8 key) /* storage key */
140{
141 char dbf_txt[15];
142 int ccode;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200143 union orb *orb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100145 CIO_TRACE_EVENT(4, "stIO");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200146 CIO_TRACE_EVENT(4, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100148 orb = &to_io_private(sch)->orb;
Stefan Weinhuber9adb8c12008-09-16 09:32:19 -0700149 memset(orb, 0, sizeof(union orb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 /* sch is always under 2G. */
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200151 orb->cmd.intparm = (u32)(addr_t)sch;
152 orb->cmd.fmt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200154 orb->cmd.pfch = sch->options.prefetch == 0;
155 orb->cmd.spnd = sch->options.suspend;
156 orb->cmd.ssic = sch->options.suspend && sch->options.inter;
157 orb->cmd.lpm = (lpm != 0) ? lpm : sch->lpm;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800158#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /*
160 * for 64 bit we always support 64 bit IDAWs with 4k page size only
161 */
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200162 orb->cmd.c64 = 1;
163 orb->cmd.i2k = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#endif
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200165 orb->cmd.key = key >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /* issue "Start Subchannel" */
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200167 orb->cmd.cpa = (__u32) __pa(cpa);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100168 ccode = ssch(sch->schid, orb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 /* process condition code */
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100171 sprintf(dbf_txt, "ccode:%d", ccode);
172 CIO_TRACE_EVENT(4, dbf_txt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 switch (ccode) {
175 case 0:
176 /*
177 * initialize device status information
178 */
Peter Oberparleiter23d805b62008-07-14 09:58:50 +0200179 sch->schib.scsw.cmd.actl |= SCSW_ACTL_START_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return 0;
181 case 1: /* status pending */
182 case 2: /* busy */
183 return -EBUSY;
Cornelia Huckc91ebe42008-09-09 12:38:58 +0200184 case 3: /* device/path not operational */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return cio_start_handle_notoper(sch, lpm);
Cornelia Huckc91ebe42008-09-09 12:38:58 +0200186 default:
187 return ccode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189}
190
191int
192cio_start (struct subchannel *sch, struct ccw1 *cpa, __u8 lpm)
193{
Peter Oberparleiter0b642ed2005-05-01 08:58:58 -0700194 return cio_start_key(sch, cpa, lpm, PAGE_DEFAULT_KEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
197/*
198 * resume suspended I/O operation
199 */
200int
201cio_resume (struct subchannel *sch)
202{
203 char dbf_txt[15];
204 int ccode;
205
206 CIO_TRACE_EVENT (4, "resIO");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200207 CIO_TRACE_EVENT(4, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800209 ccode = rsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 sprintf (dbf_txt, "ccode:%d", ccode);
212 CIO_TRACE_EVENT (4, dbf_txt);
213
214 switch (ccode) {
215 case 0:
Peter Oberparleiter23d805b62008-07-14 09:58:50 +0200216 sch->schib.scsw.cmd.actl |= SCSW_ACTL_RESUME_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return 0;
218 case 1:
219 return -EBUSY;
220 case 2:
221 return -EINVAL;
222 default:
223 /*
224 * useless to wait for request completion
225 * as device is no longer operational !
226 */
227 return -ENODEV;
228 }
229}
230
231/*
232 * halt I/O operation
233 */
234int
235cio_halt(struct subchannel *sch)
236{
237 char dbf_txt[15];
238 int ccode;
239
240 if (!sch)
241 return -ENODEV;
242
243 CIO_TRACE_EVENT (2, "haltIO");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200244 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 /*
247 * Issue "Halt subchannel" and process condition code
248 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800249 ccode = hsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 sprintf (dbf_txt, "ccode:%d", ccode);
252 CIO_TRACE_EVENT (2, dbf_txt);
253
254 switch (ccode) {
255 case 0:
Peter Oberparleiter23d805b62008-07-14 09:58:50 +0200256 sch->schib.scsw.cmd.actl |= SCSW_ACTL_HALT_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return 0;
258 case 1: /* status pending */
259 case 2: /* busy */
260 return -EBUSY;
261 default: /* device not operational */
262 return -ENODEV;
263 }
264}
265
266/*
267 * Clear I/O operation
268 */
269int
270cio_clear(struct subchannel *sch)
271{
272 char dbf_txt[15];
273 int ccode;
274
275 if (!sch)
276 return -ENODEV;
277
278 CIO_TRACE_EVENT (2, "clearIO");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200279 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 /*
282 * Issue "Clear subchannel" and process condition code
283 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800284 ccode = csch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 sprintf (dbf_txt, "ccode:%d", ccode);
287 CIO_TRACE_EVENT (2, dbf_txt);
288
289 switch (ccode) {
290 case 0:
Peter Oberparleiter23d805b62008-07-14 09:58:50 +0200291 sch->schib.scsw.cmd.actl |= SCSW_ACTL_CLEAR_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return 0;
293 default: /* device not operational */
294 return -ENODEV;
295 }
296}
297
298/*
299 * Function: cio_cancel
300 * Issues a "Cancel Subchannel" on the specified subchannel
301 * Note: We don't need any fancy intparms and flags here
302 * since xsch is executed synchronously.
303 * Only for common I/O internal use as for now.
304 */
305int
306cio_cancel (struct subchannel *sch)
307{
308 char dbf_txt[15];
309 int ccode;
310
311 if (!sch)
312 return -ENODEV;
313
314 CIO_TRACE_EVENT (2, "cancelIO");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200315 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800317 ccode = xsch (sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 sprintf (dbf_txt, "ccode:%d", ccode);
320 CIO_TRACE_EVENT (2, dbf_txt);
321
322 switch (ccode) {
323 case 0: /* success */
324 /* Update information in scsw. */
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100325 if (cio_update_schib(sch))
326 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return 0;
328 case 1: /* status pending */
329 return -EBUSY;
330 case 2: /* not applicable */
331 return -EINVAL;
332 default: /* not oper */
333 return -ENODEV;
334 }
335}
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Sebastian Ott13952ec2008-12-25 13:39:13 +0100338static void cio_apply_config(struct subchannel *sch, struct schib *schib)
339{
340 schib->pmcw.intparm = sch->config.intparm;
341 schib->pmcw.mbi = sch->config.mbi;
342 schib->pmcw.isc = sch->config.isc;
343 schib->pmcw.ena = sch->config.ena;
344 schib->pmcw.mme = sch->config.mme;
345 schib->pmcw.mp = sch->config.mp;
346 schib->pmcw.csense = sch->config.csense;
347 schib->pmcw.mbfc = sch->config.mbfc;
348 if (sch->config.mbfc)
349 schib->mba = sch->config.mba;
350}
351
352static int cio_check_config(struct subchannel *sch, struct schib *schib)
353{
354 return (schib->pmcw.intparm == sch->config.intparm) &&
355 (schib->pmcw.mbi == sch->config.mbi) &&
356 (schib->pmcw.isc == sch->config.isc) &&
357 (schib->pmcw.ena == sch->config.ena) &&
358 (schib->pmcw.mme == sch->config.mme) &&
359 (schib->pmcw.mp == sch->config.mp) &&
360 (schib->pmcw.csense == sch->config.csense) &&
361 (schib->pmcw.mbfc == sch->config.mbfc) &&
362 (!sch->config.mbfc || (schib->mba == sch->config.mba));
363}
364
365/*
366 * cio_commit_config - apply configuration to the subchannel
367 */
368int cio_commit_config(struct subchannel *sch)
369{
370 struct schib schib;
371 int ccode, retry, ret = 0;
372
373 if (stsch(sch->schid, &schib) || !css_sch_is_valid(&schib))
374 return -ENODEV;
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 for (retry = 0; retry < 5; retry++) {
Sebastian Ott13952ec2008-12-25 13:39:13 +0100377 /* copy desired changes to local schib */
378 cio_apply_config(sch, &schib);
379 ccode = msch_err(sch->schid, &schib);
380 if (ccode < 0) /* -EIO if msch gets a program check. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return ccode;
382 switch (ccode) {
Coly Li73ac36e2009-01-07 18:09:16 -0800383 case 0: /* successful */
Sebastian Ott13952ec2008-12-25 13:39:13 +0100384 if (stsch(sch->schid, &schib) ||
385 !css_sch_is_valid(&schib))
386 return -ENODEV;
387 if (cio_check_config(sch, &schib)) {
388 /* commit changes from local schib */
389 memcpy(&sch->schib, &schib, sizeof(schib));
390 return 0;
391 }
392 ret = -EAGAIN;
393 break;
394 case 1: /* status pending */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return -EBUSY;
Sebastian Ott13952ec2008-12-25 13:39:13 +0100396 case 2: /* busy */
397 udelay(100); /* allow for recovery */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 ret = -EBUSY;
399 break;
Sebastian Ott13952ec2008-12-25 13:39:13 +0100400 case 3: /* not operational */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return -ENODEV;
402 }
403 }
404 return ret;
405}
406
Cornelia Huck44a1c192008-07-14 09:58:47 +0200407/**
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100408 * cio_update_schib - Perform stsch and update schib if subchannel is valid.
409 * @sch: subchannel on which to perform stsch
410 * Return zero on success, -ENODEV otherwise.
411 */
412int cio_update_schib(struct subchannel *sch)
413{
414 struct schib schib;
415
416 if (stsch(sch->schid, &schib) || !css_sch_is_valid(&schib))
417 return -ENODEV;
418
419 memcpy(&sch->schib, &schib, sizeof(schib));
420 return 0;
421}
422EXPORT_SYMBOL_GPL(cio_update_schib);
423
424/**
Cornelia Huck44a1c192008-07-14 09:58:47 +0200425 * cio_enable_subchannel - enable a subchannel.
426 * @sch: subchannel to be enabled
427 * @intparm: interruption parameter to set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 */
Cornelia Huckedf22092008-04-30 13:38:39 +0200429int cio_enable_subchannel(struct subchannel *sch, u32 intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 char dbf_txt[15];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 int retry;
433 int ret;
434
435 CIO_TRACE_EVENT (2, "ensch");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200436 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100438 if (sch_is_pseudo_sch(sch))
439 return -EINVAL;
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100440 if (cio_update_schib(sch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return -ENODEV;
442
Sebastian Ott13952ec2008-12-25 13:39:13 +0100443 sch->config.ena = 1;
444 sch->config.isc = sch->isc;
445 sch->config.intparm = intparm;
446
447 for (retry = 0; retry < 3; retry++) {
448 ret = cio_commit_config(sch);
449 if (ret == -EIO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /*
Sebastian Ott13952ec2008-12-25 13:39:13 +0100451 * Got a program check in msch. Try without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * the concurrent sense bit the next time.
453 */
Sebastian Ott13952ec2008-12-25 13:39:13 +0100454 sch->config.csense = 0;
455 } else if (ret == -EBUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 struct irb irb;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800457 if (tsch(sch->schid, &irb) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 break;
Sebastian Ott13952ec2008-12-25 13:39:13 +0100459 } else
460 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462 sprintf (dbf_txt, "ret:%d", ret);
463 CIO_TRACE_EVENT (2, dbf_txt);
464 return ret;
465}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200466EXPORT_SYMBOL_GPL(cio_enable_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Cornelia Huck44a1c192008-07-14 09:58:47 +0200468/**
469 * cio_disable_subchannel - disable a subchannel.
470 * @sch: subchannel to disable
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 */
Cornelia Huck44a1c192008-07-14 09:58:47 +0200472int cio_disable_subchannel(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 char dbf_txt[15];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 int ret;
476
477 CIO_TRACE_EVENT (2, "dissch");
Kay Sievers2a0217d2008-10-10 21:33:09 +0200478 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100480 if (sch_is_pseudo_sch(sch))
481 return 0;
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100482 if (cio_update_schib(sch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return -ENODEV;
484
Peter Oberparleiter23d805b62008-07-14 09:58:50 +0200485 if (scsw_actl(&sch->schib.scsw) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 /*
487 * the disable function must not be called while there are
488 * requests pending for completion !
489 */
490 return -EBUSY;
491
Sebastian Ott13952ec2008-12-25 13:39:13 +0100492 sch->config.ena = 0;
493 ret = cio_commit_config(sch);
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 sprintf (dbf_txt, "ret:%d", ret);
496 CIO_TRACE_EVENT (2, dbf_txt);
497 return ret;
498}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200499EXPORT_SYMBOL_GPL(cio_disable_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100501int cio_create_sch_lock(struct subchannel *sch)
Cornelia Huck2ec22982006-12-08 15:54:26 +0100502{
503 sch->lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
504 if (!sch->lock)
505 return -ENOMEM;
506 spin_lock_init(sch->lock);
507 return 0;
508}
509
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200510static int cio_check_devno_blacklisted(struct subchannel *sch)
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200511{
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200512 if (is_blacklisted(sch->schid.ssid, sch->schib.pmcw.dev)) {
513 /*
514 * This device must not be known to Linux. So we simply
515 * say that there is no device and return ENODEV.
516 */
517 CIO_MSG_EVENT(6, "Blacklisted device detected "
518 "at devno %04X, subchannel set %x\n",
519 sch->schib.pmcw.dev, sch->schid.ssid);
520 return -ENODEV;
521 }
522 return 0;
523}
524
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200525static int cio_validate_io_subchannel(struct subchannel *sch)
526{
527 /* Initialization for io subchannels. */
528 if (!css_sch_is_valid(&sch->schib))
529 return -ENODEV;
530
531 /* Devno is valid. */
532 return cio_check_devno_blacklisted(sch);
533}
534
535static int cio_validate_msg_subchannel(struct subchannel *sch)
536{
537 /* Initialization for message subchannels. */
538 if (!css_sch_is_valid(&sch->schib))
539 return -ENODEV;
540
541 /* Devno is valid. */
542 return cio_check_devno_blacklisted(sch);
543}
544
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200545/**
546 * cio_validate_subchannel - basic validation of subchannel
547 * @sch: subchannel structure to be filled out
548 * @schid: subchannel id
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 *
550 * Find out subchannel type and initialize struct subchannel.
551 * Return codes:
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200552 * 0 on success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 * -ENXIO for non-defined subchannels
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200554 * -ENODEV for invalid subchannels or blacklisted devices
555 * -EIO for subchannels in an invalid subchannel set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200557int cio_validate_subchannel(struct subchannel *sch, struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 char dbf_txt[15];
560 int ccode;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100561 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200563 sprintf(dbf_txt, "valsch%x", schid.sch_no);
564 CIO_TRACE_EVENT(4, dbf_txt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 /* Nuke all fields. */
567 memset(sch, 0, sizeof(struct subchannel));
568
Cornelia Huck2ec22982006-12-08 15:54:26 +0100569 sch->schid = schid;
570 if (cio_is_console(schid)) {
571 sch->lock = cio_get_console_lock();
572 } else {
573 err = cio_create_sch_lock(sch);
574 if (err)
575 goto out;
576 }
Cornelia Huck6ab48792006-07-12 16:39:50 +0200577 mutex_init(&sch->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /* Set a name for the subchannel */
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +0200579 if (cio_is_console(schid))
580 sch->dev.init_name = cio_get_console_sch_name(schid);
581 else
582 dev_set_name(&sch->dev, "0.%x.%04x", schid.ssid, schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 /*
585 * The first subchannel that is not-operational (ccode==3)
586 * indicates that there aren't any more devices available.
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800587 * If stsch gets an exception, it means the current subchannel set
588 * is not valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800590 ccode = stsch_err (schid, &sch->schib);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100591 if (ccode) {
592 err = (ccode == 3) ? -ENXIO : ccode;
593 goto out;
594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 /* Copy subchannel type from path management control word. */
596 sch->st = sch->schib.pmcw.st;
Cornelia Huckc820de32008-07-14 09:58:45 +0200597
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200598 switch (sch->st) {
599 case SUBCHANNEL_TYPE_IO:
600 err = cio_validate_io_subchannel(sch);
601 break;
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200602 case SUBCHANNEL_TYPE_MSG:
603 err = cio_validate_msg_subchannel(sch);
604 break;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200605 default:
606 err = 0;
607 }
608 if (err)
Cornelia Huck2ec22982006-12-08 15:54:26 +0100609 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200611 CIO_MSG_EVENT(4, "Subchannel 0.%x.%04x reports subchannel type %04X\n",
612 sch->schid.ssid, sch->schid.sch_no, sch->st);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return 0;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100614out:
615 if (!cio_is_console(schid))
616 kfree(sch->lock);
617 sch->lock = NULL;
618 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
621/*
622 * do_IRQ() handles all normal I/O device IRQ's (the special
623 * SMP cross-CPU interrupts have their own specific
624 * handlers).
625 *
626 */
627void
628do_IRQ (struct pt_regs *regs)
629{
630 struct tpi_info *tpi_info;
631 struct subchannel *sch;
632 struct irb *irb;
Heiko Carstens5a489b982006-10-06 16:38:35 +0200633 struct pt_regs *old_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Heiko Carstens5a489b982006-10-06 16:38:35 +0200635 old_regs = set_irq_regs(regs);
Heiko Carstens43ca5c32008-04-17 07:46:23 +0200636 s390_idle_check();
Martin Schwidefsky9cfb9b32008-12-31 15:11:41 +0100637 irq_enter();
Heiko Carstens5a62b192008-04-17 07:46:25 +0200638 if (S390_lowcore.int_clock >= S390_lowcore.clock_comparator)
639 /* Serve timer interrupts first. */
640 clock_comparator_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /*
642 * Get interrupt information from lowcore
643 */
644 tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
645 irb = (struct irb *) __LC_IRB;
646 do {
647 kstat_cpu(smp_processor_id()).irqs[IO_INTERRUPT]++;
648 /*
649 * Non I/O-subchannel thin interrupts are processed differently
650 */
651 if (tpi_info->adapter_IO == 1 &&
652 tpi_info->int_type == IO_INTERRUPT_TYPE) {
Cornelia Huckda7c5af2008-07-14 09:58:59 +0200653 do_adapter_IO(tpi_info->isc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 continue;
655 }
656 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
Heiko Carstensa8061702008-04-17 07:46:26 +0200657 if (!sch) {
658 /* Clear pending interrupt condition. */
659 tsch(tpi_info->schid, irb);
660 continue;
661 }
662 spin_lock(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /* Store interrupt response block to lowcore. */
Heiko Carstensa8061702008-04-17 07:46:26 +0200664 if (tsch(tpi_info->schid, irb) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /* Keep subchannel information word up to date. */
666 memcpy (&sch->schib.scsw, &irb->scsw,
667 sizeof (irb->scsw));
668 /* Call interrupt handler if there is one. */
669 if (sch->driver && sch->driver->irq)
Cornelia Huck602b20f2008-01-26 14:10:39 +0100670 sch->driver->irq(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
Heiko Carstensa8061702008-04-17 07:46:26 +0200672 spin_unlock(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 /*
674 * Are more interrupts pending?
675 * If so, the tpi instruction will update the lowcore
676 * to hold the info for the next interrupt.
677 * We don't do this for VM because a tpi drops the cpu
678 * out of the sie which costs more cycles than it saves.
679 */
680 } while (!MACHINE_IS_VM && tpi (NULL) != 0);
Heiko Carstens9d0a57c2006-10-11 15:31:26 +0200681 irq_exit();
Heiko Carstens5a489b982006-10-06 16:38:35 +0200682 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
685#ifdef CONFIG_CCW_CONSOLE
686static struct subchannel console_subchannel;
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +0200687static char console_sch_name[10] = "0.x.xxxx";
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100688static struct io_subchannel_private console_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689static int console_subchannel_in_use;
690
Heiko Carstens191fd442008-12-25 13:39:01 +0100691/*
692 * Use tpi to get a pending interrupt, call the interrupt handler and
693 * return a pointer to the subchannel structure.
694 */
695static int cio_tpi(void)
696{
697 struct tpi_info *tpi_info;
698 struct subchannel *sch;
699 struct irb *irb;
700 int irq_context;
701
702 tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
703 if (tpi(NULL) != 1)
704 return 0;
705 irb = (struct irb *) __LC_IRB;
706 /* Store interrupt response block to lowcore. */
707 if (tsch(tpi_info->schid, irb) != 0)
708 /* Not status pending or not operational. */
709 return 1;
710 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
711 if (!sch)
712 return 1;
713 irq_context = in_interrupt();
714 if (!irq_context)
715 local_bh_disable();
716 irq_enter();
717 spin_lock(sch->lock);
718 memcpy(&sch->schib.scsw, &irb->scsw, sizeof(union scsw));
719 if (sch->driver && sch->driver->irq)
720 sch->driver->irq(sch);
721 spin_unlock(sch->lock);
722 irq_exit();
723 if (!irq_context)
724 _local_bh_enable();
725 return 1;
726}
727
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100728void *cio_get_console_priv(void)
729{
730 return &console_priv;
731}
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733/*
734 * busy wait for the next interrupt on the console
735 */
Heiko Carstensa8061702008-04-17 07:46:26 +0200736void wait_cons_dev(void)
737 __releases(console_subchannel.lock)
738 __acquires(console_subchannel.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
740 unsigned long cr6 __attribute__ ((aligned (8)));
741 unsigned long save_cr6 __attribute__ ((aligned (8)));
742
743 /*
744 * before entering the spinlock we may already have
745 * processed the interrupt on a different CPU...
746 */
747 if (!console_subchannel_in_use)
748 return;
749
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200750 /* disable all but the console isc */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 __ctl_store (save_cr6, 6, 6);
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200752 cr6 = 1UL << (31 - CONSOLE_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 __ctl_load (cr6, 6, 6);
754
755 do {
Cornelia Huck2ec22982006-12-08 15:54:26 +0100756 spin_unlock(console_subchannel.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (!cio_tpi())
758 cpu_relax();
Cornelia Huck2ec22982006-12-08 15:54:26 +0100759 spin_lock(console_subchannel.lock);
Peter Oberparleiter23d805b62008-07-14 09:58:50 +0200760 } while (console_subchannel.schib.scsw.cmd.actl != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /*
762 * restore previous isc value
763 */
764 __ctl_load (save_cr6, 6, 6);
765}
766
767static int
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800768cio_test_for_console(struct subchannel_id schid, void *data)
769{
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800770 if (stsch_err(schid, &console_subchannel.schib) != 0)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800771 return -ENXIO;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100772 if ((console_subchannel.schib.pmcw.st == SUBCHANNEL_TYPE_IO) &&
773 console_subchannel.schib.pmcw.dnv &&
774 (console_subchannel.schib.pmcw.dev == console_devno)) {
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800775 console_irq = schid.sch_no;
776 return 1; /* found */
777 }
778 return 0;
779}
780
781
782static int
783cio_get_console_sch_no(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800785 struct subchannel_id schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800787 init_subchannel_id(&schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (console_irq != -1) {
789 /* VM provided us with the irq number of the console. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800790 schid.sch_no = console_irq;
791 if (stsch(schid, &console_subchannel.schib) != 0 ||
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100792 (console_subchannel.schib.pmcw.st != SUBCHANNEL_TYPE_IO) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 !console_subchannel.schib.pmcw.dnv)
794 return -1;
795 console_devno = console_subchannel.schib.pmcw.dev;
796 } else if (console_devno != -1) {
797 /* At least the console device number is known. */
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800798 for_each_subchannel(cio_test_for_console, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (console_irq == -1)
800 return -1;
801 } else {
802 /* unlike in 2.4, we cannot autoprobe here, since
803 * the channel subsystem is not fully initialized.
804 * With some luck, the HWC console can take over */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return -1;
806 }
807 return console_irq;
808}
809
810struct subchannel *
811cio_probe_console(void)
812{
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800813 int sch_no, ret;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800814 struct subchannel_id schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 if (xchg(&console_subchannel_in_use, 1) != 0)
817 return ERR_PTR(-EBUSY);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800818 sch_no = cio_get_console_sch_no();
819 if (sch_no == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 console_subchannel_in_use = 0;
Michael Ernste6d5a422008-12-25 13:39:36 +0100821 pr_warning("No CCW console was found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return ERR_PTR(-ENODEV);
823 }
824 memset(&console_subchannel, 0, sizeof(struct subchannel));
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800825 init_subchannel_id(&schid);
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800826 schid.sch_no = sch_no;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800827 ret = cio_validate_subchannel(&console_subchannel, schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (ret) {
829 console_subchannel_in_use = 0;
830 return ERR_PTR(-ENODEV);
831 }
832
833 /*
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200834 * enable console I/O-interrupt subclass
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 */
Cornelia Huck6ef556c2008-07-14 09:59:01 +0200836 isc_register(CONSOLE_ISC);
Sebastian Ott13952ec2008-12-25 13:39:13 +0100837 console_subchannel.config.isc = CONSOLE_ISC;
838 console_subchannel.config.intparm = (u32)(addr_t)&console_subchannel;
839 ret = cio_commit_config(&console_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (ret) {
Cornelia Huck6ef556c2008-07-14 09:59:01 +0200841 isc_unregister(CONSOLE_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 console_subchannel_in_use = 0;
843 return ERR_PTR(ret);
844 }
845 return &console_subchannel;
846}
847
848void
849cio_release_console(void)
850{
Sebastian Ott13952ec2008-12-25 13:39:13 +0100851 console_subchannel.config.intparm = 0;
852 cio_commit_config(&console_subchannel);
Cornelia Huck6ef556c2008-07-14 09:59:01 +0200853 isc_unregister(CONSOLE_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 console_subchannel_in_use = 0;
855}
856
857/* Bah... hack to catch console special sausages. */
858int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800859cio_is_console(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 if (!console_subchannel_in_use)
862 return 0;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800863 return schid_equal(&schid, &console_subchannel.schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
866struct subchannel *
867cio_get_console_subchannel(void)
868{
869 if (!console_subchannel_in_use)
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200870 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return &console_subchannel;
872}
873
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +0200874const char *cio_get_console_sch_name(struct subchannel_id schid)
875{
876 snprintf(console_sch_name, 10, "0.%x.%04x", schid.ssid, schid.sch_no);
877 return (const char *)console_sch_name;
878}
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880#endif
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100881static int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800882__disable_subchannel_easy(struct subchannel_id schid, struct schib *schib)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
884 int retry, cc;
885
886 cc = 0;
887 for (retry=0;retry<3;retry++) {
888 schib->pmcw.ena = 0;
889 cc = msch(schid, schib);
890 if (cc)
891 return (cc==3?-ENODEV:-EBUSY);
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100892 if (stsch(schid, schib) || !css_sch_is_valid(schib))
893 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (!schib->pmcw.ena)
895 return 0;
896 }
897 return -EBUSY; /* uhm... */
898}
899
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100900static int
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200901__clear_io_subchannel_easy(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 int retry;
904
905 if (csch(schid))
906 return -ENODEV;
907 for (retry=0;retry<20;retry++) {
908 struct tpi_info ti;
909
910 if (tpi(&ti)) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800911 tsch(ti.schid, (struct irb *)__LC_IRB);
912 if (schid_equal(&ti.schid, &schid))
Cornelia Huck4c24da72005-09-03 15:58:01 -0700913 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200915 udelay_simple(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917 return -EBUSY;
918}
919
Cornelia Huck9d92a7e2008-07-14 09:59:05 +0200920static void __clear_chsc_subchannel_easy(void)
921{
922 /* It seems we can only wait for a bit here :/ */
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200923 udelay_simple(100);
Cornelia Huck9d92a7e2008-07-14 09:59:05 +0200924}
925
Michael Holzheua45e1412006-12-15 17:18:22 +0100926static int pgm_check_occured;
927
928static void cio_reset_pgm_check_handler(void)
929{
930 pgm_check_occured = 1;
931}
932
Peter Oberparleiterf9c9fe32008-10-10 21:33:15 +0200933static int stsch_reset(struct subchannel_id schid, struct schib *addr)
Michael Holzheua45e1412006-12-15 17:18:22 +0100934{
935 int rc;
936
937 pgm_check_occured = 0;
Heiko Carstensab14de62007-02-05 21:18:37 +0100938 s390_base_pgm_handler_fn = cio_reset_pgm_check_handler;
Heiko Carstens6faf4442007-01-09 10:18:41 +0100939 rc = stsch(schid, addr);
Heiko Carstensab14de62007-02-05 21:18:37 +0100940 s390_base_pgm_handler_fn = NULL;
Heiko Carstens6faf4442007-01-09 10:18:41 +0100941
Heiko Carstensab14de62007-02-05 21:18:37 +0100942 /* The program check handler could have changed pgm_check_occured. */
Heiko Carstens6faf4442007-01-09 10:18:41 +0100943 barrier();
944
Michael Holzheua45e1412006-12-15 17:18:22 +0100945 if (pgm_check_occured)
946 return -EIO;
947 else
948 return rc;
949}
950
Heiko Carstens15e9b582006-12-04 15:40:26 +0100951static int __shutdown_subchannel_easy(struct subchannel_id schid, void *data)
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800952{
953 struct schib schib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Michael Holzheua45e1412006-12-15 17:18:22 +0100955 if (stsch_reset(schid, &schib))
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800956 return -ENXIO;
957 if (!schib.pmcw.ena)
958 return 0;
959 switch(__disable_subchannel_easy(schid, &schib)) {
960 case 0:
961 case -ENODEV:
962 break;
963 default: /* -EBUSY */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200964 switch (schib.pmcw.st) {
965 case SUBCHANNEL_TYPE_IO:
966 if (__clear_io_subchannel_easy(schid))
967 goto out; /* give up... */
968 break;
Cornelia Huck9d92a7e2008-07-14 09:59:05 +0200969 case SUBCHANNEL_TYPE_CHSC:
970 __clear_chsc_subchannel_easy();
971 break;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200972 default:
973 /* No default clear strategy */
974 break;
975 }
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800976 stsch(schid, &schib);
977 __disable_subchannel_easy(schid, &schib);
978 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200979out:
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800980 return 0;
981}
982
Heiko Carstens15e9b582006-12-04 15:40:26 +0100983static atomic_t chpid_reset_count;
984
985static void s390_reset_chpids_mcck_handler(void)
986{
987 struct crw crw;
988 struct mci *mci;
989
990 /* Check for pending channel report word. */
991 mci = (struct mci *)&S390_lowcore.mcck_interruption_code;
992 if (!mci->cp)
993 return;
994 /* Process channel report words. */
995 while (stcrw(&crw) == 0) {
996 /* Check for responses to RCHP. */
997 if (crw.slct && crw.rsc == CRW_RSC_CPATH)
998 atomic_dec(&chpid_reset_count);
999 }
1000}
1001
1002#define RCHP_TIMEOUT (30 * USEC_PER_SEC)
1003static void css_reset(void)
1004{
1005 int i, ret;
1006 unsigned long long timeout;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +02001007 struct chp_id chpid;
Heiko Carstens15e9b582006-12-04 15:40:26 +01001008
1009 /* Reset subchannels. */
1010 for_each_subchannel(__shutdown_subchannel_easy, NULL);
1011 /* Reset channel paths. */
Heiko Carstensab14de62007-02-05 21:18:37 +01001012 s390_base_mcck_handler_fn = s390_reset_chpids_mcck_handler;
Heiko Carstens15e9b582006-12-04 15:40:26 +01001013 /* Enable channel report machine checks. */
1014 __ctl_set_bit(14, 28);
1015 /* Temporarily reenable machine checks. */
1016 local_mcck_enable();
Peter Oberparleiterf86635f2007-04-27 16:01:26 +02001017 chp_id_init(&chpid);
Heiko Carstens15e9b582006-12-04 15:40:26 +01001018 for (i = 0; i <= __MAX_CHPID; i++) {
Peter Oberparleiterf86635f2007-04-27 16:01:26 +02001019 chpid.id = i;
1020 ret = rchp(chpid);
Heiko Carstens15e9b582006-12-04 15:40:26 +01001021 if ((ret == 0) || (ret == 2))
1022 /*
1023 * rchp either succeeded, or another rchp is already
1024 * in progress. In either case, we'll get a crw.
1025 */
1026 atomic_inc(&chpid_reset_count);
1027 }
1028 /* Wait for machine check for all channel paths. */
1029 timeout = get_clock() + (RCHP_TIMEOUT << 12);
1030 while (atomic_read(&chpid_reset_count) != 0) {
1031 if (get_clock() > timeout)
1032 break;
1033 cpu_relax();
1034 }
1035 /* Disable machine checks again. */
1036 local_mcck_disable();
1037 /* Disable channel report machine checks. */
1038 __ctl_clear_bit(14, 28);
Heiko Carstensab14de62007-02-05 21:18:37 +01001039 s390_base_mcck_handler_fn = NULL;
Heiko Carstens15e9b582006-12-04 15:40:26 +01001040}
1041
1042static struct reset_call css_reset_call = {
1043 .fn = css_reset,
1044};
1045
1046static int __init init_css_reset_call(void)
1047{
1048 atomic_set(&chpid_reset_count, 0);
1049 register_reset_call(&css_reset_call);
1050 return 0;
1051}
1052
1053arch_initcall(init_css_reset_call);
1054
1055struct sch_match_id {
1056 struct subchannel_id schid;
1057 struct ccw_dev_id devid;
1058 int rc;
1059};
1060
1061static int __reipl_subchannel_match(struct subchannel_id schid, void *data)
1062{
1063 struct schib schib;
1064 struct sch_match_id *match_id = data;
1065
Michael Holzheua45e1412006-12-15 17:18:22 +01001066 if (stsch_reset(schid, &schib))
Heiko Carstens15e9b582006-12-04 15:40:26 +01001067 return -ENXIO;
Cornelia Huckb279a4f2008-01-26 14:10:45 +01001068 if ((schib.pmcw.st == SUBCHANNEL_TYPE_IO) && schib.pmcw.dnv &&
Heiko Carstens15e9b582006-12-04 15:40:26 +01001069 (schib.pmcw.dev == match_id->devid.devno) &&
1070 (schid.ssid == match_id->devid.ssid)) {
1071 match_id->schid = schid;
1072 match_id->rc = 0;
1073 return 1;
1074 }
1075 return 0;
1076}
1077
1078static int reipl_find_schid(struct ccw_dev_id *devid,
1079 struct subchannel_id *schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001081 struct sch_match_id match_id;
1082
1083 match_id.devid = *devid;
1084 match_id.rc = -ENODEV;
Heiko Carstens15e9b582006-12-04 15:40:26 +01001085 for_each_subchannel(__reipl_subchannel_match, &match_id);
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001086 if (match_id.rc == 0)
1087 *schid = match_id.schid;
1088 return match_id.rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001091extern void do_reipl_asm(__u32 schid);
1092
1093/* Make sure all subchannels are quiet before we re-ipl an lpar. */
1094void reipl_ccw_dev(struct ccw_dev_id *devid)
1095{
1096 struct subchannel_id schid;
1097
Heiko Carstens15e9b582006-12-04 15:40:26 +01001098 s390_reset_system();
1099 if (reipl_find_schid(devid, &schid) != 0)
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001100 panic("IPL Device not found\n");
Michael Holzheuff6b8ea2006-09-20 15:58:49 +02001101 do_reipl_asm(*((__u32*)&schid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
Heiko Carstense87bfe52006-09-20 15:59:15 +02001103
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001104int __init cio_get_iplinfo(struct cio_iplinfo *iplinfo)
Heiko Carstense87bfe52006-09-20 15:59:15 +02001105{
1106 struct subchannel_id schid;
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001107 struct schib schib;
Heiko Carstense87bfe52006-09-20 15:59:15 +02001108
1109 schid = *(struct subchannel_id *)__LC_SUBCHANNEL_ID;
1110 if (!schid.one)
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001111 return -ENODEV;
1112 if (stsch(schid, &schib))
1113 return -ENODEV;
Cornelia Huckb279a4f2008-01-26 14:10:45 +01001114 if (schib.pmcw.st != SUBCHANNEL_TYPE_IO)
1115 return -ENODEV;
Heiko Carstens6fc321f2007-04-27 16:01:25 +02001116 if (!schib.pmcw.dnv)
1117 return -ENODEV;
1118 iplinfo->devno = schib.pmcw.dev;
1119 iplinfo->is_qdio = schib.pmcw.qf;
1120 return 0;
Heiko Carstense87bfe52006-09-20 15:59:15 +02001121}
Peter Oberparleiter83262d62008-07-14 09:58:51 +02001122
1123/**
1124 * cio_tm_start_key - perform start function
1125 * @sch: subchannel on which to perform the start function
1126 * @tcw: transport-command word to be started
1127 * @lpm: mask of paths to use
1128 * @key: storage key to use for storage access
1129 *
1130 * Start the tcw on the given subchannel. Return zero on success, non-zero
1131 * otherwise.
1132 */
1133int cio_tm_start_key(struct subchannel *sch, struct tcw *tcw, u8 lpm, u8 key)
1134{
1135 int cc;
1136 union orb *orb = &to_io_private(sch)->orb;
1137
1138 memset(orb, 0, sizeof(union orb));
1139 orb->tm.intparm = (u32) (addr_t) sch;
1140 orb->tm.key = key >> 4;
1141 orb->tm.b = 1;
1142 orb->tm.lpm = lpm ? lpm : sch->lpm;
1143 orb->tm.tcw = (u32) (addr_t) tcw;
1144 cc = ssch(sch->schid, orb);
1145 switch (cc) {
1146 case 0:
1147 return 0;
1148 case 1:
1149 case 2:
1150 return -EBUSY;
1151 default:
1152 return cio_start_handle_notoper(sch, lpm);
1153 }
1154}
1155
1156/**
1157 * cio_tm_intrg - perform interrogate function
1158 * @sch - subchannel on which to perform the interrogate function
1159 *
1160 * If the specified subchannel is running in transport-mode, perform the
1161 * interrogate function. Return zero on success, non-zero otherwie.
1162 */
1163int cio_tm_intrg(struct subchannel *sch)
1164{
1165 int cc;
1166
1167 if (!to_io_private(sch)->orb.tm.b)
1168 return -EINVAL;
1169 cc = xsch(sch->schid);
1170 switch (cc) {
1171 case 0:
1172 case 2:
1173 return 0;
1174 case 1:
1175 return -EBUSY;
1176 default:
1177 return -ENODEV;
1178 }
1179}