blob: 6f4d8e6f32f15da12101c814e0f71179acf8196b [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>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040024#include <linux/module.h>
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070025#include <scsi/scsi.h>
26#include <scsi/scsi_eh.h>
27#include <scsi/scsi_dh.h>
28
29#define ALUA_DH_NAME "alua"
Hannes Reinecke69723d12010-09-24 15:57:04 +020030#define ALUA_DH_VER "1.3"
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070031
32#define TPGS_STATE_OPTIMIZED 0x0
33#define TPGS_STATE_NONOPTIMIZED 0x1
34#define TPGS_STATE_STANDBY 0x2
35#define TPGS_STATE_UNAVAILABLE 0x3
Hannes Reinecke69723d12010-09-24 15:57:04 +020036#define TPGS_STATE_LBA_DEPENDENT 0x4
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070037#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
Hannes Reinecke69723d12010-09-24 15:57:04 +020045#define TPGS_SUPPORT_LBA_DEPENDENT 0x10
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070046#define TPGS_SUPPORT_OFFLINE 0x40
47#define TPGS_SUPPORT_TRANSITION 0x80
48
Rob Evers3588c5a2012-05-18 14:08:54 -040049#define RTPG_FMT_MASK 0x70
50#define RTPG_FMT_EXT_HDR 0x10
51
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070052#define TPGS_MODE_UNINITIALIZED -1
53#define TPGS_MODE_NONE 0x0
54#define TPGS_MODE_IMPLICIT 0x1
55#define TPGS_MODE_EXPLICIT 0x2
56
57#define ALUA_INQUIRY_SIZE 36
Rob Evers3588c5a2012-05-18 14:08:54 -040058#define ALUA_FAILOVER_TIMEOUT 60
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070059#define ALUA_FAILOVER_RETRIES 5
60
Moger, Babu4335d092012-03-27 20:55:49 +000061/* flags passed from user level */
62#define ALUA_OPTIMIZE_STPG 1
63
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070064struct alua_dh_data {
65 int group_id;
66 int rel_port;
67 int tpgs;
68 int state;
Moger, Babudcd3a752012-03-27 20:56:08 +000069 int pref;
Moger, Babu4335d092012-03-27 20:55:49 +000070 unsigned flags; /* used for optimizing STPG */
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070071 unsigned char inq[ALUA_INQUIRY_SIZE];
72 unsigned char *buff;
73 int bufflen;
Rob Evers3588c5a2012-05-18 14:08:54 -040074 unsigned char transition_tmo;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070075 unsigned char sense[SCSI_SENSE_BUFFERSIZE];
76 int senselen;
Chandra Seetharaman96e65862009-10-21 09:23:04 -070077 struct scsi_device *sdev;
78 activate_complete callback_fn;
79 void *callback_data;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070080};
81
82#define ALUA_POLICY_SWITCH_CURRENT 0
83#define ALUA_POLICY_SWITCH_ALL 1
84
Chandra Seetharaman96e65862009-10-21 09:23:04 -070085static char print_alua_state(int);
86static int alua_check_sense(struct scsi_device *, struct scsi_sense_hdr *);
87
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070088static inline struct alua_dh_data *get_alua_data(struct scsi_device *sdev)
89{
90 struct scsi_dh_data *scsi_dh_data = sdev->scsi_dh_data;
91 BUG_ON(scsi_dh_data == NULL);
92 return ((struct alua_dh_data *) scsi_dh_data->buf);
93}
94
95static int realloc_buffer(struct alua_dh_data *h, unsigned len)
96{
97 if (h->buff && h->buff != h->inq)
98 kfree(h->buff);
99
100 h->buff = kmalloc(len, GFP_NOIO);
101 if (!h->buff) {
102 h->buff = h->inq;
103 h->bufflen = ALUA_INQUIRY_SIZE;
104 return 1;
105 }
106 h->bufflen = len;
107 return 0;
108}
109
110static struct request *get_alua_req(struct scsi_device *sdev,
111 void *buffer, unsigned buflen, int rw)
112{
113 struct request *rq;
114 struct request_queue *q = sdev->request_queue;
115
116 rq = blk_get_request(q, rw, GFP_NOIO);
117
118 if (!rq) {
119 sdev_printk(KERN_INFO, sdev,
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700120 "%s: blk_get_request failed\n", __func__);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700121 return NULL;
122 }
123
124 if (buflen && blk_rq_map_kern(q, rq, buffer, buflen, GFP_NOIO)) {
125 blk_put_request(rq);
126 sdev_printk(KERN_INFO, sdev,
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700127 "%s: blk_rq_map_kern failed\n", __func__);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700128 return NULL;
129 }
130
131 rq->cmd_type = REQ_TYPE_BLOCK_PC;
Mike Christie6000a362008-08-19 18:45:30 -0500132 rq->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
Mike Christie64f84bc2008-09-06 08:39:16 -0500133 REQ_FAILFAST_DRIVER;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700134 rq->retries = ALUA_FAILOVER_RETRIES;
Rob Evers3588c5a2012-05-18 14:08:54 -0400135 rq->timeout = ALUA_FAILOVER_TIMEOUT * HZ;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700136
137 return rq;
138}
139
140/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700141 * submit_vpd_inquiry - Issue an INQUIRY VPD page 0x83 command
142 * @sdev: sdev the command should be sent to
143 */
144static int submit_vpd_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
145{
146 struct request *rq;
147 int err = SCSI_DH_RES_TEMP_UNAVAIL;
148
149 rq = get_alua_req(sdev, h->buff, h->bufflen, READ);
150 if (!rq)
151 goto done;
152
153 /* Prepare the command. */
154 rq->cmd[0] = INQUIRY;
155 rq->cmd[1] = 1;
156 rq->cmd[2] = 0x83;
157 rq->cmd[4] = h->bufflen;
158 rq->cmd_len = COMMAND_SIZE(INQUIRY);
159
160 rq->sense = h->sense;
161 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
162 rq->sense_len = h->senselen = 0;
163
164 err = blk_execute_rq(rq->q, NULL, rq, 1);
165 if (err == -EIO) {
166 sdev_printk(KERN_INFO, sdev,
167 "%s: evpd inquiry failed with %x\n",
168 ALUA_DH_NAME, rq->errors);
169 h->senselen = rq->sense_len;
170 err = SCSI_DH_IO;
171 }
172 blk_put_request(rq);
173done:
174 return err;
175}
176
177/*
178 * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
179 * @sdev: sdev the command should be sent to
180 */
Rob Evers8e67ce62012-05-18 14:08:55 -0400181static unsigned submit_rtpg(struct scsi_device *sdev, struct alua_dh_data *h,
182 bool rtpg_ext_hdr_req)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700183{
184 struct request *rq;
185 int err = SCSI_DH_RES_TEMP_UNAVAIL;
186
187 rq = get_alua_req(sdev, h->buff, h->bufflen, READ);
188 if (!rq)
189 goto done;
190
191 /* Prepare the command. */
192 rq->cmd[0] = MAINTENANCE_IN;
Rob Evers8e67ce62012-05-18 14:08:55 -0400193 if (rtpg_ext_hdr_req)
194 rq->cmd[1] = MI_REPORT_TARGET_PGS | MI_EXT_HDR_PARAM_FMT;
195 else
196 rq->cmd[1] = MI_REPORT_TARGET_PGS;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700197 rq->cmd[6] = (h->bufflen >> 24) & 0xff;
198 rq->cmd[7] = (h->bufflen >> 16) & 0xff;
199 rq->cmd[8] = (h->bufflen >> 8) & 0xff;
200 rq->cmd[9] = h->bufflen & 0xff;
201 rq->cmd_len = COMMAND_SIZE(MAINTENANCE_IN);
202
203 rq->sense = h->sense;
204 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
205 rq->sense_len = h->senselen = 0;
206
207 err = blk_execute_rq(rq->q, NULL, rq, 1);
208 if (err == -EIO) {
209 sdev_printk(KERN_INFO, sdev,
210 "%s: rtpg failed with %x\n",
211 ALUA_DH_NAME, rq->errors);
212 h->senselen = rq->sense_len;
213 err = SCSI_DH_IO;
214 }
215 blk_put_request(rq);
216done:
217 return err;
218}
219
220/*
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700221 * alua_stpg - Evaluate SET TARGET GROUP STATES
222 * @sdev: the device to be evaluated
223 * @state: the new target group state
224 *
225 * Send a SET TARGET GROUP STATES command to the device.
226 * We only have to test here if we should resubmit the command;
227 * any other error is assumed as a failure.
228 */
229static void stpg_endio(struct request *req, int error)
230{
231 struct alua_dh_data *h = req->end_io_data;
232 struct scsi_sense_hdr sense_hdr;
Joseph Gruher93499232011-01-05 16:00:22 -0500233 unsigned err = SCSI_DH_OK;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700234
235 if (error || host_byte(req->errors) != DID_OK ||
Joseph Gruher93499232011-01-05 16:00:22 -0500236 msg_byte(req->errors) != COMMAND_COMPLETE) {
237 err = SCSI_DH_IO;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700238 goto done;
Joseph Gruher93499232011-01-05 16:00:22 -0500239 }
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700240
Joseph Gruher93499232011-01-05 16:00:22 -0500241 if (h->senselen > 0) {
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700242 err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
243 &sense_hdr);
244 if (!err) {
245 err = SCSI_DH_IO;
246 goto done;
247 }
248 err = alua_check_sense(h->sdev, &sense_hdr);
249 if (err == ADD_TO_MLQUEUE) {
250 err = SCSI_DH_RETRY;
251 goto done;
252 }
253 sdev_printk(KERN_INFO, h->sdev,
254 "%s: stpg sense code: %02x/%02x/%02x\n",
255 ALUA_DH_NAME, sense_hdr.sense_key,
256 sense_hdr.asc, sense_hdr.ascq);
257 err = SCSI_DH_IO;
258 }
259 if (err == SCSI_DH_OK) {
260 h->state = TPGS_STATE_OPTIMIZED;
261 sdev_printk(KERN_INFO, h->sdev,
262 "%s: port group %02x switched to state %c\n",
263 ALUA_DH_NAME, h->group_id,
264 print_alua_state(h->state));
265 }
266done:
Joseph Gruhered0f36b2011-01-05 16:00:21 -0500267 req->end_io_data = NULL;
268 __blk_put_request(req->q, req);
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700269 if (h->callback_fn) {
270 h->callback_fn(h->callback_data, err);
271 h->callback_fn = h->callback_data = NULL;
272 }
273 return;
274}
275
276/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700277 * submit_stpg - Issue a SET TARGET GROUP STATES command
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700278 *
279 * Currently we're only setting the current target port group state
280 * to 'active/optimized' and let the array firmware figure out
281 * the states of the remaining groups.
282 */
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700283static unsigned submit_stpg(struct alua_dh_data *h)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700284{
285 struct request *rq;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700286 int stpg_len = 8;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700287 struct scsi_device *sdev = h->sdev;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700288
289 /* Prepare the data buffer */
290 memset(h->buff, 0, stpg_len);
291 h->buff[4] = TPGS_STATE_OPTIMIZED & 0x0f;
Ilgu Hongef3fa8c2009-01-30 17:00:09 -0600292 h->buff[6] = (h->group_id >> 8) & 0xff;
293 h->buff[7] = h->group_id & 0xff;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700294
295 rq = get_alua_req(sdev, h->buff, stpg_len, WRITE);
296 if (!rq)
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700297 return SCSI_DH_RES_TEMP_UNAVAIL;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700298
299 /* Prepare the command. */
300 rq->cmd[0] = MAINTENANCE_OUT;
301 rq->cmd[1] = MO_SET_TARGET_PGS;
302 rq->cmd[6] = (stpg_len >> 24) & 0xff;
303 rq->cmd[7] = (stpg_len >> 16) & 0xff;
304 rq->cmd[8] = (stpg_len >> 8) & 0xff;
305 rq->cmd[9] = stpg_len & 0xff;
306 rq->cmd_len = COMMAND_SIZE(MAINTENANCE_OUT);
307
308 rq->sense = h->sense;
309 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
310 rq->sense_len = h->senselen = 0;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700311 rq->end_io_data = h;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700312
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700313 blk_execute_rq_nowait(rq->q, NULL, rq, 1, stpg_endio);
Joseph Gruher7c66e9a2011-01-05 16:00:20 -0500314 return SCSI_DH_OK;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700315}
316
317/*
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200318 * alua_check_tpgs - Evaluate TPGS setting
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700319 * @sdev: device to be checked
320 *
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200321 * Examine the TPGS setting of the sdev to find out if ALUA
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700322 * is supported.
323 */
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200324static int alua_check_tpgs(struct scsi_device *sdev, struct alua_dh_data *h)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700325{
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200326 int err = SCSI_DH_OK;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700327
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200328 h->tpgs = scsi_device_tpgs(sdev);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700329 switch (h->tpgs) {
330 case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT:
331 sdev_printk(KERN_INFO, sdev,
332 "%s: supports implicit and explicit TPGS\n",
333 ALUA_DH_NAME);
334 break;
335 case TPGS_MODE_EXPLICIT:
336 sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n",
337 ALUA_DH_NAME);
338 break;
339 case TPGS_MODE_IMPLICIT:
340 sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n",
341 ALUA_DH_NAME);
342 break;
343 default:
344 h->tpgs = TPGS_MODE_NONE;
345 sdev_printk(KERN_INFO, sdev, "%s: not supported\n",
346 ALUA_DH_NAME);
347 err = SCSI_DH_DEV_UNSUPP;
348 break;
349 }
350
351 return err;
352}
353
354/*
355 * alua_vpd_inquiry - Evaluate INQUIRY vpd page 0x83
356 * @sdev: device to be checked
357 *
358 * Extract the relative target port and the target port group
359 * descriptor from the list of identificators.
360 */
361static int alua_vpd_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
362{
363 int len;
364 unsigned err;
365 unsigned char *d;
366
367 retry:
368 err = submit_vpd_inquiry(sdev, h);
369
370 if (err != SCSI_DH_OK)
371 return err;
372
373 /* Check if vpd page exceeds initial buffer */
374 len = (h->buff[2] << 8) + h->buff[3] + 4;
375 if (len > h->bufflen) {
376 /* Resubmit with the correct length */
377 if (realloc_buffer(h, len)) {
378 sdev_printk(KERN_WARNING, sdev,
379 "%s: kmalloc buffer failed\n",
380 ALUA_DH_NAME);
381 /* Temporary failure, bypass */
382 return SCSI_DH_DEV_TEMP_BUSY;
383 }
384 goto retry;
385 }
386
387 /*
388 * Now look for the correct descriptor.
389 */
390 d = h->buff + 4;
391 while (d < h->buff + len) {
392 switch (d[1] & 0xf) {
393 case 0x4:
394 /* Relative target port */
395 h->rel_port = (d[6] << 8) + d[7];
396 break;
397 case 0x5:
398 /* Target port group */
399 h->group_id = (d[6] << 8) + d[7];
400 break;
401 default:
402 break;
403 }
404 d += d[3] + 4;
405 }
406
407 if (h->group_id == -1) {
408 /*
409 * Internal error; TPGS supported but required
410 * VPD identification descriptors not present.
411 * Disable ALUA support
412 */
413 sdev_printk(KERN_INFO, sdev,
414 "%s: No target port descriptors found\n",
415 ALUA_DH_NAME);
416 h->state = TPGS_STATE_OPTIMIZED;
417 h->tpgs = TPGS_MODE_NONE;
418 err = SCSI_DH_DEV_UNSUPP;
419 } else {
420 sdev_printk(KERN_INFO, sdev,
421 "%s: port group %02x rel port %02x\n",
422 ALUA_DH_NAME, h->group_id, h->rel_port);
423 }
424
425 return err;
426}
427
428static char print_alua_state(int state)
429{
430 switch (state) {
431 case TPGS_STATE_OPTIMIZED:
432 return 'A';
433 case TPGS_STATE_NONOPTIMIZED:
434 return 'N';
435 case TPGS_STATE_STANDBY:
436 return 'S';
437 case TPGS_STATE_UNAVAILABLE:
438 return 'U';
Hannes Reinecke69723d12010-09-24 15:57:04 +0200439 case TPGS_STATE_LBA_DEPENDENT:
440 return 'L';
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700441 case TPGS_STATE_OFFLINE:
442 return 'O';
443 case TPGS_STATE_TRANSITIONING:
444 return 'T';
445 default:
446 return 'X';
447 }
448}
449
450static int alua_check_sense(struct scsi_device *sdev,
451 struct scsi_sense_hdr *sense_hdr)
452{
453 switch (sense_hdr->sense_key) {
454 case NOT_READY:
455 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a)
456 /*
457 * LUN Not Accessible - ALUA state transition
458 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700459 return ADD_TO_MLQUEUE;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700460 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0b)
461 /*
462 * LUN Not Accessible -- Target port in standby state
463 */
464 return SUCCESS;
465 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0c)
466 /*
467 * LUN Not Accessible -- Target port in unavailable state
468 */
469 return SUCCESS;
470 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x12)
471 /*
472 * LUN Not Ready -- Offline
473 */
474 return SUCCESS;
475 break;
476 case UNIT_ATTENTION:
477 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
478 /*
479 * Power On, Reset, or Bus Device Reset, just retry.
480 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700481 return ADD_TO_MLQUEUE;
Moger, Babu410f02d2011-12-21 18:01:37 -0500482 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x01)
483 /*
484 * Mode Parameters Changed
485 */
486 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400487 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700488 /*
489 * ALUA state changed
490 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700491 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400492 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700493 /*
494 * Implicit ALUA state transition failed
495 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700496 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400497 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x03)
498 /*
499 * Inquiry data has changed
500 */
501 return ADD_TO_MLQUEUE;
502 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e)
Ilgu Hong4d086f62009-01-30 17:00:11 -0600503 /*
504 * REPORTED_LUNS_DATA_HAS_CHANGED is reported
505 * when switching controllers on targets like
506 * Intel Multi-Flex. We can just retry.
507 */
508 return ADD_TO_MLQUEUE;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700509 break;
510 }
511
512 return SCSI_RETURN_NOT_HANDLED;
513}
514
515/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700516 * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
517 * @sdev: the device to be evaluated.
518 *
519 * Evaluate the Target Port Group State.
520 * Returns SCSI_DH_DEV_OFFLINED if the path is
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300521 * found to be unusable.
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700522 */
523static int alua_rtpg(struct scsi_device *sdev, struct alua_dh_data *h)
524{
525 struct scsi_sense_hdr sense_hdr;
526 int len, k, off, valid_states = 0;
Hannes Reineckecfde3fa2011-08-24 10:51:18 +0200527 unsigned char *ucp;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700528 unsigned err;
Rob Evers8e67ce62012-05-18 14:08:55 -0400529 bool rtpg_ext_hdr_req = 1;
Rob Eversbc97f4b2012-05-18 14:08:56 -0400530 unsigned long expiry, interval = 0;
Rob Evers3588c5a2012-05-18 14:08:54 -0400531 unsigned int tpg_desc_tbl_off;
532 unsigned char orig_transition_tmo;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700533
Rob Evers3588c5a2012-05-18 14:08:54 -0400534 if (!h->transition_tmo)
535 expiry = round_jiffies_up(jiffies + ALUA_FAILOVER_TIMEOUT * HZ);
536 else
537 expiry = round_jiffies_up(jiffies + h->transition_tmo * HZ);
538
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700539 retry:
Rob Evers8e67ce62012-05-18 14:08:55 -0400540 err = submit_rtpg(sdev, h, rtpg_ext_hdr_req);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700541
542 if (err == SCSI_DH_IO && h->senselen > 0) {
543 err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
544 &sense_hdr);
545 if (!err)
546 return SCSI_DH_IO;
547
Rob Evers8e67ce62012-05-18 14:08:55 -0400548 /*
549 * submit_rtpg() has failed on existing arrays
550 * when requesting extended header info, and
551 * the array doesn't support extended headers,
552 * even though it shouldn't according to T10.
553 * The retry without rtpg_ext_hdr_req set
554 * handles this.
555 */
556 if (rtpg_ext_hdr_req == 1 &&
557 sense_hdr.sense_key == ILLEGAL_REQUEST &&
558 sense_hdr.asc == 0x24 && sense_hdr.ascq == 0) {
559 rtpg_ext_hdr_req = 0;
560 goto retry;
561 }
562
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700563 err = alua_check_sense(sdev, &sense_hdr);
Hannes Reinecke69723d12010-09-24 15:57:04 +0200564 if (err == ADD_TO_MLQUEUE && time_before(jiffies, expiry))
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700565 goto retry;
566 sdev_printk(KERN_INFO, sdev,
567 "%s: rtpg sense code %02x/%02x/%02x\n",
568 ALUA_DH_NAME, sense_hdr.sense_key,
569 sense_hdr.asc, sense_hdr.ascq);
570 err = SCSI_DH_IO;
571 }
572 if (err != SCSI_DH_OK)
573 return err;
574
575 len = (h->buff[0] << 24) + (h->buff[1] << 16) +
576 (h->buff[2] << 8) + h->buff[3] + 4;
577
578 if (len > h->bufflen) {
579 /* Resubmit with the correct length */
580 if (realloc_buffer(h, len)) {
581 sdev_printk(KERN_WARNING, sdev,
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700582 "%s: kmalloc buffer failed\n",__func__);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700583 /* Temporary failure, bypass */
584 return SCSI_DH_DEV_TEMP_BUSY;
585 }
586 goto retry;
587 }
588
Rob Evers3588c5a2012-05-18 14:08:54 -0400589 orig_transition_tmo = h->transition_tmo;
590 if ((h->buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR && h->buff[5] != 0)
591 h->transition_tmo = h->buff[5];
592 else
593 h->transition_tmo = ALUA_FAILOVER_TIMEOUT;
594
595 if (orig_transition_tmo != h->transition_tmo) {
596 sdev_printk(KERN_INFO, sdev,
597 "%s: transition timeout set to %d seconds\n",
598 ALUA_DH_NAME, h->transition_tmo);
599 expiry = jiffies + h->transition_tmo * HZ;
600 }
601
602 if ((h->buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR)
603 tpg_desc_tbl_off = 8;
604 else
605 tpg_desc_tbl_off = 4;
606
607 for (k = tpg_desc_tbl_off, ucp = h->buff + tpg_desc_tbl_off;
608 k < len;
609 k += off, ucp += off) {
610
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700611 if (h->group_id == (ucp[2] << 8) + ucp[3]) {
612 h->state = ucp[0] & 0x0f;
Moger, Babudcd3a752012-03-27 20:56:08 +0000613 h->pref = ucp[0] >> 7;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700614 valid_states = ucp[1];
615 }
616 off = 8 + (ucp[7] * 4);
617 }
618
619 sdev_printk(KERN_INFO, sdev,
Moger, Babudcd3a752012-03-27 20:56:08 +0000620 "%s: port group %02x state %c %s supports %c%c%c%c%c%c%c\n",
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700621 ALUA_DH_NAME, h->group_id, print_alua_state(h->state),
Moger, Babudcd3a752012-03-27 20:56:08 +0000622 h->pref ? "preferred" : "non-preferred",
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700623 valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
624 valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
Hannes Reinecke69723d12010-09-24 15:57:04 +0200625 valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700626 valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u',
627 valid_states&TPGS_SUPPORT_STANDBY?'S':'s',
628 valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n',
629 valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a');
630
Hannes Reinecke69723d12010-09-24 15:57:04 +0200631 switch (h->state) {
632 case TPGS_STATE_TRANSITIONING:
633 if (time_before(jiffies, expiry)) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700634 /* State transition, retry */
Rob Eversbc97f4b2012-05-18 14:08:56 -0400635 interval += 2000;
Hannes Reinecke69723d12010-09-24 15:57:04 +0200636 msleep(interval);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700637 goto retry;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700638 }
Hannes Reinecke69723d12010-09-24 15:57:04 +0200639 /* Transitioning time exceeded, set port to standby */
640 err = SCSI_DH_RETRY;
641 h->state = TPGS_STATE_STANDBY;
642 break;
643 case TPGS_STATE_OFFLINE:
Bart Van Asschee47f8972012-08-24 09:08:41 +0000644 /* Path unusable */
Hannes Reinecke69723d12010-09-24 15:57:04 +0200645 err = SCSI_DH_DEV_OFFLINED;
646 break;
647 default:
648 /* Useable path if active */
649 err = SCSI_DH_OK;
650 break;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700651 }
652 return err;
653}
654
655/*
656 * alua_initialize - Initialize ALUA state
657 * @sdev: the device to be initialized
658 *
659 * For the prep_fn to work correctly we have
660 * to initialize the ALUA state for the device.
661 */
662static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
663{
664 int err;
665
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200666 err = alua_check_tpgs(sdev, h);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700667 if (err != SCSI_DH_OK)
668 goto out;
669
670 err = alua_vpd_inquiry(sdev, h);
671 if (err != SCSI_DH_OK)
672 goto out;
673
674 err = alua_rtpg(sdev, h);
675 if (err != SCSI_DH_OK)
676 goto out;
677
678out:
679 return err;
680}
Moger, Babu4335d092012-03-27 20:55:49 +0000681/*
682 * alua_set_params - set/unset the optimize flag
683 * @sdev: device on the path to be activated
684 * params - parameters in the following format
685 * "no_of_params\0param1\0param2\0param3\0...\0"
686 * For example, to set the flag pass the following parameters
687 * from multipath.conf
688 * hardware_handler "2 alua 1"
689 */
690static int alua_set_params(struct scsi_device *sdev, const char *params)
691{
692 struct alua_dh_data *h = get_alua_data(sdev);
693 unsigned int optimize = 0, argc;
694 const char *p = params;
695 int result = SCSI_DH_OK;
696
697 if ((sscanf(params, "%u", &argc) != 1) || (argc != 1))
698 return -EINVAL;
699
700 while (*p++)
701 ;
702 if ((sscanf(p, "%u", &optimize) != 1) || (optimize > 1))
703 return -EINVAL;
704
705 if (optimize)
706 h->flags |= ALUA_OPTIMIZE_STPG;
707 else
708 h->flags &= ~ALUA_OPTIMIZE_STPG;
709
710 return result;
711}
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700712
713/*
714 * alua_activate - activate a path
715 * @sdev: device on the path to be activated
716 *
717 * We're currently switching the port group to be activated only and
718 * let the array figure out the rest.
719 * There may be other arrays which require us to switch all port groups
720 * based on a certain policy. But until we actually encounter them it
721 * should be okay.
722 */
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700723static int alua_activate(struct scsi_device *sdev,
724 activate_complete fn, void *data)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700725{
726 struct alua_dh_data *h = get_alua_data(sdev);
727 int err = SCSI_DH_OK;
Moger, Babu72d9e0f2012-03-27 20:56:20 +0000728 int stpg = 0;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700729
Hannes Reinecke46ccf6b2011-08-24 10:51:16 +0200730 err = alua_rtpg(sdev, h);
731 if (err != SCSI_DH_OK)
732 goto out;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700733
Moger, Babu72d9e0f2012-03-27 20:56:20 +0000734 if (h->tpgs & TPGS_MODE_EXPLICIT) {
735 switch (h->state) {
736 case TPGS_STATE_NONOPTIMIZED:
737 stpg = 1;
738 if ((h->flags & ALUA_OPTIMIZE_STPG) &&
739 (!h->pref) &&
740 (h->tpgs & TPGS_MODE_IMPLICIT))
741 stpg = 0;
742 break;
743 case TPGS_STATE_STANDBY:
Bart Van Asschebb2c94a2012-06-22 08:49:38 +0000744 case TPGS_STATE_UNAVAILABLE:
Moger, Babu72d9e0f2012-03-27 20:56:20 +0000745 stpg = 1;
746 break;
Moger, Babu72d9e0f2012-03-27 20:56:20 +0000747 case TPGS_STATE_OFFLINE:
748 err = SCSI_DH_IO;
749 break;
750 case TPGS_STATE_TRANSITIONING:
751 err = SCSI_DH_RETRY;
752 break;
753 default:
754 break;
755 }
756 }
757
758 if (stpg) {
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700759 h->callback_fn = fn;
760 h->callback_data = data;
761 err = submit_stpg(h);
762 if (err == SCSI_DH_OK)
763 return 0;
764 h->callback_fn = h->callback_data = NULL;
765 }
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700766
767out:
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700768 if (fn)
769 fn(data, err);
770 return 0;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700771}
772
773/*
774 * alua_prep_fn - request callback
775 *
776 * Fail I/O to all paths not in state
777 * active/optimized or active/non-optimized.
778 */
779static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
780{
781 struct alua_dh_data *h = get_alua_data(sdev);
782 int ret = BLKPREP_OK;
783
Hannes Reinecke69723d12010-09-24 15:57:04 +0200784 if (h->state == TPGS_STATE_TRANSITIONING)
785 ret = BLKPREP_DEFER;
786 else if (h->state != TPGS_STATE_OPTIMIZED &&
787 h->state != TPGS_STATE_NONOPTIMIZED &&
788 h->state != TPGS_STATE_LBA_DEPENDENT) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700789 ret = BLKPREP_KILL;
790 req->cmd_flags |= REQ_QUIET;
791 }
792 return ret;
793
794}
795
Hannes Reinecke6c3633d2011-08-24 10:51:15 +0200796static bool alua_match(struct scsi_device *sdev)
797{
798 return (scsi_device_tpgs(sdev) != 0);
799}
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700800
801static int alua_bus_attach(struct scsi_device *sdev);
802static void alua_bus_detach(struct scsi_device *sdev);
803
804static struct scsi_device_handler alua_dh = {
805 .name = ALUA_DH_NAME,
806 .module = THIS_MODULE,
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700807 .attach = alua_bus_attach,
808 .detach = alua_bus_detach,
809 .prep_fn = alua_prep_fn,
810 .check_sense = alua_check_sense,
811 .activate = alua_activate,
Moger, Babu4335d092012-03-27 20:55:49 +0000812 .set_params = alua_set_params,
Hannes Reinecke6c3633d2011-08-24 10:51:15 +0200813 .match = alua_match,
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700814};
815
816/*
817 * alua_bus_attach - Attach device handler
818 * @sdev: device to be attached to
819 */
820static int alua_bus_attach(struct scsi_device *sdev)
821{
822 struct scsi_dh_data *scsi_dh_data;
823 struct alua_dh_data *h;
824 unsigned long flags;
825 int err = SCSI_DH_OK;
826
Hillf Danton9dfeb312011-02-11 15:17:33 -0700827 scsi_dh_data = kzalloc(sizeof(*scsi_dh_data)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700828 + sizeof(*h) , GFP_KERNEL);
829 if (!scsi_dh_data) {
830 sdev_printk(KERN_ERR, sdev, "%s: Attach failed\n",
831 ALUA_DH_NAME);
832 return -ENOMEM;
833 }
834
835 scsi_dh_data->scsi_dh = &alua_dh;
836 h = (struct alua_dh_data *) scsi_dh_data->buf;
837 h->tpgs = TPGS_MODE_UNINITIALIZED;
838 h->state = TPGS_STATE_OPTIMIZED;
839 h->group_id = -1;
840 h->rel_port = -1;
841 h->buff = h->inq;
842 h->bufflen = ALUA_INQUIRY_SIZE;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700843 h->sdev = sdev;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700844
845 err = alua_initialize(sdev, h);
Martin Georgec0d289b2011-04-26 18:27:05 +0530846 if ((err != SCSI_DH_OK) && (err != SCSI_DH_DEV_OFFLINED))
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700847 goto failed;
848
849 if (!try_module_get(THIS_MODULE))
850 goto failed;
851
852 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
853 sdev->scsi_dh_data = scsi_dh_data;
854 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
Moger, Babuab720022011-10-27 14:36:32 -0400855 sdev_printk(KERN_NOTICE, sdev, "%s: Attached\n", ALUA_DH_NAME);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700856
857 return 0;
858
859failed:
860 kfree(scsi_dh_data);
861 sdev_printk(KERN_ERR, sdev, "%s: not attached\n", ALUA_DH_NAME);
862 return -EINVAL;
863}
864
865/*
866 * alua_bus_detach - Detach device handler
867 * @sdev: device to be detached from
868 */
869static void alua_bus_detach(struct scsi_device *sdev)
870{
871 struct scsi_dh_data *scsi_dh_data;
872 struct alua_dh_data *h;
873 unsigned long flags;
874
875 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
876 scsi_dh_data = sdev->scsi_dh_data;
877 sdev->scsi_dh_data = NULL;
878 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
879
880 h = (struct alua_dh_data *) scsi_dh_data->buf;
881 if (h->buff && h->inq != h->buff)
882 kfree(h->buff);
883 kfree(scsi_dh_data);
884 module_put(THIS_MODULE);
885 sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", ALUA_DH_NAME);
886}
887
888static int __init alua_init(void)
889{
890 int r;
891
892 r = scsi_register_device_handler(&alua_dh);
893 if (r != 0)
894 printk(KERN_ERR "%s: Failed to register scsi device handler",
895 ALUA_DH_NAME);
896 return r;
897}
898
899static void __exit alua_exit(void)
900{
901 scsi_unregister_device_handler(&alua_dh);
902}
903
904module_init(alua_init);
905module_exit(alua_exit);
906
907MODULE_DESCRIPTION("DM Multipath ALUA support");
908MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
909MODULE_LICENSE("GPL");
910MODULE_VERSION(ALUA_DH_VER);