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