1c66ac9dbSNicholas Bellinger /*******************************************************************************
2c66ac9dbSNicholas Bellinger  * Filename:  target_core_pr.c
3c66ac9dbSNicholas Bellinger  *
4c66ac9dbSNicholas Bellinger  * This file contains SPC-3 compliant persistent reservations and
5c66ac9dbSNicholas Bellinger  * legacy SPC-2 reservations with compatible reservation handling (CRH=1)
6c66ac9dbSNicholas Bellinger  *
7c66ac9dbSNicholas Bellinger  * Copyright (c) 2009, 2010 Rising Tide Systems
8c66ac9dbSNicholas Bellinger  * Copyright (c) 2009, 2010 Linux-iSCSI.org
9c66ac9dbSNicholas Bellinger  *
10c66ac9dbSNicholas Bellinger  * Nicholas A. Bellinger <nab@kernel.org>
11c66ac9dbSNicholas Bellinger  *
12c66ac9dbSNicholas Bellinger  * This program is free software; you can redistribute it and/or modify
13c66ac9dbSNicholas Bellinger  * it under the terms of the GNU General Public License as published by
14c66ac9dbSNicholas Bellinger  * the Free Software Foundation; either version 2 of the License, or
15c66ac9dbSNicholas Bellinger  * (at your option) any later version.
16c66ac9dbSNicholas Bellinger  *
17c66ac9dbSNicholas Bellinger  * This program is distributed in the hope that it will be useful,
18c66ac9dbSNicholas Bellinger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19c66ac9dbSNicholas Bellinger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20c66ac9dbSNicholas Bellinger  * GNU General Public License for more details.
21c66ac9dbSNicholas Bellinger  *
22c66ac9dbSNicholas Bellinger  * You should have received a copy of the GNU General Public License
23c66ac9dbSNicholas Bellinger  * along with this program; if not, write to the Free Software
24c66ac9dbSNicholas Bellinger  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25c66ac9dbSNicholas Bellinger  *
26c66ac9dbSNicholas Bellinger  ******************************************************************************/
27c66ac9dbSNicholas Bellinger 
28c66ac9dbSNicholas Bellinger #include <linux/slab.h>
29c66ac9dbSNicholas Bellinger #include <linux/spinlock.h>
30c66ac9dbSNicholas Bellinger #include <linux/list.h>
31c66ac9dbSNicholas Bellinger #include <scsi/scsi.h>
32c66ac9dbSNicholas Bellinger #include <scsi/scsi_cmnd.h>
33c66ac9dbSNicholas Bellinger #include <asm/unaligned.h>
34c66ac9dbSNicholas Bellinger 
35c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
36c4795fb2SChristoph Hellwig #include <target/target_core_backend.h>
37c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
38c66ac9dbSNicholas Bellinger #include <target/target_core_configfs.h>
39c66ac9dbSNicholas Bellinger 
40e26d99aeSChristoph Hellwig #include "target_core_internal.h"
41c66ac9dbSNicholas Bellinger #include "target_core_pr.h"
42c66ac9dbSNicholas Bellinger #include "target_core_ua.h"
43c66ac9dbSNicholas Bellinger 
44c66ac9dbSNicholas Bellinger /*
45c66ac9dbSNicholas Bellinger  * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
46c66ac9dbSNicholas Bellinger  */
47c66ac9dbSNicholas Bellinger struct pr_transport_id_holder {
48c66ac9dbSNicholas Bellinger 	int dest_local_nexus;
49c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *dest_pr_reg;
50c66ac9dbSNicholas Bellinger 	struct se_portal_group *dest_tpg;
51c66ac9dbSNicholas Bellinger 	struct se_node_acl *dest_node_acl;
52c66ac9dbSNicholas Bellinger 	struct se_dev_entry *dest_se_deve;
53c66ac9dbSNicholas Bellinger 	struct list_head dest_list;
54c66ac9dbSNicholas Bellinger };
55c66ac9dbSNicholas Bellinger 
56c66ac9dbSNicholas Bellinger int core_pr_dump_initiator_port(
57c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg,
58c66ac9dbSNicholas Bellinger 	char *buf,
59c66ac9dbSNicholas Bellinger 	u32 size)
60c66ac9dbSNicholas Bellinger {
616708bb27SAndy Grover 	if (!pr_reg->isid_present_at_reg)
62c66ac9dbSNicholas Bellinger 		return 0;
63c66ac9dbSNicholas Bellinger 
64c66ac9dbSNicholas Bellinger 	snprintf(buf, size, ",i,0x%s", &pr_reg->pr_reg_isid[0]);
65c66ac9dbSNicholas Bellinger 	return 1;
66c66ac9dbSNicholas Bellinger }
67c66ac9dbSNicholas Bellinger 
68c66ac9dbSNicholas Bellinger static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
69c66ac9dbSNicholas Bellinger 			struct t10_pr_registration *, int);
70c66ac9dbSNicholas Bellinger 
71c66ac9dbSNicholas Bellinger static int core_scsi2_reservation_seq_non_holder(
72c66ac9dbSNicholas Bellinger 	struct se_cmd *cmd,
73c66ac9dbSNicholas Bellinger 	unsigned char *cdb,
74c66ac9dbSNicholas Bellinger 	u32 pr_reg_type)
75c66ac9dbSNicholas Bellinger {
76c66ac9dbSNicholas Bellinger 	switch (cdb[0]) {
77c66ac9dbSNicholas Bellinger 	case INQUIRY:
78c66ac9dbSNicholas Bellinger 	case RELEASE:
79c66ac9dbSNicholas Bellinger 	case RELEASE_10:
80c66ac9dbSNicholas Bellinger 		return 0;
81c66ac9dbSNicholas Bellinger 	default:
82c66ac9dbSNicholas Bellinger 		return 1;
83c66ac9dbSNicholas Bellinger 	}
84c66ac9dbSNicholas Bellinger 
85c66ac9dbSNicholas Bellinger 	return 1;
86c66ac9dbSNicholas Bellinger }
87c66ac9dbSNicholas Bellinger 
88c66ac9dbSNicholas Bellinger static int core_scsi2_reservation_check(struct se_cmd *cmd, u32 *pr_reg_type)
89c66ac9dbSNicholas Bellinger {
90c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
91c66ac9dbSNicholas Bellinger 	struct se_session *sess = cmd->se_sess;
92c66ac9dbSNicholas Bellinger 	int ret;
93c66ac9dbSNicholas Bellinger 
946708bb27SAndy Grover 	if (!sess)
95c66ac9dbSNicholas Bellinger 		return 0;
96c66ac9dbSNicholas Bellinger 
97c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
98c66ac9dbSNicholas Bellinger 	if (!dev->dev_reserved_node_acl || !sess) {
99c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
100c66ac9dbSNicholas Bellinger 		return 0;
101c66ac9dbSNicholas Bellinger 	}
102c66ac9dbSNicholas Bellinger 	if (dev->dev_reserved_node_acl != sess->se_node_acl) {
103c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
104e3d6f909SAndy Grover 		return -EINVAL;
105c66ac9dbSNicholas Bellinger 	}
1060fd97ccfSChristoph Hellwig 	if (!(dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID)) {
107c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
108c66ac9dbSNicholas Bellinger 		return 0;
109c66ac9dbSNicholas Bellinger 	}
110e3d6f909SAndy Grover 	ret = (dev->dev_res_bin_isid == sess->sess_bin_isid) ? 0 : -EINVAL;
111c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
112c66ac9dbSNicholas Bellinger 
113c66ac9dbSNicholas Bellinger 	return ret;
114c66ac9dbSNicholas Bellinger }
115c66ac9dbSNicholas Bellinger 
116eacac00cSChristoph Hellwig static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
117eacac00cSChristoph Hellwig 					struct se_node_acl *, struct se_session *);
118eacac00cSChristoph Hellwig static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
119eacac00cSChristoph Hellwig 
120087a03b3SNicholas Bellinger static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
121eacac00cSChristoph Hellwig {
122eacac00cSChristoph Hellwig 	struct se_session *se_sess = cmd->se_sess;
1230fd97ccfSChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
124eacac00cSChristoph Hellwig 	struct t10_pr_registration *pr_reg;
1250fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
1260fd97ccfSChristoph Hellwig 	int crh = (dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS);
127eacac00cSChristoph Hellwig 	int conflict = 0;
128eacac00cSChristoph Hellwig 
129eacac00cSChristoph Hellwig 	if (!crh)
130087a03b3SNicholas Bellinger 		return -EINVAL;
131eacac00cSChristoph Hellwig 
132eacac00cSChristoph Hellwig 	pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
133eacac00cSChristoph Hellwig 			se_sess);
134eacac00cSChristoph Hellwig 	if (pr_reg) {
135eacac00cSChristoph Hellwig 		/*
136eacac00cSChristoph Hellwig 		 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
137eacac00cSChristoph Hellwig 		 * behavior
138eacac00cSChristoph Hellwig 		 *
139eacac00cSChristoph Hellwig 		 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
140eacac00cSChristoph Hellwig 		 * status, but no reservation shall be established and the
141eacac00cSChristoph Hellwig 		 * persistent reservation shall not be changed, if the command
142eacac00cSChristoph Hellwig 		 * is received from a) and b) below.
143eacac00cSChristoph Hellwig 		 *
144eacac00cSChristoph Hellwig 		 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
145eacac00cSChristoph Hellwig 		 * status, but the persistent reservation shall not be released,
146eacac00cSChristoph Hellwig 		 * if the command is received from a) and b)
147eacac00cSChristoph Hellwig 		 *
148eacac00cSChristoph Hellwig 		 * a) An I_T nexus that is a persistent reservation holder; or
149eacac00cSChristoph Hellwig 		 * b) An I_T nexus that is registered if a registrants only or
150eacac00cSChristoph Hellwig 		 *    all registrants type persistent reservation is present.
151eacac00cSChristoph Hellwig 		 *
152eacac00cSChristoph Hellwig 		 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
153eacac00cSChristoph Hellwig 		 * RELEASE(6) command, or RELEASE(10) command shall be processed
154eacac00cSChristoph Hellwig 		 * as defined in SPC-2.
155eacac00cSChristoph Hellwig 		 */
156eacac00cSChristoph Hellwig 		if (pr_reg->pr_res_holder) {
157eacac00cSChristoph Hellwig 			core_scsi3_put_pr_reg(pr_reg);
158087a03b3SNicholas Bellinger 			return 1;
159eacac00cSChristoph Hellwig 		}
160eacac00cSChristoph Hellwig 		if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
161eacac00cSChristoph Hellwig 		    (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
162eacac00cSChristoph Hellwig 		    (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
163eacac00cSChristoph Hellwig 		    (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
164eacac00cSChristoph Hellwig 			core_scsi3_put_pr_reg(pr_reg);
165087a03b3SNicholas Bellinger 			return 1;
166eacac00cSChristoph Hellwig 		}
167eacac00cSChristoph Hellwig 		core_scsi3_put_pr_reg(pr_reg);
168eacac00cSChristoph Hellwig 		conflict = 1;
169eacac00cSChristoph Hellwig 	} else {
170eacac00cSChristoph Hellwig 		/*
171eacac00cSChristoph Hellwig 		 * Following spc2r20 5.5.1 Reservations overview:
172eacac00cSChristoph Hellwig 		 *
173eacac00cSChristoph Hellwig 		 * If a logical unit has executed a PERSISTENT RESERVE OUT
174eacac00cSChristoph Hellwig 		 * command with the REGISTER or the REGISTER AND IGNORE
175eacac00cSChristoph Hellwig 		 * EXISTING KEY service action and is still registered by any
176eacac00cSChristoph Hellwig 		 * initiator, all RESERVE commands and all RELEASE commands
177eacac00cSChristoph Hellwig 		 * regardless of initiator shall conflict and shall terminate
178eacac00cSChristoph Hellwig 		 * with a RESERVATION CONFLICT status.
179eacac00cSChristoph Hellwig 		 */
180eacac00cSChristoph Hellwig 		spin_lock(&pr_tmpl->registration_lock);
181eacac00cSChristoph Hellwig 		conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
182eacac00cSChristoph Hellwig 		spin_unlock(&pr_tmpl->registration_lock);
183eacac00cSChristoph Hellwig 	}
184eacac00cSChristoph Hellwig 
185eacac00cSChristoph Hellwig 	if (conflict) {
186eacac00cSChristoph Hellwig 		pr_err("Received legacy SPC-2 RESERVE/RELEASE"
187eacac00cSChristoph Hellwig 			" while active SPC-3 registrations exist,"
188eacac00cSChristoph Hellwig 			" returning RESERVATION_CONFLICT\n");
18903e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
190087a03b3SNicholas Bellinger 		return -EBUSY;
191eacac00cSChristoph Hellwig 	}
192eacac00cSChristoph Hellwig 
193087a03b3SNicholas Bellinger 	return 0;
194eacac00cSChristoph Hellwig }
195eacac00cSChristoph Hellwig 
1966bb35e00SChristoph Hellwig int target_scsi2_reservation_release(struct se_cmd *cmd)
197c66ac9dbSNicholas Bellinger {
198c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
199c66ac9dbSNicholas Bellinger 	struct se_session *sess = cmd->se_sess;
200609234e3SWei Yongjun 	struct se_portal_group *tpg;
201087a03b3SNicholas Bellinger 	int ret = 0, rc;
202c66ac9dbSNicholas Bellinger 
203609234e3SWei Yongjun 	if (!sess || !sess->se_tpg)
204d29a5b6aSChristoph Hellwig 		goto out;
205087a03b3SNicholas Bellinger 	rc = target_check_scsi2_reservation_conflict(cmd);
206087a03b3SNicholas Bellinger 	if (rc == 1)
207d29a5b6aSChristoph Hellwig 		goto out;
208087a03b3SNicholas Bellinger 	else if (rc < 0) {
209087a03b3SNicholas Bellinger 		cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
210087a03b3SNicholas Bellinger 		ret = -EINVAL;
211087a03b3SNicholas Bellinger 		goto out;
212087a03b3SNicholas Bellinger 	}
213c66ac9dbSNicholas Bellinger 
214d29a5b6aSChristoph Hellwig 	ret = 0;
215c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
216d29a5b6aSChristoph Hellwig 	if (!dev->dev_reserved_node_acl || !sess)
217d29a5b6aSChristoph Hellwig 		goto out_unlock;
218c66ac9dbSNicholas Bellinger 
219d29a5b6aSChristoph Hellwig 	if (dev->dev_reserved_node_acl != sess->se_node_acl)
220d29a5b6aSChristoph Hellwig 		goto out_unlock;
221d29a5b6aSChristoph Hellwig 
222edc318d9SBernhard Kohl 	if (dev->dev_res_bin_isid != sess->sess_bin_isid)
223edc318d9SBernhard Kohl 		goto out_unlock;
224edc318d9SBernhard Kohl 
225c66ac9dbSNicholas Bellinger 	dev->dev_reserved_node_acl = NULL;
2260fd97ccfSChristoph Hellwig 	dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
2270fd97ccfSChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
228c66ac9dbSNicholas Bellinger 		dev->dev_res_bin_isid = 0;
2290fd97ccfSChristoph Hellwig 		dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS_WITH_ISID;
230c66ac9dbSNicholas Bellinger 	}
231609234e3SWei Yongjun 	tpg = sess->se_tpg;
2326708bb27SAndy Grover 	pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
233e3d6f909SAndy Grover 		" MAPPED LUN: %u for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
234e3d6f909SAndy Grover 		cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
235c66ac9dbSNicholas Bellinger 		sess->se_node_acl->initiatorname);
236c66ac9dbSNicholas Bellinger 
237d29a5b6aSChristoph Hellwig out_unlock:
238d29a5b6aSChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
239d29a5b6aSChristoph Hellwig out:
2406bb35e00SChristoph Hellwig 	if (!ret)
2416bb35e00SChristoph Hellwig 		target_complete_cmd(cmd, GOOD);
242d29a5b6aSChristoph Hellwig 	return ret;
243c66ac9dbSNicholas Bellinger }
244c66ac9dbSNicholas Bellinger 
2456bb35e00SChristoph Hellwig int target_scsi2_reservation_reserve(struct se_cmd *cmd)
246c66ac9dbSNicholas Bellinger {
247c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
248c66ac9dbSNicholas Bellinger 	struct se_session *sess = cmd->se_sess;
249609234e3SWei Yongjun 	struct se_portal_group *tpg;
250087a03b3SNicholas Bellinger 	int ret = 0, rc;
251c66ac9dbSNicholas Bellinger 
252a1d8b49aSAndy Grover 	if ((cmd->t_task_cdb[1] & 0x01) &&
253a1d8b49aSAndy Grover 	    (cmd->t_task_cdb[1] & 0x02)) {
2546708bb27SAndy Grover 		pr_err("LongIO and Obselete Bits set, returning"
255c66ac9dbSNicholas Bellinger 				" ILLEGAL_REQUEST\n");
25603e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
25703e98c9eSNicholas Bellinger 		ret = -EINVAL;
258d29a5b6aSChristoph Hellwig 		goto out;
259c66ac9dbSNicholas Bellinger 	}
260c66ac9dbSNicholas Bellinger 	/*
261c66ac9dbSNicholas Bellinger 	 * This is currently the case for target_core_mod passthrough struct se_cmd
262c66ac9dbSNicholas Bellinger 	 * ops
263c66ac9dbSNicholas Bellinger 	 */
264609234e3SWei Yongjun 	if (!sess || !sess->se_tpg)
265d29a5b6aSChristoph Hellwig 		goto out;
266087a03b3SNicholas Bellinger 	rc = target_check_scsi2_reservation_conflict(cmd);
267087a03b3SNicholas Bellinger 	if (rc == 1)
268d29a5b6aSChristoph Hellwig 		goto out;
269087a03b3SNicholas Bellinger 	else if (rc < 0) {
270087a03b3SNicholas Bellinger 		cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
271087a03b3SNicholas Bellinger 		ret = -EINVAL;
272087a03b3SNicholas Bellinger 		goto out;
273087a03b3SNicholas Bellinger 	}
274c66ac9dbSNicholas Bellinger 
275d29a5b6aSChristoph Hellwig 	ret = 0;
276609234e3SWei Yongjun 	tpg = sess->se_tpg;
277c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
278c66ac9dbSNicholas Bellinger 	if (dev->dev_reserved_node_acl &&
279c66ac9dbSNicholas Bellinger 	   (dev->dev_reserved_node_acl != sess->se_node_acl)) {
2806708bb27SAndy Grover 		pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
281e3d6f909SAndy Grover 			tpg->se_tpg_tfo->get_fabric_name());
2826708bb27SAndy Grover 		pr_err("Original reserver LUN: %u %s\n",
283e3d6f909SAndy Grover 			cmd->se_lun->unpacked_lun,
284c66ac9dbSNicholas Bellinger 			dev->dev_reserved_node_acl->initiatorname);
2856708bb27SAndy Grover 		pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
286e3d6f909SAndy Grover 			" from %s \n", cmd->se_lun->unpacked_lun,
287c66ac9dbSNicholas Bellinger 			cmd->se_deve->mapped_lun,
288c66ac9dbSNicholas Bellinger 			sess->se_node_acl->initiatorname);
28903e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
29003e98c9eSNicholas Bellinger 		ret = -EINVAL;
291d29a5b6aSChristoph Hellwig 		goto out_unlock;
292c66ac9dbSNicholas Bellinger 	}
293c66ac9dbSNicholas Bellinger 
294c66ac9dbSNicholas Bellinger 	dev->dev_reserved_node_acl = sess->se_node_acl;
2950fd97ccfSChristoph Hellwig 	dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS;
296c66ac9dbSNicholas Bellinger 	if (sess->sess_bin_isid != 0) {
297c66ac9dbSNicholas Bellinger 		dev->dev_res_bin_isid = sess->sess_bin_isid;
2980fd97ccfSChristoph Hellwig 		dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS_WITH_ISID;
299c66ac9dbSNicholas Bellinger 	}
3006708bb27SAndy Grover 	pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
301e3d6f909SAndy Grover 		" for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
302e3d6f909SAndy Grover 		cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
303c66ac9dbSNicholas Bellinger 		sess->se_node_acl->initiatorname);
304c66ac9dbSNicholas Bellinger 
305d29a5b6aSChristoph Hellwig out_unlock:
306d29a5b6aSChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
307d29a5b6aSChristoph Hellwig out:
3086bb35e00SChristoph Hellwig 	if (!ret)
3096bb35e00SChristoph Hellwig 		target_complete_cmd(cmd, GOOD);
310d29a5b6aSChristoph Hellwig 	return ret;
311c66ac9dbSNicholas Bellinger }
312c66ac9dbSNicholas Bellinger 
313c66ac9dbSNicholas Bellinger 
314c66ac9dbSNicholas Bellinger /*
315c66ac9dbSNicholas Bellinger  * Begin SPC-3/SPC-4 Persistent Reservations emulation support
316c66ac9dbSNicholas Bellinger  *
317c66ac9dbSNicholas Bellinger  * This function is called by those initiator ports who are *NOT*
318c66ac9dbSNicholas Bellinger  * the active PR reservation holder when a reservation is present.
319c66ac9dbSNicholas Bellinger  */
320c66ac9dbSNicholas Bellinger static int core_scsi3_pr_seq_non_holder(
321c66ac9dbSNicholas Bellinger 	struct se_cmd *cmd,
322c66ac9dbSNicholas Bellinger 	unsigned char *cdb,
323c66ac9dbSNicholas Bellinger 	u32 pr_reg_type)
324c66ac9dbSNicholas Bellinger {
325c66ac9dbSNicholas Bellinger 	struct se_dev_entry *se_deve;
326e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
327c66ac9dbSNicholas Bellinger 	int other_cdb = 0, ignore_reg;
328c66ac9dbSNicholas Bellinger 	int registered_nexus = 0, ret = 1; /* Conflict by default */
329c66ac9dbSNicholas Bellinger 	int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
330c66ac9dbSNicholas Bellinger 	int we = 0; /* Write Exclusive */
331c66ac9dbSNicholas Bellinger 	int legacy = 0; /* Act like a legacy device and return
332c66ac9dbSNicholas Bellinger 			 * RESERVATION CONFLICT on some CDBs */
333c66ac9dbSNicholas Bellinger 	/*
334c66ac9dbSNicholas Bellinger 	 * A legacy SPC-2 reservation is being held.
335c66ac9dbSNicholas Bellinger 	 */
3360fd97ccfSChristoph Hellwig 	if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
337c66ac9dbSNicholas Bellinger 		return core_scsi2_reservation_seq_non_holder(cmd,
338c66ac9dbSNicholas Bellinger 					cdb, pr_reg_type);
339c66ac9dbSNicholas Bellinger 
340f2083241SJörn Engel 	se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
341c66ac9dbSNicholas Bellinger 	/*
342c66ac9dbSNicholas Bellinger 	 * Determine if the registration should be ignored due to
343c66ac9dbSNicholas Bellinger 	 * non-matching ISIDs in core_scsi3_pr_reservation_check().
344c66ac9dbSNicholas Bellinger 	 */
345c66ac9dbSNicholas Bellinger 	ignore_reg = (pr_reg_type & 0x80000000);
346c66ac9dbSNicholas Bellinger 	if (ignore_reg)
347c66ac9dbSNicholas Bellinger 		pr_reg_type &= ~0x80000000;
348c66ac9dbSNicholas Bellinger 
349c66ac9dbSNicholas Bellinger 	switch (pr_reg_type) {
350c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE:
351c66ac9dbSNicholas Bellinger 		we = 1;
352c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS:
353c66ac9dbSNicholas Bellinger 		/*
354c66ac9dbSNicholas Bellinger 		 * Some commands are only allowed for the persistent reservation
355c66ac9dbSNicholas Bellinger 		 * holder.
356c66ac9dbSNicholas Bellinger 		 */
357c66ac9dbSNicholas Bellinger 		if ((se_deve->def_pr_registered) && !(ignore_reg))
358c66ac9dbSNicholas Bellinger 			registered_nexus = 1;
359c66ac9dbSNicholas Bellinger 		break;
360c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
361c66ac9dbSNicholas Bellinger 		we = 1;
362c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
363c66ac9dbSNicholas Bellinger 		/*
364c66ac9dbSNicholas Bellinger 		 * Some commands are only allowed for registered I_T Nexuses.
365c66ac9dbSNicholas Bellinger 		 */
366c66ac9dbSNicholas Bellinger 		reg_only = 1;
367c66ac9dbSNicholas Bellinger 		if ((se_deve->def_pr_registered) && !(ignore_reg))
368c66ac9dbSNicholas Bellinger 			registered_nexus = 1;
369c66ac9dbSNicholas Bellinger 		break;
370c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
371c66ac9dbSNicholas Bellinger 		we = 1;
372c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
373c66ac9dbSNicholas Bellinger 		/*
374c66ac9dbSNicholas Bellinger 		 * Each registered I_T Nexus is a reservation holder.
375c66ac9dbSNicholas Bellinger 		 */
376c66ac9dbSNicholas Bellinger 		all_reg = 1;
377c66ac9dbSNicholas Bellinger 		if ((se_deve->def_pr_registered) && !(ignore_reg))
378c66ac9dbSNicholas Bellinger 			registered_nexus = 1;
379c66ac9dbSNicholas Bellinger 		break;
380c66ac9dbSNicholas Bellinger 	default:
381e3d6f909SAndy Grover 		return -EINVAL;
382c66ac9dbSNicholas Bellinger 	}
383c66ac9dbSNicholas Bellinger 	/*
384c66ac9dbSNicholas Bellinger 	 * Referenced from spc4r17 table 45 for *NON* PR holder access
385c66ac9dbSNicholas Bellinger 	 */
386c66ac9dbSNicholas Bellinger 	switch (cdb[0]) {
387c66ac9dbSNicholas Bellinger 	case SECURITY_PROTOCOL_IN:
388c66ac9dbSNicholas Bellinger 		if (registered_nexus)
389c66ac9dbSNicholas Bellinger 			return 0;
390c66ac9dbSNicholas Bellinger 		ret = (we) ? 0 : 1;
391c66ac9dbSNicholas Bellinger 		break;
392c66ac9dbSNicholas Bellinger 	case MODE_SENSE:
393c66ac9dbSNicholas Bellinger 	case MODE_SENSE_10:
394c66ac9dbSNicholas Bellinger 	case READ_ATTRIBUTE:
395c66ac9dbSNicholas Bellinger 	case READ_BUFFER:
396c66ac9dbSNicholas Bellinger 	case RECEIVE_DIAGNOSTIC:
397c66ac9dbSNicholas Bellinger 		if (legacy) {
398c66ac9dbSNicholas Bellinger 			ret = 1;
399c66ac9dbSNicholas Bellinger 			break;
400c66ac9dbSNicholas Bellinger 		}
401c66ac9dbSNicholas Bellinger 		if (registered_nexus) {
402c66ac9dbSNicholas Bellinger 			ret = 0;
403c66ac9dbSNicholas Bellinger 			break;
404c66ac9dbSNicholas Bellinger 		}
405c66ac9dbSNicholas Bellinger 		ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
406c66ac9dbSNicholas Bellinger 		break;
407c66ac9dbSNicholas Bellinger 	case PERSISTENT_RESERVE_OUT:
408c66ac9dbSNicholas Bellinger 		/*
409c66ac9dbSNicholas Bellinger 		 * This follows PERSISTENT_RESERVE_OUT service actions that
410c66ac9dbSNicholas Bellinger 		 * are allowed in the presence of various reservations.
411c66ac9dbSNicholas Bellinger 		 * See spc4r17, table 46
412c66ac9dbSNicholas Bellinger 		 */
413c66ac9dbSNicholas Bellinger 		switch (cdb[1] & 0x1f) {
414c66ac9dbSNicholas Bellinger 		case PRO_CLEAR:
415c66ac9dbSNicholas Bellinger 		case PRO_PREEMPT:
416c66ac9dbSNicholas Bellinger 		case PRO_PREEMPT_AND_ABORT:
417c66ac9dbSNicholas Bellinger 			ret = (registered_nexus) ? 0 : 1;
418c66ac9dbSNicholas Bellinger 			break;
419c66ac9dbSNicholas Bellinger 		case PRO_REGISTER:
420c66ac9dbSNicholas Bellinger 		case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
421c66ac9dbSNicholas Bellinger 			ret = 0;
422c66ac9dbSNicholas Bellinger 			break;
423c66ac9dbSNicholas Bellinger 		case PRO_REGISTER_AND_MOVE:
424c66ac9dbSNicholas Bellinger 		case PRO_RESERVE:
425c66ac9dbSNicholas Bellinger 			ret = 1;
426c66ac9dbSNicholas Bellinger 			break;
427c66ac9dbSNicholas Bellinger 		case PRO_RELEASE:
428c66ac9dbSNicholas Bellinger 			ret = (registered_nexus) ? 0 : 1;
429c66ac9dbSNicholas Bellinger 			break;
430c66ac9dbSNicholas Bellinger 		default:
4316708bb27SAndy Grover 			pr_err("Unknown PERSISTENT_RESERVE_OUT service"
432c66ac9dbSNicholas Bellinger 				" action: 0x%02x\n", cdb[1] & 0x1f);
433e3d6f909SAndy Grover 			return -EINVAL;
434c66ac9dbSNicholas Bellinger 		}
435c66ac9dbSNicholas Bellinger 		break;
436c66ac9dbSNicholas Bellinger 	case RELEASE:
437c66ac9dbSNicholas Bellinger 	case RELEASE_10:
438eacac00cSChristoph Hellwig 		/* Handled by CRH=1 in target_scsi2_reservation_release() */
439c66ac9dbSNicholas Bellinger 		ret = 0;
440c66ac9dbSNicholas Bellinger 		break;
441c66ac9dbSNicholas Bellinger 	case RESERVE:
442c66ac9dbSNicholas Bellinger 	case RESERVE_10:
443eacac00cSChristoph Hellwig 		/* Handled by CRH=1 in target_scsi2_reservation_reserve() */
444c66ac9dbSNicholas Bellinger 		ret = 0;
445c66ac9dbSNicholas Bellinger 		break;
446c66ac9dbSNicholas Bellinger 	case TEST_UNIT_READY:
447c66ac9dbSNicholas Bellinger 		ret = (legacy) ? 1 : 0; /* Conflict for legacy */
448c66ac9dbSNicholas Bellinger 		break;
449c66ac9dbSNicholas Bellinger 	case MAINTENANCE_IN:
450c66ac9dbSNicholas Bellinger 		switch (cdb[1] & 0x1f) {
451c66ac9dbSNicholas Bellinger 		case MI_MANAGEMENT_PROTOCOL_IN:
452c66ac9dbSNicholas Bellinger 			if (registered_nexus) {
453c66ac9dbSNicholas Bellinger 				ret = 0;
454c66ac9dbSNicholas Bellinger 				break;
455c66ac9dbSNicholas Bellinger 			}
456c66ac9dbSNicholas Bellinger 			ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
457c66ac9dbSNicholas Bellinger 			break;
458c66ac9dbSNicholas Bellinger 		case MI_REPORT_SUPPORTED_OPERATION_CODES:
459c66ac9dbSNicholas Bellinger 		case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
460c66ac9dbSNicholas Bellinger 			if (legacy) {
461c66ac9dbSNicholas Bellinger 				ret = 1;
462c66ac9dbSNicholas Bellinger 				break;
463c66ac9dbSNicholas Bellinger 			}
464c66ac9dbSNicholas Bellinger 			if (registered_nexus) {
465c66ac9dbSNicholas Bellinger 				ret = 0;
466c66ac9dbSNicholas Bellinger 				break;
467c66ac9dbSNicholas Bellinger 			}
468c66ac9dbSNicholas Bellinger 			ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
469c66ac9dbSNicholas Bellinger 			break;
470c66ac9dbSNicholas Bellinger 		case MI_REPORT_ALIASES:
471c66ac9dbSNicholas Bellinger 		case MI_REPORT_IDENTIFYING_INFORMATION:
472c66ac9dbSNicholas Bellinger 		case MI_REPORT_PRIORITY:
473c66ac9dbSNicholas Bellinger 		case MI_REPORT_TARGET_PGS:
474c66ac9dbSNicholas Bellinger 		case MI_REPORT_TIMESTAMP:
475c66ac9dbSNicholas Bellinger 			ret = 0; /* Allowed */
476c66ac9dbSNicholas Bellinger 			break;
477c66ac9dbSNicholas Bellinger 		default:
4786708bb27SAndy Grover 			pr_err("Unknown MI Service Action: 0x%02x\n",
479c66ac9dbSNicholas Bellinger 				(cdb[1] & 0x1f));
480e3d6f909SAndy Grover 			return -EINVAL;
481c66ac9dbSNicholas Bellinger 		}
482c66ac9dbSNicholas Bellinger 		break;
483c66ac9dbSNicholas Bellinger 	case ACCESS_CONTROL_IN:
484c66ac9dbSNicholas Bellinger 	case ACCESS_CONTROL_OUT:
485c66ac9dbSNicholas Bellinger 	case INQUIRY:
486c66ac9dbSNicholas Bellinger 	case LOG_SENSE:
487c66ac9dbSNicholas Bellinger 	case READ_MEDIA_SERIAL_NUMBER:
488c66ac9dbSNicholas Bellinger 	case REPORT_LUNS:
489c66ac9dbSNicholas Bellinger 	case REQUEST_SENSE:
4906816966aSMarco Sanvido 	case PERSISTENT_RESERVE_IN:
491c66ac9dbSNicholas Bellinger 		ret = 0; /*/ Allowed CDBs */
492c66ac9dbSNicholas Bellinger 		break;
493c66ac9dbSNicholas Bellinger 	default:
494c66ac9dbSNicholas Bellinger 		other_cdb = 1;
495c66ac9dbSNicholas Bellinger 		break;
496c66ac9dbSNicholas Bellinger 	}
497c66ac9dbSNicholas Bellinger 	/*
49825985edcSLucas De Marchi 	 * Case where the CDB is explicitly allowed in the above switch
499c66ac9dbSNicholas Bellinger 	 * statement.
500c66ac9dbSNicholas Bellinger 	 */
5016708bb27SAndy Grover 	if (!ret && !other_cdb) {
5026708bb27SAndy Grover 		pr_debug("Allowing explict CDB: 0x%02x for %s"
503c66ac9dbSNicholas Bellinger 			" reservation holder\n", cdb[0],
504c66ac9dbSNicholas Bellinger 			core_scsi3_pr_dump_type(pr_reg_type));
5058b1e1244SAndy Grover 
506c66ac9dbSNicholas Bellinger 		return ret;
507c66ac9dbSNicholas Bellinger 	}
508c66ac9dbSNicholas Bellinger 	/*
509c66ac9dbSNicholas Bellinger 	 * Check if write exclusive initiator ports *NOT* holding the
510c66ac9dbSNicholas Bellinger 	 * WRITE_EXCLUSIVE_* reservation.
511c66ac9dbSNicholas Bellinger 	 */
512ee1b1b9cSAndy Grover 	if (we && !registered_nexus) {
513c66ac9dbSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE) {
514c66ac9dbSNicholas Bellinger 			/*
515c66ac9dbSNicholas Bellinger 			 * Conflict for write exclusive
516c66ac9dbSNicholas Bellinger 			 */
5176708bb27SAndy Grover 			pr_debug("%s Conflict for unregistered nexus"
518c66ac9dbSNicholas Bellinger 				" %s CDB: 0x%02x to %s reservation\n",
519c66ac9dbSNicholas Bellinger 				transport_dump_cmd_direction(cmd),
520c66ac9dbSNicholas Bellinger 				se_sess->se_node_acl->initiatorname, cdb[0],
521c66ac9dbSNicholas Bellinger 				core_scsi3_pr_dump_type(pr_reg_type));
522c66ac9dbSNicholas Bellinger 			return 1;
523c66ac9dbSNicholas Bellinger 		} else {
524c66ac9dbSNicholas Bellinger 			/*
525c66ac9dbSNicholas Bellinger 			 * Allow non WRITE CDBs for all Write Exclusive
526c66ac9dbSNicholas Bellinger 			 * PR TYPEs to pass for registered and
527c66ac9dbSNicholas Bellinger 			 * non-registered_nexuxes NOT holding the reservation.
528c66ac9dbSNicholas Bellinger 			 *
529c66ac9dbSNicholas Bellinger 			 * We only make noise for the unregisterd nexuses,
530c66ac9dbSNicholas Bellinger 			 * as we expect registered non-reservation holding
531c66ac9dbSNicholas Bellinger 			 * nexuses to issue CDBs.
532c66ac9dbSNicholas Bellinger 			 */
5338b1e1244SAndy Grover 
5346708bb27SAndy Grover 			if (!registered_nexus) {
5356708bb27SAndy Grover 				pr_debug("Allowing implict CDB: 0x%02x"
536c66ac9dbSNicholas Bellinger 					" for %s reservation on unregistered"
537c66ac9dbSNicholas Bellinger 					" nexus\n", cdb[0],
538c66ac9dbSNicholas Bellinger 					core_scsi3_pr_dump_type(pr_reg_type));
539c66ac9dbSNicholas Bellinger 			}
5408b1e1244SAndy Grover 
541c66ac9dbSNicholas Bellinger 			return 0;
542c66ac9dbSNicholas Bellinger 		}
543c66ac9dbSNicholas Bellinger 	} else if ((reg_only) || (all_reg)) {
544c66ac9dbSNicholas Bellinger 		if (registered_nexus) {
545c66ac9dbSNicholas Bellinger 			/*
546c66ac9dbSNicholas Bellinger 			 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
547c66ac9dbSNicholas Bellinger 			 * allow commands from registered nexuses.
548c66ac9dbSNicholas Bellinger 			 */
5498b1e1244SAndy Grover 
5506708bb27SAndy Grover 			pr_debug("Allowing implict CDB: 0x%02x for %s"
551c66ac9dbSNicholas Bellinger 				" reservation\n", cdb[0],
552c66ac9dbSNicholas Bellinger 				core_scsi3_pr_dump_type(pr_reg_type));
5538b1e1244SAndy Grover 
554c66ac9dbSNicholas Bellinger 			return 0;
555c66ac9dbSNicholas Bellinger 		}
556c66ac9dbSNicholas Bellinger 	}
5576708bb27SAndy Grover 	pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
558c66ac9dbSNicholas Bellinger 		" for %s reservation\n", transport_dump_cmd_direction(cmd),
559c66ac9dbSNicholas Bellinger 		(registered_nexus) ? "" : "un",
560c66ac9dbSNicholas Bellinger 		se_sess->se_node_acl->initiatorname, cdb[0],
561c66ac9dbSNicholas Bellinger 		core_scsi3_pr_dump_type(pr_reg_type));
562c66ac9dbSNicholas Bellinger 
563c66ac9dbSNicholas Bellinger 	return 1; /* Conflict by default */
564c66ac9dbSNicholas Bellinger }
565c66ac9dbSNicholas Bellinger 
566c66ac9dbSNicholas Bellinger static u32 core_scsi3_pr_generation(struct se_device *dev)
567c66ac9dbSNicholas Bellinger {
568c66ac9dbSNicholas Bellinger 	u32 prg;
5690fd97ccfSChristoph Hellwig 
570c66ac9dbSNicholas Bellinger 	/*
571c66ac9dbSNicholas Bellinger 	 * PRGeneration field shall contain the value of a 32-bit wrapping
572c66ac9dbSNicholas Bellinger 	 * counter mainted by the device server.
573c66ac9dbSNicholas Bellinger 	 *
574c66ac9dbSNicholas Bellinger 	 * Note that this is done regardless of Active Persist across
575c66ac9dbSNicholas Bellinger 	 * Target PowerLoss (APTPL)
576c66ac9dbSNicholas Bellinger 	 *
577c66ac9dbSNicholas Bellinger 	 * See spc4r17 section 6.3.12 READ_KEYS service action
578c66ac9dbSNicholas Bellinger 	 */
579c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
5800fd97ccfSChristoph Hellwig 	prg = dev->t10_pr.pr_generation++;
581c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
582c66ac9dbSNicholas Bellinger 
583c66ac9dbSNicholas Bellinger 	return prg;
584c66ac9dbSNicholas Bellinger }
585c66ac9dbSNicholas Bellinger 
586c66ac9dbSNicholas Bellinger static int core_scsi3_pr_reservation_check(
587c66ac9dbSNicholas Bellinger 	struct se_cmd *cmd,
588c66ac9dbSNicholas Bellinger 	u32 *pr_reg_type)
589c66ac9dbSNicholas Bellinger {
590c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
591c66ac9dbSNicholas Bellinger 	struct se_session *sess = cmd->se_sess;
592c66ac9dbSNicholas Bellinger 	int ret;
593c66ac9dbSNicholas Bellinger 
5946708bb27SAndy Grover 	if (!sess)
595c66ac9dbSNicholas Bellinger 		return 0;
596c66ac9dbSNicholas Bellinger 	/*
597c66ac9dbSNicholas Bellinger 	 * A legacy SPC-2 reservation is being held.
598c66ac9dbSNicholas Bellinger 	 */
5990fd97ccfSChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
600c66ac9dbSNicholas Bellinger 		return core_scsi2_reservation_check(cmd, pr_reg_type);
601c66ac9dbSNicholas Bellinger 
602c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
6036708bb27SAndy Grover 	if (!dev->dev_pr_res_holder) {
604c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
605c66ac9dbSNicholas Bellinger 		return 0;
606c66ac9dbSNicholas Bellinger 	}
607c66ac9dbSNicholas Bellinger 	*pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
608c66ac9dbSNicholas Bellinger 	cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
609c66ac9dbSNicholas Bellinger 	if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl) {
610c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
611e3d6f909SAndy Grover 		return -EINVAL;
612c66ac9dbSNicholas Bellinger 	}
6136708bb27SAndy Grover 	if (!dev->dev_pr_res_holder->isid_present_at_reg) {
614c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
615c66ac9dbSNicholas Bellinger 		return 0;
616c66ac9dbSNicholas Bellinger 	}
617c66ac9dbSNicholas Bellinger 	ret = (dev->dev_pr_res_holder->pr_reg_bin_isid ==
618e3d6f909SAndy Grover 	       sess->sess_bin_isid) ? 0 : -EINVAL;
619c66ac9dbSNicholas Bellinger 	/*
620c66ac9dbSNicholas Bellinger 	 * Use bit in *pr_reg_type to notify ISID mismatch in
621c66ac9dbSNicholas Bellinger 	 * core_scsi3_pr_seq_non_holder().
622c66ac9dbSNicholas Bellinger 	 */
623c66ac9dbSNicholas Bellinger 	if (ret != 0)
624c66ac9dbSNicholas Bellinger 		*pr_reg_type |= 0x80000000;
625c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
626c66ac9dbSNicholas Bellinger 
627c66ac9dbSNicholas Bellinger 	return ret;
628c66ac9dbSNicholas Bellinger }
629c66ac9dbSNicholas Bellinger 
630c66ac9dbSNicholas Bellinger static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
631c66ac9dbSNicholas Bellinger 	struct se_device *dev,
632c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
633c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve,
634c66ac9dbSNicholas Bellinger 	unsigned char *isid,
635c66ac9dbSNicholas Bellinger 	u64 sa_res_key,
636c66ac9dbSNicholas Bellinger 	int all_tg_pt,
637c66ac9dbSNicholas Bellinger 	int aptpl)
638c66ac9dbSNicholas Bellinger {
639c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
640c66ac9dbSNicholas Bellinger 
641c66ac9dbSNicholas Bellinger 	pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
6426708bb27SAndy Grover 	if (!pr_reg) {
6436708bb27SAndy Grover 		pr_err("Unable to allocate struct t10_pr_registration\n");
644c66ac9dbSNicholas Bellinger 		return NULL;
645c66ac9dbSNicholas Bellinger 	}
646c66ac9dbSNicholas Bellinger 
6470fd97ccfSChristoph Hellwig 	pr_reg->pr_aptpl_buf = kzalloc(dev->t10_pr.pr_aptpl_buf_len,
648c66ac9dbSNicholas Bellinger 					GFP_ATOMIC);
6496708bb27SAndy Grover 	if (!pr_reg->pr_aptpl_buf) {
6506708bb27SAndy Grover 		pr_err("Unable to allocate pr_reg->pr_aptpl_buf\n");
651c66ac9dbSNicholas Bellinger 		kmem_cache_free(t10_pr_reg_cache, pr_reg);
652c66ac9dbSNicholas Bellinger 		return NULL;
653c66ac9dbSNicholas Bellinger 	}
654c66ac9dbSNicholas Bellinger 
655c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_list);
656c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
657c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
658c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
659c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
660c66ac9dbSNicholas Bellinger 	atomic_set(&pr_reg->pr_res_holders, 0);
661c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_nacl = nacl;
662c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_deve = deve;
663c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_mapped_lun = deve->mapped_lun;
664c66ac9dbSNicholas Bellinger 	pr_reg->pr_aptpl_target_lun = deve->se_lun->unpacked_lun;
665c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_key = sa_res_key;
666c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_all_tg_pt = all_tg_pt;
667c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_aptpl = aptpl;
668c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_tg_pt_lun = deve->se_lun;
669c66ac9dbSNicholas Bellinger 	/*
670c66ac9dbSNicholas Bellinger 	 * If an ISID value for this SCSI Initiator Port exists,
671c66ac9dbSNicholas Bellinger 	 * save it to the registration now.
672c66ac9dbSNicholas Bellinger 	 */
673c66ac9dbSNicholas Bellinger 	if (isid != NULL) {
674c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
675c66ac9dbSNicholas Bellinger 		snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
676c66ac9dbSNicholas Bellinger 		pr_reg->isid_present_at_reg = 1;
677c66ac9dbSNicholas Bellinger 	}
678c66ac9dbSNicholas Bellinger 
679c66ac9dbSNicholas Bellinger 	return pr_reg;
680c66ac9dbSNicholas Bellinger }
681c66ac9dbSNicholas Bellinger 
682c66ac9dbSNicholas Bellinger static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
683c66ac9dbSNicholas Bellinger static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
684c66ac9dbSNicholas Bellinger 
685c66ac9dbSNicholas Bellinger /*
686c66ac9dbSNicholas Bellinger  * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
687c66ac9dbSNicholas Bellinger  * modes.
688c66ac9dbSNicholas Bellinger  */
689c66ac9dbSNicholas Bellinger static struct t10_pr_registration *__core_scsi3_alloc_registration(
690c66ac9dbSNicholas Bellinger 	struct se_device *dev,
691c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
692c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve,
693c66ac9dbSNicholas Bellinger 	unsigned char *isid,
694c66ac9dbSNicholas Bellinger 	u64 sa_res_key,
695c66ac9dbSNicholas Bellinger 	int all_tg_pt,
696c66ac9dbSNicholas Bellinger 	int aptpl)
697c66ac9dbSNicholas Bellinger {
698c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve_tmp;
699c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl_tmp;
700c66ac9dbSNicholas Bellinger 	struct se_port *port, *port_tmp;
701c66ac9dbSNicholas Bellinger 	struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
702c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
703c66ac9dbSNicholas Bellinger 	int ret;
704c66ac9dbSNicholas Bellinger 	/*
705c66ac9dbSNicholas Bellinger 	 * Create a registration for the I_T Nexus upon which the
706c66ac9dbSNicholas Bellinger 	 * PROUT REGISTER was received.
707c66ac9dbSNicholas Bellinger 	 */
708c66ac9dbSNicholas Bellinger 	pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, deve, isid,
709c66ac9dbSNicholas Bellinger 			sa_res_key, all_tg_pt, aptpl);
7106708bb27SAndy Grover 	if (!pr_reg)
711c66ac9dbSNicholas Bellinger 		return NULL;
712c66ac9dbSNicholas Bellinger 	/*
713c66ac9dbSNicholas Bellinger 	 * Return pointer to pr_reg for ALL_TG_PT=0
714c66ac9dbSNicholas Bellinger 	 */
7156708bb27SAndy Grover 	if (!all_tg_pt)
716c66ac9dbSNicholas Bellinger 		return pr_reg;
717c66ac9dbSNicholas Bellinger 	/*
718c66ac9dbSNicholas Bellinger 	 * Create list of matching SCSI Initiator Port registrations
719c66ac9dbSNicholas Bellinger 	 * for ALL_TG_PT=1
720c66ac9dbSNicholas Bellinger 	 */
721c66ac9dbSNicholas Bellinger 	spin_lock(&dev->se_port_lock);
722c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(port, port_tmp, &dev->dev_sep_list, sep_list) {
723c66ac9dbSNicholas Bellinger 		atomic_inc(&port->sep_tg_pt_ref_cnt);
724c66ac9dbSNicholas Bellinger 		smp_mb__after_atomic_inc();
725c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->se_port_lock);
726c66ac9dbSNicholas Bellinger 
727c66ac9dbSNicholas Bellinger 		spin_lock_bh(&port->sep_alua_lock);
728c66ac9dbSNicholas Bellinger 		list_for_each_entry(deve_tmp, &port->sep_alua_list,
729c66ac9dbSNicholas Bellinger 					alua_port_list) {
730c66ac9dbSNicholas Bellinger 			/*
731c66ac9dbSNicholas Bellinger 			 * This pointer will be NULL for demo mode MappedLUNs
732c66ac9dbSNicholas Bellinger 			 * that have not been make explict via a ConfigFS
733c66ac9dbSNicholas Bellinger 			 * MappedLUN group for the SCSI Initiator Node ACL.
734c66ac9dbSNicholas Bellinger 			 */
7356708bb27SAndy Grover 			if (!deve_tmp->se_lun_acl)
736c66ac9dbSNicholas Bellinger 				continue;
737c66ac9dbSNicholas Bellinger 
738c66ac9dbSNicholas Bellinger 			nacl_tmp = deve_tmp->se_lun_acl->se_lun_nacl;
739c66ac9dbSNicholas Bellinger 			/*
740c66ac9dbSNicholas Bellinger 			 * Skip the matching struct se_node_acl that is allocated
741c66ac9dbSNicholas Bellinger 			 * above..
742c66ac9dbSNicholas Bellinger 			 */
743c66ac9dbSNicholas Bellinger 			if (nacl == nacl_tmp)
744c66ac9dbSNicholas Bellinger 				continue;
745c66ac9dbSNicholas Bellinger 			/*
746c66ac9dbSNicholas Bellinger 			 * Only perform PR registrations for target ports on
747c66ac9dbSNicholas Bellinger 			 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
748c66ac9dbSNicholas Bellinger 			 * arrived.
749c66ac9dbSNicholas Bellinger 			 */
750c66ac9dbSNicholas Bellinger 			if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
751c66ac9dbSNicholas Bellinger 				continue;
752c66ac9dbSNicholas Bellinger 			/*
753c66ac9dbSNicholas Bellinger 			 * Look for a matching Initiator Node ACL in ASCII format
754c66ac9dbSNicholas Bellinger 			 */
755c66ac9dbSNicholas Bellinger 			if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
756c66ac9dbSNicholas Bellinger 				continue;
757c66ac9dbSNicholas Bellinger 
758c66ac9dbSNicholas Bellinger 			atomic_inc(&deve_tmp->pr_ref_count);
759c66ac9dbSNicholas Bellinger 			smp_mb__after_atomic_inc();
760c66ac9dbSNicholas Bellinger 			spin_unlock_bh(&port->sep_alua_lock);
761c66ac9dbSNicholas Bellinger 			/*
762c66ac9dbSNicholas Bellinger 			 * Grab a configfs group dependency that is released
763c66ac9dbSNicholas Bellinger 			 * for the exception path at label out: below, or upon
764c66ac9dbSNicholas Bellinger 			 * completion of adding ALL_TG_PT=1 registrations in
765c66ac9dbSNicholas Bellinger 			 * __core_scsi3_add_registration()
766c66ac9dbSNicholas Bellinger 			 */
767c66ac9dbSNicholas Bellinger 			ret = core_scsi3_lunacl_depend_item(deve_tmp);
768c66ac9dbSNicholas Bellinger 			if (ret < 0) {
7696708bb27SAndy Grover 				pr_err("core_scsi3_lunacl_depend"
770c66ac9dbSNicholas Bellinger 						"_item() failed\n");
771c66ac9dbSNicholas Bellinger 				atomic_dec(&port->sep_tg_pt_ref_cnt);
772c66ac9dbSNicholas Bellinger 				smp_mb__after_atomic_dec();
773c66ac9dbSNicholas Bellinger 				atomic_dec(&deve_tmp->pr_ref_count);
774c66ac9dbSNicholas Bellinger 				smp_mb__after_atomic_dec();
775c66ac9dbSNicholas Bellinger 				goto out;
776c66ac9dbSNicholas Bellinger 			}
777c66ac9dbSNicholas Bellinger 			/*
778c66ac9dbSNicholas Bellinger 			 * Located a matching SCSI Initiator Port on a different
779c66ac9dbSNicholas Bellinger 			 * port, allocate the pr_reg_atp and attach it to the
780c66ac9dbSNicholas Bellinger 			 * pr_reg->pr_reg_atp_list that will be processed once
781c66ac9dbSNicholas Bellinger 			 * the original *pr_reg is processed in
782c66ac9dbSNicholas Bellinger 			 * __core_scsi3_add_registration()
783c66ac9dbSNicholas Bellinger 			 */
784c66ac9dbSNicholas Bellinger 			pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
785c66ac9dbSNicholas Bellinger 						nacl_tmp, deve_tmp, NULL,
786c66ac9dbSNicholas Bellinger 						sa_res_key, all_tg_pt, aptpl);
7876708bb27SAndy Grover 			if (!pr_reg_atp) {
788c66ac9dbSNicholas Bellinger 				atomic_dec(&port->sep_tg_pt_ref_cnt);
789c66ac9dbSNicholas Bellinger 				smp_mb__after_atomic_dec();
790c66ac9dbSNicholas Bellinger 				atomic_dec(&deve_tmp->pr_ref_count);
791c66ac9dbSNicholas Bellinger 				smp_mb__after_atomic_dec();
792c66ac9dbSNicholas Bellinger 				core_scsi3_lunacl_undepend_item(deve_tmp);
793c66ac9dbSNicholas Bellinger 				goto out;
794c66ac9dbSNicholas Bellinger 			}
795c66ac9dbSNicholas Bellinger 
796c66ac9dbSNicholas Bellinger 			list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
797c66ac9dbSNicholas Bellinger 				      &pr_reg->pr_reg_atp_list);
798c66ac9dbSNicholas Bellinger 			spin_lock_bh(&port->sep_alua_lock);
799c66ac9dbSNicholas Bellinger 		}
800c66ac9dbSNicholas Bellinger 		spin_unlock_bh(&port->sep_alua_lock);
801c66ac9dbSNicholas Bellinger 
802c66ac9dbSNicholas Bellinger 		spin_lock(&dev->se_port_lock);
803c66ac9dbSNicholas Bellinger 		atomic_dec(&port->sep_tg_pt_ref_cnt);
804c66ac9dbSNicholas Bellinger 		smp_mb__after_atomic_dec();
805c66ac9dbSNicholas Bellinger 	}
806c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->se_port_lock);
807c66ac9dbSNicholas Bellinger 
808c66ac9dbSNicholas Bellinger 	return pr_reg;
809c66ac9dbSNicholas Bellinger out:
810c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
811c66ac9dbSNicholas Bellinger 			&pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
812c66ac9dbSNicholas Bellinger 		list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
813c66ac9dbSNicholas Bellinger 		core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
814c66ac9dbSNicholas Bellinger 		kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
815c66ac9dbSNicholas Bellinger 	}
816c66ac9dbSNicholas Bellinger 	kmem_cache_free(t10_pr_reg_cache, pr_reg);
817c66ac9dbSNicholas Bellinger 	return NULL;
818c66ac9dbSNicholas Bellinger }
819c66ac9dbSNicholas Bellinger 
820c66ac9dbSNicholas Bellinger int core_scsi3_alloc_aptpl_registration(
821e3d6f909SAndy Grover 	struct t10_reservation *pr_tmpl,
822c66ac9dbSNicholas Bellinger 	u64 sa_res_key,
823c66ac9dbSNicholas Bellinger 	unsigned char *i_port,
824c66ac9dbSNicholas Bellinger 	unsigned char *isid,
825c66ac9dbSNicholas Bellinger 	u32 mapped_lun,
826c66ac9dbSNicholas Bellinger 	unsigned char *t_port,
827c66ac9dbSNicholas Bellinger 	u16 tpgt,
828c66ac9dbSNicholas Bellinger 	u32 target_lun,
829c66ac9dbSNicholas Bellinger 	int res_holder,
830c66ac9dbSNicholas Bellinger 	int all_tg_pt,
831c66ac9dbSNicholas Bellinger 	u8 type)
832c66ac9dbSNicholas Bellinger {
833c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
834c66ac9dbSNicholas Bellinger 
8356708bb27SAndy Grover 	if (!i_port || !t_port || !sa_res_key) {
8366708bb27SAndy Grover 		pr_err("Illegal parameters for APTPL registration\n");
837e3d6f909SAndy Grover 		return -EINVAL;
838c66ac9dbSNicholas Bellinger 	}
839c66ac9dbSNicholas Bellinger 
840c66ac9dbSNicholas Bellinger 	pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
8416708bb27SAndy Grover 	if (!pr_reg) {
8426708bb27SAndy Grover 		pr_err("Unable to allocate struct t10_pr_registration\n");
843e3d6f909SAndy Grover 		return -ENOMEM;
844c66ac9dbSNicholas Bellinger 	}
845c66ac9dbSNicholas Bellinger 	pr_reg->pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len, GFP_KERNEL);
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_reg_tg_pt_lun = NULL;
861c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
862c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_type = type;
863c66ac9dbSNicholas Bellinger 	/*
864c66ac9dbSNicholas Bellinger 	 * If an ISID value had been saved in APTPL metadata for this
865c66ac9dbSNicholas Bellinger 	 * SCSI Initiator Port, restore it now.
866c66ac9dbSNicholas Bellinger 	 */
867c66ac9dbSNicholas Bellinger 	if (isid != NULL) {
868c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
869c66ac9dbSNicholas Bellinger 		snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
870c66ac9dbSNicholas Bellinger 		pr_reg->isid_present_at_reg = 1;
871c66ac9dbSNicholas Bellinger 	}
872c66ac9dbSNicholas Bellinger 	/*
873c66ac9dbSNicholas Bellinger 	 * Copy the i_port and t_port information from caller.
874c66ac9dbSNicholas Bellinger 	 */
875c66ac9dbSNicholas Bellinger 	snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
876c66ac9dbSNicholas Bellinger 	snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
877c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_tpgt = tpgt;
878c66ac9dbSNicholas Bellinger 	/*
879c66ac9dbSNicholas Bellinger 	 * Set pr_res_holder from caller, the pr_reg who is the reservation
880c66ac9dbSNicholas Bellinger 	 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
881c66ac9dbSNicholas Bellinger 	 * the Initiator Node LUN ACL from the fabric module is created for
882c66ac9dbSNicholas Bellinger 	 * this registration.
883c66ac9dbSNicholas Bellinger 	 */
884c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_holder = res_holder;
885c66ac9dbSNicholas Bellinger 
886c66ac9dbSNicholas Bellinger 	list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
8876708bb27SAndy Grover 	pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
888c66ac9dbSNicholas Bellinger 			" metadata\n", (res_holder) ? "+reservation" : "");
889c66ac9dbSNicholas Bellinger 	return 0;
890c66ac9dbSNicholas Bellinger }
891c66ac9dbSNicholas Bellinger 
892c66ac9dbSNicholas Bellinger static void core_scsi3_aptpl_reserve(
893c66ac9dbSNicholas Bellinger 	struct se_device *dev,
894c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
895c66ac9dbSNicholas Bellinger 	struct se_node_acl *node_acl,
896c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg)
897c66ac9dbSNicholas Bellinger {
898c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
899c66ac9dbSNicholas Bellinger 	int prf_isid;
900c66ac9dbSNicholas Bellinger 
901c66ac9dbSNicholas Bellinger 	memset(i_buf, 0, PR_REG_ISID_ID_LEN);
902c66ac9dbSNicholas Bellinger 	prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
903c66ac9dbSNicholas Bellinger 				PR_REG_ISID_ID_LEN);
904c66ac9dbSNicholas Bellinger 
905c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
906c66ac9dbSNicholas Bellinger 	dev->dev_pr_res_holder = pr_reg;
907c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
908c66ac9dbSNicholas Bellinger 
9096708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
910c66ac9dbSNicholas Bellinger 		" new reservation holder TYPE: %s ALL_TG_PT: %d\n",
911e3d6f909SAndy Grover 		tpg->se_tpg_tfo->get_fabric_name(),
912c66ac9dbSNicholas Bellinger 		core_scsi3_pr_dump_type(pr_reg->pr_res_type),
913c66ac9dbSNicholas Bellinger 		(pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
9146708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
915e3d6f909SAndy Grover 		tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname,
916c66ac9dbSNicholas Bellinger 		(prf_isid) ? &i_buf[0] : "");
917c66ac9dbSNicholas Bellinger }
918c66ac9dbSNicholas Bellinger 
919c66ac9dbSNicholas Bellinger static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
920c66ac9dbSNicholas Bellinger 				struct t10_pr_registration *, int, int);
921c66ac9dbSNicholas Bellinger 
922c66ac9dbSNicholas Bellinger static int __core_scsi3_check_aptpl_registration(
923c66ac9dbSNicholas Bellinger 	struct se_device *dev,
924c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
925c66ac9dbSNicholas Bellinger 	struct se_lun *lun,
926c66ac9dbSNicholas Bellinger 	u32 target_lun,
927c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
928c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve)
929c66ac9dbSNicholas Bellinger {
930c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp;
9310fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
932c66ac9dbSNicholas Bellinger 	unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
933c66ac9dbSNicholas Bellinger 	unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
934c66ac9dbSNicholas Bellinger 	u16 tpgt;
935c66ac9dbSNicholas Bellinger 
936c66ac9dbSNicholas Bellinger 	memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
937c66ac9dbSNicholas Bellinger 	memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
938c66ac9dbSNicholas Bellinger 	/*
939c66ac9dbSNicholas Bellinger 	 * Copy Initiator Port information from struct se_node_acl
940c66ac9dbSNicholas Bellinger 	 */
941c66ac9dbSNicholas Bellinger 	snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
942c66ac9dbSNicholas Bellinger 	snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
943e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_wwn(tpg));
944e3d6f909SAndy Grover 	tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
945c66ac9dbSNicholas Bellinger 	/*
946c66ac9dbSNicholas Bellinger 	 * Look for the matching registrations+reservation from those
947c66ac9dbSNicholas Bellinger 	 * created from APTPL metadata.  Note that multiple registrations
948c66ac9dbSNicholas Bellinger 	 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
949c66ac9dbSNicholas Bellinger 	 * TransportIDs.
950c66ac9dbSNicholas Bellinger 	 */
951c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->aptpl_reg_lock);
952c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
953c66ac9dbSNicholas Bellinger 				pr_reg_aptpl_list) {
9546708bb27SAndy Grover 		if (!strcmp(pr_reg->pr_iport, i_port) &&
955c66ac9dbSNicholas Bellinger 		     (pr_reg->pr_res_mapped_lun == deve->mapped_lun) &&
956c66ac9dbSNicholas Bellinger 		    !(strcmp(pr_reg->pr_tport, t_port)) &&
957c66ac9dbSNicholas Bellinger 		     (pr_reg->pr_reg_tpgt == tpgt) &&
958c66ac9dbSNicholas Bellinger 		     (pr_reg->pr_aptpl_target_lun == target_lun)) {
959c66ac9dbSNicholas Bellinger 
960c66ac9dbSNicholas Bellinger 			pr_reg->pr_reg_nacl = nacl;
961c66ac9dbSNicholas Bellinger 			pr_reg->pr_reg_deve = deve;
962c66ac9dbSNicholas Bellinger 			pr_reg->pr_reg_tg_pt_lun = lun;
963c66ac9dbSNicholas Bellinger 
964c66ac9dbSNicholas Bellinger 			list_del(&pr_reg->pr_reg_aptpl_list);
965c66ac9dbSNicholas Bellinger 			spin_unlock(&pr_tmpl->aptpl_reg_lock);
966c66ac9dbSNicholas Bellinger 			/*
967c66ac9dbSNicholas Bellinger 			 * At this point all of the pointers in *pr_reg will
968c66ac9dbSNicholas Bellinger 			 * be setup, so go ahead and add the registration.
969c66ac9dbSNicholas Bellinger 			 */
970c66ac9dbSNicholas Bellinger 
971c66ac9dbSNicholas Bellinger 			__core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
972c66ac9dbSNicholas Bellinger 			/*
973c66ac9dbSNicholas Bellinger 			 * If this registration is the reservation holder,
974c66ac9dbSNicholas Bellinger 			 * make that happen now..
975c66ac9dbSNicholas Bellinger 			 */
976c66ac9dbSNicholas Bellinger 			if (pr_reg->pr_res_holder)
977c66ac9dbSNicholas Bellinger 				core_scsi3_aptpl_reserve(dev, tpg,
978c66ac9dbSNicholas Bellinger 						nacl, pr_reg);
979c66ac9dbSNicholas Bellinger 			/*
980c66ac9dbSNicholas Bellinger 			 * Reenable pr_aptpl_active to accept new metadata
981c66ac9dbSNicholas Bellinger 			 * updates once the SCSI device is active again..
982c66ac9dbSNicholas Bellinger 			 */
983c66ac9dbSNicholas Bellinger 			spin_lock(&pr_tmpl->aptpl_reg_lock);
984c66ac9dbSNicholas Bellinger 			pr_tmpl->pr_aptpl_active = 1;
985c66ac9dbSNicholas Bellinger 		}
986c66ac9dbSNicholas Bellinger 	}
987c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->aptpl_reg_lock);
988c66ac9dbSNicholas Bellinger 
989c66ac9dbSNicholas Bellinger 	return 0;
990c66ac9dbSNicholas Bellinger }
991c66ac9dbSNicholas Bellinger 
992c66ac9dbSNicholas Bellinger int core_scsi3_check_aptpl_registration(
993c66ac9dbSNicholas Bellinger 	struct se_device *dev,
994c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
995c66ac9dbSNicholas Bellinger 	struct se_lun *lun,
996c66ac9dbSNicholas Bellinger 	struct se_lun_acl *lun_acl)
997c66ac9dbSNicholas Bellinger {
998c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl = lun_acl->se_lun_nacl;
999f2083241SJörn Engel 	struct se_dev_entry *deve = nacl->device_list[lun_acl->mapped_lun];
1000c66ac9dbSNicholas Bellinger 
10010fd97ccfSChristoph Hellwig 	if (dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1002c66ac9dbSNicholas Bellinger 		return 0;
1003c66ac9dbSNicholas Bellinger 
1004c66ac9dbSNicholas Bellinger 	return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
1005c66ac9dbSNicholas Bellinger 				lun->unpacked_lun, nacl, deve);
1006c66ac9dbSNicholas Bellinger }
1007c66ac9dbSNicholas Bellinger 
1008c66ac9dbSNicholas Bellinger static void __core_scsi3_dump_registration(
1009c66ac9dbSNicholas Bellinger 	struct target_core_fabric_ops *tfo,
1010c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1011c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
1012c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg,
1013c66ac9dbSNicholas Bellinger 	int register_type)
1014c66ac9dbSNicholas Bellinger {
1015c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg = nacl->se_tpg;
1016c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
1017c66ac9dbSNicholas Bellinger 	int prf_isid;
1018c66ac9dbSNicholas Bellinger 
1019c66ac9dbSNicholas Bellinger 	memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
1020c66ac9dbSNicholas Bellinger 	prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1021c66ac9dbSNicholas Bellinger 				PR_REG_ISID_ID_LEN);
1022c66ac9dbSNicholas Bellinger 
10236708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
1024c66ac9dbSNicholas Bellinger 		" Node: %s%s\n", tfo->get_fabric_name(), (register_type == 2) ?
1025c66ac9dbSNicholas Bellinger 		"_AND_MOVE" : (register_type == 1) ?
1026c66ac9dbSNicholas Bellinger 		"_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
1027c66ac9dbSNicholas Bellinger 		(prf_isid) ? i_buf : "");
10286708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
1029c66ac9dbSNicholas Bellinger 		 tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg),
1030c66ac9dbSNicholas Bellinger 		tfo->tpg_get_tag(se_tpg));
10316708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1032c66ac9dbSNicholas Bellinger 		" Port(s)\n",  tfo->get_fabric_name(),
1033c66ac9dbSNicholas Bellinger 		(pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1034e3d6f909SAndy Grover 		dev->transport->name);
10356708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1036c66ac9dbSNicholas Bellinger 		" 0x%08x  APTPL: %d\n", tfo->get_fabric_name(),
1037c66ac9dbSNicholas Bellinger 		pr_reg->pr_res_key, pr_reg->pr_res_generation,
1038c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_aptpl);
1039c66ac9dbSNicholas Bellinger }
1040c66ac9dbSNicholas Bellinger 
1041c66ac9dbSNicholas Bellinger /*
1042c66ac9dbSNicholas Bellinger  * this function can be called with struct se_device->dev_reservation_lock
1043c66ac9dbSNicholas Bellinger  * when register_move = 1
1044c66ac9dbSNicholas Bellinger  */
1045c66ac9dbSNicholas Bellinger static void __core_scsi3_add_registration(
1046c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1047c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
1048c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg,
1049c66ac9dbSNicholas Bellinger 	int register_type,
1050c66ac9dbSNicholas Bellinger 	int register_move)
1051c66ac9dbSNicholas Bellinger {
1052c66ac9dbSNicholas Bellinger 	struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1053c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
10540fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
1055c66ac9dbSNicholas Bellinger 
1056c66ac9dbSNicholas Bellinger 	/*
1057c66ac9dbSNicholas Bellinger 	 * Increment PRgeneration counter for struct se_device upon a successful
1058c66ac9dbSNicholas Bellinger 	 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1059c66ac9dbSNicholas Bellinger 	 *
1060c66ac9dbSNicholas Bellinger 	 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1061c66ac9dbSNicholas Bellinger 	 * action, the struct se_device->dev_reservation_lock will already be held,
1062c66ac9dbSNicholas Bellinger 	 * so we do not call core_scsi3_pr_generation() which grabs the lock
1063c66ac9dbSNicholas Bellinger 	 * for the REGISTER.
1064c66ac9dbSNicholas Bellinger 	 */
1065c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_generation = (register_move) ?
10660fd97ccfSChristoph Hellwig 			dev->t10_pr.pr_generation++ :
1067c66ac9dbSNicholas Bellinger 			core_scsi3_pr_generation(dev);
1068c66ac9dbSNicholas Bellinger 
1069c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
1070c66ac9dbSNicholas Bellinger 	list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
1071c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_deve->def_pr_registered = 1;
1072c66ac9dbSNicholas Bellinger 
1073c66ac9dbSNicholas Bellinger 	__core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1074c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
1075c66ac9dbSNicholas Bellinger 	/*
1076c66ac9dbSNicholas Bellinger 	 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1077c66ac9dbSNicholas Bellinger 	 */
10786708bb27SAndy Grover 	if (!pr_reg->pr_reg_all_tg_pt || register_move)
1079c66ac9dbSNicholas Bellinger 		return;
1080c66ac9dbSNicholas Bellinger 	/*
1081c66ac9dbSNicholas Bellinger 	 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1082c66ac9dbSNicholas Bellinger 	 * allocated in __core_scsi3_alloc_registration()
1083c66ac9dbSNicholas Bellinger 	 */
1084c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1085c66ac9dbSNicholas Bellinger 			&pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
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 		pr_reg_tmp->pr_reg_deve->def_pr_registered = 1;
1094c66ac9dbSNicholas Bellinger 
1095c66ac9dbSNicholas Bellinger 		__core_scsi3_dump_registration(tfo, dev,
1096c66ac9dbSNicholas Bellinger 				pr_reg_tmp->pr_reg_nacl, pr_reg_tmp,
1097c66ac9dbSNicholas Bellinger 				register_type);
1098c66ac9dbSNicholas Bellinger 		spin_unlock(&pr_tmpl->registration_lock);
1099c66ac9dbSNicholas Bellinger 		/*
1100c66ac9dbSNicholas Bellinger 		 * Drop configfs group dependency reference from
1101c66ac9dbSNicholas Bellinger 		 * __core_scsi3_alloc_registration()
1102c66ac9dbSNicholas Bellinger 		 */
1103c66ac9dbSNicholas Bellinger 		core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1104c66ac9dbSNicholas Bellinger 	}
1105c66ac9dbSNicholas Bellinger }
1106c66ac9dbSNicholas Bellinger 
1107c66ac9dbSNicholas Bellinger static int core_scsi3_alloc_registration(
1108c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1109c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
1110c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve,
1111c66ac9dbSNicholas Bellinger 	unsigned char *isid,
1112c66ac9dbSNicholas Bellinger 	u64 sa_res_key,
1113c66ac9dbSNicholas Bellinger 	int all_tg_pt,
1114c66ac9dbSNicholas Bellinger 	int aptpl,
1115c66ac9dbSNicholas Bellinger 	int register_type,
1116c66ac9dbSNicholas Bellinger 	int register_move)
1117c66ac9dbSNicholas Bellinger {
1118c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1119c66ac9dbSNicholas Bellinger 
1120c66ac9dbSNicholas Bellinger 	pr_reg = __core_scsi3_alloc_registration(dev, nacl, deve, isid,
1121c66ac9dbSNicholas Bellinger 			sa_res_key, all_tg_pt, aptpl);
11226708bb27SAndy Grover 	if (!pr_reg)
1123e3d6f909SAndy Grover 		return -EPERM;
1124c66ac9dbSNicholas Bellinger 
1125c66ac9dbSNicholas Bellinger 	__core_scsi3_add_registration(dev, nacl, pr_reg,
1126c66ac9dbSNicholas Bellinger 			register_type, register_move);
1127c66ac9dbSNicholas Bellinger 	return 0;
1128c66ac9dbSNicholas Bellinger }
1129c66ac9dbSNicholas Bellinger 
1130c66ac9dbSNicholas Bellinger static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1131c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1132c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
1133c66ac9dbSNicholas Bellinger 	unsigned char *isid)
1134c66ac9dbSNicholas Bellinger {
11350fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
1136c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1137c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg;
1138c66ac9dbSNicholas Bellinger 
1139c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
1140c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1141c66ac9dbSNicholas Bellinger 			&pr_tmpl->registration_list, pr_reg_list) {
1142c66ac9dbSNicholas Bellinger 		/*
1143c66ac9dbSNicholas Bellinger 		 * First look for a matching struct se_node_acl
1144c66ac9dbSNicholas Bellinger 		 */
1145c66ac9dbSNicholas Bellinger 		if (pr_reg->pr_reg_nacl != nacl)
1146c66ac9dbSNicholas Bellinger 			continue;
1147c66ac9dbSNicholas Bellinger 
1148c66ac9dbSNicholas Bellinger 		tpg = pr_reg->pr_reg_nacl->se_tpg;
1149c66ac9dbSNicholas Bellinger 		/*
1150c66ac9dbSNicholas Bellinger 		 * If this registration does NOT contain a fabric provided
1151c66ac9dbSNicholas Bellinger 		 * ISID, then we have found a match.
1152c66ac9dbSNicholas Bellinger 		 */
11536708bb27SAndy Grover 		if (!pr_reg->isid_present_at_reg) {
1154c66ac9dbSNicholas Bellinger 			/*
1155c66ac9dbSNicholas Bellinger 			 * Determine if this SCSI device server requires that
1156c66ac9dbSNicholas Bellinger 			 * SCSI Intiatior TransportID w/ ISIDs is enforced
1157c66ac9dbSNicholas Bellinger 			 * for fabric modules (iSCSI) requiring them.
1158c66ac9dbSNicholas Bellinger 			 */
1159e3d6f909SAndy Grover 			if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
11600fd97ccfSChristoph Hellwig 				if (dev->dev_attrib.enforce_pr_isids)
1161c66ac9dbSNicholas Bellinger 					continue;
1162c66ac9dbSNicholas Bellinger 			}
1163c66ac9dbSNicholas Bellinger 			atomic_inc(&pr_reg->pr_res_holders);
1164c66ac9dbSNicholas Bellinger 			smp_mb__after_atomic_inc();
1165c66ac9dbSNicholas Bellinger 			spin_unlock(&pr_tmpl->registration_lock);
1166c66ac9dbSNicholas Bellinger 			return pr_reg;
1167c66ac9dbSNicholas Bellinger 		}
1168c66ac9dbSNicholas Bellinger 		/*
1169c66ac9dbSNicholas Bellinger 		 * If the *pr_reg contains a fabric defined ISID for multi-value
1170c66ac9dbSNicholas Bellinger 		 * SCSI Initiator Port TransportIDs, then we expect a valid
1171c66ac9dbSNicholas Bellinger 		 * matching ISID to be provided by the local SCSI Initiator Port.
1172c66ac9dbSNicholas Bellinger 		 */
11736708bb27SAndy Grover 		if (!isid)
1174c66ac9dbSNicholas Bellinger 			continue;
1175c66ac9dbSNicholas Bellinger 		if (strcmp(isid, pr_reg->pr_reg_isid))
1176c66ac9dbSNicholas Bellinger 			continue;
1177c66ac9dbSNicholas Bellinger 
1178c66ac9dbSNicholas Bellinger 		atomic_inc(&pr_reg->pr_res_holders);
1179c66ac9dbSNicholas Bellinger 		smp_mb__after_atomic_inc();
1180c66ac9dbSNicholas Bellinger 		spin_unlock(&pr_tmpl->registration_lock);
1181c66ac9dbSNicholas Bellinger 		return pr_reg;
1182c66ac9dbSNicholas Bellinger 	}
1183c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
1184c66ac9dbSNicholas Bellinger 
1185c66ac9dbSNicholas Bellinger 	return NULL;
1186c66ac9dbSNicholas Bellinger }
1187c66ac9dbSNicholas Bellinger 
1188c66ac9dbSNicholas Bellinger static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1189c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1190c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
1191c66ac9dbSNicholas Bellinger 	struct se_session *sess)
1192c66ac9dbSNicholas Bellinger {
1193c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg = nacl->se_tpg;
1194c66ac9dbSNicholas Bellinger 	unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1195c66ac9dbSNicholas Bellinger 
1196e3d6f909SAndy Grover 	if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1197c66ac9dbSNicholas Bellinger 		memset(&buf[0], 0, PR_REG_ISID_LEN);
1198e3d6f909SAndy Grover 		tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
1199c66ac9dbSNicholas Bellinger 					PR_REG_ISID_LEN);
1200c66ac9dbSNicholas Bellinger 		isid_ptr = &buf[0];
1201c66ac9dbSNicholas Bellinger 	}
1202c66ac9dbSNicholas Bellinger 
1203c66ac9dbSNicholas Bellinger 	return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1204c66ac9dbSNicholas Bellinger }
1205c66ac9dbSNicholas Bellinger 
1206c66ac9dbSNicholas Bellinger static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1207c66ac9dbSNicholas Bellinger {
1208c66ac9dbSNicholas Bellinger 	atomic_dec(&pr_reg->pr_res_holders);
1209c66ac9dbSNicholas Bellinger 	smp_mb__after_atomic_dec();
1210c66ac9dbSNicholas Bellinger }
1211c66ac9dbSNicholas Bellinger 
1212c66ac9dbSNicholas Bellinger static int core_scsi3_check_implict_release(
1213c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1214c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg)
1215c66ac9dbSNicholas Bellinger {
1216c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1217c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_res_holder;
1218c66ac9dbSNicholas Bellinger 	int ret = 0;
1219c66ac9dbSNicholas Bellinger 
1220c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1221c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
12226708bb27SAndy Grover 	if (!pr_res_holder) {
1223c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
1224c66ac9dbSNicholas Bellinger 		return ret;
1225c66ac9dbSNicholas Bellinger 	}
1226c66ac9dbSNicholas Bellinger 	if (pr_res_holder == pr_reg) {
1227c66ac9dbSNicholas Bellinger 		/*
1228c66ac9dbSNicholas Bellinger 		 * Perform an implict RELEASE if the registration that
1229c66ac9dbSNicholas Bellinger 		 * is being released is holding the reservation.
1230c66ac9dbSNicholas Bellinger 		 *
1231c66ac9dbSNicholas Bellinger 		 * From spc4r17, section 5.7.11.1:
1232c66ac9dbSNicholas Bellinger 		 *
1233c66ac9dbSNicholas Bellinger 		 * e) If the I_T nexus is the persistent reservation holder
1234c66ac9dbSNicholas Bellinger 		 *    and the persistent reservation is not an all registrants
1235c66ac9dbSNicholas Bellinger 		 *    type, then a PERSISTENT RESERVE OUT command with REGISTER
1236c66ac9dbSNicholas Bellinger 		 *    service action or REGISTER AND  IGNORE EXISTING KEY
1237c66ac9dbSNicholas Bellinger 		 *    service action with the SERVICE ACTION RESERVATION KEY
1238c66ac9dbSNicholas Bellinger 		 *    field set to zero (see 5.7.11.3).
1239c66ac9dbSNicholas Bellinger 		 */
1240c66ac9dbSNicholas Bellinger 		__core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0);
1241c66ac9dbSNicholas Bellinger 		ret = 1;
1242c66ac9dbSNicholas Bellinger 		/*
1243c66ac9dbSNicholas Bellinger 		 * For 'All Registrants' reservation types, all existing
1244c66ac9dbSNicholas Bellinger 		 * registrations are still processed as reservation holders
1245c66ac9dbSNicholas Bellinger 		 * in core_scsi3_pr_seq_non_holder() after the initial
1246c66ac9dbSNicholas Bellinger 		 * reservation holder is implictly released here.
1247c66ac9dbSNicholas Bellinger 		 */
1248c66ac9dbSNicholas Bellinger 	} else if (pr_reg->pr_reg_all_tg_pt &&
1249c66ac9dbSNicholas Bellinger 		  (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1250c66ac9dbSNicholas Bellinger 			  pr_reg->pr_reg_nacl->initiatorname)) &&
1251c66ac9dbSNicholas Bellinger 		  (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
12526708bb27SAndy Grover 		pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1253c66ac9dbSNicholas Bellinger 			" UNREGISTER while existing reservation with matching"
1254c66ac9dbSNicholas Bellinger 			" key 0x%016Lx is present from another SCSI Initiator"
1255c66ac9dbSNicholas Bellinger 			" Port\n", pr_reg->pr_res_key);
1256e3d6f909SAndy Grover 		ret = -EPERM;
1257c66ac9dbSNicholas Bellinger 	}
1258c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
1259c66ac9dbSNicholas Bellinger 
1260c66ac9dbSNicholas Bellinger 	return ret;
1261c66ac9dbSNicholas Bellinger }
1262c66ac9dbSNicholas Bellinger 
1263c66ac9dbSNicholas Bellinger /*
1264e3d6f909SAndy Grover  * Called with struct t10_reservation->registration_lock held.
1265c66ac9dbSNicholas Bellinger  */
1266c66ac9dbSNicholas Bellinger static void __core_scsi3_free_registration(
1267c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1268c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg,
1269c66ac9dbSNicholas Bellinger 	struct list_head *preempt_and_abort_list,
1270c66ac9dbSNicholas Bellinger 	int dec_holders)
1271c66ac9dbSNicholas Bellinger {
1272c66ac9dbSNicholas Bellinger 	struct target_core_fabric_ops *tfo =
1273c66ac9dbSNicholas Bellinger 			pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
12740fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
1275c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
1276c66ac9dbSNicholas Bellinger 	int prf_isid;
1277c66ac9dbSNicholas Bellinger 
1278c66ac9dbSNicholas Bellinger 	memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1279c66ac9dbSNicholas Bellinger 	prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1280c66ac9dbSNicholas Bellinger 				PR_REG_ISID_ID_LEN);
1281c66ac9dbSNicholas Bellinger 
1282c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_deve->def_pr_registered = 0;
1283c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_deve->pr_res_key = 0;
1284c66ac9dbSNicholas Bellinger 	list_del(&pr_reg->pr_reg_list);
1285c66ac9dbSNicholas Bellinger 	/*
1286c66ac9dbSNicholas Bellinger 	 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1287c66ac9dbSNicholas Bellinger 	 * so call core_scsi3_put_pr_reg() to decrement our reference.
1288c66ac9dbSNicholas Bellinger 	 */
1289c66ac9dbSNicholas Bellinger 	if (dec_holders)
1290c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg);
1291c66ac9dbSNicholas Bellinger 	/*
1292c66ac9dbSNicholas Bellinger 	 * Wait until all reference from any other I_T nexuses for this
1293c66ac9dbSNicholas Bellinger 	 * *pr_reg have been released.  Because list_del() is called above,
1294c66ac9dbSNicholas Bellinger 	 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1295c66ac9dbSNicholas Bellinger 	 * count back to zero, and we release *pr_reg.
1296c66ac9dbSNicholas Bellinger 	 */
1297c66ac9dbSNicholas Bellinger 	while (atomic_read(&pr_reg->pr_res_holders) != 0) {
1298c66ac9dbSNicholas Bellinger 		spin_unlock(&pr_tmpl->registration_lock);
12996708bb27SAndy Grover 		pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1300c66ac9dbSNicholas Bellinger 				tfo->get_fabric_name());
1301c66ac9dbSNicholas Bellinger 		cpu_relax();
1302c66ac9dbSNicholas Bellinger 		spin_lock(&pr_tmpl->registration_lock);
1303c66ac9dbSNicholas Bellinger 	}
1304c66ac9dbSNicholas Bellinger 
13056708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1306c66ac9dbSNicholas Bellinger 		" Node: %s%s\n", tfo->get_fabric_name(),
1307c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_nacl->initiatorname,
1308c66ac9dbSNicholas Bellinger 		(prf_isid) ? &i_buf[0] : "");
13096708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1310c66ac9dbSNicholas Bellinger 		" Port(s)\n", tfo->get_fabric_name(),
1311c66ac9dbSNicholas Bellinger 		(pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1312e3d6f909SAndy Grover 		dev->transport->name);
13136708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1314c66ac9dbSNicholas Bellinger 		" 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key,
1315c66ac9dbSNicholas Bellinger 		pr_reg->pr_res_generation);
1316c66ac9dbSNicholas Bellinger 
13176708bb27SAndy Grover 	if (!preempt_and_abort_list) {
1318c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_deve = NULL;
1319c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_nacl = NULL;
1320c66ac9dbSNicholas Bellinger 		kfree(pr_reg->pr_aptpl_buf);
1321c66ac9dbSNicholas Bellinger 		kmem_cache_free(t10_pr_reg_cache, pr_reg);
1322c66ac9dbSNicholas Bellinger 		return;
1323c66ac9dbSNicholas Bellinger 	}
1324c66ac9dbSNicholas Bellinger 	/*
1325c66ac9dbSNicholas Bellinger 	 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1326c66ac9dbSNicholas Bellinger 	 * are released once the ABORT_TASK_SET has completed..
1327c66ac9dbSNicholas Bellinger 	 */
1328c66ac9dbSNicholas Bellinger 	list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1329c66ac9dbSNicholas Bellinger }
1330c66ac9dbSNicholas Bellinger 
1331c66ac9dbSNicholas Bellinger void core_scsi3_free_pr_reg_from_nacl(
1332c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1333c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl)
1334c66ac9dbSNicholas Bellinger {
13350fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
1336c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1337c66ac9dbSNicholas Bellinger 	/*
1338c66ac9dbSNicholas Bellinger 	 * If the passed se_node_acl matches the reservation holder,
1339c66ac9dbSNicholas Bellinger 	 * release the reservation.
1340c66ac9dbSNicholas Bellinger 	 */
1341c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1342c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
1343c66ac9dbSNicholas Bellinger 	if ((pr_res_holder != NULL) &&
1344c66ac9dbSNicholas Bellinger 	    (pr_res_holder->pr_reg_nacl == nacl))
1345c66ac9dbSNicholas Bellinger 		__core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0);
1346c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
1347c66ac9dbSNicholas Bellinger 	/*
1348c66ac9dbSNicholas Bellinger 	 * Release any registration associated with the struct se_node_acl.
1349c66ac9dbSNicholas Bellinger 	 */
1350c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
1351c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1352c66ac9dbSNicholas Bellinger 			&pr_tmpl->registration_list, pr_reg_list) {
1353c66ac9dbSNicholas Bellinger 
1354c66ac9dbSNicholas Bellinger 		if (pr_reg->pr_reg_nacl != nacl)
1355c66ac9dbSNicholas Bellinger 			continue;
1356c66ac9dbSNicholas Bellinger 
1357c66ac9dbSNicholas Bellinger 		__core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1358c66ac9dbSNicholas Bellinger 	}
1359c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
1360c66ac9dbSNicholas Bellinger }
1361c66ac9dbSNicholas Bellinger 
1362c66ac9dbSNicholas Bellinger void core_scsi3_free_all_registrations(
1363c66ac9dbSNicholas Bellinger 	struct se_device *dev)
1364c66ac9dbSNicholas Bellinger {
13650fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
1366c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1367c66ac9dbSNicholas Bellinger 
1368c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1369c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
1370c66ac9dbSNicholas Bellinger 	if (pr_res_holder != NULL) {
1371c66ac9dbSNicholas Bellinger 		struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1372c66ac9dbSNicholas Bellinger 		__core_scsi3_complete_pro_release(dev, pr_res_nacl,
1373c66ac9dbSNicholas Bellinger 				pr_res_holder, 0);
1374c66ac9dbSNicholas Bellinger 	}
1375c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
1376c66ac9dbSNicholas Bellinger 
1377c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
1378c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1379c66ac9dbSNicholas Bellinger 			&pr_tmpl->registration_list, pr_reg_list) {
1380c66ac9dbSNicholas Bellinger 
1381c66ac9dbSNicholas Bellinger 		__core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1382c66ac9dbSNicholas Bellinger 	}
1383c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
1384c66ac9dbSNicholas Bellinger 
1385c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->aptpl_reg_lock);
1386c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1387c66ac9dbSNicholas Bellinger 				pr_reg_aptpl_list) {
1388c66ac9dbSNicholas Bellinger 		list_del(&pr_reg->pr_reg_aptpl_list);
1389c66ac9dbSNicholas Bellinger 		kfree(pr_reg->pr_aptpl_buf);
1390c66ac9dbSNicholas Bellinger 		kmem_cache_free(t10_pr_reg_cache, pr_reg);
1391c66ac9dbSNicholas Bellinger 	}
1392c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->aptpl_reg_lock);
1393c66ac9dbSNicholas Bellinger }
1394c66ac9dbSNicholas Bellinger 
1395c66ac9dbSNicholas Bellinger static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1396c66ac9dbSNicholas Bellinger {
1397e3d6f909SAndy Grover 	return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1398c66ac9dbSNicholas Bellinger 			&tpg->tpg_group.cg_item);
1399c66ac9dbSNicholas Bellinger }
1400c66ac9dbSNicholas Bellinger 
1401c66ac9dbSNicholas Bellinger static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1402c66ac9dbSNicholas Bellinger {
1403e3d6f909SAndy Grover 	configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1404c66ac9dbSNicholas Bellinger 			&tpg->tpg_group.cg_item);
1405c66ac9dbSNicholas Bellinger 
1406c66ac9dbSNicholas Bellinger 	atomic_dec(&tpg->tpg_pr_ref_count);
1407c66ac9dbSNicholas Bellinger 	smp_mb__after_atomic_dec();
1408c66ac9dbSNicholas Bellinger }
1409c66ac9dbSNicholas Bellinger 
1410c66ac9dbSNicholas Bellinger static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1411c66ac9dbSNicholas Bellinger {
1412c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg = nacl->se_tpg;
1413c66ac9dbSNicholas Bellinger 
1414c66ac9dbSNicholas Bellinger 	if (nacl->dynamic_node_acl)
1415c66ac9dbSNicholas Bellinger 		return 0;
1416c66ac9dbSNicholas Bellinger 
1417e3d6f909SAndy Grover 	return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1418c66ac9dbSNicholas Bellinger 			&nacl->acl_group.cg_item);
1419c66ac9dbSNicholas Bellinger }
1420c66ac9dbSNicholas Bellinger 
1421c66ac9dbSNicholas Bellinger static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1422c66ac9dbSNicholas Bellinger {
1423c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg = nacl->se_tpg;
1424c66ac9dbSNicholas Bellinger 
1425c66ac9dbSNicholas Bellinger 	if (nacl->dynamic_node_acl) {
1426c66ac9dbSNicholas Bellinger 		atomic_dec(&nacl->acl_pr_ref_count);
1427c66ac9dbSNicholas Bellinger 		smp_mb__after_atomic_dec();
1428c66ac9dbSNicholas Bellinger 		return;
1429c66ac9dbSNicholas Bellinger 	}
1430c66ac9dbSNicholas Bellinger 
1431e3d6f909SAndy Grover 	configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1432c66ac9dbSNicholas Bellinger 			&nacl->acl_group.cg_item);
1433c66ac9dbSNicholas Bellinger 
1434c66ac9dbSNicholas Bellinger 	atomic_dec(&nacl->acl_pr_ref_count);
1435c66ac9dbSNicholas Bellinger 	smp_mb__after_atomic_dec();
1436c66ac9dbSNicholas Bellinger }
1437c66ac9dbSNicholas Bellinger 
1438c66ac9dbSNicholas Bellinger static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1439c66ac9dbSNicholas Bellinger {
1440c66ac9dbSNicholas Bellinger 	struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1441c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl;
1442c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg;
1443c66ac9dbSNicholas Bellinger 	/*
1444c66ac9dbSNicholas Bellinger 	 * For nacl->dynamic_node_acl=1
1445c66ac9dbSNicholas Bellinger 	 */
14466708bb27SAndy Grover 	if (!lun_acl)
1447c66ac9dbSNicholas Bellinger 		return 0;
1448c66ac9dbSNicholas Bellinger 
1449c66ac9dbSNicholas Bellinger 	nacl = lun_acl->se_lun_nacl;
1450c66ac9dbSNicholas Bellinger 	tpg = nacl->se_tpg;
1451c66ac9dbSNicholas Bellinger 
1452e3d6f909SAndy Grover 	return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1453c66ac9dbSNicholas Bellinger 			&lun_acl->se_lun_group.cg_item);
1454c66ac9dbSNicholas Bellinger }
1455c66ac9dbSNicholas Bellinger 
1456c66ac9dbSNicholas Bellinger static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1457c66ac9dbSNicholas Bellinger {
1458c66ac9dbSNicholas Bellinger 	struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1459c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl;
1460c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg;
1461c66ac9dbSNicholas Bellinger 	/*
1462c66ac9dbSNicholas Bellinger 	 * For nacl->dynamic_node_acl=1
1463c66ac9dbSNicholas Bellinger 	 */
14646708bb27SAndy Grover 	if (!lun_acl) {
1465c66ac9dbSNicholas Bellinger 		atomic_dec(&se_deve->pr_ref_count);
1466c66ac9dbSNicholas Bellinger 		smp_mb__after_atomic_dec();
1467c66ac9dbSNicholas Bellinger 		return;
1468c66ac9dbSNicholas Bellinger 	}
1469c66ac9dbSNicholas Bellinger 	nacl = lun_acl->se_lun_nacl;
1470c66ac9dbSNicholas Bellinger 	tpg = nacl->se_tpg;
1471c66ac9dbSNicholas Bellinger 
1472e3d6f909SAndy Grover 	configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1473c66ac9dbSNicholas Bellinger 			&lun_acl->se_lun_group.cg_item);
1474c66ac9dbSNicholas Bellinger 
1475c66ac9dbSNicholas Bellinger 	atomic_dec(&se_deve->pr_ref_count);
1476c66ac9dbSNicholas Bellinger 	smp_mb__after_atomic_dec();
1477c66ac9dbSNicholas Bellinger }
1478c66ac9dbSNicholas Bellinger 
1479c66ac9dbSNicholas Bellinger static int core_scsi3_decode_spec_i_port(
1480c66ac9dbSNicholas Bellinger 	struct se_cmd *cmd,
1481c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
1482c66ac9dbSNicholas Bellinger 	unsigned char *l_isid,
1483c66ac9dbSNicholas Bellinger 	u64 sa_res_key,
1484c66ac9dbSNicholas Bellinger 	int all_tg_pt,
1485c66ac9dbSNicholas Bellinger 	int aptpl)
1486c66ac9dbSNicholas Bellinger {
14875951146dSAndy Grover 	struct se_device *dev = cmd->se_dev;
1488c66ac9dbSNicholas Bellinger 	struct se_port *tmp_port;
1489c66ac9dbSNicholas Bellinger 	struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
1490e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
1491c66ac9dbSNicholas Bellinger 	struct se_node_acl *dest_node_acl = NULL;
1492c66ac9dbSNicholas Bellinger 	struct se_dev_entry *dest_se_deve = NULL, *local_se_deve;
1493c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1494c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1495d0f474e5SRoland Dreier 	LIST_HEAD(tid_dest_list);
1496c66ac9dbSNicholas Bellinger 	struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
1497c66ac9dbSNicholas Bellinger 	struct target_core_fabric_ops *tmp_tf_ops;
149805d1c7c0SAndy Grover 	unsigned char *buf;
1499c66ac9dbSNicholas Bellinger 	unsigned char *ptr, *i_str = NULL, proto_ident, tmp_proto_ident;
1500c66ac9dbSNicholas Bellinger 	char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
1501c66ac9dbSNicholas Bellinger 	u32 tpdl, tid_len = 0;
1502c66ac9dbSNicholas Bellinger 	int ret, dest_local_nexus, prf_isid;
1503c66ac9dbSNicholas Bellinger 	u32 dest_rtpi = 0;
1504c66ac9dbSNicholas Bellinger 
1505c66ac9dbSNicholas Bellinger 	memset(dest_iport, 0, 64);
1506c66ac9dbSNicholas Bellinger 
1507f2083241SJörn Engel 	local_se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
1508c66ac9dbSNicholas Bellinger 	/*
1509c66ac9dbSNicholas Bellinger 	 * Allocate a struct pr_transport_id_holder and setup the
1510c66ac9dbSNicholas Bellinger 	 * local_node_acl and local_se_deve pointers and add to
1511c66ac9dbSNicholas Bellinger 	 * struct list_head tid_dest_list for add registration
1512c66ac9dbSNicholas Bellinger 	 * processing in the loop of tid_dest_list below.
1513c66ac9dbSNicholas Bellinger 	 */
1514c66ac9dbSNicholas Bellinger 	tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
15156708bb27SAndy Grover 	if (!tidh_new) {
15166708bb27SAndy Grover 		pr_err("Unable to allocate tidh_new\n");
151703e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
151803e98c9eSNicholas Bellinger 		return -EINVAL;
1519c66ac9dbSNicholas Bellinger 	}
1520c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&tidh_new->dest_list);
1521c66ac9dbSNicholas Bellinger 	tidh_new->dest_tpg = tpg;
1522c66ac9dbSNicholas Bellinger 	tidh_new->dest_node_acl = se_sess->se_node_acl;
1523c66ac9dbSNicholas Bellinger 	tidh_new->dest_se_deve = local_se_deve;
1524c66ac9dbSNicholas Bellinger 
15255951146dSAndy Grover 	local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1526c66ac9dbSNicholas Bellinger 				se_sess->se_node_acl, local_se_deve, l_isid,
1527c66ac9dbSNicholas Bellinger 				sa_res_key, all_tg_pt, aptpl);
15286708bb27SAndy Grover 	if (!local_pr_reg) {
1529c66ac9dbSNicholas Bellinger 		kfree(tidh_new);
153003e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
153103e98c9eSNicholas Bellinger 		return -ENOMEM;
1532c66ac9dbSNicholas Bellinger 	}
1533c66ac9dbSNicholas Bellinger 	tidh_new->dest_pr_reg = local_pr_reg;
1534c66ac9dbSNicholas Bellinger 	/*
1535c66ac9dbSNicholas Bellinger 	 * The local I_T nexus does not hold any configfs dependances,
1536c66ac9dbSNicholas Bellinger 	 * so we set tid_h->dest_local_nexus=1 to prevent the
1537c66ac9dbSNicholas Bellinger 	 * configfs_undepend_item() calls in the tid_dest_list loops below.
1538c66ac9dbSNicholas Bellinger 	 */
1539c66ac9dbSNicholas Bellinger 	tidh_new->dest_local_nexus = 1;
1540c66ac9dbSNicholas Bellinger 	list_add_tail(&tidh_new->dest_list, &tid_dest_list);
154105d1c7c0SAndy Grover 
15420d7f1299SPaolo Bonzini 	if (cmd->data_length < 28) {
15430d7f1299SPaolo Bonzini 		pr_warn("SPC-PR: Received PR OUT parameter list"
15440d7f1299SPaolo Bonzini 			" length too small: %u\n", cmd->data_length);
15450d7f1299SPaolo Bonzini 		cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
15460d7f1299SPaolo Bonzini 		ret = -EINVAL;
15470d7f1299SPaolo Bonzini 		goto out;
15480d7f1299SPaolo Bonzini 	}
15490d7f1299SPaolo Bonzini 
15504949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
1551c66ac9dbSNicholas Bellinger 	/*
1552c66ac9dbSNicholas Bellinger 	 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1553c66ac9dbSNicholas Bellinger 	 * first extract TransportID Parameter Data Length, and make sure
1554c66ac9dbSNicholas Bellinger 	 * the value matches up to the SCSI expected data transfer length.
1555c66ac9dbSNicholas Bellinger 	 */
1556c66ac9dbSNicholas Bellinger 	tpdl = (buf[24] & 0xff) << 24;
1557c66ac9dbSNicholas Bellinger 	tpdl |= (buf[25] & 0xff) << 16;
1558c66ac9dbSNicholas Bellinger 	tpdl |= (buf[26] & 0xff) << 8;
1559c66ac9dbSNicholas Bellinger 	tpdl |= buf[27] & 0xff;
1560c66ac9dbSNicholas Bellinger 
1561c66ac9dbSNicholas Bellinger 	if ((tpdl + 28) != cmd->data_length) {
15626708bb27SAndy Grover 		pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1563c66ac9dbSNicholas Bellinger 			" does not equal CDB data_length: %u\n", tpdl,
1564c66ac9dbSNicholas Bellinger 			cmd->data_length);
156503e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
156603e98c9eSNicholas Bellinger 		ret = -EINVAL;
1567c66ac9dbSNicholas Bellinger 		goto out;
1568c66ac9dbSNicholas Bellinger 	}
1569c66ac9dbSNicholas Bellinger 	/*
1570c66ac9dbSNicholas Bellinger 	 * Start processing the received transport IDs using the
1571c66ac9dbSNicholas Bellinger 	 * receiving I_T Nexus portal's fabric dependent methods to
1572c66ac9dbSNicholas Bellinger 	 * obtain the SCSI Initiator Port/Device Identifiers.
1573c66ac9dbSNicholas Bellinger 	 */
1574c66ac9dbSNicholas Bellinger 	ptr = &buf[28];
1575c66ac9dbSNicholas Bellinger 
1576c66ac9dbSNicholas Bellinger 	while (tpdl > 0) {
1577c66ac9dbSNicholas Bellinger 		proto_ident = (ptr[0] & 0x0f);
1578c66ac9dbSNicholas Bellinger 		dest_tpg = NULL;
1579c66ac9dbSNicholas Bellinger 
1580c66ac9dbSNicholas Bellinger 		spin_lock(&dev->se_port_lock);
1581c66ac9dbSNicholas Bellinger 		list_for_each_entry(tmp_port, &dev->dev_sep_list, sep_list) {
1582c66ac9dbSNicholas Bellinger 			tmp_tpg = tmp_port->sep_tpg;
15836708bb27SAndy Grover 			if (!tmp_tpg)
1584c66ac9dbSNicholas Bellinger 				continue;
1585e3d6f909SAndy Grover 			tmp_tf_ops = tmp_tpg->se_tpg_tfo;
15866708bb27SAndy Grover 			if (!tmp_tf_ops)
1587c66ac9dbSNicholas Bellinger 				continue;
15886708bb27SAndy Grover 			if (!tmp_tf_ops->get_fabric_proto_ident ||
15896708bb27SAndy Grover 			    !tmp_tf_ops->tpg_parse_pr_out_transport_id)
1590c66ac9dbSNicholas Bellinger 				continue;
1591c66ac9dbSNicholas Bellinger 			/*
1592c66ac9dbSNicholas Bellinger 			 * Look for the matching proto_ident provided by
1593c66ac9dbSNicholas Bellinger 			 * the received TransportID
1594c66ac9dbSNicholas Bellinger 			 */
1595c66ac9dbSNicholas Bellinger 			tmp_proto_ident = tmp_tf_ops->get_fabric_proto_ident(tmp_tpg);
1596c66ac9dbSNicholas Bellinger 			if (tmp_proto_ident != proto_ident)
1597c66ac9dbSNicholas Bellinger 				continue;
1598c66ac9dbSNicholas Bellinger 			dest_rtpi = tmp_port->sep_rtpi;
1599c66ac9dbSNicholas Bellinger 
1600c66ac9dbSNicholas Bellinger 			i_str = tmp_tf_ops->tpg_parse_pr_out_transport_id(
1601c66ac9dbSNicholas Bellinger 					tmp_tpg, (const char *)ptr, &tid_len,
1602c66ac9dbSNicholas Bellinger 					&iport_ptr);
16036708bb27SAndy Grover 			if (!i_str)
1604c66ac9dbSNicholas Bellinger 				continue;
1605c66ac9dbSNicholas Bellinger 
1606c66ac9dbSNicholas Bellinger 			atomic_inc(&tmp_tpg->tpg_pr_ref_count);
1607c66ac9dbSNicholas Bellinger 			smp_mb__after_atomic_inc();
1608c66ac9dbSNicholas Bellinger 			spin_unlock(&dev->se_port_lock);
1609c66ac9dbSNicholas Bellinger 
1610c66ac9dbSNicholas Bellinger 			ret = core_scsi3_tpg_depend_item(tmp_tpg);
1611c66ac9dbSNicholas Bellinger 			if (ret != 0) {
16126708bb27SAndy Grover 				pr_err(" core_scsi3_tpg_depend_item()"
1613c66ac9dbSNicholas Bellinger 					" for tmp_tpg\n");
1614c66ac9dbSNicholas Bellinger 				atomic_dec(&tmp_tpg->tpg_pr_ref_count);
1615c66ac9dbSNicholas Bellinger 				smp_mb__after_atomic_dec();
161603e98c9eSNicholas Bellinger 				cmd->scsi_sense_reason =
161703e98c9eSNicholas Bellinger 					TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
161803e98c9eSNicholas Bellinger 				ret = -EINVAL;
1619c66ac9dbSNicholas Bellinger 				goto out;
1620c66ac9dbSNicholas Bellinger 			}
1621c66ac9dbSNicholas Bellinger 			/*
162235d1efe8SMasanari Iida 			 * Locate the destination initiator ACL to be registered
1623c66ac9dbSNicholas Bellinger 			 * from the decoded fabric module specific TransportID
1624c66ac9dbSNicholas Bellinger 			 * at *i_str.
1625c66ac9dbSNicholas Bellinger 			 */
162628638887SRoland Dreier 			spin_lock_irq(&tmp_tpg->acl_node_lock);
1627c66ac9dbSNicholas Bellinger 			dest_node_acl = __core_tpg_get_initiator_node_acl(
1628c66ac9dbSNicholas Bellinger 						tmp_tpg, i_str);
1629c66ac9dbSNicholas Bellinger 			if (dest_node_acl) {
1630c66ac9dbSNicholas Bellinger 				atomic_inc(&dest_node_acl->acl_pr_ref_count);
1631c66ac9dbSNicholas Bellinger 				smp_mb__after_atomic_inc();
1632c66ac9dbSNicholas Bellinger 			}
163328638887SRoland Dreier 			spin_unlock_irq(&tmp_tpg->acl_node_lock);
1634c66ac9dbSNicholas Bellinger 
16356708bb27SAndy Grover 			if (!dest_node_acl) {
1636c66ac9dbSNicholas Bellinger 				core_scsi3_tpg_undepend_item(tmp_tpg);
1637c66ac9dbSNicholas Bellinger 				spin_lock(&dev->se_port_lock);
1638c66ac9dbSNicholas Bellinger 				continue;
1639c66ac9dbSNicholas Bellinger 			}
1640c66ac9dbSNicholas Bellinger 
1641c66ac9dbSNicholas Bellinger 			ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
1642c66ac9dbSNicholas Bellinger 			if (ret != 0) {
16436708bb27SAndy Grover 				pr_err("configfs_depend_item() failed"
1644c66ac9dbSNicholas Bellinger 					" for dest_node_acl->acl_group\n");
1645c66ac9dbSNicholas Bellinger 				atomic_dec(&dest_node_acl->acl_pr_ref_count);
1646c66ac9dbSNicholas Bellinger 				smp_mb__after_atomic_dec();
1647c66ac9dbSNicholas Bellinger 				core_scsi3_tpg_undepend_item(tmp_tpg);
164803e98c9eSNicholas Bellinger 				cmd->scsi_sense_reason =
164903e98c9eSNicholas Bellinger 					TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
165003e98c9eSNicholas Bellinger 				ret = -EINVAL;
1651c66ac9dbSNicholas Bellinger 				goto out;
1652c66ac9dbSNicholas Bellinger 			}
1653c66ac9dbSNicholas Bellinger 
1654c66ac9dbSNicholas Bellinger 			dest_tpg = tmp_tpg;
16556708bb27SAndy Grover 			pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1656c66ac9dbSNicholas Bellinger 				" %s Port RTPI: %hu\n",
1657e3d6f909SAndy Grover 				dest_tpg->se_tpg_tfo->get_fabric_name(),
1658c66ac9dbSNicholas Bellinger 				dest_node_acl->initiatorname, dest_rtpi);
1659c66ac9dbSNicholas Bellinger 
1660c66ac9dbSNicholas Bellinger 			spin_lock(&dev->se_port_lock);
1661c66ac9dbSNicholas Bellinger 			break;
1662c66ac9dbSNicholas Bellinger 		}
1663c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->se_port_lock);
1664c66ac9dbSNicholas Bellinger 
16656708bb27SAndy Grover 		if (!dest_tpg) {
16666708bb27SAndy Grover 			pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1667c66ac9dbSNicholas Bellinger 					" dest_tpg\n");
166803e98c9eSNicholas Bellinger 			cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
166903e98c9eSNicholas Bellinger 			ret = -EINVAL;
1670c66ac9dbSNicholas Bellinger 			goto out;
1671c66ac9dbSNicholas Bellinger 		}
16728b1e1244SAndy Grover 
16736708bb27SAndy Grover 		pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1674c66ac9dbSNicholas Bellinger 			" tid_len: %d for %s + %s\n",
1675e3d6f909SAndy Grover 			dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
1676c66ac9dbSNicholas Bellinger 			tpdl, tid_len, i_str, iport_ptr);
16778b1e1244SAndy Grover 
1678c66ac9dbSNicholas Bellinger 		if (tid_len > tpdl) {
16796708bb27SAndy Grover 			pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1680c66ac9dbSNicholas Bellinger 				" %u for Transport ID: %s\n", tid_len, ptr);
1681c66ac9dbSNicholas Bellinger 			core_scsi3_nodeacl_undepend_item(dest_node_acl);
1682c66ac9dbSNicholas Bellinger 			core_scsi3_tpg_undepend_item(dest_tpg);
168303e98c9eSNicholas Bellinger 			cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
168403e98c9eSNicholas Bellinger 			ret = -EINVAL;
1685c66ac9dbSNicholas Bellinger 			goto out;
1686c66ac9dbSNicholas Bellinger 		}
1687c66ac9dbSNicholas Bellinger 		/*
1688c66ac9dbSNicholas Bellinger 		 * Locate the desintation struct se_dev_entry pointer for matching
1689c66ac9dbSNicholas Bellinger 		 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1690c66ac9dbSNicholas Bellinger 		 * Target Port.
1691c66ac9dbSNicholas Bellinger 		 */
1692c66ac9dbSNicholas Bellinger 		dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1693c66ac9dbSNicholas Bellinger 					dest_rtpi);
16946708bb27SAndy Grover 		if (!dest_se_deve) {
16956708bb27SAndy Grover 			pr_err("Unable to locate %s dest_se_deve"
1696c66ac9dbSNicholas Bellinger 				" from destination RTPI: %hu\n",
1697e3d6f909SAndy Grover 				dest_tpg->se_tpg_tfo->get_fabric_name(),
1698c66ac9dbSNicholas Bellinger 				dest_rtpi);
1699c66ac9dbSNicholas Bellinger 
1700c66ac9dbSNicholas Bellinger 			core_scsi3_nodeacl_undepend_item(dest_node_acl);
1701c66ac9dbSNicholas Bellinger 			core_scsi3_tpg_undepend_item(dest_tpg);
170203e98c9eSNicholas Bellinger 			cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
170303e98c9eSNicholas Bellinger 			ret = -EINVAL;
1704c66ac9dbSNicholas Bellinger 			goto out;
1705c66ac9dbSNicholas Bellinger 		}
1706c66ac9dbSNicholas Bellinger 
1707c66ac9dbSNicholas Bellinger 		ret = core_scsi3_lunacl_depend_item(dest_se_deve);
1708c66ac9dbSNicholas Bellinger 		if (ret < 0) {
17096708bb27SAndy Grover 			pr_err("core_scsi3_lunacl_depend_item()"
1710c66ac9dbSNicholas Bellinger 					" failed\n");
1711c66ac9dbSNicholas Bellinger 			atomic_dec(&dest_se_deve->pr_ref_count);
1712c66ac9dbSNicholas Bellinger 			smp_mb__after_atomic_dec();
1713c66ac9dbSNicholas Bellinger 			core_scsi3_nodeacl_undepend_item(dest_node_acl);
1714c66ac9dbSNicholas Bellinger 			core_scsi3_tpg_undepend_item(dest_tpg);
171503e98c9eSNicholas Bellinger 			cmd->scsi_sense_reason =
171603e98c9eSNicholas Bellinger 				TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
171703e98c9eSNicholas Bellinger 			ret = -EINVAL;
1718c66ac9dbSNicholas Bellinger 			goto out;
1719c66ac9dbSNicholas Bellinger 		}
17208b1e1244SAndy Grover 
17216708bb27SAndy Grover 		pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1722c66ac9dbSNicholas Bellinger 			" dest_se_deve mapped_lun: %u\n",
1723e3d6f909SAndy Grover 			dest_tpg->se_tpg_tfo->get_fabric_name(),
1724c66ac9dbSNicholas Bellinger 			dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
17258b1e1244SAndy Grover 
1726c66ac9dbSNicholas Bellinger 		/*
1727c66ac9dbSNicholas Bellinger 		 * Skip any TransportIDs that already have a registration for
1728c66ac9dbSNicholas Bellinger 		 * this target port.
1729c66ac9dbSNicholas Bellinger 		 */
1730c66ac9dbSNicholas Bellinger 		pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1731c66ac9dbSNicholas Bellinger 					iport_ptr);
1732c66ac9dbSNicholas Bellinger 		if (pr_reg_e) {
1733c66ac9dbSNicholas Bellinger 			core_scsi3_put_pr_reg(pr_reg_e);
1734c66ac9dbSNicholas Bellinger 			core_scsi3_lunacl_undepend_item(dest_se_deve);
1735c66ac9dbSNicholas Bellinger 			core_scsi3_nodeacl_undepend_item(dest_node_acl);
1736c66ac9dbSNicholas Bellinger 			core_scsi3_tpg_undepend_item(dest_tpg);
1737c66ac9dbSNicholas Bellinger 			ptr += tid_len;
1738c66ac9dbSNicholas Bellinger 			tpdl -= tid_len;
1739c66ac9dbSNicholas Bellinger 			tid_len = 0;
1740c66ac9dbSNicholas Bellinger 			continue;
1741c66ac9dbSNicholas Bellinger 		}
1742c66ac9dbSNicholas Bellinger 		/*
1743c66ac9dbSNicholas Bellinger 		 * Allocate a struct pr_transport_id_holder and setup
1744c66ac9dbSNicholas Bellinger 		 * the dest_node_acl and dest_se_deve pointers for the
1745c66ac9dbSNicholas Bellinger 		 * loop below.
1746c66ac9dbSNicholas Bellinger 		 */
1747c66ac9dbSNicholas Bellinger 		tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1748c66ac9dbSNicholas Bellinger 				GFP_KERNEL);
17496708bb27SAndy Grover 		if (!tidh_new) {
17506708bb27SAndy Grover 			pr_err("Unable to allocate tidh_new\n");
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);
175403e98c9eSNicholas Bellinger 			cmd->scsi_sense_reason =
175503e98c9eSNicholas Bellinger 				TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
175603e98c9eSNicholas Bellinger 			ret = -ENOMEM;
1757c66ac9dbSNicholas Bellinger 			goto out;
1758c66ac9dbSNicholas Bellinger 		}
1759c66ac9dbSNicholas Bellinger 		INIT_LIST_HEAD(&tidh_new->dest_list);
1760c66ac9dbSNicholas Bellinger 		tidh_new->dest_tpg = dest_tpg;
1761c66ac9dbSNicholas Bellinger 		tidh_new->dest_node_acl = dest_node_acl;
1762c66ac9dbSNicholas Bellinger 		tidh_new->dest_se_deve = dest_se_deve;
1763c66ac9dbSNicholas Bellinger 
1764c66ac9dbSNicholas Bellinger 		/*
1765c66ac9dbSNicholas Bellinger 		 * Allocate, but do NOT add the registration for the
1766c66ac9dbSNicholas Bellinger 		 * TransportID referenced SCSI Initiator port.  This
1767c66ac9dbSNicholas Bellinger 		 * done because of the following from spc4r17 in section
1768c66ac9dbSNicholas Bellinger 		 * 6.14.3 wrt SPEC_I_PT:
1769c66ac9dbSNicholas Bellinger 		 *
1770c66ac9dbSNicholas Bellinger 		 * "If a registration fails for any initiator port (e.g., if th
1771c66ac9dbSNicholas Bellinger 		 * logical unit does not have enough resources available to
1772c66ac9dbSNicholas Bellinger 		 * hold the registration information), no registrations shall be
1773c66ac9dbSNicholas Bellinger 		 * made, and the command shall be terminated with
1774c66ac9dbSNicholas Bellinger 		 * CHECK CONDITION status."
1775c66ac9dbSNicholas Bellinger 		 *
1776c66ac9dbSNicholas Bellinger 		 * That means we call __core_scsi3_alloc_registration() here,
1777c66ac9dbSNicholas Bellinger 		 * and then call __core_scsi3_add_registration() in the
1778c66ac9dbSNicholas Bellinger 		 * 2nd loop which will never fail.
1779c66ac9dbSNicholas Bellinger 		 */
17805951146dSAndy Grover 		dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1781c66ac9dbSNicholas Bellinger 				dest_node_acl, dest_se_deve, iport_ptr,
1782c66ac9dbSNicholas Bellinger 				sa_res_key, all_tg_pt, aptpl);
17836708bb27SAndy Grover 		if (!dest_pr_reg) {
1784c66ac9dbSNicholas Bellinger 			core_scsi3_lunacl_undepend_item(dest_se_deve);
1785c66ac9dbSNicholas Bellinger 			core_scsi3_nodeacl_undepend_item(dest_node_acl);
1786c66ac9dbSNicholas Bellinger 			core_scsi3_tpg_undepend_item(dest_tpg);
1787c66ac9dbSNicholas Bellinger 			kfree(tidh_new);
178803e98c9eSNicholas Bellinger 			cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
178903e98c9eSNicholas Bellinger 			ret = -EINVAL;
1790c66ac9dbSNicholas Bellinger 			goto out;
1791c66ac9dbSNicholas Bellinger 		}
1792c66ac9dbSNicholas Bellinger 		tidh_new->dest_pr_reg = dest_pr_reg;
1793c66ac9dbSNicholas Bellinger 		list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1794c66ac9dbSNicholas Bellinger 
1795c66ac9dbSNicholas Bellinger 		ptr += tid_len;
1796c66ac9dbSNicholas Bellinger 		tpdl -= tid_len;
1797c66ac9dbSNicholas Bellinger 		tid_len = 0;
1798c66ac9dbSNicholas Bellinger 
1799c66ac9dbSNicholas Bellinger 	}
180005d1c7c0SAndy Grover 
18014949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
180205d1c7c0SAndy Grover 
1803c66ac9dbSNicholas Bellinger 	/*
1804c66ac9dbSNicholas Bellinger 	 * Go ahead and create a registrations from tid_dest_list for the
1805c66ac9dbSNicholas Bellinger 	 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1806c66ac9dbSNicholas Bellinger 	 * and dest_se_deve.
1807c66ac9dbSNicholas Bellinger 	 *
1808c66ac9dbSNicholas Bellinger 	 * The SA Reservation Key from the PROUT is set for the
1809c66ac9dbSNicholas Bellinger 	 * registration, and ALL_TG_PT is also passed.  ALL_TG_PT=1
1810c66ac9dbSNicholas Bellinger 	 * means that the TransportID Initiator port will be
1811c66ac9dbSNicholas Bellinger 	 * registered on all of the target ports in the SCSI target device
1812c66ac9dbSNicholas Bellinger 	 * ALL_TG_PT=0 means the registration will only be for the
1813c66ac9dbSNicholas Bellinger 	 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1814c66ac9dbSNicholas Bellinger 	 * was received.
1815c66ac9dbSNicholas Bellinger 	 */
1816c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1817c66ac9dbSNicholas Bellinger 		dest_tpg = tidh->dest_tpg;
1818c66ac9dbSNicholas Bellinger 		dest_node_acl = tidh->dest_node_acl;
1819c66ac9dbSNicholas Bellinger 		dest_se_deve = tidh->dest_se_deve;
1820c66ac9dbSNicholas Bellinger 		dest_pr_reg = tidh->dest_pr_reg;
1821c66ac9dbSNicholas Bellinger 		dest_local_nexus = tidh->dest_local_nexus;
1822c66ac9dbSNicholas Bellinger 
1823c66ac9dbSNicholas Bellinger 		list_del(&tidh->dest_list);
1824c66ac9dbSNicholas Bellinger 		kfree(tidh);
1825c66ac9dbSNicholas Bellinger 
1826c66ac9dbSNicholas Bellinger 		memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1827c66ac9dbSNicholas Bellinger 		prf_isid = core_pr_dump_initiator_port(dest_pr_reg, &i_buf[0],
1828c66ac9dbSNicholas Bellinger 						PR_REG_ISID_ID_LEN);
1829c66ac9dbSNicholas Bellinger 
18305951146dSAndy Grover 		__core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
1831c66ac9dbSNicholas Bellinger 					dest_pr_reg, 0, 0);
1832c66ac9dbSNicholas Bellinger 
18336708bb27SAndy Grover 		pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1834c66ac9dbSNicholas Bellinger 			" registered Transport ID for Node: %s%s Mapped LUN:"
1835e3d6f909SAndy Grover 			" %u\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
1836c66ac9dbSNicholas Bellinger 			dest_node_acl->initiatorname, (prf_isid) ?
1837c66ac9dbSNicholas Bellinger 			&i_buf[0] : "", dest_se_deve->mapped_lun);
1838c66ac9dbSNicholas Bellinger 
1839c66ac9dbSNicholas Bellinger 		if (dest_local_nexus)
1840c66ac9dbSNicholas Bellinger 			continue;
1841c66ac9dbSNicholas Bellinger 
1842c66ac9dbSNicholas Bellinger 		core_scsi3_lunacl_undepend_item(dest_se_deve);
1843c66ac9dbSNicholas Bellinger 		core_scsi3_nodeacl_undepend_item(dest_node_acl);
1844c66ac9dbSNicholas Bellinger 		core_scsi3_tpg_undepend_item(dest_tpg);
1845c66ac9dbSNicholas Bellinger 	}
1846c66ac9dbSNicholas Bellinger 
1847c66ac9dbSNicholas Bellinger 	return 0;
1848c66ac9dbSNicholas Bellinger out:
18494949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
1850c66ac9dbSNicholas Bellinger 	/*
1851c66ac9dbSNicholas Bellinger 	 * For the failure case, release everything from tid_dest_list
1852c66ac9dbSNicholas Bellinger 	 * including *dest_pr_reg and the configfs dependances..
1853c66ac9dbSNicholas Bellinger 	 */
1854c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1855c66ac9dbSNicholas Bellinger 		dest_tpg = tidh->dest_tpg;
1856c66ac9dbSNicholas Bellinger 		dest_node_acl = tidh->dest_node_acl;
1857c66ac9dbSNicholas Bellinger 		dest_se_deve = tidh->dest_se_deve;
1858c66ac9dbSNicholas Bellinger 		dest_pr_reg = tidh->dest_pr_reg;
1859c66ac9dbSNicholas Bellinger 		dest_local_nexus = tidh->dest_local_nexus;
1860c66ac9dbSNicholas Bellinger 
1861c66ac9dbSNicholas Bellinger 		list_del(&tidh->dest_list);
1862c66ac9dbSNicholas Bellinger 		kfree(tidh);
1863c66ac9dbSNicholas Bellinger 		/*
1864c66ac9dbSNicholas Bellinger 		 * Release any extra ALL_TG_PT=1 registrations for
1865c66ac9dbSNicholas Bellinger 		 * the SPEC_I_PT=1 case.
1866c66ac9dbSNicholas Bellinger 		 */
1867c66ac9dbSNicholas Bellinger 		list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1868c66ac9dbSNicholas Bellinger 				&dest_pr_reg->pr_reg_atp_list,
1869c66ac9dbSNicholas Bellinger 				pr_reg_atp_mem_list) {
1870c66ac9dbSNicholas Bellinger 			list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1871c66ac9dbSNicholas Bellinger 			core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1872c66ac9dbSNicholas Bellinger 			kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1873c66ac9dbSNicholas Bellinger 		}
1874c66ac9dbSNicholas Bellinger 
1875c66ac9dbSNicholas Bellinger 		kfree(dest_pr_reg->pr_aptpl_buf);
1876c66ac9dbSNicholas Bellinger 		kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1877c66ac9dbSNicholas Bellinger 
1878c66ac9dbSNicholas Bellinger 		if (dest_local_nexus)
1879c66ac9dbSNicholas Bellinger 			continue;
1880c66ac9dbSNicholas Bellinger 
1881c66ac9dbSNicholas Bellinger 		core_scsi3_lunacl_undepend_item(dest_se_deve);
1882c66ac9dbSNicholas Bellinger 		core_scsi3_nodeacl_undepend_item(dest_node_acl);
1883c66ac9dbSNicholas Bellinger 		core_scsi3_tpg_undepend_item(dest_tpg);
1884c66ac9dbSNicholas Bellinger 	}
1885c66ac9dbSNicholas Bellinger 	return ret;
1886c66ac9dbSNicholas Bellinger }
1887c66ac9dbSNicholas Bellinger 
1888c66ac9dbSNicholas Bellinger /*
1889c66ac9dbSNicholas Bellinger  * Called with struct se_device->dev_reservation_lock held
1890c66ac9dbSNicholas Bellinger  */
1891c66ac9dbSNicholas Bellinger static int __core_scsi3_update_aptpl_buf(
1892c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1893c66ac9dbSNicholas Bellinger 	unsigned char *buf,
1894c66ac9dbSNicholas Bellinger 	u32 pr_aptpl_buf_len,
1895c66ac9dbSNicholas Bellinger 	int clear_aptpl_metadata)
1896c66ac9dbSNicholas Bellinger {
1897c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
1898c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg;
1899c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1900c66ac9dbSNicholas Bellinger 	unsigned char tmp[512], isid_buf[32];
1901c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1902c66ac9dbSNicholas Bellinger 	int reg_count = 0;
1903c66ac9dbSNicholas Bellinger 
1904c66ac9dbSNicholas Bellinger 	memset(buf, 0, pr_aptpl_buf_len);
1905c66ac9dbSNicholas Bellinger 	/*
1906c66ac9dbSNicholas Bellinger 	 * Called to clear metadata once APTPL has been deactivated.
1907c66ac9dbSNicholas Bellinger 	 */
1908c66ac9dbSNicholas Bellinger 	if (clear_aptpl_metadata) {
1909c66ac9dbSNicholas Bellinger 		snprintf(buf, pr_aptpl_buf_len,
1910c66ac9dbSNicholas Bellinger 				"No Registrations or Reservations\n");
1911c66ac9dbSNicholas Bellinger 		return 0;
1912c66ac9dbSNicholas Bellinger 	}
1913c66ac9dbSNicholas Bellinger 	/*
1914c66ac9dbSNicholas Bellinger 	 * Walk the registration list..
1915c66ac9dbSNicholas Bellinger 	 */
19160fd97ccfSChristoph Hellwig 	spin_lock(&dev->t10_pr.registration_lock);
19170fd97ccfSChristoph Hellwig 	list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1918c66ac9dbSNicholas Bellinger 			pr_reg_list) {
1919c66ac9dbSNicholas Bellinger 
1920c66ac9dbSNicholas Bellinger 		tmp[0] = '\0';
1921c66ac9dbSNicholas Bellinger 		isid_buf[0] = '\0';
1922c66ac9dbSNicholas Bellinger 		tpg = pr_reg->pr_reg_nacl->se_tpg;
1923c66ac9dbSNicholas Bellinger 		lun = pr_reg->pr_reg_tg_pt_lun;
1924c66ac9dbSNicholas Bellinger 		/*
1925c66ac9dbSNicholas Bellinger 		 * Write out any ISID value to APTPL metadata that was included
1926c66ac9dbSNicholas Bellinger 		 * in the original registration.
1927c66ac9dbSNicholas Bellinger 		 */
1928c66ac9dbSNicholas Bellinger 		if (pr_reg->isid_present_at_reg)
1929c66ac9dbSNicholas Bellinger 			snprintf(isid_buf, 32, "initiator_sid=%s\n",
1930c66ac9dbSNicholas Bellinger 					pr_reg->pr_reg_isid);
1931c66ac9dbSNicholas Bellinger 		/*
1932c66ac9dbSNicholas Bellinger 		 * Include special metadata if the pr_reg matches the
1933c66ac9dbSNicholas Bellinger 		 * reservation holder.
1934c66ac9dbSNicholas Bellinger 		 */
1935c66ac9dbSNicholas Bellinger 		if (dev->dev_pr_res_holder == pr_reg) {
1936c66ac9dbSNicholas Bellinger 			snprintf(tmp, 512, "PR_REG_START: %d"
1937c66ac9dbSNicholas Bellinger 				"\ninitiator_fabric=%s\n"
1938c66ac9dbSNicholas Bellinger 				"initiator_node=%s\n%s"
1939c66ac9dbSNicholas Bellinger 				"sa_res_key=%llu\n"
1940c66ac9dbSNicholas Bellinger 				"res_holder=1\nres_type=%02x\n"
1941c66ac9dbSNicholas Bellinger 				"res_scope=%02x\nres_all_tg_pt=%d\n"
1942c66ac9dbSNicholas Bellinger 				"mapped_lun=%u\n", reg_count,
1943e3d6f909SAndy Grover 				tpg->se_tpg_tfo->get_fabric_name(),
1944c66ac9dbSNicholas Bellinger 				pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1945c66ac9dbSNicholas Bellinger 				pr_reg->pr_res_key, pr_reg->pr_res_type,
1946c66ac9dbSNicholas Bellinger 				pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1947c66ac9dbSNicholas Bellinger 				pr_reg->pr_res_mapped_lun);
1948c66ac9dbSNicholas Bellinger 		} else {
1949c66ac9dbSNicholas Bellinger 			snprintf(tmp, 512, "PR_REG_START: %d\n"
1950c66ac9dbSNicholas Bellinger 				"initiator_fabric=%s\ninitiator_node=%s\n%s"
1951c66ac9dbSNicholas Bellinger 				"sa_res_key=%llu\nres_holder=0\n"
1952c66ac9dbSNicholas Bellinger 				"res_all_tg_pt=%d\nmapped_lun=%u\n",
1953e3d6f909SAndy Grover 				reg_count, tpg->se_tpg_tfo->get_fabric_name(),
1954c66ac9dbSNicholas Bellinger 				pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1955c66ac9dbSNicholas Bellinger 				pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1956c66ac9dbSNicholas Bellinger 				pr_reg->pr_res_mapped_lun);
1957c66ac9dbSNicholas Bellinger 		}
1958c66ac9dbSNicholas Bellinger 
195960d645a4SDan Carpenter 		if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
19606708bb27SAndy Grover 			pr_err("Unable to update renaming"
1961c66ac9dbSNicholas Bellinger 				" APTPL metadata\n");
19620fd97ccfSChristoph Hellwig 			spin_unlock(&dev->t10_pr.registration_lock);
1963e3d6f909SAndy Grover 			return -EMSGSIZE;
1964c66ac9dbSNicholas Bellinger 		}
1965c66ac9dbSNicholas Bellinger 		len += sprintf(buf+len, "%s", tmp);
1966c66ac9dbSNicholas Bellinger 
1967c66ac9dbSNicholas Bellinger 		/*
1968c66ac9dbSNicholas Bellinger 		 * Include information about the associated SCSI target port.
1969c66ac9dbSNicholas Bellinger 		 */
1970c66ac9dbSNicholas Bellinger 		snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1971c66ac9dbSNicholas Bellinger 			"tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
1972e3d6f909SAndy Grover 			" %d\n", tpg->se_tpg_tfo->get_fabric_name(),
1973e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1974e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_tag(tpg),
1975c66ac9dbSNicholas Bellinger 			lun->lun_sep->sep_rtpi, lun->unpacked_lun, reg_count);
1976c66ac9dbSNicholas Bellinger 
197760d645a4SDan Carpenter 		if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
19786708bb27SAndy Grover 			pr_err("Unable to update renaming"
1979c66ac9dbSNicholas Bellinger 				" APTPL metadata\n");
19800fd97ccfSChristoph Hellwig 			spin_unlock(&dev->t10_pr.registration_lock);
1981e3d6f909SAndy Grover 			return -EMSGSIZE;
1982c66ac9dbSNicholas Bellinger 		}
1983c66ac9dbSNicholas Bellinger 		len += sprintf(buf+len, "%s", tmp);
1984c66ac9dbSNicholas Bellinger 		reg_count++;
1985c66ac9dbSNicholas Bellinger 	}
19860fd97ccfSChristoph Hellwig 	spin_unlock(&dev->t10_pr.registration_lock);
1987c66ac9dbSNicholas Bellinger 
19886708bb27SAndy Grover 	if (!reg_count)
1989c66ac9dbSNicholas Bellinger 		len += sprintf(buf+len, "No Registrations or Reservations");
1990c66ac9dbSNicholas Bellinger 
1991c66ac9dbSNicholas Bellinger 	return 0;
1992c66ac9dbSNicholas Bellinger }
1993c66ac9dbSNicholas Bellinger 
1994c66ac9dbSNicholas Bellinger static int core_scsi3_update_aptpl_buf(
1995c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1996c66ac9dbSNicholas Bellinger 	unsigned char *buf,
1997c66ac9dbSNicholas Bellinger 	u32 pr_aptpl_buf_len,
1998c66ac9dbSNicholas Bellinger 	int clear_aptpl_metadata)
1999c66ac9dbSNicholas Bellinger {
2000c66ac9dbSNicholas Bellinger 	int ret;
2001c66ac9dbSNicholas Bellinger 
2002c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
2003c66ac9dbSNicholas Bellinger 	ret = __core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
2004c66ac9dbSNicholas Bellinger 				clear_aptpl_metadata);
2005c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
2006c66ac9dbSNicholas Bellinger 
2007c66ac9dbSNicholas Bellinger 	return ret;
2008c66ac9dbSNicholas Bellinger }
2009c66ac9dbSNicholas Bellinger 
2010c66ac9dbSNicholas Bellinger /*
2011c66ac9dbSNicholas Bellinger  * Called with struct se_device->aptpl_file_mutex held
2012c66ac9dbSNicholas Bellinger  */
2013c66ac9dbSNicholas Bellinger static int __core_scsi3_write_aptpl_to_file(
2014c66ac9dbSNicholas Bellinger 	struct se_device *dev,
2015c66ac9dbSNicholas Bellinger 	unsigned char *buf,
2016c66ac9dbSNicholas Bellinger 	u32 pr_aptpl_buf_len)
2017c66ac9dbSNicholas Bellinger {
20180fd97ccfSChristoph Hellwig 	struct t10_wwn *wwn = &dev->t10_wwn;
2019c66ac9dbSNicholas Bellinger 	struct file *file;
2020c66ac9dbSNicholas Bellinger 	struct iovec iov[1];
2021c66ac9dbSNicholas Bellinger 	mm_segment_t old_fs;
2022c66ac9dbSNicholas Bellinger 	int flags = O_RDWR | O_CREAT | O_TRUNC;
2023c66ac9dbSNicholas Bellinger 	char path[512];
2024c66ac9dbSNicholas Bellinger 	int ret;
2025c66ac9dbSNicholas Bellinger 
2026c66ac9dbSNicholas Bellinger 	memset(iov, 0, sizeof(struct iovec));
2027c66ac9dbSNicholas Bellinger 	memset(path, 0, 512);
2028c66ac9dbSNicholas Bellinger 
202960d645a4SDan Carpenter 	if (strlen(&wwn->unit_serial[0]) >= 512) {
20306708bb27SAndy Grover 		pr_err("WWN value for struct se_device does not fit"
2031c66ac9dbSNicholas Bellinger 			" into path buffer\n");
2032e3d6f909SAndy Grover 		return -EMSGSIZE;
2033c66ac9dbSNicholas Bellinger 	}
2034c66ac9dbSNicholas Bellinger 
2035c66ac9dbSNicholas Bellinger 	snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]);
2036c66ac9dbSNicholas Bellinger 	file = filp_open(path, flags, 0600);
2037c66ac9dbSNicholas Bellinger 	if (IS_ERR(file) || !file || !file->f_dentry) {
20386708bb27SAndy Grover 		pr_err("filp_open(%s) for APTPL metadata"
2039c66ac9dbSNicholas Bellinger 			" failed\n", path);
2040d35212f3SRoland Dreier 		return IS_ERR(file) ? PTR_ERR(file) : -ENOENT;
2041c66ac9dbSNicholas Bellinger 	}
2042c66ac9dbSNicholas Bellinger 
2043c66ac9dbSNicholas Bellinger 	iov[0].iov_base = &buf[0];
20446708bb27SAndy Grover 	if (!pr_aptpl_buf_len)
2045c66ac9dbSNicholas Bellinger 		iov[0].iov_len = (strlen(&buf[0]) + 1); /* Add extra for NULL */
2046c66ac9dbSNicholas Bellinger 	else
2047c66ac9dbSNicholas Bellinger 		iov[0].iov_len = pr_aptpl_buf_len;
2048c66ac9dbSNicholas Bellinger 
2049c66ac9dbSNicholas Bellinger 	old_fs = get_fs();
2050c66ac9dbSNicholas Bellinger 	set_fs(get_ds());
2051c66ac9dbSNicholas Bellinger 	ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
2052c66ac9dbSNicholas Bellinger 	set_fs(old_fs);
2053c66ac9dbSNicholas Bellinger 
2054c66ac9dbSNicholas Bellinger 	if (ret < 0) {
20556708bb27SAndy Grover 		pr_debug("Error writing APTPL metadata file: %s\n", path);
2056c66ac9dbSNicholas Bellinger 		filp_close(file, NULL);
2057e3d6f909SAndy Grover 		return -EIO;
2058c66ac9dbSNicholas Bellinger 	}
2059c66ac9dbSNicholas Bellinger 	filp_close(file, NULL);
2060c66ac9dbSNicholas Bellinger 
2061c66ac9dbSNicholas Bellinger 	return 0;
2062c66ac9dbSNicholas Bellinger }
2063c66ac9dbSNicholas Bellinger 
2064c66ac9dbSNicholas Bellinger static int core_scsi3_update_and_write_aptpl(
2065c66ac9dbSNicholas Bellinger 	struct se_device *dev,
2066c66ac9dbSNicholas Bellinger 	unsigned char *in_buf,
2067c66ac9dbSNicholas Bellinger 	u32 in_pr_aptpl_buf_len)
2068c66ac9dbSNicholas Bellinger {
2069c66ac9dbSNicholas Bellinger 	unsigned char null_buf[64], *buf;
2070c66ac9dbSNicholas Bellinger 	u32 pr_aptpl_buf_len;
2071c66ac9dbSNicholas Bellinger 	int ret, clear_aptpl_metadata = 0;
2072c66ac9dbSNicholas Bellinger 	/*
2073c66ac9dbSNicholas Bellinger 	 * Can be called with a NULL pointer from PROUT service action CLEAR
2074c66ac9dbSNicholas Bellinger 	 */
20756708bb27SAndy Grover 	if (!in_buf) {
2076c66ac9dbSNicholas Bellinger 		memset(null_buf, 0, 64);
2077c66ac9dbSNicholas Bellinger 		buf = &null_buf[0];
2078c66ac9dbSNicholas Bellinger 		/*
2079c66ac9dbSNicholas Bellinger 		 * This will clear the APTPL metadata to:
2080c66ac9dbSNicholas Bellinger 		 * "No Registrations or Reservations" status
2081c66ac9dbSNicholas Bellinger 		 */
2082c66ac9dbSNicholas Bellinger 		pr_aptpl_buf_len = 64;
2083c66ac9dbSNicholas Bellinger 		clear_aptpl_metadata = 1;
2084c66ac9dbSNicholas Bellinger 	} else {
2085c66ac9dbSNicholas Bellinger 		buf = in_buf;
2086c66ac9dbSNicholas Bellinger 		pr_aptpl_buf_len = in_pr_aptpl_buf_len;
2087c66ac9dbSNicholas Bellinger 	}
2088c66ac9dbSNicholas Bellinger 
2089c66ac9dbSNicholas Bellinger 	ret = core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
2090c66ac9dbSNicholas Bellinger 				clear_aptpl_metadata);
2091c66ac9dbSNicholas Bellinger 	if (ret != 0)
2092e3d6f909SAndy Grover 		return ret;
2093c66ac9dbSNicholas Bellinger 	/*
2094c66ac9dbSNicholas Bellinger 	 * __core_scsi3_write_aptpl_to_file() will call strlen()
2095c66ac9dbSNicholas Bellinger 	 * on the passed buf to determine pr_aptpl_buf_len.
2096c66ac9dbSNicholas Bellinger 	 */
2097c66ac9dbSNicholas Bellinger 	ret = __core_scsi3_write_aptpl_to_file(dev, buf, 0);
2098c66ac9dbSNicholas Bellinger 	if (ret != 0)
2099e3d6f909SAndy Grover 		return ret;
2100c66ac9dbSNicholas Bellinger 
2101c66ac9dbSNicholas Bellinger 	return ret;
2102c66ac9dbSNicholas Bellinger }
2103c66ac9dbSNicholas Bellinger 
2104c66ac9dbSNicholas Bellinger static int core_scsi3_emulate_pro_register(
2105c66ac9dbSNicholas Bellinger 	struct se_cmd *cmd,
2106c66ac9dbSNicholas Bellinger 	u64 res_key,
2107c66ac9dbSNicholas Bellinger 	u64 sa_res_key,
2108c66ac9dbSNicholas Bellinger 	int aptpl,
2109c66ac9dbSNicholas Bellinger 	int all_tg_pt,
2110c66ac9dbSNicholas Bellinger 	int spec_i_pt,
2111c66ac9dbSNicholas Bellinger 	int ignore_key)
2112c66ac9dbSNicholas Bellinger {
2113e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
21145951146dSAndy Grover 	struct se_device *dev = cmd->se_dev;
2115c66ac9dbSNicholas Bellinger 	struct se_dev_entry *se_deve;
2116e3d6f909SAndy Grover 	struct se_lun *se_lun = cmd->se_lun;
2117c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg;
2118c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp, *pr_reg_e;
21190fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
2120c66ac9dbSNicholas Bellinger 	/* Used for APTPL metadata w/ UNREGISTER */
2121c66ac9dbSNicholas Bellinger 	unsigned char *pr_aptpl_buf = NULL;
2122c66ac9dbSNicholas Bellinger 	unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
2123c66ac9dbSNicholas Bellinger 	int pr_holder = 0, ret = 0, type;
2124c66ac9dbSNicholas Bellinger 
21256708bb27SAndy Grover 	if (!se_sess || !se_lun) {
21266708bb27SAndy Grover 		pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
212703e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
212803e98c9eSNicholas Bellinger 		return -EINVAL;
2129c66ac9dbSNicholas Bellinger 	}
2130c66ac9dbSNicholas Bellinger 	se_tpg = se_sess->se_tpg;
2131f2083241SJörn Engel 	se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
2132c66ac9dbSNicholas Bellinger 
2133e3d6f909SAndy Grover 	if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
2134c66ac9dbSNicholas Bellinger 		memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
2135e3d6f909SAndy Grover 		se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
2136c66ac9dbSNicholas Bellinger 				PR_REG_ISID_LEN);
2137c66ac9dbSNicholas Bellinger 		isid_ptr = &isid_buf[0];
2138c66ac9dbSNicholas Bellinger 	}
2139c66ac9dbSNicholas Bellinger 	/*
2140c66ac9dbSNicholas Bellinger 	 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2141c66ac9dbSNicholas Bellinger 	 */
2142c66ac9dbSNicholas Bellinger 	pr_reg_e = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
21436708bb27SAndy Grover 	if (!pr_reg_e) {
2144c66ac9dbSNicholas Bellinger 		if (res_key) {
21456708bb27SAndy Grover 			pr_warn("SPC-3 PR: Reservation Key non-zero"
2146c66ac9dbSNicholas Bellinger 				" for SA REGISTER, returning CONFLICT\n");
214703e98c9eSNicholas Bellinger 			cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
214803e98c9eSNicholas Bellinger 			return -EINVAL;
2149c66ac9dbSNicholas Bellinger 		}
2150c66ac9dbSNicholas Bellinger 		/*
2151c66ac9dbSNicholas Bellinger 		 * Do nothing but return GOOD status.
2152c66ac9dbSNicholas Bellinger 		 */
21536708bb27SAndy Grover 		if (!sa_res_key)
215403e98c9eSNicholas Bellinger 			return 0;
2155c66ac9dbSNicholas Bellinger 
21566708bb27SAndy Grover 		if (!spec_i_pt) {
2157c66ac9dbSNicholas Bellinger 			/*
2158c66ac9dbSNicholas Bellinger 			 * Perform the Service Action REGISTER on the Initiator
2159c66ac9dbSNicholas Bellinger 			 * Port Endpoint that the PRO was received from on the
2160c66ac9dbSNicholas Bellinger 			 * Logical Unit of the SCSI device server.
2161c66ac9dbSNicholas Bellinger 			 */
21625951146dSAndy Grover 			ret = core_scsi3_alloc_registration(cmd->se_dev,
2163c66ac9dbSNicholas Bellinger 					se_sess->se_node_acl, se_deve, isid_ptr,
2164c66ac9dbSNicholas Bellinger 					sa_res_key, all_tg_pt, aptpl,
2165c66ac9dbSNicholas Bellinger 					ignore_key, 0);
2166c66ac9dbSNicholas Bellinger 			if (ret != 0) {
21676708bb27SAndy Grover 				pr_err("Unable to allocate"
2168c66ac9dbSNicholas Bellinger 					" struct t10_pr_registration\n");
216903e98c9eSNicholas Bellinger 				cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
217003e98c9eSNicholas Bellinger 				return -EINVAL;
2171c66ac9dbSNicholas Bellinger 			}
2172c66ac9dbSNicholas Bellinger 		} else {
2173c66ac9dbSNicholas Bellinger 			/*
2174c66ac9dbSNicholas Bellinger 			 * Register both the Initiator port that received
2175c66ac9dbSNicholas Bellinger 			 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2176c66ac9dbSNicholas Bellinger 			 * TransportID from Parameter list and loop through
2177c66ac9dbSNicholas Bellinger 			 * fabric dependent parameter list while calling
2178c66ac9dbSNicholas Bellinger 			 * logic from of core_scsi3_alloc_registration() for
2179c66ac9dbSNicholas Bellinger 			 * each TransportID provided SCSI Initiator Port/Device
2180c66ac9dbSNicholas Bellinger 			 */
2181c66ac9dbSNicholas Bellinger 			ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2182c66ac9dbSNicholas Bellinger 					isid_ptr, sa_res_key, all_tg_pt, aptpl);
2183c66ac9dbSNicholas Bellinger 			if (ret != 0)
2184c66ac9dbSNicholas Bellinger 				return ret;
2185c66ac9dbSNicholas Bellinger 		}
2186c66ac9dbSNicholas Bellinger 		/*
2187c66ac9dbSNicholas Bellinger 		 * Nothing left to do for the APTPL=0 case.
2188c66ac9dbSNicholas Bellinger 		 */
21896708bb27SAndy Grover 		if (!aptpl) {
2190c66ac9dbSNicholas Bellinger 			pr_tmpl->pr_aptpl_active = 0;
21915951146dSAndy Grover 			core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
21926708bb27SAndy Grover 			pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
2193c66ac9dbSNicholas Bellinger 					" REGISTER\n");
2194c66ac9dbSNicholas Bellinger 			return 0;
2195c66ac9dbSNicholas Bellinger 		}
2196c66ac9dbSNicholas Bellinger 		/*
2197c66ac9dbSNicholas Bellinger 		 * Locate the newly allocated local I_T Nexus *pr_reg, and
2198c66ac9dbSNicholas Bellinger 		 * update the APTPL metadata information using its
2199c66ac9dbSNicholas Bellinger 		 * preallocated *pr_reg->pr_aptpl_buf.
2200c66ac9dbSNicholas Bellinger 		 */
22015951146dSAndy Grover 		pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev,
2202c66ac9dbSNicholas Bellinger 				se_sess->se_node_acl, se_sess);
2203c66ac9dbSNicholas Bellinger 
22045951146dSAndy Grover 		ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2205c66ac9dbSNicholas Bellinger 				&pr_reg->pr_aptpl_buf[0],
2206c66ac9dbSNicholas Bellinger 				pr_tmpl->pr_aptpl_buf_len);
22076708bb27SAndy Grover 		if (!ret) {
2208c66ac9dbSNicholas Bellinger 			pr_tmpl->pr_aptpl_active = 1;
22096708bb27SAndy Grover 			pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
2210c66ac9dbSNicholas Bellinger 		}
2211c66ac9dbSNicholas Bellinger 
2212c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg);
2213c66ac9dbSNicholas Bellinger 		return ret;
2214c66ac9dbSNicholas Bellinger 	} else {
2215c66ac9dbSNicholas Bellinger 		/*
2216c66ac9dbSNicholas Bellinger 		 * Locate the existing *pr_reg via struct se_node_acl pointers
2217c66ac9dbSNicholas Bellinger 		 */
2218c66ac9dbSNicholas Bellinger 		pr_reg = pr_reg_e;
2219c66ac9dbSNicholas Bellinger 		type = pr_reg->pr_res_type;
2220c66ac9dbSNicholas Bellinger 
22216708bb27SAndy Grover 		if (!ignore_key) {
2222c66ac9dbSNicholas Bellinger 			if (res_key != pr_reg->pr_res_key) {
22236708bb27SAndy Grover 				pr_err("SPC-3 PR REGISTER: Received"
2224c66ac9dbSNicholas Bellinger 					" res_key: 0x%016Lx does not match"
2225c66ac9dbSNicholas Bellinger 					" existing SA REGISTER res_key:"
2226c66ac9dbSNicholas Bellinger 					" 0x%016Lx\n", res_key,
2227c66ac9dbSNicholas Bellinger 					pr_reg->pr_res_key);
2228c66ac9dbSNicholas Bellinger 				core_scsi3_put_pr_reg(pr_reg);
222903e98c9eSNicholas Bellinger 				cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
223003e98c9eSNicholas Bellinger 				return -EINVAL;
2231c66ac9dbSNicholas Bellinger 			}
2232c66ac9dbSNicholas Bellinger 		}
2233c66ac9dbSNicholas Bellinger 		if (spec_i_pt) {
22346708bb27SAndy Grover 			pr_err("SPC-3 PR UNREGISTER: SPEC_I_PT"
2235c66ac9dbSNicholas Bellinger 				" set while sa_res_key=0\n");
2236c66ac9dbSNicholas Bellinger 			core_scsi3_put_pr_reg(pr_reg);
223703e98c9eSNicholas Bellinger 			cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
223803e98c9eSNicholas Bellinger 			return -EINVAL;
2239c66ac9dbSNicholas Bellinger 		}
2240c66ac9dbSNicholas Bellinger 		/*
2241c66ac9dbSNicholas Bellinger 		 * An existing ALL_TG_PT=1 registration being released
2242c66ac9dbSNicholas Bellinger 		 * must also set ALL_TG_PT=1 in the incoming PROUT.
2243c66ac9dbSNicholas Bellinger 		 */
2244c66ac9dbSNicholas Bellinger 		if (pr_reg->pr_reg_all_tg_pt && !(all_tg_pt)) {
22456708bb27SAndy Grover 			pr_err("SPC-3 PR UNREGISTER: ALL_TG_PT=1"
2246c66ac9dbSNicholas Bellinger 				" registration exists, but ALL_TG_PT=1 bit not"
2247c66ac9dbSNicholas Bellinger 				" present in received PROUT\n");
2248c66ac9dbSNicholas Bellinger 			core_scsi3_put_pr_reg(pr_reg);
224903e98c9eSNicholas Bellinger 			cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
225003e98c9eSNicholas Bellinger 			return -EINVAL;
2251c66ac9dbSNicholas Bellinger 		}
2252c66ac9dbSNicholas Bellinger 		/*
2253c66ac9dbSNicholas Bellinger 		 * Allocate APTPL metadata buffer used for UNREGISTER ops
2254c66ac9dbSNicholas Bellinger 		 */
2255c66ac9dbSNicholas Bellinger 		if (aptpl) {
2256c66ac9dbSNicholas Bellinger 			pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len,
2257c66ac9dbSNicholas Bellinger 						GFP_KERNEL);
22586708bb27SAndy Grover 			if (!pr_aptpl_buf) {
22596708bb27SAndy Grover 				pr_err("Unable to allocate"
2260c66ac9dbSNicholas Bellinger 					" pr_aptpl_buf\n");
2261c66ac9dbSNicholas Bellinger 				core_scsi3_put_pr_reg(pr_reg);
226203e98c9eSNicholas Bellinger 				cmd->scsi_sense_reason =
226303e98c9eSNicholas Bellinger 					TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
226403e98c9eSNicholas Bellinger 				return -EINVAL;
2265c66ac9dbSNicholas Bellinger 			}
2266c66ac9dbSNicholas Bellinger 		}
2267c66ac9dbSNicholas Bellinger 		/*
2268c66ac9dbSNicholas Bellinger 		 * sa_res_key=0 Unregister Reservation Key for registered I_T
2269c66ac9dbSNicholas Bellinger 		 * Nexus sa_res_key=1 Change Reservation Key for registered I_T
2270c66ac9dbSNicholas Bellinger 		 * Nexus.
2271c66ac9dbSNicholas Bellinger 		 */
22726708bb27SAndy Grover 		if (!sa_res_key) {
2273c66ac9dbSNicholas Bellinger 			pr_holder = core_scsi3_check_implict_release(
22745951146dSAndy Grover 					cmd->se_dev, pr_reg);
2275c66ac9dbSNicholas Bellinger 			if (pr_holder < 0) {
2276c66ac9dbSNicholas Bellinger 				kfree(pr_aptpl_buf);
2277c66ac9dbSNicholas Bellinger 				core_scsi3_put_pr_reg(pr_reg);
227803e98c9eSNicholas Bellinger 				cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
227903e98c9eSNicholas Bellinger 				return -EINVAL;
2280c66ac9dbSNicholas Bellinger 			}
2281c66ac9dbSNicholas Bellinger 
2282c66ac9dbSNicholas Bellinger 			spin_lock(&pr_tmpl->registration_lock);
2283c66ac9dbSNicholas Bellinger 			/*
2284c66ac9dbSNicholas Bellinger 			 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2285c66ac9dbSNicholas Bellinger 			 * and matching pr_res_key.
2286c66ac9dbSNicholas Bellinger 			 */
2287c66ac9dbSNicholas Bellinger 			if (pr_reg->pr_reg_all_tg_pt) {
2288c66ac9dbSNicholas Bellinger 				list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2289c66ac9dbSNicholas Bellinger 						&pr_tmpl->registration_list,
2290c66ac9dbSNicholas Bellinger 						pr_reg_list) {
2291c66ac9dbSNicholas Bellinger 
22926708bb27SAndy Grover 					if (!pr_reg_p->pr_reg_all_tg_pt)
2293c66ac9dbSNicholas Bellinger 						continue;
2294c66ac9dbSNicholas Bellinger 
2295c66ac9dbSNicholas Bellinger 					if (pr_reg_p->pr_res_key != res_key)
2296c66ac9dbSNicholas Bellinger 						continue;
2297c66ac9dbSNicholas Bellinger 
2298c66ac9dbSNicholas Bellinger 					if (pr_reg == pr_reg_p)
2299c66ac9dbSNicholas Bellinger 						continue;
2300c66ac9dbSNicholas Bellinger 
2301c66ac9dbSNicholas Bellinger 					if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2302c66ac9dbSNicholas Bellinger 						   pr_reg_p->pr_reg_nacl->initiatorname))
2303c66ac9dbSNicholas Bellinger 						continue;
2304c66ac9dbSNicholas Bellinger 
2305c66ac9dbSNicholas Bellinger 					__core_scsi3_free_registration(dev,
2306c66ac9dbSNicholas Bellinger 							pr_reg_p, NULL, 0);
2307c66ac9dbSNicholas Bellinger 				}
2308c66ac9dbSNicholas Bellinger 			}
2309c66ac9dbSNicholas Bellinger 			/*
2310c66ac9dbSNicholas Bellinger 			 * Release the calling I_T Nexus registration now..
2311c66ac9dbSNicholas Bellinger 			 */
23125951146dSAndy Grover 			__core_scsi3_free_registration(cmd->se_dev, pr_reg,
2313c66ac9dbSNicholas Bellinger 							NULL, 1);
2314c66ac9dbSNicholas Bellinger 			/*
2315c66ac9dbSNicholas Bellinger 			 * From spc4r17, section 5.7.11.3 Unregistering
2316c66ac9dbSNicholas Bellinger 			 *
2317c66ac9dbSNicholas Bellinger 			 * If the persistent reservation is a registrants only
2318c66ac9dbSNicholas Bellinger 			 * type, the device server shall establish a unit
2319c66ac9dbSNicholas Bellinger 			 * attention condition for the initiator port associated
2320c66ac9dbSNicholas Bellinger 			 * with every registered I_T nexus except for the I_T
2321c66ac9dbSNicholas Bellinger 			 * nexus on which the PERSISTENT RESERVE OUT command was
2322c66ac9dbSNicholas Bellinger 			 * received, with the additional sense code set to
2323c66ac9dbSNicholas Bellinger 			 * RESERVATIONS RELEASED.
2324c66ac9dbSNicholas Bellinger 			 */
2325c66ac9dbSNicholas Bellinger 			if (pr_holder &&
2326c66ac9dbSNicholas Bellinger 			   ((type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
2327c66ac9dbSNicholas Bellinger 			    (type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY))) {
2328c66ac9dbSNicholas Bellinger 				list_for_each_entry(pr_reg_p,
2329c66ac9dbSNicholas Bellinger 						&pr_tmpl->registration_list,
2330c66ac9dbSNicholas Bellinger 						pr_reg_list) {
2331c66ac9dbSNicholas Bellinger 
2332c66ac9dbSNicholas Bellinger 					core_scsi3_ua_allocate(
2333c66ac9dbSNicholas Bellinger 						pr_reg_p->pr_reg_nacl,
2334c66ac9dbSNicholas Bellinger 						pr_reg_p->pr_res_mapped_lun,
2335c66ac9dbSNicholas Bellinger 						0x2A,
2336c66ac9dbSNicholas Bellinger 						ASCQ_2AH_RESERVATIONS_RELEASED);
2337c66ac9dbSNicholas Bellinger 				}
2338c66ac9dbSNicholas Bellinger 			}
2339c66ac9dbSNicholas Bellinger 			spin_unlock(&pr_tmpl->registration_lock);
2340c66ac9dbSNicholas Bellinger 
23416708bb27SAndy Grover 			if (!aptpl) {
2342c66ac9dbSNicholas Bellinger 				pr_tmpl->pr_aptpl_active = 0;
2343c66ac9dbSNicholas Bellinger 				core_scsi3_update_and_write_aptpl(dev, NULL, 0);
23446708bb27SAndy Grover 				pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2345c66ac9dbSNicholas Bellinger 						" for UNREGISTER\n");
2346c66ac9dbSNicholas Bellinger 				return 0;
2347c66ac9dbSNicholas Bellinger 			}
2348c66ac9dbSNicholas Bellinger 
2349c66ac9dbSNicholas Bellinger 			ret = core_scsi3_update_and_write_aptpl(dev,
2350c66ac9dbSNicholas Bellinger 					&pr_aptpl_buf[0],
2351c66ac9dbSNicholas Bellinger 					pr_tmpl->pr_aptpl_buf_len);
23526708bb27SAndy Grover 			if (!ret) {
2353c66ac9dbSNicholas Bellinger 				pr_tmpl->pr_aptpl_active = 1;
23546708bb27SAndy Grover 				pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2355c66ac9dbSNicholas Bellinger 						" for UNREGISTER\n");
2356c66ac9dbSNicholas Bellinger 			}
2357c66ac9dbSNicholas Bellinger 
2358c66ac9dbSNicholas Bellinger 			kfree(pr_aptpl_buf);
2359c66ac9dbSNicholas Bellinger 			return ret;
2360c66ac9dbSNicholas Bellinger 		} else {
2361c66ac9dbSNicholas Bellinger 			/*
2362c66ac9dbSNicholas Bellinger 			 * Increment PRgeneration counter for struct se_device"
2363c66ac9dbSNicholas Bellinger 			 * upon a successful REGISTER, see spc4r17 section 6.3.2
2364c66ac9dbSNicholas Bellinger 			 * READ_KEYS service action.
2365c66ac9dbSNicholas Bellinger 			 */
2366c66ac9dbSNicholas Bellinger 			pr_reg->pr_res_generation = core_scsi3_pr_generation(
23675951146dSAndy Grover 							cmd->se_dev);
2368c66ac9dbSNicholas Bellinger 			pr_reg->pr_res_key = sa_res_key;
23696708bb27SAndy Grover 			pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2370c66ac9dbSNicholas Bellinger 				" Key for %s to: 0x%016Lx PRgeneration:"
2371e3d6f909SAndy Grover 				" 0x%08x\n", cmd->se_tfo->get_fabric_name(),
2372c66ac9dbSNicholas Bellinger 				(ignore_key) ? "_AND_IGNORE_EXISTING_KEY" : "",
2373c66ac9dbSNicholas Bellinger 				pr_reg->pr_reg_nacl->initiatorname,
2374c66ac9dbSNicholas Bellinger 				pr_reg->pr_res_key, pr_reg->pr_res_generation);
2375c66ac9dbSNicholas Bellinger 
23766708bb27SAndy Grover 			if (!aptpl) {
2377c66ac9dbSNicholas Bellinger 				pr_tmpl->pr_aptpl_active = 0;
2378c66ac9dbSNicholas Bellinger 				core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2379c66ac9dbSNicholas Bellinger 				core_scsi3_put_pr_reg(pr_reg);
23806708bb27SAndy Grover 				pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2381c66ac9dbSNicholas Bellinger 						" for REGISTER\n");
2382c66ac9dbSNicholas Bellinger 				return 0;
2383c66ac9dbSNicholas Bellinger 			}
2384c66ac9dbSNicholas Bellinger 
2385c66ac9dbSNicholas Bellinger 			ret = core_scsi3_update_and_write_aptpl(dev,
2386c66ac9dbSNicholas Bellinger 					&pr_aptpl_buf[0],
2387c66ac9dbSNicholas Bellinger 					pr_tmpl->pr_aptpl_buf_len);
23886708bb27SAndy Grover 			if (!ret) {
2389c66ac9dbSNicholas Bellinger 				pr_tmpl->pr_aptpl_active = 1;
23906708bb27SAndy Grover 				pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2391c66ac9dbSNicholas Bellinger 						" for REGISTER\n");
2392c66ac9dbSNicholas Bellinger 			}
2393c66ac9dbSNicholas Bellinger 
2394c66ac9dbSNicholas Bellinger 			kfree(pr_aptpl_buf);
2395c66ac9dbSNicholas Bellinger 			core_scsi3_put_pr_reg(pr_reg);
2396c66ac9dbSNicholas Bellinger 		}
2397c66ac9dbSNicholas Bellinger 	}
2398c66ac9dbSNicholas Bellinger 	return 0;
2399c66ac9dbSNicholas Bellinger }
2400c66ac9dbSNicholas Bellinger 
2401c66ac9dbSNicholas Bellinger unsigned char *core_scsi3_pr_dump_type(int type)
2402c66ac9dbSNicholas Bellinger {
2403c66ac9dbSNicholas Bellinger 	switch (type) {
2404c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE:
2405c66ac9dbSNicholas Bellinger 		return "Write Exclusive Access";
2406c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS:
2407c66ac9dbSNicholas Bellinger 		return "Exclusive Access";
2408c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2409c66ac9dbSNicholas Bellinger 		return "Write Exclusive Access, Registrants Only";
2410c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2411c66ac9dbSNicholas Bellinger 		return "Exclusive Access, Registrants Only";
2412c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2413c66ac9dbSNicholas Bellinger 		return "Write Exclusive Access, All Registrants";
2414c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2415c66ac9dbSNicholas Bellinger 		return "Exclusive Access, All Registrants";
2416c66ac9dbSNicholas Bellinger 	default:
2417c66ac9dbSNicholas Bellinger 		break;
2418c66ac9dbSNicholas Bellinger 	}
2419c66ac9dbSNicholas Bellinger 
2420c66ac9dbSNicholas Bellinger 	return "Unknown SPC-3 PR Type";
2421c66ac9dbSNicholas Bellinger }
2422c66ac9dbSNicholas Bellinger 
2423c66ac9dbSNicholas Bellinger static int core_scsi3_pro_reserve(
2424c66ac9dbSNicholas Bellinger 	struct se_cmd *cmd,
2425c66ac9dbSNicholas Bellinger 	struct se_device *dev,
2426c66ac9dbSNicholas Bellinger 	int type,
2427c66ac9dbSNicholas Bellinger 	int scope,
2428c66ac9dbSNicholas Bellinger 	u64 res_key)
2429c66ac9dbSNicholas Bellinger {
2430e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
2431e3d6f909SAndy Grover 	struct se_lun *se_lun = cmd->se_lun;
2432c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_res_holder;
24330fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
2434c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
2435c66ac9dbSNicholas Bellinger 	int ret, prf_isid;
2436c66ac9dbSNicholas Bellinger 
2437c66ac9dbSNicholas Bellinger 	memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2438c66ac9dbSNicholas Bellinger 
24396708bb27SAndy Grover 	if (!se_sess || !se_lun) {
24406708bb27SAndy Grover 		pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
244103e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
244203e98c9eSNicholas Bellinger 		return -EINVAL;
2443c66ac9dbSNicholas Bellinger 	}
2444c66ac9dbSNicholas Bellinger 	/*
2445c66ac9dbSNicholas Bellinger 	 * Locate the existing *pr_reg via struct se_node_acl pointers
2446c66ac9dbSNicholas Bellinger 	 */
24475951146dSAndy Grover 	pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2448c66ac9dbSNicholas Bellinger 				se_sess);
24496708bb27SAndy Grover 	if (!pr_reg) {
24506708bb27SAndy Grover 		pr_err("SPC-3 PR: Unable to locate"
2451c66ac9dbSNicholas Bellinger 			" PR_REGISTERED *pr_reg for RESERVE\n");
245203e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
245303e98c9eSNicholas Bellinger 		return -EINVAL;
2454c66ac9dbSNicholas Bellinger 	}
2455c66ac9dbSNicholas Bellinger 	/*
2456c66ac9dbSNicholas Bellinger 	 * From spc4r17 Section 5.7.9: Reserving:
2457c66ac9dbSNicholas Bellinger 	 *
2458c66ac9dbSNicholas Bellinger 	 * An application client creates a persistent reservation by issuing
2459c66ac9dbSNicholas Bellinger 	 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2460c66ac9dbSNicholas Bellinger 	 * a registered I_T nexus with the following parameters:
2461c66ac9dbSNicholas Bellinger 	 *    a) RESERVATION KEY set to the value of the reservation key that is
2462c66ac9dbSNicholas Bellinger 	 * 	 registered with the logical unit for the I_T nexus; and
2463c66ac9dbSNicholas Bellinger 	 */
2464c66ac9dbSNicholas Bellinger 	if (res_key != pr_reg->pr_res_key) {
24656708bb27SAndy Grover 		pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2466c66ac9dbSNicholas Bellinger 			" does not match existing SA REGISTER res_key:"
2467c66ac9dbSNicholas Bellinger 			" 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2468c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg);
246903e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
247003e98c9eSNicholas Bellinger 		return -EINVAL;
2471c66ac9dbSNicholas Bellinger 	}
2472c66ac9dbSNicholas Bellinger 	/*
2473c66ac9dbSNicholas Bellinger 	 * From spc4r17 Section 5.7.9: Reserving:
2474c66ac9dbSNicholas Bellinger 	 *
2475c66ac9dbSNicholas Bellinger 	 * From above:
2476c66ac9dbSNicholas Bellinger 	 *  b) TYPE field and SCOPE field set to the persistent reservation
2477c66ac9dbSNicholas Bellinger 	 *     being created.
2478c66ac9dbSNicholas Bellinger 	 *
2479c66ac9dbSNicholas Bellinger 	 * Only one persistent reservation is allowed at a time per logical unit
2480c66ac9dbSNicholas Bellinger 	 * and that persistent reservation has a scope of LU_SCOPE.
2481c66ac9dbSNicholas Bellinger 	 */
2482c66ac9dbSNicholas Bellinger 	if (scope != PR_SCOPE_LU_SCOPE) {
24836708bb27SAndy Grover 		pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2484c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg);
248503e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
248603e98c9eSNicholas Bellinger 		return -EINVAL;
2487c66ac9dbSNicholas Bellinger 	}
2488c66ac9dbSNicholas Bellinger 	/*
2489c66ac9dbSNicholas Bellinger 	 * See if we have an existing PR reservation holder pointer at
2490c66ac9dbSNicholas Bellinger 	 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2491c66ac9dbSNicholas Bellinger 	 * *pr_res_holder.
2492c66ac9dbSNicholas Bellinger 	 */
2493c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
2494c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
2495ee1b1b9cSAndy Grover 	if (pr_res_holder) {
2496c66ac9dbSNicholas Bellinger 		/*
2497c66ac9dbSNicholas Bellinger 		 * From spc4r17 Section 5.7.9: Reserving:
2498c66ac9dbSNicholas Bellinger 		 *
2499c66ac9dbSNicholas Bellinger 		 * If the device server receives a PERSISTENT RESERVE OUT
2500c66ac9dbSNicholas Bellinger 		 * command from an I_T nexus other than a persistent reservation
2501c66ac9dbSNicholas Bellinger 		 * holder (see 5.7.10) that attempts to create a persistent
2502c66ac9dbSNicholas Bellinger 		 * reservation when a persistent reservation already exists for
2503c66ac9dbSNicholas Bellinger 		 * the logical unit, then the command shall be completed with
2504c66ac9dbSNicholas Bellinger 		 * RESERVATION CONFLICT status.
2505c66ac9dbSNicholas Bellinger 		 */
2506c66ac9dbSNicholas Bellinger 		if (pr_res_holder != pr_reg) {
2507c66ac9dbSNicholas Bellinger 			struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
25086708bb27SAndy Grover 			pr_err("SPC-3 PR: Attempted RESERVE from"
2509c66ac9dbSNicholas Bellinger 				" [%s]: %s while reservation already held by"
2510c66ac9dbSNicholas Bellinger 				" [%s]: %s, returning RESERVATION_CONFLICT\n",
2511e3d6f909SAndy Grover 				cmd->se_tfo->get_fabric_name(),
2512c66ac9dbSNicholas Bellinger 				se_sess->se_node_acl->initiatorname,
2513e3d6f909SAndy Grover 				pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2514c66ac9dbSNicholas Bellinger 				pr_res_holder->pr_reg_nacl->initiatorname);
2515c66ac9dbSNicholas Bellinger 
2516c66ac9dbSNicholas Bellinger 			spin_unlock(&dev->dev_reservation_lock);
2517c66ac9dbSNicholas Bellinger 			core_scsi3_put_pr_reg(pr_reg);
251803e98c9eSNicholas Bellinger 			cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
251903e98c9eSNicholas Bellinger 			return -EINVAL;
2520c66ac9dbSNicholas Bellinger 		}
2521c66ac9dbSNicholas Bellinger 		/*
2522c66ac9dbSNicholas Bellinger 		 * From spc4r17 Section 5.7.9: Reserving:
2523c66ac9dbSNicholas Bellinger 		 *
2524c66ac9dbSNicholas Bellinger 		 * If a persistent reservation holder attempts to modify the
2525c66ac9dbSNicholas Bellinger 		 * type or scope of an existing persistent reservation, the
2526c66ac9dbSNicholas Bellinger 		 * command shall be completed with RESERVATION CONFLICT status.
2527c66ac9dbSNicholas Bellinger 		 */
2528c66ac9dbSNicholas Bellinger 		if ((pr_res_holder->pr_res_type != type) ||
2529c66ac9dbSNicholas Bellinger 		    (pr_res_holder->pr_res_scope != scope)) {
2530c66ac9dbSNicholas Bellinger 			struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
25316708bb27SAndy Grover 			pr_err("SPC-3 PR: Attempted RESERVE from"
2532c66ac9dbSNicholas Bellinger 				" [%s]: %s trying to change TYPE and/or SCOPE,"
2533c66ac9dbSNicholas Bellinger 				" while reservation already held by [%s]: %s,"
2534c66ac9dbSNicholas Bellinger 				" returning RESERVATION_CONFLICT\n",
2535e3d6f909SAndy Grover 				cmd->se_tfo->get_fabric_name(),
2536c66ac9dbSNicholas Bellinger 				se_sess->se_node_acl->initiatorname,
2537e3d6f909SAndy Grover 				pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2538c66ac9dbSNicholas Bellinger 				pr_res_holder->pr_reg_nacl->initiatorname);
2539c66ac9dbSNicholas Bellinger 
2540c66ac9dbSNicholas Bellinger 			spin_unlock(&dev->dev_reservation_lock);
2541c66ac9dbSNicholas Bellinger 			core_scsi3_put_pr_reg(pr_reg);
254203e98c9eSNicholas Bellinger 			cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
254303e98c9eSNicholas Bellinger 			return -EINVAL;
2544c66ac9dbSNicholas Bellinger 		}
2545c66ac9dbSNicholas Bellinger 		/*
2546c66ac9dbSNicholas Bellinger 		 * From spc4r17 Section 5.7.9: Reserving:
2547c66ac9dbSNicholas Bellinger 		 *
2548c66ac9dbSNicholas Bellinger 		 * If the device server receives a PERSISTENT RESERVE OUT
2549c66ac9dbSNicholas Bellinger 		 * command with RESERVE service action where the TYPE field and
2550c66ac9dbSNicholas Bellinger 		 * the SCOPE field contain the same values as the existing type
2551c66ac9dbSNicholas Bellinger 		 * and scope from a persistent reservation holder, it shall not
2552c66ac9dbSNicholas Bellinger 		 * make any change to the existing persistent reservation and
2553c66ac9dbSNicholas Bellinger 		 * shall completethe command with GOOD status.
2554c66ac9dbSNicholas Bellinger 		 */
2555c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
2556c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg);
255703e98c9eSNicholas Bellinger 		return 0;
2558c66ac9dbSNicholas Bellinger 	}
2559c66ac9dbSNicholas Bellinger 	/*
2560c66ac9dbSNicholas Bellinger 	 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2561c66ac9dbSNicholas Bellinger 	 * TYPE/SCOPE.  Also set the received scope and type in *pr_reg.
2562c66ac9dbSNicholas Bellinger 	 */
2563c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_scope = scope;
2564c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_type = type;
2565c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_holder = 1;
2566c66ac9dbSNicholas Bellinger 	dev->dev_pr_res_holder = pr_reg;
2567c66ac9dbSNicholas Bellinger 	prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2568c66ac9dbSNicholas Bellinger 				PR_REG_ISID_ID_LEN);
2569c66ac9dbSNicholas Bellinger 
25706708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2571c66ac9dbSNicholas Bellinger 		" reservation holder TYPE: %s ALL_TG_PT: %d\n",
2572e3d6f909SAndy Grover 		cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
2573c66ac9dbSNicholas Bellinger 		(pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
25746708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2575e3d6f909SAndy Grover 			cmd->se_tfo->get_fabric_name(),
2576c66ac9dbSNicholas Bellinger 			se_sess->se_node_acl->initiatorname,
2577c66ac9dbSNicholas Bellinger 			(prf_isid) ? &i_buf[0] : "");
2578c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
2579c66ac9dbSNicholas Bellinger 
2580c66ac9dbSNicholas Bellinger 	if (pr_tmpl->pr_aptpl_active) {
25815951146dSAndy Grover 		ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2582c66ac9dbSNicholas Bellinger 				&pr_reg->pr_aptpl_buf[0],
2583c66ac9dbSNicholas Bellinger 				pr_tmpl->pr_aptpl_buf_len);
25846708bb27SAndy Grover 		if (!ret)
25856708bb27SAndy Grover 			pr_debug("SPC-3 PR: Updated APTPL metadata"
2586c66ac9dbSNicholas Bellinger 					" for RESERVE\n");
2587c66ac9dbSNicholas Bellinger 	}
2588c66ac9dbSNicholas Bellinger 
2589c66ac9dbSNicholas Bellinger 	core_scsi3_put_pr_reg(pr_reg);
2590c66ac9dbSNicholas Bellinger 	return 0;
2591c66ac9dbSNicholas Bellinger }
2592c66ac9dbSNicholas Bellinger 
2593c66ac9dbSNicholas Bellinger static int core_scsi3_emulate_pro_reserve(
2594c66ac9dbSNicholas Bellinger 	struct se_cmd *cmd,
2595c66ac9dbSNicholas Bellinger 	int type,
2596c66ac9dbSNicholas Bellinger 	int scope,
2597c66ac9dbSNicholas Bellinger 	u64 res_key)
2598c66ac9dbSNicholas Bellinger {
2599c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
2600c66ac9dbSNicholas Bellinger 	int ret = 0;
2601c66ac9dbSNicholas Bellinger 
2602c66ac9dbSNicholas Bellinger 	switch (type) {
2603c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE:
2604c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS:
2605c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2606c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2607c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2608c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2609c66ac9dbSNicholas Bellinger 		ret = core_scsi3_pro_reserve(cmd, dev, type, scope, res_key);
2610c66ac9dbSNicholas Bellinger 		break;
2611c66ac9dbSNicholas Bellinger 	default:
26126708bb27SAndy Grover 		pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2613c66ac9dbSNicholas Bellinger 			" 0x%02x\n", type);
261403e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
261503e98c9eSNicholas Bellinger 		return -EINVAL;
2616c66ac9dbSNicholas Bellinger 	}
2617c66ac9dbSNicholas Bellinger 
2618c66ac9dbSNicholas Bellinger 	return ret;
2619c66ac9dbSNicholas Bellinger }
2620c66ac9dbSNicholas Bellinger 
2621c66ac9dbSNicholas Bellinger /*
2622c66ac9dbSNicholas Bellinger  * Called with struct se_device->dev_reservation_lock held.
2623c66ac9dbSNicholas Bellinger  */
2624c66ac9dbSNicholas Bellinger static void __core_scsi3_complete_pro_release(
2625c66ac9dbSNicholas Bellinger 	struct se_device *dev,
2626c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl,
2627c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg,
2628c66ac9dbSNicholas Bellinger 	int explict)
2629c66ac9dbSNicholas Bellinger {
2630c66ac9dbSNicholas Bellinger 	struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2631c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
2632c66ac9dbSNicholas Bellinger 	int prf_isid;
2633c66ac9dbSNicholas Bellinger 
2634c66ac9dbSNicholas Bellinger 	memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2635c66ac9dbSNicholas Bellinger 	prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2636c66ac9dbSNicholas Bellinger 				PR_REG_ISID_ID_LEN);
2637c66ac9dbSNicholas Bellinger 	/*
2638c66ac9dbSNicholas Bellinger 	 * Go ahead and release the current PR reservation holder.
2639c66ac9dbSNicholas Bellinger 	 */
2640c66ac9dbSNicholas Bellinger 	dev->dev_pr_res_holder = NULL;
2641c66ac9dbSNicholas Bellinger 
26426708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2643c66ac9dbSNicholas Bellinger 		" reservation holder TYPE: %s ALL_TG_PT: %d\n",
2644c66ac9dbSNicholas Bellinger 		tfo->get_fabric_name(), (explict) ? "explict" : "implict",
2645c66ac9dbSNicholas Bellinger 		core_scsi3_pr_dump_type(pr_reg->pr_res_type),
2646c66ac9dbSNicholas Bellinger 		(pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
26476708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2648c66ac9dbSNicholas Bellinger 		tfo->get_fabric_name(), se_nacl->initiatorname,
2649c66ac9dbSNicholas Bellinger 		(prf_isid) ? &i_buf[0] : "");
2650c66ac9dbSNicholas Bellinger 	/*
2651c66ac9dbSNicholas Bellinger 	 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2652c66ac9dbSNicholas Bellinger 	 */
2653c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2654c66ac9dbSNicholas Bellinger }
2655c66ac9dbSNicholas Bellinger 
2656c66ac9dbSNicholas Bellinger static int core_scsi3_emulate_pro_release(
2657c66ac9dbSNicholas Bellinger 	struct se_cmd *cmd,
2658c66ac9dbSNicholas Bellinger 	int type,
2659c66ac9dbSNicholas Bellinger 	int scope,
2660c66ac9dbSNicholas Bellinger 	u64 res_key)
2661c66ac9dbSNicholas Bellinger {
2662c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
2663e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
2664e3d6f909SAndy Grover 	struct se_lun *se_lun = cmd->se_lun;
2665c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
26660fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
2667c66ac9dbSNicholas Bellinger 	int ret, all_reg = 0;
2668c66ac9dbSNicholas Bellinger 
26696708bb27SAndy Grover 	if (!se_sess || !se_lun) {
26706708bb27SAndy Grover 		pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
267103e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
267203e98c9eSNicholas Bellinger 		return -EINVAL;
2673c66ac9dbSNicholas Bellinger 	}
2674c66ac9dbSNicholas Bellinger 	/*
2675c66ac9dbSNicholas Bellinger 	 * Locate the existing *pr_reg via struct se_node_acl pointers
2676c66ac9dbSNicholas Bellinger 	 */
2677c66ac9dbSNicholas Bellinger 	pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
26786708bb27SAndy Grover 	if (!pr_reg) {
26796708bb27SAndy Grover 		pr_err("SPC-3 PR: Unable to locate"
2680c66ac9dbSNicholas Bellinger 			" PR_REGISTERED *pr_reg for RELEASE\n");
268103e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
268203e98c9eSNicholas Bellinger 		return -EINVAL;
2683c66ac9dbSNicholas Bellinger 	}
2684c66ac9dbSNicholas Bellinger 	/*
2685c66ac9dbSNicholas Bellinger 	 * From spc4r17 Section 5.7.11.2 Releasing:
2686c66ac9dbSNicholas Bellinger 	 *
2687c66ac9dbSNicholas Bellinger 	 * If there is no persistent reservation or in response to a persistent
2688c66ac9dbSNicholas Bellinger 	 * reservation release request from a registered I_T nexus that is not a
2689c66ac9dbSNicholas Bellinger 	 * persistent reservation holder (see 5.7.10), the device server shall
2690c66ac9dbSNicholas Bellinger 	 * do the following:
2691c66ac9dbSNicholas Bellinger 	 *
2692c66ac9dbSNicholas Bellinger 	 *     a) Not release the persistent reservation, if any;
2693c66ac9dbSNicholas Bellinger 	 *     b) Not remove any registrations; and
2694c66ac9dbSNicholas Bellinger 	 *     c) Complete the command with GOOD status.
2695c66ac9dbSNicholas Bellinger 	 */
2696c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
2697c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
26986708bb27SAndy Grover 	if (!pr_res_holder) {
2699c66ac9dbSNicholas Bellinger 		/*
2700c66ac9dbSNicholas Bellinger 		 * No persistent reservation, return GOOD status.
2701c66ac9dbSNicholas Bellinger 		 */
2702c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
2703c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg);
270403e98c9eSNicholas Bellinger 		return 0;
2705c66ac9dbSNicholas Bellinger 	}
2706c66ac9dbSNicholas Bellinger 	if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2707c66ac9dbSNicholas Bellinger 	    (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
2708c66ac9dbSNicholas Bellinger 		all_reg = 1;
2709c66ac9dbSNicholas Bellinger 
2710c66ac9dbSNicholas Bellinger 	if ((all_reg == 0) && (pr_res_holder != pr_reg)) {
2711c66ac9dbSNicholas Bellinger 		/*
2712c66ac9dbSNicholas Bellinger 		 * Non 'All Registrants' PR Type cases..
2713c66ac9dbSNicholas Bellinger 		 * Release request from a registered I_T nexus that is not a
2714c66ac9dbSNicholas Bellinger 		 * persistent reservation holder. return GOOD status.
2715c66ac9dbSNicholas Bellinger 		 */
2716c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
2717c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg);
271803e98c9eSNicholas Bellinger 		return 0;
2719c66ac9dbSNicholas Bellinger 	}
2720c66ac9dbSNicholas Bellinger 	/*
2721c66ac9dbSNicholas Bellinger 	 * From spc4r17 Section 5.7.11.2 Releasing:
2722c66ac9dbSNicholas Bellinger 	 *
2723c66ac9dbSNicholas Bellinger 	 * Only the persistent reservation holder (see 5.7.10) is allowed to
2724c66ac9dbSNicholas Bellinger 	 * release a persistent reservation.
2725c66ac9dbSNicholas Bellinger 	 *
2726c66ac9dbSNicholas Bellinger 	 * An application client releases the persistent reservation by issuing
2727c66ac9dbSNicholas Bellinger 	 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2728c66ac9dbSNicholas Bellinger 	 * an I_T nexus that is a persistent reservation holder with the
2729c66ac9dbSNicholas Bellinger 	 * following parameters:
2730c66ac9dbSNicholas Bellinger 	 *
2731c66ac9dbSNicholas Bellinger 	 *     a) RESERVATION KEY field set to the value of the reservation key
2732c66ac9dbSNicholas Bellinger 	 *	  that is registered with the logical unit for the I_T nexus;
2733c66ac9dbSNicholas Bellinger 	 */
2734c66ac9dbSNicholas Bellinger 	if (res_key != pr_reg->pr_res_key) {
27356708bb27SAndy Grover 		pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2736c66ac9dbSNicholas Bellinger 			" does not match existing SA REGISTER res_key:"
2737c66ac9dbSNicholas Bellinger 			" 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2738c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
2739c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg);
274003e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
274103e98c9eSNicholas Bellinger 		return -EINVAL;
2742c66ac9dbSNicholas Bellinger 	}
2743c66ac9dbSNicholas Bellinger 	/*
2744c66ac9dbSNicholas Bellinger 	 * From spc4r17 Section 5.7.11.2 Releasing and above:
2745c66ac9dbSNicholas Bellinger 	 *
2746c66ac9dbSNicholas Bellinger 	 * b) TYPE field and SCOPE field set to match the persistent
2747c66ac9dbSNicholas Bellinger 	 *    reservation being released.
2748c66ac9dbSNicholas Bellinger 	 */
2749c66ac9dbSNicholas Bellinger 	if ((pr_res_holder->pr_res_type != type) ||
2750c66ac9dbSNicholas Bellinger 	    (pr_res_holder->pr_res_scope != scope)) {
2751c66ac9dbSNicholas Bellinger 		struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
27526708bb27SAndy Grover 		pr_err("SPC-3 PR RELEASE: Attempted to release"
2753c66ac9dbSNicholas Bellinger 			" reservation from [%s]: %s with different TYPE "
2754c66ac9dbSNicholas Bellinger 			"and/or SCOPE  while reservation already held by"
2755c66ac9dbSNicholas Bellinger 			" [%s]: %s, returning RESERVATION_CONFLICT\n",
2756e3d6f909SAndy Grover 			cmd->se_tfo->get_fabric_name(),
2757c66ac9dbSNicholas Bellinger 			se_sess->se_node_acl->initiatorname,
2758e3d6f909SAndy Grover 			pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2759c66ac9dbSNicholas Bellinger 			pr_res_holder->pr_reg_nacl->initiatorname);
2760c66ac9dbSNicholas Bellinger 
2761c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
2762c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg);
276303e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
276403e98c9eSNicholas Bellinger 		return -EINVAL;
2765c66ac9dbSNicholas Bellinger 	}
2766c66ac9dbSNicholas Bellinger 	/*
2767c66ac9dbSNicholas Bellinger 	 * In response to a persistent reservation release request from the
2768c66ac9dbSNicholas Bellinger 	 * persistent reservation holder the device server shall perform a
2769c66ac9dbSNicholas Bellinger 	 * release by doing the following as an uninterrupted series of actions:
2770c66ac9dbSNicholas Bellinger 	 * a) Release the persistent reservation;
2771c66ac9dbSNicholas Bellinger 	 * b) Not remove any registration(s);
2772c66ac9dbSNicholas Bellinger 	 * c) If the released persistent reservation is a registrants only type
2773c66ac9dbSNicholas Bellinger 	 * or all registrants type persistent reservation,
2774c66ac9dbSNicholas Bellinger 	 *    the device server shall establish a unit attention condition for
2775c66ac9dbSNicholas Bellinger 	 *    the initiator port associated with every regis-
2776c66ac9dbSNicholas Bellinger 	 *    tered I_T nexus other than I_T nexus on which the PERSISTENT
2777c66ac9dbSNicholas Bellinger 	 *    RESERVE OUT command with RELEASE service action was received,
2778c66ac9dbSNicholas Bellinger 	 *    with the additional sense code set to RESERVATIONS RELEASED; and
2779c66ac9dbSNicholas Bellinger 	 * d) If the persistent reservation is of any other type, the device
2780c66ac9dbSNicholas Bellinger 	 *    server shall not establish a unit attention condition.
2781c66ac9dbSNicholas Bellinger 	 */
2782c66ac9dbSNicholas Bellinger 	__core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
2783c66ac9dbSNicholas Bellinger 			pr_reg, 1);
2784c66ac9dbSNicholas Bellinger 
2785c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
2786c66ac9dbSNicholas Bellinger 
2787c66ac9dbSNicholas Bellinger 	if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2788c66ac9dbSNicholas Bellinger 	    (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2789c66ac9dbSNicholas Bellinger 	    (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2790c66ac9dbSNicholas Bellinger 	    (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2791c66ac9dbSNicholas Bellinger 		/*
2792c66ac9dbSNicholas Bellinger 		 * If no UNIT ATTENTION conditions will be established for
2793c66ac9dbSNicholas Bellinger 		 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2794c66ac9dbSNicholas Bellinger 		 * go ahead and check for APTPL=1 update+write below
2795c66ac9dbSNicholas Bellinger 		 */
2796c66ac9dbSNicholas Bellinger 		goto write_aptpl;
2797c66ac9dbSNicholas Bellinger 	}
2798c66ac9dbSNicholas Bellinger 
2799c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
2800c66ac9dbSNicholas Bellinger 	list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2801c66ac9dbSNicholas Bellinger 			pr_reg_list) {
2802c66ac9dbSNicholas Bellinger 		/*
2803c66ac9dbSNicholas Bellinger 		 * Do not establish a UNIT ATTENTION condition
2804c66ac9dbSNicholas Bellinger 		 * for the calling I_T Nexus
2805c66ac9dbSNicholas Bellinger 		 */
2806c66ac9dbSNicholas Bellinger 		if (pr_reg_p == pr_reg)
2807c66ac9dbSNicholas Bellinger 			continue;
2808c66ac9dbSNicholas Bellinger 
2809c66ac9dbSNicholas Bellinger 		core_scsi3_ua_allocate(pr_reg_p->pr_reg_nacl,
2810c66ac9dbSNicholas Bellinger 				pr_reg_p->pr_res_mapped_lun,
2811c66ac9dbSNicholas Bellinger 				0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2812c66ac9dbSNicholas Bellinger 	}
2813c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
2814c66ac9dbSNicholas Bellinger 
2815c66ac9dbSNicholas Bellinger write_aptpl:
2816c66ac9dbSNicholas Bellinger 	if (pr_tmpl->pr_aptpl_active) {
28175951146dSAndy Grover 		ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
2818c66ac9dbSNicholas Bellinger 				&pr_reg->pr_aptpl_buf[0],
2819c66ac9dbSNicholas Bellinger 				pr_tmpl->pr_aptpl_buf_len);
28206708bb27SAndy Grover 		if (!ret)
28216708bb27SAndy Grover 			pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
2822c66ac9dbSNicholas Bellinger 	}
2823c66ac9dbSNicholas Bellinger 
2824c66ac9dbSNicholas Bellinger 	core_scsi3_put_pr_reg(pr_reg);
2825c66ac9dbSNicholas Bellinger 	return 0;
2826c66ac9dbSNicholas Bellinger }
2827c66ac9dbSNicholas Bellinger 
2828c66ac9dbSNicholas Bellinger static int core_scsi3_emulate_pro_clear(
2829c66ac9dbSNicholas Bellinger 	struct se_cmd *cmd,
2830c66ac9dbSNicholas Bellinger 	u64 res_key)
2831c66ac9dbSNicholas Bellinger {
2832c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
2833c66ac9dbSNicholas Bellinger 	struct se_node_acl *pr_reg_nacl;
2834e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
28350fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
2836c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2837c66ac9dbSNicholas Bellinger 	u32 pr_res_mapped_lun = 0;
2838c66ac9dbSNicholas Bellinger 	int calling_it_nexus = 0;
2839c66ac9dbSNicholas Bellinger 	/*
2840c66ac9dbSNicholas Bellinger 	 * Locate the existing *pr_reg via struct se_node_acl pointers
2841c66ac9dbSNicholas Bellinger 	 */
28425951146dSAndy Grover 	pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
2843c66ac9dbSNicholas Bellinger 			se_sess->se_node_acl, se_sess);
28446708bb27SAndy Grover 	if (!pr_reg_n) {
28456708bb27SAndy Grover 		pr_err("SPC-3 PR: Unable to locate"
2846c66ac9dbSNicholas Bellinger 			" PR_REGISTERED *pr_reg for CLEAR\n");
284703e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
284803e98c9eSNicholas Bellinger 		return -EINVAL;
2849c66ac9dbSNicholas Bellinger 	}
2850c66ac9dbSNicholas Bellinger 	/*
2851c66ac9dbSNicholas Bellinger 	 * From spc4r17 section 5.7.11.6, Clearing:
2852c66ac9dbSNicholas Bellinger 	 *
2853c66ac9dbSNicholas Bellinger 	 * Any application client may release the persistent reservation and
2854c66ac9dbSNicholas Bellinger 	 * remove all registrations from a device server by issuing a
2855c66ac9dbSNicholas Bellinger 	 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2856c66ac9dbSNicholas Bellinger 	 * registered I_T nexus with the following parameter:
2857c66ac9dbSNicholas Bellinger 	 *
2858c66ac9dbSNicholas Bellinger 	 *	a) RESERVATION KEY field set to the value of the reservation key
2859c66ac9dbSNicholas Bellinger 	 * 	   that is registered with the logical unit for the I_T nexus.
2860c66ac9dbSNicholas Bellinger 	 */
2861c66ac9dbSNicholas Bellinger 	if (res_key != pr_reg_n->pr_res_key) {
28626708bb27SAndy Grover 		pr_err("SPC-3 PR REGISTER: Received"
2863c66ac9dbSNicholas Bellinger 			" res_key: 0x%016Lx does not match"
2864c66ac9dbSNicholas Bellinger 			" existing SA REGISTER res_key:"
2865c66ac9dbSNicholas Bellinger 			" 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2866c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg_n);
286703e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
286803e98c9eSNicholas Bellinger 		return -EINVAL;
2869c66ac9dbSNicholas Bellinger 	}
2870c66ac9dbSNicholas Bellinger 	/*
2871c66ac9dbSNicholas Bellinger 	 * a) Release the persistent reservation, if any;
2872c66ac9dbSNicholas Bellinger 	 */
2873c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
2874c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
2875c66ac9dbSNicholas Bellinger 	if (pr_res_holder) {
2876c66ac9dbSNicholas Bellinger 		struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2877c66ac9dbSNicholas Bellinger 		__core_scsi3_complete_pro_release(dev, pr_res_nacl,
2878c66ac9dbSNicholas Bellinger 			pr_res_holder, 0);
2879c66ac9dbSNicholas Bellinger 	}
2880c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
2881c66ac9dbSNicholas Bellinger 	/*
2882c66ac9dbSNicholas Bellinger 	 * b) Remove all registration(s) (see spc4r17 5.7.7);
2883c66ac9dbSNicholas Bellinger 	 */
2884c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
2885c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2886c66ac9dbSNicholas Bellinger 			&pr_tmpl->registration_list, pr_reg_list) {
2887c66ac9dbSNicholas Bellinger 
2888c66ac9dbSNicholas Bellinger 		calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2889c66ac9dbSNicholas Bellinger 		pr_reg_nacl = pr_reg->pr_reg_nacl;
2890c66ac9dbSNicholas Bellinger 		pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2891c66ac9dbSNicholas Bellinger 		__core_scsi3_free_registration(dev, pr_reg, NULL,
2892c66ac9dbSNicholas Bellinger 					calling_it_nexus);
2893c66ac9dbSNicholas Bellinger 		/*
2894c66ac9dbSNicholas Bellinger 		 * e) Establish a unit attention condition for the initiator
2895c66ac9dbSNicholas Bellinger 		 *    port associated with every registered I_T nexus other
2896c66ac9dbSNicholas Bellinger 		 *    than the I_T nexus on which the PERSISTENT RESERVE OUT
2897c66ac9dbSNicholas Bellinger 		 *    command with CLEAR service action was received, with the
2898c66ac9dbSNicholas Bellinger 		 *    additional sense code set to RESERVATIONS PREEMPTED.
2899c66ac9dbSNicholas Bellinger 		 */
29006708bb27SAndy Grover 		if (!calling_it_nexus)
2901c66ac9dbSNicholas Bellinger 			core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun,
2902c66ac9dbSNicholas Bellinger 				0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2903c66ac9dbSNicholas Bellinger 	}
2904c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
2905c66ac9dbSNicholas Bellinger 
29066708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2907e3d6f909SAndy Grover 		cmd->se_tfo->get_fabric_name());
2908c66ac9dbSNicholas Bellinger 
2909c66ac9dbSNicholas Bellinger 	if (pr_tmpl->pr_aptpl_active) {
29105951146dSAndy Grover 		core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
29116708bb27SAndy Grover 		pr_debug("SPC-3 PR: Updated APTPL metadata"
2912c66ac9dbSNicholas Bellinger 				" for CLEAR\n");
2913c66ac9dbSNicholas Bellinger 	}
2914c66ac9dbSNicholas Bellinger 
2915c66ac9dbSNicholas Bellinger 	core_scsi3_pr_generation(dev);
2916c66ac9dbSNicholas Bellinger 	return 0;
2917c66ac9dbSNicholas Bellinger }
2918c66ac9dbSNicholas Bellinger 
2919c66ac9dbSNicholas Bellinger /*
2920c66ac9dbSNicholas Bellinger  * Called with struct se_device->dev_reservation_lock held.
2921c66ac9dbSNicholas Bellinger  */
2922c66ac9dbSNicholas Bellinger static void __core_scsi3_complete_pro_preempt(
2923c66ac9dbSNicholas Bellinger 	struct se_device *dev,
2924c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg,
2925c66ac9dbSNicholas Bellinger 	struct list_head *preempt_and_abort_list,
2926c66ac9dbSNicholas Bellinger 	int type,
2927c66ac9dbSNicholas Bellinger 	int scope,
2928c66ac9dbSNicholas Bellinger 	int abort)
2929c66ac9dbSNicholas Bellinger {
2930c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
2931c66ac9dbSNicholas Bellinger 	struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2932c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
2933c66ac9dbSNicholas Bellinger 	int prf_isid;
2934c66ac9dbSNicholas Bellinger 
2935c66ac9dbSNicholas Bellinger 	memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2936c66ac9dbSNicholas Bellinger 	prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2937c66ac9dbSNicholas Bellinger 				PR_REG_ISID_ID_LEN);
2938c66ac9dbSNicholas Bellinger 	/*
2939c66ac9dbSNicholas Bellinger 	 * Do an implict RELEASE of the existing reservation.
2940c66ac9dbSNicholas Bellinger 	 */
2941c66ac9dbSNicholas Bellinger 	if (dev->dev_pr_res_holder)
2942c66ac9dbSNicholas Bellinger 		__core_scsi3_complete_pro_release(dev, nacl,
2943c66ac9dbSNicholas Bellinger 				dev->dev_pr_res_holder, 0);
2944c66ac9dbSNicholas Bellinger 
2945c66ac9dbSNicholas Bellinger 	dev->dev_pr_res_holder = pr_reg;
2946c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_holder = 1;
2947c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_type = type;
2948c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_scope = scope;
2949c66ac9dbSNicholas Bellinger 
29506708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2951c66ac9dbSNicholas Bellinger 		" reservation holder TYPE: %s ALL_TG_PT: %d\n",
2952c66ac9dbSNicholas Bellinger 		tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2953c66ac9dbSNicholas Bellinger 		core_scsi3_pr_dump_type(type),
2954c66ac9dbSNicholas Bellinger 		(pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
29556708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2956c66ac9dbSNicholas Bellinger 		tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2957c66ac9dbSNicholas Bellinger 		nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
2958c66ac9dbSNicholas Bellinger 	/*
2959c66ac9dbSNicholas Bellinger 	 * For PREEMPT_AND_ABORT, add the preempting reservation's
2960c66ac9dbSNicholas Bellinger 	 * struct t10_pr_registration to the list that will be compared
2961c66ac9dbSNicholas Bellinger 	 * against received CDBs..
2962c66ac9dbSNicholas Bellinger 	 */
2963c66ac9dbSNicholas Bellinger 	if (preempt_and_abort_list)
2964c66ac9dbSNicholas Bellinger 		list_add_tail(&pr_reg->pr_reg_abort_list,
2965c66ac9dbSNicholas Bellinger 				preempt_and_abort_list);
2966c66ac9dbSNicholas Bellinger }
2967c66ac9dbSNicholas Bellinger 
2968c66ac9dbSNicholas Bellinger static void core_scsi3_release_preempt_and_abort(
2969c66ac9dbSNicholas Bellinger 	struct list_head *preempt_and_abort_list,
2970c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg_holder)
2971c66ac9dbSNicholas Bellinger {
2972c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2973c66ac9dbSNicholas Bellinger 
2974c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2975c66ac9dbSNicholas Bellinger 				pr_reg_abort_list) {
2976c66ac9dbSNicholas Bellinger 
2977c66ac9dbSNicholas Bellinger 		list_del(&pr_reg->pr_reg_abort_list);
2978c66ac9dbSNicholas Bellinger 		if (pr_reg_holder == pr_reg)
2979c66ac9dbSNicholas Bellinger 			continue;
2980c66ac9dbSNicholas Bellinger 		if (pr_reg->pr_res_holder) {
29816708bb27SAndy Grover 			pr_warn("pr_reg->pr_res_holder still set\n");
2982c66ac9dbSNicholas Bellinger 			continue;
2983c66ac9dbSNicholas Bellinger 		}
2984c66ac9dbSNicholas Bellinger 
2985c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_deve = NULL;
2986c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_nacl = NULL;
2987c66ac9dbSNicholas Bellinger 		kfree(pr_reg->pr_aptpl_buf);
2988c66ac9dbSNicholas Bellinger 		kmem_cache_free(t10_pr_reg_cache, pr_reg);
2989c66ac9dbSNicholas Bellinger 	}
2990c66ac9dbSNicholas Bellinger }
2991c66ac9dbSNicholas Bellinger 
2992c66ac9dbSNicholas Bellinger static int core_scsi3_pro_preempt(
2993c66ac9dbSNicholas Bellinger 	struct se_cmd *cmd,
2994c66ac9dbSNicholas Bellinger 	int type,
2995c66ac9dbSNicholas Bellinger 	int scope,
2996c66ac9dbSNicholas Bellinger 	u64 res_key,
2997c66ac9dbSNicholas Bellinger 	u64 sa_res_key,
2998c66ac9dbSNicholas Bellinger 	int abort)
2999c66ac9dbSNicholas Bellinger {
30005951146dSAndy Grover 	struct se_device *dev = cmd->se_dev;
3001c66ac9dbSNicholas Bellinger 	struct se_node_acl *pr_reg_nacl;
3002e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
3003d0f474e5SRoland Dreier 	LIST_HEAD(preempt_and_abort_list);
3004c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
30050fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
3006c66ac9dbSNicholas Bellinger 	u32 pr_res_mapped_lun = 0;
3007c66ac9dbSNicholas Bellinger 	int all_reg = 0, calling_it_nexus = 0, released_regs = 0;
3008c66ac9dbSNicholas Bellinger 	int prh_type = 0, prh_scope = 0, ret;
3009c66ac9dbSNicholas Bellinger 
301003e98c9eSNicholas Bellinger 	if (!se_sess) {
301103e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
301203e98c9eSNicholas Bellinger 		return -EINVAL;
301303e98c9eSNicholas Bellinger 	}
3014c66ac9dbSNicholas Bellinger 
30155951146dSAndy Grover 	pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3016c66ac9dbSNicholas Bellinger 				se_sess);
30176708bb27SAndy Grover 	if (!pr_reg_n) {
30186708bb27SAndy Grover 		pr_err("SPC-3 PR: Unable to locate"
3019c66ac9dbSNicholas Bellinger 			" PR_REGISTERED *pr_reg for PREEMPT%s\n",
3020c66ac9dbSNicholas Bellinger 			(abort) ? "_AND_ABORT" : "");
302103e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
302203e98c9eSNicholas Bellinger 		return -EINVAL;
3023c66ac9dbSNicholas Bellinger 	}
3024c66ac9dbSNicholas Bellinger 	if (pr_reg_n->pr_res_key != res_key) {
3025c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg_n);
302603e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
302703e98c9eSNicholas Bellinger 		return -EINVAL;
3028c66ac9dbSNicholas Bellinger 	}
3029c66ac9dbSNicholas Bellinger 	if (scope != PR_SCOPE_LU_SCOPE) {
30306708bb27SAndy Grover 		pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
3031c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg_n);
303203e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
303303e98c9eSNicholas Bellinger 		return -EINVAL;
3034c66ac9dbSNicholas Bellinger 	}
3035c66ac9dbSNicholas Bellinger 
3036c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
3037c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
3038c66ac9dbSNicholas Bellinger 	if (pr_res_holder &&
3039c66ac9dbSNicholas Bellinger 	   ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3040c66ac9dbSNicholas Bellinger 	    (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
3041c66ac9dbSNicholas Bellinger 		all_reg = 1;
3042c66ac9dbSNicholas Bellinger 
30436708bb27SAndy Grover 	if (!all_reg && !sa_res_key) {
3044c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
3045c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg_n);
304603e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
304703e98c9eSNicholas Bellinger 		return -EINVAL;
3048c66ac9dbSNicholas Bellinger 	}
3049c66ac9dbSNicholas Bellinger 	/*
3050c66ac9dbSNicholas Bellinger 	 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
3051c66ac9dbSNicholas Bellinger 	 *
3052c66ac9dbSNicholas Bellinger 	 * If the SERVICE ACTION RESERVATION KEY field does not identify a
3053c66ac9dbSNicholas Bellinger 	 * persistent reservation holder or there is no persistent reservation
3054c66ac9dbSNicholas Bellinger 	 * holder (i.e., there is no persistent reservation), then the device
3055c66ac9dbSNicholas Bellinger 	 * server shall perform a preempt by doing the following in an
3056c66ac9dbSNicholas Bellinger 	 * uninterrupted series of actions. (See below..)
3057c66ac9dbSNicholas Bellinger 	 */
30586708bb27SAndy Grover 	if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
3059c66ac9dbSNicholas Bellinger 		/*
3060c66ac9dbSNicholas Bellinger 		 * No existing or SA Reservation Key matching reservations..
3061c66ac9dbSNicholas Bellinger 		 *
3062c66ac9dbSNicholas Bellinger 		 * PROUT SA PREEMPT with All Registrant type reservations are
3063c66ac9dbSNicholas Bellinger 		 * allowed to be processed without a matching SA Reservation Key
3064c66ac9dbSNicholas Bellinger 		 */
3065c66ac9dbSNicholas Bellinger 		spin_lock(&pr_tmpl->registration_lock);
3066c66ac9dbSNicholas Bellinger 		list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3067c66ac9dbSNicholas Bellinger 				&pr_tmpl->registration_list, pr_reg_list) {
3068c66ac9dbSNicholas Bellinger 			/*
3069c66ac9dbSNicholas Bellinger 			 * Removing of registrations in non all registrants
3070c66ac9dbSNicholas Bellinger 			 * type reservations without a matching SA reservation
3071c66ac9dbSNicholas Bellinger 			 * key.
3072c66ac9dbSNicholas Bellinger 			 *
3073c66ac9dbSNicholas Bellinger 			 * a) Remove the registrations for all I_T nexuses
3074c66ac9dbSNicholas Bellinger 			 *    specified by the SERVICE ACTION RESERVATION KEY
3075c66ac9dbSNicholas Bellinger 			 *    field;
3076c66ac9dbSNicholas Bellinger 			 * b) Ignore the contents of the SCOPE and TYPE fields;
3077c66ac9dbSNicholas Bellinger 			 * c) Process tasks as defined in 5.7.1; and
3078c66ac9dbSNicholas Bellinger 			 * d) Establish a unit attention condition for the
3079c66ac9dbSNicholas Bellinger 			 *    initiator port associated with every I_T nexus
3080c66ac9dbSNicholas Bellinger 			 *    that lost its registration other than the I_T
3081c66ac9dbSNicholas Bellinger 			 *    nexus on which the PERSISTENT RESERVE OUT command
3082c66ac9dbSNicholas Bellinger 			 *    was received, with the additional sense code set
3083c66ac9dbSNicholas Bellinger 			 *    to REGISTRATIONS PREEMPTED.
3084c66ac9dbSNicholas Bellinger 			 */
30856708bb27SAndy Grover 			if (!all_reg) {
3086c66ac9dbSNicholas Bellinger 				if (pr_reg->pr_res_key != sa_res_key)
3087c66ac9dbSNicholas Bellinger 					continue;
3088c66ac9dbSNicholas Bellinger 
3089c66ac9dbSNicholas Bellinger 				calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3090c66ac9dbSNicholas Bellinger 				pr_reg_nacl = pr_reg->pr_reg_nacl;
3091c66ac9dbSNicholas Bellinger 				pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3092c66ac9dbSNicholas Bellinger 				__core_scsi3_free_registration(dev, pr_reg,
3093c66ac9dbSNicholas Bellinger 					(abort) ? &preempt_and_abort_list :
3094c66ac9dbSNicholas Bellinger 						NULL, calling_it_nexus);
3095c66ac9dbSNicholas Bellinger 				released_regs++;
3096c66ac9dbSNicholas Bellinger 			} else {
3097c66ac9dbSNicholas Bellinger 				/*
3098c66ac9dbSNicholas Bellinger 				 * Case for any existing all registrants type
3099c66ac9dbSNicholas Bellinger 				 * reservation, follow logic in spc4r17 section
3100c66ac9dbSNicholas Bellinger 				 * 5.7.11.4 Preempting, Table 52 and Figure 7.
3101c66ac9dbSNicholas Bellinger 				 *
3102c66ac9dbSNicholas Bellinger 				 * For a ZERO SA Reservation key, release
3103c66ac9dbSNicholas Bellinger 				 * all other registrations and do an implict
3104c66ac9dbSNicholas Bellinger 				 * release of active persistent reservation.
3105c66ac9dbSNicholas Bellinger 				 *
3106c66ac9dbSNicholas Bellinger 				 * For a non-ZERO SA Reservation key, only
3107c66ac9dbSNicholas Bellinger 				 * release the matching reservation key from
3108c66ac9dbSNicholas Bellinger 				 * registrations.
3109c66ac9dbSNicholas Bellinger 				 */
3110c66ac9dbSNicholas Bellinger 				if ((sa_res_key) &&
3111c66ac9dbSNicholas Bellinger 				     (pr_reg->pr_res_key != sa_res_key))
3112c66ac9dbSNicholas Bellinger 					continue;
3113c66ac9dbSNicholas Bellinger 
3114c66ac9dbSNicholas Bellinger 				calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3115c66ac9dbSNicholas Bellinger 				if (calling_it_nexus)
3116c66ac9dbSNicholas Bellinger 					continue;
3117c66ac9dbSNicholas Bellinger 
3118c66ac9dbSNicholas Bellinger 				pr_reg_nacl = pr_reg->pr_reg_nacl;
3119c66ac9dbSNicholas Bellinger 				pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3120c66ac9dbSNicholas Bellinger 				__core_scsi3_free_registration(dev, pr_reg,
3121c66ac9dbSNicholas Bellinger 					(abort) ? &preempt_and_abort_list :
3122c66ac9dbSNicholas Bellinger 						NULL, 0);
3123c66ac9dbSNicholas Bellinger 				released_regs++;
3124c66ac9dbSNicholas Bellinger 			}
31256708bb27SAndy Grover 			if (!calling_it_nexus)
3126c66ac9dbSNicholas Bellinger 				core_scsi3_ua_allocate(pr_reg_nacl,
3127c66ac9dbSNicholas Bellinger 					pr_res_mapped_lun, 0x2A,
31289e08e34eSMarco Sanvido 					ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3129c66ac9dbSNicholas Bellinger 		}
3130c66ac9dbSNicholas Bellinger 		spin_unlock(&pr_tmpl->registration_lock);
3131c66ac9dbSNicholas Bellinger 		/*
3132c66ac9dbSNicholas Bellinger 		 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
3133c66ac9dbSNicholas Bellinger 		 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
3134c66ac9dbSNicholas Bellinger 		 * RESERVATION KEY field to a value that does not match any
3135c66ac9dbSNicholas Bellinger 		 * registered reservation key, then the device server shall
3136c66ac9dbSNicholas Bellinger 		 * complete the command with RESERVATION CONFLICT status.
3137c66ac9dbSNicholas Bellinger 		 */
31386708bb27SAndy Grover 		if (!released_regs) {
3139c66ac9dbSNicholas Bellinger 			spin_unlock(&dev->dev_reservation_lock);
3140c66ac9dbSNicholas Bellinger 			core_scsi3_put_pr_reg(pr_reg_n);
314103e98c9eSNicholas Bellinger 			cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
314203e98c9eSNicholas Bellinger 			return -EINVAL;
3143c66ac9dbSNicholas Bellinger 		}
3144c66ac9dbSNicholas Bellinger 		/*
3145c66ac9dbSNicholas Bellinger 		 * For an existing all registrants type reservation
3146c66ac9dbSNicholas Bellinger 		 * with a zero SA rservation key, preempt the existing
3147c66ac9dbSNicholas Bellinger 		 * reservation with the new PR type and scope.
3148c66ac9dbSNicholas Bellinger 		 */
3149c66ac9dbSNicholas Bellinger 		if (pr_res_holder && all_reg && !(sa_res_key)) {
3150c66ac9dbSNicholas Bellinger 			__core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3151c66ac9dbSNicholas Bellinger 				(abort) ? &preempt_and_abort_list : NULL,
3152c66ac9dbSNicholas Bellinger 				type, scope, abort);
3153c66ac9dbSNicholas Bellinger 
3154c66ac9dbSNicholas Bellinger 			if (abort)
3155c66ac9dbSNicholas Bellinger 				core_scsi3_release_preempt_and_abort(
3156c66ac9dbSNicholas Bellinger 					&preempt_and_abort_list, pr_reg_n);
3157c66ac9dbSNicholas Bellinger 		}
3158c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
3159c66ac9dbSNicholas Bellinger 
3160c66ac9dbSNicholas Bellinger 		if (pr_tmpl->pr_aptpl_active) {
31615951146dSAndy Grover 			ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3162c66ac9dbSNicholas Bellinger 					&pr_reg_n->pr_aptpl_buf[0],
3163c66ac9dbSNicholas Bellinger 					pr_tmpl->pr_aptpl_buf_len);
31646708bb27SAndy Grover 			if (!ret)
31656708bb27SAndy Grover 				pr_debug("SPC-3 PR: Updated APTPL"
3166c66ac9dbSNicholas Bellinger 					" metadata for  PREEMPT%s\n", (abort) ?
3167c66ac9dbSNicholas Bellinger 					"_AND_ABORT" : "");
3168c66ac9dbSNicholas Bellinger 		}
3169c66ac9dbSNicholas Bellinger 
3170c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg_n);
31715951146dSAndy Grover 		core_scsi3_pr_generation(cmd->se_dev);
3172c66ac9dbSNicholas Bellinger 		return 0;
3173c66ac9dbSNicholas Bellinger 	}
3174c66ac9dbSNicholas Bellinger 	/*
3175c66ac9dbSNicholas Bellinger 	 * The PREEMPTing SA reservation key matches that of the
3176c66ac9dbSNicholas Bellinger 	 * existing persistent reservation, first, we check if
3177c66ac9dbSNicholas Bellinger 	 * we are preempting our own reservation.
3178c66ac9dbSNicholas Bellinger 	 * From spc4r17, section 5.7.11.4.3 Preempting
3179c66ac9dbSNicholas Bellinger 	 * persistent reservations and registration handling
3180c66ac9dbSNicholas Bellinger 	 *
3181c66ac9dbSNicholas Bellinger 	 * If an all registrants persistent reservation is not
3182c66ac9dbSNicholas Bellinger 	 * present, it is not an error for the persistent
3183c66ac9dbSNicholas Bellinger 	 * reservation holder to preempt itself (i.e., a
3184c66ac9dbSNicholas Bellinger 	 * PERSISTENT RESERVE OUT with a PREEMPT service action
3185c66ac9dbSNicholas Bellinger 	 * or a PREEMPT AND ABORT service action with the
3186c66ac9dbSNicholas Bellinger 	 * SERVICE ACTION RESERVATION KEY value equal to the
3187c66ac9dbSNicholas Bellinger 	 * persistent reservation holder's reservation key that
3188c66ac9dbSNicholas Bellinger 	 * is received from the persistent reservation holder).
3189c66ac9dbSNicholas Bellinger 	 * In that case, the device server shall establish the
3190c66ac9dbSNicholas Bellinger 	 * new persistent reservation and maintain the
3191c66ac9dbSNicholas Bellinger 	 * registration.
3192c66ac9dbSNicholas Bellinger 	 */
3193c66ac9dbSNicholas Bellinger 	prh_type = pr_res_holder->pr_res_type;
3194c66ac9dbSNicholas Bellinger 	prh_scope = pr_res_holder->pr_res_scope;
3195c66ac9dbSNicholas Bellinger 	/*
3196c66ac9dbSNicholas Bellinger 	 * If the SERVICE ACTION RESERVATION KEY field identifies a
3197c66ac9dbSNicholas Bellinger 	 * persistent reservation holder (see 5.7.10), the device
3198c66ac9dbSNicholas Bellinger 	 * server shall perform a preempt by doing the following as
3199c66ac9dbSNicholas Bellinger 	 * an uninterrupted series of actions:
3200c66ac9dbSNicholas Bellinger 	 *
3201c66ac9dbSNicholas Bellinger 	 * a) Release the persistent reservation for the holder
3202c66ac9dbSNicholas Bellinger 	 *    identified by the SERVICE ACTION RESERVATION KEY field;
3203c66ac9dbSNicholas Bellinger 	 */
3204c66ac9dbSNicholas Bellinger 	if (pr_reg_n != pr_res_holder)
3205c66ac9dbSNicholas Bellinger 		__core_scsi3_complete_pro_release(dev,
3206c66ac9dbSNicholas Bellinger 				pr_res_holder->pr_reg_nacl,
3207c66ac9dbSNicholas Bellinger 				dev->dev_pr_res_holder, 0);
3208c66ac9dbSNicholas Bellinger 	/*
3209c66ac9dbSNicholas Bellinger 	 * b) Remove the registrations for all I_T nexuses identified
3210c66ac9dbSNicholas Bellinger 	 *    by the SERVICE ACTION RESERVATION KEY field, except the
3211c66ac9dbSNicholas Bellinger 	 *    I_T nexus that is being used for the PERSISTENT RESERVE
3212c66ac9dbSNicholas Bellinger 	 *    OUT command. If an all registrants persistent reservation
3213c66ac9dbSNicholas Bellinger 	 *    is present and the SERVICE ACTION RESERVATION KEY field
3214c66ac9dbSNicholas Bellinger 	 *    is set to zero, then all registrations shall be removed
3215c66ac9dbSNicholas Bellinger 	 *    except for that of the I_T nexus that is being used for
3216c66ac9dbSNicholas Bellinger 	 *    the PERSISTENT RESERVE OUT command;
3217c66ac9dbSNicholas Bellinger 	 */
3218c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
3219c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3220c66ac9dbSNicholas Bellinger 			&pr_tmpl->registration_list, pr_reg_list) {
3221c66ac9dbSNicholas Bellinger 
3222c66ac9dbSNicholas Bellinger 		calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3223c66ac9dbSNicholas Bellinger 		if (calling_it_nexus)
3224c66ac9dbSNicholas Bellinger 			continue;
3225c66ac9dbSNicholas Bellinger 
3226c66ac9dbSNicholas Bellinger 		if (pr_reg->pr_res_key != sa_res_key)
3227c66ac9dbSNicholas Bellinger 			continue;
3228c66ac9dbSNicholas Bellinger 
3229c66ac9dbSNicholas Bellinger 		pr_reg_nacl = pr_reg->pr_reg_nacl;
3230c66ac9dbSNicholas Bellinger 		pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3231c66ac9dbSNicholas Bellinger 		__core_scsi3_free_registration(dev, pr_reg,
3232c66ac9dbSNicholas Bellinger 				(abort) ? &preempt_and_abort_list : NULL,
3233c66ac9dbSNicholas Bellinger 				calling_it_nexus);
3234c66ac9dbSNicholas Bellinger 		/*
3235c66ac9dbSNicholas Bellinger 		 * e) Establish a unit attention condition for the initiator
3236c66ac9dbSNicholas Bellinger 		 *    port associated with every I_T nexus that lost its
3237c66ac9dbSNicholas Bellinger 		 *    persistent reservation and/or registration, with the
3238c66ac9dbSNicholas Bellinger 		 *    additional sense code set to REGISTRATIONS PREEMPTED;
3239c66ac9dbSNicholas Bellinger 		 */
3240c66ac9dbSNicholas Bellinger 		core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
32419e08e34eSMarco Sanvido 				ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3242c66ac9dbSNicholas Bellinger 	}
3243c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
3244c66ac9dbSNicholas Bellinger 	/*
3245c66ac9dbSNicholas Bellinger 	 * c) Establish a persistent reservation for the preempting
3246c66ac9dbSNicholas Bellinger 	 *    I_T nexus using the contents of the SCOPE and TYPE fields;
3247c66ac9dbSNicholas Bellinger 	 */
3248c66ac9dbSNicholas Bellinger 	__core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3249c66ac9dbSNicholas Bellinger 			(abort) ? &preempt_and_abort_list : NULL,
3250c66ac9dbSNicholas Bellinger 			type, scope, abort);
3251c66ac9dbSNicholas Bellinger 	/*
3252c66ac9dbSNicholas Bellinger 	 * d) Process tasks as defined in 5.7.1;
3253c66ac9dbSNicholas Bellinger 	 * e) See above..
3254c66ac9dbSNicholas Bellinger 	 * f) If the type or scope has changed, then for every I_T nexus
3255c66ac9dbSNicholas Bellinger 	 *    whose reservation key was not removed, except for the I_T
3256c66ac9dbSNicholas Bellinger 	 *    nexus on which the PERSISTENT RESERVE OUT command was
3257c66ac9dbSNicholas Bellinger 	 *    received, the device server shall establish a unit
3258c66ac9dbSNicholas Bellinger 	 *    attention condition for the initiator port associated with
3259c66ac9dbSNicholas Bellinger 	 *    that I_T nexus, with the additional sense code set to
3260c66ac9dbSNicholas Bellinger 	 *    RESERVATIONS RELEASED. If the type or scope have not
3261c66ac9dbSNicholas Bellinger 	 *    changed, then no unit attention condition(s) shall be
3262c66ac9dbSNicholas Bellinger 	 *    established for this reason.
3263c66ac9dbSNicholas Bellinger 	 */
3264c66ac9dbSNicholas Bellinger 	if ((prh_type != type) || (prh_scope != scope)) {
3265c66ac9dbSNicholas Bellinger 		spin_lock(&pr_tmpl->registration_lock);
3266c66ac9dbSNicholas Bellinger 		list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3267c66ac9dbSNicholas Bellinger 				&pr_tmpl->registration_list, pr_reg_list) {
3268c66ac9dbSNicholas Bellinger 
3269c66ac9dbSNicholas Bellinger 			calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3270c66ac9dbSNicholas Bellinger 			if (calling_it_nexus)
3271c66ac9dbSNicholas Bellinger 				continue;
3272c66ac9dbSNicholas Bellinger 
3273c66ac9dbSNicholas Bellinger 			core_scsi3_ua_allocate(pr_reg->pr_reg_nacl,
3274c66ac9dbSNicholas Bellinger 					pr_reg->pr_res_mapped_lun, 0x2A,
3275c66ac9dbSNicholas Bellinger 					ASCQ_2AH_RESERVATIONS_RELEASED);
3276c66ac9dbSNicholas Bellinger 		}
3277c66ac9dbSNicholas Bellinger 		spin_unlock(&pr_tmpl->registration_lock);
3278c66ac9dbSNicholas Bellinger 	}
3279c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
3280c66ac9dbSNicholas Bellinger 	/*
3281c66ac9dbSNicholas Bellinger 	 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3282c66ac9dbSNicholas Bellinger 	 * All received CDBs for the matching existing reservation and
3283c66ac9dbSNicholas Bellinger 	 * registrations undergo ABORT_TASK logic.
3284c66ac9dbSNicholas Bellinger 	 *
3285c66ac9dbSNicholas Bellinger 	 * From there, core_scsi3_release_preempt_and_abort() will
3286c66ac9dbSNicholas Bellinger 	 * release every registration in the list (which have already
3287c66ac9dbSNicholas Bellinger 	 * been removed from the primary pr_reg list), except the
3288c66ac9dbSNicholas Bellinger 	 * new persistent reservation holder, the calling Initiator Port.
3289c66ac9dbSNicholas Bellinger 	 */
3290c66ac9dbSNicholas Bellinger 	if (abort) {
3291c66ac9dbSNicholas Bellinger 		core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3292c66ac9dbSNicholas Bellinger 		core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3293c66ac9dbSNicholas Bellinger 						pr_reg_n);
3294c66ac9dbSNicholas Bellinger 	}
3295c66ac9dbSNicholas Bellinger 
3296c66ac9dbSNicholas Bellinger 	if (pr_tmpl->pr_aptpl_active) {
32975951146dSAndy Grover 		ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3298c66ac9dbSNicholas Bellinger 				&pr_reg_n->pr_aptpl_buf[0],
3299c66ac9dbSNicholas Bellinger 				pr_tmpl->pr_aptpl_buf_len);
33006708bb27SAndy Grover 		if (!ret)
33016708bb27SAndy Grover 			pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
3302c66ac9dbSNicholas Bellinger 				"%s\n", (abort) ? "_AND_ABORT" : "");
3303c66ac9dbSNicholas Bellinger 	}
3304c66ac9dbSNicholas Bellinger 
3305c66ac9dbSNicholas Bellinger 	core_scsi3_put_pr_reg(pr_reg_n);
33065951146dSAndy Grover 	core_scsi3_pr_generation(cmd->se_dev);
3307c66ac9dbSNicholas Bellinger 	return 0;
3308c66ac9dbSNicholas Bellinger }
3309c66ac9dbSNicholas Bellinger 
3310c66ac9dbSNicholas Bellinger static int core_scsi3_emulate_pro_preempt(
3311c66ac9dbSNicholas Bellinger 	struct se_cmd *cmd,
3312c66ac9dbSNicholas Bellinger 	int type,
3313c66ac9dbSNicholas Bellinger 	int scope,
3314c66ac9dbSNicholas Bellinger 	u64 res_key,
3315c66ac9dbSNicholas Bellinger 	u64 sa_res_key,
3316c66ac9dbSNicholas Bellinger 	int abort)
3317c66ac9dbSNicholas Bellinger {
3318c66ac9dbSNicholas Bellinger 	int ret = 0;
3319c66ac9dbSNicholas Bellinger 
3320c66ac9dbSNicholas Bellinger 	switch (type) {
3321c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE:
3322c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS:
3323c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3324c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3325c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3326c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
3327c66ac9dbSNicholas Bellinger 		ret = core_scsi3_pro_preempt(cmd, type, scope,
3328c66ac9dbSNicholas Bellinger 				res_key, sa_res_key, abort);
3329c66ac9dbSNicholas Bellinger 		break;
3330c66ac9dbSNicholas Bellinger 	default:
33316708bb27SAndy Grover 		pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3332c66ac9dbSNicholas Bellinger 			" Type: 0x%02x\n", (abort) ? "_AND_ABORT" : "", type);
333303e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
333403e98c9eSNicholas Bellinger 		return -EINVAL;
3335c66ac9dbSNicholas Bellinger 	}
3336c66ac9dbSNicholas Bellinger 
3337c66ac9dbSNicholas Bellinger 	return ret;
3338c66ac9dbSNicholas Bellinger }
3339c66ac9dbSNicholas Bellinger 
3340c66ac9dbSNicholas Bellinger 
3341c66ac9dbSNicholas Bellinger static int core_scsi3_emulate_pro_register_and_move(
3342c66ac9dbSNicholas Bellinger 	struct se_cmd *cmd,
3343c66ac9dbSNicholas Bellinger 	u64 res_key,
3344c66ac9dbSNicholas Bellinger 	u64 sa_res_key,
3345c66ac9dbSNicholas Bellinger 	int aptpl,
3346c66ac9dbSNicholas Bellinger 	int unreg)
3347c66ac9dbSNicholas Bellinger {
3348e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
33495951146dSAndy Grover 	struct se_device *dev = cmd->se_dev;
335028168905SJörn Engel 	struct se_dev_entry *dest_se_deve = NULL;
3351e3d6f909SAndy Grover 	struct se_lun *se_lun = cmd->se_lun;
3352c66ac9dbSNicholas Bellinger 	struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3353c66ac9dbSNicholas Bellinger 	struct se_port *se_port;
3354c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3355c66ac9dbSNicholas Bellinger 	struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3356c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
33570fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
335805d1c7c0SAndy Grover 	unsigned char *buf;
3359c66ac9dbSNicholas Bellinger 	unsigned char *initiator_str;
3360c66ac9dbSNicholas Bellinger 	char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
3361c66ac9dbSNicholas Bellinger 	u32 tid_len, tmp_tid_len;
3362c66ac9dbSNicholas Bellinger 	int new_reg = 0, type, scope, ret, matching_iname, prf_isid;
3363c66ac9dbSNicholas Bellinger 	unsigned short rtpi;
3364c66ac9dbSNicholas Bellinger 	unsigned char proto_ident;
3365c66ac9dbSNicholas Bellinger 
33666708bb27SAndy Grover 	if (!se_sess || !se_lun) {
33676708bb27SAndy Grover 		pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
336803e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
336903e98c9eSNicholas Bellinger 		return -EINVAL;
3370c66ac9dbSNicholas Bellinger 	}
3371c66ac9dbSNicholas Bellinger 	memset(dest_iport, 0, 64);
3372c66ac9dbSNicholas Bellinger 	memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3373c66ac9dbSNicholas Bellinger 	se_tpg = se_sess->se_tpg;
3374e3d6f909SAndy Grover 	tf_ops = se_tpg->se_tpg_tfo;
3375c66ac9dbSNicholas Bellinger 	/*
3376c66ac9dbSNicholas Bellinger 	 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3377c66ac9dbSNicholas Bellinger 	 *	Register behaviors for a REGISTER AND MOVE service action
3378c66ac9dbSNicholas Bellinger 	 *
3379c66ac9dbSNicholas Bellinger 	 * Locate the existing *pr_reg via struct se_node_acl pointers
3380c66ac9dbSNicholas Bellinger 	 */
33815951146dSAndy Grover 	pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3382c66ac9dbSNicholas Bellinger 				se_sess);
33836708bb27SAndy Grover 	if (!pr_reg) {
33846708bb27SAndy Grover 		pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3385c66ac9dbSNicholas Bellinger 			" *pr_reg for REGISTER_AND_MOVE\n");
338603e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
338703e98c9eSNicholas Bellinger 		return -EINVAL;
3388c66ac9dbSNicholas Bellinger 	}
3389c66ac9dbSNicholas Bellinger 	/*
3390c66ac9dbSNicholas Bellinger 	 * The provided reservation key much match the existing reservation key
3391c66ac9dbSNicholas Bellinger 	 * provided during this initiator's I_T nexus registration.
3392c66ac9dbSNicholas Bellinger 	 */
3393c66ac9dbSNicholas Bellinger 	if (res_key != pr_reg->pr_res_key) {
33946708bb27SAndy Grover 		pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3395c66ac9dbSNicholas Bellinger 			" res_key: 0x%016Lx does not match existing SA REGISTER"
3396c66ac9dbSNicholas Bellinger 			" res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
3397c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg);
339803e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
339903e98c9eSNicholas Bellinger 		return -EINVAL;
3400c66ac9dbSNicholas Bellinger 	}
3401c66ac9dbSNicholas Bellinger 	/*
3402c66ac9dbSNicholas Bellinger 	 * The service active reservation key needs to be non zero
3403c66ac9dbSNicholas Bellinger 	 */
34046708bb27SAndy Grover 	if (!sa_res_key) {
34056708bb27SAndy Grover 		pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3406c66ac9dbSNicholas Bellinger 			" sa_res_key\n");
3407c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg);
340803e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
340903e98c9eSNicholas Bellinger 		return -EINVAL;
3410c66ac9dbSNicholas Bellinger 	}
341105d1c7c0SAndy Grover 
3412c66ac9dbSNicholas Bellinger 	/*
3413c66ac9dbSNicholas Bellinger 	 * Determine the Relative Target Port Identifier where the reservation
3414c66ac9dbSNicholas Bellinger 	 * will be moved to for the TransportID containing SCSI initiator WWN
3415c66ac9dbSNicholas Bellinger 	 * information.
3416c66ac9dbSNicholas Bellinger 	 */
34174949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
3418c66ac9dbSNicholas Bellinger 	rtpi = (buf[18] & 0xff) << 8;
3419c66ac9dbSNicholas Bellinger 	rtpi |= buf[19] & 0xff;
3420c66ac9dbSNicholas Bellinger 	tid_len = (buf[20] & 0xff) << 24;
3421c66ac9dbSNicholas Bellinger 	tid_len |= (buf[21] & 0xff) << 16;
3422c66ac9dbSNicholas Bellinger 	tid_len |= (buf[22] & 0xff) << 8;
3423c66ac9dbSNicholas Bellinger 	tid_len |= buf[23] & 0xff;
34244949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
342505d1c7c0SAndy Grover 	buf = NULL;
3426c66ac9dbSNicholas Bellinger 
3427c66ac9dbSNicholas Bellinger 	if ((tid_len + 24) != cmd->data_length) {
34286708bb27SAndy Grover 		pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3429c66ac9dbSNicholas Bellinger 			" does not equal CDB data_length: %u\n", tid_len,
3430c66ac9dbSNicholas Bellinger 			cmd->data_length);
3431c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg);
343203e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
343303e98c9eSNicholas Bellinger 		return -EINVAL;
3434c66ac9dbSNicholas Bellinger 	}
3435c66ac9dbSNicholas Bellinger 
3436c66ac9dbSNicholas Bellinger 	spin_lock(&dev->se_port_lock);
3437c66ac9dbSNicholas Bellinger 	list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) {
3438c66ac9dbSNicholas Bellinger 		if (se_port->sep_rtpi != rtpi)
3439c66ac9dbSNicholas Bellinger 			continue;
3440c66ac9dbSNicholas Bellinger 		dest_se_tpg = se_port->sep_tpg;
34416708bb27SAndy Grover 		if (!dest_se_tpg)
3442c66ac9dbSNicholas Bellinger 			continue;
3443e3d6f909SAndy Grover 		dest_tf_ops = dest_se_tpg->se_tpg_tfo;
34446708bb27SAndy Grover 		if (!dest_tf_ops)
3445c66ac9dbSNicholas Bellinger 			continue;
3446c66ac9dbSNicholas Bellinger 
3447c66ac9dbSNicholas Bellinger 		atomic_inc(&dest_se_tpg->tpg_pr_ref_count);
3448c66ac9dbSNicholas Bellinger 		smp_mb__after_atomic_inc();
3449c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->se_port_lock);
3450c66ac9dbSNicholas Bellinger 
3451c66ac9dbSNicholas Bellinger 		ret = core_scsi3_tpg_depend_item(dest_se_tpg);
3452c66ac9dbSNicholas Bellinger 		if (ret != 0) {
34536708bb27SAndy Grover 			pr_err("core_scsi3_tpg_depend_item() failed"
3454c66ac9dbSNicholas Bellinger 				" for dest_se_tpg\n");
3455c66ac9dbSNicholas Bellinger 			atomic_dec(&dest_se_tpg->tpg_pr_ref_count);
3456c66ac9dbSNicholas Bellinger 			smp_mb__after_atomic_dec();
3457c66ac9dbSNicholas Bellinger 			core_scsi3_put_pr_reg(pr_reg);
345803e98c9eSNicholas Bellinger 			cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
345903e98c9eSNicholas Bellinger 			return -EINVAL;
3460c66ac9dbSNicholas Bellinger 		}
3461c66ac9dbSNicholas Bellinger 
3462c66ac9dbSNicholas Bellinger 		spin_lock(&dev->se_port_lock);
3463c66ac9dbSNicholas Bellinger 		break;
3464c66ac9dbSNicholas Bellinger 	}
3465c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->se_port_lock);
3466c66ac9dbSNicholas Bellinger 
34676708bb27SAndy Grover 	if (!dest_se_tpg || !dest_tf_ops) {
34686708bb27SAndy Grover 		pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3469c66ac9dbSNicholas Bellinger 			" fabric ops from Relative Target Port Identifier:"
3470c66ac9dbSNicholas Bellinger 			" %hu\n", rtpi);
3471c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg);
347203e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
347303e98c9eSNicholas Bellinger 		return -EINVAL;
3474c66ac9dbSNicholas Bellinger 	}
347505d1c7c0SAndy Grover 
34764949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
3477c66ac9dbSNicholas Bellinger 	proto_ident = (buf[24] & 0x0f);
34788b1e1244SAndy Grover 
34796708bb27SAndy Grover 	pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3480c66ac9dbSNicholas Bellinger 			" 0x%02x\n", proto_ident);
34818b1e1244SAndy Grover 
3482c66ac9dbSNicholas Bellinger 	if (proto_ident != dest_tf_ops->get_fabric_proto_ident(dest_se_tpg)) {
34836708bb27SAndy Grover 		pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3484c66ac9dbSNicholas Bellinger 			" proto_ident: 0x%02x does not match ident: 0x%02x"
3485c66ac9dbSNicholas Bellinger 			" from fabric: %s\n", proto_ident,
3486c66ac9dbSNicholas Bellinger 			dest_tf_ops->get_fabric_proto_ident(dest_se_tpg),
3487c66ac9dbSNicholas Bellinger 			dest_tf_ops->get_fabric_name());
348803e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
348903e98c9eSNicholas Bellinger 		ret = -EINVAL;
3490c66ac9dbSNicholas Bellinger 		goto out;
3491c66ac9dbSNicholas Bellinger 	}
3492c66ac9dbSNicholas Bellinger 	if (dest_tf_ops->tpg_parse_pr_out_transport_id == NULL) {
34936708bb27SAndy Grover 		pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
3494c66ac9dbSNicholas Bellinger 			" containg a valid tpg_parse_pr_out_transport_id"
3495c66ac9dbSNicholas Bellinger 			" function pointer\n");
349603e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
349703e98c9eSNicholas Bellinger 		ret = -EINVAL;
3498c66ac9dbSNicholas Bellinger 		goto out;
3499c66ac9dbSNicholas Bellinger 	}
3500c66ac9dbSNicholas Bellinger 	initiator_str = dest_tf_ops->tpg_parse_pr_out_transport_id(dest_se_tpg,
3501c66ac9dbSNicholas Bellinger 			(const char *)&buf[24], &tmp_tid_len, &iport_ptr);
35026708bb27SAndy Grover 	if (!initiator_str) {
35036708bb27SAndy Grover 		pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3504c66ac9dbSNicholas Bellinger 			" initiator_str from Transport ID\n");
350503e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
350603e98c9eSNicholas Bellinger 		ret = -EINVAL;
3507c66ac9dbSNicholas Bellinger 		goto out;
3508c66ac9dbSNicholas Bellinger 	}
3509c66ac9dbSNicholas Bellinger 
35104949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
351105d1c7c0SAndy Grover 	buf = NULL;
351205d1c7c0SAndy Grover 
35136708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3514c66ac9dbSNicholas Bellinger 		" %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
3515c66ac9dbSNicholas Bellinger 		"port" : "device", initiator_str, (iport_ptr != NULL) ?
3516c66ac9dbSNicholas Bellinger 		iport_ptr : "");
3517c66ac9dbSNicholas Bellinger 	/*
3518c66ac9dbSNicholas Bellinger 	 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3519c66ac9dbSNicholas Bellinger 	 * action specifies a TransportID that is the same as the initiator port
3520c66ac9dbSNicholas Bellinger 	 * of the I_T nexus for the command received, then the command shall
3521c66ac9dbSNicholas Bellinger 	 * be terminated with CHECK CONDITION status, with the sense key set to
3522c66ac9dbSNicholas Bellinger 	 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3523c66ac9dbSNicholas Bellinger 	 * IN PARAMETER LIST.
3524c66ac9dbSNicholas Bellinger 	 */
3525c66ac9dbSNicholas Bellinger 	pr_reg_nacl = pr_reg->pr_reg_nacl;
3526c66ac9dbSNicholas Bellinger 	matching_iname = (!strcmp(initiator_str,
3527c66ac9dbSNicholas Bellinger 				  pr_reg_nacl->initiatorname)) ? 1 : 0;
35286708bb27SAndy Grover 	if (!matching_iname)
3529c66ac9dbSNicholas Bellinger 		goto after_iport_check;
3530c66ac9dbSNicholas Bellinger 
35316708bb27SAndy Grover 	if (!iport_ptr || !pr_reg->isid_present_at_reg) {
35326708bb27SAndy Grover 		pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3533c66ac9dbSNicholas Bellinger 			" matches: %s on received I_T Nexus\n", initiator_str,
3534c66ac9dbSNicholas Bellinger 			pr_reg_nacl->initiatorname);
353503e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
353603e98c9eSNicholas Bellinger 		ret = -EINVAL;
3537c66ac9dbSNicholas Bellinger 		goto out;
3538c66ac9dbSNicholas Bellinger 	}
35396708bb27SAndy Grover 	if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
35406708bb27SAndy Grover 		pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3541c66ac9dbSNicholas Bellinger 			" matches: %s %s on received I_T Nexus\n",
3542c66ac9dbSNicholas Bellinger 			initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3543c66ac9dbSNicholas Bellinger 			pr_reg->pr_reg_isid);
354403e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
354503e98c9eSNicholas Bellinger 		ret = -EINVAL;
3546c66ac9dbSNicholas Bellinger 		goto out;
3547c66ac9dbSNicholas Bellinger 	}
3548c66ac9dbSNicholas Bellinger after_iport_check:
3549c66ac9dbSNicholas Bellinger 	/*
3550c66ac9dbSNicholas Bellinger 	 * Locate the destination struct se_node_acl from the received Transport ID
3551c66ac9dbSNicholas Bellinger 	 */
355228638887SRoland Dreier 	spin_lock_irq(&dest_se_tpg->acl_node_lock);
3553c66ac9dbSNicholas Bellinger 	dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3554c66ac9dbSNicholas Bellinger 				initiator_str);
3555c66ac9dbSNicholas Bellinger 	if (dest_node_acl) {
3556c66ac9dbSNicholas Bellinger 		atomic_inc(&dest_node_acl->acl_pr_ref_count);
3557c66ac9dbSNicholas Bellinger 		smp_mb__after_atomic_inc();
3558c66ac9dbSNicholas Bellinger 	}
355928638887SRoland Dreier 	spin_unlock_irq(&dest_se_tpg->acl_node_lock);
3560c66ac9dbSNicholas Bellinger 
35616708bb27SAndy Grover 	if (!dest_node_acl) {
35626708bb27SAndy Grover 		pr_err("Unable to locate %s dest_node_acl for"
3563c66ac9dbSNicholas Bellinger 			" TransportID%s\n", dest_tf_ops->get_fabric_name(),
3564c66ac9dbSNicholas Bellinger 			initiator_str);
356503e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
356603e98c9eSNicholas Bellinger 		ret = -EINVAL;
3567c66ac9dbSNicholas Bellinger 		goto out;
3568c66ac9dbSNicholas Bellinger 	}
3569c66ac9dbSNicholas Bellinger 	ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
3570c66ac9dbSNicholas Bellinger 	if (ret != 0) {
35716708bb27SAndy Grover 		pr_err("core_scsi3_nodeacl_depend_item() for"
3572c66ac9dbSNicholas Bellinger 			" dest_node_acl\n");
3573c66ac9dbSNicholas Bellinger 		atomic_dec(&dest_node_acl->acl_pr_ref_count);
3574c66ac9dbSNicholas Bellinger 		smp_mb__after_atomic_dec();
3575c66ac9dbSNicholas Bellinger 		dest_node_acl = NULL;
357603e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
357703e98c9eSNicholas Bellinger 		ret = -EINVAL;
3578c66ac9dbSNicholas Bellinger 		goto out;
3579c66ac9dbSNicholas Bellinger 	}
35808b1e1244SAndy Grover 
35816708bb27SAndy Grover 	pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3582c66ac9dbSNicholas Bellinger 		" %s from TransportID\n", dest_tf_ops->get_fabric_name(),
3583c66ac9dbSNicholas Bellinger 		dest_node_acl->initiatorname);
35848b1e1244SAndy Grover 
3585c66ac9dbSNicholas Bellinger 	/*
3586c66ac9dbSNicholas Bellinger 	 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3587c66ac9dbSNicholas Bellinger 	 * PORT IDENTIFIER.
3588c66ac9dbSNicholas Bellinger 	 */
3589c66ac9dbSNicholas Bellinger 	dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
35906708bb27SAndy Grover 	if (!dest_se_deve) {
35916708bb27SAndy Grover 		pr_err("Unable to locate %s dest_se_deve from RTPI:"
3592c66ac9dbSNicholas Bellinger 			" %hu\n",  dest_tf_ops->get_fabric_name(), rtpi);
359303e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
359403e98c9eSNicholas Bellinger 		ret = -EINVAL;
3595c66ac9dbSNicholas Bellinger 		goto out;
3596c66ac9dbSNicholas Bellinger 	}
3597c66ac9dbSNicholas Bellinger 
3598c66ac9dbSNicholas Bellinger 	ret = core_scsi3_lunacl_depend_item(dest_se_deve);
3599c66ac9dbSNicholas Bellinger 	if (ret < 0) {
36006708bb27SAndy Grover 		pr_err("core_scsi3_lunacl_depend_item() failed\n");
3601c66ac9dbSNicholas Bellinger 		atomic_dec(&dest_se_deve->pr_ref_count);
3602c66ac9dbSNicholas Bellinger 		smp_mb__after_atomic_dec();
3603c66ac9dbSNicholas Bellinger 		dest_se_deve = NULL;
360403e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
360503e98c9eSNicholas Bellinger 		ret = -EINVAL;
3606c66ac9dbSNicholas Bellinger 		goto out;
3607c66ac9dbSNicholas Bellinger 	}
36088b1e1244SAndy Grover 
36096708bb27SAndy Grover 	pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3610c66ac9dbSNicholas Bellinger 		" ACL for dest_se_deve->mapped_lun: %u\n",
3611c66ac9dbSNicholas Bellinger 		dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
3612c66ac9dbSNicholas Bellinger 		dest_se_deve->mapped_lun);
36138b1e1244SAndy Grover 
3614c66ac9dbSNicholas Bellinger 	/*
3615c66ac9dbSNicholas Bellinger 	 * A persistent reservation needs to already existing in order to
3616c66ac9dbSNicholas Bellinger 	 * successfully complete the REGISTER_AND_MOVE service action..
3617c66ac9dbSNicholas Bellinger 	 */
3618c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
3619c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
36206708bb27SAndy Grover 	if (!pr_res_holder) {
36216708bb27SAndy Grover 		pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3622c66ac9dbSNicholas Bellinger 			" currently held\n");
3623c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
362403e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
362503e98c9eSNicholas Bellinger 		ret = -EINVAL;
3626c66ac9dbSNicholas Bellinger 		goto out;
3627c66ac9dbSNicholas Bellinger 	}
3628c66ac9dbSNicholas Bellinger 	/*
3629c66ac9dbSNicholas Bellinger 	 * The received on I_T Nexus must be the reservation holder.
3630c66ac9dbSNicholas Bellinger 	 *
3631c66ac9dbSNicholas Bellinger 	 * From spc4r17 section 5.7.8  Table 50 --
3632c66ac9dbSNicholas Bellinger 	 * 	Register behaviors for a REGISTER AND MOVE service action
3633c66ac9dbSNicholas Bellinger 	 */
3634c66ac9dbSNicholas Bellinger 	if (pr_res_holder != pr_reg) {
36356708bb27SAndy Grover 		pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3636c66ac9dbSNicholas Bellinger 			" Nexus is not reservation holder\n");
3637c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
363803e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
363903e98c9eSNicholas Bellinger 		ret = -EINVAL;
3640c66ac9dbSNicholas Bellinger 		goto out;
3641c66ac9dbSNicholas Bellinger 	}
3642c66ac9dbSNicholas Bellinger 	/*
3643c66ac9dbSNicholas Bellinger 	 * From spc4r17 section 5.7.8: registering and moving reservation
3644c66ac9dbSNicholas Bellinger 	 *
3645c66ac9dbSNicholas Bellinger 	 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3646c66ac9dbSNicholas Bellinger 	 * action is received and the established persistent reservation is a
3647c66ac9dbSNicholas Bellinger 	 * Write Exclusive - All Registrants type or Exclusive Access -
3648c66ac9dbSNicholas Bellinger 	 * All Registrants type reservation, then the command shall be completed
3649c66ac9dbSNicholas Bellinger 	 * with RESERVATION CONFLICT status.
3650c66ac9dbSNicholas Bellinger 	 */
3651c66ac9dbSNicholas Bellinger 	if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3652c66ac9dbSNicholas Bellinger 	    (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
36536708bb27SAndy Grover 		pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3654c66ac9dbSNicholas Bellinger 			" reservation for type: %s\n",
3655c66ac9dbSNicholas Bellinger 			core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3656c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
365703e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
365803e98c9eSNicholas Bellinger 		ret = -EINVAL;
3659c66ac9dbSNicholas Bellinger 		goto out;
3660c66ac9dbSNicholas Bellinger 	}
3661c66ac9dbSNicholas Bellinger 	pr_res_nacl = pr_res_holder->pr_reg_nacl;
3662c66ac9dbSNicholas Bellinger 	/*
3663c66ac9dbSNicholas Bellinger 	 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3664c66ac9dbSNicholas Bellinger 	 */
3665c66ac9dbSNicholas Bellinger 	type = pr_res_holder->pr_res_type;
3666c66ac9dbSNicholas Bellinger 	scope = pr_res_holder->pr_res_type;
3667c66ac9dbSNicholas Bellinger 	/*
3668c66ac9dbSNicholas Bellinger 	 * c) Associate the reservation key specified in the SERVICE ACTION
3669c66ac9dbSNicholas Bellinger 	 *    RESERVATION KEY field with the I_T nexus specified as the
3670c66ac9dbSNicholas Bellinger 	 *    destination of the register and move, where:
3671c66ac9dbSNicholas Bellinger 	 *    A) The I_T nexus is specified by the TransportID and the
3672c66ac9dbSNicholas Bellinger 	 *	 RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3673c66ac9dbSNicholas Bellinger 	 *    B) Regardless of the TransportID format used, the association for
3674c66ac9dbSNicholas Bellinger 	 *       the initiator port is based on either the initiator port name
3675c66ac9dbSNicholas Bellinger 	 *       (see 3.1.71) on SCSI transport protocols where port names are
3676c66ac9dbSNicholas Bellinger 	 *       required or the initiator port identifier (see 3.1.70) on SCSI
3677c66ac9dbSNicholas Bellinger 	 *       transport protocols where port names are not required;
3678c66ac9dbSNicholas Bellinger 	 * d) Register the reservation key specified in the SERVICE ACTION
3679c66ac9dbSNicholas Bellinger 	 *    RESERVATION KEY field;
3680c66ac9dbSNicholas Bellinger 	 * e) Retain the reservation key specified in the SERVICE ACTION
3681c66ac9dbSNicholas Bellinger 	 *    RESERVATION KEY field and associated information;
3682c66ac9dbSNicholas Bellinger 	 *
3683c66ac9dbSNicholas Bellinger 	 * Also, It is not an error for a REGISTER AND MOVE service action to
3684c66ac9dbSNicholas Bellinger 	 * register an I_T nexus that is already registered with the same
3685c66ac9dbSNicholas Bellinger 	 * reservation key or a different reservation key.
3686c66ac9dbSNicholas Bellinger 	 */
3687c66ac9dbSNicholas Bellinger 	dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3688c66ac9dbSNicholas Bellinger 					iport_ptr);
36896708bb27SAndy Grover 	if (!dest_pr_reg) {
36905951146dSAndy Grover 		ret = core_scsi3_alloc_registration(cmd->se_dev,
3691c66ac9dbSNicholas Bellinger 				dest_node_acl, dest_se_deve, iport_ptr,
3692c66ac9dbSNicholas Bellinger 				sa_res_key, 0, aptpl, 2, 1);
3693c66ac9dbSNicholas Bellinger 		if (ret != 0) {
3694c66ac9dbSNicholas Bellinger 			spin_unlock(&dev->dev_reservation_lock);
369503e98c9eSNicholas Bellinger 			cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
369603e98c9eSNicholas Bellinger 			ret = -EINVAL;
3697c66ac9dbSNicholas Bellinger 			goto out;
3698c66ac9dbSNicholas Bellinger 		}
3699c66ac9dbSNicholas Bellinger 		dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3700c66ac9dbSNicholas Bellinger 						iport_ptr);
3701c66ac9dbSNicholas Bellinger 		new_reg = 1;
3702c66ac9dbSNicholas Bellinger 	}
3703c66ac9dbSNicholas Bellinger 	/*
3704c66ac9dbSNicholas Bellinger 	 * f) Release the persistent reservation for the persistent reservation
3705c66ac9dbSNicholas Bellinger 	 *    holder (i.e., the I_T nexus on which the
3706c66ac9dbSNicholas Bellinger 	 */
3707c66ac9dbSNicholas Bellinger 	__core_scsi3_complete_pro_release(dev, pr_res_nacl,
3708c66ac9dbSNicholas Bellinger 			dev->dev_pr_res_holder, 0);
3709c66ac9dbSNicholas Bellinger 	/*
3710c66ac9dbSNicholas Bellinger 	 * g) Move the persistent reservation to the specified I_T nexus using
3711c66ac9dbSNicholas Bellinger 	 *    the same scope and type as the persistent reservation released in
3712c66ac9dbSNicholas Bellinger 	 *    item f); and
3713c66ac9dbSNicholas Bellinger 	 */
3714c66ac9dbSNicholas Bellinger 	dev->dev_pr_res_holder = dest_pr_reg;
3715c66ac9dbSNicholas Bellinger 	dest_pr_reg->pr_res_holder = 1;
3716c66ac9dbSNicholas Bellinger 	dest_pr_reg->pr_res_type = type;
3717c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_scope = scope;
3718c66ac9dbSNicholas Bellinger 	prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
3719c66ac9dbSNicholas Bellinger 				PR_REG_ISID_ID_LEN);
3720c66ac9dbSNicholas Bellinger 	/*
3721c66ac9dbSNicholas Bellinger 	 * Increment PRGeneration for existing registrations..
3722c66ac9dbSNicholas Bellinger 	 */
37236708bb27SAndy Grover 	if (!new_reg)
3724c66ac9dbSNicholas Bellinger 		dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3725c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
3726c66ac9dbSNicholas Bellinger 
37276708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3728c66ac9dbSNicholas Bellinger 		" created new reservation holder TYPE: %s on object RTPI:"
3729c66ac9dbSNicholas Bellinger 		" %hu  PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
3730c66ac9dbSNicholas Bellinger 		core_scsi3_pr_dump_type(type), rtpi,
3731c66ac9dbSNicholas Bellinger 		dest_pr_reg->pr_res_generation);
37326708bb27SAndy Grover 	pr_debug("SPC-3 PR Successfully moved reservation from"
3733c66ac9dbSNicholas Bellinger 		" %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3734c66ac9dbSNicholas Bellinger 		tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
3735c66ac9dbSNicholas Bellinger 		(prf_isid) ? &i_buf[0] : "", dest_tf_ops->get_fabric_name(),
3736c66ac9dbSNicholas Bellinger 		dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3737c66ac9dbSNicholas Bellinger 		iport_ptr : "");
3738c66ac9dbSNicholas Bellinger 	/*
3739c66ac9dbSNicholas Bellinger 	 * It is now safe to release configfs group dependencies for destination
3740c66ac9dbSNicholas Bellinger 	 * of Transport ID Initiator Device/Port Identifier
3741c66ac9dbSNicholas Bellinger 	 */
3742c66ac9dbSNicholas Bellinger 	core_scsi3_lunacl_undepend_item(dest_se_deve);
3743c66ac9dbSNicholas Bellinger 	core_scsi3_nodeacl_undepend_item(dest_node_acl);
3744c66ac9dbSNicholas Bellinger 	core_scsi3_tpg_undepend_item(dest_se_tpg);
3745c66ac9dbSNicholas Bellinger 	/*
3746c66ac9dbSNicholas Bellinger 	 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3747c66ac9dbSNicholas Bellinger 	 * nexus on which PERSISTENT RESERVE OUT command was received.
3748c66ac9dbSNicholas Bellinger 	 */
3749c66ac9dbSNicholas Bellinger 	if (unreg) {
3750c66ac9dbSNicholas Bellinger 		spin_lock(&pr_tmpl->registration_lock);
3751c66ac9dbSNicholas Bellinger 		__core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3752c66ac9dbSNicholas Bellinger 		spin_unlock(&pr_tmpl->registration_lock);
3753c66ac9dbSNicholas Bellinger 	} else
3754c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg);
3755c66ac9dbSNicholas Bellinger 
3756c66ac9dbSNicholas Bellinger 	/*
3757c66ac9dbSNicholas Bellinger 	 * Clear the APTPL metadata if APTPL has been disabled, otherwise
3758c66ac9dbSNicholas Bellinger 	 * write out the updated metadata to struct file for this SCSI device.
3759c66ac9dbSNicholas Bellinger 	 */
37606708bb27SAndy Grover 	if (!aptpl) {
3761c66ac9dbSNicholas Bellinger 		pr_tmpl->pr_aptpl_active = 0;
37625951146dSAndy Grover 		core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
37636708bb27SAndy Grover 		pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
3764c66ac9dbSNicholas Bellinger 				" REGISTER_AND_MOVE\n");
3765c66ac9dbSNicholas Bellinger 	} else {
3766c66ac9dbSNicholas Bellinger 		pr_tmpl->pr_aptpl_active = 1;
37675951146dSAndy Grover 		ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
3768c66ac9dbSNicholas Bellinger 				&dest_pr_reg->pr_aptpl_buf[0],
3769c66ac9dbSNicholas Bellinger 				pr_tmpl->pr_aptpl_buf_len);
37706708bb27SAndy Grover 		if (!ret)
37716708bb27SAndy Grover 			pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
3772c66ac9dbSNicholas Bellinger 					" REGISTER_AND_MOVE\n");
3773c66ac9dbSNicholas Bellinger 	}
3774c66ac9dbSNicholas Bellinger 
37754949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
377605d1c7c0SAndy Grover 
3777c66ac9dbSNicholas Bellinger 	core_scsi3_put_pr_reg(dest_pr_reg);
3778c66ac9dbSNicholas Bellinger 	return 0;
3779c66ac9dbSNicholas Bellinger out:
378005d1c7c0SAndy Grover 	if (buf)
37814949314cSAndy Grover 		transport_kunmap_data_sg(cmd);
3782c66ac9dbSNicholas Bellinger 	if (dest_se_deve)
3783c66ac9dbSNicholas Bellinger 		core_scsi3_lunacl_undepend_item(dest_se_deve);
3784c66ac9dbSNicholas Bellinger 	if (dest_node_acl)
3785c66ac9dbSNicholas Bellinger 		core_scsi3_nodeacl_undepend_item(dest_node_acl);
3786c66ac9dbSNicholas Bellinger 	core_scsi3_tpg_undepend_item(dest_se_tpg);
3787c66ac9dbSNicholas Bellinger 	core_scsi3_put_pr_reg(pr_reg);
3788c66ac9dbSNicholas Bellinger 	return ret;
3789c66ac9dbSNicholas Bellinger }
3790c66ac9dbSNicholas Bellinger 
3791c66ac9dbSNicholas Bellinger static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
3792c66ac9dbSNicholas Bellinger {
3793c66ac9dbSNicholas Bellinger 	unsigned int __v1, __v2;
3794c66ac9dbSNicholas Bellinger 
3795c66ac9dbSNicholas Bellinger 	__v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
3796c66ac9dbSNicholas Bellinger 	__v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
3797c66ac9dbSNicholas Bellinger 
3798c66ac9dbSNicholas Bellinger 	return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
3799c66ac9dbSNicholas Bellinger }
3800c66ac9dbSNicholas Bellinger 
3801c66ac9dbSNicholas Bellinger /*
3802c66ac9dbSNicholas Bellinger  * See spc4r17 section 6.14 Table 170
3803c66ac9dbSNicholas Bellinger  */
38046bb35e00SChristoph Hellwig int target_scsi3_emulate_pr_out(struct se_cmd *cmd)
3805c66ac9dbSNicholas Bellinger {
3806617c0e06SChristoph Hellwig 	unsigned char *cdb = &cmd->t_task_cdb[0];
380705d1c7c0SAndy Grover 	unsigned char *buf;
3808c66ac9dbSNicholas Bellinger 	u64 res_key, sa_res_key;
3809c66ac9dbSNicholas Bellinger 	int sa, scope, type, aptpl;
3810c66ac9dbSNicholas Bellinger 	int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
3811d29a5b6aSChristoph Hellwig 	int ret;
3812617c0e06SChristoph Hellwig 
3813617c0e06SChristoph Hellwig 	/*
3814617c0e06SChristoph Hellwig 	 * Following spc2r20 5.5.1 Reservations overview:
3815617c0e06SChristoph Hellwig 	 *
3816617c0e06SChristoph Hellwig 	 * If a logical unit has been reserved by any RESERVE command and is
3817617c0e06SChristoph Hellwig 	 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3818617c0e06SChristoph Hellwig 	 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3819617c0e06SChristoph Hellwig 	 * initiator or service action and shall terminate with a RESERVATION
3820617c0e06SChristoph Hellwig 	 * CONFLICT status.
3821617c0e06SChristoph Hellwig 	 */
38220fd97ccfSChristoph Hellwig 	if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
3823617c0e06SChristoph Hellwig 		pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3824617c0e06SChristoph Hellwig 			" SPC-2 reservation is held, returning"
3825617c0e06SChristoph Hellwig 			" RESERVATION_CONFLICT\n");
382603e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3827d35212f3SRoland Dreier 		ret = -EINVAL;
3828d29a5b6aSChristoph Hellwig 		goto out;
3829617c0e06SChristoph Hellwig 	}
3830617c0e06SChristoph Hellwig 
3831c66ac9dbSNicholas Bellinger 	/*
3832c66ac9dbSNicholas Bellinger 	 * FIXME: A NULL struct se_session pointer means an this is not coming from
3833c66ac9dbSNicholas Bellinger 	 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3834c66ac9dbSNicholas Bellinger 	 */
383503e98c9eSNicholas Bellinger 	if (!cmd->se_sess) {
383603e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3837d35212f3SRoland Dreier 		ret = -EINVAL;
3838d35212f3SRoland Dreier 		goto out;
383903e98c9eSNicholas Bellinger 	}
3840c66ac9dbSNicholas Bellinger 
3841c66ac9dbSNicholas Bellinger 	if (cmd->data_length < 24) {
38426708bb27SAndy Grover 		pr_warn("SPC-PR: Received PR OUT parameter list"
3843c66ac9dbSNicholas Bellinger 			" length too small: %u\n", cmd->data_length);
384403e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
384503e98c9eSNicholas Bellinger 		ret = -EINVAL;
3846d29a5b6aSChristoph Hellwig 		goto out;
3847c66ac9dbSNicholas Bellinger 	}
3848c66ac9dbSNicholas Bellinger 	/*
3849c66ac9dbSNicholas Bellinger 	 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3850c66ac9dbSNicholas Bellinger 	 */
3851c66ac9dbSNicholas Bellinger 	sa = (cdb[1] & 0x1f);
3852c66ac9dbSNicholas Bellinger 	scope = (cdb[2] & 0xf0);
3853c66ac9dbSNicholas Bellinger 	type = (cdb[2] & 0x0f);
385405d1c7c0SAndy Grover 
38554949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
3856c66ac9dbSNicholas Bellinger 	/*
3857c66ac9dbSNicholas Bellinger 	 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3858c66ac9dbSNicholas Bellinger 	 */
3859c66ac9dbSNicholas Bellinger 	res_key = core_scsi3_extract_reservation_key(&buf[0]);
3860c66ac9dbSNicholas Bellinger 	sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
3861c66ac9dbSNicholas Bellinger 	/*
3862c66ac9dbSNicholas Bellinger 	 * REGISTER_AND_MOVE uses a different SA parameter list containing
3863c66ac9dbSNicholas Bellinger 	 * SCSI TransportIDs.
3864c66ac9dbSNicholas Bellinger 	 */
3865c66ac9dbSNicholas Bellinger 	if (sa != PRO_REGISTER_AND_MOVE) {
3866c66ac9dbSNicholas Bellinger 		spec_i_pt = (buf[20] & 0x08);
3867c66ac9dbSNicholas Bellinger 		all_tg_pt = (buf[20] & 0x04);
3868c66ac9dbSNicholas Bellinger 		aptpl = (buf[20] & 0x01);
3869c66ac9dbSNicholas Bellinger 	} else {
3870c66ac9dbSNicholas Bellinger 		aptpl = (buf[17] & 0x01);
3871c66ac9dbSNicholas Bellinger 		unreg = (buf[17] & 0x02);
3872c66ac9dbSNicholas Bellinger 	}
38734949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
387405d1c7c0SAndy Grover 	buf = NULL;
387505d1c7c0SAndy Grover 
3876c66ac9dbSNicholas Bellinger 	/*
3877c66ac9dbSNicholas Bellinger 	 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3878c66ac9dbSNicholas Bellinger 	 */
3879d29a5b6aSChristoph Hellwig 	if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER)) {
388003e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
388103e98c9eSNicholas Bellinger 		ret = -EINVAL;
3882d29a5b6aSChristoph Hellwig 		goto out;
3883d29a5b6aSChristoph Hellwig 	}
3884d29a5b6aSChristoph Hellwig 
3885c66ac9dbSNicholas Bellinger 	/*
3886c66ac9dbSNicholas Bellinger 	 * From spc4r17 section 6.14:
3887c66ac9dbSNicholas Bellinger 	 *
3888c66ac9dbSNicholas Bellinger 	 * If the SPEC_I_PT bit is set to zero, the service action is not
3889c66ac9dbSNicholas Bellinger 	 * REGISTER AND MOVE, and the parameter list length is not 24, then
3890c66ac9dbSNicholas Bellinger 	 * the command shall be terminated with CHECK CONDITION status, with
3891c66ac9dbSNicholas Bellinger 	 * the sense key set to ILLEGAL REQUEST, and the additional sense
3892c66ac9dbSNicholas Bellinger 	 * code set to PARAMETER LIST LENGTH ERROR.
3893c66ac9dbSNicholas Bellinger 	 */
38946708bb27SAndy Grover 	if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
3895c66ac9dbSNicholas Bellinger 	    (cmd->data_length != 24)) {
38966708bb27SAndy Grover 		pr_warn("SPC-PR: Received PR OUT illegal parameter"
3897c66ac9dbSNicholas Bellinger 			" list length: %u\n", cmd->data_length);
389803e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
389903e98c9eSNicholas Bellinger 		ret = -EINVAL;
3900d29a5b6aSChristoph Hellwig 		goto out;
3901c66ac9dbSNicholas Bellinger 	}
3902c66ac9dbSNicholas Bellinger 	/*
3903c66ac9dbSNicholas Bellinger 	 * (core_scsi3_emulate_pro_* function parameters
3904c66ac9dbSNicholas Bellinger 	 * are defined by spc4r17 Table 174:
3905c66ac9dbSNicholas Bellinger 	 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3906c66ac9dbSNicholas Bellinger 	 */
3907c66ac9dbSNicholas Bellinger 	switch (sa) {
3908c66ac9dbSNicholas Bellinger 	case PRO_REGISTER:
3909d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_register(cmd,
3910c66ac9dbSNicholas Bellinger 			res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 0);
3911d29a5b6aSChristoph Hellwig 		break;
3912c66ac9dbSNicholas Bellinger 	case PRO_RESERVE:
3913d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3914d29a5b6aSChristoph Hellwig 		break;
3915c66ac9dbSNicholas Bellinger 	case PRO_RELEASE:
3916d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3917d29a5b6aSChristoph Hellwig 		break;
3918c66ac9dbSNicholas Bellinger 	case PRO_CLEAR:
3919d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3920d29a5b6aSChristoph Hellwig 		break;
3921c66ac9dbSNicholas Bellinger 	case PRO_PREEMPT:
3922d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3923c66ac9dbSNicholas Bellinger 					res_key, sa_res_key, 0);
3924d29a5b6aSChristoph Hellwig 		break;
3925c66ac9dbSNicholas Bellinger 	case PRO_PREEMPT_AND_ABORT:
3926d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3927c66ac9dbSNicholas Bellinger 					res_key, sa_res_key, 1);
3928d29a5b6aSChristoph Hellwig 		break;
3929c66ac9dbSNicholas Bellinger 	case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
3930d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_register(cmd,
3931c66ac9dbSNicholas Bellinger 			0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 1);
3932d29a5b6aSChristoph Hellwig 		break;
3933c66ac9dbSNicholas Bellinger 	case PRO_REGISTER_AND_MOVE:
3934d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
3935c66ac9dbSNicholas Bellinger 				sa_res_key, aptpl, unreg);
3936d29a5b6aSChristoph Hellwig 		break;
3937c66ac9dbSNicholas Bellinger 	default:
39386708bb27SAndy Grover 		pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3939c66ac9dbSNicholas Bellinger 			" action: 0x%02x\n", cdb[1] & 0x1f);
394003e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
394103e98c9eSNicholas Bellinger 		ret = -EINVAL;
3942d29a5b6aSChristoph Hellwig 		break;
3943c66ac9dbSNicholas Bellinger 	}
3944c66ac9dbSNicholas Bellinger 
3945d29a5b6aSChristoph Hellwig out:
39466bb35e00SChristoph Hellwig 	if (!ret)
39476bb35e00SChristoph Hellwig 		target_complete_cmd(cmd, GOOD);
3948d29a5b6aSChristoph Hellwig 	return ret;
3949c66ac9dbSNicholas Bellinger }
3950c66ac9dbSNicholas Bellinger 
3951c66ac9dbSNicholas Bellinger /*
3952c66ac9dbSNicholas Bellinger  * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3953c66ac9dbSNicholas Bellinger  *
3954c66ac9dbSNicholas Bellinger  * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3955c66ac9dbSNicholas Bellinger  */
3956c66ac9dbSNicholas Bellinger static int core_scsi3_pri_read_keys(struct se_cmd *cmd)
3957c66ac9dbSNicholas Bellinger {
39580fd97ccfSChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
3959c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
396005d1c7c0SAndy Grover 	unsigned char *buf;
3961c66ac9dbSNicholas Bellinger 	u32 add_len = 0, off = 8;
3962c66ac9dbSNicholas Bellinger 
3963c66ac9dbSNicholas Bellinger 	if (cmd->data_length < 8) {
39646708bb27SAndy Grover 		pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3965c66ac9dbSNicholas Bellinger 			" too small\n", cmd->data_length);
396603e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
396703e98c9eSNicholas Bellinger 		return -EINVAL;
3968c66ac9dbSNicholas Bellinger 	}
3969c66ac9dbSNicholas Bellinger 
39704949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
39710fd97ccfSChristoph Hellwig 	buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
39720fd97ccfSChristoph Hellwig 	buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
39730fd97ccfSChristoph Hellwig 	buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
39740fd97ccfSChristoph Hellwig 	buf[3] = (dev->t10_pr.pr_generation & 0xff);
3975c66ac9dbSNicholas Bellinger 
39760fd97ccfSChristoph Hellwig 	spin_lock(&dev->t10_pr.registration_lock);
39770fd97ccfSChristoph Hellwig 	list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
3978c66ac9dbSNicholas Bellinger 			pr_reg_list) {
3979c66ac9dbSNicholas Bellinger 		/*
3980c66ac9dbSNicholas Bellinger 		 * Check for overflow of 8byte PRI READ_KEYS payload and
3981c66ac9dbSNicholas Bellinger 		 * next reservation key list descriptor.
3982c66ac9dbSNicholas Bellinger 		 */
3983c66ac9dbSNicholas Bellinger 		if ((add_len + 8) > (cmd->data_length - 8))
3984c66ac9dbSNicholas Bellinger 			break;
3985c66ac9dbSNicholas Bellinger 
3986c66ac9dbSNicholas Bellinger 		buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3987c66ac9dbSNicholas Bellinger 		buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3988c66ac9dbSNicholas Bellinger 		buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3989c66ac9dbSNicholas Bellinger 		buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
3990c66ac9dbSNicholas Bellinger 		buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
3991c66ac9dbSNicholas Bellinger 		buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
3992c66ac9dbSNicholas Bellinger 		buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
3993c66ac9dbSNicholas Bellinger 		buf[off++] = (pr_reg->pr_res_key & 0xff);
3994c66ac9dbSNicholas Bellinger 
3995c66ac9dbSNicholas Bellinger 		add_len += 8;
3996c66ac9dbSNicholas Bellinger 	}
39970fd97ccfSChristoph Hellwig 	spin_unlock(&dev->t10_pr.registration_lock);
3998c66ac9dbSNicholas Bellinger 
3999c66ac9dbSNicholas Bellinger 	buf[4] = ((add_len >> 24) & 0xff);
4000c66ac9dbSNicholas Bellinger 	buf[5] = ((add_len >> 16) & 0xff);
4001c66ac9dbSNicholas Bellinger 	buf[6] = ((add_len >> 8) & 0xff);
4002c66ac9dbSNicholas Bellinger 	buf[7] = (add_len & 0xff);
4003c66ac9dbSNicholas Bellinger 
40044949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
400505d1c7c0SAndy Grover 
4006c66ac9dbSNicholas Bellinger 	return 0;
4007c66ac9dbSNicholas Bellinger }
4008c66ac9dbSNicholas Bellinger 
4009c66ac9dbSNicholas Bellinger /*
4010c66ac9dbSNicholas Bellinger  * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
4011c66ac9dbSNicholas Bellinger  *
4012c66ac9dbSNicholas Bellinger  * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
4013c66ac9dbSNicholas Bellinger  */
4014c66ac9dbSNicholas Bellinger static int core_scsi3_pri_read_reservation(struct se_cmd *cmd)
4015c66ac9dbSNicholas Bellinger {
40160fd97ccfSChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
4017c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
401805d1c7c0SAndy Grover 	unsigned char *buf;
4019c66ac9dbSNicholas Bellinger 	u64 pr_res_key;
4020c66ac9dbSNicholas Bellinger 	u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
4021c66ac9dbSNicholas Bellinger 
4022c66ac9dbSNicholas Bellinger 	if (cmd->data_length < 8) {
40236708bb27SAndy Grover 		pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
4024c66ac9dbSNicholas Bellinger 			" too small\n", cmd->data_length);
402503e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
402603e98c9eSNicholas Bellinger 		return -EINVAL;
4027c66ac9dbSNicholas Bellinger 	}
4028c66ac9dbSNicholas Bellinger 
40294949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
40300fd97ccfSChristoph Hellwig 	buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
40310fd97ccfSChristoph Hellwig 	buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
40320fd97ccfSChristoph Hellwig 	buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
40330fd97ccfSChristoph Hellwig 	buf[3] = (dev->t10_pr.pr_generation & 0xff);
4034c66ac9dbSNicholas Bellinger 
40350fd97ccfSChristoph Hellwig 	spin_lock(&dev->dev_reservation_lock);
40360fd97ccfSChristoph Hellwig 	pr_reg = dev->dev_pr_res_holder;
4037ee1b1b9cSAndy Grover 	if (pr_reg) {
4038c66ac9dbSNicholas Bellinger 		/*
4039c66ac9dbSNicholas Bellinger 		 * Set the hardcoded Additional Length
4040c66ac9dbSNicholas Bellinger 		 */
4041c66ac9dbSNicholas Bellinger 		buf[4] = ((add_len >> 24) & 0xff);
4042c66ac9dbSNicholas Bellinger 		buf[5] = ((add_len >> 16) & 0xff);
4043c66ac9dbSNicholas Bellinger 		buf[6] = ((add_len >> 8) & 0xff);
4044c66ac9dbSNicholas Bellinger 		buf[7] = (add_len & 0xff);
4045c66ac9dbSNicholas Bellinger 
404605d1c7c0SAndy Grover 		if (cmd->data_length < 22)
404705d1c7c0SAndy Grover 			goto err;
404805d1c7c0SAndy Grover 
4049c66ac9dbSNicholas Bellinger 		/*
4050c66ac9dbSNicholas Bellinger 		 * Set the Reservation key.
4051c66ac9dbSNicholas Bellinger 		 *
4052c66ac9dbSNicholas Bellinger 		 * From spc4r17, section 5.7.10:
4053c66ac9dbSNicholas Bellinger 		 * A persistent reservation holder has its reservation key
4054c66ac9dbSNicholas Bellinger 		 * returned in the parameter data from a PERSISTENT
4055c66ac9dbSNicholas Bellinger 		 * RESERVE IN command with READ RESERVATION service action as
4056c66ac9dbSNicholas Bellinger 		 * follows:
4057c66ac9dbSNicholas Bellinger 		 * a) For a persistent reservation of the type Write Exclusive
4058c66ac9dbSNicholas Bellinger 		 *    - All Registrants or Exclusive Access ­ All Regitrants,
4059c66ac9dbSNicholas Bellinger 		 *      the reservation key shall be set to zero; or
4060c66ac9dbSNicholas Bellinger 		 * b) For all other persistent reservation types, the
4061c66ac9dbSNicholas Bellinger 		 *    reservation key shall be set to the registered
4062c66ac9dbSNicholas Bellinger 		 *    reservation key for the I_T nexus that holds the
4063c66ac9dbSNicholas Bellinger 		 *    persistent reservation.
4064c66ac9dbSNicholas Bellinger 		 */
4065c66ac9dbSNicholas Bellinger 		if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
4066c66ac9dbSNicholas Bellinger 		    (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
4067c66ac9dbSNicholas Bellinger 			pr_res_key = 0;
4068c66ac9dbSNicholas Bellinger 		else
4069c66ac9dbSNicholas Bellinger 			pr_res_key = pr_reg->pr_res_key;
4070c66ac9dbSNicholas Bellinger 
4071c66ac9dbSNicholas Bellinger 		buf[8] = ((pr_res_key >> 56) & 0xff);
4072c66ac9dbSNicholas Bellinger 		buf[9] = ((pr_res_key >> 48) & 0xff);
4073c66ac9dbSNicholas Bellinger 		buf[10] = ((pr_res_key >> 40) & 0xff);
4074c66ac9dbSNicholas Bellinger 		buf[11] = ((pr_res_key >> 32) & 0xff);
4075c66ac9dbSNicholas Bellinger 		buf[12] = ((pr_res_key >> 24) & 0xff);
4076c66ac9dbSNicholas Bellinger 		buf[13] = ((pr_res_key >> 16) & 0xff);
4077c66ac9dbSNicholas Bellinger 		buf[14] = ((pr_res_key >> 8) & 0xff);
4078c66ac9dbSNicholas Bellinger 		buf[15] = (pr_res_key & 0xff);
4079c66ac9dbSNicholas Bellinger 		/*
4080c66ac9dbSNicholas Bellinger 		 * Set the SCOPE and TYPE
4081c66ac9dbSNicholas Bellinger 		 */
4082c66ac9dbSNicholas Bellinger 		buf[21] = (pr_reg->pr_res_scope & 0xf0) |
4083c66ac9dbSNicholas Bellinger 			  (pr_reg->pr_res_type & 0x0f);
4084c66ac9dbSNicholas Bellinger 	}
408505d1c7c0SAndy Grover 
408605d1c7c0SAndy Grover err:
40870fd97ccfSChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
40884949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
4089c66ac9dbSNicholas Bellinger 
4090c66ac9dbSNicholas Bellinger 	return 0;
4091c66ac9dbSNicholas Bellinger }
4092c66ac9dbSNicholas Bellinger 
4093c66ac9dbSNicholas Bellinger /*
4094c66ac9dbSNicholas Bellinger  * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
4095c66ac9dbSNicholas Bellinger  *
4096c66ac9dbSNicholas Bellinger  * See spc4r17 section 6.13.4 Table 165
4097c66ac9dbSNicholas Bellinger  */
4098c66ac9dbSNicholas Bellinger static int core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
4099c66ac9dbSNicholas Bellinger {
41005951146dSAndy Grover 	struct se_device *dev = cmd->se_dev;
41010fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
410205d1c7c0SAndy Grover 	unsigned char *buf;
4103c66ac9dbSNicholas Bellinger 	u16 add_len = 8; /* Hardcoded to 8. */
4104c66ac9dbSNicholas Bellinger 
4105c66ac9dbSNicholas Bellinger 	if (cmd->data_length < 6) {
41066708bb27SAndy Grover 		pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
4107c66ac9dbSNicholas Bellinger 			" %u too small\n", cmd->data_length);
410803e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
410903e98c9eSNicholas Bellinger 		return -EINVAL;
4110c66ac9dbSNicholas Bellinger 	}
4111c66ac9dbSNicholas Bellinger 
41124949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
411305d1c7c0SAndy Grover 
4114c66ac9dbSNicholas Bellinger 	buf[0] = ((add_len << 8) & 0xff);
4115c66ac9dbSNicholas Bellinger 	buf[1] = (add_len & 0xff);
4116c66ac9dbSNicholas Bellinger 	buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
4117c66ac9dbSNicholas Bellinger 	buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
4118c66ac9dbSNicholas Bellinger 	buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
4119c66ac9dbSNicholas Bellinger 	buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
4120c66ac9dbSNicholas Bellinger 	/*
4121c66ac9dbSNicholas Bellinger 	 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
4122c66ac9dbSNicholas Bellinger 	 * set the TMV: Task Mask Valid bit.
4123c66ac9dbSNicholas Bellinger 	 */
4124c66ac9dbSNicholas Bellinger 	buf[3] |= 0x80;
4125c66ac9dbSNicholas Bellinger 	/*
4126c66ac9dbSNicholas Bellinger 	 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
4127c66ac9dbSNicholas Bellinger 	 */
4128c66ac9dbSNicholas Bellinger 	buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
4129c66ac9dbSNicholas Bellinger 	/*
4130c66ac9dbSNicholas Bellinger 	 * PTPL_A: Persistence across Target Power Loss Active bit
4131c66ac9dbSNicholas Bellinger 	 */
4132c66ac9dbSNicholas Bellinger 	if (pr_tmpl->pr_aptpl_active)
4133c66ac9dbSNicholas Bellinger 		buf[3] |= 0x01;
4134c66ac9dbSNicholas Bellinger 	/*
4135c66ac9dbSNicholas Bellinger 	 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
4136c66ac9dbSNicholas Bellinger 	 */
4137c66ac9dbSNicholas Bellinger 	buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4138c66ac9dbSNicholas Bellinger 	buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
4139c66ac9dbSNicholas Bellinger 	buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
4140c66ac9dbSNicholas Bellinger 	buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
4141c66ac9dbSNicholas Bellinger 	buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
4142c66ac9dbSNicholas Bellinger 	buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4143c66ac9dbSNicholas Bellinger 
41444949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
414505d1c7c0SAndy Grover 
4146c66ac9dbSNicholas Bellinger 	return 0;
4147c66ac9dbSNicholas Bellinger }
4148c66ac9dbSNicholas Bellinger 
4149c66ac9dbSNicholas Bellinger /*
4150c66ac9dbSNicholas Bellinger  * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
4151c66ac9dbSNicholas Bellinger  *
4152c66ac9dbSNicholas Bellinger  * See spc4r17 section 6.13.5 Table 168 and 169
4153c66ac9dbSNicholas Bellinger  */
4154c66ac9dbSNicholas Bellinger static int core_scsi3_pri_read_full_status(struct se_cmd *cmd)
4155c66ac9dbSNicholas Bellinger {
41560fd97ccfSChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
4157c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
4158c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg;
4159c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp;
41600fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
416105d1c7c0SAndy Grover 	unsigned char *buf;
4162c66ac9dbSNicholas Bellinger 	u32 add_desc_len = 0, add_len = 0, desc_len, exp_desc_len;
4163c66ac9dbSNicholas Bellinger 	u32 off = 8; /* off into first Full Status descriptor */
4164c66ac9dbSNicholas Bellinger 	int format_code = 0;
4165c66ac9dbSNicholas Bellinger 
4166c66ac9dbSNicholas Bellinger 	if (cmd->data_length < 8) {
41676708bb27SAndy Grover 		pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
4168c66ac9dbSNicholas Bellinger 			" too small\n", cmd->data_length);
416903e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
417003e98c9eSNicholas Bellinger 		return -EINVAL;
4171c66ac9dbSNicholas Bellinger 	}
4172c66ac9dbSNicholas Bellinger 
41734949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
417405d1c7c0SAndy Grover 
41750fd97ccfSChristoph Hellwig 	buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
41760fd97ccfSChristoph Hellwig 	buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
41770fd97ccfSChristoph Hellwig 	buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
41780fd97ccfSChristoph Hellwig 	buf[3] = (dev->t10_pr.pr_generation & 0xff);
4179c66ac9dbSNicholas Bellinger 
4180c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
4181c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
4182c66ac9dbSNicholas Bellinger 			&pr_tmpl->registration_list, pr_reg_list) {
4183c66ac9dbSNicholas Bellinger 
4184c66ac9dbSNicholas Bellinger 		se_nacl = pr_reg->pr_reg_nacl;
4185c66ac9dbSNicholas Bellinger 		se_tpg = pr_reg->pr_reg_nacl->se_tpg;
4186c66ac9dbSNicholas Bellinger 		add_desc_len = 0;
4187c66ac9dbSNicholas Bellinger 
4188c66ac9dbSNicholas Bellinger 		atomic_inc(&pr_reg->pr_res_holders);
4189c66ac9dbSNicholas Bellinger 		smp_mb__after_atomic_inc();
4190c66ac9dbSNicholas Bellinger 		spin_unlock(&pr_tmpl->registration_lock);
4191c66ac9dbSNicholas Bellinger 		/*
4192c66ac9dbSNicholas Bellinger 		 * Determine expected length of $FABRIC_MOD specific
4193c66ac9dbSNicholas Bellinger 		 * TransportID full status descriptor..
4194c66ac9dbSNicholas Bellinger 		 */
4195e3d6f909SAndy Grover 		exp_desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id_len(
4196c66ac9dbSNicholas Bellinger 				se_tpg, se_nacl, pr_reg, &format_code);
4197c66ac9dbSNicholas Bellinger 
4198c66ac9dbSNicholas Bellinger 		if ((exp_desc_len + add_len) > cmd->data_length) {
41996708bb27SAndy Grover 			pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
4200c66ac9dbSNicholas Bellinger 				" out of buffer: %d\n", cmd->data_length);
4201c66ac9dbSNicholas Bellinger 			spin_lock(&pr_tmpl->registration_lock);
4202c66ac9dbSNicholas Bellinger 			atomic_dec(&pr_reg->pr_res_holders);
4203c66ac9dbSNicholas Bellinger 			smp_mb__after_atomic_dec();
4204c66ac9dbSNicholas Bellinger 			break;
4205c66ac9dbSNicholas Bellinger 		}
4206c66ac9dbSNicholas Bellinger 		/*
4207c66ac9dbSNicholas Bellinger 		 * Set RESERVATION KEY
4208c66ac9dbSNicholas Bellinger 		 */
4209c66ac9dbSNicholas Bellinger 		buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
4210c66ac9dbSNicholas Bellinger 		buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
4211c66ac9dbSNicholas Bellinger 		buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
4212c66ac9dbSNicholas Bellinger 		buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
4213c66ac9dbSNicholas Bellinger 		buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
4214c66ac9dbSNicholas Bellinger 		buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
4215c66ac9dbSNicholas Bellinger 		buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
4216c66ac9dbSNicholas Bellinger 		buf[off++] = (pr_reg->pr_res_key & 0xff);
4217c66ac9dbSNicholas Bellinger 		off += 4; /* Skip Over Reserved area */
4218c66ac9dbSNicholas Bellinger 
4219c66ac9dbSNicholas Bellinger 		/*
4220c66ac9dbSNicholas Bellinger 		 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4221c66ac9dbSNicholas Bellinger 		 */
4222c66ac9dbSNicholas Bellinger 		if (pr_reg->pr_reg_all_tg_pt)
4223c66ac9dbSNicholas Bellinger 			buf[off] = 0x02;
4224c66ac9dbSNicholas Bellinger 		/*
4225c66ac9dbSNicholas Bellinger 		 * The struct se_lun pointer will be present for the
4226c66ac9dbSNicholas Bellinger 		 * reservation holder for PR_HOLDER bit.
4227c66ac9dbSNicholas Bellinger 		 *
4228c66ac9dbSNicholas Bellinger 		 * Also, if this registration is the reservation
4229c66ac9dbSNicholas Bellinger 		 * holder, fill in SCOPE and TYPE in the next byte.
4230c66ac9dbSNicholas Bellinger 		 */
4231c66ac9dbSNicholas Bellinger 		if (pr_reg->pr_res_holder) {
4232c66ac9dbSNicholas Bellinger 			buf[off++] |= 0x01;
4233c66ac9dbSNicholas Bellinger 			buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
4234c66ac9dbSNicholas Bellinger 				     (pr_reg->pr_res_type & 0x0f);
4235c66ac9dbSNicholas Bellinger 		} else
4236c66ac9dbSNicholas Bellinger 			off += 2;
4237c66ac9dbSNicholas Bellinger 
4238c66ac9dbSNicholas Bellinger 		off += 4; /* Skip over reserved area */
4239c66ac9dbSNicholas Bellinger 		/*
4240c66ac9dbSNicholas Bellinger 		 * From spc4r17 6.3.15:
4241c66ac9dbSNicholas Bellinger 		 *
4242c66ac9dbSNicholas Bellinger 		 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4243c66ac9dbSNicholas Bellinger 		 * IDENTIFIER field contains the relative port identifier (see
4244c66ac9dbSNicholas Bellinger 		 * 3.1.120) of the target port that is part of the I_T nexus
4245c66ac9dbSNicholas Bellinger 		 * described by this full status descriptor. If the ALL_TG_PT
4246c66ac9dbSNicholas Bellinger 		 * bit is set to one, the contents of the RELATIVE TARGET PORT
4247c66ac9dbSNicholas Bellinger 		 * IDENTIFIER field are not defined by this standard.
4248c66ac9dbSNicholas Bellinger 		 */
42496708bb27SAndy Grover 		if (!pr_reg->pr_reg_all_tg_pt) {
4250c66ac9dbSNicholas Bellinger 			struct se_port *port = pr_reg->pr_reg_tg_pt_lun->lun_sep;
4251c66ac9dbSNicholas Bellinger 
4252c66ac9dbSNicholas Bellinger 			buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
4253c66ac9dbSNicholas Bellinger 			buf[off++] = (port->sep_rtpi & 0xff);
4254c66ac9dbSNicholas Bellinger 		} else
425535d1efe8SMasanari Iida 			off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
4256c66ac9dbSNicholas Bellinger 
4257c66ac9dbSNicholas Bellinger 		/*
4258c66ac9dbSNicholas Bellinger 		 * Now, have the $FABRIC_MOD fill in the protocol identifier
4259c66ac9dbSNicholas Bellinger 		 */
4260e3d6f909SAndy Grover 		desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id(se_tpg,
4261c66ac9dbSNicholas Bellinger 				se_nacl, pr_reg, &format_code, &buf[off+4]);
4262c66ac9dbSNicholas Bellinger 
4263c66ac9dbSNicholas Bellinger 		spin_lock(&pr_tmpl->registration_lock);
4264c66ac9dbSNicholas Bellinger 		atomic_dec(&pr_reg->pr_res_holders);
4265c66ac9dbSNicholas Bellinger 		smp_mb__after_atomic_dec();
4266c66ac9dbSNicholas Bellinger 		/*
4267c66ac9dbSNicholas Bellinger 		 * Set the ADDITIONAL DESCRIPTOR LENGTH
4268c66ac9dbSNicholas Bellinger 		 */
4269c66ac9dbSNicholas Bellinger 		buf[off++] = ((desc_len >> 24) & 0xff);
4270c66ac9dbSNicholas Bellinger 		buf[off++] = ((desc_len >> 16) & 0xff);
4271c66ac9dbSNicholas Bellinger 		buf[off++] = ((desc_len >> 8) & 0xff);
4272c66ac9dbSNicholas Bellinger 		buf[off++] = (desc_len & 0xff);
4273c66ac9dbSNicholas Bellinger 		/*
4274c66ac9dbSNicholas Bellinger 		 * Size of full desctipor header minus TransportID
4275c66ac9dbSNicholas Bellinger 		 * containing $FABRIC_MOD specific) initiator device/port
4276c66ac9dbSNicholas Bellinger 		 * WWN information.
4277c66ac9dbSNicholas Bellinger 		 *
4278c66ac9dbSNicholas Bellinger 		 *  See spc4r17 Section 6.13.5 Table 169
4279c66ac9dbSNicholas Bellinger 		 */
4280c66ac9dbSNicholas Bellinger 		add_desc_len = (24 + desc_len);
4281c66ac9dbSNicholas Bellinger 
4282c66ac9dbSNicholas Bellinger 		off += desc_len;
4283c66ac9dbSNicholas Bellinger 		add_len += add_desc_len;
4284c66ac9dbSNicholas Bellinger 	}
4285c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
4286c66ac9dbSNicholas Bellinger 	/*
4287c66ac9dbSNicholas Bellinger 	 * Set ADDITIONAL_LENGTH
4288c66ac9dbSNicholas Bellinger 	 */
4289c66ac9dbSNicholas Bellinger 	buf[4] = ((add_len >> 24) & 0xff);
4290c66ac9dbSNicholas Bellinger 	buf[5] = ((add_len >> 16) & 0xff);
4291c66ac9dbSNicholas Bellinger 	buf[6] = ((add_len >> 8) & 0xff);
4292c66ac9dbSNicholas Bellinger 	buf[7] = (add_len & 0xff);
4293c66ac9dbSNicholas Bellinger 
42944949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
429505d1c7c0SAndy Grover 
4296c66ac9dbSNicholas Bellinger 	return 0;
4297c66ac9dbSNicholas Bellinger }
4298c66ac9dbSNicholas Bellinger 
42996bb35e00SChristoph Hellwig int target_scsi3_emulate_pr_in(struct se_cmd *cmd)
4300c66ac9dbSNicholas Bellinger {
4301d29a5b6aSChristoph Hellwig 	int ret;
4302e76a35d6SChristoph Hellwig 
4303617c0e06SChristoph Hellwig 	/*
4304617c0e06SChristoph Hellwig 	 * Following spc2r20 5.5.1 Reservations overview:
4305617c0e06SChristoph Hellwig 	 *
4306617c0e06SChristoph Hellwig 	 * If a logical unit has been reserved by any RESERVE command and is
4307617c0e06SChristoph Hellwig 	 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4308617c0e06SChristoph Hellwig 	 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4309617c0e06SChristoph Hellwig 	 * initiator or service action and shall terminate with a RESERVATION
4310617c0e06SChristoph Hellwig 	 * CONFLICT status.
4311617c0e06SChristoph Hellwig 	 */
43120fd97ccfSChristoph Hellwig 	if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
4313617c0e06SChristoph Hellwig 		pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4314617c0e06SChristoph Hellwig 			" SPC-2 reservation is held, returning"
4315617c0e06SChristoph Hellwig 			" RESERVATION_CONFLICT\n");
431603e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
431703e98c9eSNicholas Bellinger 		return -EINVAL;
4318617c0e06SChristoph Hellwig 	}
4319617c0e06SChristoph Hellwig 
4320617c0e06SChristoph Hellwig 	switch (cmd->t_task_cdb[1] & 0x1f) {
4321c66ac9dbSNicholas Bellinger 	case PRI_READ_KEYS:
4322d29a5b6aSChristoph Hellwig 		ret = core_scsi3_pri_read_keys(cmd);
4323d29a5b6aSChristoph Hellwig 		break;
4324c66ac9dbSNicholas Bellinger 	case PRI_READ_RESERVATION:
4325d29a5b6aSChristoph Hellwig 		ret = core_scsi3_pri_read_reservation(cmd);
4326d29a5b6aSChristoph Hellwig 		break;
4327c66ac9dbSNicholas Bellinger 	case PRI_REPORT_CAPABILITIES:
4328d29a5b6aSChristoph Hellwig 		ret = core_scsi3_pri_report_capabilities(cmd);
4329d29a5b6aSChristoph Hellwig 		break;
4330c66ac9dbSNicholas Bellinger 	case PRI_READ_FULL_STATUS:
4331d29a5b6aSChristoph Hellwig 		ret = core_scsi3_pri_read_full_status(cmd);
4332d29a5b6aSChristoph Hellwig 		break;
4333c66ac9dbSNicholas Bellinger 	default:
43346708bb27SAndy Grover 		pr_err("Unknown PERSISTENT_RESERVE_IN service"
4335617c0e06SChristoph Hellwig 			" action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
433603e98c9eSNicholas Bellinger 		cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
433703e98c9eSNicholas Bellinger 		ret = -EINVAL;
4338d29a5b6aSChristoph Hellwig 		break;
4339c66ac9dbSNicholas Bellinger 	}
4340d29a5b6aSChristoph Hellwig 
43416bb35e00SChristoph Hellwig 	if (!ret)
43426bb35e00SChristoph Hellwig 		target_complete_cmd(cmd, GOOD);
4343d29a5b6aSChristoph Hellwig 	return ret;
4344c66ac9dbSNicholas Bellinger }
4345c66ac9dbSNicholas Bellinger 
4346c66ac9dbSNicholas Bellinger static int core_pt_reservation_check(struct se_cmd *cmd, u32 *pr_res_type)
4347c66ac9dbSNicholas Bellinger {
4348c66ac9dbSNicholas Bellinger 	return 0;
4349c66ac9dbSNicholas Bellinger }
4350c66ac9dbSNicholas Bellinger 
4351c66ac9dbSNicholas Bellinger static int core_pt_seq_non_holder(
4352c66ac9dbSNicholas Bellinger 	struct se_cmd *cmd,
4353c66ac9dbSNicholas Bellinger 	unsigned char *cdb,
4354c66ac9dbSNicholas Bellinger 	u32 pr_reg_type)
4355c66ac9dbSNicholas Bellinger {
4356c66ac9dbSNicholas Bellinger 	return 0;
4357c66ac9dbSNicholas Bellinger }
4358c66ac9dbSNicholas Bellinger 
43590fd97ccfSChristoph Hellwig void core_setup_reservations(struct se_device *dev)
4360c66ac9dbSNicholas Bellinger {
43610fd97ccfSChristoph Hellwig 	struct t10_reservation *rest = &dev->t10_pr;
43620fd97ccfSChristoph Hellwig 
4363c66ac9dbSNicholas Bellinger 	/*
4364c66ac9dbSNicholas Bellinger 	 * If this device is from Target_Core_Mod/pSCSI, use the reservations
4365c66ac9dbSNicholas Bellinger 	 * of the Underlying SCSI hardware.  In Linux/SCSI terms, this can
4366c66ac9dbSNicholas Bellinger 	 * cause a problem because libata and some SATA RAID HBAs appear
4367c66ac9dbSNicholas Bellinger 	 * under Linux/SCSI, but to emulate reservations themselves.
4368c66ac9dbSNicholas Bellinger 	 */
43690fd97ccfSChristoph Hellwig 	if ((dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE) ||
43700fd97ccfSChristoph Hellwig 	    (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV &&
43710fd97ccfSChristoph Hellwig 	     !dev->dev_attrib.emulate_reservations)) {
4372c66ac9dbSNicholas Bellinger 		rest->res_type = SPC_PASSTHROUGH;
4373c66ac9dbSNicholas Bellinger 		rest->pr_ops.t10_reservation_check = &core_pt_reservation_check;
4374c66ac9dbSNicholas Bellinger 		rest->pr_ops.t10_seq_non_holder = &core_pt_seq_non_holder;
43756708bb27SAndy Grover 		pr_debug("%s: Using SPC_PASSTHROUGH, no reservation"
4376e3d6f909SAndy Grover 			" emulation\n", dev->transport->name);
43770fd97ccfSChristoph Hellwig 	} else if (dev->transport->get_device_rev(dev) >= SCSI_3) {
4378c66ac9dbSNicholas Bellinger 		rest->res_type = SPC3_PERSISTENT_RESERVATIONS;
4379c66ac9dbSNicholas Bellinger 		rest->pr_ops.t10_reservation_check = &core_scsi3_pr_reservation_check;
4380c66ac9dbSNicholas Bellinger 		rest->pr_ops.t10_seq_non_holder = &core_scsi3_pr_seq_non_holder;
43816708bb27SAndy Grover 		pr_debug("%s: Using SPC3_PERSISTENT_RESERVATIONS"
4382e3d6f909SAndy Grover 			" emulation\n", dev->transport->name);
4383c66ac9dbSNicholas Bellinger 	} else {
4384c66ac9dbSNicholas Bellinger 		rest->res_type = SPC2_RESERVATIONS;
4385c66ac9dbSNicholas Bellinger 		rest->pr_ops.t10_reservation_check = &core_scsi2_reservation_check;
4386c66ac9dbSNicholas Bellinger 		rest->pr_ops.t10_seq_non_holder =
4387c66ac9dbSNicholas Bellinger 				&core_scsi2_reservation_seq_non_holder;
43886708bb27SAndy Grover 		pr_debug("%s: Using SPC2_RESERVATIONS emulation\n",
4389e3d6f909SAndy Grover 			dev->transport->name);
4390c66ac9dbSNicholas Bellinger 	}
4391c66ac9dbSNicholas Bellinger }
4392