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 if (cmd
->data_length
< 4) {
222 pr_warn("SET TARGET PORT GROUPS parameter list length %u too"
223 " small\n", cmd
->data_length
);
224 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
228 buf
= transport_kmap_data_sg(cmd
);
231 * Determine if explict ALUA via SET_TARGET_PORT_GROUPS is allowed
232 * for the local tg_pt_gp.
234 l_tg_pt_gp_mem
= l_port
->sep_alua_tg_pt_gp_mem
;
235 if (!l_tg_pt_gp_mem
) {
236 pr_err("Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
237 cmd
->scsi_sense_reason
= TCM_UNSUPPORTED_SCSI_OPCODE
;
241 spin_lock(&l_tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
242 l_tg_pt_gp
= l_tg_pt_gp_mem
->tg_pt_gp
;
244 spin_unlock(&l_tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
245 pr_err("Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n");
246 cmd
->scsi_sense_reason
= TCM_UNSUPPORTED_SCSI_OPCODE
;
250 rc
= (l_tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICT_ALUA
);
251 spin_unlock(&l_tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
254 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
255 " while TPGS_EXPLICT_ALUA is disabled\n");
256 cmd
->scsi_sense_reason
= TCM_UNSUPPORTED_SCSI_OPCODE
;
261 ptr
= &buf
[4]; /* Skip over RESERVED area in header */
263 while (len
< cmd
->data_length
) {
264 alua_access_state
= (ptr
[0] & 0x0f);
266 * Check the received ALUA access state, and determine if
267 * the state is a primary or secondary target port asymmetric
270 rc
= core_alua_check_transition(alua_access_state
, &primary
);
273 * If the SET TARGET PORT GROUPS attempts to establish
274 * an invalid combination of target port asymmetric
275 * access states or attempts to establish an
276 * unsupported target port asymmetric access state,
277 * then the command shall be terminated with CHECK
278 * CONDITION status, with the sense key set to ILLEGAL
279 * REQUEST, and the additional sense code set to INVALID
280 * FIELD IN PARAMETER LIST.
282 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
288 * If the ASYMMETRIC ACCESS STATE field (see table 267)
289 * specifies a primary target port asymmetric access state,
290 * then the TARGET PORT GROUP OR TARGET PORT field specifies
291 * a primary target port group for which the primary target
292 * port asymmetric access state shall be changed. If the
293 * ASYMMETRIC ACCESS STATE field specifies a secondary target
294 * port asymmetric access state, then the TARGET PORT GROUP OR
295 * TARGET PORT field specifies the relative target port
296 * identifier (see 3.1.120) of the target port for which the
297 * secondary target port asymmetric access state shall be
301 tg_pt_id
= get_unaligned_be16(ptr
+ 2);
303 * Locate the matching target port group ID from
304 * the global tg_pt_gp list
306 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
307 list_for_each_entry(tg_pt_gp
,
308 &su_dev
->t10_alua
.tg_pt_gps_list
,
310 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
313 if (tg_pt_id
!= tg_pt_gp
->tg_pt_gp_id
)
316 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
317 smp_mb__after_atomic_inc();
318 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
320 rc
= core_alua_do_port_transition(tg_pt_gp
,
322 alua_access_state
, 1);
324 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
325 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
326 smp_mb__after_atomic_dec();
329 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
331 * If not matching target port group ID can be located
332 * throw an exception with ASCQ: INVALID_PARAMETER_LIST
335 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
341 * Extact the RELATIVE TARGET PORT IDENTIFIER to identify
342 * the Target Port in question for the the incoming
343 * SET_TARGET_PORT_GROUPS op.
345 rtpi
= get_unaligned_be16(ptr
+ 2);
347 * Locate the matching relative target port identifer
348 * for the struct se_device storage object.
350 spin_lock(&dev
->se_port_lock
);
351 list_for_each_entry(port
, &dev
->dev_sep_list
,
353 if (port
->sep_rtpi
!= rtpi
)
356 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
357 spin_unlock(&dev
->se_port_lock
);
359 rc
= core_alua_set_tg_pt_secondary_state(
360 tg_pt_gp_mem
, port
, 1, 1);
362 spin_lock(&dev
->se_port_lock
);
365 spin_unlock(&dev
->se_port_lock
);
367 * If not matching relative target port identifier can
368 * be located, throw an exception with ASCQ:
369 * INVALID_PARAMETER_LIST
372 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
383 transport_kunmap_data_sg(cmd
);
385 target_complete_cmd(cmd
, GOOD
);
389 static inline int core_alua_state_nonoptimized(
392 int nonop_delay_msecs
,
396 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
397 * later to determine if processing of this cmd needs to be
398 * temporarily delayed for the Active/NonOptimized primary access state.
400 cmd
->se_cmd_flags
|= SCF_ALUA_NON_OPTIMIZED
;
401 cmd
->alua_nonop_delay
= nonop_delay_msecs
;
405 static inline int core_alua_state_standby(
411 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
412 * spc4r17 section 5.9.2.4.4
421 case RECEIVE_DIAGNOSTIC
:
422 case SEND_DIAGNOSTIC
:
424 switch (cdb
[1] & 0x1f) {
425 case MI_REPORT_TARGET_PGS
:
428 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_STANDBY
;
431 case MAINTENANCE_OUT
:
433 case MO_SET_TARGET_PGS
:
436 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_STANDBY
;
440 case PERSISTENT_RESERVE_IN
:
441 case PERSISTENT_RESERVE_OUT
:
446 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_STANDBY
;
453 static inline int core_alua_state_unavailable(
459 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
460 * spc4r17 section 5.9.2.4.5
466 switch (cdb
[1] & 0x1f) {
467 case MI_REPORT_TARGET_PGS
:
470 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
;
473 case MAINTENANCE_OUT
:
475 case MO_SET_TARGET_PGS
:
478 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
;
486 *alua_ascq
= ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
;
493 static inline int core_alua_state_transition(
499 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by
500 * spc4r17 section 5.9.2.5
506 switch (cdb
[1] & 0x1f) {
507 case MI_REPORT_TARGET_PGS
:
510 *alua_ascq
= ASCQ_04H_ALUA_STATE_TRANSITION
;
518 *alua_ascq
= ASCQ_04H_ALUA_STATE_TRANSITION
;
526 * Used for alua_type SPC_ALUA_PASSTHROUGH and SPC2_ALUA_DISABLED
527 * in transport_cmd_sequencer(). This function is assigned to
528 * struct t10_alua *->state_check() in core_setup_alua()
530 static int core_alua_state_check_nop(
539 * Used for alua_type SPC3_ALUA_EMULATED in transport_cmd_sequencer().
540 * This function is assigned to struct t10_alua *->state_check() in
543 * Also, this function can return three different return codes to
544 * signal transport_generic_cmd_sequencer()
546 * return 1: Is used to signal LUN not accecsable, and check condition/not ready
547 * return 0: Used to signal success
548 * reutrn -1: Used to signal failure, and invalid cdb field
550 static int core_alua_state_check(
555 struct se_lun
*lun
= cmd
->se_lun
;
556 struct se_port
*port
= lun
->lun_sep
;
557 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
558 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
559 int out_alua_state
, nonop_delay_msecs
;
564 * First, check for a struct se_port specific secondary ALUA target port
565 * access state: OFFLINE
567 if (atomic_read(&port
->sep_tg_pt_secondary_offline
)) {
568 *alua_ascq
= ASCQ_04H_ALUA_OFFLINE
;
569 pr_debug("ALUA: Got secondary offline status for local"
571 *alua_ascq
= ASCQ_04H_ALUA_OFFLINE
;
575 * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
576 * ALUA target port group, to obtain current ALUA access state.
577 * Otherwise look for the underlying struct se_device association with
578 * a ALUA logical unit group.
580 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
581 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
582 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
583 out_alua_state
= atomic_read(&tg_pt_gp
->tg_pt_gp_alua_access_state
);
584 nonop_delay_msecs
= tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
;
585 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
587 * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a separate conditional
588 * statement so the compiler knows explicitly to check this case first.
589 * For the Optimized ALUA access state case, we want to process the
590 * incoming fabric cmd ASAP..
592 if (out_alua_state
== ALUA_ACCESS_STATE_ACTIVE_OPTMIZED
)
595 switch (out_alua_state
) {
596 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
597 return core_alua_state_nonoptimized(cmd
, cdb
,
598 nonop_delay_msecs
, alua_ascq
);
599 case ALUA_ACCESS_STATE_STANDBY
:
600 return core_alua_state_standby(cmd
, cdb
, alua_ascq
);
601 case ALUA_ACCESS_STATE_UNAVAILABLE
:
602 return core_alua_state_unavailable(cmd
, cdb
, alua_ascq
);
603 case ALUA_ACCESS_STATE_TRANSITION
:
604 return core_alua_state_transition(cmd
, cdb
, alua_ascq
);
606 * OFFLINE is a secondary ALUA target port group access state, that is
607 * handled above with struct se_port->sep_tg_pt_secondary_offline=1
609 case ALUA_ACCESS_STATE_OFFLINE
:
611 pr_err("Unknown ALUA access state: 0x%02x\n",
620 * Check implict and explict ALUA state change request.
622 static int core_alua_check_transition(int state
, int *primary
)
625 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED
:
626 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
627 case ALUA_ACCESS_STATE_STANDBY
:
628 case ALUA_ACCESS_STATE_UNAVAILABLE
:
630 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
631 * defined as primary target port asymmetric access states.
635 case ALUA_ACCESS_STATE_OFFLINE
:
637 * OFFLINE state is defined as a secondary target port
638 * asymmetric access state.
643 pr_err("Unknown ALUA access state: 0x%02x\n", state
);
650 static char *core_alua_dump_state(int state
)
653 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED
:
654 return "Active/Optimized";
655 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
656 return "Active/NonOptimized";
657 case ALUA_ACCESS_STATE_STANDBY
:
659 case ALUA_ACCESS_STATE_UNAVAILABLE
:
660 return "Unavailable";
661 case ALUA_ACCESS_STATE_OFFLINE
:
670 char *core_alua_dump_status(int status
)
673 case ALUA_STATUS_NONE
:
675 case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG
:
676 return "Altered by Explict STPG";
677 case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA
:
678 return "Altered by Implict ALUA";
687 * Used by fabric modules to determine when we need to delay processing
688 * for the Active/NonOptimized paths..
690 int core_alua_check_nonop_delay(
693 if (!(cmd
->se_cmd_flags
& SCF_ALUA_NON_OPTIMIZED
))
698 * The ALUA Active/NonOptimized access state delay can be disabled
699 * in via configfs with a value of zero
701 if (!cmd
->alua_nonop_delay
)
704 * struct se_cmd->alua_nonop_delay gets set by a target port group
705 * defined interval in core_alua_state_nonoptimized()
707 msleep_interruptible(cmd
->alua_nonop_delay
);
710 EXPORT_SYMBOL(core_alua_check_nonop_delay
);
713 * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
716 static int core_alua_write_tpg_metadata(
718 unsigned char *md_buf
,
724 int flags
= O_RDWR
| O_CREAT
| O_TRUNC
, ret
;
726 memset(iov
, 0, sizeof(struct iovec
));
728 file
= filp_open(path
, flags
, 0600);
729 if (IS_ERR(file
) || !file
|| !file
->f_dentry
) {
730 pr_err("filp_open(%s) for ALUA metadata failed\n",
735 iov
[0].iov_base
= &md_buf
[0];
736 iov
[0].iov_len
= md_buf_len
;
740 ret
= vfs_writev(file
, &iov
[0], 1, &file
->f_pos
);
744 pr_err("Error writing ALUA metadata file: %s\n", path
);
745 filp_close(file
, NULL
);
748 filp_close(file
, NULL
);
754 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
756 static int core_alua_update_tpg_primary_metadata(
757 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
759 unsigned char *md_buf
)
761 struct se_subsystem_dev
*su_dev
= tg_pt_gp
->tg_pt_gp_su_dev
;
762 struct t10_wwn
*wwn
= &su_dev
->t10_wwn
;
763 char path
[ALUA_METADATA_PATH_LEN
];
766 memset(path
, 0, ALUA_METADATA_PATH_LEN
);
768 len
= snprintf(md_buf
, tg_pt_gp
->tg_pt_gp_md_buf_len
,
770 "alua_access_state=0x%02x\n"
771 "alua_access_status=0x%02x\n",
772 tg_pt_gp
->tg_pt_gp_id
, primary_state
,
773 tg_pt_gp
->tg_pt_gp_alua_access_status
);
775 snprintf(path
, ALUA_METADATA_PATH_LEN
,
776 "/var/target/alua/tpgs_%s/%s", &wwn
->unit_serial
[0],
777 config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
));
779 return core_alua_write_tpg_metadata(path
, md_buf
, len
);
782 static int core_alua_do_transition_tg_pt(
783 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
784 struct se_port
*l_port
,
785 struct se_node_acl
*nacl
,
786 unsigned char *md_buf
,
790 struct se_dev_entry
*se_deve
;
791 struct se_lun_acl
*lacl
;
792 struct se_port
*port
;
793 struct t10_alua_tg_pt_gp_member
*mem
;
796 * Save the old primary ALUA access state, and set the current state
797 * to ALUA_ACCESS_STATE_TRANSITION.
799 old_state
= atomic_read(&tg_pt_gp
->tg_pt_gp_alua_access_state
);
800 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
,
801 ALUA_ACCESS_STATE_TRANSITION
);
802 tg_pt_gp
->tg_pt_gp_alua_access_status
= (explict
) ?
803 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG
:
804 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA
;
806 * Check for the optional ALUA primary state transition delay
808 if (tg_pt_gp
->tg_pt_gp_trans_delay_msecs
!= 0)
809 msleep_interruptible(tg_pt_gp
->tg_pt_gp_trans_delay_msecs
);
811 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
812 list_for_each_entry(mem
, &tg_pt_gp
->tg_pt_gp_mem_list
,
816 * After an implicit target port asymmetric access state
817 * change, a device server shall establish a unit attention
818 * condition for the initiator port associated with every I_T
819 * nexus with the additional sense code set to ASYMMETRIC
820 * ACCESS STATE CHAGED.
822 * After an explicit target port asymmetric access state
823 * change, a device server shall establish a unit attention
824 * condition with the additional sense code set to ASYMMETRIC
825 * ACCESS STATE CHANGED for the initiator port associated with
826 * every I_T nexus other than the I_T nexus on which the SET
827 * TARGET PORT GROUPS command
829 atomic_inc(&mem
->tg_pt_gp_mem_ref_cnt
);
830 smp_mb__after_atomic_inc();
831 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
833 spin_lock_bh(&port
->sep_alua_lock
);
834 list_for_each_entry(se_deve
, &port
->sep_alua_list
,
836 lacl
= se_deve
->se_lun_acl
;
838 * se_deve->se_lun_acl pointer may be NULL for a
839 * entry created without explict Node+MappedLUN ACLs
845 (nacl
!= NULL
) && (nacl
== lacl
->se_lun_nacl
) &&
846 (l_port
!= NULL
) && (l_port
== port
))
849 core_scsi3_ua_allocate(lacl
->se_lun_nacl
,
850 se_deve
->mapped_lun
, 0x2A,
851 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED
);
853 spin_unlock_bh(&port
->sep_alua_lock
);
855 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
856 atomic_dec(&mem
->tg_pt_gp_mem_ref_cnt
);
857 smp_mb__after_atomic_dec();
859 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
861 * Update the ALUA metadata buf that has been allocated in
862 * core_alua_do_port_transition(), this metadata will be written
865 * Note that there is the case where we do not want to update the
866 * metadata when the saved metadata is being parsed in userspace
867 * when setting the existing port access state and access status.
869 * Also note that the failure to write out the ALUA metadata to
870 * struct file does NOT affect the actual ALUA transition.
872 if (tg_pt_gp
->tg_pt_gp_write_metadata
) {
873 mutex_lock(&tg_pt_gp
->tg_pt_gp_md_mutex
);
874 core_alua_update_tpg_primary_metadata(tg_pt_gp
,
876 mutex_unlock(&tg_pt_gp
->tg_pt_gp_md_mutex
);
879 * Set the current primary ALUA access state to the requested new state
881 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
, new_state
);
883 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
884 " from primary access state %s to %s\n", (explict
) ? "explict" :
885 "implict", config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
),
886 tg_pt_gp
->tg_pt_gp_id
, core_alua_dump_state(old_state
),
887 core_alua_dump_state(new_state
));
892 int core_alua_do_port_transition(
893 struct t10_alua_tg_pt_gp
*l_tg_pt_gp
,
894 struct se_device
*l_dev
,
895 struct se_port
*l_port
,
896 struct se_node_acl
*l_nacl
,
900 struct se_device
*dev
;
901 struct se_port
*port
;
902 struct se_subsystem_dev
*su_dev
;
903 struct se_node_acl
*nacl
;
904 struct t10_alua_lu_gp
*lu_gp
;
905 struct t10_alua_lu_gp_member
*lu_gp_mem
, *local_lu_gp_mem
;
906 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
907 unsigned char *md_buf
;
910 if (core_alua_check_transition(new_state
, &primary
) != 0)
913 md_buf
= kzalloc(l_tg_pt_gp
->tg_pt_gp_md_buf_len
, GFP_KERNEL
);
915 pr_err("Unable to allocate buf for ALUA metadata\n");
919 local_lu_gp_mem
= l_dev
->dev_alua_lu_gp_mem
;
920 spin_lock(&local_lu_gp_mem
->lu_gp_mem_lock
);
921 lu_gp
= local_lu_gp_mem
->lu_gp
;
922 atomic_inc(&lu_gp
->lu_gp_ref_cnt
);
923 smp_mb__after_atomic_inc();
924 spin_unlock(&local_lu_gp_mem
->lu_gp_mem_lock
);
926 * For storage objects that are members of the 'default_lu_gp',
927 * we only do transition on the passed *l_tp_pt_gp, and not
928 * on all of the matching target port groups IDs in default_lu_gp.
930 if (!lu_gp
->lu_gp_id
) {
932 * core_alua_do_transition_tg_pt() will always return
935 core_alua_do_transition_tg_pt(l_tg_pt_gp
, l_port
, l_nacl
,
936 md_buf
, new_state
, explict
);
937 atomic_dec(&lu_gp
->lu_gp_ref_cnt
);
938 smp_mb__after_atomic_dec();
943 * For all other LU groups aside from 'default_lu_gp', walk all of
944 * the associated storage objects looking for a matching target port
945 * group ID from the local target port group.
947 spin_lock(&lu_gp
->lu_gp_lock
);
948 list_for_each_entry(lu_gp_mem
, &lu_gp
->lu_gp_mem_list
,
951 dev
= lu_gp_mem
->lu_gp_mem_dev
;
952 su_dev
= dev
->se_sub_dev
;
953 atomic_inc(&lu_gp_mem
->lu_gp_mem_ref_cnt
);
954 smp_mb__after_atomic_inc();
955 spin_unlock(&lu_gp
->lu_gp_lock
);
957 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
958 list_for_each_entry(tg_pt_gp
,
959 &su_dev
->t10_alua
.tg_pt_gps_list
,
962 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
965 * If the target behavior port asymmetric access state
966 * is changed for any target port group accessiable via
967 * a logical unit within a LU group, the target port
968 * behavior group asymmetric access states for the same
969 * target port group accessible via other logical units
970 * in that LU group will also change.
972 if (l_tg_pt_gp
->tg_pt_gp_id
!= tg_pt_gp
->tg_pt_gp_id
)
975 if (l_tg_pt_gp
== tg_pt_gp
) {
982 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
983 smp_mb__after_atomic_inc();
984 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
986 * core_alua_do_transition_tg_pt() will always return
989 core_alua_do_transition_tg_pt(tg_pt_gp
, port
,
990 nacl
, md_buf
, new_state
, explict
);
992 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
993 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
994 smp_mb__after_atomic_dec();
996 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
998 spin_lock(&lu_gp
->lu_gp_lock
);
999 atomic_dec(&lu_gp_mem
->lu_gp_mem_ref_cnt
);
1000 smp_mb__after_atomic_dec();
1002 spin_unlock(&lu_gp
->lu_gp_lock
);
1004 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
1005 " Group IDs: %hu %s transition to primary state: %s\n",
1006 config_item_name(&lu_gp
->lu_gp_group
.cg_item
),
1007 l_tg_pt_gp
->tg_pt_gp_id
, (explict
) ? "explict" : "implict",
1008 core_alua_dump_state(new_state
));
1010 atomic_dec(&lu_gp
->lu_gp_ref_cnt
);
1011 smp_mb__after_atomic_dec();
1017 * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
1019 static int core_alua_update_tpg_secondary_metadata(
1020 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
1021 struct se_port
*port
,
1022 unsigned char *md_buf
,
1025 struct se_portal_group
*se_tpg
= port
->sep_tpg
;
1026 char path
[ALUA_METADATA_PATH_LEN
], wwn
[ALUA_SECONDARY_METADATA_WWN_LEN
];
1029 memset(path
, 0, ALUA_METADATA_PATH_LEN
);
1030 memset(wwn
, 0, ALUA_SECONDARY_METADATA_WWN_LEN
);
1032 len
= snprintf(wwn
, ALUA_SECONDARY_METADATA_WWN_LEN
, "%s",
1033 se_tpg
->se_tpg_tfo
->tpg_get_wwn(se_tpg
));
1035 if (se_tpg
->se_tpg_tfo
->tpg_get_tag
!= NULL
)
1036 snprintf(wwn
+len
, ALUA_SECONDARY_METADATA_WWN_LEN
-len
, "+%hu",
1037 se_tpg
->se_tpg_tfo
->tpg_get_tag(se_tpg
));
1039 len
= snprintf(md_buf
, md_buf_len
, "alua_tg_pt_offline=%d\n"
1040 "alua_tg_pt_status=0x%02x\n",
1041 atomic_read(&port
->sep_tg_pt_secondary_offline
),
1042 port
->sep_tg_pt_secondary_stat
);
1044 snprintf(path
, ALUA_METADATA_PATH_LEN
, "/var/target/alua/%s/%s/lun_%u",
1045 se_tpg
->se_tpg_tfo
->get_fabric_name(), wwn
,
1046 port
->sep_lun
->unpacked_lun
);
1048 return core_alua_write_tpg_metadata(path
, md_buf
, len
);
1051 static int core_alua_set_tg_pt_secondary_state(
1052 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
1053 struct se_port
*port
,
1057 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1058 unsigned char *md_buf
;
1060 int trans_delay_msecs
;
1062 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1063 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
1065 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1066 pr_err("Unable to complete secondary state"
1070 trans_delay_msecs
= tg_pt_gp
->tg_pt_gp_trans_delay_msecs
;
1072 * Set the secondary ALUA target port access state to OFFLINE
1073 * or release the previously secondary state for struct se_port
1076 atomic_set(&port
->sep_tg_pt_secondary_offline
, 1);
1078 atomic_set(&port
->sep_tg_pt_secondary_offline
, 0);
1080 md_buf_len
= tg_pt_gp
->tg_pt_gp_md_buf_len
;
1081 port
->sep_tg_pt_secondary_stat
= (explict
) ?
1082 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG
:
1083 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA
;
1085 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1086 " to secondary access state: %s\n", (explict
) ? "explict" :
1087 "implict", config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1088 tg_pt_gp
->tg_pt_gp_id
, (offline
) ? "OFFLINE" : "ONLINE");
1090 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1092 * Do the optional transition delay after we set the secondary
1093 * ALUA access state.
1095 if (trans_delay_msecs
!= 0)
1096 msleep_interruptible(trans_delay_msecs
);
1098 * See if we need to update the ALUA fabric port metadata for
1099 * secondary state and status
1101 if (port
->sep_tg_pt_secondary_write_md
) {
1102 md_buf
= kzalloc(md_buf_len
, GFP_KERNEL
);
1104 pr_err("Unable to allocate md_buf for"
1105 " secondary ALUA access metadata\n");
1108 mutex_lock(&port
->sep_tg_pt_md_mutex
);
1109 core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem
, port
,
1110 md_buf
, md_buf_len
);
1111 mutex_unlock(&port
->sep_tg_pt_md_mutex
);
1119 struct t10_alua_lu_gp
*
1120 core_alua_allocate_lu_gp(const char *name
, int def_group
)
1122 struct t10_alua_lu_gp
*lu_gp
;
1124 lu_gp
= kmem_cache_zalloc(t10_alua_lu_gp_cache
, GFP_KERNEL
);
1126 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1127 return ERR_PTR(-ENOMEM
);
1129 INIT_LIST_HEAD(&lu_gp
->lu_gp_node
);
1130 INIT_LIST_HEAD(&lu_gp
->lu_gp_mem_list
);
1131 spin_lock_init(&lu_gp
->lu_gp_lock
);
1132 atomic_set(&lu_gp
->lu_gp_ref_cnt
, 0);
1135 lu_gp
->lu_gp_id
= alua_lu_gps_counter
++;
1136 lu_gp
->lu_gp_valid_id
= 1;
1137 alua_lu_gps_count
++;
1143 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp
*lu_gp
, u16 lu_gp_id
)
1145 struct t10_alua_lu_gp
*lu_gp_tmp
;
1148 * The lu_gp->lu_gp_id may only be set once..
1150 if (lu_gp
->lu_gp_valid_id
) {
1151 pr_warn("ALUA LU Group already has a valid ID,"
1152 " ignoring request\n");
1156 spin_lock(&lu_gps_lock
);
1157 if (alua_lu_gps_count
== 0x0000ffff) {
1158 pr_err("Maximum ALUA alua_lu_gps_count:"
1159 " 0x0000ffff reached\n");
1160 spin_unlock(&lu_gps_lock
);
1161 kmem_cache_free(t10_alua_lu_gp_cache
, lu_gp
);
1165 lu_gp_id_tmp
= (lu_gp_id
!= 0) ? lu_gp_id
:
1166 alua_lu_gps_counter
++;
1168 list_for_each_entry(lu_gp_tmp
, &lu_gps_list
, lu_gp_node
) {
1169 if (lu_gp_tmp
->lu_gp_id
== lu_gp_id_tmp
) {
1173 pr_warn("ALUA Logical Unit Group ID: %hu"
1174 " already exists, ignoring request\n",
1176 spin_unlock(&lu_gps_lock
);
1181 lu_gp
->lu_gp_id
= lu_gp_id_tmp
;
1182 lu_gp
->lu_gp_valid_id
= 1;
1183 list_add_tail(&lu_gp
->lu_gp_node
, &lu_gps_list
);
1184 alua_lu_gps_count
++;
1185 spin_unlock(&lu_gps_lock
);
1190 static struct t10_alua_lu_gp_member
*
1191 core_alua_allocate_lu_gp_mem(struct se_device
*dev
)
1193 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1195 lu_gp_mem
= kmem_cache_zalloc(t10_alua_lu_gp_mem_cache
, GFP_KERNEL
);
1197 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1198 return ERR_PTR(-ENOMEM
);
1200 INIT_LIST_HEAD(&lu_gp_mem
->lu_gp_mem_list
);
1201 spin_lock_init(&lu_gp_mem
->lu_gp_mem_lock
);
1202 atomic_set(&lu_gp_mem
->lu_gp_mem_ref_cnt
, 0);
1204 lu_gp_mem
->lu_gp_mem_dev
= dev
;
1205 dev
->dev_alua_lu_gp_mem
= lu_gp_mem
;
1210 void core_alua_free_lu_gp(struct t10_alua_lu_gp
*lu_gp
)
1212 struct t10_alua_lu_gp_member
*lu_gp_mem
, *lu_gp_mem_tmp
;
1214 * Once we have reached this point, config_item_put() has
1215 * already been called from target_core_alua_drop_lu_gp().
1217 * Here, we remove the *lu_gp from the global list so that
1218 * no associations can be made while we are releasing
1219 * struct t10_alua_lu_gp.
1221 spin_lock(&lu_gps_lock
);
1222 list_del(&lu_gp
->lu_gp_node
);
1223 alua_lu_gps_count
--;
1224 spin_unlock(&lu_gps_lock
);
1226 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1227 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1228 * released with core_alua_put_lu_gp_from_name()
1230 while (atomic_read(&lu_gp
->lu_gp_ref_cnt
))
1233 * Release reference to struct t10_alua_lu_gp * from all associated
1236 spin_lock(&lu_gp
->lu_gp_lock
);
1237 list_for_each_entry_safe(lu_gp_mem
, lu_gp_mem_tmp
,
1238 &lu_gp
->lu_gp_mem_list
, lu_gp_mem_list
) {
1239 if (lu_gp_mem
->lu_gp_assoc
) {
1240 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1241 lu_gp
->lu_gp_members
--;
1242 lu_gp_mem
->lu_gp_assoc
= 0;
1244 spin_unlock(&lu_gp
->lu_gp_lock
);
1247 * lu_gp_mem is associated with a single
1248 * struct se_device->dev_alua_lu_gp_mem, and is released when
1249 * struct se_device is released via core_alua_free_lu_gp_mem().
1251 * If the passed lu_gp does NOT match the default_lu_gp, assume
1252 * we want to re-assocate a given lu_gp_mem with default_lu_gp.
1254 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1255 if (lu_gp
!= default_lu_gp
)
1256 __core_alua_attach_lu_gp_mem(lu_gp_mem
,
1259 lu_gp_mem
->lu_gp
= NULL
;
1260 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1262 spin_lock(&lu_gp
->lu_gp_lock
);
1264 spin_unlock(&lu_gp
->lu_gp_lock
);
1266 kmem_cache_free(t10_alua_lu_gp_cache
, lu_gp
);
1269 void core_alua_free_lu_gp_mem(struct se_device
*dev
)
1271 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
1272 struct t10_alua
*alua
= &su_dev
->t10_alua
;
1273 struct t10_alua_lu_gp
*lu_gp
;
1274 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1276 if (alua
->alua_type
!= SPC3_ALUA_EMULATED
)
1279 lu_gp_mem
= dev
->dev_alua_lu_gp_mem
;
1283 while (atomic_read(&lu_gp_mem
->lu_gp_mem_ref_cnt
))
1286 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1287 lu_gp
= lu_gp_mem
->lu_gp
;
1289 spin_lock(&lu_gp
->lu_gp_lock
);
1290 if (lu_gp_mem
->lu_gp_assoc
) {
1291 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1292 lu_gp
->lu_gp_members
--;
1293 lu_gp_mem
->lu_gp_assoc
= 0;
1295 spin_unlock(&lu_gp
->lu_gp_lock
);
1296 lu_gp_mem
->lu_gp
= NULL
;
1298 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1300 kmem_cache_free(t10_alua_lu_gp_mem_cache
, lu_gp_mem
);
1303 struct t10_alua_lu_gp
*core_alua_get_lu_gp_by_name(const char *name
)
1305 struct t10_alua_lu_gp
*lu_gp
;
1306 struct config_item
*ci
;
1308 spin_lock(&lu_gps_lock
);
1309 list_for_each_entry(lu_gp
, &lu_gps_list
, lu_gp_node
) {
1310 if (!lu_gp
->lu_gp_valid_id
)
1312 ci
= &lu_gp
->lu_gp_group
.cg_item
;
1313 if (!strcmp(config_item_name(ci
), name
)) {
1314 atomic_inc(&lu_gp
->lu_gp_ref_cnt
);
1315 spin_unlock(&lu_gps_lock
);
1319 spin_unlock(&lu_gps_lock
);
1324 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp
*lu_gp
)
1326 spin_lock(&lu_gps_lock
);
1327 atomic_dec(&lu_gp
->lu_gp_ref_cnt
);
1328 spin_unlock(&lu_gps_lock
);
1332 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1334 void __core_alua_attach_lu_gp_mem(
1335 struct t10_alua_lu_gp_member
*lu_gp_mem
,
1336 struct t10_alua_lu_gp
*lu_gp
)
1338 spin_lock(&lu_gp
->lu_gp_lock
);
1339 lu_gp_mem
->lu_gp
= lu_gp
;
1340 lu_gp_mem
->lu_gp_assoc
= 1;
1341 list_add_tail(&lu_gp_mem
->lu_gp_mem_list
, &lu_gp
->lu_gp_mem_list
);
1342 lu_gp
->lu_gp_members
++;
1343 spin_unlock(&lu_gp
->lu_gp_lock
);
1347 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1349 void __core_alua_drop_lu_gp_mem(
1350 struct t10_alua_lu_gp_member
*lu_gp_mem
,
1351 struct t10_alua_lu_gp
*lu_gp
)
1353 spin_lock(&lu_gp
->lu_gp_lock
);
1354 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1355 lu_gp_mem
->lu_gp
= NULL
;
1356 lu_gp_mem
->lu_gp_assoc
= 0;
1357 lu_gp
->lu_gp_members
--;
1358 spin_unlock(&lu_gp
->lu_gp_lock
);
1361 struct t10_alua_tg_pt_gp
*core_alua_allocate_tg_pt_gp(
1362 struct se_subsystem_dev
*su_dev
,
1366 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1368 tg_pt_gp
= kmem_cache_zalloc(t10_alua_tg_pt_gp_cache
, GFP_KERNEL
);
1370 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1373 INIT_LIST_HEAD(&tg_pt_gp
->tg_pt_gp_list
);
1374 INIT_LIST_HEAD(&tg_pt_gp
->tg_pt_gp_mem_list
);
1375 mutex_init(&tg_pt_gp
->tg_pt_gp_md_mutex
);
1376 spin_lock_init(&tg_pt_gp
->tg_pt_gp_lock
);
1377 atomic_set(&tg_pt_gp
->tg_pt_gp_ref_cnt
, 0);
1378 tg_pt_gp
->tg_pt_gp_su_dev
= su_dev
;
1379 tg_pt_gp
->tg_pt_gp_md_buf_len
= ALUA_MD_BUF_LEN
;
1380 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
,
1381 ALUA_ACCESS_STATE_ACTIVE_OPTMIZED
);
1383 * Enable both explict and implict ALUA support by default
1385 tg_pt_gp
->tg_pt_gp_alua_access_type
=
1386 TPGS_EXPLICT_ALUA
| TPGS_IMPLICT_ALUA
;
1388 * Set the default Active/NonOptimized Delay in milliseconds
1390 tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
= ALUA_DEFAULT_NONOP_DELAY_MSECS
;
1391 tg_pt_gp
->tg_pt_gp_trans_delay_msecs
= ALUA_DEFAULT_TRANS_DELAY_MSECS
;
1392 tg_pt_gp
->tg_pt_gp_implict_trans_secs
= ALUA_DEFAULT_IMPLICT_TRANS_SECS
;
1395 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1396 tg_pt_gp
->tg_pt_gp_id
=
1397 su_dev
->t10_alua
.alua_tg_pt_gps_counter
++;
1398 tg_pt_gp
->tg_pt_gp_valid_id
= 1;
1399 su_dev
->t10_alua
.alua_tg_pt_gps_count
++;
1400 list_add_tail(&tg_pt_gp
->tg_pt_gp_list
,
1401 &su_dev
->t10_alua
.tg_pt_gps_list
);
1402 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1408 int core_alua_set_tg_pt_gp_id(
1409 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1412 struct se_subsystem_dev
*su_dev
= tg_pt_gp
->tg_pt_gp_su_dev
;
1413 struct t10_alua_tg_pt_gp
*tg_pt_gp_tmp
;
1414 u16 tg_pt_gp_id_tmp
;
1416 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1418 if (tg_pt_gp
->tg_pt_gp_valid_id
) {
1419 pr_warn("ALUA TG PT Group already has a valid ID,"
1420 " ignoring request\n");
1424 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1425 if (su_dev
->t10_alua
.alua_tg_pt_gps_count
== 0x0000ffff) {
1426 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1427 " 0x0000ffff reached\n");
1428 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1429 kmem_cache_free(t10_alua_tg_pt_gp_cache
, tg_pt_gp
);
1433 tg_pt_gp_id_tmp
= (tg_pt_gp_id
!= 0) ? tg_pt_gp_id
:
1434 su_dev
->t10_alua
.alua_tg_pt_gps_counter
++;
1436 list_for_each_entry(tg_pt_gp_tmp
, &su_dev
->t10_alua
.tg_pt_gps_list
,
1438 if (tg_pt_gp_tmp
->tg_pt_gp_id
== tg_pt_gp_id_tmp
) {
1442 pr_err("ALUA Target Port Group ID: %hu already"
1443 " exists, ignoring request\n", tg_pt_gp_id
);
1444 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1449 tg_pt_gp
->tg_pt_gp_id
= tg_pt_gp_id_tmp
;
1450 tg_pt_gp
->tg_pt_gp_valid_id
= 1;
1451 list_add_tail(&tg_pt_gp
->tg_pt_gp_list
,
1452 &su_dev
->t10_alua
.tg_pt_gps_list
);
1453 su_dev
->t10_alua
.alua_tg_pt_gps_count
++;
1454 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1459 struct t10_alua_tg_pt_gp_member
*core_alua_allocate_tg_pt_gp_mem(
1460 struct se_port
*port
)
1462 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1464 tg_pt_gp_mem
= kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache
,
1466 if (!tg_pt_gp_mem
) {
1467 pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n");
1468 return ERR_PTR(-ENOMEM
);
1470 INIT_LIST_HEAD(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1471 spin_lock_init(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1472 atomic_set(&tg_pt_gp_mem
->tg_pt_gp_mem_ref_cnt
, 0);
1474 tg_pt_gp_mem
->tg_pt
= port
;
1475 port
->sep_alua_tg_pt_gp_mem
= tg_pt_gp_mem
;
1477 return tg_pt_gp_mem
;
1480 void core_alua_free_tg_pt_gp(
1481 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1483 struct se_subsystem_dev
*su_dev
= tg_pt_gp
->tg_pt_gp_su_dev
;
1484 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
, *tg_pt_gp_mem_tmp
;
1486 * Once we have reached this point, config_item_put() has already
1487 * been called from target_core_alua_drop_tg_pt_gp().
1489 * Here we remove *tg_pt_gp from the global list so that
1490 * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS
1491 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1493 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1494 list_del(&tg_pt_gp
->tg_pt_gp_list
);
1495 su_dev
->t10_alua
.alua_tg_pt_gps_counter
--;
1496 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1498 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1499 * core_alua_get_tg_pt_gp_by_name() in
1500 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1501 * to be released with core_alua_put_tg_pt_gp_from_name().
1503 while (atomic_read(&tg_pt_gp
->tg_pt_gp_ref_cnt
))
1506 * Release reference to struct t10_alua_tg_pt_gp from all associated
1509 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1510 list_for_each_entry_safe(tg_pt_gp_mem
, tg_pt_gp_mem_tmp
,
1511 &tg_pt_gp
->tg_pt_gp_mem_list
, tg_pt_gp_mem_list
) {
1512 if (tg_pt_gp_mem
->tg_pt_gp_assoc
) {
1513 list_del(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1514 tg_pt_gp
->tg_pt_gp_members
--;
1515 tg_pt_gp_mem
->tg_pt_gp_assoc
= 0;
1517 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1519 * tg_pt_gp_mem is associated with a single
1520 * se_port->sep_alua_tg_pt_gp_mem, and is released via
1521 * core_alua_free_tg_pt_gp_mem().
1523 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1524 * assume we want to re-assocate a given tg_pt_gp_mem with
1527 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1528 if (tg_pt_gp
!= su_dev
->t10_alua
.default_tg_pt_gp
) {
1529 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem
,
1530 su_dev
->t10_alua
.default_tg_pt_gp
);
1532 tg_pt_gp_mem
->tg_pt_gp
= NULL
;
1533 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1535 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1537 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1539 kmem_cache_free(t10_alua_tg_pt_gp_cache
, tg_pt_gp
);
1542 void core_alua_free_tg_pt_gp_mem(struct se_port
*port
)
1544 struct se_subsystem_dev
*su_dev
= port
->sep_lun
->lun_se_dev
->se_sub_dev
;
1545 struct t10_alua
*alua
= &su_dev
->t10_alua
;
1546 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1547 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1549 if (alua
->alua_type
!= SPC3_ALUA_EMULATED
)
1552 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
1556 while (atomic_read(&tg_pt_gp_mem
->tg_pt_gp_mem_ref_cnt
))
1559 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1560 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
1562 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1563 if (tg_pt_gp_mem
->tg_pt_gp_assoc
) {
1564 list_del(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1565 tg_pt_gp
->tg_pt_gp_members
--;
1566 tg_pt_gp_mem
->tg_pt_gp_assoc
= 0;
1568 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1569 tg_pt_gp_mem
->tg_pt_gp
= NULL
;
1571 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1573 kmem_cache_free(t10_alua_tg_pt_gp_mem_cache
, tg_pt_gp_mem
);
1576 static struct t10_alua_tg_pt_gp
*core_alua_get_tg_pt_gp_by_name(
1577 struct se_subsystem_dev
*su_dev
,
1580 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1581 struct config_item
*ci
;
1583 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1584 list_for_each_entry(tg_pt_gp
, &su_dev
->t10_alua
.tg_pt_gps_list
,
1586 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
1588 ci
= &tg_pt_gp
->tg_pt_gp_group
.cg_item
;
1589 if (!strcmp(config_item_name(ci
), name
)) {
1590 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1591 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1595 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1600 static void core_alua_put_tg_pt_gp_from_name(
1601 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1603 struct se_subsystem_dev
*su_dev
= tg_pt_gp
->tg_pt_gp_su_dev
;
1605 spin_lock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1606 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1607 spin_unlock(&su_dev
->t10_alua
.tg_pt_gps_lock
);
1611 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1613 void __core_alua_attach_tg_pt_gp_mem(
1614 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
1615 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1617 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1618 tg_pt_gp_mem
->tg_pt_gp
= tg_pt_gp
;
1619 tg_pt_gp_mem
->tg_pt_gp_assoc
= 1;
1620 list_add_tail(&tg_pt_gp_mem
->tg_pt_gp_mem_list
,
1621 &tg_pt_gp
->tg_pt_gp_mem_list
);
1622 tg_pt_gp
->tg_pt_gp_members
++;
1623 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1627 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1629 static void __core_alua_drop_tg_pt_gp_mem(
1630 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
1631 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1633 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1634 list_del(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1635 tg_pt_gp_mem
->tg_pt_gp
= NULL
;
1636 tg_pt_gp_mem
->tg_pt_gp_assoc
= 0;
1637 tg_pt_gp
->tg_pt_gp_members
--;
1638 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1641 ssize_t
core_alua_show_tg_pt_gp_info(struct se_port
*port
, char *page
)
1643 struct se_subsystem_dev
*su_dev
= port
->sep_lun
->lun_se_dev
->se_sub_dev
;
1644 struct config_item
*tg_pt_ci
;
1645 struct t10_alua
*alua
= &su_dev
->t10_alua
;
1646 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1647 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1650 if (alua
->alua_type
!= SPC3_ALUA_EMULATED
)
1653 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
1657 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1658 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
1660 tg_pt_ci
= &tg_pt_gp
->tg_pt_gp_group
.cg_item
;
1661 len
+= sprintf(page
, "TG Port Alias: %s\nTG Port Group ID:"
1662 " %hu\nTG Port Primary Access State: %s\nTG Port "
1663 "Primary Access Status: %s\nTG Port Secondary Access"
1664 " State: %s\nTG Port Secondary Access Status: %s\n",
1665 config_item_name(tg_pt_ci
), tg_pt_gp
->tg_pt_gp_id
,
1666 core_alua_dump_state(atomic_read(
1667 &tg_pt_gp
->tg_pt_gp_alua_access_state
)),
1668 core_alua_dump_status(
1669 tg_pt_gp
->tg_pt_gp_alua_access_status
),
1670 (atomic_read(&port
->sep_tg_pt_secondary_offline
)) ?
1672 core_alua_dump_status(port
->sep_tg_pt_secondary_stat
));
1674 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1679 ssize_t
core_alua_store_tg_pt_gp_info(
1680 struct se_port
*port
,
1684 struct se_portal_group
*tpg
;
1686 struct se_subsystem_dev
*su_dev
= port
->sep_lun
->lun_se_dev
->se_sub_dev
;
1687 struct t10_alua_tg_pt_gp
*tg_pt_gp
= NULL
, *tg_pt_gp_new
= NULL
;
1688 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1689 unsigned char buf
[TG_PT_GROUP_NAME_BUF
];
1692 tpg
= port
->sep_tpg
;
1693 lun
= port
->sep_lun
;
1695 if (su_dev
->t10_alua
.alua_type
!= SPC3_ALUA_EMULATED
) {
1696 pr_warn("SPC3_ALUA_EMULATED not enabled for"
1697 " %s/tpgt_%hu/%s\n", tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1698 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1699 config_item_name(&lun
->lun_group
.cg_item
));
1703 if (count
> TG_PT_GROUP_NAME_BUF
) {
1704 pr_err("ALUA Target Port Group alias too large!\n");
1707 memset(buf
, 0, TG_PT_GROUP_NAME_BUF
);
1708 memcpy(buf
, page
, count
);
1710 * Any ALUA target port group alias besides "NULL" means we will be
1711 * making a new group association.
1713 if (strcmp(strstrip(buf
), "NULL")) {
1715 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1716 * struct t10_alua_tg_pt_gp. This reference is released with
1717 * core_alua_put_tg_pt_gp_from_name() below.
1719 tg_pt_gp_new
= core_alua_get_tg_pt_gp_by_name(su_dev
,
1724 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
1725 if (!tg_pt_gp_mem
) {
1727 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new
);
1728 pr_err("NULL struct se_port->sep_alua_tg_pt_gp_mem pointer\n");
1732 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1733 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
1736 * Clearing an existing tg_pt_gp association, and replacing
1737 * with the default_tg_pt_gp.
1739 if (!tg_pt_gp_new
) {
1740 pr_debug("Target_Core_ConfigFS: Moving"
1741 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1742 " alua/%s, ID: %hu back to"
1743 " default_tg_pt_gp\n",
1744 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1745 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1746 config_item_name(&lun
->lun_group
.cg_item
),
1748 &tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1749 tg_pt_gp
->tg_pt_gp_id
);
1751 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem
, tg_pt_gp
);
1752 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem
,
1753 su_dev
->t10_alua
.default_tg_pt_gp
);
1754 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1759 * Removing existing association of tg_pt_gp_mem with tg_pt_gp
1761 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem
, tg_pt_gp
);
1765 * Associate tg_pt_gp_mem with tg_pt_gp_new.
1767 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem
, tg_pt_gp_new
);
1768 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1769 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
1770 " Target Port Group: alua/%s, ID: %hu\n", (move
) ?
1771 "Moving" : "Adding", tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1772 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1773 config_item_name(&lun
->lun_group
.cg_item
),
1774 config_item_name(&tg_pt_gp_new
->tg_pt_gp_group
.cg_item
),
1775 tg_pt_gp_new
->tg_pt_gp_id
);
1777 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new
);
1781 ssize_t
core_alua_show_access_type(
1782 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1785 if ((tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICT_ALUA
) &&
1786 (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_IMPLICT_ALUA
))
1787 return sprintf(page
, "Implict and Explict\n");
1788 else if (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_IMPLICT_ALUA
)
1789 return sprintf(page
, "Implict\n");
1790 else if (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICT_ALUA
)
1791 return sprintf(page
, "Explict\n");
1793 return sprintf(page
, "None\n");
1796 ssize_t
core_alua_store_access_type(
1797 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1804 ret
= strict_strtoul(page
, 0, &tmp
);
1806 pr_err("Unable to extract alua_access_type\n");
1809 if ((tmp
!= 0) && (tmp
!= 1) && (tmp
!= 2) && (tmp
!= 3)) {
1810 pr_err("Illegal value for alua_access_type:"
1815 tg_pt_gp
->tg_pt_gp_alua_access_type
=
1816 TPGS_IMPLICT_ALUA
| TPGS_EXPLICT_ALUA
;
1818 tg_pt_gp
->tg_pt_gp_alua_access_type
= TPGS_EXPLICT_ALUA
;
1820 tg_pt_gp
->tg_pt_gp_alua_access_type
= TPGS_IMPLICT_ALUA
;
1822 tg_pt_gp
->tg_pt_gp_alua_access_type
= 0;
1827 ssize_t
core_alua_show_nonop_delay_msecs(
1828 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1831 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
);
1834 ssize_t
core_alua_store_nonop_delay_msecs(
1835 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1842 ret
= strict_strtoul(page
, 0, &tmp
);
1844 pr_err("Unable to extract nonop_delay_msecs\n");
1847 if (tmp
> ALUA_MAX_NONOP_DELAY_MSECS
) {
1848 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
1849 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp
,
1850 ALUA_MAX_NONOP_DELAY_MSECS
);
1853 tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
= (int)tmp
;
1858 ssize_t
core_alua_show_trans_delay_msecs(
1859 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1862 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_trans_delay_msecs
);
1865 ssize_t
core_alua_store_trans_delay_msecs(
1866 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1873 ret
= strict_strtoul(page
, 0, &tmp
);
1875 pr_err("Unable to extract trans_delay_msecs\n");
1878 if (tmp
> ALUA_MAX_TRANS_DELAY_MSECS
) {
1879 pr_err("Passed trans_delay_msecs: %lu, exceeds"
1880 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp
,
1881 ALUA_MAX_TRANS_DELAY_MSECS
);
1884 tg_pt_gp
->tg_pt_gp_trans_delay_msecs
= (int)tmp
;
1889 ssize_t
core_alua_show_implict_trans_secs(
1890 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1893 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_implict_trans_secs
);
1896 ssize_t
core_alua_store_implict_trans_secs(
1897 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1904 ret
= strict_strtoul(page
, 0, &tmp
);
1906 pr_err("Unable to extract implict_trans_secs\n");
1909 if (tmp
> ALUA_MAX_IMPLICT_TRANS_SECS
) {
1910 pr_err("Passed implict_trans_secs: %lu, exceeds"
1911 " ALUA_MAX_IMPLICT_TRANS_SECS: %d\n", tmp
,
1912 ALUA_MAX_IMPLICT_TRANS_SECS
);
1915 tg_pt_gp
->tg_pt_gp_implict_trans_secs
= (int)tmp
;
1920 ssize_t
core_alua_show_preferred_bit(
1921 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1924 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_pref
);
1927 ssize_t
core_alua_store_preferred_bit(
1928 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1935 ret
= strict_strtoul(page
, 0, &tmp
);
1937 pr_err("Unable to extract preferred ALUA value\n");
1940 if ((tmp
!= 0) && (tmp
!= 1)) {
1941 pr_err("Illegal value for preferred ALUA: %lu\n", tmp
);
1944 tg_pt_gp
->tg_pt_gp_pref
= (int)tmp
;
1949 ssize_t
core_alua_show_offline_bit(struct se_lun
*lun
, char *page
)
1954 return sprintf(page
, "%d\n",
1955 atomic_read(&lun
->lun_sep
->sep_tg_pt_secondary_offline
));
1958 ssize_t
core_alua_store_offline_bit(
1963 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1970 ret
= strict_strtoul(page
, 0, &tmp
);
1972 pr_err("Unable to extract alua_tg_pt_offline value\n");
1975 if ((tmp
!= 0) && (tmp
!= 1)) {
1976 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
1980 tg_pt_gp_mem
= lun
->lun_sep
->sep_alua_tg_pt_gp_mem
;
1981 if (!tg_pt_gp_mem
) {
1982 pr_err("Unable to locate *tg_pt_gp_mem\n");
1986 ret
= core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem
,
1987 lun
->lun_sep
, 0, (int)tmp
);
1994 ssize_t
core_alua_show_secondary_status(
1998 return sprintf(page
, "%d\n", lun
->lun_sep
->sep_tg_pt_secondary_stat
);
2001 ssize_t
core_alua_store_secondary_status(
2009 ret
= strict_strtoul(page
, 0, &tmp
);
2011 pr_err("Unable to extract alua_tg_pt_status\n");
2014 if ((tmp
!= ALUA_STATUS_NONE
) &&
2015 (tmp
!= ALUA_STATUS_ALTERED_BY_EXPLICT_STPG
) &&
2016 (tmp
!= ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA
)) {
2017 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
2021 lun
->lun_sep
->sep_tg_pt_secondary_stat
= (int)tmp
;
2026 ssize_t
core_alua_show_secondary_write_metadata(
2030 return sprintf(page
, "%d\n",
2031 lun
->lun_sep
->sep_tg_pt_secondary_write_md
);
2034 ssize_t
core_alua_store_secondary_write_metadata(
2042 ret
= strict_strtoul(page
, 0, &tmp
);
2044 pr_err("Unable to extract alua_tg_pt_write_md\n");
2047 if ((tmp
!= 0) && (tmp
!= 1)) {
2048 pr_err("Illegal value for alua_tg_pt_write_md:"
2052 lun
->lun_sep
->sep_tg_pt_secondary_write_md
= (int)tmp
;
2057 int core_setup_alua(struct se_device
*dev
, int force_pt
)
2059 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
2060 struct t10_alua
*alua
= &su_dev
->t10_alua
;
2061 struct t10_alua_lu_gp_member
*lu_gp_mem
;
2063 * If this device is from Target_Core_Mod/pSCSI, use the ALUA logic
2064 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
2065 * cause a problem because libata and some SATA RAID HBAs appear
2066 * under Linux/SCSI, but emulate SCSI logic themselves.
2068 if (((dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
) &&
2069 !(dev
->se_sub_dev
->se_dev_attrib
.emulate_alua
)) || force_pt
) {
2070 alua
->alua_type
= SPC_ALUA_PASSTHROUGH
;
2071 alua
->alua_state_check
= &core_alua_state_check_nop
;
2072 pr_debug("%s: Using SPC_ALUA_PASSTHROUGH, no ALUA"
2073 " emulation\n", dev
->transport
->name
);
2077 * If SPC-3 or above is reported by real or emulated struct se_device,
2078 * use emulated ALUA.
2080 if (dev
->transport
->get_device_rev(dev
) >= SCSI_3
) {
2081 pr_debug("%s: Enabling ALUA Emulation for SPC-3"
2082 " device\n", dev
->transport
->name
);
2084 * Associate this struct se_device with the default ALUA
2087 lu_gp_mem
= core_alua_allocate_lu_gp_mem(dev
);
2088 if (IS_ERR(lu_gp_mem
))
2089 return PTR_ERR(lu_gp_mem
);
2091 alua
->alua_type
= SPC3_ALUA_EMULATED
;
2092 alua
->alua_state_check
= &core_alua_state_check
;
2093 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
2094 __core_alua_attach_lu_gp_mem(lu_gp_mem
,
2096 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
2098 pr_debug("%s: Adding to default ALUA LU Group:"
2099 " core/alua/lu_gps/default_lu_gp\n",
2100 dev
->transport
->name
);
2102 alua
->alua_type
= SPC2_ALUA_DISABLED
;
2103 alua
->alua_state_check
= &core_alua_state_check_nop
;
2104 pr_debug("%s: Disabling ALUA Emulation for SPC-2"
2105 " device\n", dev
->transport
->name
);