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