blob: bd78faf67c6b366657d572b8e8d1461af77abfec [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 *
Nicholas Bellingerfd9a11d2012-11-09 14:51:48 -08007 * (c) Copyright 2009-2012 RisingTide Systems LLC.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08008 *
9 * Nicholas A. Bellinger <nab@kernel.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 *
25 ******************************************************************************/
26
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080027#include <linux/slab.h>
28#include <linux/spinlock.h>
29#include <linux/list.h>
Al Viro0e9b10a2013-02-23 15:22:43 -050030#include <linux/file.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080031#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
Andy Groverd2843c12013-05-16 10:40:55 -070056void core_pr_dump_initiator_port(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080057 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)
Andy Groverd2843c12013-05-16 10:40:55 -070062 buf[0] = '\0';
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080063
Andy Groverd2843c12013-05-16 10:40:55 -070064 snprintf(buf, size, ",i,0x%s", pr_reg->pr_reg_isid);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080065}
66
Andy Grover33ce6a82013-05-16 10:40:54 -070067enum register_type {
68 REGISTER,
69 REGISTER_AND_IGNORE_EXISTING_KEY,
70 REGISTER_AND_MOVE,
71};
72
73enum preempt_type {
74 PREEMPT,
75 PREEMPT_AND_ABORT,
76};
77
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080078static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
79 struct t10_pr_registration *, int);
80
Christoph Hellwigde103c92012-11-06 12:24:09 -080081static sense_reason_t
82target_scsi2_reservation_check(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080083{
Christoph Hellwigd977f432012-10-10 17:37:15 -040084 struct se_device *dev = cmd->se_dev;
85 struct se_session *sess = cmd->se_sess;
86
87 switch (cmd->t_task_cdb[0]) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080088 case INQUIRY:
89 case RELEASE:
90 case RELEASE_10:
91 return 0;
92 default:
Christoph Hellwigd977f432012-10-10 17:37:15 -040093 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080094 }
95
Christoph Hellwigd977f432012-10-10 17:37:15 -040096 if (!dev->dev_reserved_node_acl || !sess)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080097 return 0;
98
Christoph Hellwigd977f432012-10-10 17:37:15 -040099 if (dev->dev_reserved_node_acl != sess->se_node_acl)
Christoph Hellwigde103c92012-11-06 12:24:09 -0800100 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800101
Christoph Hellwigd977f432012-10-10 17:37:15 -0400102 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
103 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
Christoph Hellwigde103c92012-11-06 12:24:09 -0800104 return TCM_RESERVATION_CONFLICT;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400105 }
106
107 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800108}
109
Christoph Hellwigeacac002011-11-03 17:50:40 -0400110static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
111 struct se_node_acl *, struct se_session *);
112static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
113
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700114static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
Christoph Hellwigeacac002011-11-03 17:50:40 -0400115{
116 struct se_session *se_sess = cmd->se_sess;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400117 struct se_device *dev = cmd->se_dev;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400118 struct t10_pr_registration *pr_reg;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400119 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400120 int conflict = 0;
121
Christoph Hellwigeacac002011-11-03 17:50:40 -0400122 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
123 se_sess);
124 if (pr_reg) {
125 /*
126 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
127 * behavior
128 *
129 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
130 * status, but no reservation shall be established and the
131 * persistent reservation shall not be changed, if the command
132 * is received from a) and b) below.
133 *
134 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
135 * status, but the persistent reservation shall not be released,
136 * if the command is received from a) and b)
137 *
138 * a) An I_T nexus that is a persistent reservation holder; or
139 * b) An I_T nexus that is registered if a registrants only or
140 * all registrants type persistent reservation is present.
141 *
142 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
143 * RELEASE(6) command, or RELEASE(10) command shall be processed
144 * as defined in SPC-2.
145 */
146 if (pr_reg->pr_res_holder) {
147 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700148 return 1;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400149 }
150 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
151 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
152 (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
153 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
154 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700155 return 1;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400156 }
157 core_scsi3_put_pr_reg(pr_reg);
158 conflict = 1;
159 } else {
160 /*
161 * Following spc2r20 5.5.1 Reservations overview:
162 *
163 * If a logical unit has executed a PERSISTENT RESERVE OUT
164 * command with the REGISTER or the REGISTER AND IGNORE
165 * EXISTING KEY service action and is still registered by any
166 * initiator, all RESERVE commands and all RELEASE commands
167 * regardless of initiator shall conflict and shall terminate
168 * with a RESERVATION CONFLICT status.
169 */
170 spin_lock(&pr_tmpl->registration_lock);
171 conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
172 spin_unlock(&pr_tmpl->registration_lock);
173 }
174
175 if (conflict) {
176 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
177 " while active SPC-3 registrations exist,"
178 " returning RESERVATION_CONFLICT\n");
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700179 return -EBUSY;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400180 }
181
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700182 return 0;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400183}
184
Christoph Hellwigde103c92012-11-06 12:24:09 -0800185sense_reason_t
186target_scsi2_reservation_release(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800187{
188 struct se_device *dev = cmd->se_dev;
189 struct se_session *sess = cmd->se_sess;
Wei Yongjun609234e2012-09-07 14:56:28 +0800190 struct se_portal_group *tpg;
Christoph Hellwigde103c92012-11-06 12:24:09 -0800191 int rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800192
Wei Yongjun609234e2012-09-07 14:56:28 +0800193 if (!sess || !sess->se_tpg)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400194 goto out;
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700195 rc = target_check_scsi2_reservation_conflict(cmd);
196 if (rc == 1)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400197 goto out;
Christoph Hellwigde103c92012-11-06 12:24:09 -0800198 if (rc < 0)
199 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800200
201 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400202 if (!dev->dev_reserved_node_acl || !sess)
203 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800204
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400205 if (dev->dev_reserved_node_acl != sess->se_node_acl)
206 goto out_unlock;
207
Bernhard Kohledc318d2012-05-13 23:39:37 +0200208 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
209 goto out_unlock;
210
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800211 dev->dev_reserved_node_acl = NULL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400212 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
213 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800214 dev->dev_res_bin_isid = 0;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400215 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS_WITH_ISID;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800216 }
Wei Yongjun609234e2012-09-07 14:56:28 +0800217 tpg = sess->se_tpg;
Andy Grover6708bb22011-06-08 10:36:43 -0700218 pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
Andy Grovere3d6f902011-07-19 08:55:10 +0000219 " MAPPED LUN: %u for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
220 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800221 sess->se_node_acl->initiatorname);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800222
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400223out_unlock:
224 spin_unlock(&dev->dev_reservation_lock);
225out:
Christoph Hellwigde103c92012-11-06 12:24:09 -0800226 target_complete_cmd(cmd, GOOD);
227 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800228}
229
Christoph Hellwigde103c92012-11-06 12:24:09 -0800230sense_reason_t
231target_scsi2_reservation_reserve(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800232{
233 struct se_device *dev = cmd->se_dev;
234 struct se_session *sess = cmd->se_sess;
Wei Yongjun609234e2012-09-07 14:56:28 +0800235 struct se_portal_group *tpg;
Christoph Hellwigde103c92012-11-06 12:24:09 -0800236 sense_reason_t ret = 0;
237 int rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800238
Andy Grovera1d8b492011-05-02 17:12:10 -0700239 if ((cmd->t_task_cdb[1] & 0x01) &&
240 (cmd->t_task_cdb[1] & 0x02)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700241 pr_err("LongIO and Obselete Bits set, returning"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800242 " ILLEGAL_REQUEST\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -0800243 return TCM_UNSUPPORTED_SCSI_OPCODE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800244 }
245 /*
246 * This is currently the case for target_core_mod passthrough struct se_cmd
247 * ops
248 */
Wei Yongjun609234e2012-09-07 14:56:28 +0800249 if (!sess || !sess->se_tpg)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400250 goto out;
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700251 rc = target_check_scsi2_reservation_conflict(cmd);
252 if (rc == 1)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400253 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800254
Christoph Hellwigde103c92012-11-06 12:24:09 -0800255 if (rc < 0)
256 return TCM_RESERVATION_CONFLICT;
257
Wei Yongjun609234e2012-09-07 14:56:28 +0800258 tpg = sess->se_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800259 spin_lock(&dev->dev_reservation_lock);
260 if (dev->dev_reserved_node_acl &&
261 (dev->dev_reserved_node_acl != sess->se_node_acl)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700262 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000263 tpg->se_tpg_tfo->get_fabric_name());
Andy Grover6708bb22011-06-08 10:36:43 -0700264 pr_err("Original reserver LUN: %u %s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000265 cmd->se_lun->unpacked_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800266 dev->dev_reserved_node_acl->initiatorname);
Andy Grover6708bb22011-06-08 10:36:43 -0700267 pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
Andy Grovere3d6f902011-07-19 08:55:10 +0000268 " from %s \n", cmd->se_lun->unpacked_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800269 cmd->se_deve->mapped_lun,
270 sess->se_node_acl->initiatorname);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800271 ret = TCM_RESERVATION_CONFLICT;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400272 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800273 }
274
275 dev->dev_reserved_node_acl = sess->se_node_acl;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400276 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800277 if (sess->sess_bin_isid != 0) {
278 dev->dev_res_bin_isid = sess->sess_bin_isid;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400279 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS_WITH_ISID;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800280 }
Andy Grover6708bb22011-06-08 10:36:43 -0700281 pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
Andy Grovere3d6f902011-07-19 08:55:10 +0000282 " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
283 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800284 sess->se_node_acl->initiatorname);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800285
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400286out_unlock:
287 spin_unlock(&dev->dev_reservation_lock);
288out:
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400289 if (!ret)
290 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400291 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800292}
293
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800294
295/*
296 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
297 *
298 * This function is called by those initiator ports who are *NOT*
299 * the active PR reservation holder when a reservation is present.
300 */
301static int core_scsi3_pr_seq_non_holder(
302 struct se_cmd *cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800303 u32 pr_reg_type)
304{
Christoph Hellwigd977f432012-10-10 17:37:15 -0400305 unsigned char *cdb = cmd->t_task_cdb;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800306 struct se_dev_entry *se_deve;
Andy Grovere3d6f902011-07-19 08:55:10 +0000307 struct se_session *se_sess = cmd->se_sess;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800308 int other_cdb = 0, ignore_reg;
309 int registered_nexus = 0, ret = 1; /* Conflict by default */
310 int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
311 int we = 0; /* Write Exclusive */
312 int legacy = 0; /* Act like a legacy device and return
313 * RESERVATION CONFLICT on some CDBs */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800314
Jörn Engelf2083242012-03-15 15:05:40 -0400315 se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800316 /*
317 * Determine if the registration should be ignored due to
Christoph Hellwigd977f432012-10-10 17:37:15 -0400318 * non-matching ISIDs in target_scsi3_pr_reservation_check().
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800319 */
320 ignore_reg = (pr_reg_type & 0x80000000);
321 if (ignore_reg)
322 pr_reg_type &= ~0x80000000;
323
324 switch (pr_reg_type) {
325 case PR_TYPE_WRITE_EXCLUSIVE:
326 we = 1;
327 case PR_TYPE_EXCLUSIVE_ACCESS:
328 /*
329 * Some commands are only allowed for the persistent reservation
330 * holder.
331 */
332 if ((se_deve->def_pr_registered) && !(ignore_reg))
333 registered_nexus = 1;
334 break;
335 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
336 we = 1;
337 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
338 /*
339 * Some commands are only allowed for registered I_T Nexuses.
340 */
341 reg_only = 1;
342 if ((se_deve->def_pr_registered) && !(ignore_reg))
343 registered_nexus = 1;
344 break;
345 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
346 we = 1;
347 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
348 /*
349 * Each registered I_T Nexus is a reservation holder.
350 */
351 all_reg = 1;
352 if ((se_deve->def_pr_registered) && !(ignore_reg))
353 registered_nexus = 1;
354 break;
355 default:
Andy Grovere3d6f902011-07-19 08:55:10 +0000356 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800357 }
358 /*
359 * Referenced from spc4r17 table 45 for *NON* PR holder access
360 */
361 switch (cdb[0]) {
362 case SECURITY_PROTOCOL_IN:
363 if (registered_nexus)
364 return 0;
365 ret = (we) ? 0 : 1;
366 break;
367 case MODE_SENSE:
368 case MODE_SENSE_10:
369 case READ_ATTRIBUTE:
370 case READ_BUFFER:
371 case RECEIVE_DIAGNOSTIC:
372 if (legacy) {
373 ret = 1;
374 break;
375 }
376 if (registered_nexus) {
377 ret = 0;
378 break;
379 }
380 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
381 break;
382 case PERSISTENT_RESERVE_OUT:
383 /*
384 * This follows PERSISTENT_RESERVE_OUT service actions that
385 * are allowed in the presence of various reservations.
386 * See spc4r17, table 46
387 */
388 switch (cdb[1] & 0x1f) {
389 case PRO_CLEAR:
390 case PRO_PREEMPT:
391 case PRO_PREEMPT_AND_ABORT:
392 ret = (registered_nexus) ? 0 : 1;
393 break;
394 case PRO_REGISTER:
395 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
396 ret = 0;
397 break;
398 case PRO_REGISTER_AND_MOVE:
399 case PRO_RESERVE:
400 ret = 1;
401 break;
402 case PRO_RELEASE:
403 ret = (registered_nexus) ? 0 : 1;
404 break;
405 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700406 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800407 " action: 0x%02x\n", cdb[1] & 0x1f);
Andy Grovere3d6f902011-07-19 08:55:10 +0000408 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800409 }
410 break;
411 case RELEASE:
412 case RELEASE_10:
Christoph Hellwigeacac002011-11-03 17:50:40 -0400413 /* Handled by CRH=1 in target_scsi2_reservation_release() */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800414 ret = 0;
415 break;
416 case RESERVE:
417 case RESERVE_10:
Christoph Hellwigeacac002011-11-03 17:50:40 -0400418 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800419 ret = 0;
420 break;
421 case TEST_UNIT_READY:
422 ret = (legacy) ? 1 : 0; /* Conflict for legacy */
423 break;
424 case MAINTENANCE_IN:
425 switch (cdb[1] & 0x1f) {
426 case MI_MANAGEMENT_PROTOCOL_IN:
427 if (registered_nexus) {
428 ret = 0;
429 break;
430 }
431 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
432 break;
433 case MI_REPORT_SUPPORTED_OPERATION_CODES:
434 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
435 if (legacy) {
436 ret = 1;
437 break;
438 }
439 if (registered_nexus) {
440 ret = 0;
441 break;
442 }
443 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
444 break;
445 case MI_REPORT_ALIASES:
446 case MI_REPORT_IDENTIFYING_INFORMATION:
447 case MI_REPORT_PRIORITY:
448 case MI_REPORT_TARGET_PGS:
449 case MI_REPORT_TIMESTAMP:
450 ret = 0; /* Allowed */
451 break;
452 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700453 pr_err("Unknown MI Service Action: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800454 (cdb[1] & 0x1f));
Andy Grovere3d6f902011-07-19 08:55:10 +0000455 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800456 }
457 break;
458 case ACCESS_CONTROL_IN:
459 case ACCESS_CONTROL_OUT:
460 case INQUIRY:
461 case LOG_SENSE:
462 case READ_MEDIA_SERIAL_NUMBER:
463 case REPORT_LUNS:
464 case REQUEST_SENSE:
Marco Sanvido68169662012-01-03 17:12:58 -0800465 case PERSISTENT_RESERVE_IN:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800466 ret = 0; /*/ Allowed CDBs */
467 break;
468 default:
469 other_cdb = 1;
470 break;
471 }
472 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300473 * Case where the CDB is explicitly allowed in the above switch
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800474 * statement.
475 */
Andy Grover6708bb22011-06-08 10:36:43 -0700476 if (!ret && !other_cdb) {
Andy Grover6708bb22011-06-08 10:36:43 -0700477 pr_debug("Allowing explict CDB: 0x%02x for %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800478 " reservation holder\n", cdb[0],
479 core_scsi3_pr_dump_type(pr_reg_type));
Andy Grover8b1e1242012-04-03 15:51:12 -0700480
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800481 return ret;
482 }
483 /*
484 * Check if write exclusive initiator ports *NOT* holding the
485 * WRITE_EXCLUSIVE_* reservation.
486 */
Andy Groveree1b1b92012-07-12 17:34:54 -0700487 if (we && !registered_nexus) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800488 if (cmd->data_direction == DMA_TO_DEVICE) {
489 /*
490 * Conflict for write exclusive
491 */
Andy Grover6708bb22011-06-08 10:36:43 -0700492 pr_debug("%s Conflict for unregistered nexus"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800493 " %s CDB: 0x%02x to %s reservation\n",
494 transport_dump_cmd_direction(cmd),
495 se_sess->se_node_acl->initiatorname, cdb[0],
496 core_scsi3_pr_dump_type(pr_reg_type));
497 return 1;
498 } else {
499 /*
500 * Allow non WRITE CDBs for all Write Exclusive
501 * PR TYPEs to pass for registered and
502 * non-registered_nexuxes NOT holding the reservation.
503 *
504 * We only make noise for the unregisterd nexuses,
505 * as we expect registered non-reservation holding
506 * nexuses to issue CDBs.
507 */
Andy Grover8b1e1242012-04-03 15:51:12 -0700508
Andy Grover6708bb22011-06-08 10:36:43 -0700509 if (!registered_nexus) {
510 pr_debug("Allowing implict CDB: 0x%02x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800511 " for %s reservation on unregistered"
512 " nexus\n", cdb[0],
513 core_scsi3_pr_dump_type(pr_reg_type));
514 }
Andy Grover8b1e1242012-04-03 15:51:12 -0700515
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800516 return 0;
517 }
518 } else if ((reg_only) || (all_reg)) {
519 if (registered_nexus) {
520 /*
521 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
522 * allow commands from registered nexuses.
523 */
Andy Grover8b1e1242012-04-03 15:51:12 -0700524
Andy Grover6708bb22011-06-08 10:36:43 -0700525 pr_debug("Allowing implict CDB: 0x%02x for %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800526 " reservation\n", cdb[0],
527 core_scsi3_pr_dump_type(pr_reg_type));
Andy Grover8b1e1242012-04-03 15:51:12 -0700528
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800529 return 0;
530 }
531 }
Andy Grover6708bb22011-06-08 10:36:43 -0700532 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800533 " for %s reservation\n", transport_dump_cmd_direction(cmd),
534 (registered_nexus) ? "" : "un",
535 se_sess->se_node_acl->initiatorname, cdb[0],
536 core_scsi3_pr_dump_type(pr_reg_type));
537
538 return 1; /* Conflict by default */
539}
540
Christoph Hellwigde103c92012-11-06 12:24:09 -0800541static sense_reason_t
542target_scsi3_pr_reservation_check(struct se_cmd *cmd)
Christoph Hellwigd977f432012-10-10 17:37:15 -0400543{
544 struct se_device *dev = cmd->se_dev;
545 struct se_session *sess = cmd->se_sess;
546 u32 pr_reg_type;
547
548 if (!dev->dev_pr_res_holder)
549 return 0;
550
551 pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
552 cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
553 if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl)
554 goto check_nonholder;
555
556 if (dev->dev_pr_res_holder->isid_present_at_reg) {
557 if (dev->dev_pr_res_holder->pr_reg_bin_isid !=
558 sess->sess_bin_isid) {
559 pr_reg_type |= 0x80000000;
560 goto check_nonholder;
561 }
562 }
563
564 return 0;
565
566check_nonholder:
567 if (core_scsi3_pr_seq_non_holder(cmd, pr_reg_type))
Christoph Hellwigde103c92012-11-06 12:24:09 -0800568 return TCM_RESERVATION_CONFLICT;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400569 return 0;
570}
571
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800572static u32 core_scsi3_pr_generation(struct se_device *dev)
573{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800574 u32 prg;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400575
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800576 /*
577 * PRGeneration field shall contain the value of a 32-bit wrapping
578 * counter mainted by the device server.
579 *
580 * Note that this is done regardless of Active Persist across
581 * Target PowerLoss (APTPL)
582 *
583 * See spc4r17 section 6.3.12 READ_KEYS service action
584 */
585 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400586 prg = dev->t10_pr.pr_generation++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800587 spin_unlock(&dev->dev_reservation_lock);
588
589 return prg;
590}
591
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800592static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
593 struct se_device *dev,
594 struct se_node_acl *nacl,
595 struct se_dev_entry *deve,
596 unsigned char *isid,
597 u64 sa_res_key,
598 int all_tg_pt,
599 int aptpl)
600{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800601 struct t10_pr_registration *pr_reg;
602
603 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
Andy Grover6708bb22011-06-08 10:36:43 -0700604 if (!pr_reg) {
605 pr_err("Unable to allocate struct t10_pr_registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800606 return NULL;
607 }
608
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800609 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
610 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
611 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
612 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
613 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
614 atomic_set(&pr_reg->pr_res_holders, 0);
615 pr_reg->pr_reg_nacl = nacl;
616 pr_reg->pr_reg_deve = deve;
617 pr_reg->pr_res_mapped_lun = deve->mapped_lun;
618 pr_reg->pr_aptpl_target_lun = deve->se_lun->unpacked_lun;
619 pr_reg->pr_res_key = sa_res_key;
620 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
621 pr_reg->pr_reg_aptpl = aptpl;
622 pr_reg->pr_reg_tg_pt_lun = deve->se_lun;
623 /*
624 * If an ISID value for this SCSI Initiator Port exists,
625 * save it to the registration now.
626 */
627 if (isid != NULL) {
628 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
629 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
630 pr_reg->isid_present_at_reg = 1;
631 }
632
633 return pr_reg;
634}
635
636static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
637static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
638
639/*
640 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
641 * modes.
642 */
643static struct t10_pr_registration *__core_scsi3_alloc_registration(
644 struct se_device *dev,
645 struct se_node_acl *nacl,
646 struct se_dev_entry *deve,
647 unsigned char *isid,
648 u64 sa_res_key,
649 int all_tg_pt,
650 int aptpl)
651{
652 struct se_dev_entry *deve_tmp;
653 struct se_node_acl *nacl_tmp;
654 struct se_port *port, *port_tmp;
655 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
656 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
657 int ret;
658 /*
659 * Create a registration for the I_T Nexus upon which the
660 * PROUT REGISTER was received.
661 */
662 pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, deve, isid,
663 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -0700664 if (!pr_reg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800665 return NULL;
666 /*
667 * Return pointer to pr_reg for ALL_TG_PT=0
668 */
Andy Grover6708bb22011-06-08 10:36:43 -0700669 if (!all_tg_pt)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800670 return pr_reg;
671 /*
672 * Create list of matching SCSI Initiator Port registrations
673 * for ALL_TG_PT=1
674 */
675 spin_lock(&dev->se_port_lock);
676 list_for_each_entry_safe(port, port_tmp, &dev->dev_sep_list, sep_list) {
677 atomic_inc(&port->sep_tg_pt_ref_cnt);
678 smp_mb__after_atomic_inc();
679 spin_unlock(&dev->se_port_lock);
680
681 spin_lock_bh(&port->sep_alua_lock);
682 list_for_each_entry(deve_tmp, &port->sep_alua_list,
683 alua_port_list) {
684 /*
685 * This pointer will be NULL for demo mode MappedLUNs
686 * that have not been make explict via a ConfigFS
687 * MappedLUN group for the SCSI Initiator Node ACL.
688 */
Andy Grover6708bb22011-06-08 10:36:43 -0700689 if (!deve_tmp->se_lun_acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800690 continue;
691
692 nacl_tmp = deve_tmp->se_lun_acl->se_lun_nacl;
693 /*
694 * Skip the matching struct se_node_acl that is allocated
695 * above..
696 */
697 if (nacl == nacl_tmp)
698 continue;
699 /*
700 * Only perform PR registrations for target ports on
701 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
702 * arrived.
703 */
704 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
705 continue;
706 /*
707 * Look for a matching Initiator Node ACL in ASCII format
708 */
709 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
710 continue;
711
712 atomic_inc(&deve_tmp->pr_ref_count);
713 smp_mb__after_atomic_inc();
714 spin_unlock_bh(&port->sep_alua_lock);
715 /*
716 * Grab a configfs group dependency that is released
717 * for the exception path at label out: below, or upon
718 * completion of adding ALL_TG_PT=1 registrations in
719 * __core_scsi3_add_registration()
720 */
721 ret = core_scsi3_lunacl_depend_item(deve_tmp);
722 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -0700723 pr_err("core_scsi3_lunacl_depend"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800724 "_item() failed\n");
725 atomic_dec(&port->sep_tg_pt_ref_cnt);
726 smp_mb__after_atomic_dec();
727 atomic_dec(&deve_tmp->pr_ref_count);
728 smp_mb__after_atomic_dec();
729 goto out;
730 }
731 /*
732 * Located a matching SCSI Initiator Port on a different
733 * port, allocate the pr_reg_atp and attach it to the
734 * pr_reg->pr_reg_atp_list that will be processed once
735 * the original *pr_reg is processed in
736 * __core_scsi3_add_registration()
737 */
738 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
739 nacl_tmp, deve_tmp, NULL,
740 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -0700741 if (!pr_reg_atp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800742 atomic_dec(&port->sep_tg_pt_ref_cnt);
743 smp_mb__after_atomic_dec();
744 atomic_dec(&deve_tmp->pr_ref_count);
745 smp_mb__after_atomic_dec();
746 core_scsi3_lunacl_undepend_item(deve_tmp);
747 goto out;
748 }
749
750 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
751 &pr_reg->pr_reg_atp_list);
752 spin_lock_bh(&port->sep_alua_lock);
753 }
754 spin_unlock_bh(&port->sep_alua_lock);
755
756 spin_lock(&dev->se_port_lock);
757 atomic_dec(&port->sep_tg_pt_ref_cnt);
758 smp_mb__after_atomic_dec();
759 }
760 spin_unlock(&dev->se_port_lock);
761
762 return pr_reg;
763out:
764 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
765 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
766 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
767 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
768 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
769 }
770 kmem_cache_free(t10_pr_reg_cache, pr_reg);
771 return NULL;
772}
773
774int core_scsi3_alloc_aptpl_registration(
Andy Grovere3d6f902011-07-19 08:55:10 +0000775 struct t10_reservation *pr_tmpl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800776 u64 sa_res_key,
777 unsigned char *i_port,
778 unsigned char *isid,
779 u32 mapped_lun,
780 unsigned char *t_port,
781 u16 tpgt,
782 u32 target_lun,
783 int res_holder,
784 int all_tg_pt,
785 u8 type)
786{
787 struct t10_pr_registration *pr_reg;
788
Andy Grover6708bb22011-06-08 10:36:43 -0700789 if (!i_port || !t_port || !sa_res_key) {
790 pr_err("Illegal parameters for APTPL registration\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000791 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800792 }
793
794 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700795 if (!pr_reg) {
796 pr_err("Unable to allocate struct t10_pr_registration\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000797 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800798 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800799
800 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
801 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
802 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
803 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
804 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
805 atomic_set(&pr_reg->pr_res_holders, 0);
806 pr_reg->pr_reg_nacl = NULL;
807 pr_reg->pr_reg_deve = NULL;
808 pr_reg->pr_res_mapped_lun = mapped_lun;
809 pr_reg->pr_aptpl_target_lun = target_lun;
810 pr_reg->pr_res_key = sa_res_key;
811 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
812 pr_reg->pr_reg_aptpl = 1;
813 pr_reg->pr_reg_tg_pt_lun = NULL;
814 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
815 pr_reg->pr_res_type = type;
816 /*
817 * If an ISID value had been saved in APTPL metadata for this
818 * SCSI Initiator Port, restore it now.
819 */
820 if (isid != NULL) {
821 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
822 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
823 pr_reg->isid_present_at_reg = 1;
824 }
825 /*
826 * Copy the i_port and t_port information from caller.
827 */
828 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
829 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
830 pr_reg->pr_reg_tpgt = tpgt;
831 /*
832 * Set pr_res_holder from caller, the pr_reg who is the reservation
833 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
834 * the Initiator Node LUN ACL from the fabric module is created for
835 * this registration.
836 */
837 pr_reg->pr_res_holder = res_holder;
838
839 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
Andy Grover6708bb22011-06-08 10:36:43 -0700840 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800841 " metadata\n", (res_holder) ? "+reservation" : "");
842 return 0;
843}
844
845static void core_scsi3_aptpl_reserve(
846 struct se_device *dev,
847 struct se_portal_group *tpg,
848 struct se_node_acl *node_acl,
849 struct t10_pr_registration *pr_reg)
850{
851 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800852
853 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -0700854 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800855
856 spin_lock(&dev->dev_reservation_lock);
857 dev->dev_pr_res_holder = pr_reg;
858 spin_unlock(&dev->dev_reservation_lock);
859
Andy Grover6708bb22011-06-08 10:36:43 -0700860 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800861 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000862 tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800863 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
864 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -0700865 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000866 tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -0700867 i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800868}
869
870static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
Andy Grover33ce6a82013-05-16 10:40:54 -0700871 struct t10_pr_registration *, enum register_type, int);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800872
873static int __core_scsi3_check_aptpl_registration(
874 struct se_device *dev,
875 struct se_portal_group *tpg,
876 struct se_lun *lun,
877 u32 target_lun,
878 struct se_node_acl *nacl,
879 struct se_dev_entry *deve)
880{
881 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400882 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800883 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
884 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
885 u16 tpgt;
886
887 memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
888 memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
889 /*
890 * Copy Initiator Port information from struct se_node_acl
891 */
892 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
893 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
Andy Grovere3d6f902011-07-19 08:55:10 +0000894 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
895 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800896 /*
897 * Look for the matching registrations+reservation from those
898 * created from APTPL metadata. Note that multiple registrations
899 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
900 * TransportIDs.
901 */
902 spin_lock(&pr_tmpl->aptpl_reg_lock);
903 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
904 pr_reg_aptpl_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700905 if (!strcmp(pr_reg->pr_iport, i_port) &&
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800906 (pr_reg->pr_res_mapped_lun == deve->mapped_lun) &&
907 !(strcmp(pr_reg->pr_tport, t_port)) &&
908 (pr_reg->pr_reg_tpgt == tpgt) &&
909 (pr_reg->pr_aptpl_target_lun == target_lun)) {
910
911 pr_reg->pr_reg_nacl = nacl;
912 pr_reg->pr_reg_deve = deve;
913 pr_reg->pr_reg_tg_pt_lun = lun;
914
915 list_del(&pr_reg->pr_reg_aptpl_list);
916 spin_unlock(&pr_tmpl->aptpl_reg_lock);
917 /*
918 * At this point all of the pointers in *pr_reg will
919 * be setup, so go ahead and add the registration.
920 */
921
922 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
923 /*
924 * If this registration is the reservation holder,
925 * make that happen now..
926 */
927 if (pr_reg->pr_res_holder)
928 core_scsi3_aptpl_reserve(dev, tpg,
929 nacl, pr_reg);
930 /*
931 * Reenable pr_aptpl_active to accept new metadata
932 * updates once the SCSI device is active again..
933 */
934 spin_lock(&pr_tmpl->aptpl_reg_lock);
935 pr_tmpl->pr_aptpl_active = 1;
936 }
937 }
938 spin_unlock(&pr_tmpl->aptpl_reg_lock);
939
940 return 0;
941}
942
943int core_scsi3_check_aptpl_registration(
944 struct se_device *dev,
945 struct se_portal_group *tpg,
946 struct se_lun *lun,
947 struct se_lun_acl *lun_acl)
948{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800949 struct se_node_acl *nacl = lun_acl->se_lun_nacl;
Jörn Engelf2083242012-03-15 15:05:40 -0400950 struct se_dev_entry *deve = nacl->device_list[lun_acl->mapped_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800951
Christoph Hellwigd977f432012-10-10 17:37:15 -0400952 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800953 return 0;
954
955 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
956 lun->unpacked_lun, nacl, deve);
957}
958
959static void __core_scsi3_dump_registration(
960 struct target_core_fabric_ops *tfo,
961 struct se_device *dev,
962 struct se_node_acl *nacl,
963 struct t10_pr_registration *pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -0700964 enum register_type register_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800965{
966 struct se_portal_group *se_tpg = nacl->se_tpg;
967 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800968
969 memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -0700970 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800971
Andy Grover6708bb22011-06-08 10:36:43 -0700972 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
Andy Grover33ce6a82013-05-16 10:40:54 -0700973 " Node: %s%s\n", tfo->get_fabric_name(), (register_type == REGISTER_AND_MOVE) ?
974 "_AND_MOVE" : (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ?
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800975 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -0700976 i_buf);
Andy Grover6708bb22011-06-08 10:36:43 -0700977 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800978 tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg),
979 tfo->tpg_get_tag(se_tpg));
Andy Grover6708bb22011-06-08 10:36:43 -0700980 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800981 " Port(s)\n", tfo->get_fabric_name(),
982 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
Andy Grovere3d6f902011-07-19 08:55:10 +0000983 dev->transport->name);
Andy Grover6708bb22011-06-08 10:36:43 -0700984 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800985 " 0x%08x APTPL: %d\n", tfo->get_fabric_name(),
986 pr_reg->pr_res_key, pr_reg->pr_res_generation,
987 pr_reg->pr_reg_aptpl);
988}
989
990/*
991 * this function can be called with struct se_device->dev_reservation_lock
992 * when register_move = 1
993 */
994static void __core_scsi3_add_registration(
995 struct se_device *dev,
996 struct se_node_acl *nacl,
997 struct t10_pr_registration *pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -0700998 enum register_type register_type,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800999 int register_move)
1000{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001001 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1002 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001003 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001004
1005 /*
1006 * Increment PRgeneration counter for struct se_device upon a successful
1007 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1008 *
1009 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1010 * action, the struct se_device->dev_reservation_lock will already be held,
1011 * so we do not call core_scsi3_pr_generation() which grabs the lock
1012 * for the REGISTER.
1013 */
1014 pr_reg->pr_res_generation = (register_move) ?
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001015 dev->t10_pr.pr_generation++ :
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001016 core_scsi3_pr_generation(dev);
1017
1018 spin_lock(&pr_tmpl->registration_lock);
1019 list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
1020 pr_reg->pr_reg_deve->def_pr_registered = 1;
1021
1022 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1023 spin_unlock(&pr_tmpl->registration_lock);
1024 /*
1025 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1026 */
Andy Grover6708bb22011-06-08 10:36:43 -07001027 if (!pr_reg->pr_reg_all_tg_pt || register_move)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001028 return;
1029 /*
1030 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1031 * allocated in __core_scsi3_alloc_registration()
1032 */
1033 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1034 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
1035 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1036
1037 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1038
1039 spin_lock(&pr_tmpl->registration_lock);
1040 list_add_tail(&pr_reg_tmp->pr_reg_list,
1041 &pr_tmpl->registration_list);
1042 pr_reg_tmp->pr_reg_deve->def_pr_registered = 1;
1043
1044 __core_scsi3_dump_registration(tfo, dev,
1045 pr_reg_tmp->pr_reg_nacl, pr_reg_tmp,
1046 register_type);
1047 spin_unlock(&pr_tmpl->registration_lock);
1048 /*
1049 * Drop configfs group dependency reference from
1050 * __core_scsi3_alloc_registration()
1051 */
1052 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1053 }
1054}
1055
1056static int core_scsi3_alloc_registration(
1057 struct se_device *dev,
1058 struct se_node_acl *nacl,
1059 struct se_dev_entry *deve,
1060 unsigned char *isid,
1061 u64 sa_res_key,
1062 int all_tg_pt,
1063 int aptpl,
Andy Grover33ce6a82013-05-16 10:40:54 -07001064 enum register_type register_type,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001065 int register_move)
1066{
1067 struct t10_pr_registration *pr_reg;
1068
1069 pr_reg = __core_scsi3_alloc_registration(dev, nacl, deve, isid,
1070 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001071 if (!pr_reg)
Andy Grovere3d6f902011-07-19 08:55:10 +00001072 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001073
1074 __core_scsi3_add_registration(dev, nacl, pr_reg,
1075 register_type, register_move);
1076 return 0;
1077}
1078
1079static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1080 struct se_device *dev,
1081 struct se_node_acl *nacl,
1082 unsigned char *isid)
1083{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001084 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001085 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1086 struct se_portal_group *tpg;
1087
1088 spin_lock(&pr_tmpl->registration_lock);
1089 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1090 &pr_tmpl->registration_list, pr_reg_list) {
1091 /*
1092 * First look for a matching struct se_node_acl
1093 */
1094 if (pr_reg->pr_reg_nacl != nacl)
1095 continue;
1096
1097 tpg = pr_reg->pr_reg_nacl->se_tpg;
1098 /*
1099 * If this registration does NOT contain a fabric provided
1100 * ISID, then we have found a match.
1101 */
Andy Grover6708bb22011-06-08 10:36:43 -07001102 if (!pr_reg->isid_present_at_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001103 /*
1104 * Determine if this SCSI device server requires that
1105 * SCSI Intiatior TransportID w/ ISIDs is enforced
1106 * for fabric modules (iSCSI) requiring them.
1107 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001108 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001109 if (dev->dev_attrib.enforce_pr_isids)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001110 continue;
1111 }
1112 atomic_inc(&pr_reg->pr_res_holders);
1113 smp_mb__after_atomic_inc();
1114 spin_unlock(&pr_tmpl->registration_lock);
1115 return pr_reg;
1116 }
1117 /*
1118 * If the *pr_reg contains a fabric defined ISID for multi-value
1119 * SCSI Initiator Port TransportIDs, then we expect a valid
1120 * matching ISID to be provided by the local SCSI Initiator Port.
1121 */
Andy Grover6708bb22011-06-08 10:36:43 -07001122 if (!isid)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001123 continue;
1124 if (strcmp(isid, pr_reg->pr_reg_isid))
1125 continue;
1126
1127 atomic_inc(&pr_reg->pr_res_holders);
1128 smp_mb__after_atomic_inc();
1129 spin_unlock(&pr_tmpl->registration_lock);
1130 return pr_reg;
1131 }
1132 spin_unlock(&pr_tmpl->registration_lock);
1133
1134 return NULL;
1135}
1136
1137static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1138 struct se_device *dev,
1139 struct se_node_acl *nacl,
1140 struct se_session *sess)
1141{
1142 struct se_portal_group *tpg = nacl->se_tpg;
1143 unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1144
Andy Grovere3d6f902011-07-19 08:55:10 +00001145 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001146 memset(&buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +00001147 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001148 PR_REG_ISID_LEN);
1149 isid_ptr = &buf[0];
1150 }
1151
1152 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1153}
1154
1155static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1156{
1157 atomic_dec(&pr_reg->pr_res_holders);
1158 smp_mb__after_atomic_dec();
1159}
1160
1161static int core_scsi3_check_implict_release(
1162 struct se_device *dev,
1163 struct t10_pr_registration *pr_reg)
1164{
1165 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1166 struct t10_pr_registration *pr_res_holder;
1167 int ret = 0;
1168
1169 spin_lock(&dev->dev_reservation_lock);
1170 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001171 if (!pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001172 spin_unlock(&dev->dev_reservation_lock);
1173 return ret;
1174 }
1175 if (pr_res_holder == pr_reg) {
1176 /*
1177 * Perform an implict RELEASE if the registration that
1178 * is being released is holding the reservation.
1179 *
1180 * From spc4r17, section 5.7.11.1:
1181 *
1182 * e) If the I_T nexus is the persistent reservation holder
1183 * and the persistent reservation is not an all registrants
1184 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1185 * service action or REGISTER AND IGNORE EXISTING KEY
1186 * service action with the SERVICE ACTION RESERVATION KEY
1187 * field set to zero (see 5.7.11.3).
1188 */
1189 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0);
1190 ret = 1;
1191 /*
1192 * For 'All Registrants' reservation types, all existing
1193 * registrations are still processed as reservation holders
1194 * in core_scsi3_pr_seq_non_holder() after the initial
1195 * reservation holder is implictly released here.
1196 */
1197 } else if (pr_reg->pr_reg_all_tg_pt &&
1198 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1199 pr_reg->pr_reg_nacl->initiatorname)) &&
1200 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001201 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001202 " UNREGISTER while existing reservation with matching"
1203 " key 0x%016Lx is present from another SCSI Initiator"
1204 " Port\n", pr_reg->pr_res_key);
Andy Grovere3d6f902011-07-19 08:55:10 +00001205 ret = -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001206 }
1207 spin_unlock(&dev->dev_reservation_lock);
1208
1209 return ret;
1210}
1211
1212/*
Andy Grovere3d6f902011-07-19 08:55:10 +00001213 * Called with struct t10_reservation->registration_lock held.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001214 */
1215static void __core_scsi3_free_registration(
1216 struct se_device *dev,
1217 struct t10_pr_registration *pr_reg,
1218 struct list_head *preempt_and_abort_list,
1219 int dec_holders)
1220{
1221 struct target_core_fabric_ops *tfo =
1222 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001223 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001224 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001225
1226 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07001227 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001228
1229 pr_reg->pr_reg_deve->def_pr_registered = 0;
1230 pr_reg->pr_reg_deve->pr_res_key = 0;
1231 list_del(&pr_reg->pr_reg_list);
1232 /*
1233 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1234 * so call core_scsi3_put_pr_reg() to decrement our reference.
1235 */
1236 if (dec_holders)
1237 core_scsi3_put_pr_reg(pr_reg);
1238 /*
1239 * Wait until all reference from any other I_T nexuses for this
1240 * *pr_reg have been released. Because list_del() is called above,
1241 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1242 * count back to zero, and we release *pr_reg.
1243 */
1244 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
1245 spin_unlock(&pr_tmpl->registration_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07001246 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001247 tfo->get_fabric_name());
1248 cpu_relax();
1249 spin_lock(&pr_tmpl->registration_lock);
1250 }
1251
Andy Grover6708bb22011-06-08 10:36:43 -07001252 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001253 " Node: %s%s\n", tfo->get_fabric_name(),
1254 pr_reg->pr_reg_nacl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07001255 i_buf);
Andy Grover6708bb22011-06-08 10:36:43 -07001256 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001257 " Port(s)\n", tfo->get_fabric_name(),
1258 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
Andy Grovere3d6f902011-07-19 08:55:10 +00001259 dev->transport->name);
Andy Grover6708bb22011-06-08 10:36:43 -07001260 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001261 " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key,
1262 pr_reg->pr_res_generation);
1263
Andy Grover6708bb22011-06-08 10:36:43 -07001264 if (!preempt_and_abort_list) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001265 pr_reg->pr_reg_deve = NULL;
1266 pr_reg->pr_reg_nacl = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001267 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1268 return;
1269 }
1270 /*
1271 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1272 * are released once the ABORT_TASK_SET has completed..
1273 */
1274 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1275}
1276
1277void core_scsi3_free_pr_reg_from_nacl(
1278 struct se_device *dev,
1279 struct se_node_acl *nacl)
1280{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001281 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001282 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1283 /*
1284 * If the passed se_node_acl matches the reservation holder,
1285 * release the reservation.
1286 */
1287 spin_lock(&dev->dev_reservation_lock);
1288 pr_res_holder = dev->dev_pr_res_holder;
1289 if ((pr_res_holder != NULL) &&
1290 (pr_res_holder->pr_reg_nacl == nacl))
1291 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0);
1292 spin_unlock(&dev->dev_reservation_lock);
1293 /*
1294 * Release any registration associated with the struct se_node_acl.
1295 */
1296 spin_lock(&pr_tmpl->registration_lock);
1297 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1298 &pr_tmpl->registration_list, pr_reg_list) {
1299
1300 if (pr_reg->pr_reg_nacl != nacl)
1301 continue;
1302
1303 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1304 }
1305 spin_unlock(&pr_tmpl->registration_lock);
1306}
1307
1308void core_scsi3_free_all_registrations(
1309 struct se_device *dev)
1310{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001311 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001312 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1313
1314 spin_lock(&dev->dev_reservation_lock);
1315 pr_res_holder = dev->dev_pr_res_holder;
1316 if (pr_res_holder != NULL) {
1317 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1318 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
1319 pr_res_holder, 0);
1320 }
1321 spin_unlock(&dev->dev_reservation_lock);
1322
1323 spin_lock(&pr_tmpl->registration_lock);
1324 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1325 &pr_tmpl->registration_list, pr_reg_list) {
1326
1327 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1328 }
1329 spin_unlock(&pr_tmpl->registration_lock);
1330
1331 spin_lock(&pr_tmpl->aptpl_reg_lock);
1332 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1333 pr_reg_aptpl_list) {
1334 list_del(&pr_reg->pr_reg_aptpl_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001335 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1336 }
1337 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1338}
1339
1340static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1341{
Andy Grovere3d6f902011-07-19 08:55:10 +00001342 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001343 &tpg->tpg_group.cg_item);
1344}
1345
1346static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1347{
Andy Grovere3d6f902011-07-19 08:55:10 +00001348 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001349 &tpg->tpg_group.cg_item);
1350
1351 atomic_dec(&tpg->tpg_pr_ref_count);
1352 smp_mb__after_atomic_dec();
1353}
1354
1355static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1356{
1357 struct se_portal_group *tpg = nacl->se_tpg;
1358
1359 if (nacl->dynamic_node_acl)
1360 return 0;
1361
Andy Grovere3d6f902011-07-19 08:55:10 +00001362 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001363 &nacl->acl_group.cg_item);
1364}
1365
1366static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1367{
1368 struct se_portal_group *tpg = nacl->se_tpg;
1369
1370 if (nacl->dynamic_node_acl) {
1371 atomic_dec(&nacl->acl_pr_ref_count);
1372 smp_mb__after_atomic_dec();
1373 return;
1374 }
1375
Andy Grovere3d6f902011-07-19 08:55:10 +00001376 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001377 &nacl->acl_group.cg_item);
1378
1379 atomic_dec(&nacl->acl_pr_ref_count);
1380 smp_mb__after_atomic_dec();
1381}
1382
1383static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1384{
1385 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1386 struct se_node_acl *nacl;
1387 struct se_portal_group *tpg;
1388 /*
1389 * For nacl->dynamic_node_acl=1
1390 */
Andy Grover6708bb22011-06-08 10:36:43 -07001391 if (!lun_acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001392 return 0;
1393
1394 nacl = lun_acl->se_lun_nacl;
1395 tpg = nacl->se_tpg;
1396
Andy Grovere3d6f902011-07-19 08:55:10 +00001397 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001398 &lun_acl->se_lun_group.cg_item);
1399}
1400
1401static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1402{
1403 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1404 struct se_node_acl *nacl;
1405 struct se_portal_group *tpg;
1406 /*
1407 * For nacl->dynamic_node_acl=1
1408 */
Andy Grover6708bb22011-06-08 10:36:43 -07001409 if (!lun_acl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001410 atomic_dec(&se_deve->pr_ref_count);
1411 smp_mb__after_atomic_dec();
1412 return;
1413 }
1414 nacl = lun_acl->se_lun_nacl;
1415 tpg = nacl->se_tpg;
1416
Andy Grovere3d6f902011-07-19 08:55:10 +00001417 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001418 &lun_acl->se_lun_group.cg_item);
1419
1420 atomic_dec(&se_deve->pr_ref_count);
1421 smp_mb__after_atomic_dec();
1422}
1423
Christoph Hellwigde103c92012-11-06 12:24:09 -08001424static sense_reason_t
1425core_scsi3_decode_spec_i_port(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001426 struct se_cmd *cmd,
1427 struct se_portal_group *tpg,
1428 unsigned char *l_isid,
1429 u64 sa_res_key,
1430 int all_tg_pt,
1431 int aptpl)
1432{
Andy Grover5951146d2011-07-19 10:26:37 +00001433 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001434 struct se_port *tmp_port;
1435 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00001436 struct se_session *se_sess = cmd->se_sess;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001437 struct se_node_acl *dest_node_acl = NULL;
1438 struct se_dev_entry *dest_se_deve = NULL, *local_se_deve;
1439 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1440 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
Roland Dreierd0f474e2012-01-12 10:41:18 -08001441 LIST_HEAD(tid_dest_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001442 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
1443 struct target_core_fabric_ops *tmp_tf_ops;
Andy Grover05d1c7c2011-07-20 19:13:28 +00001444 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001445 unsigned char *ptr, *i_str = NULL, proto_ident, tmp_proto_ident;
1446 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
Christoph Hellwigde103c92012-11-06 12:24:09 -08001447 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001448 u32 tpdl, tid_len = 0;
Andy Groverd2843c12013-05-16 10:40:55 -07001449 int dest_local_nexus;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001450 u32 dest_rtpi = 0;
1451
1452 memset(dest_iport, 0, 64);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001453
Jörn Engelf2083242012-03-15 15:05:40 -04001454 local_se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001455 /*
1456 * Allocate a struct pr_transport_id_holder and setup the
1457 * local_node_acl and local_se_deve pointers and add to
1458 * struct list_head tid_dest_list for add registration
1459 * processing in the loop of tid_dest_list below.
1460 */
1461 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001462 if (!tidh_new) {
1463 pr_err("Unable to allocate tidh_new\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08001464 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001465 }
1466 INIT_LIST_HEAD(&tidh_new->dest_list);
1467 tidh_new->dest_tpg = tpg;
1468 tidh_new->dest_node_acl = se_sess->se_node_acl;
1469 tidh_new->dest_se_deve = local_se_deve;
1470
Andy Grover5951146d2011-07-19 10:26:37 +00001471 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001472 se_sess->se_node_acl, local_se_deve, l_isid,
1473 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001474 if (!local_pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001475 kfree(tidh_new);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001476 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001477 }
1478 tidh_new->dest_pr_reg = local_pr_reg;
1479 /*
1480 * The local I_T nexus does not hold any configfs dependances,
1481 * so we set tid_h->dest_local_nexus=1 to prevent the
1482 * configfs_undepend_item() calls in the tid_dest_list loops below.
1483 */
1484 tidh_new->dest_local_nexus = 1;
1485 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
Andy Grover05d1c7c2011-07-20 19:13:28 +00001486
Paolo Bonzini0d7f1292012-09-07 17:30:33 +02001487 if (cmd->data_length < 28) {
1488 pr_warn("SPC-PR: Received PR OUT parameter list"
1489 " length too small: %u\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001490 ret = TCM_INVALID_PARAMETER_LIST;
Paolo Bonzini0d7f1292012-09-07 17:30:33 +02001491 goto out;
1492 }
1493
Andy Grover49493142012-01-16 16:57:08 -08001494 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001495 if (!buf) {
1496 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1497 goto out;
1498 }
1499
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001500 /*
1501 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1502 * first extract TransportID Parameter Data Length, and make sure
1503 * the value matches up to the SCSI expected data transfer length.
1504 */
1505 tpdl = (buf[24] & 0xff) << 24;
1506 tpdl |= (buf[25] & 0xff) << 16;
1507 tpdl |= (buf[26] & 0xff) << 8;
1508 tpdl |= buf[27] & 0xff;
1509
1510 if ((tpdl + 28) != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07001511 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001512 " does not equal CDB data_length: %u\n", tpdl,
1513 cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001514 ret = TCM_INVALID_PARAMETER_LIST;
1515 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001516 }
1517 /*
1518 * Start processing the received transport IDs using the
1519 * receiving I_T Nexus portal's fabric dependent methods to
1520 * obtain the SCSI Initiator Port/Device Identifiers.
1521 */
1522 ptr = &buf[28];
1523
1524 while (tpdl > 0) {
1525 proto_ident = (ptr[0] & 0x0f);
1526 dest_tpg = NULL;
1527
1528 spin_lock(&dev->se_port_lock);
1529 list_for_each_entry(tmp_port, &dev->dev_sep_list, sep_list) {
1530 tmp_tpg = tmp_port->sep_tpg;
Andy Grover6708bb22011-06-08 10:36:43 -07001531 if (!tmp_tpg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001532 continue;
Andy Grovere3d6f902011-07-19 08:55:10 +00001533 tmp_tf_ops = tmp_tpg->se_tpg_tfo;
Andy Grover6708bb22011-06-08 10:36:43 -07001534 if (!tmp_tf_ops)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001535 continue;
Andy Grover6708bb22011-06-08 10:36:43 -07001536 if (!tmp_tf_ops->get_fabric_proto_ident ||
1537 !tmp_tf_ops->tpg_parse_pr_out_transport_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001538 continue;
1539 /*
1540 * Look for the matching proto_ident provided by
1541 * the received TransportID
1542 */
1543 tmp_proto_ident = tmp_tf_ops->get_fabric_proto_ident(tmp_tpg);
1544 if (tmp_proto_ident != proto_ident)
1545 continue;
1546 dest_rtpi = tmp_port->sep_rtpi;
1547
1548 i_str = tmp_tf_ops->tpg_parse_pr_out_transport_id(
1549 tmp_tpg, (const char *)ptr, &tid_len,
1550 &iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07001551 if (!i_str)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001552 continue;
1553
1554 atomic_inc(&tmp_tpg->tpg_pr_ref_count);
1555 smp_mb__after_atomic_inc();
1556 spin_unlock(&dev->se_port_lock);
1557
Christoph Hellwigde103c92012-11-06 12:24:09 -08001558 if (core_scsi3_tpg_depend_item(tmp_tpg)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001559 pr_err(" core_scsi3_tpg_depend_item()"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001560 " for tmp_tpg\n");
1561 atomic_dec(&tmp_tpg->tpg_pr_ref_count);
1562 smp_mb__after_atomic_dec();
Christoph Hellwigde103c92012-11-06 12:24:09 -08001563 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1564 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001565 }
1566 /*
Masanari Iida35d1efe2012-08-16 22:43:13 +09001567 * Locate the destination initiator ACL to be registered
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001568 * from the decoded fabric module specific TransportID
1569 * at *i_str.
1570 */
Roland Dreier286388872011-08-16 09:40:01 -07001571 spin_lock_irq(&tmp_tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001572 dest_node_acl = __core_tpg_get_initiator_node_acl(
1573 tmp_tpg, i_str);
1574 if (dest_node_acl) {
1575 atomic_inc(&dest_node_acl->acl_pr_ref_count);
1576 smp_mb__after_atomic_inc();
1577 }
Roland Dreier286388872011-08-16 09:40:01 -07001578 spin_unlock_irq(&tmp_tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001579
Andy Grover6708bb22011-06-08 10:36:43 -07001580 if (!dest_node_acl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001581 core_scsi3_tpg_undepend_item(tmp_tpg);
1582 spin_lock(&dev->se_port_lock);
1583 continue;
1584 }
1585
Christoph Hellwigde103c92012-11-06 12:24:09 -08001586 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001587 pr_err("configfs_depend_item() failed"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001588 " for dest_node_acl->acl_group\n");
1589 atomic_dec(&dest_node_acl->acl_pr_ref_count);
1590 smp_mb__after_atomic_dec();
1591 core_scsi3_tpg_undepend_item(tmp_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001592 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1593 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001594 }
1595
1596 dest_tpg = tmp_tpg;
Andy Grover6708bb22011-06-08 10:36:43 -07001597 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001598 " %s Port RTPI: %hu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001599 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001600 dest_node_acl->initiatorname, dest_rtpi);
1601
1602 spin_lock(&dev->se_port_lock);
1603 break;
1604 }
1605 spin_unlock(&dev->se_port_lock);
1606
Andy Grover6708bb22011-06-08 10:36:43 -07001607 if (!dest_tpg) {
1608 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001609 " dest_tpg\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08001610 ret = TCM_INVALID_PARAMETER_LIST;
1611 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001612 }
Andy Grover8b1e1242012-04-03 15:51:12 -07001613
Andy Grover6708bb22011-06-08 10:36:43 -07001614 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001615 " tid_len: %d for %s + %s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001616 dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001617 tpdl, tid_len, i_str, iport_ptr);
Andy Grover8b1e1242012-04-03 15:51:12 -07001618
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001619 if (tid_len > tpdl) {
Andy Grover6708bb22011-06-08 10:36:43 -07001620 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001621 " %u for Transport ID: %s\n", tid_len, ptr);
1622 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1623 core_scsi3_tpg_undepend_item(dest_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001624 ret = TCM_INVALID_PARAMETER_LIST;
1625 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001626 }
1627 /*
1628 * Locate the desintation struct se_dev_entry pointer for matching
1629 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1630 * Target Port.
1631 */
1632 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1633 dest_rtpi);
Andy Grover6708bb22011-06-08 10:36:43 -07001634 if (!dest_se_deve) {
1635 pr_err("Unable to locate %s dest_se_deve"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001636 " from destination RTPI: %hu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001637 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001638 dest_rtpi);
1639
1640 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1641 core_scsi3_tpg_undepend_item(dest_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001642 ret = TCM_INVALID_PARAMETER_LIST;
1643 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001644 }
1645
Christoph Hellwigde103c92012-11-06 12:24:09 -08001646 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001647 pr_err("core_scsi3_lunacl_depend_item()"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001648 " failed\n");
1649 atomic_dec(&dest_se_deve->pr_ref_count);
1650 smp_mb__after_atomic_dec();
1651 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1652 core_scsi3_tpg_undepend_item(dest_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001653 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1654 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001655 }
Andy Grover8b1e1242012-04-03 15:51:12 -07001656
Andy Grover6708bb22011-06-08 10:36:43 -07001657 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001658 " dest_se_deve mapped_lun: %u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001659 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001660 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
Andy Grover8b1e1242012-04-03 15:51:12 -07001661
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001662 /*
1663 * Skip any TransportIDs that already have a registration for
1664 * this target port.
1665 */
1666 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1667 iport_ptr);
1668 if (pr_reg_e) {
1669 core_scsi3_put_pr_reg(pr_reg_e);
1670 core_scsi3_lunacl_undepend_item(dest_se_deve);
1671 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1672 core_scsi3_tpg_undepend_item(dest_tpg);
1673 ptr += tid_len;
1674 tpdl -= tid_len;
1675 tid_len = 0;
1676 continue;
1677 }
1678 /*
1679 * Allocate a struct pr_transport_id_holder and setup
1680 * the dest_node_acl and dest_se_deve pointers for the
1681 * loop below.
1682 */
1683 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1684 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001685 if (!tidh_new) {
1686 pr_err("Unable to allocate tidh_new\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001687 core_scsi3_lunacl_undepend_item(dest_se_deve);
1688 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1689 core_scsi3_tpg_undepend_item(dest_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001690 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1691 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001692 }
1693 INIT_LIST_HEAD(&tidh_new->dest_list);
1694 tidh_new->dest_tpg = dest_tpg;
1695 tidh_new->dest_node_acl = dest_node_acl;
1696 tidh_new->dest_se_deve = dest_se_deve;
1697
1698 /*
1699 * Allocate, but do NOT add the registration for the
1700 * TransportID referenced SCSI Initiator port. This
1701 * done because of the following from spc4r17 in section
1702 * 6.14.3 wrt SPEC_I_PT:
1703 *
1704 * "If a registration fails for any initiator port (e.g., if th
1705 * logical unit does not have enough resources available to
1706 * hold the registration information), no registrations shall be
1707 * made, and the command shall be terminated with
1708 * CHECK CONDITION status."
1709 *
1710 * That means we call __core_scsi3_alloc_registration() here,
1711 * and then call __core_scsi3_add_registration() in the
1712 * 2nd loop which will never fail.
1713 */
Andy Grover5951146d2011-07-19 10:26:37 +00001714 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001715 dest_node_acl, dest_se_deve, iport_ptr,
1716 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001717 if (!dest_pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001718 core_scsi3_lunacl_undepend_item(dest_se_deve);
1719 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1720 core_scsi3_tpg_undepend_item(dest_tpg);
1721 kfree(tidh_new);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001722 ret = TCM_INVALID_PARAMETER_LIST;
1723 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001724 }
1725 tidh_new->dest_pr_reg = dest_pr_reg;
1726 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1727
1728 ptr += tid_len;
1729 tpdl -= tid_len;
1730 tid_len = 0;
1731
1732 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00001733
Andy Grover49493142012-01-16 16:57:08 -08001734 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00001735
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001736 /*
1737 * Go ahead and create a registrations from tid_dest_list for the
1738 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1739 * and dest_se_deve.
1740 *
1741 * The SA Reservation Key from the PROUT is set for the
1742 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1743 * means that the TransportID Initiator port will be
1744 * registered on all of the target ports in the SCSI target device
1745 * ALL_TG_PT=0 means the registration will only be for the
1746 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1747 * was received.
1748 */
1749 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1750 dest_tpg = tidh->dest_tpg;
1751 dest_node_acl = tidh->dest_node_acl;
1752 dest_se_deve = tidh->dest_se_deve;
1753 dest_pr_reg = tidh->dest_pr_reg;
1754 dest_local_nexus = tidh->dest_local_nexus;
1755
1756 list_del(&tidh->dest_list);
1757 kfree(tidh);
1758
1759 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07001760 core_pr_dump_initiator_port(dest_pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001761
Andy Grover5951146d2011-07-19 10:26:37 +00001762 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001763 dest_pr_reg, 0, 0);
1764
Andy Grover6708bb22011-06-08 10:36:43 -07001765 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001766 " registered Transport ID for Node: %s%s Mapped LUN:"
Andy Grovere3d6f902011-07-19 08:55:10 +00001767 " %u\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001768 dest_node_acl->initiatorname, i_buf, dest_se_deve->mapped_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001769
1770 if (dest_local_nexus)
1771 continue;
1772
1773 core_scsi3_lunacl_undepend_item(dest_se_deve);
1774 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1775 core_scsi3_tpg_undepend_item(dest_tpg);
1776 }
1777
1778 return 0;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001779out_unmap:
Andy Grover49493142012-01-16 16:57:08 -08001780 transport_kunmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001781out:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001782 /*
1783 * For the failure case, release everything from tid_dest_list
1784 * including *dest_pr_reg and the configfs dependances..
1785 */
1786 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1787 dest_tpg = tidh->dest_tpg;
1788 dest_node_acl = tidh->dest_node_acl;
1789 dest_se_deve = tidh->dest_se_deve;
1790 dest_pr_reg = tidh->dest_pr_reg;
1791 dest_local_nexus = tidh->dest_local_nexus;
1792
1793 list_del(&tidh->dest_list);
1794 kfree(tidh);
1795 /*
1796 * Release any extra ALL_TG_PT=1 registrations for
1797 * the SPEC_I_PT=1 case.
1798 */
1799 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1800 &dest_pr_reg->pr_reg_atp_list,
1801 pr_reg_atp_mem_list) {
1802 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1803 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1804 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1805 }
1806
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001807 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1808
1809 if (dest_local_nexus)
1810 continue;
1811
1812 core_scsi3_lunacl_undepend_item(dest_se_deve);
1813 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1814 core_scsi3_tpg_undepend_item(dest_tpg);
1815 }
1816 return ret;
1817}
1818
Andy Grover3c8a6222013-05-16 10:40:58 -07001819static int core_scsi3_update_aptpl_buf(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001820 struct se_device *dev,
1821 unsigned char *buf,
Andy Grover0607dec2013-05-16 10:40:57 -07001822 u32 pr_aptpl_buf_len)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001823{
1824 struct se_lun *lun;
1825 struct se_portal_group *tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001826 struct t10_pr_registration *pr_reg;
1827 unsigned char tmp[512], isid_buf[32];
1828 ssize_t len = 0;
1829 int reg_count = 0;
Andy Grover3c8a6222013-05-16 10:40:58 -07001830 int ret = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001831
Andy Grover3c8a6222013-05-16 10:40:58 -07001832 spin_lock(&dev->dev_reservation_lock);
1833 spin_lock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001834 /*
1835 * Walk the registration list..
1836 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001837 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001838 pr_reg_list) {
1839
1840 tmp[0] = '\0';
1841 isid_buf[0] = '\0';
1842 tpg = pr_reg->pr_reg_nacl->se_tpg;
1843 lun = pr_reg->pr_reg_tg_pt_lun;
1844 /*
1845 * Write out any ISID value to APTPL metadata that was included
1846 * in the original registration.
1847 */
1848 if (pr_reg->isid_present_at_reg)
1849 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1850 pr_reg->pr_reg_isid);
1851 /*
1852 * Include special metadata if the pr_reg matches the
1853 * reservation holder.
1854 */
1855 if (dev->dev_pr_res_holder == pr_reg) {
1856 snprintf(tmp, 512, "PR_REG_START: %d"
1857 "\ninitiator_fabric=%s\n"
1858 "initiator_node=%s\n%s"
1859 "sa_res_key=%llu\n"
1860 "res_holder=1\nres_type=%02x\n"
1861 "res_scope=%02x\nres_all_tg_pt=%d\n"
1862 "mapped_lun=%u\n", reg_count,
Andy Grovere3d6f902011-07-19 08:55:10 +00001863 tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001864 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1865 pr_reg->pr_res_key, pr_reg->pr_res_type,
1866 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1867 pr_reg->pr_res_mapped_lun);
1868 } else {
1869 snprintf(tmp, 512, "PR_REG_START: %d\n"
1870 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1871 "sa_res_key=%llu\nres_holder=0\n"
1872 "res_all_tg_pt=%d\nmapped_lun=%u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001873 reg_count, tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001874 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1875 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1876 pr_reg->pr_res_mapped_lun);
1877 }
1878
Dan Carpenter60d645a2011-06-15 10:03:05 -07001879 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001880 pr_err("Unable to update renaming"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001881 " APTPL metadata\n");
Andy Grover3c8a6222013-05-16 10:40:58 -07001882 ret = -EMSGSIZE;
1883 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001884 }
1885 len += sprintf(buf+len, "%s", tmp);
1886
1887 /*
1888 * Include information about the associated SCSI target port.
1889 */
1890 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1891 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
Andy Grovere3d6f902011-07-19 08:55:10 +00001892 " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
1893 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1894 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001895 lun->lun_sep->sep_rtpi, lun->unpacked_lun, reg_count);
1896
Dan Carpenter60d645a2011-06-15 10:03:05 -07001897 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001898 pr_err("Unable to update renaming"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001899 " APTPL metadata\n");
Andy Grover3c8a6222013-05-16 10:40:58 -07001900 ret = -EMSGSIZE;
1901 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001902 }
1903 len += sprintf(buf+len, "%s", tmp);
1904 reg_count++;
1905 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001906
Andy Grover6708bb22011-06-08 10:36:43 -07001907 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001908 len += sprintf(buf+len, "No Registrations or Reservations");
1909
Andy Grover3c8a6222013-05-16 10:40:58 -07001910out:
1911 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001912 spin_unlock(&dev->dev_reservation_lock);
1913
1914 return ret;
1915}
1916
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001917static int __core_scsi3_write_aptpl_to_file(
1918 struct se_device *dev,
Andy Grover4dee96f2013-05-16 10:41:00 -07001919 unsigned char *buf)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001920{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001921 struct t10_wwn *wwn = &dev->t10_wwn;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001922 struct file *file;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001923 int flags = O_RDWR | O_CREAT | O_TRUNC;
1924 char path[512];
Andy Grover4dee96f2013-05-16 10:41:00 -07001925 u32 pr_aptpl_buf_len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001926 int ret;
1927
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001928 memset(path, 0, 512);
1929
Dan Carpenter60d645a2011-06-15 10:03:05 -07001930 if (strlen(&wwn->unit_serial[0]) >= 512) {
Andy Grover6708bb22011-06-08 10:36:43 -07001931 pr_err("WWN value for struct se_device does not fit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001932 " into path buffer\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001933 return -EMSGSIZE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001934 }
1935
1936 snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]);
1937 file = filp_open(path, flags, 0600);
Al Viro0e9b10a2013-02-23 15:22:43 -05001938 if (IS_ERR(file)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001939 pr_err("filp_open(%s) for APTPL metadata"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001940 " failed\n", path);
Al Viro0e9b10a2013-02-23 15:22:43 -05001941 return PTR_ERR(file);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001942 }
1943
Andy Grover4dee96f2013-05-16 10:41:00 -07001944 pr_aptpl_buf_len = (strlen(buf) + 1); /* Add extra for NULL */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001945
Al Viro0e9b10a2013-02-23 15:22:43 -05001946 ret = kernel_write(file, buf, pr_aptpl_buf_len, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001947
Al Viro0e9b10a2013-02-23 15:22:43 -05001948 if (ret < 0)
Andy Grover6708bb22011-06-08 10:36:43 -07001949 pr_debug("Error writing APTPL metadata file: %s\n", path);
Al Viro0e9b10a2013-02-23 15:22:43 -05001950 fput(file);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001951
Al Viro0e9b10a2013-02-23 15:22:43 -05001952 return ret ? -EIO : 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001953}
1954
Andy Grover459f2132013-05-16 10:41:02 -07001955/*
1956 * Clear the APTPL metadata if APTPL has been disabled, otherwise
1957 * write out the updated metadata to struct file for this SCSI device.
1958 */
Nicholas Bellinger8a391852013-06-24 22:18:40 -07001959static sense_reason_t core_scsi3_update_and_write_aptpl(struct se_device *dev, bool aptpl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001960{
Nicholas Bellinger8a391852013-06-24 22:18:40 -07001961 unsigned char *buf;
1962 int rc;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001963
Andy Grover459f2132013-05-16 10:41:02 -07001964 if (!aptpl) {
1965 char *null_buf = "No Registrations or Reservations\n";
1966
Nicholas Bellinger8a391852013-06-24 22:18:40 -07001967 rc = __core_scsi3_write_aptpl_to_file(dev, null_buf);
Andy Grover459f2132013-05-16 10:41:02 -07001968 dev->t10_pr.pr_aptpl_active = 0;
1969 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated\n");
Andy Grover459f2132013-05-16 10:41:02 -07001970
Nicholas Bellinger8a391852013-06-24 22:18:40 -07001971 if (rc)
1972 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover459f2132013-05-16 10:41:02 -07001973
Nicholas Bellinger8a391852013-06-24 22:18:40 -07001974 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001975 }
1976
Nicholas Bellinger8a391852013-06-24 22:18:40 -07001977 buf = kzalloc(PR_APTPL_BUF_LEN, GFP_KERNEL);
1978 if (!buf)
1979 return TCM_OUT_OF_RESOURCES;
1980
1981 rc = core_scsi3_update_aptpl_buf(dev, buf, PR_APTPL_BUF_LEN);
1982 if (rc < 0) {
1983 kfree(buf);
1984 return TCM_OUT_OF_RESOURCES;
1985 }
1986
1987 rc = __core_scsi3_write_aptpl_to_file(dev, buf);
1988 if (rc != 0) {
1989 pr_err("SPC-3 PR: Could not update APTPL\n");
1990 kfree(buf);
1991 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1992 }
1993 dev->t10_pr.pr_aptpl_active = 1;
1994 kfree(buf);
1995 pr_debug("SPC-3 PR: Set APTPL Bit Activated\n");
1996 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001997}
1998
Christoph Hellwigde103c92012-11-06 12:24:09 -08001999static sense_reason_t
2000core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
Andy Groverbc118fe2013-05-16 10:41:04 -07002001 bool aptpl, bool all_tg_pt, bool spec_i_pt, enum register_type register_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002002{
Andy Grovere3d6f902011-07-19 08:55:10 +00002003 struct se_session *se_sess = cmd->se_sess;
Andy Grover5951146d2011-07-19 10:26:37 +00002004 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002005 struct se_dev_entry *se_deve;
Andy Grovere3d6f902011-07-19 08:55:10 +00002006 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002007 struct se_portal_group *se_tpg;
Andy Groverbc118fe2013-05-16 10:41:04 -07002008 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002009 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002010 unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
Hannes Reineckea0d50f62012-12-17 09:53:34 +01002011 sense_reason_t ret = TCM_NO_SENSE;
Andy Groverbc118fe2013-05-16 10:41:04 -07002012 int pr_holder = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002013
Andy Grover6708bb22011-06-08 10:36:43 -07002014 if (!se_sess || !se_lun) {
2015 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002016 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002017 }
2018 se_tpg = se_sess->se_tpg;
Jörn Engelf2083242012-03-15 15:05:40 -04002019 se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002020
Andy Grovere3d6f902011-07-19 08:55:10 +00002021 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002022 memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +00002023 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002024 PR_REG_ISID_LEN);
2025 isid_ptr = &isid_buf[0];
2026 }
2027 /*
2028 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2029 */
Andy Groverbc118fe2013-05-16 10:41:04 -07002030 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2031 if (!pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002032 if (res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002033 pr_warn("SPC-3 PR: Reservation Key non-zero"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002034 " for SA REGISTER, returning CONFLICT\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002035 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002036 }
2037 /*
2038 * Do nothing but return GOOD status.
2039 */
Andy Grover6708bb22011-06-08 10:36:43 -07002040 if (!sa_res_key)
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002041 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002042
Andy Grover6708bb22011-06-08 10:36:43 -07002043 if (!spec_i_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002044 /*
2045 * Perform the Service Action REGISTER on the Initiator
2046 * Port Endpoint that the PRO was received from on the
2047 * Logical Unit of the SCSI device server.
2048 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08002049 if (core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002050 se_sess->se_node_acl, se_deve, isid_ptr,
2051 sa_res_key, all_tg_pt, aptpl,
Andy Grover33ce6a82013-05-16 10:40:54 -07002052 register_type, 0)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002053 pr_err("Unable to allocate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002054 " struct t10_pr_registration\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002055 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002056 }
2057 } else {
2058 /*
2059 * Register both the Initiator port that received
2060 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2061 * TransportID from Parameter list and loop through
2062 * fabric dependent parameter list while calling
2063 * logic from of core_scsi3_alloc_registration() for
2064 * each TransportID provided SCSI Initiator Port/Device
2065 */
2066 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2067 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2068 if (ret != 0)
2069 return ret;
2070 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002071
Andy Grover459f2132013-05-16 10:41:02 -07002072 return core_scsi3_update_and_write_aptpl(dev, aptpl);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002073 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002074
Andy Groverbc118fe2013-05-16 10:41:04 -07002075 /* ok, existing registration */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002076
Andy Groverbc118fe2013-05-16 10:41:04 -07002077 if ((register_type == REGISTER) && (res_key != pr_reg->pr_res_key)) {
2078 pr_err("SPC-3 PR REGISTER: Received"
2079 " res_key: 0x%016Lx does not match"
2080 " existing SA REGISTER res_key:"
2081 " 0x%016Lx\n", res_key,
2082 pr_reg->pr_res_key);
2083 ret = TCM_RESERVATION_CONFLICT;
2084 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002085 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08002086
2087 if (spec_i_pt) {
Andy Grover1f070cc2013-05-16 10:40:56 -07002088 pr_err("SPC-3 PR REGISTER: SPEC_I_PT"
2089 " set on a registered nexus\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002090 ret = TCM_INVALID_PARAMETER_LIST;
Andy Groverbc118fe2013-05-16 10:41:04 -07002091 goto out;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002092 }
2093
2094 /*
2095 * An existing ALL_TG_PT=1 registration being released
2096 * must also set ALL_TG_PT=1 in the incoming PROUT.
2097 */
Andy Grover1f070cc2013-05-16 10:40:56 -07002098 if (pr_reg->pr_reg_all_tg_pt && !all_tg_pt) {
2099 pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1"
Christoph Hellwigde103c92012-11-06 12:24:09 -08002100 " registration exists, but ALL_TG_PT=1 bit not"
2101 " present in received PROUT\n");
2102 ret = TCM_INVALID_CDB_FIELD;
Andy Groverbc118fe2013-05-16 10:41:04 -07002103 goto out;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002104 }
2105
2106 /*
Andy Grover51d9c412013-05-16 10:41:03 -07002107 * sa_res_key=1 Change Reservation Key for registered I_T Nexus.
Christoph Hellwigde103c92012-11-06 12:24:09 -08002108 */
Andy Grover51d9c412013-05-16 10:41:03 -07002109 if (sa_res_key) {
2110 /*
2111 * Increment PRgeneration counter for struct se_device"
2112 * upon a successful REGISTER, see spc4r17 section 6.3.2
2113 * READ_KEYS service action.
2114 */
2115 pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev);
2116 pr_reg->pr_res_key = sa_res_key;
2117 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2118 " Key for %s to: 0x%016Lx PRgeneration:"
2119 " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
2120 (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? "_AND_IGNORE_EXISTING_KEY" : "",
2121 pr_reg->pr_reg_nacl->initiatorname,
2122 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2123
2124 } else {
2125 /*
2126 * sa_res_key=0 Unregister Reservation Key for registered I_T Nexus.
2127 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08002128 pr_holder = core_scsi3_check_implict_release(
2129 cmd->se_dev, pr_reg);
2130 if (pr_holder < 0) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08002131 ret = TCM_RESERVATION_CONFLICT;
Andy Groverbc118fe2013-05-16 10:41:04 -07002132 goto out;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002133 }
2134
2135 spin_lock(&pr_tmpl->registration_lock);
2136 /*
2137 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2138 * and matching pr_res_key.
2139 */
2140 if (pr_reg->pr_reg_all_tg_pt) {
2141 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2142 &pr_tmpl->registration_list,
2143 pr_reg_list) {
2144
2145 if (!pr_reg_p->pr_reg_all_tg_pt)
2146 continue;
2147 if (pr_reg_p->pr_res_key != res_key)
2148 continue;
2149 if (pr_reg == pr_reg_p)
2150 continue;
2151 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2152 pr_reg_p->pr_reg_nacl->initiatorname))
2153 continue;
2154
2155 __core_scsi3_free_registration(dev,
2156 pr_reg_p, NULL, 0);
2157 }
2158 }
2159
2160 /*
2161 * Release the calling I_T Nexus registration now..
2162 */
2163 __core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1);
2164
2165 /*
2166 * From spc4r17, section 5.7.11.3 Unregistering
2167 *
2168 * If the persistent reservation is a registrants only
2169 * type, the device server shall establish a unit
2170 * attention condition for the initiator port associated
2171 * with every registered I_T nexus except for the I_T
2172 * nexus on which the PERSISTENT RESERVE OUT command was
2173 * received, with the additional sense code set to
2174 * RESERVATIONS RELEASED.
2175 */
2176 if (pr_holder &&
Andy Groverbc118fe2013-05-16 10:41:04 -07002177 (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY ||
2178 pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08002179 list_for_each_entry(pr_reg_p,
2180 &pr_tmpl->registration_list,
2181 pr_reg_list) {
2182
2183 core_scsi3_ua_allocate(
2184 pr_reg_p->pr_reg_nacl,
2185 pr_reg_p->pr_res_mapped_lun,
2186 0x2A,
2187 ASCQ_2AH_RESERVATIONS_RELEASED);
2188 }
2189 }
Andy Grover51d9c412013-05-16 10:41:03 -07002190
Christoph Hellwigde103c92012-11-06 12:24:09 -08002191 spin_unlock(&pr_tmpl->registration_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002192 }
2193
Andy Grover459f2132013-05-16 10:41:02 -07002194 ret = core_scsi3_update_and_write_aptpl(dev, aptpl);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002195
Andy Groverbc118fe2013-05-16 10:41:04 -07002196out:
Christoph Hellwigde103c92012-11-06 12:24:09 -08002197 core_scsi3_put_pr_reg(pr_reg);
2198 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002199}
2200
2201unsigned char *core_scsi3_pr_dump_type(int type)
2202{
2203 switch (type) {
2204 case PR_TYPE_WRITE_EXCLUSIVE:
2205 return "Write Exclusive Access";
2206 case PR_TYPE_EXCLUSIVE_ACCESS:
2207 return "Exclusive Access";
2208 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2209 return "Write Exclusive Access, Registrants Only";
2210 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2211 return "Exclusive Access, Registrants Only";
2212 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2213 return "Write Exclusive Access, All Registrants";
2214 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2215 return "Exclusive Access, All Registrants";
2216 default:
2217 break;
2218 }
2219
2220 return "Unknown SPC-3 PR Type";
2221}
2222
Christoph Hellwigde103c92012-11-06 12:24:09 -08002223static sense_reason_t
2224core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002225{
Christoph Hellwigde103c92012-11-06 12:24:09 -08002226 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00002227 struct se_session *se_sess = cmd->se_sess;
Andy Grovere3d6f902011-07-19 08:55:10 +00002228 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002229 struct t10_pr_registration *pr_reg, *pr_res_holder;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002230 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002231 char i_buf[PR_REG_ISID_ID_LEN];
Christoph Hellwigde103c92012-11-06 12:24:09 -08002232 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002233
2234 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2235
Andy Grover6708bb22011-06-08 10:36:43 -07002236 if (!se_sess || !se_lun) {
2237 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002238 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002239 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002240 /*
2241 * Locate the existing *pr_reg via struct se_node_acl pointers
2242 */
Andy Grover5951146d2011-07-19 10:26:37 +00002243 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002244 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002245 if (!pr_reg) {
2246 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002247 " PR_REGISTERED *pr_reg for RESERVE\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002248 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002249 }
2250 /*
2251 * From spc4r17 Section 5.7.9: Reserving:
2252 *
2253 * An application client creates a persistent reservation by issuing
2254 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2255 * a registered I_T nexus with the following parameters:
2256 * a) RESERVATION KEY set to the value of the reservation key that is
2257 * registered with the logical unit for the I_T nexus; and
2258 */
2259 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002260 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002261 " does not match existing SA REGISTER res_key:"
2262 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002263 ret = TCM_RESERVATION_CONFLICT;
2264 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002265 }
2266 /*
2267 * From spc4r17 Section 5.7.9: Reserving:
2268 *
2269 * From above:
2270 * b) TYPE field and SCOPE field set to the persistent reservation
2271 * being created.
2272 *
2273 * Only one persistent reservation is allowed at a time per logical unit
2274 * and that persistent reservation has a scope of LU_SCOPE.
2275 */
2276 if (scope != PR_SCOPE_LU_SCOPE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002277 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002278 ret = TCM_INVALID_PARAMETER_LIST;
2279 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002280 }
2281 /*
2282 * See if we have an existing PR reservation holder pointer at
2283 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2284 * *pr_res_holder.
2285 */
2286 spin_lock(&dev->dev_reservation_lock);
2287 pr_res_holder = dev->dev_pr_res_holder;
Andy Groveree1b1b92012-07-12 17:34:54 -07002288 if (pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002289 /*
2290 * From spc4r17 Section 5.7.9: Reserving:
2291 *
2292 * If the device server receives a PERSISTENT RESERVE OUT
2293 * command from an I_T nexus other than a persistent reservation
2294 * holder (see 5.7.10) that attempts to create a persistent
2295 * reservation when a persistent reservation already exists for
2296 * the logical unit, then the command shall be completed with
2297 * RESERVATION CONFLICT status.
2298 */
2299 if (pr_res_holder != pr_reg) {
2300 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002301 pr_err("SPC-3 PR: Attempted RESERVE from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002302 " [%s]: %s while reservation already held by"
2303 " [%s]: %s, returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002304 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002305 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002306 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002307 pr_res_holder->pr_reg_nacl->initiatorname);
2308
2309 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002310 ret = TCM_RESERVATION_CONFLICT;
2311 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002312 }
2313 /*
2314 * From spc4r17 Section 5.7.9: Reserving:
2315 *
2316 * If a persistent reservation holder attempts to modify the
2317 * type or scope of an existing persistent reservation, the
2318 * command shall be completed with RESERVATION CONFLICT status.
2319 */
2320 if ((pr_res_holder->pr_res_type != type) ||
2321 (pr_res_holder->pr_res_scope != scope)) {
2322 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002323 pr_err("SPC-3 PR: Attempted RESERVE from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002324 " [%s]: %s trying to change TYPE and/or SCOPE,"
2325 " while reservation already held by [%s]: %s,"
2326 " returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002327 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002328 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002329 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002330 pr_res_holder->pr_reg_nacl->initiatorname);
2331
2332 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002333 ret = TCM_RESERVATION_CONFLICT;
2334 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002335 }
2336 /*
2337 * From spc4r17 Section 5.7.9: Reserving:
2338 *
2339 * If the device server receives a PERSISTENT RESERVE OUT
2340 * command with RESERVE service action where the TYPE field and
2341 * the SCOPE field contain the same values as the existing type
2342 * and scope from a persistent reservation holder, it shall not
2343 * make any change to the existing persistent reservation and
2344 * shall completethe command with GOOD status.
2345 */
2346 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002347 ret = 0;
2348 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002349 }
2350 /*
2351 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2352 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2353 */
2354 pr_reg->pr_res_scope = scope;
2355 pr_reg->pr_res_type = type;
2356 pr_reg->pr_res_holder = 1;
2357 dev->dev_pr_res_holder = pr_reg;
Andy Groverd2843c12013-05-16 10:40:55 -07002358 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002359
Andy Grover6708bb22011-06-08 10:36:43 -07002360 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002361 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002362 cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002363 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002364 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002365 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002366 se_sess->se_node_acl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07002367 i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002368 spin_unlock(&dev->dev_reservation_lock);
2369
Andy Grover459f2132013-05-16 10:41:02 -07002370 if (pr_tmpl->pr_aptpl_active)
2371 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002372
Christoph Hellwigde103c92012-11-06 12:24:09 -08002373 ret = 0;
2374out_put_pr_reg:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002375 core_scsi3_put_pr_reg(pr_reg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002376 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002377}
2378
Christoph Hellwigde103c92012-11-06 12:24:09 -08002379static sense_reason_t
2380core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope,
2381 u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002382{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002383 switch (type) {
2384 case PR_TYPE_WRITE_EXCLUSIVE:
2385 case PR_TYPE_EXCLUSIVE_ACCESS:
2386 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2387 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2388 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2389 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
Christoph Hellwigde103c92012-11-06 12:24:09 -08002390 return core_scsi3_pro_reserve(cmd, type, scope, res_key);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002391 default:
Andy Grover6708bb22011-06-08 10:36:43 -07002392 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002393 " 0x%02x\n", type);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002394 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002395 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002396}
2397
2398/*
2399 * Called with struct se_device->dev_reservation_lock held.
2400 */
2401static void __core_scsi3_complete_pro_release(
2402 struct se_device *dev,
2403 struct se_node_acl *se_nacl,
2404 struct t10_pr_registration *pr_reg,
2405 int explict)
2406{
2407 struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2408 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002409
2410 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07002411 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002412 /*
2413 * Go ahead and release the current PR reservation holder.
2414 */
2415 dev->dev_pr_res_holder = NULL;
2416
Andy Grover6708bb22011-06-08 10:36:43 -07002417 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002418 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2419 tfo->get_fabric_name(), (explict) ? "explict" : "implict",
2420 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
2421 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002422 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002423 tfo->get_fabric_name(), se_nacl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07002424 i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002425 /*
2426 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2427 */
2428 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2429}
2430
Christoph Hellwigde103c92012-11-06 12:24:09 -08002431static sense_reason_t
2432core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope,
2433 u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002434{
2435 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00002436 struct se_session *se_sess = cmd->se_sess;
2437 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002438 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002439 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002440 int all_reg = 0;
2441 sense_reason_t ret = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002442
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");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002445 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002446 }
2447 /*
2448 * Locate the existing *pr_reg via struct se_node_acl pointers
2449 */
2450 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002451 if (!pr_reg) {
2452 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002453 " PR_REGISTERED *pr_reg for RELEASE\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002454 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002455 }
2456 /*
2457 * From spc4r17 Section 5.7.11.2 Releasing:
2458 *
2459 * If there is no persistent reservation or in response to a persistent
2460 * reservation release request from a registered I_T nexus that is not a
2461 * persistent reservation holder (see 5.7.10), the device server shall
2462 * do the following:
2463 *
2464 * a) Not release the persistent reservation, if any;
2465 * b) Not remove any registrations; and
2466 * c) Complete the command with GOOD status.
2467 */
2468 spin_lock(&dev->dev_reservation_lock);
2469 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07002470 if (!pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002471 /*
2472 * No persistent reservation, return GOOD status.
2473 */
2474 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerbb7a8c82012-11-06 15:46:25 -08002475 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002476 }
2477 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2478 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
2479 all_reg = 1;
2480
2481 if ((all_reg == 0) && (pr_res_holder != pr_reg)) {
2482 /*
2483 * Non 'All Registrants' PR Type cases..
2484 * Release request from a registered I_T nexus that is not a
2485 * persistent reservation holder. return GOOD status.
2486 */
2487 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002488 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002489 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08002490
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002491 /*
2492 * From spc4r17 Section 5.7.11.2 Releasing:
2493 *
2494 * Only the persistent reservation holder (see 5.7.10) is allowed to
2495 * release a persistent reservation.
2496 *
2497 * An application client releases the persistent reservation by issuing
2498 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2499 * an I_T nexus that is a persistent reservation holder with the
2500 * following parameters:
2501 *
2502 * a) RESERVATION KEY field set to the value of the reservation key
2503 * that is registered with the logical unit for the I_T nexus;
2504 */
2505 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002506 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002507 " does not match existing SA REGISTER res_key:"
2508 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2509 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002510 ret = TCM_RESERVATION_CONFLICT;
2511 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002512 }
2513 /*
2514 * From spc4r17 Section 5.7.11.2 Releasing and above:
2515 *
2516 * b) TYPE field and SCOPE field set to match the persistent
2517 * reservation being released.
2518 */
2519 if ((pr_res_holder->pr_res_type != type) ||
2520 (pr_res_holder->pr_res_scope != scope)) {
2521 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002522 pr_err("SPC-3 PR RELEASE: Attempted to release"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002523 " reservation from [%s]: %s with different TYPE "
2524 "and/or SCOPE while reservation already held by"
2525 " [%s]: %s, returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002526 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002527 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002528 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002529 pr_res_holder->pr_reg_nacl->initiatorname);
2530
2531 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002532 ret = TCM_RESERVATION_CONFLICT;
2533 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002534 }
2535 /*
2536 * In response to a persistent reservation release request from the
2537 * persistent reservation holder the device server shall perform a
2538 * release by doing the following as an uninterrupted series of actions:
2539 * a) Release the persistent reservation;
2540 * b) Not remove any registration(s);
2541 * c) If the released persistent reservation is a registrants only type
2542 * or all registrants type persistent reservation,
2543 * the device server shall establish a unit attention condition for
2544 * the initiator port associated with every regis-
2545 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2546 * RESERVE OUT command with RELEASE service action was received,
2547 * with the additional sense code set to RESERVATIONS RELEASED; and
2548 * d) If the persistent reservation is of any other type, the device
2549 * server shall not establish a unit attention condition.
2550 */
2551 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
2552 pr_reg, 1);
2553
2554 spin_unlock(&dev->dev_reservation_lock);
2555
2556 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2557 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2558 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2559 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2560 /*
2561 * If no UNIT ATTENTION conditions will be established for
2562 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2563 * go ahead and check for APTPL=1 update+write below
2564 */
2565 goto write_aptpl;
2566 }
2567
2568 spin_lock(&pr_tmpl->registration_lock);
2569 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2570 pr_reg_list) {
2571 /*
2572 * Do not establish a UNIT ATTENTION condition
2573 * for the calling I_T Nexus
2574 */
2575 if (pr_reg_p == pr_reg)
2576 continue;
2577
2578 core_scsi3_ua_allocate(pr_reg_p->pr_reg_nacl,
2579 pr_reg_p->pr_res_mapped_lun,
2580 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2581 }
2582 spin_unlock(&pr_tmpl->registration_lock);
2583
2584write_aptpl:
Andy Grover459f2132013-05-16 10:41:02 -07002585 if (pr_tmpl->pr_aptpl_active)
2586 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2587
Christoph Hellwigde103c92012-11-06 12:24:09 -08002588out_put_pr_reg:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002589 core_scsi3_put_pr_reg(pr_reg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002590 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002591}
2592
Christoph Hellwigde103c92012-11-06 12:24:09 -08002593static sense_reason_t
2594core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002595{
2596 struct se_device *dev = cmd->se_dev;
2597 struct se_node_acl *pr_reg_nacl;
Andy Grovere3d6f902011-07-19 08:55:10 +00002598 struct se_session *se_sess = cmd->se_sess;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002599 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002600 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2601 u32 pr_res_mapped_lun = 0;
2602 int calling_it_nexus = 0;
2603 /*
2604 * Locate the existing *pr_reg via struct se_node_acl pointers
2605 */
Andy Grover5951146d2011-07-19 10:26:37 +00002606 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002607 se_sess->se_node_acl, se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002608 if (!pr_reg_n) {
2609 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002610 " PR_REGISTERED *pr_reg for CLEAR\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002611 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002612 }
2613 /*
2614 * From spc4r17 section 5.7.11.6, Clearing:
2615 *
2616 * Any application client may release the persistent reservation and
2617 * remove all registrations from a device server by issuing a
2618 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2619 * registered I_T nexus with the following parameter:
2620 *
2621 * a) RESERVATION KEY field set to the value of the reservation key
2622 * that is registered with the logical unit for the I_T nexus.
2623 */
2624 if (res_key != pr_reg_n->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002625 pr_err("SPC-3 PR REGISTER: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002626 " res_key: 0x%016Lx does not match"
2627 " existing SA REGISTER res_key:"
2628 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2629 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002630 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002631 }
2632 /*
2633 * a) Release the persistent reservation, if any;
2634 */
2635 spin_lock(&dev->dev_reservation_lock);
2636 pr_res_holder = dev->dev_pr_res_holder;
2637 if (pr_res_holder) {
2638 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2639 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
2640 pr_res_holder, 0);
2641 }
2642 spin_unlock(&dev->dev_reservation_lock);
2643 /*
2644 * b) Remove all registration(s) (see spc4r17 5.7.7);
2645 */
2646 spin_lock(&pr_tmpl->registration_lock);
2647 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2648 &pr_tmpl->registration_list, pr_reg_list) {
2649
2650 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2651 pr_reg_nacl = pr_reg->pr_reg_nacl;
2652 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2653 __core_scsi3_free_registration(dev, pr_reg, NULL,
2654 calling_it_nexus);
2655 /*
2656 * e) Establish a unit attention condition for the initiator
2657 * port associated with every registered I_T nexus other
2658 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2659 * command with CLEAR service action was received, with the
2660 * additional sense code set to RESERVATIONS PREEMPTED.
2661 */
Andy Grover6708bb22011-06-08 10:36:43 -07002662 if (!calling_it_nexus)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002663 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun,
2664 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2665 }
2666 spin_unlock(&pr_tmpl->registration_lock);
2667
Andy Grover6708bb22011-06-08 10:36:43 -07002668 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002669 cmd->se_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002670
Andy Grover459f2132013-05-16 10:41:02 -07002671 core_scsi3_update_and_write_aptpl(cmd->se_dev, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002672
2673 core_scsi3_pr_generation(dev);
2674 return 0;
2675}
2676
2677/*
2678 * Called with struct se_device->dev_reservation_lock held.
2679 */
2680static void __core_scsi3_complete_pro_preempt(
2681 struct se_device *dev,
2682 struct t10_pr_registration *pr_reg,
2683 struct list_head *preempt_and_abort_list,
2684 int type,
2685 int scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07002686 enum preempt_type preempt_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002687{
2688 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
2689 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2690 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002691
2692 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07002693 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002694 /*
2695 * Do an implict RELEASE of the existing reservation.
2696 */
2697 if (dev->dev_pr_res_holder)
2698 __core_scsi3_complete_pro_release(dev, nacl,
2699 dev->dev_pr_res_holder, 0);
2700
2701 dev->dev_pr_res_holder = pr_reg;
2702 pr_reg->pr_res_holder = 1;
2703 pr_reg->pr_res_type = type;
2704 pr_reg->pr_res_scope = scope;
2705
Andy Grover6708bb22011-06-08 10:36:43 -07002706 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002707 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
Andy Grover33ce6a82013-05-16 10:40:54 -07002708 tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002709 core_scsi3_pr_dump_type(type),
2710 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002711 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
Andy Grover33ce6a82013-05-16 10:40:54 -07002712 tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
Andy Groverd2843c12013-05-16 10:40:55 -07002713 nacl->initiatorname, i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002714 /*
2715 * For PREEMPT_AND_ABORT, add the preempting reservation's
2716 * struct t10_pr_registration to the list that will be compared
2717 * against received CDBs..
2718 */
2719 if (preempt_and_abort_list)
2720 list_add_tail(&pr_reg->pr_reg_abort_list,
2721 preempt_and_abort_list);
2722}
2723
2724static void core_scsi3_release_preempt_and_abort(
2725 struct list_head *preempt_and_abort_list,
2726 struct t10_pr_registration *pr_reg_holder)
2727{
2728 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2729
2730 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2731 pr_reg_abort_list) {
2732
2733 list_del(&pr_reg->pr_reg_abort_list);
2734 if (pr_reg_holder == pr_reg)
2735 continue;
2736 if (pr_reg->pr_res_holder) {
Andy Grover6708bb22011-06-08 10:36:43 -07002737 pr_warn("pr_reg->pr_res_holder still set\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002738 continue;
2739 }
2740
2741 pr_reg->pr_reg_deve = NULL;
2742 pr_reg->pr_reg_nacl = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002743 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2744 }
2745}
2746
Christoph Hellwigde103c92012-11-06 12:24:09 -08002747static sense_reason_t
2748core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key,
Andy Grover33ce6a82013-05-16 10:40:54 -07002749 u64 sa_res_key, enum preempt_type preempt_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002750{
Andy Grover5951146d2011-07-19 10:26:37 +00002751 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002752 struct se_node_acl *pr_reg_nacl;
Andy Grovere3d6f902011-07-19 08:55:10 +00002753 struct se_session *se_sess = cmd->se_sess;
Roland Dreierd0f474e2012-01-12 10:41:18 -08002754 LIST_HEAD(preempt_and_abort_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002755 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002756 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002757 u32 pr_res_mapped_lun = 0;
2758 int all_reg = 0, calling_it_nexus = 0, released_regs = 0;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002759 int prh_type = 0, prh_scope = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002760
Christoph Hellwigde103c92012-11-06 12:24:09 -08002761 if (!se_sess)
2762 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002763
Andy Grover5951146d2011-07-19 10:26:37 +00002764 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002765 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002766 if (!pr_reg_n) {
2767 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002768 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
Andy Grover33ce6a82013-05-16 10:40:54 -07002769 (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002770 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002771 }
2772 if (pr_reg_n->pr_res_key != res_key) {
2773 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002774 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002775 }
2776 if (scope != PR_SCOPE_LU_SCOPE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002777 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002778 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002779 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002780 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002781
2782 spin_lock(&dev->dev_reservation_lock);
2783 pr_res_holder = dev->dev_pr_res_holder;
2784 if (pr_res_holder &&
2785 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2786 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
2787 all_reg = 1;
2788
Andy Grover6708bb22011-06-08 10:36:43 -07002789 if (!all_reg && !sa_res_key) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002790 spin_unlock(&dev->dev_reservation_lock);
2791 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002792 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002793 }
2794 /*
2795 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
2796 *
2797 * If the SERVICE ACTION RESERVATION KEY field does not identify a
2798 * persistent reservation holder or there is no persistent reservation
2799 * holder (i.e., there is no persistent reservation), then the device
2800 * server shall perform a preempt by doing the following in an
2801 * uninterrupted series of actions. (See below..)
2802 */
Andy Grover6708bb22011-06-08 10:36:43 -07002803 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002804 /*
2805 * No existing or SA Reservation Key matching reservations..
2806 *
2807 * PROUT SA PREEMPT with All Registrant type reservations are
2808 * allowed to be processed without a matching SA Reservation Key
2809 */
2810 spin_lock(&pr_tmpl->registration_lock);
2811 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2812 &pr_tmpl->registration_list, pr_reg_list) {
2813 /*
2814 * Removing of registrations in non all registrants
2815 * type reservations without a matching SA reservation
2816 * key.
2817 *
2818 * a) Remove the registrations for all I_T nexuses
2819 * specified by the SERVICE ACTION RESERVATION KEY
2820 * field;
2821 * b) Ignore the contents of the SCOPE and TYPE fields;
2822 * c) Process tasks as defined in 5.7.1; and
2823 * d) Establish a unit attention condition for the
2824 * initiator port associated with every I_T nexus
2825 * that lost its registration other than the I_T
2826 * nexus on which the PERSISTENT RESERVE OUT command
2827 * was received, with the additional sense code set
2828 * to REGISTRATIONS PREEMPTED.
2829 */
Andy Grover6708bb22011-06-08 10:36:43 -07002830 if (!all_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002831 if (pr_reg->pr_res_key != sa_res_key)
2832 continue;
2833
2834 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2835 pr_reg_nacl = pr_reg->pr_reg_nacl;
2836 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2837 __core_scsi3_free_registration(dev, pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07002838 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002839 NULL, calling_it_nexus);
2840 released_regs++;
2841 } else {
2842 /*
2843 * Case for any existing all registrants type
2844 * reservation, follow logic in spc4r17 section
2845 * 5.7.11.4 Preempting, Table 52 and Figure 7.
2846 *
2847 * For a ZERO SA Reservation key, release
2848 * all other registrations and do an implict
2849 * release of active persistent reservation.
2850 *
2851 * For a non-ZERO SA Reservation key, only
2852 * release the matching reservation key from
2853 * registrations.
2854 */
2855 if ((sa_res_key) &&
2856 (pr_reg->pr_res_key != sa_res_key))
2857 continue;
2858
2859 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2860 if (calling_it_nexus)
2861 continue;
2862
2863 pr_reg_nacl = pr_reg->pr_reg_nacl;
2864 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2865 __core_scsi3_free_registration(dev, pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07002866 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002867 NULL, 0);
2868 released_regs++;
2869 }
Andy Grover6708bb22011-06-08 10:36:43 -07002870 if (!calling_it_nexus)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002871 core_scsi3_ua_allocate(pr_reg_nacl,
2872 pr_res_mapped_lun, 0x2A,
Marco Sanvido9e08e342012-01-03 17:12:57 -08002873 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002874 }
2875 spin_unlock(&pr_tmpl->registration_lock);
2876 /*
2877 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
2878 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
2879 * RESERVATION KEY field to a value that does not match any
2880 * registered reservation key, then the device server shall
2881 * complete the command with RESERVATION CONFLICT status.
2882 */
Andy Grover6708bb22011-06-08 10:36:43 -07002883 if (!released_regs) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002884 spin_unlock(&dev->dev_reservation_lock);
2885 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002886 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002887 }
2888 /*
2889 * For an existing all registrants type reservation
2890 * with a zero SA rservation key, preempt the existing
2891 * reservation with the new PR type and scope.
2892 */
2893 if (pr_res_holder && all_reg && !(sa_res_key)) {
2894 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
Andy Grover33ce6a82013-05-16 10:40:54 -07002895 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
2896 type, scope, preempt_type);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002897
Andy Grover33ce6a82013-05-16 10:40:54 -07002898 if (preempt_type == PREEMPT_AND_ABORT)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002899 core_scsi3_release_preempt_and_abort(
2900 &preempt_and_abort_list, pr_reg_n);
2901 }
2902 spin_unlock(&dev->dev_reservation_lock);
2903
Andy Grover459f2132013-05-16 10:41:02 -07002904 if (pr_tmpl->pr_aptpl_active)
2905 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002906
2907 core_scsi3_put_pr_reg(pr_reg_n);
Andy Grover5951146d2011-07-19 10:26:37 +00002908 core_scsi3_pr_generation(cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002909 return 0;
2910 }
2911 /*
2912 * The PREEMPTing SA reservation key matches that of the
2913 * existing persistent reservation, first, we check if
2914 * we are preempting our own reservation.
2915 * From spc4r17, section 5.7.11.4.3 Preempting
2916 * persistent reservations and registration handling
2917 *
2918 * If an all registrants persistent reservation is not
2919 * present, it is not an error for the persistent
2920 * reservation holder to preempt itself (i.e., a
2921 * PERSISTENT RESERVE OUT with a PREEMPT service action
2922 * or a PREEMPT AND ABORT service action with the
2923 * SERVICE ACTION RESERVATION KEY value equal to the
2924 * persistent reservation holder's reservation key that
2925 * is received from the persistent reservation holder).
2926 * In that case, the device server shall establish the
2927 * new persistent reservation and maintain the
2928 * registration.
2929 */
2930 prh_type = pr_res_holder->pr_res_type;
2931 prh_scope = pr_res_holder->pr_res_scope;
2932 /*
2933 * If the SERVICE ACTION RESERVATION KEY field identifies a
2934 * persistent reservation holder (see 5.7.10), the device
2935 * server shall perform a preempt by doing the following as
2936 * an uninterrupted series of actions:
2937 *
2938 * a) Release the persistent reservation for the holder
2939 * identified by the SERVICE ACTION RESERVATION KEY field;
2940 */
2941 if (pr_reg_n != pr_res_holder)
2942 __core_scsi3_complete_pro_release(dev,
2943 pr_res_holder->pr_reg_nacl,
2944 dev->dev_pr_res_holder, 0);
2945 /*
2946 * b) Remove the registrations for all I_T nexuses identified
2947 * by the SERVICE ACTION RESERVATION KEY field, except the
2948 * I_T nexus that is being used for the PERSISTENT RESERVE
2949 * OUT command. If an all registrants persistent reservation
2950 * is present and the SERVICE ACTION RESERVATION KEY field
2951 * is set to zero, then all registrations shall be removed
2952 * except for that of the I_T nexus that is being used for
2953 * the PERSISTENT RESERVE OUT command;
2954 */
2955 spin_lock(&pr_tmpl->registration_lock);
2956 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2957 &pr_tmpl->registration_list, pr_reg_list) {
2958
2959 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2960 if (calling_it_nexus)
2961 continue;
2962
2963 if (pr_reg->pr_res_key != sa_res_key)
2964 continue;
2965
2966 pr_reg_nacl = pr_reg->pr_reg_nacl;
2967 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2968 __core_scsi3_free_registration(dev, pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07002969 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002970 calling_it_nexus);
2971 /*
2972 * e) Establish a unit attention condition for the initiator
2973 * port associated with every I_T nexus that lost its
2974 * persistent reservation and/or registration, with the
2975 * additional sense code set to REGISTRATIONS PREEMPTED;
2976 */
2977 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
Marco Sanvido9e08e342012-01-03 17:12:57 -08002978 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002979 }
2980 spin_unlock(&pr_tmpl->registration_lock);
2981 /*
2982 * c) Establish a persistent reservation for the preempting
2983 * I_T nexus using the contents of the SCOPE and TYPE fields;
2984 */
2985 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
Andy Grover33ce6a82013-05-16 10:40:54 -07002986 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
2987 type, scope, preempt_type);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002988 /*
2989 * d) Process tasks as defined in 5.7.1;
2990 * e) See above..
2991 * f) If the type or scope has changed, then for every I_T nexus
2992 * whose reservation key was not removed, except for the I_T
2993 * nexus on which the PERSISTENT RESERVE OUT command was
2994 * received, the device server shall establish a unit
2995 * attention condition for the initiator port associated with
2996 * that I_T nexus, with the additional sense code set to
2997 * RESERVATIONS RELEASED. If the type or scope have not
2998 * changed, then no unit attention condition(s) shall be
2999 * established for this reason.
3000 */
3001 if ((prh_type != type) || (prh_scope != scope)) {
3002 spin_lock(&pr_tmpl->registration_lock);
3003 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3004 &pr_tmpl->registration_list, pr_reg_list) {
3005
3006 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3007 if (calling_it_nexus)
3008 continue;
3009
3010 core_scsi3_ua_allocate(pr_reg->pr_reg_nacl,
3011 pr_reg->pr_res_mapped_lun, 0x2A,
3012 ASCQ_2AH_RESERVATIONS_RELEASED);
3013 }
3014 spin_unlock(&pr_tmpl->registration_lock);
3015 }
3016 spin_unlock(&dev->dev_reservation_lock);
3017 /*
3018 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3019 * All received CDBs for the matching existing reservation and
3020 * registrations undergo ABORT_TASK logic.
3021 *
3022 * From there, core_scsi3_release_preempt_and_abort() will
3023 * release every registration in the list (which have already
3024 * been removed from the primary pr_reg list), except the
3025 * new persistent reservation holder, the calling Initiator Port.
3026 */
Andy Grover33ce6a82013-05-16 10:40:54 -07003027 if (preempt_type == PREEMPT_AND_ABORT) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003028 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3029 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3030 pr_reg_n);
3031 }
3032
Andy Grover459f2132013-05-16 10:41:02 -07003033 if (pr_tmpl->pr_aptpl_active)
3034 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003035
3036 core_scsi3_put_pr_reg(pr_reg_n);
Andy Grover5951146d2011-07-19 10:26:37 +00003037 core_scsi3_pr_generation(cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003038 return 0;
3039}
3040
Christoph Hellwigde103c92012-11-06 12:24:09 -08003041static sense_reason_t
3042core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07003043 u64 res_key, u64 sa_res_key, enum preempt_type preempt_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003044{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003045 switch (type) {
3046 case PR_TYPE_WRITE_EXCLUSIVE:
3047 case PR_TYPE_EXCLUSIVE_ACCESS:
3048 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3049 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3050 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3051 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
Christoph Hellwigde103c92012-11-06 12:24:09 -08003052 return core_scsi3_pro_preempt(cmd, type, scope, res_key,
Andy Grover33ce6a82013-05-16 10:40:54 -07003053 sa_res_key, preempt_type);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003054 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003055 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
Andy Grover33ce6a82013-05-16 10:40:54 -07003056 " Type: 0x%02x\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", type);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003057 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003058 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003059}
3060
3061
Christoph Hellwigde103c92012-11-06 12:24:09 -08003062static sense_reason_t
3063core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key,
3064 u64 sa_res_key, int aptpl, int unreg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003065{
Andy Grovere3d6f902011-07-19 08:55:10 +00003066 struct se_session *se_sess = cmd->se_sess;
Andy Grover5951146d2011-07-19 10:26:37 +00003067 struct se_device *dev = cmd->se_dev;
Jörn Engel28168902012-03-15 15:06:58 -04003068 struct se_dev_entry *dest_se_deve = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00003069 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003070 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3071 struct se_port *se_port;
3072 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3073 struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3074 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003075 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003076 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003077 unsigned char *initiator_str;
3078 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
3079 u32 tid_len, tmp_tid_len;
Andy Groverd2843c12013-05-16 10:40:55 -07003080 int new_reg = 0, type, scope, matching_iname;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003081 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003082 unsigned short rtpi;
3083 unsigned char proto_ident;
3084
Andy Grover6708bb22011-06-08 10:36:43 -07003085 if (!se_sess || !se_lun) {
3086 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003087 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003088 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003089
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003090 memset(dest_iport, 0, 64);
3091 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3092 se_tpg = se_sess->se_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00003093 tf_ops = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003094 /*
3095 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3096 * Register behaviors for a REGISTER AND MOVE service action
3097 *
3098 * Locate the existing *pr_reg via struct se_node_acl pointers
3099 */
Andy Grover5951146d2011-07-19 10:26:37 +00003100 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003101 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07003102 if (!pr_reg) {
3103 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003104 " *pr_reg for REGISTER_AND_MOVE\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003105 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003106 }
3107 /*
3108 * The provided reservation key much match the existing reservation key
3109 * provided during this initiator's I_T nexus registration.
3110 */
3111 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07003112 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003113 " res_key: 0x%016Lx does not match existing SA REGISTER"
3114 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003115 ret = TCM_RESERVATION_CONFLICT;
3116 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003117 }
3118 /*
3119 * The service active reservation key needs to be non zero
3120 */
Andy Grover6708bb22011-06-08 10:36:43 -07003121 if (!sa_res_key) {
3122 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003123 " sa_res_key\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003124 ret = TCM_INVALID_PARAMETER_LIST;
3125 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003126 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003127
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003128 /*
3129 * Determine the Relative Target Port Identifier where the reservation
3130 * will be moved to for the TransportID containing SCSI initiator WWN
3131 * information.
3132 */
Andy Grover49493142012-01-16 16:57:08 -08003133 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003134 if (!buf) {
3135 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3136 goto out_put_pr_reg;
3137 }
3138
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003139 rtpi = (buf[18] & 0xff) << 8;
3140 rtpi |= buf[19] & 0xff;
3141 tid_len = (buf[20] & 0xff) << 24;
3142 tid_len |= (buf[21] & 0xff) << 16;
3143 tid_len |= (buf[22] & 0xff) << 8;
3144 tid_len |= buf[23] & 0xff;
Andy Grover49493142012-01-16 16:57:08 -08003145 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003146 buf = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003147
3148 if ((tid_len + 24) != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07003149 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003150 " does not equal CDB data_length: %u\n", tid_len,
3151 cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003152 ret = TCM_INVALID_PARAMETER_LIST;
3153 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003154 }
3155
3156 spin_lock(&dev->se_port_lock);
3157 list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) {
3158 if (se_port->sep_rtpi != rtpi)
3159 continue;
3160 dest_se_tpg = se_port->sep_tpg;
Andy Grover6708bb22011-06-08 10:36:43 -07003161 if (!dest_se_tpg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003162 continue;
Andy Grovere3d6f902011-07-19 08:55:10 +00003163 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
Andy Grover6708bb22011-06-08 10:36:43 -07003164 if (!dest_tf_ops)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003165 continue;
3166
3167 atomic_inc(&dest_se_tpg->tpg_pr_ref_count);
3168 smp_mb__after_atomic_inc();
3169 spin_unlock(&dev->se_port_lock);
3170
Christoph Hellwigde103c92012-11-06 12:24:09 -08003171 if (core_scsi3_tpg_depend_item(dest_se_tpg)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003172 pr_err("core_scsi3_tpg_depend_item() failed"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003173 " for dest_se_tpg\n");
3174 atomic_dec(&dest_se_tpg->tpg_pr_ref_count);
3175 smp_mb__after_atomic_dec();
Christoph Hellwigde103c92012-11-06 12:24:09 -08003176 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3177 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003178 }
3179
3180 spin_lock(&dev->se_port_lock);
3181 break;
3182 }
3183 spin_unlock(&dev->se_port_lock);
3184
Andy Grover6708bb22011-06-08 10:36:43 -07003185 if (!dest_se_tpg || !dest_tf_ops) {
3186 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003187 " fabric ops from Relative Target Port Identifier:"
3188 " %hu\n", rtpi);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003189 ret = TCM_INVALID_PARAMETER_LIST;
3190 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003191 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003192
Andy Grover49493142012-01-16 16:57:08 -08003193 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003194 if (!buf) {
3195 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3196 goto out_put_pr_reg;
3197 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003198 proto_ident = (buf[24] & 0x0f);
Andy Grover8b1e1242012-04-03 15:51:12 -07003199
Andy Grover6708bb22011-06-08 10:36:43 -07003200 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003201 " 0x%02x\n", proto_ident);
Andy Grover8b1e1242012-04-03 15:51:12 -07003202
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003203 if (proto_ident != dest_tf_ops->get_fabric_proto_ident(dest_se_tpg)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003204 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003205 " proto_ident: 0x%02x does not match ident: 0x%02x"
3206 " from fabric: %s\n", proto_ident,
3207 dest_tf_ops->get_fabric_proto_ident(dest_se_tpg),
3208 dest_tf_ops->get_fabric_name());
Christoph Hellwigde103c92012-11-06 12:24:09 -08003209 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003210 goto out;
3211 }
3212 if (dest_tf_ops->tpg_parse_pr_out_transport_id == NULL) {
Andy Grover6708bb22011-06-08 10:36:43 -07003213 pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003214 " containg a valid tpg_parse_pr_out_transport_id"
3215 " function pointer\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003216 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003217 goto out;
3218 }
3219 initiator_str = dest_tf_ops->tpg_parse_pr_out_transport_id(dest_se_tpg,
3220 (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07003221 if (!initiator_str) {
3222 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003223 " initiator_str from Transport ID\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003224 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003225 goto out;
3226 }
3227
Andy Grover49493142012-01-16 16:57:08 -08003228 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003229 buf = NULL;
3230
Andy Grover6708bb22011-06-08 10:36:43 -07003231 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003232 " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
3233 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3234 iport_ptr : "");
3235 /*
3236 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3237 * action specifies a TransportID that is the same as the initiator port
3238 * of the I_T nexus for the command received, then the command shall
3239 * be terminated with CHECK CONDITION status, with the sense key set to
3240 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3241 * IN PARAMETER LIST.
3242 */
3243 pr_reg_nacl = pr_reg->pr_reg_nacl;
3244 matching_iname = (!strcmp(initiator_str,
3245 pr_reg_nacl->initiatorname)) ? 1 : 0;
Andy Grover6708bb22011-06-08 10:36:43 -07003246 if (!matching_iname)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003247 goto after_iport_check;
3248
Andy Grover6708bb22011-06-08 10:36:43 -07003249 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3250 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003251 " matches: %s on received I_T Nexus\n", initiator_str,
3252 pr_reg_nacl->initiatorname);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003253 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003254 goto out;
3255 }
Andy Grover6708bb22011-06-08 10:36:43 -07003256 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3257 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003258 " matches: %s %s on received I_T Nexus\n",
3259 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3260 pr_reg->pr_reg_isid);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003261 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003262 goto out;
3263 }
3264after_iport_check:
3265 /*
3266 * Locate the destination struct se_node_acl from the received Transport ID
3267 */
Roland Dreier286388872011-08-16 09:40:01 -07003268 spin_lock_irq(&dest_se_tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003269 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3270 initiator_str);
3271 if (dest_node_acl) {
3272 atomic_inc(&dest_node_acl->acl_pr_ref_count);
3273 smp_mb__after_atomic_inc();
3274 }
Roland Dreier286388872011-08-16 09:40:01 -07003275 spin_unlock_irq(&dest_se_tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003276
Andy Grover6708bb22011-06-08 10:36:43 -07003277 if (!dest_node_acl) {
3278 pr_err("Unable to locate %s dest_node_acl for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003279 " TransportID%s\n", dest_tf_ops->get_fabric_name(),
3280 initiator_str);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003281 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003282 goto out;
3283 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003284
3285 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003286 pr_err("core_scsi3_nodeacl_depend_item() for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003287 " dest_node_acl\n");
3288 atomic_dec(&dest_node_acl->acl_pr_ref_count);
3289 smp_mb__after_atomic_dec();
3290 dest_node_acl = NULL;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003291 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003292 goto out;
3293 }
Andy Grover8b1e1242012-04-03 15:51:12 -07003294
Andy Grover6708bb22011-06-08 10:36:43 -07003295 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003296 " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
3297 dest_node_acl->initiatorname);
Andy Grover8b1e1242012-04-03 15:51:12 -07003298
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003299 /*
3300 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3301 * PORT IDENTIFIER.
3302 */
3303 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
Andy Grover6708bb22011-06-08 10:36:43 -07003304 if (!dest_se_deve) {
3305 pr_err("Unable to locate %s dest_se_deve from RTPI:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003306 " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003307 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003308 goto out;
3309 }
3310
Christoph Hellwigde103c92012-11-06 12:24:09 -08003311 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003312 pr_err("core_scsi3_lunacl_depend_item() failed\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003313 atomic_dec(&dest_se_deve->pr_ref_count);
3314 smp_mb__after_atomic_dec();
3315 dest_se_deve = NULL;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003316 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003317 goto out;
3318 }
Andy Grover8b1e1242012-04-03 15:51:12 -07003319
Andy Grover6708bb22011-06-08 10:36:43 -07003320 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003321 " ACL for dest_se_deve->mapped_lun: %u\n",
3322 dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
3323 dest_se_deve->mapped_lun);
Andy Grover8b1e1242012-04-03 15:51:12 -07003324
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003325 /*
3326 * A persistent reservation needs to already existing in order to
3327 * successfully complete the REGISTER_AND_MOVE service action..
3328 */
3329 spin_lock(&dev->dev_reservation_lock);
3330 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07003331 if (!pr_res_holder) {
3332 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003333 " currently held\n");
3334 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003335 ret = TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003336 goto out;
3337 }
3338 /*
3339 * The received on I_T Nexus must be the reservation holder.
3340 *
3341 * From spc4r17 section 5.7.8 Table 50 --
3342 * Register behaviors for a REGISTER AND MOVE service action
3343 */
3344 if (pr_res_holder != pr_reg) {
Andy Grover6708bb22011-06-08 10:36:43 -07003345 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003346 " Nexus is not reservation holder\n");
3347 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003348 ret = TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003349 goto out;
3350 }
3351 /*
3352 * From spc4r17 section 5.7.8: registering and moving reservation
3353 *
3354 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3355 * action is received and the established persistent reservation is a
3356 * Write Exclusive - All Registrants type or Exclusive Access -
3357 * All Registrants type reservation, then the command shall be completed
3358 * with RESERVATION CONFLICT status.
3359 */
3360 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3361 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003362 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003363 " reservation for type: %s\n",
3364 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3365 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003366 ret = TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003367 goto out;
3368 }
3369 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3370 /*
3371 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3372 */
3373 type = pr_res_holder->pr_res_type;
3374 scope = pr_res_holder->pr_res_type;
3375 /*
3376 * c) Associate the reservation key specified in the SERVICE ACTION
3377 * RESERVATION KEY field with the I_T nexus specified as the
3378 * destination of the register and move, where:
3379 * A) The I_T nexus is specified by the TransportID and the
3380 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3381 * B) Regardless of the TransportID format used, the association for
3382 * the initiator port is based on either the initiator port name
3383 * (see 3.1.71) on SCSI transport protocols where port names are
3384 * required or the initiator port identifier (see 3.1.70) on SCSI
3385 * transport protocols where port names are not required;
3386 * d) Register the reservation key specified in the SERVICE ACTION
3387 * RESERVATION KEY field;
3388 * e) Retain the reservation key specified in the SERVICE ACTION
3389 * RESERVATION KEY field and associated information;
3390 *
3391 * Also, It is not an error for a REGISTER AND MOVE service action to
3392 * register an I_T nexus that is already registered with the same
3393 * reservation key or a different reservation key.
3394 */
3395 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3396 iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07003397 if (!dest_pr_reg) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08003398 if (core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003399 dest_node_acl, dest_se_deve, iport_ptr,
Christoph Hellwigde103c92012-11-06 12:24:09 -08003400 sa_res_key, 0, aptpl, 2, 1)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003401 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003402 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003403 goto out;
3404 }
3405 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3406 iport_ptr);
3407 new_reg = 1;
3408 }
3409 /*
3410 * f) Release the persistent reservation for the persistent reservation
3411 * holder (i.e., the I_T nexus on which the
3412 */
3413 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
3414 dev->dev_pr_res_holder, 0);
3415 /*
3416 * g) Move the persistent reservation to the specified I_T nexus using
3417 * the same scope and type as the persistent reservation released in
3418 * item f); and
3419 */
3420 dev->dev_pr_res_holder = dest_pr_reg;
3421 dest_pr_reg->pr_res_holder = 1;
3422 dest_pr_reg->pr_res_type = type;
3423 pr_reg->pr_res_scope = scope;
Andy Groverd2843c12013-05-16 10:40:55 -07003424 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003425 /*
3426 * Increment PRGeneration for existing registrations..
3427 */
Andy Grover6708bb22011-06-08 10:36:43 -07003428 if (!new_reg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003429 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3430 spin_unlock(&dev->dev_reservation_lock);
3431
Andy Grover6708bb22011-06-08 10:36:43 -07003432 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003433 " created new reservation holder TYPE: %s on object RTPI:"
3434 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
3435 core_scsi3_pr_dump_type(type), rtpi,
3436 dest_pr_reg->pr_res_generation);
Andy Grover6708bb22011-06-08 10:36:43 -07003437 pr_debug("SPC-3 PR Successfully moved reservation from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003438 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3439 tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07003440 i_buf, dest_tf_ops->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003441 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3442 iport_ptr : "");
3443 /*
3444 * It is now safe to release configfs group dependencies for destination
3445 * of Transport ID Initiator Device/Port Identifier
3446 */
3447 core_scsi3_lunacl_undepend_item(dest_se_deve);
3448 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3449 core_scsi3_tpg_undepend_item(dest_se_tpg);
3450 /*
3451 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3452 * nexus on which PERSISTENT RESERVE OUT command was received.
3453 */
3454 if (unreg) {
3455 spin_lock(&pr_tmpl->registration_lock);
3456 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3457 spin_unlock(&pr_tmpl->registration_lock);
3458 } else
3459 core_scsi3_put_pr_reg(pr_reg);
3460
Andy Grover459f2132013-05-16 10:41:02 -07003461 core_scsi3_update_and_write_aptpl(cmd->se_dev, aptpl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003462
Andy Grover49493142012-01-16 16:57:08 -08003463 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003464
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003465 core_scsi3_put_pr_reg(dest_pr_reg);
3466 return 0;
3467out:
Andy Grover05d1c7c2011-07-20 19:13:28 +00003468 if (buf)
Andy Grover49493142012-01-16 16:57:08 -08003469 transport_kunmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003470 if (dest_se_deve)
3471 core_scsi3_lunacl_undepend_item(dest_se_deve);
3472 if (dest_node_acl)
3473 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3474 core_scsi3_tpg_undepend_item(dest_se_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003475
3476out_put_pr_reg:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003477 core_scsi3_put_pr_reg(pr_reg);
3478 return ret;
3479}
3480
3481static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
3482{
3483 unsigned int __v1, __v2;
3484
3485 __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
3486 __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
3487
3488 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
3489}
3490
3491/*
3492 * See spc4r17 section 6.14 Table 170
3493 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003494sense_reason_t
3495target_scsi3_emulate_pr_out(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003496{
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003497 unsigned char *cdb = &cmd->t_task_cdb[0];
Andy Grover05d1c7c2011-07-20 19:13:28 +00003498 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003499 u64 res_key, sa_res_key;
3500 int sa, scope, type, aptpl;
3501 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003502 sense_reason_t ret;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003503
3504 /*
3505 * Following spc2r20 5.5.1 Reservations overview:
3506 *
3507 * If a logical unit has been reserved by any RESERVE command and is
3508 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3509 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3510 * initiator or service action and shall terminate with a RESERVATION
3511 * CONFLICT status.
3512 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003513 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003514 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3515 " SPC-2 reservation is held, returning"
3516 " RESERVATION_CONFLICT\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003517 return TCM_RESERVATION_CONFLICT;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003518 }
3519
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003520 /*
3521 * FIXME: A NULL struct se_session pointer means an this is not coming from
3522 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3523 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003524 if (!cmd->se_sess)
3525 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003526
3527 if (cmd->data_length < 24) {
Andy Grover6708bb22011-06-08 10:36:43 -07003528 pr_warn("SPC-PR: Received PR OUT parameter list"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003529 " length too small: %u\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003530 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003531 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003532
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003533 /*
3534 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3535 */
3536 sa = (cdb[1] & 0x1f);
3537 scope = (cdb[2] & 0xf0);
3538 type = (cdb[2] & 0x0f);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003539
Andy Grover49493142012-01-16 16:57:08 -08003540 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003541 if (!buf)
3542 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3543
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003544 /*
3545 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3546 */
3547 res_key = core_scsi3_extract_reservation_key(&buf[0]);
3548 sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
3549 /*
3550 * REGISTER_AND_MOVE uses a different SA parameter list containing
3551 * SCSI TransportIDs.
3552 */
3553 if (sa != PRO_REGISTER_AND_MOVE) {
3554 spec_i_pt = (buf[20] & 0x08);
3555 all_tg_pt = (buf[20] & 0x04);
3556 aptpl = (buf[20] & 0x01);
3557 } else {
3558 aptpl = (buf[17] & 0x01);
3559 unreg = (buf[17] & 0x02);
3560 }
Andy Grover49493142012-01-16 16:57:08 -08003561 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003562 buf = NULL;
3563
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003564 /*
3565 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3566 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003567 if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER))
3568 return TCM_INVALID_PARAMETER_LIST;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003569
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003570 /*
3571 * From spc4r17 section 6.14:
3572 *
3573 * If the SPEC_I_PT bit is set to zero, the service action is not
3574 * REGISTER AND MOVE, and the parameter list length is not 24, then
3575 * the command shall be terminated with CHECK CONDITION status, with
3576 * the sense key set to ILLEGAL REQUEST, and the additional sense
3577 * code set to PARAMETER LIST LENGTH ERROR.
3578 */
Andy Grover6708bb22011-06-08 10:36:43 -07003579 if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003580 (cmd->data_length != 24)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003581 pr_warn("SPC-PR: Received PR OUT illegal parameter"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003582 " list length: %u\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003583 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003584 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003585
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003586 /*
3587 * (core_scsi3_emulate_pro_* function parameters
3588 * are defined by spc4r17 Table 174:
3589 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3590 */
3591 switch (sa) {
3592 case PRO_REGISTER:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003593 ret = core_scsi3_emulate_pro_register(cmd,
Andy Grover33ce6a82013-05-16 10:40:54 -07003594 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003595 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003596 case PRO_RESERVE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003597 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3598 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003599 case PRO_RELEASE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003600 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3601 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003602 case PRO_CLEAR:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003603 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3604 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003605 case PRO_PREEMPT:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003606 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07003607 res_key, sa_res_key, PREEMPT);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003608 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003609 case PRO_PREEMPT_AND_ABORT:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003610 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07003611 res_key, sa_res_key, PREEMPT_AND_ABORT);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003612 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003613 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003614 ret = core_scsi3_emulate_pro_register(cmd,
Andy Grover33ce6a82013-05-16 10:40:54 -07003615 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER_AND_IGNORE_EXISTING_KEY);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003616 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003617 case PRO_REGISTER_AND_MOVE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003618 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003619 sa_res_key, aptpl, unreg);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003620 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003621 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003622 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003623 " action: 0x%02x\n", cdb[1] & 0x1f);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003624 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003625 }
3626
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04003627 if (!ret)
3628 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003629 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003630}
3631
3632/*
3633 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3634 *
3635 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3636 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003637static sense_reason_t
3638core_scsi3_pri_read_keys(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003639{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003640 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003641 struct t10_pr_registration *pr_reg;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003642 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003643 u32 add_len = 0, off = 8;
3644
3645 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07003646 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003647 " too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003648 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003649 }
3650
Andy Grover49493142012-01-16 16:57:08 -08003651 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003652 if (!buf)
3653 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3654
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003655 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3656 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3657 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3658 buf[3] = (dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003659
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003660 spin_lock(&dev->t10_pr.registration_lock);
3661 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003662 pr_reg_list) {
3663 /*
3664 * Check for overflow of 8byte PRI READ_KEYS payload and
3665 * next reservation key list descriptor.
3666 */
3667 if ((add_len + 8) > (cmd->data_length - 8))
3668 break;
3669
3670 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3671 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3672 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3673 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
3674 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
3675 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
3676 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
3677 buf[off++] = (pr_reg->pr_res_key & 0xff);
3678
3679 add_len += 8;
3680 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003681 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003682
3683 buf[4] = ((add_len >> 24) & 0xff);
3684 buf[5] = ((add_len >> 16) & 0xff);
3685 buf[6] = ((add_len >> 8) & 0xff);
3686 buf[7] = (add_len & 0xff);
3687
Andy Grover49493142012-01-16 16:57:08 -08003688 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003689
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003690 return 0;
3691}
3692
3693/*
3694 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3695 *
3696 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3697 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003698static sense_reason_t
3699core_scsi3_pri_read_reservation(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003700{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003701 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003702 struct t10_pr_registration *pr_reg;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003703 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003704 u64 pr_res_key;
3705 u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
3706
3707 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07003708 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003709 " too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003710 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003711 }
3712
Andy Grover49493142012-01-16 16:57:08 -08003713 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003714 if (!buf)
3715 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3716
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003717 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3718 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3719 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3720 buf[3] = (dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003721
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003722 spin_lock(&dev->dev_reservation_lock);
3723 pr_reg = dev->dev_pr_res_holder;
Andy Groveree1b1b92012-07-12 17:34:54 -07003724 if (pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003725 /*
3726 * Set the hardcoded Additional Length
3727 */
3728 buf[4] = ((add_len >> 24) & 0xff);
3729 buf[5] = ((add_len >> 16) & 0xff);
3730 buf[6] = ((add_len >> 8) & 0xff);
3731 buf[7] = (add_len & 0xff);
3732
Andy Grover05d1c7c2011-07-20 19:13:28 +00003733 if (cmd->data_length < 22)
3734 goto err;
3735
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003736 /*
3737 * Set the Reservation key.
3738 *
3739 * From spc4r17, section 5.7.10:
3740 * A persistent reservation holder has its reservation key
3741 * returned in the parameter data from a PERSISTENT
3742 * RESERVE IN command with READ RESERVATION service action as
3743 * follows:
3744 * a) For a persistent reservation of the type Write Exclusive
3745 * - All Registrants or Exclusive Access ­ All Regitrants,
3746 * the reservation key shall be set to zero; or
3747 * b) For all other persistent reservation types, the
3748 * reservation key shall be set to the registered
3749 * reservation key for the I_T nexus that holds the
3750 * persistent reservation.
3751 */
3752 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3753 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
3754 pr_res_key = 0;
3755 else
3756 pr_res_key = pr_reg->pr_res_key;
3757
3758 buf[8] = ((pr_res_key >> 56) & 0xff);
3759 buf[9] = ((pr_res_key >> 48) & 0xff);
3760 buf[10] = ((pr_res_key >> 40) & 0xff);
3761 buf[11] = ((pr_res_key >> 32) & 0xff);
3762 buf[12] = ((pr_res_key >> 24) & 0xff);
3763 buf[13] = ((pr_res_key >> 16) & 0xff);
3764 buf[14] = ((pr_res_key >> 8) & 0xff);
3765 buf[15] = (pr_res_key & 0xff);
3766 /*
3767 * Set the SCOPE and TYPE
3768 */
3769 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
3770 (pr_reg->pr_res_type & 0x0f);
3771 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003772
3773err:
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003774 spin_unlock(&dev->dev_reservation_lock);
Andy Grover49493142012-01-16 16:57:08 -08003775 transport_kunmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003776
3777 return 0;
3778}
3779
3780/*
3781 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3782 *
3783 * See spc4r17 section 6.13.4 Table 165
3784 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003785static sense_reason_t
3786core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003787{
Andy Grover5951146d2011-07-19 10:26:37 +00003788 struct se_device *dev = cmd->se_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003789 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003790 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003791 u16 add_len = 8; /* Hardcoded to 8. */
3792
3793 if (cmd->data_length < 6) {
Andy Grover6708bb22011-06-08 10:36:43 -07003794 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003795 " %u too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003796 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003797 }
3798
Andy Grover49493142012-01-16 16:57:08 -08003799 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003800 if (!buf)
3801 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003802
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003803 buf[0] = ((add_len << 8) & 0xff);
3804 buf[1] = (add_len & 0xff);
3805 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3806 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3807 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3808 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3809 /*
3810 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3811 * set the TMV: Task Mask Valid bit.
3812 */
3813 buf[3] |= 0x80;
3814 /*
3815 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3816 */
3817 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3818 /*
3819 * PTPL_A: Persistence across Target Power Loss Active bit
3820 */
3821 if (pr_tmpl->pr_aptpl_active)
3822 buf[3] |= 0x01;
3823 /*
3824 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
3825 */
3826 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3827 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
3828 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
3829 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
3830 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
3831 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3832
Andy Grover49493142012-01-16 16:57:08 -08003833 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003834
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003835 return 0;
3836}
3837
3838/*
3839 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
3840 *
3841 * See spc4r17 section 6.13.5 Table 168 and 169
3842 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003843static sense_reason_t
3844core_scsi3_pri_read_full_status(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003845{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003846 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003847 struct se_node_acl *se_nacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003848 struct se_portal_group *se_tpg;
3849 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003850 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003851 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003852 u32 add_desc_len = 0, add_len = 0, desc_len, exp_desc_len;
3853 u32 off = 8; /* off into first Full Status descriptor */
3854 int format_code = 0;
3855
3856 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07003857 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003858 " too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003859 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003860 }
3861
Andy Grover49493142012-01-16 16:57:08 -08003862 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003863 if (!buf)
3864 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003865
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003866 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3867 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3868 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3869 buf[3] = (dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003870
3871 spin_lock(&pr_tmpl->registration_lock);
3872 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3873 &pr_tmpl->registration_list, pr_reg_list) {
3874
3875 se_nacl = pr_reg->pr_reg_nacl;
3876 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
3877 add_desc_len = 0;
3878
3879 atomic_inc(&pr_reg->pr_res_holders);
3880 smp_mb__after_atomic_inc();
3881 spin_unlock(&pr_tmpl->registration_lock);
3882 /*
3883 * Determine expected length of $FABRIC_MOD specific
3884 * TransportID full status descriptor..
3885 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003886 exp_desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id_len(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003887 se_tpg, se_nacl, pr_reg, &format_code);
3888
3889 if ((exp_desc_len + add_len) > cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07003890 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003891 " out of buffer: %d\n", cmd->data_length);
3892 spin_lock(&pr_tmpl->registration_lock);
3893 atomic_dec(&pr_reg->pr_res_holders);
3894 smp_mb__after_atomic_dec();
3895 break;
3896 }
3897 /*
3898 * Set RESERVATION KEY
3899 */
3900 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3901 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3902 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3903 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
3904 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
3905 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
3906 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
3907 buf[off++] = (pr_reg->pr_res_key & 0xff);
3908 off += 4; /* Skip Over Reserved area */
3909
3910 /*
3911 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
3912 */
3913 if (pr_reg->pr_reg_all_tg_pt)
3914 buf[off] = 0x02;
3915 /*
3916 * The struct se_lun pointer will be present for the
3917 * reservation holder for PR_HOLDER bit.
3918 *
3919 * Also, if this registration is the reservation
3920 * holder, fill in SCOPE and TYPE in the next byte.
3921 */
3922 if (pr_reg->pr_res_holder) {
3923 buf[off++] |= 0x01;
3924 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
3925 (pr_reg->pr_res_type & 0x0f);
3926 } else
3927 off += 2;
3928
3929 off += 4; /* Skip over reserved area */
3930 /*
3931 * From spc4r17 6.3.15:
3932 *
3933 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
3934 * IDENTIFIER field contains the relative port identifier (see
3935 * 3.1.120) of the target port that is part of the I_T nexus
3936 * described by this full status descriptor. If the ALL_TG_PT
3937 * bit is set to one, the contents of the RELATIVE TARGET PORT
3938 * IDENTIFIER field are not defined by this standard.
3939 */
Andy Grover6708bb22011-06-08 10:36:43 -07003940 if (!pr_reg->pr_reg_all_tg_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003941 struct se_port *port = pr_reg->pr_reg_tg_pt_lun->lun_sep;
3942
3943 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
3944 buf[off++] = (port->sep_rtpi & 0xff);
3945 } else
Masanari Iida35d1efe2012-08-16 22:43:13 +09003946 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003947
3948 /*
3949 * Now, have the $FABRIC_MOD fill in the protocol identifier
3950 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003951 desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id(se_tpg,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003952 se_nacl, pr_reg, &format_code, &buf[off+4]);
3953
3954 spin_lock(&pr_tmpl->registration_lock);
3955 atomic_dec(&pr_reg->pr_res_holders);
3956 smp_mb__after_atomic_dec();
3957 /*
3958 * Set the ADDITIONAL DESCRIPTOR LENGTH
3959 */
3960 buf[off++] = ((desc_len >> 24) & 0xff);
3961 buf[off++] = ((desc_len >> 16) & 0xff);
3962 buf[off++] = ((desc_len >> 8) & 0xff);
3963 buf[off++] = (desc_len & 0xff);
3964 /*
3965 * Size of full desctipor header minus TransportID
3966 * containing $FABRIC_MOD specific) initiator device/port
3967 * WWN information.
3968 *
3969 * See spc4r17 Section 6.13.5 Table 169
3970 */
3971 add_desc_len = (24 + desc_len);
3972
3973 off += desc_len;
3974 add_len += add_desc_len;
3975 }
3976 spin_unlock(&pr_tmpl->registration_lock);
3977 /*
3978 * Set ADDITIONAL_LENGTH
3979 */
3980 buf[4] = ((add_len >> 24) & 0xff);
3981 buf[5] = ((add_len >> 16) & 0xff);
3982 buf[6] = ((add_len >> 8) & 0xff);
3983 buf[7] = (add_len & 0xff);
3984
Andy Grover49493142012-01-16 16:57:08 -08003985 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003986
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003987 return 0;
3988}
3989
Christoph Hellwigde103c92012-11-06 12:24:09 -08003990sense_reason_t
3991target_scsi3_emulate_pr_in(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003992{
Christoph Hellwigde103c92012-11-06 12:24:09 -08003993 sense_reason_t ret;
Christoph Hellwige76a35d2011-11-03 17:50:42 -04003994
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003995 /*
3996 * Following spc2r20 5.5.1 Reservations overview:
3997 *
3998 * If a logical unit has been reserved by any RESERVE command and is
3999 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4000 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4001 * initiator or service action and shall terminate with a RESERVATION
4002 * CONFLICT status.
4003 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04004004 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004005 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4006 " SPC-2 reservation is held, returning"
4007 " RESERVATION_CONFLICT\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08004008 return TCM_RESERVATION_CONFLICT;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004009 }
4010
4011 switch (cmd->t_task_cdb[1] & 0x1f) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004012 case PRI_READ_KEYS:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004013 ret = core_scsi3_pri_read_keys(cmd);
4014 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004015 case PRI_READ_RESERVATION:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004016 ret = core_scsi3_pri_read_reservation(cmd);
4017 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004018 case PRI_REPORT_CAPABILITIES:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004019 ret = core_scsi3_pri_report_capabilities(cmd);
4020 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004021 case PRI_READ_FULL_STATUS:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004022 ret = core_scsi3_pri_read_full_status(cmd);
4023 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004024 default:
Andy Grover6708bb22011-06-08 10:36:43 -07004025 pr_err("Unknown PERSISTENT_RESERVE_IN service"
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004026 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
Christoph Hellwigde103c92012-11-06 12:24:09 -08004027 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004028 }
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004029
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04004030 if (!ret)
4031 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004032 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004033}
4034
Christoph Hellwigde103c92012-11-06 12:24:09 -08004035sense_reason_t
4036target_check_reservation(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004037{
Christoph Hellwigd977f432012-10-10 17:37:15 -04004038 struct se_device *dev = cmd->se_dev;
Christoph Hellwigde103c92012-11-06 12:24:09 -08004039 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004040
Christoph Hellwigd977f432012-10-10 17:37:15 -04004041 if (!cmd->se_sess)
4042 return 0;
4043 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
4044 return 0;
4045 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
4046 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004047
Christoph Hellwigd977f432012-10-10 17:37:15 -04004048 spin_lock(&dev->dev_reservation_lock);
4049 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
4050 ret = target_scsi2_reservation_check(cmd);
4051 else
4052 ret = target_scsi3_pr_reservation_check(cmd);
4053 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04004054
Christoph Hellwigd977f432012-10-10 17:37:15 -04004055 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004056}