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 * 7fd9a11d7SNicholas Bellinger * (c) Copyright 2009-2012 RisingTide Systems LLC. 8c66ac9dbSNicholas Bellinger * 9c66ac9dbSNicholas Bellinger * Nicholas A. Bellinger <nab@kernel.org> 10c66ac9dbSNicholas Bellinger * 11c66ac9dbSNicholas Bellinger * This program is free software; you can redistribute it and/or modify 12c66ac9dbSNicholas Bellinger * it under the terms of the GNU General Public License as published by 13c66ac9dbSNicholas Bellinger * the Free Software Foundation; either version 2 of the License, or 14c66ac9dbSNicholas Bellinger * (at your option) any later version. 15c66ac9dbSNicholas Bellinger * 16c66ac9dbSNicholas Bellinger * This program is distributed in the hope that it will be useful, 17c66ac9dbSNicholas Bellinger * but WITHOUT ANY WARRANTY; without even the implied warranty of 18c66ac9dbSNicholas Bellinger * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19c66ac9dbSNicholas Bellinger * GNU General Public License for more details. 20c66ac9dbSNicholas Bellinger * 21c66ac9dbSNicholas Bellinger * You should have received a copy of the GNU General Public License 22c66ac9dbSNicholas Bellinger * along with this program; if not, write to the Free Software 23c66ac9dbSNicholas Bellinger * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 24c66ac9dbSNicholas Bellinger * 25c66ac9dbSNicholas Bellinger ******************************************************************************/ 26c66ac9dbSNicholas Bellinger 27c66ac9dbSNicholas Bellinger #include <linux/slab.h> 28c66ac9dbSNicholas Bellinger #include <linux/spinlock.h> 29c66ac9dbSNicholas Bellinger #include <linux/list.h> 300e9b10a9SAl Viro #include <linux/file.h> 31c66ac9dbSNicholas Bellinger #include <scsi/scsi.h> 32c66ac9dbSNicholas Bellinger #include <scsi/scsi_cmnd.h> 33c66ac9dbSNicholas Bellinger #include <asm/unaligned.h> 34c66ac9dbSNicholas Bellinger 35c66ac9dbSNicholas Bellinger #include <target/target_core_base.h> 36c4795fb2SChristoph Hellwig #include <target/target_core_backend.h> 37c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h> 38c66ac9dbSNicholas Bellinger #include <target/target_core_configfs.h> 39c66ac9dbSNicholas Bellinger 40e26d99aeSChristoph Hellwig #include "target_core_internal.h" 41c66ac9dbSNicholas Bellinger #include "target_core_pr.h" 42c66ac9dbSNicholas Bellinger #include "target_core_ua.h" 43c66ac9dbSNicholas Bellinger 44c66ac9dbSNicholas Bellinger /* 45c66ac9dbSNicholas Bellinger * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT) 46c66ac9dbSNicholas Bellinger */ 47c66ac9dbSNicholas Bellinger struct pr_transport_id_holder { 48c66ac9dbSNicholas Bellinger int dest_local_nexus; 49c66ac9dbSNicholas Bellinger struct t10_pr_registration *dest_pr_reg; 50c66ac9dbSNicholas Bellinger struct se_portal_group *dest_tpg; 51c66ac9dbSNicholas Bellinger struct se_node_acl *dest_node_acl; 52c66ac9dbSNicholas Bellinger struct se_dev_entry *dest_se_deve; 53c66ac9dbSNicholas Bellinger struct list_head dest_list; 54c66ac9dbSNicholas Bellinger }; 55c66ac9dbSNicholas Bellinger 56c66ac9dbSNicholas Bellinger int core_pr_dump_initiator_port( 57c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, 58c66ac9dbSNicholas Bellinger char *buf, 59c66ac9dbSNicholas Bellinger u32 size) 60c66ac9dbSNicholas Bellinger { 616708bb27SAndy Grover if (!pr_reg->isid_present_at_reg) 62c66ac9dbSNicholas Bellinger return 0; 63c66ac9dbSNicholas Bellinger 64c66ac9dbSNicholas Bellinger snprintf(buf, size, ",i,0x%s", &pr_reg->pr_reg_isid[0]); 65c66ac9dbSNicholas Bellinger return 1; 66c66ac9dbSNicholas Bellinger } 67c66ac9dbSNicholas Bellinger 68c66ac9dbSNicholas Bellinger static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *, 69c66ac9dbSNicholas Bellinger struct t10_pr_registration *, int); 70c66ac9dbSNicholas Bellinger 71de103c93SChristoph Hellwig static sense_reason_t 72de103c93SChristoph Hellwig target_scsi2_reservation_check(struct se_cmd *cmd) 73c66ac9dbSNicholas Bellinger { 74d977f437SChristoph Hellwig struct se_device *dev = cmd->se_dev; 75d977f437SChristoph Hellwig struct se_session *sess = cmd->se_sess; 76d977f437SChristoph Hellwig 77d977f437SChristoph Hellwig switch (cmd->t_task_cdb[0]) { 78c66ac9dbSNicholas Bellinger case INQUIRY: 79c66ac9dbSNicholas Bellinger case RELEASE: 80c66ac9dbSNicholas Bellinger case RELEASE_10: 81c66ac9dbSNicholas Bellinger return 0; 82c66ac9dbSNicholas Bellinger default: 83d977f437SChristoph Hellwig break; 84c66ac9dbSNicholas Bellinger } 85c66ac9dbSNicholas Bellinger 86d977f437SChristoph Hellwig if (!dev->dev_reserved_node_acl || !sess) 87c66ac9dbSNicholas Bellinger return 0; 88c66ac9dbSNicholas Bellinger 89d977f437SChristoph Hellwig if (dev->dev_reserved_node_acl != sess->se_node_acl) 90de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 91c66ac9dbSNicholas Bellinger 92d977f437SChristoph Hellwig if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) { 93d977f437SChristoph Hellwig if (dev->dev_res_bin_isid != sess->sess_bin_isid) 94de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 95d977f437SChristoph Hellwig } 96d977f437SChristoph Hellwig 97d977f437SChristoph Hellwig return 0; 98c66ac9dbSNicholas Bellinger } 99c66ac9dbSNicholas Bellinger 100eacac00cSChristoph Hellwig static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *, 101eacac00cSChristoph Hellwig struct se_node_acl *, struct se_session *); 102eacac00cSChristoph Hellwig static void core_scsi3_put_pr_reg(struct t10_pr_registration *); 103eacac00cSChristoph Hellwig 104087a03b3SNicholas Bellinger static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd) 105eacac00cSChristoph Hellwig { 106eacac00cSChristoph Hellwig struct se_session *se_sess = cmd->se_sess; 1070fd97ccfSChristoph Hellwig struct se_device *dev = cmd->se_dev; 108eacac00cSChristoph Hellwig struct t10_pr_registration *pr_reg; 1090fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 110eacac00cSChristoph Hellwig int conflict = 0; 111eacac00cSChristoph Hellwig 112eacac00cSChristoph Hellwig pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl, 113eacac00cSChristoph Hellwig se_sess); 114eacac00cSChristoph Hellwig if (pr_reg) { 115eacac00cSChristoph Hellwig /* 116eacac00cSChristoph Hellwig * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE 117eacac00cSChristoph Hellwig * behavior 118eacac00cSChristoph Hellwig * 119eacac00cSChristoph Hellwig * A RESERVE(6) or RESERVE(10) command shall complete with GOOD 120eacac00cSChristoph Hellwig * status, but no reservation shall be established and the 121eacac00cSChristoph Hellwig * persistent reservation shall not be changed, if the command 122eacac00cSChristoph Hellwig * is received from a) and b) below. 123eacac00cSChristoph Hellwig * 124eacac00cSChristoph Hellwig * A RELEASE(6) or RELEASE(10) command shall complete with GOOD 125eacac00cSChristoph Hellwig * status, but the persistent reservation shall not be released, 126eacac00cSChristoph Hellwig * if the command is received from a) and b) 127eacac00cSChristoph Hellwig * 128eacac00cSChristoph Hellwig * a) An I_T nexus that is a persistent reservation holder; or 129eacac00cSChristoph Hellwig * b) An I_T nexus that is registered if a registrants only or 130eacac00cSChristoph Hellwig * all registrants type persistent reservation is present. 131eacac00cSChristoph Hellwig * 132eacac00cSChristoph Hellwig * In all other cases, a RESERVE(6) command, RESERVE(10) command, 133eacac00cSChristoph Hellwig * RELEASE(6) command, or RELEASE(10) command shall be processed 134eacac00cSChristoph Hellwig * as defined in SPC-2. 135eacac00cSChristoph Hellwig */ 136eacac00cSChristoph Hellwig if (pr_reg->pr_res_holder) { 137eacac00cSChristoph Hellwig core_scsi3_put_pr_reg(pr_reg); 138087a03b3SNicholas Bellinger return 1; 139eacac00cSChristoph Hellwig } 140eacac00cSChristoph Hellwig if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) || 141eacac00cSChristoph Hellwig (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) || 142eacac00cSChristoph Hellwig (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || 143eacac00cSChristoph Hellwig (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) { 144eacac00cSChristoph Hellwig core_scsi3_put_pr_reg(pr_reg); 145087a03b3SNicholas Bellinger return 1; 146eacac00cSChristoph Hellwig } 147eacac00cSChristoph Hellwig core_scsi3_put_pr_reg(pr_reg); 148eacac00cSChristoph Hellwig conflict = 1; 149eacac00cSChristoph Hellwig } else { 150eacac00cSChristoph Hellwig /* 151eacac00cSChristoph Hellwig * Following spc2r20 5.5.1 Reservations overview: 152eacac00cSChristoph Hellwig * 153eacac00cSChristoph Hellwig * If a logical unit has executed a PERSISTENT RESERVE OUT 154eacac00cSChristoph Hellwig * command with the REGISTER or the REGISTER AND IGNORE 155eacac00cSChristoph Hellwig * EXISTING KEY service action and is still registered by any 156eacac00cSChristoph Hellwig * initiator, all RESERVE commands and all RELEASE commands 157eacac00cSChristoph Hellwig * regardless of initiator shall conflict and shall terminate 158eacac00cSChristoph Hellwig * with a RESERVATION CONFLICT status. 159eacac00cSChristoph Hellwig */ 160eacac00cSChristoph Hellwig spin_lock(&pr_tmpl->registration_lock); 161eacac00cSChristoph Hellwig conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1; 162eacac00cSChristoph Hellwig spin_unlock(&pr_tmpl->registration_lock); 163eacac00cSChristoph Hellwig } 164eacac00cSChristoph Hellwig 165eacac00cSChristoph Hellwig if (conflict) { 166eacac00cSChristoph Hellwig pr_err("Received legacy SPC-2 RESERVE/RELEASE" 167eacac00cSChristoph Hellwig " while active SPC-3 registrations exist," 168eacac00cSChristoph Hellwig " returning RESERVATION_CONFLICT\n"); 169087a03b3SNicholas Bellinger return -EBUSY; 170eacac00cSChristoph Hellwig } 171eacac00cSChristoph Hellwig 172087a03b3SNicholas Bellinger return 0; 173eacac00cSChristoph Hellwig } 174eacac00cSChristoph Hellwig 175de103c93SChristoph Hellwig sense_reason_t 176de103c93SChristoph Hellwig target_scsi2_reservation_release(struct se_cmd *cmd) 177c66ac9dbSNicholas Bellinger { 178c66ac9dbSNicholas Bellinger struct se_device *dev = cmd->se_dev; 179c66ac9dbSNicholas Bellinger struct se_session *sess = cmd->se_sess; 180609234e3SWei Yongjun struct se_portal_group *tpg; 181de103c93SChristoph Hellwig int rc; 182c66ac9dbSNicholas Bellinger 183609234e3SWei Yongjun if (!sess || !sess->se_tpg) 184d29a5b6aSChristoph Hellwig goto out; 185087a03b3SNicholas Bellinger rc = target_check_scsi2_reservation_conflict(cmd); 186087a03b3SNicholas Bellinger if (rc == 1) 187d29a5b6aSChristoph Hellwig goto out; 188de103c93SChristoph Hellwig if (rc < 0) 189de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 190c66ac9dbSNicholas Bellinger 191c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 192d29a5b6aSChristoph Hellwig if (!dev->dev_reserved_node_acl || !sess) 193d29a5b6aSChristoph Hellwig goto out_unlock; 194c66ac9dbSNicholas Bellinger 195d29a5b6aSChristoph Hellwig if (dev->dev_reserved_node_acl != sess->se_node_acl) 196d29a5b6aSChristoph Hellwig goto out_unlock; 197d29a5b6aSChristoph Hellwig 198edc318d9SBernhard Kohl if (dev->dev_res_bin_isid != sess->sess_bin_isid) 199edc318d9SBernhard Kohl goto out_unlock; 200edc318d9SBernhard Kohl 201c66ac9dbSNicholas Bellinger dev->dev_reserved_node_acl = NULL; 2020fd97ccfSChristoph Hellwig dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS; 2030fd97ccfSChristoph Hellwig if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) { 204c66ac9dbSNicholas Bellinger dev->dev_res_bin_isid = 0; 2050fd97ccfSChristoph Hellwig dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS_WITH_ISID; 206c66ac9dbSNicholas Bellinger } 207609234e3SWei Yongjun tpg = sess->se_tpg; 2086708bb27SAndy Grover pr_debug("SCSI-2 Released reservation for %s LUN: %u ->" 209e3d6f909SAndy Grover " MAPPED LUN: %u for %s\n", tpg->se_tpg_tfo->get_fabric_name(), 210e3d6f909SAndy Grover cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun, 211c66ac9dbSNicholas Bellinger sess->se_node_acl->initiatorname); 212c66ac9dbSNicholas Bellinger 213d29a5b6aSChristoph Hellwig out_unlock: 214d29a5b6aSChristoph Hellwig spin_unlock(&dev->dev_reservation_lock); 215d29a5b6aSChristoph Hellwig out: 2166bb35e00SChristoph Hellwig target_complete_cmd(cmd, GOOD); 217de103c93SChristoph Hellwig return 0; 218c66ac9dbSNicholas Bellinger } 219c66ac9dbSNicholas Bellinger 220de103c93SChristoph Hellwig sense_reason_t 221de103c93SChristoph Hellwig target_scsi2_reservation_reserve(struct se_cmd *cmd) 222c66ac9dbSNicholas Bellinger { 223c66ac9dbSNicholas Bellinger struct se_device *dev = cmd->se_dev; 224c66ac9dbSNicholas Bellinger struct se_session *sess = cmd->se_sess; 225609234e3SWei Yongjun struct se_portal_group *tpg; 226de103c93SChristoph Hellwig sense_reason_t ret = 0; 227de103c93SChristoph Hellwig int rc; 228c66ac9dbSNicholas Bellinger 229a1d8b49aSAndy Grover if ((cmd->t_task_cdb[1] & 0x01) && 230a1d8b49aSAndy Grover (cmd->t_task_cdb[1] & 0x02)) { 2316708bb27SAndy Grover pr_err("LongIO and Obselete Bits set, returning" 232c66ac9dbSNicholas Bellinger " ILLEGAL_REQUEST\n"); 233de103c93SChristoph Hellwig return TCM_UNSUPPORTED_SCSI_OPCODE; 234c66ac9dbSNicholas Bellinger } 235c66ac9dbSNicholas Bellinger /* 236c66ac9dbSNicholas Bellinger * This is currently the case for target_core_mod passthrough struct se_cmd 237c66ac9dbSNicholas Bellinger * ops 238c66ac9dbSNicholas Bellinger */ 239609234e3SWei Yongjun if (!sess || !sess->se_tpg) 240d29a5b6aSChristoph Hellwig goto out; 241087a03b3SNicholas Bellinger rc = target_check_scsi2_reservation_conflict(cmd); 242087a03b3SNicholas Bellinger if (rc == 1) 243d29a5b6aSChristoph Hellwig goto out; 244c66ac9dbSNicholas Bellinger 245de103c93SChristoph Hellwig if (rc < 0) 246de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 247de103c93SChristoph Hellwig 248609234e3SWei Yongjun tpg = sess->se_tpg; 249c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 250c66ac9dbSNicholas Bellinger if (dev->dev_reserved_node_acl && 251c66ac9dbSNicholas Bellinger (dev->dev_reserved_node_acl != sess->se_node_acl)) { 2526708bb27SAndy Grover pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n", 253e3d6f909SAndy Grover tpg->se_tpg_tfo->get_fabric_name()); 2546708bb27SAndy Grover pr_err("Original reserver LUN: %u %s\n", 255e3d6f909SAndy Grover cmd->se_lun->unpacked_lun, 256c66ac9dbSNicholas Bellinger dev->dev_reserved_node_acl->initiatorname); 2576708bb27SAndy Grover pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u" 258e3d6f909SAndy Grover " from %s \n", cmd->se_lun->unpacked_lun, 259c66ac9dbSNicholas Bellinger cmd->se_deve->mapped_lun, 260c66ac9dbSNicholas Bellinger sess->se_node_acl->initiatorname); 261de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 262d29a5b6aSChristoph Hellwig goto out_unlock; 263c66ac9dbSNicholas Bellinger } 264c66ac9dbSNicholas Bellinger 265c66ac9dbSNicholas Bellinger dev->dev_reserved_node_acl = sess->se_node_acl; 2660fd97ccfSChristoph Hellwig dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS; 267c66ac9dbSNicholas Bellinger if (sess->sess_bin_isid != 0) { 268c66ac9dbSNicholas Bellinger dev->dev_res_bin_isid = sess->sess_bin_isid; 2690fd97ccfSChristoph Hellwig dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS_WITH_ISID; 270c66ac9dbSNicholas Bellinger } 2716708bb27SAndy Grover pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u" 272e3d6f909SAndy Grover " for %s\n", tpg->se_tpg_tfo->get_fabric_name(), 273e3d6f909SAndy Grover cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun, 274c66ac9dbSNicholas Bellinger sess->se_node_acl->initiatorname); 275c66ac9dbSNicholas Bellinger 276d29a5b6aSChristoph Hellwig out_unlock: 277d29a5b6aSChristoph Hellwig spin_unlock(&dev->dev_reservation_lock); 278d29a5b6aSChristoph Hellwig out: 2796bb35e00SChristoph Hellwig if (!ret) 2806bb35e00SChristoph Hellwig target_complete_cmd(cmd, GOOD); 281d29a5b6aSChristoph Hellwig return ret; 282c66ac9dbSNicholas Bellinger } 283c66ac9dbSNicholas Bellinger 284c66ac9dbSNicholas Bellinger 285c66ac9dbSNicholas Bellinger /* 286c66ac9dbSNicholas Bellinger * Begin SPC-3/SPC-4 Persistent Reservations emulation support 287c66ac9dbSNicholas Bellinger * 288c66ac9dbSNicholas Bellinger * This function is called by those initiator ports who are *NOT* 289c66ac9dbSNicholas Bellinger * the active PR reservation holder when a reservation is present. 290c66ac9dbSNicholas Bellinger */ 291c66ac9dbSNicholas Bellinger static int core_scsi3_pr_seq_non_holder( 292c66ac9dbSNicholas Bellinger struct se_cmd *cmd, 293c66ac9dbSNicholas Bellinger u32 pr_reg_type) 294c66ac9dbSNicholas Bellinger { 295d977f437SChristoph Hellwig unsigned char *cdb = cmd->t_task_cdb; 296c66ac9dbSNicholas Bellinger struct se_dev_entry *se_deve; 297e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 298c66ac9dbSNicholas Bellinger int other_cdb = 0, ignore_reg; 299c66ac9dbSNicholas Bellinger int registered_nexus = 0, ret = 1; /* Conflict by default */ 300c66ac9dbSNicholas Bellinger int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */ 301c66ac9dbSNicholas Bellinger int we = 0; /* Write Exclusive */ 302c66ac9dbSNicholas Bellinger int legacy = 0; /* Act like a legacy device and return 303c66ac9dbSNicholas Bellinger * RESERVATION CONFLICT on some CDBs */ 304c66ac9dbSNicholas Bellinger 305f2083241SJörn Engel se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun]; 306c66ac9dbSNicholas Bellinger /* 307c66ac9dbSNicholas Bellinger * Determine if the registration should be ignored due to 308d977f437SChristoph Hellwig * non-matching ISIDs in target_scsi3_pr_reservation_check(). 309c66ac9dbSNicholas Bellinger */ 310c66ac9dbSNicholas Bellinger ignore_reg = (pr_reg_type & 0x80000000); 311c66ac9dbSNicholas Bellinger if (ignore_reg) 312c66ac9dbSNicholas Bellinger pr_reg_type &= ~0x80000000; 313c66ac9dbSNicholas Bellinger 314c66ac9dbSNicholas Bellinger switch (pr_reg_type) { 315c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE: 316c66ac9dbSNicholas Bellinger we = 1; 317c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS: 318c66ac9dbSNicholas Bellinger /* 319c66ac9dbSNicholas Bellinger * Some commands are only allowed for the persistent reservation 320c66ac9dbSNicholas Bellinger * holder. 321c66ac9dbSNicholas Bellinger */ 322c66ac9dbSNicholas Bellinger if ((se_deve->def_pr_registered) && !(ignore_reg)) 323c66ac9dbSNicholas Bellinger registered_nexus = 1; 324c66ac9dbSNicholas Bellinger break; 325c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_REGONLY: 326c66ac9dbSNicholas Bellinger we = 1; 327c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY: 328c66ac9dbSNicholas Bellinger /* 329c66ac9dbSNicholas Bellinger * Some commands are only allowed for registered I_T Nexuses. 330c66ac9dbSNicholas Bellinger */ 331c66ac9dbSNicholas Bellinger reg_only = 1; 332c66ac9dbSNicholas Bellinger if ((se_deve->def_pr_registered) && !(ignore_reg)) 333c66ac9dbSNicholas Bellinger registered_nexus = 1; 334c66ac9dbSNicholas Bellinger break; 335c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_ALLREG: 336c66ac9dbSNicholas Bellinger we = 1; 337c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG: 338c66ac9dbSNicholas Bellinger /* 339c66ac9dbSNicholas Bellinger * Each registered I_T Nexus is a reservation holder. 340c66ac9dbSNicholas Bellinger */ 341c66ac9dbSNicholas Bellinger all_reg = 1; 342c66ac9dbSNicholas Bellinger if ((se_deve->def_pr_registered) && !(ignore_reg)) 343c66ac9dbSNicholas Bellinger registered_nexus = 1; 344c66ac9dbSNicholas Bellinger break; 345c66ac9dbSNicholas Bellinger default: 346e3d6f909SAndy Grover return -EINVAL; 347c66ac9dbSNicholas Bellinger } 348c66ac9dbSNicholas Bellinger /* 349c66ac9dbSNicholas Bellinger * Referenced from spc4r17 table 45 for *NON* PR holder access 350c66ac9dbSNicholas Bellinger */ 351c66ac9dbSNicholas Bellinger switch (cdb[0]) { 352c66ac9dbSNicholas Bellinger case SECURITY_PROTOCOL_IN: 353c66ac9dbSNicholas Bellinger if (registered_nexus) 354c66ac9dbSNicholas Bellinger return 0; 355c66ac9dbSNicholas Bellinger ret = (we) ? 0 : 1; 356c66ac9dbSNicholas Bellinger break; 357c66ac9dbSNicholas Bellinger case MODE_SENSE: 358c66ac9dbSNicholas Bellinger case MODE_SENSE_10: 359c66ac9dbSNicholas Bellinger case READ_ATTRIBUTE: 360c66ac9dbSNicholas Bellinger case READ_BUFFER: 361c66ac9dbSNicholas Bellinger case RECEIVE_DIAGNOSTIC: 362c66ac9dbSNicholas Bellinger if (legacy) { 363c66ac9dbSNicholas Bellinger ret = 1; 364c66ac9dbSNicholas Bellinger break; 365c66ac9dbSNicholas Bellinger } 366c66ac9dbSNicholas Bellinger if (registered_nexus) { 367c66ac9dbSNicholas Bellinger ret = 0; 368c66ac9dbSNicholas Bellinger break; 369c66ac9dbSNicholas Bellinger } 370c66ac9dbSNicholas Bellinger ret = (we) ? 0 : 1; /* Allowed Write Exclusive */ 371c66ac9dbSNicholas Bellinger break; 372c66ac9dbSNicholas Bellinger case PERSISTENT_RESERVE_OUT: 373c66ac9dbSNicholas Bellinger /* 374c66ac9dbSNicholas Bellinger * This follows PERSISTENT_RESERVE_OUT service actions that 375c66ac9dbSNicholas Bellinger * are allowed in the presence of various reservations. 376c66ac9dbSNicholas Bellinger * See spc4r17, table 46 377c66ac9dbSNicholas Bellinger */ 378c66ac9dbSNicholas Bellinger switch (cdb[1] & 0x1f) { 379c66ac9dbSNicholas Bellinger case PRO_CLEAR: 380c66ac9dbSNicholas Bellinger case PRO_PREEMPT: 381c66ac9dbSNicholas Bellinger case PRO_PREEMPT_AND_ABORT: 382c66ac9dbSNicholas Bellinger ret = (registered_nexus) ? 0 : 1; 383c66ac9dbSNicholas Bellinger break; 384c66ac9dbSNicholas Bellinger case PRO_REGISTER: 385c66ac9dbSNicholas Bellinger case PRO_REGISTER_AND_IGNORE_EXISTING_KEY: 386c66ac9dbSNicholas Bellinger ret = 0; 387c66ac9dbSNicholas Bellinger break; 388c66ac9dbSNicholas Bellinger case PRO_REGISTER_AND_MOVE: 389c66ac9dbSNicholas Bellinger case PRO_RESERVE: 390c66ac9dbSNicholas Bellinger ret = 1; 391c66ac9dbSNicholas Bellinger break; 392c66ac9dbSNicholas Bellinger case PRO_RELEASE: 393c66ac9dbSNicholas Bellinger ret = (registered_nexus) ? 0 : 1; 394c66ac9dbSNicholas Bellinger break; 395c66ac9dbSNicholas Bellinger default: 3966708bb27SAndy Grover pr_err("Unknown PERSISTENT_RESERVE_OUT service" 397c66ac9dbSNicholas Bellinger " action: 0x%02x\n", cdb[1] & 0x1f); 398e3d6f909SAndy Grover return -EINVAL; 399c66ac9dbSNicholas Bellinger } 400c66ac9dbSNicholas Bellinger break; 401c66ac9dbSNicholas Bellinger case RELEASE: 402c66ac9dbSNicholas Bellinger case RELEASE_10: 403eacac00cSChristoph Hellwig /* Handled by CRH=1 in target_scsi2_reservation_release() */ 404c66ac9dbSNicholas Bellinger ret = 0; 405c66ac9dbSNicholas Bellinger break; 406c66ac9dbSNicholas Bellinger case RESERVE: 407c66ac9dbSNicholas Bellinger case RESERVE_10: 408eacac00cSChristoph Hellwig /* Handled by CRH=1 in target_scsi2_reservation_reserve() */ 409c66ac9dbSNicholas Bellinger ret = 0; 410c66ac9dbSNicholas Bellinger break; 411c66ac9dbSNicholas Bellinger case TEST_UNIT_READY: 412c66ac9dbSNicholas Bellinger ret = (legacy) ? 1 : 0; /* Conflict for legacy */ 413c66ac9dbSNicholas Bellinger break; 414c66ac9dbSNicholas Bellinger case MAINTENANCE_IN: 415c66ac9dbSNicholas Bellinger switch (cdb[1] & 0x1f) { 416c66ac9dbSNicholas Bellinger case MI_MANAGEMENT_PROTOCOL_IN: 417c66ac9dbSNicholas Bellinger if (registered_nexus) { 418c66ac9dbSNicholas Bellinger ret = 0; 419c66ac9dbSNicholas Bellinger break; 420c66ac9dbSNicholas Bellinger } 421c66ac9dbSNicholas Bellinger ret = (we) ? 0 : 1; /* Allowed Write Exclusive */ 422c66ac9dbSNicholas Bellinger break; 423c66ac9dbSNicholas Bellinger case MI_REPORT_SUPPORTED_OPERATION_CODES: 424c66ac9dbSNicholas Bellinger case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS: 425c66ac9dbSNicholas Bellinger if (legacy) { 426c66ac9dbSNicholas Bellinger ret = 1; 427c66ac9dbSNicholas Bellinger break; 428c66ac9dbSNicholas Bellinger } 429c66ac9dbSNicholas Bellinger if (registered_nexus) { 430c66ac9dbSNicholas Bellinger ret = 0; 431c66ac9dbSNicholas Bellinger break; 432c66ac9dbSNicholas Bellinger } 433c66ac9dbSNicholas Bellinger ret = (we) ? 0 : 1; /* Allowed Write Exclusive */ 434c66ac9dbSNicholas Bellinger break; 435c66ac9dbSNicholas Bellinger case MI_REPORT_ALIASES: 436c66ac9dbSNicholas Bellinger case MI_REPORT_IDENTIFYING_INFORMATION: 437c66ac9dbSNicholas Bellinger case MI_REPORT_PRIORITY: 438c66ac9dbSNicholas Bellinger case MI_REPORT_TARGET_PGS: 439c66ac9dbSNicholas Bellinger case MI_REPORT_TIMESTAMP: 440c66ac9dbSNicholas Bellinger ret = 0; /* Allowed */ 441c66ac9dbSNicholas Bellinger break; 442c66ac9dbSNicholas Bellinger default: 4436708bb27SAndy Grover pr_err("Unknown MI Service Action: 0x%02x\n", 444c66ac9dbSNicholas Bellinger (cdb[1] & 0x1f)); 445e3d6f909SAndy Grover return -EINVAL; 446c66ac9dbSNicholas Bellinger } 447c66ac9dbSNicholas Bellinger break; 448c66ac9dbSNicholas Bellinger case ACCESS_CONTROL_IN: 449c66ac9dbSNicholas Bellinger case ACCESS_CONTROL_OUT: 450c66ac9dbSNicholas Bellinger case INQUIRY: 451c66ac9dbSNicholas Bellinger case LOG_SENSE: 452c66ac9dbSNicholas Bellinger case READ_MEDIA_SERIAL_NUMBER: 453c66ac9dbSNicholas Bellinger case REPORT_LUNS: 454c66ac9dbSNicholas Bellinger case REQUEST_SENSE: 4556816966aSMarco Sanvido case PERSISTENT_RESERVE_IN: 456c66ac9dbSNicholas Bellinger ret = 0; /*/ Allowed CDBs */ 457c66ac9dbSNicholas Bellinger break; 458c66ac9dbSNicholas Bellinger default: 459c66ac9dbSNicholas Bellinger other_cdb = 1; 460c66ac9dbSNicholas Bellinger break; 461c66ac9dbSNicholas Bellinger } 462c66ac9dbSNicholas Bellinger /* 46325985edcSLucas De Marchi * Case where the CDB is explicitly allowed in the above switch 464c66ac9dbSNicholas Bellinger * statement. 465c66ac9dbSNicholas Bellinger */ 4666708bb27SAndy Grover if (!ret && !other_cdb) { 4676708bb27SAndy Grover pr_debug("Allowing explict CDB: 0x%02x for %s" 468c66ac9dbSNicholas Bellinger " reservation holder\n", cdb[0], 469c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(pr_reg_type)); 4708b1e1244SAndy Grover 471c66ac9dbSNicholas Bellinger return ret; 472c66ac9dbSNicholas Bellinger } 473c66ac9dbSNicholas Bellinger /* 474c66ac9dbSNicholas Bellinger * Check if write exclusive initiator ports *NOT* holding the 475c66ac9dbSNicholas Bellinger * WRITE_EXCLUSIVE_* reservation. 476c66ac9dbSNicholas Bellinger */ 477ee1b1b9cSAndy Grover if (we && !registered_nexus) { 478c66ac9dbSNicholas Bellinger if (cmd->data_direction == DMA_TO_DEVICE) { 479c66ac9dbSNicholas Bellinger /* 480c66ac9dbSNicholas Bellinger * Conflict for write exclusive 481c66ac9dbSNicholas Bellinger */ 4826708bb27SAndy Grover pr_debug("%s Conflict for unregistered nexus" 483c66ac9dbSNicholas Bellinger " %s CDB: 0x%02x to %s reservation\n", 484c66ac9dbSNicholas Bellinger transport_dump_cmd_direction(cmd), 485c66ac9dbSNicholas Bellinger se_sess->se_node_acl->initiatorname, cdb[0], 486c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(pr_reg_type)); 487c66ac9dbSNicholas Bellinger return 1; 488c66ac9dbSNicholas Bellinger } else { 489c66ac9dbSNicholas Bellinger /* 490c66ac9dbSNicholas Bellinger * Allow non WRITE CDBs for all Write Exclusive 491c66ac9dbSNicholas Bellinger * PR TYPEs to pass for registered and 492c66ac9dbSNicholas Bellinger * non-registered_nexuxes NOT holding the reservation. 493c66ac9dbSNicholas Bellinger * 494c66ac9dbSNicholas Bellinger * We only make noise for the unregisterd nexuses, 495c66ac9dbSNicholas Bellinger * as we expect registered non-reservation holding 496c66ac9dbSNicholas Bellinger * nexuses to issue CDBs. 497c66ac9dbSNicholas Bellinger */ 4988b1e1244SAndy Grover 4996708bb27SAndy Grover if (!registered_nexus) { 5006708bb27SAndy Grover pr_debug("Allowing implict CDB: 0x%02x" 501c66ac9dbSNicholas Bellinger " for %s reservation on unregistered" 502c66ac9dbSNicholas Bellinger " nexus\n", cdb[0], 503c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(pr_reg_type)); 504c66ac9dbSNicholas Bellinger } 5058b1e1244SAndy Grover 506c66ac9dbSNicholas Bellinger return 0; 507c66ac9dbSNicholas Bellinger } 508c66ac9dbSNicholas Bellinger } else if ((reg_only) || (all_reg)) { 509c66ac9dbSNicholas Bellinger if (registered_nexus) { 510c66ac9dbSNicholas Bellinger /* 511c66ac9dbSNicholas Bellinger * For PR_*_REG_ONLY and PR_*_ALL_REG reservations, 512c66ac9dbSNicholas Bellinger * allow commands from registered nexuses. 513c66ac9dbSNicholas Bellinger */ 5148b1e1244SAndy Grover 5156708bb27SAndy Grover pr_debug("Allowing implict CDB: 0x%02x for %s" 516c66ac9dbSNicholas Bellinger " reservation\n", cdb[0], 517c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(pr_reg_type)); 5188b1e1244SAndy Grover 519c66ac9dbSNicholas Bellinger return 0; 520c66ac9dbSNicholas Bellinger } 521c66ac9dbSNicholas Bellinger } 5226708bb27SAndy Grover pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x" 523c66ac9dbSNicholas Bellinger " for %s reservation\n", transport_dump_cmd_direction(cmd), 524c66ac9dbSNicholas Bellinger (registered_nexus) ? "" : "un", 525c66ac9dbSNicholas Bellinger se_sess->se_node_acl->initiatorname, cdb[0], 526c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(pr_reg_type)); 527c66ac9dbSNicholas Bellinger 528c66ac9dbSNicholas Bellinger return 1; /* Conflict by default */ 529c66ac9dbSNicholas Bellinger } 530c66ac9dbSNicholas Bellinger 531de103c93SChristoph Hellwig static sense_reason_t 532de103c93SChristoph Hellwig target_scsi3_pr_reservation_check(struct se_cmd *cmd) 533d977f437SChristoph Hellwig { 534d977f437SChristoph Hellwig struct se_device *dev = cmd->se_dev; 535d977f437SChristoph Hellwig struct se_session *sess = cmd->se_sess; 536d977f437SChristoph Hellwig u32 pr_reg_type; 537d977f437SChristoph Hellwig 538d977f437SChristoph Hellwig if (!dev->dev_pr_res_holder) 539d977f437SChristoph Hellwig return 0; 540d977f437SChristoph Hellwig 541d977f437SChristoph Hellwig pr_reg_type = dev->dev_pr_res_holder->pr_res_type; 542d977f437SChristoph Hellwig cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key; 543d977f437SChristoph Hellwig if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl) 544d977f437SChristoph Hellwig goto check_nonholder; 545d977f437SChristoph Hellwig 546d977f437SChristoph Hellwig if (dev->dev_pr_res_holder->isid_present_at_reg) { 547d977f437SChristoph Hellwig if (dev->dev_pr_res_holder->pr_reg_bin_isid != 548d977f437SChristoph Hellwig sess->sess_bin_isid) { 549d977f437SChristoph Hellwig pr_reg_type |= 0x80000000; 550d977f437SChristoph Hellwig goto check_nonholder; 551d977f437SChristoph Hellwig } 552d977f437SChristoph Hellwig } 553d977f437SChristoph Hellwig 554d977f437SChristoph Hellwig return 0; 555d977f437SChristoph Hellwig 556d977f437SChristoph Hellwig check_nonholder: 557d977f437SChristoph Hellwig if (core_scsi3_pr_seq_non_holder(cmd, pr_reg_type)) 558de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 559d977f437SChristoph Hellwig return 0; 560d977f437SChristoph Hellwig } 561d977f437SChristoph Hellwig 562c66ac9dbSNicholas Bellinger static u32 core_scsi3_pr_generation(struct se_device *dev) 563c66ac9dbSNicholas Bellinger { 564c66ac9dbSNicholas Bellinger u32 prg; 5650fd97ccfSChristoph Hellwig 566c66ac9dbSNicholas Bellinger /* 567c66ac9dbSNicholas Bellinger * PRGeneration field shall contain the value of a 32-bit wrapping 568c66ac9dbSNicholas Bellinger * counter mainted by the device server. 569c66ac9dbSNicholas Bellinger * 570c66ac9dbSNicholas Bellinger * Note that this is done regardless of Active Persist across 571c66ac9dbSNicholas Bellinger * Target PowerLoss (APTPL) 572c66ac9dbSNicholas Bellinger * 573c66ac9dbSNicholas Bellinger * See spc4r17 section 6.3.12 READ_KEYS service action 574c66ac9dbSNicholas Bellinger */ 575c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 5760fd97ccfSChristoph Hellwig prg = dev->t10_pr.pr_generation++; 577c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 578c66ac9dbSNicholas Bellinger 579c66ac9dbSNicholas Bellinger return prg; 580c66ac9dbSNicholas Bellinger } 581c66ac9dbSNicholas Bellinger 582c66ac9dbSNicholas Bellinger static struct t10_pr_registration *__core_scsi3_do_alloc_registration( 583c66ac9dbSNicholas Bellinger struct se_device *dev, 584c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 585c66ac9dbSNicholas Bellinger struct se_dev_entry *deve, 586c66ac9dbSNicholas Bellinger unsigned char *isid, 587c66ac9dbSNicholas Bellinger u64 sa_res_key, 588c66ac9dbSNicholas Bellinger int all_tg_pt, 589c66ac9dbSNicholas Bellinger int aptpl) 590c66ac9dbSNicholas Bellinger { 591c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg; 592c66ac9dbSNicholas Bellinger 593c66ac9dbSNicholas Bellinger pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC); 5946708bb27SAndy Grover if (!pr_reg) { 5956708bb27SAndy Grover pr_err("Unable to allocate struct t10_pr_registration\n"); 596c66ac9dbSNicholas Bellinger return NULL; 597c66ac9dbSNicholas Bellinger } 598c66ac9dbSNicholas Bellinger 5990fd97ccfSChristoph Hellwig pr_reg->pr_aptpl_buf = kzalloc(dev->t10_pr.pr_aptpl_buf_len, 600c66ac9dbSNicholas Bellinger GFP_ATOMIC); 6016708bb27SAndy Grover if (!pr_reg->pr_aptpl_buf) { 6026708bb27SAndy Grover pr_err("Unable to allocate pr_reg->pr_aptpl_buf\n"); 603c66ac9dbSNicholas Bellinger kmem_cache_free(t10_pr_reg_cache, pr_reg); 604c66ac9dbSNicholas Bellinger return NULL; 605c66ac9dbSNicholas Bellinger } 606c66ac9dbSNicholas Bellinger 607c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_list); 608c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list); 609c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list); 610c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list); 611c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list); 612c66ac9dbSNicholas Bellinger atomic_set(&pr_reg->pr_res_holders, 0); 613c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl = nacl; 614c66ac9dbSNicholas Bellinger pr_reg->pr_reg_deve = deve; 615c66ac9dbSNicholas Bellinger pr_reg->pr_res_mapped_lun = deve->mapped_lun; 616c66ac9dbSNicholas Bellinger pr_reg->pr_aptpl_target_lun = deve->se_lun->unpacked_lun; 617c66ac9dbSNicholas Bellinger pr_reg->pr_res_key = sa_res_key; 618c66ac9dbSNicholas Bellinger pr_reg->pr_reg_all_tg_pt = all_tg_pt; 619c66ac9dbSNicholas Bellinger pr_reg->pr_reg_aptpl = aptpl; 620c66ac9dbSNicholas Bellinger pr_reg->pr_reg_tg_pt_lun = deve->se_lun; 621c66ac9dbSNicholas Bellinger /* 622c66ac9dbSNicholas Bellinger * If an ISID value for this SCSI Initiator Port exists, 623c66ac9dbSNicholas Bellinger * save it to the registration now. 624c66ac9dbSNicholas Bellinger */ 625c66ac9dbSNicholas Bellinger if (isid != NULL) { 626c66ac9dbSNicholas Bellinger pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid); 627c66ac9dbSNicholas Bellinger snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid); 628c66ac9dbSNicholas Bellinger pr_reg->isid_present_at_reg = 1; 629c66ac9dbSNicholas Bellinger } 630c66ac9dbSNicholas Bellinger 631c66ac9dbSNicholas Bellinger return pr_reg; 632c66ac9dbSNicholas Bellinger } 633c66ac9dbSNicholas Bellinger 634c66ac9dbSNicholas Bellinger static int core_scsi3_lunacl_depend_item(struct se_dev_entry *); 635c66ac9dbSNicholas Bellinger static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *); 636c66ac9dbSNicholas Bellinger 637c66ac9dbSNicholas Bellinger /* 638c66ac9dbSNicholas Bellinger * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0 639c66ac9dbSNicholas Bellinger * modes. 640c66ac9dbSNicholas Bellinger */ 641c66ac9dbSNicholas Bellinger static struct t10_pr_registration *__core_scsi3_alloc_registration( 642c66ac9dbSNicholas Bellinger struct se_device *dev, 643c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 644c66ac9dbSNicholas Bellinger struct se_dev_entry *deve, 645c66ac9dbSNicholas Bellinger unsigned char *isid, 646c66ac9dbSNicholas Bellinger u64 sa_res_key, 647c66ac9dbSNicholas Bellinger int all_tg_pt, 648c66ac9dbSNicholas Bellinger int aptpl) 649c66ac9dbSNicholas Bellinger { 650c66ac9dbSNicholas Bellinger struct se_dev_entry *deve_tmp; 651c66ac9dbSNicholas Bellinger struct se_node_acl *nacl_tmp; 652c66ac9dbSNicholas Bellinger struct se_port *port, *port_tmp; 653c66ac9dbSNicholas Bellinger struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo; 654c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe; 655c66ac9dbSNicholas Bellinger int ret; 656c66ac9dbSNicholas Bellinger /* 657c66ac9dbSNicholas Bellinger * Create a registration for the I_T Nexus upon which the 658c66ac9dbSNicholas Bellinger * PROUT REGISTER was received. 659c66ac9dbSNicholas Bellinger */ 660c66ac9dbSNicholas Bellinger pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, deve, isid, 661c66ac9dbSNicholas Bellinger sa_res_key, all_tg_pt, aptpl); 6626708bb27SAndy Grover if (!pr_reg) 663c66ac9dbSNicholas Bellinger return NULL; 664c66ac9dbSNicholas Bellinger /* 665c66ac9dbSNicholas Bellinger * Return pointer to pr_reg for ALL_TG_PT=0 666c66ac9dbSNicholas Bellinger */ 6676708bb27SAndy Grover if (!all_tg_pt) 668c66ac9dbSNicholas Bellinger return pr_reg; 669c66ac9dbSNicholas Bellinger /* 670c66ac9dbSNicholas Bellinger * Create list of matching SCSI Initiator Port registrations 671c66ac9dbSNicholas Bellinger * for ALL_TG_PT=1 672c66ac9dbSNicholas Bellinger */ 673c66ac9dbSNicholas Bellinger spin_lock(&dev->se_port_lock); 674c66ac9dbSNicholas Bellinger list_for_each_entry_safe(port, port_tmp, &dev->dev_sep_list, sep_list) { 675c66ac9dbSNicholas Bellinger atomic_inc(&port->sep_tg_pt_ref_cnt); 676c66ac9dbSNicholas Bellinger smp_mb__after_atomic_inc(); 677c66ac9dbSNicholas Bellinger spin_unlock(&dev->se_port_lock); 678c66ac9dbSNicholas Bellinger 679c66ac9dbSNicholas Bellinger spin_lock_bh(&port->sep_alua_lock); 680c66ac9dbSNicholas Bellinger list_for_each_entry(deve_tmp, &port->sep_alua_list, 681c66ac9dbSNicholas Bellinger alua_port_list) { 682c66ac9dbSNicholas Bellinger /* 683c66ac9dbSNicholas Bellinger * This pointer will be NULL for demo mode MappedLUNs 684c66ac9dbSNicholas Bellinger * that have not been make explict via a ConfigFS 685c66ac9dbSNicholas Bellinger * MappedLUN group for the SCSI Initiator Node ACL. 686c66ac9dbSNicholas Bellinger */ 6876708bb27SAndy Grover if (!deve_tmp->se_lun_acl) 688c66ac9dbSNicholas Bellinger continue; 689c66ac9dbSNicholas Bellinger 690c66ac9dbSNicholas Bellinger nacl_tmp = deve_tmp->se_lun_acl->se_lun_nacl; 691c66ac9dbSNicholas Bellinger /* 692c66ac9dbSNicholas Bellinger * Skip the matching struct se_node_acl that is allocated 693c66ac9dbSNicholas Bellinger * above.. 694c66ac9dbSNicholas Bellinger */ 695c66ac9dbSNicholas Bellinger if (nacl == nacl_tmp) 696c66ac9dbSNicholas Bellinger continue; 697c66ac9dbSNicholas Bellinger /* 698c66ac9dbSNicholas Bellinger * Only perform PR registrations for target ports on 699c66ac9dbSNicholas Bellinger * the same fabric module as the REGISTER w/ ALL_TG_PT=1 700c66ac9dbSNicholas Bellinger * arrived. 701c66ac9dbSNicholas Bellinger */ 702c66ac9dbSNicholas Bellinger if (tfo != nacl_tmp->se_tpg->se_tpg_tfo) 703c66ac9dbSNicholas Bellinger continue; 704c66ac9dbSNicholas Bellinger /* 705c66ac9dbSNicholas Bellinger * Look for a matching Initiator Node ACL in ASCII format 706c66ac9dbSNicholas Bellinger */ 707c66ac9dbSNicholas Bellinger if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname)) 708c66ac9dbSNicholas Bellinger continue; 709c66ac9dbSNicholas Bellinger 710c66ac9dbSNicholas Bellinger atomic_inc(&deve_tmp->pr_ref_count); 711c66ac9dbSNicholas Bellinger smp_mb__after_atomic_inc(); 712c66ac9dbSNicholas Bellinger spin_unlock_bh(&port->sep_alua_lock); 713c66ac9dbSNicholas Bellinger /* 714c66ac9dbSNicholas Bellinger * Grab a configfs group dependency that is released 715c66ac9dbSNicholas Bellinger * for the exception path at label out: below, or upon 716c66ac9dbSNicholas Bellinger * completion of adding ALL_TG_PT=1 registrations in 717c66ac9dbSNicholas Bellinger * __core_scsi3_add_registration() 718c66ac9dbSNicholas Bellinger */ 719c66ac9dbSNicholas Bellinger ret = core_scsi3_lunacl_depend_item(deve_tmp); 720c66ac9dbSNicholas Bellinger if (ret < 0) { 7216708bb27SAndy Grover pr_err("core_scsi3_lunacl_depend" 722c66ac9dbSNicholas Bellinger "_item() failed\n"); 723c66ac9dbSNicholas Bellinger atomic_dec(&port->sep_tg_pt_ref_cnt); 724c66ac9dbSNicholas Bellinger smp_mb__after_atomic_dec(); 725c66ac9dbSNicholas Bellinger atomic_dec(&deve_tmp->pr_ref_count); 726c66ac9dbSNicholas Bellinger smp_mb__after_atomic_dec(); 727c66ac9dbSNicholas Bellinger goto out; 728c66ac9dbSNicholas Bellinger } 729c66ac9dbSNicholas Bellinger /* 730c66ac9dbSNicholas Bellinger * Located a matching SCSI Initiator Port on a different 731c66ac9dbSNicholas Bellinger * port, allocate the pr_reg_atp and attach it to the 732c66ac9dbSNicholas Bellinger * pr_reg->pr_reg_atp_list that will be processed once 733c66ac9dbSNicholas Bellinger * the original *pr_reg is processed in 734c66ac9dbSNicholas Bellinger * __core_scsi3_add_registration() 735c66ac9dbSNicholas Bellinger */ 736c66ac9dbSNicholas Bellinger pr_reg_atp = __core_scsi3_do_alloc_registration(dev, 737c66ac9dbSNicholas Bellinger nacl_tmp, deve_tmp, NULL, 738c66ac9dbSNicholas Bellinger sa_res_key, all_tg_pt, aptpl); 7396708bb27SAndy Grover if (!pr_reg_atp) { 740c66ac9dbSNicholas Bellinger atomic_dec(&port->sep_tg_pt_ref_cnt); 741c66ac9dbSNicholas Bellinger smp_mb__after_atomic_dec(); 742c66ac9dbSNicholas Bellinger atomic_dec(&deve_tmp->pr_ref_count); 743c66ac9dbSNicholas Bellinger smp_mb__after_atomic_dec(); 744c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(deve_tmp); 745c66ac9dbSNicholas Bellinger goto out; 746c66ac9dbSNicholas Bellinger } 747c66ac9dbSNicholas Bellinger 748c66ac9dbSNicholas Bellinger list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list, 749c66ac9dbSNicholas Bellinger &pr_reg->pr_reg_atp_list); 750c66ac9dbSNicholas Bellinger spin_lock_bh(&port->sep_alua_lock); 751c66ac9dbSNicholas Bellinger } 752c66ac9dbSNicholas Bellinger spin_unlock_bh(&port->sep_alua_lock); 753c66ac9dbSNicholas Bellinger 754c66ac9dbSNicholas Bellinger spin_lock(&dev->se_port_lock); 755c66ac9dbSNicholas Bellinger atomic_dec(&port->sep_tg_pt_ref_cnt); 756c66ac9dbSNicholas Bellinger smp_mb__after_atomic_dec(); 757c66ac9dbSNicholas Bellinger } 758c66ac9dbSNicholas Bellinger spin_unlock(&dev->se_port_lock); 759c66ac9dbSNicholas Bellinger 760c66ac9dbSNicholas Bellinger return pr_reg; 761c66ac9dbSNicholas Bellinger out: 762c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe, 763c66ac9dbSNicholas Bellinger &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) { 764c66ac9dbSNicholas Bellinger list_del(&pr_reg_tmp->pr_reg_atp_mem_list); 765c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve); 766c66ac9dbSNicholas Bellinger kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp); 767c66ac9dbSNicholas Bellinger } 768c66ac9dbSNicholas Bellinger kmem_cache_free(t10_pr_reg_cache, pr_reg); 769c66ac9dbSNicholas Bellinger return NULL; 770c66ac9dbSNicholas Bellinger } 771c66ac9dbSNicholas Bellinger 772c66ac9dbSNicholas Bellinger int core_scsi3_alloc_aptpl_registration( 773e3d6f909SAndy Grover struct t10_reservation *pr_tmpl, 774c66ac9dbSNicholas Bellinger u64 sa_res_key, 775c66ac9dbSNicholas Bellinger unsigned char *i_port, 776c66ac9dbSNicholas Bellinger unsigned char *isid, 777c66ac9dbSNicholas Bellinger u32 mapped_lun, 778c66ac9dbSNicholas Bellinger unsigned char *t_port, 779c66ac9dbSNicholas Bellinger u16 tpgt, 780c66ac9dbSNicholas Bellinger u32 target_lun, 781c66ac9dbSNicholas Bellinger int res_holder, 782c66ac9dbSNicholas Bellinger int all_tg_pt, 783c66ac9dbSNicholas Bellinger u8 type) 784c66ac9dbSNicholas Bellinger { 785c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg; 786c66ac9dbSNicholas Bellinger 7876708bb27SAndy Grover if (!i_port || !t_port || !sa_res_key) { 7886708bb27SAndy Grover pr_err("Illegal parameters for APTPL registration\n"); 789e3d6f909SAndy Grover return -EINVAL; 790c66ac9dbSNicholas Bellinger } 791c66ac9dbSNicholas Bellinger 792c66ac9dbSNicholas Bellinger pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL); 7936708bb27SAndy Grover if (!pr_reg) { 7946708bb27SAndy Grover pr_err("Unable to allocate struct t10_pr_registration\n"); 795e3d6f909SAndy Grover return -ENOMEM; 796c66ac9dbSNicholas Bellinger } 797c66ac9dbSNicholas Bellinger pr_reg->pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len, GFP_KERNEL); 798c66ac9dbSNicholas Bellinger 799c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_list); 800c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list); 801c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list); 802c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list); 803c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list); 804c66ac9dbSNicholas Bellinger atomic_set(&pr_reg->pr_res_holders, 0); 805c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl = NULL; 806c66ac9dbSNicholas Bellinger pr_reg->pr_reg_deve = NULL; 807c66ac9dbSNicholas Bellinger pr_reg->pr_res_mapped_lun = mapped_lun; 808c66ac9dbSNicholas Bellinger pr_reg->pr_aptpl_target_lun = target_lun; 809c66ac9dbSNicholas Bellinger pr_reg->pr_res_key = sa_res_key; 810c66ac9dbSNicholas Bellinger pr_reg->pr_reg_all_tg_pt = all_tg_pt; 811c66ac9dbSNicholas Bellinger pr_reg->pr_reg_aptpl = 1; 812c66ac9dbSNicholas Bellinger pr_reg->pr_reg_tg_pt_lun = NULL; 813c66ac9dbSNicholas Bellinger pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */ 814c66ac9dbSNicholas Bellinger pr_reg->pr_res_type = type; 815c66ac9dbSNicholas Bellinger /* 816c66ac9dbSNicholas Bellinger * If an ISID value had been saved in APTPL metadata for this 817c66ac9dbSNicholas Bellinger * SCSI Initiator Port, restore it now. 818c66ac9dbSNicholas Bellinger */ 819c66ac9dbSNicholas Bellinger if (isid != NULL) { 820c66ac9dbSNicholas Bellinger pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid); 821c66ac9dbSNicholas Bellinger snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid); 822c66ac9dbSNicholas Bellinger pr_reg->isid_present_at_reg = 1; 823c66ac9dbSNicholas Bellinger } 824c66ac9dbSNicholas Bellinger /* 825c66ac9dbSNicholas Bellinger * Copy the i_port and t_port information from caller. 826c66ac9dbSNicholas Bellinger */ 827c66ac9dbSNicholas Bellinger snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port); 828c66ac9dbSNicholas Bellinger snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port); 829c66ac9dbSNicholas Bellinger pr_reg->pr_reg_tpgt = tpgt; 830c66ac9dbSNicholas Bellinger /* 831c66ac9dbSNicholas Bellinger * Set pr_res_holder from caller, the pr_reg who is the reservation 832c66ac9dbSNicholas Bellinger * holder will get it's pointer set in core_scsi3_aptpl_reserve() once 833c66ac9dbSNicholas Bellinger * the Initiator Node LUN ACL from the fabric module is created for 834c66ac9dbSNicholas Bellinger * this registration. 835c66ac9dbSNicholas Bellinger */ 836c66ac9dbSNicholas Bellinger pr_reg->pr_res_holder = res_holder; 837c66ac9dbSNicholas Bellinger 838c66ac9dbSNicholas Bellinger list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list); 8396708bb27SAndy Grover pr_debug("SPC-3 PR APTPL Successfully added registration%s from" 840c66ac9dbSNicholas Bellinger " metadata\n", (res_holder) ? "+reservation" : ""); 841c66ac9dbSNicholas Bellinger return 0; 842c66ac9dbSNicholas Bellinger } 843c66ac9dbSNicholas Bellinger 844c66ac9dbSNicholas Bellinger static void core_scsi3_aptpl_reserve( 845c66ac9dbSNicholas Bellinger struct se_device *dev, 846c66ac9dbSNicholas Bellinger struct se_portal_group *tpg, 847c66ac9dbSNicholas Bellinger struct se_node_acl *node_acl, 848c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg) 849c66ac9dbSNicholas Bellinger { 850c66ac9dbSNicholas Bellinger char i_buf[PR_REG_ISID_ID_LEN]; 851c66ac9dbSNicholas Bellinger int prf_isid; 852c66ac9dbSNicholas Bellinger 853c66ac9dbSNicholas Bellinger memset(i_buf, 0, PR_REG_ISID_ID_LEN); 854c66ac9dbSNicholas Bellinger prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0], 855c66ac9dbSNicholas Bellinger PR_REG_ISID_ID_LEN); 856c66ac9dbSNicholas Bellinger 857c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 858c66ac9dbSNicholas Bellinger dev->dev_pr_res_holder = pr_reg; 859c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 860c66ac9dbSNicholas Bellinger 8616708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created" 862c66ac9dbSNicholas Bellinger " new reservation holder TYPE: %s ALL_TG_PT: %d\n", 863e3d6f909SAndy Grover tpg->se_tpg_tfo->get_fabric_name(), 864c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(pr_reg->pr_res_type), 865c66ac9dbSNicholas Bellinger (pr_reg->pr_reg_all_tg_pt) ? 1 : 0); 8666708bb27SAndy Grover pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n", 867e3d6f909SAndy Grover tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname, 868c66ac9dbSNicholas Bellinger (prf_isid) ? &i_buf[0] : ""); 869c66ac9dbSNicholas Bellinger } 870c66ac9dbSNicholas Bellinger 871c66ac9dbSNicholas Bellinger static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *, 872c66ac9dbSNicholas Bellinger struct t10_pr_registration *, int, int); 873c66ac9dbSNicholas Bellinger 874c66ac9dbSNicholas Bellinger static int __core_scsi3_check_aptpl_registration( 875c66ac9dbSNicholas Bellinger struct se_device *dev, 876c66ac9dbSNicholas Bellinger struct se_portal_group *tpg, 877c66ac9dbSNicholas Bellinger struct se_lun *lun, 878c66ac9dbSNicholas Bellinger u32 target_lun, 879c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 880c66ac9dbSNicholas Bellinger struct se_dev_entry *deve) 881c66ac9dbSNicholas Bellinger { 882c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp; 8830fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 884c66ac9dbSNicholas Bellinger unsigned char i_port[PR_APTPL_MAX_IPORT_LEN]; 885c66ac9dbSNicholas Bellinger unsigned char t_port[PR_APTPL_MAX_TPORT_LEN]; 886c66ac9dbSNicholas Bellinger u16 tpgt; 887c66ac9dbSNicholas Bellinger 888c66ac9dbSNicholas Bellinger memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN); 889c66ac9dbSNicholas Bellinger memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN); 890c66ac9dbSNicholas Bellinger /* 891c66ac9dbSNicholas Bellinger * Copy Initiator Port information from struct se_node_acl 892c66ac9dbSNicholas Bellinger */ 893c66ac9dbSNicholas Bellinger snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname); 894c66ac9dbSNicholas Bellinger snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s", 895e3d6f909SAndy Grover tpg->se_tpg_tfo->tpg_get_wwn(tpg)); 896e3d6f909SAndy Grover tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg); 897c66ac9dbSNicholas Bellinger /* 898c66ac9dbSNicholas Bellinger * Look for the matching registrations+reservation from those 899c66ac9dbSNicholas Bellinger * created from APTPL metadata. Note that multiple registrations 900c66ac9dbSNicholas Bellinger * may exist for fabrics that use ISIDs in their SCSI Initiator Port 901c66ac9dbSNicholas Bellinger * TransportIDs. 902c66ac9dbSNicholas Bellinger */ 903c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->aptpl_reg_lock); 904c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list, 905c66ac9dbSNicholas Bellinger pr_reg_aptpl_list) { 9066708bb27SAndy Grover if (!strcmp(pr_reg->pr_iport, i_port) && 907c66ac9dbSNicholas Bellinger (pr_reg->pr_res_mapped_lun == deve->mapped_lun) && 908c66ac9dbSNicholas Bellinger !(strcmp(pr_reg->pr_tport, t_port)) && 909c66ac9dbSNicholas Bellinger (pr_reg->pr_reg_tpgt == tpgt) && 910c66ac9dbSNicholas Bellinger (pr_reg->pr_aptpl_target_lun == target_lun)) { 911c66ac9dbSNicholas Bellinger 912c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl = nacl; 913c66ac9dbSNicholas Bellinger pr_reg->pr_reg_deve = deve; 914c66ac9dbSNicholas Bellinger pr_reg->pr_reg_tg_pt_lun = lun; 915c66ac9dbSNicholas Bellinger 916c66ac9dbSNicholas Bellinger list_del(&pr_reg->pr_reg_aptpl_list); 917c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->aptpl_reg_lock); 918c66ac9dbSNicholas Bellinger /* 919c66ac9dbSNicholas Bellinger * At this point all of the pointers in *pr_reg will 920c66ac9dbSNicholas Bellinger * be setup, so go ahead and add the registration. 921c66ac9dbSNicholas Bellinger */ 922c66ac9dbSNicholas Bellinger 923c66ac9dbSNicholas Bellinger __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0); 924c66ac9dbSNicholas Bellinger /* 925c66ac9dbSNicholas Bellinger * If this registration is the reservation holder, 926c66ac9dbSNicholas Bellinger * make that happen now.. 927c66ac9dbSNicholas Bellinger */ 928c66ac9dbSNicholas Bellinger if (pr_reg->pr_res_holder) 929c66ac9dbSNicholas Bellinger core_scsi3_aptpl_reserve(dev, tpg, 930c66ac9dbSNicholas Bellinger nacl, pr_reg); 931c66ac9dbSNicholas Bellinger /* 932c66ac9dbSNicholas Bellinger * Reenable pr_aptpl_active to accept new metadata 933c66ac9dbSNicholas Bellinger * updates once the SCSI device is active again.. 934c66ac9dbSNicholas Bellinger */ 935c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->aptpl_reg_lock); 936c66ac9dbSNicholas Bellinger pr_tmpl->pr_aptpl_active = 1; 937c66ac9dbSNicholas Bellinger } 938c66ac9dbSNicholas Bellinger } 939c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->aptpl_reg_lock); 940c66ac9dbSNicholas Bellinger 941c66ac9dbSNicholas Bellinger return 0; 942c66ac9dbSNicholas Bellinger } 943c66ac9dbSNicholas Bellinger 944c66ac9dbSNicholas Bellinger int core_scsi3_check_aptpl_registration( 945c66ac9dbSNicholas Bellinger struct se_device *dev, 946c66ac9dbSNicholas Bellinger struct se_portal_group *tpg, 947c66ac9dbSNicholas Bellinger struct se_lun *lun, 948c66ac9dbSNicholas Bellinger struct se_lun_acl *lun_acl) 949c66ac9dbSNicholas Bellinger { 950c66ac9dbSNicholas Bellinger struct se_node_acl *nacl = lun_acl->se_lun_nacl; 951f2083241SJörn Engel struct se_dev_entry *deve = nacl->device_list[lun_acl->mapped_lun]; 952c66ac9dbSNicholas Bellinger 953d977f437SChristoph Hellwig if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) 954c66ac9dbSNicholas Bellinger return 0; 955c66ac9dbSNicholas Bellinger 956c66ac9dbSNicholas Bellinger return __core_scsi3_check_aptpl_registration(dev, tpg, lun, 957c66ac9dbSNicholas Bellinger lun->unpacked_lun, nacl, deve); 958c66ac9dbSNicholas Bellinger } 959c66ac9dbSNicholas Bellinger 960c66ac9dbSNicholas Bellinger static void __core_scsi3_dump_registration( 961c66ac9dbSNicholas Bellinger struct target_core_fabric_ops *tfo, 962c66ac9dbSNicholas Bellinger struct se_device *dev, 963c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 964c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, 965c66ac9dbSNicholas Bellinger int register_type) 966c66ac9dbSNicholas Bellinger { 967c66ac9dbSNicholas Bellinger struct se_portal_group *se_tpg = nacl->se_tpg; 968c66ac9dbSNicholas Bellinger char i_buf[PR_REG_ISID_ID_LEN]; 969c66ac9dbSNicholas Bellinger int prf_isid; 970c66ac9dbSNicholas Bellinger 971c66ac9dbSNicholas Bellinger memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN); 972c66ac9dbSNicholas Bellinger prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0], 973c66ac9dbSNicholas Bellinger PR_REG_ISID_ID_LEN); 974c66ac9dbSNicholas Bellinger 9756708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator" 976c66ac9dbSNicholas Bellinger " Node: %s%s\n", tfo->get_fabric_name(), (register_type == 2) ? 977c66ac9dbSNicholas Bellinger "_AND_MOVE" : (register_type == 1) ? 978c66ac9dbSNicholas Bellinger "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname, 979c66ac9dbSNicholas Bellinger (prf_isid) ? i_buf : ""); 9806708bb27SAndy Grover pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n", 981c66ac9dbSNicholas Bellinger tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg), 982c66ac9dbSNicholas Bellinger tfo->tpg_get_tag(se_tpg)); 9836708bb27SAndy Grover pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target" 984c66ac9dbSNicholas Bellinger " Port(s)\n", tfo->get_fabric_name(), 985c66ac9dbSNicholas Bellinger (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE", 986e3d6f909SAndy Grover dev->transport->name); 9876708bb27SAndy Grover pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:" 988c66ac9dbSNicholas Bellinger " 0x%08x APTPL: %d\n", tfo->get_fabric_name(), 989c66ac9dbSNicholas Bellinger pr_reg->pr_res_key, pr_reg->pr_res_generation, 990c66ac9dbSNicholas Bellinger pr_reg->pr_reg_aptpl); 991c66ac9dbSNicholas Bellinger } 992c66ac9dbSNicholas Bellinger 993c66ac9dbSNicholas Bellinger /* 994c66ac9dbSNicholas Bellinger * this function can be called with struct se_device->dev_reservation_lock 995c66ac9dbSNicholas Bellinger * when register_move = 1 996c66ac9dbSNicholas Bellinger */ 997c66ac9dbSNicholas Bellinger static void __core_scsi3_add_registration( 998c66ac9dbSNicholas Bellinger struct se_device *dev, 999c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 1000c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, 1001c66ac9dbSNicholas Bellinger int register_type, 1002c66ac9dbSNicholas Bellinger int register_move) 1003c66ac9dbSNicholas Bellinger { 1004c66ac9dbSNicholas Bellinger struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo; 1005c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe; 10060fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 1007c66ac9dbSNicholas Bellinger 1008c66ac9dbSNicholas Bellinger /* 1009c66ac9dbSNicholas Bellinger * Increment PRgeneration counter for struct se_device upon a successful 1010c66ac9dbSNicholas Bellinger * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action 1011c66ac9dbSNicholas Bellinger * 1012c66ac9dbSNicholas Bellinger * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service 1013c66ac9dbSNicholas Bellinger * action, the struct se_device->dev_reservation_lock will already be held, 1014c66ac9dbSNicholas Bellinger * so we do not call core_scsi3_pr_generation() which grabs the lock 1015c66ac9dbSNicholas Bellinger * for the REGISTER. 1016c66ac9dbSNicholas Bellinger */ 1017c66ac9dbSNicholas Bellinger pr_reg->pr_res_generation = (register_move) ? 10180fd97ccfSChristoph Hellwig dev->t10_pr.pr_generation++ : 1019c66ac9dbSNicholas Bellinger core_scsi3_pr_generation(dev); 1020c66ac9dbSNicholas Bellinger 1021c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 1022c66ac9dbSNicholas Bellinger list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list); 1023c66ac9dbSNicholas Bellinger pr_reg->pr_reg_deve->def_pr_registered = 1; 1024c66ac9dbSNicholas Bellinger 1025c66ac9dbSNicholas Bellinger __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type); 1026c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 1027c66ac9dbSNicholas Bellinger /* 1028c66ac9dbSNicholas Bellinger * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE. 1029c66ac9dbSNicholas Bellinger */ 10306708bb27SAndy Grover if (!pr_reg->pr_reg_all_tg_pt || register_move) 1031c66ac9dbSNicholas Bellinger return; 1032c66ac9dbSNicholas Bellinger /* 1033c66ac9dbSNicholas Bellinger * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1 1034c66ac9dbSNicholas Bellinger * allocated in __core_scsi3_alloc_registration() 1035c66ac9dbSNicholas Bellinger */ 1036c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe, 1037c66ac9dbSNicholas Bellinger &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) { 1038c66ac9dbSNicholas Bellinger list_del(&pr_reg_tmp->pr_reg_atp_mem_list); 1039c66ac9dbSNicholas Bellinger 1040c66ac9dbSNicholas Bellinger pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev); 1041c66ac9dbSNicholas Bellinger 1042c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 1043c66ac9dbSNicholas Bellinger list_add_tail(&pr_reg_tmp->pr_reg_list, 1044c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list); 1045c66ac9dbSNicholas Bellinger pr_reg_tmp->pr_reg_deve->def_pr_registered = 1; 1046c66ac9dbSNicholas Bellinger 1047c66ac9dbSNicholas Bellinger __core_scsi3_dump_registration(tfo, dev, 1048c66ac9dbSNicholas Bellinger pr_reg_tmp->pr_reg_nacl, pr_reg_tmp, 1049c66ac9dbSNicholas Bellinger register_type); 1050c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 1051c66ac9dbSNicholas Bellinger /* 1052c66ac9dbSNicholas Bellinger * Drop configfs group dependency reference from 1053c66ac9dbSNicholas Bellinger * __core_scsi3_alloc_registration() 1054c66ac9dbSNicholas Bellinger */ 1055c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve); 1056c66ac9dbSNicholas Bellinger } 1057c66ac9dbSNicholas Bellinger } 1058c66ac9dbSNicholas Bellinger 1059c66ac9dbSNicholas Bellinger static int core_scsi3_alloc_registration( 1060c66ac9dbSNicholas Bellinger struct se_device *dev, 1061c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 1062c66ac9dbSNicholas Bellinger struct se_dev_entry *deve, 1063c66ac9dbSNicholas Bellinger unsigned char *isid, 1064c66ac9dbSNicholas Bellinger u64 sa_res_key, 1065c66ac9dbSNicholas Bellinger int all_tg_pt, 1066c66ac9dbSNicholas Bellinger int aptpl, 1067c66ac9dbSNicholas Bellinger int register_type, 1068c66ac9dbSNicholas Bellinger int register_move) 1069c66ac9dbSNicholas Bellinger { 1070c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg; 1071c66ac9dbSNicholas Bellinger 1072c66ac9dbSNicholas Bellinger pr_reg = __core_scsi3_alloc_registration(dev, nacl, deve, isid, 1073c66ac9dbSNicholas Bellinger sa_res_key, all_tg_pt, aptpl); 10746708bb27SAndy Grover if (!pr_reg) 1075e3d6f909SAndy Grover return -EPERM; 1076c66ac9dbSNicholas Bellinger 1077c66ac9dbSNicholas Bellinger __core_scsi3_add_registration(dev, nacl, pr_reg, 1078c66ac9dbSNicholas Bellinger register_type, register_move); 1079c66ac9dbSNicholas Bellinger return 0; 1080c66ac9dbSNicholas Bellinger } 1081c66ac9dbSNicholas Bellinger 1082c66ac9dbSNicholas Bellinger static struct t10_pr_registration *__core_scsi3_locate_pr_reg( 1083c66ac9dbSNicholas Bellinger struct se_device *dev, 1084c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 1085c66ac9dbSNicholas Bellinger unsigned char *isid) 1086c66ac9dbSNicholas Bellinger { 10870fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 1088c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp; 1089c66ac9dbSNicholas Bellinger struct se_portal_group *tpg; 1090c66ac9dbSNicholas Bellinger 1091c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 1092c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 1093c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 1094c66ac9dbSNicholas Bellinger /* 1095c66ac9dbSNicholas Bellinger * First look for a matching struct se_node_acl 1096c66ac9dbSNicholas Bellinger */ 1097c66ac9dbSNicholas Bellinger if (pr_reg->pr_reg_nacl != nacl) 1098c66ac9dbSNicholas Bellinger continue; 1099c66ac9dbSNicholas Bellinger 1100c66ac9dbSNicholas Bellinger tpg = pr_reg->pr_reg_nacl->se_tpg; 1101c66ac9dbSNicholas Bellinger /* 1102c66ac9dbSNicholas Bellinger * If this registration does NOT contain a fabric provided 1103c66ac9dbSNicholas Bellinger * ISID, then we have found a match. 1104c66ac9dbSNicholas Bellinger */ 11056708bb27SAndy Grover if (!pr_reg->isid_present_at_reg) { 1106c66ac9dbSNicholas Bellinger /* 1107c66ac9dbSNicholas Bellinger * Determine if this SCSI device server requires that 1108c66ac9dbSNicholas Bellinger * SCSI Intiatior TransportID w/ ISIDs is enforced 1109c66ac9dbSNicholas Bellinger * for fabric modules (iSCSI) requiring them. 1110c66ac9dbSNicholas Bellinger */ 1111e3d6f909SAndy Grover if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) { 11120fd97ccfSChristoph Hellwig if (dev->dev_attrib.enforce_pr_isids) 1113c66ac9dbSNicholas Bellinger continue; 1114c66ac9dbSNicholas Bellinger } 1115c66ac9dbSNicholas Bellinger atomic_inc(&pr_reg->pr_res_holders); 1116c66ac9dbSNicholas Bellinger smp_mb__after_atomic_inc(); 1117c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 1118c66ac9dbSNicholas Bellinger return pr_reg; 1119c66ac9dbSNicholas Bellinger } 1120c66ac9dbSNicholas Bellinger /* 1121c66ac9dbSNicholas Bellinger * If the *pr_reg contains a fabric defined ISID for multi-value 1122c66ac9dbSNicholas Bellinger * SCSI Initiator Port TransportIDs, then we expect a valid 1123c66ac9dbSNicholas Bellinger * matching ISID to be provided by the local SCSI Initiator Port. 1124c66ac9dbSNicholas Bellinger */ 11256708bb27SAndy Grover if (!isid) 1126c66ac9dbSNicholas Bellinger continue; 1127c66ac9dbSNicholas Bellinger if (strcmp(isid, pr_reg->pr_reg_isid)) 1128c66ac9dbSNicholas Bellinger continue; 1129c66ac9dbSNicholas Bellinger 1130c66ac9dbSNicholas Bellinger atomic_inc(&pr_reg->pr_res_holders); 1131c66ac9dbSNicholas Bellinger smp_mb__after_atomic_inc(); 1132c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 1133c66ac9dbSNicholas Bellinger return pr_reg; 1134c66ac9dbSNicholas Bellinger } 1135c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 1136c66ac9dbSNicholas Bellinger 1137c66ac9dbSNicholas Bellinger return NULL; 1138c66ac9dbSNicholas Bellinger } 1139c66ac9dbSNicholas Bellinger 1140c66ac9dbSNicholas Bellinger static struct t10_pr_registration *core_scsi3_locate_pr_reg( 1141c66ac9dbSNicholas Bellinger struct se_device *dev, 1142c66ac9dbSNicholas Bellinger struct se_node_acl *nacl, 1143c66ac9dbSNicholas Bellinger struct se_session *sess) 1144c66ac9dbSNicholas Bellinger { 1145c66ac9dbSNicholas Bellinger struct se_portal_group *tpg = nacl->se_tpg; 1146c66ac9dbSNicholas Bellinger unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL; 1147c66ac9dbSNicholas Bellinger 1148e3d6f909SAndy Grover if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) { 1149c66ac9dbSNicholas Bellinger memset(&buf[0], 0, PR_REG_ISID_LEN); 1150e3d6f909SAndy Grover tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0], 1151c66ac9dbSNicholas Bellinger PR_REG_ISID_LEN); 1152c66ac9dbSNicholas Bellinger isid_ptr = &buf[0]; 1153c66ac9dbSNicholas Bellinger } 1154c66ac9dbSNicholas Bellinger 1155c66ac9dbSNicholas Bellinger return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr); 1156c66ac9dbSNicholas Bellinger } 1157c66ac9dbSNicholas Bellinger 1158c66ac9dbSNicholas Bellinger static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg) 1159c66ac9dbSNicholas Bellinger { 1160c66ac9dbSNicholas Bellinger atomic_dec(&pr_reg->pr_res_holders); 1161c66ac9dbSNicholas Bellinger smp_mb__after_atomic_dec(); 1162c66ac9dbSNicholas Bellinger } 1163c66ac9dbSNicholas Bellinger 1164c66ac9dbSNicholas Bellinger static int core_scsi3_check_implict_release( 1165c66ac9dbSNicholas Bellinger struct se_device *dev, 1166c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg) 1167c66ac9dbSNicholas Bellinger { 1168c66ac9dbSNicholas Bellinger struct se_node_acl *nacl = pr_reg->pr_reg_nacl; 1169c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_res_holder; 1170c66ac9dbSNicholas Bellinger int ret = 0; 1171c66ac9dbSNicholas Bellinger 1172c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 1173c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 11746708bb27SAndy Grover if (!pr_res_holder) { 1175c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 1176c66ac9dbSNicholas Bellinger return ret; 1177c66ac9dbSNicholas Bellinger } 1178c66ac9dbSNicholas Bellinger if (pr_res_holder == pr_reg) { 1179c66ac9dbSNicholas Bellinger /* 1180c66ac9dbSNicholas Bellinger * Perform an implict RELEASE if the registration that 1181c66ac9dbSNicholas Bellinger * is being released is holding the reservation. 1182c66ac9dbSNicholas Bellinger * 1183c66ac9dbSNicholas Bellinger * From spc4r17, section 5.7.11.1: 1184c66ac9dbSNicholas Bellinger * 1185c66ac9dbSNicholas Bellinger * e) If the I_T nexus is the persistent reservation holder 1186c66ac9dbSNicholas Bellinger * and the persistent reservation is not an all registrants 1187c66ac9dbSNicholas Bellinger * type, then a PERSISTENT RESERVE OUT command with REGISTER 1188c66ac9dbSNicholas Bellinger * service action or REGISTER AND IGNORE EXISTING KEY 1189c66ac9dbSNicholas Bellinger * service action with the SERVICE ACTION RESERVATION KEY 1190c66ac9dbSNicholas Bellinger * field set to zero (see 5.7.11.3). 1191c66ac9dbSNicholas Bellinger */ 1192c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0); 1193c66ac9dbSNicholas Bellinger ret = 1; 1194c66ac9dbSNicholas Bellinger /* 1195c66ac9dbSNicholas Bellinger * For 'All Registrants' reservation types, all existing 1196c66ac9dbSNicholas Bellinger * registrations are still processed as reservation holders 1197c66ac9dbSNicholas Bellinger * in core_scsi3_pr_seq_non_holder() after the initial 1198c66ac9dbSNicholas Bellinger * reservation holder is implictly released here. 1199c66ac9dbSNicholas Bellinger */ 1200c66ac9dbSNicholas Bellinger } else if (pr_reg->pr_reg_all_tg_pt && 1201c66ac9dbSNicholas Bellinger (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname, 1202c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl->initiatorname)) && 1203c66ac9dbSNicholas Bellinger (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) { 12046708bb27SAndy Grover pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1" 1205c66ac9dbSNicholas Bellinger " UNREGISTER while existing reservation with matching" 1206c66ac9dbSNicholas Bellinger " key 0x%016Lx is present from another SCSI Initiator" 1207c66ac9dbSNicholas Bellinger " Port\n", pr_reg->pr_res_key); 1208e3d6f909SAndy Grover ret = -EPERM; 1209c66ac9dbSNicholas Bellinger } 1210c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 1211c66ac9dbSNicholas Bellinger 1212c66ac9dbSNicholas Bellinger return ret; 1213c66ac9dbSNicholas Bellinger } 1214c66ac9dbSNicholas Bellinger 1215c66ac9dbSNicholas Bellinger /* 1216e3d6f909SAndy Grover * Called with struct t10_reservation->registration_lock held. 1217c66ac9dbSNicholas Bellinger */ 1218c66ac9dbSNicholas Bellinger static void __core_scsi3_free_registration( 1219c66ac9dbSNicholas Bellinger struct se_device *dev, 1220c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, 1221c66ac9dbSNicholas Bellinger struct list_head *preempt_and_abort_list, 1222c66ac9dbSNicholas Bellinger int dec_holders) 1223c66ac9dbSNicholas Bellinger { 1224c66ac9dbSNicholas Bellinger struct target_core_fabric_ops *tfo = 1225c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo; 12260fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 1227c66ac9dbSNicholas Bellinger char i_buf[PR_REG_ISID_ID_LEN]; 1228c66ac9dbSNicholas Bellinger int prf_isid; 1229c66ac9dbSNicholas Bellinger 1230c66ac9dbSNicholas Bellinger memset(i_buf, 0, PR_REG_ISID_ID_LEN); 1231c66ac9dbSNicholas Bellinger prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0], 1232c66ac9dbSNicholas Bellinger PR_REG_ISID_ID_LEN); 1233c66ac9dbSNicholas Bellinger 1234c66ac9dbSNicholas Bellinger pr_reg->pr_reg_deve->def_pr_registered = 0; 1235c66ac9dbSNicholas Bellinger pr_reg->pr_reg_deve->pr_res_key = 0; 1236c66ac9dbSNicholas Bellinger list_del(&pr_reg->pr_reg_list); 1237c66ac9dbSNicholas Bellinger /* 1238c66ac9dbSNicholas Bellinger * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(), 1239c66ac9dbSNicholas Bellinger * so call core_scsi3_put_pr_reg() to decrement our reference. 1240c66ac9dbSNicholas Bellinger */ 1241c66ac9dbSNicholas Bellinger if (dec_holders) 1242c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg); 1243c66ac9dbSNicholas Bellinger /* 1244c66ac9dbSNicholas Bellinger * Wait until all reference from any other I_T nexuses for this 1245c66ac9dbSNicholas Bellinger * *pr_reg have been released. Because list_del() is called above, 1246c66ac9dbSNicholas Bellinger * the last core_scsi3_put_pr_reg(pr_reg) will release this reference 1247c66ac9dbSNicholas Bellinger * count back to zero, and we release *pr_reg. 1248c66ac9dbSNicholas Bellinger */ 1249c66ac9dbSNicholas Bellinger while (atomic_read(&pr_reg->pr_res_holders) != 0) { 1250c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 12516708bb27SAndy Grover pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n", 1252c66ac9dbSNicholas Bellinger tfo->get_fabric_name()); 1253c66ac9dbSNicholas Bellinger cpu_relax(); 1254c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 1255c66ac9dbSNicholas Bellinger } 1256c66ac9dbSNicholas Bellinger 12576708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator" 1258c66ac9dbSNicholas Bellinger " Node: %s%s\n", tfo->get_fabric_name(), 1259c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl->initiatorname, 1260c66ac9dbSNicholas Bellinger (prf_isid) ? &i_buf[0] : ""); 12616708bb27SAndy Grover pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target" 1262c66ac9dbSNicholas Bellinger " Port(s)\n", tfo->get_fabric_name(), 1263c66ac9dbSNicholas Bellinger (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE", 1264e3d6f909SAndy Grover dev->transport->name); 12656708bb27SAndy Grover pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:" 1266c66ac9dbSNicholas Bellinger " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key, 1267c66ac9dbSNicholas Bellinger pr_reg->pr_res_generation); 1268c66ac9dbSNicholas Bellinger 12696708bb27SAndy Grover if (!preempt_and_abort_list) { 1270c66ac9dbSNicholas Bellinger pr_reg->pr_reg_deve = NULL; 1271c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl = NULL; 1272c66ac9dbSNicholas Bellinger kfree(pr_reg->pr_aptpl_buf); 1273c66ac9dbSNicholas Bellinger kmem_cache_free(t10_pr_reg_cache, pr_reg); 1274c66ac9dbSNicholas Bellinger return; 1275c66ac9dbSNicholas Bellinger } 1276c66ac9dbSNicholas Bellinger /* 1277c66ac9dbSNicholas Bellinger * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list 1278c66ac9dbSNicholas Bellinger * are released once the ABORT_TASK_SET has completed.. 1279c66ac9dbSNicholas Bellinger */ 1280c66ac9dbSNicholas Bellinger list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list); 1281c66ac9dbSNicholas Bellinger } 1282c66ac9dbSNicholas Bellinger 1283c66ac9dbSNicholas Bellinger void core_scsi3_free_pr_reg_from_nacl( 1284c66ac9dbSNicholas Bellinger struct se_device *dev, 1285c66ac9dbSNicholas Bellinger struct se_node_acl *nacl) 1286c66ac9dbSNicholas Bellinger { 12870fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 1288c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder; 1289c66ac9dbSNicholas Bellinger /* 1290c66ac9dbSNicholas Bellinger * If the passed se_node_acl matches the reservation holder, 1291c66ac9dbSNicholas Bellinger * release the reservation. 1292c66ac9dbSNicholas Bellinger */ 1293c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 1294c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 1295c66ac9dbSNicholas Bellinger if ((pr_res_holder != NULL) && 1296c66ac9dbSNicholas Bellinger (pr_res_holder->pr_reg_nacl == nacl)) 1297c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0); 1298c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 1299c66ac9dbSNicholas Bellinger /* 1300c66ac9dbSNicholas Bellinger * Release any registration associated with the struct se_node_acl. 1301c66ac9dbSNicholas Bellinger */ 1302c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 1303c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 1304c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 1305c66ac9dbSNicholas Bellinger 1306c66ac9dbSNicholas Bellinger if (pr_reg->pr_reg_nacl != nacl) 1307c66ac9dbSNicholas Bellinger continue; 1308c66ac9dbSNicholas Bellinger 1309c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, pr_reg, NULL, 0); 1310c66ac9dbSNicholas Bellinger } 1311c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 1312c66ac9dbSNicholas Bellinger } 1313c66ac9dbSNicholas Bellinger 1314c66ac9dbSNicholas Bellinger void core_scsi3_free_all_registrations( 1315c66ac9dbSNicholas Bellinger struct se_device *dev) 1316c66ac9dbSNicholas Bellinger { 13170fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 1318c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder; 1319c66ac9dbSNicholas Bellinger 1320c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 1321c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 1322c66ac9dbSNicholas Bellinger if (pr_res_holder != NULL) { 1323c66ac9dbSNicholas Bellinger struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; 1324c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_release(dev, pr_res_nacl, 1325c66ac9dbSNicholas Bellinger pr_res_holder, 0); 1326c66ac9dbSNicholas Bellinger } 1327c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 1328c66ac9dbSNicholas Bellinger 1329c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 1330c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 1331c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 1332c66ac9dbSNicholas Bellinger 1333c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, pr_reg, NULL, 0); 1334c66ac9dbSNicholas Bellinger } 1335c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 1336c66ac9dbSNicholas Bellinger 1337c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->aptpl_reg_lock); 1338c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list, 1339c66ac9dbSNicholas Bellinger pr_reg_aptpl_list) { 1340c66ac9dbSNicholas Bellinger list_del(&pr_reg->pr_reg_aptpl_list); 1341c66ac9dbSNicholas Bellinger kfree(pr_reg->pr_aptpl_buf); 1342c66ac9dbSNicholas Bellinger kmem_cache_free(t10_pr_reg_cache, pr_reg); 1343c66ac9dbSNicholas Bellinger } 1344c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->aptpl_reg_lock); 1345c66ac9dbSNicholas Bellinger } 1346c66ac9dbSNicholas Bellinger 1347c66ac9dbSNicholas Bellinger static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg) 1348c66ac9dbSNicholas Bellinger { 1349e3d6f909SAndy Grover return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys, 1350c66ac9dbSNicholas Bellinger &tpg->tpg_group.cg_item); 1351c66ac9dbSNicholas Bellinger } 1352c66ac9dbSNicholas Bellinger 1353c66ac9dbSNicholas Bellinger static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg) 1354c66ac9dbSNicholas Bellinger { 1355e3d6f909SAndy Grover configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys, 1356c66ac9dbSNicholas Bellinger &tpg->tpg_group.cg_item); 1357c66ac9dbSNicholas Bellinger 1358c66ac9dbSNicholas Bellinger atomic_dec(&tpg->tpg_pr_ref_count); 1359c66ac9dbSNicholas Bellinger smp_mb__after_atomic_dec(); 1360c66ac9dbSNicholas Bellinger } 1361c66ac9dbSNicholas Bellinger 1362c66ac9dbSNicholas Bellinger static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl) 1363c66ac9dbSNicholas Bellinger { 1364c66ac9dbSNicholas Bellinger struct se_portal_group *tpg = nacl->se_tpg; 1365c66ac9dbSNicholas Bellinger 1366c66ac9dbSNicholas Bellinger if (nacl->dynamic_node_acl) 1367c66ac9dbSNicholas Bellinger return 0; 1368c66ac9dbSNicholas Bellinger 1369e3d6f909SAndy Grover return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys, 1370c66ac9dbSNicholas Bellinger &nacl->acl_group.cg_item); 1371c66ac9dbSNicholas Bellinger } 1372c66ac9dbSNicholas Bellinger 1373c66ac9dbSNicholas Bellinger static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl) 1374c66ac9dbSNicholas Bellinger { 1375c66ac9dbSNicholas Bellinger struct se_portal_group *tpg = nacl->se_tpg; 1376c66ac9dbSNicholas Bellinger 1377c66ac9dbSNicholas Bellinger if (nacl->dynamic_node_acl) { 1378c66ac9dbSNicholas Bellinger atomic_dec(&nacl->acl_pr_ref_count); 1379c66ac9dbSNicholas Bellinger smp_mb__after_atomic_dec(); 1380c66ac9dbSNicholas Bellinger return; 1381c66ac9dbSNicholas Bellinger } 1382c66ac9dbSNicholas Bellinger 1383e3d6f909SAndy Grover configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys, 1384c66ac9dbSNicholas Bellinger &nacl->acl_group.cg_item); 1385c66ac9dbSNicholas Bellinger 1386c66ac9dbSNicholas Bellinger atomic_dec(&nacl->acl_pr_ref_count); 1387c66ac9dbSNicholas Bellinger smp_mb__after_atomic_dec(); 1388c66ac9dbSNicholas Bellinger } 1389c66ac9dbSNicholas Bellinger 1390c66ac9dbSNicholas Bellinger static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve) 1391c66ac9dbSNicholas Bellinger { 1392c66ac9dbSNicholas Bellinger struct se_lun_acl *lun_acl = se_deve->se_lun_acl; 1393c66ac9dbSNicholas Bellinger struct se_node_acl *nacl; 1394c66ac9dbSNicholas Bellinger struct se_portal_group *tpg; 1395c66ac9dbSNicholas Bellinger /* 1396c66ac9dbSNicholas Bellinger * For nacl->dynamic_node_acl=1 1397c66ac9dbSNicholas Bellinger */ 13986708bb27SAndy Grover if (!lun_acl) 1399c66ac9dbSNicholas Bellinger return 0; 1400c66ac9dbSNicholas Bellinger 1401c66ac9dbSNicholas Bellinger nacl = lun_acl->se_lun_nacl; 1402c66ac9dbSNicholas Bellinger tpg = nacl->se_tpg; 1403c66ac9dbSNicholas Bellinger 1404e3d6f909SAndy Grover return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys, 1405c66ac9dbSNicholas Bellinger &lun_acl->se_lun_group.cg_item); 1406c66ac9dbSNicholas Bellinger } 1407c66ac9dbSNicholas Bellinger 1408c66ac9dbSNicholas Bellinger static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve) 1409c66ac9dbSNicholas Bellinger { 1410c66ac9dbSNicholas Bellinger struct se_lun_acl *lun_acl = se_deve->se_lun_acl; 1411c66ac9dbSNicholas Bellinger struct se_node_acl *nacl; 1412c66ac9dbSNicholas Bellinger struct se_portal_group *tpg; 1413c66ac9dbSNicholas Bellinger /* 1414c66ac9dbSNicholas Bellinger * For nacl->dynamic_node_acl=1 1415c66ac9dbSNicholas Bellinger */ 14166708bb27SAndy Grover if (!lun_acl) { 1417c66ac9dbSNicholas Bellinger atomic_dec(&se_deve->pr_ref_count); 1418c66ac9dbSNicholas Bellinger smp_mb__after_atomic_dec(); 1419c66ac9dbSNicholas Bellinger return; 1420c66ac9dbSNicholas Bellinger } 1421c66ac9dbSNicholas Bellinger nacl = lun_acl->se_lun_nacl; 1422c66ac9dbSNicholas Bellinger tpg = nacl->se_tpg; 1423c66ac9dbSNicholas Bellinger 1424e3d6f909SAndy Grover configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys, 1425c66ac9dbSNicholas Bellinger &lun_acl->se_lun_group.cg_item); 1426c66ac9dbSNicholas Bellinger 1427c66ac9dbSNicholas Bellinger atomic_dec(&se_deve->pr_ref_count); 1428c66ac9dbSNicholas Bellinger smp_mb__after_atomic_dec(); 1429c66ac9dbSNicholas Bellinger } 1430c66ac9dbSNicholas Bellinger 1431de103c93SChristoph Hellwig static sense_reason_t 1432de103c93SChristoph Hellwig core_scsi3_decode_spec_i_port( 1433c66ac9dbSNicholas Bellinger struct se_cmd *cmd, 1434c66ac9dbSNicholas Bellinger struct se_portal_group *tpg, 1435c66ac9dbSNicholas Bellinger unsigned char *l_isid, 1436c66ac9dbSNicholas Bellinger u64 sa_res_key, 1437c66ac9dbSNicholas Bellinger int all_tg_pt, 1438c66ac9dbSNicholas Bellinger int aptpl) 1439c66ac9dbSNicholas Bellinger { 14405951146dSAndy Grover struct se_device *dev = cmd->se_dev; 1441c66ac9dbSNicholas Bellinger struct se_port *tmp_port; 1442c66ac9dbSNicholas Bellinger struct se_portal_group *dest_tpg = NULL, *tmp_tpg; 1443e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 1444c66ac9dbSNicholas Bellinger struct se_node_acl *dest_node_acl = NULL; 1445c66ac9dbSNicholas Bellinger struct se_dev_entry *dest_se_deve = NULL, *local_se_deve; 1446c66ac9dbSNicholas Bellinger struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e; 1447c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe; 1448d0f474e5SRoland Dreier LIST_HEAD(tid_dest_list); 1449c66ac9dbSNicholas Bellinger struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp; 1450c66ac9dbSNicholas Bellinger struct target_core_fabric_ops *tmp_tf_ops; 145105d1c7c0SAndy Grover unsigned char *buf; 1452c66ac9dbSNicholas Bellinger unsigned char *ptr, *i_str = NULL, proto_ident, tmp_proto_ident; 1453c66ac9dbSNicholas Bellinger char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN]; 1454de103c93SChristoph Hellwig sense_reason_t ret; 1455c66ac9dbSNicholas Bellinger u32 tpdl, tid_len = 0; 1456de103c93SChristoph Hellwig int dest_local_nexus, prf_isid; 1457c66ac9dbSNicholas Bellinger u32 dest_rtpi = 0; 1458c66ac9dbSNicholas Bellinger 1459c66ac9dbSNicholas Bellinger memset(dest_iport, 0, 64); 1460c66ac9dbSNicholas Bellinger 1461f2083241SJörn Engel local_se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun]; 1462c66ac9dbSNicholas Bellinger /* 1463c66ac9dbSNicholas Bellinger * Allocate a struct pr_transport_id_holder and setup the 1464c66ac9dbSNicholas Bellinger * local_node_acl and local_se_deve pointers and add to 1465c66ac9dbSNicholas Bellinger * struct list_head tid_dest_list for add registration 1466c66ac9dbSNicholas Bellinger * processing in the loop of tid_dest_list below. 1467c66ac9dbSNicholas Bellinger */ 1468c66ac9dbSNicholas Bellinger tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL); 14696708bb27SAndy Grover if (!tidh_new) { 14706708bb27SAndy Grover pr_err("Unable to allocate tidh_new\n"); 1471de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1472c66ac9dbSNicholas Bellinger } 1473c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&tidh_new->dest_list); 1474c66ac9dbSNicholas Bellinger tidh_new->dest_tpg = tpg; 1475c66ac9dbSNicholas Bellinger tidh_new->dest_node_acl = se_sess->se_node_acl; 1476c66ac9dbSNicholas Bellinger tidh_new->dest_se_deve = local_se_deve; 1477c66ac9dbSNicholas Bellinger 14785951146dSAndy Grover local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev, 1479c66ac9dbSNicholas Bellinger se_sess->se_node_acl, local_se_deve, l_isid, 1480c66ac9dbSNicholas Bellinger sa_res_key, all_tg_pt, aptpl); 14816708bb27SAndy Grover if (!local_pr_reg) { 1482c66ac9dbSNicholas Bellinger kfree(tidh_new); 1483de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1484c66ac9dbSNicholas Bellinger } 1485c66ac9dbSNicholas Bellinger tidh_new->dest_pr_reg = local_pr_reg; 1486c66ac9dbSNicholas Bellinger /* 1487c66ac9dbSNicholas Bellinger * The local I_T nexus does not hold any configfs dependances, 1488c66ac9dbSNicholas Bellinger * so we set tid_h->dest_local_nexus=1 to prevent the 1489c66ac9dbSNicholas Bellinger * configfs_undepend_item() calls in the tid_dest_list loops below. 1490c66ac9dbSNicholas Bellinger */ 1491c66ac9dbSNicholas Bellinger tidh_new->dest_local_nexus = 1; 1492c66ac9dbSNicholas Bellinger list_add_tail(&tidh_new->dest_list, &tid_dest_list); 149305d1c7c0SAndy Grover 14940d7f1299SPaolo Bonzini if (cmd->data_length < 28) { 14950d7f1299SPaolo Bonzini pr_warn("SPC-PR: Received PR OUT parameter list" 14960d7f1299SPaolo Bonzini " length too small: %u\n", cmd->data_length); 1497de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 14980d7f1299SPaolo Bonzini goto out; 14990d7f1299SPaolo Bonzini } 15000d7f1299SPaolo Bonzini 15014949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 1502de103c93SChristoph Hellwig if (!buf) { 1503de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1504de103c93SChristoph Hellwig goto out; 1505de103c93SChristoph Hellwig } 1506de103c93SChristoph Hellwig 1507c66ac9dbSNicholas Bellinger /* 1508c66ac9dbSNicholas Bellinger * For a PERSISTENT RESERVE OUT specify initiator ports payload, 1509c66ac9dbSNicholas Bellinger * first extract TransportID Parameter Data Length, and make sure 1510c66ac9dbSNicholas Bellinger * the value matches up to the SCSI expected data transfer length. 1511c66ac9dbSNicholas Bellinger */ 1512c66ac9dbSNicholas Bellinger tpdl = (buf[24] & 0xff) << 24; 1513c66ac9dbSNicholas Bellinger tpdl |= (buf[25] & 0xff) << 16; 1514c66ac9dbSNicholas Bellinger tpdl |= (buf[26] & 0xff) << 8; 1515c66ac9dbSNicholas Bellinger tpdl |= buf[27] & 0xff; 1516c66ac9dbSNicholas Bellinger 1517c66ac9dbSNicholas Bellinger if ((tpdl + 28) != cmd->data_length) { 15186708bb27SAndy Grover pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header" 1519c66ac9dbSNicholas Bellinger " does not equal CDB data_length: %u\n", tpdl, 1520c66ac9dbSNicholas Bellinger cmd->data_length); 1521de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 1522de103c93SChristoph Hellwig goto out_unmap; 1523c66ac9dbSNicholas Bellinger } 1524c66ac9dbSNicholas Bellinger /* 1525c66ac9dbSNicholas Bellinger * Start processing the received transport IDs using the 1526c66ac9dbSNicholas Bellinger * receiving I_T Nexus portal's fabric dependent methods to 1527c66ac9dbSNicholas Bellinger * obtain the SCSI Initiator Port/Device Identifiers. 1528c66ac9dbSNicholas Bellinger */ 1529c66ac9dbSNicholas Bellinger ptr = &buf[28]; 1530c66ac9dbSNicholas Bellinger 1531c66ac9dbSNicholas Bellinger while (tpdl > 0) { 1532c66ac9dbSNicholas Bellinger proto_ident = (ptr[0] & 0x0f); 1533c66ac9dbSNicholas Bellinger dest_tpg = NULL; 1534c66ac9dbSNicholas Bellinger 1535c66ac9dbSNicholas Bellinger spin_lock(&dev->se_port_lock); 1536c66ac9dbSNicholas Bellinger list_for_each_entry(tmp_port, &dev->dev_sep_list, sep_list) { 1537c66ac9dbSNicholas Bellinger tmp_tpg = tmp_port->sep_tpg; 15386708bb27SAndy Grover if (!tmp_tpg) 1539c66ac9dbSNicholas Bellinger continue; 1540e3d6f909SAndy Grover tmp_tf_ops = tmp_tpg->se_tpg_tfo; 15416708bb27SAndy Grover if (!tmp_tf_ops) 1542c66ac9dbSNicholas Bellinger continue; 15436708bb27SAndy Grover if (!tmp_tf_ops->get_fabric_proto_ident || 15446708bb27SAndy Grover !tmp_tf_ops->tpg_parse_pr_out_transport_id) 1545c66ac9dbSNicholas Bellinger continue; 1546c66ac9dbSNicholas Bellinger /* 1547c66ac9dbSNicholas Bellinger * Look for the matching proto_ident provided by 1548c66ac9dbSNicholas Bellinger * the received TransportID 1549c66ac9dbSNicholas Bellinger */ 1550c66ac9dbSNicholas Bellinger tmp_proto_ident = tmp_tf_ops->get_fabric_proto_ident(tmp_tpg); 1551c66ac9dbSNicholas Bellinger if (tmp_proto_ident != proto_ident) 1552c66ac9dbSNicholas Bellinger continue; 1553c66ac9dbSNicholas Bellinger dest_rtpi = tmp_port->sep_rtpi; 1554c66ac9dbSNicholas Bellinger 1555c66ac9dbSNicholas Bellinger i_str = tmp_tf_ops->tpg_parse_pr_out_transport_id( 1556c66ac9dbSNicholas Bellinger tmp_tpg, (const char *)ptr, &tid_len, 1557c66ac9dbSNicholas Bellinger &iport_ptr); 15586708bb27SAndy Grover if (!i_str) 1559c66ac9dbSNicholas Bellinger continue; 1560c66ac9dbSNicholas Bellinger 1561c66ac9dbSNicholas Bellinger atomic_inc(&tmp_tpg->tpg_pr_ref_count); 1562c66ac9dbSNicholas Bellinger smp_mb__after_atomic_inc(); 1563c66ac9dbSNicholas Bellinger spin_unlock(&dev->se_port_lock); 1564c66ac9dbSNicholas Bellinger 1565de103c93SChristoph Hellwig if (core_scsi3_tpg_depend_item(tmp_tpg)) { 15666708bb27SAndy Grover pr_err(" core_scsi3_tpg_depend_item()" 1567c66ac9dbSNicholas Bellinger " for tmp_tpg\n"); 1568c66ac9dbSNicholas Bellinger atomic_dec(&tmp_tpg->tpg_pr_ref_count); 1569c66ac9dbSNicholas Bellinger smp_mb__after_atomic_dec(); 1570de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1571de103c93SChristoph Hellwig goto out_unmap; 1572c66ac9dbSNicholas Bellinger } 1573c66ac9dbSNicholas Bellinger /* 157435d1efe8SMasanari Iida * Locate the destination initiator ACL to be registered 1575c66ac9dbSNicholas Bellinger * from the decoded fabric module specific TransportID 1576c66ac9dbSNicholas Bellinger * at *i_str. 1577c66ac9dbSNicholas Bellinger */ 157828638887SRoland Dreier spin_lock_irq(&tmp_tpg->acl_node_lock); 1579c66ac9dbSNicholas Bellinger dest_node_acl = __core_tpg_get_initiator_node_acl( 1580c66ac9dbSNicholas Bellinger tmp_tpg, i_str); 1581c66ac9dbSNicholas Bellinger if (dest_node_acl) { 1582c66ac9dbSNicholas Bellinger atomic_inc(&dest_node_acl->acl_pr_ref_count); 1583c66ac9dbSNicholas Bellinger smp_mb__after_atomic_inc(); 1584c66ac9dbSNicholas Bellinger } 158528638887SRoland Dreier spin_unlock_irq(&tmp_tpg->acl_node_lock); 1586c66ac9dbSNicholas Bellinger 15876708bb27SAndy Grover if (!dest_node_acl) { 1588c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(tmp_tpg); 1589c66ac9dbSNicholas Bellinger spin_lock(&dev->se_port_lock); 1590c66ac9dbSNicholas Bellinger continue; 1591c66ac9dbSNicholas Bellinger } 1592c66ac9dbSNicholas Bellinger 1593de103c93SChristoph Hellwig if (core_scsi3_nodeacl_depend_item(dest_node_acl)) { 15946708bb27SAndy Grover pr_err("configfs_depend_item() failed" 1595c66ac9dbSNicholas Bellinger " for dest_node_acl->acl_group\n"); 1596c66ac9dbSNicholas Bellinger atomic_dec(&dest_node_acl->acl_pr_ref_count); 1597c66ac9dbSNicholas Bellinger smp_mb__after_atomic_dec(); 1598c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(tmp_tpg); 1599de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1600de103c93SChristoph Hellwig goto out_unmap; 1601c66ac9dbSNicholas Bellinger } 1602c66ac9dbSNicholas Bellinger 1603c66ac9dbSNicholas Bellinger dest_tpg = tmp_tpg; 16046708bb27SAndy Grover pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:" 1605c66ac9dbSNicholas Bellinger " %s Port RTPI: %hu\n", 1606e3d6f909SAndy Grover dest_tpg->se_tpg_tfo->get_fabric_name(), 1607c66ac9dbSNicholas Bellinger dest_node_acl->initiatorname, dest_rtpi); 1608c66ac9dbSNicholas Bellinger 1609c66ac9dbSNicholas Bellinger spin_lock(&dev->se_port_lock); 1610c66ac9dbSNicholas Bellinger break; 1611c66ac9dbSNicholas Bellinger } 1612c66ac9dbSNicholas Bellinger spin_unlock(&dev->se_port_lock); 1613c66ac9dbSNicholas Bellinger 16146708bb27SAndy Grover if (!dest_tpg) { 16156708bb27SAndy Grover pr_err("SPC-3 PR SPEC_I_PT: Unable to locate" 1616c66ac9dbSNicholas Bellinger " dest_tpg\n"); 1617de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 1618de103c93SChristoph Hellwig goto out_unmap; 1619c66ac9dbSNicholas Bellinger } 16208b1e1244SAndy Grover 16216708bb27SAndy Grover pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u" 1622c66ac9dbSNicholas Bellinger " tid_len: %d for %s + %s\n", 1623e3d6f909SAndy Grover dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length, 1624c66ac9dbSNicholas Bellinger tpdl, tid_len, i_str, iport_ptr); 16258b1e1244SAndy Grover 1626c66ac9dbSNicholas Bellinger if (tid_len > tpdl) { 16276708bb27SAndy Grover pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:" 1628c66ac9dbSNicholas Bellinger " %u for Transport ID: %s\n", tid_len, ptr); 1629c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1630c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1631de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 1632de103c93SChristoph Hellwig goto out_unmap; 1633c66ac9dbSNicholas Bellinger } 1634c66ac9dbSNicholas Bellinger /* 1635c66ac9dbSNicholas Bellinger * Locate the desintation struct se_dev_entry pointer for matching 1636c66ac9dbSNicholas Bellinger * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus 1637c66ac9dbSNicholas Bellinger * Target Port. 1638c66ac9dbSNicholas Bellinger */ 1639c66ac9dbSNicholas Bellinger dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, 1640c66ac9dbSNicholas Bellinger dest_rtpi); 16416708bb27SAndy Grover if (!dest_se_deve) { 16426708bb27SAndy Grover pr_err("Unable to locate %s dest_se_deve" 1643c66ac9dbSNicholas Bellinger " from destination RTPI: %hu\n", 1644e3d6f909SAndy Grover dest_tpg->se_tpg_tfo->get_fabric_name(), 1645c66ac9dbSNicholas Bellinger dest_rtpi); 1646c66ac9dbSNicholas Bellinger 1647c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1648c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1649de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 1650de103c93SChristoph Hellwig goto out_unmap; 1651c66ac9dbSNicholas Bellinger } 1652c66ac9dbSNicholas Bellinger 1653de103c93SChristoph Hellwig if (core_scsi3_lunacl_depend_item(dest_se_deve)) { 16546708bb27SAndy Grover pr_err("core_scsi3_lunacl_depend_item()" 1655c66ac9dbSNicholas Bellinger " failed\n"); 1656c66ac9dbSNicholas Bellinger atomic_dec(&dest_se_deve->pr_ref_count); 1657c66ac9dbSNicholas Bellinger smp_mb__after_atomic_dec(); 1658c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1659c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1660de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1661de103c93SChristoph Hellwig goto out_unmap; 1662c66ac9dbSNicholas Bellinger } 16638b1e1244SAndy Grover 16646708bb27SAndy Grover pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s" 1665c66ac9dbSNicholas Bellinger " dest_se_deve mapped_lun: %u\n", 1666e3d6f909SAndy Grover dest_tpg->se_tpg_tfo->get_fabric_name(), 1667c66ac9dbSNicholas Bellinger dest_node_acl->initiatorname, dest_se_deve->mapped_lun); 16688b1e1244SAndy Grover 1669c66ac9dbSNicholas Bellinger /* 1670c66ac9dbSNicholas Bellinger * Skip any TransportIDs that already have a registration for 1671c66ac9dbSNicholas Bellinger * this target port. 1672c66ac9dbSNicholas Bellinger */ 1673c66ac9dbSNicholas Bellinger pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl, 1674c66ac9dbSNicholas Bellinger iport_ptr); 1675c66ac9dbSNicholas Bellinger if (pr_reg_e) { 1676c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_e); 1677c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(dest_se_deve); 1678c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1679c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1680c66ac9dbSNicholas Bellinger ptr += tid_len; 1681c66ac9dbSNicholas Bellinger tpdl -= tid_len; 1682c66ac9dbSNicholas Bellinger tid_len = 0; 1683c66ac9dbSNicholas Bellinger continue; 1684c66ac9dbSNicholas Bellinger } 1685c66ac9dbSNicholas Bellinger /* 1686c66ac9dbSNicholas Bellinger * Allocate a struct pr_transport_id_holder and setup 1687c66ac9dbSNicholas Bellinger * the dest_node_acl and dest_se_deve pointers for the 1688c66ac9dbSNicholas Bellinger * loop below. 1689c66ac9dbSNicholas Bellinger */ 1690c66ac9dbSNicholas Bellinger tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), 1691c66ac9dbSNicholas Bellinger GFP_KERNEL); 16926708bb27SAndy Grover if (!tidh_new) { 16936708bb27SAndy Grover pr_err("Unable to allocate tidh_new\n"); 1694c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(dest_se_deve); 1695c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1696c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1697de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1698de103c93SChristoph Hellwig goto out_unmap; 1699c66ac9dbSNicholas Bellinger } 1700c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&tidh_new->dest_list); 1701c66ac9dbSNicholas Bellinger tidh_new->dest_tpg = dest_tpg; 1702c66ac9dbSNicholas Bellinger tidh_new->dest_node_acl = dest_node_acl; 1703c66ac9dbSNicholas Bellinger tidh_new->dest_se_deve = dest_se_deve; 1704c66ac9dbSNicholas Bellinger 1705c66ac9dbSNicholas Bellinger /* 1706c66ac9dbSNicholas Bellinger * Allocate, but do NOT add the registration for the 1707c66ac9dbSNicholas Bellinger * TransportID referenced SCSI Initiator port. This 1708c66ac9dbSNicholas Bellinger * done because of the following from spc4r17 in section 1709c66ac9dbSNicholas Bellinger * 6.14.3 wrt SPEC_I_PT: 1710c66ac9dbSNicholas Bellinger * 1711c66ac9dbSNicholas Bellinger * "If a registration fails for any initiator port (e.g., if th 1712c66ac9dbSNicholas Bellinger * logical unit does not have enough resources available to 1713c66ac9dbSNicholas Bellinger * hold the registration information), no registrations shall be 1714c66ac9dbSNicholas Bellinger * made, and the command shall be terminated with 1715c66ac9dbSNicholas Bellinger * CHECK CONDITION status." 1716c66ac9dbSNicholas Bellinger * 1717c66ac9dbSNicholas Bellinger * That means we call __core_scsi3_alloc_registration() here, 1718c66ac9dbSNicholas Bellinger * and then call __core_scsi3_add_registration() in the 1719c66ac9dbSNicholas Bellinger * 2nd loop which will never fail. 1720c66ac9dbSNicholas Bellinger */ 17215951146dSAndy Grover dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev, 1722c66ac9dbSNicholas Bellinger dest_node_acl, dest_se_deve, iport_ptr, 1723c66ac9dbSNicholas Bellinger sa_res_key, all_tg_pt, aptpl); 17246708bb27SAndy Grover if (!dest_pr_reg) { 1725c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(dest_se_deve); 1726c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1727c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1728c66ac9dbSNicholas Bellinger kfree(tidh_new); 1729de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 1730de103c93SChristoph Hellwig goto out_unmap; 1731c66ac9dbSNicholas Bellinger } 1732c66ac9dbSNicholas Bellinger tidh_new->dest_pr_reg = dest_pr_reg; 1733c66ac9dbSNicholas Bellinger list_add_tail(&tidh_new->dest_list, &tid_dest_list); 1734c66ac9dbSNicholas Bellinger 1735c66ac9dbSNicholas Bellinger ptr += tid_len; 1736c66ac9dbSNicholas Bellinger tpdl -= tid_len; 1737c66ac9dbSNicholas Bellinger tid_len = 0; 1738c66ac9dbSNicholas Bellinger 1739c66ac9dbSNicholas Bellinger } 174005d1c7c0SAndy Grover 17414949314cSAndy Grover transport_kunmap_data_sg(cmd); 174205d1c7c0SAndy Grover 1743c66ac9dbSNicholas Bellinger /* 1744c66ac9dbSNicholas Bellinger * Go ahead and create a registrations from tid_dest_list for the 1745c66ac9dbSNicholas Bellinger * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl 1746c66ac9dbSNicholas Bellinger * and dest_se_deve. 1747c66ac9dbSNicholas Bellinger * 1748c66ac9dbSNicholas Bellinger * The SA Reservation Key from the PROUT is set for the 1749c66ac9dbSNicholas Bellinger * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1 1750c66ac9dbSNicholas Bellinger * means that the TransportID Initiator port will be 1751c66ac9dbSNicholas Bellinger * registered on all of the target ports in the SCSI target device 1752c66ac9dbSNicholas Bellinger * ALL_TG_PT=0 means the registration will only be for the 1753c66ac9dbSNicholas Bellinger * SCSI target port the PROUT REGISTER with SPEC_I_PT=1 1754c66ac9dbSNicholas Bellinger * was received. 1755c66ac9dbSNicholas Bellinger */ 1756c66ac9dbSNicholas Bellinger list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) { 1757c66ac9dbSNicholas Bellinger dest_tpg = tidh->dest_tpg; 1758c66ac9dbSNicholas Bellinger dest_node_acl = tidh->dest_node_acl; 1759c66ac9dbSNicholas Bellinger dest_se_deve = tidh->dest_se_deve; 1760c66ac9dbSNicholas Bellinger dest_pr_reg = tidh->dest_pr_reg; 1761c66ac9dbSNicholas Bellinger dest_local_nexus = tidh->dest_local_nexus; 1762c66ac9dbSNicholas Bellinger 1763c66ac9dbSNicholas Bellinger list_del(&tidh->dest_list); 1764c66ac9dbSNicholas Bellinger kfree(tidh); 1765c66ac9dbSNicholas Bellinger 1766c66ac9dbSNicholas Bellinger memset(i_buf, 0, PR_REG_ISID_ID_LEN); 1767c66ac9dbSNicholas Bellinger prf_isid = core_pr_dump_initiator_port(dest_pr_reg, &i_buf[0], 1768c66ac9dbSNicholas Bellinger PR_REG_ISID_ID_LEN); 1769c66ac9dbSNicholas Bellinger 17705951146dSAndy Grover __core_scsi3_add_registration(cmd->se_dev, dest_node_acl, 1771c66ac9dbSNicholas Bellinger dest_pr_reg, 0, 0); 1772c66ac9dbSNicholas Bellinger 17736708bb27SAndy Grover pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully" 1774c66ac9dbSNicholas Bellinger " registered Transport ID for Node: %s%s Mapped LUN:" 1775e3d6f909SAndy Grover " %u\n", dest_tpg->se_tpg_tfo->get_fabric_name(), 1776c66ac9dbSNicholas Bellinger dest_node_acl->initiatorname, (prf_isid) ? 1777c66ac9dbSNicholas Bellinger &i_buf[0] : "", dest_se_deve->mapped_lun); 1778c66ac9dbSNicholas Bellinger 1779c66ac9dbSNicholas Bellinger if (dest_local_nexus) 1780c66ac9dbSNicholas Bellinger continue; 1781c66ac9dbSNicholas Bellinger 1782c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(dest_se_deve); 1783c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1784c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1785c66ac9dbSNicholas Bellinger } 1786c66ac9dbSNicholas Bellinger 1787c66ac9dbSNicholas Bellinger return 0; 1788de103c93SChristoph Hellwig out_unmap: 17894949314cSAndy Grover transport_kunmap_data_sg(cmd); 1790de103c93SChristoph Hellwig out: 1791c66ac9dbSNicholas Bellinger /* 1792c66ac9dbSNicholas Bellinger * For the failure case, release everything from tid_dest_list 1793c66ac9dbSNicholas Bellinger * including *dest_pr_reg and the configfs dependances.. 1794c66ac9dbSNicholas Bellinger */ 1795c66ac9dbSNicholas Bellinger list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) { 1796c66ac9dbSNicholas Bellinger dest_tpg = tidh->dest_tpg; 1797c66ac9dbSNicholas Bellinger dest_node_acl = tidh->dest_node_acl; 1798c66ac9dbSNicholas Bellinger dest_se_deve = tidh->dest_se_deve; 1799c66ac9dbSNicholas Bellinger dest_pr_reg = tidh->dest_pr_reg; 1800c66ac9dbSNicholas Bellinger dest_local_nexus = tidh->dest_local_nexus; 1801c66ac9dbSNicholas Bellinger 1802c66ac9dbSNicholas Bellinger list_del(&tidh->dest_list); 1803c66ac9dbSNicholas Bellinger kfree(tidh); 1804c66ac9dbSNicholas Bellinger /* 1805c66ac9dbSNicholas Bellinger * Release any extra ALL_TG_PT=1 registrations for 1806c66ac9dbSNicholas Bellinger * the SPEC_I_PT=1 case. 1807c66ac9dbSNicholas Bellinger */ 1808c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe, 1809c66ac9dbSNicholas Bellinger &dest_pr_reg->pr_reg_atp_list, 1810c66ac9dbSNicholas Bellinger pr_reg_atp_mem_list) { 1811c66ac9dbSNicholas Bellinger list_del(&pr_reg_tmp->pr_reg_atp_mem_list); 1812c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve); 1813c66ac9dbSNicholas Bellinger kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp); 1814c66ac9dbSNicholas Bellinger } 1815c66ac9dbSNicholas Bellinger 1816c66ac9dbSNicholas Bellinger kfree(dest_pr_reg->pr_aptpl_buf); 1817c66ac9dbSNicholas Bellinger kmem_cache_free(t10_pr_reg_cache, dest_pr_reg); 1818c66ac9dbSNicholas Bellinger 1819c66ac9dbSNicholas Bellinger if (dest_local_nexus) 1820c66ac9dbSNicholas Bellinger continue; 1821c66ac9dbSNicholas Bellinger 1822c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(dest_se_deve); 1823c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 1824c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_tpg); 1825c66ac9dbSNicholas Bellinger } 1826c66ac9dbSNicholas Bellinger return ret; 1827c66ac9dbSNicholas Bellinger } 1828c66ac9dbSNicholas Bellinger 1829c66ac9dbSNicholas Bellinger /* 1830c66ac9dbSNicholas Bellinger * Called with struct se_device->dev_reservation_lock held 1831c66ac9dbSNicholas Bellinger */ 1832c66ac9dbSNicholas Bellinger static int __core_scsi3_update_aptpl_buf( 1833c66ac9dbSNicholas Bellinger struct se_device *dev, 1834c66ac9dbSNicholas Bellinger unsigned char *buf, 1835c66ac9dbSNicholas Bellinger u32 pr_aptpl_buf_len, 1836c66ac9dbSNicholas Bellinger int clear_aptpl_metadata) 1837c66ac9dbSNicholas Bellinger { 1838c66ac9dbSNicholas Bellinger struct se_lun *lun; 1839c66ac9dbSNicholas Bellinger struct se_portal_group *tpg; 1840c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg; 1841c66ac9dbSNicholas Bellinger unsigned char tmp[512], isid_buf[32]; 1842c66ac9dbSNicholas Bellinger ssize_t len = 0; 1843c66ac9dbSNicholas Bellinger int reg_count = 0; 1844c66ac9dbSNicholas Bellinger 1845c66ac9dbSNicholas Bellinger memset(buf, 0, pr_aptpl_buf_len); 1846c66ac9dbSNicholas Bellinger /* 1847c66ac9dbSNicholas Bellinger * Called to clear metadata once APTPL has been deactivated. 1848c66ac9dbSNicholas Bellinger */ 1849c66ac9dbSNicholas Bellinger if (clear_aptpl_metadata) { 1850c66ac9dbSNicholas Bellinger snprintf(buf, pr_aptpl_buf_len, 1851c66ac9dbSNicholas Bellinger "No Registrations or Reservations\n"); 1852c66ac9dbSNicholas Bellinger return 0; 1853c66ac9dbSNicholas Bellinger } 1854c66ac9dbSNicholas Bellinger /* 1855c66ac9dbSNicholas Bellinger * Walk the registration list.. 1856c66ac9dbSNicholas Bellinger */ 18570fd97ccfSChristoph Hellwig spin_lock(&dev->t10_pr.registration_lock); 18580fd97ccfSChristoph Hellwig list_for_each_entry(pr_reg, &dev->t10_pr.registration_list, 1859c66ac9dbSNicholas Bellinger pr_reg_list) { 1860c66ac9dbSNicholas Bellinger 1861c66ac9dbSNicholas Bellinger tmp[0] = '\0'; 1862c66ac9dbSNicholas Bellinger isid_buf[0] = '\0'; 1863c66ac9dbSNicholas Bellinger tpg = pr_reg->pr_reg_nacl->se_tpg; 1864c66ac9dbSNicholas Bellinger lun = pr_reg->pr_reg_tg_pt_lun; 1865c66ac9dbSNicholas Bellinger /* 1866c66ac9dbSNicholas Bellinger * Write out any ISID value to APTPL metadata that was included 1867c66ac9dbSNicholas Bellinger * in the original registration. 1868c66ac9dbSNicholas Bellinger */ 1869c66ac9dbSNicholas Bellinger if (pr_reg->isid_present_at_reg) 1870c66ac9dbSNicholas Bellinger snprintf(isid_buf, 32, "initiator_sid=%s\n", 1871c66ac9dbSNicholas Bellinger pr_reg->pr_reg_isid); 1872c66ac9dbSNicholas Bellinger /* 1873c66ac9dbSNicholas Bellinger * Include special metadata if the pr_reg matches the 1874c66ac9dbSNicholas Bellinger * reservation holder. 1875c66ac9dbSNicholas Bellinger */ 1876c66ac9dbSNicholas Bellinger if (dev->dev_pr_res_holder == pr_reg) { 1877c66ac9dbSNicholas Bellinger snprintf(tmp, 512, "PR_REG_START: %d" 1878c66ac9dbSNicholas Bellinger "\ninitiator_fabric=%s\n" 1879c66ac9dbSNicholas Bellinger "initiator_node=%s\n%s" 1880c66ac9dbSNicholas Bellinger "sa_res_key=%llu\n" 1881c66ac9dbSNicholas Bellinger "res_holder=1\nres_type=%02x\n" 1882c66ac9dbSNicholas Bellinger "res_scope=%02x\nres_all_tg_pt=%d\n" 1883c66ac9dbSNicholas Bellinger "mapped_lun=%u\n", reg_count, 1884e3d6f909SAndy Grover tpg->se_tpg_tfo->get_fabric_name(), 1885c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl->initiatorname, isid_buf, 1886c66ac9dbSNicholas Bellinger pr_reg->pr_res_key, pr_reg->pr_res_type, 1887c66ac9dbSNicholas Bellinger pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt, 1888c66ac9dbSNicholas Bellinger pr_reg->pr_res_mapped_lun); 1889c66ac9dbSNicholas Bellinger } else { 1890c66ac9dbSNicholas Bellinger snprintf(tmp, 512, "PR_REG_START: %d\n" 1891c66ac9dbSNicholas Bellinger "initiator_fabric=%s\ninitiator_node=%s\n%s" 1892c66ac9dbSNicholas Bellinger "sa_res_key=%llu\nres_holder=0\n" 1893c66ac9dbSNicholas Bellinger "res_all_tg_pt=%d\nmapped_lun=%u\n", 1894e3d6f909SAndy Grover reg_count, tpg->se_tpg_tfo->get_fabric_name(), 1895c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl->initiatorname, isid_buf, 1896c66ac9dbSNicholas Bellinger pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt, 1897c66ac9dbSNicholas Bellinger pr_reg->pr_res_mapped_lun); 1898c66ac9dbSNicholas Bellinger } 1899c66ac9dbSNicholas Bellinger 190060d645a4SDan Carpenter if ((len + strlen(tmp) >= pr_aptpl_buf_len)) { 19016708bb27SAndy Grover pr_err("Unable to update renaming" 1902c66ac9dbSNicholas Bellinger " APTPL metadata\n"); 19030fd97ccfSChristoph Hellwig spin_unlock(&dev->t10_pr.registration_lock); 1904e3d6f909SAndy Grover return -EMSGSIZE; 1905c66ac9dbSNicholas Bellinger } 1906c66ac9dbSNicholas Bellinger len += sprintf(buf+len, "%s", tmp); 1907c66ac9dbSNicholas Bellinger 1908c66ac9dbSNicholas Bellinger /* 1909c66ac9dbSNicholas Bellinger * Include information about the associated SCSI target port. 1910c66ac9dbSNicholas Bellinger */ 1911c66ac9dbSNicholas Bellinger snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n" 1912c66ac9dbSNicholas Bellinger "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:" 1913e3d6f909SAndy Grover " %d\n", tpg->se_tpg_tfo->get_fabric_name(), 1914e3d6f909SAndy Grover tpg->se_tpg_tfo->tpg_get_wwn(tpg), 1915e3d6f909SAndy Grover tpg->se_tpg_tfo->tpg_get_tag(tpg), 1916c66ac9dbSNicholas Bellinger lun->lun_sep->sep_rtpi, lun->unpacked_lun, reg_count); 1917c66ac9dbSNicholas Bellinger 191860d645a4SDan Carpenter if ((len + strlen(tmp) >= pr_aptpl_buf_len)) { 19196708bb27SAndy Grover pr_err("Unable to update renaming" 1920c66ac9dbSNicholas Bellinger " APTPL metadata\n"); 19210fd97ccfSChristoph Hellwig spin_unlock(&dev->t10_pr.registration_lock); 1922e3d6f909SAndy Grover return -EMSGSIZE; 1923c66ac9dbSNicholas Bellinger } 1924c66ac9dbSNicholas Bellinger len += sprintf(buf+len, "%s", tmp); 1925c66ac9dbSNicholas Bellinger reg_count++; 1926c66ac9dbSNicholas Bellinger } 19270fd97ccfSChristoph Hellwig spin_unlock(&dev->t10_pr.registration_lock); 1928c66ac9dbSNicholas Bellinger 19296708bb27SAndy Grover if (!reg_count) 1930c66ac9dbSNicholas Bellinger len += sprintf(buf+len, "No Registrations or Reservations"); 1931c66ac9dbSNicholas Bellinger 1932c66ac9dbSNicholas Bellinger return 0; 1933c66ac9dbSNicholas Bellinger } 1934c66ac9dbSNicholas Bellinger 1935c66ac9dbSNicholas Bellinger static int core_scsi3_update_aptpl_buf( 1936c66ac9dbSNicholas Bellinger struct se_device *dev, 1937c66ac9dbSNicholas Bellinger unsigned char *buf, 1938c66ac9dbSNicholas Bellinger u32 pr_aptpl_buf_len, 1939c66ac9dbSNicholas Bellinger int clear_aptpl_metadata) 1940c66ac9dbSNicholas Bellinger { 1941c66ac9dbSNicholas Bellinger int ret; 1942c66ac9dbSNicholas Bellinger 1943c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 1944c66ac9dbSNicholas Bellinger ret = __core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len, 1945c66ac9dbSNicholas Bellinger clear_aptpl_metadata); 1946c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 1947c66ac9dbSNicholas Bellinger 1948c66ac9dbSNicholas Bellinger return ret; 1949c66ac9dbSNicholas Bellinger } 1950c66ac9dbSNicholas Bellinger 1951c66ac9dbSNicholas Bellinger /* 1952c66ac9dbSNicholas Bellinger * Called with struct se_device->aptpl_file_mutex held 1953c66ac9dbSNicholas Bellinger */ 1954c66ac9dbSNicholas Bellinger static int __core_scsi3_write_aptpl_to_file( 1955c66ac9dbSNicholas Bellinger struct se_device *dev, 1956c66ac9dbSNicholas Bellinger unsigned char *buf, 1957c66ac9dbSNicholas Bellinger u32 pr_aptpl_buf_len) 1958c66ac9dbSNicholas Bellinger { 19590fd97ccfSChristoph Hellwig struct t10_wwn *wwn = &dev->t10_wwn; 1960c66ac9dbSNicholas Bellinger struct file *file; 1961c66ac9dbSNicholas Bellinger int flags = O_RDWR | O_CREAT | O_TRUNC; 1962c66ac9dbSNicholas Bellinger char path[512]; 1963c66ac9dbSNicholas Bellinger int ret; 1964c66ac9dbSNicholas Bellinger 1965c66ac9dbSNicholas Bellinger memset(path, 0, 512); 1966c66ac9dbSNicholas Bellinger 196760d645a4SDan Carpenter if (strlen(&wwn->unit_serial[0]) >= 512) { 19686708bb27SAndy Grover pr_err("WWN value for struct se_device does not fit" 1969c66ac9dbSNicholas Bellinger " into path buffer\n"); 1970e3d6f909SAndy Grover return -EMSGSIZE; 1971c66ac9dbSNicholas Bellinger } 1972c66ac9dbSNicholas Bellinger 1973c66ac9dbSNicholas Bellinger snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]); 1974c66ac9dbSNicholas Bellinger file = filp_open(path, flags, 0600); 19750e9b10a9SAl Viro if (IS_ERR(file)) { 19766708bb27SAndy Grover pr_err("filp_open(%s) for APTPL metadata" 1977c66ac9dbSNicholas Bellinger " failed\n", path); 19780e9b10a9SAl Viro return PTR_ERR(file); 1979c66ac9dbSNicholas Bellinger } 1980c66ac9dbSNicholas Bellinger 19816708bb27SAndy Grover if (!pr_aptpl_buf_len) 19820e9b10a9SAl Viro pr_aptpl_buf_len = (strlen(&buf[0]) + 1); /* Add extra for NULL */ 1983c66ac9dbSNicholas Bellinger 19840e9b10a9SAl Viro ret = kernel_write(file, buf, pr_aptpl_buf_len, 0); 1985c66ac9dbSNicholas Bellinger 19860e9b10a9SAl Viro if (ret < 0) 19876708bb27SAndy Grover pr_debug("Error writing APTPL metadata file: %s\n", path); 19880e9b10a9SAl Viro fput(file); 1989c66ac9dbSNicholas Bellinger 19900e9b10a9SAl Viro return ret ? -EIO : 0; 1991c66ac9dbSNicholas Bellinger } 1992c66ac9dbSNicholas Bellinger 1993de103c93SChristoph Hellwig static int 1994de103c93SChristoph Hellwig core_scsi3_update_and_write_aptpl(struct se_device *dev, unsigned char *in_buf, 1995c66ac9dbSNicholas Bellinger u32 in_pr_aptpl_buf_len) 1996c66ac9dbSNicholas Bellinger { 1997c66ac9dbSNicholas Bellinger unsigned char null_buf[64], *buf; 1998c66ac9dbSNicholas Bellinger u32 pr_aptpl_buf_len; 1999de103c93SChristoph Hellwig int clear_aptpl_metadata = 0; 2000de103c93SChristoph Hellwig int ret; 2001de103c93SChristoph Hellwig 2002c66ac9dbSNicholas Bellinger /* 2003c66ac9dbSNicholas Bellinger * Can be called with a NULL pointer from PROUT service action CLEAR 2004c66ac9dbSNicholas Bellinger */ 20056708bb27SAndy Grover if (!in_buf) { 2006c66ac9dbSNicholas Bellinger memset(null_buf, 0, 64); 2007c66ac9dbSNicholas Bellinger buf = &null_buf[0]; 2008c66ac9dbSNicholas Bellinger /* 2009c66ac9dbSNicholas Bellinger * This will clear the APTPL metadata to: 2010c66ac9dbSNicholas Bellinger * "No Registrations or Reservations" status 2011c66ac9dbSNicholas Bellinger */ 2012c66ac9dbSNicholas Bellinger pr_aptpl_buf_len = 64; 2013c66ac9dbSNicholas Bellinger clear_aptpl_metadata = 1; 2014c66ac9dbSNicholas Bellinger } else { 2015c66ac9dbSNicholas Bellinger buf = in_buf; 2016c66ac9dbSNicholas Bellinger pr_aptpl_buf_len = in_pr_aptpl_buf_len; 2017c66ac9dbSNicholas Bellinger } 2018c66ac9dbSNicholas Bellinger 2019c66ac9dbSNicholas Bellinger ret = core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len, 2020c66ac9dbSNicholas Bellinger clear_aptpl_metadata); 2021c66ac9dbSNicholas Bellinger if (ret != 0) 2022e3d6f909SAndy Grover return ret; 2023de103c93SChristoph Hellwig 2024c66ac9dbSNicholas Bellinger /* 2025c66ac9dbSNicholas Bellinger * __core_scsi3_write_aptpl_to_file() will call strlen() 2026c66ac9dbSNicholas Bellinger * on the passed buf to determine pr_aptpl_buf_len. 2027c66ac9dbSNicholas Bellinger */ 2028de103c93SChristoph Hellwig return __core_scsi3_write_aptpl_to_file(dev, buf, 0); 2029c66ac9dbSNicholas Bellinger } 2030c66ac9dbSNicholas Bellinger 2031de103c93SChristoph Hellwig static sense_reason_t 2032de103c93SChristoph Hellwig core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key, 2033de103c93SChristoph Hellwig int aptpl, int all_tg_pt, int spec_i_pt, int ignore_key) 2034c66ac9dbSNicholas Bellinger { 2035e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 20365951146dSAndy Grover struct se_device *dev = cmd->se_dev; 2037c66ac9dbSNicholas Bellinger struct se_dev_entry *se_deve; 2038e3d6f909SAndy Grover struct se_lun *se_lun = cmd->se_lun; 2039c66ac9dbSNicholas Bellinger struct se_portal_group *se_tpg; 2040c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp, *pr_reg_e; 20410fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 2042c66ac9dbSNicholas Bellinger /* Used for APTPL metadata w/ UNREGISTER */ 2043c66ac9dbSNicholas Bellinger unsigned char *pr_aptpl_buf = NULL; 2044c66ac9dbSNicholas Bellinger unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL; 2045a0d50f62SHannes Reinecke sense_reason_t ret = TCM_NO_SENSE; 2046de103c93SChristoph Hellwig int pr_holder = 0, type; 2047c66ac9dbSNicholas Bellinger 20486708bb27SAndy Grover if (!se_sess || !se_lun) { 20496708bb27SAndy Grover pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n"); 2050de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2051c66ac9dbSNicholas Bellinger } 2052c66ac9dbSNicholas Bellinger se_tpg = se_sess->se_tpg; 2053f2083241SJörn Engel se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun]; 2054c66ac9dbSNicholas Bellinger 2055e3d6f909SAndy Grover if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) { 2056c66ac9dbSNicholas Bellinger memset(&isid_buf[0], 0, PR_REG_ISID_LEN); 2057e3d6f909SAndy Grover se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0], 2058c66ac9dbSNicholas Bellinger PR_REG_ISID_LEN); 2059c66ac9dbSNicholas Bellinger isid_ptr = &isid_buf[0]; 2060c66ac9dbSNicholas Bellinger } 2061c66ac9dbSNicholas Bellinger /* 2062c66ac9dbSNicholas Bellinger * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47 2063c66ac9dbSNicholas Bellinger */ 2064c66ac9dbSNicholas Bellinger pr_reg_e = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess); 20656708bb27SAndy Grover if (!pr_reg_e) { 2066c66ac9dbSNicholas Bellinger if (res_key) { 20676708bb27SAndy Grover pr_warn("SPC-3 PR: Reservation Key non-zero" 2068c66ac9dbSNicholas Bellinger " for SA REGISTER, returning CONFLICT\n"); 2069de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 2070c66ac9dbSNicholas Bellinger } 2071c66ac9dbSNicholas Bellinger /* 2072c66ac9dbSNicholas Bellinger * Do nothing but return GOOD status. 2073c66ac9dbSNicholas Bellinger */ 20746708bb27SAndy Grover if (!sa_res_key) 207503e98c9eSNicholas Bellinger return 0; 2076c66ac9dbSNicholas Bellinger 20776708bb27SAndy Grover if (!spec_i_pt) { 2078c66ac9dbSNicholas Bellinger /* 2079c66ac9dbSNicholas Bellinger * Perform the Service Action REGISTER on the Initiator 2080c66ac9dbSNicholas Bellinger * Port Endpoint that the PRO was received from on the 2081c66ac9dbSNicholas Bellinger * Logical Unit of the SCSI device server. 2082c66ac9dbSNicholas Bellinger */ 2083de103c93SChristoph Hellwig if (core_scsi3_alloc_registration(cmd->se_dev, 2084c66ac9dbSNicholas Bellinger se_sess->se_node_acl, se_deve, isid_ptr, 2085c66ac9dbSNicholas Bellinger sa_res_key, all_tg_pt, aptpl, 2086de103c93SChristoph Hellwig ignore_key, 0)) { 20876708bb27SAndy Grover pr_err("Unable to allocate" 2088c66ac9dbSNicholas Bellinger " struct t10_pr_registration\n"); 2089de103c93SChristoph Hellwig return TCM_INVALID_PARAMETER_LIST; 2090c66ac9dbSNicholas Bellinger } 2091c66ac9dbSNicholas Bellinger } else { 2092c66ac9dbSNicholas Bellinger /* 2093c66ac9dbSNicholas Bellinger * Register both the Initiator port that received 2094c66ac9dbSNicholas Bellinger * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI 2095c66ac9dbSNicholas Bellinger * TransportID from Parameter list and loop through 2096c66ac9dbSNicholas Bellinger * fabric dependent parameter list while calling 2097c66ac9dbSNicholas Bellinger * logic from of core_scsi3_alloc_registration() for 2098c66ac9dbSNicholas Bellinger * each TransportID provided SCSI Initiator Port/Device 2099c66ac9dbSNicholas Bellinger */ 2100c66ac9dbSNicholas Bellinger ret = core_scsi3_decode_spec_i_port(cmd, se_tpg, 2101c66ac9dbSNicholas Bellinger isid_ptr, sa_res_key, all_tg_pt, aptpl); 2102c66ac9dbSNicholas Bellinger if (ret != 0) 2103c66ac9dbSNicholas Bellinger return ret; 2104c66ac9dbSNicholas Bellinger } 2105c66ac9dbSNicholas Bellinger /* 2106c66ac9dbSNicholas Bellinger * Nothing left to do for the APTPL=0 case. 2107c66ac9dbSNicholas Bellinger */ 21086708bb27SAndy Grover if (!aptpl) { 2109c66ac9dbSNicholas Bellinger pr_tmpl->pr_aptpl_active = 0; 21105951146dSAndy Grover core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0); 21116708bb27SAndy Grover pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for" 2112c66ac9dbSNicholas Bellinger " REGISTER\n"); 2113c66ac9dbSNicholas Bellinger return 0; 2114c66ac9dbSNicholas Bellinger } 2115c66ac9dbSNicholas Bellinger /* 2116c66ac9dbSNicholas Bellinger * Locate the newly allocated local I_T Nexus *pr_reg, and 2117c66ac9dbSNicholas Bellinger * update the APTPL metadata information using its 2118c66ac9dbSNicholas Bellinger * preallocated *pr_reg->pr_aptpl_buf. 2119c66ac9dbSNicholas Bellinger */ 21205951146dSAndy Grover pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, 2121c66ac9dbSNicholas Bellinger se_sess->se_node_acl, se_sess); 2122c66ac9dbSNicholas Bellinger 2123de103c93SChristoph Hellwig if (core_scsi3_update_and_write_aptpl(cmd->se_dev, 2124c66ac9dbSNicholas Bellinger &pr_reg->pr_aptpl_buf[0], 2125de103c93SChristoph Hellwig pr_tmpl->pr_aptpl_buf_len)) { 2126c66ac9dbSNicholas Bellinger pr_tmpl->pr_aptpl_active = 1; 21276708bb27SAndy Grover pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n"); 2128c66ac9dbSNicholas Bellinger } 2129c66ac9dbSNicholas Bellinger 2130de103c93SChristoph Hellwig goto out_put_pr_reg; 2131de103c93SChristoph Hellwig } 2132de103c93SChristoph Hellwig 2133c66ac9dbSNicholas Bellinger /* 2134c66ac9dbSNicholas Bellinger * Locate the existing *pr_reg via struct se_node_acl pointers 2135c66ac9dbSNicholas Bellinger */ 2136c66ac9dbSNicholas Bellinger pr_reg = pr_reg_e; 2137c66ac9dbSNicholas Bellinger type = pr_reg->pr_res_type; 2138c66ac9dbSNicholas Bellinger 21396708bb27SAndy Grover if (!ignore_key) { 2140c66ac9dbSNicholas Bellinger if (res_key != pr_reg->pr_res_key) { 21416708bb27SAndy Grover pr_err("SPC-3 PR REGISTER: Received" 2142c66ac9dbSNicholas Bellinger " res_key: 0x%016Lx does not match" 2143c66ac9dbSNicholas Bellinger " existing SA REGISTER res_key:" 2144c66ac9dbSNicholas Bellinger " 0x%016Lx\n", res_key, 2145c66ac9dbSNicholas Bellinger pr_reg->pr_res_key); 2146de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 2147de103c93SChristoph Hellwig goto out_put_pr_reg; 2148c66ac9dbSNicholas Bellinger } 2149c66ac9dbSNicholas Bellinger } 2150de103c93SChristoph Hellwig 2151c66ac9dbSNicholas Bellinger if (spec_i_pt) { 21526708bb27SAndy Grover pr_err("SPC-3 PR UNREGISTER: SPEC_I_PT" 2153c66ac9dbSNicholas Bellinger " set while sa_res_key=0\n"); 2154de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 2155de103c93SChristoph Hellwig goto out_put_pr_reg; 2156c66ac9dbSNicholas Bellinger } 2157de103c93SChristoph Hellwig 2158c66ac9dbSNicholas Bellinger /* 2159c66ac9dbSNicholas Bellinger * An existing ALL_TG_PT=1 registration being released 2160c66ac9dbSNicholas Bellinger * must also set ALL_TG_PT=1 in the incoming PROUT. 2161c66ac9dbSNicholas Bellinger */ 2162c66ac9dbSNicholas Bellinger if (pr_reg->pr_reg_all_tg_pt && !(all_tg_pt)) { 21636708bb27SAndy Grover pr_err("SPC-3 PR UNREGISTER: ALL_TG_PT=1" 2164c66ac9dbSNicholas Bellinger " registration exists, but ALL_TG_PT=1 bit not" 2165c66ac9dbSNicholas Bellinger " present in received PROUT\n"); 2166de103c93SChristoph Hellwig ret = TCM_INVALID_CDB_FIELD; 2167de103c93SChristoph Hellwig goto out_put_pr_reg; 2168c66ac9dbSNicholas Bellinger } 2169de103c93SChristoph Hellwig 2170c66ac9dbSNicholas Bellinger /* 2171c66ac9dbSNicholas Bellinger * Allocate APTPL metadata buffer used for UNREGISTER ops 2172c66ac9dbSNicholas Bellinger */ 2173c66ac9dbSNicholas Bellinger if (aptpl) { 2174c66ac9dbSNicholas Bellinger pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len, 2175c66ac9dbSNicholas Bellinger GFP_KERNEL); 21766708bb27SAndy Grover if (!pr_aptpl_buf) { 21776708bb27SAndy Grover pr_err("Unable to allocate" 2178c66ac9dbSNicholas Bellinger " pr_aptpl_buf\n"); 2179de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2180de103c93SChristoph Hellwig goto out_put_pr_reg; 2181c66ac9dbSNicholas Bellinger } 2182c66ac9dbSNicholas Bellinger } 2183de103c93SChristoph Hellwig 2184c66ac9dbSNicholas Bellinger /* 2185c66ac9dbSNicholas Bellinger * sa_res_key=0 Unregister Reservation Key for registered I_T 2186c66ac9dbSNicholas Bellinger * Nexus sa_res_key=1 Change Reservation Key for registered I_T 2187c66ac9dbSNicholas Bellinger * Nexus. 2188c66ac9dbSNicholas Bellinger */ 21896708bb27SAndy Grover if (!sa_res_key) { 2190c66ac9dbSNicholas Bellinger pr_holder = core_scsi3_check_implict_release( 21915951146dSAndy Grover cmd->se_dev, pr_reg); 2192c66ac9dbSNicholas Bellinger if (pr_holder < 0) { 2193c66ac9dbSNicholas Bellinger kfree(pr_aptpl_buf); 2194de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 2195de103c93SChristoph Hellwig goto out_put_pr_reg; 2196c66ac9dbSNicholas Bellinger } 2197c66ac9dbSNicholas Bellinger 2198c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 2199c66ac9dbSNicholas Bellinger /* 2200c66ac9dbSNicholas Bellinger * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port 2201c66ac9dbSNicholas Bellinger * and matching pr_res_key. 2202c66ac9dbSNicholas Bellinger */ 2203c66ac9dbSNicholas Bellinger if (pr_reg->pr_reg_all_tg_pt) { 2204c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg_p, pr_reg_tmp, 2205c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, 2206c66ac9dbSNicholas Bellinger pr_reg_list) { 2207c66ac9dbSNicholas Bellinger 22086708bb27SAndy Grover if (!pr_reg_p->pr_reg_all_tg_pt) 2209c66ac9dbSNicholas Bellinger continue; 2210c66ac9dbSNicholas Bellinger if (pr_reg_p->pr_res_key != res_key) 2211c66ac9dbSNicholas Bellinger continue; 2212c66ac9dbSNicholas Bellinger if (pr_reg == pr_reg_p) 2213c66ac9dbSNicholas Bellinger continue; 2214c66ac9dbSNicholas Bellinger if (strcmp(pr_reg->pr_reg_nacl->initiatorname, 2215c66ac9dbSNicholas Bellinger pr_reg_p->pr_reg_nacl->initiatorname)) 2216c66ac9dbSNicholas Bellinger continue; 2217c66ac9dbSNicholas Bellinger 2218c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, 2219c66ac9dbSNicholas Bellinger pr_reg_p, NULL, 0); 2220c66ac9dbSNicholas Bellinger } 2221c66ac9dbSNicholas Bellinger } 2222de103c93SChristoph Hellwig 2223c66ac9dbSNicholas Bellinger /* 2224c66ac9dbSNicholas Bellinger * Release the calling I_T Nexus registration now.. 2225c66ac9dbSNicholas Bellinger */ 2226de103c93SChristoph Hellwig __core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1); 2227de103c93SChristoph Hellwig 2228c66ac9dbSNicholas Bellinger /* 2229c66ac9dbSNicholas Bellinger * From spc4r17, section 5.7.11.3 Unregistering 2230c66ac9dbSNicholas Bellinger * 2231c66ac9dbSNicholas Bellinger * If the persistent reservation is a registrants only 2232c66ac9dbSNicholas Bellinger * type, the device server shall establish a unit 2233c66ac9dbSNicholas Bellinger * attention condition for the initiator port associated 2234c66ac9dbSNicholas Bellinger * with every registered I_T nexus except for the I_T 2235c66ac9dbSNicholas Bellinger * nexus on which the PERSISTENT RESERVE OUT command was 2236c66ac9dbSNicholas Bellinger * received, with the additional sense code set to 2237c66ac9dbSNicholas Bellinger * RESERVATIONS RELEASED. 2238c66ac9dbSNicholas Bellinger */ 2239c66ac9dbSNicholas Bellinger if (pr_holder && 2240de103c93SChristoph Hellwig (type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY || 2241de103c93SChristoph Hellwig type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) { 2242c66ac9dbSNicholas Bellinger list_for_each_entry(pr_reg_p, 2243c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, 2244c66ac9dbSNicholas Bellinger pr_reg_list) { 2245c66ac9dbSNicholas Bellinger 2246c66ac9dbSNicholas Bellinger core_scsi3_ua_allocate( 2247c66ac9dbSNicholas Bellinger pr_reg_p->pr_reg_nacl, 2248c66ac9dbSNicholas Bellinger pr_reg_p->pr_res_mapped_lun, 2249c66ac9dbSNicholas Bellinger 0x2A, 2250c66ac9dbSNicholas Bellinger ASCQ_2AH_RESERVATIONS_RELEASED); 2251c66ac9dbSNicholas Bellinger } 2252c66ac9dbSNicholas Bellinger } 2253c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 2254c66ac9dbSNicholas Bellinger 22556708bb27SAndy Grover if (!aptpl) { 2256c66ac9dbSNicholas Bellinger pr_tmpl->pr_aptpl_active = 0; 2257c66ac9dbSNicholas Bellinger core_scsi3_update_and_write_aptpl(dev, NULL, 0); 22586708bb27SAndy Grover pr_debug("SPC-3 PR: Set APTPL Bit Deactivated" 2259c66ac9dbSNicholas Bellinger " for UNREGISTER\n"); 2260c66ac9dbSNicholas Bellinger return 0; 2261c66ac9dbSNicholas Bellinger } 2262c66ac9dbSNicholas Bellinger 2263de103c93SChristoph Hellwig if (!core_scsi3_update_and_write_aptpl(dev, &pr_aptpl_buf[0], 2264de103c93SChristoph Hellwig pr_tmpl->pr_aptpl_buf_len)) { 2265c66ac9dbSNicholas Bellinger pr_tmpl->pr_aptpl_active = 1; 22666708bb27SAndy Grover pr_debug("SPC-3 PR: Set APTPL Bit Activated" 2267c66ac9dbSNicholas Bellinger " for UNREGISTER\n"); 2268c66ac9dbSNicholas Bellinger } 2269c66ac9dbSNicholas Bellinger 2270de103c93SChristoph Hellwig goto out_free_aptpl_buf; 2271de103c93SChristoph Hellwig } 2272de103c93SChristoph Hellwig 2273c66ac9dbSNicholas Bellinger /* 2274c66ac9dbSNicholas Bellinger * Increment PRgeneration counter for struct se_device" 2275c66ac9dbSNicholas Bellinger * upon a successful REGISTER, see spc4r17 section 6.3.2 2276c66ac9dbSNicholas Bellinger * READ_KEYS service action. 2277c66ac9dbSNicholas Bellinger */ 2278de103c93SChristoph Hellwig pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev); 2279c66ac9dbSNicholas Bellinger pr_reg->pr_res_key = sa_res_key; 22806708bb27SAndy Grover pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation" 2281c66ac9dbSNicholas Bellinger " Key for %s to: 0x%016Lx PRgeneration:" 2282e3d6f909SAndy Grover " 0x%08x\n", cmd->se_tfo->get_fabric_name(), 2283c66ac9dbSNicholas Bellinger (ignore_key) ? "_AND_IGNORE_EXISTING_KEY" : "", 2284c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl->initiatorname, 2285c66ac9dbSNicholas Bellinger pr_reg->pr_res_key, pr_reg->pr_res_generation); 2286c66ac9dbSNicholas Bellinger 22876708bb27SAndy Grover if (!aptpl) { 2288c66ac9dbSNicholas Bellinger pr_tmpl->pr_aptpl_active = 0; 2289c66ac9dbSNicholas Bellinger core_scsi3_update_and_write_aptpl(dev, NULL, 0); 22906708bb27SAndy Grover pr_debug("SPC-3 PR: Set APTPL Bit Deactivated" 2291c66ac9dbSNicholas Bellinger " for REGISTER\n"); 2292de103c93SChristoph Hellwig ret = 0; 2293de103c93SChristoph Hellwig goto out_put_pr_reg; 2294c66ac9dbSNicholas Bellinger } 2295c66ac9dbSNicholas Bellinger 2296de103c93SChristoph Hellwig if (!core_scsi3_update_and_write_aptpl(dev, &pr_aptpl_buf[0], 2297de103c93SChristoph Hellwig pr_tmpl->pr_aptpl_buf_len)) { 2298c66ac9dbSNicholas Bellinger pr_tmpl->pr_aptpl_active = 1; 22996708bb27SAndy Grover pr_debug("SPC-3 PR: Set APTPL Bit Activated" 2300c66ac9dbSNicholas Bellinger " for REGISTER\n"); 2301c66ac9dbSNicholas Bellinger } 2302c66ac9dbSNicholas Bellinger 2303de103c93SChristoph Hellwig out_free_aptpl_buf: 2304c66ac9dbSNicholas Bellinger kfree(pr_aptpl_buf); 2305de103c93SChristoph Hellwig ret = 0; 2306de103c93SChristoph Hellwig out_put_pr_reg: 2307c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg); 2308de103c93SChristoph Hellwig return ret; 2309c66ac9dbSNicholas Bellinger } 2310c66ac9dbSNicholas Bellinger 2311c66ac9dbSNicholas Bellinger unsigned char *core_scsi3_pr_dump_type(int type) 2312c66ac9dbSNicholas Bellinger { 2313c66ac9dbSNicholas Bellinger switch (type) { 2314c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE: 2315c66ac9dbSNicholas Bellinger return "Write Exclusive Access"; 2316c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS: 2317c66ac9dbSNicholas Bellinger return "Exclusive Access"; 2318c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_REGONLY: 2319c66ac9dbSNicholas Bellinger return "Write Exclusive Access, Registrants Only"; 2320c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY: 2321c66ac9dbSNicholas Bellinger return "Exclusive Access, Registrants Only"; 2322c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_ALLREG: 2323c66ac9dbSNicholas Bellinger return "Write Exclusive Access, All Registrants"; 2324c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG: 2325c66ac9dbSNicholas Bellinger return "Exclusive Access, All Registrants"; 2326c66ac9dbSNicholas Bellinger default: 2327c66ac9dbSNicholas Bellinger break; 2328c66ac9dbSNicholas Bellinger } 2329c66ac9dbSNicholas Bellinger 2330c66ac9dbSNicholas Bellinger return "Unknown SPC-3 PR Type"; 2331c66ac9dbSNicholas Bellinger } 2332c66ac9dbSNicholas Bellinger 2333de103c93SChristoph Hellwig static sense_reason_t 2334de103c93SChristoph Hellwig core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key) 2335c66ac9dbSNicholas Bellinger { 2336de103c93SChristoph Hellwig struct se_device *dev = cmd->se_dev; 2337e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 2338e3d6f909SAndy Grover struct se_lun *se_lun = cmd->se_lun; 2339c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_res_holder; 23400fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 2341c66ac9dbSNicholas Bellinger char i_buf[PR_REG_ISID_ID_LEN]; 2342de103c93SChristoph Hellwig sense_reason_t ret; 2343de103c93SChristoph Hellwig int prf_isid; 2344c66ac9dbSNicholas Bellinger 2345c66ac9dbSNicholas Bellinger memset(i_buf, 0, PR_REG_ISID_ID_LEN); 2346c66ac9dbSNicholas Bellinger 23476708bb27SAndy Grover if (!se_sess || !se_lun) { 23486708bb27SAndy Grover pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n"); 2349de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2350c66ac9dbSNicholas Bellinger } 2351c66ac9dbSNicholas Bellinger /* 2352c66ac9dbSNicholas Bellinger * Locate the existing *pr_reg via struct se_node_acl pointers 2353c66ac9dbSNicholas Bellinger */ 23545951146dSAndy Grover pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl, 2355c66ac9dbSNicholas Bellinger se_sess); 23566708bb27SAndy Grover if (!pr_reg) { 23576708bb27SAndy Grover pr_err("SPC-3 PR: Unable to locate" 2358c66ac9dbSNicholas Bellinger " PR_REGISTERED *pr_reg for RESERVE\n"); 2359de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2360c66ac9dbSNicholas Bellinger } 2361c66ac9dbSNicholas Bellinger /* 2362c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.9: Reserving: 2363c66ac9dbSNicholas Bellinger * 2364c66ac9dbSNicholas Bellinger * An application client creates a persistent reservation by issuing 2365c66ac9dbSNicholas Bellinger * a PERSISTENT RESERVE OUT command with RESERVE service action through 2366c66ac9dbSNicholas Bellinger * a registered I_T nexus with the following parameters: 2367c66ac9dbSNicholas Bellinger * a) RESERVATION KEY set to the value of the reservation key that is 2368c66ac9dbSNicholas Bellinger * registered with the logical unit for the I_T nexus; and 2369c66ac9dbSNicholas Bellinger */ 2370c66ac9dbSNicholas Bellinger if (res_key != pr_reg->pr_res_key) { 23716708bb27SAndy Grover pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx" 2372c66ac9dbSNicholas Bellinger " does not match existing SA REGISTER res_key:" 2373c66ac9dbSNicholas Bellinger " 0x%016Lx\n", res_key, pr_reg->pr_res_key); 2374de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 2375de103c93SChristoph Hellwig goto out_put_pr_reg; 2376c66ac9dbSNicholas Bellinger } 2377c66ac9dbSNicholas Bellinger /* 2378c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.9: Reserving: 2379c66ac9dbSNicholas Bellinger * 2380c66ac9dbSNicholas Bellinger * From above: 2381c66ac9dbSNicholas Bellinger * b) TYPE field and SCOPE field set to the persistent reservation 2382c66ac9dbSNicholas Bellinger * being created. 2383c66ac9dbSNicholas Bellinger * 2384c66ac9dbSNicholas Bellinger * Only one persistent reservation is allowed at a time per logical unit 2385c66ac9dbSNicholas Bellinger * and that persistent reservation has a scope of LU_SCOPE. 2386c66ac9dbSNicholas Bellinger */ 2387c66ac9dbSNicholas Bellinger if (scope != PR_SCOPE_LU_SCOPE) { 23886708bb27SAndy Grover pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope); 2389de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 2390de103c93SChristoph Hellwig goto out_put_pr_reg; 2391c66ac9dbSNicholas Bellinger } 2392c66ac9dbSNicholas Bellinger /* 2393c66ac9dbSNicholas Bellinger * See if we have an existing PR reservation holder pointer at 2394c66ac9dbSNicholas Bellinger * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration 2395c66ac9dbSNicholas Bellinger * *pr_res_holder. 2396c66ac9dbSNicholas Bellinger */ 2397c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 2398c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 2399ee1b1b9cSAndy Grover if (pr_res_holder) { 2400c66ac9dbSNicholas Bellinger /* 2401c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.9: Reserving: 2402c66ac9dbSNicholas Bellinger * 2403c66ac9dbSNicholas Bellinger * If the device server receives a PERSISTENT RESERVE OUT 2404c66ac9dbSNicholas Bellinger * command from an I_T nexus other than a persistent reservation 2405c66ac9dbSNicholas Bellinger * holder (see 5.7.10) that attempts to create a persistent 2406c66ac9dbSNicholas Bellinger * reservation when a persistent reservation already exists for 2407c66ac9dbSNicholas Bellinger * the logical unit, then the command shall be completed with 2408c66ac9dbSNicholas Bellinger * RESERVATION CONFLICT status. 2409c66ac9dbSNicholas Bellinger */ 2410c66ac9dbSNicholas Bellinger if (pr_res_holder != pr_reg) { 2411c66ac9dbSNicholas Bellinger struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; 24126708bb27SAndy Grover pr_err("SPC-3 PR: Attempted RESERVE from" 2413c66ac9dbSNicholas Bellinger " [%s]: %s while reservation already held by" 2414c66ac9dbSNicholas Bellinger " [%s]: %s, returning RESERVATION_CONFLICT\n", 2415e3d6f909SAndy Grover cmd->se_tfo->get_fabric_name(), 2416c66ac9dbSNicholas Bellinger se_sess->se_node_acl->initiatorname, 2417e3d6f909SAndy Grover pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(), 2418c66ac9dbSNicholas Bellinger pr_res_holder->pr_reg_nacl->initiatorname); 2419c66ac9dbSNicholas Bellinger 2420c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2421de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 2422de103c93SChristoph Hellwig goto out_put_pr_reg; 2423c66ac9dbSNicholas Bellinger } 2424c66ac9dbSNicholas Bellinger /* 2425c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.9: Reserving: 2426c66ac9dbSNicholas Bellinger * 2427c66ac9dbSNicholas Bellinger * If a persistent reservation holder attempts to modify the 2428c66ac9dbSNicholas Bellinger * type or scope of an existing persistent reservation, the 2429c66ac9dbSNicholas Bellinger * command shall be completed with RESERVATION CONFLICT status. 2430c66ac9dbSNicholas Bellinger */ 2431c66ac9dbSNicholas Bellinger if ((pr_res_holder->pr_res_type != type) || 2432c66ac9dbSNicholas Bellinger (pr_res_holder->pr_res_scope != scope)) { 2433c66ac9dbSNicholas Bellinger struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; 24346708bb27SAndy Grover pr_err("SPC-3 PR: Attempted RESERVE from" 2435c66ac9dbSNicholas Bellinger " [%s]: %s trying to change TYPE and/or SCOPE," 2436c66ac9dbSNicholas Bellinger " while reservation already held by [%s]: %s," 2437c66ac9dbSNicholas Bellinger " returning RESERVATION_CONFLICT\n", 2438e3d6f909SAndy Grover cmd->se_tfo->get_fabric_name(), 2439c66ac9dbSNicholas Bellinger se_sess->se_node_acl->initiatorname, 2440e3d6f909SAndy Grover pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(), 2441c66ac9dbSNicholas Bellinger pr_res_holder->pr_reg_nacl->initiatorname); 2442c66ac9dbSNicholas Bellinger 2443c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2444de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 2445de103c93SChristoph Hellwig goto out_put_pr_reg; 2446c66ac9dbSNicholas Bellinger } 2447c66ac9dbSNicholas Bellinger /* 2448c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.9: Reserving: 2449c66ac9dbSNicholas Bellinger * 2450c66ac9dbSNicholas Bellinger * If the device server receives a PERSISTENT RESERVE OUT 2451c66ac9dbSNicholas Bellinger * command with RESERVE service action where the TYPE field and 2452c66ac9dbSNicholas Bellinger * the SCOPE field contain the same values as the existing type 2453c66ac9dbSNicholas Bellinger * and scope from a persistent reservation holder, it shall not 2454c66ac9dbSNicholas Bellinger * make any change to the existing persistent reservation and 2455c66ac9dbSNicholas Bellinger * shall completethe command with GOOD status. 2456c66ac9dbSNicholas Bellinger */ 2457c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2458de103c93SChristoph Hellwig ret = 0; 2459de103c93SChristoph Hellwig goto out_put_pr_reg; 2460c66ac9dbSNicholas Bellinger } 2461c66ac9dbSNicholas Bellinger /* 2462c66ac9dbSNicholas Bellinger * Otherwise, our *pr_reg becomes the PR reservation holder for said 2463c66ac9dbSNicholas Bellinger * TYPE/SCOPE. Also set the received scope and type in *pr_reg. 2464c66ac9dbSNicholas Bellinger */ 2465c66ac9dbSNicholas Bellinger pr_reg->pr_res_scope = scope; 2466c66ac9dbSNicholas Bellinger pr_reg->pr_res_type = type; 2467c66ac9dbSNicholas Bellinger pr_reg->pr_res_holder = 1; 2468c66ac9dbSNicholas Bellinger dev->dev_pr_res_holder = pr_reg; 2469c66ac9dbSNicholas Bellinger prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0], 2470c66ac9dbSNicholas Bellinger PR_REG_ISID_ID_LEN); 2471c66ac9dbSNicholas Bellinger 24726708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new" 2473c66ac9dbSNicholas Bellinger " reservation holder TYPE: %s ALL_TG_PT: %d\n", 2474e3d6f909SAndy Grover cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type), 2475c66ac9dbSNicholas Bellinger (pr_reg->pr_reg_all_tg_pt) ? 1 : 0); 24766708bb27SAndy Grover pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n", 2477e3d6f909SAndy Grover cmd->se_tfo->get_fabric_name(), 2478c66ac9dbSNicholas Bellinger se_sess->se_node_acl->initiatorname, 2479c66ac9dbSNicholas Bellinger (prf_isid) ? &i_buf[0] : ""); 2480c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2481c66ac9dbSNicholas Bellinger 2482c66ac9dbSNicholas Bellinger if (pr_tmpl->pr_aptpl_active) { 2483de103c93SChristoph Hellwig if (!core_scsi3_update_and_write_aptpl(cmd->se_dev, 2484c66ac9dbSNicholas Bellinger &pr_reg->pr_aptpl_buf[0], 2485de103c93SChristoph Hellwig pr_tmpl->pr_aptpl_buf_len)) { 24866708bb27SAndy Grover pr_debug("SPC-3 PR: Updated APTPL metadata" 2487c66ac9dbSNicholas Bellinger " for RESERVE\n"); 2488c66ac9dbSNicholas Bellinger } 2489c66ac9dbSNicholas Bellinger } 2490c66ac9dbSNicholas Bellinger 2491de103c93SChristoph Hellwig ret = 0; 2492de103c93SChristoph Hellwig out_put_pr_reg: 2493de103c93SChristoph Hellwig core_scsi3_put_pr_reg(pr_reg); 2494de103c93SChristoph Hellwig return ret; 2495de103c93SChristoph Hellwig } 2496de103c93SChristoph Hellwig 2497de103c93SChristoph Hellwig static sense_reason_t 2498de103c93SChristoph Hellwig core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope, 2499c66ac9dbSNicholas Bellinger u64 res_key) 2500c66ac9dbSNicholas Bellinger { 2501c66ac9dbSNicholas Bellinger switch (type) { 2502c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE: 2503c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS: 2504c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_REGONLY: 2505c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY: 2506c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_ALLREG: 2507c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG: 2508de103c93SChristoph Hellwig return core_scsi3_pro_reserve(cmd, type, scope, res_key); 2509c66ac9dbSNicholas Bellinger default: 25106708bb27SAndy Grover pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:" 2511c66ac9dbSNicholas Bellinger " 0x%02x\n", type); 2512de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 2513c66ac9dbSNicholas Bellinger } 2514c66ac9dbSNicholas Bellinger } 2515c66ac9dbSNicholas Bellinger 2516c66ac9dbSNicholas Bellinger /* 2517c66ac9dbSNicholas Bellinger * Called with struct se_device->dev_reservation_lock held. 2518c66ac9dbSNicholas Bellinger */ 2519c66ac9dbSNicholas Bellinger static void __core_scsi3_complete_pro_release( 2520c66ac9dbSNicholas Bellinger struct se_device *dev, 2521c66ac9dbSNicholas Bellinger struct se_node_acl *se_nacl, 2522c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, 2523c66ac9dbSNicholas Bellinger int explict) 2524c66ac9dbSNicholas Bellinger { 2525c66ac9dbSNicholas Bellinger struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo; 2526c66ac9dbSNicholas Bellinger char i_buf[PR_REG_ISID_ID_LEN]; 2527c66ac9dbSNicholas Bellinger int prf_isid; 2528c66ac9dbSNicholas Bellinger 2529c66ac9dbSNicholas Bellinger memset(i_buf, 0, PR_REG_ISID_ID_LEN); 2530c66ac9dbSNicholas Bellinger prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0], 2531c66ac9dbSNicholas Bellinger PR_REG_ISID_ID_LEN); 2532c66ac9dbSNicholas Bellinger /* 2533c66ac9dbSNicholas Bellinger * Go ahead and release the current PR reservation holder. 2534c66ac9dbSNicholas Bellinger */ 2535c66ac9dbSNicholas Bellinger dev->dev_pr_res_holder = NULL; 2536c66ac9dbSNicholas Bellinger 25376708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared" 2538c66ac9dbSNicholas Bellinger " reservation holder TYPE: %s ALL_TG_PT: %d\n", 2539c66ac9dbSNicholas Bellinger tfo->get_fabric_name(), (explict) ? "explict" : "implict", 2540c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(pr_reg->pr_res_type), 2541c66ac9dbSNicholas Bellinger (pr_reg->pr_reg_all_tg_pt) ? 1 : 0); 25426708bb27SAndy Grover pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n", 2543c66ac9dbSNicholas Bellinger tfo->get_fabric_name(), se_nacl->initiatorname, 2544c66ac9dbSNicholas Bellinger (prf_isid) ? &i_buf[0] : ""); 2545c66ac9dbSNicholas Bellinger /* 2546c66ac9dbSNicholas Bellinger * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE 2547c66ac9dbSNicholas Bellinger */ 2548c66ac9dbSNicholas Bellinger pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0; 2549c66ac9dbSNicholas Bellinger } 2550c66ac9dbSNicholas Bellinger 2551de103c93SChristoph Hellwig static sense_reason_t 2552de103c93SChristoph Hellwig core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope, 2553c66ac9dbSNicholas Bellinger u64 res_key) 2554c66ac9dbSNicholas Bellinger { 2555c66ac9dbSNicholas Bellinger struct se_device *dev = cmd->se_dev; 2556e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 2557e3d6f909SAndy Grover struct se_lun *se_lun = cmd->se_lun; 2558c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder; 25590fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 2560de103c93SChristoph Hellwig int all_reg = 0; 2561de103c93SChristoph Hellwig sense_reason_t ret = 0; 2562c66ac9dbSNicholas Bellinger 25636708bb27SAndy Grover if (!se_sess || !se_lun) { 25646708bb27SAndy Grover pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n"); 2565de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2566c66ac9dbSNicholas Bellinger } 2567c66ac9dbSNicholas Bellinger /* 2568c66ac9dbSNicholas Bellinger * Locate the existing *pr_reg via struct se_node_acl pointers 2569c66ac9dbSNicholas Bellinger */ 2570c66ac9dbSNicholas Bellinger pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess); 25716708bb27SAndy Grover if (!pr_reg) { 25726708bb27SAndy Grover pr_err("SPC-3 PR: Unable to locate" 2573c66ac9dbSNicholas Bellinger " PR_REGISTERED *pr_reg for RELEASE\n"); 2574de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2575c66ac9dbSNicholas Bellinger } 2576c66ac9dbSNicholas Bellinger /* 2577c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.11.2 Releasing: 2578c66ac9dbSNicholas Bellinger * 2579c66ac9dbSNicholas Bellinger * If there is no persistent reservation or in response to a persistent 2580c66ac9dbSNicholas Bellinger * reservation release request from a registered I_T nexus that is not a 2581c66ac9dbSNicholas Bellinger * persistent reservation holder (see 5.7.10), the device server shall 2582c66ac9dbSNicholas Bellinger * do the following: 2583c66ac9dbSNicholas Bellinger * 2584c66ac9dbSNicholas Bellinger * a) Not release the persistent reservation, if any; 2585c66ac9dbSNicholas Bellinger * b) Not remove any registrations; and 2586c66ac9dbSNicholas Bellinger * c) Complete the command with GOOD status. 2587c66ac9dbSNicholas Bellinger */ 2588c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 2589c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 25906708bb27SAndy Grover if (!pr_res_holder) { 2591c66ac9dbSNicholas Bellinger /* 2592c66ac9dbSNicholas Bellinger * No persistent reservation, return GOOD status. 2593c66ac9dbSNicholas Bellinger */ 2594c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2595bb7a8c8eSNicholas Bellinger goto out_put_pr_reg; 2596c66ac9dbSNicholas Bellinger } 2597c66ac9dbSNicholas Bellinger if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || 2598c66ac9dbSNicholas Bellinger (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) 2599c66ac9dbSNicholas Bellinger all_reg = 1; 2600c66ac9dbSNicholas Bellinger 2601c66ac9dbSNicholas Bellinger if ((all_reg == 0) && (pr_res_holder != pr_reg)) { 2602c66ac9dbSNicholas Bellinger /* 2603c66ac9dbSNicholas Bellinger * Non 'All Registrants' PR Type cases.. 2604c66ac9dbSNicholas Bellinger * Release request from a registered I_T nexus that is not a 2605c66ac9dbSNicholas Bellinger * persistent reservation holder. return GOOD status. 2606c66ac9dbSNicholas Bellinger */ 2607c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2608de103c93SChristoph Hellwig goto out_put_pr_reg; 2609c66ac9dbSNicholas Bellinger } 2610de103c93SChristoph Hellwig 2611c66ac9dbSNicholas Bellinger /* 2612c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.11.2 Releasing: 2613c66ac9dbSNicholas Bellinger * 2614c66ac9dbSNicholas Bellinger * Only the persistent reservation holder (see 5.7.10) is allowed to 2615c66ac9dbSNicholas Bellinger * release a persistent reservation. 2616c66ac9dbSNicholas Bellinger * 2617c66ac9dbSNicholas Bellinger * An application client releases the persistent reservation by issuing 2618c66ac9dbSNicholas Bellinger * a PERSISTENT RESERVE OUT command with RELEASE service action through 2619c66ac9dbSNicholas Bellinger * an I_T nexus that is a persistent reservation holder with the 2620c66ac9dbSNicholas Bellinger * following parameters: 2621c66ac9dbSNicholas Bellinger * 2622c66ac9dbSNicholas Bellinger * a) RESERVATION KEY field set to the value of the reservation key 2623c66ac9dbSNicholas Bellinger * that is registered with the logical unit for the I_T nexus; 2624c66ac9dbSNicholas Bellinger */ 2625c66ac9dbSNicholas Bellinger if (res_key != pr_reg->pr_res_key) { 26266708bb27SAndy Grover pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx" 2627c66ac9dbSNicholas Bellinger " does not match existing SA REGISTER res_key:" 2628c66ac9dbSNicholas Bellinger " 0x%016Lx\n", res_key, pr_reg->pr_res_key); 2629c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2630de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 2631de103c93SChristoph Hellwig goto out_put_pr_reg; 2632c66ac9dbSNicholas Bellinger } 2633c66ac9dbSNicholas Bellinger /* 2634c66ac9dbSNicholas Bellinger * From spc4r17 Section 5.7.11.2 Releasing and above: 2635c66ac9dbSNicholas Bellinger * 2636c66ac9dbSNicholas Bellinger * b) TYPE field and SCOPE field set to match the persistent 2637c66ac9dbSNicholas Bellinger * reservation being released. 2638c66ac9dbSNicholas Bellinger */ 2639c66ac9dbSNicholas Bellinger if ((pr_res_holder->pr_res_type != type) || 2640c66ac9dbSNicholas Bellinger (pr_res_holder->pr_res_scope != scope)) { 2641c66ac9dbSNicholas Bellinger struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; 26426708bb27SAndy Grover pr_err("SPC-3 PR RELEASE: Attempted to release" 2643c66ac9dbSNicholas Bellinger " reservation from [%s]: %s with different TYPE " 2644c66ac9dbSNicholas Bellinger "and/or SCOPE while reservation already held by" 2645c66ac9dbSNicholas Bellinger " [%s]: %s, returning RESERVATION_CONFLICT\n", 2646e3d6f909SAndy Grover cmd->se_tfo->get_fabric_name(), 2647c66ac9dbSNicholas Bellinger se_sess->se_node_acl->initiatorname, 2648e3d6f909SAndy Grover pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(), 2649c66ac9dbSNicholas Bellinger pr_res_holder->pr_reg_nacl->initiatorname); 2650c66ac9dbSNicholas Bellinger 2651c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2652de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 2653de103c93SChristoph Hellwig goto out_put_pr_reg; 2654c66ac9dbSNicholas Bellinger } 2655c66ac9dbSNicholas Bellinger /* 2656c66ac9dbSNicholas Bellinger * In response to a persistent reservation release request from the 2657c66ac9dbSNicholas Bellinger * persistent reservation holder the device server shall perform a 2658c66ac9dbSNicholas Bellinger * release by doing the following as an uninterrupted series of actions: 2659c66ac9dbSNicholas Bellinger * a) Release the persistent reservation; 2660c66ac9dbSNicholas Bellinger * b) Not remove any registration(s); 2661c66ac9dbSNicholas Bellinger * c) If the released persistent reservation is a registrants only type 2662c66ac9dbSNicholas Bellinger * or all registrants type persistent reservation, 2663c66ac9dbSNicholas Bellinger * the device server shall establish a unit attention condition for 2664c66ac9dbSNicholas Bellinger * the initiator port associated with every regis- 2665c66ac9dbSNicholas Bellinger * tered I_T nexus other than I_T nexus on which the PERSISTENT 2666c66ac9dbSNicholas Bellinger * RESERVE OUT command with RELEASE service action was received, 2667c66ac9dbSNicholas Bellinger * with the additional sense code set to RESERVATIONS RELEASED; and 2668c66ac9dbSNicholas Bellinger * d) If the persistent reservation is of any other type, the device 2669c66ac9dbSNicholas Bellinger * server shall not establish a unit attention condition. 2670c66ac9dbSNicholas Bellinger */ 2671c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl, 2672c66ac9dbSNicholas Bellinger pr_reg, 1); 2673c66ac9dbSNicholas Bellinger 2674c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2675c66ac9dbSNicholas Bellinger 2676c66ac9dbSNicholas Bellinger if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) && 2677c66ac9dbSNicholas Bellinger (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) && 2678c66ac9dbSNicholas Bellinger (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) && 2679c66ac9dbSNicholas Bellinger (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) { 2680c66ac9dbSNicholas Bellinger /* 2681c66ac9dbSNicholas Bellinger * If no UNIT ATTENTION conditions will be established for 2682c66ac9dbSNicholas Bellinger * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS 2683c66ac9dbSNicholas Bellinger * go ahead and check for APTPL=1 update+write below 2684c66ac9dbSNicholas Bellinger */ 2685c66ac9dbSNicholas Bellinger goto write_aptpl; 2686c66ac9dbSNicholas Bellinger } 2687c66ac9dbSNicholas Bellinger 2688c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 2689c66ac9dbSNicholas Bellinger list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list, 2690c66ac9dbSNicholas Bellinger pr_reg_list) { 2691c66ac9dbSNicholas Bellinger /* 2692c66ac9dbSNicholas Bellinger * Do not establish a UNIT ATTENTION condition 2693c66ac9dbSNicholas Bellinger * for the calling I_T Nexus 2694c66ac9dbSNicholas Bellinger */ 2695c66ac9dbSNicholas Bellinger if (pr_reg_p == pr_reg) 2696c66ac9dbSNicholas Bellinger continue; 2697c66ac9dbSNicholas Bellinger 2698c66ac9dbSNicholas Bellinger core_scsi3_ua_allocate(pr_reg_p->pr_reg_nacl, 2699c66ac9dbSNicholas Bellinger pr_reg_p->pr_res_mapped_lun, 2700c66ac9dbSNicholas Bellinger 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED); 2701c66ac9dbSNicholas Bellinger } 2702c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 2703c66ac9dbSNicholas Bellinger 2704c66ac9dbSNicholas Bellinger write_aptpl: 2705c66ac9dbSNicholas Bellinger if (pr_tmpl->pr_aptpl_active) { 2706de103c93SChristoph Hellwig if (!core_scsi3_update_and_write_aptpl(cmd->se_dev, 2707de103c93SChristoph Hellwig &pr_reg->pr_aptpl_buf[0], pr_tmpl->pr_aptpl_buf_len)) { 27086708bb27SAndy Grover pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n"); 2709c66ac9dbSNicholas Bellinger } 2710de103c93SChristoph Hellwig } 2711de103c93SChristoph Hellwig out_put_pr_reg: 2712c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg); 2713de103c93SChristoph Hellwig return ret; 2714c66ac9dbSNicholas Bellinger } 2715c66ac9dbSNicholas Bellinger 2716de103c93SChristoph Hellwig static sense_reason_t 2717de103c93SChristoph Hellwig core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key) 2718c66ac9dbSNicholas Bellinger { 2719c66ac9dbSNicholas Bellinger struct se_device *dev = cmd->se_dev; 2720c66ac9dbSNicholas Bellinger struct se_node_acl *pr_reg_nacl; 2721e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 27220fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 2723c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder; 2724c66ac9dbSNicholas Bellinger u32 pr_res_mapped_lun = 0; 2725c66ac9dbSNicholas Bellinger int calling_it_nexus = 0; 2726c66ac9dbSNicholas Bellinger /* 2727c66ac9dbSNicholas Bellinger * Locate the existing *pr_reg via struct se_node_acl pointers 2728c66ac9dbSNicholas Bellinger */ 27295951146dSAndy Grover pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, 2730c66ac9dbSNicholas Bellinger se_sess->se_node_acl, se_sess); 27316708bb27SAndy Grover if (!pr_reg_n) { 27326708bb27SAndy Grover pr_err("SPC-3 PR: Unable to locate" 2733c66ac9dbSNicholas Bellinger " PR_REGISTERED *pr_reg for CLEAR\n"); 2734de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2735c66ac9dbSNicholas Bellinger } 2736c66ac9dbSNicholas Bellinger /* 2737c66ac9dbSNicholas Bellinger * From spc4r17 section 5.7.11.6, Clearing: 2738c66ac9dbSNicholas Bellinger * 2739c66ac9dbSNicholas Bellinger * Any application client may release the persistent reservation and 2740c66ac9dbSNicholas Bellinger * remove all registrations from a device server by issuing a 2741c66ac9dbSNicholas Bellinger * PERSISTENT RESERVE OUT command with CLEAR service action through a 2742c66ac9dbSNicholas Bellinger * registered I_T nexus with the following parameter: 2743c66ac9dbSNicholas Bellinger * 2744c66ac9dbSNicholas Bellinger * a) RESERVATION KEY field set to the value of the reservation key 2745c66ac9dbSNicholas Bellinger * that is registered with the logical unit for the I_T nexus. 2746c66ac9dbSNicholas Bellinger */ 2747c66ac9dbSNicholas Bellinger if (res_key != pr_reg_n->pr_res_key) { 27486708bb27SAndy Grover pr_err("SPC-3 PR REGISTER: Received" 2749c66ac9dbSNicholas Bellinger " res_key: 0x%016Lx does not match" 2750c66ac9dbSNicholas Bellinger " existing SA REGISTER res_key:" 2751c66ac9dbSNicholas Bellinger " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key); 2752c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_n); 2753de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 2754c66ac9dbSNicholas Bellinger } 2755c66ac9dbSNicholas Bellinger /* 2756c66ac9dbSNicholas Bellinger * a) Release the persistent reservation, if any; 2757c66ac9dbSNicholas Bellinger */ 2758c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 2759c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 2760c66ac9dbSNicholas Bellinger if (pr_res_holder) { 2761c66ac9dbSNicholas Bellinger struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl; 2762c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_release(dev, pr_res_nacl, 2763c66ac9dbSNicholas Bellinger pr_res_holder, 0); 2764c66ac9dbSNicholas Bellinger } 2765c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2766c66ac9dbSNicholas Bellinger /* 2767c66ac9dbSNicholas Bellinger * b) Remove all registration(s) (see spc4r17 5.7.7); 2768c66ac9dbSNicholas Bellinger */ 2769c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 2770c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 2771c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 2772c66ac9dbSNicholas Bellinger 2773c66ac9dbSNicholas Bellinger calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; 2774c66ac9dbSNicholas Bellinger pr_reg_nacl = pr_reg->pr_reg_nacl; 2775c66ac9dbSNicholas Bellinger pr_res_mapped_lun = pr_reg->pr_res_mapped_lun; 2776c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, pr_reg, NULL, 2777c66ac9dbSNicholas Bellinger calling_it_nexus); 2778c66ac9dbSNicholas Bellinger /* 2779c66ac9dbSNicholas Bellinger * e) Establish a unit attention condition for the initiator 2780c66ac9dbSNicholas Bellinger * port associated with every registered I_T nexus other 2781c66ac9dbSNicholas Bellinger * than the I_T nexus on which the PERSISTENT RESERVE OUT 2782c66ac9dbSNicholas Bellinger * command with CLEAR service action was received, with the 2783c66ac9dbSNicholas Bellinger * additional sense code set to RESERVATIONS PREEMPTED. 2784c66ac9dbSNicholas Bellinger */ 27856708bb27SAndy Grover if (!calling_it_nexus) 2786c66ac9dbSNicholas Bellinger core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 2787c66ac9dbSNicholas Bellinger 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED); 2788c66ac9dbSNicholas Bellinger } 2789c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 2790c66ac9dbSNicholas Bellinger 27916708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n", 2792e3d6f909SAndy Grover cmd->se_tfo->get_fabric_name()); 2793c66ac9dbSNicholas Bellinger 2794c66ac9dbSNicholas Bellinger if (pr_tmpl->pr_aptpl_active) { 27955951146dSAndy Grover core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0); 27966708bb27SAndy Grover pr_debug("SPC-3 PR: Updated APTPL metadata" 2797c66ac9dbSNicholas Bellinger " for CLEAR\n"); 2798c66ac9dbSNicholas Bellinger } 2799c66ac9dbSNicholas Bellinger 2800c66ac9dbSNicholas Bellinger core_scsi3_pr_generation(dev); 2801c66ac9dbSNicholas Bellinger return 0; 2802c66ac9dbSNicholas Bellinger } 2803c66ac9dbSNicholas Bellinger 2804c66ac9dbSNicholas Bellinger /* 2805c66ac9dbSNicholas Bellinger * Called with struct se_device->dev_reservation_lock held. 2806c66ac9dbSNicholas Bellinger */ 2807c66ac9dbSNicholas Bellinger static void __core_scsi3_complete_pro_preempt( 2808c66ac9dbSNicholas Bellinger struct se_device *dev, 2809c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, 2810c66ac9dbSNicholas Bellinger struct list_head *preempt_and_abort_list, 2811c66ac9dbSNicholas Bellinger int type, 2812c66ac9dbSNicholas Bellinger int scope, 2813c66ac9dbSNicholas Bellinger int abort) 2814c66ac9dbSNicholas Bellinger { 2815c66ac9dbSNicholas Bellinger struct se_node_acl *nacl = pr_reg->pr_reg_nacl; 2816c66ac9dbSNicholas Bellinger struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo; 2817c66ac9dbSNicholas Bellinger char i_buf[PR_REG_ISID_ID_LEN]; 2818c66ac9dbSNicholas Bellinger int prf_isid; 2819c66ac9dbSNicholas Bellinger 2820c66ac9dbSNicholas Bellinger memset(i_buf, 0, PR_REG_ISID_ID_LEN); 2821c66ac9dbSNicholas Bellinger prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0], 2822c66ac9dbSNicholas Bellinger PR_REG_ISID_ID_LEN); 2823c66ac9dbSNicholas Bellinger /* 2824c66ac9dbSNicholas Bellinger * Do an implict RELEASE of the existing reservation. 2825c66ac9dbSNicholas Bellinger */ 2826c66ac9dbSNicholas Bellinger if (dev->dev_pr_res_holder) 2827c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_release(dev, nacl, 2828c66ac9dbSNicholas Bellinger dev->dev_pr_res_holder, 0); 2829c66ac9dbSNicholas Bellinger 2830c66ac9dbSNicholas Bellinger dev->dev_pr_res_holder = pr_reg; 2831c66ac9dbSNicholas Bellinger pr_reg->pr_res_holder = 1; 2832c66ac9dbSNicholas Bellinger pr_reg->pr_res_type = type; 2833c66ac9dbSNicholas Bellinger pr_reg->pr_res_scope = scope; 2834c66ac9dbSNicholas Bellinger 28356708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new" 2836c66ac9dbSNicholas Bellinger " reservation holder TYPE: %s ALL_TG_PT: %d\n", 2837c66ac9dbSNicholas Bellinger tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "", 2838c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(type), 2839c66ac9dbSNicholas Bellinger (pr_reg->pr_reg_all_tg_pt) ? 1 : 0); 28406708bb27SAndy Grover pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n", 2841c66ac9dbSNicholas Bellinger tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "", 2842c66ac9dbSNicholas Bellinger nacl->initiatorname, (prf_isid) ? &i_buf[0] : ""); 2843c66ac9dbSNicholas Bellinger /* 2844c66ac9dbSNicholas Bellinger * For PREEMPT_AND_ABORT, add the preempting reservation's 2845c66ac9dbSNicholas Bellinger * struct t10_pr_registration to the list that will be compared 2846c66ac9dbSNicholas Bellinger * against received CDBs.. 2847c66ac9dbSNicholas Bellinger */ 2848c66ac9dbSNicholas Bellinger if (preempt_and_abort_list) 2849c66ac9dbSNicholas Bellinger list_add_tail(&pr_reg->pr_reg_abort_list, 2850c66ac9dbSNicholas Bellinger preempt_and_abort_list); 2851c66ac9dbSNicholas Bellinger } 2852c66ac9dbSNicholas Bellinger 2853c66ac9dbSNicholas Bellinger static void core_scsi3_release_preempt_and_abort( 2854c66ac9dbSNicholas Bellinger struct list_head *preempt_and_abort_list, 2855c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg_holder) 2856c66ac9dbSNicholas Bellinger { 2857c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp; 2858c66ac9dbSNicholas Bellinger 2859c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list, 2860c66ac9dbSNicholas Bellinger pr_reg_abort_list) { 2861c66ac9dbSNicholas Bellinger 2862c66ac9dbSNicholas Bellinger list_del(&pr_reg->pr_reg_abort_list); 2863c66ac9dbSNicholas Bellinger if (pr_reg_holder == pr_reg) 2864c66ac9dbSNicholas Bellinger continue; 2865c66ac9dbSNicholas Bellinger if (pr_reg->pr_res_holder) { 28666708bb27SAndy Grover pr_warn("pr_reg->pr_res_holder still set\n"); 2867c66ac9dbSNicholas Bellinger continue; 2868c66ac9dbSNicholas Bellinger } 2869c66ac9dbSNicholas Bellinger 2870c66ac9dbSNicholas Bellinger pr_reg->pr_reg_deve = NULL; 2871c66ac9dbSNicholas Bellinger pr_reg->pr_reg_nacl = NULL; 2872c66ac9dbSNicholas Bellinger kfree(pr_reg->pr_aptpl_buf); 2873c66ac9dbSNicholas Bellinger kmem_cache_free(t10_pr_reg_cache, pr_reg); 2874c66ac9dbSNicholas Bellinger } 2875c66ac9dbSNicholas Bellinger } 2876c66ac9dbSNicholas Bellinger 2877de103c93SChristoph Hellwig static sense_reason_t 2878de103c93SChristoph Hellwig core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key, 2879de103c93SChristoph Hellwig u64 sa_res_key, int abort) 2880c66ac9dbSNicholas Bellinger { 28815951146dSAndy Grover struct se_device *dev = cmd->se_dev; 2882c66ac9dbSNicholas Bellinger struct se_node_acl *pr_reg_nacl; 2883e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 2884d0f474e5SRoland Dreier LIST_HEAD(preempt_and_abort_list); 2885c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder; 28860fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 2887c66ac9dbSNicholas Bellinger u32 pr_res_mapped_lun = 0; 2888c66ac9dbSNicholas Bellinger int all_reg = 0, calling_it_nexus = 0, released_regs = 0; 2889de103c93SChristoph Hellwig int prh_type = 0, prh_scope = 0; 2890c66ac9dbSNicholas Bellinger 2891de103c93SChristoph Hellwig if (!se_sess) 2892de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2893c66ac9dbSNicholas Bellinger 28945951146dSAndy Grover pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl, 2895c66ac9dbSNicholas Bellinger se_sess); 28966708bb27SAndy Grover if (!pr_reg_n) { 28976708bb27SAndy Grover pr_err("SPC-3 PR: Unable to locate" 2898c66ac9dbSNicholas Bellinger " PR_REGISTERED *pr_reg for PREEMPT%s\n", 2899c66ac9dbSNicholas Bellinger (abort) ? "_AND_ABORT" : ""); 2900de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 2901c66ac9dbSNicholas Bellinger } 2902c66ac9dbSNicholas Bellinger if (pr_reg_n->pr_res_key != res_key) { 2903c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_n); 2904de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 2905c66ac9dbSNicholas Bellinger } 2906c66ac9dbSNicholas Bellinger if (scope != PR_SCOPE_LU_SCOPE) { 29076708bb27SAndy Grover pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope); 2908c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_n); 2909de103c93SChristoph Hellwig return TCM_INVALID_PARAMETER_LIST; 2910c66ac9dbSNicholas Bellinger } 2911c66ac9dbSNicholas Bellinger 2912c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 2913c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 2914c66ac9dbSNicholas Bellinger if (pr_res_holder && 2915c66ac9dbSNicholas Bellinger ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || 2916c66ac9dbSNicholas Bellinger (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) 2917c66ac9dbSNicholas Bellinger all_reg = 1; 2918c66ac9dbSNicholas Bellinger 29196708bb27SAndy Grover if (!all_reg && !sa_res_key) { 2920c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 2921c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_n); 2922de103c93SChristoph Hellwig return TCM_INVALID_PARAMETER_LIST; 2923c66ac9dbSNicholas Bellinger } 2924c66ac9dbSNicholas Bellinger /* 2925c66ac9dbSNicholas Bellinger * From spc4r17, section 5.7.11.4.4 Removing Registrations: 2926c66ac9dbSNicholas Bellinger * 2927c66ac9dbSNicholas Bellinger * If the SERVICE ACTION RESERVATION KEY field does not identify a 2928c66ac9dbSNicholas Bellinger * persistent reservation holder or there is no persistent reservation 2929c66ac9dbSNicholas Bellinger * holder (i.e., there is no persistent reservation), then the device 2930c66ac9dbSNicholas Bellinger * server shall perform a preempt by doing the following in an 2931c66ac9dbSNicholas Bellinger * uninterrupted series of actions. (See below..) 2932c66ac9dbSNicholas Bellinger */ 29336708bb27SAndy Grover if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) { 2934c66ac9dbSNicholas Bellinger /* 2935c66ac9dbSNicholas Bellinger * No existing or SA Reservation Key matching reservations.. 2936c66ac9dbSNicholas Bellinger * 2937c66ac9dbSNicholas Bellinger * PROUT SA PREEMPT with All Registrant type reservations are 2938c66ac9dbSNicholas Bellinger * allowed to be processed without a matching SA Reservation Key 2939c66ac9dbSNicholas Bellinger */ 2940c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 2941c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 2942c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 2943c66ac9dbSNicholas Bellinger /* 2944c66ac9dbSNicholas Bellinger * Removing of registrations in non all registrants 2945c66ac9dbSNicholas Bellinger * type reservations without a matching SA reservation 2946c66ac9dbSNicholas Bellinger * key. 2947c66ac9dbSNicholas Bellinger * 2948c66ac9dbSNicholas Bellinger * a) Remove the registrations for all I_T nexuses 2949c66ac9dbSNicholas Bellinger * specified by the SERVICE ACTION RESERVATION KEY 2950c66ac9dbSNicholas Bellinger * field; 2951c66ac9dbSNicholas Bellinger * b) Ignore the contents of the SCOPE and TYPE fields; 2952c66ac9dbSNicholas Bellinger * c) Process tasks as defined in 5.7.1; and 2953c66ac9dbSNicholas Bellinger * d) Establish a unit attention condition for the 2954c66ac9dbSNicholas Bellinger * initiator port associated with every I_T nexus 2955c66ac9dbSNicholas Bellinger * that lost its registration other than the I_T 2956c66ac9dbSNicholas Bellinger * nexus on which the PERSISTENT RESERVE OUT command 2957c66ac9dbSNicholas Bellinger * was received, with the additional sense code set 2958c66ac9dbSNicholas Bellinger * to REGISTRATIONS PREEMPTED. 2959c66ac9dbSNicholas Bellinger */ 29606708bb27SAndy Grover if (!all_reg) { 2961c66ac9dbSNicholas Bellinger if (pr_reg->pr_res_key != sa_res_key) 2962c66ac9dbSNicholas Bellinger continue; 2963c66ac9dbSNicholas Bellinger 2964c66ac9dbSNicholas Bellinger calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; 2965c66ac9dbSNicholas Bellinger pr_reg_nacl = pr_reg->pr_reg_nacl; 2966c66ac9dbSNicholas Bellinger pr_res_mapped_lun = pr_reg->pr_res_mapped_lun; 2967c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, pr_reg, 2968c66ac9dbSNicholas Bellinger (abort) ? &preempt_and_abort_list : 2969c66ac9dbSNicholas Bellinger NULL, calling_it_nexus); 2970c66ac9dbSNicholas Bellinger released_regs++; 2971c66ac9dbSNicholas Bellinger } else { 2972c66ac9dbSNicholas Bellinger /* 2973c66ac9dbSNicholas Bellinger * Case for any existing all registrants type 2974c66ac9dbSNicholas Bellinger * reservation, follow logic in spc4r17 section 2975c66ac9dbSNicholas Bellinger * 5.7.11.4 Preempting, Table 52 and Figure 7. 2976c66ac9dbSNicholas Bellinger * 2977c66ac9dbSNicholas Bellinger * For a ZERO SA Reservation key, release 2978c66ac9dbSNicholas Bellinger * all other registrations and do an implict 2979c66ac9dbSNicholas Bellinger * release of active persistent reservation. 2980c66ac9dbSNicholas Bellinger * 2981c66ac9dbSNicholas Bellinger * For a non-ZERO SA Reservation key, only 2982c66ac9dbSNicholas Bellinger * release the matching reservation key from 2983c66ac9dbSNicholas Bellinger * registrations. 2984c66ac9dbSNicholas Bellinger */ 2985c66ac9dbSNicholas Bellinger if ((sa_res_key) && 2986c66ac9dbSNicholas Bellinger (pr_reg->pr_res_key != sa_res_key)) 2987c66ac9dbSNicholas Bellinger continue; 2988c66ac9dbSNicholas Bellinger 2989c66ac9dbSNicholas Bellinger calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; 2990c66ac9dbSNicholas Bellinger if (calling_it_nexus) 2991c66ac9dbSNicholas Bellinger continue; 2992c66ac9dbSNicholas Bellinger 2993c66ac9dbSNicholas Bellinger pr_reg_nacl = pr_reg->pr_reg_nacl; 2994c66ac9dbSNicholas Bellinger pr_res_mapped_lun = pr_reg->pr_res_mapped_lun; 2995c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, pr_reg, 2996c66ac9dbSNicholas Bellinger (abort) ? &preempt_and_abort_list : 2997c66ac9dbSNicholas Bellinger NULL, 0); 2998c66ac9dbSNicholas Bellinger released_regs++; 2999c66ac9dbSNicholas Bellinger } 30006708bb27SAndy Grover if (!calling_it_nexus) 3001c66ac9dbSNicholas Bellinger core_scsi3_ua_allocate(pr_reg_nacl, 3002c66ac9dbSNicholas Bellinger pr_res_mapped_lun, 0x2A, 30039e08e34eSMarco Sanvido ASCQ_2AH_REGISTRATIONS_PREEMPTED); 3004c66ac9dbSNicholas Bellinger } 3005c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 3006c66ac9dbSNicholas Bellinger /* 3007c66ac9dbSNicholas Bellinger * If a PERSISTENT RESERVE OUT with a PREEMPT service action or 3008c66ac9dbSNicholas Bellinger * a PREEMPT AND ABORT service action sets the SERVICE ACTION 3009c66ac9dbSNicholas Bellinger * RESERVATION KEY field to a value that does not match any 3010c66ac9dbSNicholas Bellinger * registered reservation key, then the device server shall 3011c66ac9dbSNicholas Bellinger * complete the command with RESERVATION CONFLICT status. 3012c66ac9dbSNicholas Bellinger */ 30136708bb27SAndy Grover if (!released_regs) { 3014c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 3015c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_n); 3016de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 3017c66ac9dbSNicholas Bellinger } 3018c66ac9dbSNicholas Bellinger /* 3019c66ac9dbSNicholas Bellinger * For an existing all registrants type reservation 3020c66ac9dbSNicholas Bellinger * with a zero SA rservation key, preempt the existing 3021c66ac9dbSNicholas Bellinger * reservation with the new PR type and scope. 3022c66ac9dbSNicholas Bellinger */ 3023c66ac9dbSNicholas Bellinger if (pr_res_holder && all_reg && !(sa_res_key)) { 3024c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_preempt(dev, pr_reg_n, 3025c66ac9dbSNicholas Bellinger (abort) ? &preempt_and_abort_list : NULL, 3026c66ac9dbSNicholas Bellinger type, scope, abort); 3027c66ac9dbSNicholas Bellinger 3028c66ac9dbSNicholas Bellinger if (abort) 3029c66ac9dbSNicholas Bellinger core_scsi3_release_preempt_and_abort( 3030c66ac9dbSNicholas Bellinger &preempt_and_abort_list, pr_reg_n); 3031c66ac9dbSNicholas Bellinger } 3032c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 3033c66ac9dbSNicholas Bellinger 3034c66ac9dbSNicholas Bellinger if (pr_tmpl->pr_aptpl_active) { 3035de103c93SChristoph Hellwig if (!core_scsi3_update_and_write_aptpl(cmd->se_dev, 3036c66ac9dbSNicholas Bellinger &pr_reg_n->pr_aptpl_buf[0], 3037de103c93SChristoph Hellwig pr_tmpl->pr_aptpl_buf_len)) { 30386708bb27SAndy Grover pr_debug("SPC-3 PR: Updated APTPL" 3039c66ac9dbSNicholas Bellinger " metadata for PREEMPT%s\n", (abort) ? 3040c66ac9dbSNicholas Bellinger "_AND_ABORT" : ""); 3041c66ac9dbSNicholas Bellinger } 3042de103c93SChristoph Hellwig } 3043c66ac9dbSNicholas Bellinger 3044c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_n); 30455951146dSAndy Grover core_scsi3_pr_generation(cmd->se_dev); 3046c66ac9dbSNicholas Bellinger return 0; 3047c66ac9dbSNicholas Bellinger } 3048c66ac9dbSNicholas Bellinger /* 3049c66ac9dbSNicholas Bellinger * The PREEMPTing SA reservation key matches that of the 3050c66ac9dbSNicholas Bellinger * existing persistent reservation, first, we check if 3051c66ac9dbSNicholas Bellinger * we are preempting our own reservation. 3052c66ac9dbSNicholas Bellinger * From spc4r17, section 5.7.11.4.3 Preempting 3053c66ac9dbSNicholas Bellinger * persistent reservations and registration handling 3054c66ac9dbSNicholas Bellinger * 3055c66ac9dbSNicholas Bellinger * If an all registrants persistent reservation is not 3056c66ac9dbSNicholas Bellinger * present, it is not an error for the persistent 3057c66ac9dbSNicholas Bellinger * reservation holder to preempt itself (i.e., a 3058c66ac9dbSNicholas Bellinger * PERSISTENT RESERVE OUT with a PREEMPT service action 3059c66ac9dbSNicholas Bellinger * or a PREEMPT AND ABORT service action with the 3060c66ac9dbSNicholas Bellinger * SERVICE ACTION RESERVATION KEY value equal to the 3061c66ac9dbSNicholas Bellinger * persistent reservation holder's reservation key that 3062c66ac9dbSNicholas Bellinger * is received from the persistent reservation holder). 3063c66ac9dbSNicholas Bellinger * In that case, the device server shall establish the 3064c66ac9dbSNicholas Bellinger * new persistent reservation and maintain the 3065c66ac9dbSNicholas Bellinger * registration. 3066c66ac9dbSNicholas Bellinger */ 3067c66ac9dbSNicholas Bellinger prh_type = pr_res_holder->pr_res_type; 3068c66ac9dbSNicholas Bellinger prh_scope = pr_res_holder->pr_res_scope; 3069c66ac9dbSNicholas Bellinger /* 3070c66ac9dbSNicholas Bellinger * If the SERVICE ACTION RESERVATION KEY field identifies a 3071c66ac9dbSNicholas Bellinger * persistent reservation holder (see 5.7.10), the device 3072c66ac9dbSNicholas Bellinger * server shall perform a preempt by doing the following as 3073c66ac9dbSNicholas Bellinger * an uninterrupted series of actions: 3074c66ac9dbSNicholas Bellinger * 3075c66ac9dbSNicholas Bellinger * a) Release the persistent reservation for the holder 3076c66ac9dbSNicholas Bellinger * identified by the SERVICE ACTION RESERVATION KEY field; 3077c66ac9dbSNicholas Bellinger */ 3078c66ac9dbSNicholas Bellinger if (pr_reg_n != pr_res_holder) 3079c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_release(dev, 3080c66ac9dbSNicholas Bellinger pr_res_holder->pr_reg_nacl, 3081c66ac9dbSNicholas Bellinger dev->dev_pr_res_holder, 0); 3082c66ac9dbSNicholas Bellinger /* 3083c66ac9dbSNicholas Bellinger * b) Remove the registrations for all I_T nexuses identified 3084c66ac9dbSNicholas Bellinger * by the SERVICE ACTION RESERVATION KEY field, except the 3085c66ac9dbSNicholas Bellinger * I_T nexus that is being used for the PERSISTENT RESERVE 3086c66ac9dbSNicholas Bellinger * OUT command. If an all registrants persistent reservation 3087c66ac9dbSNicholas Bellinger * is present and the SERVICE ACTION RESERVATION KEY field 3088c66ac9dbSNicholas Bellinger * is set to zero, then all registrations shall be removed 3089c66ac9dbSNicholas Bellinger * except for that of the I_T nexus that is being used for 3090c66ac9dbSNicholas Bellinger * the PERSISTENT RESERVE OUT command; 3091c66ac9dbSNicholas Bellinger */ 3092c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 3093c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 3094c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 3095c66ac9dbSNicholas Bellinger 3096c66ac9dbSNicholas Bellinger calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; 3097c66ac9dbSNicholas Bellinger if (calling_it_nexus) 3098c66ac9dbSNicholas Bellinger continue; 3099c66ac9dbSNicholas Bellinger 3100c66ac9dbSNicholas Bellinger if (pr_reg->pr_res_key != sa_res_key) 3101c66ac9dbSNicholas Bellinger continue; 3102c66ac9dbSNicholas Bellinger 3103c66ac9dbSNicholas Bellinger pr_reg_nacl = pr_reg->pr_reg_nacl; 3104c66ac9dbSNicholas Bellinger pr_res_mapped_lun = pr_reg->pr_res_mapped_lun; 3105c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, pr_reg, 3106c66ac9dbSNicholas Bellinger (abort) ? &preempt_and_abort_list : NULL, 3107c66ac9dbSNicholas Bellinger calling_it_nexus); 3108c66ac9dbSNicholas Bellinger /* 3109c66ac9dbSNicholas Bellinger * e) Establish a unit attention condition for the initiator 3110c66ac9dbSNicholas Bellinger * port associated with every I_T nexus that lost its 3111c66ac9dbSNicholas Bellinger * persistent reservation and/or registration, with the 3112c66ac9dbSNicholas Bellinger * additional sense code set to REGISTRATIONS PREEMPTED; 3113c66ac9dbSNicholas Bellinger */ 3114c66ac9dbSNicholas Bellinger core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 0x2A, 31159e08e34eSMarco Sanvido ASCQ_2AH_REGISTRATIONS_PREEMPTED); 3116c66ac9dbSNicholas Bellinger } 3117c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 3118c66ac9dbSNicholas Bellinger /* 3119c66ac9dbSNicholas Bellinger * c) Establish a persistent reservation for the preempting 3120c66ac9dbSNicholas Bellinger * I_T nexus using the contents of the SCOPE and TYPE fields; 3121c66ac9dbSNicholas Bellinger */ 3122c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_preempt(dev, pr_reg_n, 3123c66ac9dbSNicholas Bellinger (abort) ? &preempt_and_abort_list : NULL, 3124c66ac9dbSNicholas Bellinger type, scope, abort); 3125c66ac9dbSNicholas Bellinger /* 3126c66ac9dbSNicholas Bellinger * d) Process tasks as defined in 5.7.1; 3127c66ac9dbSNicholas Bellinger * e) See above.. 3128c66ac9dbSNicholas Bellinger * f) If the type or scope has changed, then for every I_T nexus 3129c66ac9dbSNicholas Bellinger * whose reservation key was not removed, except for the I_T 3130c66ac9dbSNicholas Bellinger * nexus on which the PERSISTENT RESERVE OUT command was 3131c66ac9dbSNicholas Bellinger * received, the device server shall establish a unit 3132c66ac9dbSNicholas Bellinger * attention condition for the initiator port associated with 3133c66ac9dbSNicholas Bellinger * that I_T nexus, with the additional sense code set to 3134c66ac9dbSNicholas Bellinger * RESERVATIONS RELEASED. If the type or scope have not 3135c66ac9dbSNicholas Bellinger * changed, then no unit attention condition(s) shall be 3136c66ac9dbSNicholas Bellinger * established for this reason. 3137c66ac9dbSNicholas Bellinger */ 3138c66ac9dbSNicholas Bellinger if ((prh_type != type) || (prh_scope != scope)) { 3139c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 3140c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 3141c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 3142c66ac9dbSNicholas Bellinger 3143c66ac9dbSNicholas Bellinger calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0; 3144c66ac9dbSNicholas Bellinger if (calling_it_nexus) 3145c66ac9dbSNicholas Bellinger continue; 3146c66ac9dbSNicholas Bellinger 3147c66ac9dbSNicholas Bellinger core_scsi3_ua_allocate(pr_reg->pr_reg_nacl, 3148c66ac9dbSNicholas Bellinger pr_reg->pr_res_mapped_lun, 0x2A, 3149c66ac9dbSNicholas Bellinger ASCQ_2AH_RESERVATIONS_RELEASED); 3150c66ac9dbSNicholas Bellinger } 3151c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 3152c66ac9dbSNicholas Bellinger } 3153c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 3154c66ac9dbSNicholas Bellinger /* 3155c66ac9dbSNicholas Bellinger * Call LUN_RESET logic upon list of struct t10_pr_registration, 3156c66ac9dbSNicholas Bellinger * All received CDBs for the matching existing reservation and 3157c66ac9dbSNicholas Bellinger * registrations undergo ABORT_TASK logic. 3158c66ac9dbSNicholas Bellinger * 3159c66ac9dbSNicholas Bellinger * From there, core_scsi3_release_preempt_and_abort() will 3160c66ac9dbSNicholas Bellinger * release every registration in the list (which have already 3161c66ac9dbSNicholas Bellinger * been removed from the primary pr_reg list), except the 3162c66ac9dbSNicholas Bellinger * new persistent reservation holder, the calling Initiator Port. 3163c66ac9dbSNicholas Bellinger */ 3164c66ac9dbSNicholas Bellinger if (abort) { 3165c66ac9dbSNicholas Bellinger core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd); 3166c66ac9dbSNicholas Bellinger core_scsi3_release_preempt_and_abort(&preempt_and_abort_list, 3167c66ac9dbSNicholas Bellinger pr_reg_n); 3168c66ac9dbSNicholas Bellinger } 3169c66ac9dbSNicholas Bellinger 3170c66ac9dbSNicholas Bellinger if (pr_tmpl->pr_aptpl_active) { 3171de103c93SChristoph Hellwig if (!core_scsi3_update_and_write_aptpl(cmd->se_dev, 3172c66ac9dbSNicholas Bellinger &pr_reg_n->pr_aptpl_buf[0], 3173de103c93SChristoph Hellwig pr_tmpl->pr_aptpl_buf_len)) { 31746708bb27SAndy Grover pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT" 3175de103c93SChristoph Hellwig "%s\n", abort ? "_AND_ABORT" : ""); 3176de103c93SChristoph Hellwig } 3177c66ac9dbSNicholas Bellinger } 3178c66ac9dbSNicholas Bellinger 3179c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg_n); 31805951146dSAndy Grover core_scsi3_pr_generation(cmd->se_dev); 3181c66ac9dbSNicholas Bellinger return 0; 3182c66ac9dbSNicholas Bellinger } 3183c66ac9dbSNicholas Bellinger 3184de103c93SChristoph Hellwig static sense_reason_t 3185de103c93SChristoph Hellwig core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope, 3186de103c93SChristoph Hellwig u64 res_key, u64 sa_res_key, int abort) 3187c66ac9dbSNicholas Bellinger { 3188c66ac9dbSNicholas Bellinger switch (type) { 3189c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE: 3190c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS: 3191c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_REGONLY: 3192c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY: 3193c66ac9dbSNicholas Bellinger case PR_TYPE_WRITE_EXCLUSIVE_ALLREG: 3194c66ac9dbSNicholas Bellinger case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG: 3195de103c93SChristoph Hellwig return core_scsi3_pro_preempt(cmd, type, scope, res_key, 3196de103c93SChristoph Hellwig sa_res_key, abort); 3197c66ac9dbSNicholas Bellinger default: 31986708bb27SAndy Grover pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s" 3199c66ac9dbSNicholas Bellinger " Type: 0x%02x\n", (abort) ? "_AND_ABORT" : "", type); 3200de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 3201c66ac9dbSNicholas Bellinger } 3202c66ac9dbSNicholas Bellinger } 3203c66ac9dbSNicholas Bellinger 3204c66ac9dbSNicholas Bellinger 3205de103c93SChristoph Hellwig static sense_reason_t 3206de103c93SChristoph Hellwig core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key, 3207de103c93SChristoph Hellwig u64 sa_res_key, int aptpl, int unreg) 3208c66ac9dbSNicholas Bellinger { 3209e3d6f909SAndy Grover struct se_session *se_sess = cmd->se_sess; 32105951146dSAndy Grover struct se_device *dev = cmd->se_dev; 321128168905SJörn Engel struct se_dev_entry *dest_se_deve = NULL; 3212e3d6f909SAndy Grover struct se_lun *se_lun = cmd->se_lun; 3213c66ac9dbSNicholas Bellinger struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL; 3214c66ac9dbSNicholas Bellinger struct se_port *se_port; 3215c66ac9dbSNicholas Bellinger struct se_portal_group *se_tpg, *dest_se_tpg = NULL; 3216c66ac9dbSNicholas Bellinger struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops; 3217c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg; 32180fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 321905d1c7c0SAndy Grover unsigned char *buf; 3220c66ac9dbSNicholas Bellinger unsigned char *initiator_str; 3221c66ac9dbSNicholas Bellinger char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN]; 3222c66ac9dbSNicholas Bellinger u32 tid_len, tmp_tid_len; 3223de103c93SChristoph Hellwig int new_reg = 0, type, scope, matching_iname, prf_isid; 3224de103c93SChristoph Hellwig sense_reason_t ret; 3225c66ac9dbSNicholas Bellinger unsigned short rtpi; 3226c66ac9dbSNicholas Bellinger unsigned char proto_ident; 3227c66ac9dbSNicholas Bellinger 32286708bb27SAndy Grover if (!se_sess || !se_lun) { 32296708bb27SAndy Grover pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n"); 3230de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3231c66ac9dbSNicholas Bellinger } 3232de103c93SChristoph Hellwig 3233c66ac9dbSNicholas Bellinger memset(dest_iport, 0, 64); 3234c66ac9dbSNicholas Bellinger memset(i_buf, 0, PR_REG_ISID_ID_LEN); 3235c66ac9dbSNicholas Bellinger se_tpg = se_sess->se_tpg; 3236e3d6f909SAndy Grover tf_ops = se_tpg->se_tpg_tfo; 3237c66ac9dbSNicholas Bellinger /* 3238c66ac9dbSNicholas Bellinger * Follow logic from spc4r17 Section 5.7.8, Table 50 -- 3239c66ac9dbSNicholas Bellinger * Register behaviors for a REGISTER AND MOVE service action 3240c66ac9dbSNicholas Bellinger * 3241c66ac9dbSNicholas Bellinger * Locate the existing *pr_reg via struct se_node_acl pointers 3242c66ac9dbSNicholas Bellinger */ 32435951146dSAndy Grover pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl, 3244c66ac9dbSNicholas Bellinger se_sess); 32456708bb27SAndy Grover if (!pr_reg) { 32466708bb27SAndy Grover pr_err("SPC-3 PR: Unable to locate PR_REGISTERED" 3247c66ac9dbSNicholas Bellinger " *pr_reg for REGISTER_AND_MOVE\n"); 3248de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3249c66ac9dbSNicholas Bellinger } 3250c66ac9dbSNicholas Bellinger /* 3251c66ac9dbSNicholas Bellinger * The provided reservation key much match the existing reservation key 3252c66ac9dbSNicholas Bellinger * provided during this initiator's I_T nexus registration. 3253c66ac9dbSNicholas Bellinger */ 3254c66ac9dbSNicholas Bellinger if (res_key != pr_reg->pr_res_key) { 32556708bb27SAndy Grover pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received" 3256c66ac9dbSNicholas Bellinger " res_key: 0x%016Lx does not match existing SA REGISTER" 3257c66ac9dbSNicholas Bellinger " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key); 3258de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 3259de103c93SChristoph Hellwig goto out_put_pr_reg; 3260c66ac9dbSNicholas Bellinger } 3261c66ac9dbSNicholas Bellinger /* 3262c66ac9dbSNicholas Bellinger * The service active reservation key needs to be non zero 3263c66ac9dbSNicholas Bellinger */ 32646708bb27SAndy Grover if (!sa_res_key) { 32656708bb27SAndy Grover pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero" 3266c66ac9dbSNicholas Bellinger " sa_res_key\n"); 3267de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3268de103c93SChristoph Hellwig goto out_put_pr_reg; 3269c66ac9dbSNicholas Bellinger } 327005d1c7c0SAndy Grover 3271c66ac9dbSNicholas Bellinger /* 3272c66ac9dbSNicholas Bellinger * Determine the Relative Target Port Identifier where the reservation 3273c66ac9dbSNicholas Bellinger * will be moved to for the TransportID containing SCSI initiator WWN 3274c66ac9dbSNicholas Bellinger * information. 3275c66ac9dbSNicholas Bellinger */ 32764949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 3277de103c93SChristoph Hellwig if (!buf) { 3278de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3279de103c93SChristoph Hellwig goto out_put_pr_reg; 3280de103c93SChristoph Hellwig } 3281de103c93SChristoph Hellwig 3282c66ac9dbSNicholas Bellinger rtpi = (buf[18] & 0xff) << 8; 3283c66ac9dbSNicholas Bellinger rtpi |= buf[19] & 0xff; 3284c66ac9dbSNicholas Bellinger tid_len = (buf[20] & 0xff) << 24; 3285c66ac9dbSNicholas Bellinger tid_len |= (buf[21] & 0xff) << 16; 3286c66ac9dbSNicholas Bellinger tid_len |= (buf[22] & 0xff) << 8; 3287c66ac9dbSNicholas Bellinger tid_len |= buf[23] & 0xff; 32884949314cSAndy Grover transport_kunmap_data_sg(cmd); 328905d1c7c0SAndy Grover buf = NULL; 3290c66ac9dbSNicholas Bellinger 3291c66ac9dbSNicholas Bellinger if ((tid_len + 24) != cmd->data_length) { 32926708bb27SAndy Grover pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header" 3293c66ac9dbSNicholas Bellinger " does not equal CDB data_length: %u\n", tid_len, 3294c66ac9dbSNicholas Bellinger cmd->data_length); 3295de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3296de103c93SChristoph Hellwig goto out_put_pr_reg; 3297c66ac9dbSNicholas Bellinger } 3298c66ac9dbSNicholas Bellinger 3299c66ac9dbSNicholas Bellinger spin_lock(&dev->se_port_lock); 3300c66ac9dbSNicholas Bellinger list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) { 3301c66ac9dbSNicholas Bellinger if (se_port->sep_rtpi != rtpi) 3302c66ac9dbSNicholas Bellinger continue; 3303c66ac9dbSNicholas Bellinger dest_se_tpg = se_port->sep_tpg; 33046708bb27SAndy Grover if (!dest_se_tpg) 3305c66ac9dbSNicholas Bellinger continue; 3306e3d6f909SAndy Grover dest_tf_ops = dest_se_tpg->se_tpg_tfo; 33076708bb27SAndy Grover if (!dest_tf_ops) 3308c66ac9dbSNicholas Bellinger continue; 3309c66ac9dbSNicholas Bellinger 3310c66ac9dbSNicholas Bellinger atomic_inc(&dest_se_tpg->tpg_pr_ref_count); 3311c66ac9dbSNicholas Bellinger smp_mb__after_atomic_inc(); 3312c66ac9dbSNicholas Bellinger spin_unlock(&dev->se_port_lock); 3313c66ac9dbSNicholas Bellinger 3314de103c93SChristoph Hellwig if (core_scsi3_tpg_depend_item(dest_se_tpg)) { 33156708bb27SAndy Grover pr_err("core_scsi3_tpg_depend_item() failed" 3316c66ac9dbSNicholas Bellinger " for dest_se_tpg\n"); 3317c66ac9dbSNicholas Bellinger atomic_dec(&dest_se_tpg->tpg_pr_ref_count); 3318c66ac9dbSNicholas Bellinger smp_mb__after_atomic_dec(); 3319de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3320de103c93SChristoph Hellwig goto out_put_pr_reg; 3321c66ac9dbSNicholas Bellinger } 3322c66ac9dbSNicholas Bellinger 3323c66ac9dbSNicholas Bellinger spin_lock(&dev->se_port_lock); 3324c66ac9dbSNicholas Bellinger break; 3325c66ac9dbSNicholas Bellinger } 3326c66ac9dbSNicholas Bellinger spin_unlock(&dev->se_port_lock); 3327c66ac9dbSNicholas Bellinger 33286708bb27SAndy Grover if (!dest_se_tpg || !dest_tf_ops) { 33296708bb27SAndy Grover pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate" 3330c66ac9dbSNicholas Bellinger " fabric ops from Relative Target Port Identifier:" 3331c66ac9dbSNicholas Bellinger " %hu\n", rtpi); 3332de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3333de103c93SChristoph Hellwig goto out_put_pr_reg; 3334c66ac9dbSNicholas Bellinger } 333505d1c7c0SAndy Grover 33364949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 3337de103c93SChristoph Hellwig if (!buf) { 3338de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3339de103c93SChristoph Hellwig goto out_put_pr_reg; 3340de103c93SChristoph Hellwig } 3341c66ac9dbSNicholas Bellinger proto_ident = (buf[24] & 0x0f); 33428b1e1244SAndy Grover 33436708bb27SAndy Grover pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:" 3344c66ac9dbSNicholas Bellinger " 0x%02x\n", proto_ident); 33458b1e1244SAndy Grover 3346c66ac9dbSNicholas Bellinger if (proto_ident != dest_tf_ops->get_fabric_proto_ident(dest_se_tpg)) { 33476708bb27SAndy Grover pr_err("SPC-3 PR REGISTER_AND_MOVE: Received" 3348c66ac9dbSNicholas Bellinger " proto_ident: 0x%02x does not match ident: 0x%02x" 3349c66ac9dbSNicholas Bellinger " from fabric: %s\n", proto_ident, 3350c66ac9dbSNicholas Bellinger dest_tf_ops->get_fabric_proto_ident(dest_se_tpg), 3351c66ac9dbSNicholas Bellinger dest_tf_ops->get_fabric_name()); 3352de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3353c66ac9dbSNicholas Bellinger goto out; 3354c66ac9dbSNicholas Bellinger } 3355c66ac9dbSNicholas Bellinger if (dest_tf_ops->tpg_parse_pr_out_transport_id == NULL) { 33566708bb27SAndy Grover pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not" 3357c66ac9dbSNicholas Bellinger " containg a valid tpg_parse_pr_out_transport_id" 3358c66ac9dbSNicholas Bellinger " function pointer\n"); 3359de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3360c66ac9dbSNicholas Bellinger goto out; 3361c66ac9dbSNicholas Bellinger } 3362c66ac9dbSNicholas Bellinger initiator_str = dest_tf_ops->tpg_parse_pr_out_transport_id(dest_se_tpg, 3363c66ac9dbSNicholas Bellinger (const char *)&buf[24], &tmp_tid_len, &iport_ptr); 33646708bb27SAndy Grover if (!initiator_str) { 33656708bb27SAndy Grover pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate" 3366c66ac9dbSNicholas Bellinger " initiator_str from Transport ID\n"); 3367de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3368c66ac9dbSNicholas Bellinger goto out; 3369c66ac9dbSNicholas Bellinger } 3370c66ac9dbSNicholas Bellinger 33714949314cSAndy Grover transport_kunmap_data_sg(cmd); 337205d1c7c0SAndy Grover buf = NULL; 337305d1c7c0SAndy Grover 33746708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s" 3375c66ac9dbSNicholas Bellinger " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ? 3376c66ac9dbSNicholas Bellinger "port" : "device", initiator_str, (iport_ptr != NULL) ? 3377c66ac9dbSNicholas Bellinger iport_ptr : ""); 3378c66ac9dbSNicholas Bellinger /* 3379c66ac9dbSNicholas Bellinger * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service 3380c66ac9dbSNicholas Bellinger * action specifies a TransportID that is the same as the initiator port 3381c66ac9dbSNicholas Bellinger * of the I_T nexus for the command received, then the command shall 3382c66ac9dbSNicholas Bellinger * be terminated with CHECK CONDITION status, with the sense key set to 3383c66ac9dbSNicholas Bellinger * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD 3384c66ac9dbSNicholas Bellinger * IN PARAMETER LIST. 3385c66ac9dbSNicholas Bellinger */ 3386c66ac9dbSNicholas Bellinger pr_reg_nacl = pr_reg->pr_reg_nacl; 3387c66ac9dbSNicholas Bellinger matching_iname = (!strcmp(initiator_str, 3388c66ac9dbSNicholas Bellinger pr_reg_nacl->initiatorname)) ? 1 : 0; 33896708bb27SAndy Grover if (!matching_iname) 3390c66ac9dbSNicholas Bellinger goto after_iport_check; 3391c66ac9dbSNicholas Bellinger 33926708bb27SAndy Grover if (!iport_ptr || !pr_reg->isid_present_at_reg) { 33936708bb27SAndy Grover pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s" 3394c66ac9dbSNicholas Bellinger " matches: %s on received I_T Nexus\n", initiator_str, 3395c66ac9dbSNicholas Bellinger pr_reg_nacl->initiatorname); 3396de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3397c66ac9dbSNicholas Bellinger goto out; 3398c66ac9dbSNicholas Bellinger } 33996708bb27SAndy Grover if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) { 34006708bb27SAndy Grover pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s" 3401c66ac9dbSNicholas Bellinger " matches: %s %s on received I_T Nexus\n", 3402c66ac9dbSNicholas Bellinger initiator_str, iport_ptr, pr_reg_nacl->initiatorname, 3403c66ac9dbSNicholas Bellinger pr_reg->pr_reg_isid); 3404de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3405c66ac9dbSNicholas Bellinger goto out; 3406c66ac9dbSNicholas Bellinger } 3407c66ac9dbSNicholas Bellinger after_iport_check: 3408c66ac9dbSNicholas Bellinger /* 3409c66ac9dbSNicholas Bellinger * Locate the destination struct se_node_acl from the received Transport ID 3410c66ac9dbSNicholas Bellinger */ 341128638887SRoland Dreier spin_lock_irq(&dest_se_tpg->acl_node_lock); 3412c66ac9dbSNicholas Bellinger dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg, 3413c66ac9dbSNicholas Bellinger initiator_str); 3414c66ac9dbSNicholas Bellinger if (dest_node_acl) { 3415c66ac9dbSNicholas Bellinger atomic_inc(&dest_node_acl->acl_pr_ref_count); 3416c66ac9dbSNicholas Bellinger smp_mb__after_atomic_inc(); 3417c66ac9dbSNicholas Bellinger } 341828638887SRoland Dreier spin_unlock_irq(&dest_se_tpg->acl_node_lock); 3419c66ac9dbSNicholas Bellinger 34206708bb27SAndy Grover if (!dest_node_acl) { 34216708bb27SAndy Grover pr_err("Unable to locate %s dest_node_acl for" 3422c66ac9dbSNicholas Bellinger " TransportID%s\n", dest_tf_ops->get_fabric_name(), 3423c66ac9dbSNicholas Bellinger initiator_str); 3424de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3425c66ac9dbSNicholas Bellinger goto out; 3426c66ac9dbSNicholas Bellinger } 3427de103c93SChristoph Hellwig 3428de103c93SChristoph Hellwig if (core_scsi3_nodeacl_depend_item(dest_node_acl)) { 34296708bb27SAndy Grover pr_err("core_scsi3_nodeacl_depend_item() for" 3430c66ac9dbSNicholas Bellinger " dest_node_acl\n"); 3431c66ac9dbSNicholas Bellinger atomic_dec(&dest_node_acl->acl_pr_ref_count); 3432c66ac9dbSNicholas Bellinger smp_mb__after_atomic_dec(); 3433c66ac9dbSNicholas Bellinger dest_node_acl = NULL; 3434de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3435c66ac9dbSNicholas Bellinger goto out; 3436c66ac9dbSNicholas Bellinger } 34378b1e1244SAndy Grover 34386708bb27SAndy Grover pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:" 3439c66ac9dbSNicholas Bellinger " %s from TransportID\n", dest_tf_ops->get_fabric_name(), 3440c66ac9dbSNicholas Bellinger dest_node_acl->initiatorname); 34418b1e1244SAndy Grover 3442c66ac9dbSNicholas Bellinger /* 3443c66ac9dbSNicholas Bellinger * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET 3444c66ac9dbSNicholas Bellinger * PORT IDENTIFIER. 3445c66ac9dbSNicholas Bellinger */ 3446c66ac9dbSNicholas Bellinger dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi); 34476708bb27SAndy Grover if (!dest_se_deve) { 34486708bb27SAndy Grover pr_err("Unable to locate %s dest_se_deve from RTPI:" 3449c66ac9dbSNicholas Bellinger " %hu\n", dest_tf_ops->get_fabric_name(), rtpi); 3450de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3451c66ac9dbSNicholas Bellinger goto out; 3452c66ac9dbSNicholas Bellinger } 3453c66ac9dbSNicholas Bellinger 3454de103c93SChristoph Hellwig if (core_scsi3_lunacl_depend_item(dest_se_deve)) { 34556708bb27SAndy Grover pr_err("core_scsi3_lunacl_depend_item() failed\n"); 3456c66ac9dbSNicholas Bellinger atomic_dec(&dest_se_deve->pr_ref_count); 3457c66ac9dbSNicholas Bellinger smp_mb__after_atomic_dec(); 3458c66ac9dbSNicholas Bellinger dest_se_deve = NULL; 3459de103c93SChristoph Hellwig ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3460c66ac9dbSNicholas Bellinger goto out; 3461c66ac9dbSNicholas Bellinger } 34628b1e1244SAndy Grover 34636708bb27SAndy Grover pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN" 3464c66ac9dbSNicholas Bellinger " ACL for dest_se_deve->mapped_lun: %u\n", 3465c66ac9dbSNicholas Bellinger dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname, 3466c66ac9dbSNicholas Bellinger dest_se_deve->mapped_lun); 34678b1e1244SAndy Grover 3468c66ac9dbSNicholas Bellinger /* 3469c66ac9dbSNicholas Bellinger * A persistent reservation needs to already existing in order to 3470c66ac9dbSNicholas Bellinger * successfully complete the REGISTER_AND_MOVE service action.. 3471c66ac9dbSNicholas Bellinger */ 3472c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock); 3473c66ac9dbSNicholas Bellinger pr_res_holder = dev->dev_pr_res_holder; 34746708bb27SAndy Grover if (!pr_res_holder) { 34756708bb27SAndy Grover pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation" 3476c66ac9dbSNicholas Bellinger " currently held\n"); 3477c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 3478de103c93SChristoph Hellwig ret = TCM_INVALID_CDB_FIELD; 3479c66ac9dbSNicholas Bellinger goto out; 3480c66ac9dbSNicholas Bellinger } 3481c66ac9dbSNicholas Bellinger /* 3482c66ac9dbSNicholas Bellinger * The received on I_T Nexus must be the reservation holder. 3483c66ac9dbSNicholas Bellinger * 3484c66ac9dbSNicholas Bellinger * From spc4r17 section 5.7.8 Table 50 -- 3485c66ac9dbSNicholas Bellinger * Register behaviors for a REGISTER AND MOVE service action 3486c66ac9dbSNicholas Bellinger */ 3487c66ac9dbSNicholas Bellinger if (pr_res_holder != pr_reg) { 34886708bb27SAndy Grover pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T" 3489c66ac9dbSNicholas Bellinger " Nexus is not reservation holder\n"); 3490c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 3491de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 3492c66ac9dbSNicholas Bellinger goto out; 3493c66ac9dbSNicholas Bellinger } 3494c66ac9dbSNicholas Bellinger /* 3495c66ac9dbSNicholas Bellinger * From spc4r17 section 5.7.8: registering and moving reservation 3496c66ac9dbSNicholas Bellinger * 3497c66ac9dbSNicholas Bellinger * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service 3498c66ac9dbSNicholas Bellinger * action is received and the established persistent reservation is a 3499c66ac9dbSNicholas Bellinger * Write Exclusive - All Registrants type or Exclusive Access - 3500c66ac9dbSNicholas Bellinger * All Registrants type reservation, then the command shall be completed 3501c66ac9dbSNicholas Bellinger * with RESERVATION CONFLICT status. 3502c66ac9dbSNicholas Bellinger */ 3503c66ac9dbSNicholas Bellinger if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || 3504c66ac9dbSNicholas Bellinger (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) { 35056708bb27SAndy Grover pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move" 3506c66ac9dbSNicholas Bellinger " reservation for type: %s\n", 3507c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(pr_res_holder->pr_res_type)); 3508c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 3509de103c93SChristoph Hellwig ret = TCM_RESERVATION_CONFLICT; 3510c66ac9dbSNicholas Bellinger goto out; 3511c66ac9dbSNicholas Bellinger } 3512c66ac9dbSNicholas Bellinger pr_res_nacl = pr_res_holder->pr_reg_nacl; 3513c66ac9dbSNicholas Bellinger /* 3514c66ac9dbSNicholas Bellinger * b) Ignore the contents of the (received) SCOPE and TYPE fields; 3515c66ac9dbSNicholas Bellinger */ 3516c66ac9dbSNicholas Bellinger type = pr_res_holder->pr_res_type; 3517c66ac9dbSNicholas Bellinger scope = pr_res_holder->pr_res_type; 3518c66ac9dbSNicholas Bellinger /* 3519c66ac9dbSNicholas Bellinger * c) Associate the reservation key specified in the SERVICE ACTION 3520c66ac9dbSNicholas Bellinger * RESERVATION KEY field with the I_T nexus specified as the 3521c66ac9dbSNicholas Bellinger * destination of the register and move, where: 3522c66ac9dbSNicholas Bellinger * A) The I_T nexus is specified by the TransportID and the 3523c66ac9dbSNicholas Bellinger * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and 3524c66ac9dbSNicholas Bellinger * B) Regardless of the TransportID format used, the association for 3525c66ac9dbSNicholas Bellinger * the initiator port is based on either the initiator port name 3526c66ac9dbSNicholas Bellinger * (see 3.1.71) on SCSI transport protocols where port names are 3527c66ac9dbSNicholas Bellinger * required or the initiator port identifier (see 3.1.70) on SCSI 3528c66ac9dbSNicholas Bellinger * transport protocols where port names are not required; 3529c66ac9dbSNicholas Bellinger * d) Register the reservation key specified in the SERVICE ACTION 3530c66ac9dbSNicholas Bellinger * RESERVATION KEY field; 3531c66ac9dbSNicholas Bellinger * e) Retain the reservation key specified in the SERVICE ACTION 3532c66ac9dbSNicholas Bellinger * RESERVATION KEY field and associated information; 3533c66ac9dbSNicholas Bellinger * 3534c66ac9dbSNicholas Bellinger * Also, It is not an error for a REGISTER AND MOVE service action to 3535c66ac9dbSNicholas Bellinger * register an I_T nexus that is already registered with the same 3536c66ac9dbSNicholas Bellinger * reservation key or a different reservation key. 3537c66ac9dbSNicholas Bellinger */ 3538c66ac9dbSNicholas Bellinger dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl, 3539c66ac9dbSNicholas Bellinger iport_ptr); 35406708bb27SAndy Grover if (!dest_pr_reg) { 3541de103c93SChristoph Hellwig if (core_scsi3_alloc_registration(cmd->se_dev, 3542c66ac9dbSNicholas Bellinger dest_node_acl, dest_se_deve, iport_ptr, 3543de103c93SChristoph Hellwig sa_res_key, 0, aptpl, 2, 1)) { 3544c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 3545de103c93SChristoph Hellwig ret = TCM_INVALID_PARAMETER_LIST; 3546c66ac9dbSNicholas Bellinger goto out; 3547c66ac9dbSNicholas Bellinger } 3548c66ac9dbSNicholas Bellinger dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl, 3549c66ac9dbSNicholas Bellinger iport_ptr); 3550c66ac9dbSNicholas Bellinger new_reg = 1; 3551c66ac9dbSNicholas Bellinger } 3552c66ac9dbSNicholas Bellinger /* 3553c66ac9dbSNicholas Bellinger * f) Release the persistent reservation for the persistent reservation 3554c66ac9dbSNicholas Bellinger * holder (i.e., the I_T nexus on which the 3555c66ac9dbSNicholas Bellinger */ 3556c66ac9dbSNicholas Bellinger __core_scsi3_complete_pro_release(dev, pr_res_nacl, 3557c66ac9dbSNicholas Bellinger dev->dev_pr_res_holder, 0); 3558c66ac9dbSNicholas Bellinger /* 3559c66ac9dbSNicholas Bellinger * g) Move the persistent reservation to the specified I_T nexus using 3560c66ac9dbSNicholas Bellinger * the same scope and type as the persistent reservation released in 3561c66ac9dbSNicholas Bellinger * item f); and 3562c66ac9dbSNicholas Bellinger */ 3563c66ac9dbSNicholas Bellinger dev->dev_pr_res_holder = dest_pr_reg; 3564c66ac9dbSNicholas Bellinger dest_pr_reg->pr_res_holder = 1; 3565c66ac9dbSNicholas Bellinger dest_pr_reg->pr_res_type = type; 3566c66ac9dbSNicholas Bellinger pr_reg->pr_res_scope = scope; 3567c66ac9dbSNicholas Bellinger prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0], 3568c66ac9dbSNicholas Bellinger PR_REG_ISID_ID_LEN); 3569c66ac9dbSNicholas Bellinger /* 3570c66ac9dbSNicholas Bellinger * Increment PRGeneration for existing registrations.. 3571c66ac9dbSNicholas Bellinger */ 35726708bb27SAndy Grover if (!new_reg) 3573c66ac9dbSNicholas Bellinger dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++; 3574c66ac9dbSNicholas Bellinger spin_unlock(&dev->dev_reservation_lock); 3575c66ac9dbSNicholas Bellinger 35766708bb27SAndy Grover pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE" 3577c66ac9dbSNicholas Bellinger " created new reservation holder TYPE: %s on object RTPI:" 3578c66ac9dbSNicholas Bellinger " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(), 3579c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(type), rtpi, 3580c66ac9dbSNicholas Bellinger dest_pr_reg->pr_res_generation); 35816708bb27SAndy Grover pr_debug("SPC-3 PR Successfully moved reservation from" 3582c66ac9dbSNicholas Bellinger " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n", 3583c66ac9dbSNicholas Bellinger tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname, 3584c66ac9dbSNicholas Bellinger (prf_isid) ? &i_buf[0] : "", dest_tf_ops->get_fabric_name(), 3585c66ac9dbSNicholas Bellinger dest_node_acl->initiatorname, (iport_ptr != NULL) ? 3586c66ac9dbSNicholas Bellinger iport_ptr : ""); 3587c66ac9dbSNicholas Bellinger /* 3588c66ac9dbSNicholas Bellinger * It is now safe to release configfs group dependencies for destination 3589c66ac9dbSNicholas Bellinger * of Transport ID Initiator Device/Port Identifier 3590c66ac9dbSNicholas Bellinger */ 3591c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(dest_se_deve); 3592c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 3593c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_se_tpg); 3594c66ac9dbSNicholas Bellinger /* 3595c66ac9dbSNicholas Bellinger * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T 3596c66ac9dbSNicholas Bellinger * nexus on which PERSISTENT RESERVE OUT command was received. 3597c66ac9dbSNicholas Bellinger */ 3598c66ac9dbSNicholas Bellinger if (unreg) { 3599c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 3600c66ac9dbSNicholas Bellinger __core_scsi3_free_registration(dev, pr_reg, NULL, 1); 3601c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 3602c66ac9dbSNicholas Bellinger } else 3603c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg); 3604c66ac9dbSNicholas Bellinger 3605c66ac9dbSNicholas Bellinger /* 3606c66ac9dbSNicholas Bellinger * Clear the APTPL metadata if APTPL has been disabled, otherwise 3607c66ac9dbSNicholas Bellinger * write out the updated metadata to struct file for this SCSI device. 3608c66ac9dbSNicholas Bellinger */ 36096708bb27SAndy Grover if (!aptpl) { 3610c66ac9dbSNicholas Bellinger pr_tmpl->pr_aptpl_active = 0; 36115951146dSAndy Grover core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0); 36126708bb27SAndy Grover pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for" 3613c66ac9dbSNicholas Bellinger " REGISTER_AND_MOVE\n"); 3614c66ac9dbSNicholas Bellinger } else { 3615c66ac9dbSNicholas Bellinger pr_tmpl->pr_aptpl_active = 1; 3616de103c93SChristoph Hellwig if (!core_scsi3_update_and_write_aptpl(cmd->se_dev, 3617c66ac9dbSNicholas Bellinger &dest_pr_reg->pr_aptpl_buf[0], 3618de103c93SChristoph Hellwig pr_tmpl->pr_aptpl_buf_len)) { 36196708bb27SAndy Grover pr_debug("SPC-3 PR: Set APTPL Bit Activated for" 3620c66ac9dbSNicholas Bellinger " REGISTER_AND_MOVE\n"); 3621c66ac9dbSNicholas Bellinger } 3622de103c93SChristoph Hellwig } 3623c66ac9dbSNicholas Bellinger 36244949314cSAndy Grover transport_kunmap_data_sg(cmd); 362505d1c7c0SAndy Grover 3626c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(dest_pr_reg); 3627c66ac9dbSNicholas Bellinger return 0; 3628c66ac9dbSNicholas Bellinger out: 362905d1c7c0SAndy Grover if (buf) 36304949314cSAndy Grover transport_kunmap_data_sg(cmd); 3631c66ac9dbSNicholas Bellinger if (dest_se_deve) 3632c66ac9dbSNicholas Bellinger core_scsi3_lunacl_undepend_item(dest_se_deve); 3633c66ac9dbSNicholas Bellinger if (dest_node_acl) 3634c66ac9dbSNicholas Bellinger core_scsi3_nodeacl_undepend_item(dest_node_acl); 3635c66ac9dbSNicholas Bellinger core_scsi3_tpg_undepend_item(dest_se_tpg); 3636de103c93SChristoph Hellwig 3637de103c93SChristoph Hellwig out_put_pr_reg: 3638c66ac9dbSNicholas Bellinger core_scsi3_put_pr_reg(pr_reg); 3639c66ac9dbSNicholas Bellinger return ret; 3640c66ac9dbSNicholas Bellinger } 3641c66ac9dbSNicholas Bellinger 3642c66ac9dbSNicholas Bellinger static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb) 3643c66ac9dbSNicholas Bellinger { 3644c66ac9dbSNicholas Bellinger unsigned int __v1, __v2; 3645c66ac9dbSNicholas Bellinger 3646c66ac9dbSNicholas Bellinger __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3]; 3647c66ac9dbSNicholas Bellinger __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7]; 3648c66ac9dbSNicholas Bellinger 3649c66ac9dbSNicholas Bellinger return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32; 3650c66ac9dbSNicholas Bellinger } 3651c66ac9dbSNicholas Bellinger 3652c66ac9dbSNicholas Bellinger /* 3653c66ac9dbSNicholas Bellinger * See spc4r17 section 6.14 Table 170 3654c66ac9dbSNicholas Bellinger */ 3655de103c93SChristoph Hellwig sense_reason_t 3656de103c93SChristoph Hellwig target_scsi3_emulate_pr_out(struct se_cmd *cmd) 3657c66ac9dbSNicholas Bellinger { 3658617c0e06SChristoph Hellwig unsigned char *cdb = &cmd->t_task_cdb[0]; 365905d1c7c0SAndy Grover unsigned char *buf; 3660c66ac9dbSNicholas Bellinger u64 res_key, sa_res_key; 3661c66ac9dbSNicholas Bellinger int sa, scope, type, aptpl; 3662c66ac9dbSNicholas Bellinger int spec_i_pt = 0, all_tg_pt = 0, unreg = 0; 3663de103c93SChristoph Hellwig sense_reason_t ret; 3664617c0e06SChristoph Hellwig 3665617c0e06SChristoph Hellwig /* 3666617c0e06SChristoph Hellwig * Following spc2r20 5.5.1 Reservations overview: 3667617c0e06SChristoph Hellwig * 3668617c0e06SChristoph Hellwig * If a logical unit has been reserved by any RESERVE command and is 3669617c0e06SChristoph Hellwig * still reserved by any initiator, all PERSISTENT RESERVE IN and all 3670617c0e06SChristoph Hellwig * PERSISTENT RESERVE OUT commands shall conflict regardless of 3671617c0e06SChristoph Hellwig * initiator or service action and shall terminate with a RESERVATION 3672617c0e06SChristoph Hellwig * CONFLICT status. 3673617c0e06SChristoph Hellwig */ 36740fd97ccfSChristoph Hellwig if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) { 3675617c0e06SChristoph Hellwig pr_err("Received PERSISTENT_RESERVE CDB while legacy" 3676617c0e06SChristoph Hellwig " SPC-2 reservation is held, returning" 3677617c0e06SChristoph Hellwig " RESERVATION_CONFLICT\n"); 3678de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 3679617c0e06SChristoph Hellwig } 3680617c0e06SChristoph Hellwig 3681c66ac9dbSNicholas Bellinger /* 3682c66ac9dbSNicholas Bellinger * FIXME: A NULL struct se_session pointer means an this is not coming from 3683c66ac9dbSNicholas Bellinger * a $FABRIC_MOD's nexus, but from internal passthrough ops. 3684c66ac9dbSNicholas Bellinger */ 3685de103c93SChristoph Hellwig if (!cmd->se_sess) 3686de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3687c66ac9dbSNicholas Bellinger 3688c66ac9dbSNicholas Bellinger if (cmd->data_length < 24) { 36896708bb27SAndy Grover pr_warn("SPC-PR: Received PR OUT parameter list" 3690c66ac9dbSNicholas Bellinger " length too small: %u\n", cmd->data_length); 3691de103c93SChristoph Hellwig return TCM_INVALID_PARAMETER_LIST; 3692c66ac9dbSNicholas Bellinger } 3693de103c93SChristoph Hellwig 3694c66ac9dbSNicholas Bellinger /* 3695c66ac9dbSNicholas Bellinger * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB) 3696c66ac9dbSNicholas Bellinger */ 3697c66ac9dbSNicholas Bellinger sa = (cdb[1] & 0x1f); 3698c66ac9dbSNicholas Bellinger scope = (cdb[2] & 0xf0); 3699c66ac9dbSNicholas Bellinger type = (cdb[2] & 0x0f); 370005d1c7c0SAndy Grover 37014949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 3702de103c93SChristoph Hellwig if (!buf) 3703de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3704de103c93SChristoph Hellwig 3705c66ac9dbSNicholas Bellinger /* 3706c66ac9dbSNicholas Bellinger * From PERSISTENT_RESERVE_OUT parameter list (payload) 3707c66ac9dbSNicholas Bellinger */ 3708c66ac9dbSNicholas Bellinger res_key = core_scsi3_extract_reservation_key(&buf[0]); 3709c66ac9dbSNicholas Bellinger sa_res_key = core_scsi3_extract_reservation_key(&buf[8]); 3710c66ac9dbSNicholas Bellinger /* 3711c66ac9dbSNicholas Bellinger * REGISTER_AND_MOVE uses a different SA parameter list containing 3712c66ac9dbSNicholas Bellinger * SCSI TransportIDs. 3713c66ac9dbSNicholas Bellinger */ 3714c66ac9dbSNicholas Bellinger if (sa != PRO_REGISTER_AND_MOVE) { 3715c66ac9dbSNicholas Bellinger spec_i_pt = (buf[20] & 0x08); 3716c66ac9dbSNicholas Bellinger all_tg_pt = (buf[20] & 0x04); 3717c66ac9dbSNicholas Bellinger aptpl = (buf[20] & 0x01); 3718c66ac9dbSNicholas Bellinger } else { 3719c66ac9dbSNicholas Bellinger aptpl = (buf[17] & 0x01); 3720c66ac9dbSNicholas Bellinger unreg = (buf[17] & 0x02); 3721c66ac9dbSNicholas Bellinger } 37224949314cSAndy Grover transport_kunmap_data_sg(cmd); 372305d1c7c0SAndy Grover buf = NULL; 372405d1c7c0SAndy Grover 3725c66ac9dbSNicholas Bellinger /* 3726c66ac9dbSNicholas Bellinger * SPEC_I_PT=1 is only valid for Service action: REGISTER 3727c66ac9dbSNicholas Bellinger */ 3728de103c93SChristoph Hellwig if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER)) 3729de103c93SChristoph Hellwig return TCM_INVALID_PARAMETER_LIST; 3730d29a5b6aSChristoph Hellwig 3731c66ac9dbSNicholas Bellinger /* 3732c66ac9dbSNicholas Bellinger * From spc4r17 section 6.14: 3733c66ac9dbSNicholas Bellinger * 3734c66ac9dbSNicholas Bellinger * If the SPEC_I_PT bit is set to zero, the service action is not 3735c66ac9dbSNicholas Bellinger * REGISTER AND MOVE, and the parameter list length is not 24, then 3736c66ac9dbSNicholas Bellinger * the command shall be terminated with CHECK CONDITION status, with 3737c66ac9dbSNicholas Bellinger * the sense key set to ILLEGAL REQUEST, and the additional sense 3738c66ac9dbSNicholas Bellinger * code set to PARAMETER LIST LENGTH ERROR. 3739c66ac9dbSNicholas Bellinger */ 37406708bb27SAndy Grover if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) && 3741c66ac9dbSNicholas Bellinger (cmd->data_length != 24)) { 37426708bb27SAndy Grover pr_warn("SPC-PR: Received PR OUT illegal parameter" 3743c66ac9dbSNicholas Bellinger " list length: %u\n", cmd->data_length); 3744de103c93SChristoph Hellwig return TCM_INVALID_PARAMETER_LIST; 3745c66ac9dbSNicholas Bellinger } 3746de103c93SChristoph Hellwig 3747c66ac9dbSNicholas Bellinger /* 3748c66ac9dbSNicholas Bellinger * (core_scsi3_emulate_pro_* function parameters 3749c66ac9dbSNicholas Bellinger * are defined by spc4r17 Table 174: 3750c66ac9dbSNicholas Bellinger * PERSISTENT_RESERVE_OUT service actions and valid parameters. 3751c66ac9dbSNicholas Bellinger */ 3752c66ac9dbSNicholas Bellinger switch (sa) { 3753c66ac9dbSNicholas Bellinger case PRO_REGISTER: 3754d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_register(cmd, 3755c66ac9dbSNicholas Bellinger res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 0); 3756d29a5b6aSChristoph Hellwig break; 3757c66ac9dbSNicholas Bellinger case PRO_RESERVE: 3758d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key); 3759d29a5b6aSChristoph Hellwig break; 3760c66ac9dbSNicholas Bellinger case PRO_RELEASE: 3761d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key); 3762d29a5b6aSChristoph Hellwig break; 3763c66ac9dbSNicholas Bellinger case PRO_CLEAR: 3764d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_clear(cmd, res_key); 3765d29a5b6aSChristoph Hellwig break; 3766c66ac9dbSNicholas Bellinger case PRO_PREEMPT: 3767d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_preempt(cmd, type, scope, 3768c66ac9dbSNicholas Bellinger res_key, sa_res_key, 0); 3769d29a5b6aSChristoph Hellwig break; 3770c66ac9dbSNicholas Bellinger case PRO_PREEMPT_AND_ABORT: 3771d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_preempt(cmd, type, scope, 3772c66ac9dbSNicholas Bellinger res_key, sa_res_key, 1); 3773d29a5b6aSChristoph Hellwig break; 3774c66ac9dbSNicholas Bellinger case PRO_REGISTER_AND_IGNORE_EXISTING_KEY: 3775d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_register(cmd, 3776c66ac9dbSNicholas Bellinger 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 1); 3777d29a5b6aSChristoph Hellwig break; 3778c66ac9dbSNicholas Bellinger case PRO_REGISTER_AND_MOVE: 3779d29a5b6aSChristoph Hellwig ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key, 3780c66ac9dbSNicholas Bellinger sa_res_key, aptpl, unreg); 3781d29a5b6aSChristoph Hellwig break; 3782c66ac9dbSNicholas Bellinger default: 37836708bb27SAndy Grover pr_err("Unknown PERSISTENT_RESERVE_OUT service" 3784c66ac9dbSNicholas Bellinger " action: 0x%02x\n", cdb[1] & 0x1f); 3785de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 3786c66ac9dbSNicholas Bellinger } 3787c66ac9dbSNicholas Bellinger 37886bb35e00SChristoph Hellwig if (!ret) 37896bb35e00SChristoph Hellwig target_complete_cmd(cmd, GOOD); 3790d29a5b6aSChristoph Hellwig return ret; 3791c66ac9dbSNicholas Bellinger } 3792c66ac9dbSNicholas Bellinger 3793c66ac9dbSNicholas Bellinger /* 3794c66ac9dbSNicholas Bellinger * PERSISTENT_RESERVE_IN Service Action READ_KEYS 3795c66ac9dbSNicholas Bellinger * 3796c66ac9dbSNicholas Bellinger * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160 3797c66ac9dbSNicholas Bellinger */ 3798de103c93SChristoph Hellwig static sense_reason_t 3799de103c93SChristoph Hellwig core_scsi3_pri_read_keys(struct se_cmd *cmd) 3800c66ac9dbSNicholas Bellinger { 38010fd97ccfSChristoph Hellwig struct se_device *dev = cmd->se_dev; 3802c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg; 380305d1c7c0SAndy Grover unsigned char *buf; 3804c66ac9dbSNicholas Bellinger u32 add_len = 0, off = 8; 3805c66ac9dbSNicholas Bellinger 3806c66ac9dbSNicholas Bellinger if (cmd->data_length < 8) { 38076708bb27SAndy Grover pr_err("PRIN SA READ_KEYS SCSI Data Length: %u" 3808c66ac9dbSNicholas Bellinger " too small\n", cmd->data_length); 3809de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 3810c66ac9dbSNicholas Bellinger } 3811c66ac9dbSNicholas Bellinger 38124949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 3813de103c93SChristoph Hellwig if (!buf) 3814de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3815de103c93SChristoph Hellwig 38160fd97ccfSChristoph Hellwig buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff); 38170fd97ccfSChristoph Hellwig buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff); 38180fd97ccfSChristoph Hellwig buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff); 38190fd97ccfSChristoph Hellwig buf[3] = (dev->t10_pr.pr_generation & 0xff); 3820c66ac9dbSNicholas Bellinger 38210fd97ccfSChristoph Hellwig spin_lock(&dev->t10_pr.registration_lock); 38220fd97ccfSChristoph Hellwig list_for_each_entry(pr_reg, &dev->t10_pr.registration_list, 3823c66ac9dbSNicholas Bellinger pr_reg_list) { 3824c66ac9dbSNicholas Bellinger /* 3825c66ac9dbSNicholas Bellinger * Check for overflow of 8byte PRI READ_KEYS payload and 3826c66ac9dbSNicholas Bellinger * next reservation key list descriptor. 3827c66ac9dbSNicholas Bellinger */ 3828c66ac9dbSNicholas Bellinger if ((add_len + 8) > (cmd->data_length - 8)) 3829c66ac9dbSNicholas Bellinger break; 3830c66ac9dbSNicholas Bellinger 3831c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff); 3832c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff); 3833c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff); 3834c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff); 3835c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff); 3836c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff); 3837c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff); 3838c66ac9dbSNicholas Bellinger buf[off++] = (pr_reg->pr_res_key & 0xff); 3839c66ac9dbSNicholas Bellinger 3840c66ac9dbSNicholas Bellinger add_len += 8; 3841c66ac9dbSNicholas Bellinger } 38420fd97ccfSChristoph Hellwig spin_unlock(&dev->t10_pr.registration_lock); 3843c66ac9dbSNicholas Bellinger 3844c66ac9dbSNicholas Bellinger buf[4] = ((add_len >> 24) & 0xff); 3845c66ac9dbSNicholas Bellinger buf[5] = ((add_len >> 16) & 0xff); 3846c66ac9dbSNicholas Bellinger buf[6] = ((add_len >> 8) & 0xff); 3847c66ac9dbSNicholas Bellinger buf[7] = (add_len & 0xff); 3848c66ac9dbSNicholas Bellinger 38494949314cSAndy Grover transport_kunmap_data_sg(cmd); 385005d1c7c0SAndy Grover 3851c66ac9dbSNicholas Bellinger return 0; 3852c66ac9dbSNicholas Bellinger } 3853c66ac9dbSNicholas Bellinger 3854c66ac9dbSNicholas Bellinger /* 3855c66ac9dbSNicholas Bellinger * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION 3856c66ac9dbSNicholas Bellinger * 3857c66ac9dbSNicholas Bellinger * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162 3858c66ac9dbSNicholas Bellinger */ 3859de103c93SChristoph Hellwig static sense_reason_t 3860de103c93SChristoph Hellwig core_scsi3_pri_read_reservation(struct se_cmd *cmd) 3861c66ac9dbSNicholas Bellinger { 38620fd97ccfSChristoph Hellwig struct se_device *dev = cmd->se_dev; 3863c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg; 386405d1c7c0SAndy Grover unsigned char *buf; 3865c66ac9dbSNicholas Bellinger u64 pr_res_key; 3866c66ac9dbSNicholas Bellinger u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */ 3867c66ac9dbSNicholas Bellinger 3868c66ac9dbSNicholas Bellinger if (cmd->data_length < 8) { 38696708bb27SAndy Grover pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u" 3870c66ac9dbSNicholas Bellinger " too small\n", cmd->data_length); 3871de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 3872c66ac9dbSNicholas Bellinger } 3873c66ac9dbSNicholas Bellinger 38744949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 3875de103c93SChristoph Hellwig if (!buf) 3876de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3877de103c93SChristoph Hellwig 38780fd97ccfSChristoph Hellwig buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff); 38790fd97ccfSChristoph Hellwig buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff); 38800fd97ccfSChristoph Hellwig buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff); 38810fd97ccfSChristoph Hellwig buf[3] = (dev->t10_pr.pr_generation & 0xff); 3882c66ac9dbSNicholas Bellinger 38830fd97ccfSChristoph Hellwig spin_lock(&dev->dev_reservation_lock); 38840fd97ccfSChristoph Hellwig pr_reg = dev->dev_pr_res_holder; 3885ee1b1b9cSAndy Grover if (pr_reg) { 3886c66ac9dbSNicholas Bellinger /* 3887c66ac9dbSNicholas Bellinger * Set the hardcoded Additional Length 3888c66ac9dbSNicholas Bellinger */ 3889c66ac9dbSNicholas Bellinger buf[4] = ((add_len >> 24) & 0xff); 3890c66ac9dbSNicholas Bellinger buf[5] = ((add_len >> 16) & 0xff); 3891c66ac9dbSNicholas Bellinger buf[6] = ((add_len >> 8) & 0xff); 3892c66ac9dbSNicholas Bellinger buf[7] = (add_len & 0xff); 3893c66ac9dbSNicholas Bellinger 389405d1c7c0SAndy Grover if (cmd->data_length < 22) 389505d1c7c0SAndy Grover goto err; 389605d1c7c0SAndy Grover 3897c66ac9dbSNicholas Bellinger /* 3898c66ac9dbSNicholas Bellinger * Set the Reservation key. 3899c66ac9dbSNicholas Bellinger * 3900c66ac9dbSNicholas Bellinger * From spc4r17, section 5.7.10: 3901c66ac9dbSNicholas Bellinger * A persistent reservation holder has its reservation key 3902c66ac9dbSNicholas Bellinger * returned in the parameter data from a PERSISTENT 3903c66ac9dbSNicholas Bellinger * RESERVE IN command with READ RESERVATION service action as 3904c66ac9dbSNicholas Bellinger * follows: 3905c66ac9dbSNicholas Bellinger * a) For a persistent reservation of the type Write Exclusive 3906c66ac9dbSNicholas Bellinger * - All Registrants or Exclusive Access All Regitrants, 3907c66ac9dbSNicholas Bellinger * the reservation key shall be set to zero; or 3908c66ac9dbSNicholas Bellinger * b) For all other persistent reservation types, the 3909c66ac9dbSNicholas Bellinger * reservation key shall be set to the registered 3910c66ac9dbSNicholas Bellinger * reservation key for the I_T nexus that holds the 3911c66ac9dbSNicholas Bellinger * persistent reservation. 3912c66ac9dbSNicholas Bellinger */ 3913c66ac9dbSNicholas Bellinger if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) || 3914c66ac9dbSNicholas Bellinger (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) 3915c66ac9dbSNicholas Bellinger pr_res_key = 0; 3916c66ac9dbSNicholas Bellinger else 3917c66ac9dbSNicholas Bellinger pr_res_key = pr_reg->pr_res_key; 3918c66ac9dbSNicholas Bellinger 3919c66ac9dbSNicholas Bellinger buf[8] = ((pr_res_key >> 56) & 0xff); 3920c66ac9dbSNicholas Bellinger buf[9] = ((pr_res_key >> 48) & 0xff); 3921c66ac9dbSNicholas Bellinger buf[10] = ((pr_res_key >> 40) & 0xff); 3922c66ac9dbSNicholas Bellinger buf[11] = ((pr_res_key >> 32) & 0xff); 3923c66ac9dbSNicholas Bellinger buf[12] = ((pr_res_key >> 24) & 0xff); 3924c66ac9dbSNicholas Bellinger buf[13] = ((pr_res_key >> 16) & 0xff); 3925c66ac9dbSNicholas Bellinger buf[14] = ((pr_res_key >> 8) & 0xff); 3926c66ac9dbSNicholas Bellinger buf[15] = (pr_res_key & 0xff); 3927c66ac9dbSNicholas Bellinger /* 3928c66ac9dbSNicholas Bellinger * Set the SCOPE and TYPE 3929c66ac9dbSNicholas Bellinger */ 3930c66ac9dbSNicholas Bellinger buf[21] = (pr_reg->pr_res_scope & 0xf0) | 3931c66ac9dbSNicholas Bellinger (pr_reg->pr_res_type & 0x0f); 3932c66ac9dbSNicholas Bellinger } 393305d1c7c0SAndy Grover 393405d1c7c0SAndy Grover err: 39350fd97ccfSChristoph Hellwig spin_unlock(&dev->dev_reservation_lock); 39364949314cSAndy Grover transport_kunmap_data_sg(cmd); 3937c66ac9dbSNicholas Bellinger 3938c66ac9dbSNicholas Bellinger return 0; 3939c66ac9dbSNicholas Bellinger } 3940c66ac9dbSNicholas Bellinger 3941c66ac9dbSNicholas Bellinger /* 3942c66ac9dbSNicholas Bellinger * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES 3943c66ac9dbSNicholas Bellinger * 3944c66ac9dbSNicholas Bellinger * See spc4r17 section 6.13.4 Table 165 3945c66ac9dbSNicholas Bellinger */ 3946de103c93SChristoph Hellwig static sense_reason_t 3947de103c93SChristoph Hellwig core_scsi3_pri_report_capabilities(struct se_cmd *cmd) 3948c66ac9dbSNicholas Bellinger { 39495951146dSAndy Grover struct se_device *dev = cmd->se_dev; 39500fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 395105d1c7c0SAndy Grover unsigned char *buf; 3952c66ac9dbSNicholas Bellinger u16 add_len = 8; /* Hardcoded to 8. */ 3953c66ac9dbSNicholas Bellinger 3954c66ac9dbSNicholas Bellinger if (cmd->data_length < 6) { 39556708bb27SAndy Grover pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:" 3956c66ac9dbSNicholas Bellinger " %u too small\n", cmd->data_length); 3957de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 3958c66ac9dbSNicholas Bellinger } 3959c66ac9dbSNicholas Bellinger 39604949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 3961de103c93SChristoph Hellwig if (!buf) 3962de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 396305d1c7c0SAndy Grover 3964c66ac9dbSNicholas Bellinger buf[0] = ((add_len << 8) & 0xff); 3965c66ac9dbSNicholas Bellinger buf[1] = (add_len & 0xff); 3966c66ac9dbSNicholas Bellinger buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */ 3967c66ac9dbSNicholas Bellinger buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */ 3968c66ac9dbSNicholas Bellinger buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */ 3969c66ac9dbSNicholas Bellinger buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */ 3970c66ac9dbSNicholas Bellinger /* 3971c66ac9dbSNicholas Bellinger * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so 3972c66ac9dbSNicholas Bellinger * set the TMV: Task Mask Valid bit. 3973c66ac9dbSNicholas Bellinger */ 3974c66ac9dbSNicholas Bellinger buf[3] |= 0x80; 3975c66ac9dbSNicholas Bellinger /* 3976c66ac9dbSNicholas Bellinger * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166 3977c66ac9dbSNicholas Bellinger */ 3978c66ac9dbSNicholas Bellinger buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */ 3979c66ac9dbSNicholas Bellinger /* 3980c66ac9dbSNicholas Bellinger * PTPL_A: Persistence across Target Power Loss Active bit 3981c66ac9dbSNicholas Bellinger */ 3982c66ac9dbSNicholas Bellinger if (pr_tmpl->pr_aptpl_active) 3983c66ac9dbSNicholas Bellinger buf[3] |= 0x01; 3984c66ac9dbSNicholas Bellinger /* 3985c66ac9dbSNicholas Bellinger * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167 3986c66ac9dbSNicholas Bellinger */ 3987c66ac9dbSNicholas Bellinger buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */ 3988c66ac9dbSNicholas Bellinger buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */ 3989c66ac9dbSNicholas Bellinger buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */ 3990c66ac9dbSNicholas Bellinger buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */ 3991c66ac9dbSNicholas Bellinger buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */ 3992c66ac9dbSNicholas Bellinger buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */ 3993c66ac9dbSNicholas Bellinger 39944949314cSAndy Grover transport_kunmap_data_sg(cmd); 399505d1c7c0SAndy Grover 3996c66ac9dbSNicholas Bellinger return 0; 3997c66ac9dbSNicholas Bellinger } 3998c66ac9dbSNicholas Bellinger 3999c66ac9dbSNicholas Bellinger /* 4000c66ac9dbSNicholas Bellinger * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS 4001c66ac9dbSNicholas Bellinger * 4002c66ac9dbSNicholas Bellinger * See spc4r17 section 6.13.5 Table 168 and 169 4003c66ac9dbSNicholas Bellinger */ 4004de103c93SChristoph Hellwig static sense_reason_t 4005de103c93SChristoph Hellwig core_scsi3_pri_read_full_status(struct se_cmd *cmd) 4006c66ac9dbSNicholas Bellinger { 40070fd97ccfSChristoph Hellwig struct se_device *dev = cmd->se_dev; 4008c66ac9dbSNicholas Bellinger struct se_node_acl *se_nacl; 4009c66ac9dbSNicholas Bellinger struct se_portal_group *se_tpg; 4010c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg, *pr_reg_tmp; 40110fd97ccfSChristoph Hellwig struct t10_reservation *pr_tmpl = &dev->t10_pr; 401205d1c7c0SAndy Grover unsigned char *buf; 4013c66ac9dbSNicholas Bellinger u32 add_desc_len = 0, add_len = 0, desc_len, exp_desc_len; 4014c66ac9dbSNicholas Bellinger u32 off = 8; /* off into first Full Status descriptor */ 4015c66ac9dbSNicholas Bellinger int format_code = 0; 4016c66ac9dbSNicholas Bellinger 4017c66ac9dbSNicholas Bellinger if (cmd->data_length < 8) { 40186708bb27SAndy Grover pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u" 4019c66ac9dbSNicholas Bellinger " too small\n", cmd->data_length); 4020de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 4021c66ac9dbSNicholas Bellinger } 4022c66ac9dbSNicholas Bellinger 40234949314cSAndy Grover buf = transport_kmap_data_sg(cmd); 4024de103c93SChristoph Hellwig if (!buf) 4025de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 402605d1c7c0SAndy Grover 40270fd97ccfSChristoph Hellwig buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff); 40280fd97ccfSChristoph Hellwig buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff); 40290fd97ccfSChristoph Hellwig buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff); 40300fd97ccfSChristoph Hellwig buf[3] = (dev->t10_pr.pr_generation & 0xff); 4031c66ac9dbSNicholas Bellinger 4032c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 4033c66ac9dbSNicholas Bellinger list_for_each_entry_safe(pr_reg, pr_reg_tmp, 4034c66ac9dbSNicholas Bellinger &pr_tmpl->registration_list, pr_reg_list) { 4035c66ac9dbSNicholas Bellinger 4036c66ac9dbSNicholas Bellinger se_nacl = pr_reg->pr_reg_nacl; 4037c66ac9dbSNicholas Bellinger se_tpg = pr_reg->pr_reg_nacl->se_tpg; 4038c66ac9dbSNicholas Bellinger add_desc_len = 0; 4039c66ac9dbSNicholas Bellinger 4040c66ac9dbSNicholas Bellinger atomic_inc(&pr_reg->pr_res_holders); 4041c66ac9dbSNicholas Bellinger smp_mb__after_atomic_inc(); 4042c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 4043c66ac9dbSNicholas Bellinger /* 4044c66ac9dbSNicholas Bellinger * Determine expected length of $FABRIC_MOD specific 4045c66ac9dbSNicholas Bellinger * TransportID full status descriptor.. 4046c66ac9dbSNicholas Bellinger */ 4047e3d6f909SAndy Grover exp_desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id_len( 4048c66ac9dbSNicholas Bellinger se_tpg, se_nacl, pr_reg, &format_code); 4049c66ac9dbSNicholas Bellinger 4050c66ac9dbSNicholas Bellinger if ((exp_desc_len + add_len) > cmd->data_length) { 40516708bb27SAndy Grover pr_warn("SPC-3 PRIN READ_FULL_STATUS ran" 4052c66ac9dbSNicholas Bellinger " out of buffer: %d\n", cmd->data_length); 4053c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 4054c66ac9dbSNicholas Bellinger atomic_dec(&pr_reg->pr_res_holders); 4055c66ac9dbSNicholas Bellinger smp_mb__after_atomic_dec(); 4056c66ac9dbSNicholas Bellinger break; 4057c66ac9dbSNicholas Bellinger } 4058c66ac9dbSNicholas Bellinger /* 4059c66ac9dbSNicholas Bellinger * Set RESERVATION KEY 4060c66ac9dbSNicholas Bellinger */ 4061c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff); 4062c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff); 4063c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff); 4064c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff); 4065c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff); 4066c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff); 4067c66ac9dbSNicholas Bellinger buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff); 4068c66ac9dbSNicholas Bellinger buf[off++] = (pr_reg->pr_res_key & 0xff); 4069c66ac9dbSNicholas Bellinger off += 4; /* Skip Over Reserved area */ 4070c66ac9dbSNicholas Bellinger 4071c66ac9dbSNicholas Bellinger /* 4072c66ac9dbSNicholas Bellinger * Set ALL_TG_PT bit if PROUT SA REGISTER had this set. 4073c66ac9dbSNicholas Bellinger */ 4074c66ac9dbSNicholas Bellinger if (pr_reg->pr_reg_all_tg_pt) 4075c66ac9dbSNicholas Bellinger buf[off] = 0x02; 4076c66ac9dbSNicholas Bellinger /* 4077c66ac9dbSNicholas Bellinger * The struct se_lun pointer will be present for the 4078c66ac9dbSNicholas Bellinger * reservation holder for PR_HOLDER bit. 4079c66ac9dbSNicholas Bellinger * 4080c66ac9dbSNicholas Bellinger * Also, if this registration is the reservation 4081c66ac9dbSNicholas Bellinger * holder, fill in SCOPE and TYPE in the next byte. 4082c66ac9dbSNicholas Bellinger */ 4083c66ac9dbSNicholas Bellinger if (pr_reg->pr_res_holder) { 4084c66ac9dbSNicholas Bellinger buf[off++] |= 0x01; 4085c66ac9dbSNicholas Bellinger buf[off++] = (pr_reg->pr_res_scope & 0xf0) | 4086c66ac9dbSNicholas Bellinger (pr_reg->pr_res_type & 0x0f); 4087c66ac9dbSNicholas Bellinger } else 4088c66ac9dbSNicholas Bellinger off += 2; 4089c66ac9dbSNicholas Bellinger 4090c66ac9dbSNicholas Bellinger off += 4; /* Skip over reserved area */ 4091c66ac9dbSNicholas Bellinger /* 4092c66ac9dbSNicholas Bellinger * From spc4r17 6.3.15: 4093c66ac9dbSNicholas Bellinger * 4094c66ac9dbSNicholas Bellinger * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT 4095c66ac9dbSNicholas Bellinger * IDENTIFIER field contains the relative port identifier (see 4096c66ac9dbSNicholas Bellinger * 3.1.120) of the target port that is part of the I_T nexus 4097c66ac9dbSNicholas Bellinger * described by this full status descriptor. If the ALL_TG_PT 4098c66ac9dbSNicholas Bellinger * bit is set to one, the contents of the RELATIVE TARGET PORT 4099c66ac9dbSNicholas Bellinger * IDENTIFIER field are not defined by this standard. 4100c66ac9dbSNicholas Bellinger */ 41016708bb27SAndy Grover if (!pr_reg->pr_reg_all_tg_pt) { 4102c66ac9dbSNicholas Bellinger struct se_port *port = pr_reg->pr_reg_tg_pt_lun->lun_sep; 4103c66ac9dbSNicholas Bellinger 4104c66ac9dbSNicholas Bellinger buf[off++] = ((port->sep_rtpi >> 8) & 0xff); 4105c66ac9dbSNicholas Bellinger buf[off++] = (port->sep_rtpi & 0xff); 4106c66ac9dbSNicholas Bellinger } else 410735d1efe8SMasanari Iida off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */ 4108c66ac9dbSNicholas Bellinger 4109c66ac9dbSNicholas Bellinger /* 4110c66ac9dbSNicholas Bellinger * Now, have the $FABRIC_MOD fill in the protocol identifier 4111c66ac9dbSNicholas Bellinger */ 4112e3d6f909SAndy Grover desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id(se_tpg, 4113c66ac9dbSNicholas Bellinger se_nacl, pr_reg, &format_code, &buf[off+4]); 4114c66ac9dbSNicholas Bellinger 4115c66ac9dbSNicholas Bellinger spin_lock(&pr_tmpl->registration_lock); 4116c66ac9dbSNicholas Bellinger atomic_dec(&pr_reg->pr_res_holders); 4117c66ac9dbSNicholas Bellinger smp_mb__after_atomic_dec(); 4118c66ac9dbSNicholas Bellinger /* 4119c66ac9dbSNicholas Bellinger * Set the ADDITIONAL DESCRIPTOR LENGTH 4120c66ac9dbSNicholas Bellinger */ 4121c66ac9dbSNicholas Bellinger buf[off++] = ((desc_len >> 24) & 0xff); 4122c66ac9dbSNicholas Bellinger buf[off++] = ((desc_len >> 16) & 0xff); 4123c66ac9dbSNicholas Bellinger buf[off++] = ((desc_len >> 8) & 0xff); 4124c66ac9dbSNicholas Bellinger buf[off++] = (desc_len & 0xff); 4125c66ac9dbSNicholas Bellinger /* 4126c66ac9dbSNicholas Bellinger * Size of full desctipor header minus TransportID 4127c66ac9dbSNicholas Bellinger * containing $FABRIC_MOD specific) initiator device/port 4128c66ac9dbSNicholas Bellinger * WWN information. 4129c66ac9dbSNicholas Bellinger * 4130c66ac9dbSNicholas Bellinger * See spc4r17 Section 6.13.5 Table 169 4131c66ac9dbSNicholas Bellinger */ 4132c66ac9dbSNicholas Bellinger add_desc_len = (24 + desc_len); 4133c66ac9dbSNicholas Bellinger 4134c66ac9dbSNicholas Bellinger off += desc_len; 4135c66ac9dbSNicholas Bellinger add_len += add_desc_len; 4136c66ac9dbSNicholas Bellinger } 4137c66ac9dbSNicholas Bellinger spin_unlock(&pr_tmpl->registration_lock); 4138c66ac9dbSNicholas Bellinger /* 4139c66ac9dbSNicholas Bellinger * Set ADDITIONAL_LENGTH 4140c66ac9dbSNicholas Bellinger */ 4141c66ac9dbSNicholas Bellinger buf[4] = ((add_len >> 24) & 0xff); 4142c66ac9dbSNicholas Bellinger buf[5] = ((add_len >> 16) & 0xff); 4143c66ac9dbSNicholas Bellinger buf[6] = ((add_len >> 8) & 0xff); 4144c66ac9dbSNicholas Bellinger buf[7] = (add_len & 0xff); 4145c66ac9dbSNicholas Bellinger 41464949314cSAndy Grover transport_kunmap_data_sg(cmd); 414705d1c7c0SAndy Grover 4148c66ac9dbSNicholas Bellinger return 0; 4149c66ac9dbSNicholas Bellinger } 4150c66ac9dbSNicholas Bellinger 4151de103c93SChristoph Hellwig sense_reason_t 4152de103c93SChristoph Hellwig target_scsi3_emulate_pr_in(struct se_cmd *cmd) 4153c66ac9dbSNicholas Bellinger { 4154de103c93SChristoph Hellwig sense_reason_t ret; 4155e76a35d6SChristoph Hellwig 4156617c0e06SChristoph Hellwig /* 4157617c0e06SChristoph Hellwig * Following spc2r20 5.5.1 Reservations overview: 4158617c0e06SChristoph Hellwig * 4159617c0e06SChristoph Hellwig * If a logical unit has been reserved by any RESERVE command and is 4160617c0e06SChristoph Hellwig * still reserved by any initiator, all PERSISTENT RESERVE IN and all 4161617c0e06SChristoph Hellwig * PERSISTENT RESERVE OUT commands shall conflict regardless of 4162617c0e06SChristoph Hellwig * initiator or service action and shall terminate with a RESERVATION 4163617c0e06SChristoph Hellwig * CONFLICT status. 4164617c0e06SChristoph Hellwig */ 41650fd97ccfSChristoph Hellwig if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) { 4166617c0e06SChristoph Hellwig pr_err("Received PERSISTENT_RESERVE CDB while legacy" 4167617c0e06SChristoph Hellwig " SPC-2 reservation is held, returning" 4168617c0e06SChristoph Hellwig " RESERVATION_CONFLICT\n"); 4169de103c93SChristoph Hellwig return TCM_RESERVATION_CONFLICT; 4170617c0e06SChristoph Hellwig } 4171617c0e06SChristoph Hellwig 4172617c0e06SChristoph Hellwig switch (cmd->t_task_cdb[1] & 0x1f) { 4173c66ac9dbSNicholas Bellinger case PRI_READ_KEYS: 4174d29a5b6aSChristoph Hellwig ret = core_scsi3_pri_read_keys(cmd); 4175d29a5b6aSChristoph Hellwig break; 4176c66ac9dbSNicholas Bellinger case PRI_READ_RESERVATION: 4177d29a5b6aSChristoph Hellwig ret = core_scsi3_pri_read_reservation(cmd); 4178d29a5b6aSChristoph Hellwig break; 4179c66ac9dbSNicholas Bellinger case PRI_REPORT_CAPABILITIES: 4180d29a5b6aSChristoph Hellwig ret = core_scsi3_pri_report_capabilities(cmd); 4181d29a5b6aSChristoph Hellwig break; 4182c66ac9dbSNicholas Bellinger case PRI_READ_FULL_STATUS: 4183d29a5b6aSChristoph Hellwig ret = core_scsi3_pri_read_full_status(cmd); 4184d29a5b6aSChristoph Hellwig break; 4185c66ac9dbSNicholas Bellinger default: 41866708bb27SAndy Grover pr_err("Unknown PERSISTENT_RESERVE_IN service" 4187617c0e06SChristoph Hellwig " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f); 4188de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 4189c66ac9dbSNicholas Bellinger } 4190d29a5b6aSChristoph Hellwig 41916bb35e00SChristoph Hellwig if (!ret) 41926bb35e00SChristoph Hellwig target_complete_cmd(cmd, GOOD); 4193d29a5b6aSChristoph Hellwig return ret; 4194c66ac9dbSNicholas Bellinger } 4195c66ac9dbSNicholas Bellinger 4196de103c93SChristoph Hellwig sense_reason_t 4197de103c93SChristoph Hellwig target_check_reservation(struct se_cmd *cmd) 4198c66ac9dbSNicholas Bellinger { 4199d977f437SChristoph Hellwig struct se_device *dev = cmd->se_dev; 4200de103c93SChristoph Hellwig sense_reason_t ret; 4201d977f437SChristoph Hellwig 4202d977f437SChristoph Hellwig if (!cmd->se_sess) 4203c66ac9dbSNicholas Bellinger return 0; 4204d977f437SChristoph Hellwig if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE) 4205c66ac9dbSNicholas Bellinger return 0; 4206d977f437SChristoph Hellwig if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) 4207d977f437SChristoph Hellwig return 0; 4208c66ac9dbSNicholas Bellinger 4209d977f437SChristoph Hellwig spin_lock(&dev->dev_reservation_lock); 4210d977f437SChristoph Hellwig if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) 4211d977f437SChristoph Hellwig ret = target_scsi2_reservation_check(cmd); 4212d977f437SChristoph Hellwig else 4213d977f437SChristoph Hellwig ret = target_scsi3_pr_reservation_check(cmd); 4214d977f437SChristoph Hellwig spin_unlock(&dev->dev_reservation_lock); 42150fd97ccfSChristoph Hellwig 4216d977f437SChristoph Hellwig return ret; 4217c66ac9dbSNicholas Bellinger } 4218