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