blob: f772ef0a4f22bf551ec2db4cff362c4e62d5862c [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>
Peter Oberparleitere5854a52007-04-27 16:01:31 +020018#include <asm/chpid.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include "cio.h"
21#include "cio_debug.h"
22#include "css.h"
23#include "device.h"
24#include "chsc.h"
25#include "ioasm.h"
Peter Oberparleitere6b6e102007-04-27 16:01:28 +020026#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28int
29device_is_online(struct subchannel *sch)
30{
31 struct ccw_device *cdev;
32
33 if (!sch->dev.driver_data)
34 return 0;
35 cdev = sch->dev.driver_data;
36 return (cdev->private->state == DEV_STATE_ONLINE);
37}
38
39int
40device_is_disconnected(struct subchannel *sch)
41{
42 struct ccw_device *cdev;
43
44 if (!sch->dev.driver_data)
45 return 0;
46 cdev = sch->dev.driver_data;
47 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
48 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
49}
50
51void
52device_set_disconnected(struct subchannel *sch)
53{
54 struct ccw_device *cdev;
55
56 if (!sch->dev.driver_data)
57 return;
58 cdev = sch->dev.driver_data;
59 ccw_device_set_timeout(cdev, 0);
60 cdev->private->flags.fake_irb = 0;
61 cdev->private->state = DEV_STATE_DISCONNECTED;
62}
63
Cornelia Huckd23861f2006-12-04 15:41:04 +010064void device_set_intretry(struct subchannel *sch)
65{
66 struct ccw_device *cdev;
67
68 cdev = sch->dev.driver_data;
69 if (!cdev)
70 return;
71 cdev->private->flags.intretry = 1;
72}
73
Cornelia Huck24cb5b42006-12-04 15:41:01 +010074int device_trigger_verify(struct subchannel *sch)
75{
76 struct ccw_device *cdev;
77
78 cdev = sch->dev.driver_data;
79 if (!cdev || !cdev->online)
80 return -EINVAL;
81 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
82 return 0;
83}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/*
86 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
87 */
88static void
89ccw_device_timeout(unsigned long data)
90{
91 struct ccw_device *cdev;
92
93 cdev = (struct ccw_device *) data;
94 spin_lock_irq(cdev->ccwlock);
95 dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
96 spin_unlock_irq(cdev->ccwlock);
97}
98
99/*
100 * Set timeout
101 */
102void
103ccw_device_set_timeout(struct ccw_device *cdev, int expires)
104{
105 if (expires == 0) {
106 del_timer(&cdev->private->timer);
107 return;
108 }
109 if (timer_pending(&cdev->private->timer)) {
110 if (mod_timer(&cdev->private->timer, jiffies + expires))
111 return;
112 }
113 cdev->private->timer.function = ccw_device_timeout;
114 cdev->private->timer.data = (unsigned long) cdev;
115 cdev->private->timer.expires = jiffies + expires;
116 add_timer(&cdev->private->timer);
117}
118
119/* Kill any pending timers after machine check. */
120void
121device_kill_pending_timer(struct subchannel *sch)
122{
123 struct ccw_device *cdev;
124
125 if (!sch->dev.driver_data)
126 return;
127 cdev = sch->dev.driver_data;
128 ccw_device_set_timeout(cdev, 0);
129}
130
131/*
132 * Cancel running i/o. This is called repeatedly since halt/clear are
133 * asynchronous operations. We do one try with cio_cancel, two tries
134 * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
135 * Returns 0 if device now idle, -ENODEV for device not operational and
136 * -EBUSY if an interrupt is expected (either from halt/clear or from a
137 * status pending).
138 */
139int
140ccw_device_cancel_halt_clear(struct ccw_device *cdev)
141{
142 struct subchannel *sch;
143 int ret;
144
145 sch = to_subchannel(cdev->dev.parent);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800146 ret = stsch(sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (ret || !sch->schib.pmcw.dnv)
148 return -ENODEV;
Cornelia Huck2470b642007-03-05 23:36:02 +0100149 if (!sch->schib.pmcw.ena)
150 /* Not operational -> done. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return 0;
152 /* Stage 1: cancel io. */
153 if (!(sch->schib.scsw.actl & SCSW_ACTL_HALT_PEND) &&
154 !(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
155 ret = cio_cancel(sch);
156 if (ret != -EINVAL)
157 return ret;
158 /* cancel io unsuccessful. From now on it is asynchronous. */
159 cdev->private->iretry = 3; /* 3 halt retries. */
160 }
161 if (!(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
162 /* Stage 2: halt io. */
163 if (cdev->private->iretry) {
164 cdev->private->iretry--;
165 ret = cio_halt(sch);
Peter Oberparleiterba4ba8a2006-07-27 14:00:23 +0200166 if (ret != -EBUSY)
167 return (ret == 0) ? -EBUSY : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169 /* halt io unsuccessful. */
170 cdev->private->iretry = 255; /* 255 clear retries. */
171 }
172 /* Stage 3: clear io. */
173 if (cdev->private->iretry) {
174 cdev->private->iretry--;
175 ret = cio_clear (sch);
176 return (ret == 0) ? -EBUSY : ret;
177 }
178 panic("Can't stop i/o on subchannel.\n");
179}
180
181static int
182ccw_device_handle_oper(struct ccw_device *cdev)
183{
184 struct subchannel *sch;
185
186 sch = to_subchannel(cdev->dev.parent);
187 cdev->private->flags.recog_done = 1;
188 /*
189 * Check if cu type and device type still match. If
190 * not, it is certainly another device and we have to
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100191 * de- and re-register.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 */
193 if (cdev->id.cu_type != cdev->private->senseid.cu_type ||
194 cdev->id.cu_model != cdev->private->senseid.cu_model ||
195 cdev->id.dev_type != cdev->private->senseid.dev_type ||
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100196 cdev->id.dev_model != cdev->private->senseid.dev_model) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100198 ccw_device_do_unreg_rereg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 queue_work(ccw_device_work, &cdev->private->kick_work);
200 return 0;
201 }
202 cdev->private->flags.donotify = 1;
203 return 1;
204}
205
206/*
207 * The machine won't give us any notification by machine check if a chpid has
208 * been varied online on the SE so we have to find out by magic (i. e. driving
209 * the channel subsystem to device selection and updating our path masks).
210 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100211static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212__recover_lost_chpids(struct subchannel *sch, int old_lpm)
213{
214 int mask, i;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200215 struct chp_id chpid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200217 chp_id_init(&chpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 for (i = 0; i<8; i++) {
219 mask = 0x80 >> i;
220 if (!(sch->lpm & mask))
221 continue;
222 if (old_lpm & mask)
223 continue;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200224 chpid.id = sch->schib.pmcw.chpid[i];
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200225 if (!chp_is_registered(chpid))
226 css_schedule_eval_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228}
229
230/*
231 * Stop device recognition.
232 */
233static void
234ccw_device_recog_done(struct ccw_device *cdev, int state)
235{
236 struct subchannel *sch;
237 int notify, old_lpm, same_dev;
238
239 sch = to_subchannel(cdev->dev.parent);
240
241 ccw_device_set_timeout(cdev, 0);
242 cio_disable_subchannel(sch);
243 /*
244 * Now that we tried recognition, we have performed device selection
245 * through ssch() and the path information is up to date.
246 */
247 old_lpm = sch->lpm;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800248 stsch(sch->schid, &sch->schib);
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200249 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck4ffa9232005-07-29 14:03:37 -0700250 /* Check since device may again have become not operational. */
251 if (!sch->schib.pmcw.dnv)
252 state = DEV_STATE_NOT_OPER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
254 /* Force reprobe on all chpids. */
255 old_lpm = 0;
256 if (sch->lpm != old_lpm)
257 __recover_lost_chpids(sch, old_lpm);
258 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
259 if (state == DEV_STATE_NOT_OPER) {
260 cdev->private->flags.recog_done = 1;
261 cdev->private->state = DEV_STATE_DISCONNECTED;
262 return;
263 }
264 /* Boxed devices don't need extra treatment. */
265 }
266 notify = 0;
267 same_dev = 0; /* Keep the compiler quiet... */
268 switch (state) {
269 case DEV_STATE_NOT_OPER:
270 CIO_DEBUG(KERN_WARNING, 2,
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200271 "cio: SenseID : unknown device %04x on subchannel "
Cornelia Huck78964262006-10-11 15:31:38 +0200272 "0.%x.%04x\n", cdev->private->dev_id.devno,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800273 sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 break;
275 case DEV_STATE_OFFLINE:
276 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
277 same_dev = ccw_device_handle_oper(cdev);
278 notify = 1;
279 }
280 /* fill out sense information */
Heiko Carstens81388d22006-09-20 15:59:17 +0200281 memset(&cdev->id, 0, sizeof(cdev->id));
Heiko Carstens292888c2006-08-30 14:33:35 +0200282 cdev->id.cu_type = cdev->private->senseid.cu_type;
283 cdev->id.cu_model = cdev->private->senseid.cu_model;
284 cdev->id.dev_type = cdev->private->senseid.dev_type;
285 cdev->id.dev_model = cdev->private->senseid.dev_model;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (notify) {
287 cdev->private->state = DEV_STATE_OFFLINE;
288 if (same_dev) {
289 /* Get device online again. */
290 ccw_device_online(cdev);
291 wake_up(&cdev->private->wait_q);
292 }
293 return;
294 }
295 /* Issue device info message. */
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200296 CIO_DEBUG(KERN_INFO, 2,
297 "cio: SenseID : device 0.%x.%04x reports: "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800299 "%04X/%02X\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200300 cdev->private->dev_id.ssid,
301 cdev->private->dev_id.devno,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 cdev->id.cu_type, cdev->id.cu_model,
303 cdev->id.dev_type, cdev->id.dev_model);
304 break;
305 case DEV_STATE_BOXED:
306 CIO_DEBUG(KERN_WARNING, 2,
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200307 "cio: SenseID : boxed device %04x on subchannel "
Cornelia Huck78964262006-10-11 15:31:38 +0200308 "0.%x.%04x\n", cdev->private->dev_id.devno,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800309 sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 break;
311 }
312 cdev->private->state = state;
313 io_subchannel_recog_done(cdev);
314 if (state != DEV_STATE_NOT_OPER)
315 wake_up(&cdev->private->wait_q);
316}
317
318/*
319 * Function called from device_id.c after sense id has completed.
320 */
321void
322ccw_device_sense_id_done(struct ccw_device *cdev, int err)
323{
324 switch (err) {
325 case 0:
326 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
327 break;
328 case -ETIME: /* Sense id stopped by timeout. */
329 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
330 break;
331 default:
332 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
333 break;
334 }
335}
336
337static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100338ccw_device_oper_notify(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100340 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 struct ccw_device *cdev;
342 struct subchannel *sch;
343 int ret;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100344 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100346 priv = container_of(work, struct ccw_device_private, kick_work);
347 cdev = priv->cdev;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100348 spin_lock_irqsave(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100350 if (sch->driver && sch->driver->notify) {
351 spin_unlock_irqrestore(cdev->ccwlock, flags);
352 ret = sch->driver->notify(&sch->dev, CIO_OPER);
353 spin_lock_irqsave(cdev->ccwlock, flags);
354 } else
355 ret = 0;
356 if (ret) {
357 /* Reenable channel measurements, if needed. */
358 spin_unlock_irqrestore(cdev->ccwlock, flags);
359 cmf_reenable(cdev);
360 spin_lock_irqsave(cdev->ccwlock, flags);
361 wake_up(&cdev->private->wait_q);
362 }
363 spin_unlock_irqrestore(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (!ret)
365 /* Driver doesn't want device back. */
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100366 ccw_device_do_unreg_rereg(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
369/*
370 * Finished with online/offline processing.
371 */
372static void
373ccw_device_done(struct ccw_device *cdev, int state)
374{
375 struct subchannel *sch;
376
377 sch = to_subchannel(cdev->dev.parent);
378
Cornelia Huckf1ee3282006-10-04 20:02:02 +0200379 ccw_device_set_timeout(cdev, 0);
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (state != DEV_STATE_ONLINE)
382 cio_disable_subchannel(sch);
383
384 /* Reset device status. */
385 memset(&cdev->private->irb, 0, sizeof(struct irb));
386
387 cdev->private->state = state;
388
389
390 if (state == DEV_STATE_BOXED)
391 CIO_DEBUG(KERN_WARNING, 2,
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200392 "cio: Boxed device %04x on subchannel %04x\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200393 cdev->private->dev_id.devno, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 if (cdev->private->flags.donotify) {
396 cdev->private->flags.donotify = 0;
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100397 PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
399 }
400 wake_up(&cdev->private->wait_q);
401
402 if (css_init_done && state != DEV_STATE_ONLINE)
403 put_device (&cdev->dev);
404}
405
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100406static int cmp_pgid(struct pgid *p1, struct pgid *p2)
Cornelia Huck7e560812006-07-12 16:40:19 +0200407{
408 char *c1;
409 char *c2;
410
411 c1 = (char *)p1;
412 c2 = (char *)p2;
413
414 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
415}
416
417static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
418{
419 int i;
420 int last;
421
422 last = 0;
423 for (i = 0; i < 8; i++) {
424 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
425 /* No PGID yet */
426 continue;
427 if (cdev->private->pgid[last].inf.ps.state1 ==
428 SNID_STATE1_RESET) {
429 /* First non-zero PGID */
430 last = i;
431 continue;
432 }
433 if (cmp_pgid(&cdev->private->pgid[i],
434 &cdev->private->pgid[last]) == 0)
435 /* Non-conflicting PGIDs */
436 continue;
437
438 /* PGID mismatch, can't pathgroup. */
439 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
440 "0.%x.%04x, can't pathgroup\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200441 cdev->private->dev_id.ssid,
442 cdev->private->dev_id.devno);
Cornelia Huck7e560812006-07-12 16:40:19 +0200443 cdev->private->options.pgroup = 0;
444 return;
445 }
446 if (cdev->private->pgid[last].inf.ps.state1 ==
447 SNID_STATE1_RESET)
448 /* No previous pgid found */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200449 memcpy(&cdev->private->pgid[0],
450 &channel_subsystems[0]->global_pgid,
Cornelia Huck7e560812006-07-12 16:40:19 +0200451 sizeof(struct pgid));
452 else
453 /* Use existing pgid */
454 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
455 sizeof(struct pgid));
456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/*
459 * Function called from device_pgid.c after sense path ground has completed.
460 */
461void
462ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
463{
464 struct subchannel *sch;
465
466 sch = to_subchannel(cdev->dev.parent);
467 switch (err) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200468 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
469 cdev->private->options.pgroup = 0;
470 break;
471 case 0: /* success */
472 case -EACCES: /* partial success, some paths not operational */
473 /* Check if all pgids are equal or 0. */
474 __ccw_device_get_common_pgid(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 break;
476 case -ETIME: /* Sense path group id stopped by timeout. */
477 case -EUSERS: /* device is reserved for someone else. */
478 ccw_device_done(cdev, DEV_STATE_BOXED);
Cornelia Huck7e560812006-07-12 16:40:19 +0200479 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 default:
481 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Cornelia Huck7e560812006-07-12 16:40:19 +0200482 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
Cornelia Huck7e560812006-07-12 16:40:19 +0200484 /* Start Path Group verification. */
Cornelia Huck7e560812006-07-12 16:40:19 +0200485 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200486 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200487 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
490/*
491 * Start device recognition.
492 */
493int
494ccw_device_recognition(struct ccw_device *cdev)
495{
496 struct subchannel *sch;
497 int ret;
498
499 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
500 (cdev->private->state != DEV_STATE_BOXED))
501 return -EINVAL;
502 sch = to_subchannel(cdev->dev.parent);
503 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
504 if (ret != 0)
505 /* Couldn't enable the subchannel for i/o. Sick device. */
506 return ret;
507
508 /* After 60s the device recognition is considered to have failed. */
509 ccw_device_set_timeout(cdev, 60*HZ);
510
511 /*
512 * We used to start here with a sense pgid to find out whether a device
513 * is locked by someone else. Unfortunately, the sense pgid command
514 * code has other meanings on devices predating the path grouping
515 * algorithm, so we start with sense id and box the device after an
516 * timeout (or if sense pgid during path verification detects the device
517 * is locked, as may happen on newer devices).
518 */
519 cdev->private->flags.recog_done = 0;
520 cdev->private->state = DEV_STATE_SENSE_ID;
521 ccw_device_sense_id_start(cdev);
522 return 0;
523}
524
525/*
526 * Handle timeout in device recognition.
527 */
528static void
529ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
530{
531 int ret;
532
533 ret = ccw_device_cancel_halt_clear(cdev);
534 switch (ret) {
535 case 0:
536 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
537 break;
538 case -ENODEV:
539 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
540 break;
541 default:
542 ccw_device_set_timeout(cdev, 3*HZ);
543 }
544}
545
546
547static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100548ccw_device_nopath_notify(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100550 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct ccw_device *cdev;
552 struct subchannel *sch;
553 int ret;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100554 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100556 priv = container_of(work, struct ccw_device_private, kick_work);
557 cdev = priv->cdev;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100558 spin_lock_irqsave(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 sch = to_subchannel(cdev->dev.parent);
560 /* Extra sanity. */
561 if (sch->lpm)
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100562 goto out_unlock;
563 if (sch->driver && sch->driver->notify) {
564 spin_unlock_irqrestore(cdev->ccwlock, flags);
565 ret = sch->driver->notify(&sch->dev, CIO_NO_PATH);
566 spin_lock_irqsave(cdev->ccwlock, flags);
567 } else
568 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (!ret) {
570 if (get_device(&sch->dev)) {
571 /* Driver doesn't want to keep device. */
572 cio_disable_subchannel(sch);
573 if (get_device(&cdev->dev)) {
574 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100575 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 queue_work(ccw_device_work,
577 &cdev->private->kick_work);
578 } else
579 put_device(&sch->dev);
580 }
581 } else {
582 cio_disable_subchannel(sch);
583 ccw_device_set_timeout(cdev, 0);
584 cdev->private->flags.fake_irb = 0;
585 cdev->private->state = DEV_STATE_DISCONNECTED;
586 wake_up(&cdev->private->wait_q);
587 }
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100588out_unlock:
589 spin_unlock_irqrestore(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
592void
593ccw_device_verify_done(struct ccw_device *cdev, int err)
594{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200595 struct subchannel *sch;
596
597 sch = to_subchannel(cdev->dev.parent);
598 /* Update schib - pom may have changed. */
599 stsch(sch->schid, &sch->schib);
600 /* Update lpm with verified path mask. */
601 sch->lpm = sch->vpm;
602 /* Repeat path verification? */
603 if (cdev->private->flags.doverify) {
604 cdev->private->flags.doverify = 0;
605 ccw_device_verify_start(cdev);
606 return;
607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 switch (err) {
609 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
610 cdev->private->options.pgroup = 0;
611 case 0:
612 ccw_device_done(cdev, DEV_STATE_ONLINE);
613 /* Deliver fake irb to device driver, if needed. */
614 if (cdev->private->flags.fake_irb) {
615 memset(&cdev->private->irb, 0, sizeof(struct irb));
Heiko Carstens292888c2006-08-30 14:33:35 +0200616 cdev->private->irb.scsw.cc = 1;
617 cdev->private->irb.scsw.fctl = SCSW_FCTL_START_FUNC;
618 cdev->private->irb.scsw.actl = SCSW_ACTL_START_PEND;
619 cdev->private->irb.scsw.stctl = SCSW_STCTL_STATUS_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 cdev->private->flags.fake_irb = 0;
621 if (cdev->handler)
622 cdev->handler(cdev, cdev->private->intparm,
623 &cdev->private->irb);
624 memset(&cdev->private->irb, 0, sizeof(struct irb));
625 }
626 break;
627 case -ETIME:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200628 /* Reset oper notify indication after verify error. */
629 cdev->private->flags.donotify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 ccw_device_done(cdev, DEV_STATE_BOXED);
631 break;
632 default:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200633 /* Reset oper notify indication after verify error. */
634 cdev->private->flags.donotify = 0;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100635 if (cdev->online) {
636 PREPARE_WORK(&cdev->private->kick_work,
637 ccw_device_nopath_notify);
638 queue_work(ccw_device_notify_work,
639 &cdev->private->kick_work);
640 } else
641 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 break;
643 }
644}
645
646/*
647 * Get device online.
648 */
649int
650ccw_device_online(struct ccw_device *cdev)
651{
652 struct subchannel *sch;
653 int ret;
654
655 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
656 (cdev->private->state != DEV_STATE_BOXED))
657 return -EINVAL;
658 sch = to_subchannel(cdev->dev.parent);
659 if (css_init_done && !get_device(&cdev->dev))
660 return -ENODEV;
661 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
662 if (ret != 0) {
663 /* Couldn't enable the subchannel for i/o. Sick device. */
664 if (ret == -ENODEV)
665 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
666 return ret;
667 }
668 /* Do we want to do path grouping? */
669 if (!cdev->private->options.pgroup) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200670 /* Start initial path verification. */
671 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200672 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200673 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return 0;
675 }
676 /* Do a SensePGID first. */
677 cdev->private->state = DEV_STATE_SENSE_PGID;
678 ccw_device_sense_pgid_start(cdev);
679 return 0;
680}
681
682void
683ccw_device_disband_done(struct ccw_device *cdev, int err)
684{
685 switch (err) {
686 case 0:
687 ccw_device_done(cdev, DEV_STATE_OFFLINE);
688 break;
689 case -ETIME:
690 ccw_device_done(cdev, DEV_STATE_BOXED);
691 break;
692 default:
Peter Oberparleiter3ecb0a52007-05-31 17:38:07 +0200693 cdev->private->flags.donotify = 0;
694 if (get_device(&cdev->dev)) {
695 PREPARE_WORK(&cdev->private->kick_work,
696 ccw_device_call_sch_unregister);
697 queue_work(ccw_device_work, &cdev->private->kick_work);
698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
700 break;
701 }
702}
703
704/*
705 * Shutdown device.
706 */
707int
708ccw_device_offline(struct ccw_device *cdev)
709{
710 struct subchannel *sch;
711
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100712 if (ccw_device_is_orphan(cdev)) {
713 ccw_device_done(cdev, DEV_STATE_OFFLINE);
714 return 0;
715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 sch = to_subchannel(cdev->dev.parent);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800717 if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return -ENODEV;
719 if (cdev->private->state != DEV_STATE_ONLINE) {
720 if (sch->schib.scsw.actl != 0)
721 return -EBUSY;
722 return -EINVAL;
723 }
724 if (sch->schib.scsw.actl != 0)
725 return -EBUSY;
726 /* Are we doing path grouping? */
727 if (!cdev->private->options.pgroup) {
728 /* No, set state offline immediately. */
729 ccw_device_done(cdev, DEV_STATE_OFFLINE);
730 return 0;
731 }
732 /* Start Set Path Group commands. */
733 cdev->private->state = DEV_STATE_DISBAND_PGID;
734 ccw_device_disband_start(cdev);
735 return 0;
736}
737
738/*
739 * Handle timeout in device online/offline process.
740 */
741static void
742ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
743{
744 int ret;
745
746 ret = ccw_device_cancel_halt_clear(cdev);
747 switch (ret) {
748 case 0:
749 ccw_device_done(cdev, DEV_STATE_BOXED);
750 break;
751 case -ENODEV:
752 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
753 break;
754 default:
755 ccw_device_set_timeout(cdev, 3*HZ);
756 }
757}
758
759/*
760 * Handle not oper event in device recognition.
761 */
762static void
763ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
764{
765 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
766}
767
768/*
769 * Handle not operational event while offline.
770 */
771static void
772ccw_device_offline_notoper(struct ccw_device *cdev, enum dev_event dev_event)
773{
774 struct subchannel *sch;
775
776 cdev->private->state = DEV_STATE_NOT_OPER;
777 sch = to_subchannel(cdev->dev.parent);
778 if (get_device(&cdev->dev)) {
779 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100780 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 queue_work(ccw_device_work, &cdev->private->kick_work);
782 }
783 wake_up(&cdev->private->wait_q);
784}
785
786/*
787 * Handle not operational event while online.
788 */
789static void
790ccw_device_online_notoper(struct ccw_device *cdev, enum dev_event dev_event)
791{
792 struct subchannel *sch;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100793 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100796 if (sch->driver->notify) {
797 spin_unlock_irq(cdev->ccwlock);
798 ret = sch->driver->notify(&sch->dev,
799 sch->lpm ? CIO_GONE : CIO_NO_PATH);
800 spin_lock_irq(cdev->ccwlock);
801 } else
802 ret = 0;
803 if (ret) {
804 ccw_device_set_timeout(cdev, 0);
805 cdev->private->flags.fake_irb = 0;
806 cdev->private->state = DEV_STATE_DISCONNECTED;
807 wake_up(&cdev->private->wait_q);
808 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810 cdev->private->state = DEV_STATE_NOT_OPER;
811 cio_disable_subchannel(sch);
812 if (sch->schib.scsw.actl != 0) {
813 // FIXME: not-oper indication to device driver ?
814 ccw_device_call_handler(cdev);
815 }
816 if (get_device(&cdev->dev)) {
817 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100818 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 queue_work(ccw_device_work, &cdev->private->kick_work);
820 }
821 wake_up(&cdev->private->wait_q);
822}
823
824/*
825 * Handle path verification event.
826 */
827static void
828ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
829{
830 struct subchannel *sch;
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (cdev->private->state == DEV_STATE_W4SENSE) {
833 cdev->private->flags.doverify = 1;
834 return;
835 }
836 sch = to_subchannel(cdev->dev.parent);
837 /*
838 * Since we might not just be coming from an interrupt from the
839 * subchannel we have to update the schib.
840 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800841 stsch(sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 if (sch->schib.scsw.actl != 0 ||
Peter Oberparleiter65200c22006-08-07 17:00:33 +0200844 (sch->schib.scsw.stctl & SCSW_STCTL_STATUS_PEND) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
846 /*
847 * No final status yet or final status not yet delivered
848 * to the device driver. Can't do path verfication now,
849 * delay until final status was delivered.
850 */
851 cdev->private->flags.doverify = 1;
852 return;
853 }
854 /* Device is idle, we can do the path verification. */
855 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200856 cdev->private->flags.doverify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 ccw_device_verify_start(cdev);
858}
859
860/*
861 * Got an interrupt for a normal io (state online).
862 */
863static void
864ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
865{
866 struct irb *irb;
867
868 irb = (struct irb *) __LC_IRB;
869 /* Check for unsolicited interrupt. */
870 if ((irb->scsw.stctl ==
871 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
872 && (!irb->scsw.cc)) {
873 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
874 !irb->esw.esw0.erw.cons) {
875 /* Unit check but no sense data. Need basic sense. */
876 if (ccw_device_do_sense(cdev, irb) != 0)
877 goto call_handler_unsol;
Cornelia Hucke0ec5742006-06-04 02:51:27 -0700878 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 cdev->private->state = DEV_STATE_W4SENSE;
880 cdev->private->intparm = 0;
881 return;
882 }
883call_handler_unsol:
884 if (cdev->handler)
885 cdev->handler (cdev, 0, irb);
Cornelia Huck18374d32007-02-05 21:17:09 +0100886 if (cdev->private->flags.doverify)
887 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return;
889 }
890 /* Accumulate status and find out if a basic sense is needed. */
891 ccw_device_accumulate_irb(cdev, irb);
892 if (cdev->private->flags.dosense) {
893 if (ccw_device_do_sense(cdev, irb) == 0) {
894 cdev->private->state = DEV_STATE_W4SENSE;
895 }
896 return;
897 }
898 /* Call the handler. */
899 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
900 /* Start delayed path verification. */
901 ccw_device_online_verify(cdev, 0);
902}
903
904/*
905 * Got an timeout in online state.
906 */
907static void
908ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
909{
910 int ret;
911
912 ccw_device_set_timeout(cdev, 0);
913 ret = ccw_device_cancel_halt_clear(cdev);
914 if (ret == -EBUSY) {
915 ccw_device_set_timeout(cdev, 3*HZ);
916 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
917 return;
918 }
919 if (ret == -ENODEV) {
920 struct subchannel *sch;
921
922 sch = to_subchannel(cdev->dev.parent);
923 if (!sch->lpm) {
924 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100925 ccw_device_nopath_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 queue_work(ccw_device_notify_work,
927 &cdev->private->kick_work);
928 } else
929 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
930 } else if (cdev->handler)
931 cdev->handler(cdev, cdev->private->intparm,
932 ERR_PTR(-ETIMEDOUT));
933}
934
935/*
936 * Got an interrupt for a basic sense.
937 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100938static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
940{
941 struct irb *irb;
942
943 irb = (struct irb *) __LC_IRB;
944 /* Check for unsolicited interrupt. */
945 if (irb->scsw.stctl ==
946 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
947 if (irb->scsw.cc == 1)
948 /* Basic sense hasn't started. Try again. */
949 ccw_device_do_sense(cdev, irb);
950 else {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200951 CIO_MSG_EVENT(2, "Huh? 0.%x.%04x: unsolicited "
952 "interrupt during w4sense...\n",
953 cdev->private->dev_id.ssid,
954 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (cdev->handler)
956 cdev->handler (cdev, 0, irb);
957 }
958 return;
959 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800960 /*
961 * Check if a halt or clear has been issued in the meanwhile. If yes,
962 * only deliver the halt/clear interrupt to the device driver as if it
963 * had killed the original request.
964 */
965 if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
Cornelia Huckd23861f2006-12-04 15:41:04 +0100966 /* Retry Basic Sense if requested. */
967 if (cdev->private->flags.intretry) {
968 cdev->private->flags.intretry = 0;
969 ccw_device_do_sense(cdev, irb);
970 return;
971 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800972 cdev->private->flags.dosense = 0;
973 memset(&cdev->private->irb, 0, sizeof(struct irb));
974 ccw_device_accumulate_irb(cdev, irb);
975 goto call_handler;
976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 /* Add basic sense info to irb. */
978 ccw_device_accumulate_basic_sense(cdev, irb);
979 if (cdev->private->flags.dosense) {
980 /* Another basic sense is needed. */
981 ccw_device_do_sense(cdev, irb);
982 return;
983 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800984call_handler:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 cdev->private->state = DEV_STATE_ONLINE;
986 /* Call the handler. */
987 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
988 /* Start delayed path verification. */
989 ccw_device_online_verify(cdev, 0);
990}
991
992static void
993ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
994{
995 struct irb *irb;
996
997 irb = (struct irb *) __LC_IRB;
998 /* Accumulate status. We don't do basic sense. */
999 ccw_device_accumulate_irb(cdev, irb);
Cornelia Huckb4f7b1e2006-06-29 15:03:35 +02001000 /* Remember to clear irb to avoid residuals. */
1001 memset(&cdev->private->irb, 0, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 /* Try to start delayed device verification. */
1003 ccw_device_online_verify(cdev, 0);
1004 /* Note: Don't call handler for cio initiated clear! */
1005}
1006
1007static void
1008ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
1009{
1010 struct subchannel *sch;
1011
1012 sch = to_subchannel(cdev->dev.parent);
1013 ccw_device_set_timeout(cdev, 0);
Cornelia Huck7c8427c2007-03-05 23:35:59 +01001014 /* Start delayed path verification. */
1015 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 /* OK, i/o is dead now. Call interrupt handler. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (cdev->handler)
1018 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +02001019 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
1022static void
1023ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1024{
1025 int ret;
1026
1027 ret = ccw_device_cancel_halt_clear(cdev);
1028 if (ret == -EBUSY) {
1029 ccw_device_set_timeout(cdev, 3*HZ);
1030 return;
1031 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +01001032 /* Start delayed path verification. */
1033 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (cdev->handler)
1035 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +02001036 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
Cornelia Hucke7769b42006-10-11 15:31:41 +02001039void device_kill_io(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 int ret;
Cornelia Hucke7769b42006-10-11 15:31:41 +02001042 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Cornelia Hucke7769b42006-10-11 15:31:41 +02001044 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 ret = ccw_device_cancel_halt_clear(cdev);
1046 if (ret == -EBUSY) {
1047 ccw_device_set_timeout(cdev, 3*HZ);
1048 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
1049 return;
1050 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +01001051 /* Start delayed path verification. */
1052 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (cdev->handler)
1054 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +02001055 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
1058static void
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001059ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001061 /* Start verification after current task finished. */
Cornelia Huck7e560812006-07-12 16:40:19 +02001062 cdev->private->flags.doverify = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
1065static void
1066ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
1067{
1068 struct irb *irb;
1069
1070 switch (dev_event) {
1071 case DEV_EVENT_INTERRUPT:
1072 irb = (struct irb *) __LC_IRB;
1073 /* Check for unsolicited interrupt. */
1074 if ((irb->scsw.stctl ==
1075 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
1076 (!irb->scsw.cc))
1077 /* FIXME: we should restart stlck here, but this
1078 * is extremely unlikely ... */
1079 goto out_wakeup;
1080
1081 ccw_device_accumulate_irb(cdev, irb);
1082 /* We don't care about basic sense etc. */
1083 break;
1084 default: /* timeout */
1085 break;
1086 }
1087out_wakeup:
1088 wake_up(&cdev->private->wait_q);
1089}
1090
1091static void
1092ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1093{
1094 struct subchannel *sch;
1095
1096 sch = to_subchannel(cdev->dev.parent);
1097 if (cio_enable_subchannel(sch, sch->schib.pmcw.isc) != 0)
1098 /* Couldn't enable the subchannel for i/o. Sick device. */
1099 return;
1100
1101 /* After 60s the device recognition is considered to have failed. */
1102 ccw_device_set_timeout(cdev, 60*HZ);
1103
1104 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1105 ccw_device_sense_id_start(cdev);
1106}
1107
1108void
1109device_trigger_reprobe(struct subchannel *sch)
1110{
1111 struct ccw_device *cdev;
1112
1113 if (!sch->dev.driver_data)
1114 return;
1115 cdev = sch->dev.driver_data;
1116 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1117 return;
1118
1119 /* Update some values. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001120 if (stsch(sch->schid, &sch->schib))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return;
Cornelia Huck7674da72006-12-08 15:54:21 +01001122 if (!sch->schib.pmcw.dnv)
1123 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 /*
1125 * The pim, pam, pom values may not be accurate, but they are the best
1126 * we have before performing device selection :/
1127 */
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001128 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /* Re-set some bits in the pmcw that were lost. */
1130 sch->schib.pmcw.isc = 3;
1131 sch->schib.pmcw.csense = 1;
1132 sch->schib.pmcw.ena = 0;
1133 if ((sch->lpm & (sch->lpm - 1)) != 0)
1134 sch->schib.pmcw.mp = 1;
1135 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
1136 /* We should also udate ssd info, but this has to wait. */
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001137 /* Check if this is another device which appeared on the same sch. */
1138 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1139 PREPARE_WORK(&cdev->private->kick_work,
1140 ccw_device_move_to_orphanage);
1141 queue_work(ccw_device_work, &cdev->private->kick_work);
1142 } else
1143 ccw_device_start_id(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
1146static void
1147ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1148{
1149 struct subchannel *sch;
1150
1151 sch = to_subchannel(cdev->dev.parent);
1152 /*
1153 * An interrupt in state offline means a previous disable was not
1154 * successful. Try again.
1155 */
1156 cio_disable_subchannel(sch);
1157}
1158
1159static void
1160ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1161{
1162 retry_set_schib(cdev);
1163 cdev->private->state = DEV_STATE_ONLINE;
1164 dev_fsm_event(cdev, dev_event);
1165}
1166
Cornelia Huck94bb0632006-06-29 15:08:41 +02001167static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1168 enum dev_event dev_event)
1169{
1170 cmf_retry_copy_block(cdev);
1171 cdev->private->state = DEV_STATE_ONLINE;
1172 dev_fsm_event(cdev, dev_event);
1173}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175static void
1176ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1177{
1178 ccw_device_set_timeout(cdev, 0);
1179 if (dev_event == DEV_EVENT_NOTOPER)
1180 cdev->private->state = DEV_STATE_NOT_OPER;
1181 else
1182 cdev->private->state = DEV_STATE_OFFLINE;
1183 wake_up(&cdev->private->wait_q);
1184}
1185
1186static void
1187ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1188{
1189 int ret;
1190
1191 ret = ccw_device_cancel_halt_clear(cdev);
1192 switch (ret) {
1193 case 0:
1194 cdev->private->state = DEV_STATE_OFFLINE;
1195 wake_up(&cdev->private->wait_q);
1196 break;
1197 case -ENODEV:
1198 cdev->private->state = DEV_STATE_NOT_OPER;
1199 wake_up(&cdev->private->wait_q);
1200 break;
1201 default:
1202 ccw_device_set_timeout(cdev, HZ/10);
1203 }
1204}
1205
1206/*
1207 * No operation action. This is used e.g. to ignore a timeout event in
1208 * state offline.
1209 */
1210static void
1211ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1212{
1213}
1214
1215/*
1216 * Bug operation action.
1217 */
1218static void
1219ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1220{
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001221 CIO_MSG_EVENT(0, "dev_jumptable[%i][%i] == NULL\n",
1222 cdev->private->state, dev_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 BUG();
1224}
1225
1226/*
1227 * device statemachine
1228 */
1229fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1230 [DEV_STATE_NOT_OPER] = {
1231 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1232 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1233 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1234 [DEV_EVENT_VERIFY] = ccw_device_nop,
1235 },
1236 [DEV_STATE_SENSE_PGID] = {
1237 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1238 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1239 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1240 [DEV_EVENT_VERIFY] = ccw_device_nop,
1241 },
1242 [DEV_STATE_SENSE_ID] = {
1243 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1244 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1245 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1246 [DEV_EVENT_VERIFY] = ccw_device_nop,
1247 },
1248 [DEV_STATE_OFFLINE] = {
1249 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1250 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1251 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1252 [DEV_EVENT_VERIFY] = ccw_device_nop,
1253 },
1254 [DEV_STATE_VERIFY] = {
1255 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1256 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1257 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001258 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 },
1260 [DEV_STATE_ONLINE] = {
1261 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1262 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1263 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1264 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1265 },
1266 [DEV_STATE_W4SENSE] = {
1267 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1268 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1269 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1270 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1271 },
1272 [DEV_STATE_DISBAND_PGID] = {
1273 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1274 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1275 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1276 [DEV_EVENT_VERIFY] = ccw_device_nop,
1277 },
1278 [DEV_STATE_BOXED] = {
1279 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1280 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1281 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1282 [DEV_EVENT_VERIFY] = ccw_device_nop,
1283 },
1284 /* states to wait for i/o completion before doing something */
1285 [DEV_STATE_CLEAR_VERIFY] = {
1286 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1287 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1288 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1289 [DEV_EVENT_VERIFY] = ccw_device_nop,
1290 },
1291 [DEV_STATE_TIMEOUT_KILL] = {
1292 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1293 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1294 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1295 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1296 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 [DEV_STATE_QUIESCE] = {
1298 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1299 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1300 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1301 [DEV_EVENT_VERIFY] = ccw_device_nop,
1302 },
1303 /* special states for devices gone not operational */
1304 [DEV_STATE_DISCONNECTED] = {
1305 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1306 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1307 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001308 [DEV_EVENT_VERIFY] = ccw_device_start_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 },
1310 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1311 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1312 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1313 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1314 [DEV_EVENT_VERIFY] = ccw_device_nop,
1315 },
1316 [DEV_STATE_CMFCHANGE] = {
1317 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1318 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1319 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1320 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1321 },
Cornelia Huck94bb0632006-06-29 15:08:41 +02001322 [DEV_STATE_CMFUPDATE] = {
1323 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1324 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1325 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1326 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1327 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328};
1329
1330/*
1331 * io_subchannel_irq is called for "real" interrupts or for status
1332 * pending conditions on msch.
1333 */
1334void
1335io_subchannel_irq (struct device *pdev)
1336{
1337 struct ccw_device *cdev;
1338
1339 cdev = to_subchannel(pdev)->dev.driver_data;
1340
1341 CIO_TRACE_EVENT (3, "IRQ");
1342 CIO_TRACE_EVENT (3, pdev->bus_id);
1343 if (cdev)
1344 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1345}
1346
1347EXPORT_SYMBOL_GPL(ccw_device_set_timeout);