1 // SPDX-License-Identifier: GPL-2.0
3 * CCW device PGID and path verification I/O handling.
5 * Copyright IBM Corp. 2002, 2009
6 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
7 * Martin Schwidefsky <schwidefsky@de.ibm.com>
8 * Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
11 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/bitops.h>
14 #include <linux/types.h>
15 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <asm/ccwdev.h>
21 #include "cio_debug.h"
25 #define PGID_RETRIES 256
26 #define PGID_TIMEOUT (10 * HZ)
28 static void verify_start(struct ccw_device
*cdev
);
31 * Process path verification data and report result.
33 static void verify_done(struct ccw_device
*cdev
, int rc
)
35 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
36 struct ccw_dev_id
*id
= &cdev
->private->dev_id
;
37 int mpath
= cdev
->private->flags
.mpath
;
38 int pgroup
= cdev
->private->flags
.pgroup
;
42 /* Ensure consistent multipathing state at device and channel. */
43 if (sch
->config
.mp
!= mpath
) {
44 sch
->config
.mp
= mpath
;
45 rc
= cio_commit_config(sch
);
48 CIO_MSG_EVENT(2, "vrfy: device 0.%x.%04x: rc=%d pgroup=%d mpath=%d "
49 "vpm=%02x\n", id
->ssid
, id
->devno
, rc
, pgroup
, mpath
,
51 ccw_device_verify_done(cdev
, rc
);
55 * Create channel program to perform a NOOP.
57 static void nop_build_cp(struct ccw_device
*cdev
)
59 struct ccw_request
*req
= &cdev
->private->req
;
60 struct ccw1
*cp
= cdev
->private->dma_area
->iccws
;
62 cp
->cmd_code
= CCW_CMD_NOOP
;
65 cp
->flags
= CCW_FLAG_SLI
;
70 * Perform NOOP on a single path.
72 static void nop_do(struct ccw_device
*cdev
)
74 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
75 struct ccw_request
*req
= &cdev
->private->req
;
77 req
->lpm
= lpm_adjust(req
->lpm
, sch
->schib
.pmcw
.pam
& sch
->opm
&
78 ~cdev
->private->path_noirq_mask
);
82 ccw_request_start(cdev
);
86 verify_done(cdev
, sch
->vpm
? 0 : -EACCES
);
90 * Adjust NOOP I/O status.
92 static enum io_status
nop_filter(struct ccw_device
*cdev
, void *data
,
93 struct irb
*irb
, enum io_status status
)
95 /* Only subchannel status might indicate a path error. */
96 if (status
== IO_STATUS_ERROR
&& irb
->scsw
.cmd
.cstat
== 0)
102 * Process NOOP request result for a single path.
104 static void nop_callback(struct ccw_device
*cdev
, void *data
, int rc
)
106 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
107 struct ccw_request
*req
= &cdev
->private->req
;
111 sch
->vpm
|= req
->lpm
;
114 cdev
->private->path_noirq_mask
|= req
->lpm
;
117 cdev
->private->path_notoper_mask
|= req
->lpm
;
122 /* Continue on the next path. */
128 verify_done(cdev
, rc
);
132 * Create channel program to perform SET PGID on a single path.
134 static void spid_build_cp(struct ccw_device
*cdev
, u8 fn
)
136 struct ccw_request
*req
= &cdev
->private->req
;
137 struct ccw1
*cp
= cdev
->private->dma_area
->iccws
;
138 int i
= pathmask_to_pos(req
->lpm
);
139 struct pgid
*pgid
= &cdev
->private->dma_area
->pgid
[i
];
142 cp
->cmd_code
= CCW_CMD_SET_PGID
;
143 cp
->cda
= (u32
) (addr_t
) pgid
;
144 cp
->count
= sizeof(*pgid
);
145 cp
->flags
= CCW_FLAG_SLI
;
149 static void pgid_wipeout_callback(struct ccw_device
*cdev
, void *data
, int rc
)
152 /* We don't know the path groups' state. Abort. */
153 verify_done(cdev
, rc
);
157 * Path groups have been reset. Restart path verification but
158 * leave paths in path_noirq_mask out.
160 cdev
->private->flags
.pgid_unknown
= 0;
165 * Reset pathgroups and restart path verification, leave unusable paths out.
167 static void pgid_wipeout_start(struct ccw_device
*cdev
)
169 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
170 struct ccw_dev_id
*id
= &cdev
->private->dev_id
;
171 struct ccw_request
*req
= &cdev
->private->req
;
174 CIO_MSG_EVENT(2, "wipe: device 0.%x.%04x: pvm=%02x nim=%02x\n",
175 id
->ssid
, id
->devno
, cdev
->private->pgid_valid_mask
,
176 cdev
->private->path_noirq_mask
);
178 /* Initialize request data. */
179 memset(req
, 0, sizeof(*req
));
180 req
->timeout
= PGID_TIMEOUT
;
181 req
->maxretries
= PGID_RETRIES
;
182 req
->lpm
= sch
->schib
.pmcw
.pam
;
183 req
->callback
= pgid_wipeout_callback
;
184 fn
= SPID_FUNC_DISBAND
;
185 if (cdev
->private->flags
.mpath
)
186 fn
|= SPID_FUNC_MULTI_PATH
;
187 spid_build_cp(cdev
, fn
);
188 ccw_request_start(cdev
);
192 * Perform establish/resign SET PGID on a single path.
194 static void spid_do(struct ccw_device
*cdev
)
196 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
197 struct ccw_request
*req
= &cdev
->private->req
;
200 /* Use next available path that is not already in correct state. */
201 req
->lpm
= lpm_adjust(req
->lpm
, cdev
->private->pgid_todo_mask
);
204 /* Channel program setup. */
205 if (req
->lpm
& sch
->opm
)
206 fn
= SPID_FUNC_ESTABLISH
;
208 fn
= SPID_FUNC_RESIGN
;
209 if (cdev
->private->flags
.mpath
)
210 fn
|= SPID_FUNC_MULTI_PATH
;
211 spid_build_cp(cdev
, fn
);
212 ccw_request_start(cdev
);
216 if (cdev
->private->flags
.pgid_unknown
) {
217 /* At least one SPID could be partially done. */
218 pgid_wipeout_start(cdev
);
221 verify_done(cdev
, sch
->vpm
? 0 : -EACCES
);
225 * Process SET PGID request result for a single path.
227 static void spid_callback(struct ccw_device
*cdev
, void *data
, int rc
)
229 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
230 struct ccw_request
*req
= &cdev
->private->req
;
234 sch
->vpm
|= req
->lpm
& sch
->opm
;
237 cdev
->private->flags
.pgid_unknown
= 1;
238 cdev
->private->path_noirq_mask
|= req
->lpm
;
241 cdev
->private->path_notoper_mask
|= req
->lpm
;
244 if (cdev
->private->flags
.mpath
) {
245 /* Try without multipathing. */
246 cdev
->private->flags
.mpath
= 0;
249 /* Try without pathgrouping. */
250 cdev
->private->flags
.pgroup
= 0;
263 verify_done(cdev
, rc
);
266 static void spid_start(struct ccw_device
*cdev
)
268 struct ccw_request
*req
= &cdev
->private->req
;
270 /* Initialize request data. */
271 memset(req
, 0, sizeof(*req
));
272 req
->timeout
= PGID_TIMEOUT
;
273 req
->maxretries
= PGID_RETRIES
;
276 req
->callback
= spid_callback
;
280 static int pgid_is_reset(struct pgid
*p
)
284 for (c
= (char *)p
+ 1; c
< (char *)(p
+ 1); c
++) {
291 static int pgid_cmp(struct pgid
*p1
, struct pgid
*p2
)
293 return memcmp((char *) p1
+ 1, (char *) p2
+ 1,
294 sizeof(struct pgid
) - 1);
298 * Determine pathgroup state from PGID data.
300 static void pgid_analyze(struct ccw_device
*cdev
, struct pgid
**p
,
301 int *mismatch
, u8
*reserved
, u8
*reset
)
303 struct pgid
*pgid
= &cdev
->private->dma_area
->pgid
[0];
304 struct pgid
*first
= NULL
;
311 for (i
= 0, lpm
= 0x80; i
< 8; i
++, pgid
++, lpm
>>= 1) {
312 if ((cdev
->private->pgid_valid_mask
& lpm
) == 0)
314 if (pgid
->inf
.ps
.state2
== SNID_STATE2_RESVD_ELSE
)
316 if (pgid_is_reset(pgid
)) {
324 if (pgid_cmp(pgid
, first
) != 0)
328 first
= &channel_subsystems
[0]->global_pgid
;
332 static u8
pgid_to_donepm(struct ccw_device
*cdev
)
334 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
340 /* Set bits for paths which are already in the target state. */
341 for (i
= 0; i
< 8; i
++) {
343 if ((cdev
->private->pgid_valid_mask
& lpm
) == 0)
345 pgid
= &cdev
->private->dma_area
->pgid
[i
];
346 if (sch
->opm
& lpm
) {
347 if (pgid
->inf
.ps
.state1
!= SNID_STATE1_GROUPED
)
350 if (pgid
->inf
.ps
.state1
!= SNID_STATE1_UNGROUPED
)
353 if (cdev
->private->flags
.mpath
) {
354 if (pgid
->inf
.ps
.state3
!= SNID_STATE3_MULTI_PATH
)
357 if (pgid
->inf
.ps
.state3
!= SNID_STATE3_SINGLE_PATH
)
366 static void pgid_fill(struct ccw_device
*cdev
, struct pgid
*pgid
)
370 for (i
= 0; i
< 8; i
++)
371 memcpy(&cdev
->private->dma_area
->pgid
[i
], pgid
,
372 sizeof(struct pgid
));
376 * Process SENSE PGID data and report result.
378 static void snid_done(struct ccw_device
*cdev
, int rc
)
380 struct ccw_dev_id
*id
= &cdev
->private->dev_id
;
381 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
390 pgid_analyze(cdev
, &pgid
, &mismatch
, &reserved
, &reset
);
391 if (reserved
== cdev
->private->pgid_valid_mask
)
396 donepm
= pgid_to_donepm(cdev
);
397 sch
->vpm
= donepm
& sch
->opm
;
398 cdev
->private->pgid_reset_mask
|= reset
;
399 cdev
->private->pgid_todo_mask
&=
400 ~(donepm
| cdev
->private->path_noirq_mask
);
401 pgid_fill(cdev
, pgid
);
404 CIO_MSG_EVENT(2, "snid: device 0.%x.%04x: rc=%d pvm=%02x vpm=%02x "
405 "todo=%02x mism=%d rsvd=%02x reset=%02x\n", id
->ssid
,
406 id
->devno
, rc
, cdev
->private->pgid_valid_mask
, sch
->vpm
,
407 cdev
->private->pgid_todo_mask
, mismatch
, reserved
, reset
);
410 if (cdev
->private->flags
.pgid_unknown
) {
411 pgid_wipeout_start(cdev
);
414 /* Anything left to do? */
415 if (cdev
->private->pgid_todo_mask
== 0) {
416 verify_done(cdev
, sch
->vpm
== 0 ? -EACCES
: 0);
419 /* Perform path-grouping. */
423 /* Path-grouping not supported. */
424 cdev
->private->flags
.pgroup
= 0;
425 cdev
->private->flags
.mpath
= 0;
429 verify_done(cdev
, rc
);
434 * Create channel program to perform a SENSE PGID on a single path.
436 static void snid_build_cp(struct ccw_device
*cdev
)
438 struct ccw_request
*req
= &cdev
->private->req
;
439 struct ccw1
*cp
= cdev
->private->dma_area
->iccws
;
440 int i
= pathmask_to_pos(req
->lpm
);
442 /* Channel program setup. */
443 cp
->cmd_code
= CCW_CMD_SENSE_PGID
;
444 cp
->cda
= (u32
) (addr_t
) &cdev
->private->dma_area
->pgid
[i
];
445 cp
->count
= sizeof(struct pgid
);
446 cp
->flags
= CCW_FLAG_SLI
;
451 * Perform SENSE PGID on a single path.
453 static void snid_do(struct ccw_device
*cdev
)
455 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
456 struct ccw_request
*req
= &cdev
->private->req
;
459 req
->lpm
= lpm_adjust(req
->lpm
, sch
->schib
.pmcw
.pam
&
460 ~cdev
->private->path_noirq_mask
);
464 ccw_request_start(cdev
);
468 if (cdev
->private->pgid_valid_mask
)
470 else if (cdev
->private->path_noirq_mask
)
474 snid_done(cdev
, ret
);
478 * Process SENSE PGID request result for single path.
480 static void snid_callback(struct ccw_device
*cdev
, void *data
, int rc
)
482 struct ccw_request
*req
= &cdev
->private->req
;
486 cdev
->private->pgid_valid_mask
|= req
->lpm
;
489 cdev
->private->flags
.pgid_unknown
= 1;
490 cdev
->private->path_noirq_mask
|= req
->lpm
;
493 cdev
->private->path_notoper_mask
|= req
->lpm
;
498 /* Continue on the next path. */
508 * Perform path verification.
510 static void verify_start(struct ccw_device
*cdev
)
512 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
513 struct ccw_request
*req
= &cdev
->private->req
;
514 struct ccw_dev_id
*devid
= &cdev
->private->dev_id
;
517 sch
->lpm
= sch
->schib
.pmcw
.pam
;
519 /* Initialize PGID data. */
520 memset(cdev
->private->dma_area
->pgid
, 0,
521 sizeof(cdev
->private->dma_area
->pgid
));
522 cdev
->private->pgid_valid_mask
= 0;
523 cdev
->private->pgid_todo_mask
= sch
->schib
.pmcw
.pam
;
524 cdev
->private->path_notoper_mask
= 0;
526 /* Initialize request data. */
527 memset(req
, 0, sizeof(*req
));
528 req
->timeout
= PGID_TIMEOUT
;
529 req
->maxretries
= PGID_RETRIES
;
532 if (cdev
->private->flags
.pgroup
) {
533 CIO_TRACE_EVENT(4, "snid");
534 CIO_HEX_EVENT(4, devid
, sizeof(*devid
));
535 req
->callback
= snid_callback
;
538 CIO_TRACE_EVENT(4, "nop");
539 CIO_HEX_EVENT(4, devid
, sizeof(*devid
));
540 req
->filter
= nop_filter
;
541 req
->callback
= nop_callback
;
547 * ccw_device_verify_start - perform path verification
550 * Perform an I/O on each available channel path to @cdev to determine which
551 * paths are operational. The resulting path mask is stored in sch->vpm.
552 * If device options specify pathgrouping, establish a pathgroup for the
553 * operational paths. When finished, call ccw_device_verify_done with a
554 * return code specifying the result.
556 void ccw_device_verify_start(struct ccw_device
*cdev
)
558 CIO_TRACE_EVENT(4, "vrfy");
559 CIO_HEX_EVENT(4, &cdev
->private->dev_id
, sizeof(cdev
->private->dev_id
));
561 * Initialize pathgroup and multipath state with target values.
562 * They may change in the course of path verification.
564 cdev
->private->flags
.pgroup
= cdev
->private->options
.pgroup
;
565 cdev
->private->flags
.mpath
= cdev
->private->options
.mpath
;
566 cdev
->private->flags
.doverify
= 0;
567 cdev
->private->path_noirq_mask
= 0;
572 * Process disband SET PGID request result.
574 static void disband_callback(struct ccw_device
*cdev
, void *data
, int rc
)
576 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
577 struct ccw_dev_id
*id
= &cdev
->private->dev_id
;
581 /* Ensure consistent multipathing state at device and channel. */
582 cdev
->private->flags
.mpath
= 0;
583 if (sch
->config
.mp
) {
585 rc
= cio_commit_config(sch
);
588 CIO_MSG_EVENT(0, "disb: device 0.%x.%04x: rc=%d\n", id
->ssid
, id
->devno
,
590 ccw_device_disband_done(cdev
, rc
);
594 * ccw_device_disband_start - disband pathgroup
597 * Execute a SET PGID channel program on @cdev to disband a previously
598 * established pathgroup. When finished, call ccw_device_disband_done with
599 * a return code specifying the result.
601 void ccw_device_disband_start(struct ccw_device
*cdev
)
603 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
604 struct ccw_request
*req
= &cdev
->private->req
;
607 CIO_TRACE_EVENT(4, "disb");
608 CIO_HEX_EVENT(4, &cdev
->private->dev_id
, sizeof(cdev
->private->dev_id
));
610 memset(req
, 0, sizeof(*req
));
611 req
->timeout
= PGID_TIMEOUT
;
612 req
->maxretries
= PGID_RETRIES
;
613 req
->lpm
= sch
->schib
.pmcw
.pam
& sch
->opm
;
615 req
->callback
= disband_callback
;
616 fn
= SPID_FUNC_DISBAND
;
617 if (cdev
->private->flags
.mpath
)
618 fn
|= SPID_FUNC_MULTI_PATH
;
619 spid_build_cp(cdev
, fn
);
620 ccw_request_start(cdev
);
624 struct completion done
;
628 static void stlck_build_cp(struct ccw_device
*cdev
, void *buf1
, void *buf2
)
630 struct ccw_request
*req
= &cdev
->private->req
;
631 struct ccw1
*cp
= cdev
->private->dma_area
->iccws
;
633 cp
[0].cmd_code
= CCW_CMD_STLCK
;
634 cp
[0].cda
= (u32
) (addr_t
) buf1
;
636 cp
[0].flags
= CCW_FLAG_CC
;
637 cp
[1].cmd_code
= CCW_CMD_RELEASE
;
638 cp
[1].cda
= (u32
) (addr_t
) buf2
;
644 static void stlck_callback(struct ccw_device
*cdev
, void *data
, int rc
)
646 struct stlck_data
*sdata
= data
;
649 complete(&sdata
->done
);
653 * ccw_device_stlck_start - perform unconditional release
655 * @data: data pointer to be passed to ccw_device_stlck_done
656 * @buf1: data pointer used in channel program
657 * @buf2: data pointer used in channel program
659 * Execute a channel program on @cdev to release an existing PGID reservation.
661 static void ccw_device_stlck_start(struct ccw_device
*cdev
, void *data
,
662 void *buf1
, void *buf2
)
664 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
665 struct ccw_request
*req
= &cdev
->private->req
;
667 CIO_TRACE_EVENT(4, "stlck");
668 CIO_HEX_EVENT(4, &cdev
->private->dev_id
, sizeof(cdev
->private->dev_id
));
670 memset(req
, 0, sizeof(*req
));
671 req
->timeout
= PGID_TIMEOUT
;
672 req
->maxretries
= PGID_RETRIES
;
673 req
->lpm
= sch
->schib
.pmcw
.pam
& sch
->opm
;
675 req
->callback
= stlck_callback
;
676 stlck_build_cp(cdev
, buf1
, buf2
);
677 ccw_request_start(cdev
);
681 * Perform unconditional reserve + release.
683 int ccw_device_stlck(struct ccw_device
*cdev
)
685 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
686 struct stlck_data data
;
690 /* Check if steal lock operation is valid for this device. */
692 if (!cdev
->private->options
.force
)
695 buffer
= kzalloc(64, GFP_DMA
| GFP_KERNEL
);
698 init_completion(&data
.done
);
700 spin_lock_irq(sch
->lock
);
701 rc
= cio_enable_subchannel(sch
, (u32
) (addr_t
) sch
);
704 /* Perform operation. */
705 cdev
->private->state
= DEV_STATE_STEAL_LOCK
;
706 ccw_device_stlck_start(cdev
, &data
, &buffer
[0], &buffer
[32]);
707 spin_unlock_irq(sch
->lock
);
708 /* Wait for operation to finish. */
709 if (wait_for_completion_interruptible(&data
.done
)) {
711 spin_lock_irq(sch
->lock
);
712 ccw_request_cancel(cdev
);
713 spin_unlock_irq(sch
->lock
);
714 wait_for_completion(&data
.done
);
718 spin_lock_irq(sch
->lock
);
719 cio_disable_subchannel(sch
);
720 cdev
->private->state
= DEV_STATE_BOXED
;
722 spin_unlock_irq(sch
->lock
);