2 * Copyright (c) 2014 Alexander Motin <mav@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/types.h>
35 #include <sys/module.h>
36 #include <sys/mutex.h>
37 #include <sys/condvar.h>
38 #include <sys/malloc.h>
40 #include <sys/queue.h>
41 #include <sys/sysctl.h>
42 #include <machine/atomic.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>
61 #define TPC_MAX_CSCDS 64
62 #define TPC_MAX_SEGS 64
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
73 MALLOC_DEFINE(M_CTL_TPC, "ctltpc", "CTL TPC");
76 TPC_ERR_RETRY = 0x000,
79 TPC_ERR_NO_DECREMENT = 0x100
83 TAILQ_HEAD(runl, tpc_io);
87 struct tpc_list *list;
89 TAILQ_ENTRY(tpc_io) rlinks;
90 TAILQ_ENTRY(tpc_io) links;
98 struct scsi_range_desc *range;
103 TAILQ_ENTRY(tpc_token) links;
107 uint8_t service_action;
113 struct scsi_ec_cscd *cscd;
114 struct scsi_ec_segment *seg[TPC_MAX_SEGS];
119 struct tpc_token *token;
120 struct scsi_range_desc *range;
122 off_t offset_into_rod;
137 TAILQ_HEAD(, tpc_io) allio;
138 struct scsi_sense_data sense_data;
141 struct ctl_scsiio *ctsio;
144 uint8_t res_token[512];
145 TAILQ_ENTRY(tpc_list) links;
149 tpc_timeout(void *arg)
151 struct ctl_softc *softc = arg;
153 struct tpc_token *token, *ttoken;
154 struct tpc_list *list, *tlist;
156 /* Free completed lists with expired timeout. */
157 STAILQ_FOREACH(lun, &softc->lun_list, links) {
158 mtx_lock(&lun->lun_lock);
159 TAILQ_FOREACH_SAFE(list, &lun->tpc_lists, links, tlist) {
160 if (!list->completed || time_uptime < list->last_active +
161 TPC_DFL_TOKEN_TIMEOUT)
163 TAILQ_REMOVE(&lun->tpc_lists, list, links);
166 mtx_unlock(&lun->lun_lock);
169 /* Free inactive ROD tokens with expired timeout. */
170 mtx_lock(&softc->tpc_lock);
171 TAILQ_FOREACH_SAFE(token, &softc->tpc_tokens, links, ttoken) {
173 time_uptime < token->last_active + token->timeout + 1)
175 TAILQ_REMOVE(&softc->tpc_tokens, token, links);
176 free(token->params, M_CTL);
179 mtx_unlock(&softc->tpc_lock);
180 callout_schedule(&softc->tpc_timeout, hz);
184 ctl_tpc_init(struct ctl_softc *softc)
187 mtx_init(&softc->tpc_lock, "CTL TPC mutex", NULL, MTX_DEF);
188 TAILQ_INIT(&softc->tpc_tokens);
189 callout_init_mtx(&softc->tpc_timeout, &softc->ctl_lock, 0);
190 callout_reset(&softc->tpc_timeout, hz, tpc_timeout, softc);
194 ctl_tpc_shutdown(struct ctl_softc *softc)
196 struct tpc_token *token;
198 callout_drain(&softc->tpc_timeout);
200 /* Free ROD tokens. */
201 mtx_lock(&softc->tpc_lock);
202 while ((token = TAILQ_FIRST(&softc->tpc_tokens)) != NULL) {
203 TAILQ_REMOVE(&softc->tpc_tokens, token, links);
204 free(token->params, M_CTL);
207 mtx_unlock(&softc->tpc_lock);
208 mtx_destroy(&softc->tpc_lock);
212 ctl_tpc_lun_init(struct ctl_lun *lun)
215 TAILQ_INIT(&lun->tpc_lists);
219 ctl_tpc_lun_shutdown(struct ctl_lun *lun)
221 struct ctl_softc *softc = lun->ctl_softc;
222 struct tpc_list *list;
223 struct tpc_token *token, *ttoken;
225 /* Free lists for this LUN. */
226 while ((list = TAILQ_FIRST(&lun->tpc_lists)) != NULL) {
227 TAILQ_REMOVE(&lun->tpc_lists, list, links);
228 KASSERT(list->completed,
229 ("Not completed TPC (%p) on shutdown", list));
233 /* Free ROD tokens for this LUN. */
234 mtx_lock(&softc->tpc_lock);
235 TAILQ_FOREACH_SAFE(token, &softc->tpc_tokens, links, ttoken) {
236 if (token->lun != lun->lun || token->active)
238 TAILQ_REMOVE(&softc->tpc_tokens, token, links);
239 free(token->params, M_CTL);
242 mtx_unlock(&softc->tpc_lock);
246 ctl_inquiry_evpd_tpc(struct ctl_scsiio *ctsio, int alloc_len)
248 struct scsi_vpd_tpc *tpc_ptr;
249 struct scsi_vpd_tpc_descriptor *d_ptr;
250 struct scsi_vpd_tpc_descriptor_bdrl *bdrl_ptr;
251 struct scsi_vpd_tpc_descriptor_sc *sc_ptr;
252 struct scsi_vpd_tpc_descriptor_sc_descr *scd_ptr;
253 struct scsi_vpd_tpc_descriptor_pd *pd_ptr;
254 struct scsi_vpd_tpc_descriptor_sd *sd_ptr;
255 struct scsi_vpd_tpc_descriptor_sdid *sdid_ptr;
256 struct scsi_vpd_tpc_descriptor_rtf *rtf_ptr;
257 struct scsi_vpd_tpc_descriptor_rtf_block *rtfb_ptr;
258 struct scsi_vpd_tpc_descriptor_srt *srt_ptr;
259 struct scsi_vpd_tpc_descriptor_srtd *srtd_ptr;
260 struct scsi_vpd_tpc_descriptor_gco *gco_ptr;
264 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
266 data_len = sizeof(struct scsi_vpd_tpc) +
267 sizeof(struct scsi_vpd_tpc_descriptor_bdrl) +
268 roundup2(sizeof(struct scsi_vpd_tpc_descriptor_sc) +
269 2 * sizeof(struct scsi_vpd_tpc_descriptor_sc_descr) + 11, 4) +
270 sizeof(struct scsi_vpd_tpc_descriptor_pd) +
271 roundup2(sizeof(struct scsi_vpd_tpc_descriptor_sd) + 4, 4) +
272 roundup2(sizeof(struct scsi_vpd_tpc_descriptor_sdid) + 2, 4) +
273 sizeof(struct scsi_vpd_tpc_descriptor_rtf) +
274 sizeof(struct scsi_vpd_tpc_descriptor_rtf_block) +
275 sizeof(struct scsi_vpd_tpc_descriptor_srt) +
276 2*sizeof(struct scsi_vpd_tpc_descriptor_srtd) +
277 sizeof(struct scsi_vpd_tpc_descriptor_gco);
279 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
280 tpc_ptr = (struct scsi_vpd_tpc *)ctsio->kern_data_ptr;
281 ctsio->kern_sg_entries = 0;
283 if (data_len < alloc_len) {
284 ctsio->residual = alloc_len - data_len;
285 ctsio->kern_data_len = data_len;
286 ctsio->kern_total_len = data_len;
289 ctsio->kern_data_len = alloc_len;
290 ctsio->kern_total_len = alloc_len;
292 ctsio->kern_data_resid = 0;
293 ctsio->kern_rel_offset = 0;
294 ctsio->kern_sg_entries = 0;
297 * The control device is always connected. The disk device, on the
298 * other hand, may not be online all the time.
301 tpc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
302 lun->be_lun->lun_type;
304 tpc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
305 tpc_ptr->page_code = SVPD_SCSI_TPC;
306 scsi_ulto2b(data_len - 4, tpc_ptr->page_length);
308 /* Block Device ROD Limits */
309 d_ptr = (struct scsi_vpd_tpc_descriptor *)&tpc_ptr->descr[0];
310 bdrl_ptr = (struct scsi_vpd_tpc_descriptor_bdrl *)d_ptr;
311 scsi_ulto2b(SVPD_TPC_BDRL, bdrl_ptr->desc_type);
312 scsi_ulto2b(sizeof(*bdrl_ptr) - 4, bdrl_ptr->desc_length);
313 scsi_ulto2b(TPC_MAX_SEGS, bdrl_ptr->maximum_ranges);
314 scsi_ulto4b(TPC_MAX_TOKEN_TIMEOUT,
315 bdrl_ptr->maximum_inactivity_timeout);
316 scsi_ulto4b(TPC_DFL_TOKEN_TIMEOUT,
317 bdrl_ptr->default_inactivity_timeout);
318 scsi_u64to8b(0, bdrl_ptr->maximum_token_transfer_size);
319 scsi_u64to8b(0, bdrl_ptr->optimal_transfer_count);
321 /* Supported commands */
322 d_ptr = (struct scsi_vpd_tpc_descriptor *)
323 (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
324 sc_ptr = (struct scsi_vpd_tpc_descriptor_sc *)d_ptr;
325 scsi_ulto2b(SVPD_TPC_SC, sc_ptr->desc_type);
326 sc_ptr->list_length = 2 * sizeof(*scd_ptr) + 11;
327 scsi_ulto2b(roundup2(1 + sc_ptr->list_length, 4), sc_ptr->desc_length);
328 scd_ptr = &sc_ptr->descr[0];
329 scd_ptr->opcode = EXTENDED_COPY;
330 scd_ptr->sa_length = 5;
331 scd_ptr->supported_service_actions[0] = EC_EC_LID1;
332 scd_ptr->supported_service_actions[1] = EC_EC_LID4;
333 scd_ptr->supported_service_actions[2] = EC_PT;
334 scd_ptr->supported_service_actions[3] = EC_WUT;
335 scd_ptr->supported_service_actions[4] = EC_COA;
336 scd_ptr = (struct scsi_vpd_tpc_descriptor_sc_descr *)
337 &scd_ptr->supported_service_actions[scd_ptr->sa_length];
338 scd_ptr->opcode = RECEIVE_COPY_STATUS;
339 scd_ptr->sa_length = 6;
340 scd_ptr->supported_service_actions[0] = RCS_RCS_LID1;
341 scd_ptr->supported_service_actions[1] = RCS_RCFD;
342 scd_ptr->supported_service_actions[2] = RCS_RCS_LID4;
343 scd_ptr->supported_service_actions[3] = RCS_RCOP;
344 scd_ptr->supported_service_actions[4] = RCS_RRTI;
345 scd_ptr->supported_service_actions[5] = RCS_RART;
347 /* Parameter data. */
348 d_ptr = (struct scsi_vpd_tpc_descriptor *)
349 (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
350 pd_ptr = (struct scsi_vpd_tpc_descriptor_pd *)d_ptr;
351 scsi_ulto2b(SVPD_TPC_PD, pd_ptr->desc_type);
352 scsi_ulto2b(sizeof(*pd_ptr) - 4, pd_ptr->desc_length);
353 scsi_ulto2b(TPC_MAX_CSCDS, pd_ptr->maximum_cscd_descriptor_count);
354 scsi_ulto2b(TPC_MAX_SEGS, pd_ptr->maximum_segment_descriptor_count);
355 scsi_ulto4b(TPC_MAX_LIST, pd_ptr->maximum_descriptor_list_length);
356 scsi_ulto4b(TPC_MAX_INLINE, pd_ptr->maximum_inline_data_length);
358 /* Supported Descriptors */
359 d_ptr = (struct scsi_vpd_tpc_descriptor *)
360 (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
361 sd_ptr = (struct scsi_vpd_tpc_descriptor_sd *)d_ptr;
362 scsi_ulto2b(SVPD_TPC_SD, sd_ptr->desc_type);
363 scsi_ulto2b(roundup2(sizeof(*sd_ptr) - 4 + 4, 4), sd_ptr->desc_length);
364 sd_ptr->list_length = 4;
365 sd_ptr->supported_descriptor_codes[0] = EC_SEG_B2B;
366 sd_ptr->supported_descriptor_codes[1] = EC_SEG_VERIFY;
367 sd_ptr->supported_descriptor_codes[2] = EC_SEG_REGISTER_KEY;
368 sd_ptr->supported_descriptor_codes[3] = EC_CSCD_ID;
370 /* Supported CSCD Descriptor IDs */
371 d_ptr = (struct scsi_vpd_tpc_descriptor *)
372 (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
373 sdid_ptr = (struct scsi_vpd_tpc_descriptor_sdid *)d_ptr;
374 scsi_ulto2b(SVPD_TPC_SDID, sdid_ptr->desc_type);
375 scsi_ulto2b(roundup2(sizeof(*sdid_ptr) - 4 + 2, 4), sdid_ptr->desc_length);
376 scsi_ulto2b(2, sdid_ptr->list_length);
377 scsi_ulto2b(0xffff, &sdid_ptr->supported_descriptor_ids[0]);
379 /* ROD Token Features */
380 d_ptr = (struct scsi_vpd_tpc_descriptor *)
381 (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
382 rtf_ptr = (struct scsi_vpd_tpc_descriptor_rtf *)d_ptr;
383 scsi_ulto2b(SVPD_TPC_RTF, rtf_ptr->desc_type);
384 scsi_ulto2b(sizeof(*rtf_ptr) - 4 + sizeof(*rtfb_ptr), rtf_ptr->desc_length);
385 rtf_ptr->remote_tokens = 0;
386 scsi_ulto4b(TPC_MIN_TOKEN_TIMEOUT, rtf_ptr->minimum_token_lifetime);
387 scsi_ulto4b(UINT32_MAX, rtf_ptr->maximum_token_lifetime);
388 scsi_ulto4b(TPC_MAX_TOKEN_TIMEOUT,
389 rtf_ptr->maximum_token_inactivity_timeout);
390 scsi_ulto2b(sizeof(*rtfb_ptr), rtf_ptr->type_specific_features_length);
391 rtfb_ptr = (struct scsi_vpd_tpc_descriptor_rtf_block *)
392 &rtf_ptr->type_specific_features;
393 rtfb_ptr->type_format = SVPD_TPC_RTF_BLOCK;
394 scsi_ulto2b(sizeof(*rtfb_ptr) - 4, rtfb_ptr->desc_length);
395 scsi_ulto2b(0, rtfb_ptr->optimal_length_granularity);
396 scsi_u64to8b(0, rtfb_ptr->maximum_bytes);
397 scsi_u64to8b(0, rtfb_ptr->optimal_bytes);
398 scsi_u64to8b(TPC_MAX_IOCHUNK_SIZE,
399 rtfb_ptr->optimal_bytes_to_token_per_segment);
400 scsi_u64to8b(TPC_MAX_IOCHUNK_SIZE,
401 rtfb_ptr->optimal_bytes_from_token_per_segment);
403 /* Supported ROD Tokens */
404 d_ptr = (struct scsi_vpd_tpc_descriptor *)
405 (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
406 srt_ptr = (struct scsi_vpd_tpc_descriptor_srt *)d_ptr;
407 scsi_ulto2b(SVPD_TPC_SRT, srt_ptr->desc_type);
408 scsi_ulto2b(sizeof(*srt_ptr) - 4 + 2*sizeof(*srtd_ptr), srt_ptr->desc_length);
409 scsi_ulto2b(2*sizeof(*srtd_ptr), srt_ptr->rod_type_descriptors_length);
410 srtd_ptr = (struct scsi_vpd_tpc_descriptor_srtd *)
411 &srt_ptr->rod_type_descriptors;
412 scsi_ulto4b(ROD_TYPE_AUR, srtd_ptr->rod_type);
413 srtd_ptr->flags = SVPD_TPC_SRTD_TIN | SVPD_TPC_SRTD_TOUT;
414 scsi_ulto2b(0, srtd_ptr->preference_indicator);
416 scsi_ulto4b(ROD_TYPE_BLOCK_ZERO, srtd_ptr->rod_type);
417 srtd_ptr->flags = SVPD_TPC_SRTD_TIN;
418 scsi_ulto2b(0, srtd_ptr->preference_indicator);
420 /* General Copy Operations */
421 d_ptr = (struct scsi_vpd_tpc_descriptor *)
422 (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
423 gco_ptr = (struct scsi_vpd_tpc_descriptor_gco *)d_ptr;
424 scsi_ulto2b(SVPD_TPC_GCO, gco_ptr->desc_type);
425 scsi_ulto2b(sizeof(*gco_ptr) - 4, gco_ptr->desc_length);
426 scsi_ulto4b(TPC_MAX_LISTS, gco_ptr->total_concurrent_copies);
427 scsi_ulto4b(TPC_MAX_LISTS, gco_ptr->maximum_identified_concurrent_copies);
428 scsi_ulto4b(TPC_MAX_SEG, gco_ptr->maximum_segment_length);
429 gco_ptr->data_segment_granularity = 0;
430 gco_ptr->inline_data_granularity = 0;
432 ctl_set_success(ctsio);
433 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
434 ctsio->be_move_done = ctl_config_move_done;
435 ctl_datamove((union ctl_io *)ctsio);
437 return (CTL_RETVAL_COMPLETE);
441 ctl_receive_copy_operating_parameters(struct ctl_scsiio *ctsio)
443 struct scsi_receive_copy_operating_parameters *cdb;
444 struct scsi_receive_copy_operating_parameters_data *data;
446 int alloc_len, total_len;
448 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
450 cdb = (struct scsi_receive_copy_operating_parameters *)ctsio->cdb;
452 retval = CTL_RETVAL_COMPLETE;
454 total_len = sizeof(*data) + 4;
455 alloc_len = scsi_4btoul(cdb->length);
457 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
459 ctsio->kern_sg_entries = 0;
461 if (total_len < alloc_len) {
462 ctsio->residual = alloc_len - total_len;
463 ctsio->kern_data_len = total_len;
464 ctsio->kern_total_len = total_len;
467 ctsio->kern_data_len = alloc_len;
468 ctsio->kern_total_len = alloc_len;
470 ctsio->kern_data_resid = 0;
471 ctsio->kern_rel_offset = 0;
473 data = (struct scsi_receive_copy_operating_parameters_data *)ctsio->kern_data_ptr;
474 scsi_ulto4b(sizeof(*data) - 4 + 4, data->length);
475 data->snlid = RCOP_SNLID;
476 scsi_ulto2b(TPC_MAX_CSCDS, data->maximum_cscd_descriptor_count);
477 scsi_ulto2b(TPC_MAX_SEGS, data->maximum_segment_descriptor_count);
478 scsi_ulto4b(TPC_MAX_LIST, data->maximum_descriptor_list_length);
479 scsi_ulto4b(TPC_MAX_SEG, data->maximum_segment_length);
480 scsi_ulto4b(TPC_MAX_INLINE, data->maximum_inline_data_length);
481 scsi_ulto4b(0, data->held_data_limit);
482 scsi_ulto4b(0, data->maximum_stream_device_transfer_size);
483 scsi_ulto2b(TPC_MAX_LISTS, data->total_concurrent_copies);
484 data->maximum_concurrent_copies = TPC_MAX_LISTS;
485 data->data_segment_granularity = 0;
486 data->inline_data_granularity = 0;
487 data->held_data_granularity = 0;
488 data->implemented_descriptor_list_length = 4;
489 data->list_of_implemented_descriptor_type_codes[0] = EC_SEG_B2B;
490 data->list_of_implemented_descriptor_type_codes[1] = EC_SEG_VERIFY;
491 data->list_of_implemented_descriptor_type_codes[2] = EC_SEG_REGISTER_KEY;
492 data->list_of_implemented_descriptor_type_codes[3] = EC_CSCD_ID;
494 ctl_set_success(ctsio);
495 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
496 ctsio->be_move_done = ctl_config_move_done;
497 ctl_datamove((union ctl_io *)ctsio);
501 static struct tpc_list *
502 tpc_find_list(struct ctl_lun *lun, uint32_t list_id, uint32_t init_idx)
504 struct tpc_list *list;
506 mtx_assert(&lun->lun_lock, MA_OWNED);
507 TAILQ_FOREACH(list, &lun->tpc_lists, links) {
508 if ((list->flags & EC_LIST_ID_USAGE_MASK) !=
509 EC_LIST_ID_USAGE_NONE && list->list_id == list_id &&
510 list->init_idx == init_idx)
517 ctl_receive_copy_status_lid1(struct ctl_scsiio *ctsio)
520 struct scsi_receive_copy_status_lid1 *cdb;
521 struct scsi_receive_copy_status_lid1_data *data;
522 struct tpc_list *list;
523 struct tpc_list list_copy;
525 int alloc_len, total_len;
528 CTL_DEBUG_PRINT(("ctl_receive_copy_status_lid1\n"));
530 cdb = (struct scsi_receive_copy_status_lid1 *)ctsio->cdb;
531 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
533 retval = CTL_RETVAL_COMPLETE;
535 list_id = cdb->list_identifier;
536 mtx_lock(&lun->lun_lock);
537 list = tpc_find_list(lun, list_id,
538 ctl_get_resindex(&ctsio->io_hdr.nexus));
540 mtx_unlock(&lun->lun_lock);
541 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
542 /*command*/ 1, /*field*/ 2, /*bit_valid*/ 0,
544 ctl_done((union ctl_io *)ctsio);
548 if (list->completed) {
549 TAILQ_REMOVE(&lun->tpc_lists, list, links);
552 mtx_unlock(&lun->lun_lock);
554 total_len = sizeof(*data);
555 alloc_len = scsi_4btoul(cdb->length);
557 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
559 ctsio->kern_sg_entries = 0;
561 if (total_len < alloc_len) {
562 ctsio->residual = alloc_len - total_len;
563 ctsio->kern_data_len = total_len;
564 ctsio->kern_total_len = total_len;
567 ctsio->kern_data_len = alloc_len;
568 ctsio->kern_total_len = alloc_len;
570 ctsio->kern_data_resid = 0;
571 ctsio->kern_rel_offset = 0;
573 data = (struct scsi_receive_copy_status_lid1_data *)ctsio->kern_data_ptr;
574 scsi_ulto4b(sizeof(*data) - 4, data->available_data);
575 if (list_copy.completed) {
576 if (list_copy.error || list_copy.abort)
577 data->copy_command_status = RCS_CCS_ERROR;
579 data->copy_command_status = RCS_CCS_COMPLETED;
581 data->copy_command_status = RCS_CCS_INPROG;
582 scsi_ulto2b(list_copy.curseg, data->segments_processed);
583 if (list_copy.curbytes <= UINT32_MAX) {
584 data->transfer_count_units = RCS_TC_BYTES;
585 scsi_ulto4b(list_copy.curbytes, data->transfer_count);
587 data->transfer_count_units = RCS_TC_MBYTES;
588 scsi_ulto4b(list_copy.curbytes >> 20, data->transfer_count);
591 ctl_set_success(ctsio);
592 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
593 ctsio->be_move_done = ctl_config_move_done;
594 ctl_datamove((union ctl_io *)ctsio);
599 ctl_receive_copy_failure_details(struct ctl_scsiio *ctsio)
602 struct scsi_receive_copy_failure_details *cdb;
603 struct scsi_receive_copy_failure_details_data *data;
604 struct tpc_list *list;
605 struct tpc_list list_copy;
607 int alloc_len, total_len;
610 CTL_DEBUG_PRINT(("ctl_receive_copy_failure_details\n"));
612 cdb = (struct scsi_receive_copy_failure_details *)ctsio->cdb;
613 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
615 retval = CTL_RETVAL_COMPLETE;
617 list_id = cdb->list_identifier;
618 mtx_lock(&lun->lun_lock);
619 list = tpc_find_list(lun, list_id,
620 ctl_get_resindex(&ctsio->io_hdr.nexus));
621 if (list == NULL || !list->completed) {
622 mtx_unlock(&lun->lun_lock);
623 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
624 /*command*/ 1, /*field*/ 2, /*bit_valid*/ 0,
626 ctl_done((union ctl_io *)ctsio);
630 TAILQ_REMOVE(&lun->tpc_lists, list, links);
632 mtx_unlock(&lun->lun_lock);
634 total_len = sizeof(*data) + list_copy.sense_len;
635 alloc_len = scsi_4btoul(cdb->length);
637 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
639 ctsio->kern_sg_entries = 0;
641 if (total_len < alloc_len) {
642 ctsio->residual = alloc_len - total_len;
643 ctsio->kern_data_len = total_len;
644 ctsio->kern_total_len = total_len;
647 ctsio->kern_data_len = alloc_len;
648 ctsio->kern_total_len = alloc_len;
650 ctsio->kern_data_resid = 0;
651 ctsio->kern_rel_offset = 0;
653 data = (struct scsi_receive_copy_failure_details_data *)ctsio->kern_data_ptr;
654 if (list_copy.completed && (list_copy.error || list_copy.abort)) {
655 scsi_ulto4b(sizeof(*data) - 4 + list_copy.sense_len,
656 data->available_data);
657 data->copy_command_status = RCS_CCS_ERROR;
659 scsi_ulto4b(0, data->available_data);
660 scsi_ulto2b(list_copy.sense_len, data->sense_data_length);
661 memcpy(data->sense_data, &list_copy.sense_data, list_copy.sense_len);
663 ctl_set_success(ctsio);
664 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
665 ctsio->be_move_done = ctl_config_move_done;
666 ctl_datamove((union ctl_io *)ctsio);
671 ctl_receive_copy_status_lid4(struct ctl_scsiio *ctsio)
674 struct scsi_receive_copy_status_lid4 *cdb;
675 struct scsi_receive_copy_status_lid4_data *data;
676 struct tpc_list *list;
677 struct tpc_list list_copy;
679 int alloc_len, total_len;
682 CTL_DEBUG_PRINT(("ctl_receive_copy_status_lid4\n"));
684 cdb = (struct scsi_receive_copy_status_lid4 *)ctsio->cdb;
685 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
687 retval = CTL_RETVAL_COMPLETE;
689 list_id = scsi_4btoul(cdb->list_identifier);
690 mtx_lock(&lun->lun_lock);
691 list = tpc_find_list(lun, list_id,
692 ctl_get_resindex(&ctsio->io_hdr.nexus));
694 mtx_unlock(&lun->lun_lock);
695 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
696 /*command*/ 1, /*field*/ 2, /*bit_valid*/ 0,
698 ctl_done((union ctl_io *)ctsio);
702 if (list->completed) {
703 TAILQ_REMOVE(&lun->tpc_lists, list, links);
706 mtx_unlock(&lun->lun_lock);
708 total_len = sizeof(*data) + list_copy.sense_len;
709 alloc_len = scsi_4btoul(cdb->length);
711 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
713 ctsio->kern_sg_entries = 0;
715 if (total_len < alloc_len) {
716 ctsio->residual = alloc_len - total_len;
717 ctsio->kern_data_len = total_len;
718 ctsio->kern_total_len = total_len;
721 ctsio->kern_data_len = alloc_len;
722 ctsio->kern_total_len = alloc_len;
724 ctsio->kern_data_resid = 0;
725 ctsio->kern_rel_offset = 0;
727 data = (struct scsi_receive_copy_status_lid4_data *)ctsio->kern_data_ptr;
728 scsi_ulto4b(sizeof(*data) - 4 + list_copy.sense_len,
729 data->available_data);
730 data->response_to_service_action = list_copy.service_action;
731 if (list_copy.completed) {
733 data->copy_command_status = RCS_CCS_ERROR;
734 else if (list_copy.abort)
735 data->copy_command_status = RCS_CCS_ABORTED;
737 data->copy_command_status = RCS_CCS_COMPLETED;
739 data->copy_command_status = RCS_CCS_INPROG_FG;
740 scsi_ulto2b(list_copy.curops, data->operation_counter);
741 scsi_ulto4b(UINT32_MAX, data->estimated_status_update_delay);
742 data->transfer_count_units = RCS_TC_BYTES;
743 scsi_u64to8b(list_copy.curbytes, data->transfer_count);
744 scsi_ulto2b(list_copy.curseg, data->segments_processed);
745 data->length_of_the_sense_data_field = list_copy.sense_len;
746 data->sense_data_length = list_copy.sense_len;
747 memcpy(data->sense_data, &list_copy.sense_data, list_copy.sense_len);
749 ctl_set_success(ctsio);
750 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
751 ctsio->be_move_done = ctl_config_move_done;
752 ctl_datamove((union ctl_io *)ctsio);
757 ctl_copy_operation_abort(struct ctl_scsiio *ctsio)
760 struct scsi_copy_operation_abort *cdb;
761 struct tpc_list *list;
765 CTL_DEBUG_PRINT(("ctl_copy_operation_abort\n"));
767 cdb = (struct scsi_copy_operation_abort *)ctsio->cdb;
768 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
770 retval = CTL_RETVAL_COMPLETE;
772 list_id = scsi_4btoul(cdb->list_identifier);
773 mtx_lock(&lun->lun_lock);
774 list = tpc_find_list(lun, list_id,
775 ctl_get_resindex(&ctsio->io_hdr.nexus));
777 mtx_unlock(&lun->lun_lock);
778 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
779 /*command*/ 1, /*field*/ 2, /*bit_valid*/ 0,
781 ctl_done((union ctl_io *)ctsio);
785 mtx_unlock(&lun->lun_lock);
787 ctl_set_success(ctsio);
788 ctl_done((union ctl_io *)ctsio);
793 tpc_resolve(struct tpc_list *list, uint16_t idx, uint32_t *ss,
794 uint32_t *pb, uint32_t *pbo)
798 if (ss && list->lun->be_lun)
799 *ss = list->lun->be_lun->blocksize;
800 if (pb && list->lun->be_lun)
801 *pb = list->lun->be_lun->blocksize <<
802 list->lun->be_lun->pblockexp;
803 if (pbo && list->lun->be_lun)
804 *pbo = list->lun->be_lun->blocksize *
805 list->lun->be_lun->pblockoff;
806 return (list->lun->lun);
808 if (idx >= list->ncscd)
810 return (tpcl_resolve(list->lun->ctl_softc,
811 list->init_port, &list->cscd[idx], ss, pb, pbo));
815 tpc_process_b2b(struct tpc_list *list)
817 struct scsi_ec_segment_b2b *seg;
818 struct scsi_ec_cscd_dtsp *sdstp, *ddstp;
819 struct tpc_io *tior, *tiow;
820 struct runl run, *prun;
822 off_t srclba, dstlba, numbytes, donebytes, roundbytes;
824 uint32_t srcblock, dstblock, pb, pbo, adj;
826 if (list->stage == 1) {
827 while ((tior = TAILQ_FIRST(&list->allio)) != NULL) {
828 TAILQ_REMOVE(&list->allio, tior, links);
829 ctl_free_io(tior->io);
832 free(list->buf, M_CTL);
834 ctl_set_task_aborted(list->ctsio);
835 return (CTL_RETVAL_ERROR);
836 } else if (list->error) {
837 ctl_set_sense(list->ctsio, /*current_error*/ 1,
838 /*sense_key*/ SSD_KEY_COPY_ABORTED,
839 /*asc*/ 0x0d, /*ascq*/ 0x01, SSD_ELEM_NONE);
840 return (CTL_RETVAL_ERROR);
842 list->cursectors += list->segsectors;
843 list->curbytes += list->segbytes;
844 return (CTL_RETVAL_COMPLETE);
847 TAILQ_INIT(&list->allio);
848 seg = (struct scsi_ec_segment_b2b *)list->seg[list->curseg];
849 sl = tpc_resolve(list, scsi_2btoul(seg->src_cscd), &srcblock, NULL, NULL);
850 dl = tpc_resolve(list, scsi_2btoul(seg->dst_cscd), &dstblock, &pb, &pbo);
851 if (sl >= CTL_MAX_LUNS || dl >= CTL_MAX_LUNS) {
852 ctl_set_sense(list->ctsio, /*current_error*/ 1,
853 /*sense_key*/ SSD_KEY_COPY_ABORTED,
854 /*asc*/ 0x08, /*ascq*/ 0x04, SSD_ELEM_NONE);
855 return (CTL_RETVAL_ERROR);
859 sdstp = &list->cscd[scsi_2btoul(seg->src_cscd)].dtsp;
860 if (scsi_3btoul(sdstp->block_length) != 0)
861 srcblock = scsi_3btoul(sdstp->block_length);
862 ddstp = &list->cscd[scsi_2btoul(seg->dst_cscd)].dtsp;
863 if (scsi_3btoul(ddstp->block_length) != 0)
864 dstblock = scsi_3btoul(ddstp->block_length);
865 numlba = scsi_2btoul(seg->number_of_blocks);
866 if (seg->flags & EC_SEG_DC)
867 numbytes = (off_t)numlba * dstblock;
869 numbytes = (off_t)numlba * srcblock;
870 srclba = scsi_8btou64(seg->src_lba);
871 dstlba = scsi_8btou64(seg->dst_lba);
873 // printf("Copy %ju bytes from %ju @ %ju to %ju @ %ju\n",
874 // (uintmax_t)numbytes, sl, scsi_8btou64(seg->src_lba),
875 // dl, scsi_8btou64(seg->dst_lba));
878 return (CTL_RETVAL_COMPLETE);
880 if (numbytes % srcblock != 0 || numbytes % dstblock != 0) {
881 ctl_set_sense(list->ctsio, /*current_error*/ 1,
882 /*sense_key*/ SSD_KEY_COPY_ABORTED,
883 /*asc*/ 0x26, /*ascq*/ 0x0A, SSD_ELEM_NONE);
884 return (CTL_RETVAL_ERROR);
887 list->buf = malloc(numbytes, M_CTL, M_WAITOK);
888 list->segbytes = numbytes;
889 list->segsectors = numbytes / dstblock;
894 while (donebytes < numbytes) {
895 roundbytes = numbytes - donebytes;
896 if (roundbytes > TPC_MAX_IO_SIZE) {
897 roundbytes = TPC_MAX_IO_SIZE;
898 roundbytes -= roundbytes % dstblock;
900 adj = (dstlba * dstblock + roundbytes - pbo) % pb;
901 if (roundbytes > adj)
906 tior = malloc(sizeof(*tior), M_CTL, M_WAITOK | M_ZERO);
907 TAILQ_INIT(&tior->run);
909 TAILQ_INSERT_TAIL(&list->allio, tior, links);
910 tior->io = tpcl_alloc_io();
911 ctl_scsi_read_write(tior->io,
912 /*data_ptr*/ &list->buf[donebytes],
913 /*data_len*/ roundbytes,
916 /*minimum_cdb_size*/ 0,
918 /*num_blocks*/ roundbytes / srcblock,
919 /*tag_type*/ CTL_TAG_SIMPLE,
921 tior->io->io_hdr.retries = 3;
923 tior->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tior;
925 tiow = malloc(sizeof(*tior), M_CTL, M_WAITOK | M_ZERO);
926 TAILQ_INIT(&tiow->run);
928 TAILQ_INSERT_TAIL(&list->allio, tiow, links);
929 tiow->io = tpcl_alloc_io();
930 ctl_scsi_read_write(tiow->io,
931 /*data_ptr*/ &list->buf[donebytes],
932 /*data_len*/ roundbytes,
935 /*minimum_cdb_size*/ 0,
937 /*num_blocks*/ roundbytes / dstblock,
938 /*tag_type*/ CTL_TAG_SIMPLE,
940 tiow->io->io_hdr.retries = 3;
942 tiow->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tiow;
944 TAILQ_INSERT_TAIL(&tior->run, tiow, rlinks);
945 TAILQ_INSERT_TAIL(prun, tior, rlinks);
947 donebytes += roundbytes;
948 srclba += roundbytes / srcblock;
949 dstlba += roundbytes / dstblock;
952 while ((tior = TAILQ_FIRST(&run)) != NULL) {
953 TAILQ_REMOVE(&run, tior, rlinks);
954 if (tpcl_queue(tior->io, tior->lun) != CTL_RETVAL_COMPLETE)
955 panic("tpcl_queue() error");
959 return (CTL_RETVAL_QUEUED);
963 tpc_process_verify(struct tpc_list *list)
965 struct scsi_ec_segment_verify *seg;
969 if (list->stage == 1) {
970 while ((tio = TAILQ_FIRST(&list->allio)) != NULL) {
971 TAILQ_REMOVE(&list->allio, tio, links);
972 ctl_free_io(tio->io);
976 ctl_set_task_aborted(list->ctsio);
977 return (CTL_RETVAL_ERROR);
978 } else if (list->error) {
979 ctl_set_sense(list->ctsio, /*current_error*/ 1,
980 /*sense_key*/ SSD_KEY_COPY_ABORTED,
981 /*asc*/ 0x0d, /*ascq*/ 0x01, SSD_ELEM_NONE);
982 return (CTL_RETVAL_ERROR);
984 return (CTL_RETVAL_COMPLETE);
987 TAILQ_INIT(&list->allio);
988 seg = (struct scsi_ec_segment_verify *)list->seg[list->curseg];
989 sl = tpc_resolve(list, scsi_2btoul(seg->src_cscd), NULL, NULL, NULL);
990 if (sl >= CTL_MAX_LUNS) {
991 ctl_set_sense(list->ctsio, /*current_error*/ 1,
992 /*sense_key*/ SSD_KEY_COPY_ABORTED,
993 /*asc*/ 0x08, /*ascq*/ 0x04, SSD_ELEM_NONE);
994 return (CTL_RETVAL_ERROR);
997 // printf("Verify %ju\n", sl);
999 if ((seg->tur & 0x01) == 0)
1000 return (CTL_RETVAL_COMPLETE);
1003 tio = malloc(sizeof(*tio), M_CTL, M_WAITOK | M_ZERO);
1004 TAILQ_INIT(&tio->run);
1006 TAILQ_INSERT_TAIL(&list->allio, tio, links);
1007 tio->io = tpcl_alloc_io();
1008 ctl_scsi_tur(tio->io, /*tag_type*/ CTL_TAG_SIMPLE, /*control*/ 0);
1009 tio->io->io_hdr.retries = 3;
1011 tio->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tio;
1013 if (tpcl_queue(tio->io, tio->lun) != CTL_RETVAL_COMPLETE)
1014 panic("tpcl_queue() error");
1015 return (CTL_RETVAL_QUEUED);
1019 tpc_process_register_key(struct tpc_list *list)
1021 struct scsi_ec_segment_register_key *seg;
1026 if (list->stage == 1) {
1027 while ((tio = TAILQ_FIRST(&list->allio)) != NULL) {
1028 TAILQ_REMOVE(&list->allio, tio, links);
1029 ctl_free_io(tio->io);
1032 free(list->buf, M_CTL);
1034 ctl_set_task_aborted(list->ctsio);
1035 return (CTL_RETVAL_ERROR);
1036 } else if (list->error) {
1037 ctl_set_sense(list->ctsio, /*current_error*/ 1,
1038 /*sense_key*/ SSD_KEY_COPY_ABORTED,
1039 /*asc*/ 0x0d, /*ascq*/ 0x01, SSD_ELEM_NONE);
1040 return (CTL_RETVAL_ERROR);
1042 return (CTL_RETVAL_COMPLETE);
1045 TAILQ_INIT(&list->allio);
1046 seg = (struct scsi_ec_segment_register_key *)list->seg[list->curseg];
1047 dl = tpc_resolve(list, scsi_2btoul(seg->dst_cscd), NULL, NULL, NULL);
1048 if (dl >= CTL_MAX_LUNS) {
1049 ctl_set_sense(list->ctsio, /*current_error*/ 1,
1050 /*sense_key*/ SSD_KEY_COPY_ABORTED,
1051 /*asc*/ 0x08, /*ascq*/ 0x04, SSD_ELEM_NONE);
1052 return (CTL_RETVAL_ERROR);
1055 // printf("Register Key %ju\n", dl);
1058 tio = malloc(sizeof(*tio), M_CTL, M_WAITOK | M_ZERO);
1059 TAILQ_INIT(&tio->run);
1061 TAILQ_INSERT_TAIL(&list->allio, tio, links);
1062 tio->io = tpcl_alloc_io();
1063 datalen = sizeof(struct scsi_per_res_out_parms);
1064 list->buf = malloc(datalen, M_CTL, M_WAITOK);
1065 ctl_scsi_persistent_res_out(tio->io,
1066 list->buf, datalen, SPRO_REGISTER, -1,
1067 scsi_8btou64(seg->res_key), scsi_8btou64(seg->sa_res_key),
1068 /*tag_type*/ CTL_TAG_SIMPLE, /*control*/ 0);
1069 tio->io->io_hdr.retries = 3;
1071 tio->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tio;
1073 if (tpcl_queue(tio->io, tio->lun) != CTL_RETVAL_COMPLETE)
1074 panic("tpcl_queue() error");
1075 return (CTL_RETVAL_QUEUED);
1079 tpc_ranges_length(struct scsi_range_desc *range, int nrange)
1084 for (r = 0; r < nrange; r++)
1085 length += scsi_4btoul(range[r].length);
1090 tpc_skip_ranges(struct scsi_range_desc *range, int nrange, off_t skip,
1091 int *srange, off_t *soffset)
1098 while (r < nrange) {
1099 if (skip - off < scsi_4btoul(range[r].length)) {
1101 *soffset = skip - off;
1104 off += scsi_4btoul(range[r].length);
1111 tpc_process_wut(struct tpc_list *list)
1113 struct tpc_io *tio, *tior, *tiow;
1114 struct runl run, *prun;
1116 off_t doffset, soffset;
1117 off_t srclba, dstlba, numbytes, donebytes, roundbytes;
1118 uint32_t srcblock, dstblock, pb, pbo, adj;
1120 if (list->stage > 0) {
1121 /* Cleanup after previous rounds. */
1122 while ((tio = TAILQ_FIRST(&list->allio)) != NULL) {
1123 TAILQ_REMOVE(&list->allio, tio, links);
1124 ctl_free_io(tio->io);
1127 free(list->buf, M_CTL);
1129 ctl_set_task_aborted(list->ctsio);
1130 return (CTL_RETVAL_ERROR);
1131 } else if (list->error) {
1132 ctl_set_sense(list->ctsio, /*current_error*/ 1,
1133 /*sense_key*/ SSD_KEY_COPY_ABORTED,
1134 /*asc*/ 0x0d, /*ascq*/ 0x01, SSD_ELEM_NONE);
1135 return (CTL_RETVAL_ERROR);
1137 list->cursectors += list->segsectors;
1138 list->curbytes += list->segbytes;
1141 /* Check where we are on destination ranges list. */
1142 if (tpc_skip_ranges(list->range, list->nrange, list->cursectors,
1143 &drange, &doffset) != 0)
1144 return (CTL_RETVAL_COMPLETE);
1145 dstblock = list->lun->be_lun->blocksize;
1146 pb = dstblock << list->lun->be_lun->pblockexp;
1147 if (list->lun->be_lun->pblockoff > 0)
1148 pbo = pb - dstblock * list->lun->be_lun->pblockoff;
1152 /* Check where we are on source ranges list. */
1153 srcblock = list->token->blocksize;
1154 if (tpc_skip_ranges(list->token->range, list->token->nrange,
1155 list->offset_into_rod + list->cursectors * dstblock / srcblock,
1156 &srange, &soffset) != 0) {
1157 ctl_set_sense(list->ctsio, /*current_error*/ 1,
1158 /*sense_key*/ SSD_KEY_COPY_ABORTED,
1159 /*asc*/ 0x0d, /*ascq*/ 0x04, SSD_ELEM_NONE);
1160 return (CTL_RETVAL_ERROR);
1163 srclba = scsi_8btou64(list->token->range[srange].lba) + soffset;
1164 dstlba = scsi_8btou64(list->range[drange].lba) + doffset;
1165 numbytes = srcblock *
1166 (scsi_4btoul(list->token->range[srange].length) - soffset);
1167 numbytes = omin(numbytes, dstblock *
1168 (scsi_4btoul(list->range[drange].length) - doffset));
1169 if (numbytes > TPC_MAX_IOCHUNK_SIZE) {
1170 numbytes = TPC_MAX_IOCHUNK_SIZE;
1171 numbytes -= numbytes % dstblock;
1172 if (pb > dstblock) {
1173 adj = (dstlba * dstblock + numbytes - pbo) % pb;
1179 if (numbytes % srcblock != 0 || numbytes % dstblock != 0) {
1180 ctl_set_sense(list->ctsio, /*current_error*/ 1,
1181 /*sense_key*/ SSD_KEY_COPY_ABORTED,
1182 /*asc*/ 0x26, /*ascq*/ 0x0A, SSD_ELEM_NONE);
1183 return (CTL_RETVAL_ERROR);
1186 list->buf = malloc(numbytes, M_CTL, M_WAITOK |
1187 (list->token == NULL ? M_ZERO : 0));
1188 list->segbytes = numbytes;
1189 list->segsectors = numbytes / dstblock;
1190 //printf("Copy chunk of %ju sectors from %ju to %ju\n", list->segsectors,
1196 TAILQ_INIT(&list->allio);
1197 while (donebytes < numbytes) {
1198 roundbytes = numbytes - donebytes;
1199 if (roundbytes > TPC_MAX_IO_SIZE) {
1200 roundbytes = TPC_MAX_IO_SIZE;
1201 roundbytes -= roundbytes % dstblock;
1202 if (pb > dstblock) {
1203 adj = (dstlba * dstblock + roundbytes - pbo) % pb;
1204 if (roundbytes > adj)
1209 tior = malloc(sizeof(*tior), M_CTL, M_WAITOK | M_ZERO);
1210 TAILQ_INIT(&tior->run);
1212 TAILQ_INSERT_TAIL(&list->allio, tior, links);
1213 tior->io = tpcl_alloc_io();
1214 ctl_scsi_read_write(tior->io,
1215 /*data_ptr*/ &list->buf[donebytes],
1216 /*data_len*/ roundbytes,
1219 /*minimum_cdb_size*/ 0,
1221 /*num_blocks*/ roundbytes / srcblock,
1222 /*tag_type*/ CTL_TAG_SIMPLE,
1224 tior->io->io_hdr.retries = 3;
1225 tior->lun = list->token->lun;
1226 tior->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tior;
1228 tiow = malloc(sizeof(*tiow), M_CTL, M_WAITOK | M_ZERO);
1229 TAILQ_INIT(&tiow->run);
1231 TAILQ_INSERT_TAIL(&list->allio, tiow, links);
1232 tiow->io = tpcl_alloc_io();
1233 ctl_scsi_read_write(tiow->io,
1234 /*data_ptr*/ &list->buf[donebytes],
1235 /*data_len*/ roundbytes,
1238 /*minimum_cdb_size*/ 0,
1240 /*num_blocks*/ roundbytes / dstblock,
1241 /*tag_type*/ CTL_TAG_SIMPLE,
1243 tiow->io->io_hdr.retries = 3;
1244 tiow->lun = list->lun->lun;
1245 tiow->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tiow;
1247 TAILQ_INSERT_TAIL(&tior->run, tiow, rlinks);
1248 TAILQ_INSERT_TAIL(prun, tior, rlinks);
1250 donebytes += roundbytes;
1251 srclba += roundbytes / srcblock;
1252 dstlba += roundbytes / dstblock;
1255 while ((tior = TAILQ_FIRST(&run)) != NULL) {
1256 TAILQ_REMOVE(&run, tior, rlinks);
1257 if (tpcl_queue(tior->io, tior->lun) != CTL_RETVAL_COMPLETE)
1258 panic("tpcl_queue() error");
1262 return (CTL_RETVAL_QUEUED);
1266 tpc_process_zero_wut(struct tpc_list *list)
1268 struct tpc_io *tio, *tiow;
1269 struct runl run, *prun;
1271 uint32_t dstblock, len;
1273 if (list->stage > 0) {
1275 /* Cleanup after previous rounds. */
1276 while ((tio = TAILQ_FIRST(&list->allio)) != NULL) {
1277 TAILQ_REMOVE(&list->allio, tio, links);
1278 ctl_free_io(tio->io);
1281 free(list->buf, M_CTL);
1283 ctl_set_task_aborted(list->ctsio);
1284 return (CTL_RETVAL_ERROR);
1285 } else if (list->error) {
1286 ctl_set_sense(list->ctsio, /*current_error*/ 1,
1287 /*sense_key*/ SSD_KEY_COPY_ABORTED,
1288 /*asc*/ 0x0d, /*ascq*/ 0x01, SSD_ELEM_NONE);
1289 return (CTL_RETVAL_ERROR);
1291 list->cursectors += list->segsectors;
1292 list->curbytes += list->segbytes;
1293 return (CTL_RETVAL_COMPLETE);
1296 dstblock = list->lun->be_lun->blocksize;
1297 list->buf = malloc(dstblock, M_CTL, M_WAITOK | M_ZERO);
1301 TAILQ_INIT(&list->allio);
1302 list->segsectors = 0;
1303 for (r = 0; r < list->nrange; r++) {
1304 len = scsi_4btoul(list->range[r].length);
1308 tiow = malloc(sizeof(*tiow), M_CTL, M_WAITOK | M_ZERO);
1309 TAILQ_INIT(&tiow->run);
1311 TAILQ_INSERT_TAIL(&list->allio, tiow, links);
1312 tiow->io = tpcl_alloc_io();
1313 ctl_scsi_write_same(tiow->io,
1314 /*data_ptr*/ list->buf,
1315 /*data_len*/ dstblock,
1317 /*lba*/ scsi_8btou64(list->range[r].lba),
1319 /*tag_type*/ CTL_TAG_SIMPLE,
1321 tiow->io->io_hdr.retries = 3;
1322 tiow->lun = list->lun->lun;
1323 tiow->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tiow;
1325 TAILQ_INSERT_TAIL(prun, tiow, rlinks);
1327 list->segsectors += len;
1329 list->segbytes = list->segsectors * dstblock;
1331 if (TAILQ_EMPTY(&run))
1334 while ((tiow = TAILQ_FIRST(&run)) != NULL) {
1335 TAILQ_REMOVE(&run, tiow, rlinks);
1336 if (tpcl_queue(tiow->io, tiow->lun) != CTL_RETVAL_COMPLETE)
1337 panic("tpcl_queue() error");
1341 return (CTL_RETVAL_QUEUED);
1345 tpc_process(struct tpc_list *list)
1347 struct ctl_lun *lun = list->lun;
1348 struct ctl_softc *softc = lun->ctl_softc;
1349 struct scsi_ec_segment *seg;
1350 struct ctl_scsiio *ctsio = list->ctsio;
1351 int retval = CTL_RETVAL_COMPLETE;
1353 if (list->service_action == EC_WUT) {
1354 if (list->token != NULL)
1355 retval = tpc_process_wut(list);
1357 retval = tpc_process_zero_wut(list);
1358 if (retval == CTL_RETVAL_QUEUED)
1360 if (retval == CTL_RETVAL_ERROR) {
1365 //printf("ZZZ %d cscd, %d segs\n", list->ncscd, list->nseg);
1366 while (list->curseg < list->nseg) {
1367 seg = list->seg[list->curseg];
1368 switch (seg->type_code) {
1370 retval = tpc_process_b2b(list);
1373 retval = tpc_process_verify(list);
1375 case EC_SEG_REGISTER_KEY:
1376 retval = tpc_process_register_key(list);
1379 ctl_set_sense(ctsio, /*current_error*/ 1,
1380 /*sense_key*/ SSD_KEY_COPY_ABORTED,
1381 /*asc*/ 0x26, /*ascq*/ 0x09, SSD_ELEM_NONE);
1384 if (retval == CTL_RETVAL_QUEUED)
1386 if (retval == CTL_RETVAL_ERROR) {
1395 ctl_set_success(ctsio);
1398 //printf("ZZZ done\n");
1399 free(list->params, M_CTL);
1400 list->params = NULL;
1402 mtx_lock(&softc->tpc_lock);
1403 if (--list->token->active == 0)
1404 list->token->last_active = time_uptime;
1405 mtx_unlock(&softc->tpc_lock);
1408 mtx_lock(&lun->lun_lock);
1409 if ((list->flags & EC_LIST_ID_USAGE_MASK) == EC_LIST_ID_USAGE_NONE) {
1410 TAILQ_REMOVE(&lun->tpc_lists, list, links);
1413 list->completed = 1;
1414 list->last_active = time_uptime;
1415 list->sense_data = ctsio->sense_data;
1416 list->sense_len = ctsio->sense_len;
1417 list->scsi_status = ctsio->scsi_status;
1419 mtx_unlock(&lun->lun_lock);
1421 ctl_done((union ctl_io *)ctsio);
1425 * For any sort of check condition, busy, etc., we just retry. We do not
1426 * decrement the retry count for unit attention type errors. These are
1427 * normal, and we want to save the retry count for "real" errors. Otherwise,
1428 * we could end up with situations where a command will succeed in some
1429 * situations and fail in others, depending on whether a unit attention is
1430 * pending. Also, some of our error recovery actions, most notably the
1431 * LUN reset action, will cause a unit attention.
1433 * We can add more detail here later if necessary.
1435 static tpc_error_action
1436 tpc_checkcond_parse(union ctl_io *io)
1438 tpc_error_action error_action;
1439 int error_code, sense_key, asc, ascq;
1442 * Default to retrying the command.
1444 error_action = TPC_ERR_RETRY;
1446 scsi_extract_sense_len(&io->scsiio.sense_data,
1447 io->scsiio.sense_len,
1454 switch (error_code) {
1455 case SSD_DEFERRED_ERROR:
1456 case SSD_DESC_DEFERRED_ERROR:
1457 error_action |= TPC_ERR_NO_DECREMENT;
1459 case SSD_CURRENT_ERROR:
1460 case SSD_DESC_CURRENT_ERROR:
1462 switch (sense_key) {
1463 case SSD_KEY_UNIT_ATTENTION:
1464 error_action |= TPC_ERR_NO_DECREMENT;
1466 case SSD_KEY_HARDWARE_ERROR:
1468 * This is our generic "something bad happened"
1469 * error code. It often isn't recoverable.
1471 if ((asc == 0x44) && (ascq == 0x00))
1472 error_action = TPC_ERR_FAIL;
1474 case SSD_KEY_NOT_READY:
1476 * If the LUN is powered down, there likely isn't
1477 * much point in retrying right now.
1479 if ((asc == 0x04) && (ascq == 0x02))
1480 error_action = TPC_ERR_FAIL;
1482 * If the LUN is offline, there probably isn't much
1483 * point in retrying, either.
1485 if ((asc == 0x04) && (ascq == 0x03))
1486 error_action = TPC_ERR_FAIL;
1490 return (error_action);
1493 static tpc_error_action
1494 tpc_error_parse(union ctl_io *io)
1496 tpc_error_action error_action = TPC_ERR_RETRY;
1498 switch (io->io_hdr.io_type) {
1500 switch (io->io_hdr.status & CTL_STATUS_MASK) {
1501 case CTL_SCSI_ERROR:
1502 switch (io->scsiio.scsi_status) {
1503 case SCSI_STATUS_CHECK_COND:
1504 error_action = tpc_checkcond_parse(io);
1517 panic("%s: invalid ctl_io type %d\n", __func__,
1518 io->io_hdr.io_type);
1521 return (error_action);
1525 tpc_done(union ctl_io *io)
1527 struct tpc_io *tio, *tior;
1530 * Very minimal retry logic. We basically retry if we got an error
1531 * back, and the retry count is greater than 0. If we ever want
1532 * more sophisticated initiator type behavior, the CAM error
1533 * recovery code in ../common might be helpful.
1535 tio = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1536 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
1537 && (io->io_hdr.retries > 0)) {
1538 ctl_io_status old_status;
1539 tpc_error_action error_action;
1541 error_action = tpc_error_parse(io);
1542 switch (error_action & TPC_ERR_MASK) {
1547 if ((error_action & TPC_ERR_NO_DECREMENT) == 0)
1548 io->io_hdr.retries--;
1549 old_status = io->io_hdr.status;
1550 io->io_hdr.status = CTL_STATUS_NONE;
1551 io->io_hdr.flags &= ~CTL_FLAG_ABORT;
1552 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1553 if (tpcl_queue(io, tio->lun) != CTL_RETVAL_COMPLETE) {
1554 printf("%s: error returned from ctl_queue()!\n",
1556 io->io_hdr.status = old_status;
1562 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
1563 tio->list->error = 1;
1565 atomic_add_int(&tio->list->curops, 1);
1566 if (!tio->list->error && !tio->list->abort) {
1567 while ((tior = TAILQ_FIRST(&tio->run)) != NULL) {
1568 TAILQ_REMOVE(&tio->run, tior, rlinks);
1569 atomic_add_int(&tio->list->tbdio, 1);
1570 if (tpcl_queue(tior->io, tior->lun) != CTL_RETVAL_COMPLETE)
1571 panic("tpcl_queue() error");
1574 if (atomic_fetchadd_int(&tio->list->tbdio, -1) == 1)
1575 tpc_process(tio->list);
1579 ctl_extended_copy_lid1(struct ctl_scsiio *ctsio)
1581 struct scsi_extended_copy *cdb;
1582 struct scsi_extended_copy_lid1_data *data;
1583 struct ctl_lun *lun;
1584 struct tpc_list *list, *tlist;
1587 int len, off, lencscd, lenseg, leninl, nseg;
1589 CTL_DEBUG_PRINT(("ctl_extended_copy_lid1\n"));
1591 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
1592 cdb = (struct scsi_extended_copy *)ctsio->cdb;
1593 len = scsi_4btoul(cdb->length);
1595 if (len < sizeof(struct scsi_extended_copy_lid1_data) ||
1596 len > sizeof(struct scsi_extended_copy_lid1_data) +
1597 TPC_MAX_LIST + TPC_MAX_INLINE) {
1598 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
1599 /*field*/ 9, /*bit_valid*/ 0, /*bit*/ 0);
1604 * If we've got a kernel request that hasn't been malloced yet,
1605 * malloc it and tell the caller the data buffer is here.
1607 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
1608 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
1609 ctsio->kern_data_len = len;
1610 ctsio->kern_total_len = len;
1611 ctsio->kern_data_resid = 0;
1612 ctsio->kern_rel_offset = 0;
1613 ctsio->kern_sg_entries = 0;
1614 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
1615 ctsio->be_move_done = ctl_config_move_done;
1616 ctl_datamove((union ctl_io *)ctsio);
1618 return (CTL_RETVAL_COMPLETE);
1621 data = (struct scsi_extended_copy_lid1_data *)ctsio->kern_data_ptr;
1622 lencscd = scsi_2btoul(data->cscd_list_length);
1623 lenseg = scsi_4btoul(data->segment_list_length);
1624 leninl = scsi_4btoul(data->inline_data_length);
1625 if (len < sizeof(struct scsi_extended_copy_lid1_data) +
1626 lencscd + lenseg + leninl ||
1627 leninl > TPC_MAX_INLINE) {
1628 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 0,
1629 /*field*/ 2, /*bit_valid*/ 0, /*bit*/ 0);
1632 if (lencscd > TPC_MAX_CSCDS * sizeof(struct scsi_ec_cscd)) {
1633 ctl_set_sense(ctsio, /*current_error*/ 1,
1634 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1635 /*asc*/ 0x26, /*ascq*/ 0x06, SSD_ELEM_NONE);
1638 if (lencscd + lenseg > TPC_MAX_LIST) {
1639 ctl_set_param_len_error(ctsio);
1643 list = malloc(sizeof(struct tpc_list), M_CTL, M_WAITOK | M_ZERO);
1644 list->service_action = cdb->service_action;
1645 value = ctl_get_opt(&lun->be_lun->options, "insecure_tpc");
1646 if (value != NULL && strcmp(value, "on") == 0)
1647 list->init_port = -1;
1649 list->init_port = ctsio->io_hdr.nexus.targ_port;
1650 list->init_idx = ctl_get_resindex(&ctsio->io_hdr.nexus);
1651 list->list_id = data->list_identifier;
1652 list->flags = data->flags;
1653 list->params = ctsio->kern_data_ptr;
1654 list->cscd = (struct scsi_ec_cscd *)&data->data[0];
1655 ptr = &data->data[lencscd];
1656 for (nseg = 0, off = 0; off < lenseg; nseg++) {
1657 if (nseg >= TPC_MAX_SEGS) {
1659 ctl_set_sense(ctsio, /*current_error*/ 1,
1660 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1661 /*asc*/ 0x26, /*ascq*/ 0x08, SSD_ELEM_NONE);
1664 list->seg[nseg] = (struct scsi_ec_segment *)(ptr + off);
1665 off += sizeof(struct scsi_ec_segment) +
1666 scsi_2btoul(list->seg[nseg]->descr_length);
1668 list->inl = &data->data[lencscd + lenseg];
1669 list->ncscd = lencscd / sizeof(struct scsi_ec_cscd);
1671 list->leninl = leninl;
1672 list->ctsio = ctsio;
1674 mtx_lock(&lun->lun_lock);
1675 if ((list->flags & EC_LIST_ID_USAGE_MASK) != EC_LIST_ID_USAGE_NONE) {
1676 tlist = tpc_find_list(lun, list->list_id, list->init_idx);
1677 if (tlist != NULL && !tlist->completed) {
1678 mtx_unlock(&lun->lun_lock);
1680 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
1681 /*command*/ 0, /*field*/ 0, /*bit_valid*/ 0,
1685 if (tlist != NULL) {
1686 TAILQ_REMOVE(&lun->tpc_lists, tlist, links);
1690 TAILQ_INSERT_TAIL(&lun->tpc_lists, list, links);
1691 mtx_unlock(&lun->lun_lock);
1694 return (CTL_RETVAL_COMPLETE);
1697 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
1698 free(ctsio->kern_data_ptr, M_CTL);
1699 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
1701 ctl_done((union ctl_io *)ctsio);
1702 return (CTL_RETVAL_COMPLETE);
1706 ctl_extended_copy_lid4(struct ctl_scsiio *ctsio)
1708 struct scsi_extended_copy *cdb;
1709 struct scsi_extended_copy_lid4_data *data;
1710 struct ctl_lun *lun;
1711 struct tpc_list *list, *tlist;
1714 int len, off, lencscd, lenseg, leninl, nseg;
1716 CTL_DEBUG_PRINT(("ctl_extended_copy_lid4\n"));
1718 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
1719 cdb = (struct scsi_extended_copy *)ctsio->cdb;
1720 len = scsi_4btoul(cdb->length);
1722 if (len < sizeof(struct scsi_extended_copy_lid4_data) ||
1723 len > sizeof(struct scsi_extended_copy_lid4_data) +
1724 TPC_MAX_LIST + TPC_MAX_INLINE) {
1725 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
1726 /*field*/ 9, /*bit_valid*/ 0, /*bit*/ 0);
1731 * If we've got a kernel request that hasn't been malloced yet,
1732 * malloc it and tell the caller the data buffer is here.
1734 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
1735 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
1736 ctsio->kern_data_len = len;
1737 ctsio->kern_total_len = len;
1738 ctsio->kern_data_resid = 0;
1739 ctsio->kern_rel_offset = 0;
1740 ctsio->kern_sg_entries = 0;
1741 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
1742 ctsio->be_move_done = ctl_config_move_done;
1743 ctl_datamove((union ctl_io *)ctsio);
1745 return (CTL_RETVAL_COMPLETE);
1748 data = (struct scsi_extended_copy_lid4_data *)ctsio->kern_data_ptr;
1749 lencscd = scsi_2btoul(data->cscd_list_length);
1750 lenseg = scsi_2btoul(data->segment_list_length);
1751 leninl = scsi_2btoul(data->inline_data_length);
1752 if (len < sizeof(struct scsi_extended_copy_lid4_data) +
1753 lencscd + lenseg + leninl ||
1754 leninl > TPC_MAX_INLINE) {
1755 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 0,
1756 /*field*/ 2, /*bit_valid*/ 0, /*bit*/ 0);
1759 if (lencscd > TPC_MAX_CSCDS * sizeof(struct scsi_ec_cscd)) {
1760 ctl_set_sense(ctsio, /*current_error*/ 1,
1761 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1762 /*asc*/ 0x26, /*ascq*/ 0x06, SSD_ELEM_NONE);
1765 if (lencscd + lenseg > TPC_MAX_LIST) {
1766 ctl_set_param_len_error(ctsio);
1770 list = malloc(sizeof(struct tpc_list), M_CTL, M_WAITOK | M_ZERO);
1771 list->service_action = cdb->service_action;
1772 value = ctl_get_opt(&lun->be_lun->options, "insecure_tpc");
1773 if (value != NULL && strcmp(value, "on") == 0)
1774 list->init_port = -1;
1776 list->init_port = ctsio->io_hdr.nexus.targ_port;
1777 list->init_idx = ctl_get_resindex(&ctsio->io_hdr.nexus);
1778 list->list_id = scsi_4btoul(data->list_identifier);
1779 list->flags = data->flags;
1780 list->params = ctsio->kern_data_ptr;
1781 list->cscd = (struct scsi_ec_cscd *)&data->data[0];
1782 ptr = &data->data[lencscd];
1783 for (nseg = 0, off = 0; off < lenseg; nseg++) {
1784 if (nseg >= TPC_MAX_SEGS) {
1786 ctl_set_sense(ctsio, /*current_error*/ 1,
1787 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1788 /*asc*/ 0x26, /*ascq*/ 0x08, SSD_ELEM_NONE);
1791 list->seg[nseg] = (struct scsi_ec_segment *)(ptr + off);
1792 off += sizeof(struct scsi_ec_segment) +
1793 scsi_2btoul(list->seg[nseg]->descr_length);
1795 list->inl = &data->data[lencscd + lenseg];
1796 list->ncscd = lencscd / sizeof(struct scsi_ec_cscd);
1798 list->leninl = leninl;
1799 list->ctsio = ctsio;
1801 mtx_lock(&lun->lun_lock);
1802 if ((list->flags & EC_LIST_ID_USAGE_MASK) != EC_LIST_ID_USAGE_NONE) {
1803 tlist = tpc_find_list(lun, list->list_id, list->init_idx);
1804 if (tlist != NULL && !tlist->completed) {
1805 mtx_unlock(&lun->lun_lock);
1807 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
1808 /*command*/ 0, /*field*/ 0, /*bit_valid*/ 0,
1812 if (tlist != NULL) {
1813 TAILQ_REMOVE(&lun->tpc_lists, tlist, links);
1817 TAILQ_INSERT_TAIL(&lun->tpc_lists, list, links);
1818 mtx_unlock(&lun->lun_lock);
1821 return (CTL_RETVAL_COMPLETE);
1824 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
1825 free(ctsio->kern_data_ptr, M_CTL);
1826 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
1828 ctl_done((union ctl_io *)ctsio);
1829 return (CTL_RETVAL_COMPLETE);
1833 tpc_create_token(struct ctl_lun *lun, struct ctl_port *port, off_t len,
1834 struct scsi_token *token)
1837 struct scsi_vpd_id_descriptor *idd = NULL;
1838 struct scsi_ec_cscd_id *cscd;
1839 struct scsi_read_capacity_data_long *dtsd;
1842 scsi_ulto4b(ROD_TYPE_AUR, token->type);
1843 scsi_ulto2b(0x01f8, token->length);
1844 scsi_u64to8b(atomic_fetchadd_int(&id, 1), &token->body[0]);
1846 idd = scsi_get_devid_desc((struct scsi_vpd_id_descriptor *)
1847 lun->lun_devid->data, lun->lun_devid->len,
1848 scsi_devid_is_lun_naa);
1849 if (idd == NULL && lun->lun_devid)
1850 idd = scsi_get_devid_desc((struct scsi_vpd_id_descriptor *)
1851 lun->lun_devid->data, lun->lun_devid->len,
1852 scsi_devid_is_lun_eui64);
1854 cscd = (struct scsi_ec_cscd_id *)&token->body[8];
1855 cscd->type_code = EC_CSCD_ID;
1856 cscd->luidt_pdt = T_DIRECT;
1857 memcpy(&cscd->codeset, idd, 4 + idd->length);
1858 scsi_ulto3b(lun->be_lun->blocksize, cscd->dtsp.block_length);
1860 scsi_u64to8b(0, &token->body[40]); /* XXX: Should be 128bit value. */
1861 scsi_u64to8b(len, &token->body[48]);
1863 /* ROD token device type specific data (RC16 without first field) */
1864 dtsd = (struct scsi_read_capacity_data_long *)&token->body[88 - 8];
1865 scsi_ulto4b(lun->be_lun->blocksize, dtsd->length);
1866 dtsd->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
1867 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, dtsd->lalba_lbp);
1868 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
1869 dtsd->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
1871 if (port->target_devid) {
1872 targid_len = port->target_devid->len;
1873 memcpy(&token->body[120], port->target_devid->data, targid_len);
1876 arc4rand(&token->body[120 + targid_len], 384 - targid_len, 0);
1880 ctl_populate_token(struct ctl_scsiio *ctsio)
1882 struct scsi_populate_token *cdb;
1883 struct scsi_populate_token_data *data;
1884 struct ctl_softc *softc;
1885 struct ctl_lun *lun;
1886 struct ctl_port *port;
1887 struct tpc_list *list, *tlist;
1888 struct tpc_token *token;
1891 CTL_DEBUG_PRINT(("ctl_populate_token\n"));
1893 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
1894 softc = lun->ctl_softc;
1895 port = softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
1896 cdb = (struct scsi_populate_token *)ctsio->cdb;
1897 len = scsi_4btoul(cdb->length);
1899 if (len < sizeof(struct scsi_populate_token_data) ||
1900 len > sizeof(struct scsi_populate_token_data) +
1901 TPC_MAX_SEGS * sizeof(struct scsi_range_desc)) {
1902 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
1903 /*field*/ 9, /*bit_valid*/ 0, /*bit*/ 0);
1908 * If we've got a kernel request that hasn't been malloced yet,
1909 * malloc it and tell the caller the data buffer is here.
1911 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
1912 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
1913 ctsio->kern_data_len = len;
1914 ctsio->kern_total_len = len;
1915 ctsio->kern_data_resid = 0;
1916 ctsio->kern_rel_offset = 0;
1917 ctsio->kern_sg_entries = 0;
1918 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
1919 ctsio->be_move_done = ctl_config_move_done;
1920 ctl_datamove((union ctl_io *)ctsio);
1922 return (CTL_RETVAL_COMPLETE);
1925 data = (struct scsi_populate_token_data *)ctsio->kern_data_ptr;
1926 lendesc = scsi_2btoul(data->range_descriptor_length);
1927 if (len < sizeof(struct scsi_populate_token_data) + lendesc) {
1928 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 0,
1929 /*field*/ 2, /*bit_valid*/ 0, /*bit*/ 0);
1933 printf("PT(list=%u) flags=%x to=%d rt=%x len=%x\n",
1934 scsi_4btoul(cdb->list_identifier),
1935 data->flags, scsi_4btoul(data->inactivity_timeout),
1936 scsi_4btoul(data->rod_type),
1937 scsi_2btoul(data->range_descriptor_length));
1939 if ((data->flags & EC_PT_RTV) &&
1940 scsi_4btoul(data->rod_type) != ROD_TYPE_AUR) {
1941 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 0,
1942 /*field*/ 8, /*bit_valid*/ 0, /*bit*/ 0);
1946 list = malloc(sizeof(struct tpc_list), M_CTL, M_WAITOK | M_ZERO);
1947 list->service_action = cdb->service_action;
1948 list->init_port = ctsio->io_hdr.nexus.targ_port;
1949 list->init_idx = ctl_get_resindex(&ctsio->io_hdr.nexus);
1950 list->list_id = scsi_4btoul(cdb->list_identifier);
1951 list->flags = data->flags;
1952 list->ctsio = ctsio;
1954 mtx_lock(&lun->lun_lock);
1955 tlist = tpc_find_list(lun, list->list_id, list->init_idx);
1956 if (tlist != NULL && !tlist->completed) {
1957 mtx_unlock(&lun->lun_lock);
1959 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
1960 /*command*/ 0, /*field*/ 0, /*bit_valid*/ 0,
1964 if (tlist != NULL) {
1965 TAILQ_REMOVE(&lun->tpc_lists, tlist, links);
1968 TAILQ_INSERT_TAIL(&lun->tpc_lists, list, links);
1969 mtx_unlock(&lun->lun_lock);
1971 token = malloc(sizeof(*token), M_CTL, M_WAITOK | M_ZERO);
1972 token->lun = lun->lun;
1973 token->blocksize = lun->be_lun->blocksize;
1974 token->params = ctsio->kern_data_ptr;
1975 token->range = &data->desc[0];
1976 token->nrange = scsi_2btoul(data->range_descriptor_length) /
1977 sizeof(struct scsi_range_desc);
1978 list->cursectors = tpc_ranges_length(token->range, token->nrange);
1979 list->curbytes = (off_t)list->cursectors * lun->be_lun->blocksize;
1980 tpc_create_token(lun, port, list->curbytes,
1981 (struct scsi_token *)token->token);
1983 token->last_active = time_uptime;
1984 token->timeout = scsi_4btoul(data->inactivity_timeout);
1985 if (token->timeout == 0)
1986 token->timeout = TPC_DFL_TOKEN_TIMEOUT;
1987 else if (token->timeout < TPC_MIN_TOKEN_TIMEOUT)
1988 token->timeout = TPC_MIN_TOKEN_TIMEOUT;
1989 else if (token->timeout > TPC_MAX_TOKEN_TIMEOUT) {
1990 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
1991 /*command*/ 0, /*field*/ 4, /*bit_valid*/ 0,
1994 memcpy(list->res_token, token->token, sizeof(list->res_token));
1995 list->res_token_valid = 1;
1997 list->completed = 1;
1998 list->last_active = time_uptime;
1999 mtx_lock(&softc->tpc_lock);
2000 TAILQ_INSERT_TAIL(&softc->tpc_tokens, token, links);
2001 mtx_unlock(&softc->tpc_lock);
2002 ctl_set_success(ctsio);
2003 ctl_done((union ctl_io *)ctsio);
2004 return (CTL_RETVAL_COMPLETE);
2007 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
2008 free(ctsio->kern_data_ptr, M_CTL);
2009 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
2011 ctl_done((union ctl_io *)ctsio);
2012 return (CTL_RETVAL_COMPLETE);
2016 ctl_write_using_token(struct ctl_scsiio *ctsio)
2018 struct scsi_write_using_token *cdb;
2019 struct scsi_write_using_token_data *data;
2020 struct ctl_softc *softc;
2021 struct ctl_lun *lun;
2022 struct tpc_list *list, *tlist;
2023 struct tpc_token *token;
2026 CTL_DEBUG_PRINT(("ctl_write_using_token\n"));
2028 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
2029 softc = lun->ctl_softc;
2030 cdb = (struct scsi_write_using_token *)ctsio->cdb;
2031 len = scsi_4btoul(cdb->length);
2033 if (len < sizeof(struct scsi_populate_token_data) ||
2034 len > sizeof(struct scsi_populate_token_data) +
2035 TPC_MAX_SEGS * sizeof(struct scsi_range_desc)) {
2036 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
2037 /*field*/ 9, /*bit_valid*/ 0, /*bit*/ 0);
2042 * If we've got a kernel request that hasn't been malloced yet,
2043 * malloc it and tell the caller the data buffer is here.
2045 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
2046 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
2047 ctsio->kern_data_len = len;
2048 ctsio->kern_total_len = len;
2049 ctsio->kern_data_resid = 0;
2050 ctsio->kern_rel_offset = 0;
2051 ctsio->kern_sg_entries = 0;
2052 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
2053 ctsio->be_move_done = ctl_config_move_done;
2054 ctl_datamove((union ctl_io *)ctsio);
2056 return (CTL_RETVAL_COMPLETE);
2059 data = (struct scsi_write_using_token_data *)ctsio->kern_data_ptr;
2060 lendesc = scsi_2btoul(data->range_descriptor_length);
2061 if (len < sizeof(struct scsi_populate_token_data) + lendesc) {
2062 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 0,
2063 /*field*/ 2, /*bit_valid*/ 0, /*bit*/ 0);
2067 printf("WUT(list=%u) flags=%x off=%ju len=%x\n",
2068 scsi_4btoul(cdb->list_identifier),
2069 data->flags, scsi_8btou64(data->offset_into_rod),
2070 scsi_2btoul(data->range_descriptor_length));
2072 list = malloc(sizeof(struct tpc_list), M_CTL, M_WAITOK | M_ZERO);
2073 list->service_action = cdb->service_action;
2074 list->init_port = ctsio->io_hdr.nexus.targ_port;
2075 list->init_idx = ctl_get_resindex(&ctsio->io_hdr.nexus);
2076 list->list_id = scsi_4btoul(cdb->list_identifier);
2077 list->flags = data->flags;
2078 list->params = ctsio->kern_data_ptr;
2079 list->range = &data->desc[0];
2080 list->nrange = scsi_2btoul(data->range_descriptor_length) /
2081 sizeof(struct scsi_range_desc);
2082 list->offset_into_rod = scsi_8btou64(data->offset_into_rod);
2083 list->ctsio = ctsio;
2085 mtx_lock(&lun->lun_lock);
2086 tlist = tpc_find_list(lun, list->list_id, list->init_idx);
2087 if (tlist != NULL && !tlist->completed) {
2088 mtx_unlock(&lun->lun_lock);
2090 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
2091 /*command*/ 0, /*field*/ 0, /*bit_valid*/ 0,
2095 if (tlist != NULL) {
2096 TAILQ_REMOVE(&lun->tpc_lists, tlist, links);
2099 TAILQ_INSERT_TAIL(&lun->tpc_lists, list, links);
2100 mtx_unlock(&lun->lun_lock);
2102 /* Block device zero ROD token -> no token. */
2103 if (scsi_4btoul(data->rod_token) == ROD_TYPE_BLOCK_ZERO) {
2105 return (CTL_RETVAL_COMPLETE);
2108 mtx_lock(&softc->tpc_lock);
2109 TAILQ_FOREACH(token, &softc->tpc_tokens, links) {
2110 if (memcmp(token->token, data->rod_token,
2111 sizeof(data->rod_token)) == 0)
2114 if (token != NULL) {
2116 list->token = token;
2117 if (data->flags & EC_WUT_DEL_TKN)
2120 mtx_unlock(&softc->tpc_lock);
2121 if (token == NULL) {
2122 mtx_lock(&lun->lun_lock);
2123 TAILQ_REMOVE(&lun->tpc_lists, list, links);
2124 mtx_unlock(&lun->lun_lock);
2126 ctl_set_sense(ctsio, /*current_error*/ 1,
2127 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
2128 /*asc*/ 0x23, /*ascq*/ 0x04, SSD_ELEM_NONE);
2133 return (CTL_RETVAL_COMPLETE);
2136 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
2137 free(ctsio->kern_data_ptr, M_CTL);
2138 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
2140 ctl_done((union ctl_io *)ctsio);
2141 return (CTL_RETVAL_COMPLETE);
2145 ctl_receive_rod_token_information(struct ctl_scsiio *ctsio)
2147 struct ctl_lun *lun;
2148 struct scsi_receive_rod_token_information *cdb;
2149 struct scsi_receive_copy_status_lid4_data *data;
2150 struct tpc_list *list;
2151 struct tpc_list list_copy;
2154 int alloc_len, total_len, token_len;
2157 CTL_DEBUG_PRINT(("ctl_receive_rod_token_information\n"));
2159 cdb = (struct scsi_receive_rod_token_information *)ctsio->cdb;
2160 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
2162 retval = CTL_RETVAL_COMPLETE;
2164 list_id = scsi_4btoul(cdb->list_identifier);
2165 mtx_lock(&lun->lun_lock);
2166 list = tpc_find_list(lun, list_id,
2167 ctl_get_resindex(&ctsio->io_hdr.nexus));
2169 mtx_unlock(&lun->lun_lock);
2170 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
2171 /*command*/ 1, /*field*/ 2, /*bit_valid*/ 0,
2173 ctl_done((union ctl_io *)ctsio);
2177 if (list->completed) {
2178 TAILQ_REMOVE(&lun->tpc_lists, list, links);
2181 mtx_unlock(&lun->lun_lock);
2183 token_len = list_copy.res_token_valid ? 2 + sizeof(list_copy.res_token) : 0;
2184 total_len = sizeof(*data) + list_copy.sense_len + 4 + token_len;
2185 alloc_len = scsi_4btoul(cdb->length);
2187 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
2189 ctsio->kern_sg_entries = 0;
2191 if (total_len < alloc_len) {
2192 ctsio->residual = alloc_len - total_len;
2193 ctsio->kern_data_len = total_len;
2194 ctsio->kern_total_len = total_len;
2196 ctsio->residual = 0;
2197 ctsio->kern_data_len = alloc_len;
2198 ctsio->kern_total_len = alloc_len;
2200 ctsio->kern_data_resid = 0;
2201 ctsio->kern_rel_offset = 0;
2203 data = (struct scsi_receive_copy_status_lid4_data *)ctsio->kern_data_ptr;
2204 scsi_ulto4b(sizeof(*data) - 4 + list_copy.sense_len +
2205 4 + token_len, data->available_data);
2206 data->response_to_service_action = list_copy.service_action;
2207 if (list_copy.completed) {
2208 if (list_copy.error)
2209 data->copy_command_status = RCS_CCS_ERROR;
2210 else if (list_copy.abort)
2211 data->copy_command_status = RCS_CCS_ABORTED;
2213 data->copy_command_status = RCS_CCS_COMPLETED;
2215 data->copy_command_status = RCS_CCS_INPROG_FG;
2216 scsi_ulto2b(list_copy.curops, data->operation_counter);
2217 scsi_ulto4b(UINT32_MAX, data->estimated_status_update_delay);
2218 data->transfer_count_units = RCS_TC_LBAS;
2219 scsi_u64to8b(list_copy.cursectors, data->transfer_count);
2220 scsi_ulto2b(list_copy.curseg, data->segments_processed);
2221 data->length_of_the_sense_data_field = list_copy.sense_len;
2222 data->sense_data_length = list_copy.sense_len;
2223 memcpy(data->sense_data, &list_copy.sense_data, list_copy.sense_len);
2225 ptr = &data->sense_data[data->length_of_the_sense_data_field];
2226 scsi_ulto4b(token_len, &ptr[0]);
2227 if (list_copy.res_token_valid) {
2228 scsi_ulto2b(0, &ptr[4]);
2229 memcpy(&ptr[6], list_copy.res_token, sizeof(list_copy.res_token));
2232 printf("RRTI(list=%u) valid=%d\n",
2233 scsi_4btoul(cdb->list_identifier), list_copy.res_token_valid);
2235 ctl_set_success(ctsio);
2236 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
2237 ctsio->be_move_done = ctl_config_move_done;
2238 ctl_datamove((union ctl_io *)ctsio);
2243 ctl_report_all_rod_tokens(struct ctl_scsiio *ctsio)
2245 struct ctl_softc *softc;
2246 struct ctl_lun *lun;
2247 struct scsi_report_all_rod_tokens *cdb;
2248 struct scsi_report_all_rod_tokens_data *data;
2249 struct tpc_token *token;
2251 int alloc_len, total_len, tokens, i;
2253 CTL_DEBUG_PRINT(("ctl_receive_rod_token_information\n"));
2255 cdb = (struct scsi_report_all_rod_tokens *)ctsio->cdb;
2256 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
2257 softc = lun->ctl_softc;
2259 retval = CTL_RETVAL_COMPLETE;
2262 mtx_lock(&softc->tpc_lock);
2263 TAILQ_FOREACH(token, &softc->tpc_tokens, links)
2265 mtx_unlock(&softc->tpc_lock);
2269 total_len = sizeof(*data) + tokens * 96;
2270 alloc_len = scsi_4btoul(cdb->length);
2272 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
2274 ctsio->kern_sg_entries = 0;
2276 if (total_len < alloc_len) {
2277 ctsio->residual = alloc_len - total_len;
2278 ctsio->kern_data_len = total_len;
2279 ctsio->kern_total_len = total_len;
2281 ctsio->residual = 0;
2282 ctsio->kern_data_len = alloc_len;
2283 ctsio->kern_total_len = alloc_len;
2285 ctsio->kern_data_resid = 0;
2286 ctsio->kern_rel_offset = 0;
2288 data = (struct scsi_report_all_rod_tokens_data *)ctsio->kern_data_ptr;
2290 mtx_lock(&softc->tpc_lock);
2291 TAILQ_FOREACH(token, &softc->tpc_tokens, links) {
2294 memcpy(&data->rod_management_token_list[i * 96],
2298 mtx_unlock(&softc->tpc_lock);
2299 scsi_ulto4b(sizeof(*data) - 4 + i * 96, data->available_data);
2301 printf("RART tokens=%d\n", i);
2303 ctl_set_success(ctsio);
2304 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
2305 ctsio->be_move_done = ctl_config_move_done;
2306 ctl_datamove((union ctl_io *)ctsio);