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