1 /*******************************************************************************
2 * Filename: target_core_alua.c
4 * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
6 * (c) Copyright 2009-2013 Datera, Inc.
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 switch (cdb[1] & 0x1f) {
415 case MI_REPORT_TARGET_PGS:
418 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
421 case MAINTENANCE_OUT:
423 case MO_SET_TARGET_PGS:
426 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
430 case PERSISTENT_RESERVE_IN:
431 case PERSISTENT_RESERVE_OUT:
436 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
443 static inline int core_alua_state_unavailable(
449 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
450 * spc4r17 section 5.9.2.4.5
457 switch (cdb[1] & 0x1f) {
458 case MI_REPORT_TARGET_PGS:
461 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
464 case MAINTENANCE_OUT:
466 case MO_SET_TARGET_PGS:
469 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
477 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
484 static inline int core_alua_state_transition(
490 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by
491 * spc4r17 section 5.9.2.5
498 switch (cdb[1] & 0x1f) {
499 case MI_REPORT_TARGET_PGS:
502 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
510 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
518 * return 1: Is used to signal LUN not accecsable, and check condition/not ready
519 * return 0: Used to signal success
520 * reutrn -1: Used to signal failure, and invalid cdb field
523 target_alua_state_check(struct se_cmd *cmd)
525 struct se_device *dev = cmd->se_dev;
526 unsigned char *cdb = cmd->t_task_cdb;
527 struct se_lun *lun = cmd->se_lun;
528 struct se_port *port = lun->lun_sep;
529 struct t10_alua_tg_pt_gp *tg_pt_gp;
530 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
531 int out_alua_state, nonop_delay_msecs;
535 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
537 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
543 * First, check for a struct se_port specific secondary ALUA target port
544 * access state: OFFLINE
546 if (atomic_read(&port->sep_tg_pt_secondary_offline)) {
547 pr_debug("ALUA: Got secondary offline status for local"
549 alua_ascq = ASCQ_04H_ALUA_OFFLINE;
554 * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
555 * ALUA target port group, to obtain current ALUA access state.
556 * Otherwise look for the underlying struct se_device association with
557 * a ALUA logical unit group.
559 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
563 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
564 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
565 out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
566 nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
567 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
569 * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a separate conditional
570 * statement so the compiler knows explicitly to check this case first.
571 * For the Optimized ALUA access state case, we want to process the
572 * incoming fabric cmd ASAP..
574 if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTMIZED)
577 switch (out_alua_state) {
578 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
579 ret = core_alua_state_nonoptimized(cmd, cdb,
580 nonop_delay_msecs, &alua_ascq);
582 case ALUA_ACCESS_STATE_STANDBY:
583 ret = core_alua_state_standby(cmd, cdb, &alua_ascq);
585 case ALUA_ACCESS_STATE_UNAVAILABLE:
586 ret = core_alua_state_unavailable(cmd, cdb, &alua_ascq);
588 case ALUA_ACCESS_STATE_TRANSITION:
589 ret = core_alua_state_transition(cmd, cdb, &alua_ascq);
592 * OFFLINE is a secondary ALUA target port group access state, that is
593 * handled above with struct se_port->sep_tg_pt_secondary_offline=1
595 case ALUA_ACCESS_STATE_OFFLINE:
597 pr_err("Unknown ALUA access state: 0x%02x\n",
599 return TCM_INVALID_CDB_FIELD;
605 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
606 * The ALUA additional sense code qualifier (ASCQ) is determined
607 * by the ALUA primary or secondary access state..
609 pr_debug("[%s]: ALUA TG Port not available, "
610 "SenseKey: NOT_READY, ASC/ASCQ: "
612 cmd->se_tfo->get_fabric_name(), alua_ascq);
614 cmd->scsi_asc = 0x04;
615 cmd->scsi_ascq = alua_ascq;
616 return TCM_CHECK_CONDITION_NOT_READY;
623 * Check implict and explict ALUA state change request.
625 static sense_reason_t
626 core_alua_check_transition(int state, int *primary)
629 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
630 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
631 case ALUA_ACCESS_STATE_STANDBY:
632 case ALUA_ACCESS_STATE_UNAVAILABLE:
634 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
635 * defined as primary target port asymmetric access states.
639 case ALUA_ACCESS_STATE_OFFLINE:
641 * OFFLINE state is defined as a secondary target port
642 * asymmetric access state.
647 pr_err("Unknown ALUA access state: 0x%02x\n", state);
648 return TCM_INVALID_PARAMETER_LIST;
654 static char *core_alua_dump_state(int state)
657 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
658 return "Active/Optimized";
659 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
660 return "Active/NonOptimized";
661 case ALUA_ACCESS_STATE_STANDBY:
663 case ALUA_ACCESS_STATE_UNAVAILABLE:
664 return "Unavailable";
665 case ALUA_ACCESS_STATE_OFFLINE:
674 char *core_alua_dump_status(int status)
677 case ALUA_STATUS_NONE:
679 case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG:
680 return "Altered by Explict STPG";
681 case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA:
682 return "Altered by Implict ALUA";
691 * Used by fabric modules to determine when we need to delay processing
692 * for the Active/NonOptimized paths..
694 int core_alua_check_nonop_delay(
697 if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
702 * The ALUA Active/NonOptimized access state delay can be disabled
703 * in via configfs with a value of zero
705 if (!cmd->alua_nonop_delay)
708 * struct se_cmd->alua_nonop_delay gets set by a target port group
709 * defined interval in core_alua_state_nonoptimized()
711 msleep_interruptible(cmd->alua_nonop_delay);
714 EXPORT_SYMBOL(core_alua_check_nonop_delay);
717 * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
720 static int core_alua_write_tpg_metadata(
722 unsigned char *md_buf,
725 struct file *file = filp_open(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
729 pr_err("filp_open(%s) for ALUA metadata failed\n", path);
732 ret = kernel_write(file, md_buf, md_buf_len, 0);
734 pr_err("Error writing ALUA metadata file: %s\n", path);
736 return (ret < 0) ? -EIO : 0;
740 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
742 static int core_alua_update_tpg_primary_metadata(
743 struct t10_alua_tg_pt_gp *tg_pt_gp,
745 unsigned char *md_buf)
747 struct t10_wwn *wwn = &tg_pt_gp->tg_pt_gp_dev->t10_wwn;
748 char path[ALUA_METADATA_PATH_LEN];
751 memset(path, 0, ALUA_METADATA_PATH_LEN);
753 len = snprintf(md_buf, tg_pt_gp->tg_pt_gp_md_buf_len,
755 "alua_access_state=0x%02x\n"
756 "alua_access_status=0x%02x\n",
757 tg_pt_gp->tg_pt_gp_id, primary_state,
758 tg_pt_gp->tg_pt_gp_alua_access_status);
760 snprintf(path, ALUA_METADATA_PATH_LEN,
761 "/var/target/alua/tpgs_%s/%s", &wwn->unit_serial[0],
762 config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
764 return core_alua_write_tpg_metadata(path, md_buf, len);
767 static int core_alua_do_transition_tg_pt(
768 struct t10_alua_tg_pt_gp *tg_pt_gp,
769 struct se_port *l_port,
770 struct se_node_acl *nacl,
771 unsigned char *md_buf,
775 struct se_dev_entry *se_deve;
776 struct se_lun_acl *lacl;
777 struct se_port *port;
778 struct t10_alua_tg_pt_gp_member *mem;
781 * Save the old primary ALUA access state, and set the current state
782 * to ALUA_ACCESS_STATE_TRANSITION.
784 old_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
785 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
786 ALUA_ACCESS_STATE_TRANSITION);
787 tg_pt_gp->tg_pt_gp_alua_access_status = (explict) ?
788 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
789 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
791 * Check for the optional ALUA primary state transition delay
793 if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
794 msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
796 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
797 list_for_each_entry(mem, &tg_pt_gp->tg_pt_gp_mem_list,
801 * After an implicit target port asymmetric access state
802 * change, a device server shall establish a unit attention
803 * condition for the initiator port associated with every I_T
804 * nexus with the additional sense code set to ASYMMETRIC
805 * ACCESS STATE CHAGED.
807 * After an explicit target port asymmetric access state
808 * change, a device server shall establish a unit attention
809 * condition with the additional sense code set to ASYMMETRIC
810 * ACCESS STATE CHANGED for the initiator port associated with
811 * every I_T nexus other than the I_T nexus on which the SET
812 * TARGET PORT GROUPS command
814 atomic_inc(&mem->tg_pt_gp_mem_ref_cnt);
815 smp_mb__after_atomic_inc();
816 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
818 spin_lock_bh(&port->sep_alua_lock);
819 list_for_each_entry(se_deve, &port->sep_alua_list,
821 lacl = se_deve->se_lun_acl;
823 * se_deve->se_lun_acl pointer may be NULL for a
824 * entry created without explict Node+MappedLUN ACLs
830 (nacl != NULL) && (nacl == lacl->se_lun_nacl) &&
831 (l_port != NULL) && (l_port == port))
834 core_scsi3_ua_allocate(lacl->se_lun_nacl,
835 se_deve->mapped_lun, 0x2A,
836 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
838 spin_unlock_bh(&port->sep_alua_lock);
840 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
841 atomic_dec(&mem->tg_pt_gp_mem_ref_cnt);
842 smp_mb__after_atomic_dec();
844 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
846 * Update the ALUA metadata buf that has been allocated in
847 * core_alua_do_port_transition(), this metadata will be written
850 * Note that there is the case where we do not want to update the
851 * metadata when the saved metadata is being parsed in userspace
852 * when setting the existing port access state and access status.
854 * Also note that the failure to write out the ALUA metadata to
855 * struct file does NOT affect the actual ALUA transition.
857 if (tg_pt_gp->tg_pt_gp_write_metadata) {
858 mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
859 core_alua_update_tpg_primary_metadata(tg_pt_gp,
861 mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
864 * Set the current primary ALUA access state to the requested new state
866 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, new_state);
868 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
869 " from primary access state %s to %s\n", (explict) ? "explict" :
870 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
871 tg_pt_gp->tg_pt_gp_id, core_alua_dump_state(old_state),
872 core_alua_dump_state(new_state));
877 int core_alua_do_port_transition(
878 struct t10_alua_tg_pt_gp *l_tg_pt_gp,
879 struct se_device *l_dev,
880 struct se_port *l_port,
881 struct se_node_acl *l_nacl,
885 struct se_device *dev;
886 struct se_port *port;
887 struct se_node_acl *nacl;
888 struct t10_alua_lu_gp *lu_gp;
889 struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
890 struct t10_alua_tg_pt_gp *tg_pt_gp;
891 unsigned char *md_buf;
894 if (core_alua_check_transition(new_state, &primary) != 0)
897 md_buf = kzalloc(l_tg_pt_gp->tg_pt_gp_md_buf_len, GFP_KERNEL);
899 pr_err("Unable to allocate buf for ALUA metadata\n");
903 local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
904 spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
905 lu_gp = local_lu_gp_mem->lu_gp;
906 atomic_inc(&lu_gp->lu_gp_ref_cnt);
907 smp_mb__after_atomic_inc();
908 spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
910 * For storage objects that are members of the 'default_lu_gp',
911 * we only do transition on the passed *l_tp_pt_gp, and not
912 * on all of the matching target port groups IDs in default_lu_gp.
914 if (!lu_gp->lu_gp_id) {
916 * core_alua_do_transition_tg_pt() will always return
919 core_alua_do_transition_tg_pt(l_tg_pt_gp, l_port, l_nacl,
920 md_buf, new_state, explict);
921 atomic_dec(&lu_gp->lu_gp_ref_cnt);
922 smp_mb__after_atomic_dec();
927 * For all other LU groups aside from 'default_lu_gp', walk all of
928 * the associated storage objects looking for a matching target port
929 * group ID from the local target port group.
931 spin_lock(&lu_gp->lu_gp_lock);
932 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
935 dev = lu_gp_mem->lu_gp_mem_dev;
936 atomic_inc(&lu_gp_mem->lu_gp_mem_ref_cnt);
937 smp_mb__after_atomic_inc();
938 spin_unlock(&lu_gp->lu_gp_lock);
940 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
941 list_for_each_entry(tg_pt_gp,
942 &dev->t10_alua.tg_pt_gps_list,
945 if (!tg_pt_gp->tg_pt_gp_valid_id)
948 * If the target behavior port asymmetric access state
949 * is changed for any target port group accessiable via
950 * a logical unit within a LU group, the target port
951 * behavior group asymmetric access states for the same
952 * target port group accessible via other logical units
953 * in that LU group will also change.
955 if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
958 if (l_tg_pt_gp == tg_pt_gp) {
965 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
966 smp_mb__after_atomic_inc();
967 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
969 * core_alua_do_transition_tg_pt() will always return
972 core_alua_do_transition_tg_pt(tg_pt_gp, port,
973 nacl, md_buf, new_state, explict);
975 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
976 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
977 smp_mb__after_atomic_dec();
979 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
981 spin_lock(&lu_gp->lu_gp_lock);
982 atomic_dec(&lu_gp_mem->lu_gp_mem_ref_cnt);
983 smp_mb__after_atomic_dec();
985 spin_unlock(&lu_gp->lu_gp_lock);
987 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
988 " Group IDs: %hu %s transition to primary state: %s\n",
989 config_item_name(&lu_gp->lu_gp_group.cg_item),
990 l_tg_pt_gp->tg_pt_gp_id, (explict) ? "explict" : "implict",
991 core_alua_dump_state(new_state));
993 atomic_dec(&lu_gp->lu_gp_ref_cnt);
994 smp_mb__after_atomic_dec();
1000 * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
1002 static int core_alua_update_tpg_secondary_metadata(
1003 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1004 struct se_port *port,
1005 unsigned char *md_buf,
1008 struct se_portal_group *se_tpg = port->sep_tpg;
1009 char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
1012 memset(path, 0, ALUA_METADATA_PATH_LEN);
1013 memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
1015 len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
1016 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
1018 if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
1019 snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
1020 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
1022 len = snprintf(md_buf, md_buf_len, "alua_tg_pt_offline=%d\n"
1023 "alua_tg_pt_status=0x%02x\n",
1024 atomic_read(&port->sep_tg_pt_secondary_offline),
1025 port->sep_tg_pt_secondary_stat);
1027 snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u",
1028 se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
1029 port->sep_lun->unpacked_lun);
1031 return core_alua_write_tpg_metadata(path, md_buf, len);
1034 static int core_alua_set_tg_pt_secondary_state(
1035 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1036 struct se_port *port,
1040 struct t10_alua_tg_pt_gp *tg_pt_gp;
1041 unsigned char *md_buf;
1043 int trans_delay_msecs;
1045 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1046 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1048 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1049 pr_err("Unable to complete secondary state"
1053 trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
1055 * Set the secondary ALUA target port access state to OFFLINE
1056 * or release the previously secondary state for struct se_port
1059 atomic_set(&port->sep_tg_pt_secondary_offline, 1);
1061 atomic_set(&port->sep_tg_pt_secondary_offline, 0);
1063 md_buf_len = tg_pt_gp->tg_pt_gp_md_buf_len;
1064 port->sep_tg_pt_secondary_stat = (explict) ?
1065 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
1066 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
1068 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1069 " to secondary access state: %s\n", (explict) ? "explict" :
1070 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1071 tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
1073 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1075 * Do the optional transition delay after we set the secondary
1076 * ALUA access state.
1078 if (trans_delay_msecs != 0)
1079 msleep_interruptible(trans_delay_msecs);
1081 * See if we need to update the ALUA fabric port metadata for
1082 * secondary state and status
1084 if (port->sep_tg_pt_secondary_write_md) {
1085 md_buf = kzalloc(md_buf_len, GFP_KERNEL);
1087 pr_err("Unable to allocate md_buf for"
1088 " secondary ALUA access metadata\n");
1091 mutex_lock(&port->sep_tg_pt_md_mutex);
1092 core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem, port,
1093 md_buf, md_buf_len);
1094 mutex_unlock(&port->sep_tg_pt_md_mutex);
1102 struct t10_alua_lu_gp *
1103 core_alua_allocate_lu_gp(const char *name, int def_group)
1105 struct t10_alua_lu_gp *lu_gp;
1107 lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
1109 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1110 return ERR_PTR(-ENOMEM);
1112 INIT_LIST_HEAD(&lu_gp->lu_gp_node);
1113 INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
1114 spin_lock_init(&lu_gp->lu_gp_lock);
1115 atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
1118 lu_gp->lu_gp_id = alua_lu_gps_counter++;
1119 lu_gp->lu_gp_valid_id = 1;
1120 alua_lu_gps_count++;
1126 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
1128 struct t10_alua_lu_gp *lu_gp_tmp;
1131 * The lu_gp->lu_gp_id may only be set once..
1133 if (lu_gp->lu_gp_valid_id) {
1134 pr_warn("ALUA LU Group already has a valid ID,"
1135 " ignoring request\n");
1139 spin_lock(&lu_gps_lock);
1140 if (alua_lu_gps_count == 0x0000ffff) {
1141 pr_err("Maximum ALUA alua_lu_gps_count:"
1142 " 0x0000ffff reached\n");
1143 spin_unlock(&lu_gps_lock);
1144 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1148 lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
1149 alua_lu_gps_counter++;
1151 list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
1152 if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
1156 pr_warn("ALUA Logical Unit Group ID: %hu"
1157 " already exists, ignoring request\n",
1159 spin_unlock(&lu_gps_lock);
1164 lu_gp->lu_gp_id = lu_gp_id_tmp;
1165 lu_gp->lu_gp_valid_id = 1;
1166 list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
1167 alua_lu_gps_count++;
1168 spin_unlock(&lu_gps_lock);
1173 static struct t10_alua_lu_gp_member *
1174 core_alua_allocate_lu_gp_mem(struct se_device *dev)
1176 struct t10_alua_lu_gp_member *lu_gp_mem;
1178 lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
1180 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1181 return ERR_PTR(-ENOMEM);
1183 INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
1184 spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
1185 atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
1187 lu_gp_mem->lu_gp_mem_dev = dev;
1188 dev->dev_alua_lu_gp_mem = lu_gp_mem;
1193 void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
1195 struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
1197 * Once we have reached this point, config_item_put() has
1198 * already been called from target_core_alua_drop_lu_gp().
1200 * Here, we remove the *lu_gp from the global list so that
1201 * no associations can be made while we are releasing
1202 * struct t10_alua_lu_gp.
1204 spin_lock(&lu_gps_lock);
1205 list_del(&lu_gp->lu_gp_node);
1206 alua_lu_gps_count--;
1207 spin_unlock(&lu_gps_lock);
1209 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1210 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1211 * released with core_alua_put_lu_gp_from_name()
1213 while (atomic_read(&lu_gp->lu_gp_ref_cnt))
1216 * Release reference to struct t10_alua_lu_gp * from all associated
1219 spin_lock(&lu_gp->lu_gp_lock);
1220 list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
1221 &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1222 if (lu_gp_mem->lu_gp_assoc) {
1223 list_del(&lu_gp_mem->lu_gp_mem_list);
1224 lu_gp->lu_gp_members--;
1225 lu_gp_mem->lu_gp_assoc = 0;
1227 spin_unlock(&lu_gp->lu_gp_lock);
1230 * lu_gp_mem is associated with a single
1231 * struct se_device->dev_alua_lu_gp_mem, and is released when
1232 * struct se_device is released via core_alua_free_lu_gp_mem().
1234 * If the passed lu_gp does NOT match the default_lu_gp, assume
1235 * we want to re-assocate a given lu_gp_mem with default_lu_gp.
1237 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1238 if (lu_gp != default_lu_gp)
1239 __core_alua_attach_lu_gp_mem(lu_gp_mem,
1242 lu_gp_mem->lu_gp = NULL;
1243 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1245 spin_lock(&lu_gp->lu_gp_lock);
1247 spin_unlock(&lu_gp->lu_gp_lock);
1249 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1252 void core_alua_free_lu_gp_mem(struct se_device *dev)
1254 struct t10_alua_lu_gp *lu_gp;
1255 struct t10_alua_lu_gp_member *lu_gp_mem;
1257 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1261 while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
1264 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1265 lu_gp = lu_gp_mem->lu_gp;
1267 spin_lock(&lu_gp->lu_gp_lock);
1268 if (lu_gp_mem->lu_gp_assoc) {
1269 list_del(&lu_gp_mem->lu_gp_mem_list);
1270 lu_gp->lu_gp_members--;
1271 lu_gp_mem->lu_gp_assoc = 0;
1273 spin_unlock(&lu_gp->lu_gp_lock);
1274 lu_gp_mem->lu_gp = NULL;
1276 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1278 kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
1281 struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
1283 struct t10_alua_lu_gp *lu_gp;
1284 struct config_item *ci;
1286 spin_lock(&lu_gps_lock);
1287 list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
1288 if (!lu_gp->lu_gp_valid_id)
1290 ci = &lu_gp->lu_gp_group.cg_item;
1291 if (!strcmp(config_item_name(ci), name)) {
1292 atomic_inc(&lu_gp->lu_gp_ref_cnt);
1293 spin_unlock(&lu_gps_lock);
1297 spin_unlock(&lu_gps_lock);
1302 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
1304 spin_lock(&lu_gps_lock);
1305 atomic_dec(&lu_gp->lu_gp_ref_cnt);
1306 spin_unlock(&lu_gps_lock);
1310 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1312 void __core_alua_attach_lu_gp_mem(
1313 struct t10_alua_lu_gp_member *lu_gp_mem,
1314 struct t10_alua_lu_gp *lu_gp)
1316 spin_lock(&lu_gp->lu_gp_lock);
1317 lu_gp_mem->lu_gp = lu_gp;
1318 lu_gp_mem->lu_gp_assoc = 1;
1319 list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
1320 lu_gp->lu_gp_members++;
1321 spin_unlock(&lu_gp->lu_gp_lock);
1325 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1327 void __core_alua_drop_lu_gp_mem(
1328 struct t10_alua_lu_gp_member *lu_gp_mem,
1329 struct t10_alua_lu_gp *lu_gp)
1331 spin_lock(&lu_gp->lu_gp_lock);
1332 list_del(&lu_gp_mem->lu_gp_mem_list);
1333 lu_gp_mem->lu_gp = NULL;
1334 lu_gp_mem->lu_gp_assoc = 0;
1335 lu_gp->lu_gp_members--;
1336 spin_unlock(&lu_gp->lu_gp_lock);
1339 struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(struct se_device *dev,
1340 const char *name, int def_group)
1342 struct t10_alua_tg_pt_gp *tg_pt_gp;
1344 tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
1346 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1349 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
1350 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_mem_list);
1351 mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
1352 spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
1353 atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
1354 tg_pt_gp->tg_pt_gp_dev = dev;
1355 tg_pt_gp->tg_pt_gp_md_buf_len = ALUA_MD_BUF_LEN;
1356 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1357 ALUA_ACCESS_STATE_ACTIVE_OPTMIZED);
1359 * Enable both explict and implict ALUA support by default
1361 tg_pt_gp->tg_pt_gp_alua_access_type =
1362 TPGS_EXPLICT_ALUA | TPGS_IMPLICT_ALUA;
1364 * Set the default Active/NonOptimized Delay in milliseconds
1366 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
1367 tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
1368 tg_pt_gp->tg_pt_gp_implict_trans_secs = ALUA_DEFAULT_IMPLICT_TRANS_SECS;
1371 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1372 tg_pt_gp->tg_pt_gp_id =
1373 dev->t10_alua.alua_tg_pt_gps_counter++;
1374 tg_pt_gp->tg_pt_gp_valid_id = 1;
1375 dev->t10_alua.alua_tg_pt_gps_count++;
1376 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1377 &dev->t10_alua.tg_pt_gps_list);
1378 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1384 int core_alua_set_tg_pt_gp_id(
1385 struct t10_alua_tg_pt_gp *tg_pt_gp,
1388 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1389 struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
1390 u16 tg_pt_gp_id_tmp;
1393 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1395 if (tg_pt_gp->tg_pt_gp_valid_id) {
1396 pr_warn("ALUA TG PT Group already has a valid ID,"
1397 " ignoring request\n");
1401 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1402 if (dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
1403 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1404 " 0x0000ffff reached\n");
1405 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1406 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1410 tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
1411 dev->t10_alua.alua_tg_pt_gps_counter++;
1413 list_for_each_entry(tg_pt_gp_tmp, &dev->t10_alua.tg_pt_gps_list,
1415 if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
1419 pr_err("ALUA Target Port Group ID: %hu already"
1420 " exists, ignoring request\n", tg_pt_gp_id);
1421 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1426 tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
1427 tg_pt_gp->tg_pt_gp_valid_id = 1;
1428 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1429 &dev->t10_alua.tg_pt_gps_list);
1430 dev->t10_alua.alua_tg_pt_gps_count++;
1431 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1436 struct t10_alua_tg_pt_gp_member *core_alua_allocate_tg_pt_gp_mem(
1437 struct se_port *port)
1439 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1441 tg_pt_gp_mem = kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache,
1443 if (!tg_pt_gp_mem) {
1444 pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n");
1445 return ERR_PTR(-ENOMEM);
1447 INIT_LIST_HEAD(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1448 spin_lock_init(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1449 atomic_set(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt, 0);
1451 tg_pt_gp_mem->tg_pt = port;
1452 port->sep_alua_tg_pt_gp_mem = tg_pt_gp_mem;
1454 return tg_pt_gp_mem;
1457 void core_alua_free_tg_pt_gp(
1458 struct t10_alua_tg_pt_gp *tg_pt_gp)
1460 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1461 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *tg_pt_gp_mem_tmp;
1464 * Once we have reached this point, config_item_put() has already
1465 * been called from target_core_alua_drop_tg_pt_gp().
1467 * Here we remove *tg_pt_gp from the global list so that
1468 * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS
1469 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1471 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1472 list_del(&tg_pt_gp->tg_pt_gp_list);
1473 dev->t10_alua.alua_tg_pt_gps_counter--;
1474 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1477 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1478 * core_alua_get_tg_pt_gp_by_name() in
1479 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1480 * to be released with core_alua_put_tg_pt_gp_from_name().
1482 while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
1486 * Release reference to struct t10_alua_tg_pt_gp from all associated
1489 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1490 list_for_each_entry_safe(tg_pt_gp_mem, tg_pt_gp_mem_tmp,
1491 &tg_pt_gp->tg_pt_gp_mem_list, tg_pt_gp_mem_list) {
1492 if (tg_pt_gp_mem->tg_pt_gp_assoc) {
1493 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1494 tg_pt_gp->tg_pt_gp_members--;
1495 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1497 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1499 * tg_pt_gp_mem is associated with a single
1500 * se_port->sep_alua_tg_pt_gp_mem, and is released via
1501 * core_alua_free_tg_pt_gp_mem().
1503 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1504 * assume we want to re-assocate a given tg_pt_gp_mem with
1507 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1508 if (tg_pt_gp != dev->t10_alua.default_tg_pt_gp) {
1509 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
1510 dev->t10_alua.default_tg_pt_gp);
1512 tg_pt_gp_mem->tg_pt_gp = NULL;
1513 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1515 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1517 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1519 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1522 void core_alua_free_tg_pt_gp_mem(struct se_port *port)
1524 struct t10_alua_tg_pt_gp *tg_pt_gp;
1525 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1527 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1531 while (atomic_read(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt))
1534 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1535 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1537 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1538 if (tg_pt_gp_mem->tg_pt_gp_assoc) {
1539 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1540 tg_pt_gp->tg_pt_gp_members--;
1541 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1543 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1544 tg_pt_gp_mem->tg_pt_gp = NULL;
1546 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1548 kmem_cache_free(t10_alua_tg_pt_gp_mem_cache, tg_pt_gp_mem);
1551 static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
1552 struct se_device *dev, const char *name)
1554 struct t10_alua_tg_pt_gp *tg_pt_gp;
1555 struct config_item *ci;
1557 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1558 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
1560 if (!tg_pt_gp->tg_pt_gp_valid_id)
1562 ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1563 if (!strcmp(config_item_name(ci), name)) {
1564 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
1565 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1569 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1574 static void core_alua_put_tg_pt_gp_from_name(
1575 struct t10_alua_tg_pt_gp *tg_pt_gp)
1577 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1579 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1580 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
1581 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1585 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1587 void __core_alua_attach_tg_pt_gp_mem(
1588 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1589 struct t10_alua_tg_pt_gp *tg_pt_gp)
1591 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1592 tg_pt_gp_mem->tg_pt_gp = tg_pt_gp;
1593 tg_pt_gp_mem->tg_pt_gp_assoc = 1;
1594 list_add_tail(&tg_pt_gp_mem->tg_pt_gp_mem_list,
1595 &tg_pt_gp->tg_pt_gp_mem_list);
1596 tg_pt_gp->tg_pt_gp_members++;
1597 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1601 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1603 static void __core_alua_drop_tg_pt_gp_mem(
1604 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1605 struct t10_alua_tg_pt_gp *tg_pt_gp)
1607 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1608 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1609 tg_pt_gp_mem->tg_pt_gp = NULL;
1610 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1611 tg_pt_gp->tg_pt_gp_members--;
1612 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1615 ssize_t core_alua_show_tg_pt_gp_info(struct se_port *port, char *page)
1617 struct config_item *tg_pt_ci;
1618 struct t10_alua_tg_pt_gp *tg_pt_gp;
1619 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1622 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1626 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1627 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1629 tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1630 len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
1631 " %hu\nTG Port Primary Access State: %s\nTG Port "
1632 "Primary Access Status: %s\nTG Port Secondary Access"
1633 " State: %s\nTG Port Secondary Access Status: %s\n",
1634 config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
1635 core_alua_dump_state(atomic_read(
1636 &tg_pt_gp->tg_pt_gp_alua_access_state)),
1637 core_alua_dump_status(
1638 tg_pt_gp->tg_pt_gp_alua_access_status),
1639 (atomic_read(&port->sep_tg_pt_secondary_offline)) ?
1641 core_alua_dump_status(port->sep_tg_pt_secondary_stat));
1643 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1648 ssize_t core_alua_store_tg_pt_gp_info(
1649 struct se_port *port,
1653 struct se_portal_group *tpg;
1655 struct se_device *dev = port->sep_lun->lun_se_dev;
1656 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
1657 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1658 unsigned char buf[TG_PT_GROUP_NAME_BUF];
1661 tpg = port->sep_tpg;
1662 lun = port->sep_lun;
1664 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1668 if (count > TG_PT_GROUP_NAME_BUF) {
1669 pr_err("ALUA Target Port Group alias too large!\n");
1672 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
1673 memcpy(buf, page, count);
1675 * Any ALUA target port group alias besides "NULL" means we will be
1676 * making a new group association.
1678 if (strcmp(strstrip(buf), "NULL")) {
1680 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1681 * struct t10_alua_tg_pt_gp. This reference is released with
1682 * core_alua_put_tg_pt_gp_from_name() below.
1684 tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(dev,
1690 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1691 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1694 * Clearing an existing tg_pt_gp association, and replacing
1695 * with the default_tg_pt_gp.
1697 if (!tg_pt_gp_new) {
1698 pr_debug("Target_Core_ConfigFS: Moving"
1699 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1700 " alua/%s, ID: %hu back to"
1701 " default_tg_pt_gp\n",
1702 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1703 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1704 config_item_name(&lun->lun_group.cg_item),
1706 &tg_pt_gp->tg_pt_gp_group.cg_item),
1707 tg_pt_gp->tg_pt_gp_id);
1709 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1710 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
1711 dev->t10_alua.default_tg_pt_gp);
1712 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1717 * Removing existing association of tg_pt_gp_mem with tg_pt_gp
1719 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1723 * Associate tg_pt_gp_mem with tg_pt_gp_new.
1725 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp_new);
1726 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1727 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
1728 " Target Port Group: alua/%s, ID: %hu\n", (move) ?
1729 "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1730 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1731 config_item_name(&lun->lun_group.cg_item),
1732 config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
1733 tg_pt_gp_new->tg_pt_gp_id);
1735 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1739 ssize_t core_alua_show_access_type(
1740 struct t10_alua_tg_pt_gp *tg_pt_gp,
1743 if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA) &&
1744 (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA))
1745 return sprintf(page, "Implict and Explict\n");
1746 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)
1747 return sprintf(page, "Implict\n");
1748 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA)
1749 return sprintf(page, "Explict\n");
1751 return sprintf(page, "None\n");
1754 ssize_t core_alua_store_access_type(
1755 struct t10_alua_tg_pt_gp *tg_pt_gp,
1762 ret = kstrtoul(page, 0, &tmp);
1764 pr_err("Unable to extract alua_access_type\n");
1767 if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
1768 pr_err("Illegal value for alua_access_type:"
1773 tg_pt_gp->tg_pt_gp_alua_access_type =
1774 TPGS_IMPLICT_ALUA | TPGS_EXPLICT_ALUA;
1776 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICT_ALUA;
1778 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICT_ALUA;
1780 tg_pt_gp->tg_pt_gp_alua_access_type = 0;
1785 ssize_t core_alua_show_nonop_delay_msecs(
1786 struct t10_alua_tg_pt_gp *tg_pt_gp,
1789 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
1792 ssize_t core_alua_store_nonop_delay_msecs(
1793 struct t10_alua_tg_pt_gp *tg_pt_gp,
1800 ret = kstrtoul(page, 0, &tmp);
1802 pr_err("Unable to extract nonop_delay_msecs\n");
1805 if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
1806 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
1807 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
1808 ALUA_MAX_NONOP_DELAY_MSECS);
1811 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
1816 ssize_t core_alua_show_trans_delay_msecs(
1817 struct t10_alua_tg_pt_gp *tg_pt_gp,
1820 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
1823 ssize_t core_alua_store_trans_delay_msecs(
1824 struct t10_alua_tg_pt_gp *tg_pt_gp,
1831 ret = kstrtoul(page, 0, &tmp);
1833 pr_err("Unable to extract trans_delay_msecs\n");
1836 if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
1837 pr_err("Passed trans_delay_msecs: %lu, exceeds"
1838 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
1839 ALUA_MAX_TRANS_DELAY_MSECS);
1842 tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
1847 ssize_t core_alua_show_implict_trans_secs(
1848 struct t10_alua_tg_pt_gp *tg_pt_gp,
1851 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_implict_trans_secs);
1854 ssize_t core_alua_store_implict_trans_secs(
1855 struct t10_alua_tg_pt_gp *tg_pt_gp,
1862 ret = kstrtoul(page, 0, &tmp);
1864 pr_err("Unable to extract implict_trans_secs\n");
1867 if (tmp > ALUA_MAX_IMPLICT_TRANS_SECS) {
1868 pr_err("Passed implict_trans_secs: %lu, exceeds"
1869 " ALUA_MAX_IMPLICT_TRANS_SECS: %d\n", tmp,
1870 ALUA_MAX_IMPLICT_TRANS_SECS);
1873 tg_pt_gp->tg_pt_gp_implict_trans_secs = (int)tmp;
1878 ssize_t core_alua_show_preferred_bit(
1879 struct t10_alua_tg_pt_gp *tg_pt_gp,
1882 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
1885 ssize_t core_alua_store_preferred_bit(
1886 struct t10_alua_tg_pt_gp *tg_pt_gp,
1893 ret = kstrtoul(page, 0, &tmp);
1895 pr_err("Unable to extract preferred ALUA value\n");
1898 if ((tmp != 0) && (tmp != 1)) {
1899 pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
1902 tg_pt_gp->tg_pt_gp_pref = (int)tmp;
1907 ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
1912 return sprintf(page, "%d\n",
1913 atomic_read(&lun->lun_sep->sep_tg_pt_secondary_offline));
1916 ssize_t core_alua_store_offline_bit(
1921 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1928 ret = kstrtoul(page, 0, &tmp);
1930 pr_err("Unable to extract alua_tg_pt_offline value\n");
1933 if ((tmp != 0) && (tmp != 1)) {
1934 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
1938 tg_pt_gp_mem = lun->lun_sep->sep_alua_tg_pt_gp_mem;
1939 if (!tg_pt_gp_mem) {
1940 pr_err("Unable to locate *tg_pt_gp_mem\n");
1944 ret = core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem,
1945 lun->lun_sep, 0, (int)tmp);
1952 ssize_t core_alua_show_secondary_status(
1956 return sprintf(page, "%d\n", lun->lun_sep->sep_tg_pt_secondary_stat);
1959 ssize_t core_alua_store_secondary_status(
1967 ret = kstrtoul(page, 0, &tmp);
1969 pr_err("Unable to extract alua_tg_pt_status\n");
1972 if ((tmp != ALUA_STATUS_NONE) &&
1973 (tmp != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
1974 (tmp != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
1975 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
1979 lun->lun_sep->sep_tg_pt_secondary_stat = (int)tmp;
1984 ssize_t core_alua_show_secondary_write_metadata(
1988 return sprintf(page, "%d\n",
1989 lun->lun_sep->sep_tg_pt_secondary_write_md);
1992 ssize_t core_alua_store_secondary_write_metadata(
2000 ret = kstrtoul(page, 0, &tmp);
2002 pr_err("Unable to extract alua_tg_pt_write_md\n");
2005 if ((tmp != 0) && (tmp != 1)) {
2006 pr_err("Illegal value for alua_tg_pt_write_md:"
2010 lun->lun_sep->sep_tg_pt_secondary_write_md = (int)tmp;
2015 int core_setup_alua(struct se_device *dev)
2017 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV &&
2018 !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
2019 struct t10_alua_lu_gp_member *lu_gp_mem;
2022 * Associate this struct se_device with the default ALUA
2025 lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
2026 if (IS_ERR(lu_gp_mem))
2027 return PTR_ERR(lu_gp_mem);
2029 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2030 __core_alua_attach_lu_gp_mem(lu_gp_mem,
2032 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2034 pr_debug("%s: Adding to default ALUA LU Group:"
2035 " core/alua/lu_gps/default_lu_gp\n",
2036 dev->transport->name);