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