1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2cbf031f4SNicholas Bellinger /*******************************************************************************
3cbf031f4SNicholas Bellinger  * Filename: target_core_xcopy.c
4cbf031f4SNicholas Bellinger  *
5cbf031f4SNicholas Bellinger  * This file contains support for SPC-4 Extended-Copy offload with generic
6cbf031f4SNicholas Bellinger  * TCM backends.
7cbf031f4SNicholas Bellinger  *
8cbf031f4SNicholas Bellinger  * Copyright (c) 2011-2013 Datera, Inc. All rights reserved.
9cbf031f4SNicholas Bellinger  *
10cbf031f4SNicholas Bellinger  * Author:
11cbf031f4SNicholas Bellinger  * Nicholas A. Bellinger <nab@daterainc.com>
12cbf031f4SNicholas Bellinger  *
13cbf031f4SNicholas Bellinger  ******************************************************************************/
14cbf031f4SNicholas Bellinger 
15cbf031f4SNicholas Bellinger #include <linux/slab.h>
16cbf031f4SNicholas Bellinger #include <linux/spinlock.h>
17cbf031f4SNicholas Bellinger #include <linux/list.h>
18cbf031f4SNicholas Bellinger #include <linux/configfs.h>
198dcf07beSBart Van Assche #include <linux/ratelimit.h>
20ba929992SBart Van Assche #include <scsi/scsi_proto.h>
21cbf031f4SNicholas Bellinger #include <asm/unaligned.h>
22cbf031f4SNicholas Bellinger 
23cbf031f4SNicholas Bellinger #include <target/target_core_base.h>
24cbf031f4SNicholas Bellinger #include <target/target_core_backend.h>
25cbf031f4SNicholas Bellinger #include <target/target_core_fabric.h>
26cbf031f4SNicholas Bellinger 
27b13876d2SChristoph Hellwig #include "target_core_internal.h"
28cbf031f4SNicholas Bellinger #include "target_core_pr.h"
29cbf031f4SNicholas Bellinger #include "target_core_ua.h"
30cbf031f4SNicholas Bellinger #include "target_core_xcopy.h"
31cbf031f4SNicholas Bellinger 
32cbf031f4SNicholas Bellinger static struct workqueue_struct *xcopy_wq = NULL;
33cbf031f4SNicholas Bellinger 
34d877d727SBart Van Assche static sense_reason_t target_parse_xcopy_cmd(struct xcopy_op *xop);
35d877d727SBart Van Assche 
36cbf031f4SNicholas Bellinger static int target_xcopy_gen_naa_ieee(struct se_device *dev, unsigned char *buf)
37cbf031f4SNicholas Bellinger {
38cbf031f4SNicholas Bellinger 	int off = 0;
39cbf031f4SNicholas Bellinger 
40cbf031f4SNicholas Bellinger 	buf[off++] = (0x6 << 4);
41cbf031f4SNicholas Bellinger 	buf[off++] = 0x01;
42cbf031f4SNicholas Bellinger 	buf[off++] = 0x40;
43cbf031f4SNicholas Bellinger 	buf[off] = (0x5 << 4);
44cbf031f4SNicholas Bellinger 
45cbf031f4SNicholas Bellinger 	spc_parse_naa_6h_vendor_specific(dev, &buf[off]);
46cbf031f4SNicholas Bellinger 	return 0;
47cbf031f4SNicholas Bellinger }
48cbf031f4SNicholas Bellinger 
496906d008SMike Christie struct xcopy_dev_search_info {
506906d008SMike Christie 	const unsigned char *dev_wwn;
516906d008SMike Christie 	struct se_device *found_dev;
526906d008SMike Christie };
536906d008SMike Christie 
546906d008SMike Christie static int target_xcopy_locate_se_dev_e4_iter(struct se_device *se_dev,
556906d008SMike Christie 					      void *data)
56cbf031f4SNicholas Bellinger {
576906d008SMike Christie 	struct xcopy_dev_search_info *info = data;
5894aae4caSDavid Disseldorp 	unsigned char tmp_dev_wwn[XCOPY_NAA_IEEE_REGEX_LEN];
59cbf031f4SNicholas Bellinger 	int rc;
60cbf031f4SNicholas Bellinger 
61acb3f260SNicholas Bellinger 	if (!se_dev->dev_attrib.emulate_3pc)
626906d008SMike Christie 		return 0;
63acb3f260SNicholas Bellinger 
64cbf031f4SNicholas Bellinger 	memset(&tmp_dev_wwn[0], 0, XCOPY_NAA_IEEE_REGEX_LEN);
65cbf031f4SNicholas Bellinger 	target_xcopy_gen_naa_ieee(se_dev, &tmp_dev_wwn[0]);
66cbf031f4SNicholas Bellinger 
676906d008SMike Christie 	rc = memcmp(&tmp_dev_wwn[0], info->dev_wwn, XCOPY_NAA_IEEE_REGEX_LEN);
68cbf031f4SNicholas Bellinger 	if (rc != 0)
696906d008SMike Christie 		return 0;
70cbf031f4SNicholas Bellinger 
716906d008SMike Christie 	info->found_dev = se_dev;
7294aae4caSDavid Disseldorp 	pr_debug("XCOPY 0xe4: located se_dev: %p\n", se_dev);
73cbf031f4SNicholas Bellinger 
74d588cf8fSChristoph Hellwig 	rc = target_depend_item(&se_dev->dev_group.cg_item);
75cbf031f4SNicholas Bellinger 	if (rc != 0) {
766906d008SMike Christie 		pr_err("configfs_depend_item attempt failed: %d for se_dev: %p\n",
776906d008SMike Christie 		       rc, se_dev);
78cbf031f4SNicholas Bellinger 		return rc;
79cbf031f4SNicholas Bellinger 	}
80cbf031f4SNicholas Bellinger 
816906d008SMike Christie 	pr_debug("Called configfs_depend_item for se_dev: %p se_dev->se_dev_group: %p\n",
826906d008SMike Christie 		 se_dev, &se_dev->dev_group);
836906d008SMike Christie 	return 1;
84cbf031f4SNicholas Bellinger }
85cbf031f4SNicholas Bellinger 
866906d008SMike Christie static int target_xcopy_locate_se_dev_e4(const unsigned char *dev_wwn,
876906d008SMike Christie 					struct se_device **found_dev)
886906d008SMike Christie {
896906d008SMike Christie 	struct xcopy_dev_search_info info;
906906d008SMike Christie 	int ret;
916906d008SMike Christie 
926906d008SMike Christie 	memset(&info, 0, sizeof(info));
936906d008SMike Christie 	info.dev_wwn = dev_wwn;
946906d008SMike Christie 
956906d008SMike Christie 	ret = target_for_each_device(target_xcopy_locate_se_dev_e4_iter, &info);
966906d008SMike Christie 	if (ret == 1) {
976906d008SMike Christie 		*found_dev = info.found_dev;
986906d008SMike Christie 		return 0;
996906d008SMike Christie 	} else {
100449a1378SNicholas Bellinger 		pr_debug_ratelimited("Unable to locate 0xe4 descriptor for EXTENDED_COPY\n");
101cbf031f4SNicholas Bellinger 		return -EINVAL;
102cbf031f4SNicholas Bellinger 	}
1036906d008SMike Christie }
104cbf031f4SNicholas Bellinger 
105cbf031f4SNicholas Bellinger static int target_xcopy_parse_tiddesc_e4(struct se_cmd *se_cmd, struct xcopy_op *xop,
10666640d35SDavid Disseldorp 				unsigned char *p, unsigned short cscd_index)
107cbf031f4SNicholas Bellinger {
108cbf031f4SNicholas Bellinger 	unsigned char *desc = p;
109cbf031f4SNicholas Bellinger 	unsigned short ript;
110cbf031f4SNicholas Bellinger 	u8 desig_len;
111cbf031f4SNicholas Bellinger 	/*
112cbf031f4SNicholas Bellinger 	 * Extract RELATIVE INITIATOR PORT IDENTIFIER
113cbf031f4SNicholas Bellinger 	 */
114cbf031f4SNicholas Bellinger 	ript = get_unaligned_be16(&desc[2]);
115cbf031f4SNicholas Bellinger 	pr_debug("XCOPY 0xe4: RELATIVE INITIATOR PORT IDENTIFIER: %hu\n", ript);
116cbf031f4SNicholas Bellinger 	/*
117cbf031f4SNicholas Bellinger 	 * Check for supported code set, association, and designator type
118cbf031f4SNicholas Bellinger 	 */
119cbf031f4SNicholas Bellinger 	if ((desc[4] & 0x0f) != 0x1) {
120cbf031f4SNicholas Bellinger 		pr_err("XCOPY 0xe4: code set of non binary type not supported\n");
121cbf031f4SNicholas Bellinger 		return -EINVAL;
122cbf031f4SNicholas Bellinger 	}
123cbf031f4SNicholas Bellinger 	if ((desc[5] & 0x30) != 0x00) {
124cbf031f4SNicholas Bellinger 		pr_err("XCOPY 0xe4: association other than LUN not supported\n");
125cbf031f4SNicholas Bellinger 		return -EINVAL;
126cbf031f4SNicholas Bellinger 	}
127cbf031f4SNicholas Bellinger 	if ((desc[5] & 0x0f) != 0x3) {
128cbf031f4SNicholas Bellinger 		pr_err("XCOPY 0xe4: designator type unsupported: 0x%02x\n",
129cbf031f4SNicholas Bellinger 				(desc[5] & 0x0f));
130cbf031f4SNicholas Bellinger 		return -EINVAL;
131cbf031f4SNicholas Bellinger 	}
132cbf031f4SNicholas Bellinger 	/*
133cbf031f4SNicholas Bellinger 	 * Check for matching 16 byte length for NAA IEEE Registered Extended
134cbf031f4SNicholas Bellinger 	 * Assigned designator
135cbf031f4SNicholas Bellinger 	 */
136cbf031f4SNicholas Bellinger 	desig_len = desc[7];
13795b1b51eSDavid Disseldorp 	if (desig_len != XCOPY_NAA_IEEE_REGEX_LEN) {
138cbf031f4SNicholas Bellinger 		pr_err("XCOPY 0xe4: invalid desig_len: %d\n", (int)desig_len);
139cbf031f4SNicholas Bellinger 		return -EINVAL;
140cbf031f4SNicholas Bellinger 	}
141cbf031f4SNicholas Bellinger 	pr_debug("XCOPY 0xe4: desig_len: %d\n", (int)desig_len);
142cbf031f4SNicholas Bellinger 	/*
143cbf031f4SNicholas Bellinger 	 * Check for NAA IEEE Registered Extended Assigned header..
144cbf031f4SNicholas Bellinger 	 */
145cbf031f4SNicholas Bellinger 	if ((desc[8] & 0xf0) != 0x60) {
146cbf031f4SNicholas Bellinger 		pr_err("XCOPY 0xe4: Unsupported DESIGNATOR TYPE: 0x%02x\n",
147cbf031f4SNicholas Bellinger 					(desc[8] & 0xf0));
148cbf031f4SNicholas Bellinger 		return -EINVAL;
149cbf031f4SNicholas Bellinger 	}
150cbf031f4SNicholas Bellinger 
15166640d35SDavid Disseldorp 	if (cscd_index != xop->stdi && cscd_index != xop->dtdi) {
15266640d35SDavid Disseldorp 		pr_debug("XCOPY 0xe4: ignoring CSCD entry %d - neither src nor "
15366640d35SDavid Disseldorp 			 "dest\n", cscd_index);
15466640d35SDavid Disseldorp 		return 0;
15566640d35SDavid Disseldorp 	}
15666640d35SDavid Disseldorp 
15766640d35SDavid Disseldorp 	if (cscd_index == xop->stdi) {
158cbf031f4SNicholas Bellinger 		memcpy(&xop->src_tid_wwn[0], &desc[8], XCOPY_NAA_IEEE_REGEX_LEN);
159cbf031f4SNicholas Bellinger 		/*
160cbf031f4SNicholas Bellinger 		 * Determine if the source designator matches the local device
161cbf031f4SNicholas Bellinger 		 */
162cbf031f4SNicholas Bellinger 		if (!memcmp(&xop->local_dev_wwn[0], &xop->src_tid_wwn[0],
163cbf031f4SNicholas Bellinger 				XCOPY_NAA_IEEE_REGEX_LEN)) {
164cbf031f4SNicholas Bellinger 			xop->op_origin = XCOL_SOURCE_RECV_OP;
165cbf031f4SNicholas Bellinger 			xop->src_dev = se_cmd->se_dev;
166cbf031f4SNicholas Bellinger 			pr_debug("XCOPY 0xe4: Set xop->src_dev %p from source"
167cbf031f4SNicholas Bellinger 					" received xop\n", xop->src_dev);
168cbf031f4SNicholas Bellinger 		}
16966640d35SDavid Disseldorp 	}
17066640d35SDavid Disseldorp 
17166640d35SDavid Disseldorp 	if (cscd_index == xop->dtdi) {
172cbf031f4SNicholas Bellinger 		memcpy(&xop->dst_tid_wwn[0], &desc[8], XCOPY_NAA_IEEE_REGEX_LEN);
173cbf031f4SNicholas Bellinger 		/*
17466640d35SDavid Disseldorp 		 * Determine if the destination designator matches the local
17566640d35SDavid Disseldorp 		 * device. If @cscd_index corresponds to both source (stdi) and
17666640d35SDavid Disseldorp 		 * destination (dtdi), or dtdi comes after stdi, then
17766640d35SDavid Disseldorp 		 * XCOL_DEST_RECV_OP wins.
178cbf031f4SNicholas Bellinger 		 */
179cbf031f4SNicholas Bellinger 		if (!memcmp(&xop->local_dev_wwn[0], &xop->dst_tid_wwn[0],
180cbf031f4SNicholas Bellinger 				XCOPY_NAA_IEEE_REGEX_LEN)) {
181cbf031f4SNicholas Bellinger 			xop->op_origin = XCOL_DEST_RECV_OP;
182cbf031f4SNicholas Bellinger 			xop->dst_dev = se_cmd->se_dev;
183cbf031f4SNicholas Bellinger 			pr_debug("XCOPY 0xe4: Set xop->dst_dev: %p from destination"
184cbf031f4SNicholas Bellinger 				" received xop\n", xop->dst_dev);
185cbf031f4SNicholas Bellinger 		}
186cbf031f4SNicholas Bellinger 	}
187cbf031f4SNicholas Bellinger 
188cbf031f4SNicholas Bellinger 	return 0;
189cbf031f4SNicholas Bellinger }
190cbf031f4SNicholas Bellinger 
191cbf031f4SNicholas Bellinger static int target_xcopy_parse_target_descriptors(struct se_cmd *se_cmd,
192cbf031f4SNicholas Bellinger 				struct xcopy_op *xop, unsigned char *p,
193449a1378SNicholas Bellinger 				unsigned short tdll, sense_reason_t *sense_ret)
194cbf031f4SNicholas Bellinger {
195cbf031f4SNicholas Bellinger 	struct se_device *local_dev = se_cmd->se_dev;
196cbf031f4SNicholas Bellinger 	unsigned char *desc = p;
19766640d35SDavid Disseldorp 	int offset = tdll % XCOPY_TARGET_DESC_LEN, rc;
19866640d35SDavid Disseldorp 	unsigned short cscd_index = 0;
199cbf031f4SNicholas Bellinger 	unsigned short start = 0;
200cbf031f4SNicholas Bellinger 
201449a1378SNicholas Bellinger 	*sense_ret = TCM_INVALID_PARAMETER_LIST;
202449a1378SNicholas Bellinger 
203cbf031f4SNicholas Bellinger 	if (offset != 0) {
204cbf031f4SNicholas Bellinger 		pr_err("XCOPY target descriptor list length is not"
205cbf031f4SNicholas Bellinger 			" multiple of %d\n", XCOPY_TARGET_DESC_LEN);
206c2438497SDavid Disseldorp 		*sense_ret = TCM_UNSUPPORTED_TARGET_DESC_TYPE_CODE;
207cbf031f4SNicholas Bellinger 		return -EINVAL;
208cbf031f4SNicholas Bellinger 	}
20961c35919SDavid Disseldorp 	if (tdll > RCR_OP_MAX_TARGET_DESC_COUNT * XCOPY_TARGET_DESC_LEN) {
210cbf031f4SNicholas Bellinger 		pr_err("XCOPY target descriptor supports a maximum"
211cbf031f4SNicholas Bellinger 			" two src/dest descriptors, tdll: %hu too large..\n", tdll);
21261c35919SDavid Disseldorp 		/* spc4r37 6.4.3.4 CSCD DESCRIPTOR LIST LENGTH field */
21361c35919SDavid Disseldorp 		*sense_ret = TCM_TOO_MANY_TARGET_DESCS;
214cbf031f4SNicholas Bellinger 		return -EINVAL;
215cbf031f4SNicholas Bellinger 	}
216cbf031f4SNicholas Bellinger 	/*
217cbf031f4SNicholas Bellinger 	 * Generate an IEEE Registered Extended designator based upon the
218cbf031f4SNicholas Bellinger 	 * se_device the XCOPY was received upon..
219cbf031f4SNicholas Bellinger 	 */
220cbf031f4SNicholas Bellinger 	memset(&xop->local_dev_wwn[0], 0, XCOPY_NAA_IEEE_REGEX_LEN);
221cbf031f4SNicholas Bellinger 	target_xcopy_gen_naa_ieee(local_dev, &xop->local_dev_wwn[0]);
222cbf031f4SNicholas Bellinger 
223cbf031f4SNicholas Bellinger 	while (start < tdll) {
224cbf031f4SNicholas Bellinger 		/*
22566640d35SDavid Disseldorp 		 * Check target descriptor identification with 0xE4 type, and
22666640d35SDavid Disseldorp 		 * compare the current index with the CSCD descriptor IDs in
22766640d35SDavid Disseldorp 		 * the segment descriptor. Use VPD 0x83 WWPN matching ..
228cbf031f4SNicholas Bellinger 		 */
229cbf031f4SNicholas Bellinger 		switch (desc[0]) {
230cbf031f4SNicholas Bellinger 		case 0xe4:
231cbf031f4SNicholas Bellinger 			rc = target_xcopy_parse_tiddesc_e4(se_cmd, xop,
23266640d35SDavid Disseldorp 							&desc[0], cscd_index);
233cbf031f4SNicholas Bellinger 			if (rc != 0)
234cbf031f4SNicholas Bellinger 				goto out;
235cbf031f4SNicholas Bellinger 			start += XCOPY_TARGET_DESC_LEN;
236cbf031f4SNicholas Bellinger 			desc += XCOPY_TARGET_DESC_LEN;
23766640d35SDavid Disseldorp 			cscd_index++;
238cbf031f4SNicholas Bellinger 			break;
239cbf031f4SNicholas Bellinger 		default:
240cbf031f4SNicholas Bellinger 			pr_err("XCOPY unsupported descriptor type code:"
241cbf031f4SNicholas Bellinger 					" 0x%02x\n", desc[0]);
242c2438497SDavid Disseldorp 			*sense_ret = TCM_UNSUPPORTED_TARGET_DESC_TYPE_CODE;
243cbf031f4SNicholas Bellinger 			goto out;
244cbf031f4SNicholas Bellinger 		}
245cbf031f4SNicholas Bellinger 	}
246cbf031f4SNicholas Bellinger 
24766640d35SDavid Disseldorp 	switch (xop->op_origin) {
24866640d35SDavid Disseldorp 	case XCOL_SOURCE_RECV_OP:
24994aae4caSDavid Disseldorp 		rc = target_xcopy_locate_se_dev_e4(xop->dst_tid_wwn,
25094aae4caSDavid Disseldorp 						&xop->dst_dev);
25166640d35SDavid Disseldorp 		break;
25266640d35SDavid Disseldorp 	case XCOL_DEST_RECV_OP:
25394aae4caSDavid Disseldorp 		rc = target_xcopy_locate_se_dev_e4(xop->src_tid_wwn,
25494aae4caSDavid Disseldorp 						&xop->src_dev);
25566640d35SDavid Disseldorp 		break;
25666640d35SDavid Disseldorp 	default:
25766640d35SDavid Disseldorp 		pr_err("XCOPY CSCD descriptor IDs not found in CSCD list - "
25866640d35SDavid Disseldorp 			"stdi: %hu dtdi: %hu\n", xop->stdi, xop->dtdi);
25966640d35SDavid Disseldorp 		rc = -EINVAL;
26066640d35SDavid Disseldorp 		break;
26166640d35SDavid Disseldorp 	}
262449a1378SNicholas Bellinger 	/*
263449a1378SNicholas Bellinger 	 * If a matching IEEE NAA 0x83 descriptor for the requested device
264449a1378SNicholas Bellinger 	 * is not located on this node, return COPY_ABORTED with ASQ/ASQC
265449a1378SNicholas Bellinger 	 * 0x0d/0x02 - COPY_TARGET_DEVICE_NOT_REACHABLE to request the
266449a1378SNicholas Bellinger 	 * initiator to fall back to normal copy method.
267449a1378SNicholas Bellinger 	 */
268449a1378SNicholas Bellinger 	if (rc < 0) {
269449a1378SNicholas Bellinger 		*sense_ret = TCM_COPY_TARGET_DEVICE_NOT_REACHABLE;
270cbf031f4SNicholas Bellinger 		goto out;
271449a1378SNicholas Bellinger 	}
272cbf031f4SNicholas Bellinger 
273cbf031f4SNicholas Bellinger 	pr_debug("XCOPY TGT desc: Source dev: %p NAA IEEE WWN: 0x%16phN\n",
274cbf031f4SNicholas Bellinger 		 xop->src_dev, &xop->src_tid_wwn[0]);
275cbf031f4SNicholas Bellinger 	pr_debug("XCOPY TGT desc: Dest dev: %p NAA IEEE WWN: 0x%16phN\n",
276cbf031f4SNicholas Bellinger 		 xop->dst_dev, &xop->dst_tid_wwn[0]);
277cbf031f4SNicholas Bellinger 
27866640d35SDavid Disseldorp 	return cscd_index;
279cbf031f4SNicholas Bellinger 
280cbf031f4SNicholas Bellinger out:
281cbf031f4SNicholas Bellinger 	return -EINVAL;
282cbf031f4SNicholas Bellinger }
283cbf031f4SNicholas Bellinger 
284cbf031f4SNicholas Bellinger static int target_xcopy_parse_segdesc_02(struct se_cmd *se_cmd, struct xcopy_op *xop,
285cbf031f4SNicholas Bellinger 					unsigned char *p)
286cbf031f4SNicholas Bellinger {
287cbf031f4SNicholas Bellinger 	unsigned char *desc = p;
288cbf031f4SNicholas Bellinger 	int dc = (desc[1] & 0x02);
289cbf031f4SNicholas Bellinger 	unsigned short desc_len;
290cbf031f4SNicholas Bellinger 
291cbf031f4SNicholas Bellinger 	desc_len = get_unaligned_be16(&desc[2]);
292cbf031f4SNicholas Bellinger 	if (desc_len != 0x18) {
293cbf031f4SNicholas Bellinger 		pr_err("XCOPY segment desc 0x02: Illegal desc_len:"
294cbf031f4SNicholas Bellinger 				" %hu\n", desc_len);
295cbf031f4SNicholas Bellinger 		return -EINVAL;
296cbf031f4SNicholas Bellinger 	}
297cbf031f4SNicholas Bellinger 
298cbf031f4SNicholas Bellinger 	xop->stdi = get_unaligned_be16(&desc[4]);
299cbf031f4SNicholas Bellinger 	xop->dtdi = get_unaligned_be16(&desc[6]);
300f184210bSDavid Disseldorp 
301f184210bSDavid Disseldorp 	if (xop->stdi > XCOPY_CSCD_DESC_ID_LIST_OFF_MAX ||
302f184210bSDavid Disseldorp 	    xop->dtdi > XCOPY_CSCD_DESC_ID_LIST_OFF_MAX) {
303f184210bSDavid Disseldorp 		pr_err("XCOPY segment desc 0x02: unsupported CSCD ID > 0x%x; stdi: %hu dtdi: %hu\n",
304f184210bSDavid Disseldorp 			XCOPY_CSCD_DESC_ID_LIST_OFF_MAX, xop->stdi, xop->dtdi);
305f184210bSDavid Disseldorp 		return -EINVAL;
306f184210bSDavid Disseldorp 	}
307f184210bSDavid Disseldorp 
308cbf031f4SNicholas Bellinger 	pr_debug("XCOPY seg desc 0x02: desc_len: %hu stdi: %hu dtdi: %hu, DC: %d\n",
309cbf031f4SNicholas Bellinger 		desc_len, xop->stdi, xop->dtdi, dc);
310cbf031f4SNicholas Bellinger 
311cbf031f4SNicholas Bellinger 	xop->nolb = get_unaligned_be16(&desc[10]);
312cbf031f4SNicholas Bellinger 	xop->src_lba = get_unaligned_be64(&desc[12]);
313cbf031f4SNicholas Bellinger 	xop->dst_lba = get_unaligned_be64(&desc[20]);
314cbf031f4SNicholas Bellinger 	pr_debug("XCOPY seg desc 0x02: nolb: %hu src_lba: %llu dst_lba: %llu\n",
315cbf031f4SNicholas Bellinger 		xop->nolb, (unsigned long long)xop->src_lba,
316cbf031f4SNicholas Bellinger 		(unsigned long long)xop->dst_lba);
317cbf031f4SNicholas Bellinger 
318cbf031f4SNicholas Bellinger 	if (dc != 0) {
319a85d667eSBart Van Assche 		xop->dbl = get_unaligned_be24(&desc[29]);
320cbf031f4SNicholas Bellinger 
321cbf031f4SNicholas Bellinger 		pr_debug("XCOPY seg desc 0x02: DC=1 w/ dbl: %u\n", xop->dbl);
322cbf031f4SNicholas Bellinger 	}
323cbf031f4SNicholas Bellinger 	return 0;
324cbf031f4SNicholas Bellinger }
325cbf031f4SNicholas Bellinger 
326cbf031f4SNicholas Bellinger static int target_xcopy_parse_segment_descriptors(struct se_cmd *se_cmd,
327cbf031f4SNicholas Bellinger 				struct xcopy_op *xop, unsigned char *p,
328af9f62c1SDavid Disseldorp 				unsigned int sdll, sense_reason_t *sense_ret)
329cbf031f4SNicholas Bellinger {
330cbf031f4SNicholas Bellinger 	unsigned char *desc = p;
331cbf031f4SNicholas Bellinger 	unsigned int start = 0;
332cbf031f4SNicholas Bellinger 	int offset = sdll % XCOPY_SEGMENT_DESC_LEN, rc, ret = 0;
333cbf031f4SNicholas Bellinger 
334af9f62c1SDavid Disseldorp 	*sense_ret = TCM_INVALID_PARAMETER_LIST;
335af9f62c1SDavid Disseldorp 
336cbf031f4SNicholas Bellinger 	if (offset != 0) {
337cbf031f4SNicholas Bellinger 		pr_err("XCOPY segment descriptor list length is not"
338cbf031f4SNicholas Bellinger 			" multiple of %d\n", XCOPY_SEGMENT_DESC_LEN);
339c2438497SDavid Disseldorp 		*sense_ret = TCM_UNSUPPORTED_SEGMENT_DESC_TYPE_CODE;
340cbf031f4SNicholas Bellinger 		return -EINVAL;
341cbf031f4SNicholas Bellinger 	}
342af9f62c1SDavid Disseldorp 	if (sdll > RCR_OP_MAX_SG_DESC_COUNT * XCOPY_SEGMENT_DESC_LEN) {
343af9f62c1SDavid Disseldorp 		pr_err("XCOPY supports %u segment descriptor(s), sdll: %u too"
344af9f62c1SDavid Disseldorp 			" large..\n", RCR_OP_MAX_SG_DESC_COUNT, sdll);
345af9f62c1SDavid Disseldorp 		/* spc4r37 6.4.3.5 SEGMENT DESCRIPTOR LIST LENGTH field */
346af9f62c1SDavid Disseldorp 		*sense_ret = TCM_TOO_MANY_SEGMENT_DESCS;
347af9f62c1SDavid Disseldorp 		return -EINVAL;
348af9f62c1SDavid Disseldorp 	}
349cbf031f4SNicholas Bellinger 
350cbf031f4SNicholas Bellinger 	while (start < sdll) {
351cbf031f4SNicholas Bellinger 		/*
352cbf031f4SNicholas Bellinger 		 * Check segment descriptor type code for block -> block
353cbf031f4SNicholas Bellinger 		 */
354cbf031f4SNicholas Bellinger 		switch (desc[0]) {
355cbf031f4SNicholas Bellinger 		case 0x02:
356cbf031f4SNicholas Bellinger 			rc = target_xcopy_parse_segdesc_02(se_cmd, xop, desc);
357cbf031f4SNicholas Bellinger 			if (rc < 0)
358cbf031f4SNicholas Bellinger 				goto out;
359cbf031f4SNicholas Bellinger 
360cbf031f4SNicholas Bellinger 			ret++;
361cbf031f4SNicholas Bellinger 			start += XCOPY_SEGMENT_DESC_LEN;
362cbf031f4SNicholas Bellinger 			desc += XCOPY_SEGMENT_DESC_LEN;
363cbf031f4SNicholas Bellinger 			break;
364cbf031f4SNicholas Bellinger 		default:
3656774def6SMasanari Iida 			pr_err("XCOPY unsupported segment descriptor"
366cbf031f4SNicholas Bellinger 				"type: 0x%02x\n", desc[0]);
367c2438497SDavid Disseldorp 			*sense_ret = TCM_UNSUPPORTED_SEGMENT_DESC_TYPE_CODE;
368cbf031f4SNicholas Bellinger 			goto out;
369cbf031f4SNicholas Bellinger 		}
370cbf031f4SNicholas Bellinger 	}
371cbf031f4SNicholas Bellinger 
372cbf031f4SNicholas Bellinger 	return ret;
373cbf031f4SNicholas Bellinger 
374cbf031f4SNicholas Bellinger out:
375cbf031f4SNicholas Bellinger 	return -EINVAL;
376cbf031f4SNicholas Bellinger }
377cbf031f4SNicholas Bellinger 
378cbf031f4SNicholas Bellinger /*
379cbf031f4SNicholas Bellinger  * Start xcopy_pt ops
380cbf031f4SNicholas Bellinger  */
381cbf031f4SNicholas Bellinger 
382cbf031f4SNicholas Bellinger struct xcopy_pt_cmd {
383cbf031f4SNicholas Bellinger 	struct se_cmd se_cmd;
384cbf031f4SNicholas Bellinger 	struct completion xpt_passthrough_sem;
385366bda19SNicholas Bellinger 	unsigned char sense_buffer[TRANSPORT_SENSE_BUFFER];
386cbf031f4SNicholas Bellinger };
387cbf031f4SNicholas Bellinger 
388adf653f9SChristoph Hellwig struct se_portal_group xcopy_pt_tpg;
389cbf031f4SNicholas Bellinger static struct se_session xcopy_pt_sess;
390cbf031f4SNicholas Bellinger static struct se_node_acl xcopy_pt_nacl;
391cbf031f4SNicholas Bellinger 
392cbf031f4SNicholas Bellinger static int xcopy_pt_get_cmd_state(struct se_cmd *se_cmd)
393cbf031f4SNicholas Bellinger {
394cbf031f4SNicholas Bellinger         return 0;
395cbf031f4SNicholas Bellinger }
396cbf031f4SNicholas Bellinger 
397cbf031f4SNicholas Bellinger static void xcopy_pt_undepend_remotedev(struct xcopy_op *xop)
398cbf031f4SNicholas Bellinger {
399cbf031f4SNicholas Bellinger 	struct se_device *remote_dev;
400cbf031f4SNicholas Bellinger 
401cbf031f4SNicholas Bellinger 	if (xop->op_origin == XCOL_SOURCE_RECV_OP)
402cbf031f4SNicholas Bellinger 		remote_dev = xop->dst_dev;
403cbf031f4SNicholas Bellinger 	else
404cbf031f4SNicholas Bellinger 		remote_dev = xop->src_dev;
405cbf031f4SNicholas Bellinger 
406d588cf8fSChristoph Hellwig 	pr_debug("Calling configfs_undepend_item for"
407cbf031f4SNicholas Bellinger 		  " remote_dev: %p remote_dev->dev_group: %p\n",
408d588cf8fSChristoph Hellwig 		  remote_dev, &remote_dev->dev_group.cg_item);
409cbf031f4SNicholas Bellinger 
410d588cf8fSChristoph Hellwig 	target_undepend_item(&remote_dev->dev_group.cg_item);
411cbf031f4SNicholas Bellinger }
412cbf031f4SNicholas Bellinger 
413cbf031f4SNicholas Bellinger static void xcopy_pt_release_cmd(struct se_cmd *se_cmd)
414cbf031f4SNicholas Bellinger {
415cbf031f4SNicholas Bellinger 	struct xcopy_pt_cmd *xpt_cmd = container_of(se_cmd,
416cbf031f4SNicholas Bellinger 				struct xcopy_pt_cmd, se_cmd);
417cbf031f4SNicholas Bellinger 
418cbf031f4SNicholas Bellinger 	kfree(xpt_cmd);
419cbf031f4SNicholas Bellinger }
420cbf031f4SNicholas Bellinger 
421cbf031f4SNicholas Bellinger static int xcopy_pt_check_stop_free(struct se_cmd *se_cmd)
422cbf031f4SNicholas Bellinger {
423cbf031f4SNicholas Bellinger 	struct xcopy_pt_cmd *xpt_cmd = container_of(se_cmd,
424cbf031f4SNicholas Bellinger 				struct xcopy_pt_cmd, se_cmd);
425cbf031f4SNicholas Bellinger 
426cbf031f4SNicholas Bellinger 	complete(&xpt_cmd->xpt_passthrough_sem);
427cbf031f4SNicholas Bellinger 	return 0;
428cbf031f4SNicholas Bellinger }
429cbf031f4SNicholas Bellinger 
430cbf031f4SNicholas Bellinger static int xcopy_pt_write_pending(struct se_cmd *se_cmd)
431cbf031f4SNicholas Bellinger {
432cbf031f4SNicholas Bellinger 	return 0;
433cbf031f4SNicholas Bellinger }
434cbf031f4SNicholas Bellinger 
435cbf031f4SNicholas Bellinger static int xcopy_pt_queue_data_in(struct se_cmd *se_cmd)
436cbf031f4SNicholas Bellinger {
437cbf031f4SNicholas Bellinger 	return 0;
438cbf031f4SNicholas Bellinger }
439cbf031f4SNicholas Bellinger 
440cbf031f4SNicholas Bellinger static int xcopy_pt_queue_status(struct se_cmd *se_cmd)
441cbf031f4SNicholas Bellinger {
442cbf031f4SNicholas Bellinger 	return 0;
443cbf031f4SNicholas Bellinger }
444cbf031f4SNicholas Bellinger 
4459ac8928eSChristoph Hellwig static const struct target_core_fabric_ops xcopy_pt_tfo = {
44630c7ca93SDavid Disseldorp 	.fabric_name		= "xcopy-pt",
447cbf031f4SNicholas Bellinger 	.get_cmd_state		= xcopy_pt_get_cmd_state,
448cbf031f4SNicholas Bellinger 	.release_cmd		= xcopy_pt_release_cmd,
449cbf031f4SNicholas Bellinger 	.check_stop_free	= xcopy_pt_check_stop_free,
450cbf031f4SNicholas Bellinger 	.write_pending		= xcopy_pt_write_pending,
451cbf031f4SNicholas Bellinger 	.queue_data_in		= xcopy_pt_queue_data_in,
452cbf031f4SNicholas Bellinger 	.queue_status		= xcopy_pt_queue_status,
453cbf031f4SNicholas Bellinger };
454cbf031f4SNicholas Bellinger 
455cbf031f4SNicholas Bellinger /*
456cbf031f4SNicholas Bellinger  * End xcopy_pt_ops
457cbf031f4SNicholas Bellinger  */
458cbf031f4SNicholas Bellinger 
459cbf031f4SNicholas Bellinger int target_xcopy_setup_pt(void)
460cbf031f4SNicholas Bellinger {
461ad669505SBart Van Assche 	int ret;
462ad669505SBart Van Assche 
463cbf031f4SNicholas Bellinger 	xcopy_wq = alloc_workqueue("xcopy_wq", WQ_MEM_RECLAIM, 0);
464cbf031f4SNicholas Bellinger 	if (!xcopy_wq) {
465cbf031f4SNicholas Bellinger 		pr_err("Unable to allocate xcopy_wq\n");
466cbf031f4SNicholas Bellinger 		return -ENOMEM;
467cbf031f4SNicholas Bellinger 	}
468cbf031f4SNicholas Bellinger 
469cbf031f4SNicholas Bellinger 	memset(&xcopy_pt_tpg, 0, sizeof(struct se_portal_group));
470cbf031f4SNicholas Bellinger 	INIT_LIST_HEAD(&xcopy_pt_tpg.acl_node_list);
471cbf031f4SNicholas Bellinger 	INIT_LIST_HEAD(&xcopy_pt_tpg.tpg_sess_list);
472cbf031f4SNicholas Bellinger 
473cbf031f4SNicholas Bellinger 	xcopy_pt_tpg.se_tpg_tfo = &xcopy_pt_tfo;
474cbf031f4SNicholas Bellinger 
475cbf031f4SNicholas Bellinger 	memset(&xcopy_pt_nacl, 0, sizeof(struct se_node_acl));
476cbf031f4SNicholas Bellinger 	INIT_LIST_HEAD(&xcopy_pt_nacl.acl_list);
477cbf031f4SNicholas Bellinger 	INIT_LIST_HEAD(&xcopy_pt_nacl.acl_sess_list);
478cbf031f4SNicholas Bellinger 	memset(&xcopy_pt_sess, 0, sizeof(struct se_session));
479ad669505SBart Van Assche 	ret = transport_init_session(&xcopy_pt_sess);
480ad669505SBart Van Assche 	if (ret < 0)
481ad669505SBart Van Assche 		return ret;
482cbf031f4SNicholas Bellinger 
483cbf031f4SNicholas Bellinger 	xcopy_pt_nacl.se_tpg = &xcopy_pt_tpg;
484cbf031f4SNicholas Bellinger 	xcopy_pt_nacl.nacl_sess = &xcopy_pt_sess;
485cbf031f4SNicholas Bellinger 
486cbf031f4SNicholas Bellinger 	xcopy_pt_sess.se_tpg = &xcopy_pt_tpg;
487cbf031f4SNicholas Bellinger 	xcopy_pt_sess.se_node_acl = &xcopy_pt_nacl;
488cbf031f4SNicholas Bellinger 
489cbf031f4SNicholas Bellinger 	return 0;
490cbf031f4SNicholas Bellinger }
491cbf031f4SNicholas Bellinger 
492cbf031f4SNicholas Bellinger void target_xcopy_release_pt(void)
493cbf031f4SNicholas Bellinger {
494cbf031f4SNicholas Bellinger 	if (xcopy_wq)
495cbf031f4SNicholas Bellinger 		destroy_workqueue(xcopy_wq);
496cbf031f4SNicholas Bellinger }
497cbf031f4SNicholas Bellinger 
498be71530aSBart Van Assche /*
499be71530aSBart Van Assche  * target_xcopy_setup_pt_cmd - set up a pass-through command
500be71530aSBart Van Assche  * @xpt_cmd:	 Data structure to initialize.
501be71530aSBart Van Assche  * @xop:	 Describes the XCOPY operation received from an initiator.
502be71530aSBart Van Assche  * @se_dev:	 Backend device to associate with @xpt_cmd if
503be71530aSBart Van Assche  *		 @remote_port == true.
504be71530aSBart Van Assche  * @cdb:	 SCSI CDB to be copied into @xpt_cmd.
505be71530aSBart Van Assche  * @remote_port: If false, use the LUN through which the XCOPY command has
506be71530aSBart Van Assche  *		 been received. If true, use @se_dev->xcopy_lun.
507be71530aSBart Van Assche  * @alloc_mem:	 Whether or not to allocate an SGL list.
508be71530aSBart Van Assche  *
509be71530aSBart Van Assche  * Set up a SCSI command (READ or WRITE) that will be used to execute an
510be71530aSBart Van Assche  * XCOPY command.
511be71530aSBart Van Assche  */
512cbf031f4SNicholas Bellinger static int target_xcopy_setup_pt_cmd(
513cbf031f4SNicholas Bellinger 	struct xcopy_pt_cmd *xpt_cmd,
514cbf031f4SNicholas Bellinger 	struct xcopy_op *xop,
515cbf031f4SNicholas Bellinger 	struct se_device *se_dev,
516cbf031f4SNicholas Bellinger 	unsigned char *cdb,
517cbf031f4SNicholas Bellinger 	bool remote_port,
518cbf031f4SNicholas Bellinger 	bool alloc_mem)
519cbf031f4SNicholas Bellinger {
520cbf031f4SNicholas Bellinger 	struct se_cmd *cmd = &xpt_cmd->se_cmd;
521cbf031f4SNicholas Bellinger 	sense_reason_t sense_rc;
522cbf031f4SNicholas Bellinger 	int ret = 0, rc;
523be71530aSBart Van Assche 
524cbf031f4SNicholas Bellinger 	/*
525cbf031f4SNicholas Bellinger 	 * Setup LUN+port to honor reservations based upon xop->op_origin for
526cbf031f4SNicholas Bellinger 	 * X-COPY PUSH or X-COPY PULL based upon where the CDB was received.
527cbf031f4SNicholas Bellinger 	 */
528be71530aSBart Van Assche 	if (remote_port) {
529be71530aSBart Van Assche 		cmd->se_lun = &se_dev->xcopy_lun;
530be71530aSBart Van Assche 		cmd->se_dev = se_dev;
531be71530aSBart Van Assche 	} else {
532be71530aSBart Van Assche 		cmd->se_lun = xop->xop_se_cmd->se_lun;
533be71530aSBart Van Assche 		cmd->se_dev = xop->xop_se_cmd->se_dev;
534be71530aSBart Van Assche 	}
535be71530aSBart Van Assche 	cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
536cbf031f4SNicholas Bellinger 
537649ee054SBart Van Assche 	cmd->tag = 0;
538cbf031f4SNicholas Bellinger 	sense_rc = target_setup_cmd_from_cdb(cmd, cdb);
539cbf031f4SNicholas Bellinger 	if (sense_rc) {
540cbf031f4SNicholas Bellinger 		ret = -EINVAL;
541cbf031f4SNicholas Bellinger 		goto out;
542cbf031f4SNicholas Bellinger 	}
543cbf031f4SNicholas Bellinger 
544cbf031f4SNicholas Bellinger 	if (alloc_mem) {
545cbf031f4SNicholas Bellinger 		rc = target_alloc_sgl(&cmd->t_data_sg, &cmd->t_data_nents,
546e64aa657SChristoph Hellwig 				      cmd->data_length, false, false);
547cbf031f4SNicholas Bellinger 		if (rc < 0) {
548cbf031f4SNicholas Bellinger 			ret = rc;
549cbf031f4SNicholas Bellinger 			goto out;
550cbf031f4SNicholas Bellinger 		}
551cbf031f4SNicholas Bellinger 		/*
552cbf031f4SNicholas Bellinger 		 * Set this bit so that transport_free_pages() allows the
553cbf031f4SNicholas Bellinger 		 * caller to release SGLs + physical memory allocated by
554cbf031f4SNicholas Bellinger 		 * transport_generic_get_mem()..
555cbf031f4SNicholas Bellinger 		 */
556cbf031f4SNicholas Bellinger 		cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
557cbf031f4SNicholas Bellinger 	} else {
558cbf031f4SNicholas Bellinger 		/*
559cbf031f4SNicholas Bellinger 		 * Here the previously allocated SGLs for the internal READ
560cbf031f4SNicholas Bellinger 		 * are mapped zero-copy to the internal WRITE.
561cbf031f4SNicholas Bellinger 		 */
562cbf031f4SNicholas Bellinger 		sense_rc = transport_generic_map_mem_to_cmd(cmd,
563cbf031f4SNicholas Bellinger 					xop->xop_data_sg, xop->xop_data_nents,
564cbf031f4SNicholas Bellinger 					NULL, 0);
565cbf031f4SNicholas Bellinger 		if (sense_rc) {
566cbf031f4SNicholas Bellinger 			ret = -EINVAL;
567cbf031f4SNicholas Bellinger 			goto out;
568cbf031f4SNicholas Bellinger 		}
569cbf031f4SNicholas Bellinger 
570cbf031f4SNicholas Bellinger 		pr_debug("Setup PASSTHROUGH_NOALLOC t_data_sg: %p t_data_nents:"
571cbf031f4SNicholas Bellinger 			 " %u\n", cmd->t_data_sg, cmd->t_data_nents);
572cbf031f4SNicholas Bellinger 	}
573cbf031f4SNicholas Bellinger 
574cbf031f4SNicholas Bellinger 	return 0;
575cbf031f4SNicholas Bellinger 
576cbf031f4SNicholas Bellinger out:
577cbf031f4SNicholas Bellinger 	return ret;
578cbf031f4SNicholas Bellinger }
579cbf031f4SNicholas Bellinger 
580cbf031f4SNicholas Bellinger static int target_xcopy_issue_pt_cmd(struct xcopy_pt_cmd *xpt_cmd)
581cbf031f4SNicholas Bellinger {
582cbf031f4SNicholas Bellinger 	struct se_cmd *se_cmd = &xpt_cmd->se_cmd;
583cbf031f4SNicholas Bellinger 	sense_reason_t sense_rc;
584cbf031f4SNicholas Bellinger 
585cbf031f4SNicholas Bellinger 	sense_rc = transport_generic_new_cmd(se_cmd);
586cbf031f4SNicholas Bellinger 	if (sense_rc)
587cbf031f4SNicholas Bellinger 		return -EINVAL;
588cbf031f4SNicholas Bellinger 
589cbf031f4SNicholas Bellinger 	if (se_cmd->data_direction == DMA_TO_DEVICE)
590cbf031f4SNicholas Bellinger 		target_execute_cmd(se_cmd);
591cbf031f4SNicholas Bellinger 
592cbf031f4SNicholas Bellinger 	wait_for_completion_interruptible(&xpt_cmd->xpt_passthrough_sem);
593cbf031f4SNicholas Bellinger 
594cbf031f4SNicholas Bellinger 	pr_debug("target_xcopy_issue_pt_cmd(): SCSI status: 0x%02x\n",
595cbf031f4SNicholas Bellinger 			se_cmd->scsi_status);
5968a955d6dSNicholas Bellinger 
5978a955d6dSNicholas Bellinger 	return (se_cmd->scsi_status) ? -EINVAL : 0;
598cbf031f4SNicholas Bellinger }
599cbf031f4SNicholas Bellinger 
600cbf031f4SNicholas Bellinger static int target_xcopy_read_source(
601cbf031f4SNicholas Bellinger 	struct se_cmd *ec_cmd,
602cbf031f4SNicholas Bellinger 	struct xcopy_op *xop,
603cbf031f4SNicholas Bellinger 	struct se_device *src_dev,
604cbf031f4SNicholas Bellinger 	sector_t src_lba,
605cbf031f4SNicholas Bellinger 	u32 src_sectors)
606cbf031f4SNicholas Bellinger {
607cbf031f4SNicholas Bellinger 	struct xcopy_pt_cmd *xpt_cmd;
608cbf031f4SNicholas Bellinger 	struct se_cmd *se_cmd;
609cbf031f4SNicholas Bellinger 	u32 length = (src_sectors * src_dev->dev_attrib.block_size);
610cbf031f4SNicholas Bellinger 	int rc;
611cbf031f4SNicholas Bellinger 	unsigned char cdb[16];
612cbf031f4SNicholas Bellinger 	bool remote_port = (xop->op_origin == XCOL_DEST_RECV_OP);
613cbf031f4SNicholas Bellinger 
614cbf031f4SNicholas Bellinger 	xpt_cmd = kzalloc(sizeof(struct xcopy_pt_cmd), GFP_KERNEL);
615cbf031f4SNicholas Bellinger 	if (!xpt_cmd) {
616cbf031f4SNicholas Bellinger 		pr_err("Unable to allocate xcopy_pt_cmd\n");
617cbf031f4SNicholas Bellinger 		return -ENOMEM;
618cbf031f4SNicholas Bellinger 	}
619cbf031f4SNicholas Bellinger 	init_completion(&xpt_cmd->xpt_passthrough_sem);
620cbf031f4SNicholas Bellinger 	se_cmd = &xpt_cmd->se_cmd;
621cbf031f4SNicholas Bellinger 
622cbf031f4SNicholas Bellinger 	memset(&cdb[0], 0, 16);
623cbf031f4SNicholas Bellinger 	cdb[0] = READ_16;
624cbf031f4SNicholas Bellinger 	put_unaligned_be64(src_lba, &cdb[2]);
625cbf031f4SNicholas Bellinger 	put_unaligned_be32(src_sectors, &cdb[10]);
626cbf031f4SNicholas Bellinger 	pr_debug("XCOPY: Built READ_16: LBA: %llu Sectors: %u Length: %u\n",
627cbf031f4SNicholas Bellinger 		(unsigned long long)src_lba, src_sectors, length);
628cbf031f4SNicholas Bellinger 
6294416f89bSNicholas Bellinger 	transport_init_se_cmd(se_cmd, &xcopy_pt_tfo, &xcopy_pt_sess, length,
630366bda19SNicholas Bellinger 			      DMA_FROM_DEVICE, 0, &xpt_cmd->sense_buffer[0]);
631cbf031f4SNicholas Bellinger 	xop->src_pt_cmd = xpt_cmd;
632cbf031f4SNicholas Bellinger 
633cbf031f4SNicholas Bellinger 	rc = target_xcopy_setup_pt_cmd(xpt_cmd, xop, src_dev, &cdb[0],
634cbf031f4SNicholas Bellinger 				remote_port, true);
635cbf031f4SNicholas Bellinger 	if (rc < 0) {
636926317deSDinesh Israni 		ec_cmd->scsi_status = xpt_cmd->se_cmd.scsi_status;
637cbf031f4SNicholas Bellinger 		transport_generic_free_cmd(se_cmd, 0);
638cbf031f4SNicholas Bellinger 		return rc;
639cbf031f4SNicholas Bellinger 	}
640cbf031f4SNicholas Bellinger 
641cbf031f4SNicholas Bellinger 	xop->xop_data_sg = se_cmd->t_data_sg;
642cbf031f4SNicholas Bellinger 	xop->xop_data_nents = se_cmd->t_data_nents;
643cbf031f4SNicholas Bellinger 	pr_debug("XCOPY-READ: Saved xop->xop_data_sg: %p, num: %u for READ"
644cbf031f4SNicholas Bellinger 		" memory\n", xop->xop_data_sg, xop->xop_data_nents);
645cbf031f4SNicholas Bellinger 
646cbf031f4SNicholas Bellinger 	rc = target_xcopy_issue_pt_cmd(xpt_cmd);
647cbf031f4SNicholas Bellinger 	if (rc < 0) {
648926317deSDinesh Israni 		ec_cmd->scsi_status = xpt_cmd->se_cmd.scsi_status;
649cbf031f4SNicholas Bellinger 		transport_generic_free_cmd(se_cmd, 0);
650cbf031f4SNicholas Bellinger 		return rc;
651cbf031f4SNicholas Bellinger 	}
652cbf031f4SNicholas Bellinger 	/*
653cbf031f4SNicholas Bellinger 	 * Clear off the allocated t_data_sg, that has been saved for
654cbf031f4SNicholas Bellinger 	 * zero-copy WRITE submission reuse in struct xcopy_op..
655cbf031f4SNicholas Bellinger 	 */
656cbf031f4SNicholas Bellinger 	se_cmd->t_data_sg = NULL;
657cbf031f4SNicholas Bellinger 	se_cmd->t_data_nents = 0;
658cbf031f4SNicholas Bellinger 
659cbf031f4SNicholas Bellinger 	return 0;
660cbf031f4SNicholas Bellinger }
661cbf031f4SNicholas Bellinger 
662cbf031f4SNicholas Bellinger static int target_xcopy_write_destination(
663cbf031f4SNicholas Bellinger 	struct se_cmd *ec_cmd,
664cbf031f4SNicholas Bellinger 	struct xcopy_op *xop,
665cbf031f4SNicholas Bellinger 	struct se_device *dst_dev,
666cbf031f4SNicholas Bellinger 	sector_t dst_lba,
667cbf031f4SNicholas Bellinger 	u32 dst_sectors)
668cbf031f4SNicholas Bellinger {
669cbf031f4SNicholas Bellinger 	struct xcopy_pt_cmd *xpt_cmd;
670cbf031f4SNicholas Bellinger 	struct se_cmd *se_cmd;
671cbf031f4SNicholas Bellinger 	u32 length = (dst_sectors * dst_dev->dev_attrib.block_size);
672cbf031f4SNicholas Bellinger 	int rc;
673cbf031f4SNicholas Bellinger 	unsigned char cdb[16];
674cbf031f4SNicholas Bellinger 	bool remote_port = (xop->op_origin == XCOL_SOURCE_RECV_OP);
675cbf031f4SNicholas Bellinger 
676cbf031f4SNicholas Bellinger 	xpt_cmd = kzalloc(sizeof(struct xcopy_pt_cmd), GFP_KERNEL);
677cbf031f4SNicholas Bellinger 	if (!xpt_cmd) {
678cbf031f4SNicholas Bellinger 		pr_err("Unable to allocate xcopy_pt_cmd\n");
679cbf031f4SNicholas Bellinger 		return -ENOMEM;
680cbf031f4SNicholas Bellinger 	}
681cbf031f4SNicholas Bellinger 	init_completion(&xpt_cmd->xpt_passthrough_sem);
682cbf031f4SNicholas Bellinger 	se_cmd = &xpt_cmd->se_cmd;
683cbf031f4SNicholas Bellinger 
684cbf031f4SNicholas Bellinger 	memset(&cdb[0], 0, 16);
685cbf031f4SNicholas Bellinger 	cdb[0] = WRITE_16;
686cbf031f4SNicholas Bellinger 	put_unaligned_be64(dst_lba, &cdb[2]);
687cbf031f4SNicholas Bellinger 	put_unaligned_be32(dst_sectors, &cdb[10]);
688cbf031f4SNicholas Bellinger 	pr_debug("XCOPY: Built WRITE_16: LBA: %llu Sectors: %u Length: %u\n",
689cbf031f4SNicholas Bellinger 		(unsigned long long)dst_lba, dst_sectors, length);
690cbf031f4SNicholas Bellinger 
6914416f89bSNicholas Bellinger 	transport_init_se_cmd(se_cmd, &xcopy_pt_tfo, &xcopy_pt_sess, length,
692366bda19SNicholas Bellinger 			      DMA_TO_DEVICE, 0, &xpt_cmd->sense_buffer[0]);
693cbf031f4SNicholas Bellinger 	xop->dst_pt_cmd = xpt_cmd;
694cbf031f4SNicholas Bellinger 
695cbf031f4SNicholas Bellinger 	rc = target_xcopy_setup_pt_cmd(xpt_cmd, xop, dst_dev, &cdb[0],
696cbf031f4SNicholas Bellinger 				remote_port, false);
697cbf031f4SNicholas Bellinger 	if (rc < 0) {
698cbf031f4SNicholas Bellinger 		struct se_cmd *src_cmd = &xop->src_pt_cmd->se_cmd;
699926317deSDinesh Israni 		ec_cmd->scsi_status = xpt_cmd->se_cmd.scsi_status;
700cbf031f4SNicholas Bellinger 		/*
701cbf031f4SNicholas Bellinger 		 * If the failure happened before the t_mem_list hand-off in
702cbf031f4SNicholas Bellinger 		 * target_xcopy_setup_pt_cmd(), Reset memory + clear flag so that
703cbf031f4SNicholas Bellinger 		 * core releases this memory on error during X-COPY WRITE I/O.
704cbf031f4SNicholas Bellinger 		 */
705cbf031f4SNicholas Bellinger 		src_cmd->se_cmd_flags &= ~SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
706cbf031f4SNicholas Bellinger 		src_cmd->t_data_sg = xop->xop_data_sg;
707cbf031f4SNicholas Bellinger 		src_cmd->t_data_nents = xop->xop_data_nents;
708cbf031f4SNicholas Bellinger 
709cbf031f4SNicholas Bellinger 		transport_generic_free_cmd(se_cmd, 0);
710cbf031f4SNicholas Bellinger 		return rc;
711cbf031f4SNicholas Bellinger 	}
712cbf031f4SNicholas Bellinger 
713cbf031f4SNicholas Bellinger 	rc = target_xcopy_issue_pt_cmd(xpt_cmd);
714cbf031f4SNicholas Bellinger 	if (rc < 0) {
715926317deSDinesh Israni 		ec_cmd->scsi_status = xpt_cmd->se_cmd.scsi_status;
716cbf031f4SNicholas Bellinger 		se_cmd->se_cmd_flags &= ~SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
717cbf031f4SNicholas Bellinger 		transport_generic_free_cmd(se_cmd, 0);
718cbf031f4SNicholas Bellinger 		return rc;
719cbf031f4SNicholas Bellinger 	}
720cbf031f4SNicholas Bellinger 
721cbf031f4SNicholas Bellinger 	return 0;
722cbf031f4SNicholas Bellinger }
723cbf031f4SNicholas Bellinger 
724cbf031f4SNicholas Bellinger static void target_xcopy_do_work(struct work_struct *work)
725cbf031f4SNicholas Bellinger {
726cbf031f4SNicholas Bellinger 	struct xcopy_op *xop = container_of(work, struct xcopy_op, xop_work);
727cbf031f4SNicholas Bellinger 	struct se_cmd *ec_cmd = xop->xop_se_cmd;
728d877d727SBart Van Assche 	struct se_device *src_dev, *dst_dev;
729d877d727SBart Van Assche 	sector_t src_lba, dst_lba, end_lba;
730cbf031f4SNicholas Bellinger 	unsigned int max_sectors;
731d877d727SBart Van Assche 	int rc = 0;
732d877d727SBart Van Assche 	unsigned short nolb, cur_nolb, max_nolb, copied_nolb = 0;
733cbf031f4SNicholas Bellinger 
734d877d727SBart Van Assche 	if (target_parse_xcopy_cmd(xop) != TCM_NO_SENSE)
735d877d727SBart Van Assche 		goto err_free;
736d877d727SBart Van Assche 
737d877d727SBart Van Assche 	if (WARN_ON_ONCE(!xop->src_dev) || WARN_ON_ONCE(!xop->dst_dev))
738d877d727SBart Van Assche 		goto err_free;
739d877d727SBart Van Assche 
740d877d727SBart Van Assche 	src_dev = xop->src_dev;
741d877d727SBart Van Assche 	dst_dev = xop->dst_dev;
742d877d727SBart Van Assche 	src_lba = xop->src_lba;
743d877d727SBart Van Assche 	dst_lba = xop->dst_lba;
744d877d727SBart Van Assche 	nolb = xop->nolb;
745cbf031f4SNicholas Bellinger 	end_lba = src_lba + nolb;
746cbf031f4SNicholas Bellinger 	/*
747cbf031f4SNicholas Bellinger 	 * Break up XCOPY I/O into hw_max_sectors sized I/O based on the
748cbf031f4SNicholas Bellinger 	 * smallest max_sectors between src_dev + dev_dev, or
749cbf031f4SNicholas Bellinger 	 */
750cbf031f4SNicholas Bellinger 	max_sectors = min(src_dev->dev_attrib.hw_max_sectors,
751cbf031f4SNicholas Bellinger 			  dst_dev->dev_attrib.hw_max_sectors);
752cbf031f4SNicholas Bellinger 	max_sectors = min_t(u32, max_sectors, XCOPY_MAX_SECTORS);
753cbf031f4SNicholas Bellinger 
754cbf031f4SNicholas Bellinger 	max_nolb = min_t(u16, max_sectors, ((u16)(~0U)));
755cbf031f4SNicholas Bellinger 
756cbf031f4SNicholas Bellinger 	pr_debug("target_xcopy_do_work: nolb: %hu, max_nolb: %hu end_lba: %llu\n",
757cbf031f4SNicholas Bellinger 			nolb, max_nolb, (unsigned long long)end_lba);
758cbf031f4SNicholas Bellinger 	pr_debug("target_xcopy_do_work: Starting src_lba: %llu, dst_lba: %llu\n",
759cbf031f4SNicholas Bellinger 			(unsigned long long)src_lba, (unsigned long long)dst_lba);
760cbf031f4SNicholas Bellinger 
761cbf031f4SNicholas Bellinger 	while (src_lba < end_lba) {
762cbf031f4SNicholas Bellinger 		cur_nolb = min(nolb, max_nolb);
763cbf031f4SNicholas Bellinger 
764cbf031f4SNicholas Bellinger 		pr_debug("target_xcopy_do_work: Calling read src_dev: %p src_lba: %llu,"
765cbf031f4SNicholas Bellinger 			" cur_nolb: %hu\n", src_dev, (unsigned long long)src_lba, cur_nolb);
766cbf031f4SNicholas Bellinger 
767cbf031f4SNicholas Bellinger 		rc = target_xcopy_read_source(ec_cmd, xop, src_dev, src_lba, cur_nolb);
768cbf031f4SNicholas Bellinger 		if (rc < 0)
769cbf031f4SNicholas Bellinger 			goto out;
770cbf031f4SNicholas Bellinger 
771cbf031f4SNicholas Bellinger 		src_lba += cur_nolb;
772cbf031f4SNicholas Bellinger 		pr_debug("target_xcopy_do_work: Incremented READ src_lba to %llu\n",
773cbf031f4SNicholas Bellinger 				(unsigned long long)src_lba);
774cbf031f4SNicholas Bellinger 
775cbf031f4SNicholas Bellinger 		pr_debug("target_xcopy_do_work: Calling write dst_dev: %p dst_lba: %llu,"
776cbf031f4SNicholas Bellinger 			" cur_nolb: %hu\n", dst_dev, (unsigned long long)dst_lba, cur_nolb);
777cbf031f4SNicholas Bellinger 
778cbf031f4SNicholas Bellinger 		rc = target_xcopy_write_destination(ec_cmd, xop, dst_dev,
779cbf031f4SNicholas Bellinger 						dst_lba, cur_nolb);
780cbf031f4SNicholas Bellinger 		if (rc < 0) {
781cbf031f4SNicholas Bellinger 			transport_generic_free_cmd(&xop->src_pt_cmd->se_cmd, 0);
782cbf031f4SNicholas Bellinger 			goto out;
783cbf031f4SNicholas Bellinger 		}
784cbf031f4SNicholas Bellinger 
785cbf031f4SNicholas Bellinger 		dst_lba += cur_nolb;
786cbf031f4SNicholas Bellinger 		pr_debug("target_xcopy_do_work: Incremented WRITE dst_lba to %llu\n",
787cbf031f4SNicholas Bellinger 				(unsigned long long)dst_lba);
788cbf031f4SNicholas Bellinger 
789cbf031f4SNicholas Bellinger 		copied_nolb += cur_nolb;
790cbf031f4SNicholas Bellinger 		nolb -= cur_nolb;
791cbf031f4SNicholas Bellinger 
792cbf031f4SNicholas Bellinger 		transport_generic_free_cmd(&xop->src_pt_cmd->se_cmd, 0);
793cbf031f4SNicholas Bellinger 		xop->dst_pt_cmd->se_cmd.se_cmd_flags &= ~SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
794cbf031f4SNicholas Bellinger 
795cbf031f4SNicholas Bellinger 		transport_generic_free_cmd(&xop->dst_pt_cmd->se_cmd, 0);
796cbf031f4SNicholas Bellinger 	}
797cbf031f4SNicholas Bellinger 
798cbf031f4SNicholas Bellinger 	xcopy_pt_undepend_remotedev(xop);
799cbf031f4SNicholas Bellinger 	kfree(xop);
800cbf031f4SNicholas Bellinger 
801cbf031f4SNicholas Bellinger 	pr_debug("target_xcopy_do_work: Final src_lba: %llu, dst_lba: %llu\n",
802cbf031f4SNicholas Bellinger 		(unsigned long long)src_lba, (unsigned long long)dst_lba);
803cbf031f4SNicholas Bellinger 	pr_debug("target_xcopy_do_work: Blocks copied: %hu, Bytes Copied: %u\n",
804cbf031f4SNicholas Bellinger 		copied_nolb, copied_nolb * dst_dev->dev_attrib.block_size);
805cbf031f4SNicholas Bellinger 
806cbf031f4SNicholas Bellinger 	pr_debug("target_xcopy_do_work: Setting X-COPY GOOD status -> sending response\n");
807cbf031f4SNicholas Bellinger 	target_complete_cmd(ec_cmd, SAM_STAT_GOOD);
808cbf031f4SNicholas Bellinger 	return;
809cbf031f4SNicholas Bellinger 
810cbf031f4SNicholas Bellinger out:
811cbf031f4SNicholas Bellinger 	xcopy_pt_undepend_remotedev(xop);
812d877d727SBart Van Assche 
813d877d727SBart Van Assche err_free:
814cbf031f4SNicholas Bellinger 	kfree(xop);
815926317deSDinesh Israni 	/*
816926317deSDinesh Israni 	 * Don't override an error scsi status if it has already been set
817926317deSDinesh Israni 	 */
818926317deSDinesh Israni 	if (ec_cmd->scsi_status == SAM_STAT_GOOD) {
819926317deSDinesh Israni 		pr_warn_ratelimited("target_xcopy_do_work: rc: %d, Setting X-COPY"
820926317deSDinesh Israni 			" CHECK_CONDITION -> sending response\n", rc);
821cbf031f4SNicholas Bellinger 		ec_cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
822926317deSDinesh Israni 	}
8230583c261SNicholas Bellinger 	target_complete_cmd(ec_cmd, ec_cmd->scsi_status);
824cbf031f4SNicholas Bellinger }
825cbf031f4SNicholas Bellinger 
826d877d727SBart Van Assche /*
827d877d727SBart Van Assche  * Returns TCM_NO_SENSE upon success or a sense code != TCM_NO_SENSE if parsing
828d877d727SBart Van Assche  * fails.
829d877d727SBart Van Assche  */
830d877d727SBart Van Assche static sense_reason_t target_parse_xcopy_cmd(struct xcopy_op *xop)
831cbf031f4SNicholas Bellinger {
832d877d727SBart Van Assche 	struct se_cmd *se_cmd = xop->xop_se_cmd;
833cbf031f4SNicholas Bellinger 	unsigned char *p = NULL, *seg_desc;
834d877d727SBart Van Assche 	unsigned int list_id, list_id_usage, sdll, inline_dl;
83548502ddbSNicholas Bellinger 	sense_reason_t ret = TCM_INVALID_PARAMETER_LIST;
836cbf031f4SNicholas Bellinger 	int rc;
837cbf031f4SNicholas Bellinger 	unsigned short tdll;
838cbf031f4SNicholas Bellinger 
839cbf031f4SNicholas Bellinger 	p = transport_kmap_data_sg(se_cmd);
840cbf031f4SNicholas Bellinger 	if (!p) {
841cbf031f4SNicholas Bellinger 		pr_err("transport_kmap_data_sg() failed in target_do_xcopy\n");
842cbf031f4SNicholas Bellinger 		return TCM_OUT_OF_RESOURCES;
843cbf031f4SNicholas Bellinger 	}
844cbf031f4SNicholas Bellinger 
845cbf031f4SNicholas Bellinger 	list_id = p[0];
8463f7a46c6SNicholas Bellinger 	list_id_usage = (p[1] & 0x18) >> 3;
8473f7a46c6SNicholas Bellinger 
848cbf031f4SNicholas Bellinger 	/*
849cbf031f4SNicholas Bellinger 	 * Determine TARGET DESCRIPTOR LIST LENGTH + SEGMENT DESCRIPTOR LIST LENGTH
850cbf031f4SNicholas Bellinger 	 */
851cbf031f4SNicholas Bellinger 	tdll = get_unaligned_be16(&p[2]);
852cbf031f4SNicholas Bellinger 	sdll = get_unaligned_be32(&p[8]);
8537d387066SDavid Disseldorp 	if (tdll + sdll > RCR_OP_MAX_DESC_LIST_LEN) {
8547d387066SDavid Disseldorp 		pr_err("XCOPY descriptor list length %u exceeds maximum %u\n",
8557d387066SDavid Disseldorp 		       tdll + sdll, RCR_OP_MAX_DESC_LIST_LEN);
8567d387066SDavid Disseldorp 		ret = TCM_PARAMETER_LIST_LENGTH_ERROR;
8577d387066SDavid Disseldorp 		goto out;
8587d387066SDavid Disseldorp 	}
859cbf031f4SNicholas Bellinger 
860cbf031f4SNicholas Bellinger 	inline_dl = get_unaligned_be32(&p[12]);
861cbf031f4SNicholas Bellinger 	if (inline_dl != 0) {
862cbf031f4SNicholas Bellinger 		pr_err("XCOPY with non zero inline data length\n");
863cbf031f4SNicholas Bellinger 		goto out;
864cbf031f4SNicholas Bellinger 	}
865cbf031f4SNicholas Bellinger 
866f94fd098SDavid Disseldorp 	if (se_cmd->data_length < (XCOPY_HDR_LEN + tdll + sdll + inline_dl)) {
867f94fd098SDavid Disseldorp 		pr_err("XCOPY parameter truncation: data length %u too small "
868f94fd098SDavid Disseldorp 			"for tdll: %hu sdll: %u inline_dl: %u\n",
869f94fd098SDavid Disseldorp 			se_cmd->data_length, tdll, sdll, inline_dl);
870f94fd098SDavid Disseldorp 		ret = TCM_PARAMETER_LIST_LENGTH_ERROR;
871f94fd098SDavid Disseldorp 		goto out;
872f94fd098SDavid Disseldorp 	}
873f94fd098SDavid Disseldorp 
874cbf031f4SNicholas Bellinger 	pr_debug("Processing XCOPY with list_id: 0x%02x list_id_usage: 0x%02x"
875cbf031f4SNicholas Bellinger 		" tdll: %hu sdll: %u inline_dl: %u\n", list_id, list_id_usage,
876cbf031f4SNicholas Bellinger 		tdll, sdll, inline_dl);
877cbf031f4SNicholas Bellinger 
87866640d35SDavid Disseldorp 	/*
87966640d35SDavid Disseldorp 	 * skip over the target descriptors until segment descriptors
88066640d35SDavid Disseldorp 	 * have been passed - CSCD ids are needed to determine src and dest.
88166640d35SDavid Disseldorp 	 */
88266640d35SDavid Disseldorp 	seg_desc = &p[16] + tdll;
88366640d35SDavid Disseldorp 
88466640d35SDavid Disseldorp 	rc = target_xcopy_parse_segment_descriptors(se_cmd, xop, seg_desc,
88566640d35SDavid Disseldorp 						    sdll, &ret);
88666640d35SDavid Disseldorp 	if (rc <= 0)
88766640d35SDavid Disseldorp 		goto out;
88866640d35SDavid Disseldorp 
88966640d35SDavid Disseldorp 	pr_debug("XCOPY: Processed %d segment descriptors, length: %u\n", rc,
89066640d35SDavid Disseldorp 				rc * XCOPY_SEGMENT_DESC_LEN);
89166640d35SDavid Disseldorp 
892449a1378SNicholas Bellinger 	rc = target_xcopy_parse_target_descriptors(se_cmd, xop, &p[16], tdll, &ret);
893cbf031f4SNicholas Bellinger 	if (rc <= 0)
894cbf031f4SNicholas Bellinger 		goto out;
895cbf031f4SNicholas Bellinger 
89648502ddbSNicholas Bellinger 	if (xop->src_dev->dev_attrib.block_size !=
89748502ddbSNicholas Bellinger 	    xop->dst_dev->dev_attrib.block_size) {
89848502ddbSNicholas Bellinger 		pr_err("XCOPY: Non matching src_dev block_size: %u + dst_dev"
89948502ddbSNicholas Bellinger 		       " block_size: %u currently unsupported\n",
90048502ddbSNicholas Bellinger 			xop->src_dev->dev_attrib.block_size,
90148502ddbSNicholas Bellinger 			xop->dst_dev->dev_attrib.block_size);
90248502ddbSNicholas Bellinger 		xcopy_pt_undepend_remotedev(xop);
90348502ddbSNicholas Bellinger 		ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
90448502ddbSNicholas Bellinger 		goto out;
90548502ddbSNicholas Bellinger 	}
90648502ddbSNicholas Bellinger 
907cbf031f4SNicholas Bellinger 	pr_debug("XCOPY: Processed %d target descriptors, length: %u\n", rc,
908cbf031f4SNicholas Bellinger 				rc * XCOPY_TARGET_DESC_LEN);
909cbf031f4SNicholas Bellinger 	transport_kunmap_data_sg(se_cmd);
910cbf031f4SNicholas Bellinger 	return TCM_NO_SENSE;
911cbf031f4SNicholas Bellinger 
912cbf031f4SNicholas Bellinger out:
913cbf031f4SNicholas Bellinger 	if (p)
914cbf031f4SNicholas Bellinger 		transport_kunmap_data_sg(se_cmd);
91548502ddbSNicholas Bellinger 	return ret;
916cbf031f4SNicholas Bellinger }
917cbf031f4SNicholas Bellinger 
918d877d727SBart Van Assche sense_reason_t target_do_xcopy(struct se_cmd *se_cmd)
919d877d727SBart Van Assche {
920d877d727SBart Van Assche 	struct se_device *dev = se_cmd->se_dev;
921d877d727SBart Van Assche 	struct xcopy_op *xop;
922d877d727SBart Van Assche 	unsigned int sa;
923d877d727SBart Van Assche 
924d877d727SBart Van Assche 	if (!dev->dev_attrib.emulate_3pc) {
925d877d727SBart Van Assche 		pr_err("EXTENDED_COPY operation explicitly disabled\n");
926d877d727SBart Van Assche 		return TCM_UNSUPPORTED_SCSI_OPCODE;
927d877d727SBart Van Assche 	}
928d877d727SBart Van Assche 
929d877d727SBart Van Assche 	sa = se_cmd->t_task_cdb[1] & 0x1f;
930d877d727SBart Van Assche 	if (sa != 0x00) {
931d877d727SBart Van Assche 		pr_err("EXTENDED_COPY(LID4) not supported\n");
932d877d727SBart Van Assche 		return TCM_UNSUPPORTED_SCSI_OPCODE;
933d877d727SBart Van Assche 	}
934d877d727SBart Van Assche 
935d877d727SBart Van Assche 	if (se_cmd->data_length == 0) {
936d877d727SBart Van Assche 		target_complete_cmd(se_cmd, SAM_STAT_GOOD);
937d877d727SBart Van Assche 		return TCM_NO_SENSE;
938d877d727SBart Van Assche 	}
939d877d727SBart Van Assche 	if (se_cmd->data_length < XCOPY_HDR_LEN) {
940d877d727SBart Van Assche 		pr_err("XCOPY parameter truncation: length %u < hdr_len %u\n",
941d877d727SBart Van Assche 				se_cmd->data_length, XCOPY_HDR_LEN);
942d877d727SBart Van Assche 		return TCM_PARAMETER_LIST_LENGTH_ERROR;
943d877d727SBart Van Assche 	}
944d877d727SBart Van Assche 
945d877d727SBart Van Assche 	xop = kzalloc(sizeof(struct xcopy_op), GFP_KERNEL);
946d877d727SBart Van Assche 	if (!xop)
947d877d727SBart Van Assche 		goto err;
948d877d727SBart Van Assche 	xop->xop_se_cmd = se_cmd;
949d877d727SBart Van Assche 	INIT_WORK(&xop->xop_work, target_xcopy_do_work);
950d877d727SBart Van Assche 	if (WARN_ON_ONCE(!queue_work(xcopy_wq, &xop->xop_work)))
951d877d727SBart Van Assche 		goto free;
952d877d727SBart Van Assche 	return TCM_NO_SENSE;
953d877d727SBart Van Assche 
954d877d727SBart Van Assche free:
955d877d727SBart Van Assche 	kfree(xop);
956d877d727SBart Van Assche 
957d877d727SBart Van Assche err:
958d877d727SBart Van Assche 	return TCM_OUT_OF_RESOURCES;
959d877d727SBart Van Assche }
960d877d727SBart Van Assche 
961cbf031f4SNicholas Bellinger static sense_reason_t target_rcr_operating_parameters(struct se_cmd *se_cmd)
962cbf031f4SNicholas Bellinger {
963cbf031f4SNicholas Bellinger 	unsigned char *p;
964cbf031f4SNicholas Bellinger 
965cbf031f4SNicholas Bellinger 	p = transport_kmap_data_sg(se_cmd);
966cbf031f4SNicholas Bellinger 	if (!p) {
967cbf031f4SNicholas Bellinger 		pr_err("transport_kmap_data_sg failed in"
968cbf031f4SNicholas Bellinger 		       " target_rcr_operating_parameters\n");
969cbf031f4SNicholas Bellinger 		return TCM_OUT_OF_RESOURCES;
970cbf031f4SNicholas Bellinger 	}
971cbf031f4SNicholas Bellinger 
972cbf031f4SNicholas Bellinger 	if (se_cmd->data_length < 54) {
973cbf031f4SNicholas Bellinger 		pr_err("Receive Copy Results Op Parameters length"
974cbf031f4SNicholas Bellinger 		       " too small: %u\n", se_cmd->data_length);
975cbf031f4SNicholas Bellinger 		transport_kunmap_data_sg(se_cmd);
976cbf031f4SNicholas Bellinger 		return TCM_INVALID_CDB_FIELD;
977cbf031f4SNicholas Bellinger 	}
978cbf031f4SNicholas Bellinger 	/*
979cbf031f4SNicholas Bellinger 	 * Set SNLID=1 (Supports no List ID)
980cbf031f4SNicholas Bellinger 	 */
981cbf031f4SNicholas Bellinger 	p[4] = 0x1;
982cbf031f4SNicholas Bellinger 	/*
983cbf031f4SNicholas Bellinger 	 * MAXIMUM TARGET DESCRIPTOR COUNT
984cbf031f4SNicholas Bellinger 	 */
985cbf031f4SNicholas Bellinger 	put_unaligned_be16(RCR_OP_MAX_TARGET_DESC_COUNT, &p[8]);
986cbf031f4SNicholas Bellinger 	/*
987cbf031f4SNicholas Bellinger 	 * MAXIMUM SEGMENT DESCRIPTOR COUNT
988cbf031f4SNicholas Bellinger 	 */
989cbf031f4SNicholas Bellinger 	put_unaligned_be16(RCR_OP_MAX_SG_DESC_COUNT, &p[10]);
990cbf031f4SNicholas Bellinger 	/*
991cbf031f4SNicholas Bellinger 	 * MAXIMUM DESCRIPTOR LIST LENGTH
992cbf031f4SNicholas Bellinger 	 */
993cbf031f4SNicholas Bellinger 	put_unaligned_be32(RCR_OP_MAX_DESC_LIST_LEN, &p[12]);
994cbf031f4SNicholas Bellinger 	/*
995cbf031f4SNicholas Bellinger 	 * MAXIMUM SEGMENT LENGTH
996cbf031f4SNicholas Bellinger 	 */
997cbf031f4SNicholas Bellinger 	put_unaligned_be32(RCR_OP_MAX_SEGMENT_LEN, &p[16]);
998cbf031f4SNicholas Bellinger 	/*
999cbf031f4SNicholas Bellinger 	 * MAXIMUM INLINE DATA LENGTH for SA 0x04 (NOT SUPPORTED)
1000cbf031f4SNicholas Bellinger 	 */
1001cbf031f4SNicholas Bellinger 	put_unaligned_be32(0x0, &p[20]);
1002cbf031f4SNicholas Bellinger 	/*
1003cbf031f4SNicholas Bellinger 	 * HELD DATA LIMIT
1004cbf031f4SNicholas Bellinger 	 */
1005cbf031f4SNicholas Bellinger 	put_unaligned_be32(0x0, &p[24]);
1006cbf031f4SNicholas Bellinger 	/*
1007cbf031f4SNicholas Bellinger 	 * MAXIMUM STREAM DEVICE TRANSFER SIZE
1008cbf031f4SNicholas Bellinger 	 */
1009cbf031f4SNicholas Bellinger 	put_unaligned_be32(0x0, &p[28]);
1010cbf031f4SNicholas Bellinger 	/*
1011cbf031f4SNicholas Bellinger 	 * TOTAL CONCURRENT COPIES
1012cbf031f4SNicholas Bellinger 	 */
1013cbf031f4SNicholas Bellinger 	put_unaligned_be16(RCR_OP_TOTAL_CONCURR_COPIES, &p[34]);
1014cbf031f4SNicholas Bellinger 	/*
1015cbf031f4SNicholas Bellinger 	 * MAXIMUM CONCURRENT COPIES
1016cbf031f4SNicholas Bellinger 	 */
1017cbf031f4SNicholas Bellinger 	p[36] = RCR_OP_MAX_CONCURR_COPIES;
1018cbf031f4SNicholas Bellinger 	/*
1019cbf031f4SNicholas Bellinger 	 * DATA SEGMENT GRANULARITY (log 2)
1020cbf031f4SNicholas Bellinger 	 */
1021cbf031f4SNicholas Bellinger 	p[37] = RCR_OP_DATA_SEG_GRAN_LOG2;
1022cbf031f4SNicholas Bellinger 	/*
1023cbf031f4SNicholas Bellinger 	 * INLINE DATA GRANULARITY log 2)
1024cbf031f4SNicholas Bellinger 	 */
1025cbf031f4SNicholas Bellinger 	p[38] = RCR_OP_INLINE_DATA_GRAN_LOG2;
1026cbf031f4SNicholas Bellinger 	/*
1027cbf031f4SNicholas Bellinger 	 * HELD DATA GRANULARITY
1028cbf031f4SNicholas Bellinger 	 */
1029cbf031f4SNicholas Bellinger 	p[39] = RCR_OP_HELD_DATA_GRAN_LOG2;
1030cbf031f4SNicholas Bellinger 	/*
1031cbf031f4SNicholas Bellinger 	 * IMPLEMENTED DESCRIPTOR LIST LENGTH
1032cbf031f4SNicholas Bellinger 	 */
1033cbf031f4SNicholas Bellinger 	p[43] = 0x2;
1034cbf031f4SNicholas Bellinger 	/*
1035cbf031f4SNicholas Bellinger 	 * List of implemented descriptor type codes (ordered)
1036cbf031f4SNicholas Bellinger 	 */
1037cbf031f4SNicholas Bellinger 	p[44] = 0x02; /* Copy Block to Block device */
1038cbf031f4SNicholas Bellinger 	p[45] = 0xe4; /* Identification descriptor target descriptor */
1039cbf031f4SNicholas Bellinger 
1040cbf031f4SNicholas Bellinger 	/*
1041cbf031f4SNicholas Bellinger 	 * AVAILABLE DATA (n-3)
1042cbf031f4SNicholas Bellinger 	 */
1043cbf031f4SNicholas Bellinger 	put_unaligned_be32(42, &p[0]);
1044cbf031f4SNicholas Bellinger 
1045cbf031f4SNicholas Bellinger 	transport_kunmap_data_sg(se_cmd);
1046cbf031f4SNicholas Bellinger 	target_complete_cmd(se_cmd, GOOD);
1047cbf031f4SNicholas Bellinger 
1048cbf031f4SNicholas Bellinger 	return TCM_NO_SENSE;
1049cbf031f4SNicholas Bellinger }
1050cbf031f4SNicholas Bellinger 
1051cbf031f4SNicholas Bellinger sense_reason_t target_do_receive_copy_results(struct se_cmd *se_cmd)
1052cbf031f4SNicholas Bellinger {
1053cbf031f4SNicholas Bellinger 	unsigned char *cdb = &se_cmd->t_task_cdb[0];
1054cbf031f4SNicholas Bellinger 	int sa = (cdb[1] & 0x1f), list_id = cdb[2];
1055cbf031f4SNicholas Bellinger 	sense_reason_t rc = TCM_NO_SENSE;
1056cbf031f4SNicholas Bellinger 
1057cbf031f4SNicholas Bellinger 	pr_debug("Entering target_do_receive_copy_results: SA: 0x%02x, List ID:"
1058cbf031f4SNicholas Bellinger 		" 0x%02x, AL: %u\n", sa, list_id, se_cmd->data_length);
1059cbf031f4SNicholas Bellinger 
1060cbf031f4SNicholas Bellinger 	if (list_id != 0) {
1061cbf031f4SNicholas Bellinger 		pr_err("Receive Copy Results with non zero list identifier"
1062cbf031f4SNicholas Bellinger 		       " not supported\n");
1063cbf031f4SNicholas Bellinger 		return TCM_INVALID_CDB_FIELD;
1064cbf031f4SNicholas Bellinger 	}
1065cbf031f4SNicholas Bellinger 
1066cbf031f4SNicholas Bellinger 	switch (sa) {
1067cbf031f4SNicholas Bellinger 	case RCR_SA_OPERATING_PARAMETERS:
1068cbf031f4SNicholas Bellinger 		rc = target_rcr_operating_parameters(se_cmd);
1069cbf031f4SNicholas Bellinger 		break;
1070cbf031f4SNicholas Bellinger 	case RCR_SA_COPY_STATUS:
1071cbf031f4SNicholas Bellinger 	case RCR_SA_RECEIVE_DATA:
1072cbf031f4SNicholas Bellinger 	case RCR_SA_FAILED_SEGMENT_DETAILS:
1073cbf031f4SNicholas Bellinger 	default:
1074cbf031f4SNicholas Bellinger 		pr_err("Unsupported SA for receive copy results: 0x%02x\n", sa);
1075cbf031f4SNicholas Bellinger 		return TCM_INVALID_CDB_FIELD;
1076cbf031f4SNicholas Bellinger 	}
1077cbf031f4SNicholas Bellinger 
1078cbf031f4SNicholas Bellinger 	return rc;
1079cbf031f4SNicholas Bellinger }
1080