27 #include <linux/slab.h>
30 #include <linux/export.h>
31 #include <scsi/scsi.h>
33 #include <asm/unaligned.h>
44 static int core_alua_check_transition(
int state,
int *
primary);
45 static int core_alua_set_tg_pt_secondary_state(
49 static u16 alua_lu_gps_counter;
50 static u32 alua_lu_gps_count;
81 pr_warn(
"REPORT TARGET PORT GROUPS allocation length %u too"
83 (ext_hdr) ?
"extended" :
"normal");
89 spin_lock(&su_dev->
t10_alua.tg_pt_gps_lock);
126 buf[off++] = ((tg_pt_gp->
tg_pt_gp_id >> 8) & 0xff);
147 port = tg_pt_gp_mem->
tg_pt;
157 buf[off++] = ((port->
sep_rtpi >> 8) & 0xff);
158 buf[off++] = (port->
sep_rtpi & 0xff);
163 spin_unlock(&su_dev->
t10_alua.tg_pt_gps_lock);
182 port = cmd->
se_lun->lun_sep;
222 pr_warn(
"SET TARGET PORT GROUPS parameter list length %u too"
235 if (!l_tg_pt_gp_mem) {
236 pr_err(
"Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
242 l_tg_pt_gp = l_tg_pt_gp_mem->
tg_pt_gp;
245 pr_err(
"Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n");
254 pr_debug(
"Unable to process SET_TARGET_PORT_GROUPS"
255 " while TPGS_EXPLICT_ALUA is disabled\n");
264 alua_access_state = (ptr[0] & 0x0f);
270 rc = core_alua_check_transition(alua_access_state, &primary);
301 tg_pt_id = get_unaligned_be16(ptr + 2);
306 spin_lock(&su_dev->
t10_alua.tg_pt_gps_lock);
318 spin_unlock(&su_dev->
t10_alua.tg_pt_gps_lock);
322 alua_access_state, 1);
324 spin_lock(&su_dev->
t10_alua.tg_pt_gps_lock);
329 spin_unlock(&su_dev->
t10_alua.tg_pt_gps_lock);
345 rtpi = get_unaligned_be16(ptr + 2);
359 rc = core_alua_set_tg_pt_secondary_state(
360 tg_pt_gp_mem, port, 1, 1);
389 static inline int core_alua_state_nonoptimized(
392 int nonop_delay_msecs,
405 static inline int core_alua_state_standby(
424 switch (cdb[1] & 0x1f) {
453 static inline int core_alua_state_unavailable(
466 switch (cdb[1] & 0x1f) {
493 static inline int core_alua_state_transition(
506 switch (cdb[1] & 0x1f) {
530 static int core_alua_state_check_nop(
550 static int core_alua_state_check(
559 int out_alua_state, nonop_delay_msecs;
569 pr_debug(
"ALUA: Got secondary offline status for local"
595 switch (out_alua_state) {
597 return core_alua_state_nonoptimized(cmd, cdb,
598 nonop_delay_msecs, alua_ascq);
600 return core_alua_state_standby(cmd, cdb, alua_ascq);
602 return core_alua_state_unavailable(cmd, cdb, alua_ascq);
604 return core_alua_state_transition(cmd, cdb, alua_ascq);
611 pr_err(
"Unknown ALUA access state: 0x%02x\n",
622 static int core_alua_check_transition(
int state,
int *
primary)
643 pr_err(
"Unknown ALUA access state: 0x%02x\n", state);
650 static char *core_alua_dump_state(
int state)
654 return "Active/Optimized";
656 return "Active/NonOptimized";
660 return "Unavailable";
676 return "Altered by Explict STPG";
678 return "Altered by Implict ALUA";
716 static int core_alua_write_tpg_metadata(
718 unsigned char *md_buf,
729 if (IS_ERR(file) || !file || !file->f_dentry) {
730 pr_err(
"filp_open(%s) for ALUA metadata failed\n",
735 iov[0].iov_base = &md_buf[0];
736 iov[0].iov_len = md_buf_len;
744 pr_err(
"Error writing ALUA metadata file: %s\n", path);
756 static int core_alua_update_tpg_primary_metadata(
759 unsigned char *md_buf)
770 "alua_access_state=0x%02x\n"
771 "alua_access_status=0x%02x\n",
776 "/var/target/alua/tpgs_%s/%s", &wwn->
unit_serial[0],
779 return core_alua_write_tpg_metadata(path, md_buf, len);
782 static int core_alua_do_transition_tg_pt(
786 unsigned char *md_buf,
846 (l_port !=
NULL) && (l_port == port))
874 core_alua_update_tpg_primary_metadata(tg_pt_gp,
883 pr_debug(
"Successful %s ALUA transition TG PT Group: %s ID: %hu"
884 " from primary access state %s to %s\n", (explict) ?
"explict" :
886 tg_pt_gp->
tg_pt_gp_id, core_alua_dump_state(old_state),
887 core_alua_dump_state(new_state));
907 unsigned char *md_buf;
910 if (core_alua_check_transition(new_state, &primary) != 0)
915 pr_err(
"Unable to allocate buf for ALUA metadata\n");
921 lu_gp = local_lu_gp_mem->
lu_gp;
935 core_alua_do_transition_tg_pt(l_tg_pt_gp, l_port, l_nacl,
936 md_buf, new_state, explict);
957 spin_lock(&su_dev->
t10_alua.tg_pt_gps_lock);
975 if (l_tg_pt_gp == tg_pt_gp) {
984 spin_unlock(&su_dev->
t10_alua.tg_pt_gps_lock);
989 core_alua_do_transition_tg_pt(tg_pt_gp, port,
990 nacl, md_buf, new_state, explict);
992 spin_lock(&su_dev->
t10_alua.tg_pt_gps_lock);
996 spin_unlock(&su_dev->
t10_alua.tg_pt_gps_lock);
1004 pr_debug(
"Successfully processed LU Group: %s all ALUA TG PT"
1005 " Group IDs: %hu %s transition to primary state: %s\n",
1007 l_tg_pt_gp->
tg_pt_gp_id, (explict) ?
"explict" :
"implict",
1008 core_alua_dump_state(new_state));
1019 static int core_alua_update_tpg_secondary_metadata(
1022 unsigned char *md_buf,
1039 len =
snprintf(md_buf, md_buf_len,
"alua_tg_pt_offline=%d\n"
1040 "alua_tg_pt_status=0x%02x\n",
1048 return core_alua_write_tpg_metadata(path, md_buf, len);
1051 static int core_alua_set_tg_pt_secondary_state(
1058 unsigned char *md_buf;
1060 int trans_delay_msecs;
1066 pr_err(
"Unable to complete secondary state"
1085 pr_debug(
"Successful %s ALUA transition TG PT Group: %s ID: %hu"
1086 " to secondary access state: %s\n", (explict) ?
"explict" :
1088 tg_pt_gp->
tg_pt_gp_id, (offline) ?
"OFFLINE" :
"ONLINE");
1095 if (trans_delay_msecs != 0)
1104 pr_err(
"Unable to allocate md_buf for"
1105 " secondary ALUA access metadata\n");
1109 core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem, port,
1110 md_buf, md_buf_len);
1126 pr_err(
"Unable to allocate struct t10_alua_lu_gp\n");
1135 lu_gp->
lu_gp_id = alua_lu_gps_counter++;
1137 alua_lu_gps_count++;
1151 pr_warn(
"ALUA LU Group already has a valid ID,"
1152 " ignoring request\n");
1156 spin_lock(&lu_gps_lock);
1157 if (alua_lu_gps_count == 0x0000ffff) {
1158 pr_err(
"Maximum ALUA alua_lu_gps_count:"
1159 " 0x0000ffff reached\n");
1160 spin_unlock(&lu_gps_lock);
1165 lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
1166 alua_lu_gps_counter++;
1169 if (lu_gp_tmp->
lu_gp_id == lu_gp_id_tmp) {
1173 pr_warn(
"ALUA Logical Unit Group ID: %hu"
1174 " already exists, ignoring request\n",
1176 spin_unlock(&lu_gps_lock);
1184 alua_lu_gps_count++;
1185 spin_unlock(&lu_gps_lock);
1197 pr_err(
"Unable to allocate struct t10_alua_lu_gp_member\n");
1221 spin_lock(&lu_gps_lock);
1223 alua_lu_gps_count--;
1224 spin_unlock(&lu_gps_lock);
1255 if (lu_gp != default_lu_gp)
1287 lu_gp = lu_gp_mem->
lu_gp;
1308 spin_lock(&lu_gps_lock);
1313 if (!
strcmp(config_item_name(ci), name)) {
1315 spin_unlock(&lu_gps_lock);
1319 spin_unlock(&lu_gps_lock);
1326 spin_lock(&lu_gps_lock);
1328 spin_unlock(&lu_gps_lock);
1339 lu_gp_mem->
lu_gp = lu_gp;
1370 pr_err(
"Unable to allocate struct t10_alua_tg_pt_gp\n");
1395 spin_lock(&su_dev->
t10_alua.tg_pt_gps_lock);
1397 su_dev->
t10_alua.alua_tg_pt_gps_counter++;
1399 su_dev->
t10_alua.alua_tg_pt_gps_count++;
1402 spin_unlock(&su_dev->
t10_alua.tg_pt_gps_lock);
1414 u16 tg_pt_gp_id_tmp;
1419 pr_warn(
"ALUA TG PT Group already has a valid ID,"
1420 " ignoring request\n");
1424 spin_lock(&su_dev->
t10_alua.tg_pt_gps_lock);
1425 if (su_dev->
t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
1426 pr_err(
"Maximum ALUA alua_tg_pt_gps_count:"
1427 " 0x0000ffff reached\n");
1428 spin_unlock(&su_dev->
t10_alua.tg_pt_gps_lock);
1433 tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
1434 su_dev->
t10_alua.alua_tg_pt_gps_counter++;
1438 if (tg_pt_gp_tmp->
tg_pt_gp_id == tg_pt_gp_id_tmp) {
1442 pr_err(
"ALUA Target Port Group ID: %hu already"
1443 " exists, ignoring request\n", tg_pt_gp_id);
1444 spin_unlock(&su_dev->
t10_alua.tg_pt_gps_lock);
1453 su_dev->
t10_alua.alua_tg_pt_gps_count++;
1454 spin_unlock(&su_dev->
t10_alua.tg_pt_gps_lock);
1466 if (!tg_pt_gp_mem) {
1467 pr_err(
"Unable to allocate struct t10_alua_tg_pt_gp_member\n");
1477 return tg_pt_gp_mem;
1493 spin_lock(&su_dev->
t10_alua.tg_pt_gps_lock);
1495 su_dev->
t10_alua.alua_tg_pt_gps_counter--;
1496 spin_unlock(&su_dev->
t10_alua.tg_pt_gps_lock);
1528 if (tg_pt_gp != su_dev->
t10_alua.default_tg_pt_gp) {
1530 su_dev->
t10_alua.default_tg_pt_gp);
1583 spin_lock(&su_dev->
t10_alua.tg_pt_gps_lock);
1589 if (!
strcmp(config_item_name(ci), name)) {
1591 spin_unlock(&su_dev->
t10_alua.tg_pt_gps_lock);
1595 spin_unlock(&su_dev->
t10_alua.tg_pt_gps_lock);
1600 static void core_alua_put_tg_pt_gp_from_name(
1605 spin_lock(&su_dev->
t10_alua.tg_pt_gps_lock);
1607 spin_unlock(&su_dev->
t10_alua.tg_pt_gps_lock);
1629 static void __core_alua_drop_tg_pt_gp_mem(
1661 len +=
sprintf(page,
"TG Port Alias: %s\nTG Port Group ID:"
1662 " %hu\nTG Port Primary Access State: %s\nTG Port "
1663 "Primary Access Status: %s\nTG Port Secondary Access"
1664 " State: %s\nTG Port Secondary Access Status: %s\n",
1665 config_item_name(tg_pt_ci), tg_pt_gp->
tg_pt_gp_id,
1696 pr_warn(
"SPC3_ALUA_EMULATED not enabled for"
1697 " %s/tpgt_%hu/%s\n", tpg->
se_tpg_tfo->tpg_get_wwn(tpg),
1699 config_item_name(&lun->
lun_group.cg_item));
1704 pr_err(
"ALUA Target Port Group alias too large!\n");
1708 memcpy(buf, page, count);
1713 if (
strcmp(strstrip(buf),
"NULL")) {
1719 tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(su_dev,
1725 if (!tg_pt_gp_mem) {
1727 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1728 pr_err(
"NULL struct se_port->sep_alua_tg_pt_gp_mem pointer\n");
1739 if (!tg_pt_gp_new) {
1740 pr_debug(
"Target_Core_ConfigFS: Moving"
1741 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1742 " alua/%s, ID: %hu back to"
1743 " default_tg_pt_gp\n",
1746 config_item_name(&lun->
lun_group.cg_item),
1751 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1753 su_dev->
t10_alua.default_tg_pt_gp);
1761 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1769 pr_debug(
"Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
1770 " Target Port Group: alua/%s, ID: %hu\n", (move) ?
1771 "Moving" :
"Adding", tpg->
se_tpg_tfo->tpg_get_wwn(tpg),
1773 config_item_name(&lun->
lun_group.cg_item),
1774 config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
1775 tg_pt_gp_new->tg_pt_gp_id);
1777 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1787 return sprintf(page,
"Implict and Explict\n");
1789 return sprintf(page,
"Implict\n");
1791 return sprintf(page,
"Explict\n");
1793 return sprintf(page,
"None\n");
1806 pr_err(
"Unable to extract alua_access_type\n");
1809 if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
1810 pr_err(
"Illegal value for alua_access_type:"
1844 pr_err(
"Unable to extract nonop_delay_msecs\n");
1848 pr_err(
"Passed nonop_delay_msecs: %lu, exceeds"
1849 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
1875 pr_err(
"Unable to extract trans_delay_msecs\n");
1879 pr_err(
"Passed trans_delay_msecs: %lu, exceeds"
1880 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
1906 pr_err(
"Unable to extract implict_trans_secs\n");
1910 pr_err(
"Passed implict_trans_secs: %lu, exceeds"
1911 " ALUA_MAX_IMPLICT_TRANS_SECS: %d\n", tmp,
1937 pr_err(
"Unable to extract preferred ALUA value\n");
1940 if ((tmp != 0) && (tmp != 1)) {
1941 pr_err(
"Illegal value for preferred ALUA: %lu\n", tmp);
1972 pr_err(
"Unable to extract alua_tg_pt_offline value\n");
1975 if ((tmp != 0) && (tmp != 1)) {
1976 pr_err(
"Illegal value for alua_tg_pt_offline: %lu\n",
1980 tg_pt_gp_mem = lun->
lun_sep->sep_alua_tg_pt_gp_mem;
1981 if (!tg_pt_gp_mem) {
1982 pr_err(
"Unable to locate *tg_pt_gp_mem\n");
1986 ret = core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem,
1998 return sprintf(page,
"%d\n", lun->
lun_sep->sep_tg_pt_secondary_stat);
2011 pr_err(
"Unable to extract alua_tg_pt_status\n");
2017 pr_err(
"Illegal value for alua_tg_pt_status: %lu\n",
2021 lun->
lun_sep->sep_tg_pt_secondary_stat = (
int)tmp;
2031 lun->
lun_sep->sep_tg_pt_secondary_write_md);
2044 pr_err(
"Unable to extract alua_tg_pt_write_md\n");
2047 if ((tmp != 0) && (tmp != 1)) {
2048 pr_err(
"Illegal value for alua_tg_pt_write_md:"
2052 lun->
lun_sep->sep_tg_pt_secondary_write_md = (
int)tmp;
2069 !(dev->
se_sub_dev->se_dev_attrib.emulate_alua)) || force_pt) {
2072 pr_debug(
"%s: Using SPC_ALUA_PASSTHROUGH, no ALUA"
2081 pr_debug(
"%s: Enabling ALUA Emulation for SPC-3"
2087 lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
2088 if (IS_ERR(lu_gp_mem))
2089 return PTR_ERR(lu_gp_mem);
2098 pr_debug(
"%s: Adding to default ALUA LU Group:"
2099 " core/alua/lu_gps/default_lu_gp\n",
2104 pr_debug(
"%s: Disabling ALUA Emulation for SPC-2"