blob: ce493144b054a758782229f672c2609b06a40e06 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Peter Oberparleiter9679baa2009-12-07 12:51:27 +01002 * CCW device PGID and path verification I/O handling.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Peter Oberparleiter9679baa2009-12-07 12:51:27 +01004 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Peter Oberparleiter9679baa2009-12-07 12:51:27 +010010#include <linux/kernel.h>
11#include <linux/string.h>
12#include <linux/types.h>
13#include <linux/errno.h>
14#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <asm/ccwdev.h>
16#include <asm/cio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include "cio.h"
19#include "cio_debug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "device.h"
Cornelia Huckcd6b4f22008-01-26 14:10:43 +010021#include "io_sch.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Peter Oberparleiter9679baa2009-12-07 12:51:27 +010023#define PGID_RETRIES 5
24#define PGID_TIMEOUT (10 * HZ)
Stefan Bader7b7db1b2006-08-30 14:33:39 +020025
26/*
Peter Oberparleiter9679baa2009-12-07 12:51:27 +010027 * Process path verification data and report result.
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 */
Peter Oberparleiter9679baa2009-12-07 12:51:27 +010029static void verify_done(struct ccw_device *cdev, int rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Cornelia Huck6810a2b2006-01-06 00:19:13 -080031 struct subchannel *sch = to_subchannel(cdev->dev.parent);
Peter Oberparleiter9679baa2009-12-07 12:51:27 +010032 struct ccw_dev_id *id = &cdev->private->dev_id;
33 int mpath = !cdev->private->flags.pgid_single;
34 int pgroup = cdev->private->options.pgroup;
Cornelia Huck6810a2b2006-01-06 00:19:13 -080035
Peter Oberparleiter9679baa2009-12-07 12:51:27 +010036 if (rc)
37 goto out;
38 /* Ensure consistent multipathing state at device and channel. */
39 if (sch->config.mp != mpath) {
40 sch->config.mp = mpath;
41 rc = cio_commit_config(sch);
Cornelia Huck6810a2b2006-01-06 00:19:13 -080042 }
Peter Oberparleiter9679baa2009-12-07 12:51:27 +010043out:
44 CIO_MSG_EVENT(2, "vrfy: device 0.%x.%04x: rc=%d pgroup=%d mpath=%d "
45 "vpm=%02x\n", id->ssid, id->devno, rc, pgroup, mpath,
46 sch->vpm);
47 ccw_device_verify_done(cdev, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
50/*
Peter Oberparleiter9679baa2009-12-07 12:51:27 +010051 * Create channel program to perform a NOOP.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 */
Peter Oberparleiter9679baa2009-12-07 12:51:27 +010053static void nop_build_cp(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Peter Oberparleiter9679baa2009-12-07 12:51:27 +010055 struct ccw_request *req = &cdev->private->req;
56 struct ccw1 *cp = cdev->private->iccws;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Peter Oberparleiter9679baa2009-12-07 12:51:27 +010058 cp->cmd_code = CCW_CMD_NOOP;
59 cp->cda = 0;
60 cp->count = 0;
61 cp->flags = CCW_FLAG_SLI;
62 req->cp = cp;
63}
Stefan Bader7b7db1b2006-08-30 14:33:39 +020064
Peter Oberparleiter9679baa2009-12-07 12:51:27 +010065/*
66 * Perform NOOP on a single path.
67 */
68static void nop_do(struct ccw_device *cdev)
69{
70 struct subchannel *sch = to_subchannel(cdev->dev.parent);
71 struct ccw_request *req = &cdev->private->req;
72
73 /* Adjust lpm. */
74 req->lpm = lpm_adjust(req->lpm, sch->schib.pmcw.pam & sch->opm);
75 if (!req->lpm)
76 goto out_nopath;
77 nop_build_cp(cdev);
78 ccw_request_start(cdev);
79 return;
80
81out_nopath:
82 verify_done(cdev, sch->vpm ? 0 : -EACCES);
83}
84
85/*
86 * Adjust NOOP I/O status.
87 */
88static enum io_status nop_filter(struct ccw_device *cdev, void *data,
89 struct irb *irb, enum io_status status)
90{
91 /* Only subchannel status might indicate a path error. */
92 if (status == IO_STATUS_ERROR && irb->scsw.cmd.cstat == 0)
93 return IO_DONE;
94 return status;
95}
96
97/*
98 * Process NOOP request result for a single path.
99 */
100static void nop_callback(struct ccw_device *cdev, void *data, int rc)
101{
102 struct subchannel *sch = to_subchannel(cdev->dev.parent);
103 struct ccw_request *req = &cdev->private->req;
104
105 if (rc == 0)
106 sch->vpm |= req->lpm;
107 else if (rc != -EACCES)
108 goto err;
109 req->lpm >>= 1;
110 nop_do(cdev);
111 return;
112
113err:
114 verify_done(cdev, rc);
115}
116
117/*
118 * Create channel program to perform SET PGID on a single path.
119 */
120static void spid_build_cp(struct ccw_device *cdev, u8 fn)
121{
122 struct ccw_request *req = &cdev->private->req;
123 struct ccw1 *cp = cdev->private->iccws;
124 int i = 8 - ffs(req->lpm);
125 struct pgid *pgid = &cdev->private->pgid[i];
126
127 pgid->inf.fc = fn;
128 cp->cmd_code = CCW_CMD_SET_PGID;
129 cp->cda = (u32) (addr_t) pgid;
130 cp->count = sizeof(*pgid);
131 cp->flags = CCW_FLAG_SLI;
132 req->cp = cp;
133}
134
135/*
136 * Perform establish/resign SET PGID on a single path.
137 */
138static void spid_do(struct ccw_device *cdev)
139{
140 struct subchannel *sch = to_subchannel(cdev->dev.parent);
141 struct ccw_request *req = &cdev->private->req;
142 u8 fn;
143
144 /* Adjust lpm if paths are not set in pam. */
145 req->lpm = lpm_adjust(req->lpm, sch->schib.pmcw.pam);
146 if (!req->lpm)
147 goto out_nopath;
148 /* Channel program setup. */
149 if (req->lpm & sch->opm)
150 fn = SPID_FUNC_ESTABLISH;
151 else
152 fn = SPID_FUNC_RESIGN;
153 if (!cdev->private->flags.pgid_single)
154 fn |= SPID_FUNC_MULTI_PATH;
155 spid_build_cp(cdev, fn);
156 ccw_request_start(cdev);
157 return;
158
159out_nopath:
160 verify_done(cdev, sch->vpm ? 0 : -EACCES);
161}
162
163static void verify_start(struct ccw_device *cdev);
164
165/*
166 * Process SET PGID request result for a single path.
167 */
168static void spid_callback(struct ccw_device *cdev, void *data, int rc)
169{
170 struct subchannel *sch = to_subchannel(cdev->dev.parent);
171 struct ccw_request *req = &cdev->private->req;
172
173 switch (rc) {
174 case 0:
175 sch->vpm |= req->lpm & sch->opm;
176 break;
177 case -EACCES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 break;
179 case -EOPNOTSUPP:
Peter Oberparleiter9679baa2009-12-07 12:51:27 +0100180 if (!cdev->private->flags.pgid_single) {
181 /* Try without multipathing. */
182 cdev->private->flags.pgid_single = 1;
183 goto out_restart;
184 }
185 /* Try without pathgrouping. */
186 cdev->private->options.pgroup = 0;
187 goto out_restart;
188 default:
189 goto err;
190 }
191 req->lpm >>= 1;
192 spid_do(cdev);
193 return;
194
195out_restart:
196 verify_start(cdev);
197 return;
198err:
199 verify_done(cdev, rc);
200}
201
202static int pgid_cmp(struct pgid *p1, struct pgid *p2)
203{
204 return memcmp((char *) p1 + 1, (char *) p2 + 1,
205 sizeof(struct pgid) - 1);
206}
207
208/*
209 * Determine pathgroup state from PGID data.
210 */
211static void pgid_analyze(struct ccw_device *cdev, struct pgid **p,
212 int *mismatch, int *reserved, int *reset)
213{
214 struct pgid *pgid = &cdev->private->pgid[0];
215 struct pgid *first = NULL;
216 int lpm;
217 int i;
218
219 *mismatch = 0;
220 *reserved = 0;
221 *reset = 0;
222 for (i = 0, lpm = 0x80; i < 8; i++, pgid++, lpm >>= 1) {
223 if ((cdev->private->pgid_valid_mask & lpm) == 0)
224 continue;
225 if (pgid->inf.ps.state2 == SNID_STATE2_RESVD_ELSE)
226 *reserved = 1;
227 if (pgid->inf.ps.state1 == SNID_STATE1_RESET) {
228 /* A PGID was reset. */
229 *reset = 1;
230 continue;
231 }
232 if (!first) {
233 first = pgid;
234 continue;
235 }
236 if (pgid_cmp(pgid, first) != 0)
237 *mismatch = 1;
238 }
239 if (!first)
240 first = &channel_subsystems[0]->global_pgid;
241 *p = first;
242}
243
244static void pgid_fill(struct ccw_device *cdev, struct pgid *pgid)
245{
246 int i;
247
248 for (i = 0; i < 8; i++)
249 memcpy(&cdev->private->pgid[i], pgid, sizeof(struct pgid));
250}
251
252/*
253 * Process SENSE PGID data and report result.
254 */
255static void snid_done(struct ccw_device *cdev, int rc)
256{
257 struct ccw_dev_id *id = &cdev->private->dev_id;
258 struct pgid *pgid;
259 int mismatch = 0;
260 int reserved = 0;
261 int reset = 0;
262
263 if (rc)
264 goto out;
265 pgid_analyze(cdev, &pgid, &mismatch, &reserved, &reset);
266 if (!mismatch) {
267 pgid_fill(cdev, pgid);
268 cdev->private->flags.pgid_rdy = 1;
269 }
270 if (reserved)
271 rc = -EUSERS;
272out:
273 CIO_MSG_EVENT(2, "snid: device 0.%x.%04x: rc=%d pvm=%02x mism=%d "
274 "rsvd=%d reset=%d\n", id->ssid, id->devno, rc,
275 cdev->private->pgid_valid_mask, mismatch, reserved,
276 reset);
277 ccw_device_sense_pgid_done(cdev, rc);
278}
279
280/*
281 * Create channel program to perform a SENSE PGID on a single path.
282 */
283static void snid_build_cp(struct ccw_device *cdev)
284{
285 struct ccw_request *req = &cdev->private->req;
286 struct ccw1 *cp = cdev->private->iccws;
287 int i = 8 - ffs(req->lpm);
288
289 /* Channel program setup. */
290 cp->cmd_code = CCW_CMD_SENSE_PGID;
291 cp->cda = (u32) (addr_t) &cdev->private->pgid[i];
292 cp->count = sizeof(struct pgid);
293 cp->flags = CCW_FLAG_SLI;
294 req->cp = cp;
295}
296
297/*
298 * Perform SENSE PGID on a single path.
299 */
300static void snid_do(struct ccw_device *cdev)
301{
302 struct subchannel *sch = to_subchannel(cdev->dev.parent);
303 struct ccw_request *req = &cdev->private->req;
304
305 /* Adjust lpm if paths are not set in pam. */
306 req->lpm = lpm_adjust(req->lpm, sch->schib.pmcw.pam);
307 if (!req->lpm)
308 goto out_nopath;
309 snid_build_cp(cdev);
310 ccw_request_start(cdev);
311 return;
312
313out_nopath:
314 snid_done(cdev, cdev->private->pgid_valid_mask ? 0 : -EACCES);
315}
316
317/*
318 * Process SENSE PGID request result for single path.
319 */
320static void snid_callback(struct ccw_device *cdev, void *data, int rc)
321{
322 struct ccw_request *req = &cdev->private->req;
323
324 if (rc == 0)
325 cdev->private->pgid_valid_mask |= req->lpm;
326 else if (rc != -EACCES)
327 goto err;
328 req->lpm >>= 1;
329 snid_do(cdev);
330 return;
331
332err:
333 snid_done(cdev, rc);
334}
335
336/**
337 * ccw_device_sense_pgid_start - perform SENSE PGID
338 * @cdev: ccw device
339 *
340 * Execute a SENSE PGID channel program on each path to @cdev to update its
341 * PGID information. When finished, call ccw_device_sense_id_done with a
342 * return code specifying the result.
343 */
344void ccw_device_sense_pgid_start(struct ccw_device *cdev)
345{
346 struct ccw_request *req = &cdev->private->req;
347
348 CIO_TRACE_EVENT(4, "snid");
349 CIO_HEX_EVENT(4, &cdev->private->dev_id, sizeof(cdev->private->dev_id));
350 /* Initialize PGID data. */
351 memset(cdev->private->pgid, 0, sizeof(cdev->private->pgid));
352 cdev->private->flags.pgid_rdy = 0;
353 cdev->private->pgid_valid_mask = 0;
354 /* Initialize request data. */
355 memset(req, 0, sizeof(*req));
356 req->timeout = PGID_TIMEOUT;
357 req->maxretries = PGID_RETRIES;
358 req->callback = snid_callback;
359 req->lpm = 0x80;
360 snid_do(cdev);
361}
362
363/*
364 * Perform path verification.
365 */
366static void verify_start(struct ccw_device *cdev)
367{
368 struct subchannel *sch = to_subchannel(cdev->dev.parent);
369 struct ccw_request *req = &cdev->private->req;
370
371 sch->vpm = 0;
372 /* Initialize request data. */
373 memset(req, 0, sizeof(*req));
374 req->timeout = PGID_TIMEOUT;
375 req->maxretries = PGID_RETRIES;
376 req->lpm = 0x80;
377 if (cdev->private->options.pgroup) {
378 req->callback = spid_callback;
379 spid_do(cdev);
380 } else {
381 req->filter = nop_filter;
382 req->callback = nop_callback;
383 nop_do(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385}
386
Peter Oberparleiter9679baa2009-12-07 12:51:27 +0100387/**
388 * ccw_device_verify_start - perform path verification
389 * @cdev: ccw device
390 *
391 * Perform an I/O on each available channel path to @cdev to determine which
392 * paths are operational. The resulting path mask is stored in sch->vpm.
393 * If device options specify pathgrouping, establish a pathgroup for the
394 * operational paths. When finished, call ccw_device_verify_done with a
395 * return code specifying the result.
396 */
397void ccw_device_verify_start(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Peter Oberparleiter9679baa2009-12-07 12:51:27 +0100399 CIO_TRACE_EVENT(4, "vrfy");
400 CIO_HEX_EVENT(4, &cdev->private->dev_id, sizeof(cdev->private->dev_id));
401 if (!cdev->private->flags.pgid_rdy) {
402 /* No pathgrouping possible. */
403 cdev->private->options.pgroup = 0;
404 cdev->private->flags.pgid_single = 1;
405 } else
406 cdev->private->flags.pgid_single = 0;
407 cdev->private->flags.doverify = 0;
408 verify_start(cdev);
409}
Cornelia Huckf1ee3282006-10-04 20:02:02 +0200410
Peter Oberparleiter9679baa2009-12-07 12:51:27 +0100411/*
412 * Process disband SET PGID request result.
413 */
414static void disband_callback(struct ccw_device *cdev, void *data, int rc)
415{
416 struct subchannel *sch = to_subchannel(cdev->dev.parent);
417 struct ccw_dev_id *id = &cdev->private->dev_id;
418
419 if (rc)
420 goto out;
421 /* Ensure consistent multipathing state at device and channel. */
422 cdev->private->flags.pgid_single = 1;
423 if (sch->config.mp) {
424 sch->config.mp = 0;
425 rc = cio_commit_config(sch);
426 }
427out:
428 CIO_MSG_EVENT(0, "disb: device 0.%x.%04x: rc=%d\n", id->ssid, id->devno,
429 rc);
430 ccw_device_disband_done(cdev, rc);
431}
432
433/**
434 * ccw_device_disband_start - disband pathgroup
435 * @cdev: ccw device
436 *
437 * Execute a SET PGID channel program on @cdev to disband a previously
438 * established pathgroup. When finished, call ccw_device_disband_done with
439 * a return code specifying the result.
440 */
441void ccw_device_disband_start(struct ccw_device *cdev)
442{
443 struct subchannel *sch = to_subchannel(cdev->dev.parent);
444 struct ccw_request *req = &cdev->private->req;
445 u8 fn;
446
447 CIO_TRACE_EVENT(4, "disb");
448 CIO_HEX_EVENT(4, &cdev->private->dev_id, sizeof(cdev->private->dev_id));
449 /* Request setup. */
450 memset(req, 0, sizeof(*req));
451 req->timeout = PGID_TIMEOUT;
452 req->maxretries = PGID_RETRIES;
453 req->lpm = sch->schib.pmcw.pam & sch->opm;
454 req->callback = disband_callback;
455 fn = SPID_FUNC_DISBAND;
456 if (!cdev->private->flags.pgid_single)
457 fn |= SPID_FUNC_MULTI_PATH;
458 spid_build_cp(cdev, fn);
459 ccw_request_start(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}