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