1c66ac9dbSNicholas Bellinger /*******************************************************************************
2c66ac9dbSNicholas Bellinger  * Filename:  target_core_pr.c
3c66ac9dbSNicholas Bellinger  *
4c66ac9dbSNicholas Bellinger  * This file contains SPC-3 compliant persistent reservations and
5c66ac9dbSNicholas Bellinger  * legacy SPC-2 reservations with compatible reservation handling (CRH=1)
6c66ac9dbSNicholas Bellinger  *
74c76251eSNicholas Bellinger  * (c) Copyright 2009-2013 Datera, Inc.
8c66ac9dbSNicholas Bellinger  *
9c66ac9dbSNicholas Bellinger  * Nicholas A. Bellinger <nab@kernel.org>
10c66ac9dbSNicholas Bellinger  *
11c66ac9dbSNicholas Bellinger  * This program is free software; you can redistribute it and/or modify
12c66ac9dbSNicholas Bellinger  * it under the terms of the GNU General Public License as published by
13c66ac9dbSNicholas Bellinger  * the Free Software Foundation; either version 2 of the License, or
14c66ac9dbSNicholas Bellinger  * (at your option) any later version.
15c66ac9dbSNicholas Bellinger  *
16c66ac9dbSNicholas Bellinger  * This program is distributed in the hope that it will be useful,
17c66ac9dbSNicholas Bellinger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18c66ac9dbSNicholas Bellinger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19c66ac9dbSNicholas Bellinger  * GNU General Public License for more details.
20c66ac9dbSNicholas Bellinger  *
21c66ac9dbSNicholas Bellinger  * You should have received a copy of the GNU General Public License
22c66ac9dbSNicholas Bellinger  * along with this program; if not, write to the Free Software
23c66ac9dbSNicholas Bellinger  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24c66ac9dbSNicholas Bellinger  *
25c66ac9dbSNicholas Bellinger  ******************************************************************************/
26c66ac9dbSNicholas Bellinger 
27c66ac9dbSNicholas Bellinger #include <linux/slab.h>
28c66ac9dbSNicholas Bellinger #include <linux/spinlock.h>
29c66ac9dbSNicholas Bellinger #include <linux/list.h>
305538d294SDavid S. Miller #include <linux/vmalloc.h>
310e9b10a9SAl Viro #include <linux/file.h>
328dcf07beSBart Van Assche #include <linux/fcntl.h>
338dcf07beSBart Van Assche #include <linux/fs.h>
34ba929992SBart Van Assche #include <scsi/scsi_proto.h>
35c66ac9dbSNicholas Bellinger #include <asm/unaligned.h>
36c66ac9dbSNicholas Bellinger 
37c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
38c4795fb2SChristoph Hellwig #include <target/target_core_backend.h>
39c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
40c66ac9dbSNicholas Bellinger 
41e26d99aeSChristoph Hellwig #include "target_core_internal.h"
42c66ac9dbSNicholas Bellinger #include "target_core_pr.h"
43c66ac9dbSNicholas Bellinger #include "target_core_ua.h"
44c66ac9dbSNicholas Bellinger 
45c66ac9dbSNicholas Bellinger /*
46c66ac9dbSNicholas Bellinger  * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
47c66ac9dbSNicholas Bellinger  */
48c66ac9dbSNicholas Bellinger struct pr_transport_id_holder {
49c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *dest_pr_reg;
50c66ac9dbSNicholas Bellinger 	struct se_portal_group *dest_tpg;
51c66ac9dbSNicholas Bellinger 	struct se_node_acl *dest_node_acl;
52c66ac9dbSNicholas Bellinger 	struct se_dev_entry *dest_se_deve;
53c66ac9dbSNicholas Bellinger 	struct list_head dest_list;
54c66ac9dbSNicholas Bellinger };
55c66ac9dbSNicholas Bellinger 
56d2843c17SAndy Grover void core_pr_dump_initiator_port(
57c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg,
58c66ac9dbSNicholas Bellinger 	char *buf,
59c66ac9dbSNicholas Bellinger 	u32 size)
60c66ac9dbSNicholas Bellinger {
6124528f08Stangwenji 	if (!pr_reg->isid_present_at_reg) {
62d2843c17SAndy Grover 		buf[0] = '\0';
6324528f08Stangwenji 		return;
6424528f08Stangwenji 	}
65c66ac9dbSNicholas Bellinger 
66d2843c17SAndy Grover 	snprintf(buf, size, ",i,0x%s", pr_reg->pr_reg_isid);
67c66ac9dbSNicholas Bellinger }
68c66ac9dbSNicholas Bellinger 
6933ce6a87SAndy Grover enum register_type {
7033ce6a87SAndy Grover 	REGISTER,
7133ce6a87SAndy Grover 	REGISTER_AND_IGNORE_EXISTING_KEY,
7233ce6a87SAndy Grover 	REGISTER_AND_MOVE,
7333ce6a87SAndy Grover };
7433ce6a87SAndy Grover 
7533ce6a87SAndy Grover enum preempt_type {
7633ce6a87SAndy Grover 	PREEMPT,
7733ce6a87SAndy Grover 	PREEMPT_AND_ABORT,
7833ce6a87SAndy Grover };
7933ce6a87SAndy Grover 
80c66ac9dbSNicholas Bellinger static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
816c3c9baaSNicholas Bellinger 					      struct t10_pr_registration *, int, int);
82c66ac9dbSNicholas Bellinger 
83e673dc92SIlias Tsitsimpis static int is_reservation_holder(
84e673dc92SIlias Tsitsimpis 	struct t10_pr_registration *pr_res_holder,
85e673dc92SIlias Tsitsimpis 	struct t10_pr_registration *pr_reg)
86e673dc92SIlias Tsitsimpis {
87e673dc92SIlias Tsitsimpis 	int pr_res_type;
88e673dc92SIlias Tsitsimpis 
89e673dc92SIlias Tsitsimpis 	if (pr_res_holder) {
90e673dc92SIlias Tsitsimpis 		pr_res_type = pr_res_holder->pr_res_type;
91e673dc92SIlias Tsitsimpis 
92e673dc92SIlias Tsitsimpis 		return pr_res_holder == pr_reg ||
93e673dc92SIlias Tsitsimpis 		       pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
94e673dc92SIlias Tsitsimpis 		       pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG;
95e673dc92SIlias Tsitsimpis 	}
96e673dc92SIlias Tsitsimpis 	return 0;
97e673dc92SIlias Tsitsimpis }
98e673dc92SIlias Tsitsimpis 
99de103c93SChristoph Hellwig static sense_reason_t
100de103c93SChristoph Hellwig target_scsi2_reservation_check(struct se_cmd *cmd)
101c66ac9dbSNicholas Bellinger {
102d977f437SChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
103d977f437SChristoph Hellwig 	struct se_session *sess = cmd->se_sess;
104d977f437SChristoph Hellwig 
105d977f437SChristoph Hellwig 	switch (cmd->t_task_cdb[0]) {
106c66ac9dbSNicholas Bellinger 	case INQUIRY:
107c66ac9dbSNicholas Bellinger 	case RELEASE:
108c66ac9dbSNicholas Bellinger 	case RELEASE_10:
109c66ac9dbSNicholas Bellinger 		return 0;
110c66ac9dbSNicholas Bellinger 	default:
111d977f437SChristoph Hellwig 		break;
112c66ac9dbSNicholas Bellinger 	}
113c66ac9dbSNicholas Bellinger 
114d977f437SChristoph Hellwig 	if (!dev->dev_reserved_node_acl || !sess)
115c66ac9dbSNicholas Bellinger 		return 0;
116c66ac9dbSNicholas Bellinger 
117d977f437SChristoph Hellwig 	if (dev->dev_reserved_node_acl != sess->se_node_acl)
118de103c93SChristoph Hellwig 		return TCM_RESERVATION_CONFLICT;
119c66ac9dbSNicholas Bellinger 
120d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
121d977f437SChristoph Hellwig 		if (dev->dev_res_bin_isid != sess->sess_bin_isid)
122de103c93SChristoph Hellwig 			return TCM_RESERVATION_CONFLICT;
123d977f437SChristoph Hellwig 	}
124d977f437SChristoph Hellwig 
125d977f437SChristoph Hellwig 	return 0;
126c66ac9dbSNicholas Bellinger }
127c66ac9dbSNicholas Bellinger 
128eacac00cSChristoph Hellwig static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
129eacac00cSChristoph Hellwig 					struct se_node_acl *, struct se_session *);
130eacac00cSChristoph Hellwig static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
131eacac00cSChristoph Hellwig 
132087a03b3SNicholas Bellinger static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
133eacac00cSChristoph Hellwig {
134eacac00cSChristoph Hellwig 	struct se_session *se_sess = cmd->se_sess;
1350fd97ccfSChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
136eacac00cSChristoph Hellwig 	struct t10_pr_registration *pr_reg;
1370fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
138eacac00cSChristoph Hellwig 	int conflict = 0;
139eacac00cSChristoph Hellwig 
140eacac00cSChristoph Hellwig 	pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
141eacac00cSChristoph Hellwig 			se_sess);
142eacac00cSChristoph Hellwig 	if (pr_reg) {
143eacac00cSChristoph Hellwig 		/*
144eacac00cSChristoph Hellwig 		 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
145eacac00cSChristoph Hellwig 		 * behavior
146eacac00cSChristoph Hellwig 		 *
147eacac00cSChristoph Hellwig 		 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
148eacac00cSChristoph Hellwig 		 * status, but no reservation shall be established and the
149eacac00cSChristoph Hellwig 		 * persistent reservation shall not be changed, if the command
150eacac00cSChristoph Hellwig 		 * is received from a) and b) below.
151eacac00cSChristoph Hellwig 		 *
152eacac00cSChristoph Hellwig 		 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
153eacac00cSChristoph Hellwig 		 * status, but the persistent reservation shall not be released,
154eacac00cSChristoph Hellwig 		 * if the command is received from a) and b)
155eacac00cSChristoph Hellwig 		 *
156eacac00cSChristoph Hellwig 		 * a) An I_T nexus that is a persistent reservation holder; or
157eacac00cSChristoph Hellwig 		 * b) An I_T nexus that is registered if a registrants only or
158eacac00cSChristoph Hellwig 		 *    all registrants type persistent reservation is present.
159eacac00cSChristoph Hellwig 		 *
160eacac00cSChristoph Hellwig 		 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
161eacac00cSChristoph Hellwig 		 * RELEASE(6) command, or RELEASE(10) command shall be processed
162eacac00cSChristoph Hellwig 		 * as defined in SPC-2.
163eacac00cSChristoph Hellwig 		 */
164eacac00cSChristoph Hellwig 		if (pr_reg->pr_res_holder) {
165eacac00cSChristoph Hellwig 			core_scsi3_put_pr_reg(pr_reg);
166087a03b3SNicholas Bellinger 			return 1;
167eacac00cSChristoph Hellwig 		}
168eacac00cSChristoph Hellwig 		if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
169eacac00cSChristoph Hellwig 		    (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
170eacac00cSChristoph Hellwig 		    (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
171eacac00cSChristoph Hellwig 		    (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
172eacac00cSChristoph Hellwig 			core_scsi3_put_pr_reg(pr_reg);
173087a03b3SNicholas Bellinger 			return 1;
174eacac00cSChristoph Hellwig 		}
175eacac00cSChristoph Hellwig 		core_scsi3_put_pr_reg(pr_reg);
176eacac00cSChristoph Hellwig 		conflict = 1;
177eacac00cSChristoph Hellwig 	} else {
178eacac00cSChristoph Hellwig 		/*
179eacac00cSChristoph Hellwig 		 * Following spc2r20 5.5.1 Reservations overview:
180eacac00cSChristoph Hellwig 		 *
181eacac00cSChristoph Hellwig 		 * If a logical unit has executed a PERSISTENT RESERVE OUT
182eacac00cSChristoph Hellwig 		 * command with the REGISTER or the REGISTER AND IGNORE
183eacac00cSChristoph Hellwig 		 * EXISTING KEY service action and is still registered by any
184eacac00cSChristoph Hellwig 		 * initiator, all RESERVE commands and all RELEASE commands
185eacac00cSChristoph Hellwig 		 * regardless of initiator shall conflict and shall terminate
186eacac00cSChristoph Hellwig 		 * with a RESERVATION CONFLICT status.
187eacac00cSChristoph Hellwig 		 */
188eacac00cSChristoph Hellwig 		spin_lock(&pr_tmpl->registration_lock);
189eacac00cSChristoph Hellwig 		conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
190eacac00cSChristoph Hellwig 		spin_unlock(&pr_tmpl->registration_lock);
191eacac00cSChristoph Hellwig 	}
192eacac00cSChristoph Hellwig 
193eacac00cSChristoph Hellwig 	if (conflict) {
194eacac00cSChristoph Hellwig 		pr_err("Received legacy SPC-2 RESERVE/RELEASE"
195eacac00cSChristoph Hellwig 			" while active SPC-3 registrations exist,"
196eacac00cSChristoph Hellwig 			" returning RESERVATION_CONFLICT\n");
197087a03b3SNicholas Bellinger 		return -EBUSY;
198eacac00cSChristoph Hellwig 	}
199eacac00cSChristoph Hellwig 
200087a03b3SNicholas Bellinger 	return 0;
201eacac00cSChristoph Hellwig }
202eacac00cSChristoph Hellwig 
203de103c93SChristoph Hellwig sense_reason_t
204de103c93SChristoph Hellwig target_scsi2_reservation_release(struct se_cmd *cmd)
205c66ac9dbSNicholas Bellinger {
206c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
207c66ac9dbSNicholas Bellinger 	struct se_session *sess = cmd->se_sess;
208609234e3SWei Yongjun 	struct se_portal_group *tpg;
209de103c93SChristoph Hellwig 	int rc;
210c66ac9dbSNicholas Bellinger 
211609234e3SWei Yongjun 	if (!sess || !sess->se_tpg)
212d29a5b6aSChristoph Hellwig 		goto out;
213087a03b3SNicholas Bellinger 	rc = target_check_scsi2_reservation_conflict(cmd);
214087a03b3SNicholas Bellinger 	if (rc == 1)
215d29a5b6aSChristoph Hellwig 		goto out;
216de103c93SChristoph Hellwig 	if (rc < 0)
217de103c93SChristoph Hellwig 		return TCM_RESERVATION_CONFLICT;
218c66ac9dbSNicholas Bellinger 
219c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
220d29a5b6aSChristoph Hellwig 	if (!dev->dev_reserved_node_acl || !sess)
221d29a5b6aSChristoph Hellwig 		goto out_unlock;
222c66ac9dbSNicholas Bellinger 
223d29a5b6aSChristoph Hellwig 	if (dev->dev_reserved_node_acl != sess->se_node_acl)
224d29a5b6aSChristoph Hellwig 		goto out_unlock;
225d29a5b6aSChristoph Hellwig 
226edc318d9SBernhard Kohl 	if (dev->dev_res_bin_isid != sess->sess_bin_isid)
227edc318d9SBernhard Kohl 		goto out_unlock;
228edc318d9SBernhard Kohl 
229c66ac9dbSNicholas Bellinger 	dev->dev_reserved_node_acl = NULL;
2300fd97ccfSChristoph Hellwig 	dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
2310fd97ccfSChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
232c66ac9dbSNicholas Bellinger 		dev->dev_res_bin_isid = 0;
2330fd97ccfSChristoph Hellwig 		dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS_WITH_ISID;
234c66ac9dbSNicholas Bellinger 	}
235609234e3SWei Yongjun 	tpg = sess->se_tpg;
236f2d30680SHannes Reinecke 	pr_debug("SCSI-2 Released reservation for %s LUN: %llu ->"
237f2d30680SHannes Reinecke 		" MAPPED LUN: %llu for %s\n",
23830c7ca93SDavid Disseldorp 		tpg->se_tpg_tfo->fabric_name,
23929a05deeSNicholas Bellinger 		cmd->se_lun->unpacked_lun, cmd->orig_fe_lun,
240c66ac9dbSNicholas Bellinger 		sess->se_node_acl->initiatorname);
241c66ac9dbSNicholas Bellinger 
242d29a5b6aSChristoph Hellwig out_unlock:
243d29a5b6aSChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
244d29a5b6aSChristoph Hellwig out:
2456bb35e00SChristoph Hellwig 	target_complete_cmd(cmd, GOOD);
246de103c93SChristoph Hellwig 	return 0;
247c66ac9dbSNicholas Bellinger }
248c66ac9dbSNicholas Bellinger 
249de103c93SChristoph Hellwig sense_reason_t
250de103c93SChristoph Hellwig target_scsi2_reservation_reserve(struct se_cmd *cmd)
251c66ac9dbSNicholas Bellinger {
252c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
253c66ac9dbSNicholas Bellinger 	struct se_session *sess = cmd->se_sess;
254609234e3SWei Yongjun 	struct se_portal_group *tpg;
255de103c93SChristoph Hellwig 	sense_reason_t ret = 0;
256de103c93SChristoph Hellwig 	int rc;
257c66ac9dbSNicholas Bellinger 
258a1d8b49aSAndy Grover 	if ((cmd->t_task_cdb[1] & 0x01) &&
259a1d8b49aSAndy Grover 	    (cmd->t_task_cdb[1] & 0x02)) {
260a3960cedSColin Ian King 		pr_err("LongIO and Obsolete Bits set, returning ILLEGAL_REQUEST\n");
261de103c93SChristoph Hellwig 		return TCM_UNSUPPORTED_SCSI_OPCODE;
262c66ac9dbSNicholas Bellinger 	}
263c66ac9dbSNicholas Bellinger 	/*
264c66ac9dbSNicholas Bellinger 	 * This is currently the case for target_core_mod passthrough struct se_cmd
265c66ac9dbSNicholas Bellinger 	 * ops
266c66ac9dbSNicholas Bellinger 	 */
267609234e3SWei Yongjun 	if (!sess || !sess->se_tpg)
268d29a5b6aSChristoph Hellwig 		goto out;
269087a03b3SNicholas Bellinger 	rc = target_check_scsi2_reservation_conflict(cmd);
270087a03b3SNicholas Bellinger 	if (rc == 1)
271d29a5b6aSChristoph Hellwig 		goto out;
272c66ac9dbSNicholas Bellinger 
273de103c93SChristoph Hellwig 	if (rc < 0)
274de103c93SChristoph Hellwig 		return TCM_RESERVATION_CONFLICT;
275de103c93SChristoph Hellwig 
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",
28130c7ca93SDavid Disseldorp 			tpg->se_tpg_tfo->fabric_name);
282f2d30680SHannes Reinecke 		pr_err("Original reserver LUN: %llu %s\n",
283e3d6f909SAndy Grover 			cmd->se_lun->unpacked_lun,
284c66ac9dbSNicholas Bellinger 			dev->dev_reserved_node_acl->initiatorname);
285f2d30680SHannes Reinecke 		pr_err("Current attempt - LUN: %llu -> MAPPED LUN: %llu"
286e3d6f909SAndy Grover 			" from %s \n", cmd->se_lun->unpacked_lun,
28729a05deeSNicholas Bellinger 			cmd->orig_fe_lun,
288c66ac9dbSNicholas Bellinger 			sess->se_node_acl->initiatorname);
289de103c93SChristoph Hellwig 		ret = TCM_RESERVATION_CONFLICT;
290d29a5b6aSChristoph Hellwig 		goto out_unlock;
291c66ac9dbSNicholas Bellinger 	}
292c66ac9dbSNicholas Bellinger 
293c66ac9dbSNicholas Bellinger 	dev->dev_reserved_node_acl = sess->se_node_acl;
2940fd97ccfSChristoph Hellwig 	dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS;
295c66ac9dbSNicholas Bellinger 	if (sess->sess_bin_isid != 0) {
296c66ac9dbSNicholas Bellinger 		dev->dev_res_bin_isid = sess->sess_bin_isid;
2970fd97ccfSChristoph Hellwig 		dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS_WITH_ISID;
298c66ac9dbSNicholas Bellinger 	}
299f2d30680SHannes Reinecke 	pr_debug("SCSI-2 Reserved %s LUN: %llu -> MAPPED LUN: %llu"
30030c7ca93SDavid Disseldorp 		" for %s\n", tpg->se_tpg_tfo->fabric_name,
30129a05deeSNicholas Bellinger 		cmd->se_lun->unpacked_lun, cmd->orig_fe_lun,
302c66ac9dbSNicholas Bellinger 		sess->se_node_acl->initiatorname);
303c66ac9dbSNicholas Bellinger 
304d29a5b6aSChristoph Hellwig out_unlock:
305d29a5b6aSChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
306d29a5b6aSChristoph Hellwig out:
3076bb35e00SChristoph Hellwig 	if (!ret)
3086bb35e00SChristoph Hellwig 		target_complete_cmd(cmd, GOOD);
309d29a5b6aSChristoph Hellwig 	return ret;
310c66ac9dbSNicholas Bellinger }
311c66ac9dbSNicholas Bellinger 
312c66ac9dbSNicholas Bellinger 
313c66ac9dbSNicholas Bellinger /*
314c66ac9dbSNicholas Bellinger  * Begin SPC-3/SPC-4 Persistent Reservations emulation support
315c66ac9dbSNicholas Bellinger  *
316c66ac9dbSNicholas Bellinger  * This function is called by those initiator ports who are *NOT*
317c66ac9dbSNicholas Bellinger  * the active PR reservation holder when a reservation is present.
318c66ac9dbSNicholas Bellinger  */
3199fcb57f3SChristoph Hellwig static int core_scsi3_pr_seq_non_holder(struct se_cmd *cmd, u32 pr_reg_type,
3209fcb57f3SChristoph Hellwig 					bool isid_mismatch)
321c66ac9dbSNicholas Bellinger {
322d977f437SChristoph Hellwig 	unsigned char *cdb = cmd->t_task_cdb;
323e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
32429a05deeSNicholas Bellinger 	struct se_node_acl *nacl = se_sess->se_node_acl;
3259fcb57f3SChristoph Hellwig 	int other_cdb = 0;
326c66ac9dbSNicholas Bellinger 	int registered_nexus = 0, ret = 1; /* Conflict by default */
327c66ac9dbSNicholas Bellinger 	int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
328c66ac9dbSNicholas Bellinger 	int we = 0; /* Write Exclusive */
329c66ac9dbSNicholas Bellinger 	int legacy = 0; /* Act like a legacy device and return
330c66ac9dbSNicholas Bellinger 			 * RESERVATION CONFLICT on some CDBs */
331c66ac9dbSNicholas Bellinger 
3329fcb57f3SChristoph Hellwig 	if (isid_mismatch) {
3339fcb57f3SChristoph Hellwig 		registered_nexus = 0;
3349fcb57f3SChristoph Hellwig 	} else {
3359fcb57f3SChristoph Hellwig 		struct se_dev_entry *se_deve;
336c66ac9dbSNicholas Bellinger 
33729a05deeSNicholas Bellinger 		rcu_read_lock();
33829a05deeSNicholas Bellinger 		se_deve = target_nacl_find_deve(nacl, cmd->orig_fe_lun);
33980bfdfa9SNicholas Bellinger 		if (se_deve)
3409fcb57f3SChristoph Hellwig 			registered_nexus = test_bit(DEF_PR_REG_ACTIVE,
3419fcb57f3SChristoph Hellwig 						    &se_deve->deve_flags);
34280bfdfa9SNicholas Bellinger 		rcu_read_unlock();
3439fcb57f3SChristoph Hellwig 	}
344c66ac9dbSNicholas Bellinger 
345c66ac9dbSNicholas Bellinger 	switch (pr_reg_type) {
346c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE:
347c66ac9dbSNicholas Bellinger 		we = 1;
348c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS:
349c66ac9dbSNicholas Bellinger 		/*
350c66ac9dbSNicholas Bellinger 		 * Some commands are only allowed for the persistent reservation
351c66ac9dbSNicholas Bellinger 		 * holder.
352c66ac9dbSNicholas Bellinger 		 */
353c66ac9dbSNicholas Bellinger 		break;
354c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
355c66ac9dbSNicholas Bellinger 		we = 1;
356d7e595ddSBart Van Assche 		/* fall through */
357c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
358c66ac9dbSNicholas Bellinger 		/*
359c66ac9dbSNicholas Bellinger 		 * Some commands are only allowed for registered I_T Nexuses.
360c66ac9dbSNicholas Bellinger 		 */
361c66ac9dbSNicholas Bellinger 		reg_only = 1;
362c66ac9dbSNicholas Bellinger 		break;
363c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
364c66ac9dbSNicholas Bellinger 		we = 1;
365d7e595ddSBart Van Assche 		/* fall through */
366c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
367c66ac9dbSNicholas Bellinger 		/*
368c66ac9dbSNicholas Bellinger 		 * Each registered I_T Nexus is a reservation holder.
369c66ac9dbSNicholas Bellinger 		 */
370c66ac9dbSNicholas Bellinger 		all_reg = 1;
371c66ac9dbSNicholas Bellinger 		break;
372c66ac9dbSNicholas Bellinger 	default:
373e3d6f909SAndy Grover 		return -EINVAL;
374c66ac9dbSNicholas Bellinger 	}
375c66ac9dbSNicholas Bellinger 	/*
376c66ac9dbSNicholas Bellinger 	 * Referenced from spc4r17 table 45 for *NON* PR holder access
377c66ac9dbSNicholas Bellinger 	 */
378c66ac9dbSNicholas Bellinger 	switch (cdb[0]) {
379c66ac9dbSNicholas Bellinger 	case SECURITY_PROTOCOL_IN:
380c66ac9dbSNicholas Bellinger 		if (registered_nexus)
381c66ac9dbSNicholas Bellinger 			return 0;
382c66ac9dbSNicholas Bellinger 		ret = (we) ? 0 : 1;
383c66ac9dbSNicholas Bellinger 		break;
384c66ac9dbSNicholas Bellinger 	case MODE_SENSE:
385c66ac9dbSNicholas Bellinger 	case MODE_SENSE_10:
386c66ac9dbSNicholas Bellinger 	case READ_ATTRIBUTE:
387c66ac9dbSNicholas Bellinger 	case READ_BUFFER:
388c66ac9dbSNicholas Bellinger 	case RECEIVE_DIAGNOSTIC:
389c66ac9dbSNicholas Bellinger 		if (legacy) {
390c66ac9dbSNicholas Bellinger 			ret = 1;
391c66ac9dbSNicholas Bellinger 			break;
392c66ac9dbSNicholas Bellinger 		}
393c66ac9dbSNicholas Bellinger 		if (registered_nexus) {
394c66ac9dbSNicholas Bellinger 			ret = 0;
395c66ac9dbSNicholas Bellinger 			break;
396c66ac9dbSNicholas Bellinger 		}
397c66ac9dbSNicholas Bellinger 		ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
398c66ac9dbSNicholas Bellinger 		break;
399c66ac9dbSNicholas Bellinger 	case PERSISTENT_RESERVE_OUT:
400c66ac9dbSNicholas Bellinger 		/*
401c66ac9dbSNicholas Bellinger 		 * This follows PERSISTENT_RESERVE_OUT service actions that
402c66ac9dbSNicholas Bellinger 		 * are allowed in the presence of various reservations.
403c66ac9dbSNicholas Bellinger 		 * See spc4r17, table 46
404c66ac9dbSNicholas Bellinger 		 */
405c66ac9dbSNicholas Bellinger 		switch (cdb[1] & 0x1f) {
406c66ac9dbSNicholas Bellinger 		case PRO_CLEAR:
407c66ac9dbSNicholas Bellinger 		case PRO_PREEMPT:
408c66ac9dbSNicholas Bellinger 		case PRO_PREEMPT_AND_ABORT:
409c66ac9dbSNicholas Bellinger 			ret = (registered_nexus) ? 0 : 1;
410c66ac9dbSNicholas Bellinger 			break;
411c66ac9dbSNicholas Bellinger 		case PRO_REGISTER:
412c66ac9dbSNicholas Bellinger 		case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
413c66ac9dbSNicholas Bellinger 			ret = 0;
414c66ac9dbSNicholas Bellinger 			break;
415c66ac9dbSNicholas Bellinger 		case PRO_REGISTER_AND_MOVE:
416c66ac9dbSNicholas Bellinger 		case PRO_RESERVE:
417c66ac9dbSNicholas Bellinger 			ret = 1;
418c66ac9dbSNicholas Bellinger 			break;
419c66ac9dbSNicholas Bellinger 		case PRO_RELEASE:
420c66ac9dbSNicholas Bellinger 			ret = (registered_nexus) ? 0 : 1;
421c66ac9dbSNicholas Bellinger 			break;
422c66ac9dbSNicholas Bellinger 		default:
4236708bb27SAndy Grover 			pr_err("Unknown PERSISTENT_RESERVE_OUT service"
424c66ac9dbSNicholas Bellinger 				" action: 0x%02x\n", cdb[1] & 0x1f);
425e3d6f909SAndy Grover 			return -EINVAL;
426c66ac9dbSNicholas Bellinger 		}
427c66ac9dbSNicholas Bellinger 		break;
428c66ac9dbSNicholas Bellinger 	case RELEASE:
429c66ac9dbSNicholas Bellinger 	case RELEASE_10:
430eacac00cSChristoph Hellwig 		/* Handled by CRH=1 in target_scsi2_reservation_release() */
431c66ac9dbSNicholas Bellinger 		ret = 0;
432c66ac9dbSNicholas Bellinger 		break;
433c66ac9dbSNicholas Bellinger 	case RESERVE:
434c66ac9dbSNicholas Bellinger 	case RESERVE_10:
435eacac00cSChristoph Hellwig 		/* Handled by CRH=1 in target_scsi2_reservation_reserve() */
436c66ac9dbSNicholas Bellinger 		ret = 0;
437c66ac9dbSNicholas Bellinger 		break;
438c66ac9dbSNicholas Bellinger 	case TEST_UNIT_READY:
439c66ac9dbSNicholas Bellinger 		ret = (legacy) ? 1 : 0; /* Conflict for legacy */
440c66ac9dbSNicholas Bellinger 		break;
441c66ac9dbSNicholas Bellinger 	case MAINTENANCE_IN:
442c66ac9dbSNicholas Bellinger 		switch (cdb[1] & 0x1f) {
443c66ac9dbSNicholas Bellinger 		case MI_MANAGEMENT_PROTOCOL_IN:
444c66ac9dbSNicholas Bellinger 			if (registered_nexus) {
445c66ac9dbSNicholas Bellinger 				ret = 0;
446c66ac9dbSNicholas Bellinger 				break;
447c66ac9dbSNicholas Bellinger 			}
448c66ac9dbSNicholas Bellinger 			ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
449c66ac9dbSNicholas Bellinger 			break;
450c66ac9dbSNicholas Bellinger 		case MI_REPORT_SUPPORTED_OPERATION_CODES:
451c66ac9dbSNicholas Bellinger 		case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
452c66ac9dbSNicholas Bellinger 			if (legacy) {
453c66ac9dbSNicholas Bellinger 				ret = 1;
454c66ac9dbSNicholas Bellinger 				break;
455c66ac9dbSNicholas Bellinger 			}
456c66ac9dbSNicholas Bellinger 			if (registered_nexus) {
457c66ac9dbSNicholas Bellinger 				ret = 0;
458c66ac9dbSNicholas Bellinger 				break;
459c66ac9dbSNicholas Bellinger 			}
460c66ac9dbSNicholas Bellinger 			ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
461c66ac9dbSNicholas Bellinger 			break;
462c66ac9dbSNicholas Bellinger 		case MI_REPORT_ALIASES:
463c66ac9dbSNicholas Bellinger 		case MI_REPORT_IDENTIFYING_INFORMATION:
464c66ac9dbSNicholas Bellinger 		case MI_REPORT_PRIORITY:
465c66ac9dbSNicholas Bellinger 		case MI_REPORT_TARGET_PGS:
466c66ac9dbSNicholas Bellinger 		case MI_REPORT_TIMESTAMP:
467c66ac9dbSNicholas Bellinger 			ret = 0; /* Allowed */
468c66ac9dbSNicholas Bellinger 			break;
469c66ac9dbSNicholas Bellinger 		default:
4706708bb27SAndy Grover 			pr_err("Unknown MI Service Action: 0x%02x\n",
471c66ac9dbSNicholas Bellinger 				(cdb[1] & 0x1f));
472e3d6f909SAndy Grover 			return -EINVAL;
473c66ac9dbSNicholas Bellinger 		}
474c66ac9dbSNicholas Bellinger 		break;
475c66ac9dbSNicholas Bellinger 	case ACCESS_CONTROL_IN:
476c66ac9dbSNicholas Bellinger 	case ACCESS_CONTROL_OUT:
477c66ac9dbSNicholas Bellinger 	case INQUIRY:
478c66ac9dbSNicholas Bellinger 	case LOG_SENSE:
47985686f69SHannes Reinecke 	case SERVICE_ACTION_IN_12:
480c66ac9dbSNicholas Bellinger 	case REPORT_LUNS:
481c66ac9dbSNicholas Bellinger 	case REQUEST_SENSE:
4826816966aSMarco Sanvido 	case PERSISTENT_RESERVE_IN:
483c66ac9dbSNicholas Bellinger 		ret = 0; /*/ Allowed CDBs */
484c66ac9dbSNicholas Bellinger 		break;
485c66ac9dbSNicholas Bellinger 	default:
486c66ac9dbSNicholas Bellinger 		other_cdb = 1;
487c66ac9dbSNicholas Bellinger 		break;
488c66ac9dbSNicholas Bellinger 	}
489c66ac9dbSNicholas Bellinger 	/*
49025985edcSLucas De Marchi 	 * Case where the CDB is explicitly allowed in the above switch
491c66ac9dbSNicholas Bellinger 	 * statement.
492c66ac9dbSNicholas Bellinger 	 */
4936708bb27SAndy Grover 	if (!ret && !other_cdb) {
494125d0119SHannes Reinecke 		pr_debug("Allowing explicit CDB: 0x%02x for %s"
495c66ac9dbSNicholas Bellinger 			" reservation holder\n", cdb[0],
496c66ac9dbSNicholas Bellinger 			core_scsi3_pr_dump_type(pr_reg_type));
4978b1e1244SAndy Grover 
498c66ac9dbSNicholas Bellinger 		return ret;
499c66ac9dbSNicholas Bellinger 	}
500c66ac9dbSNicholas Bellinger 	/*
501c66ac9dbSNicholas Bellinger 	 * Check if write exclusive initiator ports *NOT* holding the
502c66ac9dbSNicholas Bellinger 	 * WRITE_EXCLUSIVE_* reservation.
503c66ac9dbSNicholas Bellinger 	 */
504ee1b1b9cSAndy Grover 	if (we && !registered_nexus) {
505c66ac9dbSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE) {
506c66ac9dbSNicholas Bellinger 			/*
507c66ac9dbSNicholas Bellinger 			 * Conflict for write exclusive
508c66ac9dbSNicholas Bellinger 			 */
5096708bb27SAndy Grover 			pr_debug("%s Conflict for unregistered nexus"
510c66ac9dbSNicholas Bellinger 				" %s CDB: 0x%02x to %s reservation\n",
511c66ac9dbSNicholas Bellinger 				transport_dump_cmd_direction(cmd),
512c66ac9dbSNicholas Bellinger 				se_sess->se_node_acl->initiatorname, cdb[0],
513c66ac9dbSNicholas Bellinger 				core_scsi3_pr_dump_type(pr_reg_type));
514c66ac9dbSNicholas Bellinger 			return 1;
515c66ac9dbSNicholas Bellinger 		} else {
516c66ac9dbSNicholas Bellinger 			/*
517c66ac9dbSNicholas Bellinger 			 * Allow non WRITE CDBs for all Write Exclusive
518c66ac9dbSNicholas Bellinger 			 * PR TYPEs to pass for registered and
519c66ac9dbSNicholas Bellinger 			 * non-registered_nexuxes NOT holding the reservation.
520c66ac9dbSNicholas Bellinger 			 *
521c66ac9dbSNicholas Bellinger 			 * We only make noise for the unregisterd nexuses,
522c66ac9dbSNicholas Bellinger 			 * as we expect registered non-reservation holding
523c66ac9dbSNicholas Bellinger 			 * nexuses to issue CDBs.
524c66ac9dbSNicholas Bellinger 			 */
5258b1e1244SAndy Grover 
5266708bb27SAndy Grover 			if (!registered_nexus) {
527125d0119SHannes Reinecke 				pr_debug("Allowing implicit CDB: 0x%02x"
528c66ac9dbSNicholas Bellinger 					" for %s reservation on unregistered"
529c66ac9dbSNicholas Bellinger 					" nexus\n", cdb[0],
530c66ac9dbSNicholas Bellinger 					core_scsi3_pr_dump_type(pr_reg_type));
531c66ac9dbSNicholas Bellinger 			}
5328b1e1244SAndy Grover 
533c66ac9dbSNicholas Bellinger 			return 0;
534c66ac9dbSNicholas Bellinger 		}
535c66ac9dbSNicholas Bellinger 	} else if ((reg_only) || (all_reg)) {
536c66ac9dbSNicholas Bellinger 		if (registered_nexus) {
537c66ac9dbSNicholas Bellinger 			/*
538c66ac9dbSNicholas Bellinger 			 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
539c66ac9dbSNicholas Bellinger 			 * allow commands from registered nexuses.
540c66ac9dbSNicholas Bellinger 			 */
5418b1e1244SAndy Grover 
542125d0119SHannes Reinecke 			pr_debug("Allowing implicit CDB: 0x%02x for %s"
543c66ac9dbSNicholas Bellinger 				" reservation\n", cdb[0],
544c66ac9dbSNicholas Bellinger 				core_scsi3_pr_dump_type(pr_reg_type));
5458b1e1244SAndy Grover 
546c66ac9dbSNicholas Bellinger 			return 0;
547c66ac9dbSNicholas Bellinger 		}
5481ecc7586SLee Duncan        } else if (we && registered_nexus) {
5491ecc7586SLee Duncan                /*
5501ecc7586SLee Duncan                 * Reads are allowed for Write Exclusive locks
5511ecc7586SLee Duncan                 * from all registrants.
5521ecc7586SLee Duncan                 */
5531ecc7586SLee Duncan                if (cmd->data_direction == DMA_FROM_DEVICE) {
5541ecc7586SLee Duncan                        pr_debug("Allowing READ CDB: 0x%02x for %s"
5551ecc7586SLee Duncan                                " reservation\n", cdb[0],
5561ecc7586SLee Duncan                                core_scsi3_pr_dump_type(pr_reg_type));
5571ecc7586SLee Duncan 
5581ecc7586SLee Duncan                        return 0;
5591ecc7586SLee Duncan                }
560c66ac9dbSNicholas Bellinger 	}
5616708bb27SAndy Grover 	pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
562c66ac9dbSNicholas Bellinger 		" for %s reservation\n", transport_dump_cmd_direction(cmd),
563c66ac9dbSNicholas Bellinger 		(registered_nexus) ? "" : "un",
564c66ac9dbSNicholas Bellinger 		se_sess->se_node_acl->initiatorname, cdb[0],
565c66ac9dbSNicholas Bellinger 		core_scsi3_pr_dump_type(pr_reg_type));
566c66ac9dbSNicholas Bellinger 
567c66ac9dbSNicholas Bellinger 	return 1; /* Conflict by default */
568c66ac9dbSNicholas Bellinger }
569c66ac9dbSNicholas Bellinger 
570de103c93SChristoph Hellwig static sense_reason_t
571de103c93SChristoph Hellwig target_scsi3_pr_reservation_check(struct se_cmd *cmd)
572d977f437SChristoph Hellwig {
573d977f437SChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
574d977f437SChristoph Hellwig 	struct se_session *sess = cmd->se_sess;
575d977f437SChristoph Hellwig 	u32 pr_reg_type;
5769fcb57f3SChristoph Hellwig 	bool isid_mismatch = false;
577d977f437SChristoph Hellwig 
578d977f437SChristoph Hellwig 	if (!dev->dev_pr_res_holder)
579d977f437SChristoph Hellwig 		return 0;
580d977f437SChristoph Hellwig 
581d977f437SChristoph Hellwig 	pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
582d977f437SChristoph Hellwig 	cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
583d977f437SChristoph Hellwig 	if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl)
584d977f437SChristoph Hellwig 		goto check_nonholder;
585d977f437SChristoph Hellwig 
586d977f437SChristoph Hellwig 	if (dev->dev_pr_res_holder->isid_present_at_reg) {
587d977f437SChristoph Hellwig 		if (dev->dev_pr_res_holder->pr_reg_bin_isid !=
588d977f437SChristoph Hellwig 		    sess->sess_bin_isid) {
5899fcb57f3SChristoph Hellwig 			isid_mismatch = true;
590d977f437SChristoph Hellwig 			goto check_nonholder;
591d977f437SChristoph Hellwig 		}
592d977f437SChristoph Hellwig 	}
593d977f437SChristoph Hellwig 
594d977f437SChristoph Hellwig 	return 0;
595d977f437SChristoph Hellwig 
596d977f437SChristoph Hellwig check_nonholder:
5979fcb57f3SChristoph Hellwig 	if (core_scsi3_pr_seq_non_holder(cmd, pr_reg_type, isid_mismatch))
598de103c93SChristoph Hellwig 		return TCM_RESERVATION_CONFLICT;
599d977f437SChristoph Hellwig 	return 0;
600d977f437SChristoph Hellwig }
601d977f437SChristoph Hellwig 
602c66ac9dbSNicholas Bellinger static u32 core_scsi3_pr_generation(struct se_device *dev)
603c66ac9dbSNicholas Bellinger {
604c66ac9dbSNicholas Bellinger 	u32 prg;
6050fd97ccfSChristoph Hellwig 
606c66ac9dbSNicholas Bellinger 	/*
607c66ac9dbSNicholas Bellinger 	 * PRGeneration field shall contain the value of a 32-bit wrapping
608c66ac9dbSNicholas Bellinger 	 * counter mainted by the device server.
609c66ac9dbSNicholas Bellinger 	 *
610c66ac9dbSNicholas Bellinger 	 * Note that this is done regardless of Active Persist across
611c66ac9dbSNicholas Bellinger 	 * Target PowerLoss (APTPL)
612c66ac9dbSNicholas Bellinger 	 *
613c66ac9dbSNicholas Bellinger 	 * See spc4r17 section 6.3.12 READ_KEYS service action
614c66ac9dbSNicholas Bellinger 	 */
615c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
6160fd97ccfSChristoph Hellwig 	prg = dev->t10_pr.pr_generation++;
617c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
618c66ac9dbSNicholas Bellinger 
619c66ac9dbSNicholas Bellinger 	return prg;
620c66ac9dbSNicholas Bellinger }
621c66ac9dbSNicholas Bellinger 
622c66ac9dbSNicholas Bellinger static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
623c66ac9dbSNicholas Bellinger 	struct se_device *dev,
624c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
62579dc9c9eSNicholas Bellinger 	struct se_lun *lun,
6263ccd6e83SNicholas Bellinger 	struct se_dev_entry *dest_deve,
627f2d30680SHannes Reinecke 	u64 mapped_lun,
628c66ac9dbSNicholas Bellinger 	unsigned char *isid,
629c66ac9dbSNicholas Bellinger 	u64 sa_res_key,
630c66ac9dbSNicholas Bellinger 	int all_tg_pt,
631c66ac9dbSNicholas Bellinger 	int aptpl)
632c66ac9dbSNicholas Bellinger {
633c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
634c66ac9dbSNicholas Bellinger 
635c66ac9dbSNicholas Bellinger 	pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
6366708bb27SAndy Grover 	if (!pr_reg) {
6376708bb27SAndy Grover 		pr_err("Unable to allocate struct t10_pr_registration\n");
638c66ac9dbSNicholas Bellinger 		return NULL;
639c66ac9dbSNicholas Bellinger 	}
640c66ac9dbSNicholas Bellinger 
641c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_list);
642c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
643c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
644c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
645c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
646c66ac9dbSNicholas Bellinger 	atomic_set(&pr_reg->pr_res_holders, 0);
647c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_nacl = nacl;
6483ccd6e83SNicholas Bellinger 	/*
6493ccd6e83SNicholas Bellinger 	 * For destination registrations for ALL_TG_PT=1 and SPEC_I_PT=1,
6503ccd6e83SNicholas Bellinger 	 * the se_dev_entry->pr_ref will have been already obtained by
6513ccd6e83SNicholas Bellinger 	 * core_get_se_deve_from_rtpi() or __core_scsi3_alloc_registration().
6523ccd6e83SNicholas Bellinger 	 *
6533ccd6e83SNicholas Bellinger 	 * Otherwise, locate se_dev_entry now and obtain a reference until
6543ccd6e83SNicholas Bellinger 	 * registration completes in __core_scsi3_add_registration().
6553ccd6e83SNicholas Bellinger 	 */
6563ccd6e83SNicholas Bellinger 	if (dest_deve) {
6573ccd6e83SNicholas Bellinger 		pr_reg->pr_reg_deve = dest_deve;
6583ccd6e83SNicholas Bellinger 	} else {
6593ccd6e83SNicholas Bellinger 		rcu_read_lock();
6603ccd6e83SNicholas Bellinger 		pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
6613ccd6e83SNicholas Bellinger 		if (!pr_reg->pr_reg_deve) {
6623ccd6e83SNicholas Bellinger 			rcu_read_unlock();
6633ccd6e83SNicholas Bellinger 			pr_err("Unable to locate PR deve %s mapped_lun: %llu\n",
6643ccd6e83SNicholas Bellinger 				nacl->initiatorname, mapped_lun);
6653ccd6e83SNicholas Bellinger 			kmem_cache_free(t10_pr_reg_cache, pr_reg);
6663ccd6e83SNicholas Bellinger 			return NULL;
6673ccd6e83SNicholas Bellinger 		}
6683ccd6e83SNicholas Bellinger 		kref_get(&pr_reg->pr_reg_deve->pr_kref);
6693ccd6e83SNicholas Bellinger 		rcu_read_unlock();
6703ccd6e83SNicholas Bellinger 	}
67179dc9c9eSNicholas Bellinger 	pr_reg->pr_res_mapped_lun = mapped_lun;
67279dc9c9eSNicholas Bellinger 	pr_reg->pr_aptpl_target_lun = lun->unpacked_lun;
673adf653f9SChristoph Hellwig 	pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi;
674c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_key = sa_res_key;
675c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_all_tg_pt = all_tg_pt;
676c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_aptpl = aptpl;
677c66ac9dbSNicholas Bellinger 	/*
678c66ac9dbSNicholas Bellinger 	 * If an ISID value for this SCSI Initiator Port exists,
679c66ac9dbSNicholas Bellinger 	 * save it to the registration now.
680c66ac9dbSNicholas Bellinger 	 */
681c66ac9dbSNicholas Bellinger 	if (isid != NULL) {
682c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
683c66ac9dbSNicholas Bellinger 		snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
684c66ac9dbSNicholas Bellinger 		pr_reg->isid_present_at_reg = 1;
685c66ac9dbSNicholas Bellinger 	}
686c66ac9dbSNicholas Bellinger 
687c66ac9dbSNicholas Bellinger 	return pr_reg;
688c66ac9dbSNicholas Bellinger }
689c66ac9dbSNicholas Bellinger 
690c66ac9dbSNicholas Bellinger static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
691c66ac9dbSNicholas Bellinger static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
692c66ac9dbSNicholas Bellinger 
693c66ac9dbSNicholas Bellinger /*
694c66ac9dbSNicholas Bellinger  * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
695c66ac9dbSNicholas Bellinger  * modes.
696c66ac9dbSNicholas Bellinger  */
697c66ac9dbSNicholas Bellinger static struct t10_pr_registration *__core_scsi3_alloc_registration(
698c66ac9dbSNicholas Bellinger 	struct se_device *dev,
699c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
70079dc9c9eSNicholas Bellinger 	struct se_lun *lun,
701c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve,
702f2d30680SHannes Reinecke 	u64 mapped_lun,
703c66ac9dbSNicholas Bellinger 	unsigned char *isid,
704c66ac9dbSNicholas Bellinger 	u64 sa_res_key,
705c66ac9dbSNicholas Bellinger 	int all_tg_pt,
706c66ac9dbSNicholas Bellinger 	int aptpl)
707c66ac9dbSNicholas Bellinger {
708c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve_tmp;
709c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl_tmp;
71079dc9c9eSNicholas Bellinger 	struct se_lun_acl *lacl_tmp;
711adf653f9SChristoph Hellwig 	struct se_lun *lun_tmp, *next, *dest_lun;
7129ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
713c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
714c66ac9dbSNicholas Bellinger 	int ret;
715c66ac9dbSNicholas Bellinger 	/*
716c66ac9dbSNicholas Bellinger 	 * Create a registration for the I_T Nexus upon which the
717c66ac9dbSNicholas Bellinger 	 * PROUT REGISTER was received.
718c66ac9dbSNicholas Bellinger 	 */
71979dc9c9eSNicholas Bellinger 	pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, lun, deve, mapped_lun,
72079dc9c9eSNicholas Bellinger 						    isid, sa_res_key, all_tg_pt,
72179dc9c9eSNicholas Bellinger 						    aptpl);
7226708bb27SAndy Grover 	if (!pr_reg)
723c66ac9dbSNicholas Bellinger 		return NULL;
724c66ac9dbSNicholas Bellinger 	/*
725c66ac9dbSNicholas Bellinger 	 * Return pointer to pr_reg for ALL_TG_PT=0
726c66ac9dbSNicholas Bellinger 	 */
7276708bb27SAndy Grover 	if (!all_tg_pt)
728c66ac9dbSNicholas Bellinger 		return pr_reg;
729c66ac9dbSNicholas Bellinger 	/*
730c66ac9dbSNicholas Bellinger 	 * Create list of matching SCSI Initiator Port registrations
731c66ac9dbSNicholas Bellinger 	 * for ALL_TG_PT=1
732c66ac9dbSNicholas Bellinger 	 */
733c66ac9dbSNicholas Bellinger 	spin_lock(&dev->se_port_lock);
734adf653f9SChristoph Hellwig 	list_for_each_entry_safe(lun_tmp, next, &dev->dev_sep_list, lun_dev_link) {
7359e37d042SNicholas Bellinger 		if (!percpu_ref_tryget_live(&lun_tmp->lun_ref))
7369e37d042SNicholas Bellinger 			continue;
737c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->se_port_lock);
738c66ac9dbSNicholas Bellinger 
7391adff1b3SNicholas Bellinger 		spin_lock(&lun_tmp->lun_deve_lock);
740adf653f9SChristoph Hellwig 		list_for_each_entry(deve_tmp, &lun_tmp->lun_deve_list, lun_link) {
741c66ac9dbSNicholas Bellinger 			/*
742c66ac9dbSNicholas Bellinger 			 * This pointer will be NULL for demo mode MappedLUNs
743125d0119SHannes Reinecke 			 * that have not been make explicit via a ConfigFS
744c66ac9dbSNicholas Bellinger 			 * MappedLUN group for the SCSI Initiator Node ACL.
745c66ac9dbSNicholas Bellinger 			 */
7466708bb27SAndy Grover 			if (!deve_tmp->se_lun_acl)
747c66ac9dbSNicholas Bellinger 				continue;
748c66ac9dbSNicholas Bellinger 
74979dc9c9eSNicholas Bellinger 			lacl_tmp = rcu_dereference_check(deve_tmp->se_lun_acl,
750adf653f9SChristoph Hellwig 						lockdep_is_held(&lun_tmp->lun_deve_lock));
75179dc9c9eSNicholas Bellinger 			nacl_tmp = lacl_tmp->se_lun_nacl;
752c66ac9dbSNicholas Bellinger 			/*
753c66ac9dbSNicholas Bellinger 			 * Skip the matching struct se_node_acl that is allocated
754c66ac9dbSNicholas Bellinger 			 * above..
755c66ac9dbSNicholas Bellinger 			 */
756c66ac9dbSNicholas Bellinger 			if (nacl == nacl_tmp)
757c66ac9dbSNicholas Bellinger 				continue;
758c66ac9dbSNicholas Bellinger 			/*
759c66ac9dbSNicholas Bellinger 			 * Only perform PR registrations for target ports on
760c66ac9dbSNicholas Bellinger 			 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
761c66ac9dbSNicholas Bellinger 			 * arrived.
762c66ac9dbSNicholas Bellinger 			 */
763c66ac9dbSNicholas Bellinger 			if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
764c66ac9dbSNicholas Bellinger 				continue;
765c66ac9dbSNicholas Bellinger 			/*
766c66ac9dbSNicholas Bellinger 			 * Look for a matching Initiator Node ACL in ASCII format
767c66ac9dbSNicholas Bellinger 			 */
768c66ac9dbSNicholas Bellinger 			if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
769c66ac9dbSNicholas Bellinger 				continue;
770c66ac9dbSNicholas Bellinger 
77129a05deeSNicholas Bellinger 			kref_get(&deve_tmp->pr_kref);
7721adff1b3SNicholas Bellinger 			spin_unlock(&lun_tmp->lun_deve_lock);
773c66ac9dbSNicholas Bellinger 			/*
774c66ac9dbSNicholas Bellinger 			 * Grab a configfs group dependency that is released
775c66ac9dbSNicholas Bellinger 			 * for the exception path at label out: below, or upon
776c66ac9dbSNicholas Bellinger 			 * completion of adding ALL_TG_PT=1 registrations in
777c66ac9dbSNicholas Bellinger 			 * __core_scsi3_add_registration()
778c66ac9dbSNicholas Bellinger 			 */
779c66ac9dbSNicholas Bellinger 			ret = core_scsi3_lunacl_depend_item(deve_tmp);
780c66ac9dbSNicholas Bellinger 			if (ret < 0) {
7816708bb27SAndy Grover 				pr_err("core_scsi3_lunacl_depend"
782c66ac9dbSNicholas Bellinger 						"_item() failed\n");
7839e37d042SNicholas Bellinger 				percpu_ref_put(&lun_tmp->lun_ref);
78429a05deeSNicholas Bellinger 				kref_put(&deve_tmp->pr_kref, target_pr_kref_release);
785c66ac9dbSNicholas Bellinger 				goto out;
786c66ac9dbSNicholas Bellinger 			}
787c66ac9dbSNicholas Bellinger 			/*
788c66ac9dbSNicholas Bellinger 			 * Located a matching SCSI Initiator Port on a different
789c66ac9dbSNicholas Bellinger 			 * port, allocate the pr_reg_atp and attach it to the
790c66ac9dbSNicholas Bellinger 			 * pr_reg->pr_reg_atp_list that will be processed once
791c66ac9dbSNicholas Bellinger 			 * the original *pr_reg is processed in
792c66ac9dbSNicholas Bellinger 			 * __core_scsi3_add_registration()
793c66ac9dbSNicholas Bellinger 			 */
794adf653f9SChristoph Hellwig 			dest_lun = rcu_dereference_check(deve_tmp->se_lun,
7952c935bc5SPeter Zijlstra 				kref_read(&deve_tmp->pr_kref) != 0);
79679dc9c9eSNicholas Bellinger 
797c66ac9dbSNicholas Bellinger 			pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
798adf653f9SChristoph Hellwig 						nacl_tmp, dest_lun, deve_tmp,
79979dc9c9eSNicholas Bellinger 						deve_tmp->mapped_lun, NULL,
800c66ac9dbSNicholas Bellinger 						sa_res_key, all_tg_pt, aptpl);
8016708bb27SAndy Grover 			if (!pr_reg_atp) {
8029e37d042SNicholas Bellinger 				percpu_ref_put(&lun_tmp->lun_ref);
803c66ac9dbSNicholas Bellinger 				core_scsi3_lunacl_undepend_item(deve_tmp);
804c66ac9dbSNicholas Bellinger 				goto out;
805c66ac9dbSNicholas Bellinger 			}
806c66ac9dbSNicholas Bellinger 
807c66ac9dbSNicholas Bellinger 			list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
808c66ac9dbSNicholas Bellinger 				      &pr_reg->pr_reg_atp_list);
8091adff1b3SNicholas Bellinger 			spin_lock(&lun_tmp->lun_deve_lock);
810c66ac9dbSNicholas Bellinger 		}
8111adff1b3SNicholas Bellinger 		spin_unlock(&lun_tmp->lun_deve_lock);
812c66ac9dbSNicholas Bellinger 
813c66ac9dbSNicholas Bellinger 		spin_lock(&dev->se_port_lock);
8149e37d042SNicholas Bellinger 		percpu_ref_put(&lun_tmp->lun_ref);
815c66ac9dbSNicholas Bellinger 	}
816c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->se_port_lock);
817c66ac9dbSNicholas Bellinger 
818c66ac9dbSNicholas Bellinger 	return pr_reg;
819c66ac9dbSNicholas Bellinger out:
820c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
821c66ac9dbSNicholas Bellinger 			&pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
822c66ac9dbSNicholas Bellinger 		list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
823c66ac9dbSNicholas Bellinger 		core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
824c66ac9dbSNicholas Bellinger 		kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
825c66ac9dbSNicholas Bellinger 	}
826c66ac9dbSNicholas Bellinger 	kmem_cache_free(t10_pr_reg_cache, pr_reg);
827c66ac9dbSNicholas Bellinger 	return NULL;
828c66ac9dbSNicholas Bellinger }
829c66ac9dbSNicholas Bellinger 
830c66ac9dbSNicholas Bellinger int core_scsi3_alloc_aptpl_registration(
831e3d6f909SAndy Grover 	struct t10_reservation *pr_tmpl,
832c66ac9dbSNicholas Bellinger 	u64 sa_res_key,
833c66ac9dbSNicholas Bellinger 	unsigned char *i_port,
834c66ac9dbSNicholas Bellinger 	unsigned char *isid,
835f2d30680SHannes Reinecke 	u64 mapped_lun,
836c66ac9dbSNicholas Bellinger 	unsigned char *t_port,
837c66ac9dbSNicholas Bellinger 	u16 tpgt,
838f2d30680SHannes Reinecke 	u64 target_lun,
839c66ac9dbSNicholas Bellinger 	int res_holder,
840c66ac9dbSNicholas Bellinger 	int all_tg_pt,
841c66ac9dbSNicholas Bellinger 	u8 type)
842c66ac9dbSNicholas Bellinger {
843c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
844c66ac9dbSNicholas Bellinger 
8456708bb27SAndy Grover 	if (!i_port || !t_port || !sa_res_key) {
8466708bb27SAndy Grover 		pr_err("Illegal parameters for APTPL registration\n");
847e3d6f909SAndy Grover 		return -EINVAL;
848c66ac9dbSNicholas Bellinger 	}
849c66ac9dbSNicholas Bellinger 
850c66ac9dbSNicholas Bellinger 	pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
8516708bb27SAndy Grover 	if (!pr_reg) {
8526708bb27SAndy Grover 		pr_err("Unable to allocate struct t10_pr_registration\n");
853e3d6f909SAndy Grover 		return -ENOMEM;
854c66ac9dbSNicholas Bellinger 	}
855c66ac9dbSNicholas Bellinger 
856c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_list);
857c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
858c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
859c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
860c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
861c66ac9dbSNicholas Bellinger 	atomic_set(&pr_reg->pr_res_holders, 0);
862c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_nacl = NULL;
863c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_deve = NULL;
864c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_mapped_lun = mapped_lun;
865c66ac9dbSNicholas Bellinger 	pr_reg->pr_aptpl_target_lun = target_lun;
866c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_key = sa_res_key;
867c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_all_tg_pt = all_tg_pt;
868c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_aptpl = 1;
869c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
870c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_type = type;
871c66ac9dbSNicholas Bellinger 	/*
872c66ac9dbSNicholas Bellinger 	 * If an ISID value had been saved in APTPL metadata for this
873c66ac9dbSNicholas Bellinger 	 * SCSI Initiator Port, restore it now.
874c66ac9dbSNicholas Bellinger 	 */
875c66ac9dbSNicholas Bellinger 	if (isid != NULL) {
876c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
877c66ac9dbSNicholas Bellinger 		snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
878c66ac9dbSNicholas Bellinger 		pr_reg->isid_present_at_reg = 1;
879c66ac9dbSNicholas Bellinger 	}
880c66ac9dbSNicholas Bellinger 	/*
881c66ac9dbSNicholas Bellinger 	 * Copy the i_port and t_port information from caller.
882c66ac9dbSNicholas Bellinger 	 */
883c66ac9dbSNicholas Bellinger 	snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
884c66ac9dbSNicholas Bellinger 	snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
885c66ac9dbSNicholas Bellinger 	pr_reg->pr_reg_tpgt = tpgt;
886c66ac9dbSNicholas Bellinger 	/*
887c66ac9dbSNicholas Bellinger 	 * Set pr_res_holder from caller, the pr_reg who is the reservation
888c66ac9dbSNicholas Bellinger 	 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
889c66ac9dbSNicholas Bellinger 	 * the Initiator Node LUN ACL from the fabric module is created for
890c66ac9dbSNicholas Bellinger 	 * this registration.
891c66ac9dbSNicholas Bellinger 	 */
892c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_holder = res_holder;
893c66ac9dbSNicholas Bellinger 
894c66ac9dbSNicholas Bellinger 	list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
8956708bb27SAndy Grover 	pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
896c66ac9dbSNicholas Bellinger 			" metadata\n", (res_holder) ? "+reservation" : "");
897c66ac9dbSNicholas Bellinger 	return 0;
898c66ac9dbSNicholas Bellinger }
899c66ac9dbSNicholas Bellinger 
900c66ac9dbSNicholas Bellinger static void core_scsi3_aptpl_reserve(
901c66ac9dbSNicholas Bellinger 	struct se_device *dev,
902c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
903c66ac9dbSNicholas Bellinger 	struct se_node_acl *node_acl,
904c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg)
905c66ac9dbSNicholas Bellinger {
906c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
907c66ac9dbSNicholas Bellinger 
908c66ac9dbSNicholas Bellinger 	memset(i_buf, 0, PR_REG_ISID_ID_LEN);
909d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
910c66ac9dbSNicholas Bellinger 
911c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
912c66ac9dbSNicholas Bellinger 	dev->dev_pr_res_holder = pr_reg;
913c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
914c66ac9dbSNicholas Bellinger 
9156708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
916c66ac9dbSNicholas Bellinger 		" new reservation holder TYPE: %s ALL_TG_PT: %d\n",
91730c7ca93SDavid Disseldorp 		tpg->se_tpg_tfo->fabric_name,
918c66ac9dbSNicholas Bellinger 		core_scsi3_pr_dump_type(pr_reg->pr_res_type),
919c66ac9dbSNicholas Bellinger 		(pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
9206708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
92130c7ca93SDavid Disseldorp 		tpg->se_tpg_tfo->fabric_name, node_acl->initiatorname,
922d2843c17SAndy Grover 		i_buf);
923c66ac9dbSNicholas Bellinger }
924c66ac9dbSNicholas Bellinger 
925c66ac9dbSNicholas Bellinger static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
92633ce6a87SAndy Grover 				struct t10_pr_registration *, enum register_type, int);
927c66ac9dbSNicholas Bellinger 
928c66ac9dbSNicholas Bellinger static int __core_scsi3_check_aptpl_registration(
929c66ac9dbSNicholas Bellinger 	struct se_device *dev,
930c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
931c66ac9dbSNicholas Bellinger 	struct se_lun *lun,
932f2d30680SHannes Reinecke 	u64 target_lun,
933c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
934f2d30680SHannes Reinecke 	u64 mapped_lun)
935c66ac9dbSNicholas Bellinger {
936c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp;
9370fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
938c66ac9dbSNicholas Bellinger 	unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
939c66ac9dbSNicholas Bellinger 	unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
940c66ac9dbSNicholas Bellinger 	u16 tpgt;
941c66ac9dbSNicholas Bellinger 
942c66ac9dbSNicholas Bellinger 	memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
943c66ac9dbSNicholas Bellinger 	memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
944c66ac9dbSNicholas Bellinger 	/*
945c66ac9dbSNicholas Bellinger 	 * Copy Initiator Port information from struct se_node_acl
946c66ac9dbSNicholas Bellinger 	 */
947c66ac9dbSNicholas Bellinger 	snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
948c66ac9dbSNicholas Bellinger 	snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
949e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_wwn(tpg));
950e3d6f909SAndy Grover 	tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
951c66ac9dbSNicholas Bellinger 	/*
952c66ac9dbSNicholas Bellinger 	 * Look for the matching registrations+reservation from those
953c66ac9dbSNicholas Bellinger 	 * created from APTPL metadata.  Note that multiple registrations
954c66ac9dbSNicholas Bellinger 	 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
955c66ac9dbSNicholas Bellinger 	 * TransportIDs.
956c66ac9dbSNicholas Bellinger 	 */
957c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->aptpl_reg_lock);
958c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
959c66ac9dbSNicholas Bellinger 				pr_reg_aptpl_list) {
96092404e60SNicholas Bellinger 
9616708bb27SAndy Grover 		if (!strcmp(pr_reg->pr_iport, i_port) &&
96229a05deeSNicholas Bellinger 		     (pr_reg->pr_res_mapped_lun == mapped_lun) &&
963c66ac9dbSNicholas Bellinger 		    !(strcmp(pr_reg->pr_tport, t_port)) &&
964c66ac9dbSNicholas Bellinger 		     (pr_reg->pr_reg_tpgt == tpgt) &&
965c66ac9dbSNicholas Bellinger 		     (pr_reg->pr_aptpl_target_lun == target_lun)) {
9663ccd6e83SNicholas Bellinger 			/*
9673ccd6e83SNicholas Bellinger 			 * Obtain the ->pr_reg_deve pointer + reference, that
9683ccd6e83SNicholas Bellinger 			 * is released by __core_scsi3_add_registration() below.
9693ccd6e83SNicholas Bellinger 			 */
9703ccd6e83SNicholas Bellinger 			rcu_read_lock();
9713ccd6e83SNicholas Bellinger 			pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
9723ccd6e83SNicholas Bellinger 			if (!pr_reg->pr_reg_deve) {
9733ccd6e83SNicholas Bellinger 				pr_err("Unable to locate PR APTPL %s mapped_lun:"
9743ccd6e83SNicholas Bellinger 					" %llu\n", nacl->initiatorname, mapped_lun);
9753ccd6e83SNicholas Bellinger 				rcu_read_unlock();
9763ccd6e83SNicholas Bellinger 				continue;
9773ccd6e83SNicholas Bellinger 			}
9783ccd6e83SNicholas Bellinger 			kref_get(&pr_reg->pr_reg_deve->pr_kref);
9793ccd6e83SNicholas Bellinger 			rcu_read_unlock();
980c66ac9dbSNicholas Bellinger 
981c66ac9dbSNicholas Bellinger 			pr_reg->pr_reg_nacl = nacl;
982adf653f9SChristoph Hellwig 			pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi;
983c66ac9dbSNicholas Bellinger 			list_del(&pr_reg->pr_reg_aptpl_list);
984c66ac9dbSNicholas Bellinger 			spin_unlock(&pr_tmpl->aptpl_reg_lock);
985c66ac9dbSNicholas Bellinger 			/*
986c66ac9dbSNicholas Bellinger 			 * At this point all of the pointers in *pr_reg will
987c66ac9dbSNicholas Bellinger 			 * be setup, so go ahead and add the registration.
988c66ac9dbSNicholas Bellinger 			 */
989c66ac9dbSNicholas Bellinger 			__core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
990c66ac9dbSNicholas Bellinger 			/*
991c66ac9dbSNicholas Bellinger 			 * If this registration is the reservation holder,
992c66ac9dbSNicholas Bellinger 			 * make that happen now..
993c66ac9dbSNicholas Bellinger 			 */
994c66ac9dbSNicholas Bellinger 			if (pr_reg->pr_res_holder)
995c66ac9dbSNicholas Bellinger 				core_scsi3_aptpl_reserve(dev, tpg,
996c66ac9dbSNicholas Bellinger 						nacl, pr_reg);
997c66ac9dbSNicholas Bellinger 			/*
998c66ac9dbSNicholas Bellinger 			 * Reenable pr_aptpl_active to accept new metadata
999c66ac9dbSNicholas Bellinger 			 * updates once the SCSI device is active again..
1000c66ac9dbSNicholas Bellinger 			 */
1001c66ac9dbSNicholas Bellinger 			spin_lock(&pr_tmpl->aptpl_reg_lock);
1002c66ac9dbSNicholas Bellinger 			pr_tmpl->pr_aptpl_active = 1;
1003c66ac9dbSNicholas Bellinger 		}
1004c66ac9dbSNicholas Bellinger 	}
1005c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->aptpl_reg_lock);
1006c66ac9dbSNicholas Bellinger 
1007c66ac9dbSNicholas Bellinger 	return 0;
1008c66ac9dbSNicholas Bellinger }
1009c66ac9dbSNicholas Bellinger 
1010c66ac9dbSNicholas Bellinger int core_scsi3_check_aptpl_registration(
1011c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1012c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
1013c66ac9dbSNicholas Bellinger 	struct se_lun *lun,
1014e2480563SNicholas Bellinger 	struct se_node_acl *nacl,
1015f2d30680SHannes Reinecke 	u64 mapped_lun)
1016c66ac9dbSNicholas Bellinger {
1017d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1018c66ac9dbSNicholas Bellinger 		return 0;
1019c66ac9dbSNicholas Bellinger 
1020c66ac9dbSNicholas Bellinger 	return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
102129a05deeSNicholas Bellinger 						     lun->unpacked_lun, nacl,
102229a05deeSNicholas Bellinger 						     mapped_lun);
1023c66ac9dbSNicholas Bellinger }
1024c66ac9dbSNicholas Bellinger 
1025c66ac9dbSNicholas Bellinger static void __core_scsi3_dump_registration(
10269ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo,
1027c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1028c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
1029c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg,
103033ce6a87SAndy Grover 	enum register_type register_type)
1031c66ac9dbSNicholas Bellinger {
1032c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg = nacl->se_tpg;
1033c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
1034c66ac9dbSNicholas Bellinger 
1035c66ac9dbSNicholas Bellinger 	memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
1036d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1037c66ac9dbSNicholas Bellinger 
10386708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
103930c7ca93SDavid Disseldorp 		" Node: %s%s\n", tfo->fabric_name, (register_type == REGISTER_AND_MOVE) ?
104033ce6a87SAndy Grover 		"_AND_MOVE" : (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ?
1041c66ac9dbSNicholas Bellinger 		"_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
1042d2843c17SAndy Grover 		i_buf);
10436708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
104430c7ca93SDavid Disseldorp 		 tfo->fabric_name, tfo->tpg_get_wwn(se_tpg),
1045c66ac9dbSNicholas Bellinger 		tfo->tpg_get_tag(se_tpg));
10466708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
104730c7ca93SDavid Disseldorp 		" Port(s)\n",  tfo->fabric_name,
1048c66ac9dbSNicholas Bellinger 		(pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1049e3d6f909SAndy Grover 		dev->transport->name);
10506708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
105130c7ca93SDavid Disseldorp 		" 0x%08x  APTPL: %d\n", tfo->fabric_name,
1052c66ac9dbSNicholas Bellinger 		pr_reg->pr_res_key, pr_reg->pr_res_generation,
1053c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_aptpl);
1054c66ac9dbSNicholas Bellinger }
1055c66ac9dbSNicholas Bellinger 
1056c66ac9dbSNicholas Bellinger static void __core_scsi3_add_registration(
1057c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1058c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
1059c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg,
106033ce6a87SAndy Grover 	enum register_type register_type,
1061c66ac9dbSNicholas Bellinger 	int register_move)
1062c66ac9dbSNicholas Bellinger {
10639ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1064c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
10650fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
106680bfdfa9SNicholas Bellinger 	struct se_dev_entry *deve;
1067c66ac9dbSNicholas Bellinger 
1068c66ac9dbSNicholas Bellinger 	/*
1069c66ac9dbSNicholas Bellinger 	 * Increment PRgeneration counter for struct se_device upon a successful
1070c66ac9dbSNicholas Bellinger 	 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1071c66ac9dbSNicholas Bellinger 	 *
1072c66ac9dbSNicholas Bellinger 	 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1073c66ac9dbSNicholas Bellinger 	 * action, the struct se_device->dev_reservation_lock will already be held,
1074c66ac9dbSNicholas Bellinger 	 * so we do not call core_scsi3_pr_generation() which grabs the lock
1075c66ac9dbSNicholas Bellinger 	 * for the REGISTER.
1076c66ac9dbSNicholas Bellinger 	 */
1077c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_generation = (register_move) ?
10780fd97ccfSChristoph Hellwig 			dev->t10_pr.pr_generation++ :
1079c66ac9dbSNicholas Bellinger 			core_scsi3_pr_generation(dev);
1080c66ac9dbSNicholas Bellinger 
1081c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
1082c66ac9dbSNicholas Bellinger 	list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
1083c66ac9dbSNicholas Bellinger 
1084c66ac9dbSNicholas Bellinger 	__core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1085c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
1086c66ac9dbSNicholas Bellinger 	/*
1087c66ac9dbSNicholas Bellinger 	 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1088c66ac9dbSNicholas Bellinger 	 */
10896708bb27SAndy Grover 	if (!pr_reg->pr_reg_all_tg_pt || register_move)
10903ccd6e83SNicholas Bellinger 		goto out;
1091c66ac9dbSNicholas Bellinger 	/*
1092c66ac9dbSNicholas Bellinger 	 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1093c66ac9dbSNicholas Bellinger 	 * allocated in __core_scsi3_alloc_registration()
1094c66ac9dbSNicholas Bellinger 	 */
1095c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1096c66ac9dbSNicholas Bellinger 			&pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
109780bfdfa9SNicholas Bellinger 		struct se_node_acl *nacl_tmp = pr_reg_tmp->pr_reg_nacl;
109880bfdfa9SNicholas Bellinger 
1099c66ac9dbSNicholas Bellinger 		list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1100c66ac9dbSNicholas Bellinger 
1101c66ac9dbSNicholas Bellinger 		pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1102c66ac9dbSNicholas Bellinger 
1103c66ac9dbSNicholas Bellinger 		spin_lock(&pr_tmpl->registration_lock);
1104c66ac9dbSNicholas Bellinger 		list_add_tail(&pr_reg_tmp->pr_reg_list,
1105c66ac9dbSNicholas Bellinger 			      &pr_tmpl->registration_list);
1106c66ac9dbSNicholas Bellinger 
110780bfdfa9SNicholas Bellinger 		__core_scsi3_dump_registration(tfo, dev, nacl_tmp, pr_reg_tmp,
1108c66ac9dbSNicholas Bellinger 					       register_type);
1109c66ac9dbSNicholas Bellinger 		spin_unlock(&pr_tmpl->registration_lock);
11103ccd6e83SNicholas Bellinger 		/*
11113ccd6e83SNicholas Bellinger 		 * Drop configfs group dependency reference and deve->pr_kref
11123ccd6e83SNicholas Bellinger 		 * obtained from  __core_scsi3_alloc_registration() code.
11133ccd6e83SNicholas Bellinger 		 */
111480bfdfa9SNicholas Bellinger 		rcu_read_lock();
111580bfdfa9SNicholas Bellinger 		deve = pr_reg_tmp->pr_reg_deve;
11163ccd6e83SNicholas Bellinger 		if (deve) {
111780bfdfa9SNicholas Bellinger 			set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
11183ccd6e83SNicholas Bellinger 			core_scsi3_lunacl_undepend_item(deve);
11193ccd6e83SNicholas Bellinger 			pr_reg_tmp->pr_reg_deve = NULL;
1120c66ac9dbSNicholas Bellinger 		}
11213ccd6e83SNicholas Bellinger 		rcu_read_unlock();
11223ccd6e83SNicholas Bellinger 	}
11233ccd6e83SNicholas Bellinger out:
11243ccd6e83SNicholas Bellinger 	/*
11253ccd6e83SNicholas Bellinger 	 * Drop deve->pr_kref obtained in __core_scsi3_do_alloc_registration()
11263ccd6e83SNicholas Bellinger 	 */
11273ccd6e83SNicholas Bellinger 	rcu_read_lock();
11283ccd6e83SNicholas Bellinger 	deve = pr_reg->pr_reg_deve;
11293ccd6e83SNicholas Bellinger 	if (deve) {
11303ccd6e83SNicholas Bellinger 		set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
11313ccd6e83SNicholas Bellinger 		kref_put(&deve->pr_kref, target_pr_kref_release);
11323ccd6e83SNicholas Bellinger 		pr_reg->pr_reg_deve = NULL;
11333ccd6e83SNicholas Bellinger 	}
11343ccd6e83SNicholas Bellinger 	rcu_read_unlock();
1135c66ac9dbSNicholas Bellinger }
1136c66ac9dbSNicholas Bellinger 
1137c66ac9dbSNicholas Bellinger static int core_scsi3_alloc_registration(
1138c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1139c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
114079dc9c9eSNicholas Bellinger 	struct se_lun *lun,
1141c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve,
1142f2d30680SHannes Reinecke 	u64 mapped_lun,
1143c66ac9dbSNicholas Bellinger 	unsigned char *isid,
1144c66ac9dbSNicholas Bellinger 	u64 sa_res_key,
1145c66ac9dbSNicholas Bellinger 	int all_tg_pt,
1146c66ac9dbSNicholas Bellinger 	int aptpl,
114733ce6a87SAndy Grover 	enum register_type register_type,
1148c66ac9dbSNicholas Bellinger 	int register_move)
1149c66ac9dbSNicholas Bellinger {
1150c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1151c66ac9dbSNicholas Bellinger 
115279dc9c9eSNicholas Bellinger 	pr_reg = __core_scsi3_alloc_registration(dev, nacl, lun, deve, mapped_lun,
115379dc9c9eSNicholas Bellinger 						 isid, sa_res_key, all_tg_pt,
115479dc9c9eSNicholas Bellinger 						 aptpl);
11556708bb27SAndy Grover 	if (!pr_reg)
1156e3d6f909SAndy Grover 		return -EPERM;
1157c66ac9dbSNicholas Bellinger 
1158c66ac9dbSNicholas Bellinger 	__core_scsi3_add_registration(dev, nacl, pr_reg,
1159c66ac9dbSNicholas Bellinger 			register_type, register_move);
1160c66ac9dbSNicholas Bellinger 	return 0;
1161c66ac9dbSNicholas Bellinger }
1162c66ac9dbSNicholas Bellinger 
1163c66ac9dbSNicholas Bellinger static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1164c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1165c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
1166c66ac9dbSNicholas Bellinger 	unsigned char *isid)
1167c66ac9dbSNicholas Bellinger {
11680fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
1169c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1170c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg;
1171c66ac9dbSNicholas Bellinger 
1172c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
1173c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1174c66ac9dbSNicholas Bellinger 			&pr_tmpl->registration_list, pr_reg_list) {
1175c66ac9dbSNicholas Bellinger 		/*
1176c66ac9dbSNicholas Bellinger 		 * First look for a matching struct se_node_acl
1177c66ac9dbSNicholas Bellinger 		 */
1178c66ac9dbSNicholas Bellinger 		if (pr_reg->pr_reg_nacl != nacl)
1179c66ac9dbSNicholas Bellinger 			continue;
1180c66ac9dbSNicholas Bellinger 
1181c66ac9dbSNicholas Bellinger 		tpg = pr_reg->pr_reg_nacl->se_tpg;
1182c66ac9dbSNicholas Bellinger 		/*
1183c66ac9dbSNicholas Bellinger 		 * If this registration does NOT contain a fabric provided
1184c66ac9dbSNicholas Bellinger 		 * ISID, then we have found a match.
1185c66ac9dbSNicholas Bellinger 		 */
11866708bb27SAndy Grover 		if (!pr_reg->isid_present_at_reg) {
1187c66ac9dbSNicholas Bellinger 			/*
1188c66ac9dbSNicholas Bellinger 			 * Determine if this SCSI device server requires that
1189c66ac9dbSNicholas Bellinger 			 * SCSI Intiatior TransportID w/ ISIDs is enforced
1190c66ac9dbSNicholas Bellinger 			 * for fabric modules (iSCSI) requiring them.
1191c66ac9dbSNicholas Bellinger 			 */
1192e3d6f909SAndy Grover 			if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
11930fd97ccfSChristoph Hellwig 				if (dev->dev_attrib.enforce_pr_isids)
1194c66ac9dbSNicholas Bellinger 					continue;
1195c66ac9dbSNicholas Bellinger 			}
119633940d09SJoern Engel 			atomic_inc_mb(&pr_reg->pr_res_holders);
1197c66ac9dbSNicholas Bellinger 			spin_unlock(&pr_tmpl->registration_lock);
1198c66ac9dbSNicholas Bellinger 			return pr_reg;
1199c66ac9dbSNicholas Bellinger 		}
1200c66ac9dbSNicholas Bellinger 		/*
1201c66ac9dbSNicholas Bellinger 		 * If the *pr_reg contains a fabric defined ISID for multi-value
1202c66ac9dbSNicholas Bellinger 		 * SCSI Initiator Port TransportIDs, then we expect a valid
1203c66ac9dbSNicholas Bellinger 		 * matching ISID to be provided by the local SCSI Initiator Port.
1204c66ac9dbSNicholas Bellinger 		 */
12056708bb27SAndy Grover 		if (!isid)
1206c66ac9dbSNicholas Bellinger 			continue;
1207c66ac9dbSNicholas Bellinger 		if (strcmp(isid, pr_reg->pr_reg_isid))
1208c66ac9dbSNicholas Bellinger 			continue;
1209c66ac9dbSNicholas Bellinger 
121033940d09SJoern Engel 		atomic_inc_mb(&pr_reg->pr_res_holders);
1211c66ac9dbSNicholas Bellinger 		spin_unlock(&pr_tmpl->registration_lock);
1212c66ac9dbSNicholas Bellinger 		return pr_reg;
1213c66ac9dbSNicholas Bellinger 	}
1214c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
1215c66ac9dbSNicholas Bellinger 
1216c66ac9dbSNicholas Bellinger 	return NULL;
1217c66ac9dbSNicholas Bellinger }
1218c66ac9dbSNicholas Bellinger 
1219c66ac9dbSNicholas Bellinger static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1220c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1221c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
1222c66ac9dbSNicholas Bellinger 	struct se_session *sess)
1223c66ac9dbSNicholas Bellinger {
1224c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg = nacl->se_tpg;
1225c66ac9dbSNicholas Bellinger 	unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1226c66ac9dbSNicholas Bellinger 
1227e3d6f909SAndy Grover 	if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1228c66ac9dbSNicholas Bellinger 		memset(&buf[0], 0, PR_REG_ISID_LEN);
1229e3d6f909SAndy Grover 		tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
1230c66ac9dbSNicholas Bellinger 					PR_REG_ISID_LEN);
1231c66ac9dbSNicholas Bellinger 		isid_ptr = &buf[0];
1232c66ac9dbSNicholas Bellinger 	}
1233c66ac9dbSNicholas Bellinger 
1234c66ac9dbSNicholas Bellinger 	return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1235c66ac9dbSNicholas Bellinger }
1236c66ac9dbSNicholas Bellinger 
1237c66ac9dbSNicholas Bellinger static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1238c66ac9dbSNicholas Bellinger {
123933940d09SJoern Engel 	atomic_dec_mb(&pr_reg->pr_res_holders);
1240c66ac9dbSNicholas Bellinger }
1241c66ac9dbSNicholas Bellinger 
1242125d0119SHannes Reinecke static int core_scsi3_check_implicit_release(
1243c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1244c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg)
1245c66ac9dbSNicholas Bellinger {
1246c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1247c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_res_holder;
1248c66ac9dbSNicholas Bellinger 	int ret = 0;
1249c66ac9dbSNicholas Bellinger 
1250c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1251c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
12526708bb27SAndy Grover 	if (!pr_res_holder) {
1253c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
1254c66ac9dbSNicholas Bellinger 		return ret;
1255c66ac9dbSNicholas Bellinger 	}
1256c66ac9dbSNicholas Bellinger 	if (pr_res_holder == pr_reg) {
1257c66ac9dbSNicholas Bellinger 		/*
1258125d0119SHannes Reinecke 		 * Perform an implicit RELEASE if the registration that
1259c66ac9dbSNicholas Bellinger 		 * is being released is holding the reservation.
1260c66ac9dbSNicholas Bellinger 		 *
1261c66ac9dbSNicholas Bellinger 		 * From spc4r17, section 5.7.11.1:
1262c66ac9dbSNicholas Bellinger 		 *
1263c66ac9dbSNicholas Bellinger 		 * e) If the I_T nexus is the persistent reservation holder
1264c66ac9dbSNicholas Bellinger 		 *    and the persistent reservation is not an all registrants
1265c66ac9dbSNicholas Bellinger 		 *    type, then a PERSISTENT RESERVE OUT command with REGISTER
1266c66ac9dbSNicholas Bellinger 		 *    service action or REGISTER AND  IGNORE EXISTING KEY
1267c66ac9dbSNicholas Bellinger 		 *    service action with the SERVICE ACTION RESERVATION KEY
1268c66ac9dbSNicholas Bellinger 		 *    field set to zero (see 5.7.11.3).
1269c66ac9dbSNicholas Bellinger 		 */
12706c3c9baaSNicholas Bellinger 		__core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0, 1);
1271c66ac9dbSNicholas Bellinger 		ret = 1;
1272c66ac9dbSNicholas Bellinger 		/*
1273c66ac9dbSNicholas Bellinger 		 * For 'All Registrants' reservation types, all existing
1274c66ac9dbSNicholas Bellinger 		 * registrations are still processed as reservation holders
1275c66ac9dbSNicholas Bellinger 		 * in core_scsi3_pr_seq_non_holder() after the initial
1276125d0119SHannes Reinecke 		 * reservation holder is implicitly released here.
1277c66ac9dbSNicholas Bellinger 		 */
1278c66ac9dbSNicholas Bellinger 	} else if (pr_reg->pr_reg_all_tg_pt &&
1279c66ac9dbSNicholas Bellinger 		  (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1280c66ac9dbSNicholas Bellinger 			  pr_reg->pr_reg_nacl->initiatorname)) &&
1281c66ac9dbSNicholas Bellinger 		  (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
12826708bb27SAndy Grover 		pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1283c66ac9dbSNicholas Bellinger 			" UNREGISTER while existing reservation with matching"
1284c66ac9dbSNicholas Bellinger 			" key 0x%016Lx is present from another SCSI Initiator"
1285c66ac9dbSNicholas Bellinger 			" Port\n", pr_reg->pr_res_key);
1286e3d6f909SAndy Grover 		ret = -EPERM;
1287c66ac9dbSNicholas Bellinger 	}
1288c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
1289c66ac9dbSNicholas Bellinger 
1290c66ac9dbSNicholas Bellinger 	return ret;
1291c66ac9dbSNicholas Bellinger }
1292c66ac9dbSNicholas Bellinger 
1293c66ac9dbSNicholas Bellinger /*
1294e3d6f909SAndy Grover  * Called with struct t10_reservation->registration_lock held.
1295c66ac9dbSNicholas Bellinger  */
1296c66ac9dbSNicholas Bellinger static void __core_scsi3_free_registration(
1297c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1298c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg,
1299c66ac9dbSNicholas Bellinger 	struct list_head *preempt_and_abort_list,
1300c66ac9dbSNicholas Bellinger 	int dec_holders)
1301cb0df4d3SBart Van Assche 	__releases(&pr_tmpl->registration_lock)
1302cb0df4d3SBart Van Assche 	__acquires(&pr_tmpl->registration_lock)
1303c66ac9dbSNicholas Bellinger {
13049ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo =
1305c66ac9dbSNicholas Bellinger 			pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
13060fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
130780bfdfa9SNicholas Bellinger 	struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
130880bfdfa9SNicholas Bellinger 	struct se_dev_entry *deve;
1309c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
1310c66ac9dbSNicholas Bellinger 
1311c66ac9dbSNicholas Bellinger 	memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1312d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1313c66ac9dbSNicholas Bellinger 
13146c3c9baaSNicholas Bellinger 	if (!list_empty(&pr_reg->pr_reg_list))
1315c66ac9dbSNicholas Bellinger 		list_del(&pr_reg->pr_reg_list);
1316c66ac9dbSNicholas Bellinger 	/*
1317c66ac9dbSNicholas Bellinger 	 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1318c66ac9dbSNicholas Bellinger 	 * so call core_scsi3_put_pr_reg() to decrement our reference.
1319c66ac9dbSNicholas Bellinger 	 */
1320c66ac9dbSNicholas Bellinger 	if (dec_holders)
1321c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg);
132280bfdfa9SNicholas Bellinger 
132380bfdfa9SNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
1324c66ac9dbSNicholas Bellinger 	/*
1325c66ac9dbSNicholas Bellinger 	 * Wait until all reference from any other I_T nexuses for this
1326c66ac9dbSNicholas Bellinger 	 * *pr_reg have been released.  Because list_del() is called above,
1327c66ac9dbSNicholas Bellinger 	 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1328c66ac9dbSNicholas Bellinger 	 * count back to zero, and we release *pr_reg.
1329c66ac9dbSNicholas Bellinger 	 */
1330c66ac9dbSNicholas Bellinger 	while (atomic_read(&pr_reg->pr_res_holders) != 0) {
13316708bb27SAndy Grover 		pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
133230c7ca93SDavid Disseldorp 				tfo->fabric_name);
1333c66ac9dbSNicholas Bellinger 		cpu_relax();
1334c66ac9dbSNicholas Bellinger 	}
1335c66ac9dbSNicholas Bellinger 
133680bfdfa9SNicholas Bellinger 	rcu_read_lock();
133780bfdfa9SNicholas Bellinger 	deve = target_nacl_find_deve(nacl, pr_reg->pr_res_mapped_lun);
133880bfdfa9SNicholas Bellinger 	if (deve)
133980bfdfa9SNicholas Bellinger 		clear_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
134080bfdfa9SNicholas Bellinger 	rcu_read_unlock();
134180bfdfa9SNicholas Bellinger 
134280bfdfa9SNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
13436708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
134430c7ca93SDavid Disseldorp 		" Node: %s%s\n", tfo->fabric_name,
1345c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_nacl->initiatorname,
1346d2843c17SAndy Grover 		i_buf);
13476708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
134830c7ca93SDavid Disseldorp 		" Port(s)\n", tfo->fabric_name,
1349c66ac9dbSNicholas Bellinger 		(pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1350e3d6f909SAndy Grover 		dev->transport->name);
13516708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
135230c7ca93SDavid Disseldorp 		" 0x%08x\n", tfo->fabric_name, pr_reg->pr_res_key,
1353c66ac9dbSNicholas Bellinger 		pr_reg->pr_res_generation);
1354c66ac9dbSNicholas Bellinger 
13556708bb27SAndy Grover 	if (!preempt_and_abort_list) {
1356c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_deve = NULL;
1357c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_nacl = NULL;
1358c66ac9dbSNicholas Bellinger 		kmem_cache_free(t10_pr_reg_cache, pr_reg);
1359c66ac9dbSNicholas Bellinger 		return;
1360c66ac9dbSNicholas Bellinger 	}
1361c66ac9dbSNicholas Bellinger 	/*
1362c66ac9dbSNicholas Bellinger 	 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1363c66ac9dbSNicholas Bellinger 	 * are released once the ABORT_TASK_SET has completed..
1364c66ac9dbSNicholas Bellinger 	 */
1365c66ac9dbSNicholas Bellinger 	list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1366c66ac9dbSNicholas Bellinger }
1367c66ac9dbSNicholas Bellinger 
1368c66ac9dbSNicholas Bellinger void core_scsi3_free_pr_reg_from_nacl(
1369c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1370c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl)
1371c66ac9dbSNicholas Bellinger {
13720fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
1373c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
13746c3c9baaSNicholas Bellinger 	bool free_reg = false;
1375c66ac9dbSNicholas Bellinger 	/*
1376c66ac9dbSNicholas Bellinger 	 * If the passed se_node_acl matches the reservation holder,
1377c66ac9dbSNicholas Bellinger 	 * release the reservation.
1378c66ac9dbSNicholas Bellinger 	 */
1379c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1380c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
1381c66ac9dbSNicholas Bellinger 	if ((pr_res_holder != NULL) &&
13826c3c9baaSNicholas Bellinger 	    (pr_res_holder->pr_reg_nacl == nacl)) {
13836c3c9baaSNicholas Bellinger 		__core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0, 1);
13846c3c9baaSNicholas Bellinger 		free_reg = true;
13856c3c9baaSNicholas Bellinger 	}
1386c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
1387c66ac9dbSNicholas Bellinger 	/*
1388c66ac9dbSNicholas Bellinger 	 * Release any registration associated with the struct se_node_acl.
1389c66ac9dbSNicholas Bellinger 	 */
1390c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
13916c3c9baaSNicholas Bellinger 	if (pr_res_holder && free_reg)
13926c3c9baaSNicholas Bellinger 		__core_scsi3_free_registration(dev, pr_res_holder, NULL, 0);
13936c3c9baaSNicholas Bellinger 
1394c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1395c66ac9dbSNicholas Bellinger 			&pr_tmpl->registration_list, pr_reg_list) {
1396c66ac9dbSNicholas Bellinger 
1397c66ac9dbSNicholas Bellinger 		if (pr_reg->pr_reg_nacl != nacl)
1398c66ac9dbSNicholas Bellinger 			continue;
1399c66ac9dbSNicholas Bellinger 
1400c66ac9dbSNicholas Bellinger 		__core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1401c66ac9dbSNicholas Bellinger 	}
1402c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
1403c66ac9dbSNicholas Bellinger }
1404c66ac9dbSNicholas Bellinger 
1405c66ac9dbSNicholas Bellinger void core_scsi3_free_all_registrations(
1406c66ac9dbSNicholas Bellinger 	struct se_device *dev)
1407c66ac9dbSNicholas Bellinger {
14080fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
1409c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1410c66ac9dbSNicholas Bellinger 
1411c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1412c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
1413c66ac9dbSNicholas Bellinger 	if (pr_res_holder != NULL) {
1414c66ac9dbSNicholas Bellinger 		struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1415c66ac9dbSNicholas Bellinger 		__core_scsi3_complete_pro_release(dev, pr_res_nacl,
14166c3c9baaSNicholas Bellinger 						  pr_res_holder, 0, 0);
1417c66ac9dbSNicholas Bellinger 	}
1418c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
1419c66ac9dbSNicholas Bellinger 
1420c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
1421c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1422c66ac9dbSNicholas Bellinger 			&pr_tmpl->registration_list, pr_reg_list) {
1423c66ac9dbSNicholas Bellinger 
1424c66ac9dbSNicholas Bellinger 		__core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1425c66ac9dbSNicholas Bellinger 	}
1426c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
1427c66ac9dbSNicholas Bellinger 
1428c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->aptpl_reg_lock);
1429c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1430c66ac9dbSNicholas Bellinger 				pr_reg_aptpl_list) {
1431c66ac9dbSNicholas Bellinger 		list_del(&pr_reg->pr_reg_aptpl_list);
1432c66ac9dbSNicholas Bellinger 		kmem_cache_free(t10_pr_reg_cache, pr_reg);
1433c66ac9dbSNicholas Bellinger 	}
1434c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->aptpl_reg_lock);
1435c66ac9dbSNicholas Bellinger }
1436c66ac9dbSNicholas Bellinger 
1437c66ac9dbSNicholas Bellinger static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1438c66ac9dbSNicholas Bellinger {
1439d588cf8fSChristoph Hellwig 	return target_depend_item(&tpg->tpg_group.cg_item);
1440c66ac9dbSNicholas Bellinger }
1441c66ac9dbSNicholas Bellinger 
1442c66ac9dbSNicholas Bellinger static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1443c66ac9dbSNicholas Bellinger {
1444d588cf8fSChristoph Hellwig 	target_undepend_item(&tpg->tpg_group.cg_item);
144533940d09SJoern Engel 	atomic_dec_mb(&tpg->tpg_pr_ref_count);
1446c66ac9dbSNicholas Bellinger }
1447c66ac9dbSNicholas Bellinger 
1448c66ac9dbSNicholas Bellinger static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1449c66ac9dbSNicholas Bellinger {
1450c66ac9dbSNicholas Bellinger 	if (nacl->dynamic_node_acl)
1451c66ac9dbSNicholas Bellinger 		return 0;
1452d588cf8fSChristoph Hellwig 	return target_depend_item(&nacl->acl_group.cg_item);
1453c66ac9dbSNicholas Bellinger }
1454c66ac9dbSNicholas Bellinger 
1455c66ac9dbSNicholas Bellinger static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1456c66ac9dbSNicholas Bellinger {
1457d588cf8fSChristoph Hellwig 	if (!nacl->dynamic_node_acl)
1458d588cf8fSChristoph Hellwig 		target_undepend_item(&nacl->acl_group.cg_item);
145933940d09SJoern Engel 	atomic_dec_mb(&nacl->acl_pr_ref_count);
1460c66ac9dbSNicholas Bellinger }
1461c66ac9dbSNicholas Bellinger 
1462c66ac9dbSNicholas Bellinger static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1463c66ac9dbSNicholas Bellinger {
146479dc9c9eSNicholas Bellinger 	struct se_lun_acl *lun_acl;
1465ac75d8beSBart Van Assche 
1466c66ac9dbSNicholas Bellinger 	/*
1467c66ac9dbSNicholas Bellinger 	 * For nacl->dynamic_node_acl=1
1468c66ac9dbSNicholas Bellinger 	 */
146979dc9c9eSNicholas Bellinger 	lun_acl = rcu_dereference_check(se_deve->se_lun_acl,
14702c935bc5SPeter Zijlstra 				kref_read(&se_deve->pr_kref) != 0);
14716708bb27SAndy Grover 	if (!lun_acl)
1472c66ac9dbSNicholas Bellinger 		return 0;
1473c66ac9dbSNicholas Bellinger 
1474d588cf8fSChristoph Hellwig 	return target_depend_item(&lun_acl->se_lun_group.cg_item);
1475c66ac9dbSNicholas Bellinger }
1476c66ac9dbSNicholas Bellinger 
1477c66ac9dbSNicholas Bellinger static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1478c66ac9dbSNicholas Bellinger {
147929a05deeSNicholas Bellinger 	struct se_lun_acl *lun_acl;
1480ac75d8beSBart Van Assche 
1481c66ac9dbSNicholas Bellinger 	/*
1482c66ac9dbSNicholas Bellinger 	 * For nacl->dynamic_node_acl=1
1483c66ac9dbSNicholas Bellinger 	 */
148479dc9c9eSNicholas Bellinger 	lun_acl = rcu_dereference_check(se_deve->se_lun_acl,
14852c935bc5SPeter Zijlstra 				kref_read(&se_deve->pr_kref) != 0);
14866708bb27SAndy Grover 	if (!lun_acl) {
148729a05deeSNicholas Bellinger 		kref_put(&se_deve->pr_kref, target_pr_kref_release);
1488c66ac9dbSNicholas Bellinger 		return;
1489c66ac9dbSNicholas Bellinger 	}
1490c66ac9dbSNicholas Bellinger 
1491d588cf8fSChristoph Hellwig 	target_undepend_item(&lun_acl->se_lun_group.cg_item);
149229a05deeSNicholas Bellinger 	kref_put(&se_deve->pr_kref, target_pr_kref_release);
1493c66ac9dbSNicholas Bellinger }
1494c66ac9dbSNicholas Bellinger 
1495de103c93SChristoph Hellwig static sense_reason_t
1496de103c93SChristoph Hellwig core_scsi3_decode_spec_i_port(
1497c66ac9dbSNicholas Bellinger 	struct se_cmd *cmd,
1498c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
1499c66ac9dbSNicholas Bellinger 	unsigned char *l_isid,
1500c66ac9dbSNicholas Bellinger 	u64 sa_res_key,
1501c66ac9dbSNicholas Bellinger 	int all_tg_pt,
1502c66ac9dbSNicholas Bellinger 	int aptpl)
1503c66ac9dbSNicholas Bellinger {
15045951146dSAndy Grover 	struct se_device *dev = cmd->se_dev;
1505c66ac9dbSNicholas Bellinger 	struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
1506e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
1507c66ac9dbSNicholas Bellinger 	struct se_node_acl *dest_node_acl = NULL;
150879dc9c9eSNicholas Bellinger 	struct se_dev_entry *dest_se_deve = NULL;
1509c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1510c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1511d0f474e5SRoland Dreier 	LIST_HEAD(tid_dest_list);
1512c66ac9dbSNicholas Bellinger 	struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
15132650d71eSChristoph Hellwig 	unsigned char *buf, *ptr, proto_ident;
15149b353cc8SSagi Grimberg 	const unsigned char *i_str = NULL;
151513ba564cSJulia Lawall 	char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
1516de103c93SChristoph Hellwig 	sense_reason_t ret;
1517c66ac9dbSNicholas Bellinger 	u32 tpdl, tid_len = 0;
1518c66ac9dbSNicholas Bellinger 	u32 dest_rtpi = 0;
1519c66ac9dbSNicholas Bellinger 
1520c66ac9dbSNicholas Bellinger 	/*
1521c66ac9dbSNicholas Bellinger 	 * Allocate a struct pr_transport_id_holder and setup the
152279dc9c9eSNicholas Bellinger 	 * local_node_acl pointer and add to struct list_head tid_dest_list
152379dc9c9eSNicholas Bellinger 	 * for add registration processing in the loop of tid_dest_list below.
1524c66ac9dbSNicholas Bellinger 	 */
1525c66ac9dbSNicholas Bellinger 	tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
15266708bb27SAndy Grover 	if (!tidh_new) {
15276708bb27SAndy Grover 		pr_err("Unable to allocate tidh_new\n");
152879dd6f2fStangwenji 		return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1529c66ac9dbSNicholas Bellinger 	}
1530c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&tidh_new->dest_list);
1531c66ac9dbSNicholas Bellinger 	tidh_new->dest_tpg = tpg;
1532c66ac9dbSNicholas Bellinger 	tidh_new->dest_node_acl = se_sess->se_node_acl;
1533c66ac9dbSNicholas Bellinger 
15345951146dSAndy Grover 	local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
153579dc9c9eSNicholas Bellinger 				se_sess->se_node_acl, cmd->se_lun,
153679dc9c9eSNicholas Bellinger 				NULL, cmd->orig_fe_lun, l_isid,
1537c66ac9dbSNicholas Bellinger 				sa_res_key, all_tg_pt, aptpl);
15386708bb27SAndy Grover 	if (!local_pr_reg) {
1539c66ac9dbSNicholas Bellinger 		kfree(tidh_new);
154079dd6f2fStangwenji 		return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1541c66ac9dbSNicholas Bellinger 	}
1542c66ac9dbSNicholas Bellinger 	tidh_new->dest_pr_reg = local_pr_reg;
1543c66ac9dbSNicholas Bellinger 	/*
1544c66ac9dbSNicholas Bellinger 	 * The local I_T nexus does not hold any configfs dependances,
154579dc9c9eSNicholas Bellinger 	 * so we set tidh_new->dest_se_deve to NULL to prevent the
1546c66ac9dbSNicholas Bellinger 	 * configfs_undepend_item() calls in the tid_dest_list loops below.
1547c66ac9dbSNicholas Bellinger 	 */
154879dc9c9eSNicholas Bellinger 	tidh_new->dest_se_deve = NULL;
1549c66ac9dbSNicholas Bellinger 	list_add_tail(&tidh_new->dest_list, &tid_dest_list);
155005d1c7c0SAndy Grover 
15510d7f1299SPaolo Bonzini 	if (cmd->data_length < 28) {
15520d7f1299SPaolo Bonzini 		pr_warn("SPC-PR: Received PR OUT parameter list"
15530d7f1299SPaolo Bonzini 			" length too small: %u\n", cmd->data_length);
1554de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
15550d7f1299SPaolo Bonzini 		goto out;
15560d7f1299SPaolo Bonzini 	}
15570d7f1299SPaolo Bonzini 
15584949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
1559de103c93SChristoph Hellwig 	if (!buf) {
156079dd6f2fStangwenji 		ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1561de103c93SChristoph Hellwig 		goto out;
1562de103c93SChristoph Hellwig 	}
1563de103c93SChristoph Hellwig 
1564c66ac9dbSNicholas Bellinger 	/*
1565c66ac9dbSNicholas Bellinger 	 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1566c66ac9dbSNicholas Bellinger 	 * first extract TransportID Parameter Data Length, and make sure
1567c66ac9dbSNicholas Bellinger 	 * the value matches up to the SCSI expected data transfer length.
1568c66ac9dbSNicholas Bellinger 	 */
1569a85d667eSBart Van Assche 	tpdl = get_unaligned_be32(&buf[24]);
1570c66ac9dbSNicholas Bellinger 
1571c66ac9dbSNicholas Bellinger 	if ((tpdl + 28) != cmd->data_length) {
15726708bb27SAndy Grover 		pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1573c66ac9dbSNicholas Bellinger 			" does not equal CDB data_length: %u\n", tpdl,
1574c66ac9dbSNicholas Bellinger 			cmd->data_length);
1575de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
1576de103c93SChristoph Hellwig 		goto out_unmap;
1577c66ac9dbSNicholas Bellinger 	}
1578c66ac9dbSNicholas Bellinger 	/*
1579c66ac9dbSNicholas Bellinger 	 * Start processing the received transport IDs using the
1580c66ac9dbSNicholas Bellinger 	 * receiving I_T Nexus portal's fabric dependent methods to
1581c66ac9dbSNicholas Bellinger 	 * obtain the SCSI Initiator Port/Device Identifiers.
1582c66ac9dbSNicholas Bellinger 	 */
1583c66ac9dbSNicholas Bellinger 	ptr = &buf[28];
1584c66ac9dbSNicholas Bellinger 
1585c66ac9dbSNicholas Bellinger 	while (tpdl > 0) {
1586adf653f9SChristoph Hellwig 		struct se_lun *dest_lun, *tmp_lun;
158779dc9c9eSNicholas Bellinger 
1588c66ac9dbSNicholas Bellinger 		proto_ident = (ptr[0] & 0x0f);
1589c66ac9dbSNicholas Bellinger 		dest_tpg = NULL;
1590c66ac9dbSNicholas Bellinger 
1591c66ac9dbSNicholas Bellinger 		spin_lock(&dev->se_port_lock);
1592adf653f9SChristoph Hellwig 		list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
1593adf653f9SChristoph Hellwig 			tmp_tpg = tmp_lun->lun_tpg;
15942650d71eSChristoph Hellwig 
1595c66ac9dbSNicholas Bellinger 			/*
1596c66ac9dbSNicholas Bellinger 			 * Look for the matching proto_ident provided by
1597c66ac9dbSNicholas Bellinger 			 * the received TransportID
1598c66ac9dbSNicholas Bellinger 			 */
15992aeeafaeSChristoph Hellwig 			if (tmp_tpg->proto_id != proto_ident)
1600c66ac9dbSNicholas Bellinger 				continue;
1601adf653f9SChristoph Hellwig 			dest_rtpi = tmp_lun->lun_rtpi;
1602c66ac9dbSNicholas Bellinger 
16032650d71eSChristoph Hellwig 			i_str = target_parse_pr_out_transport_id(tmp_tpg,
1604094bb5d7SRasmus Villemoes 					ptr, &tid_len, &iport_ptr);
16056708bb27SAndy Grover 			if (!i_str)
1606c66ac9dbSNicholas Bellinger 				continue;
1607c66ac9dbSNicholas Bellinger 
160833940d09SJoern Engel 			atomic_inc_mb(&tmp_tpg->tpg_pr_ref_count);
1609c66ac9dbSNicholas Bellinger 			spin_unlock(&dev->se_port_lock);
1610c66ac9dbSNicholas Bellinger 
1611de103c93SChristoph Hellwig 			if (core_scsi3_tpg_depend_item(tmp_tpg)) {
16126708bb27SAndy Grover 				pr_err(" core_scsi3_tpg_depend_item()"
1613c66ac9dbSNicholas Bellinger 					" for tmp_tpg\n");
161433940d09SJoern Engel 				atomic_dec_mb(&tmp_tpg->tpg_pr_ref_count);
1615de103c93SChristoph Hellwig 				ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1616de103c93SChristoph Hellwig 				goto out_unmap;
1617c66ac9dbSNicholas Bellinger 			}
1618c66ac9dbSNicholas Bellinger 			/*
161935d1efe8SMasanari Iida 			 * Locate the destination initiator ACL to be registered
1620c66ac9dbSNicholas Bellinger 			 * from the decoded fabric module specific TransportID
1621c66ac9dbSNicholas Bellinger 			 * at *i_str.
1622c66ac9dbSNicholas Bellinger 			 */
1623403edd78SNicholas Bellinger 			mutex_lock(&tmp_tpg->acl_node_mutex);
1624c66ac9dbSNicholas Bellinger 			dest_node_acl = __core_tpg_get_initiator_node_acl(
1625c66ac9dbSNicholas Bellinger 						tmp_tpg, i_str);
162633940d09SJoern Engel 			if (dest_node_acl)
162733940d09SJoern Engel 				atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
1628403edd78SNicholas Bellinger 			mutex_unlock(&tmp_tpg->acl_node_mutex);
1629c66ac9dbSNicholas Bellinger 
16306708bb27SAndy Grover 			if (!dest_node_acl) {
1631c66ac9dbSNicholas Bellinger 				core_scsi3_tpg_undepend_item(tmp_tpg);
1632c66ac9dbSNicholas Bellinger 				spin_lock(&dev->se_port_lock);
1633c66ac9dbSNicholas Bellinger 				continue;
1634c66ac9dbSNicholas Bellinger 			}
1635c66ac9dbSNicholas Bellinger 
1636de103c93SChristoph Hellwig 			if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
16376708bb27SAndy Grover 				pr_err("configfs_depend_item() failed"
1638c66ac9dbSNicholas Bellinger 					" for dest_node_acl->acl_group\n");
163933940d09SJoern Engel 				atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
1640c66ac9dbSNicholas Bellinger 				core_scsi3_tpg_undepend_item(tmp_tpg);
1641de103c93SChristoph Hellwig 				ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1642de103c93SChristoph Hellwig 				goto out_unmap;
1643c66ac9dbSNicholas Bellinger 			}
1644c66ac9dbSNicholas Bellinger 
1645c66ac9dbSNicholas Bellinger 			dest_tpg = tmp_tpg;
16466708bb27SAndy Grover 			pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1647c66ac9dbSNicholas Bellinger 				" %s Port RTPI: %hu\n",
164830c7ca93SDavid Disseldorp 				dest_tpg->se_tpg_tfo->fabric_name,
1649c66ac9dbSNicholas Bellinger 				dest_node_acl->initiatorname, dest_rtpi);
1650c66ac9dbSNicholas Bellinger 
1651c66ac9dbSNicholas Bellinger 			spin_lock(&dev->se_port_lock);
1652c66ac9dbSNicholas Bellinger 			break;
1653c66ac9dbSNicholas Bellinger 		}
1654c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->se_port_lock);
1655c66ac9dbSNicholas Bellinger 
16566708bb27SAndy Grover 		if (!dest_tpg) {
16576708bb27SAndy Grover 			pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1658c66ac9dbSNicholas Bellinger 					" dest_tpg\n");
1659de103c93SChristoph Hellwig 			ret = TCM_INVALID_PARAMETER_LIST;
1660de103c93SChristoph Hellwig 			goto out_unmap;
1661c66ac9dbSNicholas Bellinger 		}
16628b1e1244SAndy Grover 
16636708bb27SAndy Grover 		pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1664c66ac9dbSNicholas Bellinger 			" tid_len: %d for %s + %s\n",
166530c7ca93SDavid Disseldorp 			dest_tpg->se_tpg_tfo->fabric_name, cmd->data_length,
1666c66ac9dbSNicholas Bellinger 			tpdl, tid_len, i_str, iport_ptr);
16678b1e1244SAndy Grover 
1668c66ac9dbSNicholas Bellinger 		if (tid_len > tpdl) {
16696708bb27SAndy Grover 			pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1670c66ac9dbSNicholas Bellinger 				" %u for Transport ID: %s\n", tid_len, ptr);
1671c66ac9dbSNicholas Bellinger 			core_scsi3_nodeacl_undepend_item(dest_node_acl);
1672c66ac9dbSNicholas Bellinger 			core_scsi3_tpg_undepend_item(dest_tpg);
1673de103c93SChristoph Hellwig 			ret = TCM_INVALID_PARAMETER_LIST;
1674de103c93SChristoph Hellwig 			goto out_unmap;
1675c66ac9dbSNicholas Bellinger 		}
1676c66ac9dbSNicholas Bellinger 		/*
1677c66ac9dbSNicholas Bellinger 		 * Locate the desintation struct se_dev_entry pointer for matching
1678c66ac9dbSNicholas Bellinger 		 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1679c66ac9dbSNicholas Bellinger 		 * Target Port.
1680c66ac9dbSNicholas Bellinger 		 */
1681c66ac9dbSNicholas Bellinger 		dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1682c66ac9dbSNicholas Bellinger 					dest_rtpi);
16836708bb27SAndy Grover 		if (!dest_se_deve) {
16846708bb27SAndy Grover 			pr_err("Unable to locate %s dest_se_deve"
1685c66ac9dbSNicholas Bellinger 				" from destination RTPI: %hu\n",
168630c7ca93SDavid Disseldorp 				dest_tpg->se_tpg_tfo->fabric_name,
1687c66ac9dbSNicholas Bellinger 				dest_rtpi);
1688c66ac9dbSNicholas Bellinger 
1689c66ac9dbSNicholas Bellinger 			core_scsi3_nodeacl_undepend_item(dest_node_acl);
1690c66ac9dbSNicholas Bellinger 			core_scsi3_tpg_undepend_item(dest_tpg);
1691de103c93SChristoph Hellwig 			ret = TCM_INVALID_PARAMETER_LIST;
1692de103c93SChristoph Hellwig 			goto out_unmap;
1693c66ac9dbSNicholas Bellinger 		}
1694c66ac9dbSNicholas Bellinger 
1695de103c93SChristoph Hellwig 		if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
16966708bb27SAndy Grover 			pr_err("core_scsi3_lunacl_depend_item()"
1697c66ac9dbSNicholas Bellinger 					" failed\n");
169829a05deeSNicholas Bellinger 			kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
1699c66ac9dbSNicholas Bellinger 			core_scsi3_nodeacl_undepend_item(dest_node_acl);
1700c66ac9dbSNicholas Bellinger 			core_scsi3_tpg_undepend_item(dest_tpg);
1701de103c93SChristoph Hellwig 			ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1702de103c93SChristoph Hellwig 			goto out_unmap;
1703c66ac9dbSNicholas Bellinger 		}
17048b1e1244SAndy Grover 
17056708bb27SAndy Grover 		pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1706f2d30680SHannes Reinecke 			" dest_se_deve mapped_lun: %llu\n",
170730c7ca93SDavid Disseldorp 			dest_tpg->se_tpg_tfo->fabric_name,
1708c66ac9dbSNicholas Bellinger 			dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
17098b1e1244SAndy Grover 
1710c66ac9dbSNicholas Bellinger 		/*
1711c66ac9dbSNicholas Bellinger 		 * Skip any TransportIDs that already have a registration for
1712c66ac9dbSNicholas Bellinger 		 * this target port.
1713c66ac9dbSNicholas Bellinger 		 */
1714c66ac9dbSNicholas Bellinger 		pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1715c66ac9dbSNicholas Bellinger 					iport_ptr);
1716c66ac9dbSNicholas Bellinger 		if (pr_reg_e) {
1717c66ac9dbSNicholas Bellinger 			core_scsi3_put_pr_reg(pr_reg_e);
1718c66ac9dbSNicholas Bellinger 			core_scsi3_lunacl_undepend_item(dest_se_deve);
1719c66ac9dbSNicholas Bellinger 			core_scsi3_nodeacl_undepend_item(dest_node_acl);
1720c66ac9dbSNicholas Bellinger 			core_scsi3_tpg_undepend_item(dest_tpg);
1721c66ac9dbSNicholas Bellinger 			ptr += tid_len;
1722c66ac9dbSNicholas Bellinger 			tpdl -= tid_len;
1723c66ac9dbSNicholas Bellinger 			tid_len = 0;
1724c66ac9dbSNicholas Bellinger 			continue;
1725c66ac9dbSNicholas Bellinger 		}
1726c66ac9dbSNicholas Bellinger 		/*
1727c66ac9dbSNicholas Bellinger 		 * Allocate a struct pr_transport_id_holder and setup
1728c66ac9dbSNicholas Bellinger 		 * the dest_node_acl and dest_se_deve pointers for the
1729c66ac9dbSNicholas Bellinger 		 * loop below.
1730c66ac9dbSNicholas Bellinger 		 */
1731c66ac9dbSNicholas Bellinger 		tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1732c66ac9dbSNicholas Bellinger 				GFP_KERNEL);
17336708bb27SAndy Grover 		if (!tidh_new) {
17346708bb27SAndy Grover 			pr_err("Unable to allocate tidh_new\n");
1735c66ac9dbSNicholas Bellinger 			core_scsi3_lunacl_undepend_item(dest_se_deve);
1736c66ac9dbSNicholas Bellinger 			core_scsi3_nodeacl_undepend_item(dest_node_acl);
1737c66ac9dbSNicholas Bellinger 			core_scsi3_tpg_undepend_item(dest_tpg);
1738de103c93SChristoph Hellwig 			ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1739de103c93SChristoph Hellwig 			goto out_unmap;
1740c66ac9dbSNicholas Bellinger 		}
1741c66ac9dbSNicholas Bellinger 		INIT_LIST_HEAD(&tidh_new->dest_list);
1742c66ac9dbSNicholas Bellinger 		tidh_new->dest_tpg = dest_tpg;
1743c66ac9dbSNicholas Bellinger 		tidh_new->dest_node_acl = dest_node_acl;
1744c66ac9dbSNicholas Bellinger 		tidh_new->dest_se_deve = dest_se_deve;
1745c66ac9dbSNicholas Bellinger 
1746c66ac9dbSNicholas Bellinger 		/*
1747c66ac9dbSNicholas Bellinger 		 * Allocate, but do NOT add the registration for the
1748c66ac9dbSNicholas Bellinger 		 * TransportID referenced SCSI Initiator port.  This
1749c66ac9dbSNicholas Bellinger 		 * done because of the following from spc4r17 in section
1750c66ac9dbSNicholas Bellinger 		 * 6.14.3 wrt SPEC_I_PT:
1751c66ac9dbSNicholas Bellinger 		 *
1752c66ac9dbSNicholas Bellinger 		 * "If a registration fails for any initiator port (e.g., if th
1753c66ac9dbSNicholas Bellinger 		 * logical unit does not have enough resources available to
1754c66ac9dbSNicholas Bellinger 		 * hold the registration information), no registrations shall be
1755c66ac9dbSNicholas Bellinger 		 * made, and the command shall be terminated with
1756c66ac9dbSNicholas Bellinger 		 * CHECK CONDITION status."
1757c66ac9dbSNicholas Bellinger 		 *
1758c66ac9dbSNicholas Bellinger 		 * That means we call __core_scsi3_alloc_registration() here,
1759c66ac9dbSNicholas Bellinger 		 * and then call __core_scsi3_add_registration() in the
1760c66ac9dbSNicholas Bellinger 		 * 2nd loop which will never fail.
1761c66ac9dbSNicholas Bellinger 		 */
176279dc9c9eSNicholas Bellinger 		dest_lun = rcu_dereference_check(dest_se_deve->se_lun,
17632c935bc5SPeter Zijlstra 				kref_read(&dest_se_deve->pr_kref) != 0);
176479dc9c9eSNicholas Bellinger 
17655951146dSAndy Grover 		dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
176679dc9c9eSNicholas Bellinger 					dest_node_acl, dest_lun, dest_se_deve,
176779dc9c9eSNicholas Bellinger 					dest_se_deve->mapped_lun, iport_ptr,
1768c66ac9dbSNicholas Bellinger 					sa_res_key, all_tg_pt, aptpl);
17696708bb27SAndy Grover 		if (!dest_pr_reg) {
1770c66ac9dbSNicholas Bellinger 			core_scsi3_lunacl_undepend_item(dest_se_deve);
1771c66ac9dbSNicholas Bellinger 			core_scsi3_nodeacl_undepend_item(dest_node_acl);
1772c66ac9dbSNicholas Bellinger 			core_scsi3_tpg_undepend_item(dest_tpg);
1773c66ac9dbSNicholas Bellinger 			kfree(tidh_new);
177479dd6f2fStangwenji 			ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1775de103c93SChristoph Hellwig 			goto out_unmap;
1776c66ac9dbSNicholas Bellinger 		}
1777c66ac9dbSNicholas Bellinger 		tidh_new->dest_pr_reg = dest_pr_reg;
1778c66ac9dbSNicholas Bellinger 		list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1779c66ac9dbSNicholas Bellinger 
1780c66ac9dbSNicholas Bellinger 		ptr += tid_len;
1781c66ac9dbSNicholas Bellinger 		tpdl -= tid_len;
1782c66ac9dbSNicholas Bellinger 		tid_len = 0;
1783c66ac9dbSNicholas Bellinger 
1784c66ac9dbSNicholas Bellinger 	}
178505d1c7c0SAndy Grover 
17864949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
178705d1c7c0SAndy Grover 
1788c66ac9dbSNicholas Bellinger 	/*
1789c66ac9dbSNicholas Bellinger 	 * Go ahead and create a registrations from tid_dest_list for the
1790c66ac9dbSNicholas Bellinger 	 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1791c66ac9dbSNicholas Bellinger 	 * and dest_se_deve.
1792c66ac9dbSNicholas Bellinger 	 *
1793c66ac9dbSNicholas Bellinger 	 * The SA Reservation Key from the PROUT is set for the
1794c66ac9dbSNicholas Bellinger 	 * registration, and ALL_TG_PT is also passed.  ALL_TG_PT=1
1795c66ac9dbSNicholas Bellinger 	 * means that the TransportID Initiator port will be
1796c66ac9dbSNicholas Bellinger 	 * registered on all of the target ports in the SCSI target device
1797c66ac9dbSNicholas Bellinger 	 * ALL_TG_PT=0 means the registration will only be for the
1798c66ac9dbSNicholas Bellinger 	 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1799c66ac9dbSNicholas Bellinger 	 * was received.
1800c66ac9dbSNicholas Bellinger 	 */
1801c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1802c66ac9dbSNicholas Bellinger 		dest_tpg = tidh->dest_tpg;
1803c66ac9dbSNicholas Bellinger 		dest_node_acl = tidh->dest_node_acl;
1804c66ac9dbSNicholas Bellinger 		dest_se_deve = tidh->dest_se_deve;
1805c66ac9dbSNicholas Bellinger 		dest_pr_reg = tidh->dest_pr_reg;
1806c66ac9dbSNicholas Bellinger 
1807c66ac9dbSNicholas Bellinger 		list_del(&tidh->dest_list);
1808c66ac9dbSNicholas Bellinger 		kfree(tidh);
1809c66ac9dbSNicholas Bellinger 
1810c66ac9dbSNicholas Bellinger 		memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1811d2843c17SAndy Grover 		core_pr_dump_initiator_port(dest_pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1812c66ac9dbSNicholas Bellinger 
18135951146dSAndy Grover 		__core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
1814c66ac9dbSNicholas Bellinger 					dest_pr_reg, 0, 0);
1815c66ac9dbSNicholas Bellinger 
18166708bb27SAndy Grover 		pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1817c66ac9dbSNicholas Bellinger 			" registered Transport ID for Node: %s%s Mapped LUN:"
181830c7ca93SDavid Disseldorp 			" %llu\n", dest_tpg->se_tpg_tfo->fabric_name,
181979dc9c9eSNicholas Bellinger 			dest_node_acl->initiatorname, i_buf, (dest_se_deve) ?
182079dc9c9eSNicholas Bellinger 			dest_se_deve->mapped_lun : 0);
1821c66ac9dbSNicholas Bellinger 
18223ccd6e83SNicholas Bellinger 		if (!dest_se_deve) {
18233ccd6e83SNicholas Bellinger 			kref_put(&local_pr_reg->pr_reg_deve->pr_kref,
18243ccd6e83SNicholas Bellinger 				 target_pr_kref_release);
1825c66ac9dbSNicholas Bellinger 			continue;
18263ccd6e83SNicholas Bellinger 		}
1827c66ac9dbSNicholas Bellinger 		core_scsi3_lunacl_undepend_item(dest_se_deve);
1828c66ac9dbSNicholas Bellinger 		core_scsi3_nodeacl_undepend_item(dest_node_acl);
1829c66ac9dbSNicholas Bellinger 		core_scsi3_tpg_undepend_item(dest_tpg);
1830c66ac9dbSNicholas Bellinger 	}
1831c66ac9dbSNicholas Bellinger 
1832c66ac9dbSNicholas Bellinger 	return 0;
1833de103c93SChristoph Hellwig out_unmap:
18344949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
1835de103c93SChristoph Hellwig out:
1836c66ac9dbSNicholas Bellinger 	/*
1837c66ac9dbSNicholas Bellinger 	 * For the failure case, release everything from tid_dest_list
1838c66ac9dbSNicholas Bellinger 	 * including *dest_pr_reg and the configfs dependances..
1839c66ac9dbSNicholas Bellinger 	 */
1840c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1841c66ac9dbSNicholas Bellinger 		dest_tpg = tidh->dest_tpg;
1842c66ac9dbSNicholas Bellinger 		dest_node_acl = tidh->dest_node_acl;
1843c66ac9dbSNicholas Bellinger 		dest_se_deve = tidh->dest_se_deve;
1844c66ac9dbSNicholas Bellinger 		dest_pr_reg = tidh->dest_pr_reg;
1845c66ac9dbSNicholas Bellinger 
1846c66ac9dbSNicholas Bellinger 		list_del(&tidh->dest_list);
1847c66ac9dbSNicholas Bellinger 		kfree(tidh);
1848c66ac9dbSNicholas Bellinger 		/*
1849c66ac9dbSNicholas Bellinger 		 * Release any extra ALL_TG_PT=1 registrations for
1850c66ac9dbSNicholas Bellinger 		 * the SPEC_I_PT=1 case.
1851c66ac9dbSNicholas Bellinger 		 */
1852c66ac9dbSNicholas Bellinger 		list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1853c66ac9dbSNicholas Bellinger 				&dest_pr_reg->pr_reg_atp_list,
1854c66ac9dbSNicholas Bellinger 				pr_reg_atp_mem_list) {
1855c66ac9dbSNicholas Bellinger 			list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1856c66ac9dbSNicholas Bellinger 			core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1857c66ac9dbSNicholas Bellinger 			kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1858c66ac9dbSNicholas Bellinger 		}
1859c66ac9dbSNicholas Bellinger 
1860c66ac9dbSNicholas Bellinger 		kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1861c66ac9dbSNicholas Bellinger 
18623ccd6e83SNicholas Bellinger 		if (!dest_se_deve) {
18633ccd6e83SNicholas Bellinger 			kref_put(&local_pr_reg->pr_reg_deve->pr_kref,
18643ccd6e83SNicholas Bellinger 				 target_pr_kref_release);
1865c66ac9dbSNicholas Bellinger 			continue;
18663ccd6e83SNicholas Bellinger 		}
1867c66ac9dbSNicholas Bellinger 		core_scsi3_lunacl_undepend_item(dest_se_deve);
1868c66ac9dbSNicholas Bellinger 		core_scsi3_nodeacl_undepend_item(dest_node_acl);
1869c66ac9dbSNicholas Bellinger 		core_scsi3_tpg_undepend_item(dest_tpg);
1870c66ac9dbSNicholas Bellinger 	}
1871c66ac9dbSNicholas Bellinger 	return ret;
1872c66ac9dbSNicholas Bellinger }
1873c66ac9dbSNicholas Bellinger 
18743c8a6228SAndy Grover static int core_scsi3_update_aptpl_buf(
1875c66ac9dbSNicholas Bellinger 	struct se_device *dev,
1876c66ac9dbSNicholas Bellinger 	unsigned char *buf,
18770607decdSAndy Grover 	u32 pr_aptpl_buf_len)
1878c66ac9dbSNicholas Bellinger {
1879c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg;
1880c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1881c66ac9dbSNicholas Bellinger 	unsigned char tmp[512], isid_buf[32];
1882c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1883c66ac9dbSNicholas Bellinger 	int reg_count = 0;
18843c8a6228SAndy Grover 	int ret = 0;
1885c66ac9dbSNicholas Bellinger 
18863c8a6228SAndy Grover 	spin_lock(&dev->dev_reservation_lock);
18873c8a6228SAndy Grover 	spin_lock(&dev->t10_pr.registration_lock);
1888c66ac9dbSNicholas Bellinger 	/*
1889c66ac9dbSNicholas Bellinger 	 * Walk the registration list..
1890c66ac9dbSNicholas Bellinger 	 */
18910fd97ccfSChristoph Hellwig 	list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1892c66ac9dbSNicholas Bellinger 			pr_reg_list) {
1893c66ac9dbSNicholas Bellinger 
1894c66ac9dbSNicholas Bellinger 		tmp[0] = '\0';
1895c66ac9dbSNicholas Bellinger 		isid_buf[0] = '\0';
1896c66ac9dbSNicholas Bellinger 		tpg = pr_reg->pr_reg_nacl->se_tpg;
1897c66ac9dbSNicholas Bellinger 		/*
1898c66ac9dbSNicholas Bellinger 		 * Write out any ISID value to APTPL metadata that was included
1899c66ac9dbSNicholas Bellinger 		 * in the original registration.
1900c66ac9dbSNicholas Bellinger 		 */
1901c66ac9dbSNicholas Bellinger 		if (pr_reg->isid_present_at_reg)
1902c66ac9dbSNicholas Bellinger 			snprintf(isid_buf, 32, "initiator_sid=%s\n",
1903c66ac9dbSNicholas Bellinger 					pr_reg->pr_reg_isid);
1904c66ac9dbSNicholas Bellinger 		/*
1905c66ac9dbSNicholas Bellinger 		 * Include special metadata if the pr_reg matches the
1906c66ac9dbSNicholas Bellinger 		 * reservation holder.
1907c66ac9dbSNicholas Bellinger 		 */
1908c66ac9dbSNicholas Bellinger 		if (dev->dev_pr_res_holder == pr_reg) {
1909c66ac9dbSNicholas Bellinger 			snprintf(tmp, 512, "PR_REG_START: %d"
1910c66ac9dbSNicholas Bellinger 				"\ninitiator_fabric=%s\n"
1911c66ac9dbSNicholas Bellinger 				"initiator_node=%s\n%s"
1912c66ac9dbSNicholas Bellinger 				"sa_res_key=%llu\n"
1913c66ac9dbSNicholas Bellinger 				"res_holder=1\nres_type=%02x\n"
1914c66ac9dbSNicholas Bellinger 				"res_scope=%02x\nres_all_tg_pt=%d\n"
1915f2d30680SHannes Reinecke 				"mapped_lun=%llu\n", reg_count,
191630c7ca93SDavid Disseldorp 				tpg->se_tpg_tfo->fabric_name,
1917c66ac9dbSNicholas Bellinger 				pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1918c66ac9dbSNicholas Bellinger 				pr_reg->pr_res_key, pr_reg->pr_res_type,
1919c66ac9dbSNicholas Bellinger 				pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1920c66ac9dbSNicholas Bellinger 				pr_reg->pr_res_mapped_lun);
1921c66ac9dbSNicholas Bellinger 		} else {
1922c66ac9dbSNicholas Bellinger 			snprintf(tmp, 512, "PR_REG_START: %d\n"
1923c66ac9dbSNicholas Bellinger 				"initiator_fabric=%s\ninitiator_node=%s\n%s"
1924c66ac9dbSNicholas Bellinger 				"sa_res_key=%llu\nres_holder=0\n"
1925f2d30680SHannes Reinecke 				"res_all_tg_pt=%d\nmapped_lun=%llu\n",
192630c7ca93SDavid Disseldorp 				reg_count, tpg->se_tpg_tfo->fabric_name,
1927c66ac9dbSNicholas Bellinger 				pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1928c66ac9dbSNicholas Bellinger 				pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1929c66ac9dbSNicholas Bellinger 				pr_reg->pr_res_mapped_lun);
1930c66ac9dbSNicholas Bellinger 		}
1931c66ac9dbSNicholas Bellinger 
193260d645a4SDan Carpenter 		if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1933f161d4b4SNicholas Bellinger 			pr_err("Unable to update renaming APTPL metadata,"
1934f161d4b4SNicholas Bellinger 			       " reallocating larger buffer\n");
19353c8a6228SAndy Grover 			ret = -EMSGSIZE;
19363c8a6228SAndy Grover 			goto out;
1937c66ac9dbSNicholas Bellinger 		}
1938c66ac9dbSNicholas Bellinger 		len += sprintf(buf+len, "%s", tmp);
1939c66ac9dbSNicholas Bellinger 
1940c66ac9dbSNicholas Bellinger 		/*
1941c66ac9dbSNicholas Bellinger 		 * Include information about the associated SCSI target port.
1942c66ac9dbSNicholas Bellinger 		 */
1943c66ac9dbSNicholas Bellinger 		snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1944f2d30680SHannes Reinecke 			"tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%llu\nPR_REG_END:"
194530c7ca93SDavid Disseldorp 			" %d\n", tpg->se_tpg_tfo->fabric_name,
1946e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1947e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_tag(tpg),
194879dc9c9eSNicholas Bellinger 			pr_reg->tg_pt_sep_rtpi, pr_reg->pr_aptpl_target_lun,
194979dc9c9eSNicholas Bellinger 			reg_count);
1950c66ac9dbSNicholas Bellinger 
195160d645a4SDan Carpenter 		if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1952f161d4b4SNicholas Bellinger 			pr_err("Unable to update renaming APTPL metadata,"
1953f161d4b4SNicholas Bellinger 			       " reallocating larger buffer\n");
19543c8a6228SAndy Grover 			ret = -EMSGSIZE;
19553c8a6228SAndy Grover 			goto out;
1956c66ac9dbSNicholas Bellinger 		}
1957c66ac9dbSNicholas Bellinger 		len += sprintf(buf+len, "%s", tmp);
1958c66ac9dbSNicholas Bellinger 		reg_count++;
1959c66ac9dbSNicholas Bellinger 	}
1960c66ac9dbSNicholas Bellinger 
19616708bb27SAndy Grover 	if (!reg_count)
1962c66ac9dbSNicholas Bellinger 		len += sprintf(buf+len, "No Registrations or Reservations");
1963c66ac9dbSNicholas Bellinger 
19643c8a6228SAndy Grover out:
19653c8a6228SAndy Grover 	spin_unlock(&dev->t10_pr.registration_lock);
1966c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
1967c66ac9dbSNicholas Bellinger 
1968c66ac9dbSNicholas Bellinger 	return ret;
1969c66ac9dbSNicholas Bellinger }
1970c66ac9dbSNicholas Bellinger 
1971c66ac9dbSNicholas Bellinger static int __core_scsi3_write_aptpl_to_file(
1972c66ac9dbSNicholas Bellinger 	struct se_device *dev,
19734dee96fbSAndy Grover 	unsigned char *buf)
1974c66ac9dbSNicholas Bellinger {
19750fd97ccfSChristoph Hellwig 	struct t10_wwn *wwn = &dev->t10_wwn;
1976c66ac9dbSNicholas Bellinger 	struct file *file;
1977c66ac9dbSNicholas Bellinger 	int flags = O_RDWR | O_CREAT | O_TRUNC;
1978bdc79f0eSDavid Disseldorp 	char *path;
19794dee96fbSAndy Grover 	u32 pr_aptpl_buf_len;
1980c66ac9dbSNicholas Bellinger 	int ret;
1981e13ec939SChristoph Hellwig 	loff_t pos = 0;
1982c66ac9dbSNicholas Bellinger 
1983bdc79f0eSDavid Disseldorp 	path = kasprintf(GFP_KERNEL, "%s/pr/aptpl_%s", db_root,
1984bdc79f0eSDavid Disseldorp 			&wwn->unit_serial[0]);
1985bdc79f0eSDavid Disseldorp 	if (!path)
1986bdc79f0eSDavid Disseldorp 		return -ENOMEM;
1987c66ac9dbSNicholas Bellinger 
1988c66ac9dbSNicholas Bellinger 	file = filp_open(path, flags, 0600);
19890e9b10a9SAl Viro 	if (IS_ERR(file)) {
19906708bb27SAndy Grover 		pr_err("filp_open(%s) for APTPL metadata"
1991c66ac9dbSNicholas Bellinger 			" failed\n", path);
1992bdc79f0eSDavid Disseldorp 		kfree(path);
19930e9b10a9SAl Viro 		return PTR_ERR(file);
1994c66ac9dbSNicholas Bellinger 	}
1995c66ac9dbSNicholas Bellinger 
19964dee96fbSAndy Grover 	pr_aptpl_buf_len = (strlen(buf) + 1); /* Add extra for NULL */
1997c66ac9dbSNicholas Bellinger 
1998e13ec939SChristoph Hellwig 	ret = kernel_write(file, buf, pr_aptpl_buf_len, &pos);
1999c66ac9dbSNicholas Bellinger 
20000e9b10a9SAl Viro 	if (ret < 0)
20016708bb27SAndy Grover 		pr_debug("Error writing APTPL metadata file: %s\n", path);
20020e9b10a9SAl Viro 	fput(file);
2003bdc79f0eSDavid Disseldorp 	kfree(path);
2004c66ac9dbSNicholas Bellinger 
2005f730f915SGera Kazakov 	return (ret < 0) ? -EIO : 0;
2006c66ac9dbSNicholas Bellinger }
2007c66ac9dbSNicholas Bellinger 
2008c66ac9dbSNicholas Bellinger /*
2009459f213bSAndy Grover  * Clear the APTPL metadata if APTPL has been disabled, otherwise
2010459f213bSAndy Grover  * write out the updated metadata to struct file for this SCSI device.
2011c66ac9dbSNicholas Bellinger  */
20128a391857SNicholas Bellinger static sense_reason_t core_scsi3_update_and_write_aptpl(struct se_device *dev, bool aptpl)
2013459f213bSAndy Grover {
20148a391857SNicholas Bellinger 	unsigned char *buf;
2015f161d4b4SNicholas Bellinger 	int rc, len = PR_APTPL_BUF_LEN;
2016459f213bSAndy Grover 
2017459f213bSAndy Grover 	if (!aptpl) {
2018459f213bSAndy Grover 		char *null_buf = "No Registrations or Reservations\n";
2019459f213bSAndy Grover 
20208a391857SNicholas Bellinger 		rc = __core_scsi3_write_aptpl_to_file(dev, null_buf);
2021459f213bSAndy Grover 		dev->t10_pr.pr_aptpl_active = 0;
2022459f213bSAndy Grover 		pr_debug("SPC-3 PR: Set APTPL Bit Deactivated\n");
20238a391857SNicholas Bellinger 
20248a391857SNicholas Bellinger 		if (rc)
20258a391857SNicholas Bellinger 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
20268a391857SNicholas Bellinger 
20278a391857SNicholas Bellinger 		return 0;
20288a391857SNicholas Bellinger 	}
2029f161d4b4SNicholas Bellinger retry:
2030f161d4b4SNicholas Bellinger 	buf = vzalloc(len);
2031459f213bSAndy Grover 	if (!buf)
20328a391857SNicholas Bellinger 		return TCM_OUT_OF_RESOURCES;
2033459f213bSAndy Grover 
2034f161d4b4SNicholas Bellinger 	rc = core_scsi3_update_aptpl_buf(dev, buf, len);
20358a391857SNicholas Bellinger 	if (rc < 0) {
2036f161d4b4SNicholas Bellinger 		vfree(buf);
2037f161d4b4SNicholas Bellinger 		len *= 2;
2038f161d4b4SNicholas Bellinger 		goto retry;
20390607decdSAndy Grover 	}
2040de103c93SChristoph Hellwig 
20418a391857SNicholas Bellinger 	rc = __core_scsi3_write_aptpl_to_file(dev, buf);
20428a391857SNicholas Bellinger 	if (rc != 0) {
2043459f213bSAndy Grover 		pr_err("SPC-3 PR: Could not update APTPL\n");
2044f161d4b4SNicholas Bellinger 		vfree(buf);
20458a391857SNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2046459f213bSAndy Grover 	}
20478a391857SNicholas Bellinger 	dev->t10_pr.pr_aptpl_active = 1;
2048f161d4b4SNicholas Bellinger 	vfree(buf);
20498a391857SNicholas Bellinger 	pr_debug("SPC-3 PR: Set APTPL Bit Activated\n");
20508a391857SNicholas Bellinger 	return 0;
2051c66ac9dbSNicholas Bellinger }
2052c66ac9dbSNicholas Bellinger 
2053de103c93SChristoph Hellwig static sense_reason_t
2054de103c93SChristoph Hellwig core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
2055bc118fe4SAndy Grover 		bool aptpl, bool all_tg_pt, bool spec_i_pt, enum register_type register_type)
2056c66ac9dbSNicholas Bellinger {
2057e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
20585951146dSAndy Grover 	struct se_device *dev = cmd->se_dev;
2059e3d6f909SAndy Grover 	struct se_lun *se_lun = cmd->se_lun;
2060c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg;
2061bc118fe4SAndy Grover 	struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp;
20620fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
2063c66ac9dbSNicholas Bellinger 	unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
2064a0d50f62SHannes Reinecke 	sense_reason_t ret = TCM_NO_SENSE;
2065fc09149dSNicholas Bellinger 	int pr_holder = 0, type;
2066c66ac9dbSNicholas Bellinger 
20676708bb27SAndy Grover 	if (!se_sess || !se_lun) {
20686708bb27SAndy Grover 		pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2069de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2070c66ac9dbSNicholas Bellinger 	}
2071c66ac9dbSNicholas Bellinger 	se_tpg = se_sess->se_tpg;
2072c66ac9dbSNicholas Bellinger 
2073e3d6f909SAndy Grover 	if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
2074c66ac9dbSNicholas Bellinger 		memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
2075e3d6f909SAndy Grover 		se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
2076c66ac9dbSNicholas Bellinger 				PR_REG_ISID_LEN);
2077c66ac9dbSNicholas Bellinger 		isid_ptr = &isid_buf[0];
2078c66ac9dbSNicholas Bellinger 	}
2079c66ac9dbSNicholas Bellinger 	/*
2080c66ac9dbSNicholas Bellinger 	 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2081c66ac9dbSNicholas Bellinger 	 */
2082bc118fe4SAndy Grover 	pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2083bc118fe4SAndy Grover 	if (!pr_reg) {
2084c66ac9dbSNicholas Bellinger 		if (res_key) {
20856708bb27SAndy Grover 			pr_warn("SPC-3 PR: Reservation Key non-zero"
2086c66ac9dbSNicholas Bellinger 				" for SA REGISTER, returning CONFLICT\n");
2087de103c93SChristoph Hellwig 			return TCM_RESERVATION_CONFLICT;
2088c66ac9dbSNicholas Bellinger 		}
2089c66ac9dbSNicholas Bellinger 		/*
2090c66ac9dbSNicholas Bellinger 		 * Do nothing but return GOOD status.
2091c66ac9dbSNicholas Bellinger 		 */
20926708bb27SAndy Grover 		if (!sa_res_key)
209303e98c9eSNicholas Bellinger 			return 0;
2094c66ac9dbSNicholas Bellinger 
20956708bb27SAndy Grover 		if (!spec_i_pt) {
2096c66ac9dbSNicholas Bellinger 			/*
2097c66ac9dbSNicholas Bellinger 			 * Perform the Service Action REGISTER on the Initiator
2098c66ac9dbSNicholas Bellinger 			 * Port Endpoint that the PRO was received from on the
2099c66ac9dbSNicholas Bellinger 			 * Logical Unit of the SCSI device server.
2100c66ac9dbSNicholas Bellinger 			 */
2101de103c93SChristoph Hellwig 			if (core_scsi3_alloc_registration(cmd->se_dev,
210279dc9c9eSNicholas Bellinger 					se_sess->se_node_acl, cmd->se_lun,
210379dc9c9eSNicholas Bellinger 					NULL, cmd->orig_fe_lun, isid_ptr,
2104c66ac9dbSNicholas Bellinger 					sa_res_key, all_tg_pt, aptpl,
210533ce6a87SAndy Grover 					register_type, 0)) {
21066708bb27SAndy Grover 				pr_err("Unable to allocate"
2107c66ac9dbSNicholas Bellinger 					" struct t10_pr_registration\n");
210879dd6f2fStangwenji 				return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
2109c66ac9dbSNicholas Bellinger 			}
2110c66ac9dbSNicholas Bellinger 		} else {
2111c66ac9dbSNicholas Bellinger 			/*
2112c66ac9dbSNicholas Bellinger 			 * Register both the Initiator port that received
2113c66ac9dbSNicholas Bellinger 			 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2114c66ac9dbSNicholas Bellinger 			 * TransportID from Parameter list and loop through
2115c66ac9dbSNicholas Bellinger 			 * fabric dependent parameter list while calling
2116c66ac9dbSNicholas Bellinger 			 * logic from of core_scsi3_alloc_registration() for
2117c66ac9dbSNicholas Bellinger 			 * each TransportID provided SCSI Initiator Port/Device
2118c66ac9dbSNicholas Bellinger 			 */
2119c66ac9dbSNicholas Bellinger 			ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2120c66ac9dbSNicholas Bellinger 					isid_ptr, sa_res_key, all_tg_pt, aptpl);
2121c66ac9dbSNicholas Bellinger 			if (ret != 0)
2122c66ac9dbSNicholas Bellinger 				return ret;
2123c66ac9dbSNicholas Bellinger 		}
2124459f213bSAndy Grover 		return core_scsi3_update_and_write_aptpl(dev, aptpl);
2125de103c93SChristoph Hellwig 	}
2126de103c93SChristoph Hellwig 
2127bc118fe4SAndy Grover 	/* ok, existing registration */
2128c66ac9dbSNicholas Bellinger 
2129bc118fe4SAndy Grover 	if ((register_type == REGISTER) && (res_key != pr_reg->pr_res_key)) {
21306708bb27SAndy Grover 		pr_err("SPC-3 PR REGISTER: Received"
2131c66ac9dbSNicholas Bellinger 		       " res_key: 0x%016Lx does not match"
2132c66ac9dbSNicholas Bellinger 		       " existing SA REGISTER res_key:"
2133c66ac9dbSNicholas Bellinger 		       " 0x%016Lx\n", res_key,
2134c66ac9dbSNicholas Bellinger 		       pr_reg->pr_res_key);
2135de103c93SChristoph Hellwig 		ret = TCM_RESERVATION_CONFLICT;
2136bc118fe4SAndy Grover 		goto out;
2137c66ac9dbSNicholas Bellinger 	}
2138de103c93SChristoph Hellwig 
2139c66ac9dbSNicholas Bellinger 	if (spec_i_pt) {
21401f070cc2SAndy Grover 		pr_err("SPC-3 PR REGISTER: SPEC_I_PT"
21411f070cc2SAndy Grover 			" set on a registered nexus\n");
2142de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
2143bc118fe4SAndy Grover 		goto out;
2144c66ac9dbSNicholas Bellinger 	}
2145de103c93SChristoph Hellwig 
2146c66ac9dbSNicholas Bellinger 	/*
2147c66ac9dbSNicholas Bellinger 	 * An existing ALL_TG_PT=1 registration being released
2148c66ac9dbSNicholas Bellinger 	 * must also set ALL_TG_PT=1 in the incoming PROUT.
2149c66ac9dbSNicholas Bellinger 	 */
21501f070cc2SAndy Grover 	if (pr_reg->pr_reg_all_tg_pt && !all_tg_pt) {
21511f070cc2SAndy Grover 		pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1"
2152c66ac9dbSNicholas Bellinger 			" registration exists, but ALL_TG_PT=1 bit not"
2153c66ac9dbSNicholas Bellinger 			" present in received PROUT\n");
2154de103c93SChristoph Hellwig 		ret = TCM_INVALID_CDB_FIELD;
2155bc118fe4SAndy Grover 		goto out;
2156c66ac9dbSNicholas Bellinger 	}
2157de103c93SChristoph Hellwig 
2158c66ac9dbSNicholas Bellinger 	/*
215951d9c41dSAndy Grover 	 * sa_res_key=1 Change Reservation Key for registered I_T Nexus.
2160c66ac9dbSNicholas Bellinger 	 */
216151d9c41dSAndy Grover 	if (sa_res_key) {
216251d9c41dSAndy Grover 		/*
216351d9c41dSAndy Grover 		 * Increment PRgeneration counter for struct se_device"
216451d9c41dSAndy Grover 		 * upon a successful REGISTER, see spc4r17 section 6.3.2
216551d9c41dSAndy Grover 		 * READ_KEYS service action.
216651d9c41dSAndy Grover 		 */
216751d9c41dSAndy Grover 		pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev);
216851d9c41dSAndy Grover 		pr_reg->pr_res_key = sa_res_key;
216951d9c41dSAndy Grover 		pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
217051d9c41dSAndy Grover 			 " Key for %s to: 0x%016Lx PRgeneration:"
217130c7ca93SDavid Disseldorp 			 " 0x%08x\n", cmd->se_tfo->fabric_name,
217251d9c41dSAndy Grover 			 (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? "_AND_IGNORE_EXISTING_KEY" : "",
217351d9c41dSAndy Grover 			 pr_reg->pr_reg_nacl->initiatorname,
217451d9c41dSAndy Grover 			 pr_reg->pr_res_key, pr_reg->pr_res_generation);
217551d9c41dSAndy Grover 
217651d9c41dSAndy Grover 	} else {
217751d9c41dSAndy Grover 		/*
217851d9c41dSAndy Grover 		 * sa_res_key=0 Unregister Reservation Key for registered I_T Nexus.
217951d9c41dSAndy Grover 		 */
21806c3c9baaSNicholas Bellinger 		type = pr_reg->pr_res_type;
21816c3c9baaSNicholas Bellinger 		pr_holder = core_scsi3_check_implicit_release(cmd->se_dev,
21826c3c9baaSNicholas Bellinger 							      pr_reg);
2183c66ac9dbSNicholas Bellinger 		if (pr_holder < 0) {
2184de103c93SChristoph Hellwig 			ret = TCM_RESERVATION_CONFLICT;
2185bc118fe4SAndy Grover 			goto out;
2186c66ac9dbSNicholas Bellinger 		}
2187c66ac9dbSNicholas Bellinger 
2188c66ac9dbSNicholas Bellinger 		spin_lock(&pr_tmpl->registration_lock);
2189c66ac9dbSNicholas Bellinger 		/*
2190c66ac9dbSNicholas Bellinger 		 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2191c66ac9dbSNicholas Bellinger 		 * and matching pr_res_key.
2192c66ac9dbSNicholas Bellinger 		 */
2193c66ac9dbSNicholas Bellinger 		if (pr_reg->pr_reg_all_tg_pt) {
2194c66ac9dbSNicholas Bellinger 			list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2195c66ac9dbSNicholas Bellinger 					&pr_tmpl->registration_list,
2196c66ac9dbSNicholas Bellinger 					pr_reg_list) {
2197c66ac9dbSNicholas Bellinger 
21986708bb27SAndy Grover 				if (!pr_reg_p->pr_reg_all_tg_pt)
2199c66ac9dbSNicholas Bellinger 					continue;
2200c66ac9dbSNicholas Bellinger 				if (pr_reg_p->pr_res_key != res_key)
2201c66ac9dbSNicholas Bellinger 					continue;
2202c66ac9dbSNicholas Bellinger 				if (pr_reg == pr_reg_p)
2203c66ac9dbSNicholas Bellinger 					continue;
2204c66ac9dbSNicholas Bellinger 				if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2205c66ac9dbSNicholas Bellinger 					   pr_reg_p->pr_reg_nacl->initiatorname))
2206c66ac9dbSNicholas Bellinger 					continue;
2207c66ac9dbSNicholas Bellinger 
2208c66ac9dbSNicholas Bellinger 				__core_scsi3_free_registration(dev,
2209c66ac9dbSNicholas Bellinger 						pr_reg_p, NULL, 0);
2210c66ac9dbSNicholas Bellinger 			}
2211c66ac9dbSNicholas Bellinger 		}
2212de103c93SChristoph Hellwig 
2213c66ac9dbSNicholas Bellinger 		/*
2214c66ac9dbSNicholas Bellinger 		 * Release the calling I_T Nexus registration now..
2215c66ac9dbSNicholas Bellinger 		 */
2216de103c93SChristoph Hellwig 		__core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1);
2217fc09149dSNicholas Bellinger 		pr_reg = NULL;
2218de103c93SChristoph Hellwig 
2219c66ac9dbSNicholas Bellinger 		/*
2220c66ac9dbSNicholas Bellinger 		 * From spc4r17, section 5.7.11.3 Unregistering
2221c66ac9dbSNicholas Bellinger 		 *
2222c66ac9dbSNicholas Bellinger 		 * If the persistent reservation is a registrants only
2223c66ac9dbSNicholas Bellinger 		 * type, the device server shall establish a unit
2224c66ac9dbSNicholas Bellinger 		 * attention condition for the initiator port associated
2225c66ac9dbSNicholas Bellinger 		 * with every registered I_T nexus except for the I_T
2226c66ac9dbSNicholas Bellinger 		 * nexus on which the PERSISTENT RESERVE OUT command was
2227c66ac9dbSNicholas Bellinger 		 * received, with the additional sense code set to
2228c66ac9dbSNicholas Bellinger 		 * RESERVATIONS RELEASED.
2229c66ac9dbSNicholas Bellinger 		 */
2230c66ac9dbSNicholas Bellinger 		if (pr_holder &&
2231fc09149dSNicholas Bellinger 		    (type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY ||
2232fc09149dSNicholas Bellinger 		     type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) {
2233c66ac9dbSNicholas Bellinger 			list_for_each_entry(pr_reg_p,
2234c66ac9dbSNicholas Bellinger 					&pr_tmpl->registration_list,
2235c66ac9dbSNicholas Bellinger 					pr_reg_list) {
2236c66ac9dbSNicholas Bellinger 
2237c51c8e7bSHannes Reinecke 				target_ua_allocate_lun(
2238c66ac9dbSNicholas Bellinger 					pr_reg_p->pr_reg_nacl,
2239c66ac9dbSNicholas Bellinger 					pr_reg_p->pr_res_mapped_lun,
2240c66ac9dbSNicholas Bellinger 					0x2A,
2241c66ac9dbSNicholas Bellinger 					ASCQ_2AH_RESERVATIONS_RELEASED);
2242c66ac9dbSNicholas Bellinger 			}
2243c66ac9dbSNicholas Bellinger 		}
224451d9c41dSAndy Grover 
2245c66ac9dbSNicholas Bellinger 		spin_unlock(&pr_tmpl->registration_lock);
2246de103c93SChristoph Hellwig 	}
2247de103c93SChristoph Hellwig 
2248459f213bSAndy Grover 	ret = core_scsi3_update_and_write_aptpl(dev, aptpl);
2249c66ac9dbSNicholas Bellinger 
2250bc118fe4SAndy Grover out:
2251fc09149dSNicholas Bellinger 	if (pr_reg)
2252c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg);
2253de103c93SChristoph Hellwig 	return ret;
2254c66ac9dbSNicholas Bellinger }
2255c66ac9dbSNicholas Bellinger 
2256c66ac9dbSNicholas Bellinger unsigned char *core_scsi3_pr_dump_type(int type)
2257c66ac9dbSNicholas Bellinger {
2258c66ac9dbSNicholas Bellinger 	switch (type) {
2259c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE:
2260c66ac9dbSNicholas Bellinger 		return "Write Exclusive Access";
2261c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS:
2262c66ac9dbSNicholas Bellinger 		return "Exclusive Access";
2263c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2264c66ac9dbSNicholas Bellinger 		return "Write Exclusive Access, Registrants Only";
2265c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2266c66ac9dbSNicholas Bellinger 		return "Exclusive Access, Registrants Only";
2267c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2268c66ac9dbSNicholas Bellinger 		return "Write Exclusive Access, All Registrants";
2269c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2270c66ac9dbSNicholas Bellinger 		return "Exclusive Access, All Registrants";
2271c66ac9dbSNicholas Bellinger 	default:
2272c66ac9dbSNicholas Bellinger 		break;
2273c66ac9dbSNicholas Bellinger 	}
2274c66ac9dbSNicholas Bellinger 
2275c66ac9dbSNicholas Bellinger 	return "Unknown SPC-3 PR Type";
2276c66ac9dbSNicholas Bellinger }
2277c66ac9dbSNicholas Bellinger 
2278de103c93SChristoph Hellwig static sense_reason_t
2279de103c93SChristoph Hellwig core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key)
2280c66ac9dbSNicholas Bellinger {
2281de103c93SChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
2282e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
2283e3d6f909SAndy Grover 	struct se_lun *se_lun = cmd->se_lun;
2284c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_res_holder;
22850fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
2286c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
2287de103c93SChristoph Hellwig 	sense_reason_t ret;
2288c66ac9dbSNicholas Bellinger 
2289c66ac9dbSNicholas Bellinger 	memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2290c66ac9dbSNicholas Bellinger 
22916708bb27SAndy Grover 	if (!se_sess || !se_lun) {
22926708bb27SAndy Grover 		pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2293de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2294c66ac9dbSNicholas Bellinger 	}
2295c66ac9dbSNicholas Bellinger 	/*
2296c66ac9dbSNicholas Bellinger 	 * Locate the existing *pr_reg via struct se_node_acl pointers
2297c66ac9dbSNicholas Bellinger 	 */
22985951146dSAndy Grover 	pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2299c66ac9dbSNicholas Bellinger 				se_sess);
23006708bb27SAndy Grover 	if (!pr_reg) {
23016708bb27SAndy Grover 		pr_err("SPC-3 PR: Unable to locate"
2302c66ac9dbSNicholas Bellinger 			" PR_REGISTERED *pr_reg for RESERVE\n");
2303de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2304c66ac9dbSNicholas Bellinger 	}
2305c66ac9dbSNicholas Bellinger 	/*
2306c66ac9dbSNicholas Bellinger 	 * From spc4r17 Section 5.7.9: Reserving:
2307c66ac9dbSNicholas Bellinger 	 *
2308c66ac9dbSNicholas Bellinger 	 * An application client creates a persistent reservation by issuing
2309c66ac9dbSNicholas Bellinger 	 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2310c66ac9dbSNicholas Bellinger 	 * a registered I_T nexus with the following parameters:
2311c66ac9dbSNicholas Bellinger 	 *    a) RESERVATION KEY set to the value of the reservation key that is
2312c66ac9dbSNicholas Bellinger 	 * 	 registered with the logical unit for the I_T nexus; and
2313c66ac9dbSNicholas Bellinger 	 */
2314c66ac9dbSNicholas Bellinger 	if (res_key != pr_reg->pr_res_key) {
23156708bb27SAndy Grover 		pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2316c66ac9dbSNicholas Bellinger 			" does not match existing SA REGISTER res_key:"
2317c66ac9dbSNicholas Bellinger 			" 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2318de103c93SChristoph Hellwig 		ret = TCM_RESERVATION_CONFLICT;
2319de103c93SChristoph Hellwig 		goto out_put_pr_reg;
2320c66ac9dbSNicholas Bellinger 	}
2321c66ac9dbSNicholas Bellinger 	/*
2322c66ac9dbSNicholas Bellinger 	 * From spc4r17 Section 5.7.9: Reserving:
2323c66ac9dbSNicholas Bellinger 	 *
2324c66ac9dbSNicholas Bellinger 	 * From above:
2325c66ac9dbSNicholas Bellinger 	 *  b) TYPE field and SCOPE field set to the persistent reservation
2326c66ac9dbSNicholas Bellinger 	 *     being created.
2327c66ac9dbSNicholas Bellinger 	 *
2328c66ac9dbSNicholas Bellinger 	 * Only one persistent reservation is allowed at a time per logical unit
2329c66ac9dbSNicholas Bellinger 	 * and that persistent reservation has a scope of LU_SCOPE.
2330c66ac9dbSNicholas Bellinger 	 */
2331c66ac9dbSNicholas Bellinger 	if (scope != PR_SCOPE_LU_SCOPE) {
23326708bb27SAndy Grover 		pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2333de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
2334de103c93SChristoph Hellwig 		goto out_put_pr_reg;
2335c66ac9dbSNicholas Bellinger 	}
2336c66ac9dbSNicholas Bellinger 	/*
2337c66ac9dbSNicholas Bellinger 	 * See if we have an existing PR reservation holder pointer at
2338c66ac9dbSNicholas Bellinger 	 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2339c66ac9dbSNicholas Bellinger 	 * *pr_res_holder.
2340c66ac9dbSNicholas Bellinger 	 */
2341c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
2342c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
2343ee1b1b9cSAndy Grover 	if (pr_res_holder) {
2344c66ac9dbSNicholas Bellinger 		/*
2345c66ac9dbSNicholas Bellinger 		 * From spc4r17 Section 5.7.9: Reserving:
2346c66ac9dbSNicholas Bellinger 		 *
2347c66ac9dbSNicholas Bellinger 		 * If the device server receives a PERSISTENT RESERVE OUT
2348c66ac9dbSNicholas Bellinger 		 * command from an I_T nexus other than a persistent reservation
2349c66ac9dbSNicholas Bellinger 		 * holder (see 5.7.10) that attempts to create a persistent
2350c66ac9dbSNicholas Bellinger 		 * reservation when a persistent reservation already exists for
2351c66ac9dbSNicholas Bellinger 		 * the logical unit, then the command shall be completed with
2352c66ac9dbSNicholas Bellinger 		 * RESERVATION CONFLICT status.
2353c66ac9dbSNicholas Bellinger 		 */
2354e673dc92SIlias Tsitsimpis 		if (!is_reservation_holder(pr_res_holder, pr_reg)) {
2355c66ac9dbSNicholas Bellinger 			struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
23566708bb27SAndy Grover 			pr_err("SPC-3 PR: Attempted RESERVE from"
2357c66ac9dbSNicholas Bellinger 				" [%s]: %s while reservation already held by"
2358c66ac9dbSNicholas Bellinger 				" [%s]: %s, returning RESERVATION_CONFLICT\n",
235930c7ca93SDavid Disseldorp 				cmd->se_tfo->fabric_name,
2360c66ac9dbSNicholas Bellinger 				se_sess->se_node_acl->initiatorname,
236130c7ca93SDavid Disseldorp 				pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
2362c66ac9dbSNicholas Bellinger 				pr_res_holder->pr_reg_nacl->initiatorname);
2363c66ac9dbSNicholas Bellinger 
2364c66ac9dbSNicholas Bellinger 			spin_unlock(&dev->dev_reservation_lock);
2365de103c93SChristoph Hellwig 			ret = TCM_RESERVATION_CONFLICT;
2366de103c93SChristoph Hellwig 			goto out_put_pr_reg;
2367c66ac9dbSNicholas Bellinger 		}
2368c66ac9dbSNicholas Bellinger 		/*
2369c66ac9dbSNicholas Bellinger 		 * From spc4r17 Section 5.7.9: Reserving:
2370c66ac9dbSNicholas Bellinger 		 *
2371c66ac9dbSNicholas Bellinger 		 * If a persistent reservation holder attempts to modify the
2372c66ac9dbSNicholas Bellinger 		 * type or scope of an existing persistent reservation, the
2373c66ac9dbSNicholas Bellinger 		 * command shall be completed with RESERVATION CONFLICT status.
2374c66ac9dbSNicholas Bellinger 		 */
2375c66ac9dbSNicholas Bellinger 		if ((pr_res_holder->pr_res_type != type) ||
2376c66ac9dbSNicholas Bellinger 		    (pr_res_holder->pr_res_scope != scope)) {
2377c66ac9dbSNicholas Bellinger 			struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
23786708bb27SAndy Grover 			pr_err("SPC-3 PR: Attempted RESERVE from"
2379c66ac9dbSNicholas Bellinger 				" [%s]: %s trying to change TYPE and/or SCOPE,"
2380c66ac9dbSNicholas Bellinger 				" while reservation already held by [%s]: %s,"
2381c66ac9dbSNicholas Bellinger 				" returning RESERVATION_CONFLICT\n",
238230c7ca93SDavid Disseldorp 				cmd->se_tfo->fabric_name,
2383c66ac9dbSNicholas Bellinger 				se_sess->se_node_acl->initiatorname,
238430c7ca93SDavid Disseldorp 				pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
2385c66ac9dbSNicholas Bellinger 				pr_res_holder->pr_reg_nacl->initiatorname);
2386c66ac9dbSNicholas Bellinger 
2387c66ac9dbSNicholas Bellinger 			spin_unlock(&dev->dev_reservation_lock);
2388de103c93SChristoph Hellwig 			ret = TCM_RESERVATION_CONFLICT;
2389de103c93SChristoph Hellwig 			goto out_put_pr_reg;
2390c66ac9dbSNicholas Bellinger 		}
2391c66ac9dbSNicholas Bellinger 		/*
2392c66ac9dbSNicholas Bellinger 		 * From spc4r17 Section 5.7.9: Reserving:
2393c66ac9dbSNicholas Bellinger 		 *
2394c66ac9dbSNicholas Bellinger 		 * If the device server receives a PERSISTENT RESERVE OUT
2395c66ac9dbSNicholas Bellinger 		 * command with RESERVE service action where the TYPE field and
2396c66ac9dbSNicholas Bellinger 		 * the SCOPE field contain the same values as the existing type
2397c66ac9dbSNicholas Bellinger 		 * and scope from a persistent reservation holder, it shall not
2398c66ac9dbSNicholas Bellinger 		 * make any change to the existing persistent reservation and
2399c66ac9dbSNicholas Bellinger 		 * shall completethe command with GOOD status.
2400c66ac9dbSNicholas Bellinger 		 */
2401c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
2402de103c93SChristoph Hellwig 		ret = 0;
2403de103c93SChristoph Hellwig 		goto out_put_pr_reg;
2404c66ac9dbSNicholas Bellinger 	}
2405c66ac9dbSNicholas Bellinger 	/*
2406c66ac9dbSNicholas Bellinger 	 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2407c66ac9dbSNicholas Bellinger 	 * TYPE/SCOPE.  Also set the received scope and type in *pr_reg.
2408c66ac9dbSNicholas Bellinger 	 */
2409c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_scope = scope;
2410c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_type = type;
2411c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_holder = 1;
2412c66ac9dbSNicholas Bellinger 	dev->dev_pr_res_holder = pr_reg;
2413d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2414c66ac9dbSNicholas Bellinger 
24156708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2416c66ac9dbSNicholas Bellinger 		" reservation holder TYPE: %s ALL_TG_PT: %d\n",
241730c7ca93SDavid Disseldorp 		cmd->se_tfo->fabric_name, core_scsi3_pr_dump_type(type),
2418c66ac9dbSNicholas Bellinger 		(pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
24196708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
242030c7ca93SDavid Disseldorp 			cmd->se_tfo->fabric_name,
2421c66ac9dbSNicholas Bellinger 			se_sess->se_node_acl->initiatorname,
2422d2843c17SAndy Grover 			i_buf);
2423c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
2424c66ac9dbSNicholas Bellinger 
2425459f213bSAndy Grover 	if (pr_tmpl->pr_aptpl_active)
2426459f213bSAndy Grover 		core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2427c66ac9dbSNicholas Bellinger 
2428de103c93SChristoph Hellwig 	ret = 0;
2429de103c93SChristoph Hellwig out_put_pr_reg:
2430de103c93SChristoph Hellwig 	core_scsi3_put_pr_reg(pr_reg);
2431de103c93SChristoph Hellwig 	return ret;
2432de103c93SChristoph Hellwig }
2433de103c93SChristoph Hellwig 
2434de103c93SChristoph Hellwig static sense_reason_t
2435de103c93SChristoph Hellwig core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope,
2436c66ac9dbSNicholas Bellinger 		u64 res_key)
2437c66ac9dbSNicholas Bellinger {
2438c66ac9dbSNicholas Bellinger 	switch (type) {
2439c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE:
2440c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS:
2441c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2442c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2443c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2444c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2445de103c93SChristoph Hellwig 		return core_scsi3_pro_reserve(cmd, type, scope, res_key);
2446c66ac9dbSNicholas Bellinger 	default:
24476708bb27SAndy Grover 		pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2448c66ac9dbSNicholas Bellinger 			" 0x%02x\n", type);
2449de103c93SChristoph Hellwig 		return TCM_INVALID_CDB_FIELD;
2450c66ac9dbSNicholas Bellinger 	}
2451c66ac9dbSNicholas Bellinger }
2452c66ac9dbSNicholas Bellinger 
2453c66ac9dbSNicholas Bellinger /*
2454c66ac9dbSNicholas Bellinger  * Called with struct se_device->dev_reservation_lock held.
2455c66ac9dbSNicholas Bellinger  */
2456c66ac9dbSNicholas Bellinger static void __core_scsi3_complete_pro_release(
2457c66ac9dbSNicholas Bellinger 	struct se_device *dev,
2458c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl,
2459c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg,
24606c3c9baaSNicholas Bellinger 	int explicit,
24616c3c9baaSNicholas Bellinger 	int unreg)
2462c66ac9dbSNicholas Bellinger {
24639ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2464c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
24656c3c9baaSNicholas Bellinger 	int pr_res_type = 0, pr_res_scope = 0;
2466c66ac9dbSNicholas Bellinger 
2467c66ac9dbSNicholas Bellinger 	memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2468d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2469c66ac9dbSNicholas Bellinger 	/*
2470c66ac9dbSNicholas Bellinger 	 * Go ahead and release the current PR reservation holder.
24716c3c9baaSNicholas Bellinger 	 * If an All Registrants reservation is currently active and
24726c3c9baaSNicholas Bellinger 	 * a unregister operation is requested, replace the current
24736c3c9baaSNicholas Bellinger 	 * dev_pr_res_holder with another active registration.
2474c66ac9dbSNicholas Bellinger 	 */
24756c3c9baaSNicholas Bellinger 	if (dev->dev_pr_res_holder) {
24766c3c9baaSNicholas Bellinger 		pr_res_type = dev->dev_pr_res_holder->pr_res_type;
24776c3c9baaSNicholas Bellinger 		pr_res_scope = dev->dev_pr_res_holder->pr_res_scope;
24786c3c9baaSNicholas Bellinger 		dev->dev_pr_res_holder->pr_res_type = 0;
24796c3c9baaSNicholas Bellinger 		dev->dev_pr_res_holder->pr_res_scope = 0;
24806c3c9baaSNicholas Bellinger 		dev->dev_pr_res_holder->pr_res_holder = 0;
2481c66ac9dbSNicholas Bellinger 		dev->dev_pr_res_holder = NULL;
24826c3c9baaSNicholas Bellinger 	}
24836c3c9baaSNicholas Bellinger 	if (!unreg)
24846c3c9baaSNicholas Bellinger 		goto out;
2485c66ac9dbSNicholas Bellinger 
24866c3c9baaSNicholas Bellinger 	spin_lock(&dev->t10_pr.registration_lock);
24876c3c9baaSNicholas Bellinger 	list_del_init(&pr_reg->pr_reg_list);
24886c3c9baaSNicholas Bellinger 	/*
24896c3c9baaSNicholas Bellinger 	 * If the I_T nexus is a reservation holder, the persistent reservation
24906c3c9baaSNicholas Bellinger 	 * is of an all registrants type, and the I_T nexus is the last remaining
24916c3c9baaSNicholas Bellinger 	 * registered I_T nexus, then the device server shall also release the
24926c3c9baaSNicholas Bellinger 	 * persistent reservation.
24936c3c9baaSNicholas Bellinger 	 */
24946c3c9baaSNicholas Bellinger 	if (!list_empty(&dev->t10_pr.registration_list) &&
24956c3c9baaSNicholas Bellinger 	    ((pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
24966c3c9baaSNicholas Bellinger 	     (pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) {
24976c3c9baaSNicholas Bellinger 		dev->dev_pr_res_holder =
24986c3c9baaSNicholas Bellinger 			list_entry(dev->t10_pr.registration_list.next,
24996c3c9baaSNicholas Bellinger 				   struct t10_pr_registration, pr_reg_list);
25006c3c9baaSNicholas Bellinger 		dev->dev_pr_res_holder->pr_res_type = pr_res_type;
25016c3c9baaSNicholas Bellinger 		dev->dev_pr_res_holder->pr_res_scope = pr_res_scope;
25026c3c9baaSNicholas Bellinger 		dev->dev_pr_res_holder->pr_res_holder = 1;
25036c3c9baaSNicholas Bellinger 	}
25046c3c9baaSNicholas Bellinger 	spin_unlock(&dev->t10_pr.registration_lock);
25056c3c9baaSNicholas Bellinger out:
25066c3c9baaSNicholas Bellinger 	if (!dev->dev_pr_res_holder) {
25076708bb27SAndy Grover 		pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2508c66ac9dbSNicholas Bellinger 			" reservation holder TYPE: %s ALL_TG_PT: %d\n",
250930c7ca93SDavid Disseldorp 			tfo->fabric_name, (explicit) ? "explicit" :
25106c3c9baaSNicholas Bellinger 			"implicit", core_scsi3_pr_dump_type(pr_res_type),
2511c66ac9dbSNicholas Bellinger 			(pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
25126c3c9baaSNicholas Bellinger 	}
25136708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
251430c7ca93SDavid Disseldorp 		tfo->fabric_name, se_nacl->initiatorname,
2515d2843c17SAndy Grover 		i_buf);
2516c66ac9dbSNicholas Bellinger 	/*
2517c66ac9dbSNicholas Bellinger 	 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2518c66ac9dbSNicholas Bellinger 	 */
2519c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2520c66ac9dbSNicholas Bellinger }
2521c66ac9dbSNicholas Bellinger 
2522de103c93SChristoph Hellwig static sense_reason_t
2523de103c93SChristoph Hellwig core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope,
2524c66ac9dbSNicholas Bellinger 		u64 res_key)
2525c66ac9dbSNicholas Bellinger {
2526c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
2527e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
2528e3d6f909SAndy Grover 	struct se_lun *se_lun = cmd->se_lun;
2529c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
25300fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
2531de103c93SChristoph Hellwig 	sense_reason_t ret = 0;
2532c66ac9dbSNicholas Bellinger 
25336708bb27SAndy Grover 	if (!se_sess || !se_lun) {
25346708bb27SAndy Grover 		pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2535de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2536c66ac9dbSNicholas Bellinger 	}
2537c66ac9dbSNicholas Bellinger 	/*
2538c66ac9dbSNicholas Bellinger 	 * Locate the existing *pr_reg via struct se_node_acl pointers
2539c66ac9dbSNicholas Bellinger 	 */
2540c66ac9dbSNicholas Bellinger 	pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
25416708bb27SAndy Grover 	if (!pr_reg) {
25426708bb27SAndy Grover 		pr_err("SPC-3 PR: Unable to locate"
2543c66ac9dbSNicholas Bellinger 			" PR_REGISTERED *pr_reg for RELEASE\n");
2544de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2545c66ac9dbSNicholas Bellinger 	}
2546c66ac9dbSNicholas Bellinger 	/*
2547c66ac9dbSNicholas Bellinger 	 * From spc4r17 Section 5.7.11.2 Releasing:
2548c66ac9dbSNicholas Bellinger 	 *
2549c66ac9dbSNicholas Bellinger 	 * If there is no persistent reservation or in response to a persistent
2550c66ac9dbSNicholas Bellinger 	 * reservation release request from a registered I_T nexus that is not a
2551c66ac9dbSNicholas Bellinger 	 * persistent reservation holder (see 5.7.10), the device server shall
2552c66ac9dbSNicholas Bellinger 	 * do the following:
2553c66ac9dbSNicholas Bellinger 	 *
2554c66ac9dbSNicholas Bellinger 	 *     a) Not release the persistent reservation, if any;
2555c66ac9dbSNicholas Bellinger 	 *     b) Not remove any registrations; and
2556c66ac9dbSNicholas Bellinger 	 *     c) Complete the command with GOOD status.
2557c66ac9dbSNicholas Bellinger 	 */
2558c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
2559c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
25606708bb27SAndy Grover 	if (!pr_res_holder) {
2561c66ac9dbSNicholas Bellinger 		/*
2562c66ac9dbSNicholas Bellinger 		 * No persistent reservation, return GOOD status.
2563c66ac9dbSNicholas Bellinger 		 */
2564c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
2565bb7a8c8eSNicholas Bellinger 		goto out_put_pr_reg;
2566c66ac9dbSNicholas Bellinger 	}
2567c66ac9dbSNicholas Bellinger 
2568e673dc92SIlias Tsitsimpis 	if (!is_reservation_holder(pr_res_holder, pr_reg)) {
2569c66ac9dbSNicholas Bellinger 		/*
2570c66ac9dbSNicholas Bellinger 		 * Release request from a registered I_T nexus that is not a
2571c66ac9dbSNicholas Bellinger 		 * persistent reservation holder. return GOOD status.
2572c66ac9dbSNicholas Bellinger 		 */
2573c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
2574de103c93SChristoph Hellwig 		goto out_put_pr_reg;
2575c66ac9dbSNicholas Bellinger 	}
2576de103c93SChristoph Hellwig 
2577c66ac9dbSNicholas Bellinger 	/*
2578c66ac9dbSNicholas Bellinger 	 * From spc4r17 Section 5.7.11.2 Releasing:
2579c66ac9dbSNicholas Bellinger 	 *
2580c66ac9dbSNicholas Bellinger 	 * Only the persistent reservation holder (see 5.7.10) is allowed to
2581c66ac9dbSNicholas Bellinger 	 * release a persistent reservation.
2582c66ac9dbSNicholas Bellinger 	 *
2583c66ac9dbSNicholas Bellinger 	 * An application client releases the persistent reservation by issuing
2584c66ac9dbSNicholas Bellinger 	 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2585c66ac9dbSNicholas Bellinger 	 * an I_T nexus that is a persistent reservation holder with the
2586c66ac9dbSNicholas Bellinger 	 * following parameters:
2587c66ac9dbSNicholas Bellinger 	 *
2588c66ac9dbSNicholas Bellinger 	 *     a) RESERVATION KEY field set to the value of the reservation key
2589c66ac9dbSNicholas Bellinger 	 *	  that is registered with the logical unit for the I_T nexus;
2590c66ac9dbSNicholas Bellinger 	 */
2591c66ac9dbSNicholas Bellinger 	if (res_key != pr_reg->pr_res_key) {
25926708bb27SAndy Grover 		pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2593c66ac9dbSNicholas Bellinger 			" does not match existing SA REGISTER res_key:"
2594c66ac9dbSNicholas Bellinger 			" 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2595c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
2596de103c93SChristoph Hellwig 		ret = TCM_RESERVATION_CONFLICT;
2597de103c93SChristoph Hellwig 		goto out_put_pr_reg;
2598c66ac9dbSNicholas Bellinger 	}
2599c66ac9dbSNicholas Bellinger 	/*
2600c66ac9dbSNicholas Bellinger 	 * From spc4r17 Section 5.7.11.2 Releasing and above:
2601c66ac9dbSNicholas Bellinger 	 *
2602c66ac9dbSNicholas Bellinger 	 * b) TYPE field and SCOPE field set to match the persistent
2603c66ac9dbSNicholas Bellinger 	 *    reservation being released.
2604c66ac9dbSNicholas Bellinger 	 */
2605c66ac9dbSNicholas Bellinger 	if ((pr_res_holder->pr_res_type != type) ||
2606c66ac9dbSNicholas Bellinger 	    (pr_res_holder->pr_res_scope != scope)) {
2607c66ac9dbSNicholas Bellinger 		struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
26086708bb27SAndy Grover 		pr_err("SPC-3 PR RELEASE: Attempted to release"
2609c66ac9dbSNicholas Bellinger 			" reservation from [%s]: %s with different TYPE "
2610c66ac9dbSNicholas Bellinger 			"and/or SCOPE  while reservation already held by"
2611c66ac9dbSNicholas Bellinger 			" [%s]: %s, returning RESERVATION_CONFLICT\n",
261230c7ca93SDavid Disseldorp 			cmd->se_tfo->fabric_name,
2613c66ac9dbSNicholas Bellinger 			se_sess->se_node_acl->initiatorname,
261430c7ca93SDavid Disseldorp 			pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
2615c66ac9dbSNicholas Bellinger 			pr_res_holder->pr_reg_nacl->initiatorname);
2616c66ac9dbSNicholas Bellinger 
2617c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
2618de103c93SChristoph Hellwig 		ret = TCM_RESERVATION_CONFLICT;
2619de103c93SChristoph Hellwig 		goto out_put_pr_reg;
2620c66ac9dbSNicholas Bellinger 	}
2621c66ac9dbSNicholas Bellinger 	/*
2622c66ac9dbSNicholas Bellinger 	 * In response to a persistent reservation release request from the
2623c66ac9dbSNicholas Bellinger 	 * persistent reservation holder the device server shall perform a
2624c66ac9dbSNicholas Bellinger 	 * release by doing the following as an uninterrupted series of actions:
2625c66ac9dbSNicholas Bellinger 	 * a) Release the persistent reservation;
2626c66ac9dbSNicholas Bellinger 	 * b) Not remove any registration(s);
2627c66ac9dbSNicholas Bellinger 	 * c) If the released persistent reservation is a registrants only type
2628c66ac9dbSNicholas Bellinger 	 * or all registrants type persistent reservation,
2629c66ac9dbSNicholas Bellinger 	 *    the device server shall establish a unit attention condition for
2630c66ac9dbSNicholas Bellinger 	 *    the initiator port associated with every regis-
2631c66ac9dbSNicholas Bellinger 	 *    tered I_T nexus other than I_T nexus on which the PERSISTENT
2632c66ac9dbSNicholas Bellinger 	 *    RESERVE OUT command with RELEASE service action was received,
2633c66ac9dbSNicholas Bellinger 	 *    with the additional sense code set to RESERVATIONS RELEASED; and
2634c66ac9dbSNicholas Bellinger 	 * d) If the persistent reservation is of any other type, the device
2635c66ac9dbSNicholas Bellinger 	 *    server shall not establish a unit attention condition.
2636c66ac9dbSNicholas Bellinger 	 */
2637c66ac9dbSNicholas Bellinger 	__core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
26386c3c9baaSNicholas Bellinger 					  pr_reg, 1, 0);
2639c66ac9dbSNicholas Bellinger 
2640c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
2641c66ac9dbSNicholas Bellinger 
2642c66ac9dbSNicholas Bellinger 	if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2643c66ac9dbSNicholas Bellinger 	    (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2644c66ac9dbSNicholas Bellinger 	    (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2645c66ac9dbSNicholas Bellinger 	    (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2646c66ac9dbSNicholas Bellinger 		/*
2647c66ac9dbSNicholas Bellinger 		 * If no UNIT ATTENTION conditions will be established for
2648c66ac9dbSNicholas Bellinger 		 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2649c66ac9dbSNicholas Bellinger 		 * go ahead and check for APTPL=1 update+write below
2650c66ac9dbSNicholas Bellinger 		 */
2651c66ac9dbSNicholas Bellinger 		goto write_aptpl;
2652c66ac9dbSNicholas Bellinger 	}
2653c66ac9dbSNicholas Bellinger 
2654c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
2655c66ac9dbSNicholas Bellinger 	list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2656c66ac9dbSNicholas Bellinger 			pr_reg_list) {
2657c66ac9dbSNicholas Bellinger 		/*
2658c66ac9dbSNicholas Bellinger 		 * Do not establish a UNIT ATTENTION condition
2659c66ac9dbSNicholas Bellinger 		 * for the calling I_T Nexus
2660c66ac9dbSNicholas Bellinger 		 */
2661c66ac9dbSNicholas Bellinger 		if (pr_reg_p == pr_reg)
2662c66ac9dbSNicholas Bellinger 			continue;
2663c66ac9dbSNicholas Bellinger 
2664c51c8e7bSHannes Reinecke 		target_ua_allocate_lun(pr_reg_p->pr_reg_nacl,
2665c66ac9dbSNicholas Bellinger 				pr_reg_p->pr_res_mapped_lun,
2666c66ac9dbSNicholas Bellinger 				0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2667c66ac9dbSNicholas Bellinger 	}
2668c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
2669c66ac9dbSNicholas Bellinger 
2670c66ac9dbSNicholas Bellinger write_aptpl:
2671459f213bSAndy Grover 	if (pr_tmpl->pr_aptpl_active)
2672459f213bSAndy Grover 		core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2673459f213bSAndy Grover 
2674de103c93SChristoph Hellwig out_put_pr_reg:
2675c66ac9dbSNicholas Bellinger 	core_scsi3_put_pr_reg(pr_reg);
2676de103c93SChristoph Hellwig 	return ret;
2677c66ac9dbSNicholas Bellinger }
2678c66ac9dbSNicholas Bellinger 
2679de103c93SChristoph Hellwig static sense_reason_t
2680de103c93SChristoph Hellwig core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key)
2681c66ac9dbSNicholas Bellinger {
2682c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
2683c66ac9dbSNicholas Bellinger 	struct se_node_acl *pr_reg_nacl;
2684e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
26850fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
2686c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2687f2d30680SHannes Reinecke 	u64 pr_res_mapped_lun = 0;
2688c66ac9dbSNicholas Bellinger 	int calling_it_nexus = 0;
2689c66ac9dbSNicholas Bellinger 	/*
2690c66ac9dbSNicholas Bellinger 	 * Locate the existing *pr_reg via struct se_node_acl pointers
2691c66ac9dbSNicholas Bellinger 	 */
26925951146dSAndy Grover 	pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
2693c66ac9dbSNicholas Bellinger 			se_sess->se_node_acl, se_sess);
26946708bb27SAndy Grover 	if (!pr_reg_n) {
26956708bb27SAndy Grover 		pr_err("SPC-3 PR: Unable to locate"
2696c66ac9dbSNicholas Bellinger 			" PR_REGISTERED *pr_reg for CLEAR\n");
2697de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2698c66ac9dbSNicholas Bellinger 	}
2699c66ac9dbSNicholas Bellinger 	/*
2700c66ac9dbSNicholas Bellinger 	 * From spc4r17 section 5.7.11.6, Clearing:
2701c66ac9dbSNicholas Bellinger 	 *
2702c66ac9dbSNicholas Bellinger 	 * Any application client may release the persistent reservation and
2703c66ac9dbSNicholas Bellinger 	 * remove all registrations from a device server by issuing a
2704c66ac9dbSNicholas Bellinger 	 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2705c66ac9dbSNicholas Bellinger 	 * registered I_T nexus with the following parameter:
2706c66ac9dbSNicholas Bellinger 	 *
2707c66ac9dbSNicholas Bellinger 	 *	a) RESERVATION KEY field set to the value of the reservation key
2708c66ac9dbSNicholas Bellinger 	 * 	   that is registered with the logical unit for the I_T nexus.
2709c66ac9dbSNicholas Bellinger 	 */
2710c66ac9dbSNicholas Bellinger 	if (res_key != pr_reg_n->pr_res_key) {
27116708bb27SAndy Grover 		pr_err("SPC-3 PR REGISTER: Received"
2712c66ac9dbSNicholas Bellinger 			" res_key: 0x%016Lx does not match"
2713c66ac9dbSNicholas Bellinger 			" existing SA REGISTER res_key:"
2714c66ac9dbSNicholas Bellinger 			" 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2715c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg_n);
2716de103c93SChristoph Hellwig 		return TCM_RESERVATION_CONFLICT;
2717c66ac9dbSNicholas Bellinger 	}
2718c66ac9dbSNicholas Bellinger 	/*
2719c66ac9dbSNicholas Bellinger 	 * a) Release the persistent reservation, if any;
2720c66ac9dbSNicholas Bellinger 	 */
2721c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
2722c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
2723c66ac9dbSNicholas Bellinger 	if (pr_res_holder) {
2724c66ac9dbSNicholas Bellinger 		struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2725c66ac9dbSNicholas Bellinger 		__core_scsi3_complete_pro_release(dev, pr_res_nacl,
27266c3c9baaSNicholas Bellinger 						  pr_res_holder, 0, 0);
2727c66ac9dbSNicholas Bellinger 	}
2728c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
2729c66ac9dbSNicholas Bellinger 	/*
2730c66ac9dbSNicholas Bellinger 	 * b) Remove all registration(s) (see spc4r17 5.7.7);
2731c66ac9dbSNicholas Bellinger 	 */
2732c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
2733c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2734c66ac9dbSNicholas Bellinger 			&pr_tmpl->registration_list, pr_reg_list) {
2735c66ac9dbSNicholas Bellinger 
2736c66ac9dbSNicholas Bellinger 		calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2737c66ac9dbSNicholas Bellinger 		pr_reg_nacl = pr_reg->pr_reg_nacl;
2738c66ac9dbSNicholas Bellinger 		pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2739c66ac9dbSNicholas Bellinger 		__core_scsi3_free_registration(dev, pr_reg, NULL,
2740c66ac9dbSNicholas Bellinger 					calling_it_nexus);
2741c66ac9dbSNicholas Bellinger 		/*
2742c66ac9dbSNicholas Bellinger 		 * e) Establish a unit attention condition for the initiator
2743c66ac9dbSNicholas Bellinger 		 *    port associated with every registered I_T nexus other
2744c66ac9dbSNicholas Bellinger 		 *    than the I_T nexus on which the PERSISTENT RESERVE OUT
2745c66ac9dbSNicholas Bellinger 		 *    command with CLEAR service action was received, with the
2746c66ac9dbSNicholas Bellinger 		 *    additional sense code set to RESERVATIONS PREEMPTED.
2747c66ac9dbSNicholas Bellinger 		 */
27486708bb27SAndy Grover 		if (!calling_it_nexus)
2749c51c8e7bSHannes Reinecke 			target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun,
2750c66ac9dbSNicholas Bellinger 				0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2751c66ac9dbSNicholas Bellinger 	}
2752c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
2753c66ac9dbSNicholas Bellinger 
27546708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
275530c7ca93SDavid Disseldorp 		cmd->se_tfo->fabric_name);
2756c66ac9dbSNicholas Bellinger 
2757459f213bSAndy Grover 	core_scsi3_update_and_write_aptpl(cmd->se_dev, false);
2758c66ac9dbSNicholas Bellinger 
2759c66ac9dbSNicholas Bellinger 	core_scsi3_pr_generation(dev);
2760c66ac9dbSNicholas Bellinger 	return 0;
2761c66ac9dbSNicholas Bellinger }
2762c66ac9dbSNicholas Bellinger 
2763c66ac9dbSNicholas Bellinger /*
2764c66ac9dbSNicholas Bellinger  * Called with struct se_device->dev_reservation_lock held.
2765c66ac9dbSNicholas Bellinger  */
2766c66ac9dbSNicholas Bellinger static void __core_scsi3_complete_pro_preempt(
2767c66ac9dbSNicholas Bellinger 	struct se_device *dev,
2768c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg,
2769c66ac9dbSNicholas Bellinger 	struct list_head *preempt_and_abort_list,
2770c66ac9dbSNicholas Bellinger 	int type,
2771c66ac9dbSNicholas Bellinger 	int scope,
277233ce6a87SAndy Grover 	enum preempt_type preempt_type)
2773c66ac9dbSNicholas Bellinger {
2774c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
27759ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2776c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
2777c66ac9dbSNicholas Bellinger 
2778c66ac9dbSNicholas Bellinger 	memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2779d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2780c66ac9dbSNicholas Bellinger 	/*
2781125d0119SHannes Reinecke 	 * Do an implicit RELEASE of the existing reservation.
2782c66ac9dbSNicholas Bellinger 	 */
2783c66ac9dbSNicholas Bellinger 	if (dev->dev_pr_res_holder)
2784c66ac9dbSNicholas Bellinger 		__core_scsi3_complete_pro_release(dev, nacl,
27856c3c9baaSNicholas Bellinger 						  dev->dev_pr_res_holder, 0, 0);
2786c66ac9dbSNicholas Bellinger 
2787c66ac9dbSNicholas Bellinger 	dev->dev_pr_res_holder = pr_reg;
2788c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_holder = 1;
2789c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_type = type;
2790c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_scope = scope;
2791c66ac9dbSNicholas Bellinger 
27926708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2793c66ac9dbSNicholas Bellinger 		" reservation holder TYPE: %s ALL_TG_PT: %d\n",
279430c7ca93SDavid Disseldorp 		tfo->fabric_name, (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
2795c66ac9dbSNicholas Bellinger 		core_scsi3_pr_dump_type(type),
2796c66ac9dbSNicholas Bellinger 		(pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
27976708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
279830c7ca93SDavid Disseldorp 		tfo->fabric_name, (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
2799d2843c17SAndy Grover 		nacl->initiatorname, i_buf);
2800c66ac9dbSNicholas Bellinger 	/*
2801c66ac9dbSNicholas Bellinger 	 * For PREEMPT_AND_ABORT, add the preempting reservation's
2802c66ac9dbSNicholas Bellinger 	 * struct t10_pr_registration to the list that will be compared
2803c66ac9dbSNicholas Bellinger 	 * against received CDBs..
2804c66ac9dbSNicholas Bellinger 	 */
2805c66ac9dbSNicholas Bellinger 	if (preempt_and_abort_list)
2806c66ac9dbSNicholas Bellinger 		list_add_tail(&pr_reg->pr_reg_abort_list,
2807c66ac9dbSNicholas Bellinger 				preempt_and_abort_list);
2808c66ac9dbSNicholas Bellinger }
2809c66ac9dbSNicholas Bellinger 
2810c66ac9dbSNicholas Bellinger static void core_scsi3_release_preempt_and_abort(
2811c66ac9dbSNicholas Bellinger 	struct list_head *preempt_and_abort_list,
2812c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg_holder)
2813c66ac9dbSNicholas Bellinger {
2814c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2815c66ac9dbSNicholas Bellinger 
2816c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2817c66ac9dbSNicholas Bellinger 				pr_reg_abort_list) {
2818c66ac9dbSNicholas Bellinger 
2819c66ac9dbSNicholas Bellinger 		list_del(&pr_reg->pr_reg_abort_list);
2820c66ac9dbSNicholas Bellinger 		if (pr_reg_holder == pr_reg)
2821c66ac9dbSNicholas Bellinger 			continue;
2822c66ac9dbSNicholas Bellinger 		if (pr_reg->pr_res_holder) {
28236708bb27SAndy Grover 			pr_warn("pr_reg->pr_res_holder still set\n");
2824c66ac9dbSNicholas Bellinger 			continue;
2825c66ac9dbSNicholas Bellinger 		}
2826c66ac9dbSNicholas Bellinger 
2827c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_deve = NULL;
2828c66ac9dbSNicholas Bellinger 		pr_reg->pr_reg_nacl = NULL;
2829c66ac9dbSNicholas Bellinger 		kmem_cache_free(t10_pr_reg_cache, pr_reg);
2830c66ac9dbSNicholas Bellinger 	}
2831c66ac9dbSNicholas Bellinger }
2832c66ac9dbSNicholas Bellinger 
2833de103c93SChristoph Hellwig static sense_reason_t
2834de103c93SChristoph Hellwig core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key,
283533ce6a87SAndy Grover 		u64 sa_res_key, enum preempt_type preempt_type)
2836c66ac9dbSNicholas Bellinger {
28375951146dSAndy Grover 	struct se_device *dev = cmd->se_dev;
2838c66ac9dbSNicholas Bellinger 	struct se_node_acl *pr_reg_nacl;
2839e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
2840d0f474e5SRoland Dreier 	LIST_HEAD(preempt_and_abort_list);
2841c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
28420fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
2843f2d30680SHannes Reinecke 	u64 pr_res_mapped_lun = 0;
2844b6932ee3SSteven Allen 	int all_reg = 0, calling_it_nexus = 0;
2845b6932ee3SSteven Allen 	bool sa_res_key_unmatched = sa_res_key != 0;
2846de103c93SChristoph Hellwig 	int prh_type = 0, prh_scope = 0;
2847c66ac9dbSNicholas Bellinger 
2848de103c93SChristoph Hellwig 	if (!se_sess)
2849de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2850c66ac9dbSNicholas Bellinger 
28515951146dSAndy Grover 	pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2852c66ac9dbSNicholas Bellinger 				se_sess);
28536708bb27SAndy Grover 	if (!pr_reg_n) {
28546708bb27SAndy Grover 		pr_err("SPC-3 PR: Unable to locate"
2855c66ac9dbSNicholas Bellinger 			" PR_REGISTERED *pr_reg for PREEMPT%s\n",
285633ce6a87SAndy Grover 			(preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "");
2857de103c93SChristoph Hellwig 		return TCM_RESERVATION_CONFLICT;
2858c66ac9dbSNicholas Bellinger 	}
2859c66ac9dbSNicholas Bellinger 	if (pr_reg_n->pr_res_key != res_key) {
2860c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg_n);
2861de103c93SChristoph Hellwig 		return TCM_RESERVATION_CONFLICT;
2862c66ac9dbSNicholas Bellinger 	}
2863c66ac9dbSNicholas Bellinger 	if (scope != PR_SCOPE_LU_SCOPE) {
28646708bb27SAndy Grover 		pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2865c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg_n);
2866de103c93SChristoph Hellwig 		return TCM_INVALID_PARAMETER_LIST;
2867c66ac9dbSNicholas Bellinger 	}
2868c66ac9dbSNicholas Bellinger 
2869c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
2870c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
2871c66ac9dbSNicholas Bellinger 	if (pr_res_holder &&
2872c66ac9dbSNicholas Bellinger 	   ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2873c66ac9dbSNicholas Bellinger 	    (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
2874c66ac9dbSNicholas Bellinger 		all_reg = 1;
2875c66ac9dbSNicholas Bellinger 
28766708bb27SAndy Grover 	if (!all_reg && !sa_res_key) {
2877c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
2878c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg_n);
2879de103c93SChristoph Hellwig 		return TCM_INVALID_PARAMETER_LIST;
2880c66ac9dbSNicholas Bellinger 	}
2881c66ac9dbSNicholas Bellinger 	/*
2882c66ac9dbSNicholas Bellinger 	 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
2883c66ac9dbSNicholas Bellinger 	 *
2884c66ac9dbSNicholas Bellinger 	 * If the SERVICE ACTION RESERVATION KEY field does not identify a
2885c66ac9dbSNicholas Bellinger 	 * persistent reservation holder or there is no persistent reservation
2886c66ac9dbSNicholas Bellinger 	 * holder (i.e., there is no persistent reservation), then the device
2887c66ac9dbSNicholas Bellinger 	 * server shall perform a preempt by doing the following in an
2888c66ac9dbSNicholas Bellinger 	 * uninterrupted series of actions. (See below..)
2889c66ac9dbSNicholas Bellinger 	 */
28906708bb27SAndy Grover 	if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
2891c66ac9dbSNicholas Bellinger 		/*
2892c66ac9dbSNicholas Bellinger 		 * No existing or SA Reservation Key matching reservations..
2893c66ac9dbSNicholas Bellinger 		 *
2894c66ac9dbSNicholas Bellinger 		 * PROUT SA PREEMPT with All Registrant type reservations are
2895c66ac9dbSNicholas Bellinger 		 * allowed to be processed without a matching SA Reservation Key
2896c66ac9dbSNicholas Bellinger 		 */
2897c66ac9dbSNicholas Bellinger 		spin_lock(&pr_tmpl->registration_lock);
2898c66ac9dbSNicholas Bellinger 		list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2899c66ac9dbSNicholas Bellinger 				&pr_tmpl->registration_list, pr_reg_list) {
2900c66ac9dbSNicholas Bellinger 			/*
2901c66ac9dbSNicholas Bellinger 			 * Removing of registrations in non all registrants
2902c66ac9dbSNicholas Bellinger 			 * type reservations without a matching SA reservation
2903c66ac9dbSNicholas Bellinger 			 * key.
2904c66ac9dbSNicholas Bellinger 			 *
2905c66ac9dbSNicholas Bellinger 			 * a) Remove the registrations for all I_T nexuses
2906c66ac9dbSNicholas Bellinger 			 *    specified by the SERVICE ACTION RESERVATION KEY
2907c66ac9dbSNicholas Bellinger 			 *    field;
2908c66ac9dbSNicholas Bellinger 			 * b) Ignore the contents of the SCOPE and TYPE fields;
2909c66ac9dbSNicholas Bellinger 			 * c) Process tasks as defined in 5.7.1; and
2910c66ac9dbSNicholas Bellinger 			 * d) Establish a unit attention condition for the
2911c66ac9dbSNicholas Bellinger 			 *    initiator port associated with every I_T nexus
2912c66ac9dbSNicholas Bellinger 			 *    that lost its registration other than the I_T
2913c66ac9dbSNicholas Bellinger 			 *    nexus on which the PERSISTENT RESERVE OUT command
2914c66ac9dbSNicholas Bellinger 			 *    was received, with the additional sense code set
2915c66ac9dbSNicholas Bellinger 			 *    to REGISTRATIONS PREEMPTED.
2916c66ac9dbSNicholas Bellinger 			 */
29176708bb27SAndy Grover 			if (!all_reg) {
2918c66ac9dbSNicholas Bellinger 				if (pr_reg->pr_res_key != sa_res_key)
2919c66ac9dbSNicholas Bellinger 					continue;
2920b6932ee3SSteven Allen 				sa_res_key_unmatched = false;
2921c66ac9dbSNicholas Bellinger 
2922c66ac9dbSNicholas Bellinger 				calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2923c66ac9dbSNicholas Bellinger 				pr_reg_nacl = pr_reg->pr_reg_nacl;
2924c66ac9dbSNicholas Bellinger 				pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2925c66ac9dbSNicholas Bellinger 				__core_scsi3_free_registration(dev, pr_reg,
292633ce6a87SAndy Grover 					(preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
2927c66ac9dbSNicholas Bellinger 						NULL, calling_it_nexus);
2928c66ac9dbSNicholas Bellinger 			} else {
2929c66ac9dbSNicholas Bellinger 				/*
2930c66ac9dbSNicholas Bellinger 				 * Case for any existing all registrants type
2931c66ac9dbSNicholas Bellinger 				 * reservation, follow logic in spc4r17 section
2932c66ac9dbSNicholas Bellinger 				 * 5.7.11.4 Preempting, Table 52 and Figure 7.
2933c66ac9dbSNicholas Bellinger 				 *
2934c66ac9dbSNicholas Bellinger 				 * For a ZERO SA Reservation key, release
2935125d0119SHannes Reinecke 				 * all other registrations and do an implicit
2936c66ac9dbSNicholas Bellinger 				 * release of active persistent reservation.
2937c66ac9dbSNicholas Bellinger 				 *
2938c66ac9dbSNicholas Bellinger 				 * For a non-ZERO SA Reservation key, only
2939c66ac9dbSNicholas Bellinger 				 * release the matching reservation key from
2940c66ac9dbSNicholas Bellinger 				 * registrations.
2941c66ac9dbSNicholas Bellinger 				 */
2942c66ac9dbSNicholas Bellinger 				if ((sa_res_key) &&
2943c66ac9dbSNicholas Bellinger 				     (pr_reg->pr_res_key != sa_res_key))
2944c66ac9dbSNicholas Bellinger 					continue;
2945b6932ee3SSteven Allen 				sa_res_key_unmatched = false;
2946c66ac9dbSNicholas Bellinger 
2947c66ac9dbSNicholas Bellinger 				calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2948c66ac9dbSNicholas Bellinger 				if (calling_it_nexus)
2949c66ac9dbSNicholas Bellinger 					continue;
2950c66ac9dbSNicholas Bellinger 
2951c66ac9dbSNicholas Bellinger 				pr_reg_nacl = pr_reg->pr_reg_nacl;
2952c66ac9dbSNicholas Bellinger 				pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2953c66ac9dbSNicholas Bellinger 				__core_scsi3_free_registration(dev, pr_reg,
295433ce6a87SAndy Grover 					(preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
2955c66ac9dbSNicholas Bellinger 						NULL, 0);
2956c66ac9dbSNicholas Bellinger 			}
29576708bb27SAndy Grover 			if (!calling_it_nexus)
2958c51c8e7bSHannes Reinecke 				target_ua_allocate_lun(pr_reg_nacl,
2959c66ac9dbSNicholas Bellinger 					pr_res_mapped_lun, 0x2A,
29609e08e34eSMarco Sanvido 					ASCQ_2AH_REGISTRATIONS_PREEMPTED);
2961c66ac9dbSNicholas Bellinger 		}
2962c66ac9dbSNicholas Bellinger 		spin_unlock(&pr_tmpl->registration_lock);
2963c66ac9dbSNicholas Bellinger 		/*
2964c66ac9dbSNicholas Bellinger 		 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
2965c66ac9dbSNicholas Bellinger 		 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
2966c66ac9dbSNicholas Bellinger 		 * RESERVATION KEY field to a value that does not match any
2967c66ac9dbSNicholas Bellinger 		 * registered reservation key, then the device server shall
2968c66ac9dbSNicholas Bellinger 		 * complete the command with RESERVATION CONFLICT status.
2969c66ac9dbSNicholas Bellinger 		 */
2970b6932ee3SSteven Allen 		if (sa_res_key_unmatched) {
2971c66ac9dbSNicholas Bellinger 			spin_unlock(&dev->dev_reservation_lock);
2972c66ac9dbSNicholas Bellinger 			core_scsi3_put_pr_reg(pr_reg_n);
2973de103c93SChristoph Hellwig 			return TCM_RESERVATION_CONFLICT;
2974c66ac9dbSNicholas Bellinger 		}
2975c66ac9dbSNicholas Bellinger 		/*
2976c66ac9dbSNicholas Bellinger 		 * For an existing all registrants type reservation
2977c66ac9dbSNicholas Bellinger 		 * with a zero SA rservation key, preempt the existing
2978c66ac9dbSNicholas Bellinger 		 * reservation with the new PR type and scope.
2979c66ac9dbSNicholas Bellinger 		 */
2980c66ac9dbSNicholas Bellinger 		if (pr_res_holder && all_reg && !(sa_res_key)) {
2981c66ac9dbSNicholas Bellinger 			__core_scsi3_complete_pro_preempt(dev, pr_reg_n,
298233ce6a87SAndy Grover 				(preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
298333ce6a87SAndy Grover 				type, scope, preempt_type);
2984c66ac9dbSNicholas Bellinger 
298533ce6a87SAndy Grover 			if (preempt_type == PREEMPT_AND_ABORT)
2986c66ac9dbSNicholas Bellinger 				core_scsi3_release_preempt_and_abort(
2987c66ac9dbSNicholas Bellinger 					&preempt_and_abort_list, pr_reg_n);
2988c66ac9dbSNicholas Bellinger 		}
2989c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
2990c66ac9dbSNicholas Bellinger 
2991459f213bSAndy Grover 		if (pr_tmpl->pr_aptpl_active)
2992459f213bSAndy Grover 			core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2993c66ac9dbSNicholas Bellinger 
2994c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg_n);
29955951146dSAndy Grover 		core_scsi3_pr_generation(cmd->se_dev);
2996c66ac9dbSNicholas Bellinger 		return 0;
2997c66ac9dbSNicholas Bellinger 	}
2998c66ac9dbSNicholas Bellinger 	/*
2999c66ac9dbSNicholas Bellinger 	 * The PREEMPTing SA reservation key matches that of the
3000c66ac9dbSNicholas Bellinger 	 * existing persistent reservation, first, we check if
3001c66ac9dbSNicholas Bellinger 	 * we are preempting our own reservation.
3002c66ac9dbSNicholas Bellinger 	 * From spc4r17, section 5.7.11.4.3 Preempting
3003c66ac9dbSNicholas Bellinger 	 * persistent reservations and registration handling
3004c66ac9dbSNicholas Bellinger 	 *
3005c66ac9dbSNicholas Bellinger 	 * If an all registrants persistent reservation is not
3006c66ac9dbSNicholas Bellinger 	 * present, it is not an error for the persistent
3007c66ac9dbSNicholas Bellinger 	 * reservation holder to preempt itself (i.e., a
3008c66ac9dbSNicholas Bellinger 	 * PERSISTENT RESERVE OUT with a PREEMPT service action
3009c66ac9dbSNicholas Bellinger 	 * or a PREEMPT AND ABORT service action with the
3010c66ac9dbSNicholas Bellinger 	 * SERVICE ACTION RESERVATION KEY value equal to the
3011c66ac9dbSNicholas Bellinger 	 * persistent reservation holder's reservation key that
3012c66ac9dbSNicholas Bellinger 	 * is received from the persistent reservation holder).
3013c66ac9dbSNicholas Bellinger 	 * In that case, the device server shall establish the
3014c66ac9dbSNicholas Bellinger 	 * new persistent reservation and maintain the
3015c66ac9dbSNicholas Bellinger 	 * registration.
3016c66ac9dbSNicholas Bellinger 	 */
3017c66ac9dbSNicholas Bellinger 	prh_type = pr_res_holder->pr_res_type;
3018c66ac9dbSNicholas Bellinger 	prh_scope = pr_res_holder->pr_res_scope;
3019c66ac9dbSNicholas Bellinger 	/*
3020c66ac9dbSNicholas Bellinger 	 * If the SERVICE ACTION RESERVATION KEY field identifies a
3021c66ac9dbSNicholas Bellinger 	 * persistent reservation holder (see 5.7.10), the device
3022c66ac9dbSNicholas Bellinger 	 * server shall perform a preempt by doing the following as
3023c66ac9dbSNicholas Bellinger 	 * an uninterrupted series of actions:
3024c66ac9dbSNicholas Bellinger 	 *
3025c66ac9dbSNicholas Bellinger 	 * a) Release the persistent reservation for the holder
3026c66ac9dbSNicholas Bellinger 	 *    identified by the SERVICE ACTION RESERVATION KEY field;
3027c66ac9dbSNicholas Bellinger 	 */
3028c66ac9dbSNicholas Bellinger 	if (pr_reg_n != pr_res_holder)
3029c66ac9dbSNicholas Bellinger 		__core_scsi3_complete_pro_release(dev,
3030c66ac9dbSNicholas Bellinger 						  pr_res_holder->pr_reg_nacl,
30316c3c9baaSNicholas Bellinger 						  dev->dev_pr_res_holder, 0, 0);
3032c66ac9dbSNicholas Bellinger 	/*
3033c66ac9dbSNicholas Bellinger 	 * b) Remove the registrations for all I_T nexuses identified
3034c66ac9dbSNicholas Bellinger 	 *    by the SERVICE ACTION RESERVATION KEY field, except the
3035c66ac9dbSNicholas Bellinger 	 *    I_T nexus that is being used for the PERSISTENT RESERVE
3036c66ac9dbSNicholas Bellinger 	 *    OUT command. If an all registrants persistent reservation
3037c66ac9dbSNicholas Bellinger 	 *    is present and the SERVICE ACTION RESERVATION KEY field
3038c66ac9dbSNicholas Bellinger 	 *    is set to zero, then all registrations shall be removed
3039c66ac9dbSNicholas Bellinger 	 *    except for that of the I_T nexus that is being used for
3040c66ac9dbSNicholas Bellinger 	 *    the PERSISTENT RESERVE OUT command;
3041c66ac9dbSNicholas Bellinger 	 */
3042c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
3043c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3044c66ac9dbSNicholas Bellinger 			&pr_tmpl->registration_list, pr_reg_list) {
3045c66ac9dbSNicholas Bellinger 
3046c66ac9dbSNicholas Bellinger 		calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3047c66ac9dbSNicholas Bellinger 		if (calling_it_nexus)
3048c66ac9dbSNicholas Bellinger 			continue;
3049c66ac9dbSNicholas Bellinger 
3050c66ac9dbSNicholas Bellinger 		if (pr_reg->pr_res_key != sa_res_key)
3051c66ac9dbSNicholas Bellinger 			continue;
3052c66ac9dbSNicholas Bellinger 
3053c66ac9dbSNicholas Bellinger 		pr_reg_nacl = pr_reg->pr_reg_nacl;
3054c66ac9dbSNicholas Bellinger 		pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3055c66ac9dbSNicholas Bellinger 		__core_scsi3_free_registration(dev, pr_reg,
305633ce6a87SAndy Grover 				(preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3057c66ac9dbSNicholas Bellinger 				calling_it_nexus);
3058c66ac9dbSNicholas Bellinger 		/*
3059c66ac9dbSNicholas Bellinger 		 * e) Establish a unit attention condition for the initiator
3060c66ac9dbSNicholas Bellinger 		 *    port associated with every I_T nexus that lost its
3061c66ac9dbSNicholas Bellinger 		 *    persistent reservation and/or registration, with the
3062c66ac9dbSNicholas Bellinger 		 *    additional sense code set to REGISTRATIONS PREEMPTED;
3063c66ac9dbSNicholas Bellinger 		 */
3064c51c8e7bSHannes Reinecke 		target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
30659e08e34eSMarco Sanvido 				ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3066c66ac9dbSNicholas Bellinger 	}
3067c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
3068c66ac9dbSNicholas Bellinger 	/*
3069c66ac9dbSNicholas Bellinger 	 * c) Establish a persistent reservation for the preempting
3070c66ac9dbSNicholas Bellinger 	 *    I_T nexus using the contents of the SCOPE and TYPE fields;
3071c66ac9dbSNicholas Bellinger 	 */
3072c66ac9dbSNicholas Bellinger 	__core_scsi3_complete_pro_preempt(dev, pr_reg_n,
307333ce6a87SAndy Grover 			(preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
307433ce6a87SAndy Grover 			type, scope, preempt_type);
3075c66ac9dbSNicholas Bellinger 	/*
3076c66ac9dbSNicholas Bellinger 	 * d) Process tasks as defined in 5.7.1;
3077c66ac9dbSNicholas Bellinger 	 * e) See above..
3078c66ac9dbSNicholas Bellinger 	 * f) If the type or scope has changed, then for every I_T nexus
3079c66ac9dbSNicholas Bellinger 	 *    whose reservation key was not removed, except for the I_T
3080c66ac9dbSNicholas Bellinger 	 *    nexus on which the PERSISTENT RESERVE OUT command was
3081c66ac9dbSNicholas Bellinger 	 *    received, the device server shall establish a unit
3082c66ac9dbSNicholas Bellinger 	 *    attention condition for the initiator port associated with
3083c66ac9dbSNicholas Bellinger 	 *    that I_T nexus, with the additional sense code set to
3084c66ac9dbSNicholas Bellinger 	 *    RESERVATIONS RELEASED. If the type or scope have not
3085c66ac9dbSNicholas Bellinger 	 *    changed, then no unit attention condition(s) shall be
3086c66ac9dbSNicholas Bellinger 	 *    established for this reason.
3087c66ac9dbSNicholas Bellinger 	 */
3088c66ac9dbSNicholas Bellinger 	if ((prh_type != type) || (prh_scope != scope)) {
3089c66ac9dbSNicholas Bellinger 		spin_lock(&pr_tmpl->registration_lock);
3090c66ac9dbSNicholas Bellinger 		list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3091c66ac9dbSNicholas Bellinger 				&pr_tmpl->registration_list, pr_reg_list) {
3092c66ac9dbSNicholas Bellinger 
3093c66ac9dbSNicholas Bellinger 			calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3094c66ac9dbSNicholas Bellinger 			if (calling_it_nexus)
3095c66ac9dbSNicholas Bellinger 				continue;
3096c66ac9dbSNicholas Bellinger 
3097c51c8e7bSHannes Reinecke 			target_ua_allocate_lun(pr_reg->pr_reg_nacl,
3098c66ac9dbSNicholas Bellinger 					pr_reg->pr_res_mapped_lun, 0x2A,
3099c66ac9dbSNicholas Bellinger 					ASCQ_2AH_RESERVATIONS_RELEASED);
3100c66ac9dbSNicholas Bellinger 		}
3101c66ac9dbSNicholas Bellinger 		spin_unlock(&pr_tmpl->registration_lock);
3102c66ac9dbSNicholas Bellinger 	}
3103c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
3104c66ac9dbSNicholas Bellinger 	/*
3105c66ac9dbSNicholas Bellinger 	 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3106c66ac9dbSNicholas Bellinger 	 * All received CDBs for the matching existing reservation and
3107c66ac9dbSNicholas Bellinger 	 * registrations undergo ABORT_TASK logic.
3108c66ac9dbSNicholas Bellinger 	 *
3109c66ac9dbSNicholas Bellinger 	 * From there, core_scsi3_release_preempt_and_abort() will
3110c66ac9dbSNicholas Bellinger 	 * release every registration in the list (which have already
3111c66ac9dbSNicholas Bellinger 	 * been removed from the primary pr_reg list), except the
3112c66ac9dbSNicholas Bellinger 	 * new persistent reservation holder, the calling Initiator Port.
3113c66ac9dbSNicholas Bellinger 	 */
311433ce6a87SAndy Grover 	if (preempt_type == PREEMPT_AND_ABORT) {
3115c66ac9dbSNicholas Bellinger 		core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3116c66ac9dbSNicholas Bellinger 		core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3117c66ac9dbSNicholas Bellinger 						pr_reg_n);
3118c66ac9dbSNicholas Bellinger 	}
3119c66ac9dbSNicholas Bellinger 
3120459f213bSAndy Grover 	if (pr_tmpl->pr_aptpl_active)
3121459f213bSAndy Grover 		core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
3122c66ac9dbSNicholas Bellinger 
3123c66ac9dbSNicholas Bellinger 	core_scsi3_put_pr_reg(pr_reg_n);
31245951146dSAndy Grover 	core_scsi3_pr_generation(cmd->se_dev);
3125c66ac9dbSNicholas Bellinger 	return 0;
3126c66ac9dbSNicholas Bellinger }
3127c66ac9dbSNicholas Bellinger 
3128de103c93SChristoph Hellwig static sense_reason_t
3129de103c93SChristoph Hellwig core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope,
313033ce6a87SAndy Grover 		u64 res_key, u64 sa_res_key, enum preempt_type preempt_type)
3131c66ac9dbSNicholas Bellinger {
3132c66ac9dbSNicholas Bellinger 	switch (type) {
3133c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE:
3134c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS:
3135c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3136c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3137c66ac9dbSNicholas Bellinger 	case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3138c66ac9dbSNicholas Bellinger 	case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
3139de103c93SChristoph Hellwig 		return core_scsi3_pro_preempt(cmd, type, scope, res_key,
314033ce6a87SAndy Grover 					      sa_res_key, preempt_type);
3141c66ac9dbSNicholas Bellinger 	default:
31426708bb27SAndy Grover 		pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
314333ce6a87SAndy Grover 			" Type: 0x%02x\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", type);
3144de103c93SChristoph Hellwig 		return TCM_INVALID_CDB_FIELD;
3145c66ac9dbSNicholas Bellinger 	}
3146c66ac9dbSNicholas Bellinger }
3147c66ac9dbSNicholas Bellinger 
3148c66ac9dbSNicholas Bellinger 
3149de103c93SChristoph Hellwig static sense_reason_t
3150de103c93SChristoph Hellwig core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key,
3151de103c93SChristoph Hellwig 		u64 sa_res_key, int aptpl, int unreg)
3152c66ac9dbSNicholas Bellinger {
3153e3d6f909SAndy Grover 	struct se_session *se_sess = cmd->se_sess;
31545951146dSAndy Grover 	struct se_device *dev = cmd->se_dev;
315528168905SJörn Engel 	struct se_dev_entry *dest_se_deve = NULL;
3156adf653f9SChristoph Hellwig 	struct se_lun *se_lun = cmd->se_lun, *tmp_lun;
3157c66ac9dbSNicholas Bellinger 	struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3158c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
31599ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3160c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
31610fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
316205d1c7c0SAndy Grover 	unsigned char *buf;
31632650d71eSChristoph Hellwig 	const unsigned char *initiator_str;
316413ba564cSJulia Lawall 	char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
3165c66ac9dbSNicholas Bellinger 	u32 tid_len, tmp_tid_len;
3166d2843c17SAndy Grover 	int new_reg = 0, type, scope, matching_iname;
3167de103c93SChristoph Hellwig 	sense_reason_t ret;
3168c66ac9dbSNicholas Bellinger 	unsigned short rtpi;
3169c66ac9dbSNicholas Bellinger 	unsigned char proto_ident;
3170c66ac9dbSNicholas Bellinger 
31716708bb27SAndy Grover 	if (!se_sess || !se_lun) {
31726708bb27SAndy Grover 		pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3173de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3174c66ac9dbSNicholas Bellinger 	}
3175de103c93SChristoph Hellwig 
3176c66ac9dbSNicholas Bellinger 	memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3177c66ac9dbSNicholas Bellinger 	se_tpg = se_sess->se_tpg;
3178e3d6f909SAndy Grover 	tf_ops = se_tpg->se_tpg_tfo;
3179c66ac9dbSNicholas Bellinger 	/*
3180c66ac9dbSNicholas Bellinger 	 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3181c66ac9dbSNicholas Bellinger 	 *	Register behaviors for a REGISTER AND MOVE service action
3182c66ac9dbSNicholas Bellinger 	 *
3183c66ac9dbSNicholas Bellinger 	 * Locate the existing *pr_reg via struct se_node_acl pointers
3184c66ac9dbSNicholas Bellinger 	 */
31855951146dSAndy Grover 	pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3186c66ac9dbSNicholas Bellinger 				se_sess);
31876708bb27SAndy Grover 	if (!pr_reg) {
31886708bb27SAndy Grover 		pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3189c66ac9dbSNicholas Bellinger 			" *pr_reg for REGISTER_AND_MOVE\n");
3190de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3191c66ac9dbSNicholas Bellinger 	}
3192c66ac9dbSNicholas Bellinger 	/*
3193c66ac9dbSNicholas Bellinger 	 * The provided reservation key much match the existing reservation key
3194c66ac9dbSNicholas Bellinger 	 * provided during this initiator's I_T nexus registration.
3195c66ac9dbSNicholas Bellinger 	 */
3196c66ac9dbSNicholas Bellinger 	if (res_key != pr_reg->pr_res_key) {
31976708bb27SAndy Grover 		pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3198c66ac9dbSNicholas Bellinger 			" res_key: 0x%016Lx does not match existing SA REGISTER"
3199c66ac9dbSNicholas Bellinger 			" res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
3200de103c93SChristoph Hellwig 		ret = TCM_RESERVATION_CONFLICT;
3201de103c93SChristoph Hellwig 		goto out_put_pr_reg;
3202c66ac9dbSNicholas Bellinger 	}
3203c66ac9dbSNicholas Bellinger 	/*
3204c66ac9dbSNicholas Bellinger 	 * The service active reservation key needs to be non zero
3205c66ac9dbSNicholas Bellinger 	 */
32066708bb27SAndy Grover 	if (!sa_res_key) {
32076708bb27SAndy Grover 		pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3208c66ac9dbSNicholas Bellinger 			" sa_res_key\n");
3209de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
3210de103c93SChristoph Hellwig 		goto out_put_pr_reg;
3211c66ac9dbSNicholas Bellinger 	}
321205d1c7c0SAndy Grover 
3213c66ac9dbSNicholas Bellinger 	/*
3214c66ac9dbSNicholas Bellinger 	 * Determine the Relative Target Port Identifier where the reservation
3215c66ac9dbSNicholas Bellinger 	 * will be moved to for the TransportID containing SCSI initiator WWN
3216c66ac9dbSNicholas Bellinger 	 * information.
3217c66ac9dbSNicholas Bellinger 	 */
32184949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
3219de103c93SChristoph Hellwig 	if (!buf) {
322079dd6f2fStangwenji 		ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
3221de103c93SChristoph Hellwig 		goto out_put_pr_reg;
3222de103c93SChristoph Hellwig 	}
3223de103c93SChristoph Hellwig 
3224a85d667eSBart Van Assche 	rtpi = get_unaligned_be16(&buf[18]);
3225a85d667eSBart Van Assche 	tid_len = get_unaligned_be32(&buf[20]);
32264949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
322705d1c7c0SAndy Grover 	buf = NULL;
3228c66ac9dbSNicholas Bellinger 
3229c66ac9dbSNicholas Bellinger 	if ((tid_len + 24) != cmd->data_length) {
32306708bb27SAndy Grover 		pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3231c66ac9dbSNicholas Bellinger 			" does not equal CDB data_length: %u\n", tid_len,
3232c66ac9dbSNicholas Bellinger 			cmd->data_length);
3233de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
3234de103c93SChristoph Hellwig 		goto out_put_pr_reg;
3235c66ac9dbSNicholas Bellinger 	}
3236c66ac9dbSNicholas Bellinger 
3237c66ac9dbSNicholas Bellinger 	spin_lock(&dev->se_port_lock);
3238adf653f9SChristoph Hellwig 	list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
3239adf653f9SChristoph Hellwig 		if (tmp_lun->lun_rtpi != rtpi)
3240c66ac9dbSNicholas Bellinger 			continue;
3241adf653f9SChristoph Hellwig 		dest_se_tpg = tmp_lun->lun_tpg;
3242e3d6f909SAndy Grover 		dest_tf_ops = dest_se_tpg->se_tpg_tfo;
32436708bb27SAndy Grover 		if (!dest_tf_ops)
3244c66ac9dbSNicholas Bellinger 			continue;
3245c66ac9dbSNicholas Bellinger 
324633940d09SJoern Engel 		atomic_inc_mb(&dest_se_tpg->tpg_pr_ref_count);
3247c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->se_port_lock);
3248c66ac9dbSNicholas Bellinger 
3249de103c93SChristoph Hellwig 		if (core_scsi3_tpg_depend_item(dest_se_tpg)) {
32506708bb27SAndy Grover 			pr_err("core_scsi3_tpg_depend_item() failed"
3251c66ac9dbSNicholas Bellinger 				" for dest_se_tpg\n");
325233940d09SJoern Engel 			atomic_dec_mb(&dest_se_tpg->tpg_pr_ref_count);
3253de103c93SChristoph Hellwig 			ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3254de103c93SChristoph Hellwig 			goto out_put_pr_reg;
3255c66ac9dbSNicholas Bellinger 		}
3256c66ac9dbSNicholas Bellinger 
3257c66ac9dbSNicholas Bellinger 		spin_lock(&dev->se_port_lock);
3258c66ac9dbSNicholas Bellinger 		break;
3259c66ac9dbSNicholas Bellinger 	}
3260c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->se_port_lock);
3261c66ac9dbSNicholas Bellinger 
32626708bb27SAndy Grover 	if (!dest_se_tpg || !dest_tf_ops) {
32636708bb27SAndy Grover 		pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3264c66ac9dbSNicholas Bellinger 			" fabric ops from Relative Target Port Identifier:"
3265c66ac9dbSNicholas Bellinger 			" %hu\n", rtpi);
3266de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
3267de103c93SChristoph Hellwig 		goto out_put_pr_reg;
3268c66ac9dbSNicholas Bellinger 	}
326905d1c7c0SAndy Grover 
32704949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
3271de103c93SChristoph Hellwig 	if (!buf) {
327279dd6f2fStangwenji 		ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
3273de103c93SChristoph Hellwig 		goto out_put_pr_reg;
3274de103c93SChristoph Hellwig 	}
3275c66ac9dbSNicholas Bellinger 	proto_ident = (buf[24] & 0x0f);
32768b1e1244SAndy Grover 
32776708bb27SAndy Grover 	pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3278c66ac9dbSNicholas Bellinger 			" 0x%02x\n", proto_ident);
32798b1e1244SAndy Grover 
32802aeeafaeSChristoph Hellwig 	if (proto_ident != dest_se_tpg->proto_id) {
32816708bb27SAndy Grover 		pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3282c66ac9dbSNicholas Bellinger 			" proto_ident: 0x%02x does not match ident: 0x%02x"
3283c66ac9dbSNicholas Bellinger 			" from fabric: %s\n", proto_ident,
32842aeeafaeSChristoph Hellwig 			dest_se_tpg->proto_id,
328530c7ca93SDavid Disseldorp 			dest_tf_ops->fabric_name);
3286de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
3287c66ac9dbSNicholas Bellinger 		goto out;
3288c66ac9dbSNicholas Bellinger 	}
32892650d71eSChristoph Hellwig 	initiator_str = target_parse_pr_out_transport_id(dest_se_tpg,
3290094bb5d7SRasmus Villemoes 			&buf[24], &tmp_tid_len, &iport_ptr);
32916708bb27SAndy Grover 	if (!initiator_str) {
32926708bb27SAndy Grover 		pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3293c66ac9dbSNicholas Bellinger 			" initiator_str from Transport ID\n");
3294de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
3295c66ac9dbSNicholas Bellinger 		goto out;
3296c66ac9dbSNicholas Bellinger 	}
3297c66ac9dbSNicholas Bellinger 
32984949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
329905d1c7c0SAndy Grover 	buf = NULL;
330005d1c7c0SAndy Grover 
33016708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
330230c7ca93SDavid Disseldorp 		" %s\n", dest_tf_ops->fabric_name, (iport_ptr != NULL) ?
3303c66ac9dbSNicholas Bellinger 		"port" : "device", initiator_str, (iport_ptr != NULL) ?
3304c66ac9dbSNicholas Bellinger 		iport_ptr : "");
3305c66ac9dbSNicholas Bellinger 	/*
3306c66ac9dbSNicholas Bellinger 	 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3307c66ac9dbSNicholas Bellinger 	 * action specifies a TransportID that is the same as the initiator port
3308c66ac9dbSNicholas Bellinger 	 * of the I_T nexus for the command received, then the command shall
3309c66ac9dbSNicholas Bellinger 	 * be terminated with CHECK CONDITION status, with the sense key set to
3310c66ac9dbSNicholas Bellinger 	 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3311c66ac9dbSNicholas Bellinger 	 * IN PARAMETER LIST.
3312c66ac9dbSNicholas Bellinger 	 */
3313c66ac9dbSNicholas Bellinger 	pr_reg_nacl = pr_reg->pr_reg_nacl;
3314c66ac9dbSNicholas Bellinger 	matching_iname = (!strcmp(initiator_str,
3315c66ac9dbSNicholas Bellinger 				  pr_reg_nacl->initiatorname)) ? 1 : 0;
33166708bb27SAndy Grover 	if (!matching_iname)
3317c66ac9dbSNicholas Bellinger 		goto after_iport_check;
3318c66ac9dbSNicholas Bellinger 
33196708bb27SAndy Grover 	if (!iport_ptr || !pr_reg->isid_present_at_reg) {
33206708bb27SAndy Grover 		pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3321c66ac9dbSNicholas Bellinger 			" matches: %s on received I_T Nexus\n", initiator_str,
3322c66ac9dbSNicholas Bellinger 			pr_reg_nacl->initiatorname);
3323de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
3324c66ac9dbSNicholas Bellinger 		goto out;
3325c66ac9dbSNicholas Bellinger 	}
33266708bb27SAndy Grover 	if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
33276708bb27SAndy Grover 		pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3328c66ac9dbSNicholas Bellinger 			" matches: %s %s on received I_T Nexus\n",
3329c66ac9dbSNicholas Bellinger 			initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3330c66ac9dbSNicholas Bellinger 			pr_reg->pr_reg_isid);
3331de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
3332c66ac9dbSNicholas Bellinger 		goto out;
3333c66ac9dbSNicholas Bellinger 	}
3334c66ac9dbSNicholas Bellinger after_iport_check:
3335c66ac9dbSNicholas Bellinger 	/*
3336c66ac9dbSNicholas Bellinger 	 * Locate the destination struct se_node_acl from the received Transport ID
3337c66ac9dbSNicholas Bellinger 	 */
3338403edd78SNicholas Bellinger 	mutex_lock(&dest_se_tpg->acl_node_mutex);
3339c66ac9dbSNicholas Bellinger 	dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3340c66ac9dbSNicholas Bellinger 				initiator_str);
334133940d09SJoern Engel 	if (dest_node_acl)
334233940d09SJoern Engel 		atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
3343403edd78SNicholas Bellinger 	mutex_unlock(&dest_se_tpg->acl_node_mutex);
3344c66ac9dbSNicholas Bellinger 
33456708bb27SAndy Grover 	if (!dest_node_acl) {
33466708bb27SAndy Grover 		pr_err("Unable to locate %s dest_node_acl for"
334730c7ca93SDavid Disseldorp 			" TransportID%s\n", dest_tf_ops->fabric_name,
3348c66ac9dbSNicholas Bellinger 			initiator_str);
3349de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
3350c66ac9dbSNicholas Bellinger 		goto out;
3351c66ac9dbSNicholas Bellinger 	}
3352de103c93SChristoph Hellwig 
3353de103c93SChristoph Hellwig 	if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
33546708bb27SAndy Grover 		pr_err("core_scsi3_nodeacl_depend_item() for"
3355c66ac9dbSNicholas Bellinger 			" dest_node_acl\n");
335633940d09SJoern Engel 		atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
3357c66ac9dbSNicholas Bellinger 		dest_node_acl = NULL;
3358de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
3359c66ac9dbSNicholas Bellinger 		goto out;
3360c66ac9dbSNicholas Bellinger 	}
33618b1e1244SAndy Grover 
33626708bb27SAndy Grover 	pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
336330c7ca93SDavid Disseldorp 		" %s from TransportID\n", dest_tf_ops->fabric_name,
3364c66ac9dbSNicholas Bellinger 		dest_node_acl->initiatorname);
33658b1e1244SAndy Grover 
3366c66ac9dbSNicholas Bellinger 	/*
3367c66ac9dbSNicholas Bellinger 	 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3368c66ac9dbSNicholas Bellinger 	 * PORT IDENTIFIER.
3369c66ac9dbSNicholas Bellinger 	 */
3370c66ac9dbSNicholas Bellinger 	dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
33716708bb27SAndy Grover 	if (!dest_se_deve) {
33726708bb27SAndy Grover 		pr_err("Unable to locate %s dest_se_deve from RTPI:"
337330c7ca93SDavid Disseldorp 			" %hu\n",  dest_tf_ops->fabric_name, rtpi);
3374de103c93SChristoph Hellwig 		ret = TCM_INVALID_PARAMETER_LIST;
3375c66ac9dbSNicholas Bellinger 		goto out;
3376c66ac9dbSNicholas Bellinger 	}
3377c66ac9dbSNicholas Bellinger 
3378de103c93SChristoph Hellwig 	if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
33796708bb27SAndy Grover 		pr_err("core_scsi3_lunacl_depend_item() failed\n");
338029a05deeSNicholas Bellinger 		kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
3381c66ac9dbSNicholas Bellinger 		dest_se_deve = NULL;
3382de103c93SChristoph Hellwig 		ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3383c66ac9dbSNicholas Bellinger 		goto out;
3384c66ac9dbSNicholas Bellinger 	}
33858b1e1244SAndy Grover 
33866708bb27SAndy Grover 	pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3387f2d30680SHannes Reinecke 		" ACL for dest_se_deve->mapped_lun: %llu\n",
338830c7ca93SDavid Disseldorp 		dest_tf_ops->fabric_name, dest_node_acl->initiatorname,
3389c66ac9dbSNicholas Bellinger 		dest_se_deve->mapped_lun);
33908b1e1244SAndy Grover 
3391c66ac9dbSNicholas Bellinger 	/*
3392c66ac9dbSNicholas Bellinger 	 * A persistent reservation needs to already existing in order to
3393c66ac9dbSNicholas Bellinger 	 * successfully complete the REGISTER_AND_MOVE service action..
3394c66ac9dbSNicholas Bellinger 	 */
3395c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
3396c66ac9dbSNicholas Bellinger 	pr_res_holder = dev->dev_pr_res_holder;
33976708bb27SAndy Grover 	if (!pr_res_holder) {
33986708bb27SAndy Grover 		pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3399c66ac9dbSNicholas Bellinger 			" currently held\n");
3400c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
3401de103c93SChristoph Hellwig 		ret = TCM_INVALID_CDB_FIELD;
3402c66ac9dbSNicholas Bellinger 		goto out;
3403c66ac9dbSNicholas Bellinger 	}
3404c66ac9dbSNicholas Bellinger 	/*
3405c66ac9dbSNicholas Bellinger 	 * The received on I_T Nexus must be the reservation holder.
3406c66ac9dbSNicholas Bellinger 	 *
3407c66ac9dbSNicholas Bellinger 	 * From spc4r17 section 5.7.8  Table 50 --
3408c66ac9dbSNicholas Bellinger 	 * 	Register behaviors for a REGISTER AND MOVE service action
3409c66ac9dbSNicholas Bellinger 	 */
3410e673dc92SIlias Tsitsimpis 	if (!is_reservation_holder(pr_res_holder, pr_reg)) {
34116708bb27SAndy Grover 		pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3412c66ac9dbSNicholas Bellinger 			" Nexus is not reservation holder\n");
3413c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
3414de103c93SChristoph Hellwig 		ret = TCM_RESERVATION_CONFLICT;
3415c66ac9dbSNicholas Bellinger 		goto out;
3416c66ac9dbSNicholas Bellinger 	}
3417c66ac9dbSNicholas Bellinger 	/*
3418c66ac9dbSNicholas Bellinger 	 * From spc4r17 section 5.7.8: registering and moving reservation
3419c66ac9dbSNicholas Bellinger 	 *
3420c66ac9dbSNicholas Bellinger 	 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3421c66ac9dbSNicholas Bellinger 	 * action is received and the established persistent reservation is a
3422c66ac9dbSNicholas Bellinger 	 * Write Exclusive - All Registrants type or Exclusive Access -
3423c66ac9dbSNicholas Bellinger 	 * All Registrants type reservation, then the command shall be completed
3424c66ac9dbSNicholas Bellinger 	 * with RESERVATION CONFLICT status.
3425c66ac9dbSNicholas Bellinger 	 */
3426c66ac9dbSNicholas Bellinger 	if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3427c66ac9dbSNicholas Bellinger 	    (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
34286708bb27SAndy Grover 		pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3429c66ac9dbSNicholas Bellinger 			" reservation for type: %s\n",
3430c66ac9dbSNicholas Bellinger 			core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3431c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
3432de103c93SChristoph Hellwig 		ret = TCM_RESERVATION_CONFLICT;
3433c66ac9dbSNicholas Bellinger 		goto out;
3434c66ac9dbSNicholas Bellinger 	}
3435c66ac9dbSNicholas Bellinger 	pr_res_nacl = pr_res_holder->pr_reg_nacl;
3436c66ac9dbSNicholas Bellinger 	/*
3437c66ac9dbSNicholas Bellinger 	 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3438c66ac9dbSNicholas Bellinger 	 */
3439c66ac9dbSNicholas Bellinger 	type = pr_res_holder->pr_res_type;
3440c66ac9dbSNicholas Bellinger 	scope = pr_res_holder->pr_res_type;
3441c66ac9dbSNicholas Bellinger 	/*
3442c66ac9dbSNicholas Bellinger 	 * c) Associate the reservation key specified in the SERVICE ACTION
3443c66ac9dbSNicholas Bellinger 	 *    RESERVATION KEY field with the I_T nexus specified as the
3444c66ac9dbSNicholas Bellinger 	 *    destination of the register and move, where:
3445c66ac9dbSNicholas Bellinger 	 *    A) The I_T nexus is specified by the TransportID and the
3446c66ac9dbSNicholas Bellinger 	 *	 RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3447c66ac9dbSNicholas Bellinger 	 *    B) Regardless of the TransportID format used, the association for
3448c66ac9dbSNicholas Bellinger 	 *       the initiator port is based on either the initiator port name
3449c66ac9dbSNicholas Bellinger 	 *       (see 3.1.71) on SCSI transport protocols where port names are
3450c66ac9dbSNicholas Bellinger 	 *       required or the initiator port identifier (see 3.1.70) on SCSI
3451c66ac9dbSNicholas Bellinger 	 *       transport protocols where port names are not required;
3452c66ac9dbSNicholas Bellinger 	 * d) Register the reservation key specified in the SERVICE ACTION
3453c66ac9dbSNicholas Bellinger 	 *    RESERVATION KEY field;
3454c66ac9dbSNicholas Bellinger 	 * e) Retain the reservation key specified in the SERVICE ACTION
3455c66ac9dbSNicholas Bellinger 	 *    RESERVATION KEY field and associated information;
3456c66ac9dbSNicholas Bellinger 	 *
3457c66ac9dbSNicholas Bellinger 	 * Also, It is not an error for a REGISTER AND MOVE service action to
3458c66ac9dbSNicholas Bellinger 	 * register an I_T nexus that is already registered with the same
3459c66ac9dbSNicholas Bellinger 	 * reservation key or a different reservation key.
3460c66ac9dbSNicholas Bellinger 	 */
3461c66ac9dbSNicholas Bellinger 	dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3462c66ac9dbSNicholas Bellinger 					iport_ptr);
34636708bb27SAndy Grover 	if (!dest_pr_reg) {
346479dc9c9eSNicholas Bellinger 		struct se_lun *dest_lun = rcu_dereference_check(dest_se_deve->se_lun,
34652c935bc5SPeter Zijlstra 				kref_read(&dest_se_deve->pr_kref) != 0);
346679dc9c9eSNicholas Bellinger 
3467c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
346879dc9c9eSNicholas Bellinger 		if (core_scsi3_alloc_registration(cmd->se_dev, dest_node_acl,
346979dc9c9eSNicholas Bellinger 					dest_lun, dest_se_deve, dest_se_deve->mapped_lun,
347079dc9c9eSNicholas Bellinger 					iport_ptr, sa_res_key, 0, aptpl, 2, 1)) {
347179dd6f2fStangwenji 			ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
3472c66ac9dbSNicholas Bellinger 			goto out;
3473c66ac9dbSNicholas Bellinger 		}
347480bfdfa9SNicholas Bellinger 		spin_lock(&dev->dev_reservation_lock);
3475c66ac9dbSNicholas Bellinger 		dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3476c66ac9dbSNicholas Bellinger 						iport_ptr);
3477c66ac9dbSNicholas Bellinger 		new_reg = 1;
3478c66ac9dbSNicholas Bellinger 	}
3479c66ac9dbSNicholas Bellinger 	/*
3480c66ac9dbSNicholas Bellinger 	 * f) Release the persistent reservation for the persistent reservation
3481c66ac9dbSNicholas Bellinger 	 *    holder (i.e., the I_T nexus on which the
3482c66ac9dbSNicholas Bellinger 	 */
3483c66ac9dbSNicholas Bellinger 	__core_scsi3_complete_pro_release(dev, pr_res_nacl,
34846c3c9baaSNicholas Bellinger 					  dev->dev_pr_res_holder, 0, 0);
3485c66ac9dbSNicholas Bellinger 	/*
3486c66ac9dbSNicholas Bellinger 	 * g) Move the persistent reservation to the specified I_T nexus using
3487c66ac9dbSNicholas Bellinger 	 *    the same scope and type as the persistent reservation released in
3488c66ac9dbSNicholas Bellinger 	 *    item f); and
3489c66ac9dbSNicholas Bellinger 	 */
3490c66ac9dbSNicholas Bellinger 	dev->dev_pr_res_holder = dest_pr_reg;
3491c66ac9dbSNicholas Bellinger 	dest_pr_reg->pr_res_holder = 1;
3492c66ac9dbSNicholas Bellinger 	dest_pr_reg->pr_res_type = type;
3493c66ac9dbSNicholas Bellinger 	pr_reg->pr_res_scope = scope;
3494d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
3495c66ac9dbSNicholas Bellinger 	/*
3496c66ac9dbSNicholas Bellinger 	 * Increment PRGeneration for existing registrations..
3497c66ac9dbSNicholas Bellinger 	 */
34986708bb27SAndy Grover 	if (!new_reg)
3499c66ac9dbSNicholas Bellinger 		dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3500c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
3501c66ac9dbSNicholas Bellinger 
35026708bb27SAndy Grover 	pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3503c66ac9dbSNicholas Bellinger 		" created new reservation holder TYPE: %s on object RTPI:"
350430c7ca93SDavid Disseldorp 		" %hu  PRGeneration: 0x%08x\n", dest_tf_ops->fabric_name,
3505c66ac9dbSNicholas Bellinger 		core_scsi3_pr_dump_type(type), rtpi,
3506c66ac9dbSNicholas Bellinger 		dest_pr_reg->pr_res_generation);
35076708bb27SAndy Grover 	pr_debug("SPC-3 PR Successfully moved reservation from"
3508c66ac9dbSNicholas Bellinger 		" %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
350930c7ca93SDavid Disseldorp 		tf_ops->fabric_name, pr_reg_nacl->initiatorname,
351030c7ca93SDavid Disseldorp 		i_buf, dest_tf_ops->fabric_name,
3511c66ac9dbSNicholas Bellinger 		dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3512c66ac9dbSNicholas Bellinger 		iport_ptr : "");
3513c66ac9dbSNicholas Bellinger 	/*
3514c66ac9dbSNicholas Bellinger 	 * It is now safe to release configfs group dependencies for destination
3515c66ac9dbSNicholas Bellinger 	 * of Transport ID Initiator Device/Port Identifier
3516c66ac9dbSNicholas Bellinger 	 */
3517c66ac9dbSNicholas Bellinger 	core_scsi3_lunacl_undepend_item(dest_se_deve);
3518c66ac9dbSNicholas Bellinger 	core_scsi3_nodeacl_undepend_item(dest_node_acl);
3519c66ac9dbSNicholas Bellinger 	core_scsi3_tpg_undepend_item(dest_se_tpg);
3520c66ac9dbSNicholas Bellinger 	/*
3521c66ac9dbSNicholas Bellinger 	 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3522c66ac9dbSNicholas Bellinger 	 * nexus on which PERSISTENT RESERVE OUT command was received.
3523c66ac9dbSNicholas Bellinger 	 */
3524c66ac9dbSNicholas Bellinger 	if (unreg) {
3525c66ac9dbSNicholas Bellinger 		spin_lock(&pr_tmpl->registration_lock);
3526c66ac9dbSNicholas Bellinger 		__core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3527c66ac9dbSNicholas Bellinger 		spin_unlock(&pr_tmpl->registration_lock);
3528c66ac9dbSNicholas Bellinger 	} else
3529c66ac9dbSNicholas Bellinger 		core_scsi3_put_pr_reg(pr_reg);
3530c66ac9dbSNicholas Bellinger 
3531459f213bSAndy Grover 	core_scsi3_update_and_write_aptpl(cmd->se_dev, aptpl);
3532c66ac9dbSNicholas Bellinger 
3533c66ac9dbSNicholas Bellinger 	core_scsi3_put_pr_reg(dest_pr_reg);
3534c66ac9dbSNicholas Bellinger 	return 0;
3535c66ac9dbSNicholas Bellinger out:
353605d1c7c0SAndy Grover 	if (buf)
35374949314cSAndy Grover 		transport_kunmap_data_sg(cmd);
3538c66ac9dbSNicholas Bellinger 	if (dest_se_deve)
3539c66ac9dbSNicholas Bellinger 		core_scsi3_lunacl_undepend_item(dest_se_deve);
3540c66ac9dbSNicholas Bellinger 	if (dest_node_acl)
3541c66ac9dbSNicholas Bellinger 		core_scsi3_nodeacl_undepend_item(dest_node_acl);
3542c66ac9dbSNicholas Bellinger 	core_scsi3_tpg_undepend_item(dest_se_tpg);
3543de103c93SChristoph Hellwig 
3544de103c93SChristoph Hellwig out_put_pr_reg:
3545c66ac9dbSNicholas Bellinger 	core_scsi3_put_pr_reg(pr_reg);
3546c66ac9dbSNicholas Bellinger 	return ret;
3547c66ac9dbSNicholas Bellinger }
3548c66ac9dbSNicholas Bellinger 
3549c66ac9dbSNicholas Bellinger /*
3550c66ac9dbSNicholas Bellinger  * See spc4r17 section 6.14 Table 170
3551c66ac9dbSNicholas Bellinger  */
3552de103c93SChristoph Hellwig sense_reason_t
3553de103c93SChristoph Hellwig target_scsi3_emulate_pr_out(struct se_cmd *cmd)
3554c66ac9dbSNicholas Bellinger {
355592404e60SNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
3556617c0e06SChristoph Hellwig 	unsigned char *cdb = &cmd->t_task_cdb[0];
355705d1c7c0SAndy Grover 	unsigned char *buf;
3558c66ac9dbSNicholas Bellinger 	u64 res_key, sa_res_key;
3559c66ac9dbSNicholas Bellinger 	int sa, scope, type, aptpl;
3560c66ac9dbSNicholas Bellinger 	int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
3561de103c93SChristoph Hellwig 	sense_reason_t ret;
3562617c0e06SChristoph Hellwig 
3563617c0e06SChristoph Hellwig 	/*
3564617c0e06SChristoph Hellwig 	 * Following spc2r20 5.5.1 Reservations overview:
3565617c0e06SChristoph Hellwig 	 *
3566617c0e06SChristoph Hellwig 	 * If a logical unit has been reserved by any RESERVE command and is
3567617c0e06SChristoph Hellwig 	 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3568617c0e06SChristoph Hellwig 	 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3569617c0e06SChristoph Hellwig 	 * initiator or service action and shall terminate with a RESERVATION
3570617c0e06SChristoph Hellwig 	 * CONFLICT status.
3571617c0e06SChristoph Hellwig 	 */
35720fd97ccfSChristoph Hellwig 	if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
3573617c0e06SChristoph Hellwig 		pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3574617c0e06SChristoph Hellwig 			" SPC-2 reservation is held, returning"
3575617c0e06SChristoph Hellwig 			" RESERVATION_CONFLICT\n");
3576de103c93SChristoph Hellwig 		return TCM_RESERVATION_CONFLICT;
3577617c0e06SChristoph Hellwig 	}
3578617c0e06SChristoph Hellwig 
3579c66ac9dbSNicholas Bellinger 	/*
3580c66ac9dbSNicholas Bellinger 	 * FIXME: A NULL struct se_session pointer means an this is not coming from
3581c66ac9dbSNicholas Bellinger 	 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3582c66ac9dbSNicholas Bellinger 	 */
3583de103c93SChristoph Hellwig 	if (!cmd->se_sess)
3584de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3585c66ac9dbSNicholas Bellinger 
3586c66ac9dbSNicholas Bellinger 	if (cmd->data_length < 24) {
35876708bb27SAndy Grover 		pr_warn("SPC-PR: Received PR OUT parameter list"
3588c66ac9dbSNicholas Bellinger 			" length too small: %u\n", cmd->data_length);
35897ee00317STang Wenji 		return TCM_PARAMETER_LIST_LENGTH_ERROR;
3590c66ac9dbSNicholas Bellinger 	}
3591de103c93SChristoph Hellwig 
3592c66ac9dbSNicholas Bellinger 	/*
3593c66ac9dbSNicholas Bellinger 	 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3594c66ac9dbSNicholas Bellinger 	 */
3595c66ac9dbSNicholas Bellinger 	sa = (cdb[1] & 0x1f);
3596c66ac9dbSNicholas Bellinger 	scope = (cdb[2] & 0xf0);
3597c66ac9dbSNicholas Bellinger 	type = (cdb[2] & 0x0f);
359805d1c7c0SAndy Grover 
35994949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
3600de103c93SChristoph Hellwig 	if (!buf)
3601de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3602de103c93SChristoph Hellwig 
3603c66ac9dbSNicholas Bellinger 	/*
3604c66ac9dbSNicholas Bellinger 	 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3605c66ac9dbSNicholas Bellinger 	 */
3606a85d667eSBart Van Assche 	res_key = get_unaligned_be64(&buf[0]);
3607a85d667eSBart Van Assche 	sa_res_key = get_unaligned_be64(&buf[8]);
3608c66ac9dbSNicholas Bellinger 	/*
3609c66ac9dbSNicholas Bellinger 	 * REGISTER_AND_MOVE uses a different SA parameter list containing
3610c66ac9dbSNicholas Bellinger 	 * SCSI TransportIDs.
3611c66ac9dbSNicholas Bellinger 	 */
3612c66ac9dbSNicholas Bellinger 	if (sa != PRO_REGISTER_AND_MOVE) {
3613c66ac9dbSNicholas Bellinger 		spec_i_pt = (buf[20] & 0x08);
3614c66ac9dbSNicholas Bellinger 		all_tg_pt = (buf[20] & 0x04);
3615c66ac9dbSNicholas Bellinger 		aptpl = (buf[20] & 0x01);
3616c66ac9dbSNicholas Bellinger 	} else {
3617c66ac9dbSNicholas Bellinger 		aptpl = (buf[17] & 0x01);
3618c66ac9dbSNicholas Bellinger 		unreg = (buf[17] & 0x02);
3619c66ac9dbSNicholas Bellinger 	}
362092404e60SNicholas Bellinger 	/*
362192404e60SNicholas Bellinger 	 * If the backend device has been configured to force APTPL metadata
362292404e60SNicholas Bellinger 	 * write-out, go ahead and propigate aptpl=1 down now.
362392404e60SNicholas Bellinger 	 */
362492404e60SNicholas Bellinger 	if (dev->dev_attrib.force_pr_aptpl)
362592404e60SNicholas Bellinger 		aptpl = 1;
362692404e60SNicholas Bellinger 
36274949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
362805d1c7c0SAndy Grover 	buf = NULL;
362905d1c7c0SAndy Grover 
3630c66ac9dbSNicholas Bellinger 	/*
3631c66ac9dbSNicholas Bellinger 	 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3632c66ac9dbSNicholas Bellinger 	 */
36337ee00317STang Wenji 	if (spec_i_pt && (sa != PRO_REGISTER))
3634de103c93SChristoph Hellwig 		return TCM_INVALID_PARAMETER_LIST;
3635d29a5b6aSChristoph Hellwig 
3636c66ac9dbSNicholas Bellinger 	/*
3637c66ac9dbSNicholas Bellinger 	 * From spc4r17 section 6.14:
3638c66ac9dbSNicholas Bellinger 	 *
3639c66ac9dbSNicholas Bellinger 	 * If the SPEC_I_PT bit is set to zero, the service action is not
3640c66ac9dbSNicholas Bellinger 	 * REGISTER AND MOVE, and the parameter list length is not 24, then
3641c66ac9dbSNicholas Bellinger 	 * the command shall be terminated with CHECK CONDITION status, with
3642c66ac9dbSNicholas Bellinger 	 * the sense key set to ILLEGAL REQUEST, and the additional sense
3643c66ac9dbSNicholas Bellinger 	 * code set to PARAMETER LIST LENGTH ERROR.
3644c66ac9dbSNicholas Bellinger 	 */
36457ee00317STang Wenji 	if (!spec_i_pt && (sa != PRO_REGISTER_AND_MOVE) &&
3646c66ac9dbSNicholas Bellinger 	    (cmd->data_length != 24)) {
36476708bb27SAndy Grover 		pr_warn("SPC-PR: Received PR OUT illegal parameter"
3648c66ac9dbSNicholas Bellinger 			" list length: %u\n", cmd->data_length);
36497ee00317STang Wenji 		return TCM_PARAMETER_LIST_LENGTH_ERROR;
3650c66ac9dbSNicholas Bellinger 	}
3651de103c93SChristoph Hellwig 
3652c66ac9dbSNicholas Bellinger 	/*
3653c66ac9dbSNicholas Bellinger 	 * (core_scsi3_emulate_pro_* function parameters
3654c66ac9dbSNicholas Bellinger 	 * are defined by spc4r17 Table 174:
3655c66ac9dbSNicholas Bellinger 	 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3656c66ac9dbSNicholas Bellinger 	 */
3657c66ac9dbSNicholas Bellinger 	switch (sa) {
3658c66ac9dbSNicholas Bellinger 	case PRO_REGISTER:
3659d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_register(cmd,
366033ce6a87SAndy Grover 			res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER);
3661d29a5b6aSChristoph Hellwig 		break;
3662c66ac9dbSNicholas Bellinger 	case PRO_RESERVE:
3663d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3664d29a5b6aSChristoph Hellwig 		break;
3665c66ac9dbSNicholas Bellinger 	case PRO_RELEASE:
3666d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3667d29a5b6aSChristoph Hellwig 		break;
3668c66ac9dbSNicholas Bellinger 	case PRO_CLEAR:
3669d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3670d29a5b6aSChristoph Hellwig 		break;
3671c66ac9dbSNicholas Bellinger 	case PRO_PREEMPT:
3672d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
367333ce6a87SAndy Grover 					res_key, sa_res_key, PREEMPT);
3674d29a5b6aSChristoph Hellwig 		break;
3675c66ac9dbSNicholas Bellinger 	case PRO_PREEMPT_AND_ABORT:
3676d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
367733ce6a87SAndy Grover 					res_key, sa_res_key, PREEMPT_AND_ABORT);
3678d29a5b6aSChristoph Hellwig 		break;
3679c66ac9dbSNicholas Bellinger 	case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
3680d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_register(cmd,
368133ce6a87SAndy Grover 			0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER_AND_IGNORE_EXISTING_KEY);
3682d29a5b6aSChristoph Hellwig 		break;
3683c66ac9dbSNicholas Bellinger 	case PRO_REGISTER_AND_MOVE:
3684d29a5b6aSChristoph Hellwig 		ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
3685c66ac9dbSNicholas Bellinger 				sa_res_key, aptpl, unreg);
3686d29a5b6aSChristoph Hellwig 		break;
3687c66ac9dbSNicholas Bellinger 	default:
36886708bb27SAndy Grover 		pr_err("Unknown PERSISTENT_RESERVE_OUT service"
36897ee00317STang Wenji 			" action: 0x%02x\n", sa);
3690de103c93SChristoph Hellwig 		return TCM_INVALID_CDB_FIELD;
3691c66ac9dbSNicholas Bellinger 	}
3692c66ac9dbSNicholas Bellinger 
36936bb35e00SChristoph Hellwig 	if (!ret)
36946bb35e00SChristoph Hellwig 		target_complete_cmd(cmd, GOOD);
3695d29a5b6aSChristoph Hellwig 	return ret;
3696c66ac9dbSNicholas Bellinger }
3697c66ac9dbSNicholas Bellinger 
3698c66ac9dbSNicholas Bellinger /*
3699c66ac9dbSNicholas Bellinger  * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3700c66ac9dbSNicholas Bellinger  *
3701c66ac9dbSNicholas Bellinger  * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3702c66ac9dbSNicholas Bellinger  */
3703de103c93SChristoph Hellwig static sense_reason_t
3704de103c93SChristoph Hellwig core_scsi3_pri_read_keys(struct se_cmd *cmd)
3705c66ac9dbSNicholas Bellinger {
37060fd97ccfSChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
3707c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
370805d1c7c0SAndy Grover 	unsigned char *buf;
3709c66ac9dbSNicholas Bellinger 	u32 add_len = 0, off = 8;
3710c66ac9dbSNicholas Bellinger 
3711c66ac9dbSNicholas Bellinger 	if (cmd->data_length < 8) {
37126708bb27SAndy Grover 		pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3713c66ac9dbSNicholas Bellinger 			" too small\n", cmd->data_length);
3714de103c93SChristoph Hellwig 		return TCM_INVALID_CDB_FIELD;
3715c66ac9dbSNicholas Bellinger 	}
3716c66ac9dbSNicholas Bellinger 
37174949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
3718de103c93SChristoph Hellwig 	if (!buf)
3719de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3720de103c93SChristoph Hellwig 
3721a85d667eSBart Van Assche 	put_unaligned_be32(dev->t10_pr.pr_generation, buf);
3722c66ac9dbSNicholas Bellinger 
37230fd97ccfSChristoph Hellwig 	spin_lock(&dev->t10_pr.registration_lock);
37240fd97ccfSChristoph Hellwig 	list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
3725c66ac9dbSNicholas Bellinger 			pr_reg_list) {
3726c66ac9dbSNicholas Bellinger 		/*
3727c66ac9dbSNicholas Bellinger 		 * Check for overflow of 8byte PRI READ_KEYS payload and
3728c66ac9dbSNicholas Bellinger 		 * next reservation key list descriptor.
3729c66ac9dbSNicholas Bellinger 		 */
373063ce3c38SDavid Disseldorp 		if (off + 8 <= cmd->data_length) {
3731a85d667eSBart Van Assche 			put_unaligned_be64(pr_reg->pr_res_key, &buf[off]);
3732a85d667eSBart Van Assche 			off += 8;
373363ce3c38SDavid Disseldorp 		}
373463ce3c38SDavid Disseldorp 		/*
373563ce3c38SDavid Disseldorp 		 * SPC5r17: 6.16.2 READ KEYS service action
373663ce3c38SDavid Disseldorp 		 * The ADDITIONAL LENGTH field indicates the number of bytes in
373763ce3c38SDavid Disseldorp 		 * the Reservation key list. The contents of the ADDITIONAL
373863ce3c38SDavid Disseldorp 		 * LENGTH field are not altered based on the allocation length
373963ce3c38SDavid Disseldorp 		 */
3740c66ac9dbSNicholas Bellinger 		add_len += 8;
3741c66ac9dbSNicholas Bellinger 	}
37420fd97ccfSChristoph Hellwig 	spin_unlock(&dev->t10_pr.registration_lock);
3743c66ac9dbSNicholas Bellinger 
3744a85d667eSBart Van Assche 	put_unaligned_be32(add_len, &buf[4]);
3745c66ac9dbSNicholas Bellinger 
37464949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
374705d1c7c0SAndy Grover 
3748c66ac9dbSNicholas Bellinger 	return 0;
3749c66ac9dbSNicholas Bellinger }
3750c66ac9dbSNicholas Bellinger 
3751c66ac9dbSNicholas Bellinger /*
3752c66ac9dbSNicholas Bellinger  * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3753c66ac9dbSNicholas Bellinger  *
3754c66ac9dbSNicholas Bellinger  * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3755c66ac9dbSNicholas Bellinger  */
3756de103c93SChristoph Hellwig static sense_reason_t
3757de103c93SChristoph Hellwig core_scsi3_pri_read_reservation(struct se_cmd *cmd)
3758c66ac9dbSNicholas Bellinger {
37590fd97ccfSChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
3760c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
376105d1c7c0SAndy Grover 	unsigned char *buf;
3762c66ac9dbSNicholas Bellinger 	u64 pr_res_key;
3763c66ac9dbSNicholas Bellinger 	u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
3764c66ac9dbSNicholas Bellinger 
3765c66ac9dbSNicholas Bellinger 	if (cmd->data_length < 8) {
37666708bb27SAndy Grover 		pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
3767c66ac9dbSNicholas Bellinger 			" too small\n", cmd->data_length);
3768de103c93SChristoph Hellwig 		return TCM_INVALID_CDB_FIELD;
3769c66ac9dbSNicholas Bellinger 	}
3770c66ac9dbSNicholas Bellinger 
37714949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
3772de103c93SChristoph Hellwig 	if (!buf)
3773de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3774de103c93SChristoph Hellwig 
3775a85d667eSBart Van Assche 	put_unaligned_be32(dev->t10_pr.pr_generation, &buf[0]);
3776c66ac9dbSNicholas Bellinger 
37770fd97ccfSChristoph Hellwig 	spin_lock(&dev->dev_reservation_lock);
37780fd97ccfSChristoph Hellwig 	pr_reg = dev->dev_pr_res_holder;
3779ee1b1b9cSAndy Grover 	if (pr_reg) {
3780c66ac9dbSNicholas Bellinger 		/*
3781c66ac9dbSNicholas Bellinger 		 * Set the hardcoded Additional Length
3782c66ac9dbSNicholas Bellinger 		 */
3783a85d667eSBart Van Assche 		put_unaligned_be32(add_len, &buf[4]);
3784c66ac9dbSNicholas Bellinger 
378505d1c7c0SAndy Grover 		if (cmd->data_length < 22)
378605d1c7c0SAndy Grover 			goto err;
378705d1c7c0SAndy Grover 
3788c66ac9dbSNicholas Bellinger 		/*
3789c66ac9dbSNicholas Bellinger 		 * Set the Reservation key.
3790c66ac9dbSNicholas Bellinger 		 *
3791c66ac9dbSNicholas Bellinger 		 * From spc4r17, section 5.7.10:
3792c66ac9dbSNicholas Bellinger 		 * A persistent reservation holder has its reservation key
3793c66ac9dbSNicholas Bellinger 		 * returned in the parameter data from a PERSISTENT
3794c66ac9dbSNicholas Bellinger 		 * RESERVE IN command with READ RESERVATION service action as
3795c66ac9dbSNicholas Bellinger 		 * follows:
3796c66ac9dbSNicholas Bellinger 		 * a) For a persistent reservation of the type Write Exclusive
3797c66ac9dbSNicholas Bellinger 		 *    - All Registrants or Exclusive Access ­ All Regitrants,
3798c66ac9dbSNicholas Bellinger 		 *      the reservation key shall be set to zero; or
3799c66ac9dbSNicholas Bellinger 		 * b) For all other persistent reservation types, the
3800c66ac9dbSNicholas Bellinger 		 *    reservation key shall be set to the registered
3801c66ac9dbSNicholas Bellinger 		 *    reservation key for the I_T nexus that holds the
3802c66ac9dbSNicholas Bellinger 		 *    persistent reservation.
3803c66ac9dbSNicholas Bellinger 		 */
3804c66ac9dbSNicholas Bellinger 		if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3805c66ac9dbSNicholas Bellinger 		    (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
3806c66ac9dbSNicholas Bellinger 			pr_res_key = 0;
3807c66ac9dbSNicholas Bellinger 		else
3808c66ac9dbSNicholas Bellinger 			pr_res_key = pr_reg->pr_res_key;
3809c66ac9dbSNicholas Bellinger 
3810a85d667eSBart Van Assche 		put_unaligned_be64(pr_res_key, &buf[8]);
3811c66ac9dbSNicholas Bellinger 		/*
3812c66ac9dbSNicholas Bellinger 		 * Set the SCOPE and TYPE
3813c66ac9dbSNicholas Bellinger 		 */
3814c66ac9dbSNicholas Bellinger 		buf[21] = (pr_reg->pr_res_scope & 0xf0) |
3815c66ac9dbSNicholas Bellinger 			  (pr_reg->pr_res_type & 0x0f);
3816c66ac9dbSNicholas Bellinger 	}
381705d1c7c0SAndy Grover 
381805d1c7c0SAndy Grover err:
38190fd97ccfSChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
38204949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
3821c66ac9dbSNicholas Bellinger 
3822c66ac9dbSNicholas Bellinger 	return 0;
3823c66ac9dbSNicholas Bellinger }
3824c66ac9dbSNicholas Bellinger 
3825c66ac9dbSNicholas Bellinger /*
3826c66ac9dbSNicholas Bellinger  * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3827c66ac9dbSNicholas Bellinger  *
3828c66ac9dbSNicholas Bellinger  * See spc4r17 section 6.13.4 Table 165
3829c66ac9dbSNicholas Bellinger  */
3830de103c93SChristoph Hellwig static sense_reason_t
3831de103c93SChristoph Hellwig core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
3832c66ac9dbSNicholas Bellinger {
38335951146dSAndy Grover 	struct se_device *dev = cmd->se_dev;
38340fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
383505d1c7c0SAndy Grover 	unsigned char *buf;
3836c66ac9dbSNicholas Bellinger 	u16 add_len = 8; /* Hardcoded to 8. */
3837c66ac9dbSNicholas Bellinger 
3838c66ac9dbSNicholas Bellinger 	if (cmd->data_length < 6) {
38396708bb27SAndy Grover 		pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
3840c66ac9dbSNicholas Bellinger 			" %u too small\n", cmd->data_length);
3841de103c93SChristoph Hellwig 		return TCM_INVALID_CDB_FIELD;
3842c66ac9dbSNicholas Bellinger 	}
3843c66ac9dbSNicholas Bellinger 
38444949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
3845de103c93SChristoph Hellwig 	if (!buf)
3846de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
384705d1c7c0SAndy Grover 
3848a85d667eSBart Van Assche 	put_unaligned_be16(add_len, &buf[0]);
3849c66ac9dbSNicholas Bellinger 	buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3850c66ac9dbSNicholas Bellinger 	buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3851c66ac9dbSNicholas Bellinger 	buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3852c66ac9dbSNicholas Bellinger 	buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3853c66ac9dbSNicholas Bellinger 	/*
3854c66ac9dbSNicholas Bellinger 	 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3855c66ac9dbSNicholas Bellinger 	 * set the TMV: Task Mask Valid bit.
3856c66ac9dbSNicholas Bellinger 	 */
3857c66ac9dbSNicholas Bellinger 	buf[3] |= 0x80;
3858c66ac9dbSNicholas Bellinger 	/*
3859c66ac9dbSNicholas Bellinger 	 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3860c66ac9dbSNicholas Bellinger 	 */
3861c66ac9dbSNicholas Bellinger 	buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3862c66ac9dbSNicholas Bellinger 	/*
3863c66ac9dbSNicholas Bellinger 	 * PTPL_A: Persistence across Target Power Loss Active bit
3864c66ac9dbSNicholas Bellinger 	 */
3865c66ac9dbSNicholas Bellinger 	if (pr_tmpl->pr_aptpl_active)
3866c66ac9dbSNicholas Bellinger 		buf[3] |= 0x01;
3867c66ac9dbSNicholas Bellinger 	/*
3868c66ac9dbSNicholas Bellinger 	 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
3869c66ac9dbSNicholas Bellinger 	 */
3870c66ac9dbSNicholas Bellinger 	buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3871c66ac9dbSNicholas Bellinger 	buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
3872c66ac9dbSNicholas Bellinger 	buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
3873c66ac9dbSNicholas Bellinger 	buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
3874c66ac9dbSNicholas Bellinger 	buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
3875c66ac9dbSNicholas Bellinger 	buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3876c66ac9dbSNicholas Bellinger 
38774949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
387805d1c7c0SAndy Grover 
3879c66ac9dbSNicholas Bellinger 	return 0;
3880c66ac9dbSNicholas Bellinger }
3881c66ac9dbSNicholas Bellinger 
3882c66ac9dbSNicholas Bellinger /*
3883c66ac9dbSNicholas Bellinger  * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
3884c66ac9dbSNicholas Bellinger  *
3885c66ac9dbSNicholas Bellinger  * See spc4r17 section 6.13.5 Table 168 and 169
3886c66ac9dbSNicholas Bellinger  */
3887de103c93SChristoph Hellwig static sense_reason_t
3888de103c93SChristoph Hellwig core_scsi3_pri_read_full_status(struct se_cmd *cmd)
3889c66ac9dbSNicholas Bellinger {
38900fd97ccfSChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
3891c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
3892c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg;
3893c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg, *pr_reg_tmp;
38940fd97ccfSChristoph Hellwig 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
389505d1c7c0SAndy Grover 	unsigned char *buf;
38962650d71eSChristoph Hellwig 	u32 add_desc_len = 0, add_len = 0;
3897c66ac9dbSNicholas Bellinger 	u32 off = 8; /* off into first Full Status descriptor */
3898d16ca7c5SNicholas Bellinger 	int format_code = 0, pr_res_type = 0, pr_res_scope = 0;
38992650d71eSChristoph Hellwig 	int exp_desc_len, desc_len;
3900d16ca7c5SNicholas Bellinger 	bool all_reg = false;
3901c66ac9dbSNicholas Bellinger 
3902c66ac9dbSNicholas Bellinger 	if (cmd->data_length < 8) {
39036708bb27SAndy Grover 		pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
3904c66ac9dbSNicholas Bellinger 			" too small\n", cmd->data_length);
3905de103c93SChristoph Hellwig 		return TCM_INVALID_CDB_FIELD;
3906c66ac9dbSNicholas Bellinger 	}
3907c66ac9dbSNicholas Bellinger 
39084949314cSAndy Grover 	buf = transport_kmap_data_sg(cmd);
3909de103c93SChristoph Hellwig 	if (!buf)
3910de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
391105d1c7c0SAndy Grover 
3912a85d667eSBart Van Assche 	put_unaligned_be32(dev->t10_pr.pr_generation, &buf[0]);
3913c66ac9dbSNicholas Bellinger 
3914d16ca7c5SNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
3915d16ca7c5SNicholas Bellinger 	if (dev->dev_pr_res_holder) {
3916d16ca7c5SNicholas Bellinger 		struct t10_pr_registration *pr_holder = dev->dev_pr_res_holder;
3917d16ca7c5SNicholas Bellinger 
3918d16ca7c5SNicholas Bellinger 		if (pr_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
3919d16ca7c5SNicholas Bellinger 		    pr_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG) {
3920d16ca7c5SNicholas Bellinger 			all_reg = true;
3921d16ca7c5SNicholas Bellinger 			pr_res_type = pr_holder->pr_res_type;
3922d16ca7c5SNicholas Bellinger 			pr_res_scope = pr_holder->pr_res_scope;
3923d16ca7c5SNicholas Bellinger 		}
3924d16ca7c5SNicholas Bellinger 	}
3925d16ca7c5SNicholas Bellinger 	spin_unlock(&dev->dev_reservation_lock);
3926d16ca7c5SNicholas Bellinger 
3927c66ac9dbSNicholas Bellinger 	spin_lock(&pr_tmpl->registration_lock);
3928c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3929c66ac9dbSNicholas Bellinger 			&pr_tmpl->registration_list, pr_reg_list) {
3930c66ac9dbSNicholas Bellinger 
3931c66ac9dbSNicholas Bellinger 		se_nacl = pr_reg->pr_reg_nacl;
3932c66ac9dbSNicholas Bellinger 		se_tpg = pr_reg->pr_reg_nacl->se_tpg;
3933c66ac9dbSNicholas Bellinger 		add_desc_len = 0;
3934c66ac9dbSNicholas Bellinger 
393533940d09SJoern Engel 		atomic_inc_mb(&pr_reg->pr_res_holders);
3936c66ac9dbSNicholas Bellinger 		spin_unlock(&pr_tmpl->registration_lock);
3937c66ac9dbSNicholas Bellinger 		/*
3938c66ac9dbSNicholas Bellinger 		 * Determine expected length of $FABRIC_MOD specific
3939c66ac9dbSNicholas Bellinger 		 * TransportID full status descriptor..
3940c66ac9dbSNicholas Bellinger 		 */
39412650d71eSChristoph Hellwig 		exp_desc_len = target_get_pr_transport_id_len(se_nacl, pr_reg,
39422650d71eSChristoph Hellwig 					&format_code);
39432650d71eSChristoph Hellwig 		if (exp_desc_len < 0 ||
39442650d71eSChristoph Hellwig 		    exp_desc_len + add_len > cmd->data_length) {
39456708bb27SAndy Grover 			pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
3946c66ac9dbSNicholas Bellinger 				" out of buffer: %d\n", cmd->data_length);
3947c66ac9dbSNicholas Bellinger 			spin_lock(&pr_tmpl->registration_lock);
394833940d09SJoern Engel 			atomic_dec_mb(&pr_reg->pr_res_holders);
3949c66ac9dbSNicholas Bellinger 			break;
3950c66ac9dbSNicholas Bellinger 		}
3951c66ac9dbSNicholas Bellinger 		/*
3952c66ac9dbSNicholas Bellinger 		 * Set RESERVATION KEY
3953c66ac9dbSNicholas Bellinger 		 */
3954a85d667eSBart Van Assche 		put_unaligned_be64(pr_reg->pr_res_key, &buf[off]);
3955a85d667eSBart Van Assche 		off += 8;
3956c66ac9dbSNicholas Bellinger 		off += 4; /* Skip Over Reserved area */
3957c66ac9dbSNicholas Bellinger 
3958c66ac9dbSNicholas Bellinger 		/*
3959c66ac9dbSNicholas Bellinger 		 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
3960c66ac9dbSNicholas Bellinger 		 */
3961c66ac9dbSNicholas Bellinger 		if (pr_reg->pr_reg_all_tg_pt)
3962c66ac9dbSNicholas Bellinger 			buf[off] = 0x02;
3963c66ac9dbSNicholas Bellinger 		/*
3964c66ac9dbSNicholas Bellinger 		 * The struct se_lun pointer will be present for the
3965c66ac9dbSNicholas Bellinger 		 * reservation holder for PR_HOLDER bit.
3966c66ac9dbSNicholas Bellinger 		 *
3967c66ac9dbSNicholas Bellinger 		 * Also, if this registration is the reservation
3968d16ca7c5SNicholas Bellinger 		 * holder or there is an All Registrants reservation
3969d16ca7c5SNicholas Bellinger 		 * active, fill in SCOPE and TYPE in the next byte.
3970c66ac9dbSNicholas Bellinger 		 */
3971c66ac9dbSNicholas Bellinger 		if (pr_reg->pr_res_holder) {
3972c66ac9dbSNicholas Bellinger 			buf[off++] |= 0x01;
3973c66ac9dbSNicholas Bellinger 			buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
3974c66ac9dbSNicholas Bellinger 				     (pr_reg->pr_res_type & 0x0f);
3975d16ca7c5SNicholas Bellinger 		} else if (all_reg) {
3976d16ca7c5SNicholas Bellinger 			buf[off++] |= 0x01;
3977d16ca7c5SNicholas Bellinger 			buf[off++] = (pr_res_scope & 0xf0) |
3978d16ca7c5SNicholas Bellinger 				     (pr_res_type & 0x0f);
3979d16ca7c5SNicholas Bellinger 		} else {
3980c66ac9dbSNicholas Bellinger 			off += 2;
3981d16ca7c5SNicholas Bellinger 		}
3982c66ac9dbSNicholas Bellinger 
3983c66ac9dbSNicholas Bellinger 		off += 4; /* Skip over reserved area */
3984c66ac9dbSNicholas Bellinger 		/*
3985c66ac9dbSNicholas Bellinger 		 * From spc4r17 6.3.15:
3986c66ac9dbSNicholas Bellinger 		 *
3987c66ac9dbSNicholas Bellinger 		 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
3988c66ac9dbSNicholas Bellinger 		 * IDENTIFIER field contains the relative port identifier (see
3989c66ac9dbSNicholas Bellinger 		 * 3.1.120) of the target port that is part of the I_T nexus
3990c66ac9dbSNicholas Bellinger 		 * described by this full status descriptor. If the ALL_TG_PT
3991c66ac9dbSNicholas Bellinger 		 * bit is set to one, the contents of the RELATIVE TARGET PORT
3992c66ac9dbSNicholas Bellinger 		 * IDENTIFIER field are not defined by this standard.
3993c66ac9dbSNicholas Bellinger 		 */
39946708bb27SAndy Grover 		if (!pr_reg->pr_reg_all_tg_pt) {
399579dc9c9eSNicholas Bellinger 			u16 sep_rtpi = pr_reg->tg_pt_sep_rtpi;
3996c66ac9dbSNicholas Bellinger 
3997a85d667eSBart Van Assche 			put_unaligned_be16(sep_rtpi, &buf[off]);
3998a85d667eSBart Van Assche 			off += 2;
3999c66ac9dbSNicholas Bellinger 		} else
400035d1efe8SMasanari Iida 			off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
4001c66ac9dbSNicholas Bellinger 
40022650d71eSChristoph Hellwig 		buf[off+4] = se_tpg->proto_id;
40032650d71eSChristoph Hellwig 
4004c66ac9dbSNicholas Bellinger 		/*
40052650d71eSChristoph Hellwig 		 * Now, have the $FABRIC_MOD fill in the transport ID.
4006c66ac9dbSNicholas Bellinger 		 */
40072650d71eSChristoph Hellwig 		desc_len = target_get_pr_transport_id(se_nacl, pr_reg,
40082650d71eSChristoph Hellwig 				&format_code, &buf[off+4]);
4009c66ac9dbSNicholas Bellinger 
4010c66ac9dbSNicholas Bellinger 		spin_lock(&pr_tmpl->registration_lock);
401133940d09SJoern Engel 		atomic_dec_mb(&pr_reg->pr_res_holders);
40122650d71eSChristoph Hellwig 
40132650d71eSChristoph Hellwig 		if (desc_len < 0)
40142650d71eSChristoph Hellwig 			break;
4015c66ac9dbSNicholas Bellinger 		/*
4016c66ac9dbSNicholas Bellinger 		 * Set the ADDITIONAL DESCRIPTOR LENGTH
4017c66ac9dbSNicholas Bellinger 		 */
4018a85d667eSBart Van Assche 		put_unaligned_be32(desc_len, &buf[off]);
4019c58a252bStangwenji 		off += 4;
4020c66ac9dbSNicholas Bellinger 		/*
4021c66ac9dbSNicholas Bellinger 		 * Size of full desctipor header minus TransportID
4022c66ac9dbSNicholas Bellinger 		 * containing $FABRIC_MOD specific) initiator device/port
4023c66ac9dbSNicholas Bellinger 		 * WWN information.
4024c66ac9dbSNicholas Bellinger 		 *
4025c66ac9dbSNicholas Bellinger 		 *  See spc4r17 Section 6.13.5 Table 169
4026c66ac9dbSNicholas Bellinger 		 */
4027c66ac9dbSNicholas Bellinger 		add_desc_len = (24 + desc_len);
4028c66ac9dbSNicholas Bellinger 
4029c66ac9dbSNicholas Bellinger 		off += desc_len;
4030c66ac9dbSNicholas Bellinger 		add_len += add_desc_len;
4031c66ac9dbSNicholas Bellinger 	}
4032c66ac9dbSNicholas Bellinger 	spin_unlock(&pr_tmpl->registration_lock);
4033c66ac9dbSNicholas Bellinger 	/*
4034c66ac9dbSNicholas Bellinger 	 * Set ADDITIONAL_LENGTH
4035c66ac9dbSNicholas Bellinger 	 */
4036a85d667eSBart Van Assche 	put_unaligned_be32(add_len, &buf[4]);
4037c66ac9dbSNicholas Bellinger 
40384949314cSAndy Grover 	transport_kunmap_data_sg(cmd);
403905d1c7c0SAndy Grover 
4040c66ac9dbSNicholas Bellinger 	return 0;
4041c66ac9dbSNicholas Bellinger }
4042c66ac9dbSNicholas Bellinger 
4043de103c93SChristoph Hellwig sense_reason_t
4044de103c93SChristoph Hellwig target_scsi3_emulate_pr_in(struct se_cmd *cmd)
4045c66ac9dbSNicholas Bellinger {
4046de103c93SChristoph Hellwig 	sense_reason_t ret;
4047e76a35d6SChristoph Hellwig 
4048617c0e06SChristoph Hellwig 	/*
4049617c0e06SChristoph Hellwig 	 * Following spc2r20 5.5.1 Reservations overview:
4050617c0e06SChristoph Hellwig 	 *
4051617c0e06SChristoph Hellwig 	 * If a logical unit has been reserved by any RESERVE command and is
4052617c0e06SChristoph Hellwig 	 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4053617c0e06SChristoph Hellwig 	 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4054617c0e06SChristoph Hellwig 	 * initiator or service action and shall terminate with a RESERVATION
4055617c0e06SChristoph Hellwig 	 * CONFLICT status.
4056617c0e06SChristoph Hellwig 	 */
40570fd97ccfSChristoph Hellwig 	if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
4058617c0e06SChristoph Hellwig 		pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4059617c0e06SChristoph Hellwig 			" SPC-2 reservation is held, returning"
4060617c0e06SChristoph Hellwig 			" RESERVATION_CONFLICT\n");
4061de103c93SChristoph Hellwig 		return TCM_RESERVATION_CONFLICT;
4062617c0e06SChristoph Hellwig 	}
4063617c0e06SChristoph Hellwig 
4064617c0e06SChristoph Hellwig 	switch (cmd->t_task_cdb[1] & 0x1f) {
4065c66ac9dbSNicholas Bellinger 	case PRI_READ_KEYS:
4066d29a5b6aSChristoph Hellwig 		ret = core_scsi3_pri_read_keys(cmd);
4067d29a5b6aSChristoph Hellwig 		break;
4068c66ac9dbSNicholas Bellinger 	case PRI_READ_RESERVATION:
4069d29a5b6aSChristoph Hellwig 		ret = core_scsi3_pri_read_reservation(cmd);
4070d29a5b6aSChristoph Hellwig 		break;
4071c66ac9dbSNicholas Bellinger 	case PRI_REPORT_CAPABILITIES:
4072d29a5b6aSChristoph Hellwig 		ret = core_scsi3_pri_report_capabilities(cmd);
4073d29a5b6aSChristoph Hellwig 		break;
4074c66ac9dbSNicholas Bellinger 	case PRI_READ_FULL_STATUS:
4075d29a5b6aSChristoph Hellwig 		ret = core_scsi3_pri_read_full_status(cmd);
4076d29a5b6aSChristoph Hellwig 		break;
4077c66ac9dbSNicholas Bellinger 	default:
40786708bb27SAndy Grover 		pr_err("Unknown PERSISTENT_RESERVE_IN service"
4079617c0e06SChristoph Hellwig 			" action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
4080de103c93SChristoph Hellwig 		return TCM_INVALID_CDB_FIELD;
4081c66ac9dbSNicholas Bellinger 	}
4082d29a5b6aSChristoph Hellwig 
40836bb35e00SChristoph Hellwig 	if (!ret)
40846bb35e00SChristoph Hellwig 		target_complete_cmd(cmd, GOOD);
4085d29a5b6aSChristoph Hellwig 	return ret;
4086c66ac9dbSNicholas Bellinger }
4087c66ac9dbSNicholas Bellinger 
4088de103c93SChristoph Hellwig sense_reason_t
4089de103c93SChristoph Hellwig target_check_reservation(struct se_cmd *cmd)
4090c66ac9dbSNicholas Bellinger {
4091d977f437SChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
4092de103c93SChristoph Hellwig 	sense_reason_t ret;
4093d977f437SChristoph Hellwig 
4094d977f437SChristoph Hellwig 	if (!cmd->se_sess)
4095c66ac9dbSNicholas Bellinger 		return 0;
4096d977f437SChristoph Hellwig 	if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
4097c66ac9dbSNicholas Bellinger 		return 0;
4098b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr)
4099b49d6f78SDavid Disseldorp 		return 0;
41004ec5bf0eSBryant G. Ly 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
4101d977f437SChristoph Hellwig 		return 0;
4102c66ac9dbSNicholas Bellinger 
4103d977f437SChristoph Hellwig 	spin_lock(&dev->dev_reservation_lock);
4104d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
4105d977f437SChristoph Hellwig 		ret = target_scsi2_reservation_check(cmd);
4106d977f437SChristoph Hellwig 	else
4107d977f437SChristoph Hellwig 		ret = target_scsi3_pr_reservation_check(cmd);
4108d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
41090fd97ccfSChristoph Hellwig 
4110d977f437SChristoph Hellwig 	return ret;
4111c66ac9dbSNicholas Bellinger }
4112