]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/target/target_core_xcopy.c
target: Add missing XCOPY I/O operation sense_buffer
[mirror_ubuntu-bionic-kernel.git] / drivers / target / target_core_xcopy.c
CommitLineData
cbf031f4
NB
1/*******************************************************************************
2 * Filename: target_core_xcopy.c
3 *
4 * This file contains support for SPC-4 Extended-Copy offload with generic
5 * TCM backends.
6 *
7 * Copyright (c) 2011-2013 Datera, Inc. All rights reserved.
8 *
9 * Author:
10 * Nicholas A. Bellinger <nab@daterainc.com>
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 ******************************************************************************/
23
cbf031f4
NB
24#include <linux/slab.h>
25#include <linux/spinlock.h>
26#include <linux/list.h>
27#include <linux/configfs.h>
28#include <scsi/scsi.h>
29#include <scsi/scsi_cmnd.h>
30#include <asm/unaligned.h>
31
32#include <target/target_core_base.h>
33#include <target/target_core_backend.h>
34#include <target/target_core_fabric.h>
35#include <target/target_core_configfs.h>
36
37#include "target_core_pr.h"
38#include "target_core_ua.h"
39#include "target_core_xcopy.h"
40
41static struct workqueue_struct *xcopy_wq = NULL;
42/*
43 * From target_core_spc.c
44 */
45extern void spc_parse_naa_6h_vendor_specific(struct se_device *, unsigned char *);
46/*
47 * From target_core_device.c
48 */
49extern struct mutex g_device_mutex;
50extern struct list_head g_device_list;
51/*
52 * From target_core_configfs.c
53 */
54extern struct configfs_subsystem *target_core_subsystem[];
55
56static int target_xcopy_gen_naa_ieee(struct se_device *dev, unsigned char *buf)
57{
58 int off = 0;
59
60 buf[off++] = (0x6 << 4);
61 buf[off++] = 0x01;
62 buf[off++] = 0x40;
63 buf[off] = (0x5 << 4);
64
65 spc_parse_naa_6h_vendor_specific(dev, &buf[off]);
66 return 0;
67}
68
69static int target_xcopy_locate_se_dev_e4(struct se_cmd *se_cmd, struct xcopy_op *xop,
70 bool src)
71{
72 struct se_device *se_dev;
73 struct configfs_subsystem *subsys = target_core_subsystem[0];
74 unsigned char tmp_dev_wwn[XCOPY_NAA_IEEE_REGEX_LEN], *dev_wwn;
75 int rc;
76
77 if (src == true)
78 dev_wwn = &xop->dst_tid_wwn[0];
79 else
80 dev_wwn = &xop->src_tid_wwn[0];
81
82 mutex_lock(&g_device_mutex);
83 list_for_each_entry(se_dev, &g_device_list, g_dev_node) {
84
acb3f260
NB
85 if (!se_dev->dev_attrib.emulate_3pc)
86 continue;
87
cbf031f4
NB
88 memset(&tmp_dev_wwn[0], 0, XCOPY_NAA_IEEE_REGEX_LEN);
89 target_xcopy_gen_naa_ieee(se_dev, &tmp_dev_wwn[0]);
90
91 rc = memcmp(&tmp_dev_wwn[0], dev_wwn, XCOPY_NAA_IEEE_REGEX_LEN);
92 if (rc != 0)
93 continue;
94
95 if (src == true) {
96 xop->dst_dev = se_dev;
97 pr_debug("XCOPY 0xe4: Setting xop->dst_dev: %p from located"
98 " se_dev\n", xop->dst_dev);
99 } else {
100 xop->src_dev = se_dev;
101 pr_debug("XCOPY 0xe4: Setting xop->src_dev: %p from located"
102 " se_dev\n", xop->src_dev);
103 }
104
105 rc = configfs_depend_item(subsys,
106 &se_dev->dev_group.cg_item);
107 if (rc != 0) {
108 pr_err("configfs_depend_item attempt failed:"
109 " %d for se_dev: %p\n", rc, se_dev);
110 mutex_unlock(&g_device_mutex);
111 return rc;
112 }
113
114 pr_debug("Called configfs_depend_item for subsys: %p se_dev: %p"
115 " se_dev->se_dev_group: %p\n", subsys, se_dev,
116 &se_dev->dev_group);
117
118 mutex_unlock(&g_device_mutex);
119 return 0;
120 }
121 mutex_unlock(&g_device_mutex);
122
123 pr_err("Unable to locate 0xe4 descriptor for EXTENDED_COPY\n");
124 return -EINVAL;
125}
126
127static int target_xcopy_parse_tiddesc_e4(struct se_cmd *se_cmd, struct xcopy_op *xop,
128 unsigned char *p, bool src)
129{
130 unsigned char *desc = p;
131 unsigned short ript;
132 u8 desig_len;
133 /*
134 * Extract RELATIVE INITIATOR PORT IDENTIFIER
135 */
136 ript = get_unaligned_be16(&desc[2]);
137 pr_debug("XCOPY 0xe4: RELATIVE INITIATOR PORT IDENTIFIER: %hu\n", ript);
138 /*
139 * Check for supported code set, association, and designator type
140 */
141 if ((desc[4] & 0x0f) != 0x1) {
142 pr_err("XCOPY 0xe4: code set of non binary type not supported\n");
143 return -EINVAL;
144 }
145 if ((desc[5] & 0x30) != 0x00) {
146 pr_err("XCOPY 0xe4: association other than LUN not supported\n");
147 return -EINVAL;
148 }
149 if ((desc[5] & 0x0f) != 0x3) {
150 pr_err("XCOPY 0xe4: designator type unsupported: 0x%02x\n",
151 (desc[5] & 0x0f));
152 return -EINVAL;
153 }
154 /*
155 * Check for matching 16 byte length for NAA IEEE Registered Extended
156 * Assigned designator
157 */
158 desig_len = desc[7];
159 if (desig_len != 16) {
160 pr_err("XCOPY 0xe4: invalid desig_len: %d\n", (int)desig_len);
161 return -EINVAL;
162 }
163 pr_debug("XCOPY 0xe4: desig_len: %d\n", (int)desig_len);
164 /*
165 * Check for NAA IEEE Registered Extended Assigned header..
166 */
167 if ((desc[8] & 0xf0) != 0x60) {
168 pr_err("XCOPY 0xe4: Unsupported DESIGNATOR TYPE: 0x%02x\n",
169 (desc[8] & 0xf0));
170 return -EINVAL;
171 }
172
173 if (src == true) {
174 memcpy(&xop->src_tid_wwn[0], &desc[8], XCOPY_NAA_IEEE_REGEX_LEN);
175 /*
176 * Determine if the source designator matches the local device
177 */
178 if (!memcmp(&xop->local_dev_wwn[0], &xop->src_tid_wwn[0],
179 XCOPY_NAA_IEEE_REGEX_LEN)) {
180 xop->op_origin = XCOL_SOURCE_RECV_OP;
181 xop->src_dev = se_cmd->se_dev;
182 pr_debug("XCOPY 0xe4: Set xop->src_dev %p from source"
183 " received xop\n", xop->src_dev);
184 }
185 } else {
186 memcpy(&xop->dst_tid_wwn[0], &desc[8], XCOPY_NAA_IEEE_REGEX_LEN);
187 /*
188 * Determine if the destination designator matches the local device
189 */
190 if (!memcmp(&xop->local_dev_wwn[0], &xop->dst_tid_wwn[0],
191 XCOPY_NAA_IEEE_REGEX_LEN)) {
192 xop->op_origin = XCOL_DEST_RECV_OP;
193 xop->dst_dev = se_cmd->se_dev;
194 pr_debug("XCOPY 0xe4: Set xop->dst_dev: %p from destination"
195 " received xop\n", xop->dst_dev);
196 }
197 }
198
199 return 0;
200}
201
202static int target_xcopy_parse_target_descriptors(struct se_cmd *se_cmd,
203 struct xcopy_op *xop, unsigned char *p,
204 unsigned short tdll)
205{
206 struct se_device *local_dev = se_cmd->se_dev;
207 unsigned char *desc = p;
208 int offset = tdll % XCOPY_TARGET_DESC_LEN, rc, ret = 0;
209 unsigned short start = 0;
210 bool src = true;
211
212 if (offset != 0) {
213 pr_err("XCOPY target descriptor list length is not"
214 " multiple of %d\n", XCOPY_TARGET_DESC_LEN);
215 return -EINVAL;
216 }
217 if (tdll > 64) {
218 pr_err("XCOPY target descriptor supports a maximum"
219 " two src/dest descriptors, tdll: %hu too large..\n", tdll);
220 return -EINVAL;
221 }
222 /*
223 * Generate an IEEE Registered Extended designator based upon the
224 * se_device the XCOPY was received upon..
225 */
226 memset(&xop->local_dev_wwn[0], 0, XCOPY_NAA_IEEE_REGEX_LEN);
227 target_xcopy_gen_naa_ieee(local_dev, &xop->local_dev_wwn[0]);
228
229 while (start < tdll) {
230 /*
231 * Check target descriptor identification with 0xE4 type with
232 * use VPD 0x83 WWPN matching ..
233 */
234 switch (desc[0]) {
235 case 0xe4:
236 rc = target_xcopy_parse_tiddesc_e4(se_cmd, xop,
237 &desc[0], src);
238 if (rc != 0)
239 goto out;
240 /*
241 * Assume target descriptors are in source -> destination order..
242 */
243 if (src == true)
244 src = false;
245 else
246 src = true;
247 start += XCOPY_TARGET_DESC_LEN;
248 desc += XCOPY_TARGET_DESC_LEN;
249 ret++;
250 break;
251 default:
252 pr_err("XCOPY unsupported descriptor type code:"
253 " 0x%02x\n", desc[0]);
254 goto out;
255 }
256 }
257
258 if (xop->op_origin == XCOL_SOURCE_RECV_OP)
259 rc = target_xcopy_locate_se_dev_e4(se_cmd, xop, true);
260 else
261 rc = target_xcopy_locate_se_dev_e4(se_cmd, xop, false);
262
263 if (rc < 0)
264 goto out;
265
266 pr_debug("XCOPY TGT desc: Source dev: %p NAA IEEE WWN: 0x%16phN\n",
267 xop->src_dev, &xop->src_tid_wwn[0]);
268 pr_debug("XCOPY TGT desc: Dest dev: %p NAA IEEE WWN: 0x%16phN\n",
269 xop->dst_dev, &xop->dst_tid_wwn[0]);
270
271 return ret;
272
273out:
274 return -EINVAL;
275}
276
277static int target_xcopy_parse_segdesc_02(struct se_cmd *se_cmd, struct xcopy_op *xop,
278 unsigned char *p)
279{
280 unsigned char *desc = p;
281 int dc = (desc[1] & 0x02);
282 unsigned short desc_len;
283
284 desc_len = get_unaligned_be16(&desc[2]);
285 if (desc_len != 0x18) {
286 pr_err("XCOPY segment desc 0x02: Illegal desc_len:"
287 " %hu\n", desc_len);
288 return -EINVAL;
289 }
290
291 xop->stdi = get_unaligned_be16(&desc[4]);
292 xop->dtdi = get_unaligned_be16(&desc[6]);
293 pr_debug("XCOPY seg desc 0x02: desc_len: %hu stdi: %hu dtdi: %hu, DC: %d\n",
294 desc_len, xop->stdi, xop->dtdi, dc);
295
296 xop->nolb = get_unaligned_be16(&desc[10]);
297 xop->src_lba = get_unaligned_be64(&desc[12]);
298 xop->dst_lba = get_unaligned_be64(&desc[20]);
299 pr_debug("XCOPY seg desc 0x02: nolb: %hu src_lba: %llu dst_lba: %llu\n",
300 xop->nolb, (unsigned long long)xop->src_lba,
301 (unsigned long long)xop->dst_lba);
302
303 if (dc != 0) {
3e9e01de
NB
304 xop->dbl = (desc[29] & 0xff) << 16;
305 xop->dbl |= (desc[30] & 0xff) << 8;
cbf031f4
NB
306 xop->dbl |= desc[31] & 0xff;
307
308 pr_debug("XCOPY seg desc 0x02: DC=1 w/ dbl: %u\n", xop->dbl);
309 }
310 return 0;
311}
312
313static int target_xcopy_parse_segment_descriptors(struct se_cmd *se_cmd,
314 struct xcopy_op *xop, unsigned char *p,
315 unsigned int sdll)
316{
317 unsigned char *desc = p;
318 unsigned int start = 0;
319 int offset = sdll % XCOPY_SEGMENT_DESC_LEN, rc, ret = 0;
320
321 if (offset != 0) {
322 pr_err("XCOPY segment descriptor list length is not"
323 " multiple of %d\n", XCOPY_SEGMENT_DESC_LEN);
324 return -EINVAL;
325 }
326
327 while (start < sdll) {
328 /*
329 * Check segment descriptor type code for block -> block
330 */
331 switch (desc[0]) {
332 case 0x02:
333 rc = target_xcopy_parse_segdesc_02(se_cmd, xop, desc);
334 if (rc < 0)
335 goto out;
336
337 ret++;
338 start += XCOPY_SEGMENT_DESC_LEN;
339 desc += XCOPY_SEGMENT_DESC_LEN;
340 break;
341 default:
342 pr_err("XCOPY unspported segment descriptor"
343 "type: 0x%02x\n", desc[0]);
344 goto out;
345 }
346 }
347
348 return ret;
349
350out:
351 return -EINVAL;
352}
353
354/*
355 * Start xcopy_pt ops
356 */
357
358struct xcopy_pt_cmd {
359 bool remote_port;
360 struct se_cmd se_cmd;
361 struct xcopy_op *xcopy_op;
362 struct completion xpt_passthrough_sem;
366bda19 363 unsigned char sense_buffer[TRANSPORT_SENSE_BUFFER];
cbf031f4
NB
364};
365
366static struct se_port xcopy_pt_port;
367static struct se_portal_group xcopy_pt_tpg;
368static struct se_session xcopy_pt_sess;
369static struct se_node_acl xcopy_pt_nacl;
370
371static char *xcopy_pt_get_fabric_name(void)
372{
373 return "xcopy-pt";
374}
375
376static u32 xcopy_pt_get_tag(struct se_cmd *se_cmd)
377{
378 return 0;
379}
380
381static int xcopy_pt_get_cmd_state(struct se_cmd *se_cmd)
382{
383 return 0;
384}
385
386static void xcopy_pt_undepend_remotedev(struct xcopy_op *xop)
387{
388 struct configfs_subsystem *subsys = target_core_subsystem[0];
389 struct se_device *remote_dev;
390
391 if (xop->op_origin == XCOL_SOURCE_RECV_OP)
392 remote_dev = xop->dst_dev;
393 else
394 remote_dev = xop->src_dev;
395
396 pr_debug("Calling configfs_undepend_item for subsys: %p"
397 " remote_dev: %p remote_dev->dev_group: %p\n",
398 subsys, remote_dev, &remote_dev->dev_group.cg_item);
399
400 configfs_undepend_item(subsys, &remote_dev->dev_group.cg_item);
401}
402
403static void xcopy_pt_release_cmd(struct se_cmd *se_cmd)
404{
405 struct xcopy_pt_cmd *xpt_cmd = container_of(se_cmd,
406 struct xcopy_pt_cmd, se_cmd);
407
408 if (xpt_cmd->remote_port)
409 kfree(se_cmd->se_lun);
410
411 kfree(xpt_cmd);
412}
413
414static int xcopy_pt_check_stop_free(struct se_cmd *se_cmd)
415{
416 struct xcopy_pt_cmd *xpt_cmd = container_of(se_cmd,
417 struct xcopy_pt_cmd, se_cmd);
418
419 complete(&xpt_cmd->xpt_passthrough_sem);
420 return 0;
421}
422
423static int xcopy_pt_write_pending(struct se_cmd *se_cmd)
424{
425 return 0;
426}
427
428static int xcopy_pt_write_pending_status(struct se_cmd *se_cmd)
429{
430 return 0;
431}
432
433static int xcopy_pt_queue_data_in(struct se_cmd *se_cmd)
434{
435 return 0;
436}
437
438static int xcopy_pt_queue_status(struct se_cmd *se_cmd)
439{
440 return 0;
441}
442
443static struct target_core_fabric_ops xcopy_pt_tfo = {
444 .get_fabric_name = xcopy_pt_get_fabric_name,
445 .get_task_tag = xcopy_pt_get_tag,
446 .get_cmd_state = xcopy_pt_get_cmd_state,
447 .release_cmd = xcopy_pt_release_cmd,
448 .check_stop_free = xcopy_pt_check_stop_free,
449 .write_pending = xcopy_pt_write_pending,
450 .write_pending_status = xcopy_pt_write_pending_status,
451 .queue_data_in = xcopy_pt_queue_data_in,
452 .queue_status = xcopy_pt_queue_status,
453};
454
455/*
456 * End xcopy_pt_ops
457 */
458
459int target_xcopy_setup_pt(void)
460{
461 xcopy_wq = alloc_workqueue("xcopy_wq", WQ_MEM_RECLAIM, 0);
462 if (!xcopy_wq) {
463 pr_err("Unable to allocate xcopy_wq\n");
464 return -ENOMEM;
465 }
466
467 memset(&xcopy_pt_port, 0, sizeof(struct se_port));
468 INIT_LIST_HEAD(&xcopy_pt_port.sep_alua_list);
469 INIT_LIST_HEAD(&xcopy_pt_port.sep_list);
470 mutex_init(&xcopy_pt_port.sep_tg_pt_md_mutex);
471
472 memset(&xcopy_pt_tpg, 0, sizeof(struct se_portal_group));
473 INIT_LIST_HEAD(&xcopy_pt_tpg.se_tpg_node);
474 INIT_LIST_HEAD(&xcopy_pt_tpg.acl_node_list);
475 INIT_LIST_HEAD(&xcopy_pt_tpg.tpg_sess_list);
476
477 xcopy_pt_port.sep_tpg = &xcopy_pt_tpg;
478 xcopy_pt_tpg.se_tpg_tfo = &xcopy_pt_tfo;
479
480 memset(&xcopy_pt_nacl, 0, sizeof(struct se_node_acl));
481 INIT_LIST_HEAD(&xcopy_pt_nacl.acl_list);
482 INIT_LIST_HEAD(&xcopy_pt_nacl.acl_sess_list);
483 memset(&xcopy_pt_sess, 0, sizeof(struct se_session));
484 INIT_LIST_HEAD(&xcopy_pt_sess.sess_list);
485 INIT_LIST_HEAD(&xcopy_pt_sess.sess_acl_list);
486
487 xcopy_pt_nacl.se_tpg = &xcopy_pt_tpg;
488 xcopy_pt_nacl.nacl_sess = &xcopy_pt_sess;
489
490 xcopy_pt_sess.se_tpg = &xcopy_pt_tpg;
491 xcopy_pt_sess.se_node_acl = &xcopy_pt_nacl;
492
493 return 0;
494}
495
496void target_xcopy_release_pt(void)
497{
498 if (xcopy_wq)
499 destroy_workqueue(xcopy_wq);
500}
501
502static void target_xcopy_setup_pt_port(
503 struct xcopy_pt_cmd *xpt_cmd,
504 struct xcopy_op *xop,
505 bool remote_port)
506{
507 struct se_cmd *ec_cmd = xop->xop_se_cmd;
508 struct se_cmd *pt_cmd = &xpt_cmd->se_cmd;
509
510 if (xop->op_origin == XCOL_SOURCE_RECV_OP) {
511 /*
512 * Honor destination port reservations for X-COPY PUSH emulation
513 * when CDB is received on local source port, and READs blocks to
514 * WRITE on remote destination port.
515 */
516 if (remote_port) {
517 xpt_cmd->remote_port = remote_port;
518 pt_cmd->se_lun->lun_sep = &xcopy_pt_port;
519 pr_debug("Setup emulated remote DEST xcopy_pt_port: %p to"
520 " cmd->se_lun->lun_sep for X-COPY data PUSH\n",
521 pt_cmd->se_lun->lun_sep);
522 } else {
523 pt_cmd->se_lun = ec_cmd->se_lun;
524 pt_cmd->se_dev = ec_cmd->se_dev;
525
526 pr_debug("Honoring local SRC port from ec_cmd->se_dev:"
527 " %p\n", pt_cmd->se_dev);
528 pt_cmd->se_lun = ec_cmd->se_lun;
529 pr_debug("Honoring local SRC port from ec_cmd->se_lun: %p\n",
530 pt_cmd->se_lun);
531 }
532 } else {
533 /*
534 * Honor source port reservation for X-COPY PULL emulation
535 * when CDB is received on local desintation port, and READs
536 * blocks from the remote source port to WRITE on local
537 * destination port.
538 */
539 if (remote_port) {
540 xpt_cmd->remote_port = remote_port;
541 pt_cmd->se_lun->lun_sep = &xcopy_pt_port;
542 pr_debug("Setup emulated remote SRC xcopy_pt_port: %p to"
543 " cmd->se_lun->lun_sep for X-COPY data PULL\n",
544 pt_cmd->se_lun->lun_sep);
545 } else {
546 pt_cmd->se_lun = ec_cmd->se_lun;
547 pt_cmd->se_dev = ec_cmd->se_dev;
548
549 pr_debug("Honoring local DST port from ec_cmd->se_dev:"
550 " %p\n", pt_cmd->se_dev);
551 pt_cmd->se_lun = ec_cmd->se_lun;
552 pr_debug("Honoring local DST port from ec_cmd->se_lun: %p\n",
553 pt_cmd->se_lun);
554 }
555 }
556}
557
558static int target_xcopy_init_pt_lun(
559 struct xcopy_pt_cmd *xpt_cmd,
560 struct xcopy_op *xop,
561 struct se_device *se_dev,
562 struct se_cmd *pt_cmd,
563 bool remote_port)
564{
565 /*
566 * Don't allocate + init an pt_cmd->se_lun if honoring local port for
567 * reservations. The pt_cmd->se_lun pointer will be setup from within
568 * target_xcopy_setup_pt_port()
569 */
570 if (remote_port == false) {
571 pt_cmd->se_cmd_flags |= SCF_SE_LUN_CMD | SCF_CMD_XCOPY_PASSTHROUGH;
572 return 0;
573 }
574
575 pt_cmd->se_lun = kzalloc(sizeof(struct se_lun), GFP_KERNEL);
576 if (!pt_cmd->se_lun) {
577 pr_err("Unable to allocate pt_cmd->se_lun\n");
578 return -ENOMEM;
579 }
580 init_completion(&pt_cmd->se_lun->lun_shutdown_comp);
581 INIT_LIST_HEAD(&pt_cmd->se_lun->lun_cmd_list);
582 INIT_LIST_HEAD(&pt_cmd->se_lun->lun_acl_list);
583 spin_lock_init(&pt_cmd->se_lun->lun_acl_lock);
584 spin_lock_init(&pt_cmd->se_lun->lun_cmd_lock);
585 spin_lock_init(&pt_cmd->se_lun->lun_sep_lock);
586
587 pt_cmd->se_dev = se_dev;
588
589 pr_debug("Setup emulated se_dev: %p from se_dev\n", pt_cmd->se_dev);
590 pt_cmd->se_lun->lun_se_dev = se_dev;
591 pt_cmd->se_cmd_flags |= SCF_SE_LUN_CMD | SCF_CMD_XCOPY_PASSTHROUGH;
592
593 pr_debug("Setup emulated se_dev: %p to pt_cmd->se_lun->lun_se_dev\n",
594 pt_cmd->se_lun->lun_se_dev);
595
596 return 0;
597}
598
599static int target_xcopy_setup_pt_cmd(
600 struct xcopy_pt_cmd *xpt_cmd,
601 struct xcopy_op *xop,
602 struct se_device *se_dev,
603 unsigned char *cdb,
604 bool remote_port,
605 bool alloc_mem)
606{
607 struct se_cmd *cmd = &xpt_cmd->se_cmd;
608 sense_reason_t sense_rc;
609 int ret = 0, rc;
610 /*
611 * Setup LUN+port to honor reservations based upon xop->op_origin for
612 * X-COPY PUSH or X-COPY PULL based upon where the CDB was received.
613 */
614 rc = target_xcopy_init_pt_lun(xpt_cmd, xop, se_dev, cmd, remote_port);
615 if (rc < 0) {
616 ret = rc;
617 goto out;
618 }
619 xpt_cmd->xcopy_op = xop;
620 target_xcopy_setup_pt_port(xpt_cmd, xop, remote_port);
621
622 sense_rc = target_setup_cmd_from_cdb(cmd, cdb);
623 if (sense_rc) {
624 ret = -EINVAL;
625 goto out;
626 }
627
628 if (alloc_mem) {
629 rc = target_alloc_sgl(&cmd->t_data_sg, &cmd->t_data_nents,
630 cmd->data_length, false);
631 if (rc < 0) {
632 ret = rc;
633 goto out;
634 }
635 /*
636 * Set this bit so that transport_free_pages() allows the
637 * caller to release SGLs + physical memory allocated by
638 * transport_generic_get_mem()..
639 */
640 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
641 } else {
642 /*
643 * Here the previously allocated SGLs for the internal READ
644 * are mapped zero-copy to the internal WRITE.
645 */
646 sense_rc = transport_generic_map_mem_to_cmd(cmd,
647 xop->xop_data_sg, xop->xop_data_nents,
648 NULL, 0);
649 if (sense_rc) {
650 ret = -EINVAL;
651 goto out;
652 }
653
654 pr_debug("Setup PASSTHROUGH_NOALLOC t_data_sg: %p t_data_nents:"
655 " %u\n", cmd->t_data_sg, cmd->t_data_nents);
656 }
657
658 return 0;
659
660out:
661 if (remote_port == true)
662 kfree(cmd->se_lun);
663 return ret;
664}
665
666static int target_xcopy_issue_pt_cmd(struct xcopy_pt_cmd *xpt_cmd)
667{
668 struct se_cmd *se_cmd = &xpt_cmd->se_cmd;
669 sense_reason_t sense_rc;
670
671 sense_rc = transport_generic_new_cmd(se_cmd);
672 if (sense_rc)
673 return -EINVAL;
674
675 if (se_cmd->data_direction == DMA_TO_DEVICE)
676 target_execute_cmd(se_cmd);
677
678 wait_for_completion_interruptible(&xpt_cmd->xpt_passthrough_sem);
679
680 pr_debug("target_xcopy_issue_pt_cmd(): SCSI status: 0x%02x\n",
681 se_cmd->scsi_status);
682 return 0;
683}
684
685static int target_xcopy_read_source(
686 struct se_cmd *ec_cmd,
687 struct xcopy_op *xop,
688 struct se_device *src_dev,
689 sector_t src_lba,
690 u32 src_sectors)
691{
692 struct xcopy_pt_cmd *xpt_cmd;
693 struct se_cmd *se_cmd;
694 u32 length = (src_sectors * src_dev->dev_attrib.block_size);
695 int rc;
696 unsigned char cdb[16];
697 bool remote_port = (xop->op_origin == XCOL_DEST_RECV_OP);
698
699 xpt_cmd = kzalloc(sizeof(struct xcopy_pt_cmd), GFP_KERNEL);
700 if (!xpt_cmd) {
701 pr_err("Unable to allocate xcopy_pt_cmd\n");
702 return -ENOMEM;
703 }
704 init_completion(&xpt_cmd->xpt_passthrough_sem);
705 se_cmd = &xpt_cmd->se_cmd;
706
707 memset(&cdb[0], 0, 16);
708 cdb[0] = READ_16;
709 put_unaligned_be64(src_lba, &cdb[2]);
710 put_unaligned_be32(src_sectors, &cdb[10]);
711 pr_debug("XCOPY: Built READ_16: LBA: %llu Sectors: %u Length: %u\n",
712 (unsigned long long)src_lba, src_sectors, length);
713
714 transport_init_se_cmd(se_cmd, &xcopy_pt_tfo, NULL, length,
366bda19 715 DMA_FROM_DEVICE, 0, &xpt_cmd->sense_buffer[0]);
cbf031f4
NB
716 xop->src_pt_cmd = xpt_cmd;
717
718 rc = target_xcopy_setup_pt_cmd(xpt_cmd, xop, src_dev, &cdb[0],
719 remote_port, true);
720 if (rc < 0) {
721 transport_generic_free_cmd(se_cmd, 0);
722 return rc;
723 }
724
725 xop->xop_data_sg = se_cmd->t_data_sg;
726 xop->xop_data_nents = se_cmd->t_data_nents;
727 pr_debug("XCOPY-READ: Saved xop->xop_data_sg: %p, num: %u for READ"
728 " memory\n", xop->xop_data_sg, xop->xop_data_nents);
729
730 rc = target_xcopy_issue_pt_cmd(xpt_cmd);
731 if (rc < 0) {
732 transport_generic_free_cmd(se_cmd, 0);
733 return rc;
734 }
735 /*
736 * Clear off the allocated t_data_sg, that has been saved for
737 * zero-copy WRITE submission reuse in struct xcopy_op..
738 */
739 se_cmd->t_data_sg = NULL;
740 se_cmd->t_data_nents = 0;
741
742 return 0;
743}
744
745static int target_xcopy_write_destination(
746 struct se_cmd *ec_cmd,
747 struct xcopy_op *xop,
748 struct se_device *dst_dev,
749 sector_t dst_lba,
750 u32 dst_sectors)
751{
752 struct xcopy_pt_cmd *xpt_cmd;
753 struct se_cmd *se_cmd;
754 u32 length = (dst_sectors * dst_dev->dev_attrib.block_size);
755 int rc;
756 unsigned char cdb[16];
757 bool remote_port = (xop->op_origin == XCOL_SOURCE_RECV_OP);
758
759 xpt_cmd = kzalloc(sizeof(struct xcopy_pt_cmd), GFP_KERNEL);
760 if (!xpt_cmd) {
761 pr_err("Unable to allocate xcopy_pt_cmd\n");
762 return -ENOMEM;
763 }
764 init_completion(&xpt_cmd->xpt_passthrough_sem);
765 se_cmd = &xpt_cmd->se_cmd;
766
767 memset(&cdb[0], 0, 16);
768 cdb[0] = WRITE_16;
769 put_unaligned_be64(dst_lba, &cdb[2]);
770 put_unaligned_be32(dst_sectors, &cdb[10]);
771 pr_debug("XCOPY: Built WRITE_16: LBA: %llu Sectors: %u Length: %u\n",
772 (unsigned long long)dst_lba, dst_sectors, length);
773
774 transport_init_se_cmd(se_cmd, &xcopy_pt_tfo, NULL, length,
366bda19 775 DMA_TO_DEVICE, 0, &xpt_cmd->sense_buffer[0]);
cbf031f4
NB
776 xop->dst_pt_cmd = xpt_cmd;
777
778 rc = target_xcopy_setup_pt_cmd(xpt_cmd, xop, dst_dev, &cdb[0],
779 remote_port, false);
780 if (rc < 0) {
781 struct se_cmd *src_cmd = &xop->src_pt_cmd->se_cmd;
782 /*
783 * If the failure happened before the t_mem_list hand-off in
784 * target_xcopy_setup_pt_cmd(), Reset memory + clear flag so that
785 * core releases this memory on error during X-COPY WRITE I/O.
786 */
787 src_cmd->se_cmd_flags &= ~SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
788 src_cmd->t_data_sg = xop->xop_data_sg;
789 src_cmd->t_data_nents = xop->xop_data_nents;
790
791 transport_generic_free_cmd(se_cmd, 0);
792 return rc;
793 }
794
795 rc = target_xcopy_issue_pt_cmd(xpt_cmd);
796 if (rc < 0) {
797 se_cmd->se_cmd_flags &= ~SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
798 transport_generic_free_cmd(se_cmd, 0);
799 return rc;
800 }
801
802 return 0;
803}
804
805static void target_xcopy_do_work(struct work_struct *work)
806{
807 struct xcopy_op *xop = container_of(work, struct xcopy_op, xop_work);
808 struct se_device *src_dev = xop->src_dev, *dst_dev = xop->dst_dev;
809 struct se_cmd *ec_cmd = xop->xop_se_cmd;
810 sector_t src_lba = xop->src_lba, dst_lba = xop->dst_lba, end_lba;
811 unsigned int max_sectors;
812 int rc;
813 unsigned short nolb = xop->nolb, cur_nolb, max_nolb, copied_nolb = 0;
814
815 end_lba = src_lba + nolb;
816 /*
817 * Break up XCOPY I/O into hw_max_sectors sized I/O based on the
818 * smallest max_sectors between src_dev + dev_dev, or
819 */
820 max_sectors = min(src_dev->dev_attrib.hw_max_sectors,
821 dst_dev->dev_attrib.hw_max_sectors);
822 max_sectors = min_t(u32, max_sectors, XCOPY_MAX_SECTORS);
823
824 max_nolb = min_t(u16, max_sectors, ((u16)(~0U)));
825
826 pr_debug("target_xcopy_do_work: nolb: %hu, max_nolb: %hu end_lba: %llu\n",
827 nolb, max_nolb, (unsigned long long)end_lba);
828 pr_debug("target_xcopy_do_work: Starting src_lba: %llu, dst_lba: %llu\n",
829 (unsigned long long)src_lba, (unsigned long long)dst_lba);
830
831 while (src_lba < end_lba) {
832 cur_nolb = min(nolb, max_nolb);
833
834 pr_debug("target_xcopy_do_work: Calling read src_dev: %p src_lba: %llu,"
835 " cur_nolb: %hu\n", src_dev, (unsigned long long)src_lba, cur_nolb);
836
837 rc = target_xcopy_read_source(ec_cmd, xop, src_dev, src_lba, cur_nolb);
838 if (rc < 0)
839 goto out;
840
841 src_lba += cur_nolb;
842 pr_debug("target_xcopy_do_work: Incremented READ src_lba to %llu\n",
843 (unsigned long long)src_lba);
844
845 pr_debug("target_xcopy_do_work: Calling write dst_dev: %p dst_lba: %llu,"
846 " cur_nolb: %hu\n", dst_dev, (unsigned long long)dst_lba, cur_nolb);
847
848 rc = target_xcopy_write_destination(ec_cmd, xop, dst_dev,
849 dst_lba, cur_nolb);
850 if (rc < 0) {
851 transport_generic_free_cmd(&xop->src_pt_cmd->se_cmd, 0);
852 goto out;
853 }
854
855 dst_lba += cur_nolb;
856 pr_debug("target_xcopy_do_work: Incremented WRITE dst_lba to %llu\n",
857 (unsigned long long)dst_lba);
858
859 copied_nolb += cur_nolb;
860 nolb -= cur_nolb;
861
862 transport_generic_free_cmd(&xop->src_pt_cmd->se_cmd, 0);
863 xop->dst_pt_cmd->se_cmd.se_cmd_flags &= ~SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
864
865 transport_generic_free_cmd(&xop->dst_pt_cmd->se_cmd, 0);
866 }
867
868 xcopy_pt_undepend_remotedev(xop);
869 kfree(xop);
870
871 pr_debug("target_xcopy_do_work: Final src_lba: %llu, dst_lba: %llu\n",
872 (unsigned long long)src_lba, (unsigned long long)dst_lba);
873 pr_debug("target_xcopy_do_work: Blocks copied: %hu, Bytes Copied: %u\n",
874 copied_nolb, copied_nolb * dst_dev->dev_attrib.block_size);
875
876 pr_debug("target_xcopy_do_work: Setting X-COPY GOOD status -> sending response\n");
877 target_complete_cmd(ec_cmd, SAM_STAT_GOOD);
878 return;
879
880out:
881 xcopy_pt_undepend_remotedev(xop);
882 kfree(xop);
883
884 pr_warn("target_xcopy_do_work: Setting X-COPY CHECK_CONDITION -> sending response\n");
885 ec_cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
886 target_complete_cmd(ec_cmd, SAM_STAT_CHECK_CONDITION);
887}
888
889sense_reason_t target_do_xcopy(struct se_cmd *se_cmd)
890{
acb3f260 891 struct se_device *dev = se_cmd->se_dev;
cbf031f4
NB
892 struct xcopy_op *xop = NULL;
893 unsigned char *p = NULL, *seg_desc;
894 unsigned int list_id, list_id_usage, sdll, inline_dl, sa;
895 int rc;
896 unsigned short tdll;
897
acb3f260
NB
898 if (!dev->dev_attrib.emulate_3pc) {
899 pr_err("EXTENDED_COPY operation explicitly disabled\n");
900 return TCM_UNSUPPORTED_SCSI_OPCODE;
901 }
902
cbf031f4
NB
903 sa = se_cmd->t_task_cdb[1] & 0x1f;
904 if (sa != 0x00) {
905 pr_err("EXTENDED_COPY(LID4) not supported\n");
906 return TCM_UNSUPPORTED_SCSI_OPCODE;
907 }
908
934a138e
NB
909 xop = kzalloc(sizeof(struct xcopy_op), GFP_KERNEL);
910 if (!xop) {
911 pr_err("Unable to allocate xcopy_op\n");
912 return TCM_OUT_OF_RESOURCES;
913 }
914 xop->xop_se_cmd = se_cmd;
915
cbf031f4
NB
916 p = transport_kmap_data_sg(se_cmd);
917 if (!p) {
918 pr_err("transport_kmap_data_sg() failed in target_do_xcopy\n");
934a138e 919 kfree(xop);
cbf031f4
NB
920 return TCM_OUT_OF_RESOURCES;
921 }
922
923 list_id = p[0];
3f7a46c6
NB
924 list_id_usage = (p[1] & 0x18) >> 3;
925
cbf031f4
NB
926 /*
927 * Determine TARGET DESCRIPTOR LIST LENGTH + SEGMENT DESCRIPTOR LIST LENGTH
928 */
929 tdll = get_unaligned_be16(&p[2]);
930 sdll = get_unaligned_be32(&p[8]);
931
932 inline_dl = get_unaligned_be32(&p[12]);
933 if (inline_dl != 0) {
934 pr_err("XCOPY with non zero inline data length\n");
935 goto out;
936 }
937
cbf031f4
NB
938 pr_debug("Processing XCOPY with list_id: 0x%02x list_id_usage: 0x%02x"
939 " tdll: %hu sdll: %u inline_dl: %u\n", list_id, list_id_usage,
940 tdll, sdll, inline_dl);
941
942 rc = target_xcopy_parse_target_descriptors(se_cmd, xop, &p[16], tdll);
943 if (rc <= 0)
944 goto out;
945
946 pr_debug("XCOPY: Processed %d target descriptors, length: %u\n", rc,
947 rc * XCOPY_TARGET_DESC_LEN);
948 seg_desc = &p[16];
949 seg_desc += (rc * XCOPY_TARGET_DESC_LEN);
950
951 rc = target_xcopy_parse_segment_descriptors(se_cmd, xop, seg_desc, sdll);
952 if (rc <= 0) {
953 xcopy_pt_undepend_remotedev(xop);
954 goto out;
955 }
956 transport_kunmap_data_sg(se_cmd);
957
958 pr_debug("XCOPY: Processed %d segment descriptors, length: %u\n", rc,
959 rc * XCOPY_SEGMENT_DESC_LEN);
960 INIT_WORK(&xop->xop_work, target_xcopy_do_work);
961 queue_work(xcopy_wq, &xop->xop_work);
962 return TCM_NO_SENSE;
963
964out:
965 if (p)
966 transport_kunmap_data_sg(se_cmd);
967 kfree(xop);
934a138e 968 return TCM_INVALID_PARAMETER_LIST;
cbf031f4
NB
969}
970
971static sense_reason_t target_rcr_operating_parameters(struct se_cmd *se_cmd)
972{
973 unsigned char *p;
974
975 p = transport_kmap_data_sg(se_cmd);
976 if (!p) {
977 pr_err("transport_kmap_data_sg failed in"
978 " target_rcr_operating_parameters\n");
979 return TCM_OUT_OF_RESOURCES;
980 }
981
982 if (se_cmd->data_length < 54) {
983 pr_err("Receive Copy Results Op Parameters length"
984 " too small: %u\n", se_cmd->data_length);
985 transport_kunmap_data_sg(se_cmd);
986 return TCM_INVALID_CDB_FIELD;
987 }
988 /*
989 * Set SNLID=1 (Supports no List ID)
990 */
991 p[4] = 0x1;
992 /*
993 * MAXIMUM TARGET DESCRIPTOR COUNT
994 */
995 put_unaligned_be16(RCR_OP_MAX_TARGET_DESC_COUNT, &p[8]);
996 /*
997 * MAXIMUM SEGMENT DESCRIPTOR COUNT
998 */
999 put_unaligned_be16(RCR_OP_MAX_SG_DESC_COUNT, &p[10]);
1000 /*
1001 * MAXIMUM DESCRIPTOR LIST LENGTH
1002 */
1003 put_unaligned_be32(RCR_OP_MAX_DESC_LIST_LEN, &p[12]);
1004 /*
1005 * MAXIMUM SEGMENT LENGTH
1006 */
1007 put_unaligned_be32(RCR_OP_MAX_SEGMENT_LEN, &p[16]);
1008 /*
1009 * MAXIMUM INLINE DATA LENGTH for SA 0x04 (NOT SUPPORTED)
1010 */
1011 put_unaligned_be32(0x0, &p[20]);
1012 /*
1013 * HELD DATA LIMIT
1014 */
1015 put_unaligned_be32(0x0, &p[24]);
1016 /*
1017 * MAXIMUM STREAM DEVICE TRANSFER SIZE
1018 */
1019 put_unaligned_be32(0x0, &p[28]);
1020 /*
1021 * TOTAL CONCURRENT COPIES
1022 */
1023 put_unaligned_be16(RCR_OP_TOTAL_CONCURR_COPIES, &p[34]);
1024 /*
1025 * MAXIMUM CONCURRENT COPIES
1026 */
1027 p[36] = RCR_OP_MAX_CONCURR_COPIES;
1028 /*
1029 * DATA SEGMENT GRANULARITY (log 2)
1030 */
1031 p[37] = RCR_OP_DATA_SEG_GRAN_LOG2;
1032 /*
1033 * INLINE DATA GRANULARITY log 2)
1034 */
1035 p[38] = RCR_OP_INLINE_DATA_GRAN_LOG2;
1036 /*
1037 * HELD DATA GRANULARITY
1038 */
1039 p[39] = RCR_OP_HELD_DATA_GRAN_LOG2;
1040 /*
1041 * IMPLEMENTED DESCRIPTOR LIST LENGTH
1042 */
1043 p[43] = 0x2;
1044 /*
1045 * List of implemented descriptor type codes (ordered)
1046 */
1047 p[44] = 0x02; /* Copy Block to Block device */
1048 p[45] = 0xe4; /* Identification descriptor target descriptor */
1049
1050 /*
1051 * AVAILABLE DATA (n-3)
1052 */
1053 put_unaligned_be32(42, &p[0]);
1054
1055 transport_kunmap_data_sg(se_cmd);
1056 target_complete_cmd(se_cmd, GOOD);
1057
1058 return TCM_NO_SENSE;
1059}
1060
1061sense_reason_t target_do_receive_copy_results(struct se_cmd *se_cmd)
1062{
1063 unsigned char *cdb = &se_cmd->t_task_cdb[0];
1064 int sa = (cdb[1] & 0x1f), list_id = cdb[2];
1065 sense_reason_t rc = TCM_NO_SENSE;
1066
1067 pr_debug("Entering target_do_receive_copy_results: SA: 0x%02x, List ID:"
1068 " 0x%02x, AL: %u\n", sa, list_id, se_cmd->data_length);
1069
1070 if (list_id != 0) {
1071 pr_err("Receive Copy Results with non zero list identifier"
1072 " not supported\n");
1073 return TCM_INVALID_CDB_FIELD;
1074 }
1075
1076 switch (sa) {
1077 case RCR_SA_OPERATING_PARAMETERS:
1078 rc = target_rcr_operating_parameters(se_cmd);
1079 break;
1080 case RCR_SA_COPY_STATUS:
1081 case RCR_SA_RECEIVE_DATA:
1082 case RCR_SA_FAILED_SEGMENT_DETAILS:
1083 default:
1084 pr_err("Unsupported SA for receive copy results: 0x%02x\n", sa);
1085 return TCM_INVALID_CDB_FIELD;
1086 }
1087
1088 return rc;
1089}