blob: d14860ff25082d7bd98016a8cc04c200fb9343cc [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001/*******************************************************************************
2 * Filename: target_core_pr.c
3 *
4 * This file contains SPC-3 compliant persistent reservations and
5 * legacy SPC-2 reservations with compatible reservation handling (CRH=1)
6 *
7 * Copyright (c) 2009, 2010 Rising Tide Systems
8 * Copyright (c) 2009, 2010 Linux-iSCSI.org
9 *
10 * Nicholas A. Bellinger <nab@kernel.org>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 *
26 ******************************************************************************/
27
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080028#include <linux/slab.h>
29#include <linux/spinlock.h>
30#include <linux/list.h>
31#include <scsi/scsi.h>
32#include <scsi/scsi_cmnd.h>
33#include <asm/unaligned.h>
34
35#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050036#include <target/target_core_backend.h>
37#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080038#include <target/target_core_configfs.h>
39
Christoph Hellwige26d99a2011-11-14 12:30:30 -050040#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080041#include "target_core_pr.h"
42#include "target_core_ua.h"
43
44/*
45 * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
46 */
47struct pr_transport_id_holder {
48 int dest_local_nexus;
49 struct t10_pr_registration *dest_pr_reg;
50 struct se_portal_group *dest_tpg;
51 struct se_node_acl *dest_node_acl;
52 struct se_dev_entry *dest_se_deve;
53 struct list_head dest_list;
54};
55
56int core_pr_dump_initiator_port(
57 struct t10_pr_registration *pr_reg,
58 char *buf,
59 u32 size)
60{
Andy Grover6708bb22011-06-08 10:36:43 -070061 if (!pr_reg->isid_present_at_reg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080062 return 0;
63
64 snprintf(buf, size, ",i,0x%s", &pr_reg->pr_reg_isid[0]);
65 return 1;
66}
67
68static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
69 struct t10_pr_registration *, int);
70
71static int core_scsi2_reservation_seq_non_holder(
72 struct se_cmd *cmd,
73 unsigned char *cdb,
74 u32 pr_reg_type)
75{
76 switch (cdb[0]) {
77 case INQUIRY:
78 case RELEASE:
79 case RELEASE_10:
80 return 0;
81 default:
82 return 1;
83 }
84
85 return 1;
86}
87
88static int core_scsi2_reservation_check(struct se_cmd *cmd, u32 *pr_reg_type)
89{
90 struct se_device *dev = cmd->se_dev;
91 struct se_session *sess = cmd->se_sess;
92 int ret;
93
Andy Grover6708bb22011-06-08 10:36:43 -070094 if (!sess)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080095 return 0;
96
97 spin_lock(&dev->dev_reservation_lock);
98 if (!dev->dev_reserved_node_acl || !sess) {
99 spin_unlock(&dev->dev_reservation_lock);
100 return 0;
101 }
102 if (dev->dev_reserved_node_acl != sess->se_node_acl) {
103 spin_unlock(&dev->dev_reservation_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +0000104 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800105 }
106 if (!(dev->dev_flags & DF_SPC2_RESERVATIONS_WITH_ISID)) {
107 spin_unlock(&dev->dev_reservation_lock);
108 return 0;
109 }
Andy Grovere3d6f902011-07-19 08:55:10 +0000110 ret = (dev->dev_res_bin_isid == sess->sess_bin_isid) ? 0 : -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800111 spin_unlock(&dev->dev_reservation_lock);
112
113 return ret;
114}
115
Christoph Hellwigeacac002011-11-03 17:50:40 -0400116static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
117 struct se_node_acl *, struct se_session *);
118static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
119
120static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd, int *ret)
121{
122 struct se_session *se_sess = cmd->se_sess;
123 struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
124 struct t10_pr_registration *pr_reg;
125 struct t10_reservation *pr_tmpl = &su_dev->t10_pr;
126 int crh = (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS);
127 int conflict = 0;
128
129 if (!crh)
130 return false;
131
132 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
133 se_sess);
134 if (pr_reg) {
135 /*
136 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
137 * behavior
138 *
139 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
140 * status, but no reservation shall be established and the
141 * persistent reservation shall not be changed, if the command
142 * is received from a) and b) below.
143 *
144 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
145 * status, but the persistent reservation shall not be released,
146 * if the command is received from a) and b)
147 *
148 * a) An I_T nexus that is a persistent reservation holder; or
149 * b) An I_T nexus that is registered if a registrants only or
150 * all registrants type persistent reservation is present.
151 *
152 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
153 * RELEASE(6) command, or RELEASE(10) command shall be processed
154 * as defined in SPC-2.
155 */
156 if (pr_reg->pr_res_holder) {
157 core_scsi3_put_pr_reg(pr_reg);
158 *ret = 0;
159 return false;
160 }
161 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
162 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
163 (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
164 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
165 core_scsi3_put_pr_reg(pr_reg);
166 *ret = 0;
167 return true;
168 }
169 core_scsi3_put_pr_reg(pr_reg);
170 conflict = 1;
171 } else {
172 /*
173 * Following spc2r20 5.5.1 Reservations overview:
174 *
175 * If a logical unit has executed a PERSISTENT RESERVE OUT
176 * command with the REGISTER or the REGISTER AND IGNORE
177 * EXISTING KEY service action and is still registered by any
178 * initiator, all RESERVE commands and all RELEASE commands
179 * regardless of initiator shall conflict and shall terminate
180 * with a RESERVATION CONFLICT status.
181 */
182 spin_lock(&pr_tmpl->registration_lock);
183 conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
184 spin_unlock(&pr_tmpl->registration_lock);
185 }
186
187 if (conflict) {
188 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
189 " while active SPC-3 registrations exist,"
190 " returning RESERVATION_CONFLICT\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700191 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400192 return true;
193 }
194
195 return false;
196}
197
Christoph Hellwige76a35d2011-11-03 17:50:42 -0400198int target_scsi2_reservation_release(struct se_task *task)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800199{
Christoph Hellwige76a35d2011-11-03 17:50:42 -0400200 struct se_cmd *cmd = task->task_se_cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800201 struct se_device *dev = cmd->se_dev;
202 struct se_session *sess = cmd->se_sess;
203 struct se_portal_group *tpg = sess->se_tpg;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400204 int ret = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800205
Andy Grover6708bb22011-06-08 10:36:43 -0700206 if (!sess || !tpg)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400207 goto out;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400208 if (target_check_scsi2_reservation_conflict(cmd, &ret))
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400209 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800210
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400211 ret = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800212 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400213 if (!dev->dev_reserved_node_acl || !sess)
214 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800215
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400216 if (dev->dev_reserved_node_acl != sess->se_node_acl)
217 goto out_unlock;
218
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800219 dev->dev_reserved_node_acl = NULL;
220 dev->dev_flags &= ~DF_SPC2_RESERVATIONS;
221 if (dev->dev_flags & DF_SPC2_RESERVATIONS_WITH_ISID) {
222 dev->dev_res_bin_isid = 0;
223 dev->dev_flags &= ~DF_SPC2_RESERVATIONS_WITH_ISID;
224 }
Andy Grover6708bb22011-06-08 10:36:43 -0700225 pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
Andy Grovere3d6f902011-07-19 08:55:10 +0000226 " MAPPED LUN: %u for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
227 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800228 sess->se_node_acl->initiatorname);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800229
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400230out_unlock:
231 spin_unlock(&dev->dev_reservation_lock);
232out:
233 if (!ret) {
234 task->task_scsi_status = GOOD;
235 transport_complete_task(task, 1);
236 }
237 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800238}
239
Christoph Hellwige76a35d2011-11-03 17:50:42 -0400240int target_scsi2_reservation_reserve(struct se_task *task)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800241{
Christoph Hellwige76a35d2011-11-03 17:50:42 -0400242 struct se_cmd *cmd = task->task_se_cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800243 struct se_device *dev = cmd->se_dev;
244 struct se_session *sess = cmd->se_sess;
245 struct se_portal_group *tpg = sess->se_tpg;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400246 int ret = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800247
Andy Grovera1d8b492011-05-02 17:12:10 -0700248 if ((cmd->t_task_cdb[1] & 0x01) &&
249 (cmd->t_task_cdb[1] & 0x02)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700250 pr_err("LongIO and Obselete Bits set, returning"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800251 " ILLEGAL_REQUEST\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700252 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
253 ret = -EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400254 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800255 }
256 /*
257 * This is currently the case for target_core_mod passthrough struct se_cmd
258 * ops
259 */
Andy Grover6708bb22011-06-08 10:36:43 -0700260 if (!sess || !tpg)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400261 goto out;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400262 if (target_check_scsi2_reservation_conflict(cmd, &ret))
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400263 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800264
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400265 ret = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800266 spin_lock(&dev->dev_reservation_lock);
267 if (dev->dev_reserved_node_acl &&
268 (dev->dev_reserved_node_acl != sess->se_node_acl)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700269 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000270 tpg->se_tpg_tfo->get_fabric_name());
Andy Grover6708bb22011-06-08 10:36:43 -0700271 pr_err("Original reserver LUN: %u %s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000272 cmd->se_lun->unpacked_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800273 dev->dev_reserved_node_acl->initiatorname);
Andy Grover6708bb22011-06-08 10:36:43 -0700274 pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
Andy Grovere3d6f902011-07-19 08:55:10 +0000275 " from %s \n", cmd->se_lun->unpacked_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800276 cmd->se_deve->mapped_lun,
277 sess->se_node_acl->initiatorname);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700278 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
279 ret = -EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400280 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800281 }
282
283 dev->dev_reserved_node_acl = sess->se_node_acl;
284 dev->dev_flags |= DF_SPC2_RESERVATIONS;
285 if (sess->sess_bin_isid != 0) {
286 dev->dev_res_bin_isid = sess->sess_bin_isid;
287 dev->dev_flags |= DF_SPC2_RESERVATIONS_WITH_ISID;
288 }
Andy Grover6708bb22011-06-08 10:36:43 -0700289 pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
Andy Grovere3d6f902011-07-19 08:55:10 +0000290 " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
291 cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800292 sess->se_node_acl->initiatorname);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800293
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400294out_unlock:
295 spin_unlock(&dev->dev_reservation_lock);
296out:
297 if (!ret) {
298 task->task_scsi_status = GOOD;
299 transport_complete_task(task, 1);
300 }
301 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800302}
303
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800304
305/*
306 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
307 *
308 * This function is called by those initiator ports who are *NOT*
309 * the active PR reservation holder when a reservation is present.
310 */
311static int core_scsi3_pr_seq_non_holder(
312 struct se_cmd *cmd,
313 unsigned char *cdb,
314 u32 pr_reg_type)
315{
316 struct se_dev_entry *se_deve;
Andy Grovere3d6f902011-07-19 08:55:10 +0000317 struct se_session *se_sess = cmd->se_sess;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800318 int other_cdb = 0, ignore_reg;
319 int registered_nexus = 0, ret = 1; /* Conflict by default */
320 int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
321 int we = 0; /* Write Exclusive */
322 int legacy = 0; /* Act like a legacy device and return
323 * RESERVATION CONFLICT on some CDBs */
324 /*
325 * A legacy SPC-2 reservation is being held.
326 */
327 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS)
328 return core_scsi2_reservation_seq_non_holder(cmd,
329 cdb, pr_reg_type);
330
331 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
332 /*
333 * Determine if the registration should be ignored due to
334 * non-matching ISIDs in core_scsi3_pr_reservation_check().
335 */
336 ignore_reg = (pr_reg_type & 0x80000000);
337 if (ignore_reg)
338 pr_reg_type &= ~0x80000000;
339
340 switch (pr_reg_type) {
341 case PR_TYPE_WRITE_EXCLUSIVE:
342 we = 1;
343 case PR_TYPE_EXCLUSIVE_ACCESS:
344 /*
345 * Some commands are only allowed for the persistent reservation
346 * holder.
347 */
348 if ((se_deve->def_pr_registered) && !(ignore_reg))
349 registered_nexus = 1;
350 break;
351 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
352 we = 1;
353 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
354 /*
355 * Some commands are only allowed for registered I_T Nexuses.
356 */
357 reg_only = 1;
358 if ((se_deve->def_pr_registered) && !(ignore_reg))
359 registered_nexus = 1;
360 break;
361 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
362 we = 1;
363 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
364 /*
365 * Each registered I_T Nexus is a reservation holder.
366 */
367 all_reg = 1;
368 if ((se_deve->def_pr_registered) && !(ignore_reg))
369 registered_nexus = 1;
370 break;
371 default:
Andy Grovere3d6f902011-07-19 08:55:10 +0000372 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800373 }
374 /*
375 * Referenced from spc4r17 table 45 for *NON* PR holder access
376 */
377 switch (cdb[0]) {
378 case SECURITY_PROTOCOL_IN:
379 if (registered_nexus)
380 return 0;
381 ret = (we) ? 0 : 1;
382 break;
383 case MODE_SENSE:
384 case MODE_SENSE_10:
385 case READ_ATTRIBUTE:
386 case READ_BUFFER:
387 case RECEIVE_DIAGNOSTIC:
388 if (legacy) {
389 ret = 1;
390 break;
391 }
392 if (registered_nexus) {
393 ret = 0;
394 break;
395 }
396 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
397 break;
398 case PERSISTENT_RESERVE_OUT:
399 /*
400 * This follows PERSISTENT_RESERVE_OUT service actions that
401 * are allowed in the presence of various reservations.
402 * See spc4r17, table 46
403 */
404 switch (cdb[1] & 0x1f) {
405 case PRO_CLEAR:
406 case PRO_PREEMPT:
407 case PRO_PREEMPT_AND_ABORT:
408 ret = (registered_nexus) ? 0 : 1;
409 break;
410 case PRO_REGISTER:
411 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
412 ret = 0;
413 break;
414 case PRO_REGISTER_AND_MOVE:
415 case PRO_RESERVE:
416 ret = 1;
417 break;
418 case PRO_RELEASE:
419 ret = (registered_nexus) ? 0 : 1;
420 break;
421 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700422 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800423 " action: 0x%02x\n", cdb[1] & 0x1f);
Andy Grovere3d6f902011-07-19 08:55:10 +0000424 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800425 }
426 break;
427 case RELEASE:
428 case RELEASE_10:
Christoph Hellwigeacac002011-11-03 17:50:40 -0400429 /* Handled by CRH=1 in target_scsi2_reservation_release() */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800430 ret = 0;
431 break;
432 case RESERVE:
433 case RESERVE_10:
Christoph Hellwigeacac002011-11-03 17:50:40 -0400434 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800435 ret = 0;
436 break;
437 case TEST_UNIT_READY:
438 ret = (legacy) ? 1 : 0; /* Conflict for legacy */
439 break;
440 case MAINTENANCE_IN:
441 switch (cdb[1] & 0x1f) {
442 case MI_MANAGEMENT_PROTOCOL_IN:
443 if (registered_nexus) {
444 ret = 0;
445 break;
446 }
447 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
448 break;
449 case MI_REPORT_SUPPORTED_OPERATION_CODES:
450 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
451 if (legacy) {
452 ret = 1;
453 break;
454 }
455 if (registered_nexus) {
456 ret = 0;
457 break;
458 }
459 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
460 break;
461 case MI_REPORT_ALIASES:
462 case MI_REPORT_IDENTIFYING_INFORMATION:
463 case MI_REPORT_PRIORITY:
464 case MI_REPORT_TARGET_PGS:
465 case MI_REPORT_TIMESTAMP:
466 ret = 0; /* Allowed */
467 break;
468 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700469 pr_err("Unknown MI Service Action: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800470 (cdb[1] & 0x1f));
Andy Grovere3d6f902011-07-19 08:55:10 +0000471 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800472 }
473 break;
474 case ACCESS_CONTROL_IN:
475 case ACCESS_CONTROL_OUT:
476 case INQUIRY:
477 case LOG_SENSE:
478 case READ_MEDIA_SERIAL_NUMBER:
479 case REPORT_LUNS:
480 case REQUEST_SENSE:
481 ret = 0; /*/ Allowed CDBs */
482 break;
483 default:
484 other_cdb = 1;
485 break;
486 }
487 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300488 * Case where the CDB is explicitly allowed in the above switch
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800489 * statement.
490 */
Andy Grover6708bb22011-06-08 10:36:43 -0700491 if (!ret && !other_cdb) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800492#if 0
Andy Grover6708bb22011-06-08 10:36:43 -0700493 pr_debug("Allowing explict CDB: 0x%02x for %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800494 " reservation holder\n", cdb[0],
495 core_scsi3_pr_dump_type(pr_reg_type));
496#endif
497 return ret;
498 }
499 /*
500 * Check if write exclusive initiator ports *NOT* holding the
501 * WRITE_EXCLUSIVE_* reservation.
502 */
503 if ((we) && !(registered_nexus)) {
504 if (cmd->data_direction == DMA_TO_DEVICE) {
505 /*
506 * Conflict for write exclusive
507 */
Andy Grover6708bb22011-06-08 10:36:43 -0700508 pr_debug("%s Conflict for unregistered nexus"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800509 " %s CDB: 0x%02x to %s reservation\n",
510 transport_dump_cmd_direction(cmd),
511 se_sess->se_node_acl->initiatorname, cdb[0],
512 core_scsi3_pr_dump_type(pr_reg_type));
513 return 1;
514 } else {
515 /*
516 * Allow non WRITE CDBs for all Write Exclusive
517 * PR TYPEs to pass for registered and
518 * non-registered_nexuxes NOT holding the reservation.
519 *
520 * We only make noise for the unregisterd nexuses,
521 * as we expect registered non-reservation holding
522 * nexuses to issue CDBs.
523 */
524#if 0
Andy Grover6708bb22011-06-08 10:36:43 -0700525 if (!registered_nexus) {
526 pr_debug("Allowing implict CDB: 0x%02x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800527 " for %s reservation on unregistered"
528 " nexus\n", cdb[0],
529 core_scsi3_pr_dump_type(pr_reg_type));
530 }
531#endif
532 return 0;
533 }
534 } else if ((reg_only) || (all_reg)) {
535 if (registered_nexus) {
536 /*
537 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
538 * allow commands from registered nexuses.
539 */
540#if 0
Andy Grover6708bb22011-06-08 10:36:43 -0700541 pr_debug("Allowing implict CDB: 0x%02x for %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800542 " reservation\n", cdb[0],
543 core_scsi3_pr_dump_type(pr_reg_type));
544#endif
545 return 0;
546 }
547 }
Andy Grover6708bb22011-06-08 10:36:43 -0700548 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800549 " for %s reservation\n", transport_dump_cmd_direction(cmd),
550 (registered_nexus) ? "" : "un",
551 se_sess->se_node_acl->initiatorname, cdb[0],
552 core_scsi3_pr_dump_type(pr_reg_type));
553
554 return 1; /* Conflict by default */
555}
556
557static u32 core_scsi3_pr_generation(struct se_device *dev)
558{
Andy Grovere3d6f902011-07-19 08:55:10 +0000559 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800560 u32 prg;
561 /*
562 * PRGeneration field shall contain the value of a 32-bit wrapping
563 * counter mainted by the device server.
564 *
565 * Note that this is done regardless of Active Persist across
566 * Target PowerLoss (APTPL)
567 *
568 * See spc4r17 section 6.3.12 READ_KEYS service action
569 */
570 spin_lock(&dev->dev_reservation_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +0000571 prg = su_dev->t10_pr.pr_generation++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800572 spin_unlock(&dev->dev_reservation_lock);
573
574 return prg;
575}
576
577static int core_scsi3_pr_reservation_check(
578 struct se_cmd *cmd,
579 u32 *pr_reg_type)
580{
581 struct se_device *dev = cmd->se_dev;
582 struct se_session *sess = cmd->se_sess;
583 int ret;
584
Andy Grover6708bb22011-06-08 10:36:43 -0700585 if (!sess)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800586 return 0;
587 /*
588 * A legacy SPC-2 reservation is being held.
589 */
590 if (dev->dev_flags & DF_SPC2_RESERVATIONS)
591 return core_scsi2_reservation_check(cmd, pr_reg_type);
592
593 spin_lock(&dev->dev_reservation_lock);
Andy Grover6708bb22011-06-08 10:36:43 -0700594 if (!dev->dev_pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800595 spin_unlock(&dev->dev_reservation_lock);
596 return 0;
597 }
598 *pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
599 cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
600 if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl) {
601 spin_unlock(&dev->dev_reservation_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +0000602 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800603 }
Andy Grover6708bb22011-06-08 10:36:43 -0700604 if (!dev->dev_pr_res_holder->isid_present_at_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800605 spin_unlock(&dev->dev_reservation_lock);
606 return 0;
607 }
608 ret = (dev->dev_pr_res_holder->pr_reg_bin_isid ==
Andy Grovere3d6f902011-07-19 08:55:10 +0000609 sess->sess_bin_isid) ? 0 : -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800610 /*
611 * Use bit in *pr_reg_type to notify ISID mismatch in
612 * core_scsi3_pr_seq_non_holder().
613 */
614 if (ret != 0)
615 *pr_reg_type |= 0x80000000;
616 spin_unlock(&dev->dev_reservation_lock);
617
618 return ret;
619}
620
621static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
622 struct se_device *dev,
623 struct se_node_acl *nacl,
624 struct se_dev_entry *deve,
625 unsigned char *isid,
626 u64 sa_res_key,
627 int all_tg_pt,
628 int aptpl)
629{
Andy Grovere3d6f902011-07-19 08:55:10 +0000630 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800631 struct t10_pr_registration *pr_reg;
632
633 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
Andy Grover6708bb22011-06-08 10:36:43 -0700634 if (!pr_reg) {
635 pr_err("Unable to allocate struct t10_pr_registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800636 return NULL;
637 }
638
Andy Grovere3d6f902011-07-19 08:55:10 +0000639 pr_reg->pr_aptpl_buf = kzalloc(su_dev->t10_pr.pr_aptpl_buf_len,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800640 GFP_ATOMIC);
Andy Grover6708bb22011-06-08 10:36:43 -0700641 if (!pr_reg->pr_aptpl_buf) {
642 pr_err("Unable to allocate pr_reg->pr_aptpl_buf\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800643 kmem_cache_free(t10_pr_reg_cache, pr_reg);
644 return NULL;
645 }
646
647 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
648 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
649 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
650 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
651 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
652 atomic_set(&pr_reg->pr_res_holders, 0);
653 pr_reg->pr_reg_nacl = nacl;
654 pr_reg->pr_reg_deve = deve;
655 pr_reg->pr_res_mapped_lun = deve->mapped_lun;
656 pr_reg->pr_aptpl_target_lun = deve->se_lun->unpacked_lun;
657 pr_reg->pr_res_key = sa_res_key;
658 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
659 pr_reg->pr_reg_aptpl = aptpl;
660 pr_reg->pr_reg_tg_pt_lun = deve->se_lun;
661 /*
662 * If an ISID value for this SCSI Initiator Port exists,
663 * save it to the registration now.
664 */
665 if (isid != NULL) {
666 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
667 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
668 pr_reg->isid_present_at_reg = 1;
669 }
670
671 return pr_reg;
672}
673
674static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
675static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
676
677/*
678 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
679 * modes.
680 */
681static struct t10_pr_registration *__core_scsi3_alloc_registration(
682 struct se_device *dev,
683 struct se_node_acl *nacl,
684 struct se_dev_entry *deve,
685 unsigned char *isid,
686 u64 sa_res_key,
687 int all_tg_pt,
688 int aptpl)
689{
690 struct se_dev_entry *deve_tmp;
691 struct se_node_acl *nacl_tmp;
692 struct se_port *port, *port_tmp;
693 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
694 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
695 int ret;
696 /*
697 * Create a registration for the I_T Nexus upon which the
698 * PROUT REGISTER was received.
699 */
700 pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, deve, isid,
701 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -0700702 if (!pr_reg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800703 return NULL;
704 /*
705 * Return pointer to pr_reg for ALL_TG_PT=0
706 */
Andy Grover6708bb22011-06-08 10:36:43 -0700707 if (!all_tg_pt)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800708 return pr_reg;
709 /*
710 * Create list of matching SCSI Initiator Port registrations
711 * for ALL_TG_PT=1
712 */
713 spin_lock(&dev->se_port_lock);
714 list_for_each_entry_safe(port, port_tmp, &dev->dev_sep_list, sep_list) {
715 atomic_inc(&port->sep_tg_pt_ref_cnt);
716 smp_mb__after_atomic_inc();
717 spin_unlock(&dev->se_port_lock);
718
719 spin_lock_bh(&port->sep_alua_lock);
720 list_for_each_entry(deve_tmp, &port->sep_alua_list,
721 alua_port_list) {
722 /*
723 * This pointer will be NULL for demo mode MappedLUNs
724 * that have not been make explict via a ConfigFS
725 * MappedLUN group for the SCSI Initiator Node ACL.
726 */
Andy Grover6708bb22011-06-08 10:36:43 -0700727 if (!deve_tmp->se_lun_acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800728 continue;
729
730 nacl_tmp = deve_tmp->se_lun_acl->se_lun_nacl;
731 /*
732 * Skip the matching struct se_node_acl that is allocated
733 * above..
734 */
735 if (nacl == nacl_tmp)
736 continue;
737 /*
738 * Only perform PR registrations for target ports on
739 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
740 * arrived.
741 */
742 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
743 continue;
744 /*
745 * Look for a matching Initiator Node ACL in ASCII format
746 */
747 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
748 continue;
749
750 atomic_inc(&deve_tmp->pr_ref_count);
751 smp_mb__after_atomic_inc();
752 spin_unlock_bh(&port->sep_alua_lock);
753 /*
754 * Grab a configfs group dependency that is released
755 * for the exception path at label out: below, or upon
756 * completion of adding ALL_TG_PT=1 registrations in
757 * __core_scsi3_add_registration()
758 */
759 ret = core_scsi3_lunacl_depend_item(deve_tmp);
760 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -0700761 pr_err("core_scsi3_lunacl_depend"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800762 "_item() failed\n");
763 atomic_dec(&port->sep_tg_pt_ref_cnt);
764 smp_mb__after_atomic_dec();
765 atomic_dec(&deve_tmp->pr_ref_count);
766 smp_mb__after_atomic_dec();
767 goto out;
768 }
769 /*
770 * Located a matching SCSI Initiator Port on a different
771 * port, allocate the pr_reg_atp and attach it to the
772 * pr_reg->pr_reg_atp_list that will be processed once
773 * the original *pr_reg is processed in
774 * __core_scsi3_add_registration()
775 */
776 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
777 nacl_tmp, deve_tmp, NULL,
778 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -0700779 if (!pr_reg_atp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800780 atomic_dec(&port->sep_tg_pt_ref_cnt);
781 smp_mb__after_atomic_dec();
782 atomic_dec(&deve_tmp->pr_ref_count);
783 smp_mb__after_atomic_dec();
784 core_scsi3_lunacl_undepend_item(deve_tmp);
785 goto out;
786 }
787
788 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
789 &pr_reg->pr_reg_atp_list);
790 spin_lock_bh(&port->sep_alua_lock);
791 }
792 spin_unlock_bh(&port->sep_alua_lock);
793
794 spin_lock(&dev->se_port_lock);
795 atomic_dec(&port->sep_tg_pt_ref_cnt);
796 smp_mb__after_atomic_dec();
797 }
798 spin_unlock(&dev->se_port_lock);
799
800 return pr_reg;
801out:
802 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
803 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
804 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
805 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
806 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
807 }
808 kmem_cache_free(t10_pr_reg_cache, pr_reg);
809 return NULL;
810}
811
812int core_scsi3_alloc_aptpl_registration(
Andy Grovere3d6f902011-07-19 08:55:10 +0000813 struct t10_reservation *pr_tmpl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800814 u64 sa_res_key,
815 unsigned char *i_port,
816 unsigned char *isid,
817 u32 mapped_lun,
818 unsigned char *t_port,
819 u16 tpgt,
820 u32 target_lun,
821 int res_holder,
822 int all_tg_pt,
823 u8 type)
824{
825 struct t10_pr_registration *pr_reg;
826
Andy Grover6708bb22011-06-08 10:36:43 -0700827 if (!i_port || !t_port || !sa_res_key) {
828 pr_err("Illegal parameters for APTPL registration\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000829 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800830 }
831
832 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700833 if (!pr_reg) {
834 pr_err("Unable to allocate struct t10_pr_registration\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000835 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800836 }
837 pr_reg->pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len, GFP_KERNEL);
838
839 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
840 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
841 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
842 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
843 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
844 atomic_set(&pr_reg->pr_res_holders, 0);
845 pr_reg->pr_reg_nacl = NULL;
846 pr_reg->pr_reg_deve = NULL;
847 pr_reg->pr_res_mapped_lun = mapped_lun;
848 pr_reg->pr_aptpl_target_lun = target_lun;
849 pr_reg->pr_res_key = sa_res_key;
850 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
851 pr_reg->pr_reg_aptpl = 1;
852 pr_reg->pr_reg_tg_pt_lun = NULL;
853 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
854 pr_reg->pr_res_type = type;
855 /*
856 * If an ISID value had been saved in APTPL metadata for this
857 * SCSI Initiator Port, restore it now.
858 */
859 if (isid != NULL) {
860 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
861 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
862 pr_reg->isid_present_at_reg = 1;
863 }
864 /*
865 * Copy the i_port and t_port information from caller.
866 */
867 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
868 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
869 pr_reg->pr_reg_tpgt = tpgt;
870 /*
871 * Set pr_res_holder from caller, the pr_reg who is the reservation
872 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
873 * the Initiator Node LUN ACL from the fabric module is created for
874 * this registration.
875 */
876 pr_reg->pr_res_holder = res_holder;
877
878 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
Andy Grover6708bb22011-06-08 10:36:43 -0700879 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800880 " metadata\n", (res_holder) ? "+reservation" : "");
881 return 0;
882}
883
884static void core_scsi3_aptpl_reserve(
885 struct se_device *dev,
886 struct se_portal_group *tpg,
887 struct se_node_acl *node_acl,
888 struct t10_pr_registration *pr_reg)
889{
890 char i_buf[PR_REG_ISID_ID_LEN];
891 int prf_isid;
892
893 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
894 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
895 PR_REG_ISID_ID_LEN);
896
897 spin_lock(&dev->dev_reservation_lock);
898 dev->dev_pr_res_holder = pr_reg;
899 spin_unlock(&dev->dev_reservation_lock);
900
Andy Grover6708bb22011-06-08 10:36:43 -0700901 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800902 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000903 tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800904 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
905 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -0700906 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000907 tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800908 (prf_isid) ? &i_buf[0] : "");
909}
910
911static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
912 struct t10_pr_registration *, int, int);
913
914static int __core_scsi3_check_aptpl_registration(
915 struct se_device *dev,
916 struct se_portal_group *tpg,
917 struct se_lun *lun,
918 u32 target_lun,
919 struct se_node_acl *nacl,
920 struct se_dev_entry *deve)
921{
922 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
Andy Grovere3d6f902011-07-19 08:55:10 +0000923 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800924 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
925 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
926 u16 tpgt;
927
928 memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
929 memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
930 /*
931 * Copy Initiator Port information from struct se_node_acl
932 */
933 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
934 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
Andy Grovere3d6f902011-07-19 08:55:10 +0000935 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
936 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800937 /*
938 * Look for the matching registrations+reservation from those
939 * created from APTPL metadata. Note that multiple registrations
940 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
941 * TransportIDs.
942 */
943 spin_lock(&pr_tmpl->aptpl_reg_lock);
944 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
945 pr_reg_aptpl_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700946 if (!strcmp(pr_reg->pr_iport, i_port) &&
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800947 (pr_reg->pr_res_mapped_lun == deve->mapped_lun) &&
948 !(strcmp(pr_reg->pr_tport, t_port)) &&
949 (pr_reg->pr_reg_tpgt == tpgt) &&
950 (pr_reg->pr_aptpl_target_lun == target_lun)) {
951
952 pr_reg->pr_reg_nacl = nacl;
953 pr_reg->pr_reg_deve = deve;
954 pr_reg->pr_reg_tg_pt_lun = lun;
955
956 list_del(&pr_reg->pr_reg_aptpl_list);
957 spin_unlock(&pr_tmpl->aptpl_reg_lock);
958 /*
959 * At this point all of the pointers in *pr_reg will
960 * be setup, so go ahead and add the registration.
961 */
962
963 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
964 /*
965 * If this registration is the reservation holder,
966 * make that happen now..
967 */
968 if (pr_reg->pr_res_holder)
969 core_scsi3_aptpl_reserve(dev, tpg,
970 nacl, pr_reg);
971 /*
972 * Reenable pr_aptpl_active to accept new metadata
973 * updates once the SCSI device is active again..
974 */
975 spin_lock(&pr_tmpl->aptpl_reg_lock);
976 pr_tmpl->pr_aptpl_active = 1;
977 }
978 }
979 spin_unlock(&pr_tmpl->aptpl_reg_lock);
980
981 return 0;
982}
983
984int core_scsi3_check_aptpl_registration(
985 struct se_device *dev,
986 struct se_portal_group *tpg,
987 struct se_lun *lun,
988 struct se_lun_acl *lun_acl)
989{
Andy Grovere3d6f902011-07-19 08:55:10 +0000990 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800991 struct se_node_acl *nacl = lun_acl->se_lun_nacl;
992 struct se_dev_entry *deve = &nacl->device_list[lun_acl->mapped_lun];
993
Andy Grovere3d6f902011-07-19 08:55:10 +0000994 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800995 return 0;
996
997 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
998 lun->unpacked_lun, nacl, deve);
999}
1000
1001static void __core_scsi3_dump_registration(
1002 struct target_core_fabric_ops *tfo,
1003 struct se_device *dev,
1004 struct se_node_acl *nacl,
1005 struct t10_pr_registration *pr_reg,
1006 int register_type)
1007{
1008 struct se_portal_group *se_tpg = nacl->se_tpg;
1009 char i_buf[PR_REG_ISID_ID_LEN];
1010 int prf_isid;
1011
1012 memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
1013 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1014 PR_REG_ISID_ID_LEN);
1015
Andy Grover6708bb22011-06-08 10:36:43 -07001016 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001017 " Node: %s%s\n", tfo->get_fabric_name(), (register_type == 2) ?
1018 "_AND_MOVE" : (register_type == 1) ?
1019 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
1020 (prf_isid) ? i_buf : "");
Andy Grover6708bb22011-06-08 10:36:43 -07001021 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001022 tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg),
1023 tfo->tpg_get_tag(se_tpg));
Andy Grover6708bb22011-06-08 10:36:43 -07001024 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001025 " Port(s)\n", tfo->get_fabric_name(),
1026 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
Andy Grovere3d6f902011-07-19 08:55:10 +00001027 dev->transport->name);
Andy Grover6708bb22011-06-08 10:36:43 -07001028 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001029 " 0x%08x APTPL: %d\n", tfo->get_fabric_name(),
1030 pr_reg->pr_res_key, pr_reg->pr_res_generation,
1031 pr_reg->pr_reg_aptpl);
1032}
1033
1034/*
1035 * this function can be called with struct se_device->dev_reservation_lock
1036 * when register_move = 1
1037 */
1038static void __core_scsi3_add_registration(
1039 struct se_device *dev,
1040 struct se_node_acl *nacl,
1041 struct t10_pr_registration *pr_reg,
1042 int register_type,
1043 int register_move)
1044{
Andy Grovere3d6f902011-07-19 08:55:10 +00001045 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001046 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1047 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
Andy Grovere3d6f902011-07-19 08:55:10 +00001048 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001049
1050 /*
1051 * Increment PRgeneration counter for struct se_device upon a successful
1052 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1053 *
1054 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1055 * action, the struct se_device->dev_reservation_lock will already be held,
1056 * so we do not call core_scsi3_pr_generation() which grabs the lock
1057 * for the REGISTER.
1058 */
1059 pr_reg->pr_res_generation = (register_move) ?
Andy Grovere3d6f902011-07-19 08:55:10 +00001060 su_dev->t10_pr.pr_generation++ :
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001061 core_scsi3_pr_generation(dev);
1062
1063 spin_lock(&pr_tmpl->registration_lock);
1064 list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
1065 pr_reg->pr_reg_deve->def_pr_registered = 1;
1066
1067 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1068 spin_unlock(&pr_tmpl->registration_lock);
1069 /*
1070 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1071 */
Andy Grover6708bb22011-06-08 10:36:43 -07001072 if (!pr_reg->pr_reg_all_tg_pt || register_move)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001073 return;
1074 /*
1075 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1076 * allocated in __core_scsi3_alloc_registration()
1077 */
1078 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1079 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
1080 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1081
1082 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1083
1084 spin_lock(&pr_tmpl->registration_lock);
1085 list_add_tail(&pr_reg_tmp->pr_reg_list,
1086 &pr_tmpl->registration_list);
1087 pr_reg_tmp->pr_reg_deve->def_pr_registered = 1;
1088
1089 __core_scsi3_dump_registration(tfo, dev,
1090 pr_reg_tmp->pr_reg_nacl, pr_reg_tmp,
1091 register_type);
1092 spin_unlock(&pr_tmpl->registration_lock);
1093 /*
1094 * Drop configfs group dependency reference from
1095 * __core_scsi3_alloc_registration()
1096 */
1097 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1098 }
1099}
1100
1101static int core_scsi3_alloc_registration(
1102 struct se_device *dev,
1103 struct se_node_acl *nacl,
1104 struct se_dev_entry *deve,
1105 unsigned char *isid,
1106 u64 sa_res_key,
1107 int all_tg_pt,
1108 int aptpl,
1109 int register_type,
1110 int register_move)
1111{
1112 struct t10_pr_registration *pr_reg;
1113
1114 pr_reg = __core_scsi3_alloc_registration(dev, nacl, deve, isid,
1115 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001116 if (!pr_reg)
Andy Grovere3d6f902011-07-19 08:55:10 +00001117 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001118
1119 __core_scsi3_add_registration(dev, nacl, pr_reg,
1120 register_type, register_move);
1121 return 0;
1122}
1123
1124static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1125 struct se_device *dev,
1126 struct se_node_acl *nacl,
1127 unsigned char *isid)
1128{
Andy Grovere3d6f902011-07-19 08:55:10 +00001129 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001130 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1131 struct se_portal_group *tpg;
1132
1133 spin_lock(&pr_tmpl->registration_lock);
1134 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1135 &pr_tmpl->registration_list, pr_reg_list) {
1136 /*
1137 * First look for a matching struct se_node_acl
1138 */
1139 if (pr_reg->pr_reg_nacl != nacl)
1140 continue;
1141
1142 tpg = pr_reg->pr_reg_nacl->se_tpg;
1143 /*
1144 * If this registration does NOT contain a fabric provided
1145 * ISID, then we have found a match.
1146 */
Andy Grover6708bb22011-06-08 10:36:43 -07001147 if (!pr_reg->isid_present_at_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001148 /*
1149 * Determine if this SCSI device server requires that
1150 * SCSI Intiatior TransportID w/ ISIDs is enforced
1151 * for fabric modules (iSCSI) requiring them.
1152 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001153 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1154 if (dev->se_sub_dev->se_dev_attrib.enforce_pr_isids)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001155 continue;
1156 }
1157 atomic_inc(&pr_reg->pr_res_holders);
1158 smp_mb__after_atomic_inc();
1159 spin_unlock(&pr_tmpl->registration_lock);
1160 return pr_reg;
1161 }
1162 /*
1163 * If the *pr_reg contains a fabric defined ISID for multi-value
1164 * SCSI Initiator Port TransportIDs, then we expect a valid
1165 * matching ISID to be provided by the local SCSI Initiator Port.
1166 */
Andy Grover6708bb22011-06-08 10:36:43 -07001167 if (!isid)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001168 continue;
1169 if (strcmp(isid, pr_reg->pr_reg_isid))
1170 continue;
1171
1172 atomic_inc(&pr_reg->pr_res_holders);
1173 smp_mb__after_atomic_inc();
1174 spin_unlock(&pr_tmpl->registration_lock);
1175 return pr_reg;
1176 }
1177 spin_unlock(&pr_tmpl->registration_lock);
1178
1179 return NULL;
1180}
1181
1182static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1183 struct se_device *dev,
1184 struct se_node_acl *nacl,
1185 struct se_session *sess)
1186{
1187 struct se_portal_group *tpg = nacl->se_tpg;
1188 unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1189
Andy Grovere3d6f902011-07-19 08:55:10 +00001190 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001191 memset(&buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +00001192 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001193 PR_REG_ISID_LEN);
1194 isid_ptr = &buf[0];
1195 }
1196
1197 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1198}
1199
1200static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1201{
1202 atomic_dec(&pr_reg->pr_res_holders);
1203 smp_mb__after_atomic_dec();
1204}
1205
1206static int core_scsi3_check_implict_release(
1207 struct se_device *dev,
1208 struct t10_pr_registration *pr_reg)
1209{
1210 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1211 struct t10_pr_registration *pr_res_holder;
1212 int ret = 0;
1213
1214 spin_lock(&dev->dev_reservation_lock);
1215 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001216 if (!pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001217 spin_unlock(&dev->dev_reservation_lock);
1218 return ret;
1219 }
1220 if (pr_res_holder == pr_reg) {
1221 /*
1222 * Perform an implict RELEASE if the registration that
1223 * is being released is holding the reservation.
1224 *
1225 * From spc4r17, section 5.7.11.1:
1226 *
1227 * e) If the I_T nexus is the persistent reservation holder
1228 * and the persistent reservation is not an all registrants
1229 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1230 * service action or REGISTER AND IGNORE EXISTING KEY
1231 * service action with the SERVICE ACTION RESERVATION KEY
1232 * field set to zero (see 5.7.11.3).
1233 */
1234 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0);
1235 ret = 1;
1236 /*
1237 * For 'All Registrants' reservation types, all existing
1238 * registrations are still processed as reservation holders
1239 * in core_scsi3_pr_seq_non_holder() after the initial
1240 * reservation holder is implictly released here.
1241 */
1242 } else if (pr_reg->pr_reg_all_tg_pt &&
1243 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1244 pr_reg->pr_reg_nacl->initiatorname)) &&
1245 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001246 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001247 " UNREGISTER while existing reservation with matching"
1248 " key 0x%016Lx is present from another SCSI Initiator"
1249 " Port\n", pr_reg->pr_res_key);
Andy Grovere3d6f902011-07-19 08:55:10 +00001250 ret = -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001251 }
1252 spin_unlock(&dev->dev_reservation_lock);
1253
1254 return ret;
1255}
1256
1257/*
Andy Grovere3d6f902011-07-19 08:55:10 +00001258 * Called with struct t10_reservation->registration_lock held.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001259 */
1260static void __core_scsi3_free_registration(
1261 struct se_device *dev,
1262 struct t10_pr_registration *pr_reg,
1263 struct list_head *preempt_and_abort_list,
1264 int dec_holders)
1265{
1266 struct target_core_fabric_ops *tfo =
1267 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Andy Grovere3d6f902011-07-19 08:55:10 +00001268 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001269 char i_buf[PR_REG_ISID_ID_LEN];
1270 int prf_isid;
1271
1272 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1273 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1274 PR_REG_ISID_ID_LEN);
1275
1276 pr_reg->pr_reg_deve->def_pr_registered = 0;
1277 pr_reg->pr_reg_deve->pr_res_key = 0;
1278 list_del(&pr_reg->pr_reg_list);
1279 /*
1280 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1281 * so call core_scsi3_put_pr_reg() to decrement our reference.
1282 */
1283 if (dec_holders)
1284 core_scsi3_put_pr_reg(pr_reg);
1285 /*
1286 * Wait until all reference from any other I_T nexuses for this
1287 * *pr_reg have been released. Because list_del() is called above,
1288 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1289 * count back to zero, and we release *pr_reg.
1290 */
1291 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
1292 spin_unlock(&pr_tmpl->registration_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07001293 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001294 tfo->get_fabric_name());
1295 cpu_relax();
1296 spin_lock(&pr_tmpl->registration_lock);
1297 }
1298
Andy Grover6708bb22011-06-08 10:36:43 -07001299 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001300 " Node: %s%s\n", tfo->get_fabric_name(),
1301 pr_reg->pr_reg_nacl->initiatorname,
1302 (prf_isid) ? &i_buf[0] : "");
Andy Grover6708bb22011-06-08 10:36:43 -07001303 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001304 " Port(s)\n", tfo->get_fabric_name(),
1305 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
Andy Grovere3d6f902011-07-19 08:55:10 +00001306 dev->transport->name);
Andy Grover6708bb22011-06-08 10:36:43 -07001307 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001308 " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key,
1309 pr_reg->pr_res_generation);
1310
Andy Grover6708bb22011-06-08 10:36:43 -07001311 if (!preempt_and_abort_list) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001312 pr_reg->pr_reg_deve = NULL;
1313 pr_reg->pr_reg_nacl = NULL;
1314 kfree(pr_reg->pr_aptpl_buf);
1315 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1316 return;
1317 }
1318 /*
1319 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1320 * are released once the ABORT_TASK_SET has completed..
1321 */
1322 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1323}
1324
1325void core_scsi3_free_pr_reg_from_nacl(
1326 struct se_device *dev,
1327 struct se_node_acl *nacl)
1328{
Andy Grovere3d6f902011-07-19 08:55:10 +00001329 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001330 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1331 /*
1332 * If the passed se_node_acl matches the reservation holder,
1333 * release the reservation.
1334 */
1335 spin_lock(&dev->dev_reservation_lock);
1336 pr_res_holder = dev->dev_pr_res_holder;
1337 if ((pr_res_holder != NULL) &&
1338 (pr_res_holder->pr_reg_nacl == nacl))
1339 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0);
1340 spin_unlock(&dev->dev_reservation_lock);
1341 /*
1342 * Release any registration associated with the struct se_node_acl.
1343 */
1344 spin_lock(&pr_tmpl->registration_lock);
1345 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1346 &pr_tmpl->registration_list, pr_reg_list) {
1347
1348 if (pr_reg->pr_reg_nacl != nacl)
1349 continue;
1350
1351 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1352 }
1353 spin_unlock(&pr_tmpl->registration_lock);
1354}
1355
1356void core_scsi3_free_all_registrations(
1357 struct se_device *dev)
1358{
Andy Grovere3d6f902011-07-19 08:55:10 +00001359 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001360 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1361
1362 spin_lock(&dev->dev_reservation_lock);
1363 pr_res_holder = dev->dev_pr_res_holder;
1364 if (pr_res_holder != NULL) {
1365 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1366 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
1367 pr_res_holder, 0);
1368 }
1369 spin_unlock(&dev->dev_reservation_lock);
1370
1371 spin_lock(&pr_tmpl->registration_lock);
1372 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1373 &pr_tmpl->registration_list, pr_reg_list) {
1374
1375 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1376 }
1377 spin_unlock(&pr_tmpl->registration_lock);
1378
1379 spin_lock(&pr_tmpl->aptpl_reg_lock);
1380 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1381 pr_reg_aptpl_list) {
1382 list_del(&pr_reg->pr_reg_aptpl_list);
1383 kfree(pr_reg->pr_aptpl_buf);
1384 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1385 }
1386 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1387}
1388
1389static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1390{
Andy Grovere3d6f902011-07-19 08:55:10 +00001391 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001392 &tpg->tpg_group.cg_item);
1393}
1394
1395static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1396{
Andy Grovere3d6f902011-07-19 08:55:10 +00001397 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001398 &tpg->tpg_group.cg_item);
1399
1400 atomic_dec(&tpg->tpg_pr_ref_count);
1401 smp_mb__after_atomic_dec();
1402}
1403
1404static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1405{
1406 struct se_portal_group *tpg = nacl->se_tpg;
1407
1408 if (nacl->dynamic_node_acl)
1409 return 0;
1410
Andy Grovere3d6f902011-07-19 08:55:10 +00001411 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001412 &nacl->acl_group.cg_item);
1413}
1414
1415static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1416{
1417 struct se_portal_group *tpg = nacl->se_tpg;
1418
1419 if (nacl->dynamic_node_acl) {
1420 atomic_dec(&nacl->acl_pr_ref_count);
1421 smp_mb__after_atomic_dec();
1422 return;
1423 }
1424
Andy Grovere3d6f902011-07-19 08:55:10 +00001425 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001426 &nacl->acl_group.cg_item);
1427
1428 atomic_dec(&nacl->acl_pr_ref_count);
1429 smp_mb__after_atomic_dec();
1430}
1431
1432static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1433{
1434 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1435 struct se_node_acl *nacl;
1436 struct se_portal_group *tpg;
1437 /*
1438 * For nacl->dynamic_node_acl=1
1439 */
Andy Grover6708bb22011-06-08 10:36:43 -07001440 if (!lun_acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001441 return 0;
1442
1443 nacl = lun_acl->se_lun_nacl;
1444 tpg = nacl->se_tpg;
1445
Andy Grovere3d6f902011-07-19 08:55:10 +00001446 return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001447 &lun_acl->se_lun_group.cg_item);
1448}
1449
1450static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1451{
1452 struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1453 struct se_node_acl *nacl;
1454 struct se_portal_group *tpg;
1455 /*
1456 * For nacl->dynamic_node_acl=1
1457 */
Andy Grover6708bb22011-06-08 10:36:43 -07001458 if (!lun_acl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001459 atomic_dec(&se_deve->pr_ref_count);
1460 smp_mb__after_atomic_dec();
1461 return;
1462 }
1463 nacl = lun_acl->se_lun_nacl;
1464 tpg = nacl->se_tpg;
1465
Andy Grovere3d6f902011-07-19 08:55:10 +00001466 configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001467 &lun_acl->se_lun_group.cg_item);
1468
1469 atomic_dec(&se_deve->pr_ref_count);
1470 smp_mb__after_atomic_dec();
1471}
1472
1473static int core_scsi3_decode_spec_i_port(
1474 struct se_cmd *cmd,
1475 struct se_portal_group *tpg,
1476 unsigned char *l_isid,
1477 u64 sa_res_key,
1478 int all_tg_pt,
1479 int aptpl)
1480{
Andy Grover5951146d2011-07-19 10:26:37 +00001481 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001482 struct se_port *tmp_port;
1483 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00001484 struct se_session *se_sess = cmd->se_sess;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001485 struct se_node_acl *dest_node_acl = NULL;
1486 struct se_dev_entry *dest_se_deve = NULL, *local_se_deve;
1487 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1488 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1489 struct list_head tid_dest_list;
1490 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
1491 struct target_core_fabric_ops *tmp_tf_ops;
Andy Grover05d1c7c2011-07-20 19:13:28 +00001492 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001493 unsigned char *ptr, *i_str = NULL, proto_ident, tmp_proto_ident;
1494 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
1495 u32 tpdl, tid_len = 0;
1496 int ret, dest_local_nexus, prf_isid;
1497 u32 dest_rtpi = 0;
1498
1499 memset(dest_iport, 0, 64);
1500 INIT_LIST_HEAD(&tid_dest_list);
1501
1502 local_se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
1503 /*
1504 * Allocate a struct pr_transport_id_holder and setup the
1505 * local_node_acl and local_se_deve pointers and add to
1506 * struct list_head tid_dest_list for add registration
1507 * processing in the loop of tid_dest_list below.
1508 */
1509 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001510 if (!tidh_new) {
1511 pr_err("Unable to allocate tidh_new\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001512 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1513 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001514 }
1515 INIT_LIST_HEAD(&tidh_new->dest_list);
1516 tidh_new->dest_tpg = tpg;
1517 tidh_new->dest_node_acl = se_sess->se_node_acl;
1518 tidh_new->dest_se_deve = local_se_deve;
1519
Andy Grover5951146d2011-07-19 10:26:37 +00001520 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001521 se_sess->se_node_acl, local_se_deve, l_isid,
1522 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001523 if (!local_pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001524 kfree(tidh_new);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001525 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1526 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001527 }
1528 tidh_new->dest_pr_reg = local_pr_reg;
1529 /*
1530 * The local I_T nexus does not hold any configfs dependances,
1531 * so we set tid_h->dest_local_nexus=1 to prevent the
1532 * configfs_undepend_item() calls in the tid_dest_list loops below.
1533 */
1534 tidh_new->dest_local_nexus = 1;
1535 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
Andy Grover05d1c7c2011-07-20 19:13:28 +00001536
1537 buf = transport_kmap_first_data_page(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001538 /*
1539 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1540 * first extract TransportID Parameter Data Length, and make sure
1541 * the value matches up to the SCSI expected data transfer length.
1542 */
1543 tpdl = (buf[24] & 0xff) << 24;
1544 tpdl |= (buf[25] & 0xff) << 16;
1545 tpdl |= (buf[26] & 0xff) << 8;
1546 tpdl |= buf[27] & 0xff;
1547
1548 if ((tpdl + 28) != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07001549 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001550 " does not equal CDB data_length: %u\n", tpdl,
1551 cmd->data_length);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001552 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1553 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001554 goto out;
1555 }
1556 /*
1557 * Start processing the received transport IDs using the
1558 * receiving I_T Nexus portal's fabric dependent methods to
1559 * obtain the SCSI Initiator Port/Device Identifiers.
1560 */
1561 ptr = &buf[28];
1562
1563 while (tpdl > 0) {
1564 proto_ident = (ptr[0] & 0x0f);
1565 dest_tpg = NULL;
1566
1567 spin_lock(&dev->se_port_lock);
1568 list_for_each_entry(tmp_port, &dev->dev_sep_list, sep_list) {
1569 tmp_tpg = tmp_port->sep_tpg;
Andy Grover6708bb22011-06-08 10:36:43 -07001570 if (!tmp_tpg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001571 continue;
Andy Grovere3d6f902011-07-19 08:55:10 +00001572 tmp_tf_ops = tmp_tpg->se_tpg_tfo;
Andy Grover6708bb22011-06-08 10:36:43 -07001573 if (!tmp_tf_ops)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001574 continue;
Andy Grover6708bb22011-06-08 10:36:43 -07001575 if (!tmp_tf_ops->get_fabric_proto_ident ||
1576 !tmp_tf_ops->tpg_parse_pr_out_transport_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001577 continue;
1578 /*
1579 * Look for the matching proto_ident provided by
1580 * the received TransportID
1581 */
1582 tmp_proto_ident = tmp_tf_ops->get_fabric_proto_ident(tmp_tpg);
1583 if (tmp_proto_ident != proto_ident)
1584 continue;
1585 dest_rtpi = tmp_port->sep_rtpi;
1586
1587 i_str = tmp_tf_ops->tpg_parse_pr_out_transport_id(
1588 tmp_tpg, (const char *)ptr, &tid_len,
1589 &iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07001590 if (!i_str)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001591 continue;
1592
1593 atomic_inc(&tmp_tpg->tpg_pr_ref_count);
1594 smp_mb__after_atomic_inc();
1595 spin_unlock(&dev->se_port_lock);
1596
1597 ret = core_scsi3_tpg_depend_item(tmp_tpg);
1598 if (ret != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001599 pr_err(" core_scsi3_tpg_depend_item()"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001600 " for tmp_tpg\n");
1601 atomic_dec(&tmp_tpg->tpg_pr_ref_count);
1602 smp_mb__after_atomic_dec();
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001603 cmd->scsi_sense_reason =
1604 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1605 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001606 goto out;
1607 }
1608 /*
1609 * Locate the desination initiator ACL to be registered
1610 * from the decoded fabric module specific TransportID
1611 * at *i_str.
1612 */
Roland Dreier286388872011-08-16 09:40:01 -07001613 spin_lock_irq(&tmp_tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001614 dest_node_acl = __core_tpg_get_initiator_node_acl(
1615 tmp_tpg, i_str);
1616 if (dest_node_acl) {
1617 atomic_inc(&dest_node_acl->acl_pr_ref_count);
1618 smp_mb__after_atomic_inc();
1619 }
Roland Dreier286388872011-08-16 09:40:01 -07001620 spin_unlock_irq(&tmp_tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001621
Andy Grover6708bb22011-06-08 10:36:43 -07001622 if (!dest_node_acl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001623 core_scsi3_tpg_undepend_item(tmp_tpg);
1624 spin_lock(&dev->se_port_lock);
1625 continue;
1626 }
1627
1628 ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
1629 if (ret != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001630 pr_err("configfs_depend_item() failed"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001631 " for dest_node_acl->acl_group\n");
1632 atomic_dec(&dest_node_acl->acl_pr_ref_count);
1633 smp_mb__after_atomic_dec();
1634 core_scsi3_tpg_undepend_item(tmp_tpg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001635 cmd->scsi_sense_reason =
1636 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1637 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001638 goto out;
1639 }
1640
1641 dest_tpg = tmp_tpg;
Andy Grover6708bb22011-06-08 10:36:43 -07001642 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001643 " %s Port RTPI: %hu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001644 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001645 dest_node_acl->initiatorname, dest_rtpi);
1646
1647 spin_lock(&dev->se_port_lock);
1648 break;
1649 }
1650 spin_unlock(&dev->se_port_lock);
1651
Andy Grover6708bb22011-06-08 10:36:43 -07001652 if (!dest_tpg) {
1653 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001654 " dest_tpg\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001655 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1656 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001657 goto out;
1658 }
1659#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07001660 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001661 " tid_len: %d for %s + %s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001662 dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001663 tpdl, tid_len, i_str, iport_ptr);
1664#endif
1665 if (tid_len > tpdl) {
Andy Grover6708bb22011-06-08 10:36:43 -07001666 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001667 " %u for Transport ID: %s\n", tid_len, ptr);
1668 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1669 core_scsi3_tpg_undepend_item(dest_tpg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001670 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1671 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001672 goto out;
1673 }
1674 /*
1675 * Locate the desintation struct se_dev_entry pointer for matching
1676 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1677 * Target Port.
1678 */
1679 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1680 dest_rtpi);
Andy Grover6708bb22011-06-08 10:36:43 -07001681 if (!dest_se_deve) {
1682 pr_err("Unable to locate %s dest_se_deve"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001683 " from destination RTPI: %hu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001684 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001685 dest_rtpi);
1686
1687 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1688 core_scsi3_tpg_undepend_item(dest_tpg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001689 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1690 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001691 goto out;
1692 }
1693
1694 ret = core_scsi3_lunacl_depend_item(dest_se_deve);
1695 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001696 pr_err("core_scsi3_lunacl_depend_item()"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001697 " failed\n");
1698 atomic_dec(&dest_se_deve->pr_ref_count);
1699 smp_mb__after_atomic_dec();
1700 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1701 core_scsi3_tpg_undepend_item(dest_tpg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001702 cmd->scsi_sense_reason =
1703 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1704 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001705 goto out;
1706 }
1707#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07001708 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001709 " dest_se_deve mapped_lun: %u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001710 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001711 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
1712#endif
1713 /*
1714 * Skip any TransportIDs that already have a registration for
1715 * this target port.
1716 */
1717 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1718 iport_ptr);
1719 if (pr_reg_e) {
1720 core_scsi3_put_pr_reg(pr_reg_e);
1721 core_scsi3_lunacl_undepend_item(dest_se_deve);
1722 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1723 core_scsi3_tpg_undepend_item(dest_tpg);
1724 ptr += tid_len;
1725 tpdl -= tid_len;
1726 tid_len = 0;
1727 continue;
1728 }
1729 /*
1730 * Allocate a struct pr_transport_id_holder and setup
1731 * the dest_node_acl and dest_se_deve pointers for the
1732 * loop below.
1733 */
1734 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1735 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001736 if (!tidh_new) {
1737 pr_err("Unable to allocate tidh_new\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001738 core_scsi3_lunacl_undepend_item(dest_se_deve);
1739 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1740 core_scsi3_tpg_undepend_item(dest_tpg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001741 cmd->scsi_sense_reason =
1742 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1743 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001744 goto out;
1745 }
1746 INIT_LIST_HEAD(&tidh_new->dest_list);
1747 tidh_new->dest_tpg = dest_tpg;
1748 tidh_new->dest_node_acl = dest_node_acl;
1749 tidh_new->dest_se_deve = dest_se_deve;
1750
1751 /*
1752 * Allocate, but do NOT add the registration for the
1753 * TransportID referenced SCSI Initiator port. This
1754 * done because of the following from spc4r17 in section
1755 * 6.14.3 wrt SPEC_I_PT:
1756 *
1757 * "If a registration fails for any initiator port (e.g., if th
1758 * logical unit does not have enough resources available to
1759 * hold the registration information), no registrations shall be
1760 * made, and the command shall be terminated with
1761 * CHECK CONDITION status."
1762 *
1763 * That means we call __core_scsi3_alloc_registration() here,
1764 * and then call __core_scsi3_add_registration() in the
1765 * 2nd loop which will never fail.
1766 */
Andy Grover5951146d2011-07-19 10:26:37 +00001767 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001768 dest_node_acl, dest_se_deve, iport_ptr,
1769 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001770 if (!dest_pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001771 core_scsi3_lunacl_undepend_item(dest_se_deve);
1772 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1773 core_scsi3_tpg_undepend_item(dest_tpg);
1774 kfree(tidh_new);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001775 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1776 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001777 goto out;
1778 }
1779 tidh_new->dest_pr_reg = dest_pr_reg;
1780 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1781
1782 ptr += tid_len;
1783 tpdl -= tid_len;
1784 tid_len = 0;
1785
1786 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00001787
1788 transport_kunmap_first_data_page(cmd);
1789
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001790 /*
1791 * Go ahead and create a registrations from tid_dest_list for the
1792 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1793 * and dest_se_deve.
1794 *
1795 * The SA Reservation Key from the PROUT is set for the
1796 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1797 * means that the TransportID Initiator port will be
1798 * registered on all of the target ports in the SCSI target device
1799 * ALL_TG_PT=0 means the registration will only be for the
1800 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1801 * was received.
1802 */
1803 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1804 dest_tpg = tidh->dest_tpg;
1805 dest_node_acl = tidh->dest_node_acl;
1806 dest_se_deve = tidh->dest_se_deve;
1807 dest_pr_reg = tidh->dest_pr_reg;
1808 dest_local_nexus = tidh->dest_local_nexus;
1809
1810 list_del(&tidh->dest_list);
1811 kfree(tidh);
1812
1813 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1814 prf_isid = core_pr_dump_initiator_port(dest_pr_reg, &i_buf[0],
1815 PR_REG_ISID_ID_LEN);
1816
Andy Grover5951146d2011-07-19 10:26:37 +00001817 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001818 dest_pr_reg, 0, 0);
1819
Andy Grover6708bb22011-06-08 10:36:43 -07001820 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001821 " registered Transport ID for Node: %s%s Mapped LUN:"
Andy Grovere3d6f902011-07-19 08:55:10 +00001822 " %u\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001823 dest_node_acl->initiatorname, (prf_isid) ?
1824 &i_buf[0] : "", dest_se_deve->mapped_lun);
1825
1826 if (dest_local_nexus)
1827 continue;
1828
1829 core_scsi3_lunacl_undepend_item(dest_se_deve);
1830 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1831 core_scsi3_tpg_undepend_item(dest_tpg);
1832 }
1833
1834 return 0;
1835out:
Andy Grover05d1c7c2011-07-20 19:13:28 +00001836 transport_kunmap_first_data_page(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001837 /*
1838 * For the failure case, release everything from tid_dest_list
1839 * including *dest_pr_reg and the configfs dependances..
1840 */
1841 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1842 dest_tpg = tidh->dest_tpg;
1843 dest_node_acl = tidh->dest_node_acl;
1844 dest_se_deve = tidh->dest_se_deve;
1845 dest_pr_reg = tidh->dest_pr_reg;
1846 dest_local_nexus = tidh->dest_local_nexus;
1847
1848 list_del(&tidh->dest_list);
1849 kfree(tidh);
1850 /*
1851 * Release any extra ALL_TG_PT=1 registrations for
1852 * the SPEC_I_PT=1 case.
1853 */
1854 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1855 &dest_pr_reg->pr_reg_atp_list,
1856 pr_reg_atp_mem_list) {
1857 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1858 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1859 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1860 }
1861
1862 kfree(dest_pr_reg->pr_aptpl_buf);
1863 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1864
1865 if (dest_local_nexus)
1866 continue;
1867
1868 core_scsi3_lunacl_undepend_item(dest_se_deve);
1869 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1870 core_scsi3_tpg_undepend_item(dest_tpg);
1871 }
1872 return ret;
1873}
1874
1875/*
1876 * Called with struct se_device->dev_reservation_lock held
1877 */
1878static int __core_scsi3_update_aptpl_buf(
1879 struct se_device *dev,
1880 unsigned char *buf,
1881 u32 pr_aptpl_buf_len,
1882 int clear_aptpl_metadata)
1883{
1884 struct se_lun *lun;
1885 struct se_portal_group *tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00001886 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001887 struct t10_pr_registration *pr_reg;
1888 unsigned char tmp[512], isid_buf[32];
1889 ssize_t len = 0;
1890 int reg_count = 0;
1891
1892 memset(buf, 0, pr_aptpl_buf_len);
1893 /*
1894 * Called to clear metadata once APTPL has been deactivated.
1895 */
1896 if (clear_aptpl_metadata) {
1897 snprintf(buf, pr_aptpl_buf_len,
1898 "No Registrations or Reservations\n");
1899 return 0;
1900 }
1901 /*
1902 * Walk the registration list..
1903 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001904 spin_lock(&su_dev->t10_pr.registration_lock);
1905 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001906 pr_reg_list) {
1907
1908 tmp[0] = '\0';
1909 isid_buf[0] = '\0';
1910 tpg = pr_reg->pr_reg_nacl->se_tpg;
1911 lun = pr_reg->pr_reg_tg_pt_lun;
1912 /*
1913 * Write out any ISID value to APTPL metadata that was included
1914 * in the original registration.
1915 */
1916 if (pr_reg->isid_present_at_reg)
1917 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1918 pr_reg->pr_reg_isid);
1919 /*
1920 * Include special metadata if the pr_reg matches the
1921 * reservation holder.
1922 */
1923 if (dev->dev_pr_res_holder == pr_reg) {
1924 snprintf(tmp, 512, "PR_REG_START: %d"
1925 "\ninitiator_fabric=%s\n"
1926 "initiator_node=%s\n%s"
1927 "sa_res_key=%llu\n"
1928 "res_holder=1\nres_type=%02x\n"
1929 "res_scope=%02x\nres_all_tg_pt=%d\n"
1930 "mapped_lun=%u\n", reg_count,
Andy Grovere3d6f902011-07-19 08:55:10 +00001931 tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001932 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1933 pr_reg->pr_res_key, pr_reg->pr_res_type,
1934 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1935 pr_reg->pr_res_mapped_lun);
1936 } else {
1937 snprintf(tmp, 512, "PR_REG_START: %d\n"
1938 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1939 "sa_res_key=%llu\nres_holder=0\n"
1940 "res_all_tg_pt=%d\nmapped_lun=%u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001941 reg_count, tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001942 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1943 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1944 pr_reg->pr_res_mapped_lun);
1945 }
1946
Dan Carpenter60d645a2011-06-15 10:03:05 -07001947 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001948 pr_err("Unable to update renaming"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001949 " APTPL metadata\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001950 spin_unlock(&su_dev->t10_pr.registration_lock);
1951 return -EMSGSIZE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001952 }
1953 len += sprintf(buf+len, "%s", tmp);
1954
1955 /*
1956 * Include information about the associated SCSI target port.
1957 */
1958 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1959 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
Andy Grovere3d6f902011-07-19 08:55:10 +00001960 " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
1961 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1962 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001963 lun->lun_sep->sep_rtpi, lun->unpacked_lun, reg_count);
1964
Dan Carpenter60d645a2011-06-15 10:03:05 -07001965 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001966 pr_err("Unable to update renaming"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001967 " APTPL metadata\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001968 spin_unlock(&su_dev->t10_pr.registration_lock);
1969 return -EMSGSIZE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001970 }
1971 len += sprintf(buf+len, "%s", tmp);
1972 reg_count++;
1973 }
Andy Grovere3d6f902011-07-19 08:55:10 +00001974 spin_unlock(&su_dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001975
Andy Grover6708bb22011-06-08 10:36:43 -07001976 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001977 len += sprintf(buf+len, "No Registrations or Reservations");
1978
1979 return 0;
1980}
1981
1982static int core_scsi3_update_aptpl_buf(
1983 struct se_device *dev,
1984 unsigned char *buf,
1985 u32 pr_aptpl_buf_len,
1986 int clear_aptpl_metadata)
1987{
1988 int ret;
1989
1990 spin_lock(&dev->dev_reservation_lock);
1991 ret = __core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
1992 clear_aptpl_metadata);
1993 spin_unlock(&dev->dev_reservation_lock);
1994
1995 return ret;
1996}
1997
1998/*
1999 * Called with struct se_device->aptpl_file_mutex held
2000 */
2001static int __core_scsi3_write_aptpl_to_file(
2002 struct se_device *dev,
2003 unsigned char *buf,
2004 u32 pr_aptpl_buf_len)
2005{
Andy Grovere3d6f902011-07-19 08:55:10 +00002006 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002007 struct file *file;
2008 struct iovec iov[1];
2009 mm_segment_t old_fs;
2010 int flags = O_RDWR | O_CREAT | O_TRUNC;
2011 char path[512];
2012 int ret;
2013
2014 memset(iov, 0, sizeof(struct iovec));
2015 memset(path, 0, 512);
2016
Dan Carpenter60d645a2011-06-15 10:03:05 -07002017 if (strlen(&wwn->unit_serial[0]) >= 512) {
Andy Grover6708bb22011-06-08 10:36:43 -07002018 pr_err("WWN value for struct se_device does not fit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002019 " into path buffer\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00002020 return -EMSGSIZE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002021 }
2022
2023 snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]);
2024 file = filp_open(path, flags, 0600);
2025 if (IS_ERR(file) || !file || !file->f_dentry) {
Andy Grover6708bb22011-06-08 10:36:43 -07002026 pr_err("filp_open(%s) for APTPL metadata"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002027 " failed\n", path);
Andy Grovere3d6f902011-07-19 08:55:10 +00002028 return (PTR_ERR(file) < 0 ? PTR_ERR(file) : -ENOENT);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002029 }
2030
2031 iov[0].iov_base = &buf[0];
Andy Grover6708bb22011-06-08 10:36:43 -07002032 if (!pr_aptpl_buf_len)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002033 iov[0].iov_len = (strlen(&buf[0]) + 1); /* Add extra for NULL */
2034 else
2035 iov[0].iov_len = pr_aptpl_buf_len;
2036
2037 old_fs = get_fs();
2038 set_fs(get_ds());
2039 ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
2040 set_fs(old_fs);
2041
2042 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002043 pr_debug("Error writing APTPL metadata file: %s\n", path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002044 filp_close(file, NULL);
Andy Grovere3d6f902011-07-19 08:55:10 +00002045 return -EIO;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002046 }
2047 filp_close(file, NULL);
2048
2049 return 0;
2050}
2051
2052static int core_scsi3_update_and_write_aptpl(
2053 struct se_device *dev,
2054 unsigned char *in_buf,
2055 u32 in_pr_aptpl_buf_len)
2056{
2057 unsigned char null_buf[64], *buf;
2058 u32 pr_aptpl_buf_len;
2059 int ret, clear_aptpl_metadata = 0;
2060 /*
2061 * Can be called with a NULL pointer from PROUT service action CLEAR
2062 */
Andy Grover6708bb22011-06-08 10:36:43 -07002063 if (!in_buf) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002064 memset(null_buf, 0, 64);
2065 buf = &null_buf[0];
2066 /*
2067 * This will clear the APTPL metadata to:
2068 * "No Registrations or Reservations" status
2069 */
2070 pr_aptpl_buf_len = 64;
2071 clear_aptpl_metadata = 1;
2072 } else {
2073 buf = in_buf;
2074 pr_aptpl_buf_len = in_pr_aptpl_buf_len;
2075 }
2076
2077 ret = core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
2078 clear_aptpl_metadata);
2079 if (ret != 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00002080 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002081 /*
2082 * __core_scsi3_write_aptpl_to_file() will call strlen()
2083 * on the passed buf to determine pr_aptpl_buf_len.
2084 */
2085 ret = __core_scsi3_write_aptpl_to_file(dev, buf, 0);
2086 if (ret != 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00002087 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002088
2089 return ret;
2090}
2091
2092static int core_scsi3_emulate_pro_register(
2093 struct se_cmd *cmd,
2094 u64 res_key,
2095 u64 sa_res_key,
2096 int aptpl,
2097 int all_tg_pt,
2098 int spec_i_pt,
2099 int ignore_key)
2100{
Andy Grovere3d6f902011-07-19 08:55:10 +00002101 struct se_session *se_sess = cmd->se_sess;
Andy Grover5951146d2011-07-19 10:26:37 +00002102 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002103 struct se_dev_entry *se_deve;
Andy Grovere3d6f902011-07-19 08:55:10 +00002104 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002105 struct se_portal_group *se_tpg;
2106 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp, *pr_reg_e;
Andy Grovere3d6f902011-07-19 08:55:10 +00002107 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002108 /* Used for APTPL metadata w/ UNREGISTER */
2109 unsigned char *pr_aptpl_buf = NULL;
2110 unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
2111 int pr_holder = 0, ret = 0, type;
2112
Andy Grover6708bb22011-06-08 10:36:43 -07002113 if (!se_sess || !se_lun) {
2114 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002115 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2116 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002117 }
2118 se_tpg = se_sess->se_tpg;
2119 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
2120
Andy Grovere3d6f902011-07-19 08:55:10 +00002121 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002122 memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +00002123 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002124 PR_REG_ISID_LEN);
2125 isid_ptr = &isid_buf[0];
2126 }
2127 /*
2128 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2129 */
2130 pr_reg_e = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002131 if (!pr_reg_e) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002132 if (res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002133 pr_warn("SPC-3 PR: Reservation Key non-zero"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002134 " for SA REGISTER, returning CONFLICT\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002135 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2136 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002137 }
2138 /*
2139 * Do nothing but return GOOD status.
2140 */
Andy Grover6708bb22011-06-08 10:36:43 -07002141 if (!sa_res_key)
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002142 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002143
Andy Grover6708bb22011-06-08 10:36:43 -07002144 if (!spec_i_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002145 /*
2146 * Perform the Service Action REGISTER on the Initiator
2147 * Port Endpoint that the PRO was received from on the
2148 * Logical Unit of the SCSI device server.
2149 */
Andy Grover5951146d2011-07-19 10:26:37 +00002150 ret = core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002151 se_sess->se_node_acl, se_deve, isid_ptr,
2152 sa_res_key, all_tg_pt, aptpl,
2153 ignore_key, 0);
2154 if (ret != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002155 pr_err("Unable to allocate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002156 " struct t10_pr_registration\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002157 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2158 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002159 }
2160 } else {
2161 /*
2162 * Register both the Initiator port that received
2163 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2164 * TransportID from Parameter list and loop through
2165 * fabric dependent parameter list while calling
2166 * logic from of core_scsi3_alloc_registration() for
2167 * each TransportID provided SCSI Initiator Port/Device
2168 */
2169 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2170 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2171 if (ret != 0)
2172 return ret;
2173 }
2174 /*
2175 * Nothing left to do for the APTPL=0 case.
2176 */
Andy Grover6708bb22011-06-08 10:36:43 -07002177 if (!aptpl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002178 pr_tmpl->pr_aptpl_active = 0;
Andy Grover5951146d2011-07-19 10:26:37 +00002179 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002180 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002181 " REGISTER\n");
2182 return 0;
2183 }
2184 /*
2185 * Locate the newly allocated local I_T Nexus *pr_reg, and
2186 * update the APTPL metadata information using its
2187 * preallocated *pr_reg->pr_aptpl_buf.
2188 */
Andy Grover5951146d2011-07-19 10:26:37 +00002189 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002190 se_sess->se_node_acl, se_sess);
2191
Andy Grover5951146d2011-07-19 10:26:37 +00002192 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002193 &pr_reg->pr_aptpl_buf[0],
2194 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07002195 if (!ret) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002196 pr_tmpl->pr_aptpl_active = 1;
Andy Grover6708bb22011-06-08 10:36:43 -07002197 pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002198 }
2199
2200 core_scsi3_put_pr_reg(pr_reg);
2201 return ret;
2202 } else {
2203 /*
2204 * Locate the existing *pr_reg via struct se_node_acl pointers
2205 */
2206 pr_reg = pr_reg_e;
2207 type = pr_reg->pr_res_type;
2208
Andy Grover6708bb22011-06-08 10:36:43 -07002209 if (!ignore_key) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002210 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002211 pr_err("SPC-3 PR REGISTER: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002212 " res_key: 0x%016Lx does not match"
2213 " existing SA REGISTER res_key:"
2214 " 0x%016Lx\n", res_key,
2215 pr_reg->pr_res_key);
2216 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002217 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2218 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002219 }
2220 }
2221 if (spec_i_pt) {
Andy Grover6708bb22011-06-08 10:36:43 -07002222 pr_err("SPC-3 PR UNREGISTER: SPEC_I_PT"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002223 " set while sa_res_key=0\n");
2224 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002225 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2226 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002227 }
2228 /*
2229 * An existing ALL_TG_PT=1 registration being released
2230 * must also set ALL_TG_PT=1 in the incoming PROUT.
2231 */
2232 if (pr_reg->pr_reg_all_tg_pt && !(all_tg_pt)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002233 pr_err("SPC-3 PR UNREGISTER: ALL_TG_PT=1"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002234 " registration exists, but ALL_TG_PT=1 bit not"
2235 " present in received PROUT\n");
2236 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002237 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2238 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002239 }
2240 /*
2241 * Allocate APTPL metadata buffer used for UNREGISTER ops
2242 */
2243 if (aptpl) {
2244 pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len,
2245 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002246 if (!pr_aptpl_buf) {
2247 pr_err("Unable to allocate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002248 " pr_aptpl_buf\n");
2249 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002250 cmd->scsi_sense_reason =
2251 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2252 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002253 }
2254 }
2255 /*
2256 * sa_res_key=0 Unregister Reservation Key for registered I_T
2257 * Nexus sa_res_key=1 Change Reservation Key for registered I_T
2258 * Nexus.
2259 */
Andy Grover6708bb22011-06-08 10:36:43 -07002260 if (!sa_res_key) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002261 pr_holder = core_scsi3_check_implict_release(
Andy Grover5951146d2011-07-19 10:26:37 +00002262 cmd->se_dev, pr_reg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002263 if (pr_holder < 0) {
2264 kfree(pr_aptpl_buf);
2265 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002266 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2267 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002268 }
2269
2270 spin_lock(&pr_tmpl->registration_lock);
2271 /*
2272 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2273 * and matching pr_res_key.
2274 */
2275 if (pr_reg->pr_reg_all_tg_pt) {
2276 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2277 &pr_tmpl->registration_list,
2278 pr_reg_list) {
2279
Andy Grover6708bb22011-06-08 10:36:43 -07002280 if (!pr_reg_p->pr_reg_all_tg_pt)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002281 continue;
2282
2283 if (pr_reg_p->pr_res_key != res_key)
2284 continue;
2285
2286 if (pr_reg == pr_reg_p)
2287 continue;
2288
2289 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2290 pr_reg_p->pr_reg_nacl->initiatorname))
2291 continue;
2292
2293 __core_scsi3_free_registration(dev,
2294 pr_reg_p, NULL, 0);
2295 }
2296 }
2297 /*
2298 * Release the calling I_T Nexus registration now..
2299 */
Andy Grover5951146d2011-07-19 10:26:37 +00002300 __core_scsi3_free_registration(cmd->se_dev, pr_reg,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002301 NULL, 1);
2302 /*
2303 * From spc4r17, section 5.7.11.3 Unregistering
2304 *
2305 * If the persistent reservation is a registrants only
2306 * type, the device server shall establish a unit
2307 * attention condition for the initiator port associated
2308 * with every registered I_T nexus except for the I_T
2309 * nexus on which the PERSISTENT RESERVE OUT command was
2310 * received, with the additional sense code set to
2311 * RESERVATIONS RELEASED.
2312 */
2313 if (pr_holder &&
2314 ((type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
2315 (type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY))) {
2316 list_for_each_entry(pr_reg_p,
2317 &pr_tmpl->registration_list,
2318 pr_reg_list) {
2319
2320 core_scsi3_ua_allocate(
2321 pr_reg_p->pr_reg_nacl,
2322 pr_reg_p->pr_res_mapped_lun,
2323 0x2A,
2324 ASCQ_2AH_RESERVATIONS_RELEASED);
2325 }
2326 }
2327 spin_unlock(&pr_tmpl->registration_lock);
2328
Andy Grover6708bb22011-06-08 10:36:43 -07002329 if (!aptpl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002330 pr_tmpl->pr_aptpl_active = 0;
2331 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002332 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002333 " for UNREGISTER\n");
2334 return 0;
2335 }
2336
2337 ret = core_scsi3_update_and_write_aptpl(dev,
2338 &pr_aptpl_buf[0],
2339 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07002340 if (!ret) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002341 pr_tmpl->pr_aptpl_active = 1;
Andy Grover6708bb22011-06-08 10:36:43 -07002342 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002343 " for UNREGISTER\n");
2344 }
2345
2346 kfree(pr_aptpl_buf);
2347 return ret;
2348 } else {
2349 /*
2350 * Increment PRgeneration counter for struct se_device"
2351 * upon a successful REGISTER, see spc4r17 section 6.3.2
2352 * READ_KEYS service action.
2353 */
2354 pr_reg->pr_res_generation = core_scsi3_pr_generation(
Andy Grover5951146d2011-07-19 10:26:37 +00002355 cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002356 pr_reg->pr_res_key = sa_res_key;
Andy Grover6708bb22011-06-08 10:36:43 -07002357 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002358 " Key for %s to: 0x%016Lx PRgeneration:"
Andy Grovere3d6f902011-07-19 08:55:10 +00002359 " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002360 (ignore_key) ? "_AND_IGNORE_EXISTING_KEY" : "",
2361 pr_reg->pr_reg_nacl->initiatorname,
2362 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2363
Andy Grover6708bb22011-06-08 10:36:43 -07002364 if (!aptpl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002365 pr_tmpl->pr_aptpl_active = 0;
2366 core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2367 core_scsi3_put_pr_reg(pr_reg);
Andy Grover6708bb22011-06-08 10:36:43 -07002368 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002369 " for REGISTER\n");
2370 return 0;
2371 }
2372
2373 ret = core_scsi3_update_and_write_aptpl(dev,
2374 &pr_aptpl_buf[0],
2375 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07002376 if (!ret) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002377 pr_tmpl->pr_aptpl_active = 1;
Andy Grover6708bb22011-06-08 10:36:43 -07002378 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002379 " for REGISTER\n");
2380 }
2381
2382 kfree(pr_aptpl_buf);
2383 core_scsi3_put_pr_reg(pr_reg);
2384 }
2385 }
2386 return 0;
2387}
2388
2389unsigned char *core_scsi3_pr_dump_type(int type)
2390{
2391 switch (type) {
2392 case PR_TYPE_WRITE_EXCLUSIVE:
2393 return "Write Exclusive Access";
2394 case PR_TYPE_EXCLUSIVE_ACCESS:
2395 return "Exclusive Access";
2396 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2397 return "Write Exclusive Access, Registrants Only";
2398 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2399 return "Exclusive Access, Registrants Only";
2400 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2401 return "Write Exclusive Access, All Registrants";
2402 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2403 return "Exclusive Access, All Registrants";
2404 default:
2405 break;
2406 }
2407
2408 return "Unknown SPC-3 PR Type";
2409}
2410
2411static int core_scsi3_pro_reserve(
2412 struct se_cmd *cmd,
2413 struct se_device *dev,
2414 int type,
2415 int scope,
2416 u64 res_key)
2417{
Andy Grovere3d6f902011-07-19 08:55:10 +00002418 struct se_session *se_sess = cmd->se_sess;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002419 struct se_dev_entry *se_deve;
Andy Grovere3d6f902011-07-19 08:55:10 +00002420 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002421 struct se_portal_group *se_tpg;
2422 struct t10_pr_registration *pr_reg, *pr_res_holder;
Andy Grovere3d6f902011-07-19 08:55:10 +00002423 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002424 char i_buf[PR_REG_ISID_ID_LEN];
2425 int ret, prf_isid;
2426
2427 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2428
Andy Grover6708bb22011-06-08 10:36:43 -07002429 if (!se_sess || !se_lun) {
2430 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002431 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2432 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002433 }
2434 se_tpg = se_sess->se_tpg;
2435 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
2436 /*
2437 * Locate the existing *pr_reg via struct se_node_acl pointers
2438 */
Andy Grover5951146d2011-07-19 10:26:37 +00002439 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002440 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002441 if (!pr_reg) {
2442 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002443 " PR_REGISTERED *pr_reg for RESERVE\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002444 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2445 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002446 }
2447 /*
2448 * From spc4r17 Section 5.7.9: Reserving:
2449 *
2450 * An application client creates a persistent reservation by issuing
2451 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2452 * a registered I_T nexus with the following parameters:
2453 * a) RESERVATION KEY set to the value of the reservation key that is
2454 * registered with the logical unit for the I_T nexus; and
2455 */
2456 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002457 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002458 " does not match existing SA REGISTER res_key:"
2459 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2460 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002461 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2462 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002463 }
2464 /*
2465 * From spc4r17 Section 5.7.9: Reserving:
2466 *
2467 * From above:
2468 * b) TYPE field and SCOPE field set to the persistent reservation
2469 * being created.
2470 *
2471 * Only one persistent reservation is allowed at a time per logical unit
2472 * and that persistent reservation has a scope of LU_SCOPE.
2473 */
2474 if (scope != PR_SCOPE_LU_SCOPE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002475 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002476 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002477 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2478 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002479 }
2480 /*
2481 * See if we have an existing PR reservation holder pointer at
2482 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2483 * *pr_res_holder.
2484 */
2485 spin_lock(&dev->dev_reservation_lock);
2486 pr_res_holder = dev->dev_pr_res_holder;
2487 if ((pr_res_holder)) {
2488 /*
2489 * From spc4r17 Section 5.7.9: Reserving:
2490 *
2491 * If the device server receives a PERSISTENT RESERVE OUT
2492 * command from an I_T nexus other than a persistent reservation
2493 * holder (see 5.7.10) that attempts to create a persistent
2494 * reservation when a persistent reservation already exists for
2495 * the logical unit, then the command shall be completed with
2496 * RESERVATION CONFLICT status.
2497 */
2498 if (pr_res_holder != pr_reg) {
2499 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002500 pr_err("SPC-3 PR: Attempted RESERVE from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002501 " [%s]: %s while reservation already held by"
2502 " [%s]: %s, returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002503 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002504 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002505 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002506 pr_res_holder->pr_reg_nacl->initiatorname);
2507
2508 spin_unlock(&dev->dev_reservation_lock);
2509 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002510 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2511 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002512 }
2513 /*
2514 * From spc4r17 Section 5.7.9: Reserving:
2515 *
2516 * If a persistent reservation holder attempts to modify the
2517 * type or scope of an existing persistent reservation, the
2518 * command shall be completed with RESERVATION CONFLICT status.
2519 */
2520 if ((pr_res_holder->pr_res_type != type) ||
2521 (pr_res_holder->pr_res_scope != scope)) {
2522 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002523 pr_err("SPC-3 PR: Attempted RESERVE from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002524 " [%s]: %s trying to change TYPE and/or SCOPE,"
2525 " while reservation already held by [%s]: %s,"
2526 " returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002527 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002528 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002529 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002530 pr_res_holder->pr_reg_nacl->initiatorname);
2531
2532 spin_unlock(&dev->dev_reservation_lock);
2533 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002534 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2535 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002536 }
2537 /*
2538 * From spc4r17 Section 5.7.9: Reserving:
2539 *
2540 * If the device server receives a PERSISTENT RESERVE OUT
2541 * command with RESERVE service action where the TYPE field and
2542 * the SCOPE field contain the same values as the existing type
2543 * and scope from a persistent reservation holder, it shall not
2544 * make any change to the existing persistent reservation and
2545 * shall completethe command with GOOD status.
2546 */
2547 spin_unlock(&dev->dev_reservation_lock);
2548 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002549 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002550 }
2551 /*
2552 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2553 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2554 */
2555 pr_reg->pr_res_scope = scope;
2556 pr_reg->pr_res_type = type;
2557 pr_reg->pr_res_holder = 1;
2558 dev->dev_pr_res_holder = pr_reg;
2559 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2560 PR_REG_ISID_ID_LEN);
2561
Andy Grover6708bb22011-06-08 10:36:43 -07002562 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002563 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002564 cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002565 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002566 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002567 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002568 se_sess->se_node_acl->initiatorname,
2569 (prf_isid) ? &i_buf[0] : "");
2570 spin_unlock(&dev->dev_reservation_lock);
2571
2572 if (pr_tmpl->pr_aptpl_active) {
Andy Grover5951146d2011-07-19 10:26:37 +00002573 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002574 &pr_reg->pr_aptpl_buf[0],
2575 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07002576 if (!ret)
2577 pr_debug("SPC-3 PR: Updated APTPL metadata"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002578 " for RESERVE\n");
2579 }
2580
2581 core_scsi3_put_pr_reg(pr_reg);
2582 return 0;
2583}
2584
2585static int core_scsi3_emulate_pro_reserve(
2586 struct se_cmd *cmd,
2587 int type,
2588 int scope,
2589 u64 res_key)
2590{
2591 struct se_device *dev = cmd->se_dev;
2592 int ret = 0;
2593
2594 switch (type) {
2595 case PR_TYPE_WRITE_EXCLUSIVE:
2596 case PR_TYPE_EXCLUSIVE_ACCESS:
2597 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2598 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2599 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2600 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2601 ret = core_scsi3_pro_reserve(cmd, dev, type, scope, res_key);
2602 break;
2603 default:
Andy Grover6708bb22011-06-08 10:36:43 -07002604 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002605 " 0x%02x\n", type);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002606 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2607 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002608 }
2609
2610 return ret;
2611}
2612
2613/*
2614 * Called with struct se_device->dev_reservation_lock held.
2615 */
2616static void __core_scsi3_complete_pro_release(
2617 struct se_device *dev,
2618 struct se_node_acl *se_nacl,
2619 struct t10_pr_registration *pr_reg,
2620 int explict)
2621{
2622 struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2623 char i_buf[PR_REG_ISID_ID_LEN];
2624 int prf_isid;
2625
2626 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2627 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2628 PR_REG_ISID_ID_LEN);
2629 /*
2630 * Go ahead and release the current PR reservation holder.
2631 */
2632 dev->dev_pr_res_holder = NULL;
2633
Andy Grover6708bb22011-06-08 10:36:43 -07002634 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002635 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2636 tfo->get_fabric_name(), (explict) ? "explict" : "implict",
2637 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
2638 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002639 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002640 tfo->get_fabric_name(), se_nacl->initiatorname,
2641 (prf_isid) ? &i_buf[0] : "");
2642 /*
2643 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2644 */
2645 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2646}
2647
2648static int core_scsi3_emulate_pro_release(
2649 struct se_cmd *cmd,
2650 int type,
2651 int scope,
2652 u64 res_key)
2653{
2654 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00002655 struct se_session *se_sess = cmd->se_sess;
2656 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002657 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
Andy Grovere3d6f902011-07-19 08:55:10 +00002658 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002659 int ret, all_reg = 0;
2660
Andy Grover6708bb22011-06-08 10:36:43 -07002661 if (!se_sess || !se_lun) {
2662 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002663 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2664 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002665 }
2666 /*
2667 * Locate the existing *pr_reg via struct se_node_acl pointers
2668 */
2669 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002670 if (!pr_reg) {
2671 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002672 " PR_REGISTERED *pr_reg for RELEASE\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002673 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2674 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002675 }
2676 /*
2677 * From spc4r17 Section 5.7.11.2 Releasing:
2678 *
2679 * If there is no persistent reservation or in response to a persistent
2680 * reservation release request from a registered I_T nexus that is not a
2681 * persistent reservation holder (see 5.7.10), the device server shall
2682 * do the following:
2683 *
2684 * a) Not release the persistent reservation, if any;
2685 * b) Not remove any registrations; and
2686 * c) Complete the command with GOOD status.
2687 */
2688 spin_lock(&dev->dev_reservation_lock);
2689 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07002690 if (!pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002691 /*
2692 * No persistent reservation, return GOOD status.
2693 */
2694 spin_unlock(&dev->dev_reservation_lock);
2695 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002696 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002697 }
2698 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2699 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
2700 all_reg = 1;
2701
2702 if ((all_reg == 0) && (pr_res_holder != pr_reg)) {
2703 /*
2704 * Non 'All Registrants' PR Type cases..
2705 * Release request from a registered I_T nexus that is not a
2706 * persistent reservation holder. return GOOD status.
2707 */
2708 spin_unlock(&dev->dev_reservation_lock);
2709 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002710 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002711 }
2712 /*
2713 * From spc4r17 Section 5.7.11.2 Releasing:
2714 *
2715 * Only the persistent reservation holder (see 5.7.10) is allowed to
2716 * release a persistent reservation.
2717 *
2718 * An application client releases the persistent reservation by issuing
2719 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2720 * an I_T nexus that is a persistent reservation holder with the
2721 * following parameters:
2722 *
2723 * a) RESERVATION KEY field set to the value of the reservation key
2724 * that is registered with the logical unit for the I_T nexus;
2725 */
2726 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002727 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002728 " does not match existing SA REGISTER res_key:"
2729 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2730 spin_unlock(&dev->dev_reservation_lock);
2731 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002732 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2733 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002734 }
2735 /*
2736 * From spc4r17 Section 5.7.11.2 Releasing and above:
2737 *
2738 * b) TYPE field and SCOPE field set to match the persistent
2739 * reservation being released.
2740 */
2741 if ((pr_res_holder->pr_res_type != type) ||
2742 (pr_res_holder->pr_res_scope != scope)) {
2743 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002744 pr_err("SPC-3 PR RELEASE: Attempted to release"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002745 " reservation from [%s]: %s with different TYPE "
2746 "and/or SCOPE while reservation already held by"
2747 " [%s]: %s, returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002748 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002749 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002750 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002751 pr_res_holder->pr_reg_nacl->initiatorname);
2752
2753 spin_unlock(&dev->dev_reservation_lock);
2754 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002755 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2756 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002757 }
2758 /*
2759 * In response to a persistent reservation release request from the
2760 * persistent reservation holder the device server shall perform a
2761 * release by doing the following as an uninterrupted series of actions:
2762 * a) Release the persistent reservation;
2763 * b) Not remove any registration(s);
2764 * c) If the released persistent reservation is a registrants only type
2765 * or all registrants type persistent reservation,
2766 * the device server shall establish a unit attention condition for
2767 * the initiator port associated with every regis-
2768 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2769 * RESERVE OUT command with RELEASE service action was received,
2770 * with the additional sense code set to RESERVATIONS RELEASED; and
2771 * d) If the persistent reservation is of any other type, the device
2772 * server shall not establish a unit attention condition.
2773 */
2774 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
2775 pr_reg, 1);
2776
2777 spin_unlock(&dev->dev_reservation_lock);
2778
2779 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2780 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2781 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2782 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2783 /*
2784 * If no UNIT ATTENTION conditions will be established for
2785 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2786 * go ahead and check for APTPL=1 update+write below
2787 */
2788 goto write_aptpl;
2789 }
2790
2791 spin_lock(&pr_tmpl->registration_lock);
2792 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2793 pr_reg_list) {
2794 /*
2795 * Do not establish a UNIT ATTENTION condition
2796 * for the calling I_T Nexus
2797 */
2798 if (pr_reg_p == pr_reg)
2799 continue;
2800
2801 core_scsi3_ua_allocate(pr_reg_p->pr_reg_nacl,
2802 pr_reg_p->pr_res_mapped_lun,
2803 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2804 }
2805 spin_unlock(&pr_tmpl->registration_lock);
2806
2807write_aptpl:
2808 if (pr_tmpl->pr_aptpl_active) {
Andy Grover5951146d2011-07-19 10:26:37 +00002809 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002810 &pr_reg->pr_aptpl_buf[0],
2811 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07002812 if (!ret)
2813 pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002814 }
2815
2816 core_scsi3_put_pr_reg(pr_reg);
2817 return 0;
2818}
2819
2820static int core_scsi3_emulate_pro_clear(
2821 struct se_cmd *cmd,
2822 u64 res_key)
2823{
2824 struct se_device *dev = cmd->se_dev;
2825 struct se_node_acl *pr_reg_nacl;
Andy Grovere3d6f902011-07-19 08:55:10 +00002826 struct se_session *se_sess = cmd->se_sess;
2827 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002828 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2829 u32 pr_res_mapped_lun = 0;
2830 int calling_it_nexus = 0;
2831 /*
2832 * Locate the existing *pr_reg via struct se_node_acl pointers
2833 */
Andy Grover5951146d2011-07-19 10:26:37 +00002834 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002835 se_sess->se_node_acl, se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002836 if (!pr_reg_n) {
2837 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002838 " PR_REGISTERED *pr_reg for CLEAR\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002839 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2840 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002841 }
2842 /*
2843 * From spc4r17 section 5.7.11.6, Clearing:
2844 *
2845 * Any application client may release the persistent reservation and
2846 * remove all registrations from a device server by issuing a
2847 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2848 * registered I_T nexus with the following parameter:
2849 *
2850 * a) RESERVATION KEY field set to the value of the reservation key
2851 * that is registered with the logical unit for the I_T nexus.
2852 */
2853 if (res_key != pr_reg_n->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002854 pr_err("SPC-3 PR REGISTER: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002855 " res_key: 0x%016Lx does not match"
2856 " existing SA REGISTER res_key:"
2857 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2858 core_scsi3_put_pr_reg(pr_reg_n);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002859 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2860 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002861 }
2862 /*
2863 * a) Release the persistent reservation, if any;
2864 */
2865 spin_lock(&dev->dev_reservation_lock);
2866 pr_res_holder = dev->dev_pr_res_holder;
2867 if (pr_res_holder) {
2868 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2869 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
2870 pr_res_holder, 0);
2871 }
2872 spin_unlock(&dev->dev_reservation_lock);
2873 /*
2874 * b) Remove all registration(s) (see spc4r17 5.7.7);
2875 */
2876 spin_lock(&pr_tmpl->registration_lock);
2877 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2878 &pr_tmpl->registration_list, pr_reg_list) {
2879
2880 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2881 pr_reg_nacl = pr_reg->pr_reg_nacl;
2882 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2883 __core_scsi3_free_registration(dev, pr_reg, NULL,
2884 calling_it_nexus);
2885 /*
2886 * e) Establish a unit attention condition for the initiator
2887 * port associated with every registered I_T nexus other
2888 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2889 * command with CLEAR service action was received, with the
2890 * additional sense code set to RESERVATIONS PREEMPTED.
2891 */
Andy Grover6708bb22011-06-08 10:36:43 -07002892 if (!calling_it_nexus)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002893 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun,
2894 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2895 }
2896 spin_unlock(&pr_tmpl->registration_lock);
2897
Andy Grover6708bb22011-06-08 10:36:43 -07002898 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002899 cmd->se_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002900
2901 if (pr_tmpl->pr_aptpl_active) {
Andy Grover5951146d2011-07-19 10:26:37 +00002902 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002903 pr_debug("SPC-3 PR: Updated APTPL metadata"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002904 " for CLEAR\n");
2905 }
2906
2907 core_scsi3_pr_generation(dev);
2908 return 0;
2909}
2910
2911/*
2912 * Called with struct se_device->dev_reservation_lock held.
2913 */
2914static void __core_scsi3_complete_pro_preempt(
2915 struct se_device *dev,
2916 struct t10_pr_registration *pr_reg,
2917 struct list_head *preempt_and_abort_list,
2918 int type,
2919 int scope,
2920 int abort)
2921{
2922 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
2923 struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2924 char i_buf[PR_REG_ISID_ID_LEN];
2925 int prf_isid;
2926
2927 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2928 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2929 PR_REG_ISID_ID_LEN);
2930 /*
2931 * Do an implict RELEASE of the existing reservation.
2932 */
2933 if (dev->dev_pr_res_holder)
2934 __core_scsi3_complete_pro_release(dev, nacl,
2935 dev->dev_pr_res_holder, 0);
2936
2937 dev->dev_pr_res_holder = pr_reg;
2938 pr_reg->pr_res_holder = 1;
2939 pr_reg->pr_res_type = type;
2940 pr_reg->pr_res_scope = scope;
2941
Andy Grover6708bb22011-06-08 10:36:43 -07002942 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002943 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2944 tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2945 core_scsi3_pr_dump_type(type),
2946 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002947 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002948 tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2949 nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
2950 /*
2951 * For PREEMPT_AND_ABORT, add the preempting reservation's
2952 * struct t10_pr_registration to the list that will be compared
2953 * against received CDBs..
2954 */
2955 if (preempt_and_abort_list)
2956 list_add_tail(&pr_reg->pr_reg_abort_list,
2957 preempt_and_abort_list);
2958}
2959
2960static void core_scsi3_release_preempt_and_abort(
2961 struct list_head *preempt_and_abort_list,
2962 struct t10_pr_registration *pr_reg_holder)
2963{
2964 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2965
2966 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2967 pr_reg_abort_list) {
2968
2969 list_del(&pr_reg->pr_reg_abort_list);
2970 if (pr_reg_holder == pr_reg)
2971 continue;
2972 if (pr_reg->pr_res_holder) {
Andy Grover6708bb22011-06-08 10:36:43 -07002973 pr_warn("pr_reg->pr_res_holder still set\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002974 continue;
2975 }
2976
2977 pr_reg->pr_reg_deve = NULL;
2978 pr_reg->pr_reg_nacl = NULL;
2979 kfree(pr_reg->pr_aptpl_buf);
2980 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2981 }
2982}
2983
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002984static int core_scsi3_pro_preempt(
2985 struct se_cmd *cmd,
2986 int type,
2987 int scope,
2988 u64 res_key,
2989 u64 sa_res_key,
2990 int abort)
2991{
Andy Grover5951146d2011-07-19 10:26:37 +00002992 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002993 struct se_dev_entry *se_deve;
2994 struct se_node_acl *pr_reg_nacl;
Andy Grovere3d6f902011-07-19 08:55:10 +00002995 struct se_session *se_sess = cmd->se_sess;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002996 struct list_head preempt_and_abort_list;
2997 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
Andy Grovere3d6f902011-07-19 08:55:10 +00002998 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002999 u32 pr_res_mapped_lun = 0;
3000 int all_reg = 0, calling_it_nexus = 0, released_regs = 0;
3001 int prh_type = 0, prh_scope = 0, ret;
3002
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003003 if (!se_sess) {
3004 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3005 return -EINVAL;
3006 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003007
3008 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
Andy Grover5951146d2011-07-19 10:26:37 +00003009 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003010 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07003011 if (!pr_reg_n) {
3012 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003013 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
3014 (abort) ? "_AND_ABORT" : "");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003015 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3016 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003017 }
3018 if (pr_reg_n->pr_res_key != res_key) {
3019 core_scsi3_put_pr_reg(pr_reg_n);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003020 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3021 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003022 }
3023 if (scope != PR_SCOPE_LU_SCOPE) {
Andy Grover6708bb22011-06-08 10:36:43 -07003024 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003025 core_scsi3_put_pr_reg(pr_reg_n);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003026 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3027 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003028 }
3029 INIT_LIST_HEAD(&preempt_and_abort_list);
3030
3031 spin_lock(&dev->dev_reservation_lock);
3032 pr_res_holder = dev->dev_pr_res_holder;
3033 if (pr_res_holder &&
3034 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3035 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
3036 all_reg = 1;
3037
Andy Grover6708bb22011-06-08 10:36:43 -07003038 if (!all_reg && !sa_res_key) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003039 spin_unlock(&dev->dev_reservation_lock);
3040 core_scsi3_put_pr_reg(pr_reg_n);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003041 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3042 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003043 }
3044 /*
3045 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
3046 *
3047 * If the SERVICE ACTION RESERVATION KEY field does not identify a
3048 * persistent reservation holder or there is no persistent reservation
3049 * holder (i.e., there is no persistent reservation), then the device
3050 * server shall perform a preempt by doing the following in an
3051 * uninterrupted series of actions. (See below..)
3052 */
Andy Grover6708bb22011-06-08 10:36:43 -07003053 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003054 /*
3055 * No existing or SA Reservation Key matching reservations..
3056 *
3057 * PROUT SA PREEMPT with All Registrant type reservations are
3058 * allowed to be processed without a matching SA Reservation Key
3059 */
3060 spin_lock(&pr_tmpl->registration_lock);
3061 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3062 &pr_tmpl->registration_list, pr_reg_list) {
3063 /*
3064 * Removing of registrations in non all registrants
3065 * type reservations without a matching SA reservation
3066 * key.
3067 *
3068 * a) Remove the registrations for all I_T nexuses
3069 * specified by the SERVICE ACTION RESERVATION KEY
3070 * field;
3071 * b) Ignore the contents of the SCOPE and TYPE fields;
3072 * c) Process tasks as defined in 5.7.1; and
3073 * d) Establish a unit attention condition for the
3074 * initiator port associated with every I_T nexus
3075 * that lost its registration other than the I_T
3076 * nexus on which the PERSISTENT RESERVE OUT command
3077 * was received, with the additional sense code set
3078 * to REGISTRATIONS PREEMPTED.
3079 */
Andy Grover6708bb22011-06-08 10:36:43 -07003080 if (!all_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003081 if (pr_reg->pr_res_key != sa_res_key)
3082 continue;
3083
3084 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3085 pr_reg_nacl = pr_reg->pr_reg_nacl;
3086 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3087 __core_scsi3_free_registration(dev, pr_reg,
3088 (abort) ? &preempt_and_abort_list :
3089 NULL, calling_it_nexus);
3090 released_regs++;
3091 } else {
3092 /*
3093 * Case for any existing all registrants type
3094 * reservation, follow logic in spc4r17 section
3095 * 5.7.11.4 Preempting, Table 52 and Figure 7.
3096 *
3097 * For a ZERO SA Reservation key, release
3098 * all other registrations and do an implict
3099 * release of active persistent reservation.
3100 *
3101 * For a non-ZERO SA Reservation key, only
3102 * release the matching reservation key from
3103 * registrations.
3104 */
3105 if ((sa_res_key) &&
3106 (pr_reg->pr_res_key != sa_res_key))
3107 continue;
3108
3109 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3110 if (calling_it_nexus)
3111 continue;
3112
3113 pr_reg_nacl = pr_reg->pr_reg_nacl;
3114 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3115 __core_scsi3_free_registration(dev, pr_reg,
3116 (abort) ? &preempt_and_abort_list :
3117 NULL, 0);
3118 released_regs++;
3119 }
Andy Grover6708bb22011-06-08 10:36:43 -07003120 if (!calling_it_nexus)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003121 core_scsi3_ua_allocate(pr_reg_nacl,
3122 pr_res_mapped_lun, 0x2A,
Marco Sanvido9e08e342012-01-03 17:12:57 -08003123 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003124 }
3125 spin_unlock(&pr_tmpl->registration_lock);
3126 /*
3127 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
3128 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
3129 * RESERVATION KEY field to a value that does not match any
3130 * registered reservation key, then the device server shall
3131 * complete the command with RESERVATION CONFLICT status.
3132 */
Andy Grover6708bb22011-06-08 10:36:43 -07003133 if (!released_regs) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003134 spin_unlock(&dev->dev_reservation_lock);
3135 core_scsi3_put_pr_reg(pr_reg_n);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003136 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3137 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003138 }
3139 /*
3140 * For an existing all registrants type reservation
3141 * with a zero SA rservation key, preempt the existing
3142 * reservation with the new PR type and scope.
3143 */
3144 if (pr_res_holder && all_reg && !(sa_res_key)) {
3145 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3146 (abort) ? &preempt_and_abort_list : NULL,
3147 type, scope, abort);
3148
3149 if (abort)
3150 core_scsi3_release_preempt_and_abort(
3151 &preempt_and_abort_list, pr_reg_n);
3152 }
3153 spin_unlock(&dev->dev_reservation_lock);
3154
3155 if (pr_tmpl->pr_aptpl_active) {
Andy Grover5951146d2011-07-19 10:26:37 +00003156 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003157 &pr_reg_n->pr_aptpl_buf[0],
3158 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07003159 if (!ret)
3160 pr_debug("SPC-3 PR: Updated APTPL"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003161 " metadata for PREEMPT%s\n", (abort) ?
3162 "_AND_ABORT" : "");
3163 }
3164
3165 core_scsi3_put_pr_reg(pr_reg_n);
Andy Grover5951146d2011-07-19 10:26:37 +00003166 core_scsi3_pr_generation(cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003167 return 0;
3168 }
3169 /*
3170 * The PREEMPTing SA reservation key matches that of the
3171 * existing persistent reservation, first, we check if
3172 * we are preempting our own reservation.
3173 * From spc4r17, section 5.7.11.4.3 Preempting
3174 * persistent reservations and registration handling
3175 *
3176 * If an all registrants persistent reservation is not
3177 * present, it is not an error for the persistent
3178 * reservation holder to preempt itself (i.e., a
3179 * PERSISTENT RESERVE OUT with a PREEMPT service action
3180 * or a PREEMPT AND ABORT service action with the
3181 * SERVICE ACTION RESERVATION KEY value equal to the
3182 * persistent reservation holder's reservation key that
3183 * is received from the persistent reservation holder).
3184 * In that case, the device server shall establish the
3185 * new persistent reservation and maintain the
3186 * registration.
3187 */
3188 prh_type = pr_res_holder->pr_res_type;
3189 prh_scope = pr_res_holder->pr_res_scope;
3190 /*
3191 * If the SERVICE ACTION RESERVATION KEY field identifies a
3192 * persistent reservation holder (see 5.7.10), the device
3193 * server shall perform a preempt by doing the following as
3194 * an uninterrupted series of actions:
3195 *
3196 * a) Release the persistent reservation for the holder
3197 * identified by the SERVICE ACTION RESERVATION KEY field;
3198 */
3199 if (pr_reg_n != pr_res_holder)
3200 __core_scsi3_complete_pro_release(dev,
3201 pr_res_holder->pr_reg_nacl,
3202 dev->dev_pr_res_holder, 0);
3203 /*
3204 * b) Remove the registrations for all I_T nexuses identified
3205 * by the SERVICE ACTION RESERVATION KEY field, except the
3206 * I_T nexus that is being used for the PERSISTENT RESERVE
3207 * OUT command. If an all registrants persistent reservation
3208 * is present and the SERVICE ACTION RESERVATION KEY field
3209 * is set to zero, then all registrations shall be removed
3210 * except for that of the I_T nexus that is being used for
3211 * the PERSISTENT RESERVE OUT command;
3212 */
3213 spin_lock(&pr_tmpl->registration_lock);
3214 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3215 &pr_tmpl->registration_list, pr_reg_list) {
3216
3217 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3218 if (calling_it_nexus)
3219 continue;
3220
3221 if (pr_reg->pr_res_key != sa_res_key)
3222 continue;
3223
3224 pr_reg_nacl = pr_reg->pr_reg_nacl;
3225 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3226 __core_scsi3_free_registration(dev, pr_reg,
3227 (abort) ? &preempt_and_abort_list : NULL,
3228 calling_it_nexus);
3229 /*
3230 * e) Establish a unit attention condition for the initiator
3231 * port associated with every I_T nexus that lost its
3232 * persistent reservation and/or registration, with the
3233 * additional sense code set to REGISTRATIONS PREEMPTED;
3234 */
3235 core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
Marco Sanvido9e08e342012-01-03 17:12:57 -08003236 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003237 }
3238 spin_unlock(&pr_tmpl->registration_lock);
3239 /*
3240 * c) Establish a persistent reservation for the preempting
3241 * I_T nexus using the contents of the SCOPE and TYPE fields;
3242 */
3243 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3244 (abort) ? &preempt_and_abort_list : NULL,
3245 type, scope, abort);
3246 /*
3247 * d) Process tasks as defined in 5.7.1;
3248 * e) See above..
3249 * f) If the type or scope has changed, then for every I_T nexus
3250 * whose reservation key was not removed, except for the I_T
3251 * nexus on which the PERSISTENT RESERVE OUT command was
3252 * received, the device server shall establish a unit
3253 * attention condition for the initiator port associated with
3254 * that I_T nexus, with the additional sense code set to
3255 * RESERVATIONS RELEASED. If the type or scope have not
3256 * changed, then no unit attention condition(s) shall be
3257 * established for this reason.
3258 */
3259 if ((prh_type != type) || (prh_scope != scope)) {
3260 spin_lock(&pr_tmpl->registration_lock);
3261 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3262 &pr_tmpl->registration_list, pr_reg_list) {
3263
3264 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3265 if (calling_it_nexus)
3266 continue;
3267
3268 core_scsi3_ua_allocate(pr_reg->pr_reg_nacl,
3269 pr_reg->pr_res_mapped_lun, 0x2A,
3270 ASCQ_2AH_RESERVATIONS_RELEASED);
3271 }
3272 spin_unlock(&pr_tmpl->registration_lock);
3273 }
3274 spin_unlock(&dev->dev_reservation_lock);
3275 /*
3276 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3277 * All received CDBs for the matching existing reservation and
3278 * registrations undergo ABORT_TASK logic.
3279 *
3280 * From there, core_scsi3_release_preempt_and_abort() will
3281 * release every registration in the list (which have already
3282 * been removed from the primary pr_reg list), except the
3283 * new persistent reservation holder, the calling Initiator Port.
3284 */
3285 if (abort) {
3286 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3287 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3288 pr_reg_n);
3289 }
3290
3291 if (pr_tmpl->pr_aptpl_active) {
Andy Grover5951146d2011-07-19 10:26:37 +00003292 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003293 &pr_reg_n->pr_aptpl_buf[0],
3294 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07003295 if (!ret)
3296 pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003297 "%s\n", (abort) ? "_AND_ABORT" : "");
3298 }
3299
3300 core_scsi3_put_pr_reg(pr_reg_n);
Andy Grover5951146d2011-07-19 10:26:37 +00003301 core_scsi3_pr_generation(cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003302 return 0;
3303}
3304
3305static int core_scsi3_emulate_pro_preempt(
3306 struct se_cmd *cmd,
3307 int type,
3308 int scope,
3309 u64 res_key,
3310 u64 sa_res_key,
3311 int abort)
3312{
3313 int ret = 0;
3314
3315 switch (type) {
3316 case PR_TYPE_WRITE_EXCLUSIVE:
3317 case PR_TYPE_EXCLUSIVE_ACCESS:
3318 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3319 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3320 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3321 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
3322 ret = core_scsi3_pro_preempt(cmd, type, scope,
3323 res_key, sa_res_key, abort);
3324 break;
3325 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003326 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003327 " Type: 0x%02x\n", (abort) ? "_AND_ABORT" : "", type);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003328 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3329 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003330 }
3331
3332 return ret;
3333}
3334
3335
3336static int core_scsi3_emulate_pro_register_and_move(
3337 struct se_cmd *cmd,
3338 u64 res_key,
3339 u64 sa_res_key,
3340 int aptpl,
3341 int unreg)
3342{
Andy Grovere3d6f902011-07-19 08:55:10 +00003343 struct se_session *se_sess = cmd->se_sess;
Andy Grover5951146d2011-07-19 10:26:37 +00003344 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003345 struct se_dev_entry *se_deve, *dest_se_deve = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00003346 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003347 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3348 struct se_port *se_port;
3349 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3350 struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3351 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
Andy Grovere3d6f902011-07-19 08:55:10 +00003352 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003353 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003354 unsigned char *initiator_str;
3355 char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
3356 u32 tid_len, tmp_tid_len;
3357 int new_reg = 0, type, scope, ret, matching_iname, prf_isid;
3358 unsigned short rtpi;
3359 unsigned char proto_ident;
3360
Andy Grover6708bb22011-06-08 10:36:43 -07003361 if (!se_sess || !se_lun) {
3362 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003363 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3364 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003365 }
3366 memset(dest_iport, 0, 64);
3367 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3368 se_tpg = se_sess->se_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00003369 tf_ops = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003370 se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
3371 /*
3372 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3373 * Register behaviors for a REGISTER AND MOVE service action
3374 *
3375 * Locate the existing *pr_reg via struct se_node_acl pointers
3376 */
Andy Grover5951146d2011-07-19 10:26:37 +00003377 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003378 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07003379 if (!pr_reg) {
3380 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003381 " *pr_reg for REGISTER_AND_MOVE\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003382 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3383 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003384 }
3385 /*
3386 * The provided reservation key much match the existing reservation key
3387 * provided during this initiator's I_T nexus registration.
3388 */
3389 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07003390 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003391 " res_key: 0x%016Lx does not match existing SA REGISTER"
3392 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
3393 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003394 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3395 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003396 }
3397 /*
3398 * The service active reservation key needs to be non zero
3399 */
Andy Grover6708bb22011-06-08 10:36:43 -07003400 if (!sa_res_key) {
3401 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003402 " sa_res_key\n");
3403 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003404 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3405 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003406 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003407
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003408 /*
3409 * Determine the Relative Target Port Identifier where the reservation
3410 * will be moved to for the TransportID containing SCSI initiator WWN
3411 * information.
3412 */
Andy Grover05d1c7c2011-07-20 19:13:28 +00003413 buf = transport_kmap_first_data_page(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003414 rtpi = (buf[18] & 0xff) << 8;
3415 rtpi |= buf[19] & 0xff;
3416 tid_len = (buf[20] & 0xff) << 24;
3417 tid_len |= (buf[21] & 0xff) << 16;
3418 tid_len |= (buf[22] & 0xff) << 8;
3419 tid_len |= buf[23] & 0xff;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003420 transport_kunmap_first_data_page(cmd);
3421 buf = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003422
3423 if ((tid_len + 24) != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07003424 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003425 " does not equal CDB data_length: %u\n", tid_len,
3426 cmd->data_length);
3427 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003428 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3429 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003430 }
3431
3432 spin_lock(&dev->se_port_lock);
3433 list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) {
3434 if (se_port->sep_rtpi != rtpi)
3435 continue;
3436 dest_se_tpg = se_port->sep_tpg;
Andy Grover6708bb22011-06-08 10:36:43 -07003437 if (!dest_se_tpg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003438 continue;
Andy Grovere3d6f902011-07-19 08:55:10 +00003439 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
Andy Grover6708bb22011-06-08 10:36:43 -07003440 if (!dest_tf_ops)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003441 continue;
3442
3443 atomic_inc(&dest_se_tpg->tpg_pr_ref_count);
3444 smp_mb__after_atomic_inc();
3445 spin_unlock(&dev->se_port_lock);
3446
3447 ret = core_scsi3_tpg_depend_item(dest_se_tpg);
3448 if (ret != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003449 pr_err("core_scsi3_tpg_depend_item() failed"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003450 " for dest_se_tpg\n");
3451 atomic_dec(&dest_se_tpg->tpg_pr_ref_count);
3452 smp_mb__after_atomic_dec();
3453 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003454 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3455 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003456 }
3457
3458 spin_lock(&dev->se_port_lock);
3459 break;
3460 }
3461 spin_unlock(&dev->se_port_lock);
3462
Andy Grover6708bb22011-06-08 10:36:43 -07003463 if (!dest_se_tpg || !dest_tf_ops) {
3464 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003465 " fabric ops from Relative Target Port Identifier:"
3466 " %hu\n", rtpi);
3467 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003468 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3469 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003470 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003471
3472 buf = transport_kmap_first_data_page(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003473 proto_ident = (buf[24] & 0x0f);
3474#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07003475 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003476 " 0x%02x\n", proto_ident);
3477#endif
3478 if (proto_ident != dest_tf_ops->get_fabric_proto_ident(dest_se_tpg)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003479 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003480 " proto_ident: 0x%02x does not match ident: 0x%02x"
3481 " from fabric: %s\n", proto_ident,
3482 dest_tf_ops->get_fabric_proto_ident(dest_se_tpg),
3483 dest_tf_ops->get_fabric_name());
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003484 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3485 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003486 goto out;
3487 }
3488 if (dest_tf_ops->tpg_parse_pr_out_transport_id == NULL) {
Andy Grover6708bb22011-06-08 10:36:43 -07003489 pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003490 " containg a valid tpg_parse_pr_out_transport_id"
3491 " function pointer\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003492 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3493 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003494 goto out;
3495 }
3496 initiator_str = dest_tf_ops->tpg_parse_pr_out_transport_id(dest_se_tpg,
3497 (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07003498 if (!initiator_str) {
3499 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003500 " initiator_str from Transport ID\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003501 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3502 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003503 goto out;
3504 }
3505
Andy Grover05d1c7c2011-07-20 19:13:28 +00003506 transport_kunmap_first_data_page(cmd);
3507 buf = NULL;
3508
Andy Grover6708bb22011-06-08 10:36:43 -07003509 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003510 " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
3511 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3512 iport_ptr : "");
3513 /*
3514 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3515 * action specifies a TransportID that is the same as the initiator port
3516 * of the I_T nexus for the command received, then the command shall
3517 * be terminated with CHECK CONDITION status, with the sense key set to
3518 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3519 * IN PARAMETER LIST.
3520 */
3521 pr_reg_nacl = pr_reg->pr_reg_nacl;
3522 matching_iname = (!strcmp(initiator_str,
3523 pr_reg_nacl->initiatorname)) ? 1 : 0;
Andy Grover6708bb22011-06-08 10:36:43 -07003524 if (!matching_iname)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003525 goto after_iport_check;
3526
Andy Grover6708bb22011-06-08 10:36:43 -07003527 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3528 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003529 " matches: %s on received I_T Nexus\n", initiator_str,
3530 pr_reg_nacl->initiatorname);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003531 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3532 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003533 goto out;
3534 }
Andy Grover6708bb22011-06-08 10:36:43 -07003535 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3536 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003537 " matches: %s %s on received I_T Nexus\n",
3538 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3539 pr_reg->pr_reg_isid);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003540 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3541 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003542 goto out;
3543 }
3544after_iport_check:
3545 /*
3546 * Locate the destination struct se_node_acl from the received Transport ID
3547 */
Roland Dreier286388872011-08-16 09:40:01 -07003548 spin_lock_irq(&dest_se_tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003549 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3550 initiator_str);
3551 if (dest_node_acl) {
3552 atomic_inc(&dest_node_acl->acl_pr_ref_count);
3553 smp_mb__after_atomic_inc();
3554 }
Roland Dreier286388872011-08-16 09:40:01 -07003555 spin_unlock_irq(&dest_se_tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003556
Andy Grover6708bb22011-06-08 10:36:43 -07003557 if (!dest_node_acl) {
3558 pr_err("Unable to locate %s dest_node_acl for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003559 " TransportID%s\n", dest_tf_ops->get_fabric_name(),
3560 initiator_str);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003561 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3562 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003563 goto out;
3564 }
3565 ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
3566 if (ret != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003567 pr_err("core_scsi3_nodeacl_depend_item() for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003568 " dest_node_acl\n");
3569 atomic_dec(&dest_node_acl->acl_pr_ref_count);
3570 smp_mb__after_atomic_dec();
3571 dest_node_acl = NULL;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003572 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3573 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003574 goto out;
3575 }
3576#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07003577 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003578 " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
3579 dest_node_acl->initiatorname);
3580#endif
3581 /*
3582 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3583 * PORT IDENTIFIER.
3584 */
3585 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
Andy Grover6708bb22011-06-08 10:36:43 -07003586 if (!dest_se_deve) {
3587 pr_err("Unable to locate %s dest_se_deve from RTPI:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003588 " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003589 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3590 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003591 goto out;
3592 }
3593
3594 ret = core_scsi3_lunacl_depend_item(dest_se_deve);
3595 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003596 pr_err("core_scsi3_lunacl_depend_item() failed\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003597 atomic_dec(&dest_se_deve->pr_ref_count);
3598 smp_mb__after_atomic_dec();
3599 dest_se_deve = NULL;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003600 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3601 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003602 goto out;
3603 }
3604#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07003605 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003606 " ACL for dest_se_deve->mapped_lun: %u\n",
3607 dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
3608 dest_se_deve->mapped_lun);
3609#endif
3610 /*
3611 * A persistent reservation needs to already existing in order to
3612 * successfully complete the REGISTER_AND_MOVE service action..
3613 */
3614 spin_lock(&dev->dev_reservation_lock);
3615 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07003616 if (!pr_res_holder) {
3617 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003618 " currently held\n");
3619 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003620 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3621 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003622 goto out;
3623 }
3624 /*
3625 * The received on I_T Nexus must be the reservation holder.
3626 *
3627 * From spc4r17 section 5.7.8 Table 50 --
3628 * Register behaviors for a REGISTER AND MOVE service action
3629 */
3630 if (pr_res_holder != pr_reg) {
Andy Grover6708bb22011-06-08 10:36:43 -07003631 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003632 " Nexus is not reservation holder\n");
3633 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003634 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3635 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003636 goto out;
3637 }
3638 /*
3639 * From spc4r17 section 5.7.8: registering and moving reservation
3640 *
3641 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3642 * action is received and the established persistent reservation is a
3643 * Write Exclusive - All Registrants type or Exclusive Access -
3644 * All Registrants type reservation, then the command shall be completed
3645 * with RESERVATION CONFLICT status.
3646 */
3647 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3648 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003649 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003650 " reservation for type: %s\n",
3651 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3652 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003653 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3654 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003655 goto out;
3656 }
3657 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3658 /*
3659 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3660 */
3661 type = pr_res_holder->pr_res_type;
3662 scope = pr_res_holder->pr_res_type;
3663 /*
3664 * c) Associate the reservation key specified in the SERVICE ACTION
3665 * RESERVATION KEY field with the I_T nexus specified as the
3666 * destination of the register and move, where:
3667 * A) The I_T nexus is specified by the TransportID and the
3668 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3669 * B) Regardless of the TransportID format used, the association for
3670 * the initiator port is based on either the initiator port name
3671 * (see 3.1.71) on SCSI transport protocols where port names are
3672 * required or the initiator port identifier (see 3.1.70) on SCSI
3673 * transport protocols where port names are not required;
3674 * d) Register the reservation key specified in the SERVICE ACTION
3675 * RESERVATION KEY field;
3676 * e) Retain the reservation key specified in the SERVICE ACTION
3677 * RESERVATION KEY field and associated information;
3678 *
3679 * Also, It is not an error for a REGISTER AND MOVE service action to
3680 * register an I_T nexus that is already registered with the same
3681 * reservation key or a different reservation key.
3682 */
3683 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3684 iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07003685 if (!dest_pr_reg) {
Andy Grover5951146d2011-07-19 10:26:37 +00003686 ret = core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003687 dest_node_acl, dest_se_deve, iport_ptr,
3688 sa_res_key, 0, aptpl, 2, 1);
3689 if (ret != 0) {
3690 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003691 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3692 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003693 goto out;
3694 }
3695 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3696 iport_ptr);
3697 new_reg = 1;
3698 }
3699 /*
3700 * f) Release the persistent reservation for the persistent reservation
3701 * holder (i.e., the I_T nexus on which the
3702 */
3703 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
3704 dev->dev_pr_res_holder, 0);
3705 /*
3706 * g) Move the persistent reservation to the specified I_T nexus using
3707 * the same scope and type as the persistent reservation released in
3708 * item f); and
3709 */
3710 dev->dev_pr_res_holder = dest_pr_reg;
3711 dest_pr_reg->pr_res_holder = 1;
3712 dest_pr_reg->pr_res_type = type;
3713 pr_reg->pr_res_scope = scope;
3714 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
3715 PR_REG_ISID_ID_LEN);
3716 /*
3717 * Increment PRGeneration for existing registrations..
3718 */
Andy Grover6708bb22011-06-08 10:36:43 -07003719 if (!new_reg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003720 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3721 spin_unlock(&dev->dev_reservation_lock);
3722
Andy Grover6708bb22011-06-08 10:36:43 -07003723 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003724 " created new reservation holder TYPE: %s on object RTPI:"
3725 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
3726 core_scsi3_pr_dump_type(type), rtpi,
3727 dest_pr_reg->pr_res_generation);
Andy Grover6708bb22011-06-08 10:36:43 -07003728 pr_debug("SPC-3 PR Successfully moved reservation from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003729 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3730 tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
3731 (prf_isid) ? &i_buf[0] : "", dest_tf_ops->get_fabric_name(),
3732 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3733 iport_ptr : "");
3734 /*
3735 * It is now safe to release configfs group dependencies for destination
3736 * of Transport ID Initiator Device/Port Identifier
3737 */
3738 core_scsi3_lunacl_undepend_item(dest_se_deve);
3739 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3740 core_scsi3_tpg_undepend_item(dest_se_tpg);
3741 /*
3742 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3743 * nexus on which PERSISTENT RESERVE OUT command was received.
3744 */
3745 if (unreg) {
3746 spin_lock(&pr_tmpl->registration_lock);
3747 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3748 spin_unlock(&pr_tmpl->registration_lock);
3749 } else
3750 core_scsi3_put_pr_reg(pr_reg);
3751
3752 /*
3753 * Clear the APTPL metadata if APTPL has been disabled, otherwise
3754 * write out the updated metadata to struct file for this SCSI device.
3755 */
Andy Grover6708bb22011-06-08 10:36:43 -07003756 if (!aptpl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003757 pr_tmpl->pr_aptpl_active = 0;
Andy Grover5951146d2011-07-19 10:26:37 +00003758 core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07003759 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003760 " REGISTER_AND_MOVE\n");
3761 } else {
3762 pr_tmpl->pr_aptpl_active = 1;
Andy Grover5951146d2011-07-19 10:26:37 +00003763 ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003764 &dest_pr_reg->pr_aptpl_buf[0],
3765 pr_tmpl->pr_aptpl_buf_len);
Andy Grover6708bb22011-06-08 10:36:43 -07003766 if (!ret)
3767 pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003768 " REGISTER_AND_MOVE\n");
3769 }
3770
Andy Grover05d1c7c2011-07-20 19:13:28 +00003771 transport_kunmap_first_data_page(cmd);
3772
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003773 core_scsi3_put_pr_reg(dest_pr_reg);
3774 return 0;
3775out:
Andy Grover05d1c7c2011-07-20 19:13:28 +00003776 if (buf)
3777 transport_kunmap_first_data_page(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003778 if (dest_se_deve)
3779 core_scsi3_lunacl_undepend_item(dest_se_deve);
3780 if (dest_node_acl)
3781 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3782 core_scsi3_tpg_undepend_item(dest_se_tpg);
3783 core_scsi3_put_pr_reg(pr_reg);
3784 return ret;
3785}
3786
3787static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
3788{
3789 unsigned int __v1, __v2;
3790
3791 __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
3792 __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
3793
3794 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
3795}
3796
3797/*
3798 * See spc4r17 section 6.14 Table 170
3799 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04003800int target_scsi3_emulate_pr_out(struct se_task *task)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003801{
Christoph Hellwige76a35d2011-11-03 17:50:42 -04003802 struct se_cmd *cmd = task->task_se_cmd;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003803 unsigned char *cdb = &cmd->t_task_cdb[0];
Andy Grover05d1c7c2011-07-20 19:13:28 +00003804 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003805 u64 res_key, sa_res_key;
3806 int sa, scope, type, aptpl;
3807 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003808 int ret;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003809
3810 /*
3811 * Following spc2r20 5.5.1 Reservations overview:
3812 *
3813 * If a logical unit has been reserved by any RESERVE command and is
3814 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3815 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3816 * initiator or service action and shall terminate with a RESERVATION
3817 * CONFLICT status.
3818 */
3819 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
3820 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3821 " SPC-2 reservation is held, returning"
3822 " RESERVATION_CONFLICT\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003823 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
3824 ret = EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003825 goto out;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003826 }
3827
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003828 /*
3829 * FIXME: A NULL struct se_session pointer means an this is not coming from
3830 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3831 */
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003832 if (!cmd->se_sess) {
3833 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3834 return -EINVAL;
3835 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003836
3837 if (cmd->data_length < 24) {
Andy Grover6708bb22011-06-08 10:36:43 -07003838 pr_warn("SPC-PR: Received PR OUT parameter list"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003839 " length too small: %u\n", cmd->data_length);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003840 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3841 ret = -EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003842 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003843 }
3844 /*
3845 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3846 */
3847 sa = (cdb[1] & 0x1f);
3848 scope = (cdb[2] & 0xf0);
3849 type = (cdb[2] & 0x0f);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003850
3851 buf = transport_kmap_first_data_page(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003852 /*
3853 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3854 */
3855 res_key = core_scsi3_extract_reservation_key(&buf[0]);
3856 sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
3857 /*
3858 * REGISTER_AND_MOVE uses a different SA parameter list containing
3859 * SCSI TransportIDs.
3860 */
3861 if (sa != PRO_REGISTER_AND_MOVE) {
3862 spec_i_pt = (buf[20] & 0x08);
3863 all_tg_pt = (buf[20] & 0x04);
3864 aptpl = (buf[20] & 0x01);
3865 } else {
3866 aptpl = (buf[17] & 0x01);
3867 unreg = (buf[17] & 0x02);
3868 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003869 transport_kunmap_first_data_page(cmd);
3870 buf = NULL;
3871
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003872 /*
3873 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3874 */
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003875 if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER)) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003876 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3877 ret = -EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003878 goto out;
3879 }
3880
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003881 /*
3882 * From spc4r17 section 6.14:
3883 *
3884 * If the SPEC_I_PT bit is set to zero, the service action is not
3885 * REGISTER AND MOVE, and the parameter list length is not 24, then
3886 * the command shall be terminated with CHECK CONDITION status, with
3887 * the sense key set to ILLEGAL REQUEST, and the additional sense
3888 * code set to PARAMETER LIST LENGTH ERROR.
3889 */
Andy Grover6708bb22011-06-08 10:36:43 -07003890 if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003891 (cmd->data_length != 24)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003892 pr_warn("SPC-PR: Received PR OUT illegal parameter"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003893 " list length: %u\n", cmd->data_length);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003894 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
3895 ret = -EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003896 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003897 }
3898 /*
3899 * (core_scsi3_emulate_pro_* function parameters
3900 * are defined by spc4r17 Table 174:
3901 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3902 */
3903 switch (sa) {
3904 case PRO_REGISTER:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003905 ret = core_scsi3_emulate_pro_register(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003906 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 0);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003907 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003908 case PRO_RESERVE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003909 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3910 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003911 case PRO_RELEASE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003912 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3913 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003914 case PRO_CLEAR:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003915 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3916 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003917 case PRO_PREEMPT:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003918 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003919 res_key, sa_res_key, 0);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003920 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003921 case PRO_PREEMPT_AND_ABORT:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003922 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003923 res_key, sa_res_key, 1);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003924 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003925 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003926 ret = core_scsi3_emulate_pro_register(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003927 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 1);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003928 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003929 case PRO_REGISTER_AND_MOVE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003930 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003931 sa_res_key, aptpl, unreg);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003932 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003933 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003934 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003935 " action: 0x%02x\n", cdb[1] & 0x1f);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003936 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3937 ret = -EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003938 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003939 }
3940
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003941out:
3942 if (!ret) {
3943 task->task_scsi_status = GOOD;
3944 transport_complete_task(task, 1);
3945 }
3946 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003947}
3948
3949/*
3950 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3951 *
3952 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3953 */
3954static int core_scsi3_pri_read_keys(struct se_cmd *cmd)
3955{
Andy Grover5951146d2011-07-19 10:26:37 +00003956 struct se_device *se_dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00003957 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003958 struct t10_pr_registration *pr_reg;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003959 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003960 u32 add_len = 0, off = 8;
3961
3962 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07003963 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003964 " too small\n", cmd->data_length);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003965 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3966 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003967 }
3968
Andy Grover05d1c7c2011-07-20 19:13:28 +00003969 buf = transport_kmap_first_data_page(cmd);
Andy Grovere3d6f902011-07-19 08:55:10 +00003970 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
3971 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
3972 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
3973 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003974
Andy Grovere3d6f902011-07-19 08:55:10 +00003975 spin_lock(&su_dev->t10_pr.registration_lock);
3976 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003977 pr_reg_list) {
3978 /*
3979 * Check for overflow of 8byte PRI READ_KEYS payload and
3980 * next reservation key list descriptor.
3981 */
3982 if ((add_len + 8) > (cmd->data_length - 8))
3983 break;
3984
3985 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3986 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3987 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3988 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
3989 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
3990 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
3991 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
3992 buf[off++] = (pr_reg->pr_res_key & 0xff);
3993
3994 add_len += 8;
3995 }
Andy Grovere3d6f902011-07-19 08:55:10 +00003996 spin_unlock(&su_dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003997
3998 buf[4] = ((add_len >> 24) & 0xff);
3999 buf[5] = ((add_len >> 16) & 0xff);
4000 buf[6] = ((add_len >> 8) & 0xff);
4001 buf[7] = (add_len & 0xff);
4002
Andy Grover05d1c7c2011-07-20 19:13:28 +00004003 transport_kunmap_first_data_page(cmd);
4004
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004005 return 0;
4006}
4007
4008/*
4009 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
4010 *
4011 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
4012 */
4013static int core_scsi3_pri_read_reservation(struct se_cmd *cmd)
4014{
Andy Grover5951146d2011-07-19 10:26:37 +00004015 struct se_device *se_dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00004016 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004017 struct t10_pr_registration *pr_reg;
Andy Grover05d1c7c2011-07-20 19:13:28 +00004018 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004019 u64 pr_res_key;
4020 u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
4021
4022 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07004023 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004024 " too small\n", cmd->data_length);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004025 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4026 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004027 }
4028
Andy Grover05d1c7c2011-07-20 19:13:28 +00004029 buf = transport_kmap_first_data_page(cmd);
Andy Grovere3d6f902011-07-19 08:55:10 +00004030 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
4031 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
4032 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
4033 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004034
4035 spin_lock(&se_dev->dev_reservation_lock);
4036 pr_reg = se_dev->dev_pr_res_holder;
4037 if ((pr_reg)) {
4038 /*
4039 * Set the hardcoded Additional Length
4040 */
4041 buf[4] = ((add_len >> 24) & 0xff);
4042 buf[5] = ((add_len >> 16) & 0xff);
4043 buf[6] = ((add_len >> 8) & 0xff);
4044 buf[7] = (add_len & 0xff);
4045
Andy Grover05d1c7c2011-07-20 19:13:28 +00004046 if (cmd->data_length < 22)
4047 goto err;
4048
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004049 /*
4050 * Set the Reservation key.
4051 *
4052 * From spc4r17, section 5.7.10:
4053 * A persistent reservation holder has its reservation key
4054 * returned in the parameter data from a PERSISTENT
4055 * RESERVE IN command with READ RESERVATION service action as
4056 * follows:
4057 * a) For a persistent reservation of the type Write Exclusive
4058 * - All Registrants or Exclusive Access ­ All Regitrants,
4059 * the reservation key shall be set to zero; or
4060 * b) For all other persistent reservation types, the
4061 * reservation key shall be set to the registered
4062 * reservation key for the I_T nexus that holds the
4063 * persistent reservation.
4064 */
4065 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
4066 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
4067 pr_res_key = 0;
4068 else
4069 pr_res_key = pr_reg->pr_res_key;
4070
4071 buf[8] = ((pr_res_key >> 56) & 0xff);
4072 buf[9] = ((pr_res_key >> 48) & 0xff);
4073 buf[10] = ((pr_res_key >> 40) & 0xff);
4074 buf[11] = ((pr_res_key >> 32) & 0xff);
4075 buf[12] = ((pr_res_key >> 24) & 0xff);
4076 buf[13] = ((pr_res_key >> 16) & 0xff);
4077 buf[14] = ((pr_res_key >> 8) & 0xff);
4078 buf[15] = (pr_res_key & 0xff);
4079 /*
4080 * Set the SCOPE and TYPE
4081 */
4082 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
4083 (pr_reg->pr_res_type & 0x0f);
4084 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00004085
4086err:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004087 spin_unlock(&se_dev->dev_reservation_lock);
Andy Grover05d1c7c2011-07-20 19:13:28 +00004088 transport_kunmap_first_data_page(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004089
4090 return 0;
4091}
4092
4093/*
4094 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
4095 *
4096 * See spc4r17 section 6.13.4 Table 165
4097 */
4098static int core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
4099{
Andy Grover5951146d2011-07-19 10:26:37 +00004100 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00004101 struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00004102 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004103 u16 add_len = 8; /* Hardcoded to 8. */
4104
4105 if (cmd->data_length < 6) {
Andy Grover6708bb22011-06-08 10:36:43 -07004106 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004107 " %u too small\n", cmd->data_length);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004108 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4109 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004110 }
4111
Andy Grover05d1c7c2011-07-20 19:13:28 +00004112 buf = transport_kmap_first_data_page(cmd);
4113
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004114 buf[0] = ((add_len << 8) & 0xff);
4115 buf[1] = (add_len & 0xff);
4116 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
4117 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
4118 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
4119 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
4120 /*
4121 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
4122 * set the TMV: Task Mask Valid bit.
4123 */
4124 buf[3] |= 0x80;
4125 /*
4126 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
4127 */
4128 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
4129 /*
4130 * PTPL_A: Persistence across Target Power Loss Active bit
4131 */
4132 if (pr_tmpl->pr_aptpl_active)
4133 buf[3] |= 0x01;
4134 /*
4135 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
4136 */
4137 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4138 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
4139 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
4140 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
4141 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
4142 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
4143
Andy Grover05d1c7c2011-07-20 19:13:28 +00004144 transport_kunmap_first_data_page(cmd);
4145
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004146 return 0;
4147}
4148
4149/*
4150 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
4151 *
4152 * See spc4r17 section 6.13.5 Table 168 and 169
4153 */
4154static int core_scsi3_pri_read_full_status(struct se_cmd *cmd)
4155{
Andy Grover5951146d2011-07-19 10:26:37 +00004156 struct se_device *se_dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004157 struct se_node_acl *se_nacl;
Andy Grovere3d6f902011-07-19 08:55:10 +00004158 struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004159 struct se_portal_group *se_tpg;
4160 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
Andy Grovere3d6f902011-07-19 08:55:10 +00004161 struct t10_reservation *pr_tmpl = &se_dev->se_sub_dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00004162 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004163 u32 add_desc_len = 0, add_len = 0, desc_len, exp_desc_len;
4164 u32 off = 8; /* off into first Full Status descriptor */
4165 int format_code = 0;
4166
4167 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07004168 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004169 " too small\n", cmd->data_length);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004170 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4171 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004172 }
4173
Andy Grover05d1c7c2011-07-20 19:13:28 +00004174 buf = transport_kmap_first_data_page(cmd);
4175
Andy Grovere3d6f902011-07-19 08:55:10 +00004176 buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
4177 buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
4178 buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
4179 buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004180
4181 spin_lock(&pr_tmpl->registration_lock);
4182 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
4183 &pr_tmpl->registration_list, pr_reg_list) {
4184
4185 se_nacl = pr_reg->pr_reg_nacl;
4186 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
4187 add_desc_len = 0;
4188
4189 atomic_inc(&pr_reg->pr_res_holders);
4190 smp_mb__after_atomic_inc();
4191 spin_unlock(&pr_tmpl->registration_lock);
4192 /*
4193 * Determine expected length of $FABRIC_MOD specific
4194 * TransportID full status descriptor..
4195 */
Andy Grovere3d6f902011-07-19 08:55:10 +00004196 exp_desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id_len(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004197 se_tpg, se_nacl, pr_reg, &format_code);
4198
4199 if ((exp_desc_len + add_len) > cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07004200 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004201 " out of buffer: %d\n", cmd->data_length);
4202 spin_lock(&pr_tmpl->registration_lock);
4203 atomic_dec(&pr_reg->pr_res_holders);
4204 smp_mb__after_atomic_dec();
4205 break;
4206 }
4207 /*
4208 * Set RESERVATION KEY
4209 */
4210 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
4211 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
4212 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
4213 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
4214 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
4215 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
4216 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
4217 buf[off++] = (pr_reg->pr_res_key & 0xff);
4218 off += 4; /* Skip Over Reserved area */
4219
4220 /*
4221 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4222 */
4223 if (pr_reg->pr_reg_all_tg_pt)
4224 buf[off] = 0x02;
4225 /*
4226 * The struct se_lun pointer will be present for the
4227 * reservation holder for PR_HOLDER bit.
4228 *
4229 * Also, if this registration is the reservation
4230 * holder, fill in SCOPE and TYPE in the next byte.
4231 */
4232 if (pr_reg->pr_res_holder) {
4233 buf[off++] |= 0x01;
4234 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
4235 (pr_reg->pr_res_type & 0x0f);
4236 } else
4237 off += 2;
4238
4239 off += 4; /* Skip over reserved area */
4240 /*
4241 * From spc4r17 6.3.15:
4242 *
4243 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4244 * IDENTIFIER field contains the relative port identifier (see
4245 * 3.1.120) of the target port that is part of the I_T nexus
4246 * described by this full status descriptor. If the ALL_TG_PT
4247 * bit is set to one, the contents of the RELATIVE TARGET PORT
4248 * IDENTIFIER field are not defined by this standard.
4249 */
Andy Grover6708bb22011-06-08 10:36:43 -07004250 if (!pr_reg->pr_reg_all_tg_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004251 struct se_port *port = pr_reg->pr_reg_tg_pt_lun->lun_sep;
4252
4253 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
4254 buf[off++] = (port->sep_rtpi & 0xff);
4255 } else
4256 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFER */
4257
4258 /*
4259 * Now, have the $FABRIC_MOD fill in the protocol identifier
4260 */
Andy Grovere3d6f902011-07-19 08:55:10 +00004261 desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id(se_tpg,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004262 se_nacl, pr_reg, &format_code, &buf[off+4]);
4263
4264 spin_lock(&pr_tmpl->registration_lock);
4265 atomic_dec(&pr_reg->pr_res_holders);
4266 smp_mb__after_atomic_dec();
4267 /*
4268 * Set the ADDITIONAL DESCRIPTOR LENGTH
4269 */
4270 buf[off++] = ((desc_len >> 24) & 0xff);
4271 buf[off++] = ((desc_len >> 16) & 0xff);
4272 buf[off++] = ((desc_len >> 8) & 0xff);
4273 buf[off++] = (desc_len & 0xff);
4274 /*
4275 * Size of full desctipor header minus TransportID
4276 * containing $FABRIC_MOD specific) initiator device/port
4277 * WWN information.
4278 *
4279 * See spc4r17 Section 6.13.5 Table 169
4280 */
4281 add_desc_len = (24 + desc_len);
4282
4283 off += desc_len;
4284 add_len += add_desc_len;
4285 }
4286 spin_unlock(&pr_tmpl->registration_lock);
4287 /*
4288 * Set ADDITIONAL_LENGTH
4289 */
4290 buf[4] = ((add_len >> 24) & 0xff);
4291 buf[5] = ((add_len >> 16) & 0xff);
4292 buf[6] = ((add_len >> 8) & 0xff);
4293 buf[7] = (add_len & 0xff);
4294
Andy Grover05d1c7c2011-07-20 19:13:28 +00004295 transport_kunmap_first_data_page(cmd);
4296
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004297 return 0;
4298}
4299
Christoph Hellwige76a35d2011-11-03 17:50:42 -04004300int target_scsi3_emulate_pr_in(struct se_task *task)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004301{
Christoph Hellwige76a35d2011-11-03 17:50:42 -04004302 struct se_cmd *cmd = task->task_se_cmd;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004303 int ret;
Christoph Hellwige76a35d2011-11-03 17:50:42 -04004304
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004305 /*
4306 * Following spc2r20 5.5.1 Reservations overview:
4307 *
4308 * If a logical unit has been reserved by any RESERVE command and is
4309 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4310 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4311 * initiator or service action and shall terminate with a RESERVATION
4312 * CONFLICT status.
4313 */
4314 if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
4315 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4316 " SPC-2 reservation is held, returning"
4317 " RESERVATION_CONFLICT\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004318 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
4319 return -EINVAL;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004320 }
4321
4322 switch (cmd->t_task_cdb[1] & 0x1f) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004323 case PRI_READ_KEYS:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004324 ret = core_scsi3_pri_read_keys(cmd);
4325 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004326 case PRI_READ_RESERVATION:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004327 ret = core_scsi3_pri_read_reservation(cmd);
4328 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004329 case PRI_REPORT_CAPABILITIES:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004330 ret = core_scsi3_pri_report_capabilities(cmd);
4331 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004332 case PRI_READ_FULL_STATUS:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004333 ret = core_scsi3_pri_read_full_status(cmd);
4334 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004335 default:
Andy Grover6708bb22011-06-08 10:36:43 -07004336 pr_err("Unknown PERSISTENT_RESERVE_IN service"
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004337 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004338 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
4339 ret = -EINVAL;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004340 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004341 }
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004342
4343 if (!ret) {
4344 task->task_scsi_status = GOOD;
4345 transport_complete_task(task, 1);
4346 }
4347 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004348}
4349
4350static int core_pt_reservation_check(struct se_cmd *cmd, u32 *pr_res_type)
4351{
4352 return 0;
4353}
4354
4355static int core_pt_seq_non_holder(
4356 struct se_cmd *cmd,
4357 unsigned char *cdb,
4358 u32 pr_reg_type)
4359{
4360 return 0;
4361}
4362
4363int core_setup_reservations(struct se_device *dev, int force_pt)
4364{
4365 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00004366 struct t10_reservation *rest = &su_dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004367 /*
4368 * If this device is from Target_Core_Mod/pSCSI, use the reservations
4369 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
4370 * cause a problem because libata and some SATA RAID HBAs appear
4371 * under Linux/SCSI, but to emulate reservations themselves.
4372 */
Andy Grovere3d6f902011-07-19 08:55:10 +00004373 if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
4374 !(dev->se_sub_dev->se_dev_attrib.emulate_reservations)) || force_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004375 rest->res_type = SPC_PASSTHROUGH;
4376 rest->pr_ops.t10_reservation_check = &core_pt_reservation_check;
4377 rest->pr_ops.t10_seq_non_holder = &core_pt_seq_non_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07004378 pr_debug("%s: Using SPC_PASSTHROUGH, no reservation"
Andy Grovere3d6f902011-07-19 08:55:10 +00004379 " emulation\n", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004380 return 0;
4381 }
4382 /*
4383 * If SPC-3 or above is reported by real or emulated struct se_device,
4384 * use emulated Persistent Reservations.
4385 */
Andy Grovere3d6f902011-07-19 08:55:10 +00004386 if (dev->transport->get_device_rev(dev) >= SCSI_3) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004387 rest->res_type = SPC3_PERSISTENT_RESERVATIONS;
4388 rest->pr_ops.t10_reservation_check = &core_scsi3_pr_reservation_check;
4389 rest->pr_ops.t10_seq_non_holder = &core_scsi3_pr_seq_non_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07004390 pr_debug("%s: Using SPC3_PERSISTENT_RESERVATIONS"
Andy Grovere3d6f902011-07-19 08:55:10 +00004391 " emulation\n", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004392 } else {
4393 rest->res_type = SPC2_RESERVATIONS;
4394 rest->pr_ops.t10_reservation_check = &core_scsi2_reservation_check;
4395 rest->pr_ops.t10_seq_non_holder =
4396 &core_scsi2_reservation_seq_non_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07004397 pr_debug("%s: Using SPC2_RESERVATIONS emulation\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004398 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004399 }
4400
4401 return 0;
4402}