blob: 4395c01a9daca092d1f151f8cc31727f91562b2e [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 Ott8821d242010-04-22 17:17:05 +020048 cc = stsch_err(sch->schid, &schib);
Sebastian Ott14ff56b2008-01-26 14:10:37 +010049
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
Sebastian Ott823d4942009-06-16 10:30:20 +0200180void ccw_device_update_sense_data(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Sebastian Ott823d4942009-06-16 10:30:20 +0200182 memset(&cdev->id, 0, sizeof(cdev->id));
183 cdev->id.cu_type = cdev->private->senseid.cu_type;
184 cdev->id.cu_model = cdev->private->senseid.cu_model;
185 cdev->id.dev_type = cdev->private->senseid.dev_type;
186 cdev->id.dev_model = cdev->private->senseid.dev_model;
187}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Sebastian Ott823d4942009-06-16 10:30:20 +0200189int ccw_device_test_sense_data(struct ccw_device *cdev)
190{
191 return cdev->id.cu_type == cdev->private->senseid.cu_type &&
192 cdev->id.cu_model == cdev->private->senseid.cu_model &&
193 cdev->id.dev_type == cdev->private->senseid.dev_type &&
194 cdev->id.dev_model == cdev->private->senseid.dev_model;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
197/*
198 * The machine won't give us any notification by machine check if a chpid has
199 * been varied online on the SE so we have to find out by magic (i. e. driving
200 * the channel subsystem to device selection and updating our path masks).
201 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100202static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203__recover_lost_chpids(struct subchannel *sch, int old_lpm)
204{
205 int mask, i;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200206 struct chp_id chpid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200208 chp_id_init(&chpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 for (i = 0; i<8; i++) {
210 mask = 0x80 >> i;
211 if (!(sch->lpm & mask))
212 continue;
213 if (old_lpm & mask)
214 continue;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200215 chpid.id = sch->schib.pmcw.chpid[i];
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200216 if (!chp_is_registered(chpid))
217 css_schedule_eval_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219}
220
221/*
222 * Stop device recognition.
223 */
224static void
225ccw_device_recog_done(struct ccw_device *cdev, int state)
226{
227 struct subchannel *sch;
Sebastian Ott823d4942009-06-16 10:30:20 +0200228 int old_lpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 sch = to_subchannel(cdev->dev.parent);
231
Sebastian Ottec643332009-12-07 12:51:36 +0100232 if (cio_disable_subchannel(sch))
233 state = DEV_STATE_NOT_OPER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /*
235 * Now that we tried recognition, we have performed device selection
236 * through ssch() and the path information is up to date.
237 */
238 old_lpm = sch->lpm;
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100239
Cornelia Huck4ffa9232005-07-29 14:03:37 -0700240 /* Check since device may again have become not operational. */
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100241 if (cio_update_schib(sch))
Cornelia Huck4ffa9232005-07-29 14:03:37 -0700242 state = DEV_STATE_NOT_OPER;
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100243 else
244 sch->lpm = sch->schib.pmcw.pam & sch->opm;
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
247 /* Force reprobe on all chpids. */
248 old_lpm = 0;
249 if (sch->lpm != old_lpm)
250 __recover_lost_chpids(sch, old_lpm);
Sebastian Ott47593bf2009-03-31 19:16:05 +0200251 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID &&
252 (state == DEV_STATE_NOT_OPER || state == DEV_STATE_BOXED)) {
253 cdev->private->flags.recog_done = 1;
254 cdev->private->state = DEV_STATE_DISCONNECTED;
255 wake_up(&cdev->private->wait_q);
256 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
Sebastian Ott823d4942009-06-16 10:30:20 +0200258 if (cdev->private->flags.resuming) {
259 cdev->private->state = state;
260 cdev->private->flags.recog_done = 1;
261 wake_up(&cdev->private->wait_q);
262 return;
263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 switch (state) {
265 case DEV_STATE_NOT_OPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 break;
267 case DEV_STATE_OFFLINE:
Sebastian Ott823d4942009-06-16 10:30:20 +0200268 if (!cdev->online) {
269 ccw_device_update_sense_data(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +0200270 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
Sebastian Ott823d4942009-06-16 10:30:20 +0200272 cdev->private->state = DEV_STATE_OFFLINE;
273 cdev->private->flags.recog_done = 1;
274 if (ccw_device_test_sense_data(cdev)) {
275 cdev->private->flags.donotify = 1;
276 ccw_device_online(cdev);
277 wake_up(&cdev->private->wait_q);
278 } else {
279 ccw_device_update_sense_data(cdev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100280 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
Sebastian Ott823d4942009-06-16 10:30:20 +0200282 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 case DEV_STATE_BOXED:
Sebastian Ott47593bf2009-03-31 19:16:05 +0200284 if (cdev->id.cu_type != 0) { /* device was recognized before */
285 cdev->private->flags.recog_done = 1;
286 cdev->private->state = DEV_STATE_BOXED;
287 wake_up(&cdev->private->wait_q);
288 return;
289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 break;
291 }
292 cdev->private->state = state;
293 io_subchannel_recog_done(cdev);
Sebastian Ott156013f2009-03-31 19:16:03 +0200294 wake_up(&cdev->private->wait_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
297/*
298 * Function called from device_id.c after sense id has completed.
299 */
300void
301ccw_device_sense_id_done(struct ccw_device *cdev, int err)
302{
303 switch (err) {
304 case 0:
305 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
306 break;
307 case -ETIME: /* Sense id stopped by timeout. */
308 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
309 break;
310 default:
311 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
312 break;
313 }
314}
315
Sebastian Ott76e6fb42010-02-26 22:37:28 +0100316/**
317 * ccw_device_notify() - inform the device's driver about an event
318 * @cdev: device for which an event occured
319 * @event: event that occurred
320 *
321 * Returns:
322 * -%EINVAL if the device is offline or has no driver.
323 * -%EOPNOTSUPP if the device's driver has no notifier registered.
324 * %NOTIFY_OK if the driver wants to keep the device.
325 * %NOTIFY_BAD if the driver doesn't want to keep the device.
326 */
Cornelia Huckc820de32008-07-14 09:58:45 +0200327int ccw_device_notify(struct ccw_device *cdev, int event)
328{
Sebastian Ott76e6fb42010-02-26 22:37:28 +0100329 int ret = -EINVAL;
330
Cornelia Huckc820de32008-07-14 09:58:45 +0200331 if (!cdev->drv)
Sebastian Ott76e6fb42010-02-26 22:37:28 +0100332 goto out;
Cornelia Huckc820de32008-07-14 09:58:45 +0200333 if (!cdev->online)
Sebastian Ott76e6fb42010-02-26 22:37:28 +0100334 goto out;
Peter Oberparleiter91c36912008-08-21 19:46:39 +0200335 CIO_MSG_EVENT(2, "notify called for 0.%x.%04x, event=%d\n",
336 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
337 event);
Sebastian Ott76e6fb42010-02-26 22:37:28 +0100338 if (!cdev->drv->notify) {
339 ret = -EOPNOTSUPP;
340 goto out;
341 }
342 if (cdev->drv->notify(cdev, event))
343 ret = NOTIFY_OK;
344 else
345 ret = NOTIFY_BAD;
346out:
347 return ret;
Cornelia Huckc820de32008-07-14 09:58:45 +0200348}
349
Peter Oberparleiter91c36912008-08-21 19:46:39 +0200350static void ccw_device_oper_notify(struct ccw_device *cdev)
351{
Sebastian Ott585b9542010-10-25 16:10:34 +0200352 struct subchannel *sch = to_subchannel(cdev->dev.parent);
353
Sebastian Ott76e6fb42010-02-26 22:37:28 +0100354 if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_OK) {
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100355 /* Reenable channel measurements, if needed. */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100356 ccw_device_sched_todo(cdev, CDEV_TODO_ENABLE_CMF);
Sebastian Ott585b9542010-10-25 16:10:34 +0200357 /* Save indication for new paths. */
358 cdev->private->path_new_mask = sch->vpm;
Peter Oberparleiter91c36912008-08-21 19:46:39 +0200359 return;
360 }
361 /* Driver doesn't want device back. */
362 ccw_device_set_notoper(cdev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100363 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366/*
367 * Finished with online/offline processing.
368 */
369static void
370ccw_device_done(struct ccw_device *cdev, int state)
371{
372 struct subchannel *sch;
373
374 sch = to_subchannel(cdev->dev.parent);
375
Cornelia Huckf1ee3282006-10-04 20:02:02 +0200376 ccw_device_set_timeout(cdev, 0);
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (state != DEV_STATE_ONLINE)
379 cio_disable_subchannel(sch);
380
381 /* Reset device status. */
382 memset(&cdev->private->irb, 0, sizeof(struct irb));
383
384 cdev->private->state = state;
385
Peter Oberparleiter9a332112009-10-06 10:34:01 +0200386 switch (state) {
387 case DEV_STATE_BOXED:
Michael Ernst139b83d2008-05-07 09:22:54 +0200388 CIO_MSG_EVENT(0, "Boxed device %04x on subchannel %04x\n",
389 cdev->private->dev_id.devno, sch->schid.sch_no);
Sebastian Ott76e6fb42010-02-26 22:37:28 +0100390 if (cdev->online &&
391 ccw_device_notify(cdev, CIO_BOXED) != NOTIFY_OK)
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100392 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott47593bf2009-03-31 19:16:05 +0200393 cdev->private->flags.donotify = 0;
Peter Oberparleiter9a332112009-10-06 10:34:01 +0200394 break;
395 case DEV_STATE_NOT_OPER:
Sebastian Ott626e4762009-09-11 10:28:17 +0200396 CIO_MSG_EVENT(0, "Device %04x gone on subchannel %04x\n",
397 cdev->private->dev_id.devno, sch->schid.sch_no);
Sebastian Ott76e6fb42010-02-26 22:37:28 +0100398 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100399 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Peter Oberparleiter6afcc772009-10-06 10:34:02 +0200400 else
401 ccw_device_set_disconnected(cdev);
Sebastian Ott626e4762009-09-11 10:28:17 +0200402 cdev->private->flags.donotify = 0;
Peter Oberparleiter9a332112009-10-06 10:34:01 +0200403 break;
404 case DEV_STATE_DISCONNECTED:
405 CIO_MSG_EVENT(0, "Disconnected device %04x on subchannel "
406 "%04x\n", cdev->private->dev_id.devno,
407 sch->schid.sch_no);
Sebastian Ott76e6fb42010-02-26 22:37:28 +0100408 if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100409 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Peter Oberparleiter9a332112009-10-06 10:34:01 +0200410 else
411 ccw_device_set_disconnected(cdev);
412 cdev->private->flags.donotify = 0;
413 break;
414 default:
415 break;
Sebastian Ott626e4762009-09-11 10:28:17 +0200416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 if (cdev->private->flags.donotify) {
419 cdev->private->flags.donotify = 0;
Peter Oberparleiter91c36912008-08-21 19:46:39 +0200420 ccw_device_oper_notify(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422 wake_up(&cdev->private->wait_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
425/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 * Start device recognition.
427 */
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100428void ccw_device_recognition(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100430 struct subchannel *sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /*
433 * We used to start here with a sense pgid to find out whether a device
434 * is locked by someone else. Unfortunately, the sense pgid command
435 * code has other meanings on devices predating the path grouping
436 * algorithm, so we start with sense id and box the device after an
437 * timeout (or if sense pgid during path verification detects the device
438 * is locked, as may happen on newer devices).
439 */
440 cdev->private->flags.recog_done = 0;
441 cdev->private->state = DEV_STATE_SENSE_ID;
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100442 if (cio_enable_subchannel(sch, (u32) (addr_t) sch)) {
443 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
444 return;
445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 ccw_device_sense_id_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
449/*
Peter Oberparleiter39f53602009-12-07 12:51:26 +0100450 * Handle events for states that use the ccw request infrastructure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 */
Peter Oberparleiter39f53602009-12-07 12:51:26 +0100452static void ccw_device_request_event(struct ccw_device *cdev, enum dev_event e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Peter Oberparleiter39f53602009-12-07 12:51:26 +0100454 switch (e) {
455 case DEV_EVENT_NOTOPER:
456 ccw_request_notoper(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 break;
Peter Oberparleiter39f53602009-12-07 12:51:26 +0100458 case DEV_EVENT_INTERRUPT:
459 ccw_request_handler(cdev);
460 break;
461 case DEV_EVENT_TIMEOUT:
462 ccw_request_timeout(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 break;
464 default:
Peter Oberparleiter39f53602009-12-07 12:51:26 +0100465 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467}
468
Sebastian Ott585b9542010-10-25 16:10:34 +0200469static void ccw_device_report_path_events(struct ccw_device *cdev)
470{
471 struct subchannel *sch = to_subchannel(cdev->dev.parent);
472 int path_event[8];
473 int chp, mask;
474
475 for (chp = 0, mask = 0x80; chp < 8; chp++, mask >>= 1) {
476 path_event[chp] = PE_NONE;
477 if (mask & cdev->private->path_gone_mask & ~(sch->vpm))
478 path_event[chp] |= PE_PATH_GONE;
479 if (mask & cdev->private->path_new_mask & sch->vpm)
480 path_event[chp] |= PE_PATH_AVAILABLE;
481 if (mask & cdev->private->pgid_reset_mask & sch->vpm)
482 path_event[chp] |= PE_PATHGROUP_ESTABLISHED;
483 }
484 if (cdev->online && cdev->drv->path_event)
485 cdev->drv->path_event(cdev, path_event);
486}
487
488static void ccw_device_reset_path_events(struct ccw_device *cdev)
489{
490 cdev->private->path_gone_mask = 0;
491 cdev->private->path_new_mask = 0;
492 cdev->private->pgid_reset_mask = 0;
493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495void
496ccw_device_verify_done(struct ccw_device *cdev, int err)
497{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200498 struct subchannel *sch;
499
500 sch = to_subchannel(cdev->dev.parent);
501 /* Update schib - pom may have changed. */
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100502 if (cio_update_schib(sch)) {
Peter Oberparleiter7c4d9642009-12-07 12:51:22 +0100503 err = -ENODEV;
504 goto callback;
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100505 }
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200506 /* Update lpm with verified path mask. */
507 sch->lpm = sch->vpm;
508 /* Repeat path verification? */
509 if (cdev->private->flags.doverify) {
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200510 ccw_device_verify_start(cdev);
511 return;
512 }
Peter Oberparleiter7c4d9642009-12-07 12:51:22 +0100513callback:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 case 0:
516 ccw_device_done(cdev, DEV_STATE_ONLINE);
517 /* Deliver fake irb to device driver, if needed. */
518 if (cdev->private->flags.fake_irb) {
519 memset(&cdev->private->irb, 0, sizeof(struct irb));
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200520 cdev->private->irb.scsw.cmd.cc = 1;
521 cdev->private->irb.scsw.cmd.fctl = SCSW_FCTL_START_FUNC;
522 cdev->private->irb.scsw.cmd.actl = SCSW_ACTL_START_PEND;
523 cdev->private->irb.scsw.cmd.stctl =
524 SCSW_STCTL_STATUS_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 cdev->private->flags.fake_irb = 0;
526 if (cdev->handler)
527 cdev->handler(cdev, cdev->private->intparm,
528 &cdev->private->irb);
529 memset(&cdev->private->irb, 0, sizeof(struct irb));
530 }
Sebastian Ott585b9542010-10-25 16:10:34 +0200531 ccw_device_report_path_events(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 break;
533 case -ETIME:
Peter Oberparleiter52ef0602009-12-07 12:51:31 +0100534 case -EUSERS:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200535 /* Reset oper notify indication after verify error. */
536 cdev->private->flags.donotify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 ccw_device_done(cdev, DEV_STATE_BOXED);
538 break;
Peter Oberparleiter7c4d9642009-12-07 12:51:22 +0100539 case -EACCES:
540 /* Reset oper notify indication after verify error. */
541 cdev->private->flags.donotify = 0;
542 ccw_device_done(cdev, DEV_STATE_DISCONNECTED);
543 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 default:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200545 /* Reset oper notify indication after verify error. */
546 cdev->private->flags.donotify = 0;
Peter Oberparleiter7c4d9642009-12-07 12:51:22 +0100547 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 break;
549 }
Sebastian Ott585b9542010-10-25 16:10:34 +0200550 ccw_device_reset_path_events(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
553/*
554 * Get device online.
555 */
556int
557ccw_device_online(struct ccw_device *cdev)
558{
559 struct subchannel *sch;
560 int ret;
561
562 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
563 (cdev->private->state != DEV_STATE_BOXED))
564 return -EINVAL;
565 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckedf22092008-04-30 13:38:39 +0200566 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (ret != 0) {
568 /* Couldn't enable the subchannel for i/o. Sick device. */
569 if (ret == -ENODEV)
570 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
571 return ret;
572 }
Peter Oberparleiter52ef0602009-12-07 12:51:31 +0100573 /* Start initial path verification. */
574 cdev->private->state = DEV_STATE_VERIFY;
575 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return 0;
577}
578
579void
580ccw_device_disband_done(struct ccw_device *cdev, int err)
581{
582 switch (err) {
583 case 0:
584 ccw_device_done(cdev, DEV_STATE_OFFLINE);
585 break;
586 case -ETIME:
587 ccw_device_done(cdev, DEV_STATE_BOXED);
588 break;
589 default:
Peter Oberparleiter3ecb0a52007-05-31 17:38:07 +0200590 cdev->private->flags.donotify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
592 break;
593 }
594}
595
596/*
597 * Shutdown device.
598 */
599int
600ccw_device_offline(struct ccw_device *cdev)
601{
602 struct subchannel *sch;
603
Peter Oberparleiterb301ea82008-09-09 12:38:59 +0200604 /* Allow ccw_device_offline while disconnected. */
605 if (cdev->private->state == DEV_STATE_DISCONNECTED ||
606 cdev->private->state == DEV_STATE_NOT_OPER) {
607 cdev->private->flags.donotify = 0;
608 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
609 return 0;
610 }
Peter Oberparleiter102e8352009-10-06 10:34:03 +0200611 if (cdev->private->state == DEV_STATE_BOXED) {
612 ccw_device_done(cdev, DEV_STATE_BOXED);
613 return 0;
614 }
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100615 if (ccw_device_is_orphan(cdev)) {
616 ccw_device_done(cdev, DEV_STATE_OFFLINE);
617 return 0;
618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 sch = to_subchannel(cdev->dev.parent);
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100620 if (cio_update_schib(sch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return -ENODEV;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200622 if (scsw_actl(&sch->schib.scsw) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return -EBUSY;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200624 if (cdev->private->state != DEV_STATE_ONLINE)
625 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 /* Are we doing path grouping? */
Peter Oberparleiter454e1fa2009-12-07 12:51:30 +0100627 if (!cdev->private->flags.pgroup) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /* No, set state offline immediately. */
629 ccw_device_done(cdev, DEV_STATE_OFFLINE);
630 return 0;
631 }
632 /* Start Set Path Group commands. */
633 cdev->private->state = DEV_STATE_DISBAND_PGID;
634 ccw_device_disband_start(cdev);
635 return 0;
636}
637
638/*
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200639 * Handle not operational event in non-special state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 */
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200641static void ccw_device_generic_notoper(struct ccw_device *cdev,
642 enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Sebastian Ott76e6fb42010-02-26 22:37:28 +0100644 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100645 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Peter Oberparleiter6afcc772009-10-06 10:34:02 +0200646 else
647 ccw_device_set_disconnected(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
650/*
Peter Oberparleiter1f1148c2009-09-11 10:28:14 +0200651 * Handle path verification event in offline state.
652 */
653static void ccw_device_offline_verify(struct ccw_device *cdev,
654 enum dev_event dev_event)
655{
656 struct subchannel *sch = to_subchannel(cdev->dev.parent);
657
658 css_schedule_eval(sch->schid);
659}
660
661/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 * Handle path verification event.
663 */
664static void
665ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
666{
667 struct subchannel *sch;
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (cdev->private->state == DEV_STATE_W4SENSE) {
670 cdev->private->flags.doverify = 1;
671 return;
672 }
673 sch = to_subchannel(cdev->dev.parent);
674 /*
675 * Since we might not just be coming from an interrupt from the
676 * subchannel we have to update the schib.
677 */
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100678 if (cio_update_schib(sch)) {
679 ccw_device_verify_done(cdev, -ENODEV);
680 return;
681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200683 if (scsw_actl(&sch->schib.scsw) != 0 ||
684 (scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_STATUS_PEND) ||
685 (scsw_stctl(&cdev->private->irb.scsw) & SCSW_STCTL_STATUS_PEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 /*
687 * No final status yet or final status not yet delivered
688 * to the device driver. Can't do path verfication now,
689 * delay until final status was delivered.
690 */
691 cdev->private->flags.doverify = 1;
692 return;
693 }
694 /* Device is idle, we can do the path verification. */
695 cdev->private->state = DEV_STATE_VERIFY;
696 ccw_device_verify_start(cdev);
697}
698
699/*
Peter Oberparleiterd7d12ef2009-12-07 12:51:32 +0100700 * Handle path verification event in boxed state.
701 */
702static void ccw_device_boxed_verify(struct ccw_device *cdev,
703 enum dev_event dev_event)
704{
705 struct subchannel *sch = to_subchannel(cdev->dev.parent);
706
707 if (cdev->online) {
708 if (cio_enable_subchannel(sch, (u32) (addr_t) sch))
709 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
710 else
711 ccw_device_online_verify(cdev, dev_event);
712 } else
713 css_schedule_eval(sch->schid);
714}
715
716/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 * Got an interrupt for a normal io (state online).
718 */
719static void
720ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
721{
722 struct irb *irb;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200723 int is_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Heiko Carstenscbb870c2010-02-26 22:37:43 +0100725 irb = (struct irb *)&S390_lowcore.irb;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200726 is_cmd = !scsw_is_tm(&irb->scsw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /* Check for unsolicited interrupt. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200728 if (!scsw_is_solicited(&irb->scsw)) {
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200729 if (is_cmd && (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 !irb->esw.esw0.erw.cons) {
731 /* Unit check but no sense data. Need basic sense. */
732 if (ccw_device_do_sense(cdev, irb) != 0)
733 goto call_handler_unsol;
Cornelia Hucke0ec5742006-06-04 02:51:27 -0700734 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 cdev->private->state = DEV_STATE_W4SENSE;
736 cdev->private->intparm = 0;
737 return;
738 }
739call_handler_unsol:
740 if (cdev->handler)
741 cdev->handler (cdev, 0, irb);
Cornelia Huck18374d32007-02-05 21:17:09 +0100742 if (cdev->private->flags.doverify)
743 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return;
745 }
746 /* Accumulate status and find out if a basic sense is needed. */
747 ccw_device_accumulate_irb(cdev, irb);
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200748 if (is_cmd && cdev->private->flags.dosense) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (ccw_device_do_sense(cdev, irb) == 0) {
750 cdev->private->state = DEV_STATE_W4SENSE;
751 }
752 return;
753 }
754 /* Call the handler. */
755 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
756 /* Start delayed path verification. */
757 ccw_device_online_verify(cdev, 0);
758}
759
760/*
761 * Got an timeout in online state.
762 */
763static void
764ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
765{
766 int ret;
767
768 ccw_device_set_timeout(cdev, 0);
769 ret = ccw_device_cancel_halt_clear(cdev);
770 if (ret == -EBUSY) {
771 ccw_device_set_timeout(cdev, 3*HZ);
772 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
773 return;
774 }
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200775 if (ret == -ENODEV)
776 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
777 else if (cdev->handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 cdev->handler(cdev, cdev->private->intparm,
779 ERR_PTR(-ETIMEDOUT));
780}
781
782/*
783 * Got an interrupt for a basic sense.
784 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100785static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
787{
788 struct irb *irb;
789
Heiko Carstenscbb870c2010-02-26 22:37:43 +0100790 irb = (struct irb *)&S390_lowcore.irb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 /* Check for unsolicited interrupt. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200792 if (scsw_stctl(&irb->scsw) ==
793 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
794 if (scsw_cc(&irb->scsw) == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /* Basic sense hasn't started. Try again. */
796 ccw_device_do_sense(cdev, irb);
797 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200798 CIO_MSG_EVENT(0, "0.%x.%04x: unsolicited "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200799 "interrupt during w4sense...\n",
800 cdev->private->dev_id.ssid,
801 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (cdev->handler)
803 cdev->handler (cdev, 0, irb);
804 }
805 return;
806 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800807 /*
808 * Check if a halt or clear has been issued in the meanwhile. If yes,
809 * only deliver the halt/clear interrupt to the device driver as if it
810 * had killed the original request.
811 */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200812 if (scsw_fctl(&irb->scsw) &
813 (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
Cornelia Huck3ba19982006-03-24 03:15:12 -0800814 cdev->private->flags.dosense = 0;
815 memset(&cdev->private->irb, 0, sizeof(struct irb));
816 ccw_device_accumulate_irb(cdev, irb);
817 goto call_handler;
818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /* Add basic sense info to irb. */
820 ccw_device_accumulate_basic_sense(cdev, irb);
821 if (cdev->private->flags.dosense) {
822 /* Another basic sense is needed. */
823 ccw_device_do_sense(cdev, irb);
824 return;
825 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800826call_handler:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 cdev->private->state = DEV_STATE_ONLINE;
Michael Ernst217ee6c2009-09-11 10:28:21 +0200828 /* In case sensing interfered with setting the device online */
829 wake_up(&cdev->private->wait_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 /* Call the handler. */
831 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
832 /* Start delayed path verification. */
833 ccw_device_online_verify(cdev, 0);
834}
835
836static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
838{
839 struct subchannel *sch;
840
841 sch = to_subchannel(cdev->dev.parent);
842 ccw_device_set_timeout(cdev, 0);
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100843 /* Start delayed path verification. */
844 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 /* OK, i/o is dead now. Call interrupt handler. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (cdev->handler)
847 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200848 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
851static void
852ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
853{
854 int ret;
855
856 ret = ccw_device_cancel_halt_clear(cdev);
857 if (ret == -EBUSY) {
858 ccw_device_set_timeout(cdev, 3*HZ);
859 return;
860 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100861 /* Start delayed path verification. */
862 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (cdev->handler)
864 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200865 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
Cornelia Huckc820de32008-07-14 09:58:45 +0200868void ccw_device_kill_io(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 ret = ccw_device_cancel_halt_clear(cdev);
873 if (ret == -EBUSY) {
874 ccw_device_set_timeout(cdev, 3*HZ);
875 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
876 return;
877 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100878 /* Start delayed path verification. */
879 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (cdev->handler)
881 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200882 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
885static void
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200886ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200888 /* Start verification after current task finished. */
Cornelia Huck7e560812006-07-12 16:40:19 +0200889 cdev->private->flags.doverify = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
892static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
894{
895 struct subchannel *sch;
896
897 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckedf22092008-04-30 13:38:39 +0200898 if (cio_enable_subchannel(sch, (u32)(addr_t)sch) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /* Couldn't enable the subchannel for i/o. Sick device. */
900 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
902 ccw_device_sense_id_start(cdev);
903}
904
Cornelia Huckc820de32008-07-14 09:58:45 +0200905void ccw_device_trigger_reprobe(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Cornelia Huckc820de32008-07-14 09:58:45 +0200907 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (cdev->private->state != DEV_STATE_DISCONNECTED)
910 return;
911
Cornelia Huckc820de32008-07-14 09:58:45 +0200912 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /* Update some values. */
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100914 if (cio_update_schib(sch))
Cornelia Huck7674da72006-12-08 15:54:21 +0100915 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 /*
917 * The pim, pam, pom values may not be accurate, but they are the best
918 * we have before performing device selection :/
919 */
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200920 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Sebastian Ott13952ec2008-12-25 13:39:13 +0100921 /*
922 * Use the initial configuration since we can't be shure that the old
923 * paths are valid.
924 */
925 io_subchannel_init_config(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +0100926 if (cio_commit_config(sch))
927 return;
Sebastian Ott13952ec2008-12-25 13:39:13 +0100928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 /* We should also udate ssd info, but this has to wait. */
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100930 /* Check if this is another device which appeared on the same sch. */
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100931 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno)
932 css_schedule_eval(sch->schid);
933 else
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100934 ccw_device_start_id(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Peter Oberparleiter16b9a052009-12-07 12:51:24 +0100937static void ccw_device_disabled_irq(struct ccw_device *cdev,
938 enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 struct subchannel *sch;
941
942 sch = to_subchannel(cdev->dev.parent);
943 /*
Peter Oberparleiter16b9a052009-12-07 12:51:24 +0100944 * An interrupt in a disabled state means a previous disable was not
Cornelia Hucked04b892009-03-26 15:24:06 +0100945 * successful - should not happen, but we try to disable again.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 */
947 cio_disable_subchannel(sch);
948}
949
950static void
951ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
952{
953 retry_set_schib(cdev);
954 cdev->private->state = DEV_STATE_ONLINE;
955 dev_fsm_event(cdev, dev_event);
956}
957
Cornelia Huck94bb0632006-06-29 15:08:41 +0200958static void ccw_device_update_cmfblock(struct ccw_device *cdev,
959 enum dev_event dev_event)
960{
961 cmf_retry_copy_block(cdev);
962 cdev->private->state = DEV_STATE_ONLINE;
963 dev_fsm_event(cdev, dev_event);
964}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966static void
967ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
968{
969 ccw_device_set_timeout(cdev, 0);
Sebastian Ott56e6b792009-12-07 12:51:35 +0100970 cdev->private->state = DEV_STATE_NOT_OPER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 wake_up(&cdev->private->wait_q);
972}
973
974static void
975ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
976{
977 int ret;
978
979 ret = ccw_device_cancel_halt_clear(cdev);
Sebastian Ott56e6b792009-12-07 12:51:35 +0100980 if (ret == -EBUSY) {
981 ccw_device_set_timeout(cdev, HZ/10);
982 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 cdev->private->state = DEV_STATE_NOT_OPER;
984 wake_up(&cdev->private->wait_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986}
987
988/*
989 * No operation action. This is used e.g. to ignore a timeout event in
990 * state offline.
991 */
992static void
993ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
994{
995}
996
997/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 * device statemachine
999 */
1000fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1001 [DEV_STATE_NOT_OPER] = {
1002 [DEV_EVENT_NOTOPER] = ccw_device_nop,
Peter Oberparleiter16b9a052009-12-07 12:51:24 +01001003 [DEV_EVENT_INTERRUPT] = ccw_device_disabled_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1005 [DEV_EVENT_VERIFY] = ccw_device_nop,
1006 },
1007 [DEV_STATE_SENSE_PGID] = {
Peter Oberparleiter9679baa2009-12-07 12:51:27 +01001008 [DEV_EVENT_NOTOPER] = ccw_device_request_event,
1009 [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
1010 [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 [DEV_EVENT_VERIFY] = ccw_device_nop,
1012 },
1013 [DEV_STATE_SENSE_ID] = {
Peter Oberparleiter39f53602009-12-07 12:51:26 +01001014 [DEV_EVENT_NOTOPER] = ccw_device_request_event,
1015 [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
1016 [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 [DEV_EVENT_VERIFY] = ccw_device_nop,
1018 },
1019 [DEV_STATE_OFFLINE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001020 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Peter Oberparleiter16b9a052009-12-07 12:51:24 +01001021 [DEV_EVENT_INTERRUPT] = ccw_device_disabled_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
Peter Oberparleiter1f1148c2009-09-11 10:28:14 +02001023 [DEV_EVENT_VERIFY] = ccw_device_offline_verify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 },
1025 [DEV_STATE_VERIFY] = {
Peter Oberparleiter9679baa2009-12-07 12:51:27 +01001026 [DEV_EVENT_NOTOPER] = ccw_device_request_event,
1027 [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
1028 [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001029 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 },
1031 [DEV_STATE_ONLINE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001032 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1034 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1035 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1036 },
1037 [DEV_STATE_W4SENSE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001038 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1040 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1041 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1042 },
1043 [DEV_STATE_DISBAND_PGID] = {
Peter Oberparleiter9679baa2009-12-07 12:51:27 +01001044 [DEV_EVENT_NOTOPER] = ccw_device_request_event,
1045 [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
1046 [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 [DEV_EVENT_VERIFY] = ccw_device_nop,
1048 },
1049 [DEV_STATE_BOXED] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001050 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Peter Oberparleiterd7d12ef2009-12-07 12:51:32 +01001051 [DEV_EVENT_INTERRUPT] = ccw_device_nop,
1052 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1053 [DEV_EVENT_VERIFY] = ccw_device_boxed_verify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 },
1055 /* states to wait for i/o completion before doing something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 [DEV_STATE_TIMEOUT_KILL] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001057 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1059 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1060 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1061 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 [DEV_STATE_QUIESCE] = {
1063 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1064 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1065 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1066 [DEV_EVENT_VERIFY] = ccw_device_nop,
1067 },
1068 /* special states for devices gone not operational */
1069 [DEV_STATE_DISCONNECTED] = {
1070 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1071 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
Peter Oberparleiter16b9a052009-12-07 12:51:24 +01001072 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001073 [DEV_EVENT_VERIFY] = ccw_device_start_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 },
1075 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
Peter Oberparleiter39f53602009-12-07 12:51:26 +01001076 [DEV_EVENT_NOTOPER] = ccw_device_request_event,
1077 [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
1078 [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 [DEV_EVENT_VERIFY] = ccw_device_nop,
1080 },
1081 [DEV_STATE_CMFCHANGE] = {
1082 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1083 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1084 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1085 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1086 },
Cornelia Huck94bb0632006-06-29 15:08:41 +02001087 [DEV_STATE_CMFUPDATE] = {
1088 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1089 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1090 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1091 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1092 },
Peter Oberparleiterd7d12ef2009-12-07 12:51:32 +01001093 [DEV_STATE_STEAL_LOCK] = {
1094 [DEV_EVENT_NOTOPER] = ccw_device_request_event,
1095 [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
1096 [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
1097 [DEV_EVENT_VERIFY] = ccw_device_nop,
1098 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099};
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101EXPORT_SYMBOL_GPL(ccw_device_set_timeout);