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