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