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