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>
18 #include <asm/ccwdev.h>
22 #include "cio_debug.h"
26 #define PGID_RETRIES 256
27 #define PGID_TIMEOUT (10 * HZ)
29 static void verify_start(struct ccw_device
*cdev
);
32 * Process path verification data and report result.
34 static void verify_done(struct ccw_device
*cdev
, int rc
)
36 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
37 struct ccw_dev_id
*id
= &cdev
->private->dev_id
;
38 int mpath
= cdev
->private->flags
.mpath
;
39 int pgroup
= cdev
->private->flags
.pgroup
;
43 /* Ensure consistent multipathing state at device and channel. */
44 if (sch
->config
.mp
!= mpath
) {
45 sch
->config
.mp
= mpath
;
46 rc
= cio_commit_config(sch
);
49 CIO_MSG_EVENT(2, "vrfy: device 0.%x.%04x: rc=%d pgroup=%d mpath=%d "
50 "vpm=%02x\n", id
->ssid
, id
->devno
, rc
, pgroup
, mpath
,
52 ccw_device_verify_done(cdev
, rc
);
56 * Create channel program to perform a NOOP.
58 static void nop_build_cp(struct ccw_device
*cdev
)
60 struct ccw_request
*req
= &cdev
->private->req
;
61 struct ccw1
*cp
= cdev
->private->dma_area
->iccws
;
63 cp
->cmd_code
= CCW_CMD_NOOP
;
66 cp
->flags
= CCW_FLAG_SLI
;
71 * Perform NOOP on a single path.
73 static void nop_do(struct ccw_device
*cdev
)
75 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
76 struct ccw_request
*req
= &cdev
->private->req
;
78 req
->lpm
= lpm_adjust(req
->lpm
, sch
->schib
.pmcw
.pam
& sch
->opm
&
79 ~cdev
->private->path_noirq_mask
);
83 ccw_request_start(cdev
);
87 verify_done(cdev
, sch
->vpm
? 0 : -EACCES
);
91 * Adjust NOOP I/O status.
93 static enum io_status
nop_filter(struct ccw_device
*cdev
, void *data
,
94 struct irb
*irb
, enum io_status status
)
96 /* Only subchannel status might indicate a path error. */
97 if (status
== IO_STATUS_ERROR
&& irb
->scsw
.cmd
.cstat
== 0)
103 * Process NOOP request result for a single path.
105 static void nop_callback(struct ccw_device
*cdev
, void *data
, int rc
)
107 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
108 struct ccw_request
*req
= &cdev
->private->req
;
112 sch
->vpm
|= req
->lpm
;
115 cdev
->private->path_noirq_mask
|= req
->lpm
;
118 cdev
->private->path_notoper_mask
|= req
->lpm
;
123 /* Continue on the next path. */
129 verify_done(cdev
, rc
);
133 * Create channel program to perform SET PGID on a single path.
135 static void spid_build_cp(struct ccw_device
*cdev
, u8 fn
)
137 struct ccw_request
*req
= &cdev
->private->req
;
138 struct ccw1
*cp
= cdev
->private->dma_area
->iccws
;
139 int i
= pathmask_to_pos(req
->lpm
);
140 struct pgid
*pgid
= &cdev
->private->dma_area
->pgid
[i
];
143 cp
->cmd_code
= CCW_CMD_SET_PGID
;
144 cp
->cda
= virt_to_dma32(pgid
);
145 cp
->count
= sizeof(*pgid
);
146 cp
->flags
= CCW_FLAG_SLI
;
150 static void pgid_wipeout_callback(struct ccw_device
*cdev
, void *data
, int rc
)
153 /* We don't know the path groups' state. Abort. */
154 verify_done(cdev
, rc
);
158 * Path groups have been reset. Restart path verification but
159 * leave paths in path_noirq_mask out.
161 cdev
->private->flags
.pgid_unknown
= 0;
166 * Reset pathgroups and restart path verification, leave unusable paths out.
168 static void pgid_wipeout_start(struct ccw_device
*cdev
)
170 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
171 struct ccw_dev_id
*id
= &cdev
->private->dev_id
;
172 struct ccw_request
*req
= &cdev
->private->req
;
175 CIO_MSG_EVENT(2, "wipe: device 0.%x.%04x: pvm=%02x nim=%02x\n",
176 id
->ssid
, id
->devno
, cdev
->private->pgid_valid_mask
,
177 cdev
->private->path_noirq_mask
);
179 /* Initialize request data. */
180 memset(req
, 0, sizeof(*req
));
181 req
->timeout
= PGID_TIMEOUT
;
182 req
->maxretries
= PGID_RETRIES
;
183 req
->lpm
= sch
->schib
.pmcw
.pam
;
184 req
->callback
= pgid_wipeout_callback
;
185 fn
= SPID_FUNC_DISBAND
;
186 if (cdev
->private->flags
.mpath
)
187 fn
|= SPID_FUNC_MULTI_PATH
;
188 spid_build_cp(cdev
, fn
);
189 ccw_request_start(cdev
);
193 * Perform establish/resign SET PGID on a single path.
195 static void spid_do(struct ccw_device
*cdev
)
197 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
198 struct ccw_request
*req
= &cdev
->private->req
;
201 /* Use next available path that is not already in correct state. */
202 req
->lpm
= lpm_adjust(req
->lpm
, cdev
->private->pgid_todo_mask
);
205 /* Channel program setup. */
206 if (req
->lpm
& sch
->opm
)
207 fn
= SPID_FUNC_ESTABLISH
;
209 fn
= SPID_FUNC_RESIGN
;
210 if (cdev
->private->flags
.mpath
)
211 fn
|= SPID_FUNC_MULTI_PATH
;
212 spid_build_cp(cdev
, fn
);
213 ccw_request_start(cdev
);
217 if (cdev
->private->flags
.pgid_unknown
) {
218 /* At least one SPID could be partially done. */
219 pgid_wipeout_start(cdev
);
222 verify_done(cdev
, sch
->vpm
? 0 : -EACCES
);
226 * Process SET PGID request result for a single path.
228 static void spid_callback(struct ccw_device
*cdev
, void *data
, int rc
)
230 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
231 struct ccw_request
*req
= &cdev
->private->req
;
235 sch
->vpm
|= req
->lpm
& sch
->opm
;
238 cdev
->private->flags
.pgid_unknown
= 1;
239 cdev
->private->path_noirq_mask
|= req
->lpm
;
242 cdev
->private->path_notoper_mask
|= req
->lpm
;
245 if (cdev
->private->flags
.mpath
) {
246 /* Try without multipathing. */
247 cdev
->private->flags
.mpath
= 0;
250 /* Try without pathgrouping. */
251 cdev
->private->flags
.pgroup
= 0;
264 verify_done(cdev
, rc
);
267 static void spid_start(struct ccw_device
*cdev
)
269 struct ccw_request
*req
= &cdev
->private->req
;
271 /* Initialize request data. */
272 memset(req
, 0, sizeof(*req
));
273 req
->timeout
= PGID_TIMEOUT
;
274 req
->maxretries
= PGID_RETRIES
;
277 req
->callback
= spid_callback
;
281 static int pgid_is_reset(struct pgid
*p
)
285 for (c
= (char *)p
+ 1; c
< (char *)(p
+ 1); c
++) {
292 static int pgid_cmp(struct pgid
*p1
, struct pgid
*p2
)
294 return memcmp((char *) p1
+ 1, (char *) p2
+ 1,
295 sizeof(struct pgid
) - 1);
299 * Determine pathgroup state from PGID data.
301 static void pgid_analyze(struct ccw_device
*cdev
, struct pgid
**p
,
302 int *mismatch
, u8
*reserved
, u8
*reset
)
304 struct pgid
*pgid
= &cdev
->private->dma_area
->pgid
[0];
305 struct pgid
*first
= NULL
;
312 for (i
= 0, lpm
= 0x80; i
< 8; i
++, pgid
++, lpm
>>= 1) {
313 if ((cdev
->private->pgid_valid_mask
& lpm
) == 0)
315 if (pgid
->inf
.ps
.state2
== SNID_STATE2_RESVD_ELSE
)
317 if (pgid_is_reset(pgid
)) {
325 if (pgid_cmp(pgid
, first
) != 0)
329 first
= &channel_subsystems
[0]->global_pgid
;
333 static u8
pgid_to_donepm(struct ccw_device
*cdev
)
335 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
341 /* Set bits for paths which are already in the target state. */
342 for (i
= 0; i
< 8; i
++) {
344 if ((cdev
->private->pgid_valid_mask
& lpm
) == 0)
346 pgid
= &cdev
->private->dma_area
->pgid
[i
];
347 if (sch
->opm
& lpm
) {
348 if (pgid
->inf
.ps
.state1
!= SNID_STATE1_GROUPED
)
351 if (pgid
->inf
.ps
.state1
!= SNID_STATE1_UNGROUPED
)
354 if (cdev
->private->flags
.mpath
) {
355 if (pgid
->inf
.ps
.state3
!= SNID_STATE3_MULTI_PATH
)
358 if (pgid
->inf
.ps
.state3
!= SNID_STATE3_SINGLE_PATH
)
367 static void pgid_fill(struct ccw_device
*cdev
, struct pgid
*pgid
)
371 for (i
= 0; i
< 8; i
++)
372 memcpy(&cdev
->private->dma_area
->pgid
[i
], pgid
,
373 sizeof(struct pgid
));
377 * Process SENSE PGID data and report result.
379 static void snid_done(struct ccw_device
*cdev
, int rc
)
381 struct ccw_dev_id
*id
= &cdev
->private->dev_id
;
382 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
391 pgid_analyze(cdev
, &pgid
, &mismatch
, &reserved
, &reset
);
392 if (reserved
== cdev
->private->pgid_valid_mask
)
397 donepm
= pgid_to_donepm(cdev
);
398 sch
->vpm
= donepm
& sch
->opm
;
399 cdev
->private->pgid_reset_mask
|= reset
;
400 cdev
->private->pgid_todo_mask
&=
401 ~(donepm
| cdev
->private->path_noirq_mask
);
402 pgid_fill(cdev
, pgid
);
405 CIO_MSG_EVENT(2, "snid: device 0.%x.%04x: rc=%d pvm=%02x vpm=%02x "
406 "todo=%02x mism=%d rsvd=%02x reset=%02x\n", id
->ssid
,
407 id
->devno
, rc
, cdev
->private->pgid_valid_mask
, sch
->vpm
,
408 cdev
->private->pgid_todo_mask
, mismatch
, reserved
, reset
);
411 if (cdev
->private->flags
.pgid_unknown
) {
412 pgid_wipeout_start(cdev
);
415 /* Anything left to do? */
416 if (cdev
->private->pgid_todo_mask
== 0) {
417 verify_done(cdev
, sch
->vpm
== 0 ? -EACCES
: 0);
420 /* Perform path-grouping. */
424 /* Path-grouping not supported. */
425 cdev
->private->flags
.pgroup
= 0;
426 cdev
->private->flags
.mpath
= 0;
430 verify_done(cdev
, rc
);
435 * Create channel program to perform a SENSE PGID on a single path.
437 static void snid_build_cp(struct ccw_device
*cdev
)
439 struct ccw_request
*req
= &cdev
->private->req
;
440 struct ccw1
*cp
= cdev
->private->dma_area
->iccws
;
441 int i
= pathmask_to_pos(req
->lpm
);
443 /* Channel program setup. */
444 cp
->cmd_code
= CCW_CMD_SENSE_PGID
;
445 cp
->cda
= virt_to_dma32(&cdev
->private->dma_area
->pgid
[i
]);
446 cp
->count
= sizeof(struct pgid
);
447 cp
->flags
= CCW_FLAG_SLI
;
452 * Perform SENSE PGID on a single path.
454 static void snid_do(struct ccw_device
*cdev
)
456 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
457 struct ccw_request
*req
= &cdev
->private->req
;
460 req
->lpm
= lpm_adjust(req
->lpm
, sch
->schib
.pmcw
.pam
&
461 ~cdev
->private->path_noirq_mask
);
465 ccw_request_start(cdev
);
469 if (cdev
->private->pgid_valid_mask
)
471 else if (cdev
->private->path_noirq_mask
)
475 snid_done(cdev
, ret
);
479 * Process SENSE PGID request result for single path.
481 static void snid_callback(struct ccw_device
*cdev
, void *data
, int rc
)
483 struct ccw_request
*req
= &cdev
->private->req
;
487 cdev
->private->pgid_valid_mask
|= req
->lpm
;
490 cdev
->private->flags
.pgid_unknown
= 1;
491 cdev
->private->path_noirq_mask
|= req
->lpm
;
494 cdev
->private->path_notoper_mask
|= req
->lpm
;
499 /* Continue on the next path. */
509 * Perform path verification.
511 static void verify_start(struct ccw_device
*cdev
)
513 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
514 struct ccw_request
*req
= &cdev
->private->req
;
515 struct ccw_dev_id
*devid
= &cdev
->private->dev_id
;
518 sch
->lpm
= sch
->schib
.pmcw
.pam
;
520 /* Initialize PGID data. */
521 memset(cdev
->private->dma_area
->pgid
, 0,
522 sizeof(cdev
->private->dma_area
->pgid
));
523 cdev
->private->pgid_valid_mask
= 0;
524 cdev
->private->pgid_todo_mask
= sch
->schib
.pmcw
.pam
;
525 cdev
->private->path_notoper_mask
= 0;
527 /* Initialize request data. */
528 memset(req
, 0, sizeof(*req
));
529 req
->timeout
= PGID_TIMEOUT
;
530 req
->maxretries
= PGID_RETRIES
;
533 if (cdev
->private->flags
.pgroup
) {
534 CIO_TRACE_EVENT(4, "snid");
535 CIO_HEX_EVENT(4, devid
, sizeof(*devid
));
536 req
->callback
= snid_callback
;
539 CIO_TRACE_EVENT(4, "nop");
540 CIO_HEX_EVENT(4, devid
, sizeof(*devid
));
541 req
->filter
= nop_filter
;
542 req
->callback
= nop_callback
;
548 * ccw_device_verify_start - perform path verification
551 * Perform an I/O on each available channel path to @cdev to determine which
552 * paths are operational. The resulting path mask is stored in sch->vpm.
553 * If device options specify pathgrouping, establish a pathgroup for the
554 * operational paths. When finished, call ccw_device_verify_done with a
555 * return code specifying the result.
557 void ccw_device_verify_start(struct ccw_device
*cdev
)
559 CIO_TRACE_EVENT(4, "vrfy");
560 CIO_HEX_EVENT(4, &cdev
->private->dev_id
, sizeof(cdev
->private->dev_id
));
562 * Initialize pathgroup and multipath state with target values.
563 * They may change in the course of path verification.
565 cdev
->private->flags
.pgroup
= cdev
->private->options
.pgroup
;
566 cdev
->private->flags
.mpath
= cdev
->private->options
.mpath
;
567 cdev
->private->flags
.doverify
= 0;
568 cdev
->private->path_noirq_mask
= 0;
573 * Process disband SET PGID request result.
575 static void disband_callback(struct ccw_device
*cdev
, void *data
, int rc
)
577 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
578 struct ccw_dev_id
*id
= &cdev
->private->dev_id
;
582 /* Ensure consistent multipathing state at device and channel. */
583 cdev
->private->flags
.mpath
= 0;
584 if (sch
->config
.mp
) {
586 rc
= cio_commit_config(sch
);
589 CIO_MSG_EVENT(0, "disb: device 0.%x.%04x: rc=%d\n", id
->ssid
, id
->devno
,
591 ccw_device_disband_done(cdev
, rc
);
595 * ccw_device_disband_start - disband pathgroup
598 * Execute a SET PGID channel program on @cdev to disband a previously
599 * established pathgroup. When finished, call ccw_device_disband_done with
600 * a return code specifying the result.
602 void ccw_device_disband_start(struct ccw_device
*cdev
)
604 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
605 struct ccw_request
*req
= &cdev
->private->req
;
608 CIO_TRACE_EVENT(4, "disb");
609 CIO_HEX_EVENT(4, &cdev
->private->dev_id
, sizeof(cdev
->private->dev_id
));
611 memset(req
, 0, sizeof(*req
));
612 req
->timeout
= PGID_TIMEOUT
;
613 req
->maxretries
= PGID_RETRIES
;
614 req
->lpm
= sch
->schib
.pmcw
.pam
& sch
->opm
;
616 req
->callback
= disband_callback
;
617 fn
= SPID_FUNC_DISBAND
;
618 if (cdev
->private->flags
.mpath
)
619 fn
|= SPID_FUNC_MULTI_PATH
;
620 spid_build_cp(cdev
, fn
);
621 ccw_request_start(cdev
);
625 struct completion done
;
629 static void stlck_build_cp(struct ccw_device
*cdev
, void *buf1
, void *buf2
)
631 struct ccw_request
*req
= &cdev
->private->req
;
632 struct ccw1
*cp
= cdev
->private->dma_area
->iccws
;
634 cp
[0].cmd_code
= CCW_CMD_STLCK
;
635 cp
[0].cda
= virt_to_dma32(buf1
);
637 cp
[0].flags
= CCW_FLAG_CC
;
638 cp
[1].cmd_code
= CCW_CMD_RELEASE
;
639 cp
[1].cda
= virt_to_dma32(buf2
);
645 static void stlck_callback(struct ccw_device
*cdev
, void *data
, int rc
)
647 struct stlck_data
*sdata
= data
;
650 complete(&sdata
->done
);
654 * ccw_device_stlck_start - perform unconditional release
656 * @data: data pointer to be passed to ccw_device_stlck_done
657 * @buf1: data pointer used in channel program
658 * @buf2: data pointer used in channel program
660 * Execute a channel program on @cdev to release an existing PGID reservation.
662 static void ccw_device_stlck_start(struct ccw_device
*cdev
, void *data
,
663 void *buf1
, void *buf2
)
665 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
666 struct ccw_request
*req
= &cdev
->private->req
;
668 CIO_TRACE_EVENT(4, "stlck");
669 CIO_HEX_EVENT(4, &cdev
->private->dev_id
, sizeof(cdev
->private->dev_id
));
671 memset(req
, 0, sizeof(*req
));
672 req
->timeout
= PGID_TIMEOUT
;
673 req
->maxretries
= PGID_RETRIES
;
674 req
->lpm
= sch
->schib
.pmcw
.pam
& sch
->opm
;
676 req
->callback
= stlck_callback
;
677 stlck_build_cp(cdev
, buf1
, buf2
);
678 ccw_request_start(cdev
);
682 * Perform unconditional reserve + release.
684 int ccw_device_stlck(struct ccw_device
*cdev
)
686 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
687 struct stlck_data data
;
691 /* Check if steal lock operation is valid for this device. */
693 if (!cdev
->private->options
.force
)
696 buffer
= kzalloc(64, GFP_DMA
| GFP_KERNEL
);
699 init_completion(&data
.done
);
701 spin_lock_irq(&sch
->lock
);
702 rc
= cio_enable_subchannel(sch
, (u32
)virt_to_phys(sch
));
705 /* Perform operation. */
706 cdev
->private->state
= DEV_STATE_STEAL_LOCK
;
707 ccw_device_stlck_start(cdev
, &data
, &buffer
[0], &buffer
[32]);
708 spin_unlock_irq(&sch
->lock
);
709 /* Wait for operation to finish. */
710 if (wait_for_completion_interruptible(&data
.done
)) {
712 spin_lock_irq(&sch
->lock
);
713 ccw_request_cancel(cdev
);
714 spin_unlock_irq(&sch
->lock
);
715 wait_for_completion(&data
.done
);
719 spin_lock_irq(&sch
->lock
);
720 cio_disable_subchannel(sch
);
721 cdev
->private->state
= DEV_STATE_BOXED
;
723 spin_unlock_irq(&sch
->lock
);