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/types.h>
13 #include <linux/errno.h>
14 #include <linux/bitops.h>
15 #include <asm/ccwdev.h>
19 #include "cio_debug.h"
23 #define PGID_RETRIES 256
24 #define PGID_TIMEOUT (10 * HZ)
26 static void verify_start(struct ccw_device *cdev);
29 * Process path verification data and report result.
31 static void verify_done(struct ccw_device *cdev, int rc)
33 struct subchannel *sch = to_subchannel(cdev->dev.parent);
34 struct ccw_dev_id *id = &cdev->private->dev_id;
35 int mpath = cdev->private->flags.mpath;
36 int pgroup = cdev->private->flags.pgroup;
40 /* Ensure consistent multipathing state at device and channel. */
41 if (sch->config.mp != mpath) {
42 sch->config.mp = mpath;
43 rc = cio_commit_config(sch);
46 CIO_MSG_EVENT(2, "vrfy: device 0.%x.%04x: rc=%d pgroup=%d mpath=%d "
47 "vpm=%02x\n", id->ssid, id->devno, rc, pgroup, mpath,
49 ccw_device_verify_done(cdev, rc);
53 * Create channel program to perform a NOOP.
55 static void nop_build_cp(struct ccw_device *cdev)
57 struct ccw_request *req = &cdev->private->req;
58 struct ccw1 *cp = cdev->private->iccws;
60 cp->cmd_code = CCW_CMD_NOOP;
63 cp->flags = CCW_FLAG_SLI;
68 * Perform NOOP on a single path.
70 static void nop_do(struct ccw_device *cdev)
72 struct subchannel *sch = to_subchannel(cdev->dev.parent);
73 struct ccw_request *req = &cdev->private->req;
75 req->lpm = lpm_adjust(req->lpm, sch->schib.pmcw.pam & sch->opm &
76 ~cdev->private->path_noirq_mask);
80 ccw_request_start(cdev);
84 verify_done(cdev, sch->vpm ? 0 : -EACCES);
88 * Adjust NOOP I/O status.
90 static enum io_status nop_filter(struct ccw_device *cdev, void *data,
91 struct irb *irb, enum io_status status)
93 /* Only subchannel status might indicate a path error. */
94 if (status == IO_STATUS_ERROR && irb->scsw.cmd.cstat == 0)
100 * Process NOOP request result for a single path.
102 static void nop_callback(struct ccw_device *cdev, void *data, int rc)
104 struct subchannel *sch = to_subchannel(cdev->dev.parent);
105 struct ccw_request *req = &cdev->private->req;
109 sch->vpm |= req->lpm;
112 cdev->private->path_noirq_mask |= req->lpm;
115 cdev->private->path_notoper_mask |= req->lpm;
120 /* Continue on the next path. */
126 verify_done(cdev, rc);
130 * Create channel program to perform SET PGID on a single path.
132 static void spid_build_cp(struct ccw_device *cdev, u8 fn)
134 struct ccw_request *req = &cdev->private->req;
135 struct ccw1 *cp = cdev->private->iccws;
136 int i = 8 - ffs(req->lpm);
137 struct pgid *pgid = &cdev->private->pgid[i];
140 cp->cmd_code = CCW_CMD_SET_PGID;
141 cp->cda = (u32) (addr_t) pgid;
142 cp->count = sizeof(*pgid);
143 cp->flags = CCW_FLAG_SLI;
147 static void pgid_wipeout_callback(struct ccw_device *cdev, void *data, int rc)
150 /* We don't know the path groups' state. Abort. */
151 verify_done(cdev, rc);
155 * Path groups have been reset. Restart path verification but
156 * leave paths in path_noirq_mask out.
158 cdev->private->flags.pgid_unknown = 0;
163 * Reset pathgroups and restart path verification, leave unusable paths out.
165 static void pgid_wipeout_start(struct ccw_device *cdev)
167 struct subchannel *sch = to_subchannel(cdev->dev.parent);
168 struct ccw_dev_id *id = &cdev->private->dev_id;
169 struct ccw_request *req = &cdev->private->req;
172 CIO_MSG_EVENT(2, "wipe: device 0.%x.%04x: pvm=%02x nim=%02x\n",
173 id->ssid, id->devno, cdev->private->pgid_valid_mask,
174 cdev->private->path_noirq_mask);
176 /* Initialize request data. */
177 memset(req, 0, sizeof(*req));
178 req->timeout = PGID_TIMEOUT;
179 req->maxretries = PGID_RETRIES;
180 req->lpm = sch->schib.pmcw.pam;
181 req->callback = pgid_wipeout_callback;
182 fn = SPID_FUNC_DISBAND;
183 if (cdev->private->flags.mpath)
184 fn |= SPID_FUNC_MULTI_PATH;
185 spid_build_cp(cdev, fn);
186 ccw_request_start(cdev);
190 * Perform establish/resign SET PGID on a single path.
192 static void spid_do(struct ccw_device *cdev)
194 struct subchannel *sch = to_subchannel(cdev->dev.parent);
195 struct ccw_request *req = &cdev->private->req;
198 /* Use next available path that is not already in correct state. */
199 req->lpm = lpm_adjust(req->lpm, cdev->private->pgid_todo_mask);
202 /* Channel program setup. */
203 if (req->lpm & sch->opm)
204 fn = SPID_FUNC_ESTABLISH;
206 fn = SPID_FUNC_RESIGN;
207 if (cdev->private->flags.mpath)
208 fn |= SPID_FUNC_MULTI_PATH;
209 spid_build_cp(cdev, fn);
210 ccw_request_start(cdev);
214 if (cdev->private->flags.pgid_unknown) {
215 /* At least one SPID could be partially done. */
216 pgid_wipeout_start(cdev);
219 verify_done(cdev, sch->vpm ? 0 : -EACCES);
223 * Process SET PGID request result for a single path.
225 static void spid_callback(struct ccw_device *cdev, void *data, int rc)
227 struct subchannel *sch = to_subchannel(cdev->dev.parent);
228 struct ccw_request *req = &cdev->private->req;
232 sch->vpm |= req->lpm & sch->opm;
235 cdev->private->flags.pgid_unknown = 1;
236 cdev->private->path_noirq_mask |= req->lpm;
239 cdev->private->path_notoper_mask |= req->lpm;
242 if (cdev->private->flags.mpath) {
243 /* Try without multipathing. */
244 cdev->private->flags.mpath = 0;
247 /* Try without pathgrouping. */
248 cdev->private->flags.pgroup = 0;
261 verify_done(cdev, rc);
264 static void spid_start(struct ccw_device *cdev)
266 struct ccw_request *req = &cdev->private->req;
268 /* Initialize request data. */
269 memset(req, 0, sizeof(*req));
270 req->timeout = PGID_TIMEOUT;
271 req->maxretries = PGID_RETRIES;
274 req->callback = spid_callback;
278 static int pgid_is_reset(struct pgid *p)
282 for (c = (char *)p + 1; c < (char *)(p + 1); c++) {
289 static int pgid_cmp(struct pgid *p1, struct pgid *p2)
291 return memcmp((char *) p1 + 1, (char *) p2 + 1,
292 sizeof(struct pgid) - 1);
296 * Determine pathgroup state from PGID data.
298 static void pgid_analyze(struct ccw_device *cdev, struct pgid **p,
299 int *mismatch, u8 *reserved, u8 *reset)
301 struct pgid *pgid = &cdev->private->pgid[0];
302 struct pgid *first = NULL;
309 for (i = 0, lpm = 0x80; i < 8; i++, pgid++, lpm >>= 1) {
310 if ((cdev->private->pgid_valid_mask & lpm) == 0)
312 if (pgid->inf.ps.state2 == SNID_STATE2_RESVD_ELSE)
314 if (pgid_is_reset(pgid)) {
322 if (pgid_cmp(pgid, first) != 0)
326 first = &channel_subsystems[0]->global_pgid;
330 static u8 pgid_to_donepm(struct ccw_device *cdev)
332 struct subchannel *sch = to_subchannel(cdev->dev.parent);
338 /* Set bits for paths which are already in the target state. */
339 for (i = 0; i < 8; i++) {
341 if ((cdev->private->pgid_valid_mask & lpm) == 0)
343 pgid = &cdev->private->pgid[i];
344 if (sch->opm & lpm) {
345 if (pgid->inf.ps.state1 != SNID_STATE1_GROUPED)
348 if (pgid->inf.ps.state1 != SNID_STATE1_UNGROUPED)
351 if (cdev->private->flags.mpath) {
352 if (pgid->inf.ps.state3 != SNID_STATE3_MULTI_PATH)
355 if (pgid->inf.ps.state3 != SNID_STATE3_SINGLE_PATH)
364 static void pgid_fill(struct ccw_device *cdev, struct pgid *pgid)
368 for (i = 0; i < 8; i++)
369 memcpy(&cdev->private->pgid[i], pgid, sizeof(struct pgid));
373 * Process SENSE PGID data and report result.
375 static void snid_done(struct ccw_device *cdev, int rc)
377 struct ccw_dev_id *id = &cdev->private->dev_id;
378 struct subchannel *sch = to_subchannel(cdev->dev.parent);
387 pgid_analyze(cdev, &pgid, &mismatch, &reserved, &reset);
388 if (reserved == cdev->private->pgid_valid_mask)
393 donepm = pgid_to_donepm(cdev);
394 sch->vpm = donepm & sch->opm;
395 cdev->private->pgid_reset_mask |= reset;
396 cdev->private->pgid_todo_mask &=
397 ~(donepm | cdev->private->path_noirq_mask);
398 pgid_fill(cdev, pgid);
401 CIO_MSG_EVENT(2, "snid: device 0.%x.%04x: rc=%d pvm=%02x vpm=%02x "
402 "todo=%02x mism=%d rsvd=%02x reset=%02x\n", id->ssid,
403 id->devno, rc, cdev->private->pgid_valid_mask, sch->vpm,
404 cdev->private->pgid_todo_mask, mismatch, reserved, reset);
407 if (cdev->private->flags.pgid_unknown) {
408 pgid_wipeout_start(cdev);
411 /* Anything left to do? */
412 if (cdev->private->pgid_todo_mask == 0) {
413 verify_done(cdev, sch->vpm == 0 ? -EACCES : 0);
416 /* Perform path-grouping. */
420 /* Path-grouping not supported. */
421 cdev->private->flags.pgroup = 0;
422 cdev->private->flags.mpath = 0;
426 verify_done(cdev, rc);
431 * Create channel program to perform a SENSE PGID on a single path.
433 static void snid_build_cp(struct ccw_device *cdev)
435 struct ccw_request *req = &cdev->private->req;
436 struct ccw1 *cp = cdev->private->iccws;
437 int i = 8 - ffs(req->lpm);
439 /* Channel program setup. */
440 cp->cmd_code = CCW_CMD_SENSE_PGID;
441 cp->cda = (u32) (addr_t) &cdev->private->pgid[i];
442 cp->count = sizeof(struct pgid);
443 cp->flags = CCW_FLAG_SLI;
448 * Perform SENSE PGID on a single path.
450 static void snid_do(struct ccw_device *cdev)
452 struct subchannel *sch = to_subchannel(cdev->dev.parent);
453 struct ccw_request *req = &cdev->private->req;
456 req->lpm = lpm_adjust(req->lpm, sch->schib.pmcw.pam &
457 ~cdev->private->path_noirq_mask);
461 ccw_request_start(cdev);
465 if (cdev->private->pgid_valid_mask)
467 else if (cdev->private->path_noirq_mask)
471 snid_done(cdev, ret);
475 * Process SENSE PGID request result for single path.
477 static void snid_callback(struct ccw_device *cdev, void *data, int rc)
479 struct ccw_request *req = &cdev->private->req;
483 cdev->private->pgid_valid_mask |= req->lpm;
486 cdev->private->flags.pgid_unknown = 1;
487 cdev->private->path_noirq_mask |= req->lpm;
490 cdev->private->path_notoper_mask |= req->lpm;
495 /* Continue on the next path. */
505 * Perform path verification.
507 static void verify_start(struct ccw_device *cdev)
509 struct subchannel *sch = to_subchannel(cdev->dev.parent);
510 struct ccw_request *req = &cdev->private->req;
511 struct ccw_dev_id *devid = &cdev->private->dev_id;
514 sch->lpm = sch->schib.pmcw.pam;
516 /* Initialize PGID data. */
517 memset(cdev->private->pgid, 0, sizeof(cdev->private->pgid));
518 cdev->private->pgid_valid_mask = 0;
519 cdev->private->pgid_todo_mask = sch->schib.pmcw.pam;
520 cdev->private->path_notoper_mask = 0;
522 /* Initialize request data. */
523 memset(req, 0, sizeof(*req));
524 req->timeout = PGID_TIMEOUT;
525 req->maxretries = PGID_RETRIES;
528 if (cdev->private->flags.pgroup) {
529 CIO_TRACE_EVENT(4, "snid");
530 CIO_HEX_EVENT(4, devid, sizeof(*devid));
531 req->callback = snid_callback;
534 CIO_TRACE_EVENT(4, "nop");
535 CIO_HEX_EVENT(4, devid, sizeof(*devid));
536 req->filter = nop_filter;
537 req->callback = nop_callback;
543 * ccw_device_verify_start - perform path verification
546 * Perform an I/O on each available channel path to @cdev to determine which
547 * paths are operational. The resulting path mask is stored in sch->vpm.
548 * If device options specify pathgrouping, establish a pathgroup for the
549 * operational paths. When finished, call ccw_device_verify_done with a
550 * return code specifying the result.
552 void ccw_device_verify_start(struct ccw_device *cdev)
554 CIO_TRACE_EVENT(4, "vrfy");
555 CIO_HEX_EVENT(4, &cdev->private->dev_id, sizeof(cdev->private->dev_id));
557 * Initialize pathgroup and multipath state with target values.
558 * They may change in the course of path verification.
560 cdev->private->flags.pgroup = cdev->private->options.pgroup;
561 cdev->private->flags.mpath = cdev->private->options.mpath;
562 cdev->private->flags.doverify = 0;
563 cdev->private->path_noirq_mask = 0;
568 * Process disband SET PGID request result.
570 static void disband_callback(struct ccw_device *cdev, void *data, int rc)
572 struct subchannel *sch = to_subchannel(cdev->dev.parent);
573 struct ccw_dev_id *id = &cdev->private->dev_id;
577 /* Ensure consistent multipathing state at device and channel. */
578 cdev->private->flags.mpath = 0;
579 if (sch->config.mp) {
581 rc = cio_commit_config(sch);
584 CIO_MSG_EVENT(0, "disb: device 0.%x.%04x: rc=%d\n", id->ssid, id->devno,
586 ccw_device_disband_done(cdev, rc);
590 * ccw_device_disband_start - disband pathgroup
593 * Execute a SET PGID channel program on @cdev to disband a previously
594 * established pathgroup. When finished, call ccw_device_disband_done with
595 * a return code specifying the result.
597 void ccw_device_disband_start(struct ccw_device *cdev)
599 struct subchannel *sch = to_subchannel(cdev->dev.parent);
600 struct ccw_request *req = &cdev->private->req;
603 CIO_TRACE_EVENT(4, "disb");
604 CIO_HEX_EVENT(4, &cdev->private->dev_id, sizeof(cdev->private->dev_id));
606 memset(req, 0, sizeof(*req));
607 req->timeout = PGID_TIMEOUT;
608 req->maxretries = PGID_RETRIES;
609 req->lpm = sch->schib.pmcw.pam & sch->opm;
611 req->callback = disband_callback;
612 fn = SPID_FUNC_DISBAND;
613 if (cdev->private->flags.mpath)
614 fn |= SPID_FUNC_MULTI_PATH;
615 spid_build_cp(cdev, fn);
616 ccw_request_start(cdev);
619 static void stlck_build_cp(struct ccw_device *cdev, void *buf1, void *buf2)
621 struct ccw_request *req = &cdev->private->req;
622 struct ccw1 *cp = cdev->private->iccws;
624 cp[0].cmd_code = CCW_CMD_STLCK;
625 cp[0].cda = (u32) (addr_t) buf1;
627 cp[0].flags = CCW_FLAG_CC;
628 cp[1].cmd_code = CCW_CMD_RELEASE;
629 cp[1].cda = (u32) (addr_t) buf2;
635 static void stlck_callback(struct ccw_device *cdev, void *data, int rc)
637 ccw_device_stlck_done(cdev, data, rc);
641 * ccw_device_stlck_start - perform unconditional release
643 * @data: data pointer to be passed to ccw_device_stlck_done
644 * @buf1: data pointer used in channel program
645 * @buf2: data pointer used in channel program
647 * Execute a channel program on @cdev to release an existing PGID reservation.
648 * When finished, call ccw_device_stlck_done with a return code specifying the
651 void ccw_device_stlck_start(struct ccw_device *cdev, void *data, void *buf1,
654 struct subchannel *sch = to_subchannel(cdev->dev.parent);
655 struct ccw_request *req = &cdev->private->req;
657 CIO_TRACE_EVENT(4, "stlck");
658 CIO_HEX_EVENT(4, &cdev->private->dev_id, sizeof(cdev->private->dev_id));
660 memset(req, 0, sizeof(*req));
661 req->timeout = PGID_TIMEOUT;
662 req->maxretries = PGID_RETRIES;
663 req->lpm = sch->schib.pmcw.pam & sch->opm;
665 req->callback = stlck_callback;
666 stlck_build_cp(cdev, buf1, buf2);
667 ccw_request_start(cdev);