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 
492896c938SDavid Disseldorp /**
502896c938SDavid Disseldorp  * target_xcopy_locate_se_dev_e4_iter - compare XCOPY NAA device identifiers
512896c938SDavid Disseldorp  *
522896c938SDavid Disseldorp  * @se_dev: device being considered for match
532896c938SDavid Disseldorp  * @dev_wwn: XCOPY requested NAA dev_wwn
542896c938SDavid Disseldorp  * @return: 1 on match, 0 on no-match
552896c938SDavid Disseldorp  */
566906d008SMike Christie static int target_xcopy_locate_se_dev_e4_iter(struct se_device *se_dev,
572896c938SDavid Disseldorp 					      const unsigned char *dev_wwn)
58cbf031f4SNicholas Bellinger {
5994aae4caSDavid Disseldorp 	unsigned char tmp_dev_wwn[XCOPY_NAA_IEEE_REGEX_LEN];
60cbf031f4SNicholas Bellinger 	int rc;
61cbf031f4SNicholas Bellinger 
622896c938SDavid Disseldorp 	if (!se_dev->dev_attrib.emulate_3pc) {
632896c938SDavid Disseldorp 		pr_debug("XCOPY: emulate_3pc disabled on se_dev %p\n", se_dev);
646906d008SMike Christie 		return 0;
652896c938SDavid Disseldorp 	}
66acb3f260SNicholas Bellinger 
67cbf031f4SNicholas Bellinger 	memset(&tmp_dev_wwn[0], 0, XCOPY_NAA_IEEE_REGEX_LEN);
68cbf031f4SNicholas Bellinger 	target_xcopy_gen_naa_ieee(se_dev, &tmp_dev_wwn[0]);
69cbf031f4SNicholas Bellinger 
702896c938SDavid Disseldorp 	rc = memcmp(&tmp_dev_wwn[0], dev_wwn, XCOPY_NAA_IEEE_REGEX_LEN);
712896c938SDavid Disseldorp 	if (rc != 0) {
722896c938SDavid Disseldorp 		pr_debug("XCOPY: skip non-matching: %*ph\n",
732896c938SDavid Disseldorp 			 XCOPY_NAA_IEEE_REGEX_LEN, tmp_dev_wwn);
746906d008SMike Christie 		return 0;
752896c938SDavid Disseldorp 	}
7694aae4caSDavid Disseldorp 	pr_debug("XCOPY 0xe4: located se_dev: %p\n", se_dev);
77cbf031f4SNicholas Bellinger 
786906d008SMike Christie 	return 1;
79cbf031f4SNicholas Bellinger }
80cbf031f4SNicholas Bellinger 
812896c938SDavid Disseldorp static int target_xcopy_locate_se_dev_e4(struct se_session *sess,
822896c938SDavid Disseldorp 					const unsigned char *dev_wwn,
832896c938SDavid Disseldorp 					struct se_device **_found_dev,
842896c938SDavid Disseldorp 					struct percpu_ref **_found_lun_ref)
856906d008SMike Christie {
862896c938SDavid Disseldorp 	struct se_dev_entry *deve;
872896c938SDavid Disseldorp 	struct se_node_acl *nacl;
882896c938SDavid Disseldorp 	struct se_lun *this_lun = NULL;
892896c938SDavid Disseldorp 	struct se_device *found_dev = NULL;
906906d008SMike Christie 
912896c938SDavid Disseldorp 	/* cmd with NULL sess indicates no associated $FABRIC_MOD */
922896c938SDavid Disseldorp 	if (!sess)
932896c938SDavid Disseldorp 		goto err_out;
946906d008SMike Christie 
952896c938SDavid Disseldorp 	pr_debug("XCOPY 0xe4: searching for: %*ph\n",
962896c938SDavid Disseldorp 		 XCOPY_NAA_IEEE_REGEX_LEN, dev_wwn);
972896c938SDavid Disseldorp 
982896c938SDavid Disseldorp 	nacl = sess->se_node_acl;
992896c938SDavid Disseldorp 	rcu_read_lock();
1002896c938SDavid Disseldorp 	hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
1012896c938SDavid Disseldorp 		struct se_device *this_dev;
1022896c938SDavid Disseldorp 		int rc;
1032896c938SDavid Disseldorp 
1042896c938SDavid Disseldorp 		this_lun = rcu_dereference(deve->se_lun);
1052896c938SDavid Disseldorp 		this_dev = rcu_dereference_raw(this_lun->lun_se_dev);
1062896c938SDavid Disseldorp 
1072896c938SDavid Disseldorp 		rc = target_xcopy_locate_se_dev_e4_iter(this_dev, dev_wwn);
1082896c938SDavid Disseldorp 		if (rc) {
1092896c938SDavid Disseldorp 			if (percpu_ref_tryget_live(&this_lun->lun_ref))
1102896c938SDavid Disseldorp 				found_dev = this_dev;
1112896c938SDavid Disseldorp 			break;
1122896c938SDavid Disseldorp 		}
1132896c938SDavid Disseldorp 	}
1142896c938SDavid Disseldorp 	rcu_read_unlock();
1152896c938SDavid Disseldorp 	if (found_dev == NULL)
1162896c938SDavid Disseldorp 		goto err_out;
1172896c938SDavid Disseldorp 
1182896c938SDavid Disseldorp 	pr_debug("lun_ref held for se_dev: %p se_dev->se_dev_group: %p\n",
1192896c938SDavid Disseldorp 		 found_dev, &found_dev->dev_group);
1202896c938SDavid Disseldorp 	*_found_dev = found_dev;
1212896c938SDavid Disseldorp 	*_found_lun_ref = &this_lun->lun_ref;
1226906d008SMike Christie 	return 0;
1232896c938SDavid Disseldorp err_out:
124449a1378SNicholas Bellinger 	pr_debug_ratelimited("Unable to locate 0xe4 descriptor for EXTENDED_COPY\n");
125cbf031f4SNicholas Bellinger 	return -EINVAL;
126cbf031f4SNicholas Bellinger }
127cbf031f4SNicholas Bellinger 
128cbf031f4SNicholas Bellinger static int target_xcopy_parse_tiddesc_e4(struct se_cmd *se_cmd, struct xcopy_op *xop,
12966640d35SDavid Disseldorp 				unsigned char *p, unsigned short cscd_index)
130cbf031f4SNicholas Bellinger {
131cbf031f4SNicholas Bellinger 	unsigned char *desc = p;
132cbf031f4SNicholas Bellinger 	unsigned short ript;
133cbf031f4SNicholas Bellinger 	u8 desig_len;
134cbf031f4SNicholas Bellinger 	/*
135cbf031f4SNicholas Bellinger 	 * Extract RELATIVE INITIATOR PORT IDENTIFIER
136cbf031f4SNicholas Bellinger 	 */
137cbf031f4SNicholas Bellinger 	ript = get_unaligned_be16(&desc[2]);
138cbf031f4SNicholas Bellinger 	pr_debug("XCOPY 0xe4: RELATIVE INITIATOR PORT IDENTIFIER: %hu\n", ript);
139cbf031f4SNicholas Bellinger 	/*
140cbf031f4SNicholas Bellinger 	 * Check for supported code set, association, and designator type
141cbf031f4SNicholas Bellinger 	 */
142cbf031f4SNicholas Bellinger 	if ((desc[4] & 0x0f) != 0x1) {
143cbf031f4SNicholas Bellinger 		pr_err("XCOPY 0xe4: code set of non binary type not supported\n");
144cbf031f4SNicholas Bellinger 		return -EINVAL;
145cbf031f4SNicholas Bellinger 	}
146cbf031f4SNicholas Bellinger 	if ((desc[5] & 0x30) != 0x00) {
147cbf031f4SNicholas Bellinger 		pr_err("XCOPY 0xe4: association other than LUN not supported\n");
148cbf031f4SNicholas Bellinger 		return -EINVAL;
149cbf031f4SNicholas Bellinger 	}
150cbf031f4SNicholas Bellinger 	if ((desc[5] & 0x0f) != 0x3) {
151cbf031f4SNicholas Bellinger 		pr_err("XCOPY 0xe4: designator type unsupported: 0x%02x\n",
152cbf031f4SNicholas Bellinger 				(desc[5] & 0x0f));
153cbf031f4SNicholas Bellinger 		return -EINVAL;
154cbf031f4SNicholas Bellinger 	}
155cbf031f4SNicholas Bellinger 	/*
156cbf031f4SNicholas Bellinger 	 * Check for matching 16 byte length for NAA IEEE Registered Extended
157cbf031f4SNicholas Bellinger 	 * Assigned designator
158cbf031f4SNicholas Bellinger 	 */
159cbf031f4SNicholas Bellinger 	desig_len = desc[7];
16095b1b51eSDavid Disseldorp 	if (desig_len != XCOPY_NAA_IEEE_REGEX_LEN) {
161cbf031f4SNicholas Bellinger 		pr_err("XCOPY 0xe4: invalid desig_len: %d\n", (int)desig_len);
162cbf031f4SNicholas Bellinger 		return -EINVAL;
163cbf031f4SNicholas Bellinger 	}
164cbf031f4SNicholas Bellinger 	pr_debug("XCOPY 0xe4: desig_len: %d\n", (int)desig_len);
165cbf031f4SNicholas Bellinger 	/*
166cbf031f4SNicholas Bellinger 	 * Check for NAA IEEE Registered Extended Assigned header..
167cbf031f4SNicholas Bellinger 	 */
168cbf031f4SNicholas Bellinger 	if ((desc[8] & 0xf0) != 0x60) {
169cbf031f4SNicholas Bellinger 		pr_err("XCOPY 0xe4: Unsupported DESIGNATOR TYPE: 0x%02x\n",
170cbf031f4SNicholas Bellinger 					(desc[8] & 0xf0));
171cbf031f4SNicholas Bellinger 		return -EINVAL;
172cbf031f4SNicholas Bellinger 	}
173cbf031f4SNicholas Bellinger 
17466640d35SDavid Disseldorp 	if (cscd_index != xop->stdi && cscd_index != xop->dtdi) {
17566640d35SDavid Disseldorp 		pr_debug("XCOPY 0xe4: ignoring CSCD entry %d - neither src nor "
17666640d35SDavid Disseldorp 			 "dest\n", cscd_index);
17766640d35SDavid Disseldorp 		return 0;
17866640d35SDavid Disseldorp 	}
17966640d35SDavid Disseldorp 
18066640d35SDavid Disseldorp 	if (cscd_index == xop->stdi) {
181cbf031f4SNicholas Bellinger 		memcpy(&xop->src_tid_wwn[0], &desc[8], XCOPY_NAA_IEEE_REGEX_LEN);
182cbf031f4SNicholas Bellinger 		/*
183cbf031f4SNicholas Bellinger 		 * Determine if the source designator matches the local device
184cbf031f4SNicholas Bellinger 		 */
185cbf031f4SNicholas Bellinger 		if (!memcmp(&xop->local_dev_wwn[0], &xop->src_tid_wwn[0],
186cbf031f4SNicholas Bellinger 				XCOPY_NAA_IEEE_REGEX_LEN)) {
187cbf031f4SNicholas Bellinger 			xop->op_origin = XCOL_SOURCE_RECV_OP;
188cbf031f4SNicholas Bellinger 			xop->src_dev = se_cmd->se_dev;
189cbf031f4SNicholas Bellinger 			pr_debug("XCOPY 0xe4: Set xop->src_dev %p from source"
190cbf031f4SNicholas Bellinger 					" received xop\n", xop->src_dev);
191cbf031f4SNicholas Bellinger 		}
19266640d35SDavid Disseldorp 	}
19366640d35SDavid Disseldorp 
19466640d35SDavid Disseldorp 	if (cscd_index == xop->dtdi) {
195cbf031f4SNicholas Bellinger 		memcpy(&xop->dst_tid_wwn[0], &desc[8], XCOPY_NAA_IEEE_REGEX_LEN);
196cbf031f4SNicholas Bellinger 		/*
19766640d35SDavid Disseldorp 		 * Determine if the destination designator matches the local
19866640d35SDavid Disseldorp 		 * device. If @cscd_index corresponds to both source (stdi) and
19966640d35SDavid Disseldorp 		 * destination (dtdi), or dtdi comes after stdi, then
20066640d35SDavid Disseldorp 		 * XCOL_DEST_RECV_OP wins.
201cbf031f4SNicholas Bellinger 		 */
202cbf031f4SNicholas Bellinger 		if (!memcmp(&xop->local_dev_wwn[0], &xop->dst_tid_wwn[0],
203cbf031f4SNicholas Bellinger 				XCOPY_NAA_IEEE_REGEX_LEN)) {
204cbf031f4SNicholas Bellinger 			xop->op_origin = XCOL_DEST_RECV_OP;
205cbf031f4SNicholas Bellinger 			xop->dst_dev = se_cmd->se_dev;
206cbf031f4SNicholas Bellinger 			pr_debug("XCOPY 0xe4: Set xop->dst_dev: %p from destination"
207cbf031f4SNicholas Bellinger 				" received xop\n", xop->dst_dev);
208cbf031f4SNicholas Bellinger 		}
209cbf031f4SNicholas Bellinger 	}
210cbf031f4SNicholas Bellinger 
211cbf031f4SNicholas Bellinger 	return 0;
212cbf031f4SNicholas Bellinger }
213cbf031f4SNicholas Bellinger 
214cbf031f4SNicholas Bellinger static int target_xcopy_parse_target_descriptors(struct se_cmd *se_cmd,
215cbf031f4SNicholas Bellinger 				struct xcopy_op *xop, unsigned char *p,
216449a1378SNicholas Bellinger 				unsigned short tdll, sense_reason_t *sense_ret)
217cbf031f4SNicholas Bellinger {
218cbf031f4SNicholas Bellinger 	struct se_device *local_dev = se_cmd->se_dev;
219cbf031f4SNicholas Bellinger 	unsigned char *desc = p;
22066640d35SDavid Disseldorp 	int offset = tdll % XCOPY_TARGET_DESC_LEN, rc;
22166640d35SDavid Disseldorp 	unsigned short cscd_index = 0;
222cbf031f4SNicholas Bellinger 	unsigned short start = 0;
223cbf031f4SNicholas Bellinger 
224449a1378SNicholas Bellinger 	*sense_ret = TCM_INVALID_PARAMETER_LIST;
225449a1378SNicholas Bellinger 
226cbf031f4SNicholas Bellinger 	if (offset != 0) {
227cbf031f4SNicholas Bellinger 		pr_err("XCOPY target descriptor list length is not"
228cbf031f4SNicholas Bellinger 			" multiple of %d\n", XCOPY_TARGET_DESC_LEN);
229c2438497SDavid Disseldorp 		*sense_ret = TCM_UNSUPPORTED_TARGET_DESC_TYPE_CODE;
230cbf031f4SNicholas Bellinger 		return -EINVAL;
231cbf031f4SNicholas Bellinger 	}
23261c35919SDavid Disseldorp 	if (tdll > RCR_OP_MAX_TARGET_DESC_COUNT * XCOPY_TARGET_DESC_LEN) {
233cbf031f4SNicholas Bellinger 		pr_err("XCOPY target descriptor supports a maximum"
234cbf031f4SNicholas Bellinger 			" two src/dest descriptors, tdll: %hu too large..\n", tdll);
23561c35919SDavid Disseldorp 		/* spc4r37 6.4.3.4 CSCD DESCRIPTOR LIST LENGTH field */
23661c35919SDavid Disseldorp 		*sense_ret = TCM_TOO_MANY_TARGET_DESCS;
237cbf031f4SNicholas Bellinger 		return -EINVAL;
238cbf031f4SNicholas Bellinger 	}
239cbf031f4SNicholas Bellinger 	/*
240cbf031f4SNicholas Bellinger 	 * Generate an IEEE Registered Extended designator based upon the
241cbf031f4SNicholas Bellinger 	 * se_device the XCOPY was received upon..
242cbf031f4SNicholas Bellinger 	 */
243cbf031f4SNicholas Bellinger 	memset(&xop->local_dev_wwn[0], 0, XCOPY_NAA_IEEE_REGEX_LEN);
244cbf031f4SNicholas Bellinger 	target_xcopy_gen_naa_ieee(local_dev, &xop->local_dev_wwn[0]);
245cbf031f4SNicholas Bellinger 
246cbf031f4SNicholas Bellinger 	while (start < tdll) {
247cbf031f4SNicholas Bellinger 		/*
24866640d35SDavid Disseldorp 		 * Check target descriptor identification with 0xE4 type, and
24966640d35SDavid Disseldorp 		 * compare the current index with the CSCD descriptor IDs in
25066640d35SDavid Disseldorp 		 * the segment descriptor. Use VPD 0x83 WWPN matching ..
251cbf031f4SNicholas Bellinger 		 */
252cbf031f4SNicholas Bellinger 		switch (desc[0]) {
253cbf031f4SNicholas Bellinger 		case 0xe4:
254cbf031f4SNicholas Bellinger 			rc = target_xcopy_parse_tiddesc_e4(se_cmd, xop,
25566640d35SDavid Disseldorp 							&desc[0], cscd_index);
256cbf031f4SNicholas Bellinger 			if (rc != 0)
257cbf031f4SNicholas Bellinger 				goto out;
258cbf031f4SNicholas Bellinger 			start += XCOPY_TARGET_DESC_LEN;
259cbf031f4SNicholas Bellinger 			desc += XCOPY_TARGET_DESC_LEN;
26066640d35SDavid Disseldorp 			cscd_index++;
261cbf031f4SNicholas Bellinger 			break;
262cbf031f4SNicholas Bellinger 		default:
263cbf031f4SNicholas Bellinger 			pr_err("XCOPY unsupported descriptor type code:"
264cbf031f4SNicholas Bellinger 					" 0x%02x\n", desc[0]);
265c2438497SDavid Disseldorp 			*sense_ret = TCM_UNSUPPORTED_TARGET_DESC_TYPE_CODE;
266cbf031f4SNicholas Bellinger 			goto out;
267cbf031f4SNicholas Bellinger 		}
268cbf031f4SNicholas Bellinger 	}
269cbf031f4SNicholas Bellinger 
27066640d35SDavid Disseldorp 	switch (xop->op_origin) {
27166640d35SDavid Disseldorp 	case XCOL_SOURCE_RECV_OP:
2722896c938SDavid Disseldorp 		rc = target_xcopy_locate_se_dev_e4(se_cmd->se_sess,
2732896c938SDavid Disseldorp 						xop->dst_tid_wwn,
2742896c938SDavid Disseldorp 						&xop->dst_dev,
2752896c938SDavid Disseldorp 						&xop->remote_lun_ref);
27666640d35SDavid Disseldorp 		break;
27766640d35SDavid Disseldorp 	case XCOL_DEST_RECV_OP:
2782896c938SDavid Disseldorp 		rc = target_xcopy_locate_se_dev_e4(se_cmd->se_sess,
2792896c938SDavid Disseldorp 						xop->src_tid_wwn,
2802896c938SDavid Disseldorp 						&xop->src_dev,
2812896c938SDavid Disseldorp 						&xop->remote_lun_ref);
28266640d35SDavid Disseldorp 		break;
28366640d35SDavid Disseldorp 	default:
28466640d35SDavid Disseldorp 		pr_err("XCOPY CSCD descriptor IDs not found in CSCD list - "
28566640d35SDavid Disseldorp 			"stdi: %hu dtdi: %hu\n", xop->stdi, xop->dtdi);
28666640d35SDavid Disseldorp 		rc = -EINVAL;
28766640d35SDavid Disseldorp 		break;
28866640d35SDavid Disseldorp 	}
289449a1378SNicholas Bellinger 	/*
290449a1378SNicholas Bellinger 	 * If a matching IEEE NAA 0x83 descriptor for the requested device
291449a1378SNicholas Bellinger 	 * is not located on this node, return COPY_ABORTED with ASQ/ASQC
292449a1378SNicholas Bellinger 	 * 0x0d/0x02 - COPY_TARGET_DEVICE_NOT_REACHABLE to request the
293449a1378SNicholas Bellinger 	 * initiator to fall back to normal copy method.
294449a1378SNicholas Bellinger 	 */
295449a1378SNicholas Bellinger 	if (rc < 0) {
296449a1378SNicholas Bellinger 		*sense_ret = TCM_COPY_TARGET_DEVICE_NOT_REACHABLE;
297cbf031f4SNicholas Bellinger 		goto out;
298449a1378SNicholas Bellinger 	}
299cbf031f4SNicholas Bellinger 
300cbf031f4SNicholas Bellinger 	pr_debug("XCOPY TGT desc: Source dev: %p NAA IEEE WWN: 0x%16phN\n",
301cbf031f4SNicholas Bellinger 		 xop->src_dev, &xop->src_tid_wwn[0]);
302cbf031f4SNicholas Bellinger 	pr_debug("XCOPY TGT desc: Dest dev: %p NAA IEEE WWN: 0x%16phN\n",
303cbf031f4SNicholas Bellinger 		 xop->dst_dev, &xop->dst_tid_wwn[0]);
304cbf031f4SNicholas Bellinger 
30566640d35SDavid Disseldorp 	return cscd_index;
306cbf031f4SNicholas Bellinger 
307cbf031f4SNicholas Bellinger out:
308cbf031f4SNicholas Bellinger 	return -EINVAL;
309cbf031f4SNicholas Bellinger }
310cbf031f4SNicholas Bellinger 
311cbf031f4SNicholas Bellinger static int target_xcopy_parse_segdesc_02(struct se_cmd *se_cmd, struct xcopy_op *xop,
312cbf031f4SNicholas Bellinger 					unsigned char *p)
313cbf031f4SNicholas Bellinger {
314cbf031f4SNicholas Bellinger 	unsigned char *desc = p;
315cbf031f4SNicholas Bellinger 	int dc = (desc[1] & 0x02);
316cbf031f4SNicholas Bellinger 	unsigned short desc_len;
317cbf031f4SNicholas Bellinger 
318cbf031f4SNicholas Bellinger 	desc_len = get_unaligned_be16(&desc[2]);
319cbf031f4SNicholas Bellinger 	if (desc_len != 0x18) {
320cbf031f4SNicholas Bellinger 		pr_err("XCOPY segment desc 0x02: Illegal desc_len:"
321cbf031f4SNicholas Bellinger 				" %hu\n", desc_len);
322cbf031f4SNicholas Bellinger 		return -EINVAL;
323cbf031f4SNicholas Bellinger 	}
324cbf031f4SNicholas Bellinger 
325cbf031f4SNicholas Bellinger 	xop->stdi = get_unaligned_be16(&desc[4]);
326cbf031f4SNicholas Bellinger 	xop->dtdi = get_unaligned_be16(&desc[6]);
327f184210bSDavid Disseldorp 
328f184210bSDavid Disseldorp 	if (xop->stdi > XCOPY_CSCD_DESC_ID_LIST_OFF_MAX ||
329f184210bSDavid Disseldorp 	    xop->dtdi > XCOPY_CSCD_DESC_ID_LIST_OFF_MAX) {
330f184210bSDavid Disseldorp 		pr_err("XCOPY segment desc 0x02: unsupported CSCD ID > 0x%x; stdi: %hu dtdi: %hu\n",
331f184210bSDavid Disseldorp 			XCOPY_CSCD_DESC_ID_LIST_OFF_MAX, xop->stdi, xop->dtdi);
332f184210bSDavid Disseldorp 		return -EINVAL;
333f184210bSDavid Disseldorp 	}
334f184210bSDavid Disseldorp 
335cbf031f4SNicholas Bellinger 	pr_debug("XCOPY seg desc 0x02: desc_len: %hu stdi: %hu dtdi: %hu, DC: %d\n",
336cbf031f4SNicholas Bellinger 		desc_len, xop->stdi, xop->dtdi, dc);
337cbf031f4SNicholas Bellinger 
338cbf031f4SNicholas Bellinger 	xop->nolb = get_unaligned_be16(&desc[10]);
339cbf031f4SNicholas Bellinger 	xop->src_lba = get_unaligned_be64(&desc[12]);
340cbf031f4SNicholas Bellinger 	xop->dst_lba = get_unaligned_be64(&desc[20]);
341cbf031f4SNicholas Bellinger 	pr_debug("XCOPY seg desc 0x02: nolb: %hu src_lba: %llu dst_lba: %llu\n",
342cbf031f4SNicholas Bellinger 		xop->nolb, (unsigned long long)xop->src_lba,
343cbf031f4SNicholas Bellinger 		(unsigned long long)xop->dst_lba);
344cbf031f4SNicholas Bellinger 
345cbf031f4SNicholas Bellinger 	return 0;
346cbf031f4SNicholas Bellinger }
347cbf031f4SNicholas Bellinger 
348cbf031f4SNicholas Bellinger static int target_xcopy_parse_segment_descriptors(struct se_cmd *se_cmd,
349cbf031f4SNicholas Bellinger 				struct xcopy_op *xop, unsigned char *p,
350af9f62c1SDavid Disseldorp 				unsigned int sdll, sense_reason_t *sense_ret)
351cbf031f4SNicholas Bellinger {
352cbf031f4SNicholas Bellinger 	unsigned char *desc = p;
353cbf031f4SNicholas Bellinger 	unsigned int start = 0;
354cbf031f4SNicholas Bellinger 	int offset = sdll % XCOPY_SEGMENT_DESC_LEN, rc, ret = 0;
355cbf031f4SNicholas Bellinger 
356af9f62c1SDavid Disseldorp 	*sense_ret = TCM_INVALID_PARAMETER_LIST;
357af9f62c1SDavid Disseldorp 
358cbf031f4SNicholas Bellinger 	if (offset != 0) {
359cbf031f4SNicholas Bellinger 		pr_err("XCOPY segment descriptor list length is not"
360cbf031f4SNicholas Bellinger 			" multiple of %d\n", XCOPY_SEGMENT_DESC_LEN);
361c2438497SDavid Disseldorp 		*sense_ret = TCM_UNSUPPORTED_SEGMENT_DESC_TYPE_CODE;
362cbf031f4SNicholas Bellinger 		return -EINVAL;
363cbf031f4SNicholas Bellinger 	}
364af9f62c1SDavid Disseldorp 	if (sdll > RCR_OP_MAX_SG_DESC_COUNT * XCOPY_SEGMENT_DESC_LEN) {
365af9f62c1SDavid Disseldorp 		pr_err("XCOPY supports %u segment descriptor(s), sdll: %u too"
366af9f62c1SDavid Disseldorp 			" large..\n", RCR_OP_MAX_SG_DESC_COUNT, sdll);
367af9f62c1SDavid Disseldorp 		/* spc4r37 6.4.3.5 SEGMENT DESCRIPTOR LIST LENGTH field */
368af9f62c1SDavid Disseldorp 		*sense_ret = TCM_TOO_MANY_SEGMENT_DESCS;
369af9f62c1SDavid Disseldorp 		return -EINVAL;
370af9f62c1SDavid Disseldorp 	}
371cbf031f4SNicholas Bellinger 
372cbf031f4SNicholas Bellinger 	while (start < sdll) {
373cbf031f4SNicholas Bellinger 		/*
374cbf031f4SNicholas Bellinger 		 * Check segment descriptor type code for block -> block
375cbf031f4SNicholas Bellinger 		 */
376cbf031f4SNicholas Bellinger 		switch (desc[0]) {
377cbf031f4SNicholas Bellinger 		case 0x02:
378cbf031f4SNicholas Bellinger 			rc = target_xcopy_parse_segdesc_02(se_cmd, xop, desc);
379cbf031f4SNicholas Bellinger 			if (rc < 0)
380cbf031f4SNicholas Bellinger 				goto out;
381cbf031f4SNicholas Bellinger 
382cbf031f4SNicholas Bellinger 			ret++;
383cbf031f4SNicholas Bellinger 			start += XCOPY_SEGMENT_DESC_LEN;
384cbf031f4SNicholas Bellinger 			desc += XCOPY_SEGMENT_DESC_LEN;
385cbf031f4SNicholas Bellinger 			break;
386cbf031f4SNicholas Bellinger 		default:
3876774def6SMasanari Iida 			pr_err("XCOPY unsupported segment descriptor"
388cbf031f4SNicholas Bellinger 				"type: 0x%02x\n", desc[0]);
389c2438497SDavid Disseldorp 			*sense_ret = TCM_UNSUPPORTED_SEGMENT_DESC_TYPE_CODE;
390cbf031f4SNicholas Bellinger 			goto out;
391cbf031f4SNicholas Bellinger 		}
392cbf031f4SNicholas Bellinger 	}
393cbf031f4SNicholas Bellinger 
394cbf031f4SNicholas Bellinger 	return ret;
395cbf031f4SNicholas Bellinger 
396cbf031f4SNicholas Bellinger out:
397cbf031f4SNicholas Bellinger 	return -EINVAL;
398cbf031f4SNicholas Bellinger }
399cbf031f4SNicholas Bellinger 
400cbf031f4SNicholas Bellinger /*
401cbf031f4SNicholas Bellinger  * Start xcopy_pt ops
402cbf031f4SNicholas Bellinger  */
403cbf031f4SNicholas Bellinger 
404cbf031f4SNicholas Bellinger struct xcopy_pt_cmd {
405cbf031f4SNicholas Bellinger 	struct se_cmd se_cmd;
406cbf031f4SNicholas Bellinger 	struct completion xpt_passthrough_sem;
407366bda19SNicholas Bellinger 	unsigned char sense_buffer[TRANSPORT_SENSE_BUFFER];
408cbf031f4SNicholas Bellinger };
409cbf031f4SNicholas Bellinger 
410adf653f9SChristoph Hellwig struct se_portal_group xcopy_pt_tpg;
411cbf031f4SNicholas Bellinger static struct se_session xcopy_pt_sess;
412cbf031f4SNicholas Bellinger static struct se_node_acl xcopy_pt_nacl;
413cbf031f4SNicholas Bellinger 
414cbf031f4SNicholas Bellinger static int xcopy_pt_get_cmd_state(struct se_cmd *se_cmd)
415cbf031f4SNicholas Bellinger {
416cbf031f4SNicholas Bellinger         return 0;
417cbf031f4SNicholas Bellinger }
418cbf031f4SNicholas Bellinger 
419cbf031f4SNicholas Bellinger static void xcopy_pt_undepend_remotedev(struct xcopy_op *xop)
420cbf031f4SNicholas Bellinger {
421cbf031f4SNicholas Bellinger 	if (xop->op_origin == XCOL_SOURCE_RECV_OP)
4222896c938SDavid Disseldorp 		pr_debug("putting dst lun_ref for %p\n", xop->dst_dev);
423cbf031f4SNicholas Bellinger 	else
4242896c938SDavid Disseldorp 		pr_debug("putting src lun_ref for %p\n", xop->src_dev);
425cbf031f4SNicholas Bellinger 
4262896c938SDavid Disseldorp 	percpu_ref_put(xop->remote_lun_ref);
427cbf031f4SNicholas Bellinger }
428cbf031f4SNicholas Bellinger 
429cbf031f4SNicholas Bellinger static void xcopy_pt_release_cmd(struct se_cmd *se_cmd)
430cbf031f4SNicholas Bellinger {
431cbf031f4SNicholas Bellinger 	struct xcopy_pt_cmd *xpt_cmd = container_of(se_cmd,
432cbf031f4SNicholas Bellinger 				struct xcopy_pt_cmd, se_cmd);
433cbf031f4SNicholas Bellinger 
434b92fcfcbSDavid Disseldorp 	/* xpt_cmd is on the stack, nothing to free here */
435b92fcfcbSDavid Disseldorp 	pr_debug("xpt_cmd done: %p\n", xpt_cmd);
436cbf031f4SNicholas Bellinger }
437cbf031f4SNicholas Bellinger 
438cbf031f4SNicholas Bellinger static int xcopy_pt_check_stop_free(struct se_cmd *se_cmd)
439cbf031f4SNicholas Bellinger {
440cbf031f4SNicholas Bellinger 	struct xcopy_pt_cmd *xpt_cmd = container_of(se_cmd,
441cbf031f4SNicholas Bellinger 				struct xcopy_pt_cmd, se_cmd);
442cbf031f4SNicholas Bellinger 
443cbf031f4SNicholas Bellinger 	complete(&xpt_cmd->xpt_passthrough_sem);
444cbf031f4SNicholas Bellinger 	return 0;
445cbf031f4SNicholas Bellinger }
446cbf031f4SNicholas Bellinger 
447cbf031f4SNicholas Bellinger static int xcopy_pt_write_pending(struct se_cmd *se_cmd)
448cbf031f4SNicholas Bellinger {
449cbf031f4SNicholas Bellinger 	return 0;
450cbf031f4SNicholas Bellinger }
451cbf031f4SNicholas Bellinger 
452cbf031f4SNicholas Bellinger static int xcopy_pt_queue_data_in(struct se_cmd *se_cmd)
453cbf031f4SNicholas Bellinger {
454cbf031f4SNicholas Bellinger 	return 0;
455cbf031f4SNicholas Bellinger }
456cbf031f4SNicholas Bellinger 
457cbf031f4SNicholas Bellinger static int xcopy_pt_queue_status(struct se_cmd *se_cmd)
458cbf031f4SNicholas Bellinger {
459cbf031f4SNicholas Bellinger 	return 0;
460cbf031f4SNicholas Bellinger }
461cbf031f4SNicholas Bellinger 
4629ac8928eSChristoph Hellwig static const struct target_core_fabric_ops xcopy_pt_tfo = {
46330c7ca93SDavid Disseldorp 	.fabric_name		= "xcopy-pt",
464cbf031f4SNicholas Bellinger 	.get_cmd_state		= xcopy_pt_get_cmd_state,
465cbf031f4SNicholas Bellinger 	.release_cmd		= xcopy_pt_release_cmd,
466cbf031f4SNicholas Bellinger 	.check_stop_free	= xcopy_pt_check_stop_free,
467cbf031f4SNicholas Bellinger 	.write_pending		= xcopy_pt_write_pending,
468cbf031f4SNicholas Bellinger 	.queue_data_in		= xcopy_pt_queue_data_in,
469cbf031f4SNicholas Bellinger 	.queue_status		= xcopy_pt_queue_status,
470cbf031f4SNicholas Bellinger };
471cbf031f4SNicholas Bellinger 
472cbf031f4SNicholas Bellinger /*
473cbf031f4SNicholas Bellinger  * End xcopy_pt_ops
474cbf031f4SNicholas Bellinger  */
475cbf031f4SNicholas Bellinger 
476cbf031f4SNicholas Bellinger int target_xcopy_setup_pt(void)
477cbf031f4SNicholas Bellinger {
478ad669505SBart Van Assche 	int ret;
479ad669505SBart Van Assche 
480cbf031f4SNicholas Bellinger 	xcopy_wq = alloc_workqueue("xcopy_wq", WQ_MEM_RECLAIM, 0);
481cbf031f4SNicholas Bellinger 	if (!xcopy_wq) {
482cbf031f4SNicholas Bellinger 		pr_err("Unable to allocate xcopy_wq\n");
483cbf031f4SNicholas Bellinger 		return -ENOMEM;
484cbf031f4SNicholas Bellinger 	}
485cbf031f4SNicholas Bellinger 
486cbf031f4SNicholas Bellinger 	memset(&xcopy_pt_tpg, 0, sizeof(struct se_portal_group));
487cbf031f4SNicholas Bellinger 	INIT_LIST_HEAD(&xcopy_pt_tpg.acl_node_list);
488cbf031f4SNicholas Bellinger 	INIT_LIST_HEAD(&xcopy_pt_tpg.tpg_sess_list);
489cbf031f4SNicholas Bellinger 
490cbf031f4SNicholas Bellinger 	xcopy_pt_tpg.se_tpg_tfo = &xcopy_pt_tfo;
491cbf031f4SNicholas Bellinger 
492cbf031f4SNicholas Bellinger 	memset(&xcopy_pt_nacl, 0, sizeof(struct se_node_acl));
493cbf031f4SNicholas Bellinger 	INIT_LIST_HEAD(&xcopy_pt_nacl.acl_list);
494cbf031f4SNicholas Bellinger 	INIT_LIST_HEAD(&xcopy_pt_nacl.acl_sess_list);
495cbf031f4SNicholas Bellinger 	memset(&xcopy_pt_sess, 0, sizeof(struct se_session));
496ad669505SBart Van Assche 	ret = transport_init_session(&xcopy_pt_sess);
497ad669505SBart Van Assche 	if (ret < 0)
4983c006c7dSMike Christie 		goto destroy_wq;
499cbf031f4SNicholas Bellinger 
500cbf031f4SNicholas Bellinger 	xcopy_pt_nacl.se_tpg = &xcopy_pt_tpg;
501cbf031f4SNicholas Bellinger 	xcopy_pt_nacl.nacl_sess = &xcopy_pt_sess;
502cbf031f4SNicholas Bellinger 
503cbf031f4SNicholas Bellinger 	xcopy_pt_sess.se_tpg = &xcopy_pt_tpg;
504cbf031f4SNicholas Bellinger 	xcopy_pt_sess.se_node_acl = &xcopy_pt_nacl;
505cbf031f4SNicholas Bellinger 
506cbf031f4SNicholas Bellinger 	return 0;
5073c006c7dSMike Christie 
5083c006c7dSMike Christie destroy_wq:
5093c006c7dSMike Christie 	destroy_workqueue(xcopy_wq);
5103c006c7dSMike Christie 	xcopy_wq = NULL;
5113c006c7dSMike Christie 	return ret;
512cbf031f4SNicholas Bellinger }
513cbf031f4SNicholas Bellinger 
514cbf031f4SNicholas Bellinger void target_xcopy_release_pt(void)
515cbf031f4SNicholas Bellinger {
5163c006c7dSMike Christie 	if (xcopy_wq) {
517cbf031f4SNicholas Bellinger 		destroy_workqueue(xcopy_wq);
5183c006c7dSMike Christie 		transport_uninit_session(&xcopy_pt_sess);
5193c006c7dSMike Christie 	}
520cbf031f4SNicholas Bellinger }
521cbf031f4SNicholas Bellinger 
522be71530aSBart Van Assche /*
523be71530aSBart Van Assche  * target_xcopy_setup_pt_cmd - set up a pass-through command
524be71530aSBart Van Assche  * @xpt_cmd:	 Data structure to initialize.
525be71530aSBart Van Assche  * @xop:	 Describes the XCOPY operation received from an initiator.
526be71530aSBart Van Assche  * @se_dev:	 Backend device to associate with @xpt_cmd if
527be71530aSBart Van Assche  *		 @remote_port == true.
528be71530aSBart Van Assche  * @cdb:	 SCSI CDB to be copied into @xpt_cmd.
529be71530aSBart Van Assche  * @remote_port: If false, use the LUN through which the XCOPY command has
530be71530aSBart Van Assche  *		 been received. If true, use @se_dev->xcopy_lun.
531be71530aSBart Van Assche  *
532be71530aSBart Van Assche  * Set up a SCSI command (READ or WRITE) that will be used to execute an
533be71530aSBart Van Assche  * XCOPY command.
534be71530aSBart Van Assche  */
535cbf031f4SNicholas Bellinger static int target_xcopy_setup_pt_cmd(
536cbf031f4SNicholas Bellinger 	struct xcopy_pt_cmd *xpt_cmd,
537cbf031f4SNicholas Bellinger 	struct xcopy_op *xop,
538cbf031f4SNicholas Bellinger 	struct se_device *se_dev,
539cbf031f4SNicholas Bellinger 	unsigned char *cdb,
5400ad08996SDavid Disseldorp 	bool remote_port)
541cbf031f4SNicholas Bellinger {
542cbf031f4SNicholas Bellinger 	struct se_cmd *cmd = &xpt_cmd->se_cmd;
543be71530aSBart Van Assche 
544cbf031f4SNicholas Bellinger 	/*
545cbf031f4SNicholas Bellinger 	 * Setup LUN+port to honor reservations based upon xop->op_origin for
546cbf031f4SNicholas Bellinger 	 * X-COPY PUSH or X-COPY PULL based upon where the CDB was received.
547cbf031f4SNicholas Bellinger 	 */
548be71530aSBart Van Assche 	if (remote_port) {
549be71530aSBart Van Assche 		cmd->se_lun = &se_dev->xcopy_lun;
550be71530aSBart Van Assche 		cmd->se_dev = se_dev;
551be71530aSBart Van Assche 	} else {
552be71530aSBart Van Assche 		cmd->se_lun = xop->xop_se_cmd->se_lun;
553be71530aSBart Van Assche 		cmd->se_dev = xop->xop_se_cmd->se_dev;
554be71530aSBart Van Assche 	}
555be71530aSBart Van Assche 	cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
556cbf031f4SNicholas Bellinger 
557*08694199SMike Christie 	if (target_cmd_init_cdb(cmd, cdb, GFP_KERNEL))
5589e95fb80SSudhakar Panneerselvam 		return -EINVAL;
5599e95fb80SSudhakar Panneerselvam 
560649ee054SBart Van Assche 	cmd->tag = 0;
561987db587SSudhakar Panneerselvam 	if (target_cmd_parse_cdb(cmd))
5620ad08996SDavid Disseldorp 		return -EINVAL;
563cbf031f4SNicholas Bellinger 
5640ad08996SDavid Disseldorp 	if (transport_generic_map_mem_to_cmd(cmd, xop->xop_data_sg,
5650ad08996SDavid Disseldorp 					xop->xop_data_nents, NULL, 0))
5660ad08996SDavid Disseldorp 		return -EINVAL;
567cbf031f4SNicholas Bellinger 
568cbf031f4SNicholas Bellinger 	pr_debug("Setup PASSTHROUGH_NOALLOC t_data_sg: %p t_data_nents:"
569cbf031f4SNicholas Bellinger 		 " %u\n", cmd->t_data_sg, cmd->t_data_nents);
570cbf031f4SNicholas Bellinger 
571cbf031f4SNicholas Bellinger 	return 0;
572cbf031f4SNicholas Bellinger }
573cbf031f4SNicholas Bellinger 
574cbf031f4SNicholas Bellinger static int target_xcopy_issue_pt_cmd(struct xcopy_pt_cmd *xpt_cmd)
575cbf031f4SNicholas Bellinger {
576cbf031f4SNicholas Bellinger 	struct se_cmd *se_cmd = &xpt_cmd->se_cmd;
577cbf031f4SNicholas Bellinger 	sense_reason_t sense_rc;
578cbf031f4SNicholas Bellinger 
579cbf031f4SNicholas Bellinger 	sense_rc = transport_generic_new_cmd(se_cmd);
580cbf031f4SNicholas Bellinger 	if (sense_rc)
581cbf031f4SNicholas Bellinger 		return -EINVAL;
582cbf031f4SNicholas Bellinger 
583cbf031f4SNicholas Bellinger 	if (se_cmd->data_direction == DMA_TO_DEVICE)
584cbf031f4SNicholas Bellinger 		target_execute_cmd(se_cmd);
585cbf031f4SNicholas Bellinger 
586cbf031f4SNicholas Bellinger 	wait_for_completion_interruptible(&xpt_cmd->xpt_passthrough_sem);
587cbf031f4SNicholas Bellinger 
588cbf031f4SNicholas Bellinger 	pr_debug("target_xcopy_issue_pt_cmd(): SCSI status: 0x%02x\n",
589cbf031f4SNicholas Bellinger 			se_cmd->scsi_status);
5908a955d6dSNicholas Bellinger 
5918a955d6dSNicholas Bellinger 	return (se_cmd->scsi_status) ? -EINVAL : 0;
592cbf031f4SNicholas Bellinger }
593cbf031f4SNicholas Bellinger 
594cbf031f4SNicholas Bellinger static int target_xcopy_read_source(
595cbf031f4SNicholas Bellinger 	struct se_cmd *ec_cmd,
596cbf031f4SNicholas Bellinger 	struct xcopy_op *xop,
597cbf031f4SNicholas Bellinger 	struct se_device *src_dev,
598cbf031f4SNicholas Bellinger 	sector_t src_lba,
599cbf031f4SNicholas Bellinger 	u32 src_sectors)
600cbf031f4SNicholas Bellinger {
601b92fcfcbSDavid Disseldorp 	struct xcopy_pt_cmd xpt_cmd;
602b92fcfcbSDavid Disseldorp 	struct se_cmd *se_cmd = &xpt_cmd.se_cmd;
603cbf031f4SNicholas Bellinger 	u32 length = (src_sectors * src_dev->dev_attrib.block_size);
604cbf031f4SNicholas Bellinger 	int rc;
605cbf031f4SNicholas Bellinger 	unsigned char cdb[16];
606cbf031f4SNicholas Bellinger 	bool remote_port = (xop->op_origin == XCOL_DEST_RECV_OP);
607cbf031f4SNicholas Bellinger 
608b92fcfcbSDavid Disseldorp 	memset(&xpt_cmd, 0, sizeof(xpt_cmd));
609b92fcfcbSDavid Disseldorp 	init_completion(&xpt_cmd.xpt_passthrough_sem);
610cbf031f4SNicholas Bellinger 
611cbf031f4SNicholas Bellinger 	memset(&cdb[0], 0, 16);
612cbf031f4SNicholas Bellinger 	cdb[0] = READ_16;
613cbf031f4SNicholas Bellinger 	put_unaligned_be64(src_lba, &cdb[2]);
614cbf031f4SNicholas Bellinger 	put_unaligned_be32(src_sectors, &cdb[10]);
615cbf031f4SNicholas Bellinger 	pr_debug("XCOPY: Built READ_16: LBA: %llu Sectors: %u Length: %u\n",
616cbf031f4SNicholas Bellinger 		(unsigned long long)src_lba, src_sectors, length);
617cbf031f4SNicholas Bellinger 
618a78b7136SMike Christie 	__target_init_cmd(se_cmd, &xcopy_pt_tfo, &xcopy_pt_sess, length,
619a36840d8SSudhakar Panneerselvam 			  DMA_FROM_DEVICE, 0, &xpt_cmd.sense_buffer[0], 0);
620cbf031f4SNicholas Bellinger 
621b92fcfcbSDavid Disseldorp 	rc = target_xcopy_setup_pt_cmd(&xpt_cmd, xop, src_dev, &cdb[0],
6220ad08996SDavid Disseldorp 				remote_port);
623cbf031f4SNicholas Bellinger 	if (rc < 0) {
624b92fcfcbSDavid Disseldorp 		ec_cmd->scsi_status = se_cmd->scsi_status;
625b92fcfcbSDavid Disseldorp 		goto out;
626cbf031f4SNicholas Bellinger 	}
627cbf031f4SNicholas Bellinger 
628cbf031f4SNicholas Bellinger 	pr_debug("XCOPY-READ: Saved xop->xop_data_sg: %p, num: %u for READ"
629cbf031f4SNicholas Bellinger 		" memory\n", xop->xop_data_sg, xop->xop_data_nents);
630cbf031f4SNicholas Bellinger 
631b92fcfcbSDavid Disseldorp 	rc = target_xcopy_issue_pt_cmd(&xpt_cmd);
632b92fcfcbSDavid Disseldorp 	if (rc < 0)
633b92fcfcbSDavid Disseldorp 		ec_cmd->scsi_status = se_cmd->scsi_status;
634b92fcfcbSDavid Disseldorp out:
635cbf031f4SNicholas Bellinger 	transport_generic_free_cmd(se_cmd, 0);
636cbf031f4SNicholas Bellinger 	return rc;
637cbf031f4SNicholas Bellinger }
638cbf031f4SNicholas Bellinger 
639cbf031f4SNicholas Bellinger static int target_xcopy_write_destination(
640cbf031f4SNicholas Bellinger 	struct se_cmd *ec_cmd,
641cbf031f4SNicholas Bellinger 	struct xcopy_op *xop,
642cbf031f4SNicholas Bellinger 	struct se_device *dst_dev,
643cbf031f4SNicholas Bellinger 	sector_t dst_lba,
644cbf031f4SNicholas Bellinger 	u32 dst_sectors)
645cbf031f4SNicholas Bellinger {
646b92fcfcbSDavid Disseldorp 	struct xcopy_pt_cmd xpt_cmd;
647b92fcfcbSDavid Disseldorp 	struct se_cmd *se_cmd = &xpt_cmd.se_cmd;
648cbf031f4SNicholas Bellinger 	u32 length = (dst_sectors * dst_dev->dev_attrib.block_size);
649cbf031f4SNicholas Bellinger 	int rc;
650cbf031f4SNicholas Bellinger 	unsigned char cdb[16];
651cbf031f4SNicholas Bellinger 	bool remote_port = (xop->op_origin == XCOL_SOURCE_RECV_OP);
652cbf031f4SNicholas Bellinger 
653b92fcfcbSDavid Disseldorp 	memset(&xpt_cmd, 0, sizeof(xpt_cmd));
654b92fcfcbSDavid Disseldorp 	init_completion(&xpt_cmd.xpt_passthrough_sem);
655cbf031f4SNicholas Bellinger 
656cbf031f4SNicholas Bellinger 	memset(&cdb[0], 0, 16);
657cbf031f4SNicholas Bellinger 	cdb[0] = WRITE_16;
658cbf031f4SNicholas Bellinger 	put_unaligned_be64(dst_lba, &cdb[2]);
659cbf031f4SNicholas Bellinger 	put_unaligned_be32(dst_sectors, &cdb[10]);
660cbf031f4SNicholas Bellinger 	pr_debug("XCOPY: Built WRITE_16: LBA: %llu Sectors: %u Length: %u\n",
661cbf031f4SNicholas Bellinger 		(unsigned long long)dst_lba, dst_sectors, length);
662cbf031f4SNicholas Bellinger 
663a78b7136SMike Christie 	__target_init_cmd(se_cmd, &xcopy_pt_tfo, &xcopy_pt_sess, length,
664a36840d8SSudhakar Panneerselvam 			  DMA_TO_DEVICE, 0, &xpt_cmd.sense_buffer[0], 0);
665cbf031f4SNicholas Bellinger 
666b92fcfcbSDavid Disseldorp 	rc = target_xcopy_setup_pt_cmd(&xpt_cmd, xop, dst_dev, &cdb[0],
6670ad08996SDavid Disseldorp 				remote_port);
668cbf031f4SNicholas Bellinger 	if (rc < 0) {
669b92fcfcbSDavid Disseldorp 		ec_cmd->scsi_status = se_cmd->scsi_status;
670b92fcfcbSDavid Disseldorp 		goto out;
671cbf031f4SNicholas Bellinger 	}
672cbf031f4SNicholas Bellinger 
673b92fcfcbSDavid Disseldorp 	rc = target_xcopy_issue_pt_cmd(&xpt_cmd);
674b92fcfcbSDavid Disseldorp 	if (rc < 0)
675b92fcfcbSDavid Disseldorp 		ec_cmd->scsi_status = se_cmd->scsi_status;
676b92fcfcbSDavid Disseldorp out:
677cbf031f4SNicholas Bellinger 	transport_generic_free_cmd(se_cmd, 0);
678cbf031f4SNicholas Bellinger 	return rc;
679cbf031f4SNicholas Bellinger }
680cbf031f4SNicholas Bellinger 
681cbf031f4SNicholas Bellinger static void target_xcopy_do_work(struct work_struct *work)
682cbf031f4SNicholas Bellinger {
683cbf031f4SNicholas Bellinger 	struct xcopy_op *xop = container_of(work, struct xcopy_op, xop_work);
684cbf031f4SNicholas Bellinger 	struct se_cmd *ec_cmd = xop->xop_se_cmd;
685d877d727SBart Van Assche 	struct se_device *src_dev, *dst_dev;
686d877d727SBart Van Assche 	sector_t src_lba, dst_lba, end_lba;
687cbf031f4SNicholas Bellinger 	unsigned int max_sectors;
688d877d727SBart Van Assche 	int rc = 0;
6890ad08996SDavid Disseldorp 	unsigned short nolb, max_nolb, copied_nolb = 0;
690cbf031f4SNicholas Bellinger 
691d877d727SBart Van Assche 	if (target_parse_xcopy_cmd(xop) != TCM_NO_SENSE)
692d877d727SBart Van Assche 		goto err_free;
693d877d727SBart Van Assche 
694d877d727SBart Van Assche 	if (WARN_ON_ONCE(!xop->src_dev) || WARN_ON_ONCE(!xop->dst_dev))
695d877d727SBart Van Assche 		goto err_free;
696d877d727SBart Van Assche 
697d877d727SBart Van Assche 	src_dev = xop->src_dev;
698d877d727SBart Van Assche 	dst_dev = xop->dst_dev;
699d877d727SBart Van Assche 	src_lba = xop->src_lba;
700d877d727SBart Van Assche 	dst_lba = xop->dst_lba;
701d877d727SBart Van Assche 	nolb = xop->nolb;
702cbf031f4SNicholas Bellinger 	end_lba = src_lba + nolb;
703cbf031f4SNicholas Bellinger 	/*
704cbf031f4SNicholas Bellinger 	 * Break up XCOPY I/O into hw_max_sectors sized I/O based on the
705cbf031f4SNicholas Bellinger 	 * smallest max_sectors between src_dev + dev_dev, or
706cbf031f4SNicholas Bellinger 	 */
707cbf031f4SNicholas Bellinger 	max_sectors = min(src_dev->dev_attrib.hw_max_sectors,
708cbf031f4SNicholas Bellinger 			  dst_dev->dev_attrib.hw_max_sectors);
709cbf031f4SNicholas Bellinger 	max_sectors = min_t(u32, max_sectors, XCOPY_MAX_SECTORS);
710cbf031f4SNicholas Bellinger 
711cbf031f4SNicholas Bellinger 	max_nolb = min_t(u16, max_sectors, ((u16)(~0U)));
712cbf031f4SNicholas Bellinger 
713cbf031f4SNicholas Bellinger 	pr_debug("target_xcopy_do_work: nolb: %hu, max_nolb: %hu end_lba: %llu\n",
714cbf031f4SNicholas Bellinger 			nolb, max_nolb, (unsigned long long)end_lba);
715cbf031f4SNicholas Bellinger 	pr_debug("target_xcopy_do_work: Starting src_lba: %llu, dst_lba: %llu\n",
716cbf031f4SNicholas Bellinger 			(unsigned long long)src_lba, (unsigned long long)dst_lba);
717cbf031f4SNicholas Bellinger 
718cbf031f4SNicholas Bellinger 	while (src_lba < end_lba) {
7190ad08996SDavid Disseldorp 		unsigned short cur_nolb = min(nolb, max_nolb);
7200ad08996SDavid Disseldorp 		u32 cur_bytes = cur_nolb * src_dev->dev_attrib.block_size;
7210ad08996SDavid Disseldorp 
7220ad08996SDavid Disseldorp 		if (cur_bytes != xop->xop_data_bytes) {
7230ad08996SDavid Disseldorp 			/*
7240ad08996SDavid Disseldorp 			 * (Re)allocate a buffer large enough to hold the XCOPY
7250ad08996SDavid Disseldorp 			 * I/O size, which can be reused each read / write loop.
7260ad08996SDavid Disseldorp 			 */
7270ad08996SDavid Disseldorp 			target_free_sgl(xop->xop_data_sg, xop->xop_data_nents);
7280ad08996SDavid Disseldorp 			rc = target_alloc_sgl(&xop->xop_data_sg,
7290ad08996SDavid Disseldorp 					      &xop->xop_data_nents,
7300ad08996SDavid Disseldorp 					      cur_bytes,
7310ad08996SDavid Disseldorp 					      false, false);
7320ad08996SDavid Disseldorp 			if (rc < 0)
7330ad08996SDavid Disseldorp 				goto out;
7340ad08996SDavid Disseldorp 			xop->xop_data_bytes = cur_bytes;
7350ad08996SDavid Disseldorp 		}
736cbf031f4SNicholas Bellinger 
737cbf031f4SNicholas Bellinger 		pr_debug("target_xcopy_do_work: Calling read src_dev: %p src_lba: %llu,"
738cbf031f4SNicholas Bellinger 			" cur_nolb: %hu\n", src_dev, (unsigned long long)src_lba, cur_nolb);
739cbf031f4SNicholas Bellinger 
740cbf031f4SNicholas Bellinger 		rc = target_xcopy_read_source(ec_cmd, xop, src_dev, src_lba, cur_nolb);
741cbf031f4SNicholas Bellinger 		if (rc < 0)
742cbf031f4SNicholas Bellinger 			goto out;
743cbf031f4SNicholas Bellinger 
744cbf031f4SNicholas Bellinger 		src_lba += cur_nolb;
745cbf031f4SNicholas Bellinger 		pr_debug("target_xcopy_do_work: Incremented READ src_lba to %llu\n",
746cbf031f4SNicholas Bellinger 				(unsigned long long)src_lba);
747cbf031f4SNicholas Bellinger 
748cbf031f4SNicholas Bellinger 		pr_debug("target_xcopy_do_work: Calling write dst_dev: %p dst_lba: %llu,"
749cbf031f4SNicholas Bellinger 			" cur_nolb: %hu\n", dst_dev, (unsigned long long)dst_lba, cur_nolb);
750cbf031f4SNicholas Bellinger 
751cbf031f4SNicholas Bellinger 		rc = target_xcopy_write_destination(ec_cmd, xop, dst_dev,
752cbf031f4SNicholas Bellinger 						dst_lba, cur_nolb);
753b92fcfcbSDavid Disseldorp 		if (rc < 0)
754cbf031f4SNicholas Bellinger 			goto out;
755cbf031f4SNicholas Bellinger 
756cbf031f4SNicholas Bellinger 		dst_lba += cur_nolb;
757cbf031f4SNicholas Bellinger 		pr_debug("target_xcopy_do_work: Incremented WRITE dst_lba to %llu\n",
758cbf031f4SNicholas Bellinger 				(unsigned long long)dst_lba);
759cbf031f4SNicholas Bellinger 
760cbf031f4SNicholas Bellinger 		copied_nolb += cur_nolb;
761cbf031f4SNicholas Bellinger 		nolb -= cur_nolb;
762cbf031f4SNicholas Bellinger 	}
763cbf031f4SNicholas Bellinger 
764cbf031f4SNicholas Bellinger 	xcopy_pt_undepend_remotedev(xop);
7650ad08996SDavid Disseldorp 	target_free_sgl(xop->xop_data_sg, xop->xop_data_nents);
766cbf031f4SNicholas Bellinger 	kfree(xop);
767cbf031f4SNicholas Bellinger 
768cbf031f4SNicholas Bellinger 	pr_debug("target_xcopy_do_work: Final src_lba: %llu, dst_lba: %llu\n",
769cbf031f4SNicholas Bellinger 		(unsigned long long)src_lba, (unsigned long long)dst_lba);
770cbf031f4SNicholas Bellinger 	pr_debug("target_xcopy_do_work: Blocks copied: %hu, Bytes Copied: %u\n",
771cbf031f4SNicholas Bellinger 		copied_nolb, copied_nolb * dst_dev->dev_attrib.block_size);
772cbf031f4SNicholas Bellinger 
773cbf031f4SNicholas Bellinger 	pr_debug("target_xcopy_do_work: Setting X-COPY GOOD status -> sending response\n");
774cbf031f4SNicholas Bellinger 	target_complete_cmd(ec_cmd, SAM_STAT_GOOD);
775cbf031f4SNicholas Bellinger 	return;
776cbf031f4SNicholas Bellinger 
777cbf031f4SNicholas Bellinger out:
778cbf031f4SNicholas Bellinger 	xcopy_pt_undepend_remotedev(xop);
7790ad08996SDavid Disseldorp 	target_free_sgl(xop->xop_data_sg, xop->xop_data_nents);
780d877d727SBart Van Assche 
781d877d727SBart Van Assche err_free:
782cbf031f4SNicholas Bellinger 	kfree(xop);
783926317deSDinesh Israni 	/*
784926317deSDinesh Israni 	 * Don't override an error scsi status if it has already been set
785926317deSDinesh Israni 	 */
786926317deSDinesh Israni 	if (ec_cmd->scsi_status == SAM_STAT_GOOD) {
787926317deSDinesh Israni 		pr_warn_ratelimited("target_xcopy_do_work: rc: %d, Setting X-COPY"
788926317deSDinesh Israni 			" CHECK_CONDITION -> sending response\n", rc);
789cbf031f4SNicholas Bellinger 		ec_cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
790926317deSDinesh Israni 	}
7910583c261SNicholas Bellinger 	target_complete_cmd(ec_cmd, ec_cmd->scsi_status);
792cbf031f4SNicholas Bellinger }
793cbf031f4SNicholas Bellinger 
794d877d727SBart Van Assche /*
795d877d727SBart Van Assche  * Returns TCM_NO_SENSE upon success or a sense code != TCM_NO_SENSE if parsing
796d877d727SBart Van Assche  * fails.
797d877d727SBart Van Assche  */
798d877d727SBart Van Assche static sense_reason_t target_parse_xcopy_cmd(struct xcopy_op *xop)
799cbf031f4SNicholas Bellinger {
800d877d727SBart Van Assche 	struct se_cmd *se_cmd = xop->xop_se_cmd;
801cbf031f4SNicholas Bellinger 	unsigned char *p = NULL, *seg_desc;
802d877d727SBart Van Assche 	unsigned int list_id, list_id_usage, sdll, inline_dl;
80348502ddbSNicholas Bellinger 	sense_reason_t ret = TCM_INVALID_PARAMETER_LIST;
804cbf031f4SNicholas Bellinger 	int rc;
805cbf031f4SNicholas Bellinger 	unsigned short tdll;
806cbf031f4SNicholas Bellinger 
807cbf031f4SNicholas Bellinger 	p = transport_kmap_data_sg(se_cmd);
808cbf031f4SNicholas Bellinger 	if (!p) {
809cbf031f4SNicholas Bellinger 		pr_err("transport_kmap_data_sg() failed in target_do_xcopy\n");
810cbf031f4SNicholas Bellinger 		return TCM_OUT_OF_RESOURCES;
811cbf031f4SNicholas Bellinger 	}
812cbf031f4SNicholas Bellinger 
813cbf031f4SNicholas Bellinger 	list_id = p[0];
8143f7a46c6SNicholas Bellinger 	list_id_usage = (p[1] & 0x18) >> 3;
8153f7a46c6SNicholas Bellinger 
816cbf031f4SNicholas Bellinger 	/*
817cbf031f4SNicholas Bellinger 	 * Determine TARGET DESCRIPTOR LIST LENGTH + SEGMENT DESCRIPTOR LIST LENGTH
818cbf031f4SNicholas Bellinger 	 */
819cbf031f4SNicholas Bellinger 	tdll = get_unaligned_be16(&p[2]);
820cbf031f4SNicholas Bellinger 	sdll = get_unaligned_be32(&p[8]);
8217d387066SDavid Disseldorp 	if (tdll + sdll > RCR_OP_MAX_DESC_LIST_LEN) {
8227d387066SDavid Disseldorp 		pr_err("XCOPY descriptor list length %u exceeds maximum %u\n",
8237d387066SDavid Disseldorp 		       tdll + sdll, RCR_OP_MAX_DESC_LIST_LEN);
8247d387066SDavid Disseldorp 		ret = TCM_PARAMETER_LIST_LENGTH_ERROR;
8257d387066SDavid Disseldorp 		goto out;
8267d387066SDavid Disseldorp 	}
827cbf031f4SNicholas Bellinger 
828cbf031f4SNicholas Bellinger 	inline_dl = get_unaligned_be32(&p[12]);
829cbf031f4SNicholas Bellinger 	if (inline_dl != 0) {
830cbf031f4SNicholas Bellinger 		pr_err("XCOPY with non zero inline data length\n");
831cbf031f4SNicholas Bellinger 		goto out;
832cbf031f4SNicholas Bellinger 	}
833cbf031f4SNicholas Bellinger 
834f94fd098SDavid Disseldorp 	if (se_cmd->data_length < (XCOPY_HDR_LEN + tdll + sdll + inline_dl)) {
835f94fd098SDavid Disseldorp 		pr_err("XCOPY parameter truncation: data length %u too small "
836f94fd098SDavid Disseldorp 			"for tdll: %hu sdll: %u inline_dl: %u\n",
837f94fd098SDavid Disseldorp 			se_cmd->data_length, tdll, sdll, inline_dl);
838f94fd098SDavid Disseldorp 		ret = TCM_PARAMETER_LIST_LENGTH_ERROR;
839f94fd098SDavid Disseldorp 		goto out;
840f94fd098SDavid Disseldorp 	}
841f94fd098SDavid Disseldorp 
842cbf031f4SNicholas Bellinger 	pr_debug("Processing XCOPY with list_id: 0x%02x list_id_usage: 0x%02x"
843cbf031f4SNicholas Bellinger 		" tdll: %hu sdll: %u inline_dl: %u\n", list_id, list_id_usage,
844cbf031f4SNicholas Bellinger 		tdll, sdll, inline_dl);
845cbf031f4SNicholas Bellinger 
84666640d35SDavid Disseldorp 	/*
84766640d35SDavid Disseldorp 	 * skip over the target descriptors until segment descriptors
84866640d35SDavid Disseldorp 	 * have been passed - CSCD ids are needed to determine src and dest.
84966640d35SDavid Disseldorp 	 */
85066640d35SDavid Disseldorp 	seg_desc = &p[16] + tdll;
85166640d35SDavid Disseldorp 
85266640d35SDavid Disseldorp 	rc = target_xcopy_parse_segment_descriptors(se_cmd, xop, seg_desc,
85366640d35SDavid Disseldorp 						    sdll, &ret);
85466640d35SDavid Disseldorp 	if (rc <= 0)
85566640d35SDavid Disseldorp 		goto out;
85666640d35SDavid Disseldorp 
85766640d35SDavid Disseldorp 	pr_debug("XCOPY: Processed %d segment descriptors, length: %u\n", rc,
85866640d35SDavid Disseldorp 				rc * XCOPY_SEGMENT_DESC_LEN);
85966640d35SDavid Disseldorp 
860449a1378SNicholas Bellinger 	rc = target_xcopy_parse_target_descriptors(se_cmd, xop, &p[16], tdll, &ret);
861cbf031f4SNicholas Bellinger 	if (rc <= 0)
862cbf031f4SNicholas Bellinger 		goto out;
863cbf031f4SNicholas Bellinger 
86448502ddbSNicholas Bellinger 	if (xop->src_dev->dev_attrib.block_size !=
86548502ddbSNicholas Bellinger 	    xop->dst_dev->dev_attrib.block_size) {
86648502ddbSNicholas Bellinger 		pr_err("XCOPY: Non matching src_dev block_size: %u + dst_dev"
86748502ddbSNicholas Bellinger 		       " block_size: %u currently unsupported\n",
86848502ddbSNicholas Bellinger 			xop->src_dev->dev_attrib.block_size,
86948502ddbSNicholas Bellinger 			xop->dst_dev->dev_attrib.block_size);
87048502ddbSNicholas Bellinger 		xcopy_pt_undepend_remotedev(xop);
87148502ddbSNicholas Bellinger 		ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
87248502ddbSNicholas Bellinger 		goto out;
87348502ddbSNicholas Bellinger 	}
87448502ddbSNicholas Bellinger 
875cbf031f4SNicholas Bellinger 	pr_debug("XCOPY: Processed %d target descriptors, length: %u\n", rc,
876cbf031f4SNicholas Bellinger 				rc * XCOPY_TARGET_DESC_LEN);
877cbf031f4SNicholas Bellinger 	transport_kunmap_data_sg(se_cmd);
878cbf031f4SNicholas Bellinger 	return TCM_NO_SENSE;
879cbf031f4SNicholas Bellinger 
880cbf031f4SNicholas Bellinger out:
881cbf031f4SNicholas Bellinger 	if (p)
882cbf031f4SNicholas Bellinger 		transport_kunmap_data_sg(se_cmd);
88348502ddbSNicholas Bellinger 	return ret;
884cbf031f4SNicholas Bellinger }
885cbf031f4SNicholas Bellinger 
886d877d727SBart Van Assche sense_reason_t target_do_xcopy(struct se_cmd *se_cmd)
887d877d727SBart Van Assche {
888d877d727SBart Van Assche 	struct se_device *dev = se_cmd->se_dev;
889d877d727SBart Van Assche 	struct xcopy_op *xop;
890d877d727SBart Van Assche 	unsigned int sa;
891d877d727SBart Van Assche 
892d877d727SBart Van Assche 	if (!dev->dev_attrib.emulate_3pc) {
893d877d727SBart Van Assche 		pr_err("EXTENDED_COPY operation explicitly disabled\n");
894d877d727SBart Van Assche 		return TCM_UNSUPPORTED_SCSI_OPCODE;
895d877d727SBart Van Assche 	}
896d877d727SBart Van Assche 
897d877d727SBart Van Assche 	sa = se_cmd->t_task_cdb[1] & 0x1f;
898d877d727SBart Van Assche 	if (sa != 0x00) {
899d877d727SBart Van Assche 		pr_err("EXTENDED_COPY(LID4) not supported\n");
900d877d727SBart Van Assche 		return TCM_UNSUPPORTED_SCSI_OPCODE;
901d877d727SBart Van Assche 	}
902d877d727SBart Van Assche 
903d877d727SBart Van Assche 	if (se_cmd->data_length == 0) {
904d877d727SBart Van Assche 		target_complete_cmd(se_cmd, SAM_STAT_GOOD);
905d877d727SBart Van Assche 		return TCM_NO_SENSE;
906d877d727SBart Van Assche 	}
907d877d727SBart Van Assche 	if (se_cmd->data_length < XCOPY_HDR_LEN) {
908d877d727SBart Van Assche 		pr_err("XCOPY parameter truncation: length %u < hdr_len %u\n",
909d877d727SBart Van Assche 				se_cmd->data_length, XCOPY_HDR_LEN);
910d877d727SBart Van Assche 		return TCM_PARAMETER_LIST_LENGTH_ERROR;
911d877d727SBart Van Assche 	}
912d877d727SBart Van Assche 
913d877d727SBart Van Assche 	xop = kzalloc(sizeof(struct xcopy_op), GFP_KERNEL);
914d877d727SBart Van Assche 	if (!xop)
915d877d727SBart Van Assche 		goto err;
916d877d727SBart Van Assche 	xop->xop_se_cmd = se_cmd;
917d877d727SBart Van Assche 	INIT_WORK(&xop->xop_work, target_xcopy_do_work);
918d877d727SBart Van Assche 	if (WARN_ON_ONCE(!queue_work(xcopy_wq, &xop->xop_work)))
919d877d727SBart Van Assche 		goto free;
920d877d727SBart Van Assche 	return TCM_NO_SENSE;
921d877d727SBart Van Assche 
922d877d727SBart Van Assche free:
923d877d727SBart Van Assche 	kfree(xop);
924d877d727SBart Van Assche 
925d877d727SBart Van Assche err:
926d877d727SBart Van Assche 	return TCM_OUT_OF_RESOURCES;
927d877d727SBart Van Assche }
928d877d727SBart Van Assche 
929cbf031f4SNicholas Bellinger static sense_reason_t target_rcr_operating_parameters(struct se_cmd *se_cmd)
930cbf031f4SNicholas Bellinger {
931cbf031f4SNicholas Bellinger 	unsigned char *p;
932cbf031f4SNicholas Bellinger 
933cbf031f4SNicholas Bellinger 	p = transport_kmap_data_sg(se_cmd);
934cbf031f4SNicholas Bellinger 	if (!p) {
935cbf031f4SNicholas Bellinger 		pr_err("transport_kmap_data_sg failed in"
936cbf031f4SNicholas Bellinger 		       " target_rcr_operating_parameters\n");
937cbf031f4SNicholas Bellinger 		return TCM_OUT_OF_RESOURCES;
938cbf031f4SNicholas Bellinger 	}
939cbf031f4SNicholas Bellinger 
940cbf031f4SNicholas Bellinger 	if (se_cmd->data_length < 54) {
941cbf031f4SNicholas Bellinger 		pr_err("Receive Copy Results Op Parameters length"
942cbf031f4SNicholas Bellinger 		       " too small: %u\n", se_cmd->data_length);
943cbf031f4SNicholas Bellinger 		transport_kunmap_data_sg(se_cmd);
944cbf031f4SNicholas Bellinger 		return TCM_INVALID_CDB_FIELD;
945cbf031f4SNicholas Bellinger 	}
946cbf031f4SNicholas Bellinger 	/*
947cbf031f4SNicholas Bellinger 	 * Set SNLID=1 (Supports no List ID)
948cbf031f4SNicholas Bellinger 	 */
949cbf031f4SNicholas Bellinger 	p[4] = 0x1;
950cbf031f4SNicholas Bellinger 	/*
951cbf031f4SNicholas Bellinger 	 * MAXIMUM TARGET DESCRIPTOR COUNT
952cbf031f4SNicholas Bellinger 	 */
953cbf031f4SNicholas Bellinger 	put_unaligned_be16(RCR_OP_MAX_TARGET_DESC_COUNT, &p[8]);
954cbf031f4SNicholas Bellinger 	/*
955cbf031f4SNicholas Bellinger 	 * MAXIMUM SEGMENT DESCRIPTOR COUNT
956cbf031f4SNicholas Bellinger 	 */
957cbf031f4SNicholas Bellinger 	put_unaligned_be16(RCR_OP_MAX_SG_DESC_COUNT, &p[10]);
958cbf031f4SNicholas Bellinger 	/*
959cbf031f4SNicholas Bellinger 	 * MAXIMUM DESCRIPTOR LIST LENGTH
960cbf031f4SNicholas Bellinger 	 */
961cbf031f4SNicholas Bellinger 	put_unaligned_be32(RCR_OP_MAX_DESC_LIST_LEN, &p[12]);
962cbf031f4SNicholas Bellinger 	/*
963cbf031f4SNicholas Bellinger 	 * MAXIMUM SEGMENT LENGTH
964cbf031f4SNicholas Bellinger 	 */
965cbf031f4SNicholas Bellinger 	put_unaligned_be32(RCR_OP_MAX_SEGMENT_LEN, &p[16]);
966cbf031f4SNicholas Bellinger 	/*
967cbf031f4SNicholas Bellinger 	 * MAXIMUM INLINE DATA LENGTH for SA 0x04 (NOT SUPPORTED)
968cbf031f4SNicholas Bellinger 	 */
969cbf031f4SNicholas Bellinger 	put_unaligned_be32(0x0, &p[20]);
970cbf031f4SNicholas Bellinger 	/*
971cbf031f4SNicholas Bellinger 	 * HELD DATA LIMIT
972cbf031f4SNicholas Bellinger 	 */
973cbf031f4SNicholas Bellinger 	put_unaligned_be32(0x0, &p[24]);
974cbf031f4SNicholas Bellinger 	/*
975cbf031f4SNicholas Bellinger 	 * MAXIMUM STREAM DEVICE TRANSFER SIZE
976cbf031f4SNicholas Bellinger 	 */
977cbf031f4SNicholas Bellinger 	put_unaligned_be32(0x0, &p[28]);
978cbf031f4SNicholas Bellinger 	/*
979cbf031f4SNicholas Bellinger 	 * TOTAL CONCURRENT COPIES
980cbf031f4SNicholas Bellinger 	 */
981cbf031f4SNicholas Bellinger 	put_unaligned_be16(RCR_OP_TOTAL_CONCURR_COPIES, &p[34]);
982cbf031f4SNicholas Bellinger 	/*
983cbf031f4SNicholas Bellinger 	 * MAXIMUM CONCURRENT COPIES
984cbf031f4SNicholas Bellinger 	 */
985cbf031f4SNicholas Bellinger 	p[36] = RCR_OP_MAX_CONCURR_COPIES;
986cbf031f4SNicholas Bellinger 	/*
987cbf031f4SNicholas Bellinger 	 * DATA SEGMENT GRANULARITY (log 2)
988cbf031f4SNicholas Bellinger 	 */
989cbf031f4SNicholas Bellinger 	p[37] = RCR_OP_DATA_SEG_GRAN_LOG2;
990cbf031f4SNicholas Bellinger 	/*
991cbf031f4SNicholas Bellinger 	 * INLINE DATA GRANULARITY log 2)
992cbf031f4SNicholas Bellinger 	 */
993cbf031f4SNicholas Bellinger 	p[38] = RCR_OP_INLINE_DATA_GRAN_LOG2;
994cbf031f4SNicholas Bellinger 	/*
995cbf031f4SNicholas Bellinger 	 * HELD DATA GRANULARITY
996cbf031f4SNicholas Bellinger 	 */
997cbf031f4SNicholas Bellinger 	p[39] = RCR_OP_HELD_DATA_GRAN_LOG2;
998cbf031f4SNicholas Bellinger 	/*
999cbf031f4SNicholas Bellinger 	 * IMPLEMENTED DESCRIPTOR LIST LENGTH
1000cbf031f4SNicholas Bellinger 	 */
1001cbf031f4SNicholas Bellinger 	p[43] = 0x2;
1002cbf031f4SNicholas Bellinger 	/*
1003cbf031f4SNicholas Bellinger 	 * List of implemented descriptor type codes (ordered)
1004cbf031f4SNicholas Bellinger 	 */
1005cbf031f4SNicholas Bellinger 	p[44] = 0x02; /* Copy Block to Block device */
1006cbf031f4SNicholas Bellinger 	p[45] = 0xe4; /* Identification descriptor target descriptor */
1007cbf031f4SNicholas Bellinger 
1008cbf031f4SNicholas Bellinger 	/*
1009cbf031f4SNicholas Bellinger 	 * AVAILABLE DATA (n-3)
1010cbf031f4SNicholas Bellinger 	 */
1011cbf031f4SNicholas Bellinger 	put_unaligned_be32(42, &p[0]);
1012cbf031f4SNicholas Bellinger 
1013cbf031f4SNicholas Bellinger 	transport_kunmap_data_sg(se_cmd);
1014cbf031f4SNicholas Bellinger 	target_complete_cmd(se_cmd, GOOD);
1015cbf031f4SNicholas Bellinger 
1016cbf031f4SNicholas Bellinger 	return TCM_NO_SENSE;
1017cbf031f4SNicholas Bellinger }
1018cbf031f4SNicholas Bellinger 
1019cbf031f4SNicholas Bellinger sense_reason_t target_do_receive_copy_results(struct se_cmd *se_cmd)
1020cbf031f4SNicholas Bellinger {
1021cbf031f4SNicholas Bellinger 	unsigned char *cdb = &se_cmd->t_task_cdb[0];
1022cbf031f4SNicholas Bellinger 	int sa = (cdb[1] & 0x1f), list_id = cdb[2];
1023cbf031f4SNicholas Bellinger 	sense_reason_t rc = TCM_NO_SENSE;
1024cbf031f4SNicholas Bellinger 
1025cbf031f4SNicholas Bellinger 	pr_debug("Entering target_do_receive_copy_results: SA: 0x%02x, List ID:"
1026cbf031f4SNicholas Bellinger 		" 0x%02x, AL: %u\n", sa, list_id, se_cmd->data_length);
1027cbf031f4SNicholas Bellinger 
1028cbf031f4SNicholas Bellinger 	if (list_id != 0) {
1029cbf031f4SNicholas Bellinger 		pr_err("Receive Copy Results with non zero list identifier"
1030cbf031f4SNicholas Bellinger 		       " not supported\n");
1031cbf031f4SNicholas Bellinger 		return TCM_INVALID_CDB_FIELD;
1032cbf031f4SNicholas Bellinger 	}
1033cbf031f4SNicholas Bellinger 
1034cbf031f4SNicholas Bellinger 	switch (sa) {
1035cbf031f4SNicholas Bellinger 	case RCR_SA_OPERATING_PARAMETERS:
1036cbf031f4SNicholas Bellinger 		rc = target_rcr_operating_parameters(se_cmd);
1037cbf031f4SNicholas Bellinger 		break;
1038cbf031f4SNicholas Bellinger 	case RCR_SA_COPY_STATUS:
1039cbf031f4SNicholas Bellinger 	case RCR_SA_RECEIVE_DATA:
1040cbf031f4SNicholas Bellinger 	case RCR_SA_FAILED_SEGMENT_DETAILS:
1041cbf031f4SNicholas Bellinger 	default:
1042cbf031f4SNicholas Bellinger 		pr_err("Unsupported SA for receive copy results: 0x%02x\n", sa);
1043cbf031f4SNicholas Bellinger 		return TCM_INVALID_CDB_FIELD;
1044cbf031f4SNicholas Bellinger 	}
1045cbf031f4SNicholas Bellinger 
1046cbf031f4SNicholas Bellinger 	return rc;
1047cbf031f4SNicholas Bellinger }
1048