11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2c66ac9dbSNicholas Bellinger /*******************************************************************************
3c66ac9dbSNicholas Bellinger  * Filename:  target_core_pr.c
4c66ac9dbSNicholas Bellinger  *
5c66ac9dbSNicholas Bellinger  * This file contains SPC-3 compliant persistent reservations and
6c66ac9dbSNicholas Bellinger  * legacy SPC-2 reservations with compatible reservation handling (CRH=1)
7c66ac9dbSNicholas Bellinger  *
84c76251eSNicholas Bellinger  * (c) Copyright 2009-2013 Datera, Inc.
9c66ac9dbSNicholas Bellinger  *
10c66ac9dbSNicholas Bellinger  * Nicholas A. Bellinger <nab@kernel.org>
11c66ac9dbSNicholas Bellinger  *
12c66ac9dbSNicholas Bellinger  ******************************************************************************/
13c66ac9dbSNicholas Bellinger 
14c66ac9dbSNicholas Bellinger #include <linux/slab.h>
15c66ac9dbSNicholas Bellinger #include <linux/spinlock.h>
16c66ac9dbSNicholas Bellinger #include <linux/list.h>
175538d294SDavid S. Miller #include <linux/vmalloc.h>
180e9b10a9SAl Viro #include <linux/file.h>
198dcf07beSBart Van Assche #include <linux/fcntl.h>
208dcf07beSBart Van Assche #include <linux/fs.h>
21ba929992SBart Van Assche #include <scsi/scsi_proto.h>
22c66ac9dbSNicholas Bellinger #include <asm/unaligned.h>
23c66ac9dbSNicholas Bellinger 
24c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
25c4795fb2SChristoph Hellwig #include <target/target_core_backend.h>
26c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
27c66ac9dbSNicholas Bellinger 
28e26d99aeSChristoph Hellwig #include "target_core_internal.h"
29c66ac9dbSNicholas Bellinger #include "target_core_pr.h"
30c66ac9dbSNicholas Bellinger #include "target_core_ua.h"
31c66ac9dbSNicholas Bellinger 
32c66ac9dbSNicholas Bellinger /*
33c66ac9dbSNicholas Bellinger  * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
34c66ac9dbSNicholas Bellinger  */
35c66ac9dbSNicholas Bellinger struct pr_transport_id_holder {
36c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *dest_pr_reg;
37c66ac9dbSNicholas Bellinger 	struct se_portal_group *dest_tpg;
38c66ac9dbSNicholas Bellinger 	struct se_node_acl *dest_node_acl;
39c66ac9dbSNicholas Bellinger 	struct se_dev_entry *dest_se_deve;
40c66ac9dbSNicholas Bellinger 	struct list_head dest_list;
41c66ac9dbSNicholas Bellinger };
42c66ac9dbSNicholas Bellinger 
core_pr_dump_initiator_port(struct t10_pr_registration * pr_reg,char * buf,u32 size)43d2843c17SAndy Grover void core_pr_dump_initiator_port(
44c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg,
45c66ac9dbSNicholas Bellinger 	char *buf,
46c66ac9dbSNicholas Bellinger 	u32 size)
47c66ac9dbSNicholas Bellinger {
4824528f08Stangwenji 	if (!pr_reg->isid_present_at_reg) {
49d2843c17SAndy Grover 		buf[0] = '\0';
5024528f08Stangwenji 		return;
5124528f08Stangwenji 	}
52c66ac9dbSNicholas Bellinger 
53d2843c17SAndy Grover 	snprintf(buf, size, ",i,0x%s", pr_reg->pr_reg_isid);
54c66ac9dbSNicholas Bellinger }
55c66ac9dbSNicholas Bellinger 
5633ce6a87SAndy Grover enum register_type {
5733ce6a87SAndy Grover 	REGISTER,
5833ce6a87SAndy Grover 	REGISTER_AND_IGNORE_EXISTING_KEY,
5933ce6a87SAndy Grover 	REGISTER_AND_MOVE,
6033ce6a87SAndy Grover };
6133ce6a87SAndy Grover 
6233ce6a87SAndy Grover enum preempt_type {
6333ce6a87SAndy Grover 	PREEMPT,
6433ce6a87SAndy Grover 	PREEMPT_AND_ABORT,
6533ce6a87SAndy Grover };
6633ce6a87SAndy Grover 
67c66ac9dbSNicholas Bellinger static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
686c3c9baaSNicholas Bellinger 					      struct t10_pr_registration *, int, int);
69c66ac9dbSNicholas Bellinger 
is_reservation_holder(struct t10_pr_registration * pr_res_holder,struct t10_pr_registration * pr_reg)70e673dc92SIlias Tsitsimpis static int is_reservation_holder(
71e673dc92SIlias Tsitsimpis 	struct t10_pr_registration *pr_res_holder,
72e673dc92SIlias Tsitsimpis 	struct t10_pr_registration *pr_reg)
73e673dc92SIlias Tsitsimpis {
74e673dc92SIlias Tsitsimpis 	int pr_res_type;
75e673dc92SIlias Tsitsimpis 
76e673dc92SIlias Tsitsimpis 	if (pr_res_holder) {
77e673dc92SIlias Tsitsimpis 		pr_res_type = pr_res_holder->pr_res_type;
78e673dc92SIlias Tsitsimpis 
79e673dc92SIlias Tsitsimpis 		return pr_res_holder == pr_reg ||
80e673dc92SIlias Tsitsimpis 		       pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
81e673dc92SIlias Tsitsimpis 		       pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG;
82e673dc92SIlias Tsitsimpis 	}
83e673dc92SIlias Tsitsimpis 	return 0;
84e673dc92SIlias Tsitsimpis }
85e673dc92SIlias Tsitsimpis 
86de103c93SChristoph Hellwig static sense_reason_t
target_scsi2_reservation_check(struct se_cmd * cmd)87de103c93SChristoph Hellwig target_scsi2_reservation_check(struct se_cmd *cmd)
88c66ac9dbSNicholas Bellinger {
89d977f437SChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
90d977f437SChristoph Hellwig 	struct se_session *sess = cmd->se_sess;
91d977f437SChristoph Hellwig 
92d977f437SChristoph Hellwig 	switch (cmd->t_task_cdb[0]) {
93c66ac9dbSNicholas Bellinger 	case INQUIRY:
94c66ac9dbSNicholas Bellinger 	case RELEASE:
95c66ac9dbSNicholas Bellinger 	case RELEASE_10:
96c66ac9dbSNicholas Bellinger 		return 0;
97c66ac9dbSNicholas Bellinger 	default:
98d977f437SChristoph Hellwig 		break;
99c66ac9dbSNicholas Bellinger 	}
100c66ac9dbSNicholas Bellinger 
101fae43461SBart Van Assche 	if (!dev->reservation_holder || !sess)
102c66ac9dbSNicholas Bellinger 		return 0;
103c66ac9dbSNicholas Bellinger 
104fae43461SBart Van Assche 	if (dev->reservation_holder->se_node_acl != sess->se_node_acl)
105de103c93SChristoph Hellwig 		return TCM_RESERVATION_CONFLICT;
106c66ac9dbSNicholas Bellinger 
107d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
108d977f437SChristoph Hellwig 		if (dev->dev_res_bin_isid != sess->sess_bin_isid)
109de103c93SChristoph Hellwig 			return TCM_RESERVATION_CONFLICT;
110d977f437SChristoph Hellwig 	}
111d977f437SChristoph Hellwig 
112d977f437SChristoph Hellwig 	return 0;
113c66ac9dbSNicholas Bellinger }
114c66ac9dbSNicholas Bellinger 
115eacac00cSChristoph Hellwig static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
116eacac00cSChristoph Hellwig 					struct se_node_acl *, struct se_session *);
117eacac00cSChristoph Hellwig static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
118eacac00cSChristoph Hellwig 
target_check_scsi2_reservation_conflict(struct se_cmd * cmd)119087a03b3SNicholas Bellinger static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
120eacac00cSChristoph Hellwig {
121eacac00cSChristoph Hellwig 	struct se_session *se_sess = cmd->se_sess;
1220fd97ccfSChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
123eacac00cSChristoph Hellwig 	struct t10_pr_registration *pr_reg;
1240fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
125eacac00cSChristoph Hellwig 	int conflict = 0;
126eacac00cSChristoph Hellwig 
127eacac00cSChristoph Hellwig 	pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
128eacac00cSChristoph Hellwig 			se_sess);
129eacac00cSChristoph Hellwig 	if (pr_reg) {
130eacac00cSChristoph Hellwig 		/*
131eacac00cSChristoph Hellwig 		 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
132eacac00cSChristoph Hellwig 		 * behavior
133eacac00cSChristoph Hellwig 		 *
134eacac00cSChristoph Hellwig 		 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
135eacac00cSChristoph Hellwig 		 * status, but no reservation shall be established and the
136eacac00cSChristoph Hellwig 		 * persistent reservation shall not be changed, if the command
137eacac00cSChristoph Hellwig 		 * is received from a) and b) below.
138eacac00cSChristoph Hellwig 		 *
139eacac00cSChristoph Hellwig 		 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
140eacac00cSChristoph Hellwig 		 * status, but the persistent reservation shall not be released,
141eacac00cSChristoph Hellwig 		 * if the command is received from a) and b)
142eacac00cSChristoph Hellwig 		 *
143eacac00cSChristoph Hellwig 		 * a) An I_T nexus that is a persistent reservation holder; or
144eacac00cSChristoph Hellwig 		 * b) An I_T nexus that is registered if a registrants only or
145eacac00cSChristoph Hellwig 		 *    all registrants type persistent reservation is present.
146eacac00cSChristoph Hellwig 		 *
147eacac00cSChristoph Hellwig 		 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
148eacac00cSChristoph Hellwig 		 * RELEASE(6) command, or RELEASE(10) command shall be processed
149eacac00cSChristoph Hellwig 		 * as defined in SPC-2.
150eacac00cSChristoph Hellwig 		 */
151eacac00cSChristoph Hellwig 		if (pr_reg->pr_res_holder) {
152eacac00cSChristoph Hellwig 			core_scsi3_put_pr_reg(pr_reg);
153087a03b3SNicholas Bellinger 			return 1;
154eacac00cSChristoph Hellwig 		}
155eacac00cSChristoph Hellwig 		if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
156eacac00cSChristoph Hellwig 		    (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
157eacac00cSChristoph Hellwig 		    (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
158eacac00cSChristoph Hellwig 		    (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
159eacac00cSChristoph Hellwig 			core_scsi3_put_pr_reg(pr_reg);
160087a03b3SNicholas Bellinger 			return 1;
161eacac00cSChristoph Hellwig 		}
162eacac00cSChristoph Hellwig 		core_scsi3_put_pr_reg(pr_reg);
163eacac00cSChristoph Hellwig 		conflict = 1;
164eacac00cSChristoph Hellwig 	} else {
165eacac00cSChristoph Hellwig 		/*
166eacac00cSChristoph Hellwig 		 * Following spc2r20 5.5.1 Reservations overview:
167eacac00cSChristoph Hellwig 		 *
168eacac00cSChristoph Hellwig 		 * If a logical unit has executed a PERSISTENT RESERVE OUT
169eacac00cSChristoph Hellwig 		 * command with the REGISTER or the REGISTER AND IGNORE
170eacac00cSChristoph Hellwig 		 * EXISTING KEY service action and is still registered by any
171eacac00cSChristoph Hellwig 		 * initiator, all RESERVE commands and all RELEASE commands
172eacac00cSChristoph Hellwig 		 * regardless of initiator shall conflict and shall terminate
173eacac00cSChristoph Hellwig 		 * with a RESERVATION CONFLICT status.
174eacac00cSChristoph Hellwig 		 */
175eacac00cSChristoph Hellwig 		spin_lock(&pr_tmpl->registration_lock);
176eacac00cSChristoph Hellwig 		conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
177eacac00cSChristoph Hellwig 		spin_unlock(&pr_tmpl->registration_lock);
178eacac00cSChristoph Hellwig 	}
179eacac00cSChristoph Hellwig 
180eacac00cSChristoph Hellwig 	if (conflict) {
181eacac00cSChristoph Hellwig 		pr_err("Received legacy SPC-2 RESERVE/RELEASE"
182eacac00cSChristoph Hellwig 			" while active SPC-3 registrations exist,"
183eacac00cSChristoph Hellwig 			" returning RESERVATION_CONFLICT\n");
184087a03b3SNicholas Bellinger 		return -EBUSY;
185eacac00cSChristoph Hellwig 	}
186eacac00cSChristoph Hellwig 
187087a03b3SNicholas Bellinger 	return 0;
188eacac00cSChristoph Hellwig }
189eacac00cSChristoph Hellwig 
target_release_reservation(struct se_device * dev)190fae43461SBart Van Assche void target_release_reservation(struct se_device *dev)
191fae43461SBart Van Assche {
192fae43461SBart Van Assche 	dev->reservation_holder = NULL;
193fae43461SBart Van Assche 	dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
194fae43461SBart Van Assche 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
195fae43461SBart Van Assche 		dev->dev_res_bin_isid = 0;
196fae43461SBart Van Assche 		dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS_WITH_ISID;
197fae43461SBart Van Assche 	}
198fae43461SBart Van Assche }
199fae43461SBart Van Assche 
200de103c93SChristoph Hellwig sense_reason_t
target_scsi2_reservation_release(struct se_cmd * cmd)201de103c93SChristoph Hellwig target_scsi2_reservation_release(struct se_cmd *cmd)
202c66ac9dbSNicholas Bellinger {
203c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
204c66ac9dbSNicholas Bellinger 	struct se_session *sess = cmd->se_sess;
205609234e3SWei Yongjun 	struct se_portal_group *tpg;
206de103c93SChristoph Hellwig 	int rc;
207c66ac9dbSNicholas Bellinger 
208609234e3SWei Yongjun 	if (!sess || !sess->se_tpg)
209d29a5b6aSChristoph Hellwig 		goto out;
210087a03b3SNicholas Bellinger 	rc = target_check_scsi2_reservation_conflict(cmd);
211087a03b3SNicholas Bellinger 	if (rc == 1)
212d29a5b6aSChristoph Hellwig 		goto out;
213de103c93SChristoph Hellwig 	if (rc < 0)
214de103c93SChristoph Hellwig 		return TCM_RESERVATION_CONFLICT;
215c66ac9dbSNicholas Bellinger 
216c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
217fae43461SBart Van Assche 	if (!dev->reservation_holder || !sess)
218d29a5b6aSChristoph Hellwig 		goto out_unlock;
219c66ac9dbSNicholas Bellinger 
220fae43461SBart Van Assche 	if (dev->reservation_holder->se_node_acl != sess->se_node_acl)
221d29a5b6aSChristoph Hellwig 		goto out_unlock;
222d29a5b6aSChristoph Hellwig 
223edc318d9SBernhard Kohl 	if (dev->dev_res_bin_isid != sess->sess_bin_isid)
224edc318d9SBernhard Kohl 		goto out_unlock;
225edc318d9SBernhard Kohl 
226fae43461SBart Van Assche 	target_release_reservation(dev);
227609234e3SWei Yongjun 	tpg = sess->se_tpg;
228f2d30680SHannes Reinecke 	pr_debug("SCSI-2 Released reservation for %s LUN: %llu ->"
229f2d30680SHannes Reinecke 		" MAPPED LUN: %llu for %s\n",
23030c7ca93SDavid Disseldorp 		tpg->se_tpg_tfo->fabric_name,
23129a05deeSNicholas Bellinger 		cmd->se_lun->unpacked_lun, cmd->orig_fe_lun,
232c66ac9dbSNicholas Bellinger 		sess->se_node_acl->initiatorname);
233c66ac9dbSNicholas Bellinger 
234d29a5b6aSChristoph Hellwig out_unlock:
235d29a5b6aSChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
236d29a5b6aSChristoph Hellwig out:
23714b40c1eSHannes Reinecke 	target_complete_cmd(cmd, SAM_STAT_GOOD);
238de103c93SChristoph Hellwig 	return 0;
239c66ac9dbSNicholas Bellinger }
240c66ac9dbSNicholas Bellinger 
241de103c93SChristoph Hellwig sense_reason_t
target_scsi2_reservation_reserve(struct se_cmd * cmd)242de103c93SChristoph Hellwig target_scsi2_reservation_reserve(struct se_cmd *cmd)
243c66ac9dbSNicholas Bellinger {
244c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
245c66ac9dbSNicholas Bellinger 	struct se_session *sess = cmd->se_sess;
246609234e3SWei Yongjun 	struct se_portal_group *tpg;
247de103c93SChristoph Hellwig 	sense_reason_t ret = 0;
248de103c93SChristoph Hellwig 	int rc;
249c66ac9dbSNicholas Bellinger 
250a1d8b49aSAndy Grover 	if ((cmd->t_task_cdb[1] & 0x01) &&
251a1d8b49aSAndy Grover 	    (cmd->t_task_cdb[1] & 0x02)) {
252a3960cedSColin Ian King 		pr_err("LongIO and Obsolete Bits set, returning ILLEGAL_REQUEST\n");
253de103c93SChristoph Hellwig 		return TCM_UNSUPPORTED_SCSI_OPCODE;
254c66ac9dbSNicholas Bellinger 	}
255c66ac9dbSNicholas Bellinger 	/*
256c66ac9dbSNicholas Bellinger 	 * This is currently the case for target_core_mod passthrough struct se_cmd
257c66ac9dbSNicholas Bellinger 	 * ops
258c66ac9dbSNicholas Bellinger 	 */
259609234e3SWei Yongjun 	if (!sess || !sess->se_tpg)
260d29a5b6aSChristoph Hellwig 		goto out;
261087a03b3SNicholas Bellinger 	rc = target_check_scsi2_reservation_conflict(cmd);
262087a03b3SNicholas Bellinger 	if (rc == 1)
263d29a5b6aSChristoph Hellwig 		goto out;
264c66ac9dbSNicholas Bellinger 
265de103c93SChristoph Hellwig 	if (rc < 0)
266de103c93SChristoph Hellwig 		return TCM_RESERVATION_CONFLICT;
267de103c93SChristoph Hellwig 
268609234e3SWei Yongjun 	tpg = sess->se_tpg;
269c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
270fae43461SBart Van Assche 	if (dev->reservation_holder &&
271fae43461SBart Van Assche 	    dev->reservation_holder->se_node_acl != sess->se_node_acl) {
2729a8ef2c7SColin Ian King 		pr_err("SCSI-2 RESERVATION CONFLICT for %s fabric\n",
27330c7ca93SDavid Disseldorp 			tpg->se_tpg_tfo->fabric_name);
274f2d30680SHannes Reinecke 		pr_err("Original reserver LUN: %llu %s\n",
275e3d6f909SAndy Grover 			cmd->se_lun->unpacked_lun,
276fae43461SBart Van Assche 			dev->reservation_holder->se_node_acl->initiatorname);
277f2d30680SHannes Reinecke 		pr_err("Current attempt - LUN: %llu -> MAPPED LUN: %llu"
278e3d6f909SAndy Grover 			" from %s \n", cmd->se_lun->unpacked_lun,
27929a05deeSNicholas Bellinger 			cmd->orig_fe_lun,
280c66ac9dbSNicholas Bellinger 			sess->se_node_acl->initiatorname);
281de103c93SChristoph Hellwig 		ret = TCM_RESERVATION_CONFLICT;
282d29a5b6aSChristoph Hellwig 		goto out_unlock;
283c66ac9dbSNicholas Bellinger 	}
284c66ac9dbSNicholas Bellinger 
285fae43461SBart Van Assche 	dev->reservation_holder = sess;
2860fd97ccfSChristoph Hellwig 	dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS;
287c66ac9dbSNicholas Bellinger 	if (sess->sess_bin_isid != 0) {
288c66ac9dbSNicholas Bellinger 		dev->dev_res_bin_isid = sess->sess_bin_isid;
2890fd97ccfSChristoph Hellwig 		dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS_WITH_ISID;
290c66ac9dbSNicholas Bellinger 	}
291f2d30680SHannes Reinecke 	pr_debug("SCSI-2 Reserved %s LUN: %llu -> MAPPED LUN: %llu"
29230c7ca93SDavid Disseldorp 		" for %s\n", tpg->se_tpg_tfo->fabric_name,
29329a05deeSNicholas Bellinger 		cmd->se_lun->unpacked_lun, cmd->orig_fe_lun,
294c66ac9dbSNicholas Bellinger 		sess->se_node_acl->initiatorname);
295c66ac9dbSNicholas Bellinger 
296d29a5b6aSChristoph Hellwig out_unlock:
297d29a5b6aSChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
298d29a5b6aSChristoph Hellwig out:
2996bb35e00SChristoph Hellwig 	if (!ret)
30014b40c1eSHannes Reinecke 		target_complete_cmd(cmd, SAM_STAT_GOOD);
301d29a5b6aSChristoph Hellwig 	return ret;
302c66ac9dbSNicholas Bellinger }
303c66ac9dbSNicholas Bellinger 
304c66ac9dbSNicholas Bellinger 
305c66ac9dbSNicholas Bellinger /*
306c66ac9dbSNicholas Bellinger  * Begin SPC-3/SPC-4 Persistent Reservations emulation support
307c66ac9dbSNicholas Bellinger  *
308c66ac9dbSNicholas Bellinger  * This function is called by those initiator ports who are *NOT*
309c66ac9dbSNicholas Bellinger  * the active PR reservation holder when a reservation is present.
310c66ac9dbSNicholas Bellinger  */
core_scsi3_pr_seq_non_holder(struct se_cmd * cmd,u32 pr_reg_type,bool isid_mismatch)3119fcb57f3SChristoph Hellwig static int core_scsi3_pr_seq_non_holder(struct se_cmd *cmd, u32 pr_reg_type,
3129fcb57f3SChristoph Hellwig 					bool isid_mismatch)
313c66ac9dbSNicholas Bellinger {
314d977f437SChristoph Hellwig 	unsigned char *cdb = cmd->t_task_cdb;
315e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
31629a05deeSNicholas Bellinger 	struct se_node_acl *nacl = se_sess->se_node_acl;
3179fcb57f3SChristoph Hellwig 	int other_cdb = 0;
318c66ac9dbSNicholas Bellinger 	int registered_nexus = 0, ret = 1; /* Conflict by default */
319c66ac9dbSNicholas Bellinger 	int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
320c66ac9dbSNicholas Bellinger 	int we = 0; /* Write Exclusive */
321c66ac9dbSNicholas Bellinger 	int legacy = 0; /* Act like a legacy device and return
322c66ac9dbSNicholas Bellinger 			 * RESERVATION CONFLICT on some CDBs */
323c66ac9dbSNicholas Bellinger 
3249fcb57f3SChristoph Hellwig 	if (isid_mismatch) {
3259fcb57f3SChristoph Hellwig 		registered_nexus = 0;
3269fcb57f3SChristoph Hellwig 	} else {
3279fcb57f3SChristoph Hellwig 		struct se_dev_entry *se_deve;
328c66ac9dbSNicholas Bellinger 
32929a05deeSNicholas Bellinger 		rcu_read_lock();
33029a05deeSNicholas Bellinger 		se_deve = target_nacl_find_deve(nacl, cmd->orig_fe_lun);
33180bfdfa9SNicholas Bellinger 		if (se_deve)
3329fcb57f3SChristoph Hellwig 			registered_nexus = test_bit(DEF_PR_REG_ACTIVE,
3339fcb57f3SChristoph Hellwig 						    &se_deve->deve_flags);
33480bfdfa9SNicholas Bellinger 		rcu_read_unlock();
3359fcb57f3SChristoph Hellwig 	}
336c66ac9dbSNicholas Bellinger 
337c66ac9dbSNicholas Bellinger 	switch (pr_reg_type) {
338c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE:
339c66ac9dbSNicholas Bellinger 		we = 1;
340492096ecSGustavo A. R. Silva 		fallthrough;
341c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS:
342c66ac9dbSNicholas Bellinger 		/*
343c66ac9dbSNicholas Bellinger 		 * Some commands are only allowed for the persistent reservation
344c66ac9dbSNicholas Bellinger 		 * holder.
345c66ac9dbSNicholas Bellinger 		 */
346c66ac9dbSNicholas Bellinger 		break;
347c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
348c66ac9dbSNicholas Bellinger 		we = 1;
349df561f66SGustavo A. R. Silva 		fallthrough;
350c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
351c66ac9dbSNicholas Bellinger 		/*
352c66ac9dbSNicholas Bellinger 		 * Some commands are only allowed for registered I_T Nexuses.
353c66ac9dbSNicholas Bellinger 		 */
354c66ac9dbSNicholas Bellinger 		reg_only = 1;
355c66ac9dbSNicholas Bellinger 		break;
356c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
357c66ac9dbSNicholas Bellinger 		we = 1;
358df561f66SGustavo A. R. Silva 		fallthrough;
359c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
360c66ac9dbSNicholas Bellinger 		/*
361c66ac9dbSNicholas Bellinger 		 * Each registered I_T Nexus is a reservation holder.
362c66ac9dbSNicholas Bellinger 		 */
363c66ac9dbSNicholas Bellinger 		all_reg = 1;
364c66ac9dbSNicholas Bellinger 		break;
365c66ac9dbSNicholas Bellinger 	default:
366e3d6f909SAndy Grover 		return -EINVAL;
367c66ac9dbSNicholas Bellinger 	}
368c66ac9dbSNicholas Bellinger 	/*
369c66ac9dbSNicholas Bellinger 	 * Referenced from spc4r17 table 45 for *NON* PR holder access
370c66ac9dbSNicholas Bellinger 	 */
371c66ac9dbSNicholas Bellinger 	switch (cdb[0]) {
372c66ac9dbSNicholas Bellinger 	case SECURITY_PROTOCOL_IN:
373c66ac9dbSNicholas Bellinger 		if (registered_nexus)
374c66ac9dbSNicholas Bellinger 			return 0;
375c66ac9dbSNicholas Bellinger 		ret = (we) ? 0 : 1;
376c66ac9dbSNicholas Bellinger 		break;
377c66ac9dbSNicholas Bellinger 	case MODE_SENSE:
378c66ac9dbSNicholas Bellinger 	case MODE_SENSE_10:
379c66ac9dbSNicholas Bellinger 	case READ_ATTRIBUTE:
380c66ac9dbSNicholas Bellinger 	case READ_BUFFER:
381c66ac9dbSNicholas Bellinger 	case RECEIVE_DIAGNOSTIC:
382c66ac9dbSNicholas Bellinger 		if (legacy) {
383c66ac9dbSNicholas Bellinger 			ret = 1;
384c66ac9dbSNicholas Bellinger 			break;
385c66ac9dbSNicholas Bellinger 		}
386c66ac9dbSNicholas Bellinger 		if (registered_nexus) {
387c66ac9dbSNicholas Bellinger 			ret = 0;
388c66ac9dbSNicholas Bellinger 			break;
389c66ac9dbSNicholas Bellinger 		}
390c66ac9dbSNicholas Bellinger 		ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
391c66ac9dbSNicholas Bellinger 		break;
392c66ac9dbSNicholas Bellinger 	case PERSISTENT_RESERVE_OUT:
393c66ac9dbSNicholas Bellinger 		/*
394c66ac9dbSNicholas Bellinger 		 * This follows PERSISTENT_RESERVE_OUT service actions that
395c66ac9dbSNicholas Bellinger 		 * are allowed in the presence of various reservations.
396c66ac9dbSNicholas Bellinger 		 * See spc4r17, table 46
397c66ac9dbSNicholas Bellinger 		 */
398c66ac9dbSNicholas Bellinger 		switch (cdb[1] & 0x1f) {
399c66ac9dbSNicholas Bellinger 		case PRO_CLEAR:
400c66ac9dbSNicholas Bellinger 		case PRO_PREEMPT:
401c66ac9dbSNicholas Bellinger 		case PRO_PREEMPT_AND_ABORT:
402c66ac9dbSNicholas Bellinger 			ret = (registered_nexus) ? 0 : 1;
403c66ac9dbSNicholas Bellinger 			break;
404c66ac9dbSNicholas Bellinger 		case PRO_REGISTER:
405c66ac9dbSNicholas Bellinger 		case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
406c66ac9dbSNicholas Bellinger 			ret = 0;
407c66ac9dbSNicholas Bellinger 			break;
408c66ac9dbSNicholas Bellinger 		case PRO_REGISTER_AND_MOVE:
409c66ac9dbSNicholas Bellinger 		case PRO_RESERVE:
410c66ac9dbSNicholas Bellinger 			ret = 1;
411c66ac9dbSNicholas Bellinger 			break;
412c66ac9dbSNicholas Bellinger 		case PRO_RELEASE:
413c66ac9dbSNicholas Bellinger 			ret = (registered_nexus) ? 0 : 1;
414c66ac9dbSNicholas Bellinger 			break;
415c66ac9dbSNicholas Bellinger 		default:
4166708bb27SAndy Grover 			pr_err("Unknown PERSISTENT_RESERVE_OUT service"
417c66ac9dbSNicholas Bellinger 				" action: 0x%02x\n", cdb[1] & 0x1f);
418e3d6f909SAndy Grover 			return -EINVAL;
419c66ac9dbSNicholas Bellinger 		}
420c66ac9dbSNicholas Bellinger 		break;
421c66ac9dbSNicholas Bellinger 	case RELEASE:
422c66ac9dbSNicholas Bellinger 	case RELEASE_10:
423eacac00cSChristoph Hellwig 		/* Handled by CRH=1 in target_scsi2_reservation_release() */
424c66ac9dbSNicholas Bellinger 		ret = 0;
425c66ac9dbSNicholas Bellinger 		break;
426c66ac9dbSNicholas Bellinger 	case RESERVE:
427c66ac9dbSNicholas Bellinger 	case RESERVE_10:
428eacac00cSChristoph Hellwig 		/* Handled by CRH=1 in target_scsi2_reservation_reserve() */
429c66ac9dbSNicholas Bellinger 		ret = 0;
430c66ac9dbSNicholas Bellinger 		break;
431c66ac9dbSNicholas Bellinger 	case TEST_UNIT_READY:
432c66ac9dbSNicholas Bellinger 		ret = (legacy) ? 1 : 0; /* Conflict for legacy */
433c66ac9dbSNicholas Bellinger 		break;
434c66ac9dbSNicholas Bellinger 	case MAINTENANCE_IN:
435c66ac9dbSNicholas Bellinger 		switch (cdb[1] & 0x1f) {
436c66ac9dbSNicholas Bellinger 		case MI_MANAGEMENT_PROTOCOL_IN:
437c66ac9dbSNicholas Bellinger 			if (registered_nexus) {
438c66ac9dbSNicholas Bellinger 				ret = 0;
439c66ac9dbSNicholas Bellinger 				break;
440c66ac9dbSNicholas Bellinger 			}
441c66ac9dbSNicholas Bellinger 			ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
442c66ac9dbSNicholas Bellinger 			break;
443c66ac9dbSNicholas Bellinger 		case MI_REPORT_SUPPORTED_OPERATION_CODES:
444c66ac9dbSNicholas Bellinger 		case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
445c66ac9dbSNicholas Bellinger 			if (legacy) {
446c66ac9dbSNicholas Bellinger 				ret = 1;
447c66ac9dbSNicholas Bellinger 				break;
448c66ac9dbSNicholas Bellinger 			}
449c66ac9dbSNicholas Bellinger 			if (registered_nexus) {
450c66ac9dbSNicholas Bellinger 				ret = 0;
451c66ac9dbSNicholas Bellinger 				break;
452c66ac9dbSNicholas Bellinger 			}
453c66ac9dbSNicholas Bellinger 			ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
454c66ac9dbSNicholas Bellinger 			break;
455c66ac9dbSNicholas Bellinger 		case MI_REPORT_ALIASES:
456c66ac9dbSNicholas Bellinger 		case MI_REPORT_IDENTIFYING_INFORMATION:
457c66ac9dbSNicholas Bellinger 		case MI_REPORT_PRIORITY:
458c66ac9dbSNicholas Bellinger 		case MI_REPORT_TARGET_PGS:
459c66ac9dbSNicholas Bellinger 		case MI_REPORT_TIMESTAMP:
460c66ac9dbSNicholas Bellinger 			ret = 0; /* Allowed */
461c66ac9dbSNicholas Bellinger 			break;
462c66ac9dbSNicholas Bellinger 		default:
4636708bb27SAndy Grover 			pr_err("Unknown MI Service Action: 0x%02x\n",
464c66ac9dbSNicholas Bellinger 				(cdb[1] & 0x1f));
465e3d6f909SAndy Grover 			return -EINVAL;
466c66ac9dbSNicholas Bellinger 		}
467c66ac9dbSNicholas Bellinger 		break;
468c66ac9dbSNicholas Bellinger 	case ACCESS_CONTROL_IN:
469c66ac9dbSNicholas Bellinger 	case ACCESS_CONTROL_OUT:
470c66ac9dbSNicholas Bellinger 	case INQUIRY:
471c66ac9dbSNicholas Bellinger 	case LOG_SENSE:
47285686f69SHannes Reinecke 	case SERVICE_ACTION_IN_12:
473c66ac9dbSNicholas Bellinger 	case REPORT_LUNS:
474c66ac9dbSNicholas Bellinger 	case REQUEST_SENSE:
4756816966aSMarco Sanvido 	case PERSISTENT_RESERVE_IN:
476c66ac9dbSNicholas Bellinger 		ret = 0; /*/ Allowed CDBs */
477c66ac9dbSNicholas Bellinger 		break;
478c66ac9dbSNicholas Bellinger 	default:
479c66ac9dbSNicholas Bellinger 		other_cdb = 1;
480c66ac9dbSNicholas Bellinger 		break;
481c66ac9dbSNicholas Bellinger 	}
482c66ac9dbSNicholas Bellinger 	/*
48325985edcSLucas De Marchi 	 * Case where the CDB is explicitly allowed in the above switch
484c66ac9dbSNicholas Bellinger 	 * statement.
485c66ac9dbSNicholas Bellinger 	 */
4866708bb27SAndy Grover 	if (!ret && !other_cdb) {
487125d0119SHannes Reinecke 		pr_debug("Allowing explicit CDB: 0x%02x for %s"
488c66ac9dbSNicholas Bellinger 			" reservation holder\n", cdb[0],
489c66ac9dbSNicholas Bellinger 			core_scsi3_pr_dump_type(pr_reg_type));
4908b1e1244SAndy Grover 
491c66ac9dbSNicholas Bellinger 		return ret;
492c66ac9dbSNicholas Bellinger 	}
493c66ac9dbSNicholas Bellinger 	/*
494c66ac9dbSNicholas Bellinger 	 * Check if write exclusive initiator ports *NOT* holding the
495c66ac9dbSNicholas Bellinger 	 * WRITE_EXCLUSIVE_* reservation.
496c66ac9dbSNicholas Bellinger 	 */
497ee1b1b9cSAndy Grover 	if (we && !registered_nexus) {
498c66ac9dbSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE) {
499c66ac9dbSNicholas Bellinger 			/*
500c66ac9dbSNicholas Bellinger 			 * Conflict for write exclusive
501c66ac9dbSNicholas Bellinger 			 */
5026708bb27SAndy Grover 			pr_debug("%s Conflict for unregistered nexus"
503c66ac9dbSNicholas Bellinger 				" %s CDB: 0x%02x to %s reservation\n",
504c66ac9dbSNicholas Bellinger 				transport_dump_cmd_direction(cmd),
505c66ac9dbSNicholas Bellinger 				se_sess->se_node_acl->initiatorname, cdb[0],
506c66ac9dbSNicholas Bellinger 				core_scsi3_pr_dump_type(pr_reg_type));
507c66ac9dbSNicholas Bellinger 			return 1;
508c66ac9dbSNicholas Bellinger 		} else {
509c66ac9dbSNicholas Bellinger 			/*
510c66ac9dbSNicholas Bellinger 			 * Allow non WRITE CDBs for all Write Exclusive
511c66ac9dbSNicholas Bellinger 			 * PR TYPEs to pass for registered and
512c66ac9dbSNicholas Bellinger 			 * non-registered_nexuxes NOT holding the reservation.
513c66ac9dbSNicholas Bellinger 			 *
514c66ac9dbSNicholas Bellinger 			 * We only make noise for the unregisterd nexuses,
515c66ac9dbSNicholas Bellinger 			 * as we expect registered non-reservation holding
516c66ac9dbSNicholas Bellinger 			 * nexuses to issue CDBs.
517c66ac9dbSNicholas Bellinger 			 */
5188b1e1244SAndy Grover 
5196708bb27SAndy Grover 			if (!registered_nexus) {
520125d0119SHannes Reinecke 				pr_debug("Allowing implicit CDB: 0x%02x"
521c66ac9dbSNicholas Bellinger 					" for %s reservation on unregistered"
522c66ac9dbSNicholas Bellinger 					" nexus\n", cdb[0],
523c66ac9dbSNicholas Bellinger 					core_scsi3_pr_dump_type(pr_reg_type));
524c66ac9dbSNicholas Bellinger 			}
5258b1e1244SAndy Grover 
526c66ac9dbSNicholas Bellinger 			return 0;
527c66ac9dbSNicholas Bellinger 		}
528c66ac9dbSNicholas Bellinger 	} else if ((reg_only) || (all_reg)) {
529c66ac9dbSNicholas Bellinger 		if (registered_nexus) {
530c66ac9dbSNicholas Bellinger 			/*
531c66ac9dbSNicholas Bellinger 			 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
532c66ac9dbSNicholas Bellinger 			 * allow commands from registered nexuses.
533c66ac9dbSNicholas Bellinger 			 */
5348b1e1244SAndy Grover 
535125d0119SHannes Reinecke 			pr_debug("Allowing implicit CDB: 0x%02x for %s"
536c66ac9dbSNicholas Bellinger 				" reservation\n", cdb[0],
537c66ac9dbSNicholas Bellinger 				core_scsi3_pr_dump_type(pr_reg_type));
5388b1e1244SAndy Grover 
539c66ac9dbSNicholas Bellinger 			return 0;
540c66ac9dbSNicholas Bellinger 		}
5411ecc7586SLee Duncan        } else if (we && registered_nexus) {
5421ecc7586SLee Duncan                /*
5431ecc7586SLee Duncan                 * Reads are allowed for Write Exclusive locks
5441ecc7586SLee Duncan                 * from all registrants.
5451ecc7586SLee Duncan                 */
5461ecc7586SLee Duncan                if (cmd->data_direction == DMA_FROM_DEVICE) {
5471ecc7586SLee Duncan                        pr_debug("Allowing READ CDB: 0x%02x for %s"
5481ecc7586SLee Duncan                                " reservation\n", cdb[0],
5491ecc7586SLee Duncan                                core_scsi3_pr_dump_type(pr_reg_type));
5501ecc7586SLee Duncan 
5511ecc7586SLee Duncan                        return 0;
5521ecc7586SLee Duncan                }
553c66ac9dbSNicholas Bellinger 	}
5546708bb27SAndy Grover 	pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
555c66ac9dbSNicholas Bellinger 		" for %s reservation\n", transport_dump_cmd_direction(cmd),
556c66ac9dbSNicholas Bellinger 		(registered_nexus) ? "" : "un",
557c66ac9dbSNicholas Bellinger 		se_sess->se_node_acl->initiatorname, cdb[0],
558c66ac9dbSNicholas Bellinger 		core_scsi3_pr_dump_type(pr_reg_type));
559c66ac9dbSNicholas Bellinger 
560c66ac9dbSNicholas Bellinger 	return 1; /* Conflict by default */
561c66ac9dbSNicholas Bellinger }
562c66ac9dbSNicholas Bellinger 
563de103c93SChristoph Hellwig static sense_reason_t
target_scsi3_pr_reservation_check(struct se_cmd * cmd)564de103c93SChristoph Hellwig target_scsi3_pr_reservation_check(struct se_cmd *cmd)
565d977f437SChristoph Hellwig {
566d977f437SChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
567d977f437SChristoph Hellwig 	struct se_session *sess = cmd->se_sess;
568d977f437SChristoph Hellwig 	u32 pr_reg_type;
5699fcb57f3SChristoph Hellwig 	bool isid_mismatch = false;
570d977f437SChristoph Hellwig 
571d977f437SChristoph Hellwig 	if (!dev->dev_pr_res_holder)
572d977f437SChristoph Hellwig 		return 0;
573d977f437SChristoph Hellwig 
574d977f437SChristoph Hellwig 	pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
575d977f437SChristoph Hellwig 	cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
576d977f437SChristoph Hellwig 	if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl)
577d977f437SChristoph Hellwig 		goto check_nonholder;
578d977f437SChristoph Hellwig 
579d977f437SChristoph Hellwig 	if (dev->dev_pr_res_holder->isid_present_at_reg) {
580d977f437SChristoph Hellwig 		if (dev->dev_pr_res_holder->pr_reg_bin_isid !=
581d977f437SChristoph Hellwig 		    sess->sess_bin_isid) {
5829fcb57f3SChristoph Hellwig 			isid_mismatch = true;
583d977f437SChristoph Hellwig 			goto check_nonholder;
584d977f437SChristoph Hellwig 		}
585d977f437SChristoph Hellwig 	}
586d977f437SChristoph Hellwig 
587d977f437SChristoph Hellwig 	return 0;
588d977f437SChristoph Hellwig 
589d977f437SChristoph Hellwig check_nonholder:
5909fcb57f3SChristoph Hellwig 	if (core_scsi3_pr_seq_non_holder(cmd, pr_reg_type, isid_mismatch))
591de103c93SChristoph Hellwig 		return TCM_RESERVATION_CONFLICT;
592d977f437SChristoph Hellwig 	return 0;
593d977f437SChristoph Hellwig }
594d977f437SChristoph Hellwig 
core_scsi3_pr_generation(struct se_device * dev)595c66ac9dbSNicholas Bellinger static u32 core_scsi3_pr_generation(struct se_device *dev)
596c66ac9dbSNicholas Bellinger {
597c66ac9dbSNicholas Bellinger 	u32 prg;
5980fd97ccfSChristoph Hellwig 
599c66ac9dbSNicholas Bellinger 	/*
600c66ac9dbSNicholas Bellinger 	 * PRGeneration field shall contain the value of a 32-bit wrapping
601c66ac9dbSNicholas Bellinger 	 * counter mainted by the device server.
602c66ac9dbSNicholas Bellinger 	 *
603c66ac9dbSNicholas Bellinger 	 * Note that this is done regardless of Active Persist across
604c66ac9dbSNicholas Bellinger 	 * Target PowerLoss (APTPL)
605c66ac9dbSNicholas Bellinger 	 *
606c66ac9dbSNicholas Bellinger 	 * See spc4r17 section 6.3.12 READ_KEYS service action
607c66ac9dbSNicholas Bellinger 	 */
608c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
6090fd97ccfSChristoph Hellwig 	prg = dev->t10_pr.pr_generation++;
610c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
611c66ac9dbSNicholas Bellinger 
612c66ac9dbSNicholas Bellinger 	return prg;
613c66ac9dbSNicholas Bellinger }
614c66ac9dbSNicholas Bellinger 
__core_scsi3_do_alloc_registration(struct se_device * dev,struct se_node_acl * nacl,struct se_lun * lun,struct se_dev_entry * dest_deve,u64 mapped_lun,unsigned char * isid,u64 sa_res_key,int all_tg_pt,int aptpl)615c66ac9dbSNicholas Bellinger static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
616c66ac9dbSNicholas Bellinger 	struct se_device *dev,
617c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
61879dc9c9eSNicholas Bellinger 	struct se_lun *lun,
6193ccd6e83SNicholas Bellinger 	struct se_dev_entry *dest_deve,
620f2d30680SHannes Reinecke 	u64 mapped_lun,
621c66ac9dbSNicholas Bellinger 	unsigned char *isid,
622c66ac9dbSNicholas Bellinger 	u64 sa_res_key,
623c66ac9dbSNicholas Bellinger 	int all_tg_pt,
624c66ac9dbSNicholas Bellinger 	int aptpl)
625c66ac9dbSNicholas Bellinger {
626c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
627c66ac9dbSNicholas Bellinger 
628c66ac9dbSNicholas Bellinger 	pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
6296708bb27SAndy Grover 	if (!pr_reg) {
6306708bb27SAndy Grover 		pr_err("Unable to allocate struct t10_pr_registration\n");
631c66ac9dbSNicholas Bellinger 		return NULL;
632c66ac9dbSNicholas Bellinger 	}
633c66ac9dbSNicholas Bellinger 
634c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_list);
635c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
636c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
637c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
638c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
639c66ac9dbSNicholas Bellinger 	atomic_set(&pr_reg->pr_res_holders, 0);
640c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_nacl = nacl;
6413ccd6e83SNicholas Bellinger 	/*
6423ccd6e83SNicholas Bellinger 	 * For destination registrations for ALL_TG_PT=1 and SPEC_I_PT=1,
6433ccd6e83SNicholas Bellinger 	 * the se_dev_entry->pr_ref will have been already obtained by
6443ccd6e83SNicholas Bellinger 	 * core_get_se_deve_from_rtpi() or __core_scsi3_alloc_registration().
6453ccd6e83SNicholas Bellinger 	 *
6463ccd6e83SNicholas Bellinger 	 * Otherwise, locate se_dev_entry now and obtain a reference until
6473ccd6e83SNicholas Bellinger 	 * registration completes in __core_scsi3_add_registration().
6483ccd6e83SNicholas Bellinger 	 */
6493ccd6e83SNicholas Bellinger 	if (dest_deve) {
6503ccd6e83SNicholas Bellinger 		pr_reg->pr_reg_deve = dest_deve;
6513ccd6e83SNicholas Bellinger 	} else {
6523ccd6e83SNicholas Bellinger 		rcu_read_lock();
6533ccd6e83SNicholas Bellinger 		pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
6543ccd6e83SNicholas Bellinger 		if (!pr_reg->pr_reg_deve) {
6553ccd6e83SNicholas Bellinger 			rcu_read_unlock();
6563ccd6e83SNicholas Bellinger 			pr_err("Unable to locate PR deve %s mapped_lun: %llu\n",
6573ccd6e83SNicholas Bellinger 				nacl->initiatorname, mapped_lun);
6583ccd6e83SNicholas Bellinger 			kmem_cache_free(t10_pr_reg_cache, pr_reg);
6593ccd6e83SNicholas Bellinger 			return NULL;
6603ccd6e83SNicholas Bellinger 		}
6613ccd6e83SNicholas Bellinger 		kref_get(&pr_reg->pr_reg_deve->pr_kref);
6623ccd6e83SNicholas Bellinger 		rcu_read_unlock();
6633ccd6e83SNicholas Bellinger 	}
66479dc9c9eSNicholas Bellinger 	pr_reg->pr_res_mapped_lun = mapped_lun;
66579dc9c9eSNicholas Bellinger 	pr_reg->pr_aptpl_target_lun = lun->unpacked_lun;
666adf653f9SChristoph Hellwig 	pr_reg->tg_pt_sep_rtpi = lun->lun_tpg->tpg_rtpi;
667c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_key = sa_res_key;
668c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_all_tg_pt = all_tg_pt;
669c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_aptpl = aptpl;
670c66ac9dbSNicholas Bellinger 	/*
671c66ac9dbSNicholas Bellinger 	 * If an ISID value for this SCSI Initiator Port exists,
672c66ac9dbSNicholas Bellinger 	 * save it to the registration now.
673c66ac9dbSNicholas Bellinger 	 */
674c66ac9dbSNicholas Bellinger 	if (isid != NULL) {
675c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
676c66ac9dbSNicholas Bellinger 		snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
677c66ac9dbSNicholas Bellinger 		pr_reg->isid_present_at_reg = 1;
678c66ac9dbSNicholas Bellinger 	}
679c66ac9dbSNicholas Bellinger 
680c66ac9dbSNicholas Bellinger 	return pr_reg;
681c66ac9dbSNicholas Bellinger }
682c66ac9dbSNicholas Bellinger 
683c66ac9dbSNicholas Bellinger static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
684c66ac9dbSNicholas Bellinger static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
685c66ac9dbSNicholas Bellinger 
686c66ac9dbSNicholas Bellinger /*
687c66ac9dbSNicholas Bellinger  * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
688c66ac9dbSNicholas Bellinger  * modes.
689c66ac9dbSNicholas Bellinger  */
__core_scsi3_alloc_registration(struct se_device * dev,struct se_node_acl * nacl,struct se_lun * lun,struct se_dev_entry * deve,u64 mapped_lun,unsigned char * isid,u64 sa_res_key,int all_tg_pt,int aptpl)690c66ac9dbSNicholas Bellinger static struct t10_pr_registration *__core_scsi3_alloc_registration(
691c66ac9dbSNicholas Bellinger 	struct se_device *dev,
692c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
69379dc9c9eSNicholas Bellinger 	struct se_lun *lun,
694c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve,
695f2d30680SHannes Reinecke 	u64 mapped_lun,
696c66ac9dbSNicholas Bellinger 	unsigned char *isid,
697c66ac9dbSNicholas Bellinger 	u64 sa_res_key,
698c66ac9dbSNicholas Bellinger 	int all_tg_pt,
699c66ac9dbSNicholas Bellinger 	int aptpl)
700c66ac9dbSNicholas Bellinger {
701c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve_tmp;
702c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl_tmp;
70379dc9c9eSNicholas Bellinger 	struct se_lun_acl *lacl_tmp;
704adf653f9SChristoph Hellwig 	struct se_lun *lun_tmp, *next, *dest_lun;
7059ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
706c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
707c66ac9dbSNicholas Bellinger 	int ret;
708c66ac9dbSNicholas Bellinger 	/*
709c66ac9dbSNicholas Bellinger 	 * Create a registration for the I_T Nexus upon which the
710c66ac9dbSNicholas Bellinger 	 * PROUT REGISTER was received.
711c66ac9dbSNicholas Bellinger 	 */
71279dc9c9eSNicholas Bellinger 	pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, lun, deve, mapped_lun,
71379dc9c9eSNicholas Bellinger 						    isid, sa_res_key, all_tg_pt,
71479dc9c9eSNicholas Bellinger 						    aptpl);
7156708bb27SAndy Grover 	if (!pr_reg)
716c66ac9dbSNicholas Bellinger 		return NULL;
717c66ac9dbSNicholas Bellinger 	/*
718c66ac9dbSNicholas Bellinger 	 * Return pointer to pr_reg for ALL_TG_PT=0
719c66ac9dbSNicholas Bellinger 	 */
7206708bb27SAndy Grover 	if (!all_tg_pt)
721c66ac9dbSNicholas Bellinger 		return pr_reg;
722c66ac9dbSNicholas Bellinger 	/*
723c66ac9dbSNicholas Bellinger 	 * Create list of matching SCSI Initiator Port registrations
724c66ac9dbSNicholas Bellinger 	 * for ALL_TG_PT=1
725c66ac9dbSNicholas Bellinger 	 */
726c66ac9dbSNicholas Bellinger 	spin_lock(&dev->se_port_lock);
727adf653f9SChristoph Hellwig 	list_for_each_entry_safe(lun_tmp, next, &dev->dev_sep_list, lun_dev_link) {
7289e37d042SNicholas Bellinger 		if (!percpu_ref_tryget_live(&lun_tmp->lun_ref))
7299e37d042SNicholas Bellinger 			continue;
730c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->se_port_lock);
731c66ac9dbSNicholas Bellinger 
7321adff1b3SNicholas Bellinger 		spin_lock(&lun_tmp->lun_deve_lock);
733adf653f9SChristoph Hellwig 		list_for_each_entry(deve_tmp, &lun_tmp->lun_deve_list, lun_link) {
734c66ac9dbSNicholas Bellinger 			/*
735c66ac9dbSNicholas Bellinger 			 * This pointer will be NULL for demo mode MappedLUNs
736125d0119SHannes Reinecke 			 * that have not been make explicit via a ConfigFS
737c66ac9dbSNicholas Bellinger 			 * MappedLUN group for the SCSI Initiator Node ACL.
738c66ac9dbSNicholas Bellinger 			 */
7396708bb27SAndy Grover 			if (!deve_tmp->se_lun_acl)
740c66ac9dbSNicholas Bellinger 				continue;
741c66ac9dbSNicholas Bellinger 
742ef4f7e4bSDmitry Bogdanov 			lacl_tmp = deve_tmp->se_lun_acl;
74379dc9c9eSNicholas Bellinger 			nacl_tmp = lacl_tmp->se_lun_nacl;
744c66ac9dbSNicholas Bellinger 			/*
745c66ac9dbSNicholas Bellinger 			 * Skip the matching struct se_node_acl that is allocated
746c66ac9dbSNicholas Bellinger 			 * above..
747c66ac9dbSNicholas Bellinger 			 */
748c66ac9dbSNicholas Bellinger 			if (nacl == nacl_tmp)
749c66ac9dbSNicholas Bellinger 				continue;
750c66ac9dbSNicholas Bellinger 			/*
751c66ac9dbSNicholas Bellinger 			 * Only perform PR registrations for target ports on
752c66ac9dbSNicholas Bellinger 			 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
753c66ac9dbSNicholas Bellinger 			 * arrived.
754c66ac9dbSNicholas Bellinger 			 */
755c66ac9dbSNicholas Bellinger 			if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
756c66ac9dbSNicholas Bellinger 				continue;
757c66ac9dbSNicholas Bellinger 			/*
758c66ac9dbSNicholas Bellinger 			 * Look for a matching Initiator Node ACL in ASCII format
759c66ac9dbSNicholas Bellinger 			 */
760c66ac9dbSNicholas Bellinger 			if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
761c66ac9dbSNicholas Bellinger 				continue;
762c66ac9dbSNicholas Bellinger 
76329a05deeSNicholas Bellinger 			kref_get(&deve_tmp->pr_kref);
7641adff1b3SNicholas Bellinger 			spin_unlock(&lun_tmp->lun_deve_lock);
765c66ac9dbSNicholas Bellinger 			/*
766c66ac9dbSNicholas Bellinger 			 * Grab a configfs group dependency that is released
767c66ac9dbSNicholas Bellinger 			 * for the exception path at label out: below, or upon
768c66ac9dbSNicholas Bellinger 			 * completion of adding ALL_TG_PT=1 registrations in
769c66ac9dbSNicholas Bellinger 			 * __core_scsi3_add_registration()
770c66ac9dbSNicholas Bellinger 			 */
771c66ac9dbSNicholas Bellinger 			ret = core_scsi3_lunacl_depend_item(deve_tmp);
772c66ac9dbSNicholas Bellinger 			if (ret < 0) {
7736708bb27SAndy Grover 				pr_err("core_scsi3_lunacl_depend"
774c66ac9dbSNicholas Bellinger 						"_item() failed\n");
7759e37d042SNicholas Bellinger 				percpu_ref_put(&lun_tmp->lun_ref);
77629a05deeSNicholas Bellinger 				kref_put(&deve_tmp->pr_kref, target_pr_kref_release);
777c66ac9dbSNicholas Bellinger 				goto out;
778c66ac9dbSNicholas Bellinger 			}
779c66ac9dbSNicholas Bellinger 			/*
780c66ac9dbSNicholas Bellinger 			 * Located a matching SCSI Initiator Port on a different
781c66ac9dbSNicholas Bellinger 			 * port, allocate the pr_reg_atp and attach it to the
782c66ac9dbSNicholas Bellinger 			 * pr_reg->pr_reg_atp_list that will be processed once
783c66ac9dbSNicholas Bellinger 			 * the original *pr_reg is processed in
784c66ac9dbSNicholas Bellinger 			 * __core_scsi3_add_registration()
785c66ac9dbSNicholas Bellinger 			 */
786ef4f7e4bSDmitry Bogdanov 			dest_lun = deve_tmp->se_lun;
78779dc9c9eSNicholas Bellinger 
788c66ac9dbSNicholas Bellinger 			pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
789adf653f9SChristoph Hellwig 						nacl_tmp, dest_lun, deve_tmp,
79079dc9c9eSNicholas Bellinger 						deve_tmp->mapped_lun, NULL,
791c66ac9dbSNicholas Bellinger 						sa_res_key, all_tg_pt, aptpl);
7926708bb27SAndy Grover 			if (!pr_reg_atp) {
7939e37d042SNicholas Bellinger 				percpu_ref_put(&lun_tmp->lun_ref);
794c66ac9dbSNicholas Bellinger 				core_scsi3_lunacl_undepend_item(deve_tmp);
795c66ac9dbSNicholas Bellinger 				goto out;
796c66ac9dbSNicholas Bellinger 			}
797c66ac9dbSNicholas Bellinger 
798c66ac9dbSNicholas Bellinger 			list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
799c66ac9dbSNicholas Bellinger 				      &pr_reg->pr_reg_atp_list);
8001adff1b3SNicholas Bellinger 			spin_lock(&lun_tmp->lun_deve_lock);
801c66ac9dbSNicholas Bellinger 		}
8021adff1b3SNicholas Bellinger 		spin_unlock(&lun_tmp->lun_deve_lock);
803c66ac9dbSNicholas Bellinger 
804c66ac9dbSNicholas Bellinger 		spin_lock(&dev->se_port_lock);
8059e37d042SNicholas Bellinger 		percpu_ref_put(&lun_tmp->lun_ref);
806c66ac9dbSNicholas Bellinger 	}
807c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->se_port_lock);
808c66ac9dbSNicholas Bellinger 
809c66ac9dbSNicholas Bellinger 	return pr_reg;
810c66ac9dbSNicholas Bellinger out:
811c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
812c66ac9dbSNicholas Bellinger 			&pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
813c66ac9dbSNicholas Bellinger 		list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
814c66ac9dbSNicholas Bellinger 		core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
815c66ac9dbSNicholas Bellinger 		kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
816c66ac9dbSNicholas Bellinger 	}
817c66ac9dbSNicholas Bellinger 	kmem_cache_free(t10_pr_reg_cache, pr_reg);
818c66ac9dbSNicholas Bellinger 	return NULL;
819c66ac9dbSNicholas Bellinger }
820c66ac9dbSNicholas Bellinger 
core_scsi3_alloc_aptpl_registration(struct t10_reservation * pr_tmpl,u64 sa_res_key,unsigned char * i_port,unsigned char * isid,u64 mapped_lun,unsigned char * t_port,u16 tpgt,u64 target_lun,int res_holder,int all_tg_pt,u8 type)821c66ac9dbSNicholas Bellinger int core_scsi3_alloc_aptpl_registration(
822e3d6f909SAndy Grover 	struct t10_reservation *pr_tmpl,
823c66ac9dbSNicholas Bellinger 	u64 sa_res_key,
824c66ac9dbSNicholas Bellinger 	unsigned char *i_port,
825c66ac9dbSNicholas Bellinger 	unsigned char *isid,
826f2d30680SHannes Reinecke 	u64 mapped_lun,
827c66ac9dbSNicholas Bellinger 	unsigned char *t_port,
828c66ac9dbSNicholas Bellinger 	u16 tpgt,
829f2d30680SHannes Reinecke 	u64 target_lun,
830c66ac9dbSNicholas Bellinger 	int res_holder,
831c66ac9dbSNicholas Bellinger 	int all_tg_pt,
832c66ac9dbSNicholas Bellinger 	u8 type)
833c66ac9dbSNicholas Bellinger {
834c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
835c66ac9dbSNicholas Bellinger 
8366708bb27SAndy Grover 	if (!i_port || !t_port || !sa_res_key) {
8376708bb27SAndy Grover 		pr_err("Illegal parameters for APTPL registration\n");
838e3d6f909SAndy Grover 		return -EINVAL;
839c66ac9dbSNicholas Bellinger 	}
840c66ac9dbSNicholas Bellinger 
841c66ac9dbSNicholas Bellinger 	pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
8426708bb27SAndy Grover 	if (!pr_reg) {
8436708bb27SAndy Grover 		pr_err("Unable to allocate struct t10_pr_registration\n");
844e3d6f909SAndy Grover 		return -ENOMEM;
845c66ac9dbSNicholas Bellinger 	}
846c66ac9dbSNicholas Bellinger 
847c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_list);
848c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
849c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
850c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
851c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
852c66ac9dbSNicholas Bellinger 	atomic_set(&pr_reg->pr_res_holders, 0);
853c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_nacl = NULL;
854c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_deve = NULL;
855c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_mapped_lun = mapped_lun;
856c66ac9dbSNicholas Bellinger 	pr_reg->pr_aptpl_target_lun = target_lun;
857c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_key = sa_res_key;
858c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_all_tg_pt = all_tg_pt;
859c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_aptpl = 1;
860c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
861c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_type = type;
862c66ac9dbSNicholas Bellinger 	/*
863c66ac9dbSNicholas Bellinger 	 * If an ISID value had been saved in APTPL metadata for this
864c66ac9dbSNicholas Bellinger 	 * SCSI Initiator Port, restore it now.
865c66ac9dbSNicholas Bellinger 	 */
866c66ac9dbSNicholas Bellinger 	if (isid != NULL) {
867c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
868c66ac9dbSNicholas Bellinger 		snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
869c66ac9dbSNicholas Bellinger 		pr_reg->isid_present_at_reg = 1;
870c66ac9dbSNicholas Bellinger 	}
871c66ac9dbSNicholas Bellinger 	/*
872c66ac9dbSNicholas Bellinger 	 * Copy the i_port and t_port information from caller.
873c66ac9dbSNicholas Bellinger 	 */
874c66ac9dbSNicholas Bellinger 	snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
875c66ac9dbSNicholas Bellinger 	snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
876c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_tpgt = tpgt;
877c66ac9dbSNicholas Bellinger 	/*
878c66ac9dbSNicholas Bellinger 	 * Set pr_res_holder from caller, the pr_reg who is the reservation
879c66ac9dbSNicholas Bellinger 	 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
880c66ac9dbSNicholas Bellinger 	 * the Initiator Node LUN ACL from the fabric module is created for
881c66ac9dbSNicholas Bellinger 	 * this registration.
882c66ac9dbSNicholas Bellinger 	 */
883c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_holder = res_holder;
884c66ac9dbSNicholas Bellinger 
885c66ac9dbSNicholas Bellinger 	list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
8866708bb27SAndy Grover 	pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
887c66ac9dbSNicholas Bellinger 			" metadata\n", (res_holder) ? "+reservation" : "");
888c66ac9dbSNicholas Bellinger 	return 0;
889c66ac9dbSNicholas Bellinger }
890c66ac9dbSNicholas Bellinger 
core_scsi3_aptpl_reserve(struct se_device * dev,struct se_portal_group * tpg,struct se_node_acl * node_acl,struct t10_pr_registration * pr_reg)891c66ac9dbSNicholas Bellinger static void core_scsi3_aptpl_reserve(
892c66ac9dbSNicholas Bellinger 	struct se_device *dev,
893c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
894c66ac9dbSNicholas Bellinger 	struct se_node_acl *node_acl,
895c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg)
896c66ac9dbSNicholas Bellinger {
8974db6dfe6SChaitanya Kulkarni 	char i_buf[PR_REG_ISID_ID_LEN] = { };
898c66ac9dbSNicholas Bellinger 
899d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
900c66ac9dbSNicholas Bellinger 
901c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
902c66ac9dbSNicholas Bellinger 	dev->dev_pr_res_holder = pr_reg;
903c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
904c66ac9dbSNicholas Bellinger 
9056708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
906c66ac9dbSNicholas Bellinger 		" new reservation holder TYPE: %s ALL_TG_PT: %d\n",
90730c7ca93SDavid Disseldorp 		tpg->se_tpg_tfo->fabric_name,
908c66ac9dbSNicholas Bellinger 		core_scsi3_pr_dump_type(pr_reg->pr_res_type),
909c66ac9dbSNicholas Bellinger 		(pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
9106708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
91130c7ca93SDavid Disseldorp 		tpg->se_tpg_tfo->fabric_name, node_acl->initiatorname,
912d2843c17SAndy Grover 		i_buf);
913c66ac9dbSNicholas Bellinger }
914c66ac9dbSNicholas Bellinger 
915c66ac9dbSNicholas Bellinger static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
91633ce6a87SAndy Grover 				struct t10_pr_registration *, enum register_type, int);
917c66ac9dbSNicholas Bellinger 
__core_scsi3_check_aptpl_registration(struct se_device * dev,struct se_portal_group * tpg,struct se_lun * lun,u64 target_lun,struct se_node_acl * nacl,u64 mapped_lun)918c66ac9dbSNicholas Bellinger static int __core_scsi3_check_aptpl_registration(
919c66ac9dbSNicholas Bellinger 	struct se_device *dev,
920c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
921c66ac9dbSNicholas Bellinger 	struct se_lun *lun,
922f2d30680SHannes Reinecke 	u64 target_lun,
923c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
924f2d30680SHannes Reinecke 	u64 mapped_lun)
925c66ac9dbSNicholas Bellinger {
926c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp;
9270fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
9284db6dfe6SChaitanya Kulkarni 	unsigned char i_port[PR_APTPL_MAX_IPORT_LEN] = { };
9294db6dfe6SChaitanya Kulkarni 	unsigned char t_port[PR_APTPL_MAX_TPORT_LEN] = { };
930c66ac9dbSNicholas Bellinger 	u16 tpgt;
931c66ac9dbSNicholas Bellinger 
932c66ac9dbSNicholas Bellinger 	/*
933c66ac9dbSNicholas Bellinger 	 * Copy Initiator Port information from struct se_node_acl
934c66ac9dbSNicholas Bellinger 	 */
935c66ac9dbSNicholas Bellinger 	snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
936c66ac9dbSNicholas Bellinger 	snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
937e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_wwn(tpg));
938e3d6f909SAndy Grover 	tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
939c66ac9dbSNicholas Bellinger 	/*
940c66ac9dbSNicholas Bellinger 	 * Look for the matching registrations+reservation from those
941c66ac9dbSNicholas Bellinger 	 * created from APTPL metadata.  Note that multiple registrations
942c66ac9dbSNicholas Bellinger 	 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
943c66ac9dbSNicholas Bellinger 	 * TransportIDs.
944c66ac9dbSNicholas Bellinger 	 */
945c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->aptpl_reg_lock);
946c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
947c66ac9dbSNicholas Bellinger 				pr_reg_aptpl_list) {
94892404e60SNicholas Bellinger 
9496708bb27SAndy Grover 		if (!strcmp(pr_reg->pr_iport, i_port) &&
95029a05deeSNicholas Bellinger 		     (pr_reg->pr_res_mapped_lun == mapped_lun) &&
951c66ac9dbSNicholas Bellinger 		    !(strcmp(pr_reg->pr_tport, t_port)) &&
952c66ac9dbSNicholas Bellinger 		     (pr_reg->pr_reg_tpgt == tpgt) &&
953c66ac9dbSNicholas Bellinger 		     (pr_reg->pr_aptpl_target_lun == target_lun)) {
9543ccd6e83SNicholas Bellinger 			/*
9553ccd6e83SNicholas Bellinger 			 * Obtain the ->pr_reg_deve pointer + reference, that
9563ccd6e83SNicholas Bellinger 			 * is released by __core_scsi3_add_registration() below.
9573ccd6e83SNicholas Bellinger 			 */
9583ccd6e83SNicholas Bellinger 			rcu_read_lock();
9593ccd6e83SNicholas Bellinger 			pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
9603ccd6e83SNicholas Bellinger 			if (!pr_reg->pr_reg_deve) {
9613ccd6e83SNicholas Bellinger 				pr_err("Unable to locate PR APTPL %s mapped_lun:"
9623ccd6e83SNicholas Bellinger 					" %llu\n", nacl->initiatorname, mapped_lun);
9633ccd6e83SNicholas Bellinger 				rcu_read_unlock();
9643ccd6e83SNicholas Bellinger 				continue;
9653ccd6e83SNicholas Bellinger 			}
9663ccd6e83SNicholas Bellinger 			kref_get(&pr_reg->pr_reg_deve->pr_kref);
9673ccd6e83SNicholas Bellinger 			rcu_read_unlock();
968c66ac9dbSNicholas Bellinger 
969c66ac9dbSNicholas Bellinger 			pr_reg->pr_reg_nacl = nacl;
970adf653f9SChristoph Hellwig 			pr_reg->tg_pt_sep_rtpi = lun->lun_tpg->tpg_rtpi;
971c66ac9dbSNicholas Bellinger 			list_del(&pr_reg->pr_reg_aptpl_list);
972c66ac9dbSNicholas Bellinger 			spin_unlock(&pr_tmpl->aptpl_reg_lock);
973c66ac9dbSNicholas Bellinger 			/*
974c66ac9dbSNicholas Bellinger 			 * At this point all of the pointers in *pr_reg will
975c66ac9dbSNicholas Bellinger 			 * be setup, so go ahead and add the registration.
976c66ac9dbSNicholas Bellinger 			 */
977c66ac9dbSNicholas Bellinger 			__core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
978c66ac9dbSNicholas Bellinger 			/*
979c66ac9dbSNicholas Bellinger 			 * If this registration is the reservation holder,
980c66ac9dbSNicholas Bellinger 			 * make that happen now..
981c66ac9dbSNicholas Bellinger 			 */
982c66ac9dbSNicholas Bellinger 			if (pr_reg->pr_res_holder)
983c66ac9dbSNicholas Bellinger 				core_scsi3_aptpl_reserve(dev, tpg,
984c66ac9dbSNicholas Bellinger 						nacl, pr_reg);
985c66ac9dbSNicholas Bellinger 			/*
986c66ac9dbSNicholas Bellinger 			 * Reenable pr_aptpl_active to accept new metadata
987c66ac9dbSNicholas Bellinger 			 * updates once the SCSI device is active again..
988c66ac9dbSNicholas Bellinger 			 */
989c66ac9dbSNicholas Bellinger 			spin_lock(&pr_tmpl->aptpl_reg_lock);
990c66ac9dbSNicholas Bellinger 			pr_tmpl->pr_aptpl_active = 1;
991c66ac9dbSNicholas Bellinger 		}
992c66ac9dbSNicholas Bellinger 	}
993c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->aptpl_reg_lock);
994c66ac9dbSNicholas Bellinger 
995c66ac9dbSNicholas Bellinger 	return 0;
996c66ac9dbSNicholas Bellinger }
997c66ac9dbSNicholas Bellinger 
core_scsi3_check_aptpl_registration(struct se_device * dev,struct se_portal_group * tpg,struct se_lun * lun,struct se_node_acl * nacl,u64 mapped_lun)998c66ac9dbSNicholas Bellinger int core_scsi3_check_aptpl_registration(
999c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1000c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
1001c66ac9dbSNicholas Bellinger 	struct se_lun *lun,
1002e2480563SNicholas Bellinger 	struct se_node_acl *nacl,
1003f2d30680SHannes Reinecke 	u64 mapped_lun)
1004c66ac9dbSNicholas Bellinger {
1005d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1006c66ac9dbSNicholas Bellinger 		return 0;
1007c66ac9dbSNicholas Bellinger 
1008c66ac9dbSNicholas Bellinger 	return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
100929a05deeSNicholas Bellinger 						     lun->unpacked_lun, nacl,
101029a05deeSNicholas Bellinger 						     mapped_lun);
1011c66ac9dbSNicholas Bellinger }
1012c66ac9dbSNicholas Bellinger 
__core_scsi3_dump_registration(const struct target_core_fabric_ops * tfo,struct se_device * dev,struct se_node_acl * nacl,struct t10_pr_registration * pr_reg,enum register_type register_type)1013c66ac9dbSNicholas Bellinger static void __core_scsi3_dump_registration(
10149ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo,
1015c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1016c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
1017c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg,
101833ce6a87SAndy Grover 	enum register_type register_type)
1019c66ac9dbSNicholas Bellinger {
1020c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg = nacl->se_tpg;
10214db6dfe6SChaitanya Kulkarni 	char i_buf[PR_REG_ISID_ID_LEN] = { };
1022c66ac9dbSNicholas Bellinger 
1023d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1024c66ac9dbSNicholas Bellinger 
10256708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
102630c7ca93SDavid Disseldorp 		" Node: %s%s\n", tfo->fabric_name, (register_type == REGISTER_AND_MOVE) ?
102733ce6a87SAndy Grover 		"_AND_MOVE" : (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ?
1028c66ac9dbSNicholas Bellinger 		"_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
1029d2843c17SAndy Grover 		i_buf);
10306708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
103130c7ca93SDavid Disseldorp 		 tfo->fabric_name, tfo->tpg_get_wwn(se_tpg),
1032c66ac9dbSNicholas Bellinger 		tfo->tpg_get_tag(se_tpg));
10336708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
103430c7ca93SDavid Disseldorp 		" Port(s)\n",  tfo->fabric_name,
1035c66ac9dbSNicholas Bellinger 		(pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1036e3d6f909SAndy Grover 		dev->transport->name);
10376708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
103830c7ca93SDavid Disseldorp 		" 0x%08x  APTPL: %d\n", tfo->fabric_name,
1039c66ac9dbSNicholas Bellinger 		pr_reg->pr_res_key, pr_reg->pr_res_generation,
1040c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_aptpl);
1041c66ac9dbSNicholas Bellinger }
1042c66ac9dbSNicholas Bellinger 
__core_scsi3_add_registration(struct se_device * dev,struct se_node_acl * nacl,struct t10_pr_registration * pr_reg,enum register_type register_type,int register_move)1043c66ac9dbSNicholas Bellinger static void __core_scsi3_add_registration(
1044c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1045c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
1046c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg,
104733ce6a87SAndy Grover 	enum register_type register_type,
1048c66ac9dbSNicholas Bellinger 	int register_move)
1049c66ac9dbSNicholas Bellinger {
10509ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1051c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
10520fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
105380bfdfa9SNicholas Bellinger 	struct se_dev_entry *deve;
1054c66ac9dbSNicholas Bellinger 
1055c66ac9dbSNicholas Bellinger 	/*
1056c66ac9dbSNicholas Bellinger 	 * Increment PRgeneration counter for struct se_device upon a successful
1057c66ac9dbSNicholas Bellinger 	 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1058c66ac9dbSNicholas Bellinger 	 *
1059c66ac9dbSNicholas Bellinger 	 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1060c66ac9dbSNicholas Bellinger 	 * action, the struct se_device->dev_reservation_lock will already be held,
1061c66ac9dbSNicholas Bellinger 	 * so we do not call core_scsi3_pr_generation() which grabs the lock
1062c66ac9dbSNicholas Bellinger 	 * for the REGISTER.
1063c66ac9dbSNicholas Bellinger 	 */
1064c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_generation = (register_move) ?
10650fd97ccfSChristoph Hellwig 			dev->t10_pr.pr_generation++ :
1066c66ac9dbSNicholas Bellinger 			core_scsi3_pr_generation(dev);
1067c66ac9dbSNicholas Bellinger 
1068c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
1069c66ac9dbSNicholas Bellinger 	list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
1070c66ac9dbSNicholas Bellinger 
1071c66ac9dbSNicholas Bellinger 	__core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1072c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
1073c66ac9dbSNicholas Bellinger 	/*
1074c66ac9dbSNicholas Bellinger 	 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1075c66ac9dbSNicholas Bellinger 	 */
10766708bb27SAndy Grover 	if (!pr_reg->pr_reg_all_tg_pt || register_move)
10773ccd6e83SNicholas Bellinger 		goto out;
1078c66ac9dbSNicholas Bellinger 	/*
1079c66ac9dbSNicholas Bellinger 	 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1080c66ac9dbSNicholas Bellinger 	 * allocated in __core_scsi3_alloc_registration()
1081c66ac9dbSNicholas Bellinger 	 */
1082c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1083c66ac9dbSNicholas Bellinger 			&pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
108480bfdfa9SNicholas Bellinger 		struct se_node_acl *nacl_tmp = pr_reg_tmp->pr_reg_nacl;
108580bfdfa9SNicholas Bellinger 
1086c66ac9dbSNicholas Bellinger 		list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1087c66ac9dbSNicholas Bellinger 
1088c66ac9dbSNicholas Bellinger 		pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1089c66ac9dbSNicholas Bellinger 
1090c66ac9dbSNicholas Bellinger 		spin_lock(&pr_tmpl->registration_lock);
1091c66ac9dbSNicholas Bellinger 		list_add_tail(&pr_reg_tmp->pr_reg_list,
1092c66ac9dbSNicholas Bellinger 			      &pr_tmpl->registration_list);
1093c66ac9dbSNicholas Bellinger 
109480bfdfa9SNicholas Bellinger 		__core_scsi3_dump_registration(tfo, dev, nacl_tmp, pr_reg_tmp,
1095c66ac9dbSNicholas Bellinger 					       register_type);
1096c66ac9dbSNicholas Bellinger 		spin_unlock(&pr_tmpl->registration_lock);
10973ccd6e83SNicholas Bellinger 		/*
10983ccd6e83SNicholas Bellinger 		 * Drop configfs group dependency reference and deve->pr_kref
10993ccd6e83SNicholas Bellinger 		 * obtained from  __core_scsi3_alloc_registration() code.
11003ccd6e83SNicholas Bellinger 		 */
110180bfdfa9SNicholas Bellinger 		rcu_read_lock();
110280bfdfa9SNicholas Bellinger 		deve = pr_reg_tmp->pr_reg_deve;
11033ccd6e83SNicholas Bellinger 		if (deve) {
110480bfdfa9SNicholas Bellinger 			set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
11053ccd6e83SNicholas Bellinger 			core_scsi3_lunacl_undepend_item(deve);
11063ccd6e83SNicholas Bellinger 			pr_reg_tmp->pr_reg_deve = NULL;
1107c66ac9dbSNicholas Bellinger 		}
11083ccd6e83SNicholas Bellinger 		rcu_read_unlock();
11093ccd6e83SNicholas Bellinger 	}
11103ccd6e83SNicholas Bellinger out:
11113ccd6e83SNicholas Bellinger 	/*
11123ccd6e83SNicholas Bellinger 	 * Drop deve->pr_kref obtained in __core_scsi3_do_alloc_registration()
11133ccd6e83SNicholas Bellinger 	 */
11143ccd6e83SNicholas Bellinger 	rcu_read_lock();
11153ccd6e83SNicholas Bellinger 	deve = pr_reg->pr_reg_deve;
11163ccd6e83SNicholas Bellinger 	if (deve) {
11173ccd6e83SNicholas Bellinger 		set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
11183ccd6e83SNicholas Bellinger 		kref_put(&deve->pr_kref, target_pr_kref_release);
11193ccd6e83SNicholas Bellinger 		pr_reg->pr_reg_deve = NULL;
11203ccd6e83SNicholas Bellinger 	}
11213ccd6e83SNicholas Bellinger 	rcu_read_unlock();
1122c66ac9dbSNicholas Bellinger }
1123c66ac9dbSNicholas Bellinger 
core_scsi3_alloc_registration(struct se_device * dev,struct se_node_acl * nacl,struct se_lun * lun,struct se_dev_entry * deve,u64 mapped_lun,unsigned char * isid,u64 sa_res_key,int all_tg_pt,int aptpl,enum register_type register_type,int register_move)1124c66ac9dbSNicholas Bellinger static int core_scsi3_alloc_registration(
1125c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1126c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
112779dc9c9eSNicholas Bellinger 	struct se_lun *lun,
1128c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve,
1129f2d30680SHannes Reinecke 	u64 mapped_lun,
1130c66ac9dbSNicholas Bellinger 	unsigned char *isid,
1131c66ac9dbSNicholas Bellinger 	u64 sa_res_key,
1132c66ac9dbSNicholas Bellinger 	int all_tg_pt,
1133c66ac9dbSNicholas Bellinger 	int aptpl,
113433ce6a87SAndy Grover 	enum register_type register_type,
1135c66ac9dbSNicholas Bellinger 	int register_move)
1136c66ac9dbSNicholas Bellinger {
1137c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1138c66ac9dbSNicholas Bellinger 
113979dc9c9eSNicholas Bellinger 	pr_reg = __core_scsi3_alloc_registration(dev, nacl, lun, deve, mapped_lun,
114079dc9c9eSNicholas Bellinger 						 isid, sa_res_key, all_tg_pt,
114179dc9c9eSNicholas Bellinger 						 aptpl);
11426708bb27SAndy Grover 	if (!pr_reg)
1143e3d6f909SAndy Grover 		return -EPERM;
1144c66ac9dbSNicholas Bellinger 
1145c66ac9dbSNicholas Bellinger 	__core_scsi3_add_registration(dev, nacl, pr_reg,
1146c66ac9dbSNicholas Bellinger 			register_type, register_move);
1147c66ac9dbSNicholas Bellinger 	return 0;
1148c66ac9dbSNicholas Bellinger }
1149c66ac9dbSNicholas Bellinger 
__core_scsi3_locate_pr_reg(struct se_device * dev,struct se_node_acl * nacl,unsigned char * isid)1150c66ac9dbSNicholas Bellinger static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1151c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1152c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
1153c66ac9dbSNicholas Bellinger 	unsigned char *isid)
1154c66ac9dbSNicholas Bellinger {
11550fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
1156c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1157c66ac9dbSNicholas Bellinger 
1158c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
1159c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1160c66ac9dbSNicholas Bellinger 			&pr_tmpl->registration_list, pr_reg_list) {
1161c66ac9dbSNicholas Bellinger 		/*
1162c66ac9dbSNicholas Bellinger 		 * First look for a matching struct se_node_acl
1163c66ac9dbSNicholas Bellinger 		 */
1164c66ac9dbSNicholas Bellinger 		if (pr_reg->pr_reg_nacl != nacl)
1165c66ac9dbSNicholas Bellinger 			continue;
1166c66ac9dbSNicholas Bellinger 
1167c66ac9dbSNicholas Bellinger 		/*
1168c66ac9dbSNicholas Bellinger 		 * If this registration does NOT contain a fabric provided
1169c66ac9dbSNicholas Bellinger 		 * ISID, then we have found a match.
1170c66ac9dbSNicholas Bellinger 		 */
11716708bb27SAndy Grover 		if (!pr_reg->isid_present_at_reg) {
117233940d09SJoern Engel 			atomic_inc_mb(&pr_reg->pr_res_holders);
1173c66ac9dbSNicholas Bellinger 			spin_unlock(&pr_tmpl->registration_lock);
1174c66ac9dbSNicholas Bellinger 			return pr_reg;
1175c66ac9dbSNicholas Bellinger 		}
1176c66ac9dbSNicholas Bellinger 		/*
1177c66ac9dbSNicholas Bellinger 		 * If the *pr_reg contains a fabric defined ISID for multi-value
1178c66ac9dbSNicholas Bellinger 		 * SCSI Initiator Port TransportIDs, then we expect a valid
1179c66ac9dbSNicholas Bellinger 		 * matching ISID to be provided by the local SCSI Initiator Port.
1180c66ac9dbSNicholas Bellinger 		 */
11816708bb27SAndy Grover 		if (!isid)
1182c66ac9dbSNicholas Bellinger 			continue;
1183c66ac9dbSNicholas Bellinger 		if (strcmp(isid, pr_reg->pr_reg_isid))
1184c66ac9dbSNicholas Bellinger 			continue;
1185c66ac9dbSNicholas Bellinger 
118633940d09SJoern Engel 		atomic_inc_mb(&pr_reg->pr_res_holders);
1187c66ac9dbSNicholas Bellinger 		spin_unlock(&pr_tmpl->registration_lock);
1188c66ac9dbSNicholas Bellinger 		return pr_reg;
1189c66ac9dbSNicholas Bellinger 	}
1190c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
1191c66ac9dbSNicholas Bellinger 
1192c66ac9dbSNicholas Bellinger 	return NULL;
1193c66ac9dbSNicholas Bellinger }
1194c66ac9dbSNicholas Bellinger 
core_scsi3_locate_pr_reg(struct se_device * dev,struct se_node_acl * nacl,struct se_session * sess)1195c66ac9dbSNicholas Bellinger static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1196c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1197c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
1198c66ac9dbSNicholas Bellinger 	struct se_session *sess)
1199c66ac9dbSNicholas Bellinger {
1200c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg = nacl->se_tpg;
12014db6dfe6SChaitanya Kulkarni 	unsigned char buf[PR_REG_ISID_LEN] = { };
12024db6dfe6SChaitanya Kulkarni 	unsigned char *isid_ptr = NULL;
1203c66ac9dbSNicholas Bellinger 
1204e3d6f909SAndy Grover 	if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1205e3d6f909SAndy Grover 		tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
1206c66ac9dbSNicholas Bellinger 					PR_REG_ISID_LEN);
1207c66ac9dbSNicholas Bellinger 		isid_ptr = &buf[0];
1208c66ac9dbSNicholas Bellinger 	}
1209c66ac9dbSNicholas Bellinger 
1210c66ac9dbSNicholas Bellinger 	return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1211c66ac9dbSNicholas Bellinger }
1212c66ac9dbSNicholas Bellinger 
core_scsi3_put_pr_reg(struct t10_pr_registration * pr_reg)1213c66ac9dbSNicholas Bellinger static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1214c66ac9dbSNicholas Bellinger {
121533940d09SJoern Engel 	atomic_dec_mb(&pr_reg->pr_res_holders);
1216c66ac9dbSNicholas Bellinger }
1217c66ac9dbSNicholas Bellinger 
core_scsi3_check_implicit_release(struct se_device * dev,struct t10_pr_registration * pr_reg)1218125d0119SHannes Reinecke static int core_scsi3_check_implicit_release(
1219c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1220c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg)
1221c66ac9dbSNicholas Bellinger {
1222c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1223c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_res_holder;
1224c66ac9dbSNicholas Bellinger 	int ret = 0;
1225c66ac9dbSNicholas Bellinger 
1226c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1227c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
12286708bb27SAndy Grover 	if (!pr_res_holder) {
1229c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
1230c66ac9dbSNicholas Bellinger 		return ret;
1231c66ac9dbSNicholas Bellinger 	}
1232c66ac9dbSNicholas Bellinger 	if (pr_res_holder == pr_reg) {
1233c66ac9dbSNicholas Bellinger 		/*
1234125d0119SHannes Reinecke 		 * Perform an implicit RELEASE if the registration that
1235c66ac9dbSNicholas Bellinger 		 * is being released is holding the reservation.
1236c66ac9dbSNicholas Bellinger 		 *
1237c66ac9dbSNicholas Bellinger 		 * From spc4r17, section 5.7.11.1:
1238c66ac9dbSNicholas Bellinger 		 *
1239c66ac9dbSNicholas Bellinger 		 * e) If the I_T nexus is the persistent reservation holder
1240c66ac9dbSNicholas Bellinger 		 *    and the persistent reservation is not an all registrants
1241c66ac9dbSNicholas Bellinger 		 *    type, then a PERSISTENT RESERVE OUT command with REGISTER
1242c66ac9dbSNicholas Bellinger 		 *    service action or REGISTER AND  IGNORE EXISTING KEY
1243c66ac9dbSNicholas Bellinger 		 *    service action with the SERVICE ACTION RESERVATION KEY
1244c66ac9dbSNicholas Bellinger 		 *    field set to zero (see 5.7.11.3).
1245c66ac9dbSNicholas Bellinger 		 */
12466c3c9baaSNicholas Bellinger 		__core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0, 1);
1247c66ac9dbSNicholas Bellinger 		ret = 1;
1248c66ac9dbSNicholas Bellinger 		/*
1249c66ac9dbSNicholas Bellinger 		 * For 'All Registrants' reservation types, all existing
1250c66ac9dbSNicholas Bellinger 		 * registrations are still processed as reservation holders
1251c66ac9dbSNicholas Bellinger 		 * in core_scsi3_pr_seq_non_holder() after the initial
1252125d0119SHannes Reinecke 		 * reservation holder is implicitly released here.
1253c66ac9dbSNicholas Bellinger 		 */
1254c66ac9dbSNicholas Bellinger 	} else if (pr_reg->pr_reg_all_tg_pt &&
1255c66ac9dbSNicholas Bellinger 		  (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1256c66ac9dbSNicholas Bellinger 			  pr_reg->pr_reg_nacl->initiatorname)) &&
1257c66ac9dbSNicholas Bellinger 		  (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
12586708bb27SAndy Grover 		pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1259c66ac9dbSNicholas Bellinger 			" UNREGISTER while existing reservation with matching"
1260c66ac9dbSNicholas Bellinger 			" key 0x%016Lx is present from another SCSI Initiator"
1261c66ac9dbSNicholas Bellinger 			" Port\n", pr_reg->pr_res_key);
1262e3d6f909SAndy Grover 		ret = -EPERM;
1263c66ac9dbSNicholas Bellinger 	}
1264c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
1265c66ac9dbSNicholas Bellinger 
1266c66ac9dbSNicholas Bellinger 	return ret;
1267c66ac9dbSNicholas Bellinger }
1268c66ac9dbSNicholas Bellinger 
__core_scsi3_free_registration(struct se_device * dev,struct t10_pr_registration * pr_reg,struct list_head * preempt_and_abort_list,int dec_holders)1269c66ac9dbSNicholas Bellinger static void __core_scsi3_free_registration(
1270c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1271c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg,
1272c66ac9dbSNicholas Bellinger 	struct list_head *preempt_and_abort_list,
1273c66ac9dbSNicholas Bellinger 	int dec_holders)
1274cb0df4d3SBart Van Assche 	__releases(&pr_tmpl->registration_lock)
1275cb0df4d3SBart Van Assche 	__acquires(&pr_tmpl->registration_lock)
1276c66ac9dbSNicholas Bellinger {
12779ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo =
1278c66ac9dbSNicholas Bellinger 			pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
12790fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
128080bfdfa9SNicholas Bellinger 	struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
128180bfdfa9SNicholas Bellinger 	struct se_dev_entry *deve;
12824db6dfe6SChaitanya Kulkarni 	char i_buf[PR_REG_ISID_ID_LEN] = { };
1283c66ac9dbSNicholas Bellinger 
1284618baaf7SBart Van Assche 	lockdep_assert_held(&pr_tmpl->registration_lock);
1285618baaf7SBart Van Assche 
1286d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1287c66ac9dbSNicholas Bellinger 
12886c3c9baaSNicholas Bellinger 	if (!list_empty(&pr_reg->pr_reg_list))
1289c66ac9dbSNicholas Bellinger 		list_del(&pr_reg->pr_reg_list);
1290c66ac9dbSNicholas Bellinger 	/*
1291c66ac9dbSNicholas Bellinger 	 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1292c66ac9dbSNicholas Bellinger 	 * so call core_scsi3_put_pr_reg() to decrement our reference.
1293c66ac9dbSNicholas Bellinger 	 */
1294c66ac9dbSNicholas Bellinger 	if (dec_holders)
1295c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg);
129680bfdfa9SNicholas Bellinger 
129780bfdfa9SNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
1298c66ac9dbSNicholas Bellinger 	/*
1299c66ac9dbSNicholas Bellinger 	 * Wait until all reference from any other I_T nexuses for this
1300c66ac9dbSNicholas Bellinger 	 * *pr_reg have been released.  Because list_del() is called above,
1301c66ac9dbSNicholas Bellinger 	 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1302c66ac9dbSNicholas Bellinger 	 * count back to zero, and we release *pr_reg.
1303c66ac9dbSNicholas Bellinger 	 */
1304c66ac9dbSNicholas Bellinger 	while (atomic_read(&pr_reg->pr_res_holders) != 0) {
13056708bb27SAndy Grover 		pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
130630c7ca93SDavid Disseldorp 				tfo->fabric_name);
1307c66ac9dbSNicholas Bellinger 		cpu_relax();
1308c66ac9dbSNicholas Bellinger 	}
1309c66ac9dbSNicholas Bellinger 
131080bfdfa9SNicholas Bellinger 	rcu_read_lock();
131180bfdfa9SNicholas Bellinger 	deve = target_nacl_find_deve(nacl, pr_reg->pr_res_mapped_lun);
131280bfdfa9SNicholas Bellinger 	if (deve)
131380bfdfa9SNicholas Bellinger 		clear_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
131480bfdfa9SNicholas Bellinger 	rcu_read_unlock();
131580bfdfa9SNicholas Bellinger 
131680bfdfa9SNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
13176708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
131830c7ca93SDavid Disseldorp 		" Node: %s%s\n", tfo->fabric_name,
1319c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_nacl->initiatorname,
1320d2843c17SAndy Grover 		i_buf);
13216708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
132230c7ca93SDavid Disseldorp 		" Port(s)\n", tfo->fabric_name,
1323c66ac9dbSNicholas Bellinger 		(pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1324e3d6f909SAndy Grover 		dev->transport->name);
13256708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
132630c7ca93SDavid Disseldorp 		" 0x%08x\n", tfo->fabric_name, pr_reg->pr_res_key,
1327c66ac9dbSNicholas Bellinger 		pr_reg->pr_res_generation);
1328c66ac9dbSNicholas Bellinger 
13296708bb27SAndy Grover 	if (!preempt_and_abort_list) {
1330c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_deve = NULL;
1331c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_nacl = NULL;
1332c66ac9dbSNicholas Bellinger 		kmem_cache_free(t10_pr_reg_cache, pr_reg);
1333c66ac9dbSNicholas Bellinger 		return;
1334c66ac9dbSNicholas Bellinger 	}
1335c66ac9dbSNicholas Bellinger 	/*
1336c66ac9dbSNicholas Bellinger 	 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1337c66ac9dbSNicholas Bellinger 	 * are released once the ABORT_TASK_SET has completed..
1338c66ac9dbSNicholas Bellinger 	 */
1339c66ac9dbSNicholas Bellinger 	list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1340c66ac9dbSNicholas Bellinger }
1341c66ac9dbSNicholas Bellinger 
core_scsi3_free_pr_reg_from_nacl(struct se_device * dev,struct se_node_acl * nacl)1342c66ac9dbSNicholas Bellinger void core_scsi3_free_pr_reg_from_nacl(
1343c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1344c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl)
1345c66ac9dbSNicholas Bellinger {
13460fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
1347c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
13486c3c9baaSNicholas Bellinger 	bool free_reg = false;
1349c66ac9dbSNicholas Bellinger 	/*
1350c66ac9dbSNicholas Bellinger 	 * If the passed se_node_acl matches the reservation holder,
1351c66ac9dbSNicholas Bellinger 	 * release the reservation.
1352c66ac9dbSNicholas Bellinger 	 */
1353c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1354c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
1355c66ac9dbSNicholas Bellinger 	if ((pr_res_holder != NULL) &&
13566c3c9baaSNicholas Bellinger 	    (pr_res_holder->pr_reg_nacl == nacl)) {
13576c3c9baaSNicholas Bellinger 		__core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0, 1);
13586c3c9baaSNicholas Bellinger 		free_reg = true;
13596c3c9baaSNicholas Bellinger 	}
1360c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
1361c66ac9dbSNicholas Bellinger 	/*
1362c66ac9dbSNicholas Bellinger 	 * Release any registration associated with the struct se_node_acl.
1363c66ac9dbSNicholas Bellinger 	 */
1364c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
13656c3c9baaSNicholas Bellinger 	if (pr_res_holder && free_reg)
13666c3c9baaSNicholas Bellinger 		__core_scsi3_free_registration(dev, pr_res_holder, NULL, 0);
13676c3c9baaSNicholas Bellinger 
1368c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1369c66ac9dbSNicholas Bellinger 			&pr_tmpl->registration_list, pr_reg_list) {
1370c66ac9dbSNicholas Bellinger 
1371c66ac9dbSNicholas Bellinger 		if (pr_reg->pr_reg_nacl != nacl)
1372c66ac9dbSNicholas Bellinger 			continue;
1373c66ac9dbSNicholas Bellinger 
1374c66ac9dbSNicholas Bellinger 		__core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1375c66ac9dbSNicholas Bellinger 	}
1376c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
1377c66ac9dbSNicholas Bellinger }
1378c66ac9dbSNicholas Bellinger 
core_scsi3_free_all_registrations(struct se_device * dev)1379c66ac9dbSNicholas Bellinger void core_scsi3_free_all_registrations(
1380c66ac9dbSNicholas Bellinger 	struct se_device *dev)
1381c66ac9dbSNicholas Bellinger {
13820fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
1383c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1384c66ac9dbSNicholas Bellinger 
1385c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1386c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
1387c66ac9dbSNicholas Bellinger 	if (pr_res_holder != NULL) {
1388c66ac9dbSNicholas Bellinger 		struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1389c66ac9dbSNicholas Bellinger 		__core_scsi3_complete_pro_release(dev, pr_res_nacl,
13906c3c9baaSNicholas Bellinger 						  pr_res_holder, 0, 0);
1391c66ac9dbSNicholas Bellinger 	}
1392c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
1393c66ac9dbSNicholas Bellinger 
1394c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
1395c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1396c66ac9dbSNicholas Bellinger 			&pr_tmpl->registration_list, pr_reg_list) {
1397c66ac9dbSNicholas Bellinger 
1398c66ac9dbSNicholas Bellinger 		__core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1399c66ac9dbSNicholas Bellinger 	}
1400c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
1401c66ac9dbSNicholas Bellinger 
1402c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->aptpl_reg_lock);
1403c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1404c66ac9dbSNicholas Bellinger 				pr_reg_aptpl_list) {
1405c66ac9dbSNicholas Bellinger 		list_del(&pr_reg->pr_reg_aptpl_list);
1406c66ac9dbSNicholas Bellinger 		kmem_cache_free(t10_pr_reg_cache, pr_reg);
1407c66ac9dbSNicholas Bellinger 	}
1408c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->aptpl_reg_lock);
1409c66ac9dbSNicholas Bellinger }
1410c66ac9dbSNicholas Bellinger 
core_scsi3_tpg_depend_item(struct se_portal_group * tpg)1411c66ac9dbSNicholas Bellinger static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1412c66ac9dbSNicholas Bellinger {
1413d588cf8fSChristoph Hellwig 	return target_depend_item(&tpg->tpg_group.cg_item);
1414c66ac9dbSNicholas Bellinger }
1415c66ac9dbSNicholas Bellinger 
core_scsi3_tpg_undepend_item(struct se_portal_group * tpg)1416c66ac9dbSNicholas Bellinger static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1417c66ac9dbSNicholas Bellinger {
1418d588cf8fSChristoph Hellwig 	target_undepend_item(&tpg->tpg_group.cg_item);
141933940d09SJoern Engel 	atomic_dec_mb(&tpg->tpg_pr_ref_count);
1420c66ac9dbSNicholas Bellinger }
1421c66ac9dbSNicholas Bellinger 
core_scsi3_nodeacl_depend_item(struct se_node_acl * nacl)1422c66ac9dbSNicholas Bellinger static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1423c66ac9dbSNicholas Bellinger {
1424c66ac9dbSNicholas Bellinger 	if (nacl->dynamic_node_acl)
1425c66ac9dbSNicholas Bellinger 		return 0;
1426d588cf8fSChristoph Hellwig 	return target_depend_item(&nacl->acl_group.cg_item);
1427c66ac9dbSNicholas Bellinger }
1428c66ac9dbSNicholas Bellinger 
core_scsi3_nodeacl_undepend_item(struct se_node_acl * nacl)1429c66ac9dbSNicholas Bellinger static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1430c66ac9dbSNicholas Bellinger {
1431d588cf8fSChristoph Hellwig 	if (!nacl->dynamic_node_acl)
1432d588cf8fSChristoph Hellwig 		target_undepend_item(&nacl->acl_group.cg_item);
143333940d09SJoern Engel 	atomic_dec_mb(&nacl->acl_pr_ref_count);
1434c66ac9dbSNicholas Bellinger }
1435c66ac9dbSNicholas Bellinger 
core_scsi3_lunacl_depend_item(struct se_dev_entry * se_deve)1436c66ac9dbSNicholas Bellinger static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1437c66ac9dbSNicholas Bellinger {
1438c66ac9dbSNicholas Bellinger 	/*
1439c66ac9dbSNicholas Bellinger 	 * For nacl->dynamic_node_acl=1
1440c66ac9dbSNicholas Bellinger 	 */
1441ef4f7e4bSDmitry Bogdanov 	if (!se_deve->se_lun_acl)
1442c66ac9dbSNicholas Bellinger 		return 0;
1443c66ac9dbSNicholas Bellinger 
1444ef4f7e4bSDmitry Bogdanov 	return target_depend_item(&se_deve->se_lun_acl->se_lun_group.cg_item);
1445c66ac9dbSNicholas Bellinger }
1446c66ac9dbSNicholas Bellinger 
core_scsi3_lunacl_undepend_item(struct se_dev_entry * se_deve)1447c66ac9dbSNicholas Bellinger static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1448c66ac9dbSNicholas Bellinger {
1449c66ac9dbSNicholas Bellinger 	/*
1450c66ac9dbSNicholas Bellinger 	 * For nacl->dynamic_node_acl=1
1451c66ac9dbSNicholas Bellinger 	 */
1452ef4f7e4bSDmitry Bogdanov 	if (!se_deve->se_lun_acl) {
145329a05deeSNicholas Bellinger 		kref_put(&se_deve->pr_kref, target_pr_kref_release);
1454c66ac9dbSNicholas Bellinger 		return;
1455c66ac9dbSNicholas Bellinger 	}
1456c66ac9dbSNicholas Bellinger 
1457ef4f7e4bSDmitry Bogdanov 	target_undepend_item(&se_deve->se_lun_acl->se_lun_group.cg_item);
145829a05deeSNicholas Bellinger 	kref_put(&se_deve->pr_kref, target_pr_kref_release);
1459c66ac9dbSNicholas Bellinger }
1460c66ac9dbSNicholas Bellinger 
1461de103c93SChristoph Hellwig static sense_reason_t
core_scsi3_decode_spec_i_port(struct se_cmd * cmd,struct se_portal_group * tpg,unsigned char * l_isid,u64 sa_res_key,int all_tg_pt,int aptpl)1462de103c93SChristoph Hellwig core_scsi3_decode_spec_i_port(
1463c66ac9dbSNicholas Bellinger 	struct se_cmd *cmd,
1464c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
1465c66ac9dbSNicholas Bellinger 	unsigned char *l_isid,
1466c66ac9dbSNicholas Bellinger 	u64 sa_res_key,
1467c66ac9dbSNicholas Bellinger 	int all_tg_pt,
1468c66ac9dbSNicholas Bellinger 	int aptpl)
1469c66ac9dbSNicholas Bellinger {
14705951146dSAndy Grover 	struct se_device *dev = cmd->se_dev;
1471c66ac9dbSNicholas Bellinger 	struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
1472e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
1473c66ac9dbSNicholas Bellinger 	struct se_node_acl *dest_node_acl = NULL;
147479dc9c9eSNicholas Bellinger 	struct se_dev_entry *dest_se_deve = NULL;
1475c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1476c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1477d0f474e5SRoland Dreier 	LIST_HEAD(tid_dest_list);
1478c66ac9dbSNicholas Bellinger 	struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
14792650d71eSChristoph Hellwig 	unsigned char *buf, *ptr, proto_ident;
14809b353cc8SSagi Grimberg 	const unsigned char *i_str = NULL;
148113ba564cSJulia Lawall 	char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
1482de103c93SChristoph Hellwig 	sense_reason_t ret;
1483c66ac9dbSNicholas Bellinger 	u32 tpdl, tid_len = 0;
1484c66ac9dbSNicholas Bellinger 	u32 dest_rtpi = 0;
1485c66ac9dbSNicholas Bellinger 
1486c66ac9dbSNicholas Bellinger 	/*
1487c66ac9dbSNicholas Bellinger 	 * Allocate a struct pr_transport_id_holder and setup the
148879dc9c9eSNicholas Bellinger 	 * local_node_acl pointer and add to struct list_head tid_dest_list
148979dc9c9eSNicholas Bellinger 	 * for add registration processing in the loop of tid_dest_list below.
1490c66ac9dbSNicholas Bellinger 	 */
1491c66ac9dbSNicholas Bellinger 	tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
14926708bb27SAndy Grover 	if (!tidh_new) {
14936708bb27SAndy Grover 		pr_err("Unable to allocate tidh_new\n");
149479dd6f2fStangwenji 		return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1495c66ac9dbSNicholas Bellinger 	}
1496c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&tidh_new->dest_list);
1497c66ac9dbSNicholas Bellinger 	tidh_new->dest_tpg = tpg;
1498c66ac9dbSNicholas Bellinger 	tidh_new->dest_node_acl = se_sess->se_node_acl;
1499c66ac9dbSNicholas Bellinger 
15005951146dSAndy Grover 	local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
150179dc9c9eSNicholas Bellinger 				se_sess->se_node_acl, cmd->se_lun,
150279dc9c9eSNicholas Bellinger 				NULL, cmd->orig_fe_lun, l_isid,
1503c66ac9dbSNicholas Bellinger 				sa_res_key, all_tg_pt, aptpl);
15046708bb27SAndy Grover 	if (!local_pr_reg) {
1505c66ac9dbSNicholas Bellinger 		kfree(tidh_new);
150679dd6f2fStangwenji 		return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1507c66ac9dbSNicholas Bellinger 	}
1508f32ba612SMike Christie 
1509f32ba612SMike Christie 	if (core_scsi3_lunacl_depend_item(local_pr_reg->pr_reg_deve)) {
1510f32ba612SMike Christie 		kfree(tidh_new);
1511f32ba612SMike Christie 		kref_put(&local_pr_reg->pr_reg_deve->pr_kref,
1512f32ba612SMike Christie 			 target_pr_kref_release);
1513f32ba612SMike Christie 		kmem_cache_free(t10_pr_reg_cache, local_pr_reg);
1514f32ba612SMike Christie 		return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1515f32ba612SMike Christie 	}
1516f32ba612SMike Christie 
1517c66ac9dbSNicholas Bellinger 	tidh_new->dest_pr_reg = local_pr_reg;
1518c66ac9dbSNicholas Bellinger 	list_add_tail(&tidh_new->dest_list, &tid_dest_list);
151905d1c7c0SAndy Grover 
15200d7f1299SPaolo Bonzini 	if (cmd->data_length < 28) {
15210d7f1299SPaolo Bonzini 		pr_warn("SPC-PR: Received PR OUT parameter list"
15220d7f1299SPaolo Bonzini 			" length too small: %u\n", cmd->data_length);
1523de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
15240d7f1299SPaolo Bonzini 		goto out;
15250d7f1299SPaolo Bonzini 	}
15260d7f1299SPaolo Bonzini 
15274949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
1528de103c93SChristoph Hellwig 	if (!buf) {
152979dd6f2fStangwenji 		ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1530de103c93SChristoph Hellwig 		goto out;
1531de103c93SChristoph Hellwig 	}
1532de103c93SChristoph Hellwig 
1533c66ac9dbSNicholas Bellinger 	/*
1534c66ac9dbSNicholas Bellinger 	 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1535c66ac9dbSNicholas Bellinger 	 * first extract TransportID Parameter Data Length, and make sure
1536c66ac9dbSNicholas Bellinger 	 * the value matches up to the SCSI expected data transfer length.
1537c66ac9dbSNicholas Bellinger 	 */
1538a85d667eSBart Van Assche 	tpdl = get_unaligned_be32(&buf[24]);
1539c66ac9dbSNicholas Bellinger 
1540c66ac9dbSNicholas Bellinger 	if ((tpdl + 28) != cmd->data_length) {
15416708bb27SAndy Grover 		pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1542c66ac9dbSNicholas Bellinger 			" does not equal CDB data_length: %u\n", tpdl,
1543c66ac9dbSNicholas Bellinger 			cmd->data_length);
1544de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
1545de103c93SChristoph Hellwig 		goto out_unmap;
1546c66ac9dbSNicholas Bellinger 	}
1547c66ac9dbSNicholas Bellinger 	/*
1548c66ac9dbSNicholas Bellinger 	 * Start processing the received transport IDs using the
1549c66ac9dbSNicholas Bellinger 	 * receiving I_T Nexus portal's fabric dependent methods to
1550c66ac9dbSNicholas Bellinger 	 * obtain the SCSI Initiator Port/Device Identifiers.
1551c66ac9dbSNicholas Bellinger 	 */
1552c66ac9dbSNicholas Bellinger 	ptr = &buf[28];
1553c66ac9dbSNicholas Bellinger 
1554c66ac9dbSNicholas Bellinger 	while (tpdl > 0) {
1555adf653f9SChristoph Hellwig 		struct se_lun *dest_lun, *tmp_lun;
155679dc9c9eSNicholas Bellinger 
1557c66ac9dbSNicholas Bellinger 		proto_ident = (ptr[0] & 0x0f);
1558c66ac9dbSNicholas Bellinger 		dest_tpg = NULL;
1559c66ac9dbSNicholas Bellinger 
1560c66ac9dbSNicholas Bellinger 		spin_lock(&dev->se_port_lock);
1561adf653f9SChristoph Hellwig 		list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
1562adf653f9SChristoph Hellwig 			tmp_tpg = tmp_lun->lun_tpg;
15632650d71eSChristoph Hellwig 
1564c66ac9dbSNicholas Bellinger 			/*
1565c66ac9dbSNicholas Bellinger 			 * Look for the matching proto_ident provided by
1566c66ac9dbSNicholas Bellinger 			 * the received TransportID
1567c66ac9dbSNicholas Bellinger 			 */
15682aeeafaeSChristoph Hellwig 			if (tmp_tpg->proto_id != proto_ident)
1569c66ac9dbSNicholas Bellinger 				continue;
1570adf653f9SChristoph Hellwig 			dest_rtpi = tmp_lun->lun_tpg->tpg_rtpi;
1571c66ac9dbSNicholas Bellinger 
157263c9ffe4SMike Christie 			iport_ptr = NULL;
15732650d71eSChristoph Hellwig 			i_str = target_parse_pr_out_transport_id(tmp_tpg,
1574094bb5d7SRasmus Villemoes 					ptr, &tid_len, &iport_ptr);
15756708bb27SAndy Grover 			if (!i_str)
1576c66ac9dbSNicholas Bellinger 				continue;
157763c9ffe4SMike Christie 			/*
157863c9ffe4SMike Christie 			 * Determine if this SCSI device server requires that
157963c9ffe4SMike Christie 			 * SCSI Intiatior TransportID w/ ISIDs is enforced
158063c9ffe4SMike Christie 			 * for fabric modules (iSCSI) requiring them.
158163c9ffe4SMike Christie 			 */
158263c9ffe4SMike Christie 			if (tpg->se_tpg_tfo->sess_get_initiator_sid &&
158363c9ffe4SMike Christie 			    dev->dev_attrib.enforce_pr_isids &&
158463c9ffe4SMike Christie 			    !iport_ptr) {
158563c9ffe4SMike Christie 				pr_warn("SPC-PR: enforce_pr_isids is set but a isid has not been sent in the SPEC_I_PT data for %s.",
158663c9ffe4SMike Christie 					i_str);
158763c9ffe4SMike Christie 				ret = TCM_INVALID_PARAMETER_LIST;
158863c9ffe4SMike Christie 				spin_unlock(&dev->se_port_lock);
158963c9ffe4SMike Christie 				goto out_unmap;
159063c9ffe4SMike Christie 			}
1591c66ac9dbSNicholas Bellinger 
159233940d09SJoern Engel 			atomic_inc_mb(&tmp_tpg->tpg_pr_ref_count);
1593c66ac9dbSNicholas Bellinger 			spin_unlock(&dev->se_port_lock);
1594c66ac9dbSNicholas Bellinger 
1595de103c93SChristoph Hellwig 			if (core_scsi3_tpg_depend_item(tmp_tpg)) {
15966708bb27SAndy Grover 				pr_err(" core_scsi3_tpg_depend_item()"
1597c66ac9dbSNicholas Bellinger 					" for tmp_tpg\n");
159833940d09SJoern Engel 				atomic_dec_mb(&tmp_tpg->tpg_pr_ref_count);
1599de103c93SChristoph Hellwig 				ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1600de103c93SChristoph Hellwig 				goto out_unmap;
1601c66ac9dbSNicholas Bellinger 			}
1602c66ac9dbSNicholas Bellinger 			/*
160335d1efe8SMasanari Iida 			 * Locate the destination initiator ACL to be registered
1604c66ac9dbSNicholas Bellinger 			 * from the decoded fabric module specific TransportID
1605c66ac9dbSNicholas Bellinger 			 * at *i_str.
1606c66ac9dbSNicholas Bellinger 			 */
1607403edd78SNicholas Bellinger 			mutex_lock(&tmp_tpg->acl_node_mutex);
1608c66ac9dbSNicholas Bellinger 			dest_node_acl = __core_tpg_get_initiator_node_acl(
1609c66ac9dbSNicholas Bellinger 						tmp_tpg, i_str);
161033940d09SJoern Engel 			if (dest_node_acl)
161133940d09SJoern Engel 				atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
1612403edd78SNicholas Bellinger 			mutex_unlock(&tmp_tpg->acl_node_mutex);
1613c66ac9dbSNicholas Bellinger 
16146708bb27SAndy Grover 			if (!dest_node_acl) {
1615c66ac9dbSNicholas Bellinger 				core_scsi3_tpg_undepend_item(tmp_tpg);
1616c66ac9dbSNicholas Bellinger 				spin_lock(&dev->se_port_lock);
1617c66ac9dbSNicholas Bellinger 				continue;
1618c66ac9dbSNicholas Bellinger 			}
1619c66ac9dbSNicholas Bellinger 
1620de103c93SChristoph Hellwig 			if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
16216708bb27SAndy Grover 				pr_err("configfs_depend_item() failed"
1622c66ac9dbSNicholas Bellinger 					" for dest_node_acl->acl_group\n");
162333940d09SJoern Engel 				atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
1624c66ac9dbSNicholas Bellinger 				core_scsi3_tpg_undepend_item(tmp_tpg);
1625de103c93SChristoph Hellwig 				ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1626de103c93SChristoph Hellwig 				goto out_unmap;
1627c66ac9dbSNicholas Bellinger 			}
1628c66ac9dbSNicholas Bellinger 
1629c66ac9dbSNicholas Bellinger 			dest_tpg = tmp_tpg;
1630e15c7452SBart Van Assche 			pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s Port RTPI: %u\n",
163130c7ca93SDavid Disseldorp 				dest_tpg->se_tpg_tfo->fabric_name,
1632c66ac9dbSNicholas Bellinger 				dest_node_acl->initiatorname, dest_rtpi);
1633c66ac9dbSNicholas Bellinger 
1634c66ac9dbSNicholas Bellinger 			spin_lock(&dev->se_port_lock);
1635c66ac9dbSNicholas Bellinger 			break;
1636c66ac9dbSNicholas Bellinger 		}
1637c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->se_port_lock);
1638c66ac9dbSNicholas Bellinger 
16396708bb27SAndy Grover 		if (!dest_tpg) {
16406708bb27SAndy Grover 			pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1641c66ac9dbSNicholas Bellinger 					" dest_tpg\n");
1642de103c93SChristoph Hellwig 			ret = TCM_INVALID_PARAMETER_LIST;
1643de103c93SChristoph Hellwig 			goto out_unmap;
1644c66ac9dbSNicholas Bellinger 		}
16458b1e1244SAndy Grover 
16466708bb27SAndy Grover 		pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1647c66ac9dbSNicholas Bellinger 			" tid_len: %d for %s + %s\n",
164830c7ca93SDavid Disseldorp 			dest_tpg->se_tpg_tfo->fabric_name, cmd->data_length,
1649c66ac9dbSNicholas Bellinger 			tpdl, tid_len, i_str, iport_ptr);
16508b1e1244SAndy Grover 
1651c66ac9dbSNicholas Bellinger 		if (tid_len > tpdl) {
16526708bb27SAndy Grover 			pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1653c66ac9dbSNicholas Bellinger 				" %u for Transport ID: %s\n", tid_len, ptr);
1654c66ac9dbSNicholas Bellinger 			core_scsi3_nodeacl_undepend_item(dest_node_acl);
1655c66ac9dbSNicholas Bellinger 			core_scsi3_tpg_undepend_item(dest_tpg);
1656de103c93SChristoph Hellwig 			ret = TCM_INVALID_PARAMETER_LIST;
1657de103c93SChristoph Hellwig 			goto out_unmap;
1658c66ac9dbSNicholas Bellinger 		}
1659c66ac9dbSNicholas Bellinger 		/*
1660c66ac9dbSNicholas Bellinger 		 * Locate the desintation struct se_dev_entry pointer for matching
1661c66ac9dbSNicholas Bellinger 		 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1662c66ac9dbSNicholas Bellinger 		 * Target Port.
1663c66ac9dbSNicholas Bellinger 		 */
1664c66ac9dbSNicholas Bellinger 		dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1665c66ac9dbSNicholas Bellinger 					dest_rtpi);
16666708bb27SAndy Grover 		if (!dest_se_deve) {
1667e15c7452SBart Van Assche 			pr_err("Unable to locate %s dest_se_deve from destination RTPI: %u\n",
166830c7ca93SDavid Disseldorp 				dest_tpg->se_tpg_tfo->fabric_name,
1669c66ac9dbSNicholas Bellinger 				dest_rtpi);
1670c66ac9dbSNicholas Bellinger 
1671c66ac9dbSNicholas Bellinger 			core_scsi3_nodeacl_undepend_item(dest_node_acl);
1672c66ac9dbSNicholas Bellinger 			core_scsi3_tpg_undepend_item(dest_tpg);
1673de103c93SChristoph Hellwig 			ret = TCM_INVALID_PARAMETER_LIST;
1674de103c93SChristoph Hellwig 			goto out_unmap;
1675c66ac9dbSNicholas Bellinger 		}
1676c66ac9dbSNicholas Bellinger 
1677de103c93SChristoph Hellwig 		if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
16786708bb27SAndy Grover 			pr_err("core_scsi3_lunacl_depend_item()"
1679c66ac9dbSNicholas Bellinger 					" failed\n");
168029a05deeSNicholas Bellinger 			kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
1681c66ac9dbSNicholas Bellinger 			core_scsi3_nodeacl_undepend_item(dest_node_acl);
1682c66ac9dbSNicholas Bellinger 			core_scsi3_tpg_undepend_item(dest_tpg);
1683de103c93SChristoph Hellwig 			ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1684de103c93SChristoph Hellwig 			goto out_unmap;
1685c66ac9dbSNicholas Bellinger 		}
16868b1e1244SAndy Grover 
16876708bb27SAndy Grover 		pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1688f2d30680SHannes Reinecke 			" dest_se_deve mapped_lun: %llu\n",
168930c7ca93SDavid Disseldorp 			dest_tpg->se_tpg_tfo->fabric_name,
1690c66ac9dbSNicholas Bellinger 			dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
16918b1e1244SAndy Grover 
1692c66ac9dbSNicholas Bellinger 		/*
1693c66ac9dbSNicholas Bellinger 		 * Skip any TransportIDs that already have a registration for
1694c66ac9dbSNicholas Bellinger 		 * this target port.
1695c66ac9dbSNicholas Bellinger 		 */
1696c66ac9dbSNicholas Bellinger 		pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1697c66ac9dbSNicholas Bellinger 					iport_ptr);
1698c66ac9dbSNicholas Bellinger 		if (pr_reg_e) {
1699c66ac9dbSNicholas Bellinger 			core_scsi3_put_pr_reg(pr_reg_e);
1700c66ac9dbSNicholas Bellinger 			core_scsi3_lunacl_undepend_item(dest_se_deve);
1701c66ac9dbSNicholas Bellinger 			core_scsi3_nodeacl_undepend_item(dest_node_acl);
1702c66ac9dbSNicholas Bellinger 			core_scsi3_tpg_undepend_item(dest_tpg);
1703c66ac9dbSNicholas Bellinger 			ptr += tid_len;
1704c66ac9dbSNicholas Bellinger 			tpdl -= tid_len;
1705c66ac9dbSNicholas Bellinger 			tid_len = 0;
1706c66ac9dbSNicholas Bellinger 			continue;
1707c66ac9dbSNicholas Bellinger 		}
1708c66ac9dbSNicholas Bellinger 		/*
1709c66ac9dbSNicholas Bellinger 		 * Allocate a struct pr_transport_id_holder and setup
1710c66ac9dbSNicholas Bellinger 		 * the dest_node_acl and dest_se_deve pointers for the
1711c66ac9dbSNicholas Bellinger 		 * loop below.
1712c66ac9dbSNicholas Bellinger 		 */
1713c66ac9dbSNicholas Bellinger 		tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1714c66ac9dbSNicholas Bellinger 				GFP_KERNEL);
17156708bb27SAndy Grover 		if (!tidh_new) {
17166708bb27SAndy Grover 			pr_err("Unable to allocate tidh_new\n");
1717c66ac9dbSNicholas Bellinger 			core_scsi3_lunacl_undepend_item(dest_se_deve);
1718c66ac9dbSNicholas Bellinger 			core_scsi3_nodeacl_undepend_item(dest_node_acl);
1719c66ac9dbSNicholas Bellinger 			core_scsi3_tpg_undepend_item(dest_tpg);
1720de103c93SChristoph Hellwig 			ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1721de103c93SChristoph Hellwig 			goto out_unmap;
1722c66ac9dbSNicholas Bellinger 		}
1723c66ac9dbSNicholas Bellinger 		INIT_LIST_HEAD(&tidh_new->dest_list);
1724c66ac9dbSNicholas Bellinger 		tidh_new->dest_tpg = dest_tpg;
1725c66ac9dbSNicholas Bellinger 		tidh_new->dest_node_acl = dest_node_acl;
1726c66ac9dbSNicholas Bellinger 		tidh_new->dest_se_deve = dest_se_deve;
1727c66ac9dbSNicholas Bellinger 
1728c66ac9dbSNicholas Bellinger 		/*
1729c66ac9dbSNicholas Bellinger 		 * Allocate, but do NOT add the registration for the
1730c66ac9dbSNicholas Bellinger 		 * TransportID referenced SCSI Initiator port.  This
1731c66ac9dbSNicholas Bellinger 		 * done because of the following from spc4r17 in section
1732c66ac9dbSNicholas Bellinger 		 * 6.14.3 wrt SPEC_I_PT:
1733c66ac9dbSNicholas Bellinger 		 *
1734c66ac9dbSNicholas Bellinger 		 * "If a registration fails for any initiator port (e.g., if th
1735c66ac9dbSNicholas Bellinger 		 * logical unit does not have enough resources available to
1736c66ac9dbSNicholas Bellinger 		 * hold the registration information), no registrations shall be
1737c66ac9dbSNicholas Bellinger 		 * made, and the command shall be terminated with
1738c66ac9dbSNicholas Bellinger 		 * CHECK CONDITION status."
1739c66ac9dbSNicholas Bellinger 		 *
1740c66ac9dbSNicholas Bellinger 		 * That means we call __core_scsi3_alloc_registration() here,
1741c66ac9dbSNicholas Bellinger 		 * and then call __core_scsi3_add_registration() in the
1742c66ac9dbSNicholas Bellinger 		 * 2nd loop which will never fail.
1743c66ac9dbSNicholas Bellinger 		 */
1744ef4f7e4bSDmitry Bogdanov 		dest_lun = dest_se_deve->se_lun;
174579dc9c9eSNicholas Bellinger 
17465951146dSAndy Grover 		dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
174779dc9c9eSNicholas Bellinger 					dest_node_acl, dest_lun, dest_se_deve,
174879dc9c9eSNicholas Bellinger 					dest_se_deve->mapped_lun, iport_ptr,
1749c66ac9dbSNicholas Bellinger 					sa_res_key, all_tg_pt, aptpl);
17506708bb27SAndy Grover 		if (!dest_pr_reg) {
1751c66ac9dbSNicholas Bellinger 			core_scsi3_lunacl_undepend_item(dest_se_deve);
1752c66ac9dbSNicholas Bellinger 			core_scsi3_nodeacl_undepend_item(dest_node_acl);
1753c66ac9dbSNicholas Bellinger 			core_scsi3_tpg_undepend_item(dest_tpg);
1754c66ac9dbSNicholas Bellinger 			kfree(tidh_new);
175579dd6f2fStangwenji 			ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1756de103c93SChristoph Hellwig 			goto out_unmap;
1757c66ac9dbSNicholas Bellinger 		}
1758c66ac9dbSNicholas Bellinger 		tidh_new->dest_pr_reg = dest_pr_reg;
1759c66ac9dbSNicholas Bellinger 		list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1760c66ac9dbSNicholas Bellinger 
1761c66ac9dbSNicholas Bellinger 		ptr += tid_len;
1762c66ac9dbSNicholas Bellinger 		tpdl -= tid_len;
1763c66ac9dbSNicholas Bellinger 		tid_len = 0;
1764c66ac9dbSNicholas Bellinger 
1765c66ac9dbSNicholas Bellinger 	}
176605d1c7c0SAndy Grover 
17674949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
176805d1c7c0SAndy Grover 
1769c66ac9dbSNicholas Bellinger 	/*
1770c66ac9dbSNicholas Bellinger 	 * Go ahead and create a registrations from tid_dest_list for the
1771c66ac9dbSNicholas Bellinger 	 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1772c66ac9dbSNicholas Bellinger 	 * and dest_se_deve.
1773c66ac9dbSNicholas Bellinger 	 *
1774c66ac9dbSNicholas Bellinger 	 * The SA Reservation Key from the PROUT is set for the
1775c66ac9dbSNicholas Bellinger 	 * registration, and ALL_TG_PT is also passed.  ALL_TG_PT=1
1776c66ac9dbSNicholas Bellinger 	 * means that the TransportID Initiator port will be
1777c66ac9dbSNicholas Bellinger 	 * registered on all of the target ports in the SCSI target device
1778c66ac9dbSNicholas Bellinger 	 * ALL_TG_PT=0 means the registration will only be for the
1779c66ac9dbSNicholas Bellinger 	 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1780c66ac9dbSNicholas Bellinger 	 * was received.
1781c66ac9dbSNicholas Bellinger 	 */
1782c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1783c66ac9dbSNicholas Bellinger 		dest_tpg = tidh->dest_tpg;
1784c66ac9dbSNicholas Bellinger 		dest_node_acl = tidh->dest_node_acl;
1785c66ac9dbSNicholas Bellinger 		dest_se_deve = tidh->dest_se_deve;
1786c66ac9dbSNicholas Bellinger 		dest_pr_reg = tidh->dest_pr_reg;
1787c66ac9dbSNicholas Bellinger 
1788c66ac9dbSNicholas Bellinger 		list_del(&tidh->dest_list);
1789c66ac9dbSNicholas Bellinger 		kfree(tidh);
1790c66ac9dbSNicholas Bellinger 
1791c66ac9dbSNicholas Bellinger 		memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1792d2843c17SAndy Grover 		core_pr_dump_initiator_port(dest_pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1793c66ac9dbSNicholas Bellinger 
17945951146dSAndy Grover 		__core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
1795c66ac9dbSNicholas Bellinger 					dest_pr_reg, 0, 0);
1796c66ac9dbSNicholas Bellinger 
17976708bb27SAndy Grover 		pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1798c66ac9dbSNicholas Bellinger 			" registered Transport ID for Node: %s%s Mapped LUN:"
179930c7ca93SDavid Disseldorp 			" %llu\n", dest_tpg->se_tpg_tfo->fabric_name,
180079dc9c9eSNicholas Bellinger 			dest_node_acl->initiatorname, i_buf, (dest_se_deve) ?
180179dc9c9eSNicholas Bellinger 			dest_se_deve->mapped_lun : 0);
1802c66ac9dbSNicholas Bellinger 
1803f32ba612SMike Christie 		if (dest_pr_reg == local_pr_reg)
1804c66ac9dbSNicholas Bellinger 			continue;
1805f32ba612SMike Christie 
1806c66ac9dbSNicholas Bellinger 		core_scsi3_nodeacl_undepend_item(dest_node_acl);
1807c66ac9dbSNicholas Bellinger 		core_scsi3_tpg_undepend_item(dest_tpg);
1808c66ac9dbSNicholas Bellinger 	}
1809c66ac9dbSNicholas Bellinger 
1810c66ac9dbSNicholas Bellinger 	return 0;
1811de103c93SChristoph Hellwig out_unmap:
18124949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
1813de103c93SChristoph Hellwig out:
1814c66ac9dbSNicholas Bellinger 	/*
1815c66ac9dbSNicholas Bellinger 	 * For the failure case, release everything from tid_dest_list
1816c66ac9dbSNicholas Bellinger 	 * including *dest_pr_reg and the configfs dependances..
1817c66ac9dbSNicholas Bellinger 	 */
1818c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1819f32ba612SMike Christie 		bool is_local = false;
1820f32ba612SMike Christie 
1821c66ac9dbSNicholas Bellinger 		dest_tpg = tidh->dest_tpg;
1822c66ac9dbSNicholas Bellinger 		dest_node_acl = tidh->dest_node_acl;
1823c66ac9dbSNicholas Bellinger 		dest_se_deve = tidh->dest_se_deve;
1824c66ac9dbSNicholas Bellinger 		dest_pr_reg = tidh->dest_pr_reg;
1825c66ac9dbSNicholas Bellinger 
1826f32ba612SMike Christie 		if (dest_pr_reg == local_pr_reg)
1827f32ba612SMike Christie 			is_local = true;
1828f32ba612SMike Christie 
1829c66ac9dbSNicholas Bellinger 		list_del(&tidh->dest_list);
1830c66ac9dbSNicholas Bellinger 		kfree(tidh);
1831c66ac9dbSNicholas Bellinger 		/*
1832c66ac9dbSNicholas Bellinger 		 * Release any extra ALL_TG_PT=1 registrations for
1833c66ac9dbSNicholas Bellinger 		 * the SPEC_I_PT=1 case.
1834c66ac9dbSNicholas Bellinger 		 */
1835c66ac9dbSNicholas Bellinger 		list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1836c66ac9dbSNicholas Bellinger 				&dest_pr_reg->pr_reg_atp_list,
1837c66ac9dbSNicholas Bellinger 				pr_reg_atp_mem_list) {
1838c66ac9dbSNicholas Bellinger 			list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1839c66ac9dbSNicholas Bellinger 			core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1840c66ac9dbSNicholas Bellinger 			kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1841c66ac9dbSNicholas Bellinger 		}
1842c66ac9dbSNicholas Bellinger 
1843c66ac9dbSNicholas Bellinger 		kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1844c66ac9dbSNicholas Bellinger 		core_scsi3_lunacl_undepend_item(dest_se_deve);
1845f32ba612SMike Christie 
1846f32ba612SMike Christie 		if (is_local)
1847f32ba612SMike Christie 			continue;
1848f32ba612SMike Christie 
1849c66ac9dbSNicholas Bellinger 		core_scsi3_nodeacl_undepend_item(dest_node_acl);
1850c66ac9dbSNicholas Bellinger 		core_scsi3_tpg_undepend_item(dest_tpg);
1851c66ac9dbSNicholas Bellinger 	}
1852c66ac9dbSNicholas Bellinger 	return ret;
1853c66ac9dbSNicholas Bellinger }
1854c66ac9dbSNicholas Bellinger 
core_scsi3_update_aptpl_buf(struct se_device * dev,unsigned char * buf,u32 pr_aptpl_buf_len)18553c8a6228SAndy Grover static int core_scsi3_update_aptpl_buf(
1856c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1857c66ac9dbSNicholas Bellinger 	unsigned char *buf,
18580607decdSAndy Grover 	u32 pr_aptpl_buf_len)
1859c66ac9dbSNicholas Bellinger {
1860c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg;
1861c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1862c66ac9dbSNicholas Bellinger 	unsigned char tmp[512], isid_buf[32];
1863c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1864c66ac9dbSNicholas Bellinger 	int reg_count = 0;
18653c8a6228SAndy Grover 	int ret = 0;
1866c66ac9dbSNicholas Bellinger 
18673c8a6228SAndy Grover 	spin_lock(&dev->dev_reservation_lock);
18683c8a6228SAndy Grover 	spin_lock(&dev->t10_pr.registration_lock);
1869c66ac9dbSNicholas Bellinger 	/*
1870c66ac9dbSNicholas Bellinger 	 * Walk the registration list..
1871c66ac9dbSNicholas Bellinger 	 */
18720fd97ccfSChristoph Hellwig 	list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1873c66ac9dbSNicholas Bellinger 			pr_reg_list) {
1874c66ac9dbSNicholas Bellinger 
1875c66ac9dbSNicholas Bellinger 		tmp[0] = '\0';
1876c66ac9dbSNicholas Bellinger 		isid_buf[0] = '\0';
1877c66ac9dbSNicholas Bellinger 		tpg = pr_reg->pr_reg_nacl->se_tpg;
1878c66ac9dbSNicholas Bellinger 		/*
1879c66ac9dbSNicholas Bellinger 		 * Write out any ISID value to APTPL metadata that was included
1880c66ac9dbSNicholas Bellinger 		 * in the original registration.
1881c66ac9dbSNicholas Bellinger 		 */
1882c66ac9dbSNicholas Bellinger 		if (pr_reg->isid_present_at_reg)
1883c66ac9dbSNicholas Bellinger 			snprintf(isid_buf, 32, "initiator_sid=%s\n",
1884c66ac9dbSNicholas Bellinger 					pr_reg->pr_reg_isid);
1885c66ac9dbSNicholas Bellinger 		/*
1886c66ac9dbSNicholas Bellinger 		 * Include special metadata if the pr_reg matches the
1887c66ac9dbSNicholas Bellinger 		 * reservation holder.
1888c66ac9dbSNicholas Bellinger 		 */
1889c66ac9dbSNicholas Bellinger 		if (dev->dev_pr_res_holder == pr_reg) {
1890c66ac9dbSNicholas Bellinger 			snprintf(tmp, 512, "PR_REG_START: %d"
1891c66ac9dbSNicholas Bellinger 				"\ninitiator_fabric=%s\n"
1892c66ac9dbSNicholas Bellinger 				"initiator_node=%s\n%s"
1893c66ac9dbSNicholas Bellinger 				"sa_res_key=%llu\n"
1894c66ac9dbSNicholas Bellinger 				"res_holder=1\nres_type=%02x\n"
1895c66ac9dbSNicholas Bellinger 				"res_scope=%02x\nres_all_tg_pt=%d\n"
1896f2d30680SHannes Reinecke 				"mapped_lun=%llu\n", reg_count,
189730c7ca93SDavid Disseldorp 				tpg->se_tpg_tfo->fabric_name,
1898c66ac9dbSNicholas Bellinger 				pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1899c66ac9dbSNicholas Bellinger 				pr_reg->pr_res_key, pr_reg->pr_res_type,
1900c66ac9dbSNicholas Bellinger 				pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1901c66ac9dbSNicholas Bellinger 				pr_reg->pr_res_mapped_lun);
1902c66ac9dbSNicholas Bellinger 		} else {
1903c66ac9dbSNicholas Bellinger 			snprintf(tmp, 512, "PR_REG_START: %d\n"
1904c66ac9dbSNicholas Bellinger 				"initiator_fabric=%s\ninitiator_node=%s\n%s"
1905c66ac9dbSNicholas Bellinger 				"sa_res_key=%llu\nres_holder=0\n"
1906f2d30680SHannes Reinecke 				"res_all_tg_pt=%d\nmapped_lun=%llu\n",
190730c7ca93SDavid Disseldorp 				reg_count, tpg->se_tpg_tfo->fabric_name,
1908c66ac9dbSNicholas Bellinger 				pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1909c66ac9dbSNicholas Bellinger 				pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1910c66ac9dbSNicholas Bellinger 				pr_reg->pr_res_mapped_lun);
1911c66ac9dbSNicholas Bellinger 		}
1912c66ac9dbSNicholas Bellinger 
191360d645a4SDan Carpenter 		if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1914f161d4b4SNicholas Bellinger 			pr_err("Unable to update renaming APTPL metadata,"
1915f161d4b4SNicholas Bellinger 			       " reallocating larger buffer\n");
19163c8a6228SAndy Grover 			ret = -EMSGSIZE;
19173c8a6228SAndy Grover 			goto out;
1918c66ac9dbSNicholas Bellinger 		}
1919c66ac9dbSNicholas Bellinger 		len += sprintf(buf+len, "%s", tmp);
1920c66ac9dbSNicholas Bellinger 
1921c66ac9dbSNicholas Bellinger 		/*
1922c66ac9dbSNicholas Bellinger 		 * Include information about the associated SCSI target port.
1923c66ac9dbSNicholas Bellinger 		 */
1924c66ac9dbSNicholas Bellinger 		snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1925f2d30680SHannes Reinecke 			"tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%llu\nPR_REG_END:"
192630c7ca93SDavid Disseldorp 			" %d\n", tpg->se_tpg_tfo->fabric_name,
1927e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1928e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_tag(tpg),
192979dc9c9eSNicholas Bellinger 			pr_reg->tg_pt_sep_rtpi, pr_reg->pr_aptpl_target_lun,
193079dc9c9eSNicholas Bellinger 			reg_count);
1931c66ac9dbSNicholas Bellinger 
193260d645a4SDan Carpenter 		if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1933f161d4b4SNicholas Bellinger 			pr_err("Unable to update renaming APTPL metadata,"
1934f161d4b4SNicholas Bellinger 			       " reallocating larger buffer\n");
19353c8a6228SAndy Grover 			ret = -EMSGSIZE;
19363c8a6228SAndy Grover 			goto out;
1937c66ac9dbSNicholas Bellinger 		}
1938c66ac9dbSNicholas Bellinger 		len += sprintf(buf+len, "%s", tmp);
1939c66ac9dbSNicholas Bellinger 		reg_count++;
1940c66ac9dbSNicholas Bellinger 	}
1941c66ac9dbSNicholas Bellinger 
19426708bb27SAndy Grover 	if (!reg_count)
1943c66ac9dbSNicholas Bellinger 		len += sprintf(buf+len, "No Registrations or Reservations");
1944c66ac9dbSNicholas Bellinger 
19453c8a6228SAndy Grover out:
19463c8a6228SAndy Grover 	spin_unlock(&dev->t10_pr.registration_lock);
1947c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
1948c66ac9dbSNicholas Bellinger 
1949c66ac9dbSNicholas Bellinger 	return ret;
1950c66ac9dbSNicholas Bellinger }
1951c66ac9dbSNicholas Bellinger 
__core_scsi3_write_aptpl_to_file(struct se_device * dev,unsigned char * buf)1952c66ac9dbSNicholas Bellinger static int __core_scsi3_write_aptpl_to_file(
1953c66ac9dbSNicholas Bellinger 	struct se_device *dev,
19544dee96fbSAndy Grover 	unsigned char *buf)
1955c66ac9dbSNicholas Bellinger {
19560fd97ccfSChristoph Hellwig 	struct t10_wwn *wwn = &dev->t10_wwn;
1957c66ac9dbSNicholas Bellinger 	struct file *file;
1958c66ac9dbSNicholas Bellinger 	int flags = O_RDWR | O_CREAT | O_TRUNC;
1959bdc79f0eSDavid Disseldorp 	char *path;
19604dee96fbSAndy Grover 	u32 pr_aptpl_buf_len;
1961c66ac9dbSNicholas Bellinger 	int ret;
1962e13ec939SChristoph Hellwig 	loff_t pos = 0;
1963c66ac9dbSNicholas Bellinger 
1964bdc79f0eSDavid Disseldorp 	path = kasprintf(GFP_KERNEL, "%s/pr/aptpl_%s", db_root,
1965bdc79f0eSDavid Disseldorp 			&wwn->unit_serial[0]);
1966bdc79f0eSDavid Disseldorp 	if (!path)
1967bdc79f0eSDavid Disseldorp 		return -ENOMEM;
1968c66ac9dbSNicholas Bellinger 
1969c66ac9dbSNicholas Bellinger 	file = filp_open(path, flags, 0600);
19700e9b10a9SAl Viro 	if (IS_ERR(file)) {
19716708bb27SAndy Grover 		pr_err("filp_open(%s) for APTPL metadata"
1972c66ac9dbSNicholas Bellinger 			" failed\n", path);
1973bdc79f0eSDavid Disseldorp 		kfree(path);
19740e9b10a9SAl Viro 		return PTR_ERR(file);
1975c66ac9dbSNicholas Bellinger 	}
1976c66ac9dbSNicholas Bellinger 
19774dee96fbSAndy Grover 	pr_aptpl_buf_len = (strlen(buf) + 1); /* Add extra for NULL */
1978c66ac9dbSNicholas Bellinger 
1979e13ec939SChristoph Hellwig 	ret = kernel_write(file, buf, pr_aptpl_buf_len, &pos);
1980c66ac9dbSNicholas Bellinger 
19810e9b10a9SAl Viro 	if (ret < 0)
19826708bb27SAndy Grover 		pr_debug("Error writing APTPL metadata file: %s\n", path);
19830e9b10a9SAl Viro 	fput(file);
1984bdc79f0eSDavid Disseldorp 	kfree(path);
1985c66ac9dbSNicholas Bellinger 
1986f730f915SGera Kazakov 	return (ret < 0) ? -EIO : 0;
1987c66ac9dbSNicholas Bellinger }
1988c66ac9dbSNicholas Bellinger 
1989c66ac9dbSNicholas Bellinger /*
1990459f213bSAndy Grover  * Clear the APTPL metadata if APTPL has been disabled, otherwise
1991459f213bSAndy Grover  * write out the updated metadata to struct file for this SCSI device.
1992c66ac9dbSNicholas Bellinger  */
core_scsi3_update_and_write_aptpl(struct se_device * dev,bool aptpl)19938a391857SNicholas Bellinger static sense_reason_t core_scsi3_update_and_write_aptpl(struct se_device *dev, bool aptpl)
1994459f213bSAndy Grover {
19958a391857SNicholas Bellinger 	unsigned char *buf;
1996f161d4b4SNicholas Bellinger 	int rc, len = PR_APTPL_BUF_LEN;
1997459f213bSAndy Grover 
1998459f213bSAndy Grover 	if (!aptpl) {
1999459f213bSAndy Grover 		char *null_buf = "No Registrations or Reservations\n";
2000459f213bSAndy Grover 
20018a391857SNicholas Bellinger 		rc = __core_scsi3_write_aptpl_to_file(dev, null_buf);
2002459f213bSAndy Grover 		dev->t10_pr.pr_aptpl_active = 0;
2003459f213bSAndy Grover 		pr_debug("SPC-3 PR: Set APTPL Bit Deactivated\n");
20048a391857SNicholas Bellinger 
20058a391857SNicholas Bellinger 		if (rc)
20068a391857SNicholas Bellinger 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
20078a391857SNicholas Bellinger 
20088a391857SNicholas Bellinger 		return 0;
20098a391857SNicholas Bellinger 	}
2010f161d4b4SNicholas Bellinger retry:
2011f161d4b4SNicholas Bellinger 	buf = vzalloc(len);
2012459f213bSAndy Grover 	if (!buf)
20138a391857SNicholas Bellinger 		return TCM_OUT_OF_RESOURCES;
2014459f213bSAndy Grover 
2015f161d4b4SNicholas Bellinger 	rc = core_scsi3_update_aptpl_buf(dev, buf, len);
20168a391857SNicholas Bellinger 	if (rc < 0) {
2017f161d4b4SNicholas Bellinger 		vfree(buf);
2018f161d4b4SNicholas Bellinger 		len *= 2;
2019f161d4b4SNicholas Bellinger 		goto retry;
20200607decdSAndy Grover 	}
2021de103c93SChristoph Hellwig 
20228a391857SNicholas Bellinger 	rc = __core_scsi3_write_aptpl_to_file(dev, buf);
20238a391857SNicholas Bellinger 	if (rc != 0) {
2024459f213bSAndy Grover 		pr_err("SPC-3 PR: Could not update APTPL\n");
2025f161d4b4SNicholas Bellinger 		vfree(buf);
20268a391857SNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2027459f213bSAndy Grover 	}
20288a391857SNicholas Bellinger 	dev->t10_pr.pr_aptpl_active = 1;
2029f161d4b4SNicholas Bellinger 	vfree(buf);
20308a391857SNicholas Bellinger 	pr_debug("SPC-3 PR: Set APTPL Bit Activated\n");
20318a391857SNicholas Bellinger 	return 0;
2032c66ac9dbSNicholas Bellinger }
2033c66ac9dbSNicholas Bellinger 
2034de103c93SChristoph Hellwig static sense_reason_t
core_scsi3_emulate_pro_register(struct se_cmd * cmd,u64 res_key,u64 sa_res_key,bool aptpl,bool all_tg_pt,bool spec_i_pt,enum register_type register_type)2035de103c93SChristoph Hellwig core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
2036bc118fe4SAndy Grover 		bool aptpl, bool all_tg_pt, bool spec_i_pt, enum register_type register_type)
2037c66ac9dbSNicholas Bellinger {
2038e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
20395951146dSAndy Grover 	struct se_device *dev = cmd->se_dev;
2040e3d6f909SAndy Grover 	struct se_lun *se_lun = cmd->se_lun;
2041c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg;
2042bc118fe4SAndy Grover 	struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp;
20430fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
20444db6dfe6SChaitanya Kulkarni 	unsigned char isid_buf[PR_REG_ISID_LEN] = { };
20454db6dfe6SChaitanya Kulkarni 	unsigned char *isid_ptr = NULL;
2046a0d50f62SHannes Reinecke 	sense_reason_t ret = TCM_NO_SENSE;
2047fc09149dSNicholas Bellinger 	int pr_holder = 0, type;
2048c66ac9dbSNicholas Bellinger 
20496708bb27SAndy Grover 	if (!se_sess || !se_lun) {
20506708bb27SAndy Grover 		pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2051de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2052c66ac9dbSNicholas Bellinger 	}
2053c66ac9dbSNicholas Bellinger 	se_tpg = se_sess->se_tpg;
2054c66ac9dbSNicholas Bellinger 
2055e3d6f909SAndy Grover 	if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
2056e3d6f909SAndy Grover 		se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
2057c66ac9dbSNicholas Bellinger 				PR_REG_ISID_LEN);
2058c66ac9dbSNicholas Bellinger 		isid_ptr = &isid_buf[0];
2059c66ac9dbSNicholas Bellinger 	}
2060c66ac9dbSNicholas Bellinger 	/*
2061c66ac9dbSNicholas Bellinger 	 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2062c66ac9dbSNicholas Bellinger 	 */
2063bc118fe4SAndy Grover 	pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2064bc118fe4SAndy Grover 	if (!pr_reg) {
2065c66ac9dbSNicholas Bellinger 		if (res_key) {
20666708bb27SAndy Grover 			pr_warn("SPC-3 PR: Reservation Key non-zero"
2067c66ac9dbSNicholas Bellinger 				" for SA REGISTER, returning CONFLICT\n");
2068de103c93SChristoph Hellwig 			return TCM_RESERVATION_CONFLICT;
2069c66ac9dbSNicholas Bellinger 		}
2070c66ac9dbSNicholas Bellinger 		/*
2071c66ac9dbSNicholas Bellinger 		 * Do nothing but return GOOD status.
2072c66ac9dbSNicholas Bellinger 		 */
20736708bb27SAndy Grover 		if (!sa_res_key)
207403e98c9eSNicholas Bellinger 			return 0;
2075c66ac9dbSNicholas Bellinger 
20766708bb27SAndy Grover 		if (!spec_i_pt) {
2077c66ac9dbSNicholas Bellinger 			/*
2078c66ac9dbSNicholas Bellinger 			 * Perform the Service Action REGISTER on the Initiator
2079c66ac9dbSNicholas Bellinger 			 * Port Endpoint that the PRO was received from on the
2080c66ac9dbSNicholas Bellinger 			 * Logical Unit of the SCSI device server.
2081c66ac9dbSNicholas Bellinger 			 */
2082de103c93SChristoph Hellwig 			if (core_scsi3_alloc_registration(cmd->se_dev,
208379dc9c9eSNicholas Bellinger 					se_sess->se_node_acl, cmd->se_lun,
208479dc9c9eSNicholas Bellinger 					NULL, cmd->orig_fe_lun, isid_ptr,
2085c66ac9dbSNicholas Bellinger 					sa_res_key, all_tg_pt, aptpl,
208633ce6a87SAndy Grover 					register_type, 0)) {
20876708bb27SAndy Grover 				pr_err("Unable to allocate"
2088c66ac9dbSNicholas Bellinger 					" struct t10_pr_registration\n");
208979dd6f2fStangwenji 				return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
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 		}
2105459f213bSAndy Grover 		return core_scsi3_update_and_write_aptpl(dev, aptpl);
2106de103c93SChristoph Hellwig 	}
2107de103c93SChristoph Hellwig 
2108bc118fe4SAndy Grover 	/* ok, existing registration */
2109c66ac9dbSNicholas Bellinger 
2110bc118fe4SAndy Grover 	if ((register_type == REGISTER) && (res_key != pr_reg->pr_res_key)) {
21116708bb27SAndy Grover 		pr_err("SPC-3 PR REGISTER: Received"
2112c66ac9dbSNicholas Bellinger 		       " res_key: 0x%016Lx does not match"
2113c66ac9dbSNicholas Bellinger 		       " existing SA REGISTER res_key:"
2114c66ac9dbSNicholas Bellinger 		       " 0x%016Lx\n", res_key,
2115c66ac9dbSNicholas Bellinger 		       pr_reg->pr_res_key);
2116de103c93SChristoph Hellwig 		ret = TCM_RESERVATION_CONFLICT;
2117bc118fe4SAndy Grover 		goto out;
2118c66ac9dbSNicholas Bellinger 	}
2119de103c93SChristoph Hellwig 
2120c66ac9dbSNicholas Bellinger 	if (spec_i_pt) {
21211f070cc2SAndy Grover 		pr_err("SPC-3 PR REGISTER: SPEC_I_PT"
21221f070cc2SAndy Grover 			" set on a registered nexus\n");
2123de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
2124bc118fe4SAndy Grover 		goto out;
2125c66ac9dbSNicholas Bellinger 	}
2126de103c93SChristoph Hellwig 
2127c66ac9dbSNicholas Bellinger 	/*
2128c66ac9dbSNicholas Bellinger 	 * An existing ALL_TG_PT=1 registration being released
2129c66ac9dbSNicholas Bellinger 	 * must also set ALL_TG_PT=1 in the incoming PROUT.
2130c66ac9dbSNicholas Bellinger 	 */
21311f070cc2SAndy Grover 	if (pr_reg->pr_reg_all_tg_pt && !all_tg_pt) {
21321f070cc2SAndy Grover 		pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1"
2133c66ac9dbSNicholas Bellinger 			" registration exists, but ALL_TG_PT=1 bit not"
2134c66ac9dbSNicholas Bellinger 			" present in received PROUT\n");
2135de103c93SChristoph Hellwig 		ret = TCM_INVALID_CDB_FIELD;
2136bc118fe4SAndy Grover 		goto out;
2137c66ac9dbSNicholas Bellinger 	}
2138de103c93SChristoph Hellwig 
2139c66ac9dbSNicholas Bellinger 	/*
214051d9c41dSAndy Grover 	 * sa_res_key=1 Change Reservation Key for registered I_T Nexus.
2141c66ac9dbSNicholas Bellinger 	 */
214251d9c41dSAndy Grover 	if (sa_res_key) {
214351d9c41dSAndy Grover 		/*
214451d9c41dSAndy Grover 		 * Increment PRgeneration counter for struct se_device"
214551d9c41dSAndy Grover 		 * upon a successful REGISTER, see spc4r17 section 6.3.2
214651d9c41dSAndy Grover 		 * READ_KEYS service action.
214751d9c41dSAndy Grover 		 */
214851d9c41dSAndy Grover 		pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev);
214951d9c41dSAndy Grover 		pr_reg->pr_res_key = sa_res_key;
215051d9c41dSAndy Grover 		pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
215151d9c41dSAndy Grover 			 " Key for %s to: 0x%016Lx PRgeneration:"
215230c7ca93SDavid Disseldorp 			 " 0x%08x\n", cmd->se_tfo->fabric_name,
215351d9c41dSAndy Grover 			 (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? "_AND_IGNORE_EXISTING_KEY" : "",
215451d9c41dSAndy Grover 			 pr_reg->pr_reg_nacl->initiatorname,
215551d9c41dSAndy Grover 			 pr_reg->pr_res_key, pr_reg->pr_res_generation);
215651d9c41dSAndy Grover 
215751d9c41dSAndy Grover 	} else {
215851d9c41dSAndy Grover 		/*
215951d9c41dSAndy Grover 		 * sa_res_key=0 Unregister Reservation Key for registered I_T Nexus.
216051d9c41dSAndy Grover 		 */
21616c3c9baaSNicholas Bellinger 		type = pr_reg->pr_res_type;
21626c3c9baaSNicholas Bellinger 		pr_holder = core_scsi3_check_implicit_release(cmd->se_dev,
21636c3c9baaSNicholas Bellinger 							      pr_reg);
2164c66ac9dbSNicholas Bellinger 		if (pr_holder < 0) {
2165de103c93SChristoph Hellwig 			ret = TCM_RESERVATION_CONFLICT;
2166bc118fe4SAndy Grover 			goto out;
2167c66ac9dbSNicholas Bellinger 		}
2168c66ac9dbSNicholas Bellinger 
2169c66ac9dbSNicholas Bellinger 		spin_lock(&pr_tmpl->registration_lock);
2170c66ac9dbSNicholas Bellinger 		/*
2171c66ac9dbSNicholas Bellinger 		 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2172c66ac9dbSNicholas Bellinger 		 * and matching pr_res_key.
2173c66ac9dbSNicholas Bellinger 		 */
2174c66ac9dbSNicholas Bellinger 		if (pr_reg->pr_reg_all_tg_pt) {
2175c66ac9dbSNicholas Bellinger 			list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2176c66ac9dbSNicholas Bellinger 					&pr_tmpl->registration_list,
2177c66ac9dbSNicholas Bellinger 					pr_reg_list) {
2178c66ac9dbSNicholas Bellinger 
21796708bb27SAndy Grover 				if (!pr_reg_p->pr_reg_all_tg_pt)
2180c66ac9dbSNicholas Bellinger 					continue;
2181c66ac9dbSNicholas Bellinger 				if (pr_reg_p->pr_res_key != res_key)
2182c66ac9dbSNicholas Bellinger 					continue;
2183c66ac9dbSNicholas Bellinger 				if (pr_reg == pr_reg_p)
2184c66ac9dbSNicholas Bellinger 					continue;
2185c66ac9dbSNicholas Bellinger 				if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2186c66ac9dbSNicholas Bellinger 					   pr_reg_p->pr_reg_nacl->initiatorname))
2187c66ac9dbSNicholas Bellinger 					continue;
2188c66ac9dbSNicholas Bellinger 
2189c66ac9dbSNicholas Bellinger 				__core_scsi3_free_registration(dev,
2190c66ac9dbSNicholas Bellinger 						pr_reg_p, NULL, 0);
2191c66ac9dbSNicholas Bellinger 			}
2192c66ac9dbSNicholas Bellinger 		}
2193de103c93SChristoph Hellwig 
2194c66ac9dbSNicholas Bellinger 		/*
2195c66ac9dbSNicholas Bellinger 		 * Release the calling I_T Nexus registration now..
2196c66ac9dbSNicholas Bellinger 		 */
2197de103c93SChristoph Hellwig 		__core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1);
2198fc09149dSNicholas Bellinger 		pr_reg = NULL;
2199de103c93SChristoph Hellwig 
2200c66ac9dbSNicholas Bellinger 		/*
2201c66ac9dbSNicholas Bellinger 		 * From spc4r17, section 5.7.11.3 Unregistering
2202c66ac9dbSNicholas Bellinger 		 *
2203c66ac9dbSNicholas Bellinger 		 * If the persistent reservation is a registrants only
2204c66ac9dbSNicholas Bellinger 		 * type, the device server shall establish a unit
2205c66ac9dbSNicholas Bellinger 		 * attention condition for the initiator port associated
2206c66ac9dbSNicholas Bellinger 		 * with every registered I_T nexus except for the I_T
2207c66ac9dbSNicholas Bellinger 		 * nexus on which the PERSISTENT RESERVE OUT command was
2208c66ac9dbSNicholas Bellinger 		 * received, with the additional sense code set to
2209c66ac9dbSNicholas Bellinger 		 * RESERVATIONS RELEASED.
2210c66ac9dbSNicholas Bellinger 		 */
2211c66ac9dbSNicholas Bellinger 		if (pr_holder &&
2212fc09149dSNicholas Bellinger 		    (type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY ||
2213fc09149dSNicholas Bellinger 		     type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) {
2214c66ac9dbSNicholas Bellinger 			list_for_each_entry(pr_reg_p,
2215c66ac9dbSNicholas Bellinger 					&pr_tmpl->registration_list,
2216c66ac9dbSNicholas Bellinger 					pr_reg_list) {
2217c66ac9dbSNicholas Bellinger 
2218c51c8e7bSHannes Reinecke 				target_ua_allocate_lun(
2219c66ac9dbSNicholas Bellinger 					pr_reg_p->pr_reg_nacl,
2220c66ac9dbSNicholas Bellinger 					pr_reg_p->pr_res_mapped_lun,
2221c66ac9dbSNicholas Bellinger 					0x2A,
2222c66ac9dbSNicholas Bellinger 					ASCQ_2AH_RESERVATIONS_RELEASED);
2223c66ac9dbSNicholas Bellinger 			}
2224c66ac9dbSNicholas Bellinger 		}
222551d9c41dSAndy Grover 
2226c66ac9dbSNicholas Bellinger 		spin_unlock(&pr_tmpl->registration_lock);
2227de103c93SChristoph Hellwig 	}
2228de103c93SChristoph Hellwig 
2229459f213bSAndy Grover 	ret = core_scsi3_update_and_write_aptpl(dev, aptpl);
2230c66ac9dbSNicholas Bellinger 
2231bc118fe4SAndy Grover out:
2232fc09149dSNicholas Bellinger 	if (pr_reg)
2233c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg);
2234de103c93SChristoph Hellwig 	return ret;
2235c66ac9dbSNicholas Bellinger }
2236c66ac9dbSNicholas Bellinger 
core_scsi3_pr_dump_type(int type)2237c66ac9dbSNicholas Bellinger unsigned char *core_scsi3_pr_dump_type(int type)
2238c66ac9dbSNicholas Bellinger {
2239c66ac9dbSNicholas Bellinger 	switch (type) {
2240c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE:
2241c66ac9dbSNicholas Bellinger 		return "Write Exclusive Access";
2242c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS:
2243c66ac9dbSNicholas Bellinger 		return "Exclusive Access";
2244c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2245c66ac9dbSNicholas Bellinger 		return "Write Exclusive Access, Registrants Only";
2246c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2247c66ac9dbSNicholas Bellinger 		return "Exclusive Access, Registrants Only";
2248c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2249c66ac9dbSNicholas Bellinger 		return "Write Exclusive Access, All Registrants";
2250c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2251c66ac9dbSNicholas Bellinger 		return "Exclusive Access, All Registrants";
2252c66ac9dbSNicholas Bellinger 	default:
2253c66ac9dbSNicholas Bellinger 		break;
2254c66ac9dbSNicholas Bellinger 	}
2255c66ac9dbSNicholas Bellinger 
2256c66ac9dbSNicholas Bellinger 	return "Unknown SPC-3 PR Type";
2257c66ac9dbSNicholas Bellinger }
2258c66ac9dbSNicholas Bellinger 
2259de103c93SChristoph Hellwig static sense_reason_t
core_scsi3_pro_reserve(struct se_cmd * cmd,int type,int scope,u64 res_key)2260de103c93SChristoph Hellwig core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key)
2261c66ac9dbSNicholas Bellinger {
2262de103c93SChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
2263e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
2264e3d6f909SAndy Grover 	struct se_lun *se_lun = cmd->se_lun;
2265c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_res_holder;
22660fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
22674db6dfe6SChaitanya Kulkarni 	char i_buf[PR_REG_ISID_ID_LEN] = { };
2268de103c93SChristoph Hellwig 	sense_reason_t ret;
2269c66ac9dbSNicholas Bellinger 
22706708bb27SAndy Grover 	if (!se_sess || !se_lun) {
22716708bb27SAndy Grover 		pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2272de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2273c66ac9dbSNicholas Bellinger 	}
2274c66ac9dbSNicholas Bellinger 	/*
2275c66ac9dbSNicholas Bellinger 	 * Locate the existing *pr_reg via struct se_node_acl pointers
2276c66ac9dbSNicholas Bellinger 	 */
22775951146dSAndy Grover 	pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2278c66ac9dbSNicholas Bellinger 				se_sess);
22796708bb27SAndy Grover 	if (!pr_reg) {
22806708bb27SAndy Grover 		pr_err("SPC-3 PR: Unable to locate"
2281c66ac9dbSNicholas Bellinger 			" PR_REGISTERED *pr_reg for RESERVE\n");
2282de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2283c66ac9dbSNicholas Bellinger 	}
2284c66ac9dbSNicholas Bellinger 	/*
2285c66ac9dbSNicholas Bellinger 	 * From spc4r17 Section 5.7.9: Reserving:
2286c66ac9dbSNicholas Bellinger 	 *
2287c66ac9dbSNicholas Bellinger 	 * An application client creates a persistent reservation by issuing
2288c66ac9dbSNicholas Bellinger 	 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2289c66ac9dbSNicholas Bellinger 	 * a registered I_T nexus with the following parameters:
2290c66ac9dbSNicholas Bellinger 	 *    a) RESERVATION KEY set to the value of the reservation key that is
2291c66ac9dbSNicholas Bellinger 	 * 	 registered with the logical unit for the I_T nexus; and
2292c66ac9dbSNicholas Bellinger 	 */
2293c66ac9dbSNicholas Bellinger 	if (res_key != pr_reg->pr_res_key) {
22946708bb27SAndy Grover 		pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2295c66ac9dbSNicholas Bellinger 			" does not match existing SA REGISTER res_key:"
2296c66ac9dbSNicholas Bellinger 			" 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2297de103c93SChristoph Hellwig 		ret = TCM_RESERVATION_CONFLICT;
2298de103c93SChristoph Hellwig 		goto out_put_pr_reg;
2299c66ac9dbSNicholas Bellinger 	}
2300c66ac9dbSNicholas Bellinger 	/*
2301c66ac9dbSNicholas Bellinger 	 * From spc4r17 Section 5.7.9: Reserving:
2302c66ac9dbSNicholas Bellinger 	 *
2303c66ac9dbSNicholas Bellinger 	 * From above:
2304c66ac9dbSNicholas Bellinger 	 *  b) TYPE field and SCOPE field set to the persistent reservation
2305c66ac9dbSNicholas Bellinger 	 *     being created.
2306c66ac9dbSNicholas Bellinger 	 *
2307c66ac9dbSNicholas Bellinger 	 * Only one persistent reservation is allowed at a time per logical unit
2308c66ac9dbSNicholas Bellinger 	 * and that persistent reservation has a scope of LU_SCOPE.
2309c66ac9dbSNicholas Bellinger 	 */
2310c66ac9dbSNicholas Bellinger 	if (scope != PR_SCOPE_LU_SCOPE) {
23116708bb27SAndy Grover 		pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2312de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
2313de103c93SChristoph Hellwig 		goto out_put_pr_reg;
2314c66ac9dbSNicholas Bellinger 	}
2315c66ac9dbSNicholas Bellinger 	/*
2316c66ac9dbSNicholas Bellinger 	 * See if we have an existing PR reservation holder pointer at
2317c66ac9dbSNicholas Bellinger 	 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2318c66ac9dbSNicholas Bellinger 	 * *pr_res_holder.
2319c66ac9dbSNicholas Bellinger 	 */
2320c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
2321c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
2322ee1b1b9cSAndy Grover 	if (pr_res_holder) {
2323c66ac9dbSNicholas Bellinger 		/*
2324c66ac9dbSNicholas Bellinger 		 * From spc4r17 Section 5.7.9: Reserving:
2325c66ac9dbSNicholas Bellinger 		 *
2326c66ac9dbSNicholas Bellinger 		 * If the device server receives a PERSISTENT RESERVE OUT
2327c66ac9dbSNicholas Bellinger 		 * command from an I_T nexus other than a persistent reservation
2328c66ac9dbSNicholas Bellinger 		 * holder (see 5.7.10) that attempts to create a persistent
2329c66ac9dbSNicholas Bellinger 		 * reservation when a persistent reservation already exists for
2330c66ac9dbSNicholas Bellinger 		 * the logical unit, then the command shall be completed with
2331c66ac9dbSNicholas Bellinger 		 * RESERVATION CONFLICT status.
2332c66ac9dbSNicholas Bellinger 		 */
2333e673dc92SIlias Tsitsimpis 		if (!is_reservation_holder(pr_res_holder, pr_reg)) {
2334c66ac9dbSNicholas Bellinger 			struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
23356708bb27SAndy Grover 			pr_err("SPC-3 PR: Attempted RESERVE from"
2336c66ac9dbSNicholas Bellinger 				" [%s]: %s while reservation already held by"
2337c66ac9dbSNicholas Bellinger 				" [%s]: %s, returning RESERVATION_CONFLICT\n",
233830c7ca93SDavid Disseldorp 				cmd->se_tfo->fabric_name,
2339c66ac9dbSNicholas Bellinger 				se_sess->se_node_acl->initiatorname,
234030c7ca93SDavid Disseldorp 				pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
2341c66ac9dbSNicholas Bellinger 				pr_res_holder->pr_reg_nacl->initiatorname);
2342c66ac9dbSNicholas Bellinger 
2343c66ac9dbSNicholas Bellinger 			spin_unlock(&dev->dev_reservation_lock);
2344de103c93SChristoph Hellwig 			ret = TCM_RESERVATION_CONFLICT;
2345de103c93SChristoph Hellwig 			goto out_put_pr_reg;
2346c66ac9dbSNicholas Bellinger 		}
2347c66ac9dbSNicholas Bellinger 		/*
2348c66ac9dbSNicholas Bellinger 		 * From spc4r17 Section 5.7.9: Reserving:
2349c66ac9dbSNicholas Bellinger 		 *
2350c66ac9dbSNicholas Bellinger 		 * If a persistent reservation holder attempts to modify the
2351c66ac9dbSNicholas Bellinger 		 * type or scope of an existing persistent reservation, the
2352c66ac9dbSNicholas Bellinger 		 * command shall be completed with RESERVATION CONFLICT status.
2353c66ac9dbSNicholas Bellinger 		 */
2354c66ac9dbSNicholas Bellinger 		if ((pr_res_holder->pr_res_type != type) ||
2355c66ac9dbSNicholas Bellinger 		    (pr_res_holder->pr_res_scope != scope)) {
2356c66ac9dbSNicholas Bellinger 			struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
23576708bb27SAndy Grover 			pr_err("SPC-3 PR: Attempted RESERVE from"
2358c66ac9dbSNicholas Bellinger 				" [%s]: %s trying to change TYPE and/or SCOPE,"
2359c66ac9dbSNicholas Bellinger 				" while reservation already held by [%s]: %s,"
2360c66ac9dbSNicholas Bellinger 				" returning RESERVATION_CONFLICT\n",
236130c7ca93SDavid Disseldorp 				cmd->se_tfo->fabric_name,
2362c66ac9dbSNicholas Bellinger 				se_sess->se_node_acl->initiatorname,
236330c7ca93SDavid Disseldorp 				pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
2364c66ac9dbSNicholas Bellinger 				pr_res_holder->pr_reg_nacl->initiatorname);
2365c66ac9dbSNicholas Bellinger 
2366c66ac9dbSNicholas Bellinger 			spin_unlock(&dev->dev_reservation_lock);
2367de103c93SChristoph Hellwig 			ret = TCM_RESERVATION_CONFLICT;
2368de103c93SChristoph Hellwig 			goto out_put_pr_reg;
2369c66ac9dbSNicholas Bellinger 		}
2370c66ac9dbSNicholas Bellinger 		/*
2371c66ac9dbSNicholas Bellinger 		 * From spc4r17 Section 5.7.9: Reserving:
2372c66ac9dbSNicholas Bellinger 		 *
2373c66ac9dbSNicholas Bellinger 		 * If the device server receives a PERSISTENT RESERVE OUT
2374c66ac9dbSNicholas Bellinger 		 * command with RESERVE service action where the TYPE field and
2375c66ac9dbSNicholas Bellinger 		 * the SCOPE field contain the same values as the existing type
2376c66ac9dbSNicholas Bellinger 		 * and scope from a persistent reservation holder, it shall not
2377c66ac9dbSNicholas Bellinger 		 * make any change to the existing persistent reservation and
2378c66ac9dbSNicholas Bellinger 		 * shall completethe command with GOOD status.
2379c66ac9dbSNicholas Bellinger 		 */
2380c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
2381de103c93SChristoph Hellwig 		ret = 0;
2382de103c93SChristoph Hellwig 		goto out_put_pr_reg;
2383c66ac9dbSNicholas Bellinger 	}
2384c66ac9dbSNicholas Bellinger 	/*
2385c66ac9dbSNicholas Bellinger 	 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2386c66ac9dbSNicholas Bellinger 	 * TYPE/SCOPE.  Also set the received scope and type in *pr_reg.
2387c66ac9dbSNicholas Bellinger 	 */
2388c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_scope = scope;
2389c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_type = type;
2390c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_holder = 1;
2391c66ac9dbSNicholas Bellinger 	dev->dev_pr_res_holder = pr_reg;
2392d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2393c66ac9dbSNicholas Bellinger 
23946708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2395c66ac9dbSNicholas Bellinger 		" reservation holder TYPE: %s ALL_TG_PT: %d\n",
239630c7ca93SDavid Disseldorp 		cmd->se_tfo->fabric_name, core_scsi3_pr_dump_type(type),
2397c66ac9dbSNicholas Bellinger 		(pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
23986708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
239930c7ca93SDavid Disseldorp 			cmd->se_tfo->fabric_name,
2400c66ac9dbSNicholas Bellinger 			se_sess->se_node_acl->initiatorname,
2401d2843c17SAndy Grover 			i_buf);
2402c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
2403c66ac9dbSNicholas Bellinger 
2404459f213bSAndy Grover 	if (pr_tmpl->pr_aptpl_active)
2405459f213bSAndy Grover 		core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2406c66ac9dbSNicholas Bellinger 
2407de103c93SChristoph Hellwig 	ret = 0;
2408de103c93SChristoph Hellwig out_put_pr_reg:
2409de103c93SChristoph Hellwig 	core_scsi3_put_pr_reg(pr_reg);
2410de103c93SChristoph Hellwig 	return ret;
2411de103c93SChristoph Hellwig }
2412de103c93SChristoph Hellwig 
2413de103c93SChristoph Hellwig static sense_reason_t
core_scsi3_emulate_pro_reserve(struct se_cmd * cmd,int type,int scope,u64 res_key)2414de103c93SChristoph Hellwig core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope,
2415c66ac9dbSNicholas Bellinger 		u64 res_key)
2416c66ac9dbSNicholas Bellinger {
2417c66ac9dbSNicholas Bellinger 	switch (type) {
2418c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE:
2419c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS:
2420c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2421c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2422c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2423c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2424de103c93SChristoph Hellwig 		return core_scsi3_pro_reserve(cmd, type, scope, res_key);
2425c66ac9dbSNicholas Bellinger 	default:
24266708bb27SAndy Grover 		pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2427c66ac9dbSNicholas Bellinger 			" 0x%02x\n", type);
2428de103c93SChristoph Hellwig 		return TCM_INVALID_CDB_FIELD;
2429c66ac9dbSNicholas Bellinger 	}
2430c66ac9dbSNicholas Bellinger }
2431c66ac9dbSNicholas Bellinger 
__core_scsi3_complete_pro_release(struct se_device * dev,struct se_node_acl * se_nacl,struct t10_pr_registration * pr_reg,int explicit,int unreg)2432c66ac9dbSNicholas Bellinger static void __core_scsi3_complete_pro_release(
2433c66ac9dbSNicholas Bellinger 	struct se_device *dev,
2434c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl,
2435c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg,
24366c3c9baaSNicholas Bellinger 	int explicit,
24376c3c9baaSNicholas Bellinger 	int unreg)
2438c66ac9dbSNicholas Bellinger {
24399ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
24404db6dfe6SChaitanya Kulkarni 	char i_buf[PR_REG_ISID_ID_LEN] = { };
24416c3c9baaSNicholas Bellinger 	int pr_res_type = 0, pr_res_scope = 0;
2442c66ac9dbSNicholas Bellinger 
2443618baaf7SBart Van Assche 	lockdep_assert_held(&dev->dev_reservation_lock);
2444618baaf7SBart Van Assche 
2445d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2446c66ac9dbSNicholas Bellinger 	/*
2447c66ac9dbSNicholas Bellinger 	 * Go ahead and release the current PR reservation holder.
24486c3c9baaSNicholas Bellinger 	 * If an All Registrants reservation is currently active and
24496c3c9baaSNicholas Bellinger 	 * a unregister operation is requested, replace the current
24506c3c9baaSNicholas Bellinger 	 * dev_pr_res_holder with another active registration.
2451c66ac9dbSNicholas Bellinger 	 */
24526c3c9baaSNicholas Bellinger 	if (dev->dev_pr_res_holder) {
24536c3c9baaSNicholas Bellinger 		pr_res_type = dev->dev_pr_res_holder->pr_res_type;
24546c3c9baaSNicholas Bellinger 		pr_res_scope = dev->dev_pr_res_holder->pr_res_scope;
24556c3c9baaSNicholas Bellinger 		dev->dev_pr_res_holder->pr_res_type = 0;
24566c3c9baaSNicholas Bellinger 		dev->dev_pr_res_holder->pr_res_scope = 0;
24576c3c9baaSNicholas Bellinger 		dev->dev_pr_res_holder->pr_res_holder = 0;
2458c66ac9dbSNicholas Bellinger 		dev->dev_pr_res_holder = NULL;
24596c3c9baaSNicholas Bellinger 	}
24606c3c9baaSNicholas Bellinger 	if (!unreg)
24616c3c9baaSNicholas Bellinger 		goto out;
2462c66ac9dbSNicholas Bellinger 
24636c3c9baaSNicholas Bellinger 	spin_lock(&dev->t10_pr.registration_lock);
24646c3c9baaSNicholas Bellinger 	list_del_init(&pr_reg->pr_reg_list);
24656c3c9baaSNicholas Bellinger 	/*
24666c3c9baaSNicholas Bellinger 	 * If the I_T nexus is a reservation holder, the persistent reservation
24676c3c9baaSNicholas Bellinger 	 * is of an all registrants type, and the I_T nexus is the last remaining
24686c3c9baaSNicholas Bellinger 	 * registered I_T nexus, then the device server shall also release the
24696c3c9baaSNicholas Bellinger 	 * persistent reservation.
24706c3c9baaSNicholas Bellinger 	 */
24716c3c9baaSNicholas Bellinger 	if (!list_empty(&dev->t10_pr.registration_list) &&
24726c3c9baaSNicholas Bellinger 	    ((pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
24736c3c9baaSNicholas Bellinger 	     (pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) {
24746c3c9baaSNicholas Bellinger 		dev->dev_pr_res_holder =
24756c3c9baaSNicholas Bellinger 			list_entry(dev->t10_pr.registration_list.next,
24766c3c9baaSNicholas Bellinger 				   struct t10_pr_registration, pr_reg_list);
24776c3c9baaSNicholas Bellinger 		dev->dev_pr_res_holder->pr_res_type = pr_res_type;
24786c3c9baaSNicholas Bellinger 		dev->dev_pr_res_holder->pr_res_scope = pr_res_scope;
24796c3c9baaSNicholas Bellinger 		dev->dev_pr_res_holder->pr_res_holder = 1;
24806c3c9baaSNicholas Bellinger 	}
24816c3c9baaSNicholas Bellinger 	spin_unlock(&dev->t10_pr.registration_lock);
24826c3c9baaSNicholas Bellinger out:
24836c3c9baaSNicholas Bellinger 	if (!dev->dev_pr_res_holder) {
24846708bb27SAndy Grover 		pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2485c66ac9dbSNicholas Bellinger 			" reservation holder TYPE: %s ALL_TG_PT: %d\n",
248630c7ca93SDavid Disseldorp 			tfo->fabric_name, (explicit) ? "explicit" :
24876c3c9baaSNicholas Bellinger 			"implicit", core_scsi3_pr_dump_type(pr_res_type),
2488c66ac9dbSNicholas Bellinger 			(pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
24896c3c9baaSNicholas Bellinger 	}
24906708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
249130c7ca93SDavid Disseldorp 		tfo->fabric_name, se_nacl->initiatorname,
2492d2843c17SAndy Grover 		i_buf);
2493c66ac9dbSNicholas Bellinger 	/*
2494c66ac9dbSNicholas Bellinger 	 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2495c66ac9dbSNicholas Bellinger 	 */
2496c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2497c66ac9dbSNicholas Bellinger }
2498c66ac9dbSNicholas Bellinger 
2499de103c93SChristoph Hellwig static sense_reason_t
core_scsi3_emulate_pro_release(struct se_cmd * cmd,int type,int scope,u64 res_key)2500de103c93SChristoph Hellwig core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope,
2501c66ac9dbSNicholas Bellinger 		u64 res_key)
2502c66ac9dbSNicholas Bellinger {
2503c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
2504e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
2505e3d6f909SAndy Grover 	struct se_lun *se_lun = cmd->se_lun;
2506c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
25070fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
2508de103c93SChristoph Hellwig 	sense_reason_t ret = 0;
2509c66ac9dbSNicholas Bellinger 
25106708bb27SAndy Grover 	if (!se_sess || !se_lun) {
25116708bb27SAndy Grover 		pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2512de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2513c66ac9dbSNicholas Bellinger 	}
2514c66ac9dbSNicholas Bellinger 	/*
2515c66ac9dbSNicholas Bellinger 	 * Locate the existing *pr_reg via struct se_node_acl pointers
2516c66ac9dbSNicholas Bellinger 	 */
2517c66ac9dbSNicholas Bellinger 	pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
25186708bb27SAndy Grover 	if (!pr_reg) {
25196708bb27SAndy Grover 		pr_err("SPC-3 PR: Unable to locate"
2520c66ac9dbSNicholas Bellinger 			" PR_REGISTERED *pr_reg for RELEASE\n");
2521de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2522c66ac9dbSNicholas Bellinger 	}
2523c66ac9dbSNicholas Bellinger 	/*
2524c66ac9dbSNicholas Bellinger 	 * From spc4r17 Section 5.7.11.2 Releasing:
2525c66ac9dbSNicholas Bellinger 	 *
2526c66ac9dbSNicholas Bellinger 	 * If there is no persistent reservation or in response to a persistent
2527c66ac9dbSNicholas Bellinger 	 * reservation release request from a registered I_T nexus that is not a
2528c66ac9dbSNicholas Bellinger 	 * persistent reservation holder (see 5.7.10), the device server shall
2529c66ac9dbSNicholas Bellinger 	 * do the following:
2530c66ac9dbSNicholas Bellinger 	 *
2531c66ac9dbSNicholas Bellinger 	 *     a) Not release the persistent reservation, if any;
2532c66ac9dbSNicholas Bellinger 	 *     b) Not remove any registrations; and
2533c66ac9dbSNicholas Bellinger 	 *     c) Complete the command with GOOD status.
2534c66ac9dbSNicholas Bellinger 	 */
2535c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
2536c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
25376708bb27SAndy Grover 	if (!pr_res_holder) {
2538c66ac9dbSNicholas Bellinger 		/*
2539c66ac9dbSNicholas Bellinger 		 * No persistent reservation, return GOOD status.
2540c66ac9dbSNicholas Bellinger 		 */
2541c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
2542bb7a8c8eSNicholas Bellinger 		goto out_put_pr_reg;
2543c66ac9dbSNicholas Bellinger 	}
2544c66ac9dbSNicholas Bellinger 
2545e673dc92SIlias Tsitsimpis 	if (!is_reservation_holder(pr_res_holder, pr_reg)) {
2546c66ac9dbSNicholas Bellinger 		/*
2547c66ac9dbSNicholas Bellinger 		 * Release request from a registered I_T nexus that is not a
2548c66ac9dbSNicholas Bellinger 		 * persistent reservation holder. return GOOD status.
2549c66ac9dbSNicholas Bellinger 		 */
2550c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
2551de103c93SChristoph Hellwig 		goto out_put_pr_reg;
2552c66ac9dbSNicholas Bellinger 	}
2553de103c93SChristoph Hellwig 
2554c66ac9dbSNicholas Bellinger 	/*
2555c66ac9dbSNicholas Bellinger 	 * From spc4r17 Section 5.7.11.2 Releasing:
2556c66ac9dbSNicholas Bellinger 	 *
2557c66ac9dbSNicholas Bellinger 	 * Only the persistent reservation holder (see 5.7.10) is allowed to
2558c66ac9dbSNicholas Bellinger 	 * release a persistent reservation.
2559c66ac9dbSNicholas Bellinger 	 *
2560c66ac9dbSNicholas Bellinger 	 * An application client releases the persistent reservation by issuing
2561c66ac9dbSNicholas Bellinger 	 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2562c66ac9dbSNicholas Bellinger 	 * an I_T nexus that is a persistent reservation holder with the
2563c66ac9dbSNicholas Bellinger 	 * following parameters:
2564c66ac9dbSNicholas Bellinger 	 *
2565c66ac9dbSNicholas Bellinger 	 *     a) RESERVATION KEY field set to the value of the reservation key
2566c66ac9dbSNicholas Bellinger 	 *	  that is registered with the logical unit for the I_T nexus;
2567c66ac9dbSNicholas Bellinger 	 */
2568c66ac9dbSNicholas Bellinger 	if (res_key != pr_reg->pr_res_key) {
25696708bb27SAndy Grover 		pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2570c66ac9dbSNicholas Bellinger 			" does not match existing SA REGISTER res_key:"
2571c66ac9dbSNicholas Bellinger 			" 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2572c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
2573de103c93SChristoph Hellwig 		ret = TCM_RESERVATION_CONFLICT;
2574de103c93SChristoph Hellwig 		goto out_put_pr_reg;
2575c66ac9dbSNicholas Bellinger 	}
2576c66ac9dbSNicholas Bellinger 	/*
2577c66ac9dbSNicholas Bellinger 	 * From spc4r17 Section 5.7.11.2 Releasing and above:
2578c66ac9dbSNicholas Bellinger 	 *
2579c66ac9dbSNicholas Bellinger 	 * b) TYPE field and SCOPE field set to match the persistent
2580c66ac9dbSNicholas Bellinger 	 *    reservation being released.
2581c66ac9dbSNicholas Bellinger 	 */
2582c66ac9dbSNicholas Bellinger 	if ((pr_res_holder->pr_res_type != type) ||
2583c66ac9dbSNicholas Bellinger 	    (pr_res_holder->pr_res_scope != scope)) {
2584c66ac9dbSNicholas Bellinger 		struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
25856708bb27SAndy Grover 		pr_err("SPC-3 PR RELEASE: Attempted to release"
2586c66ac9dbSNicholas Bellinger 			" reservation from [%s]: %s with different TYPE "
2587c66ac9dbSNicholas Bellinger 			"and/or SCOPE  while reservation already held by"
2588c66ac9dbSNicholas Bellinger 			" [%s]: %s, returning RESERVATION_CONFLICT\n",
258930c7ca93SDavid Disseldorp 			cmd->se_tfo->fabric_name,
2590c66ac9dbSNicholas Bellinger 			se_sess->se_node_acl->initiatorname,
259130c7ca93SDavid Disseldorp 			pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
2592c66ac9dbSNicholas Bellinger 			pr_res_holder->pr_reg_nacl->initiatorname);
2593c66ac9dbSNicholas Bellinger 
2594c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
2595de103c93SChristoph Hellwig 		ret = TCM_RESERVATION_CONFLICT;
2596de103c93SChristoph Hellwig 		goto out_put_pr_reg;
2597c66ac9dbSNicholas Bellinger 	}
2598c66ac9dbSNicholas Bellinger 	/*
2599c66ac9dbSNicholas Bellinger 	 * In response to a persistent reservation release request from the
2600c66ac9dbSNicholas Bellinger 	 * persistent reservation holder the device server shall perform a
2601c66ac9dbSNicholas Bellinger 	 * release by doing the following as an uninterrupted series of actions:
2602c66ac9dbSNicholas Bellinger 	 * a) Release the persistent reservation;
2603c66ac9dbSNicholas Bellinger 	 * b) Not remove any registration(s);
2604c66ac9dbSNicholas Bellinger 	 * c) If the released persistent reservation is a registrants only type
2605c66ac9dbSNicholas Bellinger 	 * or all registrants type persistent reservation,
2606c66ac9dbSNicholas Bellinger 	 *    the device server shall establish a unit attention condition for
2607c66ac9dbSNicholas Bellinger 	 *    the initiator port associated with every regis-
2608c66ac9dbSNicholas Bellinger 	 *    tered I_T nexus other than I_T nexus on which the PERSISTENT
2609c66ac9dbSNicholas Bellinger 	 *    RESERVE OUT command with RELEASE service action was received,
2610c66ac9dbSNicholas Bellinger 	 *    with the additional sense code set to RESERVATIONS RELEASED; and
2611c66ac9dbSNicholas Bellinger 	 * d) If the persistent reservation is of any other type, the device
2612c66ac9dbSNicholas Bellinger 	 *    server shall not establish a unit attention condition.
2613c66ac9dbSNicholas Bellinger 	 */
2614c66ac9dbSNicholas Bellinger 	__core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
26156c3c9baaSNicholas Bellinger 					  pr_reg, 1, 0);
2616c66ac9dbSNicholas Bellinger 
2617c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
2618c66ac9dbSNicholas Bellinger 
2619c66ac9dbSNicholas Bellinger 	if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2620c66ac9dbSNicholas Bellinger 	    (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2621c66ac9dbSNicholas Bellinger 	    (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2622c66ac9dbSNicholas Bellinger 	    (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2623c66ac9dbSNicholas Bellinger 		/*
2624c66ac9dbSNicholas Bellinger 		 * If no UNIT ATTENTION conditions will be established for
2625c66ac9dbSNicholas Bellinger 		 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2626c66ac9dbSNicholas Bellinger 		 * go ahead and check for APTPL=1 update+write below
2627c66ac9dbSNicholas Bellinger 		 */
2628c66ac9dbSNicholas Bellinger 		goto write_aptpl;
2629c66ac9dbSNicholas Bellinger 	}
2630c66ac9dbSNicholas Bellinger 
2631c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
2632c66ac9dbSNicholas Bellinger 	list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2633c66ac9dbSNicholas Bellinger 			pr_reg_list) {
2634c66ac9dbSNicholas Bellinger 		/*
2635c66ac9dbSNicholas Bellinger 		 * Do not establish a UNIT ATTENTION condition
2636c66ac9dbSNicholas Bellinger 		 * for the calling I_T Nexus
2637c66ac9dbSNicholas Bellinger 		 */
2638c66ac9dbSNicholas Bellinger 		if (pr_reg_p == pr_reg)
2639c66ac9dbSNicholas Bellinger 			continue;
2640c66ac9dbSNicholas Bellinger 
2641c51c8e7bSHannes Reinecke 		target_ua_allocate_lun(pr_reg_p->pr_reg_nacl,
2642c66ac9dbSNicholas Bellinger 				pr_reg_p->pr_res_mapped_lun,
2643c66ac9dbSNicholas Bellinger 				0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2644c66ac9dbSNicholas Bellinger 	}
2645c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
2646c66ac9dbSNicholas Bellinger 
2647c66ac9dbSNicholas Bellinger write_aptpl:
2648459f213bSAndy Grover 	if (pr_tmpl->pr_aptpl_active)
2649459f213bSAndy Grover 		core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2650459f213bSAndy Grover 
2651de103c93SChristoph Hellwig out_put_pr_reg:
2652c66ac9dbSNicholas Bellinger 	core_scsi3_put_pr_reg(pr_reg);
2653de103c93SChristoph Hellwig 	return ret;
2654c66ac9dbSNicholas Bellinger }
2655c66ac9dbSNicholas Bellinger 
2656de103c93SChristoph Hellwig static sense_reason_t
core_scsi3_emulate_pro_clear(struct se_cmd * cmd,u64 res_key)2657de103c93SChristoph Hellwig core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key)
2658c66ac9dbSNicholas Bellinger {
2659c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
2660c66ac9dbSNicholas Bellinger 	struct se_node_acl *pr_reg_nacl;
2661e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
26620fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
2663c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2664f2d30680SHannes Reinecke 	u64 pr_res_mapped_lun = 0;
2665c66ac9dbSNicholas Bellinger 	int calling_it_nexus = 0;
2666c66ac9dbSNicholas Bellinger 	/*
2667c66ac9dbSNicholas Bellinger 	 * Locate the existing *pr_reg via struct se_node_acl pointers
2668c66ac9dbSNicholas Bellinger 	 */
26695951146dSAndy Grover 	pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
2670c66ac9dbSNicholas Bellinger 			se_sess->se_node_acl, se_sess);
26716708bb27SAndy Grover 	if (!pr_reg_n) {
26726708bb27SAndy Grover 		pr_err("SPC-3 PR: Unable to locate"
2673c66ac9dbSNicholas Bellinger 			" PR_REGISTERED *pr_reg for CLEAR\n");
2674de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2675c66ac9dbSNicholas Bellinger 	}
2676c66ac9dbSNicholas Bellinger 	/*
2677c66ac9dbSNicholas Bellinger 	 * From spc4r17 section 5.7.11.6, Clearing:
2678c66ac9dbSNicholas Bellinger 	 *
2679c66ac9dbSNicholas Bellinger 	 * Any application client may release the persistent reservation and
2680c66ac9dbSNicholas Bellinger 	 * remove all registrations from a device server by issuing a
2681c66ac9dbSNicholas Bellinger 	 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2682c66ac9dbSNicholas Bellinger 	 * registered I_T nexus with the following parameter:
2683c66ac9dbSNicholas Bellinger 	 *
2684c66ac9dbSNicholas Bellinger 	 *	a) RESERVATION KEY field set to the value of the reservation key
2685c66ac9dbSNicholas Bellinger 	 * 	   that is registered with the logical unit for the I_T nexus.
2686c66ac9dbSNicholas Bellinger 	 */
2687c66ac9dbSNicholas Bellinger 	if (res_key != pr_reg_n->pr_res_key) {
26886708bb27SAndy Grover 		pr_err("SPC-3 PR REGISTER: Received"
2689c66ac9dbSNicholas Bellinger 			" res_key: 0x%016Lx does not match"
2690c66ac9dbSNicholas Bellinger 			" existing SA REGISTER res_key:"
2691c66ac9dbSNicholas Bellinger 			" 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2692c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg_n);
2693de103c93SChristoph Hellwig 		return TCM_RESERVATION_CONFLICT;
2694c66ac9dbSNicholas Bellinger 	}
2695c66ac9dbSNicholas Bellinger 	/*
2696c66ac9dbSNicholas Bellinger 	 * a) Release the persistent reservation, if any;
2697c66ac9dbSNicholas Bellinger 	 */
2698c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
2699c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
2700c66ac9dbSNicholas Bellinger 	if (pr_res_holder) {
2701c66ac9dbSNicholas Bellinger 		struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2702c66ac9dbSNicholas Bellinger 		__core_scsi3_complete_pro_release(dev, pr_res_nacl,
27036c3c9baaSNicholas Bellinger 						  pr_res_holder, 0, 0);
2704c66ac9dbSNicholas Bellinger 	}
2705c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
2706c66ac9dbSNicholas Bellinger 	/*
2707c66ac9dbSNicholas Bellinger 	 * b) Remove all registration(s) (see spc4r17 5.7.7);
2708c66ac9dbSNicholas Bellinger 	 */
2709c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
2710c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2711c66ac9dbSNicholas Bellinger 			&pr_tmpl->registration_list, pr_reg_list) {
2712c66ac9dbSNicholas Bellinger 
2713c66ac9dbSNicholas Bellinger 		calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2714c66ac9dbSNicholas Bellinger 		pr_reg_nacl = pr_reg->pr_reg_nacl;
2715c66ac9dbSNicholas Bellinger 		pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2716c66ac9dbSNicholas Bellinger 		__core_scsi3_free_registration(dev, pr_reg, NULL,
2717c66ac9dbSNicholas Bellinger 					calling_it_nexus);
2718c66ac9dbSNicholas Bellinger 		/*
2719c66ac9dbSNicholas Bellinger 		 * e) Establish a unit attention condition for the initiator
2720c66ac9dbSNicholas Bellinger 		 *    port associated with every registered I_T nexus other
2721c66ac9dbSNicholas Bellinger 		 *    than the I_T nexus on which the PERSISTENT RESERVE OUT
2722c66ac9dbSNicholas Bellinger 		 *    command with CLEAR service action was received, with the
2723c66ac9dbSNicholas Bellinger 		 *    additional sense code set to RESERVATIONS PREEMPTED.
2724c66ac9dbSNicholas Bellinger 		 */
27256708bb27SAndy Grover 		if (!calling_it_nexus)
2726c51c8e7bSHannes Reinecke 			target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun,
2727c66ac9dbSNicholas Bellinger 				0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2728c66ac9dbSNicholas Bellinger 	}
2729c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
2730c66ac9dbSNicholas Bellinger 
27316708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
273230c7ca93SDavid Disseldorp 		cmd->se_tfo->fabric_name);
2733c66ac9dbSNicholas Bellinger 
2734459f213bSAndy Grover 	core_scsi3_update_and_write_aptpl(cmd->se_dev, false);
2735c66ac9dbSNicholas Bellinger 
2736c66ac9dbSNicholas Bellinger 	core_scsi3_pr_generation(dev);
2737c66ac9dbSNicholas Bellinger 	return 0;
2738c66ac9dbSNicholas Bellinger }
2739c66ac9dbSNicholas Bellinger 
__core_scsi3_complete_pro_preempt(struct se_device * dev,struct t10_pr_registration * pr_reg,struct list_head * preempt_and_abort_list,int type,int scope,enum preempt_type preempt_type)2740c66ac9dbSNicholas Bellinger static void __core_scsi3_complete_pro_preempt(
2741c66ac9dbSNicholas Bellinger 	struct se_device *dev,
2742c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg,
2743c66ac9dbSNicholas Bellinger 	struct list_head *preempt_and_abort_list,
2744c66ac9dbSNicholas Bellinger 	int type,
2745c66ac9dbSNicholas Bellinger 	int scope,
274633ce6a87SAndy Grover 	enum preempt_type preempt_type)
2747c66ac9dbSNicholas Bellinger {
2748c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
27499ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
27504db6dfe6SChaitanya Kulkarni 	char i_buf[PR_REG_ISID_ID_LEN] = { };
2751c66ac9dbSNicholas Bellinger 
2752618baaf7SBart Van Assche 	lockdep_assert_held(&dev->dev_reservation_lock);
2753618baaf7SBart Van Assche 
2754d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2755c66ac9dbSNicholas Bellinger 	/*
2756125d0119SHannes Reinecke 	 * Do an implicit RELEASE of the existing reservation.
2757c66ac9dbSNicholas Bellinger 	 */
2758c66ac9dbSNicholas Bellinger 	if (dev->dev_pr_res_holder)
2759c66ac9dbSNicholas Bellinger 		__core_scsi3_complete_pro_release(dev, nacl,
27606c3c9baaSNicholas Bellinger 						  dev->dev_pr_res_holder, 0, 0);
2761c66ac9dbSNicholas Bellinger 
2762c66ac9dbSNicholas Bellinger 	dev->dev_pr_res_holder = pr_reg;
2763c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_holder = 1;
2764c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_type = type;
2765c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_scope = scope;
2766c66ac9dbSNicholas Bellinger 
27676708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2768c66ac9dbSNicholas Bellinger 		" reservation holder TYPE: %s ALL_TG_PT: %d\n",
276930c7ca93SDavid Disseldorp 		tfo->fabric_name, (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
2770c66ac9dbSNicholas Bellinger 		core_scsi3_pr_dump_type(type),
2771c66ac9dbSNicholas Bellinger 		(pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
27726708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
277330c7ca93SDavid Disseldorp 		tfo->fabric_name, (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
2774d2843c17SAndy Grover 		nacl->initiatorname, i_buf);
2775c66ac9dbSNicholas Bellinger 	/*
2776c66ac9dbSNicholas Bellinger 	 * For PREEMPT_AND_ABORT, add the preempting reservation's
2777c66ac9dbSNicholas Bellinger 	 * struct t10_pr_registration to the list that will be compared
2778c66ac9dbSNicholas Bellinger 	 * against received CDBs..
2779c66ac9dbSNicholas Bellinger 	 */
2780c66ac9dbSNicholas Bellinger 	if (preempt_and_abort_list)
2781c66ac9dbSNicholas Bellinger 		list_add_tail(&pr_reg->pr_reg_abort_list,
2782c66ac9dbSNicholas Bellinger 				preempt_and_abort_list);
2783c66ac9dbSNicholas Bellinger }
2784c66ac9dbSNicholas Bellinger 
core_scsi3_release_preempt_and_abort(struct list_head * preempt_and_abort_list,struct t10_pr_registration * pr_reg_holder)2785c66ac9dbSNicholas Bellinger static void core_scsi3_release_preempt_and_abort(
2786c66ac9dbSNicholas Bellinger 	struct list_head *preempt_and_abort_list,
2787c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg_holder)
2788c66ac9dbSNicholas Bellinger {
2789c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2790c66ac9dbSNicholas Bellinger 
2791c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2792c66ac9dbSNicholas Bellinger 				pr_reg_abort_list) {
2793c66ac9dbSNicholas Bellinger 
2794c66ac9dbSNicholas Bellinger 		list_del(&pr_reg->pr_reg_abort_list);
2795c66ac9dbSNicholas Bellinger 		if (pr_reg_holder == pr_reg)
2796c66ac9dbSNicholas Bellinger 			continue;
2797c66ac9dbSNicholas Bellinger 		if (pr_reg->pr_res_holder) {
27986708bb27SAndy Grover 			pr_warn("pr_reg->pr_res_holder still set\n");
2799c66ac9dbSNicholas Bellinger 			continue;
2800c66ac9dbSNicholas Bellinger 		}
2801c66ac9dbSNicholas Bellinger 
2802c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_deve = NULL;
2803c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_nacl = NULL;
2804c66ac9dbSNicholas Bellinger 		kmem_cache_free(t10_pr_reg_cache, pr_reg);
2805c66ac9dbSNicholas Bellinger 	}
2806c66ac9dbSNicholas Bellinger }
2807c66ac9dbSNicholas Bellinger 
2808de103c93SChristoph Hellwig static sense_reason_t
core_scsi3_pro_preempt(struct se_cmd * cmd,int type,int scope,u64 res_key,u64 sa_res_key,enum preempt_type preempt_type)2809de103c93SChristoph Hellwig core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key,
281033ce6a87SAndy Grover 		u64 sa_res_key, enum preempt_type preempt_type)
2811c66ac9dbSNicholas Bellinger {
28125951146dSAndy Grover 	struct se_device *dev = cmd->se_dev;
2813c66ac9dbSNicholas Bellinger 	struct se_node_acl *pr_reg_nacl;
2814e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
2815d0f474e5SRoland Dreier 	LIST_HEAD(preempt_and_abort_list);
2816c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
28170fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
2818f2d30680SHannes Reinecke 	u64 pr_res_mapped_lun = 0;
2819b6932ee3SSteven Allen 	int all_reg = 0, calling_it_nexus = 0;
2820b6932ee3SSteven Allen 	bool sa_res_key_unmatched = sa_res_key != 0;
2821de103c93SChristoph Hellwig 	int prh_type = 0, prh_scope = 0;
2822c66ac9dbSNicholas Bellinger 
2823de103c93SChristoph Hellwig 	if (!se_sess)
2824de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2825c66ac9dbSNicholas Bellinger 
28265951146dSAndy Grover 	pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2827c66ac9dbSNicholas Bellinger 				se_sess);
28286708bb27SAndy Grover 	if (!pr_reg_n) {
28296708bb27SAndy Grover 		pr_err("SPC-3 PR: Unable to locate"
2830c66ac9dbSNicholas Bellinger 			" PR_REGISTERED *pr_reg for PREEMPT%s\n",
283133ce6a87SAndy Grover 			(preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "");
2832de103c93SChristoph Hellwig 		return TCM_RESERVATION_CONFLICT;
2833c66ac9dbSNicholas Bellinger 	}
2834c66ac9dbSNicholas Bellinger 	if (pr_reg_n->pr_res_key != res_key) {
2835c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg_n);
2836de103c93SChristoph Hellwig 		return TCM_RESERVATION_CONFLICT;
2837c66ac9dbSNicholas Bellinger 	}
2838c66ac9dbSNicholas Bellinger 	if (scope != PR_SCOPE_LU_SCOPE) {
28396708bb27SAndy Grover 		pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2840c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg_n);
2841de103c93SChristoph Hellwig 		return TCM_INVALID_PARAMETER_LIST;
2842c66ac9dbSNicholas Bellinger 	}
2843c66ac9dbSNicholas Bellinger 
2844c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
2845c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
2846c66ac9dbSNicholas Bellinger 	if (pr_res_holder &&
2847c66ac9dbSNicholas Bellinger 	   ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2848c66ac9dbSNicholas Bellinger 	    (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
2849c66ac9dbSNicholas Bellinger 		all_reg = 1;
2850c66ac9dbSNicholas Bellinger 
28516708bb27SAndy Grover 	if (!all_reg && !sa_res_key) {
2852c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
2853c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg_n);
2854de103c93SChristoph Hellwig 		return TCM_INVALID_PARAMETER_LIST;
2855c66ac9dbSNicholas Bellinger 	}
2856c66ac9dbSNicholas Bellinger 	/*
2857c66ac9dbSNicholas Bellinger 	 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
2858c66ac9dbSNicholas Bellinger 	 *
2859c66ac9dbSNicholas Bellinger 	 * If the SERVICE ACTION RESERVATION KEY field does not identify a
2860c66ac9dbSNicholas Bellinger 	 * persistent reservation holder or there is no persistent reservation
2861c66ac9dbSNicholas Bellinger 	 * holder (i.e., there is no persistent reservation), then the device
2862c66ac9dbSNicholas Bellinger 	 * server shall perform a preempt by doing the following in an
2863c66ac9dbSNicholas Bellinger 	 * uninterrupted series of actions. (See below..)
2864c66ac9dbSNicholas Bellinger 	 */
28656708bb27SAndy Grover 	if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
2866c66ac9dbSNicholas Bellinger 		/*
2867c66ac9dbSNicholas Bellinger 		 * No existing or SA Reservation Key matching reservations..
2868c66ac9dbSNicholas Bellinger 		 *
2869c66ac9dbSNicholas Bellinger 		 * PROUT SA PREEMPT with All Registrant type reservations are
2870c66ac9dbSNicholas Bellinger 		 * allowed to be processed without a matching SA Reservation Key
2871c66ac9dbSNicholas Bellinger 		 */
2872c66ac9dbSNicholas Bellinger 		spin_lock(&pr_tmpl->registration_lock);
2873c66ac9dbSNicholas Bellinger 		list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2874c66ac9dbSNicholas Bellinger 				&pr_tmpl->registration_list, pr_reg_list) {
2875c66ac9dbSNicholas Bellinger 			/*
2876c66ac9dbSNicholas Bellinger 			 * Removing of registrations in non all registrants
2877c66ac9dbSNicholas Bellinger 			 * type reservations without a matching SA reservation
2878c66ac9dbSNicholas Bellinger 			 * key.
2879c66ac9dbSNicholas Bellinger 			 *
2880c66ac9dbSNicholas Bellinger 			 * a) Remove the registrations for all I_T nexuses
2881c66ac9dbSNicholas Bellinger 			 *    specified by the SERVICE ACTION RESERVATION KEY
2882c66ac9dbSNicholas Bellinger 			 *    field;
2883c66ac9dbSNicholas Bellinger 			 * b) Ignore the contents of the SCOPE and TYPE fields;
2884c66ac9dbSNicholas Bellinger 			 * c) Process tasks as defined in 5.7.1; and
2885c66ac9dbSNicholas Bellinger 			 * d) Establish a unit attention condition for the
2886c66ac9dbSNicholas Bellinger 			 *    initiator port associated with every I_T nexus
2887c66ac9dbSNicholas Bellinger 			 *    that lost its registration other than the I_T
2888c66ac9dbSNicholas Bellinger 			 *    nexus on which the PERSISTENT RESERVE OUT command
2889c66ac9dbSNicholas Bellinger 			 *    was received, with the additional sense code set
2890c66ac9dbSNicholas Bellinger 			 *    to REGISTRATIONS PREEMPTED.
2891c66ac9dbSNicholas Bellinger 			 */
28926708bb27SAndy Grover 			if (!all_reg) {
2893c66ac9dbSNicholas Bellinger 				if (pr_reg->pr_res_key != sa_res_key)
2894c66ac9dbSNicholas Bellinger 					continue;
2895b6932ee3SSteven Allen 				sa_res_key_unmatched = false;
2896c66ac9dbSNicholas Bellinger 
2897c66ac9dbSNicholas Bellinger 				calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2898c66ac9dbSNicholas Bellinger 				pr_reg_nacl = pr_reg->pr_reg_nacl;
2899c66ac9dbSNicholas Bellinger 				pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2900c66ac9dbSNicholas Bellinger 				__core_scsi3_free_registration(dev, pr_reg,
290133ce6a87SAndy Grover 					(preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
2902c66ac9dbSNicholas Bellinger 						NULL, calling_it_nexus);
2903c66ac9dbSNicholas Bellinger 			} else {
2904c66ac9dbSNicholas Bellinger 				/*
2905c66ac9dbSNicholas Bellinger 				 * Case for any existing all registrants type
2906c66ac9dbSNicholas Bellinger 				 * reservation, follow logic in spc4r17 section
2907c66ac9dbSNicholas Bellinger 				 * 5.7.11.4 Preempting, Table 52 and Figure 7.
2908c66ac9dbSNicholas Bellinger 				 *
2909c66ac9dbSNicholas Bellinger 				 * For a ZERO SA Reservation key, release
2910125d0119SHannes Reinecke 				 * all other registrations and do an implicit
2911c66ac9dbSNicholas Bellinger 				 * release of active persistent reservation.
2912c66ac9dbSNicholas Bellinger 				 *
2913c66ac9dbSNicholas Bellinger 				 * For a non-ZERO SA Reservation key, only
2914c66ac9dbSNicholas Bellinger 				 * release the matching reservation key from
2915c66ac9dbSNicholas Bellinger 				 * registrations.
2916c66ac9dbSNicholas Bellinger 				 */
2917c66ac9dbSNicholas Bellinger 				if ((sa_res_key) &&
2918c66ac9dbSNicholas Bellinger 				     (pr_reg->pr_res_key != sa_res_key))
2919c66ac9dbSNicholas Bellinger 					continue;
2920b6932ee3SSteven Allen 				sa_res_key_unmatched = false;
2921c66ac9dbSNicholas Bellinger 
2922c66ac9dbSNicholas Bellinger 				calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2923c66ac9dbSNicholas Bellinger 				if (calling_it_nexus)
2924c66ac9dbSNicholas Bellinger 					continue;
2925c66ac9dbSNicholas Bellinger 
2926c66ac9dbSNicholas Bellinger 				pr_reg_nacl = pr_reg->pr_reg_nacl;
2927c66ac9dbSNicholas Bellinger 				pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2928c66ac9dbSNicholas Bellinger 				__core_scsi3_free_registration(dev, pr_reg,
292933ce6a87SAndy Grover 					(preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
2930c66ac9dbSNicholas Bellinger 						NULL, 0);
2931c66ac9dbSNicholas Bellinger 			}
29326708bb27SAndy Grover 			if (!calling_it_nexus)
2933c51c8e7bSHannes Reinecke 				target_ua_allocate_lun(pr_reg_nacl,
2934c66ac9dbSNicholas Bellinger 					pr_res_mapped_lun, 0x2A,
29359e08e34eSMarco Sanvido 					ASCQ_2AH_REGISTRATIONS_PREEMPTED);
2936c66ac9dbSNicholas Bellinger 		}
2937c66ac9dbSNicholas Bellinger 		spin_unlock(&pr_tmpl->registration_lock);
2938c66ac9dbSNicholas Bellinger 		/*
2939c66ac9dbSNicholas Bellinger 		 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
2940c66ac9dbSNicholas Bellinger 		 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
2941c66ac9dbSNicholas Bellinger 		 * RESERVATION KEY field to a value that does not match any
2942c66ac9dbSNicholas Bellinger 		 * registered reservation key, then the device server shall
2943c66ac9dbSNicholas Bellinger 		 * complete the command with RESERVATION CONFLICT status.
2944c66ac9dbSNicholas Bellinger 		 */
2945b6932ee3SSteven Allen 		if (sa_res_key_unmatched) {
2946c66ac9dbSNicholas Bellinger 			spin_unlock(&dev->dev_reservation_lock);
2947c66ac9dbSNicholas Bellinger 			core_scsi3_put_pr_reg(pr_reg_n);
2948de103c93SChristoph Hellwig 			return TCM_RESERVATION_CONFLICT;
2949c66ac9dbSNicholas Bellinger 		}
2950c66ac9dbSNicholas Bellinger 		/*
2951c66ac9dbSNicholas Bellinger 		 * For an existing all registrants type reservation
2952c66ac9dbSNicholas Bellinger 		 * with a zero SA rservation key, preempt the existing
2953c66ac9dbSNicholas Bellinger 		 * reservation with the new PR type and scope.
2954c66ac9dbSNicholas Bellinger 		 */
2955c66ac9dbSNicholas Bellinger 		if (pr_res_holder && all_reg && !(sa_res_key)) {
2956c66ac9dbSNicholas Bellinger 			__core_scsi3_complete_pro_preempt(dev, pr_reg_n,
295733ce6a87SAndy Grover 				(preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
295833ce6a87SAndy Grover 				type, scope, preempt_type);
2959f050a7c6SDmitry Bogdanov 		}
2960f050a7c6SDmitry Bogdanov 
2961f050a7c6SDmitry Bogdanov 		spin_unlock(&dev->dev_reservation_lock);
2962c66ac9dbSNicholas Bellinger 
296349790e6aSDmitry Bogdanov 		/*
296449790e6aSDmitry Bogdanov 		 * SPC-4 5.12.11.2.6 Preempting and aborting
296549790e6aSDmitry Bogdanov 		 * The actions described in this subclause shall be performed
296649790e6aSDmitry Bogdanov 		 * for all I_T nexuses that are registered with the non-zero
296749790e6aSDmitry Bogdanov 		 * SERVICE ACTION RESERVATION KEY value, without regard for
296849790e6aSDmitry Bogdanov 		 * whether the preempted I_T nexuses hold the persistent
296949790e6aSDmitry Bogdanov 		 * reservation. If the SERVICE ACTION RESERVATION KEY field is
297049790e6aSDmitry Bogdanov 		 * set to zero and an all registrants persistent reservation is
297149790e6aSDmitry Bogdanov 		 * present, the device server shall abort all commands for all
297249790e6aSDmitry Bogdanov 		 * registered I_T nexuses.
297349790e6aSDmitry Bogdanov 		 */
297449790e6aSDmitry Bogdanov 		if (preempt_type == PREEMPT_AND_ABORT) {
297549790e6aSDmitry Bogdanov 			core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list,
297649790e6aSDmitry Bogdanov 					   cmd);
2977c66ac9dbSNicholas Bellinger 			core_scsi3_release_preempt_and_abort(
2978c66ac9dbSNicholas Bellinger 				&preempt_and_abort_list, pr_reg_n);
297949790e6aSDmitry Bogdanov 		}
2980c66ac9dbSNicholas Bellinger 
2981459f213bSAndy Grover 		if (pr_tmpl->pr_aptpl_active)
2982459f213bSAndy Grover 			core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2983c66ac9dbSNicholas Bellinger 
2984c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg_n);
29855951146dSAndy Grover 		core_scsi3_pr_generation(cmd->se_dev);
2986c66ac9dbSNicholas Bellinger 		return 0;
2987c66ac9dbSNicholas Bellinger 	}
2988c66ac9dbSNicholas Bellinger 	/*
2989c66ac9dbSNicholas Bellinger 	 * The PREEMPTing SA reservation key matches that of the
2990c66ac9dbSNicholas Bellinger 	 * existing persistent reservation, first, we check if
2991c66ac9dbSNicholas Bellinger 	 * we are preempting our own reservation.
2992c66ac9dbSNicholas Bellinger 	 * From spc4r17, section 5.7.11.4.3 Preempting
2993c66ac9dbSNicholas Bellinger 	 * persistent reservations and registration handling
2994c66ac9dbSNicholas Bellinger 	 *
2995c66ac9dbSNicholas Bellinger 	 * If an all registrants persistent reservation is not
2996c66ac9dbSNicholas Bellinger 	 * present, it is not an error for the persistent
2997c66ac9dbSNicholas Bellinger 	 * reservation holder to preempt itself (i.e., a
2998c66ac9dbSNicholas Bellinger 	 * PERSISTENT RESERVE OUT with a PREEMPT service action
2999c66ac9dbSNicholas Bellinger 	 * or a PREEMPT AND ABORT service action with the
3000c66ac9dbSNicholas Bellinger 	 * SERVICE ACTION RESERVATION KEY value equal to the
3001c66ac9dbSNicholas Bellinger 	 * persistent reservation holder's reservation key that
3002c66ac9dbSNicholas Bellinger 	 * is received from the persistent reservation holder).
3003c66ac9dbSNicholas Bellinger 	 * In that case, the device server shall establish the
3004c66ac9dbSNicholas Bellinger 	 * new persistent reservation and maintain the
3005c66ac9dbSNicholas Bellinger 	 * registration.
3006c66ac9dbSNicholas Bellinger 	 */
3007c66ac9dbSNicholas Bellinger 	prh_type = pr_res_holder->pr_res_type;
3008c66ac9dbSNicholas Bellinger 	prh_scope = pr_res_holder->pr_res_scope;
3009c66ac9dbSNicholas Bellinger 	/*
3010c66ac9dbSNicholas Bellinger 	 * If the SERVICE ACTION RESERVATION KEY field identifies a
3011c66ac9dbSNicholas Bellinger 	 * persistent reservation holder (see 5.7.10), the device
3012c66ac9dbSNicholas Bellinger 	 * server shall perform a preempt by doing the following as
3013c66ac9dbSNicholas Bellinger 	 * an uninterrupted series of actions:
3014c66ac9dbSNicholas Bellinger 	 *
3015c66ac9dbSNicholas Bellinger 	 * a) Release the persistent reservation for the holder
3016c66ac9dbSNicholas Bellinger 	 *    identified by the SERVICE ACTION RESERVATION KEY field;
3017c66ac9dbSNicholas Bellinger 	 */
3018c66ac9dbSNicholas Bellinger 	if (pr_reg_n != pr_res_holder)
3019c66ac9dbSNicholas Bellinger 		__core_scsi3_complete_pro_release(dev,
3020c66ac9dbSNicholas Bellinger 						  pr_res_holder->pr_reg_nacl,
30216c3c9baaSNicholas Bellinger 						  dev->dev_pr_res_holder, 0, 0);
3022c66ac9dbSNicholas Bellinger 	/*
3023c66ac9dbSNicholas Bellinger 	 * b) Remove the registrations for all I_T nexuses identified
3024c66ac9dbSNicholas Bellinger 	 *    by the SERVICE ACTION RESERVATION KEY field, except the
3025c66ac9dbSNicholas Bellinger 	 *    I_T nexus that is being used for the PERSISTENT RESERVE
3026c66ac9dbSNicholas Bellinger 	 *    OUT command. If an all registrants persistent reservation
3027c66ac9dbSNicholas Bellinger 	 *    is present and the SERVICE ACTION RESERVATION KEY field
3028c66ac9dbSNicholas Bellinger 	 *    is set to zero, then all registrations shall be removed
3029c66ac9dbSNicholas Bellinger 	 *    except for that of the I_T nexus that is being used for
3030c66ac9dbSNicholas Bellinger 	 *    the PERSISTENT RESERVE OUT command;
3031c66ac9dbSNicholas Bellinger 	 */
3032c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
3033c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3034c66ac9dbSNicholas Bellinger 			&pr_tmpl->registration_list, pr_reg_list) {
3035c66ac9dbSNicholas Bellinger 
3036c66ac9dbSNicholas Bellinger 		calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3037c66ac9dbSNicholas Bellinger 		if (calling_it_nexus)
3038c66ac9dbSNicholas Bellinger 			continue;
3039c66ac9dbSNicholas Bellinger 
30401f1009eaSDmitry Bogdanov 		if (sa_res_key && pr_reg->pr_res_key != sa_res_key)
3041c66ac9dbSNicholas Bellinger 			continue;
3042c66ac9dbSNicholas Bellinger 
3043c66ac9dbSNicholas Bellinger 		pr_reg_nacl = pr_reg->pr_reg_nacl;
3044c66ac9dbSNicholas Bellinger 		pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3045c66ac9dbSNicholas Bellinger 		__core_scsi3_free_registration(dev, pr_reg,
304633ce6a87SAndy Grover 				(preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3047c66ac9dbSNicholas Bellinger 				calling_it_nexus);
3048c66ac9dbSNicholas Bellinger 		/*
3049c66ac9dbSNicholas Bellinger 		 * e) Establish a unit attention condition for the initiator
3050c66ac9dbSNicholas Bellinger 		 *    port associated with every I_T nexus that lost its
3051c66ac9dbSNicholas Bellinger 		 *    persistent reservation and/or registration, with the
3052c66ac9dbSNicholas Bellinger 		 *    additional sense code set to REGISTRATIONS PREEMPTED;
3053c66ac9dbSNicholas Bellinger 		 */
3054c51c8e7bSHannes Reinecke 		target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
30559e08e34eSMarco Sanvido 				ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3056c66ac9dbSNicholas Bellinger 	}
3057c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
3058c66ac9dbSNicholas Bellinger 	/*
3059c66ac9dbSNicholas Bellinger 	 * c) Establish a persistent reservation for the preempting
3060c66ac9dbSNicholas Bellinger 	 *    I_T nexus using the contents of the SCOPE and TYPE fields;
3061c66ac9dbSNicholas Bellinger 	 */
3062c66ac9dbSNicholas Bellinger 	__core_scsi3_complete_pro_preempt(dev, pr_reg_n,
306333ce6a87SAndy Grover 			(preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
306433ce6a87SAndy Grover 			type, scope, preempt_type);
3065c66ac9dbSNicholas Bellinger 	/*
3066c66ac9dbSNicholas Bellinger 	 * d) Process tasks as defined in 5.7.1;
3067c66ac9dbSNicholas Bellinger 	 * e) See above..
3068c66ac9dbSNicholas Bellinger 	 * f) If the type or scope has changed, then for every I_T nexus
3069c66ac9dbSNicholas Bellinger 	 *    whose reservation key was not removed, except for the I_T
3070c66ac9dbSNicholas Bellinger 	 *    nexus on which the PERSISTENT RESERVE OUT command was
3071c66ac9dbSNicholas Bellinger 	 *    received, the device server shall establish a unit
3072c66ac9dbSNicholas Bellinger 	 *    attention condition for the initiator port associated with
3073c66ac9dbSNicholas Bellinger 	 *    that I_T nexus, with the additional sense code set to
3074c66ac9dbSNicholas Bellinger 	 *    RESERVATIONS RELEASED. If the type or scope have not
3075c66ac9dbSNicholas Bellinger 	 *    changed, then no unit attention condition(s) shall be
3076c66ac9dbSNicholas Bellinger 	 *    established for this reason.
3077c66ac9dbSNicholas Bellinger 	 */
3078c66ac9dbSNicholas Bellinger 	if ((prh_type != type) || (prh_scope != scope)) {
3079c66ac9dbSNicholas Bellinger 		spin_lock(&pr_tmpl->registration_lock);
3080c66ac9dbSNicholas Bellinger 		list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3081c66ac9dbSNicholas Bellinger 				&pr_tmpl->registration_list, pr_reg_list) {
3082c66ac9dbSNicholas Bellinger 
3083c66ac9dbSNicholas Bellinger 			calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3084c66ac9dbSNicholas Bellinger 			if (calling_it_nexus)
3085c66ac9dbSNicholas Bellinger 				continue;
3086c66ac9dbSNicholas Bellinger 
3087c51c8e7bSHannes Reinecke 			target_ua_allocate_lun(pr_reg->pr_reg_nacl,
3088c66ac9dbSNicholas Bellinger 					pr_reg->pr_res_mapped_lun, 0x2A,
3089c66ac9dbSNicholas Bellinger 					ASCQ_2AH_RESERVATIONS_RELEASED);
3090c66ac9dbSNicholas Bellinger 		}
3091c66ac9dbSNicholas Bellinger 		spin_unlock(&pr_tmpl->registration_lock);
3092c66ac9dbSNicholas Bellinger 	}
3093c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
3094c66ac9dbSNicholas Bellinger 	/*
3095c66ac9dbSNicholas Bellinger 	 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3096c66ac9dbSNicholas Bellinger 	 * All received CDBs for the matching existing reservation and
3097c66ac9dbSNicholas Bellinger 	 * registrations undergo ABORT_TASK logic.
3098c66ac9dbSNicholas Bellinger 	 *
3099c66ac9dbSNicholas Bellinger 	 * From there, core_scsi3_release_preempt_and_abort() will
3100c66ac9dbSNicholas Bellinger 	 * release every registration in the list (which have already
3101c66ac9dbSNicholas Bellinger 	 * been removed from the primary pr_reg list), except the
3102c66ac9dbSNicholas Bellinger 	 * new persistent reservation holder, the calling Initiator Port.
3103c66ac9dbSNicholas Bellinger 	 */
310433ce6a87SAndy Grover 	if (preempt_type == PREEMPT_AND_ABORT) {
3105c66ac9dbSNicholas Bellinger 		core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3106c66ac9dbSNicholas Bellinger 		core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3107c66ac9dbSNicholas Bellinger 						pr_reg_n);
3108c66ac9dbSNicholas Bellinger 	}
3109c66ac9dbSNicholas Bellinger 
3110459f213bSAndy Grover 	if (pr_tmpl->pr_aptpl_active)
3111459f213bSAndy Grover 		core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
3112c66ac9dbSNicholas Bellinger 
3113c66ac9dbSNicholas Bellinger 	core_scsi3_put_pr_reg(pr_reg_n);
31145951146dSAndy Grover 	core_scsi3_pr_generation(cmd->se_dev);
3115c66ac9dbSNicholas Bellinger 	return 0;
3116c66ac9dbSNicholas Bellinger }
3117c66ac9dbSNicholas Bellinger 
3118de103c93SChristoph Hellwig static sense_reason_t
core_scsi3_emulate_pro_preempt(struct se_cmd * cmd,int type,int scope,u64 res_key,u64 sa_res_key,enum preempt_type preempt_type)3119de103c93SChristoph Hellwig core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope,
312033ce6a87SAndy Grover 		u64 res_key, u64 sa_res_key, enum preempt_type preempt_type)
3121c66ac9dbSNicholas Bellinger {
3122c66ac9dbSNicholas Bellinger 	switch (type) {
3123c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE:
3124c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS:
3125c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3126c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3127c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3128c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
3129de103c93SChristoph Hellwig 		return core_scsi3_pro_preempt(cmd, type, scope, res_key,
313033ce6a87SAndy Grover 					      sa_res_key, preempt_type);
3131c66ac9dbSNicholas Bellinger 	default:
31326708bb27SAndy Grover 		pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
313333ce6a87SAndy Grover 			" Type: 0x%02x\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", type);
3134de103c93SChristoph Hellwig 		return TCM_INVALID_CDB_FIELD;
3135c66ac9dbSNicholas Bellinger 	}
3136c66ac9dbSNicholas Bellinger }
3137c66ac9dbSNicholas Bellinger 
3138c66ac9dbSNicholas Bellinger 
3139de103c93SChristoph Hellwig static sense_reason_t
core_scsi3_emulate_pro_register_and_move(struct se_cmd * cmd,u64 res_key,u64 sa_res_key,int aptpl,int unreg)3140de103c93SChristoph Hellwig core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key,
3141de103c93SChristoph Hellwig 		u64 sa_res_key, int aptpl, int unreg)
3142c66ac9dbSNicholas Bellinger {
3143e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
31445951146dSAndy Grover 	struct se_device *dev = cmd->se_dev;
314528168905SJörn Engel 	struct se_dev_entry *dest_se_deve = NULL;
3146adf653f9SChristoph Hellwig 	struct se_lun *se_lun = cmd->se_lun, *tmp_lun;
3147c66ac9dbSNicholas Bellinger 	struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3148c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
31499ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3150c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
31510fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
315205d1c7c0SAndy Grover 	unsigned char *buf;
31532650d71eSChristoph Hellwig 	const unsigned char *initiator_str;
31544db6dfe6SChaitanya Kulkarni 	char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN] = { };
3155c66ac9dbSNicholas Bellinger 	u32 tid_len, tmp_tid_len;
3156d2843c17SAndy Grover 	int new_reg = 0, type, scope, matching_iname;
3157de103c93SChristoph Hellwig 	sense_reason_t ret;
3158c66ac9dbSNicholas Bellinger 	unsigned short rtpi;
3159c66ac9dbSNicholas Bellinger 	unsigned char proto_ident;
3160c66ac9dbSNicholas Bellinger 
31616708bb27SAndy Grover 	if (!se_sess || !se_lun) {
31626708bb27SAndy Grover 		pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3163de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3164c66ac9dbSNicholas Bellinger 	}
3165de103c93SChristoph Hellwig 
3166c66ac9dbSNicholas Bellinger 	se_tpg = se_sess->se_tpg;
3167e3d6f909SAndy Grover 	tf_ops = se_tpg->se_tpg_tfo;
3168c66ac9dbSNicholas Bellinger 	/*
3169c66ac9dbSNicholas Bellinger 	 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3170c66ac9dbSNicholas Bellinger 	 *	Register behaviors for a REGISTER AND MOVE service action
3171c66ac9dbSNicholas Bellinger 	 *
3172c66ac9dbSNicholas Bellinger 	 * Locate the existing *pr_reg via struct se_node_acl pointers
3173c66ac9dbSNicholas Bellinger 	 */
31745951146dSAndy Grover 	pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3175c66ac9dbSNicholas Bellinger 				se_sess);
31766708bb27SAndy Grover 	if (!pr_reg) {
31776708bb27SAndy Grover 		pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3178c66ac9dbSNicholas Bellinger 			" *pr_reg for REGISTER_AND_MOVE\n");
3179de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3180c66ac9dbSNicholas Bellinger 	}
3181c66ac9dbSNicholas Bellinger 	/*
3182c66ac9dbSNicholas Bellinger 	 * The provided reservation key much match the existing reservation key
3183c66ac9dbSNicholas Bellinger 	 * provided during this initiator's I_T nexus registration.
3184c66ac9dbSNicholas Bellinger 	 */
3185c66ac9dbSNicholas Bellinger 	if (res_key != pr_reg->pr_res_key) {
31866708bb27SAndy Grover 		pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3187c66ac9dbSNicholas Bellinger 			" res_key: 0x%016Lx does not match existing SA REGISTER"
3188c66ac9dbSNicholas Bellinger 			" res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
3189de103c93SChristoph Hellwig 		ret = TCM_RESERVATION_CONFLICT;
3190de103c93SChristoph Hellwig 		goto out_put_pr_reg;
3191c66ac9dbSNicholas Bellinger 	}
3192c66ac9dbSNicholas Bellinger 	/*
3193c66ac9dbSNicholas Bellinger 	 * The service active reservation key needs to be non zero
3194c66ac9dbSNicholas Bellinger 	 */
31956708bb27SAndy Grover 	if (!sa_res_key) {
31966708bb27SAndy Grover 		pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3197c66ac9dbSNicholas Bellinger 			" sa_res_key\n");
3198de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
3199de103c93SChristoph Hellwig 		goto out_put_pr_reg;
3200c66ac9dbSNicholas Bellinger 	}
320105d1c7c0SAndy Grover 
3202c66ac9dbSNicholas Bellinger 	/*
3203c66ac9dbSNicholas Bellinger 	 * Determine the Relative Target Port Identifier where the reservation
3204c66ac9dbSNicholas Bellinger 	 * will be moved to for the TransportID containing SCSI initiator WWN
3205c66ac9dbSNicholas Bellinger 	 * information.
3206c66ac9dbSNicholas Bellinger 	 */
32074949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
3208de103c93SChristoph Hellwig 	if (!buf) {
320979dd6f2fStangwenji 		ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
3210de103c93SChristoph Hellwig 		goto out_put_pr_reg;
3211de103c93SChristoph Hellwig 	}
3212de103c93SChristoph Hellwig 
3213a85d667eSBart Van Assche 	rtpi = get_unaligned_be16(&buf[18]);
3214a85d667eSBart Van Assche 	tid_len = get_unaligned_be32(&buf[20]);
32154949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
321605d1c7c0SAndy Grover 	buf = NULL;
3217c66ac9dbSNicholas Bellinger 
3218c66ac9dbSNicholas Bellinger 	if ((tid_len + 24) != cmd->data_length) {
32196708bb27SAndy Grover 		pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3220c66ac9dbSNicholas Bellinger 			" does not equal CDB data_length: %u\n", tid_len,
3221c66ac9dbSNicholas Bellinger 			cmd->data_length);
3222de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
3223de103c93SChristoph Hellwig 		goto out_put_pr_reg;
3224c66ac9dbSNicholas Bellinger 	}
3225c66ac9dbSNicholas Bellinger 
3226c66ac9dbSNicholas Bellinger 	spin_lock(&dev->se_port_lock);
3227adf653f9SChristoph Hellwig 	list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
3228adf653f9SChristoph Hellwig 		if (tmp_lun->lun_tpg->tpg_rtpi != rtpi)
3229c66ac9dbSNicholas Bellinger 			continue;
3230adf653f9SChristoph Hellwig 		dest_se_tpg = tmp_lun->lun_tpg;
3231e3d6f909SAndy Grover 		dest_tf_ops = dest_se_tpg->se_tpg_tfo;
32326708bb27SAndy Grover 		if (!dest_tf_ops)
3233c66ac9dbSNicholas Bellinger 			continue;
3234c66ac9dbSNicholas Bellinger 
323533940d09SJoern Engel 		atomic_inc_mb(&dest_se_tpg->tpg_pr_ref_count);
3236c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->se_port_lock);
3237c66ac9dbSNicholas Bellinger 
3238de103c93SChristoph Hellwig 		if (core_scsi3_tpg_depend_item(dest_se_tpg)) {
32396708bb27SAndy Grover 			pr_err("core_scsi3_tpg_depend_item() failed"
3240c66ac9dbSNicholas Bellinger 				" for dest_se_tpg\n");
324133940d09SJoern Engel 			atomic_dec_mb(&dest_se_tpg->tpg_pr_ref_count);
3242de103c93SChristoph Hellwig 			ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3243de103c93SChristoph Hellwig 			goto out_put_pr_reg;
3244c66ac9dbSNicholas Bellinger 		}
3245c66ac9dbSNicholas Bellinger 
3246c66ac9dbSNicholas Bellinger 		spin_lock(&dev->se_port_lock);
3247c66ac9dbSNicholas Bellinger 		break;
3248c66ac9dbSNicholas Bellinger 	}
3249c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->se_port_lock);
3250c66ac9dbSNicholas Bellinger 
32516708bb27SAndy Grover 	if (!dest_se_tpg || !dest_tf_ops) {
32526708bb27SAndy Grover 		pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3253c66ac9dbSNicholas Bellinger 			" fabric ops from Relative Target Port Identifier:"
3254c66ac9dbSNicholas Bellinger 			" %hu\n", rtpi);
3255de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
3256de103c93SChristoph Hellwig 		goto out_put_pr_reg;
3257c66ac9dbSNicholas Bellinger 	}
325805d1c7c0SAndy Grover 
32594949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
3260de103c93SChristoph Hellwig 	if (!buf) {
326179dd6f2fStangwenji 		ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
3262de103c93SChristoph Hellwig 		goto out_put_pr_reg;
3263de103c93SChristoph Hellwig 	}
3264c66ac9dbSNicholas Bellinger 	proto_ident = (buf[24] & 0x0f);
32658b1e1244SAndy Grover 
32666708bb27SAndy Grover 	pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3267c66ac9dbSNicholas Bellinger 			" 0x%02x\n", proto_ident);
32688b1e1244SAndy Grover 
32692aeeafaeSChristoph Hellwig 	if (proto_ident != dest_se_tpg->proto_id) {
32706708bb27SAndy Grover 		pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3271c66ac9dbSNicholas Bellinger 			" proto_ident: 0x%02x does not match ident: 0x%02x"
3272c66ac9dbSNicholas Bellinger 			" from fabric: %s\n", proto_ident,
32732aeeafaeSChristoph Hellwig 			dest_se_tpg->proto_id,
327430c7ca93SDavid Disseldorp 			dest_tf_ops->fabric_name);
3275de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
3276c66ac9dbSNicholas Bellinger 		goto out;
3277c66ac9dbSNicholas Bellinger 	}
32782650d71eSChristoph Hellwig 	initiator_str = target_parse_pr_out_transport_id(dest_se_tpg,
3279094bb5d7SRasmus Villemoes 			&buf[24], &tmp_tid_len, &iport_ptr);
32806708bb27SAndy Grover 	if (!initiator_str) {
32816708bb27SAndy Grover 		pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3282c66ac9dbSNicholas Bellinger 			" initiator_str from Transport ID\n");
3283de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
3284c66ac9dbSNicholas Bellinger 		goto out;
3285c66ac9dbSNicholas Bellinger 	}
3286c66ac9dbSNicholas Bellinger 
32874949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
328805d1c7c0SAndy Grover 	buf = NULL;
328905d1c7c0SAndy Grover 
32906708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
329130c7ca93SDavid Disseldorp 		" %s\n", dest_tf_ops->fabric_name, (iport_ptr != NULL) ?
3292c66ac9dbSNicholas Bellinger 		"port" : "device", initiator_str, (iport_ptr != NULL) ?
3293c66ac9dbSNicholas Bellinger 		iport_ptr : "");
3294c66ac9dbSNicholas Bellinger 	/*
3295c66ac9dbSNicholas Bellinger 	 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3296c66ac9dbSNicholas Bellinger 	 * action specifies a TransportID that is the same as the initiator port
3297c66ac9dbSNicholas Bellinger 	 * of the I_T nexus for the command received, then the command shall
3298c66ac9dbSNicholas Bellinger 	 * be terminated with CHECK CONDITION status, with the sense key set to
3299c66ac9dbSNicholas Bellinger 	 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3300c66ac9dbSNicholas Bellinger 	 * IN PARAMETER LIST.
3301c66ac9dbSNicholas Bellinger 	 */
3302c66ac9dbSNicholas Bellinger 	pr_reg_nacl = pr_reg->pr_reg_nacl;
3303c66ac9dbSNicholas Bellinger 	matching_iname = (!strcmp(initiator_str,
3304c66ac9dbSNicholas Bellinger 				  pr_reg_nacl->initiatorname)) ? 1 : 0;
33056708bb27SAndy Grover 	if (!matching_iname)
3306c66ac9dbSNicholas Bellinger 		goto after_iport_check;
3307c66ac9dbSNicholas Bellinger 
33086708bb27SAndy Grover 	if (!iport_ptr || !pr_reg->isid_present_at_reg) {
33096708bb27SAndy Grover 		pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3310c66ac9dbSNicholas Bellinger 			" matches: %s on received I_T Nexus\n", initiator_str,
3311c66ac9dbSNicholas Bellinger 			pr_reg_nacl->initiatorname);
3312de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
3313c66ac9dbSNicholas Bellinger 		goto out;
3314c66ac9dbSNicholas Bellinger 	}
33156708bb27SAndy Grover 	if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
33166708bb27SAndy Grover 		pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3317c66ac9dbSNicholas Bellinger 			" matches: %s %s on received I_T Nexus\n",
3318c66ac9dbSNicholas Bellinger 			initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3319c66ac9dbSNicholas Bellinger 			pr_reg->pr_reg_isid);
3320de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
3321c66ac9dbSNicholas Bellinger 		goto out;
3322c66ac9dbSNicholas Bellinger 	}
3323c66ac9dbSNicholas Bellinger after_iport_check:
3324c66ac9dbSNicholas Bellinger 	/*
3325c66ac9dbSNicholas Bellinger 	 * Locate the destination struct se_node_acl from the received Transport ID
3326c66ac9dbSNicholas Bellinger 	 */
3327403edd78SNicholas Bellinger 	mutex_lock(&dest_se_tpg->acl_node_mutex);
3328c66ac9dbSNicholas Bellinger 	dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3329c66ac9dbSNicholas Bellinger 				initiator_str);
333033940d09SJoern Engel 	if (dest_node_acl)
333133940d09SJoern Engel 		atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
3332403edd78SNicholas Bellinger 	mutex_unlock(&dest_se_tpg->acl_node_mutex);
3333c66ac9dbSNicholas Bellinger 
33346708bb27SAndy Grover 	if (!dest_node_acl) {
33356708bb27SAndy Grover 		pr_err("Unable to locate %s dest_node_acl for"
333630c7ca93SDavid Disseldorp 			" TransportID%s\n", dest_tf_ops->fabric_name,
3337c66ac9dbSNicholas Bellinger 			initiator_str);
3338de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
3339c66ac9dbSNicholas Bellinger 		goto out;
3340c66ac9dbSNicholas Bellinger 	}
3341de103c93SChristoph Hellwig 
3342de103c93SChristoph Hellwig 	if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
33436708bb27SAndy Grover 		pr_err("core_scsi3_nodeacl_depend_item() for"
3344c66ac9dbSNicholas Bellinger 			" dest_node_acl\n");
334533940d09SJoern Engel 		atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
3346c66ac9dbSNicholas Bellinger 		dest_node_acl = NULL;
3347de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
3348c66ac9dbSNicholas Bellinger 		goto out;
3349c66ac9dbSNicholas Bellinger 	}
33508b1e1244SAndy Grover 
33516708bb27SAndy Grover 	pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
335230c7ca93SDavid Disseldorp 		" %s from TransportID\n", dest_tf_ops->fabric_name,
3353c66ac9dbSNicholas Bellinger 		dest_node_acl->initiatorname);
33548b1e1244SAndy Grover 
3355c66ac9dbSNicholas Bellinger 	/*
3356c66ac9dbSNicholas Bellinger 	 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3357c66ac9dbSNicholas Bellinger 	 * PORT IDENTIFIER.
3358c66ac9dbSNicholas Bellinger 	 */
3359c66ac9dbSNicholas Bellinger 	dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
33606708bb27SAndy Grover 	if (!dest_se_deve) {
33616708bb27SAndy Grover 		pr_err("Unable to locate %s dest_se_deve from RTPI:"
336230c7ca93SDavid Disseldorp 			" %hu\n",  dest_tf_ops->fabric_name, rtpi);
3363de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
3364c66ac9dbSNicholas Bellinger 		goto out;
3365c66ac9dbSNicholas Bellinger 	}
3366c66ac9dbSNicholas Bellinger 
3367de103c93SChristoph Hellwig 	if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
33686708bb27SAndy Grover 		pr_err("core_scsi3_lunacl_depend_item() failed\n");
336929a05deeSNicholas Bellinger 		kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
3370c66ac9dbSNicholas Bellinger 		dest_se_deve = NULL;
3371de103c93SChristoph Hellwig 		ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3372c66ac9dbSNicholas Bellinger 		goto out;
3373c66ac9dbSNicholas Bellinger 	}
33748b1e1244SAndy Grover 
33756708bb27SAndy Grover 	pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3376f2d30680SHannes Reinecke 		" ACL for dest_se_deve->mapped_lun: %llu\n",
337730c7ca93SDavid Disseldorp 		dest_tf_ops->fabric_name, dest_node_acl->initiatorname,
3378c66ac9dbSNicholas Bellinger 		dest_se_deve->mapped_lun);
33798b1e1244SAndy Grover 
3380c66ac9dbSNicholas Bellinger 	/*
3381c66ac9dbSNicholas Bellinger 	 * A persistent reservation needs to already existing in order to
3382c66ac9dbSNicholas Bellinger 	 * successfully complete the REGISTER_AND_MOVE service action..
3383c66ac9dbSNicholas Bellinger 	 */
3384c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
3385c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
33866708bb27SAndy Grover 	if (!pr_res_holder) {
33876708bb27SAndy Grover 		pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3388c66ac9dbSNicholas Bellinger 			" currently held\n");
3389c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
3390de103c93SChristoph Hellwig 		ret = TCM_INVALID_CDB_FIELD;
3391c66ac9dbSNicholas Bellinger 		goto out;
3392c66ac9dbSNicholas Bellinger 	}
3393c66ac9dbSNicholas Bellinger 	/*
3394c66ac9dbSNicholas Bellinger 	 * The received on I_T Nexus must be the reservation holder.
3395c66ac9dbSNicholas Bellinger 	 *
3396c66ac9dbSNicholas Bellinger 	 * From spc4r17 section 5.7.8  Table 50 --
3397c66ac9dbSNicholas Bellinger 	 * 	Register behaviors for a REGISTER AND MOVE service action
3398c66ac9dbSNicholas Bellinger 	 */
3399e673dc92SIlias Tsitsimpis 	if (!is_reservation_holder(pr_res_holder, pr_reg)) {
34006708bb27SAndy Grover 		pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3401c66ac9dbSNicholas Bellinger 			" Nexus is not reservation holder\n");
3402c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
3403de103c93SChristoph Hellwig 		ret = TCM_RESERVATION_CONFLICT;
3404c66ac9dbSNicholas Bellinger 		goto out;
3405c66ac9dbSNicholas Bellinger 	}
3406c66ac9dbSNicholas Bellinger 	/*
3407c66ac9dbSNicholas Bellinger 	 * From spc4r17 section 5.7.8: registering and moving reservation
3408c66ac9dbSNicholas Bellinger 	 *
3409c66ac9dbSNicholas Bellinger 	 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3410c66ac9dbSNicholas Bellinger 	 * action is received and the established persistent reservation is a
3411c66ac9dbSNicholas Bellinger 	 * Write Exclusive - All Registrants type or Exclusive Access -
3412c66ac9dbSNicholas Bellinger 	 * All Registrants type reservation, then the command shall be completed
3413c66ac9dbSNicholas Bellinger 	 * with RESERVATION CONFLICT status.
3414c66ac9dbSNicholas Bellinger 	 */
3415c66ac9dbSNicholas Bellinger 	if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3416c66ac9dbSNicholas Bellinger 	    (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
34176708bb27SAndy Grover 		pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3418c66ac9dbSNicholas Bellinger 			" reservation for type: %s\n",
3419c66ac9dbSNicholas Bellinger 			core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3420c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
3421de103c93SChristoph Hellwig 		ret = TCM_RESERVATION_CONFLICT;
3422c66ac9dbSNicholas Bellinger 		goto out;
3423c66ac9dbSNicholas Bellinger 	}
3424c66ac9dbSNicholas Bellinger 	pr_res_nacl = pr_res_holder->pr_reg_nacl;
3425c66ac9dbSNicholas Bellinger 	/*
3426c66ac9dbSNicholas Bellinger 	 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3427c66ac9dbSNicholas Bellinger 	 */
3428c66ac9dbSNicholas Bellinger 	type = pr_res_holder->pr_res_type;
3429c66ac9dbSNicholas Bellinger 	scope = pr_res_holder->pr_res_type;
3430c66ac9dbSNicholas Bellinger 	/*
3431c66ac9dbSNicholas Bellinger 	 * c) Associate the reservation key specified in the SERVICE ACTION
3432c66ac9dbSNicholas Bellinger 	 *    RESERVATION KEY field with the I_T nexus specified as the
3433c66ac9dbSNicholas Bellinger 	 *    destination of the register and move, where:
3434c66ac9dbSNicholas Bellinger 	 *    A) The I_T nexus is specified by the TransportID and the
3435c66ac9dbSNicholas Bellinger 	 *	 RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3436c66ac9dbSNicholas Bellinger 	 *    B) Regardless of the TransportID format used, the association for
3437c66ac9dbSNicholas Bellinger 	 *       the initiator port is based on either the initiator port name
3438c66ac9dbSNicholas Bellinger 	 *       (see 3.1.71) on SCSI transport protocols where port names are
3439c66ac9dbSNicholas Bellinger 	 *       required or the initiator port identifier (see 3.1.70) on SCSI
3440c66ac9dbSNicholas Bellinger 	 *       transport protocols where port names are not required;
3441c66ac9dbSNicholas Bellinger 	 * d) Register the reservation key specified in the SERVICE ACTION
3442c66ac9dbSNicholas Bellinger 	 *    RESERVATION KEY field;
3443c66ac9dbSNicholas Bellinger 	 *
3444c66ac9dbSNicholas Bellinger 	 * Also, It is not an error for a REGISTER AND MOVE service action to
3445c66ac9dbSNicholas Bellinger 	 * register an I_T nexus that is already registered with the same
3446c66ac9dbSNicholas Bellinger 	 * reservation key or a different reservation key.
3447c66ac9dbSNicholas Bellinger 	 */
3448c66ac9dbSNicholas Bellinger 	dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3449c66ac9dbSNicholas Bellinger 					iport_ptr);
34506708bb27SAndy Grover 	if (!dest_pr_reg) {
3451ef4f7e4bSDmitry Bogdanov 		struct se_lun *dest_lun = dest_se_deve->se_lun;
345279dc9c9eSNicholas Bellinger 
3453c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
345479dc9c9eSNicholas Bellinger 		if (core_scsi3_alloc_registration(cmd->se_dev, dest_node_acl,
345579dc9c9eSNicholas Bellinger 					dest_lun, dest_se_deve, dest_se_deve->mapped_lun,
345679dc9c9eSNicholas Bellinger 					iport_ptr, sa_res_key, 0, aptpl, 2, 1)) {
345779dd6f2fStangwenji 			ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
3458c66ac9dbSNicholas Bellinger 			goto out;
3459c66ac9dbSNicholas Bellinger 		}
346080bfdfa9SNicholas Bellinger 		spin_lock(&dev->dev_reservation_lock);
3461c66ac9dbSNicholas Bellinger 		dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3462c66ac9dbSNicholas Bellinger 						iport_ptr);
3463c66ac9dbSNicholas Bellinger 		new_reg = 1;
34643e2deba7SDmitry Bogdanov 	} else {
34653e2deba7SDmitry Bogdanov 		/*
34663e2deba7SDmitry Bogdanov 		 * e) Retain the reservation key specified in the SERVICE ACTION
34673e2deba7SDmitry Bogdanov 		 *    RESERVATION KEY field and associated information;
34683e2deba7SDmitry Bogdanov 		 */
34693e2deba7SDmitry Bogdanov 		dest_pr_reg->pr_res_key = sa_res_key;
3470c66ac9dbSNicholas Bellinger 	}
3471c66ac9dbSNicholas Bellinger 	/*
3472c66ac9dbSNicholas Bellinger 	 * f) Release the persistent reservation for the persistent reservation
3473c66ac9dbSNicholas Bellinger 	 *    holder (i.e., the I_T nexus on which the
3474c66ac9dbSNicholas Bellinger 	 */
3475c66ac9dbSNicholas Bellinger 	__core_scsi3_complete_pro_release(dev, pr_res_nacl,
34766c3c9baaSNicholas Bellinger 					  dev->dev_pr_res_holder, 0, 0);
3477c66ac9dbSNicholas Bellinger 	/*
3478c66ac9dbSNicholas Bellinger 	 * g) Move the persistent reservation to the specified I_T nexus using
3479c66ac9dbSNicholas Bellinger 	 *    the same scope and type as the persistent reservation released in
3480c66ac9dbSNicholas Bellinger 	 *    item f); and
3481c66ac9dbSNicholas Bellinger 	 */
3482c66ac9dbSNicholas Bellinger 	dev->dev_pr_res_holder = dest_pr_reg;
3483c66ac9dbSNicholas Bellinger 	dest_pr_reg->pr_res_holder = 1;
3484c66ac9dbSNicholas Bellinger 	dest_pr_reg->pr_res_type = type;
3485c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_scope = scope;
3486d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
3487c66ac9dbSNicholas Bellinger 	/*
3488c66ac9dbSNicholas Bellinger 	 * Increment PRGeneration for existing registrations..
3489c66ac9dbSNicholas Bellinger 	 */
34906708bb27SAndy Grover 	if (!new_reg)
3491c66ac9dbSNicholas Bellinger 		dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3492c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
3493c66ac9dbSNicholas Bellinger 
34946708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3495c66ac9dbSNicholas Bellinger 		" created new reservation holder TYPE: %s on object RTPI:"
349630c7ca93SDavid Disseldorp 		" %hu  PRGeneration: 0x%08x\n", dest_tf_ops->fabric_name,
3497c66ac9dbSNicholas Bellinger 		core_scsi3_pr_dump_type(type), rtpi,
3498c66ac9dbSNicholas Bellinger 		dest_pr_reg->pr_res_generation);
34996708bb27SAndy Grover 	pr_debug("SPC-3 PR Successfully moved reservation from"
3500c66ac9dbSNicholas Bellinger 		" %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
350130c7ca93SDavid Disseldorp 		tf_ops->fabric_name, pr_reg_nacl->initiatorname,
350230c7ca93SDavid Disseldorp 		i_buf, dest_tf_ops->fabric_name,
3503c66ac9dbSNicholas Bellinger 		dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3504c66ac9dbSNicholas Bellinger 		iport_ptr : "");
3505c66ac9dbSNicholas Bellinger 	/*
3506c66ac9dbSNicholas Bellinger 	 * It is now safe to release configfs group dependencies for destination
3507c66ac9dbSNicholas Bellinger 	 * of Transport ID Initiator Device/Port Identifier
3508c66ac9dbSNicholas Bellinger 	 */
3509c66ac9dbSNicholas Bellinger 	core_scsi3_lunacl_undepend_item(dest_se_deve);
3510c66ac9dbSNicholas Bellinger 	core_scsi3_nodeacl_undepend_item(dest_node_acl);
3511c66ac9dbSNicholas Bellinger 	core_scsi3_tpg_undepend_item(dest_se_tpg);
3512c66ac9dbSNicholas Bellinger 	/*
3513c66ac9dbSNicholas Bellinger 	 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3514c66ac9dbSNicholas Bellinger 	 * nexus on which PERSISTENT RESERVE OUT command was received.
3515c66ac9dbSNicholas Bellinger 	 */
3516c66ac9dbSNicholas Bellinger 	if (unreg) {
3517c66ac9dbSNicholas Bellinger 		spin_lock(&pr_tmpl->registration_lock);
3518c66ac9dbSNicholas Bellinger 		__core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3519c66ac9dbSNicholas Bellinger 		spin_unlock(&pr_tmpl->registration_lock);
3520c66ac9dbSNicholas Bellinger 	} else
3521c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg);
3522c66ac9dbSNicholas Bellinger 
3523459f213bSAndy Grover 	core_scsi3_update_and_write_aptpl(cmd->se_dev, aptpl);
3524c66ac9dbSNicholas Bellinger 
3525c66ac9dbSNicholas Bellinger 	core_scsi3_put_pr_reg(dest_pr_reg);
3526c66ac9dbSNicholas Bellinger 	return 0;
3527c66ac9dbSNicholas Bellinger out:
352805d1c7c0SAndy Grover 	if (buf)
35294949314cSAndy Grover 		transport_kunmap_data_sg(cmd);
3530c66ac9dbSNicholas Bellinger 	if (dest_se_deve)
3531c66ac9dbSNicholas Bellinger 		core_scsi3_lunacl_undepend_item(dest_se_deve);
3532c66ac9dbSNicholas Bellinger 	if (dest_node_acl)
3533c66ac9dbSNicholas Bellinger 		core_scsi3_nodeacl_undepend_item(dest_node_acl);
3534c66ac9dbSNicholas Bellinger 	core_scsi3_tpg_undepend_item(dest_se_tpg);
3535de103c93SChristoph Hellwig 
3536de103c93SChristoph Hellwig out_put_pr_reg:
3537c66ac9dbSNicholas Bellinger 	core_scsi3_put_pr_reg(pr_reg);
3538c66ac9dbSNicholas Bellinger 	return ret;
3539c66ac9dbSNicholas Bellinger }
3540c66ac9dbSNicholas Bellinger 
354153062aceSMike Christie static sense_reason_t
target_try_pr_out_pt(struct se_cmd * cmd,u8 sa,u64 res_key,u64 sa_res_key,u8 type,bool aptpl,bool all_tg_pt,bool spec_i_pt)354253062aceSMike Christie target_try_pr_out_pt(struct se_cmd *cmd, u8 sa, u64 res_key, u64 sa_res_key,
354353062aceSMike Christie 		     u8 type, bool aptpl, bool all_tg_pt, bool spec_i_pt)
354453062aceSMike Christie {
354553062aceSMike Christie 	struct exec_cmd_ops *ops = cmd->protocol_data;
354653062aceSMike Christie 
354753062aceSMike Christie 	if (!cmd->se_sess || !cmd->se_lun) {
354853062aceSMike Christie 		pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
354953062aceSMike Christie 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
355053062aceSMike Christie 	}
355153062aceSMike Christie 
355253062aceSMike Christie 	if (!ops->execute_pr_out) {
355353062aceSMike Christie 		pr_err("SPC-3 PR: Device has been configured for PR passthrough but it's not supported by the backend.\n");
355453062aceSMike Christie 		return TCM_UNSUPPORTED_SCSI_OPCODE;
355553062aceSMike Christie 	}
355653062aceSMike Christie 
3557*8455799dSMike Christie 	switch (sa) {
3558*8455799dSMike Christie 	case PRO_REGISTER_AND_MOVE:
3559*8455799dSMike Christie 	case PRO_REPLACE_LOST_RESERVATION:
3560*8455799dSMike Christie 		pr_err("SPC-3 PR: PRO_REGISTER_AND_MOVE and PRO_REPLACE_LOST_RESERVATION are not supported by PR passthrough.\n");
3561*8455799dSMike Christie 		return TCM_UNSUPPORTED_SCSI_OPCODE;
3562*8455799dSMike Christie 	}
3563*8455799dSMike Christie 
3564*8455799dSMike Christie 	if (spec_i_pt || all_tg_pt) {
3565*8455799dSMike Christie 		pr_err("SPC-3 PR: SPEC_I_PT and ALL_TG_PT are not supported by PR passthrough.\n");
3566*8455799dSMike Christie 		return TCM_UNSUPPORTED_SCSI_OPCODE;
3567*8455799dSMike Christie 	}
3568*8455799dSMike Christie 
356953062aceSMike Christie 	return ops->execute_pr_out(cmd, sa, res_key, sa_res_key, type, aptpl);
357053062aceSMike Christie }
357153062aceSMike Christie 
3572c66ac9dbSNicholas Bellinger /*
3573c66ac9dbSNicholas Bellinger  * See spc4r17 section 6.14 Table 170
3574c66ac9dbSNicholas Bellinger  */
3575de103c93SChristoph Hellwig sense_reason_t
target_scsi3_emulate_pr_out(struct se_cmd * cmd)3576de103c93SChristoph Hellwig target_scsi3_emulate_pr_out(struct se_cmd *cmd)
3577c66ac9dbSNicholas Bellinger {
357892404e60SNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
3579617c0e06SChristoph Hellwig 	unsigned char *cdb = &cmd->t_task_cdb[0];
358005d1c7c0SAndy Grover 	unsigned char *buf;
3581c66ac9dbSNicholas Bellinger 	u64 res_key, sa_res_key;
3582c66ac9dbSNicholas Bellinger 	int sa, scope, type, aptpl;
3583c66ac9dbSNicholas Bellinger 	int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
3584de103c93SChristoph Hellwig 	sense_reason_t ret;
3585617c0e06SChristoph Hellwig 
3586617c0e06SChristoph Hellwig 	/*
3587617c0e06SChristoph Hellwig 	 * Following spc2r20 5.5.1 Reservations overview:
3588617c0e06SChristoph Hellwig 	 *
3589617c0e06SChristoph Hellwig 	 * If a logical unit has been reserved by any RESERVE command and is
3590617c0e06SChristoph Hellwig 	 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3591617c0e06SChristoph Hellwig 	 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3592617c0e06SChristoph Hellwig 	 * initiator or service action and shall terminate with a RESERVATION
3593617c0e06SChristoph Hellwig 	 * CONFLICT status.
3594617c0e06SChristoph Hellwig 	 */
35950fd97ccfSChristoph Hellwig 	if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
3596617c0e06SChristoph Hellwig 		pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3597617c0e06SChristoph Hellwig 			" SPC-2 reservation is held, returning"
3598617c0e06SChristoph Hellwig 			" RESERVATION_CONFLICT\n");
3599de103c93SChristoph Hellwig 		return TCM_RESERVATION_CONFLICT;
3600617c0e06SChristoph Hellwig 	}
3601617c0e06SChristoph Hellwig 
3602c66ac9dbSNicholas Bellinger 	/*
3603c66ac9dbSNicholas Bellinger 	 * FIXME: A NULL struct se_session pointer means an this is not coming from
3604c66ac9dbSNicholas Bellinger 	 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3605c66ac9dbSNicholas Bellinger 	 */
3606de103c93SChristoph Hellwig 	if (!cmd->se_sess)
3607de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3608c66ac9dbSNicholas Bellinger 
3609c66ac9dbSNicholas Bellinger 	if (cmd->data_length < 24) {
36106708bb27SAndy Grover 		pr_warn("SPC-PR: Received PR OUT parameter list"
3611c66ac9dbSNicholas Bellinger 			" length too small: %u\n", cmd->data_length);
36127ee00317STang Wenji 		return TCM_PARAMETER_LIST_LENGTH_ERROR;
3613c66ac9dbSNicholas Bellinger 	}
3614de103c93SChristoph Hellwig 
3615c66ac9dbSNicholas Bellinger 	/*
3616c66ac9dbSNicholas Bellinger 	 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3617c66ac9dbSNicholas Bellinger 	 */
3618c66ac9dbSNicholas Bellinger 	sa = (cdb[1] & 0x1f);
3619c66ac9dbSNicholas Bellinger 	scope = (cdb[2] & 0xf0);
3620c66ac9dbSNicholas Bellinger 	type = (cdb[2] & 0x0f);
362105d1c7c0SAndy Grover 
36224949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
3623de103c93SChristoph Hellwig 	if (!buf)
3624de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3625de103c93SChristoph Hellwig 
3626c66ac9dbSNicholas Bellinger 	/*
3627c66ac9dbSNicholas Bellinger 	 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3628c66ac9dbSNicholas Bellinger 	 */
3629a85d667eSBart Van Assche 	res_key = get_unaligned_be64(&buf[0]);
3630a85d667eSBart Van Assche 	sa_res_key = get_unaligned_be64(&buf[8]);
3631c66ac9dbSNicholas Bellinger 	/*
3632c66ac9dbSNicholas Bellinger 	 * REGISTER_AND_MOVE uses a different SA parameter list containing
3633c66ac9dbSNicholas Bellinger 	 * SCSI TransportIDs.
3634c66ac9dbSNicholas Bellinger 	 */
3635c66ac9dbSNicholas Bellinger 	if (sa != PRO_REGISTER_AND_MOVE) {
3636c66ac9dbSNicholas Bellinger 		spec_i_pt = (buf[20] & 0x08);
3637c66ac9dbSNicholas Bellinger 		all_tg_pt = (buf[20] & 0x04);
3638c66ac9dbSNicholas Bellinger 		aptpl = (buf[20] & 0x01);
3639c66ac9dbSNicholas Bellinger 	} else {
3640c66ac9dbSNicholas Bellinger 		aptpl = (buf[17] & 0x01);
3641c66ac9dbSNicholas Bellinger 		unreg = (buf[17] & 0x02);
3642c66ac9dbSNicholas Bellinger 	}
364392404e60SNicholas Bellinger 	/*
364492404e60SNicholas Bellinger 	 * If the backend device has been configured to force APTPL metadata
364592404e60SNicholas Bellinger 	 * write-out, go ahead and propigate aptpl=1 down now.
364692404e60SNicholas Bellinger 	 */
364792404e60SNicholas Bellinger 	if (dev->dev_attrib.force_pr_aptpl)
364892404e60SNicholas Bellinger 		aptpl = 1;
364992404e60SNicholas Bellinger 
36504949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
365105d1c7c0SAndy Grover 	buf = NULL;
365205d1c7c0SAndy Grover 
3653c66ac9dbSNicholas Bellinger 	/*
3654c66ac9dbSNicholas Bellinger 	 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3655c66ac9dbSNicholas Bellinger 	 */
36567ee00317STang Wenji 	if (spec_i_pt && (sa != PRO_REGISTER))
3657de103c93SChristoph Hellwig 		return TCM_INVALID_PARAMETER_LIST;
3658d29a5b6aSChristoph Hellwig 
3659c66ac9dbSNicholas Bellinger 	/*
3660c66ac9dbSNicholas Bellinger 	 * From spc4r17 section 6.14:
3661c66ac9dbSNicholas Bellinger 	 *
3662c66ac9dbSNicholas Bellinger 	 * If the SPEC_I_PT bit is set to zero, the service action is not
3663c66ac9dbSNicholas Bellinger 	 * REGISTER AND MOVE, and the parameter list length is not 24, then
3664c66ac9dbSNicholas Bellinger 	 * the command shall be terminated with CHECK CONDITION status, with
3665c66ac9dbSNicholas Bellinger 	 * the sense key set to ILLEGAL REQUEST, and the additional sense
3666c66ac9dbSNicholas Bellinger 	 * code set to PARAMETER LIST LENGTH ERROR.
3667c66ac9dbSNicholas Bellinger 	 */
36687ee00317STang Wenji 	if (!spec_i_pt && (sa != PRO_REGISTER_AND_MOVE) &&
3669c66ac9dbSNicholas Bellinger 	    (cmd->data_length != 24)) {
36706708bb27SAndy Grover 		pr_warn("SPC-PR: Received PR OUT illegal parameter"
3671c66ac9dbSNicholas Bellinger 			" list length: %u\n", cmd->data_length);
36727ee00317STang Wenji 		return TCM_PARAMETER_LIST_LENGTH_ERROR;
3673c66ac9dbSNicholas Bellinger 	}
3674de103c93SChristoph Hellwig 
367553062aceSMike Christie 	if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR) {
367653062aceSMike Christie 		ret = target_try_pr_out_pt(cmd, sa, res_key, sa_res_key, type,
367753062aceSMike Christie 					   aptpl, all_tg_pt, spec_i_pt);
367853062aceSMike Christie 		goto done;
367953062aceSMike Christie 	}
368053062aceSMike Christie 
3681c66ac9dbSNicholas Bellinger 	/*
3682c66ac9dbSNicholas Bellinger 	 * (core_scsi3_emulate_pro_* function parameters
3683c66ac9dbSNicholas Bellinger 	 * are defined by spc4r17 Table 174:
3684c66ac9dbSNicholas Bellinger 	 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3685c66ac9dbSNicholas Bellinger 	 */
3686c66ac9dbSNicholas Bellinger 	switch (sa) {
3687c66ac9dbSNicholas Bellinger 	case PRO_REGISTER:
3688d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_register(cmd,
368933ce6a87SAndy Grover 			res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER);
3690d29a5b6aSChristoph Hellwig 		break;
3691c66ac9dbSNicholas Bellinger 	case PRO_RESERVE:
3692d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3693d29a5b6aSChristoph Hellwig 		break;
3694c66ac9dbSNicholas Bellinger 	case PRO_RELEASE:
3695d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3696d29a5b6aSChristoph Hellwig 		break;
3697c66ac9dbSNicholas Bellinger 	case PRO_CLEAR:
3698d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3699d29a5b6aSChristoph Hellwig 		break;
3700c66ac9dbSNicholas Bellinger 	case PRO_PREEMPT:
3701d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
370233ce6a87SAndy Grover 					res_key, sa_res_key, PREEMPT);
3703d29a5b6aSChristoph Hellwig 		break;
3704c66ac9dbSNicholas Bellinger 	case PRO_PREEMPT_AND_ABORT:
3705d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
370633ce6a87SAndy Grover 					res_key, sa_res_key, PREEMPT_AND_ABORT);
3707d29a5b6aSChristoph Hellwig 		break;
3708c66ac9dbSNicholas Bellinger 	case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
3709d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_register(cmd,
371033ce6a87SAndy Grover 			0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER_AND_IGNORE_EXISTING_KEY);
3711d29a5b6aSChristoph Hellwig 		break;
3712c66ac9dbSNicholas Bellinger 	case PRO_REGISTER_AND_MOVE:
3713d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
3714c66ac9dbSNicholas Bellinger 				sa_res_key, aptpl, unreg);
3715d29a5b6aSChristoph Hellwig 		break;
3716c66ac9dbSNicholas Bellinger 	default:
37176708bb27SAndy Grover 		pr_err("Unknown PERSISTENT_RESERVE_OUT service"
37187ee00317STang Wenji 			" action: 0x%02x\n", sa);
3719de103c93SChristoph Hellwig 		return TCM_INVALID_CDB_FIELD;
3720c66ac9dbSNicholas Bellinger 	}
3721c66ac9dbSNicholas Bellinger 
372253062aceSMike Christie done:
37236bb35e00SChristoph Hellwig 	if (!ret)
372414b40c1eSHannes Reinecke 		target_complete_cmd(cmd, SAM_STAT_GOOD);
3725d29a5b6aSChristoph Hellwig 	return ret;
3726c66ac9dbSNicholas Bellinger }
3727c66ac9dbSNicholas Bellinger 
3728c66ac9dbSNicholas Bellinger /*
3729c66ac9dbSNicholas Bellinger  * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3730c66ac9dbSNicholas Bellinger  *
3731c66ac9dbSNicholas Bellinger  * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3732c66ac9dbSNicholas Bellinger  */
3733de103c93SChristoph Hellwig static sense_reason_t
core_scsi3_pri_read_keys(struct se_cmd * cmd)3734de103c93SChristoph Hellwig core_scsi3_pri_read_keys(struct se_cmd *cmd)
3735c66ac9dbSNicholas Bellinger {
37360fd97ccfSChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
3737c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
373805d1c7c0SAndy Grover 	unsigned char *buf;
3739c66ac9dbSNicholas Bellinger 	u32 add_len = 0, off = 8;
3740c66ac9dbSNicholas Bellinger 
3741c66ac9dbSNicholas Bellinger 	if (cmd->data_length < 8) {
37426708bb27SAndy Grover 		pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3743c66ac9dbSNicholas Bellinger 			" too small\n", cmd->data_length);
3744de103c93SChristoph Hellwig 		return TCM_INVALID_CDB_FIELD;
3745c66ac9dbSNicholas Bellinger 	}
3746c66ac9dbSNicholas Bellinger 
37474949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
3748de103c93SChristoph Hellwig 	if (!buf)
3749de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3750de103c93SChristoph Hellwig 
3751a85d667eSBart Van Assche 	put_unaligned_be32(dev->t10_pr.pr_generation, buf);
3752c66ac9dbSNicholas Bellinger 
37530fd97ccfSChristoph Hellwig 	spin_lock(&dev->t10_pr.registration_lock);
37540fd97ccfSChristoph Hellwig 	list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
3755c66ac9dbSNicholas Bellinger 			pr_reg_list) {
3756c66ac9dbSNicholas Bellinger 		/*
3757c66ac9dbSNicholas Bellinger 		 * Check for overflow of 8byte PRI READ_KEYS payload and
3758c66ac9dbSNicholas Bellinger 		 * next reservation key list descriptor.
3759c66ac9dbSNicholas Bellinger 		 */
376063ce3c38SDavid Disseldorp 		if (off + 8 <= cmd->data_length) {
3761a85d667eSBart Van Assche 			put_unaligned_be64(pr_reg->pr_res_key, &buf[off]);
3762a85d667eSBart Van Assche 			off += 8;
376363ce3c38SDavid Disseldorp 		}
376463ce3c38SDavid Disseldorp 		/*
376563ce3c38SDavid Disseldorp 		 * SPC5r17: 6.16.2 READ KEYS service action
376663ce3c38SDavid Disseldorp 		 * The ADDITIONAL LENGTH field indicates the number of bytes in
376763ce3c38SDavid Disseldorp 		 * the Reservation key list. The contents of the ADDITIONAL
376863ce3c38SDavid Disseldorp 		 * LENGTH field are not altered based on the allocation length
376963ce3c38SDavid Disseldorp 		 */
3770c66ac9dbSNicholas Bellinger 		add_len += 8;
3771c66ac9dbSNicholas Bellinger 	}
37720fd97ccfSChristoph Hellwig 	spin_unlock(&dev->t10_pr.registration_lock);
3773c66ac9dbSNicholas Bellinger 
3774a85d667eSBart Van Assche 	put_unaligned_be32(add_len, &buf[4]);
377514d24e2cSAleksandr Miloserdov 	target_set_cmd_data_length(cmd, 8 + add_len);
3776c66ac9dbSNicholas Bellinger 
37774949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
377805d1c7c0SAndy Grover 
3779c66ac9dbSNicholas Bellinger 	return 0;
3780c66ac9dbSNicholas Bellinger }
3781c66ac9dbSNicholas Bellinger 
3782c66ac9dbSNicholas Bellinger /*
3783c66ac9dbSNicholas Bellinger  * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3784c66ac9dbSNicholas Bellinger  *
3785c66ac9dbSNicholas Bellinger  * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3786c66ac9dbSNicholas Bellinger  */
3787de103c93SChristoph Hellwig static sense_reason_t
core_scsi3_pri_read_reservation(struct se_cmd * cmd)3788de103c93SChristoph Hellwig core_scsi3_pri_read_reservation(struct se_cmd *cmd)
3789c66ac9dbSNicholas Bellinger {
37900fd97ccfSChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
3791c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
379205d1c7c0SAndy Grover 	unsigned char *buf;
3793c66ac9dbSNicholas Bellinger 	u64 pr_res_key;
379414d24e2cSAleksandr Miloserdov 	u32 add_len = 0;
3795c66ac9dbSNicholas Bellinger 
3796c66ac9dbSNicholas Bellinger 	if (cmd->data_length < 8) {
37976708bb27SAndy Grover 		pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
3798c66ac9dbSNicholas Bellinger 			" too small\n", cmd->data_length);
3799de103c93SChristoph Hellwig 		return TCM_INVALID_CDB_FIELD;
3800c66ac9dbSNicholas Bellinger 	}
3801c66ac9dbSNicholas Bellinger 
38024949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
3803de103c93SChristoph Hellwig 	if (!buf)
3804de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3805de103c93SChristoph Hellwig 
3806a85d667eSBart Van Assche 	put_unaligned_be32(dev->t10_pr.pr_generation, &buf[0]);
3807c66ac9dbSNicholas Bellinger 
38080fd97ccfSChristoph Hellwig 	spin_lock(&dev->dev_reservation_lock);
38090fd97ccfSChristoph Hellwig 	pr_reg = dev->dev_pr_res_holder;
3810ee1b1b9cSAndy Grover 	if (pr_reg) {
3811c66ac9dbSNicholas Bellinger 		/*
381214d24e2cSAleksandr Miloserdov 		 * Set the Additional Length to 16 when a reservation is held
3813c66ac9dbSNicholas Bellinger 		 */
381414d24e2cSAleksandr Miloserdov 		add_len = 16;
3815a85d667eSBart Van Assche 		put_unaligned_be32(add_len, &buf[4]);
3816c66ac9dbSNicholas Bellinger 
381705d1c7c0SAndy Grover 		if (cmd->data_length < 22)
381805d1c7c0SAndy Grover 			goto err;
381905d1c7c0SAndy Grover 
3820c66ac9dbSNicholas Bellinger 		/*
3821c66ac9dbSNicholas Bellinger 		 * Set the Reservation key.
3822c66ac9dbSNicholas Bellinger 		 *
3823c66ac9dbSNicholas Bellinger 		 * From spc4r17, section 5.7.10:
3824c66ac9dbSNicholas Bellinger 		 * A persistent reservation holder has its reservation key
3825c66ac9dbSNicholas Bellinger 		 * returned in the parameter data from a PERSISTENT
3826c66ac9dbSNicholas Bellinger 		 * RESERVE IN command with READ RESERVATION service action as
3827c66ac9dbSNicholas Bellinger 		 * follows:
3828c66ac9dbSNicholas Bellinger 		 * a) For a persistent reservation of the type Write Exclusive
3829c66ac9dbSNicholas Bellinger 		 *    - All Registrants or Exclusive Access ­ All Regitrants,
3830c66ac9dbSNicholas Bellinger 		 *      the reservation key shall be set to zero; or
3831c66ac9dbSNicholas Bellinger 		 * b) For all other persistent reservation types, the
3832c66ac9dbSNicholas Bellinger 		 *    reservation key shall be set to the registered
3833c66ac9dbSNicholas Bellinger 		 *    reservation key for the I_T nexus that holds the
3834c66ac9dbSNicholas Bellinger 		 *    persistent reservation.
3835c66ac9dbSNicholas Bellinger 		 */
3836c66ac9dbSNicholas Bellinger 		if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3837c66ac9dbSNicholas Bellinger 		    (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
3838c66ac9dbSNicholas Bellinger 			pr_res_key = 0;
3839c66ac9dbSNicholas Bellinger 		else
3840c66ac9dbSNicholas Bellinger 			pr_res_key = pr_reg->pr_res_key;
3841c66ac9dbSNicholas Bellinger 
3842a85d667eSBart Van Assche 		put_unaligned_be64(pr_res_key, &buf[8]);
3843c66ac9dbSNicholas Bellinger 		/*
3844c66ac9dbSNicholas Bellinger 		 * Set the SCOPE and TYPE
3845c66ac9dbSNicholas Bellinger 		 */
3846c66ac9dbSNicholas Bellinger 		buf[21] = (pr_reg->pr_res_scope & 0xf0) |
3847c66ac9dbSNicholas Bellinger 			  (pr_reg->pr_res_type & 0x0f);
3848c66ac9dbSNicholas Bellinger 	}
384905d1c7c0SAndy Grover 
385014d24e2cSAleksandr Miloserdov 	target_set_cmd_data_length(cmd, 8 + add_len);
385114d24e2cSAleksandr Miloserdov 
385205d1c7c0SAndy Grover err:
38530fd97ccfSChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
38544949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
3855c66ac9dbSNicholas Bellinger 
3856c66ac9dbSNicholas Bellinger 	return 0;
3857c66ac9dbSNicholas Bellinger }
3858c66ac9dbSNicholas Bellinger 
3859c66ac9dbSNicholas Bellinger /*
3860c66ac9dbSNicholas Bellinger  * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3861c66ac9dbSNicholas Bellinger  *
3862c66ac9dbSNicholas Bellinger  * See spc4r17 section 6.13.4 Table 165
3863c66ac9dbSNicholas Bellinger  */
3864de103c93SChristoph Hellwig static sense_reason_t
core_scsi3_pri_report_capabilities(struct se_cmd * cmd)3865de103c93SChristoph Hellwig core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
3866c66ac9dbSNicholas Bellinger {
38675951146dSAndy Grover 	struct se_device *dev = cmd->se_dev;
38680fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
386905d1c7c0SAndy Grover 	unsigned char *buf;
387014d24e2cSAleksandr Miloserdov 	u16 len = 8; /* Hardcoded to 8. */
3871c66ac9dbSNicholas Bellinger 
3872c66ac9dbSNicholas Bellinger 	if (cmd->data_length < 6) {
38736708bb27SAndy Grover 		pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
3874c66ac9dbSNicholas Bellinger 			" %u too small\n", cmd->data_length);
3875de103c93SChristoph Hellwig 		return TCM_INVALID_CDB_FIELD;
3876c66ac9dbSNicholas Bellinger 	}
3877c66ac9dbSNicholas Bellinger 
38784949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
3879de103c93SChristoph Hellwig 	if (!buf)
3880de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
388105d1c7c0SAndy Grover 
388214d24e2cSAleksandr Miloserdov 	put_unaligned_be16(len, &buf[0]);
3883c66ac9dbSNicholas Bellinger 	buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3884c66ac9dbSNicholas Bellinger 	buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3885c66ac9dbSNicholas Bellinger 	buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3886c66ac9dbSNicholas Bellinger 	buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3887c66ac9dbSNicholas Bellinger 	/*
3888c66ac9dbSNicholas Bellinger 	 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3889c66ac9dbSNicholas Bellinger 	 * set the TMV: Task Mask Valid bit.
3890c66ac9dbSNicholas Bellinger 	 */
3891c66ac9dbSNicholas Bellinger 	buf[3] |= 0x80;
3892c66ac9dbSNicholas Bellinger 	/*
3893c66ac9dbSNicholas Bellinger 	 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3894c66ac9dbSNicholas Bellinger 	 */
3895c66ac9dbSNicholas Bellinger 	buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3896c66ac9dbSNicholas Bellinger 	/*
3897c66ac9dbSNicholas Bellinger 	 * PTPL_A: Persistence across Target Power Loss Active bit
3898c66ac9dbSNicholas Bellinger 	 */
3899c66ac9dbSNicholas Bellinger 	if (pr_tmpl->pr_aptpl_active)
3900c66ac9dbSNicholas Bellinger 		buf[3] |= 0x01;
3901c66ac9dbSNicholas Bellinger 	/*
3902c66ac9dbSNicholas Bellinger 	 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
3903c66ac9dbSNicholas Bellinger 	 */
3904c66ac9dbSNicholas Bellinger 	buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3905c66ac9dbSNicholas Bellinger 	buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
3906c66ac9dbSNicholas Bellinger 	buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
3907c66ac9dbSNicholas Bellinger 	buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
3908c66ac9dbSNicholas Bellinger 	buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
3909c66ac9dbSNicholas Bellinger 	buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3910c66ac9dbSNicholas Bellinger 
391114d24e2cSAleksandr Miloserdov 	target_set_cmd_data_length(cmd, len);
391214d24e2cSAleksandr Miloserdov 
39134949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
391405d1c7c0SAndy Grover 
3915c66ac9dbSNicholas Bellinger 	return 0;
3916c66ac9dbSNicholas Bellinger }
3917c66ac9dbSNicholas Bellinger 
3918c66ac9dbSNicholas Bellinger /*
3919c66ac9dbSNicholas Bellinger  * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
3920c66ac9dbSNicholas Bellinger  *
3921c66ac9dbSNicholas Bellinger  * See spc4r17 section 6.13.5 Table 168 and 169
3922c66ac9dbSNicholas Bellinger  */
3923de103c93SChristoph Hellwig static sense_reason_t
core_scsi3_pri_read_full_status(struct se_cmd * cmd)3924de103c93SChristoph Hellwig core_scsi3_pri_read_full_status(struct se_cmd *cmd)
3925c66ac9dbSNicholas Bellinger {
39260fd97ccfSChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
3927c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
3928c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg;
3929c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp;
39300fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
393105d1c7c0SAndy Grover 	unsigned char *buf;
39322650d71eSChristoph Hellwig 	u32 add_desc_len = 0, add_len = 0;
3933c66ac9dbSNicholas Bellinger 	u32 off = 8; /* off into first Full Status descriptor */
3934d16ca7c5SNicholas Bellinger 	int format_code = 0, pr_res_type = 0, pr_res_scope = 0;
39352650d71eSChristoph Hellwig 	int exp_desc_len, desc_len;
3936d16ca7c5SNicholas Bellinger 	bool all_reg = false;
3937c66ac9dbSNicholas Bellinger 
3938c66ac9dbSNicholas Bellinger 	if (cmd->data_length < 8) {
39396708bb27SAndy Grover 		pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
3940c66ac9dbSNicholas Bellinger 			" too small\n", cmd->data_length);
3941de103c93SChristoph Hellwig 		return TCM_INVALID_CDB_FIELD;
3942c66ac9dbSNicholas Bellinger 	}
3943c66ac9dbSNicholas Bellinger 
39444949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
3945de103c93SChristoph Hellwig 	if (!buf)
3946de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
394705d1c7c0SAndy Grover 
3948a85d667eSBart Van Assche 	put_unaligned_be32(dev->t10_pr.pr_generation, &buf[0]);
3949c66ac9dbSNicholas Bellinger 
3950d16ca7c5SNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
3951d16ca7c5SNicholas Bellinger 	if (dev->dev_pr_res_holder) {
3952d16ca7c5SNicholas Bellinger 		struct t10_pr_registration *pr_holder = dev->dev_pr_res_holder;
3953d16ca7c5SNicholas Bellinger 
3954d16ca7c5SNicholas Bellinger 		if (pr_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
3955d16ca7c5SNicholas Bellinger 		    pr_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG) {
3956d16ca7c5SNicholas Bellinger 			all_reg = true;
3957d16ca7c5SNicholas Bellinger 			pr_res_type = pr_holder->pr_res_type;
3958d16ca7c5SNicholas Bellinger 			pr_res_scope = pr_holder->pr_res_scope;
3959d16ca7c5SNicholas Bellinger 		}
3960d16ca7c5SNicholas Bellinger 	}
3961d16ca7c5SNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
3962d16ca7c5SNicholas Bellinger 
3963c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
3964c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3965c66ac9dbSNicholas Bellinger 			&pr_tmpl->registration_list, pr_reg_list) {
3966c66ac9dbSNicholas Bellinger 
3967c66ac9dbSNicholas Bellinger 		se_nacl = pr_reg->pr_reg_nacl;
3968c66ac9dbSNicholas Bellinger 		se_tpg = pr_reg->pr_reg_nacl->se_tpg;
3969c66ac9dbSNicholas Bellinger 		add_desc_len = 0;
3970c66ac9dbSNicholas Bellinger 
397133940d09SJoern Engel 		atomic_inc_mb(&pr_reg->pr_res_holders);
3972c66ac9dbSNicholas Bellinger 		spin_unlock(&pr_tmpl->registration_lock);
3973c66ac9dbSNicholas Bellinger 		/*
3974c66ac9dbSNicholas Bellinger 		 * Determine expected length of $FABRIC_MOD specific
3975c66ac9dbSNicholas Bellinger 		 * TransportID full status descriptor..
3976c66ac9dbSNicholas Bellinger 		 */
39772650d71eSChristoph Hellwig 		exp_desc_len = target_get_pr_transport_id_len(se_nacl, pr_reg,
39782650d71eSChristoph Hellwig 					&format_code);
39792650d71eSChristoph Hellwig 		if (exp_desc_len < 0 ||
39802650d71eSChristoph Hellwig 		    exp_desc_len + add_len > cmd->data_length) {
39816708bb27SAndy Grover 			pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
3982c66ac9dbSNicholas Bellinger 				" out of buffer: %d\n", cmd->data_length);
3983c66ac9dbSNicholas Bellinger 			spin_lock(&pr_tmpl->registration_lock);
398433940d09SJoern Engel 			atomic_dec_mb(&pr_reg->pr_res_holders);
3985c66ac9dbSNicholas Bellinger 			break;
3986c66ac9dbSNicholas Bellinger 		}
3987c66ac9dbSNicholas Bellinger 		/*
3988c66ac9dbSNicholas Bellinger 		 * Set RESERVATION KEY
3989c66ac9dbSNicholas Bellinger 		 */
3990a85d667eSBart Van Assche 		put_unaligned_be64(pr_reg->pr_res_key, &buf[off]);
3991a85d667eSBart Van Assche 		off += 8;
3992c66ac9dbSNicholas Bellinger 		off += 4; /* Skip Over Reserved area */
3993c66ac9dbSNicholas Bellinger 
3994c66ac9dbSNicholas Bellinger 		/*
3995c66ac9dbSNicholas Bellinger 		 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
3996c66ac9dbSNicholas Bellinger 		 */
3997c66ac9dbSNicholas Bellinger 		if (pr_reg->pr_reg_all_tg_pt)
3998c66ac9dbSNicholas Bellinger 			buf[off] = 0x02;
3999c66ac9dbSNicholas Bellinger 		/*
4000c66ac9dbSNicholas Bellinger 		 * The struct se_lun pointer will be present for the
4001c66ac9dbSNicholas Bellinger 		 * reservation holder for PR_HOLDER bit.
4002c66ac9dbSNicholas Bellinger 		 *
4003c66ac9dbSNicholas Bellinger 		 * Also, if this registration is the reservation
4004d16ca7c5SNicholas Bellinger 		 * holder or there is an All Registrants reservation
4005d16ca7c5SNicholas Bellinger 		 * active, fill in SCOPE and TYPE in the next byte.
4006c66ac9dbSNicholas Bellinger 		 */
4007c66ac9dbSNicholas Bellinger 		if (pr_reg->pr_res_holder) {
4008c66ac9dbSNicholas Bellinger 			buf[off++] |= 0x01;
4009c66ac9dbSNicholas Bellinger 			buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
4010c66ac9dbSNicholas Bellinger 				     (pr_reg->pr_res_type & 0x0f);
4011d16ca7c5SNicholas Bellinger 		} else if (all_reg) {
4012d16ca7c5SNicholas Bellinger 			buf[off++] |= 0x01;
4013d16ca7c5SNicholas Bellinger 			buf[off++] = (pr_res_scope & 0xf0) |
4014d16ca7c5SNicholas Bellinger 				     (pr_res_type & 0x0f);
4015d16ca7c5SNicholas Bellinger 		} else {
4016c66ac9dbSNicholas Bellinger 			off += 2;
4017d16ca7c5SNicholas Bellinger 		}
4018c66ac9dbSNicholas Bellinger 
4019c66ac9dbSNicholas Bellinger 		off += 4; /* Skip over reserved area */
4020c66ac9dbSNicholas Bellinger 		/*
4021c66ac9dbSNicholas Bellinger 		 * From spc4r17 6.3.15:
4022c66ac9dbSNicholas Bellinger 		 *
4023c66ac9dbSNicholas Bellinger 		 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4024c66ac9dbSNicholas Bellinger 		 * IDENTIFIER field contains the relative port identifier (see
4025c66ac9dbSNicholas Bellinger 		 * 3.1.120) of the target port that is part of the I_T nexus
4026c66ac9dbSNicholas Bellinger 		 * described by this full status descriptor. If the ALL_TG_PT
4027c66ac9dbSNicholas Bellinger 		 * bit is set to one, the contents of the RELATIVE TARGET PORT
4028c66ac9dbSNicholas Bellinger 		 * IDENTIFIER field are not defined by this standard.
4029c66ac9dbSNicholas Bellinger 		 */
40306708bb27SAndy Grover 		if (!pr_reg->pr_reg_all_tg_pt) {
403179dc9c9eSNicholas Bellinger 			u16 sep_rtpi = pr_reg->tg_pt_sep_rtpi;
4032c66ac9dbSNicholas Bellinger 
4033a85d667eSBart Van Assche 			put_unaligned_be16(sep_rtpi, &buf[off]);
4034a85d667eSBart Van Assche 			off += 2;
4035c66ac9dbSNicholas Bellinger 		} else
403635d1efe8SMasanari Iida 			off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
4037c66ac9dbSNicholas Bellinger 
40382650d71eSChristoph Hellwig 		buf[off+4] = se_tpg->proto_id;
40392650d71eSChristoph Hellwig 
4040c66ac9dbSNicholas Bellinger 		/*
40412650d71eSChristoph Hellwig 		 * Now, have the $FABRIC_MOD fill in the transport ID.
4042c66ac9dbSNicholas Bellinger 		 */
40432650d71eSChristoph Hellwig 		desc_len = target_get_pr_transport_id(se_nacl, pr_reg,
40442650d71eSChristoph Hellwig 				&format_code, &buf[off+4]);
4045c66ac9dbSNicholas Bellinger 
4046c66ac9dbSNicholas Bellinger 		spin_lock(&pr_tmpl->registration_lock);
404733940d09SJoern Engel 		atomic_dec_mb(&pr_reg->pr_res_holders);
40482650d71eSChristoph Hellwig 
40492650d71eSChristoph Hellwig 		if (desc_len < 0)
40502650d71eSChristoph Hellwig 			break;
4051c66ac9dbSNicholas Bellinger 		/*
4052c66ac9dbSNicholas Bellinger 		 * Set the ADDITIONAL DESCRIPTOR LENGTH
4053c66ac9dbSNicholas Bellinger 		 */
4054a85d667eSBart Van Assche 		put_unaligned_be32(desc_len, &buf[off]);
4055c58a252bStangwenji 		off += 4;
4056c66ac9dbSNicholas Bellinger 		/*
4057c66ac9dbSNicholas Bellinger 		 * Size of full desctipor header minus TransportID
4058c66ac9dbSNicholas Bellinger 		 * containing $FABRIC_MOD specific) initiator device/port
4059c66ac9dbSNicholas Bellinger 		 * WWN information.
4060c66ac9dbSNicholas Bellinger 		 *
4061c66ac9dbSNicholas Bellinger 		 *  See spc4r17 Section 6.13.5 Table 169
4062c66ac9dbSNicholas Bellinger 		 */
4063c66ac9dbSNicholas Bellinger 		add_desc_len = (24 + desc_len);
4064c66ac9dbSNicholas Bellinger 
4065c66ac9dbSNicholas Bellinger 		off += desc_len;
4066c66ac9dbSNicholas Bellinger 		add_len += add_desc_len;
4067c66ac9dbSNicholas Bellinger 	}
4068c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
4069c66ac9dbSNicholas Bellinger 	/*
4070c66ac9dbSNicholas Bellinger 	 * Set ADDITIONAL_LENGTH
4071c66ac9dbSNicholas Bellinger 	 */
4072a85d667eSBart Van Assche 	put_unaligned_be32(add_len, &buf[4]);
407314d24e2cSAleksandr Miloserdov 	target_set_cmd_data_length(cmd, 8 + add_len);
4074c66ac9dbSNicholas Bellinger 
40754949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
407605d1c7c0SAndy Grover 
4077c66ac9dbSNicholas Bellinger 	return 0;
4078c66ac9dbSNicholas Bellinger }
4079c66ac9dbSNicholas Bellinger 
target_try_pr_in_pt(struct se_cmd * cmd,u8 sa)408053062aceSMike Christie static sense_reason_t target_try_pr_in_pt(struct se_cmd *cmd, u8 sa)
408153062aceSMike Christie {
408253062aceSMike Christie 	struct exec_cmd_ops *ops = cmd->protocol_data;
408353062aceSMike Christie 	unsigned char *buf;
408453062aceSMike Christie 	sense_reason_t ret;
408553062aceSMike Christie 
408653062aceSMike Christie 	if (cmd->data_length < 8) {
408753062aceSMike Christie 		pr_err("PRIN SA SCSI Data Length: %u too small\n",
408853062aceSMike Christie 		       cmd->data_length);
408953062aceSMike Christie 		return TCM_INVALID_CDB_FIELD;
409053062aceSMike Christie 	}
409153062aceSMike Christie 
409253062aceSMike Christie 	if (!ops->execute_pr_in) {
409353062aceSMike Christie 		pr_err("SPC-3 PR: Device has been configured for PR passthrough but it's not supported by the backend.\n");
409453062aceSMike Christie 		return TCM_UNSUPPORTED_SCSI_OPCODE;
409553062aceSMike Christie 	}
409653062aceSMike Christie 
4097*8455799dSMike Christie 	if (sa == PRI_READ_FULL_STATUS) {
4098*8455799dSMike Christie 		pr_err("SPC-3 PR: PRI_READ_FULL_STATUS is not supported by PR passthrough.\n");
4099*8455799dSMike Christie 		return TCM_UNSUPPORTED_SCSI_OPCODE;
4100*8455799dSMike Christie 	}
4101*8455799dSMike Christie 
410253062aceSMike Christie 	buf = transport_kmap_data_sg(cmd);
410353062aceSMike Christie 	if (!buf)
410453062aceSMike Christie 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
410553062aceSMike Christie 
410653062aceSMike Christie 	ret = ops->execute_pr_in(cmd, sa, buf);
410753062aceSMike Christie 
410853062aceSMike Christie 	transport_kunmap_data_sg(cmd);
410953062aceSMike Christie 	return ret;
411053062aceSMike Christie }
411153062aceSMike Christie 
4112de103c93SChristoph Hellwig sense_reason_t
target_scsi3_emulate_pr_in(struct se_cmd * cmd)4113de103c93SChristoph Hellwig target_scsi3_emulate_pr_in(struct se_cmd *cmd)
4114c66ac9dbSNicholas Bellinger {
411553062aceSMike Christie 	u8 sa = cmd->t_task_cdb[1] & 0x1f;
4116de103c93SChristoph Hellwig 	sense_reason_t ret;
4117e76a35d6SChristoph Hellwig 
4118617c0e06SChristoph Hellwig 	/*
4119617c0e06SChristoph Hellwig 	 * Following spc2r20 5.5.1 Reservations overview:
4120617c0e06SChristoph Hellwig 	 *
4121617c0e06SChristoph Hellwig 	 * If a logical unit has been reserved by any RESERVE command and is
4122617c0e06SChristoph Hellwig 	 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4123617c0e06SChristoph Hellwig 	 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4124617c0e06SChristoph Hellwig 	 * initiator or service action and shall terminate with a RESERVATION
4125617c0e06SChristoph Hellwig 	 * CONFLICT status.
4126617c0e06SChristoph Hellwig 	 */
41270fd97ccfSChristoph Hellwig 	if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
4128617c0e06SChristoph Hellwig 		pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4129617c0e06SChristoph Hellwig 			" SPC-2 reservation is held, returning"
4130617c0e06SChristoph Hellwig 			" RESERVATION_CONFLICT\n");
4131de103c93SChristoph Hellwig 		return TCM_RESERVATION_CONFLICT;
4132617c0e06SChristoph Hellwig 	}
4133617c0e06SChristoph Hellwig 
413453062aceSMike Christie 	if (cmd->se_dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR) {
413553062aceSMike Christie 		ret = target_try_pr_in_pt(cmd, sa);
413653062aceSMike Christie 		goto done;
413753062aceSMike Christie 	}
413853062aceSMike Christie 
413953062aceSMike Christie 	switch (sa) {
4140c66ac9dbSNicholas Bellinger 	case PRI_READ_KEYS:
4141d29a5b6aSChristoph Hellwig 		ret = core_scsi3_pri_read_keys(cmd);
4142d29a5b6aSChristoph Hellwig 		break;
4143c66ac9dbSNicholas Bellinger 	case PRI_READ_RESERVATION:
4144d29a5b6aSChristoph Hellwig 		ret = core_scsi3_pri_read_reservation(cmd);
4145d29a5b6aSChristoph Hellwig 		break;
4146c66ac9dbSNicholas Bellinger 	case PRI_REPORT_CAPABILITIES:
4147d29a5b6aSChristoph Hellwig 		ret = core_scsi3_pri_report_capabilities(cmd);
4148d29a5b6aSChristoph Hellwig 		break;
4149c66ac9dbSNicholas Bellinger 	case PRI_READ_FULL_STATUS:
4150d29a5b6aSChristoph Hellwig 		ret = core_scsi3_pri_read_full_status(cmd);
4151d29a5b6aSChristoph Hellwig 		break;
4152c66ac9dbSNicholas Bellinger 	default:
41536708bb27SAndy Grover 		pr_err("Unknown PERSISTENT_RESERVE_IN service"
4154617c0e06SChristoph Hellwig 			" action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
4155de103c93SChristoph Hellwig 		return TCM_INVALID_CDB_FIELD;
4156c66ac9dbSNicholas Bellinger 	}
4157d29a5b6aSChristoph Hellwig 
415853062aceSMike Christie done:
41596bb35e00SChristoph Hellwig 	if (!ret)
416014b40c1eSHannes Reinecke 		target_complete_cmd(cmd, SAM_STAT_GOOD);
4161d29a5b6aSChristoph Hellwig 	return ret;
4162c66ac9dbSNicholas Bellinger }
4163c66ac9dbSNicholas Bellinger 
4164de103c93SChristoph Hellwig sense_reason_t
target_check_reservation(struct se_cmd * cmd)4165de103c93SChristoph Hellwig target_check_reservation(struct se_cmd *cmd)
4166c66ac9dbSNicholas Bellinger {
4167d977f437SChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
4168de103c93SChristoph Hellwig 	sense_reason_t ret;
4169d977f437SChristoph Hellwig 
4170d977f437SChristoph Hellwig 	if (!cmd->se_sess)
4171c66ac9dbSNicholas Bellinger 		return 0;
4172d977f437SChristoph Hellwig 	if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
4173c66ac9dbSNicholas Bellinger 		return 0;
4174b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr)
4175b49d6f78SDavid Disseldorp 		return 0;
417669088a04SBodo Stroesser 	if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
4177d977f437SChristoph Hellwig 		return 0;
4178c66ac9dbSNicholas Bellinger 
4179d977f437SChristoph Hellwig 	spin_lock(&dev->dev_reservation_lock);
4180d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
4181d977f437SChristoph Hellwig 		ret = target_scsi2_reservation_check(cmd);
4182d977f437SChristoph Hellwig 	else
4183d977f437SChristoph Hellwig 		ret = target_scsi3_pr_reservation_check(cmd);
4184d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
41850fd97ccfSChristoph Hellwig 
4186d977f437SChristoph Hellwig 	return ret;
4187c66ac9dbSNicholas Bellinger }
4188