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