blob: 26c1f2d8920f36fc71fccb233f7de47636717ce4 [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
Andy Grover4e529be2013-05-16 10:40:59 -0700609 pr_reg->pr_aptpl_buf = kzalloc(PR_APTPL_BUF_LEN, GFP_ATOMIC);
Andy Grover6708bb22011-06-08 10:36:43 -0700610 if (!pr_reg->pr_aptpl_buf) {
611 pr_err("Unable to allocate pr_reg->pr_aptpl_buf\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800612 kmem_cache_free(t10_pr_reg_cache, pr_reg);
613 return NULL;
614 }
615
616 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
617 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
618 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
619 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
620 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
621 atomic_set(&pr_reg->pr_res_holders, 0);
622 pr_reg->pr_reg_nacl = nacl;
623 pr_reg->pr_reg_deve = deve;
624 pr_reg->pr_res_mapped_lun = deve->mapped_lun;
625 pr_reg->pr_aptpl_target_lun = deve->se_lun->unpacked_lun;
626 pr_reg->pr_res_key = sa_res_key;
627 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
628 pr_reg->pr_reg_aptpl = aptpl;
629 pr_reg->pr_reg_tg_pt_lun = deve->se_lun;
630 /*
631 * If an ISID value for this SCSI Initiator Port exists,
632 * save it to the registration now.
633 */
634 if (isid != NULL) {
635 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
636 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
637 pr_reg->isid_present_at_reg = 1;
638 }
639
640 return pr_reg;
641}
642
643static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
644static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
645
646/*
647 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
648 * modes.
649 */
650static struct t10_pr_registration *__core_scsi3_alloc_registration(
651 struct se_device *dev,
652 struct se_node_acl *nacl,
653 struct se_dev_entry *deve,
654 unsigned char *isid,
655 u64 sa_res_key,
656 int all_tg_pt,
657 int aptpl)
658{
659 struct se_dev_entry *deve_tmp;
660 struct se_node_acl *nacl_tmp;
661 struct se_port *port, *port_tmp;
662 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
663 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
664 int ret;
665 /*
666 * Create a registration for the I_T Nexus upon which the
667 * PROUT REGISTER was received.
668 */
669 pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, deve, isid,
670 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -0700671 if (!pr_reg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800672 return NULL;
673 /*
674 * Return pointer to pr_reg for ALL_TG_PT=0
675 */
Andy Grover6708bb22011-06-08 10:36:43 -0700676 if (!all_tg_pt)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800677 return pr_reg;
678 /*
679 * Create list of matching SCSI Initiator Port registrations
680 * for ALL_TG_PT=1
681 */
682 spin_lock(&dev->se_port_lock);
683 list_for_each_entry_safe(port, port_tmp, &dev->dev_sep_list, sep_list) {
684 atomic_inc(&port->sep_tg_pt_ref_cnt);
685 smp_mb__after_atomic_inc();
686 spin_unlock(&dev->se_port_lock);
687
688 spin_lock_bh(&port->sep_alua_lock);
689 list_for_each_entry(deve_tmp, &port->sep_alua_list,
690 alua_port_list) {
691 /*
692 * This pointer will be NULL for demo mode MappedLUNs
693 * that have not been make explict via a ConfigFS
694 * MappedLUN group for the SCSI Initiator Node ACL.
695 */
Andy Grover6708bb22011-06-08 10:36:43 -0700696 if (!deve_tmp->se_lun_acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800697 continue;
698
699 nacl_tmp = deve_tmp->se_lun_acl->se_lun_nacl;
700 /*
701 * Skip the matching struct se_node_acl that is allocated
702 * above..
703 */
704 if (nacl == nacl_tmp)
705 continue;
706 /*
707 * Only perform PR registrations for target ports on
708 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
709 * arrived.
710 */
711 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
712 continue;
713 /*
714 * Look for a matching Initiator Node ACL in ASCII format
715 */
716 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
717 continue;
718
719 atomic_inc(&deve_tmp->pr_ref_count);
720 smp_mb__after_atomic_inc();
721 spin_unlock_bh(&port->sep_alua_lock);
722 /*
723 * Grab a configfs group dependency that is released
724 * for the exception path at label out: below, or upon
725 * completion of adding ALL_TG_PT=1 registrations in
726 * __core_scsi3_add_registration()
727 */
728 ret = core_scsi3_lunacl_depend_item(deve_tmp);
729 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -0700730 pr_err("core_scsi3_lunacl_depend"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800731 "_item() failed\n");
732 atomic_dec(&port->sep_tg_pt_ref_cnt);
733 smp_mb__after_atomic_dec();
734 atomic_dec(&deve_tmp->pr_ref_count);
735 smp_mb__after_atomic_dec();
736 goto out;
737 }
738 /*
739 * Located a matching SCSI Initiator Port on a different
740 * port, allocate the pr_reg_atp and attach it to the
741 * pr_reg->pr_reg_atp_list that will be processed once
742 * the original *pr_reg is processed in
743 * __core_scsi3_add_registration()
744 */
745 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
746 nacl_tmp, deve_tmp, NULL,
747 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -0700748 if (!pr_reg_atp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800749 atomic_dec(&port->sep_tg_pt_ref_cnt);
750 smp_mb__after_atomic_dec();
751 atomic_dec(&deve_tmp->pr_ref_count);
752 smp_mb__after_atomic_dec();
753 core_scsi3_lunacl_undepend_item(deve_tmp);
754 goto out;
755 }
756
757 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
758 &pr_reg->pr_reg_atp_list);
759 spin_lock_bh(&port->sep_alua_lock);
760 }
761 spin_unlock_bh(&port->sep_alua_lock);
762
763 spin_lock(&dev->se_port_lock);
764 atomic_dec(&port->sep_tg_pt_ref_cnt);
765 smp_mb__after_atomic_dec();
766 }
767 spin_unlock(&dev->se_port_lock);
768
769 return pr_reg;
770out:
771 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
772 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
773 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
774 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
775 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
776 }
777 kmem_cache_free(t10_pr_reg_cache, pr_reg);
778 return NULL;
779}
780
781int core_scsi3_alloc_aptpl_registration(
Andy Grovere3d6f902011-07-19 08:55:10 +0000782 struct t10_reservation *pr_tmpl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800783 u64 sa_res_key,
784 unsigned char *i_port,
785 unsigned char *isid,
786 u32 mapped_lun,
787 unsigned char *t_port,
788 u16 tpgt,
789 u32 target_lun,
790 int res_holder,
791 int all_tg_pt,
792 u8 type)
793{
794 struct t10_pr_registration *pr_reg;
795
Andy Grover6708bb22011-06-08 10:36:43 -0700796 if (!i_port || !t_port || !sa_res_key) {
797 pr_err("Illegal parameters for APTPL registration\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000798 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800799 }
800
801 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700802 if (!pr_reg) {
803 pr_err("Unable to allocate struct t10_pr_registration\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000804 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800805 }
Andy Grover4e529be2013-05-16 10:40:59 -0700806 pr_reg->pr_aptpl_buf = kzalloc(PR_APTPL_BUF_LEN, GFP_KERNEL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800807
808 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
809 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
810 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
811 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
812 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
813 atomic_set(&pr_reg->pr_res_holders, 0);
814 pr_reg->pr_reg_nacl = NULL;
815 pr_reg->pr_reg_deve = NULL;
816 pr_reg->pr_res_mapped_lun = mapped_lun;
817 pr_reg->pr_aptpl_target_lun = target_lun;
818 pr_reg->pr_res_key = sa_res_key;
819 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
820 pr_reg->pr_reg_aptpl = 1;
821 pr_reg->pr_reg_tg_pt_lun = NULL;
822 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
823 pr_reg->pr_res_type = type;
824 /*
825 * If an ISID value had been saved in APTPL metadata for this
826 * SCSI Initiator Port, restore it now.
827 */
828 if (isid != NULL) {
829 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
830 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
831 pr_reg->isid_present_at_reg = 1;
832 }
833 /*
834 * Copy the i_port and t_port information from caller.
835 */
836 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
837 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
838 pr_reg->pr_reg_tpgt = tpgt;
839 /*
840 * Set pr_res_holder from caller, the pr_reg who is the reservation
841 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
842 * the Initiator Node LUN ACL from the fabric module is created for
843 * this registration.
844 */
845 pr_reg->pr_res_holder = res_holder;
846
847 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
Andy Grover6708bb22011-06-08 10:36:43 -0700848 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800849 " metadata\n", (res_holder) ? "+reservation" : "");
850 return 0;
851}
852
853static void core_scsi3_aptpl_reserve(
854 struct se_device *dev,
855 struct se_portal_group *tpg,
856 struct se_node_acl *node_acl,
857 struct t10_pr_registration *pr_reg)
858{
859 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800860
861 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -0700862 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800863
864 spin_lock(&dev->dev_reservation_lock);
865 dev->dev_pr_res_holder = pr_reg;
866 spin_unlock(&dev->dev_reservation_lock);
867
Andy Grover6708bb22011-06-08 10:36:43 -0700868 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800869 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000870 tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800871 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
872 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -0700873 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000874 tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -0700875 i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800876}
877
878static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
Andy Grover33ce6a82013-05-16 10:40:54 -0700879 struct t10_pr_registration *, enum register_type, int);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800880
881static int __core_scsi3_check_aptpl_registration(
882 struct se_device *dev,
883 struct se_portal_group *tpg,
884 struct se_lun *lun,
885 u32 target_lun,
886 struct se_node_acl *nacl,
887 struct se_dev_entry *deve)
888{
889 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400890 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800891 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
892 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
893 u16 tpgt;
894
895 memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
896 memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
897 /*
898 * Copy Initiator Port information from struct se_node_acl
899 */
900 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
901 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
Andy Grovere3d6f902011-07-19 08:55:10 +0000902 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
903 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800904 /*
905 * Look for the matching registrations+reservation from those
906 * created from APTPL metadata. Note that multiple registrations
907 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
908 * TransportIDs.
909 */
910 spin_lock(&pr_tmpl->aptpl_reg_lock);
911 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
912 pr_reg_aptpl_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700913 if (!strcmp(pr_reg->pr_iport, i_port) &&
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800914 (pr_reg->pr_res_mapped_lun == deve->mapped_lun) &&
915 !(strcmp(pr_reg->pr_tport, t_port)) &&
916 (pr_reg->pr_reg_tpgt == tpgt) &&
917 (pr_reg->pr_aptpl_target_lun == target_lun)) {
918
919 pr_reg->pr_reg_nacl = nacl;
920 pr_reg->pr_reg_deve = deve;
921 pr_reg->pr_reg_tg_pt_lun = lun;
922
923 list_del(&pr_reg->pr_reg_aptpl_list);
924 spin_unlock(&pr_tmpl->aptpl_reg_lock);
925 /*
926 * At this point all of the pointers in *pr_reg will
927 * be setup, so go ahead and add the registration.
928 */
929
930 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
931 /*
932 * If this registration is the reservation holder,
933 * make that happen now..
934 */
935 if (pr_reg->pr_res_holder)
936 core_scsi3_aptpl_reserve(dev, tpg,
937 nacl, pr_reg);
938 /*
939 * Reenable pr_aptpl_active to accept new metadata
940 * updates once the SCSI device is active again..
941 */
942 spin_lock(&pr_tmpl->aptpl_reg_lock);
943 pr_tmpl->pr_aptpl_active = 1;
944 }
945 }
946 spin_unlock(&pr_tmpl->aptpl_reg_lock);
947
948 return 0;
949}
950
951int core_scsi3_check_aptpl_registration(
952 struct se_device *dev,
953 struct se_portal_group *tpg,
954 struct se_lun *lun,
955 struct se_lun_acl *lun_acl)
956{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800957 struct se_node_acl *nacl = lun_acl->se_lun_nacl;
Jörn Engelf2083242012-03-15 15:05:40 -0400958 struct se_dev_entry *deve = nacl->device_list[lun_acl->mapped_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800959
Christoph Hellwigd977f432012-10-10 17:37:15 -0400960 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800961 return 0;
962
963 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
964 lun->unpacked_lun, nacl, deve);
965}
966
967static void __core_scsi3_dump_registration(
968 struct target_core_fabric_ops *tfo,
969 struct se_device *dev,
970 struct se_node_acl *nacl,
971 struct t10_pr_registration *pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -0700972 enum register_type register_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800973{
974 struct se_portal_group *se_tpg = nacl->se_tpg;
975 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800976
977 memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -0700978 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800979
Andy Grover6708bb22011-06-08 10:36:43 -0700980 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
Andy Grover33ce6a82013-05-16 10:40:54 -0700981 " Node: %s%s\n", tfo->get_fabric_name(), (register_type == REGISTER_AND_MOVE) ?
982 "_AND_MOVE" : (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ?
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800983 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -0700984 i_buf);
Andy Grover6708bb22011-06-08 10:36:43 -0700985 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800986 tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg),
987 tfo->tpg_get_tag(se_tpg));
Andy Grover6708bb22011-06-08 10:36:43 -0700988 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800989 " Port(s)\n", tfo->get_fabric_name(),
990 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
Andy Grovere3d6f902011-07-19 08:55:10 +0000991 dev->transport->name);
Andy Grover6708bb22011-06-08 10:36:43 -0700992 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800993 " 0x%08x APTPL: %d\n", tfo->get_fabric_name(),
994 pr_reg->pr_res_key, pr_reg->pr_res_generation,
995 pr_reg->pr_reg_aptpl);
996}
997
998/*
999 * this function can be called with struct se_device->dev_reservation_lock
1000 * when register_move = 1
1001 */
1002static void __core_scsi3_add_registration(
1003 struct se_device *dev,
1004 struct se_node_acl *nacl,
1005 struct t10_pr_registration *pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07001006 enum register_type register_type,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001007 int register_move)
1008{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001009 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1010 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001011 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001012
1013 /*
1014 * Increment PRgeneration counter for struct se_device upon a successful
1015 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1016 *
1017 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1018 * action, the struct se_device->dev_reservation_lock will already be held,
1019 * so we do not call core_scsi3_pr_generation() which grabs the lock
1020 * for the REGISTER.
1021 */
1022 pr_reg->pr_res_generation = (register_move) ?
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001023 dev->t10_pr.pr_generation++ :
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001024 core_scsi3_pr_generation(dev);
1025
1026 spin_lock(&pr_tmpl->registration_lock);
1027 list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
1028 pr_reg->pr_reg_deve->def_pr_registered = 1;
1029
1030 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1031 spin_unlock(&pr_tmpl->registration_lock);
1032 /*
1033 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1034 */
Andy Grover6708bb22011-06-08 10:36:43 -07001035 if (!pr_reg->pr_reg_all_tg_pt || register_move)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001036 return;
1037 /*
1038 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1039 * allocated in __core_scsi3_alloc_registration()
1040 */
1041 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1042 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
1043 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1044
1045 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1046
1047 spin_lock(&pr_tmpl->registration_lock);
1048 list_add_tail(&pr_reg_tmp->pr_reg_list,
1049 &pr_tmpl->registration_list);
1050 pr_reg_tmp->pr_reg_deve->def_pr_registered = 1;
1051
1052 __core_scsi3_dump_registration(tfo, dev,
1053 pr_reg_tmp->pr_reg_nacl, pr_reg_tmp,
1054 register_type);
1055 spin_unlock(&pr_tmpl->registration_lock);
1056 /*
1057 * Drop configfs group dependency reference from
1058 * __core_scsi3_alloc_registration()
1059 */
1060 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1061 }
1062}
1063
1064static int core_scsi3_alloc_registration(
1065 struct se_device *dev,
1066 struct se_node_acl *nacl,
1067 struct se_dev_entry *deve,
1068 unsigned char *isid,
1069 u64 sa_res_key,
1070 int all_tg_pt,
1071 int aptpl,
Andy Grover33ce6a82013-05-16 10:40:54 -07001072 enum register_type register_type,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001073 int register_move)
1074{
1075 struct t10_pr_registration *pr_reg;
1076
1077 pr_reg = __core_scsi3_alloc_registration(dev, nacl, deve, isid,
1078 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001079 if (!pr_reg)
Andy Grovere3d6f902011-07-19 08:55:10 +00001080 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001081
1082 __core_scsi3_add_registration(dev, nacl, pr_reg,
1083 register_type, register_move);
1084 return 0;
1085}
1086
1087static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1088 struct se_device *dev,
1089 struct se_node_acl *nacl,
1090 unsigned char *isid)
1091{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001092 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001093 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1094 struct se_portal_group *tpg;
1095
1096 spin_lock(&pr_tmpl->registration_lock);
1097 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1098 &pr_tmpl->registration_list, pr_reg_list) {
1099 /*
1100 * First look for a matching struct se_node_acl
1101 */
1102 if (pr_reg->pr_reg_nacl != nacl)
1103 continue;
1104
1105 tpg = pr_reg->pr_reg_nacl->se_tpg;
1106 /*
1107 * If this registration does NOT contain a fabric provided
1108 * ISID, then we have found a match.
1109 */
Andy Grover6708bb22011-06-08 10:36:43 -07001110 if (!pr_reg->isid_present_at_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001111 /*
1112 * Determine if this SCSI device server requires that
1113 * SCSI Intiatior TransportID w/ ISIDs is enforced
1114 * for fabric modules (iSCSI) requiring them.
1115 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001116 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001117 if (dev->dev_attrib.enforce_pr_isids)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001118 continue;
1119 }
1120 atomic_inc(&pr_reg->pr_res_holders);
1121 smp_mb__after_atomic_inc();
1122 spin_unlock(&pr_tmpl->registration_lock);
1123 return pr_reg;
1124 }
1125 /*
1126 * If the *pr_reg contains a fabric defined ISID for multi-value
1127 * SCSI Initiator Port TransportIDs, then we expect a valid
1128 * matching ISID to be provided by the local SCSI Initiator Port.
1129 */
Andy Grover6708bb22011-06-08 10:36:43 -07001130 if (!isid)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001131 continue;
1132 if (strcmp(isid, pr_reg->pr_reg_isid))
1133 continue;
1134
1135 atomic_inc(&pr_reg->pr_res_holders);
1136 smp_mb__after_atomic_inc();
1137 spin_unlock(&pr_tmpl->registration_lock);
1138 return pr_reg;
1139 }
1140 spin_unlock(&pr_tmpl->registration_lock);
1141
1142 return NULL;
1143}
1144
1145static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1146 struct se_device *dev,
1147 struct se_node_acl *nacl,
1148 struct se_session *sess)
1149{
1150 struct se_portal_group *tpg = nacl->se_tpg;
1151 unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1152
Andy Grovere3d6f902011-07-19 08:55:10 +00001153 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001154 memset(&buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +00001155 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001156 PR_REG_ISID_LEN);
1157 isid_ptr = &buf[0];
1158 }
1159
1160 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1161}
1162
1163static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1164{
1165 atomic_dec(&pr_reg->pr_res_holders);
1166 smp_mb__after_atomic_dec();
1167}
1168
1169static int core_scsi3_check_implict_release(
1170 struct se_device *dev,
1171 struct t10_pr_registration *pr_reg)
1172{
1173 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1174 struct t10_pr_registration *pr_res_holder;
1175 int ret = 0;
1176
1177 spin_lock(&dev->dev_reservation_lock);
1178 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001179 if (!pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001180 spin_unlock(&dev->dev_reservation_lock);
1181 return ret;
1182 }
1183 if (pr_res_holder == pr_reg) {
1184 /*
1185 * Perform an implict RELEASE if the registration that
1186 * is being released is holding the reservation.
1187 *
1188 * From spc4r17, section 5.7.11.1:
1189 *
1190 * e) If the I_T nexus is the persistent reservation holder
1191 * and the persistent reservation is not an all registrants
1192 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1193 * service action or REGISTER AND IGNORE EXISTING KEY
1194 * service action with the SERVICE ACTION RESERVATION KEY
1195 * field set to zero (see 5.7.11.3).
1196 */
1197 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0);
1198 ret = 1;
1199 /*
1200 * For 'All Registrants' reservation types, all existing
1201 * registrations are still processed as reservation holders
1202 * in core_scsi3_pr_seq_non_holder() after the initial
1203 * reservation holder is implictly released here.
1204 */
1205 } else if (pr_reg->pr_reg_all_tg_pt &&
1206 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1207 pr_reg->pr_reg_nacl->initiatorname)) &&
1208 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001209 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001210 " UNREGISTER while existing reservation with matching"
1211 " key 0x%016Lx is present from another SCSI Initiator"
1212 " Port\n", pr_reg->pr_res_key);
Andy Grovere3d6f902011-07-19 08:55:10 +00001213 ret = -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001214 }
1215 spin_unlock(&dev->dev_reservation_lock);
1216
1217 return ret;
1218}
1219
1220/*
Andy Grovere3d6f902011-07-19 08:55:10 +00001221 * Called with struct t10_reservation->registration_lock held.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001222 */
1223static void __core_scsi3_free_registration(
1224 struct se_device *dev,
1225 struct t10_pr_registration *pr_reg,
1226 struct list_head *preempt_and_abort_list,
1227 int dec_holders)
1228{
1229 struct target_core_fabric_ops *tfo =
1230 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001231 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001232 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001233
1234 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07001235 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001236
1237 pr_reg->pr_reg_deve->def_pr_registered = 0;
1238 pr_reg->pr_reg_deve->pr_res_key = 0;
1239 list_del(&pr_reg->pr_reg_list);
1240 /*
1241 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1242 * so call core_scsi3_put_pr_reg() to decrement our reference.
1243 */
1244 if (dec_holders)
1245 core_scsi3_put_pr_reg(pr_reg);
1246 /*
1247 * Wait until all reference from any other I_T nexuses for this
1248 * *pr_reg have been released. Because list_del() is called above,
1249 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1250 * count back to zero, and we release *pr_reg.
1251 */
1252 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
1253 spin_unlock(&pr_tmpl->registration_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07001254 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001255 tfo->get_fabric_name());
1256 cpu_relax();
1257 spin_lock(&pr_tmpl->registration_lock);
1258 }
1259
Andy Grover6708bb22011-06-08 10:36:43 -07001260 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001261 " Node: %s%s\n", tfo->get_fabric_name(),
1262 pr_reg->pr_reg_nacl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07001263 i_buf);
Andy Grover6708bb22011-06-08 10:36:43 -07001264 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001265 " Port(s)\n", tfo->get_fabric_name(),
1266 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
Andy Grovere3d6f902011-07-19 08:55:10 +00001267 dev->transport->name);
Andy Grover6708bb22011-06-08 10:36:43 -07001268 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001269 " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key,
1270 pr_reg->pr_res_generation);
1271
Andy Grover6708bb22011-06-08 10:36:43 -07001272 if (!preempt_and_abort_list) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001273 pr_reg->pr_reg_deve = NULL;
1274 pr_reg->pr_reg_nacl = NULL;
1275 kfree(pr_reg->pr_aptpl_buf);
1276 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1277 return;
1278 }
1279 /*
1280 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1281 * are released once the ABORT_TASK_SET has completed..
1282 */
1283 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1284}
1285
1286void core_scsi3_free_pr_reg_from_nacl(
1287 struct se_device *dev,
1288 struct se_node_acl *nacl)
1289{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001290 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001291 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1292 /*
1293 * If the passed se_node_acl matches the reservation holder,
1294 * release the reservation.
1295 */
1296 spin_lock(&dev->dev_reservation_lock);
1297 pr_res_holder = dev->dev_pr_res_holder;
1298 if ((pr_res_holder != NULL) &&
1299 (pr_res_holder->pr_reg_nacl == nacl))
1300 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0);
1301 spin_unlock(&dev->dev_reservation_lock);
1302 /*
1303 * Release any registration associated with the struct se_node_acl.
1304 */
1305 spin_lock(&pr_tmpl->registration_lock);
1306 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1307 &pr_tmpl->registration_list, pr_reg_list) {
1308
1309 if (pr_reg->pr_reg_nacl != nacl)
1310 continue;
1311
1312 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1313 }
1314 spin_unlock(&pr_tmpl->registration_lock);
1315}
1316
1317void core_scsi3_free_all_registrations(
1318 struct se_device *dev)
1319{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001320 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001321 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1322
1323 spin_lock(&dev->dev_reservation_lock);
1324 pr_res_holder = dev->dev_pr_res_holder;
1325 if (pr_res_holder != NULL) {
1326 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1327 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
1328 pr_res_holder, 0);
1329 }
1330 spin_unlock(&dev->dev_reservation_lock);
1331
1332 spin_lock(&pr_tmpl->registration_lock);
1333 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1334 &pr_tmpl->registration_list, pr_reg_list) {
1335
1336 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1337 }
1338 spin_unlock(&pr_tmpl->registration_lock);
1339
1340 spin_lock(&pr_tmpl->aptpl_reg_lock);
1341 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1342 pr_reg_aptpl_list) {
1343 list_del(&pr_reg->pr_reg_aptpl_list);
1344 kfree(pr_reg->pr_aptpl_buf);
1345 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1346 }
1347 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1348}
1349
1350static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1351{
Andy Grovere3d6f902011-07-19 08:55:10 +00001352 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001353 &tpg->tpg_group.cg_item);
1354}
1355
1356static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1357{
Andy Grovere3d6f902011-07-19 08:55:10 +00001358 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001359 &tpg->tpg_group.cg_item);
1360
1361 atomic_dec(&tpg->tpg_pr_ref_count);
1362 smp_mb__after_atomic_dec();
1363}
1364
1365static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1366{
1367 struct se_portal_group *tpg = nacl->se_tpg;
1368
1369 if (nacl->dynamic_node_acl)
1370 return 0;
1371
Andy Grovere3d6f902011-07-19 08:55:10 +00001372 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001373 &nacl->acl_group.cg_item);
1374}
1375
1376static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1377{
1378 struct se_portal_group *tpg = nacl->se_tpg;
1379
1380 if (nacl->dynamic_node_acl) {
1381 atomic_dec(&nacl->acl_pr_ref_count);
1382 smp_mb__after_atomic_dec();
1383 return;
1384 }
1385
Andy Grovere3d6f902011-07-19 08:55:10 +00001386 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001387 &nacl->acl_group.cg_item);
1388
1389 atomic_dec(&nacl->acl_pr_ref_count);
1390 smp_mb__after_atomic_dec();
1391}
1392
1393static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1394{
1395 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1396 struct se_node_acl *nacl;
1397 struct se_portal_group *tpg;
1398 /*
1399 * For nacl->dynamic_node_acl=1
1400 */
Andy Grover6708bb22011-06-08 10:36:43 -07001401 if (!lun_acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001402 return 0;
1403
1404 nacl = lun_acl->se_lun_nacl;
1405 tpg = nacl->se_tpg;
1406
Andy Grovere3d6f902011-07-19 08:55:10 +00001407 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001408 &lun_acl->se_lun_group.cg_item);
1409}
1410
1411static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1412{
1413 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1414 struct se_node_acl *nacl;
1415 struct se_portal_group *tpg;
1416 /*
1417 * For nacl->dynamic_node_acl=1
1418 */
Andy Grover6708bb22011-06-08 10:36:43 -07001419 if (!lun_acl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001420 atomic_dec(&se_deve->pr_ref_count);
1421 smp_mb__after_atomic_dec();
1422 return;
1423 }
1424 nacl = lun_acl->se_lun_nacl;
1425 tpg = nacl->se_tpg;
1426
Andy Grovere3d6f902011-07-19 08:55:10 +00001427 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001428 &lun_acl->se_lun_group.cg_item);
1429
1430 atomic_dec(&se_deve->pr_ref_count);
1431 smp_mb__after_atomic_dec();
1432}
1433
Christoph Hellwigde103c92012-11-06 12:24:09 -08001434static sense_reason_t
1435core_scsi3_decode_spec_i_port(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001436 struct se_cmd *cmd,
1437 struct se_portal_group *tpg,
1438 unsigned char *l_isid,
1439 u64 sa_res_key,
1440 int all_tg_pt,
1441 int aptpl)
1442{
Andy Grover5951146d2011-07-19 10:26:37 +00001443 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001444 struct se_port *tmp_port;
1445 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00001446 struct se_session *se_sess = cmd->se_sess;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001447 struct se_node_acl *dest_node_acl = NULL;
1448 struct se_dev_entry *dest_se_deve = NULL, *local_se_deve;
1449 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1450 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
Roland Dreierd0f474e2012-01-12 10:41:18 -08001451 LIST_HEAD(tid_dest_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001452 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
1453 struct target_core_fabric_ops *tmp_tf_ops;
Andy Grover05d1c7c2011-07-20 19:13:28 +00001454 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001455 unsigned char *ptr, *i_str = NULL, proto_ident, tmp_proto_ident;
1456 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
Christoph Hellwigde103c92012-11-06 12:24:09 -08001457 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001458 u32 tpdl, tid_len = 0;
Andy Groverd2843c12013-05-16 10:40:55 -07001459 int dest_local_nexus;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001460 u32 dest_rtpi = 0;
1461
1462 memset(dest_iport, 0, 64);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001463
Jörn Engelf2083242012-03-15 15:05:40 -04001464 local_se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001465 /*
1466 * Allocate a struct pr_transport_id_holder and setup the
1467 * local_node_acl and local_se_deve pointers and add to
1468 * struct list_head tid_dest_list for add registration
1469 * processing in the loop of tid_dest_list below.
1470 */
1471 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001472 if (!tidh_new) {
1473 pr_err("Unable to allocate tidh_new\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08001474 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001475 }
1476 INIT_LIST_HEAD(&tidh_new->dest_list);
1477 tidh_new->dest_tpg = tpg;
1478 tidh_new->dest_node_acl = se_sess->se_node_acl;
1479 tidh_new->dest_se_deve = local_se_deve;
1480
Andy Grover5951146d2011-07-19 10:26:37 +00001481 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001482 se_sess->se_node_acl, local_se_deve, l_isid,
1483 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001484 if (!local_pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001485 kfree(tidh_new);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001486 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001487 }
1488 tidh_new->dest_pr_reg = local_pr_reg;
1489 /*
1490 * The local I_T nexus does not hold any configfs dependances,
1491 * so we set tid_h->dest_local_nexus=1 to prevent the
1492 * configfs_undepend_item() calls in the tid_dest_list loops below.
1493 */
1494 tidh_new->dest_local_nexus = 1;
1495 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
Andy Grover05d1c7c2011-07-20 19:13:28 +00001496
Paolo Bonzini0d7f1292012-09-07 17:30:33 +02001497 if (cmd->data_length < 28) {
1498 pr_warn("SPC-PR: Received PR OUT parameter list"
1499 " length too small: %u\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001500 ret = TCM_INVALID_PARAMETER_LIST;
Paolo Bonzini0d7f1292012-09-07 17:30:33 +02001501 goto out;
1502 }
1503
Andy Grover49493142012-01-16 16:57:08 -08001504 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001505 if (!buf) {
1506 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1507 goto out;
1508 }
1509
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001510 /*
1511 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1512 * first extract TransportID Parameter Data Length, and make sure
1513 * the value matches up to the SCSI expected data transfer length.
1514 */
1515 tpdl = (buf[24] & 0xff) << 24;
1516 tpdl |= (buf[25] & 0xff) << 16;
1517 tpdl |= (buf[26] & 0xff) << 8;
1518 tpdl |= buf[27] & 0xff;
1519
1520 if ((tpdl + 28) != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07001521 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001522 " does not equal CDB data_length: %u\n", tpdl,
1523 cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001524 ret = TCM_INVALID_PARAMETER_LIST;
1525 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001526 }
1527 /*
1528 * Start processing the received transport IDs using the
1529 * receiving I_T Nexus portal's fabric dependent methods to
1530 * obtain the SCSI Initiator Port/Device Identifiers.
1531 */
1532 ptr = &buf[28];
1533
1534 while (tpdl > 0) {
1535 proto_ident = (ptr[0] & 0x0f);
1536 dest_tpg = NULL;
1537
1538 spin_lock(&dev->se_port_lock);
1539 list_for_each_entry(tmp_port, &dev->dev_sep_list, sep_list) {
1540 tmp_tpg = tmp_port->sep_tpg;
Andy Grover6708bb22011-06-08 10:36:43 -07001541 if (!tmp_tpg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001542 continue;
Andy Grovere3d6f902011-07-19 08:55:10 +00001543 tmp_tf_ops = tmp_tpg->se_tpg_tfo;
Andy Grover6708bb22011-06-08 10:36:43 -07001544 if (!tmp_tf_ops)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001545 continue;
Andy Grover6708bb22011-06-08 10:36:43 -07001546 if (!tmp_tf_ops->get_fabric_proto_ident ||
1547 !tmp_tf_ops->tpg_parse_pr_out_transport_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001548 continue;
1549 /*
1550 * Look for the matching proto_ident provided by
1551 * the received TransportID
1552 */
1553 tmp_proto_ident = tmp_tf_ops->get_fabric_proto_ident(tmp_tpg);
1554 if (tmp_proto_ident != proto_ident)
1555 continue;
1556 dest_rtpi = tmp_port->sep_rtpi;
1557
1558 i_str = tmp_tf_ops->tpg_parse_pr_out_transport_id(
1559 tmp_tpg, (const char *)ptr, &tid_len,
1560 &iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07001561 if (!i_str)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001562 continue;
1563
1564 atomic_inc(&tmp_tpg->tpg_pr_ref_count);
1565 smp_mb__after_atomic_inc();
1566 spin_unlock(&dev->se_port_lock);
1567
Christoph Hellwigde103c92012-11-06 12:24:09 -08001568 if (core_scsi3_tpg_depend_item(tmp_tpg)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001569 pr_err(" core_scsi3_tpg_depend_item()"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001570 " for tmp_tpg\n");
1571 atomic_dec(&tmp_tpg->tpg_pr_ref_count);
1572 smp_mb__after_atomic_dec();
Christoph Hellwigde103c92012-11-06 12:24:09 -08001573 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1574 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001575 }
1576 /*
Masanari Iida35d1efe2012-08-16 22:43:13 +09001577 * Locate the destination initiator ACL to be registered
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001578 * from the decoded fabric module specific TransportID
1579 * at *i_str.
1580 */
Roland Dreier28638882011-08-16 09:40:01 -07001581 spin_lock_irq(&tmp_tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001582 dest_node_acl = __core_tpg_get_initiator_node_acl(
1583 tmp_tpg, i_str);
1584 if (dest_node_acl) {
1585 atomic_inc(&dest_node_acl->acl_pr_ref_count);
1586 smp_mb__after_atomic_inc();
1587 }
Roland Dreier28638882011-08-16 09:40:01 -07001588 spin_unlock_irq(&tmp_tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001589
Andy Grover6708bb22011-06-08 10:36:43 -07001590 if (!dest_node_acl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001591 core_scsi3_tpg_undepend_item(tmp_tpg);
1592 spin_lock(&dev->se_port_lock);
1593 continue;
1594 }
1595
Christoph Hellwigde103c92012-11-06 12:24:09 -08001596 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001597 pr_err("configfs_depend_item() failed"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001598 " for dest_node_acl->acl_group\n");
1599 atomic_dec(&dest_node_acl->acl_pr_ref_count);
1600 smp_mb__after_atomic_dec();
1601 core_scsi3_tpg_undepend_item(tmp_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001602 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1603 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001604 }
1605
1606 dest_tpg = tmp_tpg;
Andy Grover6708bb22011-06-08 10:36:43 -07001607 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001608 " %s Port RTPI: %hu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001609 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001610 dest_node_acl->initiatorname, dest_rtpi);
1611
1612 spin_lock(&dev->se_port_lock);
1613 break;
1614 }
1615 spin_unlock(&dev->se_port_lock);
1616
Andy Grover6708bb22011-06-08 10:36:43 -07001617 if (!dest_tpg) {
1618 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001619 " dest_tpg\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08001620 ret = TCM_INVALID_PARAMETER_LIST;
1621 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001622 }
Andy Grover8b1e1242012-04-03 15:51:12 -07001623
Andy Grover6708bb22011-06-08 10:36:43 -07001624 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001625 " tid_len: %d for %s + %s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001626 dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001627 tpdl, tid_len, i_str, iport_ptr);
Andy Grover8b1e1242012-04-03 15:51:12 -07001628
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001629 if (tid_len > tpdl) {
Andy Grover6708bb22011-06-08 10:36:43 -07001630 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001631 " %u for Transport ID: %s\n", tid_len, ptr);
1632 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1633 core_scsi3_tpg_undepend_item(dest_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001634 ret = TCM_INVALID_PARAMETER_LIST;
1635 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001636 }
1637 /*
1638 * Locate the desintation struct se_dev_entry pointer for matching
1639 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1640 * Target Port.
1641 */
1642 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1643 dest_rtpi);
Andy Grover6708bb22011-06-08 10:36:43 -07001644 if (!dest_se_deve) {
1645 pr_err("Unable to locate %s dest_se_deve"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001646 " from destination RTPI: %hu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001647 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001648 dest_rtpi);
1649
1650 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1651 core_scsi3_tpg_undepend_item(dest_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001652 ret = TCM_INVALID_PARAMETER_LIST;
1653 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001654 }
1655
Christoph Hellwigde103c92012-11-06 12:24:09 -08001656 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001657 pr_err("core_scsi3_lunacl_depend_item()"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001658 " failed\n");
1659 atomic_dec(&dest_se_deve->pr_ref_count);
1660 smp_mb__after_atomic_dec();
1661 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1662 core_scsi3_tpg_undepend_item(dest_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001663 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1664 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001665 }
Andy Grover8b1e1242012-04-03 15:51:12 -07001666
Andy Grover6708bb22011-06-08 10:36:43 -07001667 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001668 " dest_se_deve mapped_lun: %u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001669 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001670 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
Andy Grover8b1e1242012-04-03 15:51:12 -07001671
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001672 /*
1673 * Skip any TransportIDs that already have a registration for
1674 * this target port.
1675 */
1676 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1677 iport_ptr);
1678 if (pr_reg_e) {
1679 core_scsi3_put_pr_reg(pr_reg_e);
1680 core_scsi3_lunacl_undepend_item(dest_se_deve);
1681 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1682 core_scsi3_tpg_undepend_item(dest_tpg);
1683 ptr += tid_len;
1684 tpdl -= tid_len;
1685 tid_len = 0;
1686 continue;
1687 }
1688 /*
1689 * Allocate a struct pr_transport_id_holder and setup
1690 * the dest_node_acl and dest_se_deve pointers for the
1691 * loop below.
1692 */
1693 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1694 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001695 if (!tidh_new) {
1696 pr_err("Unable to allocate tidh_new\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001697 core_scsi3_lunacl_undepend_item(dest_se_deve);
1698 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1699 core_scsi3_tpg_undepend_item(dest_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001700 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1701 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001702 }
1703 INIT_LIST_HEAD(&tidh_new->dest_list);
1704 tidh_new->dest_tpg = dest_tpg;
1705 tidh_new->dest_node_acl = dest_node_acl;
1706 tidh_new->dest_se_deve = dest_se_deve;
1707
1708 /*
1709 * Allocate, but do NOT add the registration for the
1710 * TransportID referenced SCSI Initiator port. This
1711 * done because of the following from spc4r17 in section
1712 * 6.14.3 wrt SPEC_I_PT:
1713 *
1714 * "If a registration fails for any initiator port (e.g., if th
1715 * logical unit does not have enough resources available to
1716 * hold the registration information), no registrations shall be
1717 * made, and the command shall be terminated with
1718 * CHECK CONDITION status."
1719 *
1720 * That means we call __core_scsi3_alloc_registration() here,
1721 * and then call __core_scsi3_add_registration() in the
1722 * 2nd loop which will never fail.
1723 */
Andy Grover5951146d2011-07-19 10:26:37 +00001724 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001725 dest_node_acl, dest_se_deve, iport_ptr,
1726 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001727 if (!dest_pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001728 core_scsi3_lunacl_undepend_item(dest_se_deve);
1729 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1730 core_scsi3_tpg_undepend_item(dest_tpg);
1731 kfree(tidh_new);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001732 ret = TCM_INVALID_PARAMETER_LIST;
1733 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001734 }
1735 tidh_new->dest_pr_reg = dest_pr_reg;
1736 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1737
1738 ptr += tid_len;
1739 tpdl -= tid_len;
1740 tid_len = 0;
1741
1742 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00001743
Andy Grover49493142012-01-16 16:57:08 -08001744 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00001745
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001746 /*
1747 * Go ahead and create a registrations from tid_dest_list for the
1748 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1749 * and dest_se_deve.
1750 *
1751 * The SA Reservation Key from the PROUT is set for the
1752 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1753 * means that the TransportID Initiator port will be
1754 * registered on all of the target ports in the SCSI target device
1755 * ALL_TG_PT=0 means the registration will only be for the
1756 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1757 * was received.
1758 */
1759 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1760 dest_tpg = tidh->dest_tpg;
1761 dest_node_acl = tidh->dest_node_acl;
1762 dest_se_deve = tidh->dest_se_deve;
1763 dest_pr_reg = tidh->dest_pr_reg;
1764 dest_local_nexus = tidh->dest_local_nexus;
1765
1766 list_del(&tidh->dest_list);
1767 kfree(tidh);
1768
1769 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07001770 core_pr_dump_initiator_port(dest_pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001771
Andy Grover5951146d2011-07-19 10:26:37 +00001772 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001773 dest_pr_reg, 0, 0);
1774
Andy Grover6708bb22011-06-08 10:36:43 -07001775 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001776 " registered Transport ID for Node: %s%s Mapped LUN:"
Andy Grovere3d6f902011-07-19 08:55:10 +00001777 " %u\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001778 dest_node_acl->initiatorname, i_buf, dest_se_deve->mapped_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001779
1780 if (dest_local_nexus)
1781 continue;
1782
1783 core_scsi3_lunacl_undepend_item(dest_se_deve);
1784 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1785 core_scsi3_tpg_undepend_item(dest_tpg);
1786 }
1787
1788 return 0;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001789out_unmap:
Andy Grover49493142012-01-16 16:57:08 -08001790 transport_kunmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001791out:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001792 /*
1793 * For the failure case, release everything from tid_dest_list
1794 * including *dest_pr_reg and the configfs dependances..
1795 */
1796 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1797 dest_tpg = tidh->dest_tpg;
1798 dest_node_acl = tidh->dest_node_acl;
1799 dest_se_deve = tidh->dest_se_deve;
1800 dest_pr_reg = tidh->dest_pr_reg;
1801 dest_local_nexus = tidh->dest_local_nexus;
1802
1803 list_del(&tidh->dest_list);
1804 kfree(tidh);
1805 /*
1806 * Release any extra ALL_TG_PT=1 registrations for
1807 * the SPEC_I_PT=1 case.
1808 */
1809 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1810 &dest_pr_reg->pr_reg_atp_list,
1811 pr_reg_atp_mem_list) {
1812 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1813 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1814 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1815 }
1816
1817 kfree(dest_pr_reg->pr_aptpl_buf);
1818 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1819
1820 if (dest_local_nexus)
1821 continue;
1822
1823 core_scsi3_lunacl_undepend_item(dest_se_deve);
1824 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1825 core_scsi3_tpg_undepend_item(dest_tpg);
1826 }
1827 return ret;
1828}
1829
Andy Grover3c8a6222013-05-16 10:40:58 -07001830static int core_scsi3_update_aptpl_buf(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001831 struct se_device *dev,
1832 unsigned char *buf,
Andy Grover0607dec2013-05-16 10:40:57 -07001833 u32 pr_aptpl_buf_len)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001834{
1835 struct se_lun *lun;
1836 struct se_portal_group *tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001837 struct t10_pr_registration *pr_reg;
1838 unsigned char tmp[512], isid_buf[32];
1839 ssize_t len = 0;
1840 int reg_count = 0;
Andy Grover3c8a6222013-05-16 10:40:58 -07001841 int ret = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001842
1843 memset(buf, 0, pr_aptpl_buf_len);
Andy Grover0607dec2013-05-16 10:40:57 -07001844
Andy Grover3c8a6222013-05-16 10:40:58 -07001845 spin_lock(&dev->dev_reservation_lock);
1846 spin_lock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001847 /*
1848 * Walk the registration list..
1849 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001850 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001851 pr_reg_list) {
1852
1853 tmp[0] = '\0';
1854 isid_buf[0] = '\0';
1855 tpg = pr_reg->pr_reg_nacl->se_tpg;
1856 lun = pr_reg->pr_reg_tg_pt_lun;
1857 /*
1858 * Write out any ISID value to APTPL metadata that was included
1859 * in the original registration.
1860 */
1861 if (pr_reg->isid_present_at_reg)
1862 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1863 pr_reg->pr_reg_isid);
1864 /*
1865 * Include special metadata if the pr_reg matches the
1866 * reservation holder.
1867 */
1868 if (dev->dev_pr_res_holder == pr_reg) {
1869 snprintf(tmp, 512, "PR_REG_START: %d"
1870 "\ninitiator_fabric=%s\n"
1871 "initiator_node=%s\n%s"
1872 "sa_res_key=%llu\n"
1873 "res_holder=1\nres_type=%02x\n"
1874 "res_scope=%02x\nres_all_tg_pt=%d\n"
1875 "mapped_lun=%u\n", reg_count,
Andy Grovere3d6f902011-07-19 08:55:10 +00001876 tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001877 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1878 pr_reg->pr_res_key, pr_reg->pr_res_type,
1879 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1880 pr_reg->pr_res_mapped_lun);
1881 } else {
1882 snprintf(tmp, 512, "PR_REG_START: %d\n"
1883 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1884 "sa_res_key=%llu\nres_holder=0\n"
1885 "res_all_tg_pt=%d\nmapped_lun=%u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001886 reg_count, tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001887 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1888 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1889 pr_reg->pr_res_mapped_lun);
1890 }
1891
Dan Carpenter60d645a2011-06-15 10:03:05 -07001892 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001893 pr_err("Unable to update renaming"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001894 " APTPL metadata\n");
Andy Grover3c8a6222013-05-16 10:40:58 -07001895 ret = -EMSGSIZE;
1896 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001897 }
1898 len += sprintf(buf+len, "%s", tmp);
1899
1900 /*
1901 * Include information about the associated SCSI target port.
1902 */
1903 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1904 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
Andy Grovere3d6f902011-07-19 08:55:10 +00001905 " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
1906 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1907 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001908 lun->lun_sep->sep_rtpi, lun->unpacked_lun, reg_count);
1909
Dan Carpenter60d645a2011-06-15 10:03:05 -07001910 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001911 pr_err("Unable to update renaming"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001912 " APTPL metadata\n");
Andy Grover3c8a6222013-05-16 10:40:58 -07001913 ret = -EMSGSIZE;
1914 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001915 }
1916 len += sprintf(buf+len, "%s", tmp);
1917 reg_count++;
1918 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001919
Andy Grover6708bb22011-06-08 10:36:43 -07001920 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001921 len += sprintf(buf+len, "No Registrations or Reservations");
1922
Andy Grover3c8a6222013-05-16 10:40:58 -07001923out:
1924 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001925 spin_unlock(&dev->dev_reservation_lock);
1926
1927 return ret;
1928}
1929
1930/*
1931 * Called with struct se_device->aptpl_file_mutex held
1932 */
1933static int __core_scsi3_write_aptpl_to_file(
1934 struct se_device *dev,
Andy Grover4dee96f2013-05-16 10:41:00 -07001935 unsigned char *buf)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001936{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001937 struct t10_wwn *wwn = &dev->t10_wwn;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001938 struct file *file;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001939 int flags = O_RDWR | O_CREAT | O_TRUNC;
1940 char path[512];
Andy Grover4dee96f2013-05-16 10:41:00 -07001941 u32 pr_aptpl_buf_len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001942 int ret;
1943
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001944 memset(path, 0, 512);
1945
Dan Carpenter60d645a2011-06-15 10:03:05 -07001946 if (strlen(&wwn->unit_serial[0]) >= 512) {
Andy Grover6708bb22011-06-08 10:36:43 -07001947 pr_err("WWN value for struct se_device does not fit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001948 " into path buffer\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001949 return -EMSGSIZE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001950 }
1951
1952 snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]);
1953 file = filp_open(path, flags, 0600);
Al Viro0e9b10a2013-02-23 15:22:43 -05001954 if (IS_ERR(file)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001955 pr_err("filp_open(%s) for APTPL metadata"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001956 " failed\n", path);
Al Viro0e9b10a2013-02-23 15:22:43 -05001957 return PTR_ERR(file);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001958 }
1959
Andy Grover4dee96f2013-05-16 10:41:00 -07001960 pr_aptpl_buf_len = (strlen(buf) + 1); /* Add extra for NULL */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001961
Al Viro0e9b10a2013-02-23 15:22:43 -05001962 ret = kernel_write(file, buf, pr_aptpl_buf_len, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001963
Al Viro0e9b10a2013-02-23 15:22:43 -05001964 if (ret < 0)
Andy Grover6708bb22011-06-08 10:36:43 -07001965 pr_debug("Error writing APTPL metadata file: %s\n", path);
Al Viro0e9b10a2013-02-23 15:22:43 -05001966 fput(file);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001967
Al Viro0e9b10a2013-02-23 15:22:43 -05001968 return ret ? -EIO : 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001969}
1970
Christoph Hellwigde103c92012-11-06 12:24:09 -08001971static int
1972core_scsi3_update_and_write_aptpl(struct se_device *dev, unsigned char *in_buf,
1973 u32 in_pr_aptpl_buf_len)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001974{
1975 unsigned char null_buf[64], *buf;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001976 int ret;
1977
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001978 /*
1979 * Can be called with a NULL pointer from PROUT service action CLEAR
1980 */
Andy Grover6708bb22011-06-08 10:36:43 -07001981 if (!in_buf) {
Andy Grover0607dec2013-05-16 10:40:57 -07001982 snprintf(null_buf, 64, "No Registrations or Reservations\n");
1983 buf = null_buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001984 } else {
Andy Grover0607dec2013-05-16 10:40:57 -07001985 ret = core_scsi3_update_aptpl_buf(dev, in_buf, in_pr_aptpl_buf_len);
1986 if (ret != 0)
1987 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001988 buf = in_buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001989 }
1990
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001991 /*
1992 * __core_scsi3_write_aptpl_to_file() will call strlen()
1993 * on the passed buf to determine pr_aptpl_buf_len.
1994 */
Andy Grover4dee96f2013-05-16 10:41:00 -07001995 return __core_scsi3_write_aptpl_to_file(dev, buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001996}
1997
Christoph Hellwigde103c92012-11-06 12:24:09 -08001998static sense_reason_t
1999core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
Andy Grover33ce6a82013-05-16 10:40:54 -07002000 int aptpl, int all_tg_pt, int spec_i_pt, enum register_type register_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002001{
Andy Grovere3d6f902011-07-19 08:55:10 +00002002 struct se_session *se_sess = cmd->se_sess;
Andy Grover5951146d2011-07-19 10:26:37 +00002003 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002004 struct se_dev_entry *se_deve;
Andy Grovere3d6f902011-07-19 08:55:10 +00002005 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002006 struct se_portal_group *se_tpg;
2007 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp, *pr_reg_e;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002008 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002009 /* Used for APTPL metadata w/ UNREGISTER */
2010 unsigned char *pr_aptpl_buf = NULL;
2011 unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
Hannes Reineckea0d50f62012-12-17 09:53:34 +01002012 sense_reason_t ret = TCM_NO_SENSE;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002013 int pr_holder = 0, type;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002014
Andy Grover6708bb22011-06-08 10:36:43 -07002015 if (!se_sess || !se_lun) {
2016 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002017 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002018 }
2019 se_tpg = se_sess->se_tpg;
Jörn Engelf2083242012-03-15 15:05:40 -04002020 se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002021
Andy Grovere3d6f902011-07-19 08:55:10 +00002022 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002023 memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +00002024 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002025 PR_REG_ISID_LEN);
2026 isid_ptr = &isid_buf[0];
2027 }
2028 /*
2029 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2030 */
2031 pr_reg_e = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002032 if (!pr_reg_e) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002033 if (res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002034 pr_warn("SPC-3 PR: Reservation Key non-zero"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002035 " for SA REGISTER, returning CONFLICT\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002036 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002037 }
2038 /*
2039 * Do nothing but return GOOD status.
2040 */
Andy Grover6708bb22011-06-08 10:36:43 -07002041 if (!sa_res_key)
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002042 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002043
Andy Grover6708bb22011-06-08 10:36:43 -07002044 if (!spec_i_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002045 /*
2046 * Perform the Service Action REGISTER on the Initiator
2047 * Port Endpoint that the PRO was received from on the
2048 * Logical Unit of the SCSI device server.
2049 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08002050 if (core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002051 se_sess->se_node_acl, se_deve, isid_ptr,
2052 sa_res_key, all_tg_pt, aptpl,
Andy Grover33ce6a82013-05-16 10:40:54 -07002053 register_type, 0)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002054 pr_err("Unable to allocate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002055 " struct t10_pr_registration\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002056 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002057 }
2058 } else {
2059 /*
2060 * Register both the Initiator port that received
2061 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2062 * TransportID from Parameter list and loop through
2063 * fabric dependent parameter list while calling
2064 * logic from of core_scsi3_alloc_registration() for
2065 * each TransportID provided SCSI Initiator Port/Device
2066 */
2067 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2068 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2069 if (ret != 0)
2070 return ret;
2071 }
2072 /*
2073 * Nothing left to do for the APTPL=0 case.
2074 */
Andy Grover6708bb22011-06-08 10:36:43 -07002075 if (!aptpl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002076 pr_tmpl->pr_aptpl_active = 0;
Andy Grover5951146d2011-07-19 10:26:37 +00002077 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002078 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002079 " REGISTER\n");
2080 return 0;
2081 }
2082 /*
2083 * Locate the newly allocated local I_T Nexus *pr_reg, and
2084 * update the APTPL metadata information using its
2085 * preallocated *pr_reg->pr_aptpl_buf.
2086 */
Andy Grover5951146d2011-07-19 10:26:37 +00002087 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002088 se_sess->se_node_acl, se_sess);
2089
Christoph Hellwigde103c92012-11-06 12:24:09 -08002090 if (core_scsi3_update_and_write_aptpl(cmd->se_dev,
Andy Grover4e529be2013-05-16 10:40:59 -07002091 pr_reg->pr_aptpl_buf, PR_APTPL_BUF_LEN)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002092 pr_tmpl->pr_aptpl_active = 1;
Andy Grover6708bb22011-06-08 10:36:43 -07002093 pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002094 }
2095
Christoph Hellwigde103c92012-11-06 12:24:09 -08002096 goto out_put_pr_reg;
2097 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002098
Christoph Hellwigde103c92012-11-06 12:24:09 -08002099 /*
2100 * Locate the existing *pr_reg via struct se_node_acl pointers
2101 */
2102 pr_reg = pr_reg_e;
2103 type = pr_reg->pr_res_type;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002104
Andy Grover33ce6a82013-05-16 10:40:54 -07002105 if (register_type == REGISTER) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08002106 if (res_key != pr_reg->pr_res_key) {
2107 pr_err("SPC-3 PR REGISTER: Received"
2108 " res_key: 0x%016Lx does not match"
2109 " existing SA REGISTER res_key:"
2110 " 0x%016Lx\n", res_key,
2111 pr_reg->pr_res_key);
2112 ret = TCM_RESERVATION_CONFLICT;
2113 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002114 }
2115 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08002116
2117 if (spec_i_pt) {
Andy Grover1f070cc2013-05-16 10:40:56 -07002118 pr_err("SPC-3 PR REGISTER: SPEC_I_PT"
2119 " set on a registered nexus\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002120 ret = TCM_INVALID_PARAMETER_LIST;
2121 goto out_put_pr_reg;
2122 }
2123
2124 /*
2125 * An existing ALL_TG_PT=1 registration being released
2126 * must also set ALL_TG_PT=1 in the incoming PROUT.
2127 */
Andy Grover1f070cc2013-05-16 10:40:56 -07002128 if (pr_reg->pr_reg_all_tg_pt && !all_tg_pt) {
2129 pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1"
Christoph Hellwigde103c92012-11-06 12:24:09 -08002130 " registration exists, but ALL_TG_PT=1 bit not"
2131 " present in received PROUT\n");
2132 ret = TCM_INVALID_CDB_FIELD;
2133 goto out_put_pr_reg;
2134 }
2135
2136 /*
2137 * Allocate APTPL metadata buffer used for UNREGISTER ops
2138 */
2139 if (aptpl) {
Andy Grover4e529be2013-05-16 10:40:59 -07002140 pr_aptpl_buf = kzalloc(PR_APTPL_BUF_LEN, GFP_KERNEL);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002141 if (!pr_aptpl_buf) {
2142 pr_err("Unable to allocate"
2143 " pr_aptpl_buf\n");
2144 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2145 goto out_put_pr_reg;
2146 }
2147 }
2148
2149 /*
2150 * sa_res_key=0 Unregister Reservation Key for registered I_T
2151 * Nexus sa_res_key=1 Change Reservation Key for registered I_T
2152 * Nexus.
2153 */
2154 if (!sa_res_key) {
2155 pr_holder = core_scsi3_check_implict_release(
2156 cmd->se_dev, pr_reg);
2157 if (pr_holder < 0) {
2158 kfree(pr_aptpl_buf);
2159 ret = TCM_RESERVATION_CONFLICT;
2160 goto out_put_pr_reg;
2161 }
2162
2163 spin_lock(&pr_tmpl->registration_lock);
2164 /*
2165 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2166 * and matching pr_res_key.
2167 */
2168 if (pr_reg->pr_reg_all_tg_pt) {
2169 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2170 &pr_tmpl->registration_list,
2171 pr_reg_list) {
2172
2173 if (!pr_reg_p->pr_reg_all_tg_pt)
2174 continue;
2175 if (pr_reg_p->pr_res_key != res_key)
2176 continue;
2177 if (pr_reg == pr_reg_p)
2178 continue;
2179 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2180 pr_reg_p->pr_reg_nacl->initiatorname))
2181 continue;
2182
2183 __core_scsi3_free_registration(dev,
2184 pr_reg_p, NULL, 0);
2185 }
2186 }
2187
2188 /*
2189 * Release the calling I_T Nexus registration now..
2190 */
2191 __core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1);
2192
2193 /*
2194 * From spc4r17, section 5.7.11.3 Unregistering
2195 *
2196 * If the persistent reservation is a registrants only
2197 * type, the device server shall establish a unit
2198 * attention condition for the initiator port associated
2199 * with every registered I_T nexus except for the I_T
2200 * nexus on which the PERSISTENT RESERVE OUT command was
2201 * received, with the additional sense code set to
2202 * RESERVATIONS RELEASED.
2203 */
2204 if (pr_holder &&
2205 (type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY ||
2206 type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) {
2207 list_for_each_entry(pr_reg_p,
2208 &pr_tmpl->registration_list,
2209 pr_reg_list) {
2210
2211 core_scsi3_ua_allocate(
2212 pr_reg_p->pr_reg_nacl,
2213 pr_reg_p->pr_res_mapped_lun,
2214 0x2A,
2215 ASCQ_2AH_RESERVATIONS_RELEASED);
2216 }
2217 }
2218 spin_unlock(&pr_tmpl->registration_lock);
2219
2220 if (!aptpl) {
2221 pr_tmpl->pr_aptpl_active = 0;
2222 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2223 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2224 " for UNREGISTER\n");
2225 return 0;
2226 }
2227
Andy Grover4e529be2013-05-16 10:40:59 -07002228 if (!core_scsi3_update_and_write_aptpl(dev, pr_aptpl_buf, PR_APTPL_BUF_LEN)) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08002229 pr_tmpl->pr_aptpl_active = 1;
2230 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2231 " for UNREGISTER\n");
2232 }
2233
2234 goto out_free_aptpl_buf;
2235 }
2236
2237 /*
2238 * Increment PRgeneration counter for struct se_device"
2239 * upon a successful REGISTER, see spc4r17 section 6.3.2
2240 * READ_KEYS service action.
2241 */
2242 pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev);
2243 pr_reg->pr_res_key = sa_res_key;
2244 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2245 " Key for %s to: 0x%016Lx PRgeneration:"
2246 " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
Andy Grover33ce6a82013-05-16 10:40:54 -07002247 (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? "_AND_IGNORE_EXISTING_KEY" : "",
Christoph Hellwigde103c92012-11-06 12:24:09 -08002248 pr_reg->pr_reg_nacl->initiatorname,
2249 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2250
2251 if (!aptpl) {
2252 pr_tmpl->pr_aptpl_active = 0;
2253 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2254 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2255 " for REGISTER\n");
2256 ret = 0;
2257 goto out_put_pr_reg;
2258 }
2259
Andy Grover4e529be2013-05-16 10:40:59 -07002260 if (!core_scsi3_update_and_write_aptpl(dev, pr_aptpl_buf, PR_APTPL_BUF_LEN)) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08002261 pr_tmpl->pr_aptpl_active = 1;
2262 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2263 " for REGISTER\n");
2264 }
2265
2266out_free_aptpl_buf:
2267 kfree(pr_aptpl_buf);
2268 ret = 0;
2269out_put_pr_reg:
2270 core_scsi3_put_pr_reg(pr_reg);
2271 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002272}
2273
2274unsigned char *core_scsi3_pr_dump_type(int type)
2275{
2276 switch (type) {
2277 case PR_TYPE_WRITE_EXCLUSIVE:
2278 return "Write Exclusive Access";
2279 case PR_TYPE_EXCLUSIVE_ACCESS:
2280 return "Exclusive Access";
2281 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2282 return "Write Exclusive Access, Registrants Only";
2283 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2284 return "Exclusive Access, Registrants Only";
2285 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2286 return "Write Exclusive Access, All Registrants";
2287 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2288 return "Exclusive Access, All Registrants";
2289 default:
2290 break;
2291 }
2292
2293 return "Unknown SPC-3 PR Type";
2294}
2295
Christoph Hellwigde103c92012-11-06 12:24:09 -08002296static sense_reason_t
2297core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002298{
Christoph Hellwigde103c92012-11-06 12:24:09 -08002299 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00002300 struct se_session *se_sess = cmd->se_sess;
Andy Grovere3d6f902011-07-19 08:55:10 +00002301 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002302 struct t10_pr_registration *pr_reg, *pr_res_holder;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002303 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002304 char i_buf[PR_REG_ISID_ID_LEN];
Christoph Hellwigde103c92012-11-06 12:24:09 -08002305 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002306
2307 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2308
Andy Grover6708bb22011-06-08 10:36:43 -07002309 if (!se_sess || !se_lun) {
2310 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002311 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002312 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002313 /*
2314 * Locate the existing *pr_reg via struct se_node_acl pointers
2315 */
Andy Grover5951146d2011-07-19 10:26:37 +00002316 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002317 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002318 if (!pr_reg) {
2319 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002320 " PR_REGISTERED *pr_reg for RESERVE\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002321 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002322 }
2323 /*
2324 * From spc4r17 Section 5.7.9: Reserving:
2325 *
2326 * An application client creates a persistent reservation by issuing
2327 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2328 * a registered I_T nexus with the following parameters:
2329 * a) RESERVATION KEY set to the value of the reservation key that is
2330 * registered with the logical unit for the I_T nexus; and
2331 */
2332 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002333 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002334 " does not match existing SA REGISTER res_key:"
2335 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002336 ret = TCM_RESERVATION_CONFLICT;
2337 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002338 }
2339 /*
2340 * From spc4r17 Section 5.7.9: Reserving:
2341 *
2342 * From above:
2343 * b) TYPE field and SCOPE field set to the persistent reservation
2344 * being created.
2345 *
2346 * Only one persistent reservation is allowed at a time per logical unit
2347 * and that persistent reservation has a scope of LU_SCOPE.
2348 */
2349 if (scope != PR_SCOPE_LU_SCOPE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002350 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002351 ret = TCM_INVALID_PARAMETER_LIST;
2352 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002353 }
2354 /*
2355 * See if we have an existing PR reservation holder pointer at
2356 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2357 * *pr_res_holder.
2358 */
2359 spin_lock(&dev->dev_reservation_lock);
2360 pr_res_holder = dev->dev_pr_res_holder;
Andy Groveree1b1b92012-07-12 17:34:54 -07002361 if (pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002362 /*
2363 * From spc4r17 Section 5.7.9: Reserving:
2364 *
2365 * If the device server receives a PERSISTENT RESERVE OUT
2366 * command from an I_T nexus other than a persistent reservation
2367 * holder (see 5.7.10) that attempts to create a persistent
2368 * reservation when a persistent reservation already exists for
2369 * the logical unit, then the command shall be completed with
2370 * RESERVATION CONFLICT status.
2371 */
2372 if (pr_res_holder != pr_reg) {
2373 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002374 pr_err("SPC-3 PR: Attempted RESERVE from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002375 " [%s]: %s while reservation already held by"
2376 " [%s]: %s, returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002377 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002378 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002379 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002380 pr_res_holder->pr_reg_nacl->initiatorname);
2381
2382 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002383 ret = TCM_RESERVATION_CONFLICT;
2384 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002385 }
2386 /*
2387 * From spc4r17 Section 5.7.9: Reserving:
2388 *
2389 * If a persistent reservation holder attempts to modify the
2390 * type or scope of an existing persistent reservation, the
2391 * command shall be completed with RESERVATION CONFLICT status.
2392 */
2393 if ((pr_res_holder->pr_res_type != type) ||
2394 (pr_res_holder->pr_res_scope != scope)) {
2395 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002396 pr_err("SPC-3 PR: Attempted RESERVE from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002397 " [%s]: %s trying to change TYPE and/or SCOPE,"
2398 " while reservation already held by [%s]: %s,"
2399 " returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002400 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002401 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002402 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002403 pr_res_holder->pr_reg_nacl->initiatorname);
2404
2405 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002406 ret = TCM_RESERVATION_CONFLICT;
2407 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002408 }
2409 /*
2410 * From spc4r17 Section 5.7.9: Reserving:
2411 *
2412 * If the device server receives a PERSISTENT RESERVE OUT
2413 * command with RESERVE service action where the TYPE field and
2414 * the SCOPE field contain the same values as the existing type
2415 * and scope from a persistent reservation holder, it shall not
2416 * make any change to the existing persistent reservation and
2417 * shall completethe command with GOOD status.
2418 */
2419 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002420 ret = 0;
2421 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002422 }
2423 /*
2424 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2425 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2426 */
2427 pr_reg->pr_res_scope = scope;
2428 pr_reg->pr_res_type = type;
2429 pr_reg->pr_res_holder = 1;
2430 dev->dev_pr_res_holder = pr_reg;
Andy Groverd2843c12013-05-16 10:40:55 -07002431 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002432
Andy Grover6708bb22011-06-08 10:36:43 -07002433 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002434 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002435 cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002436 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002437 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002438 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002439 se_sess->se_node_acl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07002440 i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002441 spin_unlock(&dev->dev_reservation_lock);
2442
2443 if (pr_tmpl->pr_aptpl_active) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08002444 if (!core_scsi3_update_and_write_aptpl(cmd->se_dev,
Andy Grover4e529be2013-05-16 10:40:59 -07002445 pr_reg->pr_aptpl_buf, PR_APTPL_BUF_LEN)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002446 pr_debug("SPC-3 PR: Updated APTPL metadata"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002447 " for RESERVE\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002448 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002449 }
2450
Christoph Hellwigde103c92012-11-06 12:24:09 -08002451 ret = 0;
2452out_put_pr_reg:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002453 core_scsi3_put_pr_reg(pr_reg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002454 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002455}
2456
Christoph Hellwigde103c92012-11-06 12:24:09 -08002457static sense_reason_t
2458core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope,
2459 u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002460{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002461 switch (type) {
2462 case PR_TYPE_WRITE_EXCLUSIVE:
2463 case PR_TYPE_EXCLUSIVE_ACCESS:
2464 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2465 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2466 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2467 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
Christoph Hellwigde103c92012-11-06 12:24:09 -08002468 return core_scsi3_pro_reserve(cmd, type, scope, res_key);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002469 default:
Andy Grover6708bb22011-06-08 10:36:43 -07002470 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002471 " 0x%02x\n", type);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002472 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002473 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002474}
2475
2476/*
2477 * Called with struct se_device->dev_reservation_lock held.
2478 */
2479static void __core_scsi3_complete_pro_release(
2480 struct se_device *dev,
2481 struct se_node_acl *se_nacl,
2482 struct t10_pr_registration *pr_reg,
2483 int explict)
2484{
2485 struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2486 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002487
2488 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07002489 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002490 /*
2491 * Go ahead and release the current PR reservation holder.
2492 */
2493 dev->dev_pr_res_holder = NULL;
2494
Andy Grover6708bb22011-06-08 10:36:43 -07002495 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002496 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2497 tfo->get_fabric_name(), (explict) ? "explict" : "implict",
2498 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
2499 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002500 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002501 tfo->get_fabric_name(), se_nacl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07002502 i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002503 /*
2504 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2505 */
2506 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2507}
2508
Christoph Hellwigde103c92012-11-06 12:24:09 -08002509static sense_reason_t
2510core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope,
2511 u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002512{
2513 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00002514 struct se_session *se_sess = cmd->se_sess;
2515 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002516 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002517 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002518 int all_reg = 0;
2519 sense_reason_t ret = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002520
Andy Grover6708bb22011-06-08 10:36:43 -07002521 if (!se_sess || !se_lun) {
2522 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002523 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002524 }
2525 /*
2526 * Locate the existing *pr_reg via struct se_node_acl pointers
2527 */
2528 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002529 if (!pr_reg) {
2530 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002531 " PR_REGISTERED *pr_reg for RELEASE\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002532 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002533 }
2534 /*
2535 * From spc4r17 Section 5.7.11.2 Releasing:
2536 *
2537 * If there is no persistent reservation or in response to a persistent
2538 * reservation release request from a registered I_T nexus that is not a
2539 * persistent reservation holder (see 5.7.10), the device server shall
2540 * do the following:
2541 *
2542 * a) Not release the persistent reservation, if any;
2543 * b) Not remove any registrations; and
2544 * c) Complete the command with GOOD status.
2545 */
2546 spin_lock(&dev->dev_reservation_lock);
2547 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07002548 if (!pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002549 /*
2550 * No persistent reservation, return GOOD status.
2551 */
2552 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerbb7a8c82012-11-06 15:46:25 -08002553 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002554 }
2555 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2556 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
2557 all_reg = 1;
2558
2559 if ((all_reg == 0) && (pr_res_holder != pr_reg)) {
2560 /*
2561 * Non 'All Registrants' PR Type cases..
2562 * Release request from a registered I_T nexus that is not a
2563 * persistent reservation holder. return GOOD status.
2564 */
2565 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002566 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002567 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08002568
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002569 /*
2570 * From spc4r17 Section 5.7.11.2 Releasing:
2571 *
2572 * Only the persistent reservation holder (see 5.7.10) is allowed to
2573 * release a persistent reservation.
2574 *
2575 * An application client releases the persistent reservation by issuing
2576 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2577 * an I_T nexus that is a persistent reservation holder with the
2578 * following parameters:
2579 *
2580 * a) RESERVATION KEY field set to the value of the reservation key
2581 * that is registered with the logical unit for the I_T nexus;
2582 */
2583 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002584 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002585 " does not match existing SA REGISTER res_key:"
2586 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2587 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002588 ret = TCM_RESERVATION_CONFLICT;
2589 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002590 }
2591 /*
2592 * From spc4r17 Section 5.7.11.2 Releasing and above:
2593 *
2594 * b) TYPE field and SCOPE field set to match the persistent
2595 * reservation being released.
2596 */
2597 if ((pr_res_holder->pr_res_type != type) ||
2598 (pr_res_holder->pr_res_scope != scope)) {
2599 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002600 pr_err("SPC-3 PR RELEASE: Attempted to release"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002601 " reservation from [%s]: %s with different TYPE "
2602 "and/or SCOPE while reservation already held by"
2603 " [%s]: %s, returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002604 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002605 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002606 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002607 pr_res_holder->pr_reg_nacl->initiatorname);
2608
2609 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002610 ret = TCM_RESERVATION_CONFLICT;
2611 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002612 }
2613 /*
2614 * In response to a persistent reservation release request from the
2615 * persistent reservation holder the device server shall perform a
2616 * release by doing the following as an uninterrupted series of actions:
2617 * a) Release the persistent reservation;
2618 * b) Not remove any registration(s);
2619 * c) If the released persistent reservation is a registrants only type
2620 * or all registrants type persistent reservation,
2621 * the device server shall establish a unit attention condition for
2622 * the initiator port associated with every regis-
2623 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2624 * RESERVE OUT command with RELEASE service action was received,
2625 * with the additional sense code set to RESERVATIONS RELEASED; and
2626 * d) If the persistent reservation is of any other type, the device
2627 * server shall not establish a unit attention condition.
2628 */
2629 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
2630 pr_reg, 1);
2631
2632 spin_unlock(&dev->dev_reservation_lock);
2633
2634 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2635 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2636 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2637 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2638 /*
2639 * If no UNIT ATTENTION conditions will be established for
2640 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2641 * go ahead and check for APTPL=1 update+write below
2642 */
2643 goto write_aptpl;
2644 }
2645
2646 spin_lock(&pr_tmpl->registration_lock);
2647 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2648 pr_reg_list) {
2649 /*
2650 * Do not establish a UNIT ATTENTION condition
2651 * for the calling I_T Nexus
2652 */
2653 if (pr_reg_p == pr_reg)
2654 continue;
2655
2656 core_scsi3_ua_allocate(pr_reg_p->pr_reg_nacl,
2657 pr_reg_p->pr_res_mapped_lun,
2658 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2659 }
2660 spin_unlock(&pr_tmpl->registration_lock);
2661
2662write_aptpl:
2663 if (pr_tmpl->pr_aptpl_active) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08002664 if (!core_scsi3_update_and_write_aptpl(cmd->se_dev,
Andy Grover4e529be2013-05-16 10:40:59 -07002665 pr_reg->pr_aptpl_buf, PR_APTPL_BUF_LEN)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002666 pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002667 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002668 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08002669out_put_pr_reg:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002670 core_scsi3_put_pr_reg(pr_reg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002671 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002672}
2673
Christoph Hellwigde103c92012-11-06 12:24:09 -08002674static sense_reason_t
2675core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002676{
2677 struct se_device *dev = cmd->se_dev;
2678 struct se_node_acl *pr_reg_nacl;
Andy Grovere3d6f902011-07-19 08:55:10 +00002679 struct se_session *se_sess = cmd->se_sess;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002680 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002681 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2682 u32 pr_res_mapped_lun = 0;
2683 int calling_it_nexus = 0;
2684 /*
2685 * Locate the existing *pr_reg via struct se_node_acl pointers
2686 */
Andy Grover5951146d2011-07-19 10:26:37 +00002687 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002688 se_sess->se_node_acl, se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002689 if (!pr_reg_n) {
2690 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002691 " PR_REGISTERED *pr_reg for CLEAR\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002692 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002693 }
2694 /*
2695 * From spc4r17 section 5.7.11.6, Clearing:
2696 *
2697 * Any application client may release the persistent reservation and
2698 * remove all registrations from a device server by issuing a
2699 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2700 * registered I_T nexus with the following parameter:
2701 *
2702 * a) RESERVATION KEY field set to the value of the reservation key
2703 * that is registered with the logical unit for the I_T nexus.
2704 */
2705 if (res_key != pr_reg_n->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002706 pr_err("SPC-3 PR REGISTER: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002707 " res_key: 0x%016Lx does not match"
2708 " existing SA REGISTER res_key:"
2709 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2710 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002711 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002712 }
2713 /*
2714 * a) Release the persistent reservation, if any;
2715 */
2716 spin_lock(&dev->dev_reservation_lock);
2717 pr_res_holder = dev->dev_pr_res_holder;
2718 if (pr_res_holder) {
2719 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2720 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
2721 pr_res_holder, 0);
2722 }
2723 spin_unlock(&dev->dev_reservation_lock);
2724 /*
2725 * b) Remove all registration(s) (see spc4r17 5.7.7);
2726 */
2727 spin_lock(&pr_tmpl->registration_lock);
2728 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2729 &pr_tmpl->registration_list, pr_reg_list) {
2730
2731 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2732 pr_reg_nacl = pr_reg->pr_reg_nacl;
2733 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2734 __core_scsi3_free_registration(dev, pr_reg, NULL,
2735 calling_it_nexus);
2736 /*
2737 * e) Establish a unit attention condition for the initiator
2738 * port associated with every registered I_T nexus other
2739 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2740 * command with CLEAR service action was received, with the
2741 * additional sense code set to RESERVATIONS PREEMPTED.
2742 */
Andy Grover6708bb22011-06-08 10:36:43 -07002743 if (!calling_it_nexus)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002744 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun,
2745 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2746 }
2747 spin_unlock(&pr_tmpl->registration_lock);
2748
Andy Grover6708bb22011-06-08 10:36:43 -07002749 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002750 cmd->se_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002751
2752 if (pr_tmpl->pr_aptpl_active) {
Andy Grover5951146d2011-07-19 10:26:37 +00002753 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002754 pr_debug("SPC-3 PR: Updated APTPL metadata"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002755 " for CLEAR\n");
2756 }
2757
2758 core_scsi3_pr_generation(dev);
2759 return 0;
2760}
2761
2762/*
2763 * Called with struct se_device->dev_reservation_lock held.
2764 */
2765static void __core_scsi3_complete_pro_preempt(
2766 struct se_device *dev,
2767 struct t10_pr_registration *pr_reg,
2768 struct list_head *preempt_and_abort_list,
2769 int type,
2770 int scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07002771 enum preempt_type preempt_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002772{
2773 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
2774 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2775 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002776
2777 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07002778 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002779 /*
2780 * Do an implict RELEASE of the existing reservation.
2781 */
2782 if (dev->dev_pr_res_holder)
2783 __core_scsi3_complete_pro_release(dev, nacl,
2784 dev->dev_pr_res_holder, 0);
2785
2786 dev->dev_pr_res_holder = pr_reg;
2787 pr_reg->pr_res_holder = 1;
2788 pr_reg->pr_res_type = type;
2789 pr_reg->pr_res_scope = scope;
2790
Andy Grover6708bb22011-06-08 10:36:43 -07002791 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002792 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
Andy Grover33ce6a82013-05-16 10:40:54 -07002793 tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002794 core_scsi3_pr_dump_type(type),
2795 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002796 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
Andy Grover33ce6a82013-05-16 10:40:54 -07002797 tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
Andy Groverd2843c12013-05-16 10:40:55 -07002798 nacl->initiatorname, i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002799 /*
2800 * For PREEMPT_AND_ABORT, add the preempting reservation's
2801 * struct t10_pr_registration to the list that will be compared
2802 * against received CDBs..
2803 */
2804 if (preempt_and_abort_list)
2805 list_add_tail(&pr_reg->pr_reg_abort_list,
2806 preempt_and_abort_list);
2807}
2808
2809static void core_scsi3_release_preempt_and_abort(
2810 struct list_head *preempt_and_abort_list,
2811 struct t10_pr_registration *pr_reg_holder)
2812{
2813 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2814
2815 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2816 pr_reg_abort_list) {
2817
2818 list_del(&pr_reg->pr_reg_abort_list);
2819 if (pr_reg_holder == pr_reg)
2820 continue;
2821 if (pr_reg->pr_res_holder) {
Andy Grover6708bb22011-06-08 10:36:43 -07002822 pr_warn("pr_reg->pr_res_holder still set\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002823 continue;
2824 }
2825
2826 pr_reg->pr_reg_deve = NULL;
2827 pr_reg->pr_reg_nacl = NULL;
2828 kfree(pr_reg->pr_aptpl_buf);
2829 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2830 }
2831}
2832
Christoph Hellwigde103c92012-11-06 12:24:09 -08002833static sense_reason_t
2834core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key,
Andy Grover33ce6a82013-05-16 10:40:54 -07002835 u64 sa_res_key, enum preempt_type preempt_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002836{
Andy Grover5951146d2011-07-19 10:26:37 +00002837 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002838 struct se_node_acl *pr_reg_nacl;
Andy Grovere3d6f902011-07-19 08:55:10 +00002839 struct se_session *se_sess = cmd->se_sess;
Roland Dreierd0f474e2012-01-12 10:41:18 -08002840 LIST_HEAD(preempt_and_abort_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002841 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002842 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002843 u32 pr_res_mapped_lun = 0;
2844 int all_reg = 0, calling_it_nexus = 0, released_regs = 0;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002845 int prh_type = 0, prh_scope = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002846
Christoph Hellwigde103c92012-11-06 12:24:09 -08002847 if (!se_sess)
2848 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002849
Andy Grover5951146d2011-07-19 10:26:37 +00002850 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002851 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002852 if (!pr_reg_n) {
2853 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002854 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
Andy Grover33ce6a82013-05-16 10:40:54 -07002855 (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002856 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002857 }
2858 if (pr_reg_n->pr_res_key != res_key) {
2859 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002860 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002861 }
2862 if (scope != PR_SCOPE_LU_SCOPE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002863 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002864 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002865 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002866 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002867
2868 spin_lock(&dev->dev_reservation_lock);
2869 pr_res_holder = dev->dev_pr_res_holder;
2870 if (pr_res_holder &&
2871 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2872 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
2873 all_reg = 1;
2874
Andy Grover6708bb22011-06-08 10:36:43 -07002875 if (!all_reg && !sa_res_key) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002876 spin_unlock(&dev->dev_reservation_lock);
2877 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002878 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002879 }
2880 /*
2881 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
2882 *
2883 * If the SERVICE ACTION RESERVATION KEY field does not identify a
2884 * persistent reservation holder or there is no persistent reservation
2885 * holder (i.e., there is no persistent reservation), then the device
2886 * server shall perform a preempt by doing the following in an
2887 * uninterrupted series of actions. (See below..)
2888 */
Andy Grover6708bb22011-06-08 10:36:43 -07002889 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002890 /*
2891 * No existing or SA Reservation Key matching reservations..
2892 *
2893 * PROUT SA PREEMPT with All Registrant type reservations are
2894 * allowed to be processed without a matching SA Reservation Key
2895 */
2896 spin_lock(&pr_tmpl->registration_lock);
2897 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2898 &pr_tmpl->registration_list, pr_reg_list) {
2899 /*
2900 * Removing of registrations in non all registrants
2901 * type reservations without a matching SA reservation
2902 * key.
2903 *
2904 * a) Remove the registrations for all I_T nexuses
2905 * specified by the SERVICE ACTION RESERVATION KEY
2906 * field;
2907 * b) Ignore the contents of the SCOPE and TYPE fields;
2908 * c) Process tasks as defined in 5.7.1; and
2909 * d) Establish a unit attention condition for the
2910 * initiator port associated with every I_T nexus
2911 * that lost its registration other than the I_T
2912 * nexus on which the PERSISTENT RESERVE OUT command
2913 * was received, with the additional sense code set
2914 * to REGISTRATIONS PREEMPTED.
2915 */
Andy Grover6708bb22011-06-08 10:36:43 -07002916 if (!all_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002917 if (pr_reg->pr_res_key != sa_res_key)
2918 continue;
2919
2920 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2921 pr_reg_nacl = pr_reg->pr_reg_nacl;
2922 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2923 __core_scsi3_free_registration(dev, pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07002924 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002925 NULL, calling_it_nexus);
2926 released_regs++;
2927 } else {
2928 /*
2929 * Case for any existing all registrants type
2930 * reservation, follow logic in spc4r17 section
2931 * 5.7.11.4 Preempting, Table 52 and Figure 7.
2932 *
2933 * For a ZERO SA Reservation key, release
2934 * all other registrations and do an implict
2935 * release of active persistent reservation.
2936 *
2937 * For a non-ZERO SA Reservation key, only
2938 * release the matching reservation key from
2939 * registrations.
2940 */
2941 if ((sa_res_key) &&
2942 (pr_reg->pr_res_key != sa_res_key))
2943 continue;
2944
2945 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2946 if (calling_it_nexus)
2947 continue;
2948
2949 pr_reg_nacl = pr_reg->pr_reg_nacl;
2950 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2951 __core_scsi3_free_registration(dev, pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07002952 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002953 NULL, 0);
2954 released_regs++;
2955 }
Andy Grover6708bb22011-06-08 10:36:43 -07002956 if (!calling_it_nexus)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002957 core_scsi3_ua_allocate(pr_reg_nacl,
2958 pr_res_mapped_lun, 0x2A,
Marco Sanvido9e08e342012-01-03 17:12:57 -08002959 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002960 }
2961 spin_unlock(&pr_tmpl->registration_lock);
2962 /*
2963 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
2964 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
2965 * RESERVATION KEY field to a value that does not match any
2966 * registered reservation key, then the device server shall
2967 * complete the command with RESERVATION CONFLICT status.
2968 */
Andy Grover6708bb22011-06-08 10:36:43 -07002969 if (!released_regs) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002970 spin_unlock(&dev->dev_reservation_lock);
2971 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002972 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002973 }
2974 /*
2975 * For an existing all registrants type reservation
2976 * with a zero SA rservation key, preempt the existing
2977 * reservation with the new PR type and scope.
2978 */
2979 if (pr_res_holder && all_reg && !(sa_res_key)) {
2980 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
Andy Grover33ce6a82013-05-16 10:40:54 -07002981 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
2982 type, scope, preempt_type);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002983
Andy Grover33ce6a82013-05-16 10:40:54 -07002984 if (preempt_type == PREEMPT_AND_ABORT)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002985 core_scsi3_release_preempt_and_abort(
2986 &preempt_and_abort_list, pr_reg_n);
2987 }
2988 spin_unlock(&dev->dev_reservation_lock);
2989
2990 if (pr_tmpl->pr_aptpl_active) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08002991 if (!core_scsi3_update_and_write_aptpl(cmd->se_dev,
Andy Grover4e529be2013-05-16 10:40:59 -07002992 pr_reg_n->pr_aptpl_buf, PR_APTPL_BUF_LEN)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002993 pr_debug("SPC-3 PR: Updated APTPL"
Andy Grover33ce6a82013-05-16 10:40:54 -07002994 " metadata for PREEMPT%s\n", (preempt_type == PREEMPT_AND_ABORT) ?
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002995 "_AND_ABORT" : "");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002996 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002997 }
2998
2999 core_scsi3_put_pr_reg(pr_reg_n);
Andy Grover5951146d2011-07-19 10:26:37 +00003000 core_scsi3_pr_generation(cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003001 return 0;
3002 }
3003 /*
3004 * The PREEMPTing SA reservation key matches that of the
3005 * existing persistent reservation, first, we check if
3006 * we are preempting our own reservation.
3007 * From spc4r17, section 5.7.11.4.3 Preempting
3008 * persistent reservations and registration handling
3009 *
3010 * If an all registrants persistent reservation is not
3011 * present, it is not an error for the persistent
3012 * reservation holder to preempt itself (i.e., a
3013 * PERSISTENT RESERVE OUT with a PREEMPT service action
3014 * or a PREEMPT AND ABORT service action with the
3015 * SERVICE ACTION RESERVATION KEY value equal to the
3016 * persistent reservation holder's reservation key that
3017 * is received from the persistent reservation holder).
3018 * In that case, the device server shall establish the
3019 * new persistent reservation and maintain the
3020 * registration.
3021 */
3022 prh_type = pr_res_holder->pr_res_type;
3023 prh_scope = pr_res_holder->pr_res_scope;
3024 /*
3025 * If the SERVICE ACTION RESERVATION KEY field identifies a
3026 * persistent reservation holder (see 5.7.10), the device
3027 * server shall perform a preempt by doing the following as
3028 * an uninterrupted series of actions:
3029 *
3030 * a) Release the persistent reservation for the holder
3031 * identified by the SERVICE ACTION RESERVATION KEY field;
3032 */
3033 if (pr_reg_n != pr_res_holder)
3034 __core_scsi3_complete_pro_release(dev,
3035 pr_res_holder->pr_reg_nacl,
3036 dev->dev_pr_res_holder, 0);
3037 /*
3038 * b) Remove the registrations for all I_T nexuses identified
3039 * by the SERVICE ACTION RESERVATION KEY field, except the
3040 * I_T nexus that is being used for the PERSISTENT RESERVE
3041 * OUT command. If an all registrants persistent reservation
3042 * is present and the SERVICE ACTION RESERVATION KEY field
3043 * is set to zero, then all registrations shall be removed
3044 * except for that of the I_T nexus that is being used for
3045 * the PERSISTENT RESERVE OUT command;
3046 */
3047 spin_lock(&pr_tmpl->registration_lock);
3048 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3049 &pr_tmpl->registration_list, pr_reg_list) {
3050
3051 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3052 if (calling_it_nexus)
3053 continue;
3054
3055 if (pr_reg->pr_res_key != sa_res_key)
3056 continue;
3057
3058 pr_reg_nacl = pr_reg->pr_reg_nacl;
3059 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3060 __core_scsi3_free_registration(dev, pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07003061 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003062 calling_it_nexus);
3063 /*
3064 * e) Establish a unit attention condition for the initiator
3065 * port associated with every I_T nexus that lost its
3066 * persistent reservation and/or registration, with the
3067 * additional sense code set to REGISTRATIONS PREEMPTED;
3068 */
3069 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
Marco Sanvido9e08e342012-01-03 17:12:57 -08003070 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003071 }
3072 spin_unlock(&pr_tmpl->registration_lock);
3073 /*
3074 * c) Establish a persistent reservation for the preempting
3075 * I_T nexus using the contents of the SCOPE and TYPE fields;
3076 */
3077 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
Andy Grover33ce6a82013-05-16 10:40:54 -07003078 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3079 type, scope, preempt_type);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003080 /*
3081 * d) Process tasks as defined in 5.7.1;
3082 * e) See above..
3083 * f) If the type or scope has changed, then for every I_T nexus
3084 * whose reservation key was not removed, except for the I_T
3085 * nexus on which the PERSISTENT RESERVE OUT command was
3086 * received, the device server shall establish a unit
3087 * attention condition for the initiator port associated with
3088 * that I_T nexus, with the additional sense code set to
3089 * RESERVATIONS RELEASED. If the type or scope have not
3090 * changed, then no unit attention condition(s) shall be
3091 * established for this reason.
3092 */
3093 if ((prh_type != type) || (prh_scope != scope)) {
3094 spin_lock(&pr_tmpl->registration_lock);
3095 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3096 &pr_tmpl->registration_list, pr_reg_list) {
3097
3098 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3099 if (calling_it_nexus)
3100 continue;
3101
3102 core_scsi3_ua_allocate(pr_reg->pr_reg_nacl,
3103 pr_reg->pr_res_mapped_lun, 0x2A,
3104 ASCQ_2AH_RESERVATIONS_RELEASED);
3105 }
3106 spin_unlock(&pr_tmpl->registration_lock);
3107 }
3108 spin_unlock(&dev->dev_reservation_lock);
3109 /*
3110 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3111 * All received CDBs for the matching existing reservation and
3112 * registrations undergo ABORT_TASK logic.
3113 *
3114 * From there, core_scsi3_release_preempt_and_abort() will
3115 * release every registration in the list (which have already
3116 * been removed from the primary pr_reg list), except the
3117 * new persistent reservation holder, the calling Initiator Port.
3118 */
Andy Grover33ce6a82013-05-16 10:40:54 -07003119 if (preempt_type == PREEMPT_AND_ABORT) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003120 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3121 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3122 pr_reg_n);
3123 }
3124
3125 if (pr_tmpl->pr_aptpl_active) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08003126 if (!core_scsi3_update_and_write_aptpl(cmd->se_dev,
Andy Grover4e529be2013-05-16 10:40:59 -07003127 pr_reg_n->pr_aptpl_buf, PR_APTPL_BUF_LEN)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003128 pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
Andy Grover33ce6a82013-05-16 10:40:54 -07003129 "%s\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003130 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003131 }
3132
3133 core_scsi3_put_pr_reg(pr_reg_n);
Andy Grover5951146d2011-07-19 10:26:37 +00003134 core_scsi3_pr_generation(cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003135 return 0;
3136}
3137
Christoph Hellwigde103c92012-11-06 12:24:09 -08003138static sense_reason_t
3139core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07003140 u64 res_key, u64 sa_res_key, enum preempt_type preempt_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003141{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003142 switch (type) {
3143 case PR_TYPE_WRITE_EXCLUSIVE:
3144 case PR_TYPE_EXCLUSIVE_ACCESS:
3145 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3146 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3147 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3148 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
Christoph Hellwigde103c92012-11-06 12:24:09 -08003149 return core_scsi3_pro_preempt(cmd, type, scope, res_key,
Andy Grover33ce6a82013-05-16 10:40:54 -07003150 sa_res_key, preempt_type);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003151 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003152 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
Andy Grover33ce6a82013-05-16 10:40:54 -07003153 " Type: 0x%02x\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", type);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003154 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003155 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003156}
3157
3158
Christoph Hellwigde103c92012-11-06 12:24:09 -08003159static sense_reason_t
3160core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key,
3161 u64 sa_res_key, int aptpl, int unreg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003162{
Andy Grovere3d6f902011-07-19 08:55:10 +00003163 struct se_session *se_sess = cmd->se_sess;
Andy Grover5951146d2011-07-19 10:26:37 +00003164 struct se_device *dev = cmd->se_dev;
Jörn Engel28168902012-03-15 15:06:58 -04003165 struct se_dev_entry *dest_se_deve = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00003166 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003167 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3168 struct se_port *se_port;
3169 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3170 struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3171 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003172 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003173 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003174 unsigned char *initiator_str;
3175 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
3176 u32 tid_len, tmp_tid_len;
Andy Groverd2843c12013-05-16 10:40:55 -07003177 int new_reg = 0, type, scope, matching_iname;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003178 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003179 unsigned short rtpi;
3180 unsigned char proto_ident;
3181
Andy Grover6708bb22011-06-08 10:36:43 -07003182 if (!se_sess || !se_lun) {
3183 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003184 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003185 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003186
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003187 memset(dest_iport, 0, 64);
3188 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3189 se_tpg = se_sess->se_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00003190 tf_ops = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003191 /*
3192 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3193 * Register behaviors for a REGISTER AND MOVE service action
3194 *
3195 * Locate the existing *pr_reg via struct se_node_acl pointers
3196 */
Andy Grover5951146d2011-07-19 10:26:37 +00003197 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003198 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07003199 if (!pr_reg) {
3200 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003201 " *pr_reg for REGISTER_AND_MOVE\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003202 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003203 }
3204 /*
3205 * The provided reservation key much match the existing reservation key
3206 * provided during this initiator's I_T nexus registration.
3207 */
3208 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07003209 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003210 " res_key: 0x%016Lx does not match existing SA REGISTER"
3211 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003212 ret = TCM_RESERVATION_CONFLICT;
3213 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003214 }
3215 /*
3216 * The service active reservation key needs to be non zero
3217 */
Andy Grover6708bb22011-06-08 10:36:43 -07003218 if (!sa_res_key) {
3219 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003220 " sa_res_key\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003221 ret = TCM_INVALID_PARAMETER_LIST;
3222 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003223 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003224
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003225 /*
3226 * Determine the Relative Target Port Identifier where the reservation
3227 * will be moved to for the TransportID containing SCSI initiator WWN
3228 * information.
3229 */
Andy Grover49493142012-01-16 16:57:08 -08003230 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003231 if (!buf) {
3232 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3233 goto out_put_pr_reg;
3234 }
3235
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003236 rtpi = (buf[18] & 0xff) << 8;
3237 rtpi |= buf[19] & 0xff;
3238 tid_len = (buf[20] & 0xff) << 24;
3239 tid_len |= (buf[21] & 0xff) << 16;
3240 tid_len |= (buf[22] & 0xff) << 8;
3241 tid_len |= buf[23] & 0xff;
Andy Grover49493142012-01-16 16:57:08 -08003242 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003243 buf = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003244
3245 if ((tid_len + 24) != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07003246 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003247 " does not equal CDB data_length: %u\n", tid_len,
3248 cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003249 ret = TCM_INVALID_PARAMETER_LIST;
3250 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003251 }
3252
3253 spin_lock(&dev->se_port_lock);
3254 list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) {
3255 if (se_port->sep_rtpi != rtpi)
3256 continue;
3257 dest_se_tpg = se_port->sep_tpg;
Andy Grover6708bb22011-06-08 10:36:43 -07003258 if (!dest_se_tpg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003259 continue;
Andy Grovere3d6f902011-07-19 08:55:10 +00003260 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
Andy Grover6708bb22011-06-08 10:36:43 -07003261 if (!dest_tf_ops)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003262 continue;
3263
3264 atomic_inc(&dest_se_tpg->tpg_pr_ref_count);
3265 smp_mb__after_atomic_inc();
3266 spin_unlock(&dev->se_port_lock);
3267
Christoph Hellwigde103c92012-11-06 12:24:09 -08003268 if (core_scsi3_tpg_depend_item(dest_se_tpg)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003269 pr_err("core_scsi3_tpg_depend_item() failed"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003270 " for dest_se_tpg\n");
3271 atomic_dec(&dest_se_tpg->tpg_pr_ref_count);
3272 smp_mb__after_atomic_dec();
Christoph Hellwigde103c92012-11-06 12:24:09 -08003273 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3274 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003275 }
3276
3277 spin_lock(&dev->se_port_lock);
3278 break;
3279 }
3280 spin_unlock(&dev->se_port_lock);
3281
Andy Grover6708bb22011-06-08 10:36:43 -07003282 if (!dest_se_tpg || !dest_tf_ops) {
3283 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003284 " fabric ops from Relative Target Port Identifier:"
3285 " %hu\n", rtpi);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003286 ret = TCM_INVALID_PARAMETER_LIST;
3287 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003288 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003289
Andy Grover49493142012-01-16 16:57:08 -08003290 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003291 if (!buf) {
3292 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3293 goto out_put_pr_reg;
3294 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003295 proto_ident = (buf[24] & 0x0f);
Andy Grover8b1e1242012-04-03 15:51:12 -07003296
Andy Grover6708bb22011-06-08 10:36:43 -07003297 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003298 " 0x%02x\n", proto_ident);
Andy Grover8b1e1242012-04-03 15:51:12 -07003299
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003300 if (proto_ident != dest_tf_ops->get_fabric_proto_ident(dest_se_tpg)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003301 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003302 " proto_ident: 0x%02x does not match ident: 0x%02x"
3303 " from fabric: %s\n", proto_ident,
3304 dest_tf_ops->get_fabric_proto_ident(dest_se_tpg),
3305 dest_tf_ops->get_fabric_name());
Christoph Hellwigde103c92012-11-06 12:24:09 -08003306 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003307 goto out;
3308 }
3309 if (dest_tf_ops->tpg_parse_pr_out_transport_id == NULL) {
Andy Grover6708bb22011-06-08 10:36:43 -07003310 pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003311 " containg a valid tpg_parse_pr_out_transport_id"
3312 " function pointer\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003313 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003314 goto out;
3315 }
3316 initiator_str = dest_tf_ops->tpg_parse_pr_out_transport_id(dest_se_tpg,
3317 (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07003318 if (!initiator_str) {
3319 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003320 " initiator_str from Transport ID\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003321 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003322 goto out;
3323 }
3324
Andy Grover49493142012-01-16 16:57:08 -08003325 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003326 buf = NULL;
3327
Andy Grover6708bb22011-06-08 10:36:43 -07003328 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003329 " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
3330 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3331 iport_ptr : "");
3332 /*
3333 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3334 * action specifies a TransportID that is the same as the initiator port
3335 * of the I_T nexus for the command received, then the command shall
3336 * be terminated with CHECK CONDITION status, with the sense key set to
3337 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3338 * IN PARAMETER LIST.
3339 */
3340 pr_reg_nacl = pr_reg->pr_reg_nacl;
3341 matching_iname = (!strcmp(initiator_str,
3342 pr_reg_nacl->initiatorname)) ? 1 : 0;
Andy Grover6708bb22011-06-08 10:36:43 -07003343 if (!matching_iname)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003344 goto after_iport_check;
3345
Andy Grover6708bb22011-06-08 10:36:43 -07003346 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3347 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003348 " matches: %s on received I_T Nexus\n", initiator_str,
3349 pr_reg_nacl->initiatorname);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003350 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003351 goto out;
3352 }
Andy Grover6708bb22011-06-08 10:36:43 -07003353 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3354 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003355 " matches: %s %s on received I_T Nexus\n",
3356 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3357 pr_reg->pr_reg_isid);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003358 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003359 goto out;
3360 }
3361after_iport_check:
3362 /*
3363 * Locate the destination struct se_node_acl from the received Transport ID
3364 */
Roland Dreier28638882011-08-16 09:40:01 -07003365 spin_lock_irq(&dest_se_tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003366 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3367 initiator_str);
3368 if (dest_node_acl) {
3369 atomic_inc(&dest_node_acl->acl_pr_ref_count);
3370 smp_mb__after_atomic_inc();
3371 }
Roland Dreier28638882011-08-16 09:40:01 -07003372 spin_unlock_irq(&dest_se_tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003373
Andy Grover6708bb22011-06-08 10:36:43 -07003374 if (!dest_node_acl) {
3375 pr_err("Unable to locate %s dest_node_acl for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003376 " TransportID%s\n", dest_tf_ops->get_fabric_name(),
3377 initiator_str);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003378 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003379 goto out;
3380 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003381
3382 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003383 pr_err("core_scsi3_nodeacl_depend_item() for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003384 " dest_node_acl\n");
3385 atomic_dec(&dest_node_acl->acl_pr_ref_count);
3386 smp_mb__after_atomic_dec();
3387 dest_node_acl = NULL;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003388 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003389 goto out;
3390 }
Andy Grover8b1e1242012-04-03 15:51:12 -07003391
Andy Grover6708bb22011-06-08 10:36:43 -07003392 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003393 " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
3394 dest_node_acl->initiatorname);
Andy Grover8b1e1242012-04-03 15:51:12 -07003395
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003396 /*
3397 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3398 * PORT IDENTIFIER.
3399 */
3400 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
Andy Grover6708bb22011-06-08 10:36:43 -07003401 if (!dest_se_deve) {
3402 pr_err("Unable to locate %s dest_se_deve from RTPI:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003403 " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003404 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003405 goto out;
3406 }
3407
Christoph Hellwigde103c92012-11-06 12:24:09 -08003408 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003409 pr_err("core_scsi3_lunacl_depend_item() failed\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003410 atomic_dec(&dest_se_deve->pr_ref_count);
3411 smp_mb__after_atomic_dec();
3412 dest_se_deve = NULL;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003413 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003414 goto out;
3415 }
Andy Grover8b1e1242012-04-03 15:51:12 -07003416
Andy Grover6708bb22011-06-08 10:36:43 -07003417 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003418 " ACL for dest_se_deve->mapped_lun: %u\n",
3419 dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
3420 dest_se_deve->mapped_lun);
Andy Grover8b1e1242012-04-03 15:51:12 -07003421
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003422 /*
3423 * A persistent reservation needs to already existing in order to
3424 * successfully complete the REGISTER_AND_MOVE service action..
3425 */
3426 spin_lock(&dev->dev_reservation_lock);
3427 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07003428 if (!pr_res_holder) {
3429 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003430 " currently held\n");
3431 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003432 ret = TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003433 goto out;
3434 }
3435 /*
3436 * The received on I_T Nexus must be the reservation holder.
3437 *
3438 * From spc4r17 section 5.7.8 Table 50 --
3439 * Register behaviors for a REGISTER AND MOVE service action
3440 */
3441 if (pr_res_holder != pr_reg) {
Andy Grover6708bb22011-06-08 10:36:43 -07003442 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003443 " Nexus is not reservation holder\n");
3444 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003445 ret = TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003446 goto out;
3447 }
3448 /*
3449 * From spc4r17 section 5.7.8: registering and moving reservation
3450 *
3451 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3452 * action is received and the established persistent reservation is a
3453 * Write Exclusive - All Registrants type or Exclusive Access -
3454 * All Registrants type reservation, then the command shall be completed
3455 * with RESERVATION CONFLICT status.
3456 */
3457 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3458 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003459 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003460 " reservation for type: %s\n",
3461 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3462 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003463 ret = TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003464 goto out;
3465 }
3466 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3467 /*
3468 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3469 */
3470 type = pr_res_holder->pr_res_type;
3471 scope = pr_res_holder->pr_res_type;
3472 /*
3473 * c) Associate the reservation key specified in the SERVICE ACTION
3474 * RESERVATION KEY field with the I_T nexus specified as the
3475 * destination of the register and move, where:
3476 * A) The I_T nexus is specified by the TransportID and the
3477 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3478 * B) Regardless of the TransportID format used, the association for
3479 * the initiator port is based on either the initiator port name
3480 * (see 3.1.71) on SCSI transport protocols where port names are
3481 * required or the initiator port identifier (see 3.1.70) on SCSI
3482 * transport protocols where port names are not required;
3483 * d) Register the reservation key specified in the SERVICE ACTION
3484 * RESERVATION KEY field;
3485 * e) Retain the reservation key specified in the SERVICE ACTION
3486 * RESERVATION KEY field and associated information;
3487 *
3488 * Also, It is not an error for a REGISTER AND MOVE service action to
3489 * register an I_T nexus that is already registered with the same
3490 * reservation key or a different reservation key.
3491 */
3492 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3493 iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07003494 if (!dest_pr_reg) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08003495 if (core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003496 dest_node_acl, dest_se_deve, iport_ptr,
Christoph Hellwigde103c92012-11-06 12:24:09 -08003497 sa_res_key, 0, aptpl, 2, 1)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003498 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003499 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003500 goto out;
3501 }
3502 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3503 iport_ptr);
3504 new_reg = 1;
3505 }
3506 /*
3507 * f) Release the persistent reservation for the persistent reservation
3508 * holder (i.e., the I_T nexus on which the
3509 */
3510 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
3511 dev->dev_pr_res_holder, 0);
3512 /*
3513 * g) Move the persistent reservation to the specified I_T nexus using
3514 * the same scope and type as the persistent reservation released in
3515 * item f); and
3516 */
3517 dev->dev_pr_res_holder = dest_pr_reg;
3518 dest_pr_reg->pr_res_holder = 1;
3519 dest_pr_reg->pr_res_type = type;
3520 pr_reg->pr_res_scope = scope;
Andy Groverd2843c12013-05-16 10:40:55 -07003521 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003522 /*
3523 * Increment PRGeneration for existing registrations..
3524 */
Andy Grover6708bb22011-06-08 10:36:43 -07003525 if (!new_reg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003526 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3527 spin_unlock(&dev->dev_reservation_lock);
3528
Andy Grover6708bb22011-06-08 10:36:43 -07003529 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003530 " created new reservation holder TYPE: %s on object RTPI:"
3531 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
3532 core_scsi3_pr_dump_type(type), rtpi,
3533 dest_pr_reg->pr_res_generation);
Andy Grover6708bb22011-06-08 10:36:43 -07003534 pr_debug("SPC-3 PR Successfully moved reservation from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003535 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3536 tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07003537 i_buf, dest_tf_ops->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003538 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3539 iport_ptr : "");
3540 /*
3541 * It is now safe to release configfs group dependencies for destination
3542 * of Transport ID Initiator Device/Port Identifier
3543 */
3544 core_scsi3_lunacl_undepend_item(dest_se_deve);
3545 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3546 core_scsi3_tpg_undepend_item(dest_se_tpg);
3547 /*
3548 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3549 * nexus on which PERSISTENT RESERVE OUT command was received.
3550 */
3551 if (unreg) {
3552 spin_lock(&pr_tmpl->registration_lock);
3553 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3554 spin_unlock(&pr_tmpl->registration_lock);
3555 } else
3556 core_scsi3_put_pr_reg(pr_reg);
3557
3558 /*
3559 * Clear the APTPL metadata if APTPL has been disabled, otherwise
3560 * write out the updated metadata to struct file for this SCSI device.
3561 */
Andy Grover6708bb22011-06-08 10:36:43 -07003562 if (!aptpl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003563 pr_tmpl->pr_aptpl_active = 0;
Andy Grover5951146d2011-07-19 10:26:37 +00003564 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07003565 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003566 " REGISTER_AND_MOVE\n");
3567 } else {
3568 pr_tmpl->pr_aptpl_active = 1;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003569 if (!core_scsi3_update_and_write_aptpl(cmd->se_dev,
Andy Grover4e529be2013-05-16 10:40:59 -07003570 dest_pr_reg->pr_aptpl_buf, PR_APTPL_BUF_LEN)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003571 pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003572 " REGISTER_AND_MOVE\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003573 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003574 }
3575
Andy Grover49493142012-01-16 16:57:08 -08003576 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003577
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003578 core_scsi3_put_pr_reg(dest_pr_reg);
3579 return 0;
3580out:
Andy Grover05d1c7c2011-07-20 19:13:28 +00003581 if (buf)
Andy Grover49493142012-01-16 16:57:08 -08003582 transport_kunmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003583 if (dest_se_deve)
3584 core_scsi3_lunacl_undepend_item(dest_se_deve);
3585 if (dest_node_acl)
3586 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3587 core_scsi3_tpg_undepend_item(dest_se_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003588
3589out_put_pr_reg:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003590 core_scsi3_put_pr_reg(pr_reg);
3591 return ret;
3592}
3593
3594static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
3595{
3596 unsigned int __v1, __v2;
3597
3598 __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
3599 __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
3600
3601 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
3602}
3603
3604/*
3605 * See spc4r17 section 6.14 Table 170
3606 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003607sense_reason_t
3608target_scsi3_emulate_pr_out(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003609{
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003610 unsigned char *cdb = &cmd->t_task_cdb[0];
Andy Grover05d1c7c2011-07-20 19:13:28 +00003611 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003612 u64 res_key, sa_res_key;
3613 int sa, scope, type, aptpl;
3614 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003615 sense_reason_t ret;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003616
3617 /*
3618 * Following spc2r20 5.5.1 Reservations overview:
3619 *
3620 * If a logical unit has been reserved by any RESERVE command and is
3621 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3622 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3623 * initiator or service action and shall terminate with a RESERVATION
3624 * CONFLICT status.
3625 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003626 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003627 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3628 " SPC-2 reservation is held, returning"
3629 " RESERVATION_CONFLICT\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003630 return TCM_RESERVATION_CONFLICT;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003631 }
3632
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003633 /*
3634 * FIXME: A NULL struct se_session pointer means an this is not coming from
3635 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3636 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003637 if (!cmd->se_sess)
3638 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003639
3640 if (cmd->data_length < 24) {
Andy Grover6708bb22011-06-08 10:36:43 -07003641 pr_warn("SPC-PR: Received PR OUT parameter list"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003642 " length too small: %u\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003643 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003644 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003645
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003646 /*
3647 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3648 */
3649 sa = (cdb[1] & 0x1f);
3650 scope = (cdb[2] & 0xf0);
3651 type = (cdb[2] & 0x0f);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003652
Andy Grover49493142012-01-16 16:57:08 -08003653 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003654 if (!buf)
3655 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3656
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003657 /*
3658 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3659 */
3660 res_key = core_scsi3_extract_reservation_key(&buf[0]);
3661 sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
3662 /*
3663 * REGISTER_AND_MOVE uses a different SA parameter list containing
3664 * SCSI TransportIDs.
3665 */
3666 if (sa != PRO_REGISTER_AND_MOVE) {
3667 spec_i_pt = (buf[20] & 0x08);
3668 all_tg_pt = (buf[20] & 0x04);
3669 aptpl = (buf[20] & 0x01);
3670 } else {
3671 aptpl = (buf[17] & 0x01);
3672 unreg = (buf[17] & 0x02);
3673 }
Andy Grover49493142012-01-16 16:57:08 -08003674 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003675 buf = NULL;
3676
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003677 /*
3678 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3679 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003680 if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER))
3681 return TCM_INVALID_PARAMETER_LIST;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003682
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003683 /*
3684 * From spc4r17 section 6.14:
3685 *
3686 * If the SPEC_I_PT bit is set to zero, the service action is not
3687 * REGISTER AND MOVE, and the parameter list length is not 24, then
3688 * the command shall be terminated with CHECK CONDITION status, with
3689 * the sense key set to ILLEGAL REQUEST, and the additional sense
3690 * code set to PARAMETER LIST LENGTH ERROR.
3691 */
Andy Grover6708bb22011-06-08 10:36:43 -07003692 if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003693 (cmd->data_length != 24)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003694 pr_warn("SPC-PR: Received PR OUT illegal parameter"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003695 " list length: %u\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003696 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003697 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003698
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003699 /*
3700 * (core_scsi3_emulate_pro_* function parameters
3701 * are defined by spc4r17 Table 174:
3702 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3703 */
3704 switch (sa) {
3705 case PRO_REGISTER:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003706 ret = core_scsi3_emulate_pro_register(cmd,
Andy Grover33ce6a82013-05-16 10:40:54 -07003707 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003708 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003709 case PRO_RESERVE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003710 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3711 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003712 case PRO_RELEASE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003713 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3714 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003715 case PRO_CLEAR:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003716 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3717 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003718 case PRO_PREEMPT:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003719 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07003720 res_key, sa_res_key, PREEMPT);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003721 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003722 case PRO_PREEMPT_AND_ABORT:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003723 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07003724 res_key, sa_res_key, PREEMPT_AND_ABORT);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003725 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003726 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003727 ret = core_scsi3_emulate_pro_register(cmd,
Andy Grover33ce6a82013-05-16 10:40:54 -07003728 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER_AND_IGNORE_EXISTING_KEY);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003729 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003730 case PRO_REGISTER_AND_MOVE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003731 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003732 sa_res_key, aptpl, unreg);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003733 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003734 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003735 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003736 " action: 0x%02x\n", cdb[1] & 0x1f);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003737 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003738 }
3739
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04003740 if (!ret)
3741 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003742 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003743}
3744
3745/*
3746 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3747 *
3748 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3749 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003750static sense_reason_t
3751core_scsi3_pri_read_keys(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003752{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003753 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003754 struct t10_pr_registration *pr_reg;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003755 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003756 u32 add_len = 0, off = 8;
3757
3758 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07003759 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003760 " too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003761 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003762 }
3763
Andy Grover49493142012-01-16 16:57:08 -08003764 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003765 if (!buf)
3766 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3767
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003768 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3769 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3770 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3771 buf[3] = (dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003772
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003773 spin_lock(&dev->t10_pr.registration_lock);
3774 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003775 pr_reg_list) {
3776 /*
3777 * Check for overflow of 8byte PRI READ_KEYS payload and
3778 * next reservation key list descriptor.
3779 */
3780 if ((add_len + 8) > (cmd->data_length - 8))
3781 break;
3782
3783 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3784 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3785 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3786 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
3787 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
3788 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
3789 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
3790 buf[off++] = (pr_reg->pr_res_key & 0xff);
3791
3792 add_len += 8;
3793 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003794 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003795
3796 buf[4] = ((add_len >> 24) & 0xff);
3797 buf[5] = ((add_len >> 16) & 0xff);
3798 buf[6] = ((add_len >> 8) & 0xff);
3799 buf[7] = (add_len & 0xff);
3800
Andy Grover49493142012-01-16 16:57:08 -08003801 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003802
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003803 return 0;
3804}
3805
3806/*
3807 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3808 *
3809 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3810 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003811static sense_reason_t
3812core_scsi3_pri_read_reservation(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003813{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003814 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003815 struct t10_pr_registration *pr_reg;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003816 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003817 u64 pr_res_key;
3818 u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
3819
3820 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07003821 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003822 " too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003823 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003824 }
3825
Andy Grover49493142012-01-16 16:57:08 -08003826 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003827 if (!buf)
3828 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3829
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003830 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3831 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3832 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3833 buf[3] = (dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003834
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003835 spin_lock(&dev->dev_reservation_lock);
3836 pr_reg = dev->dev_pr_res_holder;
Andy Groveree1b1b92012-07-12 17:34:54 -07003837 if (pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003838 /*
3839 * Set the hardcoded Additional Length
3840 */
3841 buf[4] = ((add_len >> 24) & 0xff);
3842 buf[5] = ((add_len >> 16) & 0xff);
3843 buf[6] = ((add_len >> 8) & 0xff);
3844 buf[7] = (add_len & 0xff);
3845
Andy Grover05d1c7c2011-07-20 19:13:28 +00003846 if (cmd->data_length < 22)
3847 goto err;
3848
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003849 /*
3850 * Set the Reservation key.
3851 *
3852 * From spc4r17, section 5.7.10:
3853 * A persistent reservation holder has its reservation key
3854 * returned in the parameter data from a PERSISTENT
3855 * RESERVE IN command with READ RESERVATION service action as
3856 * follows:
3857 * a) For a persistent reservation of the type Write Exclusive
3858 * - All Registrants or Exclusive Access ­ All Regitrants,
3859 * the reservation key shall be set to zero; or
3860 * b) For all other persistent reservation types, the
3861 * reservation key shall be set to the registered
3862 * reservation key for the I_T nexus that holds the
3863 * persistent reservation.
3864 */
3865 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3866 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
3867 pr_res_key = 0;
3868 else
3869 pr_res_key = pr_reg->pr_res_key;
3870
3871 buf[8] = ((pr_res_key >> 56) & 0xff);
3872 buf[9] = ((pr_res_key >> 48) & 0xff);
3873 buf[10] = ((pr_res_key >> 40) & 0xff);
3874 buf[11] = ((pr_res_key >> 32) & 0xff);
3875 buf[12] = ((pr_res_key >> 24) & 0xff);
3876 buf[13] = ((pr_res_key >> 16) & 0xff);
3877 buf[14] = ((pr_res_key >> 8) & 0xff);
3878 buf[15] = (pr_res_key & 0xff);
3879 /*
3880 * Set the SCOPE and TYPE
3881 */
3882 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
3883 (pr_reg->pr_res_type & 0x0f);
3884 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003885
3886err:
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003887 spin_unlock(&dev->dev_reservation_lock);
Andy Grover49493142012-01-16 16:57:08 -08003888 transport_kunmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003889
3890 return 0;
3891}
3892
3893/*
3894 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3895 *
3896 * See spc4r17 section 6.13.4 Table 165
3897 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003898static sense_reason_t
3899core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003900{
Andy Grover5951146d2011-07-19 10:26:37 +00003901 struct se_device *dev = cmd->se_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003902 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003903 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003904 u16 add_len = 8; /* Hardcoded to 8. */
3905
3906 if (cmd->data_length < 6) {
Andy Grover6708bb22011-06-08 10:36:43 -07003907 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003908 " %u too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003909 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003910 }
3911
Andy Grover49493142012-01-16 16:57:08 -08003912 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003913 if (!buf)
3914 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003915
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003916 buf[0] = ((add_len << 8) & 0xff);
3917 buf[1] = (add_len & 0xff);
3918 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3919 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3920 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3921 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3922 /*
3923 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3924 * set the TMV: Task Mask Valid bit.
3925 */
3926 buf[3] |= 0x80;
3927 /*
3928 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3929 */
3930 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3931 /*
3932 * PTPL_A: Persistence across Target Power Loss Active bit
3933 */
3934 if (pr_tmpl->pr_aptpl_active)
3935 buf[3] |= 0x01;
3936 /*
3937 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
3938 */
3939 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3940 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
3941 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
3942 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
3943 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
3944 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3945
Andy Grover49493142012-01-16 16:57:08 -08003946 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003947
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003948 return 0;
3949}
3950
3951/*
3952 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
3953 *
3954 * See spc4r17 section 6.13.5 Table 168 and 169
3955 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003956static sense_reason_t
3957core_scsi3_pri_read_full_status(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003958{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003959 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003960 struct se_node_acl *se_nacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003961 struct se_portal_group *se_tpg;
3962 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003963 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003964 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003965 u32 add_desc_len = 0, add_len = 0, desc_len, exp_desc_len;
3966 u32 off = 8; /* off into first Full Status descriptor */
3967 int format_code = 0;
3968
3969 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07003970 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003971 " too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003972 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003973 }
3974
Andy Grover49493142012-01-16 16:57:08 -08003975 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003976 if (!buf)
3977 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003978
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003979 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3980 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3981 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3982 buf[3] = (dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003983
3984 spin_lock(&pr_tmpl->registration_lock);
3985 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3986 &pr_tmpl->registration_list, pr_reg_list) {
3987
3988 se_nacl = pr_reg->pr_reg_nacl;
3989 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
3990 add_desc_len = 0;
3991
3992 atomic_inc(&pr_reg->pr_res_holders);
3993 smp_mb__after_atomic_inc();
3994 spin_unlock(&pr_tmpl->registration_lock);
3995 /*
3996 * Determine expected length of $FABRIC_MOD specific
3997 * TransportID full status descriptor..
3998 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003999 exp_desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id_len(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004000 se_tpg, se_nacl, pr_reg, &format_code);
4001
4002 if ((exp_desc_len + add_len) > cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07004003 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004004 " out of buffer: %d\n", cmd->data_length);
4005 spin_lock(&pr_tmpl->registration_lock);
4006 atomic_dec(&pr_reg->pr_res_holders);
4007 smp_mb__after_atomic_dec();
4008 break;
4009 }
4010 /*
4011 * Set RESERVATION KEY
4012 */
4013 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
4014 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
4015 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
4016 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
4017 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
4018 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
4019 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
4020 buf[off++] = (pr_reg->pr_res_key & 0xff);
4021 off += 4; /* Skip Over Reserved area */
4022
4023 /*
4024 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4025 */
4026 if (pr_reg->pr_reg_all_tg_pt)
4027 buf[off] = 0x02;
4028 /*
4029 * The struct se_lun pointer will be present for the
4030 * reservation holder for PR_HOLDER bit.
4031 *
4032 * Also, if this registration is the reservation
4033 * holder, fill in SCOPE and TYPE in the next byte.
4034 */
4035 if (pr_reg->pr_res_holder) {
4036 buf[off++] |= 0x01;
4037 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
4038 (pr_reg->pr_res_type & 0x0f);
4039 } else
4040 off += 2;
4041
4042 off += 4; /* Skip over reserved area */
4043 /*
4044 * From spc4r17 6.3.15:
4045 *
4046 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4047 * IDENTIFIER field contains the relative port identifier (see
4048 * 3.1.120) of the target port that is part of the I_T nexus
4049 * described by this full status descriptor. If the ALL_TG_PT
4050 * bit is set to one, the contents of the RELATIVE TARGET PORT
4051 * IDENTIFIER field are not defined by this standard.
4052 */
Andy Grover6708bb22011-06-08 10:36:43 -07004053 if (!pr_reg->pr_reg_all_tg_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004054 struct se_port *port = pr_reg->pr_reg_tg_pt_lun->lun_sep;
4055
4056 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
4057 buf[off++] = (port->sep_rtpi & 0xff);
4058 } else
Masanari Iida35d1efe2012-08-16 22:43:13 +09004059 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004060
4061 /*
4062 * Now, have the $FABRIC_MOD fill in the protocol identifier
4063 */
Andy Grovere3d6f902011-07-19 08:55:10 +00004064 desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id(se_tpg,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004065 se_nacl, pr_reg, &format_code, &buf[off+4]);
4066
4067 spin_lock(&pr_tmpl->registration_lock);
4068 atomic_dec(&pr_reg->pr_res_holders);
4069 smp_mb__after_atomic_dec();
4070 /*
4071 * Set the ADDITIONAL DESCRIPTOR LENGTH
4072 */
4073 buf[off++] = ((desc_len >> 24) & 0xff);
4074 buf[off++] = ((desc_len >> 16) & 0xff);
4075 buf[off++] = ((desc_len >> 8) & 0xff);
4076 buf[off++] = (desc_len & 0xff);
4077 /*
4078 * Size of full desctipor header minus TransportID
4079 * containing $FABRIC_MOD specific) initiator device/port
4080 * WWN information.
4081 *
4082 * See spc4r17 Section 6.13.5 Table 169
4083 */
4084 add_desc_len = (24 + desc_len);
4085
4086 off += desc_len;
4087 add_len += add_desc_len;
4088 }
4089 spin_unlock(&pr_tmpl->registration_lock);
4090 /*
4091 * Set ADDITIONAL_LENGTH
4092 */
4093 buf[4] = ((add_len >> 24) & 0xff);
4094 buf[5] = ((add_len >> 16) & 0xff);
4095 buf[6] = ((add_len >> 8) & 0xff);
4096 buf[7] = (add_len & 0xff);
4097
Andy Grover49493142012-01-16 16:57:08 -08004098 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00004099
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004100 return 0;
4101}
4102
Christoph Hellwigde103c92012-11-06 12:24:09 -08004103sense_reason_t
4104target_scsi3_emulate_pr_in(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004105{
Christoph Hellwigde103c92012-11-06 12:24:09 -08004106 sense_reason_t ret;
Christoph Hellwige76a35d2011-11-03 17:50:42 -04004107
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004108 /*
4109 * Following spc2r20 5.5.1 Reservations overview:
4110 *
4111 * If a logical unit has been reserved by any RESERVE command and is
4112 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4113 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4114 * initiator or service action and shall terminate with a RESERVATION
4115 * CONFLICT status.
4116 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04004117 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004118 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4119 " SPC-2 reservation is held, returning"
4120 " RESERVATION_CONFLICT\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08004121 return TCM_RESERVATION_CONFLICT;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004122 }
4123
4124 switch (cmd->t_task_cdb[1] & 0x1f) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004125 case PRI_READ_KEYS:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004126 ret = core_scsi3_pri_read_keys(cmd);
4127 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004128 case PRI_READ_RESERVATION:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004129 ret = core_scsi3_pri_read_reservation(cmd);
4130 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004131 case PRI_REPORT_CAPABILITIES:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004132 ret = core_scsi3_pri_report_capabilities(cmd);
4133 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004134 case PRI_READ_FULL_STATUS:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004135 ret = core_scsi3_pri_read_full_status(cmd);
4136 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004137 default:
Andy Grover6708bb22011-06-08 10:36:43 -07004138 pr_err("Unknown PERSISTENT_RESERVE_IN service"
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004139 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
Christoph Hellwigde103c92012-11-06 12:24:09 -08004140 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004141 }
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004142
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04004143 if (!ret)
4144 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004145 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004146}
4147
Christoph Hellwigde103c92012-11-06 12:24:09 -08004148sense_reason_t
4149target_check_reservation(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004150{
Christoph Hellwigd977f432012-10-10 17:37:15 -04004151 struct se_device *dev = cmd->se_dev;
Christoph Hellwigde103c92012-11-06 12:24:09 -08004152 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004153
Christoph Hellwigd977f432012-10-10 17:37:15 -04004154 if (!cmd->se_sess)
4155 return 0;
4156 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
4157 return 0;
4158 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
4159 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004160
Christoph Hellwigd977f432012-10-10 17:37:15 -04004161 spin_lock(&dev->dev_reservation_lock);
4162 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
4163 ret = target_scsi2_reservation_check(cmd);
4164 else
4165 ret = target_scsi3_pr_reservation_check(cmd);
4166 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04004167
Christoph Hellwigd977f432012-10-10 17:37:15 -04004168 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004169}