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_subsystem_dev
*su_dev
= cmd
->se_dev
->se_sub_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(&su_dev
->t10_alua
.tg_pt_gps_lock
);
90 list_for_each_entry(tg_pt_gp
, &su_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(&su_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_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
207 struct se_port
*port
, *l_port
= cmd
->se_lun
->lun_sep
;
208 struct se_node_acl
*nacl
= cmd
->se_sess
->se_node_acl
;
209 struct t10_alua_tg_pt_gp
*tg_pt_gp
= NULL
, *l_tg_pt_gp
;
210 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
, *l_tg_pt_gp_mem
;
213 u32 len
= 4; /* Skip over RESERVED area in header */
214 int alua_access_state
, primary
= 0, rc
;
218 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
221 buf
= transport_kmap_data_sg(cmd
);
224 * Determine if explict ALUA via SET_TARGET_PORT_GROUPS is allowed
225 * for the local tg_pt_gp.
227 l_tg_pt_gp_mem
= l_port
->sep_alua_tg_pt_gp_mem
;
228 if (!l_tg_pt_gp_mem
) {
229 pr_err("Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
230 cmd
->scsi_sense_reason
= TCM_UNSUPPORTED_SCSI_OPCODE
;
234 spin_lock(&l_tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
235 l_tg_pt_gp
= l_tg_pt_gp_mem
->tg_pt_gp
;
237 spin_unlock(&l_tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
238 pr_err("Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n");
239 cmd
->scsi_sense_reason
= TCM_UNSUPPORTED_SCSI_OPCODE
;
243 rc
= (l_tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICT_ALUA
);
244 spin_unlock(&l_tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
247 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
248 " while TPGS_EXPLICT_ALUA is disabled\n");
249 cmd
->scsi_sense_reason
= TCM_UNSUPPORTED_SCSI_OPCODE
;
254 ptr
= &buf
[4]; /* Skip over RESERVED area in header */
256 while (len
< cmd
->data_length
) {
257 alua_access_state
= (ptr
[0] & 0x0f);
259 * Check the received ALUA access state, and determine if
260 * the state is a primary or secondary target port asymmetric
263 rc
= core_alua_check_transition(alua_access_state
, &primary
);
266 * If the SET TARGET PORT GROUPS attempts to establish
267 * an invalid combination of target port asymmetric
268 * access states or attempts to establish an
269 * unsupported target port asymmetric access state,
270 * then the command shall be terminated with CHECK
271 * CONDITION status, with the sense key set to ILLEGAL
272 * REQUEST, and the additional sense code set to INVALID
273 * FIELD IN PARAMETER LIST.
275 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
281 * If the ASYMMETRIC ACCESS STATE field (see table 267)
282 * specifies a primary target port asymmetric access state,
283 * then the TARGET PORT GROUP OR TARGET PORT field specifies
284 * a primary target port group for which the primary target
285 * port asymmetric access state shall be changed. If the
286 * ASYMMETRIC ACCESS STATE field specifies a secondary target
287 * port asymmetric access state, then the TARGET PORT GROUP OR
288 * TARGET PORT field specifies the relative target port
289 * identifier (see 3.1.120) of the target port for which the
290 * secondary target port asymmetric access state shall be
294 tg_pt_id
= get_unaligned_be16(ptr
+ 2);
296 * Locate the matching target port group ID from
297 * the global tg_pt_gp list
299 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
300 list_for_each_entry(tg_pt_gp
,
301 &su_dev
->t10_alua
.tg_pt_gps_list
,
303 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
306 if (tg_pt_id
!= tg_pt_gp
->tg_pt_gp_id
)
309 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
310 smp_mb__after_atomic_inc();
311 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
313 rc
= core_alua_do_port_transition(tg_pt_gp
,
315 alua_access_state
, 1);
317 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
318 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
319 smp_mb__after_atomic_dec();
322 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
324 * If not matching target port group ID can be located
325 * throw an exception with ASCQ: INVALID_PARAMETER_LIST
328 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
334 * Extact the RELATIVE TARGET PORT IDENTIFIER to identify
335 * the Target Port in question for the the incoming
336 * SET_TARGET_PORT_GROUPS op.
338 rtpi
= get_unaligned_be16(ptr
+ 2);
340 * Locate the matching relative target port identifer
341 * for the struct se_device storage object.
343 spin_lock(&dev
->se_port_lock
);
344 list_for_each_entry(port
, &dev
->dev_sep_list
,
346 if (port
->sep_rtpi
!= rtpi
)
349 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
350 spin_unlock(&dev
->se_port_lock
);
352 rc
= core_alua_set_tg_pt_secondary_state(
353 tg_pt_gp_mem
, port
, 1, 1);
355 spin_lock(&dev
->se_port_lock
);
358 spin_unlock(&dev
->se_port_lock
);
360 * If not matching relative target port identifier can
361 * be located, throw an exception with ASCQ:
362 * INVALID_PARAMETER_LIST
365 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
376 transport_kunmap_data_sg(cmd
);
377 target_complete_cmd(cmd
, GOOD
);
381 static inline int core_alua_state_nonoptimized(
384 int nonop_delay_msecs
,
388 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
389 * later to determine if processing of this cmd needs to be
390 * temporarily delayed for the Active/NonOptimized primary access state.
392 cmd
->se_cmd_flags
|= SCF_ALUA_NON_OPTIMIZED
;
393 cmd
->alua_nonop_delay
= nonop_delay_msecs
;
397 static inline int core_alua_state_standby(
403 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
404 * spc4r17 section 5.9.2.4.4
413 case RECEIVE_DIAGNOSTIC
:
414 case SEND_DIAGNOSTIC
:
416 switch (cdb
[1] & 0x1f) {
417 case MI_REPORT_TARGET_PGS
:
420 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_STANDBY
;
423 case MAINTENANCE_OUT
:
425 case MO_SET_TARGET_PGS
:
428 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_STANDBY
;
432 case PERSISTENT_RESERVE_IN
:
433 case PERSISTENT_RESERVE_OUT
:
438 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_STANDBY
;
445 static inline int core_alua_state_unavailable(
451 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
452 * spc4r17 section 5.9.2.4.5
458 switch (cdb
[1] & 0x1f) {
459 case MI_REPORT_TARGET_PGS
:
462 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
;
465 case MAINTENANCE_OUT
:
467 case MO_SET_TARGET_PGS
:
470 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
;
478 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
;
485 static inline int core_alua_state_transition(
491 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by
492 * spc4r17 section 5.9.2.5
498 switch (cdb
[1] & 0x1f) {
499 case MI_REPORT_TARGET_PGS
:
502 *alua_ascq
= ASCQ_04H_ALUA_STATE_TRANSITION
;
510 *alua_ascq
= ASCQ_04H_ALUA_STATE_TRANSITION
;
518 * Used for alua_type SPC_ALUA_PASSTHROUGH and SPC2_ALUA_DISABLED
519 * in transport_cmd_sequencer(). This function is assigned to
520 * struct t10_alua *->state_check() in core_setup_alua()
522 static int core_alua_state_check_nop(
531 * Used for alua_type SPC3_ALUA_EMULATED in transport_cmd_sequencer().
532 * This function is assigned to struct t10_alua *->state_check() in
535 * Also, this function can return three different return codes to
536 * signal transport_generic_cmd_sequencer()
538 * return 1: Is used to signal LUN not accecsable, and check condition/not ready
539 * return 0: Used to signal success
540 * reutrn -1: Used to signal failure, and invalid cdb field
542 static int core_alua_state_check(
547 struct se_lun
*lun
= cmd
->se_lun
;
548 struct se_port
*port
= lun
->lun_sep
;
549 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
550 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
551 int out_alua_state
, nonop_delay_msecs
;
556 * First, check for a struct se_port specific secondary ALUA target port
557 * access state: OFFLINE
559 if (atomic_read(&port
->sep_tg_pt_secondary_offline
)) {
560 *alua_ascq
= ASCQ_04H_ALUA_OFFLINE
;
561 pr_debug("ALUA: Got secondary offline status for local"
563 *alua_ascq
= ASCQ_04H_ALUA_OFFLINE
;
567 * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
568 * ALUA target port group, to obtain current ALUA access state.
569 * Otherwise look for the underlying struct se_device association with
570 * a ALUA logical unit group.
572 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
573 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
574 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
575 out_alua_state
= atomic_read(&tg_pt_gp
->tg_pt_gp_alua_access_state
);
576 nonop_delay_msecs
= tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
;
577 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
579 * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a separate conditional
580 * statement so the compiler knows explicitly to check this case first.
581 * For the Optimized ALUA access state case, we want to process the
582 * incoming fabric cmd ASAP..
584 if (out_alua_state
== ALUA_ACCESS_STATE_ACTIVE_OPTMIZED
)
587 switch (out_alua_state
) {
588 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
589 return core_alua_state_nonoptimized(cmd
, cdb
,
590 nonop_delay_msecs
, alua_ascq
);
591 case ALUA_ACCESS_STATE_STANDBY
:
592 return core_alua_state_standby(cmd
, cdb
, alua_ascq
);
593 case ALUA_ACCESS_STATE_UNAVAILABLE
:
594 return core_alua_state_unavailable(cmd
, cdb
, alua_ascq
);
595 case ALUA_ACCESS_STATE_TRANSITION
:
596 return core_alua_state_transition(cmd
, cdb
, alua_ascq
);
598 * OFFLINE is a secondary ALUA target port group access state, that is
599 * handled above with struct se_port->sep_tg_pt_secondary_offline=1
601 case ALUA_ACCESS_STATE_OFFLINE
:
603 pr_err("Unknown ALUA access state: 0x%02x\n",
612 * Check implict and explict ALUA state change request.
614 static int core_alua_check_transition(int state
, int *primary
)
617 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED
:
618 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
619 case ALUA_ACCESS_STATE_STANDBY
:
620 case ALUA_ACCESS_STATE_UNAVAILABLE
:
622 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
623 * defined as primary target port asymmetric access states.
627 case ALUA_ACCESS_STATE_OFFLINE
:
629 * OFFLINE state is defined as a secondary target port
630 * asymmetric access state.
635 pr_err("Unknown ALUA access state: 0x%02x\n", state
);
642 static char *core_alua_dump_state(int state
)
645 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED
:
646 return "Active/Optimized";
647 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
648 return "Active/NonOptimized";
649 case ALUA_ACCESS_STATE_STANDBY
:
651 case ALUA_ACCESS_STATE_UNAVAILABLE
:
652 return "Unavailable";
653 case ALUA_ACCESS_STATE_OFFLINE
:
662 char *core_alua_dump_status(int status
)
665 case ALUA_STATUS_NONE
:
667 case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG
:
668 return "Altered by Explict STPG";
669 case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA
:
670 return "Altered by Implict ALUA";
679 * Used by fabric modules to determine when we need to delay processing
680 * for the Active/NonOptimized paths..
682 int core_alua_check_nonop_delay(
685 if (!(cmd
->se_cmd_flags
& SCF_ALUA_NON_OPTIMIZED
))
690 * The ALUA Active/NonOptimized access state delay can be disabled
691 * in via configfs with a value of zero
693 if (!cmd
->alua_nonop_delay
)
696 * struct se_cmd->alua_nonop_delay gets set by a target port group
697 * defined interval in core_alua_state_nonoptimized()
699 msleep_interruptible(cmd
->alua_nonop_delay
);
702 EXPORT_SYMBOL(core_alua_check_nonop_delay
);
705 * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
708 static int core_alua_write_tpg_metadata(
710 unsigned char *md_buf
,
716 int flags
= O_RDWR
| O_CREAT
| O_TRUNC
, ret
;
718 memset(iov
, 0, sizeof(struct iovec
));
720 file
= filp_open(path
, flags
, 0600);
721 if (IS_ERR(file
) || !file
|| !file
->f_dentry
) {
722 pr_err("filp_open(%s) for ALUA metadata failed\n",
727 iov
[0].iov_base
= &md_buf
[0];
728 iov
[0].iov_len
= md_buf_len
;
732 ret
= vfs_writev(file
, &iov
[0], 1, &file
->f_pos
);
736 pr_err("Error writing ALUA metadata file: %s\n", path
);
737 filp_close(file
, NULL
);
740 filp_close(file
, NULL
);
746 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
748 static int core_alua_update_tpg_primary_metadata(
749 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
751 unsigned char *md_buf
)
753 struct se_subsystem_dev
*su_dev
= tg_pt_gp
->tg_pt_gp_su_dev
;
754 struct t10_wwn
*wwn
= &su_dev
->t10_wwn
;
755 char path
[ALUA_METADATA_PATH_LEN
];
758 memset(path
, 0, ALUA_METADATA_PATH_LEN
);
760 len
= snprintf(md_buf
, tg_pt_gp
->tg_pt_gp_md_buf_len
,
762 "alua_access_state=0x%02x\n"
763 "alua_access_status=0x%02x\n",
764 tg_pt_gp
->tg_pt_gp_id
, primary_state
,
765 tg_pt_gp
->tg_pt_gp_alua_access_status
);
767 snprintf(path
, ALUA_METADATA_PATH_LEN
,
768 "/var/target/alua/tpgs_%s/%s", &wwn
->unit_serial
[0],
769 config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
));
771 return core_alua_write_tpg_metadata(path
, md_buf
, len
);
774 static int core_alua_do_transition_tg_pt(
775 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
776 struct se_port
*l_port
,
777 struct se_node_acl
*nacl
,
778 unsigned char *md_buf
,
782 struct se_dev_entry
*se_deve
;
783 struct se_lun_acl
*lacl
;
784 struct se_port
*port
;
785 struct t10_alua_tg_pt_gp_member
*mem
;
788 * Save the old primary ALUA access state, and set the current state
789 * to ALUA_ACCESS_STATE_TRANSITION.
791 old_state
= atomic_read(&tg_pt_gp
->tg_pt_gp_alua_access_state
);
792 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
,
793 ALUA_ACCESS_STATE_TRANSITION
);
794 tg_pt_gp
->tg_pt_gp_alua_access_status
= (explict
) ?
795 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG
:
796 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA
;
798 * Check for the optional ALUA primary state transition delay
800 if (tg_pt_gp
->tg_pt_gp_trans_delay_msecs
!= 0)
801 msleep_interruptible(tg_pt_gp
->tg_pt_gp_trans_delay_msecs
);
803 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
804 list_for_each_entry(mem
, &tg_pt_gp
->tg_pt_gp_mem_list
,
808 * After an implicit target port asymmetric access state
809 * change, a device server shall establish a unit attention
810 * condition for the initiator port associated with every I_T
811 * nexus with the additional sense code set to ASYMMETRIC
812 * ACCESS STATE CHAGED.
814 * After an explicit target port asymmetric access state
815 * change, a device server shall establish a unit attention
816 * condition with the additional sense code set to ASYMMETRIC
817 * ACCESS STATE CHANGED for the initiator port associated with
818 * every I_T nexus other than the I_T nexus on which the SET
819 * TARGET PORT GROUPS command
821 atomic_inc(&mem
->tg_pt_gp_mem_ref_cnt
);
822 smp_mb__after_atomic_inc();
823 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
825 spin_lock_bh(&port
->sep_alua_lock
);
826 list_for_each_entry(se_deve
, &port
->sep_alua_list
,
828 lacl
= se_deve
->se_lun_acl
;
830 * se_deve->se_lun_acl pointer may be NULL for a
831 * entry created without explict Node+MappedLUN ACLs
837 (nacl
!= NULL
) && (nacl
== lacl
->se_lun_nacl
) &&
838 (l_port
!= NULL
) && (l_port
== port
))
841 core_scsi3_ua_allocate(lacl
->se_lun_nacl
,
842 se_deve
->mapped_lun
, 0x2A,
843 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED
);
845 spin_unlock_bh(&port
->sep_alua_lock
);
847 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
848 atomic_dec(&mem
->tg_pt_gp_mem_ref_cnt
);
849 smp_mb__after_atomic_dec();
851 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
853 * Update the ALUA metadata buf that has been allocated in
854 * core_alua_do_port_transition(), this metadata will be written
857 * Note that there is the case where we do not want to update the
858 * metadata when the saved metadata is being parsed in userspace
859 * when setting the existing port access state and access status.
861 * Also note that the failure to write out the ALUA metadata to
862 * struct file does NOT affect the actual ALUA transition.
864 if (tg_pt_gp
->tg_pt_gp_write_metadata
) {
865 mutex_lock(&tg_pt_gp
->tg_pt_gp_md_mutex
);
866 core_alua_update_tpg_primary_metadata(tg_pt_gp
,
868 mutex_unlock(&tg_pt_gp
->tg_pt_gp_md_mutex
);
871 * Set the current primary ALUA access state to the requested new state
873 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
, new_state
);
875 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
876 " from primary access state %s to %s\n", (explict
) ? "explict" :
877 "implict", config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
),
878 tg_pt_gp
->tg_pt_gp_id
, core_alua_dump_state(old_state
),
879 core_alua_dump_state(new_state
));
884 int core_alua_do_port_transition(
885 struct t10_alua_tg_pt_gp
*l_tg_pt_gp
,
886 struct se_device
*l_dev
,
887 struct se_port
*l_port
,
888 struct se_node_acl
*l_nacl
,
892 struct se_device
*dev
;
893 struct se_port
*port
;
894 struct se_subsystem_dev
*su_dev
;
895 struct se_node_acl
*nacl
;
896 struct t10_alua_lu_gp
*lu_gp
;
897 struct t10_alua_lu_gp_member
*lu_gp_mem
, *local_lu_gp_mem
;
898 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
899 unsigned char *md_buf
;
902 if (core_alua_check_transition(new_state
, &primary
) != 0)
905 md_buf
= kzalloc(l_tg_pt_gp
->tg_pt_gp_md_buf_len
, GFP_KERNEL
);
907 pr_err("Unable to allocate buf for ALUA metadata\n");
911 local_lu_gp_mem
= l_dev
->dev_alua_lu_gp_mem
;
912 spin_lock(&local_lu_gp_mem
->lu_gp_mem_lock
);
913 lu_gp
= local_lu_gp_mem
->lu_gp
;
914 atomic_inc(&lu_gp
->lu_gp_ref_cnt
);
915 smp_mb__after_atomic_inc();
916 spin_unlock(&local_lu_gp_mem
->lu_gp_mem_lock
);
918 * For storage objects that are members of the 'default_lu_gp',
919 * we only do transition on the passed *l_tp_pt_gp, and not
920 * on all of the matching target port groups IDs in default_lu_gp.
922 if (!lu_gp
->lu_gp_id
) {
924 * core_alua_do_transition_tg_pt() will always return
927 core_alua_do_transition_tg_pt(l_tg_pt_gp
, l_port
, l_nacl
,
928 md_buf
, new_state
, explict
);
929 atomic_dec(&lu_gp
->lu_gp_ref_cnt
);
930 smp_mb__after_atomic_dec();
935 * For all other LU groups aside from 'default_lu_gp', walk all of
936 * the associated storage objects looking for a matching target port
937 * group ID from the local target port group.
939 spin_lock(&lu_gp
->lu_gp_lock
);
940 list_for_each_entry(lu_gp_mem
, &lu_gp
->lu_gp_mem_list
,
943 dev
= lu_gp_mem
->lu_gp_mem_dev
;
944 su_dev
= dev
->se_sub_dev
;
945 atomic_inc(&lu_gp_mem
->lu_gp_mem_ref_cnt
);
946 smp_mb__after_atomic_inc();
947 spin_unlock(&lu_gp
->lu_gp_lock
);
949 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
950 list_for_each_entry(tg_pt_gp
,
951 &su_dev
->t10_alua
.tg_pt_gps_list
,
954 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
957 * If the target behavior port asymmetric access state
958 * is changed for any target port group accessiable via
959 * a logical unit within a LU group, the target port
960 * behavior group asymmetric access states for the same
961 * target port group accessible via other logical units
962 * in that LU group will also change.
964 if (l_tg_pt_gp
->tg_pt_gp_id
!= tg_pt_gp
->tg_pt_gp_id
)
967 if (l_tg_pt_gp
== tg_pt_gp
) {
974 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
975 smp_mb__after_atomic_inc();
976 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
978 * core_alua_do_transition_tg_pt() will always return
981 core_alua_do_transition_tg_pt(tg_pt_gp
, port
,
982 nacl
, md_buf
, new_state
, explict
);
984 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
985 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
986 smp_mb__after_atomic_dec();
988 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
990 spin_lock(&lu_gp
->lu_gp_lock
);
991 atomic_dec(&lu_gp_mem
->lu_gp_mem_ref_cnt
);
992 smp_mb__after_atomic_dec();
994 spin_unlock(&lu_gp
->lu_gp_lock
);
996 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
997 " Group IDs: %hu %s transition to primary state: %s\n",
998 config_item_name(&lu_gp
->lu_gp_group
.cg_item
),
999 l_tg_pt_gp
->tg_pt_gp_id
, (explict
) ? "explict" : "implict",
1000 core_alua_dump_state(new_state
));
1002 atomic_dec(&lu_gp
->lu_gp_ref_cnt
);
1003 smp_mb__after_atomic_dec();
1009 * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
1011 static int core_alua_update_tpg_secondary_metadata(
1012 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
1013 struct se_port
*port
,
1014 unsigned char *md_buf
,
1017 struct se_portal_group
*se_tpg
= port
->sep_tpg
;
1018 char path
[ALUA_METADATA_PATH_LEN
], wwn
[ALUA_SECONDARY_METADATA_WWN_LEN
];
1021 memset(path
, 0, ALUA_METADATA_PATH_LEN
);
1022 memset(wwn
, 0, ALUA_SECONDARY_METADATA_WWN_LEN
);
1024 len
= snprintf(wwn
, ALUA_SECONDARY_METADATA_WWN_LEN
, "%s",
1025 se_tpg
->se_tpg_tfo
->tpg_get_wwn(se_tpg
));
1027 if (se_tpg
->se_tpg_tfo
->tpg_get_tag
!= NULL
)
1028 snprintf(wwn
+len
, ALUA_SECONDARY_METADATA_WWN_LEN
-len
, "+%hu",
1029 se_tpg
->se_tpg_tfo
->tpg_get_tag(se_tpg
));
1031 len
= snprintf(md_buf
, md_buf_len
, "alua_tg_pt_offline=%d\n"
1032 "alua_tg_pt_status=0x%02x\n",
1033 atomic_read(&port
->sep_tg_pt_secondary_offline
),
1034 port
->sep_tg_pt_secondary_stat
);
1036 snprintf(path
, ALUA_METADATA_PATH_LEN
, "/var/target/alua/%s/%s/lun_%u",
1037 se_tpg
->se_tpg_tfo
->get_fabric_name(), wwn
,
1038 port
->sep_lun
->unpacked_lun
);
1040 return core_alua_write_tpg_metadata(path
, md_buf
, len
);
1043 static int core_alua_set_tg_pt_secondary_state(
1044 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
1045 struct se_port
*port
,
1049 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1050 unsigned char *md_buf
;
1052 int trans_delay_msecs
;
1054 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1055 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
1057 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1058 pr_err("Unable to complete secondary state"
1062 trans_delay_msecs
= tg_pt_gp
->tg_pt_gp_trans_delay_msecs
;
1064 * Set the secondary ALUA target port access state to OFFLINE
1065 * or release the previously secondary state for struct se_port
1068 atomic_set(&port
->sep_tg_pt_secondary_offline
, 1);
1070 atomic_set(&port
->sep_tg_pt_secondary_offline
, 0);
1072 md_buf_len
= tg_pt_gp
->tg_pt_gp_md_buf_len
;
1073 port
->sep_tg_pt_secondary_stat
= (explict
) ?
1074 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG
:
1075 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA
;
1077 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1078 " to secondary access state: %s\n", (explict
) ? "explict" :
1079 "implict", config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1080 tg_pt_gp
->tg_pt_gp_id
, (offline
) ? "OFFLINE" : "ONLINE");
1082 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1084 * Do the optional transition delay after we set the secondary
1085 * ALUA access state.
1087 if (trans_delay_msecs
!= 0)
1088 msleep_interruptible(trans_delay_msecs
);
1090 * See if we need to update the ALUA fabric port metadata for
1091 * secondary state and status
1093 if (port
->sep_tg_pt_secondary_write_md
) {
1094 md_buf
= kzalloc(md_buf_len
, GFP_KERNEL
);
1096 pr_err("Unable to allocate md_buf for"
1097 " secondary ALUA access metadata\n");
1100 mutex_lock(&port
->sep_tg_pt_md_mutex
);
1101 core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem
, port
,
1102 md_buf
, md_buf_len
);
1103 mutex_unlock(&port
->sep_tg_pt_md_mutex
);
1111 struct t10_alua_lu_gp
*
1112 core_alua_allocate_lu_gp(const char *name
, int def_group
)
1114 struct t10_alua_lu_gp
*lu_gp
;
1116 lu_gp
= kmem_cache_zalloc(t10_alua_lu_gp_cache
, GFP_KERNEL
);
1118 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1119 return ERR_PTR(-ENOMEM
);
1121 INIT_LIST_HEAD(&lu_gp
->lu_gp_node
);
1122 INIT_LIST_HEAD(&lu_gp
->lu_gp_mem_list
);
1123 spin_lock_init(&lu_gp
->lu_gp_lock
);
1124 atomic_set(&lu_gp
->lu_gp_ref_cnt
, 0);
1127 lu_gp
->lu_gp_id
= alua_lu_gps_counter
++;
1128 lu_gp
->lu_gp_valid_id
= 1;
1129 alua_lu_gps_count
++;
1135 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp
*lu_gp
, u16 lu_gp_id
)
1137 struct t10_alua_lu_gp
*lu_gp_tmp
;
1140 * The lu_gp->lu_gp_id may only be set once..
1142 if (lu_gp
->lu_gp_valid_id
) {
1143 pr_warn("ALUA LU Group already has a valid ID,"
1144 " ignoring request\n");
1148 spin_lock(&lu_gps_lock
);
1149 if (alua_lu_gps_count
== 0x0000ffff) {
1150 pr_err("Maximum ALUA alua_lu_gps_count:"
1151 " 0x0000ffff reached\n");
1152 spin_unlock(&lu_gps_lock
);
1153 kmem_cache_free(t10_alua_lu_gp_cache
, lu_gp
);
1157 lu_gp_id_tmp
= (lu_gp_id
!= 0) ? lu_gp_id
:
1158 alua_lu_gps_counter
++;
1160 list_for_each_entry(lu_gp_tmp
, &lu_gps_list
, lu_gp_node
) {
1161 if (lu_gp_tmp
->lu_gp_id
== lu_gp_id_tmp
) {
1165 pr_warn("ALUA Logical Unit Group ID: %hu"
1166 " already exists, ignoring request\n",
1168 spin_unlock(&lu_gps_lock
);
1173 lu_gp
->lu_gp_id
= lu_gp_id_tmp
;
1174 lu_gp
->lu_gp_valid_id
= 1;
1175 list_add_tail(&lu_gp
->lu_gp_node
, &lu_gps_list
);
1176 alua_lu_gps_count
++;
1177 spin_unlock(&lu_gps_lock
);
1182 static struct t10_alua_lu_gp_member
*
1183 core_alua_allocate_lu_gp_mem(struct se_device
*dev
)
1185 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1187 lu_gp_mem
= kmem_cache_zalloc(t10_alua_lu_gp_mem_cache
, GFP_KERNEL
);
1189 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1190 return ERR_PTR(-ENOMEM
);
1192 INIT_LIST_HEAD(&lu_gp_mem
->lu_gp_mem_list
);
1193 spin_lock_init(&lu_gp_mem
->lu_gp_mem_lock
);
1194 atomic_set(&lu_gp_mem
->lu_gp_mem_ref_cnt
, 0);
1196 lu_gp_mem
->lu_gp_mem_dev
= dev
;
1197 dev
->dev_alua_lu_gp_mem
= lu_gp_mem
;
1202 void core_alua_free_lu_gp(struct t10_alua_lu_gp
*lu_gp
)
1204 struct t10_alua_lu_gp_member
*lu_gp_mem
, *lu_gp_mem_tmp
;
1206 * Once we have reached this point, config_item_put() has
1207 * already been called from target_core_alua_drop_lu_gp().
1209 * Here, we remove the *lu_gp from the global list so that
1210 * no associations can be made while we are releasing
1211 * struct t10_alua_lu_gp.
1213 spin_lock(&lu_gps_lock
);
1214 list_del(&lu_gp
->lu_gp_node
);
1215 alua_lu_gps_count
--;
1216 spin_unlock(&lu_gps_lock
);
1218 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1219 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1220 * released with core_alua_put_lu_gp_from_name()
1222 while (atomic_read(&lu_gp
->lu_gp_ref_cnt
))
1225 * Release reference to struct t10_alua_lu_gp * from all associated
1228 spin_lock(&lu_gp
->lu_gp_lock
);
1229 list_for_each_entry_safe(lu_gp_mem
, lu_gp_mem_tmp
,
1230 &lu_gp
->lu_gp_mem_list
, lu_gp_mem_list
) {
1231 if (lu_gp_mem
->lu_gp_assoc
) {
1232 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1233 lu_gp
->lu_gp_members
--;
1234 lu_gp_mem
->lu_gp_assoc
= 0;
1236 spin_unlock(&lu_gp
->lu_gp_lock
);
1239 * lu_gp_mem is associated with a single
1240 * struct se_device->dev_alua_lu_gp_mem, and is released when
1241 * struct se_device is released via core_alua_free_lu_gp_mem().
1243 * If the passed lu_gp does NOT match the default_lu_gp, assume
1244 * we want to re-assocate a given lu_gp_mem with default_lu_gp.
1246 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1247 if (lu_gp
!= default_lu_gp
)
1248 __core_alua_attach_lu_gp_mem(lu_gp_mem
,
1251 lu_gp_mem
->lu_gp
= NULL
;
1252 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1254 spin_lock(&lu_gp
->lu_gp_lock
);
1256 spin_unlock(&lu_gp
->lu_gp_lock
);
1258 kmem_cache_free(t10_alua_lu_gp_cache
, lu_gp
);
1261 void core_alua_free_lu_gp_mem(struct se_device
*dev
)
1263 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
1264 struct t10_alua
*alua
= &su_dev
->t10_alua
;
1265 struct t10_alua_lu_gp
*lu_gp
;
1266 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1268 if (alua
->alua_type
!= SPC3_ALUA_EMULATED
)
1271 lu_gp_mem
= dev
->dev_alua_lu_gp_mem
;
1275 while (atomic_read(&lu_gp_mem
->lu_gp_mem_ref_cnt
))
1278 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1279 lu_gp
= lu_gp_mem
->lu_gp
;
1281 spin_lock(&lu_gp
->lu_gp_lock
);
1282 if (lu_gp_mem
->lu_gp_assoc
) {
1283 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1284 lu_gp
->lu_gp_members
--;
1285 lu_gp_mem
->lu_gp_assoc
= 0;
1287 spin_unlock(&lu_gp
->lu_gp_lock
);
1288 lu_gp_mem
->lu_gp
= NULL
;
1290 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1292 kmem_cache_free(t10_alua_lu_gp_mem_cache
, lu_gp_mem
);
1295 struct t10_alua_lu_gp
*core_alua_get_lu_gp_by_name(const char *name
)
1297 struct t10_alua_lu_gp
*lu_gp
;
1298 struct config_item
*ci
;
1300 spin_lock(&lu_gps_lock
);
1301 list_for_each_entry(lu_gp
, &lu_gps_list
, lu_gp_node
) {
1302 if (!lu_gp
->lu_gp_valid_id
)
1304 ci
= &lu_gp
->lu_gp_group
.cg_item
;
1305 if (!strcmp(config_item_name(ci
), name
)) {
1306 atomic_inc(&lu_gp
->lu_gp_ref_cnt
);
1307 spin_unlock(&lu_gps_lock
);
1311 spin_unlock(&lu_gps_lock
);
1316 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp
*lu_gp
)
1318 spin_lock(&lu_gps_lock
);
1319 atomic_dec(&lu_gp
->lu_gp_ref_cnt
);
1320 spin_unlock(&lu_gps_lock
);
1324 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1326 void __core_alua_attach_lu_gp_mem(
1327 struct t10_alua_lu_gp_member
*lu_gp_mem
,
1328 struct t10_alua_lu_gp
*lu_gp
)
1330 spin_lock(&lu_gp
->lu_gp_lock
);
1331 lu_gp_mem
->lu_gp
= lu_gp
;
1332 lu_gp_mem
->lu_gp_assoc
= 1;
1333 list_add_tail(&lu_gp_mem
->lu_gp_mem_list
, &lu_gp
->lu_gp_mem_list
);
1334 lu_gp
->lu_gp_members
++;
1335 spin_unlock(&lu_gp
->lu_gp_lock
);
1339 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1341 void __core_alua_drop_lu_gp_mem(
1342 struct t10_alua_lu_gp_member
*lu_gp_mem
,
1343 struct t10_alua_lu_gp
*lu_gp
)
1345 spin_lock(&lu_gp
->lu_gp_lock
);
1346 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1347 lu_gp_mem
->lu_gp
= NULL
;
1348 lu_gp_mem
->lu_gp_assoc
= 0;
1349 lu_gp
->lu_gp_members
--;
1350 spin_unlock(&lu_gp
->lu_gp_lock
);
1353 struct t10_alua_tg_pt_gp
*core_alua_allocate_tg_pt_gp(
1354 struct se_subsystem_dev
*su_dev
,
1358 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1360 tg_pt_gp
= kmem_cache_zalloc(t10_alua_tg_pt_gp_cache
, GFP_KERNEL
);
1362 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1365 INIT_LIST_HEAD(&tg_pt_gp
->tg_pt_gp_list
);
1366 INIT_LIST_HEAD(&tg_pt_gp
->tg_pt_gp_mem_list
);
1367 mutex_init(&tg_pt_gp
->tg_pt_gp_md_mutex
);
1368 spin_lock_init(&tg_pt_gp
->tg_pt_gp_lock
);
1369 atomic_set(&tg_pt_gp
->tg_pt_gp_ref_cnt
, 0);
1370 tg_pt_gp
->tg_pt_gp_su_dev
= su_dev
;
1371 tg_pt_gp
->tg_pt_gp_md_buf_len
= ALUA_MD_BUF_LEN
;
1372 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
,
1373 ALUA_ACCESS_STATE_ACTIVE_OPTMIZED
);
1375 * Enable both explict and implict ALUA support by default
1377 tg_pt_gp
->tg_pt_gp_alua_access_type
=
1378 TPGS_EXPLICT_ALUA
| TPGS_IMPLICT_ALUA
;
1380 * Set the default Active/NonOptimized Delay in milliseconds
1382 tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
= ALUA_DEFAULT_NONOP_DELAY_MSECS
;
1383 tg_pt_gp
->tg_pt_gp_trans_delay_msecs
= ALUA_DEFAULT_TRANS_DELAY_MSECS
;
1384 tg_pt_gp
->tg_pt_gp_implict_trans_secs
= ALUA_DEFAULT_IMPLICT_TRANS_SECS
;
1387 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1388 tg_pt_gp
->tg_pt_gp_id
=
1389 su_dev
->t10_alua
.alua_tg_pt_gps_counter
++;
1390 tg_pt_gp
->tg_pt_gp_valid_id
= 1;
1391 su_dev
->t10_alua
.alua_tg_pt_gps_count
++;
1392 list_add_tail(&tg_pt_gp
->tg_pt_gp_list
,
1393 &su_dev
->t10_alua
.tg_pt_gps_list
);
1394 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1400 int core_alua_set_tg_pt_gp_id(
1401 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1404 struct se_subsystem_dev
*su_dev
= tg_pt_gp
->tg_pt_gp_su_dev
;
1405 struct t10_alua_tg_pt_gp
*tg_pt_gp_tmp
;
1406 u16 tg_pt_gp_id_tmp
;
1408 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1410 if (tg_pt_gp
->tg_pt_gp_valid_id
) {
1411 pr_warn("ALUA TG PT Group already has a valid ID,"
1412 " ignoring request\n");
1416 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1417 if (su_dev
->t10_alua
.alua_tg_pt_gps_count
== 0x0000ffff) {
1418 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1419 " 0x0000ffff reached\n");
1420 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1421 kmem_cache_free(t10_alua_tg_pt_gp_cache
, tg_pt_gp
);
1425 tg_pt_gp_id_tmp
= (tg_pt_gp_id
!= 0) ? tg_pt_gp_id
:
1426 su_dev
->t10_alua
.alua_tg_pt_gps_counter
++;
1428 list_for_each_entry(tg_pt_gp_tmp
, &su_dev
->t10_alua
.tg_pt_gps_list
,
1430 if (tg_pt_gp_tmp
->tg_pt_gp_id
== tg_pt_gp_id_tmp
) {
1434 pr_err("ALUA Target Port Group ID: %hu already"
1435 " exists, ignoring request\n", tg_pt_gp_id
);
1436 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1441 tg_pt_gp
->tg_pt_gp_id
= tg_pt_gp_id_tmp
;
1442 tg_pt_gp
->tg_pt_gp_valid_id
= 1;
1443 list_add_tail(&tg_pt_gp
->tg_pt_gp_list
,
1444 &su_dev
->t10_alua
.tg_pt_gps_list
);
1445 su_dev
->t10_alua
.alua_tg_pt_gps_count
++;
1446 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1451 struct t10_alua_tg_pt_gp_member
*core_alua_allocate_tg_pt_gp_mem(
1452 struct se_port
*port
)
1454 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1456 tg_pt_gp_mem
= kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache
,
1458 if (!tg_pt_gp_mem
) {
1459 pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n");
1460 return ERR_PTR(-ENOMEM
);
1462 INIT_LIST_HEAD(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1463 spin_lock_init(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1464 atomic_set(&tg_pt_gp_mem
->tg_pt_gp_mem_ref_cnt
, 0);
1466 tg_pt_gp_mem
->tg_pt
= port
;
1467 port
->sep_alua_tg_pt_gp_mem
= tg_pt_gp_mem
;
1469 return tg_pt_gp_mem
;
1472 void core_alua_free_tg_pt_gp(
1473 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1475 struct se_subsystem_dev
*su_dev
= tg_pt_gp
->tg_pt_gp_su_dev
;
1476 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
, *tg_pt_gp_mem_tmp
;
1478 * Once we have reached this point, config_item_put() has already
1479 * been called from target_core_alua_drop_tg_pt_gp().
1481 * Here we remove *tg_pt_gp from the global list so that
1482 * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS
1483 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1485 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1486 list_del(&tg_pt_gp
->tg_pt_gp_list
);
1487 su_dev
->t10_alua
.alua_tg_pt_gps_counter
--;
1488 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1490 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1491 * core_alua_get_tg_pt_gp_by_name() in
1492 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1493 * to be released with core_alua_put_tg_pt_gp_from_name().
1495 while (atomic_read(&tg_pt_gp
->tg_pt_gp_ref_cnt
))
1498 * Release reference to struct t10_alua_tg_pt_gp from all associated
1501 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1502 list_for_each_entry_safe(tg_pt_gp_mem
, tg_pt_gp_mem_tmp
,
1503 &tg_pt_gp
->tg_pt_gp_mem_list
, tg_pt_gp_mem_list
) {
1504 if (tg_pt_gp_mem
->tg_pt_gp_assoc
) {
1505 list_del(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1506 tg_pt_gp
->tg_pt_gp_members
--;
1507 tg_pt_gp_mem
->tg_pt_gp_assoc
= 0;
1509 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1511 * tg_pt_gp_mem is associated with a single
1512 * se_port->sep_alua_tg_pt_gp_mem, and is released via
1513 * core_alua_free_tg_pt_gp_mem().
1515 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1516 * assume we want to re-assocate a given tg_pt_gp_mem with
1519 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1520 if (tg_pt_gp
!= su_dev
->t10_alua
.default_tg_pt_gp
) {
1521 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem
,
1522 su_dev
->t10_alua
.default_tg_pt_gp
);
1524 tg_pt_gp_mem
->tg_pt_gp
= NULL
;
1525 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1527 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1529 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1531 kmem_cache_free(t10_alua_tg_pt_gp_cache
, tg_pt_gp
);
1534 void core_alua_free_tg_pt_gp_mem(struct se_port
*port
)
1536 struct se_subsystem_dev
*su_dev
= port
->sep_lun
->lun_se_dev
->se_sub_dev
;
1537 struct t10_alua
*alua
= &su_dev
->t10_alua
;
1538 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1539 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1541 if (alua
->alua_type
!= SPC3_ALUA_EMULATED
)
1544 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
1548 while (atomic_read(&tg_pt_gp_mem
->tg_pt_gp_mem_ref_cnt
))
1551 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1552 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
1554 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1555 if (tg_pt_gp_mem
->tg_pt_gp_assoc
) {
1556 list_del(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1557 tg_pt_gp
->tg_pt_gp_members
--;
1558 tg_pt_gp_mem
->tg_pt_gp_assoc
= 0;
1560 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1561 tg_pt_gp_mem
->tg_pt_gp
= NULL
;
1563 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1565 kmem_cache_free(t10_alua_tg_pt_gp_mem_cache
, tg_pt_gp_mem
);
1568 static struct t10_alua_tg_pt_gp
*core_alua_get_tg_pt_gp_by_name(
1569 struct se_subsystem_dev
*su_dev
,
1572 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1573 struct config_item
*ci
;
1575 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1576 list_for_each_entry(tg_pt_gp
, &su_dev
->t10_alua
.tg_pt_gps_list
,
1578 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
1580 ci
= &tg_pt_gp
->tg_pt_gp_group
.cg_item
;
1581 if (!strcmp(config_item_name(ci
), name
)) {
1582 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1583 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1587 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1592 static void core_alua_put_tg_pt_gp_from_name(
1593 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1595 struct se_subsystem_dev
*su_dev
= tg_pt_gp
->tg_pt_gp_su_dev
;
1597 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1598 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1599 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1603 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1605 void __core_alua_attach_tg_pt_gp_mem(
1606 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
1607 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1609 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1610 tg_pt_gp_mem
->tg_pt_gp
= tg_pt_gp
;
1611 tg_pt_gp_mem
->tg_pt_gp_assoc
= 1;
1612 list_add_tail(&tg_pt_gp_mem
->tg_pt_gp_mem_list
,
1613 &tg_pt_gp
->tg_pt_gp_mem_list
);
1614 tg_pt_gp
->tg_pt_gp_members
++;
1615 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1619 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1621 static void __core_alua_drop_tg_pt_gp_mem(
1622 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
1623 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1625 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1626 list_del(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1627 tg_pt_gp_mem
->tg_pt_gp
= NULL
;
1628 tg_pt_gp_mem
->tg_pt_gp_assoc
= 0;
1629 tg_pt_gp
->tg_pt_gp_members
--;
1630 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1633 ssize_t
core_alua_show_tg_pt_gp_info(struct se_port
*port
, char *page
)
1635 struct se_subsystem_dev
*su_dev
= port
->sep_lun
->lun_se_dev
->se_sub_dev
;
1636 struct config_item
*tg_pt_ci
;
1637 struct t10_alua
*alua
= &su_dev
->t10_alua
;
1638 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1639 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1642 if (alua
->alua_type
!= SPC3_ALUA_EMULATED
)
1645 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
1649 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1650 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
1652 tg_pt_ci
= &tg_pt_gp
->tg_pt_gp_group
.cg_item
;
1653 len
+= sprintf(page
, "TG Port Alias: %s\nTG Port Group ID:"
1654 " %hu\nTG Port Primary Access State: %s\nTG Port "
1655 "Primary Access Status: %s\nTG Port Secondary Access"
1656 " State: %s\nTG Port Secondary Access Status: %s\n",
1657 config_item_name(tg_pt_ci
), tg_pt_gp
->tg_pt_gp_id
,
1658 core_alua_dump_state(atomic_read(
1659 &tg_pt_gp
->tg_pt_gp_alua_access_state
)),
1660 core_alua_dump_status(
1661 tg_pt_gp
->tg_pt_gp_alua_access_status
),
1662 (atomic_read(&port
->sep_tg_pt_secondary_offline
)) ?
1664 core_alua_dump_status(port
->sep_tg_pt_secondary_stat
));
1666 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1671 ssize_t
core_alua_store_tg_pt_gp_info(
1672 struct se_port
*port
,
1676 struct se_portal_group
*tpg
;
1678 struct se_subsystem_dev
*su_dev
= port
->sep_lun
->lun_se_dev
->se_sub_dev
;
1679 struct t10_alua_tg_pt_gp
*tg_pt_gp
= NULL
, *tg_pt_gp_new
= NULL
;
1680 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1681 unsigned char buf
[TG_PT_GROUP_NAME_BUF
];
1684 tpg
= port
->sep_tpg
;
1685 lun
= port
->sep_lun
;
1687 if (su_dev
->t10_alua
.alua_type
!= SPC3_ALUA_EMULATED
) {
1688 pr_warn("SPC3_ALUA_EMULATED not enabled for"
1689 " %s/tpgt_%hu/%s\n", tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1690 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1691 config_item_name(&lun
->lun_group
.cg_item
));
1695 if (count
> TG_PT_GROUP_NAME_BUF
) {
1696 pr_err("ALUA Target Port Group alias too large!\n");
1699 memset(buf
, 0, TG_PT_GROUP_NAME_BUF
);
1700 memcpy(buf
, page
, count
);
1702 * Any ALUA target port group alias besides "NULL" means we will be
1703 * making a new group association.
1705 if (strcmp(strstrip(buf
), "NULL")) {
1707 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1708 * struct t10_alua_tg_pt_gp. This reference is released with
1709 * core_alua_put_tg_pt_gp_from_name() below.
1711 tg_pt_gp_new
= core_alua_get_tg_pt_gp_by_name(su_dev
,
1716 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
1717 if (!tg_pt_gp_mem
) {
1719 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new
);
1720 pr_err("NULL struct se_port->sep_alua_tg_pt_gp_mem pointer\n");
1724 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1725 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
1728 * Clearing an existing tg_pt_gp association, and replacing
1729 * with the default_tg_pt_gp.
1731 if (!tg_pt_gp_new
) {
1732 pr_debug("Target_Core_ConfigFS: Moving"
1733 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1734 " alua/%s, ID: %hu back to"
1735 " default_tg_pt_gp\n",
1736 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1737 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1738 config_item_name(&lun
->lun_group
.cg_item
),
1740 &tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1741 tg_pt_gp
->tg_pt_gp_id
);
1743 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem
, tg_pt_gp
);
1744 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem
,
1745 su_dev
->t10_alua
.default_tg_pt_gp
);
1746 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1751 * Removing existing association of tg_pt_gp_mem with tg_pt_gp
1753 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem
, tg_pt_gp
);
1757 * Associate tg_pt_gp_mem with tg_pt_gp_new.
1759 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem
, tg_pt_gp_new
);
1760 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1761 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
1762 " Target Port Group: alua/%s, ID: %hu\n", (move
) ?
1763 "Moving" : "Adding", tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1764 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1765 config_item_name(&lun
->lun_group
.cg_item
),
1766 config_item_name(&tg_pt_gp_new
->tg_pt_gp_group
.cg_item
),
1767 tg_pt_gp_new
->tg_pt_gp_id
);
1769 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new
);
1773 ssize_t
core_alua_show_access_type(
1774 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1777 if ((tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICT_ALUA
) &&
1778 (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_IMPLICT_ALUA
))
1779 return sprintf(page
, "Implict and Explict\n");
1780 else if (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_IMPLICT_ALUA
)
1781 return sprintf(page
, "Implict\n");
1782 else if (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICT_ALUA
)
1783 return sprintf(page
, "Explict\n");
1785 return sprintf(page
, "None\n");
1788 ssize_t
core_alua_store_access_type(
1789 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1796 ret
= strict_strtoul(page
, 0, &tmp
);
1798 pr_err("Unable to extract alua_access_type\n");
1801 if ((tmp
!= 0) && (tmp
!= 1) && (tmp
!= 2) && (tmp
!= 3)) {
1802 pr_err("Illegal value for alua_access_type:"
1807 tg_pt_gp
->tg_pt_gp_alua_access_type
=
1808 TPGS_IMPLICT_ALUA
| TPGS_EXPLICT_ALUA
;
1810 tg_pt_gp
->tg_pt_gp_alua_access_type
= TPGS_EXPLICT_ALUA
;
1812 tg_pt_gp
->tg_pt_gp_alua_access_type
= TPGS_IMPLICT_ALUA
;
1814 tg_pt_gp
->tg_pt_gp_alua_access_type
= 0;
1819 ssize_t
core_alua_show_nonop_delay_msecs(
1820 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1823 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
);
1826 ssize_t
core_alua_store_nonop_delay_msecs(
1827 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1834 ret
= strict_strtoul(page
, 0, &tmp
);
1836 pr_err("Unable to extract nonop_delay_msecs\n");
1839 if (tmp
> ALUA_MAX_NONOP_DELAY_MSECS
) {
1840 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
1841 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp
,
1842 ALUA_MAX_NONOP_DELAY_MSECS
);
1845 tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
= (int)tmp
;
1850 ssize_t
core_alua_show_trans_delay_msecs(
1851 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1854 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_trans_delay_msecs
);
1857 ssize_t
core_alua_store_trans_delay_msecs(
1858 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1865 ret
= strict_strtoul(page
, 0, &tmp
);
1867 pr_err("Unable to extract trans_delay_msecs\n");
1870 if (tmp
> ALUA_MAX_TRANS_DELAY_MSECS
) {
1871 pr_err("Passed trans_delay_msecs: %lu, exceeds"
1872 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp
,
1873 ALUA_MAX_TRANS_DELAY_MSECS
);
1876 tg_pt_gp
->tg_pt_gp_trans_delay_msecs
= (int)tmp
;
1881 ssize_t
core_alua_show_implict_trans_secs(
1882 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1885 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_implict_trans_secs
);
1888 ssize_t
core_alua_store_implict_trans_secs(
1889 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1896 ret
= strict_strtoul(page
, 0, &tmp
);
1898 pr_err("Unable to extract implict_trans_secs\n");
1901 if (tmp
> ALUA_MAX_IMPLICT_TRANS_SECS
) {
1902 pr_err("Passed implict_trans_secs: %lu, exceeds"
1903 " ALUA_MAX_IMPLICT_TRANS_SECS: %d\n", tmp
,
1904 ALUA_MAX_IMPLICT_TRANS_SECS
);
1907 tg_pt_gp
->tg_pt_gp_implict_trans_secs
= (int)tmp
;
1912 ssize_t
core_alua_show_preferred_bit(
1913 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1916 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_pref
);
1919 ssize_t
core_alua_store_preferred_bit(
1920 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1927 ret
= strict_strtoul(page
, 0, &tmp
);
1929 pr_err("Unable to extract preferred ALUA value\n");
1932 if ((tmp
!= 0) && (tmp
!= 1)) {
1933 pr_err("Illegal value for preferred ALUA: %lu\n", tmp
);
1936 tg_pt_gp
->tg_pt_gp_pref
= (int)tmp
;
1941 ssize_t
core_alua_show_offline_bit(struct se_lun
*lun
, char *page
)
1946 return sprintf(page
, "%d\n",
1947 atomic_read(&lun
->lun_sep
->sep_tg_pt_secondary_offline
));
1950 ssize_t
core_alua_store_offline_bit(
1955 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1962 ret
= strict_strtoul(page
, 0, &tmp
);
1964 pr_err("Unable to extract alua_tg_pt_offline value\n");
1967 if ((tmp
!= 0) && (tmp
!= 1)) {
1968 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
1972 tg_pt_gp_mem
= lun
->lun_sep
->sep_alua_tg_pt_gp_mem
;
1973 if (!tg_pt_gp_mem
) {
1974 pr_err("Unable to locate *tg_pt_gp_mem\n");
1978 ret
= core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem
,
1979 lun
->lun_sep
, 0, (int)tmp
);
1986 ssize_t
core_alua_show_secondary_status(
1990 return sprintf(page
, "%d\n", lun
->lun_sep
->sep_tg_pt_secondary_stat
);
1993 ssize_t
core_alua_store_secondary_status(
2001 ret
= strict_strtoul(page
, 0, &tmp
);
2003 pr_err("Unable to extract alua_tg_pt_status\n");
2006 if ((tmp
!= ALUA_STATUS_NONE
) &&
2007 (tmp
!= ALUA_STATUS_ALTERED_BY_EXPLICT_STPG
) &&
2008 (tmp
!= ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA
)) {
2009 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
2013 lun
->lun_sep
->sep_tg_pt_secondary_stat
= (int)tmp
;
2018 ssize_t
core_alua_show_secondary_write_metadata(
2022 return sprintf(page
, "%d\n",
2023 lun
->lun_sep
->sep_tg_pt_secondary_write_md
);
2026 ssize_t
core_alua_store_secondary_write_metadata(
2034 ret
= strict_strtoul(page
, 0, &tmp
);
2036 pr_err("Unable to extract alua_tg_pt_write_md\n");
2039 if ((tmp
!= 0) && (tmp
!= 1)) {
2040 pr_err("Illegal value for alua_tg_pt_write_md:"
2044 lun
->lun_sep
->sep_tg_pt_secondary_write_md
= (int)tmp
;
2049 int core_setup_alua(struct se_device
*dev
, int force_pt
)
2051 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
2052 struct t10_alua
*alua
= &su_dev
->t10_alua
;
2053 struct t10_alua_lu_gp_member
*lu_gp_mem
;
2055 * If this device is from Target_Core_Mod/pSCSI, use the ALUA logic
2056 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
2057 * cause a problem because libata and some SATA RAID HBAs appear
2058 * under Linux/SCSI, but emulate SCSI logic themselves.
2060 if (((dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
) &&
2061 !(dev
->se_sub_dev
->se_dev_attrib
.emulate_alua
)) || force_pt
) {
2062 alua
->alua_type
= SPC_ALUA_PASSTHROUGH
;
2063 alua
->alua_state_check
= &core_alua_state_check_nop
;
2064 pr_debug("%s: Using SPC_ALUA_PASSTHROUGH, no ALUA"
2065 " emulation\n", dev
->transport
->name
);
2069 * If SPC-3 or above is reported by real or emulated struct se_device,
2070 * use emulated ALUA.
2072 if (dev
->transport
->get_device_rev(dev
) >= SCSI_3
) {
2073 pr_debug("%s: Enabling ALUA Emulation for SPC-3"
2074 " device\n", dev
->transport
->name
);
2076 * Associate this struct se_device with the default ALUA
2079 lu_gp_mem
= core_alua_allocate_lu_gp_mem(dev
);
2080 if (IS_ERR(lu_gp_mem
))
2081 return PTR_ERR(lu_gp_mem
);
2083 alua
->alua_type
= SPC3_ALUA_EMULATED
;
2084 alua
->alua_state_check
= &core_alua_state_check
;
2085 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
2086 __core_alua_attach_lu_gp_mem(lu_gp_mem
,
2088 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
2090 pr_debug("%s: Adding to default ALUA LU Group:"
2091 " core/alua/lu_gps/default_lu_gp\n",
2092 dev
->transport
->name
);
2094 alua
->alua_type
= SPC2_ALUA_DISABLED
;
2095 alua
->alua_state_check
= &core_alua_state_check_nop
;
2096 pr_debug("%s: Disabling ALUA Emulation for SPC-2"
2097 " device\n", dev
->transport
->name
);