1 /*******************************************************************************
2  * Filename:  target_core_alua.c
3  *
4  * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
5  *
6  * (c) Copyright 2009-2013 Datera, Inc.
7  *
8  * Nicholas A. Bellinger <nab@kernel.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23  *
24  ******************************************************************************/
25 
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <linux/configfs.h>
29 #include <linux/delay.h>
30 #include <linux/export.h>
31 #include <linux/fcntl.h>
32 #include <linux/file.h>
33 #include <linux/fs.h>
34 #include <scsi/scsi_proto.h>
35 #include <asm/unaligned.h>
36 
37 #include <target/target_core_base.h>
38 #include <target/target_core_backend.h>
39 #include <target/target_core_fabric.h>
40 
41 #include "target_core_internal.h"
42 #include "target_core_alua.h"
43 #include "target_core_ua.h"
44 
45 static sense_reason_t core_alua_check_transition(int state, int valid,
46 						 int *primary, int explicit);
47 static int core_alua_set_tg_pt_secondary_state(
48 		struct se_lun *lun, int explicit, int offline);
49 
50 static char *core_alua_dump_state(int state);
51 
52 static void __target_attach_tg_pt_gp(struct se_lun *lun,
53 		struct t10_alua_tg_pt_gp *tg_pt_gp);
54 
55 static u16 alua_lu_gps_counter;
56 static u32 alua_lu_gps_count;
57 
58 static DEFINE_SPINLOCK(lu_gps_lock);
59 static LIST_HEAD(lu_gps_list);
60 
61 struct t10_alua_lu_gp *default_lu_gp;
62 
63 /*
64  * REPORT REFERRALS
65  *
66  * See sbc3r35 section 5.23
67  */
68 sense_reason_t
69 target_emulate_report_referrals(struct se_cmd *cmd)
70 {
71 	struct se_device *dev = cmd->se_dev;
72 	struct t10_alua_lba_map *map;
73 	struct t10_alua_lba_map_member *map_mem;
74 	unsigned char *buf;
75 	u32 rd_len = 0, off;
76 
77 	if (cmd->data_length < 4) {
78 		pr_warn("REPORT REFERRALS allocation length %u too"
79 			" small\n", cmd->data_length);
80 		return TCM_INVALID_CDB_FIELD;
81 	}
82 
83 	buf = transport_kmap_data_sg(cmd);
84 	if (!buf)
85 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
86 
87 	off = 4;
88 	spin_lock(&dev->t10_alua.lba_map_lock);
89 	if (list_empty(&dev->t10_alua.lba_map_list)) {
90 		spin_unlock(&dev->t10_alua.lba_map_lock);
91 		transport_kunmap_data_sg(cmd);
92 
93 		return TCM_UNSUPPORTED_SCSI_OPCODE;
94 	}
95 
96 	list_for_each_entry(map, &dev->t10_alua.lba_map_list,
97 			    lba_map_list) {
98 		int desc_num = off + 3;
99 		int pg_num;
100 
101 		off += 4;
102 		if (cmd->data_length > off)
103 			put_unaligned_be64(map->lba_map_first_lba, &buf[off]);
104 		off += 8;
105 		if (cmd->data_length > off)
106 			put_unaligned_be64(map->lba_map_last_lba, &buf[off]);
107 		off += 8;
108 		rd_len += 20;
109 		pg_num = 0;
110 		list_for_each_entry(map_mem, &map->lba_map_mem_list,
111 				    lba_map_mem_list) {
112 			int alua_state = map_mem->lba_map_mem_alua_state;
113 			int alua_pg_id = map_mem->lba_map_mem_alua_pg_id;
114 
115 			if (cmd->data_length > off)
116 				buf[off] = alua_state & 0x0f;
117 			off += 2;
118 			if (cmd->data_length > off)
119 				buf[off] = (alua_pg_id >> 8) & 0xff;
120 			off++;
121 			if (cmd->data_length > off)
122 				buf[off] = (alua_pg_id & 0xff);
123 			off++;
124 			rd_len += 4;
125 			pg_num++;
126 		}
127 		if (cmd->data_length > desc_num)
128 			buf[desc_num] = pg_num;
129 	}
130 	spin_unlock(&dev->t10_alua.lba_map_lock);
131 
132 	/*
133 	 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
134 	 */
135 	put_unaligned_be16(rd_len, &buf[2]);
136 
137 	transport_kunmap_data_sg(cmd);
138 
139 	target_complete_cmd(cmd, GOOD);
140 	return 0;
141 }
142 
143 /*
144  * REPORT_TARGET_PORT_GROUPS
145  *
146  * See spc4r17 section 6.27
147  */
148 sense_reason_t
149 target_emulate_report_target_port_groups(struct se_cmd *cmd)
150 {
151 	struct se_device *dev = cmd->se_dev;
152 	struct t10_alua_tg_pt_gp *tg_pt_gp;
153 	struct se_lun *lun;
154 	unsigned char *buf;
155 	u32 rd_len = 0, off;
156 	int ext_hdr = (cmd->t_task_cdb[1] & 0x20);
157 
158 	/*
159 	 * Skip over RESERVED area to first Target port group descriptor
160 	 * depending on the PARAMETER DATA FORMAT type..
161 	 */
162 	if (ext_hdr != 0)
163 		off = 8;
164 	else
165 		off = 4;
166 
167 	if (cmd->data_length < off) {
168 		pr_warn("REPORT TARGET PORT GROUPS allocation length %u too"
169 			" small for %s header\n", cmd->data_length,
170 			(ext_hdr) ? "extended" : "normal");
171 		return TCM_INVALID_CDB_FIELD;
172 	}
173 	buf = transport_kmap_data_sg(cmd);
174 	if (!buf)
175 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
176 
177 	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
178 	list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
179 			tg_pt_gp_list) {
180 		/*
181 		 * Check if the Target port group and Target port descriptor list
182 		 * based on tg_pt_gp_members count will fit into the response payload.
183 		 * Otherwise, bump rd_len to let the initiator know we have exceeded
184 		 * the allocation length and the response is truncated.
185 		 */
186 		if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) >
187 		     cmd->data_length) {
188 			rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4);
189 			continue;
190 		}
191 		/*
192 		 * PREF: Preferred target port bit, determine if this
193 		 * bit should be set for port group.
194 		 */
195 		if (tg_pt_gp->tg_pt_gp_pref)
196 			buf[off] = 0x80;
197 		/*
198 		 * Set the ASYMMETRIC ACCESS State
199 		 */
200 		buf[off++] |= (atomic_read(
201 			&tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff);
202 		/*
203 		 * Set supported ASYMMETRIC ACCESS State bits
204 		 */
205 		buf[off++] |= tg_pt_gp->tg_pt_gp_alua_supported_states;
206 		/*
207 		 * TARGET PORT GROUP
208 		 */
209 		buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff);
210 		buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff);
211 
212 		off++; /* Skip over Reserved */
213 		/*
214 		 * STATUS CODE
215 		 */
216 		buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
217 		/*
218 		 * Vendor Specific field
219 		 */
220 		buf[off++] = 0x00;
221 		/*
222 		 * TARGET PORT COUNT
223 		 */
224 		buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
225 		rd_len += 8;
226 
227 		spin_lock(&tg_pt_gp->tg_pt_gp_lock);
228 		list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
229 				lun_tg_pt_gp_link) {
230 			/*
231 			 * Start Target Port descriptor format
232 			 *
233 			 * See spc4r17 section 6.2.7 Table 247
234 			 */
235 			off += 2; /* Skip over Obsolete */
236 			/*
237 			 * Set RELATIVE TARGET PORT IDENTIFIER
238 			 */
239 			buf[off++] = ((lun->lun_rtpi >> 8) & 0xff);
240 			buf[off++] = (lun->lun_rtpi & 0xff);
241 			rd_len += 4;
242 		}
243 		spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
244 	}
245 	spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
246 	/*
247 	 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
248 	 */
249 	put_unaligned_be32(rd_len, &buf[0]);
250 
251 	/*
252 	 * Fill in the Extended header parameter data format if requested
253 	 */
254 	if (ext_hdr != 0) {
255 		buf[4] = 0x10;
256 		/*
257 		 * Set the implicit transition time (in seconds) for the application
258 		 * client to use as a base for it's transition timeout value.
259 		 *
260 		 * Use the current tg_pt_gp_mem -> tg_pt_gp membership from the LUN
261 		 * this CDB was received upon to determine this value individually
262 		 * for ALUA target port group.
263 		 */
264 		spin_lock(&cmd->se_lun->lun_tg_pt_gp_lock);
265 		tg_pt_gp = cmd->se_lun->lun_tg_pt_gp;
266 		if (tg_pt_gp)
267 			buf[5] = tg_pt_gp->tg_pt_gp_implicit_trans_secs;
268 		spin_unlock(&cmd->se_lun->lun_tg_pt_gp_lock);
269 	}
270 	transport_kunmap_data_sg(cmd);
271 
272 	target_complete_cmd(cmd, GOOD);
273 	return 0;
274 }
275 
276 /*
277  * SET_TARGET_PORT_GROUPS for explicit ALUA operation.
278  *
279  * See spc4r17 section 6.35
280  */
281 sense_reason_t
282 target_emulate_set_target_port_groups(struct se_cmd *cmd)
283 {
284 	struct se_device *dev = cmd->se_dev;
285 	struct se_lun *l_lun = cmd->se_lun;
286 	struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
287 	struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
288 	unsigned char *buf;
289 	unsigned char *ptr;
290 	sense_reason_t rc = TCM_NO_SENSE;
291 	u32 len = 4; /* Skip over RESERVED area in header */
292 	int alua_access_state, primary = 0, valid_states;
293 	u16 tg_pt_id, rtpi;
294 
295 	if (cmd->data_length < 4) {
296 		pr_warn("SET TARGET PORT GROUPS parameter list length %u too"
297 			" small\n", cmd->data_length);
298 		return TCM_INVALID_PARAMETER_LIST;
299 	}
300 
301 	buf = transport_kmap_data_sg(cmd);
302 	if (!buf)
303 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
304 
305 	/*
306 	 * Determine if explicit ALUA via SET_TARGET_PORT_GROUPS is allowed
307 	 * for the local tg_pt_gp.
308 	 */
309 	spin_lock(&l_lun->lun_tg_pt_gp_lock);
310 	l_tg_pt_gp = l_lun->lun_tg_pt_gp;
311 	if (!l_tg_pt_gp) {
312 		spin_unlock(&l_lun->lun_tg_pt_gp_lock);
313 		pr_err("Unable to access l_lun->tg_pt_gp\n");
314 		rc = TCM_UNSUPPORTED_SCSI_OPCODE;
315 		goto out;
316 	}
317 
318 	if (!(l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)) {
319 		spin_unlock(&l_lun->lun_tg_pt_gp_lock);
320 		pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
321 				" while TPGS_EXPLICIT_ALUA is disabled\n");
322 		rc = TCM_UNSUPPORTED_SCSI_OPCODE;
323 		goto out;
324 	}
325 	valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states;
326 	spin_unlock(&l_lun->lun_tg_pt_gp_lock);
327 
328 	ptr = &buf[4]; /* Skip over RESERVED area in header */
329 
330 	while (len < cmd->data_length) {
331 		bool found = false;
332 		alua_access_state = (ptr[0] & 0x0f);
333 		/*
334 		 * Check the received ALUA access state, and determine if
335 		 * the state is a primary or secondary target port asymmetric
336 		 * access state.
337 		 */
338 		rc = core_alua_check_transition(alua_access_state, valid_states,
339 						&primary, 1);
340 		if (rc) {
341 			/*
342 			 * If the SET TARGET PORT GROUPS attempts to establish
343 			 * an invalid combination of target port asymmetric
344 			 * access states or attempts to establish an
345 			 * unsupported target port asymmetric access state,
346 			 * then the command shall be terminated with CHECK
347 			 * CONDITION status, with the sense key set to ILLEGAL
348 			 * REQUEST, and the additional sense code set to INVALID
349 			 * FIELD IN PARAMETER LIST.
350 			 */
351 			goto out;
352 		}
353 
354 		/*
355 		 * If the ASYMMETRIC ACCESS STATE field (see table 267)
356 		 * specifies a primary target port asymmetric access state,
357 		 * then the TARGET PORT GROUP OR TARGET PORT field specifies
358 		 * a primary target port group for which the primary target
359 		 * port asymmetric access state shall be changed. If the
360 		 * ASYMMETRIC ACCESS STATE field specifies a secondary target
361 		 * port asymmetric access state, then the TARGET PORT GROUP OR
362 		 * TARGET PORT field specifies the relative target port
363 		 * identifier (see 3.1.120) of the target port for which the
364 		 * secondary target port asymmetric access state shall be
365 		 * changed.
366 		 */
367 		if (primary) {
368 			tg_pt_id = get_unaligned_be16(ptr + 2);
369 			/*
370 			 * Locate the matching target port group ID from
371 			 * the global tg_pt_gp list
372 			 */
373 			spin_lock(&dev->t10_alua.tg_pt_gps_lock);
374 			list_for_each_entry(tg_pt_gp,
375 					&dev->t10_alua.tg_pt_gps_list,
376 					tg_pt_gp_list) {
377 				if (!tg_pt_gp->tg_pt_gp_valid_id)
378 					continue;
379 
380 				if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
381 					continue;
382 
383 				atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
384 
385 				spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
386 
387 				if (!core_alua_do_port_transition(tg_pt_gp,
388 						dev, l_lun, nacl,
389 						alua_access_state, 1))
390 					found = true;
391 
392 				spin_lock(&dev->t10_alua.tg_pt_gps_lock);
393 				atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
394 				break;
395 			}
396 			spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
397 		} else {
398 			struct se_lun *lun;
399 
400 			/*
401 			 * Extract the RELATIVE TARGET PORT IDENTIFIER to identify
402 			 * the Target Port in question for the the incoming
403 			 * SET_TARGET_PORT_GROUPS op.
404 			 */
405 			rtpi = get_unaligned_be16(ptr + 2);
406 			/*
407 			 * Locate the matching relative target port identifier
408 			 * for the struct se_device storage object.
409 			 */
410 			spin_lock(&dev->se_port_lock);
411 			list_for_each_entry(lun, &dev->dev_sep_list,
412 							lun_dev_link) {
413 				if (lun->lun_rtpi != rtpi)
414 					continue;
415 
416 				// XXX: racy unlock
417 				spin_unlock(&dev->se_port_lock);
418 
419 				if (!core_alua_set_tg_pt_secondary_state(
420 						lun, 1, 1))
421 					found = true;
422 
423 				spin_lock(&dev->se_port_lock);
424 				break;
425 			}
426 			spin_unlock(&dev->se_port_lock);
427 		}
428 
429 		if (!found) {
430 			rc = TCM_INVALID_PARAMETER_LIST;
431 			goto out;
432 		}
433 
434 		ptr += 4;
435 		len += 4;
436 	}
437 
438 out:
439 	transport_kunmap_data_sg(cmd);
440 	if (!rc)
441 		target_complete_cmd(cmd, GOOD);
442 	return rc;
443 }
444 
445 static inline void set_ascq(struct se_cmd *cmd, u8 alua_ascq)
446 {
447 	/*
448 	 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
449 	 * The ALUA additional sense code qualifier (ASCQ) is determined
450 	 * by the ALUA primary or secondary access state..
451 	 */
452 	pr_debug("[%s]: ALUA TG Port not available, "
453 		"SenseKey: NOT_READY, ASC/ASCQ: "
454 		"0x04/0x%02x\n",
455 		cmd->se_tfo->get_fabric_name(), alua_ascq);
456 
457 	cmd->scsi_asc = 0x04;
458 	cmd->scsi_ascq = alua_ascq;
459 }
460 
461 static inline void core_alua_state_nonoptimized(
462 	struct se_cmd *cmd,
463 	unsigned char *cdb,
464 	int nonop_delay_msecs)
465 {
466 	/*
467 	 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
468 	 * later to determine if processing of this cmd needs to be
469 	 * temporarily delayed for the Active/NonOptimized primary access state.
470 	 */
471 	cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
472 	cmd->alua_nonop_delay = nonop_delay_msecs;
473 }
474 
475 static inline int core_alua_state_lba_dependent(
476 	struct se_cmd *cmd,
477 	struct t10_alua_tg_pt_gp *tg_pt_gp)
478 {
479 	struct se_device *dev = cmd->se_dev;
480 	u64 segment_size, segment_mult, sectors, lba;
481 
482 	/* Only need to check for cdb actually containing LBAs */
483 	if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB))
484 		return 0;
485 
486 	spin_lock(&dev->t10_alua.lba_map_lock);
487 	segment_size = dev->t10_alua.lba_map_segment_size;
488 	segment_mult = dev->t10_alua.lba_map_segment_multiplier;
489 	sectors = cmd->data_length / dev->dev_attrib.block_size;
490 
491 	lba = cmd->t_task_lba;
492 	while (lba < cmd->t_task_lba + sectors) {
493 		struct t10_alua_lba_map *cur_map = NULL, *map;
494 		struct t10_alua_lba_map_member *map_mem;
495 
496 		list_for_each_entry(map, &dev->t10_alua.lba_map_list,
497 				    lba_map_list) {
498 			u64 start_lba, last_lba;
499 			u64 first_lba = map->lba_map_first_lba;
500 
501 			if (segment_mult) {
502 				u64 tmp = lba;
503 				start_lba = do_div(tmp, segment_size * segment_mult);
504 
505 				last_lba = first_lba + segment_size - 1;
506 				if (start_lba >= first_lba &&
507 				    start_lba <= last_lba) {
508 					lba += segment_size;
509 					cur_map = map;
510 					break;
511 				}
512 			} else {
513 				last_lba = map->lba_map_last_lba;
514 				if (lba >= first_lba && lba <= last_lba) {
515 					lba = last_lba + 1;
516 					cur_map = map;
517 					break;
518 				}
519 			}
520 		}
521 		if (!cur_map) {
522 			spin_unlock(&dev->t10_alua.lba_map_lock);
523 			set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
524 			return 1;
525 		}
526 		list_for_each_entry(map_mem, &cur_map->lba_map_mem_list,
527 				    lba_map_mem_list) {
528 			if (map_mem->lba_map_mem_alua_pg_id !=
529 			    tg_pt_gp->tg_pt_gp_id)
530 				continue;
531 			switch(map_mem->lba_map_mem_alua_state) {
532 			case ALUA_ACCESS_STATE_STANDBY:
533 				spin_unlock(&dev->t10_alua.lba_map_lock);
534 				set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
535 				return 1;
536 			case ALUA_ACCESS_STATE_UNAVAILABLE:
537 				spin_unlock(&dev->t10_alua.lba_map_lock);
538 				set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
539 				return 1;
540 			default:
541 				break;
542 			}
543 		}
544 	}
545 	spin_unlock(&dev->t10_alua.lba_map_lock);
546 	return 0;
547 }
548 
549 static inline int core_alua_state_standby(
550 	struct se_cmd *cmd,
551 	unsigned char *cdb)
552 {
553 	/*
554 	 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
555 	 * spc4r17 section 5.9.2.4.4
556 	 */
557 	switch (cdb[0]) {
558 	case INQUIRY:
559 	case LOG_SELECT:
560 	case LOG_SENSE:
561 	case MODE_SELECT:
562 	case MODE_SENSE:
563 	case REPORT_LUNS:
564 	case RECEIVE_DIAGNOSTIC:
565 	case SEND_DIAGNOSTIC:
566 	case READ_CAPACITY:
567 		return 0;
568 	case SERVICE_ACTION_IN_16:
569 		switch (cdb[1] & 0x1f) {
570 		case SAI_READ_CAPACITY_16:
571 			return 0;
572 		default:
573 			set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
574 			return 1;
575 		}
576 	case MAINTENANCE_IN:
577 		switch (cdb[1] & 0x1f) {
578 		case MI_REPORT_TARGET_PGS:
579 			return 0;
580 		default:
581 			set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
582 			return 1;
583 		}
584 	case MAINTENANCE_OUT:
585 		switch (cdb[1]) {
586 		case MO_SET_TARGET_PGS:
587 			return 0;
588 		default:
589 			set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
590 			return 1;
591 		}
592 	case REQUEST_SENSE:
593 	case PERSISTENT_RESERVE_IN:
594 	case PERSISTENT_RESERVE_OUT:
595 	case READ_BUFFER:
596 	case WRITE_BUFFER:
597 		return 0;
598 	default:
599 		set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
600 		return 1;
601 	}
602 
603 	return 0;
604 }
605 
606 static inline int core_alua_state_unavailable(
607 	struct se_cmd *cmd,
608 	unsigned char *cdb)
609 {
610 	/*
611 	 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
612 	 * spc4r17 section 5.9.2.4.5
613 	 */
614 	switch (cdb[0]) {
615 	case INQUIRY:
616 	case REPORT_LUNS:
617 		return 0;
618 	case MAINTENANCE_IN:
619 		switch (cdb[1] & 0x1f) {
620 		case MI_REPORT_TARGET_PGS:
621 			return 0;
622 		default:
623 			set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
624 			return 1;
625 		}
626 	case MAINTENANCE_OUT:
627 		switch (cdb[1]) {
628 		case MO_SET_TARGET_PGS:
629 			return 0;
630 		default:
631 			set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
632 			return 1;
633 		}
634 	case REQUEST_SENSE:
635 	case READ_BUFFER:
636 	case WRITE_BUFFER:
637 		return 0;
638 	default:
639 		set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
640 		return 1;
641 	}
642 
643 	return 0;
644 }
645 
646 static inline int core_alua_state_transition(
647 	struct se_cmd *cmd,
648 	unsigned char *cdb)
649 {
650 	/*
651 	 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITION as defined by
652 	 * spc4r17 section 5.9.2.5
653 	 */
654 	switch (cdb[0]) {
655 	case INQUIRY:
656 	case REPORT_LUNS:
657 		return 0;
658 	case MAINTENANCE_IN:
659 		switch (cdb[1] & 0x1f) {
660 		case MI_REPORT_TARGET_PGS:
661 			return 0;
662 		default:
663 			set_ascq(cmd, ASCQ_04H_ALUA_STATE_TRANSITION);
664 			return 1;
665 		}
666 	case REQUEST_SENSE:
667 	case READ_BUFFER:
668 	case WRITE_BUFFER:
669 		return 0;
670 	default:
671 		set_ascq(cmd, ASCQ_04H_ALUA_STATE_TRANSITION);
672 		return 1;
673 	}
674 
675 	return 0;
676 }
677 
678 /*
679  * return 1: Is used to signal LUN not accessible, and check condition/not ready
680  * return 0: Used to signal success
681  * return -1: Used to signal failure, and invalid cdb field
682  */
683 sense_reason_t
684 target_alua_state_check(struct se_cmd *cmd)
685 {
686 	struct se_device *dev = cmd->se_dev;
687 	unsigned char *cdb = cmd->t_task_cdb;
688 	struct se_lun *lun = cmd->se_lun;
689 	struct t10_alua_tg_pt_gp *tg_pt_gp;
690 	int out_alua_state, nonop_delay_msecs;
691 
692 	if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
693 		return 0;
694 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA)
695 		return 0;
696 
697 	/*
698 	 * First, check for a struct se_port specific secondary ALUA target port
699 	 * access state: OFFLINE
700 	 */
701 	if (atomic_read(&lun->lun_tg_pt_secondary_offline)) {
702 		pr_debug("ALUA: Got secondary offline status for local"
703 				" target port\n");
704 		set_ascq(cmd, ASCQ_04H_ALUA_OFFLINE);
705 		return TCM_CHECK_CONDITION_NOT_READY;
706 	}
707 
708 	if (!lun->lun_tg_pt_gp)
709 		return 0;
710 
711 	spin_lock(&lun->lun_tg_pt_gp_lock);
712 	tg_pt_gp = lun->lun_tg_pt_gp;
713 	out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
714 	nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
715 
716 	// XXX: keeps using tg_pt_gp witout reference after unlock
717 	spin_unlock(&lun->lun_tg_pt_gp_lock);
718 	/*
719 	 * Process ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED in a separate conditional
720 	 * statement so the compiler knows explicitly to check this case first.
721 	 * For the Optimized ALUA access state case, we want to process the
722 	 * incoming fabric cmd ASAP..
723 	 */
724 	if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED)
725 		return 0;
726 
727 	switch (out_alua_state) {
728 	case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
729 		core_alua_state_nonoptimized(cmd, cdb, nonop_delay_msecs);
730 		break;
731 	case ALUA_ACCESS_STATE_STANDBY:
732 		if (core_alua_state_standby(cmd, cdb))
733 			return TCM_CHECK_CONDITION_NOT_READY;
734 		break;
735 	case ALUA_ACCESS_STATE_UNAVAILABLE:
736 		if (core_alua_state_unavailable(cmd, cdb))
737 			return TCM_CHECK_CONDITION_NOT_READY;
738 		break;
739 	case ALUA_ACCESS_STATE_TRANSITION:
740 		if (core_alua_state_transition(cmd, cdb))
741 			return TCM_CHECK_CONDITION_NOT_READY;
742 		break;
743 	case ALUA_ACCESS_STATE_LBA_DEPENDENT:
744 		if (core_alua_state_lba_dependent(cmd, tg_pt_gp))
745 			return TCM_CHECK_CONDITION_NOT_READY;
746 		break;
747 	/*
748 	 * OFFLINE is a secondary ALUA target port group access state, that is
749 	 * handled above with struct se_lun->lun_tg_pt_secondary_offline=1
750 	 */
751 	case ALUA_ACCESS_STATE_OFFLINE:
752 	default:
753 		pr_err("Unknown ALUA access state: 0x%02x\n",
754 				out_alua_state);
755 		return TCM_INVALID_CDB_FIELD;
756 	}
757 
758 	return 0;
759 }
760 
761 /*
762  * Check implicit and explicit ALUA state change request.
763  */
764 static sense_reason_t
765 core_alua_check_transition(int state, int valid, int *primary, int explicit)
766 {
767 	/*
768 	 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
769 	 * defined as primary target port asymmetric access states.
770 	 */
771 	switch (state) {
772 	case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
773 		if (!(valid & ALUA_AO_SUP))
774 			goto not_supported;
775 		*primary = 1;
776 		break;
777 	case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
778 		if (!(valid & ALUA_AN_SUP))
779 			goto not_supported;
780 		*primary = 1;
781 		break;
782 	case ALUA_ACCESS_STATE_STANDBY:
783 		if (!(valid & ALUA_S_SUP))
784 			goto not_supported;
785 		*primary = 1;
786 		break;
787 	case ALUA_ACCESS_STATE_UNAVAILABLE:
788 		if (!(valid & ALUA_U_SUP))
789 			goto not_supported;
790 		*primary = 1;
791 		break;
792 	case ALUA_ACCESS_STATE_LBA_DEPENDENT:
793 		if (!(valid & ALUA_LBD_SUP))
794 			goto not_supported;
795 		*primary = 1;
796 		break;
797 	case ALUA_ACCESS_STATE_OFFLINE:
798 		/*
799 		 * OFFLINE state is defined as a secondary target port
800 		 * asymmetric access state.
801 		 */
802 		if (!(valid & ALUA_O_SUP))
803 			goto not_supported;
804 		*primary = 0;
805 		break;
806 	case ALUA_ACCESS_STATE_TRANSITION:
807 		if (!(valid & ALUA_T_SUP) || explicit)
808 			/*
809 			 * Transitioning is set internally and by tcmu daemon,
810 			 * and cannot be selected through a STPG.
811 			 */
812 			goto not_supported;
813 		*primary = 0;
814 		break;
815 	default:
816 		pr_err("Unknown ALUA access state: 0x%02x\n", state);
817 		return TCM_INVALID_PARAMETER_LIST;
818 	}
819 
820 	return 0;
821 
822 not_supported:
823 	pr_err("ALUA access state %s not supported",
824 	       core_alua_dump_state(state));
825 	return TCM_INVALID_PARAMETER_LIST;
826 }
827 
828 static char *core_alua_dump_state(int state)
829 {
830 	switch (state) {
831 	case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
832 		return "Active/Optimized";
833 	case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
834 		return "Active/NonOptimized";
835 	case ALUA_ACCESS_STATE_LBA_DEPENDENT:
836 		return "LBA Dependent";
837 	case ALUA_ACCESS_STATE_STANDBY:
838 		return "Standby";
839 	case ALUA_ACCESS_STATE_UNAVAILABLE:
840 		return "Unavailable";
841 	case ALUA_ACCESS_STATE_OFFLINE:
842 		return "Offline";
843 	case ALUA_ACCESS_STATE_TRANSITION:
844 		return "Transitioning";
845 	default:
846 		return "Unknown";
847 	}
848 
849 	return NULL;
850 }
851 
852 char *core_alua_dump_status(int status)
853 {
854 	switch (status) {
855 	case ALUA_STATUS_NONE:
856 		return "None";
857 	case ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG:
858 		return "Altered by Explicit STPG";
859 	case ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA:
860 		return "Altered by Implicit ALUA";
861 	default:
862 		return "Unknown";
863 	}
864 
865 	return NULL;
866 }
867 
868 /*
869  * Used by fabric modules to determine when we need to delay processing
870  * for the Active/NonOptimized paths..
871  */
872 int core_alua_check_nonop_delay(
873 	struct se_cmd *cmd)
874 {
875 	if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
876 		return 0;
877 	if (in_interrupt())
878 		return 0;
879 	/*
880 	 * The ALUA Active/NonOptimized access state delay can be disabled
881 	 * in via configfs with a value of zero
882 	 */
883 	if (!cmd->alua_nonop_delay)
884 		return 0;
885 	/*
886 	 * struct se_cmd->alua_nonop_delay gets set by a target port group
887 	 * defined interval in core_alua_state_nonoptimized()
888 	 */
889 	msleep_interruptible(cmd->alua_nonop_delay);
890 	return 0;
891 }
892 EXPORT_SYMBOL(core_alua_check_nonop_delay);
893 
894 static int core_alua_write_tpg_metadata(
895 	const char *path,
896 	unsigned char *md_buf,
897 	u32 md_buf_len)
898 {
899 	struct file *file = filp_open(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
900 	int ret;
901 
902 	if (IS_ERR(file)) {
903 		pr_err("filp_open(%s) for ALUA metadata failed\n", path);
904 		return -ENODEV;
905 	}
906 	ret = kernel_write(file, md_buf, md_buf_len, 0);
907 	if (ret < 0)
908 		pr_err("Error writing ALUA metadata file: %s\n", path);
909 	fput(file);
910 	return (ret < 0) ? -EIO : 0;
911 }
912 
913 /*
914  * Called with tg_pt_gp->tg_pt_gp_md_mutex held
915  */
916 static int core_alua_update_tpg_primary_metadata(
917 	struct t10_alua_tg_pt_gp *tg_pt_gp)
918 {
919 	unsigned char *md_buf;
920 	struct t10_wwn *wwn = &tg_pt_gp->tg_pt_gp_dev->t10_wwn;
921 	char path[ALUA_METADATA_PATH_LEN];
922 	int len, rc;
923 
924 	md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL);
925 	if (!md_buf) {
926 		pr_err("Unable to allocate buf for ALUA metadata\n");
927 		return -ENOMEM;
928 	}
929 
930 	memset(path, 0, ALUA_METADATA_PATH_LEN);
931 
932 	len = snprintf(md_buf, ALUA_MD_BUF_LEN,
933 			"tg_pt_gp_id=%hu\n"
934 			"alua_access_state=0x%02x\n"
935 			"alua_access_status=0x%02x\n",
936 			tg_pt_gp->tg_pt_gp_id,
937 			tg_pt_gp->tg_pt_gp_alua_pending_state,
938 			tg_pt_gp->tg_pt_gp_alua_access_status);
939 
940 	snprintf(path, ALUA_METADATA_PATH_LEN,
941 		"%s/alua/tpgs_%s/%s", db_root, &wwn->unit_serial[0],
942 		config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
943 
944 	rc = core_alua_write_tpg_metadata(path, md_buf, len);
945 	kfree(md_buf);
946 	return rc;
947 }
948 
949 static void core_alua_queue_state_change_ua(struct t10_alua_tg_pt_gp *tg_pt_gp)
950 {
951 	struct se_dev_entry *se_deve;
952 	struct se_lun *lun;
953 	struct se_lun_acl *lacl;
954 
955 	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
956 	list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
957 				lun_tg_pt_gp_link) {
958 		/*
959 		 * After an implicit target port asymmetric access state
960 		 * change, a device server shall establish a unit attention
961 		 * condition for the initiator port associated with every I_T
962 		 * nexus with the additional sense code set to ASYMMETRIC
963 		 * ACCESS STATE CHANGED.
964 		 *
965 		 * After an explicit target port asymmetric access state
966 		 * change, a device server shall establish a unit attention
967 		 * condition with the additional sense code set to ASYMMETRIC
968 		 * ACCESS STATE CHANGED for the initiator port associated with
969 		 * every I_T nexus other than the I_T nexus on which the SET
970 		 * TARGET PORT GROUPS command
971 		 */
972 		if (!percpu_ref_tryget_live(&lun->lun_ref))
973 			continue;
974 		spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
975 
976 		spin_lock(&lun->lun_deve_lock);
977 		list_for_each_entry(se_deve, &lun->lun_deve_list, lun_link) {
978 			lacl = rcu_dereference_check(se_deve->se_lun_acl,
979 					lockdep_is_held(&lun->lun_deve_lock));
980 
981 			/*
982 			 * spc4r37 p.242:
983 			 * After an explicit target port asymmetric access
984 			 * state change, a device server shall establish a
985 			 * unit attention condition with the additional sense
986 			 * code set to ASYMMETRIC ACCESS STATE CHANGED for
987 			 * the initiator port associated with every I_T nexus
988 			 * other than the I_T nexus on which the SET TARGET
989 			 * PORT GROUPS command was received.
990 			 */
991 			if ((tg_pt_gp->tg_pt_gp_alua_access_status ==
992 			     ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
993 			   (tg_pt_gp->tg_pt_gp_alua_lun != NULL) &&
994 			    (tg_pt_gp->tg_pt_gp_alua_lun == lun))
995 				continue;
996 
997 			/*
998 			 * se_deve->se_lun_acl pointer may be NULL for a
999 			 * entry created without explicit Node+MappedLUN ACLs
1000 			 */
1001 			if (lacl && (tg_pt_gp->tg_pt_gp_alua_nacl != NULL) &&
1002 			    (tg_pt_gp->tg_pt_gp_alua_nacl == lacl->se_lun_nacl))
1003 				continue;
1004 
1005 			core_scsi3_ua_allocate(se_deve, 0x2A,
1006 				ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
1007 		}
1008 		spin_unlock(&lun->lun_deve_lock);
1009 
1010 		spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1011 		percpu_ref_put(&lun->lun_ref);
1012 	}
1013 	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1014 }
1015 
1016 static void core_alua_do_transition_tg_pt_work(struct work_struct *work)
1017 {
1018 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(work,
1019 		struct t10_alua_tg_pt_gp, tg_pt_gp_transition_work);
1020 	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1021 	bool explicit = (tg_pt_gp->tg_pt_gp_alua_access_status ==
1022 			 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG);
1023 
1024 	/*
1025 	 * Update the ALUA metadata buf that has been allocated in
1026 	 * core_alua_do_port_transition(), this metadata will be written
1027 	 * to struct file.
1028 	 *
1029 	 * Note that there is the case where we do not want to update the
1030 	 * metadata when the saved metadata is being parsed in userspace
1031 	 * when setting the existing port access state and access status.
1032 	 *
1033 	 * Also note that the failure to write out the ALUA metadata to
1034 	 * struct file does NOT affect the actual ALUA transition.
1035 	 */
1036 	if (tg_pt_gp->tg_pt_gp_write_metadata) {
1037 		mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
1038 		core_alua_update_tpg_primary_metadata(tg_pt_gp);
1039 		mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
1040 	}
1041 	/*
1042 	 * Set the current primary ALUA access state to the requested new state
1043 	 */
1044 	atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1045 		   tg_pt_gp->tg_pt_gp_alua_pending_state);
1046 
1047 	pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1048 		" from primary access state %s to %s\n", (explicit) ? "explicit" :
1049 		"implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1050 		tg_pt_gp->tg_pt_gp_id,
1051 		core_alua_dump_state(tg_pt_gp->tg_pt_gp_alua_previous_state),
1052 		core_alua_dump_state(tg_pt_gp->tg_pt_gp_alua_pending_state));
1053 
1054 	core_alua_queue_state_change_ua(tg_pt_gp);
1055 
1056 	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1057 	atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
1058 	spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1059 
1060 	if (tg_pt_gp->tg_pt_gp_transition_complete)
1061 		complete(tg_pt_gp->tg_pt_gp_transition_complete);
1062 }
1063 
1064 static int core_alua_do_transition_tg_pt(
1065 	struct t10_alua_tg_pt_gp *tg_pt_gp,
1066 	int new_state,
1067 	int explicit)
1068 {
1069 	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1070 	DECLARE_COMPLETION_ONSTACK(wait);
1071 
1072 	/* Nothing to be done here */
1073 	if (atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state) == new_state)
1074 		return 0;
1075 
1076 	if (explicit && new_state == ALUA_ACCESS_STATE_TRANSITION)
1077 		return -EAGAIN;
1078 
1079 	/*
1080 	 * Flush any pending transitions
1081 	 */
1082 	if (!explicit && atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state) ==
1083 	    ALUA_ACCESS_STATE_TRANSITION) {
1084 		/* Just in case */
1085 		tg_pt_gp->tg_pt_gp_alua_pending_state = new_state;
1086 		tg_pt_gp->tg_pt_gp_transition_complete = &wait;
1087 		flush_work(&tg_pt_gp->tg_pt_gp_transition_work);
1088 		wait_for_completion(&wait);
1089 		tg_pt_gp->tg_pt_gp_transition_complete = NULL;
1090 		return 0;
1091 	}
1092 
1093 	/*
1094 	 * Save the old primary ALUA access state, and set the current state
1095 	 * to ALUA_ACCESS_STATE_TRANSITION.
1096 	 */
1097 	atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1098 			ALUA_ACCESS_STATE_TRANSITION);
1099 	tg_pt_gp->tg_pt_gp_alua_access_status = (explicit) ?
1100 				ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
1101 				ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;
1102 
1103 	core_alua_queue_state_change_ua(tg_pt_gp);
1104 
1105 	if (new_state == ALUA_ACCESS_STATE_TRANSITION)
1106 		return 0;
1107 
1108 	tg_pt_gp->tg_pt_gp_alua_previous_state =
1109 		atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
1110 	tg_pt_gp->tg_pt_gp_alua_pending_state = new_state;
1111 
1112 	/*
1113 	 * Check for the optional ALUA primary state transition delay
1114 	 */
1115 	if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
1116 		msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
1117 
1118 	/*
1119 	 * Take a reference for workqueue item
1120 	 */
1121 	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1122 	atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
1123 	spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1124 
1125 	schedule_work(&tg_pt_gp->tg_pt_gp_transition_work);
1126 	if (explicit) {
1127 		tg_pt_gp->tg_pt_gp_transition_complete = &wait;
1128 		wait_for_completion(&wait);
1129 		tg_pt_gp->tg_pt_gp_transition_complete = NULL;
1130 	}
1131 
1132 	return 0;
1133 }
1134 
1135 int core_alua_do_port_transition(
1136 	struct t10_alua_tg_pt_gp *l_tg_pt_gp,
1137 	struct se_device *l_dev,
1138 	struct se_lun *l_lun,
1139 	struct se_node_acl *l_nacl,
1140 	int new_state,
1141 	int explicit)
1142 {
1143 	struct se_device *dev;
1144 	struct t10_alua_lu_gp *lu_gp;
1145 	struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
1146 	struct t10_alua_tg_pt_gp *tg_pt_gp;
1147 	int primary, valid_states, rc = 0;
1148 
1149 	if (l_dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA)
1150 		return -ENODEV;
1151 
1152 	valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states;
1153 	if (core_alua_check_transition(new_state, valid_states, &primary,
1154 				       explicit) != 0)
1155 		return -EINVAL;
1156 
1157 	local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
1158 	spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
1159 	lu_gp = local_lu_gp_mem->lu_gp;
1160 	atomic_inc(&lu_gp->lu_gp_ref_cnt);
1161 	spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
1162 	/*
1163 	 * For storage objects that are members of the 'default_lu_gp',
1164 	 * we only do transition on the passed *l_tp_pt_gp, and not
1165 	 * on all of the matching target port groups IDs in default_lu_gp.
1166 	 */
1167 	if (!lu_gp->lu_gp_id) {
1168 		/*
1169 		 * core_alua_do_transition_tg_pt() will always return
1170 		 * success.
1171 		 */
1172 		l_tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
1173 		l_tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
1174 		rc = core_alua_do_transition_tg_pt(l_tg_pt_gp,
1175 						   new_state, explicit);
1176 		atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
1177 		return rc;
1178 	}
1179 	/*
1180 	 * For all other LU groups aside from 'default_lu_gp', walk all of
1181 	 * the associated storage objects looking for a matching target port
1182 	 * group ID from the local target port group.
1183 	 */
1184 	spin_lock(&lu_gp->lu_gp_lock);
1185 	list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
1186 				lu_gp_mem_list) {
1187 
1188 		dev = lu_gp_mem->lu_gp_mem_dev;
1189 		atomic_inc_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
1190 		spin_unlock(&lu_gp->lu_gp_lock);
1191 
1192 		spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1193 		list_for_each_entry(tg_pt_gp,
1194 				&dev->t10_alua.tg_pt_gps_list,
1195 				tg_pt_gp_list) {
1196 
1197 			if (!tg_pt_gp->tg_pt_gp_valid_id)
1198 				continue;
1199 			/*
1200 			 * If the target behavior port asymmetric access state
1201 			 * is changed for any target port group accessible via
1202 			 * a logical unit within a LU group, the target port
1203 			 * behavior group asymmetric access states for the same
1204 			 * target port group accessible via other logical units
1205 			 * in that LU group will also change.
1206 			 */
1207 			if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
1208 				continue;
1209 
1210 			if (l_tg_pt_gp == tg_pt_gp) {
1211 				tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
1212 				tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
1213 			} else {
1214 				tg_pt_gp->tg_pt_gp_alua_lun = NULL;
1215 				tg_pt_gp->tg_pt_gp_alua_nacl = NULL;
1216 			}
1217 			atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
1218 			spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1219 			/*
1220 			 * core_alua_do_transition_tg_pt() will always return
1221 			 * success.
1222 			 */
1223 			rc = core_alua_do_transition_tg_pt(tg_pt_gp,
1224 					new_state, explicit);
1225 
1226 			spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1227 			atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
1228 			if (rc)
1229 				break;
1230 		}
1231 		spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1232 
1233 		spin_lock(&lu_gp->lu_gp_lock);
1234 		atomic_dec_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
1235 	}
1236 	spin_unlock(&lu_gp->lu_gp_lock);
1237 
1238 	if (!rc) {
1239 		pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
1240 			 " Group IDs: %hu %s transition to primary state: %s\n",
1241 			 config_item_name(&lu_gp->lu_gp_group.cg_item),
1242 			 l_tg_pt_gp->tg_pt_gp_id,
1243 			 (explicit) ? "explicit" : "implicit",
1244 			 core_alua_dump_state(new_state));
1245 	}
1246 
1247 	atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
1248 	return rc;
1249 }
1250 
1251 static int core_alua_update_tpg_secondary_metadata(struct se_lun *lun)
1252 {
1253 	struct se_portal_group *se_tpg = lun->lun_tpg;
1254 	unsigned char *md_buf;
1255 	char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
1256 	int len, rc;
1257 
1258 	mutex_lock(&lun->lun_tg_pt_md_mutex);
1259 
1260 	md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL);
1261 	if (!md_buf) {
1262 		pr_err("Unable to allocate buf for ALUA metadata\n");
1263 		rc = -ENOMEM;
1264 		goto out_unlock;
1265 	}
1266 
1267 	memset(path, 0, ALUA_METADATA_PATH_LEN);
1268 	memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
1269 
1270 	len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
1271 			se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
1272 
1273 	if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
1274 		snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
1275 				se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
1276 
1277 	len = snprintf(md_buf, ALUA_MD_BUF_LEN, "alua_tg_pt_offline=%d\n"
1278 			"alua_tg_pt_status=0x%02x\n",
1279 			atomic_read(&lun->lun_tg_pt_secondary_offline),
1280 			lun->lun_tg_pt_secondary_stat);
1281 
1282 	snprintf(path, ALUA_METADATA_PATH_LEN, "%s/alua/%s/%s/lun_%llu",
1283 			db_root, se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
1284 			lun->unpacked_lun);
1285 
1286 	rc = core_alua_write_tpg_metadata(path, md_buf, len);
1287 	kfree(md_buf);
1288 
1289 out_unlock:
1290 	mutex_unlock(&lun->lun_tg_pt_md_mutex);
1291 	return rc;
1292 }
1293 
1294 static int core_alua_set_tg_pt_secondary_state(
1295 	struct se_lun *lun,
1296 	int explicit,
1297 	int offline)
1298 {
1299 	struct t10_alua_tg_pt_gp *tg_pt_gp;
1300 	int trans_delay_msecs;
1301 
1302 	spin_lock(&lun->lun_tg_pt_gp_lock);
1303 	tg_pt_gp = lun->lun_tg_pt_gp;
1304 	if (!tg_pt_gp) {
1305 		spin_unlock(&lun->lun_tg_pt_gp_lock);
1306 		pr_err("Unable to complete secondary state"
1307 				" transition\n");
1308 		return -EINVAL;
1309 	}
1310 	trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
1311 	/*
1312 	 * Set the secondary ALUA target port access state to OFFLINE
1313 	 * or release the previously secondary state for struct se_lun
1314 	 */
1315 	if (offline)
1316 		atomic_set(&lun->lun_tg_pt_secondary_offline, 1);
1317 	else
1318 		atomic_set(&lun->lun_tg_pt_secondary_offline, 0);
1319 
1320 	lun->lun_tg_pt_secondary_stat = (explicit) ?
1321 			ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
1322 			ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;
1323 
1324 	pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1325 		" to secondary access state: %s\n", (explicit) ? "explicit" :
1326 		"implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1327 		tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
1328 
1329 	spin_unlock(&lun->lun_tg_pt_gp_lock);
1330 	/*
1331 	 * Do the optional transition delay after we set the secondary
1332 	 * ALUA access state.
1333 	 */
1334 	if (trans_delay_msecs != 0)
1335 		msleep_interruptible(trans_delay_msecs);
1336 	/*
1337 	 * See if we need to update the ALUA fabric port metadata for
1338 	 * secondary state and status
1339 	 */
1340 	if (lun->lun_tg_pt_secondary_write_md)
1341 		core_alua_update_tpg_secondary_metadata(lun);
1342 
1343 	return 0;
1344 }
1345 
1346 struct t10_alua_lba_map *
1347 core_alua_allocate_lba_map(struct list_head *list,
1348 			   u64 first_lba, u64 last_lba)
1349 {
1350 	struct t10_alua_lba_map *lba_map;
1351 
1352 	lba_map = kmem_cache_zalloc(t10_alua_lba_map_cache, GFP_KERNEL);
1353 	if (!lba_map) {
1354 		pr_err("Unable to allocate struct t10_alua_lba_map\n");
1355 		return ERR_PTR(-ENOMEM);
1356 	}
1357 	INIT_LIST_HEAD(&lba_map->lba_map_mem_list);
1358 	lba_map->lba_map_first_lba = first_lba;
1359 	lba_map->lba_map_last_lba = last_lba;
1360 
1361 	list_add_tail(&lba_map->lba_map_list, list);
1362 	return lba_map;
1363 }
1364 
1365 int
1366 core_alua_allocate_lba_map_mem(struct t10_alua_lba_map *lba_map,
1367 			       int pg_id, int state)
1368 {
1369 	struct t10_alua_lba_map_member *lba_map_mem;
1370 
1371 	list_for_each_entry(lba_map_mem, &lba_map->lba_map_mem_list,
1372 			    lba_map_mem_list) {
1373 		if (lba_map_mem->lba_map_mem_alua_pg_id == pg_id) {
1374 			pr_err("Duplicate pg_id %d in lba_map\n", pg_id);
1375 			return -EINVAL;
1376 		}
1377 	}
1378 
1379 	lba_map_mem = kmem_cache_zalloc(t10_alua_lba_map_mem_cache, GFP_KERNEL);
1380 	if (!lba_map_mem) {
1381 		pr_err("Unable to allocate struct t10_alua_lba_map_mem\n");
1382 		return -ENOMEM;
1383 	}
1384 	lba_map_mem->lba_map_mem_alua_state = state;
1385 	lba_map_mem->lba_map_mem_alua_pg_id = pg_id;
1386 
1387 	list_add_tail(&lba_map_mem->lba_map_mem_list,
1388 		      &lba_map->lba_map_mem_list);
1389 	return 0;
1390 }
1391 
1392 void
1393 core_alua_free_lba_map(struct list_head *lba_list)
1394 {
1395 	struct t10_alua_lba_map *lba_map, *lba_map_tmp;
1396 	struct t10_alua_lba_map_member *lba_map_mem, *lba_map_mem_tmp;
1397 
1398 	list_for_each_entry_safe(lba_map, lba_map_tmp, lba_list,
1399 				 lba_map_list) {
1400 		list_for_each_entry_safe(lba_map_mem, lba_map_mem_tmp,
1401 					 &lba_map->lba_map_mem_list,
1402 					 lba_map_mem_list) {
1403 			list_del(&lba_map_mem->lba_map_mem_list);
1404 			kmem_cache_free(t10_alua_lba_map_mem_cache,
1405 					lba_map_mem);
1406 		}
1407 		list_del(&lba_map->lba_map_list);
1408 		kmem_cache_free(t10_alua_lba_map_cache, lba_map);
1409 	}
1410 }
1411 
1412 void
1413 core_alua_set_lba_map(struct se_device *dev, struct list_head *lba_map_list,
1414 		      int segment_size, int segment_mult)
1415 {
1416 	struct list_head old_lba_map_list;
1417 	struct t10_alua_tg_pt_gp *tg_pt_gp;
1418 	int activate = 0, supported;
1419 
1420 	INIT_LIST_HEAD(&old_lba_map_list);
1421 	spin_lock(&dev->t10_alua.lba_map_lock);
1422 	dev->t10_alua.lba_map_segment_size = segment_size;
1423 	dev->t10_alua.lba_map_segment_multiplier = segment_mult;
1424 	list_splice_init(&dev->t10_alua.lba_map_list, &old_lba_map_list);
1425 	if (lba_map_list) {
1426 		list_splice_init(lba_map_list, &dev->t10_alua.lba_map_list);
1427 		activate = 1;
1428 	}
1429 	spin_unlock(&dev->t10_alua.lba_map_lock);
1430 	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1431 	list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
1432 			    tg_pt_gp_list) {
1433 
1434 		if (!tg_pt_gp->tg_pt_gp_valid_id)
1435 			continue;
1436 		supported = tg_pt_gp->tg_pt_gp_alua_supported_states;
1437 		if (activate)
1438 			supported |= ALUA_LBD_SUP;
1439 		else
1440 			supported &= ~ALUA_LBD_SUP;
1441 		tg_pt_gp->tg_pt_gp_alua_supported_states = supported;
1442 	}
1443 	spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1444 	core_alua_free_lba_map(&old_lba_map_list);
1445 }
1446 
1447 struct t10_alua_lu_gp *
1448 core_alua_allocate_lu_gp(const char *name, int def_group)
1449 {
1450 	struct t10_alua_lu_gp *lu_gp;
1451 
1452 	lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
1453 	if (!lu_gp) {
1454 		pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1455 		return ERR_PTR(-ENOMEM);
1456 	}
1457 	INIT_LIST_HEAD(&lu_gp->lu_gp_node);
1458 	INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
1459 	spin_lock_init(&lu_gp->lu_gp_lock);
1460 	atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
1461 
1462 	if (def_group) {
1463 		lu_gp->lu_gp_id = alua_lu_gps_counter++;
1464 		lu_gp->lu_gp_valid_id = 1;
1465 		alua_lu_gps_count++;
1466 	}
1467 
1468 	return lu_gp;
1469 }
1470 
1471 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
1472 {
1473 	struct t10_alua_lu_gp *lu_gp_tmp;
1474 	u16 lu_gp_id_tmp;
1475 	/*
1476 	 * The lu_gp->lu_gp_id may only be set once..
1477 	 */
1478 	if (lu_gp->lu_gp_valid_id) {
1479 		pr_warn("ALUA LU Group already has a valid ID,"
1480 			" ignoring request\n");
1481 		return -EINVAL;
1482 	}
1483 
1484 	spin_lock(&lu_gps_lock);
1485 	if (alua_lu_gps_count == 0x0000ffff) {
1486 		pr_err("Maximum ALUA alua_lu_gps_count:"
1487 				" 0x0000ffff reached\n");
1488 		spin_unlock(&lu_gps_lock);
1489 		kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1490 		return -ENOSPC;
1491 	}
1492 again:
1493 	lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
1494 				alua_lu_gps_counter++;
1495 
1496 	list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
1497 		if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
1498 			if (!lu_gp_id)
1499 				goto again;
1500 
1501 			pr_warn("ALUA Logical Unit Group ID: %hu"
1502 				" already exists, ignoring request\n",
1503 				lu_gp_id);
1504 			spin_unlock(&lu_gps_lock);
1505 			return -EINVAL;
1506 		}
1507 	}
1508 
1509 	lu_gp->lu_gp_id = lu_gp_id_tmp;
1510 	lu_gp->lu_gp_valid_id = 1;
1511 	list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
1512 	alua_lu_gps_count++;
1513 	spin_unlock(&lu_gps_lock);
1514 
1515 	return 0;
1516 }
1517 
1518 static struct t10_alua_lu_gp_member *
1519 core_alua_allocate_lu_gp_mem(struct se_device *dev)
1520 {
1521 	struct t10_alua_lu_gp_member *lu_gp_mem;
1522 
1523 	lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
1524 	if (!lu_gp_mem) {
1525 		pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1526 		return ERR_PTR(-ENOMEM);
1527 	}
1528 	INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
1529 	spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
1530 	atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
1531 
1532 	lu_gp_mem->lu_gp_mem_dev = dev;
1533 	dev->dev_alua_lu_gp_mem = lu_gp_mem;
1534 
1535 	return lu_gp_mem;
1536 }
1537 
1538 void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
1539 {
1540 	struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
1541 	/*
1542 	 * Once we have reached this point, config_item_put() has
1543 	 * already been called from target_core_alua_drop_lu_gp().
1544 	 *
1545 	 * Here, we remove the *lu_gp from the global list so that
1546 	 * no associations can be made while we are releasing
1547 	 * struct t10_alua_lu_gp.
1548 	 */
1549 	spin_lock(&lu_gps_lock);
1550 	list_del(&lu_gp->lu_gp_node);
1551 	alua_lu_gps_count--;
1552 	spin_unlock(&lu_gps_lock);
1553 	/*
1554 	 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1555 	 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1556 	 * released with core_alua_put_lu_gp_from_name()
1557 	 */
1558 	while (atomic_read(&lu_gp->lu_gp_ref_cnt))
1559 		cpu_relax();
1560 	/*
1561 	 * Release reference to struct t10_alua_lu_gp * from all associated
1562 	 * struct se_device.
1563 	 */
1564 	spin_lock(&lu_gp->lu_gp_lock);
1565 	list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
1566 				&lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1567 		if (lu_gp_mem->lu_gp_assoc) {
1568 			list_del(&lu_gp_mem->lu_gp_mem_list);
1569 			lu_gp->lu_gp_members--;
1570 			lu_gp_mem->lu_gp_assoc = 0;
1571 		}
1572 		spin_unlock(&lu_gp->lu_gp_lock);
1573 		/*
1574 		 *
1575 		 * lu_gp_mem is associated with a single
1576 		 * struct se_device->dev_alua_lu_gp_mem, and is released when
1577 		 * struct se_device is released via core_alua_free_lu_gp_mem().
1578 		 *
1579 		 * If the passed lu_gp does NOT match the default_lu_gp, assume
1580 		 * we want to re-associate a given lu_gp_mem with default_lu_gp.
1581 		 */
1582 		spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1583 		if (lu_gp != default_lu_gp)
1584 			__core_alua_attach_lu_gp_mem(lu_gp_mem,
1585 					default_lu_gp);
1586 		else
1587 			lu_gp_mem->lu_gp = NULL;
1588 		spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1589 
1590 		spin_lock(&lu_gp->lu_gp_lock);
1591 	}
1592 	spin_unlock(&lu_gp->lu_gp_lock);
1593 
1594 	kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1595 }
1596 
1597 void core_alua_free_lu_gp_mem(struct se_device *dev)
1598 {
1599 	struct t10_alua_lu_gp *lu_gp;
1600 	struct t10_alua_lu_gp_member *lu_gp_mem;
1601 
1602 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
1603 	if (!lu_gp_mem)
1604 		return;
1605 
1606 	while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
1607 		cpu_relax();
1608 
1609 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1610 	lu_gp = lu_gp_mem->lu_gp;
1611 	if (lu_gp) {
1612 		spin_lock(&lu_gp->lu_gp_lock);
1613 		if (lu_gp_mem->lu_gp_assoc) {
1614 			list_del(&lu_gp_mem->lu_gp_mem_list);
1615 			lu_gp->lu_gp_members--;
1616 			lu_gp_mem->lu_gp_assoc = 0;
1617 		}
1618 		spin_unlock(&lu_gp->lu_gp_lock);
1619 		lu_gp_mem->lu_gp = NULL;
1620 	}
1621 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1622 
1623 	kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
1624 }
1625 
1626 struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
1627 {
1628 	struct t10_alua_lu_gp *lu_gp;
1629 	struct config_item *ci;
1630 
1631 	spin_lock(&lu_gps_lock);
1632 	list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
1633 		if (!lu_gp->lu_gp_valid_id)
1634 			continue;
1635 		ci = &lu_gp->lu_gp_group.cg_item;
1636 		if (!strcmp(config_item_name(ci), name)) {
1637 			atomic_inc(&lu_gp->lu_gp_ref_cnt);
1638 			spin_unlock(&lu_gps_lock);
1639 			return lu_gp;
1640 		}
1641 	}
1642 	spin_unlock(&lu_gps_lock);
1643 
1644 	return NULL;
1645 }
1646 
1647 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
1648 {
1649 	spin_lock(&lu_gps_lock);
1650 	atomic_dec(&lu_gp->lu_gp_ref_cnt);
1651 	spin_unlock(&lu_gps_lock);
1652 }
1653 
1654 /*
1655  * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1656  */
1657 void __core_alua_attach_lu_gp_mem(
1658 	struct t10_alua_lu_gp_member *lu_gp_mem,
1659 	struct t10_alua_lu_gp *lu_gp)
1660 {
1661 	spin_lock(&lu_gp->lu_gp_lock);
1662 	lu_gp_mem->lu_gp = lu_gp;
1663 	lu_gp_mem->lu_gp_assoc = 1;
1664 	list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
1665 	lu_gp->lu_gp_members++;
1666 	spin_unlock(&lu_gp->lu_gp_lock);
1667 }
1668 
1669 /*
1670  * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1671  */
1672 void __core_alua_drop_lu_gp_mem(
1673 	struct t10_alua_lu_gp_member *lu_gp_mem,
1674 	struct t10_alua_lu_gp *lu_gp)
1675 {
1676 	spin_lock(&lu_gp->lu_gp_lock);
1677 	list_del(&lu_gp_mem->lu_gp_mem_list);
1678 	lu_gp_mem->lu_gp = NULL;
1679 	lu_gp_mem->lu_gp_assoc = 0;
1680 	lu_gp->lu_gp_members--;
1681 	spin_unlock(&lu_gp->lu_gp_lock);
1682 }
1683 
1684 struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(struct se_device *dev,
1685 		const char *name, int def_group)
1686 {
1687 	struct t10_alua_tg_pt_gp *tg_pt_gp;
1688 
1689 	tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
1690 	if (!tg_pt_gp) {
1691 		pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1692 		return NULL;
1693 	}
1694 	INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
1695 	INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_lun_list);
1696 	mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
1697 	spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
1698 	atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
1699 	INIT_WORK(&tg_pt_gp->tg_pt_gp_transition_work,
1700 		  core_alua_do_transition_tg_pt_work);
1701 	tg_pt_gp->tg_pt_gp_dev = dev;
1702 	atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1703 		ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED);
1704 	/*
1705 	 * Enable both explicit and implicit ALUA support by default
1706 	 */
1707 	tg_pt_gp->tg_pt_gp_alua_access_type =
1708 			TPGS_EXPLICIT_ALUA | TPGS_IMPLICIT_ALUA;
1709 	/*
1710 	 * Set the default Active/NonOptimized Delay in milliseconds
1711 	 */
1712 	tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
1713 	tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
1714 	tg_pt_gp->tg_pt_gp_implicit_trans_secs = ALUA_DEFAULT_IMPLICIT_TRANS_SECS;
1715 
1716 	/*
1717 	 * Enable all supported states
1718 	 */
1719 	tg_pt_gp->tg_pt_gp_alua_supported_states =
1720 	    ALUA_T_SUP | ALUA_O_SUP |
1721 	    ALUA_U_SUP | ALUA_S_SUP | ALUA_AN_SUP | ALUA_AO_SUP;
1722 
1723 	if (def_group) {
1724 		spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1725 		tg_pt_gp->tg_pt_gp_id =
1726 				dev->t10_alua.alua_tg_pt_gps_counter++;
1727 		tg_pt_gp->tg_pt_gp_valid_id = 1;
1728 		dev->t10_alua.alua_tg_pt_gps_count++;
1729 		list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1730 			      &dev->t10_alua.tg_pt_gps_list);
1731 		spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1732 	}
1733 
1734 	return tg_pt_gp;
1735 }
1736 
1737 int core_alua_set_tg_pt_gp_id(
1738 	struct t10_alua_tg_pt_gp *tg_pt_gp,
1739 	u16 tg_pt_gp_id)
1740 {
1741 	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1742 	struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
1743 	u16 tg_pt_gp_id_tmp;
1744 
1745 	/*
1746 	 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1747 	 */
1748 	if (tg_pt_gp->tg_pt_gp_valid_id) {
1749 		pr_warn("ALUA TG PT Group already has a valid ID,"
1750 			" ignoring request\n");
1751 		return -EINVAL;
1752 	}
1753 
1754 	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1755 	if (dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
1756 		pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1757 			" 0x0000ffff reached\n");
1758 		spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1759 		kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1760 		return -ENOSPC;
1761 	}
1762 again:
1763 	tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
1764 			dev->t10_alua.alua_tg_pt_gps_counter++;
1765 
1766 	list_for_each_entry(tg_pt_gp_tmp, &dev->t10_alua.tg_pt_gps_list,
1767 			tg_pt_gp_list) {
1768 		if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
1769 			if (!tg_pt_gp_id)
1770 				goto again;
1771 
1772 			pr_err("ALUA Target Port Group ID: %hu already"
1773 				" exists, ignoring request\n", tg_pt_gp_id);
1774 			spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1775 			return -EINVAL;
1776 		}
1777 	}
1778 
1779 	tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
1780 	tg_pt_gp->tg_pt_gp_valid_id = 1;
1781 	list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1782 			&dev->t10_alua.tg_pt_gps_list);
1783 	dev->t10_alua.alua_tg_pt_gps_count++;
1784 	spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1785 
1786 	return 0;
1787 }
1788 
1789 void core_alua_free_tg_pt_gp(
1790 	struct t10_alua_tg_pt_gp *tg_pt_gp)
1791 {
1792 	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1793 	struct se_lun *lun, *next;
1794 
1795 	/*
1796 	 * Once we have reached this point, config_item_put() has already
1797 	 * been called from target_core_alua_drop_tg_pt_gp().
1798 	 *
1799 	 * Here we remove *tg_pt_gp from the global list so that
1800 	 * no associations *OR* explicit ALUA via SET_TARGET_PORT_GROUPS
1801 	 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1802 	 */
1803 	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1804 	list_del(&tg_pt_gp->tg_pt_gp_list);
1805 	dev->t10_alua.alua_tg_pt_gps_counter--;
1806 	spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1807 
1808 	flush_work(&tg_pt_gp->tg_pt_gp_transition_work);
1809 
1810 	/*
1811 	 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1812 	 * core_alua_get_tg_pt_gp_by_name() in
1813 	 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1814 	 * to be released with core_alua_put_tg_pt_gp_from_name().
1815 	 */
1816 	while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
1817 		cpu_relax();
1818 
1819 	/*
1820 	 * Release reference to struct t10_alua_tg_pt_gp from all associated
1821 	 * struct se_port.
1822 	 */
1823 	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1824 	list_for_each_entry_safe(lun, next,
1825 			&tg_pt_gp->tg_pt_gp_lun_list, lun_tg_pt_gp_link) {
1826 		list_del_init(&lun->lun_tg_pt_gp_link);
1827 		tg_pt_gp->tg_pt_gp_members--;
1828 
1829 		spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1830 		/*
1831 		 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1832 		 * assume we want to re-associate a given tg_pt_gp_mem with
1833 		 * default_tg_pt_gp.
1834 		 */
1835 		spin_lock(&lun->lun_tg_pt_gp_lock);
1836 		if (tg_pt_gp != dev->t10_alua.default_tg_pt_gp) {
1837 			__target_attach_tg_pt_gp(lun,
1838 					dev->t10_alua.default_tg_pt_gp);
1839 		} else
1840 			lun->lun_tg_pt_gp = NULL;
1841 		spin_unlock(&lun->lun_tg_pt_gp_lock);
1842 
1843 		spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1844 	}
1845 	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1846 
1847 	kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1848 }
1849 
1850 static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
1851 		struct se_device *dev, const char *name)
1852 {
1853 	struct t10_alua_tg_pt_gp *tg_pt_gp;
1854 	struct config_item *ci;
1855 
1856 	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1857 	list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
1858 			tg_pt_gp_list) {
1859 		if (!tg_pt_gp->tg_pt_gp_valid_id)
1860 			continue;
1861 		ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1862 		if (!strcmp(config_item_name(ci), name)) {
1863 			atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
1864 			spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1865 			return tg_pt_gp;
1866 		}
1867 	}
1868 	spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1869 
1870 	return NULL;
1871 }
1872 
1873 static void core_alua_put_tg_pt_gp_from_name(
1874 	struct t10_alua_tg_pt_gp *tg_pt_gp)
1875 {
1876 	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1877 
1878 	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1879 	atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
1880 	spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1881 }
1882 
1883 static void __target_attach_tg_pt_gp(struct se_lun *lun,
1884 		struct t10_alua_tg_pt_gp *tg_pt_gp)
1885 {
1886 	struct se_dev_entry *se_deve;
1887 
1888 	assert_spin_locked(&lun->lun_tg_pt_gp_lock);
1889 
1890 	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1891 	lun->lun_tg_pt_gp = tg_pt_gp;
1892 	list_add_tail(&lun->lun_tg_pt_gp_link, &tg_pt_gp->tg_pt_gp_lun_list);
1893 	tg_pt_gp->tg_pt_gp_members++;
1894 	spin_lock(&lun->lun_deve_lock);
1895 	list_for_each_entry(se_deve, &lun->lun_deve_list, lun_link)
1896 		core_scsi3_ua_allocate(se_deve, 0x3f,
1897 				       ASCQ_3FH_INQUIRY_DATA_HAS_CHANGED);
1898 	spin_unlock(&lun->lun_deve_lock);
1899 	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1900 }
1901 
1902 void target_attach_tg_pt_gp(struct se_lun *lun,
1903 		struct t10_alua_tg_pt_gp *tg_pt_gp)
1904 {
1905 	spin_lock(&lun->lun_tg_pt_gp_lock);
1906 	__target_attach_tg_pt_gp(lun, tg_pt_gp);
1907 	spin_unlock(&lun->lun_tg_pt_gp_lock);
1908 }
1909 
1910 static void __target_detach_tg_pt_gp(struct se_lun *lun,
1911 		struct t10_alua_tg_pt_gp *tg_pt_gp)
1912 {
1913 	assert_spin_locked(&lun->lun_tg_pt_gp_lock);
1914 
1915 	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1916 	list_del_init(&lun->lun_tg_pt_gp_link);
1917 	tg_pt_gp->tg_pt_gp_members--;
1918 	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1919 
1920 	lun->lun_tg_pt_gp = NULL;
1921 }
1922 
1923 void target_detach_tg_pt_gp(struct se_lun *lun)
1924 {
1925 	struct t10_alua_tg_pt_gp *tg_pt_gp;
1926 
1927 	spin_lock(&lun->lun_tg_pt_gp_lock);
1928 	tg_pt_gp = lun->lun_tg_pt_gp;
1929 	if (tg_pt_gp)
1930 		__target_detach_tg_pt_gp(lun, tg_pt_gp);
1931 	spin_unlock(&lun->lun_tg_pt_gp_lock);
1932 }
1933 
1934 ssize_t core_alua_show_tg_pt_gp_info(struct se_lun *lun, char *page)
1935 {
1936 	struct config_item *tg_pt_ci;
1937 	struct t10_alua_tg_pt_gp *tg_pt_gp;
1938 	ssize_t len = 0;
1939 
1940 	spin_lock(&lun->lun_tg_pt_gp_lock);
1941 	tg_pt_gp = lun->lun_tg_pt_gp;
1942 	if (tg_pt_gp) {
1943 		tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1944 		len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
1945 			" %hu\nTG Port Primary Access State: %s\nTG Port "
1946 			"Primary Access Status: %s\nTG Port Secondary Access"
1947 			" State: %s\nTG Port Secondary Access Status: %s\n",
1948 			config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
1949 			core_alua_dump_state(atomic_read(
1950 					&tg_pt_gp->tg_pt_gp_alua_access_state)),
1951 			core_alua_dump_status(
1952 				tg_pt_gp->tg_pt_gp_alua_access_status),
1953 			atomic_read(&lun->lun_tg_pt_secondary_offline) ?
1954 			"Offline" : "None",
1955 			core_alua_dump_status(lun->lun_tg_pt_secondary_stat));
1956 	}
1957 	spin_unlock(&lun->lun_tg_pt_gp_lock);
1958 
1959 	return len;
1960 }
1961 
1962 ssize_t core_alua_store_tg_pt_gp_info(
1963 	struct se_lun *lun,
1964 	const char *page,
1965 	size_t count)
1966 {
1967 	struct se_portal_group *tpg = lun->lun_tpg;
1968 	/*
1969 	 * rcu_dereference_raw protected by se_lun->lun_group symlink
1970 	 * reference to se_device->dev_group.
1971 	 */
1972 	struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
1973 	struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
1974 	unsigned char buf[TG_PT_GROUP_NAME_BUF];
1975 	int move = 0;
1976 
1977 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA ||
1978 	    (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
1979 		return -ENODEV;
1980 
1981 	if (count > TG_PT_GROUP_NAME_BUF) {
1982 		pr_err("ALUA Target Port Group alias too large!\n");
1983 		return -EINVAL;
1984 	}
1985 	memset(buf, 0, TG_PT_GROUP_NAME_BUF);
1986 	memcpy(buf, page, count);
1987 	/*
1988 	 * Any ALUA target port group alias besides "NULL" means we will be
1989 	 * making a new group association.
1990 	 */
1991 	if (strcmp(strstrip(buf), "NULL")) {
1992 		/*
1993 		 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1994 		 * struct t10_alua_tg_pt_gp.  This reference is released with
1995 		 * core_alua_put_tg_pt_gp_from_name() below.
1996 		 */
1997 		tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(dev,
1998 					strstrip(buf));
1999 		if (!tg_pt_gp_new)
2000 			return -ENODEV;
2001 	}
2002 
2003 	spin_lock(&lun->lun_tg_pt_gp_lock);
2004 	tg_pt_gp = lun->lun_tg_pt_gp;
2005 	if (tg_pt_gp) {
2006 		/*
2007 		 * Clearing an existing tg_pt_gp association, and replacing
2008 		 * with the default_tg_pt_gp.
2009 		 */
2010 		if (!tg_pt_gp_new) {
2011 			pr_debug("Target_Core_ConfigFS: Moving"
2012 				" %s/tpgt_%hu/%s from ALUA Target Port Group:"
2013 				" alua/%s, ID: %hu back to"
2014 				" default_tg_pt_gp\n",
2015 				tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2016 				tpg->se_tpg_tfo->tpg_get_tag(tpg),
2017 				config_item_name(&lun->lun_group.cg_item),
2018 				config_item_name(
2019 					&tg_pt_gp->tg_pt_gp_group.cg_item),
2020 				tg_pt_gp->tg_pt_gp_id);
2021 
2022 			__target_detach_tg_pt_gp(lun, tg_pt_gp);
2023 			__target_attach_tg_pt_gp(lun,
2024 					dev->t10_alua.default_tg_pt_gp);
2025 			spin_unlock(&lun->lun_tg_pt_gp_lock);
2026 
2027 			return count;
2028 		}
2029 		__target_detach_tg_pt_gp(lun, tg_pt_gp);
2030 		move = 1;
2031 	}
2032 
2033 	__target_attach_tg_pt_gp(lun, tg_pt_gp_new);
2034 	spin_unlock(&lun->lun_tg_pt_gp_lock);
2035 	pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
2036 		" Target Port Group: alua/%s, ID: %hu\n", (move) ?
2037 		"Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2038 		tpg->se_tpg_tfo->tpg_get_tag(tpg),
2039 		config_item_name(&lun->lun_group.cg_item),
2040 		config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
2041 		tg_pt_gp_new->tg_pt_gp_id);
2042 
2043 	core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
2044 	return count;
2045 }
2046 
2047 ssize_t core_alua_show_access_type(
2048 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2049 	char *page)
2050 {
2051 	if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA) &&
2052 	    (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA))
2053 		return sprintf(page, "Implicit and Explicit\n");
2054 	else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)
2055 		return sprintf(page, "Implicit\n");
2056 	else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)
2057 		return sprintf(page, "Explicit\n");
2058 	else
2059 		return sprintf(page, "None\n");
2060 }
2061 
2062 ssize_t core_alua_store_access_type(
2063 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2064 	const char *page,
2065 	size_t count)
2066 {
2067 	unsigned long tmp;
2068 	int ret;
2069 
2070 	ret = kstrtoul(page, 0, &tmp);
2071 	if (ret < 0) {
2072 		pr_err("Unable to extract alua_access_type\n");
2073 		return ret;
2074 	}
2075 	if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
2076 		pr_err("Illegal value for alua_access_type:"
2077 				" %lu\n", tmp);
2078 		return -EINVAL;
2079 	}
2080 	if (tmp == 3)
2081 		tg_pt_gp->tg_pt_gp_alua_access_type =
2082 			TPGS_IMPLICIT_ALUA | TPGS_EXPLICIT_ALUA;
2083 	else if (tmp == 2)
2084 		tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICIT_ALUA;
2085 	else if (tmp == 1)
2086 		tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICIT_ALUA;
2087 	else
2088 		tg_pt_gp->tg_pt_gp_alua_access_type = 0;
2089 
2090 	return count;
2091 }
2092 
2093 ssize_t core_alua_show_nonop_delay_msecs(
2094 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2095 	char *page)
2096 {
2097 	return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
2098 }
2099 
2100 ssize_t core_alua_store_nonop_delay_msecs(
2101 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2102 	const char *page,
2103 	size_t count)
2104 {
2105 	unsigned long tmp;
2106 	int ret;
2107 
2108 	ret = kstrtoul(page, 0, &tmp);
2109 	if (ret < 0) {
2110 		pr_err("Unable to extract nonop_delay_msecs\n");
2111 		return ret;
2112 	}
2113 	if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
2114 		pr_err("Passed nonop_delay_msecs: %lu, exceeds"
2115 			" ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
2116 			ALUA_MAX_NONOP_DELAY_MSECS);
2117 		return -EINVAL;
2118 	}
2119 	tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
2120 
2121 	return count;
2122 }
2123 
2124 ssize_t core_alua_show_trans_delay_msecs(
2125 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2126 	char *page)
2127 {
2128 	return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
2129 }
2130 
2131 ssize_t core_alua_store_trans_delay_msecs(
2132 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2133 	const char *page,
2134 	size_t count)
2135 {
2136 	unsigned long tmp;
2137 	int ret;
2138 
2139 	ret = kstrtoul(page, 0, &tmp);
2140 	if (ret < 0) {
2141 		pr_err("Unable to extract trans_delay_msecs\n");
2142 		return ret;
2143 	}
2144 	if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
2145 		pr_err("Passed trans_delay_msecs: %lu, exceeds"
2146 			" ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
2147 			ALUA_MAX_TRANS_DELAY_MSECS);
2148 		return -EINVAL;
2149 	}
2150 	tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
2151 
2152 	return count;
2153 }
2154 
2155 ssize_t core_alua_show_implicit_trans_secs(
2156 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2157 	char *page)
2158 {
2159 	return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_implicit_trans_secs);
2160 }
2161 
2162 ssize_t core_alua_store_implicit_trans_secs(
2163 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2164 	const char *page,
2165 	size_t count)
2166 {
2167 	unsigned long tmp;
2168 	int ret;
2169 
2170 	ret = kstrtoul(page, 0, &tmp);
2171 	if (ret < 0) {
2172 		pr_err("Unable to extract implicit_trans_secs\n");
2173 		return ret;
2174 	}
2175 	if (tmp > ALUA_MAX_IMPLICIT_TRANS_SECS) {
2176 		pr_err("Passed implicit_trans_secs: %lu, exceeds"
2177 			" ALUA_MAX_IMPLICIT_TRANS_SECS: %d\n", tmp,
2178 			ALUA_MAX_IMPLICIT_TRANS_SECS);
2179 		return  -EINVAL;
2180 	}
2181 	tg_pt_gp->tg_pt_gp_implicit_trans_secs = (int)tmp;
2182 
2183 	return count;
2184 }
2185 
2186 ssize_t core_alua_show_preferred_bit(
2187 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2188 	char *page)
2189 {
2190 	return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
2191 }
2192 
2193 ssize_t core_alua_store_preferred_bit(
2194 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2195 	const char *page,
2196 	size_t count)
2197 {
2198 	unsigned long tmp;
2199 	int ret;
2200 
2201 	ret = kstrtoul(page, 0, &tmp);
2202 	if (ret < 0) {
2203 		pr_err("Unable to extract preferred ALUA value\n");
2204 		return ret;
2205 	}
2206 	if ((tmp != 0) && (tmp != 1)) {
2207 		pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
2208 		return -EINVAL;
2209 	}
2210 	tg_pt_gp->tg_pt_gp_pref = (int)tmp;
2211 
2212 	return count;
2213 }
2214 
2215 ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
2216 {
2217 	return sprintf(page, "%d\n",
2218 		atomic_read(&lun->lun_tg_pt_secondary_offline));
2219 }
2220 
2221 ssize_t core_alua_store_offline_bit(
2222 	struct se_lun *lun,
2223 	const char *page,
2224 	size_t count)
2225 {
2226 	/*
2227 	 * rcu_dereference_raw protected by se_lun->lun_group symlink
2228 	 * reference to se_device->dev_group.
2229 	 */
2230 	struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
2231 	unsigned long tmp;
2232 	int ret;
2233 
2234 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA ||
2235 	    (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
2236 		return -ENODEV;
2237 
2238 	ret = kstrtoul(page, 0, &tmp);
2239 	if (ret < 0) {
2240 		pr_err("Unable to extract alua_tg_pt_offline value\n");
2241 		return ret;
2242 	}
2243 	if ((tmp != 0) && (tmp != 1)) {
2244 		pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
2245 				tmp);
2246 		return -EINVAL;
2247 	}
2248 
2249 	ret = core_alua_set_tg_pt_secondary_state(lun, 0, (int)tmp);
2250 	if (ret < 0)
2251 		return -EINVAL;
2252 
2253 	return count;
2254 }
2255 
2256 ssize_t core_alua_show_secondary_status(
2257 	struct se_lun *lun,
2258 	char *page)
2259 {
2260 	return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_stat);
2261 }
2262 
2263 ssize_t core_alua_store_secondary_status(
2264 	struct se_lun *lun,
2265 	const char *page,
2266 	size_t count)
2267 {
2268 	unsigned long tmp;
2269 	int ret;
2270 
2271 	ret = kstrtoul(page, 0, &tmp);
2272 	if (ret < 0) {
2273 		pr_err("Unable to extract alua_tg_pt_status\n");
2274 		return ret;
2275 	}
2276 	if ((tmp != ALUA_STATUS_NONE) &&
2277 	    (tmp != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2278 	    (tmp != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
2279 		pr_err("Illegal value for alua_tg_pt_status: %lu\n",
2280 				tmp);
2281 		return -EINVAL;
2282 	}
2283 	lun->lun_tg_pt_secondary_stat = (int)tmp;
2284 
2285 	return count;
2286 }
2287 
2288 ssize_t core_alua_show_secondary_write_metadata(
2289 	struct se_lun *lun,
2290 	char *page)
2291 {
2292 	return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_write_md);
2293 }
2294 
2295 ssize_t core_alua_store_secondary_write_metadata(
2296 	struct se_lun *lun,
2297 	const char *page,
2298 	size_t count)
2299 {
2300 	unsigned long tmp;
2301 	int ret;
2302 
2303 	ret = kstrtoul(page, 0, &tmp);
2304 	if (ret < 0) {
2305 		pr_err("Unable to extract alua_tg_pt_write_md\n");
2306 		return ret;
2307 	}
2308 	if ((tmp != 0) && (tmp != 1)) {
2309 		pr_err("Illegal value for alua_tg_pt_write_md:"
2310 				" %lu\n", tmp);
2311 		return -EINVAL;
2312 	}
2313 	lun->lun_tg_pt_secondary_write_md = (int)tmp;
2314 
2315 	return count;
2316 }
2317 
2318 int core_setup_alua(struct se_device *dev)
2319 {
2320 	if (!(dev->transport->transport_flags &
2321 	     TRANSPORT_FLAG_PASSTHROUGH_ALUA) &&
2322 	    !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
2323 		struct t10_alua_lu_gp_member *lu_gp_mem;
2324 
2325 		/*
2326 		 * Associate this struct se_device with the default ALUA
2327 		 * LUN Group.
2328 		 */
2329 		lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
2330 		if (IS_ERR(lu_gp_mem))
2331 			return PTR_ERR(lu_gp_mem);
2332 
2333 		spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2334 		__core_alua_attach_lu_gp_mem(lu_gp_mem,
2335 				default_lu_gp);
2336 		spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2337 
2338 		pr_debug("%s: Adding to default ALUA LU Group:"
2339 			" core/alua/lu_gps/default_lu_gp\n",
2340 			dev->transport->name);
2341 	}
2342 
2343 	return 0;
2344 }
2345