blob: 8633dc537695db3a99a03d35e462133fca0fa829 [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 Huckd7b5a4c92006-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 Huckd7b5a4c92006-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 */
449 memcpy(&cdev->private->pgid[0], &css[0]->global_pgid,
450 sizeof(struct pgid));
451 else
452 /* Use existing pgid */
453 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
454 sizeof(struct pgid));
455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/*
458 * Function called from device_pgid.c after sense path ground has completed.
459 */
460void
461ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
462{
463 struct subchannel *sch;
464
465 sch = to_subchannel(cdev->dev.parent);
466 switch (err) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200467 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
468 cdev->private->options.pgroup = 0;
469 break;
470 case 0: /* success */
471 case -EACCES: /* partial success, some paths not operational */
472 /* Check if all pgids are equal or 0. */
473 __ccw_device_get_common_pgid(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 break;
475 case -ETIME: /* Sense path group id stopped by timeout. */
476 case -EUSERS: /* device is reserved for someone else. */
477 ccw_device_done(cdev, DEV_STATE_BOXED);
Cornelia Huck7e560812006-07-12 16:40:19 +0200478 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 default:
480 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Cornelia Huck7e560812006-07-12 16:40:19 +0200481 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
Cornelia Huck7e560812006-07-12 16:40:19 +0200483 /* Start Path Group verification. */
Cornelia Huck7e560812006-07-12 16:40:19 +0200484 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200485 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200486 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
489/*
490 * Start device recognition.
491 */
492int
493ccw_device_recognition(struct ccw_device *cdev)
494{
495 struct subchannel *sch;
496 int ret;
497
498 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
499 (cdev->private->state != DEV_STATE_BOXED))
500 return -EINVAL;
501 sch = to_subchannel(cdev->dev.parent);
502 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
503 if (ret != 0)
504 /* Couldn't enable the subchannel for i/o. Sick device. */
505 return ret;
506
507 /* After 60s the device recognition is considered to have failed. */
508 ccw_device_set_timeout(cdev, 60*HZ);
509
510 /*
511 * We used to start here with a sense pgid to find out whether a device
512 * is locked by someone else. Unfortunately, the sense pgid command
513 * code has other meanings on devices predating the path grouping
514 * algorithm, so we start with sense id and box the device after an
515 * timeout (or if sense pgid during path verification detects the device
516 * is locked, as may happen on newer devices).
517 */
518 cdev->private->flags.recog_done = 0;
519 cdev->private->state = DEV_STATE_SENSE_ID;
520 ccw_device_sense_id_start(cdev);
521 return 0;
522}
523
524/*
525 * Handle timeout in device recognition.
526 */
527static void
528ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
529{
530 int ret;
531
532 ret = ccw_device_cancel_halt_clear(cdev);
533 switch (ret) {
534 case 0:
535 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
536 break;
537 case -ENODEV:
538 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
539 break;
540 default:
541 ccw_device_set_timeout(cdev, 3*HZ);
542 }
543}
544
545
546static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100547ccw_device_nopath_notify(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100549 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 struct ccw_device *cdev;
551 struct subchannel *sch;
552 int ret;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100553 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100555 priv = container_of(work, struct ccw_device_private, kick_work);
556 cdev = priv->cdev;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100557 spin_lock_irqsave(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 sch = to_subchannel(cdev->dev.parent);
559 /* Extra sanity. */
560 if (sch->lpm)
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100561 goto out_unlock;
562 if (sch->driver && sch->driver->notify) {
563 spin_unlock_irqrestore(cdev->ccwlock, flags);
564 ret = sch->driver->notify(&sch->dev, CIO_NO_PATH);
565 spin_lock_irqsave(cdev->ccwlock, flags);
566 } else
567 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (!ret) {
569 if (get_device(&sch->dev)) {
570 /* Driver doesn't want to keep device. */
571 cio_disable_subchannel(sch);
572 if (get_device(&cdev->dev)) {
573 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100574 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 queue_work(ccw_device_work,
576 &cdev->private->kick_work);
577 } else
578 put_device(&sch->dev);
579 }
580 } else {
581 cio_disable_subchannel(sch);
582 ccw_device_set_timeout(cdev, 0);
583 cdev->private->flags.fake_irb = 0;
584 cdev->private->state = DEV_STATE_DISCONNECTED;
585 wake_up(&cdev->private->wait_q);
586 }
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100587out_unlock:
588 spin_unlock_irqrestore(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
591void
592ccw_device_verify_done(struct ccw_device *cdev, int err)
593{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200594 struct subchannel *sch;
595
596 sch = to_subchannel(cdev->dev.parent);
597 /* Update schib - pom may have changed. */
598 stsch(sch->schid, &sch->schib);
599 /* Update lpm with verified path mask. */
600 sch->lpm = sch->vpm;
601 /* Repeat path verification? */
602 if (cdev->private->flags.doverify) {
603 cdev->private->flags.doverify = 0;
604 ccw_device_verify_start(cdev);
605 return;
606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 switch (err) {
608 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
609 cdev->private->options.pgroup = 0;
610 case 0:
611 ccw_device_done(cdev, DEV_STATE_ONLINE);
612 /* Deliver fake irb to device driver, if needed. */
613 if (cdev->private->flags.fake_irb) {
614 memset(&cdev->private->irb, 0, sizeof(struct irb));
Heiko Carstens292888c2006-08-30 14:33:35 +0200615 cdev->private->irb.scsw.cc = 1;
616 cdev->private->irb.scsw.fctl = SCSW_FCTL_START_FUNC;
617 cdev->private->irb.scsw.actl = SCSW_ACTL_START_PEND;
618 cdev->private->irb.scsw.stctl = SCSW_STCTL_STATUS_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 cdev->private->flags.fake_irb = 0;
620 if (cdev->handler)
621 cdev->handler(cdev, cdev->private->intparm,
622 &cdev->private->irb);
623 memset(&cdev->private->irb, 0, sizeof(struct irb));
624 }
625 break;
626 case -ETIME:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200627 /* Reset oper notify indication after verify error. */
628 cdev->private->flags.donotify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 ccw_device_done(cdev, DEV_STATE_BOXED);
630 break;
631 default:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200632 /* Reset oper notify indication after verify error. */
633 cdev->private->flags.donotify = 0;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100634 if (cdev->online) {
635 PREPARE_WORK(&cdev->private->kick_work,
636 ccw_device_nopath_notify);
637 queue_work(ccw_device_notify_work,
638 &cdev->private->kick_work);
639 } else
640 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 break;
642 }
643}
644
645/*
646 * Get device online.
647 */
648int
649ccw_device_online(struct ccw_device *cdev)
650{
651 struct subchannel *sch;
652 int ret;
653
654 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
655 (cdev->private->state != DEV_STATE_BOXED))
656 return -EINVAL;
657 sch = to_subchannel(cdev->dev.parent);
658 if (css_init_done && !get_device(&cdev->dev))
659 return -ENODEV;
660 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
661 if (ret != 0) {
662 /* Couldn't enable the subchannel for i/o. Sick device. */
663 if (ret == -ENODEV)
664 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
665 return ret;
666 }
667 /* Do we want to do path grouping? */
668 if (!cdev->private->options.pgroup) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200669 /* Start initial path verification. */
670 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200671 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200672 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return 0;
674 }
675 /* Do a SensePGID first. */
676 cdev->private->state = DEV_STATE_SENSE_PGID;
677 ccw_device_sense_pgid_start(cdev);
678 return 0;
679}
680
681void
682ccw_device_disband_done(struct ccw_device *cdev, int err)
683{
684 switch (err) {
685 case 0:
686 ccw_device_done(cdev, DEV_STATE_OFFLINE);
687 break;
688 case -ETIME:
689 ccw_device_done(cdev, DEV_STATE_BOXED);
690 break;
691 default:
Peter Oberparleiter3ecb0a52007-05-31 17:38:07 +0200692 cdev->private->flags.donotify = 0;
693 if (get_device(&cdev->dev)) {
694 PREPARE_WORK(&cdev->private->kick_work,
695 ccw_device_call_sch_unregister);
696 queue_work(ccw_device_work, &cdev->private->kick_work);
697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
699 break;
700 }
701}
702
703/*
704 * Shutdown device.
705 */
706int
707ccw_device_offline(struct ccw_device *cdev)
708{
709 struct subchannel *sch;
710
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100711 if (ccw_device_is_orphan(cdev)) {
712 ccw_device_done(cdev, DEV_STATE_OFFLINE);
713 return 0;
714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 sch = to_subchannel(cdev->dev.parent);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800716 if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return -ENODEV;
718 if (cdev->private->state != DEV_STATE_ONLINE) {
719 if (sch->schib.scsw.actl != 0)
720 return -EBUSY;
721 return -EINVAL;
722 }
723 if (sch->schib.scsw.actl != 0)
724 return -EBUSY;
725 /* Are we doing path grouping? */
726 if (!cdev->private->options.pgroup) {
727 /* No, set state offline immediately. */
728 ccw_device_done(cdev, DEV_STATE_OFFLINE);
729 return 0;
730 }
731 /* Start Set Path Group commands. */
732 cdev->private->state = DEV_STATE_DISBAND_PGID;
733 ccw_device_disband_start(cdev);
734 return 0;
735}
736
737/*
738 * Handle timeout in device online/offline process.
739 */
740static void
741ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
742{
743 int ret;
744
745 ret = ccw_device_cancel_halt_clear(cdev);
746 switch (ret) {
747 case 0:
748 ccw_device_done(cdev, DEV_STATE_BOXED);
749 break;
750 case -ENODEV:
751 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
752 break;
753 default:
754 ccw_device_set_timeout(cdev, 3*HZ);
755 }
756}
757
758/*
759 * Handle not oper event in device recognition.
760 */
761static void
762ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
763{
764 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
765}
766
767/*
768 * Handle not operational event while offline.
769 */
770static void
771ccw_device_offline_notoper(struct ccw_device *cdev, enum dev_event dev_event)
772{
773 struct subchannel *sch;
774
775 cdev->private->state = DEV_STATE_NOT_OPER;
776 sch = to_subchannel(cdev->dev.parent);
777 if (get_device(&cdev->dev)) {
778 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100779 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 queue_work(ccw_device_work, &cdev->private->kick_work);
781 }
782 wake_up(&cdev->private->wait_q);
783}
784
785/*
786 * Handle not operational event while online.
787 */
788static void
789ccw_device_online_notoper(struct ccw_device *cdev, enum dev_event dev_event)
790{
791 struct subchannel *sch;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100792 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100795 if (sch->driver->notify) {
796 spin_unlock_irq(cdev->ccwlock);
797 ret = sch->driver->notify(&sch->dev,
798 sch->lpm ? CIO_GONE : CIO_NO_PATH);
799 spin_lock_irq(cdev->ccwlock);
800 } else
801 ret = 0;
802 if (ret) {
803 ccw_device_set_timeout(cdev, 0);
804 cdev->private->flags.fake_irb = 0;
805 cdev->private->state = DEV_STATE_DISCONNECTED;
806 wake_up(&cdev->private->wait_q);
807 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809 cdev->private->state = DEV_STATE_NOT_OPER;
810 cio_disable_subchannel(sch);
811 if (sch->schib.scsw.actl != 0) {
812 // FIXME: not-oper indication to device driver ?
813 ccw_device_call_handler(cdev);
814 }
815 if (get_device(&cdev->dev)) {
816 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100817 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 queue_work(ccw_device_work, &cdev->private->kick_work);
819 }
820 wake_up(&cdev->private->wait_q);
821}
822
823/*
824 * Handle path verification event.
825 */
826static void
827ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
828{
829 struct subchannel *sch;
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (cdev->private->state == DEV_STATE_W4SENSE) {
832 cdev->private->flags.doverify = 1;
833 return;
834 }
835 sch = to_subchannel(cdev->dev.parent);
836 /*
837 * Since we might not just be coming from an interrupt from the
838 * subchannel we have to update the schib.
839 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800840 stsch(sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 if (sch->schib.scsw.actl != 0 ||
Peter Oberparleiter65200c22006-08-07 17:00:33 +0200843 (sch->schib.scsw.stctl & SCSW_STCTL_STATUS_PEND) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
845 /*
846 * No final status yet or final status not yet delivered
847 * to the device driver. Can't do path verfication now,
848 * delay until final status was delivered.
849 */
850 cdev->private->flags.doverify = 1;
851 return;
852 }
853 /* Device is idle, we can do the path verification. */
854 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200855 cdev->private->flags.doverify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 ccw_device_verify_start(cdev);
857}
858
859/*
860 * Got an interrupt for a normal io (state online).
861 */
862static void
863ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
864{
865 struct irb *irb;
866
867 irb = (struct irb *) __LC_IRB;
868 /* Check for unsolicited interrupt. */
869 if ((irb->scsw.stctl ==
870 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
871 && (!irb->scsw.cc)) {
872 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
873 !irb->esw.esw0.erw.cons) {
874 /* Unit check but no sense data. Need basic sense. */
875 if (ccw_device_do_sense(cdev, irb) != 0)
876 goto call_handler_unsol;
Cornelia Hucke0ec5742006-06-04 02:51:27 -0700877 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 cdev->private->state = DEV_STATE_W4SENSE;
879 cdev->private->intparm = 0;
880 return;
881 }
882call_handler_unsol:
883 if (cdev->handler)
884 cdev->handler (cdev, 0, irb);
Cornelia Huck18374d32007-02-05 21:17:09 +0100885 if (cdev->private->flags.doverify)
886 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return;
888 }
889 /* Accumulate status and find out if a basic sense is needed. */
890 ccw_device_accumulate_irb(cdev, irb);
891 if (cdev->private->flags.dosense) {
892 if (ccw_device_do_sense(cdev, irb) == 0) {
893 cdev->private->state = DEV_STATE_W4SENSE;
894 }
895 return;
896 }
897 /* Call the handler. */
898 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
899 /* Start delayed path verification. */
900 ccw_device_online_verify(cdev, 0);
901}
902
903/*
904 * Got an timeout in online state.
905 */
906static void
907ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
908{
909 int ret;
910
911 ccw_device_set_timeout(cdev, 0);
912 ret = ccw_device_cancel_halt_clear(cdev);
913 if (ret == -EBUSY) {
914 ccw_device_set_timeout(cdev, 3*HZ);
915 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
916 return;
917 }
918 if (ret == -ENODEV) {
919 struct subchannel *sch;
920
921 sch = to_subchannel(cdev->dev.parent);
922 if (!sch->lpm) {
923 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100924 ccw_device_nopath_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 queue_work(ccw_device_notify_work,
926 &cdev->private->kick_work);
927 } else
928 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
929 } else if (cdev->handler)
930 cdev->handler(cdev, cdev->private->intparm,
931 ERR_PTR(-ETIMEDOUT));
932}
933
934/*
935 * Got an interrupt for a basic sense.
936 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100937static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
939{
940 struct irb *irb;
941
942 irb = (struct irb *) __LC_IRB;
943 /* Check for unsolicited interrupt. */
944 if (irb->scsw.stctl ==
945 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
946 if (irb->scsw.cc == 1)
947 /* Basic sense hasn't started. Try again. */
948 ccw_device_do_sense(cdev, irb);
949 else {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200950 CIO_MSG_EVENT(2, "Huh? 0.%x.%04x: unsolicited "
951 "interrupt during w4sense...\n",
952 cdev->private->dev_id.ssid,
953 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (cdev->handler)
955 cdev->handler (cdev, 0, irb);
956 }
957 return;
958 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800959 /*
960 * Check if a halt or clear has been issued in the meanwhile. If yes,
961 * only deliver the halt/clear interrupt to the device driver as if it
962 * had killed the original request.
963 */
964 if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
Cornelia Huckd23861f2006-12-04 15:41:04 +0100965 /* Retry Basic Sense if requested. */
966 if (cdev->private->flags.intretry) {
967 cdev->private->flags.intretry = 0;
968 ccw_device_do_sense(cdev, irb);
969 return;
970 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800971 cdev->private->flags.dosense = 0;
972 memset(&cdev->private->irb, 0, sizeof(struct irb));
973 ccw_device_accumulate_irb(cdev, irb);
974 goto call_handler;
975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /* Add basic sense info to irb. */
977 ccw_device_accumulate_basic_sense(cdev, irb);
978 if (cdev->private->flags.dosense) {
979 /* Another basic sense is needed. */
980 ccw_device_do_sense(cdev, irb);
981 return;
982 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800983call_handler:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 cdev->private->state = DEV_STATE_ONLINE;
985 /* Call the handler. */
986 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
987 /* Start delayed path verification. */
988 ccw_device_online_verify(cdev, 0);
989}
990
991static void
992ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
993{
994 struct irb *irb;
995
996 irb = (struct irb *) __LC_IRB;
997 /* Accumulate status. We don't do basic sense. */
998 ccw_device_accumulate_irb(cdev, irb);
Cornelia Huckb4f7b1e2006-06-29 15:03:35 +0200999 /* Remember to clear irb to avoid residuals. */
1000 memset(&cdev->private->irb, 0, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 /* Try to start delayed device verification. */
1002 ccw_device_online_verify(cdev, 0);
1003 /* Note: Don't call handler for cio initiated clear! */
1004}
1005
1006static void
1007ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
1008{
1009 struct subchannel *sch;
1010
1011 sch = to_subchannel(cdev->dev.parent);
1012 ccw_device_set_timeout(cdev, 0);
Cornelia Huck7c8427c2007-03-05 23:35:59 +01001013 /* Start delayed path verification. */
1014 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 /* OK, i/o is dead now. Call interrupt handler. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (cdev->handler)
1017 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +02001018 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
1021static void
1022ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1023{
1024 int ret;
1025
1026 ret = ccw_device_cancel_halt_clear(cdev);
1027 if (ret == -EBUSY) {
1028 ccw_device_set_timeout(cdev, 3*HZ);
1029 return;
1030 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +01001031 /* Start delayed path verification. */
1032 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (cdev->handler)
1034 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +02001035 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
Cornelia Hucke7769b42006-10-11 15:31:41 +02001038void device_kill_io(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 int ret;
Cornelia Hucke7769b42006-10-11 15:31:41 +02001041 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Cornelia Hucke7769b42006-10-11 15:31:41 +02001043 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 ret = ccw_device_cancel_halt_clear(cdev);
1045 if (ret == -EBUSY) {
1046 ccw_device_set_timeout(cdev, 3*HZ);
1047 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
1048 return;
1049 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +01001050 /* Start delayed path verification. */
1051 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (cdev->handler)
1053 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +02001054 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
1057static void
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001058ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001060 /* Start verification after current task finished. */
Cornelia Huck7e560812006-07-12 16:40:19 +02001061 cdev->private->flags.doverify = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
1064static void
1065ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
1066{
1067 struct irb *irb;
1068
1069 switch (dev_event) {
1070 case DEV_EVENT_INTERRUPT:
1071 irb = (struct irb *) __LC_IRB;
1072 /* Check for unsolicited interrupt. */
1073 if ((irb->scsw.stctl ==
1074 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
1075 (!irb->scsw.cc))
1076 /* FIXME: we should restart stlck here, but this
1077 * is extremely unlikely ... */
1078 goto out_wakeup;
1079
1080 ccw_device_accumulate_irb(cdev, irb);
1081 /* We don't care about basic sense etc. */
1082 break;
1083 default: /* timeout */
1084 break;
1085 }
1086out_wakeup:
1087 wake_up(&cdev->private->wait_q);
1088}
1089
1090static void
1091ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1092{
1093 struct subchannel *sch;
1094
1095 sch = to_subchannel(cdev->dev.parent);
1096 if (cio_enable_subchannel(sch, sch->schib.pmcw.isc) != 0)
1097 /* Couldn't enable the subchannel for i/o. Sick device. */
1098 return;
1099
1100 /* After 60s the device recognition is considered to have failed. */
1101 ccw_device_set_timeout(cdev, 60*HZ);
1102
1103 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1104 ccw_device_sense_id_start(cdev);
1105}
1106
1107void
1108device_trigger_reprobe(struct subchannel *sch)
1109{
1110 struct ccw_device *cdev;
1111
1112 if (!sch->dev.driver_data)
1113 return;
1114 cdev = sch->dev.driver_data;
1115 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1116 return;
1117
1118 /* Update some values. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001119 if (stsch(sch->schid, &sch->schib))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 return;
Cornelia Huck7674da72006-12-08 15:54:21 +01001121 if (!sch->schib.pmcw.dnv)
1122 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 /*
1124 * The pim, pam, pom values may not be accurate, but they are the best
1125 * we have before performing device selection :/
1126 */
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001127 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 /* Re-set some bits in the pmcw that were lost. */
1129 sch->schib.pmcw.isc = 3;
1130 sch->schib.pmcw.csense = 1;
1131 sch->schib.pmcw.ena = 0;
1132 if ((sch->lpm & (sch->lpm - 1)) != 0)
1133 sch->schib.pmcw.mp = 1;
1134 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
1135 /* We should also udate ssd info, but this has to wait. */
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001136 /* Check if this is another device which appeared on the same sch. */
1137 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1138 PREPARE_WORK(&cdev->private->kick_work,
1139 ccw_device_move_to_orphanage);
1140 queue_work(ccw_device_work, &cdev->private->kick_work);
1141 } else
1142 ccw_device_start_id(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
1145static void
1146ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1147{
1148 struct subchannel *sch;
1149
1150 sch = to_subchannel(cdev->dev.parent);
1151 /*
1152 * An interrupt in state offline means a previous disable was not
1153 * successful. Try again.
1154 */
1155 cio_disable_subchannel(sch);
1156}
1157
1158static void
1159ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1160{
1161 retry_set_schib(cdev);
1162 cdev->private->state = DEV_STATE_ONLINE;
1163 dev_fsm_event(cdev, dev_event);
1164}
1165
Cornelia Huck94bb0632006-06-29 15:08:41 +02001166static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1167 enum dev_event dev_event)
1168{
1169 cmf_retry_copy_block(cdev);
1170 cdev->private->state = DEV_STATE_ONLINE;
1171 dev_fsm_event(cdev, dev_event);
1172}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174static void
1175ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1176{
1177 ccw_device_set_timeout(cdev, 0);
1178 if (dev_event == DEV_EVENT_NOTOPER)
1179 cdev->private->state = DEV_STATE_NOT_OPER;
1180 else
1181 cdev->private->state = DEV_STATE_OFFLINE;
1182 wake_up(&cdev->private->wait_q);
1183}
1184
1185static void
1186ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1187{
1188 int ret;
1189
1190 ret = ccw_device_cancel_halt_clear(cdev);
1191 switch (ret) {
1192 case 0:
1193 cdev->private->state = DEV_STATE_OFFLINE;
1194 wake_up(&cdev->private->wait_q);
1195 break;
1196 case -ENODEV:
1197 cdev->private->state = DEV_STATE_NOT_OPER;
1198 wake_up(&cdev->private->wait_q);
1199 break;
1200 default:
1201 ccw_device_set_timeout(cdev, HZ/10);
1202 }
1203}
1204
1205/*
1206 * No operation action. This is used e.g. to ignore a timeout event in
1207 * state offline.
1208 */
1209static void
1210ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1211{
1212}
1213
1214/*
1215 * Bug operation action.
1216 */
1217static void
1218ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1219{
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001220 CIO_MSG_EVENT(0, "dev_jumptable[%i][%i] == NULL\n",
1221 cdev->private->state, dev_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 BUG();
1223}
1224
1225/*
1226 * device statemachine
1227 */
1228fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1229 [DEV_STATE_NOT_OPER] = {
1230 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1231 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1232 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1233 [DEV_EVENT_VERIFY] = ccw_device_nop,
1234 },
1235 [DEV_STATE_SENSE_PGID] = {
1236 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1237 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1238 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1239 [DEV_EVENT_VERIFY] = ccw_device_nop,
1240 },
1241 [DEV_STATE_SENSE_ID] = {
1242 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1243 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1244 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1245 [DEV_EVENT_VERIFY] = ccw_device_nop,
1246 },
1247 [DEV_STATE_OFFLINE] = {
1248 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1249 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1250 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1251 [DEV_EVENT_VERIFY] = ccw_device_nop,
1252 },
1253 [DEV_STATE_VERIFY] = {
1254 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1255 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1256 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001257 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 },
1259 [DEV_STATE_ONLINE] = {
1260 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1261 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1262 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1263 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1264 },
1265 [DEV_STATE_W4SENSE] = {
1266 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1267 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1268 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1269 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1270 },
1271 [DEV_STATE_DISBAND_PGID] = {
1272 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1273 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1274 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1275 [DEV_EVENT_VERIFY] = ccw_device_nop,
1276 },
1277 [DEV_STATE_BOXED] = {
1278 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1279 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1280 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1281 [DEV_EVENT_VERIFY] = ccw_device_nop,
1282 },
1283 /* states to wait for i/o completion before doing something */
1284 [DEV_STATE_CLEAR_VERIFY] = {
1285 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1286 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1287 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1288 [DEV_EVENT_VERIFY] = ccw_device_nop,
1289 },
1290 [DEV_STATE_TIMEOUT_KILL] = {
1291 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1292 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1293 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1294 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1295 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 [DEV_STATE_QUIESCE] = {
1297 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1298 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1299 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1300 [DEV_EVENT_VERIFY] = ccw_device_nop,
1301 },
1302 /* special states for devices gone not operational */
1303 [DEV_STATE_DISCONNECTED] = {
1304 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1305 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1306 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001307 [DEV_EVENT_VERIFY] = ccw_device_start_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 },
1309 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1310 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1311 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1312 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1313 [DEV_EVENT_VERIFY] = ccw_device_nop,
1314 },
1315 [DEV_STATE_CMFCHANGE] = {
1316 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1317 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1318 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1319 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1320 },
Cornelia Huck94bb0632006-06-29 15:08:41 +02001321 [DEV_STATE_CMFUPDATE] = {
1322 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1323 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1324 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1325 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1326 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327};
1328
1329/*
1330 * io_subchannel_irq is called for "real" interrupts or for status
1331 * pending conditions on msch.
1332 */
1333void
1334io_subchannel_irq (struct device *pdev)
1335{
1336 struct ccw_device *cdev;
1337
1338 cdev = to_subchannel(pdev)->dev.driver_data;
1339
1340 CIO_TRACE_EVENT (3, "IRQ");
1341 CIO_TRACE_EVENT (3, pdev->bus_id);
1342 if (cdev)
1343 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1344}
1345
1346EXPORT_SYMBOL_GPL(ccw_device_set_timeout);