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