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