1 /*******************************************************************************
2 * Filename: target_core_alua.c
4 * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
6 * Copyright (c) 2009-2010 Rising Tide Systems
7 * Copyright (c) 2009-2010 Linux-iSCSI.org
9 * Nicholas A. Bellinger <nab@kernel.org>
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.
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.
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.
25 ******************************************************************************/
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>
34 #include <target/target_core_base.h>
35 #include <target/target_core_device.h>
36 #include <target/target_core_transport.h>
37 #include <target/target_core_fabric_ops.h>
38 #include <target/target_core_configfs.h>
40 #include "target_core_alua.h"
41 #include "target_core_hba.h"
42 #include "target_core_ua.h"
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
);
49 static u16 alua_lu_gps_counter
;
50 static u32 alua_lu_gps_count
;
52 static DEFINE_SPINLOCK(lu_gps_lock
);
53 static LIST_HEAD(lu_gps_list
);
55 struct t10_alua_lu_gp
*default_lu_gp
;
58 * REPORT_TARGET_PORT_GROUPS
60 * See spc4r17 section 6.27
62 int target_emulate_report_target_port_groups(struct se_task
*task
)
64 struct se_cmd
*cmd
= task
->task_se_cmd
;
65 struct se_subsystem_dev
*su_dev
= cmd
->se_dev
->se_sub_dev
;
67 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
68 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
70 u32 rd_len
= 0, off
= 4; /* Skip over RESERVED area to first
71 Target port group descriptor */
73 * Need at least 4 bytes of response data or else we can't
74 * even fit the return data length.
76 if (cmd
->data_length
< 4) {
77 pr_warn("REPORT TARGET PORT GROUPS allocation length %u"
78 " too small\n", cmd
->data_length
);
82 buf
= transport_kmap_first_data_page(cmd
);
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
,
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.
93 if ((off
+ 8 + (tg_pt_gp
->tg_pt_gp_members
* 4)) >
95 rd_len
+= 8 + (tg_pt_gp
->tg_pt_gp_members
* 4);
99 * PREF: Preferred target port bit, determine if this
100 * bit should be set for port group.
102 if (tg_pt_gp
->tg_pt_gp_pref
)
105 * Set the ASYMMETRIC ACCESS State
107 buf
[off
++] |= (atomic_read(
108 &tg_pt_gp
->tg_pt_gp_alua_access_state
) & 0xff);
110 * Set supported ASYMMETRIC ACCESS State bits
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 */
121 buf
[off
++] = ((tg_pt_gp
->tg_pt_gp_id
>> 8) & 0xff);
122 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_id
& 0xff);
124 off
++; /* Skip over Reserved */
128 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_alua_access_status
& 0xff);
130 * Vendor Specific field
136 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_members
& 0xff);
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
,
142 port
= tg_pt_gp_mem
->tg_pt
;
144 * Start Target Port descriptor format
146 * See spc4r17 section 6.2.7 Table 247
148 off
+= 2; /* Skip over Obsolete */
150 * Set RELATIVE TARGET PORT IDENTIFIER
152 buf
[off
++] = ((port
->sep_rtpi
>> 8) & 0xff);
153 buf
[off
++] = (port
->sep_rtpi
& 0xff);
156 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
158 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
160 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
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);
167 transport_kunmap_first_data_page(cmd
);
169 task
->task_scsi_status
= GOOD
;
170 transport_complete_task(task
, 1);
175 * SET_TARGET_PORT_GROUPS for explict ALUA operation.
177 * See spc4r17 section 6.35
179 int target_emulate_set_target_port_groups(struct se_task
*task
)
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
;
190 u32 len
= 4; /* Skip over RESERVED area in header */
191 int alua_access_state
, primary
= 0, rc
;
195 return PYX_TRANSPORT_LU_COMM_FAILURE
;
197 buf
= transport_kmap_first_data_page(cmd
);
200 * Determine if explict ALUA via SET_TARGET_PORT_GROUPS is allowed
201 * for the local tg_pt_gp.
203 l_tg_pt_gp_mem
= l_port
->sep_alua_tg_pt_gp_mem
;
204 if (!l_tg_pt_gp_mem
) {
205 pr_err("Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
206 rc
= PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
;
209 spin_lock(&l_tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
210 l_tg_pt_gp
= l_tg_pt_gp_mem
->tg_pt_gp
;
212 spin_unlock(&l_tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
213 pr_err("Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n");
214 rc
= PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
;
217 rc
= (l_tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICT_ALUA
);
218 spin_unlock(&l_tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
221 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
222 " while TPGS_EXPLICT_ALUA is disabled\n");
223 rc
= PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
;
227 ptr
= &buf
[4]; /* Skip over RESERVED area in header */
229 while (len
< cmd
->data_length
) {
230 alua_access_state
= (ptr
[0] & 0x0f);
232 * Check the received ALUA access state, and determine if
233 * the state is a primary or secondary target port asymmetric
236 rc
= core_alua_check_transition(alua_access_state
, &primary
);
239 * If the SET TARGET PORT GROUPS attempts to establish
240 * an invalid combination of target port asymmetric
241 * access states or attempts to establish an
242 * unsupported target port asymmetric access state,
243 * then the command shall be terminated with CHECK
244 * CONDITION status, with the sense key set to ILLEGAL
245 * REQUEST, and the additional sense code set to INVALID
246 * FIELD IN PARAMETER LIST.
248 rc
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
253 * If the ASYMMETRIC ACCESS STATE field (see table 267)
254 * specifies a primary target port asymmetric access state,
255 * then the TARGET PORT GROUP OR TARGET PORT field specifies
256 * a primary target port group for which the primary target
257 * port asymmetric access state shall be changed. If the
258 * ASYMMETRIC ACCESS STATE field specifies a secondary target
259 * port asymmetric access state, then the TARGET PORT GROUP OR
260 * TARGET PORT field specifies the relative target port
261 * identifier (see 3.1.120) of the target port for which the
262 * secondary target port asymmetric access state shall be
266 tg_pt_id
= ((ptr
[2] << 8) & 0xff);
267 tg_pt_id
|= (ptr
[3] & 0xff);
269 * Locate the matching target port group ID from
270 * the global tg_pt_gp list
272 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
273 list_for_each_entry(tg_pt_gp
,
274 &su_dev
->t10_alua
.tg_pt_gps_list
,
276 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
279 if (tg_pt_id
!= tg_pt_gp
->tg_pt_gp_id
)
282 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
283 smp_mb__after_atomic_inc();
284 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
286 rc
= core_alua_do_port_transition(tg_pt_gp
,
288 alua_access_state
, 1);
290 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
291 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
292 smp_mb__after_atomic_dec();
295 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
297 * If not matching target port group ID can be located
298 * throw an exception with ASCQ: INVALID_PARAMETER_LIST
301 rc
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
306 * Extact the RELATIVE TARGET PORT IDENTIFIER to identify
307 * the Target Port in question for the the incoming
308 * SET_TARGET_PORT_GROUPS op.
310 rtpi
= ((ptr
[2] << 8) & 0xff);
311 rtpi
|= (ptr
[3] & 0xff);
313 * Locate the matching relative target port identifer
314 * for the struct se_device storage object.
316 spin_lock(&dev
->se_port_lock
);
317 list_for_each_entry(port
, &dev
->dev_sep_list
,
319 if (port
->sep_rtpi
!= rtpi
)
322 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
323 spin_unlock(&dev
->se_port_lock
);
325 rc
= core_alua_set_tg_pt_secondary_state(
326 tg_pt_gp_mem
, port
, 1, 1);
328 spin_lock(&dev
->se_port_lock
);
331 spin_unlock(&dev
->se_port_lock
);
333 * If not matching relative target port identifier can
334 * be located, throw an exception with ASCQ:
335 * INVALID_PARAMETER_LIST
338 rc
= PYX_TRANSPORT_INVALID_PARAMETER_LIST
;
348 transport_kunmap_first_data_page(cmd
);
349 task
->task_scsi_status
= GOOD
;
350 transport_complete_task(task
, 1);
354 static inline int core_alua_state_nonoptimized(
357 int nonop_delay_msecs
,
361 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
362 * later to determine if processing of this cmd needs to be
363 * temporarily delayed for the Active/NonOptimized primary access state.
365 cmd
->se_cmd_flags
|= SCF_ALUA_NON_OPTIMIZED
;
366 cmd
->alua_nonop_delay
= nonop_delay_msecs
;
370 static inline int core_alua_state_standby(
376 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
377 * spc4r17 section 5.9.2.4.4
386 case RECEIVE_DIAGNOSTIC
:
387 case SEND_DIAGNOSTIC
:
390 case MI_REPORT_TARGET_PGS
:
393 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_STANDBY
;
396 case MAINTENANCE_OUT
:
398 case MO_SET_TARGET_PGS
:
401 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_STANDBY
;
405 case PERSISTENT_RESERVE_IN
:
406 case PERSISTENT_RESERVE_OUT
:
411 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_STANDBY
;
418 static inline int core_alua_state_unavailable(
424 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
425 * spc4r17 section 5.9.2.4.5
432 case MI_REPORT_TARGET_PGS
:
435 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
;
438 case MAINTENANCE_OUT
:
440 case MO_SET_TARGET_PGS
:
443 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
;
451 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
;
458 static inline int core_alua_state_transition(
464 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by
465 * spc4r17 section 5.9.2.5
472 case MI_REPORT_TARGET_PGS
:
475 *alua_ascq
= ASCQ_04H_ALUA_STATE_TRANSITION
;
483 *alua_ascq
= ASCQ_04H_ALUA_STATE_TRANSITION
;
491 * Used for alua_type SPC_ALUA_PASSTHROUGH and SPC2_ALUA_DISABLED
492 * in transport_cmd_sequencer(). This function is assigned to
493 * struct t10_alua *->state_check() in core_setup_alua()
495 static int core_alua_state_check_nop(
504 * Used for alua_type SPC3_ALUA_EMULATED in transport_cmd_sequencer().
505 * This function is assigned to struct t10_alua *->state_check() in
508 * Also, this function can return three different return codes to
509 * signal transport_generic_cmd_sequencer()
511 * return 1: Is used to signal LUN not accecsable, and check condition/not ready
512 * return 0: Used to signal success
513 * reutrn -1: Used to signal failure, and invalid cdb field
515 static int core_alua_state_check(
520 struct se_lun
*lun
= cmd
->se_lun
;
521 struct se_port
*port
= lun
->lun_sep
;
522 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
523 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
524 int out_alua_state
, nonop_delay_msecs
;
529 * First, check for a struct se_port specific secondary ALUA target port
530 * access state: OFFLINE
532 if (atomic_read(&port
->sep_tg_pt_secondary_offline
)) {
533 *alua_ascq
= ASCQ_04H_ALUA_OFFLINE
;
534 pr_debug("ALUA: Got secondary offline status for local"
536 *alua_ascq
= ASCQ_04H_ALUA_OFFLINE
;
540 * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
541 * ALUA target port group, to obtain current ALUA access state.
542 * Otherwise look for the underlying struct se_device association with
543 * a ALUA logical unit group.
545 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
546 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
547 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
548 out_alua_state
= atomic_read(&tg_pt_gp
->tg_pt_gp_alua_access_state
);
549 nonop_delay_msecs
= tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
;
550 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
552 * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a separate conditional
553 * statement so the compiler knows explicitly to check this case first.
554 * For the Optimized ALUA access state case, we want to process the
555 * incoming fabric cmd ASAP..
557 if (out_alua_state
== ALUA_ACCESS_STATE_ACTIVE_OPTMIZED
)
560 switch (out_alua_state
) {
561 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
562 return core_alua_state_nonoptimized(cmd
, cdb
,
563 nonop_delay_msecs
, alua_ascq
);
564 case ALUA_ACCESS_STATE_STANDBY
:
565 return core_alua_state_standby(cmd
, cdb
, alua_ascq
);
566 case ALUA_ACCESS_STATE_UNAVAILABLE
:
567 return core_alua_state_unavailable(cmd
, cdb
, alua_ascq
);
568 case ALUA_ACCESS_STATE_TRANSITION
:
569 return core_alua_state_transition(cmd
, cdb
, alua_ascq
);
571 * OFFLINE is a secondary ALUA target port group access state, that is
572 * handled above with struct se_port->sep_tg_pt_secondary_offline=1
574 case ALUA_ACCESS_STATE_OFFLINE
:
576 pr_err("Unknown ALUA access state: 0x%02x\n",
585 * Check implict and explict ALUA state change request.
587 static int core_alua_check_transition(int state
, int *primary
)
590 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED
:
591 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
592 case ALUA_ACCESS_STATE_STANDBY
:
593 case ALUA_ACCESS_STATE_UNAVAILABLE
:
595 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
596 * defined as primary target port asymmetric access states.
600 case ALUA_ACCESS_STATE_OFFLINE
:
602 * OFFLINE state is defined as a secondary target port
603 * asymmetric access state.
608 pr_err("Unknown ALUA access state: 0x%02x\n", state
);
615 static char *core_alua_dump_state(int state
)
618 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED
:
619 return "Active/Optimized";
620 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
621 return "Active/NonOptimized";
622 case ALUA_ACCESS_STATE_STANDBY
:
624 case ALUA_ACCESS_STATE_UNAVAILABLE
:
625 return "Unavailable";
626 case ALUA_ACCESS_STATE_OFFLINE
:
635 char *core_alua_dump_status(int status
)
638 case ALUA_STATUS_NONE
:
640 case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG
:
641 return "Altered by Explict STPG";
642 case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA
:
643 return "Altered by Implict ALUA";
652 * Used by fabric modules to determine when we need to delay processing
653 * for the Active/NonOptimized paths..
655 int core_alua_check_nonop_delay(
658 if (!(cmd
->se_cmd_flags
& SCF_ALUA_NON_OPTIMIZED
))
663 * The ALUA Active/NonOptimized access state delay can be disabled
664 * in via configfs with a value of zero
666 if (!cmd
->alua_nonop_delay
)
669 * struct se_cmd->alua_nonop_delay gets set by a target port group
670 * defined interval in core_alua_state_nonoptimized()
672 msleep_interruptible(cmd
->alua_nonop_delay
);
675 EXPORT_SYMBOL(core_alua_check_nonop_delay
);
678 * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
681 static int core_alua_write_tpg_metadata(
683 unsigned char *md_buf
,
689 int flags
= O_RDWR
| O_CREAT
| O_TRUNC
, ret
;
691 memset(iov
, 0, sizeof(struct iovec
));
693 file
= filp_open(path
, flags
, 0600);
694 if (IS_ERR(file
) || !file
|| !file
->f_dentry
) {
695 pr_err("filp_open(%s) for ALUA metadata failed\n",
700 iov
[0].iov_base
= &md_buf
[0];
701 iov
[0].iov_len
= md_buf_len
;
705 ret
= vfs_writev(file
, &iov
[0], 1, &file
->f_pos
);
709 pr_err("Error writing ALUA metadata file: %s\n", path
);
710 filp_close(file
, NULL
);
713 filp_close(file
, NULL
);
719 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
721 static int core_alua_update_tpg_primary_metadata(
722 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
724 unsigned char *md_buf
)
726 struct se_subsystem_dev
*su_dev
= tg_pt_gp
->tg_pt_gp_su_dev
;
727 struct t10_wwn
*wwn
= &su_dev
->t10_wwn
;
728 char path
[ALUA_METADATA_PATH_LEN
];
731 memset(path
, 0, ALUA_METADATA_PATH_LEN
);
733 len
= snprintf(md_buf
, tg_pt_gp
->tg_pt_gp_md_buf_len
,
735 "alua_access_state=0x%02x\n"
736 "alua_access_status=0x%02x\n",
737 tg_pt_gp
->tg_pt_gp_id
, primary_state
,
738 tg_pt_gp
->tg_pt_gp_alua_access_status
);
740 snprintf(path
, ALUA_METADATA_PATH_LEN
,
741 "/var/target/alua/tpgs_%s/%s", &wwn
->unit_serial
[0],
742 config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
));
744 return core_alua_write_tpg_metadata(path
, md_buf
, len
);
747 static int core_alua_do_transition_tg_pt(
748 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
749 struct se_port
*l_port
,
750 struct se_node_acl
*nacl
,
751 unsigned char *md_buf
,
755 struct se_dev_entry
*se_deve
;
756 struct se_lun_acl
*lacl
;
757 struct se_port
*port
;
758 struct t10_alua_tg_pt_gp_member
*mem
;
761 * Save the old primary ALUA access state, and set the current state
762 * to ALUA_ACCESS_STATE_TRANSITION.
764 old_state
= atomic_read(&tg_pt_gp
->tg_pt_gp_alua_access_state
);
765 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
,
766 ALUA_ACCESS_STATE_TRANSITION
);
767 tg_pt_gp
->tg_pt_gp_alua_access_status
= (explict
) ?
768 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG
:
769 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA
;
771 * Check for the optional ALUA primary state transition delay
773 if (tg_pt_gp
->tg_pt_gp_trans_delay_msecs
!= 0)
774 msleep_interruptible(tg_pt_gp
->tg_pt_gp_trans_delay_msecs
);
776 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
777 list_for_each_entry(mem
, &tg_pt_gp
->tg_pt_gp_mem_list
,
781 * After an implicit target port asymmetric access state
782 * change, a device server shall establish a unit attention
783 * condition for the initiator port associated with every I_T
784 * nexus with the additional sense code set to ASYMMETRIC
785 * ACCESS STATE CHAGED.
787 * After an explicit target port asymmetric access state
788 * change, a device server shall establish a unit attention
789 * condition with the additional sense code set to ASYMMETRIC
790 * ACCESS STATE CHANGED for the initiator port associated with
791 * every I_T nexus other than the I_T nexus on which the SET
792 * TARGET PORT GROUPS command
794 atomic_inc(&mem
->tg_pt_gp_mem_ref_cnt
);
795 smp_mb__after_atomic_inc();
796 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
798 spin_lock_bh(&port
->sep_alua_lock
);
799 list_for_each_entry(se_deve
, &port
->sep_alua_list
,
801 lacl
= se_deve
->se_lun_acl
;
803 * se_deve->se_lun_acl pointer may be NULL for a
804 * entry created without explict Node+MappedLUN ACLs
810 (nacl
!= NULL
) && (nacl
== lacl
->se_lun_nacl
) &&
811 (l_port
!= NULL
) && (l_port
== port
))
814 core_scsi3_ua_allocate(lacl
->se_lun_nacl
,
815 se_deve
->mapped_lun
, 0x2A,
816 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED
);
818 spin_unlock_bh(&port
->sep_alua_lock
);
820 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
821 atomic_dec(&mem
->tg_pt_gp_mem_ref_cnt
);
822 smp_mb__after_atomic_dec();
824 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
826 * Update the ALUA metadata buf that has been allocated in
827 * core_alua_do_port_transition(), this metadata will be written
830 * Note that there is the case where we do not want to update the
831 * metadata when the saved metadata is being parsed in userspace
832 * when setting the existing port access state and access status.
834 * Also note that the failure to write out the ALUA metadata to
835 * struct file does NOT affect the actual ALUA transition.
837 if (tg_pt_gp
->tg_pt_gp_write_metadata
) {
838 mutex_lock(&tg_pt_gp
->tg_pt_gp_md_mutex
);
839 core_alua_update_tpg_primary_metadata(tg_pt_gp
,
841 mutex_unlock(&tg_pt_gp
->tg_pt_gp_md_mutex
);
844 * Set the current primary ALUA access state to the requested new state
846 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
, new_state
);
848 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
849 " from primary access state %s to %s\n", (explict
) ? "explict" :
850 "implict", config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
),
851 tg_pt_gp
->tg_pt_gp_id
, core_alua_dump_state(old_state
),
852 core_alua_dump_state(new_state
));
857 int core_alua_do_port_transition(
858 struct t10_alua_tg_pt_gp
*l_tg_pt_gp
,
859 struct se_device
*l_dev
,
860 struct se_port
*l_port
,
861 struct se_node_acl
*l_nacl
,
865 struct se_device
*dev
;
866 struct se_port
*port
;
867 struct se_subsystem_dev
*su_dev
;
868 struct se_node_acl
*nacl
;
869 struct t10_alua_lu_gp
*lu_gp
;
870 struct t10_alua_lu_gp_member
*lu_gp_mem
, *local_lu_gp_mem
;
871 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
872 unsigned char *md_buf
;
875 if (core_alua_check_transition(new_state
, &primary
) != 0)
878 md_buf
= kzalloc(l_tg_pt_gp
->tg_pt_gp_md_buf_len
, GFP_KERNEL
);
880 pr_err("Unable to allocate buf for ALUA metadata\n");
884 local_lu_gp_mem
= l_dev
->dev_alua_lu_gp_mem
;
885 spin_lock(&local_lu_gp_mem
->lu_gp_mem_lock
);
886 lu_gp
= local_lu_gp_mem
->lu_gp
;
887 atomic_inc(&lu_gp
->lu_gp_ref_cnt
);
888 smp_mb__after_atomic_inc();
889 spin_unlock(&local_lu_gp_mem
->lu_gp_mem_lock
);
891 * For storage objects that are members of the 'default_lu_gp',
892 * we only do transition on the passed *l_tp_pt_gp, and not
893 * on all of the matching target port groups IDs in default_lu_gp.
895 if (!lu_gp
->lu_gp_id
) {
897 * core_alua_do_transition_tg_pt() will always return
900 core_alua_do_transition_tg_pt(l_tg_pt_gp
, l_port
, l_nacl
,
901 md_buf
, new_state
, explict
);
902 atomic_dec(&lu_gp
->lu_gp_ref_cnt
);
903 smp_mb__after_atomic_dec();
908 * For all other LU groups aside from 'default_lu_gp', walk all of
909 * the associated storage objects looking for a matching target port
910 * group ID from the local target port group.
912 spin_lock(&lu_gp
->lu_gp_lock
);
913 list_for_each_entry(lu_gp_mem
, &lu_gp
->lu_gp_mem_list
,
916 dev
= lu_gp_mem
->lu_gp_mem_dev
;
917 su_dev
= dev
->se_sub_dev
;
918 atomic_inc(&lu_gp_mem
->lu_gp_mem_ref_cnt
);
919 smp_mb__after_atomic_inc();
920 spin_unlock(&lu_gp
->lu_gp_lock
);
922 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
923 list_for_each_entry(tg_pt_gp
,
924 &su_dev
->t10_alua
.tg_pt_gps_list
,
927 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
930 * If the target behavior port asymmetric access state
931 * is changed for any target port group accessiable via
932 * a logical unit within a LU group, the target port
933 * behavior group asymmetric access states for the same
934 * target port group accessible via other logical units
935 * in that LU group will also change.
937 if (l_tg_pt_gp
->tg_pt_gp_id
!= tg_pt_gp
->tg_pt_gp_id
)
940 if (l_tg_pt_gp
== tg_pt_gp
) {
947 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
948 smp_mb__after_atomic_inc();
949 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
951 * core_alua_do_transition_tg_pt() will always return
954 core_alua_do_transition_tg_pt(tg_pt_gp
, port
,
955 nacl
, md_buf
, new_state
, explict
);
957 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
958 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
959 smp_mb__after_atomic_dec();
961 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
963 spin_lock(&lu_gp
->lu_gp_lock
);
964 atomic_dec(&lu_gp_mem
->lu_gp_mem_ref_cnt
);
965 smp_mb__after_atomic_dec();
967 spin_unlock(&lu_gp
->lu_gp_lock
);
969 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
970 " Group IDs: %hu %s transition to primary state: %s\n",
971 config_item_name(&lu_gp
->lu_gp_group
.cg_item
),
972 l_tg_pt_gp
->tg_pt_gp_id
, (explict
) ? "explict" : "implict",
973 core_alua_dump_state(new_state
));
975 atomic_dec(&lu_gp
->lu_gp_ref_cnt
);
976 smp_mb__after_atomic_dec();
982 * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
984 static int core_alua_update_tpg_secondary_metadata(
985 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
986 struct se_port
*port
,
987 unsigned char *md_buf
,
990 struct se_portal_group
*se_tpg
= port
->sep_tpg
;
991 char path
[ALUA_METADATA_PATH_LEN
], wwn
[ALUA_SECONDARY_METADATA_WWN_LEN
];
994 memset(path
, 0, ALUA_METADATA_PATH_LEN
);
995 memset(wwn
, 0, ALUA_SECONDARY_METADATA_WWN_LEN
);
997 len
= snprintf(wwn
, ALUA_SECONDARY_METADATA_WWN_LEN
, "%s",
998 se_tpg
->se_tpg_tfo
->tpg_get_wwn(se_tpg
));
1000 if (se_tpg
->se_tpg_tfo
->tpg_get_tag
!= NULL
)
1001 snprintf(wwn
+len
, ALUA_SECONDARY_METADATA_WWN_LEN
-len
, "+%hu",
1002 se_tpg
->se_tpg_tfo
->tpg_get_tag(se_tpg
));
1004 len
= snprintf(md_buf
, md_buf_len
, "alua_tg_pt_offline=%d\n"
1005 "alua_tg_pt_status=0x%02x\n",
1006 atomic_read(&port
->sep_tg_pt_secondary_offline
),
1007 port
->sep_tg_pt_secondary_stat
);
1009 snprintf(path
, ALUA_METADATA_PATH_LEN
, "/var/target/alua/%s/%s/lun_%u",
1010 se_tpg
->se_tpg_tfo
->get_fabric_name(), wwn
,
1011 port
->sep_lun
->unpacked_lun
);
1013 return core_alua_write_tpg_metadata(path
, md_buf
, len
);
1016 static int core_alua_set_tg_pt_secondary_state(
1017 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
1018 struct se_port
*port
,
1022 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1023 unsigned char *md_buf
;
1025 int trans_delay_msecs
;
1027 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1028 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
1030 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1031 pr_err("Unable to complete secondary state"
1035 trans_delay_msecs
= tg_pt_gp
->tg_pt_gp_trans_delay_msecs
;
1037 * Set the secondary ALUA target port access state to OFFLINE
1038 * or release the previously secondary state for struct se_port
1041 atomic_set(&port
->sep_tg_pt_secondary_offline
, 1);
1043 atomic_set(&port
->sep_tg_pt_secondary_offline
, 0);
1045 md_buf_len
= tg_pt_gp
->tg_pt_gp_md_buf_len
;
1046 port
->sep_tg_pt_secondary_stat
= (explict
) ?
1047 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG
:
1048 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA
;
1050 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1051 " to secondary access state: %s\n", (explict
) ? "explict" :
1052 "implict", config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1053 tg_pt_gp
->tg_pt_gp_id
, (offline
) ? "OFFLINE" : "ONLINE");
1055 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1057 * Do the optional transition delay after we set the secondary
1058 * ALUA access state.
1060 if (trans_delay_msecs
!= 0)
1061 msleep_interruptible(trans_delay_msecs
);
1063 * See if we need to update the ALUA fabric port metadata for
1064 * secondary state and status
1066 if (port
->sep_tg_pt_secondary_write_md
) {
1067 md_buf
= kzalloc(md_buf_len
, GFP_KERNEL
);
1069 pr_err("Unable to allocate md_buf for"
1070 " secondary ALUA access metadata\n");
1073 mutex_lock(&port
->sep_tg_pt_md_mutex
);
1074 core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem
, port
,
1075 md_buf
, md_buf_len
);
1076 mutex_unlock(&port
->sep_tg_pt_md_mutex
);
1084 struct t10_alua_lu_gp
*
1085 core_alua_allocate_lu_gp(const char *name
, int def_group
)
1087 struct t10_alua_lu_gp
*lu_gp
;
1089 lu_gp
= kmem_cache_zalloc(t10_alua_lu_gp_cache
, GFP_KERNEL
);
1091 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1092 return ERR_PTR(-ENOMEM
);
1094 INIT_LIST_HEAD(&lu_gp
->lu_gp_node
);
1095 INIT_LIST_HEAD(&lu_gp
->lu_gp_mem_list
);
1096 spin_lock_init(&lu_gp
->lu_gp_lock
);
1097 atomic_set(&lu_gp
->lu_gp_ref_cnt
, 0);
1100 lu_gp
->lu_gp_id
= alua_lu_gps_counter
++;
1101 lu_gp
->lu_gp_valid_id
= 1;
1102 alua_lu_gps_count
++;
1108 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp
*lu_gp
, u16 lu_gp_id
)
1110 struct t10_alua_lu_gp
*lu_gp_tmp
;
1113 * The lu_gp->lu_gp_id may only be set once..
1115 if (lu_gp
->lu_gp_valid_id
) {
1116 pr_warn("ALUA LU Group already has a valid ID,"
1117 " ignoring request\n");
1121 spin_lock(&lu_gps_lock
);
1122 if (alua_lu_gps_count
== 0x0000ffff) {
1123 pr_err("Maximum ALUA alua_lu_gps_count:"
1124 " 0x0000ffff reached\n");
1125 spin_unlock(&lu_gps_lock
);
1126 kmem_cache_free(t10_alua_lu_gp_cache
, lu_gp
);
1130 lu_gp_id_tmp
= (lu_gp_id
!= 0) ? lu_gp_id
:
1131 alua_lu_gps_counter
++;
1133 list_for_each_entry(lu_gp_tmp
, &lu_gps_list
, lu_gp_node
) {
1134 if (lu_gp_tmp
->lu_gp_id
== lu_gp_id_tmp
) {
1138 pr_warn("ALUA Logical Unit Group ID: %hu"
1139 " already exists, ignoring request\n",
1141 spin_unlock(&lu_gps_lock
);
1146 lu_gp
->lu_gp_id
= lu_gp_id_tmp
;
1147 lu_gp
->lu_gp_valid_id
= 1;
1148 list_add_tail(&lu_gp
->lu_gp_node
, &lu_gps_list
);
1149 alua_lu_gps_count
++;
1150 spin_unlock(&lu_gps_lock
);
1155 static struct t10_alua_lu_gp_member
*
1156 core_alua_allocate_lu_gp_mem(struct se_device
*dev
)
1158 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1160 lu_gp_mem
= kmem_cache_zalloc(t10_alua_lu_gp_mem_cache
, GFP_KERNEL
);
1162 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1163 return ERR_PTR(-ENOMEM
);
1165 INIT_LIST_HEAD(&lu_gp_mem
->lu_gp_mem_list
);
1166 spin_lock_init(&lu_gp_mem
->lu_gp_mem_lock
);
1167 atomic_set(&lu_gp_mem
->lu_gp_mem_ref_cnt
, 0);
1169 lu_gp_mem
->lu_gp_mem_dev
= dev
;
1170 dev
->dev_alua_lu_gp_mem
= lu_gp_mem
;
1175 void core_alua_free_lu_gp(struct t10_alua_lu_gp
*lu_gp
)
1177 struct t10_alua_lu_gp_member
*lu_gp_mem
, *lu_gp_mem_tmp
;
1179 * Once we have reached this point, config_item_put() has
1180 * already been called from target_core_alua_drop_lu_gp().
1182 * Here, we remove the *lu_gp from the global list so that
1183 * no associations can be made while we are releasing
1184 * struct t10_alua_lu_gp.
1186 spin_lock(&lu_gps_lock
);
1187 atomic_set(&lu_gp
->lu_gp_shutdown
, 1);
1188 list_del(&lu_gp
->lu_gp_node
);
1189 alua_lu_gps_count
--;
1190 spin_unlock(&lu_gps_lock
);
1192 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1193 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1194 * released with core_alua_put_lu_gp_from_name()
1196 while (atomic_read(&lu_gp
->lu_gp_ref_cnt
))
1199 * Release reference to struct t10_alua_lu_gp * from all associated
1202 spin_lock(&lu_gp
->lu_gp_lock
);
1203 list_for_each_entry_safe(lu_gp_mem
, lu_gp_mem_tmp
,
1204 &lu_gp
->lu_gp_mem_list
, lu_gp_mem_list
) {
1205 if (lu_gp_mem
->lu_gp_assoc
) {
1206 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1207 lu_gp
->lu_gp_members
--;
1208 lu_gp_mem
->lu_gp_assoc
= 0;
1210 spin_unlock(&lu_gp
->lu_gp_lock
);
1213 * lu_gp_mem is associated with a single
1214 * struct se_device->dev_alua_lu_gp_mem, and is released when
1215 * struct se_device is released via core_alua_free_lu_gp_mem().
1217 * If the passed lu_gp does NOT match the default_lu_gp, assume
1218 * we want to re-assocate a given lu_gp_mem with default_lu_gp.
1220 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1221 if (lu_gp
!= default_lu_gp
)
1222 __core_alua_attach_lu_gp_mem(lu_gp_mem
,
1225 lu_gp_mem
->lu_gp
= NULL
;
1226 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1228 spin_lock(&lu_gp
->lu_gp_lock
);
1230 spin_unlock(&lu_gp
->lu_gp_lock
);
1232 kmem_cache_free(t10_alua_lu_gp_cache
, lu_gp
);
1235 void core_alua_free_lu_gp_mem(struct se_device
*dev
)
1237 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
1238 struct t10_alua
*alua
= &su_dev
->t10_alua
;
1239 struct t10_alua_lu_gp
*lu_gp
;
1240 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1242 if (alua
->alua_type
!= SPC3_ALUA_EMULATED
)
1245 lu_gp_mem
= dev
->dev_alua_lu_gp_mem
;
1249 while (atomic_read(&lu_gp_mem
->lu_gp_mem_ref_cnt
))
1252 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1253 lu_gp
= lu_gp_mem
->lu_gp
;
1255 spin_lock(&lu_gp
->lu_gp_lock
);
1256 if (lu_gp_mem
->lu_gp_assoc
) {
1257 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1258 lu_gp
->lu_gp_members
--;
1259 lu_gp_mem
->lu_gp_assoc
= 0;
1261 spin_unlock(&lu_gp
->lu_gp_lock
);
1262 lu_gp_mem
->lu_gp
= NULL
;
1264 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1266 kmem_cache_free(t10_alua_lu_gp_mem_cache
, lu_gp_mem
);
1269 struct t10_alua_lu_gp
*core_alua_get_lu_gp_by_name(const char *name
)
1271 struct t10_alua_lu_gp
*lu_gp
;
1272 struct config_item
*ci
;
1274 spin_lock(&lu_gps_lock
);
1275 list_for_each_entry(lu_gp
, &lu_gps_list
, lu_gp_node
) {
1276 if (!lu_gp
->lu_gp_valid_id
)
1278 ci
= &lu_gp
->lu_gp_group
.cg_item
;
1279 if (!strcmp(config_item_name(ci
), name
)) {
1280 atomic_inc(&lu_gp
->lu_gp_ref_cnt
);
1281 spin_unlock(&lu_gps_lock
);
1285 spin_unlock(&lu_gps_lock
);
1290 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp
*lu_gp
)
1292 spin_lock(&lu_gps_lock
);
1293 atomic_dec(&lu_gp
->lu_gp_ref_cnt
);
1294 spin_unlock(&lu_gps_lock
);
1298 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1300 void __core_alua_attach_lu_gp_mem(
1301 struct t10_alua_lu_gp_member
*lu_gp_mem
,
1302 struct t10_alua_lu_gp
*lu_gp
)
1304 spin_lock(&lu_gp
->lu_gp_lock
);
1305 lu_gp_mem
->lu_gp
= lu_gp
;
1306 lu_gp_mem
->lu_gp_assoc
= 1;
1307 list_add_tail(&lu_gp_mem
->lu_gp_mem_list
, &lu_gp
->lu_gp_mem_list
);
1308 lu_gp
->lu_gp_members
++;
1309 spin_unlock(&lu_gp
->lu_gp_lock
);
1313 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1315 void __core_alua_drop_lu_gp_mem(
1316 struct t10_alua_lu_gp_member
*lu_gp_mem
,
1317 struct t10_alua_lu_gp
*lu_gp
)
1319 spin_lock(&lu_gp
->lu_gp_lock
);
1320 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1321 lu_gp_mem
->lu_gp
= NULL
;
1322 lu_gp_mem
->lu_gp_assoc
= 0;
1323 lu_gp
->lu_gp_members
--;
1324 spin_unlock(&lu_gp
->lu_gp_lock
);
1327 struct t10_alua_tg_pt_gp
*core_alua_allocate_tg_pt_gp(
1328 struct se_subsystem_dev
*su_dev
,
1332 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1334 tg_pt_gp
= kmem_cache_zalloc(t10_alua_tg_pt_gp_cache
, GFP_KERNEL
);
1336 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1339 INIT_LIST_HEAD(&tg_pt_gp
->tg_pt_gp_list
);
1340 INIT_LIST_HEAD(&tg_pt_gp
->tg_pt_gp_mem_list
);
1341 mutex_init(&tg_pt_gp
->tg_pt_gp_md_mutex
);
1342 spin_lock_init(&tg_pt_gp
->tg_pt_gp_lock
);
1343 atomic_set(&tg_pt_gp
->tg_pt_gp_ref_cnt
, 0);
1344 tg_pt_gp
->tg_pt_gp_su_dev
= su_dev
;
1345 tg_pt_gp
->tg_pt_gp_md_buf_len
= ALUA_MD_BUF_LEN
;
1346 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
,
1347 ALUA_ACCESS_STATE_ACTIVE_OPTMIZED
);
1349 * Enable both explict and implict ALUA support by default
1351 tg_pt_gp
->tg_pt_gp_alua_access_type
=
1352 TPGS_EXPLICT_ALUA
| TPGS_IMPLICT_ALUA
;
1354 * Set the default Active/NonOptimized Delay in milliseconds
1356 tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
= ALUA_DEFAULT_NONOP_DELAY_MSECS
;
1357 tg_pt_gp
->tg_pt_gp_trans_delay_msecs
= ALUA_DEFAULT_TRANS_DELAY_MSECS
;
1360 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1361 tg_pt_gp
->tg_pt_gp_id
=
1362 su_dev
->t10_alua
.alua_tg_pt_gps_counter
++;
1363 tg_pt_gp
->tg_pt_gp_valid_id
= 1;
1364 su_dev
->t10_alua
.alua_tg_pt_gps_count
++;
1365 list_add_tail(&tg_pt_gp
->tg_pt_gp_list
,
1366 &su_dev
->t10_alua
.tg_pt_gps_list
);
1367 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1373 int core_alua_set_tg_pt_gp_id(
1374 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1377 struct se_subsystem_dev
*su_dev
= tg_pt_gp
->tg_pt_gp_su_dev
;
1378 struct t10_alua_tg_pt_gp
*tg_pt_gp_tmp
;
1379 u16 tg_pt_gp_id_tmp
;
1381 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1383 if (tg_pt_gp
->tg_pt_gp_valid_id
) {
1384 pr_warn("ALUA TG PT Group already has a valid ID,"
1385 " ignoring request\n");
1389 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1390 if (su_dev
->t10_alua
.alua_tg_pt_gps_count
== 0x0000ffff) {
1391 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1392 " 0x0000ffff reached\n");
1393 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1394 kmem_cache_free(t10_alua_tg_pt_gp_cache
, tg_pt_gp
);
1398 tg_pt_gp_id_tmp
= (tg_pt_gp_id
!= 0) ? tg_pt_gp_id
:
1399 su_dev
->t10_alua
.alua_tg_pt_gps_counter
++;
1401 list_for_each_entry(tg_pt_gp_tmp
, &su_dev
->t10_alua
.tg_pt_gps_list
,
1403 if (tg_pt_gp_tmp
->tg_pt_gp_id
== tg_pt_gp_id_tmp
) {
1407 pr_err("ALUA Target Port Group ID: %hu already"
1408 " exists, ignoring request\n", tg_pt_gp_id
);
1409 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1414 tg_pt_gp
->tg_pt_gp_id
= tg_pt_gp_id_tmp
;
1415 tg_pt_gp
->tg_pt_gp_valid_id
= 1;
1416 list_add_tail(&tg_pt_gp
->tg_pt_gp_list
,
1417 &su_dev
->t10_alua
.tg_pt_gps_list
);
1418 su_dev
->t10_alua
.alua_tg_pt_gps_count
++;
1419 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1424 struct t10_alua_tg_pt_gp_member
*core_alua_allocate_tg_pt_gp_mem(
1425 struct se_port
*port
)
1427 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1429 tg_pt_gp_mem
= kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache
,
1431 if (!tg_pt_gp_mem
) {
1432 pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n");
1433 return ERR_PTR(-ENOMEM
);
1435 INIT_LIST_HEAD(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1436 spin_lock_init(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1437 atomic_set(&tg_pt_gp_mem
->tg_pt_gp_mem_ref_cnt
, 0);
1439 tg_pt_gp_mem
->tg_pt
= port
;
1440 port
->sep_alua_tg_pt_gp_mem
= tg_pt_gp_mem
;
1441 atomic_set(&port
->sep_tg_pt_gp_active
, 1);
1443 return tg_pt_gp_mem
;
1446 void core_alua_free_tg_pt_gp(
1447 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1449 struct se_subsystem_dev
*su_dev
= tg_pt_gp
->tg_pt_gp_su_dev
;
1450 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
, *tg_pt_gp_mem_tmp
;
1452 * Once we have reached this point, config_item_put() has already
1453 * been called from target_core_alua_drop_tg_pt_gp().
1455 * Here we remove *tg_pt_gp from the global list so that
1456 * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS
1457 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1459 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1460 list_del(&tg_pt_gp
->tg_pt_gp_list
);
1461 su_dev
->t10_alua
.alua_tg_pt_gps_counter
--;
1462 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1464 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1465 * core_alua_get_tg_pt_gp_by_name() in
1466 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1467 * to be released with core_alua_put_tg_pt_gp_from_name().
1469 while (atomic_read(&tg_pt_gp
->tg_pt_gp_ref_cnt
))
1472 * Release reference to struct t10_alua_tg_pt_gp from all associated
1475 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1476 list_for_each_entry_safe(tg_pt_gp_mem
, tg_pt_gp_mem_tmp
,
1477 &tg_pt_gp
->tg_pt_gp_mem_list
, tg_pt_gp_mem_list
) {
1478 if (tg_pt_gp_mem
->tg_pt_gp_assoc
) {
1479 list_del(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1480 tg_pt_gp
->tg_pt_gp_members
--;
1481 tg_pt_gp_mem
->tg_pt_gp_assoc
= 0;
1483 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1485 * tg_pt_gp_mem is associated with a single
1486 * se_port->sep_alua_tg_pt_gp_mem, and is released via
1487 * core_alua_free_tg_pt_gp_mem().
1489 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1490 * assume we want to re-assocate a given tg_pt_gp_mem with
1493 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1494 if (tg_pt_gp
!= su_dev
->t10_alua
.default_tg_pt_gp
) {
1495 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem
,
1496 su_dev
->t10_alua
.default_tg_pt_gp
);
1498 tg_pt_gp_mem
->tg_pt_gp
= NULL
;
1499 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1501 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1503 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1505 kmem_cache_free(t10_alua_tg_pt_gp_cache
, tg_pt_gp
);
1508 void core_alua_free_tg_pt_gp_mem(struct se_port
*port
)
1510 struct se_subsystem_dev
*su_dev
= port
->sep_lun
->lun_se_dev
->se_sub_dev
;
1511 struct t10_alua
*alua
= &su_dev
->t10_alua
;
1512 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1513 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1515 if (alua
->alua_type
!= SPC3_ALUA_EMULATED
)
1518 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
1522 while (atomic_read(&tg_pt_gp_mem
->tg_pt_gp_mem_ref_cnt
))
1525 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1526 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
1528 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1529 if (tg_pt_gp_mem
->tg_pt_gp_assoc
) {
1530 list_del(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1531 tg_pt_gp
->tg_pt_gp_members
--;
1532 tg_pt_gp_mem
->tg_pt_gp_assoc
= 0;
1534 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1535 tg_pt_gp_mem
->tg_pt_gp
= NULL
;
1537 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1539 kmem_cache_free(t10_alua_tg_pt_gp_mem_cache
, tg_pt_gp_mem
);
1542 static struct t10_alua_tg_pt_gp
*core_alua_get_tg_pt_gp_by_name(
1543 struct se_subsystem_dev
*su_dev
,
1546 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1547 struct config_item
*ci
;
1549 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1550 list_for_each_entry(tg_pt_gp
, &su_dev
->t10_alua
.tg_pt_gps_list
,
1552 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
1554 ci
= &tg_pt_gp
->tg_pt_gp_group
.cg_item
;
1555 if (!strcmp(config_item_name(ci
), name
)) {
1556 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1557 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1561 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1566 static void core_alua_put_tg_pt_gp_from_name(
1567 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1569 struct se_subsystem_dev
*su_dev
= tg_pt_gp
->tg_pt_gp_su_dev
;
1571 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1572 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1573 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1577 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1579 void __core_alua_attach_tg_pt_gp_mem(
1580 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
1581 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1583 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1584 tg_pt_gp_mem
->tg_pt_gp
= tg_pt_gp
;
1585 tg_pt_gp_mem
->tg_pt_gp_assoc
= 1;
1586 list_add_tail(&tg_pt_gp_mem
->tg_pt_gp_mem_list
,
1587 &tg_pt_gp
->tg_pt_gp_mem_list
);
1588 tg_pt_gp
->tg_pt_gp_members
++;
1589 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1593 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1595 static void __core_alua_drop_tg_pt_gp_mem(
1596 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
1597 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1599 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1600 list_del(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1601 tg_pt_gp_mem
->tg_pt_gp
= NULL
;
1602 tg_pt_gp_mem
->tg_pt_gp_assoc
= 0;
1603 tg_pt_gp
->tg_pt_gp_members
--;
1604 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1607 ssize_t
core_alua_show_tg_pt_gp_info(struct se_port
*port
, char *page
)
1609 struct se_subsystem_dev
*su_dev
= port
->sep_lun
->lun_se_dev
->se_sub_dev
;
1610 struct config_item
*tg_pt_ci
;
1611 struct t10_alua
*alua
= &su_dev
->t10_alua
;
1612 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1613 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1616 if (alua
->alua_type
!= SPC3_ALUA_EMULATED
)
1619 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
1623 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1624 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
1626 tg_pt_ci
= &tg_pt_gp
->tg_pt_gp_group
.cg_item
;
1627 len
+= sprintf(page
, "TG Port Alias: %s\nTG Port Group ID:"
1628 " %hu\nTG Port Primary Access State: %s\nTG Port "
1629 "Primary Access Status: %s\nTG Port Secondary Access"
1630 " State: %s\nTG Port Secondary Access Status: %s\n",
1631 config_item_name(tg_pt_ci
), tg_pt_gp
->tg_pt_gp_id
,
1632 core_alua_dump_state(atomic_read(
1633 &tg_pt_gp
->tg_pt_gp_alua_access_state
)),
1634 core_alua_dump_status(
1635 tg_pt_gp
->tg_pt_gp_alua_access_status
),
1636 (atomic_read(&port
->sep_tg_pt_secondary_offline
)) ?
1638 core_alua_dump_status(port
->sep_tg_pt_secondary_stat
));
1640 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1645 ssize_t
core_alua_store_tg_pt_gp_info(
1646 struct se_port
*port
,
1650 struct se_portal_group
*tpg
;
1652 struct se_subsystem_dev
*su_dev
= port
->sep_lun
->lun_se_dev
->se_sub_dev
;
1653 struct t10_alua_tg_pt_gp
*tg_pt_gp
= NULL
, *tg_pt_gp_new
= NULL
;
1654 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1655 unsigned char buf
[TG_PT_GROUP_NAME_BUF
];
1658 tpg
= port
->sep_tpg
;
1659 lun
= port
->sep_lun
;
1661 if (su_dev
->t10_alua
.alua_type
!= SPC3_ALUA_EMULATED
) {
1662 pr_warn("SPC3_ALUA_EMULATED not enabled for"
1663 " %s/tpgt_%hu/%s\n", tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1664 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1665 config_item_name(&lun
->lun_group
.cg_item
));
1669 if (count
> TG_PT_GROUP_NAME_BUF
) {
1670 pr_err("ALUA Target Port Group alias too large!\n");
1673 memset(buf
, 0, TG_PT_GROUP_NAME_BUF
);
1674 memcpy(buf
, page
, count
);
1676 * Any ALUA target port group alias besides "NULL" means we will be
1677 * making a new group association.
1679 if (strcmp(strstrip(buf
), "NULL")) {
1681 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1682 * struct t10_alua_tg_pt_gp. This reference is released with
1683 * core_alua_put_tg_pt_gp_from_name() below.
1685 tg_pt_gp_new
= core_alua_get_tg_pt_gp_by_name(su_dev
,
1690 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
1691 if (!tg_pt_gp_mem
) {
1693 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new
);
1694 pr_err("NULL struct se_port->sep_alua_tg_pt_gp_mem pointer\n");
1698 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1699 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
1702 * Clearing an existing tg_pt_gp association, and replacing
1703 * with the default_tg_pt_gp.
1705 if (!tg_pt_gp_new
) {
1706 pr_debug("Target_Core_ConfigFS: Moving"
1707 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1708 " alua/%s, ID: %hu back to"
1709 " default_tg_pt_gp\n",
1710 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1711 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1712 config_item_name(&lun
->lun_group
.cg_item
),
1714 &tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1715 tg_pt_gp
->tg_pt_gp_id
);
1717 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem
, tg_pt_gp
);
1718 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem
,
1719 su_dev
->t10_alua
.default_tg_pt_gp
);
1720 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1725 * Removing existing association of tg_pt_gp_mem with tg_pt_gp
1727 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem
, tg_pt_gp
);
1731 * Associate tg_pt_gp_mem with tg_pt_gp_new.
1733 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem
, tg_pt_gp_new
);
1734 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1735 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
1736 " Target Port Group: alua/%s, ID: %hu\n", (move
) ?
1737 "Moving" : "Adding", tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1738 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1739 config_item_name(&lun
->lun_group
.cg_item
),
1740 config_item_name(&tg_pt_gp_new
->tg_pt_gp_group
.cg_item
),
1741 tg_pt_gp_new
->tg_pt_gp_id
);
1743 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new
);
1747 ssize_t
core_alua_show_access_type(
1748 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1751 if ((tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICT_ALUA
) &&
1752 (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_IMPLICT_ALUA
))
1753 return sprintf(page
, "Implict and Explict\n");
1754 else if (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_IMPLICT_ALUA
)
1755 return sprintf(page
, "Implict\n");
1756 else if (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICT_ALUA
)
1757 return sprintf(page
, "Explict\n");
1759 return sprintf(page
, "None\n");
1762 ssize_t
core_alua_store_access_type(
1763 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1770 ret
= strict_strtoul(page
, 0, &tmp
);
1772 pr_err("Unable to extract alua_access_type\n");
1775 if ((tmp
!= 0) && (tmp
!= 1) && (tmp
!= 2) && (tmp
!= 3)) {
1776 pr_err("Illegal value for alua_access_type:"
1781 tg_pt_gp
->tg_pt_gp_alua_access_type
=
1782 TPGS_IMPLICT_ALUA
| TPGS_EXPLICT_ALUA
;
1784 tg_pt_gp
->tg_pt_gp_alua_access_type
= TPGS_EXPLICT_ALUA
;
1786 tg_pt_gp
->tg_pt_gp_alua_access_type
= TPGS_IMPLICT_ALUA
;
1788 tg_pt_gp
->tg_pt_gp_alua_access_type
= 0;
1793 ssize_t
core_alua_show_nonop_delay_msecs(
1794 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1797 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
);
1800 ssize_t
core_alua_store_nonop_delay_msecs(
1801 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1808 ret
= strict_strtoul(page
, 0, &tmp
);
1810 pr_err("Unable to extract nonop_delay_msecs\n");
1813 if (tmp
> ALUA_MAX_NONOP_DELAY_MSECS
) {
1814 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
1815 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp
,
1816 ALUA_MAX_NONOP_DELAY_MSECS
);
1819 tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
= (int)tmp
;
1824 ssize_t
core_alua_show_trans_delay_msecs(
1825 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1828 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_trans_delay_msecs
);
1831 ssize_t
core_alua_store_trans_delay_msecs(
1832 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1839 ret
= strict_strtoul(page
, 0, &tmp
);
1841 pr_err("Unable to extract trans_delay_msecs\n");
1844 if (tmp
> ALUA_MAX_TRANS_DELAY_MSECS
) {
1845 pr_err("Passed trans_delay_msecs: %lu, exceeds"
1846 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp
,
1847 ALUA_MAX_TRANS_DELAY_MSECS
);
1850 tg_pt_gp
->tg_pt_gp_trans_delay_msecs
= (int)tmp
;
1855 ssize_t
core_alua_show_preferred_bit(
1856 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1859 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_pref
);
1862 ssize_t
core_alua_store_preferred_bit(
1863 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1870 ret
= strict_strtoul(page
, 0, &tmp
);
1872 pr_err("Unable to extract preferred ALUA value\n");
1875 if ((tmp
!= 0) && (tmp
!= 1)) {
1876 pr_err("Illegal value for preferred ALUA: %lu\n", tmp
);
1879 tg_pt_gp
->tg_pt_gp_pref
= (int)tmp
;
1884 ssize_t
core_alua_show_offline_bit(struct se_lun
*lun
, char *page
)
1889 return sprintf(page
, "%d\n",
1890 atomic_read(&lun
->lun_sep
->sep_tg_pt_secondary_offline
));
1893 ssize_t
core_alua_store_offline_bit(
1898 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1905 ret
= strict_strtoul(page
, 0, &tmp
);
1907 pr_err("Unable to extract alua_tg_pt_offline value\n");
1910 if ((tmp
!= 0) && (tmp
!= 1)) {
1911 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
1915 tg_pt_gp_mem
= lun
->lun_sep
->sep_alua_tg_pt_gp_mem
;
1916 if (!tg_pt_gp_mem
) {
1917 pr_err("Unable to locate *tg_pt_gp_mem\n");
1921 ret
= core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem
,
1922 lun
->lun_sep
, 0, (int)tmp
);
1929 ssize_t
core_alua_show_secondary_status(
1933 return sprintf(page
, "%d\n", lun
->lun_sep
->sep_tg_pt_secondary_stat
);
1936 ssize_t
core_alua_store_secondary_status(
1944 ret
= strict_strtoul(page
, 0, &tmp
);
1946 pr_err("Unable to extract alua_tg_pt_status\n");
1949 if ((tmp
!= ALUA_STATUS_NONE
) &&
1950 (tmp
!= ALUA_STATUS_ALTERED_BY_EXPLICT_STPG
) &&
1951 (tmp
!= ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA
)) {
1952 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
1956 lun
->lun_sep
->sep_tg_pt_secondary_stat
= (int)tmp
;
1961 ssize_t
core_alua_show_secondary_write_metadata(
1965 return sprintf(page
, "%d\n",
1966 lun
->lun_sep
->sep_tg_pt_secondary_write_md
);
1969 ssize_t
core_alua_store_secondary_write_metadata(
1977 ret
= strict_strtoul(page
, 0, &tmp
);
1979 pr_err("Unable to extract alua_tg_pt_write_md\n");
1982 if ((tmp
!= 0) && (tmp
!= 1)) {
1983 pr_err("Illegal value for alua_tg_pt_write_md:"
1987 lun
->lun_sep
->sep_tg_pt_secondary_write_md
= (int)tmp
;
1992 int core_setup_alua(struct se_device
*dev
, int force_pt
)
1994 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
1995 struct t10_alua
*alua
= &su_dev
->t10_alua
;
1996 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1998 * If this device is from Target_Core_Mod/pSCSI, use the ALUA logic
1999 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
2000 * cause a problem because libata and some SATA RAID HBAs appear
2001 * under Linux/SCSI, but emulate SCSI logic themselves.
2003 if (((dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
) &&
2004 !(dev
->se_sub_dev
->se_dev_attrib
.emulate_alua
)) || force_pt
) {
2005 alua
->alua_type
= SPC_ALUA_PASSTHROUGH
;
2006 alua
->alua_state_check
= &core_alua_state_check_nop
;
2007 pr_debug("%s: Using SPC_ALUA_PASSTHROUGH, no ALUA"
2008 " emulation\n", dev
->transport
->name
);
2012 * If SPC-3 or above is reported by real or emulated struct se_device,
2013 * use emulated ALUA.
2015 if (dev
->transport
->get_device_rev(dev
) >= SCSI_3
) {
2016 pr_debug("%s: Enabling ALUA Emulation for SPC-3"
2017 " device\n", dev
->transport
->name
);
2019 * Associate this struct se_device with the default ALUA
2022 lu_gp_mem
= core_alua_allocate_lu_gp_mem(dev
);
2023 if (IS_ERR(lu_gp_mem
))
2024 return PTR_ERR(lu_gp_mem
);
2026 alua
->alua_type
= SPC3_ALUA_EMULATED
;
2027 alua
->alua_state_check
= &core_alua_state_check
;
2028 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
2029 __core_alua_attach_lu_gp_mem(lu_gp_mem
,
2031 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
2033 pr_debug("%s: Adding to default ALUA LU Group:"
2034 " core/alua/lu_gps/default_lu_gp\n",
2035 dev
->transport
->name
);
2037 alua
->alua_type
= SPC2_ALUA_DISABLED
;
2038 alua
->alua_state_check
= &core_alua_state_check_nop
;
2039 pr_debug("%s: Disabling ALUA Emulation for SPC-2"
2040 " device\n", dev
->transport
->name
);