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