blob: 8df5eaafc5ab6a4c4518f5eacaa3b22e680e5af0 [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);
Kay Sievers2a0217d2008-10-10 21:33:09 +020055 printk(KERN_WARNING "cio: ccw device bus id: %s\n",
56 dev_name(&cdev->dev));
57 printk(KERN_WARNING "cio: subchannel bus id: %s\n",
58 dev_name(&sch->dev));
Sebastian Ott14ff56b2008-01-26 14:10:37 +010059 printk(KERN_WARNING "cio: subchannel lpm: %02x, opm: %02x, "
60 "vpm: %02x\n", sch->lpm, sch->opm, sch->vpm);
61
Peter Oberparleiter83262d62008-07-14 09:58:51 +020062 if (orb->tm.b) {
63 printk(KERN_WARNING "cio: orb indicates transport mode\n");
64 printk(KERN_WARNING "cio: last tcw:\n");
65 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
66 (void *)(addr_t)orb->tm.tcw,
67 sizeof(struct tcw), 0);
68 } else {
69 printk(KERN_WARNING "cio: orb indicates command mode\n");
70 if ((void *)(addr_t)orb->cmd.cpa == &private->sense_ccw ||
71 (void *)(addr_t)orb->cmd.cpa == cdev->private->iccws)
72 printk(KERN_WARNING "cio: last channel program "
73 "(intern):\n");
74 else
75 printk(KERN_WARNING "cio: last channel program:\n");
Sebastian Ott14ff56b2008-01-26 14:10:37 +010076
Peter Oberparleiter83262d62008-07-14 09:58:51 +020077 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
78 (void *)(addr_t)orb->cmd.cpa,
79 sizeof(struct ccw1), 0);
80 }
Sebastian Ott14ff56b2008-01-26 14:10:37 +010081 printk(KERN_WARNING "cio: ccw device state: %d\n",
82 cdev->private->state);
83 printk(KERN_WARNING "cio: store subchannel returned: cc=%d\n", cc);
84 printk(KERN_WARNING "cio: schib:\n");
85 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
86 &schib, sizeof(schib), 0);
87 printk(KERN_WARNING "cio: ccw device flags:\n");
88 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
89 &cdev->private->flags, sizeof(cdev->private->flags), 0);
90}
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
94 */
95static void
96ccw_device_timeout(unsigned long data)
97{
98 struct ccw_device *cdev;
99
100 cdev = (struct ccw_device *) data;
101 spin_lock_irq(cdev->ccwlock);
Sebastian Ott14ff56b2008-01-26 14:10:37 +0100102 if (timeout_log_enabled)
103 ccw_timeout_log(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
105 spin_unlock_irq(cdev->ccwlock);
106}
107
108/*
109 * Set timeout
110 */
111void
112ccw_device_set_timeout(struct ccw_device *cdev, int expires)
113{
114 if (expires == 0) {
115 del_timer(&cdev->private->timer);
116 return;
117 }
118 if (timer_pending(&cdev->private->timer)) {
119 if (mod_timer(&cdev->private->timer, jiffies + expires))
120 return;
121 }
122 cdev->private->timer.function = ccw_device_timeout;
123 cdev->private->timer.data = (unsigned long) cdev;
124 cdev->private->timer.expires = jiffies + expires;
125 add_timer(&cdev->private->timer);
126}
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/*
129 * Cancel running i/o. This is called repeatedly since halt/clear are
130 * asynchronous operations. We do one try with cio_cancel, two tries
131 * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
132 * Returns 0 if device now idle, -ENODEV for device not operational and
133 * -EBUSY if an interrupt is expected (either from halt/clear or from a
134 * status pending).
135 */
136int
137ccw_device_cancel_halt_clear(struct ccw_device *cdev)
138{
139 struct subchannel *sch;
140 int ret;
141
142 sch = to_subchannel(cdev->dev.parent);
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100143 if (cio_update_schib(sch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return -ENODEV;
Cornelia Huck2470b642007-03-05 23:36:02 +0100145 if (!sch->schib.pmcw.ena)
146 /* Not operational -> done. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return 0;
148 /* Stage 1: cancel io. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200149 if (!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_HALT_PEND) &&
150 !(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_CLEAR_PEND)) {
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200151 if (!scsw_is_tm(&sch->schib.scsw)) {
152 ret = cio_cancel(sch);
153 if (ret != -EINVAL)
154 return ret;
155 }
156 /* cancel io unsuccessful or not applicable (transport mode).
157 * Continue with asynchronous instructions. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 cdev->private->iretry = 3; /* 3 halt retries. */
159 }
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200160 if (!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_CLEAR_PEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 /* Stage 2: halt io. */
162 if (cdev->private->iretry) {
163 cdev->private->iretry--;
164 ret = cio_halt(sch);
Peter Oberparleiterba4ba8a2006-07-27 14:00:23 +0200165 if (ret != -EBUSY)
166 return (ret == 0) ? -EBUSY : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168 /* halt io unsuccessful. */
169 cdev->private->iretry = 255; /* 255 clear retries. */
170 }
171 /* Stage 3: clear io. */
172 if (cdev->private->iretry) {
173 cdev->private->iretry--;
174 ret = cio_clear (sch);
175 return (ret == 0) ? -EBUSY : ret;
176 }
177 panic("Can't stop i/o on subchannel.\n");
178}
179
180static int
181ccw_device_handle_oper(struct ccw_device *cdev)
182{
183 struct subchannel *sch;
184
185 sch = to_subchannel(cdev->dev.parent);
186 cdev->private->flags.recog_done = 1;
187 /*
188 * Check if cu type and device type still match. If
189 * not, it is certainly another device and we have to
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100190 * de- and re-register.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 */
192 if (cdev->id.cu_type != cdev->private->senseid.cu_type ||
193 cdev->id.cu_model != cdev->private->senseid.cu_model ||
194 cdev->id.dev_type != cdev->private->senseid.dev_type ||
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100195 cdev->id.dev_model != cdev->private->senseid.dev_model) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100197 ccw_device_do_unreg_rereg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 queue_work(ccw_device_work, &cdev->private->kick_work);
199 return 0;
200 }
201 cdev->private->flags.donotify = 1;
202 return 1;
203}
204
205/*
206 * The machine won't give us any notification by machine check if a chpid has
207 * been varied online on the SE so we have to find out by magic (i. e. driving
208 * the channel subsystem to device selection and updating our path masks).
209 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100210static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211__recover_lost_chpids(struct subchannel *sch, int old_lpm)
212{
213 int mask, i;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200214 struct chp_id chpid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200216 chp_id_init(&chpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 for (i = 0; i<8; i++) {
218 mask = 0x80 >> i;
219 if (!(sch->lpm & mask))
220 continue;
221 if (old_lpm & mask)
222 continue;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200223 chpid.id = sch->schib.pmcw.chpid[i];
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200224 if (!chp_is_registered(chpid))
225 css_schedule_eval_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227}
228
229/*
230 * Stop device recognition.
231 */
232static void
233ccw_device_recog_done(struct ccw_device *cdev, int state)
234{
235 struct subchannel *sch;
236 int notify, old_lpm, same_dev;
237
238 sch = to_subchannel(cdev->dev.parent);
239
240 ccw_device_set_timeout(cdev, 0);
241 cio_disable_subchannel(sch);
242 /*
243 * Now that we tried recognition, we have performed device selection
244 * through ssch() and the path information is up to date.
245 */
246 old_lpm = sch->lpm;
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100247
Cornelia Huck4ffa9232005-07-29 14:03:37 -0700248 /* Check since device may again have become not operational. */
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100249 if (cio_update_schib(sch))
Cornelia Huck4ffa9232005-07-29 14:03:37 -0700250 state = DEV_STATE_NOT_OPER;
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100251 else
252 sch->lpm = sch->schib.pmcw.pam & sch->opm;
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
255 /* Force reprobe on all chpids. */
256 old_lpm = 0;
257 if (sch->lpm != old_lpm)
258 __recover_lost_chpids(sch, old_lpm);
259 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
260 if (state == DEV_STATE_NOT_OPER) {
261 cdev->private->flags.recog_done = 1;
262 cdev->private->state = DEV_STATE_DISCONNECTED;
263 return;
264 }
265 /* Boxed devices don't need extra treatment. */
266 }
267 notify = 0;
268 same_dev = 0; /* Keep the compiler quiet... */
269 switch (state) {
270 case DEV_STATE_NOT_OPER:
Michael Ernst139b83d2008-05-07 09:22:54 +0200271 CIO_MSG_EVENT(2, "SenseID : unknown device %04x on "
272 "subchannel 0.%x.%04x\n",
273 cdev->private->dev_id.devno,
274 sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 break;
276 case DEV_STATE_OFFLINE:
277 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
278 same_dev = ccw_device_handle_oper(cdev);
279 notify = 1;
280 }
281 /* fill out sense information */
Heiko Carstens81388d22006-09-20 15:59:17 +0200282 memset(&cdev->id, 0, sizeof(cdev->id));
Heiko Carstens292888c2006-08-30 14:33:35 +0200283 cdev->id.cu_type = cdev->private->senseid.cu_type;
284 cdev->id.cu_model = cdev->private->senseid.cu_model;
285 cdev->id.dev_type = cdev->private->senseid.dev_type;
286 cdev->id.dev_model = cdev->private->senseid.dev_model;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (notify) {
288 cdev->private->state = DEV_STATE_OFFLINE;
289 if (same_dev) {
290 /* Get device online again. */
291 ccw_device_online(cdev);
292 wake_up(&cdev->private->wait_q);
293 }
294 return;
295 }
296 /* Issue device info message. */
Michael Ernst139b83d2008-05-07 09:22:54 +0200297 CIO_MSG_EVENT(4, "SenseID : device 0.%x.%04x reports: "
298 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
299 "%04X/%02X\n",
300 cdev->private->dev_id.ssid,
301 cdev->private->dev_id.devno,
302 cdev->id.cu_type, cdev->id.cu_model,
303 cdev->id.dev_type, cdev->id.dev_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 break;
305 case DEV_STATE_BOXED:
Michael Ernst139b83d2008-05-07 09:22:54 +0200306 CIO_MSG_EVENT(0, "SenseID : boxed device %04x on "
307 " subchannel 0.%x.%04x\n",
308 cdev->private->dev_id.devno,
309 sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 break;
311 }
312 cdev->private->state = state;
313 io_subchannel_recog_done(cdev);
314 if (state != DEV_STATE_NOT_OPER)
315 wake_up(&cdev->private->wait_q);
316}
317
318/*
319 * Function called from device_id.c after sense id has completed.
320 */
321void
322ccw_device_sense_id_done(struct ccw_device *cdev, int err)
323{
324 switch (err) {
325 case 0:
326 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
327 break;
328 case -ETIME: /* Sense id stopped by timeout. */
329 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
330 break;
331 default:
332 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
333 break;
334 }
335}
336
Cornelia Huckc820de32008-07-14 09:58:45 +0200337int ccw_device_notify(struct ccw_device *cdev, int event)
338{
339 if (!cdev->drv)
340 return 0;
341 if (!cdev->online)
342 return 0;
Peter Oberparleiter91c36912008-08-21 19:46:39 +0200343 CIO_MSG_EVENT(2, "notify called for 0.%x.%04x, event=%d\n",
344 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
345 event);
Cornelia Huckc820de32008-07-14 09:58:45 +0200346 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
347}
348
Peter Oberparleiter91c36912008-08-21 19:46:39 +0200349static void cmf_reenable_delayed(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100351 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100354 priv = container_of(work, struct ccw_device_private, kick_work);
355 cdev = priv->cdev;
Peter Oberparleiter91c36912008-08-21 19:46:39 +0200356 cmf_reenable(cdev);
357}
358
359static void ccw_device_oper_notify(struct ccw_device *cdev)
360{
361 if (ccw_device_notify(cdev, CIO_OPER)) {
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100362 /* Reenable channel measurements, if needed. */
Peter Oberparleiter91c36912008-08-21 19:46:39 +0200363 PREPARE_WORK(&cdev->private->kick_work, cmf_reenable_delayed);
364 queue_work(ccw_device_work, &cdev->private->kick_work);
365 return;
366 }
367 /* Driver doesn't want device back. */
368 ccw_device_set_notoper(cdev);
369 PREPARE_WORK(&cdev->private->kick_work, ccw_device_do_unreg_rereg);
370 queue_work(ccw_device_work, &cdev->private->kick_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373/*
374 * Finished with online/offline processing.
375 */
376static void
377ccw_device_done(struct ccw_device *cdev, int state)
378{
379 struct subchannel *sch;
380
381 sch = to_subchannel(cdev->dev.parent);
382
Cornelia Huckf1ee3282006-10-04 20:02:02 +0200383 ccw_device_set_timeout(cdev, 0);
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (state != DEV_STATE_ONLINE)
386 cio_disable_subchannel(sch);
387
388 /* Reset device status. */
389 memset(&cdev->private->irb, 0, sizeof(struct irb));
390
391 cdev->private->state = state;
392
393
394 if (state == DEV_STATE_BOXED)
Michael Ernst139b83d2008-05-07 09:22:54 +0200395 CIO_MSG_EVENT(0, "Boxed device %04x on subchannel %04x\n",
396 cdev->private->dev_id.devno, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 if (cdev->private->flags.donotify) {
399 cdev->private->flags.donotify = 0;
Peter Oberparleiter91c36912008-08-21 19:46:39 +0200400 ccw_device_oper_notify(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402 wake_up(&cdev->private->wait_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100405static int cmp_pgid(struct pgid *p1, struct pgid *p2)
Cornelia Huck7e560812006-07-12 16:40:19 +0200406{
407 char *c1;
408 char *c2;
409
410 c1 = (char *)p1;
411 c2 = (char *)p2;
412
413 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
414}
415
416static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
417{
418 int i;
419 int last;
420
421 last = 0;
422 for (i = 0; i < 8; i++) {
423 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
424 /* No PGID yet */
425 continue;
426 if (cdev->private->pgid[last].inf.ps.state1 ==
427 SNID_STATE1_RESET) {
428 /* First non-zero PGID */
429 last = i;
430 continue;
431 }
432 if (cmp_pgid(&cdev->private->pgid[i],
433 &cdev->private->pgid[last]) == 0)
434 /* Non-conflicting PGIDs */
435 continue;
436
437 /* PGID mismatch, can't pathgroup. */
438 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
439 "0.%x.%04x, can't pathgroup\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200440 cdev->private->dev_id.ssid,
441 cdev->private->dev_id.devno);
Cornelia Huck7e560812006-07-12 16:40:19 +0200442 cdev->private->options.pgroup = 0;
443 return;
444 }
445 if (cdev->private->pgid[last].inf.ps.state1 ==
446 SNID_STATE1_RESET)
447 /* No previous pgid found */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200448 memcpy(&cdev->private->pgid[0],
449 &channel_subsystems[0]->global_pgid,
Cornelia Huck7e560812006-07-12 16:40:19 +0200450 sizeof(struct pgid));
451 else
452 /* Use existing pgid */
453 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
454 sizeof(struct pgid));
455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/*
458 * Function called from device_pgid.c after sense path ground has completed.
459 */
460void
461ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
462{
463 struct subchannel *sch;
464
465 sch = to_subchannel(cdev->dev.parent);
466 switch (err) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200467 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
468 cdev->private->options.pgroup = 0;
469 break;
470 case 0: /* success */
471 case -EACCES: /* partial success, some paths not operational */
472 /* Check if all pgids are equal or 0. */
473 __ccw_device_get_common_pgid(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 break;
475 case -ETIME: /* Sense path group id stopped by timeout. */
476 case -EUSERS: /* device is reserved for someone else. */
477 ccw_device_done(cdev, DEV_STATE_BOXED);
Cornelia Huck7e560812006-07-12 16:40:19 +0200478 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 default:
480 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Cornelia Huck7e560812006-07-12 16:40:19 +0200481 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
Cornelia Huck7e560812006-07-12 16:40:19 +0200483 /* Start Path Group verification. */
Cornelia Huck7e560812006-07-12 16:40:19 +0200484 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200485 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200486 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
489/*
490 * Start device recognition.
491 */
492int
493ccw_device_recognition(struct ccw_device *cdev)
494{
495 struct subchannel *sch;
496 int ret;
497
498 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
499 (cdev->private->state != DEV_STATE_BOXED))
500 return -EINVAL;
501 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckedf22092008-04-30 13:38:39 +0200502 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (ret != 0)
504 /* Couldn't enable the subchannel for i/o. Sick device. */
505 return ret;
506
507 /* After 60s the device recognition is considered to have failed. */
508 ccw_device_set_timeout(cdev, 60*HZ);
509
510 /*
511 * We used to start here with a sense pgid to find out whether a device
512 * is locked by someone else. Unfortunately, the sense pgid command
513 * code has other meanings on devices predating the path grouping
514 * algorithm, so we start with sense id and box the device after an
515 * timeout (or if sense pgid during path verification detects the device
516 * is locked, as may happen on newer devices).
517 */
518 cdev->private->flags.recog_done = 0;
519 cdev->private->state = DEV_STATE_SENSE_ID;
520 ccw_device_sense_id_start(cdev);
521 return 0;
522}
523
524/*
525 * Handle timeout in device recognition.
526 */
527static void
528ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
529{
530 int ret;
531
532 ret = ccw_device_cancel_halt_clear(cdev);
533 switch (ret) {
534 case 0:
535 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
536 break;
537 case -ENODEV:
538 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
539 break;
540 default:
541 ccw_device_set_timeout(cdev, 3*HZ);
542 }
543}
544
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546void
547ccw_device_verify_done(struct ccw_device *cdev, int err)
548{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200549 struct subchannel *sch;
550
551 sch = to_subchannel(cdev->dev.parent);
552 /* Update schib - pom may have changed. */
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100553 if (cio_update_schib(sch)) {
554 cdev->private->flags.donotify = 0;
555 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
556 return;
557 }
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200558 /* Update lpm with verified path mask. */
559 sch->lpm = sch->vpm;
560 /* Repeat path verification? */
561 if (cdev->private->flags.doverify) {
562 cdev->private->flags.doverify = 0;
563 ccw_device_verify_start(cdev);
564 return;
565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 switch (err) {
567 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
568 cdev->private->options.pgroup = 0;
569 case 0:
570 ccw_device_done(cdev, DEV_STATE_ONLINE);
571 /* Deliver fake irb to device driver, if needed. */
572 if (cdev->private->flags.fake_irb) {
573 memset(&cdev->private->irb, 0, sizeof(struct irb));
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200574 cdev->private->irb.scsw.cmd.cc = 1;
575 cdev->private->irb.scsw.cmd.fctl = SCSW_FCTL_START_FUNC;
576 cdev->private->irb.scsw.cmd.actl = SCSW_ACTL_START_PEND;
577 cdev->private->irb.scsw.cmd.stctl =
578 SCSW_STCTL_STATUS_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 cdev->private->flags.fake_irb = 0;
580 if (cdev->handler)
581 cdev->handler(cdev, cdev->private->intparm,
582 &cdev->private->irb);
583 memset(&cdev->private->irb, 0, sizeof(struct irb));
584 }
585 break;
586 case -ETIME:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200587 /* Reset oper notify indication after verify error. */
588 cdev->private->flags.donotify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 ccw_device_done(cdev, DEV_STATE_BOXED);
590 break;
591 default:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200592 /* Reset oper notify indication after verify error. */
593 cdev->private->flags.donotify = 0;
Cornelia Huck46258ab2008-01-26 14:10:49 +0100594 if (cdev->online) {
595 ccw_device_set_timeout(cdev, 0);
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200596 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Cornelia Huck46258ab2008-01-26 14:10:49 +0100597 } else
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100598 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 break;
600 }
601}
602
603/*
604 * Get device online.
605 */
606int
607ccw_device_online(struct ccw_device *cdev)
608{
609 struct subchannel *sch;
610 int ret;
611
612 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
613 (cdev->private->state != DEV_STATE_BOXED))
614 return -EINVAL;
615 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckedf22092008-04-30 13:38:39 +0200616 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (ret != 0) {
618 /* Couldn't enable the subchannel for i/o. Sick device. */
619 if (ret == -ENODEV)
620 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
621 return ret;
622 }
623 /* Do we want to do path grouping? */
624 if (!cdev->private->options.pgroup) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200625 /* Start initial path verification. */
626 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200627 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200628 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return 0;
630 }
631 /* Do a SensePGID first. */
632 cdev->private->state = DEV_STATE_SENSE_PGID;
633 ccw_device_sense_pgid_start(cdev);
634 return 0;
635}
636
637void
638ccw_device_disband_done(struct ccw_device *cdev, int err)
639{
640 switch (err) {
641 case 0:
642 ccw_device_done(cdev, DEV_STATE_OFFLINE);
643 break;
644 case -ETIME:
645 ccw_device_done(cdev, DEV_STATE_BOXED);
646 break;
647 default:
Peter Oberparleiter3ecb0a52007-05-31 17:38:07 +0200648 cdev->private->flags.donotify = 0;
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200649 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
651 break;
652 }
653}
654
655/*
656 * Shutdown device.
657 */
658int
659ccw_device_offline(struct ccw_device *cdev)
660{
661 struct subchannel *sch;
662
Peter Oberparleiterb301ea82008-09-09 12:38:59 +0200663 /* Allow ccw_device_offline while disconnected. */
664 if (cdev->private->state == DEV_STATE_DISCONNECTED ||
665 cdev->private->state == DEV_STATE_NOT_OPER) {
666 cdev->private->flags.donotify = 0;
667 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
668 return 0;
669 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100670 if (ccw_device_is_orphan(cdev)) {
671 ccw_device_done(cdev, DEV_STATE_OFFLINE);
672 return 0;
673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 sch = to_subchannel(cdev->dev.parent);
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100675 if (cio_update_schib(sch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -ENODEV;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200677 if (scsw_actl(&sch->schib.scsw) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return -EBUSY;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200679 if (cdev->private->state != DEV_STATE_ONLINE)
680 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /* Are we doing path grouping? */
682 if (!cdev->private->options.pgroup) {
683 /* No, set state offline immediately. */
684 ccw_device_done(cdev, DEV_STATE_OFFLINE);
685 return 0;
686 }
687 /* Start Set Path Group commands. */
688 cdev->private->state = DEV_STATE_DISBAND_PGID;
689 ccw_device_disband_start(cdev);
690 return 0;
691}
692
693/*
694 * Handle timeout in device online/offline process.
695 */
696static void
697ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
698{
699 int ret;
700
701 ret = ccw_device_cancel_halt_clear(cdev);
702 switch (ret) {
703 case 0:
704 ccw_device_done(cdev, DEV_STATE_BOXED);
705 break;
706 case -ENODEV:
707 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
708 break;
709 default:
710 ccw_device_set_timeout(cdev, 3*HZ);
711 }
712}
713
714/*
715 * Handle not oper event in device recognition.
716 */
717static void
718ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
719{
720 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
721}
722
723/*
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200724 * Handle not operational event in non-special state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 */
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200726static void ccw_device_generic_notoper(struct ccw_device *cdev,
727 enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
729 struct subchannel *sch;
730
731 cdev->private->state = DEV_STATE_NOT_OPER;
732 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200733 css_schedule_eval(sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
736/*
737 * Handle path verification event.
738 */
739static void
740ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
741{
742 struct subchannel *sch;
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (cdev->private->state == DEV_STATE_W4SENSE) {
745 cdev->private->flags.doverify = 1;
746 return;
747 }
748 sch = to_subchannel(cdev->dev.parent);
749 /*
750 * Since we might not just be coming from an interrupt from the
751 * subchannel we have to update the schib.
752 */
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100753 if (cio_update_schib(sch)) {
754 ccw_device_verify_done(cdev, -ENODEV);
755 return;
756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200758 if (scsw_actl(&sch->schib.scsw) != 0 ||
759 (scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_STATUS_PEND) ||
760 (scsw_stctl(&cdev->private->irb.scsw) & SCSW_STCTL_STATUS_PEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /*
762 * No final status yet or final status not yet delivered
763 * to the device driver. Can't do path verfication now,
764 * delay until final status was delivered.
765 */
766 cdev->private->flags.doverify = 1;
767 return;
768 }
769 /* Device is idle, we can do the path verification. */
770 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200771 cdev->private->flags.doverify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 ccw_device_verify_start(cdev);
773}
774
775/*
776 * Got an interrupt for a normal io (state online).
777 */
778static void
779ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
780{
781 struct irb *irb;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200782 int is_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 irb = (struct irb *) __LC_IRB;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200785 is_cmd = !scsw_is_tm(&irb->scsw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 /* Check for unsolicited interrupt. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200787 if (!scsw_is_solicited(&irb->scsw)) {
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200788 if (is_cmd && (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 !irb->esw.esw0.erw.cons) {
790 /* Unit check but no sense data. Need basic sense. */
791 if (ccw_device_do_sense(cdev, irb) != 0)
792 goto call_handler_unsol;
Cornelia Hucke0ec5742006-06-04 02:51:27 -0700793 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 cdev->private->state = DEV_STATE_W4SENSE;
795 cdev->private->intparm = 0;
796 return;
797 }
798call_handler_unsol:
799 if (cdev->handler)
800 cdev->handler (cdev, 0, irb);
Cornelia Huck18374d32007-02-05 21:17:09 +0100801 if (cdev->private->flags.doverify)
802 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return;
804 }
805 /* Accumulate status and find out if a basic sense is needed. */
806 ccw_device_accumulate_irb(cdev, irb);
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200807 if (is_cmd && cdev->private->flags.dosense) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (ccw_device_do_sense(cdev, irb) == 0) {
809 cdev->private->state = DEV_STATE_W4SENSE;
810 }
811 return;
812 }
813 /* Call the handler. */
814 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
815 /* Start delayed path verification. */
816 ccw_device_online_verify(cdev, 0);
817}
818
819/*
820 * Got an timeout in online state.
821 */
822static void
823ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
824{
825 int ret;
826
827 ccw_device_set_timeout(cdev, 0);
828 ret = ccw_device_cancel_halt_clear(cdev);
829 if (ret == -EBUSY) {
830 ccw_device_set_timeout(cdev, 3*HZ);
831 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
832 return;
833 }
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200834 if (ret == -ENODEV)
835 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
836 else if (cdev->handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 cdev->handler(cdev, cdev->private->intparm,
838 ERR_PTR(-ETIMEDOUT));
839}
840
841/*
842 * Got an interrupt for a basic sense.
843 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100844static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
846{
847 struct irb *irb;
848
849 irb = (struct irb *) __LC_IRB;
850 /* Check for unsolicited interrupt. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200851 if (scsw_stctl(&irb->scsw) ==
852 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
853 if (scsw_cc(&irb->scsw) == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 /* Basic sense hasn't started. Try again. */
855 ccw_device_do_sense(cdev, irb);
856 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200857 CIO_MSG_EVENT(0, "0.%x.%04x: unsolicited "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200858 "interrupt during w4sense...\n",
859 cdev->private->dev_id.ssid,
860 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (cdev->handler)
862 cdev->handler (cdev, 0, irb);
863 }
864 return;
865 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800866 /*
867 * Check if a halt or clear has been issued in the meanwhile. If yes,
868 * only deliver the halt/clear interrupt to the device driver as if it
869 * had killed the original request.
870 */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200871 if (scsw_fctl(&irb->scsw) &
872 (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
Cornelia Huckd23861f2006-12-04 15:41:04 +0100873 /* Retry Basic Sense if requested. */
874 if (cdev->private->flags.intretry) {
875 cdev->private->flags.intretry = 0;
876 ccw_device_do_sense(cdev, irb);
877 return;
878 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800879 cdev->private->flags.dosense = 0;
880 memset(&cdev->private->irb, 0, sizeof(struct irb));
881 ccw_device_accumulate_irb(cdev, irb);
882 goto call_handler;
883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 /* Add basic sense info to irb. */
885 ccw_device_accumulate_basic_sense(cdev, irb);
886 if (cdev->private->flags.dosense) {
887 /* Another basic sense is needed. */
888 ccw_device_do_sense(cdev, irb);
889 return;
890 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800891call_handler:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 cdev->private->state = DEV_STATE_ONLINE;
893 /* Call the handler. */
894 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
895 /* Start delayed path verification. */
896 ccw_device_online_verify(cdev, 0);
897}
898
899static void
900ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
901{
902 struct irb *irb;
903
904 irb = (struct irb *) __LC_IRB;
905 /* Accumulate status. We don't do basic sense. */
906 ccw_device_accumulate_irb(cdev, irb);
Cornelia Huckb4f7b1e2006-06-29 15:03:35 +0200907 /* Remember to clear irb to avoid residuals. */
908 memset(&cdev->private->irb, 0, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 /* Try to start delayed device verification. */
910 ccw_device_online_verify(cdev, 0);
911 /* Note: Don't call handler for cio initiated clear! */
912}
913
914static void
915ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
916{
917 struct subchannel *sch;
918
919 sch = to_subchannel(cdev->dev.parent);
920 ccw_device_set_timeout(cdev, 0);
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100921 /* Start delayed path verification. */
922 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 /* OK, i/o is dead now. Call interrupt handler. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (cdev->handler)
925 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200926 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
929static void
930ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
931{
932 int ret;
933
934 ret = ccw_device_cancel_halt_clear(cdev);
935 if (ret == -EBUSY) {
936 ccw_device_set_timeout(cdev, 3*HZ);
937 return;
938 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100939 /* Start delayed path verification. */
940 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (cdev->handler)
942 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200943 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
Cornelia Huckc820de32008-07-14 09:58:45 +0200946void ccw_device_kill_io(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
948 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 ret = ccw_device_cancel_halt_clear(cdev);
951 if (ret == -EBUSY) {
952 ccw_device_set_timeout(cdev, 3*HZ);
953 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
954 return;
955 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100956 /* Start delayed path verification. */
957 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (cdev->handler)
959 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200960 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
963static void
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200964ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200966 /* Start verification after current task finished. */
Cornelia Huck7e560812006-07-12 16:40:19 +0200967 cdev->private->flags.doverify = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
970static void
971ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
972{
973 struct irb *irb;
974
975 switch (dev_event) {
976 case DEV_EVENT_INTERRUPT:
977 irb = (struct irb *) __LC_IRB;
978 /* Check for unsolicited interrupt. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200979 if ((scsw_stctl(&irb->scsw) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200981 (!scsw_cc(&irb->scsw)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 /* FIXME: we should restart stlck here, but this
983 * is extremely unlikely ... */
984 goto out_wakeup;
985
986 ccw_device_accumulate_irb(cdev, irb);
987 /* We don't care about basic sense etc. */
988 break;
989 default: /* timeout */
990 break;
991 }
992out_wakeup:
993 wake_up(&cdev->private->wait_q);
994}
995
996static void
997ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
998{
999 struct subchannel *sch;
1000
1001 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckedf22092008-04-30 13:38:39 +02001002 if (cio_enable_subchannel(sch, (u32)(addr_t)sch) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 /* Couldn't enable the subchannel for i/o. Sick device. */
1004 return;
1005
1006 /* After 60s the device recognition is considered to have failed. */
1007 ccw_device_set_timeout(cdev, 60*HZ);
1008
1009 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1010 ccw_device_sense_id_start(cdev);
1011}
1012
Cornelia Huckc820de32008-07-14 09:58:45 +02001013void ccw_device_trigger_reprobe(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Cornelia Huckc820de32008-07-14 09:58:45 +02001015 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1018 return;
1019
Cornelia Huckc820de32008-07-14 09:58:45 +02001020 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 /* Update some values. */
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001022 if (cio_update_schib(sch))
Cornelia Huck7674da72006-12-08 15:54:21 +01001023 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 /*
1025 * The pim, pam, pom values may not be accurate, but they are the best
1026 * we have before performing device selection :/
1027 */
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001028 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Sebastian Ott13952ec2008-12-25 13:39:13 +01001029 /*
1030 * Use the initial configuration since we can't be shure that the old
1031 * paths are valid.
1032 */
1033 io_subchannel_init_config(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001034 if (cio_commit_config(sch))
1035 return;
Sebastian Ott13952ec2008-12-25 13:39:13 +01001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 /* We should also udate ssd info, but this has to wait. */
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001038 /* Check if this is another device which appeared on the same sch. */
1039 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1040 PREPARE_WORK(&cdev->private->kick_work,
1041 ccw_device_move_to_orphanage);
Cornelia Huckc5d4a992007-11-20 11:13:41 +01001042 queue_work(slow_path_wq, &cdev->private->kick_work);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001043 } else
1044 ccw_device_start_id(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
1047static void
1048ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1049{
1050 struct subchannel *sch;
1051
1052 sch = to_subchannel(cdev->dev.parent);
1053 /*
1054 * An interrupt in state offline means a previous disable was not
1055 * successful. Try again.
1056 */
1057 cio_disable_subchannel(sch);
1058}
1059
1060static void
1061ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1062{
1063 retry_set_schib(cdev);
1064 cdev->private->state = DEV_STATE_ONLINE;
1065 dev_fsm_event(cdev, dev_event);
1066}
1067
Cornelia Huck94bb0632006-06-29 15:08:41 +02001068static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1069 enum dev_event dev_event)
1070{
1071 cmf_retry_copy_block(cdev);
1072 cdev->private->state = DEV_STATE_ONLINE;
1073 dev_fsm_event(cdev, dev_event);
1074}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076static void
1077ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1078{
1079 ccw_device_set_timeout(cdev, 0);
1080 if (dev_event == DEV_EVENT_NOTOPER)
1081 cdev->private->state = DEV_STATE_NOT_OPER;
1082 else
1083 cdev->private->state = DEV_STATE_OFFLINE;
1084 wake_up(&cdev->private->wait_q);
1085}
1086
1087static void
1088ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1089{
1090 int ret;
1091
1092 ret = ccw_device_cancel_halt_clear(cdev);
1093 switch (ret) {
1094 case 0:
1095 cdev->private->state = DEV_STATE_OFFLINE;
1096 wake_up(&cdev->private->wait_q);
1097 break;
1098 case -ENODEV:
1099 cdev->private->state = DEV_STATE_NOT_OPER;
1100 wake_up(&cdev->private->wait_q);
1101 break;
1102 default:
1103 ccw_device_set_timeout(cdev, HZ/10);
1104 }
1105}
1106
1107/*
1108 * No operation action. This is used e.g. to ignore a timeout event in
1109 * state offline.
1110 */
1111static void
1112ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1113{
1114}
1115
1116/*
1117 * Bug operation action.
1118 */
1119static void
1120ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1121{
Michael Ernst139b83d2008-05-07 09:22:54 +02001122 CIO_MSG_EVENT(0, "Internal state [%i][%i] not handled for device "
1123 "0.%x.%04x\n", cdev->private->state, dev_event,
1124 cdev->private->dev_id.ssid,
1125 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 BUG();
1127}
1128
1129/*
1130 * device statemachine
1131 */
1132fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1133 [DEV_STATE_NOT_OPER] = {
1134 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1135 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1136 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1137 [DEV_EVENT_VERIFY] = ccw_device_nop,
1138 },
1139 [DEV_STATE_SENSE_PGID] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001140 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1142 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1143 [DEV_EVENT_VERIFY] = ccw_device_nop,
1144 },
1145 [DEV_STATE_SENSE_ID] = {
1146 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1147 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1148 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1149 [DEV_EVENT_VERIFY] = ccw_device_nop,
1150 },
1151 [DEV_STATE_OFFLINE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001152 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1154 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1155 [DEV_EVENT_VERIFY] = ccw_device_nop,
1156 },
1157 [DEV_STATE_VERIFY] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001158 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1160 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001161 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 },
1163 [DEV_STATE_ONLINE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001164 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1166 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1167 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1168 },
1169 [DEV_STATE_W4SENSE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001170 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1172 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1173 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1174 },
1175 [DEV_STATE_DISBAND_PGID] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001176 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1178 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1179 [DEV_EVENT_VERIFY] = ccw_device_nop,
1180 },
1181 [DEV_STATE_BOXED] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001182 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1184 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1185 [DEV_EVENT_VERIFY] = ccw_device_nop,
1186 },
1187 /* states to wait for i/o completion before doing something */
1188 [DEV_STATE_CLEAR_VERIFY] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001189 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1191 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1192 [DEV_EVENT_VERIFY] = ccw_device_nop,
1193 },
1194 [DEV_STATE_TIMEOUT_KILL] = {
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_killing_irq,
1197 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1198 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1199 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 [DEV_STATE_QUIESCE] = {
1201 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1202 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1203 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1204 [DEV_EVENT_VERIFY] = ccw_device_nop,
1205 },
1206 /* special states for devices gone not operational */
1207 [DEV_STATE_DISCONNECTED] = {
1208 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1209 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1210 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001211 [DEV_EVENT_VERIFY] = ccw_device_start_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 },
1213 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1214 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1215 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1216 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1217 [DEV_EVENT_VERIFY] = ccw_device_nop,
1218 },
1219 [DEV_STATE_CMFCHANGE] = {
1220 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1221 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1222 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1223 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1224 },
Cornelia Huck94bb0632006-06-29 15:08:41 +02001225 [DEV_STATE_CMFUPDATE] = {
1226 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1227 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1228 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1229 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1230 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231};
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233EXPORT_SYMBOL_GPL(ccw_device_set_timeout);