Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Filename: target_core_alua.c |
| 3 | * |
| 4 | * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA) |
| 5 | * |
Nicholas Bellinger | 4c76251 | 2013-09-05 15:29:12 -0700 | [diff] [blame] | 6 | * (c) Copyright 2009-2013 Datera, Inc. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 7 | * |
| 8 | * Nicholas A. Bellinger <nab@kernel.org> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 23 | * |
| 24 | ******************************************************************************/ |
| 25 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 26 | #include <linux/slab.h> |
| 27 | #include <linux/spinlock.h> |
| 28 | #include <linux/configfs.h> |
Paul Gortmaker | c53181a | 2011-08-30 18:16:43 -0400 | [diff] [blame] | 29 | #include <linux/export.h> |
Al Viro | 0e9b10a | 2013-02-23 15:22:43 -0500 | [diff] [blame] | 30 | #include <linux/file.h> |
Bart Van Assche | ba92999 | 2015-05-08 10:11:12 +0200 | [diff] [blame] | 31 | #include <scsi/scsi_proto.h> |
Roland Dreier | 33395fb | 2012-02-23 17:22:12 -0800 | [diff] [blame] | 32 | #include <asm/unaligned.h> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 33 | |
| 34 | #include <target/target_core_base.h> |
Christoph Hellwig | c4795fb | 2011-11-16 09:46:48 -0500 | [diff] [blame] | 35 | #include <target/target_core_backend.h> |
| 36 | #include <target/target_core_fabric.h> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 37 | |
Christoph Hellwig | e26d99a | 2011-11-14 12:30:30 -0500 | [diff] [blame] | 38 | #include "target_core_internal.h" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 39 | #include "target_core_alua.h" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 40 | #include "target_core_ua.h" |
| 41 | |
Hannes Reinecke | bb91c1a | 2013-12-17 09:18:43 +0100 | [diff] [blame] | 42 | static sense_reason_t core_alua_check_transition(int state, int valid, |
| 43 | int *primary); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 44 | static int core_alua_set_tg_pt_secondary_state( |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 45 | struct se_lun *lun, int explicit, int offline); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 46 | |
Hannes Reinecke | bb91c1a | 2013-12-17 09:18:43 +0100 | [diff] [blame] | 47 | static char *core_alua_dump_state(int state); |
| 48 | |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 49 | static void __target_attach_tg_pt_gp(struct se_lun *lun, |
| 50 | struct t10_alua_tg_pt_gp *tg_pt_gp); |
| 51 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 52 | static u16 alua_lu_gps_counter; |
| 53 | static u32 alua_lu_gps_count; |
| 54 | |
| 55 | static DEFINE_SPINLOCK(lu_gps_lock); |
| 56 | static LIST_HEAD(lu_gps_list); |
| 57 | |
| 58 | struct t10_alua_lu_gp *default_lu_gp; |
| 59 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 60 | /* |
Hannes Reinecke | c66094b | 2013-12-17 09:18:49 +0100 | [diff] [blame] | 61 | * REPORT REFERRALS |
| 62 | * |
| 63 | * See sbc3r35 section 5.23 |
| 64 | */ |
| 65 | sense_reason_t |
| 66 | target_emulate_report_referrals(struct se_cmd *cmd) |
| 67 | { |
| 68 | struct se_device *dev = cmd->se_dev; |
| 69 | struct t10_alua_lba_map *map; |
| 70 | struct t10_alua_lba_map_member *map_mem; |
| 71 | unsigned char *buf; |
| 72 | u32 rd_len = 0, off; |
| 73 | |
| 74 | if (cmd->data_length < 4) { |
| 75 | pr_warn("REPORT REFERRALS allocation length %u too" |
| 76 | " small\n", cmd->data_length); |
| 77 | return TCM_INVALID_CDB_FIELD; |
| 78 | } |
| 79 | |
| 80 | buf = transport_kmap_data_sg(cmd); |
| 81 | if (!buf) |
| 82 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 83 | |
| 84 | off = 4; |
| 85 | spin_lock(&dev->t10_alua.lba_map_lock); |
| 86 | if (list_empty(&dev->t10_alua.lba_map_list)) { |
| 87 | spin_unlock(&dev->t10_alua.lba_map_lock); |
| 88 | transport_kunmap_data_sg(cmd); |
| 89 | |
| 90 | return TCM_UNSUPPORTED_SCSI_OPCODE; |
| 91 | } |
| 92 | |
| 93 | list_for_each_entry(map, &dev->t10_alua.lba_map_list, |
| 94 | lba_map_list) { |
| 95 | int desc_num = off + 3; |
| 96 | int pg_num; |
| 97 | |
| 98 | off += 4; |
Hannes Reinecke | 38edd72 | 2013-12-19 14:36:11 +0100 | [diff] [blame] | 99 | if (cmd->data_length > off) |
| 100 | put_unaligned_be64(map->lba_map_first_lba, &buf[off]); |
Hannes Reinecke | c66094b | 2013-12-17 09:18:49 +0100 | [diff] [blame] | 101 | off += 8; |
Hannes Reinecke | 38edd72 | 2013-12-19 14:36:11 +0100 | [diff] [blame] | 102 | if (cmd->data_length > off) |
| 103 | put_unaligned_be64(map->lba_map_last_lba, &buf[off]); |
Hannes Reinecke | c66094b | 2013-12-17 09:18:49 +0100 | [diff] [blame] | 104 | off += 8; |
| 105 | rd_len += 20; |
| 106 | pg_num = 0; |
| 107 | list_for_each_entry(map_mem, &map->lba_map_mem_list, |
| 108 | lba_map_mem_list) { |
Hannes Reinecke | 38edd72 | 2013-12-19 14:36:11 +0100 | [diff] [blame] | 109 | int alua_state = map_mem->lba_map_mem_alua_state; |
| 110 | int alua_pg_id = map_mem->lba_map_mem_alua_pg_id; |
| 111 | |
| 112 | if (cmd->data_length > off) |
| 113 | buf[off] = alua_state & 0x0f; |
| 114 | off += 2; |
| 115 | if (cmd->data_length > off) |
| 116 | buf[off] = (alua_pg_id >> 8) & 0xff; |
Hannes Reinecke | c66094b | 2013-12-17 09:18:49 +0100 | [diff] [blame] | 117 | off++; |
Hannes Reinecke | 38edd72 | 2013-12-19 14:36:11 +0100 | [diff] [blame] | 118 | if (cmd->data_length > off) |
| 119 | buf[off] = (alua_pg_id & 0xff); |
| 120 | off++; |
Hannes Reinecke | c66094b | 2013-12-17 09:18:49 +0100 | [diff] [blame] | 121 | rd_len += 4; |
| 122 | pg_num++; |
| 123 | } |
Hannes Reinecke | 38edd72 | 2013-12-19 14:36:11 +0100 | [diff] [blame] | 124 | if (cmd->data_length > desc_num) |
| 125 | buf[desc_num] = pg_num; |
Hannes Reinecke | c66094b | 2013-12-17 09:18:49 +0100 | [diff] [blame] | 126 | } |
| 127 | spin_unlock(&dev->t10_alua.lba_map_lock); |
| 128 | |
| 129 | /* |
| 130 | * Set the RETURN DATA LENGTH set in the header of the DataIN Payload |
| 131 | */ |
| 132 | put_unaligned_be16(rd_len, &buf[2]); |
| 133 | |
| 134 | transport_kunmap_data_sg(cmd); |
| 135 | |
| 136 | target_complete_cmd(cmd, GOOD); |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | /* |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 141 | * REPORT_TARGET_PORT_GROUPS |
| 142 | * |
| 143 | * See spc4r17 section 6.27 |
| 144 | */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 145 | sense_reason_t |
| 146 | target_emulate_report_target_port_groups(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 147 | { |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 148 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 149 | struct t10_alua_tg_pt_gp *tg_pt_gp; |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 150 | struct se_lun *lun; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 151 | unsigned char *buf; |
Nicholas Bellinger | 5b9a4d7 | 2012-05-16 22:02:34 -0700 | [diff] [blame] | 152 | u32 rd_len = 0, off; |
| 153 | int ext_hdr = (cmd->t_task_cdb[1] & 0x20); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 154 | |
Nicholas Bellinger | 6b20fa9 | 2011-10-18 23:48:04 -0700 | [diff] [blame] | 155 | /* |
Nicholas Bellinger | 5b9a4d7 | 2012-05-16 22:02:34 -0700 | [diff] [blame] | 156 | * Skip over RESERVED area to first Target port group descriptor |
| 157 | * depending on the PARAMETER DATA FORMAT type.. |
Nicholas Bellinger | 6b20fa9 | 2011-10-18 23:48:04 -0700 | [diff] [blame] | 158 | */ |
Nicholas Bellinger | 5b9a4d7 | 2012-05-16 22:02:34 -0700 | [diff] [blame] | 159 | if (ext_hdr != 0) |
| 160 | off = 8; |
| 161 | else |
| 162 | off = 4; |
| 163 | |
| 164 | if (cmd->data_length < off) { |
| 165 | pr_warn("REPORT TARGET PORT GROUPS allocation length %u too" |
| 166 | " small for %s header\n", cmd->data_length, |
| 167 | (ext_hdr) ? "extended" : "normal"); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 168 | return TCM_INVALID_CDB_FIELD; |
Nicholas Bellinger | 6b20fa9 | 2011-10-18 23:48:04 -0700 | [diff] [blame] | 169 | } |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 170 | buf = transport_kmap_data_sg(cmd); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 171 | if (!buf) |
| 172 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 173 | |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 174 | spin_lock(&dev->t10_alua.tg_pt_gps_lock); |
| 175 | list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 176 | tg_pt_gp_list) { |
| 177 | /* |
Nicholas Bellinger | 6b20fa9 | 2011-10-18 23:48:04 -0700 | [diff] [blame] | 178 | * Check if the Target port group and Target port descriptor list |
| 179 | * based on tg_pt_gp_members count will fit into the response payload. |
| 180 | * Otherwise, bump rd_len to let the initiator know we have exceeded |
| 181 | * the allocation length and the response is truncated. |
| 182 | */ |
| 183 | if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) > |
| 184 | cmd->data_length) { |
| 185 | rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4); |
| 186 | continue; |
| 187 | } |
| 188 | /* |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 189 | * PREF: Preferred target port bit, determine if this |
| 190 | * bit should be set for port group. |
| 191 | */ |
| 192 | if (tg_pt_gp->tg_pt_gp_pref) |
| 193 | buf[off] = 0x80; |
| 194 | /* |
| 195 | * Set the ASYMMETRIC ACCESS State |
| 196 | */ |
| 197 | buf[off++] |= (atomic_read( |
| 198 | &tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff); |
| 199 | /* |
| 200 | * Set supported ASYMMETRIC ACCESS State bits |
| 201 | */ |
Hannes Reinecke | c0dc941 | 2013-11-19 09:07:49 +0100 | [diff] [blame] | 202 | buf[off++] |= tg_pt_gp->tg_pt_gp_alua_supported_states; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 203 | /* |
| 204 | * TARGET PORT GROUP |
| 205 | */ |
| 206 | buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff); |
| 207 | buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff); |
| 208 | |
| 209 | off++; /* Skip over Reserved */ |
| 210 | /* |
| 211 | * STATUS CODE |
| 212 | */ |
| 213 | buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff); |
| 214 | /* |
| 215 | * Vendor Specific field |
| 216 | */ |
| 217 | buf[off++] = 0x00; |
| 218 | /* |
| 219 | * TARGET PORT COUNT |
| 220 | */ |
| 221 | buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff); |
| 222 | rd_len += 8; |
| 223 | |
| 224 | spin_lock(&tg_pt_gp->tg_pt_gp_lock); |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 225 | list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list, |
| 226 | lun_tg_pt_gp_link) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 227 | /* |
| 228 | * Start Target Port descriptor format |
| 229 | * |
| 230 | * See spc4r17 section 6.2.7 Table 247 |
| 231 | */ |
| 232 | off += 2; /* Skip over Obsolete */ |
| 233 | /* |
| 234 | * Set RELATIVE TARGET PORT IDENTIFIER |
| 235 | */ |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 236 | buf[off++] = ((lun->lun_rtpi >> 8) & 0xff); |
| 237 | buf[off++] = (lun->lun_rtpi & 0xff); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 238 | rd_len += 4; |
| 239 | } |
| 240 | spin_unlock(&tg_pt_gp->tg_pt_gp_lock); |
| 241 | } |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 242 | spin_unlock(&dev->t10_alua.tg_pt_gps_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 243 | /* |
| 244 | * Set the RETURN DATA LENGTH set in the header of the DataIN Payload |
| 245 | */ |
Nicholas Bellinger | 5b9a4d7 | 2012-05-16 22:02:34 -0700 | [diff] [blame] | 246 | put_unaligned_be32(rd_len, &buf[0]); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 247 | |
Nicholas Bellinger | 5b9a4d7 | 2012-05-16 22:02:34 -0700 | [diff] [blame] | 248 | /* |
| 249 | * Fill in the Extended header parameter data format if requested |
| 250 | */ |
| 251 | if (ext_hdr != 0) { |
| 252 | buf[4] = 0x10; |
| 253 | /* |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 254 | * Set the implicit transition time (in seconds) for the application |
Nicholas Bellinger | 5b9a4d7 | 2012-05-16 22:02:34 -0700 | [diff] [blame] | 255 | * client to use as a base for it's transition timeout value. |
| 256 | * |
| 257 | * Use the current tg_pt_gp_mem -> tg_pt_gp membership from the LUN |
| 258 | * this CDB was received upon to determine this value individually |
| 259 | * for ALUA target port group. |
| 260 | */ |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 261 | spin_lock(&cmd->se_lun->lun_tg_pt_gp_lock); |
| 262 | tg_pt_gp = cmd->se_lun->lun_tg_pt_gp; |
| 263 | if (tg_pt_gp) |
| 264 | buf[5] = tg_pt_gp->tg_pt_gp_implicit_trans_secs; |
| 265 | spin_unlock(&cmd->se_lun->lun_tg_pt_gp_lock); |
Nicholas Bellinger | 5b9a4d7 | 2012-05-16 22:02:34 -0700 | [diff] [blame] | 266 | } |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 267 | transport_kunmap_data_sg(cmd); |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 268 | |
Christoph Hellwig | 6bb35e0 | 2012-04-23 11:35:33 -0400 | [diff] [blame] | 269 | target_complete_cmd(cmd, GOOD); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | /* |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 274 | * SET_TARGET_PORT_GROUPS for explicit ALUA operation. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 275 | * |
| 276 | * See spc4r17 section 6.35 |
| 277 | */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 278 | sense_reason_t |
| 279 | target_emulate_set_target_port_groups(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 280 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 281 | struct se_device *dev = cmd->se_dev; |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 282 | struct se_lun *l_lun = cmd->se_lun; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 283 | struct se_node_acl *nacl = cmd->se_sess->se_node_acl; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 284 | struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 285 | unsigned char *buf; |
| 286 | unsigned char *ptr; |
Hannes Reinecke | a0d50f6 | 2012-12-17 09:53:34 +0100 | [diff] [blame] | 287 | sense_reason_t rc = TCM_NO_SENSE; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 288 | u32 len = 4; /* Skip over RESERVED area in header */ |
Hannes Reinecke | bb91c1a | 2013-12-17 09:18:43 +0100 | [diff] [blame] | 289 | int alua_access_state, primary = 0, valid_states; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 290 | u16 tg_pt_id, rtpi; |
| 291 | |
Paolo Bonzini | 0d7f129 | 2012-09-07 17:30:33 +0200 | [diff] [blame] | 292 | if (cmd->data_length < 4) { |
| 293 | pr_warn("SET TARGET PORT GROUPS parameter list length %u too" |
| 294 | " small\n", cmd->data_length); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 295 | return TCM_INVALID_PARAMETER_LIST; |
Paolo Bonzini | 0d7f129 | 2012-09-07 17:30:33 +0200 | [diff] [blame] | 296 | } |
| 297 | |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 298 | buf = transport_kmap_data_sg(cmd); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 299 | if (!buf) |
| 300 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 301 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 302 | /* |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 303 | * Determine if explicit ALUA via SET_TARGET_PORT_GROUPS is allowed |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 304 | * for the local tg_pt_gp. |
| 305 | */ |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 306 | spin_lock(&l_lun->lun_tg_pt_gp_lock); |
| 307 | l_tg_pt_gp = l_lun->lun_tg_pt_gp; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 308 | if (!l_tg_pt_gp) { |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 309 | spin_unlock(&l_lun->lun_tg_pt_gp_lock); |
| 310 | pr_err("Unable to access l_lun->tg_pt_gp\n"); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 311 | rc = TCM_UNSUPPORTED_SCSI_OPCODE; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 312 | goto out; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 313 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 314 | |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 315 | if (!(l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)) { |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 316 | spin_unlock(&l_lun->lun_tg_pt_gp_lock); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 317 | pr_debug("Unable to process SET_TARGET_PORT_GROUPS" |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 318 | " while TPGS_EXPLICIT_ALUA is disabled\n"); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 319 | rc = TCM_UNSUPPORTED_SCSI_OPCODE; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 320 | goto out; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 321 | } |
Hannes Reinecke | bb91c1a | 2013-12-17 09:18:43 +0100 | [diff] [blame] | 322 | valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states; |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 323 | spin_unlock(&l_lun->lun_tg_pt_gp_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 324 | |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 325 | ptr = &buf[4]; /* Skip over RESERVED area in header */ |
| 326 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 327 | while (len < cmd->data_length) { |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 328 | bool found = false; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 329 | alua_access_state = (ptr[0] & 0x0f); |
| 330 | /* |
| 331 | * Check the received ALUA access state, and determine if |
| 332 | * the state is a primary or secondary target port asymmetric |
| 333 | * access state. |
| 334 | */ |
Hannes Reinecke | bb91c1a | 2013-12-17 09:18:43 +0100 | [diff] [blame] | 335 | rc = core_alua_check_transition(alua_access_state, |
| 336 | valid_states, &primary); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 337 | if (rc) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 338 | /* |
| 339 | * If the SET TARGET PORT GROUPS attempts to establish |
| 340 | * an invalid combination of target port asymmetric |
| 341 | * access states or attempts to establish an |
| 342 | * unsupported target port asymmetric access state, |
| 343 | * then the command shall be terminated with CHECK |
| 344 | * CONDITION status, with the sense key set to ILLEGAL |
| 345 | * REQUEST, and the additional sense code set to INVALID |
| 346 | * FIELD IN PARAMETER LIST. |
| 347 | */ |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 348 | goto out; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 349 | } |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 350 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 351 | /* |
| 352 | * If the ASYMMETRIC ACCESS STATE field (see table 267) |
| 353 | * specifies a primary target port asymmetric access state, |
| 354 | * then the TARGET PORT GROUP OR TARGET PORT field specifies |
| 355 | * a primary target port group for which the primary target |
| 356 | * port asymmetric access state shall be changed. If the |
| 357 | * ASYMMETRIC ACCESS STATE field specifies a secondary target |
| 358 | * port asymmetric access state, then the TARGET PORT GROUP OR |
| 359 | * TARGET PORT field specifies the relative target port |
| 360 | * identifier (see 3.1.120) of the target port for which the |
| 361 | * secondary target port asymmetric access state shall be |
| 362 | * changed. |
| 363 | */ |
| 364 | if (primary) { |
Roland Dreier | 33395fb | 2012-02-23 17:22:12 -0800 | [diff] [blame] | 365 | tg_pt_id = get_unaligned_be16(ptr + 2); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 366 | /* |
| 367 | * Locate the matching target port group ID from |
| 368 | * the global tg_pt_gp list |
| 369 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 370 | spin_lock(&dev->t10_alua.tg_pt_gps_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 371 | list_for_each_entry(tg_pt_gp, |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 372 | &dev->t10_alua.tg_pt_gps_list, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 373 | tg_pt_gp_list) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 374 | if (!tg_pt_gp->tg_pt_gp_valid_id) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 375 | continue; |
| 376 | |
| 377 | if (tg_pt_id != tg_pt_gp->tg_pt_gp_id) |
| 378 | continue; |
| 379 | |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 380 | atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 381 | |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 382 | spin_unlock(&dev->t10_alua.tg_pt_gps_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 383 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 384 | if (!core_alua_do_port_transition(tg_pt_gp, |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 385 | dev, l_lun, nacl, |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 386 | alua_access_state, 1)) |
| 387 | found = true; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 388 | |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 389 | spin_lock(&dev->t10_alua.tg_pt_gps_lock); |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 390 | atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 391 | break; |
| 392 | } |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 393 | spin_unlock(&dev->t10_alua.tg_pt_gps_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 394 | } else { |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 395 | struct se_lun *lun; |
| 396 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 397 | /* |
Hannes Reinecke | f1ae05d | 2013-11-19 09:07:47 +0100 | [diff] [blame] | 398 | * Extract the RELATIVE TARGET PORT IDENTIFIER to identify |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 399 | * the Target Port in question for the the incoming |
| 400 | * SET_TARGET_PORT_GROUPS op. |
| 401 | */ |
Roland Dreier | 33395fb | 2012-02-23 17:22:12 -0800 | [diff] [blame] | 402 | rtpi = get_unaligned_be16(ptr + 2); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 403 | /* |
Masanari Iida | 35d1efe | 2012-08-16 22:43:13 +0900 | [diff] [blame] | 404 | * Locate the matching relative target port identifier |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 405 | * for the struct se_device storage object. |
| 406 | */ |
| 407 | spin_lock(&dev->se_port_lock); |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 408 | list_for_each_entry(lun, &dev->dev_sep_list, |
| 409 | lun_dev_link) { |
| 410 | if (lun->lun_rtpi != rtpi) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 411 | continue; |
| 412 | |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 413 | // XXX: racy unlock |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 414 | spin_unlock(&dev->se_port_lock); |
| 415 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 416 | if (!core_alua_set_tg_pt_secondary_state( |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 417 | lun, 1, 1)) |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 418 | found = true; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 419 | |
| 420 | spin_lock(&dev->se_port_lock); |
| 421 | break; |
| 422 | } |
| 423 | spin_unlock(&dev->se_port_lock); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | if (!found) { |
| 427 | rc = TCM_INVALID_PARAMETER_LIST; |
| 428 | goto out; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | ptr += 4; |
| 432 | len += 4; |
| 433 | } |
| 434 | |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 435 | out: |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 436 | transport_kunmap_data_sg(cmd); |
Roland Dreier | 59e4f54 | 2012-06-04 23:24:51 -0700 | [diff] [blame] | 437 | if (!rc) |
| 438 | target_complete_cmd(cmd, GOOD); |
| 439 | return rc; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 440 | } |
| 441 | |
Paul Bolle | 1e3ab99 | 2014-02-20 09:07:52 +0100 | [diff] [blame] | 442 | static inline void set_ascq(struct se_cmd *cmd, u8 alua_ascq) |
| 443 | { |
| 444 | /* |
| 445 | * Set SCSI additional sense code (ASC) to 'LUN Not Accessible'; |
| 446 | * The ALUA additional sense code qualifier (ASCQ) is determined |
| 447 | * by the ALUA primary or secondary access state.. |
| 448 | */ |
| 449 | pr_debug("[%s]: ALUA TG Port not available, " |
| 450 | "SenseKey: NOT_READY, ASC/ASCQ: " |
| 451 | "0x04/0x%02x\n", |
| 452 | cmd->se_tfo->get_fabric_name(), alua_ascq); |
| 453 | |
| 454 | cmd->scsi_asc = 0x04; |
| 455 | cmd->scsi_ascq = alua_ascq; |
| 456 | } |
| 457 | |
| 458 | static inline void core_alua_state_nonoptimized( |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 459 | struct se_cmd *cmd, |
| 460 | unsigned char *cdb, |
Paul Bolle | 1e3ab99 | 2014-02-20 09:07:52 +0100 | [diff] [blame] | 461 | int nonop_delay_msecs) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 462 | { |
| 463 | /* |
| 464 | * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked |
| 465 | * later to determine if processing of this cmd needs to be |
| 466 | * temporarily delayed for the Active/NonOptimized primary access state. |
| 467 | */ |
| 468 | cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED; |
| 469 | cmd->alua_nonop_delay = nonop_delay_msecs; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 470 | } |
| 471 | |
Hannes Reinecke | c66094b | 2013-12-17 09:18:49 +0100 | [diff] [blame] | 472 | static inline int core_alua_state_lba_dependent( |
| 473 | struct se_cmd *cmd, |
Paul Bolle | 1e3ab99 | 2014-02-20 09:07:52 +0100 | [diff] [blame] | 474 | struct t10_alua_tg_pt_gp *tg_pt_gp) |
Hannes Reinecke | c66094b | 2013-12-17 09:18:49 +0100 | [diff] [blame] | 475 | { |
| 476 | struct se_device *dev = cmd->se_dev; |
| 477 | u64 segment_size, segment_mult, sectors, lba; |
| 478 | |
| 479 | /* Only need to check for cdb actually containing LBAs */ |
| 480 | if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB)) |
| 481 | return 0; |
| 482 | |
| 483 | spin_lock(&dev->t10_alua.lba_map_lock); |
| 484 | segment_size = dev->t10_alua.lba_map_segment_size; |
| 485 | segment_mult = dev->t10_alua.lba_map_segment_multiplier; |
| 486 | sectors = cmd->data_length / dev->dev_attrib.block_size; |
| 487 | |
| 488 | lba = cmd->t_task_lba; |
| 489 | while (lba < cmd->t_task_lba + sectors) { |
| 490 | struct t10_alua_lba_map *cur_map = NULL, *map; |
| 491 | struct t10_alua_lba_map_member *map_mem; |
| 492 | |
| 493 | list_for_each_entry(map, &dev->t10_alua.lba_map_list, |
| 494 | lba_map_list) { |
| 495 | u64 start_lba, last_lba; |
| 496 | u64 first_lba = map->lba_map_first_lba; |
| 497 | |
| 498 | if (segment_mult) { |
| 499 | u64 tmp = lba; |
Nicholas Bellinger | cdf5594 | 2014-01-30 14:05:16 -0800 | [diff] [blame] | 500 | start_lba = do_div(tmp, segment_size * segment_mult); |
Hannes Reinecke | c66094b | 2013-12-17 09:18:49 +0100 | [diff] [blame] | 501 | |
| 502 | last_lba = first_lba + segment_size - 1; |
| 503 | if (start_lba >= first_lba && |
| 504 | start_lba <= last_lba) { |
| 505 | lba += segment_size; |
| 506 | cur_map = map; |
| 507 | break; |
| 508 | } |
| 509 | } else { |
| 510 | last_lba = map->lba_map_last_lba; |
| 511 | if (lba >= first_lba && lba <= last_lba) { |
| 512 | lba = last_lba + 1; |
| 513 | cur_map = map; |
| 514 | break; |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | if (!cur_map) { |
| 519 | spin_unlock(&dev->t10_alua.lba_map_lock); |
Paul Bolle | 1e3ab99 | 2014-02-20 09:07:52 +0100 | [diff] [blame] | 520 | set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE); |
Hannes Reinecke | c66094b | 2013-12-17 09:18:49 +0100 | [diff] [blame] | 521 | return 1; |
| 522 | } |
| 523 | list_for_each_entry(map_mem, &cur_map->lba_map_mem_list, |
| 524 | lba_map_mem_list) { |
| 525 | if (map_mem->lba_map_mem_alua_pg_id != |
| 526 | tg_pt_gp->tg_pt_gp_id) |
| 527 | continue; |
| 528 | switch(map_mem->lba_map_mem_alua_state) { |
| 529 | case ALUA_ACCESS_STATE_STANDBY: |
| 530 | spin_unlock(&dev->t10_alua.lba_map_lock); |
Paul Bolle | 1e3ab99 | 2014-02-20 09:07:52 +0100 | [diff] [blame] | 531 | set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY); |
Hannes Reinecke | c66094b | 2013-12-17 09:18:49 +0100 | [diff] [blame] | 532 | return 1; |
| 533 | case ALUA_ACCESS_STATE_UNAVAILABLE: |
| 534 | spin_unlock(&dev->t10_alua.lba_map_lock); |
Paul Bolle | 1e3ab99 | 2014-02-20 09:07:52 +0100 | [diff] [blame] | 535 | set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE); |
Hannes Reinecke | c66094b | 2013-12-17 09:18:49 +0100 | [diff] [blame] | 536 | return 1; |
| 537 | default: |
| 538 | break; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | spin_unlock(&dev->t10_alua.lba_map_lock); |
| 543 | return 0; |
| 544 | } |
| 545 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 546 | static inline int core_alua_state_standby( |
| 547 | struct se_cmd *cmd, |
Paul Bolle | 1e3ab99 | 2014-02-20 09:07:52 +0100 | [diff] [blame] | 548 | unsigned char *cdb) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 549 | { |
| 550 | /* |
| 551 | * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by |
| 552 | * spc4r17 section 5.9.2.4.4 |
| 553 | */ |
| 554 | switch (cdb[0]) { |
| 555 | case INQUIRY: |
| 556 | case LOG_SELECT: |
| 557 | case LOG_SENSE: |
| 558 | case MODE_SELECT: |
| 559 | case MODE_SENSE: |
| 560 | case REPORT_LUNS: |
| 561 | case RECEIVE_DIAGNOSTIC: |
| 562 | case SEND_DIAGNOSTIC: |
Nicholas Bellinger | e7810c2 | 2014-06-05 23:37:00 -0700 | [diff] [blame] | 563 | case READ_CAPACITY: |
Nicholas Bellinger | 30f359a | 2013-04-10 15:00:27 -0700 | [diff] [blame] | 564 | return 0; |
Hannes Reinecke | eb846d9 | 2014-11-17 14:25:19 +0100 | [diff] [blame] | 565 | case SERVICE_ACTION_IN_16: |
Nicholas Bellinger | e7810c2 | 2014-06-05 23:37:00 -0700 | [diff] [blame] | 566 | switch (cdb[1] & 0x1f) { |
| 567 | case SAI_READ_CAPACITY_16: |
| 568 | return 0; |
| 569 | default: |
| 570 | set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY); |
| 571 | return 1; |
| 572 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 573 | case MAINTENANCE_IN: |
Nicholas Bellinger | ba53974 | 2012-05-16 21:52:10 -0700 | [diff] [blame] | 574 | switch (cdb[1] & 0x1f) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 575 | case MI_REPORT_TARGET_PGS: |
| 576 | return 0; |
| 577 | default: |
Paul Bolle | 1e3ab99 | 2014-02-20 09:07:52 +0100 | [diff] [blame] | 578 | set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 579 | return 1; |
| 580 | } |
| 581 | case MAINTENANCE_OUT: |
| 582 | switch (cdb[1]) { |
| 583 | case MO_SET_TARGET_PGS: |
| 584 | return 0; |
| 585 | default: |
Paul Bolle | 1e3ab99 | 2014-02-20 09:07:52 +0100 | [diff] [blame] | 586 | set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 587 | return 1; |
| 588 | } |
| 589 | case REQUEST_SENSE: |
| 590 | case PERSISTENT_RESERVE_IN: |
| 591 | case PERSISTENT_RESERVE_OUT: |
| 592 | case READ_BUFFER: |
| 593 | case WRITE_BUFFER: |
| 594 | return 0; |
| 595 | default: |
Paul Bolle | 1e3ab99 | 2014-02-20 09:07:52 +0100 | [diff] [blame] | 596 | set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 597 | return 1; |
| 598 | } |
| 599 | |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | static inline int core_alua_state_unavailable( |
| 604 | struct se_cmd *cmd, |
Paul Bolle | 1e3ab99 | 2014-02-20 09:07:52 +0100 | [diff] [blame] | 605 | unsigned char *cdb) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 606 | { |
| 607 | /* |
| 608 | * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by |
| 609 | * spc4r17 section 5.9.2.4.5 |
| 610 | */ |
| 611 | switch (cdb[0]) { |
| 612 | case INQUIRY: |
| 613 | case REPORT_LUNS: |
Nicholas Bellinger | 30f359a | 2013-04-10 15:00:27 -0700 | [diff] [blame] | 614 | return 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 615 | case MAINTENANCE_IN: |
Nicholas Bellinger | ba53974 | 2012-05-16 21:52:10 -0700 | [diff] [blame] | 616 | switch (cdb[1] & 0x1f) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 617 | case MI_REPORT_TARGET_PGS: |
| 618 | return 0; |
| 619 | default: |
Paul Bolle | 1e3ab99 | 2014-02-20 09:07:52 +0100 | [diff] [blame] | 620 | set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 621 | return 1; |
| 622 | } |
| 623 | case MAINTENANCE_OUT: |
| 624 | switch (cdb[1]) { |
| 625 | case MO_SET_TARGET_PGS: |
| 626 | return 0; |
| 627 | default: |
Paul Bolle | 1e3ab99 | 2014-02-20 09:07:52 +0100 | [diff] [blame] | 628 | set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 629 | return 1; |
| 630 | } |
| 631 | case REQUEST_SENSE: |
| 632 | case READ_BUFFER: |
| 633 | case WRITE_BUFFER: |
| 634 | return 0; |
| 635 | default: |
Paul Bolle | 1e3ab99 | 2014-02-20 09:07:52 +0100 | [diff] [blame] | 636 | set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 637 | return 1; |
| 638 | } |
| 639 | |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | static inline int core_alua_state_transition( |
| 644 | struct se_cmd *cmd, |
Paul Bolle | 1e3ab99 | 2014-02-20 09:07:52 +0100 | [diff] [blame] | 645 | unsigned char *cdb) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 646 | { |
| 647 | /* |
Hannes Reinecke | f1ae05d | 2013-11-19 09:07:47 +0100 | [diff] [blame] | 648 | * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITION as defined by |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 649 | * spc4r17 section 5.9.2.5 |
| 650 | */ |
| 651 | switch (cdb[0]) { |
| 652 | case INQUIRY: |
| 653 | case REPORT_LUNS: |
Nicholas Bellinger | 30f359a | 2013-04-10 15:00:27 -0700 | [diff] [blame] | 654 | return 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 655 | case MAINTENANCE_IN: |
Nicholas Bellinger | ba53974 | 2012-05-16 21:52:10 -0700 | [diff] [blame] | 656 | switch (cdb[1] & 0x1f) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 657 | case MI_REPORT_TARGET_PGS: |
| 658 | return 0; |
| 659 | default: |
Paul Bolle | 1e3ab99 | 2014-02-20 09:07:52 +0100 | [diff] [blame] | 660 | set_ascq(cmd, ASCQ_04H_ALUA_STATE_TRANSITION); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 661 | return 1; |
| 662 | } |
| 663 | case REQUEST_SENSE: |
| 664 | case READ_BUFFER: |
| 665 | case WRITE_BUFFER: |
| 666 | return 0; |
| 667 | default: |
Paul Bolle | 1e3ab99 | 2014-02-20 09:07:52 +0100 | [diff] [blame] | 668 | set_ascq(cmd, ASCQ_04H_ALUA_STATE_TRANSITION); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 669 | return 1; |
| 670 | } |
| 671 | |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | /* |
Hannes Reinecke | f1ae05d | 2013-11-19 09:07:47 +0100 | [diff] [blame] | 676 | * return 1: Is used to signal LUN not accessible, and check condition/not ready |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 677 | * return 0: Used to signal success |
Hannes Reinecke | f1ae05d | 2013-11-19 09:07:47 +0100 | [diff] [blame] | 678 | * return -1: Used to signal failure, and invalid cdb field |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 679 | */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 680 | sense_reason_t |
| 681 | target_alua_state_check(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 682 | { |
Christoph Hellwig | c87fbd5 | 2012-10-10 17:37:16 -0400 | [diff] [blame] | 683 | struct se_device *dev = cmd->se_dev; |
| 684 | unsigned char *cdb = cmd->t_task_cdb; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 685 | struct se_lun *lun = cmd->se_lun; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 686 | struct t10_alua_tg_pt_gp *tg_pt_gp; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 687 | int out_alua_state, nonop_delay_msecs; |
Christoph Hellwig | c87fbd5 | 2012-10-10 17:37:16 -0400 | [diff] [blame] | 688 | |
| 689 | if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE) |
| 690 | return 0; |
Andy Grover | a354170 | 2015-05-19 14:44:41 -0700 | [diff] [blame] | 691 | if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH) |
Christoph Hellwig | c87fbd5 | 2012-10-10 17:37:16 -0400 | [diff] [blame] | 692 | return 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 693 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 694 | /* |
| 695 | * First, check for a struct se_port specific secondary ALUA target port |
| 696 | * access state: OFFLINE |
| 697 | */ |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 698 | if (atomic_read(&lun->lun_tg_pt_secondary_offline)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 699 | pr_debug("ALUA: Got secondary offline status for local" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 700 | " target port\n"); |
Paul Bolle | 1e3ab99 | 2014-02-20 09:07:52 +0100 | [diff] [blame] | 701 | set_ascq(cmd, ASCQ_04H_ALUA_OFFLINE); |
| 702 | return TCM_CHECK_CONDITION_NOT_READY; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 703 | } |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 704 | |
| 705 | if (!lun->lun_tg_pt_gp) |
Nicholas Bellinger | 89c12cc | 2013-08-22 15:35:45 -0700 | [diff] [blame] | 706 | return 0; |
| 707 | |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 708 | spin_lock(&lun->lun_tg_pt_gp_lock); |
| 709 | tg_pt_gp = lun->lun_tg_pt_gp; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 710 | out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state); |
| 711 | nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs; |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 712 | |
| 713 | // XXX: keeps using tg_pt_gp witout reference after unlock |
| 714 | spin_unlock(&lun->lun_tg_pt_gp_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 715 | /* |
Hannes Reinecke | 73f3bf5 | 2013-11-19 09:07:48 +0100 | [diff] [blame] | 716 | * Process ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED in a separate conditional |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 717 | * statement so the compiler knows explicitly to check this case first. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 718 | * For the Optimized ALUA access state case, we want to process the |
| 719 | * incoming fabric cmd ASAP.. |
| 720 | */ |
Hannes Reinecke | 73f3bf5 | 2013-11-19 09:07:48 +0100 | [diff] [blame] | 721 | if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 722 | return 0; |
| 723 | |
| 724 | switch (out_alua_state) { |
| 725 | case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED: |
Paul Bolle | 1e3ab99 | 2014-02-20 09:07:52 +0100 | [diff] [blame] | 726 | core_alua_state_nonoptimized(cmd, cdb, nonop_delay_msecs); |
Christoph Hellwig | c87fbd5 | 2012-10-10 17:37:16 -0400 | [diff] [blame] | 727 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 728 | case ALUA_ACCESS_STATE_STANDBY: |
Paul Bolle | 1e3ab99 | 2014-02-20 09:07:52 +0100 | [diff] [blame] | 729 | if (core_alua_state_standby(cmd, cdb)) |
| 730 | return TCM_CHECK_CONDITION_NOT_READY; |
Christoph Hellwig | c87fbd5 | 2012-10-10 17:37:16 -0400 | [diff] [blame] | 731 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 732 | case ALUA_ACCESS_STATE_UNAVAILABLE: |
Paul Bolle | 1e3ab99 | 2014-02-20 09:07:52 +0100 | [diff] [blame] | 733 | if (core_alua_state_unavailable(cmd, cdb)) |
| 734 | return TCM_CHECK_CONDITION_NOT_READY; |
Christoph Hellwig | c87fbd5 | 2012-10-10 17:37:16 -0400 | [diff] [blame] | 735 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 736 | case ALUA_ACCESS_STATE_TRANSITION: |
Paul Bolle | 1e3ab99 | 2014-02-20 09:07:52 +0100 | [diff] [blame] | 737 | if (core_alua_state_transition(cmd, cdb)) |
| 738 | return TCM_CHECK_CONDITION_NOT_READY; |
Christoph Hellwig | c87fbd5 | 2012-10-10 17:37:16 -0400 | [diff] [blame] | 739 | break; |
Hannes Reinecke | c66094b | 2013-12-17 09:18:49 +0100 | [diff] [blame] | 740 | case ALUA_ACCESS_STATE_LBA_DEPENDENT: |
Paul Bolle | 1e3ab99 | 2014-02-20 09:07:52 +0100 | [diff] [blame] | 741 | if (core_alua_state_lba_dependent(cmd, tg_pt_gp)) |
| 742 | return TCM_CHECK_CONDITION_NOT_READY; |
Hannes Reinecke | c66094b | 2013-12-17 09:18:49 +0100 | [diff] [blame] | 743 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 744 | /* |
| 745 | * OFFLINE is a secondary ALUA target port group access state, that is |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 746 | * handled above with struct se_lun->lun_tg_pt_secondary_offline=1 |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 747 | */ |
| 748 | case ALUA_ACCESS_STATE_OFFLINE: |
| 749 | default: |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 750 | pr_err("Unknown ALUA access state: 0x%02x\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 751 | out_alua_state); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 752 | return TCM_INVALID_CDB_FIELD; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 753 | } |
| 754 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 755 | return 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | /* |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 759 | * Check implicit and explicit ALUA state change request. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 760 | */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 761 | static sense_reason_t |
Hannes Reinecke | bb91c1a | 2013-12-17 09:18:43 +0100 | [diff] [blame] | 762 | core_alua_check_transition(int state, int valid, int *primary) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 763 | { |
Hannes Reinecke | bb91c1a | 2013-12-17 09:18:43 +0100 | [diff] [blame] | 764 | /* |
| 765 | * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are |
| 766 | * defined as primary target port asymmetric access states. |
| 767 | */ |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 768 | switch (state) { |
Hannes Reinecke | 73f3bf5 | 2013-11-19 09:07:48 +0100 | [diff] [blame] | 769 | case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED: |
Hannes Reinecke | bb91c1a | 2013-12-17 09:18:43 +0100 | [diff] [blame] | 770 | if (!(valid & ALUA_AO_SUP)) |
| 771 | goto not_supported; |
| 772 | *primary = 1; |
| 773 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 774 | case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED: |
Hannes Reinecke | bb91c1a | 2013-12-17 09:18:43 +0100 | [diff] [blame] | 775 | if (!(valid & ALUA_AN_SUP)) |
| 776 | goto not_supported; |
| 777 | *primary = 1; |
| 778 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 779 | case ALUA_ACCESS_STATE_STANDBY: |
Hannes Reinecke | bb91c1a | 2013-12-17 09:18:43 +0100 | [diff] [blame] | 780 | if (!(valid & ALUA_S_SUP)) |
| 781 | goto not_supported; |
| 782 | *primary = 1; |
| 783 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 784 | case ALUA_ACCESS_STATE_UNAVAILABLE: |
Hannes Reinecke | bb91c1a | 2013-12-17 09:18:43 +0100 | [diff] [blame] | 785 | if (!(valid & ALUA_U_SUP)) |
| 786 | goto not_supported; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 787 | *primary = 1; |
| 788 | break; |
Hannes Reinecke | c66094b | 2013-12-17 09:18:49 +0100 | [diff] [blame] | 789 | case ALUA_ACCESS_STATE_LBA_DEPENDENT: |
| 790 | if (!(valid & ALUA_LBD_SUP)) |
| 791 | goto not_supported; |
| 792 | *primary = 1; |
| 793 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 794 | case ALUA_ACCESS_STATE_OFFLINE: |
| 795 | /* |
| 796 | * OFFLINE state is defined as a secondary target port |
| 797 | * asymmetric access state. |
| 798 | */ |
Hannes Reinecke | bb91c1a | 2013-12-17 09:18:43 +0100 | [diff] [blame] | 799 | if (!(valid & ALUA_O_SUP)) |
| 800 | goto not_supported; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 801 | *primary = 0; |
| 802 | break; |
Hannes Reinecke | bb91c1a | 2013-12-17 09:18:43 +0100 | [diff] [blame] | 803 | case ALUA_ACCESS_STATE_TRANSITION: |
| 804 | /* |
| 805 | * Transitioning is set internally, and |
| 806 | * cannot be selected manually. |
| 807 | */ |
| 808 | goto not_supported; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 809 | default: |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 810 | pr_err("Unknown ALUA access state: 0x%02x\n", state); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 811 | return TCM_INVALID_PARAMETER_LIST; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | return 0; |
Hannes Reinecke | bb91c1a | 2013-12-17 09:18:43 +0100 | [diff] [blame] | 815 | |
| 816 | not_supported: |
| 817 | pr_err("ALUA access state %s not supported", |
| 818 | core_alua_dump_state(state)); |
| 819 | return TCM_INVALID_PARAMETER_LIST; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | static char *core_alua_dump_state(int state) |
| 823 | { |
| 824 | switch (state) { |
Hannes Reinecke | 73f3bf5 | 2013-11-19 09:07:48 +0100 | [diff] [blame] | 825 | case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 826 | return "Active/Optimized"; |
| 827 | case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED: |
| 828 | return "Active/NonOptimized"; |
Hannes Reinecke | c66094b | 2013-12-17 09:18:49 +0100 | [diff] [blame] | 829 | case ALUA_ACCESS_STATE_LBA_DEPENDENT: |
| 830 | return "LBA Dependent"; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 831 | case ALUA_ACCESS_STATE_STANDBY: |
| 832 | return "Standby"; |
| 833 | case ALUA_ACCESS_STATE_UNAVAILABLE: |
| 834 | return "Unavailable"; |
| 835 | case ALUA_ACCESS_STATE_OFFLINE: |
| 836 | return "Offline"; |
Hannes Reinecke | bb91c1a | 2013-12-17 09:18:43 +0100 | [diff] [blame] | 837 | case ALUA_ACCESS_STATE_TRANSITION: |
| 838 | return "Transitioning"; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 839 | default: |
| 840 | return "Unknown"; |
| 841 | } |
| 842 | |
| 843 | return NULL; |
| 844 | } |
| 845 | |
| 846 | char *core_alua_dump_status(int status) |
| 847 | { |
| 848 | switch (status) { |
| 849 | case ALUA_STATUS_NONE: |
| 850 | return "None"; |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 851 | case ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG: |
| 852 | return "Altered by Explicit STPG"; |
| 853 | case ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA: |
| 854 | return "Altered by Implicit ALUA"; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 855 | default: |
| 856 | return "Unknown"; |
| 857 | } |
| 858 | |
| 859 | return NULL; |
| 860 | } |
| 861 | |
| 862 | /* |
| 863 | * Used by fabric modules to determine when we need to delay processing |
| 864 | * for the Active/NonOptimized paths.. |
| 865 | */ |
| 866 | int core_alua_check_nonop_delay( |
| 867 | struct se_cmd *cmd) |
| 868 | { |
| 869 | if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED)) |
| 870 | return 0; |
| 871 | if (in_interrupt()) |
| 872 | return 0; |
| 873 | /* |
| 874 | * The ALUA Active/NonOptimized access state delay can be disabled |
| 875 | * in via configfs with a value of zero |
| 876 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 877 | if (!cmd->alua_nonop_delay) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 878 | return 0; |
| 879 | /* |
| 880 | * struct se_cmd->alua_nonop_delay gets set by a target port group |
| 881 | * defined interval in core_alua_state_nonoptimized() |
| 882 | */ |
| 883 | msleep_interruptible(cmd->alua_nonop_delay); |
| 884 | return 0; |
| 885 | } |
| 886 | EXPORT_SYMBOL(core_alua_check_nonop_delay); |
| 887 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 888 | static int core_alua_write_tpg_metadata( |
| 889 | const char *path, |
| 890 | unsigned char *md_buf, |
| 891 | u32 md_buf_len) |
| 892 | { |
Al Viro | 0e9b10a | 2013-02-23 15:22:43 -0500 | [diff] [blame] | 893 | struct file *file = filp_open(path, O_RDWR | O_CREAT | O_TRUNC, 0600); |
| 894 | int ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 895 | |
Al Viro | 0e9b10a | 2013-02-23 15:22:43 -0500 | [diff] [blame] | 896 | if (IS_ERR(file)) { |
| 897 | pr_err("filp_open(%s) for ALUA metadata failed\n", path); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 898 | return -ENODEV; |
| 899 | } |
Al Viro | 0e9b10a | 2013-02-23 15:22:43 -0500 | [diff] [blame] | 900 | ret = kernel_write(file, md_buf, md_buf_len, 0); |
| 901 | if (ret < 0) |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 902 | pr_err("Error writing ALUA metadata file: %s\n", path); |
Al Viro | 0e9b10a | 2013-02-23 15:22:43 -0500 | [diff] [blame] | 903 | fput(file); |
Gera Kazakov | f730f91 | 2013-09-09 15:47:06 -0700 | [diff] [blame] | 904 | return (ret < 0) ? -EIO : 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | /* |
| 908 | * Called with tg_pt_gp->tg_pt_gp_md_mutex held |
| 909 | */ |
| 910 | static int core_alua_update_tpg_primary_metadata( |
Hannes Reinecke | 9c6e164 | 2013-12-17 09:18:46 +0100 | [diff] [blame] | 911 | struct t10_alua_tg_pt_gp *tg_pt_gp) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 912 | { |
Hannes Reinecke | 1e0b940 | 2013-12-17 09:18:44 +0100 | [diff] [blame] | 913 | unsigned char *md_buf; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 914 | struct t10_wwn *wwn = &tg_pt_gp->tg_pt_gp_dev->t10_wwn; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 915 | char path[ALUA_METADATA_PATH_LEN]; |
Hannes Reinecke | 1e0b940 | 2013-12-17 09:18:44 +0100 | [diff] [blame] | 916 | int len, rc; |
| 917 | |
| 918 | md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL); |
| 919 | if (!md_buf) { |
| 920 | pr_err("Unable to allocate buf for ALUA metadata\n"); |
| 921 | return -ENOMEM; |
| 922 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 923 | |
| 924 | memset(path, 0, ALUA_METADATA_PATH_LEN); |
| 925 | |
Hannes Reinecke | 1e0b940 | 2013-12-17 09:18:44 +0100 | [diff] [blame] | 926 | len = snprintf(md_buf, ALUA_MD_BUF_LEN, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 927 | "tg_pt_gp_id=%hu\n" |
| 928 | "alua_access_state=0x%02x\n" |
| 929 | "alua_access_status=0x%02x\n", |
Hannes Reinecke | 9c6e164 | 2013-12-17 09:18:46 +0100 | [diff] [blame] | 930 | tg_pt_gp->tg_pt_gp_id, |
| 931 | tg_pt_gp->tg_pt_gp_alua_pending_state, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 932 | tg_pt_gp->tg_pt_gp_alua_access_status); |
| 933 | |
| 934 | snprintf(path, ALUA_METADATA_PATH_LEN, |
Lee Duncan | fdddf93 | 2016-04-14 18:18:51 -0700 | [diff] [blame] | 935 | "%s/alua/tpgs_%s/%s", db_root, &wwn->unit_serial[0], |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 936 | config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item)); |
| 937 | |
Hannes Reinecke | 1e0b940 | 2013-12-17 09:18:44 +0100 | [diff] [blame] | 938 | rc = core_alua_write_tpg_metadata(path, md_buf, len); |
| 939 | kfree(md_buf); |
| 940 | return rc; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 941 | } |
| 942 | |
Hannes Reinecke | ee8c07d | 2015-06-11 10:01:24 +0200 | [diff] [blame] | 943 | static void core_alua_queue_state_change_ua(struct t10_alua_tg_pt_gp *tg_pt_gp) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 944 | { |
| 945 | struct se_dev_entry *se_deve; |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 946 | struct se_lun *lun; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 947 | struct se_lun_acl *lacl; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 948 | |
| 949 | spin_lock(&tg_pt_gp->tg_pt_gp_lock); |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 950 | list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list, |
| 951 | lun_tg_pt_gp_link) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 952 | /* |
| 953 | * After an implicit target port asymmetric access state |
| 954 | * change, a device server shall establish a unit attention |
| 955 | * condition for the initiator port associated with every I_T |
| 956 | * nexus with the additional sense code set to ASYMMETRIC |
Hannes Reinecke | f1ae05d | 2013-11-19 09:07:47 +0100 | [diff] [blame] | 957 | * ACCESS STATE CHANGED. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 958 | * |
| 959 | * After an explicit target port asymmetric access state |
| 960 | * change, a device server shall establish a unit attention |
| 961 | * condition with the additional sense code set to ASYMMETRIC |
| 962 | * ACCESS STATE CHANGED for the initiator port associated with |
| 963 | * every I_T nexus other than the I_T nexus on which the SET |
| 964 | * TARGET PORT GROUPS command |
| 965 | */ |
Nicholas Bellinger | 9e37d04 | 2015-05-20 21:21:08 -0700 | [diff] [blame] | 966 | if (!percpu_ref_tryget_live(&lun->lun_ref)) |
| 967 | continue; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 968 | spin_unlock(&tg_pt_gp->tg_pt_gp_lock); |
| 969 | |
Nicholas Bellinger | 1adff1b | 2015-06-22 23:44:05 -0700 | [diff] [blame] | 970 | spin_lock(&lun->lun_deve_lock); |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 971 | list_for_each_entry(se_deve, &lun->lun_deve_list, lun_link) { |
Nicholas Bellinger | 29a05de | 2015-03-22 20:42:19 -0700 | [diff] [blame] | 972 | lacl = rcu_dereference_check(se_deve->se_lun_acl, |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 973 | lockdep_is_held(&lun->lun_deve_lock)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 974 | |
Hannes Reinecke | c51c8e7 | 2015-06-11 10:01:26 +0200 | [diff] [blame] | 975 | /* |
| 976 | * spc4r37 p.242: |
| 977 | * After an explicit target port asymmetric access |
| 978 | * state change, a device server shall establish a |
| 979 | * unit attention condition with the additional sense |
| 980 | * code set to ASYMMETRIC ACCESS STATE CHANGED for |
| 981 | * the initiator port associated with every I_T nexus |
| 982 | * other than the I_T nexus on which the SET TARGET |
| 983 | * PORT GROUPS command was received. |
| 984 | */ |
Hannes Reinecke | 9c6e164 | 2013-12-17 09:18:46 +0100 | [diff] [blame] | 985 | if ((tg_pt_gp->tg_pt_gp_alua_access_status == |
| 986 | ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) && |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 987 | (tg_pt_gp->tg_pt_gp_alua_lun != NULL) && |
| 988 | (tg_pt_gp->tg_pt_gp_alua_lun == lun)) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 989 | continue; |
| 990 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 991 | /* |
| 992 | * se_deve->se_lun_acl pointer may be NULL for a |
| 993 | * entry created without explicit Node+MappedLUN ACLs |
| 994 | */ |
Hannes Reinecke | c51c8e7 | 2015-06-11 10:01:26 +0200 | [diff] [blame] | 995 | if (lacl && (tg_pt_gp->tg_pt_gp_alua_nacl != NULL) && |
| 996 | (tg_pt_gp->tg_pt_gp_alua_nacl == lacl->se_lun_nacl)) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 997 | continue; |
| 998 | |
Hannes Reinecke | c51c8e7 | 2015-06-11 10:01:26 +0200 | [diff] [blame] | 999 | core_scsi3_ua_allocate(se_deve, 0x2A, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1000 | ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED); |
| 1001 | } |
Nicholas Bellinger | 1adff1b | 2015-06-22 23:44:05 -0700 | [diff] [blame] | 1002 | spin_unlock(&lun->lun_deve_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1003 | |
| 1004 | spin_lock(&tg_pt_gp->tg_pt_gp_lock); |
Nicholas Bellinger | 9e37d04 | 2015-05-20 21:21:08 -0700 | [diff] [blame] | 1005 | percpu_ref_put(&lun->lun_ref); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1006 | } |
| 1007 | spin_unlock(&tg_pt_gp->tg_pt_gp_lock); |
Hannes Reinecke | ee8c07d | 2015-06-11 10:01:24 +0200 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | static void core_alua_do_transition_tg_pt_work(struct work_struct *work) |
| 1011 | { |
| 1012 | struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(work, |
Mike Christie | 892e4f9 | 2017-03-02 04:59:48 -0600 | [diff] [blame] | 1013 | struct t10_alua_tg_pt_gp, tg_pt_gp_transition_work); |
Hannes Reinecke | ee8c07d | 2015-06-11 10:01:24 +0200 | [diff] [blame] | 1014 | struct se_device *dev = tg_pt_gp->tg_pt_gp_dev; |
| 1015 | bool explicit = (tg_pt_gp->tg_pt_gp_alua_access_status == |
| 1016 | ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG); |
| 1017 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1018 | /* |
| 1019 | * Update the ALUA metadata buf that has been allocated in |
| 1020 | * core_alua_do_port_transition(), this metadata will be written |
| 1021 | * to struct file. |
| 1022 | * |
| 1023 | * Note that there is the case where we do not want to update the |
| 1024 | * metadata when the saved metadata is being parsed in userspace |
| 1025 | * when setting the existing port access state and access status. |
| 1026 | * |
| 1027 | * Also note that the failure to write out the ALUA metadata to |
| 1028 | * struct file does NOT affect the actual ALUA transition. |
| 1029 | */ |
| 1030 | if (tg_pt_gp->tg_pt_gp_write_metadata) { |
| 1031 | mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex); |
Hannes Reinecke | 9c6e164 | 2013-12-17 09:18:46 +0100 | [diff] [blame] | 1032 | core_alua_update_tpg_primary_metadata(tg_pt_gp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1033 | mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex); |
| 1034 | } |
| 1035 | /* |
| 1036 | * Set the current primary ALUA access state to the requested new state |
| 1037 | */ |
Hannes Reinecke | dfbce75 | 2013-12-17 09:18:45 +0100 | [diff] [blame] | 1038 | atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, |
| 1039 | tg_pt_gp->tg_pt_gp_alua_pending_state); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1040 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1041 | pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu" |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 1042 | " from primary access state %s to %s\n", (explicit) ? "explicit" : |
| 1043 | "implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item), |
Hannes Reinecke | dfbce75 | 2013-12-17 09:18:45 +0100 | [diff] [blame] | 1044 | tg_pt_gp->tg_pt_gp_id, |
| 1045 | core_alua_dump_state(tg_pt_gp->tg_pt_gp_alua_previous_state), |
| 1046 | core_alua_dump_state(tg_pt_gp->tg_pt_gp_alua_pending_state)); |
Hannes Reinecke | ee8c07d | 2015-06-11 10:01:24 +0200 | [diff] [blame] | 1047 | |
| 1048 | core_alua_queue_state_change_ua(tg_pt_gp); |
| 1049 | |
Hannes Reinecke | 9c6e164 | 2013-12-17 09:18:46 +0100 | [diff] [blame] | 1050 | spin_lock(&dev->t10_alua.tg_pt_gps_lock); |
| 1051 | atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt); |
Hannes Reinecke | 9c6e164 | 2013-12-17 09:18:46 +0100 | [diff] [blame] | 1052 | spin_unlock(&dev->t10_alua.tg_pt_gps_lock); |
| 1053 | |
| 1054 | if (tg_pt_gp->tg_pt_gp_transition_complete) |
| 1055 | complete(tg_pt_gp->tg_pt_gp_transition_complete); |
| 1056 | } |
| 1057 | |
| 1058 | static int core_alua_do_transition_tg_pt( |
| 1059 | struct t10_alua_tg_pt_gp *tg_pt_gp, |
| 1060 | int new_state, |
| 1061 | int explicit) |
| 1062 | { |
| 1063 | struct se_device *dev = tg_pt_gp->tg_pt_gp_dev; |
| 1064 | DECLARE_COMPLETION_ONSTACK(wait); |
| 1065 | |
| 1066 | /* Nothing to be done here */ |
| 1067 | if (atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state) == new_state) |
| 1068 | return 0; |
| 1069 | |
| 1070 | if (new_state == ALUA_ACCESS_STATE_TRANSITION) |
| 1071 | return -EAGAIN; |
| 1072 | |
| 1073 | /* |
| 1074 | * Flush any pending transitions |
| 1075 | */ |
Mike Christie | 25b0b3f | 2017-03-02 04:59:50 -0600 | [diff] [blame] | 1076 | if (!explicit) |
Mike Christie | 892e4f9 | 2017-03-02 04:59:48 -0600 | [diff] [blame] | 1077 | flush_work(&tg_pt_gp->tg_pt_gp_transition_work); |
Hannes Reinecke | 9c6e164 | 2013-12-17 09:18:46 +0100 | [diff] [blame] | 1078 | |
| 1079 | /* |
| 1080 | * Save the old primary ALUA access state, and set the current state |
| 1081 | * to ALUA_ACCESS_STATE_TRANSITION. |
| 1082 | */ |
| 1083 | tg_pt_gp->tg_pt_gp_alua_previous_state = |
| 1084 | atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state); |
| 1085 | tg_pt_gp->tg_pt_gp_alua_pending_state = new_state; |
| 1086 | |
| 1087 | atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, |
| 1088 | ALUA_ACCESS_STATE_TRANSITION); |
| 1089 | tg_pt_gp->tg_pt_gp_alua_access_status = (explicit) ? |
| 1090 | ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG : |
| 1091 | ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA; |
| 1092 | |
Hannes Reinecke | ee8c07d | 2015-06-11 10:01:24 +0200 | [diff] [blame] | 1093 | core_alua_queue_state_change_ua(tg_pt_gp); |
| 1094 | |
Hannes Reinecke | 9c6e164 | 2013-12-17 09:18:46 +0100 | [diff] [blame] | 1095 | /* |
| 1096 | * Check for the optional ALUA primary state transition delay |
| 1097 | */ |
| 1098 | if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0) |
| 1099 | msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs); |
| 1100 | |
| 1101 | /* |
| 1102 | * Take a reference for workqueue item |
| 1103 | */ |
| 1104 | spin_lock(&dev->t10_alua.tg_pt_gps_lock); |
| 1105 | atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt); |
Hannes Reinecke | 9c6e164 | 2013-12-17 09:18:46 +0100 | [diff] [blame] | 1106 | spin_unlock(&dev->t10_alua.tg_pt_gps_lock); |
| 1107 | |
Mike Christie | 892e4f9 | 2017-03-02 04:59:48 -0600 | [diff] [blame] | 1108 | schedule_work(&tg_pt_gp->tg_pt_gp_transition_work); |
| 1109 | if (explicit) { |
Hannes Reinecke | 9c6e164 | 2013-12-17 09:18:46 +0100 | [diff] [blame] | 1110 | tg_pt_gp->tg_pt_gp_transition_complete = &wait; |
Hannes Reinecke | 9c6e164 | 2013-12-17 09:18:46 +0100 | [diff] [blame] | 1111 | wait_for_completion(&wait); |
| 1112 | tg_pt_gp->tg_pt_gp_transition_complete = NULL; |
| 1113 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1114 | |
| 1115 | return 0; |
| 1116 | } |
| 1117 | |
| 1118 | int core_alua_do_port_transition( |
| 1119 | struct t10_alua_tg_pt_gp *l_tg_pt_gp, |
| 1120 | struct se_device *l_dev, |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1121 | struct se_lun *l_lun, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1122 | struct se_node_acl *l_nacl, |
| 1123 | int new_state, |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 1124 | int explicit) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1125 | { |
| 1126 | struct se_device *dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1127 | struct t10_alua_lu_gp *lu_gp; |
| 1128 | struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem; |
| 1129 | struct t10_alua_tg_pt_gp *tg_pt_gp; |
Hannes Reinecke | 9c6e164 | 2013-12-17 09:18:46 +0100 | [diff] [blame] | 1130 | int primary, valid_states, rc = 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1131 | |
Hannes Reinecke | bb91c1a | 2013-12-17 09:18:43 +0100 | [diff] [blame] | 1132 | valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states; |
| 1133 | if (core_alua_check_transition(new_state, valid_states, &primary) != 0) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1134 | return -EINVAL; |
| 1135 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1136 | local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem; |
| 1137 | spin_lock(&local_lu_gp_mem->lu_gp_mem_lock); |
| 1138 | lu_gp = local_lu_gp_mem->lu_gp; |
| 1139 | atomic_inc(&lu_gp->lu_gp_ref_cnt); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1140 | spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock); |
| 1141 | /* |
| 1142 | * For storage objects that are members of the 'default_lu_gp', |
| 1143 | * we only do transition on the passed *l_tp_pt_gp, and not |
| 1144 | * on all of the matching target port groups IDs in default_lu_gp. |
| 1145 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1146 | if (!lu_gp->lu_gp_id) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1147 | /* |
| 1148 | * core_alua_do_transition_tg_pt() will always return |
| 1149 | * success. |
| 1150 | */ |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1151 | l_tg_pt_gp->tg_pt_gp_alua_lun = l_lun; |
Hannes Reinecke | 9c6e164 | 2013-12-17 09:18:46 +0100 | [diff] [blame] | 1152 | l_tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl; |
| 1153 | rc = core_alua_do_transition_tg_pt(l_tg_pt_gp, |
| 1154 | new_state, explicit); |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 1155 | atomic_dec_mb(&lu_gp->lu_gp_ref_cnt); |
Hannes Reinecke | 9c6e164 | 2013-12-17 09:18:46 +0100 | [diff] [blame] | 1156 | return rc; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1157 | } |
| 1158 | /* |
| 1159 | * For all other LU groups aside from 'default_lu_gp', walk all of |
| 1160 | * the associated storage objects looking for a matching target port |
| 1161 | * group ID from the local target port group. |
| 1162 | */ |
| 1163 | spin_lock(&lu_gp->lu_gp_lock); |
| 1164 | list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, |
| 1165 | lu_gp_mem_list) { |
| 1166 | |
| 1167 | dev = lu_gp_mem->lu_gp_mem_dev; |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 1168 | atomic_inc_mb(&lu_gp_mem->lu_gp_mem_ref_cnt); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1169 | spin_unlock(&lu_gp->lu_gp_lock); |
| 1170 | |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1171 | spin_lock(&dev->t10_alua.tg_pt_gps_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1172 | list_for_each_entry(tg_pt_gp, |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1173 | &dev->t10_alua.tg_pt_gps_list, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1174 | tg_pt_gp_list) { |
| 1175 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1176 | if (!tg_pt_gp->tg_pt_gp_valid_id) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1177 | continue; |
| 1178 | /* |
| 1179 | * If the target behavior port asymmetric access state |
Hannes Reinecke | f1ae05d | 2013-11-19 09:07:47 +0100 | [diff] [blame] | 1180 | * is changed for any target port group accessible via |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1181 | * a logical unit within a LU group, the target port |
| 1182 | * behavior group asymmetric access states for the same |
| 1183 | * target port group accessible via other logical units |
| 1184 | * in that LU group will also change. |
| 1185 | */ |
| 1186 | if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id) |
| 1187 | continue; |
| 1188 | |
| 1189 | if (l_tg_pt_gp == tg_pt_gp) { |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1190 | tg_pt_gp->tg_pt_gp_alua_lun = l_lun; |
Hannes Reinecke | 9c6e164 | 2013-12-17 09:18:46 +0100 | [diff] [blame] | 1191 | tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1192 | } else { |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1193 | tg_pt_gp->tg_pt_gp_alua_lun = NULL; |
Hannes Reinecke | 9c6e164 | 2013-12-17 09:18:46 +0100 | [diff] [blame] | 1194 | tg_pt_gp->tg_pt_gp_alua_nacl = NULL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1195 | } |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 1196 | atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt); |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1197 | spin_unlock(&dev->t10_alua.tg_pt_gps_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1198 | /* |
| 1199 | * core_alua_do_transition_tg_pt() will always return |
| 1200 | * success. |
| 1201 | */ |
Hannes Reinecke | 9c6e164 | 2013-12-17 09:18:46 +0100 | [diff] [blame] | 1202 | rc = core_alua_do_transition_tg_pt(tg_pt_gp, |
| 1203 | new_state, explicit); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1204 | |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1205 | spin_lock(&dev->t10_alua.tg_pt_gps_lock); |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 1206 | atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt); |
Hannes Reinecke | 9c6e164 | 2013-12-17 09:18:46 +0100 | [diff] [blame] | 1207 | if (rc) |
| 1208 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1209 | } |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1210 | spin_unlock(&dev->t10_alua.tg_pt_gps_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1211 | |
| 1212 | spin_lock(&lu_gp->lu_gp_lock); |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 1213 | atomic_dec_mb(&lu_gp_mem->lu_gp_mem_ref_cnt); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1214 | } |
| 1215 | spin_unlock(&lu_gp->lu_gp_lock); |
| 1216 | |
Hannes Reinecke | 9c6e164 | 2013-12-17 09:18:46 +0100 | [diff] [blame] | 1217 | if (!rc) { |
| 1218 | pr_debug("Successfully processed LU Group: %s all ALUA TG PT" |
| 1219 | " Group IDs: %hu %s transition to primary state: %s\n", |
| 1220 | config_item_name(&lu_gp->lu_gp_group.cg_item), |
| 1221 | l_tg_pt_gp->tg_pt_gp_id, |
| 1222 | (explicit) ? "explicit" : "implicit", |
| 1223 | core_alua_dump_state(new_state)); |
| 1224 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1225 | |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 1226 | atomic_dec_mb(&lu_gp->lu_gp_ref_cnt); |
Hannes Reinecke | 9c6e164 | 2013-12-17 09:18:46 +0100 | [diff] [blame] | 1227 | return rc; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1228 | } |
| 1229 | |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1230 | static int core_alua_update_tpg_secondary_metadata(struct se_lun *lun) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1231 | { |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1232 | struct se_portal_group *se_tpg = lun->lun_tpg; |
Hannes Reinecke | 1e0b940 | 2013-12-17 09:18:44 +0100 | [diff] [blame] | 1233 | unsigned char *md_buf; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1234 | char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN]; |
Hannes Reinecke | 1e0b940 | 2013-12-17 09:18:44 +0100 | [diff] [blame] | 1235 | int len, rc; |
| 1236 | |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1237 | mutex_lock(&lun->lun_tg_pt_md_mutex); |
| 1238 | |
Hannes Reinecke | 1e0b940 | 2013-12-17 09:18:44 +0100 | [diff] [blame] | 1239 | md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL); |
| 1240 | if (!md_buf) { |
| 1241 | pr_err("Unable to allocate buf for ALUA metadata\n"); |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1242 | rc = -ENOMEM; |
| 1243 | goto out_unlock; |
Hannes Reinecke | 1e0b940 | 2013-12-17 09:18:44 +0100 | [diff] [blame] | 1244 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1245 | |
| 1246 | memset(path, 0, ALUA_METADATA_PATH_LEN); |
| 1247 | memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN); |
| 1248 | |
| 1249 | len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1250 | se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1251 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1252 | if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1253 | snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1254 | se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1255 | |
Hannes Reinecke | 1e0b940 | 2013-12-17 09:18:44 +0100 | [diff] [blame] | 1256 | len = snprintf(md_buf, ALUA_MD_BUF_LEN, "alua_tg_pt_offline=%d\n" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1257 | "alua_tg_pt_status=0x%02x\n", |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1258 | atomic_read(&lun->lun_tg_pt_secondary_offline), |
| 1259 | lun->lun_tg_pt_secondary_stat); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1260 | |
Lee Duncan | fdddf93 | 2016-04-14 18:18:51 -0700 | [diff] [blame] | 1261 | snprintf(path, ALUA_METADATA_PATH_LEN, "%s/alua/%s/%s/lun_%llu", |
| 1262 | db_root, se_tpg->se_tpg_tfo->get_fabric_name(), wwn, |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1263 | lun->unpacked_lun); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1264 | |
Hannes Reinecke | 1e0b940 | 2013-12-17 09:18:44 +0100 | [diff] [blame] | 1265 | rc = core_alua_write_tpg_metadata(path, md_buf, len); |
| 1266 | kfree(md_buf); |
| 1267 | |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1268 | out_unlock: |
| 1269 | mutex_unlock(&lun->lun_tg_pt_md_mutex); |
Hannes Reinecke | 1e0b940 | 2013-12-17 09:18:44 +0100 | [diff] [blame] | 1270 | return rc; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | static int core_alua_set_tg_pt_secondary_state( |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1274 | struct se_lun *lun, |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 1275 | int explicit, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1276 | int offline) |
| 1277 | { |
| 1278 | struct t10_alua_tg_pt_gp *tg_pt_gp; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1279 | int trans_delay_msecs; |
| 1280 | |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1281 | spin_lock(&lun->lun_tg_pt_gp_lock); |
| 1282 | tg_pt_gp = lun->lun_tg_pt_gp; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1283 | if (!tg_pt_gp) { |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1284 | spin_unlock(&lun->lun_tg_pt_gp_lock); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1285 | pr_err("Unable to complete secondary state" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1286 | " transition\n"); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1287 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1288 | } |
| 1289 | trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs; |
| 1290 | /* |
| 1291 | * Set the secondary ALUA target port access state to OFFLINE |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1292 | * or release the previously secondary state for struct se_lun |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1293 | */ |
| 1294 | if (offline) |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1295 | atomic_set(&lun->lun_tg_pt_secondary_offline, 1); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1296 | else |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1297 | atomic_set(&lun->lun_tg_pt_secondary_offline, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1298 | |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1299 | lun->lun_tg_pt_secondary_stat = (explicit) ? |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 1300 | ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG : |
| 1301 | ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1302 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1303 | pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu" |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 1304 | " to secondary access state: %s\n", (explicit) ? "explicit" : |
| 1305 | "implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item), |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1306 | tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE"); |
| 1307 | |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1308 | spin_unlock(&lun->lun_tg_pt_gp_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1309 | /* |
| 1310 | * Do the optional transition delay after we set the secondary |
| 1311 | * ALUA access state. |
| 1312 | */ |
| 1313 | if (trans_delay_msecs != 0) |
| 1314 | msleep_interruptible(trans_delay_msecs); |
| 1315 | /* |
| 1316 | * See if we need to update the ALUA fabric port metadata for |
| 1317 | * secondary state and status |
| 1318 | */ |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1319 | if (lun->lun_tg_pt_secondary_write_md) |
| 1320 | core_alua_update_tpg_secondary_metadata(lun); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1321 | |
| 1322 | return 0; |
| 1323 | } |
| 1324 | |
Hannes Reinecke | 229d4f1 | 2013-12-17 09:18:50 +0100 | [diff] [blame] | 1325 | struct t10_alua_lba_map * |
| 1326 | core_alua_allocate_lba_map(struct list_head *list, |
| 1327 | u64 first_lba, u64 last_lba) |
| 1328 | { |
| 1329 | struct t10_alua_lba_map *lba_map; |
| 1330 | |
| 1331 | lba_map = kmem_cache_zalloc(t10_alua_lba_map_cache, GFP_KERNEL); |
| 1332 | if (!lba_map) { |
| 1333 | pr_err("Unable to allocate struct t10_alua_lba_map\n"); |
| 1334 | return ERR_PTR(-ENOMEM); |
| 1335 | } |
| 1336 | INIT_LIST_HEAD(&lba_map->lba_map_mem_list); |
| 1337 | lba_map->lba_map_first_lba = first_lba; |
| 1338 | lba_map->lba_map_last_lba = last_lba; |
| 1339 | |
| 1340 | list_add_tail(&lba_map->lba_map_list, list); |
| 1341 | return lba_map; |
| 1342 | } |
| 1343 | |
| 1344 | int |
| 1345 | core_alua_allocate_lba_map_mem(struct t10_alua_lba_map *lba_map, |
| 1346 | int pg_id, int state) |
| 1347 | { |
| 1348 | struct t10_alua_lba_map_member *lba_map_mem; |
| 1349 | |
| 1350 | list_for_each_entry(lba_map_mem, &lba_map->lba_map_mem_list, |
| 1351 | lba_map_mem_list) { |
| 1352 | if (lba_map_mem->lba_map_mem_alua_pg_id == pg_id) { |
| 1353 | pr_err("Duplicate pg_id %d in lba_map\n", pg_id); |
| 1354 | return -EINVAL; |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | lba_map_mem = kmem_cache_zalloc(t10_alua_lba_map_mem_cache, GFP_KERNEL); |
| 1359 | if (!lba_map_mem) { |
| 1360 | pr_err("Unable to allocate struct t10_alua_lba_map_mem\n"); |
| 1361 | return -ENOMEM; |
| 1362 | } |
| 1363 | lba_map_mem->lba_map_mem_alua_state = state; |
| 1364 | lba_map_mem->lba_map_mem_alua_pg_id = pg_id; |
| 1365 | |
| 1366 | list_add_tail(&lba_map_mem->lba_map_mem_list, |
| 1367 | &lba_map->lba_map_mem_list); |
| 1368 | return 0; |
| 1369 | } |
| 1370 | |
| 1371 | void |
| 1372 | core_alua_free_lba_map(struct list_head *lba_list) |
| 1373 | { |
| 1374 | struct t10_alua_lba_map *lba_map, *lba_map_tmp; |
| 1375 | struct t10_alua_lba_map_member *lba_map_mem, *lba_map_mem_tmp; |
| 1376 | |
| 1377 | list_for_each_entry_safe(lba_map, lba_map_tmp, lba_list, |
| 1378 | lba_map_list) { |
| 1379 | list_for_each_entry_safe(lba_map_mem, lba_map_mem_tmp, |
| 1380 | &lba_map->lba_map_mem_list, |
| 1381 | lba_map_mem_list) { |
| 1382 | list_del(&lba_map_mem->lba_map_mem_list); |
| 1383 | kmem_cache_free(t10_alua_lba_map_mem_cache, |
| 1384 | lba_map_mem); |
| 1385 | } |
| 1386 | list_del(&lba_map->lba_map_list); |
| 1387 | kmem_cache_free(t10_alua_lba_map_cache, lba_map); |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | void |
| 1392 | core_alua_set_lba_map(struct se_device *dev, struct list_head *lba_map_list, |
| 1393 | int segment_size, int segment_mult) |
| 1394 | { |
| 1395 | struct list_head old_lba_map_list; |
| 1396 | struct t10_alua_tg_pt_gp *tg_pt_gp; |
| 1397 | int activate = 0, supported; |
| 1398 | |
| 1399 | INIT_LIST_HEAD(&old_lba_map_list); |
| 1400 | spin_lock(&dev->t10_alua.lba_map_lock); |
| 1401 | dev->t10_alua.lba_map_segment_size = segment_size; |
| 1402 | dev->t10_alua.lba_map_segment_multiplier = segment_mult; |
| 1403 | list_splice_init(&dev->t10_alua.lba_map_list, &old_lba_map_list); |
| 1404 | if (lba_map_list) { |
| 1405 | list_splice_init(lba_map_list, &dev->t10_alua.lba_map_list); |
| 1406 | activate = 1; |
| 1407 | } |
| 1408 | spin_unlock(&dev->t10_alua.lba_map_lock); |
| 1409 | spin_lock(&dev->t10_alua.tg_pt_gps_lock); |
| 1410 | list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list, |
| 1411 | tg_pt_gp_list) { |
| 1412 | |
| 1413 | if (!tg_pt_gp->tg_pt_gp_valid_id) |
| 1414 | continue; |
| 1415 | supported = tg_pt_gp->tg_pt_gp_alua_supported_states; |
| 1416 | if (activate) |
| 1417 | supported |= ALUA_LBD_SUP; |
| 1418 | else |
| 1419 | supported &= ~ALUA_LBD_SUP; |
| 1420 | tg_pt_gp->tg_pt_gp_alua_supported_states = supported; |
| 1421 | } |
| 1422 | spin_unlock(&dev->t10_alua.tg_pt_gps_lock); |
| 1423 | core_alua_free_lba_map(&old_lba_map_list); |
| 1424 | } |
| 1425 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1426 | struct t10_alua_lu_gp * |
| 1427 | core_alua_allocate_lu_gp(const char *name, int def_group) |
| 1428 | { |
| 1429 | struct t10_alua_lu_gp *lu_gp; |
| 1430 | |
| 1431 | lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1432 | if (!lu_gp) { |
| 1433 | pr_err("Unable to allocate struct t10_alua_lu_gp\n"); |
Justin P. Mattock | 6eab04a | 2011-04-08 19:49:08 -0700 | [diff] [blame] | 1434 | return ERR_PTR(-ENOMEM); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1435 | } |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1436 | INIT_LIST_HEAD(&lu_gp->lu_gp_node); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1437 | INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list); |
| 1438 | spin_lock_init(&lu_gp->lu_gp_lock); |
| 1439 | atomic_set(&lu_gp->lu_gp_ref_cnt, 0); |
| 1440 | |
| 1441 | if (def_group) { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1442 | lu_gp->lu_gp_id = alua_lu_gps_counter++; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1443 | lu_gp->lu_gp_valid_id = 1; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1444 | alua_lu_gps_count++; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | return lu_gp; |
| 1448 | } |
| 1449 | |
| 1450 | int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id) |
| 1451 | { |
| 1452 | struct t10_alua_lu_gp *lu_gp_tmp; |
| 1453 | u16 lu_gp_id_tmp; |
| 1454 | /* |
| 1455 | * The lu_gp->lu_gp_id may only be set once.. |
| 1456 | */ |
| 1457 | if (lu_gp->lu_gp_valid_id) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1458 | pr_warn("ALUA LU Group already has a valid ID," |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1459 | " ignoring request\n"); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1460 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1461 | } |
| 1462 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1463 | spin_lock(&lu_gps_lock); |
| 1464 | if (alua_lu_gps_count == 0x0000ffff) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1465 | pr_err("Maximum ALUA alua_lu_gps_count:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1466 | " 0x0000ffff reached\n"); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1467 | spin_unlock(&lu_gps_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1468 | kmem_cache_free(t10_alua_lu_gp_cache, lu_gp); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1469 | return -ENOSPC; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1470 | } |
| 1471 | again: |
| 1472 | lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id : |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1473 | alua_lu_gps_counter++; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1474 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1475 | list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1476 | if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1477 | if (!lu_gp_id) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1478 | goto again; |
| 1479 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1480 | pr_warn("ALUA Logical Unit Group ID: %hu" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1481 | " already exists, ignoring request\n", |
| 1482 | lu_gp_id); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1483 | spin_unlock(&lu_gps_lock); |
| 1484 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | lu_gp->lu_gp_id = lu_gp_id_tmp; |
| 1489 | lu_gp->lu_gp_valid_id = 1; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1490 | list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list); |
| 1491 | alua_lu_gps_count++; |
| 1492 | spin_unlock(&lu_gps_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1493 | |
| 1494 | return 0; |
| 1495 | } |
| 1496 | |
| 1497 | static struct t10_alua_lu_gp_member * |
| 1498 | core_alua_allocate_lu_gp_mem(struct se_device *dev) |
| 1499 | { |
| 1500 | struct t10_alua_lu_gp_member *lu_gp_mem; |
| 1501 | |
| 1502 | lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1503 | if (!lu_gp_mem) { |
| 1504 | pr_err("Unable to allocate struct t10_alua_lu_gp_member\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1505 | return ERR_PTR(-ENOMEM); |
| 1506 | } |
| 1507 | INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list); |
| 1508 | spin_lock_init(&lu_gp_mem->lu_gp_mem_lock); |
| 1509 | atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0); |
| 1510 | |
| 1511 | lu_gp_mem->lu_gp_mem_dev = dev; |
| 1512 | dev->dev_alua_lu_gp_mem = lu_gp_mem; |
| 1513 | |
| 1514 | return lu_gp_mem; |
| 1515 | } |
| 1516 | |
| 1517 | void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp) |
| 1518 | { |
| 1519 | struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp; |
| 1520 | /* |
| 1521 | * Once we have reached this point, config_item_put() has |
| 1522 | * already been called from target_core_alua_drop_lu_gp(). |
| 1523 | * |
| 1524 | * Here, we remove the *lu_gp from the global list so that |
| 1525 | * no associations can be made while we are releasing |
| 1526 | * struct t10_alua_lu_gp. |
| 1527 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1528 | spin_lock(&lu_gps_lock); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1529 | list_del(&lu_gp->lu_gp_node); |
| 1530 | alua_lu_gps_count--; |
| 1531 | spin_unlock(&lu_gps_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1532 | /* |
| 1533 | * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name() |
| 1534 | * in target_core_configfs.c:target_core_store_alua_lu_gp() to be |
| 1535 | * released with core_alua_put_lu_gp_from_name() |
| 1536 | */ |
| 1537 | while (atomic_read(&lu_gp->lu_gp_ref_cnt)) |
| 1538 | cpu_relax(); |
| 1539 | /* |
| 1540 | * Release reference to struct t10_alua_lu_gp * from all associated |
| 1541 | * struct se_device. |
| 1542 | */ |
| 1543 | spin_lock(&lu_gp->lu_gp_lock); |
| 1544 | list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp, |
| 1545 | &lu_gp->lu_gp_mem_list, lu_gp_mem_list) { |
| 1546 | if (lu_gp_mem->lu_gp_assoc) { |
| 1547 | list_del(&lu_gp_mem->lu_gp_mem_list); |
| 1548 | lu_gp->lu_gp_members--; |
| 1549 | lu_gp_mem->lu_gp_assoc = 0; |
| 1550 | } |
| 1551 | spin_unlock(&lu_gp->lu_gp_lock); |
| 1552 | /* |
| 1553 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1554 | * lu_gp_mem is associated with a single |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1555 | * struct se_device->dev_alua_lu_gp_mem, and is released when |
| 1556 | * struct se_device is released via core_alua_free_lu_gp_mem(). |
| 1557 | * |
| 1558 | * If the passed lu_gp does NOT match the default_lu_gp, assume |
Hannes Reinecke | f1ae05d | 2013-11-19 09:07:47 +0100 | [diff] [blame] | 1559 | * we want to re-associate a given lu_gp_mem with default_lu_gp. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1560 | */ |
| 1561 | spin_lock(&lu_gp_mem->lu_gp_mem_lock); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1562 | if (lu_gp != default_lu_gp) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1563 | __core_alua_attach_lu_gp_mem(lu_gp_mem, |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1564 | default_lu_gp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1565 | else |
| 1566 | lu_gp_mem->lu_gp = NULL; |
| 1567 | spin_unlock(&lu_gp_mem->lu_gp_mem_lock); |
| 1568 | |
| 1569 | spin_lock(&lu_gp->lu_gp_lock); |
| 1570 | } |
| 1571 | spin_unlock(&lu_gp->lu_gp_lock); |
| 1572 | |
| 1573 | kmem_cache_free(t10_alua_lu_gp_cache, lu_gp); |
| 1574 | } |
| 1575 | |
| 1576 | void core_alua_free_lu_gp_mem(struct se_device *dev) |
| 1577 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1578 | struct t10_alua_lu_gp *lu_gp; |
| 1579 | struct t10_alua_lu_gp_member *lu_gp_mem; |
| 1580 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1581 | lu_gp_mem = dev->dev_alua_lu_gp_mem; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1582 | if (!lu_gp_mem) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1583 | return; |
| 1584 | |
| 1585 | while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt)) |
| 1586 | cpu_relax(); |
| 1587 | |
| 1588 | spin_lock(&lu_gp_mem->lu_gp_mem_lock); |
| 1589 | lu_gp = lu_gp_mem->lu_gp; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1590 | if (lu_gp) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1591 | spin_lock(&lu_gp->lu_gp_lock); |
| 1592 | if (lu_gp_mem->lu_gp_assoc) { |
| 1593 | list_del(&lu_gp_mem->lu_gp_mem_list); |
| 1594 | lu_gp->lu_gp_members--; |
| 1595 | lu_gp_mem->lu_gp_assoc = 0; |
| 1596 | } |
| 1597 | spin_unlock(&lu_gp->lu_gp_lock); |
| 1598 | lu_gp_mem->lu_gp = NULL; |
| 1599 | } |
| 1600 | spin_unlock(&lu_gp_mem->lu_gp_mem_lock); |
| 1601 | |
| 1602 | kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem); |
| 1603 | } |
| 1604 | |
| 1605 | struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name) |
| 1606 | { |
| 1607 | struct t10_alua_lu_gp *lu_gp; |
| 1608 | struct config_item *ci; |
| 1609 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1610 | spin_lock(&lu_gps_lock); |
| 1611 | list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1612 | if (!lu_gp->lu_gp_valid_id) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1613 | continue; |
| 1614 | ci = &lu_gp->lu_gp_group.cg_item; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1615 | if (!strcmp(config_item_name(ci), name)) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1616 | atomic_inc(&lu_gp->lu_gp_ref_cnt); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1617 | spin_unlock(&lu_gps_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1618 | return lu_gp; |
| 1619 | } |
| 1620 | } |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1621 | spin_unlock(&lu_gps_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1622 | |
| 1623 | return NULL; |
| 1624 | } |
| 1625 | |
| 1626 | void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp) |
| 1627 | { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1628 | spin_lock(&lu_gps_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1629 | atomic_dec(&lu_gp->lu_gp_ref_cnt); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1630 | spin_unlock(&lu_gps_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | /* |
| 1634 | * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock |
| 1635 | */ |
| 1636 | void __core_alua_attach_lu_gp_mem( |
| 1637 | struct t10_alua_lu_gp_member *lu_gp_mem, |
| 1638 | struct t10_alua_lu_gp *lu_gp) |
| 1639 | { |
| 1640 | spin_lock(&lu_gp->lu_gp_lock); |
| 1641 | lu_gp_mem->lu_gp = lu_gp; |
| 1642 | lu_gp_mem->lu_gp_assoc = 1; |
| 1643 | list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list); |
| 1644 | lu_gp->lu_gp_members++; |
| 1645 | spin_unlock(&lu_gp->lu_gp_lock); |
| 1646 | } |
| 1647 | |
| 1648 | /* |
| 1649 | * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock |
| 1650 | */ |
| 1651 | void __core_alua_drop_lu_gp_mem( |
| 1652 | struct t10_alua_lu_gp_member *lu_gp_mem, |
| 1653 | struct t10_alua_lu_gp *lu_gp) |
| 1654 | { |
| 1655 | spin_lock(&lu_gp->lu_gp_lock); |
| 1656 | list_del(&lu_gp_mem->lu_gp_mem_list); |
| 1657 | lu_gp_mem->lu_gp = NULL; |
| 1658 | lu_gp_mem->lu_gp_assoc = 0; |
| 1659 | lu_gp->lu_gp_members--; |
| 1660 | spin_unlock(&lu_gp->lu_gp_lock); |
| 1661 | } |
| 1662 | |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1663 | struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(struct se_device *dev, |
| 1664 | const char *name, int def_group) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1665 | { |
| 1666 | struct t10_alua_tg_pt_gp *tg_pt_gp; |
| 1667 | |
| 1668 | tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1669 | if (!tg_pt_gp) { |
| 1670 | pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1671 | return NULL; |
| 1672 | } |
| 1673 | INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list); |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1674 | INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_lun_list); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1675 | mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex); |
| 1676 | spin_lock_init(&tg_pt_gp->tg_pt_gp_lock); |
| 1677 | atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0); |
Mike Christie | 892e4f9 | 2017-03-02 04:59:48 -0600 | [diff] [blame] | 1678 | INIT_WORK(&tg_pt_gp->tg_pt_gp_transition_work, |
| 1679 | core_alua_do_transition_tg_pt_work); |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1680 | tg_pt_gp->tg_pt_gp_dev = dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1681 | atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, |
Hannes Reinecke | 73f3bf5 | 2013-11-19 09:07:48 +0100 | [diff] [blame] | 1682 | ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1683 | /* |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 1684 | * Enable both explicit and implicit ALUA support by default |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1685 | */ |
| 1686 | tg_pt_gp->tg_pt_gp_alua_access_type = |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 1687 | TPGS_EXPLICIT_ALUA | TPGS_IMPLICIT_ALUA; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1688 | /* |
| 1689 | * Set the default Active/NonOptimized Delay in milliseconds |
| 1690 | */ |
| 1691 | tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS; |
| 1692 | tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS; |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 1693 | tg_pt_gp->tg_pt_gp_implicit_trans_secs = ALUA_DEFAULT_IMPLICIT_TRANS_SECS; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1694 | |
Hannes Reinecke | c0dc941 | 2013-11-19 09:07:49 +0100 | [diff] [blame] | 1695 | /* |
| 1696 | * Enable all supported states |
| 1697 | */ |
| 1698 | tg_pt_gp->tg_pt_gp_alua_supported_states = |
| 1699 | ALUA_T_SUP | ALUA_O_SUP | |
| 1700 | ALUA_U_SUP | ALUA_S_SUP | ALUA_AN_SUP | ALUA_AO_SUP; |
| 1701 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1702 | if (def_group) { |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1703 | spin_lock(&dev->t10_alua.tg_pt_gps_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1704 | tg_pt_gp->tg_pt_gp_id = |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1705 | dev->t10_alua.alua_tg_pt_gps_counter++; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1706 | tg_pt_gp->tg_pt_gp_valid_id = 1; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1707 | dev->t10_alua.alua_tg_pt_gps_count++; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1708 | list_add_tail(&tg_pt_gp->tg_pt_gp_list, |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1709 | &dev->t10_alua.tg_pt_gps_list); |
| 1710 | spin_unlock(&dev->t10_alua.tg_pt_gps_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | return tg_pt_gp; |
| 1714 | } |
| 1715 | |
| 1716 | int core_alua_set_tg_pt_gp_id( |
| 1717 | struct t10_alua_tg_pt_gp *tg_pt_gp, |
| 1718 | u16 tg_pt_gp_id) |
| 1719 | { |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1720 | struct se_device *dev = tg_pt_gp->tg_pt_gp_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1721 | struct t10_alua_tg_pt_gp *tg_pt_gp_tmp; |
| 1722 | u16 tg_pt_gp_id_tmp; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1723 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1724 | /* |
| 1725 | * The tg_pt_gp->tg_pt_gp_id may only be set once.. |
| 1726 | */ |
| 1727 | if (tg_pt_gp->tg_pt_gp_valid_id) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1728 | pr_warn("ALUA TG PT Group already has a valid ID," |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1729 | " ignoring request\n"); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1730 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1731 | } |
| 1732 | |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1733 | spin_lock(&dev->t10_alua.tg_pt_gps_lock); |
| 1734 | if (dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1735 | pr_err("Maximum ALUA alua_tg_pt_gps_count:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1736 | " 0x0000ffff reached\n"); |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1737 | spin_unlock(&dev->t10_alua.tg_pt_gps_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1738 | kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1739 | return -ENOSPC; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1740 | } |
| 1741 | again: |
| 1742 | tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id : |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1743 | dev->t10_alua.alua_tg_pt_gps_counter++; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1744 | |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1745 | list_for_each_entry(tg_pt_gp_tmp, &dev->t10_alua.tg_pt_gps_list, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1746 | tg_pt_gp_list) { |
| 1747 | if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1748 | if (!tg_pt_gp_id) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1749 | goto again; |
| 1750 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1751 | pr_err("ALUA Target Port Group ID: %hu already" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1752 | " exists, ignoring request\n", tg_pt_gp_id); |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1753 | spin_unlock(&dev->t10_alua.tg_pt_gps_lock); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1754 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1755 | } |
| 1756 | } |
| 1757 | |
| 1758 | tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp; |
| 1759 | tg_pt_gp->tg_pt_gp_valid_id = 1; |
| 1760 | list_add_tail(&tg_pt_gp->tg_pt_gp_list, |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1761 | &dev->t10_alua.tg_pt_gps_list); |
| 1762 | dev->t10_alua.alua_tg_pt_gps_count++; |
| 1763 | spin_unlock(&dev->t10_alua.tg_pt_gps_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1764 | |
| 1765 | return 0; |
| 1766 | } |
| 1767 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1768 | void core_alua_free_tg_pt_gp( |
| 1769 | struct t10_alua_tg_pt_gp *tg_pt_gp) |
| 1770 | { |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1771 | struct se_device *dev = tg_pt_gp->tg_pt_gp_dev; |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1772 | struct se_lun *lun, *next; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1773 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1774 | /* |
| 1775 | * Once we have reached this point, config_item_put() has already |
| 1776 | * been called from target_core_alua_drop_tg_pt_gp(). |
| 1777 | * |
| 1778 | * Here we remove *tg_pt_gp from the global list so that |
Hannes Reinecke | f1ae05d | 2013-11-19 09:07:47 +0100 | [diff] [blame] | 1779 | * no associations *OR* explicit ALUA via SET_TARGET_PORT_GROUPS |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1780 | * can be made while we are releasing struct t10_alua_tg_pt_gp. |
| 1781 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1782 | spin_lock(&dev->t10_alua.tg_pt_gps_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1783 | list_del(&tg_pt_gp->tg_pt_gp_list); |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1784 | dev->t10_alua.alua_tg_pt_gps_counter--; |
| 1785 | spin_unlock(&dev->t10_alua.tg_pt_gps_lock); |
| 1786 | |
Mike Christie | 892e4f9 | 2017-03-02 04:59:48 -0600 | [diff] [blame] | 1787 | flush_work(&tg_pt_gp->tg_pt_gp_transition_work); |
Hannes Reinecke | 9c6e164 | 2013-12-17 09:18:46 +0100 | [diff] [blame] | 1788 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1789 | /* |
| 1790 | * Allow a struct t10_alua_tg_pt_gp_member * referenced by |
| 1791 | * core_alua_get_tg_pt_gp_by_name() in |
| 1792 | * target_core_configfs.c:target_core_store_alua_tg_pt_gp() |
| 1793 | * to be released with core_alua_put_tg_pt_gp_from_name(). |
| 1794 | */ |
| 1795 | while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt)) |
| 1796 | cpu_relax(); |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1797 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1798 | /* |
| 1799 | * Release reference to struct t10_alua_tg_pt_gp from all associated |
| 1800 | * struct se_port. |
| 1801 | */ |
| 1802 | spin_lock(&tg_pt_gp->tg_pt_gp_lock); |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1803 | list_for_each_entry_safe(lun, next, |
| 1804 | &tg_pt_gp->tg_pt_gp_lun_list, lun_tg_pt_gp_link) { |
| 1805 | list_del_init(&lun->lun_tg_pt_gp_link); |
| 1806 | tg_pt_gp->tg_pt_gp_members--; |
| 1807 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1808 | spin_unlock(&tg_pt_gp->tg_pt_gp_lock); |
| 1809 | /* |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1810 | * If the passed tg_pt_gp does NOT match the default_tg_pt_gp, |
Hannes Reinecke | f1ae05d | 2013-11-19 09:07:47 +0100 | [diff] [blame] | 1811 | * assume we want to re-associate a given tg_pt_gp_mem with |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1812 | * default_tg_pt_gp. |
| 1813 | */ |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1814 | spin_lock(&lun->lun_tg_pt_gp_lock); |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1815 | if (tg_pt_gp != dev->t10_alua.default_tg_pt_gp) { |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1816 | __target_attach_tg_pt_gp(lun, |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1817 | dev->t10_alua.default_tg_pt_gp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1818 | } else |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1819 | lun->lun_tg_pt_gp = NULL; |
| 1820 | spin_unlock(&lun->lun_tg_pt_gp_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1821 | |
| 1822 | spin_lock(&tg_pt_gp->tg_pt_gp_lock); |
| 1823 | } |
| 1824 | spin_unlock(&tg_pt_gp->tg_pt_gp_lock); |
| 1825 | |
| 1826 | kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp); |
| 1827 | } |
| 1828 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1829 | static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name( |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1830 | struct se_device *dev, const char *name) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1831 | { |
| 1832 | struct t10_alua_tg_pt_gp *tg_pt_gp; |
| 1833 | struct config_item *ci; |
| 1834 | |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1835 | spin_lock(&dev->t10_alua.tg_pt_gps_lock); |
| 1836 | list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1837 | tg_pt_gp_list) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1838 | if (!tg_pt_gp->tg_pt_gp_valid_id) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1839 | continue; |
| 1840 | ci = &tg_pt_gp->tg_pt_gp_group.cg_item; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1841 | if (!strcmp(config_item_name(ci), name)) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1842 | atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt); |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1843 | spin_unlock(&dev->t10_alua.tg_pt_gps_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1844 | return tg_pt_gp; |
| 1845 | } |
| 1846 | } |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1847 | spin_unlock(&dev->t10_alua.tg_pt_gps_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1848 | |
| 1849 | return NULL; |
| 1850 | } |
| 1851 | |
| 1852 | static void core_alua_put_tg_pt_gp_from_name( |
| 1853 | struct t10_alua_tg_pt_gp *tg_pt_gp) |
| 1854 | { |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1855 | struct se_device *dev = tg_pt_gp->tg_pt_gp_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1856 | |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1857 | spin_lock(&dev->t10_alua.tg_pt_gps_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1858 | atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt); |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1859 | spin_unlock(&dev->t10_alua.tg_pt_gps_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1860 | } |
| 1861 | |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1862 | static void __target_attach_tg_pt_gp(struct se_lun *lun, |
| 1863 | struct t10_alua_tg_pt_gp *tg_pt_gp) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1864 | { |
Hannes Reinecke | 3dd348f | 2015-06-11 10:01:27 +0200 | [diff] [blame] | 1865 | struct se_dev_entry *se_deve; |
| 1866 | |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1867 | assert_spin_locked(&lun->lun_tg_pt_gp_lock); |
| 1868 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1869 | spin_lock(&tg_pt_gp->tg_pt_gp_lock); |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1870 | lun->lun_tg_pt_gp = tg_pt_gp; |
| 1871 | list_add_tail(&lun->lun_tg_pt_gp_link, &tg_pt_gp->tg_pt_gp_lun_list); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1872 | tg_pt_gp->tg_pt_gp_members++; |
Hannes Reinecke | 3dd348f | 2015-06-11 10:01:27 +0200 | [diff] [blame] | 1873 | spin_lock(&lun->lun_deve_lock); |
| 1874 | list_for_each_entry(se_deve, &lun->lun_deve_list, lun_link) |
| 1875 | core_scsi3_ua_allocate(se_deve, 0x3f, |
| 1876 | ASCQ_3FH_INQUIRY_DATA_HAS_CHANGED); |
| 1877 | spin_unlock(&lun->lun_deve_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1878 | spin_unlock(&tg_pt_gp->tg_pt_gp_lock); |
| 1879 | } |
| 1880 | |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1881 | void target_attach_tg_pt_gp(struct se_lun *lun, |
| 1882 | struct t10_alua_tg_pt_gp *tg_pt_gp) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1883 | { |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1884 | spin_lock(&lun->lun_tg_pt_gp_lock); |
| 1885 | __target_attach_tg_pt_gp(lun, tg_pt_gp); |
| 1886 | spin_unlock(&lun->lun_tg_pt_gp_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1887 | } |
| 1888 | |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1889 | static void __target_detach_tg_pt_gp(struct se_lun *lun, |
| 1890 | struct t10_alua_tg_pt_gp *tg_pt_gp) |
| 1891 | { |
| 1892 | assert_spin_locked(&lun->lun_tg_pt_gp_lock); |
| 1893 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1894 | spin_lock(&tg_pt_gp->tg_pt_gp_lock); |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1895 | list_del_init(&lun->lun_tg_pt_gp_link); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1896 | tg_pt_gp->tg_pt_gp_members--; |
| 1897 | spin_unlock(&tg_pt_gp->tg_pt_gp_lock); |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1898 | |
| 1899 | lun->lun_tg_pt_gp = NULL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1900 | } |
| 1901 | |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1902 | void target_detach_tg_pt_gp(struct se_lun *lun) |
| 1903 | { |
| 1904 | struct t10_alua_tg_pt_gp *tg_pt_gp; |
| 1905 | |
| 1906 | spin_lock(&lun->lun_tg_pt_gp_lock); |
| 1907 | tg_pt_gp = lun->lun_tg_pt_gp; |
| 1908 | if (tg_pt_gp) |
| 1909 | __target_detach_tg_pt_gp(lun, tg_pt_gp); |
| 1910 | spin_unlock(&lun->lun_tg_pt_gp_lock); |
| 1911 | } |
| 1912 | |
| 1913 | ssize_t core_alua_show_tg_pt_gp_info(struct se_lun *lun, char *page) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1914 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1915 | struct config_item *tg_pt_ci; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1916 | struct t10_alua_tg_pt_gp *tg_pt_gp; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1917 | ssize_t len = 0; |
| 1918 | |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1919 | spin_lock(&lun->lun_tg_pt_gp_lock); |
| 1920 | tg_pt_gp = lun->lun_tg_pt_gp; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1921 | if (tg_pt_gp) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1922 | tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item; |
| 1923 | len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:" |
| 1924 | " %hu\nTG Port Primary Access State: %s\nTG Port " |
| 1925 | "Primary Access Status: %s\nTG Port Secondary Access" |
| 1926 | " State: %s\nTG Port Secondary Access Status: %s\n", |
| 1927 | config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id, |
| 1928 | core_alua_dump_state(atomic_read( |
| 1929 | &tg_pt_gp->tg_pt_gp_alua_access_state)), |
| 1930 | core_alua_dump_status( |
| 1931 | tg_pt_gp->tg_pt_gp_alua_access_status), |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1932 | atomic_read(&lun->lun_tg_pt_secondary_offline) ? |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1933 | "Offline" : "None", |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1934 | core_alua_dump_status(lun->lun_tg_pt_secondary_stat)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1935 | } |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1936 | spin_unlock(&lun->lun_tg_pt_gp_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1937 | |
| 1938 | return len; |
| 1939 | } |
| 1940 | |
| 1941 | ssize_t core_alua_store_tg_pt_gp_info( |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1942 | struct se_lun *lun, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1943 | const char *page, |
| 1944 | size_t count) |
| 1945 | { |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1946 | struct se_portal_group *tpg = lun->lun_tpg; |
Nicholas Bellinger | 4cc987e | 2015-05-19 00:03:07 -0700 | [diff] [blame] | 1947 | /* |
| 1948 | * rcu_dereference_raw protected by se_lun->lun_group symlink |
| 1949 | * reference to se_device->dev_group. |
| 1950 | */ |
| 1951 | struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1952 | struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1953 | unsigned char buf[TG_PT_GROUP_NAME_BUF]; |
| 1954 | int move = 0; |
| 1955 | |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1956 | if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH || |
| 1957 | (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) |
| 1958 | return -ENODEV; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1959 | |
| 1960 | if (count > TG_PT_GROUP_NAME_BUF) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1961 | pr_err("ALUA Target Port Group alias too large!\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1962 | return -EINVAL; |
| 1963 | } |
| 1964 | memset(buf, 0, TG_PT_GROUP_NAME_BUF); |
| 1965 | memcpy(buf, page, count); |
| 1966 | /* |
| 1967 | * Any ALUA target port group alias besides "NULL" means we will be |
| 1968 | * making a new group association. |
| 1969 | */ |
| 1970 | if (strcmp(strstrip(buf), "NULL")) { |
| 1971 | /* |
| 1972 | * core_alua_get_tg_pt_gp_by_name() will increment reference to |
| 1973 | * struct t10_alua_tg_pt_gp. This reference is released with |
| 1974 | * core_alua_put_tg_pt_gp_from_name() below. |
| 1975 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1976 | tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(dev, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1977 | strstrip(buf)); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1978 | if (!tg_pt_gp_new) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1979 | return -ENODEV; |
| 1980 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1981 | |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 1982 | spin_lock(&lun->lun_tg_pt_gp_lock); |
| 1983 | tg_pt_gp = lun->lun_tg_pt_gp; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1984 | if (tg_pt_gp) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1985 | /* |
| 1986 | * Clearing an existing tg_pt_gp association, and replacing |
| 1987 | * with the default_tg_pt_gp. |
| 1988 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1989 | if (!tg_pt_gp_new) { |
| 1990 | pr_debug("Target_Core_ConfigFS: Moving" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1991 | " %s/tpgt_%hu/%s from ALUA Target Port Group:" |
| 1992 | " alua/%s, ID: %hu back to" |
| 1993 | " default_tg_pt_gp\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1994 | tpg->se_tpg_tfo->tpg_get_wwn(tpg), |
| 1995 | tpg->se_tpg_tfo->tpg_get_tag(tpg), |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1996 | config_item_name(&lun->lun_group.cg_item), |
| 1997 | config_item_name( |
| 1998 | &tg_pt_gp->tg_pt_gp_group.cg_item), |
| 1999 | tg_pt_gp->tg_pt_gp_id); |
| 2000 | |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 2001 | __target_detach_tg_pt_gp(lun, tg_pt_gp); |
| 2002 | __target_attach_tg_pt_gp(lun, |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 2003 | dev->t10_alua.default_tg_pt_gp); |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 2004 | spin_unlock(&lun->lun_tg_pt_gp_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2005 | |
| 2006 | return count; |
| 2007 | } |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 2008 | __target_detach_tg_pt_gp(lun, tg_pt_gp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2009 | move = 1; |
| 2010 | } |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 2011 | |
| 2012 | __target_attach_tg_pt_gp(lun, tg_pt_gp_new); |
| 2013 | spin_unlock(&lun->lun_tg_pt_gp_lock); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2014 | pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2015 | " Target Port Group: alua/%s, ID: %hu\n", (move) ? |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2016 | "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg), |
| 2017 | tpg->se_tpg_tfo->tpg_get_tag(tpg), |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2018 | config_item_name(&lun->lun_group.cg_item), |
| 2019 | config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item), |
| 2020 | tg_pt_gp_new->tg_pt_gp_id); |
| 2021 | |
| 2022 | core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new); |
| 2023 | return count; |
| 2024 | } |
| 2025 | |
| 2026 | ssize_t core_alua_show_access_type( |
| 2027 | struct t10_alua_tg_pt_gp *tg_pt_gp, |
| 2028 | char *page) |
| 2029 | { |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 2030 | if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA) && |
| 2031 | (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) |
| 2032 | return sprintf(page, "Implicit and Explicit\n"); |
| 2033 | else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA) |
| 2034 | return sprintf(page, "Implicit\n"); |
| 2035 | else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA) |
| 2036 | return sprintf(page, "Explicit\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2037 | else |
| 2038 | return sprintf(page, "None\n"); |
| 2039 | } |
| 2040 | |
| 2041 | ssize_t core_alua_store_access_type( |
| 2042 | struct t10_alua_tg_pt_gp *tg_pt_gp, |
| 2043 | const char *page, |
| 2044 | size_t count) |
| 2045 | { |
| 2046 | unsigned long tmp; |
| 2047 | int ret; |
| 2048 | |
Jingoo Han | 57103d7 | 2013-07-19 16:22:19 +0900 | [diff] [blame] | 2049 | ret = kstrtoul(page, 0, &tmp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2050 | if (ret < 0) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2051 | pr_err("Unable to extract alua_access_type\n"); |
Jingoo Han | 57103d7 | 2013-07-19 16:22:19 +0900 | [diff] [blame] | 2052 | return ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2053 | } |
| 2054 | if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2055 | pr_err("Illegal value for alua_access_type:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2056 | " %lu\n", tmp); |
| 2057 | return -EINVAL; |
| 2058 | } |
| 2059 | if (tmp == 3) |
| 2060 | tg_pt_gp->tg_pt_gp_alua_access_type = |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 2061 | TPGS_IMPLICIT_ALUA | TPGS_EXPLICIT_ALUA; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2062 | else if (tmp == 2) |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 2063 | tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICIT_ALUA; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2064 | else if (tmp == 1) |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 2065 | tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICIT_ALUA; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2066 | else |
| 2067 | tg_pt_gp->tg_pt_gp_alua_access_type = 0; |
| 2068 | |
| 2069 | return count; |
| 2070 | } |
| 2071 | |
| 2072 | ssize_t core_alua_show_nonop_delay_msecs( |
| 2073 | struct t10_alua_tg_pt_gp *tg_pt_gp, |
| 2074 | char *page) |
| 2075 | { |
| 2076 | return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs); |
| 2077 | } |
| 2078 | |
| 2079 | ssize_t core_alua_store_nonop_delay_msecs( |
| 2080 | struct t10_alua_tg_pt_gp *tg_pt_gp, |
| 2081 | const char *page, |
| 2082 | size_t count) |
| 2083 | { |
| 2084 | unsigned long tmp; |
| 2085 | int ret; |
| 2086 | |
Jingoo Han | 57103d7 | 2013-07-19 16:22:19 +0900 | [diff] [blame] | 2087 | ret = kstrtoul(page, 0, &tmp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2088 | if (ret < 0) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2089 | pr_err("Unable to extract nonop_delay_msecs\n"); |
Jingoo Han | 57103d7 | 2013-07-19 16:22:19 +0900 | [diff] [blame] | 2090 | return ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2091 | } |
| 2092 | if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2093 | pr_err("Passed nonop_delay_msecs: %lu, exceeds" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2094 | " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp, |
| 2095 | ALUA_MAX_NONOP_DELAY_MSECS); |
| 2096 | return -EINVAL; |
| 2097 | } |
| 2098 | tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp; |
| 2099 | |
| 2100 | return count; |
| 2101 | } |
| 2102 | |
| 2103 | ssize_t core_alua_show_trans_delay_msecs( |
| 2104 | struct t10_alua_tg_pt_gp *tg_pt_gp, |
| 2105 | char *page) |
| 2106 | { |
| 2107 | return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs); |
| 2108 | } |
| 2109 | |
| 2110 | ssize_t core_alua_store_trans_delay_msecs( |
| 2111 | struct t10_alua_tg_pt_gp *tg_pt_gp, |
| 2112 | const char *page, |
| 2113 | size_t count) |
| 2114 | { |
| 2115 | unsigned long tmp; |
| 2116 | int ret; |
| 2117 | |
Jingoo Han | 57103d7 | 2013-07-19 16:22:19 +0900 | [diff] [blame] | 2118 | ret = kstrtoul(page, 0, &tmp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2119 | if (ret < 0) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2120 | pr_err("Unable to extract trans_delay_msecs\n"); |
Jingoo Han | 57103d7 | 2013-07-19 16:22:19 +0900 | [diff] [blame] | 2121 | return ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2122 | } |
| 2123 | if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2124 | pr_err("Passed trans_delay_msecs: %lu, exceeds" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2125 | " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp, |
| 2126 | ALUA_MAX_TRANS_DELAY_MSECS); |
| 2127 | return -EINVAL; |
| 2128 | } |
| 2129 | tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp; |
| 2130 | |
| 2131 | return count; |
| 2132 | } |
| 2133 | |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 2134 | ssize_t core_alua_show_implicit_trans_secs( |
Nicholas Bellinger | 5b9a4d7 | 2012-05-16 22:02:34 -0700 | [diff] [blame] | 2135 | struct t10_alua_tg_pt_gp *tg_pt_gp, |
| 2136 | char *page) |
| 2137 | { |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 2138 | return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_implicit_trans_secs); |
Nicholas Bellinger | 5b9a4d7 | 2012-05-16 22:02:34 -0700 | [diff] [blame] | 2139 | } |
| 2140 | |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 2141 | ssize_t core_alua_store_implicit_trans_secs( |
Nicholas Bellinger | 5b9a4d7 | 2012-05-16 22:02:34 -0700 | [diff] [blame] | 2142 | struct t10_alua_tg_pt_gp *tg_pt_gp, |
| 2143 | const char *page, |
| 2144 | size_t count) |
| 2145 | { |
| 2146 | unsigned long tmp; |
| 2147 | int ret; |
| 2148 | |
Jingoo Han | 57103d7 | 2013-07-19 16:22:19 +0900 | [diff] [blame] | 2149 | ret = kstrtoul(page, 0, &tmp); |
Nicholas Bellinger | 5b9a4d7 | 2012-05-16 22:02:34 -0700 | [diff] [blame] | 2150 | if (ret < 0) { |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 2151 | pr_err("Unable to extract implicit_trans_secs\n"); |
Jingoo Han | 57103d7 | 2013-07-19 16:22:19 +0900 | [diff] [blame] | 2152 | return ret; |
Nicholas Bellinger | 5b9a4d7 | 2012-05-16 22:02:34 -0700 | [diff] [blame] | 2153 | } |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 2154 | if (tmp > ALUA_MAX_IMPLICIT_TRANS_SECS) { |
| 2155 | pr_err("Passed implicit_trans_secs: %lu, exceeds" |
| 2156 | " ALUA_MAX_IMPLICIT_TRANS_SECS: %d\n", tmp, |
| 2157 | ALUA_MAX_IMPLICIT_TRANS_SECS); |
Nicholas Bellinger | 5b9a4d7 | 2012-05-16 22:02:34 -0700 | [diff] [blame] | 2158 | return -EINVAL; |
| 2159 | } |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 2160 | tg_pt_gp->tg_pt_gp_implicit_trans_secs = (int)tmp; |
Nicholas Bellinger | 5b9a4d7 | 2012-05-16 22:02:34 -0700 | [diff] [blame] | 2161 | |
| 2162 | return count; |
| 2163 | } |
| 2164 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2165 | ssize_t core_alua_show_preferred_bit( |
| 2166 | struct t10_alua_tg_pt_gp *tg_pt_gp, |
| 2167 | char *page) |
| 2168 | { |
| 2169 | return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref); |
| 2170 | } |
| 2171 | |
| 2172 | ssize_t core_alua_store_preferred_bit( |
| 2173 | struct t10_alua_tg_pt_gp *tg_pt_gp, |
| 2174 | const char *page, |
| 2175 | size_t count) |
| 2176 | { |
| 2177 | unsigned long tmp; |
| 2178 | int ret; |
| 2179 | |
Jingoo Han | 57103d7 | 2013-07-19 16:22:19 +0900 | [diff] [blame] | 2180 | ret = kstrtoul(page, 0, &tmp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2181 | if (ret < 0) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2182 | pr_err("Unable to extract preferred ALUA value\n"); |
Jingoo Han | 57103d7 | 2013-07-19 16:22:19 +0900 | [diff] [blame] | 2183 | return ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2184 | } |
| 2185 | if ((tmp != 0) && (tmp != 1)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2186 | pr_err("Illegal value for preferred ALUA: %lu\n", tmp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2187 | return -EINVAL; |
| 2188 | } |
| 2189 | tg_pt_gp->tg_pt_gp_pref = (int)tmp; |
| 2190 | |
| 2191 | return count; |
| 2192 | } |
| 2193 | |
| 2194 | ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page) |
| 2195 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2196 | return sprintf(page, "%d\n", |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 2197 | atomic_read(&lun->lun_tg_pt_secondary_offline)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2198 | } |
| 2199 | |
| 2200 | ssize_t core_alua_store_offline_bit( |
| 2201 | struct se_lun *lun, |
| 2202 | const char *page, |
| 2203 | size_t count) |
| 2204 | { |
Nicholas Bellinger | 4cc987e | 2015-05-19 00:03:07 -0700 | [diff] [blame] | 2205 | /* |
| 2206 | * rcu_dereference_raw protected by se_lun->lun_group symlink |
| 2207 | * reference to se_device->dev_group. |
| 2208 | */ |
| 2209 | struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2210 | unsigned long tmp; |
| 2211 | int ret; |
| 2212 | |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 2213 | if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH || |
| 2214 | (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2215 | return -ENODEV; |
| 2216 | |
Jingoo Han | 57103d7 | 2013-07-19 16:22:19 +0900 | [diff] [blame] | 2217 | ret = kstrtoul(page, 0, &tmp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2218 | if (ret < 0) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2219 | pr_err("Unable to extract alua_tg_pt_offline value\n"); |
Jingoo Han | 57103d7 | 2013-07-19 16:22:19 +0900 | [diff] [blame] | 2220 | return ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2221 | } |
| 2222 | if ((tmp != 0) && (tmp != 1)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2223 | pr_err("Illegal value for alua_tg_pt_offline: %lu\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2224 | tmp); |
| 2225 | return -EINVAL; |
| 2226 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2227 | |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 2228 | ret = core_alua_set_tg_pt_secondary_state(lun, 0, (int)tmp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2229 | if (ret < 0) |
| 2230 | return -EINVAL; |
| 2231 | |
| 2232 | return count; |
| 2233 | } |
| 2234 | |
| 2235 | ssize_t core_alua_show_secondary_status( |
| 2236 | struct se_lun *lun, |
| 2237 | char *page) |
| 2238 | { |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 2239 | return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_stat); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2240 | } |
| 2241 | |
| 2242 | ssize_t core_alua_store_secondary_status( |
| 2243 | struct se_lun *lun, |
| 2244 | const char *page, |
| 2245 | size_t count) |
| 2246 | { |
| 2247 | unsigned long tmp; |
| 2248 | int ret; |
| 2249 | |
Jingoo Han | 57103d7 | 2013-07-19 16:22:19 +0900 | [diff] [blame] | 2250 | ret = kstrtoul(page, 0, &tmp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2251 | if (ret < 0) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2252 | pr_err("Unable to extract alua_tg_pt_status\n"); |
Jingoo Han | 57103d7 | 2013-07-19 16:22:19 +0900 | [diff] [blame] | 2253 | return ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2254 | } |
| 2255 | if ((tmp != ALUA_STATUS_NONE) && |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 2256 | (tmp != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) && |
| 2257 | (tmp != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2258 | pr_err("Illegal value for alua_tg_pt_status: %lu\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2259 | tmp); |
| 2260 | return -EINVAL; |
| 2261 | } |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 2262 | lun->lun_tg_pt_secondary_stat = (int)tmp; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2263 | |
| 2264 | return count; |
| 2265 | } |
| 2266 | |
| 2267 | ssize_t core_alua_show_secondary_write_metadata( |
| 2268 | struct se_lun *lun, |
| 2269 | char *page) |
| 2270 | { |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 2271 | return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_write_md); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | ssize_t core_alua_store_secondary_write_metadata( |
| 2275 | struct se_lun *lun, |
| 2276 | const char *page, |
| 2277 | size_t count) |
| 2278 | { |
| 2279 | unsigned long tmp; |
| 2280 | int ret; |
| 2281 | |
Jingoo Han | 57103d7 | 2013-07-19 16:22:19 +0900 | [diff] [blame] | 2282 | ret = kstrtoul(page, 0, &tmp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2283 | if (ret < 0) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2284 | pr_err("Unable to extract alua_tg_pt_write_md\n"); |
Jingoo Han | 57103d7 | 2013-07-19 16:22:19 +0900 | [diff] [blame] | 2285 | return ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2286 | } |
| 2287 | if ((tmp != 0) && (tmp != 1)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2288 | pr_err("Illegal value for alua_tg_pt_write_md:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2289 | " %lu\n", tmp); |
| 2290 | return -EINVAL; |
| 2291 | } |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 2292 | lun->lun_tg_pt_secondary_write_md = (int)tmp; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2293 | |
| 2294 | return count; |
| 2295 | } |
| 2296 | |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 2297 | int core_setup_alua(struct se_device *dev) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2298 | { |
Andy Grover | a354170 | 2015-05-19 14:44:41 -0700 | [diff] [blame] | 2299 | if (!(dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH) && |
Christoph Hellwig | c87fbd5 | 2012-10-10 17:37:16 -0400 | [diff] [blame] | 2300 | !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) { |
| 2301 | struct t10_alua_lu_gp_member *lu_gp_mem; |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 2302 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2303 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2304 | * Associate this struct se_device with the default ALUA |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2305 | * LUN Group. |
| 2306 | */ |
| 2307 | lu_gp_mem = core_alua_allocate_lu_gp_mem(dev); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2308 | if (IS_ERR(lu_gp_mem)) |
| 2309 | return PTR_ERR(lu_gp_mem); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2310 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2311 | spin_lock(&lu_gp_mem->lu_gp_mem_lock); |
| 2312 | __core_alua_attach_lu_gp_mem(lu_gp_mem, |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2313 | default_lu_gp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2314 | spin_unlock(&lu_gp_mem->lu_gp_mem_lock); |
| 2315 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2316 | pr_debug("%s: Adding to default ALUA LU Group:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2317 | " core/alua/lu_gps/default_lu_gp\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2318 | dev->transport->name); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2319 | } |
| 2320 | |
| 2321 | return 0; |
| 2322 | } |