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