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