xref: /freebsd/sys/cam/ctl/ctl_tpc.c (revision bbb29a3c0f2c4565eff6fda70426807b6ed97f8b)
1 /*-
2  * Copyright (c) 2014 Alexander Motin <mav@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/types.h>
34 #include <sys/lock.h>
35 #include <sys/module.h>
36 #include <sys/mutex.h>
37 #include <sys/condvar.h>
38 #include <sys/malloc.h>
39 #include <sys/conf.h>
40 #include <sys/queue.h>
41 #include <sys/sysctl.h>
42 #include <machine/atomic.h>
43 
44 #include <cam/cam.h>
45 #include <cam/scsi/scsi_all.h>
46 #include <cam/scsi/scsi_da.h>
47 #include <cam/ctl/ctl_io.h>
48 #include <cam/ctl/ctl.h>
49 #include <cam/ctl/ctl_frontend.h>
50 #include <cam/ctl/ctl_frontend_internal.h>
51 #include <cam/ctl/ctl_util.h>
52 #include <cam/ctl/ctl_backend.h>
53 #include <cam/ctl/ctl_ioctl.h>
54 #include <cam/ctl/ctl_ha.h>
55 #include <cam/ctl/ctl_private.h>
56 #include <cam/ctl/ctl_debug.h>
57 #include <cam/ctl/ctl_scsi_all.h>
58 #include <cam/ctl/ctl_tpc.h>
59 #include <cam/ctl/ctl_error.h>
60 
61 #define	TPC_MAX_CSCDS	64
62 #define	TPC_MAX_SEGS	64
63 #define	TPC_MAX_SEG	0
64 #define	TPC_MAX_LIST	8192
65 #define	TPC_MAX_INLINE	0
66 #define	TPC_MAX_LISTS	255
67 #define	TPC_MAX_IO_SIZE	(1024 * 1024)
68 #define	TPC_MAX_IOCHUNK_SIZE	(TPC_MAX_IO_SIZE * 16)
69 #define	TPC_MIN_TOKEN_TIMEOUT	1
70 #define	TPC_DFL_TOKEN_TIMEOUT	60
71 #define	TPC_MAX_TOKEN_TIMEOUT	600
72 
73 MALLOC_DEFINE(M_CTL_TPC, "ctltpc", "CTL TPC");
74 
75 typedef enum {
76 	TPC_ERR_RETRY		= 0x000,
77 	TPC_ERR_FAIL		= 0x001,
78 	TPC_ERR_MASK		= 0x0ff,
79 	TPC_ERR_NO_DECREMENT	= 0x100
80 } tpc_error_action;
81 
82 struct tpc_list;
83 TAILQ_HEAD(runl, tpc_io);
84 struct tpc_io {
85 	union ctl_io		*io;
86 	uint64_t		 lun;
87 	struct tpc_list		*list;
88 	struct runl		 run;
89 	TAILQ_ENTRY(tpc_io)	 rlinks;
90 	TAILQ_ENTRY(tpc_io)	 links;
91 };
92 
93 struct tpc_token {
94 	uint8_t			 token[512];
95 	uint64_t		 lun;
96 	uint32_t		 blocksize;
97 	uint8_t			*params;
98 	struct scsi_range_desc	*range;
99 	int			 nrange;
100 	int			 active;
101 	time_t			 last_active;
102 	uint32_t		 timeout;
103 	TAILQ_ENTRY(tpc_token)	 links;
104 };
105 
106 struct tpc_list {
107 	uint8_t			 service_action;
108 	int			 init_port;
109 	uint32_t		 init_idx;
110 	uint32_t		 list_id;
111 	uint8_t			 flags;
112 	uint8_t			*params;
113 	struct scsi_ec_cscd	*cscd;
114 	struct scsi_ec_segment	*seg[TPC_MAX_SEGS];
115 	uint8_t			*inl;
116 	int			 ncscd;
117 	int			 nseg;
118 	int			 leninl;
119 	struct tpc_token	*token;
120 	struct scsi_range_desc	*range;
121 	int			 nrange;
122 	off_t			 offset_into_rod;
123 
124 	int			 curseg;
125 	off_t			 cursectors;
126 	off_t			 curbytes;
127 	int			 curops;
128 	int			 stage;
129 	uint8_t			*buf;
130 	off_t			 segsectors;
131 	off_t			 segbytes;
132 	int			 tbdio;
133 	int			 error;
134 	int			 abort;
135 	int			 completed;
136 	time_t			 last_active;
137 	TAILQ_HEAD(, tpc_io)	 allio;
138 	struct scsi_sense_data	 sense_data;
139 	uint8_t			 sense_len;
140 	uint8_t			 scsi_status;
141 	struct ctl_scsiio	*ctsio;
142 	struct ctl_lun		*lun;
143 	int			 res_token_valid;
144 	uint8_t			 res_token[512];
145 	TAILQ_ENTRY(tpc_list)	 links;
146 };
147 
148 extern struct ctl_softc *control_softc;
149 
150 static void
151 tpc_timeout(void *arg)
152 {
153 	struct ctl_softc *softc = arg;
154 	struct ctl_lun *lun;
155 	struct tpc_token *token, *ttoken;
156 	struct tpc_list *list, *tlist;
157 
158 	/* Free completed lists with expired timeout. */
159 	STAILQ_FOREACH(lun, &softc->lun_list, links) {
160 		mtx_lock(&lun->lun_lock);
161 		TAILQ_FOREACH_SAFE(list, &lun->tpc_lists, links, tlist) {
162 			if (!list->completed || time_uptime < list->last_active +
163 			    TPC_DFL_TOKEN_TIMEOUT)
164 				continue;
165 			TAILQ_REMOVE(&lun->tpc_lists, list, links);
166 			free(list, M_CTL);
167 		}
168 		mtx_unlock(&lun->lun_lock);
169 	}
170 
171 	/* Free inactive ROD tokens with expired timeout. */
172 	TAILQ_FOREACH_SAFE(token, &softc->tpc_tokens, links, ttoken) {
173 		if (token->active ||
174 		    time_uptime < token->last_active + token->timeout + 1)
175 			continue;
176 		TAILQ_REMOVE(&softc->tpc_tokens, token, links);
177 		free(token->params, M_CTL);
178 		free(token, M_CTL);
179 	}
180 	callout_schedule(&softc->tpc_timeout, hz);
181 }
182 
183 void
184 ctl_tpc_init(struct ctl_softc *softc)
185 {
186 
187 	TAILQ_INIT(&softc->tpc_tokens);
188 	callout_init_mtx(&softc->tpc_timeout, &softc->ctl_lock, 0);
189 	callout_reset(&softc->tpc_timeout, hz, tpc_timeout, softc);
190 }
191 
192 void
193 ctl_tpc_shutdown(struct ctl_softc *softc)
194 {
195 	struct tpc_token *token;
196 
197 	callout_drain(&softc->tpc_timeout);
198 
199 	/* Free ROD tokens. */
200 	mtx_lock(&softc->ctl_lock);
201 	while ((token = TAILQ_FIRST(&softc->tpc_tokens)) != NULL) {
202 		TAILQ_REMOVE(&softc->tpc_tokens, token, links);
203 		free(token->params, M_CTL);
204 		free(token, M_CTL);
205 	}
206 	mtx_unlock(&softc->ctl_lock);
207 }
208 
209 void
210 ctl_tpc_lun_init(struct ctl_lun *lun)
211 {
212 
213 	TAILQ_INIT(&lun->tpc_lists);
214 }
215 
216 void
217 ctl_tpc_lun_shutdown(struct ctl_lun *lun)
218 {
219 	struct tpc_list *list;
220 	struct tpc_token *token, *ttoken;
221 
222 	/* Free lists for this LUN. */
223 	while ((list = TAILQ_FIRST(&lun->tpc_lists)) != NULL) {
224 		TAILQ_REMOVE(&lun->tpc_lists, list, links);
225 		KASSERT(list->completed,
226 		    ("Not completed TPC (%p) on shutdown", list));
227 		free(list, M_CTL);
228 	}
229 
230 	/* Free ROD tokens for this LUN. */
231 	mtx_assert(&control_softc->ctl_lock, MA_OWNED);
232 	TAILQ_FOREACH_SAFE(token, &control_softc->tpc_tokens, links, ttoken) {
233 		if (token->lun != lun->lun || token->active)
234 			continue;
235 		TAILQ_REMOVE(&control_softc->tpc_tokens, token, links);
236 		free(token->params, M_CTL);
237 		free(token, M_CTL);
238 	}
239 }
240 
241 int
242 ctl_inquiry_evpd_tpc(struct ctl_scsiio *ctsio, int alloc_len)
243 {
244 	struct scsi_vpd_tpc *tpc_ptr;
245 	struct scsi_vpd_tpc_descriptor *d_ptr;
246 	struct scsi_vpd_tpc_descriptor_bdrl *bdrl_ptr;
247 	struct scsi_vpd_tpc_descriptor_sc *sc_ptr;
248 	struct scsi_vpd_tpc_descriptor_sc_descr *scd_ptr;
249 	struct scsi_vpd_tpc_descriptor_pd *pd_ptr;
250 	struct scsi_vpd_tpc_descriptor_sd *sd_ptr;
251 	struct scsi_vpd_tpc_descriptor_sdid *sdid_ptr;
252 	struct scsi_vpd_tpc_descriptor_rtf *rtf_ptr;
253 	struct scsi_vpd_tpc_descriptor_rtf_block *rtfb_ptr;
254 	struct scsi_vpd_tpc_descriptor_srt *srt_ptr;
255 	struct scsi_vpd_tpc_descriptor_srtd *srtd_ptr;
256 	struct scsi_vpd_tpc_descriptor_gco *gco_ptr;
257 	struct ctl_lun *lun;
258 	int data_len;
259 
260 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
261 
262 	data_len = sizeof(struct scsi_vpd_tpc) +
263 	    sizeof(struct scsi_vpd_tpc_descriptor_bdrl) +
264 	    roundup2(sizeof(struct scsi_vpd_tpc_descriptor_sc) +
265 	     2 * sizeof(struct scsi_vpd_tpc_descriptor_sc_descr) + 11, 4) +
266 	    sizeof(struct scsi_vpd_tpc_descriptor_pd) +
267 	    roundup2(sizeof(struct scsi_vpd_tpc_descriptor_sd) + 4, 4) +
268 	    roundup2(sizeof(struct scsi_vpd_tpc_descriptor_sdid) + 2, 4) +
269 	    sizeof(struct scsi_vpd_tpc_descriptor_rtf) +
270 	     sizeof(struct scsi_vpd_tpc_descriptor_rtf_block) +
271 	    sizeof(struct scsi_vpd_tpc_descriptor_srt) +
272 	     2*sizeof(struct scsi_vpd_tpc_descriptor_srtd) +
273 	    sizeof(struct scsi_vpd_tpc_descriptor_gco);
274 
275 	ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
276 	tpc_ptr = (struct scsi_vpd_tpc *)ctsio->kern_data_ptr;
277 	ctsio->kern_sg_entries = 0;
278 
279 	if (data_len < alloc_len) {
280 		ctsio->residual = alloc_len - data_len;
281 		ctsio->kern_data_len = data_len;
282 		ctsio->kern_total_len = data_len;
283 	} else {
284 		ctsio->residual = 0;
285 		ctsio->kern_data_len = alloc_len;
286 		ctsio->kern_total_len = alloc_len;
287 	}
288 	ctsio->kern_data_resid = 0;
289 	ctsio->kern_rel_offset = 0;
290 	ctsio->kern_sg_entries = 0;
291 
292 	/*
293 	 * The control device is always connected.  The disk device, on the
294 	 * other hand, may not be online all the time.
295 	 */
296 	if (lun != NULL)
297 		tpc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
298 				     lun->be_lun->lun_type;
299 	else
300 		tpc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
301 	tpc_ptr->page_code = SVPD_SCSI_TPC;
302 	scsi_ulto2b(data_len - 4, tpc_ptr->page_length);
303 
304 	/* Block Device ROD Limits */
305 	d_ptr = (struct scsi_vpd_tpc_descriptor *)&tpc_ptr->descr[0];
306 	bdrl_ptr = (struct scsi_vpd_tpc_descriptor_bdrl *)d_ptr;
307 	scsi_ulto2b(SVPD_TPC_BDRL, bdrl_ptr->desc_type);
308 	scsi_ulto2b(sizeof(*bdrl_ptr) - 4, bdrl_ptr->desc_length);
309 	scsi_ulto2b(TPC_MAX_SEGS, bdrl_ptr->maximum_ranges);
310 	scsi_ulto4b(TPC_MAX_TOKEN_TIMEOUT,
311 	    bdrl_ptr->maximum_inactivity_timeout);
312 	scsi_ulto4b(TPC_DFL_TOKEN_TIMEOUT,
313 	    bdrl_ptr->default_inactivity_timeout);
314 	scsi_u64to8b(0, bdrl_ptr->maximum_token_transfer_size);
315 	scsi_u64to8b(0, bdrl_ptr->optimal_transfer_count);
316 
317 	/* Supported commands */
318 	d_ptr = (struct scsi_vpd_tpc_descriptor *)
319 	    (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
320 	sc_ptr = (struct scsi_vpd_tpc_descriptor_sc *)d_ptr;
321 	scsi_ulto2b(SVPD_TPC_SC, sc_ptr->desc_type);
322 	sc_ptr->list_length = 2 * sizeof(*scd_ptr) + 11;
323 	scsi_ulto2b(roundup2(1 + sc_ptr->list_length, 4), sc_ptr->desc_length);
324 	scd_ptr = &sc_ptr->descr[0];
325 	scd_ptr->opcode = EXTENDED_COPY;
326 	scd_ptr->sa_length = 5;
327 	scd_ptr->supported_service_actions[0] = EC_EC_LID1;
328 	scd_ptr->supported_service_actions[1] = EC_EC_LID4;
329 	scd_ptr->supported_service_actions[2] = EC_PT;
330 	scd_ptr->supported_service_actions[3] = EC_WUT;
331 	scd_ptr->supported_service_actions[4] = EC_COA;
332 	scd_ptr = (struct scsi_vpd_tpc_descriptor_sc_descr *)
333 	    &scd_ptr->supported_service_actions[scd_ptr->sa_length];
334 	scd_ptr->opcode = RECEIVE_COPY_STATUS;
335 	scd_ptr->sa_length = 6;
336 	scd_ptr->supported_service_actions[0] = RCS_RCS_LID1;
337 	scd_ptr->supported_service_actions[1] = RCS_RCFD;
338 	scd_ptr->supported_service_actions[2] = RCS_RCS_LID4;
339 	scd_ptr->supported_service_actions[3] = RCS_RCOP;
340 	scd_ptr->supported_service_actions[4] = RCS_RRTI;
341 	scd_ptr->supported_service_actions[5] = RCS_RART;
342 
343 	/* Parameter data. */
344 	d_ptr = (struct scsi_vpd_tpc_descriptor *)
345 	    (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
346 	pd_ptr = (struct scsi_vpd_tpc_descriptor_pd *)d_ptr;
347 	scsi_ulto2b(SVPD_TPC_PD, pd_ptr->desc_type);
348 	scsi_ulto2b(sizeof(*pd_ptr) - 4, pd_ptr->desc_length);
349 	scsi_ulto2b(TPC_MAX_CSCDS, pd_ptr->maximum_cscd_descriptor_count);
350 	scsi_ulto2b(TPC_MAX_SEGS, pd_ptr->maximum_segment_descriptor_count);
351 	scsi_ulto4b(TPC_MAX_LIST, pd_ptr->maximum_descriptor_list_length);
352 	scsi_ulto4b(TPC_MAX_INLINE, pd_ptr->maximum_inline_data_length);
353 
354 	/* Supported Descriptors */
355 	d_ptr = (struct scsi_vpd_tpc_descriptor *)
356 	    (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
357 	sd_ptr = (struct scsi_vpd_tpc_descriptor_sd *)d_ptr;
358 	scsi_ulto2b(SVPD_TPC_SD, sd_ptr->desc_type);
359 	scsi_ulto2b(roundup2(sizeof(*sd_ptr) - 4 + 4, 4), sd_ptr->desc_length);
360 	sd_ptr->list_length = 4;
361 	sd_ptr->supported_descriptor_codes[0] = EC_SEG_B2B;
362 	sd_ptr->supported_descriptor_codes[1] = EC_SEG_VERIFY;
363 	sd_ptr->supported_descriptor_codes[2] = EC_SEG_REGISTER_KEY;
364 	sd_ptr->supported_descriptor_codes[3] = EC_CSCD_ID;
365 
366 	/* Supported CSCD Descriptor IDs */
367 	d_ptr = (struct scsi_vpd_tpc_descriptor *)
368 	    (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
369 	sdid_ptr = (struct scsi_vpd_tpc_descriptor_sdid *)d_ptr;
370 	scsi_ulto2b(SVPD_TPC_SDID, sdid_ptr->desc_type);
371 	scsi_ulto2b(roundup2(sizeof(*sdid_ptr) - 4 + 2, 4), sdid_ptr->desc_length);
372 	scsi_ulto2b(2, sdid_ptr->list_length);
373 	scsi_ulto2b(0xffff, &sdid_ptr->supported_descriptor_ids[0]);
374 
375 	/* ROD Token Features */
376 	d_ptr = (struct scsi_vpd_tpc_descriptor *)
377 	    (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
378 	rtf_ptr = (struct scsi_vpd_tpc_descriptor_rtf *)d_ptr;
379 	scsi_ulto2b(SVPD_TPC_RTF, rtf_ptr->desc_type);
380 	scsi_ulto2b(sizeof(*rtf_ptr) - 4 + sizeof(*rtfb_ptr), rtf_ptr->desc_length);
381 	rtf_ptr->remote_tokens = 0;
382 	scsi_ulto4b(TPC_MIN_TOKEN_TIMEOUT, rtf_ptr->minimum_token_lifetime);
383 	scsi_ulto4b(UINT32_MAX, rtf_ptr->maximum_token_lifetime);
384 	scsi_ulto4b(TPC_MAX_TOKEN_TIMEOUT,
385 	    rtf_ptr->maximum_token_inactivity_timeout);
386 	scsi_ulto2b(sizeof(*rtfb_ptr), rtf_ptr->type_specific_features_length);
387 	rtfb_ptr = (struct scsi_vpd_tpc_descriptor_rtf_block *)
388 	    &rtf_ptr->type_specific_features;
389 	rtfb_ptr->type_format = SVPD_TPC_RTF_BLOCK;
390 	scsi_ulto2b(sizeof(*rtfb_ptr) - 4, rtfb_ptr->desc_length);
391 	scsi_ulto2b(0, rtfb_ptr->optimal_length_granularity);
392 	scsi_u64to8b(0, rtfb_ptr->maximum_bytes);
393 	scsi_u64to8b(0, rtfb_ptr->optimal_bytes);
394 	scsi_u64to8b(TPC_MAX_IOCHUNK_SIZE,
395 	    rtfb_ptr->optimal_bytes_to_token_per_segment);
396 	scsi_u64to8b(TPC_MAX_IOCHUNK_SIZE,
397 	    rtfb_ptr->optimal_bytes_from_token_per_segment);
398 
399 	/* Supported ROD Tokens */
400 	d_ptr = (struct scsi_vpd_tpc_descriptor *)
401 	    (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
402 	srt_ptr = (struct scsi_vpd_tpc_descriptor_srt *)d_ptr;
403 	scsi_ulto2b(SVPD_TPC_SRT, srt_ptr->desc_type);
404 	scsi_ulto2b(sizeof(*srt_ptr) - 4 + 2*sizeof(*srtd_ptr), srt_ptr->desc_length);
405 	scsi_ulto2b(2*sizeof(*srtd_ptr), srt_ptr->rod_type_descriptors_length);
406 	srtd_ptr = (struct scsi_vpd_tpc_descriptor_srtd *)
407 	    &srt_ptr->rod_type_descriptors;
408 	scsi_ulto4b(ROD_TYPE_AUR, srtd_ptr->rod_type);
409 	srtd_ptr->flags = SVPD_TPC_SRTD_TIN | SVPD_TPC_SRTD_TOUT;
410 	scsi_ulto2b(0, srtd_ptr->preference_indicator);
411 	srtd_ptr++;
412 	scsi_ulto4b(ROD_TYPE_BLOCK_ZERO, srtd_ptr->rod_type);
413 	srtd_ptr->flags = SVPD_TPC_SRTD_TIN;
414 	scsi_ulto2b(0, srtd_ptr->preference_indicator);
415 
416 	/* General Copy Operations */
417 	d_ptr = (struct scsi_vpd_tpc_descriptor *)
418 	    (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
419 	gco_ptr = (struct scsi_vpd_tpc_descriptor_gco *)d_ptr;
420 	scsi_ulto2b(SVPD_TPC_GCO, gco_ptr->desc_type);
421 	scsi_ulto2b(sizeof(*gco_ptr) - 4, gco_ptr->desc_length);
422 	scsi_ulto4b(TPC_MAX_LISTS, gco_ptr->total_concurrent_copies);
423 	scsi_ulto4b(TPC_MAX_LISTS, gco_ptr->maximum_identified_concurrent_copies);
424 	scsi_ulto4b(TPC_MAX_SEG, gco_ptr->maximum_segment_length);
425 	gco_ptr->data_segment_granularity = 0;
426 	gco_ptr->inline_data_granularity = 0;
427 
428 	ctl_set_success(ctsio);
429 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
430 	ctsio->be_move_done = ctl_config_move_done;
431 	ctl_datamove((union ctl_io *)ctsio);
432 
433 	return (CTL_RETVAL_COMPLETE);
434 }
435 
436 int
437 ctl_receive_copy_operating_parameters(struct ctl_scsiio *ctsio)
438 {
439 	struct scsi_receive_copy_operating_parameters *cdb;
440 	struct scsi_receive_copy_operating_parameters_data *data;
441 	int retval;
442 	int alloc_len, total_len;
443 
444 	CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
445 
446 	cdb = (struct scsi_receive_copy_operating_parameters *)ctsio->cdb;
447 
448 	retval = CTL_RETVAL_COMPLETE;
449 
450 	total_len = sizeof(*data) + 4;
451 	alloc_len = scsi_4btoul(cdb->length);
452 
453 	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
454 
455 	ctsio->kern_sg_entries = 0;
456 
457 	if (total_len < alloc_len) {
458 		ctsio->residual = alloc_len - total_len;
459 		ctsio->kern_data_len = total_len;
460 		ctsio->kern_total_len = total_len;
461 	} else {
462 		ctsio->residual = 0;
463 		ctsio->kern_data_len = alloc_len;
464 		ctsio->kern_total_len = alloc_len;
465 	}
466 	ctsio->kern_data_resid = 0;
467 	ctsio->kern_rel_offset = 0;
468 
469 	data = (struct scsi_receive_copy_operating_parameters_data *)ctsio->kern_data_ptr;
470 	scsi_ulto4b(sizeof(*data) - 4 + 4, data->length);
471 	data->snlid = RCOP_SNLID;
472 	scsi_ulto2b(TPC_MAX_CSCDS, data->maximum_cscd_descriptor_count);
473 	scsi_ulto2b(TPC_MAX_SEGS, data->maximum_segment_descriptor_count);
474 	scsi_ulto4b(TPC_MAX_LIST, data->maximum_descriptor_list_length);
475 	scsi_ulto4b(TPC_MAX_SEG, data->maximum_segment_length);
476 	scsi_ulto4b(TPC_MAX_INLINE, data->maximum_inline_data_length);
477 	scsi_ulto4b(0, data->held_data_limit);
478 	scsi_ulto4b(0, data->maximum_stream_device_transfer_size);
479 	scsi_ulto2b(TPC_MAX_LISTS, data->total_concurrent_copies);
480 	data->maximum_concurrent_copies = TPC_MAX_LISTS;
481 	data->data_segment_granularity = 0;
482 	data->inline_data_granularity = 0;
483 	data->held_data_granularity = 0;
484 	data->implemented_descriptor_list_length = 4;
485 	data->list_of_implemented_descriptor_type_codes[0] = EC_SEG_B2B;
486 	data->list_of_implemented_descriptor_type_codes[1] = EC_SEG_VERIFY;
487 	data->list_of_implemented_descriptor_type_codes[2] = EC_SEG_REGISTER_KEY;
488 	data->list_of_implemented_descriptor_type_codes[3] = EC_CSCD_ID;
489 
490 	ctl_set_success(ctsio);
491 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
492 	ctsio->be_move_done = ctl_config_move_done;
493 	ctl_datamove((union ctl_io *)ctsio);
494 	return (retval);
495 }
496 
497 static struct tpc_list *
498 tpc_find_list(struct ctl_lun *lun, uint32_t list_id, uint32_t init_idx)
499 {
500 	struct tpc_list *list;
501 
502 	mtx_assert(&lun->lun_lock, MA_OWNED);
503 	TAILQ_FOREACH(list, &lun->tpc_lists, links) {
504 		if ((list->flags & EC_LIST_ID_USAGE_MASK) !=
505 		     EC_LIST_ID_USAGE_NONE && list->list_id == list_id &&
506 		    list->init_idx == init_idx)
507 			break;
508 	}
509 	return (list);
510 }
511 
512 int
513 ctl_receive_copy_status_lid1(struct ctl_scsiio *ctsio)
514 {
515 	struct ctl_lun *lun;
516 	struct scsi_receive_copy_status_lid1 *cdb;
517 	struct scsi_receive_copy_status_lid1_data *data;
518 	struct tpc_list *list;
519 	struct tpc_list list_copy;
520 	int retval;
521 	int alloc_len, total_len;
522 	uint32_t list_id;
523 
524 	CTL_DEBUG_PRINT(("ctl_receive_copy_status_lid1\n"));
525 
526 	cdb = (struct scsi_receive_copy_status_lid1 *)ctsio->cdb;
527 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
528 
529 	retval = CTL_RETVAL_COMPLETE;
530 
531 	list_id = cdb->list_identifier;
532 	mtx_lock(&lun->lun_lock);
533 	list = tpc_find_list(lun, list_id,
534 	    ctl_get_resindex(&ctsio->io_hdr.nexus));
535 	if (list == NULL) {
536 		mtx_unlock(&lun->lun_lock);
537 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
538 		    /*command*/ 1, /*field*/ 2, /*bit_valid*/ 0,
539 		    /*bit*/ 0);
540 		ctl_done((union ctl_io *)ctsio);
541 		return (retval);
542 	}
543 	list_copy = *list;
544 	if (list->completed) {
545 		TAILQ_REMOVE(&lun->tpc_lists, list, links);
546 		free(list, M_CTL);
547 	}
548 	mtx_unlock(&lun->lun_lock);
549 
550 	total_len = sizeof(*data);
551 	alloc_len = scsi_4btoul(cdb->length);
552 
553 	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
554 
555 	ctsio->kern_sg_entries = 0;
556 
557 	if (total_len < alloc_len) {
558 		ctsio->residual = alloc_len - total_len;
559 		ctsio->kern_data_len = total_len;
560 		ctsio->kern_total_len = total_len;
561 	} else {
562 		ctsio->residual = 0;
563 		ctsio->kern_data_len = alloc_len;
564 		ctsio->kern_total_len = alloc_len;
565 	}
566 	ctsio->kern_data_resid = 0;
567 	ctsio->kern_rel_offset = 0;
568 
569 	data = (struct scsi_receive_copy_status_lid1_data *)ctsio->kern_data_ptr;
570 	scsi_ulto4b(sizeof(*data) - 4, data->available_data);
571 	if (list_copy.completed) {
572 		if (list_copy.error || list_copy.abort)
573 			data->copy_command_status = RCS_CCS_ERROR;
574 		else
575 			data->copy_command_status = RCS_CCS_COMPLETED;
576 	} else
577 		data->copy_command_status = RCS_CCS_INPROG;
578 	scsi_ulto2b(list_copy.curseg, data->segments_processed);
579 	if (list_copy.curbytes <= UINT32_MAX) {
580 		data->transfer_count_units = RCS_TC_BYTES;
581 		scsi_ulto4b(list_copy.curbytes, data->transfer_count);
582 	} else {
583 		data->transfer_count_units = RCS_TC_MBYTES;
584 		scsi_ulto4b(list_copy.curbytes >> 20, data->transfer_count);
585 	}
586 
587 	ctl_set_success(ctsio);
588 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
589 	ctsio->be_move_done = ctl_config_move_done;
590 	ctl_datamove((union ctl_io *)ctsio);
591 	return (retval);
592 }
593 
594 int
595 ctl_receive_copy_failure_details(struct ctl_scsiio *ctsio)
596 {
597 	struct ctl_lun *lun;
598 	struct scsi_receive_copy_failure_details *cdb;
599 	struct scsi_receive_copy_failure_details_data *data;
600 	struct tpc_list *list;
601 	struct tpc_list list_copy;
602 	int retval;
603 	int alloc_len, total_len;
604 	uint32_t list_id;
605 
606 	CTL_DEBUG_PRINT(("ctl_receive_copy_failure_details\n"));
607 
608 	cdb = (struct scsi_receive_copy_failure_details *)ctsio->cdb;
609 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
610 
611 	retval = CTL_RETVAL_COMPLETE;
612 
613 	list_id = cdb->list_identifier;
614 	mtx_lock(&lun->lun_lock);
615 	list = tpc_find_list(lun, list_id,
616 	    ctl_get_resindex(&ctsio->io_hdr.nexus));
617 	if (list == NULL || !list->completed) {
618 		mtx_unlock(&lun->lun_lock);
619 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
620 		    /*command*/ 1, /*field*/ 2, /*bit_valid*/ 0,
621 		    /*bit*/ 0);
622 		ctl_done((union ctl_io *)ctsio);
623 		return (retval);
624 	}
625 	list_copy = *list;
626 	TAILQ_REMOVE(&lun->tpc_lists, list, links);
627 	free(list, M_CTL);
628 	mtx_unlock(&lun->lun_lock);
629 
630 	total_len = sizeof(*data) + list_copy.sense_len;
631 	alloc_len = scsi_4btoul(cdb->length);
632 
633 	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
634 
635 	ctsio->kern_sg_entries = 0;
636 
637 	if (total_len < alloc_len) {
638 		ctsio->residual = alloc_len - total_len;
639 		ctsio->kern_data_len = total_len;
640 		ctsio->kern_total_len = total_len;
641 	} else {
642 		ctsio->residual = 0;
643 		ctsio->kern_data_len = alloc_len;
644 		ctsio->kern_total_len = alloc_len;
645 	}
646 	ctsio->kern_data_resid = 0;
647 	ctsio->kern_rel_offset = 0;
648 
649 	data = (struct scsi_receive_copy_failure_details_data *)ctsio->kern_data_ptr;
650 	if (list_copy.completed && (list_copy.error || list_copy.abort)) {
651 		scsi_ulto4b(sizeof(*data) - 4 + list_copy.sense_len,
652 		    data->available_data);
653 		data->copy_command_status = RCS_CCS_ERROR;
654 	} else
655 		scsi_ulto4b(0, data->available_data);
656 	scsi_ulto2b(list_copy.sense_len, data->sense_data_length);
657 	memcpy(data->sense_data, &list_copy.sense_data, list_copy.sense_len);
658 
659 	ctl_set_success(ctsio);
660 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
661 	ctsio->be_move_done = ctl_config_move_done;
662 	ctl_datamove((union ctl_io *)ctsio);
663 	return (retval);
664 }
665 
666 int
667 ctl_receive_copy_status_lid4(struct ctl_scsiio *ctsio)
668 {
669 	struct ctl_lun *lun;
670 	struct scsi_receive_copy_status_lid4 *cdb;
671 	struct scsi_receive_copy_status_lid4_data *data;
672 	struct tpc_list *list;
673 	struct tpc_list list_copy;
674 	int retval;
675 	int alloc_len, total_len;
676 	uint32_t list_id;
677 
678 	CTL_DEBUG_PRINT(("ctl_receive_copy_status_lid4\n"));
679 
680 	cdb = (struct scsi_receive_copy_status_lid4 *)ctsio->cdb;
681 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
682 
683 	retval = CTL_RETVAL_COMPLETE;
684 
685 	list_id = scsi_4btoul(cdb->list_identifier);
686 	mtx_lock(&lun->lun_lock);
687 	list = tpc_find_list(lun, list_id,
688 	    ctl_get_resindex(&ctsio->io_hdr.nexus));
689 	if (list == NULL) {
690 		mtx_unlock(&lun->lun_lock);
691 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
692 		    /*command*/ 1, /*field*/ 2, /*bit_valid*/ 0,
693 		    /*bit*/ 0);
694 		ctl_done((union ctl_io *)ctsio);
695 		return (retval);
696 	}
697 	list_copy = *list;
698 	if (list->completed) {
699 		TAILQ_REMOVE(&lun->tpc_lists, list, links);
700 		free(list, M_CTL);
701 	}
702 	mtx_unlock(&lun->lun_lock);
703 
704 	total_len = sizeof(*data) + list_copy.sense_len;
705 	alloc_len = scsi_4btoul(cdb->length);
706 
707 	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
708 
709 	ctsio->kern_sg_entries = 0;
710 
711 	if (total_len < alloc_len) {
712 		ctsio->residual = alloc_len - total_len;
713 		ctsio->kern_data_len = total_len;
714 		ctsio->kern_total_len = total_len;
715 	} else {
716 		ctsio->residual = 0;
717 		ctsio->kern_data_len = alloc_len;
718 		ctsio->kern_total_len = alloc_len;
719 	}
720 	ctsio->kern_data_resid = 0;
721 	ctsio->kern_rel_offset = 0;
722 
723 	data = (struct scsi_receive_copy_status_lid4_data *)ctsio->kern_data_ptr;
724 	scsi_ulto4b(sizeof(*data) - 4 + list_copy.sense_len,
725 	    data->available_data);
726 	data->response_to_service_action = list_copy.service_action;
727 	if (list_copy.completed) {
728 		if (list_copy.error)
729 			data->copy_command_status = RCS_CCS_ERROR;
730 		else if (list_copy.abort)
731 			data->copy_command_status = RCS_CCS_ABORTED;
732 		else
733 			data->copy_command_status = RCS_CCS_COMPLETED;
734 	} else
735 		data->copy_command_status = RCS_CCS_INPROG_FG;
736 	scsi_ulto2b(list_copy.curops, data->operation_counter);
737 	scsi_ulto4b(UINT32_MAX, data->estimated_status_update_delay);
738 	data->transfer_count_units = RCS_TC_BYTES;
739 	scsi_u64to8b(list_copy.curbytes, data->transfer_count);
740 	scsi_ulto2b(list_copy.curseg, data->segments_processed);
741 	data->length_of_the_sense_data_field = list_copy.sense_len;
742 	data->sense_data_length = list_copy.sense_len;
743 	memcpy(data->sense_data, &list_copy.sense_data, list_copy.sense_len);
744 
745 	ctl_set_success(ctsio);
746 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
747 	ctsio->be_move_done = ctl_config_move_done;
748 	ctl_datamove((union ctl_io *)ctsio);
749 	return (retval);
750 }
751 
752 int
753 ctl_copy_operation_abort(struct ctl_scsiio *ctsio)
754 {
755 	struct ctl_lun *lun;
756 	struct scsi_copy_operation_abort *cdb;
757 	struct tpc_list *list;
758 	int retval;
759 	uint32_t list_id;
760 
761 	CTL_DEBUG_PRINT(("ctl_copy_operation_abort\n"));
762 
763 	cdb = (struct scsi_copy_operation_abort *)ctsio->cdb;
764 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
765 
766 	retval = CTL_RETVAL_COMPLETE;
767 
768 	list_id = scsi_4btoul(cdb->list_identifier);
769 	mtx_lock(&lun->lun_lock);
770 	list = tpc_find_list(lun, list_id,
771 	    ctl_get_resindex(&ctsio->io_hdr.nexus));
772 	if (list == NULL) {
773 		mtx_unlock(&lun->lun_lock);
774 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
775 		    /*command*/ 1, /*field*/ 2, /*bit_valid*/ 0,
776 		    /*bit*/ 0);
777 		ctl_done((union ctl_io *)ctsio);
778 		return (retval);
779 	}
780 	list->abort = 1;
781 	mtx_unlock(&lun->lun_lock);
782 
783 	ctl_set_success(ctsio);
784 	ctl_done((union ctl_io *)ctsio);
785 	return (retval);
786 }
787 
788 static uint64_t
789 tpc_resolve(struct tpc_list *list, uint16_t idx, uint32_t *ss)
790 {
791 
792 	if (idx == 0xffff) {
793 		if (ss && list->lun->be_lun)
794 			*ss = list->lun->be_lun->blocksize;
795 		return (list->lun->lun);
796 	}
797 	if (idx >= list->ncscd)
798 		return (UINT64_MAX);
799 	return (tpcl_resolve(list->init_port, &list->cscd[idx], ss));
800 }
801 
802 static int
803 tpc_process_b2b(struct tpc_list *list)
804 {
805 	struct scsi_ec_segment_b2b *seg;
806 	struct scsi_ec_cscd_dtsp *sdstp, *ddstp;
807 	struct tpc_io *tior, *tiow;
808 	struct runl run, *prun;
809 	uint64_t sl, dl;
810 	off_t srclba, dstlba, numbytes, donebytes, roundbytes;
811 	int numlba;
812 	uint32_t srcblock, dstblock;
813 
814 	if (list->stage == 1) {
815 		while ((tior = TAILQ_FIRST(&list->allio)) != NULL) {
816 			TAILQ_REMOVE(&list->allio, tior, links);
817 			ctl_free_io(tior->io);
818 			free(tior, M_CTL);
819 		}
820 		free(list->buf, M_CTL);
821 		if (list->abort) {
822 			ctl_set_task_aborted(list->ctsio);
823 			return (CTL_RETVAL_ERROR);
824 		} else if (list->error) {
825 			ctl_set_sense(list->ctsio, /*current_error*/ 1,
826 			    /*sense_key*/ SSD_KEY_COPY_ABORTED,
827 			    /*asc*/ 0x0d, /*ascq*/ 0x01, SSD_ELEM_NONE);
828 			return (CTL_RETVAL_ERROR);
829 		}
830 		list->cursectors += list->segsectors;
831 		list->curbytes += list->segbytes;
832 		return (CTL_RETVAL_COMPLETE);
833 	}
834 
835 	TAILQ_INIT(&list->allio);
836 	seg = (struct scsi_ec_segment_b2b *)list->seg[list->curseg];
837 	sl = tpc_resolve(list, scsi_2btoul(seg->src_cscd), &srcblock);
838 	dl = tpc_resolve(list, scsi_2btoul(seg->dst_cscd), &dstblock);
839 	if (sl >= CTL_MAX_LUNS || dl >= CTL_MAX_LUNS) {
840 		ctl_set_sense(list->ctsio, /*current_error*/ 1,
841 		    /*sense_key*/ SSD_KEY_COPY_ABORTED,
842 		    /*asc*/ 0x08, /*ascq*/ 0x04, SSD_ELEM_NONE);
843 		return (CTL_RETVAL_ERROR);
844 	}
845 	sdstp = &list->cscd[scsi_2btoul(seg->src_cscd)].dtsp;
846 	if (scsi_3btoul(sdstp->block_length) != 0)
847 		srcblock = scsi_3btoul(sdstp->block_length);
848 	ddstp = &list->cscd[scsi_2btoul(seg->dst_cscd)].dtsp;
849 	if (scsi_3btoul(ddstp->block_length) != 0)
850 		dstblock = scsi_3btoul(ddstp->block_length);
851 	numlba = scsi_2btoul(seg->number_of_blocks);
852 	if (seg->flags & EC_SEG_DC)
853 		numbytes = (off_t)numlba * dstblock;
854 	else
855 		numbytes = (off_t)numlba * srcblock;
856 	srclba = scsi_8btou64(seg->src_lba);
857 	dstlba = scsi_8btou64(seg->dst_lba);
858 
859 //	printf("Copy %ju bytes from %ju @ %ju to %ju @ %ju\n",
860 //	    (uintmax_t)numbytes, sl, scsi_8btou64(seg->src_lba),
861 //	    dl, scsi_8btou64(seg->dst_lba));
862 
863 	if (numbytes == 0)
864 		return (CTL_RETVAL_COMPLETE);
865 
866 	if (numbytes % srcblock != 0 || numbytes % dstblock != 0) {
867 		ctl_set_sense(list->ctsio, /*current_error*/ 1,
868 		    /*sense_key*/ SSD_KEY_COPY_ABORTED,
869 		    /*asc*/ 0x26, /*ascq*/ 0x0A, SSD_ELEM_NONE);
870 		return (CTL_RETVAL_ERROR);
871 	}
872 
873 	list->buf = malloc(numbytes, M_CTL, M_WAITOK);
874 	list->segbytes = numbytes;
875 	list->segsectors = numbytes / dstblock;
876 	donebytes = 0;
877 	TAILQ_INIT(&run);
878 	prun = &run;
879 	list->tbdio = 1;
880 	while (donebytes < numbytes) {
881 		roundbytes = MIN(numbytes - donebytes, TPC_MAX_IO_SIZE);
882 
883 		tior = malloc(sizeof(*tior), M_CTL, M_WAITOK | M_ZERO);
884 		TAILQ_INIT(&tior->run);
885 		tior->list = list;
886 		TAILQ_INSERT_TAIL(&list->allio, tior, links);
887 		tior->io = tpcl_alloc_io();
888 		ctl_scsi_read_write(tior->io,
889 				    /*data_ptr*/ &list->buf[donebytes],
890 				    /*data_len*/ roundbytes,
891 				    /*read_op*/ 1,
892 				    /*byte2*/ 0,
893 				    /*minimum_cdb_size*/ 0,
894 				    /*lba*/ srclba + donebytes / srcblock,
895 				    /*num_blocks*/ roundbytes / srcblock,
896 				    /*tag_type*/ CTL_TAG_SIMPLE,
897 				    /*control*/ 0);
898 		tior->io->io_hdr.retries = 3;
899 		tior->lun = sl;
900 		tior->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tior;
901 
902 		tiow = malloc(sizeof(*tior), M_CTL, M_WAITOK | M_ZERO);
903 		TAILQ_INIT(&tiow->run);
904 		tiow->list = list;
905 		TAILQ_INSERT_TAIL(&list->allio, tiow, links);
906 		tiow->io = tpcl_alloc_io();
907 		ctl_scsi_read_write(tiow->io,
908 				    /*data_ptr*/ &list->buf[donebytes],
909 				    /*data_len*/ roundbytes,
910 				    /*read_op*/ 0,
911 				    /*byte2*/ 0,
912 				    /*minimum_cdb_size*/ 0,
913 				    /*lba*/ dstlba + donebytes / dstblock,
914 				    /*num_blocks*/ roundbytes / dstblock,
915 				    /*tag_type*/ CTL_TAG_SIMPLE,
916 				    /*control*/ 0);
917 		tiow->io->io_hdr.retries = 3;
918 		tiow->lun = dl;
919 		tiow->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tior;
920 
921 		TAILQ_INSERT_TAIL(&tior->run, tiow, rlinks);
922 		TAILQ_INSERT_TAIL(prun, tior, rlinks);
923 		prun = &tior->run;
924 		donebytes += roundbytes;
925 	}
926 
927 	while ((tior = TAILQ_FIRST(&run)) != NULL) {
928 		TAILQ_REMOVE(&run, tior, rlinks);
929 		if (tpcl_queue(tior->io, tior->lun) != CTL_RETVAL_COMPLETE)
930 			panic("tpcl_queue() error");
931 	}
932 
933 	list->stage++;
934 	return (CTL_RETVAL_QUEUED);
935 }
936 
937 static int
938 tpc_process_verify(struct tpc_list *list)
939 {
940 	struct scsi_ec_segment_verify *seg;
941 	struct tpc_io *tio;
942 	uint64_t sl;
943 
944 	if (list->stage == 1) {
945 		while ((tio = TAILQ_FIRST(&list->allio)) != NULL) {
946 			TAILQ_REMOVE(&list->allio, tio, links);
947 			ctl_free_io(tio->io);
948 			free(tio, M_CTL);
949 		}
950 		if (list->abort) {
951 			ctl_set_task_aborted(list->ctsio);
952 			return (CTL_RETVAL_ERROR);
953 		} else if (list->error) {
954 			ctl_set_sense(list->ctsio, /*current_error*/ 1,
955 			    /*sense_key*/ SSD_KEY_COPY_ABORTED,
956 			    /*asc*/ 0x0d, /*ascq*/ 0x01, SSD_ELEM_NONE);
957 			return (CTL_RETVAL_ERROR);
958 		} else
959 			return (CTL_RETVAL_COMPLETE);
960 	}
961 
962 	TAILQ_INIT(&list->allio);
963 	seg = (struct scsi_ec_segment_verify *)list->seg[list->curseg];
964 	sl = tpc_resolve(list, scsi_2btoul(seg->src_cscd), NULL);
965 	if (sl >= CTL_MAX_LUNS) {
966 		ctl_set_sense(list->ctsio, /*current_error*/ 1,
967 		    /*sense_key*/ SSD_KEY_COPY_ABORTED,
968 		    /*asc*/ 0x08, /*ascq*/ 0x04, SSD_ELEM_NONE);
969 		return (CTL_RETVAL_ERROR);
970 	}
971 
972 //	printf("Verify %ju\n", sl);
973 
974 	if ((seg->tur & 0x01) == 0)
975 		return (CTL_RETVAL_COMPLETE);
976 
977 	list->tbdio = 1;
978 	tio = malloc(sizeof(*tio), M_CTL, M_WAITOK | M_ZERO);
979 	TAILQ_INIT(&tio->run);
980 	tio->list = list;
981 	TAILQ_INSERT_TAIL(&list->allio, tio, links);
982 	tio->io = tpcl_alloc_io();
983 	ctl_scsi_tur(tio->io, /*tag_type*/ CTL_TAG_SIMPLE, /*control*/ 0);
984 	tio->io->io_hdr.retries = 3;
985 	tio->lun = sl;
986 	tio->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tio;
987 	list->stage++;
988 	if (tpcl_queue(tio->io, tio->lun) != CTL_RETVAL_COMPLETE)
989 		panic("tpcl_queue() error");
990 	return (CTL_RETVAL_QUEUED);
991 }
992 
993 static int
994 tpc_process_register_key(struct tpc_list *list)
995 {
996 	struct scsi_ec_segment_register_key *seg;
997 	struct tpc_io *tio;
998 	uint64_t dl;
999 	int datalen;
1000 
1001 	if (list->stage == 1) {
1002 		while ((tio = TAILQ_FIRST(&list->allio)) != NULL) {
1003 			TAILQ_REMOVE(&list->allio, tio, links);
1004 			ctl_free_io(tio->io);
1005 			free(tio, M_CTL);
1006 		}
1007 		free(list->buf, M_CTL);
1008 		if (list->abort) {
1009 			ctl_set_task_aborted(list->ctsio);
1010 			return (CTL_RETVAL_ERROR);
1011 		} else if (list->error) {
1012 			ctl_set_sense(list->ctsio, /*current_error*/ 1,
1013 			    /*sense_key*/ SSD_KEY_COPY_ABORTED,
1014 			    /*asc*/ 0x0d, /*ascq*/ 0x01, SSD_ELEM_NONE);
1015 			return (CTL_RETVAL_ERROR);
1016 		} else
1017 			return (CTL_RETVAL_COMPLETE);
1018 	}
1019 
1020 	TAILQ_INIT(&list->allio);
1021 	seg = (struct scsi_ec_segment_register_key *)list->seg[list->curseg];
1022 	dl = tpc_resolve(list, scsi_2btoul(seg->dst_cscd), NULL);
1023 	if (dl >= CTL_MAX_LUNS) {
1024 		ctl_set_sense(list->ctsio, /*current_error*/ 1,
1025 		    /*sense_key*/ SSD_KEY_COPY_ABORTED,
1026 		    /*asc*/ 0x08, /*ascq*/ 0x04, SSD_ELEM_NONE);
1027 		return (CTL_RETVAL_ERROR);
1028 	}
1029 
1030 //	printf("Register Key %ju\n", dl);
1031 
1032 	list->tbdio = 1;
1033 	tio = malloc(sizeof(*tio), M_CTL, M_WAITOK | M_ZERO);
1034 	TAILQ_INIT(&tio->run);
1035 	tio->list = list;
1036 	TAILQ_INSERT_TAIL(&list->allio, tio, links);
1037 	tio->io = tpcl_alloc_io();
1038 	datalen = sizeof(struct scsi_per_res_out_parms);
1039 	list->buf = malloc(datalen, M_CTL, M_WAITOK);
1040 	ctl_scsi_persistent_res_out(tio->io,
1041 	    list->buf, datalen, SPRO_REGISTER, -1,
1042 	    scsi_8btou64(seg->res_key), scsi_8btou64(seg->sa_res_key),
1043 	    /*tag_type*/ CTL_TAG_SIMPLE, /*control*/ 0);
1044 	tio->io->io_hdr.retries = 3;
1045 	tio->lun = dl;
1046 	tio->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tio;
1047 	list->stage++;
1048 	if (tpcl_queue(tio->io, tio->lun) != CTL_RETVAL_COMPLETE)
1049 		panic("tpcl_queue() error");
1050 	return (CTL_RETVAL_QUEUED);
1051 }
1052 
1053 static off_t
1054 tpc_ranges_length(struct scsi_range_desc *range, int nrange)
1055 {
1056 	off_t length = 0;
1057 	int r;
1058 
1059 	for (r = 0; r < nrange; r++)
1060 		length += scsi_4btoul(range[r].length);
1061 	return (length);
1062 }
1063 
1064 static int
1065 tpc_skip_ranges(struct scsi_range_desc *range, int nrange, off_t skip,
1066     int *srange, off_t *soffset)
1067 {
1068 	off_t off;
1069 	int r;
1070 
1071 	r = 0;
1072 	off = 0;
1073 	while (r < nrange) {
1074 		if (skip - off < scsi_4btoul(range[r].length)) {
1075 			*srange = r;
1076 			*soffset = skip - off;
1077 			return (0);
1078 		}
1079 		off += scsi_4btoul(range[r].length);
1080 		r++;
1081 	}
1082 	return (-1);
1083 }
1084 
1085 static int
1086 tpc_process_wut(struct tpc_list *list)
1087 {
1088 	struct tpc_io *tio, *tior, *tiow;
1089 	struct runl run, *prun;
1090 	int drange, srange;
1091 	off_t doffset, soffset;
1092 	off_t srclba, dstlba, numbytes, donebytes, roundbytes;
1093 	uint32_t srcblock, dstblock;
1094 
1095 	if (list->stage > 0) {
1096 		/* Cleanup after previous rounds. */
1097 		while ((tio = TAILQ_FIRST(&list->allio)) != NULL) {
1098 			TAILQ_REMOVE(&list->allio, tio, links);
1099 			ctl_free_io(tio->io);
1100 			free(tio, M_CTL);
1101 		}
1102 		free(list->buf, M_CTL);
1103 		if (list->abort) {
1104 			ctl_set_task_aborted(list->ctsio);
1105 			return (CTL_RETVAL_ERROR);
1106 		} else if (list->error) {
1107 			ctl_set_sense(list->ctsio, /*current_error*/ 1,
1108 			    /*sense_key*/ SSD_KEY_COPY_ABORTED,
1109 			    /*asc*/ 0x0d, /*ascq*/ 0x01, SSD_ELEM_NONE);
1110 			return (CTL_RETVAL_ERROR);
1111 		}
1112 		list->cursectors += list->segsectors;
1113 		list->curbytes += list->segbytes;
1114 	}
1115 
1116 	/* Check where we are on destination ranges list. */
1117 	if (tpc_skip_ranges(list->range, list->nrange, list->cursectors,
1118 	    &drange, &doffset) != 0)
1119 		return (CTL_RETVAL_COMPLETE);
1120 	dstblock = list->lun->be_lun->blocksize;
1121 
1122 	/* Check where we are on source ranges list. */
1123 	srcblock = list->token->blocksize;
1124 	if (tpc_skip_ranges(list->token->range, list->token->nrange,
1125 	    list->offset_into_rod + list->cursectors * dstblock / srcblock,
1126 	    &srange, &soffset) != 0) {
1127 		ctl_set_sense(list->ctsio, /*current_error*/ 1,
1128 		    /*sense_key*/ SSD_KEY_COPY_ABORTED,
1129 		    /*asc*/ 0x0d, /*ascq*/ 0x04, SSD_ELEM_NONE);
1130 		return (CTL_RETVAL_ERROR);
1131 	}
1132 
1133 	srclba = scsi_8btou64(list->token->range[srange].lba) + soffset;
1134 	numbytes = srcblock * omin(TPC_MAX_IOCHUNK_SIZE / srcblock,
1135 	    (scsi_4btoul(list->token->range[srange].length) - soffset));
1136 	dstlba = scsi_8btou64(list->range[drange].lba) + doffset;
1137 	numbytes = omin(numbytes,
1138 	    dstblock * omin(TPC_MAX_IOCHUNK_SIZE / dstblock,
1139 	    (scsi_4btoul(list->range[drange].length) - doffset)));
1140 
1141 	if (numbytes % srcblock != 0 || numbytes % dstblock != 0) {
1142 		ctl_set_sense(list->ctsio, /*current_error*/ 1,
1143 		    /*sense_key*/ SSD_KEY_COPY_ABORTED,
1144 		    /*asc*/ 0x26, /*ascq*/ 0x0A, SSD_ELEM_NONE);
1145 		return (CTL_RETVAL_ERROR);
1146 	}
1147 
1148 	list->buf = malloc(numbytes, M_CTL, M_WAITOK |
1149 	    (list->token == NULL ? M_ZERO : 0));
1150 	list->segbytes = numbytes;
1151 	list->segsectors = numbytes / dstblock;
1152 //printf("Copy chunk of %ju sectors from %ju to %ju\n", list->segsectors,
1153 //    srclba, dstlba);
1154 	donebytes = 0;
1155 	TAILQ_INIT(&run);
1156 	prun = &run;
1157 	list->tbdio = 1;
1158 	TAILQ_INIT(&list->allio);
1159 	while (donebytes < numbytes) {
1160 		roundbytes = MIN(numbytes - donebytes, TPC_MAX_IO_SIZE);
1161 
1162 		tior = malloc(sizeof(*tior), M_CTL, M_WAITOK | M_ZERO);
1163 		TAILQ_INIT(&tior->run);
1164 		tior->list = list;
1165 		TAILQ_INSERT_TAIL(&list->allio, tior, links);
1166 		tior->io = tpcl_alloc_io();
1167 		ctl_scsi_read_write(tior->io,
1168 				    /*data_ptr*/ &list->buf[donebytes],
1169 				    /*data_len*/ roundbytes,
1170 				    /*read_op*/ 1,
1171 				    /*byte2*/ 0,
1172 				    /*minimum_cdb_size*/ 0,
1173 				    /*lba*/ srclba + donebytes / srcblock,
1174 				    /*num_blocks*/ roundbytes / srcblock,
1175 				    /*tag_type*/ CTL_TAG_SIMPLE,
1176 				    /*control*/ 0);
1177 		tior->io->io_hdr.retries = 3;
1178 		tior->lun = list->token->lun;
1179 		tior->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tior;
1180 
1181 		tiow = malloc(sizeof(*tiow), M_CTL, M_WAITOK | M_ZERO);
1182 		TAILQ_INIT(&tiow->run);
1183 		tiow->list = list;
1184 		TAILQ_INSERT_TAIL(&list->allio, tiow, links);
1185 		tiow->io = tpcl_alloc_io();
1186 		ctl_scsi_read_write(tiow->io,
1187 				    /*data_ptr*/ &list->buf[donebytes],
1188 				    /*data_len*/ roundbytes,
1189 				    /*read_op*/ 0,
1190 				    /*byte2*/ 0,
1191 				    /*minimum_cdb_size*/ 0,
1192 				    /*lba*/ dstlba + donebytes / dstblock,
1193 				    /*num_blocks*/ roundbytes / dstblock,
1194 				    /*tag_type*/ CTL_TAG_SIMPLE,
1195 				    /*control*/ 0);
1196 		tiow->io->io_hdr.retries = 3;
1197 		tiow->lun = list->lun->lun;
1198 		tiow->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tiow;
1199 
1200 		TAILQ_INSERT_TAIL(&tior->run, tiow, rlinks);
1201 		TAILQ_INSERT_TAIL(prun, tior, rlinks);
1202 		prun = &tior->run;
1203 		donebytes += roundbytes;
1204 	}
1205 
1206 	while ((tior = TAILQ_FIRST(&run)) != NULL) {
1207 		TAILQ_REMOVE(&run, tior, rlinks);
1208 		if (tpcl_queue(tior->io, tior->lun) != CTL_RETVAL_COMPLETE)
1209 			panic("tpcl_queue() error");
1210 	}
1211 
1212 	list->stage++;
1213 	return (CTL_RETVAL_QUEUED);
1214 }
1215 
1216 static int
1217 tpc_process_zero_wut(struct tpc_list *list)
1218 {
1219 	struct tpc_io *tio, *tiow;
1220 	struct runl run, *prun;
1221 	int r;
1222 	uint32_t dstblock, len;
1223 
1224 	if (list->stage > 0) {
1225 complete:
1226 		/* Cleanup after previous rounds. */
1227 		while ((tio = TAILQ_FIRST(&list->allio)) != NULL) {
1228 			TAILQ_REMOVE(&list->allio, tio, links);
1229 			ctl_free_io(tio->io);
1230 			free(tio, M_CTL);
1231 		}
1232 		free(list->buf, M_CTL);
1233 		if (list->abort) {
1234 			ctl_set_task_aborted(list->ctsio);
1235 			return (CTL_RETVAL_ERROR);
1236 		} else if (list->error) {
1237 			ctl_set_sense(list->ctsio, /*current_error*/ 1,
1238 			    /*sense_key*/ SSD_KEY_COPY_ABORTED,
1239 			    /*asc*/ 0x0d, /*ascq*/ 0x01, SSD_ELEM_NONE);
1240 			return (CTL_RETVAL_ERROR);
1241 		}
1242 		list->cursectors += list->segsectors;
1243 		list->curbytes += list->segbytes;
1244 		return (CTL_RETVAL_COMPLETE);
1245 	}
1246 
1247 	dstblock = list->lun->be_lun->blocksize;
1248 	list->buf = malloc(dstblock, M_CTL, M_WAITOK | M_ZERO);
1249 	TAILQ_INIT(&run);
1250 	prun = &run;
1251 	list->tbdio = 1;
1252 	TAILQ_INIT(&list->allio);
1253 	list->segsectors = 0;
1254 	for (r = 0; r < list->nrange; r++) {
1255 		len = scsi_4btoul(list->range[r].length);
1256 		if (len == 0)
1257 			continue;
1258 
1259 		tiow = malloc(sizeof(*tiow), M_CTL, M_WAITOK | M_ZERO);
1260 		TAILQ_INIT(&tiow->run);
1261 		tiow->list = list;
1262 		TAILQ_INSERT_TAIL(&list->allio, tiow, links);
1263 		tiow->io = tpcl_alloc_io();
1264 		ctl_scsi_write_same(tiow->io,
1265 				    /*data_ptr*/ list->buf,
1266 				    /*data_len*/ dstblock,
1267 				    /*byte2*/ 0,
1268 				    /*lba*/ scsi_8btou64(list->range[r].lba),
1269 				    /*num_blocks*/ len,
1270 				    /*tag_type*/ CTL_TAG_SIMPLE,
1271 				    /*control*/ 0);
1272 		tiow->io->io_hdr.retries = 3;
1273 		tiow->lun = list->lun->lun;
1274 		tiow->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tiow;
1275 
1276 		TAILQ_INSERT_TAIL(prun, tiow, rlinks);
1277 		prun = &tiow->run;
1278 		list->segsectors += len;
1279 	}
1280 	list->segbytes = list->segsectors * dstblock;
1281 
1282 	if (TAILQ_EMPTY(&run))
1283 		goto complete;
1284 
1285 	while ((tiow = TAILQ_FIRST(&run)) != NULL) {
1286 		TAILQ_REMOVE(&run, tiow, rlinks);
1287 		if (tpcl_queue(tiow->io, tiow->lun) != CTL_RETVAL_COMPLETE)
1288 			panic("tpcl_queue() error");
1289 	}
1290 
1291 	list->stage++;
1292 	return (CTL_RETVAL_QUEUED);
1293 }
1294 
1295 static void
1296 tpc_process(struct tpc_list *list)
1297 {
1298 	struct ctl_lun *lun = list->lun;
1299 	struct scsi_ec_segment *seg;
1300 	struct ctl_scsiio *ctsio = list->ctsio;
1301 	int retval = CTL_RETVAL_COMPLETE;
1302 
1303 	if (list->service_action == EC_WUT) {
1304 		if (list->token != NULL)
1305 			retval = tpc_process_wut(list);
1306 		else
1307 			retval = tpc_process_zero_wut(list);
1308 		if (retval == CTL_RETVAL_QUEUED)
1309 			return;
1310 		if (retval == CTL_RETVAL_ERROR) {
1311 			list->error = 1;
1312 			goto done;
1313 		}
1314 	} else {
1315 //printf("ZZZ %d cscd, %d segs\n", list->ncscd, list->nseg);
1316 		while (list->curseg < list->nseg) {
1317 			seg = list->seg[list->curseg];
1318 			switch (seg->type_code) {
1319 			case EC_SEG_B2B:
1320 				retval = tpc_process_b2b(list);
1321 				break;
1322 			case EC_SEG_VERIFY:
1323 				retval = tpc_process_verify(list);
1324 				break;
1325 			case EC_SEG_REGISTER_KEY:
1326 				retval = tpc_process_register_key(list);
1327 				break;
1328 			default:
1329 				ctl_set_sense(ctsio, /*current_error*/ 1,
1330 				    /*sense_key*/ SSD_KEY_COPY_ABORTED,
1331 				    /*asc*/ 0x26, /*ascq*/ 0x09, SSD_ELEM_NONE);
1332 				goto done;
1333 			}
1334 			if (retval == CTL_RETVAL_QUEUED)
1335 				return;
1336 			if (retval == CTL_RETVAL_ERROR) {
1337 				list->error = 1;
1338 				goto done;
1339 			}
1340 			list->curseg++;
1341 			list->stage = 0;
1342 		}
1343 	}
1344 
1345 	ctl_set_success(ctsio);
1346 
1347 done:
1348 //printf("ZZZ done\n");
1349 	free(list->params, M_CTL);
1350 	list->params = NULL;
1351 	if (list->token) {
1352 		mtx_lock(&control_softc->ctl_lock);
1353 		if (--list->token->active == 0)
1354 			list->token->last_active = time_uptime;
1355 		mtx_unlock(&control_softc->ctl_lock);
1356 		list->token = NULL;
1357 	}
1358 	mtx_lock(&lun->lun_lock);
1359 	if ((list->flags & EC_LIST_ID_USAGE_MASK) == EC_LIST_ID_USAGE_NONE) {
1360 		TAILQ_REMOVE(&lun->tpc_lists, list, links);
1361 		free(list, M_CTL);
1362 	} else {
1363 		list->completed = 1;
1364 		list->last_active = time_uptime;
1365 		list->sense_data = ctsio->sense_data;
1366 		list->sense_len = ctsio->sense_len;
1367 		list->scsi_status = ctsio->scsi_status;
1368 	}
1369 	mtx_unlock(&lun->lun_lock);
1370 
1371 	ctl_done((union ctl_io *)ctsio);
1372 }
1373 
1374 /*
1375  * For any sort of check condition, busy, etc., we just retry.  We do not
1376  * decrement the retry count for unit attention type errors.  These are
1377  * normal, and we want to save the retry count for "real" errors.  Otherwise,
1378  * we could end up with situations where a command will succeed in some
1379  * situations and fail in others, depending on whether a unit attention is
1380  * pending.  Also, some of our error recovery actions, most notably the
1381  * LUN reset action, will cause a unit attention.
1382  *
1383  * We can add more detail here later if necessary.
1384  */
1385 static tpc_error_action
1386 tpc_checkcond_parse(union ctl_io *io)
1387 {
1388 	tpc_error_action error_action;
1389 	int error_code, sense_key, asc, ascq;
1390 
1391 	/*
1392 	 * Default to retrying the command.
1393 	 */
1394 	error_action = TPC_ERR_RETRY;
1395 
1396 	scsi_extract_sense_len(&io->scsiio.sense_data,
1397 			       io->scsiio.sense_len,
1398 			       &error_code,
1399 			       &sense_key,
1400 			       &asc,
1401 			       &ascq,
1402 			       /*show_errors*/ 1);
1403 
1404 	switch (error_code) {
1405 	case SSD_DEFERRED_ERROR:
1406 	case SSD_DESC_DEFERRED_ERROR:
1407 		error_action |= TPC_ERR_NO_DECREMENT;
1408 		break;
1409 	case SSD_CURRENT_ERROR:
1410 	case SSD_DESC_CURRENT_ERROR:
1411 	default:
1412 		switch (sense_key) {
1413 		case SSD_KEY_UNIT_ATTENTION:
1414 			error_action |= TPC_ERR_NO_DECREMENT;
1415 			break;
1416 		case SSD_KEY_HARDWARE_ERROR:
1417 			/*
1418 			 * This is our generic "something bad happened"
1419 			 * error code.  It often isn't recoverable.
1420 			 */
1421 			if ((asc == 0x44) && (ascq == 0x00))
1422 				error_action = TPC_ERR_FAIL;
1423 			break;
1424 		case SSD_KEY_NOT_READY:
1425 			/*
1426 			 * If the LUN is powered down, there likely isn't
1427 			 * much point in retrying right now.
1428 			 */
1429 			if ((asc == 0x04) && (ascq == 0x02))
1430 				error_action = TPC_ERR_FAIL;
1431 			/*
1432 			 * If the LUN is offline, there probably isn't much
1433 			 * point in retrying, either.
1434 			 */
1435 			if ((asc == 0x04) && (ascq == 0x03))
1436 				error_action = TPC_ERR_FAIL;
1437 			break;
1438 		}
1439 	}
1440 	return (error_action);
1441 }
1442 
1443 static tpc_error_action
1444 tpc_error_parse(union ctl_io *io)
1445 {
1446 	tpc_error_action error_action = TPC_ERR_RETRY;
1447 
1448 	switch (io->io_hdr.io_type) {
1449 	case CTL_IO_SCSI:
1450 		switch (io->io_hdr.status & CTL_STATUS_MASK) {
1451 		case CTL_SCSI_ERROR:
1452 			switch (io->scsiio.scsi_status) {
1453 			case SCSI_STATUS_CHECK_COND:
1454 				error_action = tpc_checkcond_parse(io);
1455 				break;
1456 			default:
1457 				break;
1458 			}
1459 			break;
1460 		default:
1461 			break;
1462 		}
1463 		break;
1464 	case CTL_IO_TASK:
1465 		break;
1466 	default:
1467 		panic("%s: invalid ctl_io type %d\n", __func__,
1468 		      io->io_hdr.io_type);
1469 		break;
1470 	}
1471 	return (error_action);
1472 }
1473 
1474 void
1475 tpc_done(union ctl_io *io)
1476 {
1477 	struct tpc_io *tio, *tior;
1478 
1479 	/*
1480 	 * Very minimal retry logic.  We basically retry if we got an error
1481 	 * back, and the retry count is greater than 0.  If we ever want
1482 	 * more sophisticated initiator type behavior, the CAM error
1483 	 * recovery code in ../common might be helpful.
1484 	 */
1485 	tio = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1486 	if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
1487 	 && (io->io_hdr.retries > 0)) {
1488 		ctl_io_status old_status;
1489 		tpc_error_action error_action;
1490 
1491 		error_action = tpc_error_parse(io);
1492 		switch (error_action & TPC_ERR_MASK) {
1493 		case TPC_ERR_FAIL:
1494 			break;
1495 		case TPC_ERR_RETRY:
1496 		default:
1497 			if ((error_action & TPC_ERR_NO_DECREMENT) == 0)
1498 				io->io_hdr.retries--;
1499 			old_status = io->io_hdr.status;
1500 			io->io_hdr.status = CTL_STATUS_NONE;
1501 			io->io_hdr.flags &= ~CTL_FLAG_ABORT;
1502 			io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1503 			if (tpcl_queue(io, tio->lun) != CTL_RETVAL_COMPLETE) {
1504 				printf("%s: error returned from ctl_queue()!\n",
1505 				       __func__);
1506 				io->io_hdr.status = old_status;
1507 			} else
1508 				return;
1509 		}
1510 	}
1511 
1512 	if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
1513 		tio->list->error = 1;
1514 	else
1515 		atomic_add_int(&tio->list->curops, 1);
1516 	if (!tio->list->error && !tio->list->abort) {
1517 		while ((tior = TAILQ_FIRST(&tio->run)) != NULL) {
1518 			TAILQ_REMOVE(&tio->run, tior, rlinks);
1519 			atomic_add_int(&tio->list->tbdio, 1);
1520 			if (tpcl_queue(tior->io, tior->lun) != CTL_RETVAL_COMPLETE)
1521 				panic("tpcl_queue() error");
1522 		}
1523 	}
1524 	if (atomic_fetchadd_int(&tio->list->tbdio, -1) == 1)
1525 		tpc_process(tio->list);
1526 }
1527 
1528 int
1529 ctl_extended_copy_lid1(struct ctl_scsiio *ctsio)
1530 {
1531 	struct scsi_extended_copy *cdb;
1532 	struct scsi_extended_copy_lid1_data *data;
1533 	struct ctl_lun *lun;
1534 	struct tpc_list *list, *tlist;
1535 	uint8_t *ptr;
1536 	char *value;
1537 	int len, off, lencscd, lenseg, leninl, nseg;
1538 
1539 	CTL_DEBUG_PRINT(("ctl_extended_copy_lid1\n"));
1540 
1541 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
1542 	cdb = (struct scsi_extended_copy *)ctsio->cdb;
1543 	len = scsi_4btoul(cdb->length);
1544 
1545 	if (len < sizeof(struct scsi_extended_copy_lid1_data) ||
1546 	    len > sizeof(struct scsi_extended_copy_lid1_data) +
1547 	    TPC_MAX_LIST + TPC_MAX_INLINE) {
1548 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
1549 		    /*field*/ 9, /*bit_valid*/ 0, /*bit*/ 0);
1550 		goto done;
1551 	}
1552 
1553 	/*
1554 	 * If we've got a kernel request that hasn't been malloced yet,
1555 	 * malloc it and tell the caller the data buffer is here.
1556 	 */
1557 	if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
1558 		ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
1559 		ctsio->kern_data_len = len;
1560 		ctsio->kern_total_len = len;
1561 		ctsio->kern_data_resid = 0;
1562 		ctsio->kern_rel_offset = 0;
1563 		ctsio->kern_sg_entries = 0;
1564 		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
1565 		ctsio->be_move_done = ctl_config_move_done;
1566 		ctl_datamove((union ctl_io *)ctsio);
1567 
1568 		return (CTL_RETVAL_COMPLETE);
1569 	}
1570 
1571 	data = (struct scsi_extended_copy_lid1_data *)ctsio->kern_data_ptr;
1572 	lencscd = scsi_2btoul(data->cscd_list_length);
1573 	lenseg = scsi_4btoul(data->segment_list_length);
1574 	leninl = scsi_4btoul(data->inline_data_length);
1575 	if (len < sizeof(struct scsi_extended_copy_lid1_data) +
1576 	    lencscd + lenseg + leninl ||
1577 	    leninl > TPC_MAX_INLINE) {
1578 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 0,
1579 		    /*field*/ 2, /*bit_valid*/ 0, /*bit*/ 0);
1580 		goto done;
1581 	}
1582 	if (lencscd > TPC_MAX_CSCDS * sizeof(struct scsi_ec_cscd)) {
1583 		ctl_set_sense(ctsio, /*current_error*/ 1,
1584 		    /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1585 		    /*asc*/ 0x26, /*ascq*/ 0x06, SSD_ELEM_NONE);
1586 		goto done;
1587 	}
1588 	if (lencscd + lenseg > TPC_MAX_LIST) {
1589 		ctl_set_param_len_error(ctsio);
1590 		goto done;
1591 	}
1592 
1593 	list = malloc(sizeof(struct tpc_list), M_CTL, M_WAITOK | M_ZERO);
1594 	list->service_action = cdb->service_action;
1595 	value = ctl_get_opt(&lun->be_lun->options, "insecure_tpc");
1596 	if (value != NULL && strcmp(value, "on") == 0)
1597 		list->init_port = -1;
1598 	else
1599 		list->init_port = ctsio->io_hdr.nexus.targ_port;
1600 	list->init_idx = ctl_get_resindex(&ctsio->io_hdr.nexus);
1601 	list->list_id = data->list_identifier;
1602 	list->flags = data->flags;
1603 	list->params = ctsio->kern_data_ptr;
1604 	list->cscd = (struct scsi_ec_cscd *)&data->data[0];
1605 	ptr = &data->data[lencscd];
1606 	for (nseg = 0, off = 0; off < lenseg; nseg++) {
1607 		if (nseg >= TPC_MAX_SEGS) {
1608 			free(list, M_CTL);
1609 			ctl_set_sense(ctsio, /*current_error*/ 1,
1610 			    /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1611 			    /*asc*/ 0x26, /*ascq*/ 0x08, SSD_ELEM_NONE);
1612 			goto done;
1613 		}
1614 		list->seg[nseg] = (struct scsi_ec_segment *)(ptr + off);
1615 		off += sizeof(struct scsi_ec_segment) +
1616 		    scsi_2btoul(list->seg[nseg]->descr_length);
1617 	}
1618 	list->inl = &data->data[lencscd + lenseg];
1619 	list->ncscd = lencscd / sizeof(struct scsi_ec_cscd);
1620 	list->nseg = nseg;
1621 	list->leninl = leninl;
1622 	list->ctsio = ctsio;
1623 	list->lun = lun;
1624 	mtx_lock(&lun->lun_lock);
1625 	if ((list->flags & EC_LIST_ID_USAGE_MASK) != EC_LIST_ID_USAGE_NONE) {
1626 		tlist = tpc_find_list(lun, list->list_id, list->init_idx);
1627 		if (tlist != NULL && !tlist->completed) {
1628 			mtx_unlock(&lun->lun_lock);
1629 			free(list, M_CTL);
1630 			ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
1631 			    /*command*/ 0, /*field*/ 0, /*bit_valid*/ 0,
1632 			    /*bit*/ 0);
1633 			goto done;
1634 		}
1635 		if (tlist != NULL) {
1636 			TAILQ_REMOVE(&lun->tpc_lists, tlist, links);
1637 			free(tlist, M_CTL);
1638 		}
1639 	}
1640 	TAILQ_INSERT_TAIL(&lun->tpc_lists, list, links);
1641 	mtx_unlock(&lun->lun_lock);
1642 
1643 	tpc_process(list);
1644 	return (CTL_RETVAL_COMPLETE);
1645 
1646 done:
1647 	if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
1648 		free(ctsio->kern_data_ptr, M_CTL);
1649 		ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
1650 	}
1651 	ctl_done((union ctl_io *)ctsio);
1652 	return (CTL_RETVAL_COMPLETE);
1653 }
1654 
1655 int
1656 ctl_extended_copy_lid4(struct ctl_scsiio *ctsio)
1657 {
1658 	struct scsi_extended_copy *cdb;
1659 	struct scsi_extended_copy_lid4_data *data;
1660 	struct ctl_lun *lun;
1661 	struct tpc_list *list, *tlist;
1662 	uint8_t *ptr;
1663 	char *value;
1664 	int len, off, lencscd, lenseg, leninl, nseg;
1665 
1666 	CTL_DEBUG_PRINT(("ctl_extended_copy_lid4\n"));
1667 
1668 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
1669 	cdb = (struct scsi_extended_copy *)ctsio->cdb;
1670 	len = scsi_4btoul(cdb->length);
1671 
1672 	if (len < sizeof(struct scsi_extended_copy_lid4_data) ||
1673 	    len > sizeof(struct scsi_extended_copy_lid4_data) +
1674 	    TPC_MAX_LIST + TPC_MAX_INLINE) {
1675 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
1676 		    /*field*/ 9, /*bit_valid*/ 0, /*bit*/ 0);
1677 		goto done;
1678 	}
1679 
1680 	/*
1681 	 * If we've got a kernel request that hasn't been malloced yet,
1682 	 * malloc it and tell the caller the data buffer is here.
1683 	 */
1684 	if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
1685 		ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
1686 		ctsio->kern_data_len = len;
1687 		ctsio->kern_total_len = len;
1688 		ctsio->kern_data_resid = 0;
1689 		ctsio->kern_rel_offset = 0;
1690 		ctsio->kern_sg_entries = 0;
1691 		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
1692 		ctsio->be_move_done = ctl_config_move_done;
1693 		ctl_datamove((union ctl_io *)ctsio);
1694 
1695 		return (CTL_RETVAL_COMPLETE);
1696 	}
1697 
1698 	data = (struct scsi_extended_copy_lid4_data *)ctsio->kern_data_ptr;
1699 	lencscd = scsi_2btoul(data->cscd_list_length);
1700 	lenseg = scsi_2btoul(data->segment_list_length);
1701 	leninl = scsi_2btoul(data->inline_data_length);
1702 	if (len < sizeof(struct scsi_extended_copy_lid4_data) +
1703 	    lencscd + lenseg + leninl ||
1704 	    leninl > TPC_MAX_INLINE) {
1705 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 0,
1706 		    /*field*/ 2, /*bit_valid*/ 0, /*bit*/ 0);
1707 		goto done;
1708 	}
1709 	if (lencscd > TPC_MAX_CSCDS * sizeof(struct scsi_ec_cscd)) {
1710 		ctl_set_sense(ctsio, /*current_error*/ 1,
1711 		    /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1712 		    /*asc*/ 0x26, /*ascq*/ 0x06, SSD_ELEM_NONE);
1713 		goto done;
1714 	}
1715 	if (lencscd + lenseg > TPC_MAX_LIST) {
1716 		ctl_set_param_len_error(ctsio);
1717 		goto done;
1718 	}
1719 
1720 	list = malloc(sizeof(struct tpc_list), M_CTL, M_WAITOK | M_ZERO);
1721 	list->service_action = cdb->service_action;
1722 	value = ctl_get_opt(&lun->be_lun->options, "insecure_tpc");
1723 	if (value != NULL && strcmp(value, "on") == 0)
1724 		list->init_port = -1;
1725 	else
1726 		list->init_port = ctsio->io_hdr.nexus.targ_port;
1727 	list->init_idx = ctl_get_resindex(&ctsio->io_hdr.nexus);
1728 	list->list_id = scsi_4btoul(data->list_identifier);
1729 	list->flags = data->flags;
1730 	list->params = ctsio->kern_data_ptr;
1731 	list->cscd = (struct scsi_ec_cscd *)&data->data[0];
1732 	ptr = &data->data[lencscd];
1733 	for (nseg = 0, off = 0; off < lenseg; nseg++) {
1734 		if (nseg >= TPC_MAX_SEGS) {
1735 			free(list, M_CTL);
1736 			ctl_set_sense(ctsio, /*current_error*/ 1,
1737 			    /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1738 			    /*asc*/ 0x26, /*ascq*/ 0x08, SSD_ELEM_NONE);
1739 			goto done;
1740 		}
1741 		list->seg[nseg] = (struct scsi_ec_segment *)(ptr + off);
1742 		off += sizeof(struct scsi_ec_segment) +
1743 		    scsi_2btoul(list->seg[nseg]->descr_length);
1744 	}
1745 	list->inl = &data->data[lencscd + lenseg];
1746 	list->ncscd = lencscd / sizeof(struct scsi_ec_cscd);
1747 	list->nseg = nseg;
1748 	list->leninl = leninl;
1749 	list->ctsio = ctsio;
1750 	list->lun = lun;
1751 	mtx_lock(&lun->lun_lock);
1752 	if ((list->flags & EC_LIST_ID_USAGE_MASK) != EC_LIST_ID_USAGE_NONE) {
1753 		tlist = tpc_find_list(lun, list->list_id, list->init_idx);
1754 		if (tlist != NULL && !tlist->completed) {
1755 			mtx_unlock(&lun->lun_lock);
1756 			free(list, M_CTL);
1757 			ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
1758 			    /*command*/ 0, /*field*/ 0, /*bit_valid*/ 0,
1759 			    /*bit*/ 0);
1760 			goto done;
1761 		}
1762 		if (tlist != NULL) {
1763 			TAILQ_REMOVE(&lun->tpc_lists, tlist, links);
1764 			free(tlist, M_CTL);
1765 		}
1766 	}
1767 	TAILQ_INSERT_TAIL(&lun->tpc_lists, list, links);
1768 	mtx_unlock(&lun->lun_lock);
1769 
1770 	tpc_process(list);
1771 	return (CTL_RETVAL_COMPLETE);
1772 
1773 done:
1774 	if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
1775 		free(ctsio->kern_data_ptr, M_CTL);
1776 		ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
1777 	}
1778 	ctl_done((union ctl_io *)ctsio);
1779 	return (CTL_RETVAL_COMPLETE);
1780 }
1781 
1782 static void
1783 tpc_create_token(struct ctl_lun *lun, struct ctl_port *port, off_t len,
1784     struct scsi_token *token)
1785 {
1786 	static int id = 0;
1787 	struct scsi_vpd_id_descriptor *idd = NULL;
1788 	struct scsi_ec_cscd_id *cscd;
1789 	struct scsi_read_capacity_data_long *dtsd;
1790 	int targid_len;
1791 
1792 	scsi_ulto4b(ROD_TYPE_AUR, token->type);
1793 	scsi_ulto2b(0x01f8, token->length);
1794 	scsi_u64to8b(atomic_fetchadd_int(&id, 1), &token->body[0]);
1795 	if (lun->lun_devid)
1796 		idd = scsi_get_devid_desc((struct scsi_vpd_id_descriptor *)
1797 		    lun->lun_devid->data, lun->lun_devid->len,
1798 		    scsi_devid_is_lun_naa);
1799 	if (idd == NULL && lun->lun_devid)
1800 		idd = scsi_get_devid_desc((struct scsi_vpd_id_descriptor *)
1801 		    lun->lun_devid->data, lun->lun_devid->len,
1802 		    scsi_devid_is_lun_eui64);
1803 	if (idd != NULL) {
1804 		cscd = (struct scsi_ec_cscd_id *)&token->body[8];
1805 		cscd->type_code = EC_CSCD_ID;
1806 		cscd->luidt_pdt = T_DIRECT;
1807 		memcpy(&cscd->codeset, idd, 4 + idd->length);
1808 		scsi_ulto3b(lun->be_lun->blocksize, cscd->dtsp.block_length);
1809 	}
1810 	scsi_u64to8b(0, &token->body[40]); /* XXX: Should be 128bit value. */
1811 	scsi_u64to8b(len, &token->body[48]);
1812 
1813 	/* ROD token device type specific data (RC16 without first field) */
1814 	dtsd = (struct scsi_read_capacity_data_long *)&token->body[88 - 8];
1815 	scsi_ulto4b(lun->be_lun->blocksize, dtsd->length);
1816 	dtsd->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
1817 	scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, dtsd->lalba_lbp);
1818 	if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
1819 		dtsd->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
1820 
1821 	if (port->target_devid) {
1822 		targid_len = port->target_devid->len;
1823 		memcpy(&token->body[120], port->target_devid->data, targid_len);
1824 	} else
1825 		targid_len = 32;
1826 	arc4rand(&token->body[120 + targid_len], 384 - targid_len, 0);
1827 };
1828 
1829 int
1830 ctl_populate_token(struct ctl_scsiio *ctsio)
1831 {
1832 	struct scsi_populate_token *cdb;
1833 	struct scsi_populate_token_data *data;
1834 	struct ctl_lun *lun;
1835 	struct ctl_port *port;
1836 	struct tpc_list *list, *tlist;
1837 	struct tpc_token *token;
1838 	int len, lendesc;
1839 
1840 	CTL_DEBUG_PRINT(("ctl_populate_token\n"));
1841 
1842 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
1843 	port = control_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
1844 	cdb = (struct scsi_populate_token *)ctsio->cdb;
1845 	len = scsi_4btoul(cdb->length);
1846 
1847 	if (len < sizeof(struct scsi_populate_token_data) ||
1848 	    len > sizeof(struct scsi_populate_token_data) +
1849 	     TPC_MAX_SEGS * sizeof(struct scsi_range_desc)) {
1850 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
1851 		    /*field*/ 9, /*bit_valid*/ 0, /*bit*/ 0);
1852 		goto done;
1853 	}
1854 
1855 	/*
1856 	 * If we've got a kernel request that hasn't been malloced yet,
1857 	 * malloc it and tell the caller the data buffer is here.
1858 	 */
1859 	if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
1860 		ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
1861 		ctsio->kern_data_len = len;
1862 		ctsio->kern_total_len = len;
1863 		ctsio->kern_data_resid = 0;
1864 		ctsio->kern_rel_offset = 0;
1865 		ctsio->kern_sg_entries = 0;
1866 		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
1867 		ctsio->be_move_done = ctl_config_move_done;
1868 		ctl_datamove((union ctl_io *)ctsio);
1869 
1870 		return (CTL_RETVAL_COMPLETE);
1871 	}
1872 
1873 	data = (struct scsi_populate_token_data *)ctsio->kern_data_ptr;
1874 	lendesc = scsi_2btoul(data->range_descriptor_length);
1875 	if (len < sizeof(struct scsi_populate_token_data) + lendesc) {
1876 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 0,
1877 		    /*field*/ 2, /*bit_valid*/ 0, /*bit*/ 0);
1878 		goto done;
1879 	}
1880 /*
1881 	printf("PT(list=%u) flags=%x to=%d rt=%x len=%x\n",
1882 	    scsi_4btoul(cdb->list_identifier),
1883 	    data->flags, scsi_4btoul(data->inactivity_timeout),
1884 	    scsi_4btoul(data->rod_type),
1885 	    scsi_2btoul(data->range_descriptor_length));
1886 */
1887 	if ((data->flags & EC_PT_RTV) &&
1888 	    scsi_4btoul(data->rod_type) != ROD_TYPE_AUR) {
1889 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 0,
1890 		    /*field*/ 8, /*bit_valid*/ 0, /*bit*/ 0);
1891 		goto done;
1892 	}
1893 
1894 	list = malloc(sizeof(struct tpc_list), M_CTL, M_WAITOK | M_ZERO);
1895 	list->service_action = cdb->service_action;
1896 	list->init_port = ctsio->io_hdr.nexus.targ_port;
1897 	list->init_idx = ctl_get_resindex(&ctsio->io_hdr.nexus);
1898 	list->list_id = scsi_4btoul(cdb->list_identifier);
1899 	list->flags = data->flags;
1900 	list->ctsio = ctsio;
1901 	list->lun = lun;
1902 	mtx_lock(&lun->lun_lock);
1903 	tlist = tpc_find_list(lun, list->list_id, list->init_idx);
1904 	if (tlist != NULL && !tlist->completed) {
1905 		mtx_unlock(&lun->lun_lock);
1906 		free(list, M_CTL);
1907 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
1908 		    /*command*/ 0, /*field*/ 0, /*bit_valid*/ 0,
1909 		    /*bit*/ 0);
1910 		goto done;
1911 	}
1912 	if (tlist != NULL) {
1913 		TAILQ_REMOVE(&lun->tpc_lists, tlist, links);
1914 		free(tlist, M_CTL);
1915 	}
1916 	TAILQ_INSERT_TAIL(&lun->tpc_lists, list, links);
1917 	mtx_unlock(&lun->lun_lock);
1918 
1919 	token = malloc(sizeof(*token), M_CTL, M_WAITOK | M_ZERO);
1920 	token->lun = lun->lun;
1921 	token->blocksize = lun->be_lun->blocksize;
1922 	token->params = ctsio->kern_data_ptr;
1923 	token->range = &data->desc[0];
1924 	token->nrange = scsi_2btoul(data->range_descriptor_length) /
1925 	    sizeof(struct scsi_range_desc);
1926 	list->cursectors = tpc_ranges_length(token->range, token->nrange);
1927 	list->curbytes = (off_t)list->cursectors * lun->be_lun->blocksize;
1928 	tpc_create_token(lun, port, list->curbytes,
1929 	    (struct scsi_token *)token->token);
1930 	token->active = 0;
1931 	token->last_active = time_uptime;
1932 	token->timeout = scsi_4btoul(data->inactivity_timeout);
1933 	if (token->timeout == 0)
1934 		token->timeout = TPC_DFL_TOKEN_TIMEOUT;
1935 	else if (token->timeout < TPC_MIN_TOKEN_TIMEOUT)
1936 		token->timeout = TPC_MIN_TOKEN_TIMEOUT;
1937 	else if (token->timeout > TPC_MAX_TOKEN_TIMEOUT) {
1938 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
1939 		    /*command*/ 0, /*field*/ 4, /*bit_valid*/ 0,
1940 		    /*bit*/ 0);
1941 	}
1942 	memcpy(list->res_token, token->token, sizeof(list->res_token));
1943 	list->res_token_valid = 1;
1944 	list->curseg = 0;
1945 	list->completed = 1;
1946 	list->last_active = time_uptime;
1947 	mtx_lock(&control_softc->ctl_lock);
1948 	TAILQ_INSERT_TAIL(&control_softc->tpc_tokens, token, links);
1949 	mtx_unlock(&control_softc->ctl_lock);
1950 	ctl_set_success(ctsio);
1951 	ctl_done((union ctl_io *)ctsio);
1952 	return (CTL_RETVAL_COMPLETE);
1953 
1954 done:
1955 	if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
1956 		free(ctsio->kern_data_ptr, M_CTL);
1957 		ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
1958 	}
1959 	ctl_done((union ctl_io *)ctsio);
1960 	return (CTL_RETVAL_COMPLETE);
1961 }
1962 
1963 int
1964 ctl_write_using_token(struct ctl_scsiio *ctsio)
1965 {
1966 	struct scsi_write_using_token *cdb;
1967 	struct scsi_write_using_token_data *data;
1968 	struct ctl_lun *lun;
1969 	struct tpc_list *list, *tlist;
1970 	struct tpc_token *token;
1971 	int len, lendesc;
1972 
1973 	CTL_DEBUG_PRINT(("ctl_write_using_token\n"));
1974 
1975 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
1976 	cdb = (struct scsi_write_using_token *)ctsio->cdb;
1977 	len = scsi_4btoul(cdb->length);
1978 
1979 	if (len < sizeof(struct scsi_populate_token_data) ||
1980 	    len > sizeof(struct scsi_populate_token_data) +
1981 	     TPC_MAX_SEGS * sizeof(struct scsi_range_desc)) {
1982 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
1983 		    /*field*/ 9, /*bit_valid*/ 0, /*bit*/ 0);
1984 		goto done;
1985 	}
1986 
1987 	/*
1988 	 * If we've got a kernel request that hasn't been malloced yet,
1989 	 * malloc it and tell the caller the data buffer is here.
1990 	 */
1991 	if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
1992 		ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
1993 		ctsio->kern_data_len = len;
1994 		ctsio->kern_total_len = len;
1995 		ctsio->kern_data_resid = 0;
1996 		ctsio->kern_rel_offset = 0;
1997 		ctsio->kern_sg_entries = 0;
1998 		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
1999 		ctsio->be_move_done = ctl_config_move_done;
2000 		ctl_datamove((union ctl_io *)ctsio);
2001 
2002 		return (CTL_RETVAL_COMPLETE);
2003 	}
2004 
2005 	data = (struct scsi_write_using_token_data *)ctsio->kern_data_ptr;
2006 	lendesc = scsi_2btoul(data->range_descriptor_length);
2007 	if (len < sizeof(struct scsi_populate_token_data) + lendesc) {
2008 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 0,
2009 		    /*field*/ 2, /*bit_valid*/ 0, /*bit*/ 0);
2010 		goto done;
2011 	}
2012 /*
2013 	printf("WUT(list=%u) flags=%x off=%ju len=%x\n",
2014 	    scsi_4btoul(cdb->list_identifier),
2015 	    data->flags, scsi_8btou64(data->offset_into_rod),
2016 	    scsi_2btoul(data->range_descriptor_length));
2017 */
2018 	list = malloc(sizeof(struct tpc_list), M_CTL, M_WAITOK | M_ZERO);
2019 	list->service_action = cdb->service_action;
2020 	list->init_port = ctsio->io_hdr.nexus.targ_port;
2021 	list->init_idx = ctl_get_resindex(&ctsio->io_hdr.nexus);
2022 	list->list_id = scsi_4btoul(cdb->list_identifier);
2023 	list->flags = data->flags;
2024 	list->params = ctsio->kern_data_ptr;
2025 	list->range = &data->desc[0];
2026 	list->nrange = scsi_2btoul(data->range_descriptor_length) /
2027 	    sizeof(struct scsi_range_desc);
2028 	list->offset_into_rod = scsi_8btou64(data->offset_into_rod);
2029 	list->ctsio = ctsio;
2030 	list->lun = lun;
2031 	mtx_lock(&lun->lun_lock);
2032 	tlist = tpc_find_list(lun, list->list_id, list->init_idx);
2033 	if (tlist != NULL && !tlist->completed) {
2034 		mtx_unlock(&lun->lun_lock);
2035 		free(list, M_CTL);
2036 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
2037 		    /*command*/ 0, /*field*/ 0, /*bit_valid*/ 0,
2038 		    /*bit*/ 0);
2039 		goto done;
2040 	}
2041 	if (tlist != NULL) {
2042 		TAILQ_REMOVE(&lun->tpc_lists, tlist, links);
2043 		free(tlist, M_CTL);
2044 	}
2045 	TAILQ_INSERT_TAIL(&lun->tpc_lists, list, links);
2046 	mtx_unlock(&lun->lun_lock);
2047 
2048 	/* Block device zero ROD token -> no token. */
2049 	if (scsi_4btoul(data->rod_token) == ROD_TYPE_BLOCK_ZERO) {
2050 		tpc_process(list);
2051 		return (CTL_RETVAL_COMPLETE);
2052 	}
2053 
2054 	mtx_lock(&control_softc->ctl_lock);
2055 	TAILQ_FOREACH(token, &control_softc->tpc_tokens, links) {
2056 		if (memcmp(token->token, data->rod_token,
2057 		    sizeof(data->rod_token)) == 0)
2058 			break;
2059 	}
2060 	if (token != NULL) {
2061 		token->active++;
2062 		list->token = token;
2063 		if (data->flags & EC_WUT_DEL_TKN)
2064 			token->timeout = 0;
2065 	}
2066 	mtx_unlock(&control_softc->ctl_lock);
2067 	if (token == NULL) {
2068 		mtx_lock(&lun->lun_lock);
2069 		TAILQ_REMOVE(&lun->tpc_lists, list, links);
2070 		mtx_unlock(&lun->lun_lock);
2071 		free(list, M_CTL);
2072 		ctl_set_sense(ctsio, /*current_error*/ 1,
2073 		    /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
2074 		    /*asc*/ 0x23, /*ascq*/ 0x04, SSD_ELEM_NONE);
2075 		goto done;
2076 	}
2077 
2078 	tpc_process(list);
2079 	return (CTL_RETVAL_COMPLETE);
2080 
2081 done:
2082 	if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
2083 		free(ctsio->kern_data_ptr, M_CTL);
2084 		ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
2085 	}
2086 	ctl_done((union ctl_io *)ctsio);
2087 	return (CTL_RETVAL_COMPLETE);
2088 }
2089 
2090 int
2091 ctl_receive_rod_token_information(struct ctl_scsiio *ctsio)
2092 {
2093 	struct ctl_lun *lun;
2094 	struct scsi_receive_rod_token_information *cdb;
2095 	struct scsi_receive_copy_status_lid4_data *data;
2096 	struct tpc_list *list;
2097 	struct tpc_list list_copy;
2098 	uint8_t *ptr;
2099 	int retval;
2100 	int alloc_len, total_len, token_len;
2101 	uint32_t list_id;
2102 
2103 	CTL_DEBUG_PRINT(("ctl_receive_rod_token_information\n"));
2104 
2105 	cdb = (struct scsi_receive_rod_token_information *)ctsio->cdb;
2106 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
2107 
2108 	retval = CTL_RETVAL_COMPLETE;
2109 
2110 	list_id = scsi_4btoul(cdb->list_identifier);
2111 	mtx_lock(&lun->lun_lock);
2112 	list = tpc_find_list(lun, list_id,
2113 	    ctl_get_resindex(&ctsio->io_hdr.nexus));
2114 	if (list == NULL) {
2115 		mtx_unlock(&lun->lun_lock);
2116 		ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
2117 		    /*command*/ 1, /*field*/ 2, /*bit_valid*/ 0,
2118 		    /*bit*/ 0);
2119 		ctl_done((union ctl_io *)ctsio);
2120 		return (retval);
2121 	}
2122 	list_copy = *list;
2123 	if (list->completed) {
2124 		TAILQ_REMOVE(&lun->tpc_lists, list, links);
2125 		free(list, M_CTL);
2126 	}
2127 	mtx_unlock(&lun->lun_lock);
2128 
2129 	token_len = list_copy.res_token_valid ? 2 + sizeof(list_copy.res_token) : 0;
2130 	total_len = sizeof(*data) + list_copy.sense_len + 4 + token_len;
2131 	alloc_len = scsi_4btoul(cdb->length);
2132 
2133 	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
2134 
2135 	ctsio->kern_sg_entries = 0;
2136 
2137 	if (total_len < alloc_len) {
2138 		ctsio->residual = alloc_len - total_len;
2139 		ctsio->kern_data_len = total_len;
2140 		ctsio->kern_total_len = total_len;
2141 	} else {
2142 		ctsio->residual = 0;
2143 		ctsio->kern_data_len = alloc_len;
2144 		ctsio->kern_total_len = alloc_len;
2145 	}
2146 	ctsio->kern_data_resid = 0;
2147 	ctsio->kern_rel_offset = 0;
2148 
2149 	data = (struct scsi_receive_copy_status_lid4_data *)ctsio->kern_data_ptr;
2150 	scsi_ulto4b(sizeof(*data) - 4 + list_copy.sense_len +
2151 	    4 + token_len, data->available_data);
2152 	data->response_to_service_action = list_copy.service_action;
2153 	if (list_copy.completed) {
2154 		if (list_copy.error)
2155 			data->copy_command_status = RCS_CCS_ERROR;
2156 		else if (list_copy.abort)
2157 			data->copy_command_status = RCS_CCS_ABORTED;
2158 		else
2159 			data->copy_command_status = RCS_CCS_COMPLETED;
2160 	} else
2161 		data->copy_command_status = RCS_CCS_INPROG_FG;
2162 	scsi_ulto2b(list_copy.curops, data->operation_counter);
2163 	scsi_ulto4b(UINT32_MAX, data->estimated_status_update_delay);
2164 	data->transfer_count_units = RCS_TC_LBAS;
2165 	scsi_u64to8b(list_copy.cursectors, data->transfer_count);
2166 	scsi_ulto2b(list_copy.curseg, data->segments_processed);
2167 	data->length_of_the_sense_data_field = list_copy.sense_len;
2168 	data->sense_data_length = list_copy.sense_len;
2169 	memcpy(data->sense_data, &list_copy.sense_data, list_copy.sense_len);
2170 
2171 	ptr = &data->sense_data[data->length_of_the_sense_data_field];
2172 	scsi_ulto4b(token_len, &ptr[0]);
2173 	if (list_copy.res_token_valid) {
2174 		scsi_ulto2b(0, &ptr[4]);
2175 		memcpy(&ptr[6], list_copy.res_token, sizeof(list_copy.res_token));
2176 	}
2177 /*
2178 	printf("RRTI(list=%u) valid=%d\n",
2179 	    scsi_4btoul(cdb->list_identifier), list_copy.res_token_valid);
2180 */
2181 	ctl_set_success(ctsio);
2182 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
2183 	ctsio->be_move_done = ctl_config_move_done;
2184 	ctl_datamove((union ctl_io *)ctsio);
2185 	return (retval);
2186 }
2187 
2188 int
2189 ctl_report_all_rod_tokens(struct ctl_scsiio *ctsio)
2190 {
2191 	struct ctl_lun *lun;
2192 	struct scsi_report_all_rod_tokens *cdb;
2193 	struct scsi_report_all_rod_tokens_data *data;
2194 	struct tpc_token *token;
2195 	int retval;
2196 	int alloc_len, total_len, tokens, i;
2197 
2198 	CTL_DEBUG_PRINT(("ctl_receive_rod_token_information\n"));
2199 
2200 	cdb = (struct scsi_report_all_rod_tokens *)ctsio->cdb;
2201 	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
2202 
2203 	retval = CTL_RETVAL_COMPLETE;
2204 
2205 	tokens = 0;
2206 	mtx_lock(&control_softc->ctl_lock);
2207 	TAILQ_FOREACH(token, &control_softc->tpc_tokens, links)
2208 		tokens++;
2209 	mtx_unlock(&control_softc->ctl_lock);
2210 	if (tokens > 512)
2211 		tokens = 512;
2212 
2213 	total_len = sizeof(*data) + tokens * 96;
2214 	alloc_len = scsi_4btoul(cdb->length);
2215 
2216 	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
2217 
2218 	ctsio->kern_sg_entries = 0;
2219 
2220 	if (total_len < alloc_len) {
2221 		ctsio->residual = alloc_len - total_len;
2222 		ctsio->kern_data_len = total_len;
2223 		ctsio->kern_total_len = total_len;
2224 	} else {
2225 		ctsio->residual = 0;
2226 		ctsio->kern_data_len = alloc_len;
2227 		ctsio->kern_total_len = alloc_len;
2228 	}
2229 	ctsio->kern_data_resid = 0;
2230 	ctsio->kern_rel_offset = 0;
2231 
2232 	data = (struct scsi_report_all_rod_tokens_data *)ctsio->kern_data_ptr;
2233 	i = 0;
2234 	mtx_lock(&control_softc->ctl_lock);
2235 	TAILQ_FOREACH(token, &control_softc->tpc_tokens, links) {
2236 		if (i >= tokens)
2237 			break;
2238 		memcpy(&data->rod_management_token_list[i * 96],
2239 		    token->token, 96);
2240 		i++;
2241 	}
2242 	mtx_unlock(&control_softc->ctl_lock);
2243 	scsi_ulto4b(sizeof(*data) - 4 + i * 96, data->available_data);
2244 /*
2245 	printf("RART tokens=%d\n", i);
2246 */
2247 	ctl_set_success(ctsio);
2248 	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
2249 	ctsio->be_move_done = ctl_config_move_done;
2250 	ctl_datamove((union ctl_io *)ctsio);
2251 	return (retval);
2252 }
2253 
2254