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