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