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