blob: 627f4b5e5176b16e9cc969c0b98ac80a85c4afb1 [file] [log] [blame]
Hannes Reinecke057ea7c2008-07-17 16:53:21 -07001/*
2 * Generic SCSI-3 ALUA SCSI Device Handler
3 *
Hannes Reinecke69723d12010-09-24 15:57:04 +02004 * Copyright (C) 2007-2010 Hannes Reinecke, SUSE Linux Products GmbH.
Hannes Reinecke057ea7c2008-07-17 16:53:21 -07005 * 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 */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Hannes Reinecke69723d12010-09-24 15:57:04 +020023#include <linux/delay.h>
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070024#include <scsi/scsi.h>
25#include <scsi/scsi_eh.h>
26#include <scsi/scsi_dh.h>
27
28#define ALUA_DH_NAME "alua"
Hannes Reinecke69723d12010-09-24 15:57:04 +020029#define ALUA_DH_VER "1.3"
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070030
31#define TPGS_STATE_OPTIMIZED 0x0
32#define TPGS_STATE_NONOPTIMIZED 0x1
33#define TPGS_STATE_STANDBY 0x2
34#define TPGS_STATE_UNAVAILABLE 0x3
Hannes Reinecke69723d12010-09-24 15:57:04 +020035#define TPGS_STATE_LBA_DEPENDENT 0x4
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070036#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
Hannes Reinecke69723d12010-09-24 15:57:04 +020044#define TPGS_SUPPORT_LBA_DEPENDENT 0x10
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070045#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;
Chandra Seetharaman96e65862009-10-21 09:23:04 -070067 struct scsi_device *sdev;
68 activate_complete callback_fn;
69 void *callback_data;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070070};
71
72#define ALUA_POLICY_SWITCH_CURRENT 0
73#define ALUA_POLICY_SWITCH_ALL 1
74
Chandra Seetharaman96e65862009-10-21 09:23:04 -070075static char print_alua_state(int);
76static int alua_check_sense(struct scsi_device *, struct scsi_sense_hdr *);
77
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070078static 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,
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700110 "%s: blk_get_request failed\n", __func__);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700111 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,
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700117 "%s: blk_rq_map_kern failed\n", __func__);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700118 return NULL;
119 }
120
121 rq->cmd_type = REQ_TYPE_BLOCK_PC;
Mike Christie6000a362008-08-19 18:45:30 -0500122 rq->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
Mike Christie64f84bc2008-09-06 08:39:16 -0500123 REQ_FAILFAST_DRIVER;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700124 rq->retries = ALUA_FAILOVER_RETRIES;
125 rq->timeout = ALUA_FAILOVER_TIMEOUT;
126
127 return rq;
128}
129
130/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700131 * 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
206/*
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700207 * 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;
Joseph Gruher93499232011-01-05 16:00:22 -0500219 unsigned err = SCSI_DH_OK;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700220
221 if (error || host_byte(req->errors) != DID_OK ||
Joseph Gruher93499232011-01-05 16:00:22 -0500222 msg_byte(req->errors) != COMMAND_COMPLETE) {
223 err = SCSI_DH_IO;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700224 goto done;
Joseph Gruher93499232011-01-05 16:00:22 -0500225 }
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700226
Joseph Gruher93499232011-01-05 16:00:22 -0500227 if (h->senselen > 0) {
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700228 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:
Joseph Gruhered0f36b2011-01-05 16:00:21 -0500253 req->end_io_data = NULL;
254 __blk_put_request(req->q, req);
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700255 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
262/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700263 * submit_stpg - Issue a SET TARGET GROUP STATES command
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700264 *
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 */
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700269static unsigned submit_stpg(struct alua_dh_data *h)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700270{
271 struct request *rq;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700272 int stpg_len = 8;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700273 struct scsi_device *sdev = h->sdev;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700274
275 /* Prepare the data buffer */
276 memset(h->buff, 0, stpg_len);
277 h->buff[4] = TPGS_STATE_OPTIMIZED & 0x0f;
Ilgu Hongef3fa8c2009-01-30 17:00:09 -0600278 h->buff[6] = (h->group_id >> 8) & 0xff;
279 h->buff[7] = h->group_id & 0xff;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700280
281 rq = get_alua_req(sdev, h->buff, stpg_len, WRITE);
282 if (!rq)
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700283 return SCSI_DH_RES_TEMP_UNAVAIL;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700284
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;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700297 rq->end_io_data = h;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700298
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700299 blk_execute_rq_nowait(rq->q, NULL, rq, 1, stpg_endio);
Joseph Gruher7c66e9a2011-01-05 16:00:20 -0500300 return SCSI_DH_OK;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700301}
302
303/*
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200304 * alua_check_tpgs - Evaluate TPGS setting
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700305 * @sdev: device to be checked
306 *
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200307 * Examine the TPGS setting of the sdev to find out if ALUA
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700308 * is supported.
309 */
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200310static int alua_check_tpgs(struct scsi_device *sdev, struct alua_dh_data *h)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700311{
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200312 int err = SCSI_DH_OK;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700313
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200314 h->tpgs = scsi_device_tpgs(sdev);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700315 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';
Hannes Reinecke69723d12010-09-24 15:57:04 +0200425 case TPGS_STATE_LBA_DEPENDENT:
426 return 'L';
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700427 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 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700445 return ADD_TO_MLQUEUE;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700446 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 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700467 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400468 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700469 /*
470 * ALUA state changed
471 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700472 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400473 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700474 /*
475 * Implicit ALUA state transition failed
476 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700477 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400478 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x03)
479 /*
480 * Inquiry data has changed
481 */
482 return ADD_TO_MLQUEUE;
483 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e)
Ilgu Hong4d086f62009-01-30 17:00:11 -0600484 /*
485 * REPORTED_LUNS_DATA_HAS_CHANGED is reported
486 * when switching controllers on targets like
487 * Intel Multi-Flex. We can just retry.
488 */
489 return ADD_TO_MLQUEUE;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700490 break;
491 }
492
493 return SCSI_RETURN_NOT_HANDLED;
494}
495
496/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700497 * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
498 * @sdev: the device to be evaluated.
499 *
500 * Evaluate the Target Port Group State.
501 * Returns SCSI_DH_DEV_OFFLINED if the path is
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300502 * found to be unusable.
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700503 */
504static int alua_rtpg(struct scsi_device *sdev, struct alua_dh_data *h)
505{
506 struct scsi_sense_hdr sense_hdr;
507 int len, k, off, valid_states = 0;
Hannes Reineckecfde3fa2011-08-24 10:51:18 +0200508 unsigned char *ucp;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700509 unsigned err;
Hannes Reineckeebd1f642011-08-24 10:51:19 +0200510 unsigned long expiry, interval = 1;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700511
Hannes Reinecke69723d12010-09-24 15:57:04 +0200512 expiry = round_jiffies_up(jiffies + ALUA_FAILOVER_TIMEOUT);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700513 retry:
514 err = submit_rtpg(sdev, h);
515
516 if (err == SCSI_DH_IO && h->senselen > 0) {
517 err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
518 &sense_hdr);
519 if (!err)
520 return SCSI_DH_IO;
521
522 err = alua_check_sense(sdev, &sense_hdr);
Hannes Reinecke69723d12010-09-24 15:57:04 +0200523 if (err == ADD_TO_MLQUEUE && time_before(jiffies, expiry))
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700524 goto retry;
525 sdev_printk(KERN_INFO, sdev,
526 "%s: rtpg sense code %02x/%02x/%02x\n",
527 ALUA_DH_NAME, sense_hdr.sense_key,
528 sense_hdr.asc, sense_hdr.ascq);
529 err = SCSI_DH_IO;
530 }
531 if (err != SCSI_DH_OK)
532 return err;
533
534 len = (h->buff[0] << 24) + (h->buff[1] << 16) +
535 (h->buff[2] << 8) + h->buff[3] + 4;
536
537 if (len > h->bufflen) {
538 /* Resubmit with the correct length */
539 if (realloc_buffer(h, len)) {
540 sdev_printk(KERN_WARNING, sdev,
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700541 "%s: kmalloc buffer failed\n",__func__);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700542 /* Temporary failure, bypass */
543 return SCSI_DH_DEV_TEMP_BUSY;
544 }
545 goto retry;
546 }
547
548 for (k = 4, ucp = h->buff + 4; k < len; k += off, ucp += off) {
549 if (h->group_id == (ucp[2] << 8) + ucp[3]) {
550 h->state = ucp[0] & 0x0f;
551 valid_states = ucp[1];
552 }
553 off = 8 + (ucp[7] * 4);
554 }
555
556 sdev_printk(KERN_INFO, sdev,
Hannes Reinecke69723d12010-09-24 15:57:04 +0200557 "%s: port group %02x state %c supports %c%c%c%c%c%c%c\n",
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700558 ALUA_DH_NAME, h->group_id, print_alua_state(h->state),
559 valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
560 valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
Hannes Reinecke69723d12010-09-24 15:57:04 +0200561 valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700562 valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u',
563 valid_states&TPGS_SUPPORT_STANDBY?'S':'s',
564 valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n',
565 valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a');
566
Hannes Reinecke69723d12010-09-24 15:57:04 +0200567 switch (h->state) {
568 case TPGS_STATE_TRANSITIONING:
569 if (time_before(jiffies, expiry)) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700570 /* State transition, retry */
Hannes Reineckeebd1f642011-08-24 10:51:19 +0200571 interval *= 2;
Hannes Reinecke69723d12010-09-24 15:57:04 +0200572 msleep(interval);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700573 goto retry;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700574 }
Hannes Reinecke69723d12010-09-24 15:57:04 +0200575 /* Transitioning time exceeded, set port to standby */
576 err = SCSI_DH_RETRY;
577 h->state = TPGS_STATE_STANDBY;
578 break;
579 case TPGS_STATE_OFFLINE:
580 case TPGS_STATE_UNAVAILABLE:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300581 /* Path unusable for unavailable/offline */
Hannes Reinecke69723d12010-09-24 15:57:04 +0200582 err = SCSI_DH_DEV_OFFLINED;
583 break;
584 default:
585 /* Useable path if active */
586 err = SCSI_DH_OK;
587 break;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700588 }
589 return err;
590}
591
592/*
593 * alua_initialize - Initialize ALUA state
594 * @sdev: the device to be initialized
595 *
596 * For the prep_fn to work correctly we have
597 * to initialize the ALUA state for the device.
598 */
599static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
600{
601 int err;
602
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200603 err = alua_check_tpgs(sdev, h);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700604 if (err != SCSI_DH_OK)
605 goto out;
606
607 err = alua_vpd_inquiry(sdev, h);
608 if (err != SCSI_DH_OK)
609 goto out;
610
611 err = alua_rtpg(sdev, h);
612 if (err != SCSI_DH_OK)
613 goto out;
614
615out:
616 return err;
617}
618
619/*
620 * alua_activate - activate a path
621 * @sdev: device on the path to be activated
622 *
623 * We're currently switching the port group to be activated only and
624 * let the array figure out the rest.
625 * There may be other arrays which require us to switch all port groups
626 * based on a certain policy. But until we actually encounter them it
627 * should be okay.
628 */
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700629static int alua_activate(struct scsi_device *sdev,
630 activate_complete fn, void *data)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700631{
632 struct alua_dh_data *h = get_alua_data(sdev);
633 int err = SCSI_DH_OK;
634
Hannes Reinecke46ccf6b2011-08-24 10:51:16 +0200635 err = alua_rtpg(sdev, h);
636 if (err != SCSI_DH_OK)
637 goto out;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700638
Hannes Reinecke69723d12010-09-24 15:57:04 +0200639 if (h->tpgs & TPGS_MODE_EXPLICIT &&
640 h->state != TPGS_STATE_OPTIMIZED &&
641 h->state != TPGS_STATE_LBA_DEPENDENT) {
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700642 h->callback_fn = fn;
643 h->callback_data = data;
644 err = submit_stpg(h);
645 if (err == SCSI_DH_OK)
646 return 0;
647 h->callback_fn = h->callback_data = NULL;
648 }
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700649
650out:
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700651 if (fn)
652 fn(data, err);
653 return 0;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700654}
655
656/*
657 * alua_prep_fn - request callback
658 *
659 * Fail I/O to all paths not in state
660 * active/optimized or active/non-optimized.
661 */
662static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
663{
664 struct alua_dh_data *h = get_alua_data(sdev);
665 int ret = BLKPREP_OK;
666
Hannes Reinecke69723d12010-09-24 15:57:04 +0200667 if (h->state == TPGS_STATE_TRANSITIONING)
668 ret = BLKPREP_DEFER;
669 else if (h->state != TPGS_STATE_OPTIMIZED &&
670 h->state != TPGS_STATE_NONOPTIMIZED &&
671 h->state != TPGS_STATE_LBA_DEPENDENT) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700672 ret = BLKPREP_KILL;
673 req->cmd_flags |= REQ_QUIET;
674 }
675 return ret;
676
677}
678
Hannes Reinecke6c3633d2011-08-24 10:51:15 +0200679static bool alua_match(struct scsi_device *sdev)
680{
681 return (scsi_device_tpgs(sdev) != 0);
682}
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700683
684static int alua_bus_attach(struct scsi_device *sdev);
685static void alua_bus_detach(struct scsi_device *sdev);
686
687static struct scsi_device_handler alua_dh = {
688 .name = ALUA_DH_NAME,
689 .module = THIS_MODULE,
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700690 .attach = alua_bus_attach,
691 .detach = alua_bus_detach,
692 .prep_fn = alua_prep_fn,
693 .check_sense = alua_check_sense,
694 .activate = alua_activate,
Hannes Reinecke6c3633d2011-08-24 10:51:15 +0200695 .match = alua_match,
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700696};
697
698/*
699 * alua_bus_attach - Attach device handler
700 * @sdev: device to be attached to
701 */
702static int alua_bus_attach(struct scsi_device *sdev)
703{
704 struct scsi_dh_data *scsi_dh_data;
705 struct alua_dh_data *h;
706 unsigned long flags;
707 int err = SCSI_DH_OK;
708
Hillf Danton9dfeb312011-02-11 15:17:33 -0700709 scsi_dh_data = kzalloc(sizeof(*scsi_dh_data)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700710 + sizeof(*h) , GFP_KERNEL);
711 if (!scsi_dh_data) {
712 sdev_printk(KERN_ERR, sdev, "%s: Attach failed\n",
713 ALUA_DH_NAME);
714 return -ENOMEM;
715 }
716
717 scsi_dh_data->scsi_dh = &alua_dh;
718 h = (struct alua_dh_data *) scsi_dh_data->buf;
719 h->tpgs = TPGS_MODE_UNINITIALIZED;
720 h->state = TPGS_STATE_OPTIMIZED;
721 h->group_id = -1;
722 h->rel_port = -1;
723 h->buff = h->inq;
724 h->bufflen = ALUA_INQUIRY_SIZE;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700725 h->sdev = sdev;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700726
727 err = alua_initialize(sdev, h);
Martin Georgec0d289b2011-04-26 18:27:05 +0530728 if ((err != SCSI_DH_OK) && (err != SCSI_DH_DEV_OFFLINED))
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700729 goto failed;
730
731 if (!try_module_get(THIS_MODULE))
732 goto failed;
733
734 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
735 sdev->scsi_dh_data = scsi_dh_data;
736 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
737
738 return 0;
739
740failed:
741 kfree(scsi_dh_data);
742 sdev_printk(KERN_ERR, sdev, "%s: not attached\n", ALUA_DH_NAME);
743 return -EINVAL;
744}
745
746/*
747 * alua_bus_detach - Detach device handler
748 * @sdev: device to be detached from
749 */
750static void alua_bus_detach(struct scsi_device *sdev)
751{
752 struct scsi_dh_data *scsi_dh_data;
753 struct alua_dh_data *h;
754 unsigned long flags;
755
756 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
757 scsi_dh_data = sdev->scsi_dh_data;
758 sdev->scsi_dh_data = NULL;
759 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
760
761 h = (struct alua_dh_data *) scsi_dh_data->buf;
762 if (h->buff && h->inq != h->buff)
763 kfree(h->buff);
764 kfree(scsi_dh_data);
765 module_put(THIS_MODULE);
766 sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", ALUA_DH_NAME);
767}
768
769static int __init alua_init(void)
770{
771 int r;
772
773 r = scsi_register_device_handler(&alua_dh);
774 if (r != 0)
775 printk(KERN_ERR "%s: Failed to register scsi device handler",
776 ALUA_DH_NAME);
777 return r;
778}
779
780static void __exit alua_exit(void)
781{
782 scsi_unregister_device_handler(&alua_dh);
783}
784
785module_init(alua_init);
786module_exit(alua_exit);
787
788MODULE_DESCRIPTION("DM Multipath ALUA support");
789MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
790MODULE_LICENSE("GPL");
791MODULE_VERSION(ALUA_DH_VER);