blob: baca6c6c9855c3f7532e39e95ae0fa8afe63c576 [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
Sebastian Ott14ff56b2008-01-26 14:10:37 +010028static int timeout_log_enabled;
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030int
31device_is_online(struct subchannel *sch)
32{
33 struct ccw_device *cdev;
34
35 if (!sch->dev.driver_data)
36 return 0;
37 cdev = sch->dev.driver_data;
38 return (cdev->private->state == DEV_STATE_ONLINE);
39}
40
41int
42device_is_disconnected(struct subchannel *sch)
43{
44 struct ccw_device *cdev;
45
46 if (!sch->dev.driver_data)
47 return 0;
48 cdev = sch->dev.driver_data;
49 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
50 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
51}
52
53void
54device_set_disconnected(struct subchannel *sch)
55{
56 struct ccw_device *cdev;
57
58 if (!sch->dev.driver_data)
59 return;
60 cdev = sch->dev.driver_data;
61 ccw_device_set_timeout(cdev, 0);
62 cdev->private->flags.fake_irb = 0;
63 cdev->private->state = DEV_STATE_DISCONNECTED;
64}
65
Cornelia Huckd23861f2006-12-04 15:41:04 +010066void device_set_intretry(struct subchannel *sch)
67{
68 struct ccw_device *cdev;
69
70 cdev = sch->dev.driver_data;
71 if (!cdev)
72 return;
73 cdev->private->flags.intretry = 1;
74}
75
Cornelia Huck24cb5b42006-12-04 15:41:01 +010076int device_trigger_verify(struct subchannel *sch)
77{
78 struct ccw_device *cdev;
79
80 cdev = sch->dev.driver_data;
81 if (!cdev || !cdev->online)
82 return -EINVAL;
83 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
84 return 0;
85}
86
Sebastian Ott14ff56b2008-01-26 14:10:37 +010087static int __init ccw_timeout_log_setup(char *unused)
88{
89 timeout_log_enabled = 1;
90 return 1;
91}
92
93__setup("ccw_timeout_log", ccw_timeout_log_setup);
94
95static void ccw_timeout_log(struct ccw_device *cdev)
96{
97 struct schib schib;
98 struct subchannel *sch;
99 int cc;
100
101 sch = to_subchannel(cdev->dev.parent);
102 cc = stsch(sch->schid, &schib);
103
104 printk(KERN_WARNING "cio: ccw device timeout occurred at %llx, "
105 "device information:\n", get_clock());
106 printk(KERN_WARNING "cio: orb:\n");
107 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
108 &sch->orb, sizeof(sch->orb), 0);
109 printk(KERN_WARNING "cio: ccw device bus id: %s\n", cdev->dev.bus_id);
110 printk(KERN_WARNING "cio: subchannel bus id: %s\n", sch->dev.bus_id);
111 printk(KERN_WARNING "cio: subchannel lpm: %02x, opm: %02x, "
112 "vpm: %02x\n", sch->lpm, sch->opm, sch->vpm);
113
114 if ((void *)(addr_t)sch->orb.cpa == &sch->sense_ccw ||
115 (void *)(addr_t)sch->orb.cpa == cdev->private->iccws)
116 printk(KERN_WARNING "cio: last channel program (intern):\n");
117 else
118 printk(KERN_WARNING "cio: last channel program:\n");
119
120 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
121 (void *)(addr_t)sch->orb.cpa, sizeof(struct ccw1), 0);
122 printk(KERN_WARNING "cio: ccw device state: %d\n",
123 cdev->private->state);
124 printk(KERN_WARNING "cio: store subchannel returned: cc=%d\n", cc);
125 printk(KERN_WARNING "cio: schib:\n");
126 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
127 &schib, sizeof(schib), 0);
128 printk(KERN_WARNING "cio: ccw device flags:\n");
129 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
130 &cdev->private->flags, sizeof(cdev->private->flags), 0);
131}
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/*
134 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
135 */
136static void
137ccw_device_timeout(unsigned long data)
138{
139 struct ccw_device *cdev;
140
141 cdev = (struct ccw_device *) data;
142 spin_lock_irq(cdev->ccwlock);
Sebastian Ott14ff56b2008-01-26 14:10:37 +0100143 if (timeout_log_enabled)
144 ccw_timeout_log(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
146 spin_unlock_irq(cdev->ccwlock);
147}
148
149/*
150 * Set timeout
151 */
152void
153ccw_device_set_timeout(struct ccw_device *cdev, int expires)
154{
155 if (expires == 0) {
156 del_timer(&cdev->private->timer);
157 return;
158 }
159 if (timer_pending(&cdev->private->timer)) {
160 if (mod_timer(&cdev->private->timer, jiffies + expires))
161 return;
162 }
163 cdev->private->timer.function = ccw_device_timeout;
164 cdev->private->timer.data = (unsigned long) cdev;
165 cdev->private->timer.expires = jiffies + expires;
166 add_timer(&cdev->private->timer);
167}
168
169/* Kill any pending timers after machine check. */
170void
171device_kill_pending_timer(struct subchannel *sch)
172{
173 struct ccw_device *cdev;
174
175 if (!sch->dev.driver_data)
176 return;
177 cdev = sch->dev.driver_data;
178 ccw_device_set_timeout(cdev, 0);
179}
180
181/*
182 * Cancel running i/o. This is called repeatedly since halt/clear are
183 * asynchronous operations. We do one try with cio_cancel, two tries
184 * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
185 * Returns 0 if device now idle, -ENODEV for device not operational and
186 * -EBUSY if an interrupt is expected (either from halt/clear or from a
187 * status pending).
188 */
189int
190ccw_device_cancel_halt_clear(struct ccw_device *cdev)
191{
192 struct subchannel *sch;
193 int ret;
194
195 sch = to_subchannel(cdev->dev.parent);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800196 ret = stsch(sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (ret || !sch->schib.pmcw.dnv)
198 return -ENODEV;
Cornelia Huck2470b642007-03-05 23:36:02 +0100199 if (!sch->schib.pmcw.ena)
200 /* Not operational -> done. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return 0;
202 /* Stage 1: cancel io. */
203 if (!(sch->schib.scsw.actl & SCSW_ACTL_HALT_PEND) &&
204 !(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
205 ret = cio_cancel(sch);
206 if (ret != -EINVAL)
207 return ret;
208 /* cancel io unsuccessful. From now on it is asynchronous. */
209 cdev->private->iretry = 3; /* 3 halt retries. */
210 }
211 if (!(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
212 /* Stage 2: halt io. */
213 if (cdev->private->iretry) {
214 cdev->private->iretry--;
215 ret = cio_halt(sch);
Peter Oberparleiterba4ba8a2006-07-27 14:00:23 +0200216 if (ret != -EBUSY)
217 return (ret == 0) ? -EBUSY : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219 /* halt io unsuccessful. */
220 cdev->private->iretry = 255; /* 255 clear retries. */
221 }
222 /* Stage 3: clear io. */
223 if (cdev->private->iretry) {
224 cdev->private->iretry--;
225 ret = cio_clear (sch);
226 return (ret == 0) ? -EBUSY : ret;
227 }
228 panic("Can't stop i/o on subchannel.\n");
229}
230
231static int
232ccw_device_handle_oper(struct ccw_device *cdev)
233{
234 struct subchannel *sch;
235
236 sch = to_subchannel(cdev->dev.parent);
237 cdev->private->flags.recog_done = 1;
238 /*
239 * Check if cu type and device type still match. If
240 * not, it is certainly another device and we have to
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100241 * de- and re-register.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
243 if (cdev->id.cu_type != cdev->private->senseid.cu_type ||
244 cdev->id.cu_model != cdev->private->senseid.cu_model ||
245 cdev->id.dev_type != cdev->private->senseid.dev_type ||
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100246 cdev->id.dev_model != cdev->private->senseid.dev_model) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100248 ccw_device_do_unreg_rereg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 queue_work(ccw_device_work, &cdev->private->kick_work);
250 return 0;
251 }
252 cdev->private->flags.donotify = 1;
253 return 1;
254}
255
256/*
257 * The machine won't give us any notification by machine check if a chpid has
258 * been varied online on the SE so we have to find out by magic (i. e. driving
259 * the channel subsystem to device selection and updating our path masks).
260 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100261static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262__recover_lost_chpids(struct subchannel *sch, int old_lpm)
263{
264 int mask, i;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200265 struct chp_id chpid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200267 chp_id_init(&chpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 for (i = 0; i<8; i++) {
269 mask = 0x80 >> i;
270 if (!(sch->lpm & mask))
271 continue;
272 if (old_lpm & mask)
273 continue;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200274 chpid.id = sch->schib.pmcw.chpid[i];
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200275 if (!chp_is_registered(chpid))
276 css_schedule_eval_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278}
279
280/*
281 * Stop device recognition.
282 */
283static void
284ccw_device_recog_done(struct ccw_device *cdev, int state)
285{
286 struct subchannel *sch;
287 int notify, old_lpm, same_dev;
288
289 sch = to_subchannel(cdev->dev.parent);
290
291 ccw_device_set_timeout(cdev, 0);
292 cio_disable_subchannel(sch);
293 /*
294 * Now that we tried recognition, we have performed device selection
295 * through ssch() and the path information is up to date.
296 */
297 old_lpm = sch->lpm;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800298 stsch(sch->schid, &sch->schib);
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200299 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck4ffa9232005-07-29 14:03:37 -0700300 /* Check since device may again have become not operational. */
301 if (!sch->schib.pmcw.dnv)
302 state = DEV_STATE_NOT_OPER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
304 /* Force reprobe on all chpids. */
305 old_lpm = 0;
306 if (sch->lpm != old_lpm)
307 __recover_lost_chpids(sch, old_lpm);
308 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
309 if (state == DEV_STATE_NOT_OPER) {
310 cdev->private->flags.recog_done = 1;
311 cdev->private->state = DEV_STATE_DISCONNECTED;
312 return;
313 }
314 /* Boxed devices don't need extra treatment. */
315 }
316 notify = 0;
317 same_dev = 0; /* Keep the compiler quiet... */
318 switch (state) {
319 case DEV_STATE_NOT_OPER:
320 CIO_DEBUG(KERN_WARNING, 2,
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200321 "cio: SenseID : unknown device %04x on subchannel "
Cornelia Huck78964262006-10-11 15:31:38 +0200322 "0.%x.%04x\n", cdev->private->dev_id.devno,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800323 sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 break;
325 case DEV_STATE_OFFLINE:
326 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
327 same_dev = ccw_device_handle_oper(cdev);
328 notify = 1;
329 }
330 /* fill out sense information */
Heiko Carstens81388d22006-09-20 15:59:17 +0200331 memset(&cdev->id, 0, sizeof(cdev->id));
Heiko Carstens292888c2006-08-30 14:33:35 +0200332 cdev->id.cu_type = cdev->private->senseid.cu_type;
333 cdev->id.cu_model = cdev->private->senseid.cu_model;
334 cdev->id.dev_type = cdev->private->senseid.dev_type;
335 cdev->id.dev_model = cdev->private->senseid.dev_model;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (notify) {
337 cdev->private->state = DEV_STATE_OFFLINE;
338 if (same_dev) {
339 /* Get device online again. */
340 ccw_device_online(cdev);
341 wake_up(&cdev->private->wait_q);
342 }
343 return;
344 }
345 /* Issue device info message. */
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200346 CIO_DEBUG(KERN_INFO, 2,
347 "cio: SenseID : device 0.%x.%04x reports: "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800349 "%04X/%02X\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200350 cdev->private->dev_id.ssid,
351 cdev->private->dev_id.devno,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 cdev->id.cu_type, cdev->id.cu_model,
353 cdev->id.dev_type, cdev->id.dev_model);
354 break;
355 case DEV_STATE_BOXED:
356 CIO_DEBUG(KERN_WARNING, 2,
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200357 "cio: SenseID : boxed device %04x on subchannel "
Cornelia Huck78964262006-10-11 15:31:38 +0200358 "0.%x.%04x\n", cdev->private->dev_id.devno,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800359 sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 break;
361 }
362 cdev->private->state = state;
363 io_subchannel_recog_done(cdev);
364 if (state != DEV_STATE_NOT_OPER)
365 wake_up(&cdev->private->wait_q);
366}
367
368/*
369 * Function called from device_id.c after sense id has completed.
370 */
371void
372ccw_device_sense_id_done(struct ccw_device *cdev, int err)
373{
374 switch (err) {
375 case 0:
376 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
377 break;
378 case -ETIME: /* Sense id stopped by timeout. */
379 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
380 break;
381 default:
382 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
383 break;
384 }
385}
386
387static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100388ccw_device_oper_notify(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100390 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 struct ccw_device *cdev;
392 struct subchannel *sch;
393 int ret;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100394 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100396 priv = container_of(work, struct ccw_device_private, kick_work);
397 cdev = priv->cdev;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100398 spin_lock_irqsave(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100400 if (sch->driver && sch->driver->notify) {
401 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huck602b20f2008-01-26 14:10:39 +0100402 ret = sch->driver->notify(sch, CIO_OPER);
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100403 spin_lock_irqsave(cdev->ccwlock, flags);
404 } else
405 ret = 0;
406 if (ret) {
407 /* Reenable channel measurements, if needed. */
408 spin_unlock_irqrestore(cdev->ccwlock, flags);
409 cmf_reenable(cdev);
410 spin_lock_irqsave(cdev->ccwlock, flags);
411 wake_up(&cdev->private->wait_q);
412 }
413 spin_unlock_irqrestore(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (!ret)
415 /* Driver doesn't want device back. */
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100416 ccw_device_do_unreg_rereg(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
419/*
420 * Finished with online/offline processing.
421 */
422static void
423ccw_device_done(struct ccw_device *cdev, int state)
424{
425 struct subchannel *sch;
426
427 sch = to_subchannel(cdev->dev.parent);
428
Cornelia Huckf1ee3282006-10-04 20:02:02 +0200429 ccw_device_set_timeout(cdev, 0);
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (state != DEV_STATE_ONLINE)
432 cio_disable_subchannel(sch);
433
434 /* Reset device status. */
435 memset(&cdev->private->irb, 0, sizeof(struct irb));
436
437 cdev->private->state = state;
438
439
440 if (state == DEV_STATE_BOXED)
441 CIO_DEBUG(KERN_WARNING, 2,
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200442 "cio: Boxed device %04x on subchannel %04x\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200443 cdev->private->dev_id.devno, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 if (cdev->private->flags.donotify) {
446 cdev->private->flags.donotify = 0;
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100447 PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
449 }
450 wake_up(&cdev->private->wait_q);
451
452 if (css_init_done && state != DEV_STATE_ONLINE)
453 put_device (&cdev->dev);
454}
455
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100456static int cmp_pgid(struct pgid *p1, struct pgid *p2)
Cornelia Huck7e560812006-07-12 16:40:19 +0200457{
458 char *c1;
459 char *c2;
460
461 c1 = (char *)p1;
462 c2 = (char *)p2;
463
464 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
465}
466
467static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
468{
469 int i;
470 int last;
471
472 last = 0;
473 for (i = 0; i < 8; i++) {
474 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
475 /* No PGID yet */
476 continue;
477 if (cdev->private->pgid[last].inf.ps.state1 ==
478 SNID_STATE1_RESET) {
479 /* First non-zero PGID */
480 last = i;
481 continue;
482 }
483 if (cmp_pgid(&cdev->private->pgid[i],
484 &cdev->private->pgid[last]) == 0)
485 /* Non-conflicting PGIDs */
486 continue;
487
488 /* PGID mismatch, can't pathgroup. */
489 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
490 "0.%x.%04x, can't pathgroup\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200491 cdev->private->dev_id.ssid,
492 cdev->private->dev_id.devno);
Cornelia Huck7e560812006-07-12 16:40:19 +0200493 cdev->private->options.pgroup = 0;
494 return;
495 }
496 if (cdev->private->pgid[last].inf.ps.state1 ==
497 SNID_STATE1_RESET)
498 /* No previous pgid found */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200499 memcpy(&cdev->private->pgid[0],
500 &channel_subsystems[0]->global_pgid,
Cornelia Huck7e560812006-07-12 16:40:19 +0200501 sizeof(struct pgid));
502 else
503 /* Use existing pgid */
504 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
505 sizeof(struct pgid));
506}
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508/*
509 * Function called from device_pgid.c after sense path ground has completed.
510 */
511void
512ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
513{
514 struct subchannel *sch;
515
516 sch = to_subchannel(cdev->dev.parent);
517 switch (err) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200518 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
519 cdev->private->options.pgroup = 0;
520 break;
521 case 0: /* success */
522 case -EACCES: /* partial success, some paths not operational */
523 /* Check if all pgids are equal or 0. */
524 __ccw_device_get_common_pgid(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 break;
526 case -ETIME: /* Sense path group id stopped by timeout. */
527 case -EUSERS: /* device is reserved for someone else. */
528 ccw_device_done(cdev, DEV_STATE_BOXED);
Cornelia Huck7e560812006-07-12 16:40:19 +0200529 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 default:
531 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Cornelia Huck7e560812006-07-12 16:40:19 +0200532 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
Cornelia Huck7e560812006-07-12 16:40:19 +0200534 /* Start Path Group verification. */
Cornelia Huck7e560812006-07-12 16:40:19 +0200535 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200536 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200537 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
540/*
541 * Start device recognition.
542 */
543int
544ccw_device_recognition(struct ccw_device *cdev)
545{
546 struct subchannel *sch;
547 int ret;
548
549 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
550 (cdev->private->state != DEV_STATE_BOXED))
551 return -EINVAL;
552 sch = to_subchannel(cdev->dev.parent);
553 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
554 if (ret != 0)
555 /* Couldn't enable the subchannel for i/o. Sick device. */
556 return ret;
557
558 /* After 60s the device recognition is considered to have failed. */
559 ccw_device_set_timeout(cdev, 60*HZ);
560
561 /*
562 * We used to start here with a sense pgid to find out whether a device
563 * is locked by someone else. Unfortunately, the sense pgid command
564 * code has other meanings on devices predating the path grouping
565 * algorithm, so we start with sense id and box the device after an
566 * timeout (or if sense pgid during path verification detects the device
567 * is locked, as may happen on newer devices).
568 */
569 cdev->private->flags.recog_done = 0;
570 cdev->private->state = DEV_STATE_SENSE_ID;
571 ccw_device_sense_id_start(cdev);
572 return 0;
573}
574
575/*
576 * Handle timeout in device recognition.
577 */
578static void
579ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
580{
581 int ret;
582
583 ret = ccw_device_cancel_halt_clear(cdev);
584 switch (ret) {
585 case 0:
586 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
587 break;
588 case -ENODEV:
589 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
590 break;
591 default:
592 ccw_device_set_timeout(cdev, 3*HZ);
593 }
594}
595
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597void
598ccw_device_verify_done(struct ccw_device *cdev, int err)
599{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200600 struct subchannel *sch;
601
602 sch = to_subchannel(cdev->dev.parent);
603 /* Update schib - pom may have changed. */
604 stsch(sch->schid, &sch->schib);
605 /* Update lpm with verified path mask. */
606 sch->lpm = sch->vpm;
607 /* Repeat path verification? */
608 if (cdev->private->flags.doverify) {
609 cdev->private->flags.doverify = 0;
610 ccw_device_verify_start(cdev);
611 return;
612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 switch (err) {
614 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
615 cdev->private->options.pgroup = 0;
616 case 0:
617 ccw_device_done(cdev, DEV_STATE_ONLINE);
618 /* Deliver fake irb to device driver, if needed. */
619 if (cdev->private->flags.fake_irb) {
620 memset(&cdev->private->irb, 0, sizeof(struct irb));
Heiko Carstens292888c2006-08-30 14:33:35 +0200621 cdev->private->irb.scsw.cc = 1;
622 cdev->private->irb.scsw.fctl = SCSW_FCTL_START_FUNC;
623 cdev->private->irb.scsw.actl = SCSW_ACTL_START_PEND;
624 cdev->private->irb.scsw.stctl = SCSW_STCTL_STATUS_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 cdev->private->flags.fake_irb = 0;
626 if (cdev->handler)
627 cdev->handler(cdev, cdev->private->intparm,
628 &cdev->private->irb);
629 memset(&cdev->private->irb, 0, sizeof(struct irb));
630 }
631 break;
632 case -ETIME:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200633 /* Reset oper notify indication after verify error. */
634 cdev->private->flags.donotify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 ccw_device_done(cdev, DEV_STATE_BOXED);
636 break;
637 default:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200638 /* Reset oper notify indication after verify error. */
639 cdev->private->flags.donotify = 0;
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200640 if (cdev->online)
641 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
642 else
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100643 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 break;
645 }
646}
647
648/*
649 * Get device online.
650 */
651int
652ccw_device_online(struct ccw_device *cdev)
653{
654 struct subchannel *sch;
655 int ret;
656
657 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
658 (cdev->private->state != DEV_STATE_BOXED))
659 return -EINVAL;
660 sch = to_subchannel(cdev->dev.parent);
661 if (css_init_done && !get_device(&cdev->dev))
662 return -ENODEV;
663 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
664 if (ret != 0) {
665 /* Couldn't enable the subchannel for i/o. Sick device. */
666 if (ret == -ENODEV)
667 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
668 return ret;
669 }
670 /* Do we want to do path grouping? */
671 if (!cdev->private->options.pgroup) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200672 /* Start initial path verification. */
673 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200674 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200675 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return 0;
677 }
678 /* Do a SensePGID first. */
679 cdev->private->state = DEV_STATE_SENSE_PGID;
680 ccw_device_sense_pgid_start(cdev);
681 return 0;
682}
683
684void
685ccw_device_disband_done(struct ccw_device *cdev, int err)
686{
687 switch (err) {
688 case 0:
689 ccw_device_done(cdev, DEV_STATE_OFFLINE);
690 break;
691 case -ETIME:
692 ccw_device_done(cdev, DEV_STATE_BOXED);
693 break;
694 default:
Peter Oberparleiter3ecb0a52007-05-31 17:38:07 +0200695 cdev->private->flags.donotify = 0;
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200696 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
698 break;
699 }
700}
701
702/*
703 * Shutdown device.
704 */
705int
706ccw_device_offline(struct ccw_device *cdev)
707{
708 struct subchannel *sch;
709
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100710 if (ccw_device_is_orphan(cdev)) {
711 ccw_device_done(cdev, DEV_STATE_OFFLINE);
712 return 0;
713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 sch = to_subchannel(cdev->dev.parent);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800715 if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return -ENODEV;
717 if (cdev->private->state != DEV_STATE_ONLINE) {
718 if (sch->schib.scsw.actl != 0)
719 return -EBUSY;
720 return -EINVAL;
721 }
722 if (sch->schib.scsw.actl != 0)
723 return -EBUSY;
724 /* Are we doing path grouping? */
725 if (!cdev->private->options.pgroup) {
726 /* No, set state offline immediately. */
727 ccw_device_done(cdev, DEV_STATE_OFFLINE);
728 return 0;
729 }
730 /* Start Set Path Group commands. */
731 cdev->private->state = DEV_STATE_DISBAND_PGID;
732 ccw_device_disband_start(cdev);
733 return 0;
734}
735
736/*
737 * Handle timeout in device online/offline process.
738 */
739static void
740ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
741{
742 int ret;
743
744 ret = ccw_device_cancel_halt_clear(cdev);
745 switch (ret) {
746 case 0:
747 ccw_device_done(cdev, DEV_STATE_BOXED);
748 break;
749 case -ENODEV:
750 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
751 break;
752 default:
753 ccw_device_set_timeout(cdev, 3*HZ);
754 }
755}
756
757/*
758 * Handle not oper event in device recognition.
759 */
760static void
761ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
762{
763 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
764}
765
766/*
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200767 * Handle not operational event in non-special state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 */
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200769static void ccw_device_generic_notoper(struct ccw_device *cdev,
770 enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 struct subchannel *sch;
773
774 cdev->private->state = DEV_STATE_NOT_OPER;
775 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200776 css_schedule_eval(sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
779/*
780 * Handle path verification event.
781 */
782static void
783ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
784{
785 struct subchannel *sch;
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (cdev->private->state == DEV_STATE_W4SENSE) {
788 cdev->private->flags.doverify = 1;
789 return;
790 }
791 sch = to_subchannel(cdev->dev.parent);
792 /*
793 * Since we might not just be coming from an interrupt from the
794 * subchannel we have to update the schib.
795 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800796 stsch(sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 if (sch->schib.scsw.actl != 0 ||
Peter Oberparleiter65200c22006-08-07 17:00:33 +0200799 (sch->schib.scsw.stctl & SCSW_STCTL_STATUS_PEND) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
801 /*
802 * No final status yet or final status not yet delivered
803 * to the device driver. Can't do path verfication now,
804 * delay until final status was delivered.
805 */
806 cdev->private->flags.doverify = 1;
807 return;
808 }
809 /* Device is idle, we can do the path verification. */
810 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200811 cdev->private->flags.doverify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 ccw_device_verify_start(cdev);
813}
814
815/*
816 * Got an interrupt for a normal io (state online).
817 */
818static void
819ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
820{
821 struct irb *irb;
822
823 irb = (struct irb *) __LC_IRB;
824 /* Check for unsolicited interrupt. */
825 if ((irb->scsw.stctl ==
826 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
827 && (!irb->scsw.cc)) {
828 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
829 !irb->esw.esw0.erw.cons) {
830 /* Unit check but no sense data. Need basic sense. */
831 if (ccw_device_do_sense(cdev, irb) != 0)
832 goto call_handler_unsol;
Cornelia Hucke0ec5742006-06-04 02:51:27 -0700833 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 cdev->private->state = DEV_STATE_W4SENSE;
835 cdev->private->intparm = 0;
836 return;
837 }
838call_handler_unsol:
839 if (cdev->handler)
840 cdev->handler (cdev, 0, irb);
Cornelia Huck18374d32007-02-05 21:17:09 +0100841 if (cdev->private->flags.doverify)
842 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return;
844 }
845 /* Accumulate status and find out if a basic sense is needed. */
846 ccw_device_accumulate_irb(cdev, irb);
847 if (cdev->private->flags.dosense) {
848 if (ccw_device_do_sense(cdev, irb) == 0) {
849 cdev->private->state = DEV_STATE_W4SENSE;
850 }
851 return;
852 }
853 /* Call the handler. */
854 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
855 /* Start delayed path verification. */
856 ccw_device_online_verify(cdev, 0);
857}
858
859/*
860 * Got an timeout in online state.
861 */
862static void
863ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
864{
865 int ret;
866
867 ccw_device_set_timeout(cdev, 0);
868 ret = ccw_device_cancel_halt_clear(cdev);
869 if (ret == -EBUSY) {
870 ccw_device_set_timeout(cdev, 3*HZ);
871 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
872 return;
873 }
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200874 if (ret == -ENODEV)
875 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
876 else if (cdev->handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 cdev->handler(cdev, cdev->private->intparm,
878 ERR_PTR(-ETIMEDOUT));
879}
880
881/*
882 * Got an interrupt for a basic sense.
883 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100884static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
886{
887 struct irb *irb;
888
889 irb = (struct irb *) __LC_IRB;
890 /* Check for unsolicited interrupt. */
891 if (irb->scsw.stctl ==
892 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
893 if (irb->scsw.cc == 1)
894 /* Basic sense hasn't started. Try again. */
895 ccw_device_do_sense(cdev, irb);
896 else {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200897 CIO_MSG_EVENT(2, "Huh? 0.%x.%04x: unsolicited "
898 "interrupt during w4sense...\n",
899 cdev->private->dev_id.ssid,
900 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (cdev->handler)
902 cdev->handler (cdev, 0, irb);
903 }
904 return;
905 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800906 /*
907 * Check if a halt or clear has been issued in the meanwhile. If yes,
908 * only deliver the halt/clear interrupt to the device driver as if it
909 * had killed the original request.
910 */
911 if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
Cornelia Huckd23861f2006-12-04 15:41:04 +0100912 /* Retry Basic Sense if requested. */
913 if (cdev->private->flags.intretry) {
914 cdev->private->flags.intretry = 0;
915 ccw_device_do_sense(cdev, irb);
916 return;
917 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800918 cdev->private->flags.dosense = 0;
919 memset(&cdev->private->irb, 0, sizeof(struct irb));
920 ccw_device_accumulate_irb(cdev, irb);
921 goto call_handler;
922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 /* Add basic sense info to irb. */
924 ccw_device_accumulate_basic_sense(cdev, irb);
925 if (cdev->private->flags.dosense) {
926 /* Another basic sense is needed. */
927 ccw_device_do_sense(cdev, irb);
928 return;
929 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800930call_handler:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 cdev->private->state = DEV_STATE_ONLINE;
932 /* Call the handler. */
933 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
934 /* Start delayed path verification. */
935 ccw_device_online_verify(cdev, 0);
936}
937
938static void
939ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
940{
941 struct irb *irb;
942
943 irb = (struct irb *) __LC_IRB;
944 /* Accumulate status. We don't do basic sense. */
945 ccw_device_accumulate_irb(cdev, irb);
Cornelia Huckb4f7b1e2006-06-29 15:03:35 +0200946 /* Remember to clear irb to avoid residuals. */
947 memset(&cdev->private->irb, 0, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 /* Try to start delayed device verification. */
949 ccw_device_online_verify(cdev, 0);
950 /* Note: Don't call handler for cio initiated clear! */
951}
952
953static void
954ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
955{
956 struct subchannel *sch;
957
958 sch = to_subchannel(cdev->dev.parent);
959 ccw_device_set_timeout(cdev, 0);
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100960 /* Start delayed path verification. */
961 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 /* OK, i/o is dead now. Call interrupt handler. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (cdev->handler)
964 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200965 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
968static void
969ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
970{
971 int ret;
972
973 ret = ccw_device_cancel_halt_clear(cdev);
974 if (ret == -EBUSY) {
975 ccw_device_set_timeout(cdev, 3*HZ);
976 return;
977 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100978 /* Start delayed path verification. */
979 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (cdev->handler)
981 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200982 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
Cornelia Hucke7769b42006-10-11 15:31:41 +0200985void device_kill_io(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
987 int ret;
Cornelia Hucke7769b42006-10-11 15:31:41 +0200988 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Cornelia Hucke7769b42006-10-11 15:31:41 +0200990 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 ret = ccw_device_cancel_halt_clear(cdev);
992 if (ret == -EBUSY) {
993 ccw_device_set_timeout(cdev, 3*HZ);
994 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
995 return;
996 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100997 /* Start delayed path verification. */
998 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (cdev->handler)
1000 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +02001001 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
1004static void
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001005ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001007 /* Start verification after current task finished. */
Cornelia Huck7e560812006-07-12 16:40:19 +02001008 cdev->private->flags.doverify = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
1011static void
1012ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
1013{
1014 struct irb *irb;
1015
1016 switch (dev_event) {
1017 case DEV_EVENT_INTERRUPT:
1018 irb = (struct irb *) __LC_IRB;
1019 /* Check for unsolicited interrupt. */
1020 if ((irb->scsw.stctl ==
1021 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
1022 (!irb->scsw.cc))
1023 /* FIXME: we should restart stlck here, but this
1024 * is extremely unlikely ... */
1025 goto out_wakeup;
1026
1027 ccw_device_accumulate_irb(cdev, irb);
1028 /* We don't care about basic sense etc. */
1029 break;
1030 default: /* timeout */
1031 break;
1032 }
1033out_wakeup:
1034 wake_up(&cdev->private->wait_q);
1035}
1036
1037static void
1038ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1039{
1040 struct subchannel *sch;
1041
1042 sch = to_subchannel(cdev->dev.parent);
1043 if (cio_enable_subchannel(sch, sch->schib.pmcw.isc) != 0)
1044 /* Couldn't enable the subchannel for i/o. Sick device. */
1045 return;
1046
1047 /* After 60s the device recognition is considered to have failed. */
1048 ccw_device_set_timeout(cdev, 60*HZ);
1049
1050 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1051 ccw_device_sense_id_start(cdev);
1052}
1053
1054void
1055device_trigger_reprobe(struct subchannel *sch)
1056{
1057 struct ccw_device *cdev;
1058
1059 if (!sch->dev.driver_data)
1060 return;
1061 cdev = sch->dev.driver_data;
1062 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1063 return;
1064
1065 /* Update some values. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001066 if (stsch(sch->schid, &sch->schib))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return;
Cornelia Huck7674da72006-12-08 15:54:21 +01001068 if (!sch->schib.pmcw.dnv)
1069 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 /*
1071 * The pim, pam, pom values may not be accurate, but they are the best
1072 * we have before performing device selection :/
1073 */
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001074 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 /* Re-set some bits in the pmcw that were lost. */
1076 sch->schib.pmcw.isc = 3;
1077 sch->schib.pmcw.csense = 1;
1078 sch->schib.pmcw.ena = 0;
1079 if ((sch->lpm & (sch->lpm - 1)) != 0)
1080 sch->schib.pmcw.mp = 1;
1081 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
1082 /* We should also udate ssd info, but this has to wait. */
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001083 /* Check if this is another device which appeared on the same sch. */
1084 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1085 PREPARE_WORK(&cdev->private->kick_work,
1086 ccw_device_move_to_orphanage);
Cornelia Huckc5d4a992007-11-20 11:13:41 +01001087 queue_work(slow_path_wq, &cdev->private->kick_work);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001088 } else
1089 ccw_device_start_id(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
1092static void
1093ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1094{
1095 struct subchannel *sch;
1096
1097 sch = to_subchannel(cdev->dev.parent);
1098 /*
1099 * An interrupt in state offline means a previous disable was not
1100 * successful. Try again.
1101 */
1102 cio_disable_subchannel(sch);
1103}
1104
1105static void
1106ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1107{
1108 retry_set_schib(cdev);
1109 cdev->private->state = DEV_STATE_ONLINE;
1110 dev_fsm_event(cdev, dev_event);
1111}
1112
Cornelia Huck94bb0632006-06-29 15:08:41 +02001113static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1114 enum dev_event dev_event)
1115{
1116 cmf_retry_copy_block(cdev);
1117 cdev->private->state = DEV_STATE_ONLINE;
1118 dev_fsm_event(cdev, dev_event);
1119}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121static void
1122ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1123{
1124 ccw_device_set_timeout(cdev, 0);
1125 if (dev_event == DEV_EVENT_NOTOPER)
1126 cdev->private->state = DEV_STATE_NOT_OPER;
1127 else
1128 cdev->private->state = DEV_STATE_OFFLINE;
1129 wake_up(&cdev->private->wait_q);
1130}
1131
1132static void
1133ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1134{
1135 int ret;
1136
1137 ret = ccw_device_cancel_halt_clear(cdev);
1138 switch (ret) {
1139 case 0:
1140 cdev->private->state = DEV_STATE_OFFLINE;
1141 wake_up(&cdev->private->wait_q);
1142 break;
1143 case -ENODEV:
1144 cdev->private->state = DEV_STATE_NOT_OPER;
1145 wake_up(&cdev->private->wait_q);
1146 break;
1147 default:
1148 ccw_device_set_timeout(cdev, HZ/10);
1149 }
1150}
1151
1152/*
1153 * No operation action. This is used e.g. to ignore a timeout event in
1154 * state offline.
1155 */
1156static void
1157ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1158{
1159}
1160
1161/*
1162 * Bug operation action.
1163 */
1164static void
1165ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1166{
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001167 CIO_MSG_EVENT(0, "dev_jumptable[%i][%i] == NULL\n",
1168 cdev->private->state, dev_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 BUG();
1170}
1171
1172/*
1173 * device statemachine
1174 */
1175fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1176 [DEV_STATE_NOT_OPER] = {
1177 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1178 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1179 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1180 [DEV_EVENT_VERIFY] = ccw_device_nop,
1181 },
1182 [DEV_STATE_SENSE_PGID] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001183 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1185 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1186 [DEV_EVENT_VERIFY] = ccw_device_nop,
1187 },
1188 [DEV_STATE_SENSE_ID] = {
1189 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1190 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1191 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1192 [DEV_EVENT_VERIFY] = ccw_device_nop,
1193 },
1194 [DEV_STATE_OFFLINE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001195 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1197 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1198 [DEV_EVENT_VERIFY] = ccw_device_nop,
1199 },
1200 [DEV_STATE_VERIFY] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001201 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1203 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001204 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 },
1206 [DEV_STATE_ONLINE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001207 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1209 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1210 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1211 },
1212 [DEV_STATE_W4SENSE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001213 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1215 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1216 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1217 },
1218 [DEV_STATE_DISBAND_PGID] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001219 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1221 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1222 [DEV_EVENT_VERIFY] = ccw_device_nop,
1223 },
1224 [DEV_STATE_BOXED] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001225 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1227 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1228 [DEV_EVENT_VERIFY] = ccw_device_nop,
1229 },
1230 /* states to wait for i/o completion before doing something */
1231 [DEV_STATE_CLEAR_VERIFY] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001232 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1234 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1235 [DEV_EVENT_VERIFY] = ccw_device_nop,
1236 },
1237 [DEV_STATE_TIMEOUT_KILL] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001238 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1240 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1241 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1242 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 [DEV_STATE_QUIESCE] = {
1244 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1245 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1246 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1247 [DEV_EVENT_VERIFY] = ccw_device_nop,
1248 },
1249 /* special states for devices gone not operational */
1250 [DEV_STATE_DISCONNECTED] = {
1251 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1252 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1253 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001254 [DEV_EVENT_VERIFY] = ccw_device_start_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 },
1256 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1257 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1258 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1259 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1260 [DEV_EVENT_VERIFY] = ccw_device_nop,
1261 },
1262 [DEV_STATE_CMFCHANGE] = {
1263 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1264 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1265 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1266 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1267 },
Cornelia Huck94bb0632006-06-29 15:08:41 +02001268 [DEV_STATE_CMFUPDATE] = {
1269 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1270 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1271 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1272 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1273 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274};
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276EXPORT_SYMBOL_GPL(ccw_device_set_timeout);