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 <scsi/scsi.h>
31 #include <scsi/scsi_cmnd.h>
33 #include <target/target_core_base.h>
34 #include <target/target_core_device.h>
35 #include <target/target_core_transport.h>
36 #include <target/target_core_fabric_ops.h>
37 #include <target/target_core_configfs.h>
39 #include "target_core_alua.h"
40 #include "target_core_hba.h"
41 #include "target_core_ua.h"
43 static int core_alua_check_transition(int state, int *primary);
44 static int core_alua_set_tg_pt_secondary_state(
45 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
46 struct se_port *port, int explict, int offline);
48 static u16 alua_lu_gps_counter;
49 static u32 alua_lu_gps_count;
51 static DEFINE_SPINLOCK(lu_gps_lock);
52 static LIST_HEAD(lu_gps_list);
54 struct t10_alua_lu_gp *default_lu_gp;
57 * REPORT_TARGET_PORT_GROUPS
59 * See spc4r17 section 6.27
61 int target_emulate_report_target_port_groups(struct se_task *task)
63 struct se_cmd *cmd = task->task_se_cmd;
64 struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
66 struct t10_alua_tg_pt_gp *tg_pt_gp;
67 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
69 u32 rd_len = 0, off = 4; /* Skip over RESERVED area to first
70 Target port group descriptor */
72 * Need at least 4 bytes of response data or else we can't
73 * even fit the return data length.
75 if (cmd->data_length < 4) {
76 pr_warn("REPORT TARGET PORT GROUPS allocation length %u"
77 " too small\n", cmd->data_length);
81 buf = transport_kmap_first_data_page(cmd);
83 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
84 list_for_each_entry(tg_pt_gp, &su_dev->t10_alua.tg_pt_gps_list,
87 * Check if the Target port group and Target port descriptor list
88 * based on tg_pt_gp_members count will fit into the response payload.
89 * Otherwise, bump rd_len to let the initiator know we have exceeded
90 * the allocation length and the response is truncated.
92 if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) >
94 rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4);
98 * PREF: Preferred target port bit, determine if this
99 * bit should be set for port group.
101 if (tg_pt_gp->tg_pt_gp_pref)
104 * Set the ASYMMETRIC ACCESS State
106 buf[off++] |= (atomic_read(
107 &tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff);
109 * Set supported ASYMMETRIC ACCESS State bits
111 buf[off] = 0x80; /* T_SUP */
112 buf[off] |= 0x40; /* O_SUP */
113 buf[off] |= 0x8; /* U_SUP */
114 buf[off] |= 0x4; /* S_SUP */
115 buf[off] |= 0x2; /* AN_SUP */
116 buf[off++] |= 0x1; /* AO_SUP */
120 buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff);
121 buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff);
123 off++; /* Skip over Reserved */
127 buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
129 * Vendor Specific field
135 buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
138 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
139 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
141 port = tg_pt_gp_mem->tg_pt;
143 * Start Target Port descriptor format
145 * See spc4r17 section 6.2.7 Table 247
147 off += 2; /* Skip over Obsolete */
149 * Set RELATIVE TARGET PORT IDENTIFIER
151 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
152 buf[off++] = (port->sep_rtpi & 0xff);
155 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
157 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
159 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
161 buf[0] = ((rd_len >> 24) & 0xff);
162 buf[1] = ((rd_len >> 16) & 0xff);
163 buf[2] = ((rd_len >> 8) & 0xff);
164 buf[3] = (rd_len & 0xff);
166 transport_kunmap_first_data_page(cmd);
168 task->task_scsi_status = GOOD;
169 transport_complete_task(task, 1);
174 * SET_TARGET_PORT_GROUPS for explict ALUA operation.
176 * See spc4r17 section 6.35
178 int target_emulate_set_target_port_groups(struct se_task *task)
180 struct se_cmd *cmd = task->task_se_cmd;
181 struct se_device *dev = cmd->se_dev;
182 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
183 struct se_port *port, *l_port = cmd->se_lun->lun_sep;
184 struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
185 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
186 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *l_tg_pt_gp_mem;
189 u32 len = 4; /* Skip over RESERVED area in header */
190 int alua_access_state, primary = 0, rc;
194 return PYX_TRANSPORT_LU_COMM_FAILURE;
196 buf = transport_kmap_first_data_page(cmd);
199 * Determine if explict ALUA via SET_TARGET_PORT_GROUPS is allowed
200 * for the local tg_pt_gp.
202 l_tg_pt_gp_mem = l_port->sep_alua_tg_pt_gp_mem;
203 if (!l_tg_pt_gp_mem) {
204 pr_err("Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
205 rc = PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
208 spin_lock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
209 l_tg_pt_gp = l_tg_pt_gp_mem->tg_pt_gp;
211 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
212 pr_err("Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n");
213 rc = PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
216 rc = (l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA);
217 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
220 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
221 " while TPGS_EXPLICT_ALUA is disabled\n");
222 rc = PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
226 ptr = &buf[4]; /* Skip over RESERVED area in header */
228 while (len < cmd->data_length) {
229 alua_access_state = (ptr[0] & 0x0f);
231 * Check the received ALUA access state, and determine if
232 * the state is a primary or secondary target port asymmetric
235 rc = core_alua_check_transition(alua_access_state, &primary);
238 * If the SET TARGET PORT GROUPS attempts to establish
239 * an invalid combination of target port asymmetric
240 * access states or attempts to establish an
241 * unsupported target port asymmetric access state,
242 * then the command shall be terminated with CHECK
243 * CONDITION status, with the sense key set to ILLEGAL
244 * REQUEST, and the additional sense code set to INVALID
245 * FIELD IN PARAMETER LIST.
247 rc = PYX_TRANSPORT_INVALID_PARAMETER_LIST;
252 * If the ASYMMETRIC ACCESS STATE field (see table 267)
253 * specifies a primary target port asymmetric access state,
254 * then the TARGET PORT GROUP OR TARGET PORT field specifies
255 * a primary target port group for which the primary target
256 * port asymmetric access state shall be changed. If the
257 * ASYMMETRIC ACCESS STATE field specifies a secondary target
258 * port asymmetric access state, then the TARGET PORT GROUP OR
259 * TARGET PORT field specifies the relative target port
260 * identifier (see 3.1.120) of the target port for which the
261 * secondary target port asymmetric access state shall be
265 tg_pt_id = ((ptr[2] << 8) & 0xff);
266 tg_pt_id |= (ptr[3] & 0xff);
268 * Locate the matching target port group ID from
269 * the global tg_pt_gp list
271 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
272 list_for_each_entry(tg_pt_gp,
273 &su_dev->t10_alua.tg_pt_gps_list,
275 if (!tg_pt_gp->tg_pt_gp_valid_id)
278 if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
281 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
282 smp_mb__after_atomic_inc();
283 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
285 rc = core_alua_do_port_transition(tg_pt_gp,
287 alua_access_state, 1);
289 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
290 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
291 smp_mb__after_atomic_dec();
294 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
296 * If not matching target port group ID can be located
297 * throw an exception with ASCQ: INVALID_PARAMETER_LIST
300 rc = PYX_TRANSPORT_INVALID_PARAMETER_LIST;
305 * Extact the RELATIVE TARGET PORT IDENTIFIER to identify
306 * the Target Port in question for the the incoming
307 * SET_TARGET_PORT_GROUPS op.
309 rtpi = ((ptr[2] << 8) & 0xff);
310 rtpi |= (ptr[3] & 0xff);
312 * Locate the matching relative target port identifer
313 * for the struct se_device storage object.
315 spin_lock(&dev->se_port_lock);
316 list_for_each_entry(port, &dev->dev_sep_list,
318 if (port->sep_rtpi != rtpi)
321 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
322 spin_unlock(&dev->se_port_lock);
324 rc = core_alua_set_tg_pt_secondary_state(
325 tg_pt_gp_mem, port, 1, 1);
327 spin_lock(&dev->se_port_lock);
330 spin_unlock(&dev->se_port_lock);
332 * If not matching relative target port identifier can
333 * be located, throw an exception with ASCQ:
334 * INVALID_PARAMETER_LIST
337 rc = PYX_TRANSPORT_INVALID_PARAMETER_LIST;
347 transport_kunmap_first_data_page(cmd);
348 task->task_scsi_status = GOOD;
349 transport_complete_task(task, 1);
353 static inline int core_alua_state_nonoptimized(
356 int nonop_delay_msecs,
360 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
361 * later to determine if processing of this cmd needs to be
362 * temporarily delayed for the Active/NonOptimized primary access state.
364 cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
365 cmd->alua_nonop_delay = nonop_delay_msecs;
369 static inline int core_alua_state_standby(
375 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
376 * spc4r17 section 5.9.2.4.4
385 case RECEIVE_DIAGNOSTIC:
386 case SEND_DIAGNOSTIC:
389 case MI_REPORT_TARGET_PGS:
392 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
395 case MAINTENANCE_OUT:
397 case MO_SET_TARGET_PGS:
400 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
404 case PERSISTENT_RESERVE_IN:
405 case PERSISTENT_RESERVE_OUT:
410 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
417 static inline int core_alua_state_unavailable(
423 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
424 * spc4r17 section 5.9.2.4.5
431 case MI_REPORT_TARGET_PGS:
434 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
437 case MAINTENANCE_OUT:
439 case MO_SET_TARGET_PGS:
442 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
450 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
457 static inline int core_alua_state_transition(
463 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by
464 * spc4r17 section 5.9.2.5
471 case MI_REPORT_TARGET_PGS:
474 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
482 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
490 * Used for alua_type SPC_ALUA_PASSTHROUGH and SPC2_ALUA_DISABLED
491 * in transport_cmd_sequencer(). This function is assigned to
492 * struct t10_alua *->state_check() in core_setup_alua()
494 static int core_alua_state_check_nop(
503 * Used for alua_type SPC3_ALUA_EMULATED in transport_cmd_sequencer().
504 * This function is assigned to struct t10_alua *->state_check() in
507 * Also, this function can return three different return codes to
508 * signal transport_generic_cmd_sequencer()
510 * return 1: Is used to signal LUN not accecsable, and check condition/not ready
511 * return 0: Used to signal success
512 * reutrn -1: Used to signal failure, and invalid cdb field
514 static int core_alua_state_check(
519 struct se_lun *lun = cmd->se_lun;
520 struct se_port *port = lun->lun_sep;
521 struct t10_alua_tg_pt_gp *tg_pt_gp;
522 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
523 int out_alua_state, nonop_delay_msecs;
528 * First, check for a struct se_port specific secondary ALUA target port
529 * access state: OFFLINE
531 if (atomic_read(&port->sep_tg_pt_secondary_offline)) {
532 *alua_ascq = ASCQ_04H_ALUA_OFFLINE;
533 pr_debug("ALUA: Got secondary offline status for local"
535 *alua_ascq = ASCQ_04H_ALUA_OFFLINE;
539 * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
540 * ALUA target port group, to obtain current ALUA access state.
541 * Otherwise look for the underlying struct se_device association with
542 * a ALUA logical unit group.
544 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
545 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
546 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
547 out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
548 nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
549 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
551 * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a separate conditional
552 * statement so the compiler knows explicitly to check this case first.
553 * For the Optimized ALUA access state case, we want to process the
554 * incoming fabric cmd ASAP..
556 if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTMIZED)
559 switch (out_alua_state) {
560 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
561 return core_alua_state_nonoptimized(cmd, cdb,
562 nonop_delay_msecs, alua_ascq);
563 case ALUA_ACCESS_STATE_STANDBY:
564 return core_alua_state_standby(cmd, cdb, alua_ascq);
565 case ALUA_ACCESS_STATE_UNAVAILABLE:
566 return core_alua_state_unavailable(cmd, cdb, alua_ascq);
567 case ALUA_ACCESS_STATE_TRANSITION:
568 return core_alua_state_transition(cmd, cdb, alua_ascq);
570 * OFFLINE is a secondary ALUA target port group access state, that is
571 * handled above with struct se_port->sep_tg_pt_secondary_offline=1
573 case ALUA_ACCESS_STATE_OFFLINE:
575 pr_err("Unknown ALUA access state: 0x%02x\n",
584 * Check implict and explict ALUA state change request.
586 static int core_alua_check_transition(int state, int *primary)
589 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
590 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
591 case ALUA_ACCESS_STATE_STANDBY:
592 case ALUA_ACCESS_STATE_UNAVAILABLE:
594 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
595 * defined as primary target port asymmetric access states.
599 case ALUA_ACCESS_STATE_OFFLINE:
601 * OFFLINE state is defined as a secondary target port
602 * asymmetric access state.
607 pr_err("Unknown ALUA access state: 0x%02x\n", state);
614 static char *core_alua_dump_state(int state)
617 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
618 return "Active/Optimized";
619 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
620 return "Active/NonOptimized";
621 case ALUA_ACCESS_STATE_STANDBY:
623 case ALUA_ACCESS_STATE_UNAVAILABLE:
624 return "Unavailable";
625 case ALUA_ACCESS_STATE_OFFLINE:
634 char *core_alua_dump_status(int status)
637 case ALUA_STATUS_NONE:
639 case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG:
640 return "Altered by Explict STPG";
641 case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA:
642 return "Altered by Implict ALUA";
651 * Used by fabric modules to determine when we need to delay processing
652 * for the Active/NonOptimized paths..
654 int core_alua_check_nonop_delay(
657 if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
662 * The ALUA Active/NonOptimized access state delay can be disabled
663 * in via configfs with a value of zero
665 if (!cmd->alua_nonop_delay)
668 * struct se_cmd->alua_nonop_delay gets set by a target port group
669 * defined interval in core_alua_state_nonoptimized()
671 msleep_interruptible(cmd->alua_nonop_delay);
674 EXPORT_SYMBOL(core_alua_check_nonop_delay);
677 * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
680 static int core_alua_write_tpg_metadata(
682 unsigned char *md_buf,
688 int flags = O_RDWR | O_CREAT | O_TRUNC, ret;
690 memset(iov, 0, sizeof(struct iovec));
692 file = filp_open(path, flags, 0600);
693 if (IS_ERR(file) || !file || !file->f_dentry) {
694 pr_err("filp_open(%s) for ALUA metadata failed\n",
699 iov[0].iov_base = &md_buf[0];
700 iov[0].iov_len = md_buf_len;
704 ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
708 pr_err("Error writing ALUA metadata file: %s\n", path);
709 filp_close(file, NULL);
712 filp_close(file, NULL);
718 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
720 static int core_alua_update_tpg_primary_metadata(
721 struct t10_alua_tg_pt_gp *tg_pt_gp,
723 unsigned char *md_buf)
725 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
726 struct t10_wwn *wwn = &su_dev->t10_wwn;
727 char path[ALUA_METADATA_PATH_LEN];
730 memset(path, 0, ALUA_METADATA_PATH_LEN);
732 len = snprintf(md_buf, tg_pt_gp->tg_pt_gp_md_buf_len,
734 "alua_access_state=0x%02x\n"
735 "alua_access_status=0x%02x\n",
736 tg_pt_gp->tg_pt_gp_id, primary_state,
737 tg_pt_gp->tg_pt_gp_alua_access_status);
739 snprintf(path, ALUA_METADATA_PATH_LEN,
740 "/var/target/alua/tpgs_%s/%s", &wwn->unit_serial[0],
741 config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
743 return core_alua_write_tpg_metadata(path, md_buf, len);
746 static int core_alua_do_transition_tg_pt(
747 struct t10_alua_tg_pt_gp *tg_pt_gp,
748 struct se_port *l_port,
749 struct se_node_acl *nacl,
750 unsigned char *md_buf,
754 struct se_dev_entry *se_deve;
755 struct se_lun_acl *lacl;
756 struct se_port *port;
757 struct t10_alua_tg_pt_gp_member *mem;
760 * Save the old primary ALUA access state, and set the current state
761 * to ALUA_ACCESS_STATE_TRANSITION.
763 old_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
764 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
765 ALUA_ACCESS_STATE_TRANSITION);
766 tg_pt_gp->tg_pt_gp_alua_access_status = (explict) ?
767 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
768 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
770 * Check for the optional ALUA primary state transition delay
772 if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
773 msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
775 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
776 list_for_each_entry(mem, &tg_pt_gp->tg_pt_gp_mem_list,
780 * After an implicit target port asymmetric access state
781 * change, a device server shall establish a unit attention
782 * condition for the initiator port associated with every I_T
783 * nexus with the additional sense code set to ASYMMETRIC
784 * ACCESS STATE CHAGED.
786 * After an explicit target port asymmetric access state
787 * change, a device server shall establish a unit attention
788 * condition with the additional sense code set to ASYMMETRIC
789 * ACCESS STATE CHANGED for the initiator port associated with
790 * every I_T nexus other than the I_T nexus on which the SET
791 * TARGET PORT GROUPS command
793 atomic_inc(&mem->tg_pt_gp_mem_ref_cnt);
794 smp_mb__after_atomic_inc();
795 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
797 spin_lock_bh(&port->sep_alua_lock);
798 list_for_each_entry(se_deve, &port->sep_alua_list,
800 lacl = se_deve->se_lun_acl;
802 * se_deve->se_lun_acl pointer may be NULL for a
803 * entry created without explict Node+MappedLUN ACLs
809 (nacl != NULL) && (nacl == lacl->se_lun_nacl) &&
810 (l_port != NULL) && (l_port == port))
813 core_scsi3_ua_allocate(lacl->se_lun_nacl,
814 se_deve->mapped_lun, 0x2A,
815 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
817 spin_unlock_bh(&port->sep_alua_lock);
819 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
820 atomic_dec(&mem->tg_pt_gp_mem_ref_cnt);
821 smp_mb__after_atomic_dec();
823 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
825 * Update the ALUA metadata buf that has been allocated in
826 * core_alua_do_port_transition(), this metadata will be written
829 * Note that there is the case where we do not want to update the
830 * metadata when the saved metadata is being parsed in userspace
831 * when setting the existing port access state and access status.
833 * Also note that the failure to write out the ALUA metadata to
834 * struct file does NOT affect the actual ALUA transition.
836 if (tg_pt_gp->tg_pt_gp_write_metadata) {
837 mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
838 core_alua_update_tpg_primary_metadata(tg_pt_gp,
840 mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
843 * Set the current primary ALUA access state to the requested new state
845 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, new_state);
847 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
848 " from primary access state %s to %s\n", (explict) ? "explict" :
849 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
850 tg_pt_gp->tg_pt_gp_id, core_alua_dump_state(old_state),
851 core_alua_dump_state(new_state));
856 int core_alua_do_port_transition(
857 struct t10_alua_tg_pt_gp *l_tg_pt_gp,
858 struct se_device *l_dev,
859 struct se_port *l_port,
860 struct se_node_acl *l_nacl,
864 struct se_device *dev;
865 struct se_port *port;
866 struct se_subsystem_dev *su_dev;
867 struct se_node_acl *nacl;
868 struct t10_alua_lu_gp *lu_gp;
869 struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
870 struct t10_alua_tg_pt_gp *tg_pt_gp;
871 unsigned char *md_buf;
874 if (core_alua_check_transition(new_state, &primary) != 0)
877 md_buf = kzalloc(l_tg_pt_gp->tg_pt_gp_md_buf_len, GFP_KERNEL);
879 pr_err("Unable to allocate buf for ALUA metadata\n");
883 local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
884 spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
885 lu_gp = local_lu_gp_mem->lu_gp;
886 atomic_inc(&lu_gp->lu_gp_ref_cnt);
887 smp_mb__after_atomic_inc();
888 spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
890 * For storage objects that are members of the 'default_lu_gp',
891 * we only do transition on the passed *l_tp_pt_gp, and not
892 * on all of the matching target port groups IDs in default_lu_gp.
894 if (!lu_gp->lu_gp_id) {
896 * core_alua_do_transition_tg_pt() will always return
899 core_alua_do_transition_tg_pt(l_tg_pt_gp, l_port, l_nacl,
900 md_buf, new_state, explict);
901 atomic_dec(&lu_gp->lu_gp_ref_cnt);
902 smp_mb__after_atomic_dec();
907 * For all other LU groups aside from 'default_lu_gp', walk all of
908 * the associated storage objects looking for a matching target port
909 * group ID from the local target port group.
911 spin_lock(&lu_gp->lu_gp_lock);
912 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
915 dev = lu_gp_mem->lu_gp_mem_dev;
916 su_dev = dev->se_sub_dev;
917 atomic_inc(&lu_gp_mem->lu_gp_mem_ref_cnt);
918 smp_mb__after_atomic_inc();
919 spin_unlock(&lu_gp->lu_gp_lock);
921 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
922 list_for_each_entry(tg_pt_gp,
923 &su_dev->t10_alua.tg_pt_gps_list,
926 if (!tg_pt_gp->tg_pt_gp_valid_id)
929 * If the target behavior port asymmetric access state
930 * is changed for any target port group accessiable via
931 * a logical unit within a LU group, the target port
932 * behavior group asymmetric access states for the same
933 * target port group accessible via other logical units
934 * in that LU group will also change.
936 if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
939 if (l_tg_pt_gp == tg_pt_gp) {
946 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
947 smp_mb__after_atomic_inc();
948 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
950 * core_alua_do_transition_tg_pt() will always return
953 core_alua_do_transition_tg_pt(tg_pt_gp, port,
954 nacl, md_buf, new_state, explict);
956 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
957 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
958 smp_mb__after_atomic_dec();
960 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
962 spin_lock(&lu_gp->lu_gp_lock);
963 atomic_dec(&lu_gp_mem->lu_gp_mem_ref_cnt);
964 smp_mb__after_atomic_dec();
966 spin_unlock(&lu_gp->lu_gp_lock);
968 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
969 " Group IDs: %hu %s transition to primary state: %s\n",
970 config_item_name(&lu_gp->lu_gp_group.cg_item),
971 l_tg_pt_gp->tg_pt_gp_id, (explict) ? "explict" : "implict",
972 core_alua_dump_state(new_state));
974 atomic_dec(&lu_gp->lu_gp_ref_cnt);
975 smp_mb__after_atomic_dec();
981 * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
983 static int core_alua_update_tpg_secondary_metadata(
984 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
985 struct se_port *port,
986 unsigned char *md_buf,
989 struct se_portal_group *se_tpg = port->sep_tpg;
990 char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
993 memset(path, 0, ALUA_METADATA_PATH_LEN);
994 memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
996 len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
997 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
999 if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
1000 snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
1001 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
1003 len = snprintf(md_buf, md_buf_len, "alua_tg_pt_offline=%d\n"
1004 "alua_tg_pt_status=0x%02x\n",
1005 atomic_read(&port->sep_tg_pt_secondary_offline),
1006 port->sep_tg_pt_secondary_stat);
1008 snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u",
1009 se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
1010 port->sep_lun->unpacked_lun);
1012 return core_alua_write_tpg_metadata(path, md_buf, len);
1015 static int core_alua_set_tg_pt_secondary_state(
1016 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1017 struct se_port *port,
1021 struct t10_alua_tg_pt_gp *tg_pt_gp;
1022 unsigned char *md_buf;
1024 int trans_delay_msecs;
1026 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1027 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1029 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1030 pr_err("Unable to complete secondary state"
1034 trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
1036 * Set the secondary ALUA target port access state to OFFLINE
1037 * or release the previously secondary state for struct se_port
1040 atomic_set(&port->sep_tg_pt_secondary_offline, 1);
1042 atomic_set(&port->sep_tg_pt_secondary_offline, 0);
1044 md_buf_len = tg_pt_gp->tg_pt_gp_md_buf_len;
1045 port->sep_tg_pt_secondary_stat = (explict) ?
1046 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
1047 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
1049 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1050 " to secondary access state: %s\n", (explict) ? "explict" :
1051 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1052 tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
1054 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1056 * Do the optional transition delay after we set the secondary
1057 * ALUA access state.
1059 if (trans_delay_msecs != 0)
1060 msleep_interruptible(trans_delay_msecs);
1062 * See if we need to update the ALUA fabric port metadata for
1063 * secondary state and status
1065 if (port->sep_tg_pt_secondary_write_md) {
1066 md_buf = kzalloc(md_buf_len, GFP_KERNEL);
1068 pr_err("Unable to allocate md_buf for"
1069 " secondary ALUA access metadata\n");
1072 mutex_lock(&port->sep_tg_pt_md_mutex);
1073 core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem, port,
1074 md_buf, md_buf_len);
1075 mutex_unlock(&port->sep_tg_pt_md_mutex);
1083 struct t10_alua_lu_gp *
1084 core_alua_allocate_lu_gp(const char *name, int def_group)
1086 struct t10_alua_lu_gp *lu_gp;
1088 lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
1090 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1091 return ERR_PTR(-ENOMEM);
1093 INIT_LIST_HEAD(&lu_gp->lu_gp_node);
1094 INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
1095 spin_lock_init(&lu_gp->lu_gp_lock);
1096 atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
1099 lu_gp->lu_gp_id = alua_lu_gps_counter++;
1100 lu_gp->lu_gp_valid_id = 1;
1101 alua_lu_gps_count++;
1107 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
1109 struct t10_alua_lu_gp *lu_gp_tmp;
1112 * The lu_gp->lu_gp_id may only be set once..
1114 if (lu_gp->lu_gp_valid_id) {
1115 pr_warn("ALUA LU Group already has a valid ID,"
1116 " ignoring request\n");
1120 spin_lock(&lu_gps_lock);
1121 if (alua_lu_gps_count == 0x0000ffff) {
1122 pr_err("Maximum ALUA alua_lu_gps_count:"
1123 " 0x0000ffff reached\n");
1124 spin_unlock(&lu_gps_lock);
1125 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1129 lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
1130 alua_lu_gps_counter++;
1132 list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
1133 if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
1137 pr_warn("ALUA Logical Unit Group ID: %hu"
1138 " already exists, ignoring request\n",
1140 spin_unlock(&lu_gps_lock);
1145 lu_gp->lu_gp_id = lu_gp_id_tmp;
1146 lu_gp->lu_gp_valid_id = 1;
1147 list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
1148 alua_lu_gps_count++;
1149 spin_unlock(&lu_gps_lock);
1154 static struct t10_alua_lu_gp_member *
1155 core_alua_allocate_lu_gp_mem(struct se_device *dev)
1157 struct t10_alua_lu_gp_member *lu_gp_mem;
1159 lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
1161 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1162 return ERR_PTR(-ENOMEM);
1164 INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
1165 spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
1166 atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
1168 lu_gp_mem->lu_gp_mem_dev = dev;
1169 dev->dev_alua_lu_gp_mem = lu_gp_mem;
1174 void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
1176 struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
1178 * Once we have reached this point, config_item_put() has
1179 * already been called from target_core_alua_drop_lu_gp().
1181 * Here, we remove the *lu_gp from the global list so that
1182 * no associations can be made while we are releasing
1183 * struct t10_alua_lu_gp.
1185 spin_lock(&lu_gps_lock);
1186 atomic_set(&lu_gp->lu_gp_shutdown, 1);
1187 list_del(&lu_gp->lu_gp_node);
1188 alua_lu_gps_count--;
1189 spin_unlock(&lu_gps_lock);
1191 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1192 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1193 * released with core_alua_put_lu_gp_from_name()
1195 while (atomic_read(&lu_gp->lu_gp_ref_cnt))
1198 * Release reference to struct t10_alua_lu_gp * from all associated
1201 spin_lock(&lu_gp->lu_gp_lock);
1202 list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
1203 &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1204 if (lu_gp_mem->lu_gp_assoc) {
1205 list_del(&lu_gp_mem->lu_gp_mem_list);
1206 lu_gp->lu_gp_members--;
1207 lu_gp_mem->lu_gp_assoc = 0;
1209 spin_unlock(&lu_gp->lu_gp_lock);
1212 * lu_gp_mem is associated with a single
1213 * struct se_device->dev_alua_lu_gp_mem, and is released when
1214 * struct se_device is released via core_alua_free_lu_gp_mem().
1216 * If the passed lu_gp does NOT match the default_lu_gp, assume
1217 * we want to re-assocate a given lu_gp_mem with default_lu_gp.
1219 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1220 if (lu_gp != default_lu_gp)
1221 __core_alua_attach_lu_gp_mem(lu_gp_mem,
1224 lu_gp_mem->lu_gp = NULL;
1225 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1227 spin_lock(&lu_gp->lu_gp_lock);
1229 spin_unlock(&lu_gp->lu_gp_lock);
1231 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1234 void core_alua_free_lu_gp_mem(struct se_device *dev)
1236 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1237 struct t10_alua *alua = &su_dev->t10_alua;
1238 struct t10_alua_lu_gp *lu_gp;
1239 struct t10_alua_lu_gp_member *lu_gp_mem;
1241 if (alua->alua_type != SPC3_ALUA_EMULATED)
1244 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1248 while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
1251 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1252 lu_gp = lu_gp_mem->lu_gp;
1254 spin_lock(&lu_gp->lu_gp_lock);
1255 if (lu_gp_mem->lu_gp_assoc) {
1256 list_del(&lu_gp_mem->lu_gp_mem_list);
1257 lu_gp->lu_gp_members--;
1258 lu_gp_mem->lu_gp_assoc = 0;
1260 spin_unlock(&lu_gp->lu_gp_lock);
1261 lu_gp_mem->lu_gp = NULL;
1263 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1265 kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
1268 struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
1270 struct t10_alua_lu_gp *lu_gp;
1271 struct config_item *ci;
1273 spin_lock(&lu_gps_lock);
1274 list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
1275 if (!lu_gp->lu_gp_valid_id)
1277 ci = &lu_gp->lu_gp_group.cg_item;
1278 if (!strcmp(config_item_name(ci), name)) {
1279 atomic_inc(&lu_gp->lu_gp_ref_cnt);
1280 spin_unlock(&lu_gps_lock);
1284 spin_unlock(&lu_gps_lock);
1289 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
1291 spin_lock(&lu_gps_lock);
1292 atomic_dec(&lu_gp->lu_gp_ref_cnt);
1293 spin_unlock(&lu_gps_lock);
1297 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1299 void __core_alua_attach_lu_gp_mem(
1300 struct t10_alua_lu_gp_member *lu_gp_mem,
1301 struct t10_alua_lu_gp *lu_gp)
1303 spin_lock(&lu_gp->lu_gp_lock);
1304 lu_gp_mem->lu_gp = lu_gp;
1305 lu_gp_mem->lu_gp_assoc = 1;
1306 list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
1307 lu_gp->lu_gp_members++;
1308 spin_unlock(&lu_gp->lu_gp_lock);
1312 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1314 void __core_alua_drop_lu_gp_mem(
1315 struct t10_alua_lu_gp_member *lu_gp_mem,
1316 struct t10_alua_lu_gp *lu_gp)
1318 spin_lock(&lu_gp->lu_gp_lock);
1319 list_del(&lu_gp_mem->lu_gp_mem_list);
1320 lu_gp_mem->lu_gp = NULL;
1321 lu_gp_mem->lu_gp_assoc = 0;
1322 lu_gp->lu_gp_members--;
1323 spin_unlock(&lu_gp->lu_gp_lock);
1326 struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(
1327 struct se_subsystem_dev *su_dev,
1331 struct t10_alua_tg_pt_gp *tg_pt_gp;
1333 tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
1335 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1338 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
1339 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_mem_list);
1340 mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
1341 spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
1342 atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
1343 tg_pt_gp->tg_pt_gp_su_dev = su_dev;
1344 tg_pt_gp->tg_pt_gp_md_buf_len = ALUA_MD_BUF_LEN;
1345 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1346 ALUA_ACCESS_STATE_ACTIVE_OPTMIZED);
1348 * Enable both explict and implict ALUA support by default
1350 tg_pt_gp->tg_pt_gp_alua_access_type =
1351 TPGS_EXPLICT_ALUA | TPGS_IMPLICT_ALUA;
1353 * Set the default Active/NonOptimized Delay in milliseconds
1355 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
1356 tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
1359 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1360 tg_pt_gp->tg_pt_gp_id =
1361 su_dev->t10_alua.alua_tg_pt_gps_counter++;
1362 tg_pt_gp->tg_pt_gp_valid_id = 1;
1363 su_dev->t10_alua.alua_tg_pt_gps_count++;
1364 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1365 &su_dev->t10_alua.tg_pt_gps_list);
1366 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1372 int core_alua_set_tg_pt_gp_id(
1373 struct t10_alua_tg_pt_gp *tg_pt_gp,
1376 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
1377 struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
1378 u16 tg_pt_gp_id_tmp;
1380 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1382 if (tg_pt_gp->tg_pt_gp_valid_id) {
1383 pr_warn("ALUA TG PT Group already has a valid ID,"
1384 " ignoring request\n");
1388 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1389 if (su_dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
1390 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1391 " 0x0000ffff reached\n");
1392 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1393 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1397 tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
1398 su_dev->t10_alua.alua_tg_pt_gps_counter++;
1400 list_for_each_entry(tg_pt_gp_tmp, &su_dev->t10_alua.tg_pt_gps_list,
1402 if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
1406 pr_err("ALUA Target Port Group ID: %hu already"
1407 " exists, ignoring request\n", tg_pt_gp_id);
1408 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1413 tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
1414 tg_pt_gp->tg_pt_gp_valid_id = 1;
1415 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1416 &su_dev->t10_alua.tg_pt_gps_list);
1417 su_dev->t10_alua.alua_tg_pt_gps_count++;
1418 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1423 struct t10_alua_tg_pt_gp_member *core_alua_allocate_tg_pt_gp_mem(
1424 struct se_port *port)
1426 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1428 tg_pt_gp_mem = kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache,
1430 if (!tg_pt_gp_mem) {
1431 pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n");
1432 return ERR_PTR(-ENOMEM);
1434 INIT_LIST_HEAD(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1435 spin_lock_init(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1436 atomic_set(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt, 0);
1438 tg_pt_gp_mem->tg_pt = port;
1439 port->sep_alua_tg_pt_gp_mem = tg_pt_gp_mem;
1440 atomic_set(&port->sep_tg_pt_gp_active, 1);
1442 return tg_pt_gp_mem;
1445 void core_alua_free_tg_pt_gp(
1446 struct t10_alua_tg_pt_gp *tg_pt_gp)
1448 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
1449 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *tg_pt_gp_mem_tmp;
1451 * Once we have reached this point, config_item_put() has already
1452 * been called from target_core_alua_drop_tg_pt_gp().
1454 * Here we remove *tg_pt_gp from the global list so that
1455 * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS
1456 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1458 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1459 list_del(&tg_pt_gp->tg_pt_gp_list);
1460 su_dev->t10_alua.alua_tg_pt_gps_counter--;
1461 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1463 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1464 * core_alua_get_tg_pt_gp_by_name() in
1465 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1466 * to be released with core_alua_put_tg_pt_gp_from_name().
1468 while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
1471 * Release reference to struct t10_alua_tg_pt_gp from all associated
1474 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1475 list_for_each_entry_safe(tg_pt_gp_mem, tg_pt_gp_mem_tmp,
1476 &tg_pt_gp->tg_pt_gp_mem_list, tg_pt_gp_mem_list) {
1477 if (tg_pt_gp_mem->tg_pt_gp_assoc) {
1478 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1479 tg_pt_gp->tg_pt_gp_members--;
1480 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1482 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1484 * tg_pt_gp_mem is associated with a single
1485 * se_port->sep_alua_tg_pt_gp_mem, and is released via
1486 * core_alua_free_tg_pt_gp_mem().
1488 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1489 * assume we want to re-assocate a given tg_pt_gp_mem with
1492 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1493 if (tg_pt_gp != su_dev->t10_alua.default_tg_pt_gp) {
1494 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
1495 su_dev->t10_alua.default_tg_pt_gp);
1497 tg_pt_gp_mem->tg_pt_gp = NULL;
1498 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1500 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1502 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1504 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1507 void core_alua_free_tg_pt_gp_mem(struct se_port *port)
1509 struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
1510 struct t10_alua *alua = &su_dev->t10_alua;
1511 struct t10_alua_tg_pt_gp *tg_pt_gp;
1512 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1514 if (alua->alua_type != SPC3_ALUA_EMULATED)
1517 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1521 while (atomic_read(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt))
1524 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1525 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1527 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1528 if (tg_pt_gp_mem->tg_pt_gp_assoc) {
1529 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1530 tg_pt_gp->tg_pt_gp_members--;
1531 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1533 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1534 tg_pt_gp_mem->tg_pt_gp = NULL;
1536 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1538 kmem_cache_free(t10_alua_tg_pt_gp_mem_cache, tg_pt_gp_mem);
1541 static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
1542 struct se_subsystem_dev *su_dev,
1545 struct t10_alua_tg_pt_gp *tg_pt_gp;
1546 struct config_item *ci;
1548 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1549 list_for_each_entry(tg_pt_gp, &su_dev->t10_alua.tg_pt_gps_list,
1551 if (!tg_pt_gp->tg_pt_gp_valid_id)
1553 ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1554 if (!strcmp(config_item_name(ci), name)) {
1555 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
1556 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1560 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1565 static void core_alua_put_tg_pt_gp_from_name(
1566 struct t10_alua_tg_pt_gp *tg_pt_gp)
1568 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
1570 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1571 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
1572 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1576 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1578 void __core_alua_attach_tg_pt_gp_mem(
1579 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1580 struct t10_alua_tg_pt_gp *tg_pt_gp)
1582 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1583 tg_pt_gp_mem->tg_pt_gp = tg_pt_gp;
1584 tg_pt_gp_mem->tg_pt_gp_assoc = 1;
1585 list_add_tail(&tg_pt_gp_mem->tg_pt_gp_mem_list,
1586 &tg_pt_gp->tg_pt_gp_mem_list);
1587 tg_pt_gp->tg_pt_gp_members++;
1588 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1592 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1594 static void __core_alua_drop_tg_pt_gp_mem(
1595 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1596 struct t10_alua_tg_pt_gp *tg_pt_gp)
1598 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1599 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1600 tg_pt_gp_mem->tg_pt_gp = NULL;
1601 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1602 tg_pt_gp->tg_pt_gp_members--;
1603 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1606 ssize_t core_alua_show_tg_pt_gp_info(struct se_port *port, char *page)
1608 struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
1609 struct config_item *tg_pt_ci;
1610 struct t10_alua *alua = &su_dev->t10_alua;
1611 struct t10_alua_tg_pt_gp *tg_pt_gp;
1612 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1615 if (alua->alua_type != SPC3_ALUA_EMULATED)
1618 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1622 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1623 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1625 tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1626 len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
1627 " %hu\nTG Port Primary Access State: %s\nTG Port "
1628 "Primary Access Status: %s\nTG Port Secondary Access"
1629 " State: %s\nTG Port Secondary Access Status: %s\n",
1630 config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
1631 core_alua_dump_state(atomic_read(
1632 &tg_pt_gp->tg_pt_gp_alua_access_state)),
1633 core_alua_dump_status(
1634 tg_pt_gp->tg_pt_gp_alua_access_status),
1635 (atomic_read(&port->sep_tg_pt_secondary_offline)) ?
1637 core_alua_dump_status(port->sep_tg_pt_secondary_stat));
1639 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1644 ssize_t core_alua_store_tg_pt_gp_info(
1645 struct se_port *port,
1649 struct se_portal_group *tpg;
1651 struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
1652 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
1653 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1654 unsigned char buf[TG_PT_GROUP_NAME_BUF];
1657 tpg = port->sep_tpg;
1658 lun = port->sep_lun;
1660 if (su_dev->t10_alua.alua_type != SPC3_ALUA_EMULATED) {
1661 pr_warn("SPC3_ALUA_EMULATED not enabled for"
1662 " %s/tpgt_%hu/%s\n", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1663 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1664 config_item_name(&lun->lun_group.cg_item));
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(su_dev,
1689 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1690 if (!tg_pt_gp_mem) {
1692 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1693 pr_err("NULL struct se_port->sep_alua_tg_pt_gp_mem pointer\n");
1697 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1698 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1701 * Clearing an existing tg_pt_gp association, and replacing
1702 * with the default_tg_pt_gp.
1704 if (!tg_pt_gp_new) {
1705 pr_debug("Target_Core_ConfigFS: Moving"
1706 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1707 " alua/%s, ID: %hu back to"
1708 " default_tg_pt_gp\n",
1709 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1710 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1711 config_item_name(&lun->lun_group.cg_item),
1713 &tg_pt_gp->tg_pt_gp_group.cg_item),
1714 tg_pt_gp->tg_pt_gp_id);
1716 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1717 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
1718 su_dev->t10_alua.default_tg_pt_gp);
1719 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1724 * Removing existing association of tg_pt_gp_mem with tg_pt_gp
1726 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1730 * Associate tg_pt_gp_mem with tg_pt_gp_new.
1732 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp_new);
1733 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1734 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
1735 " Target Port Group: alua/%s, ID: %hu\n", (move) ?
1736 "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1737 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1738 config_item_name(&lun->lun_group.cg_item),
1739 config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
1740 tg_pt_gp_new->tg_pt_gp_id);
1742 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1746 ssize_t core_alua_show_access_type(
1747 struct t10_alua_tg_pt_gp *tg_pt_gp,
1750 if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA) &&
1751 (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA))
1752 return sprintf(page, "Implict and Explict\n");
1753 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)
1754 return sprintf(page, "Implict\n");
1755 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA)
1756 return sprintf(page, "Explict\n");
1758 return sprintf(page, "None\n");
1761 ssize_t core_alua_store_access_type(
1762 struct t10_alua_tg_pt_gp *tg_pt_gp,
1769 ret = strict_strtoul(page, 0, &tmp);
1771 pr_err("Unable to extract alua_access_type\n");
1774 if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
1775 pr_err("Illegal value for alua_access_type:"
1780 tg_pt_gp->tg_pt_gp_alua_access_type =
1781 TPGS_IMPLICT_ALUA | TPGS_EXPLICT_ALUA;
1783 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICT_ALUA;
1785 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICT_ALUA;
1787 tg_pt_gp->tg_pt_gp_alua_access_type = 0;
1792 ssize_t core_alua_show_nonop_delay_msecs(
1793 struct t10_alua_tg_pt_gp *tg_pt_gp,
1796 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
1799 ssize_t core_alua_store_nonop_delay_msecs(
1800 struct t10_alua_tg_pt_gp *tg_pt_gp,
1807 ret = strict_strtoul(page, 0, &tmp);
1809 pr_err("Unable to extract nonop_delay_msecs\n");
1812 if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
1813 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
1814 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
1815 ALUA_MAX_NONOP_DELAY_MSECS);
1818 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
1823 ssize_t core_alua_show_trans_delay_msecs(
1824 struct t10_alua_tg_pt_gp *tg_pt_gp,
1827 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
1830 ssize_t core_alua_store_trans_delay_msecs(
1831 struct t10_alua_tg_pt_gp *tg_pt_gp,
1838 ret = strict_strtoul(page, 0, &tmp);
1840 pr_err("Unable to extract trans_delay_msecs\n");
1843 if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
1844 pr_err("Passed trans_delay_msecs: %lu, exceeds"
1845 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
1846 ALUA_MAX_TRANS_DELAY_MSECS);
1849 tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
1854 ssize_t core_alua_show_preferred_bit(
1855 struct t10_alua_tg_pt_gp *tg_pt_gp,
1858 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
1861 ssize_t core_alua_store_preferred_bit(
1862 struct t10_alua_tg_pt_gp *tg_pt_gp,
1869 ret = strict_strtoul(page, 0, &tmp);
1871 pr_err("Unable to extract preferred ALUA value\n");
1874 if ((tmp != 0) && (tmp != 1)) {
1875 pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
1878 tg_pt_gp->tg_pt_gp_pref = (int)tmp;
1883 ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
1888 return sprintf(page, "%d\n",
1889 atomic_read(&lun->lun_sep->sep_tg_pt_secondary_offline));
1892 ssize_t core_alua_store_offline_bit(
1897 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1904 ret = strict_strtoul(page, 0, &tmp);
1906 pr_err("Unable to extract alua_tg_pt_offline value\n");
1909 if ((tmp != 0) && (tmp != 1)) {
1910 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
1914 tg_pt_gp_mem = lun->lun_sep->sep_alua_tg_pt_gp_mem;
1915 if (!tg_pt_gp_mem) {
1916 pr_err("Unable to locate *tg_pt_gp_mem\n");
1920 ret = core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem,
1921 lun->lun_sep, 0, (int)tmp);
1928 ssize_t core_alua_show_secondary_status(
1932 return sprintf(page, "%d\n", lun->lun_sep->sep_tg_pt_secondary_stat);
1935 ssize_t core_alua_store_secondary_status(
1943 ret = strict_strtoul(page, 0, &tmp);
1945 pr_err("Unable to extract alua_tg_pt_status\n");
1948 if ((tmp != ALUA_STATUS_NONE) &&
1949 (tmp != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
1950 (tmp != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
1951 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
1955 lun->lun_sep->sep_tg_pt_secondary_stat = (int)tmp;
1960 ssize_t core_alua_show_secondary_write_metadata(
1964 return sprintf(page, "%d\n",
1965 lun->lun_sep->sep_tg_pt_secondary_write_md);
1968 ssize_t core_alua_store_secondary_write_metadata(
1976 ret = strict_strtoul(page, 0, &tmp);
1978 pr_err("Unable to extract alua_tg_pt_write_md\n");
1981 if ((tmp != 0) && (tmp != 1)) {
1982 pr_err("Illegal value for alua_tg_pt_write_md:"
1986 lun->lun_sep->sep_tg_pt_secondary_write_md = (int)tmp;
1991 int core_setup_alua(struct se_device *dev, int force_pt)
1993 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1994 struct t10_alua *alua = &su_dev->t10_alua;
1995 struct t10_alua_lu_gp_member *lu_gp_mem;
1997 * If this device is from Target_Core_Mod/pSCSI, use the ALUA logic
1998 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
1999 * cause a problem because libata and some SATA RAID HBAs appear
2000 * under Linux/SCSI, but emulate SCSI logic themselves.
2002 if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
2003 !(dev->se_sub_dev->se_dev_attrib.emulate_alua)) || force_pt) {
2004 alua->alua_type = SPC_ALUA_PASSTHROUGH;
2005 alua->alua_state_check = &core_alua_state_check_nop;
2006 pr_debug("%s: Using SPC_ALUA_PASSTHROUGH, no ALUA"
2007 " emulation\n", dev->transport->name);
2011 * If SPC-3 or above is reported by real or emulated struct se_device,
2012 * use emulated ALUA.
2014 if (dev->transport->get_device_rev(dev) >= SCSI_3) {
2015 pr_debug("%s: Enabling ALUA Emulation for SPC-3"
2016 " device\n", dev->transport->name);
2018 * Associate this struct se_device with the default ALUA
2021 lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
2022 if (IS_ERR(lu_gp_mem))
2023 return PTR_ERR(lu_gp_mem);
2025 alua->alua_type = SPC3_ALUA_EMULATED;
2026 alua->alua_state_check = &core_alua_state_check;
2027 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2028 __core_alua_attach_lu_gp_mem(lu_gp_mem,
2030 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2032 pr_debug("%s: Adding to default ALUA LU Group:"
2033 " core/alua/lu_gps/default_lu_gp\n",
2034 dev->transport->name);
2036 alua->alua_type = SPC2_ALUA_DISABLED;
2037 alua->alua_state_check = &core_alua_state_check_nop;
2038 pr_debug("%s: Disabling ALUA Emulation for SPC-2"
2039 " device\n", dev->transport->name);