blob: cd31bb5177e639124fde18a3b160bb42529e0604 [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 *
Cornelia Huckc820de32008-07-14 09:58:45 +02005 * Copyright IBM Corp. 2002,2008
Cornelia Huck4ce3b302006-01-14 13:21:04 -08006 * Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Martin Schwidefsky (schwidefsky@de.ibm.com)
8 */
9
10#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/init.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080012#include <linux/jiffies.h>
13#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <asm/ccwdev.h>
Cornelia Huck4c24da72005-09-03 15:58:01 -070016#include <asm/cio.h>
Peter Oberparleitere5854a52007-04-27 16:01:31 +020017#include <asm/chpid.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include "cio.h"
20#include "cio_debug.h"
21#include "css.h"
22#include "device.h"
23#include "chsc.h"
24#include "ioasm.h"
Peter Oberparleitere6b6e102007-04-27 16:01:28 +020025#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Sebastian Ott14ff56b2008-01-26 14:10:37 +010027static int timeout_log_enabled;
28
Sebastian Ott14ff56b2008-01-26 14:10:37 +010029static int __init ccw_timeout_log_setup(char *unused)
30{
31 timeout_log_enabled = 1;
32 return 1;
33}
34
35__setup("ccw_timeout_log", ccw_timeout_log_setup);
36
37static void ccw_timeout_log(struct ccw_device *cdev)
38{
39 struct schib schib;
40 struct subchannel *sch;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +010041 struct io_subchannel_private *private;
Peter Oberparleiter83262d62008-07-14 09:58:51 +020042 union orb *orb;
Sebastian Ott14ff56b2008-01-26 14:10:37 +010043 int cc;
44
45 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +010046 private = to_io_private(sch);
Peter Oberparleiter83262d62008-07-14 09:58:51 +020047 orb = &private->orb;
Sebastian Ott14ff56b2008-01-26 14:10:37 +010048 cc = stsch(sch->schid, &schib);
49
50 printk(KERN_WARNING "cio: ccw device timeout occurred at %llx, "
51 "device information:\n", get_clock());
52 printk(KERN_WARNING "cio: orb:\n");
53 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
Peter Oberparleiter83262d62008-07-14 09:58:51 +020054 orb, sizeof(*orb), 0);
Sebastian Ott14ff56b2008-01-26 14:10:37 +010055 printk(KERN_WARNING "cio: ccw device bus id: %s\n", cdev->dev.bus_id);
56 printk(KERN_WARNING "cio: subchannel bus id: %s\n", sch->dev.bus_id);
57 printk(KERN_WARNING "cio: subchannel lpm: %02x, opm: %02x, "
58 "vpm: %02x\n", sch->lpm, sch->opm, sch->vpm);
59
Peter Oberparleiter83262d62008-07-14 09:58:51 +020060 if (orb->tm.b) {
61 printk(KERN_WARNING "cio: orb indicates transport mode\n");
62 printk(KERN_WARNING "cio: last tcw:\n");
63 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
64 (void *)(addr_t)orb->tm.tcw,
65 sizeof(struct tcw), 0);
66 } else {
67 printk(KERN_WARNING "cio: orb indicates command mode\n");
68 if ((void *)(addr_t)orb->cmd.cpa == &private->sense_ccw ||
69 (void *)(addr_t)orb->cmd.cpa == cdev->private->iccws)
70 printk(KERN_WARNING "cio: last channel program "
71 "(intern):\n");
72 else
73 printk(KERN_WARNING "cio: last channel program:\n");
Sebastian Ott14ff56b2008-01-26 14:10:37 +010074
Peter Oberparleiter83262d62008-07-14 09:58:51 +020075 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
76 (void *)(addr_t)orb->cmd.cpa,
77 sizeof(struct ccw1), 0);
78 }
Sebastian Ott14ff56b2008-01-26 14:10:37 +010079 printk(KERN_WARNING "cio: ccw device state: %d\n",
80 cdev->private->state);
81 printk(KERN_WARNING "cio: store subchannel returned: cc=%d\n", cc);
82 printk(KERN_WARNING "cio: schib:\n");
83 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
84 &schib, sizeof(schib), 0);
85 printk(KERN_WARNING "cio: ccw device flags:\n");
86 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
87 &cdev->private->flags, sizeof(cdev->private->flags), 0);
88}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/*
91 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
92 */
93static void
94ccw_device_timeout(unsigned long data)
95{
96 struct ccw_device *cdev;
97
98 cdev = (struct ccw_device *) data;
99 spin_lock_irq(cdev->ccwlock);
Sebastian Ott14ff56b2008-01-26 14:10:37 +0100100 if (timeout_log_enabled)
101 ccw_timeout_log(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
103 spin_unlock_irq(cdev->ccwlock);
104}
105
106/*
107 * Set timeout
108 */
109void
110ccw_device_set_timeout(struct ccw_device *cdev, int expires)
111{
112 if (expires == 0) {
113 del_timer(&cdev->private->timer);
114 return;
115 }
116 if (timer_pending(&cdev->private->timer)) {
117 if (mod_timer(&cdev->private->timer, jiffies + expires))
118 return;
119 }
120 cdev->private->timer.function = ccw_device_timeout;
121 cdev->private->timer.data = (unsigned long) cdev;
122 cdev->private->timer.expires = jiffies + expires;
123 add_timer(&cdev->private->timer);
124}
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126/*
127 * Cancel running i/o. This is called repeatedly since halt/clear are
128 * asynchronous operations. We do one try with cio_cancel, two tries
129 * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
130 * Returns 0 if device now idle, -ENODEV for device not operational and
131 * -EBUSY if an interrupt is expected (either from halt/clear or from a
132 * status pending).
133 */
134int
135ccw_device_cancel_halt_clear(struct ccw_device *cdev)
136{
137 struct subchannel *sch;
138 int ret;
139
140 sch = to_subchannel(cdev->dev.parent);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800141 ret = stsch(sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (ret || !sch->schib.pmcw.dnv)
143 return -ENODEV;
Cornelia Huck2470b642007-03-05 23:36:02 +0100144 if (!sch->schib.pmcw.ena)
145 /* Not operational -> done. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return 0;
147 /* Stage 1: cancel io. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200148 if (!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_HALT_PEND) &&
149 !(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_CLEAR_PEND)) {
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200150 if (!scsw_is_tm(&sch->schib.scsw)) {
151 ret = cio_cancel(sch);
152 if (ret != -EINVAL)
153 return ret;
154 }
155 /* cancel io unsuccessful or not applicable (transport mode).
156 * Continue with asynchronous instructions. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 cdev->private->iretry = 3; /* 3 halt retries. */
158 }
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200159 if (!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_CLEAR_PEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 /* Stage 2: halt io. */
161 if (cdev->private->iretry) {
162 cdev->private->iretry--;
163 ret = cio_halt(sch);
Peter Oberparleiterba4ba8a2006-07-27 14:00:23 +0200164 if (ret != -EBUSY)
165 return (ret == 0) ? -EBUSY : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167 /* halt io unsuccessful. */
168 cdev->private->iretry = 255; /* 255 clear retries. */
169 }
170 /* Stage 3: clear io. */
171 if (cdev->private->iretry) {
172 cdev->private->iretry--;
173 ret = cio_clear (sch);
174 return (ret == 0) ? -EBUSY : ret;
175 }
176 panic("Can't stop i/o on subchannel.\n");
177}
178
179static int
180ccw_device_handle_oper(struct ccw_device *cdev)
181{
182 struct subchannel *sch;
183
184 sch = to_subchannel(cdev->dev.parent);
185 cdev->private->flags.recog_done = 1;
186 /*
187 * Check if cu type and device type still match. If
188 * not, it is certainly another device and we have to
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100189 * de- and re-register.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 */
191 if (cdev->id.cu_type != cdev->private->senseid.cu_type ||
192 cdev->id.cu_model != cdev->private->senseid.cu_model ||
193 cdev->id.dev_type != cdev->private->senseid.dev_type ||
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100194 cdev->id.dev_model != cdev->private->senseid.dev_model) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100196 ccw_device_do_unreg_rereg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 queue_work(ccw_device_work, &cdev->private->kick_work);
198 return 0;
199 }
200 cdev->private->flags.donotify = 1;
201 return 1;
202}
203
204/*
205 * The machine won't give us any notification by machine check if a chpid has
206 * been varied online on the SE so we have to find out by magic (i. e. driving
207 * the channel subsystem to device selection and updating our path masks).
208 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100209static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210__recover_lost_chpids(struct subchannel *sch, int old_lpm)
211{
212 int mask, i;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200213 struct chp_id chpid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200215 chp_id_init(&chpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 for (i = 0; i<8; i++) {
217 mask = 0x80 >> i;
218 if (!(sch->lpm & mask))
219 continue;
220 if (old_lpm & mask)
221 continue;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200222 chpid.id = sch->schib.pmcw.chpid[i];
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200223 if (!chp_is_registered(chpid))
224 css_schedule_eval_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226}
227
228/*
229 * Stop device recognition.
230 */
231static void
232ccw_device_recog_done(struct ccw_device *cdev, int state)
233{
234 struct subchannel *sch;
235 int notify, old_lpm, same_dev;
236
237 sch = to_subchannel(cdev->dev.parent);
238
239 ccw_device_set_timeout(cdev, 0);
240 cio_disable_subchannel(sch);
241 /*
242 * Now that we tried recognition, we have performed device selection
243 * through ssch() and the path information is up to date.
244 */
245 old_lpm = sch->lpm;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800246 stsch(sch->schid, &sch->schib);
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200247 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck4ffa9232005-07-29 14:03:37 -0700248 /* Check since device may again have become not operational. */
249 if (!sch->schib.pmcw.dnv)
250 state = DEV_STATE_NOT_OPER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
252 /* Force reprobe on all chpids. */
253 old_lpm = 0;
254 if (sch->lpm != old_lpm)
255 __recover_lost_chpids(sch, old_lpm);
256 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
257 if (state == DEV_STATE_NOT_OPER) {
258 cdev->private->flags.recog_done = 1;
259 cdev->private->state = DEV_STATE_DISCONNECTED;
260 return;
261 }
262 /* Boxed devices don't need extra treatment. */
263 }
264 notify = 0;
265 same_dev = 0; /* Keep the compiler quiet... */
266 switch (state) {
267 case DEV_STATE_NOT_OPER:
Michael Ernst139b83d2008-05-07 09:22:54 +0200268 CIO_MSG_EVENT(2, "SenseID : unknown device %04x on "
269 "subchannel 0.%x.%04x\n",
270 cdev->private->dev_id.devno,
271 sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 break;
273 case DEV_STATE_OFFLINE:
274 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
275 same_dev = ccw_device_handle_oper(cdev);
276 notify = 1;
277 }
278 /* fill out sense information */
Heiko Carstens81388d22006-09-20 15:59:17 +0200279 memset(&cdev->id, 0, sizeof(cdev->id));
Heiko Carstens292888c2006-08-30 14:33:35 +0200280 cdev->id.cu_type = cdev->private->senseid.cu_type;
281 cdev->id.cu_model = cdev->private->senseid.cu_model;
282 cdev->id.dev_type = cdev->private->senseid.dev_type;
283 cdev->id.dev_model = cdev->private->senseid.dev_model;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (notify) {
285 cdev->private->state = DEV_STATE_OFFLINE;
286 if (same_dev) {
287 /* Get device online again. */
288 ccw_device_online(cdev);
289 wake_up(&cdev->private->wait_q);
290 }
291 return;
292 }
293 /* Issue device info message. */
Michael Ernst139b83d2008-05-07 09:22:54 +0200294 CIO_MSG_EVENT(4, "SenseID : device 0.%x.%04x reports: "
295 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
296 "%04X/%02X\n",
297 cdev->private->dev_id.ssid,
298 cdev->private->dev_id.devno,
299 cdev->id.cu_type, cdev->id.cu_model,
300 cdev->id.dev_type, cdev->id.dev_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 break;
302 case DEV_STATE_BOXED:
Michael Ernst139b83d2008-05-07 09:22:54 +0200303 CIO_MSG_EVENT(0, "SenseID : boxed device %04x on "
304 " subchannel 0.%x.%04x\n",
305 cdev->private->dev_id.devno,
306 sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 break;
308 }
309 cdev->private->state = state;
310 io_subchannel_recog_done(cdev);
311 if (state != DEV_STATE_NOT_OPER)
312 wake_up(&cdev->private->wait_q);
313}
314
315/*
316 * Function called from device_id.c after sense id has completed.
317 */
318void
319ccw_device_sense_id_done(struct ccw_device *cdev, int err)
320{
321 switch (err) {
322 case 0:
323 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
324 break;
325 case -ETIME: /* Sense id stopped by timeout. */
326 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
327 break;
328 default:
329 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
330 break;
331 }
332}
333
Cornelia Huckc820de32008-07-14 09:58:45 +0200334int ccw_device_notify(struct ccw_device *cdev, int event)
335{
336 if (!cdev->drv)
337 return 0;
338 if (!cdev->online)
339 return 0;
340 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
341}
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100344ccw_device_oper_notify(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100346 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 int ret;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100349 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100351 priv = container_of(work, struct ccw_device_private, kick_work);
352 cdev = priv->cdev;
Cornelia Huckc820de32008-07-14 09:58:45 +0200353 ret = ccw_device_notify(cdev, CIO_OPER);
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100354 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100355 if (ret) {
356 /* Reenable channel measurements, if needed. */
357 spin_unlock_irqrestore(cdev->ccwlock, flags);
358 cmf_reenable(cdev);
359 spin_lock_irqsave(cdev->ccwlock, flags);
360 wake_up(&cdev->private->wait_q);
361 }
362 spin_unlock_irqrestore(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (!ret)
364 /* Driver doesn't want device back. */
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100365 ccw_device_do_unreg_rereg(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
368/*
369 * Finished with online/offline processing.
370 */
371static void
372ccw_device_done(struct ccw_device *cdev, int state)
373{
374 struct subchannel *sch;
375
376 sch = to_subchannel(cdev->dev.parent);
377
Cornelia Huckf1ee3282006-10-04 20:02:02 +0200378 ccw_device_set_timeout(cdev, 0);
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (state != DEV_STATE_ONLINE)
381 cio_disable_subchannel(sch);
382
383 /* Reset device status. */
384 memset(&cdev->private->irb, 0, sizeof(struct irb));
385
386 cdev->private->state = state;
387
388
389 if (state == DEV_STATE_BOXED)
Michael Ernst139b83d2008-05-07 09:22:54 +0200390 CIO_MSG_EVENT(0, "Boxed device %04x on subchannel %04x\n",
391 cdev->private->dev_id.devno, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 if (cdev->private->flags.donotify) {
394 cdev->private->flags.donotify = 0;
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100395 PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
397 }
398 wake_up(&cdev->private->wait_q);
399
400 if (css_init_done && state != DEV_STATE_ONLINE)
401 put_device (&cdev->dev);
402}
403
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100404static int cmp_pgid(struct pgid *p1, struct pgid *p2)
Cornelia Huck7e560812006-07-12 16:40:19 +0200405{
406 char *c1;
407 char *c2;
408
409 c1 = (char *)p1;
410 c2 = (char *)p2;
411
412 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
413}
414
415static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
416{
417 int i;
418 int last;
419
420 last = 0;
421 for (i = 0; i < 8; i++) {
422 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
423 /* No PGID yet */
424 continue;
425 if (cdev->private->pgid[last].inf.ps.state1 ==
426 SNID_STATE1_RESET) {
427 /* First non-zero PGID */
428 last = i;
429 continue;
430 }
431 if (cmp_pgid(&cdev->private->pgid[i],
432 &cdev->private->pgid[last]) == 0)
433 /* Non-conflicting PGIDs */
434 continue;
435
436 /* PGID mismatch, can't pathgroup. */
437 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
438 "0.%x.%04x, can't pathgroup\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200439 cdev->private->dev_id.ssid,
440 cdev->private->dev_id.devno);
Cornelia Huck7e560812006-07-12 16:40:19 +0200441 cdev->private->options.pgroup = 0;
442 return;
443 }
444 if (cdev->private->pgid[last].inf.ps.state1 ==
445 SNID_STATE1_RESET)
446 /* No previous pgid found */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200447 memcpy(&cdev->private->pgid[0],
448 &channel_subsystems[0]->global_pgid,
Cornelia Huck7e560812006-07-12 16:40:19 +0200449 sizeof(struct pgid));
450 else
451 /* Use existing pgid */
452 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
453 sizeof(struct pgid));
454}
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456/*
457 * Function called from device_pgid.c after sense path ground has completed.
458 */
459void
460ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
461{
462 struct subchannel *sch;
463
464 sch = to_subchannel(cdev->dev.parent);
465 switch (err) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200466 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
467 cdev->private->options.pgroup = 0;
468 break;
469 case 0: /* success */
470 case -EACCES: /* partial success, some paths not operational */
471 /* Check if all pgids are equal or 0. */
472 __ccw_device_get_common_pgid(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 break;
474 case -ETIME: /* Sense path group id stopped by timeout. */
475 case -EUSERS: /* device is reserved for someone else. */
476 ccw_device_done(cdev, DEV_STATE_BOXED);
Cornelia Huck7e560812006-07-12 16:40:19 +0200477 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 default:
479 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Cornelia Huck7e560812006-07-12 16:40:19 +0200480 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
Cornelia Huck7e560812006-07-12 16:40:19 +0200482 /* Start Path Group verification. */
Cornelia Huck7e560812006-07-12 16:40:19 +0200483 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200484 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200485 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
488/*
489 * Start device recognition.
490 */
491int
492ccw_device_recognition(struct ccw_device *cdev)
493{
494 struct subchannel *sch;
495 int ret;
496
497 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
498 (cdev->private->state != DEV_STATE_BOXED))
499 return -EINVAL;
500 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckedf22092008-04-30 13:38:39 +0200501 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (ret != 0)
503 /* Couldn't enable the subchannel for i/o. Sick device. */
504 return ret;
505
506 /* After 60s the device recognition is considered to have failed. */
507 ccw_device_set_timeout(cdev, 60*HZ);
508
509 /*
510 * We used to start here with a sense pgid to find out whether a device
511 * is locked by someone else. Unfortunately, the sense pgid command
512 * code has other meanings on devices predating the path grouping
513 * algorithm, so we start with sense id and box the device after an
514 * timeout (or if sense pgid during path verification detects the device
515 * is locked, as may happen on newer devices).
516 */
517 cdev->private->flags.recog_done = 0;
518 cdev->private->state = DEV_STATE_SENSE_ID;
519 ccw_device_sense_id_start(cdev);
520 return 0;
521}
522
523/*
524 * Handle timeout in device recognition.
525 */
526static void
527ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
528{
529 int ret;
530
531 ret = ccw_device_cancel_halt_clear(cdev);
532 switch (ret) {
533 case 0:
534 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
535 break;
536 case -ENODEV:
537 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
538 break;
539 default:
540 ccw_device_set_timeout(cdev, 3*HZ);
541 }
542}
543
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545void
546ccw_device_verify_done(struct ccw_device *cdev, int err)
547{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200548 struct subchannel *sch;
549
550 sch = to_subchannel(cdev->dev.parent);
551 /* Update schib - pom may have changed. */
552 stsch(sch->schid, &sch->schib);
553 /* Update lpm with verified path mask. */
554 sch->lpm = sch->vpm;
555 /* Repeat path verification? */
556 if (cdev->private->flags.doverify) {
557 cdev->private->flags.doverify = 0;
558 ccw_device_verify_start(cdev);
559 return;
560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 switch (err) {
562 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
563 cdev->private->options.pgroup = 0;
564 case 0:
565 ccw_device_done(cdev, DEV_STATE_ONLINE);
566 /* Deliver fake irb to device driver, if needed. */
567 if (cdev->private->flags.fake_irb) {
568 memset(&cdev->private->irb, 0, sizeof(struct irb));
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200569 cdev->private->irb.scsw.cmd.cc = 1;
570 cdev->private->irb.scsw.cmd.fctl = SCSW_FCTL_START_FUNC;
571 cdev->private->irb.scsw.cmd.actl = SCSW_ACTL_START_PEND;
572 cdev->private->irb.scsw.cmd.stctl =
573 SCSW_STCTL_STATUS_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 cdev->private->flags.fake_irb = 0;
575 if (cdev->handler)
576 cdev->handler(cdev, cdev->private->intparm,
577 &cdev->private->irb);
578 memset(&cdev->private->irb, 0, sizeof(struct irb));
579 }
580 break;
581 case -ETIME:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200582 /* Reset oper notify indication after verify error. */
583 cdev->private->flags.donotify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 ccw_device_done(cdev, DEV_STATE_BOXED);
585 break;
586 default:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200587 /* Reset oper notify indication after verify error. */
588 cdev->private->flags.donotify = 0;
Cornelia Huck46258ab2008-01-26 14:10:49 +0100589 if (cdev->online) {
590 ccw_device_set_timeout(cdev, 0);
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200591 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Cornelia Huck46258ab2008-01-26 14:10:49 +0100592 } else
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100593 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 break;
595 }
596}
597
598/*
599 * Get device online.
600 */
601int
602ccw_device_online(struct ccw_device *cdev)
603{
604 struct subchannel *sch;
605 int ret;
606
607 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
608 (cdev->private->state != DEV_STATE_BOXED))
609 return -EINVAL;
610 sch = to_subchannel(cdev->dev.parent);
611 if (css_init_done && !get_device(&cdev->dev))
612 return -ENODEV;
Cornelia Huckedf22092008-04-30 13:38:39 +0200613 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (ret != 0) {
615 /* Couldn't enable the subchannel for i/o. Sick device. */
616 if (ret == -ENODEV)
617 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
618 return ret;
619 }
620 /* Do we want to do path grouping? */
621 if (!cdev->private->options.pgroup) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200622 /* Start initial path verification. */
623 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200624 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200625 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return 0;
627 }
628 /* Do a SensePGID first. */
629 cdev->private->state = DEV_STATE_SENSE_PGID;
630 ccw_device_sense_pgid_start(cdev);
631 return 0;
632}
633
634void
635ccw_device_disband_done(struct ccw_device *cdev, int err)
636{
637 switch (err) {
638 case 0:
639 ccw_device_done(cdev, DEV_STATE_OFFLINE);
640 break;
641 case -ETIME:
642 ccw_device_done(cdev, DEV_STATE_BOXED);
643 break;
644 default:
Peter Oberparleiter3ecb0a52007-05-31 17:38:07 +0200645 cdev->private->flags.donotify = 0;
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200646 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
648 break;
649 }
650}
651
652/*
653 * Shutdown device.
654 */
655int
656ccw_device_offline(struct ccw_device *cdev)
657{
658 struct subchannel *sch;
659
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100660 if (ccw_device_is_orphan(cdev)) {
661 ccw_device_done(cdev, DEV_STATE_OFFLINE);
662 return 0;
663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 sch = to_subchannel(cdev->dev.parent);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800665 if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return -ENODEV;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200667 if (scsw_actl(&sch->schib.scsw) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return -EBUSY;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200669 if (cdev->private->state != DEV_STATE_ONLINE)
670 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /* Are we doing path grouping? */
672 if (!cdev->private->options.pgroup) {
673 /* No, set state offline immediately. */
674 ccw_device_done(cdev, DEV_STATE_OFFLINE);
675 return 0;
676 }
677 /* Start Set Path Group commands. */
678 cdev->private->state = DEV_STATE_DISBAND_PGID;
679 ccw_device_disband_start(cdev);
680 return 0;
681}
682
683/*
684 * Handle timeout in device online/offline process.
685 */
686static void
687ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
688{
689 int ret;
690
691 ret = ccw_device_cancel_halt_clear(cdev);
692 switch (ret) {
693 case 0:
694 ccw_device_done(cdev, DEV_STATE_BOXED);
695 break;
696 case -ENODEV:
697 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
698 break;
699 default:
700 ccw_device_set_timeout(cdev, 3*HZ);
701 }
702}
703
704/*
705 * Handle not oper event in device recognition.
706 */
707static void
708ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
709{
710 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
711}
712
713/*
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200714 * Handle not operational event in non-special state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 */
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200716static void ccw_device_generic_notoper(struct ccw_device *cdev,
717 enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
719 struct subchannel *sch;
720
721 cdev->private->state = DEV_STATE_NOT_OPER;
722 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200723 css_schedule_eval(sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
726/*
727 * Handle path verification event.
728 */
729static void
730ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
731{
732 struct subchannel *sch;
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (cdev->private->state == DEV_STATE_W4SENSE) {
735 cdev->private->flags.doverify = 1;
736 return;
737 }
738 sch = to_subchannel(cdev->dev.parent);
739 /*
740 * Since we might not just be coming from an interrupt from the
741 * subchannel we have to update the schib.
742 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800743 stsch(sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200745 if (scsw_actl(&sch->schib.scsw) != 0 ||
746 (scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_STATUS_PEND) ||
747 (scsw_stctl(&cdev->private->irb.scsw) & SCSW_STCTL_STATUS_PEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /*
749 * No final status yet or final status not yet delivered
750 * to the device driver. Can't do path verfication now,
751 * delay until final status was delivered.
752 */
753 cdev->private->flags.doverify = 1;
754 return;
755 }
756 /* Device is idle, we can do the path verification. */
757 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200758 cdev->private->flags.doverify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 ccw_device_verify_start(cdev);
760}
761
762/*
763 * Got an interrupt for a normal io (state online).
764 */
765static void
766ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
767{
768 struct irb *irb;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200769 int is_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 irb = (struct irb *) __LC_IRB;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200772 is_cmd = !scsw_is_tm(&irb->scsw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 /* Check for unsolicited interrupt. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200774 if (!scsw_is_solicited(&irb->scsw)) {
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200775 if (is_cmd && (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 !irb->esw.esw0.erw.cons) {
777 /* Unit check but no sense data. Need basic sense. */
778 if (ccw_device_do_sense(cdev, irb) != 0)
779 goto call_handler_unsol;
Cornelia Hucke0ec5742006-06-04 02:51:27 -0700780 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 cdev->private->state = DEV_STATE_W4SENSE;
782 cdev->private->intparm = 0;
783 return;
784 }
785call_handler_unsol:
786 if (cdev->handler)
787 cdev->handler (cdev, 0, irb);
Cornelia Huck18374d32007-02-05 21:17:09 +0100788 if (cdev->private->flags.doverify)
789 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return;
791 }
792 /* Accumulate status and find out if a basic sense is needed. */
793 ccw_device_accumulate_irb(cdev, irb);
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200794 if (is_cmd && cdev->private->flags.dosense) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (ccw_device_do_sense(cdev, irb) == 0) {
796 cdev->private->state = DEV_STATE_W4SENSE;
797 }
798 return;
799 }
800 /* Call the handler. */
801 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
802 /* Start delayed path verification. */
803 ccw_device_online_verify(cdev, 0);
804}
805
806/*
807 * Got an timeout in online state.
808 */
809static void
810ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
811{
812 int ret;
813
814 ccw_device_set_timeout(cdev, 0);
815 ret = ccw_device_cancel_halt_clear(cdev);
816 if (ret == -EBUSY) {
817 ccw_device_set_timeout(cdev, 3*HZ);
818 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
819 return;
820 }
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200821 if (ret == -ENODEV)
822 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
823 else if (cdev->handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 cdev->handler(cdev, cdev->private->intparm,
825 ERR_PTR(-ETIMEDOUT));
826}
827
828/*
829 * Got an interrupt for a basic sense.
830 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100831static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
833{
834 struct irb *irb;
835
836 irb = (struct irb *) __LC_IRB;
837 /* Check for unsolicited interrupt. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200838 if (scsw_stctl(&irb->scsw) ==
839 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
840 if (scsw_cc(&irb->scsw) == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 /* Basic sense hasn't started. Try again. */
842 ccw_device_do_sense(cdev, irb);
843 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200844 CIO_MSG_EVENT(0, "0.%x.%04x: unsolicited "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200845 "interrupt during w4sense...\n",
846 cdev->private->dev_id.ssid,
847 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (cdev->handler)
849 cdev->handler (cdev, 0, irb);
850 }
851 return;
852 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800853 /*
854 * Check if a halt or clear has been issued in the meanwhile. If yes,
855 * only deliver the halt/clear interrupt to the device driver as if it
856 * had killed the original request.
857 */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200858 if (scsw_fctl(&irb->scsw) &
859 (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
Cornelia Huckd23861f2006-12-04 15:41:04 +0100860 /* Retry Basic Sense if requested. */
861 if (cdev->private->flags.intretry) {
862 cdev->private->flags.intretry = 0;
863 ccw_device_do_sense(cdev, irb);
864 return;
865 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800866 cdev->private->flags.dosense = 0;
867 memset(&cdev->private->irb, 0, sizeof(struct irb));
868 ccw_device_accumulate_irb(cdev, irb);
869 goto call_handler;
870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 /* Add basic sense info to irb. */
872 ccw_device_accumulate_basic_sense(cdev, irb);
873 if (cdev->private->flags.dosense) {
874 /* Another basic sense is needed. */
875 ccw_device_do_sense(cdev, irb);
876 return;
877 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800878call_handler:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 cdev->private->state = DEV_STATE_ONLINE;
880 /* Call the handler. */
881 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
882 /* Start delayed path verification. */
883 ccw_device_online_verify(cdev, 0);
884}
885
886static void
887ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
888{
889 struct irb *irb;
890
891 irb = (struct irb *) __LC_IRB;
892 /* Accumulate status. We don't do basic sense. */
893 ccw_device_accumulate_irb(cdev, irb);
Cornelia Huckb4f7b1e2006-06-29 15:03:35 +0200894 /* Remember to clear irb to avoid residuals. */
895 memset(&cdev->private->irb, 0, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 /* Try to start delayed device verification. */
897 ccw_device_online_verify(cdev, 0);
898 /* Note: Don't call handler for cio initiated clear! */
899}
900
901static void
902ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
903{
904 struct subchannel *sch;
905
906 sch = to_subchannel(cdev->dev.parent);
907 ccw_device_set_timeout(cdev, 0);
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100908 /* Start delayed path verification. */
909 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 /* OK, i/o is dead now. Call interrupt handler. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (cdev->handler)
912 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200913 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
916static void
917ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
918{
919 int ret;
920
921 ret = ccw_device_cancel_halt_clear(cdev);
922 if (ret == -EBUSY) {
923 ccw_device_set_timeout(cdev, 3*HZ);
924 return;
925 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100926 /* Start delayed path verification. */
927 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (cdev->handler)
929 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200930 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
Cornelia Huckc820de32008-07-14 09:58:45 +0200933void ccw_device_kill_io(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
935 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 ret = ccw_device_cancel_halt_clear(cdev);
938 if (ret == -EBUSY) {
939 ccw_device_set_timeout(cdev, 3*HZ);
940 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
941 return;
942 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100943 /* Start delayed path verification. */
944 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (cdev->handler)
946 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200947 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
950static void
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200951ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200953 /* Start verification after current task finished. */
Cornelia Huck7e560812006-07-12 16:40:19 +0200954 cdev->private->flags.doverify = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
957static void
958ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
959{
960 struct irb *irb;
961
962 switch (dev_event) {
963 case DEV_EVENT_INTERRUPT:
964 irb = (struct irb *) __LC_IRB;
965 /* Check for unsolicited interrupt. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200966 if ((scsw_stctl(&irb->scsw) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200968 (!scsw_cc(&irb->scsw)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 /* FIXME: we should restart stlck here, but this
970 * is extremely unlikely ... */
971 goto out_wakeup;
972
973 ccw_device_accumulate_irb(cdev, irb);
974 /* We don't care about basic sense etc. */
975 break;
976 default: /* timeout */
977 break;
978 }
979out_wakeup:
980 wake_up(&cdev->private->wait_q);
981}
982
983static void
984ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
985{
986 struct subchannel *sch;
987
988 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckedf22092008-04-30 13:38:39 +0200989 if (cio_enable_subchannel(sch, (u32)(addr_t)sch) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 /* Couldn't enable the subchannel for i/o. Sick device. */
991 return;
992
993 /* After 60s the device recognition is considered to have failed. */
994 ccw_device_set_timeout(cdev, 60*HZ);
995
996 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
997 ccw_device_sense_id_start(cdev);
998}
999
Cornelia Huckc820de32008-07-14 09:58:45 +02001000void ccw_device_trigger_reprobe(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Cornelia Huckc820de32008-07-14 09:58:45 +02001002 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1005 return;
1006
Cornelia Huckc820de32008-07-14 09:58:45 +02001007 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /* Update some values. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001009 if (stsch(sch->schid, &sch->schib))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return;
Cornelia Huck7674da72006-12-08 15:54:21 +01001011 if (!sch->schib.pmcw.dnv)
1012 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 /*
1014 * The pim, pam, pom values may not be accurate, but they are the best
1015 * we have before performing device selection :/
1016 */
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001017 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 /* Re-set some bits in the pmcw that were lost. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 sch->schib.pmcw.csense = 1;
1020 sch->schib.pmcw.ena = 0;
1021 if ((sch->lpm & (sch->lpm - 1)) != 0)
1022 sch->schib.pmcw.mp = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /* We should also udate ssd info, but this has to wait. */
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001024 /* Check if this is another device which appeared on the same sch. */
1025 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1026 PREPARE_WORK(&cdev->private->kick_work,
1027 ccw_device_move_to_orphanage);
Cornelia Huckc5d4a992007-11-20 11:13:41 +01001028 queue_work(slow_path_wq, &cdev->private->kick_work);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001029 } else
1030 ccw_device_start_id(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
1033static void
1034ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1035{
1036 struct subchannel *sch;
1037
1038 sch = to_subchannel(cdev->dev.parent);
1039 /*
1040 * An interrupt in state offline means a previous disable was not
1041 * successful. Try again.
1042 */
1043 cio_disable_subchannel(sch);
1044}
1045
1046static void
1047ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1048{
1049 retry_set_schib(cdev);
1050 cdev->private->state = DEV_STATE_ONLINE;
1051 dev_fsm_event(cdev, dev_event);
1052}
1053
Cornelia Huck94bb0632006-06-29 15:08:41 +02001054static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1055 enum dev_event dev_event)
1056{
1057 cmf_retry_copy_block(cdev);
1058 cdev->private->state = DEV_STATE_ONLINE;
1059 dev_fsm_event(cdev, dev_event);
1060}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062static void
1063ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1064{
1065 ccw_device_set_timeout(cdev, 0);
1066 if (dev_event == DEV_EVENT_NOTOPER)
1067 cdev->private->state = DEV_STATE_NOT_OPER;
1068 else
1069 cdev->private->state = DEV_STATE_OFFLINE;
1070 wake_up(&cdev->private->wait_q);
1071}
1072
1073static void
1074ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1075{
1076 int ret;
1077
1078 ret = ccw_device_cancel_halt_clear(cdev);
1079 switch (ret) {
1080 case 0:
1081 cdev->private->state = DEV_STATE_OFFLINE;
1082 wake_up(&cdev->private->wait_q);
1083 break;
1084 case -ENODEV:
1085 cdev->private->state = DEV_STATE_NOT_OPER;
1086 wake_up(&cdev->private->wait_q);
1087 break;
1088 default:
1089 ccw_device_set_timeout(cdev, HZ/10);
1090 }
1091}
1092
1093/*
1094 * No operation action. This is used e.g. to ignore a timeout event in
1095 * state offline.
1096 */
1097static void
1098ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1099{
1100}
1101
1102/*
1103 * Bug operation action.
1104 */
1105static void
1106ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1107{
Michael Ernst139b83d2008-05-07 09:22:54 +02001108 CIO_MSG_EVENT(0, "Internal state [%i][%i] not handled for device "
1109 "0.%x.%04x\n", cdev->private->state, dev_event,
1110 cdev->private->dev_id.ssid,
1111 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 BUG();
1113}
1114
1115/*
1116 * device statemachine
1117 */
1118fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1119 [DEV_STATE_NOT_OPER] = {
1120 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1121 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1122 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1123 [DEV_EVENT_VERIFY] = ccw_device_nop,
1124 },
1125 [DEV_STATE_SENSE_PGID] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001126 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1128 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1129 [DEV_EVENT_VERIFY] = ccw_device_nop,
1130 },
1131 [DEV_STATE_SENSE_ID] = {
1132 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1133 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1134 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1135 [DEV_EVENT_VERIFY] = ccw_device_nop,
1136 },
1137 [DEV_STATE_OFFLINE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001138 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1140 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1141 [DEV_EVENT_VERIFY] = ccw_device_nop,
1142 },
1143 [DEV_STATE_VERIFY] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001144 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1146 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001147 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 },
1149 [DEV_STATE_ONLINE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001150 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1152 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1153 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1154 },
1155 [DEV_STATE_W4SENSE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001156 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1158 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1159 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1160 },
1161 [DEV_STATE_DISBAND_PGID] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001162 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1164 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1165 [DEV_EVENT_VERIFY] = ccw_device_nop,
1166 },
1167 [DEV_STATE_BOXED] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001168 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1170 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1171 [DEV_EVENT_VERIFY] = ccw_device_nop,
1172 },
1173 /* states to wait for i/o completion before doing something */
1174 [DEV_STATE_CLEAR_VERIFY] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001175 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1177 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1178 [DEV_EVENT_VERIFY] = ccw_device_nop,
1179 },
1180 [DEV_STATE_TIMEOUT_KILL] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001181 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1183 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1184 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1185 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 [DEV_STATE_QUIESCE] = {
1187 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1188 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1189 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1190 [DEV_EVENT_VERIFY] = ccw_device_nop,
1191 },
1192 /* special states for devices gone not operational */
1193 [DEV_STATE_DISCONNECTED] = {
1194 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1195 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1196 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001197 [DEV_EVENT_VERIFY] = ccw_device_start_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 },
1199 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1200 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1201 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1202 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1203 [DEV_EVENT_VERIFY] = ccw_device_nop,
1204 },
1205 [DEV_STATE_CMFCHANGE] = {
1206 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1207 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1208 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1209 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1210 },
Cornelia Huck94bb0632006-06-29 15:08:41 +02001211 [DEV_STATE_CMFUPDATE] = {
1212 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1213 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1214 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1215 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1216 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217};
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219EXPORT_SYMBOL_GPL(ccw_device_set_timeout);