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