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->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->iccws;
138 int i = pathmask_to_pos(req->lpm);
139 struct pgid *pgid = &cdev->private->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->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->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->pgid[i], pgid, sizeof(struct pgid));
375 * Process SENSE PGID data and report result.
377 static void snid_done(struct ccw_device *cdev, int rc)
379 struct ccw_dev_id *id = &cdev->private->dev_id;
380 struct subchannel *sch = to_subchannel(cdev->dev.parent);
389 pgid_analyze(cdev, &pgid, &mismatch, &reserved, &reset);
390 if (reserved == cdev->private->pgid_valid_mask)
395 donepm = pgid_to_donepm(cdev);
396 sch->vpm = donepm & sch->opm;
397 cdev->private->pgid_reset_mask |= reset;
398 cdev->private->pgid_todo_mask &=
399 ~(donepm | cdev->private->path_noirq_mask);
400 pgid_fill(cdev, pgid);
403 CIO_MSG_EVENT(2, "snid: device 0.%x.%04x: rc=%d pvm=%02x vpm=%02x "
404 "todo=%02x mism=%d rsvd=%02x reset=%02x\n", id->ssid,
405 id->devno, rc, cdev->private->pgid_valid_mask, sch->vpm,
406 cdev->private->pgid_todo_mask, mismatch, reserved, reset);
409 if (cdev->private->flags.pgid_unknown) {
410 pgid_wipeout_start(cdev);
413 /* Anything left to do? */
414 if (cdev->private->pgid_todo_mask == 0) {
415 verify_done(cdev, sch->vpm == 0 ? -EACCES : 0);
418 /* Perform path-grouping. */
422 /* Path-grouping not supported. */
423 cdev->private->flags.pgroup = 0;
424 cdev->private->flags.mpath = 0;
428 verify_done(cdev, rc);
433 * Create channel program to perform a SENSE PGID on a single path.
435 static void snid_build_cp(struct ccw_device *cdev)
437 struct ccw_request *req = &cdev->private->req;
438 struct ccw1 *cp = cdev->private->iccws;
439 int i = pathmask_to_pos(req->lpm);
441 /* Channel program setup. */
442 cp->cmd_code = CCW_CMD_SENSE_PGID;
443 cp->cda = (u32) (addr_t) &cdev->private->pgid[i];
444 cp->count = sizeof(struct pgid);
445 cp->flags = CCW_FLAG_SLI;
450 * Perform SENSE PGID on a single path.
452 static void snid_do(struct ccw_device *cdev)
454 struct subchannel *sch = to_subchannel(cdev->dev.parent);
455 struct ccw_request *req = &cdev->private->req;
458 req->lpm = lpm_adjust(req->lpm, sch->schib.pmcw.pam &
459 ~cdev->private->path_noirq_mask);
463 ccw_request_start(cdev);
467 if (cdev->private->pgid_valid_mask)
469 else if (cdev->private->path_noirq_mask)
473 snid_done(cdev, ret);
477 * Process SENSE PGID request result for single path.
479 static void snid_callback(struct ccw_device *cdev, void *data, int rc)
481 struct ccw_request *req = &cdev->private->req;
485 cdev->private->pgid_valid_mask |= req->lpm;
488 cdev->private->flags.pgid_unknown = 1;
489 cdev->private->path_noirq_mask |= req->lpm;
492 cdev->private->path_notoper_mask |= req->lpm;
497 /* Continue on the next path. */
507 * Perform path verification.
509 static void verify_start(struct ccw_device *cdev)
511 struct subchannel *sch = to_subchannel(cdev->dev.parent);
512 struct ccw_request *req = &cdev->private->req;
513 struct ccw_dev_id *devid = &cdev->private->dev_id;
516 sch->lpm = sch->schib.pmcw.pam;
518 /* Initialize PGID data. */
519 memset(cdev->private->pgid, 0, sizeof(cdev->private->pgid));
520 cdev->private->pgid_valid_mask = 0;
521 cdev->private->pgid_todo_mask = sch->schib.pmcw.pam;
522 cdev->private->path_notoper_mask = 0;
524 /* Initialize request data. */
525 memset(req, 0, sizeof(*req));
526 req->timeout = PGID_TIMEOUT;
527 req->maxretries = PGID_RETRIES;
530 if (cdev->private->flags.pgroup) {
531 CIO_TRACE_EVENT(4, "snid");
532 CIO_HEX_EVENT(4, devid, sizeof(*devid));
533 req->callback = snid_callback;
536 CIO_TRACE_EVENT(4, "nop");
537 CIO_HEX_EVENT(4, devid, sizeof(*devid));
538 req->filter = nop_filter;
539 req->callback = nop_callback;
545 * ccw_device_verify_start - perform path verification
548 * Perform an I/O on each available channel path to @cdev to determine which
549 * paths are operational. The resulting path mask is stored in sch->vpm.
550 * If device options specify pathgrouping, establish a pathgroup for the
551 * operational paths. When finished, call ccw_device_verify_done with a
552 * return code specifying the result.
554 void ccw_device_verify_start(struct ccw_device *cdev)
556 CIO_TRACE_EVENT(4, "vrfy");
557 CIO_HEX_EVENT(4, &cdev->private->dev_id, sizeof(cdev->private->dev_id));
559 * Initialize pathgroup and multipath state with target values.
560 * They may change in the course of path verification.
562 cdev->private->flags.pgroup = cdev->private->options.pgroup;
563 cdev->private->flags.mpath = cdev->private->options.mpath;
564 cdev->private->flags.doverify = 0;
565 cdev->private->path_noirq_mask = 0;
570 * Process disband SET PGID request result.
572 static void disband_callback(struct ccw_device *cdev, void *data, int rc)
574 struct subchannel *sch = to_subchannel(cdev->dev.parent);
575 struct ccw_dev_id *id = &cdev->private->dev_id;
579 /* Ensure consistent multipathing state at device and channel. */
580 cdev->private->flags.mpath = 0;
581 if (sch->config.mp) {
583 rc = cio_commit_config(sch);
586 CIO_MSG_EVENT(0, "disb: device 0.%x.%04x: rc=%d\n", id->ssid, id->devno,
588 ccw_device_disband_done(cdev, rc);
592 * ccw_device_disband_start - disband pathgroup
595 * Execute a SET PGID channel program on @cdev to disband a previously
596 * established pathgroup. When finished, call ccw_device_disband_done with
597 * a return code specifying the result.
599 void ccw_device_disband_start(struct ccw_device *cdev)
601 struct subchannel *sch = to_subchannel(cdev->dev.parent);
602 struct ccw_request *req = &cdev->private->req;
605 CIO_TRACE_EVENT(4, "disb");
606 CIO_HEX_EVENT(4, &cdev->private->dev_id, sizeof(cdev->private->dev_id));
608 memset(req, 0, sizeof(*req));
609 req->timeout = PGID_TIMEOUT;
610 req->maxretries = PGID_RETRIES;
611 req->lpm = sch->schib.pmcw.pam & sch->opm;
613 req->callback = disband_callback;
614 fn = SPID_FUNC_DISBAND;
615 if (cdev->private->flags.mpath)
616 fn |= SPID_FUNC_MULTI_PATH;
617 spid_build_cp(cdev, fn);
618 ccw_request_start(cdev);
622 struct completion done;
626 static void stlck_build_cp(struct ccw_device *cdev, void *buf1, void *buf2)
628 struct ccw_request *req = &cdev->private->req;
629 struct ccw1 *cp = cdev->private->iccws;
631 cp[0].cmd_code = CCW_CMD_STLCK;
632 cp[0].cda = (u32) (addr_t) buf1;
634 cp[0].flags = CCW_FLAG_CC;
635 cp[1].cmd_code = CCW_CMD_RELEASE;
636 cp[1].cda = (u32) (addr_t) buf2;
642 static void stlck_callback(struct ccw_device *cdev, void *data, int rc)
644 struct stlck_data *sdata = data;
647 complete(&sdata->done);
651 * ccw_device_stlck_start - perform unconditional release
653 * @data: data pointer to be passed to ccw_device_stlck_done
654 * @buf1: data pointer used in channel program
655 * @buf2: data pointer used in channel program
657 * Execute a channel program on @cdev to release an existing PGID reservation.
659 static void ccw_device_stlck_start(struct ccw_device *cdev, void *data,
660 void *buf1, void *buf2)
662 struct subchannel *sch = to_subchannel(cdev->dev.parent);
663 struct ccw_request *req = &cdev->private->req;
665 CIO_TRACE_EVENT(4, "stlck");
666 CIO_HEX_EVENT(4, &cdev->private->dev_id, sizeof(cdev->private->dev_id));
668 memset(req, 0, sizeof(*req));
669 req->timeout = PGID_TIMEOUT;
670 req->maxretries = PGID_RETRIES;
671 req->lpm = sch->schib.pmcw.pam & sch->opm;
673 req->callback = stlck_callback;
674 stlck_build_cp(cdev, buf1, buf2);
675 ccw_request_start(cdev);
679 * Perform unconditional reserve + release.
681 int ccw_device_stlck(struct ccw_device *cdev)
683 struct subchannel *sch = to_subchannel(cdev->dev.parent);
684 struct stlck_data data;
688 /* Check if steal lock operation is valid for this device. */
690 if (!cdev->private->options.force)
693 buffer = kzalloc(64, GFP_DMA | GFP_KERNEL);
696 init_completion(&data.done);
698 spin_lock_irq(sch->lock);
699 rc = cio_enable_subchannel(sch, (u32) (addr_t) sch);
702 /* Perform operation. */
703 cdev->private->state = DEV_STATE_STEAL_LOCK;
704 ccw_device_stlck_start(cdev, &data, &buffer[0], &buffer[32]);
705 spin_unlock_irq(sch->lock);
706 /* Wait for operation to finish. */
707 if (wait_for_completion_interruptible(&data.done)) {
709 spin_lock_irq(sch->lock);
710 ccw_request_cancel(cdev);
711 spin_unlock_irq(sch->lock);
712 wait_for_completion(&data.done);
716 spin_lock_irq(sch->lock);
717 cio_disable_subchannel(sch);
718 cdev->private->state = DEV_STATE_BOXED;
720 spin_unlock_irq(sch->lock);