blob: 4b92c84fb438440874eeb9ea4cb42b5f72965739 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/cio/device_fsm.c
3 * finite state machine for device handling
4 *
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6 * IBM Corporation
Cornelia Huck4ce3b302006-01-14 13:21:04 -08007 * Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 */
10
11#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/init.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080013#include <linux/jiffies.h>
14#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include <asm/ccwdev.h>
Cornelia Huck4c24da72005-09-03 15:58:01 -070017#include <asm/cio.h>
Peter Oberparleitere5854a52007-04-27 16:01:31 +020018#include <asm/chpid.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include "cio.h"
21#include "cio_debug.h"
22#include "css.h"
23#include "device.h"
24#include "chsc.h"
25#include "ioasm.h"
Peter Oberparleitere6b6e102007-04-27 16:01:28 +020026#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Sebastian Ott14ff56b2008-01-26 14:10:37 +010028static int timeout_log_enabled;
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030int
31device_is_online(struct subchannel *sch)
32{
33 struct ccw_device *cdev;
34
Cornelia Huckdb6a6422008-01-26 14:10:46 +010035 cdev = sch_get_cdev(sch);
36 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 return (cdev->private->state == DEV_STATE_ONLINE);
39}
40
41int
42device_is_disconnected(struct subchannel *sch)
43{
44 struct ccw_device *cdev;
45
Cornelia Huckdb6a6422008-01-26 14:10:46 +010046 cdev = sch_get_cdev(sch);
47 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
50 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
51}
52
53void
54device_set_disconnected(struct subchannel *sch)
55{
56 struct ccw_device *cdev;
57
Cornelia Huckdb6a6422008-01-26 14:10:46 +010058 cdev = sch_get_cdev(sch);
59 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 ccw_device_set_timeout(cdev, 0);
62 cdev->private->flags.fake_irb = 0;
63 cdev->private->state = DEV_STATE_DISCONNECTED;
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010064 if (cdev->online)
65 ccw_device_schedule_recovery();
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
Cornelia Huckd23861f2006-12-04 15:41:04 +010068void device_set_intretry(struct subchannel *sch)
69{
70 struct ccw_device *cdev;
71
Cornelia Huckdb6a6422008-01-26 14:10:46 +010072 cdev = sch_get_cdev(sch);
Cornelia Huckd23861f2006-12-04 15:41:04 +010073 if (!cdev)
74 return;
75 cdev->private->flags.intretry = 1;
76}
77
Cornelia Huck24cb5b42006-12-04 15:41:01 +010078int device_trigger_verify(struct subchannel *sch)
79{
80 struct ccw_device *cdev;
81
Cornelia Huckdb6a6422008-01-26 14:10:46 +010082 cdev = sch_get_cdev(sch);
Cornelia Huck24cb5b42006-12-04 15:41:01 +010083 if (!cdev || !cdev->online)
84 return -EINVAL;
85 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
86 return 0;
87}
88
Sebastian Ott14ff56b2008-01-26 14:10:37 +010089static int __init ccw_timeout_log_setup(char *unused)
90{
91 timeout_log_enabled = 1;
92 return 1;
93}
94
95__setup("ccw_timeout_log", ccw_timeout_log_setup);
96
97static void ccw_timeout_log(struct ccw_device *cdev)
98{
99 struct schib schib;
100 struct subchannel *sch;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100101 struct io_subchannel_private *private;
Sebastian Ott14ff56b2008-01-26 14:10:37 +0100102 int cc;
103
104 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100105 private = to_io_private(sch);
Sebastian Ott14ff56b2008-01-26 14:10:37 +0100106 cc = stsch(sch->schid, &schib);
107
108 printk(KERN_WARNING "cio: ccw device timeout occurred at %llx, "
109 "device information:\n", get_clock());
110 printk(KERN_WARNING "cio: orb:\n");
111 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100112 &private->orb, sizeof(private->orb), 0);
Sebastian Ott14ff56b2008-01-26 14:10:37 +0100113 printk(KERN_WARNING "cio: ccw device bus id: %s\n", cdev->dev.bus_id);
114 printk(KERN_WARNING "cio: subchannel bus id: %s\n", sch->dev.bus_id);
115 printk(KERN_WARNING "cio: subchannel lpm: %02x, opm: %02x, "
116 "vpm: %02x\n", sch->lpm, sch->opm, sch->vpm);
117
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100118 if ((void *)(addr_t)private->orb.cpa == &private->sense_ccw ||
119 (void *)(addr_t)private->orb.cpa == cdev->private->iccws)
Sebastian Ott14ff56b2008-01-26 14:10:37 +0100120 printk(KERN_WARNING "cio: last channel program (intern):\n");
121 else
122 printk(KERN_WARNING "cio: last channel program:\n");
123
124 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
Cornelia Huckcd6b4f22008-01-26 14:10:43 +0100125 (void *)(addr_t)private->orb.cpa,
126 sizeof(struct ccw1), 0);
Sebastian Ott14ff56b2008-01-26 14:10:37 +0100127 printk(KERN_WARNING "cio: ccw device state: %d\n",
128 cdev->private->state);
129 printk(KERN_WARNING "cio: store subchannel returned: cc=%d\n", cc);
130 printk(KERN_WARNING "cio: schib:\n");
131 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
132 &schib, sizeof(schib), 0);
133 printk(KERN_WARNING "cio: ccw device flags:\n");
134 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
135 &cdev->private->flags, sizeof(cdev->private->flags), 0);
136}
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/*
139 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
140 */
141static void
142ccw_device_timeout(unsigned long data)
143{
144 struct ccw_device *cdev;
145
146 cdev = (struct ccw_device *) data;
147 spin_lock_irq(cdev->ccwlock);
Sebastian Ott14ff56b2008-01-26 14:10:37 +0100148 if (timeout_log_enabled)
149 ccw_timeout_log(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
151 spin_unlock_irq(cdev->ccwlock);
152}
153
154/*
155 * Set timeout
156 */
157void
158ccw_device_set_timeout(struct ccw_device *cdev, int expires)
159{
160 if (expires == 0) {
161 del_timer(&cdev->private->timer);
162 return;
163 }
164 if (timer_pending(&cdev->private->timer)) {
165 if (mod_timer(&cdev->private->timer, jiffies + expires))
166 return;
167 }
168 cdev->private->timer.function = ccw_device_timeout;
169 cdev->private->timer.data = (unsigned long) cdev;
170 cdev->private->timer.expires = jiffies + expires;
171 add_timer(&cdev->private->timer);
172}
173
174/* Kill any pending timers after machine check. */
175void
176device_kill_pending_timer(struct subchannel *sch)
177{
178 struct ccw_device *cdev;
179
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100180 cdev = sch_get_cdev(sch);
181 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 ccw_device_set_timeout(cdev, 0);
184}
185
186/*
187 * Cancel running i/o. This is called repeatedly since halt/clear are
188 * asynchronous operations. We do one try with cio_cancel, two tries
189 * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
190 * Returns 0 if device now idle, -ENODEV for device not operational and
191 * -EBUSY if an interrupt is expected (either from halt/clear or from a
192 * status pending).
193 */
194int
195ccw_device_cancel_halt_clear(struct ccw_device *cdev)
196{
197 struct subchannel *sch;
198 int ret;
199
200 sch = to_subchannel(cdev->dev.parent);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800201 ret = stsch(sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (ret || !sch->schib.pmcw.dnv)
203 return -ENODEV;
Cornelia Huck2470b642007-03-05 23:36:02 +0100204 if (!sch->schib.pmcw.ena)
205 /* Not operational -> done. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return 0;
207 /* Stage 1: cancel io. */
208 if (!(sch->schib.scsw.actl & SCSW_ACTL_HALT_PEND) &&
209 !(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
210 ret = cio_cancel(sch);
211 if (ret != -EINVAL)
212 return ret;
213 /* cancel io unsuccessful. From now on it is asynchronous. */
214 cdev->private->iretry = 3; /* 3 halt retries. */
215 }
216 if (!(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
217 /* Stage 2: halt io. */
218 if (cdev->private->iretry) {
219 cdev->private->iretry--;
220 ret = cio_halt(sch);
Peter Oberparleiterba4ba8a2006-07-27 14:00:23 +0200221 if (ret != -EBUSY)
222 return (ret == 0) ? -EBUSY : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224 /* halt io unsuccessful. */
225 cdev->private->iretry = 255; /* 255 clear retries. */
226 }
227 /* Stage 3: clear io. */
228 if (cdev->private->iretry) {
229 cdev->private->iretry--;
230 ret = cio_clear (sch);
231 return (ret == 0) ? -EBUSY : ret;
232 }
233 panic("Can't stop i/o on subchannel.\n");
234}
235
236static int
237ccw_device_handle_oper(struct ccw_device *cdev)
238{
239 struct subchannel *sch;
240
241 sch = to_subchannel(cdev->dev.parent);
242 cdev->private->flags.recog_done = 1;
243 /*
244 * Check if cu type and device type still match. If
245 * not, it is certainly another device and we have to
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100246 * de- and re-register.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 */
248 if (cdev->id.cu_type != cdev->private->senseid.cu_type ||
249 cdev->id.cu_model != cdev->private->senseid.cu_model ||
250 cdev->id.dev_type != cdev->private->senseid.dev_type ||
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100251 cdev->id.dev_model != cdev->private->senseid.dev_model) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100253 ccw_device_do_unreg_rereg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 queue_work(ccw_device_work, &cdev->private->kick_work);
255 return 0;
256 }
257 cdev->private->flags.donotify = 1;
258 return 1;
259}
260
261/*
262 * The machine won't give us any notification by machine check if a chpid has
263 * been varied online on the SE so we have to find out by magic (i. e. driving
264 * the channel subsystem to device selection and updating our path masks).
265 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100266static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267__recover_lost_chpids(struct subchannel *sch, int old_lpm)
268{
269 int mask, i;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200270 struct chp_id chpid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200272 chp_id_init(&chpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 for (i = 0; i<8; i++) {
274 mask = 0x80 >> i;
275 if (!(sch->lpm & mask))
276 continue;
277 if (old_lpm & mask)
278 continue;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200279 chpid.id = sch->schib.pmcw.chpid[i];
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200280 if (!chp_is_registered(chpid))
281 css_schedule_eval_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283}
284
285/*
286 * Stop device recognition.
287 */
288static void
289ccw_device_recog_done(struct ccw_device *cdev, int state)
290{
291 struct subchannel *sch;
292 int notify, old_lpm, same_dev;
293
294 sch = to_subchannel(cdev->dev.parent);
295
296 ccw_device_set_timeout(cdev, 0);
297 cio_disable_subchannel(sch);
298 /*
299 * Now that we tried recognition, we have performed device selection
300 * through ssch() and the path information is up to date.
301 */
302 old_lpm = sch->lpm;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800303 stsch(sch->schid, &sch->schib);
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200304 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck4ffa9232005-07-29 14:03:37 -0700305 /* Check since device may again have become not operational. */
306 if (!sch->schib.pmcw.dnv)
307 state = DEV_STATE_NOT_OPER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
309 /* Force reprobe on all chpids. */
310 old_lpm = 0;
311 if (sch->lpm != old_lpm)
312 __recover_lost_chpids(sch, old_lpm);
313 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
314 if (state == DEV_STATE_NOT_OPER) {
315 cdev->private->flags.recog_done = 1;
316 cdev->private->state = DEV_STATE_DISCONNECTED;
317 return;
318 }
319 /* Boxed devices don't need extra treatment. */
320 }
321 notify = 0;
322 same_dev = 0; /* Keep the compiler quiet... */
323 switch (state) {
324 case DEV_STATE_NOT_OPER:
325 CIO_DEBUG(KERN_WARNING, 2,
Cornelia Huckbc698bc2008-01-26 14:10:42 +0100326 "SenseID : unknown device %04x on subchannel "
Cornelia Huck78964262006-10-11 15:31:38 +0200327 "0.%x.%04x\n", cdev->private->dev_id.devno,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800328 sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 break;
330 case DEV_STATE_OFFLINE:
331 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
332 same_dev = ccw_device_handle_oper(cdev);
333 notify = 1;
334 }
335 /* fill out sense information */
Heiko Carstens81388d22006-09-20 15:59:17 +0200336 memset(&cdev->id, 0, sizeof(cdev->id));
Heiko Carstens292888c2006-08-30 14:33:35 +0200337 cdev->id.cu_type = cdev->private->senseid.cu_type;
338 cdev->id.cu_model = cdev->private->senseid.cu_model;
339 cdev->id.dev_type = cdev->private->senseid.dev_type;
340 cdev->id.dev_model = cdev->private->senseid.dev_model;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (notify) {
342 cdev->private->state = DEV_STATE_OFFLINE;
343 if (same_dev) {
344 /* Get device online again. */
345 ccw_device_online(cdev);
346 wake_up(&cdev->private->wait_q);
347 }
348 return;
349 }
350 /* Issue device info message. */
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200351 CIO_DEBUG(KERN_INFO, 2,
Cornelia Huckbc698bc2008-01-26 14:10:42 +0100352 "SenseID : device 0.%x.%04x reports: "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800354 "%04X/%02X\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200355 cdev->private->dev_id.ssid,
356 cdev->private->dev_id.devno,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 cdev->id.cu_type, cdev->id.cu_model,
358 cdev->id.dev_type, cdev->id.dev_model);
359 break;
360 case DEV_STATE_BOXED:
361 CIO_DEBUG(KERN_WARNING, 2,
Cornelia Huckbc698bc2008-01-26 14:10:42 +0100362 "SenseID : boxed device %04x on subchannel "
Cornelia Huck78964262006-10-11 15:31:38 +0200363 "0.%x.%04x\n", cdev->private->dev_id.devno,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800364 sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 break;
366 }
367 cdev->private->state = state;
368 io_subchannel_recog_done(cdev);
369 if (state != DEV_STATE_NOT_OPER)
370 wake_up(&cdev->private->wait_q);
371}
372
373/*
374 * Function called from device_id.c after sense id has completed.
375 */
376void
377ccw_device_sense_id_done(struct ccw_device *cdev, int err)
378{
379 switch (err) {
380 case 0:
381 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
382 break;
383 case -ETIME: /* Sense id stopped by timeout. */
384 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
385 break;
386 default:
387 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
388 break;
389 }
390}
391
392static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100393ccw_device_oper_notify(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100395 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 struct ccw_device *cdev;
397 struct subchannel *sch;
398 int ret;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100399 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100401 priv = container_of(work, struct ccw_device_private, kick_work);
402 cdev = priv->cdev;
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100403 spin_lock_irqsave(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100405 if (sch->driver && sch->driver->notify) {
406 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huck602b20f2008-01-26 14:10:39 +0100407 ret = sch->driver->notify(sch, CIO_OPER);
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100408 spin_lock_irqsave(cdev->ccwlock, flags);
409 } else
410 ret = 0;
411 if (ret) {
412 /* Reenable channel measurements, if needed. */
413 spin_unlock_irqrestore(cdev->ccwlock, flags);
414 cmf_reenable(cdev);
415 spin_lock_irqsave(cdev->ccwlock, flags);
416 wake_up(&cdev->private->wait_q);
417 }
418 spin_unlock_irqrestore(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (!ret)
420 /* Driver doesn't want device back. */
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100421 ccw_device_do_unreg_rereg(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
424/*
425 * Finished with online/offline processing.
426 */
427static void
428ccw_device_done(struct ccw_device *cdev, int state)
429{
430 struct subchannel *sch;
431
432 sch = to_subchannel(cdev->dev.parent);
433
Cornelia Huckf1ee3282006-10-04 20:02:02 +0200434 ccw_device_set_timeout(cdev, 0);
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (state != DEV_STATE_ONLINE)
437 cio_disable_subchannel(sch);
438
439 /* Reset device status. */
440 memset(&cdev->private->irb, 0, sizeof(struct irb));
441
442 cdev->private->state = state;
443
444
445 if (state == DEV_STATE_BOXED)
446 CIO_DEBUG(KERN_WARNING, 2,
Cornelia Huckbc698bc2008-01-26 14:10:42 +0100447 "Boxed device %04x on subchannel %04x\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200448 cdev->private->dev_id.devno, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 if (cdev->private->flags.donotify) {
451 cdev->private->flags.donotify = 0;
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100452 PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
454 }
455 wake_up(&cdev->private->wait_q);
456
457 if (css_init_done && state != DEV_STATE_ONLINE)
458 put_device (&cdev->dev);
459}
460
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100461static int cmp_pgid(struct pgid *p1, struct pgid *p2)
Cornelia Huck7e560812006-07-12 16:40:19 +0200462{
463 char *c1;
464 char *c2;
465
466 c1 = (char *)p1;
467 c2 = (char *)p2;
468
469 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
470}
471
472static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
473{
474 int i;
475 int last;
476
477 last = 0;
478 for (i = 0; i < 8; i++) {
479 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
480 /* No PGID yet */
481 continue;
482 if (cdev->private->pgid[last].inf.ps.state1 ==
483 SNID_STATE1_RESET) {
484 /* First non-zero PGID */
485 last = i;
486 continue;
487 }
488 if (cmp_pgid(&cdev->private->pgid[i],
489 &cdev->private->pgid[last]) == 0)
490 /* Non-conflicting PGIDs */
491 continue;
492
493 /* PGID mismatch, can't pathgroup. */
494 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
495 "0.%x.%04x, can't pathgroup\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200496 cdev->private->dev_id.ssid,
497 cdev->private->dev_id.devno);
Cornelia Huck7e560812006-07-12 16:40:19 +0200498 cdev->private->options.pgroup = 0;
499 return;
500 }
501 if (cdev->private->pgid[last].inf.ps.state1 ==
502 SNID_STATE1_RESET)
503 /* No previous pgid found */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200504 memcpy(&cdev->private->pgid[0],
505 &channel_subsystems[0]->global_pgid,
Cornelia Huck7e560812006-07-12 16:40:19 +0200506 sizeof(struct pgid));
507 else
508 /* Use existing pgid */
509 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
510 sizeof(struct pgid));
511}
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513/*
514 * Function called from device_pgid.c after sense path ground has completed.
515 */
516void
517ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
518{
519 struct subchannel *sch;
520
521 sch = to_subchannel(cdev->dev.parent);
522 switch (err) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200523 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
524 cdev->private->options.pgroup = 0;
525 break;
526 case 0: /* success */
527 case -EACCES: /* partial success, some paths not operational */
528 /* Check if all pgids are equal or 0. */
529 __ccw_device_get_common_pgid(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 break;
531 case -ETIME: /* Sense path group id stopped by timeout. */
532 case -EUSERS: /* device is reserved for someone else. */
533 ccw_device_done(cdev, DEV_STATE_BOXED);
Cornelia Huck7e560812006-07-12 16:40:19 +0200534 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 default:
536 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Cornelia Huck7e560812006-07-12 16:40:19 +0200537 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
Cornelia Huck7e560812006-07-12 16:40:19 +0200539 /* Start Path Group verification. */
Cornelia Huck7e560812006-07-12 16:40:19 +0200540 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200541 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200542 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545/*
546 * Start device recognition.
547 */
548int
549ccw_device_recognition(struct ccw_device *cdev)
550{
551 struct subchannel *sch;
552 int ret;
553
554 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
555 (cdev->private->state != DEV_STATE_BOXED))
556 return -EINVAL;
557 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100558 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc,
559 (u32)(addr_t)sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (ret != 0)
561 /* Couldn't enable the subchannel for i/o. Sick device. */
562 return ret;
563
564 /* After 60s the device recognition is considered to have failed. */
565 ccw_device_set_timeout(cdev, 60*HZ);
566
567 /*
568 * We used to start here with a sense pgid to find out whether a device
569 * is locked by someone else. Unfortunately, the sense pgid command
570 * code has other meanings on devices predating the path grouping
571 * algorithm, so we start with sense id and box the device after an
572 * timeout (or if sense pgid during path verification detects the device
573 * is locked, as may happen on newer devices).
574 */
575 cdev->private->flags.recog_done = 0;
576 cdev->private->state = DEV_STATE_SENSE_ID;
577 ccw_device_sense_id_start(cdev);
578 return 0;
579}
580
581/*
582 * Handle timeout in device recognition.
583 */
584static void
585ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
586{
587 int ret;
588
589 ret = ccw_device_cancel_halt_clear(cdev);
590 switch (ret) {
591 case 0:
592 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
593 break;
594 case -ENODEV:
595 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
596 break;
597 default:
598 ccw_device_set_timeout(cdev, 3*HZ);
599 }
600}
601
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603void
604ccw_device_verify_done(struct ccw_device *cdev, int err)
605{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200606 struct subchannel *sch;
607
608 sch = to_subchannel(cdev->dev.parent);
609 /* Update schib - pom may have changed. */
610 stsch(sch->schid, &sch->schib);
611 /* Update lpm with verified path mask. */
612 sch->lpm = sch->vpm;
613 /* Repeat path verification? */
614 if (cdev->private->flags.doverify) {
615 cdev->private->flags.doverify = 0;
616 ccw_device_verify_start(cdev);
617 return;
618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 switch (err) {
620 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
621 cdev->private->options.pgroup = 0;
622 case 0:
623 ccw_device_done(cdev, DEV_STATE_ONLINE);
624 /* Deliver fake irb to device driver, if needed. */
625 if (cdev->private->flags.fake_irb) {
626 memset(&cdev->private->irb, 0, sizeof(struct irb));
Heiko Carstens292888c2006-08-30 14:33:35 +0200627 cdev->private->irb.scsw.cc = 1;
628 cdev->private->irb.scsw.fctl = SCSW_FCTL_START_FUNC;
629 cdev->private->irb.scsw.actl = SCSW_ACTL_START_PEND;
630 cdev->private->irb.scsw.stctl = SCSW_STCTL_STATUS_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 cdev->private->flags.fake_irb = 0;
632 if (cdev->handler)
633 cdev->handler(cdev, cdev->private->intparm,
634 &cdev->private->irb);
635 memset(&cdev->private->irb, 0, sizeof(struct irb));
636 }
637 break;
638 case -ETIME:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200639 /* Reset oper notify indication after verify error. */
640 cdev->private->flags.donotify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 ccw_device_done(cdev, DEV_STATE_BOXED);
642 break;
643 default:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200644 /* Reset oper notify indication after verify error. */
645 cdev->private->flags.donotify = 0;
Cornelia Huck46258ab2008-01-26 14:10:49 +0100646 if (cdev->online) {
647 ccw_device_set_timeout(cdev, 0);
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200648 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Cornelia Huck46258ab2008-01-26 14:10:49 +0100649 } else
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100650 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 break;
652 }
653}
654
655/*
656 * Get device online.
657 */
658int
659ccw_device_online(struct ccw_device *cdev)
660{
661 struct subchannel *sch;
662 int ret;
663
664 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
665 (cdev->private->state != DEV_STATE_BOXED))
666 return -EINVAL;
667 sch = to_subchannel(cdev->dev.parent);
668 if (css_init_done && !get_device(&cdev->dev))
669 return -ENODEV;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100670 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc,
671 (u32)(addr_t)sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (ret != 0) {
673 /* Couldn't enable the subchannel for i/o. Sick device. */
674 if (ret == -ENODEV)
675 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
676 return ret;
677 }
678 /* Do we want to do path grouping? */
679 if (!cdev->private->options.pgroup) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200680 /* Start initial path verification. */
681 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200682 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200683 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return 0;
685 }
686 /* Do a SensePGID first. */
687 cdev->private->state = DEV_STATE_SENSE_PGID;
688 ccw_device_sense_pgid_start(cdev);
689 return 0;
690}
691
692void
693ccw_device_disband_done(struct ccw_device *cdev, int err)
694{
695 switch (err) {
696 case 0:
697 ccw_device_done(cdev, DEV_STATE_OFFLINE);
698 break;
699 case -ETIME:
700 ccw_device_done(cdev, DEV_STATE_BOXED);
701 break;
702 default:
Peter Oberparleiter3ecb0a52007-05-31 17:38:07 +0200703 cdev->private->flags.donotify = 0;
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200704 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
706 break;
707 }
708}
709
710/*
711 * Shutdown device.
712 */
713int
714ccw_device_offline(struct ccw_device *cdev)
715{
716 struct subchannel *sch;
717
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100718 if (ccw_device_is_orphan(cdev)) {
719 ccw_device_done(cdev, DEV_STATE_OFFLINE);
720 return 0;
721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 sch = to_subchannel(cdev->dev.parent);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800723 if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return -ENODEV;
725 if (cdev->private->state != DEV_STATE_ONLINE) {
726 if (sch->schib.scsw.actl != 0)
727 return -EBUSY;
728 return -EINVAL;
729 }
730 if (sch->schib.scsw.actl != 0)
731 return -EBUSY;
732 /* Are we doing path grouping? */
733 if (!cdev->private->options.pgroup) {
734 /* No, set state offline immediately. */
735 ccw_device_done(cdev, DEV_STATE_OFFLINE);
736 return 0;
737 }
738 /* Start Set Path Group commands. */
739 cdev->private->state = DEV_STATE_DISBAND_PGID;
740 ccw_device_disband_start(cdev);
741 return 0;
742}
743
744/*
745 * Handle timeout in device online/offline process.
746 */
747static void
748ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
749{
750 int ret;
751
752 ret = ccw_device_cancel_halt_clear(cdev);
753 switch (ret) {
754 case 0:
755 ccw_device_done(cdev, DEV_STATE_BOXED);
756 break;
757 case -ENODEV:
758 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
759 break;
760 default:
761 ccw_device_set_timeout(cdev, 3*HZ);
762 }
763}
764
765/*
766 * Handle not oper event in device recognition.
767 */
768static void
769ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
770{
771 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
772}
773
774/*
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200775 * Handle not operational event in non-special state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 */
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200777static void ccw_device_generic_notoper(struct ccw_device *cdev,
778 enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 struct subchannel *sch;
781
782 cdev->private->state = DEV_STATE_NOT_OPER;
783 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200784 css_schedule_eval(sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
787/*
788 * Handle path verification event.
789 */
790static void
791ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
792{
793 struct subchannel *sch;
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (cdev->private->state == DEV_STATE_W4SENSE) {
796 cdev->private->flags.doverify = 1;
797 return;
798 }
799 sch = to_subchannel(cdev->dev.parent);
800 /*
801 * Since we might not just be coming from an interrupt from the
802 * subchannel we have to update the schib.
803 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800804 stsch(sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 if (sch->schib.scsw.actl != 0 ||
Peter Oberparleiter65200c22006-08-07 17:00:33 +0200807 (sch->schib.scsw.stctl & SCSW_STCTL_STATUS_PEND) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
809 /*
810 * No final status yet or final status not yet delivered
811 * to the device driver. Can't do path verfication now,
812 * delay until final status was delivered.
813 */
814 cdev->private->flags.doverify = 1;
815 return;
816 }
817 /* Device is idle, we can do the path verification. */
818 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200819 cdev->private->flags.doverify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 ccw_device_verify_start(cdev);
821}
822
823/*
824 * Got an interrupt for a normal io (state online).
825 */
826static void
827ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
828{
829 struct irb *irb;
830
831 irb = (struct irb *) __LC_IRB;
832 /* Check for unsolicited interrupt. */
833 if ((irb->scsw.stctl ==
834 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
835 && (!irb->scsw.cc)) {
836 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
837 !irb->esw.esw0.erw.cons) {
838 /* Unit check but no sense data. Need basic sense. */
839 if (ccw_device_do_sense(cdev, irb) != 0)
840 goto call_handler_unsol;
Cornelia Hucke0ec5742006-06-04 02:51:27 -0700841 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 cdev->private->state = DEV_STATE_W4SENSE;
843 cdev->private->intparm = 0;
844 return;
845 }
846call_handler_unsol:
847 if (cdev->handler)
848 cdev->handler (cdev, 0, irb);
Cornelia Huck18374d32007-02-05 21:17:09 +0100849 if (cdev->private->flags.doverify)
850 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return;
852 }
853 /* Accumulate status and find out if a basic sense is needed. */
854 ccw_device_accumulate_irb(cdev, irb);
855 if (cdev->private->flags.dosense) {
856 if (ccw_device_do_sense(cdev, irb) == 0) {
857 cdev->private->state = DEV_STATE_W4SENSE;
858 }
859 return;
860 }
861 /* Call the handler. */
862 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
863 /* Start delayed path verification. */
864 ccw_device_online_verify(cdev, 0);
865}
866
867/*
868 * Got an timeout in online state.
869 */
870static void
871ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
872{
873 int ret;
874
875 ccw_device_set_timeout(cdev, 0);
876 ret = ccw_device_cancel_halt_clear(cdev);
877 if (ret == -EBUSY) {
878 ccw_device_set_timeout(cdev, 3*HZ);
879 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
880 return;
881 }
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200882 if (ret == -ENODEV)
883 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
884 else if (cdev->handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 cdev->handler(cdev, cdev->private->intparm,
886 ERR_PTR(-ETIMEDOUT));
887}
888
889/*
890 * Got an interrupt for a basic sense.
891 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100892static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
894{
895 struct irb *irb;
896
897 irb = (struct irb *) __LC_IRB;
898 /* Check for unsolicited interrupt. */
899 if (irb->scsw.stctl ==
900 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
901 if (irb->scsw.cc == 1)
902 /* Basic sense hasn't started. Try again. */
903 ccw_device_do_sense(cdev, irb);
904 else {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200905 CIO_MSG_EVENT(2, "Huh? 0.%x.%04x: unsolicited "
906 "interrupt during w4sense...\n",
907 cdev->private->dev_id.ssid,
908 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (cdev->handler)
910 cdev->handler (cdev, 0, irb);
911 }
912 return;
913 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800914 /*
915 * Check if a halt or clear has been issued in the meanwhile. If yes,
916 * only deliver the halt/clear interrupt to the device driver as if it
917 * had killed the original request.
918 */
919 if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
Cornelia Huckd23861f2006-12-04 15:41:04 +0100920 /* Retry Basic Sense if requested. */
921 if (cdev->private->flags.intretry) {
922 cdev->private->flags.intretry = 0;
923 ccw_device_do_sense(cdev, irb);
924 return;
925 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800926 cdev->private->flags.dosense = 0;
927 memset(&cdev->private->irb, 0, sizeof(struct irb));
928 ccw_device_accumulate_irb(cdev, irb);
929 goto call_handler;
930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 /* Add basic sense info to irb. */
932 ccw_device_accumulate_basic_sense(cdev, irb);
933 if (cdev->private->flags.dosense) {
934 /* Another basic sense is needed. */
935 ccw_device_do_sense(cdev, irb);
936 return;
937 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800938call_handler:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 cdev->private->state = DEV_STATE_ONLINE;
940 /* Call the handler. */
941 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
942 /* Start delayed path verification. */
943 ccw_device_online_verify(cdev, 0);
944}
945
946static void
947ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
948{
949 struct irb *irb;
950
951 irb = (struct irb *) __LC_IRB;
952 /* Accumulate status. We don't do basic sense. */
953 ccw_device_accumulate_irb(cdev, irb);
Cornelia Huckb4f7b1e2006-06-29 15:03:35 +0200954 /* Remember to clear irb to avoid residuals. */
955 memset(&cdev->private->irb, 0, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 /* Try to start delayed device verification. */
957 ccw_device_online_verify(cdev, 0);
958 /* Note: Don't call handler for cio initiated clear! */
959}
960
961static void
962ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
963{
964 struct subchannel *sch;
965
966 sch = to_subchannel(cdev->dev.parent);
967 ccw_device_set_timeout(cdev, 0);
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100968 /* Start delayed path verification. */
969 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* OK, i/o is dead now. Call interrupt handler. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (cdev->handler)
972 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200973 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
976static void
977ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
978{
979 int ret;
980
981 ret = ccw_device_cancel_halt_clear(cdev);
982 if (ret == -EBUSY) {
983 ccw_device_set_timeout(cdev, 3*HZ);
984 return;
985 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100986 /* Start delayed path verification. */
987 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (cdev->handler)
989 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200990 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992
Cornelia Hucke7769b42006-10-11 15:31:41 +0200993void device_kill_io(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 int ret;
Cornelia Hucke7769b42006-10-11 15:31:41 +0200996 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100998 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 ret = ccw_device_cancel_halt_clear(cdev);
1000 if (ret == -EBUSY) {
1001 ccw_device_set_timeout(cdev, 3*HZ);
1002 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
1003 return;
1004 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +01001005 /* Start delayed path verification. */
1006 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (cdev->handler)
1008 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +02001009 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
1012static void
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001013ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001015 /* Start verification after current task finished. */
Cornelia Huck7e560812006-07-12 16:40:19 +02001016 cdev->private->flags.doverify = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
1019static void
1020ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
1021{
1022 struct irb *irb;
1023
1024 switch (dev_event) {
1025 case DEV_EVENT_INTERRUPT:
1026 irb = (struct irb *) __LC_IRB;
1027 /* Check for unsolicited interrupt. */
1028 if ((irb->scsw.stctl ==
1029 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
1030 (!irb->scsw.cc))
1031 /* FIXME: we should restart stlck here, but this
1032 * is extremely unlikely ... */
1033 goto out_wakeup;
1034
1035 ccw_device_accumulate_irb(cdev, irb);
1036 /* We don't care about basic sense etc. */
1037 break;
1038 default: /* timeout */
1039 break;
1040 }
1041out_wakeup:
1042 wake_up(&cdev->private->wait_q);
1043}
1044
1045static void
1046ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1047{
1048 struct subchannel *sch;
1049
1050 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckb279a4f2008-01-26 14:10:45 +01001051 if (cio_enable_subchannel(sch, sch->schib.pmcw.isc,
1052 (u32)(addr_t)sch) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /* Couldn't enable the subchannel for i/o. Sick device. */
1054 return;
1055
1056 /* After 60s the device recognition is considered to have failed. */
1057 ccw_device_set_timeout(cdev, 60*HZ);
1058
1059 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1060 ccw_device_sense_id_start(cdev);
1061}
1062
1063void
1064device_trigger_reprobe(struct subchannel *sch)
1065{
1066 struct ccw_device *cdev;
1067
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001068 cdev = sch_get_cdev(sch);
1069 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1072 return;
1073
1074 /* Update some values. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001075 if (stsch(sch->schid, &sch->schib))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return;
Cornelia Huck7674da72006-12-08 15:54:21 +01001077 if (!sch->schib.pmcw.dnv)
1078 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /*
1080 * The pim, pam, pom values may not be accurate, but they are the best
1081 * we have before performing device selection :/
1082 */
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001083 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 /* Re-set some bits in the pmcw that were lost. */
1085 sch->schib.pmcw.isc = 3;
1086 sch->schib.pmcw.csense = 1;
1087 sch->schib.pmcw.ena = 0;
1088 if ((sch->lpm & (sch->lpm - 1)) != 0)
1089 sch->schib.pmcw.mp = 1;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001090 sch->schib.pmcw.intparm = (u32)(addr_t)sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 /* We should also udate ssd info, but this has to wait. */
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001092 /* Check if this is another device which appeared on the same sch. */
1093 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1094 PREPARE_WORK(&cdev->private->kick_work,
1095 ccw_device_move_to_orphanage);
Cornelia Huckc5d4a992007-11-20 11:13:41 +01001096 queue_work(slow_path_wq, &cdev->private->kick_work);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001097 } else
1098 ccw_device_start_id(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
1101static void
1102ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1103{
1104 struct subchannel *sch;
1105
1106 sch = to_subchannel(cdev->dev.parent);
1107 /*
1108 * An interrupt in state offline means a previous disable was not
1109 * successful. Try again.
1110 */
1111 cio_disable_subchannel(sch);
1112}
1113
1114static void
1115ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1116{
1117 retry_set_schib(cdev);
1118 cdev->private->state = DEV_STATE_ONLINE;
1119 dev_fsm_event(cdev, dev_event);
1120}
1121
Cornelia Huck94bb0632006-06-29 15:08:41 +02001122static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1123 enum dev_event dev_event)
1124{
1125 cmf_retry_copy_block(cdev);
1126 cdev->private->state = DEV_STATE_ONLINE;
1127 dev_fsm_event(cdev, dev_event);
1128}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130static void
1131ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1132{
1133 ccw_device_set_timeout(cdev, 0);
1134 if (dev_event == DEV_EVENT_NOTOPER)
1135 cdev->private->state = DEV_STATE_NOT_OPER;
1136 else
1137 cdev->private->state = DEV_STATE_OFFLINE;
1138 wake_up(&cdev->private->wait_q);
1139}
1140
1141static void
1142ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1143{
1144 int ret;
1145
1146 ret = ccw_device_cancel_halt_clear(cdev);
1147 switch (ret) {
1148 case 0:
1149 cdev->private->state = DEV_STATE_OFFLINE;
1150 wake_up(&cdev->private->wait_q);
1151 break;
1152 case -ENODEV:
1153 cdev->private->state = DEV_STATE_NOT_OPER;
1154 wake_up(&cdev->private->wait_q);
1155 break;
1156 default:
1157 ccw_device_set_timeout(cdev, HZ/10);
1158 }
1159}
1160
1161/*
1162 * No operation action. This is used e.g. to ignore a timeout event in
1163 * state offline.
1164 */
1165static void
1166ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1167{
1168}
1169
1170/*
1171 * Bug operation action.
1172 */
1173static void
1174ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1175{
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001176 CIO_MSG_EVENT(0, "dev_jumptable[%i][%i] == NULL\n",
1177 cdev->private->state, dev_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 BUG();
1179}
1180
1181/*
1182 * device statemachine
1183 */
1184fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1185 [DEV_STATE_NOT_OPER] = {
1186 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1187 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1188 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1189 [DEV_EVENT_VERIFY] = ccw_device_nop,
1190 },
1191 [DEV_STATE_SENSE_PGID] = {
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_sense_pgid_irq,
1194 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1195 [DEV_EVENT_VERIFY] = ccw_device_nop,
1196 },
1197 [DEV_STATE_SENSE_ID] = {
1198 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1199 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1200 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1201 [DEV_EVENT_VERIFY] = ccw_device_nop,
1202 },
1203 [DEV_STATE_OFFLINE] = {
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_offline_irq,
1206 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1207 [DEV_EVENT_VERIFY] = ccw_device_nop,
1208 },
1209 [DEV_STATE_VERIFY] = {
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_verify_irq,
1212 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001213 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 },
1215 [DEV_STATE_ONLINE] = {
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_irq,
1218 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1219 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1220 },
1221 [DEV_STATE_W4SENSE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001222 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1224 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1225 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1226 },
1227 [DEV_STATE_DISBAND_PGID] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001228 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1230 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1231 [DEV_EVENT_VERIFY] = ccw_device_nop,
1232 },
1233 [DEV_STATE_BOXED] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001234 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1236 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1237 [DEV_EVENT_VERIFY] = ccw_device_nop,
1238 },
1239 /* states to wait for i/o completion before doing something */
1240 [DEV_STATE_CLEAR_VERIFY] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001241 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1243 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1244 [DEV_EVENT_VERIFY] = ccw_device_nop,
1245 },
1246 [DEV_STATE_TIMEOUT_KILL] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001247 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1249 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1250 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1251 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 [DEV_STATE_QUIESCE] = {
1253 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1254 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1255 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1256 [DEV_EVENT_VERIFY] = ccw_device_nop,
1257 },
1258 /* special states for devices gone not operational */
1259 [DEV_STATE_DISCONNECTED] = {
1260 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1261 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1262 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001263 [DEV_EVENT_VERIFY] = ccw_device_start_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 },
1265 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1266 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1267 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1268 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1269 [DEV_EVENT_VERIFY] = ccw_device_nop,
1270 },
1271 [DEV_STATE_CMFCHANGE] = {
1272 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1273 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1274 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1275 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1276 },
Cornelia Huck94bb0632006-06-29 15:08:41 +02001277 [DEV_STATE_CMFUPDATE] = {
1278 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1279 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1280 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1281 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1282 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283};
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285EXPORT_SYMBOL_GPL(ccw_device_set_timeout);