blob: 1b853513c891ca2f010f8703b110f49c6f65afb3 [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 }
Peter Oberparleiter376ae472010-10-25 16:10:44 +0200177 /* Function was unsuccessful */
178 CIO_MSG_EVENT(0, "0.%x.%04x: could not stop I/O\n",
179 cdev->private->dev_id.ssid, cdev->private->dev_id.devno);
180 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Sebastian Ott823d4942009-06-16 10:30:20 +0200183void ccw_device_update_sense_data(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Sebastian Ott823d4942009-06-16 10:30:20 +0200185 memset(&cdev->id, 0, sizeof(cdev->id));
186 cdev->id.cu_type = cdev->private->senseid.cu_type;
187 cdev->id.cu_model = cdev->private->senseid.cu_model;
188 cdev->id.dev_type = cdev->private->senseid.dev_type;
189 cdev->id.dev_model = cdev->private->senseid.dev_model;
190}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Sebastian Ott823d4942009-06-16 10:30:20 +0200192int ccw_device_test_sense_data(struct ccw_device *cdev)
193{
194 return cdev->id.cu_type == cdev->private->senseid.cu_type &&
195 cdev->id.cu_model == cdev->private->senseid.cu_model &&
196 cdev->id.dev_type == cdev->private->senseid.dev_type &&
197 cdev->id.dev_model == cdev->private->senseid.dev_model;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200/*
201 * The machine won't give us any notification by machine check if a chpid has
202 * been varied online on the SE so we have to find out by magic (i. e. driving
203 * the channel subsystem to device selection and updating our path masks).
204 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100205static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206__recover_lost_chpids(struct subchannel *sch, int old_lpm)
207{
208 int mask, i;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200209 struct chp_id chpid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200211 chp_id_init(&chpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 for (i = 0; i<8; i++) {
213 mask = 0x80 >> i;
214 if (!(sch->lpm & mask))
215 continue;
216 if (old_lpm & mask)
217 continue;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200218 chpid.id = sch->schib.pmcw.chpid[i];
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200219 if (!chp_is_registered(chpid))
220 css_schedule_eval_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222}
223
224/*
225 * Stop device recognition.
226 */
227static void
228ccw_device_recog_done(struct ccw_device *cdev, int state)
229{
230 struct subchannel *sch;
Sebastian Ott823d4942009-06-16 10:30:20 +0200231 int old_lpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 sch = to_subchannel(cdev->dev.parent);
234
Sebastian Ottec643332009-12-07 12:51:36 +0100235 if (cio_disable_subchannel(sch))
236 state = DEV_STATE_NOT_OPER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 /*
238 * Now that we tried recognition, we have performed device selection
239 * through ssch() and the path information is up to date.
240 */
241 old_lpm = sch->lpm;
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100242
Cornelia Huck4ffa9232005-07-29 14:03:37 -0700243 /* Check since device may again have become not operational. */
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100244 if (cio_update_schib(sch))
Cornelia Huck4ffa9232005-07-29 14:03:37 -0700245 state = DEV_STATE_NOT_OPER;
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100246 else
247 sch->lpm = sch->schib.pmcw.pam & sch->opm;
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
250 /* Force reprobe on all chpids. */
251 old_lpm = 0;
252 if (sch->lpm != old_lpm)
253 __recover_lost_chpids(sch, old_lpm);
Sebastian Ott47593bf2009-03-31 19:16:05 +0200254 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID &&
255 (state == DEV_STATE_NOT_OPER || state == DEV_STATE_BOXED)) {
256 cdev->private->flags.recog_done = 1;
257 cdev->private->state = DEV_STATE_DISCONNECTED;
258 wake_up(&cdev->private->wait_q);
259 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Sebastian Ott823d4942009-06-16 10:30:20 +0200261 if (cdev->private->flags.resuming) {
262 cdev->private->state = state;
263 cdev->private->flags.recog_done = 1;
264 wake_up(&cdev->private->wait_q);
265 return;
266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 switch (state) {
268 case DEV_STATE_NOT_OPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 break;
270 case DEV_STATE_OFFLINE:
Sebastian Ott823d4942009-06-16 10:30:20 +0200271 if (!cdev->online) {
272 ccw_device_update_sense_data(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +0200273 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
Sebastian Ott823d4942009-06-16 10:30:20 +0200275 cdev->private->state = DEV_STATE_OFFLINE;
276 cdev->private->flags.recog_done = 1;
277 if (ccw_device_test_sense_data(cdev)) {
278 cdev->private->flags.donotify = 1;
279 ccw_device_online(cdev);
280 wake_up(&cdev->private->wait_q);
281 } else {
282 ccw_device_update_sense_data(cdev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100283 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
Sebastian Ott823d4942009-06-16 10:30:20 +0200285 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 case DEV_STATE_BOXED:
Sebastian Ott47593bf2009-03-31 19:16:05 +0200287 if (cdev->id.cu_type != 0) { /* device was recognized before */
288 cdev->private->flags.recog_done = 1;
289 cdev->private->state = DEV_STATE_BOXED;
290 wake_up(&cdev->private->wait_q);
291 return;
292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 break;
294 }
295 cdev->private->state = state;
296 io_subchannel_recog_done(cdev);
Sebastian Ott156013f2009-03-31 19:16:03 +0200297 wake_up(&cdev->private->wait_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300/*
301 * Function called from device_id.c after sense id has completed.
302 */
303void
304ccw_device_sense_id_done(struct ccw_device *cdev, int err)
305{
306 switch (err) {
307 case 0:
308 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
309 break;
310 case -ETIME: /* Sense id stopped by timeout. */
311 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
312 break;
313 default:
314 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
315 break;
316 }
317}
318
Sebastian Ott76e6fb42010-02-26 22:37:28 +0100319/**
320 * ccw_device_notify() - inform the device's driver about an event
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300321 * @cdev: device for which an event occurred
Sebastian Ott76e6fb42010-02-26 22:37:28 +0100322 * @event: event that occurred
323 *
324 * Returns:
325 * -%EINVAL if the device is offline or has no driver.
326 * -%EOPNOTSUPP if the device's driver has no notifier registered.
327 * %NOTIFY_OK if the driver wants to keep the device.
328 * %NOTIFY_BAD if the driver doesn't want to keep the device.
329 */
Cornelia Huckc820de32008-07-14 09:58:45 +0200330int ccw_device_notify(struct ccw_device *cdev, int event)
331{
Sebastian Ott76e6fb42010-02-26 22:37:28 +0100332 int ret = -EINVAL;
333
Cornelia Huckc820de32008-07-14 09:58:45 +0200334 if (!cdev->drv)
Sebastian Ott76e6fb42010-02-26 22:37:28 +0100335 goto out;
Cornelia Huckc820de32008-07-14 09:58:45 +0200336 if (!cdev->online)
Sebastian Ott76e6fb42010-02-26 22:37:28 +0100337 goto out;
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);
Sebastian Ott76e6fb42010-02-26 22:37:28 +0100341 if (!cdev->drv->notify) {
342 ret = -EOPNOTSUPP;
343 goto out;
344 }
345 if (cdev->drv->notify(cdev, event))
346 ret = NOTIFY_OK;
347 else
348 ret = NOTIFY_BAD;
349out:
350 return ret;
Cornelia Huckc820de32008-07-14 09:58:45 +0200351}
352
Peter Oberparleiter91c36912008-08-21 19:46:39 +0200353static void ccw_device_oper_notify(struct ccw_device *cdev)
354{
Sebastian Ott585b9542010-10-25 16:10:34 +0200355 struct subchannel *sch = to_subchannel(cdev->dev.parent);
356
Sebastian Ott76e6fb42010-02-26 22:37:28 +0100357 if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_OK) {
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100358 /* Reenable channel measurements, if needed. */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100359 ccw_device_sched_todo(cdev, CDEV_TODO_ENABLE_CMF);
Sebastian Ott585b9542010-10-25 16:10:34 +0200360 /* Save indication for new paths. */
361 cdev->private->path_new_mask = sch->vpm;
Peter Oberparleiter91c36912008-08-21 19:46:39 +0200362 return;
363 }
364 /* Driver doesn't want device back. */
365 ccw_device_set_notoper(cdev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100366 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
369/*
370 * Finished with online/offline processing.
371 */
372static void
373ccw_device_done(struct ccw_device *cdev, int state)
374{
375 struct subchannel *sch;
376
377 sch = to_subchannel(cdev->dev.parent);
378
Cornelia Huckf1ee3282006-10-04 20:02:02 +0200379 ccw_device_set_timeout(cdev, 0);
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (state != DEV_STATE_ONLINE)
382 cio_disable_subchannel(sch);
383
384 /* Reset device status. */
385 memset(&cdev->private->irb, 0, sizeof(struct irb));
386
387 cdev->private->state = state;
388
Peter Oberparleiter9a332112009-10-06 10:34:01 +0200389 switch (state) {
390 case DEV_STATE_BOXED:
Michael Ernst139b83d2008-05-07 09:22:54 +0200391 CIO_MSG_EVENT(0, "Boxed device %04x on subchannel %04x\n",
392 cdev->private->dev_id.devno, sch->schid.sch_no);
Sebastian Ott76e6fb42010-02-26 22:37:28 +0100393 if (cdev->online &&
394 ccw_device_notify(cdev, CIO_BOXED) != NOTIFY_OK)
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100395 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott47593bf2009-03-31 19:16:05 +0200396 cdev->private->flags.donotify = 0;
Peter Oberparleiter9a332112009-10-06 10:34:01 +0200397 break;
398 case DEV_STATE_NOT_OPER:
Sebastian Ott626e4762009-09-11 10:28:17 +0200399 CIO_MSG_EVENT(0, "Device %04x gone on subchannel %04x\n",
400 cdev->private->dev_id.devno, sch->schid.sch_no);
Sebastian Ott76e6fb42010-02-26 22:37:28 +0100401 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100402 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Peter Oberparleiter6afcc772009-10-06 10:34:02 +0200403 else
404 ccw_device_set_disconnected(cdev);
Sebastian Ott626e4762009-09-11 10:28:17 +0200405 cdev->private->flags.donotify = 0;
Peter Oberparleiter9a332112009-10-06 10:34:01 +0200406 break;
407 case DEV_STATE_DISCONNECTED:
408 CIO_MSG_EVENT(0, "Disconnected device %04x on subchannel "
409 "%04x\n", cdev->private->dev_id.devno,
410 sch->schid.sch_no);
Sebastian Ott9bf05092011-05-23 10:23:32 +0200411 if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK) {
412 cdev->private->state = DEV_STATE_NOT_OPER;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100413 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott9bf05092011-05-23 10:23:32 +0200414 } else
Peter Oberparleiter9a332112009-10-06 10:34:01 +0200415 ccw_device_set_disconnected(cdev);
416 cdev->private->flags.donotify = 0;
417 break;
418 default:
419 break;
Sebastian Ott626e4762009-09-11 10:28:17 +0200420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 if (cdev->private->flags.donotify) {
423 cdev->private->flags.donotify = 0;
Peter Oberparleiter91c36912008-08-21 19:46:39 +0200424 ccw_device_oper_notify(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426 wake_up(&cdev->private->wait_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
429/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 * Start device recognition.
431 */
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100432void ccw_device_recognition(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100434 struct subchannel *sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /*
437 * We used to start here with a sense pgid to find out whether a device
438 * is locked by someone else. Unfortunately, the sense pgid command
439 * code has other meanings on devices predating the path grouping
440 * algorithm, so we start with sense id and box the device after an
441 * timeout (or if sense pgid during path verification detects the device
442 * is locked, as may happen on newer devices).
443 */
444 cdev->private->flags.recog_done = 0;
445 cdev->private->state = DEV_STATE_SENSE_ID;
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100446 if (cio_enable_subchannel(sch, (u32) (addr_t) sch)) {
447 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
448 return;
449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 ccw_device_sense_id_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453/*
Peter Oberparleiter39f53602009-12-07 12:51:26 +0100454 * Handle events for states that use the ccw request infrastructure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 */
Peter Oberparleiter39f53602009-12-07 12:51:26 +0100456static void ccw_device_request_event(struct ccw_device *cdev, enum dev_event e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Peter Oberparleiter39f53602009-12-07 12:51:26 +0100458 switch (e) {
459 case DEV_EVENT_NOTOPER:
460 ccw_request_notoper(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 break;
Peter Oberparleiter39f53602009-12-07 12:51:26 +0100462 case DEV_EVENT_INTERRUPT:
463 ccw_request_handler(cdev);
464 break;
465 case DEV_EVENT_TIMEOUT:
466 ccw_request_timeout(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 break;
468 default:
Peter Oberparleiter39f53602009-12-07 12:51:26 +0100469 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471}
472
Sebastian Ott585b9542010-10-25 16:10:34 +0200473static void ccw_device_report_path_events(struct ccw_device *cdev)
474{
475 struct subchannel *sch = to_subchannel(cdev->dev.parent);
476 int path_event[8];
477 int chp, mask;
478
479 for (chp = 0, mask = 0x80; chp < 8; chp++, mask >>= 1) {
480 path_event[chp] = PE_NONE;
481 if (mask & cdev->private->path_gone_mask & ~(sch->vpm))
482 path_event[chp] |= PE_PATH_GONE;
483 if (mask & cdev->private->path_new_mask & sch->vpm)
484 path_event[chp] |= PE_PATH_AVAILABLE;
485 if (mask & cdev->private->pgid_reset_mask & sch->vpm)
486 path_event[chp] |= PE_PATHGROUP_ESTABLISHED;
487 }
488 if (cdev->online && cdev->drv->path_event)
489 cdev->drv->path_event(cdev, path_event);
490}
491
492static void ccw_device_reset_path_events(struct ccw_device *cdev)
493{
494 cdev->private->path_gone_mask = 0;
495 cdev->private->path_new_mask = 0;
496 cdev->private->pgid_reset_mask = 0;
497}
498
Sebastian Ott50c8e312011-12-01 13:32:21 +0100499static void create_fake_irb(struct irb *irb, int type)
500{
501 memset(irb, 0, sizeof(*irb));
502 if (type == FAKE_CMD_IRB) {
503 struct cmd_scsw *scsw = &irb->scsw.cmd;
504 scsw->cc = 1;
505 scsw->fctl = SCSW_FCTL_START_FUNC;
506 scsw->actl = SCSW_ACTL_START_PEND;
507 scsw->stctl = SCSW_STCTL_STATUS_PEND;
508 } else if (type == FAKE_TM_IRB) {
509 struct tm_scsw *scsw = &irb->scsw.tm;
510 scsw->x = 1;
511 scsw->cc = 1;
512 scsw->fctl = SCSW_FCTL_START_FUNC;
513 scsw->actl = SCSW_ACTL_START_PEND;
514 scsw->stctl = SCSW_STCTL_STATUS_PEND;
515 }
516}
517
518void ccw_device_verify_done(struct ccw_device *cdev, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200520 struct subchannel *sch;
521
522 sch = to_subchannel(cdev->dev.parent);
523 /* Update schib - pom may have changed. */
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100524 if (cio_update_schib(sch)) {
Peter Oberparleiter7c4d9642009-12-07 12:51:22 +0100525 err = -ENODEV;
526 goto callback;
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100527 }
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200528 /* Update lpm with verified path mask. */
529 sch->lpm = sch->vpm;
530 /* Repeat path verification? */
531 if (cdev->private->flags.doverify) {
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200532 ccw_device_verify_start(cdev);
533 return;
534 }
Peter Oberparleiter7c4d9642009-12-07 12:51:22 +0100535callback:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 case 0:
538 ccw_device_done(cdev, DEV_STATE_ONLINE);
539 /* Deliver fake irb to device driver, if needed. */
540 if (cdev->private->flags.fake_irb) {
Sebastian Ott50c8e312011-12-01 13:32:21 +0100541 create_fake_irb(&cdev->private->irb,
542 cdev->private->flags.fake_irb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 cdev->private->flags.fake_irb = 0;
544 if (cdev->handler)
545 cdev->handler(cdev, cdev->private->intparm,
546 &cdev->private->irb);
547 memset(&cdev->private->irb, 0, sizeof(struct irb));
548 }
Sebastian Ott585b9542010-10-25 16:10:34 +0200549 ccw_device_report_path_events(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 break;
551 case -ETIME:
Peter Oberparleiter52ef0602009-12-07 12:51:31 +0100552 case -EUSERS:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200553 /* Reset oper notify indication after verify error. */
554 cdev->private->flags.donotify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 ccw_device_done(cdev, DEV_STATE_BOXED);
556 break;
Peter Oberparleiter7c4d9642009-12-07 12:51:22 +0100557 case -EACCES:
558 /* Reset oper notify indication after verify error. */
559 cdev->private->flags.donotify = 0;
560 ccw_device_done(cdev, DEV_STATE_DISCONNECTED);
561 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 default:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200563 /* Reset oper notify indication after verify error. */
564 cdev->private->flags.donotify = 0;
Peter Oberparleiter7c4d9642009-12-07 12:51:22 +0100565 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 break;
567 }
Sebastian Ott585b9542010-10-25 16:10:34 +0200568 ccw_device_reset_path_events(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
571/*
572 * Get device online.
573 */
574int
575ccw_device_online(struct ccw_device *cdev)
576{
577 struct subchannel *sch;
578 int ret;
579
580 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
581 (cdev->private->state != DEV_STATE_BOXED))
582 return -EINVAL;
583 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckedf22092008-04-30 13:38:39 +0200584 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (ret != 0) {
586 /* Couldn't enable the subchannel for i/o. Sick device. */
587 if (ret == -ENODEV)
588 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
589 return ret;
590 }
Peter Oberparleiter52ef0602009-12-07 12:51:31 +0100591 /* Start initial path verification. */
592 cdev->private->state = DEV_STATE_VERIFY;
593 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return 0;
595}
596
597void
598ccw_device_disband_done(struct ccw_device *cdev, int err)
599{
600 switch (err) {
601 case 0:
602 ccw_device_done(cdev, DEV_STATE_OFFLINE);
603 break;
604 case -ETIME:
605 ccw_device_done(cdev, DEV_STATE_BOXED);
606 break;
607 default:
Peter Oberparleiter3ecb0a52007-05-31 17:38:07 +0200608 cdev->private->flags.donotify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
610 break;
611 }
612}
613
614/*
615 * Shutdown device.
616 */
617int
618ccw_device_offline(struct ccw_device *cdev)
619{
620 struct subchannel *sch;
621
Peter Oberparleiterb301ea82008-09-09 12:38:59 +0200622 /* Allow ccw_device_offline while disconnected. */
623 if (cdev->private->state == DEV_STATE_DISCONNECTED ||
624 cdev->private->state == DEV_STATE_NOT_OPER) {
625 cdev->private->flags.donotify = 0;
626 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
627 return 0;
628 }
Peter Oberparleiter102e8352009-10-06 10:34:03 +0200629 if (cdev->private->state == DEV_STATE_BOXED) {
630 ccw_device_done(cdev, DEV_STATE_BOXED);
631 return 0;
632 }
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100633 if (ccw_device_is_orphan(cdev)) {
634 ccw_device_done(cdev, DEV_STATE_OFFLINE);
635 return 0;
636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 sch = to_subchannel(cdev->dev.parent);
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100638 if (cio_update_schib(sch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return -ENODEV;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200640 if (scsw_actl(&sch->schib.scsw) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return -EBUSY;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200642 if (cdev->private->state != DEV_STATE_ONLINE)
643 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /* Are we doing path grouping? */
Peter Oberparleiter454e1fa2009-12-07 12:51:30 +0100645 if (!cdev->private->flags.pgroup) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 /* No, set state offline immediately. */
647 ccw_device_done(cdev, DEV_STATE_OFFLINE);
648 return 0;
649 }
650 /* Start Set Path Group commands. */
651 cdev->private->state = DEV_STATE_DISBAND_PGID;
652 ccw_device_disband_start(cdev);
653 return 0;
654}
655
656/*
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200657 * Handle not operational event in non-special state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 */
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200659static void ccw_device_generic_notoper(struct ccw_device *cdev,
660 enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Sebastian Ott76e6fb42010-02-26 22:37:28 +0100662 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100663 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Peter Oberparleiter6afcc772009-10-06 10:34:02 +0200664 else
665 ccw_device_set_disconnected(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
668/*
Peter Oberparleiter1f1148c2009-09-11 10:28:14 +0200669 * Handle path verification event in offline state.
670 */
671static void ccw_device_offline_verify(struct ccw_device *cdev,
672 enum dev_event dev_event)
673{
674 struct subchannel *sch = to_subchannel(cdev->dev.parent);
675
676 css_schedule_eval(sch->schid);
677}
678
679/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 * Handle path verification event.
681 */
682static void
683ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
684{
685 struct subchannel *sch;
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (cdev->private->state == DEV_STATE_W4SENSE) {
688 cdev->private->flags.doverify = 1;
689 return;
690 }
691 sch = to_subchannel(cdev->dev.parent);
692 /*
693 * Since we might not just be coming from an interrupt from the
694 * subchannel we have to update the schib.
695 */
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100696 if (cio_update_schib(sch)) {
697 ccw_device_verify_done(cdev, -ENODEV);
698 return;
699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200701 if (scsw_actl(&sch->schib.scsw) != 0 ||
702 (scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_STATUS_PEND) ||
703 (scsw_stctl(&cdev->private->irb.scsw) & SCSW_STCTL_STATUS_PEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 /*
705 * No final status yet or final status not yet delivered
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300706 * to the device driver. Can't do path verification now,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 * delay until final status was delivered.
708 */
709 cdev->private->flags.doverify = 1;
710 return;
711 }
712 /* Device is idle, we can do the path verification. */
713 cdev->private->state = DEV_STATE_VERIFY;
714 ccw_device_verify_start(cdev);
715}
716
717/*
Peter Oberparleiterd7d12ef2009-12-07 12:51:32 +0100718 * Handle path verification event in boxed state.
719 */
720static void ccw_device_boxed_verify(struct ccw_device *cdev,
721 enum dev_event dev_event)
722{
723 struct subchannel *sch = to_subchannel(cdev->dev.parent);
724
725 if (cdev->online) {
726 if (cio_enable_subchannel(sch, (u32) (addr_t) sch))
727 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
728 else
729 ccw_device_online_verify(cdev, dev_event);
730 } else
731 css_schedule_eval(sch->schid);
732}
733
734/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 * Got an interrupt for a normal io (state online).
736 */
737static void
738ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
739{
740 struct irb *irb;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200741 int is_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Heiko Carstenscbb870c2010-02-26 22:37:43 +0100743 irb = (struct irb *)&S390_lowcore.irb;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200744 is_cmd = !scsw_is_tm(&irb->scsw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 /* Check for unsolicited interrupt. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200746 if (!scsw_is_solicited(&irb->scsw)) {
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200747 if (is_cmd && (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 !irb->esw.esw0.erw.cons) {
749 /* Unit check but no sense data. Need basic sense. */
750 if (ccw_device_do_sense(cdev, irb) != 0)
751 goto call_handler_unsol;
Cornelia Hucke0ec5742006-06-04 02:51:27 -0700752 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 cdev->private->state = DEV_STATE_W4SENSE;
754 cdev->private->intparm = 0;
755 return;
756 }
757call_handler_unsol:
758 if (cdev->handler)
759 cdev->handler (cdev, 0, irb);
Cornelia Huck18374d32007-02-05 21:17:09 +0100760 if (cdev->private->flags.doverify)
761 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return;
763 }
764 /* Accumulate status and find out if a basic sense is needed. */
765 ccw_device_accumulate_irb(cdev, irb);
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200766 if (is_cmd && cdev->private->flags.dosense) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (ccw_device_do_sense(cdev, irb) == 0) {
768 cdev->private->state = DEV_STATE_W4SENSE;
769 }
770 return;
771 }
772 /* Call the handler. */
773 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
774 /* Start delayed path verification. */
775 ccw_device_online_verify(cdev, 0);
776}
777
778/*
779 * Got an timeout in online state.
780 */
781static void
782ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
783{
784 int ret;
785
786 ccw_device_set_timeout(cdev, 0);
Peter Oberparleiter376ae472010-10-25 16:10:44 +0200787 cdev->private->iretry = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 ret = ccw_device_cancel_halt_clear(cdev);
789 if (ret == -EBUSY) {
790 ccw_device_set_timeout(cdev, 3*HZ);
791 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
792 return;
793 }
Peter Oberparleiter376ae472010-10-25 16:10:44 +0200794 if (ret)
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200795 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
796 else if (cdev->handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 cdev->handler(cdev, cdev->private->intparm,
798 ERR_PTR(-ETIMEDOUT));
799}
800
801/*
802 * Got an interrupt for a basic sense.
803 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100804static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
806{
807 struct irb *irb;
808
Heiko Carstenscbb870c2010-02-26 22:37:43 +0100809 irb = (struct irb *)&S390_lowcore.irb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 /* Check for unsolicited interrupt. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200811 if (scsw_stctl(&irb->scsw) ==
812 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
813 if (scsw_cc(&irb->scsw) == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /* Basic sense hasn't started. Try again. */
815 ccw_device_do_sense(cdev, irb);
816 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200817 CIO_MSG_EVENT(0, "0.%x.%04x: unsolicited "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200818 "interrupt during w4sense...\n",
819 cdev->private->dev_id.ssid,
820 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (cdev->handler)
822 cdev->handler (cdev, 0, irb);
823 }
824 return;
825 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800826 /*
827 * Check if a halt or clear has been issued in the meanwhile. If yes,
828 * only deliver the halt/clear interrupt to the device driver as if it
829 * had killed the original request.
830 */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200831 if (scsw_fctl(&irb->scsw) &
832 (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
Cornelia Huck3ba19982006-03-24 03:15:12 -0800833 cdev->private->flags.dosense = 0;
834 memset(&cdev->private->irb, 0, sizeof(struct irb));
835 ccw_device_accumulate_irb(cdev, irb);
836 goto call_handler;
837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /* Add basic sense info to irb. */
839 ccw_device_accumulate_basic_sense(cdev, irb);
840 if (cdev->private->flags.dosense) {
841 /* Another basic sense is needed. */
842 ccw_device_do_sense(cdev, irb);
843 return;
844 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800845call_handler:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 cdev->private->state = DEV_STATE_ONLINE;
Michael Ernst217ee6c2009-09-11 10:28:21 +0200847 /* In case sensing interfered with setting the device online */
848 wake_up(&cdev->private->wait_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /* Call the handler. */
850 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
851 /* Start delayed path verification. */
852 ccw_device_online_verify(cdev, 0);
853}
854
855static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
857{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 ccw_device_set_timeout(cdev, 0);
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100859 /* Start delayed path verification. */
860 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /* OK, i/o is dead now. Call interrupt handler. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (cdev->handler)
863 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200864 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
867static void
868ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
869{
870 int ret;
871
872 ret = ccw_device_cancel_halt_clear(cdev);
873 if (ret == -EBUSY) {
874 ccw_device_set_timeout(cdev, 3*HZ);
875 return;
876 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100877 /* Start delayed path verification. */
878 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (cdev->handler)
880 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200881 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
Cornelia Huckc820de32008-07-14 09:58:45 +0200884void ccw_device_kill_io(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Peter Oberparleiter376ae472010-10-25 16:10:44 +0200888 cdev->private->iretry = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 ret = ccw_device_cancel_halt_clear(cdev);
890 if (ret == -EBUSY) {
891 ccw_device_set_timeout(cdev, 3*HZ);
892 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
893 return;
894 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100895 /* Start delayed path verification. */
896 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (cdev->handler)
898 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200899 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
902static void
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200903ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200905 /* Start verification after current task finished. */
Cornelia Huck7e560812006-07-12 16:40:19 +0200906 cdev->private->flags.doverify = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
909static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
911{
912 struct subchannel *sch;
913
914 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckedf22092008-04-30 13:38:39 +0200915 if (cio_enable_subchannel(sch, (u32)(addr_t)sch) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 /* Couldn't enable the subchannel for i/o. Sick device. */
917 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
919 ccw_device_sense_id_start(cdev);
920}
921
Cornelia Huckc820de32008-07-14 09:58:45 +0200922void ccw_device_trigger_reprobe(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Cornelia Huckc820de32008-07-14 09:58:45 +0200924 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (cdev->private->state != DEV_STATE_DISCONNECTED)
927 return;
928
Cornelia Huckc820de32008-07-14 09:58:45 +0200929 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 /* Update some values. */
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100931 if (cio_update_schib(sch))
Cornelia Huck7674da72006-12-08 15:54:21 +0100932 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /*
934 * The pim, pam, pom values may not be accurate, but they are the best
935 * we have before performing device selection :/
936 */
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200937 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Sebastian Ott13952ec2008-12-25 13:39:13 +0100938 /*
939 * Use the initial configuration since we can't be shure that the old
940 * paths are valid.
941 */
942 io_subchannel_init_config(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +0100943 if (cio_commit_config(sch))
944 return;
Sebastian Ott13952ec2008-12-25 13:39:13 +0100945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 /* We should also udate ssd info, but this has to wait. */
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100947 /* Check if this is another device which appeared on the same sch. */
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100948 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno)
949 css_schedule_eval(sch->schid);
950 else
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100951 ccw_device_start_id(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Peter Oberparleiter16b9a052009-12-07 12:51:24 +0100954static void ccw_device_disabled_irq(struct ccw_device *cdev,
955 enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 struct subchannel *sch;
958
959 sch = to_subchannel(cdev->dev.parent);
960 /*
Peter Oberparleiter16b9a052009-12-07 12:51:24 +0100961 * An interrupt in a disabled state means a previous disable was not
Cornelia Hucked04b892009-03-26 15:24:06 +0100962 * successful - should not happen, but we try to disable again.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 */
964 cio_disable_subchannel(sch);
965}
966
967static void
968ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
969{
970 retry_set_schib(cdev);
971 cdev->private->state = DEV_STATE_ONLINE;
972 dev_fsm_event(cdev, dev_event);
973}
974
Cornelia Huck94bb0632006-06-29 15:08:41 +0200975static void ccw_device_update_cmfblock(struct ccw_device *cdev,
976 enum dev_event dev_event)
977{
978 cmf_retry_copy_block(cdev);
979 cdev->private->state = DEV_STATE_ONLINE;
980 dev_fsm_event(cdev, dev_event);
981}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983static void
984ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
985{
986 ccw_device_set_timeout(cdev, 0);
Sebastian Ott56e6b792009-12-07 12:51:35 +0100987 cdev->private->state = DEV_STATE_NOT_OPER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 wake_up(&cdev->private->wait_q);
989}
990
991static void
992ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
993{
994 int ret;
995
996 ret = ccw_device_cancel_halt_clear(cdev);
Sebastian Ott56e6b792009-12-07 12:51:35 +0100997 if (ret == -EBUSY) {
998 ccw_device_set_timeout(cdev, HZ/10);
999 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 cdev->private->state = DEV_STATE_NOT_OPER;
1001 wake_up(&cdev->private->wait_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003}
1004
1005/*
1006 * No operation action. This is used e.g. to ignore a timeout event in
1007 * state offline.
1008 */
1009static void
1010ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1011{
1012}
1013
1014/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 * device statemachine
1016 */
1017fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1018 [DEV_STATE_NOT_OPER] = {
1019 [DEV_EVENT_NOTOPER] = ccw_device_nop,
Peter Oberparleiter16b9a052009-12-07 12:51:24 +01001020 [DEV_EVENT_INTERRUPT] = ccw_device_disabled_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1022 [DEV_EVENT_VERIFY] = ccw_device_nop,
1023 },
1024 [DEV_STATE_SENSE_PGID] = {
Peter Oberparleiter9679baa2009-12-07 12:51:27 +01001025 [DEV_EVENT_NOTOPER] = ccw_device_request_event,
1026 [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
1027 [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 [DEV_EVENT_VERIFY] = ccw_device_nop,
1029 },
1030 [DEV_STATE_SENSE_ID] = {
Peter Oberparleiter39f53602009-12-07 12:51:26 +01001031 [DEV_EVENT_NOTOPER] = ccw_device_request_event,
1032 [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
1033 [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 [DEV_EVENT_VERIFY] = ccw_device_nop,
1035 },
1036 [DEV_STATE_OFFLINE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001037 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Peter Oberparleiter16b9a052009-12-07 12:51:24 +01001038 [DEV_EVENT_INTERRUPT] = ccw_device_disabled_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
Peter Oberparleiter1f1148c2009-09-11 10:28:14 +02001040 [DEV_EVENT_VERIFY] = ccw_device_offline_verify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 },
1042 [DEV_STATE_VERIFY] = {
Peter Oberparleiter9679baa2009-12-07 12:51:27 +01001043 [DEV_EVENT_NOTOPER] = ccw_device_request_event,
1044 [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
1045 [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001046 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 },
1048 [DEV_STATE_ONLINE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001049 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1051 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1052 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1053 },
1054 [DEV_STATE_W4SENSE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001055 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1057 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1058 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1059 },
1060 [DEV_STATE_DISBAND_PGID] = {
Peter Oberparleiter9679baa2009-12-07 12:51:27 +01001061 [DEV_EVENT_NOTOPER] = ccw_device_request_event,
1062 [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
1063 [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 [DEV_EVENT_VERIFY] = ccw_device_nop,
1065 },
1066 [DEV_STATE_BOXED] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001067 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Peter Oberparleiterd7d12ef2009-12-07 12:51:32 +01001068 [DEV_EVENT_INTERRUPT] = ccw_device_nop,
1069 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1070 [DEV_EVENT_VERIFY] = ccw_device_boxed_verify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 },
1072 /* states to wait for i/o completion before doing something */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 [DEV_STATE_TIMEOUT_KILL] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001074 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1076 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1077 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1078 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 [DEV_STATE_QUIESCE] = {
1080 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1081 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1082 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1083 [DEV_EVENT_VERIFY] = ccw_device_nop,
1084 },
1085 /* special states for devices gone not operational */
1086 [DEV_STATE_DISCONNECTED] = {
1087 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1088 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
Peter Oberparleiter16b9a052009-12-07 12:51:24 +01001089 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001090 [DEV_EVENT_VERIFY] = ccw_device_start_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 },
1092 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
Peter Oberparleiter39f53602009-12-07 12:51:26 +01001093 [DEV_EVENT_NOTOPER] = ccw_device_request_event,
1094 [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
1095 [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 [DEV_EVENT_VERIFY] = ccw_device_nop,
1097 },
1098 [DEV_STATE_CMFCHANGE] = {
1099 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1100 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1101 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1102 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1103 },
Cornelia Huck94bb0632006-06-29 15:08:41 +02001104 [DEV_STATE_CMFUPDATE] = {
1105 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1106 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1107 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1108 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1109 },
Peter Oberparleiterd7d12ef2009-12-07 12:51:32 +01001110 [DEV_STATE_STEAL_LOCK] = {
1111 [DEV_EVENT_NOTOPER] = ccw_device_request_event,
1112 [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
1113 [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
1114 [DEV_EVENT_VERIFY] = ccw_device_nop,
1115 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116};
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118EXPORT_SYMBOL_GPL(ccw_device_set_timeout);