blob: 485741a12125ceab9d86c6332d610ae07f4c1265 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/cio/device_fsm.c
3 * finite state machine for device handling
4 *
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6 * IBM Corporation
Cornelia Huck4ce3b302006-01-14 13:21:04 -08007 * Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 */
10
11#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/init.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080013#include <linux/jiffies.h>
14#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include <asm/ccwdev.h>
Cornelia Huck4c24da72005-09-03 15:58:01 -070017#include <asm/cio.h>
Peter Oberparleitere5854a52007-04-27 16:01:31 +020018#include <asm/chpid.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include "cio.h"
21#include "cio_debug.h"
22#include "css.h"
23#include "device.h"
24#include "chsc.h"
25#include "ioasm.h"
Peter Oberparleitere6b6e102007-04-27 16:01:28 +020026#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Sebastian Ott14ff56b2008-01-26 14:10:37 +010028static int timeout_log_enabled;
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030int
31device_is_online(struct subchannel *sch)
32{
33 struct ccw_device *cdev;
34
Cornelia Huckdb6a6422008-01-26 14:10:46 +010035 cdev = sch_get_cdev(sch);
36 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 return (cdev->private->state == DEV_STATE_ONLINE);
39}
40
41int
42device_is_disconnected(struct subchannel *sch)
43{
44 struct ccw_device *cdev;
45
Cornelia Huckdb6a6422008-01-26 14:10:46 +010046 cdev = sch_get_cdev(sch);
47 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
50 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
51}
52
53void
54device_set_disconnected(struct subchannel *sch)
55{
56 struct ccw_device *cdev;
57
Cornelia Huckdb6a6422008-01-26 14:10:46 +010058 cdev = sch_get_cdev(sch);
59 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 ccw_device_set_timeout(cdev, 0);
62 cdev->private->flags.fake_irb = 0;
63 cdev->private->state = DEV_STATE_DISCONNECTED;
64}
65
Cornelia Huckd23861f2006-12-04 15:41:04 +010066void device_set_intretry(struct subchannel *sch)
67{
68 struct ccw_device *cdev;
69
Cornelia Huckdb6a6422008-01-26 14:10:46 +010070 cdev = sch_get_cdev(sch);
Cornelia Huckd23861f2006-12-04 15:41:04 +010071 if (!cdev)
72 return;
73 cdev->private->flags.intretry = 1;
74}
75
Cornelia Huck24cb5b42006-12-04 15:41:01 +010076int device_trigger_verify(struct subchannel *sch)
77{
78 struct ccw_device *cdev;
79
Cornelia Huckdb6a6422008-01-26 14:10:46 +010080 cdev = sch_get_cdev(sch);
Cornelia Huck24cb5b42006-12-04 15:41:01 +010081 if (!cdev || !cdev->online)
82 return -EINVAL;
83 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
84 return 0;
85}
86
Sebastian Ott14ff56b2008-01-26 14:10:37 +010087static int __init ccw_timeout_log_setup(char *unused)
88{
89 timeout_log_enabled = 1;
90 return 1;
91}
92
93__setup("ccw_timeout_log", ccw_timeout_log_setup);
94
95static void ccw_timeout_log(struct ccw_device *cdev)
96{
97 struct schib schib;
98 struct subchannel *sch;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +010099 struct io_subchannel_private *private;
Sebastian Ott14ff56b2008-01-26 14:10:37 +0100100 int cc;
101
102 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100103 private = to_io_private(sch);
Sebastian Ott14ff56b2008-01-26 14:10:37 +0100104 cc = stsch(sch->schid, &schib);
105
106 printk(KERN_WARNING "cio: ccw device timeout occurred at %llx, "
107 "device information:\n", get_clock());
108 printk(KERN_WARNING "cio: orb:\n");
109 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100110 &private->orb, sizeof(private->orb), 0);
Sebastian Ott14ff56b2008-01-26 14:10:37 +0100111 printk(KERN_WARNING "cio: ccw device bus id: %s\n", cdev->dev.bus_id);
112 printk(KERN_WARNING "cio: subchannel bus id: %s\n", sch->dev.bus_id);
113 printk(KERN_WARNING "cio: subchannel lpm: %02x, opm: %02x, "
114 "vpm: %02x\n", sch->lpm, sch->opm, sch->vpm);
115
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100116 if ((void *)(addr_t)private->orb.cpa == &private->sense_ccw ||
117 (void *)(addr_t)private->orb.cpa == cdev->private->iccws)
Sebastian Ott14ff56b2008-01-26 14:10:37 +0100118 printk(KERN_WARNING "cio: last channel program (intern):\n");
119 else
120 printk(KERN_WARNING "cio: last channel program:\n");
121
122 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100123 (void *)(addr_t)private->orb.cpa,
124 sizeof(struct ccw1), 0);
Sebastian Ott14ff56b2008-01-26 14:10:37 +0100125 printk(KERN_WARNING "cio: ccw device state: %d\n",
126 cdev->private->state);
127 printk(KERN_WARNING "cio: store subchannel returned: cc=%d\n", cc);
128 printk(KERN_WARNING "cio: schib:\n");
129 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
130 &schib, sizeof(schib), 0);
131 printk(KERN_WARNING "cio: ccw device flags:\n");
132 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
133 &cdev->private->flags, sizeof(cdev->private->flags), 0);
134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/*
137 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
138 */
139static void
140ccw_device_timeout(unsigned long data)
141{
142 struct ccw_device *cdev;
143
144 cdev = (struct ccw_device *) data;
145 spin_lock_irq(cdev->ccwlock);
Sebastian Ott14ff56b2008-01-26 14:10:37 +0100146 if (timeout_log_enabled)
147 ccw_timeout_log(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
149 spin_unlock_irq(cdev->ccwlock);
150}
151
152/*
153 * Set timeout
154 */
155void
156ccw_device_set_timeout(struct ccw_device *cdev, int expires)
157{
158 if (expires == 0) {
159 del_timer(&cdev->private->timer);
160 return;
161 }
162 if (timer_pending(&cdev->private->timer)) {
163 if (mod_timer(&cdev->private->timer, jiffies + expires))
164 return;
165 }
166 cdev->private->timer.function = ccw_device_timeout;
167 cdev->private->timer.data = (unsigned long) cdev;
168 cdev->private->timer.expires = jiffies + expires;
169 add_timer(&cdev->private->timer);
170}
171
172/* Kill any pending timers after machine check. */
173void
174device_kill_pending_timer(struct subchannel *sch)
175{
176 struct ccw_device *cdev;
177
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100178 cdev = sch_get_cdev(sch);
179 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 ccw_device_set_timeout(cdev, 0);
182}
183
184/*
185 * Cancel running i/o. This is called repeatedly since halt/clear are
186 * asynchronous operations. We do one try with cio_cancel, two tries
187 * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
188 * Returns 0 if device now idle, -ENODEV for device not operational and
189 * -EBUSY if an interrupt is expected (either from halt/clear or from a
190 * status pending).
191 */
192int
193ccw_device_cancel_halt_clear(struct ccw_device *cdev)
194{
195 struct subchannel *sch;
196 int ret;
197
198 sch = to_subchannel(cdev->dev.parent);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800199 ret = stsch(sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (ret || !sch->schib.pmcw.dnv)
201 return -ENODEV;
Cornelia Huck2470b642007-03-05 23:36:02 +0100202 if (!sch->schib.pmcw.ena)
203 /* Not operational -> done. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return 0;
205 /* Stage 1: cancel io. */
206 if (!(sch->schib.scsw.actl & SCSW_ACTL_HALT_PEND) &&
207 !(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
208 ret = cio_cancel(sch);
209 if (ret != -EINVAL)
210 return ret;
211 /* cancel io unsuccessful. From now on it is asynchronous. */
212 cdev->private->iretry = 3; /* 3 halt retries. */
213 }
214 if (!(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
215 /* Stage 2: halt io. */
216 if (cdev->private->iretry) {
217 cdev->private->iretry--;
218 ret = cio_halt(sch);
Peter Oberparleiterba4ba8a2006-07-27 14:00:23 +0200219 if (ret != -EBUSY)
220 return (ret == 0) ? -EBUSY : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222 /* halt io unsuccessful. */
223 cdev->private->iretry = 255; /* 255 clear retries. */
224 }
225 /* Stage 3: clear io. */
226 if (cdev->private->iretry) {
227 cdev->private->iretry--;
228 ret = cio_clear (sch);
229 return (ret == 0) ? -EBUSY : ret;
230 }
231 panic("Can't stop i/o on subchannel.\n");
232}
233
234static int
235ccw_device_handle_oper(struct ccw_device *cdev)
236{
237 struct subchannel *sch;
238
239 sch = to_subchannel(cdev->dev.parent);
240 cdev->private->flags.recog_done = 1;
241 /*
242 * Check if cu type and device type still match. If
243 * not, it is certainly another device and we have to
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100244 * de- and re-register.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
246 if (cdev->id.cu_type != cdev->private->senseid.cu_type ||
247 cdev->id.cu_model != cdev->private->senseid.cu_model ||
248 cdev->id.dev_type != cdev->private->senseid.dev_type ||
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100249 cdev->id.dev_model != cdev->private->senseid.dev_model) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100251 ccw_device_do_unreg_rereg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 queue_work(ccw_device_work, &cdev->private->kick_work);
253 return 0;
254 }
255 cdev->private->flags.donotify = 1;
256 return 1;
257}
258
259/*
260 * The machine won't give us any notification by machine check if a chpid has
261 * been varied online on the SE so we have to find out by magic (i. e. driving
262 * the channel subsystem to device selection and updating our path masks).
263 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100264static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265__recover_lost_chpids(struct subchannel *sch, int old_lpm)
266{
267 int mask, i;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200268 struct chp_id chpid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200270 chp_id_init(&chpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 for (i = 0; i<8; i++) {
272 mask = 0x80 >> i;
273 if (!(sch->lpm & mask))
274 continue;
275 if (old_lpm & mask)
276 continue;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200277 chpid.id = sch->schib.pmcw.chpid[i];
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200278 if (!chp_is_registered(chpid))
279 css_schedule_eval_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281}
282
283/*
284 * Stop device recognition.
285 */
286static void
287ccw_device_recog_done(struct ccw_device *cdev, int state)
288{
289 struct subchannel *sch;
290 int notify, old_lpm, same_dev;
291
292 sch = to_subchannel(cdev->dev.parent);
293
294 ccw_device_set_timeout(cdev, 0);
295 cio_disable_subchannel(sch);
296 /*
297 * Now that we tried recognition, we have performed device selection
298 * through ssch() and the path information is up to date.
299 */
300 old_lpm = sch->lpm;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800301 stsch(sch->schid, &sch->schib);
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200302 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck4ffa9232005-07-29 14:03:37 -0700303 /* Check since device may again have become not operational. */
304 if (!sch->schib.pmcw.dnv)
305 state = DEV_STATE_NOT_OPER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
307 /* Force reprobe on all chpids. */
308 old_lpm = 0;
309 if (sch->lpm != old_lpm)
310 __recover_lost_chpids(sch, old_lpm);
311 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
312 if (state == DEV_STATE_NOT_OPER) {
313 cdev->private->flags.recog_done = 1;
314 cdev->private->state = DEV_STATE_DISCONNECTED;
315 return;
316 }
317 /* Boxed devices don't need extra treatment. */
318 }
319 notify = 0;
320 same_dev = 0; /* Keep the compiler quiet... */
321 switch (state) {
322 case DEV_STATE_NOT_OPER:
323 CIO_DEBUG(KERN_WARNING, 2,
Cornelia Huckbc698bc2008-01-26 14:10:42 +0100324 "SenseID : unknown device %04x on subchannel "
Cornelia Huck78964262006-10-11 15:31:38 +0200325 "0.%x.%04x\n", cdev->private->dev_id.devno,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800326 sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 break;
328 case DEV_STATE_OFFLINE:
329 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
330 same_dev = ccw_device_handle_oper(cdev);
331 notify = 1;
332 }
333 /* fill out sense information */
Heiko Carstens81388d22006-09-20 15:59:17 +0200334 memset(&cdev->id, 0, sizeof(cdev->id));
Heiko Carstens292888c2006-08-30 14:33:35 +0200335 cdev->id.cu_type = cdev->private->senseid.cu_type;
336 cdev->id.cu_model = cdev->private->senseid.cu_model;
337 cdev->id.dev_type = cdev->private->senseid.dev_type;
338 cdev->id.dev_model = cdev->private->senseid.dev_model;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (notify) {
340 cdev->private->state = DEV_STATE_OFFLINE;
341 if (same_dev) {
342 /* Get device online again. */
343 ccw_device_online(cdev);
344 wake_up(&cdev->private->wait_q);
345 }
346 return;
347 }
348 /* Issue device info message. */
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200349 CIO_DEBUG(KERN_INFO, 2,
Cornelia Huckbc698bc2008-01-26 14:10:42 +0100350 "SenseID : device 0.%x.%04x reports: "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800352 "%04X/%02X\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200353 cdev->private->dev_id.ssid,
354 cdev->private->dev_id.devno,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 cdev->id.cu_type, cdev->id.cu_model,
356 cdev->id.dev_type, cdev->id.dev_model);
357 break;
358 case DEV_STATE_BOXED:
359 CIO_DEBUG(KERN_WARNING, 2,
Cornelia Huckbc698bc2008-01-26 14:10:42 +0100360 "SenseID : boxed device %04x on subchannel "
Cornelia Huck78964262006-10-11 15:31:38 +0200361 "0.%x.%04x\n", cdev->private->dev_id.devno,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800362 sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 break;
364 }
365 cdev->private->state = state;
366 io_subchannel_recog_done(cdev);
367 if (state != DEV_STATE_NOT_OPER)
368 wake_up(&cdev->private->wait_q);
369}
370
371/*
372 * Function called from device_id.c after sense id has completed.
373 */
374void
375ccw_device_sense_id_done(struct ccw_device *cdev, int err)
376{
377 switch (err) {
378 case 0:
379 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
380 break;
381 case -ETIME: /* Sense id stopped by timeout. */
382 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
383 break;
384 default:
385 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
386 break;
387 }
388}
389
390static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100391ccw_device_oper_notify(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100393 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 struct ccw_device *cdev;
395 struct subchannel *sch;
396 int ret;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100397 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100399 priv = container_of(work, struct ccw_device_private, kick_work);
400 cdev = priv->cdev;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100401 spin_lock_irqsave(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100403 if (sch->driver && sch->driver->notify) {
404 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huck602b20f2008-01-26 14:10:39 +0100405 ret = sch->driver->notify(sch, CIO_OPER);
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100406 spin_lock_irqsave(cdev->ccwlock, flags);
407 } else
408 ret = 0;
409 if (ret) {
410 /* Reenable channel measurements, if needed. */
411 spin_unlock_irqrestore(cdev->ccwlock, flags);
412 cmf_reenable(cdev);
413 spin_lock_irqsave(cdev->ccwlock, flags);
414 wake_up(&cdev->private->wait_q);
415 }
416 spin_unlock_irqrestore(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (!ret)
418 /* Driver doesn't want device back. */
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100419 ccw_device_do_unreg_rereg(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422/*
423 * Finished with online/offline processing.
424 */
425static void
426ccw_device_done(struct ccw_device *cdev, int state)
427{
428 struct subchannel *sch;
429
430 sch = to_subchannel(cdev->dev.parent);
431
Cornelia Huckf1ee3282006-10-04 20:02:02 +0200432 ccw_device_set_timeout(cdev, 0);
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (state != DEV_STATE_ONLINE)
435 cio_disable_subchannel(sch);
436
437 /* Reset device status. */
438 memset(&cdev->private->irb, 0, sizeof(struct irb));
439
440 cdev->private->state = state;
441
442
443 if (state == DEV_STATE_BOXED)
444 CIO_DEBUG(KERN_WARNING, 2,
Cornelia Huckbc698bc2008-01-26 14:10:42 +0100445 "Boxed device %04x on subchannel %04x\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200446 cdev->private->dev_id.devno, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 if (cdev->private->flags.donotify) {
449 cdev->private->flags.donotify = 0;
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100450 PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
452 }
453 wake_up(&cdev->private->wait_q);
454
455 if (css_init_done && state != DEV_STATE_ONLINE)
456 put_device (&cdev->dev);
457}
458
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100459static int cmp_pgid(struct pgid *p1, struct pgid *p2)
Cornelia Huck7e560812006-07-12 16:40:19 +0200460{
461 char *c1;
462 char *c2;
463
464 c1 = (char *)p1;
465 c2 = (char *)p2;
466
467 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
468}
469
470static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
471{
472 int i;
473 int last;
474
475 last = 0;
476 for (i = 0; i < 8; i++) {
477 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
478 /* No PGID yet */
479 continue;
480 if (cdev->private->pgid[last].inf.ps.state1 ==
481 SNID_STATE1_RESET) {
482 /* First non-zero PGID */
483 last = i;
484 continue;
485 }
486 if (cmp_pgid(&cdev->private->pgid[i],
487 &cdev->private->pgid[last]) == 0)
488 /* Non-conflicting PGIDs */
489 continue;
490
491 /* PGID mismatch, can't pathgroup. */
492 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
493 "0.%x.%04x, can't pathgroup\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200494 cdev->private->dev_id.ssid,
495 cdev->private->dev_id.devno);
Cornelia Huck7e560812006-07-12 16:40:19 +0200496 cdev->private->options.pgroup = 0;
497 return;
498 }
499 if (cdev->private->pgid[last].inf.ps.state1 ==
500 SNID_STATE1_RESET)
501 /* No previous pgid found */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200502 memcpy(&cdev->private->pgid[0],
503 &channel_subsystems[0]->global_pgid,
Cornelia Huck7e560812006-07-12 16:40:19 +0200504 sizeof(struct pgid));
505 else
506 /* Use existing pgid */
507 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
508 sizeof(struct pgid));
509}
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511/*
512 * Function called from device_pgid.c after sense path ground has completed.
513 */
514void
515ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
516{
517 struct subchannel *sch;
518
519 sch = to_subchannel(cdev->dev.parent);
520 switch (err) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200521 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
522 cdev->private->options.pgroup = 0;
523 break;
524 case 0: /* success */
525 case -EACCES: /* partial success, some paths not operational */
526 /* Check if all pgids are equal or 0. */
527 __ccw_device_get_common_pgid(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 break;
529 case -ETIME: /* Sense path group id stopped by timeout. */
530 case -EUSERS: /* device is reserved for someone else. */
531 ccw_device_done(cdev, DEV_STATE_BOXED);
Cornelia Huck7e560812006-07-12 16:40:19 +0200532 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 default:
534 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Cornelia Huck7e560812006-07-12 16:40:19 +0200535 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
Cornelia Huck7e560812006-07-12 16:40:19 +0200537 /* Start Path Group verification. */
Cornelia Huck7e560812006-07-12 16:40:19 +0200538 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200539 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200540 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
543/*
544 * Start device recognition.
545 */
546int
547ccw_device_recognition(struct ccw_device *cdev)
548{
549 struct subchannel *sch;
550 int ret;
551
552 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
553 (cdev->private->state != DEV_STATE_BOXED))
554 return -EINVAL;
555 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100556 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc,
557 (u32)(addr_t)sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (ret != 0)
559 /* Couldn't enable the subchannel for i/o. Sick device. */
560 return ret;
561
562 /* After 60s the device recognition is considered to have failed. */
563 ccw_device_set_timeout(cdev, 60*HZ);
564
565 /*
566 * We used to start here with a sense pgid to find out whether a device
567 * is locked by someone else. Unfortunately, the sense pgid command
568 * code has other meanings on devices predating the path grouping
569 * algorithm, so we start with sense id and box the device after an
570 * timeout (or if sense pgid during path verification detects the device
571 * is locked, as may happen on newer devices).
572 */
573 cdev->private->flags.recog_done = 0;
574 cdev->private->state = DEV_STATE_SENSE_ID;
575 ccw_device_sense_id_start(cdev);
576 return 0;
577}
578
579/*
580 * Handle timeout in device recognition.
581 */
582static void
583ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
584{
585 int ret;
586
587 ret = ccw_device_cancel_halt_clear(cdev);
588 switch (ret) {
589 case 0:
590 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
591 break;
592 case -ENODEV:
593 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
594 break;
595 default:
596 ccw_device_set_timeout(cdev, 3*HZ);
597 }
598}
599
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601void
602ccw_device_verify_done(struct ccw_device *cdev, int err)
603{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200604 struct subchannel *sch;
605
606 sch = to_subchannel(cdev->dev.parent);
607 /* Update schib - pom may have changed. */
608 stsch(sch->schid, &sch->schib);
609 /* Update lpm with verified path mask. */
610 sch->lpm = sch->vpm;
611 /* Repeat path verification? */
612 if (cdev->private->flags.doverify) {
613 cdev->private->flags.doverify = 0;
614 ccw_device_verify_start(cdev);
615 return;
616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 switch (err) {
618 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
619 cdev->private->options.pgroup = 0;
620 case 0:
621 ccw_device_done(cdev, DEV_STATE_ONLINE);
622 /* Deliver fake irb to device driver, if needed. */
623 if (cdev->private->flags.fake_irb) {
624 memset(&cdev->private->irb, 0, sizeof(struct irb));
Heiko Carstens292888c2006-08-30 14:33:35 +0200625 cdev->private->irb.scsw.cc = 1;
626 cdev->private->irb.scsw.fctl = SCSW_FCTL_START_FUNC;
627 cdev->private->irb.scsw.actl = SCSW_ACTL_START_PEND;
628 cdev->private->irb.scsw.stctl = SCSW_STCTL_STATUS_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 cdev->private->flags.fake_irb = 0;
630 if (cdev->handler)
631 cdev->handler(cdev, cdev->private->intparm,
632 &cdev->private->irb);
633 memset(&cdev->private->irb, 0, sizeof(struct irb));
634 }
635 break;
636 case -ETIME:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200637 /* Reset oper notify indication after verify error. */
638 cdev->private->flags.donotify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 ccw_device_done(cdev, DEV_STATE_BOXED);
640 break;
641 default:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200642 /* Reset oper notify indication after verify error. */
643 cdev->private->flags.donotify = 0;
Cornelia Huck46258ab2008-01-26 14:10:49 +0100644 if (cdev->online) {
645 ccw_device_set_timeout(cdev, 0);
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200646 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Cornelia Huck46258ab2008-01-26 14:10:49 +0100647 } else
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100648 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 break;
650 }
651}
652
653/*
654 * Get device online.
655 */
656int
657ccw_device_online(struct ccw_device *cdev)
658{
659 struct subchannel *sch;
660 int ret;
661
662 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
663 (cdev->private->state != DEV_STATE_BOXED))
664 return -EINVAL;
665 sch = to_subchannel(cdev->dev.parent);
666 if (css_init_done && !get_device(&cdev->dev))
667 return -ENODEV;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100668 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc,
669 (u32)(addr_t)sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (ret != 0) {
671 /* Couldn't enable the subchannel for i/o. Sick device. */
672 if (ret == -ENODEV)
673 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
674 return ret;
675 }
676 /* Do we want to do path grouping? */
677 if (!cdev->private->options.pgroup) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200678 /* Start initial path verification. */
679 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200680 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200681 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return 0;
683 }
684 /* Do a SensePGID first. */
685 cdev->private->state = DEV_STATE_SENSE_PGID;
686 ccw_device_sense_pgid_start(cdev);
687 return 0;
688}
689
690void
691ccw_device_disband_done(struct ccw_device *cdev, int err)
692{
693 switch (err) {
694 case 0:
695 ccw_device_done(cdev, DEV_STATE_OFFLINE);
696 break;
697 case -ETIME:
698 ccw_device_done(cdev, DEV_STATE_BOXED);
699 break;
700 default:
Peter Oberparleiter3ecb0a52007-05-31 17:38:07 +0200701 cdev->private->flags.donotify = 0;
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200702 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
704 break;
705 }
706}
707
708/*
709 * Shutdown device.
710 */
711int
712ccw_device_offline(struct ccw_device *cdev)
713{
714 struct subchannel *sch;
715
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100716 if (ccw_device_is_orphan(cdev)) {
717 ccw_device_done(cdev, DEV_STATE_OFFLINE);
718 return 0;
719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 sch = to_subchannel(cdev->dev.parent);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800721 if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return -ENODEV;
723 if (cdev->private->state != DEV_STATE_ONLINE) {
724 if (sch->schib.scsw.actl != 0)
725 return -EBUSY;
726 return -EINVAL;
727 }
728 if (sch->schib.scsw.actl != 0)
729 return -EBUSY;
730 /* Are we doing path grouping? */
731 if (!cdev->private->options.pgroup) {
732 /* No, set state offline immediately. */
733 ccw_device_done(cdev, DEV_STATE_OFFLINE);
734 return 0;
735 }
736 /* Start Set Path Group commands. */
737 cdev->private->state = DEV_STATE_DISBAND_PGID;
738 ccw_device_disband_start(cdev);
739 return 0;
740}
741
742/*
743 * Handle timeout in device online/offline process.
744 */
745static void
746ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
747{
748 int ret;
749
750 ret = ccw_device_cancel_halt_clear(cdev);
751 switch (ret) {
752 case 0:
753 ccw_device_done(cdev, DEV_STATE_BOXED);
754 break;
755 case -ENODEV:
756 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
757 break;
758 default:
759 ccw_device_set_timeout(cdev, 3*HZ);
760 }
761}
762
763/*
764 * Handle not oper event in device recognition.
765 */
766static void
767ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
768{
769 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
770}
771
772/*
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200773 * Handle not operational event in non-special state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 */
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200775static void ccw_device_generic_notoper(struct ccw_device *cdev,
776 enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 struct subchannel *sch;
779
780 cdev->private->state = DEV_STATE_NOT_OPER;
781 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200782 css_schedule_eval(sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
785/*
786 * Handle path verification event.
787 */
788static void
789ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
790{
791 struct subchannel *sch;
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (cdev->private->state == DEV_STATE_W4SENSE) {
794 cdev->private->flags.doverify = 1;
795 return;
796 }
797 sch = to_subchannel(cdev->dev.parent);
798 /*
799 * Since we might not just be coming from an interrupt from the
800 * subchannel we have to update the schib.
801 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800802 stsch(sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 if (sch->schib.scsw.actl != 0 ||
Peter Oberparleiter65200c22006-08-07 17:00:33 +0200805 (sch->schib.scsw.stctl & SCSW_STCTL_STATUS_PEND) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
807 /*
808 * No final status yet or final status not yet delivered
809 * to the device driver. Can't do path verfication now,
810 * delay until final status was delivered.
811 */
812 cdev->private->flags.doverify = 1;
813 return;
814 }
815 /* Device is idle, we can do the path verification. */
816 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200817 cdev->private->flags.doverify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 ccw_device_verify_start(cdev);
819}
820
821/*
822 * Got an interrupt for a normal io (state online).
823 */
824static void
825ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
826{
827 struct irb *irb;
828
829 irb = (struct irb *) __LC_IRB;
830 /* Check for unsolicited interrupt. */
831 if ((irb->scsw.stctl ==
832 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
833 && (!irb->scsw.cc)) {
834 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
835 !irb->esw.esw0.erw.cons) {
836 /* Unit check but no sense data. Need basic sense. */
837 if (ccw_device_do_sense(cdev, irb) != 0)
838 goto call_handler_unsol;
Cornelia Hucke0ec5742006-06-04 02:51:27 -0700839 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 cdev->private->state = DEV_STATE_W4SENSE;
841 cdev->private->intparm = 0;
842 return;
843 }
844call_handler_unsol:
845 if (cdev->handler)
846 cdev->handler (cdev, 0, irb);
Cornelia Huck18374d32007-02-05 21:17:09 +0100847 if (cdev->private->flags.doverify)
848 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return;
850 }
851 /* Accumulate status and find out if a basic sense is needed. */
852 ccw_device_accumulate_irb(cdev, irb);
853 if (cdev->private->flags.dosense) {
854 if (ccw_device_do_sense(cdev, irb) == 0) {
855 cdev->private->state = DEV_STATE_W4SENSE;
856 }
857 return;
858 }
859 /* Call the handler. */
860 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
861 /* Start delayed path verification. */
862 ccw_device_online_verify(cdev, 0);
863}
864
865/*
866 * Got an timeout in online state.
867 */
868static void
869ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
870{
871 int ret;
872
873 ccw_device_set_timeout(cdev, 0);
874 ret = ccw_device_cancel_halt_clear(cdev);
875 if (ret == -EBUSY) {
876 ccw_device_set_timeout(cdev, 3*HZ);
877 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
878 return;
879 }
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200880 if (ret == -ENODEV)
881 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
882 else if (cdev->handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 cdev->handler(cdev, cdev->private->intparm,
884 ERR_PTR(-ETIMEDOUT));
885}
886
887/*
888 * Got an interrupt for a basic sense.
889 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100890static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
892{
893 struct irb *irb;
894
895 irb = (struct irb *) __LC_IRB;
896 /* Check for unsolicited interrupt. */
897 if (irb->scsw.stctl ==
898 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
899 if (irb->scsw.cc == 1)
900 /* Basic sense hasn't started. Try again. */
901 ccw_device_do_sense(cdev, irb);
902 else {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200903 CIO_MSG_EVENT(2, "Huh? 0.%x.%04x: unsolicited "
904 "interrupt during w4sense...\n",
905 cdev->private->dev_id.ssid,
906 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (cdev->handler)
908 cdev->handler (cdev, 0, irb);
909 }
910 return;
911 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800912 /*
913 * Check if a halt or clear has been issued in the meanwhile. If yes,
914 * only deliver the halt/clear interrupt to the device driver as if it
915 * had killed the original request.
916 */
917 if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
Cornelia Huckd23861f2006-12-04 15:41:04 +0100918 /* Retry Basic Sense if requested. */
919 if (cdev->private->flags.intretry) {
920 cdev->private->flags.intretry = 0;
921 ccw_device_do_sense(cdev, irb);
922 return;
923 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800924 cdev->private->flags.dosense = 0;
925 memset(&cdev->private->irb, 0, sizeof(struct irb));
926 ccw_device_accumulate_irb(cdev, irb);
927 goto call_handler;
928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 /* Add basic sense info to irb. */
930 ccw_device_accumulate_basic_sense(cdev, irb);
931 if (cdev->private->flags.dosense) {
932 /* Another basic sense is needed. */
933 ccw_device_do_sense(cdev, irb);
934 return;
935 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800936call_handler:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 cdev->private->state = DEV_STATE_ONLINE;
938 /* Call the handler. */
939 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
940 /* Start delayed path verification. */
941 ccw_device_online_verify(cdev, 0);
942}
943
944static void
945ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
946{
947 struct irb *irb;
948
949 irb = (struct irb *) __LC_IRB;
950 /* Accumulate status. We don't do basic sense. */
951 ccw_device_accumulate_irb(cdev, irb);
Cornelia Huckb4f7b1e2006-06-29 15:03:35 +0200952 /* Remember to clear irb to avoid residuals. */
953 memset(&cdev->private->irb, 0, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 /* Try to start delayed device verification. */
955 ccw_device_online_verify(cdev, 0);
956 /* Note: Don't call handler for cio initiated clear! */
957}
958
959static void
960ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
961{
962 struct subchannel *sch;
963
964 sch = to_subchannel(cdev->dev.parent);
965 ccw_device_set_timeout(cdev, 0);
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100966 /* Start delayed path verification. */
967 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 /* OK, i/o is dead now. Call interrupt handler. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (cdev->handler)
970 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200971 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
974static void
975ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
976{
977 int ret;
978
979 ret = ccw_device_cancel_halt_clear(cdev);
980 if (ret == -EBUSY) {
981 ccw_device_set_timeout(cdev, 3*HZ);
982 return;
983 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100984 /* Start delayed path verification. */
985 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (cdev->handler)
987 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200988 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
Cornelia Hucke7769b42006-10-11 15:31:41 +0200991void device_kill_io(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
993 int ret;
Cornelia Hucke7769b42006-10-11 15:31:41 +0200994 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100996 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 ret = ccw_device_cancel_halt_clear(cdev);
998 if (ret == -EBUSY) {
999 ccw_device_set_timeout(cdev, 3*HZ);
1000 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
1001 return;
1002 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +01001003 /* Start delayed path verification. */
1004 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (cdev->handler)
1006 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +02001007 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
1010static void
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001011ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001013 /* Start verification after current task finished. */
Cornelia Huck7e560812006-07-12 16:40:19 +02001014 cdev->private->flags.doverify = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
1017static void
1018ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
1019{
1020 struct irb *irb;
1021
1022 switch (dev_event) {
1023 case DEV_EVENT_INTERRUPT:
1024 irb = (struct irb *) __LC_IRB;
1025 /* Check for unsolicited interrupt. */
1026 if ((irb->scsw.stctl ==
1027 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
1028 (!irb->scsw.cc))
1029 /* FIXME: we should restart stlck here, but this
1030 * is extremely unlikely ... */
1031 goto out_wakeup;
1032
1033 ccw_device_accumulate_irb(cdev, irb);
1034 /* We don't care about basic sense etc. */
1035 break;
1036 default: /* timeout */
1037 break;
1038 }
1039out_wakeup:
1040 wake_up(&cdev->private->wait_q);
1041}
1042
1043static void
1044ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1045{
1046 struct subchannel *sch;
1047
1048 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckb279a4f2008-01-26 14:10:45 +01001049 if (cio_enable_subchannel(sch, sch->schib.pmcw.isc,
1050 (u32)(addr_t)sch) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 /* Couldn't enable the subchannel for i/o. Sick device. */
1052 return;
1053
1054 /* After 60s the device recognition is considered to have failed. */
1055 ccw_device_set_timeout(cdev, 60*HZ);
1056
1057 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1058 ccw_device_sense_id_start(cdev);
1059}
1060
1061void
1062device_trigger_reprobe(struct subchannel *sch)
1063{
1064 struct ccw_device *cdev;
1065
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001066 cdev = sch_get_cdev(sch);
1067 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1070 return;
1071
1072 /* Update some values. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001073 if (stsch(sch->schid, &sch->schib))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return;
Cornelia Huck7674da72006-12-08 15:54:21 +01001075 if (!sch->schib.pmcw.dnv)
1076 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 /*
1078 * The pim, pam, pom values may not be accurate, but they are the best
1079 * we have before performing device selection :/
1080 */
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001081 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 /* Re-set some bits in the pmcw that were lost. */
1083 sch->schib.pmcw.isc = 3;
1084 sch->schib.pmcw.csense = 1;
1085 sch->schib.pmcw.ena = 0;
1086 if ((sch->lpm & (sch->lpm - 1)) != 0)
1087 sch->schib.pmcw.mp = 1;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001088 sch->schib.pmcw.intparm = (u32)(addr_t)sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 /* We should also udate ssd info, but this has to wait. */
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001090 /* Check if this is another device which appeared on the same sch. */
1091 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1092 PREPARE_WORK(&cdev->private->kick_work,
1093 ccw_device_move_to_orphanage);
Cornelia Huckc5d4a992007-11-20 11:13:41 +01001094 queue_work(slow_path_wq, &cdev->private->kick_work);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001095 } else
1096 ccw_device_start_id(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
1099static void
1100ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1101{
1102 struct subchannel *sch;
1103
1104 sch = to_subchannel(cdev->dev.parent);
1105 /*
1106 * An interrupt in state offline means a previous disable was not
1107 * successful. Try again.
1108 */
1109 cio_disable_subchannel(sch);
1110}
1111
1112static void
1113ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1114{
1115 retry_set_schib(cdev);
1116 cdev->private->state = DEV_STATE_ONLINE;
1117 dev_fsm_event(cdev, dev_event);
1118}
1119
Cornelia Huck94bb0632006-06-29 15:08:41 +02001120static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1121 enum dev_event dev_event)
1122{
1123 cmf_retry_copy_block(cdev);
1124 cdev->private->state = DEV_STATE_ONLINE;
1125 dev_fsm_event(cdev, dev_event);
1126}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128static void
1129ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1130{
1131 ccw_device_set_timeout(cdev, 0);
1132 if (dev_event == DEV_EVENT_NOTOPER)
1133 cdev->private->state = DEV_STATE_NOT_OPER;
1134 else
1135 cdev->private->state = DEV_STATE_OFFLINE;
1136 wake_up(&cdev->private->wait_q);
1137}
1138
1139static void
1140ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1141{
1142 int ret;
1143
1144 ret = ccw_device_cancel_halt_clear(cdev);
1145 switch (ret) {
1146 case 0:
1147 cdev->private->state = DEV_STATE_OFFLINE;
1148 wake_up(&cdev->private->wait_q);
1149 break;
1150 case -ENODEV:
1151 cdev->private->state = DEV_STATE_NOT_OPER;
1152 wake_up(&cdev->private->wait_q);
1153 break;
1154 default:
1155 ccw_device_set_timeout(cdev, HZ/10);
1156 }
1157}
1158
1159/*
1160 * No operation action. This is used e.g. to ignore a timeout event in
1161 * state offline.
1162 */
1163static void
1164ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1165{
1166}
1167
1168/*
1169 * Bug operation action.
1170 */
1171static void
1172ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1173{
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001174 CIO_MSG_EVENT(0, "dev_jumptable[%i][%i] == NULL\n",
1175 cdev->private->state, dev_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 BUG();
1177}
1178
1179/*
1180 * device statemachine
1181 */
1182fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1183 [DEV_STATE_NOT_OPER] = {
1184 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1185 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1186 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1187 [DEV_EVENT_VERIFY] = ccw_device_nop,
1188 },
1189 [DEV_STATE_SENSE_PGID] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001190 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1192 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1193 [DEV_EVENT_VERIFY] = ccw_device_nop,
1194 },
1195 [DEV_STATE_SENSE_ID] = {
1196 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1197 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1198 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1199 [DEV_EVENT_VERIFY] = ccw_device_nop,
1200 },
1201 [DEV_STATE_OFFLINE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001202 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1204 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1205 [DEV_EVENT_VERIFY] = ccw_device_nop,
1206 },
1207 [DEV_STATE_VERIFY] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001208 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1210 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001211 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 },
1213 [DEV_STATE_ONLINE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001214 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1216 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1217 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1218 },
1219 [DEV_STATE_W4SENSE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001220 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1222 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1223 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1224 },
1225 [DEV_STATE_DISBAND_PGID] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001226 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1228 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1229 [DEV_EVENT_VERIFY] = ccw_device_nop,
1230 },
1231 [DEV_STATE_BOXED] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001232 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1234 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1235 [DEV_EVENT_VERIFY] = ccw_device_nop,
1236 },
1237 /* states to wait for i/o completion before doing something */
1238 [DEV_STATE_CLEAR_VERIFY] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001239 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1241 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1242 [DEV_EVENT_VERIFY] = ccw_device_nop,
1243 },
1244 [DEV_STATE_TIMEOUT_KILL] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001245 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1247 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1248 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1249 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 [DEV_STATE_QUIESCE] = {
1251 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1252 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1253 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1254 [DEV_EVENT_VERIFY] = ccw_device_nop,
1255 },
1256 /* special states for devices gone not operational */
1257 [DEV_STATE_DISCONNECTED] = {
1258 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1259 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1260 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001261 [DEV_EVENT_VERIFY] = ccw_device_start_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 },
1263 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1264 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1265 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1266 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1267 [DEV_EVENT_VERIFY] = ccw_device_nop,
1268 },
1269 [DEV_STATE_CMFCHANGE] = {
1270 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1271 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1272 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1273 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1274 },
Cornelia Huck94bb0632006-06-29 15:08:41 +02001275 [DEV_STATE_CMFUPDATE] = {
1276 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1277 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1278 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1279 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1280 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281};
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283EXPORT_SYMBOL_GPL(ccw_device_set_timeout);