1 /*******************************************************************************
2 * Filename: target_core_alua.c
4 * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
6 * (c) Copyright 2009-2012 RisingTide Systems LLC.
8 * Nicholas A. Bellinger <nab@kernel.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 ******************************************************************************/
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <linux/configfs.h>
29 #include <linux/export.h>
30 #include <linux/file.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 sense_reason_t 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
63 target_emulate_report_target_port_groups(struct se_cmd *cmd)
65 struct se_device *dev = cmd->se_dev;
67 struct t10_alua_tg_pt_gp *tg_pt_gp;
68 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
71 int ext_hdr = (cmd->t_task_cdb[1] & 0x20);
74 * Skip over RESERVED area to first Target port group descriptor
75 * depending on the PARAMETER DATA FORMAT type..
82 if (cmd->data_length < off) {
83 pr_warn("REPORT TARGET PORT GROUPS allocation length %u too"
84 " small for %s header\n", cmd->data_length,
85 (ext_hdr) ? "extended" : "normal");
86 return TCM_INVALID_CDB_FIELD;
88 buf = transport_kmap_data_sg(cmd);
90 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
92 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
93 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
96 * Check if the Target port group and Target port descriptor list
97 * based on tg_pt_gp_members count will fit into the response payload.
98 * Otherwise, bump rd_len to let the initiator know we have exceeded
99 * the allocation length and the response is truncated.
101 if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) >
103 rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4);
107 * PREF: Preferred target port bit, determine if this
108 * bit should be set for port group.
110 if (tg_pt_gp->tg_pt_gp_pref)
113 * Set the ASYMMETRIC ACCESS State
115 buf[off++] |= (atomic_read(
116 &tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff);
118 * Set supported ASYMMETRIC ACCESS State bits
120 buf[off] = 0x80; /* T_SUP */
121 buf[off] |= 0x40; /* O_SUP */
122 buf[off] |= 0x8; /* U_SUP */
123 buf[off] |= 0x4; /* S_SUP */
124 buf[off] |= 0x2; /* AN_SUP */
125 buf[off++] |= 0x1; /* AO_SUP */
129 buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff);
130 buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff);
132 off++; /* Skip over Reserved */
136 buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
138 * Vendor Specific field
144 buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
147 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
148 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
150 port = tg_pt_gp_mem->tg_pt;
152 * Start Target Port descriptor format
154 * See spc4r17 section 6.2.7 Table 247
156 off += 2; /* Skip over Obsolete */
158 * Set RELATIVE TARGET PORT IDENTIFIER
160 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
161 buf[off++] = (port->sep_rtpi & 0xff);
164 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
166 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
168 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
170 put_unaligned_be32(rd_len, &buf[0]);
173 * Fill in the Extended header parameter data format if requested
178 * Set the implict transition time (in seconds) for the application
179 * client to use as a base for it's transition timeout value.
181 * Use the current tg_pt_gp_mem -> tg_pt_gp membership from the LUN
182 * this CDB was received upon to determine this value individually
183 * for ALUA target port group.
185 port = cmd->se_lun->lun_sep;
186 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
188 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
189 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
191 buf[5] = tg_pt_gp->tg_pt_gp_implict_trans_secs;
192 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
195 transport_kunmap_data_sg(cmd);
197 target_complete_cmd(cmd, GOOD);
202 * SET_TARGET_PORT_GROUPS for explict ALUA operation.
204 * See spc4r17 section 6.35
207 target_emulate_set_target_port_groups(struct se_cmd *cmd)
209 struct se_device *dev = cmd->se_dev;
210 struct se_port *port, *l_port = cmd->se_lun->lun_sep;
211 struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
212 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
213 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *l_tg_pt_gp_mem;
216 sense_reason_t rc = TCM_NO_SENSE;
217 u32 len = 4; /* Skip over RESERVED area in header */
218 int alua_access_state, primary = 0;
222 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
224 if (cmd->data_length < 4) {
225 pr_warn("SET TARGET PORT GROUPS parameter list length %u too"
226 " small\n", cmd->data_length);
227 return TCM_INVALID_PARAMETER_LIST;
230 buf = transport_kmap_data_sg(cmd);
232 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
235 * Determine if explict ALUA via SET_TARGET_PORT_GROUPS is allowed
236 * for the local tg_pt_gp.
238 l_tg_pt_gp_mem = l_port->sep_alua_tg_pt_gp_mem;
239 if (!l_tg_pt_gp_mem) {
240 pr_err("Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
241 rc = TCM_UNSUPPORTED_SCSI_OPCODE;
244 spin_lock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
245 l_tg_pt_gp = l_tg_pt_gp_mem->tg_pt_gp;
247 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
248 pr_err("Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n");
249 rc = TCM_UNSUPPORTED_SCSI_OPCODE;
252 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
254 if (!(l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA)) {
255 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
256 " while TPGS_EXPLICT_ALUA is disabled\n");
257 rc = TCM_UNSUPPORTED_SCSI_OPCODE;
261 ptr = &buf[4]; /* Skip over RESERVED area in header */
263 while (len < cmd->data_length) {
265 alua_access_state = (ptr[0] & 0x0f);
267 * Check the received ALUA access state, and determine if
268 * the state is a primary or secondary target port asymmetric
271 rc = core_alua_check_transition(alua_access_state, &primary);
274 * If the SET TARGET PORT GROUPS attempts to establish
275 * an invalid combination of target port asymmetric
276 * access states or attempts to establish an
277 * unsupported target port asymmetric access state,
278 * then the command shall be terminated with CHECK
279 * CONDITION status, with the sense key set to ILLEGAL
280 * REQUEST, and the additional sense code set to INVALID
281 * FIELD IN PARAMETER LIST.
287 * If the ASYMMETRIC ACCESS STATE field (see table 267)
288 * specifies a primary target port asymmetric access state,
289 * then the TARGET PORT GROUP OR TARGET PORT field specifies
290 * a primary target port group for which the primary target
291 * port asymmetric access state shall be changed. If the
292 * ASYMMETRIC ACCESS STATE field specifies a secondary target
293 * port asymmetric access state, then the TARGET PORT GROUP OR
294 * TARGET PORT field specifies the relative target port
295 * identifier (see 3.1.120) of the target port for which the
296 * secondary target port asymmetric access state shall be
300 tg_pt_id = get_unaligned_be16(ptr + 2);
302 * Locate the matching target port group ID from
303 * the global tg_pt_gp list
305 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
306 list_for_each_entry(tg_pt_gp,
307 &dev->t10_alua.tg_pt_gps_list,
309 if (!tg_pt_gp->tg_pt_gp_valid_id)
312 if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
315 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
316 smp_mb__after_atomic_inc();
318 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
320 if (!core_alua_do_port_transition(tg_pt_gp,
322 alua_access_state, 1))
325 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
326 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
327 smp_mb__after_atomic_dec();
330 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
333 * Extact the RELATIVE TARGET PORT IDENTIFIER to identify
334 * the Target Port in question for the the incoming
335 * SET_TARGET_PORT_GROUPS op.
337 rtpi = get_unaligned_be16(ptr + 2);
339 * Locate the matching relative target port identifier
340 * for the struct se_device storage object.
342 spin_lock(&dev->se_port_lock);
343 list_for_each_entry(port, &dev->dev_sep_list,
345 if (port->sep_rtpi != rtpi)
348 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
350 spin_unlock(&dev->se_port_lock);
352 if (!core_alua_set_tg_pt_secondary_state(
353 tg_pt_gp_mem, port, 1, 1))
356 spin_lock(&dev->se_port_lock);
359 spin_unlock(&dev->se_port_lock);
363 rc = TCM_INVALID_PARAMETER_LIST;
372 transport_kunmap_data_sg(cmd);
374 target_complete_cmd(cmd, GOOD);
378 static inline int core_alua_state_nonoptimized(
381 int nonop_delay_msecs,
385 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
386 * later to determine if processing of this cmd needs to be
387 * temporarily delayed for the Active/NonOptimized primary access state.
389 cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
390 cmd->alua_nonop_delay = nonop_delay_msecs;
394 static inline int core_alua_state_standby(
400 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
401 * spc4r17 section 5.9.2.4.4
410 case RECEIVE_DIAGNOSTIC:
411 case SEND_DIAGNOSTIC:
414 case SERVICE_ACTION_IN:
415 switch (cdb[1] & 0x1f) {
416 case SAI_READ_CAPACITY_16:
419 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
423 switch (cdb[1] & 0x1f) {
424 case MI_REPORT_TARGET_PGS:
427 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
430 case MAINTENANCE_OUT:
432 case MO_SET_TARGET_PGS:
435 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
439 case PERSISTENT_RESERVE_IN:
440 case PERSISTENT_RESERVE_OUT:
445 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
452 static inline int core_alua_state_unavailable(
458 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
459 * spc4r17 section 5.9.2.4.5
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
507 switch (cdb[1] & 0x1f) {
508 case MI_REPORT_TARGET_PGS:
511 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
519 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
527 * return 1: Is used to signal LUN not accecsable, and check condition/not ready
528 * return 0: Used to signal success
529 * reutrn -1: Used to signal failure, and invalid cdb field
532 target_alua_state_check(struct se_cmd *cmd)
534 struct se_device *dev = cmd->se_dev;
535 unsigned char *cdb = cmd->t_task_cdb;
536 struct se_lun *lun = cmd->se_lun;
537 struct se_port *port = lun->lun_sep;
538 struct t10_alua_tg_pt_gp *tg_pt_gp;
539 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
540 int out_alua_state, nonop_delay_msecs;
544 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
546 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
552 * First, check for a struct se_port specific secondary ALUA target port
553 * access state: OFFLINE
555 if (atomic_read(&port->sep_tg_pt_secondary_offline)) {
556 pr_debug("ALUA: Got secondary offline status for local"
558 alua_ascq = ASCQ_04H_ALUA_OFFLINE;
563 * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
564 * ALUA target port group, to obtain current ALUA access state.
565 * Otherwise look for the underlying struct se_device association with
566 * a ALUA logical unit group.
568 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
569 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
570 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
571 out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
572 nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
573 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
575 * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a separate conditional
576 * statement so the compiler knows explicitly to check this case first.
577 * For the Optimized ALUA access state case, we want to process the
578 * incoming fabric cmd ASAP..
580 if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTMIZED)
583 switch (out_alua_state) {
584 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
585 ret = core_alua_state_nonoptimized(cmd, cdb,
586 nonop_delay_msecs, &alua_ascq);
588 case ALUA_ACCESS_STATE_STANDBY:
589 ret = core_alua_state_standby(cmd, cdb, &alua_ascq);
591 case ALUA_ACCESS_STATE_UNAVAILABLE:
592 ret = core_alua_state_unavailable(cmd, cdb, &alua_ascq);
594 case ALUA_ACCESS_STATE_TRANSITION:
595 ret = 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",
605 return TCM_INVALID_CDB_FIELD;
611 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
612 * The ALUA additional sense code qualifier (ASCQ) is determined
613 * by the ALUA primary or secondary access state..
615 pr_debug("[%s]: ALUA TG Port not available, "
616 "SenseKey: NOT_READY, ASC/ASCQ: "
618 cmd->se_tfo->get_fabric_name(), alua_ascq);
620 cmd->scsi_asc = 0x04;
621 cmd->scsi_ascq = alua_ascq;
622 return TCM_CHECK_CONDITION_NOT_READY;
629 * Check implict and explict ALUA state change request.
631 static sense_reason_t
632 core_alua_check_transition(int state, int *primary)
635 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
636 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
637 case ALUA_ACCESS_STATE_STANDBY:
638 case ALUA_ACCESS_STATE_UNAVAILABLE:
640 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
641 * defined as primary target port asymmetric access states.
645 case ALUA_ACCESS_STATE_OFFLINE:
647 * OFFLINE state is defined as a secondary target port
648 * asymmetric access state.
653 pr_err("Unknown ALUA access state: 0x%02x\n", state);
654 return TCM_INVALID_PARAMETER_LIST;
660 static char *core_alua_dump_state(int state)
663 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
664 return "Active/Optimized";
665 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
666 return "Active/NonOptimized";
667 case ALUA_ACCESS_STATE_STANDBY:
669 case ALUA_ACCESS_STATE_UNAVAILABLE:
670 return "Unavailable";
671 case ALUA_ACCESS_STATE_OFFLINE:
680 char *core_alua_dump_status(int status)
683 case ALUA_STATUS_NONE:
685 case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG:
686 return "Altered by Explict STPG";
687 case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA:
688 return "Altered by Implict ALUA";
697 * Used by fabric modules to determine when we need to delay processing
698 * for the Active/NonOptimized paths..
700 int core_alua_check_nonop_delay(
703 if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
708 * The ALUA Active/NonOptimized access state delay can be disabled
709 * in via configfs with a value of zero
711 if (!cmd->alua_nonop_delay)
714 * struct se_cmd->alua_nonop_delay gets set by a target port group
715 * defined interval in core_alua_state_nonoptimized()
717 msleep_interruptible(cmd->alua_nonop_delay);
720 EXPORT_SYMBOL(core_alua_check_nonop_delay);
723 * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
726 static int core_alua_write_tpg_metadata(
728 unsigned char *md_buf,
731 struct file *file = filp_open(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
735 pr_err("filp_open(%s) for ALUA metadata failed\n", path);
738 ret = kernel_write(file, md_buf, md_buf_len, 0);
740 pr_err("Error writing ALUA metadata file: %s\n", path);
742 return (ret < 0) ? -EIO : 0;
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 t10_wwn *wwn = &tg_pt_gp->tg_pt_gp_dev->t10_wwn;
754 char path[ALUA_METADATA_PATH_LEN];
757 memset(path, 0, ALUA_METADATA_PATH_LEN);
759 len = snprintf(md_buf, tg_pt_gp->tg_pt_gp_md_buf_len,
761 "alua_access_state=0x%02x\n"
762 "alua_access_status=0x%02x\n",
763 tg_pt_gp->tg_pt_gp_id, primary_state,
764 tg_pt_gp->tg_pt_gp_alua_access_status);
766 snprintf(path, ALUA_METADATA_PATH_LEN,
767 "/var/target/alua/tpgs_%s/%s", &wwn->unit_serial[0],
768 config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
770 return core_alua_write_tpg_metadata(path, md_buf, len);
773 static int core_alua_do_transition_tg_pt(
774 struct t10_alua_tg_pt_gp *tg_pt_gp,
775 struct se_port *l_port,
776 struct se_node_acl *nacl,
777 unsigned char *md_buf,
781 struct se_dev_entry *se_deve;
782 struct se_lun_acl *lacl;
783 struct se_port *port;
784 struct t10_alua_tg_pt_gp_member *mem;
787 * Save the old primary ALUA access state, and set the current state
788 * to ALUA_ACCESS_STATE_TRANSITION.
790 old_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
791 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
792 ALUA_ACCESS_STATE_TRANSITION);
793 tg_pt_gp->tg_pt_gp_alua_access_status = (explict) ?
794 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
795 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
797 * Check for the optional ALUA primary state transition delay
799 if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
800 msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
802 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
803 list_for_each_entry(mem, &tg_pt_gp->tg_pt_gp_mem_list,
807 * After an implicit target port asymmetric access state
808 * change, a device server shall establish a unit attention
809 * condition for the initiator port associated with every I_T
810 * nexus with the additional sense code set to ASYMMETRIC
811 * ACCESS STATE CHAGED.
813 * After an explicit target port asymmetric access state
814 * change, a device server shall establish a unit attention
815 * condition with the additional sense code set to ASYMMETRIC
816 * ACCESS STATE CHANGED for the initiator port associated with
817 * every I_T nexus other than the I_T nexus on which the SET
818 * TARGET PORT GROUPS command
820 atomic_inc(&mem->tg_pt_gp_mem_ref_cnt);
821 smp_mb__after_atomic_inc();
822 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
824 spin_lock_bh(&port->sep_alua_lock);
825 list_for_each_entry(se_deve, &port->sep_alua_list,
827 lacl = se_deve->se_lun_acl;
829 * se_deve->se_lun_acl pointer may be NULL for a
830 * entry created without explict Node+MappedLUN ACLs
836 (nacl != NULL) && (nacl == lacl->se_lun_nacl) &&
837 (l_port != NULL) && (l_port == port))
840 core_scsi3_ua_allocate(lacl->se_lun_nacl,
841 se_deve->mapped_lun, 0x2A,
842 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
844 spin_unlock_bh(&port->sep_alua_lock);
846 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
847 atomic_dec(&mem->tg_pt_gp_mem_ref_cnt);
848 smp_mb__after_atomic_dec();
850 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
852 * Update the ALUA metadata buf that has been allocated in
853 * core_alua_do_port_transition(), this metadata will be written
856 * Note that there is the case where we do not want to update the
857 * metadata when the saved metadata is being parsed in userspace
858 * when setting the existing port access state and access status.
860 * Also note that the failure to write out the ALUA metadata to
861 * struct file does NOT affect the actual ALUA transition.
863 if (tg_pt_gp->tg_pt_gp_write_metadata) {
864 mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
865 core_alua_update_tpg_primary_metadata(tg_pt_gp,
867 mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
870 * Set the current primary ALUA access state to the requested new state
872 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, new_state);
874 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
875 " from primary access state %s to %s\n", (explict) ? "explict" :
876 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
877 tg_pt_gp->tg_pt_gp_id, core_alua_dump_state(old_state),
878 core_alua_dump_state(new_state));
883 int core_alua_do_port_transition(
884 struct t10_alua_tg_pt_gp *l_tg_pt_gp,
885 struct se_device *l_dev,
886 struct se_port *l_port,
887 struct se_node_acl *l_nacl,
891 struct se_device *dev;
892 struct se_port *port;
893 struct se_node_acl *nacl;
894 struct t10_alua_lu_gp *lu_gp;
895 struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
896 struct t10_alua_tg_pt_gp *tg_pt_gp;
897 unsigned char *md_buf;
900 if (core_alua_check_transition(new_state, &primary) != 0)
903 md_buf = kzalloc(l_tg_pt_gp->tg_pt_gp_md_buf_len, GFP_KERNEL);
905 pr_err("Unable to allocate buf for ALUA metadata\n");
909 local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
910 spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
911 lu_gp = local_lu_gp_mem->lu_gp;
912 atomic_inc(&lu_gp->lu_gp_ref_cnt);
913 smp_mb__after_atomic_inc();
914 spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
916 * For storage objects that are members of the 'default_lu_gp',
917 * we only do transition on the passed *l_tp_pt_gp, and not
918 * on all of the matching target port groups IDs in default_lu_gp.
920 if (!lu_gp->lu_gp_id) {
922 * core_alua_do_transition_tg_pt() will always return
925 core_alua_do_transition_tg_pt(l_tg_pt_gp, l_port, l_nacl,
926 md_buf, new_state, explict);
927 atomic_dec(&lu_gp->lu_gp_ref_cnt);
928 smp_mb__after_atomic_dec();
933 * For all other LU groups aside from 'default_lu_gp', walk all of
934 * the associated storage objects looking for a matching target port
935 * group ID from the local target port group.
937 spin_lock(&lu_gp->lu_gp_lock);
938 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
941 dev = lu_gp_mem->lu_gp_mem_dev;
942 atomic_inc(&lu_gp_mem->lu_gp_mem_ref_cnt);
943 smp_mb__after_atomic_inc();
944 spin_unlock(&lu_gp->lu_gp_lock);
946 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
947 list_for_each_entry(tg_pt_gp,
948 &dev->t10_alua.tg_pt_gps_list,
951 if (!tg_pt_gp->tg_pt_gp_valid_id)
954 * If the target behavior port asymmetric access state
955 * is changed for any target port group accessiable via
956 * a logical unit within a LU group, the target port
957 * behavior group asymmetric access states for the same
958 * target port group accessible via other logical units
959 * in that LU group will also change.
961 if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
964 if (l_tg_pt_gp == tg_pt_gp) {
971 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
972 smp_mb__after_atomic_inc();
973 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
975 * core_alua_do_transition_tg_pt() will always return
978 core_alua_do_transition_tg_pt(tg_pt_gp, port,
979 nacl, md_buf, new_state, explict);
981 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
982 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
983 smp_mb__after_atomic_dec();
985 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
987 spin_lock(&lu_gp->lu_gp_lock);
988 atomic_dec(&lu_gp_mem->lu_gp_mem_ref_cnt);
989 smp_mb__after_atomic_dec();
991 spin_unlock(&lu_gp->lu_gp_lock);
993 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
994 " Group IDs: %hu %s transition to primary state: %s\n",
995 config_item_name(&lu_gp->lu_gp_group.cg_item),
996 l_tg_pt_gp->tg_pt_gp_id, (explict) ? "explict" : "implict",
997 core_alua_dump_state(new_state));
999 atomic_dec(&lu_gp->lu_gp_ref_cnt);
1000 smp_mb__after_atomic_dec();
1006 * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
1008 static int core_alua_update_tpg_secondary_metadata(
1009 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1010 struct se_port *port,
1011 unsigned char *md_buf,
1014 struct se_portal_group *se_tpg = port->sep_tpg;
1015 char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
1018 memset(path, 0, ALUA_METADATA_PATH_LEN);
1019 memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
1021 len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
1022 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
1024 if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
1025 snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
1026 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
1028 len = snprintf(md_buf, md_buf_len, "alua_tg_pt_offline=%d\n"
1029 "alua_tg_pt_status=0x%02x\n",
1030 atomic_read(&port->sep_tg_pt_secondary_offline),
1031 port->sep_tg_pt_secondary_stat);
1033 snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u",
1034 se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
1035 port->sep_lun->unpacked_lun);
1037 return core_alua_write_tpg_metadata(path, md_buf, len);
1040 static int core_alua_set_tg_pt_secondary_state(
1041 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1042 struct se_port *port,
1046 struct t10_alua_tg_pt_gp *tg_pt_gp;
1047 unsigned char *md_buf;
1049 int trans_delay_msecs;
1051 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1052 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1054 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1055 pr_err("Unable to complete secondary state"
1059 trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
1061 * Set the secondary ALUA target port access state to OFFLINE
1062 * or release the previously secondary state for struct se_port
1065 atomic_set(&port->sep_tg_pt_secondary_offline, 1);
1067 atomic_set(&port->sep_tg_pt_secondary_offline, 0);
1069 md_buf_len = tg_pt_gp->tg_pt_gp_md_buf_len;
1070 port->sep_tg_pt_secondary_stat = (explict) ?
1071 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
1072 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
1074 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1075 " to secondary access state: %s\n", (explict) ? "explict" :
1076 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1077 tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
1079 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1081 * Do the optional transition delay after we set the secondary
1082 * ALUA access state.
1084 if (trans_delay_msecs != 0)
1085 msleep_interruptible(trans_delay_msecs);
1087 * See if we need to update the ALUA fabric port metadata for
1088 * secondary state and status
1090 if (port->sep_tg_pt_secondary_write_md) {
1091 md_buf = kzalloc(md_buf_len, GFP_KERNEL);
1093 pr_err("Unable to allocate md_buf for"
1094 " secondary ALUA access metadata\n");
1097 mutex_lock(&port->sep_tg_pt_md_mutex);
1098 core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem, port,
1099 md_buf, md_buf_len);
1100 mutex_unlock(&port->sep_tg_pt_md_mutex);
1108 struct t10_alua_lu_gp *
1109 core_alua_allocate_lu_gp(const char *name, int def_group)
1111 struct t10_alua_lu_gp *lu_gp;
1113 lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
1115 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1116 return ERR_PTR(-ENOMEM);
1118 INIT_LIST_HEAD(&lu_gp->lu_gp_node);
1119 INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
1120 spin_lock_init(&lu_gp->lu_gp_lock);
1121 atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
1124 lu_gp->lu_gp_id = alua_lu_gps_counter++;
1125 lu_gp->lu_gp_valid_id = 1;
1126 alua_lu_gps_count++;
1132 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
1134 struct t10_alua_lu_gp *lu_gp_tmp;
1137 * The lu_gp->lu_gp_id may only be set once..
1139 if (lu_gp->lu_gp_valid_id) {
1140 pr_warn("ALUA LU Group already has a valid ID,"
1141 " ignoring request\n");
1145 spin_lock(&lu_gps_lock);
1146 if (alua_lu_gps_count == 0x0000ffff) {
1147 pr_err("Maximum ALUA alua_lu_gps_count:"
1148 " 0x0000ffff reached\n");
1149 spin_unlock(&lu_gps_lock);
1150 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1154 lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
1155 alua_lu_gps_counter++;
1157 list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
1158 if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
1162 pr_warn("ALUA Logical Unit Group ID: %hu"
1163 " already exists, ignoring request\n",
1165 spin_unlock(&lu_gps_lock);
1170 lu_gp->lu_gp_id = lu_gp_id_tmp;
1171 lu_gp->lu_gp_valid_id = 1;
1172 list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
1173 alua_lu_gps_count++;
1174 spin_unlock(&lu_gps_lock);
1179 static struct t10_alua_lu_gp_member *
1180 core_alua_allocate_lu_gp_mem(struct se_device *dev)
1182 struct t10_alua_lu_gp_member *lu_gp_mem;
1184 lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
1186 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1187 return ERR_PTR(-ENOMEM);
1189 INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
1190 spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
1191 atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
1193 lu_gp_mem->lu_gp_mem_dev = dev;
1194 dev->dev_alua_lu_gp_mem = lu_gp_mem;
1199 void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
1201 struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
1203 * Once we have reached this point, config_item_put() has
1204 * already been called from target_core_alua_drop_lu_gp().
1206 * Here, we remove the *lu_gp from the global list so that
1207 * no associations can be made while we are releasing
1208 * struct t10_alua_lu_gp.
1210 spin_lock(&lu_gps_lock);
1211 list_del(&lu_gp->lu_gp_node);
1212 alua_lu_gps_count--;
1213 spin_unlock(&lu_gps_lock);
1215 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1216 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1217 * released with core_alua_put_lu_gp_from_name()
1219 while (atomic_read(&lu_gp->lu_gp_ref_cnt))
1222 * Release reference to struct t10_alua_lu_gp * from all associated
1225 spin_lock(&lu_gp->lu_gp_lock);
1226 list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
1227 &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1228 if (lu_gp_mem->lu_gp_assoc) {
1229 list_del(&lu_gp_mem->lu_gp_mem_list);
1230 lu_gp->lu_gp_members--;
1231 lu_gp_mem->lu_gp_assoc = 0;
1233 spin_unlock(&lu_gp->lu_gp_lock);
1236 * lu_gp_mem is associated with a single
1237 * struct se_device->dev_alua_lu_gp_mem, and is released when
1238 * struct se_device is released via core_alua_free_lu_gp_mem().
1240 * If the passed lu_gp does NOT match the default_lu_gp, assume
1241 * we want to re-assocate a given lu_gp_mem with default_lu_gp.
1243 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1244 if (lu_gp != default_lu_gp)
1245 __core_alua_attach_lu_gp_mem(lu_gp_mem,
1248 lu_gp_mem->lu_gp = NULL;
1249 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1251 spin_lock(&lu_gp->lu_gp_lock);
1253 spin_unlock(&lu_gp->lu_gp_lock);
1255 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1258 void core_alua_free_lu_gp_mem(struct se_device *dev)
1260 struct t10_alua_lu_gp *lu_gp;
1261 struct t10_alua_lu_gp_member *lu_gp_mem;
1263 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1267 while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
1270 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1271 lu_gp = lu_gp_mem->lu_gp;
1273 spin_lock(&lu_gp->lu_gp_lock);
1274 if (lu_gp_mem->lu_gp_assoc) {
1275 list_del(&lu_gp_mem->lu_gp_mem_list);
1276 lu_gp->lu_gp_members--;
1277 lu_gp_mem->lu_gp_assoc = 0;
1279 spin_unlock(&lu_gp->lu_gp_lock);
1280 lu_gp_mem->lu_gp = NULL;
1282 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1284 kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
1287 struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
1289 struct t10_alua_lu_gp *lu_gp;
1290 struct config_item *ci;
1292 spin_lock(&lu_gps_lock);
1293 list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
1294 if (!lu_gp->lu_gp_valid_id)
1296 ci = &lu_gp->lu_gp_group.cg_item;
1297 if (!strcmp(config_item_name(ci), name)) {
1298 atomic_inc(&lu_gp->lu_gp_ref_cnt);
1299 spin_unlock(&lu_gps_lock);
1303 spin_unlock(&lu_gps_lock);
1308 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
1310 spin_lock(&lu_gps_lock);
1311 atomic_dec(&lu_gp->lu_gp_ref_cnt);
1312 spin_unlock(&lu_gps_lock);
1316 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1318 void __core_alua_attach_lu_gp_mem(
1319 struct t10_alua_lu_gp_member *lu_gp_mem,
1320 struct t10_alua_lu_gp *lu_gp)
1322 spin_lock(&lu_gp->lu_gp_lock);
1323 lu_gp_mem->lu_gp = lu_gp;
1324 lu_gp_mem->lu_gp_assoc = 1;
1325 list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
1326 lu_gp->lu_gp_members++;
1327 spin_unlock(&lu_gp->lu_gp_lock);
1331 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1333 void __core_alua_drop_lu_gp_mem(
1334 struct t10_alua_lu_gp_member *lu_gp_mem,
1335 struct t10_alua_lu_gp *lu_gp)
1337 spin_lock(&lu_gp->lu_gp_lock);
1338 list_del(&lu_gp_mem->lu_gp_mem_list);
1339 lu_gp_mem->lu_gp = NULL;
1340 lu_gp_mem->lu_gp_assoc = 0;
1341 lu_gp->lu_gp_members--;
1342 spin_unlock(&lu_gp->lu_gp_lock);
1345 struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(struct se_device *dev,
1346 const char *name, int def_group)
1348 struct t10_alua_tg_pt_gp *tg_pt_gp;
1350 tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
1352 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1355 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
1356 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_mem_list);
1357 mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
1358 spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
1359 atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
1360 tg_pt_gp->tg_pt_gp_dev = dev;
1361 tg_pt_gp->tg_pt_gp_md_buf_len = ALUA_MD_BUF_LEN;
1362 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1363 ALUA_ACCESS_STATE_ACTIVE_OPTMIZED);
1365 * Enable both explict and implict ALUA support by default
1367 tg_pt_gp->tg_pt_gp_alua_access_type =
1368 TPGS_EXPLICT_ALUA | TPGS_IMPLICT_ALUA;
1370 * Set the default Active/NonOptimized Delay in milliseconds
1372 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
1373 tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
1374 tg_pt_gp->tg_pt_gp_implict_trans_secs = ALUA_DEFAULT_IMPLICT_TRANS_SECS;
1377 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1378 tg_pt_gp->tg_pt_gp_id =
1379 dev->t10_alua.alua_tg_pt_gps_counter++;
1380 tg_pt_gp->tg_pt_gp_valid_id = 1;
1381 dev->t10_alua.alua_tg_pt_gps_count++;
1382 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1383 &dev->t10_alua.tg_pt_gps_list);
1384 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1390 int core_alua_set_tg_pt_gp_id(
1391 struct t10_alua_tg_pt_gp *tg_pt_gp,
1394 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1395 struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
1396 u16 tg_pt_gp_id_tmp;
1399 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1401 if (tg_pt_gp->tg_pt_gp_valid_id) {
1402 pr_warn("ALUA TG PT Group already has a valid ID,"
1403 " ignoring request\n");
1407 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1408 if (dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
1409 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1410 " 0x0000ffff reached\n");
1411 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1412 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1416 tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
1417 dev->t10_alua.alua_tg_pt_gps_counter++;
1419 list_for_each_entry(tg_pt_gp_tmp, &dev->t10_alua.tg_pt_gps_list,
1421 if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
1425 pr_err("ALUA Target Port Group ID: %hu already"
1426 " exists, ignoring request\n", tg_pt_gp_id);
1427 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1432 tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
1433 tg_pt_gp->tg_pt_gp_valid_id = 1;
1434 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1435 &dev->t10_alua.tg_pt_gps_list);
1436 dev->t10_alua.alua_tg_pt_gps_count++;
1437 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1442 struct t10_alua_tg_pt_gp_member *core_alua_allocate_tg_pt_gp_mem(
1443 struct se_port *port)
1445 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1447 tg_pt_gp_mem = kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache,
1449 if (!tg_pt_gp_mem) {
1450 pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n");
1451 return ERR_PTR(-ENOMEM);
1453 INIT_LIST_HEAD(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1454 spin_lock_init(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1455 atomic_set(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt, 0);
1457 tg_pt_gp_mem->tg_pt = port;
1458 port->sep_alua_tg_pt_gp_mem = tg_pt_gp_mem;
1460 return tg_pt_gp_mem;
1463 void core_alua_free_tg_pt_gp(
1464 struct t10_alua_tg_pt_gp *tg_pt_gp)
1466 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1467 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *tg_pt_gp_mem_tmp;
1470 * Once we have reached this point, config_item_put() has already
1471 * been called from target_core_alua_drop_tg_pt_gp().
1473 * Here we remove *tg_pt_gp from the global list so that
1474 * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS
1475 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1477 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1478 list_del(&tg_pt_gp->tg_pt_gp_list);
1479 dev->t10_alua.alua_tg_pt_gps_counter--;
1480 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1483 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1484 * core_alua_get_tg_pt_gp_by_name() in
1485 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1486 * to be released with core_alua_put_tg_pt_gp_from_name().
1488 while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
1492 * Release reference to struct t10_alua_tg_pt_gp from all associated
1495 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1496 list_for_each_entry_safe(tg_pt_gp_mem, tg_pt_gp_mem_tmp,
1497 &tg_pt_gp->tg_pt_gp_mem_list, tg_pt_gp_mem_list) {
1498 if (tg_pt_gp_mem->tg_pt_gp_assoc) {
1499 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1500 tg_pt_gp->tg_pt_gp_members--;
1501 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1503 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1505 * tg_pt_gp_mem is associated with a single
1506 * se_port->sep_alua_tg_pt_gp_mem, and is released via
1507 * core_alua_free_tg_pt_gp_mem().
1509 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1510 * assume we want to re-assocate a given tg_pt_gp_mem with
1513 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1514 if (tg_pt_gp != dev->t10_alua.default_tg_pt_gp) {
1515 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
1516 dev->t10_alua.default_tg_pt_gp);
1518 tg_pt_gp_mem->tg_pt_gp = NULL;
1519 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1521 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1523 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1525 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1528 void core_alua_free_tg_pt_gp_mem(struct se_port *port)
1530 struct t10_alua_tg_pt_gp *tg_pt_gp;
1531 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1533 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1537 while (atomic_read(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt))
1540 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1541 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1543 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1544 if (tg_pt_gp_mem->tg_pt_gp_assoc) {
1545 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1546 tg_pt_gp->tg_pt_gp_members--;
1547 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1549 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1550 tg_pt_gp_mem->tg_pt_gp = NULL;
1552 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1554 kmem_cache_free(t10_alua_tg_pt_gp_mem_cache, tg_pt_gp_mem);
1557 static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
1558 struct se_device *dev, const char *name)
1560 struct t10_alua_tg_pt_gp *tg_pt_gp;
1561 struct config_item *ci;
1563 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1564 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
1566 if (!tg_pt_gp->tg_pt_gp_valid_id)
1568 ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1569 if (!strcmp(config_item_name(ci), name)) {
1570 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
1571 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1575 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1580 static void core_alua_put_tg_pt_gp_from_name(
1581 struct t10_alua_tg_pt_gp *tg_pt_gp)
1583 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1585 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1586 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
1587 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1591 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1593 void __core_alua_attach_tg_pt_gp_mem(
1594 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1595 struct t10_alua_tg_pt_gp *tg_pt_gp)
1597 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1598 tg_pt_gp_mem->tg_pt_gp = tg_pt_gp;
1599 tg_pt_gp_mem->tg_pt_gp_assoc = 1;
1600 list_add_tail(&tg_pt_gp_mem->tg_pt_gp_mem_list,
1601 &tg_pt_gp->tg_pt_gp_mem_list);
1602 tg_pt_gp->tg_pt_gp_members++;
1603 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1607 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1609 static void __core_alua_drop_tg_pt_gp_mem(
1610 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1611 struct t10_alua_tg_pt_gp *tg_pt_gp)
1613 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1614 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1615 tg_pt_gp_mem->tg_pt_gp = NULL;
1616 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1617 tg_pt_gp->tg_pt_gp_members--;
1618 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1621 ssize_t core_alua_show_tg_pt_gp_info(struct se_port *port, char *page)
1623 struct config_item *tg_pt_ci;
1624 struct t10_alua_tg_pt_gp *tg_pt_gp;
1625 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1628 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1632 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1633 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1635 tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1636 len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
1637 " %hu\nTG Port Primary Access State: %s\nTG Port "
1638 "Primary Access Status: %s\nTG Port Secondary Access"
1639 " State: %s\nTG Port Secondary Access Status: %s\n",
1640 config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
1641 core_alua_dump_state(atomic_read(
1642 &tg_pt_gp->tg_pt_gp_alua_access_state)),
1643 core_alua_dump_status(
1644 tg_pt_gp->tg_pt_gp_alua_access_status),
1645 (atomic_read(&port->sep_tg_pt_secondary_offline)) ?
1647 core_alua_dump_status(port->sep_tg_pt_secondary_stat));
1649 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1654 ssize_t core_alua_store_tg_pt_gp_info(
1655 struct se_port *port,
1659 struct se_portal_group *tpg;
1661 struct se_device *dev = port->sep_lun->lun_se_dev;
1662 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
1663 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1664 unsigned char buf[TG_PT_GROUP_NAME_BUF];
1667 tpg = port->sep_tpg;
1668 lun = port->sep_lun;
1670 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1674 if (count > TG_PT_GROUP_NAME_BUF) {
1675 pr_err("ALUA Target Port Group alias too large!\n");
1678 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
1679 memcpy(buf, page, count);
1681 * Any ALUA target port group alias besides "NULL" means we will be
1682 * making a new group association.
1684 if (strcmp(strstrip(buf), "NULL")) {
1686 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1687 * struct t10_alua_tg_pt_gp. This reference is released with
1688 * core_alua_put_tg_pt_gp_from_name() below.
1690 tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(dev,
1696 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1697 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1700 * Clearing an existing tg_pt_gp association, and replacing
1701 * with the default_tg_pt_gp.
1703 if (!tg_pt_gp_new) {
1704 pr_debug("Target_Core_ConfigFS: Moving"
1705 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1706 " alua/%s, ID: %hu back to"
1707 " default_tg_pt_gp\n",
1708 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1709 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1710 config_item_name(&lun->lun_group.cg_item),
1712 &tg_pt_gp->tg_pt_gp_group.cg_item),
1713 tg_pt_gp->tg_pt_gp_id);
1715 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1716 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
1717 dev->t10_alua.default_tg_pt_gp);
1718 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1723 * Removing existing association of tg_pt_gp_mem with tg_pt_gp
1725 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1729 * Associate tg_pt_gp_mem with tg_pt_gp_new.
1731 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp_new);
1732 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1733 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
1734 " Target Port Group: alua/%s, ID: %hu\n", (move) ?
1735 "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1736 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1737 config_item_name(&lun->lun_group.cg_item),
1738 config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
1739 tg_pt_gp_new->tg_pt_gp_id);
1741 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1745 ssize_t core_alua_show_access_type(
1746 struct t10_alua_tg_pt_gp *tg_pt_gp,
1749 if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA) &&
1750 (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA))
1751 return sprintf(page, "Implict and Explict\n");
1752 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)
1753 return sprintf(page, "Implict\n");
1754 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA)
1755 return sprintf(page, "Explict\n");
1757 return sprintf(page, "None\n");
1760 ssize_t core_alua_store_access_type(
1761 struct t10_alua_tg_pt_gp *tg_pt_gp,
1768 ret = strict_strtoul(page, 0, &tmp);
1770 pr_err("Unable to extract alua_access_type\n");
1773 if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
1774 pr_err("Illegal value for alua_access_type:"
1779 tg_pt_gp->tg_pt_gp_alua_access_type =
1780 TPGS_IMPLICT_ALUA | TPGS_EXPLICT_ALUA;
1782 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICT_ALUA;
1784 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICT_ALUA;
1786 tg_pt_gp->tg_pt_gp_alua_access_type = 0;
1791 ssize_t core_alua_show_nonop_delay_msecs(
1792 struct t10_alua_tg_pt_gp *tg_pt_gp,
1795 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
1798 ssize_t core_alua_store_nonop_delay_msecs(
1799 struct t10_alua_tg_pt_gp *tg_pt_gp,
1806 ret = strict_strtoul(page, 0, &tmp);
1808 pr_err("Unable to extract nonop_delay_msecs\n");
1811 if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
1812 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
1813 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
1814 ALUA_MAX_NONOP_DELAY_MSECS);
1817 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
1822 ssize_t core_alua_show_trans_delay_msecs(
1823 struct t10_alua_tg_pt_gp *tg_pt_gp,
1826 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
1829 ssize_t core_alua_store_trans_delay_msecs(
1830 struct t10_alua_tg_pt_gp *tg_pt_gp,
1837 ret = strict_strtoul(page, 0, &tmp);
1839 pr_err("Unable to extract trans_delay_msecs\n");
1842 if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
1843 pr_err("Passed trans_delay_msecs: %lu, exceeds"
1844 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
1845 ALUA_MAX_TRANS_DELAY_MSECS);
1848 tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
1853 ssize_t core_alua_show_implict_trans_secs(
1854 struct t10_alua_tg_pt_gp *tg_pt_gp,
1857 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_implict_trans_secs);
1860 ssize_t core_alua_store_implict_trans_secs(
1861 struct t10_alua_tg_pt_gp *tg_pt_gp,
1868 ret = strict_strtoul(page, 0, &tmp);
1870 pr_err("Unable to extract implict_trans_secs\n");
1873 if (tmp > ALUA_MAX_IMPLICT_TRANS_SECS) {
1874 pr_err("Passed implict_trans_secs: %lu, exceeds"
1875 " ALUA_MAX_IMPLICT_TRANS_SECS: %d\n", tmp,
1876 ALUA_MAX_IMPLICT_TRANS_SECS);
1879 tg_pt_gp->tg_pt_gp_implict_trans_secs = (int)tmp;
1884 ssize_t core_alua_show_preferred_bit(
1885 struct t10_alua_tg_pt_gp *tg_pt_gp,
1888 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
1891 ssize_t core_alua_store_preferred_bit(
1892 struct t10_alua_tg_pt_gp *tg_pt_gp,
1899 ret = strict_strtoul(page, 0, &tmp);
1901 pr_err("Unable to extract preferred ALUA value\n");
1904 if ((tmp != 0) && (tmp != 1)) {
1905 pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
1908 tg_pt_gp->tg_pt_gp_pref = (int)tmp;
1913 ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
1918 return sprintf(page, "%d\n",
1919 atomic_read(&lun->lun_sep->sep_tg_pt_secondary_offline));
1922 ssize_t core_alua_store_offline_bit(
1927 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1934 ret = strict_strtoul(page, 0, &tmp);
1936 pr_err("Unable to extract alua_tg_pt_offline value\n");
1939 if ((tmp != 0) && (tmp != 1)) {
1940 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
1944 tg_pt_gp_mem = lun->lun_sep->sep_alua_tg_pt_gp_mem;
1945 if (!tg_pt_gp_mem) {
1946 pr_err("Unable to locate *tg_pt_gp_mem\n");
1950 ret = core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem,
1951 lun->lun_sep, 0, (int)tmp);
1958 ssize_t core_alua_show_secondary_status(
1962 return sprintf(page, "%d\n", lun->lun_sep->sep_tg_pt_secondary_stat);
1965 ssize_t core_alua_store_secondary_status(
1973 ret = strict_strtoul(page, 0, &tmp);
1975 pr_err("Unable to extract alua_tg_pt_status\n");
1978 if ((tmp != ALUA_STATUS_NONE) &&
1979 (tmp != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
1980 (tmp != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
1981 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
1985 lun->lun_sep->sep_tg_pt_secondary_stat = (int)tmp;
1990 ssize_t core_alua_show_secondary_write_metadata(
1994 return sprintf(page, "%d\n",
1995 lun->lun_sep->sep_tg_pt_secondary_write_md);
1998 ssize_t core_alua_store_secondary_write_metadata(
2006 ret = strict_strtoul(page, 0, &tmp);
2008 pr_err("Unable to extract alua_tg_pt_write_md\n");
2011 if ((tmp != 0) && (tmp != 1)) {
2012 pr_err("Illegal value for alua_tg_pt_write_md:"
2016 lun->lun_sep->sep_tg_pt_secondary_write_md = (int)tmp;
2021 int core_setup_alua(struct se_device *dev)
2023 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV &&
2024 !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
2025 struct t10_alua_lu_gp_member *lu_gp_mem;
2028 * Associate this struct se_device with the default ALUA
2031 lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
2032 if (IS_ERR(lu_gp_mem))
2033 return PTR_ERR(lu_gp_mem);
2035 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2036 __core_alua_attach_lu_gp_mem(lu_gp_mem,
2038 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2040 pr_debug("%s: Adding to default ALUA LU Group:"
2041 " core/alua/lu_gps/default_lu_gp\n",
2042 dev->transport->name);