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