1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
3 * Filename: target_core_alua.c
5 * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
7 * (c) Copyright 2009-2013 Datera, Inc.
9 * Nicholas A. Bellinger <nab@kernel.org>
11 ******************************************************************************/
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
15 #include <linux/configfs.h>
16 #include <linux/delay.h>
17 #include <linux/export.h>
18 #include <linux/fcntl.h>
19 #include <linux/file.h>
21 #include <scsi/scsi_proto.h>
22 #include <asm/unaligned.h>
24 #include <target/target_core_base.h>
25 #include <target/target_core_backend.h>
26 #include <target/target_core_fabric.h>
28 #include "target_core_internal.h"
29 #include "target_core_alua.h"
30 #include "target_core_ua.h"
32 static sense_reason_t core_alua_check_transition(int state, int valid,
33 int *primary, int explicit);
34 static int core_alua_set_tg_pt_secondary_state(
35 struct se_lun *lun, int explicit, int offline);
37 static char *core_alua_dump_state(int state);
39 static void __target_attach_tg_pt_gp(struct se_lun *lun,
40 struct t10_alua_tg_pt_gp *tg_pt_gp);
42 static u16 alua_lu_gps_counter;
43 static u32 alua_lu_gps_count;
45 static DEFINE_SPINLOCK(lu_gps_lock);
46 static LIST_HEAD(lu_gps_list);
48 struct t10_alua_lu_gp *default_lu_gp;
53 * See sbc3r35 section 5.23
56 target_emulate_report_referrals(struct se_cmd *cmd)
58 struct se_device *dev = cmd->se_dev;
59 struct t10_alua_lba_map *map;
60 struct t10_alua_lba_map_member *map_mem;
64 if (cmd->data_length < 4) {
65 pr_warn("REPORT REFERRALS allocation length %u too"
66 " small\n", cmd->data_length);
67 return TCM_INVALID_CDB_FIELD;
70 buf = transport_kmap_data_sg(cmd);
72 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
75 spin_lock(&dev->t10_alua.lba_map_lock);
76 if (list_empty(&dev->t10_alua.lba_map_list)) {
77 spin_unlock(&dev->t10_alua.lba_map_lock);
78 transport_kunmap_data_sg(cmd);
80 return TCM_UNSUPPORTED_SCSI_OPCODE;
83 list_for_each_entry(map, &dev->t10_alua.lba_map_list,
85 int desc_num = off + 3;
89 if (cmd->data_length > off)
90 put_unaligned_be64(map->lba_map_first_lba, &buf[off]);
92 if (cmd->data_length > off)
93 put_unaligned_be64(map->lba_map_last_lba, &buf[off]);
97 list_for_each_entry(map_mem, &map->lba_map_mem_list,
99 int alua_state = map_mem->lba_map_mem_alua_state;
100 int alua_pg_id = map_mem->lba_map_mem_alua_pg_id;
102 if (cmd->data_length > off)
103 buf[off] = alua_state & 0x0f;
105 if (cmd->data_length > off)
106 buf[off] = (alua_pg_id >> 8) & 0xff;
108 if (cmd->data_length > off)
109 buf[off] = (alua_pg_id & 0xff);
114 if (cmd->data_length > desc_num)
115 buf[desc_num] = pg_num;
117 spin_unlock(&dev->t10_alua.lba_map_lock);
120 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
122 put_unaligned_be16(rd_len, &buf[2]);
124 transport_kunmap_data_sg(cmd);
126 target_complete_cmd(cmd, SAM_STAT_GOOD);
131 * REPORT_TARGET_PORT_GROUPS
133 * See spc4r17 section 6.27
136 target_emulate_report_target_port_groups(struct se_cmd *cmd)
138 struct se_device *dev = cmd->se_dev;
139 struct t10_alua_tg_pt_gp *tg_pt_gp;
143 int ext_hdr = (cmd->t_task_cdb[1] & 0x20);
146 * Skip over RESERVED area to first Target port group descriptor
147 * depending on the PARAMETER DATA FORMAT type..
154 if (cmd->data_length < off) {
155 pr_warn("REPORT TARGET PORT GROUPS allocation length %u too"
156 " small for %s header\n", cmd->data_length,
157 (ext_hdr) ? "extended" : "normal");
158 return TCM_INVALID_CDB_FIELD;
160 buf = transport_kmap_data_sg(cmd);
162 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
164 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
165 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
168 * Check if the Target port group and Target port descriptor list
169 * based on tg_pt_gp_members count will fit into the response payload.
170 * Otherwise, bump rd_len to let the initiator know we have exceeded
171 * the allocation length and the response is truncated.
173 if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) >
175 rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4);
179 * PREF: Preferred target port bit, determine if this
180 * bit should be set for port group.
182 if (tg_pt_gp->tg_pt_gp_pref)
185 * Set the ASYMMETRIC ACCESS State
187 buf[off++] |= tg_pt_gp->tg_pt_gp_alua_access_state & 0xff;
189 * Set supported ASYMMETRIC ACCESS State bits
191 buf[off++] |= tg_pt_gp->tg_pt_gp_alua_supported_states;
195 put_unaligned_be16(tg_pt_gp->tg_pt_gp_id, &buf[off]);
198 off++; /* Skip over Reserved */
202 buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
204 * Vendor Specific field
210 buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
213 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
214 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
217 * Start Target Port descriptor format
219 * See spc4r17 section 6.2.7 Table 247
221 off += 2; /* Skip over Obsolete */
223 * Set RELATIVE TARGET PORT IDENTIFIER
225 put_unaligned_be16(lun->lun_rtpi, &buf[off]);
229 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
231 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
233 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
235 put_unaligned_be32(rd_len, &buf[0]);
238 * Fill in the Extended header parameter data format if requested
243 * Set the implicit transition time (in seconds) for the application
244 * client to use as a base for it's transition timeout value.
246 * Use the current tg_pt_gp_mem -> tg_pt_gp membership from the LUN
247 * this CDB was received upon to determine this value individually
248 * for ALUA target port group.
250 spin_lock(&cmd->se_lun->lun_tg_pt_gp_lock);
251 tg_pt_gp = cmd->se_lun->lun_tg_pt_gp;
253 buf[5] = tg_pt_gp->tg_pt_gp_implicit_trans_secs;
254 spin_unlock(&cmd->se_lun->lun_tg_pt_gp_lock);
256 transport_kunmap_data_sg(cmd);
258 target_complete_cmd_with_length(cmd, SAM_STAT_GOOD, rd_len + 4);
263 * SET_TARGET_PORT_GROUPS for explicit ALUA operation.
265 * See spc4r17 section 6.35
268 target_emulate_set_target_port_groups(struct se_cmd *cmd)
270 struct se_device *dev = cmd->se_dev;
271 struct se_lun *l_lun = cmd->se_lun;
272 struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
273 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
276 sense_reason_t rc = TCM_NO_SENSE;
277 u32 len = 4; /* Skip over RESERVED area in header */
278 int alua_access_state, primary = 0, valid_states;
281 if (cmd->data_length < 4) {
282 pr_warn("SET TARGET PORT GROUPS parameter list length %u too"
283 " small\n", cmd->data_length);
284 return TCM_INVALID_PARAMETER_LIST;
287 buf = transport_kmap_data_sg(cmd);
289 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
292 * Determine if explicit ALUA via SET_TARGET_PORT_GROUPS is allowed
293 * for the local tg_pt_gp.
295 spin_lock(&l_lun->lun_tg_pt_gp_lock);
296 l_tg_pt_gp = l_lun->lun_tg_pt_gp;
298 spin_unlock(&l_lun->lun_tg_pt_gp_lock);
299 pr_err("Unable to access l_lun->tg_pt_gp\n");
300 rc = TCM_UNSUPPORTED_SCSI_OPCODE;
304 if (!(l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)) {
305 spin_unlock(&l_lun->lun_tg_pt_gp_lock);
306 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
307 " while TPGS_EXPLICIT_ALUA is disabled\n");
308 rc = TCM_UNSUPPORTED_SCSI_OPCODE;
311 valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states;
312 spin_unlock(&l_lun->lun_tg_pt_gp_lock);
314 ptr = &buf[4]; /* Skip over RESERVED area in header */
316 while (len < cmd->data_length) {
318 alua_access_state = (ptr[0] & 0x0f);
320 * Check the received ALUA access state, and determine if
321 * the state is a primary or secondary target port asymmetric
324 rc = core_alua_check_transition(alua_access_state, valid_states,
328 * If the SET TARGET PORT GROUPS attempts to establish
329 * an invalid combination of target port asymmetric
330 * access states or attempts to establish an
331 * unsupported target port asymmetric access state,
332 * then the command shall be terminated with CHECK
333 * CONDITION status, with the sense key set to ILLEGAL
334 * REQUEST, and the additional sense code set to INVALID
335 * FIELD IN PARAMETER LIST.
341 * If the ASYMMETRIC ACCESS STATE field (see table 267)
342 * specifies a primary target port asymmetric access state,
343 * then the TARGET PORT GROUP OR TARGET PORT field specifies
344 * a primary target port group for which the primary target
345 * port asymmetric access state shall be changed. If the
346 * ASYMMETRIC ACCESS STATE field specifies a secondary target
347 * port asymmetric access state, then the TARGET PORT GROUP OR
348 * TARGET PORT field specifies the relative target port
349 * identifier (see 3.1.120) of the target port for which the
350 * secondary target port asymmetric access state shall be
354 tg_pt_id = get_unaligned_be16(ptr + 2);
356 * Locate the matching target port group ID from
357 * the global tg_pt_gp list
359 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
360 list_for_each_entry(tg_pt_gp,
361 &dev->t10_alua.tg_pt_gps_list,
363 if (!tg_pt_gp->tg_pt_gp_valid_id)
366 if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
369 atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
371 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
373 if (!core_alua_do_port_transition(tg_pt_gp,
375 alua_access_state, 1))
378 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
379 atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
382 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
387 * Extract the RELATIVE TARGET PORT IDENTIFIER to identify
388 * the Target Port in question for the the incoming
389 * SET_TARGET_PORT_GROUPS op.
391 rtpi = get_unaligned_be16(ptr + 2);
393 * Locate the matching relative target port identifier
394 * for the struct se_device storage object.
396 spin_lock(&dev->se_port_lock);
397 list_for_each_entry(lun, &dev->dev_sep_list,
399 if (lun->lun_rtpi != rtpi)
403 spin_unlock(&dev->se_port_lock);
405 if (!core_alua_set_tg_pt_secondary_state(
409 spin_lock(&dev->se_port_lock);
412 spin_unlock(&dev->se_port_lock);
416 rc = TCM_INVALID_PARAMETER_LIST;
425 transport_kunmap_data_sg(cmd);
427 target_complete_cmd(cmd, SAM_STAT_GOOD);
431 static inline void core_alua_state_nonoptimized(
434 int nonop_delay_msecs)
437 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
438 * later to determine if processing of this cmd needs to be
439 * temporarily delayed for the Active/NonOptimized primary access state.
441 cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
442 cmd->alua_nonop_delay = nonop_delay_msecs;
445 static inline sense_reason_t core_alua_state_lba_dependent(
449 struct se_device *dev = cmd->se_dev;
450 u64 segment_size, segment_mult, sectors, lba;
452 /* Only need to check for cdb actually containing LBAs */
453 if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB))
456 spin_lock(&dev->t10_alua.lba_map_lock);
457 segment_size = dev->t10_alua.lba_map_segment_size;
458 segment_mult = dev->t10_alua.lba_map_segment_multiplier;
459 sectors = cmd->data_length / dev->dev_attrib.block_size;
461 lba = cmd->t_task_lba;
462 while (lba < cmd->t_task_lba + sectors) {
463 struct t10_alua_lba_map *cur_map = NULL, *map;
464 struct t10_alua_lba_map_member *map_mem;
466 list_for_each_entry(map, &dev->t10_alua.lba_map_list,
468 u64 start_lba, last_lba;
469 u64 first_lba = map->lba_map_first_lba;
473 start_lba = do_div(tmp, segment_size * segment_mult);
475 last_lba = first_lba + segment_size - 1;
476 if (start_lba >= first_lba &&
477 start_lba <= last_lba) {
483 last_lba = map->lba_map_last_lba;
484 if (lba >= first_lba && lba <= last_lba) {
492 spin_unlock(&dev->t10_alua.lba_map_lock);
493 return TCM_ALUA_TG_PT_UNAVAILABLE;
495 list_for_each_entry(map_mem, &cur_map->lba_map_mem_list,
497 if (map_mem->lba_map_mem_alua_pg_id != tg_pt_gp_id)
499 switch(map_mem->lba_map_mem_alua_state) {
500 case ALUA_ACCESS_STATE_STANDBY:
501 spin_unlock(&dev->t10_alua.lba_map_lock);
502 return TCM_ALUA_TG_PT_STANDBY;
503 case ALUA_ACCESS_STATE_UNAVAILABLE:
504 spin_unlock(&dev->t10_alua.lba_map_lock);
505 return TCM_ALUA_TG_PT_UNAVAILABLE;
511 spin_unlock(&dev->t10_alua.lba_map_lock);
515 static inline sense_reason_t core_alua_state_standby(
520 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
521 * spc4r17 section 5.9.2.4.4
530 case RECEIVE_DIAGNOSTIC:
531 case SEND_DIAGNOSTIC:
534 case SERVICE_ACTION_IN_16:
535 switch (cdb[1] & 0x1f) {
536 case SAI_READ_CAPACITY_16:
539 return TCM_ALUA_TG_PT_STANDBY;
542 switch (cdb[1] & 0x1f) {
543 case MI_REPORT_TARGET_PGS:
546 return TCM_ALUA_TG_PT_STANDBY;
548 case MAINTENANCE_OUT:
550 case MO_SET_TARGET_PGS:
553 return TCM_ALUA_TG_PT_STANDBY;
556 case PERSISTENT_RESERVE_IN:
557 case PERSISTENT_RESERVE_OUT:
562 return TCM_ALUA_TG_PT_STANDBY;
568 static inline sense_reason_t core_alua_state_unavailable(
573 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
574 * spc4r17 section 5.9.2.4.5
581 switch (cdb[1] & 0x1f) {
582 case MI_REPORT_TARGET_PGS:
585 return TCM_ALUA_TG_PT_UNAVAILABLE;
587 case MAINTENANCE_OUT:
589 case MO_SET_TARGET_PGS:
592 return TCM_ALUA_TG_PT_UNAVAILABLE;
599 return TCM_ALUA_TG_PT_UNAVAILABLE;
605 static inline sense_reason_t core_alua_state_transition(
610 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITION as defined by
611 * spc4r17 section 5.9.2.5
618 switch (cdb[1] & 0x1f) {
619 case MI_REPORT_TARGET_PGS:
622 return TCM_ALUA_STATE_TRANSITION;
629 return TCM_ALUA_STATE_TRANSITION;
636 * return 1: Is used to signal LUN not accessible, and check condition/not ready
637 * return 0: Used to signal success
638 * return -1: Used to signal failure, and invalid cdb field
641 target_alua_state_check(struct se_cmd *cmd)
643 struct se_device *dev = cmd->se_dev;
644 unsigned char *cdb = cmd->t_task_cdb;
645 struct se_lun *lun = cmd->se_lun;
646 struct t10_alua_tg_pt_gp *tg_pt_gp;
647 int out_alua_state, nonop_delay_msecs;
649 sense_reason_t rc = TCM_NO_SENSE;
651 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
653 if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA)
657 * First, check for a struct se_port specific secondary ALUA target port
658 * access state: OFFLINE
660 if (atomic_read(&lun->lun_tg_pt_secondary_offline)) {
661 pr_debug("ALUA: Got secondary offline status for local"
663 return TCM_ALUA_OFFLINE;
666 if (!lun->lun_tg_pt_gp)
669 spin_lock(&lun->lun_tg_pt_gp_lock);
670 tg_pt_gp = lun->lun_tg_pt_gp;
671 out_alua_state = tg_pt_gp->tg_pt_gp_alua_access_state;
672 nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
673 tg_pt_gp_id = tg_pt_gp->tg_pt_gp_id;
675 spin_unlock(&lun->lun_tg_pt_gp_lock);
677 * Process ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED in a separate conditional
678 * statement so the compiler knows explicitly to check this case first.
679 * For the Optimized ALUA access state case, we want to process the
680 * incoming fabric cmd ASAP..
682 if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED)
685 switch (out_alua_state) {
686 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
687 core_alua_state_nonoptimized(cmd, cdb, nonop_delay_msecs);
689 case ALUA_ACCESS_STATE_STANDBY:
690 rc = core_alua_state_standby(cmd, cdb);
692 case ALUA_ACCESS_STATE_UNAVAILABLE:
693 rc = core_alua_state_unavailable(cmd, cdb);
695 case ALUA_ACCESS_STATE_TRANSITION:
696 rc = core_alua_state_transition(cmd, cdb);
698 case ALUA_ACCESS_STATE_LBA_DEPENDENT:
699 rc = core_alua_state_lba_dependent(cmd, tg_pt_gp_id);
702 * OFFLINE is a secondary ALUA target port group access state, that is
703 * handled above with struct se_lun->lun_tg_pt_secondary_offline=1
705 case ALUA_ACCESS_STATE_OFFLINE:
707 pr_err("Unknown ALUA access state: 0x%02x\n",
709 rc = TCM_INVALID_CDB_FIELD;
712 if (rc && rc != TCM_INVALID_CDB_FIELD) {
713 pr_debug("[%s]: ALUA TG Port not available, "
714 "SenseKey: NOT_READY, ASC/rc: 0x04/%d\n",
715 cmd->se_tfo->fabric_name, rc);
722 * Check implicit and explicit ALUA state change request.
724 static sense_reason_t
725 core_alua_check_transition(int state, int valid, int *primary, int explicit)
728 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
729 * defined as primary target port asymmetric access states.
732 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
733 if (!(valid & ALUA_AO_SUP))
737 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
738 if (!(valid & ALUA_AN_SUP))
742 case ALUA_ACCESS_STATE_STANDBY:
743 if (!(valid & ALUA_S_SUP))
747 case ALUA_ACCESS_STATE_UNAVAILABLE:
748 if (!(valid & ALUA_U_SUP))
752 case ALUA_ACCESS_STATE_LBA_DEPENDENT:
753 if (!(valid & ALUA_LBD_SUP))
757 case ALUA_ACCESS_STATE_OFFLINE:
759 * OFFLINE state is defined as a secondary target port
760 * asymmetric access state.
762 if (!(valid & ALUA_O_SUP))
766 case ALUA_ACCESS_STATE_TRANSITION:
767 if (!(valid & ALUA_T_SUP) || explicit)
769 * Transitioning is set internally and by tcmu daemon,
770 * and cannot be selected through a STPG.
776 pr_err("Unknown ALUA access state: 0x%02x\n", state);
777 return TCM_INVALID_PARAMETER_LIST;
783 pr_err("ALUA access state %s not supported",
784 core_alua_dump_state(state));
785 return TCM_INVALID_PARAMETER_LIST;
788 static char *core_alua_dump_state(int state)
791 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
792 return "Active/Optimized";
793 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
794 return "Active/NonOptimized";
795 case ALUA_ACCESS_STATE_LBA_DEPENDENT:
796 return "LBA Dependent";
797 case ALUA_ACCESS_STATE_STANDBY:
799 case ALUA_ACCESS_STATE_UNAVAILABLE:
800 return "Unavailable";
801 case ALUA_ACCESS_STATE_OFFLINE:
803 case ALUA_ACCESS_STATE_TRANSITION:
804 return "Transitioning";
812 char *core_alua_dump_status(int status)
815 case ALUA_STATUS_NONE:
817 case ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG:
818 return "Altered by Explicit STPG";
819 case ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA:
820 return "Altered by Implicit ALUA";
829 * Used by fabric modules to determine when we need to delay processing
830 * for the Active/NonOptimized paths..
832 int core_alua_check_nonop_delay(
835 if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
838 * The ALUA Active/NonOptimized access state delay can be disabled
839 * in via configfs with a value of zero
841 if (!cmd->alua_nonop_delay)
844 * struct se_cmd->alua_nonop_delay gets set by a target port group
845 * defined interval in core_alua_state_nonoptimized()
847 msleep_interruptible(cmd->alua_nonop_delay);
850 EXPORT_SYMBOL(core_alua_check_nonop_delay);
852 static int core_alua_write_tpg_metadata(
854 unsigned char *md_buf,
857 struct file *file = filp_open(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
862 pr_err("filp_open(%s) for ALUA metadata failed\n", path);
865 ret = kernel_write(file, md_buf, md_buf_len, &pos);
867 pr_err("Error writing ALUA metadata file: %s\n", path);
869 return (ret < 0) ? -EIO : 0;
872 static int core_alua_update_tpg_primary_metadata(
873 struct t10_alua_tg_pt_gp *tg_pt_gp)
875 unsigned char *md_buf;
876 struct t10_wwn *wwn = &tg_pt_gp->tg_pt_gp_dev->t10_wwn;
880 lockdep_assert_held(&tg_pt_gp->tg_pt_gp_transition_mutex);
882 md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL);
884 pr_err("Unable to allocate buf for ALUA metadata\n");
888 len = snprintf(md_buf, ALUA_MD_BUF_LEN,
890 "alua_access_state=0x%02x\n"
891 "alua_access_status=0x%02x\n",
892 tg_pt_gp->tg_pt_gp_id,
893 tg_pt_gp->tg_pt_gp_alua_access_state,
894 tg_pt_gp->tg_pt_gp_alua_access_status);
897 path = kasprintf(GFP_KERNEL, "%s/alua/tpgs_%s/%s", db_root,
898 &wwn->unit_serial[0],
899 config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
901 rc = core_alua_write_tpg_metadata(path, md_buf, len);
908 static void core_alua_queue_state_change_ua(struct t10_alua_tg_pt_gp *tg_pt_gp)
910 struct se_dev_entry *se_deve;
912 struct se_lun_acl *lacl;
914 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
915 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
918 * After an implicit target port asymmetric access state
919 * change, a device server shall establish a unit attention
920 * condition for the initiator port associated with every I_T
921 * nexus with the additional sense code set to ASYMMETRIC
922 * ACCESS STATE CHANGED.
924 * After an explicit target port asymmetric access state
925 * change, a device server shall establish a unit attention
926 * condition with the additional sense code set to ASYMMETRIC
927 * ACCESS STATE CHANGED for the initiator port associated with
928 * every I_T nexus other than the I_T nexus on which the SET
929 * TARGET PORT GROUPS command
931 if (!percpu_ref_tryget_live(&lun->lun_ref))
933 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
935 spin_lock(&lun->lun_deve_lock);
936 list_for_each_entry(se_deve, &lun->lun_deve_list, lun_link) {
937 lacl = rcu_dereference_check(se_deve->se_lun_acl,
938 lockdep_is_held(&lun->lun_deve_lock));
942 * After an explicit target port asymmetric access
943 * state change, a device server shall establish a
944 * unit attention condition with the additional sense
945 * code set to ASYMMETRIC ACCESS STATE CHANGED for
946 * the initiator port associated with every I_T nexus
947 * other than the I_T nexus on which the SET TARGET
948 * PORT GROUPS command was received.
950 if ((tg_pt_gp->tg_pt_gp_alua_access_status ==
951 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
952 (tg_pt_gp->tg_pt_gp_alua_lun != NULL) &&
953 (tg_pt_gp->tg_pt_gp_alua_lun == lun))
957 * se_deve->se_lun_acl pointer may be NULL for a
958 * entry created without explicit Node+MappedLUN ACLs
960 if (lacl && (tg_pt_gp->tg_pt_gp_alua_nacl != NULL) &&
961 (tg_pt_gp->tg_pt_gp_alua_nacl == lacl->se_lun_nacl))
964 core_scsi3_ua_allocate(se_deve, 0x2A,
965 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
967 spin_unlock(&lun->lun_deve_lock);
969 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
970 percpu_ref_put(&lun->lun_ref);
972 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
975 static int core_alua_do_transition_tg_pt(
976 struct t10_alua_tg_pt_gp *tg_pt_gp,
982 mutex_lock(&tg_pt_gp->tg_pt_gp_transition_mutex);
983 /* Nothing to be done here */
984 if (tg_pt_gp->tg_pt_gp_alua_access_state == new_state) {
985 mutex_unlock(&tg_pt_gp->tg_pt_gp_transition_mutex);
989 if (explicit && new_state == ALUA_ACCESS_STATE_TRANSITION) {
990 mutex_unlock(&tg_pt_gp->tg_pt_gp_transition_mutex);
995 * Save the old primary ALUA access state, and set the current state
996 * to ALUA_ACCESS_STATE_TRANSITION.
998 prev_state = tg_pt_gp->tg_pt_gp_alua_access_state;
999 tg_pt_gp->tg_pt_gp_alua_access_state = ALUA_ACCESS_STATE_TRANSITION;
1000 tg_pt_gp->tg_pt_gp_alua_access_status = (explicit) ?
1001 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
1002 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;
1004 core_alua_queue_state_change_ua(tg_pt_gp);
1006 if (new_state == ALUA_ACCESS_STATE_TRANSITION) {
1007 mutex_unlock(&tg_pt_gp->tg_pt_gp_transition_mutex);
1012 * Check for the optional ALUA primary state transition delay
1014 if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
1015 msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
1018 * Set the current primary ALUA access state to the requested new state
1020 tg_pt_gp->tg_pt_gp_alua_access_state = new_state;
1023 * Update the ALUA metadata buf that has been allocated in
1024 * core_alua_do_port_transition(), this metadata will be written
1027 * Note that there is the case where we do not want to update the
1028 * metadata when the saved metadata is being parsed in userspace
1029 * when setting the existing port access state and access status.
1031 * Also note that the failure to write out the ALUA metadata to
1032 * struct file does NOT affect the actual ALUA transition.
1034 if (tg_pt_gp->tg_pt_gp_write_metadata) {
1035 core_alua_update_tpg_primary_metadata(tg_pt_gp);
1038 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1039 " from primary access state %s to %s\n", (explicit) ? "explicit" :
1040 "implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1041 tg_pt_gp->tg_pt_gp_id,
1042 core_alua_dump_state(prev_state),
1043 core_alua_dump_state(new_state));
1045 core_alua_queue_state_change_ua(tg_pt_gp);
1047 mutex_unlock(&tg_pt_gp->tg_pt_gp_transition_mutex);
1051 int core_alua_do_port_transition(
1052 struct t10_alua_tg_pt_gp *l_tg_pt_gp,
1053 struct se_device *l_dev,
1054 struct se_lun *l_lun,
1055 struct se_node_acl *l_nacl,
1059 struct se_device *dev;
1060 struct t10_alua_lu_gp *lu_gp;
1061 struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
1062 struct t10_alua_tg_pt_gp *tg_pt_gp;
1063 int primary, valid_states, rc = 0;
1065 if (l_dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA)
1068 valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states;
1069 if (core_alua_check_transition(new_state, valid_states, &primary,
1073 local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
1074 spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
1075 lu_gp = local_lu_gp_mem->lu_gp;
1076 atomic_inc(&lu_gp->lu_gp_ref_cnt);
1077 spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
1079 * For storage objects that are members of the 'default_lu_gp',
1080 * we only do transition on the passed *l_tp_pt_gp, and not
1081 * on all of the matching target port groups IDs in default_lu_gp.
1083 if (!lu_gp->lu_gp_id) {
1085 * core_alua_do_transition_tg_pt() will always return
1088 l_tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
1089 l_tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
1090 rc = core_alua_do_transition_tg_pt(l_tg_pt_gp,
1091 new_state, explicit);
1092 atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
1096 * For all other LU groups aside from 'default_lu_gp', walk all of
1097 * the associated storage objects looking for a matching target port
1098 * group ID from the local target port group.
1100 spin_lock(&lu_gp->lu_gp_lock);
1101 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
1104 dev = lu_gp_mem->lu_gp_mem_dev;
1105 atomic_inc_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
1106 spin_unlock(&lu_gp->lu_gp_lock);
1108 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1109 list_for_each_entry(tg_pt_gp,
1110 &dev->t10_alua.tg_pt_gps_list,
1113 if (!tg_pt_gp->tg_pt_gp_valid_id)
1116 * If the target behavior port asymmetric access state
1117 * is changed for any target port group accessible via
1118 * a logical unit within a LU group, the target port
1119 * behavior group asymmetric access states for the same
1120 * target port group accessible via other logical units
1121 * in that LU group will also change.
1123 if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
1126 if (l_tg_pt_gp == tg_pt_gp) {
1127 tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
1128 tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
1130 tg_pt_gp->tg_pt_gp_alua_lun = NULL;
1131 tg_pt_gp->tg_pt_gp_alua_nacl = NULL;
1133 atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
1134 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1136 * core_alua_do_transition_tg_pt() will always return
1139 rc = core_alua_do_transition_tg_pt(tg_pt_gp,
1140 new_state, explicit);
1142 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1143 atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
1147 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1149 spin_lock(&lu_gp->lu_gp_lock);
1150 atomic_dec_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
1152 spin_unlock(&lu_gp->lu_gp_lock);
1155 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
1156 " Group IDs: %hu %s transition to primary state: %s\n",
1157 config_item_name(&lu_gp->lu_gp_group.cg_item),
1158 l_tg_pt_gp->tg_pt_gp_id,
1159 (explicit) ? "explicit" : "implicit",
1160 core_alua_dump_state(new_state));
1163 atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
1167 static int core_alua_update_tpg_secondary_metadata(struct se_lun *lun)
1169 struct se_portal_group *se_tpg = lun->lun_tpg;
1170 unsigned char *md_buf;
1174 mutex_lock(&lun->lun_tg_pt_md_mutex);
1176 md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL);
1178 pr_err("Unable to allocate buf for ALUA metadata\n");
1183 len = snprintf(md_buf, ALUA_MD_BUF_LEN, "alua_tg_pt_offline=%d\n"
1184 "alua_tg_pt_status=0x%02x\n",
1185 atomic_read(&lun->lun_tg_pt_secondary_offline),
1186 lun->lun_tg_pt_secondary_stat);
1188 if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL) {
1189 path = kasprintf(GFP_KERNEL, "%s/alua/%s/%s+%hu/lun_%llu",
1190 db_root, se_tpg->se_tpg_tfo->fabric_name,
1191 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg),
1192 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg),
1195 path = kasprintf(GFP_KERNEL, "%s/alua/%s/%s/lun_%llu",
1196 db_root, se_tpg->se_tpg_tfo->fabric_name,
1197 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg),
1205 rc = core_alua_write_tpg_metadata(path, md_buf, len);
1210 mutex_unlock(&lun->lun_tg_pt_md_mutex);
1214 static int core_alua_set_tg_pt_secondary_state(
1219 struct t10_alua_tg_pt_gp *tg_pt_gp;
1220 int trans_delay_msecs;
1222 spin_lock(&lun->lun_tg_pt_gp_lock);
1223 tg_pt_gp = lun->lun_tg_pt_gp;
1225 spin_unlock(&lun->lun_tg_pt_gp_lock);
1226 pr_err("Unable to complete secondary state"
1230 trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
1232 * Set the secondary ALUA target port access state to OFFLINE
1233 * or release the previously secondary state for struct se_lun
1236 atomic_set(&lun->lun_tg_pt_secondary_offline, 1);
1238 atomic_set(&lun->lun_tg_pt_secondary_offline, 0);
1240 lun->lun_tg_pt_secondary_stat = (explicit) ?
1241 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
1242 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;
1244 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1245 " to secondary access state: %s\n", (explicit) ? "explicit" :
1246 "implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1247 tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
1249 spin_unlock(&lun->lun_tg_pt_gp_lock);
1251 * Do the optional transition delay after we set the secondary
1252 * ALUA access state.
1254 if (trans_delay_msecs != 0)
1255 msleep_interruptible(trans_delay_msecs);
1257 * See if we need to update the ALUA fabric port metadata for
1258 * secondary state and status
1260 if (lun->lun_tg_pt_secondary_write_md)
1261 core_alua_update_tpg_secondary_metadata(lun);
1266 struct t10_alua_lba_map *
1267 core_alua_allocate_lba_map(struct list_head *list,
1268 u64 first_lba, u64 last_lba)
1270 struct t10_alua_lba_map *lba_map;
1272 lba_map = kmem_cache_zalloc(t10_alua_lba_map_cache, GFP_KERNEL);
1274 pr_err("Unable to allocate struct t10_alua_lba_map\n");
1275 return ERR_PTR(-ENOMEM);
1277 INIT_LIST_HEAD(&lba_map->lba_map_mem_list);
1278 lba_map->lba_map_first_lba = first_lba;
1279 lba_map->lba_map_last_lba = last_lba;
1281 list_add_tail(&lba_map->lba_map_list, list);
1286 core_alua_allocate_lba_map_mem(struct t10_alua_lba_map *lba_map,
1287 int pg_id, int state)
1289 struct t10_alua_lba_map_member *lba_map_mem;
1291 list_for_each_entry(lba_map_mem, &lba_map->lba_map_mem_list,
1293 if (lba_map_mem->lba_map_mem_alua_pg_id == pg_id) {
1294 pr_err("Duplicate pg_id %d in lba_map\n", pg_id);
1299 lba_map_mem = kmem_cache_zalloc(t10_alua_lba_map_mem_cache, GFP_KERNEL);
1301 pr_err("Unable to allocate struct t10_alua_lba_map_mem\n");
1304 lba_map_mem->lba_map_mem_alua_state = state;
1305 lba_map_mem->lba_map_mem_alua_pg_id = pg_id;
1307 list_add_tail(&lba_map_mem->lba_map_mem_list,
1308 &lba_map->lba_map_mem_list);
1313 core_alua_free_lba_map(struct list_head *lba_list)
1315 struct t10_alua_lba_map *lba_map, *lba_map_tmp;
1316 struct t10_alua_lba_map_member *lba_map_mem, *lba_map_mem_tmp;
1318 list_for_each_entry_safe(lba_map, lba_map_tmp, lba_list,
1320 list_for_each_entry_safe(lba_map_mem, lba_map_mem_tmp,
1321 &lba_map->lba_map_mem_list,
1323 list_del(&lba_map_mem->lba_map_mem_list);
1324 kmem_cache_free(t10_alua_lba_map_mem_cache,
1327 list_del(&lba_map->lba_map_list);
1328 kmem_cache_free(t10_alua_lba_map_cache, lba_map);
1333 core_alua_set_lba_map(struct se_device *dev, struct list_head *lba_map_list,
1334 int segment_size, int segment_mult)
1336 struct list_head old_lba_map_list;
1337 struct t10_alua_tg_pt_gp *tg_pt_gp;
1338 int activate = 0, supported;
1340 INIT_LIST_HEAD(&old_lba_map_list);
1341 spin_lock(&dev->t10_alua.lba_map_lock);
1342 dev->t10_alua.lba_map_segment_size = segment_size;
1343 dev->t10_alua.lba_map_segment_multiplier = segment_mult;
1344 list_splice_init(&dev->t10_alua.lba_map_list, &old_lba_map_list);
1346 list_splice_init(lba_map_list, &dev->t10_alua.lba_map_list);
1349 spin_unlock(&dev->t10_alua.lba_map_lock);
1350 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1351 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
1354 if (!tg_pt_gp->tg_pt_gp_valid_id)
1356 supported = tg_pt_gp->tg_pt_gp_alua_supported_states;
1358 supported |= ALUA_LBD_SUP;
1360 supported &= ~ALUA_LBD_SUP;
1361 tg_pt_gp->tg_pt_gp_alua_supported_states = supported;
1363 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1364 core_alua_free_lba_map(&old_lba_map_list);
1367 struct t10_alua_lu_gp *
1368 core_alua_allocate_lu_gp(const char *name, int def_group)
1370 struct t10_alua_lu_gp *lu_gp;
1372 lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
1374 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1375 return ERR_PTR(-ENOMEM);
1377 INIT_LIST_HEAD(&lu_gp->lu_gp_node);
1378 INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
1379 spin_lock_init(&lu_gp->lu_gp_lock);
1380 atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
1383 lu_gp->lu_gp_id = alua_lu_gps_counter++;
1384 lu_gp->lu_gp_valid_id = 1;
1385 alua_lu_gps_count++;
1391 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
1393 struct t10_alua_lu_gp *lu_gp_tmp;
1396 * The lu_gp->lu_gp_id may only be set once..
1398 if (lu_gp->lu_gp_valid_id) {
1399 pr_warn("ALUA LU Group already has a valid ID,"
1400 " ignoring request\n");
1404 spin_lock(&lu_gps_lock);
1405 if (alua_lu_gps_count == 0x0000ffff) {
1406 pr_err("Maximum ALUA alua_lu_gps_count:"
1407 " 0x0000ffff reached\n");
1408 spin_unlock(&lu_gps_lock);
1409 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1413 lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
1414 alua_lu_gps_counter++;
1416 list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
1417 if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
1421 pr_warn("ALUA Logical Unit Group ID: %hu"
1422 " already exists, ignoring request\n",
1424 spin_unlock(&lu_gps_lock);
1429 lu_gp->lu_gp_id = lu_gp_id_tmp;
1430 lu_gp->lu_gp_valid_id = 1;
1431 list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
1432 alua_lu_gps_count++;
1433 spin_unlock(&lu_gps_lock);
1438 static struct t10_alua_lu_gp_member *
1439 core_alua_allocate_lu_gp_mem(struct se_device *dev)
1441 struct t10_alua_lu_gp_member *lu_gp_mem;
1443 lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
1445 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1446 return ERR_PTR(-ENOMEM);
1448 INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
1449 spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
1450 atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
1452 lu_gp_mem->lu_gp_mem_dev = dev;
1453 dev->dev_alua_lu_gp_mem = lu_gp_mem;
1458 void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
1460 struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
1462 * Once we have reached this point, config_item_put() has
1463 * already been called from target_core_alua_drop_lu_gp().
1465 * Here, we remove the *lu_gp from the global list so that
1466 * no associations can be made while we are releasing
1467 * struct t10_alua_lu_gp.
1469 spin_lock(&lu_gps_lock);
1470 list_del(&lu_gp->lu_gp_node);
1471 alua_lu_gps_count--;
1472 spin_unlock(&lu_gps_lock);
1474 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1475 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1476 * released with core_alua_put_lu_gp_from_name()
1478 while (atomic_read(&lu_gp->lu_gp_ref_cnt))
1481 * Release reference to struct t10_alua_lu_gp * from all associated
1484 spin_lock(&lu_gp->lu_gp_lock);
1485 list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
1486 &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1487 if (lu_gp_mem->lu_gp_assoc) {
1488 list_del(&lu_gp_mem->lu_gp_mem_list);
1489 lu_gp->lu_gp_members--;
1490 lu_gp_mem->lu_gp_assoc = 0;
1492 spin_unlock(&lu_gp->lu_gp_lock);
1495 * lu_gp_mem is associated with a single
1496 * struct se_device->dev_alua_lu_gp_mem, and is released when
1497 * struct se_device is released via core_alua_free_lu_gp_mem().
1499 * If the passed lu_gp does NOT match the default_lu_gp, assume
1500 * we want to re-associate a given lu_gp_mem with default_lu_gp.
1502 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1503 if (lu_gp != default_lu_gp)
1504 __core_alua_attach_lu_gp_mem(lu_gp_mem,
1507 lu_gp_mem->lu_gp = NULL;
1508 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1510 spin_lock(&lu_gp->lu_gp_lock);
1512 spin_unlock(&lu_gp->lu_gp_lock);
1514 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1517 void core_alua_free_lu_gp_mem(struct se_device *dev)
1519 struct t10_alua_lu_gp *lu_gp;
1520 struct t10_alua_lu_gp_member *lu_gp_mem;
1522 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1526 while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
1529 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1530 lu_gp = lu_gp_mem->lu_gp;
1532 spin_lock(&lu_gp->lu_gp_lock);
1533 if (lu_gp_mem->lu_gp_assoc) {
1534 list_del(&lu_gp_mem->lu_gp_mem_list);
1535 lu_gp->lu_gp_members--;
1536 lu_gp_mem->lu_gp_assoc = 0;
1538 spin_unlock(&lu_gp->lu_gp_lock);
1539 lu_gp_mem->lu_gp = NULL;
1541 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1543 kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
1546 struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
1548 struct t10_alua_lu_gp *lu_gp;
1549 struct config_item *ci;
1551 spin_lock(&lu_gps_lock);
1552 list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
1553 if (!lu_gp->lu_gp_valid_id)
1555 ci = &lu_gp->lu_gp_group.cg_item;
1556 if (!strcmp(config_item_name(ci), name)) {
1557 atomic_inc(&lu_gp->lu_gp_ref_cnt);
1558 spin_unlock(&lu_gps_lock);
1562 spin_unlock(&lu_gps_lock);
1567 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
1569 spin_lock(&lu_gps_lock);
1570 atomic_dec(&lu_gp->lu_gp_ref_cnt);
1571 spin_unlock(&lu_gps_lock);
1575 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1577 void __core_alua_attach_lu_gp_mem(
1578 struct t10_alua_lu_gp_member *lu_gp_mem,
1579 struct t10_alua_lu_gp *lu_gp)
1581 spin_lock(&lu_gp->lu_gp_lock);
1582 lu_gp_mem->lu_gp = lu_gp;
1583 lu_gp_mem->lu_gp_assoc = 1;
1584 list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
1585 lu_gp->lu_gp_members++;
1586 spin_unlock(&lu_gp->lu_gp_lock);
1590 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1592 void __core_alua_drop_lu_gp_mem(
1593 struct t10_alua_lu_gp_member *lu_gp_mem,
1594 struct t10_alua_lu_gp *lu_gp)
1596 spin_lock(&lu_gp->lu_gp_lock);
1597 list_del(&lu_gp_mem->lu_gp_mem_list);
1598 lu_gp_mem->lu_gp = NULL;
1599 lu_gp_mem->lu_gp_assoc = 0;
1600 lu_gp->lu_gp_members--;
1601 spin_unlock(&lu_gp->lu_gp_lock);
1604 struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(struct se_device *dev,
1605 const char *name, int def_group)
1607 struct t10_alua_tg_pt_gp *tg_pt_gp;
1609 tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
1611 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1614 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
1615 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_lun_list);
1616 mutex_init(&tg_pt_gp->tg_pt_gp_transition_mutex);
1617 spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
1618 atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
1619 tg_pt_gp->tg_pt_gp_dev = dev;
1620 tg_pt_gp->tg_pt_gp_alua_access_state =
1621 ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
1623 * Enable both explicit and implicit ALUA support by default
1625 tg_pt_gp->tg_pt_gp_alua_access_type =
1626 TPGS_EXPLICIT_ALUA | TPGS_IMPLICIT_ALUA;
1628 * Set the default Active/NonOptimized Delay in milliseconds
1630 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
1631 tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
1632 tg_pt_gp->tg_pt_gp_implicit_trans_secs = ALUA_DEFAULT_IMPLICIT_TRANS_SECS;
1635 * Enable all supported states
1637 tg_pt_gp->tg_pt_gp_alua_supported_states =
1638 ALUA_T_SUP | ALUA_O_SUP |
1639 ALUA_U_SUP | ALUA_S_SUP | ALUA_AN_SUP | ALUA_AO_SUP;
1642 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1643 tg_pt_gp->tg_pt_gp_id =
1644 dev->t10_alua.alua_tg_pt_gps_counter++;
1645 tg_pt_gp->tg_pt_gp_valid_id = 1;
1646 dev->t10_alua.alua_tg_pt_gps_count++;
1647 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1648 &dev->t10_alua.tg_pt_gps_list);
1649 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1655 int core_alua_set_tg_pt_gp_id(
1656 struct t10_alua_tg_pt_gp *tg_pt_gp,
1659 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1660 struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
1661 u16 tg_pt_gp_id_tmp;
1664 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1666 if (tg_pt_gp->tg_pt_gp_valid_id) {
1667 pr_warn("ALUA TG PT Group already has a valid ID,"
1668 " ignoring request\n");
1672 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1673 if (dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
1674 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1675 " 0x0000ffff reached\n");
1676 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1680 tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
1681 dev->t10_alua.alua_tg_pt_gps_counter++;
1683 list_for_each_entry(tg_pt_gp_tmp, &dev->t10_alua.tg_pt_gps_list,
1685 if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
1689 pr_err("ALUA Target Port Group ID: %hu already"
1690 " exists, ignoring request\n", tg_pt_gp_id);
1691 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1696 tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
1697 tg_pt_gp->tg_pt_gp_valid_id = 1;
1698 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1699 &dev->t10_alua.tg_pt_gps_list);
1700 dev->t10_alua.alua_tg_pt_gps_count++;
1701 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1706 void core_alua_free_tg_pt_gp(
1707 struct t10_alua_tg_pt_gp *tg_pt_gp)
1709 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1710 struct se_lun *lun, *next;
1713 * Once we have reached this point, config_item_put() has already
1714 * been called from target_core_alua_drop_tg_pt_gp().
1716 * Here we remove *tg_pt_gp from the global list so that
1717 * no associations *OR* explicit ALUA via SET_TARGET_PORT_GROUPS
1718 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1720 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1721 if (tg_pt_gp->tg_pt_gp_valid_id) {
1722 list_del(&tg_pt_gp->tg_pt_gp_list);
1723 dev->t10_alua.alua_tg_pt_gps_count--;
1725 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1728 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1729 * core_alua_get_tg_pt_gp_by_name() in
1730 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1731 * to be released with core_alua_put_tg_pt_gp_from_name().
1733 while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
1737 * Release reference to struct t10_alua_tg_pt_gp from all associated
1740 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1741 list_for_each_entry_safe(lun, next,
1742 &tg_pt_gp->tg_pt_gp_lun_list, lun_tg_pt_gp_link) {
1743 list_del_init(&lun->lun_tg_pt_gp_link);
1744 tg_pt_gp->tg_pt_gp_members--;
1746 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1748 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1749 * assume we want to re-associate a given tg_pt_gp_mem with
1752 spin_lock(&lun->lun_tg_pt_gp_lock);
1753 if (tg_pt_gp != dev->t10_alua.default_tg_pt_gp) {
1754 __target_attach_tg_pt_gp(lun,
1755 dev->t10_alua.default_tg_pt_gp);
1757 lun->lun_tg_pt_gp = NULL;
1758 spin_unlock(&lun->lun_tg_pt_gp_lock);
1760 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1762 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1764 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1767 static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
1768 struct se_device *dev, const char *name)
1770 struct t10_alua_tg_pt_gp *tg_pt_gp;
1771 struct config_item *ci;
1773 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1774 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
1776 if (!tg_pt_gp->tg_pt_gp_valid_id)
1778 ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1779 if (!strcmp(config_item_name(ci), name)) {
1780 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
1781 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1785 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1790 static void core_alua_put_tg_pt_gp_from_name(
1791 struct t10_alua_tg_pt_gp *tg_pt_gp)
1793 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1795 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1796 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
1797 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1800 static void __target_attach_tg_pt_gp(struct se_lun *lun,
1801 struct t10_alua_tg_pt_gp *tg_pt_gp)
1803 struct se_dev_entry *se_deve;
1805 assert_spin_locked(&lun->lun_tg_pt_gp_lock);
1807 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1808 lun->lun_tg_pt_gp = tg_pt_gp;
1809 list_add_tail(&lun->lun_tg_pt_gp_link, &tg_pt_gp->tg_pt_gp_lun_list);
1810 tg_pt_gp->tg_pt_gp_members++;
1811 spin_lock(&lun->lun_deve_lock);
1812 list_for_each_entry(se_deve, &lun->lun_deve_list, lun_link)
1813 core_scsi3_ua_allocate(se_deve, 0x3f,
1814 ASCQ_3FH_INQUIRY_DATA_HAS_CHANGED);
1815 spin_unlock(&lun->lun_deve_lock);
1816 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1819 void target_attach_tg_pt_gp(struct se_lun *lun,
1820 struct t10_alua_tg_pt_gp *tg_pt_gp)
1822 spin_lock(&lun->lun_tg_pt_gp_lock);
1823 __target_attach_tg_pt_gp(lun, tg_pt_gp);
1824 spin_unlock(&lun->lun_tg_pt_gp_lock);
1827 static void __target_detach_tg_pt_gp(struct se_lun *lun,
1828 struct t10_alua_tg_pt_gp *tg_pt_gp)
1830 assert_spin_locked(&lun->lun_tg_pt_gp_lock);
1832 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1833 list_del_init(&lun->lun_tg_pt_gp_link);
1834 tg_pt_gp->tg_pt_gp_members--;
1835 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1837 lun->lun_tg_pt_gp = NULL;
1840 void target_detach_tg_pt_gp(struct se_lun *lun)
1842 struct t10_alua_tg_pt_gp *tg_pt_gp;
1844 spin_lock(&lun->lun_tg_pt_gp_lock);
1845 tg_pt_gp = lun->lun_tg_pt_gp;
1847 __target_detach_tg_pt_gp(lun, tg_pt_gp);
1848 spin_unlock(&lun->lun_tg_pt_gp_lock);
1851 ssize_t core_alua_show_tg_pt_gp_info(struct se_lun *lun, char *page)
1853 struct config_item *tg_pt_ci;
1854 struct t10_alua_tg_pt_gp *tg_pt_gp;
1857 spin_lock(&lun->lun_tg_pt_gp_lock);
1858 tg_pt_gp = lun->lun_tg_pt_gp;
1860 tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1861 len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
1862 " %hu\nTG Port Primary Access State: %s\nTG Port "
1863 "Primary Access Status: %s\nTG Port Secondary Access"
1864 " State: %s\nTG Port Secondary Access Status: %s\n",
1865 config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
1866 core_alua_dump_state(
1867 tg_pt_gp->tg_pt_gp_alua_access_state),
1868 core_alua_dump_status(
1869 tg_pt_gp->tg_pt_gp_alua_access_status),
1870 atomic_read(&lun->lun_tg_pt_secondary_offline) ?
1872 core_alua_dump_status(lun->lun_tg_pt_secondary_stat));
1874 spin_unlock(&lun->lun_tg_pt_gp_lock);
1879 ssize_t core_alua_store_tg_pt_gp_info(
1884 struct se_portal_group *tpg = lun->lun_tpg;
1886 * rcu_dereference_raw protected by se_lun->lun_group symlink
1887 * reference to se_device->dev_group.
1889 struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
1890 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
1891 unsigned char buf[TG_PT_GROUP_NAME_BUF];
1894 if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA ||
1895 (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
1898 if (count > TG_PT_GROUP_NAME_BUF) {
1899 pr_err("ALUA Target Port Group alias too large!\n");
1902 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
1903 memcpy(buf, page, count);
1905 * Any ALUA target port group alias besides "NULL" means we will be
1906 * making a new group association.
1908 if (strcmp(strstrip(buf), "NULL")) {
1910 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1911 * struct t10_alua_tg_pt_gp. This reference is released with
1912 * core_alua_put_tg_pt_gp_from_name() below.
1914 tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(dev,
1920 spin_lock(&lun->lun_tg_pt_gp_lock);
1921 tg_pt_gp = lun->lun_tg_pt_gp;
1924 * Clearing an existing tg_pt_gp association, and replacing
1925 * with the default_tg_pt_gp.
1927 if (!tg_pt_gp_new) {
1928 pr_debug("Target_Core_ConfigFS: Moving"
1929 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1930 " alua/%s, ID: %hu back to"
1931 " default_tg_pt_gp\n",
1932 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1933 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1934 config_item_name(&lun->lun_group.cg_item),
1936 &tg_pt_gp->tg_pt_gp_group.cg_item),
1937 tg_pt_gp->tg_pt_gp_id);
1939 __target_detach_tg_pt_gp(lun, tg_pt_gp);
1940 __target_attach_tg_pt_gp(lun,
1941 dev->t10_alua.default_tg_pt_gp);
1942 spin_unlock(&lun->lun_tg_pt_gp_lock);
1946 __target_detach_tg_pt_gp(lun, tg_pt_gp);
1950 __target_attach_tg_pt_gp(lun, tg_pt_gp_new);
1951 spin_unlock(&lun->lun_tg_pt_gp_lock);
1952 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
1953 " Target Port Group: alua/%s, ID: %hu\n", (move) ?
1954 "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1955 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1956 config_item_name(&lun->lun_group.cg_item),
1957 config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
1958 tg_pt_gp_new->tg_pt_gp_id);
1960 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1964 ssize_t core_alua_show_access_type(
1965 struct t10_alua_tg_pt_gp *tg_pt_gp,
1968 if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA) &&
1969 (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA))
1970 return sprintf(page, "Implicit and Explicit\n");
1971 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)
1972 return sprintf(page, "Implicit\n");
1973 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)
1974 return sprintf(page, "Explicit\n");
1976 return sprintf(page, "None\n");
1979 ssize_t core_alua_store_access_type(
1980 struct t10_alua_tg_pt_gp *tg_pt_gp,
1987 ret = kstrtoul(page, 0, &tmp);
1989 pr_err("Unable to extract alua_access_type\n");
1992 if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
1993 pr_err("Illegal value for alua_access_type:"
1998 tg_pt_gp->tg_pt_gp_alua_access_type =
1999 TPGS_IMPLICIT_ALUA | TPGS_EXPLICIT_ALUA;
2001 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICIT_ALUA;
2003 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICIT_ALUA;
2005 tg_pt_gp->tg_pt_gp_alua_access_type = 0;
2010 ssize_t core_alua_show_nonop_delay_msecs(
2011 struct t10_alua_tg_pt_gp *tg_pt_gp,
2014 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
2017 ssize_t core_alua_store_nonop_delay_msecs(
2018 struct t10_alua_tg_pt_gp *tg_pt_gp,
2025 ret = kstrtoul(page, 0, &tmp);
2027 pr_err("Unable to extract nonop_delay_msecs\n");
2030 if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
2031 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
2032 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
2033 ALUA_MAX_NONOP_DELAY_MSECS);
2036 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
2041 ssize_t core_alua_show_trans_delay_msecs(
2042 struct t10_alua_tg_pt_gp *tg_pt_gp,
2045 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
2048 ssize_t core_alua_store_trans_delay_msecs(
2049 struct t10_alua_tg_pt_gp *tg_pt_gp,
2056 ret = kstrtoul(page, 0, &tmp);
2058 pr_err("Unable to extract trans_delay_msecs\n");
2061 if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
2062 pr_err("Passed trans_delay_msecs: %lu, exceeds"
2063 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
2064 ALUA_MAX_TRANS_DELAY_MSECS);
2067 tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
2072 ssize_t core_alua_show_implicit_trans_secs(
2073 struct t10_alua_tg_pt_gp *tg_pt_gp,
2076 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_implicit_trans_secs);
2079 ssize_t core_alua_store_implicit_trans_secs(
2080 struct t10_alua_tg_pt_gp *tg_pt_gp,
2087 ret = kstrtoul(page, 0, &tmp);
2089 pr_err("Unable to extract implicit_trans_secs\n");
2092 if (tmp > ALUA_MAX_IMPLICIT_TRANS_SECS) {
2093 pr_err("Passed implicit_trans_secs: %lu, exceeds"
2094 " ALUA_MAX_IMPLICIT_TRANS_SECS: %d\n", tmp,
2095 ALUA_MAX_IMPLICIT_TRANS_SECS);
2098 tg_pt_gp->tg_pt_gp_implicit_trans_secs = (int)tmp;
2103 ssize_t core_alua_show_preferred_bit(
2104 struct t10_alua_tg_pt_gp *tg_pt_gp,
2107 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
2110 ssize_t core_alua_store_preferred_bit(
2111 struct t10_alua_tg_pt_gp *tg_pt_gp,
2118 ret = kstrtoul(page, 0, &tmp);
2120 pr_err("Unable to extract preferred ALUA value\n");
2123 if ((tmp != 0) && (tmp != 1)) {
2124 pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
2127 tg_pt_gp->tg_pt_gp_pref = (int)tmp;
2132 ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
2134 return sprintf(page, "%d\n",
2135 atomic_read(&lun->lun_tg_pt_secondary_offline));
2138 ssize_t core_alua_store_offline_bit(
2144 * rcu_dereference_raw protected by se_lun->lun_group symlink
2145 * reference to se_device->dev_group.
2147 struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
2151 if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA ||
2152 (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
2155 ret = kstrtoul(page, 0, &tmp);
2157 pr_err("Unable to extract alua_tg_pt_offline value\n");
2160 if ((tmp != 0) && (tmp != 1)) {
2161 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
2166 ret = core_alua_set_tg_pt_secondary_state(lun, 0, (int)tmp);
2173 ssize_t core_alua_show_secondary_status(
2177 return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_stat);
2180 ssize_t core_alua_store_secondary_status(
2188 ret = kstrtoul(page, 0, &tmp);
2190 pr_err("Unable to extract alua_tg_pt_status\n");
2193 if ((tmp != ALUA_STATUS_NONE) &&
2194 (tmp != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2195 (tmp != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
2196 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
2200 lun->lun_tg_pt_secondary_stat = (int)tmp;
2205 ssize_t core_alua_show_secondary_write_metadata(
2209 return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_write_md);
2212 ssize_t core_alua_store_secondary_write_metadata(
2220 ret = kstrtoul(page, 0, &tmp);
2222 pr_err("Unable to extract alua_tg_pt_write_md\n");
2225 if ((tmp != 0) && (tmp != 1)) {
2226 pr_err("Illegal value for alua_tg_pt_write_md:"
2230 lun->lun_tg_pt_secondary_write_md = (int)tmp;
2235 int core_setup_alua(struct se_device *dev)
2237 if (!(dev->transport_flags &
2238 TRANSPORT_FLAG_PASSTHROUGH_ALUA) &&
2239 !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
2240 struct t10_alua_lu_gp_member *lu_gp_mem;
2243 * Associate this struct se_device with the default ALUA
2246 lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
2247 if (IS_ERR(lu_gp_mem))
2248 return PTR_ERR(lu_gp_mem);
2250 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2251 __core_alua_attach_lu_gp_mem(lu_gp_mem,
2253 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2255 pr_debug("%s: Adding to default ALUA LU Group:"
2256 " core/alua/lu_gps/default_lu_gp\n",
2257 dev->transport->name);