blob: b163743bf5863b16038166d1f86d0819820c72e1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/cio/device_fsm.c
3 * finite state machine for device handling
4 *
Cornelia Huckc820de32008-07-14 09:58:45 +02005 * Copyright IBM Corp. 2002,2008
Cornelia Huck4ce3b302006-01-14 13:21:04 -08006 * Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Martin Schwidefsky (schwidefsky@de.ibm.com)
8 */
9
10#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/init.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080012#include <linux/jiffies.h>
13#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <asm/ccwdev.h>
Cornelia Huck4c24da72005-09-03 15:58:01 -070016#include <asm/cio.h>
Peter Oberparleitere5854a52007-04-27 16:01:31 +020017#include <asm/chpid.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include "cio.h"
20#include "cio_debug.h"
21#include "css.h"
22#include "device.h"
23#include "chsc.h"
24#include "ioasm.h"
Peter Oberparleitere6b6e102007-04-27 16:01:28 +020025#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Sebastian Ott14ff56b2008-01-26 14:10:37 +010027static int timeout_log_enabled;
28
Sebastian Ott14ff56b2008-01-26 14:10:37 +010029static int __init ccw_timeout_log_setup(char *unused)
30{
31 timeout_log_enabled = 1;
32 return 1;
33}
34
35__setup("ccw_timeout_log", ccw_timeout_log_setup);
36
37static void ccw_timeout_log(struct ccw_device *cdev)
38{
39 struct schib schib;
40 struct subchannel *sch;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +010041 struct io_subchannel_private *private;
Peter Oberparleiter83262d62008-07-14 09:58:51 +020042 union orb *orb;
Sebastian Ott14ff56b2008-01-26 14:10:37 +010043 int cc;
44
45 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +010046 private = to_io_private(sch);
Peter Oberparleiter83262d62008-07-14 09:58:51 +020047 orb = &private->orb;
Sebastian Ott14ff56b2008-01-26 14:10:37 +010048 cc = stsch(sch->schid, &schib);
49
50 printk(KERN_WARNING "cio: ccw device timeout occurred at %llx, "
51 "device information:\n", get_clock());
52 printk(KERN_WARNING "cio: orb:\n");
53 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
Peter Oberparleiter83262d62008-07-14 09:58:51 +020054 orb, sizeof(*orb), 0);
Kay Sievers2a0217d2008-10-10 21:33:09 +020055 printk(KERN_WARNING "cio: ccw device bus id: %s\n",
56 dev_name(&cdev->dev));
57 printk(KERN_WARNING "cio: subchannel bus id: %s\n",
58 dev_name(&sch->dev));
Sebastian Ott14ff56b2008-01-26 14:10:37 +010059 printk(KERN_WARNING "cio: subchannel lpm: %02x, opm: %02x, "
60 "vpm: %02x\n", sch->lpm, sch->opm, sch->vpm);
61
Peter Oberparleiter83262d62008-07-14 09:58:51 +020062 if (orb->tm.b) {
63 printk(KERN_WARNING "cio: orb indicates transport mode\n");
64 printk(KERN_WARNING "cio: last tcw:\n");
65 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
66 (void *)(addr_t)orb->tm.tcw,
67 sizeof(struct tcw), 0);
68 } else {
69 printk(KERN_WARNING "cio: orb indicates command mode\n");
70 if ((void *)(addr_t)orb->cmd.cpa == &private->sense_ccw ||
71 (void *)(addr_t)orb->cmd.cpa == cdev->private->iccws)
72 printk(KERN_WARNING "cio: last channel program "
73 "(intern):\n");
74 else
75 printk(KERN_WARNING "cio: last channel program:\n");
Sebastian Ott14ff56b2008-01-26 14:10:37 +010076
Peter Oberparleiter83262d62008-07-14 09:58:51 +020077 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
78 (void *)(addr_t)orb->cmd.cpa,
79 sizeof(struct ccw1), 0);
80 }
Sebastian Ott14ff56b2008-01-26 14:10:37 +010081 printk(KERN_WARNING "cio: ccw device state: %d\n",
82 cdev->private->state);
83 printk(KERN_WARNING "cio: store subchannel returned: cc=%d\n", cc);
84 printk(KERN_WARNING "cio: schib:\n");
85 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
86 &schib, sizeof(schib), 0);
87 printk(KERN_WARNING "cio: ccw device flags:\n");
88 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
89 &cdev->private->flags, sizeof(cdev->private->flags), 0);
90}
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
94 */
95static void
96ccw_device_timeout(unsigned long data)
97{
98 struct ccw_device *cdev;
99
100 cdev = (struct ccw_device *) data;
101 spin_lock_irq(cdev->ccwlock);
Sebastian Ott14ff56b2008-01-26 14:10:37 +0100102 if (timeout_log_enabled)
103 ccw_timeout_log(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
105 spin_unlock_irq(cdev->ccwlock);
106}
107
108/*
109 * Set timeout
110 */
111void
112ccw_device_set_timeout(struct ccw_device *cdev, int expires)
113{
114 if (expires == 0) {
115 del_timer(&cdev->private->timer);
116 return;
117 }
118 if (timer_pending(&cdev->private->timer)) {
119 if (mod_timer(&cdev->private->timer, jiffies + expires))
120 return;
121 }
122 cdev->private->timer.function = ccw_device_timeout;
123 cdev->private->timer.data = (unsigned long) cdev;
124 cdev->private->timer.expires = jiffies + expires;
125 add_timer(&cdev->private->timer);
126}
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/*
129 * Cancel running i/o. This is called repeatedly since halt/clear are
130 * asynchronous operations. We do one try with cio_cancel, two tries
131 * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
132 * Returns 0 if device now idle, -ENODEV for device not operational and
133 * -EBUSY if an interrupt is expected (either from halt/clear or from a
134 * status pending).
135 */
136int
137ccw_device_cancel_halt_clear(struct ccw_device *cdev)
138{
139 struct subchannel *sch;
140 int ret;
141
142 sch = to_subchannel(cdev->dev.parent);
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100143 if (cio_update_schib(sch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return -ENODEV;
Cornelia Huck2470b642007-03-05 23:36:02 +0100145 if (!sch->schib.pmcw.ena)
146 /* Not operational -> done. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return 0;
148 /* Stage 1: cancel io. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200149 if (!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_HALT_PEND) &&
150 !(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_CLEAR_PEND)) {
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200151 if (!scsw_is_tm(&sch->schib.scsw)) {
152 ret = cio_cancel(sch);
153 if (ret != -EINVAL)
154 return ret;
155 }
156 /* cancel io unsuccessful or not applicable (transport mode).
157 * Continue with asynchronous instructions. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 cdev->private->iretry = 3; /* 3 halt retries. */
159 }
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200160 if (!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_CLEAR_PEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 /* Stage 2: halt io. */
162 if (cdev->private->iretry) {
163 cdev->private->iretry--;
164 ret = cio_halt(sch);
Peter Oberparleiterba4ba8a2006-07-27 14:00:23 +0200165 if (ret != -EBUSY)
166 return (ret == 0) ? -EBUSY : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168 /* halt io unsuccessful. */
169 cdev->private->iretry = 255; /* 255 clear retries. */
170 }
171 /* Stage 3: clear io. */
172 if (cdev->private->iretry) {
173 cdev->private->iretry--;
174 ret = cio_clear (sch);
175 return (ret == 0) ? -EBUSY : ret;
176 }
177 panic("Can't stop i/o on subchannel.\n");
178}
179
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
232 ccw_device_set_timeout(cdev, 0);
233 cio_disable_subchannel(sch);
234 /*
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:
Michael Ernst139b83d2008-05-07 09:22:54 +0200266 CIO_MSG_EVENT(2, "SenseID : unknown device %04x on "
267 "subchannel 0.%x.%04x\n",
268 cdev->private->dev_id.devno,
269 sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 break;
271 case DEV_STATE_OFFLINE:
Sebastian Ott823d4942009-06-16 10:30:20 +0200272 if (!cdev->online) {
273 ccw_device_update_sense_data(cdev);
274 /* Issue device info message. */
275 CIO_MSG_EVENT(4, "SenseID : device 0.%x.%04x reports: "
276 "CU Type/Mod = %04X/%02X, Dev Type/Mod "
277 "= %04X/%02X\n",
278 cdev->private->dev_id.ssid,
279 cdev->private->dev_id.devno,
280 cdev->id.cu_type, cdev->id.cu_model,
281 cdev->id.dev_type, cdev->id.dev_model);
282 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
Sebastian Ott823d4942009-06-16 10:30:20 +0200284 cdev->private->state = DEV_STATE_OFFLINE;
285 cdev->private->flags.recog_done = 1;
286 if (ccw_device_test_sense_data(cdev)) {
287 cdev->private->flags.donotify = 1;
288 ccw_device_online(cdev);
289 wake_up(&cdev->private->wait_q);
290 } else {
291 ccw_device_update_sense_data(cdev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100292 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Sebastian Ott823d4942009-06-16 10:30:20 +0200294 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 case DEV_STATE_BOXED:
Michael Ernst139b83d2008-05-07 09:22:54 +0200296 CIO_MSG_EVENT(0, "SenseID : boxed device %04x on "
297 " subchannel 0.%x.%04x\n",
298 cdev->private->dev_id.devno,
299 sch->schid.ssid, sch->schid.sch_no);
Sebastian Ott47593bf2009-03-31 19:16:05 +0200300 if (cdev->id.cu_type != 0) { /* device was recognized before */
301 cdev->private->flags.recog_done = 1;
302 cdev->private->state = DEV_STATE_BOXED;
303 wake_up(&cdev->private->wait_q);
304 return;
305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 break;
307 }
308 cdev->private->state = state;
309 io_subchannel_recog_done(cdev);
Sebastian Ott156013f2009-03-31 19:16:03 +0200310 wake_up(&cdev->private->wait_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313/*
314 * Function called from device_id.c after sense id has completed.
315 */
316void
317ccw_device_sense_id_done(struct ccw_device *cdev, int err)
318{
319 switch (err) {
320 case 0:
321 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
322 break;
323 case -ETIME: /* Sense id stopped by timeout. */
324 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
325 break;
326 default:
327 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
328 break;
329 }
330}
331
Cornelia Huckc820de32008-07-14 09:58:45 +0200332int ccw_device_notify(struct ccw_device *cdev, int event)
333{
334 if (!cdev->drv)
335 return 0;
336 if (!cdev->online)
337 return 0;
Peter Oberparleiter91c36912008-08-21 19:46:39 +0200338 CIO_MSG_EVENT(2, "notify called for 0.%x.%04x, event=%d\n",
339 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
340 event);
Cornelia Huckc820de32008-07-14 09:58:45 +0200341 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
342}
343
Peter Oberparleiter91c36912008-08-21 19:46:39 +0200344static void ccw_device_oper_notify(struct ccw_device *cdev)
345{
346 if (ccw_device_notify(cdev, CIO_OPER)) {
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100347 /* Reenable channel measurements, if needed. */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100348 ccw_device_sched_todo(cdev, CDEV_TODO_ENABLE_CMF);
Peter Oberparleiter91c36912008-08-21 19:46:39 +0200349 return;
350 }
351 /* Driver doesn't want device back. */
352 ccw_device_set_notoper(cdev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100353 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
356/*
357 * Finished with online/offline processing.
358 */
359static void
360ccw_device_done(struct ccw_device *cdev, int state)
361{
362 struct subchannel *sch;
363
364 sch = to_subchannel(cdev->dev.parent);
365
Cornelia Huckf1ee3282006-10-04 20:02:02 +0200366 ccw_device_set_timeout(cdev, 0);
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (state != DEV_STATE_ONLINE)
369 cio_disable_subchannel(sch);
370
371 /* Reset device status. */
372 memset(&cdev->private->irb, 0, sizeof(struct irb));
373
374 cdev->private->state = state;
375
Peter Oberparleiter9a332112009-10-06 10:34:01 +0200376 switch (state) {
377 case DEV_STATE_BOXED:
Michael Ernst139b83d2008-05-07 09:22:54 +0200378 CIO_MSG_EVENT(0, "Boxed device %04x on subchannel %04x\n",
379 cdev->private->dev_id.devno, sch->schid.sch_no);
Sebastian Ott47593bf2009-03-31 19:16:05 +0200380 if (cdev->online && !ccw_device_notify(cdev, CIO_BOXED))
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100381 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott47593bf2009-03-31 19:16:05 +0200382 cdev->private->flags.donotify = 0;
Peter Oberparleiter9a332112009-10-06 10:34:01 +0200383 break;
384 case DEV_STATE_NOT_OPER:
Sebastian Ott626e4762009-09-11 10:28:17 +0200385 CIO_MSG_EVENT(0, "Device %04x gone on subchannel %04x\n",
386 cdev->private->dev_id.devno, sch->schid.sch_no);
387 if (!ccw_device_notify(cdev, CIO_GONE))
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100388 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Peter Oberparleiter6afcc772009-10-06 10:34:02 +0200389 else
390 ccw_device_set_disconnected(cdev);
Sebastian Ott626e4762009-09-11 10:28:17 +0200391 cdev->private->flags.donotify = 0;
Peter Oberparleiter9a332112009-10-06 10:34:01 +0200392 break;
393 case DEV_STATE_DISCONNECTED:
394 CIO_MSG_EVENT(0, "Disconnected device %04x on subchannel "
395 "%04x\n", cdev->private->dev_id.devno,
396 sch->schid.sch_no);
397 if (!ccw_device_notify(cdev, CIO_NO_PATH))
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100398 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Peter Oberparleiter9a332112009-10-06 10:34:01 +0200399 else
400 ccw_device_set_disconnected(cdev);
401 cdev->private->flags.donotify = 0;
402 break;
403 default:
404 break;
Sebastian Ott626e4762009-09-11 10:28:17 +0200405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 if (cdev->private->flags.donotify) {
408 cdev->private->flags.donotify = 0;
Peter Oberparleiter91c36912008-08-21 19:46:39 +0200409 ccw_device_oper_notify(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411 wake_up(&cdev->private->wait_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100414static int cmp_pgid(struct pgid *p1, struct pgid *p2)
Cornelia Huck7e560812006-07-12 16:40:19 +0200415{
416 char *c1;
417 char *c2;
418
419 c1 = (char *)p1;
420 c2 = (char *)p2;
421
422 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
423}
424
425static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
426{
427 int i;
428 int last;
429
430 last = 0;
431 for (i = 0; i < 8; i++) {
432 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
433 /* No PGID yet */
434 continue;
435 if (cdev->private->pgid[last].inf.ps.state1 ==
436 SNID_STATE1_RESET) {
437 /* First non-zero PGID */
438 last = i;
439 continue;
440 }
441 if (cmp_pgid(&cdev->private->pgid[i],
442 &cdev->private->pgid[last]) == 0)
443 /* Non-conflicting PGIDs */
444 continue;
445
446 /* PGID mismatch, can't pathgroup. */
447 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
448 "0.%x.%04x, can't pathgroup\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200449 cdev->private->dev_id.ssid,
450 cdev->private->dev_id.devno);
Cornelia Huck7e560812006-07-12 16:40:19 +0200451 cdev->private->options.pgroup = 0;
452 return;
453 }
454 if (cdev->private->pgid[last].inf.ps.state1 ==
455 SNID_STATE1_RESET)
456 /* No previous pgid found */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200457 memcpy(&cdev->private->pgid[0],
458 &channel_subsystems[0]->global_pgid,
Cornelia Huck7e560812006-07-12 16:40:19 +0200459 sizeof(struct pgid));
460 else
461 /* Use existing pgid */
462 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
463 sizeof(struct pgid));
464}
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466/*
467 * Function called from device_pgid.c after sense path ground has completed.
468 */
469void
470ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
471{
472 struct subchannel *sch;
473
474 sch = to_subchannel(cdev->dev.parent);
475 switch (err) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200476 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
477 cdev->private->options.pgroup = 0;
478 break;
479 case 0: /* success */
480 case -EACCES: /* partial success, some paths not operational */
481 /* Check if all pgids are equal or 0. */
482 __ccw_device_get_common_pgid(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 break;
484 case -ETIME: /* Sense path group id stopped by timeout. */
485 case -EUSERS: /* device is reserved for someone else. */
486 ccw_device_done(cdev, DEV_STATE_BOXED);
Cornelia Huck7e560812006-07-12 16:40:19 +0200487 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 default:
489 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Cornelia Huck7e560812006-07-12 16:40:19 +0200490 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
Cornelia Huck7e560812006-07-12 16:40:19 +0200492 /* Start Path Group verification. */
Cornelia Huck7e560812006-07-12 16:40:19 +0200493 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200494 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200495 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498/*
499 * Start device recognition.
500 */
501int
502ccw_device_recognition(struct ccw_device *cdev)
503{
504 struct subchannel *sch;
505 int ret;
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckedf22092008-04-30 13:38:39 +0200508 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (ret != 0)
510 /* Couldn't enable the subchannel for i/o. Sick device. */
511 return ret;
512
513 /* After 60s the device recognition is considered to have failed. */
514 ccw_device_set_timeout(cdev, 60*HZ);
515
516 /*
517 * We used to start here with a sense pgid to find out whether a device
518 * is locked by someone else. Unfortunately, the sense pgid command
519 * code has other meanings on devices predating the path grouping
520 * algorithm, so we start with sense id and box the device after an
521 * timeout (or if sense pgid during path verification detects the device
522 * is locked, as may happen on newer devices).
523 */
524 cdev->private->flags.recog_done = 0;
525 cdev->private->state = DEV_STATE_SENSE_ID;
526 ccw_device_sense_id_start(cdev);
527 return 0;
528}
529
530/*
531 * Handle timeout in device recognition.
532 */
533static void
534ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
535{
536 int ret;
537
538 ret = ccw_device_cancel_halt_clear(cdev);
539 switch (ret) {
540 case 0:
541 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
542 break;
543 case -ENODEV:
544 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
545 break;
546 default:
547 ccw_device_set_timeout(cdev, 3*HZ);
548 }
549}
550
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552void
553ccw_device_verify_done(struct ccw_device *cdev, int err)
554{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200555 struct subchannel *sch;
556
557 sch = to_subchannel(cdev->dev.parent);
558 /* Update schib - pom may have changed. */
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100559 if (cio_update_schib(sch)) {
560 cdev->private->flags.donotify = 0;
561 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
562 return;
563 }
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200564 /* Update lpm with verified path mask. */
565 sch->lpm = sch->vpm;
566 /* Repeat path verification? */
567 if (cdev->private->flags.doverify) {
568 cdev->private->flags.doverify = 0;
569 ccw_device_verify_start(cdev);
570 return;
571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 switch (err) {
573 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
574 cdev->private->options.pgroup = 0;
575 case 0:
576 ccw_device_done(cdev, DEV_STATE_ONLINE);
577 /* Deliver fake irb to device driver, if needed. */
578 if (cdev->private->flags.fake_irb) {
579 memset(&cdev->private->irb, 0, sizeof(struct irb));
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200580 cdev->private->irb.scsw.cmd.cc = 1;
581 cdev->private->irb.scsw.cmd.fctl = SCSW_FCTL_START_FUNC;
582 cdev->private->irb.scsw.cmd.actl = SCSW_ACTL_START_PEND;
583 cdev->private->irb.scsw.cmd.stctl =
584 SCSW_STCTL_STATUS_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 cdev->private->flags.fake_irb = 0;
586 if (cdev->handler)
587 cdev->handler(cdev, cdev->private->intparm,
588 &cdev->private->irb);
589 memset(&cdev->private->irb, 0, sizeof(struct irb));
590 }
591 break;
592 case -ETIME:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200593 /* Reset oper notify indication after verify error. */
594 cdev->private->flags.donotify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 ccw_device_done(cdev, DEV_STATE_BOXED);
596 break;
597 default:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200598 /* Reset oper notify indication after verify error. */
599 cdev->private->flags.donotify = 0;
Cornelia Huck46258ab2008-01-26 14:10:49 +0100600 if (cdev->online) {
601 ccw_device_set_timeout(cdev, 0);
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200602 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Cornelia Huck46258ab2008-01-26 14:10:49 +0100603 } else
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100604 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 break;
606 }
607}
608
609/*
610 * Get device online.
611 */
612int
613ccw_device_online(struct ccw_device *cdev)
614{
615 struct subchannel *sch;
616 int ret;
617
618 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
619 (cdev->private->state != DEV_STATE_BOXED))
620 return -EINVAL;
621 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckedf22092008-04-30 13:38:39 +0200622 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (ret != 0) {
624 /* Couldn't enable the subchannel for i/o. Sick device. */
625 if (ret == -ENODEV)
626 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
627 return ret;
628 }
629 /* Do we want to do path grouping? */
630 if (!cdev->private->options.pgroup) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200631 /* Start initial path verification. */
632 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200633 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200634 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return 0;
636 }
637 /* Do a SensePGID first. */
638 cdev->private->state = DEV_STATE_SENSE_PGID;
639 ccw_device_sense_pgid_start(cdev);
640 return 0;
641}
642
643void
644ccw_device_disband_done(struct ccw_device *cdev, int err)
645{
646 switch (err) {
647 case 0:
648 ccw_device_done(cdev, DEV_STATE_OFFLINE);
649 break;
650 case -ETIME:
651 ccw_device_done(cdev, DEV_STATE_BOXED);
652 break;
653 default:
Peter Oberparleiter3ecb0a52007-05-31 17:38:07 +0200654 cdev->private->flags.donotify = 0;
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200655 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
657 break;
658 }
659}
660
661/*
662 * Shutdown device.
663 */
664int
665ccw_device_offline(struct ccw_device *cdev)
666{
667 struct subchannel *sch;
668
Peter Oberparleiterb301ea82008-09-09 12:38:59 +0200669 /* Allow ccw_device_offline while disconnected. */
670 if (cdev->private->state == DEV_STATE_DISCONNECTED ||
671 cdev->private->state == DEV_STATE_NOT_OPER) {
672 cdev->private->flags.donotify = 0;
673 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
674 return 0;
675 }
Peter Oberparleiter102e8352009-10-06 10:34:03 +0200676 if (cdev->private->state == DEV_STATE_BOXED) {
677 ccw_device_done(cdev, DEV_STATE_BOXED);
678 return 0;
679 }
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100680 if (ccw_device_is_orphan(cdev)) {
681 ccw_device_done(cdev, DEV_STATE_OFFLINE);
682 return 0;
683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 sch = to_subchannel(cdev->dev.parent);
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100685 if (cio_update_schib(sch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return -ENODEV;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200687 if (scsw_actl(&sch->schib.scsw) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return -EBUSY;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200689 if (cdev->private->state != DEV_STATE_ONLINE)
690 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /* Are we doing path grouping? */
692 if (!cdev->private->options.pgroup) {
693 /* No, set state offline immediately. */
694 ccw_device_done(cdev, DEV_STATE_OFFLINE);
695 return 0;
696 }
697 /* Start Set Path Group commands. */
698 cdev->private->state = DEV_STATE_DISBAND_PGID;
699 ccw_device_disband_start(cdev);
700 return 0;
701}
702
703/*
704 * Handle timeout in device online/offline process.
705 */
706static void
707ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
708{
709 int ret;
710
711 ret = ccw_device_cancel_halt_clear(cdev);
712 switch (ret) {
713 case 0:
714 ccw_device_done(cdev, DEV_STATE_BOXED);
715 break;
716 case -ENODEV:
717 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
718 break;
719 default:
720 ccw_device_set_timeout(cdev, 3*HZ);
721 }
722}
723
724/*
725 * Handle not oper event in device recognition.
726 */
727static void
728ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
729{
730 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
731}
732
733/*
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200734 * Handle not operational event in non-special state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 */
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200736static void ccw_device_generic_notoper(struct ccw_device *cdev,
737 enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Peter Oberparleiter6afcc772009-10-06 10:34:02 +0200739 if (!ccw_device_notify(cdev, CIO_GONE))
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100740 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Peter Oberparleiter6afcc772009-10-06 10:34:02 +0200741 else
742 ccw_device_set_disconnected(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
745/*
Peter Oberparleiter1f1148c2009-09-11 10:28:14 +0200746 * Handle path verification event in offline state.
747 */
748static void ccw_device_offline_verify(struct ccw_device *cdev,
749 enum dev_event dev_event)
750{
751 struct subchannel *sch = to_subchannel(cdev->dev.parent);
752
753 css_schedule_eval(sch->schid);
754}
755
756/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 * Handle path verification event.
758 */
759static void
760ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
761{
762 struct subchannel *sch;
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (cdev->private->state == DEV_STATE_W4SENSE) {
765 cdev->private->flags.doverify = 1;
766 return;
767 }
768 sch = to_subchannel(cdev->dev.parent);
769 /*
770 * Since we might not just be coming from an interrupt from the
771 * subchannel we have to update the schib.
772 */
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100773 if (cio_update_schib(sch)) {
774 ccw_device_verify_done(cdev, -ENODEV);
775 return;
776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200778 if (scsw_actl(&sch->schib.scsw) != 0 ||
779 (scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_STATUS_PEND) ||
780 (scsw_stctl(&cdev->private->irb.scsw) & SCSW_STCTL_STATUS_PEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /*
782 * No final status yet or final status not yet delivered
783 * to the device driver. Can't do path verfication now,
784 * delay until final status was delivered.
785 */
786 cdev->private->flags.doverify = 1;
787 return;
788 }
789 /* Device is idle, we can do the path verification. */
790 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200791 cdev->private->flags.doverify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 ccw_device_verify_start(cdev);
793}
794
795/*
796 * Got an interrupt for a normal io (state online).
797 */
798static void
799ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
800{
801 struct irb *irb;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200802 int is_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 irb = (struct irb *) __LC_IRB;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200805 is_cmd = !scsw_is_tm(&irb->scsw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 /* Check for unsolicited interrupt. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200807 if (!scsw_is_solicited(&irb->scsw)) {
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200808 if (is_cmd && (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 !irb->esw.esw0.erw.cons) {
810 /* Unit check but no sense data. Need basic sense. */
811 if (ccw_device_do_sense(cdev, irb) != 0)
812 goto call_handler_unsol;
Cornelia Hucke0ec5742006-06-04 02:51:27 -0700813 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 cdev->private->state = DEV_STATE_W4SENSE;
815 cdev->private->intparm = 0;
816 return;
817 }
818call_handler_unsol:
819 if (cdev->handler)
820 cdev->handler (cdev, 0, irb);
Cornelia Huck18374d32007-02-05 21:17:09 +0100821 if (cdev->private->flags.doverify)
822 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return;
824 }
825 /* Accumulate status and find out if a basic sense is needed. */
826 ccw_device_accumulate_irb(cdev, irb);
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200827 if (is_cmd && cdev->private->flags.dosense) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (ccw_device_do_sense(cdev, irb) == 0) {
829 cdev->private->state = DEV_STATE_W4SENSE;
830 }
831 return;
832 }
833 /* Call the handler. */
834 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
835 /* Start delayed path verification. */
836 ccw_device_online_verify(cdev, 0);
837}
838
839/*
840 * Got an timeout in online state.
841 */
842static void
843ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
844{
845 int ret;
846
847 ccw_device_set_timeout(cdev, 0);
848 ret = ccw_device_cancel_halt_clear(cdev);
849 if (ret == -EBUSY) {
850 ccw_device_set_timeout(cdev, 3*HZ);
851 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
852 return;
853 }
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200854 if (ret == -ENODEV)
855 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
856 else if (cdev->handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 cdev->handler(cdev, cdev->private->intparm,
858 ERR_PTR(-ETIMEDOUT));
859}
860
861/*
862 * Got an interrupt for a basic sense.
863 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100864static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
866{
867 struct irb *irb;
868
869 irb = (struct irb *) __LC_IRB;
870 /* Check for unsolicited interrupt. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200871 if (scsw_stctl(&irb->scsw) ==
872 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
873 if (scsw_cc(&irb->scsw) == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 /* Basic sense hasn't started. Try again. */
875 ccw_device_do_sense(cdev, irb);
876 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200877 CIO_MSG_EVENT(0, "0.%x.%04x: unsolicited "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200878 "interrupt during w4sense...\n",
879 cdev->private->dev_id.ssid,
880 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (cdev->handler)
882 cdev->handler (cdev, 0, irb);
883 }
884 return;
885 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800886 /*
887 * Check if a halt or clear has been issued in the meanwhile. If yes,
888 * only deliver the halt/clear interrupt to the device driver as if it
889 * had killed the original request.
890 */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200891 if (scsw_fctl(&irb->scsw) &
892 (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
Cornelia Huckd23861f2006-12-04 15:41:04 +0100893 /* Retry Basic Sense if requested. */
894 if (cdev->private->flags.intretry) {
895 cdev->private->flags.intretry = 0;
896 ccw_device_do_sense(cdev, irb);
897 return;
898 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800899 cdev->private->flags.dosense = 0;
900 memset(&cdev->private->irb, 0, sizeof(struct irb));
901 ccw_device_accumulate_irb(cdev, irb);
902 goto call_handler;
903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 /* Add basic sense info to irb. */
905 ccw_device_accumulate_basic_sense(cdev, irb);
906 if (cdev->private->flags.dosense) {
907 /* Another basic sense is needed. */
908 ccw_device_do_sense(cdev, irb);
909 return;
910 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800911call_handler:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 cdev->private->state = DEV_STATE_ONLINE;
Michael Ernst217ee6c2009-09-11 10:28:21 +0200913 /* In case sensing interfered with setting the device online */
914 wake_up(&cdev->private->wait_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 /* Call the handler. */
916 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
917 /* Start delayed path verification. */
918 ccw_device_online_verify(cdev, 0);
919}
920
921static void
922ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
923{
924 struct irb *irb;
925
926 irb = (struct irb *) __LC_IRB;
927 /* Accumulate status. We don't do basic sense. */
928 ccw_device_accumulate_irb(cdev, irb);
Cornelia Huckb4f7b1e2006-06-29 15:03:35 +0200929 /* Remember to clear irb to avoid residuals. */
930 memset(&cdev->private->irb, 0, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 /* Try to start delayed device verification. */
932 ccw_device_online_verify(cdev, 0);
933 /* Note: Don't call handler for cio initiated clear! */
934}
935
936static void
937ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
938{
939 struct subchannel *sch;
940
941 sch = to_subchannel(cdev->dev.parent);
942 ccw_device_set_timeout(cdev, 0);
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100943 /* Start delayed path verification. */
944 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 /* OK, i/o is dead now. Call interrupt handler. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (cdev->handler)
947 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200948 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
951static void
952ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
953{
954 int ret;
955
956 ret = ccw_device_cancel_halt_clear(cdev);
957 if (ret == -EBUSY) {
958 ccw_device_set_timeout(cdev, 3*HZ);
959 return;
960 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100961 /* Start delayed path verification. */
962 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (cdev->handler)
964 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200965 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
Cornelia Huckc820de32008-07-14 09:58:45 +0200968void ccw_device_kill_io(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 ret = ccw_device_cancel_halt_clear(cdev);
973 if (ret == -EBUSY) {
974 ccw_device_set_timeout(cdev, 3*HZ);
975 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
976 return;
977 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100978 /* Start delayed path verification. */
979 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (cdev->handler)
981 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200982 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
985static void
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200986ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200988 /* Start verification after current task finished. */
Cornelia Huck7e560812006-07-12 16:40:19 +0200989 cdev->private->flags.doverify = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
992static void
993ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
994{
995 struct irb *irb;
996
997 switch (dev_event) {
998 case DEV_EVENT_INTERRUPT:
999 irb = (struct irb *) __LC_IRB;
1000 /* Check for unsolicited interrupt. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001001 if ((scsw_stctl(&irb->scsw) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001003 (!scsw_cc(&irb->scsw)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 /* FIXME: we should restart stlck here, but this
1005 * is extremely unlikely ... */
1006 goto out_wakeup;
1007
1008 ccw_device_accumulate_irb(cdev, irb);
1009 /* We don't care about basic sense etc. */
1010 break;
1011 default: /* timeout */
1012 break;
1013 }
1014out_wakeup:
1015 wake_up(&cdev->private->wait_q);
1016}
1017
1018static void
1019ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1020{
1021 struct subchannel *sch;
1022
1023 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckedf22092008-04-30 13:38:39 +02001024 if (cio_enable_subchannel(sch, (u32)(addr_t)sch) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 /* Couldn't enable the subchannel for i/o. Sick device. */
1026 return;
1027
1028 /* After 60s the device recognition is considered to have failed. */
1029 ccw_device_set_timeout(cdev, 60*HZ);
1030
1031 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1032 ccw_device_sense_id_start(cdev);
1033}
1034
Cornelia Huckc820de32008-07-14 09:58:45 +02001035void ccw_device_trigger_reprobe(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Cornelia Huckc820de32008-07-14 09:58:45 +02001037 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1040 return;
1041
Cornelia Huckc820de32008-07-14 09:58:45 +02001042 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 /* Update some values. */
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001044 if (cio_update_schib(sch))
Cornelia Huck7674da72006-12-08 15:54:21 +01001045 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 /*
1047 * The pim, pam, pom values may not be accurate, but they are the best
1048 * we have before performing device selection :/
1049 */
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001050 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Sebastian Ott13952ec2008-12-25 13:39:13 +01001051 /*
1052 * Use the initial configuration since we can't be shure that the old
1053 * paths are valid.
1054 */
1055 io_subchannel_init_config(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001056 if (cio_commit_config(sch))
1057 return;
Sebastian Ott13952ec2008-12-25 13:39:13 +01001058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 /* We should also udate ssd info, but this has to wait. */
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001060 /* Check if this is another device which appeared on the same sch. */
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001061 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno)
1062 css_schedule_eval(sch->schid);
1063 else
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001064 ccw_device_start_id(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
1067static void
1068ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1069{
1070 struct subchannel *sch;
1071
1072 sch = to_subchannel(cdev->dev.parent);
1073 /*
1074 * An interrupt in state offline means a previous disable was not
Cornelia Hucked04b892009-03-26 15:24:06 +01001075 * successful - should not happen, but we try to disable again.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 */
1077 cio_disable_subchannel(sch);
1078}
1079
1080static void
1081ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1082{
1083 retry_set_schib(cdev);
1084 cdev->private->state = DEV_STATE_ONLINE;
1085 dev_fsm_event(cdev, dev_event);
1086}
1087
Cornelia Huck94bb0632006-06-29 15:08:41 +02001088static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1089 enum dev_event dev_event)
1090{
1091 cmf_retry_copy_block(cdev);
1092 cdev->private->state = DEV_STATE_ONLINE;
1093 dev_fsm_event(cdev, dev_event);
1094}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096static void
1097ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1098{
1099 ccw_device_set_timeout(cdev, 0);
1100 if (dev_event == DEV_EVENT_NOTOPER)
1101 cdev->private->state = DEV_STATE_NOT_OPER;
1102 else
1103 cdev->private->state = DEV_STATE_OFFLINE;
1104 wake_up(&cdev->private->wait_q);
1105}
1106
1107static void
1108ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1109{
1110 int ret;
1111
1112 ret = ccw_device_cancel_halt_clear(cdev);
1113 switch (ret) {
1114 case 0:
1115 cdev->private->state = DEV_STATE_OFFLINE;
1116 wake_up(&cdev->private->wait_q);
1117 break;
1118 case -ENODEV:
1119 cdev->private->state = DEV_STATE_NOT_OPER;
1120 wake_up(&cdev->private->wait_q);
1121 break;
1122 default:
1123 ccw_device_set_timeout(cdev, HZ/10);
1124 }
1125}
1126
1127/*
1128 * No operation action. This is used e.g. to ignore a timeout event in
1129 * state offline.
1130 */
1131static void
1132ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1133{
1134}
1135
1136/*
1137 * Bug operation action.
1138 */
1139static void
1140ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1141{
Michael Ernst139b83d2008-05-07 09:22:54 +02001142 CIO_MSG_EVENT(0, "Internal state [%i][%i] not handled for device "
1143 "0.%x.%04x\n", cdev->private->state, dev_event,
1144 cdev->private->dev_id.ssid,
1145 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 BUG();
1147}
1148
1149/*
1150 * device statemachine
1151 */
1152fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1153 [DEV_STATE_NOT_OPER] = {
1154 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1155 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1156 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1157 [DEV_EVENT_VERIFY] = ccw_device_nop,
1158 },
1159 [DEV_STATE_SENSE_PGID] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001160 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1162 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1163 [DEV_EVENT_VERIFY] = ccw_device_nop,
1164 },
1165 [DEV_STATE_SENSE_ID] = {
1166 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1167 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1168 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1169 [DEV_EVENT_VERIFY] = ccw_device_nop,
1170 },
1171 [DEV_STATE_OFFLINE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001172 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1174 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
Peter Oberparleiter1f1148c2009-09-11 10:28:14 +02001175 [DEV_EVENT_VERIFY] = ccw_device_offline_verify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 },
1177 [DEV_STATE_VERIFY] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001178 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1180 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001181 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 },
1183 [DEV_STATE_ONLINE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001184 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1186 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1187 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1188 },
1189 [DEV_STATE_W4SENSE] = {
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_w4sense,
1192 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1193 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1194 },
1195 [DEV_STATE_DISBAND_PGID] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001196 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1198 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1199 [DEV_EVENT_VERIFY] = ccw_device_nop,
1200 },
1201 [DEV_STATE_BOXED] = {
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_stlck_done,
1204 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1205 [DEV_EVENT_VERIFY] = ccw_device_nop,
1206 },
1207 /* states to wait for i/o completion before doing something */
1208 [DEV_STATE_CLEAR_VERIFY] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001209 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1211 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1212 [DEV_EVENT_VERIFY] = ccw_device_nop,
1213 },
1214 [DEV_STATE_TIMEOUT_KILL] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001215 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1217 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1218 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1219 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 [DEV_STATE_QUIESCE] = {
1221 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1222 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1223 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1224 [DEV_EVENT_VERIFY] = ccw_device_nop,
1225 },
1226 /* special states for devices gone not operational */
1227 [DEV_STATE_DISCONNECTED] = {
1228 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1229 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1230 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001231 [DEV_EVENT_VERIFY] = ccw_device_start_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 },
1233 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1234 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1235 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1236 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1237 [DEV_EVENT_VERIFY] = ccw_device_nop,
1238 },
1239 [DEV_STATE_CMFCHANGE] = {
1240 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1241 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1242 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1243 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1244 },
Cornelia Huck94bb0632006-06-29 15:08:41 +02001245 [DEV_STATE_CMFUPDATE] = {
1246 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1247 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1248 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1249 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1250 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251};
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253EXPORT_SYMBOL_GPL(ccw_device_set_timeout);