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 
362896c938SDavid Disseldorp /**
372896c938SDavid Disseldorp  * target_xcopy_locate_se_dev_e4_iter - compare XCOPY NAA device identifiers
382896c938SDavid Disseldorp  *
392896c938SDavid Disseldorp  * @se_dev: device being considered for match
402896c938SDavid Disseldorp  * @dev_wwn: XCOPY requested NAA dev_wwn
412896c938SDavid Disseldorp  * @return: 1 on match, 0 on no-match
422896c938SDavid Disseldorp  */
target_xcopy_locate_se_dev_e4_iter(struct se_device * se_dev,const unsigned char * dev_wwn)436906d008SMike Christie static int target_xcopy_locate_se_dev_e4_iter(struct se_device *se_dev,
442896c938SDavid Disseldorp 					      const unsigned char *dev_wwn)
45cbf031f4SNicholas Bellinger {
4694aae4caSDavid Disseldorp 	unsigned char tmp_dev_wwn[XCOPY_NAA_IEEE_REGEX_LEN];
47cbf031f4SNicholas Bellinger 	int rc;
48cbf031f4SNicholas Bellinger 
492896c938SDavid Disseldorp 	if (!se_dev->dev_attrib.emulate_3pc) {
502896c938SDavid Disseldorp 		pr_debug("XCOPY: emulate_3pc disabled on se_dev %p\n", se_dev);
516906d008SMike Christie 		return 0;
522896c938SDavid Disseldorp 	}
53acb3f260SNicholas Bellinger 
54cbf031f4SNicholas Bellinger 	memset(&tmp_dev_wwn[0], 0, XCOPY_NAA_IEEE_REGEX_LEN);
5517f947b8SSergey Samoylenko 	spc_gen_naa_6h_vendor_specific(se_dev, &tmp_dev_wwn[0]);
56cbf031f4SNicholas Bellinger 
572896c938SDavid Disseldorp 	rc = memcmp(&tmp_dev_wwn[0], dev_wwn, XCOPY_NAA_IEEE_REGEX_LEN);
582896c938SDavid Disseldorp 	if (rc != 0) {
592896c938SDavid Disseldorp 		pr_debug("XCOPY: skip non-matching: %*ph\n",
602896c938SDavid Disseldorp 			 XCOPY_NAA_IEEE_REGEX_LEN, tmp_dev_wwn);
616906d008SMike Christie 		return 0;
622896c938SDavid Disseldorp 	}
6394aae4caSDavid Disseldorp 	pr_debug("XCOPY 0xe4: located se_dev: %p\n", se_dev);
64cbf031f4SNicholas Bellinger 
656906d008SMike Christie 	return 1;
66cbf031f4SNicholas Bellinger }
67cbf031f4SNicholas Bellinger 
target_xcopy_locate_se_dev_e4(struct se_session * sess,const unsigned char * dev_wwn,struct se_device ** _found_dev,struct percpu_ref ** _found_lun_ref)682896c938SDavid Disseldorp static int target_xcopy_locate_se_dev_e4(struct se_session *sess,
692896c938SDavid Disseldorp 					const unsigned char *dev_wwn,
702896c938SDavid Disseldorp 					struct se_device **_found_dev,
712896c938SDavid Disseldorp 					struct percpu_ref **_found_lun_ref)
726906d008SMike Christie {
732896c938SDavid Disseldorp 	struct se_dev_entry *deve;
742896c938SDavid Disseldorp 	struct se_node_acl *nacl;
752896c938SDavid Disseldorp 	struct se_lun *this_lun = NULL;
762896c938SDavid Disseldorp 	struct se_device *found_dev = NULL;
776906d008SMike Christie 
782896c938SDavid Disseldorp 	/* cmd with NULL sess indicates no associated $FABRIC_MOD */
792896c938SDavid Disseldorp 	if (!sess)
802896c938SDavid Disseldorp 		goto err_out;
816906d008SMike Christie 
822896c938SDavid Disseldorp 	pr_debug("XCOPY 0xe4: searching for: %*ph\n",
832896c938SDavid Disseldorp 		 XCOPY_NAA_IEEE_REGEX_LEN, dev_wwn);
842896c938SDavid Disseldorp 
852896c938SDavid Disseldorp 	nacl = sess->se_node_acl;
862896c938SDavid Disseldorp 	rcu_read_lock();
872896c938SDavid Disseldorp 	hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
882896c938SDavid Disseldorp 		struct se_device *this_dev;
892896c938SDavid Disseldorp 		int rc;
902896c938SDavid Disseldorp 
91ef4f7e4bSDmitry Bogdanov 		this_lun = deve->se_lun;
922896c938SDavid Disseldorp 		this_dev = rcu_dereference_raw(this_lun->lun_se_dev);
932896c938SDavid Disseldorp 
942896c938SDavid Disseldorp 		rc = target_xcopy_locate_se_dev_e4_iter(this_dev, dev_wwn);
952896c938SDavid Disseldorp 		if (rc) {
962896c938SDavid Disseldorp 			if (percpu_ref_tryget_live(&this_lun->lun_ref))
972896c938SDavid Disseldorp 				found_dev = this_dev;
982896c938SDavid Disseldorp 			break;
992896c938SDavid Disseldorp 		}
1002896c938SDavid Disseldorp 	}
1012896c938SDavid Disseldorp 	rcu_read_unlock();
1022896c938SDavid Disseldorp 	if (found_dev == NULL)
1032896c938SDavid Disseldorp 		goto err_out;
1042896c938SDavid Disseldorp 
1052896c938SDavid Disseldorp 	pr_debug("lun_ref held for se_dev: %p se_dev->se_dev_group: %p\n",
1062896c938SDavid Disseldorp 		 found_dev, &found_dev->dev_group);
1072896c938SDavid Disseldorp 	*_found_dev = found_dev;
1082896c938SDavid Disseldorp 	*_found_lun_ref = &this_lun->lun_ref;
1096906d008SMike Christie 	return 0;
1102896c938SDavid Disseldorp err_out:
111449a1378SNicholas Bellinger 	pr_debug_ratelimited("Unable to locate 0xe4 descriptor for EXTENDED_COPY\n");
112cbf031f4SNicholas Bellinger 	return -EINVAL;
113cbf031f4SNicholas Bellinger }
114cbf031f4SNicholas Bellinger 
target_xcopy_parse_tiddesc_e4(struct se_cmd * se_cmd,struct xcopy_op * xop,unsigned char * p,unsigned short cscd_index)115cbf031f4SNicholas Bellinger static int target_xcopy_parse_tiddesc_e4(struct se_cmd *se_cmd, struct xcopy_op *xop,
11666640d35SDavid Disseldorp 				unsigned char *p, unsigned short cscd_index)
117cbf031f4SNicholas Bellinger {
118cbf031f4SNicholas Bellinger 	unsigned char *desc = p;
119cbf031f4SNicholas Bellinger 	unsigned short ript;
120cbf031f4SNicholas Bellinger 	u8 desig_len;
121cbf031f4SNicholas Bellinger 	/*
122cbf031f4SNicholas Bellinger 	 * Extract RELATIVE INITIATOR PORT IDENTIFIER
123cbf031f4SNicholas Bellinger 	 */
124cbf031f4SNicholas Bellinger 	ript = get_unaligned_be16(&desc[2]);
125cbf031f4SNicholas Bellinger 	pr_debug("XCOPY 0xe4: RELATIVE INITIATOR PORT IDENTIFIER: %hu\n", ript);
126cbf031f4SNicholas Bellinger 	/*
127cbf031f4SNicholas Bellinger 	 * Check for supported code set, association, and designator type
128cbf031f4SNicholas Bellinger 	 */
129cbf031f4SNicholas Bellinger 	if ((desc[4] & 0x0f) != 0x1) {
130cbf031f4SNicholas Bellinger 		pr_err("XCOPY 0xe4: code set of non binary type not supported\n");
131cbf031f4SNicholas Bellinger 		return -EINVAL;
132cbf031f4SNicholas Bellinger 	}
133cbf031f4SNicholas Bellinger 	if ((desc[5] & 0x30) != 0x00) {
134cbf031f4SNicholas Bellinger 		pr_err("XCOPY 0xe4: association other than LUN not supported\n");
135cbf031f4SNicholas Bellinger 		return -EINVAL;
136cbf031f4SNicholas Bellinger 	}
137cbf031f4SNicholas Bellinger 	if ((desc[5] & 0x0f) != 0x3) {
138cbf031f4SNicholas Bellinger 		pr_err("XCOPY 0xe4: designator type unsupported: 0x%02x\n",
139cbf031f4SNicholas Bellinger 				(desc[5] & 0x0f));
140cbf031f4SNicholas Bellinger 		return -EINVAL;
141cbf031f4SNicholas Bellinger 	}
142cbf031f4SNicholas Bellinger 	/*
143cbf031f4SNicholas Bellinger 	 * Check for matching 16 byte length for NAA IEEE Registered Extended
144cbf031f4SNicholas Bellinger 	 * Assigned designator
145cbf031f4SNicholas Bellinger 	 */
146cbf031f4SNicholas Bellinger 	desig_len = desc[7];
14795b1b51eSDavid Disseldorp 	if (desig_len != XCOPY_NAA_IEEE_REGEX_LEN) {
148cbf031f4SNicholas Bellinger 		pr_err("XCOPY 0xe4: invalid desig_len: %d\n", (int)desig_len);
149cbf031f4SNicholas Bellinger 		return -EINVAL;
150cbf031f4SNicholas Bellinger 	}
151cbf031f4SNicholas Bellinger 	pr_debug("XCOPY 0xe4: desig_len: %d\n", (int)desig_len);
152cbf031f4SNicholas Bellinger 	/*
153cbf031f4SNicholas Bellinger 	 * Check for NAA IEEE Registered Extended Assigned header..
154cbf031f4SNicholas Bellinger 	 */
155cbf031f4SNicholas Bellinger 	if ((desc[8] & 0xf0) != 0x60) {
156cbf031f4SNicholas Bellinger 		pr_err("XCOPY 0xe4: Unsupported DESIGNATOR TYPE: 0x%02x\n",
157cbf031f4SNicholas Bellinger 					(desc[8] & 0xf0));
158cbf031f4SNicholas Bellinger 		return -EINVAL;
159cbf031f4SNicholas Bellinger 	}
160cbf031f4SNicholas Bellinger 
16166640d35SDavid Disseldorp 	if (cscd_index != xop->stdi && cscd_index != xop->dtdi) {
16266640d35SDavid Disseldorp 		pr_debug("XCOPY 0xe4: ignoring CSCD entry %d - neither src nor "
16366640d35SDavid Disseldorp 			 "dest\n", cscd_index);
16466640d35SDavid Disseldorp 		return 0;
16566640d35SDavid Disseldorp 	}
16666640d35SDavid Disseldorp 
16766640d35SDavid Disseldorp 	if (cscd_index == xop->stdi) {
168cbf031f4SNicholas Bellinger 		memcpy(&xop->src_tid_wwn[0], &desc[8], XCOPY_NAA_IEEE_REGEX_LEN);
169cbf031f4SNicholas Bellinger 		/*
170cbf031f4SNicholas Bellinger 		 * Determine if the source designator matches the local device
171cbf031f4SNicholas Bellinger 		 */
172cbf031f4SNicholas Bellinger 		if (!memcmp(&xop->local_dev_wwn[0], &xop->src_tid_wwn[0],
173cbf031f4SNicholas Bellinger 				XCOPY_NAA_IEEE_REGEX_LEN)) {
174cbf031f4SNicholas Bellinger 			xop->op_origin = XCOL_SOURCE_RECV_OP;
175cbf031f4SNicholas Bellinger 			xop->src_dev = se_cmd->se_dev;
176cbf031f4SNicholas Bellinger 			pr_debug("XCOPY 0xe4: Set xop->src_dev %p from source"
177cbf031f4SNicholas Bellinger 					" received xop\n", xop->src_dev);
178cbf031f4SNicholas Bellinger 		}
17966640d35SDavid Disseldorp 	}
18066640d35SDavid Disseldorp 
18166640d35SDavid Disseldorp 	if (cscd_index == xop->dtdi) {
182cbf031f4SNicholas Bellinger 		memcpy(&xop->dst_tid_wwn[0], &desc[8], XCOPY_NAA_IEEE_REGEX_LEN);
183cbf031f4SNicholas Bellinger 		/*
18466640d35SDavid Disseldorp 		 * Determine if the destination designator matches the local
18566640d35SDavid Disseldorp 		 * device. If @cscd_index corresponds to both source (stdi) and
18666640d35SDavid Disseldorp 		 * destination (dtdi), or dtdi comes after stdi, then
18766640d35SDavid Disseldorp 		 * XCOL_DEST_RECV_OP wins.
188cbf031f4SNicholas Bellinger 		 */
189cbf031f4SNicholas Bellinger 		if (!memcmp(&xop->local_dev_wwn[0], &xop->dst_tid_wwn[0],
190cbf031f4SNicholas Bellinger 				XCOPY_NAA_IEEE_REGEX_LEN)) {
191cbf031f4SNicholas Bellinger 			xop->op_origin = XCOL_DEST_RECV_OP;
192cbf031f4SNicholas Bellinger 			xop->dst_dev = se_cmd->se_dev;
193cbf031f4SNicholas Bellinger 			pr_debug("XCOPY 0xe4: Set xop->dst_dev: %p from destination"
194cbf031f4SNicholas Bellinger 				" received xop\n", xop->dst_dev);
195cbf031f4SNicholas Bellinger 		}
196cbf031f4SNicholas Bellinger 	}
197cbf031f4SNicholas Bellinger 
198cbf031f4SNicholas Bellinger 	return 0;
199cbf031f4SNicholas Bellinger }
200cbf031f4SNicholas Bellinger 
target_xcopy_parse_target_descriptors(struct se_cmd * se_cmd,struct xcopy_op * xop,unsigned char * p,unsigned short tdll,sense_reason_t * sense_ret)201cbf031f4SNicholas Bellinger static int target_xcopy_parse_target_descriptors(struct se_cmd *se_cmd,
202cbf031f4SNicholas Bellinger 				struct xcopy_op *xop, unsigned char *p,
203449a1378SNicholas Bellinger 				unsigned short tdll, sense_reason_t *sense_ret)
204cbf031f4SNicholas Bellinger {
205cbf031f4SNicholas Bellinger 	struct se_device *local_dev = se_cmd->se_dev;
206cbf031f4SNicholas Bellinger 	unsigned char *desc = p;
20766640d35SDavid Disseldorp 	int offset = tdll % XCOPY_TARGET_DESC_LEN, rc;
20866640d35SDavid Disseldorp 	unsigned short cscd_index = 0;
209cbf031f4SNicholas Bellinger 	unsigned short start = 0;
210cbf031f4SNicholas Bellinger 
211449a1378SNicholas Bellinger 	*sense_ret = TCM_INVALID_PARAMETER_LIST;
212449a1378SNicholas Bellinger 
213cbf031f4SNicholas Bellinger 	if (offset != 0) {
214cbf031f4SNicholas Bellinger 		pr_err("XCOPY target descriptor list length is not"
215cbf031f4SNicholas Bellinger 			" multiple of %d\n", XCOPY_TARGET_DESC_LEN);
216c2438497SDavid Disseldorp 		*sense_ret = TCM_UNSUPPORTED_TARGET_DESC_TYPE_CODE;
217cbf031f4SNicholas Bellinger 		return -EINVAL;
218cbf031f4SNicholas Bellinger 	}
21961c35919SDavid Disseldorp 	if (tdll > RCR_OP_MAX_TARGET_DESC_COUNT * XCOPY_TARGET_DESC_LEN) {
220cbf031f4SNicholas Bellinger 		pr_err("XCOPY target descriptor supports a maximum"
221cbf031f4SNicholas Bellinger 			" two src/dest descriptors, tdll: %hu too large..\n", tdll);
22261c35919SDavid Disseldorp 		/* spc4r37 6.4.3.4 CSCD DESCRIPTOR LIST LENGTH field */
22361c35919SDavid Disseldorp 		*sense_ret = TCM_TOO_MANY_TARGET_DESCS;
224cbf031f4SNicholas Bellinger 		return -EINVAL;
225cbf031f4SNicholas Bellinger 	}
226cbf031f4SNicholas Bellinger 	/*
227cbf031f4SNicholas Bellinger 	 * Generate an IEEE Registered Extended designator based upon the
228cbf031f4SNicholas Bellinger 	 * se_device the XCOPY was received upon..
229cbf031f4SNicholas Bellinger 	 */
230cbf031f4SNicholas Bellinger 	memset(&xop->local_dev_wwn[0], 0, XCOPY_NAA_IEEE_REGEX_LEN);
23117f947b8SSergey Samoylenko 	spc_gen_naa_6h_vendor_specific(local_dev, &xop->local_dev_wwn[0]);
232cbf031f4SNicholas Bellinger 
233cbf031f4SNicholas Bellinger 	while (start < tdll) {
234cbf031f4SNicholas Bellinger 		/*
23566640d35SDavid Disseldorp 		 * Check target descriptor identification with 0xE4 type, and
23666640d35SDavid Disseldorp 		 * compare the current index with the CSCD descriptor IDs in
23766640d35SDavid Disseldorp 		 * the segment descriptor. Use VPD 0x83 WWPN matching ..
238cbf031f4SNicholas Bellinger 		 */
239cbf031f4SNicholas Bellinger 		switch (desc[0]) {
240cbf031f4SNicholas Bellinger 		case 0xe4:
241cbf031f4SNicholas Bellinger 			rc = target_xcopy_parse_tiddesc_e4(se_cmd, xop,
24266640d35SDavid Disseldorp 							&desc[0], cscd_index);
243cbf031f4SNicholas Bellinger 			if (rc != 0)
244cbf031f4SNicholas Bellinger 				goto out;
245cbf031f4SNicholas Bellinger 			start += XCOPY_TARGET_DESC_LEN;
246cbf031f4SNicholas Bellinger 			desc += XCOPY_TARGET_DESC_LEN;
24766640d35SDavid Disseldorp 			cscd_index++;
248cbf031f4SNicholas Bellinger 			break;
249cbf031f4SNicholas Bellinger 		default:
250cbf031f4SNicholas Bellinger 			pr_err("XCOPY unsupported descriptor type code:"
251cbf031f4SNicholas Bellinger 					" 0x%02x\n", desc[0]);
252c2438497SDavid Disseldorp 			*sense_ret = TCM_UNSUPPORTED_TARGET_DESC_TYPE_CODE;
253cbf031f4SNicholas Bellinger 			goto out;
254cbf031f4SNicholas Bellinger 		}
255cbf031f4SNicholas Bellinger 	}
256cbf031f4SNicholas Bellinger 
25766640d35SDavid Disseldorp 	switch (xop->op_origin) {
25866640d35SDavid Disseldorp 	case XCOL_SOURCE_RECV_OP:
2592896c938SDavid Disseldorp 		rc = target_xcopy_locate_se_dev_e4(se_cmd->se_sess,
2602896c938SDavid Disseldorp 						xop->dst_tid_wwn,
2612896c938SDavid Disseldorp 						&xop->dst_dev,
2622896c938SDavid Disseldorp 						&xop->remote_lun_ref);
26366640d35SDavid Disseldorp 		break;
26466640d35SDavid Disseldorp 	case XCOL_DEST_RECV_OP:
2652896c938SDavid Disseldorp 		rc = target_xcopy_locate_se_dev_e4(se_cmd->se_sess,
2662896c938SDavid Disseldorp 						xop->src_tid_wwn,
2672896c938SDavid Disseldorp 						&xop->src_dev,
2682896c938SDavid Disseldorp 						&xop->remote_lun_ref);
26966640d35SDavid Disseldorp 		break;
27066640d35SDavid Disseldorp 	default:
27166640d35SDavid Disseldorp 		pr_err("XCOPY CSCD descriptor IDs not found in CSCD list - "
27266640d35SDavid Disseldorp 			"stdi: %hu dtdi: %hu\n", xop->stdi, xop->dtdi);
27366640d35SDavid Disseldorp 		rc = -EINVAL;
27466640d35SDavid Disseldorp 		break;
27566640d35SDavid Disseldorp 	}
276449a1378SNicholas Bellinger 	/*
277449a1378SNicholas Bellinger 	 * If a matching IEEE NAA 0x83 descriptor for the requested device
278449a1378SNicholas Bellinger 	 * is not located on this node, return COPY_ABORTED with ASQ/ASQC
279449a1378SNicholas Bellinger 	 * 0x0d/0x02 - COPY_TARGET_DEVICE_NOT_REACHABLE to request the
280449a1378SNicholas Bellinger 	 * initiator to fall back to normal copy method.
281449a1378SNicholas Bellinger 	 */
282449a1378SNicholas Bellinger 	if (rc < 0) {
283449a1378SNicholas Bellinger 		*sense_ret = TCM_COPY_TARGET_DEVICE_NOT_REACHABLE;
284cbf031f4SNicholas Bellinger 		goto out;
285449a1378SNicholas Bellinger 	}
286cbf031f4SNicholas Bellinger 
287cbf031f4SNicholas Bellinger 	pr_debug("XCOPY TGT desc: Source dev: %p NAA IEEE WWN: 0x%16phN\n",
288cbf031f4SNicholas Bellinger 		 xop->src_dev, &xop->src_tid_wwn[0]);
289cbf031f4SNicholas Bellinger 	pr_debug("XCOPY TGT desc: Dest dev: %p NAA IEEE WWN: 0x%16phN\n",
290cbf031f4SNicholas Bellinger 		 xop->dst_dev, &xop->dst_tid_wwn[0]);
291cbf031f4SNicholas Bellinger 
29266640d35SDavid Disseldorp 	return cscd_index;
293cbf031f4SNicholas Bellinger 
294cbf031f4SNicholas Bellinger out:
295cbf031f4SNicholas Bellinger 	return -EINVAL;
296cbf031f4SNicholas Bellinger }
297cbf031f4SNicholas Bellinger 
target_xcopy_parse_segdesc_02(struct xcopy_op * xop,unsigned char * p)2987e642ca0SLi Feng static int target_xcopy_parse_segdesc_02(struct xcopy_op *xop, unsigned char *p)
299cbf031f4SNicholas Bellinger {
300cbf031f4SNicholas Bellinger 	unsigned char *desc = p;
301cbf031f4SNicholas Bellinger 	int dc = (desc[1] & 0x02);
302cbf031f4SNicholas Bellinger 	unsigned short desc_len;
303cbf031f4SNicholas Bellinger 
304cbf031f4SNicholas Bellinger 	desc_len = get_unaligned_be16(&desc[2]);
305cbf031f4SNicholas Bellinger 	if (desc_len != 0x18) {
306cbf031f4SNicholas Bellinger 		pr_err("XCOPY segment desc 0x02: Illegal desc_len:"
307cbf031f4SNicholas Bellinger 				" %hu\n", desc_len);
308cbf031f4SNicholas Bellinger 		return -EINVAL;
309cbf031f4SNicholas Bellinger 	}
310cbf031f4SNicholas Bellinger 
311cbf031f4SNicholas Bellinger 	xop->stdi = get_unaligned_be16(&desc[4]);
312cbf031f4SNicholas Bellinger 	xop->dtdi = get_unaligned_be16(&desc[6]);
313f184210bSDavid Disseldorp 
314f184210bSDavid Disseldorp 	if (xop->stdi > XCOPY_CSCD_DESC_ID_LIST_OFF_MAX ||
315f184210bSDavid Disseldorp 	    xop->dtdi > XCOPY_CSCD_DESC_ID_LIST_OFF_MAX) {
316f184210bSDavid Disseldorp 		pr_err("XCOPY segment desc 0x02: unsupported CSCD ID > 0x%x; stdi: %hu dtdi: %hu\n",
317f184210bSDavid Disseldorp 			XCOPY_CSCD_DESC_ID_LIST_OFF_MAX, xop->stdi, xop->dtdi);
318f184210bSDavid Disseldorp 		return -EINVAL;
319f184210bSDavid Disseldorp 	}
320f184210bSDavid Disseldorp 
321cbf031f4SNicholas Bellinger 	pr_debug("XCOPY seg desc 0x02: desc_len: %hu stdi: %hu dtdi: %hu, DC: %d\n",
322cbf031f4SNicholas Bellinger 		desc_len, xop->stdi, xop->dtdi, dc);
323cbf031f4SNicholas Bellinger 
324cbf031f4SNicholas Bellinger 	xop->nolb = get_unaligned_be16(&desc[10]);
325cbf031f4SNicholas Bellinger 	xop->src_lba = get_unaligned_be64(&desc[12]);
326cbf031f4SNicholas Bellinger 	xop->dst_lba = get_unaligned_be64(&desc[20]);
327cbf031f4SNicholas Bellinger 	pr_debug("XCOPY seg desc 0x02: nolb: %hu src_lba: %llu dst_lba: %llu\n",
328cbf031f4SNicholas Bellinger 		xop->nolb, (unsigned long long)xop->src_lba,
329cbf031f4SNicholas Bellinger 		(unsigned long long)xop->dst_lba);
330cbf031f4SNicholas Bellinger 
331cbf031f4SNicholas Bellinger 	return 0;
332cbf031f4SNicholas Bellinger }
333cbf031f4SNicholas Bellinger 
target_xcopy_parse_segment_descriptors(struct xcopy_op * xop,unsigned char * p,unsigned int sdll,sense_reason_t * sense_ret)3347e642ca0SLi Feng static int target_xcopy_parse_segment_descriptors(struct xcopy_op *xop,
3357e642ca0SLi Feng 				unsigned char *p, unsigned int sdll,
3367e642ca0SLi Feng 				sense_reason_t *sense_ret)
337cbf031f4SNicholas Bellinger {
338cbf031f4SNicholas Bellinger 	unsigned char *desc = p;
339cbf031f4SNicholas Bellinger 	unsigned int start = 0;
340cbf031f4SNicholas Bellinger 	int offset = sdll % XCOPY_SEGMENT_DESC_LEN, rc, ret = 0;
341cbf031f4SNicholas Bellinger 
342af9f62c1SDavid Disseldorp 	*sense_ret = TCM_INVALID_PARAMETER_LIST;
343af9f62c1SDavid Disseldorp 
344cbf031f4SNicholas Bellinger 	if (offset != 0) {
345cbf031f4SNicholas Bellinger 		pr_err("XCOPY segment descriptor list length is not"
346cbf031f4SNicholas Bellinger 			" multiple of %d\n", XCOPY_SEGMENT_DESC_LEN);
347c2438497SDavid Disseldorp 		*sense_ret = TCM_UNSUPPORTED_SEGMENT_DESC_TYPE_CODE;
348cbf031f4SNicholas Bellinger 		return -EINVAL;
349cbf031f4SNicholas Bellinger 	}
350af9f62c1SDavid Disseldorp 	if (sdll > RCR_OP_MAX_SG_DESC_COUNT * XCOPY_SEGMENT_DESC_LEN) {
351af9f62c1SDavid Disseldorp 		pr_err("XCOPY supports %u segment descriptor(s), sdll: %u too"
352af9f62c1SDavid Disseldorp 			" large..\n", RCR_OP_MAX_SG_DESC_COUNT, sdll);
353af9f62c1SDavid Disseldorp 		/* spc4r37 6.4.3.5 SEGMENT DESCRIPTOR LIST LENGTH field */
354af9f62c1SDavid Disseldorp 		*sense_ret = TCM_TOO_MANY_SEGMENT_DESCS;
355af9f62c1SDavid Disseldorp 		return -EINVAL;
356af9f62c1SDavid Disseldorp 	}
357cbf031f4SNicholas Bellinger 
358cbf031f4SNicholas Bellinger 	while (start < sdll) {
359cbf031f4SNicholas Bellinger 		/*
360cbf031f4SNicholas Bellinger 		 * Check segment descriptor type code for block -> block
361cbf031f4SNicholas Bellinger 		 */
362cbf031f4SNicholas Bellinger 		switch (desc[0]) {
363cbf031f4SNicholas Bellinger 		case 0x02:
3647e642ca0SLi Feng 			rc = target_xcopy_parse_segdesc_02(xop, desc);
365cbf031f4SNicholas Bellinger 			if (rc < 0)
366cbf031f4SNicholas Bellinger 				goto out;
367cbf031f4SNicholas Bellinger 
368cbf031f4SNicholas Bellinger 			ret++;
369cbf031f4SNicholas Bellinger 			start += XCOPY_SEGMENT_DESC_LEN;
370cbf031f4SNicholas Bellinger 			desc += XCOPY_SEGMENT_DESC_LEN;
371cbf031f4SNicholas Bellinger 			break;
372cbf031f4SNicholas Bellinger 		default:
3736774def6SMasanari Iida 			pr_err("XCOPY unsupported segment descriptor"
374cbf031f4SNicholas Bellinger 				"type: 0x%02x\n", desc[0]);
375c2438497SDavid Disseldorp 			*sense_ret = TCM_UNSUPPORTED_SEGMENT_DESC_TYPE_CODE;
376cbf031f4SNicholas Bellinger 			goto out;
377cbf031f4SNicholas Bellinger 		}
378cbf031f4SNicholas Bellinger 	}
379cbf031f4SNicholas Bellinger 
380cbf031f4SNicholas Bellinger 	return ret;
381cbf031f4SNicholas Bellinger 
382cbf031f4SNicholas Bellinger out:
383cbf031f4SNicholas Bellinger 	return -EINVAL;
384cbf031f4SNicholas Bellinger }
385cbf031f4SNicholas Bellinger 
386cbf031f4SNicholas Bellinger /*
387cbf031f4SNicholas Bellinger  * Start xcopy_pt ops
388cbf031f4SNicholas Bellinger  */
389cbf031f4SNicholas Bellinger 
390cbf031f4SNicholas Bellinger struct xcopy_pt_cmd {
391cbf031f4SNicholas Bellinger 	struct se_cmd se_cmd;
392cbf031f4SNicholas Bellinger 	struct completion xpt_passthrough_sem;
393366bda19SNicholas Bellinger 	unsigned char sense_buffer[TRANSPORT_SENSE_BUFFER];
394cbf031f4SNicholas Bellinger };
395cbf031f4SNicholas Bellinger 
396adf653f9SChristoph Hellwig struct se_portal_group xcopy_pt_tpg;
397cbf031f4SNicholas Bellinger static struct se_session xcopy_pt_sess;
398cbf031f4SNicholas Bellinger static struct se_node_acl xcopy_pt_nacl;
399cbf031f4SNicholas Bellinger 
xcopy_pt_get_cmd_state(struct se_cmd * se_cmd)400cbf031f4SNicholas Bellinger static int xcopy_pt_get_cmd_state(struct se_cmd *se_cmd)
401cbf031f4SNicholas Bellinger {
402cbf031f4SNicholas Bellinger         return 0;
403cbf031f4SNicholas Bellinger }
404cbf031f4SNicholas Bellinger 
xcopy_pt_undepend_remotedev(struct xcopy_op * xop)405cbf031f4SNicholas Bellinger static void xcopy_pt_undepend_remotedev(struct xcopy_op *xop)
406cbf031f4SNicholas Bellinger {
407cbf031f4SNicholas Bellinger 	if (xop->op_origin == XCOL_SOURCE_RECV_OP)
4082896c938SDavid Disseldorp 		pr_debug("putting dst lun_ref for %p\n", xop->dst_dev);
409cbf031f4SNicholas Bellinger 	else
4102896c938SDavid Disseldorp 		pr_debug("putting src lun_ref for %p\n", xop->src_dev);
411cbf031f4SNicholas Bellinger 
4122896c938SDavid Disseldorp 	percpu_ref_put(xop->remote_lun_ref);
413cbf031f4SNicholas Bellinger }
414cbf031f4SNicholas Bellinger 
xcopy_pt_release_cmd(struct se_cmd * se_cmd)415cbf031f4SNicholas Bellinger static void xcopy_pt_release_cmd(struct se_cmd *se_cmd)
416cbf031f4SNicholas Bellinger {
417cbf031f4SNicholas Bellinger 	struct xcopy_pt_cmd *xpt_cmd = container_of(se_cmd,
418cbf031f4SNicholas Bellinger 				struct xcopy_pt_cmd, se_cmd);
419cbf031f4SNicholas Bellinger 
420b92fcfcbSDavid Disseldorp 	/* xpt_cmd is on the stack, nothing to free here */
421b92fcfcbSDavid Disseldorp 	pr_debug("xpt_cmd done: %p\n", xpt_cmd);
422cbf031f4SNicholas Bellinger }
423cbf031f4SNicholas Bellinger 
xcopy_pt_check_stop_free(struct se_cmd * se_cmd)424cbf031f4SNicholas Bellinger static int xcopy_pt_check_stop_free(struct se_cmd *se_cmd)
425cbf031f4SNicholas Bellinger {
426cbf031f4SNicholas Bellinger 	struct xcopy_pt_cmd *xpt_cmd = container_of(se_cmd,
427cbf031f4SNicholas Bellinger 				struct xcopy_pt_cmd, se_cmd);
428cbf031f4SNicholas Bellinger 
429cbf031f4SNicholas Bellinger 	complete(&xpt_cmd->xpt_passthrough_sem);
430cbf031f4SNicholas Bellinger 	return 0;
431cbf031f4SNicholas Bellinger }
432cbf031f4SNicholas Bellinger 
xcopy_pt_write_pending(struct se_cmd * se_cmd)433cbf031f4SNicholas Bellinger static int xcopy_pt_write_pending(struct se_cmd *se_cmd)
434cbf031f4SNicholas Bellinger {
435cbf031f4SNicholas Bellinger 	return 0;
436cbf031f4SNicholas Bellinger }
437cbf031f4SNicholas Bellinger 
xcopy_pt_queue_data_in(struct se_cmd * se_cmd)438cbf031f4SNicholas Bellinger static int xcopy_pt_queue_data_in(struct se_cmd *se_cmd)
439cbf031f4SNicholas Bellinger {
440cbf031f4SNicholas Bellinger 	return 0;
441cbf031f4SNicholas Bellinger }
442cbf031f4SNicholas Bellinger 
xcopy_pt_queue_status(struct se_cmd * se_cmd)443cbf031f4SNicholas Bellinger static int xcopy_pt_queue_status(struct se_cmd *se_cmd)
444cbf031f4SNicholas Bellinger {
445cbf031f4SNicholas Bellinger 	return 0;
446cbf031f4SNicholas Bellinger }
447cbf031f4SNicholas Bellinger 
4489ac8928eSChristoph Hellwig static const struct target_core_fabric_ops xcopy_pt_tfo = {
44930c7ca93SDavid Disseldorp 	.fabric_name		= "xcopy-pt",
450cbf031f4SNicholas Bellinger 	.get_cmd_state		= xcopy_pt_get_cmd_state,
451cbf031f4SNicholas Bellinger 	.release_cmd		= xcopy_pt_release_cmd,
452cbf031f4SNicholas Bellinger 	.check_stop_free	= xcopy_pt_check_stop_free,
453cbf031f4SNicholas Bellinger 	.write_pending		= xcopy_pt_write_pending,
454cbf031f4SNicholas Bellinger 	.queue_data_in		= xcopy_pt_queue_data_in,
455cbf031f4SNicholas Bellinger 	.queue_status		= xcopy_pt_queue_status,
456cbf031f4SNicholas Bellinger };
457cbf031f4SNicholas Bellinger 
458cbf031f4SNicholas Bellinger /*
459cbf031f4SNicholas Bellinger  * End xcopy_pt_ops
460cbf031f4SNicholas Bellinger  */
461cbf031f4SNicholas Bellinger 
target_xcopy_setup_pt(void)462cbf031f4SNicholas Bellinger int target_xcopy_setup_pt(void)
463cbf031f4SNicholas Bellinger {
464cbf031f4SNicholas Bellinger 	xcopy_wq = alloc_workqueue("xcopy_wq", WQ_MEM_RECLAIM, 0);
465cbf031f4SNicholas Bellinger 	if (!xcopy_wq) {
466cbf031f4SNicholas Bellinger 		pr_err("Unable to allocate xcopy_wq\n");
467cbf031f4SNicholas Bellinger 		return -ENOMEM;
468cbf031f4SNicholas Bellinger 	}
469cbf031f4SNicholas Bellinger 
470cbf031f4SNicholas Bellinger 	memset(&xcopy_pt_tpg, 0, sizeof(struct se_portal_group));
471cbf031f4SNicholas Bellinger 	INIT_LIST_HEAD(&xcopy_pt_tpg.acl_node_list);
472cbf031f4SNicholas Bellinger 	INIT_LIST_HEAD(&xcopy_pt_tpg.tpg_sess_list);
473cbf031f4SNicholas Bellinger 
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));
4804edba7e4SMike Christie 	transport_init_session(&xcopy_pt_sess);
481cbf031f4SNicholas Bellinger 
482cbf031f4SNicholas Bellinger 	xcopy_pt_nacl.se_tpg = &xcopy_pt_tpg;
483cbf031f4SNicholas Bellinger 	xcopy_pt_nacl.nacl_sess = &xcopy_pt_sess;
484cbf031f4SNicholas Bellinger 
485cbf031f4SNicholas Bellinger 	xcopy_pt_sess.se_tpg = &xcopy_pt_tpg;
486cbf031f4SNicholas Bellinger 	xcopy_pt_sess.se_node_acl = &xcopy_pt_nacl;
487cbf031f4SNicholas Bellinger 
488cbf031f4SNicholas Bellinger 	return 0;
489cbf031f4SNicholas Bellinger }
490cbf031f4SNicholas Bellinger 
target_xcopy_release_pt(void)491cbf031f4SNicholas Bellinger void target_xcopy_release_pt(void)
492cbf031f4SNicholas Bellinger {
4934edba7e4SMike Christie 	if (xcopy_wq)
494cbf031f4SNicholas Bellinger 		destroy_workqueue(xcopy_wq);
495cbf031f4SNicholas Bellinger }
496cbf031f4SNicholas Bellinger 
497be71530aSBart Van Assche /*
498be71530aSBart Van Assche  * target_xcopy_setup_pt_cmd - set up a pass-through command
499be71530aSBart Van Assche  * @xpt_cmd:	 Data structure to initialize.
500be71530aSBart Van Assche  * @xop:	 Describes the XCOPY operation received from an initiator.
501be71530aSBart Van Assche  * @se_dev:	 Backend device to associate with @xpt_cmd if
502be71530aSBart Van Assche  *		 @remote_port == true.
503be71530aSBart Van Assche  * @cdb:	 SCSI CDB to be copied into @xpt_cmd.
504be71530aSBart Van Assche  * @remote_port: If false, use the LUN through which the XCOPY command has
505be71530aSBart Van Assche  *		 been received. If true, use @se_dev->xcopy_lun.
506be71530aSBart Van Assche  *
507be71530aSBart Van Assche  * Set up a SCSI command (READ or WRITE) that will be used to execute an
508be71530aSBart Van Assche  * XCOPY command.
509be71530aSBart Van Assche  */
target_xcopy_setup_pt_cmd(struct xcopy_pt_cmd * xpt_cmd,struct xcopy_op * xop,struct se_device * se_dev,unsigned char * cdb,bool remote_port)510cbf031f4SNicholas Bellinger static int target_xcopy_setup_pt_cmd(
511cbf031f4SNicholas Bellinger 	struct xcopy_pt_cmd *xpt_cmd,
512cbf031f4SNicholas Bellinger 	struct xcopy_op *xop,
513cbf031f4SNicholas Bellinger 	struct se_device *se_dev,
514cbf031f4SNicholas Bellinger 	unsigned char *cdb,
5150ad08996SDavid Disseldorp 	bool remote_port)
516cbf031f4SNicholas Bellinger {
517cbf031f4SNicholas Bellinger 	struct se_cmd *cmd = &xpt_cmd->se_cmd;
518be71530aSBart Van Assche 
519cbf031f4SNicholas Bellinger 	/*
520cbf031f4SNicholas Bellinger 	 * Setup LUN+port to honor reservations based upon xop->op_origin for
521cbf031f4SNicholas Bellinger 	 * X-COPY PUSH or X-COPY PULL based upon where the CDB was received.
522cbf031f4SNicholas Bellinger 	 */
523be71530aSBart Van Assche 	if (remote_port) {
524be71530aSBart Van Assche 		cmd->se_lun = &se_dev->xcopy_lun;
525be71530aSBart Van Assche 		cmd->se_dev = se_dev;
526be71530aSBart Van Assche 	} else {
527be71530aSBart Van Assche 		cmd->se_lun = xop->xop_se_cmd->se_lun;
528be71530aSBart Van Assche 		cmd->se_dev = xop->xop_se_cmd->se_dev;
529be71530aSBart Van Assche 	}
530be71530aSBart Van Assche 	cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
531cbf031f4SNicholas Bellinger 
53208694199SMike Christie 	if (target_cmd_init_cdb(cmd, cdb, GFP_KERNEL))
5339e95fb80SSudhakar Panneerselvam 		return -EINVAL;
5349e95fb80SSudhakar Panneerselvam 
535649ee054SBart Van Assche 	cmd->tag = 0;
536987db587SSudhakar Panneerselvam 	if (target_cmd_parse_cdb(cmd))
5370ad08996SDavid Disseldorp 		return -EINVAL;
538cbf031f4SNicholas Bellinger 
5390ad08996SDavid Disseldorp 	if (transport_generic_map_mem_to_cmd(cmd, xop->xop_data_sg,
5400ad08996SDavid Disseldorp 					xop->xop_data_nents, NULL, 0))
5410ad08996SDavid Disseldorp 		return -EINVAL;
542cbf031f4SNicholas Bellinger 
543cbf031f4SNicholas Bellinger 	pr_debug("Setup PASSTHROUGH_NOALLOC t_data_sg: %p t_data_nents:"
544cbf031f4SNicholas Bellinger 		 " %u\n", cmd->t_data_sg, cmd->t_data_nents);
545cbf031f4SNicholas Bellinger 
546cbf031f4SNicholas Bellinger 	return 0;
547cbf031f4SNicholas Bellinger }
548cbf031f4SNicholas Bellinger 
target_xcopy_issue_pt_cmd(struct xcopy_pt_cmd * xpt_cmd)549cbf031f4SNicholas Bellinger static int target_xcopy_issue_pt_cmd(struct xcopy_pt_cmd *xpt_cmd)
550cbf031f4SNicholas Bellinger {
551cbf031f4SNicholas Bellinger 	struct se_cmd *se_cmd = &xpt_cmd->se_cmd;
552cbf031f4SNicholas Bellinger 	sense_reason_t sense_rc;
553cbf031f4SNicholas Bellinger 
554cbf031f4SNicholas Bellinger 	sense_rc = transport_generic_new_cmd(se_cmd);
555cbf031f4SNicholas Bellinger 	if (sense_rc)
556cbf031f4SNicholas Bellinger 		return -EINVAL;
557cbf031f4SNicholas Bellinger 
558cbf031f4SNicholas Bellinger 	if (se_cmd->data_direction == DMA_TO_DEVICE)
559cbf031f4SNicholas Bellinger 		target_execute_cmd(se_cmd);
560cbf031f4SNicholas Bellinger 
561cbf031f4SNicholas Bellinger 	wait_for_completion_interruptible(&xpt_cmd->xpt_passthrough_sem);
562cbf031f4SNicholas Bellinger 
563cbf031f4SNicholas Bellinger 	pr_debug("target_xcopy_issue_pt_cmd(): SCSI status: 0x%02x\n",
564cbf031f4SNicholas Bellinger 			se_cmd->scsi_status);
5658a955d6dSNicholas Bellinger 
5668a955d6dSNicholas Bellinger 	return (se_cmd->scsi_status) ? -EINVAL : 0;
567cbf031f4SNicholas Bellinger }
568cbf031f4SNicholas Bellinger 
target_xcopy_read_source(struct se_cmd * ec_cmd,struct xcopy_op * xop,struct se_device * src_dev,sector_t src_lba,u32 src_bytes)569cbf031f4SNicholas Bellinger static int target_xcopy_read_source(
570cbf031f4SNicholas Bellinger 	struct se_cmd *ec_cmd,
571cbf031f4SNicholas Bellinger 	struct xcopy_op *xop,
572cbf031f4SNicholas Bellinger 	struct se_device *src_dev,
573cbf031f4SNicholas Bellinger 	sector_t src_lba,
574689d94ecSAnastasia Kovaleva 	u32 src_bytes)
575cbf031f4SNicholas Bellinger {
576b92fcfcbSDavid Disseldorp 	struct xcopy_pt_cmd xpt_cmd;
577b92fcfcbSDavid Disseldorp 	struct se_cmd *se_cmd = &xpt_cmd.se_cmd;
578689d94ecSAnastasia Kovaleva 	u32 transfer_length_block = src_bytes / src_dev->dev_attrib.block_size;
579cbf031f4SNicholas Bellinger 	int rc;
580cbf031f4SNicholas Bellinger 	unsigned char cdb[16];
581cbf031f4SNicholas Bellinger 	bool remote_port = (xop->op_origin == XCOL_DEST_RECV_OP);
582cbf031f4SNicholas Bellinger 
583b92fcfcbSDavid Disseldorp 	memset(&xpt_cmd, 0, sizeof(xpt_cmd));
584b92fcfcbSDavid Disseldorp 	init_completion(&xpt_cmd.xpt_passthrough_sem);
585cbf031f4SNicholas Bellinger 
586cbf031f4SNicholas Bellinger 	memset(&cdb[0], 0, 16);
587cbf031f4SNicholas Bellinger 	cdb[0] = READ_16;
588cbf031f4SNicholas Bellinger 	put_unaligned_be64(src_lba, &cdb[2]);
589689d94ecSAnastasia Kovaleva 	put_unaligned_be32(transfer_length_block, &cdb[10]);
590689d94ecSAnastasia Kovaleva 	pr_debug("XCOPY: Built READ_16: LBA: %llu Blocks: %u Length: %u\n",
591689d94ecSAnastasia Kovaleva 		(unsigned long long)src_lba, transfer_length_block, src_bytes);
592cbf031f4SNicholas Bellinger 
593689d94ecSAnastasia Kovaleva 	__target_init_cmd(se_cmd, &xcopy_pt_tfo, &xcopy_pt_sess, src_bytes,
594*8e288be8SMike Christie 			  DMA_FROM_DEVICE, 0, &xpt_cmd.sense_buffer[0], 0,
595*8e288be8SMike Christie 			  NULL);
596b92fcfcbSDavid Disseldorp 	rc = target_xcopy_setup_pt_cmd(&xpt_cmd, xop, src_dev, &cdb[0],
5970ad08996SDavid Disseldorp 				remote_port);
598cbf031f4SNicholas Bellinger 	if (rc < 0) {
599b92fcfcbSDavid Disseldorp 		ec_cmd->scsi_status = se_cmd->scsi_status;
600b92fcfcbSDavid Disseldorp 		goto out;
601cbf031f4SNicholas Bellinger 	}
602cbf031f4SNicholas Bellinger 
603cbf031f4SNicholas Bellinger 	pr_debug("XCOPY-READ: Saved xop->xop_data_sg: %p, num: %u for READ"
604cbf031f4SNicholas Bellinger 		" memory\n", xop->xop_data_sg, xop->xop_data_nents);
605cbf031f4SNicholas Bellinger 
606b92fcfcbSDavid Disseldorp 	rc = target_xcopy_issue_pt_cmd(&xpt_cmd);
607b92fcfcbSDavid Disseldorp 	if (rc < 0)
608b92fcfcbSDavid Disseldorp 		ec_cmd->scsi_status = se_cmd->scsi_status;
609b92fcfcbSDavid Disseldorp out:
610cbf031f4SNicholas Bellinger 	transport_generic_free_cmd(se_cmd, 0);
611cbf031f4SNicholas Bellinger 	return rc;
612cbf031f4SNicholas Bellinger }
613cbf031f4SNicholas Bellinger 
target_xcopy_write_destination(struct se_cmd * ec_cmd,struct xcopy_op * xop,struct se_device * dst_dev,sector_t dst_lba,u32 dst_bytes)614cbf031f4SNicholas Bellinger static int target_xcopy_write_destination(
615cbf031f4SNicholas Bellinger 	struct se_cmd *ec_cmd,
616cbf031f4SNicholas Bellinger 	struct xcopy_op *xop,
617cbf031f4SNicholas Bellinger 	struct se_device *dst_dev,
618cbf031f4SNicholas Bellinger 	sector_t dst_lba,
619689d94ecSAnastasia Kovaleva 	u32 dst_bytes)
620cbf031f4SNicholas Bellinger {
621b92fcfcbSDavid Disseldorp 	struct xcopy_pt_cmd xpt_cmd;
622b92fcfcbSDavid Disseldorp 	struct se_cmd *se_cmd = &xpt_cmd.se_cmd;
623689d94ecSAnastasia Kovaleva 	u32 transfer_length_block = dst_bytes / dst_dev->dev_attrib.block_size;
624cbf031f4SNicholas Bellinger 	int rc;
625cbf031f4SNicholas Bellinger 	unsigned char cdb[16];
626cbf031f4SNicholas Bellinger 	bool remote_port = (xop->op_origin == XCOL_SOURCE_RECV_OP);
627cbf031f4SNicholas Bellinger 
628b92fcfcbSDavid Disseldorp 	memset(&xpt_cmd, 0, sizeof(xpt_cmd));
629b92fcfcbSDavid Disseldorp 	init_completion(&xpt_cmd.xpt_passthrough_sem);
630cbf031f4SNicholas Bellinger 
631cbf031f4SNicholas Bellinger 	memset(&cdb[0], 0, 16);
632cbf031f4SNicholas Bellinger 	cdb[0] = WRITE_16;
633cbf031f4SNicholas Bellinger 	put_unaligned_be64(dst_lba, &cdb[2]);
634689d94ecSAnastasia Kovaleva 	put_unaligned_be32(transfer_length_block, &cdb[10]);
635689d94ecSAnastasia Kovaleva 	pr_debug("XCOPY: Built WRITE_16: LBA: %llu Blocks: %u Length: %u\n",
636689d94ecSAnastasia Kovaleva 		(unsigned long long)dst_lba, transfer_length_block, dst_bytes);
637cbf031f4SNicholas Bellinger 
638689d94ecSAnastasia Kovaleva 	__target_init_cmd(se_cmd, &xcopy_pt_tfo, &xcopy_pt_sess, dst_bytes,
639*8e288be8SMike Christie 			  DMA_TO_DEVICE, 0, &xpt_cmd.sense_buffer[0], 0,
640*8e288be8SMike Christie 			  NULL);
641b92fcfcbSDavid Disseldorp 	rc = target_xcopy_setup_pt_cmd(&xpt_cmd, xop, dst_dev, &cdb[0],
6420ad08996SDavid Disseldorp 				remote_port);
643cbf031f4SNicholas Bellinger 	if (rc < 0) {
644b92fcfcbSDavid Disseldorp 		ec_cmd->scsi_status = se_cmd->scsi_status;
645b92fcfcbSDavid Disseldorp 		goto out;
646cbf031f4SNicholas Bellinger 	}
647cbf031f4SNicholas Bellinger 
648b92fcfcbSDavid Disseldorp 	rc = target_xcopy_issue_pt_cmd(&xpt_cmd);
649b92fcfcbSDavid Disseldorp 	if (rc < 0)
650b92fcfcbSDavid Disseldorp 		ec_cmd->scsi_status = se_cmd->scsi_status;
651b92fcfcbSDavid Disseldorp out:
652cbf031f4SNicholas Bellinger 	transport_generic_free_cmd(se_cmd, 0);
653cbf031f4SNicholas Bellinger 	return rc;
654cbf031f4SNicholas Bellinger }
655cbf031f4SNicholas Bellinger 
target_xcopy_do_work(struct work_struct * work)656cbf031f4SNicholas Bellinger static void target_xcopy_do_work(struct work_struct *work)
657cbf031f4SNicholas Bellinger {
658cbf031f4SNicholas Bellinger 	struct xcopy_op *xop = container_of(work, struct xcopy_op, xop_work);
659cbf031f4SNicholas Bellinger 	struct se_cmd *ec_cmd = xop->xop_se_cmd;
660d877d727SBart Van Assche 	struct se_device *src_dev, *dst_dev;
661d877d727SBart Van Assche 	sector_t src_lba, dst_lba, end_lba;
662689d94ecSAnastasia Kovaleva 	unsigned long long max_bytes, max_bytes_src, max_bytes_dst, max_blocks;
663d877d727SBart Van Assche 	int rc = 0;
664689d94ecSAnastasia Kovaleva 	unsigned short nolb;
665689d94ecSAnastasia Kovaleva 	unsigned int copied_bytes = 0;
6660394b504SSergey Samoylenko 	sense_reason_t sense_rc;
667cbf031f4SNicholas Bellinger 
6680394b504SSergey Samoylenko 	sense_rc = target_parse_xcopy_cmd(xop);
6690394b504SSergey Samoylenko 	if (sense_rc != TCM_NO_SENSE)
670d877d727SBart Van Assche 		goto err_free;
671d877d727SBart Van Assche 
6720394b504SSergey Samoylenko 	if (WARN_ON_ONCE(!xop->src_dev) || WARN_ON_ONCE(!xop->dst_dev)) {
6730394b504SSergey Samoylenko 		sense_rc = TCM_INVALID_PARAMETER_LIST;
674d877d727SBart Van Assche 		goto err_free;
6750394b504SSergey Samoylenko 	}
676d877d727SBart Van Assche 
677d877d727SBart Van Assche 	src_dev = xop->src_dev;
678d877d727SBart Van Assche 	dst_dev = xop->dst_dev;
679d877d727SBart Van Assche 	src_lba = xop->src_lba;
680d877d727SBart Van Assche 	dst_lba = xop->dst_lba;
681d877d727SBart Van Assche 	nolb = xop->nolb;
682cbf031f4SNicholas Bellinger 	end_lba = src_lba + nolb;
683cbf031f4SNicholas Bellinger 	/*
684689d94ecSAnastasia Kovaleva 	 * Break up XCOPY I/O into hw_max_sectors * hw_block_size sized
685689d94ecSAnastasia Kovaleva 	 * I/O based on the smallest max_bytes between src_dev + dst_dev
686cbf031f4SNicholas Bellinger 	 */
687689d94ecSAnastasia Kovaleva 	max_bytes_src = (unsigned long long) src_dev->dev_attrib.hw_max_sectors *
688689d94ecSAnastasia Kovaleva 			src_dev->dev_attrib.hw_block_size;
689689d94ecSAnastasia Kovaleva 	max_bytes_dst = (unsigned long long) dst_dev->dev_attrib.hw_max_sectors *
690689d94ecSAnastasia Kovaleva 			dst_dev->dev_attrib.hw_block_size;
691cbf031f4SNicholas Bellinger 
692689d94ecSAnastasia Kovaleva 	max_bytes = min_t(u64, max_bytes_src, max_bytes_dst);
693689d94ecSAnastasia Kovaleva 	max_bytes = min_t(u64, max_bytes, XCOPY_MAX_BYTES);
694cbf031f4SNicholas Bellinger 
695689d94ecSAnastasia Kovaleva 	/*
696689d94ecSAnastasia Kovaleva 	 * Using shift instead of the division because otherwise GCC
697689d94ecSAnastasia Kovaleva 	 * generates __udivdi3 that is missing on i386
698689d94ecSAnastasia Kovaleva 	 */
699689d94ecSAnastasia Kovaleva 	max_blocks = max_bytes >> ilog2(src_dev->dev_attrib.block_size);
700689d94ecSAnastasia Kovaleva 
701689d94ecSAnastasia Kovaleva 	pr_debug("%s: nolb: %u, max_blocks: %llu end_lba: %llu\n", __func__,
702689d94ecSAnastasia Kovaleva 			nolb, max_blocks, (unsigned long long)end_lba);
703689d94ecSAnastasia Kovaleva 	pr_debug("%s: Starting src_lba: %llu, dst_lba: %llu\n", __func__,
704cbf031f4SNicholas Bellinger 			(unsigned long long)src_lba, (unsigned long long)dst_lba);
705cbf031f4SNicholas Bellinger 
706689d94ecSAnastasia Kovaleva 	while (nolb) {
707689d94ecSAnastasia Kovaleva 		u32 cur_bytes = min_t(u64, max_bytes, nolb * src_dev->dev_attrib.block_size);
708689d94ecSAnastasia Kovaleva 		unsigned short cur_nolb = cur_bytes / src_dev->dev_attrib.block_size;
7090ad08996SDavid Disseldorp 
7100ad08996SDavid Disseldorp 		if (cur_bytes != xop->xop_data_bytes) {
7110ad08996SDavid Disseldorp 			/*
7120ad08996SDavid Disseldorp 			 * (Re)allocate a buffer large enough to hold the XCOPY
7130ad08996SDavid Disseldorp 			 * I/O size, which can be reused each read / write loop.
7140ad08996SDavid Disseldorp 			 */
7150ad08996SDavid Disseldorp 			target_free_sgl(xop->xop_data_sg, xop->xop_data_nents);
7160ad08996SDavid Disseldorp 			rc = target_alloc_sgl(&xop->xop_data_sg,
7170ad08996SDavid Disseldorp 					      &xop->xop_data_nents,
7180ad08996SDavid Disseldorp 					      cur_bytes,
7190ad08996SDavid Disseldorp 					      false, false);
7200ad08996SDavid Disseldorp 			if (rc < 0)
7210ad08996SDavid Disseldorp 				goto out;
7220ad08996SDavid Disseldorp 			xop->xop_data_bytes = cur_bytes;
7230ad08996SDavid Disseldorp 		}
724cbf031f4SNicholas Bellinger 
725689d94ecSAnastasia Kovaleva 		pr_debug("%s: Calling read src_dev: %p src_lba: %llu, cur_nolb: %hu\n",
726689d94ecSAnastasia Kovaleva 				__func__, src_dev, (unsigned long long)src_lba, cur_nolb);
727cbf031f4SNicholas Bellinger 
728689d94ecSAnastasia Kovaleva 		rc = target_xcopy_read_source(ec_cmd, xop, src_dev, src_lba, cur_bytes);
729cbf031f4SNicholas Bellinger 		if (rc < 0)
730cbf031f4SNicholas Bellinger 			goto out;
731cbf031f4SNicholas Bellinger 
732689d94ecSAnastasia Kovaleva 		src_lba += cur_bytes / src_dev->dev_attrib.block_size;
733689d94ecSAnastasia Kovaleva 		pr_debug("%s: Incremented READ src_lba to %llu\n", __func__,
734cbf031f4SNicholas Bellinger 				(unsigned long long)src_lba);
735cbf031f4SNicholas Bellinger 
736689d94ecSAnastasia Kovaleva 		pr_debug("%s: Calling write dst_dev: %p dst_lba: %llu, cur_nolb: %u\n",
737689d94ecSAnastasia Kovaleva 				__func__, dst_dev, (unsigned long long)dst_lba, cur_nolb);
738cbf031f4SNicholas Bellinger 
739cbf031f4SNicholas Bellinger 		rc = target_xcopy_write_destination(ec_cmd, xop, dst_dev,
740689d94ecSAnastasia Kovaleva 						dst_lba, cur_bytes);
741b92fcfcbSDavid Disseldorp 		if (rc < 0)
742cbf031f4SNicholas Bellinger 			goto out;
743cbf031f4SNicholas Bellinger 
744689d94ecSAnastasia Kovaleva 		dst_lba += cur_bytes / dst_dev->dev_attrib.block_size;
745689d94ecSAnastasia Kovaleva 		pr_debug("%s: Incremented WRITE dst_lba to %llu\n", __func__,
746cbf031f4SNicholas Bellinger 				(unsigned long long)dst_lba);
747cbf031f4SNicholas Bellinger 
748689d94ecSAnastasia Kovaleva 		copied_bytes += cur_bytes;
749689d94ecSAnastasia Kovaleva 		nolb -= cur_bytes / src_dev->dev_attrib.block_size;
750cbf031f4SNicholas Bellinger 	}
751cbf031f4SNicholas Bellinger 
752cbf031f4SNicholas Bellinger 	xcopy_pt_undepend_remotedev(xop);
7530ad08996SDavid Disseldorp 	target_free_sgl(xop->xop_data_sg, xop->xop_data_nents);
754cbf031f4SNicholas Bellinger 	kfree(xop);
755cbf031f4SNicholas Bellinger 
756689d94ecSAnastasia Kovaleva 	pr_debug("%s: Final src_lba: %llu, dst_lba: %llu\n", __func__,
757cbf031f4SNicholas Bellinger 		(unsigned long long)src_lba, (unsigned long long)dst_lba);
758689d94ecSAnastasia Kovaleva 	pr_debug("%s: Blocks copied: %u, Bytes Copied: %u\n", __func__,
759689d94ecSAnastasia Kovaleva 		copied_bytes / dst_dev->dev_attrib.block_size, copied_bytes);
760cbf031f4SNicholas Bellinger 
761689d94ecSAnastasia Kovaleva 	pr_debug("%s: Setting X-COPY GOOD status -> sending response\n", __func__);
762cbf031f4SNicholas Bellinger 	target_complete_cmd(ec_cmd, SAM_STAT_GOOD);
763cbf031f4SNicholas Bellinger 	return;
764cbf031f4SNicholas Bellinger 
765cbf031f4SNicholas Bellinger out:
7660394b504SSergey Samoylenko 	/*
7670394b504SSergey Samoylenko 	 * The XCOPY command was aborted after some data was transferred.
7680394b504SSergey Samoylenko 	 * Terminate command with CHECK CONDITION status, with the sense key
7690394b504SSergey Samoylenko 	 * set to COPY ABORTED.
7700394b504SSergey Samoylenko 	 */
7710394b504SSergey Samoylenko 	sense_rc = TCM_COPY_TARGET_DEVICE_NOT_REACHABLE;
772cbf031f4SNicholas Bellinger 	xcopy_pt_undepend_remotedev(xop);
7730ad08996SDavid Disseldorp 	target_free_sgl(xop->xop_data_sg, xop->xop_data_nents);
774d877d727SBart Van Assche 
775d877d727SBart Van Assche err_free:
776cbf031f4SNicholas Bellinger 	kfree(xop);
777689d94ecSAnastasia Kovaleva 	pr_warn_ratelimited("%s: rc: %d, sense: %u, XCOPY operation failed\n",
778689d94ecSAnastasia Kovaleva 			   __func__, rc, sense_rc);
7790394b504SSergey Samoylenko 	target_complete_cmd_with_sense(ec_cmd, SAM_STAT_CHECK_CONDITION, sense_rc);
780cbf031f4SNicholas Bellinger }
781cbf031f4SNicholas Bellinger 
782d877d727SBart Van Assche /*
783d877d727SBart Van Assche  * Returns TCM_NO_SENSE upon success or a sense code != TCM_NO_SENSE if parsing
784d877d727SBart Van Assche  * fails.
785d877d727SBart Van Assche  */
target_parse_xcopy_cmd(struct xcopy_op * xop)786d877d727SBart Van Assche static sense_reason_t target_parse_xcopy_cmd(struct xcopy_op *xop)
787cbf031f4SNicholas Bellinger {
788d877d727SBart Van Assche 	struct se_cmd *se_cmd = xop->xop_se_cmd;
789cbf031f4SNicholas Bellinger 	unsigned char *p = NULL, *seg_desc;
790d877d727SBart Van Assche 	unsigned int list_id, list_id_usage, sdll, inline_dl;
79148502ddbSNicholas Bellinger 	sense_reason_t ret = TCM_INVALID_PARAMETER_LIST;
792cbf031f4SNicholas Bellinger 	int rc;
793cbf031f4SNicholas Bellinger 	unsigned short tdll;
794cbf031f4SNicholas Bellinger 
795cbf031f4SNicholas Bellinger 	p = transport_kmap_data_sg(se_cmd);
796cbf031f4SNicholas Bellinger 	if (!p) {
797cbf031f4SNicholas Bellinger 		pr_err("transport_kmap_data_sg() failed in target_do_xcopy\n");
798cbf031f4SNicholas Bellinger 		return TCM_OUT_OF_RESOURCES;
799cbf031f4SNicholas Bellinger 	}
800cbf031f4SNicholas Bellinger 
801cbf031f4SNicholas Bellinger 	list_id = p[0];
8023f7a46c6SNicholas Bellinger 	list_id_usage = (p[1] & 0x18) >> 3;
8033f7a46c6SNicholas Bellinger 
804cbf031f4SNicholas Bellinger 	/*
805cbf031f4SNicholas Bellinger 	 * Determine TARGET DESCRIPTOR LIST LENGTH + SEGMENT DESCRIPTOR LIST LENGTH
806cbf031f4SNicholas Bellinger 	 */
807cbf031f4SNicholas Bellinger 	tdll = get_unaligned_be16(&p[2]);
808cbf031f4SNicholas Bellinger 	sdll = get_unaligned_be32(&p[8]);
8097d387066SDavid Disseldorp 	if (tdll + sdll > RCR_OP_MAX_DESC_LIST_LEN) {
8107d387066SDavid Disseldorp 		pr_err("XCOPY descriptor list length %u exceeds maximum %u\n",
8117d387066SDavid Disseldorp 		       tdll + sdll, RCR_OP_MAX_DESC_LIST_LEN);
8127d387066SDavid Disseldorp 		ret = TCM_PARAMETER_LIST_LENGTH_ERROR;
8137d387066SDavid Disseldorp 		goto out;
8147d387066SDavid Disseldorp 	}
815cbf031f4SNicholas Bellinger 
816cbf031f4SNicholas Bellinger 	inline_dl = get_unaligned_be32(&p[12]);
817cbf031f4SNicholas Bellinger 	if (inline_dl != 0) {
818cbf031f4SNicholas Bellinger 		pr_err("XCOPY with non zero inline data length\n");
819cbf031f4SNicholas Bellinger 		goto out;
820cbf031f4SNicholas Bellinger 	}
821cbf031f4SNicholas Bellinger 
822f94fd098SDavid Disseldorp 	if (se_cmd->data_length < (XCOPY_HDR_LEN + tdll + sdll + inline_dl)) {
823f94fd098SDavid Disseldorp 		pr_err("XCOPY parameter truncation: data length %u too small "
824f94fd098SDavid Disseldorp 			"for tdll: %hu sdll: %u inline_dl: %u\n",
825f94fd098SDavid Disseldorp 			se_cmd->data_length, tdll, sdll, inline_dl);
826f94fd098SDavid Disseldorp 		ret = TCM_PARAMETER_LIST_LENGTH_ERROR;
827f94fd098SDavid Disseldorp 		goto out;
828f94fd098SDavid Disseldorp 	}
829f94fd098SDavid Disseldorp 
830cbf031f4SNicholas Bellinger 	pr_debug("Processing XCOPY with list_id: 0x%02x list_id_usage: 0x%02x"
831cbf031f4SNicholas Bellinger 		" tdll: %hu sdll: %u inline_dl: %u\n", list_id, list_id_usage,
832cbf031f4SNicholas Bellinger 		tdll, sdll, inline_dl);
833cbf031f4SNicholas Bellinger 
83466640d35SDavid Disseldorp 	/*
83566640d35SDavid Disseldorp 	 * skip over the target descriptors until segment descriptors
83666640d35SDavid Disseldorp 	 * have been passed - CSCD ids are needed to determine src and dest.
83766640d35SDavid Disseldorp 	 */
83866640d35SDavid Disseldorp 	seg_desc = &p[16] + tdll;
83966640d35SDavid Disseldorp 
8407e642ca0SLi Feng 	rc = target_xcopy_parse_segment_descriptors(xop, seg_desc, sdll, &ret);
84166640d35SDavid Disseldorp 	if (rc <= 0)
84266640d35SDavid Disseldorp 		goto out;
84366640d35SDavid Disseldorp 
84466640d35SDavid Disseldorp 	pr_debug("XCOPY: Processed %d segment descriptors, length: %u\n", rc,
84566640d35SDavid Disseldorp 				rc * XCOPY_SEGMENT_DESC_LEN);
84666640d35SDavid Disseldorp 
847449a1378SNicholas Bellinger 	rc = target_xcopy_parse_target_descriptors(se_cmd, xop, &p[16], tdll, &ret);
848cbf031f4SNicholas Bellinger 	if (rc <= 0)
849cbf031f4SNicholas Bellinger 		goto out;
850cbf031f4SNicholas Bellinger 
85148502ddbSNicholas Bellinger 	if (xop->src_dev->dev_attrib.block_size !=
85248502ddbSNicholas Bellinger 	    xop->dst_dev->dev_attrib.block_size) {
85348502ddbSNicholas Bellinger 		pr_err("XCOPY: Non matching src_dev block_size: %u + dst_dev"
85448502ddbSNicholas Bellinger 		       " block_size: %u currently unsupported\n",
85548502ddbSNicholas Bellinger 			xop->src_dev->dev_attrib.block_size,
85648502ddbSNicholas Bellinger 			xop->dst_dev->dev_attrib.block_size);
85748502ddbSNicholas Bellinger 		xcopy_pt_undepend_remotedev(xop);
85848502ddbSNicholas Bellinger 		ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
85948502ddbSNicholas Bellinger 		goto out;
86048502ddbSNicholas Bellinger 	}
86148502ddbSNicholas Bellinger 
862cbf031f4SNicholas Bellinger 	pr_debug("XCOPY: Processed %d target descriptors, length: %u\n", rc,
863cbf031f4SNicholas Bellinger 				rc * XCOPY_TARGET_DESC_LEN);
864cbf031f4SNicholas Bellinger 	transport_kunmap_data_sg(se_cmd);
865cbf031f4SNicholas Bellinger 	return TCM_NO_SENSE;
866cbf031f4SNicholas Bellinger 
867cbf031f4SNicholas Bellinger out:
868cbf031f4SNicholas Bellinger 	if (p)
869cbf031f4SNicholas Bellinger 		transport_kunmap_data_sg(se_cmd);
87048502ddbSNicholas Bellinger 	return ret;
871cbf031f4SNicholas Bellinger }
872cbf031f4SNicholas Bellinger 
target_do_xcopy(struct se_cmd * se_cmd)873d877d727SBart Van Assche sense_reason_t target_do_xcopy(struct se_cmd *se_cmd)
874d877d727SBart Van Assche {
875d877d727SBart Van Assche 	struct se_device *dev = se_cmd->se_dev;
876d877d727SBart Van Assche 	struct xcopy_op *xop;
877d877d727SBart Van Assche 	unsigned int sa;
878d877d727SBart Van Assche 
879d877d727SBart Van Assche 	if (!dev->dev_attrib.emulate_3pc) {
880d877d727SBart Van Assche 		pr_err("EXTENDED_COPY operation explicitly disabled\n");
881d877d727SBart Van Assche 		return TCM_UNSUPPORTED_SCSI_OPCODE;
882d877d727SBart Van Assche 	}
883d877d727SBart Van Assche 
884d877d727SBart Van Assche 	sa = se_cmd->t_task_cdb[1] & 0x1f;
885d877d727SBart Van Assche 	if (sa != 0x00) {
886d877d727SBart Van Assche 		pr_err("EXTENDED_COPY(LID4) not supported\n");
887d877d727SBart Van Assche 		return TCM_UNSUPPORTED_SCSI_OPCODE;
888d877d727SBart Van Assche 	}
889d877d727SBart Van Assche 
890d877d727SBart Van Assche 	if (se_cmd->data_length == 0) {
891d877d727SBart Van Assche 		target_complete_cmd(se_cmd, SAM_STAT_GOOD);
892d877d727SBart Van Assche 		return TCM_NO_SENSE;
893d877d727SBart Van Assche 	}
894d877d727SBart Van Assche 	if (se_cmd->data_length < XCOPY_HDR_LEN) {
895d877d727SBart Van Assche 		pr_err("XCOPY parameter truncation: length %u < hdr_len %u\n",
896d877d727SBart Van Assche 				se_cmd->data_length, XCOPY_HDR_LEN);
897d877d727SBart Van Assche 		return TCM_PARAMETER_LIST_LENGTH_ERROR;
898d877d727SBart Van Assche 	}
899d877d727SBart Van Assche 
900d877d727SBart Van Assche 	xop = kzalloc(sizeof(struct xcopy_op), GFP_KERNEL);
901d877d727SBart Van Assche 	if (!xop)
902d877d727SBart Van Assche 		goto err;
903d877d727SBart Van Assche 	xop->xop_se_cmd = se_cmd;
904d877d727SBart Van Assche 	INIT_WORK(&xop->xop_work, target_xcopy_do_work);
905d877d727SBart Van Assche 	if (WARN_ON_ONCE(!queue_work(xcopy_wq, &xop->xop_work)))
906d877d727SBart Van Assche 		goto free;
907d877d727SBart Van Assche 	return TCM_NO_SENSE;
908d877d727SBart Van Assche 
909d877d727SBart Van Assche free:
910d877d727SBart Van Assche 	kfree(xop);
911d877d727SBart Van Assche 
912d877d727SBart Van Assche err:
913d877d727SBart Van Assche 	return TCM_OUT_OF_RESOURCES;
914d877d727SBart Van Assche }
915d877d727SBart Van Assche 
target_rcr_operating_parameters(struct se_cmd * se_cmd)916cbf031f4SNicholas Bellinger static sense_reason_t target_rcr_operating_parameters(struct se_cmd *se_cmd)
917cbf031f4SNicholas Bellinger {
918cbf031f4SNicholas Bellinger 	unsigned char *p;
919cbf031f4SNicholas Bellinger 
920cbf031f4SNicholas Bellinger 	p = transport_kmap_data_sg(se_cmd);
921cbf031f4SNicholas Bellinger 	if (!p) {
922cbf031f4SNicholas Bellinger 		pr_err("transport_kmap_data_sg failed in"
923cbf031f4SNicholas Bellinger 		       " target_rcr_operating_parameters\n");
924cbf031f4SNicholas Bellinger 		return TCM_OUT_OF_RESOURCES;
925cbf031f4SNicholas Bellinger 	}
926cbf031f4SNicholas Bellinger 
927cbf031f4SNicholas Bellinger 	if (se_cmd->data_length < 54) {
928cbf031f4SNicholas Bellinger 		pr_err("Receive Copy Results Op Parameters length"
929cbf031f4SNicholas Bellinger 		       " too small: %u\n", se_cmd->data_length);
930cbf031f4SNicholas Bellinger 		transport_kunmap_data_sg(se_cmd);
931cbf031f4SNicholas Bellinger 		return TCM_INVALID_CDB_FIELD;
932cbf031f4SNicholas Bellinger 	}
933cbf031f4SNicholas Bellinger 	/*
934cbf031f4SNicholas Bellinger 	 * Set SNLID=1 (Supports no List ID)
935cbf031f4SNicholas Bellinger 	 */
936cbf031f4SNicholas Bellinger 	p[4] = 0x1;
937cbf031f4SNicholas Bellinger 	/*
938cbf031f4SNicholas Bellinger 	 * MAXIMUM TARGET DESCRIPTOR COUNT
939cbf031f4SNicholas Bellinger 	 */
940cbf031f4SNicholas Bellinger 	put_unaligned_be16(RCR_OP_MAX_TARGET_DESC_COUNT, &p[8]);
941cbf031f4SNicholas Bellinger 	/*
942cbf031f4SNicholas Bellinger 	 * MAXIMUM SEGMENT DESCRIPTOR COUNT
943cbf031f4SNicholas Bellinger 	 */
944cbf031f4SNicholas Bellinger 	put_unaligned_be16(RCR_OP_MAX_SG_DESC_COUNT, &p[10]);
945cbf031f4SNicholas Bellinger 	/*
946cbf031f4SNicholas Bellinger 	 * MAXIMUM DESCRIPTOR LIST LENGTH
947cbf031f4SNicholas Bellinger 	 */
948cbf031f4SNicholas Bellinger 	put_unaligned_be32(RCR_OP_MAX_DESC_LIST_LEN, &p[12]);
949cbf031f4SNicholas Bellinger 	/*
950cbf031f4SNicholas Bellinger 	 * MAXIMUM SEGMENT LENGTH
951cbf031f4SNicholas Bellinger 	 */
952cbf031f4SNicholas Bellinger 	put_unaligned_be32(RCR_OP_MAX_SEGMENT_LEN, &p[16]);
953cbf031f4SNicholas Bellinger 	/*
954cbf031f4SNicholas Bellinger 	 * MAXIMUM INLINE DATA LENGTH for SA 0x04 (NOT SUPPORTED)
955cbf031f4SNicholas Bellinger 	 */
956cbf031f4SNicholas Bellinger 	put_unaligned_be32(0x0, &p[20]);
957cbf031f4SNicholas Bellinger 	/*
958cbf031f4SNicholas Bellinger 	 * HELD DATA LIMIT
959cbf031f4SNicholas Bellinger 	 */
960cbf031f4SNicholas Bellinger 	put_unaligned_be32(0x0, &p[24]);
961cbf031f4SNicholas Bellinger 	/*
962cbf031f4SNicholas Bellinger 	 * MAXIMUM STREAM DEVICE TRANSFER SIZE
963cbf031f4SNicholas Bellinger 	 */
964cbf031f4SNicholas Bellinger 	put_unaligned_be32(0x0, &p[28]);
965cbf031f4SNicholas Bellinger 	/*
966cbf031f4SNicholas Bellinger 	 * TOTAL CONCURRENT COPIES
967cbf031f4SNicholas Bellinger 	 */
968cbf031f4SNicholas Bellinger 	put_unaligned_be16(RCR_OP_TOTAL_CONCURR_COPIES, &p[34]);
969cbf031f4SNicholas Bellinger 	/*
970cbf031f4SNicholas Bellinger 	 * MAXIMUM CONCURRENT COPIES
971cbf031f4SNicholas Bellinger 	 */
972cbf031f4SNicholas Bellinger 	p[36] = RCR_OP_MAX_CONCURR_COPIES;
973cbf031f4SNicholas Bellinger 	/*
974cbf031f4SNicholas Bellinger 	 * DATA SEGMENT GRANULARITY (log 2)
975cbf031f4SNicholas Bellinger 	 */
976cbf031f4SNicholas Bellinger 	p[37] = RCR_OP_DATA_SEG_GRAN_LOG2;
977cbf031f4SNicholas Bellinger 	/*
978cbf031f4SNicholas Bellinger 	 * INLINE DATA GRANULARITY log 2)
979cbf031f4SNicholas Bellinger 	 */
980cbf031f4SNicholas Bellinger 	p[38] = RCR_OP_INLINE_DATA_GRAN_LOG2;
981cbf031f4SNicholas Bellinger 	/*
982cbf031f4SNicholas Bellinger 	 * HELD DATA GRANULARITY
983cbf031f4SNicholas Bellinger 	 */
984cbf031f4SNicholas Bellinger 	p[39] = RCR_OP_HELD_DATA_GRAN_LOG2;
985cbf031f4SNicholas Bellinger 	/*
986cbf031f4SNicholas Bellinger 	 * IMPLEMENTED DESCRIPTOR LIST LENGTH
987cbf031f4SNicholas Bellinger 	 */
988cbf031f4SNicholas Bellinger 	p[43] = 0x2;
989cbf031f4SNicholas Bellinger 	/*
990cbf031f4SNicholas Bellinger 	 * List of implemented descriptor type codes (ordered)
991cbf031f4SNicholas Bellinger 	 */
992cbf031f4SNicholas Bellinger 	p[44] = 0x02; /* Copy Block to Block device */
993cbf031f4SNicholas Bellinger 	p[45] = 0xe4; /* Identification descriptor target descriptor */
994cbf031f4SNicholas Bellinger 
995cbf031f4SNicholas Bellinger 	/*
996cbf031f4SNicholas Bellinger 	 * AVAILABLE DATA (n-3)
997cbf031f4SNicholas Bellinger 	 */
998cbf031f4SNicholas Bellinger 	put_unaligned_be32(42, &p[0]);
999cbf031f4SNicholas Bellinger 
1000cbf031f4SNicholas Bellinger 	transport_kunmap_data_sg(se_cmd);
100114b40c1eSHannes Reinecke 	target_complete_cmd(se_cmd, SAM_STAT_GOOD);
1002cbf031f4SNicholas Bellinger 
1003cbf031f4SNicholas Bellinger 	return TCM_NO_SENSE;
1004cbf031f4SNicholas Bellinger }
1005cbf031f4SNicholas Bellinger 
target_do_receive_copy_results(struct se_cmd * se_cmd)1006cbf031f4SNicholas Bellinger sense_reason_t target_do_receive_copy_results(struct se_cmd *se_cmd)
1007cbf031f4SNicholas Bellinger {
1008cbf031f4SNicholas Bellinger 	unsigned char *cdb = &se_cmd->t_task_cdb[0];
1009cbf031f4SNicholas Bellinger 	int sa = (cdb[1] & 0x1f), list_id = cdb[2];
1010b8908e5eSDmitry Bogdanov 	struct se_device *dev = se_cmd->se_dev;
1011cbf031f4SNicholas Bellinger 	sense_reason_t rc = TCM_NO_SENSE;
1012cbf031f4SNicholas Bellinger 
1013b8908e5eSDmitry Bogdanov 	if (!dev->dev_attrib.emulate_3pc) {
1014b8908e5eSDmitry Bogdanov 		pr_debug("Third-party copy operations explicitly disabled\n");
1015b8908e5eSDmitry Bogdanov 		return TCM_UNSUPPORTED_SCSI_OPCODE;
1016b8908e5eSDmitry Bogdanov 	}
1017b8908e5eSDmitry Bogdanov 
1018cbf031f4SNicholas Bellinger 	pr_debug("Entering target_do_receive_copy_results: SA: 0x%02x, List ID:"
1019cbf031f4SNicholas Bellinger 		" 0x%02x, AL: %u\n", sa, list_id, se_cmd->data_length);
1020cbf031f4SNicholas Bellinger 
1021cbf031f4SNicholas Bellinger 	if (list_id != 0) {
1022cbf031f4SNicholas Bellinger 		pr_err("Receive Copy Results with non zero list identifier"
1023cbf031f4SNicholas Bellinger 		       " not supported\n");
1024cbf031f4SNicholas Bellinger 		return TCM_INVALID_CDB_FIELD;
1025cbf031f4SNicholas Bellinger 	}
1026cbf031f4SNicholas Bellinger 
1027cbf031f4SNicholas Bellinger 	switch (sa) {
1028cbf031f4SNicholas Bellinger 	case RCR_SA_OPERATING_PARAMETERS:
1029cbf031f4SNicholas Bellinger 		rc = target_rcr_operating_parameters(se_cmd);
1030cbf031f4SNicholas Bellinger 		break;
1031cbf031f4SNicholas Bellinger 	case RCR_SA_COPY_STATUS:
1032cbf031f4SNicholas Bellinger 	case RCR_SA_RECEIVE_DATA:
1033cbf031f4SNicholas Bellinger 	case RCR_SA_FAILED_SEGMENT_DETAILS:
1034cbf031f4SNicholas Bellinger 	default:
1035cbf031f4SNicholas Bellinger 		pr_err("Unsupported SA for receive copy results: 0x%02x\n", sa);
1036cbf031f4SNicholas Bellinger 		return TCM_INVALID_CDB_FIELD;
1037cbf031f4SNicholas Bellinger 	}
1038cbf031f4SNicholas Bellinger 
1039cbf031f4SNicholas Bellinger 	return rc;
1040cbf031f4SNicholas Bellinger }
1041