]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/s390/cio/device_pgid.c
[S390] cio: use sense-pgid operation for path verification
[net-next-2.6.git] / drivers / s390 / cio / device_pgid.c
1 /*
2  *  CCW device PGID and path verification I/O handling.
3  *
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>
8  */
9
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>
16 #include <asm/cio.h>
17
18 #include "cio.h"
19 #include "cio_debug.h"
20 #include "device.h"
21 #include "io_sch.h"
22
23 #define PGID_RETRIES    5
24 #define PGID_TIMEOUT    (10 * HZ)
25
26 /*
27  * Process path verification data and report result.
28  */
29 static void verify_done(struct ccw_device *cdev, int rc)
30 {
31         struct subchannel *sch = to_subchannel(cdev->dev.parent);
32         struct ccw_dev_id *id = &cdev->private->dev_id;
33         int mpath = cdev->private->flags.mpath;
34         int pgroup = cdev->private->flags.pgroup;
35
36         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);
42         }
43 out:
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);
48 }
49
50 /*
51  * Create channel program to perform a NOOP.
52  */
53 static void nop_build_cp(struct ccw_device *cdev)
54 {
55         struct ccw_request *req = &cdev->private->req;
56         struct ccw1 *cp = cdev->private->iccws;
57
58         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 }
64
65 /*
66  * Perform NOOP on a single path.
67  */
68 static 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
81 out_nopath:
82         verify_done(cdev, sch->vpm ? 0 : -EACCES);
83 }
84
85 /*
86  * Adjust NOOP I/O status.
87  */
88 static 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  */
100 static 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
113 err:
114         verify_done(cdev, rc);
115 }
116
117 /*
118  * Create channel program to perform SET PGID on a single path.
119  */
120 static 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  */
138 static 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         /* Use next available path that is not already in correct state. */
145         req->lpm = lpm_adjust(req->lpm, sch->schib.pmcw.pam & ~sch->vpm);
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.mpath)
154                 fn |= SPID_FUNC_MULTI_PATH;
155         spid_build_cp(cdev, fn);
156         ccw_request_start(cdev);
157         return;
158
159 out_nopath:
160         verify_done(cdev, sch->vpm ? 0 : -EACCES);
161 }
162
163 static void verify_start(struct ccw_device *cdev);
164
165 /*
166  * Process SET PGID request result for a single path.
167  */
168 static 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:
178                 break;
179         case -EOPNOTSUPP:
180                 if (cdev->private->flags.mpath) {
181                         /* Try without multipathing. */
182                         cdev->private->flags.mpath = 0;
183                         goto out_restart;
184                 }
185                 /* Try without pathgrouping. */
186                 cdev->private->flags.pgroup = 0;
187                 goto out_restart;
188         default:
189                 goto err;
190         }
191         req->lpm >>= 1;
192         spid_do(cdev);
193         return;
194
195 out_restart:
196         verify_start(cdev);
197         return;
198 err:
199         verify_done(cdev, rc);
200 }
201
202 static void spid_start(struct ccw_device *cdev)
203 {
204         struct ccw_request *req = &cdev->private->req;
205
206         /* Initialize request data. */
207         memset(req, 0, sizeof(*req));
208         req->timeout    = PGID_TIMEOUT;
209         req->maxretries = PGID_RETRIES;
210         req->lpm        = 0x80;
211         req->callback   = spid_callback;
212         spid_do(cdev);
213 }
214
215 static int pgid_cmp(struct pgid *p1, struct pgid *p2)
216 {
217         return memcmp((char *) p1 + 1, (char *) p2 + 1,
218                       sizeof(struct pgid) - 1);
219 }
220
221 /*
222  * Determine pathgroup state from PGID data.
223  */
224 static void pgid_analyze(struct ccw_device *cdev, struct pgid **p,
225                          int *mismatch, int *reserved, int *reset)
226 {
227         struct pgid *pgid = &cdev->private->pgid[0];
228         struct pgid *first = NULL;
229         int lpm;
230         int i;
231
232         *mismatch = 0;
233         *reserved = 0;
234         *reset = 0;
235         for (i = 0, lpm = 0x80; i < 8; i++, pgid++, lpm >>= 1) {
236                 if ((cdev->private->pgid_valid_mask & lpm) == 0)
237                         continue;
238                 if (pgid->inf.ps.state2 == SNID_STATE2_RESVD_ELSE)
239                         *reserved = 1;
240                 if (pgid->inf.ps.state1 == SNID_STATE1_RESET) {
241                         /* A PGID was reset. */
242                         *reset = 1;
243                         continue;
244                 }
245                 if (!first) {
246                         first = pgid;
247                         continue;
248                 }
249                 if (pgid_cmp(pgid, first) != 0)
250                         *mismatch = 1;
251         }
252         if (!first)
253                 first = &channel_subsystems[0]->global_pgid;
254         *p = first;
255 }
256
257 static u8 pgid_to_vpm(struct ccw_device *cdev)
258 {
259         struct subchannel *sch = to_subchannel(cdev->dev.parent);
260         struct pgid *pgid;
261         int i;
262         int lpm;
263         u8 vpm = 0;
264
265         /* Set VPM bits for paths which are already in the target state. */
266         for (i = 0; i < 8; i++) {
267                 lpm = 0x80 >> i;
268                 if ((cdev->private->pgid_valid_mask & lpm) == 0)
269                         continue;
270                 pgid = &cdev->private->pgid[i];
271                 if (sch->opm & lpm) {
272                         if (pgid->inf.ps.state1 != SNID_STATE1_GROUPED)
273                                 continue;
274                 } else {
275                         if (pgid->inf.ps.state1 != SNID_STATE1_UNGROUPED)
276                                 continue;
277                 }
278                 if (cdev->private->flags.mpath) {
279                         if (pgid->inf.ps.state3 != SNID_STATE3_MULTI_PATH)
280                                 continue;
281                 } else {
282                         if (pgid->inf.ps.state3 != SNID_STATE3_SINGLE_PATH)
283                                 continue;
284                 }
285                 vpm |= lpm;
286         }
287
288         return vpm;
289 }
290
291 static void pgid_fill(struct ccw_device *cdev, struct pgid *pgid)
292 {
293         int i;
294
295         for (i = 0; i < 8; i++)
296                 memcpy(&cdev->private->pgid[i], pgid, sizeof(struct pgid));
297 }
298
299 /*
300  * Process SENSE PGID data and report result.
301  */
302 static void snid_done(struct ccw_device *cdev, int rc)
303 {
304         struct ccw_dev_id *id = &cdev->private->dev_id;
305         struct subchannel *sch = to_subchannel(cdev->dev.parent);
306         struct pgid *pgid;
307         int mismatch = 0;
308         int reserved = 0;
309         int reset = 0;
310
311         if (rc)
312                 goto out;
313         pgid_analyze(cdev, &pgid, &mismatch, &reserved, &reset);
314         if (reserved)
315                 rc = -EUSERS;
316         else if (mismatch)
317                 rc = -EOPNOTSUPP;
318         else {
319                 sch->vpm = pgid_to_vpm(cdev);
320                 pgid_fill(cdev, pgid);
321         }
322 out:
323         CIO_MSG_EVENT(2, "snid: device 0.%x.%04x: rc=%d pvm=%02x vpm=%02x "
324                       "mism=%d rsvd=%d reset=%d\n", id->ssid, id->devno, rc,
325                       cdev->private->pgid_valid_mask, sch->vpm, mismatch,
326                       reserved, reset);
327         switch (rc) {
328         case 0:
329                 /* Anything left to do? */
330                 if (sch->vpm == sch->schib.pmcw.pam) {
331                         verify_done(cdev, sch->vpm == 0 ? -EACCES : 0);
332                         return;
333                 }
334                 /* Perform path-grouping. */
335                 spid_start(cdev);
336                 break;
337         case -EOPNOTSUPP:
338                 /* Path-grouping not supported. */
339                 cdev->private->flags.pgroup = 0;
340                 cdev->private->flags.mpath = 0;
341                 verify_start(cdev);
342                 break;
343         default:
344                 verify_done(cdev, rc);
345         }
346 }
347
348 /*
349  * Create channel program to perform a SENSE PGID on a single path.
350  */
351 static void snid_build_cp(struct ccw_device *cdev)
352 {
353         struct ccw_request *req = &cdev->private->req;
354         struct ccw1 *cp = cdev->private->iccws;
355         int i = 8 - ffs(req->lpm);
356
357         /* Channel program setup. */
358         cp->cmd_code    = CCW_CMD_SENSE_PGID;
359         cp->cda         = (u32) (addr_t) &cdev->private->pgid[i];
360         cp->count       = sizeof(struct pgid);
361         cp->flags       = CCW_FLAG_SLI;
362         req->cp         = cp;
363 }
364
365 /*
366  * Perform SENSE PGID on a single path.
367  */
368 static void snid_do(struct ccw_device *cdev)
369 {
370         struct subchannel *sch = to_subchannel(cdev->dev.parent);
371         struct ccw_request *req = &cdev->private->req;
372
373         /* Adjust lpm if paths are not set in pam. */
374         req->lpm = lpm_adjust(req->lpm, sch->schib.pmcw.pam);
375         if (!req->lpm)
376                 goto out_nopath;
377         snid_build_cp(cdev);
378         ccw_request_start(cdev);
379         return;
380
381 out_nopath:
382         snid_done(cdev, cdev->private->pgid_valid_mask ? 0 : -EACCES);
383 }
384
385 /*
386  * Process SENSE PGID request result for single path.
387  */
388 static void snid_callback(struct ccw_device *cdev, void *data, int rc)
389 {
390         struct ccw_request *req = &cdev->private->req;
391
392         if (rc == 0)
393                 cdev->private->pgid_valid_mask |= req->lpm;
394         else if (rc != -EACCES)
395                 goto err;
396         req->lpm >>= 1;
397         snid_do(cdev);
398         return;
399
400 err:
401         snid_done(cdev, rc);
402 }
403
404 /*
405  * Perform path verification.
406  */
407 static void verify_start(struct ccw_device *cdev)
408 {
409         struct subchannel *sch = to_subchannel(cdev->dev.parent);
410         struct ccw_request *req = &cdev->private->req;
411         struct ccw_dev_id *devid = &cdev->private->dev_id;
412
413         sch->vpm = 0;
414         /* Initialize request data. */
415         memset(req, 0, sizeof(*req));
416         req->timeout    = PGID_TIMEOUT;
417         req->maxretries = PGID_RETRIES;
418         req->lpm        = 0x80;
419         if (cdev->private->flags.pgroup) {
420                 CIO_TRACE_EVENT(4, "snid");
421                 CIO_HEX_EVENT(4, devid, sizeof(*devid));
422                 req->callback   = snid_callback;
423                 snid_do(cdev);
424         } else {
425                 CIO_TRACE_EVENT(4, "nop");
426                 CIO_HEX_EVENT(4, devid, sizeof(*devid));
427                 req->filter     = nop_filter;
428                 req->callback   = nop_callback;
429                 nop_do(cdev);
430         }
431 }
432
433 /**
434  * ccw_device_verify_start - perform path verification
435  * @cdev: ccw device
436  *
437  * Perform an I/O on each available channel path to @cdev to determine which
438  * paths are operational. The resulting path mask is stored in sch->vpm.
439  * If device options specify pathgrouping, establish a pathgroup for the
440  * operational paths. When finished, call ccw_device_verify_done with a
441  * return code specifying the result.
442  */
443 void ccw_device_verify_start(struct ccw_device *cdev)
444 {
445         CIO_TRACE_EVENT(4, "vrfy");
446         CIO_HEX_EVENT(4, &cdev->private->dev_id, sizeof(cdev->private->dev_id));
447         /* Initialize PGID data. */
448         memset(cdev->private->pgid, 0, sizeof(cdev->private->pgid));
449         cdev->private->pgid_valid_mask = 0;
450         /*
451          * Initialize pathgroup and multipath state with target values.
452          * They may change in the course of path verification.
453          */
454         cdev->private->flags.pgroup = cdev->private->options.pgroup;
455         cdev->private->flags.mpath = cdev->private->options.mpath;
456         cdev->private->flags.doverify = 0;
457         verify_start(cdev);
458 }
459
460 /*
461  * Process disband SET PGID request result.
462  */
463 static void disband_callback(struct ccw_device *cdev, void *data, int rc)
464 {
465         struct subchannel *sch = to_subchannel(cdev->dev.parent);
466         struct ccw_dev_id *id = &cdev->private->dev_id;
467
468         if (rc)
469                 goto out;
470         /* Ensure consistent multipathing state at device and channel. */
471         cdev->private->flags.mpath = 0;
472         if (sch->config.mp) {
473                 sch->config.mp = 0;
474                 rc = cio_commit_config(sch);
475         }
476 out:
477         CIO_MSG_EVENT(0, "disb: device 0.%x.%04x: rc=%d\n", id->ssid, id->devno,
478                       rc);
479         ccw_device_disband_done(cdev, rc);
480 }
481
482 /**
483  * ccw_device_disband_start - disband pathgroup
484  * @cdev: ccw device
485  *
486  * Execute a SET PGID channel program on @cdev to disband a previously
487  * established pathgroup. When finished, call ccw_device_disband_done with
488  * a return code specifying the result.
489  */
490 void ccw_device_disband_start(struct ccw_device *cdev)
491 {
492         struct subchannel *sch = to_subchannel(cdev->dev.parent);
493         struct ccw_request *req = &cdev->private->req;
494         u8 fn;
495
496         CIO_TRACE_EVENT(4, "disb");
497         CIO_HEX_EVENT(4, &cdev->private->dev_id, sizeof(cdev->private->dev_id));
498         /* Request setup. */
499         memset(req, 0, sizeof(*req));
500         req->timeout    = PGID_TIMEOUT;
501         req->maxretries = PGID_RETRIES;
502         req->lpm        = sch->schib.pmcw.pam & sch->opm;
503         req->callback   = disband_callback;
504         fn = SPID_FUNC_DISBAND;
505         if (cdev->private->flags.mpath)
506                 fn |= SPID_FUNC_MULTI_PATH;
507         spid_build_cp(cdev, fn);
508         ccw_request_start(cdev);
509 }