1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * finite state machine for device handling
4 *
5 * Copyright IBM Corp. 2002, 2008
6 * Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
7 * Martin Schwidefsky (schwidefsky@de.ibm.com)
8 */
9
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/jiffies.h>
13 #include <linux/string.h>
14
15 #include <asm/ccwdev.h>
16 #include <asm/cio.h>
17 #include <asm/chpid.h>
18
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"
25 #include "chp.h"
26
27 static int timeout_log_enabled;
28
ccw_timeout_log_setup(char * unused)29 static 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
ccw_timeout_log(struct ccw_device * cdev)37 static void ccw_timeout_log(struct ccw_device *cdev)
38 {
39 struct schib schib;
40 struct subchannel *sch;
41 struct io_subchannel_private *private;
42 union orb *orb;
43 int cc;
44
45 sch = to_subchannel(cdev->dev.parent);
46 private = to_io_private(sch);
47 orb = &private->orb;
48 cc = stsch(sch->schid, &schib);
49
50 printk(KERN_WARNING "cio: ccw device timeout occurred at %llx, "
51 "device information:\n", get_tod_clock());
52 printk(KERN_WARNING "cio: orb:\n");
53 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
54 orb, sizeof(*orb), 0);
55 printk(KERN_WARNING "cio: ccw device bus id: %s\n",
56 dev_name(&cdev->dev));
57 printk(KERN_WARNING "cio: subchannel bus id: %s\n",
58 dev_name(&sch->dev));
59 printk(KERN_WARNING "cio: subchannel lpm: %02x, opm: %02x, "
60 "vpm: %02x\n", sch->lpm, sch->opm, sch->vpm);
61
62 if (orb->tm.b) {
63 printk(KERN_WARNING "cio: orb indicates transport mode\n");
64 printk(KERN_WARNING "cio: last tcw:\n");
65 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
66 (void *)(addr_t)orb->tm.tcw,
67 sizeof(struct tcw), 0);
68 } else {
69 printk(KERN_WARNING "cio: orb indicates command mode\n");
70 if ((void *)(addr_t)orb->cmd.cpa ==
71 &private->dma_area->sense_ccw ||
72 (void *)(addr_t)orb->cmd.cpa ==
73 cdev->private->dma_area->iccws)
74 printk(KERN_WARNING "cio: last channel program "
75 "(intern):\n");
76 else
77 printk(KERN_WARNING "cio: last channel program:\n");
78
79 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
80 (void *)(addr_t)orb->cmd.cpa,
81 sizeof(struct ccw1), 0);
82 }
83 printk(KERN_WARNING "cio: ccw device state: %d\n",
84 cdev->private->state);
85 printk(KERN_WARNING "cio: store subchannel returned: cc=%d\n", cc);
86 printk(KERN_WARNING "cio: schib:\n");
87 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
88 &schib, sizeof(schib), 0);
89 printk(KERN_WARNING "cio: ccw device flags:\n");
90 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
91 &cdev->private->flags, sizeof(cdev->private->flags), 0);
92 }
93
94 /*
95 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
96 */
97 void
ccw_device_timeout(struct timer_list * t)98 ccw_device_timeout(struct timer_list *t)
99 {
100 struct ccw_device_private *priv = from_timer(priv, t, timer);
101 struct ccw_device *cdev = priv->cdev;
102
103 spin_lock_irq(cdev->ccwlock);
104 if (timeout_log_enabled)
105 ccw_timeout_log(cdev);
106 dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
107 spin_unlock_irq(cdev->ccwlock);
108 }
109
110 /*
111 * Set timeout
112 */
113 void
ccw_device_set_timeout(struct ccw_device * cdev,int expires)114 ccw_device_set_timeout(struct ccw_device *cdev, int expires)
115 {
116 if (expires == 0) {
117 del_timer(&cdev->private->timer);
118 return;
119 }
120 if (timer_pending(&cdev->private->timer)) {
121 if (mod_timer(&cdev->private->timer, jiffies + expires))
122 return;
123 }
124 cdev->private->timer.expires = jiffies + expires;
125 add_timer(&cdev->private->timer);
126 }
127
128 int
ccw_device_cancel_halt_clear(struct ccw_device * cdev)129 ccw_device_cancel_halt_clear(struct ccw_device *cdev)
130 {
131 struct subchannel *sch;
132 int ret;
133
134 sch = to_subchannel(cdev->dev.parent);
135 ret = cio_cancel_halt_clear(sch, &cdev->private->iretry);
136
137 if (ret == -EIO)
138 CIO_MSG_EVENT(0, "0.%x.%04x: could not stop I/O\n",
139 cdev->private->dev_id.ssid,
140 cdev->private->dev_id.devno);
141
142 return ret;
143 }
144
ccw_device_update_sense_data(struct ccw_device * cdev)145 void ccw_device_update_sense_data(struct ccw_device *cdev)
146 {
147 memset(&cdev->id, 0, sizeof(cdev->id));
148 cdev->id.cu_type = cdev->private->dma_area->senseid.cu_type;
149 cdev->id.cu_model = cdev->private->dma_area->senseid.cu_model;
150 cdev->id.dev_type = cdev->private->dma_area->senseid.dev_type;
151 cdev->id.dev_model = cdev->private->dma_area->senseid.dev_model;
152 }
153
ccw_device_test_sense_data(struct ccw_device * cdev)154 int ccw_device_test_sense_data(struct ccw_device *cdev)
155 {
156 return cdev->id.cu_type ==
157 cdev->private->dma_area->senseid.cu_type &&
158 cdev->id.cu_model ==
159 cdev->private->dma_area->senseid.cu_model &&
160 cdev->id.dev_type ==
161 cdev->private->dma_area->senseid.dev_type &&
162 cdev->id.dev_model ==
163 cdev->private->dma_area->senseid.dev_model;
164 }
165
166 /*
167 * The machine won't give us any notification by machine check if a chpid has
168 * been varied online on the SE so we have to find out by magic (i. e. driving
169 * the channel subsystem to device selection and updating our path masks).
170 */
171 static void
__recover_lost_chpids(struct subchannel * sch,int old_lpm)172 __recover_lost_chpids(struct subchannel *sch, int old_lpm)
173 {
174 int mask, i;
175 struct chp_id chpid;
176
177 chp_id_init(&chpid);
178 for (i = 0; i<8; i++) {
179 mask = 0x80 >> i;
180 if (!(sch->lpm & mask))
181 continue;
182 if (old_lpm & mask)
183 continue;
184 chpid.id = sch->schib.pmcw.chpid[i];
185 if (!chp_is_registered(chpid))
186 css_schedule_eval_all();
187 }
188 }
189
190 /*
191 * Stop device recognition.
192 */
193 static void
ccw_device_recog_done(struct ccw_device * cdev,int state)194 ccw_device_recog_done(struct ccw_device *cdev, int state)
195 {
196 struct subchannel *sch;
197 int old_lpm;
198
199 sch = to_subchannel(cdev->dev.parent);
200
201 if (cio_disable_subchannel(sch))
202 state = DEV_STATE_NOT_OPER;
203 /*
204 * Now that we tried recognition, we have performed device selection
205 * through ssch() and the path information is up to date.
206 */
207 old_lpm = sch->lpm;
208
209 /* Check since device may again have become not operational. */
210 if (cio_update_schib(sch))
211 state = DEV_STATE_NOT_OPER;
212 else
213 sch->lpm = sch->schib.pmcw.pam & sch->opm;
214
215 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
216 /* Force reprobe on all chpids. */
217 old_lpm = 0;
218 if (sch->lpm != old_lpm)
219 __recover_lost_chpids(sch, old_lpm);
220 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID &&
221 (state == DEV_STATE_NOT_OPER || state == DEV_STATE_BOXED)) {
222 cdev->private->flags.recog_done = 1;
223 cdev->private->state = DEV_STATE_DISCONNECTED;
224 wake_up(&cdev->private->wait_q);
225 return;
226 }
227 if (cdev->private->flags.resuming) {
228 cdev->private->state = state;
229 cdev->private->flags.recog_done = 1;
230 wake_up(&cdev->private->wait_q);
231 return;
232 }
233 switch (state) {
234 case DEV_STATE_NOT_OPER:
235 break;
236 case DEV_STATE_OFFLINE:
237 if (!cdev->online) {
238 ccw_device_update_sense_data(cdev);
239 break;
240 }
241 cdev->private->state = DEV_STATE_OFFLINE;
242 cdev->private->flags.recog_done = 1;
243 if (ccw_device_test_sense_data(cdev)) {
244 cdev->private->flags.donotify = 1;
245 ccw_device_online(cdev);
246 wake_up(&cdev->private->wait_q);
247 } else {
248 ccw_device_update_sense_data(cdev);
249 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
250 }
251 return;
252 case DEV_STATE_BOXED:
253 if (cdev->id.cu_type != 0) { /* device was recognized before */
254 cdev->private->flags.recog_done = 1;
255 cdev->private->state = DEV_STATE_BOXED;
256 wake_up(&cdev->private->wait_q);
257 return;
258 }
259 break;
260 }
261 cdev->private->state = state;
262 io_subchannel_recog_done(cdev);
263 wake_up(&cdev->private->wait_q);
264 }
265
266 /*
267 * Function called from device_id.c after sense id has completed.
268 */
269 void
ccw_device_sense_id_done(struct ccw_device * cdev,int err)270 ccw_device_sense_id_done(struct ccw_device *cdev, int err)
271 {
272 switch (err) {
273 case 0:
274 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
275 break;
276 case -ETIME: /* Sense id stopped by timeout. */
277 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
278 break;
279 default:
280 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
281 break;
282 }
283 }
284
285 /**
286 * ccw_device_notify() - inform the device's driver about an event
287 * @cdev: device for which an event occurred
288 * @event: event that occurred
289 *
290 * Returns:
291 * -%EINVAL if the device is offline or has no driver.
292 * -%EOPNOTSUPP if the device's driver has no notifier registered.
293 * %NOTIFY_OK if the driver wants to keep the device.
294 * %NOTIFY_BAD if the driver doesn't want to keep the device.
295 */
ccw_device_notify(struct ccw_device * cdev,int event)296 int ccw_device_notify(struct ccw_device *cdev, int event)
297 {
298 int ret = -EINVAL;
299
300 if (!cdev->drv)
301 goto out;
302 if (!cdev->online)
303 goto out;
304 CIO_MSG_EVENT(2, "notify called for 0.%x.%04x, event=%d\n",
305 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
306 event);
307 if (!cdev->drv->notify) {
308 ret = -EOPNOTSUPP;
309 goto out;
310 }
311 if (cdev->drv->notify(cdev, event))
312 ret = NOTIFY_OK;
313 else
314 ret = NOTIFY_BAD;
315 out:
316 return ret;
317 }
318
ccw_device_oper_notify(struct ccw_device * cdev)319 static void ccw_device_oper_notify(struct ccw_device *cdev)
320 {
321 struct subchannel *sch = to_subchannel(cdev->dev.parent);
322
323 if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_OK) {
324 /* Reenable channel measurements, if needed. */
325 ccw_device_sched_todo(cdev, CDEV_TODO_ENABLE_CMF);
326 /* Save indication for new paths. */
327 cdev->private->path_new_mask = sch->vpm;
328 return;
329 }
330 /* Driver doesn't want device back. */
331 ccw_device_set_notoper(cdev);
332 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
333 }
334
335 /*
336 * Finished with online/offline processing.
337 */
338 static void
ccw_device_done(struct ccw_device * cdev,int state)339 ccw_device_done(struct ccw_device *cdev, int state)
340 {
341 struct subchannel *sch;
342
343 sch = to_subchannel(cdev->dev.parent);
344
345 ccw_device_set_timeout(cdev, 0);
346
347 if (state != DEV_STATE_ONLINE)
348 cio_disable_subchannel(sch);
349
350 /* Reset device status. */
351 memset(&cdev->private->dma_area->irb, 0, sizeof(struct irb));
352
353 cdev->private->state = state;
354
355 switch (state) {
356 case DEV_STATE_BOXED:
357 CIO_MSG_EVENT(0, "Boxed device %04x on subchannel %04x\n",
358 cdev->private->dev_id.devno, sch->schid.sch_no);
359 if (cdev->online &&
360 ccw_device_notify(cdev, CIO_BOXED) != NOTIFY_OK)
361 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
362 cdev->private->flags.donotify = 0;
363 break;
364 case DEV_STATE_NOT_OPER:
365 CIO_MSG_EVENT(0, "Device %04x gone on subchannel %04x\n",
366 cdev->private->dev_id.devno, sch->schid.sch_no);
367 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
368 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
369 else
370 ccw_device_set_disconnected(cdev);
371 cdev->private->flags.donotify = 0;
372 break;
373 case DEV_STATE_DISCONNECTED:
374 CIO_MSG_EVENT(0, "Disconnected device %04x on subchannel "
375 "%04x\n", cdev->private->dev_id.devno,
376 sch->schid.sch_no);
377 if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK) {
378 cdev->private->state = DEV_STATE_NOT_OPER;
379 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
380 } else
381 ccw_device_set_disconnected(cdev);
382 cdev->private->flags.donotify = 0;
383 break;
384 default:
385 break;
386 }
387
388 if (cdev->private->flags.donotify) {
389 cdev->private->flags.donotify = 0;
390 ccw_device_oper_notify(cdev);
391 }
392 wake_up(&cdev->private->wait_q);
393 }
394
395 /*
396 * Start device recognition.
397 */
ccw_device_recognition(struct ccw_device * cdev)398 void ccw_device_recognition(struct ccw_device *cdev)
399 {
400 struct subchannel *sch = to_subchannel(cdev->dev.parent);
401
402 /*
403 * We used to start here with a sense pgid to find out whether a device
404 * is locked by someone else. Unfortunately, the sense pgid command
405 * code has other meanings on devices predating the path grouping
406 * algorithm, so we start with sense id and box the device after an
407 * timeout (or if sense pgid during path verification detects the device
408 * is locked, as may happen on newer devices).
409 */
410 cdev->private->flags.recog_done = 0;
411 cdev->private->state = DEV_STATE_SENSE_ID;
412 if (cio_enable_subchannel(sch, (u32) (addr_t) sch)) {
413 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
414 return;
415 }
416 ccw_device_sense_id_start(cdev);
417 }
418
419 /*
420 * Handle events for states that use the ccw request infrastructure.
421 */
ccw_device_request_event(struct ccw_device * cdev,enum dev_event e)422 static void ccw_device_request_event(struct ccw_device *cdev, enum dev_event e)
423 {
424 switch (e) {
425 case DEV_EVENT_NOTOPER:
426 ccw_request_notoper(cdev);
427 break;
428 case DEV_EVENT_INTERRUPT:
429 ccw_request_handler(cdev);
430 break;
431 case DEV_EVENT_TIMEOUT:
432 ccw_request_timeout(cdev);
433 break;
434 default:
435 break;
436 }
437 }
438
ccw_device_report_path_events(struct ccw_device * cdev)439 static void ccw_device_report_path_events(struct ccw_device *cdev)
440 {
441 struct subchannel *sch = to_subchannel(cdev->dev.parent);
442 int path_event[8];
443 int chp, mask;
444
445 for (chp = 0, mask = 0x80; chp < 8; chp++, mask >>= 1) {
446 path_event[chp] = PE_NONE;
447 if (mask & cdev->private->path_gone_mask & ~(sch->vpm))
448 path_event[chp] |= PE_PATH_GONE;
449 if (mask & cdev->private->path_new_mask & sch->vpm)
450 path_event[chp] |= PE_PATH_AVAILABLE;
451 if (mask & cdev->private->pgid_reset_mask & sch->vpm)
452 path_event[chp] |= PE_PATHGROUP_ESTABLISHED;
453 }
454 if (cdev->online && cdev->drv->path_event)
455 cdev->drv->path_event(cdev, path_event);
456 }
457
ccw_device_reset_path_events(struct ccw_device * cdev)458 static void ccw_device_reset_path_events(struct ccw_device *cdev)
459 {
460 cdev->private->path_gone_mask = 0;
461 cdev->private->path_new_mask = 0;
462 cdev->private->pgid_reset_mask = 0;
463 }
464
create_fake_irb(struct irb * irb,int type)465 static void create_fake_irb(struct irb *irb, int type)
466 {
467 memset(irb, 0, sizeof(*irb));
468 if (type == FAKE_CMD_IRB) {
469 struct cmd_scsw *scsw = &irb->scsw.cmd;
470 scsw->cc = 1;
471 scsw->fctl = SCSW_FCTL_START_FUNC;
472 scsw->actl = SCSW_ACTL_START_PEND;
473 scsw->stctl = SCSW_STCTL_STATUS_PEND;
474 } else if (type == FAKE_TM_IRB) {
475 struct tm_scsw *scsw = &irb->scsw.tm;
476 scsw->x = 1;
477 scsw->cc = 1;
478 scsw->fctl = SCSW_FCTL_START_FUNC;
479 scsw->actl = SCSW_ACTL_START_PEND;
480 scsw->stctl = SCSW_STCTL_STATUS_PEND;
481 }
482 }
483
ccw_device_handle_broken_paths(struct ccw_device * cdev)484 static void ccw_device_handle_broken_paths(struct ccw_device *cdev)
485 {
486 struct subchannel *sch = to_subchannel(cdev->dev.parent);
487 u8 broken_paths = (sch->schib.pmcw.pam & sch->opm) ^ sch->vpm;
488
489 if (broken_paths && (cdev->private->path_broken_mask != broken_paths))
490 ccw_device_schedule_recovery();
491
492 cdev->private->path_broken_mask = broken_paths;
493 }
494
ccw_device_verify_done(struct ccw_device * cdev,int err)495 void ccw_device_verify_done(struct ccw_device *cdev, int err)
496 {
497 struct subchannel *sch;
498
499 sch = to_subchannel(cdev->dev.parent);
500 /* Update schib - pom may have changed. */
501 if (cio_update_schib(sch)) {
502 err = -ENODEV;
503 goto callback;
504 }
505 /* Update lpm with verified path mask. */
506 sch->lpm = sch->vpm;
507 /* Repeat path verification? */
508 if (cdev->private->flags.doverify) {
509 ccw_device_verify_start(cdev);
510 return;
511 }
512 callback:
513 switch (err) {
514 case 0:
515 ccw_device_done(cdev, DEV_STATE_ONLINE);
516 /* Deliver fake irb to device driver, if needed. */
517 if (cdev->private->flags.fake_irb) {
518 create_fake_irb(&cdev->private->dma_area->irb,
519 cdev->private->flags.fake_irb);
520 cdev->private->flags.fake_irb = 0;
521 if (cdev->handler)
522 cdev->handler(cdev, cdev->private->intparm,
523 &cdev->private->dma_area->irb);
524 memset(&cdev->private->dma_area->irb, 0,
525 sizeof(struct irb));
526 }
527 ccw_device_report_path_events(cdev);
528 ccw_device_handle_broken_paths(cdev);
529 break;
530 case -ETIME:
531 case -EUSERS:
532 /* Reset oper notify indication after verify error. */
533 cdev->private->flags.donotify = 0;
534 ccw_device_done(cdev, DEV_STATE_BOXED);
535 break;
536 case -EACCES:
537 /* Reset oper notify indication after verify error. */
538 cdev->private->flags.donotify = 0;
539 ccw_device_done(cdev, DEV_STATE_DISCONNECTED);
540 break;
541 default:
542 /* Reset oper notify indication after verify error. */
543 cdev->private->flags.donotify = 0;
544 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
545 break;
546 }
547 ccw_device_reset_path_events(cdev);
548 }
549
550 /*
551 * Get device online.
552 */
553 int
ccw_device_online(struct ccw_device * cdev)554 ccw_device_online(struct ccw_device *cdev)
555 {
556 struct subchannel *sch;
557 int ret;
558
559 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
560 (cdev->private->state != DEV_STATE_BOXED))
561 return -EINVAL;
562 sch = to_subchannel(cdev->dev.parent);
563 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
564 if (ret != 0) {
565 /* Couldn't enable the subchannel for i/o. Sick device. */
566 if (ret == -ENODEV)
567 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
568 return ret;
569 }
570 /* Start initial path verification. */
571 cdev->private->state = DEV_STATE_VERIFY;
572 ccw_device_verify_start(cdev);
573 return 0;
574 }
575
576 void
ccw_device_disband_done(struct ccw_device * cdev,int err)577 ccw_device_disband_done(struct ccw_device *cdev, int err)
578 {
579 switch (err) {
580 case 0:
581 ccw_device_done(cdev, DEV_STATE_OFFLINE);
582 break;
583 case -ETIME:
584 ccw_device_done(cdev, DEV_STATE_BOXED);
585 break;
586 default:
587 cdev->private->flags.donotify = 0;
588 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
589 break;
590 }
591 }
592
593 /*
594 * Shutdown device.
595 */
596 int
ccw_device_offline(struct ccw_device * cdev)597 ccw_device_offline(struct ccw_device *cdev)
598 {
599 struct subchannel *sch;
600
601 /* Allow ccw_device_offline while disconnected. */
602 if (cdev->private->state == DEV_STATE_DISCONNECTED ||
603 cdev->private->state == DEV_STATE_NOT_OPER) {
604 cdev->private->flags.donotify = 0;
605 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
606 return 0;
607 }
608 if (cdev->private->state == DEV_STATE_BOXED) {
609 ccw_device_done(cdev, DEV_STATE_BOXED);
610 return 0;
611 }
612 if (ccw_device_is_orphan(cdev)) {
613 ccw_device_done(cdev, DEV_STATE_OFFLINE);
614 return 0;
615 }
616 sch = to_subchannel(cdev->dev.parent);
617 if (cio_update_schib(sch))
618 return -ENODEV;
619 if (scsw_actl(&sch->schib.scsw) != 0)
620 return -EBUSY;
621 if (cdev->private->state != DEV_STATE_ONLINE)
622 return -EINVAL;
623 /* Are we doing path grouping? */
624 if (!cdev->private->flags.pgroup) {
625 /* No, set state offline immediately. */
626 ccw_device_done(cdev, DEV_STATE_OFFLINE);
627 return 0;
628 }
629 /* Start Set Path Group commands. */
630 cdev->private->state = DEV_STATE_DISBAND_PGID;
631 ccw_device_disband_start(cdev);
632 return 0;
633 }
634
635 /*
636 * Handle not operational event in non-special state.
637 */
ccw_device_generic_notoper(struct ccw_device * cdev,enum dev_event dev_event)638 static void ccw_device_generic_notoper(struct ccw_device *cdev,
639 enum dev_event dev_event)
640 {
641 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
642 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
643 else
644 ccw_device_set_disconnected(cdev);
645 }
646
647 /*
648 * Handle path verification event in offline state.
649 */
ccw_device_offline_verify(struct ccw_device * cdev,enum dev_event dev_event)650 static void ccw_device_offline_verify(struct ccw_device *cdev,
651 enum dev_event dev_event)
652 {
653 struct subchannel *sch = to_subchannel(cdev->dev.parent);
654
655 css_schedule_eval(sch->schid);
656 }
657
658 /*
659 * Handle path verification event.
660 */
661 static void
ccw_device_online_verify(struct ccw_device * cdev,enum dev_event dev_event)662 ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
663 {
664 struct subchannel *sch;
665
666 if (cdev->private->state == DEV_STATE_W4SENSE) {
667 cdev->private->flags.doverify = 1;
668 return;
669 }
670 sch = to_subchannel(cdev->dev.parent);
671 /*
672 * Since we might not just be coming from an interrupt from the
673 * subchannel we have to update the schib.
674 */
675 if (cio_update_schib(sch)) {
676 ccw_device_verify_done(cdev, -ENODEV);
677 return;
678 }
679
680 if (scsw_actl(&sch->schib.scsw) != 0 ||
681 (scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_STATUS_PEND) ||
682 (scsw_stctl(&cdev->private->dma_area->irb.scsw) &
683 SCSW_STCTL_STATUS_PEND)) {
684 /*
685 * No final status yet or final status not yet delivered
686 * to the device driver. Can't do path verification now,
687 * delay until final status was delivered.
688 */
689 cdev->private->flags.doverify = 1;
690 return;
691 }
692 /* Device is idle, we can do the path verification. */
693 cdev->private->state = DEV_STATE_VERIFY;
694 ccw_device_verify_start(cdev);
695 }
696
697 /*
698 * Handle path verification event in boxed state.
699 */
ccw_device_boxed_verify(struct ccw_device * cdev,enum dev_event dev_event)700 static void ccw_device_boxed_verify(struct ccw_device *cdev,
701 enum dev_event dev_event)
702 {
703 struct subchannel *sch = to_subchannel(cdev->dev.parent);
704
705 if (cdev->online) {
706 if (cio_enable_subchannel(sch, (u32) (addr_t) sch))
707 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
708 else
709 ccw_device_online_verify(cdev, dev_event);
710 } else
711 css_schedule_eval(sch->schid);
712 }
713
714 /*
715 * Pass interrupt to device driver.
716 */
ccw_device_call_handler(struct ccw_device * cdev)717 static int ccw_device_call_handler(struct ccw_device *cdev)
718 {
719 unsigned int stctl;
720 int ending_status;
721
722 /*
723 * we allow for the device action handler if .
724 * - we received ending status
725 * - the action handler requested to see all interrupts
726 * - we received an intermediate status
727 * - fast notification was requested (primary status)
728 * - unsolicited interrupts
729 */
730 stctl = scsw_stctl(&cdev->private->dma_area->irb.scsw);
731 ending_status = (stctl & SCSW_STCTL_SEC_STATUS) ||
732 (stctl == (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)) ||
733 (stctl == SCSW_STCTL_STATUS_PEND);
734 if (!ending_status &&
735 !cdev->private->options.repall &&
736 !(stctl & SCSW_STCTL_INTER_STATUS) &&
737 !(cdev->private->options.fast &&
738 (stctl & SCSW_STCTL_PRIM_STATUS)))
739 return 0;
740
741 if (ending_status)
742 ccw_device_set_timeout(cdev, 0);
743
744 if (cdev->handler)
745 cdev->handler(cdev, cdev->private->intparm,
746 &cdev->private->dma_area->irb);
747
748 memset(&cdev->private->dma_area->irb, 0, sizeof(struct irb));
749 return 1;
750 }
751
752 /*
753 * Got an interrupt for a normal io (state online).
754 */
755 static void
ccw_device_irq(struct ccw_device * cdev,enum dev_event dev_event)756 ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
757 {
758 struct irb *irb;
759 int is_cmd;
760
761 irb = this_cpu_ptr(&cio_irb);
762 is_cmd = !scsw_is_tm(&irb->scsw);
763 /* Check for unsolicited interrupt. */
764 if (!scsw_is_solicited(&irb->scsw)) {
765 if (is_cmd && (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
766 !irb->esw.esw0.erw.cons) {
767 /* Unit check but no sense data. Need basic sense. */
768 if (ccw_device_do_sense(cdev, irb) != 0)
769 goto call_handler_unsol;
770 memcpy(&cdev->private->dma_area->irb, irb,
771 sizeof(struct irb));
772 cdev->private->state = DEV_STATE_W4SENSE;
773 cdev->private->intparm = 0;
774 return;
775 }
776 call_handler_unsol:
777 if (cdev->handler)
778 cdev->handler (cdev, 0, irb);
779 if (cdev->private->flags.doverify)
780 ccw_device_online_verify(cdev, 0);
781 return;
782 }
783 /* Accumulate status and find out if a basic sense is needed. */
784 ccw_device_accumulate_irb(cdev, irb);
785 if (is_cmd && cdev->private->flags.dosense) {
786 if (ccw_device_do_sense(cdev, irb) == 0) {
787 cdev->private->state = DEV_STATE_W4SENSE;
788 }
789 return;
790 }
791 /* Call the handler. */
792 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
793 /* Start delayed path verification. */
794 ccw_device_online_verify(cdev, 0);
795 }
796
797 /*
798 * Got an timeout in online state.
799 */
800 static void
ccw_device_online_timeout(struct ccw_device * cdev,enum dev_event dev_event)801 ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
802 {
803 int ret;
804
805 ccw_device_set_timeout(cdev, 0);
806 cdev->private->iretry = 255;
807 cdev->private->async_kill_io_rc = -ETIMEDOUT;
808 ret = ccw_device_cancel_halt_clear(cdev);
809 if (ret == -EBUSY) {
810 ccw_device_set_timeout(cdev, 3*HZ);
811 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
812 return;
813 }
814 if (ret)
815 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
816 else if (cdev->handler)
817 cdev->handler(cdev, cdev->private->intparm,
818 ERR_PTR(-ETIMEDOUT));
819 }
820
821 /*
822 * Got an interrupt for a basic sense.
823 */
824 static void
ccw_device_w4sense(struct ccw_device * cdev,enum dev_event dev_event)825 ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
826 {
827 struct irb *irb;
828
829 irb = this_cpu_ptr(&cio_irb);
830 /* Check for unsolicited interrupt. */
831 if (scsw_stctl(&irb->scsw) ==
832 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
833 if (scsw_cc(&irb->scsw) == 1)
834 /* Basic sense hasn't started. Try again. */
835 ccw_device_do_sense(cdev, irb);
836 else {
837 CIO_MSG_EVENT(0, "0.%x.%04x: unsolicited "
838 "interrupt during w4sense...\n",
839 cdev->private->dev_id.ssid,
840 cdev->private->dev_id.devno);
841 if (cdev->handler)
842 cdev->handler (cdev, 0, irb);
843 }
844 return;
845 }
846 /*
847 * Check if a halt or clear has been issued in the meanwhile. If yes,
848 * only deliver the halt/clear interrupt to the device driver as if it
849 * had killed the original request.
850 */
851 if (scsw_fctl(&irb->scsw) &
852 (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
853 cdev->private->flags.dosense = 0;
854 memset(&cdev->private->dma_area->irb, 0, sizeof(struct irb));
855 ccw_device_accumulate_irb(cdev, irb);
856 goto call_handler;
857 }
858 /* Add basic sense info to irb. */
859 ccw_device_accumulate_basic_sense(cdev, irb);
860 if (cdev->private->flags.dosense) {
861 /* Another basic sense is needed. */
862 ccw_device_do_sense(cdev, irb);
863 return;
864 }
865 call_handler:
866 cdev->private->state = DEV_STATE_ONLINE;
867 /* In case sensing interfered with setting the device online */
868 wake_up(&cdev->private->wait_q);
869 /* Call the handler. */
870 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
871 /* Start delayed path verification. */
872 ccw_device_online_verify(cdev, 0);
873 }
874
875 static void
ccw_device_killing_irq(struct ccw_device * cdev,enum dev_event dev_event)876 ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
877 {
878 ccw_device_set_timeout(cdev, 0);
879 /* Start delayed path verification. */
880 ccw_device_online_verify(cdev, 0);
881 /* OK, i/o is dead now. Call interrupt handler. */
882 if (cdev->handler)
883 cdev->handler(cdev, cdev->private->intparm,
884 ERR_PTR(cdev->private->async_kill_io_rc));
885 }
886
887 static void
ccw_device_killing_timeout(struct ccw_device * cdev,enum dev_event dev_event)888 ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
889 {
890 int ret;
891
892 ret = ccw_device_cancel_halt_clear(cdev);
893 if (ret == -EBUSY) {
894 ccw_device_set_timeout(cdev, 3*HZ);
895 return;
896 }
897 /* Start delayed path verification. */
898 ccw_device_online_verify(cdev, 0);
899 if (cdev->handler)
900 cdev->handler(cdev, cdev->private->intparm,
901 ERR_PTR(cdev->private->async_kill_io_rc));
902 }
903
ccw_device_kill_io(struct ccw_device * cdev)904 void ccw_device_kill_io(struct ccw_device *cdev)
905 {
906 int ret;
907
908 ccw_device_set_timeout(cdev, 0);
909 cdev->private->iretry = 255;
910 cdev->private->async_kill_io_rc = -EIO;
911 ret = ccw_device_cancel_halt_clear(cdev);
912 if (ret == -EBUSY) {
913 ccw_device_set_timeout(cdev, 3*HZ);
914 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
915 return;
916 }
917 /* Start delayed path verification. */
918 ccw_device_online_verify(cdev, 0);
919 if (cdev->handler)
920 cdev->handler(cdev, cdev->private->intparm,
921 ERR_PTR(-EIO));
922 }
923
924 static void
ccw_device_delay_verify(struct ccw_device * cdev,enum dev_event dev_event)925 ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
926 {
927 /* Start verification after current task finished. */
928 cdev->private->flags.doverify = 1;
929 }
930
931 static void
ccw_device_start_id(struct ccw_device * cdev,enum dev_event dev_event)932 ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
933 {
934 struct subchannel *sch;
935
936 sch = to_subchannel(cdev->dev.parent);
937 if (cio_enable_subchannel(sch, (u32)(addr_t)sch) != 0)
938 /* Couldn't enable the subchannel for i/o. Sick device. */
939 return;
940 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
941 ccw_device_sense_id_start(cdev);
942 }
943
ccw_device_trigger_reprobe(struct ccw_device * cdev)944 void ccw_device_trigger_reprobe(struct ccw_device *cdev)
945 {
946 struct subchannel *sch;
947
948 if (cdev->private->state != DEV_STATE_DISCONNECTED)
949 return;
950
951 sch = to_subchannel(cdev->dev.parent);
952 /* Update some values. */
953 if (cio_update_schib(sch))
954 return;
955 /*
956 * The pim, pam, pom values may not be accurate, but they are the best
957 * we have before performing device selection :/
958 */
959 sch->lpm = sch->schib.pmcw.pam & sch->opm;
960 /*
961 * Use the initial configuration since we can't be shure that the old
962 * paths are valid.
963 */
964 io_subchannel_init_config(sch);
965 if (cio_commit_config(sch))
966 return;
967
968 /* We should also udate ssd info, but this has to wait. */
969 /* Check if this is another device which appeared on the same sch. */
970 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno)
971 css_schedule_eval(sch->schid);
972 else
973 ccw_device_start_id(cdev, 0);
974 }
975
ccw_device_disabled_irq(struct ccw_device * cdev,enum dev_event dev_event)976 static void ccw_device_disabled_irq(struct ccw_device *cdev,
977 enum dev_event dev_event)
978 {
979 struct subchannel *sch;
980
981 sch = to_subchannel(cdev->dev.parent);
982 /*
983 * An interrupt in a disabled state means a previous disable was not
984 * successful - should not happen, but we try to disable again.
985 */
986 cio_disable_subchannel(sch);
987 }
988
989 static void
ccw_device_change_cmfstate(struct ccw_device * cdev,enum dev_event dev_event)990 ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
991 {
992 retry_set_schib(cdev);
993 cdev->private->state = DEV_STATE_ONLINE;
994 dev_fsm_event(cdev, dev_event);
995 }
996
ccw_device_update_cmfblock(struct ccw_device * cdev,enum dev_event dev_event)997 static void ccw_device_update_cmfblock(struct ccw_device *cdev,
998 enum dev_event dev_event)
999 {
1000 cmf_retry_copy_block(cdev);
1001 cdev->private->state = DEV_STATE_ONLINE;
1002 dev_fsm_event(cdev, dev_event);
1003 }
1004
1005 static void
ccw_device_quiesce_done(struct ccw_device * cdev,enum dev_event dev_event)1006 ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1007 {
1008 ccw_device_set_timeout(cdev, 0);
1009 cdev->private->state = DEV_STATE_NOT_OPER;
1010 wake_up(&cdev->private->wait_q);
1011 }
1012
1013 static void
ccw_device_quiesce_timeout(struct ccw_device * cdev,enum dev_event dev_event)1014 ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1015 {
1016 int ret;
1017
1018 ret = ccw_device_cancel_halt_clear(cdev);
1019 if (ret == -EBUSY) {
1020 ccw_device_set_timeout(cdev, HZ/10);
1021 } else {
1022 cdev->private->state = DEV_STATE_NOT_OPER;
1023 wake_up(&cdev->private->wait_q);
1024 }
1025 }
1026
1027 /*
1028 * No operation action. This is used e.g. to ignore a timeout event in
1029 * state offline.
1030 */
1031 static void
ccw_device_nop(struct ccw_device * cdev,enum dev_event dev_event)1032 ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1033 {
1034 }
1035
1036 /*
1037 * device statemachine
1038 */
1039 fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1040 [DEV_STATE_NOT_OPER] = {
1041 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1042 [DEV_EVENT_INTERRUPT] = ccw_device_disabled_irq,
1043 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1044 [DEV_EVENT_VERIFY] = ccw_device_nop,
1045 },
1046 [DEV_STATE_SENSE_ID] = {
1047 [DEV_EVENT_NOTOPER] = ccw_device_request_event,
1048 [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
1049 [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
1050 [DEV_EVENT_VERIFY] = ccw_device_nop,
1051 },
1052 [DEV_STATE_OFFLINE] = {
1053 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1054 [DEV_EVENT_INTERRUPT] = ccw_device_disabled_irq,
1055 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1056 [DEV_EVENT_VERIFY] = ccw_device_offline_verify,
1057 },
1058 [DEV_STATE_VERIFY] = {
1059 [DEV_EVENT_NOTOPER] = ccw_device_request_event,
1060 [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
1061 [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
1062 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
1063 },
1064 [DEV_STATE_ONLINE] = {
1065 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1066 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1067 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1068 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1069 },
1070 [DEV_STATE_W4SENSE] = {
1071 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1072 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1073 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1074 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1075 },
1076 [DEV_STATE_DISBAND_PGID] = {
1077 [DEV_EVENT_NOTOPER] = ccw_device_request_event,
1078 [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
1079 [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
1080 [DEV_EVENT_VERIFY] = ccw_device_nop,
1081 },
1082 [DEV_STATE_BOXED] = {
1083 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1084 [DEV_EVENT_INTERRUPT] = ccw_device_nop,
1085 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1086 [DEV_EVENT_VERIFY] = ccw_device_boxed_verify,
1087 },
1088 /* states to wait for i/o completion before doing something */
1089 [DEV_STATE_TIMEOUT_KILL] = {
1090 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1091 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1092 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1093 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1094 },
1095 [DEV_STATE_QUIESCE] = {
1096 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1097 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1098 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1099 [DEV_EVENT_VERIFY] = ccw_device_nop,
1100 },
1101 /* special states for devices gone not operational */
1102 [DEV_STATE_DISCONNECTED] = {
1103 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1104 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1105 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1106 [DEV_EVENT_VERIFY] = ccw_device_start_id,
1107 },
1108 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1109 [DEV_EVENT_NOTOPER] = ccw_device_request_event,
1110 [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
1111 [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
1112 [DEV_EVENT_VERIFY] = ccw_device_nop,
1113 },
1114 [DEV_STATE_CMFCHANGE] = {
1115 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1116 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1117 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1118 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1119 },
1120 [DEV_STATE_CMFUPDATE] = {
1121 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1122 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1123 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1124 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1125 },
1126 [DEV_STATE_STEAL_LOCK] = {
1127 [DEV_EVENT_NOTOPER] = ccw_device_request_event,
1128 [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
1129 [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
1130 [DEV_EVENT_VERIFY] = ccw_device_nop,
1131 },
1132 };
1133
1134 EXPORT_SYMBOL_GPL(ccw_device_set_timeout);
1135