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 <linux/module.h>
25 #include <asm/unaligned.h>
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_proto.h>
28 #include <scsi/scsi_dbg.h>
29 #include <scsi/scsi_eh.h>
30 #include <scsi/scsi_dh.h>
31 
32 #define ALUA_DH_NAME "alua"
33 #define ALUA_DH_VER "2.0"
34 
35 #define TPGS_SUPPORT_NONE		0x00
36 #define TPGS_SUPPORT_OPTIMIZED		0x01
37 #define TPGS_SUPPORT_NONOPTIMIZED	0x02
38 #define TPGS_SUPPORT_STANDBY		0x04
39 #define TPGS_SUPPORT_UNAVAILABLE	0x08
40 #define TPGS_SUPPORT_LBA_DEPENDENT	0x10
41 #define TPGS_SUPPORT_OFFLINE		0x40
42 #define TPGS_SUPPORT_TRANSITION		0x80
43 
44 #define RTPG_FMT_MASK			0x70
45 #define RTPG_FMT_EXT_HDR		0x10
46 
47 #define TPGS_MODE_UNINITIALIZED		 -1
48 #define TPGS_MODE_NONE			0x0
49 #define TPGS_MODE_IMPLICIT		0x1
50 #define TPGS_MODE_EXPLICIT		0x2
51 
52 #define ALUA_RTPG_SIZE			128
53 #define ALUA_FAILOVER_TIMEOUT		60
54 #define ALUA_FAILOVER_RETRIES		5
55 #define ALUA_RTPG_DELAY_MSECS		5
56 
57 /* device handler flags */
58 #define ALUA_OPTIMIZE_STPG		0x01
59 #define ALUA_RTPG_EXT_HDR_UNSUPP	0x02
60 #define ALUA_SYNC_STPG			0x04
61 /* State machine flags */
62 #define ALUA_PG_RUN_RTPG		0x10
63 #define ALUA_PG_RUN_STPG		0x20
64 #define ALUA_PG_RUNNING			0x40
65 
66 static uint optimize_stpg;
67 module_param(optimize_stpg, uint, S_IRUGO|S_IWUSR);
68 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.");
69 
70 static LIST_HEAD(port_group_list);
71 static DEFINE_SPINLOCK(port_group_lock);
72 static struct workqueue_struct *kaluad_wq;
73 static struct workqueue_struct *kaluad_sync_wq;
74 
75 struct alua_port_group {
76 	struct kref		kref;
77 	struct rcu_head		rcu;
78 	struct list_head	node;
79 	struct list_head	dh_list;
80 	unsigned char		device_id_str[256];
81 	int			device_id_len;
82 	int			group_id;
83 	int			tpgs;
84 	int			state;
85 	int			pref;
86 	unsigned		flags; /* used for optimizing STPG */
87 	unsigned char		transition_tmo;
88 	unsigned long		expiry;
89 	unsigned long		interval;
90 	struct delayed_work	rtpg_work;
91 	spinlock_t		lock;
92 	struct list_head	rtpg_list;
93 	struct scsi_device	*rtpg_sdev;
94 };
95 
96 struct alua_dh_data {
97 	struct list_head	node;
98 	struct alua_port_group	*pg;
99 	int			group_id;
100 	spinlock_t		pg_lock;
101 	struct scsi_device	*sdev;
102 	int			init_error;
103 	struct mutex		init_mutex;
104 };
105 
106 struct alua_queue_data {
107 	struct list_head	entry;
108 	activate_complete	callback_fn;
109 	void			*callback_data;
110 };
111 
112 #define ALUA_POLICY_SWITCH_CURRENT	0
113 #define ALUA_POLICY_SWITCH_ALL		1
114 
115 static void alua_rtpg_work(struct work_struct *work);
116 static void alua_rtpg_queue(struct alua_port_group *pg,
117 			    struct scsi_device *sdev,
118 			    struct alua_queue_data *qdata, bool force);
119 static void alua_check(struct scsi_device *sdev, bool force);
120 
121 static void release_port_group(struct kref *kref)
122 {
123 	struct alua_port_group *pg;
124 
125 	pg = container_of(kref, struct alua_port_group, kref);
126 	if (pg->rtpg_sdev)
127 		flush_delayed_work(&pg->rtpg_work);
128 	spin_lock(&port_group_lock);
129 	list_del(&pg->node);
130 	spin_unlock(&port_group_lock);
131 	kfree_rcu(pg, rcu);
132 }
133 
134 /*
135  * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
136  * @sdev: sdev the command should be sent to
137  */
138 static int submit_rtpg(struct scsi_device *sdev, unsigned char *buff,
139 		       int bufflen, struct scsi_sense_hdr *sshdr, int flags)
140 {
141 	u8 cdb[COMMAND_SIZE(MAINTENANCE_IN)];
142 	int req_flags = REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
143 		REQ_FAILFAST_DRIVER;
144 
145 	/* Prepare the command. */
146 	memset(cdb, 0x0, COMMAND_SIZE(MAINTENANCE_IN));
147 	cdb[0] = MAINTENANCE_IN;
148 	if (!(flags & ALUA_RTPG_EXT_HDR_UNSUPP))
149 		cdb[1] = MI_REPORT_TARGET_PGS | MI_EXT_HDR_PARAM_FMT;
150 	else
151 		cdb[1] = MI_REPORT_TARGET_PGS;
152 	put_unaligned_be32(bufflen, &cdb[6]);
153 
154 	return scsi_execute_req_flags(sdev, cdb, DMA_FROM_DEVICE,
155 				      buff, bufflen, sshdr,
156 				      ALUA_FAILOVER_TIMEOUT * HZ,
157 				      ALUA_FAILOVER_RETRIES, NULL, req_flags);
158 }
159 
160 /*
161  * submit_stpg - Issue a SET TARGET PORT GROUP command
162  *
163  * Currently we're only setting the current target port group state
164  * to 'active/optimized' and let the array firmware figure out
165  * the states of the remaining groups.
166  */
167 static int submit_stpg(struct scsi_device *sdev, int group_id,
168 		       struct scsi_sense_hdr *sshdr)
169 {
170 	u8 cdb[COMMAND_SIZE(MAINTENANCE_OUT)];
171 	unsigned char stpg_data[8];
172 	int stpg_len = 8;
173 	int req_flags = REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
174 		REQ_FAILFAST_DRIVER;
175 
176 	/* Prepare the data buffer */
177 	memset(stpg_data, 0, stpg_len);
178 	stpg_data[4] = SCSI_ACCESS_STATE_OPTIMAL;
179 	put_unaligned_be16(group_id, &stpg_data[6]);
180 
181 	/* Prepare the command. */
182 	memset(cdb, 0x0, COMMAND_SIZE(MAINTENANCE_OUT));
183 	cdb[0] = MAINTENANCE_OUT;
184 	cdb[1] = MO_SET_TARGET_PGS;
185 	put_unaligned_be32(stpg_len, &cdb[6]);
186 
187 	return scsi_execute_req_flags(sdev, cdb, DMA_TO_DEVICE,
188 				      stpg_data, stpg_len,
189 				      sshdr, ALUA_FAILOVER_TIMEOUT * HZ,
190 				      ALUA_FAILOVER_RETRIES, NULL, req_flags);
191 }
192 
193 static struct alua_port_group *alua_find_get_pg(char *id_str, size_t id_size,
194 						int group_id)
195 {
196 	struct alua_port_group *pg;
197 
198 	if (!id_str || !id_size || !strlen(id_str))
199 		return NULL;
200 
201 	list_for_each_entry(pg, &port_group_list, node) {
202 		if (pg->group_id != group_id)
203 			continue;
204 		if (!pg->device_id_len || pg->device_id_len != id_size)
205 			continue;
206 		if (strncmp(pg->device_id_str, id_str, id_size))
207 			continue;
208 		if (!kref_get_unless_zero(&pg->kref))
209 			continue;
210 		return pg;
211 	}
212 
213 	return NULL;
214 }
215 
216 /*
217  * alua_alloc_pg - Allocate a new port_group structure
218  * @sdev: scsi device
219  * @h: alua device_handler data
220  * @group_id: port group id
221  *
222  * Allocate a new port_group structure for a given
223  * device.
224  */
225 static struct alua_port_group *alua_alloc_pg(struct scsi_device *sdev,
226 					     int group_id, int tpgs)
227 {
228 	struct alua_port_group *pg, *tmp_pg;
229 
230 	pg = kzalloc(sizeof(struct alua_port_group), GFP_KERNEL);
231 	if (!pg)
232 		return ERR_PTR(-ENOMEM);
233 
234 	pg->device_id_len = scsi_vpd_lun_id(sdev, pg->device_id_str,
235 					    sizeof(pg->device_id_str));
236 	if (pg->device_id_len <= 0) {
237 		/*
238 		 * TPGS supported but no device identification found.
239 		 * Generate private device identification.
240 		 */
241 		sdev_printk(KERN_INFO, sdev,
242 			    "%s: No device descriptors found\n",
243 			    ALUA_DH_NAME);
244 		pg->device_id_str[0] = '\0';
245 		pg->device_id_len = 0;
246 	}
247 	pg->group_id = group_id;
248 	pg->tpgs = tpgs;
249 	pg->state = SCSI_ACCESS_STATE_OPTIMAL;
250 	if (optimize_stpg)
251 		pg->flags |= ALUA_OPTIMIZE_STPG;
252 	kref_init(&pg->kref);
253 	INIT_DELAYED_WORK(&pg->rtpg_work, alua_rtpg_work);
254 	INIT_LIST_HEAD(&pg->rtpg_list);
255 	INIT_LIST_HEAD(&pg->node);
256 	INIT_LIST_HEAD(&pg->dh_list);
257 	spin_lock_init(&pg->lock);
258 
259 	spin_lock(&port_group_lock);
260 	tmp_pg = alua_find_get_pg(pg->device_id_str, pg->device_id_len,
261 				  group_id);
262 	if (tmp_pg) {
263 		spin_unlock(&port_group_lock);
264 		kfree(pg);
265 		return tmp_pg;
266 	}
267 
268 	list_add(&pg->node, &port_group_list);
269 	spin_unlock(&port_group_lock);
270 
271 	return pg;
272 }
273 
274 /*
275  * alua_check_tpgs - Evaluate TPGS setting
276  * @sdev: device to be checked
277  *
278  * Examine the TPGS setting of the sdev to find out if ALUA
279  * is supported.
280  */
281 static int alua_check_tpgs(struct scsi_device *sdev)
282 {
283 	int tpgs = TPGS_MODE_NONE;
284 
285 	/*
286 	 * ALUA support for non-disk devices is fraught with
287 	 * difficulties, so disable it for now.
288 	 */
289 	if (sdev->type != TYPE_DISK) {
290 		sdev_printk(KERN_INFO, sdev,
291 			    "%s: disable for non-disk devices\n",
292 			    ALUA_DH_NAME);
293 		return tpgs;
294 	}
295 
296 	tpgs = scsi_device_tpgs(sdev);
297 	switch (tpgs) {
298 	case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT:
299 		sdev_printk(KERN_INFO, sdev,
300 			    "%s: supports implicit and explicit TPGS\n",
301 			    ALUA_DH_NAME);
302 		break;
303 	case TPGS_MODE_EXPLICIT:
304 		sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n",
305 			    ALUA_DH_NAME);
306 		break;
307 	case TPGS_MODE_IMPLICIT:
308 		sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n",
309 			    ALUA_DH_NAME);
310 		break;
311 	case TPGS_MODE_NONE:
312 		sdev_printk(KERN_INFO, sdev, "%s: not supported\n",
313 			    ALUA_DH_NAME);
314 		break;
315 	default:
316 		sdev_printk(KERN_INFO, sdev,
317 			    "%s: unsupported TPGS setting %d\n",
318 			    ALUA_DH_NAME, tpgs);
319 		tpgs = TPGS_MODE_NONE;
320 		break;
321 	}
322 
323 	return tpgs;
324 }
325 
326 /*
327  * alua_check_vpd - 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_check_vpd(struct scsi_device *sdev, struct alua_dh_data *h,
334 			  int tpgs)
335 {
336 	int rel_port = -1, group_id;
337 	struct alua_port_group *pg, *old_pg = NULL;
338 	bool pg_updated = false;
339 	unsigned long flags;
340 
341 	group_id = scsi_vpd_tpg_id(sdev, &rel_port);
342 	if (group_id < 0) {
343 		/*
344 		 * Internal error; TPGS supported but required
345 		 * VPD identification descriptors not present.
346 		 * Disable ALUA support
347 		 */
348 		sdev_printk(KERN_INFO, sdev,
349 			    "%s: No target port descriptors found\n",
350 			    ALUA_DH_NAME);
351 		return SCSI_DH_DEV_UNSUPP;
352 	}
353 
354 	pg = alua_alloc_pg(sdev, group_id, tpgs);
355 	if (IS_ERR(pg)) {
356 		if (PTR_ERR(pg) == -ENOMEM)
357 			return SCSI_DH_NOMEM;
358 		return SCSI_DH_DEV_UNSUPP;
359 	}
360 	if (pg->device_id_len)
361 		sdev_printk(KERN_INFO, sdev,
362 			    "%s: device %s port group %x rel port %x\n",
363 			    ALUA_DH_NAME, pg->device_id_str,
364 			    group_id, rel_port);
365 	else
366 		sdev_printk(KERN_INFO, sdev,
367 			    "%s: port group %x rel port %x\n",
368 			    ALUA_DH_NAME, group_id, rel_port);
369 
370 	/* Check for existing port group references */
371 	spin_lock(&h->pg_lock);
372 	old_pg = h->pg;
373 	if (old_pg != pg) {
374 		/* port group has changed. Update to new port group */
375 		if (h->pg) {
376 			spin_lock_irqsave(&old_pg->lock, flags);
377 			list_del_rcu(&h->node);
378 			spin_unlock_irqrestore(&old_pg->lock, flags);
379 		}
380 		rcu_assign_pointer(h->pg, pg);
381 		pg_updated = true;
382 	}
383 
384 	spin_lock_irqsave(&pg->lock, flags);
385 	if (sdev->synchronous_alua)
386 		pg->flags |= ALUA_SYNC_STPG;
387 	if (pg_updated)
388 		list_add_rcu(&h->node, &pg->dh_list);
389 	spin_unlock_irqrestore(&pg->lock, flags);
390 
391 	alua_rtpg_queue(h->pg, sdev, NULL, true);
392 	spin_unlock(&h->pg_lock);
393 
394 	if (old_pg)
395 		kref_put(&old_pg->kref, release_port_group);
396 
397 	return SCSI_DH_OK;
398 }
399 
400 static char print_alua_state(unsigned char state)
401 {
402 	switch (state) {
403 	case SCSI_ACCESS_STATE_OPTIMAL:
404 		return 'A';
405 	case SCSI_ACCESS_STATE_ACTIVE:
406 		return 'N';
407 	case SCSI_ACCESS_STATE_STANDBY:
408 		return 'S';
409 	case SCSI_ACCESS_STATE_UNAVAILABLE:
410 		return 'U';
411 	case SCSI_ACCESS_STATE_LBA:
412 		return 'L';
413 	case SCSI_ACCESS_STATE_OFFLINE:
414 		return 'O';
415 	case SCSI_ACCESS_STATE_TRANSITIONING:
416 		return 'T';
417 	default:
418 		return 'X';
419 	}
420 }
421 
422 static int alua_check_sense(struct scsi_device *sdev,
423 			    struct scsi_sense_hdr *sense_hdr)
424 {
425 	switch (sense_hdr->sense_key) {
426 	case NOT_READY:
427 		if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a) {
428 			/*
429 			 * LUN Not Accessible - ALUA state transition
430 			 */
431 			alua_check(sdev, false);
432 			return NEEDS_RETRY;
433 		}
434 		break;
435 	case UNIT_ATTENTION:
436 		if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00) {
437 			/*
438 			 * Power On, Reset, or Bus Device Reset.
439 			 * Might have obscured a state transition,
440 			 * so schedule a recheck.
441 			 */
442 			alua_check(sdev, true);
443 			return ADD_TO_MLQUEUE;
444 		}
445 		if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x04)
446 			/*
447 			 * Device internal reset
448 			 */
449 			return ADD_TO_MLQUEUE;
450 		if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x01)
451 			/*
452 			 * Mode Parameters Changed
453 			 */
454 			return ADD_TO_MLQUEUE;
455 		if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06) {
456 			/*
457 			 * ALUA state changed
458 			 */
459 			alua_check(sdev, true);
460 			return ADD_TO_MLQUEUE;
461 		}
462 		if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07) {
463 			/*
464 			 * Implicit ALUA state transition failed
465 			 */
466 			alua_check(sdev, true);
467 			return ADD_TO_MLQUEUE;
468 		}
469 		if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x03)
470 			/*
471 			 * Inquiry data has changed
472 			 */
473 			return ADD_TO_MLQUEUE;
474 		if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e)
475 			/*
476 			 * REPORTED_LUNS_DATA_HAS_CHANGED is reported
477 			 * when switching controllers on targets like
478 			 * Intel Multi-Flex. We can just retry.
479 			 */
480 			return ADD_TO_MLQUEUE;
481 		break;
482 	}
483 
484 	return SCSI_RETURN_NOT_HANDLED;
485 }
486 
487 /*
488  * alua_tur - Send a TEST UNIT READY
489  * @sdev: device to which the TEST UNIT READY command should be send
490  *
491  * Send a TEST UNIT READY to @sdev to figure out the device state
492  * Returns SCSI_DH_RETRY if the sense code is NOT READY/ALUA TRANSITIONING,
493  * SCSI_DH_OK if no error occurred, and SCSI_DH_IO otherwise.
494  */
495 static int alua_tur(struct scsi_device *sdev)
496 {
497 	struct scsi_sense_hdr sense_hdr;
498 	int retval;
499 
500 	retval = scsi_test_unit_ready(sdev, ALUA_FAILOVER_TIMEOUT * HZ,
501 				      ALUA_FAILOVER_RETRIES, &sense_hdr);
502 	if (sense_hdr.sense_key == NOT_READY &&
503 	    sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x0a)
504 		return SCSI_DH_RETRY;
505 	else if (retval)
506 		return SCSI_DH_IO;
507 	else
508 		return SCSI_DH_OK;
509 }
510 
511 /*
512  * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
513  * @sdev: the device to be evaluated.
514  *
515  * Evaluate the Target Port Group State.
516  * Returns SCSI_DH_DEV_OFFLINED if the path is
517  * found to be unusable.
518  */
519 static int alua_rtpg(struct scsi_device *sdev, struct alua_port_group *pg)
520 {
521 	struct scsi_sense_hdr sense_hdr;
522 	struct alua_port_group *tmp_pg;
523 	int len, k, off, valid_states = 0, bufflen = ALUA_RTPG_SIZE;
524 	unsigned char *desc, *buff;
525 	unsigned err, retval;
526 	unsigned int tpg_desc_tbl_off;
527 	unsigned char orig_transition_tmo;
528 	unsigned long flags;
529 
530 	if (!pg->expiry) {
531 		unsigned long transition_tmo = ALUA_FAILOVER_TIMEOUT * HZ;
532 
533 		if (pg->transition_tmo)
534 			transition_tmo = pg->transition_tmo * HZ;
535 
536 		pg->expiry = round_jiffies_up(jiffies + transition_tmo);
537 	}
538 
539 	buff = kzalloc(bufflen, GFP_KERNEL);
540 	if (!buff)
541 		return SCSI_DH_DEV_TEMP_BUSY;
542 
543  retry:
544 	err = 0;
545 	retval = submit_rtpg(sdev, buff, bufflen, &sense_hdr, pg->flags);
546 
547 	if (retval) {
548 		if (!scsi_sense_valid(&sense_hdr)) {
549 			sdev_printk(KERN_INFO, sdev,
550 				    "%s: rtpg failed, result %d\n",
551 				    ALUA_DH_NAME, retval);
552 			kfree(buff);
553 			if (driver_byte(retval) == DRIVER_ERROR)
554 				return SCSI_DH_DEV_TEMP_BUSY;
555 			return SCSI_DH_IO;
556 		}
557 
558 		/*
559 		 * submit_rtpg() has failed on existing arrays
560 		 * when requesting extended header info, and
561 		 * the array doesn't support extended headers,
562 		 * even though it shouldn't according to T10.
563 		 * The retry without rtpg_ext_hdr_req set
564 		 * handles this.
565 		 */
566 		if (!(pg->flags & ALUA_RTPG_EXT_HDR_UNSUPP) &&
567 		    sense_hdr.sense_key == ILLEGAL_REQUEST &&
568 		    sense_hdr.asc == 0x24 && sense_hdr.ascq == 0) {
569 			pg->flags |= ALUA_RTPG_EXT_HDR_UNSUPP;
570 			goto retry;
571 		}
572 		/*
573 		 * Retry on ALUA state transition or if any
574 		 * UNIT ATTENTION occurred.
575 		 */
576 		if (sense_hdr.sense_key == NOT_READY &&
577 		    sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x0a)
578 			err = SCSI_DH_RETRY;
579 		else if (sense_hdr.sense_key == UNIT_ATTENTION)
580 			err = SCSI_DH_RETRY;
581 		if (err == SCSI_DH_RETRY &&
582 		    pg->expiry != 0 && time_before(jiffies, pg->expiry)) {
583 			sdev_printk(KERN_ERR, sdev, "%s: rtpg retry\n",
584 				    ALUA_DH_NAME);
585 			scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
586 			kfree(buff);
587 			return err;
588 		}
589 		sdev_printk(KERN_ERR, sdev, "%s: rtpg failed\n",
590 			    ALUA_DH_NAME);
591 		scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
592 		kfree(buff);
593 		pg->expiry = 0;
594 		return SCSI_DH_IO;
595 	}
596 
597 	len = get_unaligned_be32(&buff[0]) + 4;
598 
599 	if (len > bufflen) {
600 		/* Resubmit with the correct length */
601 		kfree(buff);
602 		bufflen = len;
603 		buff = kmalloc(bufflen, GFP_KERNEL);
604 		if (!buff) {
605 			sdev_printk(KERN_WARNING, sdev,
606 				    "%s: kmalloc buffer failed\n",__func__);
607 			/* Temporary failure, bypass */
608 			pg->expiry = 0;
609 			return SCSI_DH_DEV_TEMP_BUSY;
610 		}
611 		goto retry;
612 	}
613 
614 	orig_transition_tmo = pg->transition_tmo;
615 	if ((buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR && buff[5] != 0)
616 		pg->transition_tmo = buff[5];
617 	else
618 		pg->transition_tmo = ALUA_FAILOVER_TIMEOUT;
619 
620 	if (orig_transition_tmo != pg->transition_tmo) {
621 		sdev_printk(KERN_INFO, sdev,
622 			    "%s: transition timeout set to %d seconds\n",
623 			    ALUA_DH_NAME, pg->transition_tmo);
624 		pg->expiry = jiffies + pg->transition_tmo * HZ;
625 	}
626 
627 	if ((buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR)
628 		tpg_desc_tbl_off = 8;
629 	else
630 		tpg_desc_tbl_off = 4;
631 
632 	for (k = tpg_desc_tbl_off, desc = buff + tpg_desc_tbl_off;
633 	     k < len;
634 	     k += off, desc += off) {
635 		u16 group_id = get_unaligned_be16(&desc[2]);
636 
637 		spin_lock_irqsave(&port_group_lock, flags);
638 		tmp_pg = alua_find_get_pg(pg->device_id_str, pg->device_id_len,
639 					  group_id);
640 		spin_unlock_irqrestore(&port_group_lock, flags);
641 		if (tmp_pg) {
642 			if (spin_trylock_irqsave(&tmp_pg->lock, flags)) {
643 				if ((tmp_pg == pg) ||
644 				    !(tmp_pg->flags & ALUA_PG_RUNNING)) {
645 					struct alua_dh_data *h;
646 
647 					tmp_pg->state = desc[0] & 0x0f;
648 					tmp_pg->pref = desc[0] >> 7;
649 					rcu_read_lock();
650 					list_for_each_entry_rcu(h,
651 						&tmp_pg->dh_list, node) {
652 						/* h->sdev should always be valid */
653 						BUG_ON(!h->sdev);
654 						h->sdev->access_state = desc[0];
655 					}
656 					rcu_read_unlock();
657 				}
658 				if (tmp_pg == pg)
659 					valid_states = desc[1];
660 				spin_unlock_irqrestore(&tmp_pg->lock, flags);
661 			}
662 			kref_put(&tmp_pg->kref, release_port_group);
663 		}
664 		off = 8 + (desc[7] * 4);
665 	}
666 
667 	spin_lock_irqsave(&pg->lock, flags);
668 	sdev_printk(KERN_INFO, sdev,
669 		    "%s: port group %02x state %c %s supports %c%c%c%c%c%c%c\n",
670 		    ALUA_DH_NAME, pg->group_id, print_alua_state(pg->state),
671 		    pg->pref ? "preferred" : "non-preferred",
672 		    valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
673 		    valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
674 		    valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
675 		    valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u',
676 		    valid_states&TPGS_SUPPORT_STANDBY?'S':'s',
677 		    valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n',
678 		    valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a');
679 
680 	switch (pg->state) {
681 	case SCSI_ACCESS_STATE_TRANSITIONING:
682 		if (time_before(jiffies, pg->expiry)) {
683 			/* State transition, retry */
684 			pg->interval = 2;
685 			err = SCSI_DH_RETRY;
686 		} else {
687 			struct alua_dh_data *h;
688 
689 			/* Transitioning time exceeded, set port to standby */
690 			err = SCSI_DH_IO;
691 			pg->state = SCSI_ACCESS_STATE_STANDBY;
692 			pg->expiry = 0;
693 			rcu_read_lock();
694 			list_for_each_entry_rcu(h, &pg->dh_list, node) {
695 				BUG_ON(!h->sdev);
696 				h->sdev->access_state =
697 					(pg->state & SCSI_ACCESS_STATE_MASK);
698 				if (pg->pref)
699 					h->sdev->access_state |=
700 						SCSI_ACCESS_STATE_PREFERRED;
701 			}
702 			rcu_read_unlock();
703 		}
704 		break;
705 	case SCSI_ACCESS_STATE_OFFLINE:
706 		/* Path unusable */
707 		err = SCSI_DH_DEV_OFFLINED;
708 		pg->expiry = 0;
709 		break;
710 	default:
711 		/* Useable path if active */
712 		err = SCSI_DH_OK;
713 		pg->expiry = 0;
714 		break;
715 	}
716 	spin_unlock_irqrestore(&pg->lock, flags);
717 	kfree(buff);
718 	return err;
719 }
720 
721 /*
722  * alua_stpg - Issue a SET TARGET PORT GROUP command
723  *
724  * Issue a SET TARGET PORT GROUP command and evaluate the
725  * response. Returns SCSI_DH_RETRY per default to trigger
726  * a re-evaluation of the target group state or SCSI_DH_OK
727  * if no further action needs to be taken.
728  */
729 static unsigned alua_stpg(struct scsi_device *sdev, struct alua_port_group *pg)
730 {
731 	int retval;
732 	struct scsi_sense_hdr sense_hdr;
733 
734 	if (!(pg->tpgs & TPGS_MODE_EXPLICIT)) {
735 		/* Only implicit ALUA supported, retry */
736 		return SCSI_DH_RETRY;
737 	}
738 	switch (pg->state) {
739 	case SCSI_ACCESS_STATE_OPTIMAL:
740 		return SCSI_DH_OK;
741 	case SCSI_ACCESS_STATE_ACTIVE:
742 		if ((pg->flags & ALUA_OPTIMIZE_STPG) &&
743 		    !pg->pref &&
744 		    (pg->tpgs & TPGS_MODE_IMPLICIT))
745 			return SCSI_DH_OK;
746 		break;
747 	case SCSI_ACCESS_STATE_STANDBY:
748 	case SCSI_ACCESS_STATE_UNAVAILABLE:
749 		break;
750 	case SCSI_ACCESS_STATE_OFFLINE:
751 		return SCSI_DH_IO;
752 	case SCSI_ACCESS_STATE_TRANSITIONING:
753 		break;
754 	default:
755 		sdev_printk(KERN_INFO, sdev,
756 			    "%s: stpg failed, unhandled TPGS state %d",
757 			    ALUA_DH_NAME, pg->state);
758 		return SCSI_DH_NOSYS;
759 	}
760 	retval = submit_stpg(sdev, pg->group_id, &sense_hdr);
761 
762 	if (retval) {
763 		if (!scsi_sense_valid(&sense_hdr)) {
764 			sdev_printk(KERN_INFO, sdev,
765 				    "%s: stpg failed, result %d",
766 				    ALUA_DH_NAME, retval);
767 			if (driver_byte(retval) == DRIVER_ERROR)
768 				return SCSI_DH_DEV_TEMP_BUSY;
769 		} else {
770 			sdev_printk(KERN_INFO, sdev, "%s: stpg failed\n",
771 				    ALUA_DH_NAME);
772 			scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
773 		}
774 	}
775 	/* Retry RTPG */
776 	return SCSI_DH_RETRY;
777 }
778 
779 static void alua_rtpg_work(struct work_struct *work)
780 {
781 	struct alua_port_group *pg =
782 		container_of(work, struct alua_port_group, rtpg_work.work);
783 	struct scsi_device *sdev;
784 	LIST_HEAD(qdata_list);
785 	int err = SCSI_DH_OK;
786 	struct alua_queue_data *qdata, *tmp;
787 	unsigned long flags;
788 	struct workqueue_struct *alua_wq = kaluad_wq;
789 
790 	spin_lock_irqsave(&pg->lock, flags);
791 	sdev = pg->rtpg_sdev;
792 	if (!sdev) {
793 		WARN_ON(pg->flags & ALUA_PG_RUN_RTPG);
794 		WARN_ON(pg->flags & ALUA_PG_RUN_STPG);
795 		spin_unlock_irqrestore(&pg->lock, flags);
796 		kref_put(&pg->kref, release_port_group);
797 		return;
798 	}
799 	if (pg->flags & ALUA_SYNC_STPG)
800 		alua_wq = kaluad_sync_wq;
801 	pg->flags |= ALUA_PG_RUNNING;
802 	if (pg->flags & ALUA_PG_RUN_RTPG) {
803 		int state = pg->state;
804 
805 		pg->flags &= ~ALUA_PG_RUN_RTPG;
806 		spin_unlock_irqrestore(&pg->lock, flags);
807 		if (state == SCSI_ACCESS_STATE_TRANSITIONING) {
808 			if (alua_tur(sdev) == SCSI_DH_RETRY) {
809 				spin_lock_irqsave(&pg->lock, flags);
810 				pg->flags &= ~ALUA_PG_RUNNING;
811 				pg->flags |= ALUA_PG_RUN_RTPG;
812 				spin_unlock_irqrestore(&pg->lock, flags);
813 				queue_delayed_work(alua_wq, &pg->rtpg_work,
814 						   pg->interval * HZ);
815 				return;
816 			}
817 			/* Send RTPG on failure or if TUR indicates SUCCESS */
818 		}
819 		err = alua_rtpg(sdev, pg);
820 		spin_lock_irqsave(&pg->lock, flags);
821 		if (err == SCSI_DH_RETRY || pg->flags & ALUA_PG_RUN_RTPG) {
822 			pg->flags &= ~ALUA_PG_RUNNING;
823 			pg->flags |= ALUA_PG_RUN_RTPG;
824 			spin_unlock_irqrestore(&pg->lock, flags);
825 			queue_delayed_work(alua_wq, &pg->rtpg_work,
826 					   pg->interval * HZ);
827 			return;
828 		}
829 		if (err != SCSI_DH_OK)
830 			pg->flags &= ~ALUA_PG_RUN_STPG;
831 	}
832 	if (pg->flags & ALUA_PG_RUN_STPG) {
833 		pg->flags &= ~ALUA_PG_RUN_STPG;
834 		spin_unlock_irqrestore(&pg->lock, flags);
835 		err = alua_stpg(sdev, pg);
836 		spin_lock_irqsave(&pg->lock, flags);
837 		if (err == SCSI_DH_RETRY || pg->flags & ALUA_PG_RUN_RTPG) {
838 			pg->flags |= ALUA_PG_RUN_RTPG;
839 			pg->interval = 0;
840 			pg->flags &= ~ALUA_PG_RUNNING;
841 			spin_unlock_irqrestore(&pg->lock, flags);
842 			queue_delayed_work(alua_wq, &pg->rtpg_work,
843 					   pg->interval * HZ);
844 			return;
845 		}
846 	}
847 
848 	list_splice_init(&pg->rtpg_list, &qdata_list);
849 	pg->rtpg_sdev = NULL;
850 	spin_unlock_irqrestore(&pg->lock, flags);
851 
852 	list_for_each_entry_safe(qdata, tmp, &qdata_list, entry) {
853 		list_del(&qdata->entry);
854 		if (qdata->callback_fn)
855 			qdata->callback_fn(qdata->callback_data, err);
856 		kfree(qdata);
857 	}
858 	spin_lock_irqsave(&pg->lock, flags);
859 	pg->flags &= ~ALUA_PG_RUNNING;
860 	spin_unlock_irqrestore(&pg->lock, flags);
861 	scsi_device_put(sdev);
862 	kref_put(&pg->kref, release_port_group);
863 }
864 
865 static void alua_rtpg_queue(struct alua_port_group *pg,
866 			    struct scsi_device *sdev,
867 			    struct alua_queue_data *qdata, bool force)
868 {
869 	int start_queue = 0;
870 	unsigned long flags;
871 	struct workqueue_struct *alua_wq = kaluad_wq;
872 
873 	if (!pg)
874 		return;
875 
876 	spin_lock_irqsave(&pg->lock, flags);
877 	if (qdata) {
878 		list_add_tail(&qdata->entry, &pg->rtpg_list);
879 		pg->flags |= ALUA_PG_RUN_STPG;
880 		force = true;
881 	}
882 	if (pg->rtpg_sdev == NULL) {
883 		pg->interval = 0;
884 		pg->flags |= ALUA_PG_RUN_RTPG;
885 		kref_get(&pg->kref);
886 		pg->rtpg_sdev = sdev;
887 		scsi_device_get(sdev);
888 		start_queue = 1;
889 	} else if (!(pg->flags & ALUA_PG_RUN_RTPG) && force) {
890 		pg->flags |= ALUA_PG_RUN_RTPG;
891 		/* Do not queue if the worker is already running */
892 		if (!(pg->flags & ALUA_PG_RUNNING)) {
893 			kref_get(&pg->kref);
894 			sdev = NULL;
895 			start_queue = 1;
896 		}
897 	}
898 
899 	if (pg->flags & ALUA_SYNC_STPG)
900 		alua_wq = kaluad_sync_wq;
901 	spin_unlock_irqrestore(&pg->lock, flags);
902 
903 	if (start_queue &&
904 	    !queue_delayed_work(alua_wq, &pg->rtpg_work,
905 				msecs_to_jiffies(ALUA_RTPG_DELAY_MSECS))) {
906 		if (sdev)
907 			scsi_device_put(sdev);
908 		kref_put(&pg->kref, release_port_group);
909 	}
910 }
911 
912 /*
913  * alua_initialize - Initialize ALUA state
914  * @sdev: the device to be initialized
915  *
916  * For the prep_fn to work correctly we have
917  * to initialize the ALUA state for the device.
918  */
919 static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
920 {
921 	int err = SCSI_DH_DEV_UNSUPP, tpgs;
922 
923 	mutex_lock(&h->init_mutex);
924 	tpgs = alua_check_tpgs(sdev);
925 	if (tpgs != TPGS_MODE_NONE)
926 		err = alua_check_vpd(sdev, h, tpgs);
927 	h->init_error = err;
928 	mutex_unlock(&h->init_mutex);
929 	return err;
930 }
931 /*
932  * alua_set_params - set/unset the optimize flag
933  * @sdev: device on the path to be activated
934  * params - parameters in the following format
935  *      "no_of_params\0param1\0param2\0param3\0...\0"
936  * For example, to set the flag pass the following parameters
937  * from multipath.conf
938  *     hardware_handler        "2 alua 1"
939  */
940 static int alua_set_params(struct scsi_device *sdev, const char *params)
941 {
942 	struct alua_dh_data *h = sdev->handler_data;
943 	struct alua_port_group __rcu *pg = NULL;
944 	unsigned int optimize = 0, argc;
945 	const char *p = params;
946 	int result = SCSI_DH_OK;
947 	unsigned long flags;
948 
949 	if ((sscanf(params, "%u", &argc) != 1) || (argc != 1))
950 		return -EINVAL;
951 
952 	while (*p++)
953 		;
954 	if ((sscanf(p, "%u", &optimize) != 1) || (optimize > 1))
955 		return -EINVAL;
956 
957 	rcu_read_lock();
958 	pg = rcu_dereference(h->pg);
959 	if (!pg) {
960 		rcu_read_unlock();
961 		return -ENXIO;
962 	}
963 	spin_lock_irqsave(&pg->lock, flags);
964 	if (optimize)
965 		pg->flags |= ALUA_OPTIMIZE_STPG;
966 	else
967 		pg->flags &= ~ALUA_OPTIMIZE_STPG;
968 	spin_unlock_irqrestore(&pg->lock, flags);
969 	rcu_read_unlock();
970 
971 	return result;
972 }
973 
974 /*
975  * alua_activate - activate a path
976  * @sdev: device on the path to be activated
977  *
978  * We're currently switching the port group to be activated only and
979  * let the array figure out the rest.
980  * There may be other arrays which require us to switch all port groups
981  * based on a certain policy. But until we actually encounter them it
982  * should be okay.
983  */
984 static int alua_activate(struct scsi_device *sdev,
985 			activate_complete fn, void *data)
986 {
987 	struct alua_dh_data *h = sdev->handler_data;
988 	int err = SCSI_DH_OK;
989 	struct alua_queue_data *qdata;
990 	struct alua_port_group __rcu *pg;
991 
992 	qdata = kzalloc(sizeof(*qdata), GFP_KERNEL);
993 	if (!qdata) {
994 		err = SCSI_DH_RES_TEMP_UNAVAIL;
995 		goto out;
996 	}
997 	qdata->callback_fn = fn;
998 	qdata->callback_data = data;
999 
1000 	mutex_lock(&h->init_mutex);
1001 	rcu_read_lock();
1002 	pg = rcu_dereference(h->pg);
1003 	if (!pg || !kref_get_unless_zero(&pg->kref)) {
1004 		rcu_read_unlock();
1005 		kfree(qdata);
1006 		err = h->init_error;
1007 		mutex_unlock(&h->init_mutex);
1008 		goto out;
1009 	}
1010 	fn = NULL;
1011 	rcu_read_unlock();
1012 	mutex_unlock(&h->init_mutex);
1013 
1014 	alua_rtpg_queue(pg, sdev, qdata, true);
1015 	kref_put(&pg->kref, release_port_group);
1016 out:
1017 	if (fn)
1018 		fn(data, err);
1019 	return 0;
1020 }
1021 
1022 /*
1023  * alua_check - check path status
1024  * @sdev: device on the path to be checked
1025  *
1026  * Check the device status
1027  */
1028 static void alua_check(struct scsi_device *sdev, bool force)
1029 {
1030 	struct alua_dh_data *h = sdev->handler_data;
1031 	struct alua_port_group *pg;
1032 
1033 	rcu_read_lock();
1034 	pg = rcu_dereference(h->pg);
1035 	if (!pg || !kref_get_unless_zero(&pg->kref)) {
1036 		rcu_read_unlock();
1037 		return;
1038 	}
1039 	rcu_read_unlock();
1040 
1041 	alua_rtpg_queue(pg, sdev, NULL, force);
1042 	kref_put(&pg->kref, release_port_group);
1043 }
1044 
1045 /*
1046  * alua_prep_fn - request callback
1047  *
1048  * Fail I/O to all paths not in state
1049  * active/optimized or active/non-optimized.
1050  */
1051 static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
1052 {
1053 	struct alua_dh_data *h = sdev->handler_data;
1054 	struct alua_port_group __rcu *pg;
1055 	unsigned char state = SCSI_ACCESS_STATE_OPTIMAL;
1056 	int ret = BLKPREP_OK;
1057 
1058 	rcu_read_lock();
1059 	pg = rcu_dereference(h->pg);
1060 	if (pg)
1061 		state = pg->state;
1062 	rcu_read_unlock();
1063 	if (state == SCSI_ACCESS_STATE_TRANSITIONING)
1064 		ret = BLKPREP_DEFER;
1065 	else if (state != SCSI_ACCESS_STATE_OPTIMAL &&
1066 		 state != SCSI_ACCESS_STATE_ACTIVE &&
1067 		 state != SCSI_ACCESS_STATE_LBA) {
1068 		ret = BLKPREP_KILL;
1069 		req->cmd_flags |= REQ_QUIET;
1070 	}
1071 	return ret;
1072 
1073 }
1074 
1075 static void alua_rescan(struct scsi_device *sdev)
1076 {
1077 	struct alua_dh_data *h = sdev->handler_data;
1078 
1079 	alua_initialize(sdev, h);
1080 }
1081 
1082 /*
1083  * alua_bus_attach - Attach device handler
1084  * @sdev: device to be attached to
1085  */
1086 static int alua_bus_attach(struct scsi_device *sdev)
1087 {
1088 	struct alua_dh_data *h;
1089 	int err, ret = -EINVAL;
1090 
1091 	h = kzalloc(sizeof(*h) , GFP_KERNEL);
1092 	if (!h)
1093 		return -ENOMEM;
1094 	spin_lock_init(&h->pg_lock);
1095 	rcu_assign_pointer(h->pg, NULL);
1096 	h->init_error = SCSI_DH_OK;
1097 	h->sdev = sdev;
1098 	INIT_LIST_HEAD(&h->node);
1099 
1100 	mutex_init(&h->init_mutex);
1101 	err = alua_initialize(sdev, h);
1102 	if (err == SCSI_DH_NOMEM)
1103 		ret = -ENOMEM;
1104 	if (err != SCSI_DH_OK && err != SCSI_DH_DEV_OFFLINED)
1105 		goto failed;
1106 
1107 	sdev->handler_data = h;
1108 	return 0;
1109 failed:
1110 	kfree(h);
1111 	return ret;
1112 }
1113 
1114 /*
1115  * alua_bus_detach - Detach device handler
1116  * @sdev: device to be detached from
1117  */
1118 static void alua_bus_detach(struct scsi_device *sdev)
1119 {
1120 	struct alua_dh_data *h = sdev->handler_data;
1121 	struct alua_port_group *pg;
1122 
1123 	spin_lock(&h->pg_lock);
1124 	pg = h->pg;
1125 	rcu_assign_pointer(h->pg, NULL);
1126 	h->sdev = NULL;
1127 	spin_unlock(&h->pg_lock);
1128 	if (pg) {
1129 		spin_lock_irq(&pg->lock);
1130 		list_del_rcu(&h->node);
1131 		spin_unlock_irq(&pg->lock);
1132 		kref_put(&pg->kref, release_port_group);
1133 	}
1134 	sdev->handler_data = NULL;
1135 	kfree(h);
1136 }
1137 
1138 static struct scsi_device_handler alua_dh = {
1139 	.name = ALUA_DH_NAME,
1140 	.module = THIS_MODULE,
1141 	.attach = alua_bus_attach,
1142 	.detach = alua_bus_detach,
1143 	.prep_fn = alua_prep_fn,
1144 	.check_sense = alua_check_sense,
1145 	.activate = alua_activate,
1146 	.rescan = alua_rescan,
1147 	.set_params = alua_set_params,
1148 };
1149 
1150 static int __init alua_init(void)
1151 {
1152 	int r;
1153 
1154 	kaluad_wq = alloc_workqueue("kaluad", WQ_MEM_RECLAIM, 0);
1155 	if (!kaluad_wq) {
1156 		/* Temporary failure, bypass */
1157 		return SCSI_DH_DEV_TEMP_BUSY;
1158 	}
1159 	kaluad_sync_wq = create_workqueue("kaluad_sync");
1160 	if (!kaluad_sync_wq) {
1161 		destroy_workqueue(kaluad_wq);
1162 		return SCSI_DH_DEV_TEMP_BUSY;
1163 	}
1164 	r = scsi_register_device_handler(&alua_dh);
1165 	if (r != 0) {
1166 		printk(KERN_ERR "%s: Failed to register scsi device handler",
1167 			ALUA_DH_NAME);
1168 		destroy_workqueue(kaluad_sync_wq);
1169 		destroy_workqueue(kaluad_wq);
1170 	}
1171 	return r;
1172 }
1173 
1174 static void __exit alua_exit(void)
1175 {
1176 	scsi_unregister_device_handler(&alua_dh);
1177 	destroy_workqueue(kaluad_sync_wq);
1178 	destroy_workqueue(kaluad_wq);
1179 }
1180 
1181 module_init(alua_init);
1182 module_exit(alua_exit);
1183 
1184 MODULE_DESCRIPTION("DM Multipath ALUA support");
1185 MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
1186 MODULE_LICENSE("GPL");
1187 MODULE_VERSION(ALUA_DH_VER);
1188