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