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