blob: 84cc9ea346dbc264639d892a19e591ef7535ef8f [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);
Sebastian Ott14ff56b2008-01-26 14:10:37 +010055 printk(KERN_WARNING "cio: ccw device bus id: %s\n", cdev->dev.bus_id);
56 printk(KERN_WARNING "cio: subchannel bus id: %s\n", sch->dev.bus_id);
57 printk(KERN_WARNING "cio: subchannel lpm: %02x, opm: %02x, "
58 "vpm: %02x\n", sch->lpm, sch->opm, sch->vpm);
59
Peter Oberparleiter83262d62008-07-14 09:58:51 +020060 if (orb->tm.b) {
61 printk(KERN_WARNING "cio: orb indicates transport mode\n");
62 printk(KERN_WARNING "cio: last tcw:\n");
63 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
64 (void *)(addr_t)orb->tm.tcw,
65 sizeof(struct tcw), 0);
66 } else {
67 printk(KERN_WARNING "cio: orb indicates command mode\n");
68 if ((void *)(addr_t)orb->cmd.cpa == &private->sense_ccw ||
69 (void *)(addr_t)orb->cmd.cpa == cdev->private->iccws)
70 printk(KERN_WARNING "cio: last channel program "
71 "(intern):\n");
72 else
73 printk(KERN_WARNING "cio: last channel program:\n");
Sebastian Ott14ff56b2008-01-26 14:10:37 +010074
Peter Oberparleiter83262d62008-07-14 09:58:51 +020075 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
76 (void *)(addr_t)orb->cmd.cpa,
77 sizeof(struct ccw1), 0);
78 }
Sebastian Ott14ff56b2008-01-26 14:10:37 +010079 printk(KERN_WARNING "cio: ccw device state: %d\n",
80 cdev->private->state);
81 printk(KERN_WARNING "cio: store subchannel returned: cc=%d\n", cc);
82 printk(KERN_WARNING "cio: schib:\n");
83 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
84 &schib, sizeof(schib), 0);
85 printk(KERN_WARNING "cio: ccw device flags:\n");
86 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
87 &cdev->private->flags, sizeof(cdev->private->flags), 0);
88}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/*
91 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
92 */
93static void
94ccw_device_timeout(unsigned long data)
95{
96 struct ccw_device *cdev;
97
98 cdev = (struct ccw_device *) data;
99 spin_lock_irq(cdev->ccwlock);
Sebastian Ott14ff56b2008-01-26 14:10:37 +0100100 if (timeout_log_enabled)
101 ccw_timeout_log(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
103 spin_unlock_irq(cdev->ccwlock);
104}
105
106/*
107 * Set timeout
108 */
109void
110ccw_device_set_timeout(struct ccw_device *cdev, int expires)
111{
112 if (expires == 0) {
113 del_timer(&cdev->private->timer);
114 return;
115 }
116 if (timer_pending(&cdev->private->timer)) {
117 if (mod_timer(&cdev->private->timer, jiffies + expires))
118 return;
119 }
120 cdev->private->timer.function = ccw_device_timeout;
121 cdev->private->timer.data = (unsigned long) cdev;
122 cdev->private->timer.expires = jiffies + expires;
123 add_timer(&cdev->private->timer);
124}
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126/*
127 * Cancel running i/o. This is called repeatedly since halt/clear are
128 * asynchronous operations. We do one try with cio_cancel, two tries
129 * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
130 * Returns 0 if device now idle, -ENODEV for device not operational and
131 * -EBUSY if an interrupt is expected (either from halt/clear or from a
132 * status pending).
133 */
134int
135ccw_device_cancel_halt_clear(struct ccw_device *cdev)
136{
137 struct subchannel *sch;
138 int ret;
139
140 sch = to_subchannel(cdev->dev.parent);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800141 ret = stsch(sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (ret || !sch->schib.pmcw.dnv)
143 return -ENODEV;
Cornelia Huck2470b642007-03-05 23:36:02 +0100144 if (!sch->schib.pmcw.ena)
145 /* Not operational -> done. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return 0;
147 /* Stage 1: cancel io. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200148 if (!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_HALT_PEND) &&
149 !(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_CLEAR_PEND)) {
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200150 if (!scsw_is_tm(&sch->schib.scsw)) {
151 ret = cio_cancel(sch);
152 if (ret != -EINVAL)
153 return ret;
154 }
155 /* cancel io unsuccessful or not applicable (transport mode).
156 * Continue with asynchronous instructions. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 cdev->private->iretry = 3; /* 3 halt retries. */
158 }
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200159 if (!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_CLEAR_PEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 /* Stage 2: halt io. */
161 if (cdev->private->iretry) {
162 cdev->private->iretry--;
163 ret = cio_halt(sch);
Peter Oberparleiterba4ba8a2006-07-27 14:00:23 +0200164 if (ret != -EBUSY)
165 return (ret == 0) ? -EBUSY : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167 /* halt io unsuccessful. */
168 cdev->private->iretry = 255; /* 255 clear retries. */
169 }
170 /* Stage 3: clear io. */
171 if (cdev->private->iretry) {
172 cdev->private->iretry--;
173 ret = cio_clear (sch);
174 return (ret == 0) ? -EBUSY : ret;
175 }
176 panic("Can't stop i/o on subchannel.\n");
177}
178
179static int
180ccw_device_handle_oper(struct ccw_device *cdev)
181{
182 struct subchannel *sch;
183
184 sch = to_subchannel(cdev->dev.parent);
185 cdev->private->flags.recog_done = 1;
186 /*
187 * Check if cu type and device type still match. If
188 * not, it is certainly another device and we have to
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100189 * de- and re-register.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 */
191 if (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 ||
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100194 cdev->id.dev_model != cdev->private->senseid.dev_model) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100196 ccw_device_do_unreg_rereg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 queue_work(ccw_device_work, &cdev->private->kick_work);
198 return 0;
199 }
200 cdev->private->flags.donotify = 1;
201 return 1;
202}
203
204/*
205 * The machine won't give us any notification by machine check if a chpid has
206 * been varied online on the SE so we have to find out by magic (i. e. driving
207 * the channel subsystem to device selection and updating our path masks).
208 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100209static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210__recover_lost_chpids(struct subchannel *sch, int old_lpm)
211{
212 int mask, i;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200213 struct chp_id chpid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200215 chp_id_init(&chpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 for (i = 0; i<8; i++) {
217 mask = 0x80 >> i;
218 if (!(sch->lpm & mask))
219 continue;
220 if (old_lpm & mask)
221 continue;
Peter Oberparleiterf86635f2007-04-27 16:01:26 +0200222 chpid.id = sch->schib.pmcw.chpid[i];
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200223 if (!chp_is_registered(chpid))
224 css_schedule_eval_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226}
227
228/*
229 * Stop device recognition.
230 */
231static void
232ccw_device_recog_done(struct ccw_device *cdev, int state)
233{
234 struct subchannel *sch;
235 int notify, old_lpm, same_dev;
236
237 sch = to_subchannel(cdev->dev.parent);
238
239 ccw_device_set_timeout(cdev, 0);
240 cio_disable_subchannel(sch);
241 /*
242 * Now that we tried recognition, we have performed device selection
243 * through ssch() and the path information is up to date.
244 */
245 old_lpm = sch->lpm;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800246 stsch(sch->schid, &sch->schib);
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200247 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck4ffa9232005-07-29 14:03:37 -0700248 /* Check since device may again have become not operational. */
249 if (!sch->schib.pmcw.dnv)
250 state = DEV_STATE_NOT_OPER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
252 /* Force reprobe on all chpids. */
253 old_lpm = 0;
254 if (sch->lpm != old_lpm)
255 __recover_lost_chpids(sch, old_lpm);
256 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
257 if (state == DEV_STATE_NOT_OPER) {
258 cdev->private->flags.recog_done = 1;
259 cdev->private->state = DEV_STATE_DISCONNECTED;
260 return;
261 }
262 /* Boxed devices don't need extra treatment. */
263 }
264 notify = 0;
265 same_dev = 0; /* Keep the compiler quiet... */
266 switch (state) {
267 case DEV_STATE_NOT_OPER:
Michael Ernst139b83d2008-05-07 09:22:54 +0200268 CIO_MSG_EVENT(2, "SenseID : unknown device %04x on "
269 "subchannel 0.%x.%04x\n",
270 cdev->private->dev_id.devno,
271 sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 break;
273 case DEV_STATE_OFFLINE:
274 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
275 same_dev = ccw_device_handle_oper(cdev);
276 notify = 1;
277 }
278 /* fill out sense information */
Heiko Carstens81388d22006-09-20 15:59:17 +0200279 memset(&cdev->id, 0, sizeof(cdev->id));
Heiko Carstens292888c2006-08-30 14:33:35 +0200280 cdev->id.cu_type = cdev->private->senseid.cu_type;
281 cdev->id.cu_model = cdev->private->senseid.cu_model;
282 cdev->id.dev_type = cdev->private->senseid.dev_type;
283 cdev->id.dev_model = cdev->private->senseid.dev_model;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (notify) {
285 cdev->private->state = DEV_STATE_OFFLINE;
286 if (same_dev) {
287 /* Get device online again. */
288 ccw_device_online(cdev);
289 wake_up(&cdev->private->wait_q);
290 }
291 return;
292 }
293 /* Issue device info message. */
Michael Ernst139b83d2008-05-07 09:22:54 +0200294 CIO_MSG_EVENT(4, "SenseID : device 0.%x.%04x reports: "
295 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
296 "%04X/%02X\n",
297 cdev->private->dev_id.ssid,
298 cdev->private->dev_id.devno,
299 cdev->id.cu_type, cdev->id.cu_model,
300 cdev->id.dev_type, cdev->id.dev_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 break;
302 case DEV_STATE_BOXED:
Michael Ernst139b83d2008-05-07 09:22:54 +0200303 CIO_MSG_EVENT(0, "SenseID : boxed device %04x on "
304 " subchannel 0.%x.%04x\n",
305 cdev->private->dev_id.devno,
306 sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 break;
308 }
309 cdev->private->state = state;
310 io_subchannel_recog_done(cdev);
311 if (state != DEV_STATE_NOT_OPER)
312 wake_up(&cdev->private->wait_q);
313}
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);
366 PREPARE_WORK(&cdev->private->kick_work, ccw_device_do_unreg_rereg);
367 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
390
391 if (state == DEV_STATE_BOXED)
Michael Ernst139b83d2008-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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 if (cdev->private->flags.donotify) {
396 cdev->private->flags.donotify = 0;
Peter Oberparleiter91c36912008-08-21 19:46:39 +0200397 ccw_device_oper_notify(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399 wake_up(&cdev->private->wait_q);
400
401 if (css_init_done && state != DEV_STATE_ONLINE)
402 put_device (&cdev->dev);
403}
404
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100405static int cmp_pgid(struct pgid *p1, struct pgid *p2)
Cornelia Huck7e560812006-07-12 16:40:19 +0200406{
407 char *c1;
408 char *c2;
409
410 c1 = (char *)p1;
411 c2 = (char *)p2;
412
413 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
414}
415
416static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
417{
418 int i;
419 int last;
420
421 last = 0;
422 for (i = 0; i < 8; i++) {
423 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
424 /* No PGID yet */
425 continue;
426 if (cdev->private->pgid[last].inf.ps.state1 ==
427 SNID_STATE1_RESET) {
428 /* First non-zero PGID */
429 last = i;
430 continue;
431 }
432 if (cmp_pgid(&cdev->private->pgid[i],
433 &cdev->private->pgid[last]) == 0)
434 /* Non-conflicting PGIDs */
435 continue;
436
437 /* PGID mismatch, can't pathgroup. */
438 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
439 "0.%x.%04x, can't pathgroup\n",
Cornelia Huck78964262006-10-11 15:31:38 +0200440 cdev->private->dev_id.ssid,
441 cdev->private->dev_id.devno);
Cornelia Huck7e560812006-07-12 16:40:19 +0200442 cdev->private->options.pgroup = 0;
443 return;
444 }
445 if (cdev->private->pgid[last].inf.ps.state1 ==
446 SNID_STATE1_RESET)
447 /* No previous pgid found */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200448 memcpy(&cdev->private->pgid[0],
449 &channel_subsystems[0]->global_pgid,
Cornelia Huck7e560812006-07-12 16:40:19 +0200450 sizeof(struct pgid));
451 else
452 /* Use existing pgid */
453 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
454 sizeof(struct pgid));
455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/*
458 * Function called from device_pgid.c after sense path ground has completed.
459 */
460void
461ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
462{
463 struct subchannel *sch;
464
465 sch = to_subchannel(cdev->dev.parent);
466 switch (err) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200467 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
468 cdev->private->options.pgroup = 0;
469 break;
470 case 0: /* success */
471 case -EACCES: /* partial success, some paths not operational */
472 /* Check if all pgids are equal or 0. */
473 __ccw_device_get_common_pgid(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 break;
475 case -ETIME: /* Sense path group id stopped by timeout. */
476 case -EUSERS: /* device is reserved for someone else. */
477 ccw_device_done(cdev, DEV_STATE_BOXED);
Cornelia Huck7e560812006-07-12 16:40:19 +0200478 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 default:
480 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Cornelia Huck7e560812006-07-12 16:40:19 +0200481 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
Cornelia Huck7e560812006-07-12 16:40:19 +0200483 /* Start Path Group verification. */
Cornelia Huck7e560812006-07-12 16:40:19 +0200484 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200485 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200486 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
489/*
490 * Start device recognition.
491 */
492int
493ccw_device_recognition(struct ccw_device *cdev)
494{
495 struct subchannel *sch;
496 int ret;
497
498 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
499 (cdev->private->state != DEV_STATE_BOXED))
500 return -EINVAL;
501 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckedf22092008-04-30 13:38:39 +0200502 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (ret != 0)
504 /* Couldn't enable the subchannel for i/o. Sick device. */
505 return ret;
506
507 /* After 60s the device recognition is considered to have failed. */
508 ccw_device_set_timeout(cdev, 60*HZ);
509
510 /*
511 * We used to start here with a sense pgid to find out whether a device
512 * is locked by someone else. Unfortunately, the sense pgid command
513 * code has other meanings on devices predating the path grouping
514 * algorithm, so we start with sense id and box the device after an
515 * timeout (or if sense pgid during path verification detects the device
516 * is locked, as may happen on newer devices).
517 */
518 cdev->private->flags.recog_done = 0;
519 cdev->private->state = DEV_STATE_SENSE_ID;
520 ccw_device_sense_id_start(cdev);
521 return 0;
522}
523
524/*
525 * Handle timeout in device recognition.
526 */
527static void
528ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
529{
530 int ret;
531
532 ret = ccw_device_cancel_halt_clear(cdev);
533 switch (ret) {
534 case 0:
535 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
536 break;
537 case -ENODEV:
538 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
539 break;
540 default:
541 ccw_device_set_timeout(cdev, 3*HZ);
542 }
543}
544
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546void
547ccw_device_verify_done(struct ccw_device *cdev, int err)
548{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200549 struct subchannel *sch;
550
551 sch = to_subchannel(cdev->dev.parent);
552 /* Update schib - pom may have changed. */
553 stsch(sch->schid, &sch->schib);
554 /* Update lpm with verified path mask. */
555 sch->lpm = sch->vpm;
556 /* Repeat path verification? */
557 if (cdev->private->flags.doverify) {
558 cdev->private->flags.doverify = 0;
559 ccw_device_verify_start(cdev);
560 return;
561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 switch (err) {
563 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
564 cdev->private->options.pgroup = 0;
565 case 0:
566 ccw_device_done(cdev, DEV_STATE_ONLINE);
567 /* Deliver fake irb to device driver, if needed. */
568 if (cdev->private->flags.fake_irb) {
569 memset(&cdev->private->irb, 0, sizeof(struct irb));
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200570 cdev->private->irb.scsw.cmd.cc = 1;
571 cdev->private->irb.scsw.cmd.fctl = SCSW_FCTL_START_FUNC;
572 cdev->private->irb.scsw.cmd.actl = SCSW_ACTL_START_PEND;
573 cdev->private->irb.scsw.cmd.stctl =
574 SCSW_STCTL_STATUS_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 cdev->private->flags.fake_irb = 0;
576 if (cdev->handler)
577 cdev->handler(cdev, cdev->private->intparm,
578 &cdev->private->irb);
579 memset(&cdev->private->irb, 0, sizeof(struct irb));
580 }
581 break;
582 case -ETIME:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200583 /* Reset oper notify indication after verify error. */
584 cdev->private->flags.donotify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 ccw_device_done(cdev, DEV_STATE_BOXED);
586 break;
587 default:
Peter Oberparleiter8b42f5c2006-10-18 18:30:43 +0200588 /* Reset oper notify indication after verify error. */
589 cdev->private->flags.donotify = 0;
Cornelia Huck46258ab2008-01-26 14:10:49 +0100590 if (cdev->online) {
591 ccw_device_set_timeout(cdev, 0);
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200592 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Cornelia Huck46258ab2008-01-26 14:10:49 +0100593 } else
Cornelia Huckee04bbc2007-03-05 23:35:56 +0100594 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 break;
596 }
597}
598
599/*
600 * Get device online.
601 */
602int
603ccw_device_online(struct ccw_device *cdev)
604{
605 struct subchannel *sch;
606 int ret;
607
608 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
609 (cdev->private->state != DEV_STATE_BOXED))
610 return -EINVAL;
611 sch = to_subchannel(cdev->dev.parent);
612 if (css_init_done && !get_device(&cdev->dev))
613 return -ENODEV;
Cornelia Huckedf22092008-04-30 13:38:39 +0200614 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (ret != 0) {
616 /* Couldn't enable the subchannel for i/o. Sick device. */
617 if (ret == -ENODEV)
618 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
619 return ret;
620 }
621 /* Do we want to do path grouping? */
622 if (!cdev->private->options.pgroup) {
Cornelia Huck7e560812006-07-12 16:40:19 +0200623 /* Start initial path verification. */
624 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200625 cdev->private->flags.doverify = 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200626 ccw_device_verify_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return 0;
628 }
629 /* Do a SensePGID first. */
630 cdev->private->state = DEV_STATE_SENSE_PGID;
631 ccw_device_sense_pgid_start(cdev);
632 return 0;
633}
634
635void
636ccw_device_disband_done(struct ccw_device *cdev, int err)
637{
638 switch (err) {
639 case 0:
640 ccw_device_done(cdev, DEV_STATE_OFFLINE);
641 break;
642 case -ETIME:
643 ccw_device_done(cdev, DEV_STATE_BOXED);
644 break;
645 default:
Peter Oberparleiter3ecb0a52007-05-31 17:38:07 +0200646 cdev->private->flags.donotify = 0;
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200647 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
649 break;
650 }
651}
652
653/*
654 * Shutdown device.
655 */
656int
657ccw_device_offline(struct ccw_device *cdev)
658{
659 struct subchannel *sch;
660
Peter Oberparleiterb301ea82008-09-09 12:38:59 +0200661 /* Allow ccw_device_offline while disconnected. */
662 if (cdev->private->state == DEV_STATE_DISCONNECTED ||
663 cdev->private->state == DEV_STATE_NOT_OPER) {
664 cdev->private->flags.donotify = 0;
665 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
666 return 0;
667 }
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100668 if (ccw_device_is_orphan(cdev)) {
669 ccw_device_done(cdev, DEV_STATE_OFFLINE);
670 return 0;
671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 sch = to_subchannel(cdev->dev.parent);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800673 if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return -ENODEV;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200675 if (scsw_actl(&sch->schib.scsw) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -EBUSY;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200677 if (cdev->private->state != DEV_STATE_ONLINE)
678 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /* Are we doing path grouping? */
680 if (!cdev->private->options.pgroup) {
681 /* No, set state offline immediately. */
682 ccw_device_done(cdev, DEV_STATE_OFFLINE);
683 return 0;
684 }
685 /* Start Set Path Group commands. */
686 cdev->private->state = DEV_STATE_DISBAND_PGID;
687 ccw_device_disband_start(cdev);
688 return 0;
689}
690
691/*
692 * Handle timeout in device online/offline process.
693 */
694static void
695ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
696{
697 int ret;
698
699 ret = ccw_device_cancel_halt_clear(cdev);
700 switch (ret) {
701 case 0:
702 ccw_device_done(cdev, DEV_STATE_BOXED);
703 break;
704 case -ENODEV:
705 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
706 break;
707 default:
708 ccw_device_set_timeout(cdev, 3*HZ);
709 }
710}
711
712/*
713 * Handle not oper event in device recognition.
714 */
715static void
716ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
717{
718 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
719}
720
721/*
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200722 * Handle not operational event in non-special state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 */
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200724static void ccw_device_generic_notoper(struct ccw_device *cdev,
725 enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 struct subchannel *sch;
728
729 cdev->private->state = DEV_STATE_NOT_OPER;
730 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200731 css_schedule_eval(sch->schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
734/*
735 * Handle path verification event.
736 */
737static void
738ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
739{
740 struct subchannel *sch;
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (cdev->private->state == DEV_STATE_W4SENSE) {
743 cdev->private->flags.doverify = 1;
744 return;
745 }
746 sch = to_subchannel(cdev->dev.parent);
747 /*
748 * Since we might not just be coming from an interrupt from the
749 * subchannel we have to update the schib.
750 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800751 stsch(sch->schid, &sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200753 if (scsw_actl(&sch->schib.scsw) != 0 ||
754 (scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_STATUS_PEND) ||
755 (scsw_stctl(&cdev->private->irb.scsw) & SCSW_STCTL_STATUS_PEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /*
757 * No final status yet or final status not yet delivered
758 * to the device driver. Can't do path verfication now,
759 * delay until final status was delivered.
760 */
761 cdev->private->flags.doverify = 1;
762 return;
763 }
764 /* Device is idle, we can do the path verification. */
765 cdev->private->state = DEV_STATE_VERIFY;
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200766 cdev->private->flags.doverify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 ccw_device_verify_start(cdev);
768}
769
770/*
771 * Got an interrupt for a normal io (state online).
772 */
773static void
774ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
775{
776 struct irb *irb;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200777 int is_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 irb = (struct irb *) __LC_IRB;
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200780 is_cmd = !scsw_is_tm(&irb->scsw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /* Check for unsolicited interrupt. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200782 if (!scsw_is_solicited(&irb->scsw)) {
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200783 if (is_cmd && (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 !irb->esw.esw0.erw.cons) {
785 /* Unit check but no sense data. Need basic sense. */
786 if (ccw_device_do_sense(cdev, irb) != 0)
787 goto call_handler_unsol;
Cornelia Hucke0ec5742006-06-04 02:51:27 -0700788 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 cdev->private->state = DEV_STATE_W4SENSE;
790 cdev->private->intparm = 0;
791 return;
792 }
793call_handler_unsol:
794 if (cdev->handler)
795 cdev->handler (cdev, 0, irb);
Cornelia Huck18374d32007-02-05 21:17:09 +0100796 if (cdev->private->flags.doverify)
797 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return;
799 }
800 /* Accumulate status and find out if a basic sense is needed. */
801 ccw_device_accumulate_irb(cdev, irb);
Peter Oberparleiter83262d62008-07-14 09:58:51 +0200802 if (is_cmd && cdev->private->flags.dosense) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (ccw_device_do_sense(cdev, irb) == 0) {
804 cdev->private->state = DEV_STATE_W4SENSE;
805 }
806 return;
807 }
808 /* Call the handler. */
809 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
810 /* Start delayed path verification. */
811 ccw_device_online_verify(cdev, 0);
812}
813
814/*
815 * Got an timeout in online state.
816 */
817static void
818ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
819{
820 int ret;
821
822 ccw_device_set_timeout(cdev, 0);
823 ret = ccw_device_cancel_halt_clear(cdev);
824 if (ret == -EBUSY) {
825 ccw_device_set_timeout(cdev, 3*HZ);
826 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
827 return;
828 }
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200829 if (ret == -ENODEV)
830 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
831 else if (cdev->handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 cdev->handler(cdev, cdev->private->intparm,
833 ERR_PTR(-ETIMEDOUT));
834}
835
836/*
837 * Got an interrupt for a basic sense.
838 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100839static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
841{
842 struct irb *irb;
843
844 irb = (struct irb *) __LC_IRB;
845 /* Check for unsolicited interrupt. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200846 if (scsw_stctl(&irb->scsw) ==
847 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
848 if (scsw_cc(&irb->scsw) == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /* Basic sense hasn't started. Try again. */
850 ccw_device_do_sense(cdev, irb);
851 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200852 CIO_MSG_EVENT(0, "0.%x.%04x: unsolicited "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200853 "interrupt during w4sense...\n",
854 cdev->private->dev_id.ssid,
855 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (cdev->handler)
857 cdev->handler (cdev, 0, irb);
858 }
859 return;
860 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800861 /*
862 * Check if a halt or clear has been issued in the meanwhile. If yes,
863 * only deliver the halt/clear interrupt to the device driver as if it
864 * had killed the original request.
865 */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200866 if (scsw_fctl(&irb->scsw) &
867 (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
Cornelia Huckd23861f2006-12-04 15:41:04 +0100868 /* Retry Basic Sense if requested. */
869 if (cdev->private->flags.intretry) {
870 cdev->private->flags.intretry = 0;
871 ccw_device_do_sense(cdev, irb);
872 return;
873 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800874 cdev->private->flags.dosense = 0;
875 memset(&cdev->private->irb, 0, sizeof(struct irb));
876 ccw_device_accumulate_irb(cdev, irb);
877 goto call_handler;
878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /* Add basic sense info to irb. */
880 ccw_device_accumulate_basic_sense(cdev, irb);
881 if (cdev->private->flags.dosense) {
882 /* Another basic sense is needed. */
883 ccw_device_do_sense(cdev, irb);
884 return;
885 }
Cornelia Huck3ba19982006-03-24 03:15:12 -0800886call_handler:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 cdev->private->state = DEV_STATE_ONLINE;
888 /* Call the handler. */
889 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
890 /* Start delayed path verification. */
891 ccw_device_online_verify(cdev, 0);
892}
893
894static void
895ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
896{
897 struct irb *irb;
898
899 irb = (struct irb *) __LC_IRB;
900 /* Accumulate status. We don't do basic sense. */
901 ccw_device_accumulate_irb(cdev, irb);
Cornelia Huckb4f7b1e2006-06-29 15:03:35 +0200902 /* Remember to clear irb to avoid residuals. */
903 memset(&cdev->private->irb, 0, sizeof(struct irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 /* Try to start delayed device verification. */
905 ccw_device_online_verify(cdev, 0);
906 /* Note: Don't call handler for cio initiated clear! */
907}
908
909static void
910ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
911{
912 struct subchannel *sch;
913
914 sch = to_subchannel(cdev->dev.parent);
915 ccw_device_set_timeout(cdev, 0);
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100916 /* Start delayed path verification. */
917 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 /* OK, i/o is dead now. Call interrupt handler. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (cdev->handler)
920 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200921 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
924static void
925ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
926{
927 int ret;
928
929 ret = ccw_device_cancel_halt_clear(cdev);
930 if (ret == -EBUSY) {
931 ccw_device_set_timeout(cdev, 3*HZ);
932 return;
933 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100934 /* Start delayed path verification. */
935 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (cdev->handler)
937 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200938 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
Cornelia Huckc820de32008-07-14 09:58:45 +0200941void ccw_device_kill_io(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 ret = ccw_device_cancel_halt_clear(cdev);
946 if (ret == -EBUSY) {
947 ccw_device_set_timeout(cdev, 3*HZ);
948 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
949 return;
950 }
Cornelia Huck7c8427c2007-03-05 23:35:59 +0100951 /* Start delayed path verification. */
952 ccw_device_online_verify(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (cdev->handler)
954 cdev->handler(cdev, cdev->private->intparm,
Cornelia Hucke7769b42006-10-11 15:31:41 +0200955 ERR_PTR(-EIO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
958static void
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200959ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +0200961 /* Start verification after current task finished. */
Cornelia Huck7e560812006-07-12 16:40:19 +0200962 cdev->private->flags.doverify = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
965static void
966ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
967{
968 struct irb *irb;
969
970 switch (dev_event) {
971 case DEV_EVENT_INTERRUPT:
972 irb = (struct irb *) __LC_IRB;
973 /* Check for unsolicited interrupt. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200974 if ((scsw_stctl(&irb->scsw) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200976 (!scsw_cc(&irb->scsw)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 /* FIXME: we should restart stlck here, but this
978 * is extremely unlikely ... */
979 goto out_wakeup;
980
981 ccw_device_accumulate_irb(cdev, irb);
982 /* We don't care about basic sense etc. */
983 break;
984 default: /* timeout */
985 break;
986 }
987out_wakeup:
988 wake_up(&cdev->private->wait_q);
989}
990
991static void
992ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
993{
994 struct subchannel *sch;
995
996 sch = to_subchannel(cdev->dev.parent);
Cornelia Huckedf22092008-04-30 13:38:39 +0200997 if (cio_enable_subchannel(sch, (u32)(addr_t)sch) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 /* Couldn't enable the subchannel for i/o. Sick device. */
999 return;
1000
1001 /* After 60s the device recognition is considered to have failed. */
1002 ccw_device_set_timeout(cdev, 60*HZ);
1003
1004 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1005 ccw_device_sense_id_start(cdev);
1006}
1007
Cornelia Huckc820de32008-07-14 09:58:45 +02001008void ccw_device_trigger_reprobe(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Cornelia Huckc820de32008-07-14 09:58:45 +02001010 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1013 return;
1014
Cornelia Huckc820de32008-07-14 09:58:45 +02001015 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 /* Update some values. */
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001017 if (stsch(sch->schid, &sch->schib))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return;
Cornelia Huck7674da72006-12-08 15:54:21 +01001019 if (!sch->schib.pmcw.dnv)
1020 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 /*
1022 * The pim, pam, pom values may not be accurate, but they are the best
1023 * we have before performing device selection :/
1024 */
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001025 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 /* Re-set some bits in the pmcw that were lost. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 sch->schib.pmcw.csense = 1;
1028 sch->schib.pmcw.ena = 0;
1029 if ((sch->lpm & (sch->lpm - 1)) != 0)
1030 sch->schib.pmcw.mp = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 /* We should also udate ssd info, but this has to wait. */
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001032 /* Check if this is another device which appeared on the same sch. */
1033 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1034 PREPARE_WORK(&cdev->private->kick_work,
1035 ccw_device_move_to_orphanage);
Cornelia Huckc5d4a992007-11-20 11:13:41 +01001036 queue_work(slow_path_wq, &cdev->private->kick_work);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001037 } else
1038 ccw_device_start_id(cdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040
1041static void
1042ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1043{
1044 struct subchannel *sch;
1045
1046 sch = to_subchannel(cdev->dev.parent);
1047 /*
1048 * An interrupt in state offline means a previous disable was not
1049 * successful. Try again.
1050 */
1051 cio_disable_subchannel(sch);
1052}
1053
1054static void
1055ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1056{
1057 retry_set_schib(cdev);
1058 cdev->private->state = DEV_STATE_ONLINE;
1059 dev_fsm_event(cdev, dev_event);
1060}
1061
Cornelia Huck94bb0632006-06-29 15:08:41 +02001062static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1063 enum dev_event dev_event)
1064{
1065 cmf_retry_copy_block(cdev);
1066 cdev->private->state = DEV_STATE_ONLINE;
1067 dev_fsm_event(cdev, dev_event);
1068}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070static void
1071ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1072{
1073 ccw_device_set_timeout(cdev, 0);
1074 if (dev_event == DEV_EVENT_NOTOPER)
1075 cdev->private->state = DEV_STATE_NOT_OPER;
1076 else
1077 cdev->private->state = DEV_STATE_OFFLINE;
1078 wake_up(&cdev->private->wait_q);
1079}
1080
1081static void
1082ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1083{
1084 int ret;
1085
1086 ret = ccw_device_cancel_halt_clear(cdev);
1087 switch (ret) {
1088 case 0:
1089 cdev->private->state = DEV_STATE_OFFLINE;
1090 wake_up(&cdev->private->wait_q);
1091 break;
1092 case -ENODEV:
1093 cdev->private->state = DEV_STATE_NOT_OPER;
1094 wake_up(&cdev->private->wait_q);
1095 break;
1096 default:
1097 ccw_device_set_timeout(cdev, HZ/10);
1098 }
1099}
1100
1101/*
1102 * No operation action. This is used e.g. to ignore a timeout event in
1103 * state offline.
1104 */
1105static void
1106ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1107{
1108}
1109
1110/*
1111 * Bug operation action.
1112 */
1113static void
1114ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1115{
Michael Ernst139b83d2008-05-07 09:22:54 +02001116 CIO_MSG_EVENT(0, "Internal state [%i][%i] not handled for device "
1117 "0.%x.%04x\n", cdev->private->state, dev_event,
1118 cdev->private->dev_id.ssid,
1119 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 BUG();
1121}
1122
1123/*
1124 * device statemachine
1125 */
1126fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1127 [DEV_STATE_NOT_OPER] = {
1128 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1129 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1130 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1131 [DEV_EVENT_VERIFY] = ccw_device_nop,
1132 },
1133 [DEV_STATE_SENSE_PGID] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001134 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1136 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1137 [DEV_EVENT_VERIFY] = ccw_device_nop,
1138 },
1139 [DEV_STATE_SENSE_ID] = {
1140 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1141 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1142 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1143 [DEV_EVENT_VERIFY] = ccw_device_nop,
1144 },
1145 [DEV_STATE_OFFLINE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001146 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1148 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1149 [DEV_EVENT_VERIFY] = ccw_device_nop,
1150 },
1151 [DEV_STATE_VERIFY] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001152 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1154 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001155 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 },
1157 [DEV_STATE_ONLINE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001158 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1160 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1161 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1162 },
1163 [DEV_STATE_W4SENSE] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001164 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1166 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1167 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1168 },
1169 [DEV_STATE_DISBAND_PGID] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001170 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1172 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1173 [DEV_EVENT_VERIFY] = ccw_device_nop,
1174 },
1175 [DEV_STATE_BOXED] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001176 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1178 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1179 [DEV_EVENT_VERIFY] = ccw_device_nop,
1180 },
1181 /* states to wait for i/o completion before doing something */
1182 [DEV_STATE_CLEAR_VERIFY] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001183 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1185 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1186 [DEV_EVENT_VERIFY] = ccw_device_nop,
1187 },
1188 [DEV_STATE_TIMEOUT_KILL] = {
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001189 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1191 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1192 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1193 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 [DEV_STATE_QUIESCE] = {
1195 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1196 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1197 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1198 [DEV_EVENT_VERIFY] = ccw_device_nop,
1199 },
1200 /* special states for devices gone not operational */
1201 [DEV_STATE_DISCONNECTED] = {
1202 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1203 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1204 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
Peter Oberparleiter28bdc6f2006-09-20 15:59:59 +02001205 [DEV_EVENT_VERIFY] = ccw_device_start_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 },
1207 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1208 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1209 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1210 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1211 [DEV_EVENT_VERIFY] = ccw_device_nop,
1212 },
1213 [DEV_STATE_CMFCHANGE] = {
1214 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1215 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1216 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1217 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1218 },
Cornelia Huck94bb0632006-06-29 15:08:41 +02001219 [DEV_STATE_CMFUPDATE] = {
1220 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1221 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1222 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1223 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1224 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225};
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227EXPORT_SYMBOL_GPL(ccw_device_set_timeout);