1c66ac9dbSNicholas Bellinger /******************************************************************************* 2c66ac9dbSNicholas Bellinger * Filename: target_core_pr.c 3c66ac9dbSNicholas Bellinger * 4c66ac9dbSNicholas Bellinger * This file contains SPC-3 compliant persistent reservations and 5c66ac9dbSNicholas Bellinger * legacy SPC-2 reservations with compatible reservation handling (CRH=1) 6c66ac9dbSNicholas Bellinger * 74c76251eSNicholas Bellinger * (c) Copyright 2009-2013 Datera, Inc. 8c66ac9dbSNicholas Bellinger * 9c66ac9dbSNicholas Bellinger * Nicholas A. Bellinger <nab@kernel.org> 10c66ac9dbSNicholas Bellinger * 11c66ac9dbSNicholas Bellinger * This program is free software; you can redistribute it and/or modify 12c66ac9dbSNicholas Bellinger * it under the terms of the GNU General Public License as published by 13c66ac9dbSNicholas Bellinger * the Free Software Foundation; either version 2 of the License, or 14c66ac9dbSNicholas Bellinger * (at your option) any later version. 15c66ac9dbSNicholas Bellinger * 16c66ac9dbSNicholas Bellinger * This program is distributed in the hope that it will be useful, 17c66ac9dbSNicholas Bellinger * but WITHOUT ANY WARRANTY; without even the implied warranty of 18c66ac9dbSNicholas Bellinger * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19c66ac9dbSNicholas Bellinger * GNU General Public License for more details. 20c66ac9dbSNicholas Bellinger * 21c66ac9dbSNicholas Bellinger * You should have received a copy of the GNU General Public License 22c66ac9dbSNicholas Bellinger * along with this program; if not, write to the Free Software 23c66ac9dbSNicholas Bellinger * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 24c66ac9dbSNicholas Bellinger * 25c66ac9dbSNicholas Bellinger ******************************************************************************/ 26c66ac9dbSNicholas Bellinger 27c66ac9dbSNicholas Bellinger #include <linux/slab.h> 28c66ac9dbSNicholas Bellinger #include <linux/spinlock.h> 29c66ac9dbSNicholas Bellinger #include <linux/list.h> 300e9b10a9SAl Viro #include <linux/file.h> 31c66ac9dbSNicholas Bellinger #include <scsi/scsi.h> 32c66ac9dbSNicholas Bellinger #include <scsi/scsi_cmnd.h> 33c66ac9dbSNicholas Bellinger #include <asm/unaligned.h> 34c66ac9dbSNicholas Bellinger 35c66ac9dbSNicholas Bellinger #include <target/target_core_base.h> 36c4795fb2SChristoph Hellwig #include <target/target_core_backend.h> 37c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h> 38c66ac9dbSNicholas Bellinger #include <target/target_core_configfs.h> 39c66ac9dbSNicholas Bellinger 40e26d99aeSChristoph Hellwig #include "target_core_internal.h" 41c66ac9dbSNicholas Bellinger #include "target_core_pr.h" 42c66ac9dbSNicholas Bellinger #include "target_core_ua.h" 43c66ac9dbSNicholas Bellinger 44c66ac9dbSNicholas Bellinger /* 45c66ac9dbSNicholas Bellinger * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT) 46c66ac9dbSNicholas Bellinger */ 47c66ac9dbSNicholas Bellinger struct pr_transport_id_holder { 48c66ac9dbSNicholas Bellinger int dest_local_nexus; 49c66ac9dbSNicholas Bellinger struct t10_pr_registration *dest_pr_reg; 50c66ac9dbSNicholas Bellinger struct se_portal_group *dest_tpg; 51c66ac9dbSNicholas Bellinger struct se_node_acl *dest_node_acl; 52c66ac9dbSNicholas Bellinger struct se_dev_entry *dest_se_deve; 53c66ac9dbSNicholas Bellinger struct list_head dest_list; 54c66ac9dbSNicholas Bellinger }; 55c66ac9dbSNicholas Bellinger 56d2843c17SAndy Grover void core_pr_dump_initiator_port( 57c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, 58c66ac9dbSNicholas Bellinger char *buf, 59c66ac9dbSNicholas Bellinger u32 size) 60c66ac9dbSNicholas Bellinger { 616708bb27SAndy Grover if (!pr_reg->isid_present_at_reg) 62d2843c17SAndy Grover buf[0] = '\0'; 63c66ac9dbSNicholas Bellinger 64d2843c17SAndy Grover snprintf(buf, size, ",i,0x%s", pr_reg->pr_reg_isid); 65c66ac9dbSNicholas Bellinger } 66c66ac9dbSNicholas Bellinger 6733ce6a87SAndy Grover enum register_type { 6833ce6a87SAndy Grover REGISTER, 6933ce6a87SAndy Grover REGISTER_AND_IGNORE_EXISTING_KEY, 7033ce6a87SAndy Grover REGISTER_AND_MOVE, 7133ce6a87SAndy Grover }; 7233ce6a87SAndy Grover 7333ce6a87SAndy Grover enum preempt_type { 7433ce6a87SAndy Grover PREEMPT, 7533ce6a87SAndy Grover PREEMPT_AND_ABORT, 7633ce6a87SAndy Grover }; 7733ce6a87SAndy Grover 78c66ac9dbSNicholas Bellinger static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *, 796c3c9baaSNicholas Bellinger struct t10_pr_registration *, int, int); 80c66ac9dbSNicholas Bellinger 81de103c93SChristoph Hellwig static sense_reason_t 82de103c93SChristoph Hellwig target_scsi2_reservation_check(struct se_cmd *cmd) 83c66ac9dbSNicholas Bellinger { 84d977f437SChristoph Hellwig struct se_device *dev = cmd->se_dev; 85d977f437SChristoph Hellwig struct se_session *sess = cmd->se_sess; 86d977f437SChristoph Hellwig 87d977f437SChristoph Hellwig switch (cmd->t_task_cdb[0]) { 88c66ac9dbSNicholas Bellinger case INQUIRY: 89c66ac9dbSNicholas Bellinger case RELEASE: 90c66ac9dbSNicholas Bellinger case RELEASE_10: 91c66ac9dbSNicholas Bellinger return 0; 92c66ac9dbSNicholas Bellinger default: 93d977f437SChristoph Hellwig break; 94c66ac9dbSNicholas Bellinger } 95c66ac9dbSNicholas Bellinger 96d977f437SChristoph Hellwig if (!dev->dev_reserved_node_acl || !sess) 97c66ac9dbSNicholas Bellinger return 0; 98c66ac9dbSNicholas Bellinger 99d977f437SChristoph Hellwig if (dev->dev_reserved_node_acl != sess->se_node_acl) 100de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 101c66ac9dbSNicholas Bellinger 102d977f437SChristoph Hellwig if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) { 103d977f437SChristoph Hellwig if (dev->dev_res_bin_isid != sess->sess_bin_isid) 104de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 105d977f437SChristoph Hellwig } 106d977f437SChristoph Hellwig 107d977f437SChristoph Hellwig return 0; 108c66ac9dbSNicholas Bellinger } 109c66ac9dbSNicholas Bellinger 110eacac00cSChristoph Hellwig static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *, 111eacac00cSChristoph Hellwig struct se_node_acl *, struct se_session *); 112eacac00cSChristoph Hellwig static void core_scsi3_put_pr_reg(struct t10_pr_registration *); 113eacac00cSChristoph Hellwig 114087a03b3SNicholas Bellinger static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd) 115eacac00cSChristoph Hellwig { 116eacac00cSChristoph Hellwig struct se_session *se_sess = cmd->se_sess; 1170fd97ccfSChristoph Hellwig struct se_device *dev = cmd->se_dev; 118eacac00cSChristoph Hellwig struct t10_pr_registration *pr_reg; 1190fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 120eacac00cSChristoph Hellwig int conflict = 0; 121eacac00cSChristoph Hellwig 122eacac00cSChristoph Hellwig pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl, 123eacac00cSChristoph Hellwig se_sess); 124eacac00cSChristoph Hellwig if (pr_reg) { 125eacac00cSChristoph Hellwig /* 126eacac00cSChristoph Hellwig * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE 127eacac00cSChristoph Hellwig * behavior 128eacac00cSChristoph Hellwig * 129eacac00cSChristoph Hellwig * A RESERVE(6) or RESERVE(10) command shall complete with GOOD 130eacac00cSChristoph Hellwig * status, but no reservation shall be established and the 131eacac00cSChristoph Hellwig * persistent reservation shall not be changed, if the command 132eacac00cSChristoph Hellwig * is received from a) and b) below. 133eacac00cSChristoph Hellwig * 134eacac00cSChristoph Hellwig * A RELEASE(6) or RELEASE(10) command shall complete with GOOD 135eacac00cSChristoph Hellwig * status, but the persistent reservation shall not be released, 136eacac00cSChristoph Hellwig * if the command is received from a) and b) 137eacac00cSChristoph Hellwig * 138eacac00cSChristoph Hellwig * a) An I_T nexus that is a persistent reservation holder; or 139eacac00cSChristoph Hellwig * b) An I_T nexus that is registered if a registrants only or 140eacac00cSChristoph Hellwig * all registrants type persistent reservation is present. 141eacac00cSChristoph Hellwig * 142eacac00cSChristoph Hellwig * In all other cases, a RESERVE(6) command, RESERVE(10) command, 143eacac00cSChristoph Hellwig * RELEASE(6) command, or RELEASE(10) command shall be processed 144eacac00cSChristoph Hellwig * as defined in SPC-2. 145eacac00cSChristoph Hellwig */ 146eacac00cSChristoph Hellwig if (pr_reg->pr_res_holder) { 147eacac00cSChristoph Hellwig core_scsi3_put_pr_reg(pr_reg); 148087a03b3SNicholas Bellinger return 1; 149eacac00cSChristoph Hellwig } 150eacac00cSChristoph Hellwig if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) || 151eacac00cSChristoph Hellwig (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) || 152eacac00cSChristoph Hellwig (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || 153eacac00cSChristoph Hellwig (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) { 154eacac00cSChristoph Hellwig core_scsi3_put_pr_reg(pr_reg); 155087a03b3SNicholas Bellinger return 1; 156eacac00cSChristoph Hellwig } 157eacac00cSChristoph Hellwig core_scsi3_put_pr_reg(pr_reg); 158eacac00cSChristoph Hellwig conflict = 1; 159eacac00cSChristoph Hellwig } else { 160eacac00cSChristoph Hellwig /* 161eacac00cSChristoph Hellwig * Following spc2r20 5.5.1 Reservations overview: 162eacac00cSChristoph Hellwig * 163eacac00cSChristoph Hellwig * If a logical unit has executed a PERSISTENT RESERVE OUT 164eacac00cSChristoph Hellwig * command with the REGISTER or the REGISTER AND IGNORE 165eacac00cSChristoph Hellwig * EXISTING KEY service action and is still registered by any 166eacac00cSChristoph Hellwig * initiator, all RESERVE commands and all RELEASE commands 167eacac00cSChristoph Hellwig * regardless of initiator shall conflict and shall terminate 168eacac00cSChristoph Hellwig * with a RESERVATION CONFLICT status. 169eacac00cSChristoph Hellwig */ 170eacac00cSChristoph Hellwig spin_lock(&pr_tmpl->registration_lock); 171eacac00cSChristoph Hellwig conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1; 172eacac00cSChristoph Hellwig spin_unlock(&pr_tmpl->registration_lock); 173eacac00cSChristoph Hellwig } 174eacac00cSChristoph Hellwig 175eacac00cSChristoph Hellwig if (conflict) { 176eacac00cSChristoph Hellwig pr_err("Received legacy SPC-2 RESERVE/RELEASE" 177eacac00cSChristoph Hellwig " while active SPC-3 registrations exist," 178eacac00cSChristoph Hellwig " returning RESERVATION_CONFLICT\n"); 179087a03b3SNicholas Bellinger return -EBUSY; 180eacac00cSChristoph Hellwig } 181eacac00cSChristoph Hellwig 182087a03b3SNicholas Bellinger return 0; 183eacac00cSChristoph Hellwig } 184eacac00cSChristoph Hellwig 185de103c93SChristoph Hellwig sense_reason_t 186de103c93SChristoph Hellwig target_scsi2_reservation_release(struct se_cmd *cmd) 187c66ac9dbSNicholas Bellinger { 188c66ac9dbSNicholas Bellinger struct se_device *dev = cmd->se_dev; 189c66ac9dbSNicholas Bellinger struct se_session *sess = cmd->se_sess; 190609234e3SWei Yongjun struct se_portal_group *tpg; 191de103c93SChristoph Hellwig int rc; 192c66ac9dbSNicholas Bellinger 193609234e3SWei Yongjun if (!sess || !sess->se_tpg) 194d29a5b6aSChristoph Hellwig goto out; 195087a03b3SNicholas Bellinger rc = target_check_scsi2_reservation_conflict(cmd); 196087a03b3SNicholas Bellinger if (rc == 1) 197d29a5b6aSChristoph Hellwig goto out; 198de103c93SChristoph Hellwig if (rc < 0) 199de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 200c66ac9dbSNicholas Bellinger 201c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 202d29a5b6aSChristoph Hellwig if (!dev->dev_reserved_node_acl || !sess) 203d29a5b6aSChristoph Hellwig goto out_unlock; 204c66ac9dbSNicholas Bellinger 205d29a5b6aSChristoph Hellwig if (dev->dev_reserved_node_acl != sess->se_node_acl) 206d29a5b6aSChristoph Hellwig goto out_unlock; 207d29a5b6aSChristoph Hellwig 208edc318d9SBernhard Kohl if (dev->dev_res_bin_isid != sess->sess_bin_isid) 209edc318d9SBernhard Kohl goto out_unlock; 210edc318d9SBernhard Kohl 211c66ac9dbSNicholas Bellinger dev->dev_reserved_node_acl = NULL; 2120fd97ccfSChristoph Hellwig dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS; 2130fd97ccfSChristoph Hellwig if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) { 214c66ac9dbSNicholas Bellinger dev->dev_res_bin_isid = 0; 2150fd97ccfSChristoph Hellwig dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS_WITH_ISID; 216c66ac9dbSNicholas Bellinger } 217609234e3SWei Yongjun tpg = sess->se_tpg; 2186708bb27SAndy Grover pr_debug("SCSI-2 Released reservation for %s LUN: %u ->" 219e3d6f909SAndy Grover " MAPPED LUN: %u for %s\n", tpg->se_tpg_tfo->get_fabric_name(), 220e3d6f909SAndy Grover cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun, 221c66ac9dbSNicholas Bellinger sess->se_node_acl->initiatorname); 222c66ac9dbSNicholas Bellinger 223d29a5b6aSChristoph Hellwig out_unlock: 224d29a5b6aSChristoph Hellwig spin_unlock(&dev->dev_reservation_lock); 225d29a5b6aSChristoph Hellwig out: 2266bb35e00SChristoph Hellwig target_complete_cmd(cmd, GOOD); 227de103c93SChristoph Hellwig return 0; 228c66ac9dbSNicholas Bellinger } 229c66ac9dbSNicholas Bellinger 230de103c93SChristoph Hellwig sense_reason_t 231de103c93SChristoph Hellwig target_scsi2_reservation_reserve(struct se_cmd *cmd) 232c66ac9dbSNicholas Bellinger { 233c66ac9dbSNicholas Bellinger struct se_device *dev = cmd->se_dev; 234c66ac9dbSNicholas Bellinger struct se_session *sess = cmd->se_sess; 235609234e3SWei Yongjun struct se_portal_group *tpg; 236de103c93SChristoph Hellwig sense_reason_t ret = 0; 237de103c93SChristoph Hellwig int rc; 238c66ac9dbSNicholas Bellinger 239a1d8b49aSAndy Grover if ((cmd->t_task_cdb[1] & 0x01) && 240a1d8b49aSAndy Grover (cmd->t_task_cdb[1] & 0x02)) { 2416708bb27SAndy Grover pr_err("LongIO and Obselete Bits set, returning" 242c66ac9dbSNicholas Bellinger " ILLEGAL_REQUEST\n"); 243de103c93SChristoph Hellwig return TCM_UNSUPPORTED_SCSI_OPCODE; 244c66ac9dbSNicholas Bellinger } 245c66ac9dbSNicholas Bellinger /* 246c66ac9dbSNicholas Bellinger * This is currently the case for target_core_mod passthrough struct se_cmd 247c66ac9dbSNicholas Bellinger * ops 248c66ac9dbSNicholas Bellinger */ 249609234e3SWei Yongjun if (!sess || !sess->se_tpg) 250d29a5b6aSChristoph Hellwig goto out; 251087a03b3SNicholas Bellinger rc = target_check_scsi2_reservation_conflict(cmd); 252087a03b3SNicholas Bellinger if (rc == 1) 253d29a5b6aSChristoph Hellwig goto out; 254c66ac9dbSNicholas Bellinger 255de103c93SChristoph Hellwig if (rc < 0) 256de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 257de103c93SChristoph Hellwig 258609234e3SWei Yongjun tpg = sess->se_tpg; 259c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 260c66ac9dbSNicholas Bellinger if (dev->dev_reserved_node_acl && 261c66ac9dbSNicholas Bellinger (dev->dev_reserved_node_acl != sess->se_node_acl)) { 2626708bb27SAndy Grover pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n", 263e3d6f909SAndy Grover tpg->se_tpg_tfo->get_fabric_name()); 2646708bb27SAndy Grover pr_err("Original reserver LUN: %u %s\n", 265e3d6f909SAndy Grover cmd->se_lun->unpacked_lun, 266c66ac9dbSNicholas Bellinger dev->dev_reserved_node_acl->initiatorname); 2676708bb27SAndy Grover pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u" 268e3d6f909SAndy Grover " from %s \n", cmd->se_lun->unpacked_lun, 269c66ac9dbSNicholas Bellinger cmd->se_deve->mapped_lun, 270c66ac9dbSNicholas Bellinger sess->se_node_acl->initiatorname); 271de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 272d29a5b6aSChristoph Hellwig goto out_unlock; 273c66ac9dbSNicholas Bellinger } 274c66ac9dbSNicholas Bellinger 275c66ac9dbSNicholas Bellinger dev->dev_reserved_node_acl = sess->se_node_acl; 2760fd97ccfSChristoph Hellwig dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS; 277c66ac9dbSNicholas Bellinger if (sess->sess_bin_isid != 0) { 278c66ac9dbSNicholas Bellinger dev->dev_res_bin_isid = sess->sess_bin_isid; 2790fd97ccfSChristoph Hellwig dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS_WITH_ISID; 280c66ac9dbSNicholas Bellinger } 2816708bb27SAndy Grover pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u" 282e3d6f909SAndy Grover " for %s\n", tpg->se_tpg_tfo->get_fabric_name(), 283e3d6f909SAndy Grover cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun, 284c66ac9dbSNicholas Bellinger sess->se_node_acl->initiatorname); 285c66ac9dbSNicholas Bellinger 286d29a5b6aSChristoph Hellwig out_unlock: 287d29a5b6aSChristoph Hellwig spin_unlock(&dev->dev_reservation_lock); 288d29a5b6aSChristoph Hellwig out: 2896bb35e00SChristoph Hellwig if (!ret) 2906bb35e00SChristoph Hellwig target_complete_cmd(cmd, GOOD); 291d29a5b6aSChristoph Hellwig return ret; 292c66ac9dbSNicholas Bellinger } 293c66ac9dbSNicholas Bellinger 294c66ac9dbSNicholas Bellinger 295c66ac9dbSNicholas Bellinger /* 296c66ac9dbSNicholas Bellinger * Begin SPC-3/SPC-4 Persistent Reservations emulation support 297c66ac9dbSNicholas Bellinger * 298c66ac9dbSNicholas Bellinger * This function is called by those initiator ports who are *NOT* 299c66ac9dbSNicholas Bellinger * the active PR reservation holder when a reservation is present. 300c66ac9dbSNicholas Bellinger */ 301c66ac9dbSNicholas Bellinger static int core_scsi3_pr_seq_non_holder( 302c66ac9dbSNicholas Bellinger struct se_cmd *cmd, 303c66ac9dbSNicholas Bellinger u32 pr_reg_type) 304c66ac9dbSNicholas Bellinger { 305d977f437SChristoph Hellwig unsigned char *cdb = cmd->t_task_cdb; 306c66ac9dbSNicholas Bellinger struct se_dev_entry *se_deve; 307e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 308c66ac9dbSNicholas Bellinger int other_cdb = 0, ignore_reg; 309c66ac9dbSNicholas Bellinger int registered_nexus = 0, ret = 1; /* Conflict by default */ 310c66ac9dbSNicholas Bellinger int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */ 311c66ac9dbSNicholas Bellinger int we = 0; /* Write Exclusive */ 312c66ac9dbSNicholas Bellinger int legacy = 0; /* Act like a legacy device and return 313c66ac9dbSNicholas Bellinger * RESERVATION CONFLICT on some CDBs */ 314c66ac9dbSNicholas Bellinger 315f2083241SJörn Engel se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun]; 316c66ac9dbSNicholas Bellinger /* 317c66ac9dbSNicholas Bellinger * Determine if the registration should be ignored due to 318d977f437SChristoph Hellwig * non-matching ISIDs in target_scsi3_pr_reservation_check(). 319c66ac9dbSNicholas Bellinger */ 320c66ac9dbSNicholas Bellinger ignore_reg = (pr_reg_type & 0x80000000); 321c66ac9dbSNicholas Bellinger if (ignore_reg) 322c66ac9dbSNicholas Bellinger pr_reg_type &= ~0x80000000; 323c66ac9dbSNicholas Bellinger 324c66ac9dbSNicholas Bellinger switch (pr_reg_type) { 325c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE: 326c66ac9dbSNicholas Bellinger we = 1; 327c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS: 328c66ac9dbSNicholas Bellinger /* 329c66ac9dbSNicholas Bellinger * Some commands are only allowed for the persistent reservation 330c66ac9dbSNicholas Bellinger * holder. 331c66ac9dbSNicholas Bellinger */ 332c66ac9dbSNicholas Bellinger if ((se_deve->def_pr_registered) && !(ignore_reg)) 333c66ac9dbSNicholas Bellinger registered_nexus = 1; 334c66ac9dbSNicholas Bellinger break; 335c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_REGONLY: 336c66ac9dbSNicholas Bellinger we = 1; 337c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY: 338c66ac9dbSNicholas Bellinger /* 339c66ac9dbSNicholas Bellinger * Some commands are only allowed for registered I_T Nexuses. 340c66ac9dbSNicholas Bellinger */ 341c66ac9dbSNicholas Bellinger reg_only = 1; 342c66ac9dbSNicholas Bellinger if ((se_deve->def_pr_registered) && !(ignore_reg)) 343c66ac9dbSNicholas Bellinger registered_nexus = 1; 344c66ac9dbSNicholas Bellinger break; 345c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_ALLREG: 346c66ac9dbSNicholas Bellinger we = 1; 347c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG: 348c66ac9dbSNicholas Bellinger /* 349c66ac9dbSNicholas Bellinger * Each registered I_T Nexus is a reservation holder. 350c66ac9dbSNicholas Bellinger */ 351c66ac9dbSNicholas Bellinger all_reg = 1; 352c66ac9dbSNicholas Bellinger if ((se_deve->def_pr_registered) && !(ignore_reg)) 353c66ac9dbSNicholas Bellinger registered_nexus = 1; 354c66ac9dbSNicholas Bellinger break; 355c66ac9dbSNicholas Bellinger default: 356e3d6f909SAndy Grover return -EINVAL; 357c66ac9dbSNicholas Bellinger } 358c66ac9dbSNicholas Bellinger /* 359c66ac9dbSNicholas Bellinger * Referenced from spc4r17 table 45 for *NON* PR holder access 360c66ac9dbSNicholas Bellinger */ 361c66ac9dbSNicholas Bellinger switch (cdb[0]) { 362c66ac9dbSNicholas Bellinger case SECURITY_PROTOCOL_IN: 363c66ac9dbSNicholas Bellinger if (registered_nexus) 364c66ac9dbSNicholas Bellinger return 0; 365c66ac9dbSNicholas Bellinger ret = (we) ? 0 : 1; 366c66ac9dbSNicholas Bellinger break; 367c66ac9dbSNicholas Bellinger case MODE_SENSE: 368c66ac9dbSNicholas Bellinger case MODE_SENSE_10: 369c66ac9dbSNicholas Bellinger case READ_ATTRIBUTE: 370c66ac9dbSNicholas Bellinger case READ_BUFFER: 371c66ac9dbSNicholas Bellinger case RECEIVE_DIAGNOSTIC: 372c66ac9dbSNicholas Bellinger if (legacy) { 373c66ac9dbSNicholas Bellinger ret = 1; 374c66ac9dbSNicholas Bellinger break; 375c66ac9dbSNicholas Bellinger } 376c66ac9dbSNicholas Bellinger if (registered_nexus) { 377c66ac9dbSNicholas Bellinger ret = 0; 378c66ac9dbSNicholas Bellinger break; 379c66ac9dbSNicholas Bellinger } 380c66ac9dbSNicholas Bellinger ret = (we) ? 0 : 1; /* Allowed Write Exclusive */ 381c66ac9dbSNicholas Bellinger break; 382c66ac9dbSNicholas Bellinger case PERSISTENT_RESERVE_OUT: 383c66ac9dbSNicholas Bellinger /* 384c66ac9dbSNicholas Bellinger * This follows PERSISTENT_RESERVE_OUT service actions that 385c66ac9dbSNicholas Bellinger * are allowed in the presence of various reservations. 386c66ac9dbSNicholas Bellinger * See spc4r17, table 46 387c66ac9dbSNicholas Bellinger */ 388c66ac9dbSNicholas Bellinger switch (cdb[1] & 0x1f) { 389c66ac9dbSNicholas Bellinger case PRO_CLEAR: 390c66ac9dbSNicholas Bellinger case PRO_PREEMPT: 391c66ac9dbSNicholas Bellinger case PRO_PREEMPT_AND_ABORT: 392c66ac9dbSNicholas Bellinger ret = (registered_nexus) ? 0 : 1; 393c66ac9dbSNicholas Bellinger break; 394c66ac9dbSNicholas Bellinger case PRO_REGISTER: 395c66ac9dbSNicholas Bellinger case PRO_REGISTER_AND_IGNORE_EXISTING_KEY: 396c66ac9dbSNicholas Bellinger ret = 0; 397c66ac9dbSNicholas Bellinger break; 398c66ac9dbSNicholas Bellinger case PRO_REGISTER_AND_MOVE: 399c66ac9dbSNicholas Bellinger case PRO_RESERVE: 400c66ac9dbSNicholas Bellinger ret = 1; 401c66ac9dbSNicholas Bellinger break; 402c66ac9dbSNicholas Bellinger case PRO_RELEASE: 403c66ac9dbSNicholas Bellinger ret = (registered_nexus) ? 0 : 1; 404c66ac9dbSNicholas Bellinger break; 405c66ac9dbSNicholas Bellinger default: 4066708bb27SAndy Grover pr_err("Unknown PERSISTENT_RESERVE_OUT service" 407c66ac9dbSNicholas Bellinger " action: 0x%02x\n", cdb[1] & 0x1f); 408e3d6f909SAndy Grover return -EINVAL; 409c66ac9dbSNicholas Bellinger } 410c66ac9dbSNicholas Bellinger break; 411c66ac9dbSNicholas Bellinger case RELEASE: 412c66ac9dbSNicholas Bellinger case RELEASE_10: 413eacac00cSChristoph Hellwig /* Handled by CRH=1 in target_scsi2_reservation_release() */ 414c66ac9dbSNicholas Bellinger ret = 0; 415c66ac9dbSNicholas Bellinger break; 416c66ac9dbSNicholas Bellinger case RESERVE: 417c66ac9dbSNicholas Bellinger case RESERVE_10: 418eacac00cSChristoph Hellwig /* Handled by CRH=1 in target_scsi2_reservation_reserve() */ 419c66ac9dbSNicholas Bellinger ret = 0; 420c66ac9dbSNicholas Bellinger break; 421c66ac9dbSNicholas Bellinger case TEST_UNIT_READY: 422c66ac9dbSNicholas Bellinger ret = (legacy) ? 1 : 0; /* Conflict for legacy */ 423c66ac9dbSNicholas Bellinger break; 424c66ac9dbSNicholas Bellinger case MAINTENANCE_IN: 425c66ac9dbSNicholas Bellinger switch (cdb[1] & 0x1f) { 426c66ac9dbSNicholas Bellinger case MI_MANAGEMENT_PROTOCOL_IN: 427c66ac9dbSNicholas Bellinger if (registered_nexus) { 428c66ac9dbSNicholas Bellinger ret = 0; 429c66ac9dbSNicholas Bellinger break; 430c66ac9dbSNicholas Bellinger } 431c66ac9dbSNicholas Bellinger ret = (we) ? 0 : 1; /* Allowed Write Exclusive */ 432c66ac9dbSNicholas Bellinger break; 433c66ac9dbSNicholas Bellinger case MI_REPORT_SUPPORTED_OPERATION_CODES: 434c66ac9dbSNicholas Bellinger case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS: 435c66ac9dbSNicholas Bellinger if (legacy) { 436c66ac9dbSNicholas Bellinger ret = 1; 437c66ac9dbSNicholas Bellinger break; 438c66ac9dbSNicholas Bellinger } 439c66ac9dbSNicholas Bellinger if (registered_nexus) { 440c66ac9dbSNicholas Bellinger ret = 0; 441c66ac9dbSNicholas Bellinger break; 442c66ac9dbSNicholas Bellinger } 443c66ac9dbSNicholas Bellinger ret = (we) ? 0 : 1; /* Allowed Write Exclusive */ 444c66ac9dbSNicholas Bellinger break; 445c66ac9dbSNicholas Bellinger case MI_REPORT_ALIASES: 446c66ac9dbSNicholas Bellinger case MI_REPORT_IDENTIFYING_INFORMATION: 447c66ac9dbSNicholas Bellinger case MI_REPORT_PRIORITY: 448c66ac9dbSNicholas Bellinger case MI_REPORT_TARGET_PGS: 449c66ac9dbSNicholas Bellinger case MI_REPORT_TIMESTAMP: 450c66ac9dbSNicholas Bellinger ret = 0; /* Allowed */ 451c66ac9dbSNicholas Bellinger break; 452c66ac9dbSNicholas Bellinger default: 4536708bb27SAndy Grover pr_err("Unknown MI Service Action: 0x%02x\n", 454c66ac9dbSNicholas Bellinger (cdb[1] & 0x1f)); 455e3d6f909SAndy Grover return -EINVAL; 456c66ac9dbSNicholas Bellinger } 457c66ac9dbSNicholas Bellinger break; 458c66ac9dbSNicholas Bellinger case ACCESS_CONTROL_IN: 459c66ac9dbSNicholas Bellinger case ACCESS_CONTROL_OUT: 460c66ac9dbSNicholas Bellinger case INQUIRY: 461c66ac9dbSNicholas Bellinger case LOG_SENSE: 462c66ac9dbSNicholas Bellinger case READ_MEDIA_SERIAL_NUMBER: 463c66ac9dbSNicholas Bellinger case REPORT_LUNS: 464c66ac9dbSNicholas Bellinger case REQUEST_SENSE: 4656816966aSMarco Sanvido case PERSISTENT_RESERVE_IN: 466c66ac9dbSNicholas Bellinger ret = 0; /*/ Allowed CDBs */ 467c66ac9dbSNicholas Bellinger break; 468c66ac9dbSNicholas Bellinger default: 469c66ac9dbSNicholas Bellinger other_cdb = 1; 470c66ac9dbSNicholas Bellinger break; 471c66ac9dbSNicholas Bellinger } 472c66ac9dbSNicholas Bellinger /* 47325985edcSLucas De Marchi * Case where the CDB is explicitly allowed in the above switch 474c66ac9dbSNicholas Bellinger * statement. 475c66ac9dbSNicholas Bellinger */ 4766708bb27SAndy Grover if (!ret && !other_cdb) { 477125d0119SHannes Reinecke pr_debug("Allowing explicit CDB: 0x%02x for %s" 478c66ac9dbSNicholas Bellinger " reservation holder\n", cdb[0], 479c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(pr_reg_type)); 4808b1e1244SAndy Grover 481c66ac9dbSNicholas Bellinger return ret; 482c66ac9dbSNicholas Bellinger } 483c66ac9dbSNicholas Bellinger /* 484c66ac9dbSNicholas Bellinger * Check if write exclusive initiator ports *NOT* holding the 485c66ac9dbSNicholas Bellinger * WRITE_EXCLUSIVE_* reservation. 486c66ac9dbSNicholas Bellinger */ 487ee1b1b9cSAndy Grover if (we && !registered_nexus) { 488c66ac9dbSNicholas Bellinger if (cmd->data_direction == DMA_TO_DEVICE) { 489c66ac9dbSNicholas Bellinger /* 490c66ac9dbSNicholas Bellinger * Conflict for write exclusive 491c66ac9dbSNicholas Bellinger */ 4926708bb27SAndy Grover pr_debug("%s Conflict for unregistered nexus" 493c66ac9dbSNicholas Bellinger " %s CDB: 0x%02x to %s reservation\n", 494c66ac9dbSNicholas Bellinger transport_dump_cmd_direction(cmd), 495c66ac9dbSNicholas Bellinger se_sess->se_node_acl->initiatorname, cdb[0], 496c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(pr_reg_type)); 497c66ac9dbSNicholas Bellinger return 1; 498c66ac9dbSNicholas Bellinger } else { 499c66ac9dbSNicholas Bellinger /* 500c66ac9dbSNicholas Bellinger * Allow non WRITE CDBs for all Write Exclusive 501c66ac9dbSNicholas Bellinger * PR TYPEs to pass for registered and 502c66ac9dbSNicholas Bellinger * non-registered_nexuxes NOT holding the reservation. 503c66ac9dbSNicholas Bellinger * 504c66ac9dbSNicholas Bellinger * We only make noise for the unregisterd nexuses, 505c66ac9dbSNicholas Bellinger * as we expect registered non-reservation holding 506c66ac9dbSNicholas Bellinger * nexuses to issue CDBs. 507c66ac9dbSNicholas Bellinger */ 5088b1e1244SAndy Grover 5096708bb27SAndy Grover if (!registered_nexus) { 510125d0119SHannes Reinecke pr_debug("Allowing implicit CDB: 0x%02x" 511c66ac9dbSNicholas Bellinger " for %s reservation on unregistered" 512c66ac9dbSNicholas Bellinger " nexus\n", cdb[0], 513c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(pr_reg_type)); 514c66ac9dbSNicholas Bellinger } 5158b1e1244SAndy Grover 516c66ac9dbSNicholas Bellinger return 0; 517c66ac9dbSNicholas Bellinger } 518c66ac9dbSNicholas Bellinger } else if ((reg_only) || (all_reg)) { 519c66ac9dbSNicholas Bellinger if (registered_nexus) { 520c66ac9dbSNicholas Bellinger /* 521c66ac9dbSNicholas Bellinger * For PR_*_REG_ONLY and PR_*_ALL_REG reservations, 522c66ac9dbSNicholas Bellinger * allow commands from registered nexuses. 523c66ac9dbSNicholas Bellinger */ 5248b1e1244SAndy Grover 525125d0119SHannes Reinecke pr_debug("Allowing implicit CDB: 0x%02x for %s" 526c66ac9dbSNicholas Bellinger " reservation\n", cdb[0], 527c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(pr_reg_type)); 5288b1e1244SAndy Grover 529c66ac9dbSNicholas Bellinger return 0; 530c66ac9dbSNicholas Bellinger } 531c66ac9dbSNicholas Bellinger } 5326708bb27SAndy Grover pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x" 533c66ac9dbSNicholas Bellinger " for %s reservation\n", transport_dump_cmd_direction(cmd), 534c66ac9dbSNicholas Bellinger (registered_nexus) ? "" : "un", 535c66ac9dbSNicholas Bellinger se_sess->se_node_acl->initiatorname, cdb[0], 536c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(pr_reg_type)); 537c66ac9dbSNicholas Bellinger 538c66ac9dbSNicholas Bellinger return 1; /* Conflict by default */ 539c66ac9dbSNicholas Bellinger } 540c66ac9dbSNicholas Bellinger 541de103c93SChristoph Hellwig static sense_reason_t 542de103c93SChristoph Hellwig target_scsi3_pr_reservation_check(struct se_cmd *cmd) 543d977f437SChristoph Hellwig { 544d977f437SChristoph Hellwig struct se_device *dev = cmd->se_dev; 545d977f437SChristoph Hellwig struct se_session *sess = cmd->se_sess; 546d977f437SChristoph Hellwig u32 pr_reg_type; 547d977f437SChristoph Hellwig 548d977f437SChristoph Hellwig if (!dev->dev_pr_res_holder) 549d977f437SChristoph Hellwig return 0; 550d977f437SChristoph Hellwig 551d977f437SChristoph Hellwig pr_reg_type = dev->dev_pr_res_holder->pr_res_type; 552d977f437SChristoph Hellwig cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key; 553d977f437SChristoph Hellwig if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl) 554d977f437SChristoph Hellwig goto check_nonholder; 555d977f437SChristoph Hellwig 556d977f437SChristoph Hellwig if (dev->dev_pr_res_holder->isid_present_at_reg) { 557d977f437SChristoph Hellwig if (dev->dev_pr_res_holder->pr_reg_bin_isid != 558d977f437SChristoph Hellwig sess->sess_bin_isid) { 559d977f437SChristoph Hellwig pr_reg_type |= 0x80000000; 560d977f437SChristoph Hellwig goto check_nonholder; 561d977f437SChristoph Hellwig } 562d977f437SChristoph Hellwig } 563d977f437SChristoph Hellwig 564d977f437SChristoph Hellwig return 0; 565d977f437SChristoph Hellwig 566d977f437SChristoph Hellwig check_nonholder: 567d977f437SChristoph Hellwig if (core_scsi3_pr_seq_non_holder(cmd, pr_reg_type)) 568de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 569d977f437SChristoph Hellwig return 0; 570d977f437SChristoph Hellwig } 571d977f437SChristoph Hellwig 572c66ac9dbSNicholas Bellinger static u32 core_scsi3_pr_generation(struct se_device *dev) 573c66ac9dbSNicholas Bellinger { 574c66ac9dbSNicholas Bellinger u32 prg; 5750fd97ccfSChristoph Hellwig 576c66ac9dbSNicholas Bellinger /* 577c66ac9dbSNicholas Bellinger * PRGeneration field shall contain the value of a 32-bit wrapping 578c66ac9dbSNicholas Bellinger * counter mainted by the device server. 579c66ac9dbSNicholas Bellinger * 580c66ac9dbSNicholas Bellinger * Note that this is done regardless of Active Persist across 581c66ac9dbSNicholas Bellinger * Target PowerLoss (APTPL) 582c66ac9dbSNicholas Bellinger * 583c66ac9dbSNicholas Bellinger * See spc4r17 section 6.3.12 READ_KEYS service action 584c66ac9dbSNicholas Bellinger */ 585c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 5860fd97ccfSChristoph Hellwig prg = dev->t10_pr.pr_generation++; 587c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 588c66ac9dbSNicholas Bellinger 589c66ac9dbSNicholas Bellinger return prg; 590c66ac9dbSNicholas Bellinger } 591c66ac9dbSNicholas Bellinger 592c66ac9dbSNicholas Bellinger static struct t10_pr_registration *__core_scsi3_do_alloc_registration( 593c66ac9dbSNicholas Bellinger struct se_device *dev, 594c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 595c66ac9dbSNicholas Bellinger struct se_dev_entry *deve, 596c66ac9dbSNicholas Bellinger unsigned char *isid, 597c66ac9dbSNicholas Bellinger u64 sa_res_key, 598c66ac9dbSNicholas Bellinger int all_tg_pt, 599c66ac9dbSNicholas Bellinger int aptpl) 600c66ac9dbSNicholas Bellinger { 601c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg; 602c66ac9dbSNicholas Bellinger 603c66ac9dbSNicholas Bellinger pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC); 6046708bb27SAndy Grover if (!pr_reg) { 6056708bb27SAndy Grover pr_err("Unable to allocate struct t10_pr_registration\n"); 606c66ac9dbSNicholas Bellinger return NULL; 607c66ac9dbSNicholas Bellinger } 608c66ac9dbSNicholas Bellinger 609c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_list); 610c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list); 611c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list); 612c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list); 613c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list); 614c66ac9dbSNicholas Bellinger atomic_set(&pr_reg->pr_res_holders, 0); 615c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl = nacl; 616c66ac9dbSNicholas Bellinger pr_reg->pr_reg_deve = deve; 617c66ac9dbSNicholas Bellinger pr_reg->pr_res_mapped_lun = deve->mapped_lun; 618c66ac9dbSNicholas Bellinger pr_reg->pr_aptpl_target_lun = deve->se_lun->unpacked_lun; 619c66ac9dbSNicholas Bellinger pr_reg->pr_res_key = sa_res_key; 620c66ac9dbSNicholas Bellinger pr_reg->pr_reg_all_tg_pt = all_tg_pt; 621c66ac9dbSNicholas Bellinger pr_reg->pr_reg_aptpl = aptpl; 622c66ac9dbSNicholas Bellinger pr_reg->pr_reg_tg_pt_lun = deve->se_lun; 623c66ac9dbSNicholas Bellinger /* 624c66ac9dbSNicholas Bellinger * If an ISID value for this SCSI Initiator Port exists, 625c66ac9dbSNicholas Bellinger * save it to the registration now. 626c66ac9dbSNicholas Bellinger */ 627c66ac9dbSNicholas Bellinger if (isid != NULL) { 628c66ac9dbSNicholas Bellinger pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid); 629c66ac9dbSNicholas Bellinger snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid); 630c66ac9dbSNicholas Bellinger pr_reg->isid_present_at_reg = 1; 631c66ac9dbSNicholas Bellinger } 632c66ac9dbSNicholas Bellinger 633c66ac9dbSNicholas Bellinger return pr_reg; 634c66ac9dbSNicholas Bellinger } 635c66ac9dbSNicholas Bellinger 636c66ac9dbSNicholas Bellinger static int core_scsi3_lunacl_depend_item(struct se_dev_entry *); 637c66ac9dbSNicholas Bellinger static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *); 638c66ac9dbSNicholas Bellinger 639c66ac9dbSNicholas Bellinger /* 640c66ac9dbSNicholas Bellinger * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0 641c66ac9dbSNicholas Bellinger * modes. 642c66ac9dbSNicholas Bellinger */ 643c66ac9dbSNicholas Bellinger static struct t10_pr_registration *__core_scsi3_alloc_registration( 644c66ac9dbSNicholas Bellinger struct se_device *dev, 645c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 646c66ac9dbSNicholas Bellinger struct se_dev_entry *deve, 647c66ac9dbSNicholas Bellinger unsigned char *isid, 648c66ac9dbSNicholas Bellinger u64 sa_res_key, 649c66ac9dbSNicholas Bellinger int all_tg_pt, 650c66ac9dbSNicholas Bellinger int aptpl) 651c66ac9dbSNicholas Bellinger { 652c66ac9dbSNicholas Bellinger struct se_dev_entry *deve_tmp; 653c66ac9dbSNicholas Bellinger struct se_node_acl *nacl_tmp; 654c66ac9dbSNicholas Bellinger struct se_port *port, *port_tmp; 655c66ac9dbSNicholas Bellinger struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo; 656c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe; 657c66ac9dbSNicholas Bellinger int ret; 658c66ac9dbSNicholas Bellinger /* 659c66ac9dbSNicholas Bellinger * Create a registration for the I_T Nexus upon which the 660c66ac9dbSNicholas Bellinger * PROUT REGISTER was received. 661c66ac9dbSNicholas Bellinger */ 662c66ac9dbSNicholas Bellinger pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, deve, isid, 663c66ac9dbSNicholas Bellinger sa_res_key, all_tg_pt, aptpl); 6646708bb27SAndy Grover if (!pr_reg) 665c66ac9dbSNicholas Bellinger return NULL; 666c66ac9dbSNicholas Bellinger /* 667c66ac9dbSNicholas Bellinger * Return pointer to pr_reg for ALL_TG_PT=0 668c66ac9dbSNicholas Bellinger */ 6696708bb27SAndy Grover if (!all_tg_pt) 670c66ac9dbSNicholas Bellinger return pr_reg; 671c66ac9dbSNicholas Bellinger /* 672c66ac9dbSNicholas Bellinger * Create list of matching SCSI Initiator Port registrations 673c66ac9dbSNicholas Bellinger * for ALL_TG_PT=1 674c66ac9dbSNicholas Bellinger */ 675c66ac9dbSNicholas Bellinger spin_lock(&dev->se_port_lock); 676c66ac9dbSNicholas Bellinger list_for_each_entry_safe(port, port_tmp, &dev->dev_sep_list, sep_list) { 67733940d09SJoern Engel atomic_inc_mb(&port->sep_tg_pt_ref_cnt); 678c66ac9dbSNicholas Bellinger spin_unlock(&dev->se_port_lock); 679c66ac9dbSNicholas Bellinger 680c66ac9dbSNicholas Bellinger spin_lock_bh(&port->sep_alua_lock); 681c66ac9dbSNicholas Bellinger list_for_each_entry(deve_tmp, &port->sep_alua_list, 682c66ac9dbSNicholas Bellinger alua_port_list) { 683c66ac9dbSNicholas Bellinger /* 684c66ac9dbSNicholas Bellinger * This pointer will be NULL for demo mode MappedLUNs 685125d0119SHannes Reinecke * that have not been make explicit via a ConfigFS 686c66ac9dbSNicholas Bellinger * MappedLUN group for the SCSI Initiator Node ACL. 687c66ac9dbSNicholas Bellinger */ 6886708bb27SAndy Grover if (!deve_tmp->se_lun_acl) 689c66ac9dbSNicholas Bellinger continue; 690c66ac9dbSNicholas Bellinger 691c66ac9dbSNicholas Bellinger nacl_tmp = deve_tmp->se_lun_acl->se_lun_nacl; 692c66ac9dbSNicholas Bellinger /* 693c66ac9dbSNicholas Bellinger * Skip the matching struct se_node_acl that is allocated 694c66ac9dbSNicholas Bellinger * above.. 695c66ac9dbSNicholas Bellinger */ 696c66ac9dbSNicholas Bellinger if (nacl == nacl_tmp) 697c66ac9dbSNicholas Bellinger continue; 698c66ac9dbSNicholas Bellinger /* 699c66ac9dbSNicholas Bellinger * Only perform PR registrations for target ports on 700c66ac9dbSNicholas Bellinger * the same fabric module as the REGISTER w/ ALL_TG_PT=1 701c66ac9dbSNicholas Bellinger * arrived. 702c66ac9dbSNicholas Bellinger */ 703c66ac9dbSNicholas Bellinger if (tfo != nacl_tmp->se_tpg->se_tpg_tfo) 704c66ac9dbSNicholas Bellinger continue; 705c66ac9dbSNicholas Bellinger /* 706c66ac9dbSNicholas Bellinger * Look for a matching Initiator Node ACL in ASCII format 707c66ac9dbSNicholas Bellinger */ 708c66ac9dbSNicholas Bellinger if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname)) 709c66ac9dbSNicholas Bellinger continue; 710c66ac9dbSNicholas Bellinger 71133940d09SJoern Engel atomic_inc_mb(&deve_tmp->pr_ref_count); 712c66ac9dbSNicholas Bellinger spin_unlock_bh(&port->sep_alua_lock); 713c66ac9dbSNicholas Bellinger /* 714c66ac9dbSNicholas Bellinger * Grab a configfs group dependency that is released 715c66ac9dbSNicholas Bellinger * for the exception path at label out: below, or upon 716c66ac9dbSNicholas Bellinger * completion of adding ALL_TG_PT=1 registrations in 717c66ac9dbSNicholas Bellinger * __core_scsi3_add_registration() 718c66ac9dbSNicholas Bellinger */ 719c66ac9dbSNicholas Bellinger ret = core_scsi3_lunacl_depend_item(deve_tmp); 720c66ac9dbSNicholas Bellinger if (ret < 0) { 7216708bb27SAndy Grover pr_err("core_scsi3_lunacl_depend" 722c66ac9dbSNicholas Bellinger "_item() failed\n"); 72333940d09SJoern Engel atomic_dec_mb(&port->sep_tg_pt_ref_cnt); 72433940d09SJoern Engel atomic_dec_mb(&deve_tmp->pr_ref_count); 725c66ac9dbSNicholas Bellinger goto out; 726c66ac9dbSNicholas Bellinger } 727c66ac9dbSNicholas Bellinger /* 728c66ac9dbSNicholas Bellinger * Located a matching SCSI Initiator Port on a different 729c66ac9dbSNicholas Bellinger * port, allocate the pr_reg_atp and attach it to the 730c66ac9dbSNicholas Bellinger * pr_reg->pr_reg_atp_list that will be processed once 731c66ac9dbSNicholas Bellinger * the original *pr_reg is processed in 732c66ac9dbSNicholas Bellinger * __core_scsi3_add_registration() 733c66ac9dbSNicholas Bellinger */ 734c66ac9dbSNicholas Bellinger pr_reg_atp = __core_scsi3_do_alloc_registration(dev, 735c66ac9dbSNicholas Bellinger nacl_tmp, deve_tmp, NULL, 736c66ac9dbSNicholas Bellinger sa_res_key, all_tg_pt, aptpl); 7376708bb27SAndy Grover if (!pr_reg_atp) { 73833940d09SJoern Engel atomic_dec_mb(&port->sep_tg_pt_ref_cnt); 73933940d09SJoern Engel atomic_dec_mb(&deve_tmp->pr_ref_count); 740c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(deve_tmp); 741c66ac9dbSNicholas Bellinger goto out; 742c66ac9dbSNicholas Bellinger } 743c66ac9dbSNicholas Bellinger 744c66ac9dbSNicholas Bellinger list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list, 745c66ac9dbSNicholas Bellinger &pr_reg->pr_reg_atp_list); 746c66ac9dbSNicholas Bellinger spin_lock_bh(&port->sep_alua_lock); 747c66ac9dbSNicholas Bellinger } 748c66ac9dbSNicholas Bellinger spin_unlock_bh(&port->sep_alua_lock); 749c66ac9dbSNicholas Bellinger 750c66ac9dbSNicholas Bellinger spin_lock(&dev->se_port_lock); 75133940d09SJoern Engel atomic_dec_mb(&port->sep_tg_pt_ref_cnt); 752c66ac9dbSNicholas Bellinger } 753c66ac9dbSNicholas Bellinger spin_unlock(&dev->se_port_lock); 754c66ac9dbSNicholas Bellinger 755c66ac9dbSNicholas Bellinger return pr_reg; 756c66ac9dbSNicholas Bellinger out: 757c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe, 758c66ac9dbSNicholas Bellinger &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) { 759c66ac9dbSNicholas Bellinger list_del(&pr_reg_tmp->pr_reg_atp_mem_list); 760c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve); 761c66ac9dbSNicholas Bellinger kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp); 762c66ac9dbSNicholas Bellinger } 763c66ac9dbSNicholas Bellinger kmem_cache_free(t10_pr_reg_cache, pr_reg); 764c66ac9dbSNicholas Bellinger return NULL; 765c66ac9dbSNicholas Bellinger } 766c66ac9dbSNicholas Bellinger 767c66ac9dbSNicholas Bellinger int core_scsi3_alloc_aptpl_registration( 768e3d6f909SAndy Grover struct t10_reservation *pr_tmpl, 769c66ac9dbSNicholas Bellinger u64 sa_res_key, 770c66ac9dbSNicholas Bellinger unsigned char *i_port, 771c66ac9dbSNicholas Bellinger unsigned char *isid, 772c66ac9dbSNicholas Bellinger u32 mapped_lun, 773c66ac9dbSNicholas Bellinger unsigned char *t_port, 774c66ac9dbSNicholas Bellinger u16 tpgt, 775c66ac9dbSNicholas Bellinger u32 target_lun, 776c66ac9dbSNicholas Bellinger int res_holder, 777c66ac9dbSNicholas Bellinger int all_tg_pt, 778c66ac9dbSNicholas Bellinger u8 type) 779c66ac9dbSNicholas Bellinger { 780c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg; 781c66ac9dbSNicholas Bellinger 7826708bb27SAndy Grover if (!i_port || !t_port || !sa_res_key) { 7836708bb27SAndy Grover pr_err("Illegal parameters for APTPL registration\n"); 784e3d6f909SAndy Grover return -EINVAL; 785c66ac9dbSNicholas Bellinger } 786c66ac9dbSNicholas Bellinger 787c66ac9dbSNicholas Bellinger pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL); 7886708bb27SAndy Grover if (!pr_reg) { 7896708bb27SAndy Grover pr_err("Unable to allocate struct t10_pr_registration\n"); 790e3d6f909SAndy Grover return -ENOMEM; 791c66ac9dbSNicholas Bellinger } 792c66ac9dbSNicholas Bellinger 793c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_list); 794c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list); 795c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list); 796c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list); 797c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list); 798c66ac9dbSNicholas Bellinger atomic_set(&pr_reg->pr_res_holders, 0); 799c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl = NULL; 800c66ac9dbSNicholas Bellinger pr_reg->pr_reg_deve = NULL; 801c66ac9dbSNicholas Bellinger pr_reg->pr_res_mapped_lun = mapped_lun; 802c66ac9dbSNicholas Bellinger pr_reg->pr_aptpl_target_lun = target_lun; 803c66ac9dbSNicholas Bellinger pr_reg->pr_res_key = sa_res_key; 804c66ac9dbSNicholas Bellinger pr_reg->pr_reg_all_tg_pt = all_tg_pt; 805c66ac9dbSNicholas Bellinger pr_reg->pr_reg_aptpl = 1; 806c66ac9dbSNicholas Bellinger pr_reg->pr_reg_tg_pt_lun = NULL; 807c66ac9dbSNicholas Bellinger pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */ 808c66ac9dbSNicholas Bellinger pr_reg->pr_res_type = type; 809c66ac9dbSNicholas Bellinger /* 810c66ac9dbSNicholas Bellinger * If an ISID value had been saved in APTPL metadata for this 811c66ac9dbSNicholas Bellinger * SCSI Initiator Port, restore it now. 812c66ac9dbSNicholas Bellinger */ 813c66ac9dbSNicholas Bellinger if (isid != NULL) { 814c66ac9dbSNicholas Bellinger pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid); 815c66ac9dbSNicholas Bellinger snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid); 816c66ac9dbSNicholas Bellinger pr_reg->isid_present_at_reg = 1; 817c66ac9dbSNicholas Bellinger } 818c66ac9dbSNicholas Bellinger /* 819c66ac9dbSNicholas Bellinger * Copy the i_port and t_port information from caller. 820c66ac9dbSNicholas Bellinger */ 821c66ac9dbSNicholas Bellinger snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port); 822c66ac9dbSNicholas Bellinger snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port); 823c66ac9dbSNicholas Bellinger pr_reg->pr_reg_tpgt = tpgt; 824c66ac9dbSNicholas Bellinger /* 825c66ac9dbSNicholas Bellinger * Set pr_res_holder from caller, the pr_reg who is the reservation 826c66ac9dbSNicholas Bellinger * holder will get it's pointer set in core_scsi3_aptpl_reserve() once 827c66ac9dbSNicholas Bellinger * the Initiator Node LUN ACL from the fabric module is created for 828c66ac9dbSNicholas Bellinger * this registration. 829c66ac9dbSNicholas Bellinger */ 830c66ac9dbSNicholas Bellinger pr_reg->pr_res_holder = res_holder; 831c66ac9dbSNicholas Bellinger 832c66ac9dbSNicholas Bellinger list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list); 8336708bb27SAndy Grover pr_debug("SPC-3 PR APTPL Successfully added registration%s from" 834c66ac9dbSNicholas Bellinger " metadata\n", (res_holder) ? "+reservation" : ""); 835c66ac9dbSNicholas Bellinger return 0; 836c66ac9dbSNicholas Bellinger } 837c66ac9dbSNicholas Bellinger 838c66ac9dbSNicholas Bellinger static void core_scsi3_aptpl_reserve( 839c66ac9dbSNicholas Bellinger struct se_device *dev, 840c66ac9dbSNicholas Bellinger struct se_portal_group *tpg, 841c66ac9dbSNicholas Bellinger struct se_node_acl *node_acl, 842c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg) 843c66ac9dbSNicholas Bellinger { 844c66ac9dbSNicholas Bellinger char i_buf[PR_REG_ISID_ID_LEN]; 845c66ac9dbSNicholas Bellinger 846c66ac9dbSNicholas Bellinger memset(i_buf, 0, PR_REG_ISID_ID_LEN); 847d2843c17SAndy Grover core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); 848c66ac9dbSNicholas Bellinger 849c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 850c66ac9dbSNicholas Bellinger dev->dev_pr_res_holder = pr_reg; 851c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 852c66ac9dbSNicholas Bellinger 8536708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created" 854c66ac9dbSNicholas Bellinger " new reservation holder TYPE: %s ALL_TG_PT: %d\n", 855e3d6f909SAndy Grover tpg->se_tpg_tfo->get_fabric_name(), 856c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(pr_reg->pr_res_type), 857c66ac9dbSNicholas Bellinger (pr_reg->pr_reg_all_tg_pt) ? 1 : 0); 8586708bb27SAndy Grover pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n", 859e3d6f909SAndy Grover tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname, 860d2843c17SAndy Grover i_buf); 861c66ac9dbSNicholas Bellinger } 862c66ac9dbSNicholas Bellinger 863c66ac9dbSNicholas Bellinger static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *, 86433ce6a87SAndy Grover struct t10_pr_registration *, enum register_type, int); 865c66ac9dbSNicholas Bellinger 866c66ac9dbSNicholas Bellinger static int __core_scsi3_check_aptpl_registration( 867c66ac9dbSNicholas Bellinger struct se_device *dev, 868c66ac9dbSNicholas Bellinger struct se_portal_group *tpg, 869c66ac9dbSNicholas Bellinger struct se_lun *lun, 870c66ac9dbSNicholas Bellinger u32 target_lun, 871c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 872c66ac9dbSNicholas Bellinger struct se_dev_entry *deve) 873c66ac9dbSNicholas Bellinger { 874c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp; 8750fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 876c66ac9dbSNicholas Bellinger unsigned char i_port[PR_APTPL_MAX_IPORT_LEN]; 877c66ac9dbSNicholas Bellinger unsigned char t_port[PR_APTPL_MAX_TPORT_LEN]; 878c66ac9dbSNicholas Bellinger u16 tpgt; 879c66ac9dbSNicholas Bellinger 880c66ac9dbSNicholas Bellinger memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN); 881c66ac9dbSNicholas Bellinger memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN); 882c66ac9dbSNicholas Bellinger /* 883c66ac9dbSNicholas Bellinger * Copy Initiator Port information from struct se_node_acl 884c66ac9dbSNicholas Bellinger */ 885c66ac9dbSNicholas Bellinger snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname); 886c66ac9dbSNicholas Bellinger snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s", 887e3d6f909SAndy Grover tpg->se_tpg_tfo->tpg_get_wwn(tpg)); 888e3d6f909SAndy Grover tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg); 889c66ac9dbSNicholas Bellinger /* 890c66ac9dbSNicholas Bellinger * Look for the matching registrations+reservation from those 891c66ac9dbSNicholas Bellinger * created from APTPL metadata. Note that multiple registrations 892c66ac9dbSNicholas Bellinger * may exist for fabrics that use ISIDs in their SCSI Initiator Port 893c66ac9dbSNicholas Bellinger * TransportIDs. 894c66ac9dbSNicholas Bellinger */ 895c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->aptpl_reg_lock); 896c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list, 897c66ac9dbSNicholas Bellinger pr_reg_aptpl_list) { 89892404e60SNicholas Bellinger 8996708bb27SAndy Grover if (!strcmp(pr_reg->pr_iport, i_port) && 900c66ac9dbSNicholas Bellinger (pr_reg->pr_res_mapped_lun == deve->mapped_lun) && 901c66ac9dbSNicholas Bellinger !(strcmp(pr_reg->pr_tport, t_port)) && 902c66ac9dbSNicholas Bellinger (pr_reg->pr_reg_tpgt == tpgt) && 903c66ac9dbSNicholas Bellinger (pr_reg->pr_aptpl_target_lun == target_lun)) { 904c66ac9dbSNicholas Bellinger 905c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl = nacl; 906c66ac9dbSNicholas Bellinger pr_reg->pr_reg_deve = deve; 907c66ac9dbSNicholas Bellinger pr_reg->pr_reg_tg_pt_lun = lun; 908c66ac9dbSNicholas Bellinger 909c66ac9dbSNicholas Bellinger list_del(&pr_reg->pr_reg_aptpl_list); 910c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->aptpl_reg_lock); 911c66ac9dbSNicholas Bellinger /* 912c66ac9dbSNicholas Bellinger * At this point all of the pointers in *pr_reg will 913c66ac9dbSNicholas Bellinger * be setup, so go ahead and add the registration. 914c66ac9dbSNicholas Bellinger */ 915c66ac9dbSNicholas Bellinger 916c66ac9dbSNicholas Bellinger __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0); 917c66ac9dbSNicholas Bellinger /* 918c66ac9dbSNicholas Bellinger * If this registration is the reservation holder, 919c66ac9dbSNicholas Bellinger * make that happen now.. 920c66ac9dbSNicholas Bellinger */ 921c66ac9dbSNicholas Bellinger if (pr_reg->pr_res_holder) 922c66ac9dbSNicholas Bellinger core_scsi3_aptpl_reserve(dev, tpg, 923c66ac9dbSNicholas Bellinger nacl, pr_reg); 924c66ac9dbSNicholas Bellinger /* 925c66ac9dbSNicholas Bellinger * Reenable pr_aptpl_active to accept new metadata 926c66ac9dbSNicholas Bellinger * updates once the SCSI device is active again.. 927c66ac9dbSNicholas Bellinger */ 928c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->aptpl_reg_lock); 929c66ac9dbSNicholas Bellinger pr_tmpl->pr_aptpl_active = 1; 930c66ac9dbSNicholas Bellinger } 931c66ac9dbSNicholas Bellinger } 932c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->aptpl_reg_lock); 933c66ac9dbSNicholas Bellinger 934c66ac9dbSNicholas Bellinger return 0; 935c66ac9dbSNicholas Bellinger } 936c66ac9dbSNicholas Bellinger 937c66ac9dbSNicholas Bellinger int core_scsi3_check_aptpl_registration( 938c66ac9dbSNicholas Bellinger struct se_device *dev, 939c66ac9dbSNicholas Bellinger struct se_portal_group *tpg, 940c66ac9dbSNicholas Bellinger struct se_lun *lun, 941e2480563SNicholas Bellinger struct se_node_acl *nacl, 942e2480563SNicholas Bellinger u32 mapped_lun) 943c66ac9dbSNicholas Bellinger { 944e2480563SNicholas Bellinger struct se_dev_entry *deve = nacl->device_list[mapped_lun]; 945c66ac9dbSNicholas Bellinger 946d977f437SChristoph Hellwig if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) 947c66ac9dbSNicholas Bellinger return 0; 948c66ac9dbSNicholas Bellinger 949c66ac9dbSNicholas Bellinger return __core_scsi3_check_aptpl_registration(dev, tpg, lun, 950c66ac9dbSNicholas Bellinger lun->unpacked_lun, nacl, deve); 951c66ac9dbSNicholas Bellinger } 952c66ac9dbSNicholas Bellinger 953c66ac9dbSNicholas Bellinger static void __core_scsi3_dump_registration( 954c66ac9dbSNicholas Bellinger struct target_core_fabric_ops *tfo, 955c66ac9dbSNicholas Bellinger struct se_device *dev, 956c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 957c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, 95833ce6a87SAndy Grover enum register_type register_type) 959c66ac9dbSNicholas Bellinger { 960c66ac9dbSNicholas Bellinger struct se_portal_group *se_tpg = nacl->se_tpg; 961c66ac9dbSNicholas Bellinger char i_buf[PR_REG_ISID_ID_LEN]; 962c66ac9dbSNicholas Bellinger 963c66ac9dbSNicholas Bellinger memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN); 964d2843c17SAndy Grover core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); 965c66ac9dbSNicholas Bellinger 9666708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator" 96733ce6a87SAndy Grover " Node: %s%s\n", tfo->get_fabric_name(), (register_type == REGISTER_AND_MOVE) ? 96833ce6a87SAndy Grover "_AND_MOVE" : (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? 969c66ac9dbSNicholas Bellinger "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname, 970d2843c17SAndy Grover i_buf); 9716708bb27SAndy Grover pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n", 972c66ac9dbSNicholas Bellinger tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg), 973c66ac9dbSNicholas Bellinger tfo->tpg_get_tag(se_tpg)); 9746708bb27SAndy Grover pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target" 975c66ac9dbSNicholas Bellinger " Port(s)\n", tfo->get_fabric_name(), 976c66ac9dbSNicholas Bellinger (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE", 977e3d6f909SAndy Grover dev->transport->name); 9786708bb27SAndy Grover pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:" 979c66ac9dbSNicholas Bellinger " 0x%08x APTPL: %d\n", tfo->get_fabric_name(), 980c66ac9dbSNicholas Bellinger pr_reg->pr_res_key, pr_reg->pr_res_generation, 981c66ac9dbSNicholas Bellinger pr_reg->pr_reg_aptpl); 982c66ac9dbSNicholas Bellinger } 983c66ac9dbSNicholas Bellinger 984c66ac9dbSNicholas Bellinger /* 985c66ac9dbSNicholas Bellinger * this function can be called with struct se_device->dev_reservation_lock 986c66ac9dbSNicholas Bellinger * when register_move = 1 987c66ac9dbSNicholas Bellinger */ 988c66ac9dbSNicholas Bellinger static void __core_scsi3_add_registration( 989c66ac9dbSNicholas Bellinger struct se_device *dev, 990c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 991c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, 99233ce6a87SAndy Grover enum register_type register_type, 993c66ac9dbSNicholas Bellinger int register_move) 994c66ac9dbSNicholas Bellinger { 995c66ac9dbSNicholas Bellinger struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo; 996c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe; 9970fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 998c66ac9dbSNicholas Bellinger 999c66ac9dbSNicholas Bellinger /* 1000c66ac9dbSNicholas Bellinger * Increment PRgeneration counter for struct se_device upon a successful 1001c66ac9dbSNicholas Bellinger * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action 1002c66ac9dbSNicholas Bellinger * 1003c66ac9dbSNicholas Bellinger * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service 1004c66ac9dbSNicholas Bellinger * action, the struct se_device->dev_reservation_lock will already be held, 1005c66ac9dbSNicholas Bellinger * so we do not call core_scsi3_pr_generation() which grabs the lock 1006c66ac9dbSNicholas Bellinger * for the REGISTER. 1007c66ac9dbSNicholas Bellinger */ 1008c66ac9dbSNicholas Bellinger pr_reg->pr_res_generation = (register_move) ? 10090fd97ccfSChristoph Hellwig dev->t10_pr.pr_generation++ : 1010c66ac9dbSNicholas Bellinger core_scsi3_pr_generation(dev); 1011c66ac9dbSNicholas Bellinger 1012c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 1013c66ac9dbSNicholas Bellinger list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list); 1014c66ac9dbSNicholas Bellinger pr_reg->pr_reg_deve->def_pr_registered = 1; 1015c66ac9dbSNicholas Bellinger 1016c66ac9dbSNicholas Bellinger __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type); 1017c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 1018c66ac9dbSNicholas Bellinger /* 1019c66ac9dbSNicholas Bellinger * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE. 1020c66ac9dbSNicholas Bellinger */ 10216708bb27SAndy Grover if (!pr_reg->pr_reg_all_tg_pt || register_move) 1022c66ac9dbSNicholas Bellinger return; 1023c66ac9dbSNicholas Bellinger /* 1024c66ac9dbSNicholas Bellinger * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1 1025c66ac9dbSNicholas Bellinger * allocated in __core_scsi3_alloc_registration() 1026c66ac9dbSNicholas Bellinger */ 1027c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe, 1028c66ac9dbSNicholas Bellinger &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) { 1029c66ac9dbSNicholas Bellinger list_del(&pr_reg_tmp->pr_reg_atp_mem_list); 1030c66ac9dbSNicholas Bellinger 1031c66ac9dbSNicholas Bellinger pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev); 1032c66ac9dbSNicholas Bellinger 1033c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 1034c66ac9dbSNicholas Bellinger list_add_tail(&pr_reg_tmp->pr_reg_list, 1035c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list); 1036c66ac9dbSNicholas Bellinger pr_reg_tmp->pr_reg_deve->def_pr_registered = 1; 1037c66ac9dbSNicholas Bellinger 1038c66ac9dbSNicholas Bellinger __core_scsi3_dump_registration(tfo, dev, 1039c66ac9dbSNicholas Bellinger pr_reg_tmp->pr_reg_nacl, pr_reg_tmp, 1040c66ac9dbSNicholas Bellinger register_type); 1041c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 1042c66ac9dbSNicholas Bellinger /* 1043c66ac9dbSNicholas Bellinger * Drop configfs group dependency reference from 1044c66ac9dbSNicholas Bellinger * __core_scsi3_alloc_registration() 1045c66ac9dbSNicholas Bellinger */ 1046c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve); 1047c66ac9dbSNicholas Bellinger } 1048c66ac9dbSNicholas Bellinger } 1049c66ac9dbSNicholas Bellinger 1050c66ac9dbSNicholas Bellinger static int core_scsi3_alloc_registration( 1051c66ac9dbSNicholas Bellinger struct se_device *dev, 1052c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 1053c66ac9dbSNicholas Bellinger struct se_dev_entry *deve, 1054c66ac9dbSNicholas Bellinger unsigned char *isid, 1055c66ac9dbSNicholas Bellinger u64 sa_res_key, 1056c66ac9dbSNicholas Bellinger int all_tg_pt, 1057c66ac9dbSNicholas Bellinger int aptpl, 105833ce6a87SAndy Grover enum register_type register_type, 1059c66ac9dbSNicholas Bellinger int register_move) 1060c66ac9dbSNicholas Bellinger { 1061c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg; 1062c66ac9dbSNicholas Bellinger 1063c66ac9dbSNicholas Bellinger pr_reg = __core_scsi3_alloc_registration(dev, nacl, deve, isid, 1064c66ac9dbSNicholas Bellinger sa_res_key, all_tg_pt, aptpl); 10656708bb27SAndy Grover if (!pr_reg) 1066e3d6f909SAndy Grover return -EPERM; 1067c66ac9dbSNicholas Bellinger 1068c66ac9dbSNicholas Bellinger __core_scsi3_add_registration(dev, nacl, pr_reg, 1069c66ac9dbSNicholas Bellinger register_type, register_move); 1070c66ac9dbSNicholas Bellinger return 0; 1071c66ac9dbSNicholas Bellinger } 1072c66ac9dbSNicholas Bellinger 1073c66ac9dbSNicholas Bellinger static struct t10_pr_registration *__core_scsi3_locate_pr_reg( 1074c66ac9dbSNicholas Bellinger struct se_device *dev, 1075c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 1076c66ac9dbSNicholas Bellinger unsigned char *isid) 1077c66ac9dbSNicholas Bellinger { 10780fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 1079c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp; 1080c66ac9dbSNicholas Bellinger struct se_portal_group *tpg; 1081c66ac9dbSNicholas Bellinger 1082c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 1083c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 1084c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 1085c66ac9dbSNicholas Bellinger /* 1086c66ac9dbSNicholas Bellinger * First look for a matching struct se_node_acl 1087c66ac9dbSNicholas Bellinger */ 1088c66ac9dbSNicholas Bellinger if (pr_reg->pr_reg_nacl != nacl) 1089c66ac9dbSNicholas Bellinger continue; 1090c66ac9dbSNicholas Bellinger 1091c66ac9dbSNicholas Bellinger tpg = pr_reg->pr_reg_nacl->se_tpg; 1092c66ac9dbSNicholas Bellinger /* 1093c66ac9dbSNicholas Bellinger * If this registration does NOT contain a fabric provided 1094c66ac9dbSNicholas Bellinger * ISID, then we have found a match. 1095c66ac9dbSNicholas Bellinger */ 10966708bb27SAndy Grover if (!pr_reg->isid_present_at_reg) { 1097c66ac9dbSNicholas Bellinger /* 1098c66ac9dbSNicholas Bellinger * Determine if this SCSI device server requires that 1099c66ac9dbSNicholas Bellinger * SCSI Intiatior TransportID w/ ISIDs is enforced 1100c66ac9dbSNicholas Bellinger * for fabric modules (iSCSI) requiring them. 1101c66ac9dbSNicholas Bellinger */ 1102e3d6f909SAndy Grover if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) { 11030fd97ccfSChristoph Hellwig if (dev->dev_attrib.enforce_pr_isids) 1104c66ac9dbSNicholas Bellinger continue; 1105c66ac9dbSNicholas Bellinger } 110633940d09SJoern Engel atomic_inc_mb(&pr_reg->pr_res_holders); 1107c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 1108c66ac9dbSNicholas Bellinger return pr_reg; 1109c66ac9dbSNicholas Bellinger } 1110c66ac9dbSNicholas Bellinger /* 1111c66ac9dbSNicholas Bellinger * If the *pr_reg contains a fabric defined ISID for multi-value 1112c66ac9dbSNicholas Bellinger * SCSI Initiator Port TransportIDs, then we expect a valid 1113c66ac9dbSNicholas Bellinger * matching ISID to be provided by the local SCSI Initiator Port. 1114c66ac9dbSNicholas Bellinger */ 11156708bb27SAndy Grover if (!isid) 1116c66ac9dbSNicholas Bellinger continue; 1117c66ac9dbSNicholas Bellinger if (strcmp(isid, pr_reg->pr_reg_isid)) 1118c66ac9dbSNicholas Bellinger continue; 1119c66ac9dbSNicholas Bellinger 112033940d09SJoern Engel atomic_inc_mb(&pr_reg->pr_res_holders); 1121c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 1122c66ac9dbSNicholas Bellinger return pr_reg; 1123c66ac9dbSNicholas Bellinger } 1124c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 1125c66ac9dbSNicholas Bellinger 1126c66ac9dbSNicholas Bellinger return NULL; 1127c66ac9dbSNicholas Bellinger } 1128c66ac9dbSNicholas Bellinger 1129c66ac9dbSNicholas Bellinger static struct t10_pr_registration *core_scsi3_locate_pr_reg( 1130c66ac9dbSNicholas Bellinger struct se_device *dev, 1131c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 1132c66ac9dbSNicholas Bellinger struct se_session *sess) 1133c66ac9dbSNicholas Bellinger { 1134c66ac9dbSNicholas Bellinger struct se_portal_group *tpg = nacl->se_tpg; 1135c66ac9dbSNicholas Bellinger unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL; 1136c66ac9dbSNicholas Bellinger 1137e3d6f909SAndy Grover if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) { 1138c66ac9dbSNicholas Bellinger memset(&buf[0], 0, PR_REG_ISID_LEN); 1139e3d6f909SAndy Grover tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0], 1140c66ac9dbSNicholas Bellinger PR_REG_ISID_LEN); 1141c66ac9dbSNicholas Bellinger isid_ptr = &buf[0]; 1142c66ac9dbSNicholas Bellinger } 1143c66ac9dbSNicholas Bellinger 1144c66ac9dbSNicholas Bellinger return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr); 1145c66ac9dbSNicholas Bellinger } 1146c66ac9dbSNicholas Bellinger 1147c66ac9dbSNicholas Bellinger static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg) 1148c66ac9dbSNicholas Bellinger { 114933940d09SJoern Engel atomic_dec_mb(&pr_reg->pr_res_holders); 1150c66ac9dbSNicholas Bellinger } 1151c66ac9dbSNicholas Bellinger 1152125d0119SHannes Reinecke static int core_scsi3_check_implicit_release( 1153c66ac9dbSNicholas Bellinger struct se_device *dev, 1154c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg) 1155c66ac9dbSNicholas Bellinger { 1156c66ac9dbSNicholas Bellinger struct se_node_acl *nacl = pr_reg->pr_reg_nacl; 1157c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_res_holder; 1158c66ac9dbSNicholas Bellinger int ret = 0; 1159c66ac9dbSNicholas Bellinger 1160c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 1161c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 11626708bb27SAndy Grover if (!pr_res_holder) { 1163c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 1164c66ac9dbSNicholas Bellinger return ret; 1165c66ac9dbSNicholas Bellinger } 1166c66ac9dbSNicholas Bellinger if (pr_res_holder == pr_reg) { 1167c66ac9dbSNicholas Bellinger /* 1168125d0119SHannes Reinecke * Perform an implicit RELEASE if the registration that 1169c66ac9dbSNicholas Bellinger * is being released is holding the reservation. 1170c66ac9dbSNicholas Bellinger * 1171c66ac9dbSNicholas Bellinger * From spc4r17, section 5.7.11.1: 1172c66ac9dbSNicholas Bellinger * 1173c66ac9dbSNicholas Bellinger * e) If the I_T nexus is the persistent reservation holder 1174c66ac9dbSNicholas Bellinger * and the persistent reservation is not an all registrants 1175c66ac9dbSNicholas Bellinger * type, then a PERSISTENT RESERVE OUT command with REGISTER 1176c66ac9dbSNicholas Bellinger * service action or REGISTER AND IGNORE EXISTING KEY 1177c66ac9dbSNicholas Bellinger * service action with the SERVICE ACTION RESERVATION KEY 1178c66ac9dbSNicholas Bellinger * field set to zero (see 5.7.11.3). 1179c66ac9dbSNicholas Bellinger */ 11806c3c9baaSNicholas Bellinger __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0, 1); 1181c66ac9dbSNicholas Bellinger ret = 1; 1182c66ac9dbSNicholas Bellinger /* 1183c66ac9dbSNicholas Bellinger * For 'All Registrants' reservation types, all existing 1184c66ac9dbSNicholas Bellinger * registrations are still processed as reservation holders 1185c66ac9dbSNicholas Bellinger * in core_scsi3_pr_seq_non_holder() after the initial 1186125d0119SHannes Reinecke * reservation holder is implicitly released here. 1187c66ac9dbSNicholas Bellinger */ 1188c66ac9dbSNicholas Bellinger } else if (pr_reg->pr_reg_all_tg_pt && 1189c66ac9dbSNicholas Bellinger (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname, 1190c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl->initiatorname)) && 1191c66ac9dbSNicholas Bellinger (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) { 11926708bb27SAndy Grover pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1" 1193c66ac9dbSNicholas Bellinger " UNREGISTER while existing reservation with matching" 1194c66ac9dbSNicholas Bellinger " key 0x%016Lx is present from another SCSI Initiator" 1195c66ac9dbSNicholas Bellinger " Port\n", pr_reg->pr_res_key); 1196e3d6f909SAndy Grover ret = -EPERM; 1197c66ac9dbSNicholas Bellinger } 1198c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 1199c66ac9dbSNicholas Bellinger 1200c66ac9dbSNicholas Bellinger return ret; 1201c66ac9dbSNicholas Bellinger } 1202c66ac9dbSNicholas Bellinger 1203c66ac9dbSNicholas Bellinger /* 1204e3d6f909SAndy Grover * Called with struct t10_reservation->registration_lock held. 1205c66ac9dbSNicholas Bellinger */ 1206c66ac9dbSNicholas Bellinger static void __core_scsi3_free_registration( 1207c66ac9dbSNicholas Bellinger struct se_device *dev, 1208c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, 1209c66ac9dbSNicholas Bellinger struct list_head *preempt_and_abort_list, 1210c66ac9dbSNicholas Bellinger int dec_holders) 1211c66ac9dbSNicholas Bellinger { 1212c66ac9dbSNicholas Bellinger struct target_core_fabric_ops *tfo = 1213c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo; 12140fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 1215c66ac9dbSNicholas Bellinger char i_buf[PR_REG_ISID_ID_LEN]; 1216c66ac9dbSNicholas Bellinger 1217c66ac9dbSNicholas Bellinger memset(i_buf, 0, PR_REG_ISID_ID_LEN); 1218d2843c17SAndy Grover core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); 1219c66ac9dbSNicholas Bellinger 1220c66ac9dbSNicholas Bellinger pr_reg->pr_reg_deve->def_pr_registered = 0; 1221c66ac9dbSNicholas Bellinger pr_reg->pr_reg_deve->pr_res_key = 0; 12226c3c9baaSNicholas Bellinger if (!list_empty(&pr_reg->pr_reg_list)) 1223c66ac9dbSNicholas Bellinger list_del(&pr_reg->pr_reg_list); 1224c66ac9dbSNicholas Bellinger /* 1225c66ac9dbSNicholas Bellinger * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(), 1226c66ac9dbSNicholas Bellinger * so call core_scsi3_put_pr_reg() to decrement our reference. 1227c66ac9dbSNicholas Bellinger */ 1228c66ac9dbSNicholas Bellinger if (dec_holders) 1229c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg); 1230c66ac9dbSNicholas Bellinger /* 1231c66ac9dbSNicholas Bellinger * Wait until all reference from any other I_T nexuses for this 1232c66ac9dbSNicholas Bellinger * *pr_reg have been released. Because list_del() is called above, 1233c66ac9dbSNicholas Bellinger * the last core_scsi3_put_pr_reg(pr_reg) will release this reference 1234c66ac9dbSNicholas Bellinger * count back to zero, and we release *pr_reg. 1235c66ac9dbSNicholas Bellinger */ 1236c66ac9dbSNicholas Bellinger while (atomic_read(&pr_reg->pr_res_holders) != 0) { 1237c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 12386708bb27SAndy Grover pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n", 1239c66ac9dbSNicholas Bellinger tfo->get_fabric_name()); 1240c66ac9dbSNicholas Bellinger cpu_relax(); 1241c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 1242c66ac9dbSNicholas Bellinger } 1243c66ac9dbSNicholas Bellinger 12446708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator" 1245c66ac9dbSNicholas Bellinger " Node: %s%s\n", tfo->get_fabric_name(), 1246c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl->initiatorname, 1247d2843c17SAndy Grover i_buf); 12486708bb27SAndy Grover pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target" 1249c66ac9dbSNicholas Bellinger " Port(s)\n", tfo->get_fabric_name(), 1250c66ac9dbSNicholas Bellinger (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE", 1251e3d6f909SAndy Grover dev->transport->name); 12526708bb27SAndy Grover pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:" 1253c66ac9dbSNicholas Bellinger " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key, 1254c66ac9dbSNicholas Bellinger pr_reg->pr_res_generation); 1255c66ac9dbSNicholas Bellinger 12566708bb27SAndy Grover if (!preempt_and_abort_list) { 1257c66ac9dbSNicholas Bellinger pr_reg->pr_reg_deve = NULL; 1258c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl = NULL; 1259c66ac9dbSNicholas Bellinger kmem_cache_free(t10_pr_reg_cache, pr_reg); 1260c66ac9dbSNicholas Bellinger return; 1261c66ac9dbSNicholas Bellinger } 1262c66ac9dbSNicholas Bellinger /* 1263c66ac9dbSNicholas Bellinger * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list 1264c66ac9dbSNicholas Bellinger * are released once the ABORT_TASK_SET has completed.. 1265c66ac9dbSNicholas Bellinger */ 1266c66ac9dbSNicholas Bellinger list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list); 1267c66ac9dbSNicholas Bellinger } 1268c66ac9dbSNicholas Bellinger 1269c66ac9dbSNicholas Bellinger void core_scsi3_free_pr_reg_from_nacl( 1270c66ac9dbSNicholas Bellinger struct se_device *dev, 1271c66ac9dbSNicholas Bellinger struct se_node_acl *nacl) 1272c66ac9dbSNicholas Bellinger { 12730fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 1274c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder; 12756c3c9baaSNicholas Bellinger bool free_reg = false; 1276c66ac9dbSNicholas Bellinger /* 1277c66ac9dbSNicholas Bellinger * If the passed se_node_acl matches the reservation holder, 1278c66ac9dbSNicholas Bellinger * release the reservation. 1279c66ac9dbSNicholas Bellinger */ 1280c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 1281c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 1282c66ac9dbSNicholas Bellinger if ((pr_res_holder != NULL) && 12836c3c9baaSNicholas Bellinger (pr_res_holder->pr_reg_nacl == nacl)) { 12846c3c9baaSNicholas Bellinger __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0, 1); 12856c3c9baaSNicholas Bellinger free_reg = true; 12866c3c9baaSNicholas Bellinger } 1287c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 1288c66ac9dbSNicholas Bellinger /* 1289c66ac9dbSNicholas Bellinger * Release any registration associated with the struct se_node_acl. 1290c66ac9dbSNicholas Bellinger */ 1291c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 12926c3c9baaSNicholas Bellinger if (pr_res_holder && free_reg) 12936c3c9baaSNicholas Bellinger __core_scsi3_free_registration(dev, pr_res_holder, NULL, 0); 12946c3c9baaSNicholas Bellinger 1295c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 1296c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 1297c66ac9dbSNicholas Bellinger 1298c66ac9dbSNicholas Bellinger if (pr_reg->pr_reg_nacl != nacl) 1299c66ac9dbSNicholas Bellinger continue; 1300c66ac9dbSNicholas Bellinger 1301c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, pr_reg, NULL, 0); 1302c66ac9dbSNicholas Bellinger } 1303c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 1304c66ac9dbSNicholas Bellinger } 1305c66ac9dbSNicholas Bellinger 1306c66ac9dbSNicholas Bellinger void core_scsi3_free_all_registrations( 1307c66ac9dbSNicholas Bellinger struct se_device *dev) 1308c66ac9dbSNicholas Bellinger { 13090fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 1310c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder; 1311c66ac9dbSNicholas Bellinger 1312c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 1313c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 1314c66ac9dbSNicholas Bellinger if (pr_res_holder != NULL) { 1315c66ac9dbSNicholas Bellinger struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; 1316c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_release(dev, pr_res_nacl, 13176c3c9baaSNicholas Bellinger pr_res_holder, 0, 0); 1318c66ac9dbSNicholas Bellinger } 1319c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 1320c66ac9dbSNicholas Bellinger 1321c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 1322c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 1323c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 1324c66ac9dbSNicholas Bellinger 1325c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, pr_reg, NULL, 0); 1326c66ac9dbSNicholas Bellinger } 1327c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 1328c66ac9dbSNicholas Bellinger 1329c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->aptpl_reg_lock); 1330c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list, 1331c66ac9dbSNicholas Bellinger pr_reg_aptpl_list) { 1332c66ac9dbSNicholas Bellinger list_del(&pr_reg->pr_reg_aptpl_list); 1333c66ac9dbSNicholas Bellinger kmem_cache_free(t10_pr_reg_cache, pr_reg); 1334c66ac9dbSNicholas Bellinger } 1335c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->aptpl_reg_lock); 1336c66ac9dbSNicholas Bellinger } 1337c66ac9dbSNicholas Bellinger 1338c66ac9dbSNicholas Bellinger static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg) 1339c66ac9dbSNicholas Bellinger { 1340e3d6f909SAndy Grover return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys, 1341c66ac9dbSNicholas Bellinger &tpg->tpg_group.cg_item); 1342c66ac9dbSNicholas Bellinger } 1343c66ac9dbSNicholas Bellinger 1344c66ac9dbSNicholas Bellinger static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg) 1345c66ac9dbSNicholas Bellinger { 1346e3d6f909SAndy Grover configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys, 1347c66ac9dbSNicholas Bellinger &tpg->tpg_group.cg_item); 1348c66ac9dbSNicholas Bellinger 134933940d09SJoern Engel atomic_dec_mb(&tpg->tpg_pr_ref_count); 1350c66ac9dbSNicholas Bellinger } 1351c66ac9dbSNicholas Bellinger 1352c66ac9dbSNicholas Bellinger static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl) 1353c66ac9dbSNicholas Bellinger { 1354c66ac9dbSNicholas Bellinger struct se_portal_group *tpg = nacl->se_tpg; 1355c66ac9dbSNicholas Bellinger 1356c66ac9dbSNicholas Bellinger if (nacl->dynamic_node_acl) 1357c66ac9dbSNicholas Bellinger return 0; 1358c66ac9dbSNicholas Bellinger 1359e3d6f909SAndy Grover return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys, 1360c66ac9dbSNicholas Bellinger &nacl->acl_group.cg_item); 1361c66ac9dbSNicholas Bellinger } 1362c66ac9dbSNicholas Bellinger 1363c66ac9dbSNicholas Bellinger static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl) 1364c66ac9dbSNicholas Bellinger { 1365c66ac9dbSNicholas Bellinger struct se_portal_group *tpg = nacl->se_tpg; 1366c66ac9dbSNicholas Bellinger 1367c66ac9dbSNicholas Bellinger if (nacl->dynamic_node_acl) { 136833940d09SJoern Engel atomic_dec_mb(&nacl->acl_pr_ref_count); 1369c66ac9dbSNicholas Bellinger return; 1370c66ac9dbSNicholas Bellinger } 1371c66ac9dbSNicholas Bellinger 1372e3d6f909SAndy Grover configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys, 1373c66ac9dbSNicholas Bellinger &nacl->acl_group.cg_item); 1374c66ac9dbSNicholas Bellinger 137533940d09SJoern Engel atomic_dec_mb(&nacl->acl_pr_ref_count); 1376c66ac9dbSNicholas Bellinger } 1377c66ac9dbSNicholas Bellinger 1378c66ac9dbSNicholas Bellinger static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve) 1379c66ac9dbSNicholas Bellinger { 1380c66ac9dbSNicholas Bellinger struct se_lun_acl *lun_acl = se_deve->se_lun_acl; 1381c66ac9dbSNicholas Bellinger struct se_node_acl *nacl; 1382c66ac9dbSNicholas Bellinger struct se_portal_group *tpg; 1383c66ac9dbSNicholas Bellinger /* 1384c66ac9dbSNicholas Bellinger * For nacl->dynamic_node_acl=1 1385c66ac9dbSNicholas Bellinger */ 13866708bb27SAndy Grover if (!lun_acl) 1387c66ac9dbSNicholas Bellinger return 0; 1388c66ac9dbSNicholas Bellinger 1389c66ac9dbSNicholas Bellinger nacl = lun_acl->se_lun_nacl; 1390c66ac9dbSNicholas Bellinger tpg = nacl->se_tpg; 1391c66ac9dbSNicholas Bellinger 1392e3d6f909SAndy Grover return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys, 1393c66ac9dbSNicholas Bellinger &lun_acl->se_lun_group.cg_item); 1394c66ac9dbSNicholas Bellinger } 1395c66ac9dbSNicholas Bellinger 1396c66ac9dbSNicholas Bellinger static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve) 1397c66ac9dbSNicholas Bellinger { 1398c66ac9dbSNicholas Bellinger struct se_lun_acl *lun_acl = se_deve->se_lun_acl; 1399c66ac9dbSNicholas Bellinger struct se_node_acl *nacl; 1400c66ac9dbSNicholas Bellinger struct se_portal_group *tpg; 1401c66ac9dbSNicholas Bellinger /* 1402c66ac9dbSNicholas Bellinger * For nacl->dynamic_node_acl=1 1403c66ac9dbSNicholas Bellinger */ 14046708bb27SAndy Grover if (!lun_acl) { 140533940d09SJoern Engel atomic_dec_mb(&se_deve->pr_ref_count); 1406c66ac9dbSNicholas Bellinger return; 1407c66ac9dbSNicholas Bellinger } 1408c66ac9dbSNicholas Bellinger nacl = lun_acl->se_lun_nacl; 1409c66ac9dbSNicholas Bellinger tpg = nacl->se_tpg; 1410c66ac9dbSNicholas Bellinger 1411e3d6f909SAndy Grover configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys, 1412c66ac9dbSNicholas Bellinger &lun_acl->se_lun_group.cg_item); 1413c66ac9dbSNicholas Bellinger 141433940d09SJoern Engel atomic_dec_mb(&se_deve->pr_ref_count); 1415c66ac9dbSNicholas Bellinger } 1416c66ac9dbSNicholas Bellinger 1417de103c93SChristoph Hellwig static sense_reason_t 1418de103c93SChristoph Hellwig core_scsi3_decode_spec_i_port( 1419c66ac9dbSNicholas Bellinger struct se_cmd *cmd, 1420c66ac9dbSNicholas Bellinger struct se_portal_group *tpg, 1421c66ac9dbSNicholas Bellinger unsigned char *l_isid, 1422c66ac9dbSNicholas Bellinger u64 sa_res_key, 1423c66ac9dbSNicholas Bellinger int all_tg_pt, 1424c66ac9dbSNicholas Bellinger int aptpl) 1425c66ac9dbSNicholas Bellinger { 14265951146dSAndy Grover struct se_device *dev = cmd->se_dev; 1427c66ac9dbSNicholas Bellinger struct se_port *tmp_port; 1428c66ac9dbSNicholas Bellinger struct se_portal_group *dest_tpg = NULL, *tmp_tpg; 1429e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 1430c66ac9dbSNicholas Bellinger struct se_node_acl *dest_node_acl = NULL; 1431c66ac9dbSNicholas Bellinger struct se_dev_entry *dest_se_deve = NULL, *local_se_deve; 1432c66ac9dbSNicholas Bellinger struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e; 1433c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe; 1434d0f474e5SRoland Dreier LIST_HEAD(tid_dest_list); 1435c66ac9dbSNicholas Bellinger struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp; 1436c66ac9dbSNicholas Bellinger struct target_core_fabric_ops *tmp_tf_ops; 143705d1c7c0SAndy Grover unsigned char *buf; 1438c66ac9dbSNicholas Bellinger unsigned char *ptr, *i_str = NULL, proto_ident, tmp_proto_ident; 143913ba564cSJulia Lawall char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN]; 1440de103c93SChristoph Hellwig sense_reason_t ret; 1441c66ac9dbSNicholas Bellinger u32 tpdl, tid_len = 0; 1442d2843c17SAndy Grover int dest_local_nexus; 1443c66ac9dbSNicholas Bellinger u32 dest_rtpi = 0; 1444c66ac9dbSNicholas Bellinger 1445f2083241SJörn Engel local_se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun]; 1446c66ac9dbSNicholas Bellinger /* 1447c66ac9dbSNicholas Bellinger * Allocate a struct pr_transport_id_holder and setup the 1448c66ac9dbSNicholas Bellinger * local_node_acl and local_se_deve pointers and add to 1449c66ac9dbSNicholas Bellinger * struct list_head tid_dest_list for add registration 1450c66ac9dbSNicholas Bellinger * processing in the loop of tid_dest_list below. 1451c66ac9dbSNicholas Bellinger */ 1452c66ac9dbSNicholas Bellinger tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL); 14536708bb27SAndy Grover if (!tidh_new) { 14546708bb27SAndy Grover pr_err("Unable to allocate tidh_new\n"); 1455de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1456c66ac9dbSNicholas Bellinger } 1457c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&tidh_new->dest_list); 1458c66ac9dbSNicholas Bellinger tidh_new->dest_tpg = tpg; 1459c66ac9dbSNicholas Bellinger tidh_new->dest_node_acl = se_sess->se_node_acl; 1460c66ac9dbSNicholas Bellinger tidh_new->dest_se_deve = local_se_deve; 1461c66ac9dbSNicholas Bellinger 14625951146dSAndy Grover local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev, 1463c66ac9dbSNicholas Bellinger se_sess->se_node_acl, local_se_deve, l_isid, 1464c66ac9dbSNicholas Bellinger sa_res_key, all_tg_pt, aptpl); 14656708bb27SAndy Grover if (!local_pr_reg) { 1466c66ac9dbSNicholas Bellinger kfree(tidh_new); 1467de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1468c66ac9dbSNicholas Bellinger } 1469c66ac9dbSNicholas Bellinger tidh_new->dest_pr_reg = local_pr_reg; 1470c66ac9dbSNicholas Bellinger /* 1471c66ac9dbSNicholas Bellinger * The local I_T nexus does not hold any configfs dependances, 1472c66ac9dbSNicholas Bellinger * so we set tid_h->dest_local_nexus=1 to prevent the 1473c66ac9dbSNicholas Bellinger * configfs_undepend_item() calls in the tid_dest_list loops below. 1474c66ac9dbSNicholas Bellinger */ 1475c66ac9dbSNicholas Bellinger tidh_new->dest_local_nexus = 1; 1476c66ac9dbSNicholas Bellinger list_add_tail(&tidh_new->dest_list, &tid_dest_list); 147705d1c7c0SAndy Grover 14780d7f1299SPaolo Bonzini if (cmd->data_length < 28) { 14790d7f1299SPaolo Bonzini pr_warn("SPC-PR: Received PR OUT parameter list" 14800d7f1299SPaolo Bonzini " length too small: %u\n", cmd->data_length); 1481de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 14820d7f1299SPaolo Bonzini goto out; 14830d7f1299SPaolo Bonzini } 14840d7f1299SPaolo Bonzini 14854949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 1486de103c93SChristoph Hellwig if (!buf) { 1487de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1488de103c93SChristoph Hellwig goto out; 1489de103c93SChristoph Hellwig } 1490de103c93SChristoph Hellwig 1491c66ac9dbSNicholas Bellinger /* 1492c66ac9dbSNicholas Bellinger * For a PERSISTENT RESERVE OUT specify initiator ports payload, 1493c66ac9dbSNicholas Bellinger * first extract TransportID Parameter Data Length, and make sure 1494c66ac9dbSNicholas Bellinger * the value matches up to the SCSI expected data transfer length. 1495c66ac9dbSNicholas Bellinger */ 1496c66ac9dbSNicholas Bellinger tpdl = (buf[24] & 0xff) << 24; 1497c66ac9dbSNicholas Bellinger tpdl |= (buf[25] & 0xff) << 16; 1498c66ac9dbSNicholas Bellinger tpdl |= (buf[26] & 0xff) << 8; 1499c66ac9dbSNicholas Bellinger tpdl |= buf[27] & 0xff; 1500c66ac9dbSNicholas Bellinger 1501c66ac9dbSNicholas Bellinger if ((tpdl + 28) != cmd->data_length) { 15026708bb27SAndy Grover pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header" 1503c66ac9dbSNicholas Bellinger " does not equal CDB data_length: %u\n", tpdl, 1504c66ac9dbSNicholas Bellinger cmd->data_length); 1505de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 1506de103c93SChristoph Hellwig goto out_unmap; 1507c66ac9dbSNicholas Bellinger } 1508c66ac9dbSNicholas Bellinger /* 1509c66ac9dbSNicholas Bellinger * Start processing the received transport IDs using the 1510c66ac9dbSNicholas Bellinger * receiving I_T Nexus portal's fabric dependent methods to 1511c66ac9dbSNicholas Bellinger * obtain the SCSI Initiator Port/Device Identifiers. 1512c66ac9dbSNicholas Bellinger */ 1513c66ac9dbSNicholas Bellinger ptr = &buf[28]; 1514c66ac9dbSNicholas Bellinger 1515c66ac9dbSNicholas Bellinger while (tpdl > 0) { 1516c66ac9dbSNicholas Bellinger proto_ident = (ptr[0] & 0x0f); 1517c66ac9dbSNicholas Bellinger dest_tpg = NULL; 1518c66ac9dbSNicholas Bellinger 1519c66ac9dbSNicholas Bellinger spin_lock(&dev->se_port_lock); 1520c66ac9dbSNicholas Bellinger list_for_each_entry(tmp_port, &dev->dev_sep_list, sep_list) { 1521c66ac9dbSNicholas Bellinger tmp_tpg = tmp_port->sep_tpg; 15226708bb27SAndy Grover if (!tmp_tpg) 1523c66ac9dbSNicholas Bellinger continue; 1524e3d6f909SAndy Grover tmp_tf_ops = tmp_tpg->se_tpg_tfo; 15256708bb27SAndy Grover if (!tmp_tf_ops) 1526c66ac9dbSNicholas Bellinger continue; 15276708bb27SAndy Grover if (!tmp_tf_ops->get_fabric_proto_ident || 15286708bb27SAndy Grover !tmp_tf_ops->tpg_parse_pr_out_transport_id) 1529c66ac9dbSNicholas Bellinger continue; 1530c66ac9dbSNicholas Bellinger /* 1531c66ac9dbSNicholas Bellinger * Look for the matching proto_ident provided by 1532c66ac9dbSNicholas Bellinger * the received TransportID 1533c66ac9dbSNicholas Bellinger */ 1534c66ac9dbSNicholas Bellinger tmp_proto_ident = tmp_tf_ops->get_fabric_proto_ident(tmp_tpg); 1535c66ac9dbSNicholas Bellinger if (tmp_proto_ident != proto_ident) 1536c66ac9dbSNicholas Bellinger continue; 1537c66ac9dbSNicholas Bellinger dest_rtpi = tmp_port->sep_rtpi; 1538c66ac9dbSNicholas Bellinger 1539c66ac9dbSNicholas Bellinger i_str = tmp_tf_ops->tpg_parse_pr_out_transport_id( 1540c66ac9dbSNicholas Bellinger tmp_tpg, (const char *)ptr, &tid_len, 1541c66ac9dbSNicholas Bellinger &iport_ptr); 15426708bb27SAndy Grover if (!i_str) 1543c66ac9dbSNicholas Bellinger continue; 1544c66ac9dbSNicholas Bellinger 154533940d09SJoern Engel atomic_inc_mb(&tmp_tpg->tpg_pr_ref_count); 1546c66ac9dbSNicholas Bellinger spin_unlock(&dev->se_port_lock); 1547c66ac9dbSNicholas Bellinger 1548de103c93SChristoph Hellwig if (core_scsi3_tpg_depend_item(tmp_tpg)) { 15496708bb27SAndy Grover pr_err(" core_scsi3_tpg_depend_item()" 1550c66ac9dbSNicholas Bellinger " for tmp_tpg\n"); 155133940d09SJoern Engel atomic_dec_mb(&tmp_tpg->tpg_pr_ref_count); 1552de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1553de103c93SChristoph Hellwig goto out_unmap; 1554c66ac9dbSNicholas Bellinger } 1555c66ac9dbSNicholas Bellinger /* 155635d1efe8SMasanari Iida * Locate the destination initiator ACL to be registered 1557c66ac9dbSNicholas Bellinger * from the decoded fabric module specific TransportID 1558c66ac9dbSNicholas Bellinger * at *i_str. 1559c66ac9dbSNicholas Bellinger */ 156028638887SRoland Dreier spin_lock_irq(&tmp_tpg->acl_node_lock); 1561c66ac9dbSNicholas Bellinger dest_node_acl = __core_tpg_get_initiator_node_acl( 1562c66ac9dbSNicholas Bellinger tmp_tpg, i_str); 156333940d09SJoern Engel if (dest_node_acl) 156433940d09SJoern Engel atomic_inc_mb(&dest_node_acl->acl_pr_ref_count); 156528638887SRoland Dreier spin_unlock_irq(&tmp_tpg->acl_node_lock); 1566c66ac9dbSNicholas Bellinger 15676708bb27SAndy Grover if (!dest_node_acl) { 1568c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(tmp_tpg); 1569c66ac9dbSNicholas Bellinger spin_lock(&dev->se_port_lock); 1570c66ac9dbSNicholas Bellinger continue; 1571c66ac9dbSNicholas Bellinger } 1572c66ac9dbSNicholas Bellinger 1573de103c93SChristoph Hellwig if (core_scsi3_nodeacl_depend_item(dest_node_acl)) { 15746708bb27SAndy Grover pr_err("configfs_depend_item() failed" 1575c66ac9dbSNicholas Bellinger " for dest_node_acl->acl_group\n"); 157633940d09SJoern Engel atomic_dec_mb(&dest_node_acl->acl_pr_ref_count); 1577c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(tmp_tpg); 1578de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1579de103c93SChristoph Hellwig goto out_unmap; 1580c66ac9dbSNicholas Bellinger } 1581c66ac9dbSNicholas Bellinger 1582c66ac9dbSNicholas Bellinger dest_tpg = tmp_tpg; 15836708bb27SAndy Grover pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:" 1584c66ac9dbSNicholas Bellinger " %s Port RTPI: %hu\n", 1585e3d6f909SAndy Grover dest_tpg->se_tpg_tfo->get_fabric_name(), 1586c66ac9dbSNicholas Bellinger dest_node_acl->initiatorname, dest_rtpi); 1587c66ac9dbSNicholas Bellinger 1588c66ac9dbSNicholas Bellinger spin_lock(&dev->se_port_lock); 1589c66ac9dbSNicholas Bellinger break; 1590c66ac9dbSNicholas Bellinger } 1591c66ac9dbSNicholas Bellinger spin_unlock(&dev->se_port_lock); 1592c66ac9dbSNicholas Bellinger 15936708bb27SAndy Grover if (!dest_tpg) { 15946708bb27SAndy Grover pr_err("SPC-3 PR SPEC_I_PT: Unable to locate" 1595c66ac9dbSNicholas Bellinger " dest_tpg\n"); 1596de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 1597de103c93SChristoph Hellwig goto out_unmap; 1598c66ac9dbSNicholas Bellinger } 15998b1e1244SAndy Grover 16006708bb27SAndy Grover pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u" 1601c66ac9dbSNicholas Bellinger " tid_len: %d for %s + %s\n", 1602e3d6f909SAndy Grover dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length, 1603c66ac9dbSNicholas Bellinger tpdl, tid_len, i_str, iport_ptr); 16048b1e1244SAndy Grover 1605c66ac9dbSNicholas Bellinger if (tid_len > tpdl) { 16066708bb27SAndy Grover pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:" 1607c66ac9dbSNicholas Bellinger " %u for Transport ID: %s\n", tid_len, ptr); 1608c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1609c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1610de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 1611de103c93SChristoph Hellwig goto out_unmap; 1612c66ac9dbSNicholas Bellinger } 1613c66ac9dbSNicholas Bellinger /* 1614c66ac9dbSNicholas Bellinger * Locate the desintation struct se_dev_entry pointer for matching 1615c66ac9dbSNicholas Bellinger * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus 1616c66ac9dbSNicholas Bellinger * Target Port. 1617c66ac9dbSNicholas Bellinger */ 1618c66ac9dbSNicholas Bellinger dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, 1619c66ac9dbSNicholas Bellinger dest_rtpi); 16206708bb27SAndy Grover if (!dest_se_deve) { 16216708bb27SAndy Grover pr_err("Unable to locate %s dest_se_deve" 1622c66ac9dbSNicholas Bellinger " from destination RTPI: %hu\n", 1623e3d6f909SAndy Grover dest_tpg->se_tpg_tfo->get_fabric_name(), 1624c66ac9dbSNicholas Bellinger dest_rtpi); 1625c66ac9dbSNicholas Bellinger 1626c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1627c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1628de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 1629de103c93SChristoph Hellwig goto out_unmap; 1630c66ac9dbSNicholas Bellinger } 1631c66ac9dbSNicholas Bellinger 1632de103c93SChristoph Hellwig if (core_scsi3_lunacl_depend_item(dest_se_deve)) { 16336708bb27SAndy Grover pr_err("core_scsi3_lunacl_depend_item()" 1634c66ac9dbSNicholas Bellinger " failed\n"); 163533940d09SJoern Engel atomic_dec_mb(&dest_se_deve->pr_ref_count); 1636c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1637c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1638de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1639de103c93SChristoph Hellwig goto out_unmap; 1640c66ac9dbSNicholas Bellinger } 16418b1e1244SAndy Grover 16426708bb27SAndy Grover pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s" 1643c66ac9dbSNicholas Bellinger " dest_se_deve mapped_lun: %u\n", 1644e3d6f909SAndy Grover dest_tpg->se_tpg_tfo->get_fabric_name(), 1645c66ac9dbSNicholas Bellinger dest_node_acl->initiatorname, dest_se_deve->mapped_lun); 16468b1e1244SAndy Grover 1647c66ac9dbSNicholas Bellinger /* 1648c66ac9dbSNicholas Bellinger * Skip any TransportIDs that already have a registration for 1649c66ac9dbSNicholas Bellinger * this target port. 1650c66ac9dbSNicholas Bellinger */ 1651c66ac9dbSNicholas Bellinger pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl, 1652c66ac9dbSNicholas Bellinger iport_ptr); 1653c66ac9dbSNicholas Bellinger if (pr_reg_e) { 1654c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_e); 1655c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(dest_se_deve); 1656c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1657c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1658c66ac9dbSNicholas Bellinger ptr += tid_len; 1659c66ac9dbSNicholas Bellinger tpdl -= tid_len; 1660c66ac9dbSNicholas Bellinger tid_len = 0; 1661c66ac9dbSNicholas Bellinger continue; 1662c66ac9dbSNicholas Bellinger } 1663c66ac9dbSNicholas Bellinger /* 1664c66ac9dbSNicholas Bellinger * Allocate a struct pr_transport_id_holder and setup 1665c66ac9dbSNicholas Bellinger * the dest_node_acl and dest_se_deve pointers for the 1666c66ac9dbSNicholas Bellinger * loop below. 1667c66ac9dbSNicholas Bellinger */ 1668c66ac9dbSNicholas Bellinger tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), 1669c66ac9dbSNicholas Bellinger GFP_KERNEL); 16706708bb27SAndy Grover if (!tidh_new) { 16716708bb27SAndy Grover pr_err("Unable to allocate tidh_new\n"); 1672c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(dest_se_deve); 1673c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1674c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1675de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1676de103c93SChristoph Hellwig goto out_unmap; 1677c66ac9dbSNicholas Bellinger } 1678c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&tidh_new->dest_list); 1679c66ac9dbSNicholas Bellinger tidh_new->dest_tpg = dest_tpg; 1680c66ac9dbSNicholas Bellinger tidh_new->dest_node_acl = dest_node_acl; 1681c66ac9dbSNicholas Bellinger tidh_new->dest_se_deve = dest_se_deve; 1682c66ac9dbSNicholas Bellinger 1683c66ac9dbSNicholas Bellinger /* 1684c66ac9dbSNicholas Bellinger * Allocate, but do NOT add the registration for the 1685c66ac9dbSNicholas Bellinger * TransportID referenced SCSI Initiator port. This 1686c66ac9dbSNicholas Bellinger * done because of the following from spc4r17 in section 1687c66ac9dbSNicholas Bellinger * 6.14.3 wrt SPEC_I_PT: 1688c66ac9dbSNicholas Bellinger * 1689c66ac9dbSNicholas Bellinger * "If a registration fails for any initiator port (e.g., if th 1690c66ac9dbSNicholas Bellinger * logical unit does not have enough resources available to 1691c66ac9dbSNicholas Bellinger * hold the registration information), no registrations shall be 1692c66ac9dbSNicholas Bellinger * made, and the command shall be terminated with 1693c66ac9dbSNicholas Bellinger * CHECK CONDITION status." 1694c66ac9dbSNicholas Bellinger * 1695c66ac9dbSNicholas Bellinger * That means we call __core_scsi3_alloc_registration() here, 1696c66ac9dbSNicholas Bellinger * and then call __core_scsi3_add_registration() in the 1697c66ac9dbSNicholas Bellinger * 2nd loop which will never fail. 1698c66ac9dbSNicholas Bellinger */ 16995951146dSAndy Grover dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev, 1700c66ac9dbSNicholas Bellinger dest_node_acl, dest_se_deve, iport_ptr, 1701c66ac9dbSNicholas Bellinger sa_res_key, all_tg_pt, aptpl); 17026708bb27SAndy Grover if (!dest_pr_reg) { 1703c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(dest_se_deve); 1704c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1705c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1706c66ac9dbSNicholas Bellinger kfree(tidh_new); 1707de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 1708de103c93SChristoph Hellwig goto out_unmap; 1709c66ac9dbSNicholas Bellinger } 1710c66ac9dbSNicholas Bellinger tidh_new->dest_pr_reg = dest_pr_reg; 1711c66ac9dbSNicholas Bellinger list_add_tail(&tidh_new->dest_list, &tid_dest_list); 1712c66ac9dbSNicholas Bellinger 1713c66ac9dbSNicholas Bellinger ptr += tid_len; 1714c66ac9dbSNicholas Bellinger tpdl -= tid_len; 1715c66ac9dbSNicholas Bellinger tid_len = 0; 1716c66ac9dbSNicholas Bellinger 1717c66ac9dbSNicholas Bellinger } 171805d1c7c0SAndy Grover 17194949314cSAndy Grover transport_kunmap_data_sg(cmd); 172005d1c7c0SAndy Grover 1721c66ac9dbSNicholas Bellinger /* 1722c66ac9dbSNicholas Bellinger * Go ahead and create a registrations from tid_dest_list for the 1723c66ac9dbSNicholas Bellinger * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl 1724c66ac9dbSNicholas Bellinger * and dest_se_deve. 1725c66ac9dbSNicholas Bellinger * 1726c66ac9dbSNicholas Bellinger * The SA Reservation Key from the PROUT is set for the 1727c66ac9dbSNicholas Bellinger * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1 1728c66ac9dbSNicholas Bellinger * means that the TransportID Initiator port will be 1729c66ac9dbSNicholas Bellinger * registered on all of the target ports in the SCSI target device 1730c66ac9dbSNicholas Bellinger * ALL_TG_PT=0 means the registration will only be for the 1731c66ac9dbSNicholas Bellinger * SCSI target port the PROUT REGISTER with SPEC_I_PT=1 1732c66ac9dbSNicholas Bellinger * was received. 1733c66ac9dbSNicholas Bellinger */ 1734c66ac9dbSNicholas Bellinger list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) { 1735c66ac9dbSNicholas Bellinger dest_tpg = tidh->dest_tpg; 1736c66ac9dbSNicholas Bellinger dest_node_acl = tidh->dest_node_acl; 1737c66ac9dbSNicholas Bellinger dest_se_deve = tidh->dest_se_deve; 1738c66ac9dbSNicholas Bellinger dest_pr_reg = tidh->dest_pr_reg; 1739c66ac9dbSNicholas Bellinger dest_local_nexus = tidh->dest_local_nexus; 1740c66ac9dbSNicholas Bellinger 1741c66ac9dbSNicholas Bellinger list_del(&tidh->dest_list); 1742c66ac9dbSNicholas Bellinger kfree(tidh); 1743c66ac9dbSNicholas Bellinger 1744c66ac9dbSNicholas Bellinger memset(i_buf, 0, PR_REG_ISID_ID_LEN); 1745d2843c17SAndy Grover core_pr_dump_initiator_port(dest_pr_reg, i_buf, PR_REG_ISID_ID_LEN); 1746c66ac9dbSNicholas Bellinger 17475951146dSAndy Grover __core_scsi3_add_registration(cmd->se_dev, dest_node_acl, 1748c66ac9dbSNicholas Bellinger dest_pr_reg, 0, 0); 1749c66ac9dbSNicholas Bellinger 17506708bb27SAndy Grover pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully" 1751c66ac9dbSNicholas Bellinger " registered Transport ID for Node: %s%s Mapped LUN:" 1752e3d6f909SAndy Grover " %u\n", dest_tpg->se_tpg_tfo->get_fabric_name(), 1753d2843c17SAndy Grover dest_node_acl->initiatorname, i_buf, dest_se_deve->mapped_lun); 1754c66ac9dbSNicholas Bellinger 1755c66ac9dbSNicholas Bellinger if (dest_local_nexus) 1756c66ac9dbSNicholas Bellinger continue; 1757c66ac9dbSNicholas Bellinger 1758c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(dest_se_deve); 1759c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1760c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1761c66ac9dbSNicholas Bellinger } 1762c66ac9dbSNicholas Bellinger 1763c66ac9dbSNicholas Bellinger return 0; 1764de103c93SChristoph Hellwig out_unmap: 17654949314cSAndy Grover transport_kunmap_data_sg(cmd); 1766de103c93SChristoph Hellwig out: 1767c66ac9dbSNicholas Bellinger /* 1768c66ac9dbSNicholas Bellinger * For the failure case, release everything from tid_dest_list 1769c66ac9dbSNicholas Bellinger * including *dest_pr_reg and the configfs dependances.. 1770c66ac9dbSNicholas Bellinger */ 1771c66ac9dbSNicholas Bellinger list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) { 1772c66ac9dbSNicholas Bellinger dest_tpg = tidh->dest_tpg; 1773c66ac9dbSNicholas Bellinger dest_node_acl = tidh->dest_node_acl; 1774c66ac9dbSNicholas Bellinger dest_se_deve = tidh->dest_se_deve; 1775c66ac9dbSNicholas Bellinger dest_pr_reg = tidh->dest_pr_reg; 1776c66ac9dbSNicholas Bellinger dest_local_nexus = tidh->dest_local_nexus; 1777c66ac9dbSNicholas Bellinger 1778c66ac9dbSNicholas Bellinger list_del(&tidh->dest_list); 1779c66ac9dbSNicholas Bellinger kfree(tidh); 1780c66ac9dbSNicholas Bellinger /* 1781c66ac9dbSNicholas Bellinger * Release any extra ALL_TG_PT=1 registrations for 1782c66ac9dbSNicholas Bellinger * the SPEC_I_PT=1 case. 1783c66ac9dbSNicholas Bellinger */ 1784c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe, 1785c66ac9dbSNicholas Bellinger &dest_pr_reg->pr_reg_atp_list, 1786c66ac9dbSNicholas Bellinger pr_reg_atp_mem_list) { 1787c66ac9dbSNicholas Bellinger list_del(&pr_reg_tmp->pr_reg_atp_mem_list); 1788c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve); 1789c66ac9dbSNicholas Bellinger kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp); 1790c66ac9dbSNicholas Bellinger } 1791c66ac9dbSNicholas Bellinger 1792c66ac9dbSNicholas Bellinger kmem_cache_free(t10_pr_reg_cache, dest_pr_reg); 1793c66ac9dbSNicholas Bellinger 1794c66ac9dbSNicholas Bellinger if (dest_local_nexus) 1795c66ac9dbSNicholas Bellinger continue; 1796c66ac9dbSNicholas Bellinger 1797c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(dest_se_deve); 1798c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1799c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1800c66ac9dbSNicholas Bellinger } 1801c66ac9dbSNicholas Bellinger return ret; 1802c66ac9dbSNicholas Bellinger } 1803c66ac9dbSNicholas Bellinger 18043c8a6228SAndy Grover static int core_scsi3_update_aptpl_buf( 1805c66ac9dbSNicholas Bellinger struct se_device *dev, 1806c66ac9dbSNicholas Bellinger unsigned char *buf, 18070607decdSAndy Grover u32 pr_aptpl_buf_len) 1808c66ac9dbSNicholas Bellinger { 1809c66ac9dbSNicholas Bellinger struct se_lun *lun; 1810c66ac9dbSNicholas Bellinger struct se_portal_group *tpg; 1811c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg; 1812c66ac9dbSNicholas Bellinger unsigned char tmp[512], isid_buf[32]; 1813c66ac9dbSNicholas Bellinger ssize_t len = 0; 1814c66ac9dbSNicholas Bellinger int reg_count = 0; 18153c8a6228SAndy Grover int ret = 0; 1816c66ac9dbSNicholas Bellinger 18173c8a6228SAndy Grover spin_lock(&dev->dev_reservation_lock); 18183c8a6228SAndy Grover spin_lock(&dev->t10_pr.registration_lock); 1819c66ac9dbSNicholas Bellinger /* 1820c66ac9dbSNicholas Bellinger * Walk the registration list.. 1821c66ac9dbSNicholas Bellinger */ 18220fd97ccfSChristoph Hellwig list_for_each_entry(pr_reg, &dev->t10_pr.registration_list, 1823c66ac9dbSNicholas Bellinger pr_reg_list) { 1824c66ac9dbSNicholas Bellinger 1825c66ac9dbSNicholas Bellinger tmp[0] = '\0'; 1826c66ac9dbSNicholas Bellinger isid_buf[0] = '\0'; 1827c66ac9dbSNicholas Bellinger tpg = pr_reg->pr_reg_nacl->se_tpg; 1828c66ac9dbSNicholas Bellinger lun = pr_reg->pr_reg_tg_pt_lun; 1829c66ac9dbSNicholas Bellinger /* 1830c66ac9dbSNicholas Bellinger * Write out any ISID value to APTPL metadata that was included 1831c66ac9dbSNicholas Bellinger * in the original registration. 1832c66ac9dbSNicholas Bellinger */ 1833c66ac9dbSNicholas Bellinger if (pr_reg->isid_present_at_reg) 1834c66ac9dbSNicholas Bellinger snprintf(isid_buf, 32, "initiator_sid=%s\n", 1835c66ac9dbSNicholas Bellinger pr_reg->pr_reg_isid); 1836c66ac9dbSNicholas Bellinger /* 1837c66ac9dbSNicholas Bellinger * Include special metadata if the pr_reg matches the 1838c66ac9dbSNicholas Bellinger * reservation holder. 1839c66ac9dbSNicholas Bellinger */ 1840c66ac9dbSNicholas Bellinger if (dev->dev_pr_res_holder == pr_reg) { 1841c66ac9dbSNicholas Bellinger snprintf(tmp, 512, "PR_REG_START: %d" 1842c66ac9dbSNicholas Bellinger "\ninitiator_fabric=%s\n" 1843c66ac9dbSNicholas Bellinger "initiator_node=%s\n%s" 1844c66ac9dbSNicholas Bellinger "sa_res_key=%llu\n" 1845c66ac9dbSNicholas Bellinger "res_holder=1\nres_type=%02x\n" 1846c66ac9dbSNicholas Bellinger "res_scope=%02x\nres_all_tg_pt=%d\n" 1847c66ac9dbSNicholas Bellinger "mapped_lun=%u\n", reg_count, 1848e3d6f909SAndy Grover tpg->se_tpg_tfo->get_fabric_name(), 1849c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl->initiatorname, isid_buf, 1850c66ac9dbSNicholas Bellinger pr_reg->pr_res_key, pr_reg->pr_res_type, 1851c66ac9dbSNicholas Bellinger pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt, 1852c66ac9dbSNicholas Bellinger pr_reg->pr_res_mapped_lun); 1853c66ac9dbSNicholas Bellinger } else { 1854c66ac9dbSNicholas Bellinger snprintf(tmp, 512, "PR_REG_START: %d\n" 1855c66ac9dbSNicholas Bellinger "initiator_fabric=%s\ninitiator_node=%s\n%s" 1856c66ac9dbSNicholas Bellinger "sa_res_key=%llu\nres_holder=0\n" 1857c66ac9dbSNicholas Bellinger "res_all_tg_pt=%d\nmapped_lun=%u\n", 1858e3d6f909SAndy Grover reg_count, tpg->se_tpg_tfo->get_fabric_name(), 1859c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl->initiatorname, isid_buf, 1860c66ac9dbSNicholas Bellinger pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt, 1861c66ac9dbSNicholas Bellinger pr_reg->pr_res_mapped_lun); 1862c66ac9dbSNicholas Bellinger } 1863c66ac9dbSNicholas Bellinger 186460d645a4SDan Carpenter if ((len + strlen(tmp) >= pr_aptpl_buf_len)) { 18656708bb27SAndy Grover pr_err("Unable to update renaming" 1866c66ac9dbSNicholas Bellinger " APTPL metadata\n"); 18673c8a6228SAndy Grover ret = -EMSGSIZE; 18683c8a6228SAndy Grover goto out; 1869c66ac9dbSNicholas Bellinger } 1870c66ac9dbSNicholas Bellinger len += sprintf(buf+len, "%s", tmp); 1871c66ac9dbSNicholas Bellinger 1872c66ac9dbSNicholas Bellinger /* 1873c66ac9dbSNicholas Bellinger * Include information about the associated SCSI target port. 1874c66ac9dbSNicholas Bellinger */ 1875c66ac9dbSNicholas Bellinger snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n" 1876c66ac9dbSNicholas Bellinger "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:" 1877e3d6f909SAndy Grover " %d\n", tpg->se_tpg_tfo->get_fabric_name(), 1878e3d6f909SAndy Grover tpg->se_tpg_tfo->tpg_get_wwn(tpg), 1879e3d6f909SAndy Grover tpg->se_tpg_tfo->tpg_get_tag(tpg), 1880c66ac9dbSNicholas Bellinger lun->lun_sep->sep_rtpi, lun->unpacked_lun, reg_count); 1881c66ac9dbSNicholas Bellinger 188260d645a4SDan Carpenter if ((len + strlen(tmp) >= pr_aptpl_buf_len)) { 18836708bb27SAndy Grover pr_err("Unable to update renaming" 1884c66ac9dbSNicholas Bellinger " APTPL metadata\n"); 18853c8a6228SAndy Grover ret = -EMSGSIZE; 18863c8a6228SAndy Grover goto out; 1887c66ac9dbSNicholas Bellinger } 1888c66ac9dbSNicholas Bellinger len += sprintf(buf+len, "%s", tmp); 1889c66ac9dbSNicholas Bellinger reg_count++; 1890c66ac9dbSNicholas Bellinger } 1891c66ac9dbSNicholas Bellinger 18926708bb27SAndy Grover if (!reg_count) 1893c66ac9dbSNicholas Bellinger len += sprintf(buf+len, "No Registrations or Reservations"); 1894c66ac9dbSNicholas Bellinger 18953c8a6228SAndy Grover out: 18963c8a6228SAndy Grover spin_unlock(&dev->t10_pr.registration_lock); 1897c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 1898c66ac9dbSNicholas Bellinger 1899c66ac9dbSNicholas Bellinger return ret; 1900c66ac9dbSNicholas Bellinger } 1901c66ac9dbSNicholas Bellinger 1902c66ac9dbSNicholas Bellinger static int __core_scsi3_write_aptpl_to_file( 1903c66ac9dbSNicholas Bellinger struct se_device *dev, 19044dee96fbSAndy Grover unsigned char *buf) 1905c66ac9dbSNicholas Bellinger { 19060fd97ccfSChristoph Hellwig struct t10_wwn *wwn = &dev->t10_wwn; 1907c66ac9dbSNicholas Bellinger struct file *file; 1908c66ac9dbSNicholas Bellinger int flags = O_RDWR | O_CREAT | O_TRUNC; 1909c66ac9dbSNicholas Bellinger char path[512]; 19104dee96fbSAndy Grover u32 pr_aptpl_buf_len; 1911c66ac9dbSNicholas Bellinger int ret; 1912c66ac9dbSNicholas Bellinger 1913c66ac9dbSNicholas Bellinger memset(path, 0, 512); 1914c66ac9dbSNicholas Bellinger 191560d645a4SDan Carpenter if (strlen(&wwn->unit_serial[0]) >= 512) { 19166708bb27SAndy Grover pr_err("WWN value for struct se_device does not fit" 1917c66ac9dbSNicholas Bellinger " into path buffer\n"); 1918e3d6f909SAndy Grover return -EMSGSIZE; 1919c66ac9dbSNicholas Bellinger } 1920c66ac9dbSNicholas Bellinger 1921c66ac9dbSNicholas Bellinger snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]); 1922c66ac9dbSNicholas Bellinger file = filp_open(path, flags, 0600); 19230e9b10a9SAl Viro if (IS_ERR(file)) { 19246708bb27SAndy Grover pr_err("filp_open(%s) for APTPL metadata" 1925c66ac9dbSNicholas Bellinger " failed\n", path); 19260e9b10a9SAl Viro return PTR_ERR(file); 1927c66ac9dbSNicholas Bellinger } 1928c66ac9dbSNicholas Bellinger 19294dee96fbSAndy Grover pr_aptpl_buf_len = (strlen(buf) + 1); /* Add extra for NULL */ 1930c66ac9dbSNicholas Bellinger 19310e9b10a9SAl Viro ret = kernel_write(file, buf, pr_aptpl_buf_len, 0); 1932c66ac9dbSNicholas Bellinger 19330e9b10a9SAl Viro if (ret < 0) 19346708bb27SAndy Grover pr_debug("Error writing APTPL metadata file: %s\n", path); 19350e9b10a9SAl Viro fput(file); 1936c66ac9dbSNicholas Bellinger 1937f730f915SGera Kazakov return (ret < 0) ? -EIO : 0; 1938c66ac9dbSNicholas Bellinger } 1939c66ac9dbSNicholas Bellinger 1940c66ac9dbSNicholas Bellinger /* 1941459f213bSAndy Grover * Clear the APTPL metadata if APTPL has been disabled, otherwise 1942459f213bSAndy Grover * write out the updated metadata to struct file for this SCSI device. 1943c66ac9dbSNicholas Bellinger */ 19448a391857SNicholas Bellinger static sense_reason_t core_scsi3_update_and_write_aptpl(struct se_device *dev, bool aptpl) 1945459f213bSAndy Grover { 19468a391857SNicholas Bellinger unsigned char *buf; 19478a391857SNicholas Bellinger int rc; 1948459f213bSAndy Grover 1949459f213bSAndy Grover if (!aptpl) { 1950459f213bSAndy Grover char *null_buf = "No Registrations or Reservations\n"; 1951459f213bSAndy Grover 19528a391857SNicholas Bellinger rc = __core_scsi3_write_aptpl_to_file(dev, null_buf); 1953459f213bSAndy Grover dev->t10_pr.pr_aptpl_active = 0; 1954459f213bSAndy Grover pr_debug("SPC-3 PR: Set APTPL Bit Deactivated\n"); 19558a391857SNicholas Bellinger 19568a391857SNicholas Bellinger if (rc) 19578a391857SNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 19588a391857SNicholas Bellinger 19598a391857SNicholas Bellinger return 0; 19608a391857SNicholas Bellinger } 1961459f213bSAndy Grover 1962459f213bSAndy Grover buf = kzalloc(PR_APTPL_BUF_LEN, GFP_KERNEL); 1963459f213bSAndy Grover if (!buf) 19648a391857SNicholas Bellinger return TCM_OUT_OF_RESOURCES; 1965459f213bSAndy Grover 19668a391857SNicholas Bellinger rc = core_scsi3_update_aptpl_buf(dev, buf, PR_APTPL_BUF_LEN); 19678a391857SNicholas Bellinger if (rc < 0) { 1968459f213bSAndy Grover kfree(buf); 19698a391857SNicholas Bellinger return TCM_OUT_OF_RESOURCES; 19700607decdSAndy Grover } 1971de103c93SChristoph Hellwig 19728a391857SNicholas Bellinger rc = __core_scsi3_write_aptpl_to_file(dev, buf); 19738a391857SNicholas Bellinger if (rc != 0) { 1974459f213bSAndy Grover pr_err("SPC-3 PR: Could not update APTPL\n"); 1975459f213bSAndy Grover kfree(buf); 19768a391857SNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1977459f213bSAndy Grover } 19788a391857SNicholas Bellinger dev->t10_pr.pr_aptpl_active = 1; 19798a391857SNicholas Bellinger kfree(buf); 19808a391857SNicholas Bellinger pr_debug("SPC-3 PR: Set APTPL Bit Activated\n"); 19818a391857SNicholas Bellinger return 0; 1982c66ac9dbSNicholas Bellinger } 1983c66ac9dbSNicholas Bellinger 1984de103c93SChristoph Hellwig static sense_reason_t 1985de103c93SChristoph Hellwig core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key, 1986bc118fe4SAndy Grover bool aptpl, bool all_tg_pt, bool spec_i_pt, enum register_type register_type) 1987c66ac9dbSNicholas Bellinger { 1988e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 19895951146dSAndy Grover struct se_device *dev = cmd->se_dev; 1990c66ac9dbSNicholas Bellinger struct se_dev_entry *se_deve; 1991e3d6f909SAndy Grover struct se_lun *se_lun = cmd->se_lun; 1992c66ac9dbSNicholas Bellinger struct se_portal_group *se_tpg; 1993bc118fe4SAndy Grover struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp; 19940fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 1995c66ac9dbSNicholas Bellinger unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL; 1996a0d50f62SHannes Reinecke sense_reason_t ret = TCM_NO_SENSE; 1997fc09149dSNicholas Bellinger int pr_holder = 0, type; 1998c66ac9dbSNicholas Bellinger 19996708bb27SAndy Grover if (!se_sess || !se_lun) { 20006708bb27SAndy Grover pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n"); 2001de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2002c66ac9dbSNicholas Bellinger } 2003c66ac9dbSNicholas Bellinger se_tpg = se_sess->se_tpg; 2004f2083241SJörn Engel se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun]; 2005c66ac9dbSNicholas Bellinger 2006e3d6f909SAndy Grover if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) { 2007c66ac9dbSNicholas Bellinger memset(&isid_buf[0], 0, PR_REG_ISID_LEN); 2008e3d6f909SAndy Grover se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0], 2009c66ac9dbSNicholas Bellinger PR_REG_ISID_LEN); 2010c66ac9dbSNicholas Bellinger isid_ptr = &isid_buf[0]; 2011c66ac9dbSNicholas Bellinger } 2012c66ac9dbSNicholas Bellinger /* 2013c66ac9dbSNicholas Bellinger * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47 2014c66ac9dbSNicholas Bellinger */ 2015bc118fe4SAndy Grover pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess); 2016bc118fe4SAndy Grover if (!pr_reg) { 2017c66ac9dbSNicholas Bellinger if (res_key) { 20186708bb27SAndy Grover pr_warn("SPC-3 PR: Reservation Key non-zero" 2019c66ac9dbSNicholas Bellinger " for SA REGISTER, returning CONFLICT\n"); 2020de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 2021c66ac9dbSNicholas Bellinger } 2022c66ac9dbSNicholas Bellinger /* 2023c66ac9dbSNicholas Bellinger * Do nothing but return GOOD status. 2024c66ac9dbSNicholas Bellinger */ 20256708bb27SAndy Grover if (!sa_res_key) 202603e98c9eSNicholas Bellinger return 0; 2027c66ac9dbSNicholas Bellinger 20286708bb27SAndy Grover if (!spec_i_pt) { 2029c66ac9dbSNicholas Bellinger /* 2030c66ac9dbSNicholas Bellinger * Perform the Service Action REGISTER on the Initiator 2031c66ac9dbSNicholas Bellinger * Port Endpoint that the PRO was received from on the 2032c66ac9dbSNicholas Bellinger * Logical Unit of the SCSI device server. 2033c66ac9dbSNicholas Bellinger */ 2034de103c93SChristoph Hellwig if (core_scsi3_alloc_registration(cmd->se_dev, 2035c66ac9dbSNicholas Bellinger se_sess->se_node_acl, se_deve, isid_ptr, 2036c66ac9dbSNicholas Bellinger sa_res_key, all_tg_pt, aptpl, 203733ce6a87SAndy Grover register_type, 0)) { 20386708bb27SAndy Grover pr_err("Unable to allocate" 2039c66ac9dbSNicholas Bellinger " struct t10_pr_registration\n"); 2040de103c93SChristoph Hellwig return TCM_INVALID_PARAMETER_LIST; 2041c66ac9dbSNicholas Bellinger } 2042c66ac9dbSNicholas Bellinger } else { 2043c66ac9dbSNicholas Bellinger /* 2044c66ac9dbSNicholas Bellinger * Register both the Initiator port that received 2045c66ac9dbSNicholas Bellinger * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI 2046c66ac9dbSNicholas Bellinger * TransportID from Parameter list and loop through 2047c66ac9dbSNicholas Bellinger * fabric dependent parameter list while calling 2048c66ac9dbSNicholas Bellinger * logic from of core_scsi3_alloc_registration() for 2049c66ac9dbSNicholas Bellinger * each TransportID provided SCSI Initiator Port/Device 2050c66ac9dbSNicholas Bellinger */ 2051c66ac9dbSNicholas Bellinger ret = core_scsi3_decode_spec_i_port(cmd, se_tpg, 2052c66ac9dbSNicholas Bellinger isid_ptr, sa_res_key, all_tg_pt, aptpl); 2053c66ac9dbSNicholas Bellinger if (ret != 0) 2054c66ac9dbSNicholas Bellinger return ret; 2055c66ac9dbSNicholas Bellinger } 2056c66ac9dbSNicholas Bellinger 2057459f213bSAndy Grover return core_scsi3_update_and_write_aptpl(dev, aptpl); 2058de103c93SChristoph Hellwig } 2059de103c93SChristoph Hellwig 2060bc118fe4SAndy Grover /* ok, existing registration */ 2061c66ac9dbSNicholas Bellinger 2062bc118fe4SAndy Grover if ((register_type == REGISTER) && (res_key != pr_reg->pr_res_key)) { 20636708bb27SAndy Grover pr_err("SPC-3 PR REGISTER: Received" 2064c66ac9dbSNicholas Bellinger " res_key: 0x%016Lx does not match" 2065c66ac9dbSNicholas Bellinger " existing SA REGISTER res_key:" 2066c66ac9dbSNicholas Bellinger " 0x%016Lx\n", res_key, 2067c66ac9dbSNicholas Bellinger pr_reg->pr_res_key); 2068de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 2069bc118fe4SAndy Grover goto out; 2070c66ac9dbSNicholas Bellinger } 2071de103c93SChristoph Hellwig 2072c66ac9dbSNicholas Bellinger if (spec_i_pt) { 20731f070cc2SAndy Grover pr_err("SPC-3 PR REGISTER: SPEC_I_PT" 20741f070cc2SAndy Grover " set on a registered nexus\n"); 2075de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 2076bc118fe4SAndy Grover goto out; 2077c66ac9dbSNicholas Bellinger } 2078de103c93SChristoph Hellwig 2079c66ac9dbSNicholas Bellinger /* 2080c66ac9dbSNicholas Bellinger * An existing ALL_TG_PT=1 registration being released 2081c66ac9dbSNicholas Bellinger * must also set ALL_TG_PT=1 in the incoming PROUT. 2082c66ac9dbSNicholas Bellinger */ 20831f070cc2SAndy Grover if (pr_reg->pr_reg_all_tg_pt && !all_tg_pt) { 20841f070cc2SAndy Grover pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1" 2085c66ac9dbSNicholas Bellinger " registration exists, but ALL_TG_PT=1 bit not" 2086c66ac9dbSNicholas Bellinger " present in received PROUT\n"); 2087de103c93SChristoph Hellwig ret = TCM_INVALID_CDB_FIELD; 2088bc118fe4SAndy Grover goto out; 2089c66ac9dbSNicholas Bellinger } 2090de103c93SChristoph Hellwig 2091c66ac9dbSNicholas Bellinger /* 209251d9c41dSAndy Grover * sa_res_key=1 Change Reservation Key for registered I_T Nexus. 2093c66ac9dbSNicholas Bellinger */ 209451d9c41dSAndy Grover if (sa_res_key) { 209551d9c41dSAndy Grover /* 209651d9c41dSAndy Grover * Increment PRgeneration counter for struct se_device" 209751d9c41dSAndy Grover * upon a successful REGISTER, see spc4r17 section 6.3.2 209851d9c41dSAndy Grover * READ_KEYS service action. 209951d9c41dSAndy Grover */ 210051d9c41dSAndy Grover pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev); 210151d9c41dSAndy Grover pr_reg->pr_res_key = sa_res_key; 210251d9c41dSAndy Grover pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation" 210351d9c41dSAndy Grover " Key for %s to: 0x%016Lx PRgeneration:" 210451d9c41dSAndy Grover " 0x%08x\n", cmd->se_tfo->get_fabric_name(), 210551d9c41dSAndy Grover (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? "_AND_IGNORE_EXISTING_KEY" : "", 210651d9c41dSAndy Grover pr_reg->pr_reg_nacl->initiatorname, 210751d9c41dSAndy Grover pr_reg->pr_res_key, pr_reg->pr_res_generation); 210851d9c41dSAndy Grover 210951d9c41dSAndy Grover } else { 211051d9c41dSAndy Grover /* 211151d9c41dSAndy Grover * sa_res_key=0 Unregister Reservation Key for registered I_T Nexus. 211251d9c41dSAndy Grover */ 21136c3c9baaSNicholas Bellinger type = pr_reg->pr_res_type; 21146c3c9baaSNicholas Bellinger pr_holder = core_scsi3_check_implicit_release(cmd->se_dev, 21156c3c9baaSNicholas Bellinger pr_reg); 2116c66ac9dbSNicholas Bellinger if (pr_holder < 0) { 2117de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 2118bc118fe4SAndy Grover goto out; 2119c66ac9dbSNicholas Bellinger } 2120c66ac9dbSNicholas Bellinger 2121c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 2122c66ac9dbSNicholas Bellinger /* 2123c66ac9dbSNicholas Bellinger * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port 2124c66ac9dbSNicholas Bellinger * and matching pr_res_key. 2125c66ac9dbSNicholas Bellinger */ 2126c66ac9dbSNicholas Bellinger if (pr_reg->pr_reg_all_tg_pt) { 2127c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg_p, pr_reg_tmp, 2128c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, 2129c66ac9dbSNicholas Bellinger pr_reg_list) { 2130c66ac9dbSNicholas Bellinger 21316708bb27SAndy Grover if (!pr_reg_p->pr_reg_all_tg_pt) 2132c66ac9dbSNicholas Bellinger continue; 2133c66ac9dbSNicholas Bellinger if (pr_reg_p->pr_res_key != res_key) 2134c66ac9dbSNicholas Bellinger continue; 2135c66ac9dbSNicholas Bellinger if (pr_reg == pr_reg_p) 2136c66ac9dbSNicholas Bellinger continue; 2137c66ac9dbSNicholas Bellinger if (strcmp(pr_reg->pr_reg_nacl->initiatorname, 2138c66ac9dbSNicholas Bellinger pr_reg_p->pr_reg_nacl->initiatorname)) 2139c66ac9dbSNicholas Bellinger continue; 2140c66ac9dbSNicholas Bellinger 2141c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, 2142c66ac9dbSNicholas Bellinger pr_reg_p, NULL, 0); 2143c66ac9dbSNicholas Bellinger } 2144c66ac9dbSNicholas Bellinger } 2145de103c93SChristoph Hellwig 2146c66ac9dbSNicholas Bellinger /* 2147c66ac9dbSNicholas Bellinger * Release the calling I_T Nexus registration now.. 2148c66ac9dbSNicholas Bellinger */ 2149de103c93SChristoph Hellwig __core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1); 2150fc09149dSNicholas Bellinger pr_reg = NULL; 2151de103c93SChristoph Hellwig 2152c66ac9dbSNicholas Bellinger /* 2153c66ac9dbSNicholas Bellinger * From spc4r17, section 5.7.11.3 Unregistering 2154c66ac9dbSNicholas Bellinger * 2155c66ac9dbSNicholas Bellinger * If the persistent reservation is a registrants only 2156c66ac9dbSNicholas Bellinger * type, the device server shall establish a unit 2157c66ac9dbSNicholas Bellinger * attention condition for the initiator port associated 2158c66ac9dbSNicholas Bellinger * with every registered I_T nexus except for the I_T 2159c66ac9dbSNicholas Bellinger * nexus on which the PERSISTENT RESERVE OUT command was 2160c66ac9dbSNicholas Bellinger * received, with the additional sense code set to 2161c66ac9dbSNicholas Bellinger * RESERVATIONS RELEASED. 2162c66ac9dbSNicholas Bellinger */ 2163c66ac9dbSNicholas Bellinger if (pr_holder && 2164fc09149dSNicholas Bellinger (type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY || 2165fc09149dSNicholas Bellinger type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) { 2166c66ac9dbSNicholas Bellinger list_for_each_entry(pr_reg_p, 2167c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, 2168c66ac9dbSNicholas Bellinger pr_reg_list) { 2169c66ac9dbSNicholas Bellinger 2170c66ac9dbSNicholas Bellinger core_scsi3_ua_allocate( 2171c66ac9dbSNicholas Bellinger pr_reg_p->pr_reg_nacl, 2172c66ac9dbSNicholas Bellinger pr_reg_p->pr_res_mapped_lun, 2173c66ac9dbSNicholas Bellinger 0x2A, 2174c66ac9dbSNicholas Bellinger ASCQ_2AH_RESERVATIONS_RELEASED); 2175c66ac9dbSNicholas Bellinger } 2176c66ac9dbSNicholas Bellinger } 217751d9c41dSAndy Grover 2178c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 2179de103c93SChristoph Hellwig } 2180de103c93SChristoph Hellwig 2181459f213bSAndy Grover ret = core_scsi3_update_and_write_aptpl(dev, aptpl); 2182c66ac9dbSNicholas Bellinger 2183bc118fe4SAndy Grover out: 2184fc09149dSNicholas Bellinger if (pr_reg) 2185c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg); 2186de103c93SChristoph Hellwig return ret; 2187c66ac9dbSNicholas Bellinger } 2188c66ac9dbSNicholas Bellinger 2189c66ac9dbSNicholas Bellinger unsigned char *core_scsi3_pr_dump_type(int type) 2190c66ac9dbSNicholas Bellinger { 2191c66ac9dbSNicholas Bellinger switch (type) { 2192c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE: 2193c66ac9dbSNicholas Bellinger return "Write Exclusive Access"; 2194c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS: 2195c66ac9dbSNicholas Bellinger return "Exclusive Access"; 2196c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_REGONLY: 2197c66ac9dbSNicholas Bellinger return "Write Exclusive Access, Registrants Only"; 2198c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY: 2199c66ac9dbSNicholas Bellinger return "Exclusive Access, Registrants Only"; 2200c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_ALLREG: 2201c66ac9dbSNicholas Bellinger return "Write Exclusive Access, All Registrants"; 2202c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG: 2203c66ac9dbSNicholas Bellinger return "Exclusive Access, All Registrants"; 2204c66ac9dbSNicholas Bellinger default: 2205c66ac9dbSNicholas Bellinger break; 2206c66ac9dbSNicholas Bellinger } 2207c66ac9dbSNicholas Bellinger 2208c66ac9dbSNicholas Bellinger return "Unknown SPC-3 PR Type"; 2209c66ac9dbSNicholas Bellinger } 2210c66ac9dbSNicholas Bellinger 2211de103c93SChristoph Hellwig static sense_reason_t 2212de103c93SChristoph Hellwig core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key) 2213c66ac9dbSNicholas Bellinger { 2214de103c93SChristoph Hellwig struct se_device *dev = cmd->se_dev; 2215e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 2216e3d6f909SAndy Grover struct se_lun *se_lun = cmd->se_lun; 2217c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_res_holder; 22180fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 2219c66ac9dbSNicholas Bellinger char i_buf[PR_REG_ISID_ID_LEN]; 2220de103c93SChristoph Hellwig sense_reason_t ret; 2221c66ac9dbSNicholas Bellinger 2222c66ac9dbSNicholas Bellinger memset(i_buf, 0, PR_REG_ISID_ID_LEN); 2223c66ac9dbSNicholas Bellinger 22246708bb27SAndy Grover if (!se_sess || !se_lun) { 22256708bb27SAndy Grover pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n"); 2226de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2227c66ac9dbSNicholas Bellinger } 2228c66ac9dbSNicholas Bellinger /* 2229c66ac9dbSNicholas Bellinger * Locate the existing *pr_reg via struct se_node_acl pointers 2230c66ac9dbSNicholas Bellinger */ 22315951146dSAndy Grover pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl, 2232c66ac9dbSNicholas Bellinger se_sess); 22336708bb27SAndy Grover if (!pr_reg) { 22346708bb27SAndy Grover pr_err("SPC-3 PR: Unable to locate" 2235c66ac9dbSNicholas Bellinger " PR_REGISTERED *pr_reg for RESERVE\n"); 2236de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2237c66ac9dbSNicholas Bellinger } 2238c66ac9dbSNicholas Bellinger /* 2239c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.9: Reserving: 2240c66ac9dbSNicholas Bellinger * 2241c66ac9dbSNicholas Bellinger * An application client creates a persistent reservation by issuing 2242c66ac9dbSNicholas Bellinger * a PERSISTENT RESERVE OUT command with RESERVE service action through 2243c66ac9dbSNicholas Bellinger * a registered I_T nexus with the following parameters: 2244c66ac9dbSNicholas Bellinger * a) RESERVATION KEY set to the value of the reservation key that is 2245c66ac9dbSNicholas Bellinger * registered with the logical unit for the I_T nexus; and 2246c66ac9dbSNicholas Bellinger */ 2247c66ac9dbSNicholas Bellinger if (res_key != pr_reg->pr_res_key) { 22486708bb27SAndy Grover pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx" 2249c66ac9dbSNicholas Bellinger " does not match existing SA REGISTER res_key:" 2250c66ac9dbSNicholas Bellinger " 0x%016Lx\n", res_key, pr_reg->pr_res_key); 2251de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 2252de103c93SChristoph Hellwig goto out_put_pr_reg; 2253c66ac9dbSNicholas Bellinger } 2254c66ac9dbSNicholas Bellinger /* 2255c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.9: Reserving: 2256c66ac9dbSNicholas Bellinger * 2257c66ac9dbSNicholas Bellinger * From above: 2258c66ac9dbSNicholas Bellinger * b) TYPE field and SCOPE field set to the persistent reservation 2259c66ac9dbSNicholas Bellinger * being created. 2260c66ac9dbSNicholas Bellinger * 2261c66ac9dbSNicholas Bellinger * Only one persistent reservation is allowed at a time per logical unit 2262c66ac9dbSNicholas Bellinger * and that persistent reservation has a scope of LU_SCOPE. 2263c66ac9dbSNicholas Bellinger */ 2264c66ac9dbSNicholas Bellinger if (scope != PR_SCOPE_LU_SCOPE) { 22656708bb27SAndy Grover pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope); 2266de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 2267de103c93SChristoph Hellwig goto out_put_pr_reg; 2268c66ac9dbSNicholas Bellinger } 2269c66ac9dbSNicholas Bellinger /* 2270c66ac9dbSNicholas Bellinger * See if we have an existing PR reservation holder pointer at 2271c66ac9dbSNicholas Bellinger * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration 2272c66ac9dbSNicholas Bellinger * *pr_res_holder. 2273c66ac9dbSNicholas Bellinger */ 2274c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 2275c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 2276ee1b1b9cSAndy Grover if (pr_res_holder) { 2277c66ac9dbSNicholas Bellinger /* 2278c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.9: Reserving: 2279c66ac9dbSNicholas Bellinger * 2280c66ac9dbSNicholas Bellinger * If the device server receives a PERSISTENT RESERVE OUT 2281c66ac9dbSNicholas Bellinger * command from an I_T nexus other than a persistent reservation 2282c66ac9dbSNicholas Bellinger * holder (see 5.7.10) that attempts to create a persistent 2283c66ac9dbSNicholas Bellinger * reservation when a persistent reservation already exists for 2284c66ac9dbSNicholas Bellinger * the logical unit, then the command shall be completed with 2285c66ac9dbSNicholas Bellinger * RESERVATION CONFLICT status. 2286c66ac9dbSNicholas Bellinger */ 2287c66ac9dbSNicholas Bellinger if (pr_res_holder != pr_reg) { 2288c66ac9dbSNicholas Bellinger struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; 22896708bb27SAndy Grover pr_err("SPC-3 PR: Attempted RESERVE from" 2290c66ac9dbSNicholas Bellinger " [%s]: %s while reservation already held by" 2291c66ac9dbSNicholas Bellinger " [%s]: %s, returning RESERVATION_CONFLICT\n", 2292e3d6f909SAndy Grover cmd->se_tfo->get_fabric_name(), 2293c66ac9dbSNicholas Bellinger se_sess->se_node_acl->initiatorname, 2294e3d6f909SAndy Grover pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(), 2295c66ac9dbSNicholas Bellinger pr_res_holder->pr_reg_nacl->initiatorname); 2296c66ac9dbSNicholas Bellinger 2297c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2298de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 2299de103c93SChristoph Hellwig goto out_put_pr_reg; 2300c66ac9dbSNicholas Bellinger } 2301c66ac9dbSNicholas Bellinger /* 2302c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.9: Reserving: 2303c66ac9dbSNicholas Bellinger * 2304c66ac9dbSNicholas Bellinger * If a persistent reservation holder attempts to modify the 2305c66ac9dbSNicholas Bellinger * type or scope of an existing persistent reservation, the 2306c66ac9dbSNicholas Bellinger * command shall be completed with RESERVATION CONFLICT status. 2307c66ac9dbSNicholas Bellinger */ 2308c66ac9dbSNicholas Bellinger if ((pr_res_holder->pr_res_type != type) || 2309c66ac9dbSNicholas Bellinger (pr_res_holder->pr_res_scope != scope)) { 2310c66ac9dbSNicholas Bellinger struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; 23116708bb27SAndy Grover pr_err("SPC-3 PR: Attempted RESERVE from" 2312c66ac9dbSNicholas Bellinger " [%s]: %s trying to change TYPE and/or SCOPE," 2313c66ac9dbSNicholas Bellinger " while reservation already held by [%s]: %s," 2314c66ac9dbSNicholas Bellinger " returning RESERVATION_CONFLICT\n", 2315e3d6f909SAndy Grover cmd->se_tfo->get_fabric_name(), 2316c66ac9dbSNicholas Bellinger se_sess->se_node_acl->initiatorname, 2317e3d6f909SAndy Grover pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(), 2318c66ac9dbSNicholas Bellinger pr_res_holder->pr_reg_nacl->initiatorname); 2319c66ac9dbSNicholas Bellinger 2320c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2321de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 2322de103c93SChristoph Hellwig goto out_put_pr_reg; 2323c66ac9dbSNicholas Bellinger } 2324c66ac9dbSNicholas Bellinger /* 2325c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.9: Reserving: 2326c66ac9dbSNicholas Bellinger * 2327c66ac9dbSNicholas Bellinger * If the device server receives a PERSISTENT RESERVE OUT 2328c66ac9dbSNicholas Bellinger * command with RESERVE service action where the TYPE field and 2329c66ac9dbSNicholas Bellinger * the SCOPE field contain the same values as the existing type 2330c66ac9dbSNicholas Bellinger * and scope from a persistent reservation holder, it shall not 2331c66ac9dbSNicholas Bellinger * make any change to the existing persistent reservation and 2332c66ac9dbSNicholas Bellinger * shall completethe command with GOOD status. 2333c66ac9dbSNicholas Bellinger */ 2334c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2335de103c93SChristoph Hellwig ret = 0; 2336de103c93SChristoph Hellwig goto out_put_pr_reg; 2337c66ac9dbSNicholas Bellinger } 2338c66ac9dbSNicholas Bellinger /* 2339c66ac9dbSNicholas Bellinger * Otherwise, our *pr_reg becomes the PR reservation holder for said 2340c66ac9dbSNicholas Bellinger * TYPE/SCOPE. Also set the received scope and type in *pr_reg. 2341c66ac9dbSNicholas Bellinger */ 2342c66ac9dbSNicholas Bellinger pr_reg->pr_res_scope = scope; 2343c66ac9dbSNicholas Bellinger pr_reg->pr_res_type = type; 2344c66ac9dbSNicholas Bellinger pr_reg->pr_res_holder = 1; 2345c66ac9dbSNicholas Bellinger dev->dev_pr_res_holder = pr_reg; 2346d2843c17SAndy Grover core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); 2347c66ac9dbSNicholas Bellinger 23486708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new" 2349c66ac9dbSNicholas Bellinger " reservation holder TYPE: %s ALL_TG_PT: %d\n", 2350e3d6f909SAndy Grover cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type), 2351c66ac9dbSNicholas Bellinger (pr_reg->pr_reg_all_tg_pt) ? 1 : 0); 23526708bb27SAndy Grover pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n", 2353e3d6f909SAndy Grover cmd->se_tfo->get_fabric_name(), 2354c66ac9dbSNicholas Bellinger se_sess->se_node_acl->initiatorname, 2355d2843c17SAndy Grover i_buf); 2356c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2357c66ac9dbSNicholas Bellinger 2358459f213bSAndy Grover if (pr_tmpl->pr_aptpl_active) 2359459f213bSAndy Grover core_scsi3_update_and_write_aptpl(cmd->se_dev, true); 2360c66ac9dbSNicholas Bellinger 2361de103c93SChristoph Hellwig ret = 0; 2362de103c93SChristoph Hellwig out_put_pr_reg: 2363de103c93SChristoph Hellwig core_scsi3_put_pr_reg(pr_reg); 2364de103c93SChristoph Hellwig return ret; 2365de103c93SChristoph Hellwig } 2366de103c93SChristoph Hellwig 2367de103c93SChristoph Hellwig static sense_reason_t 2368de103c93SChristoph Hellwig core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope, 2369c66ac9dbSNicholas Bellinger u64 res_key) 2370c66ac9dbSNicholas Bellinger { 2371c66ac9dbSNicholas Bellinger switch (type) { 2372c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE: 2373c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS: 2374c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_REGONLY: 2375c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY: 2376c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_ALLREG: 2377c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG: 2378de103c93SChristoph Hellwig return core_scsi3_pro_reserve(cmd, type, scope, res_key); 2379c66ac9dbSNicholas Bellinger default: 23806708bb27SAndy Grover pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:" 2381c66ac9dbSNicholas Bellinger " 0x%02x\n", type); 2382de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 2383c66ac9dbSNicholas Bellinger } 2384c66ac9dbSNicholas Bellinger } 2385c66ac9dbSNicholas Bellinger 2386c66ac9dbSNicholas Bellinger /* 2387c66ac9dbSNicholas Bellinger * Called with struct se_device->dev_reservation_lock held. 2388c66ac9dbSNicholas Bellinger */ 2389c66ac9dbSNicholas Bellinger static void __core_scsi3_complete_pro_release( 2390c66ac9dbSNicholas Bellinger struct se_device *dev, 2391c66ac9dbSNicholas Bellinger struct se_node_acl *se_nacl, 2392c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, 23936c3c9baaSNicholas Bellinger int explicit, 23946c3c9baaSNicholas Bellinger int unreg) 2395c66ac9dbSNicholas Bellinger { 2396c66ac9dbSNicholas Bellinger struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo; 2397c66ac9dbSNicholas Bellinger char i_buf[PR_REG_ISID_ID_LEN]; 23986c3c9baaSNicholas Bellinger int pr_res_type = 0, pr_res_scope = 0; 2399c66ac9dbSNicholas Bellinger 2400c66ac9dbSNicholas Bellinger memset(i_buf, 0, PR_REG_ISID_ID_LEN); 2401d2843c17SAndy Grover core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); 2402c66ac9dbSNicholas Bellinger /* 2403c66ac9dbSNicholas Bellinger * Go ahead and release the current PR reservation holder. 24046c3c9baaSNicholas Bellinger * If an All Registrants reservation is currently active and 24056c3c9baaSNicholas Bellinger * a unregister operation is requested, replace the current 24066c3c9baaSNicholas Bellinger * dev_pr_res_holder with another active registration. 2407c66ac9dbSNicholas Bellinger */ 24086c3c9baaSNicholas Bellinger if (dev->dev_pr_res_holder) { 24096c3c9baaSNicholas Bellinger pr_res_type = dev->dev_pr_res_holder->pr_res_type; 24106c3c9baaSNicholas Bellinger pr_res_scope = dev->dev_pr_res_holder->pr_res_scope; 24116c3c9baaSNicholas Bellinger dev->dev_pr_res_holder->pr_res_type = 0; 24126c3c9baaSNicholas Bellinger dev->dev_pr_res_holder->pr_res_scope = 0; 24136c3c9baaSNicholas Bellinger dev->dev_pr_res_holder->pr_res_holder = 0; 2414c66ac9dbSNicholas Bellinger dev->dev_pr_res_holder = NULL; 24156c3c9baaSNicholas Bellinger } 24166c3c9baaSNicholas Bellinger if (!unreg) 24176c3c9baaSNicholas Bellinger goto out; 2418c66ac9dbSNicholas Bellinger 24196c3c9baaSNicholas Bellinger spin_lock(&dev->t10_pr.registration_lock); 24206c3c9baaSNicholas Bellinger list_del_init(&pr_reg->pr_reg_list); 24216c3c9baaSNicholas Bellinger /* 24226c3c9baaSNicholas Bellinger * If the I_T nexus is a reservation holder, the persistent reservation 24236c3c9baaSNicholas Bellinger * is of an all registrants type, and the I_T nexus is the last remaining 24246c3c9baaSNicholas Bellinger * registered I_T nexus, then the device server shall also release the 24256c3c9baaSNicholas Bellinger * persistent reservation. 24266c3c9baaSNicholas Bellinger */ 24276c3c9baaSNicholas Bellinger if (!list_empty(&dev->t10_pr.registration_list) && 24286c3c9baaSNicholas Bellinger ((pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || 24296c3c9baaSNicholas Bellinger (pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) { 24306c3c9baaSNicholas Bellinger dev->dev_pr_res_holder = 24316c3c9baaSNicholas Bellinger list_entry(dev->t10_pr.registration_list.next, 24326c3c9baaSNicholas Bellinger struct t10_pr_registration, pr_reg_list); 24336c3c9baaSNicholas Bellinger dev->dev_pr_res_holder->pr_res_type = pr_res_type; 24346c3c9baaSNicholas Bellinger dev->dev_pr_res_holder->pr_res_scope = pr_res_scope; 24356c3c9baaSNicholas Bellinger dev->dev_pr_res_holder->pr_res_holder = 1; 24366c3c9baaSNicholas Bellinger } 24376c3c9baaSNicholas Bellinger spin_unlock(&dev->t10_pr.registration_lock); 24386c3c9baaSNicholas Bellinger out: 24396c3c9baaSNicholas Bellinger if (!dev->dev_pr_res_holder) { 24406708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared" 2441c66ac9dbSNicholas Bellinger " reservation holder TYPE: %s ALL_TG_PT: %d\n", 24426c3c9baaSNicholas Bellinger tfo->get_fabric_name(), (explicit) ? "explicit" : 24436c3c9baaSNicholas Bellinger "implicit", core_scsi3_pr_dump_type(pr_res_type), 2444c66ac9dbSNicholas Bellinger (pr_reg->pr_reg_all_tg_pt) ? 1 : 0); 24456c3c9baaSNicholas Bellinger } 24466708bb27SAndy Grover pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n", 2447c66ac9dbSNicholas Bellinger tfo->get_fabric_name(), se_nacl->initiatorname, 2448d2843c17SAndy Grover i_buf); 2449c66ac9dbSNicholas Bellinger /* 2450c66ac9dbSNicholas Bellinger * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE 2451c66ac9dbSNicholas Bellinger */ 2452c66ac9dbSNicholas Bellinger pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0; 2453c66ac9dbSNicholas Bellinger } 2454c66ac9dbSNicholas Bellinger 2455de103c93SChristoph Hellwig static sense_reason_t 2456de103c93SChristoph Hellwig core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope, 2457c66ac9dbSNicholas Bellinger u64 res_key) 2458c66ac9dbSNicholas Bellinger { 2459c66ac9dbSNicholas Bellinger struct se_device *dev = cmd->se_dev; 2460e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 2461e3d6f909SAndy Grover struct se_lun *se_lun = cmd->se_lun; 2462c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder; 24630fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 2464de103c93SChristoph Hellwig int all_reg = 0; 2465de103c93SChristoph Hellwig sense_reason_t ret = 0; 2466c66ac9dbSNicholas Bellinger 24676708bb27SAndy Grover if (!se_sess || !se_lun) { 24686708bb27SAndy Grover pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n"); 2469de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2470c66ac9dbSNicholas Bellinger } 2471c66ac9dbSNicholas Bellinger /* 2472c66ac9dbSNicholas Bellinger * Locate the existing *pr_reg via struct se_node_acl pointers 2473c66ac9dbSNicholas Bellinger */ 2474c66ac9dbSNicholas Bellinger pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess); 24756708bb27SAndy Grover if (!pr_reg) { 24766708bb27SAndy Grover pr_err("SPC-3 PR: Unable to locate" 2477c66ac9dbSNicholas Bellinger " PR_REGISTERED *pr_reg for RELEASE\n"); 2478de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2479c66ac9dbSNicholas Bellinger } 2480c66ac9dbSNicholas Bellinger /* 2481c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.11.2 Releasing: 2482c66ac9dbSNicholas Bellinger * 2483c66ac9dbSNicholas Bellinger * If there is no persistent reservation or in response to a persistent 2484c66ac9dbSNicholas Bellinger * reservation release request from a registered I_T nexus that is not a 2485c66ac9dbSNicholas Bellinger * persistent reservation holder (see 5.7.10), the device server shall 2486c66ac9dbSNicholas Bellinger * do the following: 2487c66ac9dbSNicholas Bellinger * 2488c66ac9dbSNicholas Bellinger * a) Not release the persistent reservation, if any; 2489c66ac9dbSNicholas Bellinger * b) Not remove any registrations; and 2490c66ac9dbSNicholas Bellinger * c) Complete the command with GOOD status. 2491c66ac9dbSNicholas Bellinger */ 2492c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 2493c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 24946708bb27SAndy Grover if (!pr_res_holder) { 2495c66ac9dbSNicholas Bellinger /* 2496c66ac9dbSNicholas Bellinger * No persistent reservation, return GOOD status. 2497c66ac9dbSNicholas Bellinger */ 2498c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2499bb7a8c8eSNicholas Bellinger goto out_put_pr_reg; 2500c66ac9dbSNicholas Bellinger } 2501c66ac9dbSNicholas Bellinger if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || 2502c66ac9dbSNicholas Bellinger (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) 2503c66ac9dbSNicholas Bellinger all_reg = 1; 2504c66ac9dbSNicholas Bellinger 2505c66ac9dbSNicholas Bellinger if ((all_reg == 0) && (pr_res_holder != pr_reg)) { 2506c66ac9dbSNicholas Bellinger /* 2507c66ac9dbSNicholas Bellinger * Non 'All Registrants' PR Type cases.. 2508c66ac9dbSNicholas Bellinger * Release request from a registered I_T nexus that is not a 2509c66ac9dbSNicholas Bellinger * persistent reservation holder. return GOOD status. 2510c66ac9dbSNicholas Bellinger */ 2511c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2512de103c93SChristoph Hellwig goto out_put_pr_reg; 2513c66ac9dbSNicholas Bellinger } 2514de103c93SChristoph Hellwig 2515c66ac9dbSNicholas Bellinger /* 2516c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.11.2 Releasing: 2517c66ac9dbSNicholas Bellinger * 2518c66ac9dbSNicholas Bellinger * Only the persistent reservation holder (see 5.7.10) is allowed to 2519c66ac9dbSNicholas Bellinger * release a persistent reservation. 2520c66ac9dbSNicholas Bellinger * 2521c66ac9dbSNicholas Bellinger * An application client releases the persistent reservation by issuing 2522c66ac9dbSNicholas Bellinger * a PERSISTENT RESERVE OUT command with RELEASE service action through 2523c66ac9dbSNicholas Bellinger * an I_T nexus that is a persistent reservation holder with the 2524c66ac9dbSNicholas Bellinger * following parameters: 2525c66ac9dbSNicholas Bellinger * 2526c66ac9dbSNicholas Bellinger * a) RESERVATION KEY field set to the value of the reservation key 2527c66ac9dbSNicholas Bellinger * that is registered with the logical unit for the I_T nexus; 2528c66ac9dbSNicholas Bellinger */ 2529c66ac9dbSNicholas Bellinger if (res_key != pr_reg->pr_res_key) { 25306708bb27SAndy Grover pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx" 2531c66ac9dbSNicholas Bellinger " does not match existing SA REGISTER res_key:" 2532c66ac9dbSNicholas Bellinger " 0x%016Lx\n", res_key, pr_reg->pr_res_key); 2533c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2534de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 2535de103c93SChristoph Hellwig goto out_put_pr_reg; 2536c66ac9dbSNicholas Bellinger } 2537c66ac9dbSNicholas Bellinger /* 2538c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.11.2 Releasing and above: 2539c66ac9dbSNicholas Bellinger * 2540c66ac9dbSNicholas Bellinger * b) TYPE field and SCOPE field set to match the persistent 2541c66ac9dbSNicholas Bellinger * reservation being released. 2542c66ac9dbSNicholas Bellinger */ 2543c66ac9dbSNicholas Bellinger if ((pr_res_holder->pr_res_type != type) || 2544c66ac9dbSNicholas Bellinger (pr_res_holder->pr_res_scope != scope)) { 2545c66ac9dbSNicholas Bellinger struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; 25466708bb27SAndy Grover pr_err("SPC-3 PR RELEASE: Attempted to release" 2547c66ac9dbSNicholas Bellinger " reservation from [%s]: %s with different TYPE " 2548c66ac9dbSNicholas Bellinger "and/or SCOPE while reservation already held by" 2549c66ac9dbSNicholas Bellinger " [%s]: %s, returning RESERVATION_CONFLICT\n", 2550e3d6f909SAndy Grover cmd->se_tfo->get_fabric_name(), 2551c66ac9dbSNicholas Bellinger se_sess->se_node_acl->initiatorname, 2552e3d6f909SAndy Grover pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(), 2553c66ac9dbSNicholas Bellinger pr_res_holder->pr_reg_nacl->initiatorname); 2554c66ac9dbSNicholas Bellinger 2555c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2556de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 2557de103c93SChristoph Hellwig goto out_put_pr_reg; 2558c66ac9dbSNicholas Bellinger } 2559c66ac9dbSNicholas Bellinger /* 2560c66ac9dbSNicholas Bellinger * In response to a persistent reservation release request from the 2561c66ac9dbSNicholas Bellinger * persistent reservation holder the device server shall perform a 2562c66ac9dbSNicholas Bellinger * release by doing the following as an uninterrupted series of actions: 2563c66ac9dbSNicholas Bellinger * a) Release the persistent reservation; 2564c66ac9dbSNicholas Bellinger * b) Not remove any registration(s); 2565c66ac9dbSNicholas Bellinger * c) If the released persistent reservation is a registrants only type 2566c66ac9dbSNicholas Bellinger * or all registrants type persistent reservation, 2567c66ac9dbSNicholas Bellinger * the device server shall establish a unit attention condition for 2568c66ac9dbSNicholas Bellinger * the initiator port associated with every regis- 2569c66ac9dbSNicholas Bellinger * tered I_T nexus other than I_T nexus on which the PERSISTENT 2570c66ac9dbSNicholas Bellinger * RESERVE OUT command with RELEASE service action was received, 2571c66ac9dbSNicholas Bellinger * with the additional sense code set to RESERVATIONS RELEASED; and 2572c66ac9dbSNicholas Bellinger * d) If the persistent reservation is of any other type, the device 2573c66ac9dbSNicholas Bellinger * server shall not establish a unit attention condition. 2574c66ac9dbSNicholas Bellinger */ 2575c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl, 25766c3c9baaSNicholas Bellinger pr_reg, 1, 0); 2577c66ac9dbSNicholas Bellinger 2578c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2579c66ac9dbSNicholas Bellinger 2580c66ac9dbSNicholas Bellinger if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) && 2581c66ac9dbSNicholas Bellinger (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) && 2582c66ac9dbSNicholas Bellinger (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) && 2583c66ac9dbSNicholas Bellinger (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) { 2584c66ac9dbSNicholas Bellinger /* 2585c66ac9dbSNicholas Bellinger * If no UNIT ATTENTION conditions will be established for 2586c66ac9dbSNicholas Bellinger * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS 2587c66ac9dbSNicholas Bellinger * go ahead and check for APTPL=1 update+write below 2588c66ac9dbSNicholas Bellinger */ 2589c66ac9dbSNicholas Bellinger goto write_aptpl; 2590c66ac9dbSNicholas Bellinger } 2591c66ac9dbSNicholas Bellinger 2592c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 2593c66ac9dbSNicholas Bellinger list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list, 2594c66ac9dbSNicholas Bellinger pr_reg_list) { 2595c66ac9dbSNicholas Bellinger /* 2596c66ac9dbSNicholas Bellinger * Do not establish a UNIT ATTENTION condition 2597c66ac9dbSNicholas Bellinger * for the calling I_T Nexus 2598c66ac9dbSNicholas Bellinger */ 2599c66ac9dbSNicholas Bellinger if (pr_reg_p == pr_reg) 2600c66ac9dbSNicholas Bellinger continue; 2601c66ac9dbSNicholas Bellinger 2602c66ac9dbSNicholas Bellinger core_scsi3_ua_allocate(pr_reg_p->pr_reg_nacl, 2603c66ac9dbSNicholas Bellinger pr_reg_p->pr_res_mapped_lun, 2604c66ac9dbSNicholas Bellinger 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED); 2605c66ac9dbSNicholas Bellinger } 2606c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 2607c66ac9dbSNicholas Bellinger 2608c66ac9dbSNicholas Bellinger write_aptpl: 2609459f213bSAndy Grover if (pr_tmpl->pr_aptpl_active) 2610459f213bSAndy Grover core_scsi3_update_and_write_aptpl(cmd->se_dev, true); 2611459f213bSAndy Grover 2612de103c93SChristoph Hellwig out_put_pr_reg: 2613c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg); 2614de103c93SChristoph Hellwig return ret; 2615c66ac9dbSNicholas Bellinger } 2616c66ac9dbSNicholas Bellinger 2617de103c93SChristoph Hellwig static sense_reason_t 2618de103c93SChristoph Hellwig core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key) 2619c66ac9dbSNicholas Bellinger { 2620c66ac9dbSNicholas Bellinger struct se_device *dev = cmd->se_dev; 2621c66ac9dbSNicholas Bellinger struct se_node_acl *pr_reg_nacl; 2622e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 26230fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 2624c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder; 2625c66ac9dbSNicholas Bellinger u32 pr_res_mapped_lun = 0; 2626c66ac9dbSNicholas Bellinger int calling_it_nexus = 0; 2627c66ac9dbSNicholas Bellinger /* 2628c66ac9dbSNicholas Bellinger * Locate the existing *pr_reg via struct se_node_acl pointers 2629c66ac9dbSNicholas Bellinger */ 26305951146dSAndy Grover pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, 2631c66ac9dbSNicholas Bellinger se_sess->se_node_acl, se_sess); 26326708bb27SAndy Grover if (!pr_reg_n) { 26336708bb27SAndy Grover pr_err("SPC-3 PR: Unable to locate" 2634c66ac9dbSNicholas Bellinger " PR_REGISTERED *pr_reg for CLEAR\n"); 2635de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2636c66ac9dbSNicholas Bellinger } 2637c66ac9dbSNicholas Bellinger /* 2638c66ac9dbSNicholas Bellinger * From spc4r17 section 5.7.11.6, Clearing: 2639c66ac9dbSNicholas Bellinger * 2640c66ac9dbSNicholas Bellinger * Any application client may release the persistent reservation and 2641c66ac9dbSNicholas Bellinger * remove all registrations from a device server by issuing a 2642c66ac9dbSNicholas Bellinger * PERSISTENT RESERVE OUT command with CLEAR service action through a 2643c66ac9dbSNicholas Bellinger * registered I_T nexus with the following parameter: 2644c66ac9dbSNicholas Bellinger * 2645c66ac9dbSNicholas Bellinger * a) RESERVATION KEY field set to the value of the reservation key 2646c66ac9dbSNicholas Bellinger * that is registered with the logical unit for the I_T nexus. 2647c66ac9dbSNicholas Bellinger */ 2648c66ac9dbSNicholas Bellinger if (res_key != pr_reg_n->pr_res_key) { 26496708bb27SAndy Grover pr_err("SPC-3 PR REGISTER: Received" 2650c66ac9dbSNicholas Bellinger " res_key: 0x%016Lx does not match" 2651c66ac9dbSNicholas Bellinger " existing SA REGISTER res_key:" 2652c66ac9dbSNicholas Bellinger " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key); 2653c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_n); 2654de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 2655c66ac9dbSNicholas Bellinger } 2656c66ac9dbSNicholas Bellinger /* 2657c66ac9dbSNicholas Bellinger * a) Release the persistent reservation, if any; 2658c66ac9dbSNicholas Bellinger */ 2659c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 2660c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 2661c66ac9dbSNicholas Bellinger if (pr_res_holder) { 2662c66ac9dbSNicholas Bellinger struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; 2663c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_release(dev, pr_res_nacl, 26646c3c9baaSNicholas Bellinger pr_res_holder, 0, 0); 2665c66ac9dbSNicholas Bellinger } 2666c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2667c66ac9dbSNicholas Bellinger /* 2668c66ac9dbSNicholas Bellinger * b) Remove all registration(s) (see spc4r17 5.7.7); 2669c66ac9dbSNicholas Bellinger */ 2670c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 2671c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 2672c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 2673c66ac9dbSNicholas Bellinger 2674c66ac9dbSNicholas Bellinger calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; 2675c66ac9dbSNicholas Bellinger pr_reg_nacl = pr_reg->pr_reg_nacl; 2676c66ac9dbSNicholas Bellinger pr_res_mapped_lun = pr_reg->pr_res_mapped_lun; 2677c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, pr_reg, NULL, 2678c66ac9dbSNicholas Bellinger calling_it_nexus); 2679c66ac9dbSNicholas Bellinger /* 2680c66ac9dbSNicholas Bellinger * e) Establish a unit attention condition for the initiator 2681c66ac9dbSNicholas Bellinger * port associated with every registered I_T nexus other 2682c66ac9dbSNicholas Bellinger * than the I_T nexus on which the PERSISTENT RESERVE OUT 2683c66ac9dbSNicholas Bellinger * command with CLEAR service action was received, with the 2684c66ac9dbSNicholas Bellinger * additional sense code set to RESERVATIONS PREEMPTED. 2685c66ac9dbSNicholas Bellinger */ 26866708bb27SAndy Grover if (!calling_it_nexus) 2687c66ac9dbSNicholas Bellinger core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 2688c66ac9dbSNicholas Bellinger 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED); 2689c66ac9dbSNicholas Bellinger } 2690c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 2691c66ac9dbSNicholas Bellinger 26926708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n", 2693e3d6f909SAndy Grover cmd->se_tfo->get_fabric_name()); 2694c66ac9dbSNicholas Bellinger 2695459f213bSAndy Grover core_scsi3_update_and_write_aptpl(cmd->se_dev, false); 2696c66ac9dbSNicholas Bellinger 2697c66ac9dbSNicholas Bellinger core_scsi3_pr_generation(dev); 2698c66ac9dbSNicholas Bellinger return 0; 2699c66ac9dbSNicholas Bellinger } 2700c66ac9dbSNicholas Bellinger 2701c66ac9dbSNicholas Bellinger /* 2702c66ac9dbSNicholas Bellinger * Called with struct se_device->dev_reservation_lock held. 2703c66ac9dbSNicholas Bellinger */ 2704c66ac9dbSNicholas Bellinger static void __core_scsi3_complete_pro_preempt( 2705c66ac9dbSNicholas Bellinger struct se_device *dev, 2706c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, 2707c66ac9dbSNicholas Bellinger struct list_head *preempt_and_abort_list, 2708c66ac9dbSNicholas Bellinger int type, 2709c66ac9dbSNicholas Bellinger int scope, 271033ce6a87SAndy Grover enum preempt_type preempt_type) 2711c66ac9dbSNicholas Bellinger { 2712c66ac9dbSNicholas Bellinger struct se_node_acl *nacl = pr_reg->pr_reg_nacl; 2713c66ac9dbSNicholas Bellinger struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo; 2714c66ac9dbSNicholas Bellinger char i_buf[PR_REG_ISID_ID_LEN]; 2715c66ac9dbSNicholas Bellinger 2716c66ac9dbSNicholas Bellinger memset(i_buf, 0, PR_REG_ISID_ID_LEN); 2717d2843c17SAndy Grover core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); 2718c66ac9dbSNicholas Bellinger /* 2719125d0119SHannes Reinecke * Do an implicit RELEASE of the existing reservation. 2720c66ac9dbSNicholas Bellinger */ 2721c66ac9dbSNicholas Bellinger if (dev->dev_pr_res_holder) 2722c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_release(dev, nacl, 27236c3c9baaSNicholas Bellinger dev->dev_pr_res_holder, 0, 0); 2724c66ac9dbSNicholas Bellinger 2725c66ac9dbSNicholas Bellinger dev->dev_pr_res_holder = pr_reg; 2726c66ac9dbSNicholas Bellinger pr_reg->pr_res_holder = 1; 2727c66ac9dbSNicholas Bellinger pr_reg->pr_res_type = type; 2728c66ac9dbSNicholas Bellinger pr_reg->pr_res_scope = scope; 2729c66ac9dbSNicholas Bellinger 27306708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new" 2731c66ac9dbSNicholas Bellinger " reservation holder TYPE: %s ALL_TG_PT: %d\n", 273233ce6a87SAndy Grover tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", 2733c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(type), 2734c66ac9dbSNicholas Bellinger (pr_reg->pr_reg_all_tg_pt) ? 1 : 0); 27356708bb27SAndy Grover pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n", 273633ce6a87SAndy Grover tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", 2737d2843c17SAndy Grover nacl->initiatorname, i_buf); 2738c66ac9dbSNicholas Bellinger /* 2739c66ac9dbSNicholas Bellinger * For PREEMPT_AND_ABORT, add the preempting reservation's 2740c66ac9dbSNicholas Bellinger * struct t10_pr_registration to the list that will be compared 2741c66ac9dbSNicholas Bellinger * against received CDBs.. 2742c66ac9dbSNicholas Bellinger */ 2743c66ac9dbSNicholas Bellinger if (preempt_and_abort_list) 2744c66ac9dbSNicholas Bellinger list_add_tail(&pr_reg->pr_reg_abort_list, 2745c66ac9dbSNicholas Bellinger preempt_and_abort_list); 2746c66ac9dbSNicholas Bellinger } 2747c66ac9dbSNicholas Bellinger 2748c66ac9dbSNicholas Bellinger static void core_scsi3_release_preempt_and_abort( 2749c66ac9dbSNicholas Bellinger struct list_head *preempt_and_abort_list, 2750c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg_holder) 2751c66ac9dbSNicholas Bellinger { 2752c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp; 2753c66ac9dbSNicholas Bellinger 2754c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list, 2755c66ac9dbSNicholas Bellinger pr_reg_abort_list) { 2756c66ac9dbSNicholas Bellinger 2757c66ac9dbSNicholas Bellinger list_del(&pr_reg->pr_reg_abort_list); 2758c66ac9dbSNicholas Bellinger if (pr_reg_holder == pr_reg) 2759c66ac9dbSNicholas Bellinger continue; 2760c66ac9dbSNicholas Bellinger if (pr_reg->pr_res_holder) { 27616708bb27SAndy Grover pr_warn("pr_reg->pr_res_holder still set\n"); 2762c66ac9dbSNicholas Bellinger continue; 2763c66ac9dbSNicholas Bellinger } 2764c66ac9dbSNicholas Bellinger 2765c66ac9dbSNicholas Bellinger pr_reg->pr_reg_deve = NULL; 2766c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl = NULL; 2767c66ac9dbSNicholas Bellinger kmem_cache_free(t10_pr_reg_cache, pr_reg); 2768c66ac9dbSNicholas Bellinger } 2769c66ac9dbSNicholas Bellinger } 2770c66ac9dbSNicholas Bellinger 2771de103c93SChristoph Hellwig static sense_reason_t 2772de103c93SChristoph Hellwig core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key, 277333ce6a87SAndy Grover u64 sa_res_key, enum preempt_type preempt_type) 2774c66ac9dbSNicholas Bellinger { 27755951146dSAndy Grover struct se_device *dev = cmd->se_dev; 2776c66ac9dbSNicholas Bellinger struct se_node_acl *pr_reg_nacl; 2777e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 2778d0f474e5SRoland Dreier LIST_HEAD(preempt_and_abort_list); 2779c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder; 27800fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 2781c66ac9dbSNicholas Bellinger u32 pr_res_mapped_lun = 0; 2782b6932ee3SSteven Allen int all_reg = 0, calling_it_nexus = 0; 2783b6932ee3SSteven Allen bool sa_res_key_unmatched = sa_res_key != 0; 2784de103c93SChristoph Hellwig int prh_type = 0, prh_scope = 0; 2785c66ac9dbSNicholas Bellinger 2786de103c93SChristoph Hellwig if (!se_sess) 2787de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2788c66ac9dbSNicholas Bellinger 27895951146dSAndy Grover pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl, 2790c66ac9dbSNicholas Bellinger se_sess); 27916708bb27SAndy Grover if (!pr_reg_n) { 27926708bb27SAndy Grover pr_err("SPC-3 PR: Unable to locate" 2793c66ac9dbSNicholas Bellinger " PR_REGISTERED *pr_reg for PREEMPT%s\n", 279433ce6a87SAndy Grover (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : ""); 2795de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 2796c66ac9dbSNicholas Bellinger } 2797c66ac9dbSNicholas Bellinger if (pr_reg_n->pr_res_key != res_key) { 2798c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_n); 2799de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 2800c66ac9dbSNicholas Bellinger } 2801c66ac9dbSNicholas Bellinger if (scope != PR_SCOPE_LU_SCOPE) { 28026708bb27SAndy Grover pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope); 2803c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_n); 2804de103c93SChristoph Hellwig return TCM_INVALID_PARAMETER_LIST; 2805c66ac9dbSNicholas Bellinger } 2806c66ac9dbSNicholas Bellinger 2807c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 2808c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 2809c66ac9dbSNicholas Bellinger if (pr_res_holder && 2810c66ac9dbSNicholas Bellinger ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || 2811c66ac9dbSNicholas Bellinger (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) 2812c66ac9dbSNicholas Bellinger all_reg = 1; 2813c66ac9dbSNicholas Bellinger 28146708bb27SAndy Grover if (!all_reg && !sa_res_key) { 2815c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2816c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_n); 2817de103c93SChristoph Hellwig return TCM_INVALID_PARAMETER_LIST; 2818c66ac9dbSNicholas Bellinger } 2819c66ac9dbSNicholas Bellinger /* 2820c66ac9dbSNicholas Bellinger * From spc4r17, section 5.7.11.4.4 Removing Registrations: 2821c66ac9dbSNicholas Bellinger * 2822c66ac9dbSNicholas Bellinger * If the SERVICE ACTION RESERVATION KEY field does not identify a 2823c66ac9dbSNicholas Bellinger * persistent reservation holder or there is no persistent reservation 2824c66ac9dbSNicholas Bellinger * holder (i.e., there is no persistent reservation), then the device 2825c66ac9dbSNicholas Bellinger * server shall perform a preempt by doing the following in an 2826c66ac9dbSNicholas Bellinger * uninterrupted series of actions. (See below..) 2827c66ac9dbSNicholas Bellinger */ 28286708bb27SAndy Grover if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) { 2829c66ac9dbSNicholas Bellinger /* 2830c66ac9dbSNicholas Bellinger * No existing or SA Reservation Key matching reservations.. 2831c66ac9dbSNicholas Bellinger * 2832c66ac9dbSNicholas Bellinger * PROUT SA PREEMPT with All Registrant type reservations are 2833c66ac9dbSNicholas Bellinger * allowed to be processed without a matching SA Reservation Key 2834c66ac9dbSNicholas Bellinger */ 2835c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 2836c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 2837c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 2838c66ac9dbSNicholas Bellinger /* 2839c66ac9dbSNicholas Bellinger * Removing of registrations in non all registrants 2840c66ac9dbSNicholas Bellinger * type reservations without a matching SA reservation 2841c66ac9dbSNicholas Bellinger * key. 2842c66ac9dbSNicholas Bellinger * 2843c66ac9dbSNicholas Bellinger * a) Remove the registrations for all I_T nexuses 2844c66ac9dbSNicholas Bellinger * specified by the SERVICE ACTION RESERVATION KEY 2845c66ac9dbSNicholas Bellinger * field; 2846c66ac9dbSNicholas Bellinger * b) Ignore the contents of the SCOPE and TYPE fields; 2847c66ac9dbSNicholas Bellinger * c) Process tasks as defined in 5.7.1; and 2848c66ac9dbSNicholas Bellinger * d) Establish a unit attention condition for the 2849c66ac9dbSNicholas Bellinger * initiator port associated with every I_T nexus 2850c66ac9dbSNicholas Bellinger * that lost its registration other than the I_T 2851c66ac9dbSNicholas Bellinger * nexus on which the PERSISTENT RESERVE OUT command 2852c66ac9dbSNicholas Bellinger * was received, with the additional sense code set 2853c66ac9dbSNicholas Bellinger * to REGISTRATIONS PREEMPTED. 2854c66ac9dbSNicholas Bellinger */ 28556708bb27SAndy Grover if (!all_reg) { 2856c66ac9dbSNicholas Bellinger if (pr_reg->pr_res_key != sa_res_key) 2857c66ac9dbSNicholas Bellinger continue; 2858b6932ee3SSteven Allen sa_res_key_unmatched = false; 2859c66ac9dbSNicholas Bellinger 2860c66ac9dbSNicholas Bellinger calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; 2861c66ac9dbSNicholas Bellinger pr_reg_nacl = pr_reg->pr_reg_nacl; 2862c66ac9dbSNicholas Bellinger pr_res_mapped_lun = pr_reg->pr_res_mapped_lun; 2863c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, pr_reg, 286433ce6a87SAndy Grover (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : 2865c66ac9dbSNicholas Bellinger NULL, calling_it_nexus); 2866c66ac9dbSNicholas Bellinger } else { 2867c66ac9dbSNicholas Bellinger /* 2868c66ac9dbSNicholas Bellinger * Case for any existing all registrants type 2869c66ac9dbSNicholas Bellinger * reservation, follow logic in spc4r17 section 2870c66ac9dbSNicholas Bellinger * 5.7.11.4 Preempting, Table 52 and Figure 7. 2871c66ac9dbSNicholas Bellinger * 2872c66ac9dbSNicholas Bellinger * For a ZERO SA Reservation key, release 2873125d0119SHannes Reinecke * all other registrations and do an implicit 2874c66ac9dbSNicholas Bellinger * release of active persistent reservation. 2875c66ac9dbSNicholas Bellinger * 2876c66ac9dbSNicholas Bellinger * For a non-ZERO SA Reservation key, only 2877c66ac9dbSNicholas Bellinger * release the matching reservation key from 2878c66ac9dbSNicholas Bellinger * registrations. 2879c66ac9dbSNicholas Bellinger */ 2880c66ac9dbSNicholas Bellinger if ((sa_res_key) && 2881c66ac9dbSNicholas Bellinger (pr_reg->pr_res_key != sa_res_key)) 2882c66ac9dbSNicholas Bellinger continue; 2883b6932ee3SSteven Allen sa_res_key_unmatched = false; 2884c66ac9dbSNicholas Bellinger 2885c66ac9dbSNicholas Bellinger calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; 2886c66ac9dbSNicholas Bellinger if (calling_it_nexus) 2887c66ac9dbSNicholas Bellinger continue; 2888c66ac9dbSNicholas Bellinger 2889c66ac9dbSNicholas Bellinger pr_reg_nacl = pr_reg->pr_reg_nacl; 2890c66ac9dbSNicholas Bellinger pr_res_mapped_lun = pr_reg->pr_res_mapped_lun; 2891c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, pr_reg, 289233ce6a87SAndy Grover (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : 2893c66ac9dbSNicholas Bellinger NULL, 0); 2894c66ac9dbSNicholas Bellinger } 28956708bb27SAndy Grover if (!calling_it_nexus) 2896c66ac9dbSNicholas Bellinger core_scsi3_ua_allocate(pr_reg_nacl, 2897c66ac9dbSNicholas Bellinger pr_res_mapped_lun, 0x2A, 28989e08e34eSMarco Sanvido ASCQ_2AH_REGISTRATIONS_PREEMPTED); 2899c66ac9dbSNicholas Bellinger } 2900c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 2901c66ac9dbSNicholas Bellinger /* 2902c66ac9dbSNicholas Bellinger * If a PERSISTENT RESERVE OUT with a PREEMPT service action or 2903c66ac9dbSNicholas Bellinger * a PREEMPT AND ABORT service action sets the SERVICE ACTION 2904c66ac9dbSNicholas Bellinger * RESERVATION KEY field to a value that does not match any 2905c66ac9dbSNicholas Bellinger * registered reservation key, then the device server shall 2906c66ac9dbSNicholas Bellinger * complete the command with RESERVATION CONFLICT status. 2907c66ac9dbSNicholas Bellinger */ 2908b6932ee3SSteven Allen if (sa_res_key_unmatched) { 2909c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2910c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_n); 2911de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 2912c66ac9dbSNicholas Bellinger } 2913c66ac9dbSNicholas Bellinger /* 2914c66ac9dbSNicholas Bellinger * For an existing all registrants type reservation 2915c66ac9dbSNicholas Bellinger * with a zero SA rservation key, preempt the existing 2916c66ac9dbSNicholas Bellinger * reservation with the new PR type and scope. 2917c66ac9dbSNicholas Bellinger */ 2918c66ac9dbSNicholas Bellinger if (pr_res_holder && all_reg && !(sa_res_key)) { 2919c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_preempt(dev, pr_reg_n, 292033ce6a87SAndy Grover (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL, 292133ce6a87SAndy Grover type, scope, preempt_type); 2922c66ac9dbSNicholas Bellinger 292333ce6a87SAndy Grover if (preempt_type == PREEMPT_AND_ABORT) 2924c66ac9dbSNicholas Bellinger core_scsi3_release_preempt_and_abort( 2925c66ac9dbSNicholas Bellinger &preempt_and_abort_list, pr_reg_n); 2926c66ac9dbSNicholas Bellinger } 2927c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2928c66ac9dbSNicholas Bellinger 2929459f213bSAndy Grover if (pr_tmpl->pr_aptpl_active) 2930459f213bSAndy Grover core_scsi3_update_and_write_aptpl(cmd->se_dev, true); 2931c66ac9dbSNicholas Bellinger 2932c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_n); 29335951146dSAndy Grover core_scsi3_pr_generation(cmd->se_dev); 2934c66ac9dbSNicholas Bellinger return 0; 2935c66ac9dbSNicholas Bellinger } 2936c66ac9dbSNicholas Bellinger /* 2937c66ac9dbSNicholas Bellinger * The PREEMPTing SA reservation key matches that of the 2938c66ac9dbSNicholas Bellinger * existing persistent reservation, first, we check if 2939c66ac9dbSNicholas Bellinger * we are preempting our own reservation. 2940c66ac9dbSNicholas Bellinger * From spc4r17, section 5.7.11.4.3 Preempting 2941c66ac9dbSNicholas Bellinger * persistent reservations and registration handling 2942c66ac9dbSNicholas Bellinger * 2943c66ac9dbSNicholas Bellinger * If an all registrants persistent reservation is not 2944c66ac9dbSNicholas Bellinger * present, it is not an error for the persistent 2945c66ac9dbSNicholas Bellinger * reservation holder to preempt itself (i.e., a 2946c66ac9dbSNicholas Bellinger * PERSISTENT RESERVE OUT with a PREEMPT service action 2947c66ac9dbSNicholas Bellinger * or a PREEMPT AND ABORT service action with the 2948c66ac9dbSNicholas Bellinger * SERVICE ACTION RESERVATION KEY value equal to the 2949c66ac9dbSNicholas Bellinger * persistent reservation holder's reservation key that 2950c66ac9dbSNicholas Bellinger * is received from the persistent reservation holder). 2951c66ac9dbSNicholas Bellinger * In that case, the device server shall establish the 2952c66ac9dbSNicholas Bellinger * new persistent reservation and maintain the 2953c66ac9dbSNicholas Bellinger * registration. 2954c66ac9dbSNicholas Bellinger */ 2955c66ac9dbSNicholas Bellinger prh_type = pr_res_holder->pr_res_type; 2956c66ac9dbSNicholas Bellinger prh_scope = pr_res_holder->pr_res_scope; 2957c66ac9dbSNicholas Bellinger /* 2958c66ac9dbSNicholas Bellinger * If the SERVICE ACTION RESERVATION KEY field identifies a 2959c66ac9dbSNicholas Bellinger * persistent reservation holder (see 5.7.10), the device 2960c66ac9dbSNicholas Bellinger * server shall perform a preempt by doing the following as 2961c66ac9dbSNicholas Bellinger * an uninterrupted series of actions: 2962c66ac9dbSNicholas Bellinger * 2963c66ac9dbSNicholas Bellinger * a) Release the persistent reservation for the holder 2964c66ac9dbSNicholas Bellinger * identified by the SERVICE ACTION RESERVATION KEY field; 2965c66ac9dbSNicholas Bellinger */ 2966c66ac9dbSNicholas Bellinger if (pr_reg_n != pr_res_holder) 2967c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_release(dev, 2968c66ac9dbSNicholas Bellinger pr_res_holder->pr_reg_nacl, 29696c3c9baaSNicholas Bellinger dev->dev_pr_res_holder, 0, 0); 2970c66ac9dbSNicholas Bellinger /* 2971c66ac9dbSNicholas Bellinger * b) Remove the registrations for all I_T nexuses identified 2972c66ac9dbSNicholas Bellinger * by the SERVICE ACTION RESERVATION KEY field, except the 2973c66ac9dbSNicholas Bellinger * I_T nexus that is being used for the PERSISTENT RESERVE 2974c66ac9dbSNicholas Bellinger * OUT command. If an all registrants persistent reservation 2975c66ac9dbSNicholas Bellinger * is present and the SERVICE ACTION RESERVATION KEY field 2976c66ac9dbSNicholas Bellinger * is set to zero, then all registrations shall be removed 2977c66ac9dbSNicholas Bellinger * except for that of the I_T nexus that is being used for 2978c66ac9dbSNicholas Bellinger * the PERSISTENT RESERVE OUT command; 2979c66ac9dbSNicholas Bellinger */ 2980c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 2981c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 2982c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 2983c66ac9dbSNicholas Bellinger 2984c66ac9dbSNicholas Bellinger calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; 2985c66ac9dbSNicholas Bellinger if (calling_it_nexus) 2986c66ac9dbSNicholas Bellinger continue; 2987c66ac9dbSNicholas Bellinger 2988c66ac9dbSNicholas Bellinger if (pr_reg->pr_res_key != sa_res_key) 2989c66ac9dbSNicholas Bellinger continue; 2990c66ac9dbSNicholas Bellinger 2991c66ac9dbSNicholas Bellinger pr_reg_nacl = pr_reg->pr_reg_nacl; 2992c66ac9dbSNicholas Bellinger pr_res_mapped_lun = pr_reg->pr_res_mapped_lun; 2993c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, pr_reg, 299433ce6a87SAndy Grover (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL, 2995c66ac9dbSNicholas Bellinger calling_it_nexus); 2996c66ac9dbSNicholas Bellinger /* 2997c66ac9dbSNicholas Bellinger * e) Establish a unit attention condition for the initiator 2998c66ac9dbSNicholas Bellinger * port associated with every I_T nexus that lost its 2999c66ac9dbSNicholas Bellinger * persistent reservation and/or registration, with the 3000c66ac9dbSNicholas Bellinger * additional sense code set to REGISTRATIONS PREEMPTED; 3001c66ac9dbSNicholas Bellinger */ 3002c66ac9dbSNicholas Bellinger core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 0x2A, 30039e08e34eSMarco Sanvido ASCQ_2AH_REGISTRATIONS_PREEMPTED); 3004c66ac9dbSNicholas Bellinger } 3005c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 3006c66ac9dbSNicholas Bellinger /* 3007c66ac9dbSNicholas Bellinger * c) Establish a persistent reservation for the preempting 3008c66ac9dbSNicholas Bellinger * I_T nexus using the contents of the SCOPE and TYPE fields; 3009c66ac9dbSNicholas Bellinger */ 3010c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_preempt(dev, pr_reg_n, 301133ce6a87SAndy Grover (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL, 301233ce6a87SAndy Grover type, scope, preempt_type); 3013c66ac9dbSNicholas Bellinger /* 3014c66ac9dbSNicholas Bellinger * d) Process tasks as defined in 5.7.1; 3015c66ac9dbSNicholas Bellinger * e) See above.. 3016c66ac9dbSNicholas Bellinger * f) If the type or scope has changed, then for every I_T nexus 3017c66ac9dbSNicholas Bellinger * whose reservation key was not removed, except for the I_T 3018c66ac9dbSNicholas Bellinger * nexus on which the PERSISTENT RESERVE OUT command was 3019c66ac9dbSNicholas Bellinger * received, the device server shall establish a unit 3020c66ac9dbSNicholas Bellinger * attention condition for the initiator port associated with 3021c66ac9dbSNicholas Bellinger * that I_T nexus, with the additional sense code set to 3022c66ac9dbSNicholas Bellinger * RESERVATIONS RELEASED. If the type or scope have not 3023c66ac9dbSNicholas Bellinger * changed, then no unit attention condition(s) shall be 3024c66ac9dbSNicholas Bellinger * established for this reason. 3025c66ac9dbSNicholas Bellinger */ 3026c66ac9dbSNicholas Bellinger if ((prh_type != type) || (prh_scope != scope)) { 3027c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 3028c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 3029c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 3030c66ac9dbSNicholas Bellinger 3031c66ac9dbSNicholas Bellinger calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; 3032c66ac9dbSNicholas Bellinger if (calling_it_nexus) 3033c66ac9dbSNicholas Bellinger continue; 3034c66ac9dbSNicholas Bellinger 3035c66ac9dbSNicholas Bellinger core_scsi3_ua_allocate(pr_reg->pr_reg_nacl, 3036c66ac9dbSNicholas Bellinger pr_reg->pr_res_mapped_lun, 0x2A, 3037c66ac9dbSNicholas Bellinger ASCQ_2AH_RESERVATIONS_RELEASED); 3038c66ac9dbSNicholas Bellinger } 3039c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 3040c66ac9dbSNicholas Bellinger } 3041c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 3042c66ac9dbSNicholas Bellinger /* 3043c66ac9dbSNicholas Bellinger * Call LUN_RESET logic upon list of struct t10_pr_registration, 3044c66ac9dbSNicholas Bellinger * All received CDBs for the matching existing reservation and 3045c66ac9dbSNicholas Bellinger * registrations undergo ABORT_TASK logic. 3046c66ac9dbSNicholas Bellinger * 3047c66ac9dbSNicholas Bellinger * From there, core_scsi3_release_preempt_and_abort() will 3048c66ac9dbSNicholas Bellinger * release every registration in the list (which have already 3049c66ac9dbSNicholas Bellinger * been removed from the primary pr_reg list), except the 3050c66ac9dbSNicholas Bellinger * new persistent reservation holder, the calling Initiator Port. 3051c66ac9dbSNicholas Bellinger */ 305233ce6a87SAndy Grover if (preempt_type == PREEMPT_AND_ABORT) { 3053c66ac9dbSNicholas Bellinger core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd); 3054c66ac9dbSNicholas Bellinger core_scsi3_release_preempt_and_abort(&preempt_and_abort_list, 3055c66ac9dbSNicholas Bellinger pr_reg_n); 3056c66ac9dbSNicholas Bellinger } 3057c66ac9dbSNicholas Bellinger 3058459f213bSAndy Grover if (pr_tmpl->pr_aptpl_active) 3059459f213bSAndy Grover core_scsi3_update_and_write_aptpl(cmd->se_dev, true); 3060c66ac9dbSNicholas Bellinger 3061c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_n); 30625951146dSAndy Grover core_scsi3_pr_generation(cmd->se_dev); 3063c66ac9dbSNicholas Bellinger return 0; 3064c66ac9dbSNicholas Bellinger } 3065c66ac9dbSNicholas Bellinger 3066de103c93SChristoph Hellwig static sense_reason_t 3067de103c93SChristoph Hellwig core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope, 306833ce6a87SAndy Grover u64 res_key, u64 sa_res_key, enum preempt_type preempt_type) 3069c66ac9dbSNicholas Bellinger { 3070c66ac9dbSNicholas Bellinger switch (type) { 3071c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE: 3072c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS: 3073c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_REGONLY: 3074c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY: 3075c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_ALLREG: 3076c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG: 3077de103c93SChristoph Hellwig return core_scsi3_pro_preempt(cmd, type, scope, res_key, 307833ce6a87SAndy Grover sa_res_key, preempt_type); 3079c66ac9dbSNicholas Bellinger default: 30806708bb27SAndy Grover pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s" 308133ce6a87SAndy Grover " Type: 0x%02x\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", type); 3082de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 3083c66ac9dbSNicholas Bellinger } 3084c66ac9dbSNicholas Bellinger } 3085c66ac9dbSNicholas Bellinger 3086c66ac9dbSNicholas Bellinger 3087de103c93SChristoph Hellwig static sense_reason_t 3088de103c93SChristoph Hellwig core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key, 3089de103c93SChristoph Hellwig u64 sa_res_key, int aptpl, int unreg) 3090c66ac9dbSNicholas Bellinger { 3091e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 30925951146dSAndy Grover struct se_device *dev = cmd->se_dev; 309328168905SJörn Engel struct se_dev_entry *dest_se_deve = NULL; 3094e3d6f909SAndy Grover struct se_lun *se_lun = cmd->se_lun; 3095c66ac9dbSNicholas Bellinger struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL; 3096c66ac9dbSNicholas Bellinger struct se_port *se_port; 3097c66ac9dbSNicholas Bellinger struct se_portal_group *se_tpg, *dest_se_tpg = NULL; 3098c66ac9dbSNicholas Bellinger struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops; 3099c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg; 31000fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 310105d1c7c0SAndy Grover unsigned char *buf; 3102c66ac9dbSNicholas Bellinger unsigned char *initiator_str; 310313ba564cSJulia Lawall char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN]; 3104c66ac9dbSNicholas Bellinger u32 tid_len, tmp_tid_len; 3105d2843c17SAndy Grover int new_reg = 0, type, scope, matching_iname; 3106de103c93SChristoph Hellwig sense_reason_t ret; 3107c66ac9dbSNicholas Bellinger unsigned short rtpi; 3108c66ac9dbSNicholas Bellinger unsigned char proto_ident; 3109c66ac9dbSNicholas Bellinger 31106708bb27SAndy Grover if (!se_sess || !se_lun) { 31116708bb27SAndy Grover pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n"); 3112de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3113c66ac9dbSNicholas Bellinger } 3114de103c93SChristoph Hellwig 3115c66ac9dbSNicholas Bellinger memset(i_buf, 0, PR_REG_ISID_ID_LEN); 3116c66ac9dbSNicholas Bellinger se_tpg = se_sess->se_tpg; 3117e3d6f909SAndy Grover tf_ops = se_tpg->se_tpg_tfo; 3118c66ac9dbSNicholas Bellinger /* 3119c66ac9dbSNicholas Bellinger * Follow logic from spc4r17 Section 5.7.8, Table 50 -- 3120c66ac9dbSNicholas Bellinger * Register behaviors for a REGISTER AND MOVE service action 3121c66ac9dbSNicholas Bellinger * 3122c66ac9dbSNicholas Bellinger * Locate the existing *pr_reg via struct se_node_acl pointers 3123c66ac9dbSNicholas Bellinger */ 31245951146dSAndy Grover pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl, 3125c66ac9dbSNicholas Bellinger se_sess); 31266708bb27SAndy Grover if (!pr_reg) { 31276708bb27SAndy Grover pr_err("SPC-3 PR: Unable to locate PR_REGISTERED" 3128c66ac9dbSNicholas Bellinger " *pr_reg for REGISTER_AND_MOVE\n"); 3129de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3130c66ac9dbSNicholas Bellinger } 3131c66ac9dbSNicholas Bellinger /* 3132c66ac9dbSNicholas Bellinger * The provided reservation key much match the existing reservation key 3133c66ac9dbSNicholas Bellinger * provided during this initiator's I_T nexus registration. 3134c66ac9dbSNicholas Bellinger */ 3135c66ac9dbSNicholas Bellinger if (res_key != pr_reg->pr_res_key) { 31366708bb27SAndy Grover pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received" 3137c66ac9dbSNicholas Bellinger " res_key: 0x%016Lx does not match existing SA REGISTER" 3138c66ac9dbSNicholas Bellinger " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key); 3139de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 3140de103c93SChristoph Hellwig goto out_put_pr_reg; 3141c66ac9dbSNicholas Bellinger } 3142c66ac9dbSNicholas Bellinger /* 3143c66ac9dbSNicholas Bellinger * The service active reservation key needs to be non zero 3144c66ac9dbSNicholas Bellinger */ 31456708bb27SAndy Grover if (!sa_res_key) { 31466708bb27SAndy Grover pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero" 3147c66ac9dbSNicholas Bellinger " sa_res_key\n"); 3148de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3149de103c93SChristoph Hellwig goto out_put_pr_reg; 3150c66ac9dbSNicholas Bellinger } 315105d1c7c0SAndy Grover 3152c66ac9dbSNicholas Bellinger /* 3153c66ac9dbSNicholas Bellinger * Determine the Relative Target Port Identifier where the reservation 3154c66ac9dbSNicholas Bellinger * will be moved to for the TransportID containing SCSI initiator WWN 3155c66ac9dbSNicholas Bellinger * information. 3156c66ac9dbSNicholas Bellinger */ 31574949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 3158de103c93SChristoph Hellwig if (!buf) { 3159de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3160de103c93SChristoph Hellwig goto out_put_pr_reg; 3161de103c93SChristoph Hellwig } 3162de103c93SChristoph Hellwig 3163c66ac9dbSNicholas Bellinger rtpi = (buf[18] & 0xff) << 8; 3164c66ac9dbSNicholas Bellinger rtpi |= buf[19] & 0xff; 3165c66ac9dbSNicholas Bellinger tid_len = (buf[20] & 0xff) << 24; 3166c66ac9dbSNicholas Bellinger tid_len |= (buf[21] & 0xff) << 16; 3167c66ac9dbSNicholas Bellinger tid_len |= (buf[22] & 0xff) << 8; 3168c66ac9dbSNicholas Bellinger tid_len |= buf[23] & 0xff; 31694949314cSAndy Grover transport_kunmap_data_sg(cmd); 317005d1c7c0SAndy Grover buf = NULL; 3171c66ac9dbSNicholas Bellinger 3172c66ac9dbSNicholas Bellinger if ((tid_len + 24) != cmd->data_length) { 31736708bb27SAndy Grover pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header" 3174c66ac9dbSNicholas Bellinger " does not equal CDB data_length: %u\n", tid_len, 3175c66ac9dbSNicholas Bellinger cmd->data_length); 3176de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3177de103c93SChristoph Hellwig goto out_put_pr_reg; 3178c66ac9dbSNicholas Bellinger } 3179c66ac9dbSNicholas Bellinger 3180c66ac9dbSNicholas Bellinger spin_lock(&dev->se_port_lock); 3181c66ac9dbSNicholas Bellinger list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) { 3182c66ac9dbSNicholas Bellinger if (se_port->sep_rtpi != rtpi) 3183c66ac9dbSNicholas Bellinger continue; 3184c66ac9dbSNicholas Bellinger dest_se_tpg = se_port->sep_tpg; 31856708bb27SAndy Grover if (!dest_se_tpg) 3186c66ac9dbSNicholas Bellinger continue; 3187e3d6f909SAndy Grover dest_tf_ops = dest_se_tpg->se_tpg_tfo; 31886708bb27SAndy Grover if (!dest_tf_ops) 3189c66ac9dbSNicholas Bellinger continue; 3190c66ac9dbSNicholas Bellinger 319133940d09SJoern Engel atomic_inc_mb(&dest_se_tpg->tpg_pr_ref_count); 3192c66ac9dbSNicholas Bellinger spin_unlock(&dev->se_port_lock); 3193c66ac9dbSNicholas Bellinger 3194de103c93SChristoph Hellwig if (core_scsi3_tpg_depend_item(dest_se_tpg)) { 31956708bb27SAndy Grover pr_err("core_scsi3_tpg_depend_item() failed" 3196c66ac9dbSNicholas Bellinger " for dest_se_tpg\n"); 319733940d09SJoern Engel atomic_dec_mb(&dest_se_tpg->tpg_pr_ref_count); 3198de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3199de103c93SChristoph Hellwig goto out_put_pr_reg; 3200c66ac9dbSNicholas Bellinger } 3201c66ac9dbSNicholas Bellinger 3202c66ac9dbSNicholas Bellinger spin_lock(&dev->se_port_lock); 3203c66ac9dbSNicholas Bellinger break; 3204c66ac9dbSNicholas Bellinger } 3205c66ac9dbSNicholas Bellinger spin_unlock(&dev->se_port_lock); 3206c66ac9dbSNicholas Bellinger 32076708bb27SAndy Grover if (!dest_se_tpg || !dest_tf_ops) { 32086708bb27SAndy Grover pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate" 3209c66ac9dbSNicholas Bellinger " fabric ops from Relative Target Port Identifier:" 3210c66ac9dbSNicholas Bellinger " %hu\n", rtpi); 3211de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3212de103c93SChristoph Hellwig goto out_put_pr_reg; 3213c66ac9dbSNicholas Bellinger } 321405d1c7c0SAndy Grover 32154949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 3216de103c93SChristoph Hellwig if (!buf) { 3217de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3218de103c93SChristoph Hellwig goto out_put_pr_reg; 3219de103c93SChristoph Hellwig } 3220c66ac9dbSNicholas Bellinger proto_ident = (buf[24] & 0x0f); 32218b1e1244SAndy Grover 32226708bb27SAndy Grover pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:" 3223c66ac9dbSNicholas Bellinger " 0x%02x\n", proto_ident); 32248b1e1244SAndy Grover 3225c66ac9dbSNicholas Bellinger if (proto_ident != dest_tf_ops->get_fabric_proto_ident(dest_se_tpg)) { 32266708bb27SAndy Grover pr_err("SPC-3 PR REGISTER_AND_MOVE: Received" 3227c66ac9dbSNicholas Bellinger " proto_ident: 0x%02x does not match ident: 0x%02x" 3228c66ac9dbSNicholas Bellinger " from fabric: %s\n", proto_ident, 3229c66ac9dbSNicholas Bellinger dest_tf_ops->get_fabric_proto_ident(dest_se_tpg), 3230c66ac9dbSNicholas Bellinger dest_tf_ops->get_fabric_name()); 3231de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3232c66ac9dbSNicholas Bellinger goto out; 3233c66ac9dbSNicholas Bellinger } 3234c66ac9dbSNicholas Bellinger if (dest_tf_ops->tpg_parse_pr_out_transport_id == NULL) { 32356708bb27SAndy Grover pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not" 3236c66ac9dbSNicholas Bellinger " containg a valid tpg_parse_pr_out_transport_id" 3237c66ac9dbSNicholas Bellinger " function pointer\n"); 3238de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3239c66ac9dbSNicholas Bellinger goto out; 3240c66ac9dbSNicholas Bellinger } 3241c66ac9dbSNicholas Bellinger initiator_str = dest_tf_ops->tpg_parse_pr_out_transport_id(dest_se_tpg, 3242c66ac9dbSNicholas Bellinger (const char *)&buf[24], &tmp_tid_len, &iport_ptr); 32436708bb27SAndy Grover if (!initiator_str) { 32446708bb27SAndy Grover pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate" 3245c66ac9dbSNicholas Bellinger " initiator_str from Transport ID\n"); 3246de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3247c66ac9dbSNicholas Bellinger goto out; 3248c66ac9dbSNicholas Bellinger } 3249c66ac9dbSNicholas Bellinger 32504949314cSAndy Grover transport_kunmap_data_sg(cmd); 325105d1c7c0SAndy Grover buf = NULL; 325205d1c7c0SAndy Grover 32536708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s" 3254c66ac9dbSNicholas Bellinger " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ? 3255c66ac9dbSNicholas Bellinger "port" : "device", initiator_str, (iport_ptr != NULL) ? 3256c66ac9dbSNicholas Bellinger iport_ptr : ""); 3257c66ac9dbSNicholas Bellinger /* 3258c66ac9dbSNicholas Bellinger * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service 3259c66ac9dbSNicholas Bellinger * action specifies a TransportID that is the same as the initiator port 3260c66ac9dbSNicholas Bellinger * of the I_T nexus for the command received, then the command shall 3261c66ac9dbSNicholas Bellinger * be terminated with CHECK CONDITION status, with the sense key set to 3262c66ac9dbSNicholas Bellinger * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD 3263c66ac9dbSNicholas Bellinger * IN PARAMETER LIST. 3264c66ac9dbSNicholas Bellinger */ 3265c66ac9dbSNicholas Bellinger pr_reg_nacl = pr_reg->pr_reg_nacl; 3266c66ac9dbSNicholas Bellinger matching_iname = (!strcmp(initiator_str, 3267c66ac9dbSNicholas Bellinger pr_reg_nacl->initiatorname)) ? 1 : 0; 32686708bb27SAndy Grover if (!matching_iname) 3269c66ac9dbSNicholas Bellinger goto after_iport_check; 3270c66ac9dbSNicholas Bellinger 32716708bb27SAndy Grover if (!iport_ptr || !pr_reg->isid_present_at_reg) { 32726708bb27SAndy Grover pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s" 3273c66ac9dbSNicholas Bellinger " matches: %s on received I_T Nexus\n", initiator_str, 3274c66ac9dbSNicholas Bellinger pr_reg_nacl->initiatorname); 3275de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3276c66ac9dbSNicholas Bellinger goto out; 3277c66ac9dbSNicholas Bellinger } 32786708bb27SAndy Grover if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) { 32796708bb27SAndy Grover pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s" 3280c66ac9dbSNicholas Bellinger " matches: %s %s on received I_T Nexus\n", 3281c66ac9dbSNicholas Bellinger initiator_str, iport_ptr, pr_reg_nacl->initiatorname, 3282c66ac9dbSNicholas Bellinger pr_reg->pr_reg_isid); 3283de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3284c66ac9dbSNicholas Bellinger goto out; 3285c66ac9dbSNicholas Bellinger } 3286c66ac9dbSNicholas Bellinger after_iport_check: 3287c66ac9dbSNicholas Bellinger /* 3288c66ac9dbSNicholas Bellinger * Locate the destination struct se_node_acl from the received Transport ID 3289c66ac9dbSNicholas Bellinger */ 329028638887SRoland Dreier spin_lock_irq(&dest_se_tpg->acl_node_lock); 3291c66ac9dbSNicholas Bellinger dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg, 3292c66ac9dbSNicholas Bellinger initiator_str); 329333940d09SJoern Engel if (dest_node_acl) 329433940d09SJoern Engel atomic_inc_mb(&dest_node_acl->acl_pr_ref_count); 329528638887SRoland Dreier spin_unlock_irq(&dest_se_tpg->acl_node_lock); 3296c66ac9dbSNicholas Bellinger 32976708bb27SAndy Grover if (!dest_node_acl) { 32986708bb27SAndy Grover pr_err("Unable to locate %s dest_node_acl for" 3299c66ac9dbSNicholas Bellinger " TransportID%s\n", dest_tf_ops->get_fabric_name(), 3300c66ac9dbSNicholas Bellinger initiator_str); 3301de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3302c66ac9dbSNicholas Bellinger goto out; 3303c66ac9dbSNicholas Bellinger } 3304de103c93SChristoph Hellwig 3305de103c93SChristoph Hellwig if (core_scsi3_nodeacl_depend_item(dest_node_acl)) { 33066708bb27SAndy Grover pr_err("core_scsi3_nodeacl_depend_item() for" 3307c66ac9dbSNicholas Bellinger " dest_node_acl\n"); 330833940d09SJoern Engel atomic_dec_mb(&dest_node_acl->acl_pr_ref_count); 3309c66ac9dbSNicholas Bellinger dest_node_acl = NULL; 3310de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3311c66ac9dbSNicholas Bellinger goto out; 3312c66ac9dbSNicholas Bellinger } 33138b1e1244SAndy Grover 33146708bb27SAndy Grover pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:" 3315c66ac9dbSNicholas Bellinger " %s from TransportID\n", dest_tf_ops->get_fabric_name(), 3316c66ac9dbSNicholas Bellinger dest_node_acl->initiatorname); 33178b1e1244SAndy Grover 3318c66ac9dbSNicholas Bellinger /* 3319c66ac9dbSNicholas Bellinger * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET 3320c66ac9dbSNicholas Bellinger * PORT IDENTIFIER. 3321c66ac9dbSNicholas Bellinger */ 3322c66ac9dbSNicholas Bellinger dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi); 33236708bb27SAndy Grover if (!dest_se_deve) { 33246708bb27SAndy Grover pr_err("Unable to locate %s dest_se_deve from RTPI:" 3325c66ac9dbSNicholas Bellinger " %hu\n", dest_tf_ops->get_fabric_name(), rtpi); 3326de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3327c66ac9dbSNicholas Bellinger goto out; 3328c66ac9dbSNicholas Bellinger } 3329c66ac9dbSNicholas Bellinger 3330de103c93SChristoph Hellwig if (core_scsi3_lunacl_depend_item(dest_se_deve)) { 33316708bb27SAndy Grover pr_err("core_scsi3_lunacl_depend_item() failed\n"); 333233940d09SJoern Engel atomic_dec_mb(&dest_se_deve->pr_ref_count); 3333c66ac9dbSNicholas Bellinger dest_se_deve = NULL; 3334de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3335c66ac9dbSNicholas Bellinger goto out; 3336c66ac9dbSNicholas Bellinger } 33378b1e1244SAndy Grover 33386708bb27SAndy Grover pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN" 3339c66ac9dbSNicholas Bellinger " ACL for dest_se_deve->mapped_lun: %u\n", 3340c66ac9dbSNicholas Bellinger dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname, 3341c66ac9dbSNicholas Bellinger dest_se_deve->mapped_lun); 33428b1e1244SAndy Grover 3343c66ac9dbSNicholas Bellinger /* 3344c66ac9dbSNicholas Bellinger * A persistent reservation needs to already existing in order to 3345c66ac9dbSNicholas Bellinger * successfully complete the REGISTER_AND_MOVE service action.. 3346c66ac9dbSNicholas Bellinger */ 3347c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 3348c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 33496708bb27SAndy Grover if (!pr_res_holder) { 33506708bb27SAndy Grover pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation" 3351c66ac9dbSNicholas Bellinger " currently held\n"); 3352c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 3353de103c93SChristoph Hellwig ret = TCM_INVALID_CDB_FIELD; 3354c66ac9dbSNicholas Bellinger goto out; 3355c66ac9dbSNicholas Bellinger } 3356c66ac9dbSNicholas Bellinger /* 3357c66ac9dbSNicholas Bellinger * The received on I_T Nexus must be the reservation holder. 3358c66ac9dbSNicholas Bellinger * 3359c66ac9dbSNicholas Bellinger * From spc4r17 section 5.7.8 Table 50 -- 3360c66ac9dbSNicholas Bellinger * Register behaviors for a REGISTER AND MOVE service action 3361c66ac9dbSNicholas Bellinger */ 3362c66ac9dbSNicholas Bellinger if (pr_res_holder != pr_reg) { 33636708bb27SAndy Grover pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T" 3364c66ac9dbSNicholas Bellinger " Nexus is not reservation holder\n"); 3365c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 3366de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 3367c66ac9dbSNicholas Bellinger goto out; 3368c66ac9dbSNicholas Bellinger } 3369c66ac9dbSNicholas Bellinger /* 3370c66ac9dbSNicholas Bellinger * From spc4r17 section 5.7.8: registering and moving reservation 3371c66ac9dbSNicholas Bellinger * 3372c66ac9dbSNicholas Bellinger * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service 3373c66ac9dbSNicholas Bellinger * action is received and the established persistent reservation is a 3374c66ac9dbSNicholas Bellinger * Write Exclusive - All Registrants type or Exclusive Access - 3375c66ac9dbSNicholas Bellinger * All Registrants type reservation, then the command shall be completed 3376c66ac9dbSNicholas Bellinger * with RESERVATION CONFLICT status. 3377c66ac9dbSNicholas Bellinger */ 3378c66ac9dbSNicholas Bellinger if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || 3379c66ac9dbSNicholas Bellinger (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) { 33806708bb27SAndy Grover pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move" 3381c66ac9dbSNicholas Bellinger " reservation for type: %s\n", 3382c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(pr_res_holder->pr_res_type)); 3383c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 3384de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 3385c66ac9dbSNicholas Bellinger goto out; 3386c66ac9dbSNicholas Bellinger } 3387c66ac9dbSNicholas Bellinger pr_res_nacl = pr_res_holder->pr_reg_nacl; 3388c66ac9dbSNicholas Bellinger /* 3389c66ac9dbSNicholas Bellinger * b) Ignore the contents of the (received) SCOPE and TYPE fields; 3390c66ac9dbSNicholas Bellinger */ 3391c66ac9dbSNicholas Bellinger type = pr_res_holder->pr_res_type; 3392c66ac9dbSNicholas Bellinger scope = pr_res_holder->pr_res_type; 3393c66ac9dbSNicholas Bellinger /* 3394c66ac9dbSNicholas Bellinger * c) Associate the reservation key specified in the SERVICE ACTION 3395c66ac9dbSNicholas Bellinger * RESERVATION KEY field with the I_T nexus specified as the 3396c66ac9dbSNicholas Bellinger * destination of the register and move, where: 3397c66ac9dbSNicholas Bellinger * A) The I_T nexus is specified by the TransportID and the 3398c66ac9dbSNicholas Bellinger * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and 3399c66ac9dbSNicholas Bellinger * B) Regardless of the TransportID format used, the association for 3400c66ac9dbSNicholas Bellinger * the initiator port is based on either the initiator port name 3401c66ac9dbSNicholas Bellinger * (see 3.1.71) on SCSI transport protocols where port names are 3402c66ac9dbSNicholas Bellinger * required or the initiator port identifier (see 3.1.70) on SCSI 3403c66ac9dbSNicholas Bellinger * transport protocols where port names are not required; 3404c66ac9dbSNicholas Bellinger * d) Register the reservation key specified in the SERVICE ACTION 3405c66ac9dbSNicholas Bellinger * RESERVATION KEY field; 3406c66ac9dbSNicholas Bellinger * e) Retain the reservation key specified in the SERVICE ACTION 3407c66ac9dbSNicholas Bellinger * RESERVATION KEY field and associated information; 3408c66ac9dbSNicholas Bellinger * 3409c66ac9dbSNicholas Bellinger * Also, It is not an error for a REGISTER AND MOVE service action to 3410c66ac9dbSNicholas Bellinger * register an I_T nexus that is already registered with the same 3411c66ac9dbSNicholas Bellinger * reservation key or a different reservation key. 3412c66ac9dbSNicholas Bellinger */ 3413c66ac9dbSNicholas Bellinger dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl, 3414c66ac9dbSNicholas Bellinger iport_ptr); 34156708bb27SAndy Grover if (!dest_pr_reg) { 3416de103c93SChristoph Hellwig if (core_scsi3_alloc_registration(cmd->se_dev, 3417c66ac9dbSNicholas Bellinger dest_node_acl, dest_se_deve, iport_ptr, 3418de103c93SChristoph Hellwig sa_res_key, 0, aptpl, 2, 1)) { 3419c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 3420de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3421c66ac9dbSNicholas Bellinger goto out; 3422c66ac9dbSNicholas Bellinger } 3423c66ac9dbSNicholas Bellinger dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl, 3424c66ac9dbSNicholas Bellinger iport_ptr); 3425c66ac9dbSNicholas Bellinger new_reg = 1; 3426c66ac9dbSNicholas Bellinger } 3427c66ac9dbSNicholas Bellinger /* 3428c66ac9dbSNicholas Bellinger * f) Release the persistent reservation for the persistent reservation 3429c66ac9dbSNicholas Bellinger * holder (i.e., the I_T nexus on which the 3430c66ac9dbSNicholas Bellinger */ 3431c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_release(dev, pr_res_nacl, 34326c3c9baaSNicholas Bellinger dev->dev_pr_res_holder, 0, 0); 3433c66ac9dbSNicholas Bellinger /* 3434c66ac9dbSNicholas Bellinger * g) Move the persistent reservation to the specified I_T nexus using 3435c66ac9dbSNicholas Bellinger * the same scope and type as the persistent reservation released in 3436c66ac9dbSNicholas Bellinger * item f); and 3437c66ac9dbSNicholas Bellinger */ 3438c66ac9dbSNicholas Bellinger dev->dev_pr_res_holder = dest_pr_reg; 3439c66ac9dbSNicholas Bellinger dest_pr_reg->pr_res_holder = 1; 3440c66ac9dbSNicholas Bellinger dest_pr_reg->pr_res_type = type; 3441c66ac9dbSNicholas Bellinger pr_reg->pr_res_scope = scope; 3442d2843c17SAndy Grover core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); 3443c66ac9dbSNicholas Bellinger /* 3444c66ac9dbSNicholas Bellinger * Increment PRGeneration for existing registrations.. 3445c66ac9dbSNicholas Bellinger */ 34466708bb27SAndy Grover if (!new_reg) 3447c66ac9dbSNicholas Bellinger dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++; 3448c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 3449c66ac9dbSNicholas Bellinger 34506708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE" 3451c66ac9dbSNicholas Bellinger " created new reservation holder TYPE: %s on object RTPI:" 3452c66ac9dbSNicholas Bellinger " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(), 3453c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(type), rtpi, 3454c66ac9dbSNicholas Bellinger dest_pr_reg->pr_res_generation); 34556708bb27SAndy Grover pr_debug("SPC-3 PR Successfully moved reservation from" 3456c66ac9dbSNicholas Bellinger " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n", 3457c66ac9dbSNicholas Bellinger tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname, 3458d2843c17SAndy Grover i_buf, dest_tf_ops->get_fabric_name(), 3459c66ac9dbSNicholas Bellinger dest_node_acl->initiatorname, (iport_ptr != NULL) ? 3460c66ac9dbSNicholas Bellinger iport_ptr : ""); 3461c66ac9dbSNicholas Bellinger /* 3462c66ac9dbSNicholas Bellinger * It is now safe to release configfs group dependencies for destination 3463c66ac9dbSNicholas Bellinger * of Transport ID Initiator Device/Port Identifier 3464c66ac9dbSNicholas Bellinger */ 3465c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(dest_se_deve); 3466c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 3467c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_se_tpg); 3468c66ac9dbSNicholas Bellinger /* 3469c66ac9dbSNicholas Bellinger * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T 3470c66ac9dbSNicholas Bellinger * nexus on which PERSISTENT RESERVE OUT command was received. 3471c66ac9dbSNicholas Bellinger */ 3472c66ac9dbSNicholas Bellinger if (unreg) { 3473c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 3474c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, pr_reg, NULL, 1); 3475c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 3476c66ac9dbSNicholas Bellinger } else 3477c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg); 3478c66ac9dbSNicholas Bellinger 3479459f213bSAndy Grover core_scsi3_update_and_write_aptpl(cmd->se_dev, aptpl); 3480c66ac9dbSNicholas Bellinger 34814949314cSAndy Grover transport_kunmap_data_sg(cmd); 348205d1c7c0SAndy Grover 3483c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(dest_pr_reg); 3484c66ac9dbSNicholas Bellinger return 0; 3485c66ac9dbSNicholas Bellinger out: 348605d1c7c0SAndy Grover if (buf) 34874949314cSAndy Grover transport_kunmap_data_sg(cmd); 3488c66ac9dbSNicholas Bellinger if (dest_se_deve) 3489c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(dest_se_deve); 3490c66ac9dbSNicholas Bellinger if (dest_node_acl) 3491c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 3492c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_se_tpg); 3493de103c93SChristoph Hellwig 3494de103c93SChristoph Hellwig out_put_pr_reg: 3495c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg); 3496c66ac9dbSNicholas Bellinger return ret; 3497c66ac9dbSNicholas Bellinger } 3498c66ac9dbSNicholas Bellinger 3499c66ac9dbSNicholas Bellinger static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb) 3500c66ac9dbSNicholas Bellinger { 3501c66ac9dbSNicholas Bellinger unsigned int __v1, __v2; 3502c66ac9dbSNicholas Bellinger 3503c66ac9dbSNicholas Bellinger __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3]; 3504c66ac9dbSNicholas Bellinger __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7]; 3505c66ac9dbSNicholas Bellinger 3506c66ac9dbSNicholas Bellinger return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32; 3507c66ac9dbSNicholas Bellinger } 3508c66ac9dbSNicholas Bellinger 3509c66ac9dbSNicholas Bellinger /* 3510c66ac9dbSNicholas Bellinger * See spc4r17 section 6.14 Table 170 3511c66ac9dbSNicholas Bellinger */ 3512de103c93SChristoph Hellwig sense_reason_t 3513de103c93SChristoph Hellwig target_scsi3_emulate_pr_out(struct se_cmd *cmd) 3514c66ac9dbSNicholas Bellinger { 351592404e60SNicholas Bellinger struct se_device *dev = cmd->se_dev; 3516617c0e06SChristoph Hellwig unsigned char *cdb = &cmd->t_task_cdb[0]; 351705d1c7c0SAndy Grover unsigned char *buf; 3518c66ac9dbSNicholas Bellinger u64 res_key, sa_res_key; 3519c66ac9dbSNicholas Bellinger int sa, scope, type, aptpl; 3520c66ac9dbSNicholas Bellinger int spec_i_pt = 0, all_tg_pt = 0, unreg = 0; 3521de103c93SChristoph Hellwig sense_reason_t ret; 3522617c0e06SChristoph Hellwig 3523617c0e06SChristoph Hellwig /* 3524617c0e06SChristoph Hellwig * Following spc2r20 5.5.1 Reservations overview: 3525617c0e06SChristoph Hellwig * 3526617c0e06SChristoph Hellwig * If a logical unit has been reserved by any RESERVE command and is 3527617c0e06SChristoph Hellwig * still reserved by any initiator, all PERSISTENT RESERVE IN and all 3528617c0e06SChristoph Hellwig * PERSISTENT RESERVE OUT commands shall conflict regardless of 3529617c0e06SChristoph Hellwig * initiator or service action and shall terminate with a RESERVATION 3530617c0e06SChristoph Hellwig * CONFLICT status. 3531617c0e06SChristoph Hellwig */ 35320fd97ccfSChristoph Hellwig if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) { 3533617c0e06SChristoph Hellwig pr_err("Received PERSISTENT_RESERVE CDB while legacy" 3534617c0e06SChristoph Hellwig " SPC-2 reservation is held, returning" 3535617c0e06SChristoph Hellwig " RESERVATION_CONFLICT\n"); 3536de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 3537617c0e06SChristoph Hellwig } 3538617c0e06SChristoph Hellwig 3539c66ac9dbSNicholas Bellinger /* 3540c66ac9dbSNicholas Bellinger * FIXME: A NULL struct se_session pointer means an this is not coming from 3541c66ac9dbSNicholas Bellinger * a $FABRIC_MOD's nexus, but from internal passthrough ops. 3542c66ac9dbSNicholas Bellinger */ 3543de103c93SChristoph Hellwig if (!cmd->se_sess) 3544de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3545c66ac9dbSNicholas Bellinger 3546c66ac9dbSNicholas Bellinger if (cmd->data_length < 24) { 35476708bb27SAndy Grover pr_warn("SPC-PR: Received PR OUT parameter list" 3548c66ac9dbSNicholas Bellinger " length too small: %u\n", cmd->data_length); 3549de103c93SChristoph Hellwig return TCM_INVALID_PARAMETER_LIST; 3550c66ac9dbSNicholas Bellinger } 3551de103c93SChristoph Hellwig 3552c66ac9dbSNicholas Bellinger /* 3553c66ac9dbSNicholas Bellinger * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB) 3554c66ac9dbSNicholas Bellinger */ 3555c66ac9dbSNicholas Bellinger sa = (cdb[1] & 0x1f); 3556c66ac9dbSNicholas Bellinger scope = (cdb[2] & 0xf0); 3557c66ac9dbSNicholas Bellinger type = (cdb[2] & 0x0f); 355805d1c7c0SAndy Grover 35594949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 3560de103c93SChristoph Hellwig if (!buf) 3561de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3562de103c93SChristoph Hellwig 3563c66ac9dbSNicholas Bellinger /* 3564c66ac9dbSNicholas Bellinger * From PERSISTENT_RESERVE_OUT parameter list (payload) 3565c66ac9dbSNicholas Bellinger */ 3566c66ac9dbSNicholas Bellinger res_key = core_scsi3_extract_reservation_key(&buf[0]); 3567c66ac9dbSNicholas Bellinger sa_res_key = core_scsi3_extract_reservation_key(&buf[8]); 3568c66ac9dbSNicholas Bellinger /* 3569c66ac9dbSNicholas Bellinger * REGISTER_AND_MOVE uses a different SA parameter list containing 3570c66ac9dbSNicholas Bellinger * SCSI TransportIDs. 3571c66ac9dbSNicholas Bellinger */ 3572c66ac9dbSNicholas Bellinger if (sa != PRO_REGISTER_AND_MOVE) { 3573c66ac9dbSNicholas Bellinger spec_i_pt = (buf[20] & 0x08); 3574c66ac9dbSNicholas Bellinger all_tg_pt = (buf[20] & 0x04); 3575c66ac9dbSNicholas Bellinger aptpl = (buf[20] & 0x01); 3576c66ac9dbSNicholas Bellinger } else { 3577c66ac9dbSNicholas Bellinger aptpl = (buf[17] & 0x01); 3578c66ac9dbSNicholas Bellinger unreg = (buf[17] & 0x02); 3579c66ac9dbSNicholas Bellinger } 358092404e60SNicholas Bellinger /* 358192404e60SNicholas Bellinger * If the backend device has been configured to force APTPL metadata 358292404e60SNicholas Bellinger * write-out, go ahead and propigate aptpl=1 down now. 358392404e60SNicholas Bellinger */ 358492404e60SNicholas Bellinger if (dev->dev_attrib.force_pr_aptpl) 358592404e60SNicholas Bellinger aptpl = 1; 358692404e60SNicholas Bellinger 35874949314cSAndy Grover transport_kunmap_data_sg(cmd); 358805d1c7c0SAndy Grover buf = NULL; 358905d1c7c0SAndy Grover 3590c66ac9dbSNicholas Bellinger /* 3591c66ac9dbSNicholas Bellinger * SPEC_I_PT=1 is only valid for Service action: REGISTER 3592c66ac9dbSNicholas Bellinger */ 3593de103c93SChristoph Hellwig if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER)) 3594de103c93SChristoph Hellwig return TCM_INVALID_PARAMETER_LIST; 3595d29a5b6aSChristoph Hellwig 3596c66ac9dbSNicholas Bellinger /* 3597c66ac9dbSNicholas Bellinger * From spc4r17 section 6.14: 3598c66ac9dbSNicholas Bellinger * 3599c66ac9dbSNicholas Bellinger * If the SPEC_I_PT bit is set to zero, the service action is not 3600c66ac9dbSNicholas Bellinger * REGISTER AND MOVE, and the parameter list length is not 24, then 3601c66ac9dbSNicholas Bellinger * the command shall be terminated with CHECK CONDITION status, with 3602c66ac9dbSNicholas Bellinger * the sense key set to ILLEGAL REQUEST, and the additional sense 3603c66ac9dbSNicholas Bellinger * code set to PARAMETER LIST LENGTH ERROR. 3604c66ac9dbSNicholas Bellinger */ 36056708bb27SAndy Grover if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) && 3606c66ac9dbSNicholas Bellinger (cmd->data_length != 24)) { 36076708bb27SAndy Grover pr_warn("SPC-PR: Received PR OUT illegal parameter" 3608c66ac9dbSNicholas Bellinger " list length: %u\n", cmd->data_length); 3609de103c93SChristoph Hellwig return TCM_INVALID_PARAMETER_LIST; 3610c66ac9dbSNicholas Bellinger } 3611de103c93SChristoph Hellwig 3612c66ac9dbSNicholas Bellinger /* 3613c66ac9dbSNicholas Bellinger * (core_scsi3_emulate_pro_* function parameters 3614c66ac9dbSNicholas Bellinger * are defined by spc4r17 Table 174: 3615c66ac9dbSNicholas Bellinger * PERSISTENT_RESERVE_OUT service actions and valid parameters. 3616c66ac9dbSNicholas Bellinger */ 3617c66ac9dbSNicholas Bellinger switch (sa) { 3618c66ac9dbSNicholas Bellinger case PRO_REGISTER: 3619d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_register(cmd, 362033ce6a87SAndy Grover res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER); 3621d29a5b6aSChristoph Hellwig break; 3622c66ac9dbSNicholas Bellinger case PRO_RESERVE: 3623d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key); 3624d29a5b6aSChristoph Hellwig break; 3625c66ac9dbSNicholas Bellinger case PRO_RELEASE: 3626d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key); 3627d29a5b6aSChristoph Hellwig break; 3628c66ac9dbSNicholas Bellinger case PRO_CLEAR: 3629d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_clear(cmd, res_key); 3630d29a5b6aSChristoph Hellwig break; 3631c66ac9dbSNicholas Bellinger case PRO_PREEMPT: 3632d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_preempt(cmd, type, scope, 363333ce6a87SAndy Grover res_key, sa_res_key, PREEMPT); 3634d29a5b6aSChristoph Hellwig break; 3635c66ac9dbSNicholas Bellinger case PRO_PREEMPT_AND_ABORT: 3636d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_preempt(cmd, type, scope, 363733ce6a87SAndy Grover res_key, sa_res_key, PREEMPT_AND_ABORT); 3638d29a5b6aSChristoph Hellwig break; 3639c66ac9dbSNicholas Bellinger case PRO_REGISTER_AND_IGNORE_EXISTING_KEY: 3640d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_register(cmd, 364133ce6a87SAndy Grover 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER_AND_IGNORE_EXISTING_KEY); 3642d29a5b6aSChristoph Hellwig break; 3643c66ac9dbSNicholas Bellinger case PRO_REGISTER_AND_MOVE: 3644d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key, 3645c66ac9dbSNicholas Bellinger sa_res_key, aptpl, unreg); 3646d29a5b6aSChristoph Hellwig break; 3647c66ac9dbSNicholas Bellinger default: 36486708bb27SAndy Grover pr_err("Unknown PERSISTENT_RESERVE_OUT service" 3649c66ac9dbSNicholas Bellinger " action: 0x%02x\n", cdb[1] & 0x1f); 3650de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 3651c66ac9dbSNicholas Bellinger } 3652c66ac9dbSNicholas Bellinger 36536bb35e00SChristoph Hellwig if (!ret) 36546bb35e00SChristoph Hellwig target_complete_cmd(cmd, GOOD); 3655d29a5b6aSChristoph Hellwig return ret; 3656c66ac9dbSNicholas Bellinger } 3657c66ac9dbSNicholas Bellinger 3658c66ac9dbSNicholas Bellinger /* 3659c66ac9dbSNicholas Bellinger * PERSISTENT_RESERVE_IN Service Action READ_KEYS 3660c66ac9dbSNicholas Bellinger * 3661c66ac9dbSNicholas Bellinger * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160 3662c66ac9dbSNicholas Bellinger */ 3663de103c93SChristoph Hellwig static sense_reason_t 3664de103c93SChristoph Hellwig core_scsi3_pri_read_keys(struct se_cmd *cmd) 3665c66ac9dbSNicholas Bellinger { 36660fd97ccfSChristoph Hellwig struct se_device *dev = cmd->se_dev; 3667c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg; 366805d1c7c0SAndy Grover unsigned char *buf; 3669c66ac9dbSNicholas Bellinger u32 add_len = 0, off = 8; 3670c66ac9dbSNicholas Bellinger 3671c66ac9dbSNicholas Bellinger if (cmd->data_length < 8) { 36726708bb27SAndy Grover pr_err("PRIN SA READ_KEYS SCSI Data Length: %u" 3673c66ac9dbSNicholas Bellinger " too small\n", cmd->data_length); 3674de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 3675c66ac9dbSNicholas Bellinger } 3676c66ac9dbSNicholas Bellinger 36774949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 3678de103c93SChristoph Hellwig if (!buf) 3679de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3680de103c93SChristoph Hellwig 36810fd97ccfSChristoph Hellwig buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff); 36820fd97ccfSChristoph Hellwig buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff); 36830fd97ccfSChristoph Hellwig buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff); 36840fd97ccfSChristoph Hellwig buf[3] = (dev->t10_pr.pr_generation & 0xff); 3685c66ac9dbSNicholas Bellinger 36860fd97ccfSChristoph Hellwig spin_lock(&dev->t10_pr.registration_lock); 36870fd97ccfSChristoph Hellwig list_for_each_entry(pr_reg, &dev->t10_pr.registration_list, 3688c66ac9dbSNicholas Bellinger pr_reg_list) { 3689c66ac9dbSNicholas Bellinger /* 3690c66ac9dbSNicholas Bellinger * Check for overflow of 8byte PRI READ_KEYS payload and 3691c66ac9dbSNicholas Bellinger * next reservation key list descriptor. 3692c66ac9dbSNicholas Bellinger */ 3693c66ac9dbSNicholas Bellinger if ((add_len + 8) > (cmd->data_length - 8)) 3694c66ac9dbSNicholas Bellinger break; 3695c66ac9dbSNicholas Bellinger 3696c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff); 3697c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff); 3698c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff); 3699c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff); 3700c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff); 3701c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff); 3702c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff); 3703c66ac9dbSNicholas Bellinger buf[off++] = (pr_reg->pr_res_key & 0xff); 3704c66ac9dbSNicholas Bellinger 3705c66ac9dbSNicholas Bellinger add_len += 8; 3706c66ac9dbSNicholas Bellinger } 37070fd97ccfSChristoph Hellwig spin_unlock(&dev->t10_pr.registration_lock); 3708c66ac9dbSNicholas Bellinger 3709c66ac9dbSNicholas Bellinger buf[4] = ((add_len >> 24) & 0xff); 3710c66ac9dbSNicholas Bellinger buf[5] = ((add_len >> 16) & 0xff); 3711c66ac9dbSNicholas Bellinger buf[6] = ((add_len >> 8) & 0xff); 3712c66ac9dbSNicholas Bellinger buf[7] = (add_len & 0xff); 3713c66ac9dbSNicholas Bellinger 37144949314cSAndy Grover transport_kunmap_data_sg(cmd); 371505d1c7c0SAndy Grover 3716c66ac9dbSNicholas Bellinger return 0; 3717c66ac9dbSNicholas Bellinger } 3718c66ac9dbSNicholas Bellinger 3719c66ac9dbSNicholas Bellinger /* 3720c66ac9dbSNicholas Bellinger * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION 3721c66ac9dbSNicholas Bellinger * 3722c66ac9dbSNicholas Bellinger * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162 3723c66ac9dbSNicholas Bellinger */ 3724de103c93SChristoph Hellwig static sense_reason_t 3725de103c93SChristoph Hellwig core_scsi3_pri_read_reservation(struct se_cmd *cmd) 3726c66ac9dbSNicholas Bellinger { 37270fd97ccfSChristoph Hellwig struct se_device *dev = cmd->se_dev; 3728c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg; 372905d1c7c0SAndy Grover unsigned char *buf; 3730c66ac9dbSNicholas Bellinger u64 pr_res_key; 3731c66ac9dbSNicholas Bellinger u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */ 3732c66ac9dbSNicholas Bellinger 3733c66ac9dbSNicholas Bellinger if (cmd->data_length < 8) { 37346708bb27SAndy Grover pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u" 3735c66ac9dbSNicholas Bellinger " too small\n", cmd->data_length); 3736de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 3737c66ac9dbSNicholas Bellinger } 3738c66ac9dbSNicholas Bellinger 37394949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 3740de103c93SChristoph Hellwig if (!buf) 3741de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3742de103c93SChristoph Hellwig 37430fd97ccfSChristoph Hellwig buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff); 37440fd97ccfSChristoph Hellwig buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff); 37450fd97ccfSChristoph Hellwig buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff); 37460fd97ccfSChristoph Hellwig buf[3] = (dev->t10_pr.pr_generation & 0xff); 3747c66ac9dbSNicholas Bellinger 37480fd97ccfSChristoph Hellwig spin_lock(&dev->dev_reservation_lock); 37490fd97ccfSChristoph Hellwig pr_reg = dev->dev_pr_res_holder; 3750ee1b1b9cSAndy Grover if (pr_reg) { 3751c66ac9dbSNicholas Bellinger /* 3752c66ac9dbSNicholas Bellinger * Set the hardcoded Additional Length 3753c66ac9dbSNicholas Bellinger */ 3754c66ac9dbSNicholas Bellinger buf[4] = ((add_len >> 24) & 0xff); 3755c66ac9dbSNicholas Bellinger buf[5] = ((add_len >> 16) & 0xff); 3756c66ac9dbSNicholas Bellinger buf[6] = ((add_len >> 8) & 0xff); 3757c66ac9dbSNicholas Bellinger buf[7] = (add_len & 0xff); 3758c66ac9dbSNicholas Bellinger 375905d1c7c0SAndy Grover if (cmd->data_length < 22) 376005d1c7c0SAndy Grover goto err; 376105d1c7c0SAndy Grover 3762c66ac9dbSNicholas Bellinger /* 3763c66ac9dbSNicholas Bellinger * Set the Reservation key. 3764c66ac9dbSNicholas Bellinger * 3765c66ac9dbSNicholas Bellinger * From spc4r17, section 5.7.10: 3766c66ac9dbSNicholas Bellinger * A persistent reservation holder has its reservation key 3767c66ac9dbSNicholas Bellinger * returned in the parameter data from a PERSISTENT 3768c66ac9dbSNicholas Bellinger * RESERVE IN command with READ RESERVATION service action as 3769c66ac9dbSNicholas Bellinger * follows: 3770c66ac9dbSNicholas Bellinger * a) For a persistent reservation of the type Write Exclusive 3771c66ac9dbSNicholas Bellinger * - All Registrants or Exclusive Access All Regitrants, 3772c66ac9dbSNicholas Bellinger * the reservation key shall be set to zero; or 3773c66ac9dbSNicholas Bellinger * b) For all other persistent reservation types, the 3774c66ac9dbSNicholas Bellinger * reservation key shall be set to the registered 3775c66ac9dbSNicholas Bellinger * reservation key for the I_T nexus that holds the 3776c66ac9dbSNicholas Bellinger * persistent reservation. 3777c66ac9dbSNicholas Bellinger */ 3778c66ac9dbSNicholas Bellinger if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || 3779c66ac9dbSNicholas Bellinger (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) 3780c66ac9dbSNicholas Bellinger pr_res_key = 0; 3781c66ac9dbSNicholas Bellinger else 3782c66ac9dbSNicholas Bellinger pr_res_key = pr_reg->pr_res_key; 3783c66ac9dbSNicholas Bellinger 3784c66ac9dbSNicholas Bellinger buf[8] = ((pr_res_key >> 56) & 0xff); 3785c66ac9dbSNicholas Bellinger buf[9] = ((pr_res_key >> 48) & 0xff); 3786c66ac9dbSNicholas Bellinger buf[10] = ((pr_res_key >> 40) & 0xff); 3787c66ac9dbSNicholas Bellinger buf[11] = ((pr_res_key >> 32) & 0xff); 3788c66ac9dbSNicholas Bellinger buf[12] = ((pr_res_key >> 24) & 0xff); 3789c66ac9dbSNicholas Bellinger buf[13] = ((pr_res_key >> 16) & 0xff); 3790c66ac9dbSNicholas Bellinger buf[14] = ((pr_res_key >> 8) & 0xff); 3791c66ac9dbSNicholas Bellinger buf[15] = (pr_res_key & 0xff); 3792c66ac9dbSNicholas Bellinger /* 3793c66ac9dbSNicholas Bellinger * Set the SCOPE and TYPE 3794c66ac9dbSNicholas Bellinger */ 3795c66ac9dbSNicholas Bellinger buf[21] = (pr_reg->pr_res_scope & 0xf0) | 3796c66ac9dbSNicholas Bellinger (pr_reg->pr_res_type & 0x0f); 3797c66ac9dbSNicholas Bellinger } 379805d1c7c0SAndy Grover 379905d1c7c0SAndy Grover err: 38000fd97ccfSChristoph Hellwig spin_unlock(&dev->dev_reservation_lock); 38014949314cSAndy Grover transport_kunmap_data_sg(cmd); 3802c66ac9dbSNicholas Bellinger 3803c66ac9dbSNicholas Bellinger return 0; 3804c66ac9dbSNicholas Bellinger } 3805c66ac9dbSNicholas Bellinger 3806c66ac9dbSNicholas Bellinger /* 3807c66ac9dbSNicholas Bellinger * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES 3808c66ac9dbSNicholas Bellinger * 3809c66ac9dbSNicholas Bellinger * See spc4r17 section 6.13.4 Table 165 3810c66ac9dbSNicholas Bellinger */ 3811de103c93SChristoph Hellwig static sense_reason_t 3812de103c93SChristoph Hellwig core_scsi3_pri_report_capabilities(struct se_cmd *cmd) 3813c66ac9dbSNicholas Bellinger { 38145951146dSAndy Grover struct se_device *dev = cmd->se_dev; 38150fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 381605d1c7c0SAndy Grover unsigned char *buf; 3817c66ac9dbSNicholas Bellinger u16 add_len = 8; /* Hardcoded to 8. */ 3818c66ac9dbSNicholas Bellinger 3819c66ac9dbSNicholas Bellinger if (cmd->data_length < 6) { 38206708bb27SAndy Grover pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:" 3821c66ac9dbSNicholas Bellinger " %u too small\n", cmd->data_length); 3822de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 3823c66ac9dbSNicholas Bellinger } 3824c66ac9dbSNicholas Bellinger 38254949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 3826de103c93SChristoph Hellwig if (!buf) 3827de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 382805d1c7c0SAndy Grover 3829c435285dSJoern Engel buf[0] = ((add_len >> 8) & 0xff); 3830c66ac9dbSNicholas Bellinger buf[1] = (add_len & 0xff); 3831c66ac9dbSNicholas Bellinger buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */ 3832c66ac9dbSNicholas Bellinger buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */ 3833c66ac9dbSNicholas Bellinger buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */ 3834c66ac9dbSNicholas Bellinger buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */ 3835c66ac9dbSNicholas Bellinger /* 3836c66ac9dbSNicholas Bellinger * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so 3837c66ac9dbSNicholas Bellinger * set the TMV: Task Mask Valid bit. 3838c66ac9dbSNicholas Bellinger */ 3839c66ac9dbSNicholas Bellinger buf[3] |= 0x80; 3840c66ac9dbSNicholas Bellinger /* 3841c66ac9dbSNicholas Bellinger * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166 3842c66ac9dbSNicholas Bellinger */ 3843c66ac9dbSNicholas Bellinger buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */ 3844c66ac9dbSNicholas Bellinger /* 3845c66ac9dbSNicholas Bellinger * PTPL_A: Persistence across Target Power Loss Active bit 3846c66ac9dbSNicholas Bellinger */ 3847c66ac9dbSNicholas Bellinger if (pr_tmpl->pr_aptpl_active) 3848c66ac9dbSNicholas Bellinger buf[3] |= 0x01; 3849c66ac9dbSNicholas Bellinger /* 3850c66ac9dbSNicholas Bellinger * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167 3851c66ac9dbSNicholas Bellinger */ 3852c66ac9dbSNicholas Bellinger buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */ 3853c66ac9dbSNicholas Bellinger buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */ 3854c66ac9dbSNicholas Bellinger buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */ 3855c66ac9dbSNicholas Bellinger buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */ 3856c66ac9dbSNicholas Bellinger buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */ 3857c66ac9dbSNicholas Bellinger buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */ 3858c66ac9dbSNicholas Bellinger 38594949314cSAndy Grover transport_kunmap_data_sg(cmd); 386005d1c7c0SAndy Grover 3861c66ac9dbSNicholas Bellinger return 0; 3862c66ac9dbSNicholas Bellinger } 3863c66ac9dbSNicholas Bellinger 3864c66ac9dbSNicholas Bellinger /* 3865c66ac9dbSNicholas Bellinger * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS 3866c66ac9dbSNicholas Bellinger * 3867c66ac9dbSNicholas Bellinger * See spc4r17 section 6.13.5 Table 168 and 169 3868c66ac9dbSNicholas Bellinger */ 3869de103c93SChristoph Hellwig static sense_reason_t 3870de103c93SChristoph Hellwig core_scsi3_pri_read_full_status(struct se_cmd *cmd) 3871c66ac9dbSNicholas Bellinger { 38720fd97ccfSChristoph Hellwig struct se_device *dev = cmd->se_dev; 3873c66ac9dbSNicholas Bellinger struct se_node_acl *se_nacl; 3874c66ac9dbSNicholas Bellinger struct se_portal_group *se_tpg; 3875c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp; 38760fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 387705d1c7c0SAndy Grover unsigned char *buf; 3878c66ac9dbSNicholas Bellinger u32 add_desc_len = 0, add_len = 0, desc_len, exp_desc_len; 3879c66ac9dbSNicholas Bellinger u32 off = 8; /* off into first Full Status descriptor */ 3880d16ca7c5SNicholas Bellinger int format_code = 0, pr_res_type = 0, pr_res_scope = 0; 3881d16ca7c5SNicholas Bellinger bool all_reg = false; 3882c66ac9dbSNicholas Bellinger 3883c66ac9dbSNicholas Bellinger if (cmd->data_length < 8) { 38846708bb27SAndy Grover pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u" 3885c66ac9dbSNicholas Bellinger " too small\n", cmd->data_length); 3886de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 3887c66ac9dbSNicholas Bellinger } 3888c66ac9dbSNicholas Bellinger 38894949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 3890de103c93SChristoph Hellwig if (!buf) 3891de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 389205d1c7c0SAndy Grover 38930fd97ccfSChristoph Hellwig buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff); 38940fd97ccfSChristoph Hellwig buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff); 38950fd97ccfSChristoph Hellwig buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff); 38960fd97ccfSChristoph Hellwig buf[3] = (dev->t10_pr.pr_generation & 0xff); 3897c66ac9dbSNicholas Bellinger 3898d16ca7c5SNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 3899d16ca7c5SNicholas Bellinger if (dev->dev_pr_res_holder) { 3900d16ca7c5SNicholas Bellinger struct t10_pr_registration *pr_holder = dev->dev_pr_res_holder; 3901d16ca7c5SNicholas Bellinger 3902d16ca7c5SNicholas Bellinger if (pr_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG || 3903d16ca7c5SNicholas Bellinger pr_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG) { 3904d16ca7c5SNicholas Bellinger all_reg = true; 3905d16ca7c5SNicholas Bellinger pr_res_type = pr_holder->pr_res_type; 3906d16ca7c5SNicholas Bellinger pr_res_scope = pr_holder->pr_res_scope; 3907d16ca7c5SNicholas Bellinger } 3908d16ca7c5SNicholas Bellinger } 3909d16ca7c5SNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 3910d16ca7c5SNicholas Bellinger 3911c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 3912c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 3913c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 3914c66ac9dbSNicholas Bellinger 3915c66ac9dbSNicholas Bellinger se_nacl = pr_reg->pr_reg_nacl; 3916c66ac9dbSNicholas Bellinger se_tpg = pr_reg->pr_reg_nacl->se_tpg; 3917c66ac9dbSNicholas Bellinger add_desc_len = 0; 3918c66ac9dbSNicholas Bellinger 391933940d09SJoern Engel atomic_inc_mb(&pr_reg->pr_res_holders); 3920c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 3921c66ac9dbSNicholas Bellinger /* 3922c66ac9dbSNicholas Bellinger * Determine expected length of $FABRIC_MOD specific 3923c66ac9dbSNicholas Bellinger * TransportID full status descriptor.. 3924c66ac9dbSNicholas Bellinger */ 3925e3d6f909SAndy Grover exp_desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id_len( 3926c66ac9dbSNicholas Bellinger se_tpg, se_nacl, pr_reg, &format_code); 3927c66ac9dbSNicholas Bellinger 3928c66ac9dbSNicholas Bellinger if ((exp_desc_len + add_len) > cmd->data_length) { 39296708bb27SAndy Grover pr_warn("SPC-3 PRIN READ_FULL_STATUS ran" 3930c66ac9dbSNicholas Bellinger " out of buffer: %d\n", cmd->data_length); 3931c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 393233940d09SJoern Engel atomic_dec_mb(&pr_reg->pr_res_holders); 3933c66ac9dbSNicholas Bellinger break; 3934c66ac9dbSNicholas Bellinger } 3935c66ac9dbSNicholas Bellinger /* 3936c66ac9dbSNicholas Bellinger * Set RESERVATION KEY 3937c66ac9dbSNicholas Bellinger */ 3938c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff); 3939c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff); 3940c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff); 3941c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff); 3942c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff); 3943c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff); 3944c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff); 3945c66ac9dbSNicholas Bellinger buf[off++] = (pr_reg->pr_res_key & 0xff); 3946c66ac9dbSNicholas Bellinger off += 4; /* Skip Over Reserved area */ 3947c66ac9dbSNicholas Bellinger 3948c66ac9dbSNicholas Bellinger /* 3949c66ac9dbSNicholas Bellinger * Set ALL_TG_PT bit if PROUT SA REGISTER had this set. 3950c66ac9dbSNicholas Bellinger */ 3951c66ac9dbSNicholas Bellinger if (pr_reg->pr_reg_all_tg_pt) 3952c66ac9dbSNicholas Bellinger buf[off] = 0x02; 3953c66ac9dbSNicholas Bellinger /* 3954c66ac9dbSNicholas Bellinger * The struct se_lun pointer will be present for the 3955c66ac9dbSNicholas Bellinger * reservation holder for PR_HOLDER bit. 3956c66ac9dbSNicholas Bellinger * 3957c66ac9dbSNicholas Bellinger * Also, if this registration is the reservation 3958d16ca7c5SNicholas Bellinger * holder or there is an All Registrants reservation 3959d16ca7c5SNicholas Bellinger * active, fill in SCOPE and TYPE in the next byte. 3960c66ac9dbSNicholas Bellinger */ 3961c66ac9dbSNicholas Bellinger if (pr_reg->pr_res_holder) { 3962c66ac9dbSNicholas Bellinger buf[off++] |= 0x01; 3963c66ac9dbSNicholas Bellinger buf[off++] = (pr_reg->pr_res_scope & 0xf0) | 3964c66ac9dbSNicholas Bellinger (pr_reg->pr_res_type & 0x0f); 3965d16ca7c5SNicholas Bellinger } else if (all_reg) { 3966d16ca7c5SNicholas Bellinger buf[off++] |= 0x01; 3967d16ca7c5SNicholas Bellinger buf[off++] = (pr_res_scope & 0xf0) | 3968d16ca7c5SNicholas Bellinger (pr_res_type & 0x0f); 3969d16ca7c5SNicholas Bellinger } else { 3970c66ac9dbSNicholas Bellinger off += 2; 3971d16ca7c5SNicholas Bellinger } 3972c66ac9dbSNicholas Bellinger 3973c66ac9dbSNicholas Bellinger off += 4; /* Skip over reserved area */ 3974c66ac9dbSNicholas Bellinger /* 3975c66ac9dbSNicholas Bellinger * From spc4r17 6.3.15: 3976c66ac9dbSNicholas Bellinger * 3977c66ac9dbSNicholas Bellinger * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT 3978c66ac9dbSNicholas Bellinger * IDENTIFIER field contains the relative port identifier (see 3979c66ac9dbSNicholas Bellinger * 3.1.120) of the target port that is part of the I_T nexus 3980c66ac9dbSNicholas Bellinger * described by this full status descriptor. If the ALL_TG_PT 3981c66ac9dbSNicholas Bellinger * bit is set to one, the contents of the RELATIVE TARGET PORT 3982c66ac9dbSNicholas Bellinger * IDENTIFIER field are not defined by this standard. 3983c66ac9dbSNicholas Bellinger */ 39846708bb27SAndy Grover if (!pr_reg->pr_reg_all_tg_pt) { 3985c66ac9dbSNicholas Bellinger struct se_port *port = pr_reg->pr_reg_tg_pt_lun->lun_sep; 3986c66ac9dbSNicholas Bellinger 3987c66ac9dbSNicholas Bellinger buf[off++] = ((port->sep_rtpi >> 8) & 0xff); 3988c66ac9dbSNicholas Bellinger buf[off++] = (port->sep_rtpi & 0xff); 3989c66ac9dbSNicholas Bellinger } else 399035d1efe8SMasanari Iida off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */ 3991c66ac9dbSNicholas Bellinger 3992c66ac9dbSNicholas Bellinger /* 3993c66ac9dbSNicholas Bellinger * Now, have the $FABRIC_MOD fill in the protocol identifier 3994c66ac9dbSNicholas Bellinger */ 3995e3d6f909SAndy Grover desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id(se_tpg, 3996c66ac9dbSNicholas Bellinger se_nacl, pr_reg, &format_code, &buf[off+4]); 3997c66ac9dbSNicholas Bellinger 3998c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 399933940d09SJoern Engel atomic_dec_mb(&pr_reg->pr_res_holders); 4000c66ac9dbSNicholas Bellinger /* 4001c66ac9dbSNicholas Bellinger * Set the ADDITIONAL DESCRIPTOR LENGTH 4002c66ac9dbSNicholas Bellinger */ 4003c66ac9dbSNicholas Bellinger buf[off++] = ((desc_len >> 24) & 0xff); 4004c66ac9dbSNicholas Bellinger buf[off++] = ((desc_len >> 16) & 0xff); 4005c66ac9dbSNicholas Bellinger buf[off++] = ((desc_len >> 8) & 0xff); 4006c66ac9dbSNicholas Bellinger buf[off++] = (desc_len & 0xff); 4007c66ac9dbSNicholas Bellinger /* 4008c66ac9dbSNicholas Bellinger * Size of full desctipor header minus TransportID 4009c66ac9dbSNicholas Bellinger * containing $FABRIC_MOD specific) initiator device/port 4010c66ac9dbSNicholas Bellinger * WWN information. 4011c66ac9dbSNicholas Bellinger * 4012c66ac9dbSNicholas Bellinger * See spc4r17 Section 6.13.5 Table 169 4013c66ac9dbSNicholas Bellinger */ 4014c66ac9dbSNicholas Bellinger add_desc_len = (24 + desc_len); 4015c66ac9dbSNicholas Bellinger 4016c66ac9dbSNicholas Bellinger off += desc_len; 4017c66ac9dbSNicholas Bellinger add_len += add_desc_len; 4018c66ac9dbSNicholas Bellinger } 4019c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 4020c66ac9dbSNicholas Bellinger /* 4021c66ac9dbSNicholas Bellinger * Set ADDITIONAL_LENGTH 4022c66ac9dbSNicholas Bellinger */ 4023c66ac9dbSNicholas Bellinger buf[4] = ((add_len >> 24) & 0xff); 4024c66ac9dbSNicholas Bellinger buf[5] = ((add_len >> 16) & 0xff); 4025c66ac9dbSNicholas Bellinger buf[6] = ((add_len >> 8) & 0xff); 4026c66ac9dbSNicholas Bellinger buf[7] = (add_len & 0xff); 4027c66ac9dbSNicholas Bellinger 40284949314cSAndy Grover transport_kunmap_data_sg(cmd); 402905d1c7c0SAndy Grover 4030c66ac9dbSNicholas Bellinger return 0; 4031c66ac9dbSNicholas Bellinger } 4032c66ac9dbSNicholas Bellinger 4033de103c93SChristoph Hellwig sense_reason_t 4034de103c93SChristoph Hellwig target_scsi3_emulate_pr_in(struct se_cmd *cmd) 4035c66ac9dbSNicholas Bellinger { 4036de103c93SChristoph Hellwig sense_reason_t ret; 4037e76a35d6SChristoph Hellwig 4038617c0e06SChristoph Hellwig /* 4039617c0e06SChristoph Hellwig * Following spc2r20 5.5.1 Reservations overview: 4040617c0e06SChristoph Hellwig * 4041617c0e06SChristoph Hellwig * If a logical unit has been reserved by any RESERVE command and is 4042617c0e06SChristoph Hellwig * still reserved by any initiator, all PERSISTENT RESERVE IN and all 4043617c0e06SChristoph Hellwig * PERSISTENT RESERVE OUT commands shall conflict regardless of 4044617c0e06SChristoph Hellwig * initiator or service action and shall terminate with a RESERVATION 4045617c0e06SChristoph Hellwig * CONFLICT status. 4046617c0e06SChristoph Hellwig */ 40470fd97ccfSChristoph Hellwig if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) { 4048617c0e06SChristoph Hellwig pr_err("Received PERSISTENT_RESERVE CDB while legacy" 4049617c0e06SChristoph Hellwig " SPC-2 reservation is held, returning" 4050617c0e06SChristoph Hellwig " RESERVATION_CONFLICT\n"); 4051de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 4052617c0e06SChristoph Hellwig } 4053617c0e06SChristoph Hellwig 4054617c0e06SChristoph Hellwig switch (cmd->t_task_cdb[1] & 0x1f) { 4055c66ac9dbSNicholas Bellinger case PRI_READ_KEYS: 4056d29a5b6aSChristoph Hellwig ret = core_scsi3_pri_read_keys(cmd); 4057d29a5b6aSChristoph Hellwig break; 4058c66ac9dbSNicholas Bellinger case PRI_READ_RESERVATION: 4059d29a5b6aSChristoph Hellwig ret = core_scsi3_pri_read_reservation(cmd); 4060d29a5b6aSChristoph Hellwig break; 4061c66ac9dbSNicholas Bellinger case PRI_REPORT_CAPABILITIES: 4062d29a5b6aSChristoph Hellwig ret = core_scsi3_pri_report_capabilities(cmd); 4063d29a5b6aSChristoph Hellwig break; 4064c66ac9dbSNicholas Bellinger case PRI_READ_FULL_STATUS: 4065d29a5b6aSChristoph Hellwig ret = core_scsi3_pri_read_full_status(cmd); 4066d29a5b6aSChristoph Hellwig break; 4067c66ac9dbSNicholas Bellinger default: 40686708bb27SAndy Grover pr_err("Unknown PERSISTENT_RESERVE_IN service" 4069617c0e06SChristoph Hellwig " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f); 4070de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 4071c66ac9dbSNicholas Bellinger } 4072d29a5b6aSChristoph Hellwig 40736bb35e00SChristoph Hellwig if (!ret) 40746bb35e00SChristoph Hellwig target_complete_cmd(cmd, GOOD); 4075d29a5b6aSChristoph Hellwig return ret; 4076c66ac9dbSNicholas Bellinger } 4077c66ac9dbSNicholas Bellinger 4078de103c93SChristoph Hellwig sense_reason_t 4079de103c93SChristoph Hellwig target_check_reservation(struct se_cmd *cmd) 4080c66ac9dbSNicholas Bellinger { 4081d977f437SChristoph Hellwig struct se_device *dev = cmd->se_dev; 4082de103c93SChristoph Hellwig sense_reason_t ret; 4083d977f437SChristoph Hellwig 4084d977f437SChristoph Hellwig if (!cmd->se_sess) 4085c66ac9dbSNicholas Bellinger return 0; 4086d977f437SChristoph Hellwig if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE) 4087c66ac9dbSNicholas Bellinger return 0; 4088d977f437SChristoph Hellwig if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) 4089d977f437SChristoph Hellwig return 0; 4090c66ac9dbSNicholas Bellinger 4091d977f437SChristoph Hellwig spin_lock(&dev->dev_reservation_lock); 4092d977f437SChristoph Hellwig if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) 4093d977f437SChristoph Hellwig ret = target_scsi2_reservation_check(cmd); 4094d977f437SChristoph Hellwig else 4095d977f437SChristoph Hellwig ret = target_scsi3_pr_reservation_check(cmd); 4096d977f437SChristoph Hellwig spin_unlock(&dev->dev_reservation_lock); 40970fd97ccfSChristoph Hellwig 4098d977f437SChristoph Hellwig return ret; 4099c66ac9dbSNicholas Bellinger } 4100