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