blob: 8c323a98c4a02bbf7773384f87b885b737d1124a [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;
Wei Yongjun609234e2012-09-07 14:56:28 +0800200 struct se_portal_group *tpg;
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700201 int ret = 0, rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800202
Wei Yongjun609234e2012-09-07 14:56:28 +0800203 if (!sess || !sess->se_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 }
Wei Yongjun609234e2012-09-07 14:56:28 +0800231 tpg = sess->se_tpg;
Andy Grover6708bb22011-06-08 10:36:43 -0700232 pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
Andy Grovere3d6f902011-07-19 08:55:10 +0000233 " MAPPED LUN: %u for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
234 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800235 sess->se_node_acl->initiatorname);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800236
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400237out_unlock:
238 spin_unlock(&dev->dev_reservation_lock);
239out:
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400240 if (!ret)
241 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400242 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800243}
244
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400245int target_scsi2_reservation_reserve(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800246{
247 struct se_device *dev = cmd->se_dev;
248 struct se_session *sess = cmd->se_sess;
Wei Yongjun609234e2012-09-07 14:56:28 +0800249 struct se_portal_group *tpg;
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700250 int ret = 0, rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800251
Andy Grovera1d8b492011-05-02 17:12:10 -0700252 if ((cmd->t_task_cdb[1] & 0x01) &&
253 (cmd->t_task_cdb[1] & 0x02)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700254 pr_err("LongIO and Obselete Bits set, returning"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800255 " ILLEGAL_REQUEST\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700256 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
257 ret = -EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400258 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800259 }
260 /*
261 * This is currently the case for target_core_mod passthrough struct se_cmd
262 * ops
263 */
Wei Yongjun609234e2012-09-07 14:56:28 +0800264 if (!sess || !sess->se_tpg)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400265 goto out;
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700266 rc = target_check_scsi2_reservation_conflict(cmd);
267 if (rc == 1)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400268 goto out;
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700269 else if (rc < 0) {
270 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
271 ret = -EINVAL;
272 goto out;
273 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800274
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400275 ret = 0;
Wei Yongjun609234e2012-09-07 14:56:28 +0800276 tpg = sess->se_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800277 spin_lock(&dev->dev_reservation_lock);
278 if (dev->dev_reserved_node_acl &&
279 (dev->dev_reserved_node_acl != sess->se_node_acl)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700280 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000281 tpg->se_tpg_tfo->get_fabric_name());
Andy Grover6708bb22011-06-08 10:36:43 -0700282 pr_err("Original reserver LUN: %u %s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000283 cmd->se_lun->unpacked_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800284 dev->dev_reserved_node_acl->initiatorname);
Andy Grover6708bb22011-06-08 10:36:43 -0700285 pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
Andy Grovere3d6f902011-07-19 08:55:10 +0000286 " from %s \n", cmd->se_lun->unpacked_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800287 cmd->se_deve->mapped_lun,
288 sess->se_node_acl->initiatorname);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700289 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
290 ret = -EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400291 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800292 }
293
294 dev->dev_reserved_node_acl = sess->se_node_acl;
295 dev->dev_flags |= DF_SPC2_RESERVATIONS;
296 if (sess->sess_bin_isid != 0) {
297 dev->dev_res_bin_isid = sess->sess_bin_isid;
298 dev->dev_flags |= DF_SPC2_RESERVATIONS_WITH_ISID;
299 }
Andy Grover6708bb22011-06-08 10:36:43 -0700300 pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
Andy Grovere3d6f902011-07-19 08:55:10 +0000301 " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
302 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800303 sess->se_node_acl->initiatorname);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800304
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400305out_unlock:
306 spin_unlock(&dev->dev_reservation_lock);
307out:
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400308 if (!ret)
309 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400310 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800311}
312
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800313
314/*
315 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
316 *
317 * This function is called by those initiator ports who are *NOT*
318 * the active PR reservation holder when a reservation is present.
319 */
320static int core_scsi3_pr_seq_non_holder(
321 struct se_cmd *cmd,
322 unsigned char *cdb,
323 u32 pr_reg_type)
324{
325 struct se_dev_entry *se_deve;
Andy Grovere3d6f902011-07-19 08:55:10 +0000326 struct se_session *se_sess = cmd->se_sess;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800327 int other_cdb = 0, ignore_reg;
328 int registered_nexus = 0, ret = 1; /* Conflict by default */
329 int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
330 int we = 0; /* Write Exclusive */
331 int legacy = 0; /* Act like a legacy device and return
332 * RESERVATION CONFLICT on some CDBs */
333 /*
334 * A legacy SPC-2 reservation is being held.
335 */
336 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS)
337 return core_scsi2_reservation_seq_non_holder(cmd,
338 cdb, pr_reg_type);
339
Jörn Engelf2083242012-03-15 15:05:40 -0400340 se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800341 /*
342 * Determine if the registration should be ignored due to
343 * non-matching ISIDs in core_scsi3_pr_reservation_check().
344 */
345 ignore_reg = (pr_reg_type & 0x80000000);
346 if (ignore_reg)
347 pr_reg_type &= ~0x80000000;
348
349 switch (pr_reg_type) {
350 case PR_TYPE_WRITE_EXCLUSIVE:
351 we = 1;
352 case PR_TYPE_EXCLUSIVE_ACCESS:
353 /*
354 * Some commands are only allowed for the persistent reservation
355 * holder.
356 */
357 if ((se_deve->def_pr_registered) && !(ignore_reg))
358 registered_nexus = 1;
359 break;
360 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
361 we = 1;
362 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
363 /*
364 * Some commands are only allowed for registered I_T Nexuses.
365 */
366 reg_only = 1;
367 if ((se_deve->def_pr_registered) && !(ignore_reg))
368 registered_nexus = 1;
369 break;
370 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
371 we = 1;
372 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
373 /*
374 * Each registered I_T Nexus is a reservation holder.
375 */
376 all_reg = 1;
377 if ((se_deve->def_pr_registered) && !(ignore_reg))
378 registered_nexus = 1;
379 break;
380 default:
Andy Grovere3d6f902011-07-19 08:55:10 +0000381 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800382 }
383 /*
384 * Referenced from spc4r17 table 45 for *NON* PR holder access
385 */
386 switch (cdb[0]) {
387 case SECURITY_PROTOCOL_IN:
388 if (registered_nexus)
389 return 0;
390 ret = (we) ? 0 : 1;
391 break;
392 case MODE_SENSE:
393 case MODE_SENSE_10:
394 case READ_ATTRIBUTE:
395 case READ_BUFFER:
396 case RECEIVE_DIAGNOSTIC:
397 if (legacy) {
398 ret = 1;
399 break;
400 }
401 if (registered_nexus) {
402 ret = 0;
403 break;
404 }
405 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
406 break;
407 case PERSISTENT_RESERVE_OUT:
408 /*
409 * This follows PERSISTENT_RESERVE_OUT service actions that
410 * are allowed in the presence of various reservations.
411 * See spc4r17, table 46
412 */
413 switch (cdb[1] & 0x1f) {
414 case PRO_CLEAR:
415 case PRO_PREEMPT:
416 case PRO_PREEMPT_AND_ABORT:
417 ret = (registered_nexus) ? 0 : 1;
418 break;
419 case PRO_REGISTER:
420 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
421 ret = 0;
422 break;
423 case PRO_REGISTER_AND_MOVE:
424 case PRO_RESERVE:
425 ret = 1;
426 break;
427 case PRO_RELEASE:
428 ret = (registered_nexus) ? 0 : 1;
429 break;
430 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700431 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800432 " action: 0x%02x\n", cdb[1] & 0x1f);
Andy Grovere3d6f902011-07-19 08:55:10 +0000433 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800434 }
435 break;
436 case RELEASE:
437 case RELEASE_10:
Christoph Hellwigeacac002011-11-03 17:50:40 -0400438 /* Handled by CRH=1 in target_scsi2_reservation_release() */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800439 ret = 0;
440 break;
441 case RESERVE:
442 case RESERVE_10:
Christoph Hellwigeacac002011-11-03 17:50:40 -0400443 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800444 ret = 0;
445 break;
446 case TEST_UNIT_READY:
447 ret = (legacy) ? 1 : 0; /* Conflict for legacy */
448 break;
449 case MAINTENANCE_IN:
450 switch (cdb[1] & 0x1f) {
451 case MI_MANAGEMENT_PROTOCOL_IN:
452 if (registered_nexus) {
453 ret = 0;
454 break;
455 }
456 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
457 break;
458 case MI_REPORT_SUPPORTED_OPERATION_CODES:
459 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
460 if (legacy) {
461 ret = 1;
462 break;
463 }
464 if (registered_nexus) {
465 ret = 0;
466 break;
467 }
468 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
469 break;
470 case MI_REPORT_ALIASES:
471 case MI_REPORT_IDENTIFYING_INFORMATION:
472 case MI_REPORT_PRIORITY:
473 case MI_REPORT_TARGET_PGS:
474 case MI_REPORT_TIMESTAMP:
475 ret = 0; /* Allowed */
476 break;
477 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700478 pr_err("Unknown MI Service Action: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800479 (cdb[1] & 0x1f));
Andy Grovere3d6f902011-07-19 08:55:10 +0000480 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800481 }
482 break;
483 case ACCESS_CONTROL_IN:
484 case ACCESS_CONTROL_OUT:
485 case INQUIRY:
486 case LOG_SENSE:
487 case READ_MEDIA_SERIAL_NUMBER:
488 case REPORT_LUNS:
489 case REQUEST_SENSE:
Marco Sanvido68169662012-01-03 17:12:58 -0800490 case PERSISTENT_RESERVE_IN:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800491 ret = 0; /*/ Allowed CDBs */
492 break;
493 default:
494 other_cdb = 1;
495 break;
496 }
497 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300498 * Case where the CDB is explicitly allowed in the above switch
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800499 * statement.
500 */
Andy Grover6708bb22011-06-08 10:36:43 -0700501 if (!ret && !other_cdb) {
Andy Grover6708bb22011-06-08 10:36:43 -0700502 pr_debug("Allowing explict CDB: 0x%02x for %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800503 " reservation holder\n", cdb[0],
504 core_scsi3_pr_dump_type(pr_reg_type));
Andy Grover8b1e1242012-04-03 15:51:12 -0700505
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800506 return ret;
507 }
508 /*
509 * Check if write exclusive initiator ports *NOT* holding the
510 * WRITE_EXCLUSIVE_* reservation.
511 */
Andy Groveree1b1b92012-07-12 17:34:54 -0700512 if (we && !registered_nexus) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800513 if (cmd->data_direction == DMA_TO_DEVICE) {
514 /*
515 * Conflict for write exclusive
516 */
Andy Grover6708bb22011-06-08 10:36:43 -0700517 pr_debug("%s Conflict for unregistered nexus"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800518 " %s CDB: 0x%02x to %s reservation\n",
519 transport_dump_cmd_direction(cmd),
520 se_sess->se_node_acl->initiatorname, cdb[0],
521 core_scsi3_pr_dump_type(pr_reg_type));
522 return 1;
523 } else {
524 /*
525 * Allow non WRITE CDBs for all Write Exclusive
526 * PR TYPEs to pass for registered and
527 * non-registered_nexuxes NOT holding the reservation.
528 *
529 * We only make noise for the unregisterd nexuses,
530 * as we expect registered non-reservation holding
531 * nexuses to issue CDBs.
532 */
Andy Grover8b1e1242012-04-03 15:51:12 -0700533
Andy Grover6708bb22011-06-08 10:36:43 -0700534 if (!registered_nexus) {
535 pr_debug("Allowing implict CDB: 0x%02x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800536 " for %s reservation on unregistered"
537 " nexus\n", cdb[0],
538 core_scsi3_pr_dump_type(pr_reg_type));
539 }
Andy Grover8b1e1242012-04-03 15:51:12 -0700540
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800541 return 0;
542 }
543 } else if ((reg_only) || (all_reg)) {
544 if (registered_nexus) {
545 /*
546 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
547 * allow commands from registered nexuses.
548 */
Andy Grover8b1e1242012-04-03 15:51:12 -0700549
Andy Grover6708bb22011-06-08 10:36:43 -0700550 pr_debug("Allowing implict CDB: 0x%02x for %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800551 " reservation\n", cdb[0],
552 core_scsi3_pr_dump_type(pr_reg_type));
Andy Grover8b1e1242012-04-03 15:51:12 -0700553
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800554 return 0;
555 }
556 }
Andy Grover6708bb22011-06-08 10:36:43 -0700557 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800558 " for %s reservation\n", transport_dump_cmd_direction(cmd),
559 (registered_nexus) ? "" : "un",
560 se_sess->se_node_acl->initiatorname, cdb[0],
561 core_scsi3_pr_dump_type(pr_reg_type));
562
563 return 1; /* Conflict by default */
564}
565
566static u32 core_scsi3_pr_generation(struct se_device *dev)
567{
Andy Grovere3d6f902011-07-19 08:55:10 +0000568 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800569 u32 prg;
570 /*
571 * PRGeneration field shall contain the value of a 32-bit wrapping
572 * counter mainted by the device server.
573 *
574 * Note that this is done regardless of Active Persist across
575 * Target PowerLoss (APTPL)
576 *
577 * See spc4r17 section 6.3.12 READ_KEYS service action
578 */
579 spin_lock(&dev->dev_reservation_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +0000580 prg = su_dev->t10_pr.pr_generation++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800581 spin_unlock(&dev->dev_reservation_lock);
582
583 return prg;
584}
585
586static int core_scsi3_pr_reservation_check(
587 struct se_cmd *cmd,
588 u32 *pr_reg_type)
589{
590 struct se_device *dev = cmd->se_dev;
591 struct se_session *sess = cmd->se_sess;
592 int ret;
593
Andy Grover6708bb22011-06-08 10:36:43 -0700594 if (!sess)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800595 return 0;
596 /*
597 * A legacy SPC-2 reservation is being held.
598 */
599 if (dev->dev_flags & DF_SPC2_RESERVATIONS)
600 return core_scsi2_reservation_check(cmd, pr_reg_type);
601
602 spin_lock(&dev->dev_reservation_lock);
Andy Grover6708bb22011-06-08 10:36:43 -0700603 if (!dev->dev_pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800604 spin_unlock(&dev->dev_reservation_lock);
605 return 0;
606 }
607 *pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
608 cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
609 if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl) {
610 spin_unlock(&dev->dev_reservation_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +0000611 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800612 }
Andy Grover6708bb22011-06-08 10:36:43 -0700613 if (!dev->dev_pr_res_holder->isid_present_at_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800614 spin_unlock(&dev->dev_reservation_lock);
615 return 0;
616 }
617 ret = (dev->dev_pr_res_holder->pr_reg_bin_isid ==
Andy Grovere3d6f902011-07-19 08:55:10 +0000618 sess->sess_bin_isid) ? 0 : -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800619 /*
620 * Use bit in *pr_reg_type to notify ISID mismatch in
621 * core_scsi3_pr_seq_non_holder().
622 */
623 if (ret != 0)
624 *pr_reg_type |= 0x80000000;
625 spin_unlock(&dev->dev_reservation_lock);
626
627 return ret;
628}
629
630static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
631 struct se_device *dev,
632 struct se_node_acl *nacl,
633 struct se_dev_entry *deve,
634 unsigned char *isid,
635 u64 sa_res_key,
636 int all_tg_pt,
637 int aptpl)
638{
Andy Grovere3d6f902011-07-19 08:55:10 +0000639 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800640 struct t10_pr_registration *pr_reg;
641
642 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
Andy Grover6708bb22011-06-08 10:36:43 -0700643 if (!pr_reg) {
644 pr_err("Unable to allocate struct t10_pr_registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800645 return NULL;
646 }
647
Andy Grovere3d6f902011-07-19 08:55:10 +0000648 pr_reg->pr_aptpl_buf = kzalloc(su_dev->t10_pr.pr_aptpl_buf_len,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800649 GFP_ATOMIC);
Andy Grover6708bb22011-06-08 10:36:43 -0700650 if (!pr_reg->pr_aptpl_buf) {
651 pr_err("Unable to allocate pr_reg->pr_aptpl_buf\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800652 kmem_cache_free(t10_pr_reg_cache, pr_reg);
653 return NULL;
654 }
655
656 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
657 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
658 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
659 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
660 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
661 atomic_set(&pr_reg->pr_res_holders, 0);
662 pr_reg->pr_reg_nacl = nacl;
663 pr_reg->pr_reg_deve = deve;
664 pr_reg->pr_res_mapped_lun = deve->mapped_lun;
665 pr_reg->pr_aptpl_target_lun = deve->se_lun->unpacked_lun;
666 pr_reg->pr_res_key = sa_res_key;
667 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
668 pr_reg->pr_reg_aptpl = aptpl;
669 pr_reg->pr_reg_tg_pt_lun = deve->se_lun;
670 /*
671 * If an ISID value for this SCSI Initiator Port exists,
672 * save it to the registration now.
673 */
674 if (isid != NULL) {
675 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
676 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
677 pr_reg->isid_present_at_reg = 1;
678 }
679
680 return pr_reg;
681}
682
683static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
684static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
685
686/*
687 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
688 * modes.
689 */
690static struct t10_pr_registration *__core_scsi3_alloc_registration(
691 struct se_device *dev,
692 struct se_node_acl *nacl,
693 struct se_dev_entry *deve,
694 unsigned char *isid,
695 u64 sa_res_key,
696 int all_tg_pt,
697 int aptpl)
698{
699 struct se_dev_entry *deve_tmp;
700 struct se_node_acl *nacl_tmp;
701 struct se_port *port, *port_tmp;
702 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
703 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
704 int ret;
705 /*
706 * Create a registration for the I_T Nexus upon which the
707 * PROUT REGISTER was received.
708 */
709 pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, deve, isid,
710 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -0700711 if (!pr_reg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800712 return NULL;
713 /*
714 * Return pointer to pr_reg for ALL_TG_PT=0
715 */
Andy Grover6708bb22011-06-08 10:36:43 -0700716 if (!all_tg_pt)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800717 return pr_reg;
718 /*
719 * Create list of matching SCSI Initiator Port registrations
720 * for ALL_TG_PT=1
721 */
722 spin_lock(&dev->se_port_lock);
723 list_for_each_entry_safe(port, port_tmp, &dev->dev_sep_list, sep_list) {
724 atomic_inc(&port->sep_tg_pt_ref_cnt);
725 smp_mb__after_atomic_inc();
726 spin_unlock(&dev->se_port_lock);
727
728 spin_lock_bh(&port->sep_alua_lock);
729 list_for_each_entry(deve_tmp, &port->sep_alua_list,
730 alua_port_list) {
731 /*
732 * This pointer will be NULL for demo mode MappedLUNs
733 * that have not been make explict via a ConfigFS
734 * MappedLUN group for the SCSI Initiator Node ACL.
735 */
Andy Grover6708bb22011-06-08 10:36:43 -0700736 if (!deve_tmp->se_lun_acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800737 continue;
738
739 nacl_tmp = deve_tmp->se_lun_acl->se_lun_nacl;
740 /*
741 * Skip the matching struct se_node_acl that is allocated
742 * above..
743 */
744 if (nacl == nacl_tmp)
745 continue;
746 /*
747 * Only perform PR registrations for target ports on
748 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
749 * arrived.
750 */
751 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
752 continue;
753 /*
754 * Look for a matching Initiator Node ACL in ASCII format
755 */
756 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
757 continue;
758
759 atomic_inc(&deve_tmp->pr_ref_count);
760 smp_mb__after_atomic_inc();
761 spin_unlock_bh(&port->sep_alua_lock);
762 /*
763 * Grab a configfs group dependency that is released
764 * for the exception path at label out: below, or upon
765 * completion of adding ALL_TG_PT=1 registrations in
766 * __core_scsi3_add_registration()
767 */
768 ret = core_scsi3_lunacl_depend_item(deve_tmp);
769 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -0700770 pr_err("core_scsi3_lunacl_depend"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800771 "_item() failed\n");
772 atomic_dec(&port->sep_tg_pt_ref_cnt);
773 smp_mb__after_atomic_dec();
774 atomic_dec(&deve_tmp->pr_ref_count);
775 smp_mb__after_atomic_dec();
776 goto out;
777 }
778 /*
779 * Located a matching SCSI Initiator Port on a different
780 * port, allocate the pr_reg_atp and attach it to the
781 * pr_reg->pr_reg_atp_list that will be processed once
782 * the original *pr_reg is processed in
783 * __core_scsi3_add_registration()
784 */
785 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
786 nacl_tmp, deve_tmp, NULL,
787 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -0700788 if (!pr_reg_atp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800789 atomic_dec(&port->sep_tg_pt_ref_cnt);
790 smp_mb__after_atomic_dec();
791 atomic_dec(&deve_tmp->pr_ref_count);
792 smp_mb__after_atomic_dec();
793 core_scsi3_lunacl_undepend_item(deve_tmp);
794 goto out;
795 }
796
797 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
798 &pr_reg->pr_reg_atp_list);
799 spin_lock_bh(&port->sep_alua_lock);
800 }
801 spin_unlock_bh(&port->sep_alua_lock);
802
803 spin_lock(&dev->se_port_lock);
804 atomic_dec(&port->sep_tg_pt_ref_cnt);
805 smp_mb__after_atomic_dec();
806 }
807 spin_unlock(&dev->se_port_lock);
808
809 return pr_reg;
810out:
811 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
812 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
813 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
814 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
815 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
816 }
817 kmem_cache_free(t10_pr_reg_cache, pr_reg);
818 return NULL;
819}
820
821int core_scsi3_alloc_aptpl_registration(
Andy Grovere3d6f902011-07-19 08:55:10 +0000822 struct t10_reservation *pr_tmpl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800823 u64 sa_res_key,
824 unsigned char *i_port,
825 unsigned char *isid,
826 u32 mapped_lun,
827 unsigned char *t_port,
828 u16 tpgt,
829 u32 target_lun,
830 int res_holder,
831 int all_tg_pt,
832 u8 type)
833{
834 struct t10_pr_registration *pr_reg;
835
Andy Grover6708bb22011-06-08 10:36:43 -0700836 if (!i_port || !t_port || !sa_res_key) {
837 pr_err("Illegal parameters for APTPL registration\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000838 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800839 }
840
841 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700842 if (!pr_reg) {
843 pr_err("Unable to allocate struct t10_pr_registration\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000844 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800845 }
846 pr_reg->pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len, GFP_KERNEL);
847
848 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
849 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
850 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
851 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
852 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
853 atomic_set(&pr_reg->pr_res_holders, 0);
854 pr_reg->pr_reg_nacl = NULL;
855 pr_reg->pr_reg_deve = NULL;
856 pr_reg->pr_res_mapped_lun = mapped_lun;
857 pr_reg->pr_aptpl_target_lun = target_lun;
858 pr_reg->pr_res_key = sa_res_key;
859 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
860 pr_reg->pr_reg_aptpl = 1;
861 pr_reg->pr_reg_tg_pt_lun = NULL;
862 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
863 pr_reg->pr_res_type = type;
864 /*
865 * If an ISID value had been saved in APTPL metadata for this
866 * SCSI Initiator Port, restore it now.
867 */
868 if (isid != NULL) {
869 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
870 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
871 pr_reg->isid_present_at_reg = 1;
872 }
873 /*
874 * Copy the i_port and t_port information from caller.
875 */
876 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
877 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
878 pr_reg->pr_reg_tpgt = tpgt;
879 /*
880 * Set pr_res_holder from caller, the pr_reg who is the reservation
881 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
882 * the Initiator Node LUN ACL from the fabric module is created for
883 * this registration.
884 */
885 pr_reg->pr_res_holder = res_holder;
886
887 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
Andy Grover6708bb22011-06-08 10:36:43 -0700888 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800889 " metadata\n", (res_holder) ? "+reservation" : "");
890 return 0;
891}
892
893static void core_scsi3_aptpl_reserve(
894 struct se_device *dev,
895 struct se_portal_group *tpg,
896 struct se_node_acl *node_acl,
897 struct t10_pr_registration *pr_reg)
898{
899 char i_buf[PR_REG_ISID_ID_LEN];
900 int prf_isid;
901
902 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
903 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
904 PR_REG_ISID_ID_LEN);
905
906 spin_lock(&dev->dev_reservation_lock);
907 dev->dev_pr_res_holder = pr_reg;
908 spin_unlock(&dev->dev_reservation_lock);
909
Andy Grover6708bb22011-06-08 10:36:43 -0700910 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800911 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000912 tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800913 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
914 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -0700915 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000916 tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800917 (prf_isid) ? &i_buf[0] : "");
918}
919
920static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
921 struct t10_pr_registration *, int, int);
922
923static int __core_scsi3_check_aptpl_registration(
924 struct se_device *dev,
925 struct se_portal_group *tpg,
926 struct se_lun *lun,
927 u32 target_lun,
928 struct se_node_acl *nacl,
929 struct se_dev_entry *deve)
930{
931 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
Andy Grovere3d6f902011-07-19 08:55:10 +0000932 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800933 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
934 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
935 u16 tpgt;
936
937 memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
938 memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
939 /*
940 * Copy Initiator Port information from struct se_node_acl
941 */
942 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
943 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
Andy Grovere3d6f902011-07-19 08:55:10 +0000944 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
945 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800946 /*
947 * Look for the matching registrations+reservation from those
948 * created from APTPL metadata. Note that multiple registrations
949 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
950 * TransportIDs.
951 */
952 spin_lock(&pr_tmpl->aptpl_reg_lock);
953 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
954 pr_reg_aptpl_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700955 if (!strcmp(pr_reg->pr_iport, i_port) &&
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800956 (pr_reg->pr_res_mapped_lun == deve->mapped_lun) &&
957 !(strcmp(pr_reg->pr_tport, t_port)) &&
958 (pr_reg->pr_reg_tpgt == tpgt) &&
959 (pr_reg->pr_aptpl_target_lun == target_lun)) {
960
961 pr_reg->pr_reg_nacl = nacl;
962 pr_reg->pr_reg_deve = deve;
963 pr_reg->pr_reg_tg_pt_lun = lun;
964
965 list_del(&pr_reg->pr_reg_aptpl_list);
966 spin_unlock(&pr_tmpl->aptpl_reg_lock);
967 /*
968 * At this point all of the pointers in *pr_reg will
969 * be setup, so go ahead and add the registration.
970 */
971
972 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
973 /*
974 * If this registration is the reservation holder,
975 * make that happen now..
976 */
977 if (pr_reg->pr_res_holder)
978 core_scsi3_aptpl_reserve(dev, tpg,
979 nacl, pr_reg);
980 /*
981 * Reenable pr_aptpl_active to accept new metadata
982 * updates once the SCSI device is active again..
983 */
984 spin_lock(&pr_tmpl->aptpl_reg_lock);
985 pr_tmpl->pr_aptpl_active = 1;
986 }
987 }
988 spin_unlock(&pr_tmpl->aptpl_reg_lock);
989
990 return 0;
991}
992
993int core_scsi3_check_aptpl_registration(
994 struct se_device *dev,
995 struct se_portal_group *tpg,
996 struct se_lun *lun,
997 struct se_lun_acl *lun_acl)
998{
Andy Grovere3d6f902011-07-19 08:55:10 +0000999 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001000 struct se_node_acl *nacl = lun_acl->se_lun_nacl;
Jörn Engelf2083242012-03-15 15:05:40 -04001001 struct se_dev_entry *deve = nacl->device_list[lun_acl->mapped_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001002
Andy Grovere3d6f902011-07-19 08:55:10 +00001003 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001004 return 0;
1005
1006 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
1007 lun->unpacked_lun, nacl, deve);
1008}
1009
1010static void __core_scsi3_dump_registration(
1011 struct target_core_fabric_ops *tfo,
1012 struct se_device *dev,
1013 struct se_node_acl *nacl,
1014 struct t10_pr_registration *pr_reg,
1015 int register_type)
1016{
1017 struct se_portal_group *se_tpg = nacl->se_tpg;
1018 char i_buf[PR_REG_ISID_ID_LEN];
1019 int prf_isid;
1020
1021 memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
1022 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1023 PR_REG_ISID_ID_LEN);
1024
Andy Grover6708bb22011-06-08 10:36:43 -07001025 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001026 " Node: %s%s\n", tfo->get_fabric_name(), (register_type == 2) ?
1027 "_AND_MOVE" : (register_type == 1) ?
1028 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
1029 (prf_isid) ? i_buf : "");
Andy Grover6708bb22011-06-08 10:36:43 -07001030 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001031 tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg),
1032 tfo->tpg_get_tag(se_tpg));
Andy Grover6708bb22011-06-08 10:36:43 -07001033 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001034 " Port(s)\n", tfo->get_fabric_name(),
1035 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
Andy Grovere3d6f902011-07-19 08:55:10 +00001036 dev->transport->name);
Andy Grover6708bb22011-06-08 10:36:43 -07001037 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001038 " 0x%08x APTPL: %d\n", tfo->get_fabric_name(),
1039 pr_reg->pr_res_key, pr_reg->pr_res_generation,
1040 pr_reg->pr_reg_aptpl);
1041}
1042
1043/*
1044 * this function can be called with struct se_device->dev_reservation_lock
1045 * when register_move = 1
1046 */
1047static void __core_scsi3_add_registration(
1048 struct se_device *dev,
1049 struct se_node_acl *nacl,
1050 struct t10_pr_registration *pr_reg,
1051 int register_type,
1052 int register_move)
1053{
Andy Grovere3d6f902011-07-19 08:55:10 +00001054 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001055 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1056 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
Andy Grovere3d6f902011-07-19 08:55:10 +00001057 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001058
1059 /*
1060 * Increment PRgeneration counter for struct se_device upon a successful
1061 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1062 *
1063 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1064 * action, the struct se_device->dev_reservation_lock will already be held,
1065 * so we do not call core_scsi3_pr_generation() which grabs the lock
1066 * for the REGISTER.
1067 */
1068 pr_reg->pr_res_generation = (register_move) ?
Andy Grovere3d6f902011-07-19 08:55:10 +00001069 su_dev->t10_pr.pr_generation++ :
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001070 core_scsi3_pr_generation(dev);
1071
1072 spin_lock(&pr_tmpl->registration_lock);
1073 list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
1074 pr_reg->pr_reg_deve->def_pr_registered = 1;
1075
1076 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1077 spin_unlock(&pr_tmpl->registration_lock);
1078 /*
1079 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1080 */
Andy Grover6708bb22011-06-08 10:36:43 -07001081 if (!pr_reg->pr_reg_all_tg_pt || register_move)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001082 return;
1083 /*
1084 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1085 * allocated in __core_scsi3_alloc_registration()
1086 */
1087 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1088 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
1089 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1090
1091 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1092
1093 spin_lock(&pr_tmpl->registration_lock);
1094 list_add_tail(&pr_reg_tmp->pr_reg_list,
1095 &pr_tmpl->registration_list);
1096 pr_reg_tmp->pr_reg_deve->def_pr_registered = 1;
1097
1098 __core_scsi3_dump_registration(tfo, dev,
1099 pr_reg_tmp->pr_reg_nacl, pr_reg_tmp,
1100 register_type);
1101 spin_unlock(&pr_tmpl->registration_lock);
1102 /*
1103 * Drop configfs group dependency reference from
1104 * __core_scsi3_alloc_registration()
1105 */
1106 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1107 }
1108}
1109
1110static int core_scsi3_alloc_registration(
1111 struct se_device *dev,
1112 struct se_node_acl *nacl,
1113 struct se_dev_entry *deve,
1114 unsigned char *isid,
1115 u64 sa_res_key,
1116 int all_tg_pt,
1117 int aptpl,
1118 int register_type,
1119 int register_move)
1120{
1121 struct t10_pr_registration *pr_reg;
1122
1123 pr_reg = __core_scsi3_alloc_registration(dev, nacl, deve, isid,
1124 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001125 if (!pr_reg)
Andy Grovere3d6f902011-07-19 08:55:10 +00001126 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001127
1128 __core_scsi3_add_registration(dev, nacl, pr_reg,
1129 register_type, register_move);
1130 return 0;
1131}
1132
1133static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1134 struct se_device *dev,
1135 struct se_node_acl *nacl,
1136 unsigned char *isid)
1137{
Andy Grovere3d6f902011-07-19 08:55:10 +00001138 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001139 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1140 struct se_portal_group *tpg;
1141
1142 spin_lock(&pr_tmpl->registration_lock);
1143 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1144 &pr_tmpl->registration_list, pr_reg_list) {
1145 /*
1146 * First look for a matching struct se_node_acl
1147 */
1148 if (pr_reg->pr_reg_nacl != nacl)
1149 continue;
1150
1151 tpg = pr_reg->pr_reg_nacl->se_tpg;
1152 /*
1153 * If this registration does NOT contain a fabric provided
1154 * ISID, then we have found a match.
1155 */
Andy Grover6708bb22011-06-08 10:36:43 -07001156 if (!pr_reg->isid_present_at_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001157 /*
1158 * Determine if this SCSI device server requires that
1159 * SCSI Intiatior TransportID w/ ISIDs is enforced
1160 * for fabric modules (iSCSI) requiring them.
1161 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001162 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1163 if (dev->se_sub_dev->se_dev_attrib.enforce_pr_isids)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001164 continue;
1165 }
1166 atomic_inc(&pr_reg->pr_res_holders);
1167 smp_mb__after_atomic_inc();
1168 spin_unlock(&pr_tmpl->registration_lock);
1169 return pr_reg;
1170 }
1171 /*
1172 * If the *pr_reg contains a fabric defined ISID for multi-value
1173 * SCSI Initiator Port TransportIDs, then we expect a valid
1174 * matching ISID to be provided by the local SCSI Initiator Port.
1175 */
Andy Grover6708bb22011-06-08 10:36:43 -07001176 if (!isid)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001177 continue;
1178 if (strcmp(isid, pr_reg->pr_reg_isid))
1179 continue;
1180
1181 atomic_inc(&pr_reg->pr_res_holders);
1182 smp_mb__after_atomic_inc();
1183 spin_unlock(&pr_tmpl->registration_lock);
1184 return pr_reg;
1185 }
1186 spin_unlock(&pr_tmpl->registration_lock);
1187
1188 return NULL;
1189}
1190
1191static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1192 struct se_device *dev,
1193 struct se_node_acl *nacl,
1194 struct se_session *sess)
1195{
1196 struct se_portal_group *tpg = nacl->se_tpg;
1197 unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1198
Andy Grovere3d6f902011-07-19 08:55:10 +00001199 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001200 memset(&buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +00001201 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001202 PR_REG_ISID_LEN);
1203 isid_ptr = &buf[0];
1204 }
1205
1206 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1207}
1208
1209static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1210{
1211 atomic_dec(&pr_reg->pr_res_holders);
1212 smp_mb__after_atomic_dec();
1213}
1214
1215static int core_scsi3_check_implict_release(
1216 struct se_device *dev,
1217 struct t10_pr_registration *pr_reg)
1218{
1219 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1220 struct t10_pr_registration *pr_res_holder;
1221 int ret = 0;
1222
1223 spin_lock(&dev->dev_reservation_lock);
1224 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001225 if (!pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001226 spin_unlock(&dev->dev_reservation_lock);
1227 return ret;
1228 }
1229 if (pr_res_holder == pr_reg) {
1230 /*
1231 * Perform an implict RELEASE if the registration that
1232 * is being released is holding the reservation.
1233 *
1234 * From spc4r17, section 5.7.11.1:
1235 *
1236 * e) If the I_T nexus is the persistent reservation holder
1237 * and the persistent reservation is not an all registrants
1238 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1239 * service action or REGISTER AND IGNORE EXISTING KEY
1240 * service action with the SERVICE ACTION RESERVATION KEY
1241 * field set to zero (see 5.7.11.3).
1242 */
1243 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0);
1244 ret = 1;
1245 /*
1246 * For 'All Registrants' reservation types, all existing
1247 * registrations are still processed as reservation holders
1248 * in core_scsi3_pr_seq_non_holder() after the initial
1249 * reservation holder is implictly released here.
1250 */
1251 } else if (pr_reg->pr_reg_all_tg_pt &&
1252 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1253 pr_reg->pr_reg_nacl->initiatorname)) &&
1254 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001255 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001256 " UNREGISTER while existing reservation with matching"
1257 " key 0x%016Lx is present from another SCSI Initiator"
1258 " Port\n", pr_reg->pr_res_key);
Andy Grovere3d6f902011-07-19 08:55:10 +00001259 ret = -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001260 }
1261 spin_unlock(&dev->dev_reservation_lock);
1262
1263 return ret;
1264}
1265
1266/*
Andy Grovere3d6f902011-07-19 08:55:10 +00001267 * Called with struct t10_reservation->registration_lock held.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001268 */
1269static void __core_scsi3_free_registration(
1270 struct se_device *dev,
1271 struct t10_pr_registration *pr_reg,
1272 struct list_head *preempt_and_abort_list,
1273 int dec_holders)
1274{
1275 struct target_core_fabric_ops *tfo =
1276 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Andy Grovere3d6f902011-07-19 08:55:10 +00001277 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001278 char i_buf[PR_REG_ISID_ID_LEN];
1279 int prf_isid;
1280
1281 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1282 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1283 PR_REG_ISID_ID_LEN);
1284
1285 pr_reg->pr_reg_deve->def_pr_registered = 0;
1286 pr_reg->pr_reg_deve->pr_res_key = 0;
1287 list_del(&pr_reg->pr_reg_list);
1288 /*
1289 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1290 * so call core_scsi3_put_pr_reg() to decrement our reference.
1291 */
1292 if (dec_holders)
1293 core_scsi3_put_pr_reg(pr_reg);
1294 /*
1295 * Wait until all reference from any other I_T nexuses for this
1296 * *pr_reg have been released. Because list_del() is called above,
1297 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1298 * count back to zero, and we release *pr_reg.
1299 */
1300 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
1301 spin_unlock(&pr_tmpl->registration_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07001302 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001303 tfo->get_fabric_name());
1304 cpu_relax();
1305 spin_lock(&pr_tmpl->registration_lock);
1306 }
1307
Andy Grover6708bb22011-06-08 10:36:43 -07001308 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001309 " Node: %s%s\n", tfo->get_fabric_name(),
1310 pr_reg->pr_reg_nacl->initiatorname,
1311 (prf_isid) ? &i_buf[0] : "");
Andy Grover6708bb22011-06-08 10:36:43 -07001312 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001313 " Port(s)\n", tfo->get_fabric_name(),
1314 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
Andy Grovere3d6f902011-07-19 08:55:10 +00001315 dev->transport->name);
Andy Grover6708bb22011-06-08 10:36:43 -07001316 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001317 " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key,
1318 pr_reg->pr_res_generation);
1319
Andy Grover6708bb22011-06-08 10:36:43 -07001320 if (!preempt_and_abort_list) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001321 pr_reg->pr_reg_deve = NULL;
1322 pr_reg->pr_reg_nacl = NULL;
1323 kfree(pr_reg->pr_aptpl_buf);
1324 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1325 return;
1326 }
1327 /*
1328 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1329 * are released once the ABORT_TASK_SET has completed..
1330 */
1331 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1332}
1333
1334void core_scsi3_free_pr_reg_from_nacl(
1335 struct se_device *dev,
1336 struct se_node_acl *nacl)
1337{
Andy Grovere3d6f902011-07-19 08:55:10 +00001338 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001339 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1340 /*
1341 * If the passed se_node_acl matches the reservation holder,
1342 * release the reservation.
1343 */
1344 spin_lock(&dev->dev_reservation_lock);
1345 pr_res_holder = dev->dev_pr_res_holder;
1346 if ((pr_res_holder != NULL) &&
1347 (pr_res_holder->pr_reg_nacl == nacl))
1348 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0);
1349 spin_unlock(&dev->dev_reservation_lock);
1350 /*
1351 * Release any registration associated with the struct se_node_acl.
1352 */
1353 spin_lock(&pr_tmpl->registration_lock);
1354 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1355 &pr_tmpl->registration_list, pr_reg_list) {
1356
1357 if (pr_reg->pr_reg_nacl != nacl)
1358 continue;
1359
1360 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1361 }
1362 spin_unlock(&pr_tmpl->registration_lock);
1363}
1364
1365void core_scsi3_free_all_registrations(
1366 struct se_device *dev)
1367{
Andy Grovere3d6f902011-07-19 08:55:10 +00001368 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001369 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1370
1371 spin_lock(&dev->dev_reservation_lock);
1372 pr_res_holder = dev->dev_pr_res_holder;
1373 if (pr_res_holder != NULL) {
1374 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1375 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
1376 pr_res_holder, 0);
1377 }
1378 spin_unlock(&dev->dev_reservation_lock);
1379
1380 spin_lock(&pr_tmpl->registration_lock);
1381 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1382 &pr_tmpl->registration_list, pr_reg_list) {
1383
1384 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1385 }
1386 spin_unlock(&pr_tmpl->registration_lock);
1387
1388 spin_lock(&pr_tmpl->aptpl_reg_lock);
1389 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1390 pr_reg_aptpl_list) {
1391 list_del(&pr_reg->pr_reg_aptpl_list);
1392 kfree(pr_reg->pr_aptpl_buf);
1393 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1394 }
1395 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1396}
1397
1398static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1399{
Andy Grovere3d6f902011-07-19 08:55:10 +00001400 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001401 &tpg->tpg_group.cg_item);
1402}
1403
1404static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1405{
Andy Grovere3d6f902011-07-19 08:55:10 +00001406 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001407 &tpg->tpg_group.cg_item);
1408
1409 atomic_dec(&tpg->tpg_pr_ref_count);
1410 smp_mb__after_atomic_dec();
1411}
1412
1413static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1414{
1415 struct se_portal_group *tpg = nacl->se_tpg;
1416
1417 if (nacl->dynamic_node_acl)
1418 return 0;
1419
Andy Grovere3d6f902011-07-19 08:55:10 +00001420 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001421 &nacl->acl_group.cg_item);
1422}
1423
1424static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1425{
1426 struct se_portal_group *tpg = nacl->se_tpg;
1427
1428 if (nacl->dynamic_node_acl) {
1429 atomic_dec(&nacl->acl_pr_ref_count);
1430 smp_mb__after_atomic_dec();
1431 return;
1432 }
1433
Andy Grovere3d6f902011-07-19 08:55:10 +00001434 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001435 &nacl->acl_group.cg_item);
1436
1437 atomic_dec(&nacl->acl_pr_ref_count);
1438 smp_mb__after_atomic_dec();
1439}
1440
1441static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1442{
1443 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1444 struct se_node_acl *nacl;
1445 struct se_portal_group *tpg;
1446 /*
1447 * For nacl->dynamic_node_acl=1
1448 */
Andy Grover6708bb22011-06-08 10:36:43 -07001449 if (!lun_acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001450 return 0;
1451
1452 nacl = lun_acl->se_lun_nacl;
1453 tpg = nacl->se_tpg;
1454
Andy Grovere3d6f902011-07-19 08:55:10 +00001455 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001456 &lun_acl->se_lun_group.cg_item);
1457}
1458
1459static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1460{
1461 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1462 struct se_node_acl *nacl;
1463 struct se_portal_group *tpg;
1464 /*
1465 * For nacl->dynamic_node_acl=1
1466 */
Andy Grover6708bb22011-06-08 10:36:43 -07001467 if (!lun_acl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001468 atomic_dec(&se_deve->pr_ref_count);
1469 smp_mb__after_atomic_dec();
1470 return;
1471 }
1472 nacl = lun_acl->se_lun_nacl;
1473 tpg = nacl->se_tpg;
1474
Andy Grovere3d6f902011-07-19 08:55:10 +00001475 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001476 &lun_acl->se_lun_group.cg_item);
1477
1478 atomic_dec(&se_deve->pr_ref_count);
1479 smp_mb__after_atomic_dec();
1480}
1481
1482static int core_scsi3_decode_spec_i_port(
1483 struct se_cmd *cmd,
1484 struct se_portal_group *tpg,
1485 unsigned char *l_isid,
1486 u64 sa_res_key,
1487 int all_tg_pt,
1488 int aptpl)
1489{
Andy Grover5951146d2011-07-19 10:26:37 +00001490 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001491 struct se_port *tmp_port;
1492 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00001493 struct se_session *se_sess = cmd->se_sess;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001494 struct se_node_acl *dest_node_acl = NULL;
1495 struct se_dev_entry *dest_se_deve = NULL, *local_se_deve;
1496 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1497 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
Roland Dreierd0f474e2012-01-12 10:41:18 -08001498 LIST_HEAD(tid_dest_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001499 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
1500 struct target_core_fabric_ops *tmp_tf_ops;
Andy Grover05d1c7c2011-07-20 19:13:28 +00001501 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001502 unsigned char *ptr, *i_str = NULL, proto_ident, tmp_proto_ident;
1503 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
1504 u32 tpdl, tid_len = 0;
1505 int ret, dest_local_nexus, prf_isid;
1506 u32 dest_rtpi = 0;
1507
1508 memset(dest_iport, 0, 64);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001509
Jörn Engelf2083242012-03-15 15:05:40 -04001510 local_se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001511 /*
1512 * Allocate a struct pr_transport_id_holder and setup the
1513 * local_node_acl and local_se_deve pointers and add to
1514 * struct list_head tid_dest_list for add registration
1515 * processing in the loop of tid_dest_list below.
1516 */
1517 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001518 if (!tidh_new) {
1519 pr_err("Unable to allocate tidh_new\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001520 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1521 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001522 }
1523 INIT_LIST_HEAD(&tidh_new->dest_list);
1524 tidh_new->dest_tpg = tpg;
1525 tidh_new->dest_node_acl = se_sess->se_node_acl;
1526 tidh_new->dest_se_deve = local_se_deve;
1527
Andy Grover5951146d2011-07-19 10:26:37 +00001528 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001529 se_sess->se_node_acl, local_se_deve, l_isid,
1530 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001531 if (!local_pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001532 kfree(tidh_new);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001533 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1534 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001535 }
1536 tidh_new->dest_pr_reg = local_pr_reg;
1537 /*
1538 * The local I_T nexus does not hold any configfs dependances,
1539 * so we set tid_h->dest_local_nexus=1 to prevent the
1540 * configfs_undepend_item() calls in the tid_dest_list loops below.
1541 */
1542 tidh_new->dest_local_nexus = 1;
1543 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
Andy Grover05d1c7c2011-07-20 19:13:28 +00001544
Paolo Bonzini0d7f1292012-09-07 17:30:33 +02001545 if (cmd->data_length < 28) {
1546 pr_warn("SPC-PR: Received PR OUT parameter list"
1547 " length too small: %u\n", cmd->data_length);
1548 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1549 ret = -EINVAL;
1550 goto out;
1551 }
1552
Andy Grover49493142012-01-16 16:57:08 -08001553 buf = transport_kmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001554 /*
1555 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1556 * first extract TransportID Parameter Data Length, and make sure
1557 * the value matches up to the SCSI expected data transfer length.
1558 */
1559 tpdl = (buf[24] & 0xff) << 24;
1560 tpdl |= (buf[25] & 0xff) << 16;
1561 tpdl |= (buf[26] & 0xff) << 8;
1562 tpdl |= buf[27] & 0xff;
1563
1564 if ((tpdl + 28) != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07001565 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001566 " does not equal CDB data_length: %u\n", tpdl,
1567 cmd->data_length);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001568 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1569 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001570 goto out;
1571 }
1572 /*
1573 * Start processing the received transport IDs using the
1574 * receiving I_T Nexus portal's fabric dependent methods to
1575 * obtain the SCSI Initiator Port/Device Identifiers.
1576 */
1577 ptr = &buf[28];
1578
1579 while (tpdl > 0) {
1580 proto_ident = (ptr[0] & 0x0f);
1581 dest_tpg = NULL;
1582
1583 spin_lock(&dev->se_port_lock);
1584 list_for_each_entry(tmp_port, &dev->dev_sep_list, sep_list) {
1585 tmp_tpg = tmp_port->sep_tpg;
Andy Grover6708bb22011-06-08 10:36:43 -07001586 if (!tmp_tpg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001587 continue;
Andy Grovere3d6f902011-07-19 08:55:10 +00001588 tmp_tf_ops = tmp_tpg->se_tpg_tfo;
Andy Grover6708bb22011-06-08 10:36:43 -07001589 if (!tmp_tf_ops)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001590 continue;
Andy Grover6708bb22011-06-08 10:36:43 -07001591 if (!tmp_tf_ops->get_fabric_proto_ident ||
1592 !tmp_tf_ops->tpg_parse_pr_out_transport_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001593 continue;
1594 /*
1595 * Look for the matching proto_ident provided by
1596 * the received TransportID
1597 */
1598 tmp_proto_ident = tmp_tf_ops->get_fabric_proto_ident(tmp_tpg);
1599 if (tmp_proto_ident != proto_ident)
1600 continue;
1601 dest_rtpi = tmp_port->sep_rtpi;
1602
1603 i_str = tmp_tf_ops->tpg_parse_pr_out_transport_id(
1604 tmp_tpg, (const char *)ptr, &tid_len,
1605 &iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07001606 if (!i_str)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001607 continue;
1608
1609 atomic_inc(&tmp_tpg->tpg_pr_ref_count);
1610 smp_mb__after_atomic_inc();
1611 spin_unlock(&dev->se_port_lock);
1612
1613 ret = core_scsi3_tpg_depend_item(tmp_tpg);
1614 if (ret != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001615 pr_err(" core_scsi3_tpg_depend_item()"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001616 " for tmp_tpg\n");
1617 atomic_dec(&tmp_tpg->tpg_pr_ref_count);
1618 smp_mb__after_atomic_dec();
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001619 cmd->scsi_sense_reason =
1620 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1621 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001622 goto out;
1623 }
1624 /*
Masanari Iida35d1efe2012-08-16 22:43:13 +09001625 * Locate the destination initiator ACL to be registered
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001626 * from the decoded fabric module specific TransportID
1627 * at *i_str.
1628 */
Roland Dreier286388872011-08-16 09:40:01 -07001629 spin_lock_irq(&tmp_tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001630 dest_node_acl = __core_tpg_get_initiator_node_acl(
1631 tmp_tpg, i_str);
1632 if (dest_node_acl) {
1633 atomic_inc(&dest_node_acl->acl_pr_ref_count);
1634 smp_mb__after_atomic_inc();
1635 }
Roland Dreier286388872011-08-16 09:40:01 -07001636 spin_unlock_irq(&tmp_tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001637
Andy Grover6708bb22011-06-08 10:36:43 -07001638 if (!dest_node_acl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001639 core_scsi3_tpg_undepend_item(tmp_tpg);
1640 spin_lock(&dev->se_port_lock);
1641 continue;
1642 }
1643
1644 ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
1645 if (ret != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001646 pr_err("configfs_depend_item() failed"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001647 " for dest_node_acl->acl_group\n");
1648 atomic_dec(&dest_node_acl->acl_pr_ref_count);
1649 smp_mb__after_atomic_dec();
1650 core_scsi3_tpg_undepend_item(tmp_tpg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001651 cmd->scsi_sense_reason =
1652 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1653 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001654 goto out;
1655 }
1656
1657 dest_tpg = tmp_tpg;
Andy Grover6708bb22011-06-08 10:36:43 -07001658 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001659 " %s Port RTPI: %hu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001660 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001661 dest_node_acl->initiatorname, dest_rtpi);
1662
1663 spin_lock(&dev->se_port_lock);
1664 break;
1665 }
1666 spin_unlock(&dev->se_port_lock);
1667
Andy Grover6708bb22011-06-08 10:36:43 -07001668 if (!dest_tpg) {
1669 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001670 " dest_tpg\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001671 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1672 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001673 goto out;
1674 }
Andy Grover8b1e1242012-04-03 15:51:12 -07001675
Andy Grover6708bb22011-06-08 10:36:43 -07001676 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001677 " tid_len: %d for %s + %s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001678 dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001679 tpdl, tid_len, i_str, iport_ptr);
Andy Grover8b1e1242012-04-03 15:51:12 -07001680
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001681 if (tid_len > tpdl) {
Andy Grover6708bb22011-06-08 10:36:43 -07001682 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001683 " %u for Transport ID: %s\n", tid_len, ptr);
1684 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1685 core_scsi3_tpg_undepend_item(dest_tpg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001686 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1687 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001688 goto out;
1689 }
1690 /*
1691 * Locate the desintation struct se_dev_entry pointer for matching
1692 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1693 * Target Port.
1694 */
1695 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1696 dest_rtpi);
Andy Grover6708bb22011-06-08 10:36:43 -07001697 if (!dest_se_deve) {
1698 pr_err("Unable to locate %s dest_se_deve"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001699 " from destination RTPI: %hu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001700 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001701 dest_rtpi);
1702
1703 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1704 core_scsi3_tpg_undepend_item(dest_tpg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001705 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1706 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001707 goto out;
1708 }
1709
1710 ret = core_scsi3_lunacl_depend_item(dest_se_deve);
1711 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001712 pr_err("core_scsi3_lunacl_depend_item()"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001713 " failed\n");
1714 atomic_dec(&dest_se_deve->pr_ref_count);
1715 smp_mb__after_atomic_dec();
1716 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1717 core_scsi3_tpg_undepend_item(dest_tpg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001718 cmd->scsi_sense_reason =
1719 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1720 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001721 goto out;
1722 }
Andy Grover8b1e1242012-04-03 15:51:12 -07001723
Andy Grover6708bb22011-06-08 10:36:43 -07001724 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001725 " dest_se_deve mapped_lun: %u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001726 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001727 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
Andy Grover8b1e1242012-04-03 15:51:12 -07001728
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001729 /*
1730 * Skip any TransportIDs that already have a registration for
1731 * this target port.
1732 */
1733 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1734 iport_ptr);
1735 if (pr_reg_e) {
1736 core_scsi3_put_pr_reg(pr_reg_e);
1737 core_scsi3_lunacl_undepend_item(dest_se_deve);
1738 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1739 core_scsi3_tpg_undepend_item(dest_tpg);
1740 ptr += tid_len;
1741 tpdl -= tid_len;
1742 tid_len = 0;
1743 continue;
1744 }
1745 /*
1746 * Allocate a struct pr_transport_id_holder and setup
1747 * the dest_node_acl and dest_se_deve pointers for the
1748 * loop below.
1749 */
1750 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1751 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001752 if (!tidh_new) {
1753 pr_err("Unable to allocate tidh_new\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001754 core_scsi3_lunacl_undepend_item(dest_se_deve);
1755 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1756 core_scsi3_tpg_undepend_item(dest_tpg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001757 cmd->scsi_sense_reason =
1758 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1759 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001760 goto out;
1761 }
1762 INIT_LIST_HEAD(&tidh_new->dest_list);
1763 tidh_new->dest_tpg = dest_tpg;
1764 tidh_new->dest_node_acl = dest_node_acl;
1765 tidh_new->dest_se_deve = dest_se_deve;
1766
1767 /*
1768 * Allocate, but do NOT add the registration for the
1769 * TransportID referenced SCSI Initiator port. This
1770 * done because of the following from spc4r17 in section
1771 * 6.14.3 wrt SPEC_I_PT:
1772 *
1773 * "If a registration fails for any initiator port (e.g., if th
1774 * logical unit does not have enough resources available to
1775 * hold the registration information), no registrations shall be
1776 * made, and the command shall be terminated with
1777 * CHECK CONDITION status."
1778 *
1779 * That means we call __core_scsi3_alloc_registration() here,
1780 * and then call __core_scsi3_add_registration() in the
1781 * 2nd loop which will never fail.
1782 */
Andy Grover5951146d2011-07-19 10:26:37 +00001783 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001784 dest_node_acl, dest_se_deve, iport_ptr,
1785 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001786 if (!dest_pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001787 core_scsi3_lunacl_undepend_item(dest_se_deve);
1788 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1789 core_scsi3_tpg_undepend_item(dest_tpg);
1790 kfree(tidh_new);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001791 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1792 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001793 goto out;
1794 }
1795 tidh_new->dest_pr_reg = dest_pr_reg;
1796 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1797
1798 ptr += tid_len;
1799 tpdl -= tid_len;
1800 tid_len = 0;
1801
1802 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00001803
Andy Grover49493142012-01-16 16:57:08 -08001804 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00001805
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001806 /*
1807 * Go ahead and create a registrations from tid_dest_list for the
1808 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1809 * and dest_se_deve.
1810 *
1811 * The SA Reservation Key from the PROUT is set for the
1812 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1813 * means that the TransportID Initiator port will be
1814 * registered on all of the target ports in the SCSI target device
1815 * ALL_TG_PT=0 means the registration will only be for the
1816 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1817 * was received.
1818 */
1819 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1820 dest_tpg = tidh->dest_tpg;
1821 dest_node_acl = tidh->dest_node_acl;
1822 dest_se_deve = tidh->dest_se_deve;
1823 dest_pr_reg = tidh->dest_pr_reg;
1824 dest_local_nexus = tidh->dest_local_nexus;
1825
1826 list_del(&tidh->dest_list);
1827 kfree(tidh);
1828
1829 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1830 prf_isid = core_pr_dump_initiator_port(dest_pr_reg, &i_buf[0],
1831 PR_REG_ISID_ID_LEN);
1832
Andy Grover5951146d2011-07-19 10:26:37 +00001833 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001834 dest_pr_reg, 0, 0);
1835
Andy Grover6708bb22011-06-08 10:36:43 -07001836 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001837 " registered Transport ID for Node: %s%s Mapped LUN:"
Andy Grovere3d6f902011-07-19 08:55:10 +00001838 " %u\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001839 dest_node_acl->initiatorname, (prf_isid) ?
1840 &i_buf[0] : "", dest_se_deve->mapped_lun);
1841
1842 if (dest_local_nexus)
1843 continue;
1844
1845 core_scsi3_lunacl_undepend_item(dest_se_deve);
1846 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1847 core_scsi3_tpg_undepend_item(dest_tpg);
1848 }
1849
1850 return 0;
1851out:
Andy Grover49493142012-01-16 16:57:08 -08001852 transport_kunmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001853 /*
1854 * For the failure case, release everything from tid_dest_list
1855 * including *dest_pr_reg and the configfs dependances..
1856 */
1857 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1858 dest_tpg = tidh->dest_tpg;
1859 dest_node_acl = tidh->dest_node_acl;
1860 dest_se_deve = tidh->dest_se_deve;
1861 dest_pr_reg = tidh->dest_pr_reg;
1862 dest_local_nexus = tidh->dest_local_nexus;
1863
1864 list_del(&tidh->dest_list);
1865 kfree(tidh);
1866 /*
1867 * Release any extra ALL_TG_PT=1 registrations for
1868 * the SPEC_I_PT=1 case.
1869 */
1870 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1871 &dest_pr_reg->pr_reg_atp_list,
1872 pr_reg_atp_mem_list) {
1873 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1874 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1875 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1876 }
1877
1878 kfree(dest_pr_reg->pr_aptpl_buf);
1879 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1880
1881 if (dest_local_nexus)
1882 continue;
1883
1884 core_scsi3_lunacl_undepend_item(dest_se_deve);
1885 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1886 core_scsi3_tpg_undepend_item(dest_tpg);
1887 }
1888 return ret;
1889}
1890
1891/*
1892 * Called with struct se_device->dev_reservation_lock held
1893 */
1894static int __core_scsi3_update_aptpl_buf(
1895 struct se_device *dev,
1896 unsigned char *buf,
1897 u32 pr_aptpl_buf_len,
1898 int clear_aptpl_metadata)
1899{
1900 struct se_lun *lun;
1901 struct se_portal_group *tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00001902 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001903 struct t10_pr_registration *pr_reg;
1904 unsigned char tmp[512], isid_buf[32];
1905 ssize_t len = 0;
1906 int reg_count = 0;
1907
1908 memset(buf, 0, pr_aptpl_buf_len);
1909 /*
1910 * Called to clear metadata once APTPL has been deactivated.
1911 */
1912 if (clear_aptpl_metadata) {
1913 snprintf(buf, pr_aptpl_buf_len,
1914 "No Registrations or Reservations\n");
1915 return 0;
1916 }
1917 /*
1918 * Walk the registration list..
1919 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001920 spin_lock(&su_dev->t10_pr.registration_lock);
1921 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001922 pr_reg_list) {
1923
1924 tmp[0] = '\0';
1925 isid_buf[0] = '\0';
1926 tpg = pr_reg->pr_reg_nacl->se_tpg;
1927 lun = pr_reg->pr_reg_tg_pt_lun;
1928 /*
1929 * Write out any ISID value to APTPL metadata that was included
1930 * in the original registration.
1931 */
1932 if (pr_reg->isid_present_at_reg)
1933 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1934 pr_reg->pr_reg_isid);
1935 /*
1936 * Include special metadata if the pr_reg matches the
1937 * reservation holder.
1938 */
1939 if (dev->dev_pr_res_holder == pr_reg) {
1940 snprintf(tmp, 512, "PR_REG_START: %d"
1941 "\ninitiator_fabric=%s\n"
1942 "initiator_node=%s\n%s"
1943 "sa_res_key=%llu\n"
1944 "res_holder=1\nres_type=%02x\n"
1945 "res_scope=%02x\nres_all_tg_pt=%d\n"
1946 "mapped_lun=%u\n", reg_count,
Andy Grovere3d6f902011-07-19 08:55:10 +00001947 tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001948 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1949 pr_reg->pr_res_key, pr_reg->pr_res_type,
1950 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1951 pr_reg->pr_res_mapped_lun);
1952 } else {
1953 snprintf(tmp, 512, "PR_REG_START: %d\n"
1954 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1955 "sa_res_key=%llu\nres_holder=0\n"
1956 "res_all_tg_pt=%d\nmapped_lun=%u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001957 reg_count, tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001958 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1959 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1960 pr_reg->pr_res_mapped_lun);
1961 }
1962
Dan Carpenter60d645a2011-06-15 10:03:05 -07001963 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001964 pr_err("Unable to update renaming"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001965 " APTPL metadata\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001966 spin_unlock(&su_dev->t10_pr.registration_lock);
1967 return -EMSGSIZE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001968 }
1969 len += sprintf(buf+len, "%s", tmp);
1970
1971 /*
1972 * Include information about the associated SCSI target port.
1973 */
1974 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1975 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
Andy Grovere3d6f902011-07-19 08:55:10 +00001976 " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
1977 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1978 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001979 lun->lun_sep->sep_rtpi, lun->unpacked_lun, reg_count);
1980
Dan Carpenter60d645a2011-06-15 10:03:05 -07001981 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001982 pr_err("Unable to update renaming"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001983 " APTPL metadata\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001984 spin_unlock(&su_dev->t10_pr.registration_lock);
1985 return -EMSGSIZE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001986 }
1987 len += sprintf(buf+len, "%s", tmp);
1988 reg_count++;
1989 }
Andy Grovere3d6f902011-07-19 08:55:10 +00001990 spin_unlock(&su_dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001991
Andy Grover6708bb22011-06-08 10:36:43 -07001992 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001993 len += sprintf(buf+len, "No Registrations or Reservations");
1994
1995 return 0;
1996}
1997
1998static int core_scsi3_update_aptpl_buf(
1999 struct se_device *dev,
2000 unsigned char *buf,
2001 u32 pr_aptpl_buf_len,
2002 int clear_aptpl_metadata)
2003{
2004 int ret;
2005
2006 spin_lock(&dev->dev_reservation_lock);
2007 ret = __core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
2008 clear_aptpl_metadata);
2009 spin_unlock(&dev->dev_reservation_lock);
2010
2011 return ret;
2012}
2013
2014/*
2015 * Called with struct se_device->aptpl_file_mutex held
2016 */
2017static int __core_scsi3_write_aptpl_to_file(
2018 struct se_device *dev,
2019 unsigned char *buf,
2020 u32 pr_aptpl_buf_len)
2021{
Andy Grovere3d6f902011-07-19 08:55:10 +00002022 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002023 struct file *file;
2024 struct iovec iov[1];
2025 mm_segment_t old_fs;
2026 int flags = O_RDWR | O_CREAT | O_TRUNC;
2027 char path[512];
2028 int ret;
2029
2030 memset(iov, 0, sizeof(struct iovec));
2031 memset(path, 0, 512);
2032
Dan Carpenter60d645a2011-06-15 10:03:05 -07002033 if (strlen(&wwn->unit_serial[0]) >= 512) {
Andy Grover6708bb22011-06-08 10:36:43 -07002034 pr_err("WWN value for struct se_device does not fit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002035 " into path buffer\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00002036 return -EMSGSIZE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002037 }
2038
2039 snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]);
2040 file = filp_open(path, flags, 0600);
2041 if (IS_ERR(file) || !file || !file->f_dentry) {
Andy Grover6708bb22011-06-08 10:36:43 -07002042 pr_err("filp_open(%s) for APTPL metadata"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002043 " failed\n", path);
Roland Dreierd35212f2012-07-16 15:17:10 -07002044 return IS_ERR(file) ? PTR_ERR(file) : -ENOENT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002045 }
2046
2047 iov[0].iov_base = &buf[0];
Andy Grover6708bb22011-06-08 10:36:43 -07002048 if (!pr_aptpl_buf_len)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002049 iov[0].iov_len = (strlen(&buf[0]) + 1); /* Add extra for NULL */
2050 else
2051 iov[0].iov_len = pr_aptpl_buf_len;
2052
2053 old_fs = get_fs();
2054 set_fs(get_ds());
2055 ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
2056 set_fs(old_fs);
2057
2058 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002059 pr_debug("Error writing APTPL metadata file: %s\n", path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002060 filp_close(file, NULL);
Andy Grovere3d6f902011-07-19 08:55:10 +00002061 return -EIO;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002062 }
2063 filp_close(file, NULL);
2064
2065 return 0;
2066}
2067
2068static int core_scsi3_update_and_write_aptpl(
2069 struct se_device *dev,
2070 unsigned char *in_buf,
2071 u32 in_pr_aptpl_buf_len)
2072{
2073 unsigned char null_buf[64], *buf;
2074 u32 pr_aptpl_buf_len;
2075 int ret, clear_aptpl_metadata = 0;
2076 /*
2077 * Can be called with a NULL pointer from PROUT service action CLEAR
2078 */
Andy Grover6708bb22011-06-08 10:36:43 -07002079 if (!in_buf) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002080 memset(null_buf, 0, 64);
2081 buf = &null_buf[0];
2082 /*
2083 * This will clear the APTPL metadata to:
2084 * "No Registrations or Reservations" status
2085 */
2086 pr_aptpl_buf_len = 64;
2087 clear_aptpl_metadata = 1;
2088 } else {
2089 buf = in_buf;
2090 pr_aptpl_buf_len = in_pr_aptpl_buf_len;
2091 }
2092
2093 ret = core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
2094 clear_aptpl_metadata);
2095 if (ret != 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00002096 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002097 /*
2098 * __core_scsi3_write_aptpl_to_file() will call strlen()
2099 * on the passed buf to determine pr_aptpl_buf_len.
2100 */
2101 ret = __core_scsi3_write_aptpl_to_file(dev, buf, 0);
2102 if (ret != 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00002103 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002104
2105 return ret;
2106}
2107
2108static int core_scsi3_emulate_pro_register(
2109 struct se_cmd *cmd,
2110 u64 res_key,
2111 u64 sa_res_key,
2112 int aptpl,
2113 int all_tg_pt,
2114 int spec_i_pt,
2115 int ignore_key)
2116{
Andy Grovere3d6f902011-07-19 08:55:10 +00002117 struct se_session *se_sess = cmd->se_sess;
Andy Grover5951146d2011-07-19 10:26:37 +00002118 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002119 struct se_dev_entry *se_deve;
Andy Grovere3d6f902011-07-19 08:55:10 +00002120 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002121 struct se_portal_group *se_tpg;
2122 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp, *pr_reg_e;
Andy Grovere3d6f902011-07-19 08:55:10 +00002123 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002124 /* Used for APTPL metadata w/ UNREGISTER */
2125 unsigned char *pr_aptpl_buf = NULL;
2126 unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
2127 int pr_holder = 0, ret = 0, type;
2128
Andy Grover6708bb22011-06-08 10:36:43 -07002129 if (!se_sess || !se_lun) {
2130 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002131 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2132 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002133 }
2134 se_tpg = se_sess->se_tpg;
Jörn Engelf2083242012-03-15 15:05:40 -04002135 se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002136
Andy Grovere3d6f902011-07-19 08:55:10 +00002137 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002138 memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +00002139 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002140 PR_REG_ISID_LEN);
2141 isid_ptr = &isid_buf[0];
2142 }
2143 /*
2144 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2145 */
2146 pr_reg_e = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002147 if (!pr_reg_e) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002148 if (res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002149 pr_warn("SPC-3 PR: Reservation Key non-zero"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002150 " for SA REGISTER, returning CONFLICT\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002151 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2152 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002153 }
2154 /*
2155 * Do nothing but return GOOD status.
2156 */
Andy Grover6708bb22011-06-08 10:36:43 -07002157 if (!sa_res_key)
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002158 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002159
Andy Grover6708bb22011-06-08 10:36:43 -07002160 if (!spec_i_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002161 /*
2162 * Perform the Service Action REGISTER on the Initiator
2163 * Port Endpoint that the PRO was received from on the
2164 * Logical Unit of the SCSI device server.
2165 */
Andy Grover5951146d2011-07-19 10:26:37 +00002166 ret = core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002167 se_sess->se_node_acl, se_deve, isid_ptr,
2168 sa_res_key, all_tg_pt, aptpl,
2169 ignore_key, 0);
2170 if (ret != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002171 pr_err("Unable to allocate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002172 " struct t10_pr_registration\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002173 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2174 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002175 }
2176 } else {
2177 /*
2178 * Register both the Initiator port that received
2179 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2180 * TransportID from Parameter list and loop through
2181 * fabric dependent parameter list while calling
2182 * logic from of core_scsi3_alloc_registration() for
2183 * each TransportID provided SCSI Initiator Port/Device
2184 */
2185 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2186 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2187 if (ret != 0)
2188 return ret;
2189 }
2190 /*
2191 * Nothing left to do for the APTPL=0 case.
2192 */
Andy Grover6708bb22011-06-08 10:36:43 -07002193 if (!aptpl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002194 pr_tmpl->pr_aptpl_active = 0;
Andy Grover5951146d2011-07-19 10:26:37 +00002195 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002196 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002197 " REGISTER\n");
2198 return 0;
2199 }
2200 /*
2201 * Locate the newly allocated local I_T Nexus *pr_reg, and
2202 * update the APTPL metadata information using its
2203 * preallocated *pr_reg->pr_aptpl_buf.
2204 */
Andy Grover5951146d2011-07-19 10:26:37 +00002205 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002206 se_sess->se_node_acl, se_sess);
2207
Andy Grover5951146d2011-07-19 10:26:37 +00002208 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002209 &pr_reg->pr_aptpl_buf[0],
2210 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07002211 if (!ret) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002212 pr_tmpl->pr_aptpl_active = 1;
Andy Grover6708bb22011-06-08 10:36:43 -07002213 pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002214 }
2215
2216 core_scsi3_put_pr_reg(pr_reg);
2217 return ret;
2218 } else {
2219 /*
2220 * Locate the existing *pr_reg via struct se_node_acl pointers
2221 */
2222 pr_reg = pr_reg_e;
2223 type = pr_reg->pr_res_type;
2224
Andy Grover6708bb22011-06-08 10:36:43 -07002225 if (!ignore_key) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002226 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002227 pr_err("SPC-3 PR REGISTER: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002228 " res_key: 0x%016Lx does not match"
2229 " existing SA REGISTER res_key:"
2230 " 0x%016Lx\n", res_key,
2231 pr_reg->pr_res_key);
2232 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002233 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2234 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002235 }
2236 }
2237 if (spec_i_pt) {
Andy Grover6708bb22011-06-08 10:36:43 -07002238 pr_err("SPC-3 PR UNREGISTER: SPEC_I_PT"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002239 " set while sa_res_key=0\n");
2240 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002241 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2242 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002243 }
2244 /*
2245 * An existing ALL_TG_PT=1 registration being released
2246 * must also set ALL_TG_PT=1 in the incoming PROUT.
2247 */
2248 if (pr_reg->pr_reg_all_tg_pt && !(all_tg_pt)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002249 pr_err("SPC-3 PR UNREGISTER: ALL_TG_PT=1"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002250 " registration exists, but ALL_TG_PT=1 bit not"
2251 " present in received PROUT\n");
2252 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002253 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2254 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002255 }
2256 /*
2257 * Allocate APTPL metadata buffer used for UNREGISTER ops
2258 */
2259 if (aptpl) {
2260 pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len,
2261 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002262 if (!pr_aptpl_buf) {
2263 pr_err("Unable to allocate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002264 " pr_aptpl_buf\n");
2265 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002266 cmd->scsi_sense_reason =
2267 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2268 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002269 }
2270 }
2271 /*
2272 * sa_res_key=0 Unregister Reservation Key for registered I_T
2273 * Nexus sa_res_key=1 Change Reservation Key for registered I_T
2274 * Nexus.
2275 */
Andy Grover6708bb22011-06-08 10:36:43 -07002276 if (!sa_res_key) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002277 pr_holder = core_scsi3_check_implict_release(
Andy Grover5951146d2011-07-19 10:26:37 +00002278 cmd->se_dev, pr_reg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002279 if (pr_holder < 0) {
2280 kfree(pr_aptpl_buf);
2281 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002282 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2283 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002284 }
2285
2286 spin_lock(&pr_tmpl->registration_lock);
2287 /*
2288 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2289 * and matching pr_res_key.
2290 */
2291 if (pr_reg->pr_reg_all_tg_pt) {
2292 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2293 &pr_tmpl->registration_list,
2294 pr_reg_list) {
2295
Andy Grover6708bb22011-06-08 10:36:43 -07002296 if (!pr_reg_p->pr_reg_all_tg_pt)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002297 continue;
2298
2299 if (pr_reg_p->pr_res_key != res_key)
2300 continue;
2301
2302 if (pr_reg == pr_reg_p)
2303 continue;
2304
2305 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2306 pr_reg_p->pr_reg_nacl->initiatorname))
2307 continue;
2308
2309 __core_scsi3_free_registration(dev,
2310 pr_reg_p, NULL, 0);
2311 }
2312 }
2313 /*
2314 * Release the calling I_T Nexus registration now..
2315 */
Andy Grover5951146d2011-07-19 10:26:37 +00002316 __core_scsi3_free_registration(cmd->se_dev, pr_reg,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002317 NULL, 1);
2318 /*
2319 * From spc4r17, section 5.7.11.3 Unregistering
2320 *
2321 * If the persistent reservation is a registrants only
2322 * type, the device server shall establish a unit
2323 * attention condition for the initiator port associated
2324 * with every registered I_T nexus except for the I_T
2325 * nexus on which the PERSISTENT RESERVE OUT command was
2326 * received, with the additional sense code set to
2327 * RESERVATIONS RELEASED.
2328 */
2329 if (pr_holder &&
2330 ((type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
2331 (type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY))) {
2332 list_for_each_entry(pr_reg_p,
2333 &pr_tmpl->registration_list,
2334 pr_reg_list) {
2335
2336 core_scsi3_ua_allocate(
2337 pr_reg_p->pr_reg_nacl,
2338 pr_reg_p->pr_res_mapped_lun,
2339 0x2A,
2340 ASCQ_2AH_RESERVATIONS_RELEASED);
2341 }
2342 }
2343 spin_unlock(&pr_tmpl->registration_lock);
2344
Andy Grover6708bb22011-06-08 10:36:43 -07002345 if (!aptpl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002346 pr_tmpl->pr_aptpl_active = 0;
2347 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002348 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002349 " for UNREGISTER\n");
2350 return 0;
2351 }
2352
2353 ret = core_scsi3_update_and_write_aptpl(dev,
2354 &pr_aptpl_buf[0],
2355 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07002356 if (!ret) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002357 pr_tmpl->pr_aptpl_active = 1;
Andy Grover6708bb22011-06-08 10:36:43 -07002358 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002359 " for UNREGISTER\n");
2360 }
2361
2362 kfree(pr_aptpl_buf);
2363 return ret;
2364 } else {
2365 /*
2366 * Increment PRgeneration counter for struct se_device"
2367 * upon a successful REGISTER, see spc4r17 section 6.3.2
2368 * READ_KEYS service action.
2369 */
2370 pr_reg->pr_res_generation = core_scsi3_pr_generation(
Andy Grover5951146d2011-07-19 10:26:37 +00002371 cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002372 pr_reg->pr_res_key = sa_res_key;
Andy Grover6708bb22011-06-08 10:36:43 -07002373 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002374 " Key for %s to: 0x%016Lx PRgeneration:"
Andy Grovere3d6f902011-07-19 08:55:10 +00002375 " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002376 (ignore_key) ? "_AND_IGNORE_EXISTING_KEY" : "",
2377 pr_reg->pr_reg_nacl->initiatorname,
2378 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2379
Andy Grover6708bb22011-06-08 10:36:43 -07002380 if (!aptpl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002381 pr_tmpl->pr_aptpl_active = 0;
2382 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2383 core_scsi3_put_pr_reg(pr_reg);
Andy Grover6708bb22011-06-08 10:36:43 -07002384 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002385 " for REGISTER\n");
2386 return 0;
2387 }
2388
2389 ret = core_scsi3_update_and_write_aptpl(dev,
2390 &pr_aptpl_buf[0],
2391 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07002392 if (!ret) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002393 pr_tmpl->pr_aptpl_active = 1;
Andy Grover6708bb22011-06-08 10:36:43 -07002394 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002395 " for REGISTER\n");
2396 }
2397
2398 kfree(pr_aptpl_buf);
2399 core_scsi3_put_pr_reg(pr_reg);
2400 }
2401 }
2402 return 0;
2403}
2404
2405unsigned char *core_scsi3_pr_dump_type(int type)
2406{
2407 switch (type) {
2408 case PR_TYPE_WRITE_EXCLUSIVE:
2409 return "Write Exclusive Access";
2410 case PR_TYPE_EXCLUSIVE_ACCESS:
2411 return "Exclusive Access";
2412 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2413 return "Write Exclusive Access, Registrants Only";
2414 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2415 return "Exclusive Access, Registrants Only";
2416 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2417 return "Write Exclusive Access, All Registrants";
2418 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2419 return "Exclusive Access, All Registrants";
2420 default:
2421 break;
2422 }
2423
2424 return "Unknown SPC-3 PR Type";
2425}
2426
2427static int core_scsi3_pro_reserve(
2428 struct se_cmd *cmd,
2429 struct se_device *dev,
2430 int type,
2431 int scope,
2432 u64 res_key)
2433{
Andy Grovere3d6f902011-07-19 08:55:10 +00002434 struct se_session *se_sess = cmd->se_sess;
Andy Grovere3d6f902011-07-19 08:55:10 +00002435 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002436 struct t10_pr_registration *pr_reg, *pr_res_holder;
Andy Grovere3d6f902011-07-19 08:55:10 +00002437 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002438 char i_buf[PR_REG_ISID_ID_LEN];
2439 int ret, prf_isid;
2440
2441 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2442
Andy Grover6708bb22011-06-08 10:36:43 -07002443 if (!se_sess || !se_lun) {
2444 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002445 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2446 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002447 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002448 /*
2449 * Locate the existing *pr_reg via struct se_node_acl pointers
2450 */
Andy Grover5951146d2011-07-19 10:26:37 +00002451 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002452 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002453 if (!pr_reg) {
2454 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002455 " PR_REGISTERED *pr_reg for RESERVE\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002456 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2457 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002458 }
2459 /*
2460 * From spc4r17 Section 5.7.9: Reserving:
2461 *
2462 * An application client creates a persistent reservation by issuing
2463 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2464 * a registered I_T nexus with the following parameters:
2465 * a) RESERVATION KEY set to the value of the reservation key that is
2466 * registered with the logical unit for the I_T nexus; and
2467 */
2468 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002469 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002470 " does not match existing SA REGISTER res_key:"
2471 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2472 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002473 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2474 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002475 }
2476 /*
2477 * From spc4r17 Section 5.7.9: Reserving:
2478 *
2479 * From above:
2480 * b) TYPE field and SCOPE field set to the persistent reservation
2481 * being created.
2482 *
2483 * Only one persistent reservation is allowed at a time per logical unit
2484 * and that persistent reservation has a scope of LU_SCOPE.
2485 */
2486 if (scope != PR_SCOPE_LU_SCOPE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002487 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002488 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002489 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2490 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002491 }
2492 /*
2493 * See if we have an existing PR reservation holder pointer at
2494 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2495 * *pr_res_holder.
2496 */
2497 spin_lock(&dev->dev_reservation_lock);
2498 pr_res_holder = dev->dev_pr_res_holder;
Andy Groveree1b1b92012-07-12 17:34:54 -07002499 if (pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002500 /*
2501 * From spc4r17 Section 5.7.9: Reserving:
2502 *
2503 * If the device server receives a PERSISTENT RESERVE OUT
2504 * command from an I_T nexus other than a persistent reservation
2505 * holder (see 5.7.10) that attempts to create a persistent
2506 * reservation when a persistent reservation already exists for
2507 * the logical unit, then the command shall be completed with
2508 * RESERVATION CONFLICT status.
2509 */
2510 if (pr_res_holder != pr_reg) {
2511 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002512 pr_err("SPC-3 PR: Attempted RESERVE from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002513 " [%s]: %s while reservation already held by"
2514 " [%s]: %s, returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002515 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002516 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002517 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002518 pr_res_holder->pr_reg_nacl->initiatorname);
2519
2520 spin_unlock(&dev->dev_reservation_lock);
2521 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002522 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2523 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002524 }
2525 /*
2526 * From spc4r17 Section 5.7.9: Reserving:
2527 *
2528 * If a persistent reservation holder attempts to modify the
2529 * type or scope of an existing persistent reservation, the
2530 * command shall be completed with RESERVATION CONFLICT status.
2531 */
2532 if ((pr_res_holder->pr_res_type != type) ||
2533 (pr_res_holder->pr_res_scope != scope)) {
2534 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002535 pr_err("SPC-3 PR: Attempted RESERVE from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002536 " [%s]: %s trying to change TYPE and/or SCOPE,"
2537 " while reservation already held by [%s]: %s,"
2538 " returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002539 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002540 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002541 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002542 pr_res_holder->pr_reg_nacl->initiatorname);
2543
2544 spin_unlock(&dev->dev_reservation_lock);
2545 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002546 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2547 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002548 }
2549 /*
2550 * From spc4r17 Section 5.7.9: Reserving:
2551 *
2552 * If the device server receives a PERSISTENT RESERVE OUT
2553 * command with RESERVE service action where the TYPE field and
2554 * the SCOPE field contain the same values as the existing type
2555 * and scope from a persistent reservation holder, it shall not
2556 * make any change to the existing persistent reservation and
2557 * shall completethe command with GOOD status.
2558 */
2559 spin_unlock(&dev->dev_reservation_lock);
2560 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002561 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002562 }
2563 /*
2564 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2565 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2566 */
2567 pr_reg->pr_res_scope = scope;
2568 pr_reg->pr_res_type = type;
2569 pr_reg->pr_res_holder = 1;
2570 dev->dev_pr_res_holder = pr_reg;
2571 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2572 PR_REG_ISID_ID_LEN);
2573
Andy Grover6708bb22011-06-08 10:36:43 -07002574 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002575 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002576 cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002577 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002578 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002579 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002580 se_sess->se_node_acl->initiatorname,
2581 (prf_isid) ? &i_buf[0] : "");
2582 spin_unlock(&dev->dev_reservation_lock);
2583
2584 if (pr_tmpl->pr_aptpl_active) {
Andy Grover5951146d2011-07-19 10:26:37 +00002585 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002586 &pr_reg->pr_aptpl_buf[0],
2587 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07002588 if (!ret)
2589 pr_debug("SPC-3 PR: Updated APTPL metadata"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002590 " for RESERVE\n");
2591 }
2592
2593 core_scsi3_put_pr_reg(pr_reg);
2594 return 0;
2595}
2596
2597static int core_scsi3_emulate_pro_reserve(
2598 struct se_cmd *cmd,
2599 int type,
2600 int scope,
2601 u64 res_key)
2602{
2603 struct se_device *dev = cmd->se_dev;
2604 int ret = 0;
2605
2606 switch (type) {
2607 case PR_TYPE_WRITE_EXCLUSIVE:
2608 case PR_TYPE_EXCLUSIVE_ACCESS:
2609 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2610 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2611 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2612 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2613 ret = core_scsi3_pro_reserve(cmd, dev, type, scope, res_key);
2614 break;
2615 default:
Andy Grover6708bb22011-06-08 10:36:43 -07002616 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002617 " 0x%02x\n", type);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002618 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2619 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002620 }
2621
2622 return ret;
2623}
2624
2625/*
2626 * Called with struct se_device->dev_reservation_lock held.
2627 */
2628static void __core_scsi3_complete_pro_release(
2629 struct se_device *dev,
2630 struct se_node_acl *se_nacl,
2631 struct t10_pr_registration *pr_reg,
2632 int explict)
2633{
2634 struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2635 char i_buf[PR_REG_ISID_ID_LEN];
2636 int prf_isid;
2637
2638 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2639 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2640 PR_REG_ISID_ID_LEN);
2641 /*
2642 * Go ahead and release the current PR reservation holder.
2643 */
2644 dev->dev_pr_res_holder = NULL;
2645
Andy Grover6708bb22011-06-08 10:36:43 -07002646 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002647 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2648 tfo->get_fabric_name(), (explict) ? "explict" : "implict",
2649 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
2650 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002651 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002652 tfo->get_fabric_name(), se_nacl->initiatorname,
2653 (prf_isid) ? &i_buf[0] : "");
2654 /*
2655 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2656 */
2657 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2658}
2659
2660static int core_scsi3_emulate_pro_release(
2661 struct se_cmd *cmd,
2662 int type,
2663 int scope,
2664 u64 res_key)
2665{
2666 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00002667 struct se_session *se_sess = cmd->se_sess;
2668 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002669 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
Andy Grovere3d6f902011-07-19 08:55:10 +00002670 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002671 int ret, all_reg = 0;
2672
Andy Grover6708bb22011-06-08 10:36:43 -07002673 if (!se_sess || !se_lun) {
2674 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002675 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2676 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002677 }
2678 /*
2679 * Locate the existing *pr_reg via struct se_node_acl pointers
2680 */
2681 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002682 if (!pr_reg) {
2683 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002684 " PR_REGISTERED *pr_reg for RELEASE\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002685 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2686 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002687 }
2688 /*
2689 * From spc4r17 Section 5.7.11.2 Releasing:
2690 *
2691 * If there is no persistent reservation or in response to a persistent
2692 * reservation release request from a registered I_T nexus that is not a
2693 * persistent reservation holder (see 5.7.10), the device server shall
2694 * do the following:
2695 *
2696 * a) Not release the persistent reservation, if any;
2697 * b) Not remove any registrations; and
2698 * c) Complete the command with GOOD status.
2699 */
2700 spin_lock(&dev->dev_reservation_lock);
2701 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07002702 if (!pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002703 /*
2704 * No persistent reservation, return GOOD status.
2705 */
2706 spin_unlock(&dev->dev_reservation_lock);
2707 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002708 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002709 }
2710 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2711 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
2712 all_reg = 1;
2713
2714 if ((all_reg == 0) && (pr_res_holder != pr_reg)) {
2715 /*
2716 * Non 'All Registrants' PR Type cases..
2717 * Release request from a registered I_T nexus that is not a
2718 * persistent reservation holder. return GOOD status.
2719 */
2720 spin_unlock(&dev->dev_reservation_lock);
2721 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002722 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002723 }
2724 /*
2725 * From spc4r17 Section 5.7.11.2 Releasing:
2726 *
2727 * Only the persistent reservation holder (see 5.7.10) is allowed to
2728 * release a persistent reservation.
2729 *
2730 * An application client releases the persistent reservation by issuing
2731 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2732 * an I_T nexus that is a persistent reservation holder with the
2733 * following parameters:
2734 *
2735 * a) RESERVATION KEY field set to the value of the reservation key
2736 * that is registered with the logical unit for the I_T nexus;
2737 */
2738 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002739 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002740 " does not match existing SA REGISTER res_key:"
2741 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2742 spin_unlock(&dev->dev_reservation_lock);
2743 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002744 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2745 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002746 }
2747 /*
2748 * From spc4r17 Section 5.7.11.2 Releasing and above:
2749 *
2750 * b) TYPE field and SCOPE field set to match the persistent
2751 * reservation being released.
2752 */
2753 if ((pr_res_holder->pr_res_type != type) ||
2754 (pr_res_holder->pr_res_scope != scope)) {
2755 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002756 pr_err("SPC-3 PR RELEASE: Attempted to release"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002757 " reservation from [%s]: %s with different TYPE "
2758 "and/or SCOPE while reservation already held by"
2759 " [%s]: %s, returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002760 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002761 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002762 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002763 pr_res_holder->pr_reg_nacl->initiatorname);
2764
2765 spin_unlock(&dev->dev_reservation_lock);
2766 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002767 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2768 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002769 }
2770 /*
2771 * In response to a persistent reservation release request from the
2772 * persistent reservation holder the device server shall perform a
2773 * release by doing the following as an uninterrupted series of actions:
2774 * a) Release the persistent reservation;
2775 * b) Not remove any registration(s);
2776 * c) If the released persistent reservation is a registrants only type
2777 * or all registrants type persistent reservation,
2778 * the device server shall establish a unit attention condition for
2779 * the initiator port associated with every regis-
2780 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2781 * RESERVE OUT command with RELEASE service action was received,
2782 * with the additional sense code set to RESERVATIONS RELEASED; and
2783 * d) If the persistent reservation is of any other type, the device
2784 * server shall not establish a unit attention condition.
2785 */
2786 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
2787 pr_reg, 1);
2788
2789 spin_unlock(&dev->dev_reservation_lock);
2790
2791 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2792 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2793 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2794 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2795 /*
2796 * If no UNIT ATTENTION conditions will be established for
2797 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2798 * go ahead and check for APTPL=1 update+write below
2799 */
2800 goto write_aptpl;
2801 }
2802
2803 spin_lock(&pr_tmpl->registration_lock);
2804 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2805 pr_reg_list) {
2806 /*
2807 * Do not establish a UNIT ATTENTION condition
2808 * for the calling I_T Nexus
2809 */
2810 if (pr_reg_p == pr_reg)
2811 continue;
2812
2813 core_scsi3_ua_allocate(pr_reg_p->pr_reg_nacl,
2814 pr_reg_p->pr_res_mapped_lun,
2815 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2816 }
2817 spin_unlock(&pr_tmpl->registration_lock);
2818
2819write_aptpl:
2820 if (pr_tmpl->pr_aptpl_active) {
Andy Grover5951146d2011-07-19 10:26:37 +00002821 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002822 &pr_reg->pr_aptpl_buf[0],
2823 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07002824 if (!ret)
2825 pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002826 }
2827
2828 core_scsi3_put_pr_reg(pr_reg);
2829 return 0;
2830}
2831
2832static int core_scsi3_emulate_pro_clear(
2833 struct se_cmd *cmd,
2834 u64 res_key)
2835{
2836 struct se_device *dev = cmd->se_dev;
2837 struct se_node_acl *pr_reg_nacl;
Andy Grovere3d6f902011-07-19 08:55:10 +00002838 struct se_session *se_sess = cmd->se_sess;
2839 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002840 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2841 u32 pr_res_mapped_lun = 0;
2842 int calling_it_nexus = 0;
2843 /*
2844 * Locate the existing *pr_reg via struct se_node_acl pointers
2845 */
Andy Grover5951146d2011-07-19 10:26:37 +00002846 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002847 se_sess->se_node_acl, se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002848 if (!pr_reg_n) {
2849 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002850 " PR_REGISTERED *pr_reg for CLEAR\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002851 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2852 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002853 }
2854 /*
2855 * From spc4r17 section 5.7.11.6, Clearing:
2856 *
2857 * Any application client may release the persistent reservation and
2858 * remove all registrations from a device server by issuing a
2859 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2860 * registered I_T nexus with the following parameter:
2861 *
2862 * a) RESERVATION KEY field set to the value of the reservation key
2863 * that is registered with the logical unit for the I_T nexus.
2864 */
2865 if (res_key != pr_reg_n->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002866 pr_err("SPC-3 PR REGISTER: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002867 " res_key: 0x%016Lx does not match"
2868 " existing SA REGISTER res_key:"
2869 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2870 core_scsi3_put_pr_reg(pr_reg_n);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002871 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2872 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002873 }
2874 /*
2875 * a) Release the persistent reservation, if any;
2876 */
2877 spin_lock(&dev->dev_reservation_lock);
2878 pr_res_holder = dev->dev_pr_res_holder;
2879 if (pr_res_holder) {
2880 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2881 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
2882 pr_res_holder, 0);
2883 }
2884 spin_unlock(&dev->dev_reservation_lock);
2885 /*
2886 * b) Remove all registration(s) (see spc4r17 5.7.7);
2887 */
2888 spin_lock(&pr_tmpl->registration_lock);
2889 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2890 &pr_tmpl->registration_list, pr_reg_list) {
2891
2892 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2893 pr_reg_nacl = pr_reg->pr_reg_nacl;
2894 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2895 __core_scsi3_free_registration(dev, pr_reg, NULL,
2896 calling_it_nexus);
2897 /*
2898 * e) Establish a unit attention condition for the initiator
2899 * port associated with every registered I_T nexus other
2900 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2901 * command with CLEAR service action was received, with the
2902 * additional sense code set to RESERVATIONS PREEMPTED.
2903 */
Andy Grover6708bb22011-06-08 10:36:43 -07002904 if (!calling_it_nexus)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002905 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun,
2906 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2907 }
2908 spin_unlock(&pr_tmpl->registration_lock);
2909
Andy Grover6708bb22011-06-08 10:36:43 -07002910 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002911 cmd->se_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002912
2913 if (pr_tmpl->pr_aptpl_active) {
Andy Grover5951146d2011-07-19 10:26:37 +00002914 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002915 pr_debug("SPC-3 PR: Updated APTPL metadata"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002916 " for CLEAR\n");
2917 }
2918
2919 core_scsi3_pr_generation(dev);
2920 return 0;
2921}
2922
2923/*
2924 * Called with struct se_device->dev_reservation_lock held.
2925 */
2926static void __core_scsi3_complete_pro_preempt(
2927 struct se_device *dev,
2928 struct t10_pr_registration *pr_reg,
2929 struct list_head *preempt_and_abort_list,
2930 int type,
2931 int scope,
2932 int abort)
2933{
2934 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
2935 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2936 char i_buf[PR_REG_ISID_ID_LEN];
2937 int prf_isid;
2938
2939 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2940 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2941 PR_REG_ISID_ID_LEN);
2942 /*
2943 * Do an implict RELEASE of the existing reservation.
2944 */
2945 if (dev->dev_pr_res_holder)
2946 __core_scsi3_complete_pro_release(dev, nacl,
2947 dev->dev_pr_res_holder, 0);
2948
2949 dev->dev_pr_res_holder = pr_reg;
2950 pr_reg->pr_res_holder = 1;
2951 pr_reg->pr_res_type = type;
2952 pr_reg->pr_res_scope = scope;
2953
Andy Grover6708bb22011-06-08 10:36:43 -07002954 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002955 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2956 tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2957 core_scsi3_pr_dump_type(type),
2958 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002959 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002960 tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2961 nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
2962 /*
2963 * For PREEMPT_AND_ABORT, add the preempting reservation's
2964 * struct t10_pr_registration to the list that will be compared
2965 * against received CDBs..
2966 */
2967 if (preempt_and_abort_list)
2968 list_add_tail(&pr_reg->pr_reg_abort_list,
2969 preempt_and_abort_list);
2970}
2971
2972static void core_scsi3_release_preempt_and_abort(
2973 struct list_head *preempt_and_abort_list,
2974 struct t10_pr_registration *pr_reg_holder)
2975{
2976 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2977
2978 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2979 pr_reg_abort_list) {
2980
2981 list_del(&pr_reg->pr_reg_abort_list);
2982 if (pr_reg_holder == pr_reg)
2983 continue;
2984 if (pr_reg->pr_res_holder) {
Andy Grover6708bb22011-06-08 10:36:43 -07002985 pr_warn("pr_reg->pr_res_holder still set\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002986 continue;
2987 }
2988
2989 pr_reg->pr_reg_deve = NULL;
2990 pr_reg->pr_reg_nacl = NULL;
2991 kfree(pr_reg->pr_aptpl_buf);
2992 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2993 }
2994}
2995
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002996static int core_scsi3_pro_preempt(
2997 struct se_cmd *cmd,
2998 int type,
2999 int scope,
3000 u64 res_key,
3001 u64 sa_res_key,
3002 int abort)
3003{
Andy Grover5951146d2011-07-19 10:26:37 +00003004 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003005 struct se_node_acl *pr_reg_nacl;
Andy Grovere3d6f902011-07-19 08:55:10 +00003006 struct se_session *se_sess = cmd->se_sess;
Roland Dreierd0f474e2012-01-12 10:41:18 -08003007 LIST_HEAD(preempt_and_abort_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003008 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
Andy Grovere3d6f902011-07-19 08:55:10 +00003009 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003010 u32 pr_res_mapped_lun = 0;
3011 int all_reg = 0, calling_it_nexus = 0, released_regs = 0;
3012 int prh_type = 0, prh_scope = 0, ret;
3013
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003014 if (!se_sess) {
3015 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3016 return -EINVAL;
3017 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003018
Andy Grover5951146d2011-07-19 10:26:37 +00003019 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003020 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07003021 if (!pr_reg_n) {
3022 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003023 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
3024 (abort) ? "_AND_ABORT" : "");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003025 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3026 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003027 }
3028 if (pr_reg_n->pr_res_key != res_key) {
3029 core_scsi3_put_pr_reg(pr_reg_n);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003030 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3031 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003032 }
3033 if (scope != PR_SCOPE_LU_SCOPE) {
Andy Grover6708bb22011-06-08 10:36:43 -07003034 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003035 core_scsi3_put_pr_reg(pr_reg_n);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003036 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3037 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003038 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003039
3040 spin_lock(&dev->dev_reservation_lock);
3041 pr_res_holder = dev->dev_pr_res_holder;
3042 if (pr_res_holder &&
3043 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3044 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
3045 all_reg = 1;
3046
Andy Grover6708bb22011-06-08 10:36:43 -07003047 if (!all_reg && !sa_res_key) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003048 spin_unlock(&dev->dev_reservation_lock);
3049 core_scsi3_put_pr_reg(pr_reg_n);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003050 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3051 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003052 }
3053 /*
3054 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
3055 *
3056 * If the SERVICE ACTION RESERVATION KEY field does not identify a
3057 * persistent reservation holder or there is no persistent reservation
3058 * holder (i.e., there is no persistent reservation), then the device
3059 * server shall perform a preempt by doing the following in an
3060 * uninterrupted series of actions. (See below..)
3061 */
Andy Grover6708bb22011-06-08 10:36:43 -07003062 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003063 /*
3064 * No existing or SA Reservation Key matching reservations..
3065 *
3066 * PROUT SA PREEMPT with All Registrant type reservations are
3067 * allowed to be processed without a matching SA Reservation Key
3068 */
3069 spin_lock(&pr_tmpl->registration_lock);
3070 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3071 &pr_tmpl->registration_list, pr_reg_list) {
3072 /*
3073 * Removing of registrations in non all registrants
3074 * type reservations without a matching SA reservation
3075 * key.
3076 *
3077 * a) Remove the registrations for all I_T nexuses
3078 * specified by the SERVICE ACTION RESERVATION KEY
3079 * field;
3080 * b) Ignore the contents of the SCOPE and TYPE fields;
3081 * c) Process tasks as defined in 5.7.1; and
3082 * d) Establish a unit attention condition for the
3083 * initiator port associated with every I_T nexus
3084 * that lost its registration other than the I_T
3085 * nexus on which the PERSISTENT RESERVE OUT command
3086 * was received, with the additional sense code set
3087 * to REGISTRATIONS PREEMPTED.
3088 */
Andy Grover6708bb22011-06-08 10:36:43 -07003089 if (!all_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003090 if (pr_reg->pr_res_key != sa_res_key)
3091 continue;
3092
3093 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3094 pr_reg_nacl = pr_reg->pr_reg_nacl;
3095 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3096 __core_scsi3_free_registration(dev, pr_reg,
3097 (abort) ? &preempt_and_abort_list :
3098 NULL, calling_it_nexus);
3099 released_regs++;
3100 } else {
3101 /*
3102 * Case for any existing all registrants type
3103 * reservation, follow logic in spc4r17 section
3104 * 5.7.11.4 Preempting, Table 52 and Figure 7.
3105 *
3106 * For a ZERO SA Reservation key, release
3107 * all other registrations and do an implict
3108 * release of active persistent reservation.
3109 *
3110 * For a non-ZERO SA Reservation key, only
3111 * release the matching reservation key from
3112 * registrations.
3113 */
3114 if ((sa_res_key) &&
3115 (pr_reg->pr_res_key != sa_res_key))
3116 continue;
3117
3118 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3119 if (calling_it_nexus)
3120 continue;
3121
3122 pr_reg_nacl = pr_reg->pr_reg_nacl;
3123 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3124 __core_scsi3_free_registration(dev, pr_reg,
3125 (abort) ? &preempt_and_abort_list :
3126 NULL, 0);
3127 released_regs++;
3128 }
Andy Grover6708bb22011-06-08 10:36:43 -07003129 if (!calling_it_nexus)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003130 core_scsi3_ua_allocate(pr_reg_nacl,
3131 pr_res_mapped_lun, 0x2A,
Marco Sanvido9e08e342012-01-03 17:12:57 -08003132 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003133 }
3134 spin_unlock(&pr_tmpl->registration_lock);
3135 /*
3136 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
3137 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
3138 * RESERVATION KEY field to a value that does not match any
3139 * registered reservation key, then the device server shall
3140 * complete the command with RESERVATION CONFLICT status.
3141 */
Andy Grover6708bb22011-06-08 10:36:43 -07003142 if (!released_regs) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003143 spin_unlock(&dev->dev_reservation_lock);
3144 core_scsi3_put_pr_reg(pr_reg_n);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003145 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3146 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003147 }
3148 /*
3149 * For an existing all registrants type reservation
3150 * with a zero SA rservation key, preempt the existing
3151 * reservation with the new PR type and scope.
3152 */
3153 if (pr_res_holder && all_reg && !(sa_res_key)) {
3154 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3155 (abort) ? &preempt_and_abort_list : NULL,
3156 type, scope, abort);
3157
3158 if (abort)
3159 core_scsi3_release_preempt_and_abort(
3160 &preempt_and_abort_list, pr_reg_n);
3161 }
3162 spin_unlock(&dev->dev_reservation_lock);
3163
3164 if (pr_tmpl->pr_aptpl_active) {
Andy Grover5951146d2011-07-19 10:26:37 +00003165 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003166 &pr_reg_n->pr_aptpl_buf[0],
3167 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07003168 if (!ret)
3169 pr_debug("SPC-3 PR: Updated APTPL"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003170 " metadata for PREEMPT%s\n", (abort) ?
3171 "_AND_ABORT" : "");
3172 }
3173
3174 core_scsi3_put_pr_reg(pr_reg_n);
Andy Grover5951146d2011-07-19 10:26:37 +00003175 core_scsi3_pr_generation(cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003176 return 0;
3177 }
3178 /*
3179 * The PREEMPTing SA reservation key matches that of the
3180 * existing persistent reservation, first, we check if
3181 * we are preempting our own reservation.
3182 * From spc4r17, section 5.7.11.4.3 Preempting
3183 * persistent reservations and registration handling
3184 *
3185 * If an all registrants persistent reservation is not
3186 * present, it is not an error for the persistent
3187 * reservation holder to preempt itself (i.e., a
3188 * PERSISTENT RESERVE OUT with a PREEMPT service action
3189 * or a PREEMPT AND ABORT service action with the
3190 * SERVICE ACTION RESERVATION KEY value equal to the
3191 * persistent reservation holder's reservation key that
3192 * is received from the persistent reservation holder).
3193 * In that case, the device server shall establish the
3194 * new persistent reservation and maintain the
3195 * registration.
3196 */
3197 prh_type = pr_res_holder->pr_res_type;
3198 prh_scope = pr_res_holder->pr_res_scope;
3199 /*
3200 * If the SERVICE ACTION RESERVATION KEY field identifies a
3201 * persistent reservation holder (see 5.7.10), the device
3202 * server shall perform a preempt by doing the following as
3203 * an uninterrupted series of actions:
3204 *
3205 * a) Release the persistent reservation for the holder
3206 * identified by the SERVICE ACTION RESERVATION KEY field;
3207 */
3208 if (pr_reg_n != pr_res_holder)
3209 __core_scsi3_complete_pro_release(dev,
3210 pr_res_holder->pr_reg_nacl,
3211 dev->dev_pr_res_holder, 0);
3212 /*
3213 * b) Remove the registrations for all I_T nexuses identified
3214 * by the SERVICE ACTION RESERVATION KEY field, except the
3215 * I_T nexus that is being used for the PERSISTENT RESERVE
3216 * OUT command. If an all registrants persistent reservation
3217 * is present and the SERVICE ACTION RESERVATION KEY field
3218 * is set to zero, then all registrations shall be removed
3219 * except for that of the I_T nexus that is being used for
3220 * the PERSISTENT RESERVE OUT command;
3221 */
3222 spin_lock(&pr_tmpl->registration_lock);
3223 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3224 &pr_tmpl->registration_list, pr_reg_list) {
3225
3226 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3227 if (calling_it_nexus)
3228 continue;
3229
3230 if (pr_reg->pr_res_key != sa_res_key)
3231 continue;
3232
3233 pr_reg_nacl = pr_reg->pr_reg_nacl;
3234 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3235 __core_scsi3_free_registration(dev, pr_reg,
3236 (abort) ? &preempt_and_abort_list : NULL,
3237 calling_it_nexus);
3238 /*
3239 * e) Establish a unit attention condition for the initiator
3240 * port associated with every I_T nexus that lost its
3241 * persistent reservation and/or registration, with the
3242 * additional sense code set to REGISTRATIONS PREEMPTED;
3243 */
3244 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
Marco Sanvido9e08e342012-01-03 17:12:57 -08003245 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003246 }
3247 spin_unlock(&pr_tmpl->registration_lock);
3248 /*
3249 * c) Establish a persistent reservation for the preempting
3250 * I_T nexus using the contents of the SCOPE and TYPE fields;
3251 */
3252 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3253 (abort) ? &preempt_and_abort_list : NULL,
3254 type, scope, abort);
3255 /*
3256 * d) Process tasks as defined in 5.7.1;
3257 * e) See above..
3258 * f) If the type or scope has changed, then for every I_T nexus
3259 * whose reservation key was not removed, except for the I_T
3260 * nexus on which the PERSISTENT RESERVE OUT command was
3261 * received, the device server shall establish a unit
3262 * attention condition for the initiator port associated with
3263 * that I_T nexus, with the additional sense code set to
3264 * RESERVATIONS RELEASED. If the type or scope have not
3265 * changed, then no unit attention condition(s) shall be
3266 * established for this reason.
3267 */
3268 if ((prh_type != type) || (prh_scope != scope)) {
3269 spin_lock(&pr_tmpl->registration_lock);
3270 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3271 &pr_tmpl->registration_list, pr_reg_list) {
3272
3273 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3274 if (calling_it_nexus)
3275 continue;
3276
3277 core_scsi3_ua_allocate(pr_reg->pr_reg_nacl,
3278 pr_reg->pr_res_mapped_lun, 0x2A,
3279 ASCQ_2AH_RESERVATIONS_RELEASED);
3280 }
3281 spin_unlock(&pr_tmpl->registration_lock);
3282 }
3283 spin_unlock(&dev->dev_reservation_lock);
3284 /*
3285 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3286 * All received CDBs for the matching existing reservation and
3287 * registrations undergo ABORT_TASK logic.
3288 *
3289 * From there, core_scsi3_release_preempt_and_abort() will
3290 * release every registration in the list (which have already
3291 * been removed from the primary pr_reg list), except the
3292 * new persistent reservation holder, the calling Initiator Port.
3293 */
3294 if (abort) {
3295 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3296 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3297 pr_reg_n);
3298 }
3299
3300 if (pr_tmpl->pr_aptpl_active) {
Andy Grover5951146d2011-07-19 10:26:37 +00003301 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003302 &pr_reg_n->pr_aptpl_buf[0],
3303 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07003304 if (!ret)
3305 pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003306 "%s\n", (abort) ? "_AND_ABORT" : "");
3307 }
3308
3309 core_scsi3_put_pr_reg(pr_reg_n);
Andy Grover5951146d2011-07-19 10:26:37 +00003310 core_scsi3_pr_generation(cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003311 return 0;
3312}
3313
3314static int core_scsi3_emulate_pro_preempt(
3315 struct se_cmd *cmd,
3316 int type,
3317 int scope,
3318 u64 res_key,
3319 u64 sa_res_key,
3320 int abort)
3321{
3322 int ret = 0;
3323
3324 switch (type) {
3325 case PR_TYPE_WRITE_EXCLUSIVE:
3326 case PR_TYPE_EXCLUSIVE_ACCESS:
3327 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3328 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3329 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3330 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
3331 ret = core_scsi3_pro_preempt(cmd, type, scope,
3332 res_key, sa_res_key, abort);
3333 break;
3334 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003335 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003336 " Type: 0x%02x\n", (abort) ? "_AND_ABORT" : "", type);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003337 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3338 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003339 }
3340
3341 return ret;
3342}
3343
3344
3345static int core_scsi3_emulate_pro_register_and_move(
3346 struct se_cmd *cmd,
3347 u64 res_key,
3348 u64 sa_res_key,
3349 int aptpl,
3350 int unreg)
3351{
Andy Grovere3d6f902011-07-19 08:55:10 +00003352 struct se_session *se_sess = cmd->se_sess;
Andy Grover5951146d2011-07-19 10:26:37 +00003353 struct se_device *dev = cmd->se_dev;
Jörn Engel28168902012-03-15 15:06:58 -04003354 struct se_dev_entry *dest_se_deve = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00003355 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003356 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3357 struct se_port *se_port;
3358 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3359 struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3360 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
Andy Grovere3d6f902011-07-19 08:55:10 +00003361 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003362 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003363 unsigned char *initiator_str;
3364 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
3365 u32 tid_len, tmp_tid_len;
3366 int new_reg = 0, type, scope, ret, matching_iname, prf_isid;
3367 unsigned short rtpi;
3368 unsigned char proto_ident;
3369
Andy Grover6708bb22011-06-08 10:36:43 -07003370 if (!se_sess || !se_lun) {
3371 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003372 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3373 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003374 }
3375 memset(dest_iport, 0, 64);
3376 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3377 se_tpg = se_sess->se_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00003378 tf_ops = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003379 /*
3380 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3381 * Register behaviors for a REGISTER AND MOVE service action
3382 *
3383 * Locate the existing *pr_reg via struct se_node_acl pointers
3384 */
Andy Grover5951146d2011-07-19 10:26:37 +00003385 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003386 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07003387 if (!pr_reg) {
3388 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003389 " *pr_reg for REGISTER_AND_MOVE\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003390 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3391 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003392 }
3393 /*
3394 * The provided reservation key much match the existing reservation key
3395 * provided during this initiator's I_T nexus registration.
3396 */
3397 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07003398 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003399 " res_key: 0x%016Lx does not match existing SA REGISTER"
3400 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
3401 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003402 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3403 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003404 }
3405 /*
3406 * The service active reservation key needs to be non zero
3407 */
Andy Grover6708bb22011-06-08 10:36:43 -07003408 if (!sa_res_key) {
3409 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003410 " sa_res_key\n");
3411 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003412 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3413 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003414 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003415
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003416 /*
3417 * Determine the Relative Target Port Identifier where the reservation
3418 * will be moved to for the TransportID containing SCSI initiator WWN
3419 * information.
3420 */
Andy Grover49493142012-01-16 16:57:08 -08003421 buf = transport_kmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003422 rtpi = (buf[18] & 0xff) << 8;
3423 rtpi |= buf[19] & 0xff;
3424 tid_len = (buf[20] & 0xff) << 24;
3425 tid_len |= (buf[21] & 0xff) << 16;
3426 tid_len |= (buf[22] & 0xff) << 8;
3427 tid_len |= buf[23] & 0xff;
Andy Grover49493142012-01-16 16:57:08 -08003428 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003429 buf = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003430
3431 if ((tid_len + 24) != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07003432 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003433 " does not equal CDB data_length: %u\n", tid_len,
3434 cmd->data_length);
3435 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003436 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3437 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003438 }
3439
3440 spin_lock(&dev->se_port_lock);
3441 list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) {
3442 if (se_port->sep_rtpi != rtpi)
3443 continue;
3444 dest_se_tpg = se_port->sep_tpg;
Andy Grover6708bb22011-06-08 10:36:43 -07003445 if (!dest_se_tpg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003446 continue;
Andy Grovere3d6f902011-07-19 08:55:10 +00003447 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
Andy Grover6708bb22011-06-08 10:36:43 -07003448 if (!dest_tf_ops)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003449 continue;
3450
3451 atomic_inc(&dest_se_tpg->tpg_pr_ref_count);
3452 smp_mb__after_atomic_inc();
3453 spin_unlock(&dev->se_port_lock);
3454
3455 ret = core_scsi3_tpg_depend_item(dest_se_tpg);
3456 if (ret != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003457 pr_err("core_scsi3_tpg_depend_item() failed"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003458 " for dest_se_tpg\n");
3459 atomic_dec(&dest_se_tpg->tpg_pr_ref_count);
3460 smp_mb__after_atomic_dec();
3461 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003462 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3463 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003464 }
3465
3466 spin_lock(&dev->se_port_lock);
3467 break;
3468 }
3469 spin_unlock(&dev->se_port_lock);
3470
Andy Grover6708bb22011-06-08 10:36:43 -07003471 if (!dest_se_tpg || !dest_tf_ops) {
3472 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003473 " fabric ops from Relative Target Port Identifier:"
3474 " %hu\n", rtpi);
3475 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003476 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3477 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003478 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003479
Andy Grover49493142012-01-16 16:57:08 -08003480 buf = transport_kmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003481 proto_ident = (buf[24] & 0x0f);
Andy Grover8b1e1242012-04-03 15:51:12 -07003482
Andy Grover6708bb22011-06-08 10:36:43 -07003483 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003484 " 0x%02x\n", proto_ident);
Andy Grover8b1e1242012-04-03 15:51:12 -07003485
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003486 if (proto_ident != dest_tf_ops->get_fabric_proto_ident(dest_se_tpg)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003487 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003488 " proto_ident: 0x%02x does not match ident: 0x%02x"
3489 " from fabric: %s\n", proto_ident,
3490 dest_tf_ops->get_fabric_proto_ident(dest_se_tpg),
3491 dest_tf_ops->get_fabric_name());
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003492 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3493 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003494 goto out;
3495 }
3496 if (dest_tf_ops->tpg_parse_pr_out_transport_id == NULL) {
Andy Grover6708bb22011-06-08 10:36:43 -07003497 pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003498 " containg a valid tpg_parse_pr_out_transport_id"
3499 " function pointer\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003500 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3501 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003502 goto out;
3503 }
3504 initiator_str = dest_tf_ops->tpg_parse_pr_out_transport_id(dest_se_tpg,
3505 (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07003506 if (!initiator_str) {
3507 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003508 " initiator_str from Transport ID\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003509 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3510 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003511 goto out;
3512 }
3513
Andy Grover49493142012-01-16 16:57:08 -08003514 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003515 buf = NULL;
3516
Andy Grover6708bb22011-06-08 10:36:43 -07003517 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003518 " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
3519 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3520 iport_ptr : "");
3521 /*
3522 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3523 * action specifies a TransportID that is the same as the initiator port
3524 * of the I_T nexus for the command received, then the command shall
3525 * be terminated with CHECK CONDITION status, with the sense key set to
3526 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3527 * IN PARAMETER LIST.
3528 */
3529 pr_reg_nacl = pr_reg->pr_reg_nacl;
3530 matching_iname = (!strcmp(initiator_str,
3531 pr_reg_nacl->initiatorname)) ? 1 : 0;
Andy Grover6708bb22011-06-08 10:36:43 -07003532 if (!matching_iname)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003533 goto after_iport_check;
3534
Andy Grover6708bb22011-06-08 10:36:43 -07003535 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3536 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003537 " matches: %s on received I_T Nexus\n", initiator_str,
3538 pr_reg_nacl->initiatorname);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003539 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3540 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003541 goto out;
3542 }
Andy Grover6708bb22011-06-08 10:36:43 -07003543 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3544 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003545 " matches: %s %s on received I_T Nexus\n",
3546 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3547 pr_reg->pr_reg_isid);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003548 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3549 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003550 goto out;
3551 }
3552after_iport_check:
3553 /*
3554 * Locate the destination struct se_node_acl from the received Transport ID
3555 */
Roland Dreier286388872011-08-16 09:40:01 -07003556 spin_lock_irq(&dest_se_tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003557 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3558 initiator_str);
3559 if (dest_node_acl) {
3560 atomic_inc(&dest_node_acl->acl_pr_ref_count);
3561 smp_mb__after_atomic_inc();
3562 }
Roland Dreier286388872011-08-16 09:40:01 -07003563 spin_unlock_irq(&dest_se_tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003564
Andy Grover6708bb22011-06-08 10:36:43 -07003565 if (!dest_node_acl) {
3566 pr_err("Unable to locate %s dest_node_acl for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003567 " TransportID%s\n", dest_tf_ops->get_fabric_name(),
3568 initiator_str);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003569 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3570 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003571 goto out;
3572 }
3573 ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
3574 if (ret != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003575 pr_err("core_scsi3_nodeacl_depend_item() for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003576 " dest_node_acl\n");
3577 atomic_dec(&dest_node_acl->acl_pr_ref_count);
3578 smp_mb__after_atomic_dec();
3579 dest_node_acl = NULL;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003580 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3581 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003582 goto out;
3583 }
Andy Grover8b1e1242012-04-03 15:51:12 -07003584
Andy Grover6708bb22011-06-08 10:36:43 -07003585 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003586 " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
3587 dest_node_acl->initiatorname);
Andy Grover8b1e1242012-04-03 15:51:12 -07003588
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003589 /*
3590 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3591 * PORT IDENTIFIER.
3592 */
3593 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
Andy Grover6708bb22011-06-08 10:36:43 -07003594 if (!dest_se_deve) {
3595 pr_err("Unable to locate %s dest_se_deve from RTPI:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003596 " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003597 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3598 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003599 goto out;
3600 }
3601
3602 ret = core_scsi3_lunacl_depend_item(dest_se_deve);
3603 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003604 pr_err("core_scsi3_lunacl_depend_item() failed\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003605 atomic_dec(&dest_se_deve->pr_ref_count);
3606 smp_mb__after_atomic_dec();
3607 dest_se_deve = NULL;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003608 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3609 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003610 goto out;
3611 }
Andy Grover8b1e1242012-04-03 15:51:12 -07003612
Andy Grover6708bb22011-06-08 10:36:43 -07003613 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003614 " ACL for dest_se_deve->mapped_lun: %u\n",
3615 dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
3616 dest_se_deve->mapped_lun);
Andy Grover8b1e1242012-04-03 15:51:12 -07003617
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003618 /*
3619 * A persistent reservation needs to already existing in order to
3620 * successfully complete the REGISTER_AND_MOVE service action..
3621 */
3622 spin_lock(&dev->dev_reservation_lock);
3623 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07003624 if (!pr_res_holder) {
3625 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003626 " currently held\n");
3627 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003628 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3629 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003630 goto out;
3631 }
3632 /*
3633 * The received on I_T Nexus must be the reservation holder.
3634 *
3635 * From spc4r17 section 5.7.8 Table 50 --
3636 * Register behaviors for a REGISTER AND MOVE service action
3637 */
3638 if (pr_res_holder != pr_reg) {
Andy Grover6708bb22011-06-08 10:36:43 -07003639 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003640 " Nexus is not reservation holder\n");
3641 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003642 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3643 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003644 goto out;
3645 }
3646 /*
3647 * From spc4r17 section 5.7.8: registering and moving reservation
3648 *
3649 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3650 * action is received and the established persistent reservation is a
3651 * Write Exclusive - All Registrants type or Exclusive Access -
3652 * All Registrants type reservation, then the command shall be completed
3653 * with RESERVATION CONFLICT status.
3654 */
3655 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3656 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003657 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003658 " reservation for type: %s\n",
3659 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3660 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003661 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3662 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003663 goto out;
3664 }
3665 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3666 /*
3667 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3668 */
3669 type = pr_res_holder->pr_res_type;
3670 scope = pr_res_holder->pr_res_type;
3671 /*
3672 * c) Associate the reservation key specified in the SERVICE ACTION
3673 * RESERVATION KEY field with the I_T nexus specified as the
3674 * destination of the register and move, where:
3675 * A) The I_T nexus is specified by the TransportID and the
3676 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3677 * B) Regardless of the TransportID format used, the association for
3678 * the initiator port is based on either the initiator port name
3679 * (see 3.1.71) on SCSI transport protocols where port names are
3680 * required or the initiator port identifier (see 3.1.70) on SCSI
3681 * transport protocols where port names are not required;
3682 * d) Register the reservation key specified in the SERVICE ACTION
3683 * RESERVATION KEY field;
3684 * e) Retain the reservation key specified in the SERVICE ACTION
3685 * RESERVATION KEY field and associated information;
3686 *
3687 * Also, It is not an error for a REGISTER AND MOVE service action to
3688 * register an I_T nexus that is already registered with the same
3689 * reservation key or a different reservation key.
3690 */
3691 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3692 iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07003693 if (!dest_pr_reg) {
Andy Grover5951146d2011-07-19 10:26:37 +00003694 ret = core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003695 dest_node_acl, dest_se_deve, iport_ptr,
3696 sa_res_key, 0, aptpl, 2, 1);
3697 if (ret != 0) {
3698 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003699 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3700 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003701 goto out;
3702 }
3703 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3704 iport_ptr);
3705 new_reg = 1;
3706 }
3707 /*
3708 * f) Release the persistent reservation for the persistent reservation
3709 * holder (i.e., the I_T nexus on which the
3710 */
3711 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
3712 dev->dev_pr_res_holder, 0);
3713 /*
3714 * g) Move the persistent reservation to the specified I_T nexus using
3715 * the same scope and type as the persistent reservation released in
3716 * item f); and
3717 */
3718 dev->dev_pr_res_holder = dest_pr_reg;
3719 dest_pr_reg->pr_res_holder = 1;
3720 dest_pr_reg->pr_res_type = type;
3721 pr_reg->pr_res_scope = scope;
3722 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
3723 PR_REG_ISID_ID_LEN);
3724 /*
3725 * Increment PRGeneration for existing registrations..
3726 */
Andy Grover6708bb22011-06-08 10:36:43 -07003727 if (!new_reg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003728 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3729 spin_unlock(&dev->dev_reservation_lock);
3730
Andy Grover6708bb22011-06-08 10:36:43 -07003731 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003732 " created new reservation holder TYPE: %s on object RTPI:"
3733 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
3734 core_scsi3_pr_dump_type(type), rtpi,
3735 dest_pr_reg->pr_res_generation);
Andy Grover6708bb22011-06-08 10:36:43 -07003736 pr_debug("SPC-3 PR Successfully moved reservation from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003737 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3738 tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
3739 (prf_isid) ? &i_buf[0] : "", dest_tf_ops->get_fabric_name(),
3740 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3741 iport_ptr : "");
3742 /*
3743 * It is now safe to release configfs group dependencies for destination
3744 * of Transport ID Initiator Device/Port Identifier
3745 */
3746 core_scsi3_lunacl_undepend_item(dest_se_deve);
3747 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3748 core_scsi3_tpg_undepend_item(dest_se_tpg);
3749 /*
3750 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3751 * nexus on which PERSISTENT RESERVE OUT command was received.
3752 */
3753 if (unreg) {
3754 spin_lock(&pr_tmpl->registration_lock);
3755 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3756 spin_unlock(&pr_tmpl->registration_lock);
3757 } else
3758 core_scsi3_put_pr_reg(pr_reg);
3759
3760 /*
3761 * Clear the APTPL metadata if APTPL has been disabled, otherwise
3762 * write out the updated metadata to struct file for this SCSI device.
3763 */
Andy Grover6708bb22011-06-08 10:36:43 -07003764 if (!aptpl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003765 pr_tmpl->pr_aptpl_active = 0;
Andy Grover5951146d2011-07-19 10:26:37 +00003766 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07003767 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003768 " REGISTER_AND_MOVE\n");
3769 } else {
3770 pr_tmpl->pr_aptpl_active = 1;
Andy Grover5951146d2011-07-19 10:26:37 +00003771 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003772 &dest_pr_reg->pr_aptpl_buf[0],
3773 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07003774 if (!ret)
3775 pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003776 " REGISTER_AND_MOVE\n");
3777 }
3778
Andy Grover49493142012-01-16 16:57:08 -08003779 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003780
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003781 core_scsi3_put_pr_reg(dest_pr_reg);
3782 return 0;
3783out:
Andy Grover05d1c7c2011-07-20 19:13:28 +00003784 if (buf)
Andy Grover49493142012-01-16 16:57:08 -08003785 transport_kunmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003786 if (dest_se_deve)
3787 core_scsi3_lunacl_undepend_item(dest_se_deve);
3788 if (dest_node_acl)
3789 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3790 core_scsi3_tpg_undepend_item(dest_se_tpg);
3791 core_scsi3_put_pr_reg(pr_reg);
3792 return ret;
3793}
3794
3795static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
3796{
3797 unsigned int __v1, __v2;
3798
3799 __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
3800 __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
3801
3802 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
3803}
3804
3805/*
3806 * See spc4r17 section 6.14 Table 170
3807 */
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04003808int target_scsi3_emulate_pr_out(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003809{
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003810 unsigned char *cdb = &cmd->t_task_cdb[0];
Andy Grover05d1c7c2011-07-20 19:13:28 +00003811 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003812 u64 res_key, sa_res_key;
3813 int sa, scope, type, aptpl;
3814 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003815 int ret;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003816
3817 /*
3818 * Following spc2r20 5.5.1 Reservations overview:
3819 *
3820 * If a logical unit has been reserved by any RESERVE command and is
3821 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3822 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3823 * initiator or service action and shall terminate with a RESERVATION
3824 * CONFLICT status.
3825 */
3826 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
3827 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3828 " SPC-2 reservation is held, returning"
3829 " RESERVATION_CONFLICT\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003830 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
Roland Dreierd35212f2012-07-16 15:17:10 -07003831 ret = -EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003832 goto out;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003833 }
3834
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003835 /*
3836 * FIXME: A NULL struct se_session pointer means an this is not coming from
3837 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3838 */
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003839 if (!cmd->se_sess) {
3840 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Roland Dreierd35212f2012-07-16 15:17:10 -07003841 ret = -EINVAL;
3842 goto out;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003843 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003844
3845 if (cmd->data_length < 24) {
Andy Grover6708bb22011-06-08 10:36:43 -07003846 pr_warn("SPC-PR: Received PR OUT parameter list"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003847 " length too small: %u\n", cmd->data_length);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003848 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3849 ret = -EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003850 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003851 }
3852 /*
3853 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3854 */
3855 sa = (cdb[1] & 0x1f);
3856 scope = (cdb[2] & 0xf0);
3857 type = (cdb[2] & 0x0f);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003858
Andy Grover49493142012-01-16 16:57:08 -08003859 buf = transport_kmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003860 /*
3861 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3862 */
3863 res_key = core_scsi3_extract_reservation_key(&buf[0]);
3864 sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
3865 /*
3866 * REGISTER_AND_MOVE uses a different SA parameter list containing
3867 * SCSI TransportIDs.
3868 */
3869 if (sa != PRO_REGISTER_AND_MOVE) {
3870 spec_i_pt = (buf[20] & 0x08);
3871 all_tg_pt = (buf[20] & 0x04);
3872 aptpl = (buf[20] & 0x01);
3873 } else {
3874 aptpl = (buf[17] & 0x01);
3875 unreg = (buf[17] & 0x02);
3876 }
Andy Grover49493142012-01-16 16:57:08 -08003877 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003878 buf = NULL;
3879
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003880 /*
3881 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3882 */
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003883 if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER)) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003884 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3885 ret = -EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003886 goto out;
3887 }
3888
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003889 /*
3890 * From spc4r17 section 6.14:
3891 *
3892 * If the SPEC_I_PT bit is set to zero, the service action is not
3893 * REGISTER AND MOVE, and the parameter list length is not 24, then
3894 * the command shall be terminated with CHECK CONDITION status, with
3895 * the sense key set to ILLEGAL REQUEST, and the additional sense
3896 * code set to PARAMETER LIST LENGTH ERROR.
3897 */
Andy Grover6708bb22011-06-08 10:36:43 -07003898 if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003899 (cmd->data_length != 24)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003900 pr_warn("SPC-PR: Received PR OUT illegal parameter"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003901 " list length: %u\n", cmd->data_length);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003902 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3903 ret = -EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003904 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003905 }
3906 /*
3907 * (core_scsi3_emulate_pro_* function parameters
3908 * are defined by spc4r17 Table 174:
3909 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3910 */
3911 switch (sa) {
3912 case PRO_REGISTER:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003913 ret = core_scsi3_emulate_pro_register(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003914 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 0);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003915 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003916 case PRO_RESERVE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003917 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3918 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003919 case PRO_RELEASE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003920 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3921 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003922 case PRO_CLEAR:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003923 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3924 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003925 case PRO_PREEMPT:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003926 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003927 res_key, sa_res_key, 0);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003928 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003929 case PRO_PREEMPT_AND_ABORT:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003930 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003931 res_key, sa_res_key, 1);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003932 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003933 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003934 ret = core_scsi3_emulate_pro_register(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003935 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 1);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003936 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003937 case PRO_REGISTER_AND_MOVE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003938 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003939 sa_res_key, aptpl, unreg);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003940 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003941 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003942 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003943 " action: 0x%02x\n", cdb[1] & 0x1f);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003944 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3945 ret = -EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003946 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003947 }
3948
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003949out:
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04003950 if (!ret)
3951 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003952 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003953}
3954
3955/*
3956 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3957 *
3958 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3959 */
3960static int core_scsi3_pri_read_keys(struct se_cmd *cmd)
3961{
Andy Grover5951146d2011-07-19 10:26:37 +00003962 struct se_device *se_dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00003963 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003964 struct t10_pr_registration *pr_reg;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003965 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003966 u32 add_len = 0, off = 8;
3967
3968 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07003969 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003970 " too small\n", cmd->data_length);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003971 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3972 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003973 }
3974
Andy Grover49493142012-01-16 16:57:08 -08003975 buf = transport_kmap_data_sg(cmd);
Andy Grovere3d6f902011-07-19 08:55:10 +00003976 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
3977 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
3978 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
3979 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003980
Andy Grovere3d6f902011-07-19 08:55:10 +00003981 spin_lock(&su_dev->t10_pr.registration_lock);
3982 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003983 pr_reg_list) {
3984 /*
3985 * Check for overflow of 8byte PRI READ_KEYS payload and
3986 * next reservation key list descriptor.
3987 */
3988 if ((add_len + 8) > (cmd->data_length - 8))
3989 break;
3990
3991 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3992 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3993 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3994 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
3995 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
3996 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
3997 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
3998 buf[off++] = (pr_reg->pr_res_key & 0xff);
3999
4000 add_len += 8;
4001 }
Andy Grovere3d6f902011-07-19 08:55:10 +00004002 spin_unlock(&su_dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004003
4004 buf[4] = ((add_len >> 24) & 0xff);
4005 buf[5] = ((add_len >> 16) & 0xff);
4006 buf[6] = ((add_len >> 8) & 0xff);
4007 buf[7] = (add_len & 0xff);
4008
Andy Grover49493142012-01-16 16:57:08 -08004009 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00004010
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004011 return 0;
4012}
4013
4014/*
4015 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
4016 *
4017 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
4018 */
4019static int core_scsi3_pri_read_reservation(struct se_cmd *cmd)
4020{
Andy Grover5951146d2011-07-19 10:26:37 +00004021 struct se_device *se_dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00004022 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004023 struct t10_pr_registration *pr_reg;
Andy Grover05d1c7c2011-07-20 19:13:28 +00004024 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004025 u64 pr_res_key;
4026 u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
4027
4028 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07004029 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004030 " too small\n", cmd->data_length);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004031 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4032 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004033 }
4034
Andy Grover49493142012-01-16 16:57:08 -08004035 buf = transport_kmap_data_sg(cmd);
Andy Grovere3d6f902011-07-19 08:55:10 +00004036 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
4037 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
4038 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
4039 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004040
4041 spin_lock(&se_dev->dev_reservation_lock);
4042 pr_reg = se_dev->dev_pr_res_holder;
Andy Groveree1b1b92012-07-12 17:34:54 -07004043 if (pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004044 /*
4045 * Set the hardcoded Additional Length
4046 */
4047 buf[4] = ((add_len >> 24) & 0xff);
4048 buf[5] = ((add_len >> 16) & 0xff);
4049 buf[6] = ((add_len >> 8) & 0xff);
4050 buf[7] = (add_len & 0xff);
4051
Andy Grover05d1c7c2011-07-20 19:13:28 +00004052 if (cmd->data_length < 22)
4053 goto err;
4054
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004055 /*
4056 * Set the Reservation key.
4057 *
4058 * From spc4r17, section 5.7.10:
4059 * A persistent reservation holder has its reservation key
4060 * returned in the parameter data from a PERSISTENT
4061 * RESERVE IN command with READ RESERVATION service action as
4062 * follows:
4063 * a) For a persistent reservation of the type Write Exclusive
4064 * - All Registrants or Exclusive Access ­ All Regitrants,
4065 * the reservation key shall be set to zero; or
4066 * b) For all other persistent reservation types, the
4067 * reservation key shall be set to the registered
4068 * reservation key for the I_T nexus that holds the
4069 * persistent reservation.
4070 */
4071 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
4072 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
4073 pr_res_key = 0;
4074 else
4075 pr_res_key = pr_reg->pr_res_key;
4076
4077 buf[8] = ((pr_res_key >> 56) & 0xff);
4078 buf[9] = ((pr_res_key >> 48) & 0xff);
4079 buf[10] = ((pr_res_key >> 40) & 0xff);
4080 buf[11] = ((pr_res_key >> 32) & 0xff);
4081 buf[12] = ((pr_res_key >> 24) & 0xff);
4082 buf[13] = ((pr_res_key >> 16) & 0xff);
4083 buf[14] = ((pr_res_key >> 8) & 0xff);
4084 buf[15] = (pr_res_key & 0xff);
4085 /*
4086 * Set the SCOPE and TYPE
4087 */
4088 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
4089 (pr_reg->pr_res_type & 0x0f);
4090 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00004091
4092err:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004093 spin_unlock(&se_dev->dev_reservation_lock);
Andy Grover49493142012-01-16 16:57:08 -08004094 transport_kunmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004095
4096 return 0;
4097}
4098
4099/*
4100 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
4101 *
4102 * See spc4r17 section 6.13.4 Table 165
4103 */
4104static int core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
4105{
Andy Grover5951146d2011-07-19 10:26:37 +00004106 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00004107 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00004108 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004109 u16 add_len = 8; /* Hardcoded to 8. */
4110
4111 if (cmd->data_length < 6) {
Andy Grover6708bb22011-06-08 10:36:43 -07004112 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004113 " %u too small\n", cmd->data_length);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004114 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4115 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004116 }
4117
Andy Grover49493142012-01-16 16:57:08 -08004118 buf = transport_kmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00004119
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004120 buf[0] = ((add_len << 8) & 0xff);
4121 buf[1] = (add_len & 0xff);
4122 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
4123 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
4124 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
4125 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
4126 /*
4127 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
4128 * set the TMV: Task Mask Valid bit.
4129 */
4130 buf[3] |= 0x80;
4131 /*
4132 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
4133 */
4134 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
4135 /*
4136 * PTPL_A: Persistence across Target Power Loss Active bit
4137 */
4138 if (pr_tmpl->pr_aptpl_active)
4139 buf[3] |= 0x01;
4140 /*
4141 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
4142 */
4143 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4144 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
4145 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
4146 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
4147 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
4148 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4149
Andy Grover49493142012-01-16 16:57:08 -08004150 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00004151
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004152 return 0;
4153}
4154
4155/*
4156 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
4157 *
4158 * See spc4r17 section 6.13.5 Table 168 and 169
4159 */
4160static int core_scsi3_pri_read_full_status(struct se_cmd *cmd)
4161{
Andy Grover5951146d2011-07-19 10:26:37 +00004162 struct se_device *se_dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004163 struct se_node_acl *se_nacl;
Andy Grovere3d6f902011-07-19 08:55:10 +00004164 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004165 struct se_portal_group *se_tpg;
4166 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
Andy Grovere3d6f902011-07-19 08:55:10 +00004167 struct t10_reservation *pr_tmpl = &se_dev->se_sub_dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00004168 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004169 u32 add_desc_len = 0, add_len = 0, desc_len, exp_desc_len;
4170 u32 off = 8; /* off into first Full Status descriptor */
4171 int format_code = 0;
4172
4173 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07004174 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004175 " too small\n", cmd->data_length);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004176 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4177 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004178 }
4179
Andy Grover49493142012-01-16 16:57:08 -08004180 buf = transport_kmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00004181
Andy Grovere3d6f902011-07-19 08:55:10 +00004182 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
4183 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
4184 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
4185 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004186
4187 spin_lock(&pr_tmpl->registration_lock);
4188 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
4189 &pr_tmpl->registration_list, pr_reg_list) {
4190
4191 se_nacl = pr_reg->pr_reg_nacl;
4192 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
4193 add_desc_len = 0;
4194
4195 atomic_inc(&pr_reg->pr_res_holders);
4196 smp_mb__after_atomic_inc();
4197 spin_unlock(&pr_tmpl->registration_lock);
4198 /*
4199 * Determine expected length of $FABRIC_MOD specific
4200 * TransportID full status descriptor..
4201 */
Andy Grovere3d6f902011-07-19 08:55:10 +00004202 exp_desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id_len(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004203 se_tpg, se_nacl, pr_reg, &format_code);
4204
4205 if ((exp_desc_len + add_len) > cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07004206 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004207 " out of buffer: %d\n", cmd->data_length);
4208 spin_lock(&pr_tmpl->registration_lock);
4209 atomic_dec(&pr_reg->pr_res_holders);
4210 smp_mb__after_atomic_dec();
4211 break;
4212 }
4213 /*
4214 * Set RESERVATION KEY
4215 */
4216 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
4217 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
4218 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
4219 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
4220 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
4221 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
4222 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
4223 buf[off++] = (pr_reg->pr_res_key & 0xff);
4224 off += 4; /* Skip Over Reserved area */
4225
4226 /*
4227 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4228 */
4229 if (pr_reg->pr_reg_all_tg_pt)
4230 buf[off] = 0x02;
4231 /*
4232 * The struct se_lun pointer will be present for the
4233 * reservation holder for PR_HOLDER bit.
4234 *
4235 * Also, if this registration is the reservation
4236 * holder, fill in SCOPE and TYPE in the next byte.
4237 */
4238 if (pr_reg->pr_res_holder) {
4239 buf[off++] |= 0x01;
4240 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
4241 (pr_reg->pr_res_type & 0x0f);
4242 } else
4243 off += 2;
4244
4245 off += 4; /* Skip over reserved area */
4246 /*
4247 * From spc4r17 6.3.15:
4248 *
4249 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4250 * IDENTIFIER field contains the relative port identifier (see
4251 * 3.1.120) of the target port that is part of the I_T nexus
4252 * described by this full status descriptor. If the ALL_TG_PT
4253 * bit is set to one, the contents of the RELATIVE TARGET PORT
4254 * IDENTIFIER field are not defined by this standard.
4255 */
Andy Grover6708bb22011-06-08 10:36:43 -07004256 if (!pr_reg->pr_reg_all_tg_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004257 struct se_port *port = pr_reg->pr_reg_tg_pt_lun->lun_sep;
4258
4259 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
4260 buf[off++] = (port->sep_rtpi & 0xff);
4261 } else
Masanari Iida35d1efe2012-08-16 22:43:13 +09004262 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004263
4264 /*
4265 * Now, have the $FABRIC_MOD fill in the protocol identifier
4266 */
Andy Grovere3d6f902011-07-19 08:55:10 +00004267 desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id(se_tpg,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004268 se_nacl, pr_reg, &format_code, &buf[off+4]);
4269
4270 spin_lock(&pr_tmpl->registration_lock);
4271 atomic_dec(&pr_reg->pr_res_holders);
4272 smp_mb__after_atomic_dec();
4273 /*
4274 * Set the ADDITIONAL DESCRIPTOR LENGTH
4275 */
4276 buf[off++] = ((desc_len >> 24) & 0xff);
4277 buf[off++] = ((desc_len >> 16) & 0xff);
4278 buf[off++] = ((desc_len >> 8) & 0xff);
4279 buf[off++] = (desc_len & 0xff);
4280 /*
4281 * Size of full desctipor header minus TransportID
4282 * containing $FABRIC_MOD specific) initiator device/port
4283 * WWN information.
4284 *
4285 * See spc4r17 Section 6.13.5 Table 169
4286 */
4287 add_desc_len = (24 + desc_len);
4288
4289 off += desc_len;
4290 add_len += add_desc_len;
4291 }
4292 spin_unlock(&pr_tmpl->registration_lock);
4293 /*
4294 * Set ADDITIONAL_LENGTH
4295 */
4296 buf[4] = ((add_len >> 24) & 0xff);
4297 buf[5] = ((add_len >> 16) & 0xff);
4298 buf[6] = ((add_len >> 8) & 0xff);
4299 buf[7] = (add_len & 0xff);
4300
Andy Grover49493142012-01-16 16:57:08 -08004301 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00004302
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004303 return 0;
4304}
4305
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04004306int target_scsi3_emulate_pr_in(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004307{
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004308 int ret;
Christoph Hellwige76a35d2011-11-03 17:50:42 -04004309
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004310 /*
4311 * Following spc2r20 5.5.1 Reservations overview:
4312 *
4313 * If a logical unit has been reserved by any RESERVE command and is
4314 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4315 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4316 * initiator or service action and shall terminate with a RESERVATION
4317 * CONFLICT status.
4318 */
4319 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
4320 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4321 " SPC-2 reservation is held, returning"
4322 " RESERVATION_CONFLICT\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004323 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
4324 return -EINVAL;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004325 }
4326
4327 switch (cmd->t_task_cdb[1] & 0x1f) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004328 case PRI_READ_KEYS:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004329 ret = core_scsi3_pri_read_keys(cmd);
4330 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004331 case PRI_READ_RESERVATION:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004332 ret = core_scsi3_pri_read_reservation(cmd);
4333 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004334 case PRI_REPORT_CAPABILITIES:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004335 ret = core_scsi3_pri_report_capabilities(cmd);
4336 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004337 case PRI_READ_FULL_STATUS:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004338 ret = core_scsi3_pri_read_full_status(cmd);
4339 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004340 default:
Andy Grover6708bb22011-06-08 10:36:43 -07004341 pr_err("Unknown PERSISTENT_RESERVE_IN service"
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004342 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004343 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4344 ret = -EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004345 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004346 }
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004347
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04004348 if (!ret)
4349 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004350 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004351}
4352
4353static int core_pt_reservation_check(struct se_cmd *cmd, u32 *pr_res_type)
4354{
4355 return 0;
4356}
4357
4358static int core_pt_seq_non_holder(
4359 struct se_cmd *cmd,
4360 unsigned char *cdb,
4361 u32 pr_reg_type)
4362{
4363 return 0;
4364}
4365
4366int core_setup_reservations(struct se_device *dev, int force_pt)
4367{
4368 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00004369 struct t10_reservation *rest = &su_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004370 /*
4371 * If this device is from Target_Core_Mod/pSCSI, use the reservations
4372 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
4373 * cause a problem because libata and some SATA RAID HBAs appear
4374 * under Linux/SCSI, but to emulate reservations themselves.
4375 */
Andy Grovere3d6f902011-07-19 08:55:10 +00004376 if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
4377 !(dev->se_sub_dev->se_dev_attrib.emulate_reservations)) || force_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004378 rest->res_type = SPC_PASSTHROUGH;
4379 rest->pr_ops.t10_reservation_check = &core_pt_reservation_check;
4380 rest->pr_ops.t10_seq_non_holder = &core_pt_seq_non_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07004381 pr_debug("%s: Using SPC_PASSTHROUGH, no reservation"
Andy Grovere3d6f902011-07-19 08:55:10 +00004382 " emulation\n", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004383 return 0;
4384 }
4385 /*
4386 * If SPC-3 or above is reported by real or emulated struct se_device,
4387 * use emulated Persistent Reservations.
4388 */
Andy Grovere3d6f902011-07-19 08:55:10 +00004389 if (dev->transport->get_device_rev(dev) >= SCSI_3) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004390 rest->res_type = SPC3_PERSISTENT_RESERVATIONS;
4391 rest->pr_ops.t10_reservation_check = &core_scsi3_pr_reservation_check;
4392 rest->pr_ops.t10_seq_non_holder = &core_scsi3_pr_seq_non_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07004393 pr_debug("%s: Using SPC3_PERSISTENT_RESERVATIONS"
Andy Grovere3d6f902011-07-19 08:55:10 +00004394 " emulation\n", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004395 } else {
4396 rest->res_type = SPC2_RESERVATIONS;
4397 rest->pr_ops.t10_reservation_check = &core_scsi2_reservation_check;
4398 rest->pr_ops.t10_seq_non_holder =
4399 &core_scsi2_reservation_seq_non_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07004400 pr_debug("%s: Using SPC2_RESERVATIONS emulation\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004401 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004402 }
4403
4404 return 0;
4405}