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>
33 #include <asm/unaligned.h>
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>
40 #include "target_core_internal.h"
41 #include "target_core_alua.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_cmd
*cmd
)
64 struct se_device
*dev
= cmd
->se_dev
;
66 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
67 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
70 int ext_hdr
= (cmd
->t_task_cdb
[1] & 0x20);
72 * Skip over RESERVED area to first Target port group descriptor
73 * depending on the PARAMETER DATA FORMAT type..
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
;
87 buf
= transport_kmap_data_sg(cmd
);
89 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
90 list_for_each_entry(tg_pt_gp
, &dev
->t10_alua
.tg_pt_gps_list
,
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.
98 if ((off
+ 8 + (tg_pt_gp
->tg_pt_gp_members
* 4)) >
100 rd_len
+= 8 + (tg_pt_gp
->tg_pt_gp_members
* 4);
104 * PREF: Preferred target port bit, determine if this
105 * bit should be set for port group.
107 if (tg_pt_gp
->tg_pt_gp_pref
)
110 * Set the ASYMMETRIC ACCESS State
112 buf
[off
++] |= (atomic_read(
113 &tg_pt_gp
->tg_pt_gp_alua_access_state
) & 0xff);
115 * Set supported ASYMMETRIC ACCESS State bits
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 */
126 buf
[off
++] = ((tg_pt_gp
->tg_pt_gp_id
>> 8) & 0xff);
127 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_id
& 0xff);
129 off
++; /* Skip over Reserved */
133 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_alua_access_status
& 0xff);
135 * Vendor Specific field
141 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_members
& 0xff);
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
,
147 port
= tg_pt_gp_mem
->tg_pt
;
149 * Start Target Port descriptor format
151 * See spc4r17 section 6.2.7 Table 247
153 off
+= 2; /* Skip over Obsolete */
155 * Set RELATIVE TARGET PORT IDENTIFIER
157 buf
[off
++] = ((port
->sep_rtpi
>> 8) & 0xff);
158 buf
[off
++] = (port
->sep_rtpi
& 0xff);
161 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
163 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
165 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
167 put_unaligned_be32(rd_len
, &buf
[0]);
170 * Fill in the Extended header parameter data format if requested
175 * Set the implict transition time (in seconds) for the application
176 * client to use as a base for it's transition timeout value.
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.
182 port
= cmd
->se_lun
->lun_sep
;
183 tg_pt_gp_mem
= port
->sep_alua_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
;
188 buf
[5] = tg_pt_gp
->tg_pt_gp_implict_trans_secs
;
189 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
192 transport_kunmap_data_sg(cmd
);
194 target_complete_cmd(cmd
, GOOD
);
199 * SET_TARGET_PORT_GROUPS for explict ALUA operation.
201 * See spc4r17 section 6.35
203 int target_emulate_set_target_port_groups(struct se_cmd
*cmd
)
205 struct se_device
*dev
= cmd
->se_dev
;
206 struct se_port
*port
, *l_port
= cmd
->se_lun
->lun_sep
;
207 struct se_node_acl
*nacl
= cmd
->se_sess
->se_node_acl
;
208 struct t10_alua_tg_pt_gp
*tg_pt_gp
= NULL
, *l_tg_pt_gp
;
209 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
, *l_tg_pt_gp_mem
;
212 u32 len
= 4; /* Skip over RESERVED area in header */
213 int alua_access_state
, primary
= 0, rc
;
217 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
220 if (cmd
->data_length
< 4) {
221 pr_warn("SET TARGET PORT GROUPS parameter list length %u too"
222 " small\n", cmd
->data_length
);
223 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
227 buf
= transport_kmap_data_sg(cmd
);
230 * Determine if explict ALUA via SET_TARGET_PORT_GROUPS is allowed
231 * for the local tg_pt_gp.
233 l_tg_pt_gp_mem
= l_port
->sep_alua_tg_pt_gp_mem
;
234 if (!l_tg_pt_gp_mem
) {
235 pr_err("Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
236 cmd
->scsi_sense_reason
= TCM_UNSUPPORTED_SCSI_OPCODE
;
240 spin_lock(&l_tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
241 l_tg_pt_gp
= l_tg_pt_gp_mem
->tg_pt_gp
;
243 spin_unlock(&l_tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
244 pr_err("Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n");
245 cmd
->scsi_sense_reason
= TCM_UNSUPPORTED_SCSI_OPCODE
;
249 rc
= (l_tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICT_ALUA
);
250 spin_unlock(&l_tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
253 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
254 " while TPGS_EXPLICT_ALUA is disabled\n");
255 cmd
->scsi_sense_reason
= TCM_UNSUPPORTED_SCSI_OPCODE
;
260 ptr
= &buf
[4]; /* Skip over RESERVED area in header */
262 while (len
< cmd
->data_length
) {
263 alua_access_state
= (ptr
[0] & 0x0f);
265 * Check the received ALUA access state, and determine if
266 * the state is a primary or secondary target port asymmetric
269 rc
= core_alua_check_transition(alua_access_state
, &primary
);
272 * If the SET TARGET PORT GROUPS attempts to establish
273 * an invalid combination of target port asymmetric
274 * access states or attempts to establish an
275 * unsupported target port asymmetric access state,
276 * then the command shall be terminated with CHECK
277 * CONDITION status, with the sense key set to ILLEGAL
278 * REQUEST, and the additional sense code set to INVALID
279 * FIELD IN PARAMETER LIST.
281 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
287 * If the ASYMMETRIC ACCESS STATE field (see table 267)
288 * specifies a primary target port asymmetric access state,
289 * then the TARGET PORT GROUP OR TARGET PORT field specifies
290 * a primary target port group for which the primary target
291 * port asymmetric access state shall be changed. If the
292 * ASYMMETRIC ACCESS STATE field specifies a secondary target
293 * port asymmetric access state, then the TARGET PORT GROUP OR
294 * TARGET PORT field specifies the relative target port
295 * identifier (see 3.1.120) of the target port for which the
296 * secondary target port asymmetric access state shall be
300 tg_pt_id
= get_unaligned_be16(ptr
+ 2);
302 * Locate the matching target port group ID from
303 * the global tg_pt_gp list
305 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
306 list_for_each_entry(tg_pt_gp
,
307 &dev
->t10_alua
.tg_pt_gps_list
,
309 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
312 if (tg_pt_id
!= tg_pt_gp
->tg_pt_gp_id
)
315 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
316 smp_mb__after_atomic_inc();
317 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
319 rc
= core_alua_do_port_transition(tg_pt_gp
,
321 alua_access_state
, 1);
323 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
324 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
325 smp_mb__after_atomic_dec();
328 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
330 * If not matching target port group ID can be located
331 * throw an exception with ASCQ: INVALID_PARAMETER_LIST
334 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
340 * Extact the RELATIVE TARGET PORT IDENTIFIER to identify
341 * the Target Port in question for the the incoming
342 * SET_TARGET_PORT_GROUPS op.
344 rtpi
= get_unaligned_be16(ptr
+ 2);
346 * Locate the matching relative target port identifier
347 * for the struct se_device storage object.
349 spin_lock(&dev
->se_port_lock
);
350 list_for_each_entry(port
, &dev
->dev_sep_list
,
352 if (port
->sep_rtpi
!= rtpi
)
355 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
356 spin_unlock(&dev
->se_port_lock
);
358 rc
= core_alua_set_tg_pt_secondary_state(
359 tg_pt_gp_mem
, port
, 1, 1);
361 spin_lock(&dev
->se_port_lock
);
364 spin_unlock(&dev
->se_port_lock
);
366 * If not matching relative target port identifier can
367 * be located, throw an exception with ASCQ:
368 * INVALID_PARAMETER_LIST
371 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
382 transport_kunmap_data_sg(cmd
);
384 target_complete_cmd(cmd
, GOOD
);
388 static inline int core_alua_state_nonoptimized(
391 int nonop_delay_msecs
,
395 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
396 * later to determine if processing of this cmd needs to be
397 * temporarily delayed for the Active/NonOptimized primary access state.
399 cmd
->se_cmd_flags
|= SCF_ALUA_NON_OPTIMIZED
;
400 cmd
->alua_nonop_delay
= nonop_delay_msecs
;
404 static inline int core_alua_state_standby(
410 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
411 * spc4r17 section 5.9.2.4.4
420 case RECEIVE_DIAGNOSTIC
:
421 case SEND_DIAGNOSTIC
:
423 switch (cdb
[1] & 0x1f) {
424 case MI_REPORT_TARGET_PGS
:
427 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_STANDBY
;
430 case MAINTENANCE_OUT
:
432 case MO_SET_TARGET_PGS
:
435 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_STANDBY
;
439 case PERSISTENT_RESERVE_IN
:
440 case PERSISTENT_RESERVE_OUT
:
445 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_STANDBY
;
452 static inline int core_alua_state_unavailable(
458 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
459 * spc4r17 section 5.9.2.4.5
465 switch (cdb
[1] & 0x1f) {
466 case MI_REPORT_TARGET_PGS
:
469 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
;
472 case MAINTENANCE_OUT
:
474 case MO_SET_TARGET_PGS
:
477 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
;
485 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
;
492 static inline int core_alua_state_transition(
498 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by
499 * spc4r17 section 5.9.2.5
505 switch (cdb
[1] & 0x1f) {
506 case MI_REPORT_TARGET_PGS
:
509 *alua_ascq
= ASCQ_04H_ALUA_STATE_TRANSITION
;
517 *alua_ascq
= ASCQ_04H_ALUA_STATE_TRANSITION
;
525 * Used for alua_type SPC_ALUA_PASSTHROUGH and SPC2_ALUA_DISABLED
526 * in transport_cmd_sequencer(). This function is assigned to
527 * struct t10_alua *->state_check() in core_setup_alua()
529 static int core_alua_state_check_nop(
538 * Used for alua_type SPC3_ALUA_EMULATED in transport_cmd_sequencer().
539 * This function is assigned to struct t10_alua *->state_check() in
542 * Also, this function can return three different return codes to
543 * signal transport_generic_cmd_sequencer()
545 * return 1: Is used to signal LUN not accecsable, and check condition/not ready
546 * return 0: Used to signal success
547 * reutrn -1: Used to signal failure, and invalid cdb field
549 static int core_alua_state_check(
554 struct se_lun
*lun
= cmd
->se_lun
;
555 struct se_port
*port
= lun
->lun_sep
;
556 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
557 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
558 int out_alua_state
, nonop_delay_msecs
;
563 * First, check for a struct se_port specific secondary ALUA target port
564 * access state: OFFLINE
566 if (atomic_read(&port
->sep_tg_pt_secondary_offline
)) {
567 *alua_ascq
= ASCQ_04H_ALUA_OFFLINE
;
568 pr_debug("ALUA: Got secondary offline status for local"
570 *alua_ascq
= ASCQ_04H_ALUA_OFFLINE
;
574 * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
575 * ALUA target port group, to obtain current ALUA access state.
576 * Otherwise look for the underlying struct se_device association with
577 * a ALUA logical unit group.
579 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
580 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
581 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
582 out_alua_state
= atomic_read(&tg_pt_gp
->tg_pt_gp_alua_access_state
);
583 nonop_delay_msecs
= tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
;
584 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
586 * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a separate conditional
587 * statement so the compiler knows explicitly to check this case first.
588 * For the Optimized ALUA access state case, we want to process the
589 * incoming fabric cmd ASAP..
591 if (out_alua_state
== ALUA_ACCESS_STATE_ACTIVE_OPTMIZED
)
594 switch (out_alua_state
) {
595 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
596 return core_alua_state_nonoptimized(cmd
, cdb
,
597 nonop_delay_msecs
, alua_ascq
);
598 case ALUA_ACCESS_STATE_STANDBY
:
599 return core_alua_state_standby(cmd
, cdb
, alua_ascq
);
600 case ALUA_ACCESS_STATE_UNAVAILABLE
:
601 return core_alua_state_unavailable(cmd
, cdb
, alua_ascq
);
602 case ALUA_ACCESS_STATE_TRANSITION
:
603 return core_alua_state_transition(cmd
, cdb
, alua_ascq
);
605 * OFFLINE is a secondary ALUA target port group access state, that is
606 * handled above with struct se_port->sep_tg_pt_secondary_offline=1
608 case ALUA_ACCESS_STATE_OFFLINE
:
610 pr_err("Unknown ALUA access state: 0x%02x\n",
619 * Check implict and explict ALUA state change request.
621 static int core_alua_check_transition(int state
, int *primary
)
624 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED
:
625 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
626 case ALUA_ACCESS_STATE_STANDBY
:
627 case ALUA_ACCESS_STATE_UNAVAILABLE
:
629 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
630 * defined as primary target port asymmetric access states.
634 case ALUA_ACCESS_STATE_OFFLINE
:
636 * OFFLINE state is defined as a secondary target port
637 * asymmetric access state.
642 pr_err("Unknown ALUA access state: 0x%02x\n", state
);
649 static char *core_alua_dump_state(int state
)
652 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED
:
653 return "Active/Optimized";
654 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
655 return "Active/NonOptimized";
656 case ALUA_ACCESS_STATE_STANDBY
:
658 case ALUA_ACCESS_STATE_UNAVAILABLE
:
659 return "Unavailable";
660 case ALUA_ACCESS_STATE_OFFLINE
:
669 char *core_alua_dump_status(int status
)
672 case ALUA_STATUS_NONE
:
674 case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG
:
675 return "Altered by Explict STPG";
676 case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA
:
677 return "Altered by Implict ALUA";
686 * Used by fabric modules to determine when we need to delay processing
687 * for the Active/NonOptimized paths..
689 int core_alua_check_nonop_delay(
692 if (!(cmd
->se_cmd_flags
& SCF_ALUA_NON_OPTIMIZED
))
697 * The ALUA Active/NonOptimized access state delay can be disabled
698 * in via configfs with a value of zero
700 if (!cmd
->alua_nonop_delay
)
703 * struct se_cmd->alua_nonop_delay gets set by a target port group
704 * defined interval in core_alua_state_nonoptimized()
706 msleep_interruptible(cmd
->alua_nonop_delay
);
709 EXPORT_SYMBOL(core_alua_check_nonop_delay
);
712 * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
715 static int core_alua_write_tpg_metadata(
717 unsigned char *md_buf
,
723 int flags
= O_RDWR
| O_CREAT
| O_TRUNC
, ret
;
725 memset(iov
, 0, sizeof(struct iovec
));
727 file
= filp_open(path
, flags
, 0600);
728 if (IS_ERR(file
) || !file
|| !file
->f_dentry
) {
729 pr_err("filp_open(%s) for ALUA metadata failed\n",
734 iov
[0].iov_base
= &md_buf
[0];
735 iov
[0].iov_len
= md_buf_len
;
739 ret
= vfs_writev(file
, &iov
[0], 1, &file
->f_pos
);
743 pr_err("Error writing ALUA metadata file: %s\n", path
);
744 filp_close(file
, NULL
);
747 filp_close(file
, NULL
);
753 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
755 static int core_alua_update_tpg_primary_metadata(
756 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
758 unsigned char *md_buf
)
760 struct t10_wwn
*wwn
= &tg_pt_gp
->tg_pt_gp_dev
->t10_wwn
;
761 char path
[ALUA_METADATA_PATH_LEN
];
764 memset(path
, 0, ALUA_METADATA_PATH_LEN
);
766 len
= snprintf(md_buf
, tg_pt_gp
->tg_pt_gp_md_buf_len
,
768 "alua_access_state=0x%02x\n"
769 "alua_access_status=0x%02x\n",
770 tg_pt_gp
->tg_pt_gp_id
, primary_state
,
771 tg_pt_gp
->tg_pt_gp_alua_access_status
);
773 snprintf(path
, ALUA_METADATA_PATH_LEN
,
774 "/var/target/alua/tpgs_%s/%s", &wwn
->unit_serial
[0],
775 config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
));
777 return core_alua_write_tpg_metadata(path
, md_buf
, len
);
780 static int core_alua_do_transition_tg_pt(
781 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
782 struct se_port
*l_port
,
783 struct se_node_acl
*nacl
,
784 unsigned char *md_buf
,
788 struct se_dev_entry
*se_deve
;
789 struct se_lun_acl
*lacl
;
790 struct se_port
*port
;
791 struct t10_alua_tg_pt_gp_member
*mem
;
794 * Save the old primary ALUA access state, and set the current state
795 * to ALUA_ACCESS_STATE_TRANSITION.
797 old_state
= atomic_read(&tg_pt_gp
->tg_pt_gp_alua_access_state
);
798 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
,
799 ALUA_ACCESS_STATE_TRANSITION
);
800 tg_pt_gp
->tg_pt_gp_alua_access_status
= (explict
) ?
801 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG
:
802 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA
;
804 * Check for the optional ALUA primary state transition delay
806 if (tg_pt_gp
->tg_pt_gp_trans_delay_msecs
!= 0)
807 msleep_interruptible(tg_pt_gp
->tg_pt_gp_trans_delay_msecs
);
809 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
810 list_for_each_entry(mem
, &tg_pt_gp
->tg_pt_gp_mem_list
,
814 * After an implicit target port asymmetric access state
815 * change, a device server shall establish a unit attention
816 * condition for the initiator port associated with every I_T
817 * nexus with the additional sense code set to ASYMMETRIC
818 * ACCESS STATE CHAGED.
820 * After an explicit target port asymmetric access state
821 * change, a device server shall establish a unit attention
822 * condition with the additional sense code set to ASYMMETRIC
823 * ACCESS STATE CHANGED for the initiator port associated with
824 * every I_T nexus other than the I_T nexus on which the SET
825 * TARGET PORT GROUPS command
827 atomic_inc(&mem
->tg_pt_gp_mem_ref_cnt
);
828 smp_mb__after_atomic_inc();
829 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
831 spin_lock_bh(&port
->sep_alua_lock
);
832 list_for_each_entry(se_deve
, &port
->sep_alua_list
,
834 lacl
= se_deve
->se_lun_acl
;
836 * se_deve->se_lun_acl pointer may be NULL for a
837 * entry created without explict Node+MappedLUN ACLs
843 (nacl
!= NULL
) && (nacl
== lacl
->se_lun_nacl
) &&
844 (l_port
!= NULL
) && (l_port
== port
))
847 core_scsi3_ua_allocate(lacl
->se_lun_nacl
,
848 se_deve
->mapped_lun
, 0x2A,
849 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED
);
851 spin_unlock_bh(&port
->sep_alua_lock
);
853 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
854 atomic_dec(&mem
->tg_pt_gp_mem_ref_cnt
);
855 smp_mb__after_atomic_dec();
857 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
859 * Update the ALUA metadata buf that has been allocated in
860 * core_alua_do_port_transition(), this metadata will be written
863 * Note that there is the case where we do not want to update the
864 * metadata when the saved metadata is being parsed in userspace
865 * when setting the existing port access state and access status.
867 * Also note that the failure to write out the ALUA metadata to
868 * struct file does NOT affect the actual ALUA transition.
870 if (tg_pt_gp
->tg_pt_gp_write_metadata
) {
871 mutex_lock(&tg_pt_gp
->tg_pt_gp_md_mutex
);
872 core_alua_update_tpg_primary_metadata(tg_pt_gp
,
874 mutex_unlock(&tg_pt_gp
->tg_pt_gp_md_mutex
);
877 * Set the current primary ALUA access state to the requested new state
879 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
, new_state
);
881 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
882 " from primary access state %s to %s\n", (explict
) ? "explict" :
883 "implict", config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
),
884 tg_pt_gp
->tg_pt_gp_id
, core_alua_dump_state(old_state
),
885 core_alua_dump_state(new_state
));
890 int core_alua_do_port_transition(
891 struct t10_alua_tg_pt_gp
*l_tg_pt_gp
,
892 struct se_device
*l_dev
,
893 struct se_port
*l_port
,
894 struct se_node_acl
*l_nacl
,
898 struct se_device
*dev
;
899 struct se_port
*port
;
900 struct se_node_acl
*nacl
;
901 struct t10_alua_lu_gp
*lu_gp
;
902 struct t10_alua_lu_gp_member
*lu_gp_mem
, *local_lu_gp_mem
;
903 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
904 unsigned char *md_buf
;
907 if (core_alua_check_transition(new_state
, &primary
) != 0)
910 md_buf
= kzalloc(l_tg_pt_gp
->tg_pt_gp_md_buf_len
, GFP_KERNEL
);
912 pr_err("Unable to allocate buf for ALUA metadata\n");
916 local_lu_gp_mem
= l_dev
->dev_alua_lu_gp_mem
;
917 spin_lock(&local_lu_gp_mem
->lu_gp_mem_lock
);
918 lu_gp
= local_lu_gp_mem
->lu_gp
;
919 atomic_inc(&lu_gp
->lu_gp_ref_cnt
);
920 smp_mb__after_atomic_inc();
921 spin_unlock(&local_lu_gp_mem
->lu_gp_mem_lock
);
923 * For storage objects that are members of the 'default_lu_gp',
924 * we only do transition on the passed *l_tp_pt_gp, and not
925 * on all of the matching target port groups IDs in default_lu_gp.
927 if (!lu_gp
->lu_gp_id
) {
929 * core_alua_do_transition_tg_pt() will always return
932 core_alua_do_transition_tg_pt(l_tg_pt_gp
, l_port
, l_nacl
,
933 md_buf
, new_state
, explict
);
934 atomic_dec(&lu_gp
->lu_gp_ref_cnt
);
935 smp_mb__after_atomic_dec();
940 * For all other LU groups aside from 'default_lu_gp', walk all of
941 * the associated storage objects looking for a matching target port
942 * group ID from the local target port group.
944 spin_lock(&lu_gp
->lu_gp_lock
);
945 list_for_each_entry(lu_gp_mem
, &lu_gp
->lu_gp_mem_list
,
948 dev
= lu_gp_mem
->lu_gp_mem_dev
;
949 atomic_inc(&lu_gp_mem
->lu_gp_mem_ref_cnt
);
950 smp_mb__after_atomic_inc();
951 spin_unlock(&lu_gp
->lu_gp_lock
);
953 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
954 list_for_each_entry(tg_pt_gp
,
955 &dev
->t10_alua
.tg_pt_gps_list
,
958 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
961 * If the target behavior port asymmetric access state
962 * is changed for any target port group accessiable via
963 * a logical unit within a LU group, the target port
964 * behavior group asymmetric access states for the same
965 * target port group accessible via other logical units
966 * in that LU group will also change.
968 if (l_tg_pt_gp
->tg_pt_gp_id
!= tg_pt_gp
->tg_pt_gp_id
)
971 if (l_tg_pt_gp
== tg_pt_gp
) {
978 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
979 smp_mb__after_atomic_inc();
980 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
982 * core_alua_do_transition_tg_pt() will always return
985 core_alua_do_transition_tg_pt(tg_pt_gp
, port
,
986 nacl
, md_buf
, new_state
, explict
);
988 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
989 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
990 smp_mb__after_atomic_dec();
992 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
994 spin_lock(&lu_gp
->lu_gp_lock
);
995 atomic_dec(&lu_gp_mem
->lu_gp_mem_ref_cnt
);
996 smp_mb__after_atomic_dec();
998 spin_unlock(&lu_gp
->lu_gp_lock
);
1000 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
1001 " Group IDs: %hu %s transition to primary state: %s\n",
1002 config_item_name(&lu_gp
->lu_gp_group
.cg_item
),
1003 l_tg_pt_gp
->tg_pt_gp_id
, (explict
) ? "explict" : "implict",
1004 core_alua_dump_state(new_state
));
1006 atomic_dec(&lu_gp
->lu_gp_ref_cnt
);
1007 smp_mb__after_atomic_dec();
1013 * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
1015 static int core_alua_update_tpg_secondary_metadata(
1016 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
1017 struct se_port
*port
,
1018 unsigned char *md_buf
,
1021 struct se_portal_group
*se_tpg
= port
->sep_tpg
;
1022 char path
[ALUA_METADATA_PATH_LEN
], wwn
[ALUA_SECONDARY_METADATA_WWN_LEN
];
1025 memset(path
, 0, ALUA_METADATA_PATH_LEN
);
1026 memset(wwn
, 0, ALUA_SECONDARY_METADATA_WWN_LEN
);
1028 len
= snprintf(wwn
, ALUA_SECONDARY_METADATA_WWN_LEN
, "%s",
1029 se_tpg
->se_tpg_tfo
->tpg_get_wwn(se_tpg
));
1031 if (se_tpg
->se_tpg_tfo
->tpg_get_tag
!= NULL
)
1032 snprintf(wwn
+len
, ALUA_SECONDARY_METADATA_WWN_LEN
-len
, "+%hu",
1033 se_tpg
->se_tpg_tfo
->tpg_get_tag(se_tpg
));
1035 len
= snprintf(md_buf
, md_buf_len
, "alua_tg_pt_offline=%d\n"
1036 "alua_tg_pt_status=0x%02x\n",
1037 atomic_read(&port
->sep_tg_pt_secondary_offline
),
1038 port
->sep_tg_pt_secondary_stat
);
1040 snprintf(path
, ALUA_METADATA_PATH_LEN
, "/var/target/alua/%s/%s/lun_%u",
1041 se_tpg
->se_tpg_tfo
->get_fabric_name(), wwn
,
1042 port
->sep_lun
->unpacked_lun
);
1044 return core_alua_write_tpg_metadata(path
, md_buf
, len
);
1047 static int core_alua_set_tg_pt_secondary_state(
1048 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
1049 struct se_port
*port
,
1053 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1054 unsigned char *md_buf
;
1056 int trans_delay_msecs
;
1058 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1059 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
1061 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1062 pr_err("Unable to complete secondary state"
1066 trans_delay_msecs
= tg_pt_gp
->tg_pt_gp_trans_delay_msecs
;
1068 * Set the secondary ALUA target port access state to OFFLINE
1069 * or release the previously secondary state for struct se_port
1072 atomic_set(&port
->sep_tg_pt_secondary_offline
, 1);
1074 atomic_set(&port
->sep_tg_pt_secondary_offline
, 0);
1076 md_buf_len
= tg_pt_gp
->tg_pt_gp_md_buf_len
;
1077 port
->sep_tg_pt_secondary_stat
= (explict
) ?
1078 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG
:
1079 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA
;
1081 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1082 " to secondary access state: %s\n", (explict
) ? "explict" :
1083 "implict", config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1084 tg_pt_gp
->tg_pt_gp_id
, (offline
) ? "OFFLINE" : "ONLINE");
1086 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1088 * Do the optional transition delay after we set the secondary
1089 * ALUA access state.
1091 if (trans_delay_msecs
!= 0)
1092 msleep_interruptible(trans_delay_msecs
);
1094 * See if we need to update the ALUA fabric port metadata for
1095 * secondary state and status
1097 if (port
->sep_tg_pt_secondary_write_md
) {
1098 md_buf
= kzalloc(md_buf_len
, GFP_KERNEL
);
1100 pr_err("Unable to allocate md_buf for"
1101 " secondary ALUA access metadata\n");
1104 mutex_lock(&port
->sep_tg_pt_md_mutex
);
1105 core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem
, port
,
1106 md_buf
, md_buf_len
);
1107 mutex_unlock(&port
->sep_tg_pt_md_mutex
);
1115 struct t10_alua_lu_gp
*
1116 core_alua_allocate_lu_gp(const char *name
, int def_group
)
1118 struct t10_alua_lu_gp
*lu_gp
;
1120 lu_gp
= kmem_cache_zalloc(t10_alua_lu_gp_cache
, GFP_KERNEL
);
1122 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1123 return ERR_PTR(-ENOMEM
);
1125 INIT_LIST_HEAD(&lu_gp
->lu_gp_node
);
1126 INIT_LIST_HEAD(&lu_gp
->lu_gp_mem_list
);
1127 spin_lock_init(&lu_gp
->lu_gp_lock
);
1128 atomic_set(&lu_gp
->lu_gp_ref_cnt
, 0);
1131 lu_gp
->lu_gp_id
= alua_lu_gps_counter
++;
1132 lu_gp
->lu_gp_valid_id
= 1;
1133 alua_lu_gps_count
++;
1139 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp
*lu_gp
, u16 lu_gp_id
)
1141 struct t10_alua_lu_gp
*lu_gp_tmp
;
1144 * The lu_gp->lu_gp_id may only be set once..
1146 if (lu_gp
->lu_gp_valid_id
) {
1147 pr_warn("ALUA LU Group already has a valid ID,"
1148 " ignoring request\n");
1152 spin_lock(&lu_gps_lock
);
1153 if (alua_lu_gps_count
== 0x0000ffff) {
1154 pr_err("Maximum ALUA alua_lu_gps_count:"
1155 " 0x0000ffff reached\n");
1156 spin_unlock(&lu_gps_lock
);
1157 kmem_cache_free(t10_alua_lu_gp_cache
, lu_gp
);
1161 lu_gp_id_tmp
= (lu_gp_id
!= 0) ? lu_gp_id
:
1162 alua_lu_gps_counter
++;
1164 list_for_each_entry(lu_gp_tmp
, &lu_gps_list
, lu_gp_node
) {
1165 if (lu_gp_tmp
->lu_gp_id
== lu_gp_id_tmp
) {
1169 pr_warn("ALUA Logical Unit Group ID: %hu"
1170 " already exists, ignoring request\n",
1172 spin_unlock(&lu_gps_lock
);
1177 lu_gp
->lu_gp_id
= lu_gp_id_tmp
;
1178 lu_gp
->lu_gp_valid_id
= 1;
1179 list_add_tail(&lu_gp
->lu_gp_node
, &lu_gps_list
);
1180 alua_lu_gps_count
++;
1181 spin_unlock(&lu_gps_lock
);
1186 static struct t10_alua_lu_gp_member
*
1187 core_alua_allocate_lu_gp_mem(struct se_device
*dev
)
1189 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1191 lu_gp_mem
= kmem_cache_zalloc(t10_alua_lu_gp_mem_cache
, GFP_KERNEL
);
1193 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1194 return ERR_PTR(-ENOMEM
);
1196 INIT_LIST_HEAD(&lu_gp_mem
->lu_gp_mem_list
);
1197 spin_lock_init(&lu_gp_mem
->lu_gp_mem_lock
);
1198 atomic_set(&lu_gp_mem
->lu_gp_mem_ref_cnt
, 0);
1200 lu_gp_mem
->lu_gp_mem_dev
= dev
;
1201 dev
->dev_alua_lu_gp_mem
= lu_gp_mem
;
1206 void core_alua_free_lu_gp(struct t10_alua_lu_gp
*lu_gp
)
1208 struct t10_alua_lu_gp_member
*lu_gp_mem
, *lu_gp_mem_tmp
;
1210 * Once we have reached this point, config_item_put() has
1211 * already been called from target_core_alua_drop_lu_gp().
1213 * Here, we remove the *lu_gp from the global list so that
1214 * no associations can be made while we are releasing
1215 * struct t10_alua_lu_gp.
1217 spin_lock(&lu_gps_lock
);
1218 list_del(&lu_gp
->lu_gp_node
);
1219 alua_lu_gps_count
--;
1220 spin_unlock(&lu_gps_lock
);
1222 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1223 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1224 * released with core_alua_put_lu_gp_from_name()
1226 while (atomic_read(&lu_gp
->lu_gp_ref_cnt
))
1229 * Release reference to struct t10_alua_lu_gp * from all associated
1232 spin_lock(&lu_gp
->lu_gp_lock
);
1233 list_for_each_entry_safe(lu_gp_mem
, lu_gp_mem_tmp
,
1234 &lu_gp
->lu_gp_mem_list
, lu_gp_mem_list
) {
1235 if (lu_gp_mem
->lu_gp_assoc
) {
1236 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1237 lu_gp
->lu_gp_members
--;
1238 lu_gp_mem
->lu_gp_assoc
= 0;
1240 spin_unlock(&lu_gp
->lu_gp_lock
);
1243 * lu_gp_mem is associated with a single
1244 * struct se_device->dev_alua_lu_gp_mem, and is released when
1245 * struct se_device is released via core_alua_free_lu_gp_mem().
1247 * If the passed lu_gp does NOT match the default_lu_gp, assume
1248 * we want to re-assocate a given lu_gp_mem with default_lu_gp.
1250 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1251 if (lu_gp
!= default_lu_gp
)
1252 __core_alua_attach_lu_gp_mem(lu_gp_mem
,
1255 lu_gp_mem
->lu_gp
= NULL
;
1256 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1258 spin_lock(&lu_gp
->lu_gp_lock
);
1260 spin_unlock(&lu_gp
->lu_gp_lock
);
1262 kmem_cache_free(t10_alua_lu_gp_cache
, lu_gp
);
1265 void core_alua_free_lu_gp_mem(struct se_device
*dev
)
1267 struct t10_alua
*alua
= &dev
->t10_alua
;
1268 struct t10_alua_lu_gp
*lu_gp
;
1269 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1271 if (alua
->alua_type
!= SPC3_ALUA_EMULATED
)
1274 lu_gp_mem
= dev
->dev_alua_lu_gp_mem
;
1278 while (atomic_read(&lu_gp_mem
->lu_gp_mem_ref_cnt
))
1281 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1282 lu_gp
= lu_gp_mem
->lu_gp
;
1284 spin_lock(&lu_gp
->lu_gp_lock
);
1285 if (lu_gp_mem
->lu_gp_assoc
) {
1286 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1287 lu_gp
->lu_gp_members
--;
1288 lu_gp_mem
->lu_gp_assoc
= 0;
1290 spin_unlock(&lu_gp
->lu_gp_lock
);
1291 lu_gp_mem
->lu_gp
= NULL
;
1293 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1295 kmem_cache_free(t10_alua_lu_gp_mem_cache
, lu_gp_mem
);
1298 struct t10_alua_lu_gp
*core_alua_get_lu_gp_by_name(const char *name
)
1300 struct t10_alua_lu_gp
*lu_gp
;
1301 struct config_item
*ci
;
1303 spin_lock(&lu_gps_lock
);
1304 list_for_each_entry(lu_gp
, &lu_gps_list
, lu_gp_node
) {
1305 if (!lu_gp
->lu_gp_valid_id
)
1307 ci
= &lu_gp
->lu_gp_group
.cg_item
;
1308 if (!strcmp(config_item_name(ci
), name
)) {
1309 atomic_inc(&lu_gp
->lu_gp_ref_cnt
);
1310 spin_unlock(&lu_gps_lock
);
1314 spin_unlock(&lu_gps_lock
);
1319 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp
*lu_gp
)
1321 spin_lock(&lu_gps_lock
);
1322 atomic_dec(&lu_gp
->lu_gp_ref_cnt
);
1323 spin_unlock(&lu_gps_lock
);
1327 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1329 void __core_alua_attach_lu_gp_mem(
1330 struct t10_alua_lu_gp_member
*lu_gp_mem
,
1331 struct t10_alua_lu_gp
*lu_gp
)
1333 spin_lock(&lu_gp
->lu_gp_lock
);
1334 lu_gp_mem
->lu_gp
= lu_gp
;
1335 lu_gp_mem
->lu_gp_assoc
= 1;
1336 list_add_tail(&lu_gp_mem
->lu_gp_mem_list
, &lu_gp
->lu_gp_mem_list
);
1337 lu_gp
->lu_gp_members
++;
1338 spin_unlock(&lu_gp
->lu_gp_lock
);
1342 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1344 void __core_alua_drop_lu_gp_mem(
1345 struct t10_alua_lu_gp_member
*lu_gp_mem
,
1346 struct t10_alua_lu_gp
*lu_gp
)
1348 spin_lock(&lu_gp
->lu_gp_lock
);
1349 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1350 lu_gp_mem
->lu_gp
= NULL
;
1351 lu_gp_mem
->lu_gp_assoc
= 0;
1352 lu_gp
->lu_gp_members
--;
1353 spin_unlock(&lu_gp
->lu_gp_lock
);
1356 struct t10_alua_tg_pt_gp
*core_alua_allocate_tg_pt_gp(struct se_device
*dev
,
1357 const char *name
, int def_group
)
1359 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1361 tg_pt_gp
= kmem_cache_zalloc(t10_alua_tg_pt_gp_cache
, GFP_KERNEL
);
1363 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1366 INIT_LIST_HEAD(&tg_pt_gp
->tg_pt_gp_list
);
1367 INIT_LIST_HEAD(&tg_pt_gp
->tg_pt_gp_mem_list
);
1368 mutex_init(&tg_pt_gp
->tg_pt_gp_md_mutex
);
1369 spin_lock_init(&tg_pt_gp
->tg_pt_gp_lock
);
1370 atomic_set(&tg_pt_gp
->tg_pt_gp_ref_cnt
, 0);
1371 tg_pt_gp
->tg_pt_gp_dev
= dev
;
1372 tg_pt_gp
->tg_pt_gp_md_buf_len
= ALUA_MD_BUF_LEN
;
1373 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
,
1374 ALUA_ACCESS_STATE_ACTIVE_OPTMIZED
);
1376 * Enable both explict and implict ALUA support by default
1378 tg_pt_gp
->tg_pt_gp_alua_access_type
=
1379 TPGS_EXPLICT_ALUA
| TPGS_IMPLICT_ALUA
;
1381 * Set the default Active/NonOptimized Delay in milliseconds
1383 tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
= ALUA_DEFAULT_NONOP_DELAY_MSECS
;
1384 tg_pt_gp
->tg_pt_gp_trans_delay_msecs
= ALUA_DEFAULT_TRANS_DELAY_MSECS
;
1385 tg_pt_gp
->tg_pt_gp_implict_trans_secs
= ALUA_DEFAULT_IMPLICT_TRANS_SECS
;
1388 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1389 tg_pt_gp
->tg_pt_gp_id
=
1390 dev
->t10_alua
.alua_tg_pt_gps_counter
++;
1391 tg_pt_gp
->tg_pt_gp_valid_id
= 1;
1392 dev
->t10_alua
.alua_tg_pt_gps_count
++;
1393 list_add_tail(&tg_pt_gp
->tg_pt_gp_list
,
1394 &dev
->t10_alua
.tg_pt_gps_list
);
1395 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1401 int core_alua_set_tg_pt_gp_id(
1402 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1405 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1406 struct t10_alua_tg_pt_gp
*tg_pt_gp_tmp
;
1407 u16 tg_pt_gp_id_tmp
;
1410 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1412 if (tg_pt_gp
->tg_pt_gp_valid_id
) {
1413 pr_warn("ALUA TG PT Group already has a valid ID,"
1414 " ignoring request\n");
1418 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1419 if (dev
->t10_alua
.alua_tg_pt_gps_count
== 0x0000ffff) {
1420 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1421 " 0x0000ffff reached\n");
1422 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1423 kmem_cache_free(t10_alua_tg_pt_gp_cache
, tg_pt_gp
);
1427 tg_pt_gp_id_tmp
= (tg_pt_gp_id
!= 0) ? tg_pt_gp_id
:
1428 dev
->t10_alua
.alua_tg_pt_gps_counter
++;
1430 list_for_each_entry(tg_pt_gp_tmp
, &dev
->t10_alua
.tg_pt_gps_list
,
1432 if (tg_pt_gp_tmp
->tg_pt_gp_id
== tg_pt_gp_id_tmp
) {
1436 pr_err("ALUA Target Port Group ID: %hu already"
1437 " exists, ignoring request\n", tg_pt_gp_id
);
1438 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1443 tg_pt_gp
->tg_pt_gp_id
= tg_pt_gp_id_tmp
;
1444 tg_pt_gp
->tg_pt_gp_valid_id
= 1;
1445 list_add_tail(&tg_pt_gp
->tg_pt_gp_list
,
1446 &dev
->t10_alua
.tg_pt_gps_list
);
1447 dev
->t10_alua
.alua_tg_pt_gps_count
++;
1448 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1453 struct t10_alua_tg_pt_gp_member
*core_alua_allocate_tg_pt_gp_mem(
1454 struct se_port
*port
)
1456 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1458 tg_pt_gp_mem
= kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache
,
1460 if (!tg_pt_gp_mem
) {
1461 pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n");
1462 return ERR_PTR(-ENOMEM
);
1464 INIT_LIST_HEAD(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1465 spin_lock_init(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1466 atomic_set(&tg_pt_gp_mem
->tg_pt_gp_mem_ref_cnt
, 0);
1468 tg_pt_gp_mem
->tg_pt
= port
;
1469 port
->sep_alua_tg_pt_gp_mem
= tg_pt_gp_mem
;
1471 return tg_pt_gp_mem
;
1474 void core_alua_free_tg_pt_gp(
1475 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1477 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1478 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
, *tg_pt_gp_mem_tmp
;
1481 * Once we have reached this point, config_item_put() has already
1482 * been called from target_core_alua_drop_tg_pt_gp().
1484 * Here we remove *tg_pt_gp from the global list so that
1485 * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS
1486 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1488 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1489 list_del(&tg_pt_gp
->tg_pt_gp_list
);
1490 dev
->t10_alua
.alua_tg_pt_gps_counter
--;
1491 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1494 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1495 * core_alua_get_tg_pt_gp_by_name() in
1496 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1497 * to be released with core_alua_put_tg_pt_gp_from_name().
1499 while (atomic_read(&tg_pt_gp
->tg_pt_gp_ref_cnt
))
1503 * Release reference to struct t10_alua_tg_pt_gp from all associated
1506 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1507 list_for_each_entry_safe(tg_pt_gp_mem
, tg_pt_gp_mem_tmp
,
1508 &tg_pt_gp
->tg_pt_gp_mem_list
, tg_pt_gp_mem_list
) {
1509 if (tg_pt_gp_mem
->tg_pt_gp_assoc
) {
1510 list_del(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1511 tg_pt_gp
->tg_pt_gp_members
--;
1512 tg_pt_gp_mem
->tg_pt_gp_assoc
= 0;
1514 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1516 * tg_pt_gp_mem is associated with a single
1517 * se_port->sep_alua_tg_pt_gp_mem, and is released via
1518 * core_alua_free_tg_pt_gp_mem().
1520 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1521 * assume we want to re-assocate a given tg_pt_gp_mem with
1524 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1525 if (tg_pt_gp
!= dev
->t10_alua
.default_tg_pt_gp
) {
1526 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem
,
1527 dev
->t10_alua
.default_tg_pt_gp
);
1529 tg_pt_gp_mem
->tg_pt_gp
= NULL
;
1530 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1532 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1534 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1536 kmem_cache_free(t10_alua_tg_pt_gp_cache
, tg_pt_gp
);
1539 void core_alua_free_tg_pt_gp_mem(struct se_port
*port
)
1541 struct t10_alua
*alua
= &port
->sep_lun
->lun_se_dev
->t10_alua
;
1542 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1543 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1545 if (alua
->alua_type
!= SPC3_ALUA_EMULATED
)
1548 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
1552 while (atomic_read(&tg_pt_gp_mem
->tg_pt_gp_mem_ref_cnt
))
1555 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1556 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
1558 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1559 if (tg_pt_gp_mem
->tg_pt_gp_assoc
) {
1560 list_del(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1561 tg_pt_gp
->tg_pt_gp_members
--;
1562 tg_pt_gp_mem
->tg_pt_gp_assoc
= 0;
1564 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1565 tg_pt_gp_mem
->tg_pt_gp
= NULL
;
1567 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1569 kmem_cache_free(t10_alua_tg_pt_gp_mem_cache
, tg_pt_gp_mem
);
1572 static struct t10_alua_tg_pt_gp
*core_alua_get_tg_pt_gp_by_name(
1573 struct se_device
*dev
, const char *name
)
1575 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1576 struct config_item
*ci
;
1578 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1579 list_for_each_entry(tg_pt_gp
, &dev
->t10_alua
.tg_pt_gps_list
,
1581 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
1583 ci
= &tg_pt_gp
->tg_pt_gp_group
.cg_item
;
1584 if (!strcmp(config_item_name(ci
), name
)) {
1585 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1586 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1590 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1595 static void core_alua_put_tg_pt_gp_from_name(
1596 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1598 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1600 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1601 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1602 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1606 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1608 void __core_alua_attach_tg_pt_gp_mem(
1609 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
1610 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1612 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1613 tg_pt_gp_mem
->tg_pt_gp
= tg_pt_gp
;
1614 tg_pt_gp_mem
->tg_pt_gp_assoc
= 1;
1615 list_add_tail(&tg_pt_gp_mem
->tg_pt_gp_mem_list
,
1616 &tg_pt_gp
->tg_pt_gp_mem_list
);
1617 tg_pt_gp
->tg_pt_gp_members
++;
1618 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1622 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1624 static void __core_alua_drop_tg_pt_gp_mem(
1625 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
1626 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1628 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1629 list_del(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1630 tg_pt_gp_mem
->tg_pt_gp
= NULL
;
1631 tg_pt_gp_mem
->tg_pt_gp_assoc
= 0;
1632 tg_pt_gp
->tg_pt_gp_members
--;
1633 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1636 ssize_t
core_alua_show_tg_pt_gp_info(struct se_port
*port
, char *page
)
1638 struct config_item
*tg_pt_ci
;
1639 struct t10_alua
*alua
= &port
->sep_lun
->lun_se_dev
->t10_alua
;
1640 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1641 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1644 if (alua
->alua_type
!= SPC3_ALUA_EMULATED
)
1647 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
1651 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1652 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
1654 tg_pt_ci
= &tg_pt_gp
->tg_pt_gp_group
.cg_item
;
1655 len
+= sprintf(page
, "TG Port Alias: %s\nTG Port Group ID:"
1656 " %hu\nTG Port Primary Access State: %s\nTG Port "
1657 "Primary Access Status: %s\nTG Port Secondary Access"
1658 " State: %s\nTG Port Secondary Access Status: %s\n",
1659 config_item_name(tg_pt_ci
), tg_pt_gp
->tg_pt_gp_id
,
1660 core_alua_dump_state(atomic_read(
1661 &tg_pt_gp
->tg_pt_gp_alua_access_state
)),
1662 core_alua_dump_status(
1663 tg_pt_gp
->tg_pt_gp_alua_access_status
),
1664 (atomic_read(&port
->sep_tg_pt_secondary_offline
)) ?
1666 core_alua_dump_status(port
->sep_tg_pt_secondary_stat
));
1668 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1673 ssize_t
core_alua_store_tg_pt_gp_info(
1674 struct se_port
*port
,
1678 struct se_portal_group
*tpg
;
1680 struct se_device
*dev
= port
->sep_lun
->lun_se_dev
;
1681 struct t10_alua_tg_pt_gp
*tg_pt_gp
= NULL
, *tg_pt_gp_new
= NULL
;
1682 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1683 unsigned char buf
[TG_PT_GROUP_NAME_BUF
];
1686 tpg
= port
->sep_tpg
;
1687 lun
= port
->sep_lun
;
1689 if (dev
->t10_alua
.alua_type
!= SPC3_ALUA_EMULATED
) {
1690 pr_warn("SPC3_ALUA_EMULATED not enabled for"
1691 " %s/tpgt_%hu/%s\n", tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1692 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1693 config_item_name(&lun
->lun_group
.cg_item
));
1697 if (count
> TG_PT_GROUP_NAME_BUF
) {
1698 pr_err("ALUA Target Port Group alias too large!\n");
1701 memset(buf
, 0, TG_PT_GROUP_NAME_BUF
);
1702 memcpy(buf
, page
, count
);
1704 * Any ALUA target port group alias besides "NULL" means we will be
1705 * making a new group association.
1707 if (strcmp(strstrip(buf
), "NULL")) {
1709 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1710 * struct t10_alua_tg_pt_gp. This reference is released with
1711 * core_alua_put_tg_pt_gp_from_name() below.
1713 tg_pt_gp_new
= core_alua_get_tg_pt_gp_by_name(dev
,
1718 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
1719 if (!tg_pt_gp_mem
) {
1721 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new
);
1722 pr_err("NULL struct se_port->sep_alua_tg_pt_gp_mem pointer\n");
1726 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1727 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
1730 * Clearing an existing tg_pt_gp association, and replacing
1731 * with the default_tg_pt_gp.
1733 if (!tg_pt_gp_new
) {
1734 pr_debug("Target_Core_ConfigFS: Moving"
1735 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1736 " alua/%s, ID: %hu back to"
1737 " default_tg_pt_gp\n",
1738 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1739 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1740 config_item_name(&lun
->lun_group
.cg_item
),
1742 &tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1743 tg_pt_gp
->tg_pt_gp_id
);
1745 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem
, tg_pt_gp
);
1746 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem
,
1747 dev
->t10_alua
.default_tg_pt_gp
);
1748 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1753 * Removing existing association of tg_pt_gp_mem with tg_pt_gp
1755 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem
, tg_pt_gp
);
1759 * Associate tg_pt_gp_mem with tg_pt_gp_new.
1761 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem
, tg_pt_gp_new
);
1762 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1763 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
1764 " Target Port Group: alua/%s, ID: %hu\n", (move
) ?
1765 "Moving" : "Adding", tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1766 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1767 config_item_name(&lun
->lun_group
.cg_item
),
1768 config_item_name(&tg_pt_gp_new
->tg_pt_gp_group
.cg_item
),
1769 tg_pt_gp_new
->tg_pt_gp_id
);
1771 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new
);
1775 ssize_t
core_alua_show_access_type(
1776 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1779 if ((tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICT_ALUA
) &&
1780 (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_IMPLICT_ALUA
))
1781 return sprintf(page
, "Implict and Explict\n");
1782 else if (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_IMPLICT_ALUA
)
1783 return sprintf(page
, "Implict\n");
1784 else if (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICT_ALUA
)
1785 return sprintf(page
, "Explict\n");
1787 return sprintf(page
, "None\n");
1790 ssize_t
core_alua_store_access_type(
1791 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1798 ret
= strict_strtoul(page
, 0, &tmp
);
1800 pr_err("Unable to extract alua_access_type\n");
1803 if ((tmp
!= 0) && (tmp
!= 1) && (tmp
!= 2) && (tmp
!= 3)) {
1804 pr_err("Illegal value for alua_access_type:"
1809 tg_pt_gp
->tg_pt_gp_alua_access_type
=
1810 TPGS_IMPLICT_ALUA
| TPGS_EXPLICT_ALUA
;
1812 tg_pt_gp
->tg_pt_gp_alua_access_type
= TPGS_EXPLICT_ALUA
;
1814 tg_pt_gp
->tg_pt_gp_alua_access_type
= TPGS_IMPLICT_ALUA
;
1816 tg_pt_gp
->tg_pt_gp_alua_access_type
= 0;
1821 ssize_t
core_alua_show_nonop_delay_msecs(
1822 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1825 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
);
1828 ssize_t
core_alua_store_nonop_delay_msecs(
1829 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1836 ret
= strict_strtoul(page
, 0, &tmp
);
1838 pr_err("Unable to extract nonop_delay_msecs\n");
1841 if (tmp
> ALUA_MAX_NONOP_DELAY_MSECS
) {
1842 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
1843 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp
,
1844 ALUA_MAX_NONOP_DELAY_MSECS
);
1847 tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
= (int)tmp
;
1852 ssize_t
core_alua_show_trans_delay_msecs(
1853 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1856 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_trans_delay_msecs
);
1859 ssize_t
core_alua_store_trans_delay_msecs(
1860 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1867 ret
= strict_strtoul(page
, 0, &tmp
);
1869 pr_err("Unable to extract trans_delay_msecs\n");
1872 if (tmp
> ALUA_MAX_TRANS_DELAY_MSECS
) {
1873 pr_err("Passed trans_delay_msecs: %lu, exceeds"
1874 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp
,
1875 ALUA_MAX_TRANS_DELAY_MSECS
);
1878 tg_pt_gp
->tg_pt_gp_trans_delay_msecs
= (int)tmp
;
1883 ssize_t
core_alua_show_implict_trans_secs(
1884 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1887 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_implict_trans_secs
);
1890 ssize_t
core_alua_store_implict_trans_secs(
1891 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1898 ret
= strict_strtoul(page
, 0, &tmp
);
1900 pr_err("Unable to extract implict_trans_secs\n");
1903 if (tmp
> ALUA_MAX_IMPLICT_TRANS_SECS
) {
1904 pr_err("Passed implict_trans_secs: %lu, exceeds"
1905 " ALUA_MAX_IMPLICT_TRANS_SECS: %d\n", tmp
,
1906 ALUA_MAX_IMPLICT_TRANS_SECS
);
1909 tg_pt_gp
->tg_pt_gp_implict_trans_secs
= (int)tmp
;
1914 ssize_t
core_alua_show_preferred_bit(
1915 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1918 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_pref
);
1921 ssize_t
core_alua_store_preferred_bit(
1922 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1929 ret
= strict_strtoul(page
, 0, &tmp
);
1931 pr_err("Unable to extract preferred ALUA value\n");
1934 if ((tmp
!= 0) && (tmp
!= 1)) {
1935 pr_err("Illegal value for preferred ALUA: %lu\n", tmp
);
1938 tg_pt_gp
->tg_pt_gp_pref
= (int)tmp
;
1943 ssize_t
core_alua_show_offline_bit(struct se_lun
*lun
, char *page
)
1948 return sprintf(page
, "%d\n",
1949 atomic_read(&lun
->lun_sep
->sep_tg_pt_secondary_offline
));
1952 ssize_t
core_alua_store_offline_bit(
1957 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1964 ret
= strict_strtoul(page
, 0, &tmp
);
1966 pr_err("Unable to extract alua_tg_pt_offline value\n");
1969 if ((tmp
!= 0) && (tmp
!= 1)) {
1970 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
1974 tg_pt_gp_mem
= lun
->lun_sep
->sep_alua_tg_pt_gp_mem
;
1975 if (!tg_pt_gp_mem
) {
1976 pr_err("Unable to locate *tg_pt_gp_mem\n");
1980 ret
= core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem
,
1981 lun
->lun_sep
, 0, (int)tmp
);
1988 ssize_t
core_alua_show_secondary_status(
1992 return sprintf(page
, "%d\n", lun
->lun_sep
->sep_tg_pt_secondary_stat
);
1995 ssize_t
core_alua_store_secondary_status(
2003 ret
= strict_strtoul(page
, 0, &tmp
);
2005 pr_err("Unable to extract alua_tg_pt_status\n");
2008 if ((tmp
!= ALUA_STATUS_NONE
) &&
2009 (tmp
!= ALUA_STATUS_ALTERED_BY_EXPLICT_STPG
) &&
2010 (tmp
!= ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA
)) {
2011 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
2015 lun
->lun_sep
->sep_tg_pt_secondary_stat
= (int)tmp
;
2020 ssize_t
core_alua_show_secondary_write_metadata(
2024 return sprintf(page
, "%d\n",
2025 lun
->lun_sep
->sep_tg_pt_secondary_write_md
);
2028 ssize_t
core_alua_store_secondary_write_metadata(
2036 ret
= strict_strtoul(page
, 0, &tmp
);
2038 pr_err("Unable to extract alua_tg_pt_write_md\n");
2041 if ((tmp
!= 0) && (tmp
!= 1)) {
2042 pr_err("Illegal value for alua_tg_pt_write_md:"
2046 lun
->lun_sep
->sep_tg_pt_secondary_write_md
= (int)tmp
;
2051 int core_setup_alua(struct se_device
*dev
)
2053 struct t10_alua
*alua
= &dev
->t10_alua
;
2054 struct t10_alua_lu_gp_member
*lu_gp_mem
;
2057 * If this device is from Target_Core_Mod/pSCSI, use the ALUA logic
2058 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
2059 * cause a problem because libata and some SATA RAID HBAs appear
2060 * under Linux/SCSI, but emulate SCSI logic themselves.
2062 if ((dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
) ||
2063 (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
&&
2064 !dev
->dev_attrib
.emulate_alua
)) {
2065 pr_debug("%s: Using SPC_ALUA_PASSTHROUGH, no ALUA"
2066 " emulation\n", dev
->transport
->name
);
2068 alua
->alua_type
= SPC_ALUA_PASSTHROUGH
;
2069 alua
->alua_state_check
= &core_alua_state_check_nop
;
2070 } else if (dev
->transport
->get_device_rev(dev
) >= SCSI_3
) {
2071 pr_debug("%s: Enabling ALUA Emulation for SPC-3"
2072 " device\n", dev
->transport
->name
);
2075 * Associate this struct se_device with the default ALUA
2078 lu_gp_mem
= core_alua_allocate_lu_gp_mem(dev
);
2079 if (IS_ERR(lu_gp_mem
))
2080 return PTR_ERR(lu_gp_mem
);
2082 alua
->alua_type
= SPC3_ALUA_EMULATED
;
2083 alua
->alua_state_check
= &core_alua_state_check
;
2084 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
2085 __core_alua_attach_lu_gp_mem(lu_gp_mem
,
2087 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
2089 pr_debug("%s: Adding to default ALUA LU Group:"
2090 " core/alua/lu_gps/default_lu_gp\n",
2091 dev
->transport
->name
);
2093 pr_debug("%s: Disabling ALUA Emulation for SPC-2"
2094 " device\n", dev
->transport
->name
);
2096 alua
->alua_type
= SPC2_ALUA_DISABLED
;
2097 alua
->alua_state_check
= &core_alua_state_check_nop
;