blob: 8baa9cd3794cdc14a47d1e6f625c1fbd8fe8164c [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26int
27device_is_online(struct subchannel *sch)
28{
29 struct ccw_device *cdev;
30
31 if (!sch->dev.driver_data)
32 return 0;
33 cdev = sch->dev.driver_data;
34 return (cdev->private->state == DEV_STATE_ONLINE);
35}
36
37int
38device_is_disconnected(struct subchannel *sch)
39{
40 struct ccw_device *cdev;
41
42 if (!sch->dev.driver_data)
43 return 0;
44 cdev = sch->dev.driver_data;
45 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
46 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
47}
48
49void
50device_set_disconnected(struct subchannel *sch)
51{
52 struct ccw_device *cdev;
53
54 if (!sch->dev.driver_data)
55 return;
56 cdev = sch->dev.driver_data;
57 ccw_device_set_timeout(cdev, 0);
58 cdev->private->flags.fake_irb = 0;
59 cdev->private->state = DEV_STATE_DISCONNECTED;
60}
61
Cornelia Huckd23861f2006-12-04 15:41:04 +010062void device_set_intretry(struct subchannel *sch)
63{
64 struct ccw_device *cdev;
65
66 cdev = sch->dev.driver_data;
67 if (!cdev)
68 return;
69 cdev->private->flags.intretry = 1;
70}
71
Cornelia Huck24cb5b42006-12-04 15:41:01 +010072int device_trigger_verify(struct subchannel *sch)
73{
74 struct ccw_device *cdev;
75
76 cdev = sch->dev.driver_data;
77 if (!cdev || !cdev->online)
78 return -EINVAL;
79 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
80 return 0;
81}
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*
84 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
85 */
86static void
87ccw_device_timeout(unsigned long data)
88{
89 struct ccw_device *cdev;
90
91 cdev = (struct ccw_device *) data;
92 spin_lock_irq(cdev->ccwlock);
93 dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
94 spin_unlock_irq(cdev->ccwlock);
95}
96
97/*
98 * Set timeout
99 */
100void
101ccw_device_set_timeout(struct ccw_device *cdev, int expires)
102{
103 if (expires == 0) {
104 del_timer(&cdev->private->timer);
105 return;
106 }
107 if (timer_pending(&cdev->private->timer)) {
108 if (mod_timer(&cdev->private->timer, jiffies + expires))
109 return;
110 }
111 cdev->private->timer.function = ccw_device_timeout;
112 cdev->private->timer.data = (unsigned long) cdev;
113 cdev->private->timer.expires = jiffies + expires;
114 add_timer(&cdev->private->timer);
115}
116
117/* Kill any pending timers after machine check. */
118void
119device_kill_pending_timer(struct subchannel *sch)
120{
121 struct ccw_device *cdev;
122
123 if (!sch->dev.driver_data)
124 return;
125 cdev = sch->dev.driver_data;
126 ccw_device_set_timeout(cdev, 0);
127}
128
129/*
130 * Cancel running i/o. This is called repeatedly since halt/clear are
131 * asynchronous operations. We do one try with cio_cancel, two tries
132 * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
133 * Returns 0 if device now idle, -ENODEV for device not operational and
134 * -EBUSY if an interrupt is expected (either from halt/clear or from a
135 * status pending).
136 */
137int
138ccw_device_cancel_halt_clear(struct ccw_device *cdev)
139{
140 struct subchannel *sch;
141 int ret;
142
143 sch = to_subchannel(cdev->dev.parent);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800144 ret = stsch(sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (ret || !sch->schib.pmcw.dnv)
146 return -ENODEV;
147 if (!sch->schib.pmcw.ena || sch->schib.scsw.actl == 0)
148 /* Not operational or no activity -> done. */
149 return 0;
150 /* Stage 1: cancel io. */
151 if (!(sch->schib.scsw.actl & SCSW_ACTL_HALT_PEND) &&
152 !(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
153 ret = cio_cancel(sch);
154 if (ret != -EINVAL)
155 return ret;
156 /* cancel io unsuccessful. From now on it is asynchronous. */
157 cdev->private->iretry = 3; /* 3 halt retries. */
158 }
159 if (!(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
160 /* Stage 2: halt io. */
161 if (cdev->private->iretry) {
162 cdev->private->iretry--;
163 ret = cio_halt(sch);
Peter Oberparleiterba4ba8a2006-07-27 14:00:23 +0200164 if (ret != -EBUSY)
165 return (ret == 0) ? -EBUSY : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167 /* halt io unsuccessful. */
168 cdev->private->iretry = 255; /* 255 clear retries. */
169 }
170 /* Stage 3: clear io. */
171 if (cdev->private->iretry) {
172 cdev->private->iretry--;
173 ret = cio_clear (sch);
174 return (ret == 0) ? -EBUSY : ret;
175 }
176 panic("Can't stop i/o on subchannel.\n");
177}
178
179static int
180ccw_device_handle_oper(struct ccw_device *cdev)
181{
182 struct subchannel *sch;
183
184 sch = to_subchannel(cdev->dev.parent);
185 cdev->private->flags.recog_done = 1;
186 /*
187 * Check if cu type and device type still match. If
188 * not, it is certainly another device and we have to
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100189 * de- and re-register.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 */
191 if (cdev->id.cu_type != cdev->private->senseid.cu_type ||
192 cdev->id.cu_model != cdev->private->senseid.cu_model ||
193 cdev->id.dev_type != cdev->private->senseid.dev_type ||
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100194 cdev->id.dev_model != cdev->private->senseid.dev_model) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100196 ccw_device_do_unreg_rereg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 queue_work(ccw_device_work, &cdev->private->kick_work);
198 return 0;
199 }
200 cdev->private->flags.donotify = 1;
201 return 1;
202}
203
204/*
205 * The machine won't give us any notification by machine check if a chpid has
206 * been varied online on the SE so we have to find out by magic (i. e. driving
207 * the channel subsystem to device selection and updating our path masks).
208 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100209static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210__recover_lost_chpids(struct subchannel *sch, int old_lpm)
211{
212 int mask, i;
213
214 for (i = 0; i<8; i++) {
215 mask = 0x80 >> i;
216 if (!(sch->lpm & mask))
217 continue;
218 if (old_lpm & mask)
219 continue;
220 chpid_is_actually_online(sch->schib.pmcw.chpid[i]);
221 }
222}
223
224/*
225 * Stop device recognition.
226 */
227static void
228ccw_device_recog_done(struct ccw_device *cdev, int state)
229{
230 struct subchannel *sch;
231 int notify, old_lpm, same_dev;
232
233 sch = to_subchannel(cdev->dev.parent);
234
235 ccw_device_set_timeout(cdev, 0);
236 cio_disable_subchannel(sch);
237 /*
238 * Now that we tried recognition, we have performed device selection
239 * through ssch() and the path information is up to date.
240 */
241 old_lpm = sch->lpm;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800242 stsch(sch->schid, &sch->schib);
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200243 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck4ffa9232005-07-29 14:03:37 -0700244 /* Check since device may again have become not operational. */
245 if (!sch->schib.pmcw.dnv)
246 state = DEV_STATE_NOT_OPER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
248 /* Force reprobe on all chpids. */
249 old_lpm = 0;
250 if (sch->lpm != old_lpm)
251 __recover_lost_chpids(sch, old_lpm);
252 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
253 if (state == DEV_STATE_NOT_OPER) {
254 cdev->private->flags.recog_done = 1;
255 cdev->private->state = DEV_STATE_DISCONNECTED;
256 return;
257 }
258 /* Boxed devices don't need extra treatment. */
259 }
260 notify = 0;
261 same_dev = 0; /* Keep the compiler quiet... */
262 switch (state) {
263 case DEV_STATE_NOT_OPER:
264 CIO_DEBUG(KERN_WARNING, 2,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800265 "SenseID : unknown device %04x on subchannel "
Cornelia Huck78964262006-10-11 15:31:38 +0200266 "0.%x.%04x\n", cdev->private->dev_id.devno,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800267 sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 break;
269 case DEV_STATE_OFFLINE:
270 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
271 same_dev = ccw_device_handle_oper(cdev);
272 notify = 1;
273 }
274 /* fill out sense information */
Heiko Carstens81388d22006-09-20 15:59:17 +0200275 memset(&cdev->id, 0, sizeof(cdev->id));
Heiko Carstens292888c2006-08-30 14:33:35 +0200276 cdev->id.cu_type = cdev->private->senseid.cu_type;
277 cdev->id.cu_model = cdev->private->senseid.cu_model;
278 cdev->id.dev_type = cdev->private->senseid.dev_type;
279 cdev->id.dev_model = cdev->private->senseid.dev_model;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (notify) {
281 cdev->private->state = DEV_STATE_OFFLINE;
282 if (same_dev) {
283 /* Get device online again. */
284 ccw_device_online(cdev);
285 wake_up(&cdev->private->wait_q);
286 }
287 return;
288 }
289 /* Issue device info message. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800290 CIO_DEBUG(KERN_INFO, 2, "SenseID : device 0.%x.%04x reports: "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800292 "%04X/%02X\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200293 cdev->private->dev_id.ssid,
294 cdev->private->dev_id.devno,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 cdev->id.cu_type, cdev->id.cu_model,
296 cdev->id.dev_type, cdev->id.dev_model);
297 break;
298 case DEV_STATE_BOXED:
299 CIO_DEBUG(KERN_WARNING, 2,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800300 "SenseID : boxed device %04x on subchannel "
Cornelia Huck78964262006-10-11 15:31:38 +0200301 "0.%x.%04x\n", cdev->private->dev_id.devno,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800302 sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 break;
304 }
305 cdev->private->state = state;
306 io_subchannel_recog_done(cdev);
307 if (state != DEV_STATE_NOT_OPER)
308 wake_up(&cdev->private->wait_q);
309}
310
311/*
312 * Function called from device_id.c after sense id has completed.
313 */
314void
315ccw_device_sense_id_done(struct ccw_device *cdev, int err)
316{
317 switch (err) {
318 case 0:
319 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
320 break;
321 case -ETIME: /* Sense id stopped by timeout. */
322 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
323 break;
324 default:
325 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
326 break;
327 }
328}
329
330static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100331ccw_device_oper_notify(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100333 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 struct ccw_device *cdev;
335 struct subchannel *sch;
336 int ret;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100337 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100339 priv = container_of(work, struct ccw_device_private, kick_work);
340 cdev = priv->cdev;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100341 spin_lock_irqsave(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100343 if (sch->driver && sch->driver->notify) {
344 spin_unlock_irqrestore(cdev->ccwlock, flags);
345 ret = sch->driver->notify(&sch->dev, CIO_OPER);
346 spin_lock_irqsave(cdev->ccwlock, flags);
347 } else
348 ret = 0;
349 if (ret) {
350 /* Reenable channel measurements, if needed. */
351 spin_unlock_irqrestore(cdev->ccwlock, flags);
352 cmf_reenable(cdev);
353 spin_lock_irqsave(cdev->ccwlock, flags);
354 wake_up(&cdev->private->wait_q);
355 }
356 spin_unlock_irqrestore(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (!ret)
358 /* Driver doesn't want device back. */
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100359 ccw_device_do_unreg_rereg(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
362/*
363 * Finished with online/offline processing.
364 */
365static void
366ccw_device_done(struct ccw_device *cdev, int state)
367{
368 struct subchannel *sch;
369
370 sch = to_subchannel(cdev->dev.parent);
371
Cornelia Huckf1ee3282006-10-04 20:02:02 +0200372 ccw_device_set_timeout(cdev, 0);
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (state != DEV_STATE_ONLINE)
375 cio_disable_subchannel(sch);
376
377 /* Reset device status. */
378 memset(&cdev->private->irb, 0, sizeof(struct irb));
379
380 cdev->private->state = state;
381
382
383 if (state == DEV_STATE_BOXED)
384 CIO_DEBUG(KERN_WARNING, 2,
385 "Boxed device %04x on subchannel %04x\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200386 cdev->private->dev_id.devno, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 if (cdev->private->flags.donotify) {
389 cdev->private->flags.donotify = 0;
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100390 PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
392 }
393 wake_up(&cdev->private->wait_q);
394
395 if (css_init_done && state != DEV_STATE_ONLINE)
396 put_device (&cdev->dev);
397}
398
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100399static int cmp_pgid(struct pgid *p1, struct pgid *p2)
Cornelia Huck7e560812006-07-12 16:40:19 +0200400{
401 char *c1;
402 char *c2;
403
404 c1 = (char *)p1;
405 c2 = (char *)p2;
406
407 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
408}
409
410static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
411{
412 int i;
413 int last;
414
415 last = 0;
416 for (i = 0; i < 8; i++) {
417 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
418 /* No PGID yet */
419 continue;
420 if (cdev->private->pgid[last].inf.ps.state1 ==
421 SNID_STATE1_RESET) {
422 /* First non-zero PGID */
423 last = i;
424 continue;
425 }
426 if (cmp_pgid(&cdev->private->pgid[i],
427 &cdev->private->pgid[last]) == 0)
428 /* Non-conflicting PGIDs */
429 continue;
430
431 /* PGID mismatch, can't pathgroup. */
432 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
433 "0.%x.%04x, can't pathgroup\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200434 cdev->private->dev_id.ssid,
435 cdev->private->dev_id.devno);
Cornelia Huck7e560812006-07-12 16:40:19 +0200436 cdev->private->options.pgroup = 0;
437 return;
438 }
439 if (cdev->private->pgid[last].inf.ps.state1 ==
440 SNID_STATE1_RESET)
441 /* No previous pgid found */
442 memcpy(&cdev->private->pgid[0], &css[0]->global_pgid,
443 sizeof(struct pgid));
444 else
445 /* Use existing pgid */
446 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
447 sizeof(struct pgid));
448}
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450/*
451 * Function called from device_pgid.c after sense path ground has completed.
452 */
453void
454ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
455{
456 struct subchannel *sch;
457
458 sch = to_subchannel(cdev->dev.parent);
459 switch (err) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200460 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
461 cdev->private->options.pgroup = 0;
462 break;
463 case 0: /* success */
464 case -EACCES: /* partial success, some paths not operational */
465 /* Check if all pgids are equal or 0. */
466 __ccw_device_get_common_pgid(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 break;
468 case -ETIME: /* Sense path group id stopped by timeout. */
469 case -EUSERS: /* device is reserved for someone else. */
470 ccw_device_done(cdev, DEV_STATE_BOXED);
Cornelia Huck7e560812006-07-12 16:40:19 +0200471 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 default:
473 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Cornelia Huck7e560812006-07-12 16:40:19 +0200474 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
Cornelia Huck7e560812006-07-12 16:40:19 +0200476 /* Start Path Group verification. */
Cornelia Huck7e560812006-07-12 16:40:19 +0200477 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200478 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200479 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
482/*
483 * Start device recognition.
484 */
485int
486ccw_device_recognition(struct ccw_device *cdev)
487{
488 struct subchannel *sch;
489 int ret;
490
491 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
492 (cdev->private->state != DEV_STATE_BOXED))
493 return -EINVAL;
494 sch = to_subchannel(cdev->dev.parent);
495 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
496 if (ret != 0)
497 /* Couldn't enable the subchannel for i/o. Sick device. */
498 return ret;
499
500 /* After 60s the device recognition is considered to have failed. */
501 ccw_device_set_timeout(cdev, 60*HZ);
502
503 /*
504 * We used to start here with a sense pgid to find out whether a device
505 * is locked by someone else. Unfortunately, the sense pgid command
506 * code has other meanings on devices predating the path grouping
507 * algorithm, so we start with sense id and box the device after an
508 * timeout (or if sense pgid during path verification detects the device
509 * is locked, as may happen on newer devices).
510 */
511 cdev->private->flags.recog_done = 0;
512 cdev->private->state = DEV_STATE_SENSE_ID;
513 ccw_device_sense_id_start(cdev);
514 return 0;
515}
516
517/*
518 * Handle timeout in device recognition.
519 */
520static void
521ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
522{
523 int ret;
524
525 ret = ccw_device_cancel_halt_clear(cdev);
526 switch (ret) {
527 case 0:
528 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
529 break;
530 case -ENODEV:
531 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
532 break;
533 default:
534 ccw_device_set_timeout(cdev, 3*HZ);
535 }
536}
537
538
539static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100540ccw_device_nopath_notify(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100542 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 struct ccw_device *cdev;
544 struct subchannel *sch;
545 int ret;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100546 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100548 priv = container_of(work, struct ccw_device_private, kick_work);
549 cdev = priv->cdev;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100550 spin_lock_irqsave(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 sch = to_subchannel(cdev->dev.parent);
552 /* Extra sanity. */
553 if (sch->lpm)
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100554 goto out_unlock;
555 if (sch->driver && sch->driver->notify) {
556 spin_unlock_irqrestore(cdev->ccwlock, flags);
557 ret = sch->driver->notify(&sch->dev, CIO_NO_PATH);
558 spin_lock_irqsave(cdev->ccwlock, flags);
559 } else
560 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (!ret) {
562 if (get_device(&sch->dev)) {
563 /* Driver doesn't want to keep device. */
564 cio_disable_subchannel(sch);
565 if (get_device(&cdev->dev)) {
566 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100567 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 queue_work(ccw_device_work,
569 &cdev->private->kick_work);
570 } else
571 put_device(&sch->dev);
572 }
573 } else {
574 cio_disable_subchannel(sch);
575 ccw_device_set_timeout(cdev, 0);
576 cdev->private->flags.fake_irb = 0;
577 cdev->private->state = DEV_STATE_DISCONNECTED;
578 wake_up(&cdev->private->wait_q);
579 }
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100580out_unlock:
581 spin_unlock_irqrestore(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
584void
585ccw_device_verify_done(struct ccw_device *cdev, int err)
586{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200587 struct subchannel *sch;
588
589 sch = to_subchannel(cdev->dev.parent);
590 /* Update schib - pom may have changed. */
591 stsch(sch->schid, &sch->schib);
592 /* Update lpm with verified path mask. */
593 sch->lpm = sch->vpm;
594 /* Repeat path verification? */
595 if (cdev->private->flags.doverify) {
596 cdev->private->flags.doverify = 0;
597 ccw_device_verify_start(cdev);
598 return;
599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 switch (err) {
601 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
602 cdev->private->options.pgroup = 0;
603 case 0:
604 ccw_device_done(cdev, DEV_STATE_ONLINE);
605 /* Deliver fake irb to device driver, if needed. */
606 if (cdev->private->flags.fake_irb) {
607 memset(&cdev->private->irb, 0, sizeof(struct irb));
Heiko Carstens292888c2006-08-30 14:33:35 +0200608 cdev->private->irb.scsw.cc = 1;
609 cdev->private->irb.scsw.fctl = SCSW_FCTL_START_FUNC;
610 cdev->private->irb.scsw.actl = SCSW_ACTL_START_PEND;
611 cdev->private->irb.scsw.stctl = SCSW_STCTL_STATUS_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 cdev->private->flags.fake_irb = 0;
613 if (cdev->handler)
614 cdev->handler(cdev, cdev->private->intparm,
615 &cdev->private->irb);
616 memset(&cdev->private->irb, 0, sizeof(struct irb));
617 }
618 break;
619 case -ETIME:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200620 /* Reset oper notify indication after verify error. */
621 cdev->private->flags.donotify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 ccw_device_done(cdev, DEV_STATE_BOXED);
623 break;
624 default:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200625 /* Reset oper notify indication after verify error. */
626 cdev->private->flags.donotify = 0;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100627 if (cdev->online) {
628 PREPARE_WORK(&cdev->private->kick_work,
629 ccw_device_nopath_notify);
630 queue_work(ccw_device_notify_work,
631 &cdev->private->kick_work);
632 } else
633 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 break;
635 }
636}
637
638/*
639 * Get device online.
640 */
641int
642ccw_device_online(struct ccw_device *cdev)
643{
644 struct subchannel *sch;
645 int ret;
646
647 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
648 (cdev->private->state != DEV_STATE_BOXED))
649 return -EINVAL;
650 sch = to_subchannel(cdev->dev.parent);
651 if (css_init_done && !get_device(&cdev->dev))
652 return -ENODEV;
653 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
654 if (ret != 0) {
655 /* Couldn't enable the subchannel for i/o. Sick device. */
656 if (ret == -ENODEV)
657 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
658 return ret;
659 }
660 /* Do we want to do path grouping? */
661 if (!cdev->private->options.pgroup) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200662 /* Start initial path verification. */
663 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200664 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200665 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 0;
667 }
668 /* Do a SensePGID first. */
669 cdev->private->state = DEV_STATE_SENSE_PGID;
670 ccw_device_sense_pgid_start(cdev);
671 return 0;
672}
673
674void
675ccw_device_disband_done(struct ccw_device *cdev, int err)
676{
677 switch (err) {
678 case 0:
679 ccw_device_done(cdev, DEV_STATE_OFFLINE);
680 break;
681 case -ETIME:
682 ccw_device_done(cdev, DEV_STATE_BOXED);
683 break;
684 default:
685 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
686 break;
687 }
688}
689
690/*
691 * Shutdown device.
692 */
693int
694ccw_device_offline(struct ccw_device *cdev)
695{
696 struct subchannel *sch;
697
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100698 if (ccw_device_is_orphan(cdev)) {
699 ccw_device_done(cdev, DEV_STATE_OFFLINE);
700 return 0;
701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 sch = to_subchannel(cdev->dev.parent);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800703 if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return -ENODEV;
705 if (cdev->private->state != DEV_STATE_ONLINE) {
706 if (sch->schib.scsw.actl != 0)
707 return -EBUSY;
708 return -EINVAL;
709 }
710 if (sch->schib.scsw.actl != 0)
711 return -EBUSY;
712 /* Are we doing path grouping? */
713 if (!cdev->private->options.pgroup) {
714 /* No, set state offline immediately. */
715 ccw_device_done(cdev, DEV_STATE_OFFLINE);
716 return 0;
717 }
718 /* Start Set Path Group commands. */
719 cdev->private->state = DEV_STATE_DISBAND_PGID;
720 ccw_device_disband_start(cdev);
721 return 0;
722}
723
724/*
725 * Handle timeout in device online/offline process.
726 */
727static void
728ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
729{
730 int ret;
731
732 ret = ccw_device_cancel_halt_clear(cdev);
733 switch (ret) {
734 case 0:
735 ccw_device_done(cdev, DEV_STATE_BOXED);
736 break;
737 case -ENODEV:
738 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
739 break;
740 default:
741 ccw_device_set_timeout(cdev, 3*HZ);
742 }
743}
744
745/*
746 * Handle not oper event in device recognition.
747 */
748static void
749ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
750{
751 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
752}
753
754/*
755 * Handle not operational event while offline.
756 */
757static void
758ccw_device_offline_notoper(struct ccw_device *cdev, enum dev_event dev_event)
759{
760 struct subchannel *sch;
761
762 cdev->private->state = DEV_STATE_NOT_OPER;
763 sch = to_subchannel(cdev->dev.parent);
764 if (get_device(&cdev->dev)) {
765 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100766 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 queue_work(ccw_device_work, &cdev->private->kick_work);
768 }
769 wake_up(&cdev->private->wait_q);
770}
771
772/*
773 * Handle not operational event while online.
774 */
775static void
776ccw_device_online_notoper(struct ccw_device *cdev, enum dev_event dev_event)
777{
778 struct subchannel *sch;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100779 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100782 if (sch->driver->notify) {
783 spin_unlock_irq(cdev->ccwlock);
784 ret = sch->driver->notify(&sch->dev,
785 sch->lpm ? CIO_GONE : CIO_NO_PATH);
786 spin_lock_irq(cdev->ccwlock);
787 } else
788 ret = 0;
789 if (ret) {
790 ccw_device_set_timeout(cdev, 0);
791 cdev->private->flags.fake_irb = 0;
792 cdev->private->state = DEV_STATE_DISCONNECTED;
793 wake_up(&cdev->private->wait_q);
794 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796 cdev->private->state = DEV_STATE_NOT_OPER;
797 cio_disable_subchannel(sch);
798 if (sch->schib.scsw.actl != 0) {
799 // FIXME: not-oper indication to device driver ?
800 ccw_device_call_handler(cdev);
801 }
802 if (get_device(&cdev->dev)) {
803 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100804 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 queue_work(ccw_device_work, &cdev->private->kick_work);
806 }
807 wake_up(&cdev->private->wait_q);
808}
809
810/*
811 * Handle path verification event.
812 */
813static void
814ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
815{
816 struct subchannel *sch;
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (cdev->private->state == DEV_STATE_W4SENSE) {
819 cdev->private->flags.doverify = 1;
820 return;
821 }
822 sch = to_subchannel(cdev->dev.parent);
823 /*
824 * Since we might not just be coming from an interrupt from the
825 * subchannel we have to update the schib.
826 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800827 stsch(sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 if (sch->schib.scsw.actl != 0 ||
Peter Oberparleiter65200c22006-08-07 17:00:33 +0200830 (sch->schib.scsw.stctl & SCSW_STCTL_STATUS_PEND) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
832 /*
833 * No final status yet or final status not yet delivered
834 * to the device driver. Can't do path verfication now,
835 * delay until final status was delivered.
836 */
837 cdev->private->flags.doverify = 1;
838 return;
839 }
840 /* Device is idle, we can do the path verification. */
841 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200842 cdev->private->flags.doverify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 ccw_device_verify_start(cdev);
844}
845
846/*
847 * Got an interrupt for a normal io (state online).
848 */
849static void
850ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
851{
852 struct irb *irb;
853
854 irb = (struct irb *) __LC_IRB;
855 /* Check for unsolicited interrupt. */
856 if ((irb->scsw.stctl ==
857 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
858 && (!irb->scsw.cc)) {
859 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
860 !irb->esw.esw0.erw.cons) {
861 /* Unit check but no sense data. Need basic sense. */
862 if (ccw_device_do_sense(cdev, irb) != 0)
863 goto call_handler_unsol;
Cornelia Hucke0ec5742006-06-04 02:51:27 -0700864 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 cdev->private->state = DEV_STATE_W4SENSE;
866 cdev->private->intparm = 0;
867 return;
868 }
869call_handler_unsol:
870 if (cdev->handler)
871 cdev->handler (cdev, 0, irb);
Cornelia Huck18374d32007-02-05 21:17:09 +0100872 if (cdev->private->flags.doverify)
873 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return;
875 }
876 /* Accumulate status and find out if a basic sense is needed. */
877 ccw_device_accumulate_irb(cdev, irb);
878 if (cdev->private->flags.dosense) {
879 if (ccw_device_do_sense(cdev, irb) == 0) {
880 cdev->private->state = DEV_STATE_W4SENSE;
881 }
882 return;
883 }
884 /* Call the handler. */
885 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
886 /* Start delayed path verification. */
887 ccw_device_online_verify(cdev, 0);
888}
889
890/*
891 * Got an timeout in online state.
892 */
893static void
894ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
895{
896 int ret;
897
898 ccw_device_set_timeout(cdev, 0);
899 ret = ccw_device_cancel_halt_clear(cdev);
900 if (ret == -EBUSY) {
901 ccw_device_set_timeout(cdev, 3*HZ);
902 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
903 return;
904 }
905 if (ret == -ENODEV) {
906 struct subchannel *sch;
907
908 sch = to_subchannel(cdev->dev.parent);
909 if (!sch->lpm) {
910 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100911 ccw_device_nopath_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 queue_work(ccw_device_notify_work,
913 &cdev->private->kick_work);
914 } else
915 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
916 } else if (cdev->handler)
917 cdev->handler(cdev, cdev->private->intparm,
918 ERR_PTR(-ETIMEDOUT));
919}
920
921/*
922 * Got an interrupt for a basic sense.
923 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100924static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
926{
927 struct irb *irb;
928
929 irb = (struct irb *) __LC_IRB;
930 /* Check for unsolicited interrupt. */
931 if (irb->scsw.stctl ==
932 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
933 if (irb->scsw.cc == 1)
934 /* Basic sense hasn't started. Try again. */
935 ccw_device_do_sense(cdev, irb);
936 else {
Cornelia Huck08983782006-10-11 15:31:30 +0200937 printk(KERN_INFO "Huh? %s(%s): unsolicited "
938 "interrupt...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 __FUNCTION__, cdev->dev.bus_id);
940 if (cdev->handler)
941 cdev->handler (cdev, 0, irb);
942 }
943 return;
944 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800945 /*
946 * Check if a halt or clear has been issued in the meanwhile. If yes,
947 * only deliver the halt/clear interrupt to the device driver as if it
948 * had killed the original request.
949 */
950 if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
Cornelia Huckd23861f2006-12-04 15:41:04 +0100951 /* Retry Basic Sense if requested. */
952 if (cdev->private->flags.intretry) {
953 cdev->private->flags.intretry = 0;
954 ccw_device_do_sense(cdev, irb);
955 return;
956 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800957 cdev->private->flags.dosense = 0;
958 memset(&cdev->private->irb, 0, sizeof(struct irb));
959 ccw_device_accumulate_irb(cdev, irb);
960 goto call_handler;
961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 /* Add basic sense info to irb. */
963 ccw_device_accumulate_basic_sense(cdev, irb);
964 if (cdev->private->flags.dosense) {
965 /* Another basic sense is needed. */
966 ccw_device_do_sense(cdev, irb);
967 return;
968 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800969call_handler:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 cdev->private->state = DEV_STATE_ONLINE;
971 /* Call the handler. */
972 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
973 /* Start delayed path verification. */
974 ccw_device_online_verify(cdev, 0);
975}
976
977static void
978ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
979{
980 struct irb *irb;
981
982 irb = (struct irb *) __LC_IRB;
983 /* Accumulate status. We don't do basic sense. */
984 ccw_device_accumulate_irb(cdev, irb);
Cornelia Huckb4f7b1e2006-06-29 15:03:35 +0200985 /* Remember to clear irb to avoid residuals. */
986 memset(&cdev->private->irb, 0, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 /* Try to start delayed device verification. */
988 ccw_device_online_verify(cdev, 0);
989 /* Note: Don't call handler for cio initiated clear! */
990}
991
992static void
993ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
994{
995 struct subchannel *sch;
996
997 sch = to_subchannel(cdev->dev.parent);
998 ccw_device_set_timeout(cdev, 0);
999 /* OK, i/o is dead now. Call interrupt handler. */
1000 cdev->private->state = DEV_STATE_ONLINE;
1001 if (cdev->handler)
1002 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +02001003 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (!sch->lpm) {
1005 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001006 ccw_device_nopath_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
1008 } else if (cdev->private->flags.doverify)
1009 /* Start delayed path verification. */
1010 ccw_device_online_verify(cdev, 0);
1011}
1012
1013static void
1014ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1015{
1016 int ret;
1017
1018 ret = ccw_device_cancel_halt_clear(cdev);
1019 if (ret == -EBUSY) {
1020 ccw_device_set_timeout(cdev, 3*HZ);
1021 return;
1022 }
1023 if (ret == -ENODEV) {
1024 struct subchannel *sch;
1025
1026 sch = to_subchannel(cdev->dev.parent);
1027 if (!sch->lpm) {
1028 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001029 ccw_device_nopath_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 queue_work(ccw_device_notify_work,
1031 &cdev->private->kick_work);
1032 } else
1033 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1034 return;
1035 }
1036 //FIXME: Can we get here?
1037 cdev->private->state = DEV_STATE_ONLINE;
1038 if (cdev->handler)
1039 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +02001040 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
Cornelia Hucke7769b42006-10-11 15:31:41 +02001043void device_kill_io(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 int ret;
Cornelia Hucke7769b42006-10-11 15:31:41 +02001046 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Cornelia Hucke7769b42006-10-11 15:31:41 +02001048 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 ret = ccw_device_cancel_halt_clear(cdev);
1050 if (ret == -EBUSY) {
1051 ccw_device_set_timeout(cdev, 3*HZ);
1052 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
1053 return;
1054 }
1055 if (ret == -ENODEV) {
1056 if (!sch->lpm) {
1057 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001058 ccw_device_nopath_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 queue_work(ccw_device_notify_work,
1060 &cdev->private->kick_work);
1061 } else
1062 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1063 return;
1064 }
1065 if (cdev->handler)
1066 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +02001067 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (!sch->lpm) {
1069 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001070 ccw_device_nopath_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
Cornelia Hucke7769b42006-10-11 15:31:41 +02001072 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /* Start delayed path verification. */
1074 ccw_device_online_verify(cdev, 0);
1075}
1076
1077static void
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001078ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001080 /* Start verification after current task finished. */
Cornelia Huck7e560812006-07-12 16:40:19 +02001081 cdev->private->flags.doverify = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
1084static void
1085ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
1086{
1087 struct irb *irb;
1088
1089 switch (dev_event) {
1090 case DEV_EVENT_INTERRUPT:
1091 irb = (struct irb *) __LC_IRB;
1092 /* Check for unsolicited interrupt. */
1093 if ((irb->scsw.stctl ==
1094 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
1095 (!irb->scsw.cc))
1096 /* FIXME: we should restart stlck here, but this
1097 * is extremely unlikely ... */
1098 goto out_wakeup;
1099
1100 ccw_device_accumulate_irb(cdev, irb);
1101 /* We don't care about basic sense etc. */
1102 break;
1103 default: /* timeout */
1104 break;
1105 }
1106out_wakeup:
1107 wake_up(&cdev->private->wait_q);
1108}
1109
1110static void
1111ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1112{
1113 struct subchannel *sch;
1114
1115 sch = to_subchannel(cdev->dev.parent);
1116 if (cio_enable_subchannel(sch, sch->schib.pmcw.isc) != 0)
1117 /* Couldn't enable the subchannel for i/o. Sick device. */
1118 return;
1119
1120 /* After 60s the device recognition is considered to have failed. */
1121 ccw_device_set_timeout(cdev, 60*HZ);
1122
1123 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1124 ccw_device_sense_id_start(cdev);
1125}
1126
1127void
1128device_trigger_reprobe(struct subchannel *sch)
1129{
1130 struct ccw_device *cdev;
1131
1132 if (!sch->dev.driver_data)
1133 return;
1134 cdev = sch->dev.driver_data;
1135 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1136 return;
1137
1138 /* Update some values. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001139 if (stsch(sch->schid, &sch->schib))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return;
Cornelia Huck7674da72006-12-08 15:54:21 +01001141 if (!sch->schib.pmcw.dnv)
1142 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 /*
1144 * The pim, pam, pom values may not be accurate, but they are the best
1145 * we have before performing device selection :/
1146 */
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001147 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 /* Re-set some bits in the pmcw that were lost. */
1149 sch->schib.pmcw.isc = 3;
1150 sch->schib.pmcw.csense = 1;
1151 sch->schib.pmcw.ena = 0;
1152 if ((sch->lpm & (sch->lpm - 1)) != 0)
1153 sch->schib.pmcw.mp = 1;
1154 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
1155 /* We should also udate ssd info, but this has to wait. */
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001156 /* Check if this is another device which appeared on the same sch. */
1157 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1158 PREPARE_WORK(&cdev->private->kick_work,
1159 ccw_device_move_to_orphanage);
1160 queue_work(ccw_device_work, &cdev->private->kick_work);
1161 } else
1162 ccw_device_start_id(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
1165static void
1166ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1167{
1168 struct subchannel *sch;
1169
1170 sch = to_subchannel(cdev->dev.parent);
1171 /*
1172 * An interrupt in state offline means a previous disable was not
1173 * successful. Try again.
1174 */
1175 cio_disable_subchannel(sch);
1176}
1177
1178static void
1179ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1180{
1181 retry_set_schib(cdev);
1182 cdev->private->state = DEV_STATE_ONLINE;
1183 dev_fsm_event(cdev, dev_event);
1184}
1185
Cornelia Huck94bb0632006-06-29 15:08:41 +02001186static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1187 enum dev_event dev_event)
1188{
1189 cmf_retry_copy_block(cdev);
1190 cdev->private->state = DEV_STATE_ONLINE;
1191 dev_fsm_event(cdev, dev_event);
1192}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194static void
1195ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1196{
1197 ccw_device_set_timeout(cdev, 0);
1198 if (dev_event == DEV_EVENT_NOTOPER)
1199 cdev->private->state = DEV_STATE_NOT_OPER;
1200 else
1201 cdev->private->state = DEV_STATE_OFFLINE;
1202 wake_up(&cdev->private->wait_q);
1203}
1204
1205static void
1206ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1207{
1208 int ret;
1209
1210 ret = ccw_device_cancel_halt_clear(cdev);
1211 switch (ret) {
1212 case 0:
1213 cdev->private->state = DEV_STATE_OFFLINE;
1214 wake_up(&cdev->private->wait_q);
1215 break;
1216 case -ENODEV:
1217 cdev->private->state = DEV_STATE_NOT_OPER;
1218 wake_up(&cdev->private->wait_q);
1219 break;
1220 default:
1221 ccw_device_set_timeout(cdev, HZ/10);
1222 }
1223}
1224
1225/*
1226 * No operation action. This is used e.g. to ignore a timeout event in
1227 * state offline.
1228 */
1229static void
1230ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1231{
1232}
1233
1234/*
1235 * Bug operation action.
1236 */
1237static void
1238ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1239{
1240 printk(KERN_EMERG "dev_jumptable[%i][%i] == NULL\n",
1241 cdev->private->state, dev_event);
1242 BUG();
1243}
1244
1245/*
1246 * device statemachine
1247 */
1248fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1249 [DEV_STATE_NOT_OPER] = {
1250 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1251 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1252 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1253 [DEV_EVENT_VERIFY] = ccw_device_nop,
1254 },
1255 [DEV_STATE_SENSE_PGID] = {
1256 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1257 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1258 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1259 [DEV_EVENT_VERIFY] = ccw_device_nop,
1260 },
1261 [DEV_STATE_SENSE_ID] = {
1262 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1263 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1264 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1265 [DEV_EVENT_VERIFY] = ccw_device_nop,
1266 },
1267 [DEV_STATE_OFFLINE] = {
1268 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1269 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1270 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1271 [DEV_EVENT_VERIFY] = ccw_device_nop,
1272 },
1273 [DEV_STATE_VERIFY] = {
1274 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1275 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1276 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001277 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 },
1279 [DEV_STATE_ONLINE] = {
1280 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1281 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1282 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1283 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1284 },
1285 [DEV_STATE_W4SENSE] = {
1286 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1287 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1288 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1289 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1290 },
1291 [DEV_STATE_DISBAND_PGID] = {
1292 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1293 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1294 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1295 [DEV_EVENT_VERIFY] = ccw_device_nop,
1296 },
1297 [DEV_STATE_BOXED] = {
1298 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1299 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1300 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1301 [DEV_EVENT_VERIFY] = ccw_device_nop,
1302 },
1303 /* states to wait for i/o completion before doing something */
1304 [DEV_STATE_CLEAR_VERIFY] = {
1305 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1306 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1307 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1308 [DEV_EVENT_VERIFY] = ccw_device_nop,
1309 },
1310 [DEV_STATE_TIMEOUT_KILL] = {
1311 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1312 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1313 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1314 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1315 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 [DEV_STATE_QUIESCE] = {
1317 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1318 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1319 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1320 [DEV_EVENT_VERIFY] = ccw_device_nop,
1321 },
1322 /* special states for devices gone not operational */
1323 [DEV_STATE_DISCONNECTED] = {
1324 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1325 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1326 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001327 [DEV_EVENT_VERIFY] = ccw_device_start_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 },
1329 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1330 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1331 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1332 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1333 [DEV_EVENT_VERIFY] = ccw_device_nop,
1334 },
1335 [DEV_STATE_CMFCHANGE] = {
1336 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1337 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1338 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1339 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1340 },
Cornelia Huck94bb0632006-06-29 15:08:41 +02001341 [DEV_STATE_CMFUPDATE] = {
1342 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1343 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1344 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1345 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1346 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347};
1348
1349/*
1350 * io_subchannel_irq is called for "real" interrupts or for status
1351 * pending conditions on msch.
1352 */
1353void
1354io_subchannel_irq (struct device *pdev)
1355{
1356 struct ccw_device *cdev;
1357
1358 cdev = to_subchannel(pdev)->dev.driver_data;
1359
1360 CIO_TRACE_EVENT (3, "IRQ");
1361 CIO_TRACE_EVENT (3, pdev->bus_id);
1362 if (cdev)
1363 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1364}
1365
1366EXPORT_SYMBOL_GPL(ccw_device_set_timeout);