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 39e26d99aeSChristoph Hellwig #include "target_core_internal.h" 40c66ac9dbSNicholas Bellinger #include "target_core_pr.h" 41c66ac9dbSNicholas Bellinger #include "target_core_ua.h" 42c66ac9dbSNicholas Bellinger 43c66ac9dbSNicholas Bellinger /* 44c66ac9dbSNicholas Bellinger * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT) 45c66ac9dbSNicholas Bellinger */ 46c66ac9dbSNicholas Bellinger struct pr_transport_id_holder { 47c66ac9dbSNicholas Bellinger struct t10_pr_registration *dest_pr_reg; 48c66ac9dbSNicholas Bellinger struct se_portal_group *dest_tpg; 49c66ac9dbSNicholas Bellinger struct se_node_acl *dest_node_acl; 5079dc9c9eSNicholas Bellinger struct se_dev_entry *dest_se_deve; 51c66ac9dbSNicholas Bellinger struct list_head dest_list; 52c66ac9dbSNicholas Bellinger }; 53c66ac9dbSNicholas Bellinger 54d2843c17SAndy Grover void core_pr_dump_initiator_port( 55c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, 56c66ac9dbSNicholas Bellinger char *buf, 57c66ac9dbSNicholas Bellinger u32 size) 58c66ac9dbSNicholas Bellinger { 596708bb27SAndy Grover if (!pr_reg->isid_present_at_reg) 60d2843c17SAndy Grover buf[0] = '\0'; 61c66ac9dbSNicholas Bellinger 62d2843c17SAndy Grover snprintf(buf, size, ",i,0x%s", pr_reg->pr_reg_isid); 63c66ac9dbSNicholas Bellinger } 64c66ac9dbSNicholas Bellinger 6533ce6a87SAndy Grover enum register_type { 6633ce6a87SAndy Grover REGISTER, 6733ce6a87SAndy Grover REGISTER_AND_IGNORE_EXISTING_KEY, 6833ce6a87SAndy Grover REGISTER_AND_MOVE, 6933ce6a87SAndy Grover }; 7033ce6a87SAndy Grover 7133ce6a87SAndy Grover enum preempt_type { 7233ce6a87SAndy Grover PREEMPT, 7333ce6a87SAndy Grover PREEMPT_AND_ABORT, 7433ce6a87SAndy Grover }; 7533ce6a87SAndy Grover 76c66ac9dbSNicholas Bellinger static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *, 776c3c9baaSNicholas Bellinger struct t10_pr_registration *, int, int); 78c66ac9dbSNicholas Bellinger 79e673dc92SIlias Tsitsimpis static int is_reservation_holder( 80e673dc92SIlias Tsitsimpis struct t10_pr_registration *pr_res_holder, 81e673dc92SIlias Tsitsimpis struct t10_pr_registration *pr_reg) 82e673dc92SIlias Tsitsimpis { 83e673dc92SIlias Tsitsimpis int pr_res_type; 84e673dc92SIlias Tsitsimpis 85e673dc92SIlias Tsitsimpis if (pr_res_holder) { 86e673dc92SIlias Tsitsimpis pr_res_type = pr_res_holder->pr_res_type; 87e673dc92SIlias Tsitsimpis 88e673dc92SIlias Tsitsimpis return pr_res_holder == pr_reg || 89e673dc92SIlias Tsitsimpis pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG || 90e673dc92SIlias Tsitsimpis pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG; 91e673dc92SIlias Tsitsimpis } 92e673dc92SIlias Tsitsimpis return 0; 93e673dc92SIlias Tsitsimpis } 94e673dc92SIlias Tsitsimpis 95de103c93SChristoph Hellwig static sense_reason_t 96de103c93SChristoph Hellwig target_scsi2_reservation_check(struct se_cmd *cmd) 97c66ac9dbSNicholas Bellinger { 98d977f437SChristoph Hellwig struct se_device *dev = cmd->se_dev; 99d977f437SChristoph Hellwig struct se_session *sess = cmd->se_sess; 100d977f437SChristoph Hellwig 101d977f437SChristoph Hellwig switch (cmd->t_task_cdb[0]) { 102c66ac9dbSNicholas Bellinger case INQUIRY: 103c66ac9dbSNicholas Bellinger case RELEASE: 104c66ac9dbSNicholas Bellinger case RELEASE_10: 105c66ac9dbSNicholas Bellinger return 0; 106c66ac9dbSNicholas Bellinger default: 107d977f437SChristoph Hellwig break; 108c66ac9dbSNicholas Bellinger } 109c66ac9dbSNicholas Bellinger 110d977f437SChristoph Hellwig if (!dev->dev_reserved_node_acl || !sess) 111c66ac9dbSNicholas Bellinger return 0; 112c66ac9dbSNicholas Bellinger 113d977f437SChristoph Hellwig if (dev->dev_reserved_node_acl != sess->se_node_acl) 114de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 115c66ac9dbSNicholas Bellinger 116d977f437SChristoph Hellwig if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) { 117d977f437SChristoph Hellwig if (dev->dev_res_bin_isid != sess->sess_bin_isid) 118de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 119d977f437SChristoph Hellwig } 120d977f437SChristoph Hellwig 121d977f437SChristoph Hellwig return 0; 122c66ac9dbSNicholas Bellinger } 123c66ac9dbSNicholas Bellinger 124eacac00cSChristoph Hellwig static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *, 125eacac00cSChristoph Hellwig struct se_node_acl *, struct se_session *); 126eacac00cSChristoph Hellwig static void core_scsi3_put_pr_reg(struct t10_pr_registration *); 127eacac00cSChristoph Hellwig 128087a03b3SNicholas Bellinger static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd) 129eacac00cSChristoph Hellwig { 130eacac00cSChristoph Hellwig struct se_session *se_sess = cmd->se_sess; 1310fd97ccfSChristoph Hellwig struct se_device *dev = cmd->se_dev; 132eacac00cSChristoph Hellwig struct t10_pr_registration *pr_reg; 1330fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 134eacac00cSChristoph Hellwig int conflict = 0; 135eacac00cSChristoph Hellwig 136eacac00cSChristoph Hellwig pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl, 137eacac00cSChristoph Hellwig se_sess); 138eacac00cSChristoph Hellwig if (pr_reg) { 139eacac00cSChristoph Hellwig /* 140eacac00cSChristoph Hellwig * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE 141eacac00cSChristoph Hellwig * behavior 142eacac00cSChristoph Hellwig * 143eacac00cSChristoph Hellwig * A RESERVE(6) or RESERVE(10) command shall complete with GOOD 144eacac00cSChristoph Hellwig * status, but no reservation shall be established and the 145eacac00cSChristoph Hellwig * persistent reservation shall not be changed, if the command 146eacac00cSChristoph Hellwig * is received from a) and b) below. 147eacac00cSChristoph Hellwig * 148eacac00cSChristoph Hellwig * A RELEASE(6) or RELEASE(10) command shall complete with GOOD 149eacac00cSChristoph Hellwig * status, but the persistent reservation shall not be released, 150eacac00cSChristoph Hellwig * if the command is received from a) and b) 151eacac00cSChristoph Hellwig * 152eacac00cSChristoph Hellwig * a) An I_T nexus that is a persistent reservation holder; or 153eacac00cSChristoph Hellwig * b) An I_T nexus that is registered if a registrants only or 154eacac00cSChristoph Hellwig * all registrants type persistent reservation is present. 155eacac00cSChristoph Hellwig * 156eacac00cSChristoph Hellwig * In all other cases, a RESERVE(6) command, RESERVE(10) command, 157eacac00cSChristoph Hellwig * RELEASE(6) command, or RELEASE(10) command shall be processed 158eacac00cSChristoph Hellwig * as defined in SPC-2. 159eacac00cSChristoph Hellwig */ 160eacac00cSChristoph Hellwig if (pr_reg->pr_res_holder) { 161eacac00cSChristoph Hellwig core_scsi3_put_pr_reg(pr_reg); 162087a03b3SNicholas Bellinger return 1; 163eacac00cSChristoph Hellwig } 164eacac00cSChristoph Hellwig if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) || 165eacac00cSChristoph Hellwig (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) || 166eacac00cSChristoph Hellwig (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || 167eacac00cSChristoph Hellwig (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) { 168eacac00cSChristoph Hellwig core_scsi3_put_pr_reg(pr_reg); 169087a03b3SNicholas Bellinger return 1; 170eacac00cSChristoph Hellwig } 171eacac00cSChristoph Hellwig core_scsi3_put_pr_reg(pr_reg); 172eacac00cSChristoph Hellwig conflict = 1; 173eacac00cSChristoph Hellwig } else { 174eacac00cSChristoph Hellwig /* 175eacac00cSChristoph Hellwig * Following spc2r20 5.5.1 Reservations overview: 176eacac00cSChristoph Hellwig * 177eacac00cSChristoph Hellwig * If a logical unit has executed a PERSISTENT RESERVE OUT 178eacac00cSChristoph Hellwig * command with the REGISTER or the REGISTER AND IGNORE 179eacac00cSChristoph Hellwig * EXISTING KEY service action and is still registered by any 180eacac00cSChristoph Hellwig * initiator, all RESERVE commands and all RELEASE commands 181eacac00cSChristoph Hellwig * regardless of initiator shall conflict and shall terminate 182eacac00cSChristoph Hellwig * with a RESERVATION CONFLICT status. 183eacac00cSChristoph Hellwig */ 184eacac00cSChristoph Hellwig spin_lock(&pr_tmpl->registration_lock); 185eacac00cSChristoph Hellwig conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1; 186eacac00cSChristoph Hellwig spin_unlock(&pr_tmpl->registration_lock); 187eacac00cSChristoph Hellwig } 188eacac00cSChristoph Hellwig 189eacac00cSChristoph Hellwig if (conflict) { 190eacac00cSChristoph Hellwig pr_err("Received legacy SPC-2 RESERVE/RELEASE" 191eacac00cSChristoph Hellwig " while active SPC-3 registrations exist," 192eacac00cSChristoph Hellwig " returning RESERVATION_CONFLICT\n"); 193087a03b3SNicholas Bellinger return -EBUSY; 194eacac00cSChristoph Hellwig } 195eacac00cSChristoph Hellwig 196087a03b3SNicholas Bellinger return 0; 197eacac00cSChristoph Hellwig } 198eacac00cSChristoph Hellwig 199de103c93SChristoph Hellwig sense_reason_t 200de103c93SChristoph Hellwig target_scsi2_reservation_release(struct se_cmd *cmd) 201c66ac9dbSNicholas Bellinger { 202c66ac9dbSNicholas Bellinger struct se_device *dev = cmd->se_dev; 203c66ac9dbSNicholas Bellinger struct se_session *sess = cmd->se_sess; 204609234e3SWei Yongjun struct se_portal_group *tpg; 205de103c93SChristoph Hellwig int rc; 206c66ac9dbSNicholas Bellinger 207609234e3SWei Yongjun if (!sess || !sess->se_tpg) 208d29a5b6aSChristoph Hellwig goto out; 209087a03b3SNicholas Bellinger rc = target_check_scsi2_reservation_conflict(cmd); 210087a03b3SNicholas Bellinger if (rc == 1) 211d29a5b6aSChristoph Hellwig goto out; 212de103c93SChristoph Hellwig if (rc < 0) 213de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 214c66ac9dbSNicholas Bellinger 215c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 216d29a5b6aSChristoph Hellwig if (!dev->dev_reserved_node_acl || !sess) 217d29a5b6aSChristoph Hellwig goto out_unlock; 218c66ac9dbSNicholas Bellinger 219d29a5b6aSChristoph Hellwig if (dev->dev_reserved_node_acl != sess->se_node_acl) 220d29a5b6aSChristoph Hellwig goto out_unlock; 221d29a5b6aSChristoph Hellwig 222edc318d9SBernhard Kohl if (dev->dev_res_bin_isid != sess->sess_bin_isid) 223edc318d9SBernhard Kohl goto out_unlock; 224edc318d9SBernhard Kohl 225c66ac9dbSNicholas Bellinger dev->dev_reserved_node_acl = NULL; 2260fd97ccfSChristoph Hellwig dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS; 2270fd97ccfSChristoph Hellwig if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) { 228c66ac9dbSNicholas Bellinger dev->dev_res_bin_isid = 0; 2290fd97ccfSChristoph Hellwig dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS_WITH_ISID; 230c66ac9dbSNicholas Bellinger } 231609234e3SWei Yongjun tpg = sess->se_tpg; 232f2d30680SHannes Reinecke pr_debug("SCSI-2 Released reservation for %s LUN: %llu ->" 233f2d30680SHannes Reinecke " MAPPED LUN: %llu for %s\n", 234f2d30680SHannes Reinecke tpg->se_tpg_tfo->get_fabric_name(), 23529a05deeSNicholas Bellinger cmd->se_lun->unpacked_lun, cmd->orig_fe_lun, 236c66ac9dbSNicholas Bellinger sess->se_node_acl->initiatorname); 237c66ac9dbSNicholas Bellinger 238d29a5b6aSChristoph Hellwig out_unlock: 239d29a5b6aSChristoph Hellwig spin_unlock(&dev->dev_reservation_lock); 240d29a5b6aSChristoph Hellwig out: 2416bb35e00SChristoph Hellwig target_complete_cmd(cmd, GOOD); 242de103c93SChristoph Hellwig return 0; 243c66ac9dbSNicholas Bellinger } 244c66ac9dbSNicholas Bellinger 245de103c93SChristoph Hellwig sense_reason_t 246de103c93SChristoph Hellwig target_scsi2_reservation_reserve(struct se_cmd *cmd) 247c66ac9dbSNicholas Bellinger { 248c66ac9dbSNicholas Bellinger struct se_device *dev = cmd->se_dev; 249c66ac9dbSNicholas Bellinger struct se_session *sess = cmd->se_sess; 250609234e3SWei Yongjun struct se_portal_group *tpg; 251de103c93SChristoph Hellwig sense_reason_t ret = 0; 252de103c93SChristoph Hellwig int rc; 253c66ac9dbSNicholas Bellinger 254a1d8b49aSAndy Grover if ((cmd->t_task_cdb[1] & 0x01) && 255a1d8b49aSAndy Grover (cmd->t_task_cdb[1] & 0x02)) { 2566708bb27SAndy Grover pr_err("LongIO and Obselete Bits set, returning" 257c66ac9dbSNicholas Bellinger " ILLEGAL_REQUEST\n"); 258de103c93SChristoph Hellwig return TCM_UNSUPPORTED_SCSI_OPCODE; 259c66ac9dbSNicholas Bellinger } 260c66ac9dbSNicholas Bellinger /* 261c66ac9dbSNicholas Bellinger * This is currently the case for target_core_mod passthrough struct se_cmd 262c66ac9dbSNicholas Bellinger * ops 263c66ac9dbSNicholas Bellinger */ 264609234e3SWei Yongjun if (!sess || !sess->se_tpg) 265d29a5b6aSChristoph Hellwig goto out; 266087a03b3SNicholas Bellinger rc = target_check_scsi2_reservation_conflict(cmd); 267087a03b3SNicholas Bellinger if (rc == 1) 268d29a5b6aSChristoph Hellwig goto out; 269c66ac9dbSNicholas Bellinger 270de103c93SChristoph Hellwig if (rc < 0) 271de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 272de103c93SChristoph Hellwig 273609234e3SWei Yongjun tpg = sess->se_tpg; 274c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 275c66ac9dbSNicholas Bellinger if (dev->dev_reserved_node_acl && 276c66ac9dbSNicholas Bellinger (dev->dev_reserved_node_acl != sess->se_node_acl)) { 2776708bb27SAndy Grover pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n", 278e3d6f909SAndy Grover tpg->se_tpg_tfo->get_fabric_name()); 279f2d30680SHannes Reinecke pr_err("Original reserver LUN: %llu %s\n", 280e3d6f909SAndy Grover cmd->se_lun->unpacked_lun, 281c66ac9dbSNicholas Bellinger dev->dev_reserved_node_acl->initiatorname); 282f2d30680SHannes Reinecke pr_err("Current attempt - LUN: %llu -> MAPPED LUN: %llu" 283e3d6f909SAndy Grover " from %s \n", cmd->se_lun->unpacked_lun, 28429a05deeSNicholas Bellinger cmd->orig_fe_lun, 285c66ac9dbSNicholas Bellinger sess->se_node_acl->initiatorname); 286de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 287d29a5b6aSChristoph Hellwig goto out_unlock; 288c66ac9dbSNicholas Bellinger } 289c66ac9dbSNicholas Bellinger 290c66ac9dbSNicholas Bellinger dev->dev_reserved_node_acl = sess->se_node_acl; 2910fd97ccfSChristoph Hellwig dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS; 292c66ac9dbSNicholas Bellinger if (sess->sess_bin_isid != 0) { 293c66ac9dbSNicholas Bellinger dev->dev_res_bin_isid = sess->sess_bin_isid; 2940fd97ccfSChristoph Hellwig dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS_WITH_ISID; 295c66ac9dbSNicholas Bellinger } 296f2d30680SHannes Reinecke pr_debug("SCSI-2 Reserved %s LUN: %llu -> MAPPED LUN: %llu" 297e3d6f909SAndy Grover " for %s\n", tpg->se_tpg_tfo->get_fabric_name(), 29829a05deeSNicholas Bellinger cmd->se_lun->unpacked_lun, cmd->orig_fe_lun, 299c66ac9dbSNicholas Bellinger sess->se_node_acl->initiatorname); 300c66ac9dbSNicholas Bellinger 301d29a5b6aSChristoph Hellwig out_unlock: 302d29a5b6aSChristoph Hellwig spin_unlock(&dev->dev_reservation_lock); 303d29a5b6aSChristoph Hellwig out: 3046bb35e00SChristoph Hellwig if (!ret) 3056bb35e00SChristoph Hellwig target_complete_cmd(cmd, GOOD); 306d29a5b6aSChristoph Hellwig return ret; 307c66ac9dbSNicholas Bellinger } 308c66ac9dbSNicholas Bellinger 309c66ac9dbSNicholas Bellinger 310c66ac9dbSNicholas Bellinger /* 311c66ac9dbSNicholas Bellinger * Begin SPC-3/SPC-4 Persistent Reservations emulation support 312c66ac9dbSNicholas Bellinger * 313c66ac9dbSNicholas Bellinger * This function is called by those initiator ports who are *NOT* 314c66ac9dbSNicholas Bellinger * the active PR reservation holder when a reservation is present. 315c66ac9dbSNicholas Bellinger */ 3169fcb57f3SChristoph Hellwig static int core_scsi3_pr_seq_non_holder(struct se_cmd *cmd, u32 pr_reg_type, 3179fcb57f3SChristoph Hellwig bool isid_mismatch) 318c66ac9dbSNicholas Bellinger { 319d977f437SChristoph Hellwig unsigned char *cdb = cmd->t_task_cdb; 320e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 32129a05deeSNicholas Bellinger struct se_node_acl *nacl = se_sess->se_node_acl; 3229fcb57f3SChristoph Hellwig int other_cdb = 0; 323c66ac9dbSNicholas Bellinger int registered_nexus = 0, ret = 1; /* Conflict by default */ 324c66ac9dbSNicholas Bellinger int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */ 325c66ac9dbSNicholas Bellinger int we = 0; /* Write Exclusive */ 326c66ac9dbSNicholas Bellinger int legacy = 0; /* Act like a legacy device and return 327c66ac9dbSNicholas Bellinger * RESERVATION CONFLICT on some CDBs */ 3289fcb57f3SChristoph Hellwig 3299fcb57f3SChristoph Hellwig if (isid_mismatch) { 3309fcb57f3SChristoph Hellwig registered_nexus = 0; 3319fcb57f3SChristoph Hellwig } else { 3329fcb57f3SChristoph Hellwig struct se_dev_entry *se_deve; 333c66ac9dbSNicholas Bellinger 33429a05deeSNicholas Bellinger rcu_read_lock(); 33529a05deeSNicholas Bellinger se_deve = target_nacl_find_deve(nacl, cmd->orig_fe_lun); 33680bfdfa9SNicholas Bellinger if (se_deve) 3379fcb57f3SChristoph Hellwig registered_nexus = test_bit(DEF_PR_REG_ACTIVE, 3389fcb57f3SChristoph Hellwig &se_deve->deve_flags); 33980bfdfa9SNicholas Bellinger rcu_read_unlock(); 3409fcb57f3SChristoph Hellwig } 341c66ac9dbSNicholas Bellinger 342c66ac9dbSNicholas Bellinger switch (pr_reg_type) { 343c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE: 344c66ac9dbSNicholas Bellinger we = 1; 345c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS: 346c66ac9dbSNicholas Bellinger /* 347c66ac9dbSNicholas Bellinger * Some commands are only allowed for the persistent reservation 348c66ac9dbSNicholas Bellinger * holder. 349c66ac9dbSNicholas Bellinger */ 350c66ac9dbSNicholas Bellinger break; 351c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_REGONLY: 352c66ac9dbSNicholas Bellinger we = 1; 353c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY: 354c66ac9dbSNicholas Bellinger /* 355c66ac9dbSNicholas Bellinger * Some commands are only allowed for registered I_T Nexuses. 356c66ac9dbSNicholas Bellinger */ 357c66ac9dbSNicholas Bellinger reg_only = 1; 358c66ac9dbSNicholas Bellinger break; 359c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_ALLREG: 360c66ac9dbSNicholas Bellinger we = 1; 361c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG: 362c66ac9dbSNicholas Bellinger /* 363c66ac9dbSNicholas Bellinger * Each registered I_T Nexus is a reservation holder. 364c66ac9dbSNicholas Bellinger */ 365c66ac9dbSNicholas Bellinger all_reg = 1; 366c66ac9dbSNicholas Bellinger break; 367c66ac9dbSNicholas Bellinger default: 368e3d6f909SAndy Grover return -EINVAL; 369c66ac9dbSNicholas Bellinger } 370c66ac9dbSNicholas Bellinger /* 371c66ac9dbSNicholas Bellinger * Referenced from spc4r17 table 45 for *NON* PR holder access 372c66ac9dbSNicholas Bellinger */ 373c66ac9dbSNicholas Bellinger switch (cdb[0]) { 374c66ac9dbSNicholas Bellinger case SECURITY_PROTOCOL_IN: 375c66ac9dbSNicholas Bellinger if (registered_nexus) 376c66ac9dbSNicholas Bellinger return 0; 377c66ac9dbSNicholas Bellinger ret = (we) ? 0 : 1; 378c66ac9dbSNicholas Bellinger break; 379c66ac9dbSNicholas Bellinger case MODE_SENSE: 380c66ac9dbSNicholas Bellinger case MODE_SENSE_10: 381c66ac9dbSNicholas Bellinger case READ_ATTRIBUTE: 382c66ac9dbSNicholas Bellinger case READ_BUFFER: 383c66ac9dbSNicholas Bellinger case RECEIVE_DIAGNOSTIC: 384c66ac9dbSNicholas Bellinger if (legacy) { 385c66ac9dbSNicholas Bellinger ret = 1; 386c66ac9dbSNicholas Bellinger break; 387c66ac9dbSNicholas Bellinger } 388c66ac9dbSNicholas Bellinger if (registered_nexus) { 389c66ac9dbSNicholas Bellinger ret = 0; 390c66ac9dbSNicholas Bellinger break; 391c66ac9dbSNicholas Bellinger } 392c66ac9dbSNicholas Bellinger ret = (we) ? 0 : 1; /* Allowed Write Exclusive */ 393c66ac9dbSNicholas Bellinger break; 394c66ac9dbSNicholas Bellinger case PERSISTENT_RESERVE_OUT: 395c66ac9dbSNicholas Bellinger /* 396c66ac9dbSNicholas Bellinger * This follows PERSISTENT_RESERVE_OUT service actions that 397c66ac9dbSNicholas Bellinger * are allowed in the presence of various reservations. 398c66ac9dbSNicholas Bellinger * See spc4r17, table 46 399c66ac9dbSNicholas Bellinger */ 400c66ac9dbSNicholas Bellinger switch (cdb[1] & 0x1f) { 401c66ac9dbSNicholas Bellinger case PRO_CLEAR: 402c66ac9dbSNicholas Bellinger case PRO_PREEMPT: 403c66ac9dbSNicholas Bellinger case PRO_PREEMPT_AND_ABORT: 404c66ac9dbSNicholas Bellinger ret = (registered_nexus) ? 0 : 1; 405c66ac9dbSNicholas Bellinger break; 406c66ac9dbSNicholas Bellinger case PRO_REGISTER: 407c66ac9dbSNicholas Bellinger case PRO_REGISTER_AND_IGNORE_EXISTING_KEY: 408c66ac9dbSNicholas Bellinger ret = 0; 409c66ac9dbSNicholas Bellinger break; 410c66ac9dbSNicholas Bellinger case PRO_REGISTER_AND_MOVE: 411c66ac9dbSNicholas Bellinger case PRO_RESERVE: 412c66ac9dbSNicholas Bellinger ret = 1; 413c66ac9dbSNicholas Bellinger break; 414c66ac9dbSNicholas Bellinger case PRO_RELEASE: 415c66ac9dbSNicholas Bellinger ret = (registered_nexus) ? 0 : 1; 416c66ac9dbSNicholas Bellinger break; 417c66ac9dbSNicholas Bellinger default: 4186708bb27SAndy Grover pr_err("Unknown PERSISTENT_RESERVE_OUT service" 419c66ac9dbSNicholas Bellinger " action: 0x%02x\n", cdb[1] & 0x1f); 420e3d6f909SAndy Grover return -EINVAL; 421c66ac9dbSNicholas Bellinger } 422c66ac9dbSNicholas Bellinger break; 423c66ac9dbSNicholas Bellinger case RELEASE: 424c66ac9dbSNicholas Bellinger case RELEASE_10: 425eacac00cSChristoph Hellwig /* Handled by CRH=1 in target_scsi2_reservation_release() */ 426c66ac9dbSNicholas Bellinger ret = 0; 427c66ac9dbSNicholas Bellinger break; 428c66ac9dbSNicholas Bellinger case RESERVE: 429c66ac9dbSNicholas Bellinger case RESERVE_10: 430eacac00cSChristoph Hellwig /* Handled by CRH=1 in target_scsi2_reservation_reserve() */ 431c66ac9dbSNicholas Bellinger ret = 0; 432c66ac9dbSNicholas Bellinger break; 433c66ac9dbSNicholas Bellinger case TEST_UNIT_READY: 434c66ac9dbSNicholas Bellinger ret = (legacy) ? 1 : 0; /* Conflict for legacy */ 435c66ac9dbSNicholas Bellinger break; 436c66ac9dbSNicholas Bellinger case MAINTENANCE_IN: 437c66ac9dbSNicholas Bellinger switch (cdb[1] & 0x1f) { 438c66ac9dbSNicholas Bellinger case MI_MANAGEMENT_PROTOCOL_IN: 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_SUPPORTED_OPERATION_CODES: 446c66ac9dbSNicholas Bellinger case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS: 447c66ac9dbSNicholas Bellinger if (legacy) { 448c66ac9dbSNicholas Bellinger ret = 1; 449c66ac9dbSNicholas Bellinger break; 450c66ac9dbSNicholas Bellinger } 451c66ac9dbSNicholas Bellinger if (registered_nexus) { 452c66ac9dbSNicholas Bellinger ret = 0; 453c66ac9dbSNicholas Bellinger break; 454c66ac9dbSNicholas Bellinger } 455c66ac9dbSNicholas Bellinger ret = (we) ? 0 : 1; /* Allowed Write Exclusive */ 456c66ac9dbSNicholas Bellinger break; 457c66ac9dbSNicholas Bellinger case MI_REPORT_ALIASES: 458c66ac9dbSNicholas Bellinger case MI_REPORT_IDENTIFYING_INFORMATION: 459c66ac9dbSNicholas Bellinger case MI_REPORT_PRIORITY: 460c66ac9dbSNicholas Bellinger case MI_REPORT_TARGET_PGS: 461c66ac9dbSNicholas Bellinger case MI_REPORT_TIMESTAMP: 462c66ac9dbSNicholas Bellinger ret = 0; /* Allowed */ 463c66ac9dbSNicholas Bellinger break; 464c66ac9dbSNicholas Bellinger default: 4656708bb27SAndy Grover pr_err("Unknown MI Service Action: 0x%02x\n", 466c66ac9dbSNicholas Bellinger (cdb[1] & 0x1f)); 467e3d6f909SAndy Grover return -EINVAL; 468c66ac9dbSNicholas Bellinger } 469c66ac9dbSNicholas Bellinger break; 470c66ac9dbSNicholas Bellinger case ACCESS_CONTROL_IN: 471c66ac9dbSNicholas Bellinger case ACCESS_CONTROL_OUT: 472c66ac9dbSNicholas Bellinger case INQUIRY: 473c66ac9dbSNicholas Bellinger case LOG_SENSE: 47485686f69SHannes Reinecke case SERVICE_ACTION_IN_12: 475c66ac9dbSNicholas Bellinger case REPORT_LUNS: 476c66ac9dbSNicholas Bellinger case REQUEST_SENSE: 4776816966aSMarco Sanvido case PERSISTENT_RESERVE_IN: 478c66ac9dbSNicholas Bellinger ret = 0; /*/ Allowed CDBs */ 479c66ac9dbSNicholas Bellinger break; 480c66ac9dbSNicholas Bellinger default: 481c66ac9dbSNicholas Bellinger other_cdb = 1; 482c66ac9dbSNicholas Bellinger break; 483c66ac9dbSNicholas Bellinger } 484c66ac9dbSNicholas Bellinger /* 48525985edcSLucas De Marchi * Case where the CDB is explicitly allowed in the above switch 486c66ac9dbSNicholas Bellinger * statement. 487c66ac9dbSNicholas Bellinger */ 4886708bb27SAndy Grover if (!ret && !other_cdb) { 489125d0119SHannes Reinecke pr_debug("Allowing explicit CDB: 0x%02x for %s" 490c66ac9dbSNicholas Bellinger " reservation holder\n", cdb[0], 491c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(pr_reg_type)); 4928b1e1244SAndy Grover 493c66ac9dbSNicholas Bellinger return ret; 494c66ac9dbSNicholas Bellinger } 495c66ac9dbSNicholas Bellinger /* 496c66ac9dbSNicholas Bellinger * Check if write exclusive initiator ports *NOT* holding the 497c66ac9dbSNicholas Bellinger * WRITE_EXCLUSIVE_* reservation. 498c66ac9dbSNicholas Bellinger */ 499ee1b1b9cSAndy Grover if (we && !registered_nexus) { 500c66ac9dbSNicholas Bellinger if (cmd->data_direction == DMA_TO_DEVICE) { 501c66ac9dbSNicholas Bellinger /* 502c66ac9dbSNicholas Bellinger * Conflict for write exclusive 503c66ac9dbSNicholas Bellinger */ 5046708bb27SAndy Grover pr_debug("%s Conflict for unregistered nexus" 505c66ac9dbSNicholas Bellinger " %s CDB: 0x%02x to %s reservation\n", 506c66ac9dbSNicholas Bellinger transport_dump_cmd_direction(cmd), 507c66ac9dbSNicholas Bellinger se_sess->se_node_acl->initiatorname, cdb[0], 508c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(pr_reg_type)); 509c66ac9dbSNicholas Bellinger return 1; 510c66ac9dbSNicholas Bellinger } else { 511c66ac9dbSNicholas Bellinger /* 512c66ac9dbSNicholas Bellinger * Allow non WRITE CDBs for all Write Exclusive 513c66ac9dbSNicholas Bellinger * PR TYPEs to pass for registered and 514c66ac9dbSNicholas Bellinger * non-registered_nexuxes NOT holding the reservation. 515c66ac9dbSNicholas Bellinger * 516c66ac9dbSNicholas Bellinger * We only make noise for the unregisterd nexuses, 517c66ac9dbSNicholas Bellinger * as we expect registered non-reservation holding 518c66ac9dbSNicholas Bellinger * nexuses to issue CDBs. 519c66ac9dbSNicholas Bellinger */ 5208b1e1244SAndy Grover 5216708bb27SAndy Grover if (!registered_nexus) { 522125d0119SHannes Reinecke pr_debug("Allowing implicit CDB: 0x%02x" 523c66ac9dbSNicholas Bellinger " for %s reservation on unregistered" 524c66ac9dbSNicholas Bellinger " nexus\n", cdb[0], 525c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(pr_reg_type)); 526c66ac9dbSNicholas Bellinger } 5278b1e1244SAndy Grover 528c66ac9dbSNicholas Bellinger return 0; 529c66ac9dbSNicholas Bellinger } 530c66ac9dbSNicholas Bellinger } else if ((reg_only) || (all_reg)) { 531c66ac9dbSNicholas Bellinger if (registered_nexus) { 532c66ac9dbSNicholas Bellinger /* 533c66ac9dbSNicholas Bellinger * For PR_*_REG_ONLY and PR_*_ALL_REG reservations, 534c66ac9dbSNicholas Bellinger * allow commands from registered nexuses. 535c66ac9dbSNicholas Bellinger */ 5368b1e1244SAndy Grover 537125d0119SHannes Reinecke pr_debug("Allowing implicit CDB: 0x%02x for %s" 538c66ac9dbSNicholas Bellinger " reservation\n", cdb[0], 539c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(pr_reg_type)); 5408b1e1244SAndy Grover 541c66ac9dbSNicholas Bellinger return 0; 542c66ac9dbSNicholas Bellinger } 5431ecc7586SLee Duncan } else if (we && registered_nexus) { 5441ecc7586SLee Duncan /* 5451ecc7586SLee Duncan * Reads are allowed for Write Exclusive locks 5461ecc7586SLee Duncan * from all registrants. 5471ecc7586SLee Duncan */ 5481ecc7586SLee Duncan if (cmd->data_direction == DMA_FROM_DEVICE) { 5491ecc7586SLee Duncan pr_debug("Allowing READ CDB: 0x%02x for %s" 5501ecc7586SLee Duncan " reservation\n", cdb[0], 5511ecc7586SLee Duncan core_scsi3_pr_dump_type(pr_reg_type)); 5521ecc7586SLee Duncan 5531ecc7586SLee Duncan return 0; 5541ecc7586SLee Duncan } 555c66ac9dbSNicholas Bellinger } 5566708bb27SAndy Grover pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x" 557c66ac9dbSNicholas Bellinger " for %s reservation\n", transport_dump_cmd_direction(cmd), 558c66ac9dbSNicholas Bellinger (registered_nexus) ? "" : "un", 559c66ac9dbSNicholas Bellinger se_sess->se_node_acl->initiatorname, cdb[0], 560c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(pr_reg_type)); 561c66ac9dbSNicholas Bellinger 562c66ac9dbSNicholas Bellinger return 1; /* Conflict by default */ 563c66ac9dbSNicholas Bellinger } 564c66ac9dbSNicholas Bellinger 565de103c93SChristoph Hellwig static sense_reason_t 566de103c93SChristoph Hellwig target_scsi3_pr_reservation_check(struct se_cmd *cmd) 567d977f437SChristoph Hellwig { 568d977f437SChristoph Hellwig struct se_device *dev = cmd->se_dev; 569d977f437SChristoph Hellwig struct se_session *sess = cmd->se_sess; 570d977f437SChristoph Hellwig u32 pr_reg_type; 5719fcb57f3SChristoph Hellwig bool isid_mismatch = false; 572d977f437SChristoph Hellwig 573d977f437SChristoph Hellwig if (!dev->dev_pr_res_holder) 574d977f437SChristoph Hellwig return 0; 575d977f437SChristoph Hellwig 576d977f437SChristoph Hellwig pr_reg_type = dev->dev_pr_res_holder->pr_res_type; 577d977f437SChristoph Hellwig cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key; 578d977f437SChristoph Hellwig if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl) 579d977f437SChristoph Hellwig goto check_nonholder; 580d977f437SChristoph Hellwig 581d977f437SChristoph Hellwig if (dev->dev_pr_res_holder->isid_present_at_reg) { 582d977f437SChristoph Hellwig if (dev->dev_pr_res_holder->pr_reg_bin_isid != 583d977f437SChristoph Hellwig sess->sess_bin_isid) { 5849fcb57f3SChristoph Hellwig isid_mismatch = true; 585d977f437SChristoph Hellwig goto check_nonholder; 586d977f437SChristoph Hellwig } 587d977f437SChristoph Hellwig } 588d977f437SChristoph Hellwig 589d977f437SChristoph Hellwig return 0; 590d977f437SChristoph Hellwig 591d977f437SChristoph Hellwig check_nonholder: 5929fcb57f3SChristoph Hellwig if (core_scsi3_pr_seq_non_holder(cmd, pr_reg_type, isid_mismatch)) 593de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 594d977f437SChristoph Hellwig return 0; 595d977f437SChristoph Hellwig } 596d977f437SChristoph Hellwig 597c66ac9dbSNicholas Bellinger static u32 core_scsi3_pr_generation(struct se_device *dev) 598c66ac9dbSNicholas Bellinger { 599c66ac9dbSNicholas Bellinger u32 prg; 6000fd97ccfSChristoph Hellwig 601c66ac9dbSNicholas Bellinger /* 602c66ac9dbSNicholas Bellinger * PRGeneration field shall contain the value of a 32-bit wrapping 603c66ac9dbSNicholas Bellinger * counter mainted by the device server. 604c66ac9dbSNicholas Bellinger * 605c66ac9dbSNicholas Bellinger * Note that this is done regardless of Active Persist across 606c66ac9dbSNicholas Bellinger * Target PowerLoss (APTPL) 607c66ac9dbSNicholas Bellinger * 608c66ac9dbSNicholas Bellinger * See spc4r17 section 6.3.12 READ_KEYS service action 609c66ac9dbSNicholas Bellinger */ 610c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 6110fd97ccfSChristoph Hellwig prg = dev->t10_pr.pr_generation++; 612c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 613c66ac9dbSNicholas Bellinger 614c66ac9dbSNicholas Bellinger return prg; 615c66ac9dbSNicholas Bellinger } 616c66ac9dbSNicholas Bellinger 617c66ac9dbSNicholas Bellinger static struct t10_pr_registration *__core_scsi3_do_alloc_registration( 618c66ac9dbSNicholas Bellinger struct se_device *dev, 619c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 62079dc9c9eSNicholas Bellinger struct se_lun *lun, 621c66ac9dbSNicholas Bellinger struct se_dev_entry *deve, 622f2d30680SHannes Reinecke u64 mapped_lun, 623c66ac9dbSNicholas Bellinger unsigned char *isid, 624c66ac9dbSNicholas Bellinger u64 sa_res_key, 625c66ac9dbSNicholas Bellinger int all_tg_pt, 626c66ac9dbSNicholas Bellinger int aptpl) 627c66ac9dbSNicholas Bellinger { 628c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg; 629c66ac9dbSNicholas Bellinger 630c66ac9dbSNicholas Bellinger pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC); 6316708bb27SAndy Grover if (!pr_reg) { 6326708bb27SAndy Grover pr_err("Unable to allocate struct t10_pr_registration\n"); 633c66ac9dbSNicholas Bellinger return NULL; 634c66ac9dbSNicholas Bellinger } 635c66ac9dbSNicholas Bellinger 636c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_list); 637c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list); 638c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list); 639c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list); 640c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list); 641c66ac9dbSNicholas Bellinger atomic_set(&pr_reg->pr_res_holders, 0); 642c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl = nacl; 643c66ac9dbSNicholas Bellinger pr_reg->pr_reg_deve = deve; 64479dc9c9eSNicholas Bellinger pr_reg->pr_res_mapped_lun = mapped_lun; 64579dc9c9eSNicholas Bellinger pr_reg->pr_aptpl_target_lun = lun->unpacked_lun; 646adf653f9SChristoph Hellwig pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi; 647c66ac9dbSNicholas Bellinger pr_reg->pr_res_key = sa_res_key; 648c66ac9dbSNicholas Bellinger pr_reg->pr_reg_all_tg_pt = all_tg_pt; 649c66ac9dbSNicholas Bellinger pr_reg->pr_reg_aptpl = aptpl; 650c66ac9dbSNicholas Bellinger /* 651c66ac9dbSNicholas Bellinger * If an ISID value for this SCSI Initiator Port exists, 652c66ac9dbSNicholas Bellinger * save it to the registration now. 653c66ac9dbSNicholas Bellinger */ 654c66ac9dbSNicholas Bellinger if (isid != NULL) { 655c66ac9dbSNicholas Bellinger pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid); 656c66ac9dbSNicholas Bellinger snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid); 657c66ac9dbSNicholas Bellinger pr_reg->isid_present_at_reg = 1; 658c66ac9dbSNicholas Bellinger } 659c66ac9dbSNicholas Bellinger 660c66ac9dbSNicholas Bellinger return pr_reg; 661c66ac9dbSNicholas Bellinger } 662c66ac9dbSNicholas Bellinger 663c66ac9dbSNicholas Bellinger static int core_scsi3_lunacl_depend_item(struct se_dev_entry *); 664c66ac9dbSNicholas Bellinger static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *); 665c66ac9dbSNicholas Bellinger 666c66ac9dbSNicholas Bellinger /* 667c66ac9dbSNicholas Bellinger * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0 668c66ac9dbSNicholas Bellinger * modes. 669c66ac9dbSNicholas Bellinger */ 670c66ac9dbSNicholas Bellinger static struct t10_pr_registration *__core_scsi3_alloc_registration( 671c66ac9dbSNicholas Bellinger struct se_device *dev, 672c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 67379dc9c9eSNicholas Bellinger struct se_lun *lun, 674c66ac9dbSNicholas Bellinger struct se_dev_entry *deve, 675f2d30680SHannes Reinecke u64 mapped_lun, 676c66ac9dbSNicholas Bellinger unsigned char *isid, 677c66ac9dbSNicholas Bellinger u64 sa_res_key, 678c66ac9dbSNicholas Bellinger int all_tg_pt, 679c66ac9dbSNicholas Bellinger int aptpl) 680c66ac9dbSNicholas Bellinger { 681c66ac9dbSNicholas Bellinger struct se_dev_entry *deve_tmp; 682c66ac9dbSNicholas Bellinger struct se_node_acl *nacl_tmp; 68379dc9c9eSNicholas Bellinger struct se_lun_acl *lacl_tmp; 684adf653f9SChristoph Hellwig struct se_lun *lun_tmp, *next, *dest_lun; 6859ac8928eSChristoph Hellwig const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo; 686c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe; 687c66ac9dbSNicholas Bellinger int ret; 688c66ac9dbSNicholas Bellinger /* 689c66ac9dbSNicholas Bellinger * Create a registration for the I_T Nexus upon which the 690c66ac9dbSNicholas Bellinger * PROUT REGISTER was received. 691c66ac9dbSNicholas Bellinger */ 69279dc9c9eSNicholas Bellinger pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, lun, deve, mapped_lun, 69379dc9c9eSNicholas Bellinger isid, sa_res_key, all_tg_pt, 69479dc9c9eSNicholas Bellinger aptpl); 6956708bb27SAndy Grover if (!pr_reg) 696c66ac9dbSNicholas Bellinger return NULL; 697c66ac9dbSNicholas Bellinger /* 698c66ac9dbSNicholas Bellinger * Return pointer to pr_reg for ALL_TG_PT=0 699c66ac9dbSNicholas Bellinger */ 7006708bb27SAndy Grover if (!all_tg_pt) 701c66ac9dbSNicholas Bellinger return pr_reg; 702c66ac9dbSNicholas Bellinger /* 703c66ac9dbSNicholas Bellinger * Create list of matching SCSI Initiator Port registrations 704c66ac9dbSNicholas Bellinger * for ALL_TG_PT=1 705c66ac9dbSNicholas Bellinger */ 706c66ac9dbSNicholas Bellinger spin_lock(&dev->se_port_lock); 707adf653f9SChristoph Hellwig list_for_each_entry_safe(lun_tmp, next, &dev->dev_sep_list, lun_dev_link) { 7089e37d042SNicholas Bellinger if (!percpu_ref_tryget_live(&lun_tmp->lun_ref)) 7099e37d042SNicholas Bellinger continue; 710c66ac9dbSNicholas Bellinger spin_unlock(&dev->se_port_lock); 711c66ac9dbSNicholas Bellinger 712adf653f9SChristoph Hellwig spin_lock_bh(&lun_tmp->lun_deve_lock); 713adf653f9SChristoph Hellwig list_for_each_entry(deve_tmp, &lun_tmp->lun_deve_list, lun_link) { 714c66ac9dbSNicholas Bellinger /* 715c66ac9dbSNicholas Bellinger * This pointer will be NULL for demo mode MappedLUNs 716125d0119SHannes Reinecke * that have not been make explicit via a ConfigFS 717c66ac9dbSNicholas Bellinger * MappedLUN group for the SCSI Initiator Node ACL. 718c66ac9dbSNicholas Bellinger */ 7196708bb27SAndy Grover if (!deve_tmp->se_lun_acl) 720c66ac9dbSNicholas Bellinger continue; 721c66ac9dbSNicholas Bellinger 72279dc9c9eSNicholas Bellinger lacl_tmp = rcu_dereference_check(deve_tmp->se_lun_acl, 723adf653f9SChristoph Hellwig lockdep_is_held(&lun_tmp->lun_deve_lock)); 72479dc9c9eSNicholas Bellinger nacl_tmp = lacl_tmp->se_lun_nacl; 725c66ac9dbSNicholas Bellinger /* 726c66ac9dbSNicholas Bellinger * Skip the matching struct se_node_acl that is allocated 727c66ac9dbSNicholas Bellinger * above.. 728c66ac9dbSNicholas Bellinger */ 729c66ac9dbSNicholas Bellinger if (nacl == nacl_tmp) 730c66ac9dbSNicholas Bellinger continue; 731c66ac9dbSNicholas Bellinger /* 732c66ac9dbSNicholas Bellinger * Only perform PR registrations for target ports on 733c66ac9dbSNicholas Bellinger * the same fabric module as the REGISTER w/ ALL_TG_PT=1 734c66ac9dbSNicholas Bellinger * arrived. 735c66ac9dbSNicholas Bellinger */ 736c66ac9dbSNicholas Bellinger if (tfo != nacl_tmp->se_tpg->se_tpg_tfo) 737c66ac9dbSNicholas Bellinger continue; 738c66ac9dbSNicholas Bellinger /* 739c66ac9dbSNicholas Bellinger * Look for a matching Initiator Node ACL in ASCII format 740c66ac9dbSNicholas Bellinger */ 741c66ac9dbSNicholas Bellinger if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname)) 742c66ac9dbSNicholas Bellinger continue; 743c66ac9dbSNicholas Bellinger 74429a05deeSNicholas Bellinger kref_get(&deve_tmp->pr_kref); 745adf653f9SChristoph Hellwig spin_unlock_bh(&lun_tmp->lun_deve_lock); 746c66ac9dbSNicholas Bellinger /* 747c66ac9dbSNicholas Bellinger * Grab a configfs group dependency that is released 748c66ac9dbSNicholas Bellinger * for the exception path at label out: below, or upon 749c66ac9dbSNicholas Bellinger * completion of adding ALL_TG_PT=1 registrations in 750c66ac9dbSNicholas Bellinger * __core_scsi3_add_registration() 751c66ac9dbSNicholas Bellinger */ 752c66ac9dbSNicholas Bellinger ret = core_scsi3_lunacl_depend_item(deve_tmp); 753c66ac9dbSNicholas Bellinger if (ret < 0) { 7546708bb27SAndy Grover pr_err("core_scsi3_lunacl_depend" 755c66ac9dbSNicholas Bellinger "_item() failed\n"); 7569e37d042SNicholas Bellinger percpu_ref_put(&lun_tmp->lun_ref); 75729a05deeSNicholas Bellinger kref_put(&deve_tmp->pr_kref, target_pr_kref_release); 758c66ac9dbSNicholas Bellinger goto out; 759c66ac9dbSNicholas Bellinger } 760c66ac9dbSNicholas Bellinger /* 761c66ac9dbSNicholas Bellinger * Located a matching SCSI Initiator Port on a different 762c66ac9dbSNicholas Bellinger * port, allocate the pr_reg_atp and attach it to the 763c66ac9dbSNicholas Bellinger * pr_reg->pr_reg_atp_list that will be processed once 764c66ac9dbSNicholas Bellinger * the original *pr_reg is processed in 765c66ac9dbSNicholas Bellinger * __core_scsi3_add_registration() 766c66ac9dbSNicholas Bellinger */ 767adf653f9SChristoph Hellwig dest_lun = rcu_dereference_check(deve_tmp->se_lun, 76879dc9c9eSNicholas Bellinger atomic_read(&deve_tmp->pr_kref.refcount) != 0); 76979dc9c9eSNicholas Bellinger 770c66ac9dbSNicholas Bellinger pr_reg_atp = __core_scsi3_do_alloc_registration(dev, 771adf653f9SChristoph Hellwig nacl_tmp, dest_lun, deve_tmp, 77279dc9c9eSNicholas Bellinger deve_tmp->mapped_lun, NULL, 773c66ac9dbSNicholas Bellinger sa_res_key, all_tg_pt, aptpl); 7746708bb27SAndy Grover if (!pr_reg_atp) { 7759e37d042SNicholas Bellinger percpu_ref_put(&lun_tmp->lun_ref); 776c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(deve_tmp); 777c66ac9dbSNicholas Bellinger goto out; 778c66ac9dbSNicholas Bellinger } 779c66ac9dbSNicholas Bellinger 780c66ac9dbSNicholas Bellinger list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list, 781c66ac9dbSNicholas Bellinger &pr_reg->pr_reg_atp_list); 782adf653f9SChristoph Hellwig spin_lock_bh(&lun_tmp->lun_deve_lock); 783c66ac9dbSNicholas Bellinger } 784adf653f9SChristoph Hellwig spin_unlock_bh(&lun_tmp->lun_deve_lock); 785c66ac9dbSNicholas Bellinger 786c66ac9dbSNicholas Bellinger spin_lock(&dev->se_port_lock); 7879e37d042SNicholas Bellinger percpu_ref_put(&lun_tmp->lun_ref); 788c66ac9dbSNicholas Bellinger } 789c66ac9dbSNicholas Bellinger spin_unlock(&dev->se_port_lock); 790c66ac9dbSNicholas Bellinger 791c66ac9dbSNicholas Bellinger return pr_reg; 792c66ac9dbSNicholas Bellinger out: 793c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe, 794c66ac9dbSNicholas Bellinger &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) { 795c66ac9dbSNicholas Bellinger list_del(&pr_reg_tmp->pr_reg_atp_mem_list); 796c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve); 797c66ac9dbSNicholas Bellinger kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp); 798c66ac9dbSNicholas Bellinger } 799c66ac9dbSNicholas Bellinger kmem_cache_free(t10_pr_reg_cache, pr_reg); 800c66ac9dbSNicholas Bellinger return NULL; 801c66ac9dbSNicholas Bellinger } 802c66ac9dbSNicholas Bellinger 803c66ac9dbSNicholas Bellinger int core_scsi3_alloc_aptpl_registration( 804e3d6f909SAndy Grover struct t10_reservation *pr_tmpl, 805c66ac9dbSNicholas Bellinger u64 sa_res_key, 806c66ac9dbSNicholas Bellinger unsigned char *i_port, 807c66ac9dbSNicholas Bellinger unsigned char *isid, 808f2d30680SHannes Reinecke u64 mapped_lun, 809c66ac9dbSNicholas Bellinger unsigned char *t_port, 810c66ac9dbSNicholas Bellinger u16 tpgt, 811f2d30680SHannes Reinecke u64 target_lun, 812c66ac9dbSNicholas Bellinger int res_holder, 813c66ac9dbSNicholas Bellinger int all_tg_pt, 814c66ac9dbSNicholas Bellinger u8 type) 815c66ac9dbSNicholas Bellinger { 816c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg; 817c66ac9dbSNicholas Bellinger 8186708bb27SAndy Grover if (!i_port || !t_port || !sa_res_key) { 8196708bb27SAndy Grover pr_err("Illegal parameters for APTPL registration\n"); 820e3d6f909SAndy Grover return -EINVAL; 821c66ac9dbSNicholas Bellinger } 822c66ac9dbSNicholas Bellinger 823c66ac9dbSNicholas Bellinger pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL); 8246708bb27SAndy Grover if (!pr_reg) { 8256708bb27SAndy Grover pr_err("Unable to allocate struct t10_pr_registration\n"); 826e3d6f909SAndy Grover return -ENOMEM; 827c66ac9dbSNicholas Bellinger } 828c66ac9dbSNicholas Bellinger 829c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_list); 830c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list); 831c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list); 832c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list); 833c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list); 834c66ac9dbSNicholas Bellinger atomic_set(&pr_reg->pr_res_holders, 0); 835c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl = NULL; 836c66ac9dbSNicholas Bellinger pr_reg->pr_reg_deve = NULL; 837c66ac9dbSNicholas Bellinger pr_reg->pr_res_mapped_lun = mapped_lun; 838c66ac9dbSNicholas Bellinger pr_reg->pr_aptpl_target_lun = target_lun; 839c66ac9dbSNicholas Bellinger pr_reg->pr_res_key = sa_res_key; 840c66ac9dbSNicholas Bellinger pr_reg->pr_reg_all_tg_pt = all_tg_pt; 841c66ac9dbSNicholas Bellinger pr_reg->pr_reg_aptpl = 1; 842c66ac9dbSNicholas Bellinger pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */ 843c66ac9dbSNicholas Bellinger pr_reg->pr_res_type = type; 844c66ac9dbSNicholas Bellinger /* 845c66ac9dbSNicholas Bellinger * If an ISID value had been saved in APTPL metadata for this 846c66ac9dbSNicholas Bellinger * SCSI Initiator Port, restore it now. 847c66ac9dbSNicholas Bellinger */ 848c66ac9dbSNicholas Bellinger if (isid != NULL) { 849c66ac9dbSNicholas Bellinger pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid); 850c66ac9dbSNicholas Bellinger snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid); 851c66ac9dbSNicholas Bellinger pr_reg->isid_present_at_reg = 1; 852c66ac9dbSNicholas Bellinger } 853c66ac9dbSNicholas Bellinger /* 854c66ac9dbSNicholas Bellinger * Copy the i_port and t_port information from caller. 855c66ac9dbSNicholas Bellinger */ 856c66ac9dbSNicholas Bellinger snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port); 857c66ac9dbSNicholas Bellinger snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port); 858c66ac9dbSNicholas Bellinger pr_reg->pr_reg_tpgt = tpgt; 859c66ac9dbSNicholas Bellinger /* 860c66ac9dbSNicholas Bellinger * Set pr_res_holder from caller, the pr_reg who is the reservation 861c66ac9dbSNicholas Bellinger * holder will get it's pointer set in core_scsi3_aptpl_reserve() once 862c66ac9dbSNicholas Bellinger * the Initiator Node LUN ACL from the fabric module is created for 863c66ac9dbSNicholas Bellinger * this registration. 864c66ac9dbSNicholas Bellinger */ 865c66ac9dbSNicholas Bellinger pr_reg->pr_res_holder = res_holder; 866c66ac9dbSNicholas Bellinger 867c66ac9dbSNicholas Bellinger list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list); 8686708bb27SAndy Grover pr_debug("SPC-3 PR APTPL Successfully added registration%s from" 869c66ac9dbSNicholas Bellinger " metadata\n", (res_holder) ? "+reservation" : ""); 870c66ac9dbSNicholas Bellinger return 0; 871c66ac9dbSNicholas Bellinger } 872c66ac9dbSNicholas Bellinger 873c66ac9dbSNicholas Bellinger static void core_scsi3_aptpl_reserve( 874c66ac9dbSNicholas Bellinger struct se_device *dev, 875c66ac9dbSNicholas Bellinger struct se_portal_group *tpg, 876c66ac9dbSNicholas Bellinger struct se_node_acl *node_acl, 877c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg) 878c66ac9dbSNicholas Bellinger { 879c66ac9dbSNicholas Bellinger char i_buf[PR_REG_ISID_ID_LEN]; 880c66ac9dbSNicholas Bellinger 881c66ac9dbSNicholas Bellinger memset(i_buf, 0, PR_REG_ISID_ID_LEN); 882d2843c17SAndy Grover core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); 883c66ac9dbSNicholas Bellinger 884c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 885c66ac9dbSNicholas Bellinger dev->dev_pr_res_holder = pr_reg; 886c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 887c66ac9dbSNicholas Bellinger 8886708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created" 889c66ac9dbSNicholas Bellinger " new reservation holder TYPE: %s ALL_TG_PT: %d\n", 890e3d6f909SAndy Grover tpg->se_tpg_tfo->get_fabric_name(), 891c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(pr_reg->pr_res_type), 892c66ac9dbSNicholas Bellinger (pr_reg->pr_reg_all_tg_pt) ? 1 : 0); 8936708bb27SAndy Grover pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n", 894e3d6f909SAndy Grover tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname, 895d2843c17SAndy Grover i_buf); 896c66ac9dbSNicholas Bellinger } 897c66ac9dbSNicholas Bellinger 898c66ac9dbSNicholas Bellinger static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *, 89933ce6a87SAndy Grover struct t10_pr_registration *, enum register_type, int); 900c66ac9dbSNicholas Bellinger 901c66ac9dbSNicholas Bellinger static int __core_scsi3_check_aptpl_registration( 902c66ac9dbSNicholas Bellinger struct se_device *dev, 903c66ac9dbSNicholas Bellinger struct se_portal_group *tpg, 904c66ac9dbSNicholas Bellinger struct se_lun *lun, 905f2d30680SHannes Reinecke u64 target_lun, 906c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 907f2d30680SHannes Reinecke u64 mapped_lun) 908c66ac9dbSNicholas Bellinger { 909c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp; 9100fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 911c66ac9dbSNicholas Bellinger unsigned char i_port[PR_APTPL_MAX_IPORT_LEN]; 912c66ac9dbSNicholas Bellinger unsigned char t_port[PR_APTPL_MAX_TPORT_LEN]; 913c66ac9dbSNicholas Bellinger u16 tpgt; 914c66ac9dbSNicholas Bellinger 915c66ac9dbSNicholas Bellinger memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN); 916c66ac9dbSNicholas Bellinger memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN); 917c66ac9dbSNicholas Bellinger /* 918c66ac9dbSNicholas Bellinger * Copy Initiator Port information from struct se_node_acl 919c66ac9dbSNicholas Bellinger */ 920c66ac9dbSNicholas Bellinger snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname); 921c66ac9dbSNicholas Bellinger snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s", 922e3d6f909SAndy Grover tpg->se_tpg_tfo->tpg_get_wwn(tpg)); 923e3d6f909SAndy Grover tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg); 924c66ac9dbSNicholas Bellinger /* 925c66ac9dbSNicholas Bellinger * Look for the matching registrations+reservation from those 926c66ac9dbSNicholas Bellinger * created from APTPL metadata. Note that multiple registrations 927c66ac9dbSNicholas Bellinger * may exist for fabrics that use ISIDs in their SCSI Initiator Port 928c66ac9dbSNicholas Bellinger * TransportIDs. 929c66ac9dbSNicholas Bellinger */ 930c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->aptpl_reg_lock); 931c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list, 932c66ac9dbSNicholas Bellinger pr_reg_aptpl_list) { 93392404e60SNicholas Bellinger 9346708bb27SAndy Grover if (!strcmp(pr_reg->pr_iport, i_port) && 93529a05deeSNicholas Bellinger (pr_reg->pr_res_mapped_lun == mapped_lun) && 936c66ac9dbSNicholas Bellinger !(strcmp(pr_reg->pr_tport, t_port)) && 937c66ac9dbSNicholas Bellinger (pr_reg->pr_reg_tpgt == tpgt) && 938c66ac9dbSNicholas Bellinger (pr_reg->pr_aptpl_target_lun == target_lun)) { 939c66ac9dbSNicholas Bellinger 940c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl = nacl; 941adf653f9SChristoph Hellwig pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi; 942c66ac9dbSNicholas Bellinger 943c66ac9dbSNicholas Bellinger list_del(&pr_reg->pr_reg_aptpl_list); 944c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->aptpl_reg_lock); 945c66ac9dbSNicholas Bellinger /* 946c66ac9dbSNicholas Bellinger * At this point all of the pointers in *pr_reg will 947c66ac9dbSNicholas Bellinger * be setup, so go ahead and add the registration. 948c66ac9dbSNicholas Bellinger */ 949c66ac9dbSNicholas Bellinger 950c66ac9dbSNicholas Bellinger __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0); 951c66ac9dbSNicholas Bellinger /* 952c66ac9dbSNicholas Bellinger * If this registration is the reservation holder, 953c66ac9dbSNicholas Bellinger * make that happen now.. 954c66ac9dbSNicholas Bellinger */ 955c66ac9dbSNicholas Bellinger if (pr_reg->pr_res_holder) 956c66ac9dbSNicholas Bellinger core_scsi3_aptpl_reserve(dev, tpg, 957c66ac9dbSNicholas Bellinger nacl, pr_reg); 958c66ac9dbSNicholas Bellinger /* 959c66ac9dbSNicholas Bellinger * Reenable pr_aptpl_active to accept new metadata 960c66ac9dbSNicholas Bellinger * updates once the SCSI device is active again.. 961c66ac9dbSNicholas Bellinger */ 962c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->aptpl_reg_lock); 963c66ac9dbSNicholas Bellinger pr_tmpl->pr_aptpl_active = 1; 964c66ac9dbSNicholas Bellinger } 965c66ac9dbSNicholas Bellinger } 966c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->aptpl_reg_lock); 967c66ac9dbSNicholas Bellinger 968c66ac9dbSNicholas Bellinger return 0; 969c66ac9dbSNicholas Bellinger } 970c66ac9dbSNicholas Bellinger 971c66ac9dbSNicholas Bellinger int core_scsi3_check_aptpl_registration( 972c66ac9dbSNicholas Bellinger struct se_device *dev, 973c66ac9dbSNicholas Bellinger struct se_portal_group *tpg, 974c66ac9dbSNicholas Bellinger struct se_lun *lun, 975e2480563SNicholas Bellinger struct se_node_acl *nacl, 976f2d30680SHannes Reinecke u64 mapped_lun) 977c66ac9dbSNicholas Bellinger { 978d977f437SChristoph Hellwig if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) 979c66ac9dbSNicholas Bellinger return 0; 980c66ac9dbSNicholas Bellinger 981c66ac9dbSNicholas Bellinger return __core_scsi3_check_aptpl_registration(dev, tpg, lun, 98229a05deeSNicholas Bellinger lun->unpacked_lun, nacl, 98329a05deeSNicholas Bellinger mapped_lun); 984c66ac9dbSNicholas Bellinger } 985c66ac9dbSNicholas Bellinger 986c66ac9dbSNicholas Bellinger static void __core_scsi3_dump_registration( 9879ac8928eSChristoph Hellwig const struct target_core_fabric_ops *tfo, 988c66ac9dbSNicholas Bellinger struct se_device *dev, 989c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 990c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, 99133ce6a87SAndy Grover enum register_type register_type) 992c66ac9dbSNicholas Bellinger { 993c66ac9dbSNicholas Bellinger struct se_portal_group *se_tpg = nacl->se_tpg; 994c66ac9dbSNicholas Bellinger char i_buf[PR_REG_ISID_ID_LEN]; 995c66ac9dbSNicholas Bellinger 996c66ac9dbSNicholas Bellinger memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN); 997d2843c17SAndy Grover core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); 998c66ac9dbSNicholas Bellinger 9996708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator" 100033ce6a87SAndy Grover " Node: %s%s\n", tfo->get_fabric_name(), (register_type == REGISTER_AND_MOVE) ? 100133ce6a87SAndy Grover "_AND_MOVE" : (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? 1002c66ac9dbSNicholas Bellinger "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname, 1003d2843c17SAndy Grover i_buf); 10046708bb27SAndy Grover pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n", 1005c66ac9dbSNicholas Bellinger tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg), 1006c66ac9dbSNicholas Bellinger tfo->tpg_get_tag(se_tpg)); 10076708bb27SAndy Grover pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target" 1008c66ac9dbSNicholas Bellinger " Port(s)\n", tfo->get_fabric_name(), 1009c66ac9dbSNicholas Bellinger (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE", 1010e3d6f909SAndy Grover dev->transport->name); 10116708bb27SAndy Grover pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:" 1012c66ac9dbSNicholas Bellinger " 0x%08x APTPL: %d\n", tfo->get_fabric_name(), 1013c66ac9dbSNicholas Bellinger pr_reg->pr_res_key, pr_reg->pr_res_generation, 1014c66ac9dbSNicholas Bellinger pr_reg->pr_reg_aptpl); 1015c66ac9dbSNicholas Bellinger } 1016c66ac9dbSNicholas Bellinger 1017c66ac9dbSNicholas Bellinger static void __core_scsi3_add_registration( 1018c66ac9dbSNicholas Bellinger struct se_device *dev, 1019c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 1020c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, 102133ce6a87SAndy Grover enum register_type register_type, 1022c66ac9dbSNicholas Bellinger int register_move) 1023c66ac9dbSNicholas Bellinger { 10249ac8928eSChristoph Hellwig const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo; 1025c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe; 10260fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 102780bfdfa9SNicholas Bellinger struct se_dev_entry *deve; 1028c66ac9dbSNicholas Bellinger 1029c66ac9dbSNicholas Bellinger /* 1030c66ac9dbSNicholas Bellinger * Increment PRgeneration counter for struct se_device upon a successful 1031c66ac9dbSNicholas Bellinger * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action 1032c66ac9dbSNicholas Bellinger * 1033c66ac9dbSNicholas Bellinger * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service 1034c66ac9dbSNicholas Bellinger * action, the struct se_device->dev_reservation_lock will already be held, 1035c66ac9dbSNicholas Bellinger * so we do not call core_scsi3_pr_generation() which grabs the lock 1036c66ac9dbSNicholas Bellinger * for the REGISTER. 1037c66ac9dbSNicholas Bellinger */ 1038c66ac9dbSNicholas Bellinger pr_reg->pr_res_generation = (register_move) ? 10390fd97ccfSChristoph Hellwig dev->t10_pr.pr_generation++ : 1040c66ac9dbSNicholas Bellinger core_scsi3_pr_generation(dev); 1041c66ac9dbSNicholas Bellinger 1042c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 1043c66ac9dbSNicholas Bellinger list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list); 1044c66ac9dbSNicholas Bellinger 1045c66ac9dbSNicholas Bellinger __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type); 1046c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 104780bfdfa9SNicholas Bellinger 104880bfdfa9SNicholas Bellinger rcu_read_lock(); 104980bfdfa9SNicholas Bellinger deve = pr_reg->pr_reg_deve; 105080bfdfa9SNicholas Bellinger if (deve) 105180bfdfa9SNicholas Bellinger set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags); 105280bfdfa9SNicholas Bellinger rcu_read_unlock(); 105380bfdfa9SNicholas Bellinger 1054c66ac9dbSNicholas Bellinger /* 1055c66ac9dbSNicholas Bellinger * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE. 1056c66ac9dbSNicholas Bellinger */ 10576708bb27SAndy Grover if (!pr_reg->pr_reg_all_tg_pt || register_move) 1058c66ac9dbSNicholas Bellinger return; 1059c66ac9dbSNicholas Bellinger /* 1060c66ac9dbSNicholas Bellinger * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1 1061c66ac9dbSNicholas Bellinger * allocated in __core_scsi3_alloc_registration() 1062c66ac9dbSNicholas Bellinger */ 1063c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe, 1064c66ac9dbSNicholas Bellinger &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) { 106580bfdfa9SNicholas Bellinger struct se_node_acl *nacl_tmp = pr_reg_tmp->pr_reg_nacl; 106680bfdfa9SNicholas Bellinger 1067c66ac9dbSNicholas Bellinger list_del(&pr_reg_tmp->pr_reg_atp_mem_list); 1068c66ac9dbSNicholas Bellinger 1069c66ac9dbSNicholas Bellinger pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev); 1070c66ac9dbSNicholas Bellinger 1071c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 1072c66ac9dbSNicholas Bellinger list_add_tail(&pr_reg_tmp->pr_reg_list, 1073c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list); 1074c66ac9dbSNicholas Bellinger 107580bfdfa9SNicholas Bellinger __core_scsi3_dump_registration(tfo, dev, nacl_tmp, pr_reg_tmp, 1076c66ac9dbSNicholas Bellinger register_type); 1077c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 107880bfdfa9SNicholas Bellinger 107980bfdfa9SNicholas Bellinger rcu_read_lock(); 108080bfdfa9SNicholas Bellinger deve = pr_reg_tmp->pr_reg_deve; 108180bfdfa9SNicholas Bellinger if (deve) 108280bfdfa9SNicholas Bellinger set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags); 108380bfdfa9SNicholas Bellinger rcu_read_unlock(); 108480bfdfa9SNicholas Bellinger 1085c66ac9dbSNicholas Bellinger /* 1086c66ac9dbSNicholas Bellinger * Drop configfs group dependency reference from 1087c66ac9dbSNicholas Bellinger * __core_scsi3_alloc_registration() 1088c66ac9dbSNicholas Bellinger */ 1089c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve); 1090c66ac9dbSNicholas Bellinger } 1091c66ac9dbSNicholas Bellinger } 1092c66ac9dbSNicholas Bellinger 1093c66ac9dbSNicholas Bellinger static int core_scsi3_alloc_registration( 1094c66ac9dbSNicholas Bellinger struct se_device *dev, 1095c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 109679dc9c9eSNicholas Bellinger struct se_lun *lun, 1097c66ac9dbSNicholas Bellinger struct se_dev_entry *deve, 1098f2d30680SHannes Reinecke u64 mapped_lun, 1099c66ac9dbSNicholas Bellinger unsigned char *isid, 1100c66ac9dbSNicholas Bellinger u64 sa_res_key, 1101c66ac9dbSNicholas Bellinger int all_tg_pt, 1102c66ac9dbSNicholas Bellinger int aptpl, 110333ce6a87SAndy Grover enum register_type register_type, 1104c66ac9dbSNicholas Bellinger int register_move) 1105c66ac9dbSNicholas Bellinger { 1106c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg; 1107c66ac9dbSNicholas Bellinger 110879dc9c9eSNicholas Bellinger pr_reg = __core_scsi3_alloc_registration(dev, nacl, lun, deve, mapped_lun, 110979dc9c9eSNicholas Bellinger isid, sa_res_key, all_tg_pt, 111079dc9c9eSNicholas Bellinger aptpl); 11116708bb27SAndy Grover if (!pr_reg) 1112e3d6f909SAndy Grover return -EPERM; 1113c66ac9dbSNicholas Bellinger 1114c66ac9dbSNicholas Bellinger __core_scsi3_add_registration(dev, nacl, pr_reg, 1115c66ac9dbSNicholas Bellinger register_type, register_move); 1116c66ac9dbSNicholas Bellinger return 0; 1117c66ac9dbSNicholas Bellinger } 1118c66ac9dbSNicholas Bellinger 1119c66ac9dbSNicholas Bellinger static struct t10_pr_registration *__core_scsi3_locate_pr_reg( 1120c66ac9dbSNicholas Bellinger struct se_device *dev, 1121c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 1122c66ac9dbSNicholas Bellinger unsigned char *isid) 1123c66ac9dbSNicholas Bellinger { 11240fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 1125c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp; 1126c66ac9dbSNicholas Bellinger struct se_portal_group *tpg; 1127c66ac9dbSNicholas Bellinger 1128c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 1129c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 1130c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 1131c66ac9dbSNicholas Bellinger /* 1132c66ac9dbSNicholas Bellinger * First look for a matching struct se_node_acl 1133c66ac9dbSNicholas Bellinger */ 1134c66ac9dbSNicholas Bellinger if (pr_reg->pr_reg_nacl != nacl) 1135c66ac9dbSNicholas Bellinger continue; 1136c66ac9dbSNicholas Bellinger 1137c66ac9dbSNicholas Bellinger tpg = pr_reg->pr_reg_nacl->se_tpg; 1138c66ac9dbSNicholas Bellinger /* 1139c66ac9dbSNicholas Bellinger * If this registration does NOT contain a fabric provided 1140c66ac9dbSNicholas Bellinger * ISID, then we have found a match. 1141c66ac9dbSNicholas Bellinger */ 11426708bb27SAndy Grover if (!pr_reg->isid_present_at_reg) { 1143c66ac9dbSNicholas Bellinger /* 1144c66ac9dbSNicholas Bellinger * Determine if this SCSI device server requires that 1145c66ac9dbSNicholas Bellinger * SCSI Intiatior TransportID w/ ISIDs is enforced 1146c66ac9dbSNicholas Bellinger * for fabric modules (iSCSI) requiring them. 1147c66ac9dbSNicholas Bellinger */ 1148e3d6f909SAndy Grover if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) { 11490fd97ccfSChristoph Hellwig if (dev->dev_attrib.enforce_pr_isids) 1150c66ac9dbSNicholas Bellinger continue; 1151c66ac9dbSNicholas Bellinger } 115233940d09SJoern Engel atomic_inc_mb(&pr_reg->pr_res_holders); 1153c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 1154c66ac9dbSNicholas Bellinger return pr_reg; 1155c66ac9dbSNicholas Bellinger } 1156c66ac9dbSNicholas Bellinger /* 1157c66ac9dbSNicholas Bellinger * If the *pr_reg contains a fabric defined ISID for multi-value 1158c66ac9dbSNicholas Bellinger * SCSI Initiator Port TransportIDs, then we expect a valid 1159c66ac9dbSNicholas Bellinger * matching ISID to be provided by the local SCSI Initiator Port. 1160c66ac9dbSNicholas Bellinger */ 11616708bb27SAndy Grover if (!isid) 1162c66ac9dbSNicholas Bellinger continue; 1163c66ac9dbSNicholas Bellinger if (strcmp(isid, pr_reg->pr_reg_isid)) 1164c66ac9dbSNicholas Bellinger continue; 1165c66ac9dbSNicholas Bellinger 116633940d09SJoern Engel atomic_inc_mb(&pr_reg->pr_res_holders); 1167c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 1168c66ac9dbSNicholas Bellinger return pr_reg; 1169c66ac9dbSNicholas Bellinger } 1170c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 1171c66ac9dbSNicholas Bellinger 1172c66ac9dbSNicholas Bellinger return NULL; 1173c66ac9dbSNicholas Bellinger } 1174c66ac9dbSNicholas Bellinger 1175c66ac9dbSNicholas Bellinger static struct t10_pr_registration *core_scsi3_locate_pr_reg( 1176c66ac9dbSNicholas Bellinger struct se_device *dev, 1177c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 1178c66ac9dbSNicholas Bellinger struct se_session *sess) 1179c66ac9dbSNicholas Bellinger { 1180c66ac9dbSNicholas Bellinger struct se_portal_group *tpg = nacl->se_tpg; 1181c66ac9dbSNicholas Bellinger unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL; 1182c66ac9dbSNicholas Bellinger 1183e3d6f909SAndy Grover if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) { 1184c66ac9dbSNicholas Bellinger memset(&buf[0], 0, PR_REG_ISID_LEN); 1185e3d6f909SAndy Grover tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0], 1186c66ac9dbSNicholas Bellinger PR_REG_ISID_LEN); 1187c66ac9dbSNicholas Bellinger isid_ptr = &buf[0]; 1188c66ac9dbSNicholas Bellinger } 1189c66ac9dbSNicholas Bellinger 1190c66ac9dbSNicholas Bellinger return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr); 1191c66ac9dbSNicholas Bellinger } 1192c66ac9dbSNicholas Bellinger 1193c66ac9dbSNicholas Bellinger static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg) 1194c66ac9dbSNicholas Bellinger { 119533940d09SJoern Engel atomic_dec_mb(&pr_reg->pr_res_holders); 1196c66ac9dbSNicholas Bellinger } 1197c66ac9dbSNicholas Bellinger 1198125d0119SHannes Reinecke static int core_scsi3_check_implicit_release( 1199c66ac9dbSNicholas Bellinger struct se_device *dev, 1200c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg) 1201c66ac9dbSNicholas Bellinger { 1202c66ac9dbSNicholas Bellinger struct se_node_acl *nacl = pr_reg->pr_reg_nacl; 1203c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_res_holder; 1204c66ac9dbSNicholas Bellinger int ret = 0; 1205c66ac9dbSNicholas Bellinger 1206c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 1207c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 12086708bb27SAndy Grover if (!pr_res_holder) { 1209c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 1210c66ac9dbSNicholas Bellinger return ret; 1211c66ac9dbSNicholas Bellinger } 1212c66ac9dbSNicholas Bellinger if (pr_res_holder == pr_reg) { 1213c66ac9dbSNicholas Bellinger /* 1214125d0119SHannes Reinecke * Perform an implicit RELEASE if the registration that 1215c66ac9dbSNicholas Bellinger * is being released is holding the reservation. 1216c66ac9dbSNicholas Bellinger * 1217c66ac9dbSNicholas Bellinger * From spc4r17, section 5.7.11.1: 1218c66ac9dbSNicholas Bellinger * 1219c66ac9dbSNicholas Bellinger * e) If the I_T nexus is the persistent reservation holder 1220c66ac9dbSNicholas Bellinger * and the persistent reservation is not an all registrants 1221c66ac9dbSNicholas Bellinger * type, then a PERSISTENT RESERVE OUT command with REGISTER 1222c66ac9dbSNicholas Bellinger * service action or REGISTER AND IGNORE EXISTING KEY 1223c66ac9dbSNicholas Bellinger * service action with the SERVICE ACTION RESERVATION KEY 1224c66ac9dbSNicholas Bellinger * field set to zero (see 5.7.11.3). 1225c66ac9dbSNicholas Bellinger */ 12266c3c9baaSNicholas Bellinger __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0, 1); 1227c66ac9dbSNicholas Bellinger ret = 1; 1228c66ac9dbSNicholas Bellinger /* 1229c66ac9dbSNicholas Bellinger * For 'All Registrants' reservation types, all existing 1230c66ac9dbSNicholas Bellinger * registrations are still processed as reservation holders 1231c66ac9dbSNicholas Bellinger * in core_scsi3_pr_seq_non_holder() after the initial 1232125d0119SHannes Reinecke * reservation holder is implicitly released here. 1233c66ac9dbSNicholas Bellinger */ 1234c66ac9dbSNicholas Bellinger } else if (pr_reg->pr_reg_all_tg_pt && 1235c66ac9dbSNicholas Bellinger (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname, 1236c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl->initiatorname)) && 1237c66ac9dbSNicholas Bellinger (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) { 12386708bb27SAndy Grover pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1" 1239c66ac9dbSNicholas Bellinger " UNREGISTER while existing reservation with matching" 1240c66ac9dbSNicholas Bellinger " key 0x%016Lx is present from another SCSI Initiator" 1241c66ac9dbSNicholas Bellinger " Port\n", pr_reg->pr_res_key); 1242e3d6f909SAndy Grover ret = -EPERM; 1243c66ac9dbSNicholas Bellinger } 1244c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 1245c66ac9dbSNicholas Bellinger 1246c66ac9dbSNicholas Bellinger return ret; 1247c66ac9dbSNicholas Bellinger } 1248c66ac9dbSNicholas Bellinger 1249c66ac9dbSNicholas Bellinger /* 1250e3d6f909SAndy Grover * Called with struct t10_reservation->registration_lock held. 1251c66ac9dbSNicholas Bellinger */ 1252c66ac9dbSNicholas Bellinger static void __core_scsi3_free_registration( 1253c66ac9dbSNicholas Bellinger struct se_device *dev, 1254c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, 1255c66ac9dbSNicholas Bellinger struct list_head *preempt_and_abort_list, 1256c66ac9dbSNicholas Bellinger int dec_holders) 1257cb0df4d3SBart Van Assche __releases(&pr_tmpl->registration_lock) 1258cb0df4d3SBart Van Assche __acquires(&pr_tmpl->registration_lock) 1259c66ac9dbSNicholas Bellinger { 12609ac8928eSChristoph Hellwig const struct target_core_fabric_ops *tfo = 1261c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo; 12620fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 126380bfdfa9SNicholas Bellinger struct se_node_acl *nacl = pr_reg->pr_reg_nacl; 126480bfdfa9SNicholas Bellinger struct se_dev_entry *deve; 1265c66ac9dbSNicholas Bellinger char i_buf[PR_REG_ISID_ID_LEN]; 1266c66ac9dbSNicholas Bellinger 1267c66ac9dbSNicholas Bellinger memset(i_buf, 0, PR_REG_ISID_ID_LEN); 1268d2843c17SAndy Grover core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); 1269c66ac9dbSNicholas Bellinger 12706c3c9baaSNicholas Bellinger if (!list_empty(&pr_reg->pr_reg_list)) 1271c66ac9dbSNicholas Bellinger list_del(&pr_reg->pr_reg_list); 1272c66ac9dbSNicholas Bellinger /* 1273c66ac9dbSNicholas Bellinger * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(), 1274c66ac9dbSNicholas Bellinger * so call core_scsi3_put_pr_reg() to decrement our reference. 1275c66ac9dbSNicholas Bellinger */ 1276c66ac9dbSNicholas Bellinger if (dec_holders) 1277c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg); 127880bfdfa9SNicholas Bellinger 127980bfdfa9SNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 1280c66ac9dbSNicholas Bellinger /* 1281c66ac9dbSNicholas Bellinger * Wait until all reference from any other I_T nexuses for this 1282c66ac9dbSNicholas Bellinger * *pr_reg have been released. Because list_del() is called above, 1283c66ac9dbSNicholas Bellinger * the last core_scsi3_put_pr_reg(pr_reg) will release this reference 1284c66ac9dbSNicholas Bellinger * count back to zero, and we release *pr_reg. 1285c66ac9dbSNicholas Bellinger */ 1286c66ac9dbSNicholas Bellinger while (atomic_read(&pr_reg->pr_res_holders) != 0) { 12876708bb27SAndy Grover pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n", 1288c66ac9dbSNicholas Bellinger tfo->get_fabric_name()); 1289c66ac9dbSNicholas Bellinger cpu_relax(); 1290c66ac9dbSNicholas Bellinger } 1291c66ac9dbSNicholas Bellinger 129280bfdfa9SNicholas Bellinger rcu_read_lock(); 129380bfdfa9SNicholas Bellinger deve = target_nacl_find_deve(nacl, pr_reg->pr_res_mapped_lun); 129480bfdfa9SNicholas Bellinger if (deve) 129580bfdfa9SNicholas Bellinger clear_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags); 129680bfdfa9SNicholas Bellinger rcu_read_unlock(); 129780bfdfa9SNicholas Bellinger 129880bfdfa9SNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 12996708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator" 1300c66ac9dbSNicholas Bellinger " Node: %s%s\n", tfo->get_fabric_name(), 1301c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl->initiatorname, 1302d2843c17SAndy Grover i_buf); 13036708bb27SAndy Grover pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target" 1304c66ac9dbSNicholas Bellinger " Port(s)\n", tfo->get_fabric_name(), 1305c66ac9dbSNicholas Bellinger (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE", 1306e3d6f909SAndy Grover dev->transport->name); 13076708bb27SAndy Grover pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:" 1308c66ac9dbSNicholas Bellinger " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key, 1309c66ac9dbSNicholas Bellinger pr_reg->pr_res_generation); 1310c66ac9dbSNicholas Bellinger 13116708bb27SAndy Grover if (!preempt_and_abort_list) { 1312c66ac9dbSNicholas Bellinger pr_reg->pr_reg_deve = NULL; 1313c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl = NULL; 1314c66ac9dbSNicholas Bellinger kmem_cache_free(t10_pr_reg_cache, pr_reg); 1315c66ac9dbSNicholas Bellinger return; 1316c66ac9dbSNicholas Bellinger } 1317c66ac9dbSNicholas Bellinger /* 1318c66ac9dbSNicholas Bellinger * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list 1319c66ac9dbSNicholas Bellinger * are released once the ABORT_TASK_SET has completed.. 1320c66ac9dbSNicholas Bellinger */ 1321c66ac9dbSNicholas Bellinger list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list); 1322c66ac9dbSNicholas Bellinger } 1323c66ac9dbSNicholas Bellinger 1324c66ac9dbSNicholas Bellinger void core_scsi3_free_pr_reg_from_nacl( 1325c66ac9dbSNicholas Bellinger struct se_device *dev, 1326c66ac9dbSNicholas Bellinger struct se_node_acl *nacl) 1327c66ac9dbSNicholas Bellinger { 13280fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 1329c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder; 13306c3c9baaSNicholas Bellinger bool free_reg = false; 1331c66ac9dbSNicholas Bellinger /* 1332c66ac9dbSNicholas Bellinger * If the passed se_node_acl matches the reservation holder, 1333c66ac9dbSNicholas Bellinger * release the reservation. 1334c66ac9dbSNicholas Bellinger */ 1335c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 1336c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 1337c66ac9dbSNicholas Bellinger if ((pr_res_holder != NULL) && 13386c3c9baaSNicholas Bellinger (pr_res_holder->pr_reg_nacl == nacl)) { 13396c3c9baaSNicholas Bellinger __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0, 1); 13406c3c9baaSNicholas Bellinger free_reg = true; 13416c3c9baaSNicholas Bellinger } 1342c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 1343c66ac9dbSNicholas Bellinger /* 1344c66ac9dbSNicholas Bellinger * Release any registration associated with the struct se_node_acl. 1345c66ac9dbSNicholas Bellinger */ 1346c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 13476c3c9baaSNicholas Bellinger if (pr_res_holder && free_reg) 13486c3c9baaSNicholas Bellinger __core_scsi3_free_registration(dev, pr_res_holder, NULL, 0); 13496c3c9baaSNicholas Bellinger 1350c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 1351c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 1352c66ac9dbSNicholas Bellinger 1353c66ac9dbSNicholas Bellinger if (pr_reg->pr_reg_nacl != nacl) 1354c66ac9dbSNicholas Bellinger continue; 1355c66ac9dbSNicholas Bellinger 1356c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, pr_reg, NULL, 0); 1357c66ac9dbSNicholas Bellinger } 1358c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 1359c66ac9dbSNicholas Bellinger } 1360c66ac9dbSNicholas Bellinger 1361c66ac9dbSNicholas Bellinger void core_scsi3_free_all_registrations( 1362c66ac9dbSNicholas Bellinger struct se_device *dev) 1363c66ac9dbSNicholas Bellinger { 13640fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 1365c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder; 1366c66ac9dbSNicholas Bellinger 1367c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 1368c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 1369c66ac9dbSNicholas Bellinger if (pr_res_holder != NULL) { 1370c66ac9dbSNicholas Bellinger struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; 1371c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_release(dev, pr_res_nacl, 13726c3c9baaSNicholas Bellinger pr_res_holder, 0, 0); 1373c66ac9dbSNicholas Bellinger } 1374c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 1375c66ac9dbSNicholas Bellinger 1376c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 1377c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 1378c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 1379c66ac9dbSNicholas Bellinger 1380c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, pr_reg, NULL, 0); 1381c66ac9dbSNicholas Bellinger } 1382c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 1383c66ac9dbSNicholas Bellinger 1384c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->aptpl_reg_lock); 1385c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list, 1386c66ac9dbSNicholas Bellinger pr_reg_aptpl_list) { 1387c66ac9dbSNicholas Bellinger list_del(&pr_reg->pr_reg_aptpl_list); 1388c66ac9dbSNicholas Bellinger kmem_cache_free(t10_pr_reg_cache, pr_reg); 1389c66ac9dbSNicholas Bellinger } 1390c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->aptpl_reg_lock); 1391c66ac9dbSNicholas Bellinger } 1392c66ac9dbSNicholas Bellinger 1393c66ac9dbSNicholas Bellinger static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg) 1394c66ac9dbSNicholas Bellinger { 1395d588cf8fSChristoph Hellwig return target_depend_item(&tpg->tpg_group.cg_item); 1396c66ac9dbSNicholas Bellinger } 1397c66ac9dbSNicholas Bellinger 1398c66ac9dbSNicholas Bellinger static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg) 1399c66ac9dbSNicholas Bellinger { 1400d588cf8fSChristoph Hellwig target_undepend_item(&tpg->tpg_group.cg_item); 140133940d09SJoern Engel atomic_dec_mb(&tpg->tpg_pr_ref_count); 1402c66ac9dbSNicholas Bellinger } 1403c66ac9dbSNicholas Bellinger 1404c66ac9dbSNicholas Bellinger static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl) 1405c66ac9dbSNicholas Bellinger { 1406c66ac9dbSNicholas Bellinger if (nacl->dynamic_node_acl) 1407c66ac9dbSNicholas Bellinger return 0; 1408d588cf8fSChristoph Hellwig return target_depend_item(&nacl->acl_group.cg_item); 1409c66ac9dbSNicholas Bellinger } 1410c66ac9dbSNicholas Bellinger 1411c66ac9dbSNicholas Bellinger static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl) 1412c66ac9dbSNicholas Bellinger { 1413d588cf8fSChristoph Hellwig if (!nacl->dynamic_node_acl) 1414d588cf8fSChristoph Hellwig target_undepend_item(&nacl->acl_group.cg_item); 141533940d09SJoern Engel atomic_dec_mb(&nacl->acl_pr_ref_count); 1416c66ac9dbSNicholas Bellinger } 1417c66ac9dbSNicholas Bellinger 1418c66ac9dbSNicholas Bellinger static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve) 1419c66ac9dbSNicholas Bellinger { 142079dc9c9eSNicholas Bellinger struct se_lun_acl *lun_acl; 1421c66ac9dbSNicholas Bellinger struct se_node_acl *nacl; 1422c66ac9dbSNicholas Bellinger struct se_portal_group *tpg; 1423c66ac9dbSNicholas Bellinger /* 1424c66ac9dbSNicholas Bellinger * For nacl->dynamic_node_acl=1 1425c66ac9dbSNicholas Bellinger */ 142679dc9c9eSNicholas Bellinger lun_acl = rcu_dereference_check(se_deve->se_lun_acl, 142779dc9c9eSNicholas Bellinger atomic_read(&se_deve->pr_kref.refcount) != 0); 14286708bb27SAndy Grover if (!lun_acl) 1429c66ac9dbSNicholas Bellinger return 0; 1430c66ac9dbSNicholas Bellinger 1431c66ac9dbSNicholas Bellinger nacl = lun_acl->se_lun_nacl; 1432c66ac9dbSNicholas Bellinger tpg = nacl->se_tpg; 1433c66ac9dbSNicholas Bellinger 1434d588cf8fSChristoph Hellwig return target_depend_item(&lun_acl->se_lun_group.cg_item); 1435c66ac9dbSNicholas Bellinger } 1436c66ac9dbSNicholas Bellinger 1437c66ac9dbSNicholas Bellinger static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve) 1438c66ac9dbSNicholas Bellinger { 143929a05deeSNicholas Bellinger struct se_lun_acl *lun_acl; 1440c66ac9dbSNicholas Bellinger struct se_node_acl *nacl; 1441c66ac9dbSNicholas Bellinger struct se_portal_group *tpg; 1442c66ac9dbSNicholas Bellinger /* 1443c66ac9dbSNicholas Bellinger * For nacl->dynamic_node_acl=1 1444c66ac9dbSNicholas Bellinger */ 144579dc9c9eSNicholas Bellinger lun_acl = rcu_dereference_check(se_deve->se_lun_acl, 144679dc9c9eSNicholas Bellinger atomic_read(&se_deve->pr_kref.refcount) != 0); 14476708bb27SAndy Grover if (!lun_acl) { 144829a05deeSNicholas Bellinger kref_put(&se_deve->pr_kref, target_pr_kref_release); 1449c66ac9dbSNicholas Bellinger return; 1450c66ac9dbSNicholas Bellinger } 1451c66ac9dbSNicholas Bellinger nacl = lun_acl->se_lun_nacl; 1452c66ac9dbSNicholas Bellinger tpg = nacl->se_tpg; 1453c66ac9dbSNicholas Bellinger 1454d588cf8fSChristoph Hellwig target_undepend_item(&lun_acl->se_lun_group.cg_item); 145529a05deeSNicholas Bellinger kref_put(&se_deve->pr_kref, target_pr_kref_release); 1456c66ac9dbSNicholas Bellinger } 1457c66ac9dbSNicholas Bellinger 1458de103c93SChristoph Hellwig static sense_reason_t 1459de103c93SChristoph Hellwig core_scsi3_decode_spec_i_port( 1460c66ac9dbSNicholas Bellinger struct se_cmd *cmd, 1461c66ac9dbSNicholas Bellinger struct se_portal_group *tpg, 1462c66ac9dbSNicholas Bellinger unsigned char *l_isid, 1463c66ac9dbSNicholas Bellinger u64 sa_res_key, 1464c66ac9dbSNicholas Bellinger int all_tg_pt, 1465c66ac9dbSNicholas Bellinger int aptpl) 1466c66ac9dbSNicholas Bellinger { 14675951146dSAndy Grover struct se_device *dev = cmd->se_dev; 1468c66ac9dbSNicholas Bellinger struct se_portal_group *dest_tpg = NULL, *tmp_tpg; 1469e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 1470c66ac9dbSNicholas Bellinger struct se_node_acl *dest_node_acl = NULL; 147179dc9c9eSNicholas Bellinger struct se_dev_entry *dest_se_deve = NULL; 1472c66ac9dbSNicholas Bellinger struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e; 1473c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe; 1474d0f474e5SRoland Dreier LIST_HEAD(tid_dest_list); 1475c66ac9dbSNicholas Bellinger struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp; 14762650d71eSChristoph Hellwig unsigned char *buf, *ptr, proto_ident; 14772650d71eSChristoph Hellwig const unsigned char *i_str; 147813ba564cSJulia Lawall char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN]; 1479de103c93SChristoph Hellwig sense_reason_t ret; 1480c66ac9dbSNicholas Bellinger u32 tpdl, tid_len = 0; 1481c66ac9dbSNicholas Bellinger u32 dest_rtpi = 0; 1482c66ac9dbSNicholas Bellinger 1483c66ac9dbSNicholas Bellinger /* 1484c66ac9dbSNicholas Bellinger * Allocate a struct pr_transport_id_holder and setup the 148579dc9c9eSNicholas Bellinger * local_node_acl pointer and add to struct list_head tid_dest_list 148679dc9c9eSNicholas Bellinger * for add registration processing in the loop of tid_dest_list below. 1487c66ac9dbSNicholas Bellinger */ 1488c66ac9dbSNicholas Bellinger tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL); 14896708bb27SAndy Grover if (!tidh_new) { 14906708bb27SAndy Grover pr_err("Unable to allocate tidh_new\n"); 1491de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1492c66ac9dbSNicholas Bellinger } 1493c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&tidh_new->dest_list); 1494c66ac9dbSNicholas Bellinger tidh_new->dest_tpg = tpg; 1495c66ac9dbSNicholas Bellinger tidh_new->dest_node_acl = se_sess->se_node_acl; 1496c66ac9dbSNicholas Bellinger 14975951146dSAndy Grover local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev, 149879dc9c9eSNicholas Bellinger se_sess->se_node_acl, cmd->se_lun, 149979dc9c9eSNicholas Bellinger NULL, cmd->orig_fe_lun, l_isid, 1500c66ac9dbSNicholas Bellinger sa_res_key, all_tg_pt, aptpl); 15016708bb27SAndy Grover if (!local_pr_reg) { 1502c66ac9dbSNicholas Bellinger kfree(tidh_new); 1503de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1504c66ac9dbSNicholas Bellinger } 1505c66ac9dbSNicholas Bellinger tidh_new->dest_pr_reg = local_pr_reg; 1506c66ac9dbSNicholas Bellinger /* 1507c66ac9dbSNicholas Bellinger * The local I_T nexus does not hold any configfs dependances, 150879dc9c9eSNicholas Bellinger * so we set tidh_new->dest_se_deve to NULL to prevent the 1509c66ac9dbSNicholas Bellinger * configfs_undepend_item() calls in the tid_dest_list loops below. 1510c66ac9dbSNicholas Bellinger */ 151179dc9c9eSNicholas Bellinger tidh_new->dest_se_deve = NULL; 1512c66ac9dbSNicholas Bellinger list_add_tail(&tidh_new->dest_list, &tid_dest_list); 151305d1c7c0SAndy Grover 15140d7f1299SPaolo Bonzini if (cmd->data_length < 28) { 15150d7f1299SPaolo Bonzini pr_warn("SPC-PR: Received PR OUT parameter list" 15160d7f1299SPaolo Bonzini " length too small: %u\n", cmd->data_length); 1517de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 15180d7f1299SPaolo Bonzini goto out; 15190d7f1299SPaolo Bonzini } 15200d7f1299SPaolo Bonzini 15214949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 1522de103c93SChristoph Hellwig if (!buf) { 1523de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1524de103c93SChristoph Hellwig goto out; 1525de103c93SChristoph Hellwig } 1526de103c93SChristoph Hellwig 1527c66ac9dbSNicholas Bellinger /* 1528c66ac9dbSNicholas Bellinger * For a PERSISTENT RESERVE OUT specify initiator ports payload, 1529c66ac9dbSNicholas Bellinger * first extract TransportID Parameter Data Length, and make sure 1530c66ac9dbSNicholas Bellinger * the value matches up to the SCSI expected data transfer length. 1531c66ac9dbSNicholas Bellinger */ 1532c66ac9dbSNicholas Bellinger tpdl = (buf[24] & 0xff) << 24; 1533c66ac9dbSNicholas Bellinger tpdl |= (buf[25] & 0xff) << 16; 1534c66ac9dbSNicholas Bellinger tpdl |= (buf[26] & 0xff) << 8; 1535c66ac9dbSNicholas Bellinger tpdl |= buf[27] & 0xff; 1536c66ac9dbSNicholas Bellinger 1537c66ac9dbSNicholas Bellinger if ((tpdl + 28) != cmd->data_length) { 15386708bb27SAndy Grover pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header" 1539c66ac9dbSNicholas Bellinger " does not equal CDB data_length: %u\n", tpdl, 1540c66ac9dbSNicholas Bellinger cmd->data_length); 1541de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 1542de103c93SChristoph Hellwig goto out_unmap; 1543c66ac9dbSNicholas Bellinger } 1544c66ac9dbSNicholas Bellinger /* 1545c66ac9dbSNicholas Bellinger * Start processing the received transport IDs using the 1546c66ac9dbSNicholas Bellinger * receiving I_T Nexus portal's fabric dependent methods to 1547c66ac9dbSNicholas Bellinger * obtain the SCSI Initiator Port/Device Identifiers. 1548c66ac9dbSNicholas Bellinger */ 1549c66ac9dbSNicholas Bellinger ptr = &buf[28]; 1550c66ac9dbSNicholas Bellinger 1551c66ac9dbSNicholas Bellinger while (tpdl > 0) { 1552adf653f9SChristoph Hellwig struct se_lun *dest_lun, *tmp_lun; 155379dc9c9eSNicholas Bellinger 1554c66ac9dbSNicholas Bellinger proto_ident = (ptr[0] & 0x0f); 1555c66ac9dbSNicholas Bellinger dest_tpg = NULL; 1556c66ac9dbSNicholas Bellinger 1557c66ac9dbSNicholas Bellinger spin_lock(&dev->se_port_lock); 1558adf653f9SChristoph Hellwig list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) { 1559adf653f9SChristoph Hellwig tmp_tpg = tmp_lun->lun_tpg; 15602650d71eSChristoph Hellwig 1561c66ac9dbSNicholas Bellinger /* 1562c66ac9dbSNicholas Bellinger * Look for the matching proto_ident provided by 1563c66ac9dbSNicholas Bellinger * the received TransportID 1564c66ac9dbSNicholas Bellinger */ 15652aeeafaeSChristoph Hellwig if (tmp_tpg->proto_id != proto_ident) 1566c66ac9dbSNicholas Bellinger continue; 1567adf653f9SChristoph Hellwig dest_rtpi = tmp_lun->lun_rtpi; 1568c66ac9dbSNicholas Bellinger 15692650d71eSChristoph Hellwig i_str = target_parse_pr_out_transport_id(tmp_tpg, 15702650d71eSChristoph Hellwig (const char *)ptr, &tid_len, &iport_ptr); 15716708bb27SAndy Grover if (!i_str) 1572c66ac9dbSNicholas Bellinger continue; 1573c66ac9dbSNicholas Bellinger 157433940d09SJoern Engel atomic_inc_mb(&tmp_tpg->tpg_pr_ref_count); 1575c66ac9dbSNicholas Bellinger spin_unlock(&dev->se_port_lock); 1576c66ac9dbSNicholas Bellinger 1577de103c93SChristoph Hellwig if (core_scsi3_tpg_depend_item(tmp_tpg)) { 15786708bb27SAndy Grover pr_err(" core_scsi3_tpg_depend_item()" 1579c66ac9dbSNicholas Bellinger " for tmp_tpg\n"); 158033940d09SJoern Engel atomic_dec_mb(&tmp_tpg->tpg_pr_ref_count); 1581de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1582de103c93SChristoph Hellwig goto out_unmap; 1583c66ac9dbSNicholas Bellinger } 1584c66ac9dbSNicholas Bellinger /* 158535d1efe8SMasanari Iida * Locate the destination initiator ACL to be registered 1586c66ac9dbSNicholas Bellinger * from the decoded fabric module specific TransportID 1587c66ac9dbSNicholas Bellinger * at *i_str. 1588c66ac9dbSNicholas Bellinger */ 1589403edd78SNicholas Bellinger mutex_lock(&tmp_tpg->acl_node_mutex); 1590c66ac9dbSNicholas Bellinger dest_node_acl = __core_tpg_get_initiator_node_acl( 1591c66ac9dbSNicholas Bellinger tmp_tpg, i_str); 159233940d09SJoern Engel if (dest_node_acl) 159333940d09SJoern Engel atomic_inc_mb(&dest_node_acl->acl_pr_ref_count); 1594403edd78SNicholas Bellinger mutex_unlock(&tmp_tpg->acl_node_mutex); 1595c66ac9dbSNicholas Bellinger 15966708bb27SAndy Grover if (!dest_node_acl) { 1597c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(tmp_tpg); 1598c66ac9dbSNicholas Bellinger spin_lock(&dev->se_port_lock); 1599c66ac9dbSNicholas Bellinger continue; 1600c66ac9dbSNicholas Bellinger } 1601c66ac9dbSNicholas Bellinger 1602de103c93SChristoph Hellwig if (core_scsi3_nodeacl_depend_item(dest_node_acl)) { 16036708bb27SAndy Grover pr_err("configfs_depend_item() failed" 1604c66ac9dbSNicholas Bellinger " for dest_node_acl->acl_group\n"); 160533940d09SJoern Engel atomic_dec_mb(&dest_node_acl->acl_pr_ref_count); 1606c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(tmp_tpg); 1607de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1608de103c93SChristoph Hellwig goto out_unmap; 1609c66ac9dbSNicholas Bellinger } 1610c66ac9dbSNicholas Bellinger 1611c66ac9dbSNicholas Bellinger dest_tpg = tmp_tpg; 16126708bb27SAndy Grover pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:" 1613c66ac9dbSNicholas Bellinger " %s Port RTPI: %hu\n", 1614e3d6f909SAndy Grover dest_tpg->se_tpg_tfo->get_fabric_name(), 1615c66ac9dbSNicholas Bellinger dest_node_acl->initiatorname, dest_rtpi); 1616c66ac9dbSNicholas Bellinger 1617c66ac9dbSNicholas Bellinger spin_lock(&dev->se_port_lock); 1618c66ac9dbSNicholas Bellinger break; 1619c66ac9dbSNicholas Bellinger } 1620c66ac9dbSNicholas Bellinger spin_unlock(&dev->se_port_lock); 1621c66ac9dbSNicholas Bellinger 16226708bb27SAndy Grover if (!dest_tpg) { 16236708bb27SAndy Grover pr_err("SPC-3 PR SPEC_I_PT: Unable to locate" 1624c66ac9dbSNicholas Bellinger " dest_tpg\n"); 1625de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 1626de103c93SChristoph Hellwig goto out_unmap; 1627c66ac9dbSNicholas Bellinger } 16288b1e1244SAndy Grover 16296708bb27SAndy Grover pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u" 1630c66ac9dbSNicholas Bellinger " tid_len: %d for %s + %s\n", 1631e3d6f909SAndy Grover dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length, 1632c66ac9dbSNicholas Bellinger tpdl, tid_len, i_str, iport_ptr); 16338b1e1244SAndy Grover 1634c66ac9dbSNicholas Bellinger if (tid_len > tpdl) { 16356708bb27SAndy Grover pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:" 1636c66ac9dbSNicholas Bellinger " %u for Transport ID: %s\n", tid_len, ptr); 1637c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1638c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1639de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 1640de103c93SChristoph Hellwig goto out_unmap; 1641c66ac9dbSNicholas Bellinger } 1642c66ac9dbSNicholas Bellinger /* 1643c66ac9dbSNicholas Bellinger * Locate the desintation struct se_dev_entry pointer for matching 1644c66ac9dbSNicholas Bellinger * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus 1645c66ac9dbSNicholas Bellinger * Target Port. 1646c66ac9dbSNicholas Bellinger */ 1647c66ac9dbSNicholas Bellinger dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, 1648c66ac9dbSNicholas Bellinger dest_rtpi); 16496708bb27SAndy Grover if (!dest_se_deve) { 16506708bb27SAndy Grover pr_err("Unable to locate %s dest_se_deve" 1651c66ac9dbSNicholas Bellinger " from destination RTPI: %hu\n", 1652e3d6f909SAndy Grover dest_tpg->se_tpg_tfo->get_fabric_name(), 1653c66ac9dbSNicholas Bellinger dest_rtpi); 1654c66ac9dbSNicholas Bellinger 1655c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1656c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1657de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 1658de103c93SChristoph Hellwig goto out_unmap; 1659c66ac9dbSNicholas Bellinger } 1660c66ac9dbSNicholas Bellinger 1661de103c93SChristoph Hellwig if (core_scsi3_lunacl_depend_item(dest_se_deve)) { 16626708bb27SAndy Grover pr_err("core_scsi3_lunacl_depend_item()" 1663c66ac9dbSNicholas Bellinger " failed\n"); 166429a05deeSNicholas Bellinger kref_put(&dest_se_deve->pr_kref, target_pr_kref_release); 1665c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1666c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1667de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1668de103c93SChristoph Hellwig goto out_unmap; 1669c66ac9dbSNicholas Bellinger } 16708b1e1244SAndy Grover 16716708bb27SAndy Grover pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s" 1672f2d30680SHannes Reinecke " dest_se_deve mapped_lun: %llu\n", 1673e3d6f909SAndy Grover dest_tpg->se_tpg_tfo->get_fabric_name(), 1674c66ac9dbSNicholas Bellinger dest_node_acl->initiatorname, dest_se_deve->mapped_lun); 16758b1e1244SAndy Grover 1676c66ac9dbSNicholas Bellinger /* 1677c66ac9dbSNicholas Bellinger * Skip any TransportIDs that already have a registration for 1678c66ac9dbSNicholas Bellinger * this target port. 1679c66ac9dbSNicholas Bellinger */ 1680c66ac9dbSNicholas Bellinger pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl, 1681c66ac9dbSNicholas Bellinger iport_ptr); 1682c66ac9dbSNicholas Bellinger if (pr_reg_e) { 1683c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_e); 1684c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(dest_se_deve); 1685c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1686c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1687c66ac9dbSNicholas Bellinger ptr += tid_len; 1688c66ac9dbSNicholas Bellinger tpdl -= tid_len; 1689c66ac9dbSNicholas Bellinger tid_len = 0; 1690c66ac9dbSNicholas Bellinger continue; 1691c66ac9dbSNicholas Bellinger } 1692c66ac9dbSNicholas Bellinger /* 1693c66ac9dbSNicholas Bellinger * Allocate a struct pr_transport_id_holder and setup 1694c66ac9dbSNicholas Bellinger * the dest_node_acl and dest_se_deve pointers for the 1695c66ac9dbSNicholas Bellinger * loop below. 1696c66ac9dbSNicholas Bellinger */ 1697c66ac9dbSNicholas Bellinger tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), 1698c66ac9dbSNicholas Bellinger GFP_KERNEL); 16996708bb27SAndy Grover if (!tidh_new) { 17006708bb27SAndy Grover pr_err("Unable to allocate tidh_new\n"); 1701c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(dest_se_deve); 1702c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1703c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1704de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1705de103c93SChristoph Hellwig goto out_unmap; 1706c66ac9dbSNicholas Bellinger } 1707c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&tidh_new->dest_list); 1708c66ac9dbSNicholas Bellinger tidh_new->dest_tpg = dest_tpg; 1709c66ac9dbSNicholas Bellinger tidh_new->dest_node_acl = dest_node_acl; 1710c66ac9dbSNicholas Bellinger tidh_new->dest_se_deve = dest_se_deve; 1711c66ac9dbSNicholas Bellinger 1712c66ac9dbSNicholas Bellinger /* 1713c66ac9dbSNicholas Bellinger * Allocate, but do NOT add the registration for the 1714c66ac9dbSNicholas Bellinger * TransportID referenced SCSI Initiator port. This 1715c66ac9dbSNicholas Bellinger * done because of the following from spc4r17 in section 1716c66ac9dbSNicholas Bellinger * 6.14.3 wrt SPEC_I_PT: 1717c66ac9dbSNicholas Bellinger * 1718c66ac9dbSNicholas Bellinger * "If a registration fails for any initiator port (e.g., if th 1719c66ac9dbSNicholas Bellinger * logical unit does not have enough resources available to 1720c66ac9dbSNicholas Bellinger * hold the registration information), no registrations shall be 1721c66ac9dbSNicholas Bellinger * made, and the command shall be terminated with 1722c66ac9dbSNicholas Bellinger * CHECK CONDITION status." 1723c66ac9dbSNicholas Bellinger * 1724c66ac9dbSNicholas Bellinger * That means we call __core_scsi3_alloc_registration() here, 1725c66ac9dbSNicholas Bellinger * and then call __core_scsi3_add_registration() in the 1726c66ac9dbSNicholas Bellinger * 2nd loop which will never fail. 1727c66ac9dbSNicholas Bellinger */ 172879dc9c9eSNicholas Bellinger dest_lun = rcu_dereference_check(dest_se_deve->se_lun, 172979dc9c9eSNicholas Bellinger atomic_read(&dest_se_deve->pr_kref.refcount) != 0); 173079dc9c9eSNicholas Bellinger 17315951146dSAndy Grover dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev, 173279dc9c9eSNicholas Bellinger dest_node_acl, dest_lun, dest_se_deve, 173379dc9c9eSNicholas Bellinger dest_se_deve->mapped_lun, iport_ptr, 1734c66ac9dbSNicholas Bellinger sa_res_key, all_tg_pt, aptpl); 17356708bb27SAndy Grover if (!dest_pr_reg) { 1736c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(dest_se_deve); 1737c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1738c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1739c66ac9dbSNicholas Bellinger kfree(tidh_new); 1740de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 1741de103c93SChristoph Hellwig goto out_unmap; 1742c66ac9dbSNicholas Bellinger } 1743c66ac9dbSNicholas Bellinger tidh_new->dest_pr_reg = dest_pr_reg; 1744c66ac9dbSNicholas Bellinger list_add_tail(&tidh_new->dest_list, &tid_dest_list); 1745c66ac9dbSNicholas Bellinger 1746c66ac9dbSNicholas Bellinger ptr += tid_len; 1747c66ac9dbSNicholas Bellinger tpdl -= tid_len; 1748c66ac9dbSNicholas Bellinger tid_len = 0; 1749c66ac9dbSNicholas Bellinger 1750c66ac9dbSNicholas Bellinger } 175105d1c7c0SAndy Grover 17524949314cSAndy Grover transport_kunmap_data_sg(cmd); 175305d1c7c0SAndy Grover 1754c66ac9dbSNicholas Bellinger /* 1755c66ac9dbSNicholas Bellinger * Go ahead and create a registrations from tid_dest_list for the 1756c66ac9dbSNicholas Bellinger * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl 1757c66ac9dbSNicholas Bellinger * and dest_se_deve. 1758c66ac9dbSNicholas Bellinger * 1759c66ac9dbSNicholas Bellinger * The SA Reservation Key from the PROUT is set for the 1760c66ac9dbSNicholas Bellinger * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1 1761c66ac9dbSNicholas Bellinger * means that the TransportID Initiator port will be 1762c66ac9dbSNicholas Bellinger * registered on all of the target ports in the SCSI target device 1763c66ac9dbSNicholas Bellinger * ALL_TG_PT=0 means the registration will only be for the 1764c66ac9dbSNicholas Bellinger * SCSI target port the PROUT REGISTER with SPEC_I_PT=1 1765c66ac9dbSNicholas Bellinger * was received. 1766c66ac9dbSNicholas Bellinger */ 1767c66ac9dbSNicholas Bellinger list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) { 1768c66ac9dbSNicholas Bellinger dest_tpg = tidh->dest_tpg; 1769c66ac9dbSNicholas Bellinger dest_node_acl = tidh->dest_node_acl; 1770c66ac9dbSNicholas Bellinger dest_se_deve = tidh->dest_se_deve; 1771c66ac9dbSNicholas Bellinger dest_pr_reg = tidh->dest_pr_reg; 1772c66ac9dbSNicholas Bellinger 1773c66ac9dbSNicholas Bellinger list_del(&tidh->dest_list); 1774c66ac9dbSNicholas Bellinger kfree(tidh); 1775c66ac9dbSNicholas Bellinger 1776c66ac9dbSNicholas Bellinger memset(i_buf, 0, PR_REG_ISID_ID_LEN); 1777d2843c17SAndy Grover core_pr_dump_initiator_port(dest_pr_reg, i_buf, PR_REG_ISID_ID_LEN); 1778c66ac9dbSNicholas Bellinger 17795951146dSAndy Grover __core_scsi3_add_registration(cmd->se_dev, dest_node_acl, 1780c66ac9dbSNicholas Bellinger dest_pr_reg, 0, 0); 1781c66ac9dbSNicholas Bellinger 17826708bb27SAndy Grover pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully" 1783c66ac9dbSNicholas Bellinger " registered Transport ID for Node: %s%s Mapped LUN:" 1784f2d30680SHannes Reinecke " %llu\n", dest_tpg->se_tpg_tfo->get_fabric_name(), 178579dc9c9eSNicholas Bellinger dest_node_acl->initiatorname, i_buf, (dest_se_deve) ? 178679dc9c9eSNicholas Bellinger dest_se_deve->mapped_lun : 0); 1787c66ac9dbSNicholas Bellinger 178879dc9c9eSNicholas Bellinger if (!dest_se_deve) 1789c66ac9dbSNicholas Bellinger continue; 1790c66ac9dbSNicholas Bellinger 1791c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(dest_se_deve); 1792c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1793c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1794c66ac9dbSNicholas Bellinger } 1795c66ac9dbSNicholas Bellinger 1796c66ac9dbSNicholas Bellinger return 0; 1797de103c93SChristoph Hellwig out_unmap: 17984949314cSAndy Grover transport_kunmap_data_sg(cmd); 1799de103c93SChristoph Hellwig out: 1800c66ac9dbSNicholas Bellinger /* 1801c66ac9dbSNicholas Bellinger * For the failure case, release everything from tid_dest_list 1802c66ac9dbSNicholas Bellinger * including *dest_pr_reg and the configfs dependances.. 1803c66ac9dbSNicholas Bellinger */ 1804c66ac9dbSNicholas Bellinger list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) { 1805c66ac9dbSNicholas Bellinger dest_tpg = tidh->dest_tpg; 1806c66ac9dbSNicholas Bellinger dest_node_acl = tidh->dest_node_acl; 1807c66ac9dbSNicholas Bellinger dest_se_deve = tidh->dest_se_deve; 1808c66ac9dbSNicholas Bellinger dest_pr_reg = tidh->dest_pr_reg; 1809c66ac9dbSNicholas Bellinger 1810c66ac9dbSNicholas Bellinger list_del(&tidh->dest_list); 1811c66ac9dbSNicholas Bellinger kfree(tidh); 1812c66ac9dbSNicholas Bellinger /* 1813c66ac9dbSNicholas Bellinger * Release any extra ALL_TG_PT=1 registrations for 1814c66ac9dbSNicholas Bellinger * the SPEC_I_PT=1 case. 1815c66ac9dbSNicholas Bellinger */ 1816c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe, 1817c66ac9dbSNicholas Bellinger &dest_pr_reg->pr_reg_atp_list, 1818c66ac9dbSNicholas Bellinger pr_reg_atp_mem_list) { 1819c66ac9dbSNicholas Bellinger list_del(&pr_reg_tmp->pr_reg_atp_mem_list); 1820c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve); 1821c66ac9dbSNicholas Bellinger kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp); 1822c66ac9dbSNicholas Bellinger } 1823c66ac9dbSNicholas Bellinger 1824c66ac9dbSNicholas Bellinger kmem_cache_free(t10_pr_reg_cache, dest_pr_reg); 1825c66ac9dbSNicholas Bellinger 182679dc9c9eSNicholas Bellinger if (!dest_se_deve) 1827c66ac9dbSNicholas Bellinger continue; 1828c66ac9dbSNicholas Bellinger 1829c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(dest_se_deve); 1830c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1831c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1832c66ac9dbSNicholas Bellinger } 1833c66ac9dbSNicholas Bellinger return ret; 1834c66ac9dbSNicholas Bellinger } 1835c66ac9dbSNicholas Bellinger 18363c8a6228SAndy Grover static int core_scsi3_update_aptpl_buf( 1837c66ac9dbSNicholas Bellinger struct se_device *dev, 1838c66ac9dbSNicholas Bellinger unsigned char *buf, 18390607decdSAndy Grover u32 pr_aptpl_buf_len) 1840c66ac9dbSNicholas Bellinger { 1841c66ac9dbSNicholas Bellinger struct se_portal_group *tpg; 1842c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg; 1843c66ac9dbSNicholas Bellinger unsigned char tmp[512], isid_buf[32]; 1844c66ac9dbSNicholas Bellinger ssize_t len = 0; 1845c66ac9dbSNicholas Bellinger int reg_count = 0; 18463c8a6228SAndy Grover int ret = 0; 1847c66ac9dbSNicholas Bellinger 18483c8a6228SAndy Grover spin_lock(&dev->dev_reservation_lock); 18493c8a6228SAndy Grover spin_lock(&dev->t10_pr.registration_lock); 1850c66ac9dbSNicholas Bellinger /* 1851c66ac9dbSNicholas Bellinger * Walk the registration list.. 1852c66ac9dbSNicholas Bellinger */ 18530fd97ccfSChristoph Hellwig list_for_each_entry(pr_reg, &dev->t10_pr.registration_list, 1854c66ac9dbSNicholas Bellinger pr_reg_list) { 1855c66ac9dbSNicholas Bellinger 1856c66ac9dbSNicholas Bellinger tmp[0] = '\0'; 1857c66ac9dbSNicholas Bellinger isid_buf[0] = '\0'; 1858c66ac9dbSNicholas Bellinger tpg = pr_reg->pr_reg_nacl->se_tpg; 1859c66ac9dbSNicholas Bellinger /* 1860c66ac9dbSNicholas Bellinger * Write out any ISID value to APTPL metadata that was included 1861c66ac9dbSNicholas Bellinger * in the original registration. 1862c66ac9dbSNicholas Bellinger */ 1863c66ac9dbSNicholas Bellinger if (pr_reg->isid_present_at_reg) 1864c66ac9dbSNicholas Bellinger snprintf(isid_buf, 32, "initiator_sid=%s\n", 1865c66ac9dbSNicholas Bellinger pr_reg->pr_reg_isid); 1866c66ac9dbSNicholas Bellinger /* 1867c66ac9dbSNicholas Bellinger * Include special metadata if the pr_reg matches the 1868c66ac9dbSNicholas Bellinger * reservation holder. 1869c66ac9dbSNicholas Bellinger */ 1870c66ac9dbSNicholas Bellinger if (dev->dev_pr_res_holder == pr_reg) { 1871c66ac9dbSNicholas Bellinger snprintf(tmp, 512, "PR_REG_START: %d" 1872c66ac9dbSNicholas Bellinger "\ninitiator_fabric=%s\n" 1873c66ac9dbSNicholas Bellinger "initiator_node=%s\n%s" 1874c66ac9dbSNicholas Bellinger "sa_res_key=%llu\n" 1875c66ac9dbSNicholas Bellinger "res_holder=1\nres_type=%02x\n" 1876c66ac9dbSNicholas Bellinger "res_scope=%02x\nres_all_tg_pt=%d\n" 1877f2d30680SHannes Reinecke "mapped_lun=%llu\n", reg_count, 1878e3d6f909SAndy Grover tpg->se_tpg_tfo->get_fabric_name(), 1879c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl->initiatorname, isid_buf, 1880c66ac9dbSNicholas Bellinger pr_reg->pr_res_key, pr_reg->pr_res_type, 1881c66ac9dbSNicholas Bellinger pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt, 1882c66ac9dbSNicholas Bellinger pr_reg->pr_res_mapped_lun); 1883c66ac9dbSNicholas Bellinger } else { 1884c66ac9dbSNicholas Bellinger snprintf(tmp, 512, "PR_REG_START: %d\n" 1885c66ac9dbSNicholas Bellinger "initiator_fabric=%s\ninitiator_node=%s\n%s" 1886c66ac9dbSNicholas Bellinger "sa_res_key=%llu\nres_holder=0\n" 1887f2d30680SHannes Reinecke "res_all_tg_pt=%d\nmapped_lun=%llu\n", 1888e3d6f909SAndy Grover reg_count, tpg->se_tpg_tfo->get_fabric_name(), 1889c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl->initiatorname, isid_buf, 1890c66ac9dbSNicholas Bellinger pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt, 1891c66ac9dbSNicholas Bellinger pr_reg->pr_res_mapped_lun); 1892c66ac9dbSNicholas Bellinger } 1893c66ac9dbSNicholas Bellinger 189460d645a4SDan Carpenter if ((len + strlen(tmp) >= pr_aptpl_buf_len)) { 1895f161d4b4SNicholas Bellinger pr_err("Unable to update renaming APTPL metadata," 1896f161d4b4SNicholas Bellinger " reallocating larger buffer\n"); 18973c8a6228SAndy Grover ret = -EMSGSIZE; 18983c8a6228SAndy Grover goto out; 1899c66ac9dbSNicholas Bellinger } 1900c66ac9dbSNicholas Bellinger len += sprintf(buf+len, "%s", tmp); 1901c66ac9dbSNicholas Bellinger 1902c66ac9dbSNicholas Bellinger /* 1903c66ac9dbSNicholas Bellinger * Include information about the associated SCSI target port. 1904c66ac9dbSNicholas Bellinger */ 1905c66ac9dbSNicholas Bellinger snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n" 1906f2d30680SHannes Reinecke "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%llu\nPR_REG_END:" 1907e3d6f909SAndy Grover " %d\n", tpg->se_tpg_tfo->get_fabric_name(), 1908e3d6f909SAndy Grover tpg->se_tpg_tfo->tpg_get_wwn(tpg), 1909e3d6f909SAndy Grover tpg->se_tpg_tfo->tpg_get_tag(tpg), 191079dc9c9eSNicholas Bellinger pr_reg->tg_pt_sep_rtpi, pr_reg->pr_aptpl_target_lun, 191179dc9c9eSNicholas Bellinger reg_count); 1912c66ac9dbSNicholas Bellinger 191360d645a4SDan Carpenter if ((len + strlen(tmp) >= pr_aptpl_buf_len)) { 1914f161d4b4SNicholas Bellinger pr_err("Unable to update renaming APTPL metadata," 1915f161d4b4SNicholas Bellinger " reallocating larger buffer\n"); 19163c8a6228SAndy Grover ret = -EMSGSIZE; 19173c8a6228SAndy Grover goto out; 1918c66ac9dbSNicholas Bellinger } 1919c66ac9dbSNicholas Bellinger len += sprintf(buf+len, "%s", tmp); 1920c66ac9dbSNicholas Bellinger reg_count++; 1921c66ac9dbSNicholas Bellinger } 1922c66ac9dbSNicholas Bellinger 19236708bb27SAndy Grover if (!reg_count) 1924c66ac9dbSNicholas Bellinger len += sprintf(buf+len, "No Registrations or Reservations"); 1925c66ac9dbSNicholas Bellinger 19263c8a6228SAndy Grover out: 19273c8a6228SAndy Grover spin_unlock(&dev->t10_pr.registration_lock); 1928c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 1929c66ac9dbSNicholas Bellinger 1930c66ac9dbSNicholas Bellinger return ret; 1931c66ac9dbSNicholas Bellinger } 1932c66ac9dbSNicholas Bellinger 1933c66ac9dbSNicholas Bellinger static int __core_scsi3_write_aptpl_to_file( 1934c66ac9dbSNicholas Bellinger struct se_device *dev, 19354dee96fbSAndy Grover unsigned char *buf) 1936c66ac9dbSNicholas Bellinger { 19370fd97ccfSChristoph Hellwig struct t10_wwn *wwn = &dev->t10_wwn; 1938c66ac9dbSNicholas Bellinger struct file *file; 1939c66ac9dbSNicholas Bellinger int flags = O_RDWR | O_CREAT | O_TRUNC; 1940c66ac9dbSNicholas Bellinger char path[512]; 19414dee96fbSAndy Grover u32 pr_aptpl_buf_len; 1942c66ac9dbSNicholas Bellinger int ret; 1943c66ac9dbSNicholas Bellinger 1944c66ac9dbSNicholas Bellinger memset(path, 0, 512); 1945c66ac9dbSNicholas Bellinger 194660d645a4SDan Carpenter if (strlen(&wwn->unit_serial[0]) >= 512) { 19476708bb27SAndy Grover pr_err("WWN value for struct se_device does not fit" 1948c66ac9dbSNicholas Bellinger " into path buffer\n"); 1949e3d6f909SAndy Grover return -EMSGSIZE; 1950c66ac9dbSNicholas Bellinger } 1951c66ac9dbSNicholas Bellinger 1952c66ac9dbSNicholas Bellinger snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]); 1953c66ac9dbSNicholas Bellinger file = filp_open(path, flags, 0600); 19540e9b10a9SAl Viro if (IS_ERR(file)) { 19556708bb27SAndy Grover pr_err("filp_open(%s) for APTPL metadata" 1956c66ac9dbSNicholas Bellinger " failed\n", path); 19570e9b10a9SAl Viro return PTR_ERR(file); 1958c66ac9dbSNicholas Bellinger } 1959c66ac9dbSNicholas Bellinger 19604dee96fbSAndy Grover pr_aptpl_buf_len = (strlen(buf) + 1); /* Add extra for NULL */ 1961c66ac9dbSNicholas Bellinger 19620e9b10a9SAl Viro ret = kernel_write(file, buf, pr_aptpl_buf_len, 0); 1963c66ac9dbSNicholas Bellinger 19640e9b10a9SAl Viro if (ret < 0) 19656708bb27SAndy Grover pr_debug("Error writing APTPL metadata file: %s\n", path); 19660e9b10a9SAl Viro fput(file); 1967c66ac9dbSNicholas Bellinger 1968f730f915SGera Kazakov return (ret < 0) ? -EIO : 0; 1969c66ac9dbSNicholas Bellinger } 1970c66ac9dbSNicholas Bellinger 1971c66ac9dbSNicholas Bellinger /* 1972459f213bSAndy Grover * Clear the APTPL metadata if APTPL has been disabled, otherwise 1973459f213bSAndy Grover * write out the updated metadata to struct file for this SCSI device. 1974c66ac9dbSNicholas Bellinger */ 19758a391857SNicholas Bellinger static sense_reason_t core_scsi3_update_and_write_aptpl(struct se_device *dev, bool aptpl) 1976459f213bSAndy Grover { 19778a391857SNicholas Bellinger unsigned char *buf; 1978f161d4b4SNicholas Bellinger int rc, len = PR_APTPL_BUF_LEN; 1979459f213bSAndy Grover 1980459f213bSAndy Grover if (!aptpl) { 1981459f213bSAndy Grover char *null_buf = "No Registrations or Reservations\n"; 1982459f213bSAndy Grover 19838a391857SNicholas Bellinger rc = __core_scsi3_write_aptpl_to_file(dev, null_buf); 1984459f213bSAndy Grover dev->t10_pr.pr_aptpl_active = 0; 1985459f213bSAndy Grover pr_debug("SPC-3 PR: Set APTPL Bit Deactivated\n"); 19868a391857SNicholas Bellinger 19878a391857SNicholas Bellinger if (rc) 19888a391857SNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 19898a391857SNicholas Bellinger 19908a391857SNicholas Bellinger return 0; 19918a391857SNicholas Bellinger } 1992f161d4b4SNicholas Bellinger retry: 1993f161d4b4SNicholas Bellinger buf = vzalloc(len); 1994459f213bSAndy Grover if (!buf) 19958a391857SNicholas Bellinger return TCM_OUT_OF_RESOURCES; 1996459f213bSAndy Grover 1997f161d4b4SNicholas Bellinger rc = core_scsi3_update_aptpl_buf(dev, buf, len); 19988a391857SNicholas Bellinger if (rc < 0) { 1999f161d4b4SNicholas Bellinger vfree(buf); 2000f161d4b4SNicholas Bellinger len *= 2; 2001f161d4b4SNicholas Bellinger goto retry; 20020607decdSAndy Grover } 2003de103c93SChristoph Hellwig 20048a391857SNicholas Bellinger rc = __core_scsi3_write_aptpl_to_file(dev, buf); 20058a391857SNicholas Bellinger if (rc != 0) { 2006459f213bSAndy Grover pr_err("SPC-3 PR: Could not update APTPL\n"); 2007f161d4b4SNicholas Bellinger vfree(buf); 20088a391857SNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2009459f213bSAndy Grover } 20108a391857SNicholas Bellinger dev->t10_pr.pr_aptpl_active = 1; 2011f161d4b4SNicholas Bellinger vfree(buf); 20128a391857SNicholas Bellinger pr_debug("SPC-3 PR: Set APTPL Bit Activated\n"); 20138a391857SNicholas Bellinger return 0; 2014c66ac9dbSNicholas Bellinger } 2015c66ac9dbSNicholas Bellinger 2016de103c93SChristoph Hellwig static sense_reason_t 2017de103c93SChristoph Hellwig core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key, 2018bc118fe4SAndy Grover bool aptpl, bool all_tg_pt, bool spec_i_pt, enum register_type register_type) 2019c66ac9dbSNicholas Bellinger { 2020e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 20215951146dSAndy Grover struct se_device *dev = cmd->se_dev; 2022e3d6f909SAndy Grover struct se_lun *se_lun = cmd->se_lun; 2023c66ac9dbSNicholas Bellinger struct se_portal_group *se_tpg; 2024bc118fe4SAndy Grover struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp; 20250fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 2026c66ac9dbSNicholas Bellinger unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL; 2027a0d50f62SHannes Reinecke sense_reason_t ret = TCM_NO_SENSE; 2028fc09149dSNicholas Bellinger int pr_holder = 0, type; 2029c66ac9dbSNicholas Bellinger 20306708bb27SAndy Grover if (!se_sess || !se_lun) { 20316708bb27SAndy Grover pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n"); 2032de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2033c66ac9dbSNicholas Bellinger } 2034c66ac9dbSNicholas Bellinger se_tpg = se_sess->se_tpg; 203529a05deeSNicholas Bellinger 2036e3d6f909SAndy Grover if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) { 2037c66ac9dbSNicholas Bellinger memset(&isid_buf[0], 0, PR_REG_ISID_LEN); 2038e3d6f909SAndy Grover se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0], 2039c66ac9dbSNicholas Bellinger PR_REG_ISID_LEN); 2040c66ac9dbSNicholas Bellinger isid_ptr = &isid_buf[0]; 2041c66ac9dbSNicholas Bellinger } 2042c66ac9dbSNicholas Bellinger /* 2043c66ac9dbSNicholas Bellinger * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47 2044c66ac9dbSNicholas Bellinger */ 2045bc118fe4SAndy Grover pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess); 2046bc118fe4SAndy Grover if (!pr_reg) { 2047c66ac9dbSNicholas Bellinger if (res_key) { 20486708bb27SAndy Grover pr_warn("SPC-3 PR: Reservation Key non-zero" 2049c66ac9dbSNicholas Bellinger " for SA REGISTER, returning CONFLICT\n"); 2050de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 2051c66ac9dbSNicholas Bellinger } 2052c66ac9dbSNicholas Bellinger /* 2053c66ac9dbSNicholas Bellinger * Do nothing but return GOOD status. 2054c66ac9dbSNicholas Bellinger */ 205579dc9c9eSNicholas Bellinger if (!sa_res_key) 205603e98c9eSNicholas Bellinger return 0; 205779dc9c9eSNicholas Bellinger 20586708bb27SAndy Grover if (!spec_i_pt) { 2059c66ac9dbSNicholas Bellinger /* 2060c66ac9dbSNicholas Bellinger * Perform the Service Action REGISTER on the Initiator 2061c66ac9dbSNicholas Bellinger * Port Endpoint that the PRO was received from on the 2062c66ac9dbSNicholas Bellinger * Logical Unit of the SCSI device server. 2063c66ac9dbSNicholas Bellinger */ 2064de103c93SChristoph Hellwig if (core_scsi3_alloc_registration(cmd->se_dev, 206579dc9c9eSNicholas Bellinger se_sess->se_node_acl, cmd->se_lun, 206679dc9c9eSNicholas Bellinger NULL, cmd->orig_fe_lun, isid_ptr, 2067c66ac9dbSNicholas Bellinger sa_res_key, all_tg_pt, aptpl, 206833ce6a87SAndy Grover register_type, 0)) { 20696708bb27SAndy Grover pr_err("Unable to allocate" 2070c66ac9dbSNicholas Bellinger " struct t10_pr_registration\n"); 2071de103c93SChristoph Hellwig return TCM_INVALID_PARAMETER_LIST; 2072c66ac9dbSNicholas Bellinger } 2073c66ac9dbSNicholas Bellinger } else { 2074c66ac9dbSNicholas Bellinger /* 2075c66ac9dbSNicholas Bellinger * Register both the Initiator port that received 2076c66ac9dbSNicholas Bellinger * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI 2077c66ac9dbSNicholas Bellinger * TransportID from Parameter list and loop through 2078c66ac9dbSNicholas Bellinger * fabric dependent parameter list while calling 2079c66ac9dbSNicholas Bellinger * logic from of core_scsi3_alloc_registration() for 2080c66ac9dbSNicholas Bellinger * each TransportID provided SCSI Initiator Port/Device 2081c66ac9dbSNicholas Bellinger */ 208279dc9c9eSNicholas Bellinger ret = core_scsi3_decode_spec_i_port(cmd, se_tpg, 2083c66ac9dbSNicholas Bellinger isid_ptr, sa_res_key, all_tg_pt, aptpl); 208479dc9c9eSNicholas Bellinger if (ret != 0) 2085c66ac9dbSNicholas Bellinger return ret; 2086c66ac9dbSNicholas Bellinger } 2087459f213bSAndy Grover return core_scsi3_update_and_write_aptpl(dev, aptpl); 2088de103c93SChristoph Hellwig } 2089de103c93SChristoph Hellwig 2090bc118fe4SAndy Grover /* ok, existing registration */ 2091c66ac9dbSNicholas Bellinger 2092bc118fe4SAndy Grover if ((register_type == REGISTER) && (res_key != pr_reg->pr_res_key)) { 20936708bb27SAndy Grover pr_err("SPC-3 PR REGISTER: Received" 2094c66ac9dbSNicholas Bellinger " res_key: 0x%016Lx does not match" 2095c66ac9dbSNicholas Bellinger " existing SA REGISTER res_key:" 2096c66ac9dbSNicholas Bellinger " 0x%016Lx\n", res_key, 2097c66ac9dbSNicholas Bellinger pr_reg->pr_res_key); 2098de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 2099bc118fe4SAndy Grover goto out; 2100c66ac9dbSNicholas Bellinger } 2101de103c93SChristoph Hellwig 2102c66ac9dbSNicholas Bellinger if (spec_i_pt) { 21031f070cc2SAndy Grover pr_err("SPC-3 PR REGISTER: SPEC_I_PT" 21041f070cc2SAndy Grover " set on a registered nexus\n"); 2105de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 2106bc118fe4SAndy Grover goto out; 2107c66ac9dbSNicholas Bellinger } 2108de103c93SChristoph Hellwig 2109c66ac9dbSNicholas Bellinger /* 2110c66ac9dbSNicholas Bellinger * An existing ALL_TG_PT=1 registration being released 2111c66ac9dbSNicholas Bellinger * must also set ALL_TG_PT=1 in the incoming PROUT. 2112c66ac9dbSNicholas Bellinger */ 21131f070cc2SAndy Grover if (pr_reg->pr_reg_all_tg_pt && !all_tg_pt) { 21141f070cc2SAndy Grover pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1" 2115c66ac9dbSNicholas Bellinger " registration exists, but ALL_TG_PT=1 bit not" 2116c66ac9dbSNicholas Bellinger " present in received PROUT\n"); 2117de103c93SChristoph Hellwig ret = TCM_INVALID_CDB_FIELD; 2118bc118fe4SAndy Grover goto out; 2119c66ac9dbSNicholas Bellinger } 2120de103c93SChristoph Hellwig 2121c66ac9dbSNicholas Bellinger /* 212251d9c41dSAndy Grover * sa_res_key=1 Change Reservation Key for registered I_T Nexus. 2123c66ac9dbSNicholas Bellinger */ 212451d9c41dSAndy Grover if (sa_res_key) { 212551d9c41dSAndy Grover /* 212651d9c41dSAndy Grover * Increment PRgeneration counter for struct se_device" 212751d9c41dSAndy Grover * upon a successful REGISTER, see spc4r17 section 6.3.2 212851d9c41dSAndy Grover * READ_KEYS service action. 212951d9c41dSAndy Grover */ 213051d9c41dSAndy Grover pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev); 213151d9c41dSAndy Grover pr_reg->pr_res_key = sa_res_key; 213251d9c41dSAndy Grover pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation" 213351d9c41dSAndy Grover " Key for %s to: 0x%016Lx PRgeneration:" 213451d9c41dSAndy Grover " 0x%08x\n", cmd->se_tfo->get_fabric_name(), 213551d9c41dSAndy Grover (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? "_AND_IGNORE_EXISTING_KEY" : "", 213651d9c41dSAndy Grover pr_reg->pr_reg_nacl->initiatorname, 213751d9c41dSAndy Grover pr_reg->pr_res_key, pr_reg->pr_res_generation); 213851d9c41dSAndy Grover 213951d9c41dSAndy Grover } else { 214051d9c41dSAndy Grover /* 214151d9c41dSAndy Grover * sa_res_key=0 Unregister Reservation Key for registered I_T Nexus. 214251d9c41dSAndy Grover */ 21436c3c9baaSNicholas Bellinger type = pr_reg->pr_res_type; 21446c3c9baaSNicholas Bellinger pr_holder = core_scsi3_check_implicit_release(cmd->se_dev, 21456c3c9baaSNicholas Bellinger pr_reg); 2146c66ac9dbSNicholas Bellinger if (pr_holder < 0) { 2147de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 2148bc118fe4SAndy Grover goto out; 2149c66ac9dbSNicholas Bellinger } 2150c66ac9dbSNicholas Bellinger 2151c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 2152c66ac9dbSNicholas Bellinger /* 2153c66ac9dbSNicholas Bellinger * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port 2154c66ac9dbSNicholas Bellinger * and matching pr_res_key. 2155c66ac9dbSNicholas Bellinger */ 2156c66ac9dbSNicholas Bellinger if (pr_reg->pr_reg_all_tg_pt) { 2157c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg_p, pr_reg_tmp, 2158c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, 2159c66ac9dbSNicholas Bellinger pr_reg_list) { 2160c66ac9dbSNicholas Bellinger 21616708bb27SAndy Grover if (!pr_reg_p->pr_reg_all_tg_pt) 2162c66ac9dbSNicholas Bellinger continue; 2163c66ac9dbSNicholas Bellinger if (pr_reg_p->pr_res_key != res_key) 2164c66ac9dbSNicholas Bellinger continue; 2165c66ac9dbSNicholas Bellinger if (pr_reg == pr_reg_p) 2166c66ac9dbSNicholas Bellinger continue; 2167c66ac9dbSNicholas Bellinger if (strcmp(pr_reg->pr_reg_nacl->initiatorname, 2168c66ac9dbSNicholas Bellinger pr_reg_p->pr_reg_nacl->initiatorname)) 2169c66ac9dbSNicholas Bellinger continue; 2170c66ac9dbSNicholas Bellinger 2171c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, 2172c66ac9dbSNicholas Bellinger pr_reg_p, NULL, 0); 2173c66ac9dbSNicholas Bellinger } 2174c66ac9dbSNicholas Bellinger } 2175de103c93SChristoph Hellwig 2176c66ac9dbSNicholas Bellinger /* 2177c66ac9dbSNicholas Bellinger * Release the calling I_T Nexus registration now.. 2178c66ac9dbSNicholas Bellinger */ 2179de103c93SChristoph Hellwig __core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1); 2180fc09149dSNicholas Bellinger pr_reg = NULL; 2181de103c93SChristoph Hellwig 2182c66ac9dbSNicholas Bellinger /* 2183c66ac9dbSNicholas Bellinger * From spc4r17, section 5.7.11.3 Unregistering 2184c66ac9dbSNicholas Bellinger * 2185c66ac9dbSNicholas Bellinger * If the persistent reservation is a registrants only 2186c66ac9dbSNicholas Bellinger * type, the device server shall establish a unit 2187c66ac9dbSNicholas Bellinger * attention condition for the initiator port associated 2188c66ac9dbSNicholas Bellinger * with every registered I_T nexus except for the I_T 2189c66ac9dbSNicholas Bellinger * nexus on which the PERSISTENT RESERVE OUT command was 2190c66ac9dbSNicholas Bellinger * received, with the additional sense code set to 2191c66ac9dbSNicholas Bellinger * RESERVATIONS RELEASED. 2192c66ac9dbSNicholas Bellinger */ 2193c66ac9dbSNicholas Bellinger if (pr_holder && 2194fc09149dSNicholas Bellinger (type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY || 2195fc09149dSNicholas Bellinger type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) { 2196c66ac9dbSNicholas Bellinger list_for_each_entry(pr_reg_p, 2197c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, 2198c66ac9dbSNicholas Bellinger pr_reg_list) { 2199c66ac9dbSNicholas Bellinger 2200c51c8e7bSHannes Reinecke target_ua_allocate_lun( 2201c66ac9dbSNicholas Bellinger pr_reg_p->pr_reg_nacl, 2202c66ac9dbSNicholas Bellinger pr_reg_p->pr_res_mapped_lun, 2203c66ac9dbSNicholas Bellinger 0x2A, 2204c66ac9dbSNicholas Bellinger ASCQ_2AH_RESERVATIONS_RELEASED); 2205c66ac9dbSNicholas Bellinger } 2206c66ac9dbSNicholas Bellinger } 220751d9c41dSAndy Grover 2208c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 2209de103c93SChristoph Hellwig } 2210de103c93SChristoph Hellwig 2211459f213bSAndy Grover ret = core_scsi3_update_and_write_aptpl(dev, aptpl); 2212c66ac9dbSNicholas Bellinger 2213bc118fe4SAndy Grover out: 2214fc09149dSNicholas Bellinger if (pr_reg) 2215c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg); 2216de103c93SChristoph Hellwig return ret; 2217c66ac9dbSNicholas Bellinger } 2218c66ac9dbSNicholas Bellinger 2219c66ac9dbSNicholas Bellinger unsigned char *core_scsi3_pr_dump_type(int type) 2220c66ac9dbSNicholas Bellinger { 2221c66ac9dbSNicholas Bellinger switch (type) { 2222c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE: 2223c66ac9dbSNicholas Bellinger return "Write Exclusive Access"; 2224c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS: 2225c66ac9dbSNicholas Bellinger return "Exclusive Access"; 2226c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_REGONLY: 2227c66ac9dbSNicholas Bellinger return "Write Exclusive Access, Registrants Only"; 2228c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY: 2229c66ac9dbSNicholas Bellinger return "Exclusive Access, Registrants Only"; 2230c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_ALLREG: 2231c66ac9dbSNicholas Bellinger return "Write Exclusive Access, All Registrants"; 2232c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG: 2233c66ac9dbSNicholas Bellinger return "Exclusive Access, All Registrants"; 2234c66ac9dbSNicholas Bellinger default: 2235c66ac9dbSNicholas Bellinger break; 2236c66ac9dbSNicholas Bellinger } 2237c66ac9dbSNicholas Bellinger 2238c66ac9dbSNicholas Bellinger return "Unknown SPC-3 PR Type"; 2239c66ac9dbSNicholas Bellinger } 2240c66ac9dbSNicholas Bellinger 2241de103c93SChristoph Hellwig static sense_reason_t 2242de103c93SChristoph Hellwig core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key) 2243c66ac9dbSNicholas Bellinger { 2244de103c93SChristoph Hellwig struct se_device *dev = cmd->se_dev; 2245e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 2246e3d6f909SAndy Grover struct se_lun *se_lun = cmd->se_lun; 2247c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_res_holder; 22480fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 2249c66ac9dbSNicholas Bellinger char i_buf[PR_REG_ISID_ID_LEN]; 2250de103c93SChristoph Hellwig sense_reason_t ret; 2251c66ac9dbSNicholas Bellinger 2252c66ac9dbSNicholas Bellinger memset(i_buf, 0, PR_REG_ISID_ID_LEN); 2253c66ac9dbSNicholas Bellinger 22546708bb27SAndy Grover if (!se_sess || !se_lun) { 22556708bb27SAndy Grover pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n"); 2256de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2257c66ac9dbSNicholas Bellinger } 2258c66ac9dbSNicholas Bellinger /* 2259c66ac9dbSNicholas Bellinger * Locate the existing *pr_reg via struct se_node_acl pointers 2260c66ac9dbSNicholas Bellinger */ 22615951146dSAndy Grover pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl, 2262c66ac9dbSNicholas Bellinger se_sess); 22636708bb27SAndy Grover if (!pr_reg) { 22646708bb27SAndy Grover pr_err("SPC-3 PR: Unable to locate" 2265c66ac9dbSNicholas Bellinger " PR_REGISTERED *pr_reg for RESERVE\n"); 2266de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2267c66ac9dbSNicholas Bellinger } 2268c66ac9dbSNicholas Bellinger /* 2269c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.9: Reserving: 2270c66ac9dbSNicholas Bellinger * 2271c66ac9dbSNicholas Bellinger * An application client creates a persistent reservation by issuing 2272c66ac9dbSNicholas Bellinger * a PERSISTENT RESERVE OUT command with RESERVE service action through 2273c66ac9dbSNicholas Bellinger * a registered I_T nexus with the following parameters: 2274c66ac9dbSNicholas Bellinger * a) RESERVATION KEY set to the value of the reservation key that is 2275c66ac9dbSNicholas Bellinger * registered with the logical unit for the I_T nexus; and 2276c66ac9dbSNicholas Bellinger */ 2277c66ac9dbSNicholas Bellinger if (res_key != pr_reg->pr_res_key) { 22786708bb27SAndy Grover pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx" 2279c66ac9dbSNicholas Bellinger " does not match existing SA REGISTER res_key:" 2280c66ac9dbSNicholas Bellinger " 0x%016Lx\n", res_key, pr_reg->pr_res_key); 2281de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 2282de103c93SChristoph Hellwig goto out_put_pr_reg; 2283c66ac9dbSNicholas Bellinger } 2284c66ac9dbSNicholas Bellinger /* 2285c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.9: Reserving: 2286c66ac9dbSNicholas Bellinger * 2287c66ac9dbSNicholas Bellinger * From above: 2288c66ac9dbSNicholas Bellinger * b) TYPE field and SCOPE field set to the persistent reservation 2289c66ac9dbSNicholas Bellinger * being created. 2290c66ac9dbSNicholas Bellinger * 2291c66ac9dbSNicholas Bellinger * Only one persistent reservation is allowed at a time per logical unit 2292c66ac9dbSNicholas Bellinger * and that persistent reservation has a scope of LU_SCOPE. 2293c66ac9dbSNicholas Bellinger */ 2294c66ac9dbSNicholas Bellinger if (scope != PR_SCOPE_LU_SCOPE) { 22956708bb27SAndy Grover pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope); 2296de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 2297de103c93SChristoph Hellwig goto out_put_pr_reg; 2298c66ac9dbSNicholas Bellinger } 2299c66ac9dbSNicholas Bellinger /* 2300c66ac9dbSNicholas Bellinger * See if we have an existing PR reservation holder pointer at 2301c66ac9dbSNicholas Bellinger * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration 2302c66ac9dbSNicholas Bellinger * *pr_res_holder. 2303c66ac9dbSNicholas Bellinger */ 2304c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 2305c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 2306ee1b1b9cSAndy Grover if (pr_res_holder) { 2307c66ac9dbSNicholas Bellinger /* 2308c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.9: Reserving: 2309c66ac9dbSNicholas Bellinger * 2310c66ac9dbSNicholas Bellinger * If the device server receives a PERSISTENT RESERVE OUT 2311c66ac9dbSNicholas Bellinger * command from an I_T nexus other than a persistent reservation 2312c66ac9dbSNicholas Bellinger * holder (see 5.7.10) that attempts to create a persistent 2313c66ac9dbSNicholas Bellinger * reservation when a persistent reservation already exists for 2314c66ac9dbSNicholas Bellinger * the logical unit, then the command shall be completed with 2315c66ac9dbSNicholas Bellinger * RESERVATION CONFLICT status. 2316c66ac9dbSNicholas Bellinger */ 2317e673dc92SIlias Tsitsimpis if (!is_reservation_holder(pr_res_holder, pr_reg)) { 2318c66ac9dbSNicholas Bellinger struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; 23196708bb27SAndy Grover pr_err("SPC-3 PR: Attempted RESERVE from" 2320c66ac9dbSNicholas Bellinger " [%s]: %s while reservation already held by" 2321c66ac9dbSNicholas Bellinger " [%s]: %s, returning RESERVATION_CONFLICT\n", 2322e3d6f909SAndy Grover cmd->se_tfo->get_fabric_name(), 2323c66ac9dbSNicholas Bellinger se_sess->se_node_acl->initiatorname, 2324e3d6f909SAndy Grover pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(), 2325c66ac9dbSNicholas Bellinger pr_res_holder->pr_reg_nacl->initiatorname); 2326c66ac9dbSNicholas Bellinger 2327c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2328de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 2329de103c93SChristoph Hellwig goto out_put_pr_reg; 2330c66ac9dbSNicholas Bellinger } 2331c66ac9dbSNicholas Bellinger /* 2332c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.9: Reserving: 2333c66ac9dbSNicholas Bellinger * 2334c66ac9dbSNicholas Bellinger * If a persistent reservation holder attempts to modify the 2335c66ac9dbSNicholas Bellinger * type or scope of an existing persistent reservation, the 2336c66ac9dbSNicholas Bellinger * command shall be completed with RESERVATION CONFLICT status. 2337c66ac9dbSNicholas Bellinger */ 2338c66ac9dbSNicholas Bellinger if ((pr_res_holder->pr_res_type != type) || 2339c66ac9dbSNicholas Bellinger (pr_res_holder->pr_res_scope != scope)) { 2340c66ac9dbSNicholas Bellinger struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; 23416708bb27SAndy Grover pr_err("SPC-3 PR: Attempted RESERVE from" 2342c66ac9dbSNicholas Bellinger " [%s]: %s trying to change TYPE and/or SCOPE," 2343c66ac9dbSNicholas Bellinger " while reservation already held by [%s]: %s," 2344c66ac9dbSNicholas Bellinger " returning RESERVATION_CONFLICT\n", 2345e3d6f909SAndy Grover cmd->se_tfo->get_fabric_name(), 2346c66ac9dbSNicholas Bellinger se_sess->se_node_acl->initiatorname, 2347e3d6f909SAndy Grover pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(), 2348c66ac9dbSNicholas Bellinger pr_res_holder->pr_reg_nacl->initiatorname); 2349c66ac9dbSNicholas Bellinger 2350c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2351de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 2352de103c93SChristoph Hellwig goto out_put_pr_reg; 2353c66ac9dbSNicholas Bellinger } 2354c66ac9dbSNicholas Bellinger /* 2355c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.9: Reserving: 2356c66ac9dbSNicholas Bellinger * 2357c66ac9dbSNicholas Bellinger * If the device server receives a PERSISTENT RESERVE OUT 2358c66ac9dbSNicholas Bellinger * command with RESERVE service action where the TYPE field and 2359c66ac9dbSNicholas Bellinger * the SCOPE field contain the same values as the existing type 2360c66ac9dbSNicholas Bellinger * and scope from a persistent reservation holder, it shall not 2361c66ac9dbSNicholas Bellinger * make any change to the existing persistent reservation and 2362c66ac9dbSNicholas Bellinger * shall completethe command with GOOD status. 2363c66ac9dbSNicholas Bellinger */ 2364c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2365de103c93SChristoph Hellwig ret = 0; 2366de103c93SChristoph Hellwig goto out_put_pr_reg; 2367c66ac9dbSNicholas Bellinger } 2368c66ac9dbSNicholas Bellinger /* 2369c66ac9dbSNicholas Bellinger * Otherwise, our *pr_reg becomes the PR reservation holder for said 2370c66ac9dbSNicholas Bellinger * TYPE/SCOPE. Also set the received scope and type in *pr_reg. 2371c66ac9dbSNicholas Bellinger */ 2372c66ac9dbSNicholas Bellinger pr_reg->pr_res_scope = scope; 2373c66ac9dbSNicholas Bellinger pr_reg->pr_res_type = type; 2374c66ac9dbSNicholas Bellinger pr_reg->pr_res_holder = 1; 2375c66ac9dbSNicholas Bellinger dev->dev_pr_res_holder = pr_reg; 2376d2843c17SAndy Grover core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); 2377c66ac9dbSNicholas Bellinger 23786708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new" 2379c66ac9dbSNicholas Bellinger " reservation holder TYPE: %s ALL_TG_PT: %d\n", 2380e3d6f909SAndy Grover cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type), 2381c66ac9dbSNicholas Bellinger (pr_reg->pr_reg_all_tg_pt) ? 1 : 0); 23826708bb27SAndy Grover pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n", 2383e3d6f909SAndy Grover cmd->se_tfo->get_fabric_name(), 2384c66ac9dbSNicholas Bellinger se_sess->se_node_acl->initiatorname, 2385d2843c17SAndy Grover i_buf); 2386c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2387c66ac9dbSNicholas Bellinger 2388459f213bSAndy Grover if (pr_tmpl->pr_aptpl_active) 2389459f213bSAndy Grover core_scsi3_update_and_write_aptpl(cmd->se_dev, true); 2390c66ac9dbSNicholas Bellinger 2391de103c93SChristoph Hellwig ret = 0; 2392de103c93SChristoph Hellwig out_put_pr_reg: 2393de103c93SChristoph Hellwig core_scsi3_put_pr_reg(pr_reg); 2394de103c93SChristoph Hellwig return ret; 2395de103c93SChristoph Hellwig } 2396de103c93SChristoph Hellwig 2397de103c93SChristoph Hellwig static sense_reason_t 2398de103c93SChristoph Hellwig core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope, 2399c66ac9dbSNicholas Bellinger u64 res_key) 2400c66ac9dbSNicholas Bellinger { 2401c66ac9dbSNicholas Bellinger switch (type) { 2402c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE: 2403c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS: 2404c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_REGONLY: 2405c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY: 2406c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_ALLREG: 2407c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG: 2408de103c93SChristoph Hellwig return core_scsi3_pro_reserve(cmd, type, scope, res_key); 2409c66ac9dbSNicholas Bellinger default: 24106708bb27SAndy Grover pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:" 2411c66ac9dbSNicholas Bellinger " 0x%02x\n", type); 2412de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 2413c66ac9dbSNicholas Bellinger } 2414c66ac9dbSNicholas Bellinger } 2415c66ac9dbSNicholas Bellinger 2416c66ac9dbSNicholas Bellinger /* 2417c66ac9dbSNicholas Bellinger * Called with struct se_device->dev_reservation_lock held. 2418c66ac9dbSNicholas Bellinger */ 2419c66ac9dbSNicholas Bellinger static void __core_scsi3_complete_pro_release( 2420c66ac9dbSNicholas Bellinger struct se_device *dev, 2421c66ac9dbSNicholas Bellinger struct se_node_acl *se_nacl, 2422c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, 24236c3c9baaSNicholas Bellinger int explicit, 24246c3c9baaSNicholas Bellinger int unreg) 2425c66ac9dbSNicholas Bellinger { 24269ac8928eSChristoph Hellwig const struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo; 2427c66ac9dbSNicholas Bellinger char i_buf[PR_REG_ISID_ID_LEN]; 24286c3c9baaSNicholas Bellinger int pr_res_type = 0, pr_res_scope = 0; 2429c66ac9dbSNicholas Bellinger 2430c66ac9dbSNicholas Bellinger memset(i_buf, 0, PR_REG_ISID_ID_LEN); 2431d2843c17SAndy Grover core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); 2432c66ac9dbSNicholas Bellinger /* 2433c66ac9dbSNicholas Bellinger * Go ahead and release the current PR reservation holder. 24346c3c9baaSNicholas Bellinger * If an All Registrants reservation is currently active and 24356c3c9baaSNicholas Bellinger * a unregister operation is requested, replace the current 24366c3c9baaSNicholas Bellinger * dev_pr_res_holder with another active registration. 2437c66ac9dbSNicholas Bellinger */ 24386c3c9baaSNicholas Bellinger if (dev->dev_pr_res_holder) { 24396c3c9baaSNicholas Bellinger pr_res_type = dev->dev_pr_res_holder->pr_res_type; 24406c3c9baaSNicholas Bellinger pr_res_scope = dev->dev_pr_res_holder->pr_res_scope; 24416c3c9baaSNicholas Bellinger dev->dev_pr_res_holder->pr_res_type = 0; 24426c3c9baaSNicholas Bellinger dev->dev_pr_res_holder->pr_res_scope = 0; 24436c3c9baaSNicholas Bellinger dev->dev_pr_res_holder->pr_res_holder = 0; 2444c66ac9dbSNicholas Bellinger dev->dev_pr_res_holder = NULL; 24456c3c9baaSNicholas Bellinger } 24466c3c9baaSNicholas Bellinger if (!unreg) 24476c3c9baaSNicholas Bellinger goto out; 2448c66ac9dbSNicholas Bellinger 24496c3c9baaSNicholas Bellinger spin_lock(&dev->t10_pr.registration_lock); 24506c3c9baaSNicholas Bellinger list_del_init(&pr_reg->pr_reg_list); 24516c3c9baaSNicholas Bellinger /* 24526c3c9baaSNicholas Bellinger * If the I_T nexus is a reservation holder, the persistent reservation 24536c3c9baaSNicholas Bellinger * is of an all registrants type, and the I_T nexus is the last remaining 24546c3c9baaSNicholas Bellinger * registered I_T nexus, then the device server shall also release the 24556c3c9baaSNicholas Bellinger * persistent reservation. 24566c3c9baaSNicholas Bellinger */ 24576c3c9baaSNicholas Bellinger if (!list_empty(&dev->t10_pr.registration_list) && 24586c3c9baaSNicholas Bellinger ((pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || 24596c3c9baaSNicholas Bellinger (pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) { 24606c3c9baaSNicholas Bellinger dev->dev_pr_res_holder = 24616c3c9baaSNicholas Bellinger list_entry(dev->t10_pr.registration_list.next, 24626c3c9baaSNicholas Bellinger struct t10_pr_registration, pr_reg_list); 24636c3c9baaSNicholas Bellinger dev->dev_pr_res_holder->pr_res_type = pr_res_type; 24646c3c9baaSNicholas Bellinger dev->dev_pr_res_holder->pr_res_scope = pr_res_scope; 24656c3c9baaSNicholas Bellinger dev->dev_pr_res_holder->pr_res_holder = 1; 24666c3c9baaSNicholas Bellinger } 24676c3c9baaSNicholas Bellinger spin_unlock(&dev->t10_pr.registration_lock); 24686c3c9baaSNicholas Bellinger out: 24696c3c9baaSNicholas Bellinger if (!dev->dev_pr_res_holder) { 24706708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared" 2471c66ac9dbSNicholas Bellinger " reservation holder TYPE: %s ALL_TG_PT: %d\n", 24726c3c9baaSNicholas Bellinger tfo->get_fabric_name(), (explicit) ? "explicit" : 24736c3c9baaSNicholas Bellinger "implicit", core_scsi3_pr_dump_type(pr_res_type), 2474c66ac9dbSNicholas Bellinger (pr_reg->pr_reg_all_tg_pt) ? 1 : 0); 24756c3c9baaSNicholas Bellinger } 24766708bb27SAndy Grover pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n", 2477c66ac9dbSNicholas Bellinger tfo->get_fabric_name(), se_nacl->initiatorname, 2478d2843c17SAndy Grover i_buf); 2479c66ac9dbSNicholas Bellinger /* 2480c66ac9dbSNicholas Bellinger * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE 2481c66ac9dbSNicholas Bellinger */ 2482c66ac9dbSNicholas Bellinger pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0; 2483c66ac9dbSNicholas Bellinger } 2484c66ac9dbSNicholas Bellinger 2485de103c93SChristoph Hellwig static sense_reason_t 2486de103c93SChristoph Hellwig core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope, 2487c66ac9dbSNicholas Bellinger u64 res_key) 2488c66ac9dbSNicholas Bellinger { 2489c66ac9dbSNicholas Bellinger struct se_device *dev = cmd->se_dev; 2490e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 2491e3d6f909SAndy Grover struct se_lun *se_lun = cmd->se_lun; 2492c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder; 24930fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 2494de103c93SChristoph Hellwig sense_reason_t ret = 0; 2495c66ac9dbSNicholas Bellinger 24966708bb27SAndy Grover if (!se_sess || !se_lun) { 24976708bb27SAndy Grover pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n"); 2498de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2499c66ac9dbSNicholas Bellinger } 2500c66ac9dbSNicholas Bellinger /* 2501c66ac9dbSNicholas Bellinger * Locate the existing *pr_reg via struct se_node_acl pointers 2502c66ac9dbSNicholas Bellinger */ 2503c66ac9dbSNicholas Bellinger pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess); 25046708bb27SAndy Grover if (!pr_reg) { 25056708bb27SAndy Grover pr_err("SPC-3 PR: Unable to locate" 2506c66ac9dbSNicholas Bellinger " PR_REGISTERED *pr_reg for RELEASE\n"); 2507de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2508c66ac9dbSNicholas Bellinger } 2509c66ac9dbSNicholas Bellinger /* 2510c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.11.2 Releasing: 2511c66ac9dbSNicholas Bellinger * 2512c66ac9dbSNicholas Bellinger * If there is no persistent reservation or in response to a persistent 2513c66ac9dbSNicholas Bellinger * reservation release request from a registered I_T nexus that is not a 2514c66ac9dbSNicholas Bellinger * persistent reservation holder (see 5.7.10), the device server shall 2515c66ac9dbSNicholas Bellinger * do the following: 2516c66ac9dbSNicholas Bellinger * 2517c66ac9dbSNicholas Bellinger * a) Not release the persistent reservation, if any; 2518c66ac9dbSNicholas Bellinger * b) Not remove any registrations; and 2519c66ac9dbSNicholas Bellinger * c) Complete the command with GOOD status. 2520c66ac9dbSNicholas Bellinger */ 2521c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 2522c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 25236708bb27SAndy Grover if (!pr_res_holder) { 2524c66ac9dbSNicholas Bellinger /* 2525c66ac9dbSNicholas Bellinger * No persistent reservation, return GOOD status. 2526c66ac9dbSNicholas Bellinger */ 2527c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2528bb7a8c8eSNicholas Bellinger goto out_put_pr_reg; 2529c66ac9dbSNicholas Bellinger } 2530c66ac9dbSNicholas Bellinger 2531e673dc92SIlias Tsitsimpis if (!is_reservation_holder(pr_res_holder, pr_reg)) { 2532c66ac9dbSNicholas Bellinger /* 2533c66ac9dbSNicholas Bellinger * Release request from a registered I_T nexus that is not a 2534c66ac9dbSNicholas Bellinger * persistent reservation holder. return GOOD status. 2535c66ac9dbSNicholas Bellinger */ 2536c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2537de103c93SChristoph Hellwig goto out_put_pr_reg; 2538c66ac9dbSNicholas Bellinger } 2539de103c93SChristoph Hellwig 2540c66ac9dbSNicholas Bellinger /* 2541c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.11.2 Releasing: 2542c66ac9dbSNicholas Bellinger * 2543c66ac9dbSNicholas Bellinger * Only the persistent reservation holder (see 5.7.10) is allowed to 2544c66ac9dbSNicholas Bellinger * release a persistent reservation. 2545c66ac9dbSNicholas Bellinger * 2546c66ac9dbSNicholas Bellinger * An application client releases the persistent reservation by issuing 2547c66ac9dbSNicholas Bellinger * a PERSISTENT RESERVE OUT command with RELEASE service action through 2548c66ac9dbSNicholas Bellinger * an I_T nexus that is a persistent reservation holder with the 2549c66ac9dbSNicholas Bellinger * following parameters: 2550c66ac9dbSNicholas Bellinger * 2551c66ac9dbSNicholas Bellinger * a) RESERVATION KEY field set to the value of the reservation key 2552c66ac9dbSNicholas Bellinger * that is registered with the logical unit for the I_T nexus; 2553c66ac9dbSNicholas Bellinger */ 2554c66ac9dbSNicholas Bellinger if (res_key != pr_reg->pr_res_key) { 25556708bb27SAndy Grover pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx" 2556c66ac9dbSNicholas Bellinger " does not match existing SA REGISTER res_key:" 2557c66ac9dbSNicholas Bellinger " 0x%016Lx\n", res_key, pr_reg->pr_res_key); 2558c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2559de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 2560de103c93SChristoph Hellwig goto out_put_pr_reg; 2561c66ac9dbSNicholas Bellinger } 2562c66ac9dbSNicholas Bellinger /* 2563c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.11.2 Releasing and above: 2564c66ac9dbSNicholas Bellinger * 2565c66ac9dbSNicholas Bellinger * b) TYPE field and SCOPE field set to match the persistent 2566c66ac9dbSNicholas Bellinger * reservation being released. 2567c66ac9dbSNicholas Bellinger */ 2568c66ac9dbSNicholas Bellinger if ((pr_res_holder->pr_res_type != type) || 2569c66ac9dbSNicholas Bellinger (pr_res_holder->pr_res_scope != scope)) { 2570c66ac9dbSNicholas Bellinger struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; 25716708bb27SAndy Grover pr_err("SPC-3 PR RELEASE: Attempted to release" 2572c66ac9dbSNicholas Bellinger " reservation from [%s]: %s with different TYPE " 2573c66ac9dbSNicholas Bellinger "and/or SCOPE while reservation already held by" 2574c66ac9dbSNicholas Bellinger " [%s]: %s, returning RESERVATION_CONFLICT\n", 2575e3d6f909SAndy Grover cmd->se_tfo->get_fabric_name(), 2576c66ac9dbSNicholas Bellinger se_sess->se_node_acl->initiatorname, 2577e3d6f909SAndy Grover pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(), 2578c66ac9dbSNicholas Bellinger pr_res_holder->pr_reg_nacl->initiatorname); 2579c66ac9dbSNicholas Bellinger 2580c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2581de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 2582de103c93SChristoph Hellwig goto out_put_pr_reg; 2583c66ac9dbSNicholas Bellinger } 2584c66ac9dbSNicholas Bellinger /* 2585c66ac9dbSNicholas Bellinger * In response to a persistent reservation release request from the 2586c66ac9dbSNicholas Bellinger * persistent reservation holder the device server shall perform a 2587c66ac9dbSNicholas Bellinger * release by doing the following as an uninterrupted series of actions: 2588c66ac9dbSNicholas Bellinger * a) Release the persistent reservation; 2589c66ac9dbSNicholas Bellinger * b) Not remove any registration(s); 2590c66ac9dbSNicholas Bellinger * c) If the released persistent reservation is a registrants only type 2591c66ac9dbSNicholas Bellinger * or all registrants type persistent reservation, 2592c66ac9dbSNicholas Bellinger * the device server shall establish a unit attention condition for 2593c66ac9dbSNicholas Bellinger * the initiator port associated with every regis- 2594c66ac9dbSNicholas Bellinger * tered I_T nexus other than I_T nexus on which the PERSISTENT 2595c66ac9dbSNicholas Bellinger * RESERVE OUT command with RELEASE service action was received, 2596c66ac9dbSNicholas Bellinger * with the additional sense code set to RESERVATIONS RELEASED; and 2597c66ac9dbSNicholas Bellinger * d) If the persistent reservation is of any other type, the device 2598c66ac9dbSNicholas Bellinger * server shall not establish a unit attention condition. 2599c66ac9dbSNicholas Bellinger */ 2600c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl, 26016c3c9baaSNicholas Bellinger pr_reg, 1, 0); 2602c66ac9dbSNicholas Bellinger 2603c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2604c66ac9dbSNicholas Bellinger 2605c66ac9dbSNicholas Bellinger if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) && 2606c66ac9dbSNicholas Bellinger (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) && 2607c66ac9dbSNicholas Bellinger (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) && 2608c66ac9dbSNicholas Bellinger (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) { 2609c66ac9dbSNicholas Bellinger /* 2610c66ac9dbSNicholas Bellinger * If no UNIT ATTENTION conditions will be established for 2611c66ac9dbSNicholas Bellinger * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS 2612c66ac9dbSNicholas Bellinger * go ahead and check for APTPL=1 update+write below 2613c66ac9dbSNicholas Bellinger */ 2614c66ac9dbSNicholas Bellinger goto write_aptpl; 2615c66ac9dbSNicholas Bellinger } 2616c66ac9dbSNicholas Bellinger 2617c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 2618c66ac9dbSNicholas Bellinger list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list, 2619c66ac9dbSNicholas Bellinger pr_reg_list) { 2620c66ac9dbSNicholas Bellinger /* 2621c66ac9dbSNicholas Bellinger * Do not establish a UNIT ATTENTION condition 2622c66ac9dbSNicholas Bellinger * for the calling I_T Nexus 2623c66ac9dbSNicholas Bellinger */ 2624c66ac9dbSNicholas Bellinger if (pr_reg_p == pr_reg) 2625c66ac9dbSNicholas Bellinger continue; 2626c66ac9dbSNicholas Bellinger 2627c51c8e7bSHannes Reinecke target_ua_allocate_lun(pr_reg_p->pr_reg_nacl, 2628c66ac9dbSNicholas Bellinger pr_reg_p->pr_res_mapped_lun, 2629c66ac9dbSNicholas Bellinger 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED); 2630c66ac9dbSNicholas Bellinger } 2631c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 2632c66ac9dbSNicholas Bellinger 2633c66ac9dbSNicholas Bellinger write_aptpl: 2634459f213bSAndy Grover if (pr_tmpl->pr_aptpl_active) 2635459f213bSAndy Grover core_scsi3_update_and_write_aptpl(cmd->se_dev, true); 2636459f213bSAndy Grover 2637de103c93SChristoph Hellwig out_put_pr_reg: 2638c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg); 2639de103c93SChristoph Hellwig return ret; 2640c66ac9dbSNicholas Bellinger } 2641c66ac9dbSNicholas Bellinger 2642de103c93SChristoph Hellwig static sense_reason_t 2643de103c93SChristoph Hellwig core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key) 2644c66ac9dbSNicholas Bellinger { 2645c66ac9dbSNicholas Bellinger struct se_device *dev = cmd->se_dev; 2646c66ac9dbSNicholas Bellinger struct se_node_acl *pr_reg_nacl; 2647e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 26480fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 2649c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder; 2650f2d30680SHannes Reinecke u64 pr_res_mapped_lun = 0; 2651c66ac9dbSNicholas Bellinger int calling_it_nexus = 0; 2652c66ac9dbSNicholas Bellinger /* 2653c66ac9dbSNicholas Bellinger * Locate the existing *pr_reg via struct se_node_acl pointers 2654c66ac9dbSNicholas Bellinger */ 26555951146dSAndy Grover pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, 2656c66ac9dbSNicholas Bellinger se_sess->se_node_acl, se_sess); 26576708bb27SAndy Grover if (!pr_reg_n) { 26586708bb27SAndy Grover pr_err("SPC-3 PR: Unable to locate" 2659c66ac9dbSNicholas Bellinger " PR_REGISTERED *pr_reg for CLEAR\n"); 2660de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2661c66ac9dbSNicholas Bellinger } 2662c66ac9dbSNicholas Bellinger /* 2663c66ac9dbSNicholas Bellinger * From spc4r17 section 5.7.11.6, Clearing: 2664c66ac9dbSNicholas Bellinger * 2665c66ac9dbSNicholas Bellinger * Any application client may release the persistent reservation and 2666c66ac9dbSNicholas Bellinger * remove all registrations from a device server by issuing a 2667c66ac9dbSNicholas Bellinger * PERSISTENT RESERVE OUT command with CLEAR service action through a 2668c66ac9dbSNicholas Bellinger * registered I_T nexus with the following parameter: 2669c66ac9dbSNicholas Bellinger * 2670c66ac9dbSNicholas Bellinger * a) RESERVATION KEY field set to the value of the reservation key 2671c66ac9dbSNicholas Bellinger * that is registered with the logical unit for the I_T nexus. 2672c66ac9dbSNicholas Bellinger */ 2673c66ac9dbSNicholas Bellinger if (res_key != pr_reg_n->pr_res_key) { 26746708bb27SAndy Grover pr_err("SPC-3 PR REGISTER: Received" 2675c66ac9dbSNicholas Bellinger " res_key: 0x%016Lx does not match" 2676c66ac9dbSNicholas Bellinger " existing SA REGISTER res_key:" 2677c66ac9dbSNicholas Bellinger " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key); 2678c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_n); 2679de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 2680c66ac9dbSNicholas Bellinger } 2681c66ac9dbSNicholas Bellinger /* 2682c66ac9dbSNicholas Bellinger * a) Release the persistent reservation, if any; 2683c66ac9dbSNicholas Bellinger */ 2684c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 2685c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 2686c66ac9dbSNicholas Bellinger if (pr_res_holder) { 2687c66ac9dbSNicholas Bellinger struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; 2688c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_release(dev, pr_res_nacl, 26896c3c9baaSNicholas Bellinger pr_res_holder, 0, 0); 2690c66ac9dbSNicholas Bellinger } 2691c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2692c66ac9dbSNicholas Bellinger /* 2693c66ac9dbSNicholas Bellinger * b) Remove all registration(s) (see spc4r17 5.7.7); 2694c66ac9dbSNicholas Bellinger */ 2695c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 2696c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 2697c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 2698c66ac9dbSNicholas Bellinger 2699c66ac9dbSNicholas Bellinger calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; 2700c66ac9dbSNicholas Bellinger pr_reg_nacl = pr_reg->pr_reg_nacl; 2701c66ac9dbSNicholas Bellinger pr_res_mapped_lun = pr_reg->pr_res_mapped_lun; 2702c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, pr_reg, NULL, 2703c66ac9dbSNicholas Bellinger calling_it_nexus); 2704c66ac9dbSNicholas Bellinger /* 2705c66ac9dbSNicholas Bellinger * e) Establish a unit attention condition for the initiator 2706c66ac9dbSNicholas Bellinger * port associated with every registered I_T nexus other 2707c66ac9dbSNicholas Bellinger * than the I_T nexus on which the PERSISTENT RESERVE OUT 2708c66ac9dbSNicholas Bellinger * command with CLEAR service action was received, with the 2709c66ac9dbSNicholas Bellinger * additional sense code set to RESERVATIONS PREEMPTED. 2710c66ac9dbSNicholas Bellinger */ 27116708bb27SAndy Grover if (!calling_it_nexus) 2712c51c8e7bSHannes Reinecke target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun, 2713c66ac9dbSNicholas Bellinger 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED); 2714c66ac9dbSNicholas Bellinger } 2715c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 2716c66ac9dbSNicholas Bellinger 27176708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n", 2718e3d6f909SAndy Grover cmd->se_tfo->get_fabric_name()); 2719c66ac9dbSNicholas Bellinger 2720459f213bSAndy Grover core_scsi3_update_and_write_aptpl(cmd->se_dev, false); 2721c66ac9dbSNicholas Bellinger 2722c66ac9dbSNicholas Bellinger core_scsi3_pr_generation(dev); 2723c66ac9dbSNicholas Bellinger return 0; 2724c66ac9dbSNicholas Bellinger } 2725c66ac9dbSNicholas Bellinger 2726c66ac9dbSNicholas Bellinger /* 2727c66ac9dbSNicholas Bellinger * Called with struct se_device->dev_reservation_lock held. 2728c66ac9dbSNicholas Bellinger */ 2729c66ac9dbSNicholas Bellinger static void __core_scsi3_complete_pro_preempt( 2730c66ac9dbSNicholas Bellinger struct se_device *dev, 2731c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, 2732c66ac9dbSNicholas Bellinger struct list_head *preempt_and_abort_list, 2733c66ac9dbSNicholas Bellinger int type, 2734c66ac9dbSNicholas Bellinger int scope, 273533ce6a87SAndy Grover enum preempt_type preempt_type) 2736c66ac9dbSNicholas Bellinger { 2737c66ac9dbSNicholas Bellinger struct se_node_acl *nacl = pr_reg->pr_reg_nacl; 27389ac8928eSChristoph Hellwig const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo; 2739c66ac9dbSNicholas Bellinger char i_buf[PR_REG_ISID_ID_LEN]; 2740c66ac9dbSNicholas Bellinger 2741c66ac9dbSNicholas Bellinger memset(i_buf, 0, PR_REG_ISID_ID_LEN); 2742d2843c17SAndy Grover core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); 2743c66ac9dbSNicholas Bellinger /* 2744125d0119SHannes Reinecke * Do an implicit RELEASE of the existing reservation. 2745c66ac9dbSNicholas Bellinger */ 2746c66ac9dbSNicholas Bellinger if (dev->dev_pr_res_holder) 2747c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_release(dev, nacl, 27486c3c9baaSNicholas Bellinger dev->dev_pr_res_holder, 0, 0); 2749c66ac9dbSNicholas Bellinger 2750c66ac9dbSNicholas Bellinger dev->dev_pr_res_holder = pr_reg; 2751c66ac9dbSNicholas Bellinger pr_reg->pr_res_holder = 1; 2752c66ac9dbSNicholas Bellinger pr_reg->pr_res_type = type; 2753c66ac9dbSNicholas Bellinger pr_reg->pr_res_scope = scope; 2754c66ac9dbSNicholas Bellinger 27556708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new" 2756c66ac9dbSNicholas Bellinger " reservation holder TYPE: %s ALL_TG_PT: %d\n", 275733ce6a87SAndy Grover tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", 2758c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(type), 2759c66ac9dbSNicholas Bellinger (pr_reg->pr_reg_all_tg_pt) ? 1 : 0); 27606708bb27SAndy Grover pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n", 276133ce6a87SAndy Grover tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", 2762d2843c17SAndy Grover nacl->initiatorname, i_buf); 2763c66ac9dbSNicholas Bellinger /* 2764c66ac9dbSNicholas Bellinger * For PREEMPT_AND_ABORT, add the preempting reservation's 2765c66ac9dbSNicholas Bellinger * struct t10_pr_registration to the list that will be compared 2766c66ac9dbSNicholas Bellinger * against received CDBs.. 2767c66ac9dbSNicholas Bellinger */ 2768c66ac9dbSNicholas Bellinger if (preempt_and_abort_list) 2769c66ac9dbSNicholas Bellinger list_add_tail(&pr_reg->pr_reg_abort_list, 2770c66ac9dbSNicholas Bellinger preempt_and_abort_list); 2771c66ac9dbSNicholas Bellinger } 2772c66ac9dbSNicholas Bellinger 2773c66ac9dbSNicholas Bellinger static void core_scsi3_release_preempt_and_abort( 2774c66ac9dbSNicholas Bellinger struct list_head *preempt_and_abort_list, 2775c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg_holder) 2776c66ac9dbSNicholas Bellinger { 2777c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp; 2778c66ac9dbSNicholas Bellinger 2779c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list, 2780c66ac9dbSNicholas Bellinger pr_reg_abort_list) { 2781c66ac9dbSNicholas Bellinger 2782c66ac9dbSNicholas Bellinger list_del(&pr_reg->pr_reg_abort_list); 2783c66ac9dbSNicholas Bellinger if (pr_reg_holder == pr_reg) 2784c66ac9dbSNicholas Bellinger continue; 2785c66ac9dbSNicholas Bellinger if (pr_reg->pr_res_holder) { 27866708bb27SAndy Grover pr_warn("pr_reg->pr_res_holder still set\n"); 2787c66ac9dbSNicholas Bellinger continue; 2788c66ac9dbSNicholas Bellinger } 2789c66ac9dbSNicholas Bellinger 2790c66ac9dbSNicholas Bellinger pr_reg->pr_reg_deve = NULL; 2791c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl = NULL; 2792c66ac9dbSNicholas Bellinger kmem_cache_free(t10_pr_reg_cache, pr_reg); 2793c66ac9dbSNicholas Bellinger } 2794c66ac9dbSNicholas Bellinger } 2795c66ac9dbSNicholas Bellinger 2796de103c93SChristoph Hellwig static sense_reason_t 2797de103c93SChristoph Hellwig core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key, 279833ce6a87SAndy Grover u64 sa_res_key, enum preempt_type preempt_type) 2799c66ac9dbSNicholas Bellinger { 28005951146dSAndy Grover struct se_device *dev = cmd->se_dev; 2801c66ac9dbSNicholas Bellinger struct se_node_acl *pr_reg_nacl; 2802e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 2803d0f474e5SRoland Dreier LIST_HEAD(preempt_and_abort_list); 2804c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder; 28050fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 2806f2d30680SHannes Reinecke u64 pr_res_mapped_lun = 0; 2807b6932ee3SSteven Allen int all_reg = 0, calling_it_nexus = 0; 2808b6932ee3SSteven Allen bool sa_res_key_unmatched = sa_res_key != 0; 2809de103c93SChristoph Hellwig int prh_type = 0, prh_scope = 0; 2810c66ac9dbSNicholas Bellinger 2811de103c93SChristoph Hellwig if (!se_sess) 2812de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2813c66ac9dbSNicholas Bellinger 28145951146dSAndy Grover pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl, 2815c66ac9dbSNicholas Bellinger se_sess); 28166708bb27SAndy Grover if (!pr_reg_n) { 28176708bb27SAndy Grover pr_err("SPC-3 PR: Unable to locate" 2818c66ac9dbSNicholas Bellinger " PR_REGISTERED *pr_reg for PREEMPT%s\n", 281933ce6a87SAndy Grover (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : ""); 2820de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 2821c66ac9dbSNicholas Bellinger } 2822c66ac9dbSNicholas Bellinger if (pr_reg_n->pr_res_key != res_key) { 2823c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_n); 2824de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 2825c66ac9dbSNicholas Bellinger } 2826c66ac9dbSNicholas Bellinger if (scope != PR_SCOPE_LU_SCOPE) { 28276708bb27SAndy Grover pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope); 2828c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_n); 2829de103c93SChristoph Hellwig return TCM_INVALID_PARAMETER_LIST; 2830c66ac9dbSNicholas Bellinger } 2831c66ac9dbSNicholas Bellinger 2832c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 2833c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 2834c66ac9dbSNicholas Bellinger if (pr_res_holder && 2835c66ac9dbSNicholas Bellinger ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || 2836c66ac9dbSNicholas Bellinger (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) 2837c66ac9dbSNicholas Bellinger all_reg = 1; 2838c66ac9dbSNicholas Bellinger 28396708bb27SAndy Grover if (!all_reg && !sa_res_key) { 2840c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2841c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_n); 2842de103c93SChristoph Hellwig return TCM_INVALID_PARAMETER_LIST; 2843c66ac9dbSNicholas Bellinger } 2844c66ac9dbSNicholas Bellinger /* 2845c66ac9dbSNicholas Bellinger * From spc4r17, section 5.7.11.4.4 Removing Registrations: 2846c66ac9dbSNicholas Bellinger * 2847c66ac9dbSNicholas Bellinger * If the SERVICE ACTION RESERVATION KEY field does not identify a 2848c66ac9dbSNicholas Bellinger * persistent reservation holder or there is no persistent reservation 2849c66ac9dbSNicholas Bellinger * holder (i.e., there is no persistent reservation), then the device 2850c66ac9dbSNicholas Bellinger * server shall perform a preempt by doing the following in an 2851c66ac9dbSNicholas Bellinger * uninterrupted series of actions. (See below..) 2852c66ac9dbSNicholas Bellinger */ 28536708bb27SAndy Grover if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) { 2854c66ac9dbSNicholas Bellinger /* 2855c66ac9dbSNicholas Bellinger * No existing or SA Reservation Key matching reservations.. 2856c66ac9dbSNicholas Bellinger * 2857c66ac9dbSNicholas Bellinger * PROUT SA PREEMPT with All Registrant type reservations are 2858c66ac9dbSNicholas Bellinger * allowed to be processed without a matching SA Reservation Key 2859c66ac9dbSNicholas Bellinger */ 2860c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 2861c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 2862c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 2863c66ac9dbSNicholas Bellinger /* 2864c66ac9dbSNicholas Bellinger * Removing of registrations in non all registrants 2865c66ac9dbSNicholas Bellinger * type reservations without a matching SA reservation 2866c66ac9dbSNicholas Bellinger * key. 2867c66ac9dbSNicholas Bellinger * 2868c66ac9dbSNicholas Bellinger * a) Remove the registrations for all I_T nexuses 2869c66ac9dbSNicholas Bellinger * specified by the SERVICE ACTION RESERVATION KEY 2870c66ac9dbSNicholas Bellinger * field; 2871c66ac9dbSNicholas Bellinger * b) Ignore the contents of the SCOPE and TYPE fields; 2872c66ac9dbSNicholas Bellinger * c) Process tasks as defined in 5.7.1; and 2873c66ac9dbSNicholas Bellinger * d) Establish a unit attention condition for the 2874c66ac9dbSNicholas Bellinger * initiator port associated with every I_T nexus 2875c66ac9dbSNicholas Bellinger * that lost its registration other than the I_T 2876c66ac9dbSNicholas Bellinger * nexus on which the PERSISTENT RESERVE OUT command 2877c66ac9dbSNicholas Bellinger * was received, with the additional sense code set 2878c66ac9dbSNicholas Bellinger * to REGISTRATIONS PREEMPTED. 2879c66ac9dbSNicholas Bellinger */ 28806708bb27SAndy Grover if (!all_reg) { 2881c66ac9dbSNicholas Bellinger if (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 pr_reg_nacl = pr_reg->pr_reg_nacl; 2887c66ac9dbSNicholas Bellinger pr_res_mapped_lun = pr_reg->pr_res_mapped_lun; 2888c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, pr_reg, 288933ce6a87SAndy Grover (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : 2890c66ac9dbSNicholas Bellinger NULL, calling_it_nexus); 2891c66ac9dbSNicholas Bellinger } else { 2892c66ac9dbSNicholas Bellinger /* 2893c66ac9dbSNicholas Bellinger * Case for any existing all registrants type 2894c66ac9dbSNicholas Bellinger * reservation, follow logic in spc4r17 section 2895c66ac9dbSNicholas Bellinger * 5.7.11.4 Preempting, Table 52 and Figure 7. 2896c66ac9dbSNicholas Bellinger * 2897c66ac9dbSNicholas Bellinger * For a ZERO SA Reservation key, release 2898125d0119SHannes Reinecke * all other registrations and do an implicit 2899c66ac9dbSNicholas Bellinger * release of active persistent reservation. 2900c66ac9dbSNicholas Bellinger * 2901c66ac9dbSNicholas Bellinger * For a non-ZERO SA Reservation key, only 2902c66ac9dbSNicholas Bellinger * release the matching reservation key from 2903c66ac9dbSNicholas Bellinger * registrations. 2904c66ac9dbSNicholas Bellinger */ 2905c66ac9dbSNicholas Bellinger if ((sa_res_key) && 2906c66ac9dbSNicholas Bellinger (pr_reg->pr_res_key != sa_res_key)) 2907c66ac9dbSNicholas Bellinger continue; 2908b6932ee3SSteven Allen sa_res_key_unmatched = false; 2909c66ac9dbSNicholas Bellinger 2910c66ac9dbSNicholas Bellinger calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; 2911c66ac9dbSNicholas Bellinger if (calling_it_nexus) 2912c66ac9dbSNicholas Bellinger continue; 2913c66ac9dbSNicholas Bellinger 2914c66ac9dbSNicholas Bellinger pr_reg_nacl = pr_reg->pr_reg_nacl; 2915c66ac9dbSNicholas Bellinger pr_res_mapped_lun = pr_reg->pr_res_mapped_lun; 2916c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, pr_reg, 291733ce6a87SAndy Grover (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : 2918c66ac9dbSNicholas Bellinger NULL, 0); 2919c66ac9dbSNicholas Bellinger } 29206708bb27SAndy Grover if (!calling_it_nexus) 2921c51c8e7bSHannes Reinecke target_ua_allocate_lun(pr_reg_nacl, 2922c66ac9dbSNicholas Bellinger pr_res_mapped_lun, 0x2A, 29239e08e34eSMarco Sanvido ASCQ_2AH_REGISTRATIONS_PREEMPTED); 2924c66ac9dbSNicholas Bellinger } 2925c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 2926c66ac9dbSNicholas Bellinger /* 2927c66ac9dbSNicholas Bellinger * If a PERSISTENT RESERVE OUT with a PREEMPT service action or 2928c66ac9dbSNicholas Bellinger * a PREEMPT AND ABORT service action sets the SERVICE ACTION 2929c66ac9dbSNicholas Bellinger * RESERVATION KEY field to a value that does not match any 2930c66ac9dbSNicholas Bellinger * registered reservation key, then the device server shall 2931c66ac9dbSNicholas Bellinger * complete the command with RESERVATION CONFLICT status. 2932c66ac9dbSNicholas Bellinger */ 2933b6932ee3SSteven Allen if (sa_res_key_unmatched) { 2934c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2935c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_n); 2936de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 2937c66ac9dbSNicholas Bellinger } 2938c66ac9dbSNicholas Bellinger /* 2939c66ac9dbSNicholas Bellinger * For an existing all registrants type reservation 2940c66ac9dbSNicholas Bellinger * with a zero SA rservation key, preempt the existing 2941c66ac9dbSNicholas Bellinger * reservation with the new PR type and scope. 2942c66ac9dbSNicholas Bellinger */ 2943c66ac9dbSNicholas Bellinger if (pr_res_holder && all_reg && !(sa_res_key)) { 2944c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_preempt(dev, pr_reg_n, 294533ce6a87SAndy Grover (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL, 294633ce6a87SAndy Grover type, scope, preempt_type); 2947c66ac9dbSNicholas Bellinger 294833ce6a87SAndy Grover if (preempt_type == PREEMPT_AND_ABORT) 2949c66ac9dbSNicholas Bellinger core_scsi3_release_preempt_and_abort( 2950c66ac9dbSNicholas Bellinger &preempt_and_abort_list, pr_reg_n); 2951c66ac9dbSNicholas Bellinger } 2952c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2953c66ac9dbSNicholas Bellinger 2954459f213bSAndy Grover if (pr_tmpl->pr_aptpl_active) 2955459f213bSAndy Grover core_scsi3_update_and_write_aptpl(cmd->se_dev, true); 2956c66ac9dbSNicholas Bellinger 2957c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_n); 29585951146dSAndy Grover core_scsi3_pr_generation(cmd->se_dev); 2959c66ac9dbSNicholas Bellinger return 0; 2960c66ac9dbSNicholas Bellinger } 2961c66ac9dbSNicholas Bellinger /* 2962c66ac9dbSNicholas Bellinger * The PREEMPTing SA reservation key matches that of the 2963c66ac9dbSNicholas Bellinger * existing persistent reservation, first, we check if 2964c66ac9dbSNicholas Bellinger * we are preempting our own reservation. 2965c66ac9dbSNicholas Bellinger * From spc4r17, section 5.7.11.4.3 Preempting 2966c66ac9dbSNicholas Bellinger * persistent reservations and registration handling 2967c66ac9dbSNicholas Bellinger * 2968c66ac9dbSNicholas Bellinger * If an all registrants persistent reservation is not 2969c66ac9dbSNicholas Bellinger * present, it is not an error for the persistent 2970c66ac9dbSNicholas Bellinger * reservation holder to preempt itself (i.e., a 2971c66ac9dbSNicholas Bellinger * PERSISTENT RESERVE OUT with a PREEMPT service action 2972c66ac9dbSNicholas Bellinger * or a PREEMPT AND ABORT service action with the 2973c66ac9dbSNicholas Bellinger * SERVICE ACTION RESERVATION KEY value equal to the 2974c66ac9dbSNicholas Bellinger * persistent reservation holder's reservation key that 2975c66ac9dbSNicholas Bellinger * is received from the persistent reservation holder). 2976c66ac9dbSNicholas Bellinger * In that case, the device server shall establish the 2977c66ac9dbSNicholas Bellinger * new persistent reservation and maintain the 2978c66ac9dbSNicholas Bellinger * registration. 2979c66ac9dbSNicholas Bellinger */ 2980c66ac9dbSNicholas Bellinger prh_type = pr_res_holder->pr_res_type; 2981c66ac9dbSNicholas Bellinger prh_scope = pr_res_holder->pr_res_scope; 2982c66ac9dbSNicholas Bellinger /* 2983c66ac9dbSNicholas Bellinger * If the SERVICE ACTION RESERVATION KEY field identifies a 2984c66ac9dbSNicholas Bellinger * persistent reservation holder (see 5.7.10), the device 2985c66ac9dbSNicholas Bellinger * server shall perform a preempt by doing the following as 2986c66ac9dbSNicholas Bellinger * an uninterrupted series of actions: 2987c66ac9dbSNicholas Bellinger * 2988c66ac9dbSNicholas Bellinger * a) Release the persistent reservation for the holder 2989c66ac9dbSNicholas Bellinger * identified by the SERVICE ACTION RESERVATION KEY field; 2990c66ac9dbSNicholas Bellinger */ 2991c66ac9dbSNicholas Bellinger if (pr_reg_n != pr_res_holder) 2992c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_release(dev, 2993c66ac9dbSNicholas Bellinger pr_res_holder->pr_reg_nacl, 29946c3c9baaSNicholas Bellinger dev->dev_pr_res_holder, 0, 0); 2995c66ac9dbSNicholas Bellinger /* 2996c66ac9dbSNicholas Bellinger * b) Remove the registrations for all I_T nexuses identified 2997c66ac9dbSNicholas Bellinger * by the SERVICE ACTION RESERVATION KEY field, except the 2998c66ac9dbSNicholas Bellinger * I_T nexus that is being used for the PERSISTENT RESERVE 2999c66ac9dbSNicholas Bellinger * OUT command. If an all registrants persistent reservation 3000c66ac9dbSNicholas Bellinger * is present and the SERVICE ACTION RESERVATION KEY field 3001c66ac9dbSNicholas Bellinger * is set to zero, then all registrations shall be removed 3002c66ac9dbSNicholas Bellinger * except for that of the I_T nexus that is being used for 3003c66ac9dbSNicholas Bellinger * the PERSISTENT RESERVE OUT command; 3004c66ac9dbSNicholas Bellinger */ 3005c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 3006c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 3007c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 3008c66ac9dbSNicholas Bellinger 3009c66ac9dbSNicholas Bellinger calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; 3010c66ac9dbSNicholas Bellinger if (calling_it_nexus) 3011c66ac9dbSNicholas Bellinger continue; 3012c66ac9dbSNicholas Bellinger 3013c66ac9dbSNicholas Bellinger if (pr_reg->pr_res_key != sa_res_key) 3014c66ac9dbSNicholas Bellinger continue; 3015c66ac9dbSNicholas Bellinger 3016c66ac9dbSNicholas Bellinger pr_reg_nacl = pr_reg->pr_reg_nacl; 3017c66ac9dbSNicholas Bellinger pr_res_mapped_lun = pr_reg->pr_res_mapped_lun; 3018c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, pr_reg, 301933ce6a87SAndy Grover (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL, 3020c66ac9dbSNicholas Bellinger calling_it_nexus); 3021c66ac9dbSNicholas Bellinger /* 3022c66ac9dbSNicholas Bellinger * e) Establish a unit attention condition for the initiator 3023c66ac9dbSNicholas Bellinger * port associated with every I_T nexus that lost its 3024c66ac9dbSNicholas Bellinger * persistent reservation and/or registration, with the 3025c66ac9dbSNicholas Bellinger * additional sense code set to REGISTRATIONS PREEMPTED; 3026c66ac9dbSNicholas Bellinger */ 3027c51c8e7bSHannes Reinecke target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun, 0x2A, 30289e08e34eSMarco Sanvido ASCQ_2AH_REGISTRATIONS_PREEMPTED); 3029c66ac9dbSNicholas Bellinger } 3030c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 3031c66ac9dbSNicholas Bellinger /* 3032c66ac9dbSNicholas Bellinger * c) Establish a persistent reservation for the preempting 3033c66ac9dbSNicholas Bellinger * I_T nexus using the contents of the SCOPE and TYPE fields; 3034c66ac9dbSNicholas Bellinger */ 3035c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_preempt(dev, pr_reg_n, 303633ce6a87SAndy Grover (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL, 303733ce6a87SAndy Grover type, scope, preempt_type); 3038c66ac9dbSNicholas Bellinger /* 3039c66ac9dbSNicholas Bellinger * d) Process tasks as defined in 5.7.1; 3040c66ac9dbSNicholas Bellinger * e) See above.. 3041c66ac9dbSNicholas Bellinger * f) If the type or scope has changed, then for every I_T nexus 3042c66ac9dbSNicholas Bellinger * whose reservation key was not removed, except for the I_T 3043c66ac9dbSNicholas Bellinger * nexus on which the PERSISTENT RESERVE OUT command was 3044c66ac9dbSNicholas Bellinger * received, the device server shall establish a unit 3045c66ac9dbSNicholas Bellinger * attention condition for the initiator port associated with 3046c66ac9dbSNicholas Bellinger * that I_T nexus, with the additional sense code set to 3047c66ac9dbSNicholas Bellinger * RESERVATIONS RELEASED. If the type or scope have not 3048c66ac9dbSNicholas Bellinger * changed, then no unit attention condition(s) shall be 3049c66ac9dbSNicholas Bellinger * established for this reason. 3050c66ac9dbSNicholas Bellinger */ 3051c66ac9dbSNicholas Bellinger if ((prh_type != type) || (prh_scope != scope)) { 3052c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 3053c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 3054c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 3055c66ac9dbSNicholas Bellinger 3056c66ac9dbSNicholas Bellinger calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; 3057c66ac9dbSNicholas Bellinger if (calling_it_nexus) 3058c66ac9dbSNicholas Bellinger continue; 3059c66ac9dbSNicholas Bellinger 3060c51c8e7bSHannes Reinecke target_ua_allocate_lun(pr_reg->pr_reg_nacl, 3061c66ac9dbSNicholas Bellinger pr_reg->pr_res_mapped_lun, 0x2A, 3062c66ac9dbSNicholas Bellinger ASCQ_2AH_RESERVATIONS_RELEASED); 3063c66ac9dbSNicholas Bellinger } 3064c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 3065c66ac9dbSNicholas Bellinger } 3066c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 3067c66ac9dbSNicholas Bellinger /* 3068c66ac9dbSNicholas Bellinger * Call LUN_RESET logic upon list of struct t10_pr_registration, 3069c66ac9dbSNicholas Bellinger * All received CDBs for the matching existing reservation and 3070c66ac9dbSNicholas Bellinger * registrations undergo ABORT_TASK logic. 3071c66ac9dbSNicholas Bellinger * 3072c66ac9dbSNicholas Bellinger * From there, core_scsi3_release_preempt_and_abort() will 3073c66ac9dbSNicholas Bellinger * release every registration in the list (which have already 3074c66ac9dbSNicholas Bellinger * been removed from the primary pr_reg list), except the 3075c66ac9dbSNicholas Bellinger * new persistent reservation holder, the calling Initiator Port. 3076c66ac9dbSNicholas Bellinger */ 307733ce6a87SAndy Grover if (preempt_type == PREEMPT_AND_ABORT) { 3078c66ac9dbSNicholas Bellinger core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd); 3079c66ac9dbSNicholas Bellinger core_scsi3_release_preempt_and_abort(&preempt_and_abort_list, 3080c66ac9dbSNicholas Bellinger pr_reg_n); 3081c66ac9dbSNicholas Bellinger } 3082c66ac9dbSNicholas Bellinger 3083459f213bSAndy Grover if (pr_tmpl->pr_aptpl_active) 3084459f213bSAndy Grover core_scsi3_update_and_write_aptpl(cmd->se_dev, true); 3085c66ac9dbSNicholas Bellinger 3086c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_n); 30875951146dSAndy Grover core_scsi3_pr_generation(cmd->se_dev); 3088c66ac9dbSNicholas Bellinger return 0; 3089c66ac9dbSNicholas Bellinger } 3090c66ac9dbSNicholas Bellinger 3091de103c93SChristoph Hellwig static sense_reason_t 3092de103c93SChristoph Hellwig core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope, 309333ce6a87SAndy Grover u64 res_key, u64 sa_res_key, enum preempt_type preempt_type) 3094c66ac9dbSNicholas Bellinger { 3095c66ac9dbSNicholas Bellinger switch (type) { 3096c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE: 3097c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS: 3098c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_REGONLY: 3099c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY: 3100c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_ALLREG: 3101c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG: 3102de103c93SChristoph Hellwig return core_scsi3_pro_preempt(cmd, type, scope, res_key, 310333ce6a87SAndy Grover sa_res_key, preempt_type); 3104c66ac9dbSNicholas Bellinger default: 31056708bb27SAndy Grover pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s" 310633ce6a87SAndy Grover " Type: 0x%02x\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", type); 3107de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 3108c66ac9dbSNicholas Bellinger } 3109c66ac9dbSNicholas Bellinger } 3110c66ac9dbSNicholas Bellinger 3111c66ac9dbSNicholas Bellinger 3112de103c93SChristoph Hellwig static sense_reason_t 3113de103c93SChristoph Hellwig core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key, 3114de103c93SChristoph Hellwig u64 sa_res_key, int aptpl, int unreg) 3115c66ac9dbSNicholas Bellinger { 3116e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 31175951146dSAndy Grover struct se_device *dev = cmd->se_dev; 311828168905SJörn Engel struct se_dev_entry *dest_se_deve = NULL; 3119adf653f9SChristoph Hellwig struct se_lun *se_lun = cmd->se_lun, *tmp_lun; 3120c66ac9dbSNicholas Bellinger struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL; 3121c66ac9dbSNicholas Bellinger struct se_portal_group *se_tpg, *dest_se_tpg = NULL; 31229ac8928eSChristoph Hellwig const struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops; 3123c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg; 31240fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 312505d1c7c0SAndy Grover unsigned char *buf; 31262650d71eSChristoph Hellwig const unsigned char *initiator_str; 312713ba564cSJulia Lawall char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN]; 3128c66ac9dbSNicholas Bellinger u32 tid_len, tmp_tid_len; 3129d2843c17SAndy Grover int new_reg = 0, type, scope, matching_iname; 3130de103c93SChristoph Hellwig sense_reason_t ret; 3131c66ac9dbSNicholas Bellinger unsigned short rtpi; 3132c66ac9dbSNicholas Bellinger unsigned char proto_ident; 3133c66ac9dbSNicholas Bellinger 31346708bb27SAndy Grover if (!se_sess || !se_lun) { 31356708bb27SAndy Grover pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n"); 3136de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3137c66ac9dbSNicholas Bellinger } 3138de103c93SChristoph Hellwig 3139c66ac9dbSNicholas Bellinger memset(i_buf, 0, PR_REG_ISID_ID_LEN); 3140c66ac9dbSNicholas Bellinger se_tpg = se_sess->se_tpg; 3141e3d6f909SAndy Grover tf_ops = se_tpg->se_tpg_tfo; 3142c66ac9dbSNicholas Bellinger /* 3143c66ac9dbSNicholas Bellinger * Follow logic from spc4r17 Section 5.7.8, Table 50 -- 3144c66ac9dbSNicholas Bellinger * Register behaviors for a REGISTER AND MOVE service action 3145c66ac9dbSNicholas Bellinger * 3146c66ac9dbSNicholas Bellinger * Locate the existing *pr_reg via struct se_node_acl pointers 3147c66ac9dbSNicholas Bellinger */ 31485951146dSAndy Grover pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl, 3149c66ac9dbSNicholas Bellinger se_sess); 31506708bb27SAndy Grover if (!pr_reg) { 31516708bb27SAndy Grover pr_err("SPC-3 PR: Unable to locate PR_REGISTERED" 3152c66ac9dbSNicholas Bellinger " *pr_reg for REGISTER_AND_MOVE\n"); 3153de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3154c66ac9dbSNicholas Bellinger } 3155c66ac9dbSNicholas Bellinger /* 3156c66ac9dbSNicholas Bellinger * The provided reservation key much match the existing reservation key 3157c66ac9dbSNicholas Bellinger * provided during this initiator's I_T nexus registration. 3158c66ac9dbSNicholas Bellinger */ 3159c66ac9dbSNicholas Bellinger if (res_key != pr_reg->pr_res_key) { 31606708bb27SAndy Grover pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received" 3161c66ac9dbSNicholas Bellinger " res_key: 0x%016Lx does not match existing SA REGISTER" 3162c66ac9dbSNicholas Bellinger " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key); 3163de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 3164de103c93SChristoph Hellwig goto out_put_pr_reg; 3165c66ac9dbSNicholas Bellinger } 3166c66ac9dbSNicholas Bellinger /* 3167c66ac9dbSNicholas Bellinger * The service active reservation key needs to be non zero 3168c66ac9dbSNicholas Bellinger */ 31696708bb27SAndy Grover if (!sa_res_key) { 31706708bb27SAndy Grover pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero" 3171c66ac9dbSNicholas Bellinger " sa_res_key\n"); 3172de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3173de103c93SChristoph Hellwig goto out_put_pr_reg; 3174c66ac9dbSNicholas Bellinger } 317505d1c7c0SAndy Grover 3176c66ac9dbSNicholas Bellinger /* 3177c66ac9dbSNicholas Bellinger * Determine the Relative Target Port Identifier where the reservation 3178c66ac9dbSNicholas Bellinger * will be moved to for the TransportID containing SCSI initiator WWN 3179c66ac9dbSNicholas Bellinger * information. 3180c66ac9dbSNicholas Bellinger */ 31814949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 3182de103c93SChristoph Hellwig if (!buf) { 3183de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3184de103c93SChristoph Hellwig goto out_put_pr_reg; 3185de103c93SChristoph Hellwig } 3186de103c93SChristoph Hellwig 3187c66ac9dbSNicholas Bellinger rtpi = (buf[18] & 0xff) << 8; 3188c66ac9dbSNicholas Bellinger rtpi |= buf[19] & 0xff; 3189c66ac9dbSNicholas Bellinger tid_len = (buf[20] & 0xff) << 24; 3190c66ac9dbSNicholas Bellinger tid_len |= (buf[21] & 0xff) << 16; 3191c66ac9dbSNicholas Bellinger tid_len |= (buf[22] & 0xff) << 8; 3192c66ac9dbSNicholas Bellinger tid_len |= buf[23] & 0xff; 31934949314cSAndy Grover transport_kunmap_data_sg(cmd); 319405d1c7c0SAndy Grover buf = NULL; 3195c66ac9dbSNicholas Bellinger 3196c66ac9dbSNicholas Bellinger if ((tid_len + 24) != cmd->data_length) { 31976708bb27SAndy Grover pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header" 3198c66ac9dbSNicholas Bellinger " does not equal CDB data_length: %u\n", tid_len, 3199c66ac9dbSNicholas Bellinger cmd->data_length); 3200de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3201de103c93SChristoph Hellwig goto out_put_pr_reg; 3202c66ac9dbSNicholas Bellinger } 3203c66ac9dbSNicholas Bellinger 3204c66ac9dbSNicholas Bellinger spin_lock(&dev->se_port_lock); 3205adf653f9SChristoph Hellwig list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) { 3206adf653f9SChristoph Hellwig if (tmp_lun->lun_rtpi != rtpi) 3207c66ac9dbSNicholas Bellinger continue; 3208adf653f9SChristoph Hellwig dest_se_tpg = tmp_lun->lun_tpg; 3209e3d6f909SAndy Grover dest_tf_ops = dest_se_tpg->se_tpg_tfo; 32106708bb27SAndy Grover if (!dest_tf_ops) 3211c66ac9dbSNicholas Bellinger continue; 3212c66ac9dbSNicholas Bellinger 321333940d09SJoern Engel atomic_inc_mb(&dest_se_tpg->tpg_pr_ref_count); 3214c66ac9dbSNicholas Bellinger spin_unlock(&dev->se_port_lock); 3215c66ac9dbSNicholas Bellinger 3216de103c93SChristoph Hellwig if (core_scsi3_tpg_depend_item(dest_se_tpg)) { 32176708bb27SAndy Grover pr_err("core_scsi3_tpg_depend_item() failed" 3218c66ac9dbSNicholas Bellinger " for dest_se_tpg\n"); 321933940d09SJoern Engel atomic_dec_mb(&dest_se_tpg->tpg_pr_ref_count); 3220de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3221de103c93SChristoph Hellwig goto out_put_pr_reg; 3222c66ac9dbSNicholas Bellinger } 3223c66ac9dbSNicholas Bellinger 3224c66ac9dbSNicholas Bellinger spin_lock(&dev->se_port_lock); 3225c66ac9dbSNicholas Bellinger break; 3226c66ac9dbSNicholas Bellinger } 3227c66ac9dbSNicholas Bellinger spin_unlock(&dev->se_port_lock); 3228c66ac9dbSNicholas Bellinger 32296708bb27SAndy Grover if (!dest_se_tpg || !dest_tf_ops) { 32306708bb27SAndy Grover pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate" 3231c66ac9dbSNicholas Bellinger " fabric ops from Relative Target Port Identifier:" 3232c66ac9dbSNicholas Bellinger " %hu\n", rtpi); 3233de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3234de103c93SChristoph Hellwig goto out_put_pr_reg; 3235c66ac9dbSNicholas Bellinger } 323605d1c7c0SAndy Grover 32374949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 3238de103c93SChristoph Hellwig if (!buf) { 3239de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3240de103c93SChristoph Hellwig goto out_put_pr_reg; 3241de103c93SChristoph Hellwig } 3242c66ac9dbSNicholas Bellinger proto_ident = (buf[24] & 0x0f); 32438b1e1244SAndy Grover 32446708bb27SAndy Grover pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:" 3245c66ac9dbSNicholas Bellinger " 0x%02x\n", proto_ident); 32468b1e1244SAndy Grover 32472aeeafaeSChristoph Hellwig if (proto_ident != dest_se_tpg->proto_id) { 32486708bb27SAndy Grover pr_err("SPC-3 PR REGISTER_AND_MOVE: Received" 3249c66ac9dbSNicholas Bellinger " proto_ident: 0x%02x does not match ident: 0x%02x" 3250c66ac9dbSNicholas Bellinger " from fabric: %s\n", proto_ident, 32512aeeafaeSChristoph Hellwig dest_se_tpg->proto_id, 3252c66ac9dbSNicholas Bellinger dest_tf_ops->get_fabric_name()); 3253de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3254c66ac9dbSNicholas Bellinger goto out; 3255c66ac9dbSNicholas Bellinger } 32562650d71eSChristoph Hellwig initiator_str = target_parse_pr_out_transport_id(dest_se_tpg, 3257c66ac9dbSNicholas Bellinger (const char *)&buf[24], &tmp_tid_len, &iport_ptr); 32586708bb27SAndy Grover if (!initiator_str) { 32596708bb27SAndy Grover pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate" 3260c66ac9dbSNicholas Bellinger " initiator_str from Transport ID\n"); 3261de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3262c66ac9dbSNicholas Bellinger goto out; 3263c66ac9dbSNicholas Bellinger } 3264c66ac9dbSNicholas Bellinger 32654949314cSAndy Grover transport_kunmap_data_sg(cmd); 326605d1c7c0SAndy Grover buf = NULL; 326705d1c7c0SAndy Grover 32686708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s" 3269c66ac9dbSNicholas Bellinger " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ? 3270c66ac9dbSNicholas Bellinger "port" : "device", initiator_str, (iport_ptr != NULL) ? 3271c66ac9dbSNicholas Bellinger iport_ptr : ""); 3272c66ac9dbSNicholas Bellinger /* 3273c66ac9dbSNicholas Bellinger * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service 3274c66ac9dbSNicholas Bellinger * action specifies a TransportID that is the same as the initiator port 3275c66ac9dbSNicholas Bellinger * of the I_T nexus for the command received, then the command shall 3276c66ac9dbSNicholas Bellinger * be terminated with CHECK CONDITION status, with the sense key set to 3277c66ac9dbSNicholas Bellinger * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD 3278c66ac9dbSNicholas Bellinger * IN PARAMETER LIST. 3279c66ac9dbSNicholas Bellinger */ 3280c66ac9dbSNicholas Bellinger pr_reg_nacl = pr_reg->pr_reg_nacl; 3281c66ac9dbSNicholas Bellinger matching_iname = (!strcmp(initiator_str, 3282c66ac9dbSNicholas Bellinger pr_reg_nacl->initiatorname)) ? 1 : 0; 32836708bb27SAndy Grover if (!matching_iname) 3284c66ac9dbSNicholas Bellinger goto after_iport_check; 3285c66ac9dbSNicholas Bellinger 32866708bb27SAndy Grover if (!iport_ptr || !pr_reg->isid_present_at_reg) { 32876708bb27SAndy Grover pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s" 3288c66ac9dbSNicholas Bellinger " matches: %s on received I_T Nexus\n", initiator_str, 3289c66ac9dbSNicholas Bellinger pr_reg_nacl->initiatorname); 3290de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3291c66ac9dbSNicholas Bellinger goto out; 3292c66ac9dbSNicholas Bellinger } 32936708bb27SAndy Grover if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) { 32946708bb27SAndy Grover pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s" 3295c66ac9dbSNicholas Bellinger " matches: %s %s on received I_T Nexus\n", 3296c66ac9dbSNicholas Bellinger initiator_str, iport_ptr, pr_reg_nacl->initiatorname, 3297c66ac9dbSNicholas Bellinger pr_reg->pr_reg_isid); 3298de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3299c66ac9dbSNicholas Bellinger goto out; 3300c66ac9dbSNicholas Bellinger } 3301c66ac9dbSNicholas Bellinger after_iport_check: 3302c66ac9dbSNicholas Bellinger /* 3303c66ac9dbSNicholas Bellinger * Locate the destination struct se_node_acl from the received Transport ID 3304c66ac9dbSNicholas Bellinger */ 3305403edd78SNicholas Bellinger mutex_lock(&dest_se_tpg->acl_node_mutex); 3306c66ac9dbSNicholas Bellinger dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg, 3307c66ac9dbSNicholas Bellinger initiator_str); 330833940d09SJoern Engel if (dest_node_acl) 330933940d09SJoern Engel atomic_inc_mb(&dest_node_acl->acl_pr_ref_count); 3310403edd78SNicholas Bellinger mutex_unlock(&dest_se_tpg->acl_node_mutex); 3311c66ac9dbSNicholas Bellinger 33126708bb27SAndy Grover if (!dest_node_acl) { 33136708bb27SAndy Grover pr_err("Unable to locate %s dest_node_acl for" 3314c66ac9dbSNicholas Bellinger " TransportID%s\n", dest_tf_ops->get_fabric_name(), 3315c66ac9dbSNicholas Bellinger initiator_str); 3316de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3317c66ac9dbSNicholas Bellinger goto out; 3318c66ac9dbSNicholas Bellinger } 3319de103c93SChristoph Hellwig 3320de103c93SChristoph Hellwig if (core_scsi3_nodeacl_depend_item(dest_node_acl)) { 33216708bb27SAndy Grover pr_err("core_scsi3_nodeacl_depend_item() for" 3322c66ac9dbSNicholas Bellinger " dest_node_acl\n"); 332333940d09SJoern Engel atomic_dec_mb(&dest_node_acl->acl_pr_ref_count); 3324c66ac9dbSNicholas Bellinger dest_node_acl = NULL; 3325de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3326c66ac9dbSNicholas Bellinger goto out; 3327c66ac9dbSNicholas Bellinger } 33288b1e1244SAndy Grover 33296708bb27SAndy Grover pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:" 3330c66ac9dbSNicholas Bellinger " %s from TransportID\n", dest_tf_ops->get_fabric_name(), 3331c66ac9dbSNicholas Bellinger dest_node_acl->initiatorname); 33328b1e1244SAndy Grover 3333c66ac9dbSNicholas Bellinger /* 3334c66ac9dbSNicholas Bellinger * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET 3335c66ac9dbSNicholas Bellinger * PORT IDENTIFIER. 3336c66ac9dbSNicholas Bellinger */ 3337c66ac9dbSNicholas Bellinger dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi); 33386708bb27SAndy Grover if (!dest_se_deve) { 33396708bb27SAndy Grover pr_err("Unable to locate %s dest_se_deve from RTPI:" 3340c66ac9dbSNicholas Bellinger " %hu\n", dest_tf_ops->get_fabric_name(), rtpi); 3341de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3342c66ac9dbSNicholas Bellinger goto out; 3343c66ac9dbSNicholas Bellinger } 3344c66ac9dbSNicholas Bellinger 3345de103c93SChristoph Hellwig if (core_scsi3_lunacl_depend_item(dest_se_deve)) { 33466708bb27SAndy Grover pr_err("core_scsi3_lunacl_depend_item() failed\n"); 334729a05deeSNicholas Bellinger kref_put(&dest_se_deve->pr_kref, target_pr_kref_release); 3348c66ac9dbSNicholas Bellinger dest_se_deve = NULL; 3349de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3350c66ac9dbSNicholas Bellinger goto out; 3351c66ac9dbSNicholas Bellinger } 33528b1e1244SAndy Grover 33536708bb27SAndy Grover pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN" 3354f2d30680SHannes Reinecke " ACL for dest_se_deve->mapped_lun: %llu\n", 3355c66ac9dbSNicholas Bellinger dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname, 3356c66ac9dbSNicholas Bellinger dest_se_deve->mapped_lun); 33578b1e1244SAndy Grover 3358c66ac9dbSNicholas Bellinger /* 3359c66ac9dbSNicholas Bellinger * A persistent reservation needs to already existing in order to 3360c66ac9dbSNicholas Bellinger * successfully complete the REGISTER_AND_MOVE service action.. 3361c66ac9dbSNicholas Bellinger */ 3362c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 3363c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 33646708bb27SAndy Grover if (!pr_res_holder) { 33656708bb27SAndy Grover pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation" 3366c66ac9dbSNicholas Bellinger " currently held\n"); 3367c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 3368de103c93SChristoph Hellwig ret = TCM_INVALID_CDB_FIELD; 3369c66ac9dbSNicholas Bellinger goto out; 3370c66ac9dbSNicholas Bellinger } 3371c66ac9dbSNicholas Bellinger /* 3372c66ac9dbSNicholas Bellinger * The received on I_T Nexus must be the reservation holder. 3373c66ac9dbSNicholas Bellinger * 3374c66ac9dbSNicholas Bellinger * From spc4r17 section 5.7.8 Table 50 -- 3375c66ac9dbSNicholas Bellinger * Register behaviors for a REGISTER AND MOVE service action 3376c66ac9dbSNicholas Bellinger */ 3377e673dc92SIlias Tsitsimpis if (!is_reservation_holder(pr_res_holder, pr_reg)) { 33786708bb27SAndy Grover pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T" 3379c66ac9dbSNicholas Bellinger " Nexus is not reservation holder\n"); 3380c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 3381de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 3382c66ac9dbSNicholas Bellinger goto out; 3383c66ac9dbSNicholas Bellinger } 3384c66ac9dbSNicholas Bellinger /* 3385c66ac9dbSNicholas Bellinger * From spc4r17 section 5.7.8: registering and moving reservation 3386c66ac9dbSNicholas Bellinger * 3387c66ac9dbSNicholas Bellinger * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service 3388c66ac9dbSNicholas Bellinger * action is received and the established persistent reservation is a 3389c66ac9dbSNicholas Bellinger * Write Exclusive - All Registrants type or Exclusive Access - 3390c66ac9dbSNicholas Bellinger * All Registrants type reservation, then the command shall be completed 3391c66ac9dbSNicholas Bellinger * with RESERVATION CONFLICT status. 3392c66ac9dbSNicholas Bellinger */ 3393c66ac9dbSNicholas Bellinger if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || 3394c66ac9dbSNicholas Bellinger (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) { 33956708bb27SAndy Grover pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move" 3396c66ac9dbSNicholas Bellinger " reservation for type: %s\n", 3397c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(pr_res_holder->pr_res_type)); 3398c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 3399de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 3400c66ac9dbSNicholas Bellinger goto out; 3401c66ac9dbSNicholas Bellinger } 3402c66ac9dbSNicholas Bellinger pr_res_nacl = pr_res_holder->pr_reg_nacl; 3403c66ac9dbSNicholas Bellinger /* 3404c66ac9dbSNicholas Bellinger * b) Ignore the contents of the (received) SCOPE and TYPE fields; 3405c66ac9dbSNicholas Bellinger */ 3406c66ac9dbSNicholas Bellinger type = pr_res_holder->pr_res_type; 3407c66ac9dbSNicholas Bellinger scope = pr_res_holder->pr_res_type; 3408c66ac9dbSNicholas Bellinger /* 3409c66ac9dbSNicholas Bellinger * c) Associate the reservation key specified in the SERVICE ACTION 3410c66ac9dbSNicholas Bellinger * RESERVATION KEY field with the I_T nexus specified as the 3411c66ac9dbSNicholas Bellinger * destination of the register and move, where: 3412c66ac9dbSNicholas Bellinger * A) The I_T nexus is specified by the TransportID and the 3413c66ac9dbSNicholas Bellinger * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and 3414c66ac9dbSNicholas Bellinger * B) Regardless of the TransportID format used, the association for 3415c66ac9dbSNicholas Bellinger * the initiator port is based on either the initiator port name 3416c66ac9dbSNicholas Bellinger * (see 3.1.71) on SCSI transport protocols where port names are 3417c66ac9dbSNicholas Bellinger * required or the initiator port identifier (see 3.1.70) on SCSI 3418c66ac9dbSNicholas Bellinger * transport protocols where port names are not required; 3419c66ac9dbSNicholas Bellinger * d) Register the reservation key specified in the SERVICE ACTION 3420c66ac9dbSNicholas Bellinger * RESERVATION KEY field; 3421c66ac9dbSNicholas Bellinger * e) Retain the reservation key specified in the SERVICE ACTION 3422c66ac9dbSNicholas Bellinger * RESERVATION KEY field and associated information; 3423c66ac9dbSNicholas Bellinger * 3424c66ac9dbSNicholas Bellinger * Also, It is not an error for a REGISTER AND MOVE service action to 3425c66ac9dbSNicholas Bellinger * register an I_T nexus that is already registered with the same 3426c66ac9dbSNicholas Bellinger * reservation key or a different reservation key. 3427c66ac9dbSNicholas Bellinger */ 3428c66ac9dbSNicholas Bellinger dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl, 3429c66ac9dbSNicholas Bellinger iport_ptr); 34306708bb27SAndy Grover if (!dest_pr_reg) { 343179dc9c9eSNicholas Bellinger struct se_lun *dest_lun = rcu_dereference_check(dest_se_deve->se_lun, 343279dc9c9eSNicholas Bellinger atomic_read(&dest_se_deve->pr_kref.refcount) != 0); 343379dc9c9eSNicholas Bellinger 343480bfdfa9SNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 343579dc9c9eSNicholas Bellinger if (core_scsi3_alloc_registration(cmd->se_dev, dest_node_acl, 343679dc9c9eSNicholas Bellinger dest_lun, dest_se_deve, dest_se_deve->mapped_lun, 343779dc9c9eSNicholas Bellinger iport_ptr, sa_res_key, 0, aptpl, 2, 1)) { 3438de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3439c66ac9dbSNicholas Bellinger goto out; 3440c66ac9dbSNicholas Bellinger } 344180bfdfa9SNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 3442c66ac9dbSNicholas Bellinger dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl, 3443c66ac9dbSNicholas Bellinger iport_ptr); 3444c66ac9dbSNicholas Bellinger new_reg = 1; 3445c66ac9dbSNicholas Bellinger } 3446c66ac9dbSNicholas Bellinger /* 3447c66ac9dbSNicholas Bellinger * f) Release the persistent reservation for the persistent reservation 3448c66ac9dbSNicholas Bellinger * holder (i.e., the I_T nexus on which the 3449c66ac9dbSNicholas Bellinger */ 3450c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_release(dev, pr_res_nacl, 34516c3c9baaSNicholas Bellinger dev->dev_pr_res_holder, 0, 0); 3452c66ac9dbSNicholas Bellinger /* 3453c66ac9dbSNicholas Bellinger * g) Move the persistent reservation to the specified I_T nexus using 3454c66ac9dbSNicholas Bellinger * the same scope and type as the persistent reservation released in 3455c66ac9dbSNicholas Bellinger * item f); and 3456c66ac9dbSNicholas Bellinger */ 3457c66ac9dbSNicholas Bellinger dev->dev_pr_res_holder = dest_pr_reg; 3458c66ac9dbSNicholas Bellinger dest_pr_reg->pr_res_holder = 1; 3459c66ac9dbSNicholas Bellinger dest_pr_reg->pr_res_type = type; 3460c66ac9dbSNicholas Bellinger pr_reg->pr_res_scope = scope; 3461d2843c17SAndy Grover core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN); 3462c66ac9dbSNicholas Bellinger /* 3463c66ac9dbSNicholas Bellinger * Increment PRGeneration for existing registrations.. 3464c66ac9dbSNicholas Bellinger */ 34656708bb27SAndy Grover if (!new_reg) 3466c66ac9dbSNicholas Bellinger dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++; 3467c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 3468c66ac9dbSNicholas Bellinger 34696708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE" 3470c66ac9dbSNicholas Bellinger " created new reservation holder TYPE: %s on object RTPI:" 3471c66ac9dbSNicholas Bellinger " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(), 3472c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(type), rtpi, 3473c66ac9dbSNicholas Bellinger dest_pr_reg->pr_res_generation); 34746708bb27SAndy Grover pr_debug("SPC-3 PR Successfully moved reservation from" 3475c66ac9dbSNicholas Bellinger " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n", 3476c66ac9dbSNicholas Bellinger tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname, 3477d2843c17SAndy Grover i_buf, dest_tf_ops->get_fabric_name(), 3478c66ac9dbSNicholas Bellinger dest_node_acl->initiatorname, (iport_ptr != NULL) ? 3479c66ac9dbSNicholas Bellinger iport_ptr : ""); 3480c66ac9dbSNicholas Bellinger /* 3481c66ac9dbSNicholas Bellinger * It is now safe to release configfs group dependencies for destination 3482c66ac9dbSNicholas Bellinger * of Transport ID Initiator Device/Port Identifier 3483c66ac9dbSNicholas Bellinger */ 3484c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(dest_se_deve); 3485c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 3486c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_se_tpg); 3487c66ac9dbSNicholas Bellinger /* 3488c66ac9dbSNicholas Bellinger * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T 3489c66ac9dbSNicholas Bellinger * nexus on which PERSISTENT RESERVE OUT command was received. 3490c66ac9dbSNicholas Bellinger */ 3491c66ac9dbSNicholas Bellinger if (unreg) { 3492c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 3493c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, pr_reg, NULL, 1); 3494c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 3495c66ac9dbSNicholas Bellinger } else 3496c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg); 3497c66ac9dbSNicholas Bellinger 3498459f213bSAndy Grover core_scsi3_update_and_write_aptpl(cmd->se_dev, aptpl); 3499c66ac9dbSNicholas Bellinger 35004949314cSAndy Grover transport_kunmap_data_sg(cmd); 350105d1c7c0SAndy Grover 3502c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(dest_pr_reg); 3503c66ac9dbSNicholas Bellinger return 0; 3504c66ac9dbSNicholas Bellinger out: 350505d1c7c0SAndy Grover if (buf) 35064949314cSAndy Grover transport_kunmap_data_sg(cmd); 3507c66ac9dbSNicholas Bellinger if (dest_se_deve) 3508c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(dest_se_deve); 3509c66ac9dbSNicholas Bellinger if (dest_node_acl) 3510c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 3511c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_se_tpg); 3512de103c93SChristoph Hellwig 3513de103c93SChristoph Hellwig out_put_pr_reg: 3514c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg); 3515c66ac9dbSNicholas Bellinger return ret; 3516c66ac9dbSNicholas Bellinger } 3517c66ac9dbSNicholas Bellinger 3518c66ac9dbSNicholas Bellinger static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb) 3519c66ac9dbSNicholas Bellinger { 3520c66ac9dbSNicholas Bellinger unsigned int __v1, __v2; 3521c66ac9dbSNicholas Bellinger 3522c66ac9dbSNicholas Bellinger __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3]; 3523c66ac9dbSNicholas Bellinger __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7]; 3524c66ac9dbSNicholas Bellinger 3525c66ac9dbSNicholas Bellinger return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32; 3526c66ac9dbSNicholas Bellinger } 3527c66ac9dbSNicholas Bellinger 3528c66ac9dbSNicholas Bellinger /* 3529c66ac9dbSNicholas Bellinger * See spc4r17 section 6.14 Table 170 3530c66ac9dbSNicholas Bellinger */ 3531de103c93SChristoph Hellwig sense_reason_t 3532de103c93SChristoph Hellwig target_scsi3_emulate_pr_out(struct se_cmd *cmd) 3533c66ac9dbSNicholas Bellinger { 353492404e60SNicholas Bellinger struct se_device *dev = cmd->se_dev; 3535617c0e06SChristoph Hellwig unsigned char *cdb = &cmd->t_task_cdb[0]; 353605d1c7c0SAndy Grover unsigned char *buf; 3537c66ac9dbSNicholas Bellinger u64 res_key, sa_res_key; 3538c66ac9dbSNicholas Bellinger int sa, scope, type, aptpl; 3539c66ac9dbSNicholas Bellinger int spec_i_pt = 0, all_tg_pt = 0, unreg = 0; 3540de103c93SChristoph Hellwig sense_reason_t ret; 3541617c0e06SChristoph Hellwig 3542617c0e06SChristoph Hellwig /* 3543617c0e06SChristoph Hellwig * Following spc2r20 5.5.1 Reservations overview: 3544617c0e06SChristoph Hellwig * 3545617c0e06SChristoph Hellwig * If a logical unit has been reserved by any RESERVE command and is 3546617c0e06SChristoph Hellwig * still reserved by any initiator, all PERSISTENT RESERVE IN and all 3547617c0e06SChristoph Hellwig * PERSISTENT RESERVE OUT commands shall conflict regardless of 3548617c0e06SChristoph Hellwig * initiator or service action and shall terminate with a RESERVATION 3549617c0e06SChristoph Hellwig * CONFLICT status. 3550617c0e06SChristoph Hellwig */ 35510fd97ccfSChristoph Hellwig if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) { 3552617c0e06SChristoph Hellwig pr_err("Received PERSISTENT_RESERVE CDB while legacy" 3553617c0e06SChristoph Hellwig " SPC-2 reservation is held, returning" 3554617c0e06SChristoph Hellwig " RESERVATION_CONFLICT\n"); 3555de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 3556617c0e06SChristoph Hellwig } 3557617c0e06SChristoph Hellwig 3558c66ac9dbSNicholas Bellinger /* 3559c66ac9dbSNicholas Bellinger * FIXME: A NULL struct se_session pointer means an this is not coming from 3560c66ac9dbSNicholas Bellinger * a $FABRIC_MOD's nexus, but from internal passthrough ops. 3561c66ac9dbSNicholas Bellinger */ 3562de103c93SChristoph Hellwig if (!cmd->se_sess) 3563de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3564c66ac9dbSNicholas Bellinger 3565c66ac9dbSNicholas Bellinger if (cmd->data_length < 24) { 35666708bb27SAndy Grover pr_warn("SPC-PR: Received PR OUT parameter list" 3567c66ac9dbSNicholas Bellinger " length too small: %u\n", cmd->data_length); 3568de103c93SChristoph Hellwig return TCM_INVALID_PARAMETER_LIST; 3569c66ac9dbSNicholas Bellinger } 3570de103c93SChristoph Hellwig 3571c66ac9dbSNicholas Bellinger /* 3572c66ac9dbSNicholas Bellinger * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB) 3573c66ac9dbSNicholas Bellinger */ 3574c66ac9dbSNicholas Bellinger sa = (cdb[1] & 0x1f); 3575c66ac9dbSNicholas Bellinger scope = (cdb[2] & 0xf0); 3576c66ac9dbSNicholas Bellinger type = (cdb[2] & 0x0f); 357705d1c7c0SAndy Grover 35784949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 3579de103c93SChristoph Hellwig if (!buf) 3580de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3581de103c93SChristoph Hellwig 3582c66ac9dbSNicholas Bellinger /* 3583c66ac9dbSNicholas Bellinger * From PERSISTENT_RESERVE_OUT parameter list (payload) 3584c66ac9dbSNicholas Bellinger */ 3585c66ac9dbSNicholas Bellinger res_key = core_scsi3_extract_reservation_key(&buf[0]); 3586c66ac9dbSNicholas Bellinger sa_res_key = core_scsi3_extract_reservation_key(&buf[8]); 3587c66ac9dbSNicholas Bellinger /* 3588c66ac9dbSNicholas Bellinger * REGISTER_AND_MOVE uses a different SA parameter list containing 3589c66ac9dbSNicholas Bellinger * SCSI TransportIDs. 3590c66ac9dbSNicholas Bellinger */ 3591c66ac9dbSNicholas Bellinger if (sa != PRO_REGISTER_AND_MOVE) { 3592c66ac9dbSNicholas Bellinger spec_i_pt = (buf[20] & 0x08); 3593c66ac9dbSNicholas Bellinger all_tg_pt = (buf[20] & 0x04); 3594c66ac9dbSNicholas Bellinger aptpl = (buf[20] & 0x01); 3595c66ac9dbSNicholas Bellinger } else { 3596c66ac9dbSNicholas Bellinger aptpl = (buf[17] & 0x01); 3597c66ac9dbSNicholas Bellinger unreg = (buf[17] & 0x02); 3598c66ac9dbSNicholas Bellinger } 359992404e60SNicholas Bellinger /* 360092404e60SNicholas Bellinger * If the backend device has been configured to force APTPL metadata 360192404e60SNicholas Bellinger * write-out, go ahead and propigate aptpl=1 down now. 360292404e60SNicholas Bellinger */ 360392404e60SNicholas Bellinger if (dev->dev_attrib.force_pr_aptpl) 360492404e60SNicholas Bellinger aptpl = 1; 360592404e60SNicholas Bellinger 36064949314cSAndy Grover transport_kunmap_data_sg(cmd); 360705d1c7c0SAndy Grover buf = NULL; 360805d1c7c0SAndy Grover 3609c66ac9dbSNicholas Bellinger /* 3610c66ac9dbSNicholas Bellinger * SPEC_I_PT=1 is only valid for Service action: REGISTER 3611c66ac9dbSNicholas Bellinger */ 3612de103c93SChristoph Hellwig if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER)) 3613de103c93SChristoph Hellwig return TCM_INVALID_PARAMETER_LIST; 3614d29a5b6aSChristoph Hellwig 3615c66ac9dbSNicholas Bellinger /* 3616c66ac9dbSNicholas Bellinger * From spc4r17 section 6.14: 3617c66ac9dbSNicholas Bellinger * 3618c66ac9dbSNicholas Bellinger * If the SPEC_I_PT bit is set to zero, the service action is not 3619c66ac9dbSNicholas Bellinger * REGISTER AND MOVE, and the parameter list length is not 24, then 3620c66ac9dbSNicholas Bellinger * the command shall be terminated with CHECK CONDITION status, with 3621c66ac9dbSNicholas Bellinger * the sense key set to ILLEGAL REQUEST, and the additional sense 3622c66ac9dbSNicholas Bellinger * code set to PARAMETER LIST LENGTH ERROR. 3623c66ac9dbSNicholas Bellinger */ 36246708bb27SAndy Grover if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) && 3625c66ac9dbSNicholas Bellinger (cmd->data_length != 24)) { 36266708bb27SAndy Grover pr_warn("SPC-PR: Received PR OUT illegal parameter" 3627c66ac9dbSNicholas Bellinger " list length: %u\n", cmd->data_length); 3628de103c93SChristoph Hellwig return TCM_INVALID_PARAMETER_LIST; 3629c66ac9dbSNicholas Bellinger } 3630de103c93SChristoph Hellwig 3631c66ac9dbSNicholas Bellinger /* 3632c66ac9dbSNicholas Bellinger * (core_scsi3_emulate_pro_* function parameters 3633c66ac9dbSNicholas Bellinger * are defined by spc4r17 Table 174: 3634c66ac9dbSNicholas Bellinger * PERSISTENT_RESERVE_OUT service actions and valid parameters. 3635c66ac9dbSNicholas Bellinger */ 3636c66ac9dbSNicholas Bellinger switch (sa) { 3637c66ac9dbSNicholas Bellinger case PRO_REGISTER: 3638d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_register(cmd, 363933ce6a87SAndy Grover res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER); 3640d29a5b6aSChristoph Hellwig break; 3641c66ac9dbSNicholas Bellinger case PRO_RESERVE: 3642d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key); 3643d29a5b6aSChristoph Hellwig break; 3644c66ac9dbSNicholas Bellinger case PRO_RELEASE: 3645d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key); 3646d29a5b6aSChristoph Hellwig break; 3647c66ac9dbSNicholas Bellinger case PRO_CLEAR: 3648d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_clear(cmd, res_key); 3649d29a5b6aSChristoph Hellwig break; 3650c66ac9dbSNicholas Bellinger case PRO_PREEMPT: 3651d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_preempt(cmd, type, scope, 365233ce6a87SAndy Grover res_key, sa_res_key, PREEMPT); 3653d29a5b6aSChristoph Hellwig break; 3654c66ac9dbSNicholas Bellinger case PRO_PREEMPT_AND_ABORT: 3655d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_preempt(cmd, type, scope, 365633ce6a87SAndy Grover res_key, sa_res_key, PREEMPT_AND_ABORT); 3657d29a5b6aSChristoph Hellwig break; 3658c66ac9dbSNicholas Bellinger case PRO_REGISTER_AND_IGNORE_EXISTING_KEY: 3659d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_register(cmd, 366033ce6a87SAndy Grover 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER_AND_IGNORE_EXISTING_KEY); 3661d29a5b6aSChristoph Hellwig break; 3662c66ac9dbSNicholas Bellinger case PRO_REGISTER_AND_MOVE: 3663d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key, 3664c66ac9dbSNicholas Bellinger sa_res_key, aptpl, unreg); 3665d29a5b6aSChristoph Hellwig break; 3666c66ac9dbSNicholas Bellinger default: 36676708bb27SAndy Grover pr_err("Unknown PERSISTENT_RESERVE_OUT service" 3668c66ac9dbSNicholas Bellinger " action: 0x%02x\n", cdb[1] & 0x1f); 3669de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 3670c66ac9dbSNicholas Bellinger } 3671c66ac9dbSNicholas Bellinger 36726bb35e00SChristoph Hellwig if (!ret) 36736bb35e00SChristoph Hellwig target_complete_cmd(cmd, GOOD); 3674d29a5b6aSChristoph Hellwig return ret; 3675c66ac9dbSNicholas Bellinger } 3676c66ac9dbSNicholas Bellinger 3677c66ac9dbSNicholas Bellinger /* 3678c66ac9dbSNicholas Bellinger * PERSISTENT_RESERVE_IN Service Action READ_KEYS 3679c66ac9dbSNicholas Bellinger * 3680c66ac9dbSNicholas Bellinger * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160 3681c66ac9dbSNicholas Bellinger */ 3682de103c93SChristoph Hellwig static sense_reason_t 3683de103c93SChristoph Hellwig core_scsi3_pri_read_keys(struct se_cmd *cmd) 3684c66ac9dbSNicholas Bellinger { 36850fd97ccfSChristoph Hellwig struct se_device *dev = cmd->se_dev; 3686c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg; 368705d1c7c0SAndy Grover unsigned char *buf; 3688c66ac9dbSNicholas Bellinger u32 add_len = 0, off = 8; 3689c66ac9dbSNicholas Bellinger 3690c66ac9dbSNicholas Bellinger if (cmd->data_length < 8) { 36916708bb27SAndy Grover pr_err("PRIN SA READ_KEYS SCSI Data Length: %u" 3692c66ac9dbSNicholas Bellinger " too small\n", cmd->data_length); 3693de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 3694c66ac9dbSNicholas Bellinger } 3695c66ac9dbSNicholas Bellinger 36964949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 3697de103c93SChristoph Hellwig if (!buf) 3698de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3699de103c93SChristoph Hellwig 37000fd97ccfSChristoph Hellwig buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff); 37010fd97ccfSChristoph Hellwig buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff); 37020fd97ccfSChristoph Hellwig buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff); 37030fd97ccfSChristoph Hellwig buf[3] = (dev->t10_pr.pr_generation & 0xff); 3704c66ac9dbSNicholas Bellinger 37050fd97ccfSChristoph Hellwig spin_lock(&dev->t10_pr.registration_lock); 37060fd97ccfSChristoph Hellwig list_for_each_entry(pr_reg, &dev->t10_pr.registration_list, 3707c66ac9dbSNicholas Bellinger pr_reg_list) { 3708c66ac9dbSNicholas Bellinger /* 3709c66ac9dbSNicholas Bellinger * Check for overflow of 8byte PRI READ_KEYS payload and 3710c66ac9dbSNicholas Bellinger * next reservation key list descriptor. 3711c66ac9dbSNicholas Bellinger */ 3712c66ac9dbSNicholas Bellinger if ((add_len + 8) > (cmd->data_length - 8)) 3713c66ac9dbSNicholas Bellinger break; 3714c66ac9dbSNicholas Bellinger 3715c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff); 3716c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff); 3717c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff); 3718c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff); 3719c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff); 3720c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff); 3721c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff); 3722c66ac9dbSNicholas Bellinger buf[off++] = (pr_reg->pr_res_key & 0xff); 3723c66ac9dbSNicholas Bellinger 3724c66ac9dbSNicholas Bellinger add_len += 8; 3725c66ac9dbSNicholas Bellinger } 37260fd97ccfSChristoph Hellwig spin_unlock(&dev->t10_pr.registration_lock); 3727c66ac9dbSNicholas Bellinger 3728c66ac9dbSNicholas Bellinger buf[4] = ((add_len >> 24) & 0xff); 3729c66ac9dbSNicholas Bellinger buf[5] = ((add_len >> 16) & 0xff); 3730c66ac9dbSNicholas Bellinger buf[6] = ((add_len >> 8) & 0xff); 3731c66ac9dbSNicholas Bellinger buf[7] = (add_len & 0xff); 3732c66ac9dbSNicholas Bellinger 37334949314cSAndy Grover transport_kunmap_data_sg(cmd); 373405d1c7c0SAndy Grover 3735c66ac9dbSNicholas Bellinger return 0; 3736c66ac9dbSNicholas Bellinger } 3737c66ac9dbSNicholas Bellinger 3738c66ac9dbSNicholas Bellinger /* 3739c66ac9dbSNicholas Bellinger * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION 3740c66ac9dbSNicholas Bellinger * 3741c66ac9dbSNicholas Bellinger * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162 3742c66ac9dbSNicholas Bellinger */ 3743de103c93SChristoph Hellwig static sense_reason_t 3744de103c93SChristoph Hellwig core_scsi3_pri_read_reservation(struct se_cmd *cmd) 3745c66ac9dbSNicholas Bellinger { 37460fd97ccfSChristoph Hellwig struct se_device *dev = cmd->se_dev; 3747c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg; 374805d1c7c0SAndy Grover unsigned char *buf; 3749c66ac9dbSNicholas Bellinger u64 pr_res_key; 3750c66ac9dbSNicholas Bellinger u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */ 3751c66ac9dbSNicholas Bellinger 3752c66ac9dbSNicholas Bellinger if (cmd->data_length < 8) { 37536708bb27SAndy Grover pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u" 3754c66ac9dbSNicholas Bellinger " too small\n", cmd->data_length); 3755de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 3756c66ac9dbSNicholas Bellinger } 3757c66ac9dbSNicholas Bellinger 37584949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 3759de103c93SChristoph Hellwig if (!buf) 3760de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3761de103c93SChristoph Hellwig 37620fd97ccfSChristoph Hellwig buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff); 37630fd97ccfSChristoph Hellwig buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff); 37640fd97ccfSChristoph Hellwig buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff); 37650fd97ccfSChristoph Hellwig buf[3] = (dev->t10_pr.pr_generation & 0xff); 3766c66ac9dbSNicholas Bellinger 37670fd97ccfSChristoph Hellwig spin_lock(&dev->dev_reservation_lock); 37680fd97ccfSChristoph Hellwig pr_reg = dev->dev_pr_res_holder; 3769ee1b1b9cSAndy Grover if (pr_reg) { 3770c66ac9dbSNicholas Bellinger /* 3771c66ac9dbSNicholas Bellinger * Set the hardcoded Additional Length 3772c66ac9dbSNicholas Bellinger */ 3773c66ac9dbSNicholas Bellinger buf[4] = ((add_len >> 24) & 0xff); 3774c66ac9dbSNicholas Bellinger buf[5] = ((add_len >> 16) & 0xff); 3775c66ac9dbSNicholas Bellinger buf[6] = ((add_len >> 8) & 0xff); 3776c66ac9dbSNicholas Bellinger buf[7] = (add_len & 0xff); 3777c66ac9dbSNicholas Bellinger 377805d1c7c0SAndy Grover if (cmd->data_length < 22) 377905d1c7c0SAndy Grover goto err; 378005d1c7c0SAndy Grover 3781c66ac9dbSNicholas Bellinger /* 3782c66ac9dbSNicholas Bellinger * Set the Reservation key. 3783c66ac9dbSNicholas Bellinger * 3784c66ac9dbSNicholas Bellinger * From spc4r17, section 5.7.10: 3785c66ac9dbSNicholas Bellinger * A persistent reservation holder has its reservation key 3786c66ac9dbSNicholas Bellinger * returned in the parameter data from a PERSISTENT 3787c66ac9dbSNicholas Bellinger * RESERVE IN command with READ RESERVATION service action as 3788c66ac9dbSNicholas Bellinger * follows: 3789c66ac9dbSNicholas Bellinger * a) For a persistent reservation of the type Write Exclusive 3790c66ac9dbSNicholas Bellinger * - All Registrants or Exclusive Access All Regitrants, 3791c66ac9dbSNicholas Bellinger * the reservation key shall be set to zero; or 3792c66ac9dbSNicholas Bellinger * b) For all other persistent reservation types, the 3793c66ac9dbSNicholas Bellinger * reservation key shall be set to the registered 3794c66ac9dbSNicholas Bellinger * reservation key for the I_T nexus that holds the 3795c66ac9dbSNicholas Bellinger * persistent reservation. 3796c66ac9dbSNicholas Bellinger */ 3797c66ac9dbSNicholas Bellinger if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || 3798c66ac9dbSNicholas Bellinger (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) 3799c66ac9dbSNicholas Bellinger pr_res_key = 0; 3800c66ac9dbSNicholas Bellinger else 3801c66ac9dbSNicholas Bellinger pr_res_key = pr_reg->pr_res_key; 3802c66ac9dbSNicholas Bellinger 3803c66ac9dbSNicholas Bellinger buf[8] = ((pr_res_key >> 56) & 0xff); 3804c66ac9dbSNicholas Bellinger buf[9] = ((pr_res_key >> 48) & 0xff); 3805c66ac9dbSNicholas Bellinger buf[10] = ((pr_res_key >> 40) & 0xff); 3806c66ac9dbSNicholas Bellinger buf[11] = ((pr_res_key >> 32) & 0xff); 3807c66ac9dbSNicholas Bellinger buf[12] = ((pr_res_key >> 24) & 0xff); 3808c66ac9dbSNicholas Bellinger buf[13] = ((pr_res_key >> 16) & 0xff); 3809c66ac9dbSNicholas Bellinger buf[14] = ((pr_res_key >> 8) & 0xff); 3810c66ac9dbSNicholas Bellinger buf[15] = (pr_res_key & 0xff); 3811c66ac9dbSNicholas Bellinger /* 3812c66ac9dbSNicholas Bellinger * Set the SCOPE and TYPE 3813c66ac9dbSNicholas Bellinger */ 3814c66ac9dbSNicholas Bellinger buf[21] = (pr_reg->pr_res_scope & 0xf0) | 3815c66ac9dbSNicholas Bellinger (pr_reg->pr_res_type & 0x0f); 3816c66ac9dbSNicholas Bellinger } 381705d1c7c0SAndy Grover 381805d1c7c0SAndy Grover err: 38190fd97ccfSChristoph Hellwig spin_unlock(&dev->dev_reservation_lock); 38204949314cSAndy Grover transport_kunmap_data_sg(cmd); 3821c66ac9dbSNicholas Bellinger 3822c66ac9dbSNicholas Bellinger return 0; 3823c66ac9dbSNicholas Bellinger } 3824c66ac9dbSNicholas Bellinger 3825c66ac9dbSNicholas Bellinger /* 3826c66ac9dbSNicholas Bellinger * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES 3827c66ac9dbSNicholas Bellinger * 3828c66ac9dbSNicholas Bellinger * See spc4r17 section 6.13.4 Table 165 3829c66ac9dbSNicholas Bellinger */ 3830de103c93SChristoph Hellwig static sense_reason_t 3831de103c93SChristoph Hellwig core_scsi3_pri_report_capabilities(struct se_cmd *cmd) 3832c66ac9dbSNicholas Bellinger { 38335951146dSAndy Grover struct se_device *dev = cmd->se_dev; 38340fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 383505d1c7c0SAndy Grover unsigned char *buf; 3836c66ac9dbSNicholas Bellinger u16 add_len = 8; /* Hardcoded to 8. */ 3837c66ac9dbSNicholas Bellinger 3838c66ac9dbSNicholas Bellinger if (cmd->data_length < 6) { 38396708bb27SAndy Grover pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:" 3840c66ac9dbSNicholas Bellinger " %u too small\n", cmd->data_length); 3841de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 3842c66ac9dbSNicholas Bellinger } 3843c66ac9dbSNicholas Bellinger 38444949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 3845de103c93SChristoph Hellwig if (!buf) 3846de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 384705d1c7c0SAndy Grover 3848c435285dSJoern Engel buf[0] = ((add_len >> 8) & 0xff); 3849c66ac9dbSNicholas Bellinger buf[1] = (add_len & 0xff); 3850c66ac9dbSNicholas Bellinger buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */ 3851c66ac9dbSNicholas Bellinger buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */ 3852c66ac9dbSNicholas Bellinger buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */ 3853c66ac9dbSNicholas Bellinger buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */ 3854c66ac9dbSNicholas Bellinger /* 3855c66ac9dbSNicholas Bellinger * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so 3856c66ac9dbSNicholas Bellinger * set the TMV: Task Mask Valid bit. 3857c66ac9dbSNicholas Bellinger */ 3858c66ac9dbSNicholas Bellinger buf[3] |= 0x80; 3859c66ac9dbSNicholas Bellinger /* 3860c66ac9dbSNicholas Bellinger * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166 3861c66ac9dbSNicholas Bellinger */ 3862c66ac9dbSNicholas Bellinger buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */ 3863c66ac9dbSNicholas Bellinger /* 3864c66ac9dbSNicholas Bellinger * PTPL_A: Persistence across Target Power Loss Active bit 3865c66ac9dbSNicholas Bellinger */ 3866c66ac9dbSNicholas Bellinger if (pr_tmpl->pr_aptpl_active) 3867c66ac9dbSNicholas Bellinger buf[3] |= 0x01; 3868c66ac9dbSNicholas Bellinger /* 3869c66ac9dbSNicholas Bellinger * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167 3870c66ac9dbSNicholas Bellinger */ 3871c66ac9dbSNicholas Bellinger buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */ 3872c66ac9dbSNicholas Bellinger buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */ 3873c66ac9dbSNicholas Bellinger buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */ 3874c66ac9dbSNicholas Bellinger buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */ 3875c66ac9dbSNicholas Bellinger buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */ 3876c66ac9dbSNicholas Bellinger buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */ 3877c66ac9dbSNicholas Bellinger 38784949314cSAndy Grover transport_kunmap_data_sg(cmd); 387905d1c7c0SAndy Grover 3880c66ac9dbSNicholas Bellinger return 0; 3881c66ac9dbSNicholas Bellinger } 3882c66ac9dbSNicholas Bellinger 3883c66ac9dbSNicholas Bellinger /* 3884c66ac9dbSNicholas Bellinger * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS 3885c66ac9dbSNicholas Bellinger * 3886c66ac9dbSNicholas Bellinger * See spc4r17 section 6.13.5 Table 168 and 169 3887c66ac9dbSNicholas Bellinger */ 3888de103c93SChristoph Hellwig static sense_reason_t 3889de103c93SChristoph Hellwig core_scsi3_pri_read_full_status(struct se_cmd *cmd) 3890c66ac9dbSNicholas Bellinger { 38910fd97ccfSChristoph Hellwig struct se_device *dev = cmd->se_dev; 3892c66ac9dbSNicholas Bellinger struct se_node_acl *se_nacl; 3893c66ac9dbSNicholas Bellinger struct se_portal_group *se_tpg; 3894c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp; 38950fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 389605d1c7c0SAndy Grover unsigned char *buf; 38972650d71eSChristoph Hellwig u32 add_desc_len = 0, add_len = 0; 3898c66ac9dbSNicholas Bellinger u32 off = 8; /* off into first Full Status descriptor */ 3899d16ca7c5SNicholas Bellinger int format_code = 0, pr_res_type = 0, pr_res_scope = 0; 39002650d71eSChristoph Hellwig int exp_desc_len, desc_len; 3901d16ca7c5SNicholas Bellinger bool all_reg = false; 3902c66ac9dbSNicholas Bellinger 3903c66ac9dbSNicholas Bellinger if (cmd->data_length < 8) { 39046708bb27SAndy Grover pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u" 3905c66ac9dbSNicholas Bellinger " too small\n", cmd->data_length); 3906de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 3907c66ac9dbSNicholas Bellinger } 3908c66ac9dbSNicholas Bellinger 39094949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 3910de103c93SChristoph Hellwig if (!buf) 3911de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 391205d1c7c0SAndy Grover 39130fd97ccfSChristoph Hellwig buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff); 39140fd97ccfSChristoph Hellwig buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff); 39150fd97ccfSChristoph Hellwig buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff); 39160fd97ccfSChristoph Hellwig buf[3] = (dev->t10_pr.pr_generation & 0xff); 3917c66ac9dbSNicholas Bellinger 3918d16ca7c5SNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 3919d16ca7c5SNicholas Bellinger if (dev->dev_pr_res_holder) { 3920d16ca7c5SNicholas Bellinger struct t10_pr_registration *pr_holder = dev->dev_pr_res_holder; 3921d16ca7c5SNicholas Bellinger 3922d16ca7c5SNicholas Bellinger if (pr_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG || 3923d16ca7c5SNicholas Bellinger pr_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG) { 3924d16ca7c5SNicholas Bellinger all_reg = true; 3925d16ca7c5SNicholas Bellinger pr_res_type = pr_holder->pr_res_type; 3926d16ca7c5SNicholas Bellinger pr_res_scope = pr_holder->pr_res_scope; 3927d16ca7c5SNicholas Bellinger } 3928d16ca7c5SNicholas Bellinger } 3929d16ca7c5SNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 3930d16ca7c5SNicholas Bellinger 3931c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 3932c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 3933c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 3934c66ac9dbSNicholas Bellinger 3935c66ac9dbSNicholas Bellinger se_nacl = pr_reg->pr_reg_nacl; 3936c66ac9dbSNicholas Bellinger se_tpg = pr_reg->pr_reg_nacl->se_tpg; 3937c66ac9dbSNicholas Bellinger add_desc_len = 0; 3938c66ac9dbSNicholas Bellinger 393933940d09SJoern Engel atomic_inc_mb(&pr_reg->pr_res_holders); 3940c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 3941c66ac9dbSNicholas Bellinger /* 3942c66ac9dbSNicholas Bellinger * Determine expected length of $FABRIC_MOD specific 3943c66ac9dbSNicholas Bellinger * TransportID full status descriptor.. 3944c66ac9dbSNicholas Bellinger */ 39452650d71eSChristoph Hellwig exp_desc_len = target_get_pr_transport_id_len(se_nacl, pr_reg, 39462650d71eSChristoph Hellwig &format_code); 39472650d71eSChristoph Hellwig if (exp_desc_len < 0 || 39482650d71eSChristoph Hellwig exp_desc_len + add_len > cmd->data_length) { 39496708bb27SAndy Grover pr_warn("SPC-3 PRIN READ_FULL_STATUS ran" 3950c66ac9dbSNicholas Bellinger " out of buffer: %d\n", cmd->data_length); 3951c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 395233940d09SJoern Engel atomic_dec_mb(&pr_reg->pr_res_holders); 3953c66ac9dbSNicholas Bellinger break; 3954c66ac9dbSNicholas Bellinger } 3955c66ac9dbSNicholas Bellinger /* 3956c66ac9dbSNicholas Bellinger * Set RESERVATION KEY 3957c66ac9dbSNicholas Bellinger */ 3958c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff); 3959c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff); 3960c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff); 3961c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff); 3962c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff); 3963c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff); 3964c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff); 3965c66ac9dbSNicholas Bellinger buf[off++] = (pr_reg->pr_res_key & 0xff); 3966c66ac9dbSNicholas Bellinger off += 4; /* Skip Over Reserved area */ 3967c66ac9dbSNicholas Bellinger 3968c66ac9dbSNicholas Bellinger /* 3969c66ac9dbSNicholas Bellinger * Set ALL_TG_PT bit if PROUT SA REGISTER had this set. 3970c66ac9dbSNicholas Bellinger */ 3971c66ac9dbSNicholas Bellinger if (pr_reg->pr_reg_all_tg_pt) 3972c66ac9dbSNicholas Bellinger buf[off] = 0x02; 3973c66ac9dbSNicholas Bellinger /* 3974c66ac9dbSNicholas Bellinger * The struct se_lun pointer will be present for the 3975c66ac9dbSNicholas Bellinger * reservation holder for PR_HOLDER bit. 3976c66ac9dbSNicholas Bellinger * 3977c66ac9dbSNicholas Bellinger * Also, if this registration is the reservation 3978d16ca7c5SNicholas Bellinger * holder or there is an All Registrants reservation 3979d16ca7c5SNicholas Bellinger * active, fill in SCOPE and TYPE in the next byte. 3980c66ac9dbSNicholas Bellinger */ 3981c66ac9dbSNicholas Bellinger if (pr_reg->pr_res_holder) { 3982c66ac9dbSNicholas Bellinger buf[off++] |= 0x01; 3983c66ac9dbSNicholas Bellinger buf[off++] = (pr_reg->pr_res_scope & 0xf0) | 3984c66ac9dbSNicholas Bellinger (pr_reg->pr_res_type & 0x0f); 3985d16ca7c5SNicholas Bellinger } else if (all_reg) { 3986d16ca7c5SNicholas Bellinger buf[off++] |= 0x01; 3987d16ca7c5SNicholas Bellinger buf[off++] = (pr_res_scope & 0xf0) | 3988d16ca7c5SNicholas Bellinger (pr_res_type & 0x0f); 3989d16ca7c5SNicholas Bellinger } else { 3990c66ac9dbSNicholas Bellinger off += 2; 3991d16ca7c5SNicholas Bellinger } 3992c66ac9dbSNicholas Bellinger 3993c66ac9dbSNicholas Bellinger off += 4; /* Skip over reserved area */ 3994c66ac9dbSNicholas Bellinger /* 3995c66ac9dbSNicholas Bellinger * From spc4r17 6.3.15: 3996c66ac9dbSNicholas Bellinger * 3997c66ac9dbSNicholas Bellinger * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT 3998c66ac9dbSNicholas Bellinger * IDENTIFIER field contains the relative port identifier (see 3999c66ac9dbSNicholas Bellinger * 3.1.120) of the target port that is part of the I_T nexus 4000c66ac9dbSNicholas Bellinger * described by this full status descriptor. If the ALL_TG_PT 4001c66ac9dbSNicholas Bellinger * bit is set to one, the contents of the RELATIVE TARGET PORT 4002c66ac9dbSNicholas Bellinger * IDENTIFIER field are not defined by this standard. 4003c66ac9dbSNicholas Bellinger */ 40046708bb27SAndy Grover if (!pr_reg->pr_reg_all_tg_pt) { 400579dc9c9eSNicholas Bellinger u16 sep_rtpi = pr_reg->tg_pt_sep_rtpi; 4006c66ac9dbSNicholas Bellinger 400779dc9c9eSNicholas Bellinger buf[off++] = ((sep_rtpi >> 8) & 0xff); 400879dc9c9eSNicholas Bellinger buf[off++] = (sep_rtpi & 0xff); 4009c66ac9dbSNicholas Bellinger } else 401035d1efe8SMasanari Iida off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */ 4011c66ac9dbSNicholas Bellinger 40122650d71eSChristoph Hellwig buf[off+4] = se_tpg->proto_id; 40132650d71eSChristoph Hellwig 4014c66ac9dbSNicholas Bellinger /* 40152650d71eSChristoph Hellwig * Now, have the $FABRIC_MOD fill in the transport ID. 4016c66ac9dbSNicholas Bellinger */ 40172650d71eSChristoph Hellwig desc_len = target_get_pr_transport_id(se_nacl, pr_reg, 40182650d71eSChristoph Hellwig &format_code, &buf[off+4]); 4019c66ac9dbSNicholas Bellinger 4020c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 402133940d09SJoern Engel atomic_dec_mb(&pr_reg->pr_res_holders); 40222650d71eSChristoph Hellwig 40232650d71eSChristoph Hellwig if (desc_len < 0) 40242650d71eSChristoph Hellwig break; 4025c66ac9dbSNicholas Bellinger /* 4026c66ac9dbSNicholas Bellinger * Set the ADDITIONAL DESCRIPTOR LENGTH 4027c66ac9dbSNicholas Bellinger */ 4028c66ac9dbSNicholas Bellinger buf[off++] = ((desc_len >> 24) & 0xff); 4029c66ac9dbSNicholas Bellinger buf[off++] = ((desc_len >> 16) & 0xff); 4030c66ac9dbSNicholas Bellinger buf[off++] = ((desc_len >> 8) & 0xff); 4031c66ac9dbSNicholas Bellinger buf[off++] = (desc_len & 0xff); 4032c66ac9dbSNicholas Bellinger /* 4033c66ac9dbSNicholas Bellinger * Size of full desctipor header minus TransportID 4034c66ac9dbSNicholas Bellinger * containing $FABRIC_MOD specific) initiator device/port 4035c66ac9dbSNicholas Bellinger * WWN information. 4036c66ac9dbSNicholas Bellinger * 4037c66ac9dbSNicholas Bellinger * See spc4r17 Section 6.13.5 Table 169 4038c66ac9dbSNicholas Bellinger */ 4039c66ac9dbSNicholas Bellinger add_desc_len = (24 + desc_len); 4040c66ac9dbSNicholas Bellinger 4041c66ac9dbSNicholas Bellinger off += desc_len; 4042c66ac9dbSNicholas Bellinger add_len += add_desc_len; 4043c66ac9dbSNicholas Bellinger } 4044c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 4045c66ac9dbSNicholas Bellinger /* 4046c66ac9dbSNicholas Bellinger * Set ADDITIONAL_LENGTH 4047c66ac9dbSNicholas Bellinger */ 4048c66ac9dbSNicholas Bellinger buf[4] = ((add_len >> 24) & 0xff); 4049c66ac9dbSNicholas Bellinger buf[5] = ((add_len >> 16) & 0xff); 4050c66ac9dbSNicholas Bellinger buf[6] = ((add_len >> 8) & 0xff); 4051c66ac9dbSNicholas Bellinger buf[7] = (add_len & 0xff); 4052c66ac9dbSNicholas Bellinger 40534949314cSAndy Grover transport_kunmap_data_sg(cmd); 405405d1c7c0SAndy Grover 4055c66ac9dbSNicholas Bellinger return 0; 4056c66ac9dbSNicholas Bellinger } 4057c66ac9dbSNicholas Bellinger 4058de103c93SChristoph Hellwig sense_reason_t 4059de103c93SChristoph Hellwig target_scsi3_emulate_pr_in(struct se_cmd *cmd) 4060c66ac9dbSNicholas Bellinger { 4061de103c93SChristoph Hellwig sense_reason_t ret; 4062e76a35d6SChristoph Hellwig 4063617c0e06SChristoph Hellwig /* 4064617c0e06SChristoph Hellwig * Following spc2r20 5.5.1 Reservations overview: 4065617c0e06SChristoph Hellwig * 4066617c0e06SChristoph Hellwig * If a logical unit has been reserved by any RESERVE command and is 4067617c0e06SChristoph Hellwig * still reserved by any initiator, all PERSISTENT RESERVE IN and all 4068617c0e06SChristoph Hellwig * PERSISTENT RESERVE OUT commands shall conflict regardless of 4069617c0e06SChristoph Hellwig * initiator or service action and shall terminate with a RESERVATION 4070617c0e06SChristoph Hellwig * CONFLICT status. 4071617c0e06SChristoph Hellwig */ 40720fd97ccfSChristoph Hellwig if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) { 4073617c0e06SChristoph Hellwig pr_err("Received PERSISTENT_RESERVE CDB while legacy" 4074617c0e06SChristoph Hellwig " SPC-2 reservation is held, returning" 4075617c0e06SChristoph Hellwig " RESERVATION_CONFLICT\n"); 4076de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 4077617c0e06SChristoph Hellwig } 4078617c0e06SChristoph Hellwig 4079617c0e06SChristoph Hellwig switch (cmd->t_task_cdb[1] & 0x1f) { 4080c66ac9dbSNicholas Bellinger case PRI_READ_KEYS: 4081d29a5b6aSChristoph Hellwig ret = core_scsi3_pri_read_keys(cmd); 4082d29a5b6aSChristoph Hellwig break; 4083c66ac9dbSNicholas Bellinger case PRI_READ_RESERVATION: 4084d29a5b6aSChristoph Hellwig ret = core_scsi3_pri_read_reservation(cmd); 4085d29a5b6aSChristoph Hellwig break; 4086c66ac9dbSNicholas Bellinger case PRI_REPORT_CAPABILITIES: 4087d29a5b6aSChristoph Hellwig ret = core_scsi3_pri_report_capabilities(cmd); 4088d29a5b6aSChristoph Hellwig break; 4089c66ac9dbSNicholas Bellinger case PRI_READ_FULL_STATUS: 4090d29a5b6aSChristoph Hellwig ret = core_scsi3_pri_read_full_status(cmd); 4091d29a5b6aSChristoph Hellwig break; 4092c66ac9dbSNicholas Bellinger default: 40936708bb27SAndy Grover pr_err("Unknown PERSISTENT_RESERVE_IN service" 4094617c0e06SChristoph Hellwig " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f); 4095de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 4096c66ac9dbSNicholas Bellinger } 4097d29a5b6aSChristoph Hellwig 40986bb35e00SChristoph Hellwig if (!ret) 40996bb35e00SChristoph Hellwig target_complete_cmd(cmd, GOOD); 4100d29a5b6aSChristoph Hellwig return ret; 4101c66ac9dbSNicholas Bellinger } 4102c66ac9dbSNicholas Bellinger 4103de103c93SChristoph Hellwig sense_reason_t 4104de103c93SChristoph Hellwig target_check_reservation(struct se_cmd *cmd) 4105c66ac9dbSNicholas Bellinger { 4106d977f437SChristoph Hellwig struct se_device *dev = cmd->se_dev; 4107de103c93SChristoph Hellwig sense_reason_t ret; 4108d977f437SChristoph Hellwig 4109d977f437SChristoph Hellwig if (!cmd->se_sess) 4110c66ac9dbSNicholas Bellinger return 0; 4111d977f437SChristoph Hellwig if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE) 4112c66ac9dbSNicholas Bellinger return 0; 4113a3541703SAndy Grover if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH) 4114d977f437SChristoph Hellwig return 0; 4115c66ac9dbSNicholas Bellinger 4116d977f437SChristoph Hellwig spin_lock(&dev->dev_reservation_lock); 4117d977f437SChristoph Hellwig if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) 4118d977f437SChristoph Hellwig ret = target_scsi2_reservation_check(cmd); 4119d977f437SChristoph Hellwig else 4120d977f437SChristoph Hellwig ret = target_scsi3_pr_reservation_check(cmd); 4121d977f437SChristoph Hellwig spin_unlock(&dev->dev_reservation_lock); 41220fd97ccfSChristoph Hellwig 4123d977f437SChristoph Hellwig return ret; 4124c66ac9dbSNicholas Bellinger } 4125