2 * CCW device PGID and path verification I/O handling.
4 * Copyright IBM Corp. 2002, 2009
5 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
6 * Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
10 #include <linux/kernel.h>
11 #include <linux/string.h>
12 #include <linux/bitops.h>
13 #include <linux/types.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <asm/ccwdev.h>
20 #include "cio_debug.h"
24 #define PGID_RETRIES 256
25 #define PGID_TIMEOUT (10 * HZ)
27 static void verify_start(struct ccw_device
*cdev
);
30 * Process path verification data and report result.
32 static void verify_done(struct ccw_device
*cdev
, int rc
)
34 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
35 struct ccw_dev_id
*id
= &cdev
->private->dev_id
;
36 int mpath
= cdev
->private->flags
.mpath
;
37 int pgroup
= cdev
->private->flags
.pgroup
;
41 /* Ensure consistent multipathing state at device and channel. */
42 if (sch
->config
.mp
!= mpath
) {
43 sch
->config
.mp
= mpath
;
44 rc
= cio_commit_config(sch
);
47 CIO_MSG_EVENT(2, "vrfy: device 0.%x.%04x: rc=%d pgroup=%d mpath=%d "
48 "vpm=%02x\n", id
->ssid
, id
->devno
, rc
, pgroup
, mpath
,
50 ccw_device_verify_done(cdev
, rc
);
54 * Create channel program to perform a NOOP.
56 static void nop_build_cp(struct ccw_device
*cdev
)
58 struct ccw_request
*req
= &cdev
->private->req
;
59 struct ccw1
*cp
= cdev
->private->iccws
;
61 cp
->cmd_code
= CCW_CMD_NOOP
;
64 cp
->flags
= CCW_FLAG_SLI
;
69 * Perform NOOP on a single path.
71 static void nop_do(struct ccw_device
*cdev
)
73 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
74 struct ccw_request
*req
= &cdev
->private->req
;
76 req
->lpm
= lpm_adjust(req
->lpm
, sch
->schib
.pmcw
.pam
& sch
->opm
&
77 ~cdev
->private->path_noirq_mask
);
81 ccw_request_start(cdev
);
85 verify_done(cdev
, sch
->vpm
? 0 : -EACCES
);
89 * Adjust NOOP I/O status.
91 static enum io_status
nop_filter(struct ccw_device
*cdev
, void *data
,
92 struct irb
*irb
, enum io_status status
)
94 /* Only subchannel status might indicate a path error. */
95 if (status
== IO_STATUS_ERROR
&& irb
->scsw
.cmd
.cstat
== 0)
101 * Process NOOP request result for a single path.
103 static void nop_callback(struct ccw_device
*cdev
, void *data
, int rc
)
105 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
106 struct ccw_request
*req
= &cdev
->private->req
;
110 sch
->vpm
|= req
->lpm
;
113 cdev
->private->path_noirq_mask
|= req
->lpm
;
116 cdev
->private->path_notoper_mask
|= req
->lpm
;
121 /* Continue on the next path. */
127 verify_done(cdev
, rc
);
131 * Create channel program to perform SET PGID on a single path.
133 static void spid_build_cp(struct ccw_device
*cdev
, u8 fn
)
135 struct ccw_request
*req
= &cdev
->private->req
;
136 struct ccw1
*cp
= cdev
->private->iccws
;
137 int i
= pathmask_to_pos(req
->lpm
);
138 struct pgid
*pgid
= &cdev
->private->pgid
[i
];
141 cp
->cmd_code
= CCW_CMD_SET_PGID
;
142 cp
->cda
= (u32
) (addr_t
) pgid
;
143 cp
->count
= sizeof(*pgid
);
144 cp
->flags
= CCW_FLAG_SLI
;
148 static void pgid_wipeout_callback(struct ccw_device
*cdev
, void *data
, int rc
)
151 /* We don't know the path groups' state. Abort. */
152 verify_done(cdev
, rc
);
156 * Path groups have been reset. Restart path verification but
157 * leave paths in path_noirq_mask out.
159 cdev
->private->flags
.pgid_unknown
= 0;
164 * Reset pathgroups and restart path verification, leave unusable paths out.
166 static void pgid_wipeout_start(struct ccw_device
*cdev
)
168 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
169 struct ccw_dev_id
*id
= &cdev
->private->dev_id
;
170 struct ccw_request
*req
= &cdev
->private->req
;
173 CIO_MSG_EVENT(2, "wipe: device 0.%x.%04x: pvm=%02x nim=%02x\n",
174 id
->ssid
, id
->devno
, cdev
->private->pgid_valid_mask
,
175 cdev
->private->path_noirq_mask
);
177 /* Initialize request data. */
178 memset(req
, 0, sizeof(*req
));
179 req
->timeout
= PGID_TIMEOUT
;
180 req
->maxretries
= PGID_RETRIES
;
181 req
->lpm
= sch
->schib
.pmcw
.pam
;
182 req
->callback
= pgid_wipeout_callback
;
183 fn
= SPID_FUNC_DISBAND
;
184 if (cdev
->private->flags
.mpath
)
185 fn
|= SPID_FUNC_MULTI_PATH
;
186 spid_build_cp(cdev
, fn
);
187 ccw_request_start(cdev
);
191 * Perform establish/resign SET PGID on a single path.
193 static void spid_do(struct ccw_device
*cdev
)
195 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
196 struct ccw_request
*req
= &cdev
->private->req
;
199 /* Use next available path that is not already in correct state. */
200 req
->lpm
= lpm_adjust(req
->lpm
, cdev
->private->pgid_todo_mask
);
203 /* Channel program setup. */
204 if (req
->lpm
& sch
->opm
)
205 fn
= SPID_FUNC_ESTABLISH
;
207 fn
= SPID_FUNC_RESIGN
;
208 if (cdev
->private->flags
.mpath
)
209 fn
|= SPID_FUNC_MULTI_PATH
;
210 spid_build_cp(cdev
, fn
);
211 ccw_request_start(cdev
);
215 if (cdev
->private->flags
.pgid_unknown
) {
216 /* At least one SPID could be partially done. */
217 pgid_wipeout_start(cdev
);
220 verify_done(cdev
, sch
->vpm
? 0 : -EACCES
);
224 * Process SET PGID request result for a single path.
226 static void spid_callback(struct ccw_device
*cdev
, void *data
, int rc
)
228 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
229 struct ccw_request
*req
= &cdev
->private->req
;
233 sch
->vpm
|= req
->lpm
& sch
->opm
;
236 cdev
->private->flags
.pgid_unknown
= 1;
237 cdev
->private->path_noirq_mask
|= req
->lpm
;
240 cdev
->private->path_notoper_mask
|= req
->lpm
;
243 if (cdev
->private->flags
.mpath
) {
244 /* Try without multipathing. */
245 cdev
->private->flags
.mpath
= 0;
248 /* Try without pathgrouping. */
249 cdev
->private->flags
.pgroup
= 0;
262 verify_done(cdev
, rc
);
265 static void spid_start(struct ccw_device
*cdev
)
267 struct ccw_request
*req
= &cdev
->private->req
;
269 /* Initialize request data. */
270 memset(req
, 0, sizeof(*req
));
271 req
->timeout
= PGID_TIMEOUT
;
272 req
->maxretries
= PGID_RETRIES
;
275 req
->callback
= spid_callback
;
279 static int pgid_is_reset(struct pgid
*p
)
283 for (c
= (char *)p
+ 1; c
< (char *)(p
+ 1); c
++) {
290 static int pgid_cmp(struct pgid
*p1
, struct pgid
*p2
)
292 return memcmp((char *) p1
+ 1, (char *) p2
+ 1,
293 sizeof(struct pgid
) - 1);
297 * Determine pathgroup state from PGID data.
299 static void pgid_analyze(struct ccw_device
*cdev
, struct pgid
**p
,
300 int *mismatch
, u8
*reserved
, u8
*reset
)
302 struct pgid
*pgid
= &cdev
->private->pgid
[0];
303 struct pgid
*first
= NULL
;
310 for (i
= 0, lpm
= 0x80; i
< 8; i
++, pgid
++, lpm
>>= 1) {
311 if ((cdev
->private->pgid_valid_mask
& lpm
) == 0)
313 if (pgid
->inf
.ps
.state2
== SNID_STATE2_RESVD_ELSE
)
315 if (pgid_is_reset(pgid
)) {
323 if (pgid_cmp(pgid
, first
) != 0)
327 first
= &channel_subsystems
[0]->global_pgid
;
331 static u8
pgid_to_donepm(struct ccw_device
*cdev
)
333 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
339 /* Set bits for paths which are already in the target state. */
340 for (i
= 0; i
< 8; i
++) {
342 if ((cdev
->private->pgid_valid_mask
& lpm
) == 0)
344 pgid
= &cdev
->private->pgid
[i
];
345 if (sch
->opm
& lpm
) {
346 if (pgid
->inf
.ps
.state1
!= SNID_STATE1_GROUPED
)
349 if (pgid
->inf
.ps
.state1
!= SNID_STATE1_UNGROUPED
)
352 if (cdev
->private->flags
.mpath
) {
353 if (pgid
->inf
.ps
.state3
!= SNID_STATE3_MULTI_PATH
)
356 if (pgid
->inf
.ps
.state3
!= SNID_STATE3_SINGLE_PATH
)
365 static void pgid_fill(struct ccw_device
*cdev
, struct pgid
*pgid
)
369 for (i
= 0; i
< 8; i
++)
370 memcpy(&cdev
->private->pgid
[i
], pgid
, sizeof(struct pgid
));
374 * Process SENSE PGID data and report result.
376 static void snid_done(struct ccw_device
*cdev
, int rc
)
378 struct ccw_dev_id
*id
= &cdev
->private->dev_id
;
379 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
388 pgid_analyze(cdev
, &pgid
, &mismatch
, &reserved
, &reset
);
389 if (reserved
== cdev
->private->pgid_valid_mask
)
394 donepm
= pgid_to_donepm(cdev
);
395 sch
->vpm
= donepm
& sch
->opm
;
396 cdev
->private->pgid_reset_mask
|= reset
;
397 cdev
->private->pgid_todo_mask
&=
398 ~(donepm
| cdev
->private->path_noirq_mask
);
399 pgid_fill(cdev
, pgid
);
402 CIO_MSG_EVENT(2, "snid: device 0.%x.%04x: rc=%d pvm=%02x vpm=%02x "
403 "todo=%02x mism=%d rsvd=%02x reset=%02x\n", id
->ssid
,
404 id
->devno
, rc
, cdev
->private->pgid_valid_mask
, sch
->vpm
,
405 cdev
->private->pgid_todo_mask
, mismatch
, reserved
, reset
);
408 if (cdev
->private->flags
.pgid_unknown
) {
409 pgid_wipeout_start(cdev
);
412 /* Anything left to do? */
413 if (cdev
->private->pgid_todo_mask
== 0) {
414 verify_done(cdev
, sch
->vpm
== 0 ? -EACCES
: 0);
417 /* Perform path-grouping. */
421 /* Path-grouping not supported. */
422 cdev
->private->flags
.pgroup
= 0;
423 cdev
->private->flags
.mpath
= 0;
427 verify_done(cdev
, rc
);
432 * Create channel program to perform a SENSE PGID on a single path.
434 static void snid_build_cp(struct ccw_device
*cdev
)
436 struct ccw_request
*req
= &cdev
->private->req
;
437 struct ccw1
*cp
= cdev
->private->iccws
;
438 int i
= pathmask_to_pos(req
->lpm
);
440 /* Channel program setup. */
441 cp
->cmd_code
= CCW_CMD_SENSE_PGID
;
442 cp
->cda
= (u32
) (addr_t
) &cdev
->private->pgid
[i
];
443 cp
->count
= sizeof(struct pgid
);
444 cp
->flags
= CCW_FLAG_SLI
;
449 * Perform SENSE PGID on a single path.
451 static void snid_do(struct ccw_device
*cdev
)
453 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
454 struct ccw_request
*req
= &cdev
->private->req
;
457 req
->lpm
= lpm_adjust(req
->lpm
, sch
->schib
.pmcw
.pam
&
458 ~cdev
->private->path_noirq_mask
);
462 ccw_request_start(cdev
);
466 if (cdev
->private->pgid_valid_mask
)
468 else if (cdev
->private->path_noirq_mask
)
472 snid_done(cdev
, ret
);
476 * Process SENSE PGID request result for single path.
478 static void snid_callback(struct ccw_device
*cdev
, void *data
, int rc
)
480 struct ccw_request
*req
= &cdev
->private->req
;
484 cdev
->private->pgid_valid_mask
|= req
->lpm
;
487 cdev
->private->flags
.pgid_unknown
= 1;
488 cdev
->private->path_noirq_mask
|= req
->lpm
;
491 cdev
->private->path_notoper_mask
|= req
->lpm
;
496 /* Continue on the next path. */
506 * Perform path verification.
508 static void verify_start(struct ccw_device
*cdev
)
510 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
511 struct ccw_request
*req
= &cdev
->private->req
;
512 struct ccw_dev_id
*devid
= &cdev
->private->dev_id
;
515 sch
->lpm
= sch
->schib
.pmcw
.pam
;
517 /* Initialize PGID data. */
518 memset(cdev
->private->pgid
, 0, sizeof(cdev
->private->pgid
));
519 cdev
->private->pgid_valid_mask
= 0;
520 cdev
->private->pgid_todo_mask
= sch
->schib
.pmcw
.pam
;
521 cdev
->private->path_notoper_mask
= 0;
523 /* Initialize request data. */
524 memset(req
, 0, sizeof(*req
));
525 req
->timeout
= PGID_TIMEOUT
;
526 req
->maxretries
= PGID_RETRIES
;
529 if (cdev
->private->flags
.pgroup
) {
530 CIO_TRACE_EVENT(4, "snid");
531 CIO_HEX_EVENT(4, devid
, sizeof(*devid
));
532 req
->callback
= snid_callback
;
535 CIO_TRACE_EVENT(4, "nop");
536 CIO_HEX_EVENT(4, devid
, sizeof(*devid
));
537 req
->filter
= nop_filter
;
538 req
->callback
= nop_callback
;
544 * ccw_device_verify_start - perform path verification
547 * Perform an I/O on each available channel path to @cdev to determine which
548 * paths are operational. The resulting path mask is stored in sch->vpm.
549 * If device options specify pathgrouping, establish a pathgroup for the
550 * operational paths. When finished, call ccw_device_verify_done with a
551 * return code specifying the result.
553 void ccw_device_verify_start(struct ccw_device
*cdev
)
555 CIO_TRACE_EVENT(4, "vrfy");
556 CIO_HEX_EVENT(4, &cdev
->private->dev_id
, sizeof(cdev
->private->dev_id
));
558 * Initialize pathgroup and multipath state with target values.
559 * They may change in the course of path verification.
561 cdev
->private->flags
.pgroup
= cdev
->private->options
.pgroup
;
562 cdev
->private->flags
.mpath
= cdev
->private->options
.mpath
;
563 cdev
->private->flags
.doverify
= 0;
564 cdev
->private->path_noirq_mask
= 0;
569 * Process disband SET PGID request result.
571 static void disband_callback(struct ccw_device
*cdev
, void *data
, int rc
)
573 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
574 struct ccw_dev_id
*id
= &cdev
->private->dev_id
;
578 /* Ensure consistent multipathing state at device and channel. */
579 cdev
->private->flags
.mpath
= 0;
580 if (sch
->config
.mp
) {
582 rc
= cio_commit_config(sch
);
585 CIO_MSG_EVENT(0, "disb: device 0.%x.%04x: rc=%d\n", id
->ssid
, id
->devno
,
587 ccw_device_disband_done(cdev
, rc
);
591 * ccw_device_disband_start - disband pathgroup
594 * Execute a SET PGID channel program on @cdev to disband a previously
595 * established pathgroup. When finished, call ccw_device_disband_done with
596 * a return code specifying the result.
598 void ccw_device_disband_start(struct ccw_device
*cdev
)
600 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
601 struct ccw_request
*req
= &cdev
->private->req
;
604 CIO_TRACE_EVENT(4, "disb");
605 CIO_HEX_EVENT(4, &cdev
->private->dev_id
, sizeof(cdev
->private->dev_id
));
607 memset(req
, 0, sizeof(*req
));
608 req
->timeout
= PGID_TIMEOUT
;
609 req
->maxretries
= PGID_RETRIES
;
610 req
->lpm
= sch
->schib
.pmcw
.pam
& sch
->opm
;
612 req
->callback
= disband_callback
;
613 fn
= SPID_FUNC_DISBAND
;
614 if (cdev
->private->flags
.mpath
)
615 fn
|= SPID_FUNC_MULTI_PATH
;
616 spid_build_cp(cdev
, fn
);
617 ccw_request_start(cdev
);
621 struct completion done
;
625 static void stlck_build_cp(struct ccw_device
*cdev
, void *buf1
, void *buf2
)
627 struct ccw_request
*req
= &cdev
->private->req
;
628 struct ccw1
*cp
= cdev
->private->iccws
;
630 cp
[0].cmd_code
= CCW_CMD_STLCK
;
631 cp
[0].cda
= (u32
) (addr_t
) buf1
;
633 cp
[0].flags
= CCW_FLAG_CC
;
634 cp
[1].cmd_code
= CCW_CMD_RELEASE
;
635 cp
[1].cda
= (u32
) (addr_t
) buf2
;
641 static void stlck_callback(struct ccw_device
*cdev
, void *data
, int rc
)
643 struct stlck_data
*sdata
= data
;
646 complete(&sdata
->done
);
650 * ccw_device_stlck_start - perform unconditional release
652 * @data: data pointer to be passed to ccw_device_stlck_done
653 * @buf1: data pointer used in channel program
654 * @buf2: data pointer used in channel program
656 * Execute a channel program on @cdev to release an existing PGID reservation.
658 static void ccw_device_stlck_start(struct ccw_device
*cdev
, void *data
,
659 void *buf1
, void *buf2
)
661 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
662 struct ccw_request
*req
= &cdev
->private->req
;
664 CIO_TRACE_EVENT(4, "stlck");
665 CIO_HEX_EVENT(4, &cdev
->private->dev_id
, sizeof(cdev
->private->dev_id
));
667 memset(req
, 0, sizeof(*req
));
668 req
->timeout
= PGID_TIMEOUT
;
669 req
->maxretries
= PGID_RETRIES
;
670 req
->lpm
= sch
->schib
.pmcw
.pam
& sch
->opm
;
672 req
->callback
= stlck_callback
;
673 stlck_build_cp(cdev
, buf1
, buf2
);
674 ccw_request_start(cdev
);
678 * Perform unconditional reserve + release.
680 int ccw_device_stlck(struct ccw_device
*cdev
)
682 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
683 struct stlck_data data
;
687 /* Check if steal lock operation is valid for this device. */
689 if (!cdev
->private->options
.force
)
692 buffer
= kzalloc(64, GFP_DMA
| GFP_KERNEL
);
695 init_completion(&data
.done
);
697 spin_lock_irq(sch
->lock
);
698 rc
= cio_enable_subchannel(sch
, (u32
) (addr_t
) sch
);
701 /* Perform operation. */
702 cdev
->private->state
= DEV_STATE_STEAL_LOCK
;
703 ccw_device_stlck_start(cdev
, &data
, &buffer
[0], &buffer
[32]);
704 spin_unlock_irq(sch
->lock
);
705 /* Wait for operation to finish. */
706 if (wait_for_completion_interruptible(&data
.done
)) {
708 spin_lock_irq(sch
->lock
);
709 ccw_request_cancel(cdev
);
710 spin_unlock_irq(sch
->lock
);
711 wait_for_completion(&data
.done
);
715 spin_lock_irq(sch
->lock
);
716 cio_disable_subchannel(sch
);
717 cdev
->private->state
= DEV_STATE_BOXED
;
719 spin_unlock_irq(sch
->lock
);