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