1 /*******************************************************************************
2 * Filename: target_core_alua.c
4 * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
6 * Copyright (c) 2009-2010 Rising Tide Systems
7 * Copyright (c) 2009-2010 Linux-iSCSI.org
9 * Nicholas A. Bellinger <nab@kernel.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 ******************************************************************************/
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <linux/configfs.h>
30 #include <linux/export.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <asm/unaligned.h>
35 #include <target/target_core_base.h>
36 #include <target/target_core_device.h>
37 #include <target/target_core_transport.h>
38 #include <target/target_core_fabric_ops.h>
39 #include <target/target_core_configfs.h>
41 #include "target_core_alua.h"
42 #include "target_core_hba.h"
43 #include "target_core_ua.h"
45 static int core_alua_check_transition(int state, int *primary);
46 static int core_alua_set_tg_pt_secondary_state(
47 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
48 struct se_port *port, int explict, int offline);
50 static u16 alua_lu_gps_counter;
51 static u32 alua_lu_gps_count;
53 static DEFINE_SPINLOCK(lu_gps_lock);
54 static LIST_HEAD(lu_gps_list);
56 struct t10_alua_lu_gp *default_lu_gp;
59 * REPORT_TARGET_PORT_GROUPS
61 * See spc4r17 section 6.27
63 int target_emulate_report_target_port_groups(struct se_task *task)
65 struct se_cmd *cmd = task->task_se_cmd;
66 struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
68 struct t10_alua_tg_pt_gp *tg_pt_gp;
69 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
71 u32 rd_len = 0, off = 4; /* Skip over RESERVED area to first
72 Target port group descriptor */
74 * Need at least 4 bytes of response data or else we can't
75 * even fit the return data length.
77 if (cmd->data_length < 4) {
78 pr_warn("REPORT TARGET PORT GROUPS allocation length %u"
79 " too small\n", cmd->data_length);
83 buf = transport_kmap_data_sg(cmd);
85 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
86 list_for_each_entry(tg_pt_gp, &su_dev->t10_alua.tg_pt_gps_list,
89 * Check if the Target port group and Target port descriptor list
90 * based on tg_pt_gp_members count will fit into the response payload.
91 * Otherwise, bump rd_len to let the initiator know we have exceeded
92 * the allocation length and the response is truncated.
94 if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) >
96 rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4);
100 * PREF: Preferred target port bit, determine if this
101 * bit should be set for port group.
103 if (tg_pt_gp->tg_pt_gp_pref)
106 * Set the ASYMMETRIC ACCESS State
108 buf[off++] |= (atomic_read(
109 &tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff);
111 * Set supported ASYMMETRIC ACCESS State bits
113 buf[off] = 0x80; /* T_SUP */
114 buf[off] |= 0x40; /* O_SUP */
115 buf[off] |= 0x8; /* U_SUP */
116 buf[off] |= 0x4; /* S_SUP */
117 buf[off] |= 0x2; /* AN_SUP */
118 buf[off++] |= 0x1; /* AO_SUP */
122 buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff);
123 buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff);
125 off++; /* Skip over Reserved */
129 buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
131 * Vendor Specific field
137 buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
140 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
141 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
143 port = tg_pt_gp_mem->tg_pt;
145 * Start Target Port descriptor format
147 * See spc4r17 section 6.2.7 Table 247
149 off += 2; /* Skip over Obsolete */
151 * Set RELATIVE TARGET PORT IDENTIFIER
153 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
154 buf[off++] = (port->sep_rtpi & 0xff);
157 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
159 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
161 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
163 buf[0] = ((rd_len >> 24) & 0xff);
164 buf[1] = ((rd_len >> 16) & 0xff);
165 buf[2] = ((rd_len >> 8) & 0xff);
166 buf[3] = (rd_len & 0xff);
168 transport_kunmap_data_sg(cmd);
170 task->task_scsi_status = GOOD;
171 transport_complete_task(task, 1);
176 * SET_TARGET_PORT_GROUPS for explict ALUA operation.
178 * See spc4r17 section 6.35
180 int target_emulate_set_target_port_groups(struct se_task *task)
182 struct se_cmd *cmd = task->task_se_cmd;
183 struct se_device *dev = cmd->se_dev;
184 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
185 struct se_port *port, *l_port = cmd->se_lun->lun_sep;
186 struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
187 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
188 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *l_tg_pt_gp_mem;
191 u32 len = 4; /* Skip over RESERVED area in header */
192 int alua_access_state, primary = 0, rc;
196 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
199 buf = transport_kmap_data_sg(cmd);
202 * Determine if explict ALUA via SET_TARGET_PORT_GROUPS is allowed
203 * for the local tg_pt_gp.
205 l_tg_pt_gp_mem = l_port->sep_alua_tg_pt_gp_mem;
206 if (!l_tg_pt_gp_mem) {
207 pr_err("Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
208 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
212 spin_lock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
213 l_tg_pt_gp = l_tg_pt_gp_mem->tg_pt_gp;
215 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
216 pr_err("Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n");
217 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
221 rc = (l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA);
222 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
225 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
226 " while TPGS_EXPLICT_ALUA is disabled\n");
227 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
232 ptr = &buf[4]; /* Skip over RESERVED area in header */
234 while (len < cmd->data_length) {
235 alua_access_state = (ptr[0] & 0x0f);
237 * Check the received ALUA access state, and determine if
238 * the state is a primary or secondary target port asymmetric
241 rc = core_alua_check_transition(alua_access_state, &primary);
244 * If the SET TARGET PORT GROUPS attempts to establish
245 * an invalid combination of target port asymmetric
246 * access states or attempts to establish an
247 * unsupported target port asymmetric access state,
248 * then the command shall be terminated with CHECK
249 * CONDITION status, with the sense key set to ILLEGAL
250 * REQUEST, and the additional sense code set to INVALID
251 * FIELD IN PARAMETER LIST.
253 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
259 * If the ASYMMETRIC ACCESS STATE field (see table 267)
260 * specifies a primary target port asymmetric access state,
261 * then the TARGET PORT GROUP OR TARGET PORT field specifies
262 * a primary target port group for which the primary target
263 * port asymmetric access state shall be changed. If the
264 * ASYMMETRIC ACCESS STATE field specifies a secondary target
265 * port asymmetric access state, then the TARGET PORT GROUP OR
266 * TARGET PORT field specifies the relative target port
267 * identifier (see 3.1.120) of the target port for which the
268 * secondary target port asymmetric access state shall be
272 tg_pt_id = get_unaligned_be16(ptr + 2);
274 * Locate the matching target port group ID from
275 * the global tg_pt_gp list
277 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
278 list_for_each_entry(tg_pt_gp,
279 &su_dev->t10_alua.tg_pt_gps_list,
281 if (!tg_pt_gp->tg_pt_gp_valid_id)
284 if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
287 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
288 smp_mb__after_atomic_inc();
289 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
291 rc = core_alua_do_port_transition(tg_pt_gp,
293 alua_access_state, 1);
295 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
296 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
297 smp_mb__after_atomic_dec();
300 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
302 * If not matching target port group ID can be located
303 * throw an exception with ASCQ: INVALID_PARAMETER_LIST
306 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
312 * Extact the RELATIVE TARGET PORT IDENTIFIER to identify
313 * the Target Port in question for the the incoming
314 * SET_TARGET_PORT_GROUPS op.
316 rtpi = get_unaligned_be16(ptr + 2);
318 * Locate the matching relative target port identifer
319 * for the struct se_device storage object.
321 spin_lock(&dev->se_port_lock);
322 list_for_each_entry(port, &dev->dev_sep_list,
324 if (port->sep_rtpi != rtpi)
327 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
328 spin_unlock(&dev->se_port_lock);
330 rc = core_alua_set_tg_pt_secondary_state(
331 tg_pt_gp_mem, port, 1, 1);
333 spin_lock(&dev->se_port_lock);
336 spin_unlock(&dev->se_port_lock);
338 * If not matching relative target port identifier can
339 * be located, throw an exception with ASCQ:
340 * INVALID_PARAMETER_LIST
343 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
354 transport_kunmap_data_sg(cmd);
356 task->task_scsi_status = GOOD;
357 transport_complete_task(task, 1);
362 static inline int core_alua_state_nonoptimized(
365 int nonop_delay_msecs,
369 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
370 * later to determine if processing of this cmd needs to be
371 * temporarily delayed for the Active/NonOptimized primary access state.
373 cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
374 cmd->alua_nonop_delay = nonop_delay_msecs;
378 static inline int core_alua_state_standby(
384 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
385 * spc4r17 section 5.9.2.4.4
394 case RECEIVE_DIAGNOSTIC:
395 case SEND_DIAGNOSTIC:
398 case MI_REPORT_TARGET_PGS:
401 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
404 case MAINTENANCE_OUT:
406 case MO_SET_TARGET_PGS:
409 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
413 case PERSISTENT_RESERVE_IN:
414 case PERSISTENT_RESERVE_OUT:
419 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
426 static inline int core_alua_state_unavailable(
432 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
433 * spc4r17 section 5.9.2.4.5
440 case MI_REPORT_TARGET_PGS:
443 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
446 case MAINTENANCE_OUT:
448 case MO_SET_TARGET_PGS:
451 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
459 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
466 static inline int core_alua_state_transition(
472 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by
473 * spc4r17 section 5.9.2.5
480 case MI_REPORT_TARGET_PGS:
483 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
491 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
499 * Used for alua_type SPC_ALUA_PASSTHROUGH and SPC2_ALUA_DISABLED
500 * in transport_cmd_sequencer(). This function is assigned to
501 * struct t10_alua *->state_check() in core_setup_alua()
503 static int core_alua_state_check_nop(
512 * Used for alua_type SPC3_ALUA_EMULATED in transport_cmd_sequencer().
513 * This function is assigned to struct t10_alua *->state_check() in
516 * Also, this function can return three different return codes to
517 * signal transport_generic_cmd_sequencer()
519 * return 1: Is used to signal LUN not accecsable, and check condition/not ready
520 * return 0: Used to signal success
521 * reutrn -1: Used to signal failure, and invalid cdb field
523 static int core_alua_state_check(
528 struct se_lun *lun = cmd->se_lun;
529 struct se_port *port = lun->lun_sep;
530 struct t10_alua_tg_pt_gp *tg_pt_gp;
531 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
532 int out_alua_state, nonop_delay_msecs;
537 * First, check for a struct se_port specific secondary ALUA target port
538 * access state: OFFLINE
540 if (atomic_read(&port->sep_tg_pt_secondary_offline)) {
541 *alua_ascq = ASCQ_04H_ALUA_OFFLINE;
542 pr_debug("ALUA: Got secondary offline status for local"
544 *alua_ascq = ASCQ_04H_ALUA_OFFLINE;
548 * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
549 * ALUA target port group, to obtain current ALUA access state.
550 * Otherwise look for the underlying struct se_device association with
551 * a ALUA logical unit group.
553 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
554 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
555 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
556 out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
557 nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
558 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
560 * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a separate conditional
561 * statement so the compiler knows explicitly to check this case first.
562 * For the Optimized ALUA access state case, we want to process the
563 * incoming fabric cmd ASAP..
565 if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTMIZED)
568 switch (out_alua_state) {
569 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
570 return core_alua_state_nonoptimized(cmd, cdb,
571 nonop_delay_msecs, alua_ascq);
572 case ALUA_ACCESS_STATE_STANDBY:
573 return core_alua_state_standby(cmd, cdb, alua_ascq);
574 case ALUA_ACCESS_STATE_UNAVAILABLE:
575 return core_alua_state_unavailable(cmd, cdb, alua_ascq);
576 case ALUA_ACCESS_STATE_TRANSITION:
577 return core_alua_state_transition(cmd, cdb, alua_ascq);
579 * OFFLINE is a secondary ALUA target port group access state, that is
580 * handled above with struct se_port->sep_tg_pt_secondary_offline=1
582 case ALUA_ACCESS_STATE_OFFLINE:
584 pr_err("Unknown ALUA access state: 0x%02x\n",
593 * Check implict and explict ALUA state change request.
595 static int core_alua_check_transition(int state, int *primary)
598 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
599 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
600 case ALUA_ACCESS_STATE_STANDBY:
601 case ALUA_ACCESS_STATE_UNAVAILABLE:
603 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
604 * defined as primary target port asymmetric access states.
608 case ALUA_ACCESS_STATE_OFFLINE:
610 * OFFLINE state is defined as a secondary target port
611 * asymmetric access state.
616 pr_err("Unknown ALUA access state: 0x%02x\n", state);
623 static char *core_alua_dump_state(int state)
626 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
627 return "Active/Optimized";
628 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
629 return "Active/NonOptimized";
630 case ALUA_ACCESS_STATE_STANDBY:
632 case ALUA_ACCESS_STATE_UNAVAILABLE:
633 return "Unavailable";
634 case ALUA_ACCESS_STATE_OFFLINE:
643 char *core_alua_dump_status(int status)
646 case ALUA_STATUS_NONE:
648 case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG:
649 return "Altered by Explict STPG";
650 case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA:
651 return "Altered by Implict ALUA";
660 * Used by fabric modules to determine when we need to delay processing
661 * for the Active/NonOptimized paths..
663 int core_alua_check_nonop_delay(
666 if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
671 * The ALUA Active/NonOptimized access state delay can be disabled
672 * in via configfs with a value of zero
674 if (!cmd->alua_nonop_delay)
677 * struct se_cmd->alua_nonop_delay gets set by a target port group
678 * defined interval in core_alua_state_nonoptimized()
680 msleep_interruptible(cmd->alua_nonop_delay);
683 EXPORT_SYMBOL(core_alua_check_nonop_delay);
686 * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
689 static int core_alua_write_tpg_metadata(
691 unsigned char *md_buf,
697 int flags = O_RDWR | O_CREAT | O_TRUNC, ret;
699 memset(iov, 0, sizeof(struct iovec));
701 file = filp_open(path, flags, 0600);
702 if (IS_ERR(file) || !file || !file->f_dentry) {
703 pr_err("filp_open(%s) for ALUA metadata failed\n",
708 iov[0].iov_base = &md_buf[0];
709 iov[0].iov_len = md_buf_len;
713 ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
717 pr_err("Error writing ALUA metadata file: %s\n", path);
718 filp_close(file, NULL);
721 filp_close(file, NULL);
727 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
729 static int core_alua_update_tpg_primary_metadata(
730 struct t10_alua_tg_pt_gp *tg_pt_gp,
732 unsigned char *md_buf)
734 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
735 struct t10_wwn *wwn = &su_dev->t10_wwn;
736 char path[ALUA_METADATA_PATH_LEN];
739 memset(path, 0, ALUA_METADATA_PATH_LEN);
741 len = snprintf(md_buf, tg_pt_gp->tg_pt_gp_md_buf_len,
743 "alua_access_state=0x%02x\n"
744 "alua_access_status=0x%02x\n",
745 tg_pt_gp->tg_pt_gp_id, primary_state,
746 tg_pt_gp->tg_pt_gp_alua_access_status);
748 snprintf(path, ALUA_METADATA_PATH_LEN,
749 "/var/target/alua/tpgs_%s/%s", &wwn->unit_serial[0],
750 config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
752 return core_alua_write_tpg_metadata(path, md_buf, len);
755 static int core_alua_do_transition_tg_pt(
756 struct t10_alua_tg_pt_gp *tg_pt_gp,
757 struct se_port *l_port,
758 struct se_node_acl *nacl,
759 unsigned char *md_buf,
763 struct se_dev_entry *se_deve;
764 struct se_lun_acl *lacl;
765 struct se_port *port;
766 struct t10_alua_tg_pt_gp_member *mem;
769 * Save the old primary ALUA access state, and set the current state
770 * to ALUA_ACCESS_STATE_TRANSITION.
772 old_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
773 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
774 ALUA_ACCESS_STATE_TRANSITION);
775 tg_pt_gp->tg_pt_gp_alua_access_status = (explict) ?
776 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
777 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
779 * Check for the optional ALUA primary state transition delay
781 if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
782 msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
784 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
785 list_for_each_entry(mem, &tg_pt_gp->tg_pt_gp_mem_list,
789 * After an implicit target port asymmetric access state
790 * change, a device server shall establish a unit attention
791 * condition for the initiator port associated with every I_T
792 * nexus with the additional sense code set to ASYMMETRIC
793 * ACCESS STATE CHAGED.
795 * After an explicit target port asymmetric access state
796 * change, a device server shall establish a unit attention
797 * condition with the additional sense code set to ASYMMETRIC
798 * ACCESS STATE CHANGED for the initiator port associated with
799 * every I_T nexus other than the I_T nexus on which the SET
800 * TARGET PORT GROUPS command
802 atomic_inc(&mem->tg_pt_gp_mem_ref_cnt);
803 smp_mb__after_atomic_inc();
804 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
806 spin_lock_bh(&port->sep_alua_lock);
807 list_for_each_entry(se_deve, &port->sep_alua_list,
809 lacl = se_deve->se_lun_acl;
811 * se_deve->se_lun_acl pointer may be NULL for a
812 * entry created without explict Node+MappedLUN ACLs
818 (nacl != NULL) && (nacl == lacl->se_lun_nacl) &&
819 (l_port != NULL) && (l_port == port))
822 core_scsi3_ua_allocate(lacl->se_lun_nacl,
823 se_deve->mapped_lun, 0x2A,
824 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
826 spin_unlock_bh(&port->sep_alua_lock);
828 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
829 atomic_dec(&mem->tg_pt_gp_mem_ref_cnt);
830 smp_mb__after_atomic_dec();
832 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
834 * Update the ALUA metadata buf that has been allocated in
835 * core_alua_do_port_transition(), this metadata will be written
838 * Note that there is the case where we do not want to update the
839 * metadata when the saved metadata is being parsed in userspace
840 * when setting the existing port access state and access status.
842 * Also note that the failure to write out the ALUA metadata to
843 * struct file does NOT affect the actual ALUA transition.
845 if (tg_pt_gp->tg_pt_gp_write_metadata) {
846 mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
847 core_alua_update_tpg_primary_metadata(tg_pt_gp,
849 mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
852 * Set the current primary ALUA access state to the requested new state
854 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, new_state);
856 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
857 " from primary access state %s to %s\n", (explict) ? "explict" :
858 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
859 tg_pt_gp->tg_pt_gp_id, core_alua_dump_state(old_state),
860 core_alua_dump_state(new_state));
865 int core_alua_do_port_transition(
866 struct t10_alua_tg_pt_gp *l_tg_pt_gp,
867 struct se_device *l_dev,
868 struct se_port *l_port,
869 struct se_node_acl *l_nacl,
873 struct se_device *dev;
874 struct se_port *port;
875 struct se_subsystem_dev *su_dev;
876 struct se_node_acl *nacl;
877 struct t10_alua_lu_gp *lu_gp;
878 struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
879 struct t10_alua_tg_pt_gp *tg_pt_gp;
880 unsigned char *md_buf;
883 if (core_alua_check_transition(new_state, &primary) != 0)
886 md_buf = kzalloc(l_tg_pt_gp->tg_pt_gp_md_buf_len, GFP_KERNEL);
888 pr_err("Unable to allocate buf for ALUA metadata\n");
892 local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
893 spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
894 lu_gp = local_lu_gp_mem->lu_gp;
895 atomic_inc(&lu_gp->lu_gp_ref_cnt);
896 smp_mb__after_atomic_inc();
897 spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
899 * For storage objects that are members of the 'default_lu_gp',
900 * we only do transition on the passed *l_tp_pt_gp, and not
901 * on all of the matching target port groups IDs in default_lu_gp.
903 if (!lu_gp->lu_gp_id) {
905 * core_alua_do_transition_tg_pt() will always return
908 core_alua_do_transition_tg_pt(l_tg_pt_gp, l_port, l_nacl,
909 md_buf, new_state, explict);
910 atomic_dec(&lu_gp->lu_gp_ref_cnt);
911 smp_mb__after_atomic_dec();
916 * For all other LU groups aside from 'default_lu_gp', walk all of
917 * the associated storage objects looking for a matching target port
918 * group ID from the local target port group.
920 spin_lock(&lu_gp->lu_gp_lock);
921 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
924 dev = lu_gp_mem->lu_gp_mem_dev;
925 su_dev = dev->se_sub_dev;
926 atomic_inc(&lu_gp_mem->lu_gp_mem_ref_cnt);
927 smp_mb__after_atomic_inc();
928 spin_unlock(&lu_gp->lu_gp_lock);
930 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
931 list_for_each_entry(tg_pt_gp,
932 &su_dev->t10_alua.tg_pt_gps_list,
935 if (!tg_pt_gp->tg_pt_gp_valid_id)
938 * If the target behavior port asymmetric access state
939 * is changed for any target port group accessiable via
940 * a logical unit within a LU group, the target port
941 * behavior group asymmetric access states for the same
942 * target port group accessible via other logical units
943 * in that LU group will also change.
945 if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
948 if (l_tg_pt_gp == tg_pt_gp) {
955 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
956 smp_mb__after_atomic_inc();
957 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
959 * core_alua_do_transition_tg_pt() will always return
962 core_alua_do_transition_tg_pt(tg_pt_gp, port,
963 nacl, md_buf, new_state, explict);
965 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
966 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
967 smp_mb__after_atomic_dec();
969 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
971 spin_lock(&lu_gp->lu_gp_lock);
972 atomic_dec(&lu_gp_mem->lu_gp_mem_ref_cnt);
973 smp_mb__after_atomic_dec();
975 spin_unlock(&lu_gp->lu_gp_lock);
977 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
978 " Group IDs: %hu %s transition to primary state: %s\n",
979 config_item_name(&lu_gp->lu_gp_group.cg_item),
980 l_tg_pt_gp->tg_pt_gp_id, (explict) ? "explict" : "implict",
981 core_alua_dump_state(new_state));
983 atomic_dec(&lu_gp->lu_gp_ref_cnt);
984 smp_mb__after_atomic_dec();
990 * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
992 static int core_alua_update_tpg_secondary_metadata(
993 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
994 struct se_port *port,
995 unsigned char *md_buf,
998 struct se_portal_group *se_tpg = port->sep_tpg;
999 char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
1002 memset(path, 0, ALUA_METADATA_PATH_LEN);
1003 memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
1005 len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
1006 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
1008 if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
1009 snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
1010 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
1012 len = snprintf(md_buf, md_buf_len, "alua_tg_pt_offline=%d\n"
1013 "alua_tg_pt_status=0x%02x\n",
1014 atomic_read(&port->sep_tg_pt_secondary_offline),
1015 port->sep_tg_pt_secondary_stat);
1017 snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u",
1018 se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
1019 port->sep_lun->unpacked_lun);
1021 return core_alua_write_tpg_metadata(path, md_buf, len);
1024 static int core_alua_set_tg_pt_secondary_state(
1025 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1026 struct se_port *port,
1030 struct t10_alua_tg_pt_gp *tg_pt_gp;
1031 unsigned char *md_buf;
1033 int trans_delay_msecs;
1035 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1036 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1038 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1039 pr_err("Unable to complete secondary state"
1043 trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
1045 * Set the secondary ALUA target port access state to OFFLINE
1046 * or release the previously secondary state for struct se_port
1049 atomic_set(&port->sep_tg_pt_secondary_offline, 1);
1051 atomic_set(&port->sep_tg_pt_secondary_offline, 0);
1053 md_buf_len = tg_pt_gp->tg_pt_gp_md_buf_len;
1054 port->sep_tg_pt_secondary_stat = (explict) ?
1055 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
1056 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
1058 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1059 " to secondary access state: %s\n", (explict) ? "explict" :
1060 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1061 tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
1063 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1065 * Do the optional transition delay after we set the secondary
1066 * ALUA access state.
1068 if (trans_delay_msecs != 0)
1069 msleep_interruptible(trans_delay_msecs);
1071 * See if we need to update the ALUA fabric port metadata for
1072 * secondary state and status
1074 if (port->sep_tg_pt_secondary_write_md) {
1075 md_buf = kzalloc(md_buf_len, GFP_KERNEL);
1077 pr_err("Unable to allocate md_buf for"
1078 " secondary ALUA access metadata\n");
1081 mutex_lock(&port->sep_tg_pt_md_mutex);
1082 core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem, port,
1083 md_buf, md_buf_len);
1084 mutex_unlock(&port->sep_tg_pt_md_mutex);
1092 struct t10_alua_lu_gp *
1093 core_alua_allocate_lu_gp(const char *name, int def_group)
1095 struct t10_alua_lu_gp *lu_gp;
1097 lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
1099 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1100 return ERR_PTR(-ENOMEM);
1102 INIT_LIST_HEAD(&lu_gp->lu_gp_node);
1103 INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
1104 spin_lock_init(&lu_gp->lu_gp_lock);
1105 atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
1108 lu_gp->lu_gp_id = alua_lu_gps_counter++;
1109 lu_gp->lu_gp_valid_id = 1;
1110 alua_lu_gps_count++;
1116 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
1118 struct t10_alua_lu_gp *lu_gp_tmp;
1121 * The lu_gp->lu_gp_id may only be set once..
1123 if (lu_gp->lu_gp_valid_id) {
1124 pr_warn("ALUA LU Group already has a valid ID,"
1125 " ignoring request\n");
1129 spin_lock(&lu_gps_lock);
1130 if (alua_lu_gps_count == 0x0000ffff) {
1131 pr_err("Maximum ALUA alua_lu_gps_count:"
1132 " 0x0000ffff reached\n");
1133 spin_unlock(&lu_gps_lock);
1134 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1138 lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
1139 alua_lu_gps_counter++;
1141 list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
1142 if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
1146 pr_warn("ALUA Logical Unit Group ID: %hu"
1147 " already exists, ignoring request\n",
1149 spin_unlock(&lu_gps_lock);
1154 lu_gp->lu_gp_id = lu_gp_id_tmp;
1155 lu_gp->lu_gp_valid_id = 1;
1156 list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
1157 alua_lu_gps_count++;
1158 spin_unlock(&lu_gps_lock);
1163 static struct t10_alua_lu_gp_member *
1164 core_alua_allocate_lu_gp_mem(struct se_device *dev)
1166 struct t10_alua_lu_gp_member *lu_gp_mem;
1168 lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
1170 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1171 return ERR_PTR(-ENOMEM);
1173 INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
1174 spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
1175 atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
1177 lu_gp_mem->lu_gp_mem_dev = dev;
1178 dev->dev_alua_lu_gp_mem = lu_gp_mem;
1183 void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
1185 struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
1187 * Once we have reached this point, config_item_put() has
1188 * already been called from target_core_alua_drop_lu_gp().
1190 * Here, we remove the *lu_gp from the global list so that
1191 * no associations can be made while we are releasing
1192 * struct t10_alua_lu_gp.
1194 spin_lock(&lu_gps_lock);
1195 list_del(&lu_gp->lu_gp_node);
1196 alua_lu_gps_count--;
1197 spin_unlock(&lu_gps_lock);
1199 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1200 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1201 * released with core_alua_put_lu_gp_from_name()
1203 while (atomic_read(&lu_gp->lu_gp_ref_cnt))
1206 * Release reference to struct t10_alua_lu_gp * from all associated
1209 spin_lock(&lu_gp->lu_gp_lock);
1210 list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
1211 &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1212 if (lu_gp_mem->lu_gp_assoc) {
1213 list_del(&lu_gp_mem->lu_gp_mem_list);
1214 lu_gp->lu_gp_members--;
1215 lu_gp_mem->lu_gp_assoc = 0;
1217 spin_unlock(&lu_gp->lu_gp_lock);
1220 * lu_gp_mem is associated with a single
1221 * struct se_device->dev_alua_lu_gp_mem, and is released when
1222 * struct se_device is released via core_alua_free_lu_gp_mem().
1224 * If the passed lu_gp does NOT match the default_lu_gp, assume
1225 * we want to re-assocate a given lu_gp_mem with default_lu_gp.
1227 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1228 if (lu_gp != default_lu_gp)
1229 __core_alua_attach_lu_gp_mem(lu_gp_mem,
1232 lu_gp_mem->lu_gp = NULL;
1233 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1235 spin_lock(&lu_gp->lu_gp_lock);
1237 spin_unlock(&lu_gp->lu_gp_lock);
1239 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1242 void core_alua_free_lu_gp_mem(struct se_device *dev)
1244 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1245 struct t10_alua *alua = &su_dev->t10_alua;
1246 struct t10_alua_lu_gp *lu_gp;
1247 struct t10_alua_lu_gp_member *lu_gp_mem;
1249 if (alua->alua_type != SPC3_ALUA_EMULATED)
1252 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1256 while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
1259 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1260 lu_gp = lu_gp_mem->lu_gp;
1262 spin_lock(&lu_gp->lu_gp_lock);
1263 if (lu_gp_mem->lu_gp_assoc) {
1264 list_del(&lu_gp_mem->lu_gp_mem_list);
1265 lu_gp->lu_gp_members--;
1266 lu_gp_mem->lu_gp_assoc = 0;
1268 spin_unlock(&lu_gp->lu_gp_lock);
1269 lu_gp_mem->lu_gp = NULL;
1271 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1273 kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
1276 struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
1278 struct t10_alua_lu_gp *lu_gp;
1279 struct config_item *ci;
1281 spin_lock(&lu_gps_lock);
1282 list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
1283 if (!lu_gp->lu_gp_valid_id)
1285 ci = &lu_gp->lu_gp_group.cg_item;
1286 if (!strcmp(config_item_name(ci), name)) {
1287 atomic_inc(&lu_gp->lu_gp_ref_cnt);
1288 spin_unlock(&lu_gps_lock);
1292 spin_unlock(&lu_gps_lock);
1297 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
1299 spin_lock(&lu_gps_lock);
1300 atomic_dec(&lu_gp->lu_gp_ref_cnt);
1301 spin_unlock(&lu_gps_lock);
1305 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1307 void __core_alua_attach_lu_gp_mem(
1308 struct t10_alua_lu_gp_member *lu_gp_mem,
1309 struct t10_alua_lu_gp *lu_gp)
1311 spin_lock(&lu_gp->lu_gp_lock);
1312 lu_gp_mem->lu_gp = lu_gp;
1313 lu_gp_mem->lu_gp_assoc = 1;
1314 list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
1315 lu_gp->lu_gp_members++;
1316 spin_unlock(&lu_gp->lu_gp_lock);
1320 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1322 void __core_alua_drop_lu_gp_mem(
1323 struct t10_alua_lu_gp_member *lu_gp_mem,
1324 struct t10_alua_lu_gp *lu_gp)
1326 spin_lock(&lu_gp->lu_gp_lock);
1327 list_del(&lu_gp_mem->lu_gp_mem_list);
1328 lu_gp_mem->lu_gp = NULL;
1329 lu_gp_mem->lu_gp_assoc = 0;
1330 lu_gp->lu_gp_members--;
1331 spin_unlock(&lu_gp->lu_gp_lock);
1334 struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(
1335 struct se_subsystem_dev *su_dev,
1339 struct t10_alua_tg_pt_gp *tg_pt_gp;
1341 tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
1343 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1346 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
1347 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_mem_list);
1348 mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
1349 spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
1350 atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
1351 tg_pt_gp->tg_pt_gp_su_dev = su_dev;
1352 tg_pt_gp->tg_pt_gp_md_buf_len = ALUA_MD_BUF_LEN;
1353 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1354 ALUA_ACCESS_STATE_ACTIVE_OPTMIZED);
1356 * Enable both explict and implict ALUA support by default
1358 tg_pt_gp->tg_pt_gp_alua_access_type =
1359 TPGS_EXPLICT_ALUA | TPGS_IMPLICT_ALUA;
1361 * Set the default Active/NonOptimized Delay in milliseconds
1363 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
1364 tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
1367 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1368 tg_pt_gp->tg_pt_gp_id =
1369 su_dev->t10_alua.alua_tg_pt_gps_counter++;
1370 tg_pt_gp->tg_pt_gp_valid_id = 1;
1371 su_dev->t10_alua.alua_tg_pt_gps_count++;
1372 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1373 &su_dev->t10_alua.tg_pt_gps_list);
1374 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1380 int core_alua_set_tg_pt_gp_id(
1381 struct t10_alua_tg_pt_gp *tg_pt_gp,
1384 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
1385 struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
1386 u16 tg_pt_gp_id_tmp;
1388 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1390 if (tg_pt_gp->tg_pt_gp_valid_id) {
1391 pr_warn("ALUA TG PT Group already has a valid ID,"
1392 " ignoring request\n");
1396 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1397 if (su_dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
1398 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1399 " 0x0000ffff reached\n");
1400 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1401 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1405 tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
1406 su_dev->t10_alua.alua_tg_pt_gps_counter++;
1408 list_for_each_entry(tg_pt_gp_tmp, &su_dev->t10_alua.tg_pt_gps_list,
1410 if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
1414 pr_err("ALUA Target Port Group ID: %hu already"
1415 " exists, ignoring request\n", tg_pt_gp_id);
1416 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1421 tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
1422 tg_pt_gp->tg_pt_gp_valid_id = 1;
1423 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1424 &su_dev->t10_alua.tg_pt_gps_list);
1425 su_dev->t10_alua.alua_tg_pt_gps_count++;
1426 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1431 struct t10_alua_tg_pt_gp_member *core_alua_allocate_tg_pt_gp_mem(
1432 struct se_port *port)
1434 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1436 tg_pt_gp_mem = kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache,
1438 if (!tg_pt_gp_mem) {
1439 pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n");
1440 return ERR_PTR(-ENOMEM);
1442 INIT_LIST_HEAD(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1443 spin_lock_init(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1444 atomic_set(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt, 0);
1446 tg_pt_gp_mem->tg_pt = port;
1447 port->sep_alua_tg_pt_gp_mem = tg_pt_gp_mem;
1449 return tg_pt_gp_mem;
1452 void core_alua_free_tg_pt_gp(
1453 struct t10_alua_tg_pt_gp *tg_pt_gp)
1455 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
1456 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *tg_pt_gp_mem_tmp;
1458 * Once we have reached this point, config_item_put() has already
1459 * been called from target_core_alua_drop_tg_pt_gp().
1461 * Here we remove *tg_pt_gp from the global list so that
1462 * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS
1463 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1465 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1466 list_del(&tg_pt_gp->tg_pt_gp_list);
1467 su_dev->t10_alua.alua_tg_pt_gps_counter--;
1468 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1470 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1471 * core_alua_get_tg_pt_gp_by_name() in
1472 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1473 * to be released with core_alua_put_tg_pt_gp_from_name().
1475 while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
1478 * Release reference to struct t10_alua_tg_pt_gp from all associated
1481 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1482 list_for_each_entry_safe(tg_pt_gp_mem, tg_pt_gp_mem_tmp,
1483 &tg_pt_gp->tg_pt_gp_mem_list, tg_pt_gp_mem_list) {
1484 if (tg_pt_gp_mem->tg_pt_gp_assoc) {
1485 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1486 tg_pt_gp->tg_pt_gp_members--;
1487 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1489 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1491 * tg_pt_gp_mem is associated with a single
1492 * se_port->sep_alua_tg_pt_gp_mem, and is released via
1493 * core_alua_free_tg_pt_gp_mem().
1495 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1496 * assume we want to re-assocate a given tg_pt_gp_mem with
1499 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1500 if (tg_pt_gp != su_dev->t10_alua.default_tg_pt_gp) {
1501 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
1502 su_dev->t10_alua.default_tg_pt_gp);
1504 tg_pt_gp_mem->tg_pt_gp = NULL;
1505 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1507 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1509 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1511 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1514 void core_alua_free_tg_pt_gp_mem(struct se_port *port)
1516 struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
1517 struct t10_alua *alua = &su_dev->t10_alua;
1518 struct t10_alua_tg_pt_gp *tg_pt_gp;
1519 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1521 if (alua->alua_type != SPC3_ALUA_EMULATED)
1524 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1528 while (atomic_read(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt))
1531 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1532 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1534 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1535 if (tg_pt_gp_mem->tg_pt_gp_assoc) {
1536 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1537 tg_pt_gp->tg_pt_gp_members--;
1538 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1540 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1541 tg_pt_gp_mem->tg_pt_gp = NULL;
1543 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1545 kmem_cache_free(t10_alua_tg_pt_gp_mem_cache, tg_pt_gp_mem);
1548 static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
1549 struct se_subsystem_dev *su_dev,
1552 struct t10_alua_tg_pt_gp *tg_pt_gp;
1553 struct config_item *ci;
1555 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1556 list_for_each_entry(tg_pt_gp, &su_dev->t10_alua.tg_pt_gps_list,
1558 if (!tg_pt_gp->tg_pt_gp_valid_id)
1560 ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1561 if (!strcmp(config_item_name(ci), name)) {
1562 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
1563 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1567 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1572 static void core_alua_put_tg_pt_gp_from_name(
1573 struct t10_alua_tg_pt_gp *tg_pt_gp)
1575 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
1577 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1578 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
1579 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1583 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1585 void __core_alua_attach_tg_pt_gp_mem(
1586 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1587 struct t10_alua_tg_pt_gp *tg_pt_gp)
1589 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1590 tg_pt_gp_mem->tg_pt_gp = tg_pt_gp;
1591 tg_pt_gp_mem->tg_pt_gp_assoc = 1;
1592 list_add_tail(&tg_pt_gp_mem->tg_pt_gp_mem_list,
1593 &tg_pt_gp->tg_pt_gp_mem_list);
1594 tg_pt_gp->tg_pt_gp_members++;
1595 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1599 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1601 static void __core_alua_drop_tg_pt_gp_mem(
1602 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1603 struct t10_alua_tg_pt_gp *tg_pt_gp)
1605 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1606 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1607 tg_pt_gp_mem->tg_pt_gp = NULL;
1608 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1609 tg_pt_gp->tg_pt_gp_members--;
1610 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1613 ssize_t core_alua_show_tg_pt_gp_info(struct se_port *port, char *page)
1615 struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
1616 struct config_item *tg_pt_ci;
1617 struct t10_alua *alua = &su_dev->t10_alua;
1618 struct t10_alua_tg_pt_gp *tg_pt_gp;
1619 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1622 if (alua->alua_type != SPC3_ALUA_EMULATED)
1625 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1629 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1630 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1632 tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1633 len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
1634 " %hu\nTG Port Primary Access State: %s\nTG Port "
1635 "Primary Access Status: %s\nTG Port Secondary Access"
1636 " State: %s\nTG Port Secondary Access Status: %s\n",
1637 config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
1638 core_alua_dump_state(atomic_read(
1639 &tg_pt_gp->tg_pt_gp_alua_access_state)),
1640 core_alua_dump_status(
1641 tg_pt_gp->tg_pt_gp_alua_access_status),
1642 (atomic_read(&port->sep_tg_pt_secondary_offline)) ?
1644 core_alua_dump_status(port->sep_tg_pt_secondary_stat));
1646 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1651 ssize_t core_alua_store_tg_pt_gp_info(
1652 struct se_port *port,
1656 struct se_portal_group *tpg;
1658 struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
1659 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
1660 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1661 unsigned char buf[TG_PT_GROUP_NAME_BUF];
1664 tpg = port->sep_tpg;
1665 lun = port->sep_lun;
1667 if (su_dev->t10_alua.alua_type != SPC3_ALUA_EMULATED) {
1668 pr_warn("SPC3_ALUA_EMULATED not enabled for"
1669 " %s/tpgt_%hu/%s\n", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1670 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1671 config_item_name(&lun->lun_group.cg_item));
1675 if (count > TG_PT_GROUP_NAME_BUF) {
1676 pr_err("ALUA Target Port Group alias too large!\n");
1679 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
1680 memcpy(buf, page, count);
1682 * Any ALUA target port group alias besides "NULL" means we will be
1683 * making a new group association.
1685 if (strcmp(strstrip(buf), "NULL")) {
1687 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1688 * struct t10_alua_tg_pt_gp. This reference is released with
1689 * core_alua_put_tg_pt_gp_from_name() below.
1691 tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(su_dev,
1696 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1697 if (!tg_pt_gp_mem) {
1699 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1700 pr_err("NULL struct se_port->sep_alua_tg_pt_gp_mem pointer\n");
1704 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1705 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1708 * Clearing an existing tg_pt_gp association, and replacing
1709 * with the default_tg_pt_gp.
1711 if (!tg_pt_gp_new) {
1712 pr_debug("Target_Core_ConfigFS: Moving"
1713 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1714 " alua/%s, ID: %hu back to"
1715 " default_tg_pt_gp\n",
1716 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1717 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1718 config_item_name(&lun->lun_group.cg_item),
1720 &tg_pt_gp->tg_pt_gp_group.cg_item),
1721 tg_pt_gp->tg_pt_gp_id);
1723 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1724 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
1725 su_dev->t10_alua.default_tg_pt_gp);
1726 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1731 * Removing existing association of tg_pt_gp_mem with tg_pt_gp
1733 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1737 * Associate tg_pt_gp_mem with tg_pt_gp_new.
1739 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp_new);
1740 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1741 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
1742 " Target Port Group: alua/%s, ID: %hu\n", (move) ?
1743 "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1744 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1745 config_item_name(&lun->lun_group.cg_item),
1746 config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
1747 tg_pt_gp_new->tg_pt_gp_id);
1749 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1753 ssize_t core_alua_show_access_type(
1754 struct t10_alua_tg_pt_gp *tg_pt_gp,
1757 if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA) &&
1758 (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA))
1759 return sprintf(page, "Implict and Explict\n");
1760 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)
1761 return sprintf(page, "Implict\n");
1762 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA)
1763 return sprintf(page, "Explict\n");
1765 return sprintf(page, "None\n");
1768 ssize_t core_alua_store_access_type(
1769 struct t10_alua_tg_pt_gp *tg_pt_gp,
1776 ret = strict_strtoul(page, 0, &tmp);
1778 pr_err("Unable to extract alua_access_type\n");
1781 if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
1782 pr_err("Illegal value for alua_access_type:"
1787 tg_pt_gp->tg_pt_gp_alua_access_type =
1788 TPGS_IMPLICT_ALUA | TPGS_EXPLICT_ALUA;
1790 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICT_ALUA;
1792 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICT_ALUA;
1794 tg_pt_gp->tg_pt_gp_alua_access_type = 0;
1799 ssize_t core_alua_show_nonop_delay_msecs(
1800 struct t10_alua_tg_pt_gp *tg_pt_gp,
1803 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
1806 ssize_t core_alua_store_nonop_delay_msecs(
1807 struct t10_alua_tg_pt_gp *tg_pt_gp,
1814 ret = strict_strtoul(page, 0, &tmp);
1816 pr_err("Unable to extract nonop_delay_msecs\n");
1819 if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
1820 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
1821 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
1822 ALUA_MAX_NONOP_DELAY_MSECS);
1825 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
1830 ssize_t core_alua_show_trans_delay_msecs(
1831 struct t10_alua_tg_pt_gp *tg_pt_gp,
1834 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
1837 ssize_t core_alua_store_trans_delay_msecs(
1838 struct t10_alua_tg_pt_gp *tg_pt_gp,
1845 ret = strict_strtoul(page, 0, &tmp);
1847 pr_err("Unable to extract trans_delay_msecs\n");
1850 if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
1851 pr_err("Passed trans_delay_msecs: %lu, exceeds"
1852 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
1853 ALUA_MAX_TRANS_DELAY_MSECS);
1856 tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
1861 ssize_t core_alua_show_preferred_bit(
1862 struct t10_alua_tg_pt_gp *tg_pt_gp,
1865 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
1868 ssize_t core_alua_store_preferred_bit(
1869 struct t10_alua_tg_pt_gp *tg_pt_gp,
1876 ret = strict_strtoul(page, 0, &tmp);
1878 pr_err("Unable to extract preferred ALUA value\n");
1881 if ((tmp != 0) && (tmp != 1)) {
1882 pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
1885 tg_pt_gp->tg_pt_gp_pref = (int)tmp;
1890 ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
1895 return sprintf(page, "%d\n",
1896 atomic_read(&lun->lun_sep->sep_tg_pt_secondary_offline));
1899 ssize_t core_alua_store_offline_bit(
1904 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1911 ret = strict_strtoul(page, 0, &tmp);
1913 pr_err("Unable to extract alua_tg_pt_offline value\n");
1916 if ((tmp != 0) && (tmp != 1)) {
1917 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
1921 tg_pt_gp_mem = lun->lun_sep->sep_alua_tg_pt_gp_mem;
1922 if (!tg_pt_gp_mem) {
1923 pr_err("Unable to locate *tg_pt_gp_mem\n");
1927 ret = core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem,
1928 lun->lun_sep, 0, (int)tmp);
1935 ssize_t core_alua_show_secondary_status(
1939 return sprintf(page, "%d\n", lun->lun_sep->sep_tg_pt_secondary_stat);
1942 ssize_t core_alua_store_secondary_status(
1950 ret = strict_strtoul(page, 0, &tmp);
1952 pr_err("Unable to extract alua_tg_pt_status\n");
1955 if ((tmp != ALUA_STATUS_NONE) &&
1956 (tmp != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
1957 (tmp != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
1958 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
1962 lun->lun_sep->sep_tg_pt_secondary_stat = (int)tmp;
1967 ssize_t core_alua_show_secondary_write_metadata(
1971 return sprintf(page, "%d\n",
1972 lun->lun_sep->sep_tg_pt_secondary_write_md);
1975 ssize_t core_alua_store_secondary_write_metadata(
1983 ret = strict_strtoul(page, 0, &tmp);
1985 pr_err("Unable to extract alua_tg_pt_write_md\n");
1988 if ((tmp != 0) && (tmp != 1)) {
1989 pr_err("Illegal value for alua_tg_pt_write_md:"
1993 lun->lun_sep->sep_tg_pt_secondary_write_md = (int)tmp;
1998 int core_setup_alua(struct se_device *dev, int force_pt)
2000 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
2001 struct t10_alua *alua = &su_dev->t10_alua;
2002 struct t10_alua_lu_gp_member *lu_gp_mem;
2004 * If this device is from Target_Core_Mod/pSCSI, use the ALUA logic
2005 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
2006 * cause a problem because libata and some SATA RAID HBAs appear
2007 * under Linux/SCSI, but emulate SCSI logic themselves.
2009 if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
2010 !(dev->se_sub_dev->se_dev_attrib.emulate_alua)) || force_pt) {
2011 alua->alua_type = SPC_ALUA_PASSTHROUGH;
2012 alua->alua_state_check = &core_alua_state_check_nop;
2013 pr_debug("%s: Using SPC_ALUA_PASSTHROUGH, no ALUA"
2014 " emulation\n", dev->transport->name);
2018 * If SPC-3 or above is reported by real or emulated struct se_device,
2019 * use emulated ALUA.
2021 if (dev->transport->get_device_rev(dev) >= SCSI_3) {
2022 pr_debug("%s: Enabling ALUA Emulation for SPC-3"
2023 " device\n", dev->transport->name);
2025 * Associate this struct se_device with the default ALUA
2028 lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
2029 if (IS_ERR(lu_gp_mem))
2030 return PTR_ERR(lu_gp_mem);
2032 alua->alua_type = SPC3_ALUA_EMULATED;
2033 alua->alua_state_check = &core_alua_state_check;
2034 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2035 __core_alua_attach_lu_gp_mem(lu_gp_mem,
2037 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2039 pr_debug("%s: Adding to default ALUA LU Group:"
2040 " core/alua/lu_gps/default_lu_gp\n",
2041 dev->transport->name);
2043 alua->alua_type = SPC2_ALUA_DISABLED;
2044 alua->alua_state_check = &core_alua_state_check_nop;
2045 pr_debug("%s: Disabling ALUA Emulation for SPC-2"
2046 " device\n", dev->transport->name);