[SCSI] scsi_dh_alua: implement 'implied transition timeout'
[linux-2.6-block.git] / drivers / scsi / device_handler / scsi_dh_alua.c
CommitLineData
057ea7c9
HR
1/*
2 * Generic SCSI-3 ALUA SCSI Device Handler
3 *
69723d17 4 * Copyright (C) 2007-2010 Hannes Reinecke, SUSE Linux Products GmbH.
057ea7c9
HR
5 * All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 *
21 */
5a0e3ad6 22#include <linux/slab.h>
69723d17 23#include <linux/delay.h>
acf3368f 24#include <linux/module.h>
057ea7c9
HR
25#include <scsi/scsi.h>
26#include <scsi/scsi_eh.h>
27#include <scsi/scsi_dh.h>
28
29#define ALUA_DH_NAME "alua"
69723d17 30#define ALUA_DH_VER "1.3"
057ea7c9
HR
31
32#define TPGS_STATE_OPTIMIZED 0x0
33#define TPGS_STATE_NONOPTIMIZED 0x1
34#define TPGS_STATE_STANDBY 0x2
35#define TPGS_STATE_UNAVAILABLE 0x3
69723d17 36#define TPGS_STATE_LBA_DEPENDENT 0x4
057ea7c9
HR
37#define TPGS_STATE_OFFLINE 0xe
38#define TPGS_STATE_TRANSITIONING 0xf
39
40#define TPGS_SUPPORT_NONE 0x00
41#define TPGS_SUPPORT_OPTIMIZED 0x01
42#define TPGS_SUPPORT_NONOPTIMIZED 0x02
43#define TPGS_SUPPORT_STANDBY 0x04
44#define TPGS_SUPPORT_UNAVAILABLE 0x08
69723d17 45#define TPGS_SUPPORT_LBA_DEPENDENT 0x10
057ea7c9
HR
46#define TPGS_SUPPORT_OFFLINE 0x40
47#define TPGS_SUPPORT_TRANSITION 0x80
48
3588c5a2
RE
49#define RTPG_FMT_MASK 0x70
50#define RTPG_FMT_EXT_HDR 0x10
51
057ea7c9
HR
52#define TPGS_MODE_UNINITIALIZED -1
53#define TPGS_MODE_NONE 0x0
54#define TPGS_MODE_IMPLICIT 0x1
55#define TPGS_MODE_EXPLICIT 0x2
56
57#define ALUA_INQUIRY_SIZE 36
3588c5a2 58#define ALUA_FAILOVER_TIMEOUT 60
057ea7c9
HR
59#define ALUA_FAILOVER_RETRIES 5
60
4335d092
MB
61/* flags passed from user level */
62#define ALUA_OPTIMIZE_STPG 1
63
057ea7c9
HR
64struct alua_dh_data {
65 int group_id;
66 int rel_port;
67 int tpgs;
68 int state;
dcd3a754 69 int pref;
4335d092 70 unsigned flags; /* used for optimizing STPG */
057ea7c9
HR
71 unsigned char inq[ALUA_INQUIRY_SIZE];
72 unsigned char *buff;
73 int bufflen;
3588c5a2 74 unsigned char transition_tmo;
057ea7c9
HR
75 unsigned char sense[SCSI_SENSE_BUFFERSIZE];
76 int senselen;
96e65865
CS
77 struct scsi_device *sdev;
78 activate_complete callback_fn;
79 void *callback_data;
057ea7c9
HR
80};
81
82#define ALUA_POLICY_SWITCH_CURRENT 0
83#define ALUA_POLICY_SWITCH_ALL 1
84
96e65865
CS
85static char print_alua_state(int);
86static int alua_check_sense(struct scsi_device *, struct scsi_sense_hdr *);
87
057ea7c9
HR
88static inline struct alua_dh_data *get_alua_data(struct scsi_device *sdev)
89{
90 struct scsi_dh_data *scsi_dh_data = sdev->scsi_dh_data;
91 BUG_ON(scsi_dh_data == NULL);
92 return ((struct alua_dh_data *) scsi_dh_data->buf);
93}
94
95static int realloc_buffer(struct alua_dh_data *h, unsigned len)
96{
97 if (h->buff && h->buff != h->inq)
98 kfree(h->buff);
99
100 h->buff = kmalloc(len, GFP_NOIO);
101 if (!h->buff) {
102 h->buff = h->inq;
103 h->bufflen = ALUA_INQUIRY_SIZE;
104 return 1;
105 }
106 h->bufflen = len;
107 return 0;
108}
109
110static struct request *get_alua_req(struct scsi_device *sdev,
111 void *buffer, unsigned buflen, int rw)
112{
113 struct request *rq;
114 struct request_queue *q = sdev->request_queue;
115
116 rq = blk_get_request(q, rw, GFP_NOIO);
117
118 if (!rq) {
119 sdev_printk(KERN_INFO, sdev,
cadbd4a5 120 "%s: blk_get_request failed\n", __func__);
057ea7c9
HR
121 return NULL;
122 }
123
124 if (buflen && blk_rq_map_kern(q, rq, buffer, buflen, GFP_NOIO)) {
125 blk_put_request(rq);
126 sdev_printk(KERN_INFO, sdev,
cadbd4a5 127 "%s: blk_rq_map_kern failed\n", __func__);
057ea7c9
HR
128 return NULL;
129 }
130
131 rq->cmd_type = REQ_TYPE_BLOCK_PC;
6000a368 132 rq->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
64f84bc1 133 REQ_FAILFAST_DRIVER;
057ea7c9 134 rq->retries = ALUA_FAILOVER_RETRIES;
3588c5a2 135 rq->timeout = ALUA_FAILOVER_TIMEOUT * HZ;
057ea7c9
HR
136
137 return rq;
138}
139
057ea7c9
HR
140/*
141 * submit_vpd_inquiry - Issue an INQUIRY VPD page 0x83 command
142 * @sdev: sdev the command should be sent to
143 */
144static int submit_vpd_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
145{
146 struct request *rq;
147 int err = SCSI_DH_RES_TEMP_UNAVAIL;
148
149 rq = get_alua_req(sdev, h->buff, h->bufflen, READ);
150 if (!rq)
151 goto done;
152
153 /* Prepare the command. */
154 rq->cmd[0] = INQUIRY;
155 rq->cmd[1] = 1;
156 rq->cmd[2] = 0x83;
157 rq->cmd[4] = h->bufflen;
158 rq->cmd_len = COMMAND_SIZE(INQUIRY);
159
160 rq->sense = h->sense;
161 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
162 rq->sense_len = h->senselen = 0;
163
164 err = blk_execute_rq(rq->q, NULL, rq, 1);
165 if (err == -EIO) {
166 sdev_printk(KERN_INFO, sdev,
167 "%s: evpd inquiry failed with %x\n",
168 ALUA_DH_NAME, rq->errors);
169 h->senselen = rq->sense_len;
170 err = SCSI_DH_IO;
171 }
172 blk_put_request(rq);
173done:
174 return err;
175}
176
177/*
178 * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
179 * @sdev: sdev the command should be sent to
180 */
181static unsigned submit_rtpg(struct scsi_device *sdev, struct alua_dh_data *h)
182{
183 struct request *rq;
184 int err = SCSI_DH_RES_TEMP_UNAVAIL;
185
186 rq = get_alua_req(sdev, h->buff, h->bufflen, READ);
187 if (!rq)
188 goto done;
189
190 /* Prepare the command. */
191 rq->cmd[0] = MAINTENANCE_IN;
3588c5a2 192 rq->cmd[1] = MI_REPORT_TARGET_PGS | MI_EXT_HDR_PARAM_FMT;
057ea7c9
HR
193 rq->cmd[6] = (h->bufflen >> 24) & 0xff;
194 rq->cmd[7] = (h->bufflen >> 16) & 0xff;
195 rq->cmd[8] = (h->bufflen >> 8) & 0xff;
196 rq->cmd[9] = h->bufflen & 0xff;
197 rq->cmd_len = COMMAND_SIZE(MAINTENANCE_IN);
198
199 rq->sense = h->sense;
200 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
201 rq->sense_len = h->senselen = 0;
202
203 err = blk_execute_rq(rq->q, NULL, rq, 1);
204 if (err == -EIO) {
205 sdev_printk(KERN_INFO, sdev,
206 "%s: rtpg failed with %x\n",
207 ALUA_DH_NAME, rq->errors);
208 h->senselen = rq->sense_len;
209 err = SCSI_DH_IO;
210 }
211 blk_put_request(rq);
212done:
213 return err;
214}
215
96e65865
CS
216/*
217 * alua_stpg - Evaluate SET TARGET GROUP STATES
218 * @sdev: the device to be evaluated
219 * @state: the new target group state
220 *
221 * Send a SET TARGET GROUP STATES command to the device.
222 * We only have to test here if we should resubmit the command;
223 * any other error is assumed as a failure.
224 */
225static void stpg_endio(struct request *req, int error)
226{
227 struct alua_dh_data *h = req->end_io_data;
228 struct scsi_sense_hdr sense_hdr;
9349923d 229 unsigned err = SCSI_DH_OK;
96e65865
CS
230
231 if (error || host_byte(req->errors) != DID_OK ||
9349923d
JG
232 msg_byte(req->errors) != COMMAND_COMPLETE) {
233 err = SCSI_DH_IO;
96e65865 234 goto done;
9349923d 235 }
96e65865 236
9349923d 237 if (h->senselen > 0) {
96e65865
CS
238 err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
239 &sense_hdr);
240 if (!err) {
241 err = SCSI_DH_IO;
242 goto done;
243 }
244 err = alua_check_sense(h->sdev, &sense_hdr);
245 if (err == ADD_TO_MLQUEUE) {
246 err = SCSI_DH_RETRY;
247 goto done;
248 }
249 sdev_printk(KERN_INFO, h->sdev,
250 "%s: stpg sense code: %02x/%02x/%02x\n",
251 ALUA_DH_NAME, sense_hdr.sense_key,
252 sense_hdr.asc, sense_hdr.ascq);
253 err = SCSI_DH_IO;
254 }
255 if (err == SCSI_DH_OK) {
256 h->state = TPGS_STATE_OPTIMIZED;
257 sdev_printk(KERN_INFO, h->sdev,
258 "%s: port group %02x switched to state %c\n",
259 ALUA_DH_NAME, h->group_id,
260 print_alua_state(h->state));
261 }
262done:
ed0f36bc
JG
263 req->end_io_data = NULL;
264 __blk_put_request(req->q, req);
96e65865
CS
265 if (h->callback_fn) {
266 h->callback_fn(h->callback_data, err);
267 h->callback_fn = h->callback_data = NULL;
268 }
269 return;
270}
271
057ea7c9
HR
272/*
273 * submit_stpg - Issue a SET TARGET GROUP STATES command
057ea7c9
HR
274 *
275 * Currently we're only setting the current target port group state
276 * to 'active/optimized' and let the array firmware figure out
277 * the states of the remaining groups.
278 */
96e65865 279static unsigned submit_stpg(struct alua_dh_data *h)
057ea7c9
HR
280{
281 struct request *rq;
057ea7c9 282 int stpg_len = 8;
96e65865 283 struct scsi_device *sdev = h->sdev;
057ea7c9
HR
284
285 /* Prepare the data buffer */
286 memset(h->buff, 0, stpg_len);
287 h->buff[4] = TPGS_STATE_OPTIMIZED & 0x0f;
ef3fa8c6
IH
288 h->buff[6] = (h->group_id >> 8) & 0xff;
289 h->buff[7] = h->group_id & 0xff;
057ea7c9
HR
290
291 rq = get_alua_req(sdev, h->buff, stpg_len, WRITE);
292 if (!rq)
96e65865 293 return SCSI_DH_RES_TEMP_UNAVAIL;
057ea7c9
HR
294
295 /* Prepare the command. */
296 rq->cmd[0] = MAINTENANCE_OUT;
297 rq->cmd[1] = MO_SET_TARGET_PGS;
298 rq->cmd[6] = (stpg_len >> 24) & 0xff;
299 rq->cmd[7] = (stpg_len >> 16) & 0xff;
300 rq->cmd[8] = (stpg_len >> 8) & 0xff;
301 rq->cmd[9] = stpg_len & 0xff;
302 rq->cmd_len = COMMAND_SIZE(MAINTENANCE_OUT);
303
304 rq->sense = h->sense;
305 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
306 rq->sense_len = h->senselen = 0;
96e65865 307 rq->end_io_data = h;
057ea7c9 308
96e65865 309 blk_execute_rq_nowait(rq->q, NULL, rq, 1, stpg_endio);
7c66e9a5 310 return SCSI_DH_OK;
057ea7c9
HR
311}
312
313/*
d7c48feb 314 * alua_check_tpgs - Evaluate TPGS setting
057ea7c9
HR
315 * @sdev: device to be checked
316 *
d7c48feb 317 * Examine the TPGS setting of the sdev to find out if ALUA
057ea7c9
HR
318 * is supported.
319 */
d7c48feb 320static int alua_check_tpgs(struct scsi_device *sdev, struct alua_dh_data *h)
057ea7c9 321{
d7c48feb 322 int err = SCSI_DH_OK;
057ea7c9 323
d7c48feb 324 h->tpgs = scsi_device_tpgs(sdev);
057ea7c9
HR
325 switch (h->tpgs) {
326 case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT:
327 sdev_printk(KERN_INFO, sdev,
328 "%s: supports implicit and explicit TPGS\n",
329 ALUA_DH_NAME);
330 break;
331 case TPGS_MODE_EXPLICIT:
332 sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n",
333 ALUA_DH_NAME);
334 break;
335 case TPGS_MODE_IMPLICIT:
336 sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n",
337 ALUA_DH_NAME);
338 break;
339 default:
340 h->tpgs = TPGS_MODE_NONE;
341 sdev_printk(KERN_INFO, sdev, "%s: not supported\n",
342 ALUA_DH_NAME);
343 err = SCSI_DH_DEV_UNSUPP;
344 break;
345 }
346
347 return err;
348}
349
350/*
351 * alua_vpd_inquiry - Evaluate INQUIRY vpd page 0x83
352 * @sdev: device to be checked
353 *
354 * Extract the relative target port and the target port group
355 * descriptor from the list of identificators.
356 */
357static int alua_vpd_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
358{
359 int len;
360 unsigned err;
361 unsigned char *d;
362
363 retry:
364 err = submit_vpd_inquiry(sdev, h);
365
366 if (err != SCSI_DH_OK)
367 return err;
368
369 /* Check if vpd page exceeds initial buffer */
370 len = (h->buff[2] << 8) + h->buff[3] + 4;
371 if (len > h->bufflen) {
372 /* Resubmit with the correct length */
373 if (realloc_buffer(h, len)) {
374 sdev_printk(KERN_WARNING, sdev,
375 "%s: kmalloc buffer failed\n",
376 ALUA_DH_NAME);
377 /* Temporary failure, bypass */
378 return SCSI_DH_DEV_TEMP_BUSY;
379 }
380 goto retry;
381 }
382
383 /*
384 * Now look for the correct descriptor.
385 */
386 d = h->buff + 4;
387 while (d < h->buff + len) {
388 switch (d[1] & 0xf) {
389 case 0x4:
390 /* Relative target port */
391 h->rel_port = (d[6] << 8) + d[7];
392 break;
393 case 0x5:
394 /* Target port group */
395 h->group_id = (d[6] << 8) + d[7];
396 break;
397 default:
398 break;
399 }
400 d += d[3] + 4;
401 }
402
403 if (h->group_id == -1) {
404 /*
405 * Internal error; TPGS supported but required
406 * VPD identification descriptors not present.
407 * Disable ALUA support
408 */
409 sdev_printk(KERN_INFO, sdev,
410 "%s: No target port descriptors found\n",
411 ALUA_DH_NAME);
412 h->state = TPGS_STATE_OPTIMIZED;
413 h->tpgs = TPGS_MODE_NONE;
414 err = SCSI_DH_DEV_UNSUPP;
415 } else {
416 sdev_printk(KERN_INFO, sdev,
417 "%s: port group %02x rel port %02x\n",
418 ALUA_DH_NAME, h->group_id, h->rel_port);
419 }
420
421 return err;
422}
423
424static char print_alua_state(int state)
425{
426 switch (state) {
427 case TPGS_STATE_OPTIMIZED:
428 return 'A';
429 case TPGS_STATE_NONOPTIMIZED:
430 return 'N';
431 case TPGS_STATE_STANDBY:
432 return 'S';
433 case TPGS_STATE_UNAVAILABLE:
434 return 'U';
69723d17
HR
435 case TPGS_STATE_LBA_DEPENDENT:
436 return 'L';
057ea7c9
HR
437 case TPGS_STATE_OFFLINE:
438 return 'O';
439 case TPGS_STATE_TRANSITIONING:
440 return 'T';
441 default:
442 return 'X';
443 }
444}
445
446static int alua_check_sense(struct scsi_device *sdev,
447 struct scsi_sense_hdr *sense_hdr)
448{
449 switch (sense_hdr->sense_key) {
450 case NOT_READY:
451 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a)
452 /*
453 * LUN Not Accessible - ALUA state transition
454 */
c7dbb627 455 return ADD_TO_MLQUEUE;
057ea7c9
HR
456 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0b)
457 /*
458 * LUN Not Accessible -- Target port in standby state
459 */
460 return SUCCESS;
461 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0c)
462 /*
463 * LUN Not Accessible -- Target port in unavailable state
464 */
465 return SUCCESS;
466 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x12)
467 /*
468 * LUN Not Ready -- Offline
469 */
470 return SUCCESS;
471 break;
472 case UNIT_ATTENTION:
473 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
474 /*
475 * Power On, Reset, or Bus Device Reset, just retry.
476 */
c7dbb627 477 return ADD_TO_MLQUEUE;
410f02d8
MB
478 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x01)
479 /*
480 * Mode Parameters Changed
481 */
482 return ADD_TO_MLQUEUE;
bf81973a 483 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06)
057ea7c9
HR
484 /*
485 * ALUA state changed
486 */
c7dbb627 487 return ADD_TO_MLQUEUE;
bf81973a 488 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07)
057ea7c9
HR
489 /*
490 * Implicit ALUA state transition failed
491 */
c7dbb627 492 return ADD_TO_MLQUEUE;
bf81973a
MB
493 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x03)
494 /*
495 * Inquiry data has changed
496 */
497 return ADD_TO_MLQUEUE;
498 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e)
4d086f6b
IH
499 /*
500 * REPORTED_LUNS_DATA_HAS_CHANGED is reported
501 * when switching controllers on targets like
502 * Intel Multi-Flex. We can just retry.
503 */
504 return ADD_TO_MLQUEUE;
057ea7c9
HR
505 break;
506 }
507
508 return SCSI_RETURN_NOT_HANDLED;
509}
510
057ea7c9
HR
511/*
512 * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
513 * @sdev: the device to be evaluated.
514 *
515 * Evaluate the Target Port Group State.
516 * Returns SCSI_DH_DEV_OFFLINED if the path is
25985edc 517 * found to be unusable.
057ea7c9
HR
518 */
519static int alua_rtpg(struct scsi_device *sdev, struct alua_dh_data *h)
520{
521 struct scsi_sense_hdr sense_hdr;
522 int len, k, off, valid_states = 0;
cfde3fa1 523 unsigned char *ucp;
057ea7c9 524 unsigned err;
ab72002b 525 unsigned long expiry, interval = 1000;
3588c5a2
RE
526 unsigned int tpg_desc_tbl_off;
527 unsigned char orig_transition_tmo;
528
529 if (!h->transition_tmo)
530 expiry = round_jiffies_up(jiffies + ALUA_FAILOVER_TIMEOUT * HZ);
531 else
532 expiry = round_jiffies_up(jiffies + h->transition_tmo * HZ);
057ea7c9
HR
533
534 retry:
535 err = submit_rtpg(sdev, h);
536
537 if (err == SCSI_DH_IO && h->senselen > 0) {
538 err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
539 &sense_hdr);
540 if (!err)
541 return SCSI_DH_IO;
542
543 err = alua_check_sense(sdev, &sense_hdr);
69723d17 544 if (err == ADD_TO_MLQUEUE && time_before(jiffies, expiry))
057ea7c9
HR
545 goto retry;
546 sdev_printk(KERN_INFO, sdev,
547 "%s: rtpg sense code %02x/%02x/%02x\n",
548 ALUA_DH_NAME, sense_hdr.sense_key,
549 sense_hdr.asc, sense_hdr.ascq);
550 err = SCSI_DH_IO;
551 }
552 if (err != SCSI_DH_OK)
553 return err;
554
555 len = (h->buff[0] << 24) + (h->buff[1] << 16) +
556 (h->buff[2] << 8) + h->buff[3] + 4;
557
558 if (len > h->bufflen) {
559 /* Resubmit with the correct length */
560 if (realloc_buffer(h, len)) {
561 sdev_printk(KERN_WARNING, sdev,
cadbd4a5 562 "%s: kmalloc buffer failed\n",__func__);
057ea7c9
HR
563 /* Temporary failure, bypass */
564 return SCSI_DH_DEV_TEMP_BUSY;
565 }
566 goto retry;
567 }
568
3588c5a2
RE
569 orig_transition_tmo = h->transition_tmo;
570 if ((h->buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR && h->buff[5] != 0)
571 h->transition_tmo = h->buff[5];
572 else
573 h->transition_tmo = ALUA_FAILOVER_TIMEOUT;
574
575 if (orig_transition_tmo != h->transition_tmo) {
576 sdev_printk(KERN_INFO, sdev,
577 "%s: transition timeout set to %d seconds\n",
578 ALUA_DH_NAME, h->transition_tmo);
579 expiry = jiffies + h->transition_tmo * HZ;
580 }
581
582 if ((h->buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR)
583 tpg_desc_tbl_off = 8;
584 else
585 tpg_desc_tbl_off = 4;
586
587 for (k = tpg_desc_tbl_off, ucp = h->buff + tpg_desc_tbl_off;
588 k < len;
589 k += off, ucp += off) {
590
057ea7c9
HR
591 if (h->group_id == (ucp[2] << 8) + ucp[3]) {
592 h->state = ucp[0] & 0x0f;
dcd3a754 593 h->pref = ucp[0] >> 7;
057ea7c9
HR
594 valid_states = ucp[1];
595 }
596 off = 8 + (ucp[7] * 4);
597 }
598
599 sdev_printk(KERN_INFO, sdev,
dcd3a754 600 "%s: port group %02x state %c %s supports %c%c%c%c%c%c%c\n",
057ea7c9 601 ALUA_DH_NAME, h->group_id, print_alua_state(h->state),
dcd3a754 602 h->pref ? "preferred" : "non-preferred",
057ea7c9
HR
603 valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
604 valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
69723d17 605 valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
057ea7c9
HR
606 valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u',
607 valid_states&TPGS_SUPPORT_STANDBY?'S':'s',
608 valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n',
609 valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a');
610
69723d17
HR
611 switch (h->state) {
612 case TPGS_STATE_TRANSITIONING:
613 if (time_before(jiffies, expiry)) {
057ea7c9 614 /* State transition, retry */
ebd1f645 615 interval *= 2;
69723d17 616 msleep(interval);
057ea7c9 617 goto retry;
057ea7c9 618 }
69723d17
HR
619 /* Transitioning time exceeded, set port to standby */
620 err = SCSI_DH_RETRY;
621 h->state = TPGS_STATE_STANDBY;
622 break;
623 case TPGS_STATE_OFFLINE:
624 case TPGS_STATE_UNAVAILABLE:
25985edc 625 /* Path unusable for unavailable/offline */
69723d17
HR
626 err = SCSI_DH_DEV_OFFLINED;
627 break;
628 default:
629 /* Useable path if active */
630 err = SCSI_DH_OK;
631 break;
057ea7c9
HR
632 }
633 return err;
634}
635
636/*
637 * alua_initialize - Initialize ALUA state
638 * @sdev: the device to be initialized
639 *
640 * For the prep_fn to work correctly we have
641 * to initialize the ALUA state for the device.
642 */
643static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
644{
645 int err;
646
d7c48feb 647 err = alua_check_tpgs(sdev, h);
057ea7c9
HR
648 if (err != SCSI_DH_OK)
649 goto out;
650
651 err = alua_vpd_inquiry(sdev, h);
652 if (err != SCSI_DH_OK)
653 goto out;
654
655 err = alua_rtpg(sdev, h);
656 if (err != SCSI_DH_OK)
657 goto out;
658
659out:
660 return err;
661}
4335d092
MB
662/*
663 * alua_set_params - set/unset the optimize flag
664 * @sdev: device on the path to be activated
665 * params - parameters in the following format
666 * "no_of_params\0param1\0param2\0param3\0...\0"
667 * For example, to set the flag pass the following parameters
668 * from multipath.conf
669 * hardware_handler "2 alua 1"
670 */
671static int alua_set_params(struct scsi_device *sdev, const char *params)
672{
673 struct alua_dh_data *h = get_alua_data(sdev);
674 unsigned int optimize = 0, argc;
675 const char *p = params;
676 int result = SCSI_DH_OK;
677
678 if ((sscanf(params, "%u", &argc) != 1) || (argc != 1))
679 return -EINVAL;
680
681 while (*p++)
682 ;
683 if ((sscanf(p, "%u", &optimize) != 1) || (optimize > 1))
684 return -EINVAL;
685
686 if (optimize)
687 h->flags |= ALUA_OPTIMIZE_STPG;
688 else
689 h->flags &= ~ALUA_OPTIMIZE_STPG;
690
691 return result;
692}
057ea7c9
HR
693
694/*
695 * alua_activate - activate a path
696 * @sdev: device on the path to be activated
697 *
698 * We're currently switching the port group to be activated only and
699 * let the array figure out the rest.
700 * There may be other arrays which require us to switch all port groups
701 * based on a certain policy. But until we actually encounter them it
702 * should be okay.
703 */
3ae31f6a
CS
704static int alua_activate(struct scsi_device *sdev,
705 activate_complete fn, void *data)
057ea7c9
HR
706{
707 struct alua_dh_data *h = get_alua_data(sdev);
708 int err = SCSI_DH_OK;
72d9e0f3 709 int stpg = 0;
057ea7c9 710
46ccf6b5
HR
711 err = alua_rtpg(sdev, h);
712 if (err != SCSI_DH_OK)
713 goto out;
057ea7c9 714
72d9e0f3
MB
715 if (h->tpgs & TPGS_MODE_EXPLICIT) {
716 switch (h->state) {
717 case TPGS_STATE_NONOPTIMIZED:
718 stpg = 1;
719 if ((h->flags & ALUA_OPTIMIZE_STPG) &&
720 (!h->pref) &&
721 (h->tpgs & TPGS_MODE_IMPLICIT))
722 stpg = 0;
723 break;
724 case TPGS_STATE_STANDBY:
725 stpg = 1;
726 break;
727 case TPGS_STATE_UNAVAILABLE:
728 case TPGS_STATE_OFFLINE:
729 err = SCSI_DH_IO;
730 break;
731 case TPGS_STATE_TRANSITIONING:
732 err = SCSI_DH_RETRY;
733 break;
734 default:
735 break;
736 }
737 }
738
739 if (stpg) {
96e65865
CS
740 h->callback_fn = fn;
741 h->callback_data = data;
742 err = submit_stpg(h);
743 if (err == SCSI_DH_OK)
744 return 0;
745 h->callback_fn = h->callback_data = NULL;
746 }
057ea7c9
HR
747
748out:
3ae31f6a
CS
749 if (fn)
750 fn(data, err);
751 return 0;
057ea7c9
HR
752}
753
754/*
755 * alua_prep_fn - request callback
756 *
757 * Fail I/O to all paths not in state
758 * active/optimized or active/non-optimized.
759 */
760static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
761{
762 struct alua_dh_data *h = get_alua_data(sdev);
763 int ret = BLKPREP_OK;
764
69723d17
HR
765 if (h->state == TPGS_STATE_TRANSITIONING)
766 ret = BLKPREP_DEFER;
767 else if (h->state != TPGS_STATE_OPTIMIZED &&
768 h->state != TPGS_STATE_NONOPTIMIZED &&
769 h->state != TPGS_STATE_LBA_DEPENDENT) {
057ea7c9
HR
770 ret = BLKPREP_KILL;
771 req->cmd_flags |= REQ_QUIET;
772 }
773 return ret;
774
775}
776
6c3633d0
HR
777static bool alua_match(struct scsi_device *sdev)
778{
779 return (scsi_device_tpgs(sdev) != 0);
780}
057ea7c9
HR
781
782static int alua_bus_attach(struct scsi_device *sdev);
783static void alua_bus_detach(struct scsi_device *sdev);
784
785static struct scsi_device_handler alua_dh = {
786 .name = ALUA_DH_NAME,
787 .module = THIS_MODULE,
057ea7c9
HR
788 .attach = alua_bus_attach,
789 .detach = alua_bus_detach,
790 .prep_fn = alua_prep_fn,
791 .check_sense = alua_check_sense,
792 .activate = alua_activate,
4335d092 793 .set_params = alua_set_params,
6c3633d0 794 .match = alua_match,
057ea7c9
HR
795};
796
797/*
798 * alua_bus_attach - Attach device handler
799 * @sdev: device to be attached to
800 */
801static int alua_bus_attach(struct scsi_device *sdev)
802{
803 struct scsi_dh_data *scsi_dh_data;
804 struct alua_dh_data *h;
805 unsigned long flags;
806 int err = SCSI_DH_OK;
807
9dfeb315 808 scsi_dh_data = kzalloc(sizeof(*scsi_dh_data)
057ea7c9
HR
809 + sizeof(*h) , GFP_KERNEL);
810 if (!scsi_dh_data) {
811 sdev_printk(KERN_ERR, sdev, "%s: Attach failed\n",
812 ALUA_DH_NAME);
813 return -ENOMEM;
814 }
815
816 scsi_dh_data->scsi_dh = &alua_dh;
817 h = (struct alua_dh_data *) scsi_dh_data->buf;
818 h->tpgs = TPGS_MODE_UNINITIALIZED;
819 h->state = TPGS_STATE_OPTIMIZED;
820 h->group_id = -1;
821 h->rel_port = -1;
822 h->buff = h->inq;
823 h->bufflen = ALUA_INQUIRY_SIZE;
96e65865 824 h->sdev = sdev;
057ea7c9
HR
825
826 err = alua_initialize(sdev, h);
c0d289b3 827 if ((err != SCSI_DH_OK) && (err != SCSI_DH_DEV_OFFLINED))
057ea7c9
HR
828 goto failed;
829
830 if (!try_module_get(THIS_MODULE))
831 goto failed;
832
833 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
834 sdev->scsi_dh_data = scsi_dh_data;
835 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
ab72002b 836 sdev_printk(KERN_NOTICE, sdev, "%s: Attached\n", ALUA_DH_NAME);
057ea7c9
HR
837
838 return 0;
839
840failed:
841 kfree(scsi_dh_data);
842 sdev_printk(KERN_ERR, sdev, "%s: not attached\n", ALUA_DH_NAME);
843 return -EINVAL;
844}
845
846/*
847 * alua_bus_detach - Detach device handler
848 * @sdev: device to be detached from
849 */
850static void alua_bus_detach(struct scsi_device *sdev)
851{
852 struct scsi_dh_data *scsi_dh_data;
853 struct alua_dh_data *h;
854 unsigned long flags;
855
856 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
857 scsi_dh_data = sdev->scsi_dh_data;
858 sdev->scsi_dh_data = NULL;
859 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
860
861 h = (struct alua_dh_data *) scsi_dh_data->buf;
862 if (h->buff && h->inq != h->buff)
863 kfree(h->buff);
864 kfree(scsi_dh_data);
865 module_put(THIS_MODULE);
866 sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", ALUA_DH_NAME);
867}
868
869static int __init alua_init(void)
870{
871 int r;
872
873 r = scsi_register_device_handler(&alua_dh);
874 if (r != 0)
875 printk(KERN_ERR "%s: Failed to register scsi device handler",
876 ALUA_DH_NAME);
877 return r;
878}
879
880static void __exit alua_exit(void)
881{
882 scsi_unregister_device_handler(&alua_dh);
883}
884
885module_init(alua_init);
886module_exit(alua_exit);
887
888MODULE_DESCRIPTION("DM Multipath ALUA support");
889MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
890MODULE_LICENSE("GPL");
891MODULE_VERSION(ALUA_DH_VER);