blob: d1e05f44fb6f2b014f9cf4eef4294acd74a2fe3d [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 Oberparleiter23d805b62008-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 Oberparleiter23d805b62008-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 Ernst139b83dd2008-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);
292 PREPARE_WORK(&cdev->private->kick_work,
293 ccw_device_do_unbind_bind);
294 queue_work(ccw_device_work, &cdev->private->kick_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
Sebastian Ott823d4942009-06-16 10:30:20 +0200296 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 case DEV_STATE_BOXED:
Michael Ernst139b83dd2008-05-07 09:22:54 +0200298 CIO_MSG_EVENT(0, "SenseID : boxed device %04x on "
299 " subchannel 0.%x.%04x\n",
300 cdev->private->dev_id.devno,
301 sch->schid.ssid, sch->schid.sch_no);
Sebastian Ott47593bf2009-03-31 19:16:05 +0200302 if (cdev->id.cu_type != 0) { /* device was recognized before */
303 cdev->private->flags.recog_done = 1;
304 cdev->private->state = DEV_STATE_BOXED;
305 wake_up(&cdev->private->wait_q);
306 return;
307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 break;
309 }
310 cdev->private->state = state;
311 io_subchannel_recog_done(cdev);
Sebastian Ott156013f2009-03-31 19:16:03 +0200312 wake_up(&cdev->private->wait_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315/*
316 * Function called from device_id.c after sense id has completed.
317 */
318void
319ccw_device_sense_id_done(struct ccw_device *cdev, int err)
320{
321 switch (err) {
322 case 0:
323 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
324 break;
325 case -ETIME: /* Sense id stopped by timeout. */
326 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
327 break;
328 default:
329 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
330 break;
331 }
332}
333
Cornelia Huckc820de32008-07-14 09:58:45 +0200334int ccw_device_notify(struct ccw_device *cdev, int event)
335{
336 if (!cdev->drv)
337 return 0;
338 if (!cdev->online)
339 return 0;
Peter Oberparleiter91c36912008-08-21 19:46:39 +0200340 CIO_MSG_EVENT(2, "notify called for 0.%x.%04x, event=%d\n",
341 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
342 event);
Cornelia Huckc820de32008-07-14 09:58:45 +0200343 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
344}
345
Peter Oberparleiter91c36912008-08-21 19:46:39 +0200346static void cmf_reenable_delayed(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100348 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100351 priv = container_of(work, struct ccw_device_private, kick_work);
352 cdev = priv->cdev;
Peter Oberparleiter91c36912008-08-21 19:46:39 +0200353 cmf_reenable(cdev);
354}
355
356static void ccw_device_oper_notify(struct ccw_device *cdev)
357{
358 if (ccw_device_notify(cdev, CIO_OPER)) {
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100359 /* Reenable channel measurements, if needed. */
Peter Oberparleiter91c36912008-08-21 19:46:39 +0200360 PREPARE_WORK(&cdev->private->kick_work, cmf_reenable_delayed);
361 queue_work(ccw_device_work, &cdev->private->kick_work);
362 return;
363 }
364 /* Driver doesn't want device back. */
365 ccw_device_set_notoper(cdev);
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100366 PREPARE_WORK(&cdev->private->kick_work, ccw_device_do_unbind_bind);
Peter Oberparleiter91c36912008-08-21 19:46:39 +0200367 queue_work(ccw_device_work, &cdev->private->kick_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
370/*
371 * Finished with online/offline processing.
372 */
373static void
374ccw_device_done(struct ccw_device *cdev, int state)
375{
376 struct subchannel *sch;
377
378 sch = to_subchannel(cdev->dev.parent);
379
Cornelia Huckf1ee3282006-10-04 20:02:02 +0200380 ccw_device_set_timeout(cdev, 0);
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (state != DEV_STATE_ONLINE)
383 cio_disable_subchannel(sch);
384
385 /* Reset device status. */
386 memset(&cdev->private->irb, 0, sizeof(struct irb));
387
388 cdev->private->state = state;
389
Peter Oberparleiter9a332112009-10-06 10:34:01 +0200390 switch (state) {
391 case DEV_STATE_BOXED:
Michael Ernst139b83dd2008-05-07 09:22:54 +0200392 CIO_MSG_EVENT(0, "Boxed device %04x on subchannel %04x\n",
393 cdev->private->dev_id.devno, sch->schid.sch_no);
Sebastian Ott47593bf2009-03-31 19:16:05 +0200394 if (cdev->online && !ccw_device_notify(cdev, CIO_BOXED))
395 ccw_device_schedule_sch_unregister(cdev);
396 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);
401 if (!ccw_device_notify(cdev, CIO_GONE))
402 ccw_device_schedule_sch_unregister(cdev);
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);
411 if (!ccw_device_notify(cdev, CIO_NO_PATH))
412 ccw_device_schedule_sch_unregister(cdev);
413 else
414 ccw_device_set_disconnected(cdev);
415 cdev->private->flags.donotify = 0;
416 break;
417 default:
418 break;
Sebastian Ott626e4762009-09-11 10:28:17 +0200419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 if (cdev->private->flags.donotify) {
422 cdev->private->flags.donotify = 0;
Peter Oberparleiter91c36912008-08-21 19:46:39 +0200423 ccw_device_oper_notify(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425 wake_up(&cdev->private->wait_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100428static int cmp_pgid(struct pgid *p1, struct pgid *p2)
Cornelia Huck7e560812006-07-12 16:40:19 +0200429{
430 char *c1;
431 char *c2;
432
433 c1 = (char *)p1;
434 c2 = (char *)p2;
435
436 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
437}
438
439static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
440{
441 int i;
442 int last;
443
444 last = 0;
445 for (i = 0; i < 8; i++) {
446 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
447 /* No PGID yet */
448 continue;
449 if (cdev->private->pgid[last].inf.ps.state1 ==
450 SNID_STATE1_RESET) {
451 /* First non-zero PGID */
452 last = i;
453 continue;
454 }
455 if (cmp_pgid(&cdev->private->pgid[i],
456 &cdev->private->pgid[last]) == 0)
457 /* Non-conflicting PGIDs */
458 continue;
459
460 /* PGID mismatch, can't pathgroup. */
461 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
462 "0.%x.%04x, can't pathgroup\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200463 cdev->private->dev_id.ssid,
464 cdev->private->dev_id.devno);
Cornelia Huck7e560812006-07-12 16:40:19 +0200465 cdev->private->options.pgroup = 0;
466 return;
467 }
468 if (cdev->private->pgid[last].inf.ps.state1 ==
469 SNID_STATE1_RESET)
470 /* No previous pgid found */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200471 memcpy(&cdev->private->pgid[0],
472 &channel_subsystems[0]->global_pgid,
Cornelia Huck7e560812006-07-12 16:40:19 +0200473 sizeof(struct pgid));
474 else
475 /* Use existing pgid */
476 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
477 sizeof(struct pgid));
478}
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480/*
481 * Function called from device_pgid.c after sense path ground has completed.
482 */
483void
484ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
485{
486 struct subchannel *sch;
487
488 sch = to_subchannel(cdev->dev.parent);
489 switch (err) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200490 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
491 cdev->private->options.pgroup = 0;
492 break;
493 case 0: /* success */
494 case -EACCES: /* partial success, some paths not operational */
495 /* Check if all pgids are equal or 0. */
496 __ccw_device_get_common_pgid(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 break;
498 case -ETIME: /* Sense path group id stopped by timeout. */
499 case -EUSERS: /* device is reserved for someone else. */
500 ccw_device_done(cdev, DEV_STATE_BOXED);
Cornelia Huck7e560812006-07-12 16:40:19 +0200501 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 default:
503 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Cornelia Huck7e560812006-07-12 16:40:19 +0200504 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
Cornelia Huck7e560812006-07-12 16:40:19 +0200506 /* Start Path Group verification. */
Cornelia Huck7e560812006-07-12 16:40:19 +0200507 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200508 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200509 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
512/*
513 * Start device recognition.
514 */
515int
516ccw_device_recognition(struct ccw_device *cdev)
517{
518 struct subchannel *sch;
519 int ret;
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckedf22092008-04-30 13:38:39 +0200522 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (ret != 0)
524 /* Couldn't enable the subchannel for i/o. Sick device. */
525 return ret;
526
527 /* After 60s the device recognition is considered to have failed. */
528 ccw_device_set_timeout(cdev, 60*HZ);
529
530 /*
531 * We used to start here with a sense pgid to find out whether a device
532 * is locked by someone else. Unfortunately, the sense pgid command
533 * code has other meanings on devices predating the path grouping
534 * algorithm, so we start with sense id and box the device after an
535 * timeout (or if sense pgid during path verification detects the device
536 * is locked, as may happen on newer devices).
537 */
538 cdev->private->flags.recog_done = 0;
539 cdev->private->state = DEV_STATE_SENSE_ID;
540 ccw_device_sense_id_start(cdev);
541 return 0;
542}
543
544/*
545 * Handle timeout in device recognition.
546 */
547static void
548ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
549{
550 int ret;
551
552 ret = ccw_device_cancel_halt_clear(cdev);
553 switch (ret) {
554 case 0:
555 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
556 break;
557 case -ENODEV:
558 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
559 break;
560 default:
561 ccw_device_set_timeout(cdev, 3*HZ);
562 }
563}
564
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566void
567ccw_device_verify_done(struct ccw_device *cdev, int err)
568{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200569 struct subchannel *sch;
570
571 sch = to_subchannel(cdev->dev.parent);
572 /* Update schib - pom may have changed. */
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100573 if (cio_update_schib(sch)) {
574 cdev->private->flags.donotify = 0;
575 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
576 return;
577 }
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200578 /* Update lpm with verified path mask. */
579 sch->lpm = sch->vpm;
580 /* Repeat path verification? */
581 if (cdev->private->flags.doverify) {
582 cdev->private->flags.doverify = 0;
583 ccw_device_verify_start(cdev);
584 return;
585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 switch (err) {
587 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
588 cdev->private->options.pgroup = 0;
589 case 0:
590 ccw_device_done(cdev, DEV_STATE_ONLINE);
591 /* Deliver fake irb to device driver, if needed. */
592 if (cdev->private->flags.fake_irb) {
593 memset(&cdev->private->irb, 0, sizeof(struct irb));
Peter Oberparleiter23d805b62008-07-14 09:58:50 +0200594 cdev->private->irb.scsw.cmd.cc = 1;
595 cdev->private->irb.scsw.cmd.fctl = SCSW_FCTL_START_FUNC;
596 cdev->private->irb.scsw.cmd.actl = SCSW_ACTL_START_PEND;
597 cdev->private->irb.scsw.cmd.stctl =
598 SCSW_STCTL_STATUS_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 cdev->private->flags.fake_irb = 0;
600 if (cdev->handler)
601 cdev->handler(cdev, cdev->private->intparm,
602 &cdev->private->irb);
603 memset(&cdev->private->irb, 0, sizeof(struct irb));
604 }
605 break;
606 case -ETIME:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200607 /* Reset oper notify indication after verify error. */
608 cdev->private->flags.donotify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 ccw_device_done(cdev, DEV_STATE_BOXED);
610 break;
611 default:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200612 /* Reset oper notify indication after verify error. */
613 cdev->private->flags.donotify = 0;
Cornelia Huck46258ab2008-01-26 14:10:49 +0100614 if (cdev->online) {
615 ccw_device_set_timeout(cdev, 0);
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200616 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Cornelia Huck46258ab2008-01-26 14:10:49 +0100617 } else
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100618 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 break;
620 }
621}
622
623/*
624 * Get device online.
625 */
626int
627ccw_device_online(struct ccw_device *cdev)
628{
629 struct subchannel *sch;
630 int ret;
631
632 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
633 (cdev->private->state != DEV_STATE_BOXED))
634 return -EINVAL;
635 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckedf22092008-04-30 13:38:39 +0200636 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (ret != 0) {
638 /* Couldn't enable the subchannel for i/o. Sick device. */
639 if (ret == -ENODEV)
640 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
641 return ret;
642 }
643 /* Do we want to do path grouping? */
644 if (!cdev->private->options.pgroup) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200645 /* Start initial path verification. */
646 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200647 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200648 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return 0;
650 }
651 /* Do a SensePGID first. */
652 cdev->private->state = DEV_STATE_SENSE_PGID;
653 ccw_device_sense_pgid_start(cdev);
654 return 0;
655}
656
657void
658ccw_device_disband_done(struct ccw_device *cdev, int err)
659{
660 switch (err) {
661 case 0:
662 ccw_device_done(cdev, DEV_STATE_OFFLINE);
663 break;
664 case -ETIME:
665 ccw_device_done(cdev, DEV_STATE_BOXED);
666 break;
667 default:
Peter Oberparleiter3ecb0a52007-05-31 17:38:07 +0200668 cdev->private->flags.donotify = 0;
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200669 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
671 break;
672 }
673}
674
675/*
676 * Shutdown device.
677 */
678int
679ccw_device_offline(struct ccw_device *cdev)
680{
681 struct subchannel *sch;
682
Peter Oberparleiterb301ea82008-09-09 12:38:59 +0200683 /* Allow ccw_device_offline while disconnected. */
684 if (cdev->private->state == DEV_STATE_DISCONNECTED ||
685 cdev->private->state == DEV_STATE_NOT_OPER) {
686 cdev->private->flags.donotify = 0;
687 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
688 return 0;
689 }
Peter Oberparleiter102e8352009-10-06 10:34:03 +0200690 if (cdev->private->state == DEV_STATE_BOXED) {
691 ccw_device_done(cdev, DEV_STATE_BOXED);
692 return 0;
693 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100694 if (ccw_device_is_orphan(cdev)) {
695 ccw_device_done(cdev, DEV_STATE_OFFLINE);
696 return 0;
697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 sch = to_subchannel(cdev->dev.parent);
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100699 if (cio_update_schib(sch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return -ENODEV;
Peter Oberparleiter23d805b62008-07-14 09:58:50 +0200701 if (scsw_actl(&sch->schib.scsw) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return -EBUSY;
Peter Oberparleiter23d805b62008-07-14 09:58:50 +0200703 if (cdev->private->state != DEV_STATE_ONLINE)
704 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 /* Are we doing path grouping? */
706 if (!cdev->private->options.pgroup) {
707 /* No, set state offline immediately. */
708 ccw_device_done(cdev, DEV_STATE_OFFLINE);
709 return 0;
710 }
711 /* Start Set Path Group commands. */
712 cdev->private->state = DEV_STATE_DISBAND_PGID;
713 ccw_device_disband_start(cdev);
714 return 0;
715}
716
717/*
718 * Handle timeout in device online/offline process.
719 */
720static void
721ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
722{
723 int ret;
724
725 ret = ccw_device_cancel_halt_clear(cdev);
726 switch (ret) {
727 case 0:
728 ccw_device_done(cdev, DEV_STATE_BOXED);
729 break;
730 case -ENODEV:
731 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
732 break;
733 default:
734 ccw_device_set_timeout(cdev, 3*HZ);
735 }
736}
737
738/*
739 * Handle not oper event in device recognition.
740 */
741static void
742ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
743{
744 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
745}
746
747/*
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200748 * Handle not operational event in non-special state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 */
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200750static void ccw_device_generic_notoper(struct ccw_device *cdev,
751 enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Peter Oberparleiter6afcc772009-10-06 10:34:02 +0200753 if (!ccw_device_notify(cdev, CIO_GONE))
754 ccw_device_schedule_sch_unregister(cdev);
755 else
756 ccw_device_set_disconnected(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
759/*
Peter Oberparleiter1f1148c2009-09-11 10:28:14 +0200760 * Handle path verification event in offline state.
761 */
762static void ccw_device_offline_verify(struct ccw_device *cdev,
763 enum dev_event dev_event)
764{
765 struct subchannel *sch = to_subchannel(cdev->dev.parent);
766
767 css_schedule_eval(sch->schid);
768}
769
770/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 * Handle path verification event.
772 */
773static void
774ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
775{
776 struct subchannel *sch;
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (cdev->private->state == DEV_STATE_W4SENSE) {
779 cdev->private->flags.doverify = 1;
780 return;
781 }
782 sch = to_subchannel(cdev->dev.parent);
783 /*
784 * Since we might not just be coming from an interrupt from the
785 * subchannel we have to update the schib.
786 */
Sebastian Ottcdb912a2008-12-25 13:39:12 +0100787 if (cio_update_schib(sch)) {
788 ccw_device_verify_done(cdev, -ENODEV);
789 return;
790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Peter Oberparleiter23d805b62008-07-14 09:58:50 +0200792 if (scsw_actl(&sch->schib.scsw) != 0 ||
793 (scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_STATUS_PEND) ||
794 (scsw_stctl(&cdev->private->irb.scsw) & SCSW_STCTL_STATUS_PEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /*
796 * No final status yet or final status not yet delivered
797 * to the device driver. Can't do path verfication now,
798 * delay until final status was delivered.
799 */
800 cdev->private->flags.doverify = 1;
801 return;
802 }
803 /* Device is idle, we can do the path verification. */
804 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200805 cdev->private->flags.doverify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 ccw_device_verify_start(cdev);
807}
808
809/*
810 * Got an interrupt for a normal io (state online).
811 */
812static void
813ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
814{
815 struct irb *irb;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200816 int is_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 irb = (struct irb *) __LC_IRB;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200819 is_cmd = !scsw_is_tm(&irb->scsw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 /* Check for unsolicited interrupt. */
Peter Oberparleiter23d805b62008-07-14 09:58:50 +0200821 if (!scsw_is_solicited(&irb->scsw)) {
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200822 if (is_cmd && (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 !irb->esw.esw0.erw.cons) {
824 /* Unit check but no sense data. Need basic sense. */
825 if (ccw_device_do_sense(cdev, irb) != 0)
826 goto call_handler_unsol;
Cornelia Hucke0ec5742006-06-04 02:51:27 -0700827 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 cdev->private->state = DEV_STATE_W4SENSE;
829 cdev->private->intparm = 0;
830 return;
831 }
832call_handler_unsol:
833 if (cdev->handler)
834 cdev->handler (cdev, 0, irb);
Cornelia Huck18374d32007-02-05 21:17:09 +0100835 if (cdev->private->flags.doverify)
836 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return;
838 }
839 /* Accumulate status and find out if a basic sense is needed. */
840 ccw_device_accumulate_irb(cdev, irb);
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200841 if (is_cmd && cdev->private->flags.dosense) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (ccw_device_do_sense(cdev, irb) == 0) {
843 cdev->private->state = DEV_STATE_W4SENSE;
844 }
845 return;
846 }
847 /* Call the handler. */
848 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
849 /* Start delayed path verification. */
850 ccw_device_online_verify(cdev, 0);
851}
852
853/*
854 * Got an timeout in online state.
855 */
856static void
857ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
858{
859 int ret;
860
861 ccw_device_set_timeout(cdev, 0);
862 ret = ccw_device_cancel_halt_clear(cdev);
863 if (ret == -EBUSY) {
864 ccw_device_set_timeout(cdev, 3*HZ);
865 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
866 return;
867 }
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200868 if (ret == -ENODEV)
869 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
870 else if (cdev->handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 cdev->handler(cdev, cdev->private->intparm,
872 ERR_PTR(-ETIMEDOUT));
873}
874
875/*
876 * Got an interrupt for a basic sense.
877 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100878static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
880{
881 struct irb *irb;
882
883 irb = (struct irb *) __LC_IRB;
884 /* Check for unsolicited interrupt. */
Peter Oberparleiter23d805b62008-07-14 09:58:50 +0200885 if (scsw_stctl(&irb->scsw) ==
886 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
887 if (scsw_cc(&irb->scsw) == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 /* Basic sense hasn't started. Try again. */
889 ccw_device_do_sense(cdev, irb);
890 else {
Michael Ernst139b83dd2008-05-07 09:22:54 +0200891 CIO_MSG_EVENT(0, "0.%x.%04x: unsolicited "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200892 "interrupt during w4sense...\n",
893 cdev->private->dev_id.ssid,
894 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (cdev->handler)
896 cdev->handler (cdev, 0, irb);
897 }
898 return;
899 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800900 /*
901 * Check if a halt or clear has been issued in the meanwhile. If yes,
902 * only deliver the halt/clear interrupt to the device driver as if it
903 * had killed the original request.
904 */
Peter Oberparleiter23d805b62008-07-14 09:58:50 +0200905 if (scsw_fctl(&irb->scsw) &
906 (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
Cornelia Huckd23861f2006-12-04 15:41:04 +0100907 /* Retry Basic Sense if requested. */
908 if (cdev->private->flags.intretry) {
909 cdev->private->flags.intretry = 0;
910 ccw_device_do_sense(cdev, irb);
911 return;
912 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800913 cdev->private->flags.dosense = 0;
914 memset(&cdev->private->irb, 0, sizeof(struct irb));
915 ccw_device_accumulate_irb(cdev, irb);
916 goto call_handler;
917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 /* Add basic sense info to irb. */
919 ccw_device_accumulate_basic_sense(cdev, irb);
920 if (cdev->private->flags.dosense) {
921 /* Another basic sense is needed. */
922 ccw_device_do_sense(cdev, irb);
923 return;
924 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800925call_handler:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 cdev->private->state = DEV_STATE_ONLINE;
Michael Ernst217ee6c2009-09-11 10:28:21 +0200927 /* In case sensing interfered with setting the device online */
928 wake_up(&cdev->private->wait_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 /* Call the handler. */
930 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
931 /* Start delayed path verification. */
932 ccw_device_online_verify(cdev, 0);
933}
934
935static void
936ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
937{
938 struct irb *irb;
939
940 irb = (struct irb *) __LC_IRB;
941 /* Accumulate status. We don't do basic sense. */
942 ccw_device_accumulate_irb(cdev, irb);
Cornelia Huckb4f7b1e2006-06-29 15:03:35 +0200943 /* Remember to clear irb to avoid residuals. */
944 memset(&cdev->private->irb, 0, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 /* Try to start delayed device verification. */
946 ccw_device_online_verify(cdev, 0);
947 /* Note: Don't call handler for cio initiated clear! */
948}
949
950static void
951ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
952{
953 struct subchannel *sch;
954
955 sch = to_subchannel(cdev->dev.parent);
956 ccw_device_set_timeout(cdev, 0);
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100957 /* Start delayed path verification. */
958 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 /* OK, i/o is dead now. Call interrupt handler. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (cdev->handler)
961 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200962 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
965static void
966ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
967{
968 int ret;
969
970 ret = ccw_device_cancel_halt_clear(cdev);
971 if (ret == -EBUSY) {
972 ccw_device_set_timeout(cdev, 3*HZ);
973 return;
974 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100975 /* Start delayed path verification. */
976 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (cdev->handler)
978 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200979 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
Cornelia Huckc820de32008-07-14 09:58:45 +0200982void ccw_device_kill_io(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
984 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 ret = ccw_device_cancel_halt_clear(cdev);
987 if (ret == -EBUSY) {
988 ccw_device_set_timeout(cdev, 3*HZ);
989 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
990 return;
991 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100992 /* Start delayed path verification. */
993 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (cdev->handler)
995 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200996 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
999static void
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001000ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001002 /* Start verification after current task finished. */
Cornelia Huck7e560812006-07-12 16:40:19 +02001003 cdev->private->flags.doverify = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
1006static void
1007ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
1008{
1009 struct irb *irb;
1010
1011 switch (dev_event) {
1012 case DEV_EVENT_INTERRUPT:
1013 irb = (struct irb *) __LC_IRB;
1014 /* Check for unsolicited interrupt. */
Peter Oberparleiter23d805b62008-07-14 09:58:50 +02001015 if ((scsw_stctl(&irb->scsw) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
Peter Oberparleiter23d805b62008-07-14 09:58:50 +02001017 (!scsw_cc(&irb->scsw)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 /* FIXME: we should restart stlck here, but this
1019 * is extremely unlikely ... */
1020 goto out_wakeup;
1021
1022 ccw_device_accumulate_irb(cdev, irb);
1023 /* We don't care about basic sense etc. */
1024 break;
1025 default: /* timeout */
1026 break;
1027 }
1028out_wakeup:
1029 wake_up(&cdev->private->wait_q);
1030}
1031
1032static void
1033ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1034{
1035 struct subchannel *sch;
1036
1037 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckedf22092008-04-30 13:38:39 +02001038 if (cio_enable_subchannel(sch, (u32)(addr_t)sch) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 /* Couldn't enable the subchannel for i/o. Sick device. */
1040 return;
1041
1042 /* After 60s the device recognition is considered to have failed. */
1043 ccw_device_set_timeout(cdev, 60*HZ);
1044
1045 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1046 ccw_device_sense_id_start(cdev);
1047}
1048
Cornelia Huckc820de32008-07-14 09:58:45 +02001049void ccw_device_trigger_reprobe(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Cornelia Huckc820de32008-07-14 09:58:45 +02001051 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1054 return;
1055
Cornelia Huckc820de32008-07-14 09:58:45 +02001056 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 /* Update some values. */
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001058 if (cio_update_schib(sch))
Cornelia Huck7674da72006-12-08 15:54:21 +01001059 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 /*
1061 * The pim, pam, pom values may not be accurate, but they are the best
1062 * we have before performing device selection :/
1063 */
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001064 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Sebastian Ott13952ec2008-12-25 13:39:13 +01001065 /*
1066 * Use the initial configuration since we can't be shure that the old
1067 * paths are valid.
1068 */
1069 io_subchannel_init_config(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001070 if (cio_commit_config(sch))
1071 return;
Sebastian Ott13952ec2008-12-25 13:39:13 +01001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /* We should also udate ssd info, but this has to wait. */
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001074 /* Check if this is another device which appeared on the same sch. */
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001075 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno)
1076 css_schedule_eval(sch->schid);
1077 else
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001078 ccw_device_start_id(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
1081static void
1082ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1083{
1084 struct subchannel *sch;
1085
1086 sch = to_subchannel(cdev->dev.parent);
1087 /*
1088 * An interrupt in state offline means a previous disable was not
Cornelia Hucked04b892009-03-26 15:24:06 +01001089 * successful - should not happen, but we try to disable again.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 */
1091 cio_disable_subchannel(sch);
1092}
1093
1094static void
1095ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1096{
1097 retry_set_schib(cdev);
1098 cdev->private->state = DEV_STATE_ONLINE;
1099 dev_fsm_event(cdev, dev_event);
1100}
1101
Cornelia Huck94bb0632006-06-29 15:08:41 +02001102static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1103 enum dev_event dev_event)
1104{
1105 cmf_retry_copy_block(cdev);
1106 cdev->private->state = DEV_STATE_ONLINE;
1107 dev_fsm_event(cdev, dev_event);
1108}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110static void
1111ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1112{
1113 ccw_device_set_timeout(cdev, 0);
1114 if (dev_event == DEV_EVENT_NOTOPER)
1115 cdev->private->state = DEV_STATE_NOT_OPER;
1116 else
1117 cdev->private->state = DEV_STATE_OFFLINE;
1118 wake_up(&cdev->private->wait_q);
1119}
1120
1121static void
1122ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1123{
1124 int ret;
1125
1126 ret = ccw_device_cancel_halt_clear(cdev);
1127 switch (ret) {
1128 case 0:
1129 cdev->private->state = DEV_STATE_OFFLINE;
1130 wake_up(&cdev->private->wait_q);
1131 break;
1132 case -ENODEV:
1133 cdev->private->state = DEV_STATE_NOT_OPER;
1134 wake_up(&cdev->private->wait_q);
1135 break;
1136 default:
1137 ccw_device_set_timeout(cdev, HZ/10);
1138 }
1139}
1140
1141/*
1142 * No operation action. This is used e.g. to ignore a timeout event in
1143 * state offline.
1144 */
1145static void
1146ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1147{
1148}
1149
1150/*
1151 * Bug operation action.
1152 */
1153static void
1154ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1155{
Michael Ernst139b83dd2008-05-07 09:22:54 +02001156 CIO_MSG_EVENT(0, "Internal state [%i][%i] not handled for device "
1157 "0.%x.%04x\n", cdev->private->state, dev_event,
1158 cdev->private->dev_id.ssid,
1159 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 BUG();
1161}
1162
1163/*
1164 * device statemachine
1165 */
1166fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1167 [DEV_STATE_NOT_OPER] = {
1168 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1169 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1170 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1171 [DEV_EVENT_VERIFY] = ccw_device_nop,
1172 },
1173 [DEV_STATE_SENSE_PGID] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001174 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1176 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1177 [DEV_EVENT_VERIFY] = ccw_device_nop,
1178 },
1179 [DEV_STATE_SENSE_ID] = {
1180 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1181 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1182 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1183 [DEV_EVENT_VERIFY] = ccw_device_nop,
1184 },
1185 [DEV_STATE_OFFLINE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001186 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1188 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
Peter Oberparleiter1f1148c2009-09-11 10:28:14 +02001189 [DEV_EVENT_VERIFY] = ccw_device_offline_verify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 },
1191 [DEV_STATE_VERIFY] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001192 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1194 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001195 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 },
1197 [DEV_STATE_ONLINE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001198 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1200 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1201 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1202 },
1203 [DEV_STATE_W4SENSE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001204 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1206 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1207 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1208 },
1209 [DEV_STATE_DISBAND_PGID] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001210 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1212 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1213 [DEV_EVENT_VERIFY] = ccw_device_nop,
1214 },
1215 [DEV_STATE_BOXED] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001216 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1218 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1219 [DEV_EVENT_VERIFY] = ccw_device_nop,
1220 },
1221 /* states to wait for i/o completion before doing something */
1222 [DEV_STATE_CLEAR_VERIFY] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001223 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1225 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1226 [DEV_EVENT_VERIFY] = ccw_device_nop,
1227 },
1228 [DEV_STATE_TIMEOUT_KILL] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001229 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1231 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1232 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1233 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 [DEV_STATE_QUIESCE] = {
1235 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1236 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1237 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1238 [DEV_EVENT_VERIFY] = ccw_device_nop,
1239 },
1240 /* special states for devices gone not operational */
1241 [DEV_STATE_DISCONNECTED] = {
1242 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1243 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1244 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001245 [DEV_EVENT_VERIFY] = ccw_device_start_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 },
1247 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1248 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1249 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1250 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1251 [DEV_EVENT_VERIFY] = ccw_device_nop,
1252 },
1253 [DEV_STATE_CMFCHANGE] = {
1254 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1255 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1256 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1257 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1258 },
Cornelia Huck94bb0632006-06-29 15:08:41 +02001259 [DEV_STATE_CMFUPDATE] = {
1260 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1261 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1262 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1263 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1264 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265};
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267EXPORT_SYMBOL_GPL(ccw_device_set_timeout);