blob: 3f8aac36ec3148b95cd5bb1281cdf7496e98d52d [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001/*******************************************************************************
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
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080028#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>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050036#include <target/target_core_backend.h>
37#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080038#include <target/target_core_configfs.h>
39
Christoph Hellwige26d99a2011-11-14 12:30:30 -050040#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080041#include "target_core_pr.h"
42#include "target_core_ua.h"
43
44/*
45 * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
46 */
47struct pr_transport_id_holder {
48 int dest_local_nexus;
49 struct t10_pr_registration *dest_pr_reg;
50 struct se_portal_group *dest_tpg;
51 struct se_node_acl *dest_node_acl;
52 struct se_dev_entry *dest_se_deve;
53 struct list_head dest_list;
54};
55
56int core_pr_dump_initiator_port(
57 struct t10_pr_registration *pr_reg,
58 char *buf,
59 u32 size)
60{
Andy Grover6708bb22011-06-08 10:36:43 -070061 if (!pr_reg->isid_present_at_reg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080062 return 0;
63
64 snprintf(buf, size, ",i,0x%s", &pr_reg->pr_reg_isid[0]);
65 return 1;
66}
67
68static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
69 struct t10_pr_registration *, int);
70
71static int core_scsi2_reservation_seq_non_holder(
72 struct se_cmd *cmd,
73 unsigned char *cdb,
74 u32 pr_reg_type)
75{
76 switch (cdb[0]) {
77 case INQUIRY:
78 case RELEASE:
79 case RELEASE_10:
80 return 0;
81 default:
82 return 1;
83 }
84
85 return 1;
86}
87
88static int core_scsi2_reservation_check(struct se_cmd *cmd, u32 *pr_reg_type)
89{
90 struct se_device *dev = cmd->se_dev;
91 struct se_session *sess = cmd->se_sess;
92 int ret;
93
Andy Grover6708bb22011-06-08 10:36:43 -070094 if (!sess)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080095 return 0;
96
97 spin_lock(&dev->dev_reservation_lock);
98 if (!dev->dev_reserved_node_acl || !sess) {
99 spin_unlock(&dev->dev_reservation_lock);
100 return 0;
101 }
102 if (dev->dev_reserved_node_acl != sess->se_node_acl) {
103 spin_unlock(&dev->dev_reservation_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +0000104 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800105 }
106 if (!(dev->dev_flags & DF_SPC2_RESERVATIONS_WITH_ISID)) {
107 spin_unlock(&dev->dev_reservation_lock);
108 return 0;
109 }
Andy Grovere3d6f902011-07-19 08:55:10 +0000110 ret = (dev->dev_res_bin_isid == sess->sess_bin_isid) ? 0 : -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800111 spin_unlock(&dev->dev_reservation_lock);
112
113 return ret;
114}
115
Christoph Hellwigeacac002011-11-03 17:50:40 -0400116static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
117 struct se_node_acl *, struct se_session *);
118static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
119
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700120static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
Christoph Hellwigeacac002011-11-03 17:50:40 -0400121{
122 struct se_session *se_sess = cmd->se_sess;
123 struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
124 struct t10_pr_registration *pr_reg;
125 struct t10_reservation *pr_tmpl = &su_dev->t10_pr;
126 int crh = (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS);
127 int conflict = 0;
128
129 if (!crh)
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700130 return -EINVAL;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400131
132 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
133 se_sess);
134 if (pr_reg) {
135 /*
136 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
137 * behavior
138 *
139 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
140 * status, but no reservation shall be established and the
141 * persistent reservation shall not be changed, if the command
142 * is received from a) and b) below.
143 *
144 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
145 * status, but the persistent reservation shall not be released,
146 * if the command is received from a) and b)
147 *
148 * a) An I_T nexus that is a persistent reservation holder; or
149 * b) An I_T nexus that is registered if a registrants only or
150 * all registrants type persistent reservation is present.
151 *
152 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
153 * RELEASE(6) command, or RELEASE(10) command shall be processed
154 * as defined in SPC-2.
155 */
156 if (pr_reg->pr_res_holder) {
157 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700158 return 1;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400159 }
160 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
161 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
162 (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
163 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
164 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700165 return 1;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400166 }
167 core_scsi3_put_pr_reg(pr_reg);
168 conflict = 1;
169 } else {
170 /*
171 * Following spc2r20 5.5.1 Reservations overview:
172 *
173 * If a logical unit has executed a PERSISTENT RESERVE OUT
174 * command with the REGISTER or the REGISTER AND IGNORE
175 * EXISTING KEY service action and is still registered by any
176 * initiator, all RESERVE commands and all RELEASE commands
177 * regardless of initiator shall conflict and shall terminate
178 * with a RESERVATION CONFLICT status.
179 */
180 spin_lock(&pr_tmpl->registration_lock);
181 conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
182 spin_unlock(&pr_tmpl->registration_lock);
183 }
184
185 if (conflict) {
186 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
187 " while active SPC-3 registrations exist,"
188 " returning RESERVATION_CONFLICT\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700189 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700190 return -EBUSY;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400191 }
192
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700193 return 0;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400194}
195
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400196int target_scsi2_reservation_release(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800197{
198 struct se_device *dev = cmd->se_dev;
199 struct se_session *sess = cmd->se_sess;
200 struct se_portal_group *tpg = sess->se_tpg;
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700201 int ret = 0, rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800202
Andy Grover6708bb22011-06-08 10:36:43 -0700203 if (!sess || !tpg)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400204 goto out;
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700205 rc = target_check_scsi2_reservation_conflict(cmd);
206 if (rc == 1)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400207 goto out;
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700208 else if (rc < 0) {
209 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
210 ret = -EINVAL;
211 goto out;
212 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800213
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400214 ret = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800215 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400216 if (!dev->dev_reserved_node_acl || !sess)
217 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800218
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400219 if (dev->dev_reserved_node_acl != sess->se_node_acl)
220 goto out_unlock;
221
Bernhard Kohledc318d2012-05-13 23:39:37 +0200222 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
223 goto out_unlock;
224
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800225 dev->dev_reserved_node_acl = NULL;
226 dev->dev_flags &= ~DF_SPC2_RESERVATIONS;
227 if (dev->dev_flags & DF_SPC2_RESERVATIONS_WITH_ISID) {
228 dev->dev_res_bin_isid = 0;
229 dev->dev_flags &= ~DF_SPC2_RESERVATIONS_WITH_ISID;
230 }
Andy Grover6708bb22011-06-08 10:36:43 -0700231 pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
Andy Grovere3d6f902011-07-19 08:55:10 +0000232 " MAPPED LUN: %u for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
233 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800234 sess->se_node_acl->initiatorname);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800235
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400236out_unlock:
237 spin_unlock(&dev->dev_reservation_lock);
238out:
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400239 if (!ret)
240 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400241 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800242}
243
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400244int target_scsi2_reservation_reserve(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800245{
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;
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700249 int ret = 0, rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800250
Andy Grovera1d8b492011-05-02 17:12:10 -0700251 if ((cmd->t_task_cdb[1] & 0x01) &&
252 (cmd->t_task_cdb[1] & 0x02)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700253 pr_err("LongIO and Obselete Bits set, returning"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800254 " ILLEGAL_REQUEST\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700255 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
256 ret = -EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400257 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800258 }
259 /*
260 * This is currently the case for target_core_mod passthrough struct se_cmd
261 * ops
262 */
Andy Grover6708bb22011-06-08 10:36:43 -0700263 if (!sess || !tpg)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400264 goto out;
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700265 rc = target_check_scsi2_reservation_conflict(cmd);
266 if (rc == 1)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400267 goto out;
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700268 else if (rc < 0) {
269 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
270 ret = -EINVAL;
271 goto out;
272 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800273
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400274 ret = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800275 spin_lock(&dev->dev_reservation_lock);
276 if (dev->dev_reserved_node_acl &&
277 (dev->dev_reserved_node_acl != sess->se_node_acl)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700278 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000279 tpg->se_tpg_tfo->get_fabric_name());
Andy Grover6708bb22011-06-08 10:36:43 -0700280 pr_err("Original reserver LUN: %u %s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000281 cmd->se_lun->unpacked_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800282 dev->dev_reserved_node_acl->initiatorname);
Andy Grover6708bb22011-06-08 10:36:43 -0700283 pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
Andy Grovere3d6f902011-07-19 08:55:10 +0000284 " from %s \n", cmd->se_lun->unpacked_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800285 cmd->se_deve->mapped_lun,
286 sess->se_node_acl->initiatorname);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700287 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
288 ret = -EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400289 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800290 }
291
292 dev->dev_reserved_node_acl = sess->se_node_acl;
293 dev->dev_flags |= DF_SPC2_RESERVATIONS;
294 if (sess->sess_bin_isid != 0) {
295 dev->dev_res_bin_isid = sess->sess_bin_isid;
296 dev->dev_flags |= DF_SPC2_RESERVATIONS_WITH_ISID;
297 }
Andy Grover6708bb22011-06-08 10:36:43 -0700298 pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
Andy Grovere3d6f902011-07-19 08:55:10 +0000299 " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
300 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800301 sess->se_node_acl->initiatorname);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800302
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400303out_unlock:
304 spin_unlock(&dev->dev_reservation_lock);
305out:
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400306 if (!ret)
307 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400308 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800309}
310
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800311
312/*
313 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
314 *
315 * This function is called by those initiator ports who are *NOT*
316 * the active PR reservation holder when a reservation is present.
317 */
318static int core_scsi3_pr_seq_non_holder(
319 struct se_cmd *cmd,
320 unsigned char *cdb,
321 u32 pr_reg_type)
322{
323 struct se_dev_entry *se_deve;
Andy Grovere3d6f902011-07-19 08:55:10 +0000324 struct se_session *se_sess = cmd->se_sess;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800325 int other_cdb = 0, ignore_reg;
326 int registered_nexus = 0, ret = 1; /* Conflict by default */
327 int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
328 int we = 0; /* Write Exclusive */
329 int legacy = 0; /* Act like a legacy device and return
330 * RESERVATION CONFLICT on some CDBs */
331 /*
332 * A legacy SPC-2 reservation is being held.
333 */
334 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS)
335 return core_scsi2_reservation_seq_non_holder(cmd,
336 cdb, pr_reg_type);
337
Jörn Engelf2083242012-03-15 15:05:40 -0400338 se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800339 /*
340 * Determine if the registration should be ignored due to
341 * non-matching ISIDs in core_scsi3_pr_reservation_check().
342 */
343 ignore_reg = (pr_reg_type & 0x80000000);
344 if (ignore_reg)
345 pr_reg_type &= ~0x80000000;
346
347 switch (pr_reg_type) {
348 case PR_TYPE_WRITE_EXCLUSIVE:
349 we = 1;
350 case PR_TYPE_EXCLUSIVE_ACCESS:
351 /*
352 * Some commands are only allowed for the persistent reservation
353 * holder.
354 */
355 if ((se_deve->def_pr_registered) && !(ignore_reg))
356 registered_nexus = 1;
357 break;
358 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
359 we = 1;
360 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
361 /*
362 * Some commands are only allowed for registered I_T Nexuses.
363 */
364 reg_only = 1;
365 if ((se_deve->def_pr_registered) && !(ignore_reg))
366 registered_nexus = 1;
367 break;
368 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
369 we = 1;
370 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
371 /*
372 * Each registered I_T Nexus is a reservation holder.
373 */
374 all_reg = 1;
375 if ((se_deve->def_pr_registered) && !(ignore_reg))
376 registered_nexus = 1;
377 break;
378 default:
Andy Grovere3d6f902011-07-19 08:55:10 +0000379 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800380 }
381 /*
382 * Referenced from spc4r17 table 45 for *NON* PR holder access
383 */
384 switch (cdb[0]) {
385 case SECURITY_PROTOCOL_IN:
386 if (registered_nexus)
387 return 0;
388 ret = (we) ? 0 : 1;
389 break;
390 case MODE_SENSE:
391 case MODE_SENSE_10:
392 case READ_ATTRIBUTE:
393 case READ_BUFFER:
394 case RECEIVE_DIAGNOSTIC:
395 if (legacy) {
396 ret = 1;
397 break;
398 }
399 if (registered_nexus) {
400 ret = 0;
401 break;
402 }
403 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
404 break;
405 case PERSISTENT_RESERVE_OUT:
406 /*
407 * This follows PERSISTENT_RESERVE_OUT service actions that
408 * are allowed in the presence of various reservations.
409 * See spc4r17, table 46
410 */
411 switch (cdb[1] & 0x1f) {
412 case PRO_CLEAR:
413 case PRO_PREEMPT:
414 case PRO_PREEMPT_AND_ABORT:
415 ret = (registered_nexus) ? 0 : 1;
416 break;
417 case PRO_REGISTER:
418 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
419 ret = 0;
420 break;
421 case PRO_REGISTER_AND_MOVE:
422 case PRO_RESERVE:
423 ret = 1;
424 break;
425 case PRO_RELEASE:
426 ret = (registered_nexus) ? 0 : 1;
427 break;
428 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700429 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800430 " action: 0x%02x\n", cdb[1] & 0x1f);
Andy Grovere3d6f902011-07-19 08:55:10 +0000431 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800432 }
433 break;
434 case RELEASE:
435 case RELEASE_10:
Christoph Hellwigeacac002011-11-03 17:50:40 -0400436 /* Handled by CRH=1 in target_scsi2_reservation_release() */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800437 ret = 0;
438 break;
439 case RESERVE:
440 case RESERVE_10:
Christoph Hellwigeacac002011-11-03 17:50:40 -0400441 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800442 ret = 0;
443 break;
444 case TEST_UNIT_READY:
445 ret = (legacy) ? 1 : 0; /* Conflict for legacy */
446 break;
447 case MAINTENANCE_IN:
448 switch (cdb[1] & 0x1f) {
449 case MI_MANAGEMENT_PROTOCOL_IN:
450 if (registered_nexus) {
451 ret = 0;
452 break;
453 }
454 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
455 break;
456 case MI_REPORT_SUPPORTED_OPERATION_CODES:
457 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
458 if (legacy) {
459 ret = 1;
460 break;
461 }
462 if (registered_nexus) {
463 ret = 0;
464 break;
465 }
466 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
467 break;
468 case MI_REPORT_ALIASES:
469 case MI_REPORT_IDENTIFYING_INFORMATION:
470 case MI_REPORT_PRIORITY:
471 case MI_REPORT_TARGET_PGS:
472 case MI_REPORT_TIMESTAMP:
473 ret = 0; /* Allowed */
474 break;
475 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700476 pr_err("Unknown MI Service Action: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800477 (cdb[1] & 0x1f));
Andy Grovere3d6f902011-07-19 08:55:10 +0000478 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800479 }
480 break;
481 case ACCESS_CONTROL_IN:
482 case ACCESS_CONTROL_OUT:
483 case INQUIRY:
484 case LOG_SENSE:
485 case READ_MEDIA_SERIAL_NUMBER:
486 case REPORT_LUNS:
487 case REQUEST_SENSE:
Marco Sanvido68169662012-01-03 17:12:58 -0800488 case PERSISTENT_RESERVE_IN:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800489 ret = 0; /*/ Allowed CDBs */
490 break;
491 default:
492 other_cdb = 1;
493 break;
494 }
495 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300496 * Case where the CDB is explicitly allowed in the above switch
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800497 * statement.
498 */
Andy Grover6708bb22011-06-08 10:36:43 -0700499 if (!ret && !other_cdb) {
Andy Grover6708bb22011-06-08 10:36:43 -0700500 pr_debug("Allowing explict CDB: 0x%02x for %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800501 " reservation holder\n", cdb[0],
502 core_scsi3_pr_dump_type(pr_reg_type));
Andy Grover8b1e1242012-04-03 15:51:12 -0700503
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800504 return ret;
505 }
506 /*
507 * Check if write exclusive initiator ports *NOT* holding the
508 * WRITE_EXCLUSIVE_* reservation.
509 */
Andy Groveree1b1b92012-07-12 17:34:54 -0700510 if (we && !registered_nexus) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800511 if (cmd->data_direction == DMA_TO_DEVICE) {
512 /*
513 * Conflict for write exclusive
514 */
Andy Grover6708bb22011-06-08 10:36:43 -0700515 pr_debug("%s Conflict for unregistered nexus"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800516 " %s CDB: 0x%02x to %s reservation\n",
517 transport_dump_cmd_direction(cmd),
518 se_sess->se_node_acl->initiatorname, cdb[0],
519 core_scsi3_pr_dump_type(pr_reg_type));
520 return 1;
521 } else {
522 /*
523 * Allow non WRITE CDBs for all Write Exclusive
524 * PR TYPEs to pass for registered and
525 * non-registered_nexuxes NOT holding the reservation.
526 *
527 * We only make noise for the unregisterd nexuses,
528 * as we expect registered non-reservation holding
529 * nexuses to issue CDBs.
530 */
Andy Grover8b1e1242012-04-03 15:51:12 -0700531
Andy Grover6708bb22011-06-08 10:36:43 -0700532 if (!registered_nexus) {
533 pr_debug("Allowing implict CDB: 0x%02x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800534 " for %s reservation on unregistered"
535 " nexus\n", cdb[0],
536 core_scsi3_pr_dump_type(pr_reg_type));
537 }
Andy Grover8b1e1242012-04-03 15:51:12 -0700538
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800539 return 0;
540 }
541 } else if ((reg_only) || (all_reg)) {
542 if (registered_nexus) {
543 /*
544 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
545 * allow commands from registered nexuses.
546 */
Andy Grover8b1e1242012-04-03 15:51:12 -0700547
Andy Grover6708bb22011-06-08 10:36:43 -0700548 pr_debug("Allowing implict CDB: 0x%02x for %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800549 " reservation\n", cdb[0],
550 core_scsi3_pr_dump_type(pr_reg_type));
Andy Grover8b1e1242012-04-03 15:51:12 -0700551
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800552 return 0;
553 }
554 }
Andy Grover6708bb22011-06-08 10:36:43 -0700555 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800556 " for %s reservation\n", transport_dump_cmd_direction(cmd),
557 (registered_nexus) ? "" : "un",
558 se_sess->se_node_acl->initiatorname, cdb[0],
559 core_scsi3_pr_dump_type(pr_reg_type));
560
561 return 1; /* Conflict by default */
562}
563
564static u32 core_scsi3_pr_generation(struct se_device *dev)
565{
Andy Grovere3d6f902011-07-19 08:55:10 +0000566 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800567 u32 prg;
568 /*
569 * PRGeneration field shall contain the value of a 32-bit wrapping
570 * counter mainted by the device server.
571 *
572 * Note that this is done regardless of Active Persist across
573 * Target PowerLoss (APTPL)
574 *
575 * See spc4r17 section 6.3.12 READ_KEYS service action
576 */
577 spin_lock(&dev->dev_reservation_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +0000578 prg = su_dev->t10_pr.pr_generation++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800579 spin_unlock(&dev->dev_reservation_lock);
580
581 return prg;
582}
583
584static int core_scsi3_pr_reservation_check(
585 struct se_cmd *cmd,
586 u32 *pr_reg_type)
587{
588 struct se_device *dev = cmd->se_dev;
589 struct se_session *sess = cmd->se_sess;
590 int ret;
591
Andy Grover6708bb22011-06-08 10:36:43 -0700592 if (!sess)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800593 return 0;
594 /*
595 * A legacy SPC-2 reservation is being held.
596 */
597 if (dev->dev_flags & DF_SPC2_RESERVATIONS)
598 return core_scsi2_reservation_check(cmd, pr_reg_type);
599
600 spin_lock(&dev->dev_reservation_lock);
Andy Grover6708bb22011-06-08 10:36:43 -0700601 if (!dev->dev_pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800602 spin_unlock(&dev->dev_reservation_lock);
603 return 0;
604 }
605 *pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
606 cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
607 if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl) {
608 spin_unlock(&dev->dev_reservation_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +0000609 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800610 }
Andy Grover6708bb22011-06-08 10:36:43 -0700611 if (!dev->dev_pr_res_holder->isid_present_at_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800612 spin_unlock(&dev->dev_reservation_lock);
613 return 0;
614 }
615 ret = (dev->dev_pr_res_holder->pr_reg_bin_isid ==
Andy Grovere3d6f902011-07-19 08:55:10 +0000616 sess->sess_bin_isid) ? 0 : -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800617 /*
618 * Use bit in *pr_reg_type to notify ISID mismatch in
619 * core_scsi3_pr_seq_non_holder().
620 */
621 if (ret != 0)
622 *pr_reg_type |= 0x80000000;
623 spin_unlock(&dev->dev_reservation_lock);
624
625 return ret;
626}
627
628static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
629 struct se_device *dev,
630 struct se_node_acl *nacl,
631 struct se_dev_entry *deve,
632 unsigned char *isid,
633 u64 sa_res_key,
634 int all_tg_pt,
635 int aptpl)
636{
Andy Grovere3d6f902011-07-19 08:55:10 +0000637 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800638 struct t10_pr_registration *pr_reg;
639
640 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
Andy Grover6708bb22011-06-08 10:36:43 -0700641 if (!pr_reg) {
642 pr_err("Unable to allocate struct t10_pr_registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800643 return NULL;
644 }
645
Andy Grovere3d6f902011-07-19 08:55:10 +0000646 pr_reg->pr_aptpl_buf = kzalloc(su_dev->t10_pr.pr_aptpl_buf_len,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800647 GFP_ATOMIC);
Andy Grover6708bb22011-06-08 10:36:43 -0700648 if (!pr_reg->pr_aptpl_buf) {
649 pr_err("Unable to allocate pr_reg->pr_aptpl_buf\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800650 kmem_cache_free(t10_pr_reg_cache, pr_reg);
651 return NULL;
652 }
653
654 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
655 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
656 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
657 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
658 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
659 atomic_set(&pr_reg->pr_res_holders, 0);
660 pr_reg->pr_reg_nacl = nacl;
661 pr_reg->pr_reg_deve = deve;
662 pr_reg->pr_res_mapped_lun = deve->mapped_lun;
663 pr_reg->pr_aptpl_target_lun = deve->se_lun->unpacked_lun;
664 pr_reg->pr_res_key = sa_res_key;
665 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
666 pr_reg->pr_reg_aptpl = aptpl;
667 pr_reg->pr_reg_tg_pt_lun = deve->se_lun;
668 /*
669 * If an ISID value for this SCSI Initiator Port exists,
670 * save it to the registration now.
671 */
672 if (isid != NULL) {
673 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
674 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
675 pr_reg->isid_present_at_reg = 1;
676 }
677
678 return pr_reg;
679}
680
681static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
682static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
683
684/*
685 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
686 * modes.
687 */
688static struct t10_pr_registration *__core_scsi3_alloc_registration(
689 struct se_device *dev,
690 struct se_node_acl *nacl,
691 struct se_dev_entry *deve,
692 unsigned char *isid,
693 u64 sa_res_key,
694 int all_tg_pt,
695 int aptpl)
696{
697 struct se_dev_entry *deve_tmp;
698 struct se_node_acl *nacl_tmp;
699 struct se_port *port, *port_tmp;
700 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
701 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
702 int ret;
703 /*
704 * Create a registration for the I_T Nexus upon which the
705 * PROUT REGISTER was received.
706 */
707 pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, deve, isid,
708 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -0700709 if (!pr_reg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800710 return NULL;
711 /*
712 * Return pointer to pr_reg for ALL_TG_PT=0
713 */
Andy Grover6708bb22011-06-08 10:36:43 -0700714 if (!all_tg_pt)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800715 return pr_reg;
716 /*
717 * Create list of matching SCSI Initiator Port registrations
718 * for ALL_TG_PT=1
719 */
720 spin_lock(&dev->se_port_lock);
721 list_for_each_entry_safe(port, port_tmp, &dev->dev_sep_list, sep_list) {
722 atomic_inc(&port->sep_tg_pt_ref_cnt);
723 smp_mb__after_atomic_inc();
724 spin_unlock(&dev->se_port_lock);
725
726 spin_lock_bh(&port->sep_alua_lock);
727 list_for_each_entry(deve_tmp, &port->sep_alua_list,
728 alua_port_list) {
729 /*
730 * This pointer will be NULL for demo mode MappedLUNs
731 * that have not been make explict via a ConfigFS
732 * MappedLUN group for the SCSI Initiator Node ACL.
733 */
Andy Grover6708bb22011-06-08 10:36:43 -0700734 if (!deve_tmp->se_lun_acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800735 continue;
736
737 nacl_tmp = deve_tmp->se_lun_acl->se_lun_nacl;
738 /*
739 * Skip the matching struct se_node_acl that is allocated
740 * above..
741 */
742 if (nacl == nacl_tmp)
743 continue;
744 /*
745 * Only perform PR registrations for target ports on
746 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
747 * arrived.
748 */
749 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
750 continue;
751 /*
752 * Look for a matching Initiator Node ACL in ASCII format
753 */
754 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
755 continue;
756
757 atomic_inc(&deve_tmp->pr_ref_count);
758 smp_mb__after_atomic_inc();
759 spin_unlock_bh(&port->sep_alua_lock);
760 /*
761 * Grab a configfs group dependency that is released
762 * for the exception path at label out: below, or upon
763 * completion of adding ALL_TG_PT=1 registrations in
764 * __core_scsi3_add_registration()
765 */
766 ret = core_scsi3_lunacl_depend_item(deve_tmp);
767 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -0700768 pr_err("core_scsi3_lunacl_depend"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800769 "_item() failed\n");
770 atomic_dec(&port->sep_tg_pt_ref_cnt);
771 smp_mb__after_atomic_dec();
772 atomic_dec(&deve_tmp->pr_ref_count);
773 smp_mb__after_atomic_dec();
774 goto out;
775 }
776 /*
777 * Located a matching SCSI Initiator Port on a different
778 * port, allocate the pr_reg_atp and attach it to the
779 * pr_reg->pr_reg_atp_list that will be processed once
780 * the original *pr_reg is processed in
781 * __core_scsi3_add_registration()
782 */
783 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
784 nacl_tmp, deve_tmp, NULL,
785 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -0700786 if (!pr_reg_atp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800787 atomic_dec(&port->sep_tg_pt_ref_cnt);
788 smp_mb__after_atomic_dec();
789 atomic_dec(&deve_tmp->pr_ref_count);
790 smp_mb__after_atomic_dec();
791 core_scsi3_lunacl_undepend_item(deve_tmp);
792 goto out;
793 }
794
795 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
796 &pr_reg->pr_reg_atp_list);
797 spin_lock_bh(&port->sep_alua_lock);
798 }
799 spin_unlock_bh(&port->sep_alua_lock);
800
801 spin_lock(&dev->se_port_lock);
802 atomic_dec(&port->sep_tg_pt_ref_cnt);
803 smp_mb__after_atomic_dec();
804 }
805 spin_unlock(&dev->se_port_lock);
806
807 return pr_reg;
808out:
809 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
810 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
811 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
812 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
813 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
814 }
815 kmem_cache_free(t10_pr_reg_cache, pr_reg);
816 return NULL;
817}
818
819int core_scsi3_alloc_aptpl_registration(
Andy Grovere3d6f902011-07-19 08:55:10 +0000820 struct t10_reservation *pr_tmpl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800821 u64 sa_res_key,
822 unsigned char *i_port,
823 unsigned char *isid,
824 u32 mapped_lun,
825 unsigned char *t_port,
826 u16 tpgt,
827 u32 target_lun,
828 int res_holder,
829 int all_tg_pt,
830 u8 type)
831{
832 struct t10_pr_registration *pr_reg;
833
Andy Grover6708bb22011-06-08 10:36:43 -0700834 if (!i_port || !t_port || !sa_res_key) {
835 pr_err("Illegal parameters for APTPL registration\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000836 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800837 }
838
839 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700840 if (!pr_reg) {
841 pr_err("Unable to allocate struct t10_pr_registration\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000842 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800843 }
844 pr_reg->pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len, GFP_KERNEL);
845
846 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
847 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
848 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
849 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
850 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
851 atomic_set(&pr_reg->pr_res_holders, 0);
852 pr_reg->pr_reg_nacl = NULL;
853 pr_reg->pr_reg_deve = NULL;
854 pr_reg->pr_res_mapped_lun = mapped_lun;
855 pr_reg->pr_aptpl_target_lun = target_lun;
856 pr_reg->pr_res_key = sa_res_key;
857 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
858 pr_reg->pr_reg_aptpl = 1;
859 pr_reg->pr_reg_tg_pt_lun = NULL;
860 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
861 pr_reg->pr_res_type = type;
862 /*
863 * If an ISID value had been saved in APTPL metadata for this
864 * SCSI Initiator Port, restore it now.
865 */
866 if (isid != NULL) {
867 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
868 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
869 pr_reg->isid_present_at_reg = 1;
870 }
871 /*
872 * Copy the i_port and t_port information from caller.
873 */
874 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
875 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
876 pr_reg->pr_reg_tpgt = tpgt;
877 /*
878 * Set pr_res_holder from caller, the pr_reg who is the reservation
879 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
880 * the Initiator Node LUN ACL from the fabric module is created for
881 * this registration.
882 */
883 pr_reg->pr_res_holder = res_holder;
884
885 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
Andy Grover6708bb22011-06-08 10:36:43 -0700886 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800887 " metadata\n", (res_holder) ? "+reservation" : "");
888 return 0;
889}
890
891static void core_scsi3_aptpl_reserve(
892 struct se_device *dev,
893 struct se_portal_group *tpg,
894 struct se_node_acl *node_acl,
895 struct t10_pr_registration *pr_reg)
896{
897 char i_buf[PR_REG_ISID_ID_LEN];
898 int prf_isid;
899
900 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
901 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
902 PR_REG_ISID_ID_LEN);
903
904 spin_lock(&dev->dev_reservation_lock);
905 dev->dev_pr_res_holder = pr_reg;
906 spin_unlock(&dev->dev_reservation_lock);
907
Andy Grover6708bb22011-06-08 10:36:43 -0700908 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800909 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000910 tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800911 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
912 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -0700913 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000914 tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800915 (prf_isid) ? &i_buf[0] : "");
916}
917
918static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
919 struct t10_pr_registration *, int, int);
920
921static int __core_scsi3_check_aptpl_registration(
922 struct se_device *dev,
923 struct se_portal_group *tpg,
924 struct se_lun *lun,
925 u32 target_lun,
926 struct se_node_acl *nacl,
927 struct se_dev_entry *deve)
928{
929 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
Andy Grovere3d6f902011-07-19 08:55:10 +0000930 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800931 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
932 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
933 u16 tpgt;
934
935 memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
936 memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
937 /*
938 * Copy Initiator Port information from struct se_node_acl
939 */
940 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
941 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
Andy Grovere3d6f902011-07-19 08:55:10 +0000942 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
943 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800944 /*
945 * Look for the matching registrations+reservation from those
946 * created from APTPL metadata. Note that multiple registrations
947 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
948 * TransportIDs.
949 */
950 spin_lock(&pr_tmpl->aptpl_reg_lock);
951 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
952 pr_reg_aptpl_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700953 if (!strcmp(pr_reg->pr_iport, i_port) &&
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800954 (pr_reg->pr_res_mapped_lun == deve->mapped_lun) &&
955 !(strcmp(pr_reg->pr_tport, t_port)) &&
956 (pr_reg->pr_reg_tpgt == tpgt) &&
957 (pr_reg->pr_aptpl_target_lun == target_lun)) {
958
959 pr_reg->pr_reg_nacl = nacl;
960 pr_reg->pr_reg_deve = deve;
961 pr_reg->pr_reg_tg_pt_lun = lun;
962
963 list_del(&pr_reg->pr_reg_aptpl_list);
964 spin_unlock(&pr_tmpl->aptpl_reg_lock);
965 /*
966 * At this point all of the pointers in *pr_reg will
967 * be setup, so go ahead and add the registration.
968 */
969
970 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
971 /*
972 * If this registration is the reservation holder,
973 * make that happen now..
974 */
975 if (pr_reg->pr_res_holder)
976 core_scsi3_aptpl_reserve(dev, tpg,
977 nacl, pr_reg);
978 /*
979 * Reenable pr_aptpl_active to accept new metadata
980 * updates once the SCSI device is active again..
981 */
982 spin_lock(&pr_tmpl->aptpl_reg_lock);
983 pr_tmpl->pr_aptpl_active = 1;
984 }
985 }
986 spin_unlock(&pr_tmpl->aptpl_reg_lock);
987
988 return 0;
989}
990
991int core_scsi3_check_aptpl_registration(
992 struct se_device *dev,
993 struct se_portal_group *tpg,
994 struct se_lun *lun,
995 struct se_lun_acl *lun_acl)
996{
Andy Grovere3d6f902011-07-19 08:55:10 +0000997 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800998 struct se_node_acl *nacl = lun_acl->se_lun_nacl;
Jörn Engelf2083242012-03-15 15:05:40 -0400999 struct se_dev_entry *deve = nacl->device_list[lun_acl->mapped_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001000
Andy Grovere3d6f902011-07-19 08:55:10 +00001001 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001002 return 0;
1003
1004 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
1005 lun->unpacked_lun, nacl, deve);
1006}
1007
1008static void __core_scsi3_dump_registration(
1009 struct target_core_fabric_ops *tfo,
1010 struct se_device *dev,
1011 struct se_node_acl *nacl,
1012 struct t10_pr_registration *pr_reg,
1013 int register_type)
1014{
1015 struct se_portal_group *se_tpg = nacl->se_tpg;
1016 char i_buf[PR_REG_ISID_ID_LEN];
1017 int prf_isid;
1018
1019 memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
1020 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1021 PR_REG_ISID_ID_LEN);
1022
Andy Grover6708bb22011-06-08 10:36:43 -07001023 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001024 " Node: %s%s\n", tfo->get_fabric_name(), (register_type == 2) ?
1025 "_AND_MOVE" : (register_type == 1) ?
1026 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
1027 (prf_isid) ? i_buf : "");
Andy Grover6708bb22011-06-08 10:36:43 -07001028 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001029 tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg),
1030 tfo->tpg_get_tag(se_tpg));
Andy Grover6708bb22011-06-08 10:36:43 -07001031 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001032 " Port(s)\n", tfo->get_fabric_name(),
1033 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
Andy Grovere3d6f902011-07-19 08:55:10 +00001034 dev->transport->name);
Andy Grover6708bb22011-06-08 10:36:43 -07001035 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001036 " 0x%08x APTPL: %d\n", tfo->get_fabric_name(),
1037 pr_reg->pr_res_key, pr_reg->pr_res_generation,
1038 pr_reg->pr_reg_aptpl);
1039}
1040
1041/*
1042 * this function can be called with struct se_device->dev_reservation_lock
1043 * when register_move = 1
1044 */
1045static void __core_scsi3_add_registration(
1046 struct se_device *dev,
1047 struct se_node_acl *nacl,
1048 struct t10_pr_registration *pr_reg,
1049 int register_type,
1050 int register_move)
1051{
Andy Grovere3d6f902011-07-19 08:55:10 +00001052 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001053 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1054 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
Andy Grovere3d6f902011-07-19 08:55:10 +00001055 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001056
1057 /*
1058 * Increment PRgeneration counter for struct se_device upon a successful
1059 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1060 *
1061 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1062 * action, the struct se_device->dev_reservation_lock will already be held,
1063 * so we do not call core_scsi3_pr_generation() which grabs the lock
1064 * for the REGISTER.
1065 */
1066 pr_reg->pr_res_generation = (register_move) ?
Andy Grovere3d6f902011-07-19 08:55:10 +00001067 su_dev->t10_pr.pr_generation++ :
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001068 core_scsi3_pr_generation(dev);
1069
1070 spin_lock(&pr_tmpl->registration_lock);
1071 list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
1072 pr_reg->pr_reg_deve->def_pr_registered = 1;
1073
1074 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1075 spin_unlock(&pr_tmpl->registration_lock);
1076 /*
1077 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1078 */
Andy Grover6708bb22011-06-08 10:36:43 -07001079 if (!pr_reg->pr_reg_all_tg_pt || register_move)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001080 return;
1081 /*
1082 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1083 * allocated in __core_scsi3_alloc_registration()
1084 */
1085 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1086 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
1087 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1088
1089 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1090
1091 spin_lock(&pr_tmpl->registration_lock);
1092 list_add_tail(&pr_reg_tmp->pr_reg_list,
1093 &pr_tmpl->registration_list);
1094 pr_reg_tmp->pr_reg_deve->def_pr_registered = 1;
1095
1096 __core_scsi3_dump_registration(tfo, dev,
1097 pr_reg_tmp->pr_reg_nacl, pr_reg_tmp,
1098 register_type);
1099 spin_unlock(&pr_tmpl->registration_lock);
1100 /*
1101 * Drop configfs group dependency reference from
1102 * __core_scsi3_alloc_registration()
1103 */
1104 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1105 }
1106}
1107
1108static int core_scsi3_alloc_registration(
1109 struct se_device *dev,
1110 struct se_node_acl *nacl,
1111 struct se_dev_entry *deve,
1112 unsigned char *isid,
1113 u64 sa_res_key,
1114 int all_tg_pt,
1115 int aptpl,
1116 int register_type,
1117 int register_move)
1118{
1119 struct t10_pr_registration *pr_reg;
1120
1121 pr_reg = __core_scsi3_alloc_registration(dev, nacl, deve, isid,
1122 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001123 if (!pr_reg)
Andy Grovere3d6f902011-07-19 08:55:10 +00001124 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001125
1126 __core_scsi3_add_registration(dev, nacl, pr_reg,
1127 register_type, register_move);
1128 return 0;
1129}
1130
1131static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1132 struct se_device *dev,
1133 struct se_node_acl *nacl,
1134 unsigned char *isid)
1135{
Andy Grovere3d6f902011-07-19 08:55:10 +00001136 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001137 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1138 struct se_portal_group *tpg;
1139
1140 spin_lock(&pr_tmpl->registration_lock);
1141 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1142 &pr_tmpl->registration_list, pr_reg_list) {
1143 /*
1144 * First look for a matching struct se_node_acl
1145 */
1146 if (pr_reg->pr_reg_nacl != nacl)
1147 continue;
1148
1149 tpg = pr_reg->pr_reg_nacl->se_tpg;
1150 /*
1151 * If this registration does NOT contain a fabric provided
1152 * ISID, then we have found a match.
1153 */
Andy Grover6708bb22011-06-08 10:36:43 -07001154 if (!pr_reg->isid_present_at_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001155 /*
1156 * Determine if this SCSI device server requires that
1157 * SCSI Intiatior TransportID w/ ISIDs is enforced
1158 * for fabric modules (iSCSI) requiring them.
1159 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001160 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1161 if (dev->se_sub_dev->se_dev_attrib.enforce_pr_isids)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001162 continue;
1163 }
1164 atomic_inc(&pr_reg->pr_res_holders);
1165 smp_mb__after_atomic_inc();
1166 spin_unlock(&pr_tmpl->registration_lock);
1167 return pr_reg;
1168 }
1169 /*
1170 * If the *pr_reg contains a fabric defined ISID for multi-value
1171 * SCSI Initiator Port TransportIDs, then we expect a valid
1172 * matching ISID to be provided by the local SCSI Initiator Port.
1173 */
Andy Grover6708bb22011-06-08 10:36:43 -07001174 if (!isid)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001175 continue;
1176 if (strcmp(isid, pr_reg->pr_reg_isid))
1177 continue;
1178
1179 atomic_inc(&pr_reg->pr_res_holders);
1180 smp_mb__after_atomic_inc();
1181 spin_unlock(&pr_tmpl->registration_lock);
1182 return pr_reg;
1183 }
1184 spin_unlock(&pr_tmpl->registration_lock);
1185
1186 return NULL;
1187}
1188
1189static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1190 struct se_device *dev,
1191 struct se_node_acl *nacl,
1192 struct se_session *sess)
1193{
1194 struct se_portal_group *tpg = nacl->se_tpg;
1195 unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1196
Andy Grovere3d6f902011-07-19 08:55:10 +00001197 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001198 memset(&buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +00001199 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001200 PR_REG_ISID_LEN);
1201 isid_ptr = &buf[0];
1202 }
1203
1204 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1205}
1206
1207static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1208{
1209 atomic_dec(&pr_reg->pr_res_holders);
1210 smp_mb__after_atomic_dec();
1211}
1212
1213static int core_scsi3_check_implict_release(
1214 struct se_device *dev,
1215 struct t10_pr_registration *pr_reg)
1216{
1217 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1218 struct t10_pr_registration *pr_res_holder;
1219 int ret = 0;
1220
1221 spin_lock(&dev->dev_reservation_lock);
1222 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001223 if (!pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001224 spin_unlock(&dev->dev_reservation_lock);
1225 return ret;
1226 }
1227 if (pr_res_holder == pr_reg) {
1228 /*
1229 * Perform an implict RELEASE if the registration that
1230 * is being released is holding the reservation.
1231 *
1232 * From spc4r17, section 5.7.11.1:
1233 *
1234 * e) If the I_T nexus is the persistent reservation holder
1235 * and the persistent reservation is not an all registrants
1236 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1237 * service action or REGISTER AND IGNORE EXISTING KEY
1238 * service action with the SERVICE ACTION RESERVATION KEY
1239 * field set to zero (see 5.7.11.3).
1240 */
1241 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0);
1242 ret = 1;
1243 /*
1244 * For 'All Registrants' reservation types, all existing
1245 * registrations are still processed as reservation holders
1246 * in core_scsi3_pr_seq_non_holder() after the initial
1247 * reservation holder is implictly released here.
1248 */
1249 } else if (pr_reg->pr_reg_all_tg_pt &&
1250 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1251 pr_reg->pr_reg_nacl->initiatorname)) &&
1252 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001253 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001254 " UNREGISTER while existing reservation with matching"
1255 " key 0x%016Lx is present from another SCSI Initiator"
1256 " Port\n", pr_reg->pr_res_key);
Andy Grovere3d6f902011-07-19 08:55:10 +00001257 ret = -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001258 }
1259 spin_unlock(&dev->dev_reservation_lock);
1260
1261 return ret;
1262}
1263
1264/*
Andy Grovere3d6f902011-07-19 08:55:10 +00001265 * Called with struct t10_reservation->registration_lock held.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001266 */
1267static void __core_scsi3_free_registration(
1268 struct se_device *dev,
1269 struct t10_pr_registration *pr_reg,
1270 struct list_head *preempt_and_abort_list,
1271 int dec_holders)
1272{
1273 struct target_core_fabric_ops *tfo =
1274 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Andy Grovere3d6f902011-07-19 08:55:10 +00001275 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001276 char i_buf[PR_REG_ISID_ID_LEN];
1277 int prf_isid;
1278
1279 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1280 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1281 PR_REG_ISID_ID_LEN);
1282
1283 pr_reg->pr_reg_deve->def_pr_registered = 0;
1284 pr_reg->pr_reg_deve->pr_res_key = 0;
1285 list_del(&pr_reg->pr_reg_list);
1286 /*
1287 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1288 * so call core_scsi3_put_pr_reg() to decrement our reference.
1289 */
1290 if (dec_holders)
1291 core_scsi3_put_pr_reg(pr_reg);
1292 /*
1293 * Wait until all reference from any other I_T nexuses for this
1294 * *pr_reg have been released. Because list_del() is called above,
1295 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1296 * count back to zero, and we release *pr_reg.
1297 */
1298 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
1299 spin_unlock(&pr_tmpl->registration_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07001300 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001301 tfo->get_fabric_name());
1302 cpu_relax();
1303 spin_lock(&pr_tmpl->registration_lock);
1304 }
1305
Andy Grover6708bb22011-06-08 10:36:43 -07001306 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001307 " Node: %s%s\n", tfo->get_fabric_name(),
1308 pr_reg->pr_reg_nacl->initiatorname,
1309 (prf_isid) ? &i_buf[0] : "");
Andy Grover6708bb22011-06-08 10:36:43 -07001310 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001311 " Port(s)\n", tfo->get_fabric_name(),
1312 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
Andy Grovere3d6f902011-07-19 08:55:10 +00001313 dev->transport->name);
Andy Grover6708bb22011-06-08 10:36:43 -07001314 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001315 " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key,
1316 pr_reg->pr_res_generation);
1317
Andy Grover6708bb22011-06-08 10:36:43 -07001318 if (!preempt_and_abort_list) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001319 pr_reg->pr_reg_deve = NULL;
1320 pr_reg->pr_reg_nacl = NULL;
1321 kfree(pr_reg->pr_aptpl_buf);
1322 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1323 return;
1324 }
1325 /*
1326 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1327 * are released once the ABORT_TASK_SET has completed..
1328 */
1329 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1330}
1331
1332void core_scsi3_free_pr_reg_from_nacl(
1333 struct se_device *dev,
1334 struct se_node_acl *nacl)
1335{
Andy Grovere3d6f902011-07-19 08:55:10 +00001336 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001337 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1338 /*
1339 * If the passed se_node_acl matches the reservation holder,
1340 * release the reservation.
1341 */
1342 spin_lock(&dev->dev_reservation_lock);
1343 pr_res_holder = dev->dev_pr_res_holder;
1344 if ((pr_res_holder != NULL) &&
1345 (pr_res_holder->pr_reg_nacl == nacl))
1346 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0);
1347 spin_unlock(&dev->dev_reservation_lock);
1348 /*
1349 * Release any registration associated with the struct se_node_acl.
1350 */
1351 spin_lock(&pr_tmpl->registration_lock);
1352 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1353 &pr_tmpl->registration_list, pr_reg_list) {
1354
1355 if (pr_reg->pr_reg_nacl != nacl)
1356 continue;
1357
1358 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1359 }
1360 spin_unlock(&pr_tmpl->registration_lock);
1361}
1362
1363void core_scsi3_free_all_registrations(
1364 struct se_device *dev)
1365{
Andy Grovere3d6f902011-07-19 08:55:10 +00001366 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001367 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1368
1369 spin_lock(&dev->dev_reservation_lock);
1370 pr_res_holder = dev->dev_pr_res_holder;
1371 if (pr_res_holder != NULL) {
1372 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1373 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
1374 pr_res_holder, 0);
1375 }
1376 spin_unlock(&dev->dev_reservation_lock);
1377
1378 spin_lock(&pr_tmpl->registration_lock);
1379 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1380 &pr_tmpl->registration_list, pr_reg_list) {
1381
1382 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1383 }
1384 spin_unlock(&pr_tmpl->registration_lock);
1385
1386 spin_lock(&pr_tmpl->aptpl_reg_lock);
1387 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1388 pr_reg_aptpl_list) {
1389 list_del(&pr_reg->pr_reg_aptpl_list);
1390 kfree(pr_reg->pr_aptpl_buf);
1391 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1392 }
1393 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1394}
1395
1396static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1397{
Andy Grovere3d6f902011-07-19 08:55:10 +00001398 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001399 &tpg->tpg_group.cg_item);
1400}
1401
1402static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1403{
Andy Grovere3d6f902011-07-19 08:55:10 +00001404 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001405 &tpg->tpg_group.cg_item);
1406
1407 atomic_dec(&tpg->tpg_pr_ref_count);
1408 smp_mb__after_atomic_dec();
1409}
1410
1411static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1412{
1413 struct se_portal_group *tpg = nacl->se_tpg;
1414
1415 if (nacl->dynamic_node_acl)
1416 return 0;
1417
Andy Grovere3d6f902011-07-19 08:55:10 +00001418 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001419 &nacl->acl_group.cg_item);
1420}
1421
1422static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1423{
1424 struct se_portal_group *tpg = nacl->se_tpg;
1425
1426 if (nacl->dynamic_node_acl) {
1427 atomic_dec(&nacl->acl_pr_ref_count);
1428 smp_mb__after_atomic_dec();
1429 return;
1430 }
1431
Andy Grovere3d6f902011-07-19 08:55:10 +00001432 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001433 &nacl->acl_group.cg_item);
1434
1435 atomic_dec(&nacl->acl_pr_ref_count);
1436 smp_mb__after_atomic_dec();
1437}
1438
1439static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1440{
1441 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1442 struct se_node_acl *nacl;
1443 struct se_portal_group *tpg;
1444 /*
1445 * For nacl->dynamic_node_acl=1
1446 */
Andy Grover6708bb22011-06-08 10:36:43 -07001447 if (!lun_acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001448 return 0;
1449
1450 nacl = lun_acl->se_lun_nacl;
1451 tpg = nacl->se_tpg;
1452
Andy Grovere3d6f902011-07-19 08:55:10 +00001453 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001454 &lun_acl->se_lun_group.cg_item);
1455}
1456
1457static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1458{
1459 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1460 struct se_node_acl *nacl;
1461 struct se_portal_group *tpg;
1462 /*
1463 * For nacl->dynamic_node_acl=1
1464 */
Andy Grover6708bb22011-06-08 10:36:43 -07001465 if (!lun_acl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001466 atomic_dec(&se_deve->pr_ref_count);
1467 smp_mb__after_atomic_dec();
1468 return;
1469 }
1470 nacl = lun_acl->se_lun_nacl;
1471 tpg = nacl->se_tpg;
1472
Andy Grovere3d6f902011-07-19 08:55:10 +00001473 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001474 &lun_acl->se_lun_group.cg_item);
1475
1476 atomic_dec(&se_deve->pr_ref_count);
1477 smp_mb__after_atomic_dec();
1478}
1479
1480static int core_scsi3_decode_spec_i_port(
1481 struct se_cmd *cmd,
1482 struct se_portal_group *tpg,
1483 unsigned char *l_isid,
1484 u64 sa_res_key,
1485 int all_tg_pt,
1486 int aptpl)
1487{
Andy Grover5951146d2011-07-19 10:26:37 +00001488 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001489 struct se_port *tmp_port;
1490 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00001491 struct se_session *se_sess = cmd->se_sess;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001492 struct se_node_acl *dest_node_acl = NULL;
1493 struct se_dev_entry *dest_se_deve = NULL, *local_se_deve;
1494 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1495 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
Roland Dreierd0f474e2012-01-12 10:41:18 -08001496 LIST_HEAD(tid_dest_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001497 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
1498 struct target_core_fabric_ops *tmp_tf_ops;
Andy Grover05d1c7c2011-07-20 19:13:28 +00001499 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001500 unsigned char *ptr, *i_str = NULL, proto_ident, tmp_proto_ident;
1501 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
1502 u32 tpdl, tid_len = 0;
1503 int ret, dest_local_nexus, prf_isid;
1504 u32 dest_rtpi = 0;
1505
1506 memset(dest_iport, 0, 64);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001507
Jörn Engelf2083242012-03-15 15:05:40 -04001508 local_se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001509 /*
1510 * Allocate a struct pr_transport_id_holder and setup the
1511 * local_node_acl and local_se_deve pointers and add to
1512 * struct list_head tid_dest_list for add registration
1513 * processing in the loop of tid_dest_list below.
1514 */
1515 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001516 if (!tidh_new) {
1517 pr_err("Unable to allocate tidh_new\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001518 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1519 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001520 }
1521 INIT_LIST_HEAD(&tidh_new->dest_list);
1522 tidh_new->dest_tpg = tpg;
1523 tidh_new->dest_node_acl = se_sess->se_node_acl;
1524 tidh_new->dest_se_deve = local_se_deve;
1525
Andy Grover5951146d2011-07-19 10:26:37 +00001526 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001527 se_sess->se_node_acl, local_se_deve, l_isid,
1528 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001529 if (!local_pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001530 kfree(tidh_new);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001531 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1532 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001533 }
1534 tidh_new->dest_pr_reg = local_pr_reg;
1535 /*
1536 * The local I_T nexus does not hold any configfs dependances,
1537 * so we set tid_h->dest_local_nexus=1 to prevent the
1538 * configfs_undepend_item() calls in the tid_dest_list loops below.
1539 */
1540 tidh_new->dest_local_nexus = 1;
1541 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
Andy Grover05d1c7c2011-07-20 19:13:28 +00001542
Paolo Bonzini0d7f1292012-09-07 17:30:33 +02001543 if (cmd->data_length < 28) {
1544 pr_warn("SPC-PR: Received PR OUT parameter list"
1545 " length too small: %u\n", cmd->data_length);
1546 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1547 ret = -EINVAL;
1548 goto out;
1549 }
1550
Andy Grover49493142012-01-16 16:57:08 -08001551 buf = transport_kmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001552 /*
1553 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1554 * first extract TransportID Parameter Data Length, and make sure
1555 * the value matches up to the SCSI expected data transfer length.
1556 */
1557 tpdl = (buf[24] & 0xff) << 24;
1558 tpdl |= (buf[25] & 0xff) << 16;
1559 tpdl |= (buf[26] & 0xff) << 8;
1560 tpdl |= buf[27] & 0xff;
1561
1562 if ((tpdl + 28) != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07001563 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001564 " does not equal CDB data_length: %u\n", tpdl,
1565 cmd->data_length);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001566 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1567 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001568 goto out;
1569 }
1570 /*
1571 * Start processing the received transport IDs using the
1572 * receiving I_T Nexus portal's fabric dependent methods to
1573 * obtain the SCSI Initiator Port/Device Identifiers.
1574 */
1575 ptr = &buf[28];
1576
1577 while (tpdl > 0) {
1578 proto_ident = (ptr[0] & 0x0f);
1579 dest_tpg = NULL;
1580
1581 spin_lock(&dev->se_port_lock);
1582 list_for_each_entry(tmp_port, &dev->dev_sep_list, sep_list) {
1583 tmp_tpg = tmp_port->sep_tpg;
Andy Grover6708bb22011-06-08 10:36:43 -07001584 if (!tmp_tpg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001585 continue;
Andy Grovere3d6f902011-07-19 08:55:10 +00001586 tmp_tf_ops = tmp_tpg->se_tpg_tfo;
Andy Grover6708bb22011-06-08 10:36:43 -07001587 if (!tmp_tf_ops)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001588 continue;
Andy Grover6708bb22011-06-08 10:36:43 -07001589 if (!tmp_tf_ops->get_fabric_proto_ident ||
1590 !tmp_tf_ops->tpg_parse_pr_out_transport_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001591 continue;
1592 /*
1593 * Look for the matching proto_ident provided by
1594 * the received TransportID
1595 */
1596 tmp_proto_ident = tmp_tf_ops->get_fabric_proto_ident(tmp_tpg);
1597 if (tmp_proto_ident != proto_ident)
1598 continue;
1599 dest_rtpi = tmp_port->sep_rtpi;
1600
1601 i_str = tmp_tf_ops->tpg_parse_pr_out_transport_id(
1602 tmp_tpg, (const char *)ptr, &tid_len,
1603 &iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07001604 if (!i_str)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001605 continue;
1606
1607 atomic_inc(&tmp_tpg->tpg_pr_ref_count);
1608 smp_mb__after_atomic_inc();
1609 spin_unlock(&dev->se_port_lock);
1610
1611 ret = core_scsi3_tpg_depend_item(tmp_tpg);
1612 if (ret != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001613 pr_err(" core_scsi3_tpg_depend_item()"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001614 " for tmp_tpg\n");
1615 atomic_dec(&tmp_tpg->tpg_pr_ref_count);
1616 smp_mb__after_atomic_dec();
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001617 cmd->scsi_sense_reason =
1618 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1619 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001620 goto out;
1621 }
1622 /*
Masanari Iida35d1efe2012-08-16 22:43:13 +09001623 * Locate the destination initiator ACL to be registered
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001624 * from the decoded fabric module specific TransportID
1625 * at *i_str.
1626 */
Roland Dreier286388872011-08-16 09:40:01 -07001627 spin_lock_irq(&tmp_tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001628 dest_node_acl = __core_tpg_get_initiator_node_acl(
1629 tmp_tpg, i_str);
1630 if (dest_node_acl) {
1631 atomic_inc(&dest_node_acl->acl_pr_ref_count);
1632 smp_mb__after_atomic_inc();
1633 }
Roland Dreier286388872011-08-16 09:40:01 -07001634 spin_unlock_irq(&tmp_tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001635
Andy Grover6708bb22011-06-08 10:36:43 -07001636 if (!dest_node_acl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001637 core_scsi3_tpg_undepend_item(tmp_tpg);
1638 spin_lock(&dev->se_port_lock);
1639 continue;
1640 }
1641
1642 ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
1643 if (ret != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001644 pr_err("configfs_depend_item() failed"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001645 " for dest_node_acl->acl_group\n");
1646 atomic_dec(&dest_node_acl->acl_pr_ref_count);
1647 smp_mb__after_atomic_dec();
1648 core_scsi3_tpg_undepend_item(tmp_tpg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001649 cmd->scsi_sense_reason =
1650 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1651 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001652 goto out;
1653 }
1654
1655 dest_tpg = tmp_tpg;
Andy Grover6708bb22011-06-08 10:36:43 -07001656 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001657 " %s Port RTPI: %hu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001658 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001659 dest_node_acl->initiatorname, dest_rtpi);
1660
1661 spin_lock(&dev->se_port_lock);
1662 break;
1663 }
1664 spin_unlock(&dev->se_port_lock);
1665
Andy Grover6708bb22011-06-08 10:36:43 -07001666 if (!dest_tpg) {
1667 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001668 " dest_tpg\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001669 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1670 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001671 goto out;
1672 }
Andy Grover8b1e1242012-04-03 15:51:12 -07001673
Andy Grover6708bb22011-06-08 10:36:43 -07001674 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001675 " tid_len: %d for %s + %s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001676 dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001677 tpdl, tid_len, i_str, iport_ptr);
Andy Grover8b1e1242012-04-03 15:51:12 -07001678
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001679 if (tid_len > tpdl) {
Andy Grover6708bb22011-06-08 10:36:43 -07001680 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001681 " %u for Transport ID: %s\n", tid_len, ptr);
1682 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1683 core_scsi3_tpg_undepend_item(dest_tpg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001684 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1685 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001686 goto out;
1687 }
1688 /*
1689 * Locate the desintation struct se_dev_entry pointer for matching
1690 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1691 * Target Port.
1692 */
1693 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1694 dest_rtpi);
Andy Grover6708bb22011-06-08 10:36:43 -07001695 if (!dest_se_deve) {
1696 pr_err("Unable to locate %s dest_se_deve"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001697 " from destination RTPI: %hu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001698 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001699 dest_rtpi);
1700
1701 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1702 core_scsi3_tpg_undepend_item(dest_tpg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001703 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1704 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001705 goto out;
1706 }
1707
1708 ret = core_scsi3_lunacl_depend_item(dest_se_deve);
1709 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001710 pr_err("core_scsi3_lunacl_depend_item()"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001711 " failed\n");
1712 atomic_dec(&dest_se_deve->pr_ref_count);
1713 smp_mb__after_atomic_dec();
1714 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1715 core_scsi3_tpg_undepend_item(dest_tpg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001716 cmd->scsi_sense_reason =
1717 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1718 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001719 goto out;
1720 }
Andy Grover8b1e1242012-04-03 15:51:12 -07001721
Andy Grover6708bb22011-06-08 10:36:43 -07001722 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001723 " dest_se_deve mapped_lun: %u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001724 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001725 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
Andy Grover8b1e1242012-04-03 15:51:12 -07001726
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001727 /*
1728 * Skip any TransportIDs that already have a registration for
1729 * this target port.
1730 */
1731 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1732 iport_ptr);
1733 if (pr_reg_e) {
1734 core_scsi3_put_pr_reg(pr_reg_e);
1735 core_scsi3_lunacl_undepend_item(dest_se_deve);
1736 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1737 core_scsi3_tpg_undepend_item(dest_tpg);
1738 ptr += tid_len;
1739 tpdl -= tid_len;
1740 tid_len = 0;
1741 continue;
1742 }
1743 /*
1744 * Allocate a struct pr_transport_id_holder and setup
1745 * the dest_node_acl and dest_se_deve pointers for the
1746 * loop below.
1747 */
1748 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1749 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001750 if (!tidh_new) {
1751 pr_err("Unable to allocate tidh_new\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001752 core_scsi3_lunacl_undepend_item(dest_se_deve);
1753 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1754 core_scsi3_tpg_undepend_item(dest_tpg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001755 cmd->scsi_sense_reason =
1756 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1757 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001758 goto out;
1759 }
1760 INIT_LIST_HEAD(&tidh_new->dest_list);
1761 tidh_new->dest_tpg = dest_tpg;
1762 tidh_new->dest_node_acl = dest_node_acl;
1763 tidh_new->dest_se_deve = dest_se_deve;
1764
1765 /*
1766 * Allocate, but do NOT add the registration for the
1767 * TransportID referenced SCSI Initiator port. This
1768 * done because of the following from spc4r17 in section
1769 * 6.14.3 wrt SPEC_I_PT:
1770 *
1771 * "If a registration fails for any initiator port (e.g., if th
1772 * logical unit does not have enough resources available to
1773 * hold the registration information), no registrations shall be
1774 * made, and the command shall be terminated with
1775 * CHECK CONDITION status."
1776 *
1777 * That means we call __core_scsi3_alloc_registration() here,
1778 * and then call __core_scsi3_add_registration() in the
1779 * 2nd loop which will never fail.
1780 */
Andy Grover5951146d2011-07-19 10:26:37 +00001781 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001782 dest_node_acl, dest_se_deve, iport_ptr,
1783 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001784 if (!dest_pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001785 core_scsi3_lunacl_undepend_item(dest_se_deve);
1786 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1787 core_scsi3_tpg_undepend_item(dest_tpg);
1788 kfree(tidh_new);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001789 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1790 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001791 goto out;
1792 }
1793 tidh_new->dest_pr_reg = dest_pr_reg;
1794 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1795
1796 ptr += tid_len;
1797 tpdl -= tid_len;
1798 tid_len = 0;
1799
1800 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00001801
Andy Grover49493142012-01-16 16:57:08 -08001802 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00001803
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001804 /*
1805 * Go ahead and create a registrations from tid_dest_list for the
1806 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1807 * and dest_se_deve.
1808 *
1809 * The SA Reservation Key from the PROUT is set for the
1810 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1811 * means that the TransportID Initiator port will be
1812 * registered on all of the target ports in the SCSI target device
1813 * ALL_TG_PT=0 means the registration will only be for the
1814 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1815 * was received.
1816 */
1817 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1818 dest_tpg = tidh->dest_tpg;
1819 dest_node_acl = tidh->dest_node_acl;
1820 dest_se_deve = tidh->dest_se_deve;
1821 dest_pr_reg = tidh->dest_pr_reg;
1822 dest_local_nexus = tidh->dest_local_nexus;
1823
1824 list_del(&tidh->dest_list);
1825 kfree(tidh);
1826
1827 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1828 prf_isid = core_pr_dump_initiator_port(dest_pr_reg, &i_buf[0],
1829 PR_REG_ISID_ID_LEN);
1830
Andy Grover5951146d2011-07-19 10:26:37 +00001831 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001832 dest_pr_reg, 0, 0);
1833
Andy Grover6708bb22011-06-08 10:36:43 -07001834 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001835 " registered Transport ID for Node: %s%s Mapped LUN:"
Andy Grovere3d6f902011-07-19 08:55:10 +00001836 " %u\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001837 dest_node_acl->initiatorname, (prf_isid) ?
1838 &i_buf[0] : "", dest_se_deve->mapped_lun);
1839
1840 if (dest_local_nexus)
1841 continue;
1842
1843 core_scsi3_lunacl_undepend_item(dest_se_deve);
1844 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1845 core_scsi3_tpg_undepend_item(dest_tpg);
1846 }
1847
1848 return 0;
1849out:
Andy Grover49493142012-01-16 16:57:08 -08001850 transport_kunmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001851 /*
1852 * For the failure case, release everything from tid_dest_list
1853 * including *dest_pr_reg and the configfs dependances..
1854 */
1855 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1856 dest_tpg = tidh->dest_tpg;
1857 dest_node_acl = tidh->dest_node_acl;
1858 dest_se_deve = tidh->dest_se_deve;
1859 dest_pr_reg = tidh->dest_pr_reg;
1860 dest_local_nexus = tidh->dest_local_nexus;
1861
1862 list_del(&tidh->dest_list);
1863 kfree(tidh);
1864 /*
1865 * Release any extra ALL_TG_PT=1 registrations for
1866 * the SPEC_I_PT=1 case.
1867 */
1868 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1869 &dest_pr_reg->pr_reg_atp_list,
1870 pr_reg_atp_mem_list) {
1871 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1872 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1873 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1874 }
1875
1876 kfree(dest_pr_reg->pr_aptpl_buf);
1877 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1878
1879 if (dest_local_nexus)
1880 continue;
1881
1882 core_scsi3_lunacl_undepend_item(dest_se_deve);
1883 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1884 core_scsi3_tpg_undepend_item(dest_tpg);
1885 }
1886 return ret;
1887}
1888
1889/*
1890 * Called with struct se_device->dev_reservation_lock held
1891 */
1892static int __core_scsi3_update_aptpl_buf(
1893 struct se_device *dev,
1894 unsigned char *buf,
1895 u32 pr_aptpl_buf_len,
1896 int clear_aptpl_metadata)
1897{
1898 struct se_lun *lun;
1899 struct se_portal_group *tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00001900 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001901 struct t10_pr_registration *pr_reg;
1902 unsigned char tmp[512], isid_buf[32];
1903 ssize_t len = 0;
1904 int reg_count = 0;
1905
1906 memset(buf, 0, pr_aptpl_buf_len);
1907 /*
1908 * Called to clear metadata once APTPL has been deactivated.
1909 */
1910 if (clear_aptpl_metadata) {
1911 snprintf(buf, pr_aptpl_buf_len,
1912 "No Registrations or Reservations\n");
1913 return 0;
1914 }
1915 /*
1916 * Walk the registration list..
1917 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001918 spin_lock(&su_dev->t10_pr.registration_lock);
1919 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001920 pr_reg_list) {
1921
1922 tmp[0] = '\0';
1923 isid_buf[0] = '\0';
1924 tpg = pr_reg->pr_reg_nacl->se_tpg;
1925 lun = pr_reg->pr_reg_tg_pt_lun;
1926 /*
1927 * Write out any ISID value to APTPL metadata that was included
1928 * in the original registration.
1929 */
1930 if (pr_reg->isid_present_at_reg)
1931 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1932 pr_reg->pr_reg_isid);
1933 /*
1934 * Include special metadata if the pr_reg matches the
1935 * reservation holder.
1936 */
1937 if (dev->dev_pr_res_holder == pr_reg) {
1938 snprintf(tmp, 512, "PR_REG_START: %d"
1939 "\ninitiator_fabric=%s\n"
1940 "initiator_node=%s\n%s"
1941 "sa_res_key=%llu\n"
1942 "res_holder=1\nres_type=%02x\n"
1943 "res_scope=%02x\nres_all_tg_pt=%d\n"
1944 "mapped_lun=%u\n", reg_count,
Andy Grovere3d6f902011-07-19 08:55:10 +00001945 tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001946 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1947 pr_reg->pr_res_key, pr_reg->pr_res_type,
1948 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1949 pr_reg->pr_res_mapped_lun);
1950 } else {
1951 snprintf(tmp, 512, "PR_REG_START: %d\n"
1952 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1953 "sa_res_key=%llu\nres_holder=0\n"
1954 "res_all_tg_pt=%d\nmapped_lun=%u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001955 reg_count, tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001956 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1957 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1958 pr_reg->pr_res_mapped_lun);
1959 }
1960
Dan Carpenter60d645a2011-06-15 10:03:05 -07001961 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001962 pr_err("Unable to update renaming"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001963 " APTPL metadata\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001964 spin_unlock(&su_dev->t10_pr.registration_lock);
1965 return -EMSGSIZE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001966 }
1967 len += sprintf(buf+len, "%s", tmp);
1968
1969 /*
1970 * Include information about the associated SCSI target port.
1971 */
1972 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1973 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
Andy Grovere3d6f902011-07-19 08:55:10 +00001974 " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
1975 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1976 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001977 lun->lun_sep->sep_rtpi, lun->unpacked_lun, reg_count);
1978
Dan Carpenter60d645a2011-06-15 10:03:05 -07001979 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001980 pr_err("Unable to update renaming"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001981 " APTPL metadata\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001982 spin_unlock(&su_dev->t10_pr.registration_lock);
1983 return -EMSGSIZE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001984 }
1985 len += sprintf(buf+len, "%s", tmp);
1986 reg_count++;
1987 }
Andy Grovere3d6f902011-07-19 08:55:10 +00001988 spin_unlock(&su_dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001989
Andy Grover6708bb22011-06-08 10:36:43 -07001990 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001991 len += sprintf(buf+len, "No Registrations or Reservations");
1992
1993 return 0;
1994}
1995
1996static int core_scsi3_update_aptpl_buf(
1997 struct se_device *dev,
1998 unsigned char *buf,
1999 u32 pr_aptpl_buf_len,
2000 int clear_aptpl_metadata)
2001{
2002 int ret;
2003
2004 spin_lock(&dev->dev_reservation_lock);
2005 ret = __core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
2006 clear_aptpl_metadata);
2007 spin_unlock(&dev->dev_reservation_lock);
2008
2009 return ret;
2010}
2011
2012/*
2013 * Called with struct se_device->aptpl_file_mutex held
2014 */
2015static int __core_scsi3_write_aptpl_to_file(
2016 struct se_device *dev,
2017 unsigned char *buf,
2018 u32 pr_aptpl_buf_len)
2019{
Andy Grovere3d6f902011-07-19 08:55:10 +00002020 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002021 struct file *file;
2022 struct iovec iov[1];
2023 mm_segment_t old_fs;
2024 int flags = O_RDWR | O_CREAT | O_TRUNC;
2025 char path[512];
2026 int ret;
2027
2028 memset(iov, 0, sizeof(struct iovec));
2029 memset(path, 0, 512);
2030
Dan Carpenter60d645a2011-06-15 10:03:05 -07002031 if (strlen(&wwn->unit_serial[0]) >= 512) {
Andy Grover6708bb22011-06-08 10:36:43 -07002032 pr_err("WWN value for struct se_device does not fit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002033 " into path buffer\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00002034 return -EMSGSIZE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002035 }
2036
2037 snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]);
2038 file = filp_open(path, flags, 0600);
2039 if (IS_ERR(file) || !file || !file->f_dentry) {
Andy Grover6708bb22011-06-08 10:36:43 -07002040 pr_err("filp_open(%s) for APTPL metadata"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002041 " failed\n", path);
Roland Dreierd35212f2012-07-16 15:17:10 -07002042 return IS_ERR(file) ? PTR_ERR(file) : -ENOENT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002043 }
2044
2045 iov[0].iov_base = &buf[0];
Andy Grover6708bb22011-06-08 10:36:43 -07002046 if (!pr_aptpl_buf_len)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002047 iov[0].iov_len = (strlen(&buf[0]) + 1); /* Add extra for NULL */
2048 else
2049 iov[0].iov_len = pr_aptpl_buf_len;
2050
2051 old_fs = get_fs();
2052 set_fs(get_ds());
2053 ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
2054 set_fs(old_fs);
2055
2056 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002057 pr_debug("Error writing APTPL metadata file: %s\n", path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002058 filp_close(file, NULL);
Andy Grovere3d6f902011-07-19 08:55:10 +00002059 return -EIO;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002060 }
2061 filp_close(file, NULL);
2062
2063 return 0;
2064}
2065
2066static int core_scsi3_update_and_write_aptpl(
2067 struct se_device *dev,
2068 unsigned char *in_buf,
2069 u32 in_pr_aptpl_buf_len)
2070{
2071 unsigned char null_buf[64], *buf;
2072 u32 pr_aptpl_buf_len;
2073 int ret, clear_aptpl_metadata = 0;
2074 /*
2075 * Can be called with a NULL pointer from PROUT service action CLEAR
2076 */
Andy Grover6708bb22011-06-08 10:36:43 -07002077 if (!in_buf) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002078 memset(null_buf, 0, 64);
2079 buf = &null_buf[0];
2080 /*
2081 * This will clear the APTPL metadata to:
2082 * "No Registrations or Reservations" status
2083 */
2084 pr_aptpl_buf_len = 64;
2085 clear_aptpl_metadata = 1;
2086 } else {
2087 buf = in_buf;
2088 pr_aptpl_buf_len = in_pr_aptpl_buf_len;
2089 }
2090
2091 ret = core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
2092 clear_aptpl_metadata);
2093 if (ret != 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00002094 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002095 /*
2096 * __core_scsi3_write_aptpl_to_file() will call strlen()
2097 * on the passed buf to determine pr_aptpl_buf_len.
2098 */
2099 ret = __core_scsi3_write_aptpl_to_file(dev, buf, 0);
2100 if (ret != 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00002101 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002102
2103 return ret;
2104}
2105
2106static int core_scsi3_emulate_pro_register(
2107 struct se_cmd *cmd,
2108 u64 res_key,
2109 u64 sa_res_key,
2110 int aptpl,
2111 int all_tg_pt,
2112 int spec_i_pt,
2113 int ignore_key)
2114{
Andy Grovere3d6f902011-07-19 08:55:10 +00002115 struct se_session *se_sess = cmd->se_sess;
Andy Grover5951146d2011-07-19 10:26:37 +00002116 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002117 struct se_dev_entry *se_deve;
Andy Grovere3d6f902011-07-19 08:55:10 +00002118 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002119 struct se_portal_group *se_tpg;
2120 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp, *pr_reg_e;
Andy Grovere3d6f902011-07-19 08:55:10 +00002121 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002122 /* Used for APTPL metadata w/ UNREGISTER */
2123 unsigned char *pr_aptpl_buf = NULL;
2124 unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
2125 int pr_holder = 0, ret = 0, type;
2126
Andy Grover6708bb22011-06-08 10:36:43 -07002127 if (!se_sess || !se_lun) {
2128 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002129 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2130 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002131 }
2132 se_tpg = se_sess->se_tpg;
Jörn Engelf2083242012-03-15 15:05:40 -04002133 se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002134
Andy Grovere3d6f902011-07-19 08:55:10 +00002135 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002136 memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +00002137 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002138 PR_REG_ISID_LEN);
2139 isid_ptr = &isid_buf[0];
2140 }
2141 /*
2142 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2143 */
2144 pr_reg_e = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002145 if (!pr_reg_e) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002146 if (res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002147 pr_warn("SPC-3 PR: Reservation Key non-zero"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002148 " for SA REGISTER, returning CONFLICT\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002149 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2150 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002151 }
2152 /*
2153 * Do nothing but return GOOD status.
2154 */
Andy Grover6708bb22011-06-08 10:36:43 -07002155 if (!sa_res_key)
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002156 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002157
Andy Grover6708bb22011-06-08 10:36:43 -07002158 if (!spec_i_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002159 /*
2160 * Perform the Service Action REGISTER on the Initiator
2161 * Port Endpoint that the PRO was received from on the
2162 * Logical Unit of the SCSI device server.
2163 */
Andy Grover5951146d2011-07-19 10:26:37 +00002164 ret = core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002165 se_sess->se_node_acl, se_deve, isid_ptr,
2166 sa_res_key, all_tg_pt, aptpl,
2167 ignore_key, 0);
2168 if (ret != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002169 pr_err("Unable to allocate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002170 " struct t10_pr_registration\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002171 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2172 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002173 }
2174 } else {
2175 /*
2176 * Register both the Initiator port that received
2177 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2178 * TransportID from Parameter list and loop through
2179 * fabric dependent parameter list while calling
2180 * logic from of core_scsi3_alloc_registration() for
2181 * each TransportID provided SCSI Initiator Port/Device
2182 */
2183 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2184 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2185 if (ret != 0)
2186 return ret;
2187 }
2188 /*
2189 * Nothing left to do for the APTPL=0 case.
2190 */
Andy Grover6708bb22011-06-08 10:36:43 -07002191 if (!aptpl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002192 pr_tmpl->pr_aptpl_active = 0;
Andy Grover5951146d2011-07-19 10:26:37 +00002193 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002194 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002195 " REGISTER\n");
2196 return 0;
2197 }
2198 /*
2199 * Locate the newly allocated local I_T Nexus *pr_reg, and
2200 * update the APTPL metadata information using its
2201 * preallocated *pr_reg->pr_aptpl_buf.
2202 */
Andy Grover5951146d2011-07-19 10:26:37 +00002203 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002204 se_sess->se_node_acl, se_sess);
2205
Andy Grover5951146d2011-07-19 10:26:37 +00002206 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002207 &pr_reg->pr_aptpl_buf[0],
2208 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07002209 if (!ret) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002210 pr_tmpl->pr_aptpl_active = 1;
Andy Grover6708bb22011-06-08 10:36:43 -07002211 pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002212 }
2213
2214 core_scsi3_put_pr_reg(pr_reg);
2215 return ret;
2216 } else {
2217 /*
2218 * Locate the existing *pr_reg via struct se_node_acl pointers
2219 */
2220 pr_reg = pr_reg_e;
2221 type = pr_reg->pr_res_type;
2222
Andy Grover6708bb22011-06-08 10:36:43 -07002223 if (!ignore_key) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002224 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002225 pr_err("SPC-3 PR REGISTER: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002226 " res_key: 0x%016Lx does not match"
2227 " existing SA REGISTER res_key:"
2228 " 0x%016Lx\n", res_key,
2229 pr_reg->pr_res_key);
2230 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002231 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2232 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002233 }
2234 }
2235 if (spec_i_pt) {
Andy Grover6708bb22011-06-08 10:36:43 -07002236 pr_err("SPC-3 PR UNREGISTER: SPEC_I_PT"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002237 " set while sa_res_key=0\n");
2238 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002239 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2240 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002241 }
2242 /*
2243 * An existing ALL_TG_PT=1 registration being released
2244 * must also set ALL_TG_PT=1 in the incoming PROUT.
2245 */
2246 if (pr_reg->pr_reg_all_tg_pt && !(all_tg_pt)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002247 pr_err("SPC-3 PR UNREGISTER: ALL_TG_PT=1"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002248 " registration exists, but ALL_TG_PT=1 bit not"
2249 " present in received PROUT\n");
2250 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002251 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2252 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002253 }
2254 /*
2255 * Allocate APTPL metadata buffer used for UNREGISTER ops
2256 */
2257 if (aptpl) {
2258 pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len,
2259 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002260 if (!pr_aptpl_buf) {
2261 pr_err("Unable to allocate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002262 " pr_aptpl_buf\n");
2263 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002264 cmd->scsi_sense_reason =
2265 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2266 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002267 }
2268 }
2269 /*
2270 * sa_res_key=0 Unregister Reservation Key for registered I_T
2271 * Nexus sa_res_key=1 Change Reservation Key for registered I_T
2272 * Nexus.
2273 */
Andy Grover6708bb22011-06-08 10:36:43 -07002274 if (!sa_res_key) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002275 pr_holder = core_scsi3_check_implict_release(
Andy Grover5951146d2011-07-19 10:26:37 +00002276 cmd->se_dev, pr_reg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002277 if (pr_holder < 0) {
2278 kfree(pr_aptpl_buf);
2279 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002280 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2281 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002282 }
2283
2284 spin_lock(&pr_tmpl->registration_lock);
2285 /*
2286 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2287 * and matching pr_res_key.
2288 */
2289 if (pr_reg->pr_reg_all_tg_pt) {
2290 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2291 &pr_tmpl->registration_list,
2292 pr_reg_list) {
2293
Andy Grover6708bb22011-06-08 10:36:43 -07002294 if (!pr_reg_p->pr_reg_all_tg_pt)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002295 continue;
2296
2297 if (pr_reg_p->pr_res_key != res_key)
2298 continue;
2299
2300 if (pr_reg == pr_reg_p)
2301 continue;
2302
2303 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2304 pr_reg_p->pr_reg_nacl->initiatorname))
2305 continue;
2306
2307 __core_scsi3_free_registration(dev,
2308 pr_reg_p, NULL, 0);
2309 }
2310 }
2311 /*
2312 * Release the calling I_T Nexus registration now..
2313 */
Andy Grover5951146d2011-07-19 10:26:37 +00002314 __core_scsi3_free_registration(cmd->se_dev, pr_reg,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002315 NULL, 1);
2316 /*
2317 * From spc4r17, section 5.7.11.3 Unregistering
2318 *
2319 * If the persistent reservation is a registrants only
2320 * type, the device server shall establish a unit
2321 * attention condition for the initiator port associated
2322 * with every registered I_T nexus except for the I_T
2323 * nexus on which the PERSISTENT RESERVE OUT command was
2324 * received, with the additional sense code set to
2325 * RESERVATIONS RELEASED.
2326 */
2327 if (pr_holder &&
2328 ((type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
2329 (type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY))) {
2330 list_for_each_entry(pr_reg_p,
2331 &pr_tmpl->registration_list,
2332 pr_reg_list) {
2333
2334 core_scsi3_ua_allocate(
2335 pr_reg_p->pr_reg_nacl,
2336 pr_reg_p->pr_res_mapped_lun,
2337 0x2A,
2338 ASCQ_2AH_RESERVATIONS_RELEASED);
2339 }
2340 }
2341 spin_unlock(&pr_tmpl->registration_lock);
2342
Andy Grover6708bb22011-06-08 10:36:43 -07002343 if (!aptpl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002344 pr_tmpl->pr_aptpl_active = 0;
2345 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002346 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002347 " for UNREGISTER\n");
2348 return 0;
2349 }
2350
2351 ret = core_scsi3_update_and_write_aptpl(dev,
2352 &pr_aptpl_buf[0],
2353 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07002354 if (!ret) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002355 pr_tmpl->pr_aptpl_active = 1;
Andy Grover6708bb22011-06-08 10:36:43 -07002356 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002357 " for UNREGISTER\n");
2358 }
2359
2360 kfree(pr_aptpl_buf);
2361 return ret;
2362 } else {
2363 /*
2364 * Increment PRgeneration counter for struct se_device"
2365 * upon a successful REGISTER, see spc4r17 section 6.3.2
2366 * READ_KEYS service action.
2367 */
2368 pr_reg->pr_res_generation = core_scsi3_pr_generation(
Andy Grover5951146d2011-07-19 10:26:37 +00002369 cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002370 pr_reg->pr_res_key = sa_res_key;
Andy Grover6708bb22011-06-08 10:36:43 -07002371 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002372 " Key for %s to: 0x%016Lx PRgeneration:"
Andy Grovere3d6f902011-07-19 08:55:10 +00002373 " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002374 (ignore_key) ? "_AND_IGNORE_EXISTING_KEY" : "",
2375 pr_reg->pr_reg_nacl->initiatorname,
2376 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2377
Andy Grover6708bb22011-06-08 10:36:43 -07002378 if (!aptpl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002379 pr_tmpl->pr_aptpl_active = 0;
2380 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2381 core_scsi3_put_pr_reg(pr_reg);
Andy Grover6708bb22011-06-08 10:36:43 -07002382 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002383 " for REGISTER\n");
2384 return 0;
2385 }
2386
2387 ret = core_scsi3_update_and_write_aptpl(dev,
2388 &pr_aptpl_buf[0],
2389 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07002390 if (!ret) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002391 pr_tmpl->pr_aptpl_active = 1;
Andy Grover6708bb22011-06-08 10:36:43 -07002392 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002393 " for REGISTER\n");
2394 }
2395
2396 kfree(pr_aptpl_buf);
2397 core_scsi3_put_pr_reg(pr_reg);
2398 }
2399 }
2400 return 0;
2401}
2402
2403unsigned char *core_scsi3_pr_dump_type(int type)
2404{
2405 switch (type) {
2406 case PR_TYPE_WRITE_EXCLUSIVE:
2407 return "Write Exclusive Access";
2408 case PR_TYPE_EXCLUSIVE_ACCESS:
2409 return "Exclusive Access";
2410 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2411 return "Write Exclusive Access, Registrants Only";
2412 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2413 return "Exclusive Access, Registrants Only";
2414 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2415 return "Write Exclusive Access, All Registrants";
2416 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2417 return "Exclusive Access, All Registrants";
2418 default:
2419 break;
2420 }
2421
2422 return "Unknown SPC-3 PR Type";
2423}
2424
2425static int core_scsi3_pro_reserve(
2426 struct se_cmd *cmd,
2427 struct se_device *dev,
2428 int type,
2429 int scope,
2430 u64 res_key)
2431{
Andy Grovere3d6f902011-07-19 08:55:10 +00002432 struct se_session *se_sess = cmd->se_sess;
Andy Grovere3d6f902011-07-19 08:55:10 +00002433 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002434 struct t10_pr_registration *pr_reg, *pr_res_holder;
Andy Grovere3d6f902011-07-19 08:55:10 +00002435 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002436 char i_buf[PR_REG_ISID_ID_LEN];
2437 int ret, prf_isid;
2438
2439 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2440
Andy Grover6708bb22011-06-08 10:36:43 -07002441 if (!se_sess || !se_lun) {
2442 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002443 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2444 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002445 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002446 /*
2447 * Locate the existing *pr_reg via struct se_node_acl pointers
2448 */
Andy Grover5951146d2011-07-19 10:26:37 +00002449 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002450 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002451 if (!pr_reg) {
2452 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002453 " PR_REGISTERED *pr_reg for RESERVE\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002454 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2455 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002456 }
2457 /*
2458 * From spc4r17 Section 5.7.9: Reserving:
2459 *
2460 * An application client creates a persistent reservation by issuing
2461 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2462 * a registered I_T nexus with the following parameters:
2463 * a) RESERVATION KEY set to the value of the reservation key that is
2464 * registered with the logical unit for the I_T nexus; and
2465 */
2466 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002467 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002468 " does not match existing SA REGISTER res_key:"
2469 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2470 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002471 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2472 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002473 }
2474 /*
2475 * From spc4r17 Section 5.7.9: Reserving:
2476 *
2477 * From above:
2478 * b) TYPE field and SCOPE field set to the persistent reservation
2479 * being created.
2480 *
2481 * Only one persistent reservation is allowed at a time per logical unit
2482 * and that persistent reservation has a scope of LU_SCOPE.
2483 */
2484 if (scope != PR_SCOPE_LU_SCOPE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002485 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002486 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002487 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2488 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002489 }
2490 /*
2491 * See if we have an existing PR reservation holder pointer at
2492 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2493 * *pr_res_holder.
2494 */
2495 spin_lock(&dev->dev_reservation_lock);
2496 pr_res_holder = dev->dev_pr_res_holder;
Andy Groveree1b1b92012-07-12 17:34:54 -07002497 if (pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002498 /*
2499 * From spc4r17 Section 5.7.9: Reserving:
2500 *
2501 * If the device server receives a PERSISTENT RESERVE OUT
2502 * command from an I_T nexus other than a persistent reservation
2503 * holder (see 5.7.10) that attempts to create a persistent
2504 * reservation when a persistent reservation already exists for
2505 * the logical unit, then the command shall be completed with
2506 * RESERVATION CONFLICT status.
2507 */
2508 if (pr_res_holder != pr_reg) {
2509 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002510 pr_err("SPC-3 PR: Attempted RESERVE from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002511 " [%s]: %s while reservation already held by"
2512 " [%s]: %s, returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002513 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002514 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002515 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002516 pr_res_holder->pr_reg_nacl->initiatorname);
2517
2518 spin_unlock(&dev->dev_reservation_lock);
2519 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002520 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2521 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002522 }
2523 /*
2524 * From spc4r17 Section 5.7.9: Reserving:
2525 *
2526 * If a persistent reservation holder attempts to modify the
2527 * type or scope of an existing persistent reservation, the
2528 * command shall be completed with RESERVATION CONFLICT status.
2529 */
2530 if ((pr_res_holder->pr_res_type != type) ||
2531 (pr_res_holder->pr_res_scope != scope)) {
2532 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002533 pr_err("SPC-3 PR: Attempted RESERVE from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002534 " [%s]: %s trying to change TYPE and/or SCOPE,"
2535 " while reservation already held by [%s]: %s,"
2536 " returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002537 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002538 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002539 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002540 pr_res_holder->pr_reg_nacl->initiatorname);
2541
2542 spin_unlock(&dev->dev_reservation_lock);
2543 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002544 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2545 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002546 }
2547 /*
2548 * From spc4r17 Section 5.7.9: Reserving:
2549 *
2550 * If the device server receives a PERSISTENT RESERVE OUT
2551 * command with RESERVE service action where the TYPE field and
2552 * the SCOPE field contain the same values as the existing type
2553 * and scope from a persistent reservation holder, it shall not
2554 * make any change to the existing persistent reservation and
2555 * shall completethe command with GOOD status.
2556 */
2557 spin_unlock(&dev->dev_reservation_lock);
2558 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002559 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002560 }
2561 /*
2562 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2563 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2564 */
2565 pr_reg->pr_res_scope = scope;
2566 pr_reg->pr_res_type = type;
2567 pr_reg->pr_res_holder = 1;
2568 dev->dev_pr_res_holder = pr_reg;
2569 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2570 PR_REG_ISID_ID_LEN);
2571
Andy Grover6708bb22011-06-08 10:36:43 -07002572 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002573 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002574 cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002575 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002576 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002577 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002578 se_sess->se_node_acl->initiatorname,
2579 (prf_isid) ? &i_buf[0] : "");
2580 spin_unlock(&dev->dev_reservation_lock);
2581
2582 if (pr_tmpl->pr_aptpl_active) {
Andy Grover5951146d2011-07-19 10:26:37 +00002583 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002584 &pr_reg->pr_aptpl_buf[0],
2585 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07002586 if (!ret)
2587 pr_debug("SPC-3 PR: Updated APTPL metadata"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002588 " for RESERVE\n");
2589 }
2590
2591 core_scsi3_put_pr_reg(pr_reg);
2592 return 0;
2593}
2594
2595static int core_scsi3_emulate_pro_reserve(
2596 struct se_cmd *cmd,
2597 int type,
2598 int scope,
2599 u64 res_key)
2600{
2601 struct se_device *dev = cmd->se_dev;
2602 int ret = 0;
2603
2604 switch (type) {
2605 case PR_TYPE_WRITE_EXCLUSIVE:
2606 case PR_TYPE_EXCLUSIVE_ACCESS:
2607 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2608 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2609 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2610 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2611 ret = core_scsi3_pro_reserve(cmd, dev, type, scope, res_key);
2612 break;
2613 default:
Andy Grover6708bb22011-06-08 10:36:43 -07002614 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002615 " 0x%02x\n", type);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002616 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2617 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002618 }
2619
2620 return ret;
2621}
2622
2623/*
2624 * Called with struct se_device->dev_reservation_lock held.
2625 */
2626static void __core_scsi3_complete_pro_release(
2627 struct se_device *dev,
2628 struct se_node_acl *se_nacl,
2629 struct t10_pr_registration *pr_reg,
2630 int explict)
2631{
2632 struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2633 char i_buf[PR_REG_ISID_ID_LEN];
2634 int prf_isid;
2635
2636 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2637 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2638 PR_REG_ISID_ID_LEN);
2639 /*
2640 * Go ahead and release the current PR reservation holder.
2641 */
2642 dev->dev_pr_res_holder = NULL;
2643
Andy Grover6708bb22011-06-08 10:36:43 -07002644 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002645 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2646 tfo->get_fabric_name(), (explict) ? "explict" : "implict",
2647 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
2648 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002649 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002650 tfo->get_fabric_name(), se_nacl->initiatorname,
2651 (prf_isid) ? &i_buf[0] : "");
2652 /*
2653 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2654 */
2655 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2656}
2657
2658static int core_scsi3_emulate_pro_release(
2659 struct se_cmd *cmd,
2660 int type,
2661 int scope,
2662 u64 res_key)
2663{
2664 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00002665 struct se_session *se_sess = cmd->se_sess;
2666 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002667 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
Andy Grovere3d6f902011-07-19 08:55:10 +00002668 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002669 int ret, all_reg = 0;
2670
Andy Grover6708bb22011-06-08 10:36:43 -07002671 if (!se_sess || !se_lun) {
2672 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002673 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2674 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002675 }
2676 /*
2677 * Locate the existing *pr_reg via struct se_node_acl pointers
2678 */
2679 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002680 if (!pr_reg) {
2681 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002682 " PR_REGISTERED *pr_reg for RELEASE\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002683 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2684 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002685 }
2686 /*
2687 * From spc4r17 Section 5.7.11.2 Releasing:
2688 *
2689 * If there is no persistent reservation or in response to a persistent
2690 * reservation release request from a registered I_T nexus that is not a
2691 * persistent reservation holder (see 5.7.10), the device server shall
2692 * do the following:
2693 *
2694 * a) Not release the persistent reservation, if any;
2695 * b) Not remove any registrations; and
2696 * c) Complete the command with GOOD status.
2697 */
2698 spin_lock(&dev->dev_reservation_lock);
2699 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07002700 if (!pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002701 /*
2702 * No persistent reservation, return GOOD status.
2703 */
2704 spin_unlock(&dev->dev_reservation_lock);
2705 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002706 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002707 }
2708 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2709 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
2710 all_reg = 1;
2711
2712 if ((all_reg == 0) && (pr_res_holder != pr_reg)) {
2713 /*
2714 * Non 'All Registrants' PR Type cases..
2715 * Release request from a registered I_T nexus that is not a
2716 * persistent reservation holder. return GOOD status.
2717 */
2718 spin_unlock(&dev->dev_reservation_lock);
2719 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002720 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002721 }
2722 /*
2723 * From spc4r17 Section 5.7.11.2 Releasing:
2724 *
2725 * Only the persistent reservation holder (see 5.7.10) is allowed to
2726 * release a persistent reservation.
2727 *
2728 * An application client releases the persistent reservation by issuing
2729 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2730 * an I_T nexus that is a persistent reservation holder with the
2731 * following parameters:
2732 *
2733 * a) RESERVATION KEY field set to the value of the reservation key
2734 * that is registered with the logical unit for the I_T nexus;
2735 */
2736 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002737 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002738 " does not match existing SA REGISTER res_key:"
2739 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2740 spin_unlock(&dev->dev_reservation_lock);
2741 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002742 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2743 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002744 }
2745 /*
2746 * From spc4r17 Section 5.7.11.2 Releasing and above:
2747 *
2748 * b) TYPE field and SCOPE field set to match the persistent
2749 * reservation being released.
2750 */
2751 if ((pr_res_holder->pr_res_type != type) ||
2752 (pr_res_holder->pr_res_scope != scope)) {
2753 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002754 pr_err("SPC-3 PR RELEASE: Attempted to release"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002755 " reservation from [%s]: %s with different TYPE "
2756 "and/or SCOPE while reservation already held by"
2757 " [%s]: %s, returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002758 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002759 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002760 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002761 pr_res_holder->pr_reg_nacl->initiatorname);
2762
2763 spin_unlock(&dev->dev_reservation_lock);
2764 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002765 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2766 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002767 }
2768 /*
2769 * In response to a persistent reservation release request from the
2770 * persistent reservation holder the device server shall perform a
2771 * release by doing the following as an uninterrupted series of actions:
2772 * a) Release the persistent reservation;
2773 * b) Not remove any registration(s);
2774 * c) If the released persistent reservation is a registrants only type
2775 * or all registrants type persistent reservation,
2776 * the device server shall establish a unit attention condition for
2777 * the initiator port associated with every regis-
2778 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2779 * RESERVE OUT command with RELEASE service action was received,
2780 * with the additional sense code set to RESERVATIONS RELEASED; and
2781 * d) If the persistent reservation is of any other type, the device
2782 * server shall not establish a unit attention condition.
2783 */
2784 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
2785 pr_reg, 1);
2786
2787 spin_unlock(&dev->dev_reservation_lock);
2788
2789 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2790 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2791 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2792 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2793 /*
2794 * If no UNIT ATTENTION conditions will be established for
2795 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2796 * go ahead and check for APTPL=1 update+write below
2797 */
2798 goto write_aptpl;
2799 }
2800
2801 spin_lock(&pr_tmpl->registration_lock);
2802 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2803 pr_reg_list) {
2804 /*
2805 * Do not establish a UNIT ATTENTION condition
2806 * for the calling I_T Nexus
2807 */
2808 if (pr_reg_p == pr_reg)
2809 continue;
2810
2811 core_scsi3_ua_allocate(pr_reg_p->pr_reg_nacl,
2812 pr_reg_p->pr_res_mapped_lun,
2813 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2814 }
2815 spin_unlock(&pr_tmpl->registration_lock);
2816
2817write_aptpl:
2818 if (pr_tmpl->pr_aptpl_active) {
Andy Grover5951146d2011-07-19 10:26:37 +00002819 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002820 &pr_reg->pr_aptpl_buf[0],
2821 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07002822 if (!ret)
2823 pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002824 }
2825
2826 core_scsi3_put_pr_reg(pr_reg);
2827 return 0;
2828}
2829
2830static int core_scsi3_emulate_pro_clear(
2831 struct se_cmd *cmd,
2832 u64 res_key)
2833{
2834 struct se_device *dev = cmd->se_dev;
2835 struct se_node_acl *pr_reg_nacl;
Andy Grovere3d6f902011-07-19 08:55:10 +00002836 struct se_session *se_sess = cmd->se_sess;
2837 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002838 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2839 u32 pr_res_mapped_lun = 0;
2840 int calling_it_nexus = 0;
2841 /*
2842 * Locate the existing *pr_reg via struct se_node_acl pointers
2843 */
Andy Grover5951146d2011-07-19 10:26:37 +00002844 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002845 se_sess->se_node_acl, se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002846 if (!pr_reg_n) {
2847 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002848 " PR_REGISTERED *pr_reg for CLEAR\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002849 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2850 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002851 }
2852 /*
2853 * From spc4r17 section 5.7.11.6, Clearing:
2854 *
2855 * Any application client may release the persistent reservation and
2856 * remove all registrations from a device server by issuing a
2857 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2858 * registered I_T nexus with the following parameter:
2859 *
2860 * a) RESERVATION KEY field set to the value of the reservation key
2861 * that is registered with the logical unit for the I_T nexus.
2862 */
2863 if (res_key != pr_reg_n->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002864 pr_err("SPC-3 PR REGISTER: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002865 " res_key: 0x%016Lx does not match"
2866 " existing SA REGISTER res_key:"
2867 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2868 core_scsi3_put_pr_reg(pr_reg_n);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002869 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2870 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002871 }
2872 /*
2873 * a) Release the persistent reservation, if any;
2874 */
2875 spin_lock(&dev->dev_reservation_lock);
2876 pr_res_holder = dev->dev_pr_res_holder;
2877 if (pr_res_holder) {
2878 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2879 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
2880 pr_res_holder, 0);
2881 }
2882 spin_unlock(&dev->dev_reservation_lock);
2883 /*
2884 * b) Remove all registration(s) (see spc4r17 5.7.7);
2885 */
2886 spin_lock(&pr_tmpl->registration_lock);
2887 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2888 &pr_tmpl->registration_list, pr_reg_list) {
2889
2890 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2891 pr_reg_nacl = pr_reg->pr_reg_nacl;
2892 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2893 __core_scsi3_free_registration(dev, pr_reg, NULL,
2894 calling_it_nexus);
2895 /*
2896 * e) Establish a unit attention condition for the initiator
2897 * port associated with every registered I_T nexus other
2898 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2899 * command with CLEAR service action was received, with the
2900 * additional sense code set to RESERVATIONS PREEMPTED.
2901 */
Andy Grover6708bb22011-06-08 10:36:43 -07002902 if (!calling_it_nexus)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002903 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun,
2904 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2905 }
2906 spin_unlock(&pr_tmpl->registration_lock);
2907
Andy Grover6708bb22011-06-08 10:36:43 -07002908 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002909 cmd->se_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002910
2911 if (pr_tmpl->pr_aptpl_active) {
Andy Grover5951146d2011-07-19 10:26:37 +00002912 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002913 pr_debug("SPC-3 PR: Updated APTPL metadata"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002914 " for CLEAR\n");
2915 }
2916
2917 core_scsi3_pr_generation(dev);
2918 return 0;
2919}
2920
2921/*
2922 * Called with struct se_device->dev_reservation_lock held.
2923 */
2924static void __core_scsi3_complete_pro_preempt(
2925 struct se_device *dev,
2926 struct t10_pr_registration *pr_reg,
2927 struct list_head *preempt_and_abort_list,
2928 int type,
2929 int scope,
2930 int abort)
2931{
2932 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
2933 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2934 char i_buf[PR_REG_ISID_ID_LEN];
2935 int prf_isid;
2936
2937 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2938 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2939 PR_REG_ISID_ID_LEN);
2940 /*
2941 * Do an implict RELEASE of the existing reservation.
2942 */
2943 if (dev->dev_pr_res_holder)
2944 __core_scsi3_complete_pro_release(dev, nacl,
2945 dev->dev_pr_res_holder, 0);
2946
2947 dev->dev_pr_res_holder = pr_reg;
2948 pr_reg->pr_res_holder = 1;
2949 pr_reg->pr_res_type = type;
2950 pr_reg->pr_res_scope = scope;
2951
Andy Grover6708bb22011-06-08 10:36:43 -07002952 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002953 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2954 tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2955 core_scsi3_pr_dump_type(type),
2956 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002957 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002958 tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2959 nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
2960 /*
2961 * For PREEMPT_AND_ABORT, add the preempting reservation's
2962 * struct t10_pr_registration to the list that will be compared
2963 * against received CDBs..
2964 */
2965 if (preempt_and_abort_list)
2966 list_add_tail(&pr_reg->pr_reg_abort_list,
2967 preempt_and_abort_list);
2968}
2969
2970static void core_scsi3_release_preempt_and_abort(
2971 struct list_head *preempt_and_abort_list,
2972 struct t10_pr_registration *pr_reg_holder)
2973{
2974 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2975
2976 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2977 pr_reg_abort_list) {
2978
2979 list_del(&pr_reg->pr_reg_abort_list);
2980 if (pr_reg_holder == pr_reg)
2981 continue;
2982 if (pr_reg->pr_res_holder) {
Andy Grover6708bb22011-06-08 10:36:43 -07002983 pr_warn("pr_reg->pr_res_holder still set\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002984 continue;
2985 }
2986
2987 pr_reg->pr_reg_deve = NULL;
2988 pr_reg->pr_reg_nacl = NULL;
2989 kfree(pr_reg->pr_aptpl_buf);
2990 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2991 }
2992}
2993
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002994static int core_scsi3_pro_preempt(
2995 struct se_cmd *cmd,
2996 int type,
2997 int scope,
2998 u64 res_key,
2999 u64 sa_res_key,
3000 int abort)
3001{
Andy Grover5951146d2011-07-19 10:26:37 +00003002 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003003 struct se_node_acl *pr_reg_nacl;
Andy Grovere3d6f902011-07-19 08:55:10 +00003004 struct se_session *se_sess = cmd->se_sess;
Roland Dreierd0f474e2012-01-12 10:41:18 -08003005 LIST_HEAD(preempt_and_abort_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003006 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
Andy Grovere3d6f902011-07-19 08:55:10 +00003007 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003008 u32 pr_res_mapped_lun = 0;
3009 int all_reg = 0, calling_it_nexus = 0, released_regs = 0;
3010 int prh_type = 0, prh_scope = 0, ret;
3011
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003012 if (!se_sess) {
3013 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3014 return -EINVAL;
3015 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003016
Andy Grover5951146d2011-07-19 10:26:37 +00003017 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003018 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07003019 if (!pr_reg_n) {
3020 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003021 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
3022 (abort) ? "_AND_ABORT" : "");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003023 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3024 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003025 }
3026 if (pr_reg_n->pr_res_key != res_key) {
3027 core_scsi3_put_pr_reg(pr_reg_n);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003028 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3029 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003030 }
3031 if (scope != PR_SCOPE_LU_SCOPE) {
Andy Grover6708bb22011-06-08 10:36:43 -07003032 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003033 core_scsi3_put_pr_reg(pr_reg_n);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003034 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3035 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003036 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003037
3038 spin_lock(&dev->dev_reservation_lock);
3039 pr_res_holder = dev->dev_pr_res_holder;
3040 if (pr_res_holder &&
3041 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3042 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
3043 all_reg = 1;
3044
Andy Grover6708bb22011-06-08 10:36:43 -07003045 if (!all_reg && !sa_res_key) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003046 spin_unlock(&dev->dev_reservation_lock);
3047 core_scsi3_put_pr_reg(pr_reg_n);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003048 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3049 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003050 }
3051 /*
3052 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
3053 *
3054 * If the SERVICE ACTION RESERVATION KEY field does not identify a
3055 * persistent reservation holder or there is no persistent reservation
3056 * holder (i.e., there is no persistent reservation), then the device
3057 * server shall perform a preempt by doing the following in an
3058 * uninterrupted series of actions. (See below..)
3059 */
Andy Grover6708bb22011-06-08 10:36:43 -07003060 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003061 /*
3062 * No existing or SA Reservation Key matching reservations..
3063 *
3064 * PROUT SA PREEMPT with All Registrant type reservations are
3065 * allowed to be processed without a matching SA Reservation Key
3066 */
3067 spin_lock(&pr_tmpl->registration_lock);
3068 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3069 &pr_tmpl->registration_list, pr_reg_list) {
3070 /*
3071 * Removing of registrations in non all registrants
3072 * type reservations without a matching SA reservation
3073 * key.
3074 *
3075 * a) Remove the registrations for all I_T nexuses
3076 * specified by the SERVICE ACTION RESERVATION KEY
3077 * field;
3078 * b) Ignore the contents of the SCOPE and TYPE fields;
3079 * c) Process tasks as defined in 5.7.1; and
3080 * d) Establish a unit attention condition for the
3081 * initiator port associated with every I_T nexus
3082 * that lost its registration other than the I_T
3083 * nexus on which the PERSISTENT RESERVE OUT command
3084 * was received, with the additional sense code set
3085 * to REGISTRATIONS PREEMPTED.
3086 */
Andy Grover6708bb22011-06-08 10:36:43 -07003087 if (!all_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003088 if (pr_reg->pr_res_key != sa_res_key)
3089 continue;
3090
3091 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3092 pr_reg_nacl = pr_reg->pr_reg_nacl;
3093 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3094 __core_scsi3_free_registration(dev, pr_reg,
3095 (abort) ? &preempt_and_abort_list :
3096 NULL, calling_it_nexus);
3097 released_regs++;
3098 } else {
3099 /*
3100 * Case for any existing all registrants type
3101 * reservation, follow logic in spc4r17 section
3102 * 5.7.11.4 Preempting, Table 52 and Figure 7.
3103 *
3104 * For a ZERO SA Reservation key, release
3105 * all other registrations and do an implict
3106 * release of active persistent reservation.
3107 *
3108 * For a non-ZERO SA Reservation key, only
3109 * release the matching reservation key from
3110 * registrations.
3111 */
3112 if ((sa_res_key) &&
3113 (pr_reg->pr_res_key != sa_res_key))
3114 continue;
3115
3116 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3117 if (calling_it_nexus)
3118 continue;
3119
3120 pr_reg_nacl = pr_reg->pr_reg_nacl;
3121 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3122 __core_scsi3_free_registration(dev, pr_reg,
3123 (abort) ? &preempt_and_abort_list :
3124 NULL, 0);
3125 released_regs++;
3126 }
Andy Grover6708bb22011-06-08 10:36:43 -07003127 if (!calling_it_nexus)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003128 core_scsi3_ua_allocate(pr_reg_nacl,
3129 pr_res_mapped_lun, 0x2A,
Marco Sanvido9e08e342012-01-03 17:12:57 -08003130 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003131 }
3132 spin_unlock(&pr_tmpl->registration_lock);
3133 /*
3134 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
3135 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
3136 * RESERVATION KEY field to a value that does not match any
3137 * registered reservation key, then the device server shall
3138 * complete the command with RESERVATION CONFLICT status.
3139 */
Andy Grover6708bb22011-06-08 10:36:43 -07003140 if (!released_regs) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003141 spin_unlock(&dev->dev_reservation_lock);
3142 core_scsi3_put_pr_reg(pr_reg_n);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003143 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3144 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003145 }
3146 /*
3147 * For an existing all registrants type reservation
3148 * with a zero SA rservation key, preempt the existing
3149 * reservation with the new PR type and scope.
3150 */
3151 if (pr_res_holder && all_reg && !(sa_res_key)) {
3152 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3153 (abort) ? &preempt_and_abort_list : NULL,
3154 type, scope, abort);
3155
3156 if (abort)
3157 core_scsi3_release_preempt_and_abort(
3158 &preempt_and_abort_list, pr_reg_n);
3159 }
3160 spin_unlock(&dev->dev_reservation_lock);
3161
3162 if (pr_tmpl->pr_aptpl_active) {
Andy Grover5951146d2011-07-19 10:26:37 +00003163 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003164 &pr_reg_n->pr_aptpl_buf[0],
3165 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07003166 if (!ret)
3167 pr_debug("SPC-3 PR: Updated APTPL"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003168 " metadata for PREEMPT%s\n", (abort) ?
3169 "_AND_ABORT" : "");
3170 }
3171
3172 core_scsi3_put_pr_reg(pr_reg_n);
Andy Grover5951146d2011-07-19 10:26:37 +00003173 core_scsi3_pr_generation(cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003174 return 0;
3175 }
3176 /*
3177 * The PREEMPTing SA reservation key matches that of the
3178 * existing persistent reservation, first, we check if
3179 * we are preempting our own reservation.
3180 * From spc4r17, section 5.7.11.4.3 Preempting
3181 * persistent reservations and registration handling
3182 *
3183 * If an all registrants persistent reservation is not
3184 * present, it is not an error for the persistent
3185 * reservation holder to preempt itself (i.e., a
3186 * PERSISTENT RESERVE OUT with a PREEMPT service action
3187 * or a PREEMPT AND ABORT service action with the
3188 * SERVICE ACTION RESERVATION KEY value equal to the
3189 * persistent reservation holder's reservation key that
3190 * is received from the persistent reservation holder).
3191 * In that case, the device server shall establish the
3192 * new persistent reservation and maintain the
3193 * registration.
3194 */
3195 prh_type = pr_res_holder->pr_res_type;
3196 prh_scope = pr_res_holder->pr_res_scope;
3197 /*
3198 * If the SERVICE ACTION RESERVATION KEY field identifies a
3199 * persistent reservation holder (see 5.7.10), the device
3200 * server shall perform a preempt by doing the following as
3201 * an uninterrupted series of actions:
3202 *
3203 * a) Release the persistent reservation for the holder
3204 * identified by the SERVICE ACTION RESERVATION KEY field;
3205 */
3206 if (pr_reg_n != pr_res_holder)
3207 __core_scsi3_complete_pro_release(dev,
3208 pr_res_holder->pr_reg_nacl,
3209 dev->dev_pr_res_holder, 0);
3210 /*
3211 * b) Remove the registrations for all I_T nexuses identified
3212 * by the SERVICE ACTION RESERVATION KEY field, except the
3213 * I_T nexus that is being used for the PERSISTENT RESERVE
3214 * OUT command. If an all registrants persistent reservation
3215 * is present and the SERVICE ACTION RESERVATION KEY field
3216 * is set to zero, then all registrations shall be removed
3217 * except for that of the I_T nexus that is being used for
3218 * the PERSISTENT RESERVE OUT command;
3219 */
3220 spin_lock(&pr_tmpl->registration_lock);
3221 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3222 &pr_tmpl->registration_list, pr_reg_list) {
3223
3224 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3225 if (calling_it_nexus)
3226 continue;
3227
3228 if (pr_reg->pr_res_key != sa_res_key)
3229 continue;
3230
3231 pr_reg_nacl = pr_reg->pr_reg_nacl;
3232 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3233 __core_scsi3_free_registration(dev, pr_reg,
3234 (abort) ? &preempt_and_abort_list : NULL,
3235 calling_it_nexus);
3236 /*
3237 * e) Establish a unit attention condition for the initiator
3238 * port associated with every I_T nexus that lost its
3239 * persistent reservation and/or registration, with the
3240 * additional sense code set to REGISTRATIONS PREEMPTED;
3241 */
3242 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
Marco Sanvido9e08e342012-01-03 17:12:57 -08003243 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003244 }
3245 spin_unlock(&pr_tmpl->registration_lock);
3246 /*
3247 * c) Establish a persistent reservation for the preempting
3248 * I_T nexus using the contents of the SCOPE and TYPE fields;
3249 */
3250 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3251 (abort) ? &preempt_and_abort_list : NULL,
3252 type, scope, abort);
3253 /*
3254 * d) Process tasks as defined in 5.7.1;
3255 * e) See above..
3256 * f) If the type or scope has changed, then for every I_T nexus
3257 * whose reservation key was not removed, except for the I_T
3258 * nexus on which the PERSISTENT RESERVE OUT command was
3259 * received, the device server shall establish a unit
3260 * attention condition for the initiator port associated with
3261 * that I_T nexus, with the additional sense code set to
3262 * RESERVATIONS RELEASED. If the type or scope have not
3263 * changed, then no unit attention condition(s) shall be
3264 * established for this reason.
3265 */
3266 if ((prh_type != type) || (prh_scope != scope)) {
3267 spin_lock(&pr_tmpl->registration_lock);
3268 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3269 &pr_tmpl->registration_list, pr_reg_list) {
3270
3271 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3272 if (calling_it_nexus)
3273 continue;
3274
3275 core_scsi3_ua_allocate(pr_reg->pr_reg_nacl,
3276 pr_reg->pr_res_mapped_lun, 0x2A,
3277 ASCQ_2AH_RESERVATIONS_RELEASED);
3278 }
3279 spin_unlock(&pr_tmpl->registration_lock);
3280 }
3281 spin_unlock(&dev->dev_reservation_lock);
3282 /*
3283 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3284 * All received CDBs for the matching existing reservation and
3285 * registrations undergo ABORT_TASK logic.
3286 *
3287 * From there, core_scsi3_release_preempt_and_abort() will
3288 * release every registration in the list (which have already
3289 * been removed from the primary pr_reg list), except the
3290 * new persistent reservation holder, the calling Initiator Port.
3291 */
3292 if (abort) {
3293 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3294 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3295 pr_reg_n);
3296 }
3297
3298 if (pr_tmpl->pr_aptpl_active) {
Andy Grover5951146d2011-07-19 10:26:37 +00003299 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003300 &pr_reg_n->pr_aptpl_buf[0],
3301 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07003302 if (!ret)
3303 pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003304 "%s\n", (abort) ? "_AND_ABORT" : "");
3305 }
3306
3307 core_scsi3_put_pr_reg(pr_reg_n);
Andy Grover5951146d2011-07-19 10:26:37 +00003308 core_scsi3_pr_generation(cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003309 return 0;
3310}
3311
3312static int core_scsi3_emulate_pro_preempt(
3313 struct se_cmd *cmd,
3314 int type,
3315 int scope,
3316 u64 res_key,
3317 u64 sa_res_key,
3318 int abort)
3319{
3320 int ret = 0;
3321
3322 switch (type) {
3323 case PR_TYPE_WRITE_EXCLUSIVE:
3324 case PR_TYPE_EXCLUSIVE_ACCESS:
3325 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3326 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3327 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3328 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
3329 ret = core_scsi3_pro_preempt(cmd, type, scope,
3330 res_key, sa_res_key, abort);
3331 break;
3332 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003333 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003334 " Type: 0x%02x\n", (abort) ? "_AND_ABORT" : "", type);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003335 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3336 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003337 }
3338
3339 return ret;
3340}
3341
3342
3343static int core_scsi3_emulate_pro_register_and_move(
3344 struct se_cmd *cmd,
3345 u64 res_key,
3346 u64 sa_res_key,
3347 int aptpl,
3348 int unreg)
3349{
Andy Grovere3d6f902011-07-19 08:55:10 +00003350 struct se_session *se_sess = cmd->se_sess;
Andy Grover5951146d2011-07-19 10:26:37 +00003351 struct se_device *dev = cmd->se_dev;
Jörn Engel28168902012-03-15 15:06:58 -04003352 struct se_dev_entry *dest_se_deve = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00003353 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003354 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3355 struct se_port *se_port;
3356 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3357 struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3358 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
Andy Grovere3d6f902011-07-19 08:55:10 +00003359 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003360 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003361 unsigned char *initiator_str;
3362 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
3363 u32 tid_len, tmp_tid_len;
3364 int new_reg = 0, type, scope, ret, matching_iname, prf_isid;
3365 unsigned short rtpi;
3366 unsigned char proto_ident;
3367
Andy Grover6708bb22011-06-08 10:36:43 -07003368 if (!se_sess || !se_lun) {
3369 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003370 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3371 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003372 }
3373 memset(dest_iport, 0, 64);
3374 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3375 se_tpg = se_sess->se_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00003376 tf_ops = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003377 /*
3378 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3379 * Register behaviors for a REGISTER AND MOVE service action
3380 *
3381 * Locate the existing *pr_reg via struct se_node_acl pointers
3382 */
Andy Grover5951146d2011-07-19 10:26:37 +00003383 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003384 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07003385 if (!pr_reg) {
3386 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003387 " *pr_reg for REGISTER_AND_MOVE\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003388 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3389 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003390 }
3391 /*
3392 * The provided reservation key much match the existing reservation key
3393 * provided during this initiator's I_T nexus registration.
3394 */
3395 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07003396 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003397 " res_key: 0x%016Lx does not match existing SA REGISTER"
3398 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
3399 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003400 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3401 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003402 }
3403 /*
3404 * The service active reservation key needs to be non zero
3405 */
Andy Grover6708bb22011-06-08 10:36:43 -07003406 if (!sa_res_key) {
3407 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003408 " sa_res_key\n");
3409 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003410 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3411 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003412 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003413
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003414 /*
3415 * Determine the Relative Target Port Identifier where the reservation
3416 * will be moved to for the TransportID containing SCSI initiator WWN
3417 * information.
3418 */
Andy Grover49493142012-01-16 16:57:08 -08003419 buf = transport_kmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003420 rtpi = (buf[18] & 0xff) << 8;
3421 rtpi |= buf[19] & 0xff;
3422 tid_len = (buf[20] & 0xff) << 24;
3423 tid_len |= (buf[21] & 0xff) << 16;
3424 tid_len |= (buf[22] & 0xff) << 8;
3425 tid_len |= buf[23] & 0xff;
Andy Grover49493142012-01-16 16:57:08 -08003426 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003427 buf = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003428
3429 if ((tid_len + 24) != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07003430 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003431 " does not equal CDB data_length: %u\n", tid_len,
3432 cmd->data_length);
3433 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003434 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3435 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003436 }
3437
3438 spin_lock(&dev->se_port_lock);
3439 list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) {
3440 if (se_port->sep_rtpi != rtpi)
3441 continue;
3442 dest_se_tpg = se_port->sep_tpg;
Andy Grover6708bb22011-06-08 10:36:43 -07003443 if (!dest_se_tpg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003444 continue;
Andy Grovere3d6f902011-07-19 08:55:10 +00003445 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
Andy Grover6708bb22011-06-08 10:36:43 -07003446 if (!dest_tf_ops)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003447 continue;
3448
3449 atomic_inc(&dest_se_tpg->tpg_pr_ref_count);
3450 smp_mb__after_atomic_inc();
3451 spin_unlock(&dev->se_port_lock);
3452
3453 ret = core_scsi3_tpg_depend_item(dest_se_tpg);
3454 if (ret != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003455 pr_err("core_scsi3_tpg_depend_item() failed"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003456 " for dest_se_tpg\n");
3457 atomic_dec(&dest_se_tpg->tpg_pr_ref_count);
3458 smp_mb__after_atomic_dec();
3459 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003460 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3461 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003462 }
3463
3464 spin_lock(&dev->se_port_lock);
3465 break;
3466 }
3467 spin_unlock(&dev->se_port_lock);
3468
Andy Grover6708bb22011-06-08 10:36:43 -07003469 if (!dest_se_tpg || !dest_tf_ops) {
3470 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003471 " fabric ops from Relative Target Port Identifier:"
3472 " %hu\n", rtpi);
3473 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003474 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3475 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003476 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003477
Andy Grover49493142012-01-16 16:57:08 -08003478 buf = transport_kmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003479 proto_ident = (buf[24] & 0x0f);
Andy Grover8b1e1242012-04-03 15:51:12 -07003480
Andy Grover6708bb22011-06-08 10:36:43 -07003481 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003482 " 0x%02x\n", proto_ident);
Andy Grover8b1e1242012-04-03 15:51:12 -07003483
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003484 if (proto_ident != dest_tf_ops->get_fabric_proto_ident(dest_se_tpg)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003485 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003486 " proto_ident: 0x%02x does not match ident: 0x%02x"
3487 " from fabric: %s\n", proto_ident,
3488 dest_tf_ops->get_fabric_proto_ident(dest_se_tpg),
3489 dest_tf_ops->get_fabric_name());
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003490 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3491 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003492 goto out;
3493 }
3494 if (dest_tf_ops->tpg_parse_pr_out_transport_id == NULL) {
Andy Grover6708bb22011-06-08 10:36:43 -07003495 pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003496 " containg a valid tpg_parse_pr_out_transport_id"
3497 " function pointer\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003498 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3499 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003500 goto out;
3501 }
3502 initiator_str = dest_tf_ops->tpg_parse_pr_out_transport_id(dest_se_tpg,
3503 (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07003504 if (!initiator_str) {
3505 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003506 " initiator_str from Transport ID\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003507 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3508 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003509 goto out;
3510 }
3511
Andy Grover49493142012-01-16 16:57:08 -08003512 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003513 buf = NULL;
3514
Andy Grover6708bb22011-06-08 10:36:43 -07003515 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003516 " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
3517 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3518 iport_ptr : "");
3519 /*
3520 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3521 * action specifies a TransportID that is the same as the initiator port
3522 * of the I_T nexus for the command received, then the command shall
3523 * be terminated with CHECK CONDITION status, with the sense key set to
3524 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3525 * IN PARAMETER LIST.
3526 */
3527 pr_reg_nacl = pr_reg->pr_reg_nacl;
3528 matching_iname = (!strcmp(initiator_str,
3529 pr_reg_nacl->initiatorname)) ? 1 : 0;
Andy Grover6708bb22011-06-08 10:36:43 -07003530 if (!matching_iname)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003531 goto after_iport_check;
3532
Andy Grover6708bb22011-06-08 10:36:43 -07003533 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3534 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003535 " matches: %s on received I_T Nexus\n", initiator_str,
3536 pr_reg_nacl->initiatorname);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003537 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3538 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003539 goto out;
3540 }
Andy Grover6708bb22011-06-08 10:36:43 -07003541 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3542 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003543 " matches: %s %s on received I_T Nexus\n",
3544 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3545 pr_reg->pr_reg_isid);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003546 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3547 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003548 goto out;
3549 }
3550after_iport_check:
3551 /*
3552 * Locate the destination struct se_node_acl from the received Transport ID
3553 */
Roland Dreier286388872011-08-16 09:40:01 -07003554 spin_lock_irq(&dest_se_tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003555 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3556 initiator_str);
3557 if (dest_node_acl) {
3558 atomic_inc(&dest_node_acl->acl_pr_ref_count);
3559 smp_mb__after_atomic_inc();
3560 }
Roland Dreier286388872011-08-16 09:40:01 -07003561 spin_unlock_irq(&dest_se_tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003562
Andy Grover6708bb22011-06-08 10:36:43 -07003563 if (!dest_node_acl) {
3564 pr_err("Unable to locate %s dest_node_acl for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003565 " TransportID%s\n", dest_tf_ops->get_fabric_name(),
3566 initiator_str);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003567 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3568 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003569 goto out;
3570 }
3571 ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
3572 if (ret != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003573 pr_err("core_scsi3_nodeacl_depend_item() for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003574 " dest_node_acl\n");
3575 atomic_dec(&dest_node_acl->acl_pr_ref_count);
3576 smp_mb__after_atomic_dec();
3577 dest_node_acl = NULL;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003578 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3579 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003580 goto out;
3581 }
Andy Grover8b1e1242012-04-03 15:51:12 -07003582
Andy Grover6708bb22011-06-08 10:36:43 -07003583 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003584 " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
3585 dest_node_acl->initiatorname);
Andy Grover8b1e1242012-04-03 15:51:12 -07003586
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003587 /*
3588 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3589 * PORT IDENTIFIER.
3590 */
3591 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
Andy Grover6708bb22011-06-08 10:36:43 -07003592 if (!dest_se_deve) {
3593 pr_err("Unable to locate %s dest_se_deve from RTPI:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003594 " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003595 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3596 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003597 goto out;
3598 }
3599
3600 ret = core_scsi3_lunacl_depend_item(dest_se_deve);
3601 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003602 pr_err("core_scsi3_lunacl_depend_item() failed\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003603 atomic_dec(&dest_se_deve->pr_ref_count);
3604 smp_mb__after_atomic_dec();
3605 dest_se_deve = NULL;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003606 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3607 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003608 goto out;
3609 }
Andy Grover8b1e1242012-04-03 15:51:12 -07003610
Andy Grover6708bb22011-06-08 10:36:43 -07003611 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003612 " ACL for dest_se_deve->mapped_lun: %u\n",
3613 dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
3614 dest_se_deve->mapped_lun);
Andy Grover8b1e1242012-04-03 15:51:12 -07003615
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003616 /*
3617 * A persistent reservation needs to already existing in order to
3618 * successfully complete the REGISTER_AND_MOVE service action..
3619 */
3620 spin_lock(&dev->dev_reservation_lock);
3621 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07003622 if (!pr_res_holder) {
3623 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003624 " currently held\n");
3625 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003626 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3627 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003628 goto out;
3629 }
3630 /*
3631 * The received on I_T Nexus must be the reservation holder.
3632 *
3633 * From spc4r17 section 5.7.8 Table 50 --
3634 * Register behaviors for a REGISTER AND MOVE service action
3635 */
3636 if (pr_res_holder != pr_reg) {
Andy Grover6708bb22011-06-08 10:36:43 -07003637 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003638 " Nexus is not reservation holder\n");
3639 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003640 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3641 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003642 goto out;
3643 }
3644 /*
3645 * From spc4r17 section 5.7.8: registering and moving reservation
3646 *
3647 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3648 * action is received and the established persistent reservation is a
3649 * Write Exclusive - All Registrants type or Exclusive Access -
3650 * All Registrants type reservation, then the command shall be completed
3651 * with RESERVATION CONFLICT status.
3652 */
3653 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3654 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003655 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003656 " reservation for type: %s\n",
3657 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3658 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003659 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3660 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003661 goto out;
3662 }
3663 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3664 /*
3665 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3666 */
3667 type = pr_res_holder->pr_res_type;
3668 scope = pr_res_holder->pr_res_type;
3669 /*
3670 * c) Associate the reservation key specified in the SERVICE ACTION
3671 * RESERVATION KEY field with the I_T nexus specified as the
3672 * destination of the register and move, where:
3673 * A) The I_T nexus is specified by the TransportID and the
3674 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3675 * B) Regardless of the TransportID format used, the association for
3676 * the initiator port is based on either the initiator port name
3677 * (see 3.1.71) on SCSI transport protocols where port names are
3678 * required or the initiator port identifier (see 3.1.70) on SCSI
3679 * transport protocols where port names are not required;
3680 * d) Register the reservation key specified in the SERVICE ACTION
3681 * RESERVATION KEY field;
3682 * e) Retain the reservation key specified in the SERVICE ACTION
3683 * RESERVATION KEY field and associated information;
3684 *
3685 * Also, It is not an error for a REGISTER AND MOVE service action to
3686 * register an I_T nexus that is already registered with the same
3687 * reservation key or a different reservation key.
3688 */
3689 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3690 iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07003691 if (!dest_pr_reg) {
Andy Grover5951146d2011-07-19 10:26:37 +00003692 ret = core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003693 dest_node_acl, dest_se_deve, iport_ptr,
3694 sa_res_key, 0, aptpl, 2, 1);
3695 if (ret != 0) {
3696 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003697 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3698 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003699 goto out;
3700 }
3701 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3702 iport_ptr);
3703 new_reg = 1;
3704 }
3705 /*
3706 * f) Release the persistent reservation for the persistent reservation
3707 * holder (i.e., the I_T nexus on which the
3708 */
3709 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
3710 dev->dev_pr_res_holder, 0);
3711 /*
3712 * g) Move the persistent reservation to the specified I_T nexus using
3713 * the same scope and type as the persistent reservation released in
3714 * item f); and
3715 */
3716 dev->dev_pr_res_holder = dest_pr_reg;
3717 dest_pr_reg->pr_res_holder = 1;
3718 dest_pr_reg->pr_res_type = type;
3719 pr_reg->pr_res_scope = scope;
3720 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
3721 PR_REG_ISID_ID_LEN);
3722 /*
3723 * Increment PRGeneration for existing registrations..
3724 */
Andy Grover6708bb22011-06-08 10:36:43 -07003725 if (!new_reg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003726 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3727 spin_unlock(&dev->dev_reservation_lock);
3728
Andy Grover6708bb22011-06-08 10:36:43 -07003729 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003730 " created new reservation holder TYPE: %s on object RTPI:"
3731 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
3732 core_scsi3_pr_dump_type(type), rtpi,
3733 dest_pr_reg->pr_res_generation);
Andy Grover6708bb22011-06-08 10:36:43 -07003734 pr_debug("SPC-3 PR Successfully moved reservation from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003735 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3736 tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
3737 (prf_isid) ? &i_buf[0] : "", dest_tf_ops->get_fabric_name(),
3738 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3739 iport_ptr : "");
3740 /*
3741 * It is now safe to release configfs group dependencies for destination
3742 * of Transport ID Initiator Device/Port Identifier
3743 */
3744 core_scsi3_lunacl_undepend_item(dest_se_deve);
3745 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3746 core_scsi3_tpg_undepend_item(dest_se_tpg);
3747 /*
3748 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3749 * nexus on which PERSISTENT RESERVE OUT command was received.
3750 */
3751 if (unreg) {
3752 spin_lock(&pr_tmpl->registration_lock);
3753 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3754 spin_unlock(&pr_tmpl->registration_lock);
3755 } else
3756 core_scsi3_put_pr_reg(pr_reg);
3757
3758 /*
3759 * Clear the APTPL metadata if APTPL has been disabled, otherwise
3760 * write out the updated metadata to struct file for this SCSI device.
3761 */
Andy Grover6708bb22011-06-08 10:36:43 -07003762 if (!aptpl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003763 pr_tmpl->pr_aptpl_active = 0;
Andy Grover5951146d2011-07-19 10:26:37 +00003764 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07003765 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003766 " REGISTER_AND_MOVE\n");
3767 } else {
3768 pr_tmpl->pr_aptpl_active = 1;
Andy Grover5951146d2011-07-19 10:26:37 +00003769 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003770 &dest_pr_reg->pr_aptpl_buf[0],
3771 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07003772 if (!ret)
3773 pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003774 " REGISTER_AND_MOVE\n");
3775 }
3776
Andy Grover49493142012-01-16 16:57:08 -08003777 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003778
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003779 core_scsi3_put_pr_reg(dest_pr_reg);
3780 return 0;
3781out:
Andy Grover05d1c7c2011-07-20 19:13:28 +00003782 if (buf)
Andy Grover49493142012-01-16 16:57:08 -08003783 transport_kunmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003784 if (dest_se_deve)
3785 core_scsi3_lunacl_undepend_item(dest_se_deve);
3786 if (dest_node_acl)
3787 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3788 core_scsi3_tpg_undepend_item(dest_se_tpg);
3789 core_scsi3_put_pr_reg(pr_reg);
3790 return ret;
3791}
3792
3793static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
3794{
3795 unsigned int __v1, __v2;
3796
3797 __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
3798 __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
3799
3800 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
3801}
3802
3803/*
3804 * See spc4r17 section 6.14 Table 170
3805 */
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04003806int target_scsi3_emulate_pr_out(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003807{
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003808 unsigned char *cdb = &cmd->t_task_cdb[0];
Andy Grover05d1c7c2011-07-20 19:13:28 +00003809 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003810 u64 res_key, sa_res_key;
3811 int sa, scope, type, aptpl;
3812 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003813 int ret;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003814
3815 /*
3816 * Following spc2r20 5.5.1 Reservations overview:
3817 *
3818 * If a logical unit has been reserved by any RESERVE command and is
3819 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3820 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3821 * initiator or service action and shall terminate with a RESERVATION
3822 * CONFLICT status.
3823 */
3824 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
3825 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3826 " SPC-2 reservation is held, returning"
3827 " RESERVATION_CONFLICT\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003828 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
Roland Dreierd35212f2012-07-16 15:17:10 -07003829 ret = -EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003830 goto out;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003831 }
3832
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003833 /*
3834 * FIXME: A NULL struct se_session pointer means an this is not coming from
3835 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3836 */
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003837 if (!cmd->se_sess) {
3838 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Roland Dreierd35212f2012-07-16 15:17:10 -07003839 ret = -EINVAL;
3840 goto out;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003841 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003842
3843 if (cmd->data_length < 24) {
Andy Grover6708bb22011-06-08 10:36:43 -07003844 pr_warn("SPC-PR: Received PR OUT parameter list"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003845 " length too small: %u\n", cmd->data_length);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003846 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3847 ret = -EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003848 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003849 }
3850 /*
3851 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3852 */
3853 sa = (cdb[1] & 0x1f);
3854 scope = (cdb[2] & 0xf0);
3855 type = (cdb[2] & 0x0f);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003856
Andy Grover49493142012-01-16 16:57:08 -08003857 buf = transport_kmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003858 /*
3859 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3860 */
3861 res_key = core_scsi3_extract_reservation_key(&buf[0]);
3862 sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
3863 /*
3864 * REGISTER_AND_MOVE uses a different SA parameter list containing
3865 * SCSI TransportIDs.
3866 */
3867 if (sa != PRO_REGISTER_AND_MOVE) {
3868 spec_i_pt = (buf[20] & 0x08);
3869 all_tg_pt = (buf[20] & 0x04);
3870 aptpl = (buf[20] & 0x01);
3871 } else {
3872 aptpl = (buf[17] & 0x01);
3873 unreg = (buf[17] & 0x02);
3874 }
Andy Grover49493142012-01-16 16:57:08 -08003875 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003876 buf = NULL;
3877
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003878 /*
3879 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3880 */
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003881 if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER)) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003882 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3883 ret = -EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003884 goto out;
3885 }
3886
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003887 /*
3888 * From spc4r17 section 6.14:
3889 *
3890 * If the SPEC_I_PT bit is set to zero, the service action is not
3891 * REGISTER AND MOVE, and the parameter list length is not 24, then
3892 * the command shall be terminated with CHECK CONDITION status, with
3893 * the sense key set to ILLEGAL REQUEST, and the additional sense
3894 * code set to PARAMETER LIST LENGTH ERROR.
3895 */
Andy Grover6708bb22011-06-08 10:36:43 -07003896 if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003897 (cmd->data_length != 24)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003898 pr_warn("SPC-PR: Received PR OUT illegal parameter"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003899 " list length: %u\n", cmd->data_length);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003900 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3901 ret = -EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003902 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003903 }
3904 /*
3905 * (core_scsi3_emulate_pro_* function parameters
3906 * are defined by spc4r17 Table 174:
3907 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3908 */
3909 switch (sa) {
3910 case PRO_REGISTER:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003911 ret = core_scsi3_emulate_pro_register(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003912 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 0);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003913 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003914 case PRO_RESERVE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003915 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3916 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003917 case PRO_RELEASE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003918 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3919 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003920 case PRO_CLEAR:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003921 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3922 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003923 case PRO_PREEMPT:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003924 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003925 res_key, sa_res_key, 0);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003926 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003927 case PRO_PREEMPT_AND_ABORT:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003928 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003929 res_key, sa_res_key, 1);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003930 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003931 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003932 ret = core_scsi3_emulate_pro_register(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003933 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 1);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003934 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003935 case PRO_REGISTER_AND_MOVE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003936 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003937 sa_res_key, aptpl, unreg);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003938 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003939 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003940 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003941 " action: 0x%02x\n", cdb[1] & 0x1f);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003942 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3943 ret = -EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003944 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003945 }
3946
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003947out:
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04003948 if (!ret)
3949 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003950 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003951}
3952
3953/*
3954 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3955 *
3956 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3957 */
3958static int core_scsi3_pri_read_keys(struct se_cmd *cmd)
3959{
Andy Grover5951146d2011-07-19 10:26:37 +00003960 struct se_device *se_dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00003961 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003962 struct t10_pr_registration *pr_reg;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003963 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003964 u32 add_len = 0, off = 8;
3965
3966 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07003967 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003968 " too small\n", cmd->data_length);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003969 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3970 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003971 }
3972
Andy Grover49493142012-01-16 16:57:08 -08003973 buf = transport_kmap_data_sg(cmd);
Andy Grovere3d6f902011-07-19 08:55:10 +00003974 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
3975 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
3976 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
3977 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003978
Andy Grovere3d6f902011-07-19 08:55:10 +00003979 spin_lock(&su_dev->t10_pr.registration_lock);
3980 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003981 pr_reg_list) {
3982 /*
3983 * Check for overflow of 8byte PRI READ_KEYS payload and
3984 * next reservation key list descriptor.
3985 */
3986 if ((add_len + 8) > (cmd->data_length - 8))
3987 break;
3988
3989 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3990 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3991 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3992 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
3993 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
3994 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
3995 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
3996 buf[off++] = (pr_reg->pr_res_key & 0xff);
3997
3998 add_len += 8;
3999 }
Andy Grovere3d6f902011-07-19 08:55:10 +00004000 spin_unlock(&su_dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004001
4002 buf[4] = ((add_len >> 24) & 0xff);
4003 buf[5] = ((add_len >> 16) & 0xff);
4004 buf[6] = ((add_len >> 8) & 0xff);
4005 buf[7] = (add_len & 0xff);
4006
Andy Grover49493142012-01-16 16:57:08 -08004007 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00004008
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004009 return 0;
4010}
4011
4012/*
4013 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
4014 *
4015 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
4016 */
4017static int core_scsi3_pri_read_reservation(struct se_cmd *cmd)
4018{
Andy Grover5951146d2011-07-19 10:26:37 +00004019 struct se_device *se_dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00004020 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004021 struct t10_pr_registration *pr_reg;
Andy Grover05d1c7c2011-07-20 19:13:28 +00004022 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004023 u64 pr_res_key;
4024 u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
4025
4026 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07004027 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004028 " too small\n", cmd->data_length);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004029 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4030 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004031 }
4032
Andy Grover49493142012-01-16 16:57:08 -08004033 buf = transport_kmap_data_sg(cmd);
Andy Grovere3d6f902011-07-19 08:55:10 +00004034 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
4035 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
4036 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
4037 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004038
4039 spin_lock(&se_dev->dev_reservation_lock);
4040 pr_reg = se_dev->dev_pr_res_holder;
Andy Groveree1b1b92012-07-12 17:34:54 -07004041 if (pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004042 /*
4043 * Set the hardcoded Additional Length
4044 */
4045 buf[4] = ((add_len >> 24) & 0xff);
4046 buf[5] = ((add_len >> 16) & 0xff);
4047 buf[6] = ((add_len >> 8) & 0xff);
4048 buf[7] = (add_len & 0xff);
4049
Andy Grover05d1c7c2011-07-20 19:13:28 +00004050 if (cmd->data_length < 22)
4051 goto err;
4052
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004053 /*
4054 * Set the Reservation key.
4055 *
4056 * From spc4r17, section 5.7.10:
4057 * A persistent reservation holder has its reservation key
4058 * returned in the parameter data from a PERSISTENT
4059 * RESERVE IN command with READ RESERVATION service action as
4060 * follows:
4061 * a) For a persistent reservation of the type Write Exclusive
4062 * - All Registrants or Exclusive Access ­ All Regitrants,
4063 * the reservation key shall be set to zero; or
4064 * b) For all other persistent reservation types, the
4065 * reservation key shall be set to the registered
4066 * reservation key for the I_T nexus that holds the
4067 * persistent reservation.
4068 */
4069 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
4070 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
4071 pr_res_key = 0;
4072 else
4073 pr_res_key = pr_reg->pr_res_key;
4074
4075 buf[8] = ((pr_res_key >> 56) & 0xff);
4076 buf[9] = ((pr_res_key >> 48) & 0xff);
4077 buf[10] = ((pr_res_key >> 40) & 0xff);
4078 buf[11] = ((pr_res_key >> 32) & 0xff);
4079 buf[12] = ((pr_res_key >> 24) & 0xff);
4080 buf[13] = ((pr_res_key >> 16) & 0xff);
4081 buf[14] = ((pr_res_key >> 8) & 0xff);
4082 buf[15] = (pr_res_key & 0xff);
4083 /*
4084 * Set the SCOPE and TYPE
4085 */
4086 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
4087 (pr_reg->pr_res_type & 0x0f);
4088 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00004089
4090err:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004091 spin_unlock(&se_dev->dev_reservation_lock);
Andy Grover49493142012-01-16 16:57:08 -08004092 transport_kunmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004093
4094 return 0;
4095}
4096
4097/*
4098 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
4099 *
4100 * See spc4r17 section 6.13.4 Table 165
4101 */
4102static int core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
4103{
Andy Grover5951146d2011-07-19 10:26:37 +00004104 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00004105 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00004106 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004107 u16 add_len = 8; /* Hardcoded to 8. */
4108
4109 if (cmd->data_length < 6) {
Andy Grover6708bb22011-06-08 10:36:43 -07004110 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004111 " %u too small\n", cmd->data_length);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004112 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4113 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004114 }
4115
Andy Grover49493142012-01-16 16:57:08 -08004116 buf = transport_kmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00004117
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004118 buf[0] = ((add_len << 8) & 0xff);
4119 buf[1] = (add_len & 0xff);
4120 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
4121 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
4122 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
4123 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
4124 /*
4125 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
4126 * set the TMV: Task Mask Valid bit.
4127 */
4128 buf[3] |= 0x80;
4129 /*
4130 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
4131 */
4132 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
4133 /*
4134 * PTPL_A: Persistence across Target Power Loss Active bit
4135 */
4136 if (pr_tmpl->pr_aptpl_active)
4137 buf[3] |= 0x01;
4138 /*
4139 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
4140 */
4141 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4142 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
4143 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
4144 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
4145 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
4146 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4147
Andy Grover49493142012-01-16 16:57:08 -08004148 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00004149
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004150 return 0;
4151}
4152
4153/*
4154 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
4155 *
4156 * See spc4r17 section 6.13.5 Table 168 and 169
4157 */
4158static int core_scsi3_pri_read_full_status(struct se_cmd *cmd)
4159{
Andy Grover5951146d2011-07-19 10:26:37 +00004160 struct se_device *se_dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004161 struct se_node_acl *se_nacl;
Andy Grovere3d6f902011-07-19 08:55:10 +00004162 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004163 struct se_portal_group *se_tpg;
4164 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
Andy Grovere3d6f902011-07-19 08:55:10 +00004165 struct t10_reservation *pr_tmpl = &se_dev->se_sub_dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00004166 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004167 u32 add_desc_len = 0, add_len = 0, desc_len, exp_desc_len;
4168 u32 off = 8; /* off into first Full Status descriptor */
4169 int format_code = 0;
4170
4171 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07004172 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004173 " too small\n", cmd->data_length);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004174 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4175 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004176 }
4177
Andy Grover49493142012-01-16 16:57:08 -08004178 buf = transport_kmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00004179
Andy Grovere3d6f902011-07-19 08:55:10 +00004180 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
4181 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
4182 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
4183 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004184
4185 spin_lock(&pr_tmpl->registration_lock);
4186 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
4187 &pr_tmpl->registration_list, pr_reg_list) {
4188
4189 se_nacl = pr_reg->pr_reg_nacl;
4190 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
4191 add_desc_len = 0;
4192
4193 atomic_inc(&pr_reg->pr_res_holders);
4194 smp_mb__after_atomic_inc();
4195 spin_unlock(&pr_tmpl->registration_lock);
4196 /*
4197 * Determine expected length of $FABRIC_MOD specific
4198 * TransportID full status descriptor..
4199 */
Andy Grovere3d6f902011-07-19 08:55:10 +00004200 exp_desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id_len(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004201 se_tpg, se_nacl, pr_reg, &format_code);
4202
4203 if ((exp_desc_len + add_len) > cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07004204 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004205 " out of buffer: %d\n", cmd->data_length);
4206 spin_lock(&pr_tmpl->registration_lock);
4207 atomic_dec(&pr_reg->pr_res_holders);
4208 smp_mb__after_atomic_dec();
4209 break;
4210 }
4211 /*
4212 * Set RESERVATION KEY
4213 */
4214 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
4215 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
4216 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
4217 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
4218 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
4219 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
4220 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
4221 buf[off++] = (pr_reg->pr_res_key & 0xff);
4222 off += 4; /* Skip Over Reserved area */
4223
4224 /*
4225 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4226 */
4227 if (pr_reg->pr_reg_all_tg_pt)
4228 buf[off] = 0x02;
4229 /*
4230 * The struct se_lun pointer will be present for the
4231 * reservation holder for PR_HOLDER bit.
4232 *
4233 * Also, if this registration is the reservation
4234 * holder, fill in SCOPE and TYPE in the next byte.
4235 */
4236 if (pr_reg->pr_res_holder) {
4237 buf[off++] |= 0x01;
4238 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
4239 (pr_reg->pr_res_type & 0x0f);
4240 } else
4241 off += 2;
4242
4243 off += 4; /* Skip over reserved area */
4244 /*
4245 * From spc4r17 6.3.15:
4246 *
4247 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4248 * IDENTIFIER field contains the relative port identifier (see
4249 * 3.1.120) of the target port that is part of the I_T nexus
4250 * described by this full status descriptor. If the ALL_TG_PT
4251 * bit is set to one, the contents of the RELATIVE TARGET PORT
4252 * IDENTIFIER field are not defined by this standard.
4253 */
Andy Grover6708bb22011-06-08 10:36:43 -07004254 if (!pr_reg->pr_reg_all_tg_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004255 struct se_port *port = pr_reg->pr_reg_tg_pt_lun->lun_sep;
4256
4257 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
4258 buf[off++] = (port->sep_rtpi & 0xff);
4259 } else
Masanari Iida35d1efe2012-08-16 22:43:13 +09004260 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004261
4262 /*
4263 * Now, have the $FABRIC_MOD fill in the protocol identifier
4264 */
Andy Grovere3d6f902011-07-19 08:55:10 +00004265 desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id(se_tpg,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004266 se_nacl, pr_reg, &format_code, &buf[off+4]);
4267
4268 spin_lock(&pr_tmpl->registration_lock);
4269 atomic_dec(&pr_reg->pr_res_holders);
4270 smp_mb__after_atomic_dec();
4271 /*
4272 * Set the ADDITIONAL DESCRIPTOR LENGTH
4273 */
4274 buf[off++] = ((desc_len >> 24) & 0xff);
4275 buf[off++] = ((desc_len >> 16) & 0xff);
4276 buf[off++] = ((desc_len >> 8) & 0xff);
4277 buf[off++] = (desc_len & 0xff);
4278 /*
4279 * Size of full desctipor header minus TransportID
4280 * containing $FABRIC_MOD specific) initiator device/port
4281 * WWN information.
4282 *
4283 * See spc4r17 Section 6.13.5 Table 169
4284 */
4285 add_desc_len = (24 + desc_len);
4286
4287 off += desc_len;
4288 add_len += add_desc_len;
4289 }
4290 spin_unlock(&pr_tmpl->registration_lock);
4291 /*
4292 * Set ADDITIONAL_LENGTH
4293 */
4294 buf[4] = ((add_len >> 24) & 0xff);
4295 buf[5] = ((add_len >> 16) & 0xff);
4296 buf[6] = ((add_len >> 8) & 0xff);
4297 buf[7] = (add_len & 0xff);
4298
Andy Grover49493142012-01-16 16:57:08 -08004299 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00004300
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004301 return 0;
4302}
4303
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04004304int target_scsi3_emulate_pr_in(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004305{
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004306 int ret;
Christoph Hellwige76a35d2011-11-03 17:50:42 -04004307
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004308 /*
4309 * Following spc2r20 5.5.1 Reservations overview:
4310 *
4311 * If a logical unit has been reserved by any RESERVE command and is
4312 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4313 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4314 * initiator or service action and shall terminate with a RESERVATION
4315 * CONFLICT status.
4316 */
4317 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
4318 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4319 " SPC-2 reservation is held, returning"
4320 " RESERVATION_CONFLICT\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004321 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
4322 return -EINVAL;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004323 }
4324
4325 switch (cmd->t_task_cdb[1] & 0x1f) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004326 case PRI_READ_KEYS:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004327 ret = core_scsi3_pri_read_keys(cmd);
4328 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004329 case PRI_READ_RESERVATION:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004330 ret = core_scsi3_pri_read_reservation(cmd);
4331 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004332 case PRI_REPORT_CAPABILITIES:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004333 ret = core_scsi3_pri_report_capabilities(cmd);
4334 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004335 case PRI_READ_FULL_STATUS:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004336 ret = core_scsi3_pri_read_full_status(cmd);
4337 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004338 default:
Andy Grover6708bb22011-06-08 10:36:43 -07004339 pr_err("Unknown PERSISTENT_RESERVE_IN service"
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004340 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004341 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4342 ret = -EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004343 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004344 }
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004345
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04004346 if (!ret)
4347 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004348 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004349}
4350
4351static int core_pt_reservation_check(struct se_cmd *cmd, u32 *pr_res_type)
4352{
4353 return 0;
4354}
4355
4356static int core_pt_seq_non_holder(
4357 struct se_cmd *cmd,
4358 unsigned char *cdb,
4359 u32 pr_reg_type)
4360{
4361 return 0;
4362}
4363
4364int core_setup_reservations(struct se_device *dev, int force_pt)
4365{
4366 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00004367 struct t10_reservation *rest = &su_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004368 /*
4369 * If this device is from Target_Core_Mod/pSCSI, use the reservations
4370 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
4371 * cause a problem because libata and some SATA RAID HBAs appear
4372 * under Linux/SCSI, but to emulate reservations themselves.
4373 */
Andy Grovere3d6f902011-07-19 08:55:10 +00004374 if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
4375 !(dev->se_sub_dev->se_dev_attrib.emulate_reservations)) || force_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004376 rest->res_type = SPC_PASSTHROUGH;
4377 rest->pr_ops.t10_reservation_check = &core_pt_reservation_check;
4378 rest->pr_ops.t10_seq_non_holder = &core_pt_seq_non_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07004379 pr_debug("%s: Using SPC_PASSTHROUGH, no reservation"
Andy Grovere3d6f902011-07-19 08:55:10 +00004380 " emulation\n", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004381 return 0;
4382 }
4383 /*
4384 * If SPC-3 or above is reported by real or emulated struct se_device,
4385 * use emulated Persistent Reservations.
4386 */
Andy Grovere3d6f902011-07-19 08:55:10 +00004387 if (dev->transport->get_device_rev(dev) >= SCSI_3) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004388 rest->res_type = SPC3_PERSISTENT_RESERVATIONS;
4389 rest->pr_ops.t10_reservation_check = &core_scsi3_pr_reservation_check;
4390 rest->pr_ops.t10_seq_non_holder = &core_scsi3_pr_seq_non_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07004391 pr_debug("%s: Using SPC3_PERSISTENT_RESERVATIONS"
Andy Grovere3d6f902011-07-19 08:55:10 +00004392 " emulation\n", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004393 } else {
4394 rest->res_type = SPC2_RESERVATIONS;
4395 rest->pr_ops.t10_reservation_check = &core_scsi2_reservation_check;
4396 rest->pr_ops.t10_seq_non_holder =
4397 &core_scsi2_reservation_seq_non_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07004398 pr_debug("%s: Using SPC2_RESERVATIONS emulation\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004399 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004400 }
4401
4402 return 0;
4403}