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