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