blob: 5f5ee1eef07a33c88341197dd079e0471b544fd4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/cio/device_fsm.c
3 * finite state machine for device handling
4 *
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6 * IBM Corporation
Cornelia Huck4ce3b302006-01-14 13:21:04 -08007 * Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 */
10
11#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/init.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080013#include <linux/jiffies.h>
14#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include <asm/ccwdev.h>
Cornelia Huck4c24da72005-09-03 15:58:01 -070017#include <asm/cio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include "cio.h"
20#include "cio_debug.h"
21#include "css.h"
22#include "device.h"
23#include "chsc.h"
24#include "ioasm.h"
Peter Oberparleiterf86635f2007-04-27 16:01:26 +020025#include "chpid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27int
28device_is_online(struct subchannel *sch)
29{
30 struct ccw_device *cdev;
31
32 if (!sch->dev.driver_data)
33 return 0;
34 cdev = sch->dev.driver_data;
35 return (cdev->private->state == DEV_STATE_ONLINE);
36}
37
38int
39device_is_disconnected(struct subchannel *sch)
40{
41 struct ccw_device *cdev;
42
43 if (!sch->dev.driver_data)
44 return 0;
45 cdev = sch->dev.driver_data;
46 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
47 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
48}
49
50void
51device_set_disconnected(struct subchannel *sch)
52{
53 struct ccw_device *cdev;
54
55 if (!sch->dev.driver_data)
56 return;
57 cdev = sch->dev.driver_data;
58 ccw_device_set_timeout(cdev, 0);
59 cdev->private->flags.fake_irb = 0;
60 cdev->private->state = DEV_STATE_DISCONNECTED;
61}
62
Cornelia Huckd23861f2006-12-04 15:41:04 +010063void device_set_intretry(struct subchannel *sch)
64{
65 struct ccw_device *cdev;
66
67 cdev = sch->dev.driver_data;
68 if (!cdev)
69 return;
70 cdev->private->flags.intretry = 1;
71}
72
Cornelia Huck24cb5b42006-12-04 15:41:01 +010073int device_trigger_verify(struct subchannel *sch)
74{
75 struct ccw_device *cdev;
76
77 cdev = sch->dev.driver_data;
78 if (!cdev || !cdev->online)
79 return -EINVAL;
80 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
81 return 0;
82}
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/*
85 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
86 */
87static void
88ccw_device_timeout(unsigned long data)
89{
90 struct ccw_device *cdev;
91
92 cdev = (struct ccw_device *) data;
93 spin_lock_irq(cdev->ccwlock);
94 dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
95 spin_unlock_irq(cdev->ccwlock);
96}
97
98/*
99 * Set timeout
100 */
101void
102ccw_device_set_timeout(struct ccw_device *cdev, int expires)
103{
104 if (expires == 0) {
105 del_timer(&cdev->private->timer);
106 return;
107 }
108 if (timer_pending(&cdev->private->timer)) {
109 if (mod_timer(&cdev->private->timer, jiffies + expires))
110 return;
111 }
112 cdev->private->timer.function = ccw_device_timeout;
113 cdev->private->timer.data = (unsigned long) cdev;
114 cdev->private->timer.expires = jiffies + expires;
115 add_timer(&cdev->private->timer);
116}
117
118/* Kill any pending timers after machine check. */
119void
120device_kill_pending_timer(struct subchannel *sch)
121{
122 struct ccw_device *cdev;
123
124 if (!sch->dev.driver_data)
125 return;
126 cdev = sch->dev.driver_data;
127 ccw_device_set_timeout(cdev, 0);
128}
129
130/*
131 * Cancel running i/o. This is called repeatedly since halt/clear are
132 * asynchronous operations. We do one try with cio_cancel, two tries
133 * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
134 * Returns 0 if device now idle, -ENODEV for device not operational and
135 * -EBUSY if an interrupt is expected (either from halt/clear or from a
136 * status pending).
137 */
138int
139ccw_device_cancel_halt_clear(struct ccw_device *cdev)
140{
141 struct subchannel *sch;
142 int ret;
143
144 sch = to_subchannel(cdev->dev.parent);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800145 ret = stsch(sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (ret || !sch->schib.pmcw.dnv)
147 return -ENODEV;
Cornelia Huck2470b642007-03-05 23:36:02 +0100148 if (!sch->schib.pmcw.ena)
149 /* Not operational -> done. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return 0;
151 /* Stage 1: cancel io. */
152 if (!(sch->schib.scsw.actl & SCSW_ACTL_HALT_PEND) &&
153 !(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
154 ret = cio_cancel(sch);
155 if (ret != -EINVAL)
156 return ret;
157 /* cancel io unsuccessful. From now on it is asynchronous. */
158 cdev->private->iretry = 3; /* 3 halt retries. */
159 }
160 if (!(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
161 /* Stage 2: halt io. */
162 if (cdev->private->iretry) {
163 cdev->private->iretry--;
164 ret = cio_halt(sch);
Peter Oberparleiterba4ba8a2006-07-27 14:00:23 +0200165 if (ret != -EBUSY)
166 return (ret == 0) ? -EBUSY : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168 /* halt io unsuccessful. */
169 cdev->private->iretry = 255; /* 255 clear retries. */
170 }
171 /* Stage 3: clear io. */
172 if (cdev->private->iretry) {
173 cdev->private->iretry--;
174 ret = cio_clear (sch);
175 return (ret == 0) ? -EBUSY : ret;
176 }
177 panic("Can't stop i/o on subchannel.\n");
178}
179
180static int
181ccw_device_handle_oper(struct ccw_device *cdev)
182{
183 struct subchannel *sch;
184
185 sch = to_subchannel(cdev->dev.parent);
186 cdev->private->flags.recog_done = 1;
187 /*
188 * Check if cu type and device type still match. If
189 * not, it is certainly another device and we have to
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100190 * de- and re-register.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 */
192 if (cdev->id.cu_type != cdev->private->senseid.cu_type ||
193 cdev->id.cu_model != cdev->private->senseid.cu_model ||
194 cdev->id.dev_type != cdev->private->senseid.dev_type ||
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100195 cdev->id.dev_model != cdev->private->senseid.dev_model) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100197 ccw_device_do_unreg_rereg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 queue_work(ccw_device_work, &cdev->private->kick_work);
199 return 0;
200 }
201 cdev->private->flags.donotify = 1;
202 return 1;
203}
204
205/*
206 * The machine won't give us any notification by machine check if a chpid has
207 * been varied online on the SE so we have to find out by magic (i. e. driving
208 * the channel subsystem to device selection and updating our path masks).
209 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100210static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211__recover_lost_chpids(struct subchannel *sch, int old_lpm)
212{
213 int mask, i;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200214 struct chp_id chpid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200216 chp_id_init(&chpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 for (i = 0; i<8; i++) {
218 mask = 0x80 >> i;
219 if (!(sch->lpm & mask))
220 continue;
221 if (old_lpm & mask)
222 continue;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200223 chpid.id = sch->schib.pmcw.chpid[i];
224 chpid_is_actually_online(chpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226}
227
228/*
229 * Stop device recognition.
230 */
231static void
232ccw_device_recog_done(struct ccw_device *cdev, int state)
233{
234 struct subchannel *sch;
235 int notify, old_lpm, same_dev;
236
237 sch = to_subchannel(cdev->dev.parent);
238
239 ccw_device_set_timeout(cdev, 0);
240 cio_disable_subchannel(sch);
241 /*
242 * Now that we tried recognition, we have performed device selection
243 * through ssch() and the path information is up to date.
244 */
245 old_lpm = sch->lpm;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800246 stsch(sch->schid, &sch->schib);
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200247 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck4ffa9232005-07-29 14:03:37 -0700248 /* Check since device may again have become not operational. */
249 if (!sch->schib.pmcw.dnv)
250 state = DEV_STATE_NOT_OPER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
252 /* Force reprobe on all chpids. */
253 old_lpm = 0;
254 if (sch->lpm != old_lpm)
255 __recover_lost_chpids(sch, old_lpm);
256 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
257 if (state == DEV_STATE_NOT_OPER) {
258 cdev->private->flags.recog_done = 1;
259 cdev->private->state = DEV_STATE_DISCONNECTED;
260 return;
261 }
262 /* Boxed devices don't need extra treatment. */
263 }
264 notify = 0;
265 same_dev = 0; /* Keep the compiler quiet... */
266 switch (state) {
267 case DEV_STATE_NOT_OPER:
268 CIO_DEBUG(KERN_WARNING, 2,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800269 "SenseID : unknown device %04x on subchannel "
Cornelia Huck78964262006-10-11 15:31:38 +0200270 "0.%x.%04x\n", cdev->private->dev_id.devno,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800271 sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 break;
273 case DEV_STATE_OFFLINE:
274 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
275 same_dev = ccw_device_handle_oper(cdev);
276 notify = 1;
277 }
278 /* fill out sense information */
Heiko Carstens81388d22006-09-20 15:59:17 +0200279 memset(&cdev->id, 0, sizeof(cdev->id));
Heiko Carstens292888c2006-08-30 14:33:35 +0200280 cdev->id.cu_type = cdev->private->senseid.cu_type;
281 cdev->id.cu_model = cdev->private->senseid.cu_model;
282 cdev->id.dev_type = cdev->private->senseid.dev_type;
283 cdev->id.dev_model = cdev->private->senseid.dev_model;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (notify) {
285 cdev->private->state = DEV_STATE_OFFLINE;
286 if (same_dev) {
287 /* Get device online again. */
288 ccw_device_online(cdev);
289 wake_up(&cdev->private->wait_q);
290 }
291 return;
292 }
293 /* Issue device info message. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800294 CIO_DEBUG(KERN_INFO, 2, "SenseID : device 0.%x.%04x reports: "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800296 "%04X/%02X\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200297 cdev->private->dev_id.ssid,
298 cdev->private->dev_id.devno,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 cdev->id.cu_type, cdev->id.cu_model,
300 cdev->id.dev_type, cdev->id.dev_model);
301 break;
302 case DEV_STATE_BOXED:
303 CIO_DEBUG(KERN_WARNING, 2,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800304 "SenseID : boxed device %04x on subchannel "
Cornelia Huck78964262006-10-11 15:31:38 +0200305 "0.%x.%04x\n", cdev->private->dev_id.devno,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800306 sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 break;
308 }
309 cdev->private->state = state;
310 io_subchannel_recog_done(cdev);
311 if (state != DEV_STATE_NOT_OPER)
312 wake_up(&cdev->private->wait_q);
313}
314
315/*
316 * Function called from device_id.c after sense id has completed.
317 */
318void
319ccw_device_sense_id_done(struct ccw_device *cdev, int err)
320{
321 switch (err) {
322 case 0:
323 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
324 break;
325 case -ETIME: /* Sense id stopped by timeout. */
326 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
327 break;
328 default:
329 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
330 break;
331 }
332}
333
334static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100335ccw_device_oper_notify(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100337 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct ccw_device *cdev;
339 struct subchannel *sch;
340 int ret;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100341 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100343 priv = container_of(work, struct ccw_device_private, kick_work);
344 cdev = priv->cdev;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100345 spin_lock_irqsave(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100347 if (sch->driver && sch->driver->notify) {
348 spin_unlock_irqrestore(cdev->ccwlock, flags);
349 ret = sch->driver->notify(&sch->dev, CIO_OPER);
350 spin_lock_irqsave(cdev->ccwlock, flags);
351 } else
352 ret = 0;
353 if (ret) {
354 /* Reenable channel measurements, if needed. */
355 spin_unlock_irqrestore(cdev->ccwlock, flags);
356 cmf_reenable(cdev);
357 spin_lock_irqsave(cdev->ccwlock, flags);
358 wake_up(&cdev->private->wait_q);
359 }
360 spin_unlock_irqrestore(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (!ret)
362 /* Driver doesn't want device back. */
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100363 ccw_device_do_unreg_rereg(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366/*
367 * Finished with online/offline processing.
368 */
369static void
370ccw_device_done(struct ccw_device *cdev, int state)
371{
372 struct subchannel *sch;
373
374 sch = to_subchannel(cdev->dev.parent);
375
Cornelia Huckf1ee3282006-10-04 20:02:02 +0200376 ccw_device_set_timeout(cdev, 0);
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (state != DEV_STATE_ONLINE)
379 cio_disable_subchannel(sch);
380
381 /* Reset device status. */
382 memset(&cdev->private->irb, 0, sizeof(struct irb));
383
384 cdev->private->state = state;
385
386
387 if (state == DEV_STATE_BOXED)
388 CIO_DEBUG(KERN_WARNING, 2,
389 "Boxed device %04x on subchannel %04x\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200390 cdev->private->dev_id.devno, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 if (cdev->private->flags.donotify) {
393 cdev->private->flags.donotify = 0;
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100394 PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
396 }
397 wake_up(&cdev->private->wait_q);
398
399 if (css_init_done && state != DEV_STATE_ONLINE)
400 put_device (&cdev->dev);
401}
402
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100403static int cmp_pgid(struct pgid *p1, struct pgid *p2)
Cornelia Huck7e560812006-07-12 16:40:19 +0200404{
405 char *c1;
406 char *c2;
407
408 c1 = (char *)p1;
409 c2 = (char *)p2;
410
411 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
412}
413
414static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
415{
416 int i;
417 int last;
418
419 last = 0;
420 for (i = 0; i < 8; i++) {
421 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
422 /* No PGID yet */
423 continue;
424 if (cdev->private->pgid[last].inf.ps.state1 ==
425 SNID_STATE1_RESET) {
426 /* First non-zero PGID */
427 last = i;
428 continue;
429 }
430 if (cmp_pgid(&cdev->private->pgid[i],
431 &cdev->private->pgid[last]) == 0)
432 /* Non-conflicting PGIDs */
433 continue;
434
435 /* PGID mismatch, can't pathgroup. */
436 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
437 "0.%x.%04x, can't pathgroup\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200438 cdev->private->dev_id.ssid,
439 cdev->private->dev_id.devno);
Cornelia Huck7e560812006-07-12 16:40:19 +0200440 cdev->private->options.pgroup = 0;
441 return;
442 }
443 if (cdev->private->pgid[last].inf.ps.state1 ==
444 SNID_STATE1_RESET)
445 /* No previous pgid found */
446 memcpy(&cdev->private->pgid[0], &css[0]->global_pgid,
447 sizeof(struct pgid));
448 else
449 /* Use existing pgid */
450 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
451 sizeof(struct pgid));
452}
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454/*
455 * Function called from device_pgid.c after sense path ground has completed.
456 */
457void
458ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
459{
460 struct subchannel *sch;
461
462 sch = to_subchannel(cdev->dev.parent);
463 switch (err) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200464 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
465 cdev->private->options.pgroup = 0;
466 break;
467 case 0: /* success */
468 case -EACCES: /* partial success, some paths not operational */
469 /* Check if all pgids are equal or 0. */
470 __ccw_device_get_common_pgid(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 break;
472 case -ETIME: /* Sense path group id stopped by timeout. */
473 case -EUSERS: /* device is reserved for someone else. */
474 ccw_device_done(cdev, DEV_STATE_BOXED);
Cornelia Huck7e560812006-07-12 16:40:19 +0200475 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 default:
477 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Cornelia Huck7e560812006-07-12 16:40:19 +0200478 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
Cornelia Huck7e560812006-07-12 16:40:19 +0200480 /* Start Path Group verification. */
Cornelia Huck7e560812006-07-12 16:40:19 +0200481 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200482 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200483 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
486/*
487 * Start device recognition.
488 */
489int
490ccw_device_recognition(struct ccw_device *cdev)
491{
492 struct subchannel *sch;
493 int ret;
494
495 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
496 (cdev->private->state != DEV_STATE_BOXED))
497 return -EINVAL;
498 sch = to_subchannel(cdev->dev.parent);
499 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
500 if (ret != 0)
501 /* Couldn't enable the subchannel for i/o. Sick device. */
502 return ret;
503
504 /* After 60s the device recognition is considered to have failed. */
505 ccw_device_set_timeout(cdev, 60*HZ);
506
507 /*
508 * We used to start here with a sense pgid to find out whether a device
509 * is locked by someone else. Unfortunately, the sense pgid command
510 * code has other meanings on devices predating the path grouping
511 * algorithm, so we start with sense id and box the device after an
512 * timeout (or if sense pgid during path verification detects the device
513 * is locked, as may happen on newer devices).
514 */
515 cdev->private->flags.recog_done = 0;
516 cdev->private->state = DEV_STATE_SENSE_ID;
517 ccw_device_sense_id_start(cdev);
518 return 0;
519}
520
521/*
522 * Handle timeout in device recognition.
523 */
524static void
525ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
526{
527 int ret;
528
529 ret = ccw_device_cancel_halt_clear(cdev);
530 switch (ret) {
531 case 0:
532 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
533 break;
534 case -ENODEV:
535 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
536 break;
537 default:
538 ccw_device_set_timeout(cdev, 3*HZ);
539 }
540}
541
542
543static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100544ccw_device_nopath_notify(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100546 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 struct ccw_device *cdev;
548 struct subchannel *sch;
549 int ret;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100550 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100552 priv = container_of(work, struct ccw_device_private, kick_work);
553 cdev = priv->cdev;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100554 spin_lock_irqsave(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 sch = to_subchannel(cdev->dev.parent);
556 /* Extra sanity. */
557 if (sch->lpm)
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100558 goto out_unlock;
559 if (sch->driver && sch->driver->notify) {
560 spin_unlock_irqrestore(cdev->ccwlock, flags);
561 ret = sch->driver->notify(&sch->dev, CIO_NO_PATH);
562 spin_lock_irqsave(cdev->ccwlock, flags);
563 } else
564 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (!ret) {
566 if (get_device(&sch->dev)) {
567 /* Driver doesn't want to keep device. */
568 cio_disable_subchannel(sch);
569 if (get_device(&cdev->dev)) {
570 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100571 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 queue_work(ccw_device_work,
573 &cdev->private->kick_work);
574 } else
575 put_device(&sch->dev);
576 }
577 } else {
578 cio_disable_subchannel(sch);
579 ccw_device_set_timeout(cdev, 0);
580 cdev->private->flags.fake_irb = 0;
581 cdev->private->state = DEV_STATE_DISCONNECTED;
582 wake_up(&cdev->private->wait_q);
583 }
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100584out_unlock:
585 spin_unlock_irqrestore(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
588void
589ccw_device_verify_done(struct ccw_device *cdev, int err)
590{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200591 struct subchannel *sch;
592
593 sch = to_subchannel(cdev->dev.parent);
594 /* Update schib - pom may have changed. */
595 stsch(sch->schid, &sch->schib);
596 /* Update lpm with verified path mask. */
597 sch->lpm = sch->vpm;
598 /* Repeat path verification? */
599 if (cdev->private->flags.doverify) {
600 cdev->private->flags.doverify = 0;
601 ccw_device_verify_start(cdev);
602 return;
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 switch (err) {
605 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
606 cdev->private->options.pgroup = 0;
607 case 0:
608 ccw_device_done(cdev, DEV_STATE_ONLINE);
609 /* Deliver fake irb to device driver, if needed. */
610 if (cdev->private->flags.fake_irb) {
611 memset(&cdev->private->irb, 0, sizeof(struct irb));
Heiko Carstens292888c2006-08-30 14:33:35 +0200612 cdev->private->irb.scsw.cc = 1;
613 cdev->private->irb.scsw.fctl = SCSW_FCTL_START_FUNC;
614 cdev->private->irb.scsw.actl = SCSW_ACTL_START_PEND;
615 cdev->private->irb.scsw.stctl = SCSW_STCTL_STATUS_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 cdev->private->flags.fake_irb = 0;
617 if (cdev->handler)
618 cdev->handler(cdev, cdev->private->intparm,
619 &cdev->private->irb);
620 memset(&cdev->private->irb, 0, sizeof(struct irb));
621 }
622 break;
623 case -ETIME:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200624 /* Reset oper notify indication after verify error. */
625 cdev->private->flags.donotify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 ccw_device_done(cdev, DEV_STATE_BOXED);
627 break;
628 default:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200629 /* Reset oper notify indication after verify error. */
630 cdev->private->flags.donotify = 0;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100631 if (cdev->online) {
632 PREPARE_WORK(&cdev->private->kick_work,
633 ccw_device_nopath_notify);
634 queue_work(ccw_device_notify_work,
635 &cdev->private->kick_work);
636 } else
637 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 break;
639 }
640}
641
642/*
643 * Get device online.
644 */
645int
646ccw_device_online(struct ccw_device *cdev)
647{
648 struct subchannel *sch;
649 int ret;
650
651 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
652 (cdev->private->state != DEV_STATE_BOXED))
653 return -EINVAL;
654 sch = to_subchannel(cdev->dev.parent);
655 if (css_init_done && !get_device(&cdev->dev))
656 return -ENODEV;
657 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
658 if (ret != 0) {
659 /* Couldn't enable the subchannel for i/o. Sick device. */
660 if (ret == -ENODEV)
661 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
662 return ret;
663 }
664 /* Do we want to do path grouping? */
665 if (!cdev->private->options.pgroup) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200666 /* Start initial path verification. */
667 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200668 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200669 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return 0;
671 }
672 /* Do a SensePGID first. */
673 cdev->private->state = DEV_STATE_SENSE_PGID;
674 ccw_device_sense_pgid_start(cdev);
675 return 0;
676}
677
678void
679ccw_device_disband_done(struct ccw_device *cdev, int err)
680{
681 switch (err) {
682 case 0:
683 ccw_device_done(cdev, DEV_STATE_OFFLINE);
684 break;
685 case -ETIME:
686 ccw_device_done(cdev, DEV_STATE_BOXED);
687 break;
688 default:
689 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
690 break;
691 }
692}
693
694/*
695 * Shutdown device.
696 */
697int
698ccw_device_offline(struct ccw_device *cdev)
699{
700 struct subchannel *sch;
701
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100702 if (ccw_device_is_orphan(cdev)) {
703 ccw_device_done(cdev, DEV_STATE_OFFLINE);
704 return 0;
705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 sch = to_subchannel(cdev->dev.parent);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800707 if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return -ENODEV;
709 if (cdev->private->state != DEV_STATE_ONLINE) {
710 if (sch->schib.scsw.actl != 0)
711 return -EBUSY;
712 return -EINVAL;
713 }
714 if (sch->schib.scsw.actl != 0)
715 return -EBUSY;
716 /* Are we doing path grouping? */
717 if (!cdev->private->options.pgroup) {
718 /* No, set state offline immediately. */
719 ccw_device_done(cdev, DEV_STATE_OFFLINE);
720 return 0;
721 }
722 /* Start Set Path Group commands. */
723 cdev->private->state = DEV_STATE_DISBAND_PGID;
724 ccw_device_disband_start(cdev);
725 return 0;
726}
727
728/*
729 * Handle timeout in device online/offline process.
730 */
731static void
732ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
733{
734 int ret;
735
736 ret = ccw_device_cancel_halt_clear(cdev);
737 switch (ret) {
738 case 0:
739 ccw_device_done(cdev, DEV_STATE_BOXED);
740 break;
741 case -ENODEV:
742 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
743 break;
744 default:
745 ccw_device_set_timeout(cdev, 3*HZ);
746 }
747}
748
749/*
750 * Handle not oper event in device recognition.
751 */
752static void
753ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
754{
755 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
756}
757
758/*
759 * Handle not operational event while offline.
760 */
761static void
762ccw_device_offline_notoper(struct ccw_device *cdev, enum dev_event dev_event)
763{
764 struct subchannel *sch;
765
766 cdev->private->state = DEV_STATE_NOT_OPER;
767 sch = to_subchannel(cdev->dev.parent);
768 if (get_device(&cdev->dev)) {
769 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100770 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 queue_work(ccw_device_work, &cdev->private->kick_work);
772 }
773 wake_up(&cdev->private->wait_q);
774}
775
776/*
777 * Handle not operational event while online.
778 */
779static void
780ccw_device_online_notoper(struct ccw_device *cdev, enum dev_event dev_event)
781{
782 struct subchannel *sch;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100783 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100786 if (sch->driver->notify) {
787 spin_unlock_irq(cdev->ccwlock);
788 ret = sch->driver->notify(&sch->dev,
789 sch->lpm ? CIO_GONE : CIO_NO_PATH);
790 spin_lock_irq(cdev->ccwlock);
791 } else
792 ret = 0;
793 if (ret) {
794 ccw_device_set_timeout(cdev, 0);
795 cdev->private->flags.fake_irb = 0;
796 cdev->private->state = DEV_STATE_DISCONNECTED;
797 wake_up(&cdev->private->wait_q);
798 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800 cdev->private->state = DEV_STATE_NOT_OPER;
801 cio_disable_subchannel(sch);
802 if (sch->schib.scsw.actl != 0) {
803 // FIXME: not-oper indication to device driver ?
804 ccw_device_call_handler(cdev);
805 }
806 if (get_device(&cdev->dev)) {
807 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100808 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 queue_work(ccw_device_work, &cdev->private->kick_work);
810 }
811 wake_up(&cdev->private->wait_q);
812}
813
814/*
815 * Handle path verification event.
816 */
817static void
818ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
819{
820 struct subchannel *sch;
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (cdev->private->state == DEV_STATE_W4SENSE) {
823 cdev->private->flags.doverify = 1;
824 return;
825 }
826 sch = to_subchannel(cdev->dev.parent);
827 /*
828 * Since we might not just be coming from an interrupt from the
829 * subchannel we have to update the schib.
830 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800831 stsch(sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 if (sch->schib.scsw.actl != 0 ||
Peter Oberparleiter65200c22006-08-07 17:00:33 +0200834 (sch->schib.scsw.stctl & SCSW_STCTL_STATUS_PEND) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
836 /*
837 * No final status yet or final status not yet delivered
838 * to the device driver. Can't do path verfication now,
839 * delay until final status was delivered.
840 */
841 cdev->private->flags.doverify = 1;
842 return;
843 }
844 /* Device is idle, we can do the path verification. */
845 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200846 cdev->private->flags.doverify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 ccw_device_verify_start(cdev);
848}
849
850/*
851 * Got an interrupt for a normal io (state online).
852 */
853static void
854ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
855{
856 struct irb *irb;
857
858 irb = (struct irb *) __LC_IRB;
859 /* Check for unsolicited interrupt. */
860 if ((irb->scsw.stctl ==
861 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
862 && (!irb->scsw.cc)) {
863 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
864 !irb->esw.esw0.erw.cons) {
865 /* Unit check but no sense data. Need basic sense. */
866 if (ccw_device_do_sense(cdev, irb) != 0)
867 goto call_handler_unsol;
Cornelia Hucke0ec5742006-06-04 02:51:27 -0700868 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 cdev->private->state = DEV_STATE_W4SENSE;
870 cdev->private->intparm = 0;
871 return;
872 }
873call_handler_unsol:
874 if (cdev->handler)
875 cdev->handler (cdev, 0, irb);
Cornelia Huck18374d32007-02-05 21:17:09 +0100876 if (cdev->private->flags.doverify)
877 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return;
879 }
880 /* Accumulate status and find out if a basic sense is needed. */
881 ccw_device_accumulate_irb(cdev, irb);
882 if (cdev->private->flags.dosense) {
883 if (ccw_device_do_sense(cdev, irb) == 0) {
884 cdev->private->state = DEV_STATE_W4SENSE;
885 }
886 return;
887 }
888 /* Call the handler. */
889 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
890 /* Start delayed path verification. */
891 ccw_device_online_verify(cdev, 0);
892}
893
894/*
895 * Got an timeout in online state.
896 */
897static void
898ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
899{
900 int ret;
901
902 ccw_device_set_timeout(cdev, 0);
903 ret = ccw_device_cancel_halt_clear(cdev);
904 if (ret == -EBUSY) {
905 ccw_device_set_timeout(cdev, 3*HZ);
906 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
907 return;
908 }
909 if (ret == -ENODEV) {
910 struct subchannel *sch;
911
912 sch = to_subchannel(cdev->dev.parent);
913 if (!sch->lpm) {
914 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100915 ccw_device_nopath_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 queue_work(ccw_device_notify_work,
917 &cdev->private->kick_work);
918 } else
919 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
920 } else if (cdev->handler)
921 cdev->handler(cdev, cdev->private->intparm,
922 ERR_PTR(-ETIMEDOUT));
923}
924
925/*
926 * Got an interrupt for a basic sense.
927 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100928static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
930{
931 struct irb *irb;
932
933 irb = (struct irb *) __LC_IRB;
934 /* Check for unsolicited interrupt. */
935 if (irb->scsw.stctl ==
936 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
937 if (irb->scsw.cc == 1)
938 /* Basic sense hasn't started. Try again. */
939 ccw_device_do_sense(cdev, irb);
940 else {
Cornelia Huck08983782006-10-11 15:31:30 +0200941 printk(KERN_INFO "Huh? %s(%s): unsolicited "
942 "interrupt...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 __FUNCTION__, cdev->dev.bus_id);
944 if (cdev->handler)
945 cdev->handler (cdev, 0, irb);
946 }
947 return;
948 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800949 /*
950 * Check if a halt or clear has been issued in the meanwhile. If yes,
951 * only deliver the halt/clear interrupt to the device driver as if it
952 * had killed the original request.
953 */
954 if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
Cornelia Huckd23861f2006-12-04 15:41:04 +0100955 /* Retry Basic Sense if requested. */
956 if (cdev->private->flags.intretry) {
957 cdev->private->flags.intretry = 0;
958 ccw_device_do_sense(cdev, irb);
959 return;
960 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800961 cdev->private->flags.dosense = 0;
962 memset(&cdev->private->irb, 0, sizeof(struct irb));
963 ccw_device_accumulate_irb(cdev, irb);
964 goto call_handler;
965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 /* Add basic sense info to irb. */
967 ccw_device_accumulate_basic_sense(cdev, irb);
968 if (cdev->private->flags.dosense) {
969 /* Another basic sense is needed. */
970 ccw_device_do_sense(cdev, irb);
971 return;
972 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800973call_handler:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 cdev->private->state = DEV_STATE_ONLINE;
975 /* Call the handler. */
976 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
977 /* Start delayed path verification. */
978 ccw_device_online_verify(cdev, 0);
979}
980
981static void
982ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
983{
984 struct irb *irb;
985
986 irb = (struct irb *) __LC_IRB;
987 /* Accumulate status. We don't do basic sense. */
988 ccw_device_accumulate_irb(cdev, irb);
Cornelia Huckb4f7b1e2006-06-29 15:03:35 +0200989 /* Remember to clear irb to avoid residuals. */
990 memset(&cdev->private->irb, 0, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 /* Try to start delayed device verification. */
992 ccw_device_online_verify(cdev, 0);
993 /* Note: Don't call handler for cio initiated clear! */
994}
995
996static void
997ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
998{
999 struct subchannel *sch;
1000
1001 sch = to_subchannel(cdev->dev.parent);
1002 ccw_device_set_timeout(cdev, 0);
Cornelia Huck7c8427c2007-03-05 23:35:59 +01001003 /* Start delayed path verification. */
1004 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 /* OK, i/o is dead now. Call interrupt handler. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (cdev->handler)
1007 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +02001008 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
1011static void
1012ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1013{
1014 int ret;
1015
1016 ret = ccw_device_cancel_halt_clear(cdev);
1017 if (ret == -EBUSY) {
1018 ccw_device_set_timeout(cdev, 3*HZ);
1019 return;
1020 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +01001021 /* Start delayed path verification. */
1022 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (cdev->handler)
1024 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +02001025 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
Cornelia Hucke7769b42006-10-11 15:31:41 +02001028void device_kill_io(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 int ret;
Cornelia Hucke7769b42006-10-11 15:31:41 +02001031 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Cornelia Hucke7769b42006-10-11 15:31:41 +02001033 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 ret = ccw_device_cancel_halt_clear(cdev);
1035 if (ret == -EBUSY) {
1036 ccw_device_set_timeout(cdev, 3*HZ);
1037 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
1038 return;
1039 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +01001040 /* Start delayed path verification. */
1041 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (cdev->handler)
1043 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +02001044 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
1047static void
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001048ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001050 /* Start verification after current task finished. */
Cornelia Huck7e560812006-07-12 16:40:19 +02001051 cdev->private->flags.doverify = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
1054static void
1055ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
1056{
1057 struct irb *irb;
1058
1059 switch (dev_event) {
1060 case DEV_EVENT_INTERRUPT:
1061 irb = (struct irb *) __LC_IRB;
1062 /* Check for unsolicited interrupt. */
1063 if ((irb->scsw.stctl ==
1064 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
1065 (!irb->scsw.cc))
1066 /* FIXME: we should restart stlck here, but this
1067 * is extremely unlikely ... */
1068 goto out_wakeup;
1069
1070 ccw_device_accumulate_irb(cdev, irb);
1071 /* We don't care about basic sense etc. */
1072 break;
1073 default: /* timeout */
1074 break;
1075 }
1076out_wakeup:
1077 wake_up(&cdev->private->wait_q);
1078}
1079
1080static void
1081ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1082{
1083 struct subchannel *sch;
1084
1085 sch = to_subchannel(cdev->dev.parent);
1086 if (cio_enable_subchannel(sch, sch->schib.pmcw.isc) != 0)
1087 /* Couldn't enable the subchannel for i/o. Sick device. */
1088 return;
1089
1090 /* After 60s the device recognition is considered to have failed. */
1091 ccw_device_set_timeout(cdev, 60*HZ);
1092
1093 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1094 ccw_device_sense_id_start(cdev);
1095}
1096
1097void
1098device_trigger_reprobe(struct subchannel *sch)
1099{
1100 struct ccw_device *cdev;
1101
1102 if (!sch->dev.driver_data)
1103 return;
1104 cdev = sch->dev.driver_data;
1105 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1106 return;
1107
1108 /* Update some values. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001109 if (stsch(sch->schid, &sch->schib))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return;
Cornelia Huck7674da72006-12-08 15:54:21 +01001111 if (!sch->schib.pmcw.dnv)
1112 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 /*
1114 * The pim, pam, pom values may not be accurate, but they are the best
1115 * we have before performing device selection :/
1116 */
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001117 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 /* Re-set some bits in the pmcw that were lost. */
1119 sch->schib.pmcw.isc = 3;
1120 sch->schib.pmcw.csense = 1;
1121 sch->schib.pmcw.ena = 0;
1122 if ((sch->lpm & (sch->lpm - 1)) != 0)
1123 sch->schib.pmcw.mp = 1;
1124 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
1125 /* We should also udate ssd info, but this has to wait. */
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001126 /* Check if this is another device which appeared on the same sch. */
1127 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1128 PREPARE_WORK(&cdev->private->kick_work,
1129 ccw_device_move_to_orphanage);
1130 queue_work(ccw_device_work, &cdev->private->kick_work);
1131 } else
1132 ccw_device_start_id(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
1134
1135static void
1136ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1137{
1138 struct subchannel *sch;
1139
1140 sch = to_subchannel(cdev->dev.parent);
1141 /*
1142 * An interrupt in state offline means a previous disable was not
1143 * successful. Try again.
1144 */
1145 cio_disable_subchannel(sch);
1146}
1147
1148static void
1149ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1150{
1151 retry_set_schib(cdev);
1152 cdev->private->state = DEV_STATE_ONLINE;
1153 dev_fsm_event(cdev, dev_event);
1154}
1155
Cornelia Huck94bb0632006-06-29 15:08:41 +02001156static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1157 enum dev_event dev_event)
1158{
1159 cmf_retry_copy_block(cdev);
1160 cdev->private->state = DEV_STATE_ONLINE;
1161 dev_fsm_event(cdev, dev_event);
1162}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164static void
1165ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1166{
1167 ccw_device_set_timeout(cdev, 0);
1168 if (dev_event == DEV_EVENT_NOTOPER)
1169 cdev->private->state = DEV_STATE_NOT_OPER;
1170 else
1171 cdev->private->state = DEV_STATE_OFFLINE;
1172 wake_up(&cdev->private->wait_q);
1173}
1174
1175static void
1176ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1177{
1178 int ret;
1179
1180 ret = ccw_device_cancel_halt_clear(cdev);
1181 switch (ret) {
1182 case 0:
1183 cdev->private->state = DEV_STATE_OFFLINE;
1184 wake_up(&cdev->private->wait_q);
1185 break;
1186 case -ENODEV:
1187 cdev->private->state = DEV_STATE_NOT_OPER;
1188 wake_up(&cdev->private->wait_q);
1189 break;
1190 default:
1191 ccw_device_set_timeout(cdev, HZ/10);
1192 }
1193}
1194
1195/*
1196 * No operation action. This is used e.g. to ignore a timeout event in
1197 * state offline.
1198 */
1199static void
1200ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1201{
1202}
1203
1204/*
1205 * Bug operation action.
1206 */
1207static void
1208ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1209{
1210 printk(KERN_EMERG "dev_jumptable[%i][%i] == NULL\n",
1211 cdev->private->state, dev_event);
1212 BUG();
1213}
1214
1215/*
1216 * device statemachine
1217 */
1218fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1219 [DEV_STATE_NOT_OPER] = {
1220 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1221 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1222 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1223 [DEV_EVENT_VERIFY] = ccw_device_nop,
1224 },
1225 [DEV_STATE_SENSE_PGID] = {
1226 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1227 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1228 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1229 [DEV_EVENT_VERIFY] = ccw_device_nop,
1230 },
1231 [DEV_STATE_SENSE_ID] = {
1232 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1233 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1234 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1235 [DEV_EVENT_VERIFY] = ccw_device_nop,
1236 },
1237 [DEV_STATE_OFFLINE] = {
1238 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1239 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1240 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1241 [DEV_EVENT_VERIFY] = ccw_device_nop,
1242 },
1243 [DEV_STATE_VERIFY] = {
1244 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1245 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1246 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001247 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 },
1249 [DEV_STATE_ONLINE] = {
1250 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1251 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1252 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1253 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1254 },
1255 [DEV_STATE_W4SENSE] = {
1256 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1257 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1258 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1259 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1260 },
1261 [DEV_STATE_DISBAND_PGID] = {
1262 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1263 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1264 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1265 [DEV_EVENT_VERIFY] = ccw_device_nop,
1266 },
1267 [DEV_STATE_BOXED] = {
1268 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1269 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1270 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1271 [DEV_EVENT_VERIFY] = ccw_device_nop,
1272 },
1273 /* states to wait for i/o completion before doing something */
1274 [DEV_STATE_CLEAR_VERIFY] = {
1275 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1276 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1277 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1278 [DEV_EVENT_VERIFY] = ccw_device_nop,
1279 },
1280 [DEV_STATE_TIMEOUT_KILL] = {
1281 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1282 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1283 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1284 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1285 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 [DEV_STATE_QUIESCE] = {
1287 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1288 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1289 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1290 [DEV_EVENT_VERIFY] = ccw_device_nop,
1291 },
1292 /* special states for devices gone not operational */
1293 [DEV_STATE_DISCONNECTED] = {
1294 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1295 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1296 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001297 [DEV_EVENT_VERIFY] = ccw_device_start_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 },
1299 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1300 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1301 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1302 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1303 [DEV_EVENT_VERIFY] = ccw_device_nop,
1304 },
1305 [DEV_STATE_CMFCHANGE] = {
1306 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1307 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1308 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1309 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1310 },
Cornelia Huck94bb0632006-06-29 15:08:41 +02001311 [DEV_STATE_CMFUPDATE] = {
1312 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1313 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1314 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1315 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1316 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317};
1318
1319/*
1320 * io_subchannel_irq is called for "real" interrupts or for status
1321 * pending conditions on msch.
1322 */
1323void
1324io_subchannel_irq (struct device *pdev)
1325{
1326 struct ccw_device *cdev;
1327
1328 cdev = to_subchannel(pdev)->dev.driver_data;
1329
1330 CIO_TRACE_EVENT (3, "IRQ");
1331 CIO_TRACE_EVENT (3, pdev->bus_id);
1332 if (cdev)
1333 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1334}
1335
1336EXPORT_SYMBOL_GPL(ccw_device_set_timeout);