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