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