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