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