xref: /freebsd/sys/cam/ctl/ctl_tpc.c (revision 2ffd30f7ee15c87ee092cbac6a4438bcb3af923c)
1984a2ea9SAlexander Motin /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3f24882ecSPedro F. Giffuni  *
4b06771aaSAlexander Motin  * Copyright (c) 2014-2021 Alexander Motin <mav@FreeBSD.org>
5984a2ea9SAlexander Motin  * All rights reserved.
6984a2ea9SAlexander Motin  *
7984a2ea9SAlexander Motin  * Redistribution and use in source and binary forms, with or without
8984a2ea9SAlexander Motin  * modification, are permitted provided that the following conditions
9984a2ea9SAlexander Motin  * are met:
10984a2ea9SAlexander Motin  * 1. Redistributions of source code must retain the above copyright
11984a2ea9SAlexander Motin  *    notice, this list of conditions and the following disclaimer,
12984a2ea9SAlexander Motin  *    without modification, immediately at the beginning of the file.
13984a2ea9SAlexander Motin  * 2. Redistributions in binary form must reproduce the above copyright
14984a2ea9SAlexander Motin  *    notice, this list of conditions and the following disclaimer in the
15984a2ea9SAlexander Motin  *    documentation and/or other materials provided with the distribution.
16984a2ea9SAlexander Motin  *
17984a2ea9SAlexander Motin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18984a2ea9SAlexander Motin  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19984a2ea9SAlexander Motin  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20984a2ea9SAlexander Motin  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21984a2ea9SAlexander Motin  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22984a2ea9SAlexander Motin  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23984a2ea9SAlexander Motin  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24984a2ea9SAlexander Motin  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25984a2ea9SAlexander Motin  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26984a2ea9SAlexander Motin  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27984a2ea9SAlexander Motin  */
28984a2ea9SAlexander Motin 
29984a2ea9SAlexander Motin #include <sys/param.h>
30984a2ea9SAlexander Motin #include <sys/systm.h>
31984a2ea9SAlexander Motin #include <sys/kernel.h>
32984a2ea9SAlexander Motin #include <sys/types.h>
33984a2ea9SAlexander Motin #include <sys/lock.h>
34984a2ea9SAlexander Motin #include <sys/module.h>
35984a2ea9SAlexander Motin #include <sys/mutex.h>
36984a2ea9SAlexander Motin #include <sys/condvar.h>
37984a2ea9SAlexander Motin #include <sys/malloc.h>
38984a2ea9SAlexander Motin #include <sys/conf.h>
39984a2ea9SAlexander Motin #include <sys/queue.h>
40984a2ea9SAlexander Motin #include <sys/sysctl.h>
418951f055SMarcelo Araujo #include <sys/nv.h>
428951f055SMarcelo Araujo #include <sys/dnv.h>
43984a2ea9SAlexander Motin #include <machine/atomic.h>
44984a2ea9SAlexander Motin 
45984a2ea9SAlexander Motin #include <cam/cam.h>
46984a2ea9SAlexander Motin #include <cam/scsi/scsi_all.h>
47984a2ea9SAlexander Motin #include <cam/scsi/scsi_da.h>
48984a2ea9SAlexander Motin #include <cam/ctl/ctl_io.h>
49984a2ea9SAlexander Motin #include <cam/ctl/ctl.h>
50984a2ea9SAlexander Motin #include <cam/ctl/ctl_frontend.h>
51984a2ea9SAlexander Motin #include <cam/ctl/ctl_util.h>
52984a2ea9SAlexander Motin #include <cam/ctl/ctl_backend.h>
53984a2ea9SAlexander Motin #include <cam/ctl/ctl_ioctl.h>
54984a2ea9SAlexander Motin #include <cam/ctl/ctl_ha.h>
55984a2ea9SAlexander Motin #include <cam/ctl/ctl_private.h>
56984a2ea9SAlexander Motin #include <cam/ctl/ctl_debug.h>
57984a2ea9SAlexander Motin #include <cam/ctl/ctl_scsi_all.h>
58984a2ea9SAlexander Motin #include <cam/ctl/ctl_tpc.h>
59984a2ea9SAlexander Motin #include <cam/ctl/ctl_error.h>
60984a2ea9SAlexander Motin 
61984a2ea9SAlexander Motin #define	TPC_MAX_CSCDS	64
62984a2ea9SAlexander Motin #define	TPC_MAX_SEGS	64
63984a2ea9SAlexander Motin #define	TPC_MAX_SEG	0
64984a2ea9SAlexander Motin #define	TPC_MAX_LIST	8192
65984a2ea9SAlexander Motin #define	TPC_MAX_INLINE	0
66984a2ea9SAlexander Motin #define	TPC_MAX_LISTS	255
67b06771aaSAlexander Motin #define	TPC_MAX_IO_SIZE	(8 * MIN(1024 * 1024, MAX(128 * 1024, maxphys)))
68b06771aaSAlexander Motin #define	TPC_MAX_IOCHUNK_SIZE	(TPC_MAX_IO_SIZE * 4)
6925eee848SAlexander Motin #define	TPC_MIN_TOKEN_TIMEOUT	1
7025eee848SAlexander Motin #define	TPC_DFL_TOKEN_TIMEOUT	60
7125eee848SAlexander Motin #define	TPC_MAX_TOKEN_TIMEOUT	600
72984a2ea9SAlexander Motin 
73984a2ea9SAlexander Motin MALLOC_DEFINE(M_CTL_TPC, "ctltpc", "CTL TPC");
74984a2ea9SAlexander Motin 
75984a2ea9SAlexander Motin typedef enum {
76984a2ea9SAlexander Motin 	TPC_ERR_RETRY		= 0x000,
77984a2ea9SAlexander Motin 	TPC_ERR_FAIL		= 0x001,
78984a2ea9SAlexander Motin 	TPC_ERR_MASK		= 0x0ff,
79984a2ea9SAlexander Motin 	TPC_ERR_NO_DECREMENT	= 0x100
80984a2ea9SAlexander Motin } tpc_error_action;
81984a2ea9SAlexander Motin 
82984a2ea9SAlexander Motin struct tpc_list;
83984a2ea9SAlexander Motin TAILQ_HEAD(runl, tpc_io);
84984a2ea9SAlexander Motin struct tpc_io {
85984a2ea9SAlexander Motin 	union ctl_io		*io;
86a3dd8378SAlexander Motin 	uint8_t			 target;
87a3dd8378SAlexander Motin 	uint32_t		 cscd;
88984a2ea9SAlexander Motin 	uint64_t		 lun;
8946511441SAlexander Motin 	uint8_t			*buf;
90984a2ea9SAlexander Motin 	struct tpc_list		*list;
91984a2ea9SAlexander Motin 	struct runl		 run;
92984a2ea9SAlexander Motin 	TAILQ_ENTRY(tpc_io)	 rlinks;
93984a2ea9SAlexander Motin 	TAILQ_ENTRY(tpc_io)	 links;
94984a2ea9SAlexander Motin };
95984a2ea9SAlexander Motin 
9625eee848SAlexander Motin struct tpc_token {
9725eee848SAlexander Motin 	uint8_t			 token[512];
9825eee848SAlexander Motin 	uint64_t		 lun;
9925eee848SAlexander Motin 	uint32_t		 blocksize;
10025eee848SAlexander Motin 	uint8_t			*params;
10125eee848SAlexander Motin 	struct scsi_range_desc	*range;
10225eee848SAlexander Motin 	int			 nrange;
10325eee848SAlexander Motin 	int			 active;
10425eee848SAlexander Motin 	time_t			 last_active;
10525eee848SAlexander Motin 	uint32_t		 timeout;
10625eee848SAlexander Motin 	TAILQ_ENTRY(tpc_token)	 links;
10725eee848SAlexander Motin };
10825eee848SAlexander Motin 
109984a2ea9SAlexander Motin struct tpc_list {
110984a2ea9SAlexander Motin 	uint8_t			 service_action;
111984a2ea9SAlexander Motin 	int			 init_port;
1128cbf9eaeSAlexander Motin 	uint32_t		 init_idx;
113984a2ea9SAlexander Motin 	uint32_t		 list_id;
114984a2ea9SAlexander Motin 	uint8_t			 flags;
115984a2ea9SAlexander Motin 	uint8_t			*params;
116984a2ea9SAlexander Motin 	struct scsi_ec_cscd	*cscd;
117984a2ea9SAlexander Motin 	struct scsi_ec_segment	*seg[TPC_MAX_SEGS];
118984a2ea9SAlexander Motin 	uint8_t			*inl;
119984a2ea9SAlexander Motin 	int			 ncscd;
120984a2ea9SAlexander Motin 	int			 nseg;
121984a2ea9SAlexander Motin 	int			 leninl;
12225eee848SAlexander Motin 	struct tpc_token	*token;
12325eee848SAlexander Motin 	struct scsi_range_desc	*range;
12425eee848SAlexander Motin 	int			 nrange;
12525eee848SAlexander Motin 	off_t			 offset_into_rod;
12625eee848SAlexander Motin 
127984a2ea9SAlexander Motin 	int			 curseg;
12825eee848SAlexander Motin 	off_t			 cursectors;
129984a2ea9SAlexander Motin 	off_t			 curbytes;
130984a2ea9SAlexander Motin 	int			 curops;
131984a2ea9SAlexander Motin 	int			 stage;
13225eee848SAlexander Motin 	off_t			 segsectors;
13325eee848SAlexander Motin 	off_t			 segbytes;
134984a2ea9SAlexander Motin 	int			 tbdio;
135984a2ea9SAlexander Motin 	int			 error;
136984a2ea9SAlexander Motin 	int			 abort;
137984a2ea9SAlexander Motin 	int			 completed;
13825eee848SAlexander Motin 	time_t			 last_active;
139984a2ea9SAlexander Motin 	TAILQ_HEAD(, tpc_io)	 allio;
140a3dd8378SAlexander Motin 	struct scsi_sense_data	 fwd_sense_data;
141a3dd8378SAlexander Motin 	uint8_t			 fwd_sense_len;
142a3dd8378SAlexander Motin 	uint8_t			 fwd_scsi_status;
143a3dd8378SAlexander Motin 	uint8_t			 fwd_target;
144a3dd8378SAlexander Motin 	uint16_t		 fwd_cscd;
145984a2ea9SAlexander Motin 	struct scsi_sense_data	 sense_data;
146984a2ea9SAlexander Motin 	uint8_t			 sense_len;
147984a2ea9SAlexander Motin 	uint8_t			 scsi_status;
148984a2ea9SAlexander Motin 	struct ctl_scsiio	*ctsio;
149984a2ea9SAlexander Motin 	struct ctl_lun		*lun;
15025eee848SAlexander Motin 	int			 res_token_valid;
15125eee848SAlexander Motin 	uint8_t			 res_token[512];
152984a2ea9SAlexander Motin 	TAILQ_ENTRY(tpc_list)	 links;
153984a2ea9SAlexander Motin };
154984a2ea9SAlexander Motin 
15525eee848SAlexander Motin static void
tpc_timeout(void * arg)15625eee848SAlexander Motin tpc_timeout(void *arg)
15725eee848SAlexander Motin {
15825eee848SAlexander Motin 	struct ctl_softc *softc = arg;
15925eee848SAlexander Motin 	struct ctl_lun *lun;
16025eee848SAlexander Motin 	struct tpc_token *token, *ttoken;
16125eee848SAlexander Motin 	struct tpc_list *list, *tlist;
16225eee848SAlexander Motin 
16325eee848SAlexander Motin 	/* Free completed lists with expired timeout. */
16425eee848SAlexander Motin 	STAILQ_FOREACH(lun, &softc->lun_list, links) {
16525eee848SAlexander Motin 		mtx_lock(&lun->lun_lock);
16625eee848SAlexander Motin 		TAILQ_FOREACH_SAFE(list, &lun->tpc_lists, links, tlist) {
16725eee848SAlexander Motin 			if (!list->completed || time_uptime < list->last_active +
16825eee848SAlexander Motin 			    TPC_DFL_TOKEN_TIMEOUT)
16925eee848SAlexander Motin 				continue;
17025eee848SAlexander Motin 			TAILQ_REMOVE(&lun->tpc_lists, list, links);
17125eee848SAlexander Motin 			free(list, M_CTL);
17225eee848SAlexander Motin 		}
17325eee848SAlexander Motin 		mtx_unlock(&lun->lun_lock);
17425eee848SAlexander Motin 	}
17525eee848SAlexander Motin 
17625eee848SAlexander Motin 	/* Free inactive ROD tokens with expired timeout. */
1772d8b2876SAlexander Motin 	mtx_lock(&softc->tpc_lock);
17825eee848SAlexander Motin 	TAILQ_FOREACH_SAFE(token, &softc->tpc_tokens, links, ttoken) {
17925eee848SAlexander Motin 		if (token->active ||
18025eee848SAlexander Motin 		    time_uptime < token->last_active + token->timeout + 1)
18125eee848SAlexander Motin 			continue;
18225eee848SAlexander Motin 		TAILQ_REMOVE(&softc->tpc_tokens, token, links);
18325eee848SAlexander Motin 		free(token->params, M_CTL);
18425eee848SAlexander Motin 		free(token, M_CTL);
18525eee848SAlexander Motin 	}
1862d8b2876SAlexander Motin 	mtx_unlock(&softc->tpc_lock);
187f4d499fdSAlexander Motin 	callout_schedule_sbt(&softc->tpc_timeout, SBT_1S, SBT_1S, 0);
18825eee848SAlexander Motin }
18925eee848SAlexander Motin 
190984a2ea9SAlexander Motin void
ctl_tpc_init(struct ctl_softc * softc)19125eee848SAlexander Motin ctl_tpc_init(struct ctl_softc *softc)
19225eee848SAlexander Motin {
19325eee848SAlexander Motin 
1942d8b2876SAlexander Motin 	mtx_init(&softc->tpc_lock, "CTL TPC mutex", NULL, MTX_DEF);
19525eee848SAlexander Motin 	TAILQ_INIT(&softc->tpc_tokens);
19625eee848SAlexander Motin 	callout_init_mtx(&softc->tpc_timeout, &softc->ctl_lock, 0);
197f4d499fdSAlexander Motin 	callout_reset_sbt(&softc->tpc_timeout, SBT_1S, SBT_1S,
198f4d499fdSAlexander Motin 	    tpc_timeout, softc, 0);
19925eee848SAlexander Motin }
20025eee848SAlexander Motin 
20125eee848SAlexander Motin void
ctl_tpc_shutdown(struct ctl_softc * softc)20225eee848SAlexander Motin ctl_tpc_shutdown(struct ctl_softc *softc)
20325eee848SAlexander Motin {
20425eee848SAlexander Motin 	struct tpc_token *token;
20525eee848SAlexander Motin 
20625eee848SAlexander Motin 	callout_drain(&softc->tpc_timeout);
20725eee848SAlexander Motin 
20825eee848SAlexander Motin 	/* Free ROD tokens. */
2092d8b2876SAlexander Motin 	mtx_lock(&softc->tpc_lock);
21025eee848SAlexander Motin 	while ((token = TAILQ_FIRST(&softc->tpc_tokens)) != NULL) {
21125eee848SAlexander Motin 		TAILQ_REMOVE(&softc->tpc_tokens, token, links);
21225eee848SAlexander Motin 		free(token->params, M_CTL);
21325eee848SAlexander Motin 		free(token, M_CTL);
21425eee848SAlexander Motin 	}
2152d8b2876SAlexander Motin 	mtx_unlock(&softc->tpc_lock);
2162d8b2876SAlexander Motin 	mtx_destroy(&softc->tpc_lock);
21725eee848SAlexander Motin }
21825eee848SAlexander Motin 
21925eee848SAlexander Motin void
ctl_tpc_lun_init(struct ctl_lun * lun)22025eee848SAlexander Motin ctl_tpc_lun_init(struct ctl_lun *lun)
221984a2ea9SAlexander Motin {
222984a2ea9SAlexander Motin 
223984a2ea9SAlexander Motin 	TAILQ_INIT(&lun->tpc_lists);
224984a2ea9SAlexander Motin }
225984a2ea9SAlexander Motin 
226984a2ea9SAlexander Motin void
ctl_tpc_lun_clear(struct ctl_lun * lun,uint32_t initidx)2279ff948d0SAlexander Motin ctl_tpc_lun_clear(struct ctl_lun *lun, uint32_t initidx)
2289ff948d0SAlexander Motin {
2299ff948d0SAlexander Motin 	struct tpc_list *list, *tlist;
2309ff948d0SAlexander Motin 
2319ff948d0SAlexander Motin 	TAILQ_FOREACH_SAFE(list, &lun->tpc_lists, links, tlist) {
2329ff948d0SAlexander Motin 		if (initidx != -1 && list->init_idx != initidx)
2339ff948d0SAlexander Motin 			continue;
2349ff948d0SAlexander Motin 		if (!list->completed)
2359ff948d0SAlexander Motin 			continue;
2369ff948d0SAlexander Motin 		TAILQ_REMOVE(&lun->tpc_lists, list, links);
2379ff948d0SAlexander Motin 		free(list, M_CTL);
2389ff948d0SAlexander Motin 	}
2399ff948d0SAlexander Motin }
2409ff948d0SAlexander Motin 
2419ff948d0SAlexander Motin void
ctl_tpc_lun_shutdown(struct ctl_lun * lun)24225eee848SAlexander Motin ctl_tpc_lun_shutdown(struct ctl_lun *lun)
243984a2ea9SAlexander Motin {
2449602f436SAlexander Motin 	struct ctl_softc *softc = lun->ctl_softc;
245984a2ea9SAlexander Motin 	struct tpc_list *list;
24625eee848SAlexander Motin 	struct tpc_token *token, *ttoken;
247984a2ea9SAlexander Motin 
24825eee848SAlexander Motin 	/* Free lists for this LUN. */
249984a2ea9SAlexander Motin 	while ((list = TAILQ_FIRST(&lun->tpc_lists)) != NULL) {
250984a2ea9SAlexander Motin 		TAILQ_REMOVE(&lun->tpc_lists, list, links);
251984a2ea9SAlexander Motin 		KASSERT(list->completed,
252984a2ea9SAlexander Motin 		    ("Not completed TPC (%p) on shutdown", list));
253984a2ea9SAlexander Motin 		free(list, M_CTL);
254984a2ea9SAlexander Motin 	}
25525eee848SAlexander Motin 
25625eee848SAlexander Motin 	/* Free ROD tokens for this LUN. */
2572d8b2876SAlexander Motin 	mtx_lock(&softc->tpc_lock);
2589602f436SAlexander Motin 	TAILQ_FOREACH_SAFE(token, &softc->tpc_tokens, links, ttoken) {
25925eee848SAlexander Motin 		if (token->lun != lun->lun || token->active)
26025eee848SAlexander Motin 			continue;
2619602f436SAlexander Motin 		TAILQ_REMOVE(&softc->tpc_tokens, token, links);
26225eee848SAlexander Motin 		free(token->params, M_CTL);
26325eee848SAlexander Motin 		free(token, M_CTL);
26425eee848SAlexander Motin 	}
2652d8b2876SAlexander Motin 	mtx_unlock(&softc->tpc_lock);
266984a2ea9SAlexander Motin }
267984a2ea9SAlexander Motin 
268984a2ea9SAlexander Motin int
ctl_inquiry_evpd_tpc(struct ctl_scsiio * ctsio,int alloc_len)269984a2ea9SAlexander Motin ctl_inquiry_evpd_tpc(struct ctl_scsiio *ctsio, int alloc_len)
270984a2ea9SAlexander Motin {
2719cbbfd2fSAlexander Motin 	struct ctl_lun *lun = CTL_LUN(ctsio);
272984a2ea9SAlexander Motin 	struct scsi_vpd_tpc *tpc_ptr;
273984a2ea9SAlexander Motin 	struct scsi_vpd_tpc_descriptor *d_ptr;
27425eee848SAlexander Motin 	struct scsi_vpd_tpc_descriptor_bdrl *bdrl_ptr;
275984a2ea9SAlexander Motin 	struct scsi_vpd_tpc_descriptor_sc *sc_ptr;
276984a2ea9SAlexander Motin 	struct scsi_vpd_tpc_descriptor_sc_descr *scd_ptr;
277984a2ea9SAlexander Motin 	struct scsi_vpd_tpc_descriptor_pd *pd_ptr;
278984a2ea9SAlexander Motin 	struct scsi_vpd_tpc_descriptor_sd *sd_ptr;
279984a2ea9SAlexander Motin 	struct scsi_vpd_tpc_descriptor_sdid *sdid_ptr;
28025eee848SAlexander Motin 	struct scsi_vpd_tpc_descriptor_rtf *rtf_ptr;
28125eee848SAlexander Motin 	struct scsi_vpd_tpc_descriptor_rtf_block *rtfb_ptr;
28225eee848SAlexander Motin 	struct scsi_vpd_tpc_descriptor_srt *srt_ptr;
28325eee848SAlexander Motin 	struct scsi_vpd_tpc_descriptor_srtd *srtd_ptr;
284984a2ea9SAlexander Motin 	struct scsi_vpd_tpc_descriptor_gco *gco_ptr;
285984a2ea9SAlexander Motin 	int data_len;
286984a2ea9SAlexander Motin 
287984a2ea9SAlexander Motin 	data_len = sizeof(struct scsi_vpd_tpc) +
28825eee848SAlexander Motin 	    sizeof(struct scsi_vpd_tpc_descriptor_bdrl) +
289984a2ea9SAlexander Motin 	    roundup2(sizeof(struct scsi_vpd_tpc_descriptor_sc) +
29025eee848SAlexander Motin 	     2 * sizeof(struct scsi_vpd_tpc_descriptor_sc_descr) + 11, 4) +
291984a2ea9SAlexander Motin 	    sizeof(struct scsi_vpd_tpc_descriptor_pd) +
292984a2ea9SAlexander Motin 	    roundup2(sizeof(struct scsi_vpd_tpc_descriptor_sd) + 4, 4) +
293984a2ea9SAlexander Motin 	    roundup2(sizeof(struct scsi_vpd_tpc_descriptor_sdid) + 2, 4) +
29425eee848SAlexander Motin 	    sizeof(struct scsi_vpd_tpc_descriptor_rtf) +
29525eee848SAlexander Motin 	     sizeof(struct scsi_vpd_tpc_descriptor_rtf_block) +
29625eee848SAlexander Motin 	    sizeof(struct scsi_vpd_tpc_descriptor_srt) +
29725eee848SAlexander Motin 	     2*sizeof(struct scsi_vpd_tpc_descriptor_srtd) +
298984a2ea9SAlexander Motin 	    sizeof(struct scsi_vpd_tpc_descriptor_gco);
299984a2ea9SAlexander Motin 
300984a2ea9SAlexander Motin 	ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
301984a2ea9SAlexander Motin 	tpc_ptr = (struct scsi_vpd_tpc *)ctsio->kern_data_ptr;
302984a2ea9SAlexander Motin 	ctsio->kern_rel_offset = 0;
303984a2ea9SAlexander Motin 	ctsio->kern_sg_entries = 0;
304640603fbSAlexander Motin 	ctsio->kern_data_len = min(data_len, alloc_len);
305640603fbSAlexander Motin 	ctsio->kern_total_len = ctsio->kern_data_len;
306984a2ea9SAlexander Motin 
307984a2ea9SAlexander Motin 	/*
308984a2ea9SAlexander Motin 	 * The control device is always connected.  The disk device, on the
309984a2ea9SAlexander Motin 	 * other hand, may not be online all the time.
310984a2ea9SAlexander Motin 	 */
311984a2ea9SAlexander Motin 	if (lun != NULL)
312984a2ea9SAlexander Motin 		tpc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
313984a2ea9SAlexander Motin 				     lun->be_lun->lun_type;
314984a2ea9SAlexander Motin 	else
315984a2ea9SAlexander Motin 		tpc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
316984a2ea9SAlexander Motin 	tpc_ptr->page_code = SVPD_SCSI_TPC;
317984a2ea9SAlexander Motin 	scsi_ulto2b(data_len - 4, tpc_ptr->page_length);
318984a2ea9SAlexander Motin 
31925eee848SAlexander Motin 	/* Block Device ROD Limits */
320984a2ea9SAlexander Motin 	d_ptr = (struct scsi_vpd_tpc_descriptor *)&tpc_ptr->descr[0];
32125eee848SAlexander Motin 	bdrl_ptr = (struct scsi_vpd_tpc_descriptor_bdrl *)d_ptr;
32225eee848SAlexander Motin 	scsi_ulto2b(SVPD_TPC_BDRL, bdrl_ptr->desc_type);
32325eee848SAlexander Motin 	scsi_ulto2b(sizeof(*bdrl_ptr) - 4, bdrl_ptr->desc_length);
32425eee848SAlexander Motin 	scsi_ulto2b(TPC_MAX_SEGS, bdrl_ptr->maximum_ranges);
32525eee848SAlexander Motin 	scsi_ulto4b(TPC_MAX_TOKEN_TIMEOUT,
32625eee848SAlexander Motin 	    bdrl_ptr->maximum_inactivity_timeout);
32725eee848SAlexander Motin 	scsi_ulto4b(TPC_DFL_TOKEN_TIMEOUT,
32825eee848SAlexander Motin 	    bdrl_ptr->default_inactivity_timeout);
32925eee848SAlexander Motin 	scsi_u64to8b(0, bdrl_ptr->maximum_token_transfer_size);
33025eee848SAlexander Motin 	scsi_u64to8b(0, bdrl_ptr->optimal_transfer_count);
33125eee848SAlexander Motin 
33225eee848SAlexander Motin 	/* Supported commands */
33325eee848SAlexander Motin 	d_ptr = (struct scsi_vpd_tpc_descriptor *)
33425eee848SAlexander Motin 	    (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
335984a2ea9SAlexander Motin 	sc_ptr = (struct scsi_vpd_tpc_descriptor_sc *)d_ptr;
336984a2ea9SAlexander Motin 	scsi_ulto2b(SVPD_TPC_SC, sc_ptr->desc_type);
33725eee848SAlexander Motin 	sc_ptr->list_length = 2 * sizeof(*scd_ptr) + 11;
338984a2ea9SAlexander Motin 	scsi_ulto2b(roundup2(1 + sc_ptr->list_length, 4), sc_ptr->desc_length);
339984a2ea9SAlexander Motin 	scd_ptr = &sc_ptr->descr[0];
340984a2ea9SAlexander Motin 	scd_ptr->opcode = EXTENDED_COPY;
34125eee848SAlexander Motin 	scd_ptr->sa_length = 5;
342984a2ea9SAlexander Motin 	scd_ptr->supported_service_actions[0] = EC_EC_LID1;
343984a2ea9SAlexander Motin 	scd_ptr->supported_service_actions[1] = EC_EC_LID4;
34425eee848SAlexander Motin 	scd_ptr->supported_service_actions[2] = EC_PT;
34525eee848SAlexander Motin 	scd_ptr->supported_service_actions[3] = EC_WUT;
34625eee848SAlexander Motin 	scd_ptr->supported_service_actions[4] = EC_COA;
347984a2ea9SAlexander Motin 	scd_ptr = (struct scsi_vpd_tpc_descriptor_sc_descr *)
348984a2ea9SAlexander Motin 	    &scd_ptr->supported_service_actions[scd_ptr->sa_length];
349984a2ea9SAlexander Motin 	scd_ptr->opcode = RECEIVE_COPY_STATUS;
35025eee848SAlexander Motin 	scd_ptr->sa_length = 6;
351984a2ea9SAlexander Motin 	scd_ptr->supported_service_actions[0] = RCS_RCS_LID1;
352984a2ea9SAlexander Motin 	scd_ptr->supported_service_actions[1] = RCS_RCFD;
353984a2ea9SAlexander Motin 	scd_ptr->supported_service_actions[2] = RCS_RCS_LID4;
354984a2ea9SAlexander Motin 	scd_ptr->supported_service_actions[3] = RCS_RCOP;
35525eee848SAlexander Motin 	scd_ptr->supported_service_actions[4] = RCS_RRTI;
35625eee848SAlexander Motin 	scd_ptr->supported_service_actions[5] = RCS_RART;
357984a2ea9SAlexander Motin 
358984a2ea9SAlexander Motin 	/* Parameter data. */
359984a2ea9SAlexander Motin 	d_ptr = (struct scsi_vpd_tpc_descriptor *)
360984a2ea9SAlexander Motin 	    (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
361984a2ea9SAlexander Motin 	pd_ptr = (struct scsi_vpd_tpc_descriptor_pd *)d_ptr;
362984a2ea9SAlexander Motin 	scsi_ulto2b(SVPD_TPC_PD, pd_ptr->desc_type);
363984a2ea9SAlexander Motin 	scsi_ulto2b(sizeof(*pd_ptr) - 4, pd_ptr->desc_length);
364984a2ea9SAlexander Motin 	scsi_ulto2b(TPC_MAX_CSCDS, pd_ptr->maximum_cscd_descriptor_count);
365984a2ea9SAlexander Motin 	scsi_ulto2b(TPC_MAX_SEGS, pd_ptr->maximum_segment_descriptor_count);
366984a2ea9SAlexander Motin 	scsi_ulto4b(TPC_MAX_LIST, pd_ptr->maximum_descriptor_list_length);
367984a2ea9SAlexander Motin 	scsi_ulto4b(TPC_MAX_INLINE, pd_ptr->maximum_inline_data_length);
368984a2ea9SAlexander Motin 
369984a2ea9SAlexander Motin 	/* Supported Descriptors */
370984a2ea9SAlexander Motin 	d_ptr = (struct scsi_vpd_tpc_descriptor *)
371984a2ea9SAlexander Motin 	    (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
372984a2ea9SAlexander Motin 	sd_ptr = (struct scsi_vpd_tpc_descriptor_sd *)d_ptr;
373984a2ea9SAlexander Motin 	scsi_ulto2b(SVPD_TPC_SD, sd_ptr->desc_type);
374984a2ea9SAlexander Motin 	scsi_ulto2b(roundup2(sizeof(*sd_ptr) - 4 + 4, 4), sd_ptr->desc_length);
375984a2ea9SAlexander Motin 	sd_ptr->list_length = 4;
376984a2ea9SAlexander Motin 	sd_ptr->supported_descriptor_codes[0] = EC_SEG_B2B;
377984a2ea9SAlexander Motin 	sd_ptr->supported_descriptor_codes[1] = EC_SEG_VERIFY;
378984a2ea9SAlexander Motin 	sd_ptr->supported_descriptor_codes[2] = EC_SEG_REGISTER_KEY;
379984a2ea9SAlexander Motin 	sd_ptr->supported_descriptor_codes[3] = EC_CSCD_ID;
380984a2ea9SAlexander Motin 
381984a2ea9SAlexander Motin 	/* Supported CSCD Descriptor IDs */
382984a2ea9SAlexander Motin 	d_ptr = (struct scsi_vpd_tpc_descriptor *)
383984a2ea9SAlexander Motin 	    (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
384984a2ea9SAlexander Motin 	sdid_ptr = (struct scsi_vpd_tpc_descriptor_sdid *)d_ptr;
385984a2ea9SAlexander Motin 	scsi_ulto2b(SVPD_TPC_SDID, sdid_ptr->desc_type);
386984a2ea9SAlexander Motin 	scsi_ulto2b(roundup2(sizeof(*sdid_ptr) - 4 + 2, 4), sdid_ptr->desc_length);
387984a2ea9SAlexander Motin 	scsi_ulto2b(2, sdid_ptr->list_length);
388984a2ea9SAlexander Motin 	scsi_ulto2b(0xffff, &sdid_ptr->supported_descriptor_ids[0]);
389984a2ea9SAlexander Motin 
39025eee848SAlexander Motin 	/* ROD Token Features */
39125eee848SAlexander Motin 	d_ptr = (struct scsi_vpd_tpc_descriptor *)
39225eee848SAlexander Motin 	    (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
39325eee848SAlexander Motin 	rtf_ptr = (struct scsi_vpd_tpc_descriptor_rtf *)d_ptr;
39425eee848SAlexander Motin 	scsi_ulto2b(SVPD_TPC_RTF, rtf_ptr->desc_type);
39525eee848SAlexander Motin 	scsi_ulto2b(sizeof(*rtf_ptr) - 4 + sizeof(*rtfb_ptr), rtf_ptr->desc_length);
39625eee848SAlexander Motin 	rtf_ptr->remote_tokens = 0;
39725eee848SAlexander Motin 	scsi_ulto4b(TPC_MIN_TOKEN_TIMEOUT, rtf_ptr->minimum_token_lifetime);
39825eee848SAlexander Motin 	scsi_ulto4b(UINT32_MAX, rtf_ptr->maximum_token_lifetime);
39925eee848SAlexander Motin 	scsi_ulto4b(TPC_MAX_TOKEN_TIMEOUT,
40025eee848SAlexander Motin 	    rtf_ptr->maximum_token_inactivity_timeout);
40125eee848SAlexander Motin 	scsi_ulto2b(sizeof(*rtfb_ptr), rtf_ptr->type_specific_features_length);
40225eee848SAlexander Motin 	rtfb_ptr = (struct scsi_vpd_tpc_descriptor_rtf_block *)
40325eee848SAlexander Motin 	    &rtf_ptr->type_specific_features;
40425eee848SAlexander Motin 	rtfb_ptr->type_format = SVPD_TPC_RTF_BLOCK;
40525eee848SAlexander Motin 	scsi_ulto2b(sizeof(*rtfb_ptr) - 4, rtfb_ptr->desc_length);
40625eee848SAlexander Motin 	scsi_ulto2b(0, rtfb_ptr->optimal_length_granularity);
40725eee848SAlexander Motin 	scsi_u64to8b(0, rtfb_ptr->maximum_bytes);
40825eee848SAlexander Motin 	scsi_u64to8b(0, rtfb_ptr->optimal_bytes);
409238b6b7cSAlexander Motin 	scsi_u64to8b(UINT64_MAX, rtfb_ptr->optimal_bytes_to_token_per_segment);
41025eee848SAlexander Motin 	scsi_u64to8b(TPC_MAX_IOCHUNK_SIZE,
41125eee848SAlexander Motin 	    rtfb_ptr->optimal_bytes_from_token_per_segment);
41225eee848SAlexander Motin 
41325eee848SAlexander Motin 	/* Supported ROD Tokens */
41425eee848SAlexander Motin 	d_ptr = (struct scsi_vpd_tpc_descriptor *)
41525eee848SAlexander Motin 	    (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
41625eee848SAlexander Motin 	srt_ptr = (struct scsi_vpd_tpc_descriptor_srt *)d_ptr;
41725eee848SAlexander Motin 	scsi_ulto2b(SVPD_TPC_SRT, srt_ptr->desc_type);
41825eee848SAlexander Motin 	scsi_ulto2b(sizeof(*srt_ptr) - 4 + 2*sizeof(*srtd_ptr), srt_ptr->desc_length);
41925eee848SAlexander Motin 	scsi_ulto2b(2*sizeof(*srtd_ptr), srt_ptr->rod_type_descriptors_length);
42025eee848SAlexander Motin 	srtd_ptr = (struct scsi_vpd_tpc_descriptor_srtd *)
42125eee848SAlexander Motin 	    &srt_ptr->rod_type_descriptors;
42225eee848SAlexander Motin 	scsi_ulto4b(ROD_TYPE_AUR, srtd_ptr->rod_type);
42325eee848SAlexander Motin 	srtd_ptr->flags = SVPD_TPC_SRTD_TIN | SVPD_TPC_SRTD_TOUT;
42425eee848SAlexander Motin 	scsi_ulto2b(0, srtd_ptr->preference_indicator);
42525eee848SAlexander Motin 	srtd_ptr++;
42625eee848SAlexander Motin 	scsi_ulto4b(ROD_TYPE_BLOCK_ZERO, srtd_ptr->rod_type);
42725eee848SAlexander Motin 	srtd_ptr->flags = SVPD_TPC_SRTD_TIN;
42825eee848SAlexander Motin 	scsi_ulto2b(0, srtd_ptr->preference_indicator);
42925eee848SAlexander Motin 
430984a2ea9SAlexander Motin 	/* General Copy Operations */
431984a2ea9SAlexander Motin 	d_ptr = (struct scsi_vpd_tpc_descriptor *)
432984a2ea9SAlexander Motin 	    (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
433984a2ea9SAlexander Motin 	gco_ptr = (struct scsi_vpd_tpc_descriptor_gco *)d_ptr;
434984a2ea9SAlexander Motin 	scsi_ulto2b(SVPD_TPC_GCO, gco_ptr->desc_type);
435984a2ea9SAlexander Motin 	scsi_ulto2b(sizeof(*gco_ptr) - 4, gco_ptr->desc_length);
436984a2ea9SAlexander Motin 	scsi_ulto4b(TPC_MAX_LISTS, gco_ptr->total_concurrent_copies);
437984a2ea9SAlexander Motin 	scsi_ulto4b(TPC_MAX_LISTS, gco_ptr->maximum_identified_concurrent_copies);
438984a2ea9SAlexander Motin 	scsi_ulto4b(TPC_MAX_SEG, gco_ptr->maximum_segment_length);
439984a2ea9SAlexander Motin 	gco_ptr->data_segment_granularity = 0;
440984a2ea9SAlexander Motin 	gco_ptr->inline_data_granularity = 0;
441984a2ea9SAlexander Motin 
442f7241cceSAlexander Motin 	ctl_set_success(ctsio);
443984a2ea9SAlexander Motin 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
444984a2ea9SAlexander Motin 	ctsio->be_move_done = ctl_config_move_done;
445984a2ea9SAlexander Motin 	ctl_datamove((union ctl_io *)ctsio);
446984a2ea9SAlexander Motin 
447984a2ea9SAlexander Motin 	return (CTL_RETVAL_COMPLETE);
448984a2ea9SAlexander Motin }
449984a2ea9SAlexander Motin 
450984a2ea9SAlexander Motin int
ctl_receive_copy_operating_parameters(struct ctl_scsiio * ctsio)451984a2ea9SAlexander Motin ctl_receive_copy_operating_parameters(struct ctl_scsiio *ctsio)
452984a2ea9SAlexander Motin {
453984a2ea9SAlexander Motin 	struct scsi_receive_copy_operating_parameters *cdb;
454984a2ea9SAlexander Motin 	struct scsi_receive_copy_operating_parameters_data *data;
455984a2ea9SAlexander Motin 	int retval;
456984a2ea9SAlexander Motin 	int alloc_len, total_len;
457984a2ea9SAlexander Motin 
458984a2ea9SAlexander Motin 	CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
459984a2ea9SAlexander Motin 
460984a2ea9SAlexander Motin 	cdb = (struct scsi_receive_copy_operating_parameters *)ctsio->cdb;
461984a2ea9SAlexander Motin 
462984a2ea9SAlexander Motin 	retval = CTL_RETVAL_COMPLETE;
463984a2ea9SAlexander Motin 
464984a2ea9SAlexander Motin 	total_len = sizeof(*data) + 4;
465984a2ea9SAlexander Motin 	alloc_len = scsi_4btoul(cdb->length);
466984a2ea9SAlexander Motin 
467984a2ea9SAlexander Motin 	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
468984a2ea9SAlexander Motin 	ctsio->kern_sg_entries = 0;
469984a2ea9SAlexander Motin 	ctsio->kern_rel_offset = 0;
470640603fbSAlexander Motin 	ctsio->kern_data_len = min(total_len, alloc_len);
471640603fbSAlexander Motin 	ctsio->kern_total_len = ctsio->kern_data_len;
472984a2ea9SAlexander Motin 
473984a2ea9SAlexander Motin 	data = (struct scsi_receive_copy_operating_parameters_data *)ctsio->kern_data_ptr;
474984a2ea9SAlexander Motin 	scsi_ulto4b(sizeof(*data) - 4 + 4, data->length);
475984a2ea9SAlexander Motin 	data->snlid = RCOP_SNLID;
476984a2ea9SAlexander Motin 	scsi_ulto2b(TPC_MAX_CSCDS, data->maximum_cscd_descriptor_count);
477984a2ea9SAlexander Motin 	scsi_ulto2b(TPC_MAX_SEGS, data->maximum_segment_descriptor_count);
478984a2ea9SAlexander Motin 	scsi_ulto4b(TPC_MAX_LIST, data->maximum_descriptor_list_length);
479984a2ea9SAlexander Motin 	scsi_ulto4b(TPC_MAX_SEG, data->maximum_segment_length);
480984a2ea9SAlexander Motin 	scsi_ulto4b(TPC_MAX_INLINE, data->maximum_inline_data_length);
481984a2ea9SAlexander Motin 	scsi_ulto4b(0, data->held_data_limit);
482984a2ea9SAlexander Motin 	scsi_ulto4b(0, data->maximum_stream_device_transfer_size);
483984a2ea9SAlexander Motin 	scsi_ulto2b(TPC_MAX_LISTS, data->total_concurrent_copies);
484984a2ea9SAlexander Motin 	data->maximum_concurrent_copies = TPC_MAX_LISTS;
485984a2ea9SAlexander Motin 	data->data_segment_granularity = 0;
486984a2ea9SAlexander Motin 	data->inline_data_granularity = 0;
487984a2ea9SAlexander Motin 	data->held_data_granularity = 0;
488984a2ea9SAlexander Motin 	data->implemented_descriptor_list_length = 4;
489984a2ea9SAlexander Motin 	data->list_of_implemented_descriptor_type_codes[0] = EC_SEG_B2B;
490984a2ea9SAlexander Motin 	data->list_of_implemented_descriptor_type_codes[1] = EC_SEG_VERIFY;
491984a2ea9SAlexander Motin 	data->list_of_implemented_descriptor_type_codes[2] = EC_SEG_REGISTER_KEY;
492984a2ea9SAlexander Motin 	data->list_of_implemented_descriptor_type_codes[3] = EC_CSCD_ID;
493984a2ea9SAlexander Motin 
494f7241cceSAlexander Motin 	ctl_set_success(ctsio);
495984a2ea9SAlexander Motin 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
496984a2ea9SAlexander Motin 	ctsio->be_move_done = ctl_config_move_done;
497984a2ea9SAlexander Motin 	ctl_datamove((union ctl_io *)ctsio);
498984a2ea9SAlexander Motin 	return (retval);
499984a2ea9SAlexander Motin }
500984a2ea9SAlexander Motin 
50143d2d719SAlexander Motin static struct tpc_list *
tpc_find_list(struct ctl_lun * lun,uint32_t list_id,uint32_t init_idx)50243d2d719SAlexander Motin tpc_find_list(struct ctl_lun *lun, uint32_t list_id, uint32_t init_idx)
50343d2d719SAlexander Motin {
50443d2d719SAlexander Motin 	struct tpc_list *list;
50543d2d719SAlexander Motin 
50643d2d719SAlexander Motin 	mtx_assert(&lun->lun_lock, MA_OWNED);
50743d2d719SAlexander Motin 	TAILQ_FOREACH(list, &lun->tpc_lists, links) {
50843d2d719SAlexander Motin 		if ((list->flags & EC_LIST_ID_USAGE_MASK) !=
50943d2d719SAlexander Motin 		     EC_LIST_ID_USAGE_NONE && list->list_id == list_id &&
51043d2d719SAlexander Motin 		    list->init_idx == init_idx)
51143d2d719SAlexander Motin 			break;
51243d2d719SAlexander Motin 	}
51343d2d719SAlexander Motin 	return (list);
51443d2d719SAlexander Motin }
51543d2d719SAlexander Motin 
516984a2ea9SAlexander Motin int
ctl_receive_copy_status_lid1(struct ctl_scsiio * ctsio)517984a2ea9SAlexander Motin ctl_receive_copy_status_lid1(struct ctl_scsiio *ctsio)
518984a2ea9SAlexander Motin {
5199cbbfd2fSAlexander Motin 	struct ctl_lun *lun = CTL_LUN(ctsio);
520984a2ea9SAlexander Motin 	struct scsi_receive_copy_status_lid1 *cdb;
521984a2ea9SAlexander Motin 	struct scsi_receive_copy_status_lid1_data *data;
522984a2ea9SAlexander Motin 	struct tpc_list *list;
523984a2ea9SAlexander Motin 	struct tpc_list list_copy;
524984a2ea9SAlexander Motin 	int retval;
525984a2ea9SAlexander Motin 	int alloc_len, total_len;
526984a2ea9SAlexander Motin 	uint32_t list_id;
527984a2ea9SAlexander Motin 
528984a2ea9SAlexander Motin 	CTL_DEBUG_PRINT(("ctl_receive_copy_status_lid1\n"));
529984a2ea9SAlexander Motin 
530984a2ea9SAlexander Motin 	cdb = (struct scsi_receive_copy_status_lid1 *)ctsio->cdb;
531984a2ea9SAlexander Motin 	retval = CTL_RETVAL_COMPLETE;
532984a2ea9SAlexander Motin 
533984a2ea9SAlexander Motin 	list_id = cdb->list_identifier;
534984a2ea9SAlexander Motin 	mtx_lock(&lun->lun_lock);
53543d2d719SAlexander Motin 	list = tpc_find_list(lun, list_id,
5367ac58230SAlexander Motin 	    ctl_get_initindex(&ctsio->io_hdr.nexus));
537984a2ea9SAlexander Motin 	if (list == NULL) {
538984a2ea9SAlexander Motin 		mtx_unlock(&lun->lun_lock);
539984a2ea9SAlexander Motin 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
540984a2ea9SAlexander Motin 		    /*command*/ 1, /*field*/ 2, /*bit_valid*/ 0,
541984a2ea9SAlexander Motin 		    /*bit*/ 0);
542984a2ea9SAlexander Motin 		ctl_done((union ctl_io *)ctsio);
543984a2ea9SAlexander Motin 		return (retval);
544984a2ea9SAlexander Motin 	}
545984a2ea9SAlexander Motin 	list_copy = *list;
546984a2ea9SAlexander Motin 	if (list->completed) {
547984a2ea9SAlexander Motin 		TAILQ_REMOVE(&lun->tpc_lists, list, links);
548984a2ea9SAlexander Motin 		free(list, M_CTL);
549984a2ea9SAlexander Motin 	}
550984a2ea9SAlexander Motin 	mtx_unlock(&lun->lun_lock);
551984a2ea9SAlexander Motin 
552984a2ea9SAlexander Motin 	total_len = sizeof(*data);
553984a2ea9SAlexander Motin 	alloc_len = scsi_4btoul(cdb->length);
554984a2ea9SAlexander Motin 
555984a2ea9SAlexander Motin 	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
556984a2ea9SAlexander Motin 	ctsio->kern_sg_entries = 0;
557984a2ea9SAlexander Motin 	ctsio->kern_rel_offset = 0;
558640603fbSAlexander Motin 	ctsio->kern_data_len = min(total_len, alloc_len);
559640603fbSAlexander Motin 	ctsio->kern_total_len = ctsio->kern_data_len;
560984a2ea9SAlexander Motin 
561984a2ea9SAlexander Motin 	data = (struct scsi_receive_copy_status_lid1_data *)ctsio->kern_data_ptr;
562984a2ea9SAlexander Motin 	scsi_ulto4b(sizeof(*data) - 4, data->available_data);
563984a2ea9SAlexander Motin 	if (list_copy.completed) {
564984a2ea9SAlexander Motin 		if (list_copy.error || list_copy.abort)
565984a2ea9SAlexander Motin 			data->copy_command_status = RCS_CCS_ERROR;
566984a2ea9SAlexander Motin 		else
567984a2ea9SAlexander Motin 			data->copy_command_status = RCS_CCS_COMPLETED;
568984a2ea9SAlexander Motin 	} else
569984a2ea9SAlexander Motin 		data->copy_command_status = RCS_CCS_INPROG;
570984a2ea9SAlexander Motin 	scsi_ulto2b(list_copy.curseg, data->segments_processed);
571984a2ea9SAlexander Motin 	if (list_copy.curbytes <= UINT32_MAX) {
572984a2ea9SAlexander Motin 		data->transfer_count_units = RCS_TC_BYTES;
573984a2ea9SAlexander Motin 		scsi_ulto4b(list_copy.curbytes, data->transfer_count);
574984a2ea9SAlexander Motin 	} else {
575984a2ea9SAlexander Motin 		data->transfer_count_units = RCS_TC_MBYTES;
576984a2ea9SAlexander Motin 		scsi_ulto4b(list_copy.curbytes >> 20, data->transfer_count);
577984a2ea9SAlexander Motin 	}
578984a2ea9SAlexander Motin 
579f7241cceSAlexander Motin 	ctl_set_success(ctsio);
580984a2ea9SAlexander Motin 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
581984a2ea9SAlexander Motin 	ctsio->be_move_done = ctl_config_move_done;
582984a2ea9SAlexander Motin 	ctl_datamove((union ctl_io *)ctsio);
583984a2ea9SAlexander Motin 	return (retval);
584984a2ea9SAlexander Motin }
585984a2ea9SAlexander Motin 
586984a2ea9SAlexander Motin int
ctl_receive_copy_failure_details(struct ctl_scsiio * ctsio)587984a2ea9SAlexander Motin ctl_receive_copy_failure_details(struct ctl_scsiio *ctsio)
588984a2ea9SAlexander Motin {
5899cbbfd2fSAlexander Motin 	struct ctl_lun *lun = CTL_LUN(ctsio);
590984a2ea9SAlexander Motin 	struct scsi_receive_copy_failure_details *cdb;
591984a2ea9SAlexander Motin 	struct scsi_receive_copy_failure_details_data *data;
592984a2ea9SAlexander Motin 	struct tpc_list *list;
593984a2ea9SAlexander Motin 	struct tpc_list list_copy;
594984a2ea9SAlexander Motin 	int retval;
595984a2ea9SAlexander Motin 	int alloc_len, total_len;
596984a2ea9SAlexander Motin 	uint32_t list_id;
597984a2ea9SAlexander Motin 
598984a2ea9SAlexander Motin 	CTL_DEBUG_PRINT(("ctl_receive_copy_failure_details\n"));
599984a2ea9SAlexander Motin 
600984a2ea9SAlexander Motin 	cdb = (struct scsi_receive_copy_failure_details *)ctsio->cdb;
601984a2ea9SAlexander Motin 	retval = CTL_RETVAL_COMPLETE;
602984a2ea9SAlexander Motin 
603984a2ea9SAlexander Motin 	list_id = cdb->list_identifier;
604984a2ea9SAlexander Motin 	mtx_lock(&lun->lun_lock);
60543d2d719SAlexander Motin 	list = tpc_find_list(lun, list_id,
6067ac58230SAlexander Motin 	    ctl_get_initindex(&ctsio->io_hdr.nexus));
60743d2d719SAlexander Motin 	if (list == NULL || !list->completed) {
608984a2ea9SAlexander Motin 		mtx_unlock(&lun->lun_lock);
609984a2ea9SAlexander Motin 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
610984a2ea9SAlexander Motin 		    /*command*/ 1, /*field*/ 2, /*bit_valid*/ 0,
611984a2ea9SAlexander Motin 		    /*bit*/ 0);
612984a2ea9SAlexander Motin 		ctl_done((union ctl_io *)ctsio);
613984a2ea9SAlexander Motin 		return (retval);
614984a2ea9SAlexander Motin 	}
615984a2ea9SAlexander Motin 	list_copy = *list;
616984a2ea9SAlexander Motin 	TAILQ_REMOVE(&lun->tpc_lists, list, links);
617984a2ea9SAlexander Motin 	free(list, M_CTL);
618984a2ea9SAlexander Motin 	mtx_unlock(&lun->lun_lock);
619984a2ea9SAlexander Motin 
620984a2ea9SAlexander Motin 	total_len = sizeof(*data) + list_copy.sense_len;
621984a2ea9SAlexander Motin 	alloc_len = scsi_4btoul(cdb->length);
622984a2ea9SAlexander Motin 
623984a2ea9SAlexander Motin 	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
624984a2ea9SAlexander Motin 	ctsio->kern_sg_entries = 0;
625984a2ea9SAlexander Motin 	ctsio->kern_rel_offset = 0;
626640603fbSAlexander Motin 	ctsio->kern_data_len = min(total_len, alloc_len);
627640603fbSAlexander Motin 	ctsio->kern_total_len = ctsio->kern_data_len;
628984a2ea9SAlexander Motin 
629984a2ea9SAlexander Motin 	data = (struct scsi_receive_copy_failure_details_data *)ctsio->kern_data_ptr;
630984a2ea9SAlexander Motin 	if (list_copy.completed && (list_copy.error || list_copy.abort)) {
631a7c09f5cSAlexander Motin 		scsi_ulto4b(sizeof(*data) - 4 + list_copy.sense_len,
632a7c09f5cSAlexander Motin 		    data->available_data);
633984a2ea9SAlexander Motin 		data->copy_command_status = RCS_CCS_ERROR;
634984a2ea9SAlexander Motin 	} else
635984a2ea9SAlexander Motin 		scsi_ulto4b(0, data->available_data);
636984a2ea9SAlexander Motin 	scsi_ulto2b(list_copy.sense_len, data->sense_data_length);
637984a2ea9SAlexander Motin 	memcpy(data->sense_data, &list_copy.sense_data, list_copy.sense_len);
638984a2ea9SAlexander Motin 
639f7241cceSAlexander Motin 	ctl_set_success(ctsio);
640984a2ea9SAlexander Motin 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
641984a2ea9SAlexander Motin 	ctsio->be_move_done = ctl_config_move_done;
642984a2ea9SAlexander Motin 	ctl_datamove((union ctl_io *)ctsio);
643984a2ea9SAlexander Motin 	return (retval);
644984a2ea9SAlexander Motin }
645984a2ea9SAlexander Motin 
646984a2ea9SAlexander Motin int
ctl_receive_copy_status_lid4(struct ctl_scsiio * ctsio)647984a2ea9SAlexander Motin ctl_receive_copy_status_lid4(struct ctl_scsiio *ctsio)
648984a2ea9SAlexander Motin {
6499cbbfd2fSAlexander Motin 	struct ctl_lun *lun = CTL_LUN(ctsio);
650984a2ea9SAlexander Motin 	struct scsi_receive_copy_status_lid4 *cdb;
651984a2ea9SAlexander Motin 	struct scsi_receive_copy_status_lid4_data *data;
652984a2ea9SAlexander Motin 	struct tpc_list *list;
653984a2ea9SAlexander Motin 	struct tpc_list list_copy;
654984a2ea9SAlexander Motin 	int retval;
655984a2ea9SAlexander Motin 	int alloc_len, total_len;
656984a2ea9SAlexander Motin 	uint32_t list_id;
657984a2ea9SAlexander Motin 
658984a2ea9SAlexander Motin 	CTL_DEBUG_PRINT(("ctl_receive_copy_status_lid4\n"));
659984a2ea9SAlexander Motin 
660984a2ea9SAlexander Motin 	cdb = (struct scsi_receive_copy_status_lid4 *)ctsio->cdb;
661984a2ea9SAlexander Motin 	retval = CTL_RETVAL_COMPLETE;
662984a2ea9SAlexander Motin 
663984a2ea9SAlexander Motin 	list_id = scsi_4btoul(cdb->list_identifier);
664984a2ea9SAlexander Motin 	mtx_lock(&lun->lun_lock);
66543d2d719SAlexander Motin 	list = tpc_find_list(lun, list_id,
6667ac58230SAlexander Motin 	    ctl_get_initindex(&ctsio->io_hdr.nexus));
667984a2ea9SAlexander Motin 	if (list == NULL) {
668984a2ea9SAlexander Motin 		mtx_unlock(&lun->lun_lock);
669984a2ea9SAlexander Motin 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
670984a2ea9SAlexander Motin 		    /*command*/ 1, /*field*/ 2, /*bit_valid*/ 0,
671984a2ea9SAlexander Motin 		    /*bit*/ 0);
672984a2ea9SAlexander Motin 		ctl_done((union ctl_io *)ctsio);
673984a2ea9SAlexander Motin 		return (retval);
674984a2ea9SAlexander Motin 	}
675984a2ea9SAlexander Motin 	list_copy = *list;
676984a2ea9SAlexander Motin 	if (list->completed) {
677984a2ea9SAlexander Motin 		TAILQ_REMOVE(&lun->tpc_lists, list, links);
678984a2ea9SAlexander Motin 		free(list, M_CTL);
679984a2ea9SAlexander Motin 	}
680984a2ea9SAlexander Motin 	mtx_unlock(&lun->lun_lock);
681984a2ea9SAlexander Motin 
682984a2ea9SAlexander Motin 	total_len = sizeof(*data) + list_copy.sense_len;
683984a2ea9SAlexander Motin 	alloc_len = scsi_4btoul(cdb->length);
684984a2ea9SAlexander Motin 
685984a2ea9SAlexander Motin 	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
686984a2ea9SAlexander Motin 	ctsio->kern_sg_entries = 0;
687984a2ea9SAlexander Motin 	ctsio->kern_rel_offset = 0;
688640603fbSAlexander Motin 	ctsio->kern_data_len = min(total_len, alloc_len);
689640603fbSAlexander Motin 	ctsio->kern_total_len = ctsio->kern_data_len;
690984a2ea9SAlexander Motin 
691984a2ea9SAlexander Motin 	data = (struct scsi_receive_copy_status_lid4_data *)ctsio->kern_data_ptr;
692a7c09f5cSAlexander Motin 	scsi_ulto4b(sizeof(*data) - 4 + list_copy.sense_len,
693a7c09f5cSAlexander Motin 	    data->available_data);
694984a2ea9SAlexander Motin 	data->response_to_service_action = list_copy.service_action;
695984a2ea9SAlexander Motin 	if (list_copy.completed) {
696984a2ea9SAlexander Motin 		if (list_copy.error)
697984a2ea9SAlexander Motin 			data->copy_command_status = RCS_CCS_ERROR;
698984a2ea9SAlexander Motin 		else if (list_copy.abort)
699984a2ea9SAlexander Motin 			data->copy_command_status = RCS_CCS_ABORTED;
700984a2ea9SAlexander Motin 		else
701984a2ea9SAlexander Motin 			data->copy_command_status = RCS_CCS_COMPLETED;
702984a2ea9SAlexander Motin 	} else
703984a2ea9SAlexander Motin 		data->copy_command_status = RCS_CCS_INPROG_FG;
704984a2ea9SAlexander Motin 	scsi_ulto2b(list_copy.curops, data->operation_counter);
705984a2ea9SAlexander Motin 	scsi_ulto4b(UINT32_MAX, data->estimated_status_update_delay);
706984a2ea9SAlexander Motin 	data->transfer_count_units = RCS_TC_BYTES;
707a7c09f5cSAlexander Motin 	scsi_u64to8b(list_copy.curbytes, data->transfer_count);
708984a2ea9SAlexander Motin 	scsi_ulto2b(list_copy.curseg, data->segments_processed);
709a7c09f5cSAlexander Motin 	data->length_of_the_sense_data_field = list_copy.sense_len;
710984a2ea9SAlexander Motin 	data->sense_data_length = list_copy.sense_len;
711984a2ea9SAlexander Motin 	memcpy(data->sense_data, &list_copy.sense_data, list_copy.sense_len);
712984a2ea9SAlexander Motin 
713f7241cceSAlexander Motin 	ctl_set_success(ctsio);
714984a2ea9SAlexander Motin 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
715984a2ea9SAlexander Motin 	ctsio->be_move_done = ctl_config_move_done;
716984a2ea9SAlexander Motin 	ctl_datamove((union ctl_io *)ctsio);
717984a2ea9SAlexander Motin 	return (retval);
718984a2ea9SAlexander Motin }
719984a2ea9SAlexander Motin 
720984a2ea9SAlexander Motin int
ctl_copy_operation_abort(struct ctl_scsiio * ctsio)721984a2ea9SAlexander Motin ctl_copy_operation_abort(struct ctl_scsiio *ctsio)
722984a2ea9SAlexander Motin {
7239cbbfd2fSAlexander Motin 	struct ctl_lun *lun = CTL_LUN(ctsio);
724984a2ea9SAlexander Motin 	struct scsi_copy_operation_abort *cdb;
725984a2ea9SAlexander Motin 	struct tpc_list *list;
726984a2ea9SAlexander Motin 	int retval;
727984a2ea9SAlexander Motin 	uint32_t list_id;
728984a2ea9SAlexander Motin 
729984a2ea9SAlexander Motin 	CTL_DEBUG_PRINT(("ctl_copy_operation_abort\n"));
730984a2ea9SAlexander Motin 
731984a2ea9SAlexander Motin 	cdb = (struct scsi_copy_operation_abort *)ctsio->cdb;
732984a2ea9SAlexander Motin 	retval = CTL_RETVAL_COMPLETE;
733984a2ea9SAlexander Motin 
734984a2ea9SAlexander Motin 	list_id = scsi_4btoul(cdb->list_identifier);
735984a2ea9SAlexander Motin 	mtx_lock(&lun->lun_lock);
73643d2d719SAlexander Motin 	list = tpc_find_list(lun, list_id,
7377ac58230SAlexander Motin 	    ctl_get_initindex(&ctsio->io_hdr.nexus));
738984a2ea9SAlexander Motin 	if (list == NULL) {
739984a2ea9SAlexander Motin 		mtx_unlock(&lun->lun_lock);
740984a2ea9SAlexander Motin 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
741984a2ea9SAlexander Motin 		    /*command*/ 1, /*field*/ 2, /*bit_valid*/ 0,
742984a2ea9SAlexander Motin 		    /*bit*/ 0);
743984a2ea9SAlexander Motin 		ctl_done((union ctl_io *)ctsio);
744984a2ea9SAlexander Motin 		return (retval);
745984a2ea9SAlexander Motin 	}
746984a2ea9SAlexander Motin 	list->abort = 1;
747984a2ea9SAlexander Motin 	mtx_unlock(&lun->lun_lock);
748984a2ea9SAlexander Motin 
749984a2ea9SAlexander Motin 	ctl_set_success(ctsio);
750984a2ea9SAlexander Motin 	ctl_done((union ctl_io *)ctsio);
751984a2ea9SAlexander Motin 	return (retval);
752984a2ea9SAlexander Motin }
753984a2ea9SAlexander Motin 
754984a2ea9SAlexander Motin static uint64_t
tpc_resolve(struct tpc_list * list,uint16_t idx,uint32_t * ss,uint32_t * pb,uint32_t * pbo)755fee04ef7SAlexander Motin tpc_resolve(struct tpc_list *list, uint16_t idx, uint32_t *ss,
756fee04ef7SAlexander Motin     uint32_t *pb, uint32_t *pbo)
757984a2ea9SAlexander Motin {
758984a2ea9SAlexander Motin 
759984a2ea9SAlexander Motin 	if (idx == 0xffff) {
760a9bd2281SAlexander Motin 		if (ss)
761984a2ea9SAlexander Motin 			*ss = list->lun->be_lun->blocksize;
762a9bd2281SAlexander Motin 		if (pb)
763fee04ef7SAlexander Motin 			*pb = list->lun->be_lun->blocksize <<
764fee04ef7SAlexander Motin 			    list->lun->be_lun->pblockexp;
765a9bd2281SAlexander Motin 		if (pbo)
766fee04ef7SAlexander Motin 			*pbo = list->lun->be_lun->blocksize *
767fee04ef7SAlexander Motin 			    list->lun->be_lun->pblockoff;
768984a2ea9SAlexander Motin 		return (list->lun->lun);
769984a2ea9SAlexander Motin 	}
770984a2ea9SAlexander Motin 	if (idx >= list->ncscd)
771984a2ea9SAlexander Motin 		return (UINT64_MAX);
7729602f436SAlexander Motin 	return (tpcl_resolve(list->lun->ctl_softc,
773fee04ef7SAlexander Motin 	    list->init_port, &list->cscd[idx], ss, pb, pbo));
774984a2ea9SAlexander Motin }
775984a2ea9SAlexander Motin 
776a3dd8378SAlexander Motin static void
tpc_set_io_error_sense(struct tpc_list * list)777a3dd8378SAlexander Motin tpc_set_io_error_sense(struct tpc_list *list)
778a3dd8378SAlexander Motin {
779a3dd8378SAlexander Motin 	int flen;
780a3dd8378SAlexander Motin 	uint8_t csi[4];
781a3dd8378SAlexander Motin 	uint8_t sks[3];
782a3dd8378SAlexander Motin 	uint8_t fbuf[4 + 64];
783a3dd8378SAlexander Motin 
784a3dd8378SAlexander Motin 	scsi_ulto4b(list->curseg, csi);
785a3dd8378SAlexander Motin 	if (list->fwd_cscd <= 0x07ff) {
786a3dd8378SAlexander Motin 		sks[0] = SSD_SKS_SEGMENT_VALID;
787a3dd8378SAlexander Motin 		scsi_ulto2b((uint8_t *)&list->cscd[list->fwd_cscd] -
788a3dd8378SAlexander Motin 		    list->params, &sks[1]);
789a3dd8378SAlexander Motin 	} else
790a3dd8378SAlexander Motin 		sks[0] = 0;
791a3dd8378SAlexander Motin 	if (list->fwd_scsi_status) {
792a3dd8378SAlexander Motin 		fbuf[0] = 0x0c;
793a3dd8378SAlexander Motin 		fbuf[2] = list->fwd_target;
794a3dd8378SAlexander Motin 		flen = list->fwd_sense_len;
795a3dd8378SAlexander Motin 		if (flen > 64) {
796a3dd8378SAlexander Motin 			flen = 64;
797a3dd8378SAlexander Motin 			fbuf[2] |= SSD_FORWARDED_FSDT;
798a3dd8378SAlexander Motin 		}
799a3dd8378SAlexander Motin 		fbuf[1] = 2 + flen;
800a3dd8378SAlexander Motin 		fbuf[3] = list->fwd_scsi_status;
801a3dd8378SAlexander Motin 		bcopy(&list->fwd_sense_data, &fbuf[4], flen);
802a3dd8378SAlexander Motin 		flen += 4;
803a3dd8378SAlexander Motin 	} else
804a3dd8378SAlexander Motin 		flen = 0;
805a3dd8378SAlexander Motin 	ctl_set_sense(list->ctsio, /*current_error*/ 1,
806a3dd8378SAlexander Motin 	    /*sense_key*/ SSD_KEY_COPY_ABORTED,
807a3dd8378SAlexander Motin 	    /*asc*/ 0x0d, /*ascq*/ 0x01,
808a3dd8378SAlexander Motin 	    SSD_ELEM_COMMAND, sizeof(csi), csi,
809a3dd8378SAlexander Motin 	    sks[0] ? SSD_ELEM_SKS : SSD_ELEM_SKIP, sizeof(sks), sks,
810a3dd8378SAlexander Motin 	    flen ? SSD_ELEM_DESC : SSD_ELEM_SKIP, flen, fbuf,
811a3dd8378SAlexander Motin 	    SSD_ELEM_NONE);
812a3dd8378SAlexander Motin }
813a3dd8378SAlexander Motin 
814984a2ea9SAlexander Motin static int
tpc_process_b2b(struct tpc_list * list)815984a2ea9SAlexander Motin tpc_process_b2b(struct tpc_list *list)
816984a2ea9SAlexander Motin {
817984a2ea9SAlexander Motin 	struct scsi_ec_segment_b2b *seg;
818984a2ea9SAlexander Motin 	struct scsi_ec_cscd_dtsp *sdstp, *ddstp;
819984a2ea9SAlexander Motin 	struct tpc_io *tior, *tiow;
82073942c5cSAlexander Motin 	struct runl run;
821984a2ea9SAlexander Motin 	uint64_t sl, dl;
822984a2ea9SAlexander Motin 	off_t srclba, dstlba, numbytes, donebytes, roundbytes;
823984a2ea9SAlexander Motin 	int numlba;
824fee04ef7SAlexander Motin 	uint32_t srcblock, dstblock, pb, pbo, adj;
825a3dd8378SAlexander Motin 	uint16_t scscd, dcscd;
826042e9bdcSAlexander Motin 	uint8_t csi[4];
827984a2ea9SAlexander Motin 
828042e9bdcSAlexander Motin 	scsi_ulto4b(list->curseg, csi);
829984a2ea9SAlexander Motin 	if (list->stage == 1) {
830984a2ea9SAlexander Motin 		while ((tior = TAILQ_FIRST(&list->allio)) != NULL) {
831984a2ea9SAlexander Motin 			TAILQ_REMOVE(&list->allio, tior, links);
832984a2ea9SAlexander Motin 			ctl_free_io(tior->io);
83346511441SAlexander Motin 			free(tior->buf, M_CTL);
834984a2ea9SAlexander Motin 			free(tior, M_CTL);
835984a2ea9SAlexander Motin 		}
836984a2ea9SAlexander Motin 		if (list->abort) {
837984a2ea9SAlexander Motin 			ctl_set_task_aborted(list->ctsio);
838984a2ea9SAlexander Motin 			return (CTL_RETVAL_ERROR);
839984a2ea9SAlexander Motin 		} else if (list->error) {
840a3dd8378SAlexander Motin 			tpc_set_io_error_sense(list);
841984a2ea9SAlexander Motin 			return (CTL_RETVAL_ERROR);
842e3e592bbSAlexander Motin 		}
84325eee848SAlexander Motin 		list->cursectors += list->segsectors;
844984a2ea9SAlexander Motin 		list->curbytes += list->segbytes;
845984a2ea9SAlexander Motin 		return (CTL_RETVAL_COMPLETE);
846984a2ea9SAlexander Motin 	}
847984a2ea9SAlexander Motin 
848984a2ea9SAlexander Motin 	TAILQ_INIT(&list->allio);
849984a2ea9SAlexander Motin 	seg = (struct scsi_ec_segment_b2b *)list->seg[list->curseg];
850a3dd8378SAlexander Motin 	scscd = scsi_2btoul(seg->src_cscd);
851a3dd8378SAlexander Motin 	dcscd = scsi_2btoul(seg->dst_cscd);
852a3dd8378SAlexander Motin 	sl = tpc_resolve(list, scscd, &srcblock, NULL, NULL);
853a3dd8378SAlexander Motin 	dl = tpc_resolve(list, dcscd, &dstblock, &pb, &pbo);
85441243159SAlexander Motin 	if (sl == UINT64_MAX || dl == UINT64_MAX) {
855984a2ea9SAlexander Motin 		ctl_set_sense(list->ctsio, /*current_error*/ 1,
856984a2ea9SAlexander Motin 		    /*sense_key*/ SSD_KEY_COPY_ABORTED,
857042e9bdcSAlexander Motin 		    /*asc*/ 0x08, /*ascq*/ 0x04,
858862aedb0SAlexander Motin 		    SSD_ELEM_COMMAND, sizeof(csi), csi,
859042e9bdcSAlexander Motin 		    SSD_ELEM_NONE);
860984a2ea9SAlexander Motin 		return (CTL_RETVAL_ERROR);
861984a2ea9SAlexander Motin 	}
862fee04ef7SAlexander Motin 	if (pbo > 0)
863fee04ef7SAlexander Motin 		pbo = pb - pbo;
864a3dd8378SAlexander Motin 	sdstp = &list->cscd[scscd].dtsp;
865984a2ea9SAlexander Motin 	if (scsi_3btoul(sdstp->block_length) != 0)
866984a2ea9SAlexander Motin 		srcblock = scsi_3btoul(sdstp->block_length);
867a3dd8378SAlexander Motin 	ddstp = &list->cscd[dcscd].dtsp;
868984a2ea9SAlexander Motin 	if (scsi_3btoul(ddstp->block_length) != 0)
869984a2ea9SAlexander Motin 		dstblock = scsi_3btoul(ddstp->block_length);
870984a2ea9SAlexander Motin 	numlba = scsi_2btoul(seg->number_of_blocks);
871984a2ea9SAlexander Motin 	if (seg->flags & EC_SEG_DC)
872984a2ea9SAlexander Motin 		numbytes = (off_t)numlba * dstblock;
873984a2ea9SAlexander Motin 	else
874984a2ea9SAlexander Motin 		numbytes = (off_t)numlba * srcblock;
875984a2ea9SAlexander Motin 	srclba = scsi_8btou64(seg->src_lba);
876984a2ea9SAlexander Motin 	dstlba = scsi_8btou64(seg->dst_lba);
877984a2ea9SAlexander Motin 
878984a2ea9SAlexander Motin //	printf("Copy %ju bytes from %ju @ %ju to %ju @ %ju\n",
879984a2ea9SAlexander Motin //	    (uintmax_t)numbytes, sl, scsi_8btou64(seg->src_lba),
880984a2ea9SAlexander Motin //	    dl, scsi_8btou64(seg->dst_lba));
881984a2ea9SAlexander Motin 
882984a2ea9SAlexander Motin 	if (numbytes == 0)
883984a2ea9SAlexander Motin 		return (CTL_RETVAL_COMPLETE);
884984a2ea9SAlexander Motin 
885984a2ea9SAlexander Motin 	if (numbytes % srcblock != 0 || numbytes % dstblock != 0) {
886984a2ea9SAlexander Motin 		ctl_set_sense(list->ctsio, /*current_error*/ 1,
887984a2ea9SAlexander Motin 		    /*sense_key*/ SSD_KEY_COPY_ABORTED,
888042e9bdcSAlexander Motin 		    /*asc*/ 0x26, /*ascq*/ 0x0A,
889862aedb0SAlexander Motin 		    SSD_ELEM_COMMAND, sizeof(csi), csi,
890042e9bdcSAlexander Motin 		    SSD_ELEM_NONE);
891984a2ea9SAlexander Motin 		return (CTL_RETVAL_ERROR);
892984a2ea9SAlexander Motin 	}
893984a2ea9SAlexander Motin 
894984a2ea9SAlexander Motin 	list->segbytes = numbytes;
89525eee848SAlexander Motin 	list->segsectors = numbytes / dstblock;
896984a2ea9SAlexander Motin 	donebytes = 0;
897984a2ea9SAlexander Motin 	TAILQ_INIT(&run);
89873942c5cSAlexander Motin 	list->tbdio = 0;
899984a2ea9SAlexander Motin 	while (donebytes < numbytes) {
900fee04ef7SAlexander Motin 		roundbytes = numbytes - donebytes;
901fee04ef7SAlexander Motin 		if (roundbytes > TPC_MAX_IO_SIZE) {
902fee04ef7SAlexander Motin 			roundbytes = TPC_MAX_IO_SIZE;
903fee04ef7SAlexander Motin 			roundbytes -= roundbytes % dstblock;
904fee04ef7SAlexander Motin 			if (pb > dstblock) {
905fee04ef7SAlexander Motin 				adj = (dstlba * dstblock + roundbytes - pbo) % pb;
906fee04ef7SAlexander Motin 				if (roundbytes > adj)
907fee04ef7SAlexander Motin 					roundbytes -= adj;
908fee04ef7SAlexander Motin 			}
909fee04ef7SAlexander Motin 		}
910984a2ea9SAlexander Motin 
911984a2ea9SAlexander Motin 		tior = malloc(sizeof(*tior), M_CTL, M_WAITOK | M_ZERO);
912984a2ea9SAlexander Motin 		TAILQ_INIT(&tior->run);
91346511441SAlexander Motin 		tior->buf = malloc(roundbytes, M_CTL, M_WAITOK);
914984a2ea9SAlexander Motin 		tior->list = list;
915984a2ea9SAlexander Motin 		TAILQ_INSERT_TAIL(&list->allio, tior, links);
916984a2ea9SAlexander Motin 		tior->io = tpcl_alloc_io();
917984a2ea9SAlexander Motin 		ctl_scsi_read_write(tior->io,
91846511441SAlexander Motin 				    /*data_ptr*/ tior->buf,
919984a2ea9SAlexander Motin 				    /*data_len*/ roundbytes,
920984a2ea9SAlexander Motin 				    /*read_op*/ 1,
921984a2ea9SAlexander Motin 				    /*byte2*/ 0,
922984a2ea9SAlexander Motin 				    /*minimum_cdb_size*/ 0,
923fee04ef7SAlexander Motin 				    /*lba*/ srclba,
924984a2ea9SAlexander Motin 				    /*num_blocks*/ roundbytes / srcblock,
925984a2ea9SAlexander Motin 				    /*tag_type*/ CTL_TAG_SIMPLE,
926984a2ea9SAlexander Motin 				    /*control*/ 0);
927984a2ea9SAlexander Motin 		tior->io->io_hdr.retries = 3;
928a3dd8378SAlexander Motin 		tior->target = SSD_FORWARDED_SDS_EXSRC;
929a3dd8378SAlexander Motin 		tior->cscd = scscd;
930984a2ea9SAlexander Motin 		tior->lun = sl;
931984a2ea9SAlexander Motin 		tior->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tior;
932984a2ea9SAlexander Motin 
933984a2ea9SAlexander Motin 		tiow = malloc(sizeof(*tior), M_CTL, M_WAITOK | M_ZERO);
934984a2ea9SAlexander Motin 		TAILQ_INIT(&tiow->run);
935984a2ea9SAlexander Motin 		tiow->list = list;
936984a2ea9SAlexander Motin 		TAILQ_INSERT_TAIL(&list->allio, tiow, links);
937984a2ea9SAlexander Motin 		tiow->io = tpcl_alloc_io();
938984a2ea9SAlexander Motin 		ctl_scsi_read_write(tiow->io,
93946511441SAlexander Motin 				    /*data_ptr*/ tior->buf,
940984a2ea9SAlexander Motin 				    /*data_len*/ roundbytes,
941984a2ea9SAlexander Motin 				    /*read_op*/ 0,
942984a2ea9SAlexander Motin 				    /*byte2*/ 0,
943984a2ea9SAlexander Motin 				    /*minimum_cdb_size*/ 0,
944fee04ef7SAlexander Motin 				    /*lba*/ dstlba,
945984a2ea9SAlexander Motin 				    /*num_blocks*/ roundbytes / dstblock,
946984a2ea9SAlexander Motin 				    /*tag_type*/ CTL_TAG_SIMPLE,
947984a2ea9SAlexander Motin 				    /*control*/ 0);
948984a2ea9SAlexander Motin 		tiow->io->io_hdr.retries = 3;
949a3dd8378SAlexander Motin 		tiow->target = SSD_FORWARDED_SDS_EXDST;
950a3dd8378SAlexander Motin 		tiow->cscd = dcscd;
951984a2ea9SAlexander Motin 		tiow->lun = dl;
952117f1bc1SAlexander Motin 		tiow->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tiow;
953984a2ea9SAlexander Motin 
954984a2ea9SAlexander Motin 		TAILQ_INSERT_TAIL(&tior->run, tiow, rlinks);
95573942c5cSAlexander Motin 		TAILQ_INSERT_TAIL(&run, tior, rlinks);
95673942c5cSAlexander Motin 		list->tbdio++;
957984a2ea9SAlexander Motin 		donebytes += roundbytes;
958fee04ef7SAlexander Motin 		srclba += roundbytes / srcblock;
959fee04ef7SAlexander Motin 		dstlba += roundbytes / dstblock;
960984a2ea9SAlexander Motin 	}
961984a2ea9SAlexander Motin 
962984a2ea9SAlexander Motin 	while ((tior = TAILQ_FIRST(&run)) != NULL) {
963984a2ea9SAlexander Motin 		TAILQ_REMOVE(&run, tior, rlinks);
964984a2ea9SAlexander Motin 		if (tpcl_queue(tior->io, tior->lun) != CTL_RETVAL_COMPLETE)
965984a2ea9SAlexander Motin 			panic("tpcl_queue() error");
966984a2ea9SAlexander Motin 	}
967984a2ea9SAlexander Motin 
968984a2ea9SAlexander Motin 	list->stage++;
969984a2ea9SAlexander Motin 	return (CTL_RETVAL_QUEUED);
970984a2ea9SAlexander Motin }
971984a2ea9SAlexander Motin 
972984a2ea9SAlexander Motin static int
tpc_process_verify(struct tpc_list * list)973984a2ea9SAlexander Motin tpc_process_verify(struct tpc_list *list)
974984a2ea9SAlexander Motin {
975984a2ea9SAlexander Motin 	struct scsi_ec_segment_verify *seg;
976984a2ea9SAlexander Motin 	struct tpc_io *tio;
977984a2ea9SAlexander Motin 	uint64_t sl;
978a3dd8378SAlexander Motin 	uint16_t cscd;
979042e9bdcSAlexander Motin 	uint8_t csi[4];
980984a2ea9SAlexander Motin 
981042e9bdcSAlexander Motin 	scsi_ulto4b(list->curseg, csi);
982984a2ea9SAlexander Motin 	if (list->stage == 1) {
983984a2ea9SAlexander Motin 		while ((tio = TAILQ_FIRST(&list->allio)) != NULL) {
984984a2ea9SAlexander Motin 			TAILQ_REMOVE(&list->allio, tio, links);
985984a2ea9SAlexander Motin 			ctl_free_io(tio->io);
986984a2ea9SAlexander Motin 			free(tio, M_CTL);
987984a2ea9SAlexander Motin 		}
988984a2ea9SAlexander Motin 		if (list->abort) {
989984a2ea9SAlexander Motin 			ctl_set_task_aborted(list->ctsio);
990984a2ea9SAlexander Motin 			return (CTL_RETVAL_ERROR);
991984a2ea9SAlexander Motin 		} else if (list->error) {
992a3dd8378SAlexander Motin 			tpc_set_io_error_sense(list);
993984a2ea9SAlexander Motin 			return (CTL_RETVAL_ERROR);
994984a2ea9SAlexander Motin 		} else
995984a2ea9SAlexander Motin 			return (CTL_RETVAL_COMPLETE);
996984a2ea9SAlexander Motin 	}
997984a2ea9SAlexander Motin 
998984a2ea9SAlexander Motin 	TAILQ_INIT(&list->allio);
999984a2ea9SAlexander Motin 	seg = (struct scsi_ec_segment_verify *)list->seg[list->curseg];
1000a3dd8378SAlexander Motin 	cscd = scsi_2btoul(seg->src_cscd);
1001a3dd8378SAlexander Motin 	sl = tpc_resolve(list, cscd, NULL, NULL, NULL);
100241243159SAlexander Motin 	if (sl == UINT64_MAX) {
1003984a2ea9SAlexander Motin 		ctl_set_sense(list->ctsio, /*current_error*/ 1,
1004984a2ea9SAlexander Motin 		    /*sense_key*/ SSD_KEY_COPY_ABORTED,
1005042e9bdcSAlexander Motin 		    /*asc*/ 0x08, /*ascq*/ 0x04,
1006862aedb0SAlexander Motin 		    SSD_ELEM_COMMAND, sizeof(csi), csi,
1007042e9bdcSAlexander Motin 		    SSD_ELEM_NONE);
1008984a2ea9SAlexander Motin 		return (CTL_RETVAL_ERROR);
1009984a2ea9SAlexander Motin 	}
1010984a2ea9SAlexander Motin 
1011984a2ea9SAlexander Motin //	printf("Verify %ju\n", sl);
1012984a2ea9SAlexander Motin 
1013984a2ea9SAlexander Motin 	if ((seg->tur & 0x01) == 0)
1014984a2ea9SAlexander Motin 		return (CTL_RETVAL_COMPLETE);
1015984a2ea9SAlexander Motin 
1016984a2ea9SAlexander Motin 	list->tbdio = 1;
1017984a2ea9SAlexander Motin 	tio = malloc(sizeof(*tio), M_CTL, M_WAITOK | M_ZERO);
1018984a2ea9SAlexander Motin 	TAILQ_INIT(&tio->run);
1019984a2ea9SAlexander Motin 	tio->list = list;
1020984a2ea9SAlexander Motin 	TAILQ_INSERT_TAIL(&list->allio, tio, links);
1021984a2ea9SAlexander Motin 	tio->io = tpcl_alloc_io();
1022984a2ea9SAlexander Motin 	ctl_scsi_tur(tio->io, /*tag_type*/ CTL_TAG_SIMPLE, /*control*/ 0);
1023984a2ea9SAlexander Motin 	tio->io->io_hdr.retries = 3;
1024a3dd8378SAlexander Motin 	tio->target = SSD_FORWARDED_SDS_EXSRC;
1025a3dd8378SAlexander Motin 	tio->cscd = cscd;
1026984a2ea9SAlexander Motin 	tio->lun = sl;
1027984a2ea9SAlexander Motin 	tio->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tio;
1028984a2ea9SAlexander Motin 	list->stage++;
1029984a2ea9SAlexander Motin 	if (tpcl_queue(tio->io, tio->lun) != CTL_RETVAL_COMPLETE)
1030984a2ea9SAlexander Motin 		panic("tpcl_queue() error");
1031984a2ea9SAlexander Motin 	return (CTL_RETVAL_QUEUED);
1032984a2ea9SAlexander Motin }
1033984a2ea9SAlexander Motin 
1034984a2ea9SAlexander Motin static int
tpc_process_register_key(struct tpc_list * list)1035984a2ea9SAlexander Motin tpc_process_register_key(struct tpc_list *list)
1036984a2ea9SAlexander Motin {
1037984a2ea9SAlexander Motin 	struct scsi_ec_segment_register_key *seg;
1038984a2ea9SAlexander Motin 	struct tpc_io *tio;
1039984a2ea9SAlexander Motin 	uint64_t dl;
1040984a2ea9SAlexander Motin 	int datalen;
1041a3dd8378SAlexander Motin 	uint16_t cscd;
1042042e9bdcSAlexander Motin 	uint8_t csi[4];
1043984a2ea9SAlexander Motin 
1044042e9bdcSAlexander Motin 	scsi_ulto4b(list->curseg, csi);
1045984a2ea9SAlexander Motin 	if (list->stage == 1) {
1046984a2ea9SAlexander Motin 		while ((tio = TAILQ_FIRST(&list->allio)) != NULL) {
1047984a2ea9SAlexander Motin 			TAILQ_REMOVE(&list->allio, tio, links);
1048984a2ea9SAlexander Motin 			ctl_free_io(tio->io);
104946511441SAlexander Motin 			free(tio->buf, M_CTL);
1050984a2ea9SAlexander Motin 			free(tio, M_CTL);
1051984a2ea9SAlexander Motin 		}
1052984a2ea9SAlexander Motin 		if (list->abort) {
1053984a2ea9SAlexander Motin 			ctl_set_task_aborted(list->ctsio);
1054984a2ea9SAlexander Motin 			return (CTL_RETVAL_ERROR);
1055984a2ea9SAlexander Motin 		} else if (list->error) {
1056a3dd8378SAlexander Motin 			tpc_set_io_error_sense(list);
1057984a2ea9SAlexander Motin 			return (CTL_RETVAL_ERROR);
1058984a2ea9SAlexander Motin 		} else
1059984a2ea9SAlexander Motin 			return (CTL_RETVAL_COMPLETE);
1060984a2ea9SAlexander Motin 	}
1061984a2ea9SAlexander Motin 
1062984a2ea9SAlexander Motin 	TAILQ_INIT(&list->allio);
1063984a2ea9SAlexander Motin 	seg = (struct scsi_ec_segment_register_key *)list->seg[list->curseg];
1064a3dd8378SAlexander Motin 	cscd = scsi_2btoul(seg->dst_cscd);
1065a3dd8378SAlexander Motin 	dl = tpc_resolve(list, cscd, NULL, NULL, NULL);
106641243159SAlexander Motin 	if (dl == UINT64_MAX) {
1067984a2ea9SAlexander Motin 		ctl_set_sense(list->ctsio, /*current_error*/ 1,
1068984a2ea9SAlexander Motin 		    /*sense_key*/ SSD_KEY_COPY_ABORTED,
1069042e9bdcSAlexander Motin 		    /*asc*/ 0x08, /*ascq*/ 0x04,
1070862aedb0SAlexander Motin 		    SSD_ELEM_COMMAND, sizeof(csi), csi,
1071042e9bdcSAlexander Motin 		    SSD_ELEM_NONE);
1072984a2ea9SAlexander Motin 		return (CTL_RETVAL_ERROR);
1073984a2ea9SAlexander Motin 	}
1074984a2ea9SAlexander Motin 
1075984a2ea9SAlexander Motin //	printf("Register Key %ju\n", dl);
1076984a2ea9SAlexander Motin 
1077984a2ea9SAlexander Motin 	list->tbdio = 1;
1078984a2ea9SAlexander Motin 	tio = malloc(sizeof(*tio), M_CTL, M_WAITOK | M_ZERO);
1079984a2ea9SAlexander Motin 	TAILQ_INIT(&tio->run);
1080984a2ea9SAlexander Motin 	tio->list = list;
1081984a2ea9SAlexander Motin 	TAILQ_INSERT_TAIL(&list->allio, tio, links);
1082984a2ea9SAlexander Motin 	tio->io = tpcl_alloc_io();
1083984a2ea9SAlexander Motin 	datalen = sizeof(struct scsi_per_res_out_parms);
108446511441SAlexander Motin 	tio->buf = malloc(datalen, M_CTL, M_WAITOK);
1085984a2ea9SAlexander Motin 	ctl_scsi_persistent_res_out(tio->io,
108646511441SAlexander Motin 	    tio->buf, datalen, SPRO_REGISTER, -1,
1087984a2ea9SAlexander Motin 	    scsi_8btou64(seg->res_key), scsi_8btou64(seg->sa_res_key),
1088984a2ea9SAlexander Motin 	    /*tag_type*/ CTL_TAG_SIMPLE, /*control*/ 0);
1089984a2ea9SAlexander Motin 	tio->io->io_hdr.retries = 3;
1090a3dd8378SAlexander Motin 	tio->target = SSD_FORWARDED_SDS_EXDST;
1091a3dd8378SAlexander Motin 	tio->cscd = cscd;
1092984a2ea9SAlexander Motin 	tio->lun = dl;
1093984a2ea9SAlexander Motin 	tio->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tio;
1094984a2ea9SAlexander Motin 	list->stage++;
1095984a2ea9SAlexander Motin 	if (tpcl_queue(tio->io, tio->lun) != CTL_RETVAL_COMPLETE)
1096984a2ea9SAlexander Motin 		panic("tpcl_queue() error");
1097984a2ea9SAlexander Motin 	return (CTL_RETVAL_QUEUED);
1098984a2ea9SAlexander Motin }
1099984a2ea9SAlexander Motin 
110025eee848SAlexander Motin static off_t
tpc_ranges_length(struct scsi_range_desc * range,int nrange)110125eee848SAlexander Motin tpc_ranges_length(struct scsi_range_desc *range, int nrange)
110225eee848SAlexander Motin {
110325eee848SAlexander Motin 	off_t length = 0;
110425eee848SAlexander Motin 	int r;
110525eee848SAlexander Motin 
110625eee848SAlexander Motin 	for (r = 0; r < nrange; r++)
110725eee848SAlexander Motin 		length += scsi_4btoul(range[r].length);
110825eee848SAlexander Motin 	return (length);
110925eee848SAlexander Motin }
111025eee848SAlexander Motin 
111125eee848SAlexander Motin static int
tpc_check_ranges_l(struct scsi_range_desc * range,int nrange,uint64_t maxlba,uint64_t * lba)111232920cbfSAlexander Motin tpc_check_ranges_l(struct scsi_range_desc *range, int nrange, uint64_t maxlba,
111332920cbfSAlexander Motin     uint64_t *lba)
1114e13f4248SAlexander Motin {
11158fadf660SAlexander Motin 	uint64_t b1;
11168fadf660SAlexander Motin 	uint32_t l1;
11178fadf660SAlexander Motin 	int i;
1118e13f4248SAlexander Motin 
111938618bf4SAlexander Motin 	for (i = 0; i < nrange; i++) {
112038618bf4SAlexander Motin 		b1 = scsi_8btou64(range[i].lba);
112138618bf4SAlexander Motin 		l1 = scsi_4btoul(range[i].length);
112232920cbfSAlexander Motin 		if (b1 + l1 < b1 || b1 + l1 > maxlba + 1) {
112332920cbfSAlexander Motin 			*lba = MAX(b1, maxlba + 1);
112438618bf4SAlexander Motin 			return (-1);
112538618bf4SAlexander Motin 		}
112632920cbfSAlexander Motin 	}
11278fadf660SAlexander Motin 	return (0);
11288fadf660SAlexander Motin }
11298fadf660SAlexander Motin 
11308fadf660SAlexander Motin static int
tpc_check_ranges_x(struct scsi_range_desc * range,int nrange)11318fadf660SAlexander Motin tpc_check_ranges_x(struct scsi_range_desc *range, int nrange)
11328fadf660SAlexander Motin {
11338fadf660SAlexander Motin 	uint64_t b1, b2;
11348fadf660SAlexander Motin 	uint32_t l1, l2;
11358fadf660SAlexander Motin 	int i, j;
11368fadf660SAlexander Motin 
1137e13f4248SAlexander Motin 	for (i = 0; i < nrange - 1; i++) {
1138e13f4248SAlexander Motin 		b1 = scsi_8btou64(range[i].lba);
1139e13f4248SAlexander Motin 		l1 = scsi_4btoul(range[i].length);
1140e13f4248SAlexander Motin 		for (j = i + 1; j < nrange; j++) {
1141e13f4248SAlexander Motin 			b2 = scsi_8btou64(range[j].lba);
1142e13f4248SAlexander Motin 			l2 = scsi_4btoul(range[j].length);
1143e13f4248SAlexander Motin 			if (b1 + l1 > b2 && b2 + l2 > b1)
1144e13f4248SAlexander Motin 				return (-1);
1145e13f4248SAlexander Motin 		}
1146e13f4248SAlexander Motin 	}
1147e13f4248SAlexander Motin 	return (0);
1148e13f4248SAlexander Motin }
1149e13f4248SAlexander Motin 
1150e13f4248SAlexander Motin static int
tpc_skip_ranges(struct scsi_range_desc * range,int nrange,off_t skip,int * srange,off_t * soffset)115125eee848SAlexander Motin tpc_skip_ranges(struct scsi_range_desc *range, int nrange, off_t skip,
115225eee848SAlexander Motin     int *srange, off_t *soffset)
115325eee848SAlexander Motin {
115425eee848SAlexander Motin 	off_t off;
115525eee848SAlexander Motin 	int r;
115625eee848SAlexander Motin 
115725eee848SAlexander Motin 	r = 0;
115825eee848SAlexander Motin 	off = 0;
115925eee848SAlexander Motin 	while (r < nrange) {
116025eee848SAlexander Motin 		if (skip - off < scsi_4btoul(range[r].length)) {
116125eee848SAlexander Motin 			*srange = r;
116225eee848SAlexander Motin 			*soffset = skip - off;
116325eee848SAlexander Motin 			return (0);
116425eee848SAlexander Motin 		}
116525eee848SAlexander Motin 		off += scsi_4btoul(range[r].length);
116625eee848SAlexander Motin 		r++;
116725eee848SAlexander Motin 	}
116825eee848SAlexander Motin 	return (-1);
116925eee848SAlexander Motin }
117025eee848SAlexander Motin 
117125eee848SAlexander Motin static int
tpc_process_wut(struct tpc_list * list)117225eee848SAlexander Motin tpc_process_wut(struct tpc_list *list)
117325eee848SAlexander Motin {
117425eee848SAlexander Motin 	struct tpc_io *tio, *tior, *tiow;
11750952a19fSAlexander Motin 	struct runl run;
117625eee848SAlexander Motin 	int drange, srange;
117725eee848SAlexander Motin 	off_t doffset, soffset;
117825eee848SAlexander Motin 	off_t srclba, dstlba, numbytes, donebytes, roundbytes;
1179fee04ef7SAlexander Motin 	uint32_t srcblock, dstblock, pb, pbo, adj;
118025eee848SAlexander Motin 
118125eee848SAlexander Motin 	if (list->stage > 0) {
118225eee848SAlexander Motin 		/* Cleanup after previous rounds. */
118325eee848SAlexander Motin 		while ((tio = TAILQ_FIRST(&list->allio)) != NULL) {
118425eee848SAlexander Motin 			TAILQ_REMOVE(&list->allio, tio, links);
118525eee848SAlexander Motin 			ctl_free_io(tio->io);
118646511441SAlexander Motin 			free(tio->buf, M_CTL);
118725eee848SAlexander Motin 			free(tio, M_CTL);
118825eee848SAlexander Motin 		}
118925eee848SAlexander Motin 		if (list->abort) {
119025eee848SAlexander Motin 			ctl_set_task_aborted(list->ctsio);
119125eee848SAlexander Motin 			return (CTL_RETVAL_ERROR);
119225eee848SAlexander Motin 		} else if (list->error) {
1193a3dd8378SAlexander Motin 			if (list->fwd_scsi_status) {
1194a3dd8378SAlexander Motin 				list->ctsio->io_hdr.status =
1195a3dd8378SAlexander Motin 				    CTL_SCSI_ERROR | CTL_AUTOSENSE;
1196a3dd8378SAlexander Motin 				list->ctsio->scsi_status = list->fwd_scsi_status;
1197a3dd8378SAlexander Motin 				list->ctsio->sense_data = list->fwd_sense_data;
1198a3dd8378SAlexander Motin 				list->ctsio->sense_len = list->fwd_sense_len;
1199a3dd8378SAlexander Motin 			} else {
1200a3dd8378SAlexander Motin 				ctl_set_invalid_field(list->ctsio,
1201a3dd8378SAlexander Motin 				    /*sks_valid*/ 0, /*command*/ 0,
1202a3dd8378SAlexander Motin 				    /*field*/ 0, /*bit_valid*/ 0, /*bit*/ 0);
1203a3dd8378SAlexander Motin 			}
120425eee848SAlexander Motin 			return (CTL_RETVAL_ERROR);
120525eee848SAlexander Motin 		}
120625eee848SAlexander Motin 		list->cursectors += list->segsectors;
120725eee848SAlexander Motin 		list->curbytes += list->segbytes;
120825eee848SAlexander Motin 	}
120925eee848SAlexander Motin 
121025eee848SAlexander Motin 	/* Check where we are on destination ranges list. */
121125eee848SAlexander Motin 	if (tpc_skip_ranges(list->range, list->nrange, list->cursectors,
121225eee848SAlexander Motin 	    &drange, &doffset) != 0)
121325eee848SAlexander Motin 		return (CTL_RETVAL_COMPLETE);
121425eee848SAlexander Motin 	dstblock = list->lun->be_lun->blocksize;
1215fee04ef7SAlexander Motin 	pb = dstblock << list->lun->be_lun->pblockexp;
1216fee04ef7SAlexander Motin 	if (list->lun->be_lun->pblockoff > 0)
1217fee04ef7SAlexander Motin 		pbo = pb - dstblock * list->lun->be_lun->pblockoff;
1218fee04ef7SAlexander Motin 	else
1219fee04ef7SAlexander Motin 		pbo = 0;
122025eee848SAlexander Motin 
122125eee848SAlexander Motin 	/* Check where we are on source ranges list. */
122225eee848SAlexander Motin 	srcblock = list->token->blocksize;
122325eee848SAlexander Motin 	if (tpc_skip_ranges(list->token->range, list->token->nrange,
122425eee848SAlexander Motin 	    list->offset_into_rod + list->cursectors * dstblock / srcblock,
122525eee848SAlexander Motin 	    &srange, &soffset) != 0) {
1226a3dd8378SAlexander Motin 		ctl_set_invalid_field(list->ctsio, /*sks_valid*/ 0,
1227a3dd8378SAlexander Motin 		    /*command*/ 0, /*field*/ 0, /*bit_valid*/ 0, /*bit*/ 0);
122825eee848SAlexander Motin 		return (CTL_RETVAL_ERROR);
122925eee848SAlexander Motin 	}
123025eee848SAlexander Motin 
123125eee848SAlexander Motin 	srclba = scsi_8btou64(list->token->range[srange].lba) + soffset;
123225eee848SAlexander Motin 	dstlba = scsi_8btou64(list->range[drange].lba) + doffset;
1233fee04ef7SAlexander Motin 	numbytes = srcblock *
1234fee04ef7SAlexander Motin 	    (scsi_4btoul(list->token->range[srange].length) - soffset);
1235fee04ef7SAlexander Motin 	numbytes = omin(numbytes, dstblock *
1236fee04ef7SAlexander Motin 	    (scsi_4btoul(list->range[drange].length) - doffset));
1237fee04ef7SAlexander Motin 	if (numbytes > TPC_MAX_IOCHUNK_SIZE) {
1238fee04ef7SAlexander Motin 		numbytes = TPC_MAX_IOCHUNK_SIZE;
1239fee04ef7SAlexander Motin 		numbytes -= numbytes % dstblock;
1240fee04ef7SAlexander Motin 		if (pb > dstblock) {
1241fee04ef7SAlexander Motin 			adj = (dstlba * dstblock + numbytes - pbo) % pb;
1242fee04ef7SAlexander Motin 			if (numbytes > adj)
1243fee04ef7SAlexander Motin 				numbytes -= adj;
1244fee04ef7SAlexander Motin 		}
1245fee04ef7SAlexander Motin 	}
124625eee848SAlexander Motin 
124725eee848SAlexander Motin 	if (numbytes % srcblock != 0 || numbytes % dstblock != 0) {
1248a3dd8378SAlexander Motin 		ctl_set_invalid_field(list->ctsio, /*sks_valid*/ 0,
1249a3dd8378SAlexander Motin 		    /*command*/ 0, /*field*/ 0, /*bit_valid*/ 0, /*bit*/ 0);
125025eee848SAlexander Motin 		return (CTL_RETVAL_ERROR);
125125eee848SAlexander Motin 	}
125225eee848SAlexander Motin 
125325eee848SAlexander Motin 	list->segbytes = numbytes;
125425eee848SAlexander Motin 	list->segsectors = numbytes / dstblock;
125525eee848SAlexander Motin //printf("Copy chunk of %ju sectors from %ju to %ju\n", list->segsectors,
125625eee848SAlexander Motin //    srclba, dstlba);
125725eee848SAlexander Motin 	donebytes = 0;
125825eee848SAlexander Motin 	TAILQ_INIT(&run);
12590952a19fSAlexander Motin 	list->tbdio = 0;
126025eee848SAlexander Motin 	TAILQ_INIT(&list->allio);
126125eee848SAlexander Motin 	while (donebytes < numbytes) {
1262fee04ef7SAlexander Motin 		roundbytes = numbytes - donebytes;
1263fee04ef7SAlexander Motin 		if (roundbytes > TPC_MAX_IO_SIZE) {
1264fee04ef7SAlexander Motin 			roundbytes = TPC_MAX_IO_SIZE;
1265fee04ef7SAlexander Motin 			roundbytes -= roundbytes % dstblock;
1266fee04ef7SAlexander Motin 			if (pb > dstblock) {
1267fee04ef7SAlexander Motin 				adj = (dstlba * dstblock + roundbytes - pbo) % pb;
1268fee04ef7SAlexander Motin 				if (roundbytes > adj)
1269fee04ef7SAlexander Motin 					roundbytes -= adj;
1270fee04ef7SAlexander Motin 			}
1271fee04ef7SAlexander Motin 		}
127225eee848SAlexander Motin 
127325eee848SAlexander Motin 		tior = malloc(sizeof(*tior), M_CTL, M_WAITOK | M_ZERO);
127425eee848SAlexander Motin 		TAILQ_INIT(&tior->run);
127546511441SAlexander Motin 		tior->buf = malloc(roundbytes, M_CTL, M_WAITOK);
127625eee848SAlexander Motin 		tior->list = list;
127725eee848SAlexander Motin 		TAILQ_INSERT_TAIL(&list->allio, tior, links);
127825eee848SAlexander Motin 		tior->io = tpcl_alloc_io();
127925eee848SAlexander Motin 		ctl_scsi_read_write(tior->io,
128046511441SAlexander Motin 				    /*data_ptr*/ tior->buf,
128125eee848SAlexander Motin 				    /*data_len*/ roundbytes,
128225eee848SAlexander Motin 				    /*read_op*/ 1,
128325eee848SAlexander Motin 				    /*byte2*/ 0,
128425eee848SAlexander Motin 				    /*minimum_cdb_size*/ 0,
1285fee04ef7SAlexander Motin 				    /*lba*/ srclba,
128625eee848SAlexander Motin 				    /*num_blocks*/ roundbytes / srcblock,
128725eee848SAlexander Motin 				    /*tag_type*/ CTL_TAG_SIMPLE,
128825eee848SAlexander Motin 				    /*control*/ 0);
128925eee848SAlexander Motin 		tior->io->io_hdr.retries = 3;
129025eee848SAlexander Motin 		tior->lun = list->token->lun;
129125eee848SAlexander Motin 		tior->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tior;
129225eee848SAlexander Motin 
129325eee848SAlexander Motin 		tiow = malloc(sizeof(*tiow), M_CTL, M_WAITOK | M_ZERO);
129425eee848SAlexander Motin 		TAILQ_INIT(&tiow->run);
129525eee848SAlexander Motin 		tiow->list = list;
129625eee848SAlexander Motin 		TAILQ_INSERT_TAIL(&list->allio, tiow, links);
129725eee848SAlexander Motin 		tiow->io = tpcl_alloc_io();
129825eee848SAlexander Motin 		ctl_scsi_read_write(tiow->io,
129946511441SAlexander Motin 				    /*data_ptr*/ tior->buf,
130025eee848SAlexander Motin 				    /*data_len*/ roundbytes,
130125eee848SAlexander Motin 				    /*read_op*/ 0,
130225eee848SAlexander Motin 				    /*byte2*/ 0,
130325eee848SAlexander Motin 				    /*minimum_cdb_size*/ 0,
1304fee04ef7SAlexander Motin 				    /*lba*/ dstlba,
130525eee848SAlexander Motin 				    /*num_blocks*/ roundbytes / dstblock,
130625eee848SAlexander Motin 				    /*tag_type*/ CTL_TAG_SIMPLE,
130725eee848SAlexander Motin 				    /*control*/ 0);
130825eee848SAlexander Motin 		tiow->io->io_hdr.retries = 3;
130925eee848SAlexander Motin 		tiow->lun = list->lun->lun;
131025eee848SAlexander Motin 		tiow->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tiow;
131125eee848SAlexander Motin 
131225eee848SAlexander Motin 		TAILQ_INSERT_TAIL(&tior->run, tiow, rlinks);
13130952a19fSAlexander Motin 		TAILQ_INSERT_TAIL(&run, tior, rlinks);
13140952a19fSAlexander Motin 		list->tbdio++;
131525eee848SAlexander Motin 		donebytes += roundbytes;
1316fee04ef7SAlexander Motin 		srclba += roundbytes / srcblock;
1317fee04ef7SAlexander Motin 		dstlba += roundbytes / dstblock;
131825eee848SAlexander Motin 	}
131925eee848SAlexander Motin 
132025eee848SAlexander Motin 	while ((tior = TAILQ_FIRST(&run)) != NULL) {
132125eee848SAlexander Motin 		TAILQ_REMOVE(&run, tior, rlinks);
132225eee848SAlexander Motin 		if (tpcl_queue(tior->io, tior->lun) != CTL_RETVAL_COMPLETE)
132325eee848SAlexander Motin 			panic("tpcl_queue() error");
132425eee848SAlexander Motin 	}
132525eee848SAlexander Motin 
132625eee848SAlexander Motin 	list->stage++;
132725eee848SAlexander Motin 	return (CTL_RETVAL_QUEUED);
132825eee848SAlexander Motin }
132925eee848SAlexander Motin 
1330e3e592bbSAlexander Motin static int
tpc_process_zero_wut(struct tpc_list * list)1331e3e592bbSAlexander Motin tpc_process_zero_wut(struct tpc_list *list)
1332e3e592bbSAlexander Motin {
1333e3e592bbSAlexander Motin 	struct tpc_io *tio, *tiow;
1334e3e592bbSAlexander Motin 	struct runl run, *prun;
1335e3e592bbSAlexander Motin 	int r;
1336e3e592bbSAlexander Motin 	uint32_t dstblock, len;
1337e3e592bbSAlexander Motin 
1338e3e592bbSAlexander Motin 	if (list->stage > 0) {
1339e3e592bbSAlexander Motin complete:
1340e3e592bbSAlexander Motin 		/* Cleanup after previous rounds. */
1341e3e592bbSAlexander Motin 		while ((tio = TAILQ_FIRST(&list->allio)) != NULL) {
1342e3e592bbSAlexander Motin 			TAILQ_REMOVE(&list->allio, tio, links);
1343e3e592bbSAlexander Motin 			ctl_free_io(tio->io);
1344e3e592bbSAlexander Motin 			free(tio, M_CTL);
1345e3e592bbSAlexander Motin 		}
1346e3e592bbSAlexander Motin 		if (list->abort) {
1347e3e592bbSAlexander Motin 			ctl_set_task_aborted(list->ctsio);
1348e3e592bbSAlexander Motin 			return (CTL_RETVAL_ERROR);
1349e3e592bbSAlexander Motin 		} else if (list->error) {
1350a3dd8378SAlexander Motin 			if (list->fwd_scsi_status) {
1351a3dd8378SAlexander Motin 				list->ctsio->io_hdr.status =
1352a3dd8378SAlexander Motin 				    CTL_SCSI_ERROR | CTL_AUTOSENSE;
1353a3dd8378SAlexander Motin 				list->ctsio->scsi_status = list->fwd_scsi_status;
1354a3dd8378SAlexander Motin 				list->ctsio->sense_data = list->fwd_sense_data;
1355a3dd8378SAlexander Motin 				list->ctsio->sense_len = list->fwd_sense_len;
1356a3dd8378SAlexander Motin 			} else {
1357a3dd8378SAlexander Motin 				ctl_set_invalid_field(list->ctsio,
1358a3dd8378SAlexander Motin 				    /*sks_valid*/ 0, /*command*/ 0,
1359a3dd8378SAlexander Motin 				    /*field*/ 0, /*bit_valid*/ 0, /*bit*/ 0);
1360a3dd8378SAlexander Motin 			}
1361e3e592bbSAlexander Motin 			return (CTL_RETVAL_ERROR);
1362e3e592bbSAlexander Motin 		}
1363e3e592bbSAlexander Motin 		list->cursectors += list->segsectors;
1364e3e592bbSAlexander Motin 		list->curbytes += list->segbytes;
1365e3e592bbSAlexander Motin 		return (CTL_RETVAL_COMPLETE);
1366e3e592bbSAlexander Motin 	}
1367e3e592bbSAlexander Motin 
1368e3e592bbSAlexander Motin 	dstblock = list->lun->be_lun->blocksize;
1369e3e592bbSAlexander Motin 	TAILQ_INIT(&run);
1370e3e592bbSAlexander Motin 	prun = &run;
1371e3e592bbSAlexander Motin 	list->tbdio = 1;
1372e3e592bbSAlexander Motin 	TAILQ_INIT(&list->allio);
1373e3e592bbSAlexander Motin 	list->segsectors = 0;
1374e3e592bbSAlexander Motin 	for (r = 0; r < list->nrange; r++) {
1375e3e592bbSAlexander Motin 		len = scsi_4btoul(list->range[r].length);
1376e3e592bbSAlexander Motin 		if (len == 0)
1377e3e592bbSAlexander Motin 			continue;
1378e3e592bbSAlexander Motin 
1379e3e592bbSAlexander Motin 		tiow = malloc(sizeof(*tiow), M_CTL, M_WAITOK | M_ZERO);
1380e3e592bbSAlexander Motin 		TAILQ_INIT(&tiow->run);
1381e3e592bbSAlexander Motin 		tiow->list = list;
1382e3e592bbSAlexander Motin 		TAILQ_INSERT_TAIL(&list->allio, tiow, links);
1383e3e592bbSAlexander Motin 		tiow->io = tpcl_alloc_io();
1384e3e592bbSAlexander Motin 		ctl_scsi_write_same(tiow->io,
13856ac1446dSAlexander Motin 				    /*data_ptr*/ NULL,
13866ac1446dSAlexander Motin 				    /*data_len*/ 0,
13876ac1446dSAlexander Motin 				    /*byte2*/ SWS_NDOB,
1388e3e592bbSAlexander Motin 				    /*lba*/ scsi_8btou64(list->range[r].lba),
1389e3e592bbSAlexander Motin 				    /*num_blocks*/ len,
1390e3e592bbSAlexander Motin 				    /*tag_type*/ CTL_TAG_SIMPLE,
1391e3e592bbSAlexander Motin 				    /*control*/ 0);
1392e3e592bbSAlexander Motin 		tiow->io->io_hdr.retries = 3;
1393e3e592bbSAlexander Motin 		tiow->lun = list->lun->lun;
1394e3e592bbSAlexander Motin 		tiow->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tiow;
1395e3e592bbSAlexander Motin 
1396e3e592bbSAlexander Motin 		TAILQ_INSERT_TAIL(prun, tiow, rlinks);
1397e3e592bbSAlexander Motin 		prun = &tiow->run;
1398e3e592bbSAlexander Motin 		list->segsectors += len;
1399e3e592bbSAlexander Motin 	}
1400e3e592bbSAlexander Motin 	list->segbytes = list->segsectors * dstblock;
1401e3e592bbSAlexander Motin 
1402e3e592bbSAlexander Motin 	if (TAILQ_EMPTY(&run))
1403e3e592bbSAlexander Motin 		goto complete;
1404e3e592bbSAlexander Motin 
1405e3e592bbSAlexander Motin 	while ((tiow = TAILQ_FIRST(&run)) != NULL) {
1406e3e592bbSAlexander Motin 		TAILQ_REMOVE(&run, tiow, rlinks);
1407e3e592bbSAlexander Motin 		if (tpcl_queue(tiow->io, tiow->lun) != CTL_RETVAL_COMPLETE)
1408e3e592bbSAlexander Motin 			panic("tpcl_queue() error");
1409e3e592bbSAlexander Motin 	}
1410e3e592bbSAlexander Motin 
1411e3e592bbSAlexander Motin 	list->stage++;
1412e3e592bbSAlexander Motin 	return (CTL_RETVAL_QUEUED);
1413e3e592bbSAlexander Motin }
1414e3e592bbSAlexander Motin 
1415984a2ea9SAlexander Motin static void
tpc_process(struct tpc_list * list)1416984a2ea9SAlexander Motin tpc_process(struct tpc_list *list)
1417984a2ea9SAlexander Motin {
1418984a2ea9SAlexander Motin 	struct ctl_lun *lun = list->lun;
14199602f436SAlexander Motin 	struct ctl_softc *softc = lun->ctl_softc;
1420984a2ea9SAlexander Motin 	struct scsi_ec_segment *seg;
1421984a2ea9SAlexander Motin 	struct ctl_scsiio *ctsio = list->ctsio;
1422984a2ea9SAlexander Motin 	int retval = CTL_RETVAL_COMPLETE;
1423042e9bdcSAlexander Motin 	uint8_t csi[4];
1424984a2ea9SAlexander Motin 
142525eee848SAlexander Motin 	if (list->service_action == EC_WUT) {
1426e3e592bbSAlexander Motin 		if (list->token != NULL)
142725eee848SAlexander Motin 			retval = tpc_process_wut(list);
1428e3e592bbSAlexander Motin 		else
1429e3e592bbSAlexander Motin 			retval = tpc_process_zero_wut(list);
143025eee848SAlexander Motin 		if (retval == CTL_RETVAL_QUEUED)
143125eee848SAlexander Motin 			return;
143225eee848SAlexander Motin 		if (retval == CTL_RETVAL_ERROR) {
143325eee848SAlexander Motin 			list->error = 1;
143425eee848SAlexander Motin 			goto done;
143525eee848SAlexander Motin 		}
143625eee848SAlexander Motin 	} else {
1437984a2ea9SAlexander Motin //printf("ZZZ %d cscd, %d segs\n", list->ncscd, list->nseg);
1438984a2ea9SAlexander Motin 		while (list->curseg < list->nseg) {
1439984a2ea9SAlexander Motin 			seg = list->seg[list->curseg];
1440984a2ea9SAlexander Motin 			switch (seg->type_code) {
1441984a2ea9SAlexander Motin 			case EC_SEG_B2B:
1442984a2ea9SAlexander Motin 				retval = tpc_process_b2b(list);
1443984a2ea9SAlexander Motin 				break;
1444984a2ea9SAlexander Motin 			case EC_SEG_VERIFY:
1445984a2ea9SAlexander Motin 				retval = tpc_process_verify(list);
1446984a2ea9SAlexander Motin 				break;
1447984a2ea9SAlexander Motin 			case EC_SEG_REGISTER_KEY:
1448984a2ea9SAlexander Motin 				retval = tpc_process_register_key(list);
1449984a2ea9SAlexander Motin 				break;
1450984a2ea9SAlexander Motin 			default:
1451042e9bdcSAlexander Motin 				scsi_ulto4b(list->curseg, csi);
1452984a2ea9SAlexander Motin 				ctl_set_sense(ctsio, /*current_error*/ 1,
1453984a2ea9SAlexander Motin 				    /*sense_key*/ SSD_KEY_COPY_ABORTED,
1454042e9bdcSAlexander Motin 				    /*asc*/ 0x26, /*ascq*/ 0x09,
1455862aedb0SAlexander Motin 				    SSD_ELEM_COMMAND, sizeof(csi), csi,
1456042e9bdcSAlexander Motin 				    SSD_ELEM_NONE);
1457984a2ea9SAlexander Motin 				goto done;
1458984a2ea9SAlexander Motin 			}
1459984a2ea9SAlexander Motin 			if (retval == CTL_RETVAL_QUEUED)
1460984a2ea9SAlexander Motin 				return;
1461984a2ea9SAlexander Motin 			if (retval == CTL_RETVAL_ERROR) {
1462984a2ea9SAlexander Motin 				list->error = 1;
1463984a2ea9SAlexander Motin 				goto done;
1464984a2ea9SAlexander Motin 			}
1465984a2ea9SAlexander Motin 			list->curseg++;
1466984a2ea9SAlexander Motin 			list->stage = 0;
1467984a2ea9SAlexander Motin 		}
146825eee848SAlexander Motin 	}
1469984a2ea9SAlexander Motin 
1470984a2ea9SAlexander Motin 	ctl_set_success(ctsio);
1471984a2ea9SAlexander Motin 
1472984a2ea9SAlexander Motin done:
1473984a2ea9SAlexander Motin //printf("ZZZ done\n");
1474c5c60595SAlexander Motin 	free(list->params, M_CTL);
1475c5c60595SAlexander Motin 	list->params = NULL;
147625eee848SAlexander Motin 	if (list->token) {
14772d8b2876SAlexander Motin 		mtx_lock(&softc->tpc_lock);
147825eee848SAlexander Motin 		if (--list->token->active == 0)
147925eee848SAlexander Motin 			list->token->last_active = time_uptime;
14802d8b2876SAlexander Motin 		mtx_unlock(&softc->tpc_lock);
148125eee848SAlexander Motin 		list->token = NULL;
148225eee848SAlexander Motin 	}
1483984a2ea9SAlexander Motin 	mtx_lock(&lun->lun_lock);
1484984a2ea9SAlexander Motin 	if ((list->flags & EC_LIST_ID_USAGE_MASK) == EC_LIST_ID_USAGE_NONE) {
1485984a2ea9SAlexander Motin 		TAILQ_REMOVE(&lun->tpc_lists, list, links);
1486984a2ea9SAlexander Motin 		free(list, M_CTL);
1487984a2ea9SAlexander Motin 	} else {
1488984a2ea9SAlexander Motin 		list->completed = 1;
148925eee848SAlexander Motin 		list->last_active = time_uptime;
1490984a2ea9SAlexander Motin 		list->sense_data = ctsio->sense_data;
1491984a2ea9SAlexander Motin 		list->sense_len = ctsio->sense_len;
1492984a2ea9SAlexander Motin 		list->scsi_status = ctsio->scsi_status;
1493984a2ea9SAlexander Motin 	}
1494984a2ea9SAlexander Motin 	mtx_unlock(&lun->lun_lock);
1495984a2ea9SAlexander Motin 
1496984a2ea9SAlexander Motin 	ctl_done((union ctl_io *)ctsio);
1497984a2ea9SAlexander Motin }
1498984a2ea9SAlexander Motin 
1499984a2ea9SAlexander Motin /*
1500984a2ea9SAlexander Motin  * For any sort of check condition, busy, etc., we just retry.  We do not
1501984a2ea9SAlexander Motin  * decrement the retry count for unit attention type errors.  These are
1502984a2ea9SAlexander Motin  * normal, and we want to save the retry count for "real" errors.  Otherwise,
1503984a2ea9SAlexander Motin  * we could end up with situations where a command will succeed in some
1504984a2ea9SAlexander Motin  * situations and fail in others, depending on whether a unit attention is
1505984a2ea9SAlexander Motin  * pending.  Also, some of our error recovery actions, most notably the
1506984a2ea9SAlexander Motin  * LUN reset action, will cause a unit attention.
1507984a2ea9SAlexander Motin  *
1508984a2ea9SAlexander Motin  * We can add more detail here later if necessary.
1509984a2ea9SAlexander Motin  */
1510984a2ea9SAlexander Motin static tpc_error_action
tpc_checkcond_parse(union ctl_io * io)1511984a2ea9SAlexander Motin tpc_checkcond_parse(union ctl_io *io)
1512984a2ea9SAlexander Motin {
1513984a2ea9SAlexander Motin 	tpc_error_action error_action;
1514984a2ea9SAlexander Motin 	int error_code, sense_key, asc, ascq;
1515984a2ea9SAlexander Motin 
1516984a2ea9SAlexander Motin 	/*
1517984a2ea9SAlexander Motin 	 * Default to retrying the command.
1518984a2ea9SAlexander Motin 	 */
1519984a2ea9SAlexander Motin 	error_action = TPC_ERR_RETRY;
1520984a2ea9SAlexander Motin 
1521984a2ea9SAlexander Motin 	scsi_extract_sense_len(&io->scsiio.sense_data,
1522984a2ea9SAlexander Motin 			       io->scsiio.sense_len,
1523984a2ea9SAlexander Motin 			       &error_code,
1524984a2ea9SAlexander Motin 			       &sense_key,
1525984a2ea9SAlexander Motin 			       &asc,
1526984a2ea9SAlexander Motin 			       &ascq,
1527984a2ea9SAlexander Motin 			       /*show_errors*/ 1);
1528984a2ea9SAlexander Motin 
1529984a2ea9SAlexander Motin 	switch (error_code) {
1530984a2ea9SAlexander Motin 	case SSD_DEFERRED_ERROR:
1531984a2ea9SAlexander Motin 	case SSD_DESC_DEFERRED_ERROR:
1532984a2ea9SAlexander Motin 		error_action |= TPC_ERR_NO_DECREMENT;
1533984a2ea9SAlexander Motin 		break;
1534984a2ea9SAlexander Motin 	case SSD_CURRENT_ERROR:
1535984a2ea9SAlexander Motin 	case SSD_DESC_CURRENT_ERROR:
1536984a2ea9SAlexander Motin 	default:
1537984a2ea9SAlexander Motin 		switch (sense_key) {
1538984a2ea9SAlexander Motin 		case SSD_KEY_UNIT_ATTENTION:
1539984a2ea9SAlexander Motin 			error_action |= TPC_ERR_NO_DECREMENT;
1540984a2ea9SAlexander Motin 			break;
1541984a2ea9SAlexander Motin 		case SSD_KEY_HARDWARE_ERROR:
1542984a2ea9SAlexander Motin 			/*
1543984a2ea9SAlexander Motin 			 * This is our generic "something bad happened"
1544984a2ea9SAlexander Motin 			 * error code.  It often isn't recoverable.
1545984a2ea9SAlexander Motin 			 */
1546984a2ea9SAlexander Motin 			if ((asc == 0x44) && (ascq == 0x00))
1547984a2ea9SAlexander Motin 				error_action = TPC_ERR_FAIL;
1548984a2ea9SAlexander Motin 			break;
1549984a2ea9SAlexander Motin 		case SSD_KEY_NOT_READY:
1550984a2ea9SAlexander Motin 			/*
1551984a2ea9SAlexander Motin 			 * If the LUN is powered down, there likely isn't
1552984a2ea9SAlexander Motin 			 * much point in retrying right now.
1553984a2ea9SAlexander Motin 			 */
1554984a2ea9SAlexander Motin 			if ((asc == 0x04) && (ascq == 0x02))
1555984a2ea9SAlexander Motin 				error_action = TPC_ERR_FAIL;
1556984a2ea9SAlexander Motin 			/*
1557984a2ea9SAlexander Motin 			 * If the LUN is offline, there probably isn't much
1558984a2ea9SAlexander Motin 			 * point in retrying, either.
1559984a2ea9SAlexander Motin 			 */
1560984a2ea9SAlexander Motin 			if ((asc == 0x04) && (ascq == 0x03))
1561984a2ea9SAlexander Motin 				error_action = TPC_ERR_FAIL;
1562984a2ea9SAlexander Motin 			break;
1563984a2ea9SAlexander Motin 		}
1564984a2ea9SAlexander Motin 	}
1565984a2ea9SAlexander Motin 	return (error_action);
1566984a2ea9SAlexander Motin }
1567984a2ea9SAlexander Motin 
1568984a2ea9SAlexander Motin static tpc_error_action
tpc_error_parse(union ctl_io * io)1569984a2ea9SAlexander Motin tpc_error_parse(union ctl_io *io)
1570984a2ea9SAlexander Motin {
1571984a2ea9SAlexander Motin 	tpc_error_action error_action = TPC_ERR_RETRY;
1572984a2ea9SAlexander Motin 
1573984a2ea9SAlexander Motin 	switch (io->io_hdr.io_type) {
1574984a2ea9SAlexander Motin 	case CTL_IO_SCSI:
1575984a2ea9SAlexander Motin 		switch (io->io_hdr.status & CTL_STATUS_MASK) {
1576984a2ea9SAlexander Motin 		case CTL_SCSI_ERROR:
1577984a2ea9SAlexander Motin 			switch (io->scsiio.scsi_status) {
1578984a2ea9SAlexander Motin 			case SCSI_STATUS_CHECK_COND:
1579984a2ea9SAlexander Motin 				error_action = tpc_checkcond_parse(io);
1580984a2ea9SAlexander Motin 				break;
1581984a2ea9SAlexander Motin 			default:
1582984a2ea9SAlexander Motin 				break;
1583984a2ea9SAlexander Motin 			}
1584984a2ea9SAlexander Motin 			break;
1585984a2ea9SAlexander Motin 		default:
1586984a2ea9SAlexander Motin 			break;
1587984a2ea9SAlexander Motin 		}
1588984a2ea9SAlexander Motin 		break;
1589984a2ea9SAlexander Motin 	case CTL_IO_TASK:
1590984a2ea9SAlexander Motin 		break;
1591984a2ea9SAlexander Motin 	default:
1592984a2ea9SAlexander Motin 		panic("%s: invalid ctl_io type %d\n", __func__,
1593984a2ea9SAlexander Motin 		      io->io_hdr.io_type);
1594984a2ea9SAlexander Motin 		break;
1595984a2ea9SAlexander Motin 	}
1596984a2ea9SAlexander Motin 	return (error_action);
1597984a2ea9SAlexander Motin }
1598984a2ea9SAlexander Motin 
1599984a2ea9SAlexander Motin void
tpc_done(union ctl_io * io)1600984a2ea9SAlexander Motin tpc_done(union ctl_io *io)
1601984a2ea9SAlexander Motin {
1602984a2ea9SAlexander Motin 	struct tpc_io *tio, *tior;
1603984a2ea9SAlexander Motin 
1604984a2ea9SAlexander Motin 	/*
1605984a2ea9SAlexander Motin 	 * Very minimal retry logic.  We basically retry if we got an error
1606984a2ea9SAlexander Motin 	 * back, and the retry count is greater than 0.  If we ever want
1607984a2ea9SAlexander Motin 	 * more sophisticated initiator type behavior, the CAM error
1608984a2ea9SAlexander Motin 	 * recovery code in ../common might be helpful.
1609984a2ea9SAlexander Motin 	 */
1610984a2ea9SAlexander Motin 	tio = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1611984a2ea9SAlexander Motin 	if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
1612984a2ea9SAlexander Motin 	 && (io->io_hdr.retries > 0)) {
1613984a2ea9SAlexander Motin 		ctl_io_status old_status;
1614984a2ea9SAlexander Motin 		tpc_error_action error_action;
1615984a2ea9SAlexander Motin 
1616984a2ea9SAlexander Motin 		error_action = tpc_error_parse(io);
1617984a2ea9SAlexander Motin 		switch (error_action & TPC_ERR_MASK) {
1618984a2ea9SAlexander Motin 		case TPC_ERR_FAIL:
1619984a2ea9SAlexander Motin 			break;
1620984a2ea9SAlexander Motin 		case TPC_ERR_RETRY:
1621984a2ea9SAlexander Motin 		default:
1622984a2ea9SAlexander Motin 			if ((error_action & TPC_ERR_NO_DECREMENT) == 0)
1623984a2ea9SAlexander Motin 				io->io_hdr.retries--;
1624984a2ea9SAlexander Motin 			old_status = io->io_hdr.status;
1625984a2ea9SAlexander Motin 			io->io_hdr.status = CTL_STATUS_NONE;
1626984a2ea9SAlexander Motin 			io->io_hdr.flags &= ~CTL_FLAG_ABORT;
1627984a2ea9SAlexander Motin 			io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1628984a2ea9SAlexander Motin 			if (tpcl_queue(io, tio->lun) != CTL_RETVAL_COMPLETE) {
1629812c9f48SAlexander Motin 				printf("%s: error returned from tpcl_queue()!\n",
1630984a2ea9SAlexander Motin 				       __func__);
1631984a2ea9SAlexander Motin 				io->io_hdr.status = old_status;
1632984a2ea9SAlexander Motin 			} else
1633984a2ea9SAlexander Motin 				return;
1634984a2ea9SAlexander Motin 		}
1635984a2ea9SAlexander Motin 	}
1636984a2ea9SAlexander Motin 
1637a3dd8378SAlexander Motin 	if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
1638984a2ea9SAlexander Motin 		tio->list->error = 1;
1639a3dd8378SAlexander Motin 		if (io->io_hdr.io_type == CTL_IO_SCSI &&
1640a3dd8378SAlexander Motin 		    (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) {
1641a3dd8378SAlexander Motin 			tio->list->fwd_scsi_status = io->scsiio.scsi_status;
1642a3dd8378SAlexander Motin 			tio->list->fwd_sense_data = io->scsiio.sense_data;
1643a3dd8378SAlexander Motin 			tio->list->fwd_sense_len = io->scsiio.sense_len;
1644a3dd8378SAlexander Motin 			tio->list->fwd_target = tio->target;
1645a3dd8378SAlexander Motin 			tio->list->fwd_cscd = tio->cscd;
1646a3dd8378SAlexander Motin 		}
1647a3dd8378SAlexander Motin 	} else
1648984a2ea9SAlexander Motin 		atomic_add_int(&tio->list->curops, 1);
1649984a2ea9SAlexander Motin 	if (!tio->list->error && !tio->list->abort) {
1650984a2ea9SAlexander Motin 		while ((tior = TAILQ_FIRST(&tio->run)) != NULL) {
1651984a2ea9SAlexander Motin 			TAILQ_REMOVE(&tio->run, tior, rlinks);
1652984a2ea9SAlexander Motin 			atomic_add_int(&tio->list->tbdio, 1);
1653984a2ea9SAlexander Motin 			if (tpcl_queue(tior->io, tior->lun) != CTL_RETVAL_COMPLETE)
1654984a2ea9SAlexander Motin 				panic("tpcl_queue() error");
1655984a2ea9SAlexander Motin 		}
1656984a2ea9SAlexander Motin 	}
1657984a2ea9SAlexander Motin 	if (atomic_fetchadd_int(&tio->list->tbdio, -1) == 1)
1658984a2ea9SAlexander Motin 		tpc_process(tio->list);
1659984a2ea9SAlexander Motin }
1660984a2ea9SAlexander Motin 
1661984a2ea9SAlexander Motin int
ctl_extended_copy_lid1(struct ctl_scsiio * ctsio)1662984a2ea9SAlexander Motin ctl_extended_copy_lid1(struct ctl_scsiio *ctsio)
1663984a2ea9SAlexander Motin {
16649cbbfd2fSAlexander Motin 	struct ctl_lun *lun = CTL_LUN(ctsio);
1665984a2ea9SAlexander Motin 	struct scsi_extended_copy *cdb;
1666984a2ea9SAlexander Motin 	struct scsi_extended_copy_lid1_data *data;
1667a3dd8378SAlexander Motin 	struct scsi_ec_cscd *cscd;
1668a3dd8378SAlexander Motin 	struct scsi_ec_segment *seg;
1669984a2ea9SAlexander Motin 	struct tpc_list *list, *tlist;
1670984a2ea9SAlexander Motin 	uint8_t *ptr;
16718951f055SMarcelo Araujo 	const char *value;
1672984a2ea9SAlexander Motin 	int len, off, lencscd, lenseg, leninl, nseg;
1673984a2ea9SAlexander Motin 
1674984a2ea9SAlexander Motin 	CTL_DEBUG_PRINT(("ctl_extended_copy_lid1\n"));
1675984a2ea9SAlexander Motin 
1676984a2ea9SAlexander Motin 	cdb = (struct scsi_extended_copy *)ctsio->cdb;
1677984a2ea9SAlexander Motin 	len = scsi_4btoul(cdb->length);
1678984a2ea9SAlexander Motin 
1679a65a997fSAlexander Motin 	if (len == 0) {
1680a65a997fSAlexander Motin 		ctl_set_success(ctsio);
1681a65a997fSAlexander Motin 		goto done;
1682a65a997fSAlexander Motin 	}
1683984a2ea9SAlexander Motin 	if (len < sizeof(struct scsi_extended_copy_lid1_data) ||
1684984a2ea9SAlexander Motin 	    len > sizeof(struct scsi_extended_copy_lid1_data) +
1685984a2ea9SAlexander Motin 	    TPC_MAX_LIST + TPC_MAX_INLINE) {
1686984a2ea9SAlexander Motin 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
1687984a2ea9SAlexander Motin 		    /*field*/ 9, /*bit_valid*/ 0, /*bit*/ 0);
1688984a2ea9SAlexander Motin 		goto done;
1689984a2ea9SAlexander Motin 	}
1690984a2ea9SAlexander Motin 
1691984a2ea9SAlexander Motin 	/*
1692984a2ea9SAlexander Motin 	 * If we've got a kernel request that hasn't been malloced yet,
1693984a2ea9SAlexander Motin 	 * malloc it and tell the caller the data buffer is here.
1694984a2ea9SAlexander Motin 	 */
1695984a2ea9SAlexander Motin 	if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
1696984a2ea9SAlexander Motin 		ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
1697984a2ea9SAlexander Motin 		ctsio->kern_data_len = len;
1698984a2ea9SAlexander Motin 		ctsio->kern_total_len = len;
1699984a2ea9SAlexander Motin 		ctsio->kern_rel_offset = 0;
1700984a2ea9SAlexander Motin 		ctsio->kern_sg_entries = 0;
1701984a2ea9SAlexander Motin 		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
1702984a2ea9SAlexander Motin 		ctsio->be_move_done = ctl_config_move_done;
1703984a2ea9SAlexander Motin 		ctl_datamove((union ctl_io *)ctsio);
1704984a2ea9SAlexander Motin 
1705984a2ea9SAlexander Motin 		return (CTL_RETVAL_COMPLETE);
1706984a2ea9SAlexander Motin 	}
1707984a2ea9SAlexander Motin 
1708984a2ea9SAlexander Motin 	data = (struct scsi_extended_copy_lid1_data *)ctsio->kern_data_ptr;
1709984a2ea9SAlexander Motin 	lencscd = scsi_2btoul(data->cscd_list_length);
1710984a2ea9SAlexander Motin 	lenseg = scsi_4btoul(data->segment_list_length);
1711984a2ea9SAlexander Motin 	leninl = scsi_4btoul(data->inline_data_length);
1712984a2ea9SAlexander Motin 	if (lencscd > TPC_MAX_CSCDS * sizeof(struct scsi_ec_cscd)) {
1713984a2ea9SAlexander Motin 		ctl_set_sense(ctsio, /*current_error*/ 1,
1714984a2ea9SAlexander Motin 		    /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1715984a2ea9SAlexander Motin 		    /*asc*/ 0x26, /*ascq*/ 0x06, SSD_ELEM_NONE);
1716984a2ea9SAlexander Motin 		goto done;
1717984a2ea9SAlexander Motin 	}
1718a65a997fSAlexander Motin 	if (lenseg > TPC_MAX_SEGS * sizeof(struct scsi_ec_segment)) {
1719a65a997fSAlexander Motin 		ctl_set_sense(ctsio, /*current_error*/ 1,
1720a65a997fSAlexander Motin 		    /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1721a65a997fSAlexander Motin 		    /*asc*/ 0x26, /*ascq*/ 0x08, SSD_ELEM_NONE);
1722a65a997fSAlexander Motin 		goto done;
1723a65a997fSAlexander Motin 	}
1724a65a997fSAlexander Motin 	if (lencscd + lenseg > TPC_MAX_LIST ||
1725a65a997fSAlexander Motin 	    leninl > TPC_MAX_INLINE ||
1726a65a997fSAlexander Motin 	    len < sizeof(struct scsi_extended_copy_lid1_data) +
1727a65a997fSAlexander Motin 	     lencscd + lenseg + leninl) {
1728984a2ea9SAlexander Motin 		ctl_set_param_len_error(ctsio);
1729984a2ea9SAlexander Motin 		goto done;
1730984a2ea9SAlexander Motin 	}
1731984a2ea9SAlexander Motin 
1732984a2ea9SAlexander Motin 	list = malloc(sizeof(struct tpc_list), M_CTL, M_WAITOK | M_ZERO);
1733984a2ea9SAlexander Motin 	list->service_action = cdb->service_action;
17348951f055SMarcelo Araujo 	value = dnvlist_get_string(lun->be_lun->options, "insecure_tpc", NULL);
1735984a2ea9SAlexander Motin 	if (value != NULL && strcmp(value, "on") == 0)
1736984a2ea9SAlexander Motin 		list->init_port = -1;
1737984a2ea9SAlexander Motin 	else
1738984a2ea9SAlexander Motin 		list->init_port = ctsio->io_hdr.nexus.targ_port;
17397ac58230SAlexander Motin 	list->init_idx = ctl_get_initindex(&ctsio->io_hdr.nexus);
1740984a2ea9SAlexander Motin 	list->list_id = data->list_identifier;
1741984a2ea9SAlexander Motin 	list->flags = data->flags;
1742984a2ea9SAlexander Motin 	list->params = ctsio->kern_data_ptr;
1743984a2ea9SAlexander Motin 	list->cscd = (struct scsi_ec_cscd *)&data->data[0];
1744a3dd8378SAlexander Motin 	ptr = &data->data[0];
1745a3dd8378SAlexander Motin 	for (off = 0; off < lencscd; off += sizeof(struct scsi_ec_cscd)) {
1746a3dd8378SAlexander Motin 		cscd = (struct scsi_ec_cscd *)(ptr + off);
1747a3dd8378SAlexander Motin 		if (cscd->type_code != EC_CSCD_ID) {
1748a3dd8378SAlexander Motin 			free(list, M_CTL);
1749a3dd8378SAlexander Motin 			ctl_set_sense(ctsio, /*current_error*/ 1,
1750a3dd8378SAlexander Motin 			    /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1751a3dd8378SAlexander Motin 			    /*asc*/ 0x26, /*ascq*/ 0x07, SSD_ELEM_NONE);
1752a3dd8378SAlexander Motin 			goto done;
1753a3dd8378SAlexander Motin 		}
1754a3dd8378SAlexander Motin 	}
1755984a2ea9SAlexander Motin 	ptr = &data->data[lencscd];
1756984a2ea9SAlexander Motin 	for (nseg = 0, off = 0; off < lenseg; nseg++) {
1757984a2ea9SAlexander Motin 		if (nseg >= TPC_MAX_SEGS) {
1758984a2ea9SAlexander Motin 			free(list, M_CTL);
1759984a2ea9SAlexander Motin 			ctl_set_sense(ctsio, /*current_error*/ 1,
1760984a2ea9SAlexander Motin 			    /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1761984a2ea9SAlexander Motin 			    /*asc*/ 0x26, /*ascq*/ 0x08, SSD_ELEM_NONE);
1762984a2ea9SAlexander Motin 			goto done;
1763984a2ea9SAlexander Motin 		}
1764a3dd8378SAlexander Motin 		seg = (struct scsi_ec_segment *)(ptr + off);
1765a3dd8378SAlexander Motin 		if (seg->type_code != EC_SEG_B2B &&
1766a3dd8378SAlexander Motin 		    seg->type_code != EC_SEG_VERIFY &&
1767a3dd8378SAlexander Motin 		    seg->type_code != EC_SEG_REGISTER_KEY) {
1768a3dd8378SAlexander Motin 			free(list, M_CTL);
1769a3dd8378SAlexander Motin 			ctl_set_sense(ctsio, /*current_error*/ 1,
1770a3dd8378SAlexander Motin 			    /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1771a3dd8378SAlexander Motin 			    /*asc*/ 0x26, /*ascq*/ 0x09, SSD_ELEM_NONE);
1772a3dd8378SAlexander Motin 			goto done;
1773a3dd8378SAlexander Motin 		}
1774a3dd8378SAlexander Motin 		list->seg[nseg] = seg;
1775984a2ea9SAlexander Motin 		off += sizeof(struct scsi_ec_segment) +
1776a3dd8378SAlexander Motin 		    scsi_2btoul(seg->descr_length);
1777984a2ea9SAlexander Motin 	}
1778984a2ea9SAlexander Motin 	list->inl = &data->data[lencscd + lenseg];
1779984a2ea9SAlexander Motin 	list->ncscd = lencscd / sizeof(struct scsi_ec_cscd);
1780984a2ea9SAlexander Motin 	list->nseg = nseg;
1781984a2ea9SAlexander Motin 	list->leninl = leninl;
1782984a2ea9SAlexander Motin 	list->ctsio = ctsio;
1783984a2ea9SAlexander Motin 	list->lun = lun;
1784984a2ea9SAlexander Motin 	mtx_lock(&lun->lun_lock);
1785984a2ea9SAlexander Motin 	if ((list->flags & EC_LIST_ID_USAGE_MASK) != EC_LIST_ID_USAGE_NONE) {
178643d2d719SAlexander Motin 		tlist = tpc_find_list(lun, list->list_id, list->init_idx);
1787984a2ea9SAlexander Motin 		if (tlist != NULL && !tlist->completed) {
1788984a2ea9SAlexander Motin 			mtx_unlock(&lun->lun_lock);
1789984a2ea9SAlexander Motin 			free(list, M_CTL);
1790984a2ea9SAlexander Motin 			ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
1791984a2ea9SAlexander Motin 			    /*command*/ 0, /*field*/ 0, /*bit_valid*/ 0,
1792984a2ea9SAlexander Motin 			    /*bit*/ 0);
1793984a2ea9SAlexander Motin 			goto done;
1794984a2ea9SAlexander Motin 		}
1795984a2ea9SAlexander Motin 		if (tlist != NULL) {
1796984a2ea9SAlexander Motin 			TAILQ_REMOVE(&lun->tpc_lists, tlist, links);
1797984a2ea9SAlexander Motin 			free(tlist, M_CTL);
1798984a2ea9SAlexander Motin 		}
1799984a2ea9SAlexander Motin 	}
1800984a2ea9SAlexander Motin 	TAILQ_INSERT_TAIL(&lun->tpc_lists, list, links);
1801984a2ea9SAlexander Motin 	mtx_unlock(&lun->lun_lock);
1802984a2ea9SAlexander Motin 
1803984a2ea9SAlexander Motin 	tpc_process(list);
1804984a2ea9SAlexander Motin 	return (CTL_RETVAL_COMPLETE);
1805984a2ea9SAlexander Motin 
1806984a2ea9SAlexander Motin done:
18072a72b593SAlexander Motin 	if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
18082a72b593SAlexander Motin 		free(ctsio->kern_data_ptr, M_CTL);
18092a72b593SAlexander Motin 		ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
18102a72b593SAlexander Motin 	}
1811984a2ea9SAlexander Motin 	ctl_done((union ctl_io *)ctsio);
1812984a2ea9SAlexander Motin 	return (CTL_RETVAL_COMPLETE);
1813984a2ea9SAlexander Motin }
1814984a2ea9SAlexander Motin 
1815984a2ea9SAlexander Motin int
ctl_extended_copy_lid4(struct ctl_scsiio * ctsio)1816984a2ea9SAlexander Motin ctl_extended_copy_lid4(struct ctl_scsiio *ctsio)
1817984a2ea9SAlexander Motin {
18189cbbfd2fSAlexander Motin 	struct ctl_lun *lun = CTL_LUN(ctsio);
1819984a2ea9SAlexander Motin 	struct scsi_extended_copy *cdb;
1820984a2ea9SAlexander Motin 	struct scsi_extended_copy_lid4_data *data;
1821a3dd8378SAlexander Motin 	struct scsi_ec_cscd *cscd;
1822a3dd8378SAlexander Motin 	struct scsi_ec_segment *seg;
1823984a2ea9SAlexander Motin 	struct tpc_list *list, *tlist;
1824984a2ea9SAlexander Motin 	uint8_t *ptr;
18258951f055SMarcelo Araujo 	const char *value;
1826984a2ea9SAlexander Motin 	int len, off, lencscd, lenseg, leninl, nseg;
1827984a2ea9SAlexander Motin 
1828984a2ea9SAlexander Motin 	CTL_DEBUG_PRINT(("ctl_extended_copy_lid4\n"));
1829984a2ea9SAlexander Motin 
1830984a2ea9SAlexander Motin 	cdb = (struct scsi_extended_copy *)ctsio->cdb;
1831984a2ea9SAlexander Motin 	len = scsi_4btoul(cdb->length);
1832984a2ea9SAlexander Motin 
1833a65a997fSAlexander Motin 	if (len == 0) {
1834a65a997fSAlexander Motin 		ctl_set_success(ctsio);
1835a65a997fSAlexander Motin 		goto done;
1836a65a997fSAlexander Motin 	}
1837984a2ea9SAlexander Motin 	if (len < sizeof(struct scsi_extended_copy_lid4_data) ||
1838984a2ea9SAlexander Motin 	    len > sizeof(struct scsi_extended_copy_lid4_data) +
1839984a2ea9SAlexander Motin 	    TPC_MAX_LIST + TPC_MAX_INLINE) {
1840984a2ea9SAlexander Motin 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
1841984a2ea9SAlexander Motin 		    /*field*/ 9, /*bit_valid*/ 0, /*bit*/ 0);
1842984a2ea9SAlexander Motin 		goto done;
1843984a2ea9SAlexander Motin 	}
1844984a2ea9SAlexander Motin 
1845984a2ea9SAlexander Motin 	/*
1846984a2ea9SAlexander Motin 	 * If we've got a kernel request that hasn't been malloced yet,
1847984a2ea9SAlexander Motin 	 * malloc it and tell the caller the data buffer is here.
1848984a2ea9SAlexander Motin 	 */
1849984a2ea9SAlexander Motin 	if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
1850984a2ea9SAlexander Motin 		ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
1851984a2ea9SAlexander Motin 		ctsio->kern_data_len = len;
1852984a2ea9SAlexander Motin 		ctsio->kern_total_len = len;
1853984a2ea9SAlexander Motin 		ctsio->kern_rel_offset = 0;
1854984a2ea9SAlexander Motin 		ctsio->kern_sg_entries = 0;
1855984a2ea9SAlexander Motin 		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
1856984a2ea9SAlexander Motin 		ctsio->be_move_done = ctl_config_move_done;
1857984a2ea9SAlexander Motin 		ctl_datamove((union ctl_io *)ctsio);
1858984a2ea9SAlexander Motin 
1859984a2ea9SAlexander Motin 		return (CTL_RETVAL_COMPLETE);
1860984a2ea9SAlexander Motin 	}
1861984a2ea9SAlexander Motin 
1862984a2ea9SAlexander Motin 	data = (struct scsi_extended_copy_lid4_data *)ctsio->kern_data_ptr;
1863984a2ea9SAlexander Motin 	lencscd = scsi_2btoul(data->cscd_list_length);
1864984a2ea9SAlexander Motin 	lenseg = scsi_2btoul(data->segment_list_length);
1865984a2ea9SAlexander Motin 	leninl = scsi_2btoul(data->inline_data_length);
1866984a2ea9SAlexander Motin 	if (lencscd > TPC_MAX_CSCDS * sizeof(struct scsi_ec_cscd)) {
1867984a2ea9SAlexander Motin 		ctl_set_sense(ctsio, /*current_error*/ 1,
1868984a2ea9SAlexander Motin 		    /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1869984a2ea9SAlexander Motin 		    /*asc*/ 0x26, /*ascq*/ 0x06, SSD_ELEM_NONE);
1870984a2ea9SAlexander Motin 		goto done;
1871984a2ea9SAlexander Motin 	}
1872a65a997fSAlexander Motin 	if (lenseg > TPC_MAX_SEGS * sizeof(struct scsi_ec_segment)) {
1873a65a997fSAlexander Motin 		ctl_set_sense(ctsio, /*current_error*/ 1,
1874a65a997fSAlexander Motin 		    /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1875a65a997fSAlexander Motin 		    /*asc*/ 0x26, /*ascq*/ 0x08, SSD_ELEM_NONE);
1876a65a997fSAlexander Motin 		goto done;
1877a65a997fSAlexander Motin 	}
1878a65a997fSAlexander Motin 	if (lencscd + lenseg > TPC_MAX_LIST ||
1879a65a997fSAlexander Motin 	    leninl > TPC_MAX_INLINE ||
1880a65a997fSAlexander Motin 	    len < sizeof(struct scsi_extended_copy_lid1_data) +
1881a65a997fSAlexander Motin 	     lencscd + lenseg + leninl) {
1882984a2ea9SAlexander Motin 		ctl_set_param_len_error(ctsio);
1883984a2ea9SAlexander Motin 		goto done;
1884984a2ea9SAlexander Motin 	}
1885984a2ea9SAlexander Motin 
1886984a2ea9SAlexander Motin 	list = malloc(sizeof(struct tpc_list), M_CTL, M_WAITOK | M_ZERO);
1887984a2ea9SAlexander Motin 	list->service_action = cdb->service_action;
18888951f055SMarcelo Araujo 	value = dnvlist_get_string(lun->be_lun->options, "insecure_tpc", NULL);
1889984a2ea9SAlexander Motin 	if (value != NULL && strcmp(value, "on") == 0)
1890984a2ea9SAlexander Motin 		list->init_port = -1;
1891984a2ea9SAlexander Motin 	else
1892984a2ea9SAlexander Motin 		list->init_port = ctsio->io_hdr.nexus.targ_port;
18937ac58230SAlexander Motin 	list->init_idx = ctl_get_initindex(&ctsio->io_hdr.nexus);
1894984a2ea9SAlexander Motin 	list->list_id = scsi_4btoul(data->list_identifier);
1895984a2ea9SAlexander Motin 	list->flags = data->flags;
1896984a2ea9SAlexander Motin 	list->params = ctsio->kern_data_ptr;
1897984a2ea9SAlexander Motin 	list->cscd = (struct scsi_ec_cscd *)&data->data[0];
1898a3dd8378SAlexander Motin 	ptr = &data->data[0];
1899a3dd8378SAlexander Motin 	for (off = 0; off < lencscd; off += sizeof(struct scsi_ec_cscd)) {
1900a3dd8378SAlexander Motin 		cscd = (struct scsi_ec_cscd *)(ptr + off);
1901a3dd8378SAlexander Motin 		if (cscd->type_code != EC_CSCD_ID) {
1902a3dd8378SAlexander Motin 			free(list, M_CTL);
1903a3dd8378SAlexander Motin 			ctl_set_sense(ctsio, /*current_error*/ 1,
1904a3dd8378SAlexander Motin 			    /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1905a3dd8378SAlexander Motin 			    /*asc*/ 0x26, /*ascq*/ 0x07, SSD_ELEM_NONE);
1906a3dd8378SAlexander Motin 			goto done;
1907a3dd8378SAlexander Motin 		}
1908a3dd8378SAlexander Motin 	}
1909984a2ea9SAlexander Motin 	ptr = &data->data[lencscd];
1910984a2ea9SAlexander Motin 	for (nseg = 0, off = 0; off < lenseg; nseg++) {
1911984a2ea9SAlexander Motin 		if (nseg >= TPC_MAX_SEGS) {
1912984a2ea9SAlexander Motin 			free(list, M_CTL);
1913984a2ea9SAlexander Motin 			ctl_set_sense(ctsio, /*current_error*/ 1,
1914984a2ea9SAlexander Motin 			    /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1915984a2ea9SAlexander Motin 			    /*asc*/ 0x26, /*ascq*/ 0x08, SSD_ELEM_NONE);
1916984a2ea9SAlexander Motin 			goto done;
1917984a2ea9SAlexander Motin 		}
1918a3dd8378SAlexander Motin 		seg = (struct scsi_ec_segment *)(ptr + off);
1919a3dd8378SAlexander Motin 		if (seg->type_code != EC_SEG_B2B &&
1920a3dd8378SAlexander Motin 		    seg->type_code != EC_SEG_VERIFY &&
1921a3dd8378SAlexander Motin 		    seg->type_code != EC_SEG_REGISTER_KEY) {
1922a3dd8378SAlexander Motin 			free(list, M_CTL);
1923a3dd8378SAlexander Motin 			ctl_set_sense(ctsio, /*current_error*/ 1,
1924a3dd8378SAlexander Motin 			    /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1925a3dd8378SAlexander Motin 			    /*asc*/ 0x26, /*ascq*/ 0x09, SSD_ELEM_NONE);
1926a3dd8378SAlexander Motin 			goto done;
1927a3dd8378SAlexander Motin 		}
1928a3dd8378SAlexander Motin 		list->seg[nseg] = seg;
1929984a2ea9SAlexander Motin 		off += sizeof(struct scsi_ec_segment) +
1930a3dd8378SAlexander Motin 		    scsi_2btoul(seg->descr_length);
1931984a2ea9SAlexander Motin 	}
1932984a2ea9SAlexander Motin 	list->inl = &data->data[lencscd + lenseg];
1933984a2ea9SAlexander Motin 	list->ncscd = lencscd / sizeof(struct scsi_ec_cscd);
1934984a2ea9SAlexander Motin 	list->nseg = nseg;
1935984a2ea9SAlexander Motin 	list->leninl = leninl;
1936984a2ea9SAlexander Motin 	list->ctsio = ctsio;
1937984a2ea9SAlexander Motin 	list->lun = lun;
1938984a2ea9SAlexander Motin 	mtx_lock(&lun->lun_lock);
1939984a2ea9SAlexander Motin 	if ((list->flags & EC_LIST_ID_USAGE_MASK) != EC_LIST_ID_USAGE_NONE) {
194043d2d719SAlexander Motin 		tlist = tpc_find_list(lun, list->list_id, list->init_idx);
1941984a2ea9SAlexander Motin 		if (tlist != NULL && !tlist->completed) {
1942984a2ea9SAlexander Motin 			mtx_unlock(&lun->lun_lock);
1943984a2ea9SAlexander Motin 			free(list, M_CTL);
1944984a2ea9SAlexander Motin 			ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
1945984a2ea9SAlexander Motin 			    /*command*/ 0, /*field*/ 0, /*bit_valid*/ 0,
1946984a2ea9SAlexander Motin 			    /*bit*/ 0);
1947984a2ea9SAlexander Motin 			goto done;
1948984a2ea9SAlexander Motin 		}
1949984a2ea9SAlexander Motin 		if (tlist != NULL) {
1950984a2ea9SAlexander Motin 			TAILQ_REMOVE(&lun->tpc_lists, tlist, links);
1951984a2ea9SAlexander Motin 			free(tlist, M_CTL);
1952984a2ea9SAlexander Motin 		}
1953984a2ea9SAlexander Motin 	}
1954984a2ea9SAlexander Motin 	TAILQ_INSERT_TAIL(&lun->tpc_lists, list, links);
1955984a2ea9SAlexander Motin 	mtx_unlock(&lun->lun_lock);
1956984a2ea9SAlexander Motin 
1957984a2ea9SAlexander Motin 	tpc_process(list);
1958984a2ea9SAlexander Motin 	return (CTL_RETVAL_COMPLETE);
1959984a2ea9SAlexander Motin 
1960984a2ea9SAlexander Motin done:
19612a72b593SAlexander Motin 	if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
19622a72b593SAlexander Motin 		free(ctsio->kern_data_ptr, M_CTL);
19632a72b593SAlexander Motin 		ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
19642a72b593SAlexander Motin 	}
1965984a2ea9SAlexander Motin 	ctl_done((union ctl_io *)ctsio);
1966984a2ea9SAlexander Motin 	return (CTL_RETVAL_COMPLETE);
1967984a2ea9SAlexander Motin }
1968984a2ea9SAlexander Motin 
196925eee848SAlexander Motin static void
tpc_create_token(struct ctl_lun * lun,struct ctl_port * port,off_t len,struct scsi_token * token)197025eee848SAlexander Motin tpc_create_token(struct ctl_lun *lun, struct ctl_port *port, off_t len,
197125eee848SAlexander Motin     struct scsi_token *token)
197225eee848SAlexander Motin {
197325eee848SAlexander Motin 	static int id = 0;
197425eee848SAlexander Motin 	struct scsi_vpd_id_descriptor *idd = NULL;
19754ab4d687SAlexander Motin 	struct scsi_ec_cscd_id *cscd;
19760b060244SAlexander Motin 	struct scsi_read_capacity_data_long *dtsd;
197725eee848SAlexander Motin 	int targid_len;
197825eee848SAlexander Motin 
197925eee848SAlexander Motin 	scsi_ulto4b(ROD_TYPE_AUR, token->type);
198025eee848SAlexander Motin 	scsi_ulto2b(0x01f8, token->length);
198125eee848SAlexander Motin 	scsi_u64to8b(atomic_fetchadd_int(&id, 1), &token->body[0]);
198225eee848SAlexander Motin 	if (lun->lun_devid)
198325eee848SAlexander Motin 		idd = scsi_get_devid_desc((struct scsi_vpd_id_descriptor *)
198425eee848SAlexander Motin 		    lun->lun_devid->data, lun->lun_devid->len,
198525eee848SAlexander Motin 		    scsi_devid_is_lun_naa);
198625eee848SAlexander Motin 	if (idd == NULL && lun->lun_devid)
198725eee848SAlexander Motin 		idd = scsi_get_devid_desc((struct scsi_vpd_id_descriptor *)
198825eee848SAlexander Motin 		    lun->lun_devid->data, lun->lun_devid->len,
198925eee848SAlexander Motin 		    scsi_devid_is_lun_eui64);
19904ab4d687SAlexander Motin 	if (idd != NULL) {
19914ab4d687SAlexander Motin 		cscd = (struct scsi_ec_cscd_id *)&token->body[8];
19924ab4d687SAlexander Motin 		cscd->type_code = EC_CSCD_ID;
19934ab4d687SAlexander Motin 		cscd->luidt_pdt = T_DIRECT;
19944ab4d687SAlexander Motin 		memcpy(&cscd->codeset, idd, 4 + idd->length);
19950b060244SAlexander Motin 		scsi_ulto3b(lun->be_lun->blocksize, cscd->dtsp.block_length);
19964ab4d687SAlexander Motin 	}
19970b060244SAlexander Motin 	scsi_u64to8b(0, &token->body[40]); /* XXX: Should be 128bit value. */
199825eee848SAlexander Motin 	scsi_u64to8b(len, &token->body[48]);
19990b060244SAlexander Motin 
20000b060244SAlexander Motin 	/* ROD token device type specific data (RC16 without first field) */
20010b060244SAlexander Motin 	dtsd = (struct scsi_read_capacity_data_long *)&token->body[88 - 8];
20020b060244SAlexander Motin 	scsi_ulto4b(lun->be_lun->blocksize, dtsd->length);
20030b060244SAlexander Motin 	dtsd->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
20040b060244SAlexander Motin 	scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, dtsd->lalba_lbp);
20050b060244SAlexander Motin 	if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
20060b060244SAlexander Motin 		dtsd->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
20070b060244SAlexander Motin 
200825eee848SAlexander Motin 	if (port->target_devid) {
200925eee848SAlexander Motin 		targid_len = port->target_devid->len;
201025eee848SAlexander Motin 		memcpy(&token->body[120], port->target_devid->data, targid_len);
201125eee848SAlexander Motin 	} else
201225eee848SAlexander Motin 		targid_len = 32;
201325eee848SAlexander Motin 	arc4rand(&token->body[120 + targid_len], 384 - targid_len, 0);
201425eee848SAlexander Motin };
201525eee848SAlexander Motin 
201625eee848SAlexander Motin int
ctl_populate_token(struct ctl_scsiio * ctsio)201725eee848SAlexander Motin ctl_populate_token(struct ctl_scsiio *ctsio)
201825eee848SAlexander Motin {
20199cbbfd2fSAlexander Motin 	struct ctl_softc *softc = CTL_SOFTC(ctsio);
20209cbbfd2fSAlexander Motin 	struct ctl_port *port = CTL_PORT(ctsio);
20219cbbfd2fSAlexander Motin 	struct ctl_lun *lun = CTL_LUN(ctsio);
202225eee848SAlexander Motin 	struct scsi_populate_token *cdb;
202325eee848SAlexander Motin 	struct scsi_populate_token_data *data;
202425eee848SAlexander Motin 	struct tpc_list *list, *tlist;
202525eee848SAlexander Motin 	struct tpc_token *token;
202632920cbfSAlexander Motin 	uint64_t lba;
20273eb7651aSAlexander Motin 	int len, lendata, lendesc;
202825eee848SAlexander Motin 
202925eee848SAlexander Motin 	CTL_DEBUG_PRINT(("ctl_populate_token\n"));
203025eee848SAlexander Motin 
203125eee848SAlexander Motin 	cdb = (struct scsi_populate_token *)ctsio->cdb;
203225eee848SAlexander Motin 	len = scsi_4btoul(cdb->length);
203325eee848SAlexander Motin 
203425eee848SAlexander Motin 	if (len < sizeof(struct scsi_populate_token_data) ||
203525eee848SAlexander Motin 	    len > sizeof(struct scsi_populate_token_data) +
203625eee848SAlexander Motin 	     TPC_MAX_SEGS * sizeof(struct scsi_range_desc)) {
203725eee848SAlexander Motin 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
203825eee848SAlexander Motin 		    /*field*/ 9, /*bit_valid*/ 0, /*bit*/ 0);
203925eee848SAlexander Motin 		goto done;
204025eee848SAlexander Motin 	}
204125eee848SAlexander Motin 
204225eee848SAlexander Motin 	/*
204325eee848SAlexander Motin 	 * If we've got a kernel request that hasn't been malloced yet,
204425eee848SAlexander Motin 	 * malloc it and tell the caller the data buffer is here.
204525eee848SAlexander Motin 	 */
204625eee848SAlexander Motin 	if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
204725eee848SAlexander Motin 		ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
204825eee848SAlexander Motin 		ctsio->kern_data_len = len;
204925eee848SAlexander Motin 		ctsio->kern_total_len = len;
205025eee848SAlexander Motin 		ctsio->kern_rel_offset = 0;
205125eee848SAlexander Motin 		ctsio->kern_sg_entries = 0;
205225eee848SAlexander Motin 		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
205325eee848SAlexander Motin 		ctsio->be_move_done = ctl_config_move_done;
205425eee848SAlexander Motin 		ctl_datamove((union ctl_io *)ctsio);
205525eee848SAlexander Motin 
205625eee848SAlexander Motin 		return (CTL_RETVAL_COMPLETE);
205725eee848SAlexander Motin 	}
205825eee848SAlexander Motin 
205925eee848SAlexander Motin 	data = (struct scsi_populate_token_data *)ctsio->kern_data_ptr;
20603eb7651aSAlexander Motin 	lendata = scsi_2btoul(data->length);
20613eb7651aSAlexander Motin 	if (lendata < sizeof(struct scsi_populate_token_data) - 2 +
20623eb7651aSAlexander Motin 	    sizeof(struct scsi_range_desc)) {
206325eee848SAlexander Motin 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 0,
20643eb7651aSAlexander Motin 		    /*field*/ 0, /*bit_valid*/ 0, /*bit*/ 0);
20653eb7651aSAlexander Motin 		goto done;
20663eb7651aSAlexander Motin 	}
20673eb7651aSAlexander Motin 	lendesc = scsi_2btoul(data->range_descriptor_length);
20683eb7651aSAlexander Motin 	if (lendesc < sizeof(struct scsi_range_desc) ||
20693eb7651aSAlexander Motin 	    len < sizeof(struct scsi_populate_token_data) + lendesc ||
20703eb7651aSAlexander Motin 	    lendata < sizeof(struct scsi_populate_token_data) - 2 + lendesc) {
20713eb7651aSAlexander Motin 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 0,
20723eb7651aSAlexander Motin 		    /*field*/ 14, /*bit_valid*/ 0, /*bit*/ 0);
207325eee848SAlexander Motin 		goto done;
207425eee848SAlexander Motin 	}
207525eee848SAlexander Motin /*
207625eee848SAlexander Motin 	printf("PT(list=%u) flags=%x to=%d rt=%x len=%x\n",
207725eee848SAlexander Motin 	    scsi_4btoul(cdb->list_identifier),
207825eee848SAlexander Motin 	    data->flags, scsi_4btoul(data->inactivity_timeout),
207925eee848SAlexander Motin 	    scsi_4btoul(data->rod_type),
208025eee848SAlexander Motin 	    scsi_2btoul(data->range_descriptor_length));
208125eee848SAlexander Motin */
20823eb7651aSAlexander Motin 
20833eb7651aSAlexander Motin 	/* Validate INACTIVITY TIMEOUT field */
20843eb7651aSAlexander Motin 	if (scsi_4btoul(data->inactivity_timeout) > TPC_MAX_TOKEN_TIMEOUT) {
20853eb7651aSAlexander Motin 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
20863eb7651aSAlexander Motin 		    /*command*/ 0, /*field*/ 4, /*bit_valid*/ 0,
20873eb7651aSAlexander Motin 		    /*bit*/ 0);
20883eb7651aSAlexander Motin 		goto done;
20893eb7651aSAlexander Motin 	}
20903eb7651aSAlexander Motin 
20913eb7651aSAlexander Motin 	/* Validate ROD TYPE field */
209225eee848SAlexander Motin 	if ((data->flags & EC_PT_RTV) &&
209325eee848SAlexander Motin 	    scsi_4btoul(data->rod_type) != ROD_TYPE_AUR) {
209425eee848SAlexander Motin 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 0,
209525eee848SAlexander Motin 		    /*field*/ 8, /*bit_valid*/ 0, /*bit*/ 0);
209625eee848SAlexander Motin 		goto done;
209725eee848SAlexander Motin 	}
209825eee848SAlexander Motin 
2099e13f4248SAlexander Motin 	/* Validate list of ranges */
21008fadf660SAlexander Motin 	if (tpc_check_ranges_l(&data->desc[0],
2101e13f4248SAlexander Motin 	    scsi_2btoul(data->range_descriptor_length) /
210238618bf4SAlexander Motin 	    sizeof(struct scsi_range_desc),
210332920cbfSAlexander Motin 	    lun->be_lun->maxlba, &lba) != 0) {
210432920cbfSAlexander Motin 		ctl_set_lba_out_of_range(ctsio, lba);
21058fadf660SAlexander Motin 		goto done;
21068fadf660SAlexander Motin 	}
21078fadf660SAlexander Motin 	if (tpc_check_ranges_x(&data->desc[0],
21088fadf660SAlexander Motin 	    scsi_2btoul(data->range_descriptor_length) /
21098fadf660SAlexander Motin 	    sizeof(struct scsi_range_desc)) != 0) {
2110e13f4248SAlexander Motin 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 0,
2111e13f4248SAlexander Motin 		    /*command*/ 0, /*field*/ 0, /*bit_valid*/ 0,
2112e13f4248SAlexander Motin 		    /*bit*/ 0);
2113e13f4248SAlexander Motin 		goto done;
2114e13f4248SAlexander Motin 	}
2115e13f4248SAlexander Motin 
211625eee848SAlexander Motin 	list = malloc(sizeof(struct tpc_list), M_CTL, M_WAITOK | M_ZERO);
211725eee848SAlexander Motin 	list->service_action = cdb->service_action;
211825eee848SAlexander Motin 	list->init_port = ctsio->io_hdr.nexus.targ_port;
21197ac58230SAlexander Motin 	list->init_idx = ctl_get_initindex(&ctsio->io_hdr.nexus);
212025eee848SAlexander Motin 	list->list_id = scsi_4btoul(cdb->list_identifier);
212125eee848SAlexander Motin 	list->flags = data->flags;
212225eee848SAlexander Motin 	list->ctsio = ctsio;
212325eee848SAlexander Motin 	list->lun = lun;
212425eee848SAlexander Motin 	mtx_lock(&lun->lun_lock);
212525eee848SAlexander Motin 	tlist = tpc_find_list(lun, list->list_id, list->init_idx);
212625eee848SAlexander Motin 	if (tlist != NULL && !tlist->completed) {
212725eee848SAlexander Motin 		mtx_unlock(&lun->lun_lock);
212825eee848SAlexander Motin 		free(list, M_CTL);
212925eee848SAlexander Motin 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
213025eee848SAlexander Motin 		    /*command*/ 0, /*field*/ 0, /*bit_valid*/ 0,
213125eee848SAlexander Motin 		    /*bit*/ 0);
213225eee848SAlexander Motin 		goto done;
213325eee848SAlexander Motin 	}
213425eee848SAlexander Motin 	if (tlist != NULL) {
213525eee848SAlexander Motin 		TAILQ_REMOVE(&lun->tpc_lists, tlist, links);
213625eee848SAlexander Motin 		free(tlist, M_CTL);
213725eee848SAlexander Motin 	}
213825eee848SAlexander Motin 	TAILQ_INSERT_TAIL(&lun->tpc_lists, list, links);
213925eee848SAlexander Motin 	mtx_unlock(&lun->lun_lock);
214025eee848SAlexander Motin 
214125eee848SAlexander Motin 	token = malloc(sizeof(*token), M_CTL, M_WAITOK | M_ZERO);
214225eee848SAlexander Motin 	token->lun = lun->lun;
214325eee848SAlexander Motin 	token->blocksize = lun->be_lun->blocksize;
214425eee848SAlexander Motin 	token->params = ctsio->kern_data_ptr;
214525eee848SAlexander Motin 	token->range = &data->desc[0];
214625eee848SAlexander Motin 	token->nrange = scsi_2btoul(data->range_descriptor_length) /
214725eee848SAlexander Motin 	    sizeof(struct scsi_range_desc);
21480b060244SAlexander Motin 	list->cursectors = tpc_ranges_length(token->range, token->nrange);
21490b060244SAlexander Motin 	list->curbytes = (off_t)list->cursectors * lun->be_lun->blocksize;
215025eee848SAlexander Motin 	tpc_create_token(lun, port, list->curbytes,
215125eee848SAlexander Motin 	    (struct scsi_token *)token->token);
215225eee848SAlexander Motin 	token->active = 0;
215325eee848SAlexander Motin 	token->last_active = time_uptime;
215425eee848SAlexander Motin 	token->timeout = scsi_4btoul(data->inactivity_timeout);
215525eee848SAlexander Motin 	if (token->timeout == 0)
215625eee848SAlexander Motin 		token->timeout = TPC_DFL_TOKEN_TIMEOUT;
215725eee848SAlexander Motin 	else if (token->timeout < TPC_MIN_TOKEN_TIMEOUT)
215825eee848SAlexander Motin 		token->timeout = TPC_MIN_TOKEN_TIMEOUT;
215925eee848SAlexander Motin 	memcpy(list->res_token, token->token, sizeof(list->res_token));
216025eee848SAlexander Motin 	list->res_token_valid = 1;
216125eee848SAlexander Motin 	list->curseg = 0;
216225eee848SAlexander Motin 	list->completed = 1;
216325eee848SAlexander Motin 	list->last_active = time_uptime;
21642d8b2876SAlexander Motin 	mtx_lock(&softc->tpc_lock);
21659602f436SAlexander Motin 	TAILQ_INSERT_TAIL(&softc->tpc_tokens, token, links);
21662d8b2876SAlexander Motin 	mtx_unlock(&softc->tpc_lock);
216725eee848SAlexander Motin 	ctl_set_success(ctsio);
216825eee848SAlexander Motin 	ctl_done((union ctl_io *)ctsio);
216925eee848SAlexander Motin 	return (CTL_RETVAL_COMPLETE);
217025eee848SAlexander Motin 
217125eee848SAlexander Motin done:
21722a72b593SAlexander Motin 	if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
217325eee848SAlexander Motin 		free(ctsio->kern_data_ptr, M_CTL);
21742a72b593SAlexander Motin 		ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
21752a72b593SAlexander Motin 	}
217625eee848SAlexander Motin 	ctl_done((union ctl_io *)ctsio);
217725eee848SAlexander Motin 	return (CTL_RETVAL_COMPLETE);
217825eee848SAlexander Motin }
217925eee848SAlexander Motin 
218025eee848SAlexander Motin int
ctl_write_using_token(struct ctl_scsiio * ctsio)218125eee848SAlexander Motin ctl_write_using_token(struct ctl_scsiio *ctsio)
218225eee848SAlexander Motin {
21839cbbfd2fSAlexander Motin 	struct ctl_softc *softc = CTL_SOFTC(ctsio);
21849cbbfd2fSAlexander Motin 	struct ctl_lun *lun = CTL_LUN(ctsio);
218525eee848SAlexander Motin 	struct scsi_write_using_token *cdb;
218625eee848SAlexander Motin 	struct scsi_write_using_token_data *data;
218725eee848SAlexander Motin 	struct tpc_list *list, *tlist;
218825eee848SAlexander Motin 	struct tpc_token *token;
218932920cbfSAlexander Motin 	uint64_t lba;
2190e13f4248SAlexander Motin 	int len, lendata, lendesc;
219125eee848SAlexander Motin 
219225eee848SAlexander Motin 	CTL_DEBUG_PRINT(("ctl_write_using_token\n"));
219325eee848SAlexander Motin 
219425eee848SAlexander Motin 	cdb = (struct scsi_write_using_token *)ctsio->cdb;
219525eee848SAlexander Motin 	len = scsi_4btoul(cdb->length);
219625eee848SAlexander Motin 
2197e13f4248SAlexander Motin 	if (len < sizeof(struct scsi_write_using_token_data) ||
2198e13f4248SAlexander Motin 	    len > sizeof(struct scsi_write_using_token_data) +
219925eee848SAlexander Motin 	     TPC_MAX_SEGS * sizeof(struct scsi_range_desc)) {
220025eee848SAlexander Motin 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
220125eee848SAlexander Motin 		    /*field*/ 9, /*bit_valid*/ 0, /*bit*/ 0);
220225eee848SAlexander Motin 		goto done;
220325eee848SAlexander Motin 	}
220425eee848SAlexander Motin 
220525eee848SAlexander Motin 	/*
220625eee848SAlexander Motin 	 * If we've got a kernel request that hasn't been malloced yet,
220725eee848SAlexander Motin 	 * malloc it and tell the caller the data buffer is here.
220825eee848SAlexander Motin 	 */
220925eee848SAlexander Motin 	if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
221025eee848SAlexander Motin 		ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
221125eee848SAlexander Motin 		ctsio->kern_data_len = len;
221225eee848SAlexander Motin 		ctsio->kern_total_len = len;
221325eee848SAlexander Motin 		ctsio->kern_rel_offset = 0;
221425eee848SAlexander Motin 		ctsio->kern_sg_entries = 0;
221525eee848SAlexander Motin 		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
221625eee848SAlexander Motin 		ctsio->be_move_done = ctl_config_move_done;
221725eee848SAlexander Motin 		ctl_datamove((union ctl_io *)ctsio);
221825eee848SAlexander Motin 
221925eee848SAlexander Motin 		return (CTL_RETVAL_COMPLETE);
222025eee848SAlexander Motin 	}
222125eee848SAlexander Motin 
222225eee848SAlexander Motin 	data = (struct scsi_write_using_token_data *)ctsio->kern_data_ptr;
2223e13f4248SAlexander Motin 	lendata = scsi_2btoul(data->length);
2224e13f4248SAlexander Motin 	if (lendata < sizeof(struct scsi_write_using_token_data) - 2 +
2225e13f4248SAlexander Motin 	    sizeof(struct scsi_range_desc)) {
222625eee848SAlexander Motin 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 0,
2227e13f4248SAlexander Motin 		    /*field*/ 0, /*bit_valid*/ 0, /*bit*/ 0);
2228e13f4248SAlexander Motin 		goto done;
2229e13f4248SAlexander Motin 	}
2230e13f4248SAlexander Motin 	lendesc = scsi_2btoul(data->range_descriptor_length);
2231e13f4248SAlexander Motin 	if (lendesc < sizeof(struct scsi_range_desc) ||
2232e13f4248SAlexander Motin 	    len < sizeof(struct scsi_write_using_token_data) + lendesc ||
2233e13f4248SAlexander Motin 	    lendata < sizeof(struct scsi_write_using_token_data) - 2 + lendesc) {
2234e13f4248SAlexander Motin 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 0,
2235e13f4248SAlexander Motin 		    /*field*/ 534, /*bit_valid*/ 0, /*bit*/ 0);
223625eee848SAlexander Motin 		goto done;
223725eee848SAlexander Motin 	}
223825eee848SAlexander Motin /*
223925eee848SAlexander Motin 	printf("WUT(list=%u) flags=%x off=%ju len=%x\n",
224025eee848SAlexander Motin 	    scsi_4btoul(cdb->list_identifier),
224125eee848SAlexander Motin 	    data->flags, scsi_8btou64(data->offset_into_rod),
224225eee848SAlexander Motin 	    scsi_2btoul(data->range_descriptor_length));
224325eee848SAlexander Motin */
2244e13f4248SAlexander Motin 
2245e13f4248SAlexander Motin 	/* Validate list of ranges */
22468fadf660SAlexander Motin 	if (tpc_check_ranges_l(&data->desc[0],
2247e13f4248SAlexander Motin 	    scsi_2btoul(data->range_descriptor_length) /
224838618bf4SAlexander Motin 	    sizeof(struct scsi_range_desc),
224932920cbfSAlexander Motin 	    lun->be_lun->maxlba, &lba) != 0) {
225032920cbfSAlexander Motin 		ctl_set_lba_out_of_range(ctsio, lba);
22518fadf660SAlexander Motin 		goto done;
22528fadf660SAlexander Motin 	}
22538fadf660SAlexander Motin 	if (tpc_check_ranges_x(&data->desc[0],
22548fadf660SAlexander Motin 	    scsi_2btoul(data->range_descriptor_length) /
22558fadf660SAlexander Motin 	    sizeof(struct scsi_range_desc)) != 0) {
2256e13f4248SAlexander Motin 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 0,
2257e13f4248SAlexander Motin 		    /*command*/ 0, /*field*/ 0, /*bit_valid*/ 0,
2258e13f4248SAlexander Motin 		    /*bit*/ 0);
2259e13f4248SAlexander Motin 		goto done;
2260e13f4248SAlexander Motin 	}
2261e13f4248SAlexander Motin 
226225eee848SAlexander Motin 	list = malloc(sizeof(struct tpc_list), M_CTL, M_WAITOK | M_ZERO);
226325eee848SAlexander Motin 	list->service_action = cdb->service_action;
226425eee848SAlexander Motin 	list->init_port = ctsio->io_hdr.nexus.targ_port;
22657ac58230SAlexander Motin 	list->init_idx = ctl_get_initindex(&ctsio->io_hdr.nexus);
226625eee848SAlexander Motin 	list->list_id = scsi_4btoul(cdb->list_identifier);
226725eee848SAlexander Motin 	list->flags = data->flags;
226825eee848SAlexander Motin 	list->params = ctsio->kern_data_ptr;
226925eee848SAlexander Motin 	list->range = &data->desc[0];
227025eee848SAlexander Motin 	list->nrange = scsi_2btoul(data->range_descriptor_length) /
227125eee848SAlexander Motin 	    sizeof(struct scsi_range_desc);
227225eee848SAlexander Motin 	list->offset_into_rod = scsi_8btou64(data->offset_into_rod);
227325eee848SAlexander Motin 	list->ctsio = ctsio;
227425eee848SAlexander Motin 	list->lun = lun;
227525eee848SAlexander Motin 	mtx_lock(&lun->lun_lock);
227625eee848SAlexander Motin 	tlist = tpc_find_list(lun, list->list_id, list->init_idx);
227725eee848SAlexander Motin 	if (tlist != NULL && !tlist->completed) {
227825eee848SAlexander Motin 		mtx_unlock(&lun->lun_lock);
227925eee848SAlexander Motin 		free(list, M_CTL);
228025eee848SAlexander Motin 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
228125eee848SAlexander Motin 		    /*command*/ 0, /*field*/ 0, /*bit_valid*/ 0,
228225eee848SAlexander Motin 		    /*bit*/ 0);
228325eee848SAlexander Motin 		goto done;
228425eee848SAlexander Motin 	}
228525eee848SAlexander Motin 	if (tlist != NULL) {
228625eee848SAlexander Motin 		TAILQ_REMOVE(&lun->tpc_lists, tlist, links);
228725eee848SAlexander Motin 		free(tlist, M_CTL);
228825eee848SAlexander Motin 	}
228925eee848SAlexander Motin 	TAILQ_INSERT_TAIL(&lun->tpc_lists, list, links);
229025eee848SAlexander Motin 	mtx_unlock(&lun->lun_lock);
229125eee848SAlexander Motin 
229225eee848SAlexander Motin 	/* Block device zero ROD token -> no token. */
229325eee848SAlexander Motin 	if (scsi_4btoul(data->rod_token) == ROD_TYPE_BLOCK_ZERO) {
229425eee848SAlexander Motin 		tpc_process(list);
229525eee848SAlexander Motin 		return (CTL_RETVAL_COMPLETE);
229625eee848SAlexander Motin 	}
229725eee848SAlexander Motin 
22982d8b2876SAlexander Motin 	mtx_lock(&softc->tpc_lock);
22999602f436SAlexander Motin 	TAILQ_FOREACH(token, &softc->tpc_tokens, links) {
230025eee848SAlexander Motin 		if (memcmp(token->token, data->rod_token,
230125eee848SAlexander Motin 		    sizeof(data->rod_token)) == 0)
230225eee848SAlexander Motin 			break;
230325eee848SAlexander Motin 	}
230425eee848SAlexander Motin 	if (token != NULL) {
230525eee848SAlexander Motin 		token->active++;
230625eee848SAlexander Motin 		list->token = token;
230725eee848SAlexander Motin 		if (data->flags & EC_WUT_DEL_TKN)
230825eee848SAlexander Motin 			token->timeout = 0;
230925eee848SAlexander Motin 	}
23102d8b2876SAlexander Motin 	mtx_unlock(&softc->tpc_lock);
231125eee848SAlexander Motin 	if (token == NULL) {
231225eee848SAlexander Motin 		mtx_lock(&lun->lun_lock);
231325eee848SAlexander Motin 		TAILQ_REMOVE(&lun->tpc_lists, list, links);
231425eee848SAlexander Motin 		mtx_unlock(&lun->lun_lock);
231525eee848SAlexander Motin 		free(list, M_CTL);
231625eee848SAlexander Motin 		ctl_set_sense(ctsio, /*current_error*/ 1,
231725eee848SAlexander Motin 		    /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
231825eee848SAlexander Motin 		    /*asc*/ 0x23, /*ascq*/ 0x04, SSD_ELEM_NONE);
231925eee848SAlexander Motin 		goto done;
232025eee848SAlexander Motin 	}
232125eee848SAlexander Motin 
232225eee848SAlexander Motin 	tpc_process(list);
232325eee848SAlexander Motin 	return (CTL_RETVAL_COMPLETE);
232425eee848SAlexander Motin 
232525eee848SAlexander Motin done:
23262a72b593SAlexander Motin 	if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
232725eee848SAlexander Motin 		free(ctsio->kern_data_ptr, M_CTL);
23282a72b593SAlexander Motin 		ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
23292a72b593SAlexander Motin 	}
233025eee848SAlexander Motin 	ctl_done((union ctl_io *)ctsio);
233125eee848SAlexander Motin 	return (CTL_RETVAL_COMPLETE);
233225eee848SAlexander Motin }
233325eee848SAlexander Motin 
233425eee848SAlexander Motin int
ctl_receive_rod_token_information(struct ctl_scsiio * ctsio)233525eee848SAlexander Motin ctl_receive_rod_token_information(struct ctl_scsiio *ctsio)
233625eee848SAlexander Motin {
23379cbbfd2fSAlexander Motin 	struct ctl_lun *lun = CTL_LUN(ctsio);
233825eee848SAlexander Motin 	struct scsi_receive_rod_token_information *cdb;
233925eee848SAlexander Motin 	struct scsi_receive_copy_status_lid4_data *data;
234025eee848SAlexander Motin 	struct tpc_list *list;
234125eee848SAlexander Motin 	struct tpc_list list_copy;
234225eee848SAlexander Motin 	uint8_t *ptr;
234325eee848SAlexander Motin 	int retval;
234425eee848SAlexander Motin 	int alloc_len, total_len, token_len;
234525eee848SAlexander Motin 	uint32_t list_id;
234625eee848SAlexander Motin 
234725eee848SAlexander Motin 	CTL_DEBUG_PRINT(("ctl_receive_rod_token_information\n"));
234825eee848SAlexander Motin 
234925eee848SAlexander Motin 	cdb = (struct scsi_receive_rod_token_information *)ctsio->cdb;
235025eee848SAlexander Motin 	retval = CTL_RETVAL_COMPLETE;
235125eee848SAlexander Motin 
235225eee848SAlexander Motin 	list_id = scsi_4btoul(cdb->list_identifier);
235325eee848SAlexander Motin 	mtx_lock(&lun->lun_lock);
235425eee848SAlexander Motin 	list = tpc_find_list(lun, list_id,
23557ac58230SAlexander Motin 	    ctl_get_initindex(&ctsio->io_hdr.nexus));
235625eee848SAlexander Motin 	if (list == NULL) {
235725eee848SAlexander Motin 		mtx_unlock(&lun->lun_lock);
235825eee848SAlexander Motin 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
235925eee848SAlexander Motin 		    /*command*/ 1, /*field*/ 2, /*bit_valid*/ 0,
236025eee848SAlexander Motin 		    /*bit*/ 0);
236125eee848SAlexander Motin 		ctl_done((union ctl_io *)ctsio);
236225eee848SAlexander Motin 		return (retval);
236325eee848SAlexander Motin 	}
236425eee848SAlexander Motin 	list_copy = *list;
236525eee848SAlexander Motin 	if (list->completed) {
236625eee848SAlexander Motin 		TAILQ_REMOVE(&lun->tpc_lists, list, links);
236725eee848SAlexander Motin 		free(list, M_CTL);
236825eee848SAlexander Motin 	}
236925eee848SAlexander Motin 	mtx_unlock(&lun->lun_lock);
237025eee848SAlexander Motin 
237125eee848SAlexander Motin 	token_len = list_copy.res_token_valid ? 2 + sizeof(list_copy.res_token) : 0;
237225eee848SAlexander Motin 	total_len = sizeof(*data) + list_copy.sense_len + 4 + token_len;
237325eee848SAlexander Motin 	alloc_len = scsi_4btoul(cdb->length);
237425eee848SAlexander Motin 
237525eee848SAlexander Motin 	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
237625eee848SAlexander Motin 	ctsio->kern_sg_entries = 0;
237725eee848SAlexander Motin 	ctsio->kern_rel_offset = 0;
2378640603fbSAlexander Motin 	ctsio->kern_data_len = min(total_len, alloc_len);
2379640603fbSAlexander Motin 	ctsio->kern_total_len = ctsio->kern_data_len;
238025eee848SAlexander Motin 
238125eee848SAlexander Motin 	data = (struct scsi_receive_copy_status_lid4_data *)ctsio->kern_data_ptr;
238225eee848SAlexander Motin 	scsi_ulto4b(sizeof(*data) - 4 + list_copy.sense_len +
238325eee848SAlexander Motin 	    4 + token_len, data->available_data);
238425eee848SAlexander Motin 	data->response_to_service_action = list_copy.service_action;
238525eee848SAlexander Motin 	if (list_copy.completed) {
238625eee848SAlexander Motin 		if (list_copy.error)
238725eee848SAlexander Motin 			data->copy_command_status = RCS_CCS_ERROR;
238825eee848SAlexander Motin 		else if (list_copy.abort)
238925eee848SAlexander Motin 			data->copy_command_status = RCS_CCS_ABORTED;
239025eee848SAlexander Motin 		else
239125eee848SAlexander Motin 			data->copy_command_status = RCS_CCS_COMPLETED;
239225eee848SAlexander Motin 	} else
239325eee848SAlexander Motin 		data->copy_command_status = RCS_CCS_INPROG_FG;
239425eee848SAlexander Motin 	scsi_ulto2b(list_copy.curops, data->operation_counter);
239525eee848SAlexander Motin 	scsi_ulto4b(UINT32_MAX, data->estimated_status_update_delay);
239625eee848SAlexander Motin 	data->transfer_count_units = RCS_TC_LBAS;
239725eee848SAlexander Motin 	scsi_u64to8b(list_copy.cursectors, data->transfer_count);
239825eee848SAlexander Motin 	scsi_ulto2b(list_copy.curseg, data->segments_processed);
239925eee848SAlexander Motin 	data->length_of_the_sense_data_field = list_copy.sense_len;
240025eee848SAlexander Motin 	data->sense_data_length = list_copy.sense_len;
240125eee848SAlexander Motin 	memcpy(data->sense_data, &list_copy.sense_data, list_copy.sense_len);
240225eee848SAlexander Motin 
240325eee848SAlexander Motin 	ptr = &data->sense_data[data->length_of_the_sense_data_field];
240425eee848SAlexander Motin 	scsi_ulto4b(token_len, &ptr[0]);
240525eee848SAlexander Motin 	if (list_copy.res_token_valid) {
240625eee848SAlexander Motin 		scsi_ulto2b(0, &ptr[4]);
240725eee848SAlexander Motin 		memcpy(&ptr[6], list_copy.res_token, sizeof(list_copy.res_token));
240825eee848SAlexander Motin 	}
240925eee848SAlexander Motin /*
241025eee848SAlexander Motin 	printf("RRTI(list=%u) valid=%d\n",
241125eee848SAlexander Motin 	    scsi_4btoul(cdb->list_identifier), list_copy.res_token_valid);
241225eee848SAlexander Motin */
2413f7241cceSAlexander Motin 	ctl_set_success(ctsio);
241425eee848SAlexander Motin 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
241525eee848SAlexander Motin 	ctsio->be_move_done = ctl_config_move_done;
241625eee848SAlexander Motin 	ctl_datamove((union ctl_io *)ctsio);
241725eee848SAlexander Motin 	return (retval);
241825eee848SAlexander Motin }
241925eee848SAlexander Motin 
242025eee848SAlexander Motin int
ctl_report_all_rod_tokens(struct ctl_scsiio * ctsio)242125eee848SAlexander Motin ctl_report_all_rod_tokens(struct ctl_scsiio *ctsio)
242225eee848SAlexander Motin {
24239cbbfd2fSAlexander Motin 	struct ctl_softc *softc = CTL_SOFTC(ctsio);
242425eee848SAlexander Motin 	struct scsi_report_all_rod_tokens *cdb;
242525eee848SAlexander Motin 	struct scsi_report_all_rod_tokens_data *data;
242625eee848SAlexander Motin 	struct tpc_token *token;
242725eee848SAlexander Motin 	int retval;
242825eee848SAlexander Motin 	int alloc_len, total_len, tokens, i;
242925eee848SAlexander Motin 
243025eee848SAlexander Motin 	CTL_DEBUG_PRINT(("ctl_receive_rod_token_information\n"));
243125eee848SAlexander Motin 
243225eee848SAlexander Motin 	cdb = (struct scsi_report_all_rod_tokens *)ctsio->cdb;
243325eee848SAlexander Motin 	retval = CTL_RETVAL_COMPLETE;
243425eee848SAlexander Motin 
243525eee848SAlexander Motin 	tokens = 0;
24362d8b2876SAlexander Motin 	mtx_lock(&softc->tpc_lock);
24379602f436SAlexander Motin 	TAILQ_FOREACH(token, &softc->tpc_tokens, links)
243825eee848SAlexander Motin 		tokens++;
24392d8b2876SAlexander Motin 	mtx_unlock(&softc->tpc_lock);
244025eee848SAlexander Motin 	if (tokens > 512)
244125eee848SAlexander Motin 		tokens = 512;
244225eee848SAlexander Motin 
244325eee848SAlexander Motin 	total_len = sizeof(*data) + tokens * 96;
244425eee848SAlexander Motin 	alloc_len = scsi_4btoul(cdb->length);
244525eee848SAlexander Motin 
244625eee848SAlexander Motin 	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
244725eee848SAlexander Motin 	ctsio->kern_sg_entries = 0;
244825eee848SAlexander Motin 	ctsio->kern_rel_offset = 0;
2449640603fbSAlexander Motin 	ctsio->kern_data_len = min(total_len, alloc_len);
2450640603fbSAlexander Motin 	ctsio->kern_total_len = ctsio->kern_data_len;
245125eee848SAlexander Motin 
245225eee848SAlexander Motin 	data = (struct scsi_report_all_rod_tokens_data *)ctsio->kern_data_ptr;
245325eee848SAlexander Motin 	i = 0;
24542d8b2876SAlexander Motin 	mtx_lock(&softc->tpc_lock);
24559602f436SAlexander Motin 	TAILQ_FOREACH(token, &softc->tpc_tokens, links) {
245625eee848SAlexander Motin 		if (i >= tokens)
245725eee848SAlexander Motin 			break;
245825eee848SAlexander Motin 		memcpy(&data->rod_management_token_list[i * 96],
245925eee848SAlexander Motin 		    token->token, 96);
246025eee848SAlexander Motin 		i++;
246125eee848SAlexander Motin 	}
24622d8b2876SAlexander Motin 	mtx_unlock(&softc->tpc_lock);
246325eee848SAlexander Motin 	scsi_ulto4b(sizeof(*data) - 4 + i * 96, data->available_data);
246425eee848SAlexander Motin /*
246525eee848SAlexander Motin 	printf("RART tokens=%d\n", i);
246625eee848SAlexander Motin */
2467f7241cceSAlexander Motin 	ctl_set_success(ctsio);
246825eee848SAlexander Motin 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
246925eee848SAlexander Motin 	ctsio->be_move_done = ctl_config_move_done;
247025eee848SAlexander Motin 	ctl_datamove((union ctl_io *)ctsio);
247125eee848SAlexander Motin 	return (retval);
247225eee848SAlexander Motin }
2473