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