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