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;
148 extern struct ctl_softc *control_softc;
151 tpc_timeout(void *arg)
153 struct ctl_softc *softc = arg;
155 struct tpc_token *token, *ttoken;
156 struct tpc_list *list, *tlist;
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)
165 TAILQ_REMOVE(&lun->tpc_lists, list, links);
168 mtx_unlock(&lun->lun_lock);
171 /* Free inactive ROD tokens with expired timeout. */
172 TAILQ_FOREACH_SAFE(token, &softc->tpc_tokens, links, ttoken) {
174 time_uptime < token->last_active + token->timeout + 1)
176 TAILQ_REMOVE(&softc->tpc_tokens, token, links);
177 free(token->params, M_CTL);
180 callout_schedule(&softc->tpc_timeout, hz);
184 ctl_tpc_init(struct ctl_softc *softc)
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);
193 ctl_tpc_shutdown(struct ctl_softc *softc)
195 struct tpc_token *token;
197 callout_drain(&softc->tpc_timeout);
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);
206 mtx_unlock(&softc->ctl_lock);
210 ctl_tpc_lun_init(struct ctl_lun *lun)
213 TAILQ_INIT(&lun->tpc_lists);
217 ctl_tpc_lun_shutdown(struct ctl_lun *lun)
219 struct tpc_list *list;
220 struct tpc_token *token, *ttoken;
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));
230 /* Free ROD tokens for this LUN. */
231 mtx_lock(&control_softc->ctl_lock);
232 TAILQ_FOREACH_SAFE(token, &control_softc->tpc_tokens, links, ttoken) {
233 if (token->lun != lun->lun || token->active)
235 TAILQ_REMOVE(&control_softc->tpc_tokens, token, links);
236 free(token->params, M_CTL);
239 mtx_unlock(&control_softc->ctl_lock);
243 ctl_inquiry_evpd_tpc(struct ctl_scsiio *ctsio, int alloc_len)
245 struct scsi_vpd_tpc *tpc_ptr;
246 struct scsi_vpd_tpc_descriptor *d_ptr;
247 struct scsi_vpd_tpc_descriptor_bdrl *bdrl_ptr;
248 struct scsi_vpd_tpc_descriptor_sc *sc_ptr;
249 struct scsi_vpd_tpc_descriptor_sc_descr *scd_ptr;
250 struct scsi_vpd_tpc_descriptor_pd *pd_ptr;
251 struct scsi_vpd_tpc_descriptor_sd *sd_ptr;
252 struct scsi_vpd_tpc_descriptor_sdid *sdid_ptr;
253 struct scsi_vpd_tpc_descriptor_rtf *rtf_ptr;
254 struct scsi_vpd_tpc_descriptor_rtf_block *rtfb_ptr;
255 struct scsi_vpd_tpc_descriptor_srt *srt_ptr;
256 struct scsi_vpd_tpc_descriptor_srtd *srtd_ptr;
257 struct scsi_vpd_tpc_descriptor_gco *gco_ptr;
261 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
263 data_len = sizeof(struct scsi_vpd_tpc) +
264 sizeof(struct scsi_vpd_tpc_descriptor_bdrl) +
265 roundup2(sizeof(struct scsi_vpd_tpc_descriptor_sc) +
266 2 * sizeof(struct scsi_vpd_tpc_descriptor_sc_descr) + 11, 4) +
267 sizeof(struct scsi_vpd_tpc_descriptor_pd) +
268 roundup2(sizeof(struct scsi_vpd_tpc_descriptor_sd) + 4, 4) +
269 roundup2(sizeof(struct scsi_vpd_tpc_descriptor_sdid) + 2, 4) +
270 sizeof(struct scsi_vpd_tpc_descriptor_rtf) +
271 sizeof(struct scsi_vpd_tpc_descriptor_rtf_block) +
272 sizeof(struct scsi_vpd_tpc_descriptor_srt) +
273 2*sizeof(struct scsi_vpd_tpc_descriptor_srtd) +
274 sizeof(struct scsi_vpd_tpc_descriptor_gco);
276 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
277 tpc_ptr = (struct scsi_vpd_tpc *)ctsio->kern_data_ptr;
278 ctsio->kern_sg_entries = 0;
280 if (data_len < alloc_len) {
281 ctsio->residual = alloc_len - data_len;
282 ctsio->kern_data_len = data_len;
283 ctsio->kern_total_len = data_len;
286 ctsio->kern_data_len = alloc_len;
287 ctsio->kern_total_len = alloc_len;
289 ctsio->kern_data_resid = 0;
290 ctsio->kern_rel_offset = 0;
291 ctsio->kern_sg_entries = 0;
294 * The control device is always connected. The disk device, on the
295 * other hand, may not be online all the time.
298 tpc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
299 lun->be_lun->lun_type;
301 tpc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
302 tpc_ptr->page_code = SVPD_SCSI_TPC;
303 scsi_ulto2b(data_len - 4, tpc_ptr->page_length);
305 /* Block Device ROD Limits */
306 d_ptr = (struct scsi_vpd_tpc_descriptor *)&tpc_ptr->descr[0];
307 bdrl_ptr = (struct scsi_vpd_tpc_descriptor_bdrl *)d_ptr;
308 scsi_ulto2b(SVPD_TPC_BDRL, bdrl_ptr->desc_type);
309 scsi_ulto2b(sizeof(*bdrl_ptr) - 4, bdrl_ptr->desc_length);
310 scsi_ulto2b(TPC_MAX_SEGS, bdrl_ptr->maximum_ranges);
311 scsi_ulto4b(TPC_MAX_TOKEN_TIMEOUT,
312 bdrl_ptr->maximum_inactivity_timeout);
313 scsi_ulto4b(TPC_DFL_TOKEN_TIMEOUT,
314 bdrl_ptr->default_inactivity_timeout);
315 scsi_u64to8b(0, bdrl_ptr->maximum_token_transfer_size);
316 scsi_u64to8b(0, bdrl_ptr->optimal_transfer_count);
318 /* Supported commands */
319 d_ptr = (struct scsi_vpd_tpc_descriptor *)
320 (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
321 sc_ptr = (struct scsi_vpd_tpc_descriptor_sc *)d_ptr;
322 scsi_ulto2b(SVPD_TPC_SC, sc_ptr->desc_type);
323 sc_ptr->list_length = 2 * sizeof(*scd_ptr) + 11;
324 scsi_ulto2b(roundup2(1 + sc_ptr->list_length, 4), sc_ptr->desc_length);
325 scd_ptr = &sc_ptr->descr[0];
326 scd_ptr->opcode = EXTENDED_COPY;
327 scd_ptr->sa_length = 5;
328 scd_ptr->supported_service_actions[0] = EC_EC_LID1;
329 scd_ptr->supported_service_actions[1] = EC_EC_LID4;
330 scd_ptr->supported_service_actions[2] = EC_PT;
331 scd_ptr->supported_service_actions[3] = EC_WUT;
332 scd_ptr->supported_service_actions[4] = EC_COA;
333 scd_ptr = (struct scsi_vpd_tpc_descriptor_sc_descr *)
334 &scd_ptr->supported_service_actions[scd_ptr->sa_length];
335 scd_ptr->opcode = RECEIVE_COPY_STATUS;
336 scd_ptr->sa_length = 6;
337 scd_ptr->supported_service_actions[0] = RCS_RCS_LID1;
338 scd_ptr->supported_service_actions[1] = RCS_RCFD;
339 scd_ptr->supported_service_actions[2] = RCS_RCS_LID4;
340 scd_ptr->supported_service_actions[3] = RCS_RCOP;
341 scd_ptr->supported_service_actions[4] = RCS_RRTI;
342 scd_ptr->supported_service_actions[5] = RCS_RART;
344 /* Parameter data. */
345 d_ptr = (struct scsi_vpd_tpc_descriptor *)
346 (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
347 pd_ptr = (struct scsi_vpd_tpc_descriptor_pd *)d_ptr;
348 scsi_ulto2b(SVPD_TPC_PD, pd_ptr->desc_type);
349 scsi_ulto2b(sizeof(*pd_ptr) - 4, pd_ptr->desc_length);
350 scsi_ulto2b(TPC_MAX_CSCDS, pd_ptr->maximum_cscd_descriptor_count);
351 scsi_ulto2b(TPC_MAX_SEGS, pd_ptr->maximum_segment_descriptor_count);
352 scsi_ulto4b(TPC_MAX_LIST, pd_ptr->maximum_descriptor_list_length);
353 scsi_ulto4b(TPC_MAX_INLINE, pd_ptr->maximum_inline_data_length);
355 /* Supported Descriptors */
356 d_ptr = (struct scsi_vpd_tpc_descriptor *)
357 (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
358 sd_ptr = (struct scsi_vpd_tpc_descriptor_sd *)d_ptr;
359 scsi_ulto2b(SVPD_TPC_SD, sd_ptr->desc_type);
360 scsi_ulto2b(roundup2(sizeof(*sd_ptr) - 4 + 4, 4), sd_ptr->desc_length);
361 sd_ptr->list_length = 4;
362 sd_ptr->supported_descriptor_codes[0] = EC_SEG_B2B;
363 sd_ptr->supported_descriptor_codes[1] = EC_SEG_VERIFY;
364 sd_ptr->supported_descriptor_codes[2] = EC_SEG_REGISTER_KEY;
365 sd_ptr->supported_descriptor_codes[3] = EC_CSCD_ID;
367 /* Supported CSCD Descriptor IDs */
368 d_ptr = (struct scsi_vpd_tpc_descriptor *)
369 (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
370 sdid_ptr = (struct scsi_vpd_tpc_descriptor_sdid *)d_ptr;
371 scsi_ulto2b(SVPD_TPC_SDID, sdid_ptr->desc_type);
372 scsi_ulto2b(roundup2(sizeof(*sdid_ptr) - 4 + 2, 4), sdid_ptr->desc_length);
373 scsi_ulto2b(2, sdid_ptr->list_length);
374 scsi_ulto2b(0xffff, &sdid_ptr->supported_descriptor_ids[0]);
376 /* ROD Token Features */
377 d_ptr = (struct scsi_vpd_tpc_descriptor *)
378 (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
379 rtf_ptr = (struct scsi_vpd_tpc_descriptor_rtf *)d_ptr;
380 scsi_ulto2b(SVPD_TPC_RTF, rtf_ptr->desc_type);
381 scsi_ulto2b(sizeof(*rtf_ptr) - 4 + sizeof(*rtfb_ptr), rtf_ptr->desc_length);
382 rtf_ptr->remote_tokens = 0;
383 scsi_ulto4b(TPC_MIN_TOKEN_TIMEOUT, rtf_ptr->minimum_token_lifetime);
384 scsi_ulto4b(UINT32_MAX, rtf_ptr->maximum_token_lifetime);
385 scsi_ulto4b(TPC_MAX_TOKEN_TIMEOUT,
386 rtf_ptr->maximum_token_inactivity_timeout);
387 scsi_ulto2b(sizeof(*rtfb_ptr), rtf_ptr->type_specific_features_length);
388 rtfb_ptr = (struct scsi_vpd_tpc_descriptor_rtf_block *)
389 &rtf_ptr->type_specific_features;
390 rtfb_ptr->type_format = SVPD_TPC_RTF_BLOCK;
391 scsi_ulto2b(sizeof(*rtfb_ptr) - 4, rtfb_ptr->desc_length);
392 scsi_ulto2b(0, rtfb_ptr->optimal_length_granularity);
393 scsi_u64to8b(0, rtfb_ptr->maximum_bytes);
394 scsi_u64to8b(0, rtfb_ptr->optimal_bytes);
395 scsi_u64to8b(TPC_MAX_IOCHUNK_SIZE,
396 rtfb_ptr->optimal_bytes_to_token_per_segment);
397 scsi_u64to8b(TPC_MAX_IOCHUNK_SIZE,
398 rtfb_ptr->optimal_bytes_from_token_per_segment);
400 /* Supported ROD Tokens */
401 d_ptr = (struct scsi_vpd_tpc_descriptor *)
402 (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
403 srt_ptr = (struct scsi_vpd_tpc_descriptor_srt *)d_ptr;
404 scsi_ulto2b(SVPD_TPC_SRT, srt_ptr->desc_type);
405 scsi_ulto2b(sizeof(*srt_ptr) - 4 + 2*sizeof(*srtd_ptr), srt_ptr->desc_length);
406 scsi_ulto2b(2*sizeof(*srtd_ptr), srt_ptr->rod_type_descriptors_length);
407 srtd_ptr = (struct scsi_vpd_tpc_descriptor_srtd *)
408 &srt_ptr->rod_type_descriptors;
409 scsi_ulto4b(ROD_TYPE_AUR, srtd_ptr->rod_type);
410 srtd_ptr->flags = SVPD_TPC_SRTD_TIN | SVPD_TPC_SRTD_TOUT;
411 scsi_ulto2b(0, srtd_ptr->preference_indicator);
413 scsi_ulto4b(ROD_TYPE_BLOCK_ZERO, srtd_ptr->rod_type);
414 srtd_ptr->flags = SVPD_TPC_SRTD_TIN;
415 scsi_ulto2b(0, srtd_ptr->preference_indicator);
417 /* General Copy Operations */
418 d_ptr = (struct scsi_vpd_tpc_descriptor *)
419 (&d_ptr->parameters[0] + scsi_2btoul(d_ptr->desc_length));
420 gco_ptr = (struct scsi_vpd_tpc_descriptor_gco *)d_ptr;
421 scsi_ulto2b(SVPD_TPC_GCO, gco_ptr->desc_type);
422 scsi_ulto2b(sizeof(*gco_ptr) - 4, gco_ptr->desc_length);
423 scsi_ulto4b(TPC_MAX_LISTS, gco_ptr->total_concurrent_copies);
424 scsi_ulto4b(TPC_MAX_LISTS, gco_ptr->maximum_identified_concurrent_copies);
425 scsi_ulto4b(TPC_MAX_SEG, gco_ptr->maximum_segment_length);
426 gco_ptr->data_segment_granularity = 0;
427 gco_ptr->inline_data_granularity = 0;
429 ctsio->scsi_status = SCSI_STATUS_OK;
430 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
431 ctsio->be_move_done = ctl_config_move_done;
432 ctl_datamove((union ctl_io *)ctsio);
434 return (CTL_RETVAL_COMPLETE);
438 ctl_receive_copy_operating_parameters(struct ctl_scsiio *ctsio)
440 struct scsi_receive_copy_operating_parameters *cdb;
441 struct scsi_receive_copy_operating_parameters_data *data;
443 int alloc_len, total_len;
445 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
447 cdb = (struct scsi_receive_copy_operating_parameters *)ctsio->cdb;
449 retval = CTL_RETVAL_COMPLETE;
451 total_len = sizeof(*data) + 4;
452 alloc_len = scsi_4btoul(cdb->length);
454 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
456 ctsio->kern_sg_entries = 0;
458 if (total_len < alloc_len) {
459 ctsio->residual = alloc_len - total_len;
460 ctsio->kern_data_len = total_len;
461 ctsio->kern_total_len = total_len;
464 ctsio->kern_data_len = alloc_len;
465 ctsio->kern_total_len = alloc_len;
467 ctsio->kern_data_resid = 0;
468 ctsio->kern_rel_offset = 0;
470 data = (struct scsi_receive_copy_operating_parameters_data *)ctsio->kern_data_ptr;
471 scsi_ulto4b(sizeof(*data) - 4 + 4, data->length);
472 data->snlid = RCOP_SNLID;
473 scsi_ulto2b(TPC_MAX_CSCDS, data->maximum_cscd_descriptor_count);
474 scsi_ulto2b(TPC_MAX_SEGS, data->maximum_segment_descriptor_count);
475 scsi_ulto4b(TPC_MAX_LIST, data->maximum_descriptor_list_length);
476 scsi_ulto4b(TPC_MAX_SEG, data->maximum_segment_length);
477 scsi_ulto4b(TPC_MAX_INLINE, data->maximum_inline_data_length);
478 scsi_ulto4b(0, data->held_data_limit);
479 scsi_ulto4b(0, data->maximum_stream_device_transfer_size);
480 scsi_ulto2b(TPC_MAX_LISTS, data->total_concurrent_copies);
481 data->maximum_concurrent_copies = TPC_MAX_LISTS;
482 data->data_segment_granularity = 0;
483 data->inline_data_granularity = 0;
484 data->held_data_granularity = 0;
485 data->implemented_descriptor_list_length = 4;
486 data->list_of_implemented_descriptor_type_codes[0] = EC_SEG_B2B;
487 data->list_of_implemented_descriptor_type_codes[1] = EC_SEG_VERIFY;
488 data->list_of_implemented_descriptor_type_codes[2] = EC_SEG_REGISTER_KEY;
489 data->list_of_implemented_descriptor_type_codes[3] = EC_CSCD_ID;
491 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
492 ctsio->be_move_done = ctl_config_move_done;
494 ctl_datamove((union ctl_io *)ctsio);
498 static struct tpc_list *
499 tpc_find_list(struct ctl_lun *lun, uint32_t list_id, uint32_t init_idx)
501 struct tpc_list *list;
503 mtx_assert(&lun->lun_lock, MA_OWNED);
504 TAILQ_FOREACH(list, &lun->tpc_lists, links) {
505 if ((list->flags & EC_LIST_ID_USAGE_MASK) !=
506 EC_LIST_ID_USAGE_NONE && list->list_id == list_id &&
507 list->init_idx == init_idx)
514 ctl_receive_copy_status_lid1(struct ctl_scsiio *ctsio)
517 struct scsi_receive_copy_status_lid1 *cdb;
518 struct scsi_receive_copy_status_lid1_data *data;
519 struct tpc_list *list;
520 struct tpc_list list_copy;
522 int alloc_len, total_len;
525 CTL_DEBUG_PRINT(("ctl_receive_copy_status_lid1\n"));
527 cdb = (struct scsi_receive_copy_status_lid1 *)ctsio->cdb;
528 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
530 retval = CTL_RETVAL_COMPLETE;
532 list_id = cdb->list_identifier;
533 mtx_lock(&lun->lun_lock);
534 list = tpc_find_list(lun, list_id,
535 ctl_get_resindex(&ctsio->io_hdr.nexus));
537 mtx_unlock(&lun->lun_lock);
538 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
539 /*command*/ 1, /*field*/ 2, /*bit_valid*/ 0,
541 ctl_done((union ctl_io *)ctsio);
545 if (list->completed) {
546 TAILQ_REMOVE(&lun->tpc_lists, list, links);
549 mtx_unlock(&lun->lun_lock);
551 total_len = sizeof(*data);
552 alloc_len = scsi_4btoul(cdb->length);
554 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
556 ctsio->kern_sg_entries = 0;
558 if (total_len < alloc_len) {
559 ctsio->residual = alloc_len - total_len;
560 ctsio->kern_data_len = total_len;
561 ctsio->kern_total_len = total_len;
564 ctsio->kern_data_len = alloc_len;
565 ctsio->kern_total_len = alloc_len;
567 ctsio->kern_data_resid = 0;
568 ctsio->kern_rel_offset = 0;
570 data = (struct scsi_receive_copy_status_lid1_data *)ctsio->kern_data_ptr;
571 scsi_ulto4b(sizeof(*data) - 4, data->available_data);
572 if (list_copy.completed) {
573 if (list_copy.error || list_copy.abort)
574 data->copy_command_status = RCS_CCS_ERROR;
576 data->copy_command_status = RCS_CCS_COMPLETED;
578 data->copy_command_status = RCS_CCS_INPROG;
579 scsi_ulto2b(list_copy.curseg, data->segments_processed);
580 if (list_copy.curbytes <= UINT32_MAX) {
581 data->transfer_count_units = RCS_TC_BYTES;
582 scsi_ulto4b(list_copy.curbytes, data->transfer_count);
584 data->transfer_count_units = RCS_TC_MBYTES;
585 scsi_ulto4b(list_copy.curbytes >> 20, data->transfer_count);
588 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
589 ctsio->be_move_done = ctl_config_move_done;
591 ctl_datamove((union ctl_io *)ctsio);
596 ctl_receive_copy_failure_details(struct ctl_scsiio *ctsio)
599 struct scsi_receive_copy_failure_details *cdb;
600 struct scsi_receive_copy_failure_details_data *data;
601 struct tpc_list *list;
602 struct tpc_list list_copy;
604 int alloc_len, total_len;
607 CTL_DEBUG_PRINT(("ctl_receive_copy_failure_details\n"));
609 cdb = (struct scsi_receive_copy_failure_details *)ctsio->cdb;
610 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
612 retval = CTL_RETVAL_COMPLETE;
614 list_id = cdb->list_identifier;
615 mtx_lock(&lun->lun_lock);
616 list = tpc_find_list(lun, list_id,
617 ctl_get_resindex(&ctsio->io_hdr.nexus));
618 if (list == NULL || !list->completed) {
619 mtx_unlock(&lun->lun_lock);
620 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
621 /*command*/ 1, /*field*/ 2, /*bit_valid*/ 0,
623 ctl_done((union ctl_io *)ctsio);
627 TAILQ_REMOVE(&lun->tpc_lists, list, links);
629 mtx_unlock(&lun->lun_lock);
631 total_len = sizeof(*data) + list_copy.sense_len;
632 alloc_len = scsi_4btoul(cdb->length);
634 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
636 ctsio->kern_sg_entries = 0;
638 if (total_len < alloc_len) {
639 ctsio->residual = alloc_len - total_len;
640 ctsio->kern_data_len = total_len;
641 ctsio->kern_total_len = total_len;
644 ctsio->kern_data_len = alloc_len;
645 ctsio->kern_total_len = alloc_len;
647 ctsio->kern_data_resid = 0;
648 ctsio->kern_rel_offset = 0;
650 data = (struct scsi_receive_copy_failure_details_data *)ctsio->kern_data_ptr;
651 if (list_copy.completed && (list_copy.error || list_copy.abort)) {
652 scsi_ulto4b(sizeof(*data) - 4 + list_copy.sense_len,
653 data->available_data);
654 data->copy_command_status = RCS_CCS_ERROR;
656 scsi_ulto4b(0, data->available_data);
657 scsi_ulto2b(list_copy.sense_len, data->sense_data_length);
658 memcpy(data->sense_data, &list_copy.sense_data, list_copy.sense_len);
660 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
661 ctsio->be_move_done = ctl_config_move_done;
663 ctl_datamove((union ctl_io *)ctsio);
668 ctl_receive_copy_status_lid4(struct ctl_scsiio *ctsio)
671 struct scsi_receive_copy_status_lid4 *cdb;
672 struct scsi_receive_copy_status_lid4_data *data;
673 struct tpc_list *list;
674 struct tpc_list list_copy;
676 int alloc_len, total_len;
679 CTL_DEBUG_PRINT(("ctl_receive_copy_status_lid4\n"));
681 cdb = (struct scsi_receive_copy_status_lid4 *)ctsio->cdb;
682 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
684 retval = CTL_RETVAL_COMPLETE;
686 list_id = scsi_4btoul(cdb->list_identifier);
687 mtx_lock(&lun->lun_lock);
688 list = tpc_find_list(lun, list_id,
689 ctl_get_resindex(&ctsio->io_hdr.nexus));
691 mtx_unlock(&lun->lun_lock);
692 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
693 /*command*/ 1, /*field*/ 2, /*bit_valid*/ 0,
695 ctl_done((union ctl_io *)ctsio);
699 if (list->completed) {
700 TAILQ_REMOVE(&lun->tpc_lists, list, links);
703 mtx_unlock(&lun->lun_lock);
705 total_len = sizeof(*data) + list_copy.sense_len;
706 alloc_len = scsi_4btoul(cdb->length);
708 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
710 ctsio->kern_sg_entries = 0;
712 if (total_len < alloc_len) {
713 ctsio->residual = alloc_len - total_len;
714 ctsio->kern_data_len = total_len;
715 ctsio->kern_total_len = total_len;
718 ctsio->kern_data_len = alloc_len;
719 ctsio->kern_total_len = alloc_len;
721 ctsio->kern_data_resid = 0;
722 ctsio->kern_rel_offset = 0;
724 data = (struct scsi_receive_copy_status_lid4_data *)ctsio->kern_data_ptr;
725 scsi_ulto4b(sizeof(*data) - 4 + list_copy.sense_len,
726 data->available_data);
727 data->response_to_service_action = list_copy.service_action;
728 if (list_copy.completed) {
730 data->copy_command_status = RCS_CCS_ERROR;
731 else if (list_copy.abort)
732 data->copy_command_status = RCS_CCS_ABORTED;
734 data->copy_command_status = RCS_CCS_COMPLETED;
736 data->copy_command_status = RCS_CCS_INPROG_FG;
737 scsi_ulto2b(list_copy.curops, data->operation_counter);
738 scsi_ulto4b(UINT32_MAX, data->estimated_status_update_delay);
739 data->transfer_count_units = RCS_TC_BYTES;
740 scsi_u64to8b(list_copy.curbytes, data->transfer_count);
741 scsi_ulto2b(list_copy.curseg, data->segments_processed);
742 data->length_of_the_sense_data_field = list_copy.sense_len;
743 data->sense_data_length = list_copy.sense_len;
744 memcpy(data->sense_data, &list_copy.sense_data, list_copy.sense_len);
746 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
747 ctsio->be_move_done = ctl_config_move_done;
749 ctl_datamove((union ctl_io *)ctsio);
754 ctl_copy_operation_abort(struct ctl_scsiio *ctsio)
757 struct scsi_copy_operation_abort *cdb;
758 struct tpc_list *list;
762 CTL_DEBUG_PRINT(("ctl_copy_operation_abort\n"));
764 cdb = (struct scsi_copy_operation_abort *)ctsio->cdb;
765 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
767 retval = CTL_RETVAL_COMPLETE;
769 list_id = scsi_4btoul(cdb->list_identifier);
770 mtx_lock(&lun->lun_lock);
771 list = tpc_find_list(lun, list_id,
772 ctl_get_resindex(&ctsio->io_hdr.nexus));
774 mtx_unlock(&lun->lun_lock);
775 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
776 /*command*/ 1, /*field*/ 2, /*bit_valid*/ 0,
778 ctl_done((union ctl_io *)ctsio);
782 mtx_unlock(&lun->lun_lock);
784 ctl_set_success(ctsio);
785 ctl_done((union ctl_io *)ctsio);
790 tpc_resolve(struct tpc_list *list, uint16_t idx, uint32_t *ss)
794 if (ss && list->lun->be_lun)
795 *ss = list->lun->be_lun->blocksize;
796 return (list->lun->lun);
798 if (idx >= list->ncscd)
800 return (tpcl_resolve(list->init_port, &list->cscd[idx], ss));
804 tpc_process_b2b(struct tpc_list *list)
806 struct scsi_ec_segment_b2b *seg;
807 struct scsi_ec_cscd_dtsp *sdstp, *ddstp;
808 struct tpc_io *tior, *tiow;
809 struct runl run, *prun;
811 off_t srclba, dstlba, numbytes, donebytes, roundbytes;
813 uint32_t srcblock, dstblock;
815 if (list->stage == 1) {
817 while ((tior = TAILQ_FIRST(&list->allio)) != NULL) {
818 TAILQ_REMOVE(&list->allio, tior, links);
819 ctl_free_io(tior->io);
822 free(list->buf, M_CTL);
824 ctl_set_task_aborted(list->ctsio);
825 return (CTL_RETVAL_ERROR);
826 } else if (list->error) {
827 ctl_set_sense(list->ctsio, /*current_error*/ 1,
828 /*sense_key*/ SSD_KEY_COPY_ABORTED,
829 /*asc*/ 0x0d, /*ascq*/ 0x01, SSD_ELEM_NONE);
830 return (CTL_RETVAL_ERROR);
832 list->cursectors += list->segsectors;
833 list->curbytes += list->segbytes;
834 return (CTL_RETVAL_COMPLETE);
837 TAILQ_INIT(&list->allio);
838 seg = (struct scsi_ec_segment_b2b *)list->seg[list->curseg];
839 sl = tpc_resolve(list, scsi_2btoul(seg->src_cscd), &srcblock);
840 dl = tpc_resolve(list, scsi_2btoul(seg->dst_cscd), &dstblock);
841 if (sl >= CTL_MAX_LUNS || dl >= CTL_MAX_LUNS) {
842 ctl_set_sense(list->ctsio, /*current_error*/ 1,
843 /*sense_key*/ SSD_KEY_COPY_ABORTED,
844 /*asc*/ 0x08, /*ascq*/ 0x04, SSD_ELEM_NONE);
845 return (CTL_RETVAL_ERROR);
847 sdstp = &list->cscd[scsi_2btoul(seg->src_cscd)].dtsp;
848 if (scsi_3btoul(sdstp->block_length) != 0)
849 srcblock = scsi_3btoul(sdstp->block_length);
850 ddstp = &list->cscd[scsi_2btoul(seg->dst_cscd)].dtsp;
851 if (scsi_3btoul(ddstp->block_length) != 0)
852 dstblock = scsi_3btoul(ddstp->block_length);
853 numlba = scsi_2btoul(seg->number_of_blocks);
854 if (seg->flags & EC_SEG_DC)
855 numbytes = (off_t)numlba * dstblock;
857 numbytes = (off_t)numlba * srcblock;
858 srclba = scsi_8btou64(seg->src_lba);
859 dstlba = scsi_8btou64(seg->dst_lba);
861 // printf("Copy %ju bytes from %ju @ %ju to %ju @ %ju\n",
862 // (uintmax_t)numbytes, sl, scsi_8btou64(seg->src_lba),
863 // dl, scsi_8btou64(seg->dst_lba));
866 return (CTL_RETVAL_COMPLETE);
868 if (numbytes % srcblock != 0 || numbytes % dstblock != 0) {
869 ctl_set_sense(list->ctsio, /*current_error*/ 1,
870 /*sense_key*/ SSD_KEY_COPY_ABORTED,
871 /*asc*/ 0x26, /*ascq*/ 0x0A, SSD_ELEM_NONE);
872 return (CTL_RETVAL_ERROR);
875 list->buf = malloc(numbytes, M_CTL, M_WAITOK);
876 list->segbytes = numbytes;
877 list->segsectors = numbytes / dstblock;
882 while (donebytes < numbytes) {
883 roundbytes = MIN(numbytes - donebytes, TPC_MAX_IO_SIZE);
885 tior = malloc(sizeof(*tior), M_CTL, M_WAITOK | M_ZERO);
886 TAILQ_INIT(&tior->run);
888 TAILQ_INSERT_TAIL(&list->allio, tior, links);
889 tior->io = tpcl_alloc_io();
890 if (tior->io == NULL) {
894 ctl_scsi_read_write(tior->io,
895 /*data_ptr*/ &list->buf[donebytes],
896 /*data_len*/ roundbytes,
899 /*minimum_cdb_size*/ 0,
900 /*lba*/ srclba + donebytes / srcblock,
901 /*num_blocks*/ roundbytes / srcblock,
902 /*tag_type*/ CTL_TAG_SIMPLE,
904 tior->io->io_hdr.retries = 3;
906 tior->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tior;
908 tiow = malloc(sizeof(*tior), M_CTL, M_WAITOK | M_ZERO);
909 TAILQ_INIT(&tiow->run);
911 TAILQ_INSERT_TAIL(&list->allio, tiow, links);
912 tiow->io = tpcl_alloc_io();
913 if (tiow->io == NULL) {
917 ctl_scsi_read_write(tiow->io,
918 /*data_ptr*/ &list->buf[donebytes],
919 /*data_len*/ roundbytes,
922 /*minimum_cdb_size*/ 0,
923 /*lba*/ dstlba + donebytes / dstblock,
924 /*num_blocks*/ roundbytes / dstblock,
925 /*tag_type*/ CTL_TAG_SIMPLE,
927 tiow->io->io_hdr.retries = 3;
929 tiow->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tior;
931 TAILQ_INSERT_TAIL(&tior->run, tiow, rlinks);
932 TAILQ_INSERT_TAIL(prun, tior, rlinks);
934 donebytes += roundbytes;
937 while ((tior = TAILQ_FIRST(&run)) != NULL) {
938 TAILQ_REMOVE(&run, tior, rlinks);
939 if (tpcl_queue(tior->io, tior->lun) != CTL_RETVAL_COMPLETE)
940 panic("tpcl_queue() error");
944 return (CTL_RETVAL_QUEUED);
948 tpc_process_verify(struct tpc_list *list)
950 struct scsi_ec_segment_verify *seg;
954 if (list->stage == 1) {
956 while ((tio = TAILQ_FIRST(&list->allio)) != NULL) {
957 TAILQ_REMOVE(&list->allio, tio, links);
958 ctl_free_io(tio->io);
962 ctl_set_task_aborted(list->ctsio);
963 return (CTL_RETVAL_ERROR);
964 } else if (list->error) {
965 ctl_set_sense(list->ctsio, /*current_error*/ 1,
966 /*sense_key*/ SSD_KEY_COPY_ABORTED,
967 /*asc*/ 0x0d, /*ascq*/ 0x01, SSD_ELEM_NONE);
968 return (CTL_RETVAL_ERROR);
970 return (CTL_RETVAL_COMPLETE);
973 TAILQ_INIT(&list->allio);
974 seg = (struct scsi_ec_segment_verify *)list->seg[list->curseg];
975 sl = tpc_resolve(list, scsi_2btoul(seg->src_cscd), NULL);
976 if (sl >= CTL_MAX_LUNS) {
977 ctl_set_sense(list->ctsio, /*current_error*/ 1,
978 /*sense_key*/ SSD_KEY_COPY_ABORTED,
979 /*asc*/ 0x08, /*ascq*/ 0x04, SSD_ELEM_NONE);
980 return (CTL_RETVAL_ERROR);
983 // printf("Verify %ju\n", sl);
985 if ((seg->tur & 0x01) == 0)
986 return (CTL_RETVAL_COMPLETE);
989 tio = malloc(sizeof(*tio), M_CTL, M_WAITOK | M_ZERO);
990 TAILQ_INIT(&tio->run);
992 TAILQ_INSERT_TAIL(&list->allio, tio, links);
993 tio->io = tpcl_alloc_io();
994 if (tio->io == NULL) {
998 ctl_scsi_tur(tio->io, /*tag_type*/ CTL_TAG_SIMPLE, /*control*/ 0);
999 tio->io->io_hdr.retries = 3;
1001 tio->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tio;
1003 if (tpcl_queue(tio->io, tio->lun) != CTL_RETVAL_COMPLETE)
1004 panic("tpcl_queue() error");
1005 return (CTL_RETVAL_QUEUED);
1009 tpc_process_register_key(struct tpc_list *list)
1011 struct scsi_ec_segment_register_key *seg;
1016 if (list->stage == 1) {
1018 while ((tio = TAILQ_FIRST(&list->allio)) != NULL) {
1019 TAILQ_REMOVE(&list->allio, tio, links);
1020 ctl_free_io(tio->io);
1023 free(list->buf, M_CTL);
1025 ctl_set_task_aborted(list->ctsio);
1026 return (CTL_RETVAL_ERROR);
1027 } else if (list->error) {
1028 ctl_set_sense(list->ctsio, /*current_error*/ 1,
1029 /*sense_key*/ SSD_KEY_COPY_ABORTED,
1030 /*asc*/ 0x0d, /*ascq*/ 0x01, SSD_ELEM_NONE);
1031 return (CTL_RETVAL_ERROR);
1033 return (CTL_RETVAL_COMPLETE);
1036 TAILQ_INIT(&list->allio);
1037 seg = (struct scsi_ec_segment_register_key *)list->seg[list->curseg];
1038 dl = tpc_resolve(list, scsi_2btoul(seg->dst_cscd), NULL);
1039 if (dl >= CTL_MAX_LUNS) {
1040 ctl_set_sense(list->ctsio, /*current_error*/ 1,
1041 /*sense_key*/ SSD_KEY_COPY_ABORTED,
1042 /*asc*/ 0x08, /*ascq*/ 0x04, SSD_ELEM_NONE);
1043 return (CTL_RETVAL_ERROR);
1046 // printf("Register Key %ju\n", dl);
1049 tio = malloc(sizeof(*tio), M_CTL, M_WAITOK | M_ZERO);
1050 TAILQ_INIT(&tio->run);
1052 TAILQ_INSERT_TAIL(&list->allio, tio, links);
1053 tio->io = tpcl_alloc_io();
1054 if (tio->io == NULL) {
1058 datalen = sizeof(struct scsi_per_res_out_parms);
1059 list->buf = malloc(datalen, M_CTL, M_WAITOK);
1060 ctl_scsi_persistent_res_out(tio->io,
1061 list->buf, datalen, SPRO_REGISTER, -1,
1062 scsi_8btou64(seg->res_key), scsi_8btou64(seg->sa_res_key),
1063 /*tag_type*/ CTL_TAG_SIMPLE, /*control*/ 0);
1064 tio->io->io_hdr.retries = 3;
1066 tio->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tio;
1068 if (tpcl_queue(tio->io, tio->lun) != CTL_RETVAL_COMPLETE)
1069 panic("tpcl_queue() error");
1070 return (CTL_RETVAL_QUEUED);
1074 tpc_ranges_length(struct scsi_range_desc *range, int nrange)
1079 for (r = 0; r < nrange; r++)
1080 length += scsi_4btoul(range[r].length);
1085 tpc_skip_ranges(struct scsi_range_desc *range, int nrange, off_t skip,
1086 int *srange, off_t *soffset)
1093 while (r < nrange) {
1094 if (skip - off < scsi_4btoul(range[r].length)) {
1096 *soffset = skip - off;
1099 off += scsi_4btoul(range[r].length);
1106 tpc_process_wut(struct tpc_list *list)
1108 struct tpc_io *tio, *tior, *tiow;
1109 struct runl run, *prun;
1111 off_t doffset, soffset;
1112 off_t srclba, dstlba, numbytes, donebytes, roundbytes;
1113 uint32_t srcblock, dstblock;
1115 if (list->stage > 0) {
1117 /* Cleanup after previous rounds. */
1118 while ((tio = TAILQ_FIRST(&list->allio)) != NULL) {
1119 TAILQ_REMOVE(&list->allio, tio, links);
1120 ctl_free_io(tio->io);
1123 free(list->buf, M_CTL);
1125 ctl_set_task_aborted(list->ctsio);
1126 return (CTL_RETVAL_ERROR);
1127 } else if (list->error) {
1128 ctl_set_sense(list->ctsio, /*current_error*/ 1,
1129 /*sense_key*/ SSD_KEY_COPY_ABORTED,
1130 /*asc*/ 0x0d, /*ascq*/ 0x01, SSD_ELEM_NONE);
1131 return (CTL_RETVAL_ERROR);
1133 list->cursectors += list->segsectors;
1134 list->curbytes += list->segbytes;
1137 /* Check where we are on destination ranges list. */
1138 if (tpc_skip_ranges(list->range, list->nrange, list->cursectors,
1139 &drange, &doffset) != 0)
1140 return (CTL_RETVAL_COMPLETE);
1141 dstblock = list->lun->be_lun->blocksize;
1143 /* Check where we are on source ranges list. */
1144 srcblock = list->token->blocksize;
1145 if (tpc_skip_ranges(list->token->range, list->token->nrange,
1146 list->offset_into_rod + list->cursectors * dstblock / srcblock,
1147 &srange, &soffset) != 0) {
1148 ctl_set_sense(list->ctsio, /*current_error*/ 1,
1149 /*sense_key*/ SSD_KEY_COPY_ABORTED,
1150 /*asc*/ 0x0d, /*ascq*/ 0x04, SSD_ELEM_NONE);
1151 return (CTL_RETVAL_ERROR);
1154 srclba = scsi_8btou64(list->token->range[srange].lba) + soffset;
1155 numbytes = srcblock * omin(TPC_MAX_IOCHUNK_SIZE / srcblock,
1156 (scsi_4btoul(list->token->range[srange].length) - soffset));
1157 dstlba = scsi_8btou64(list->range[drange].lba) + doffset;
1158 numbytes = omin(numbytes,
1159 dstblock * omin(TPC_MAX_IOCHUNK_SIZE / dstblock,
1160 (scsi_4btoul(list->range[drange].length) - doffset)));
1162 if (numbytes % srcblock != 0 || numbytes % dstblock != 0) {
1163 ctl_set_sense(list->ctsio, /*current_error*/ 1,
1164 /*sense_key*/ SSD_KEY_COPY_ABORTED,
1165 /*asc*/ 0x26, /*ascq*/ 0x0A, SSD_ELEM_NONE);
1166 return (CTL_RETVAL_ERROR);
1169 list->buf = malloc(numbytes, M_CTL, M_WAITOK |
1170 (list->token == NULL ? M_ZERO : 0));
1171 list->segbytes = numbytes;
1172 list->segsectors = numbytes / dstblock;
1173 //printf("Copy chunk of %ju sectors from %ju to %ju\n", list->segsectors,
1179 TAILQ_INIT(&list->allio);
1180 while (donebytes < numbytes) {
1181 roundbytes = MIN(numbytes - donebytes, TPC_MAX_IO_SIZE);
1183 tior = malloc(sizeof(*tior), M_CTL, M_WAITOK | M_ZERO);
1184 TAILQ_INIT(&tior->run);
1186 TAILQ_INSERT_TAIL(&list->allio, tior, links);
1187 tior->io = tpcl_alloc_io();
1188 if (tior->io == NULL) {
1192 ctl_scsi_read_write(tior->io,
1193 /*data_ptr*/ &list->buf[donebytes],
1194 /*data_len*/ roundbytes,
1197 /*minimum_cdb_size*/ 0,
1198 /*lba*/ srclba + donebytes / srcblock,
1199 /*num_blocks*/ roundbytes / srcblock,
1200 /*tag_type*/ CTL_TAG_SIMPLE,
1202 tior->io->io_hdr.retries = 3;
1203 tior->lun = list->token->lun;
1204 tior->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tior;
1206 tiow = malloc(sizeof(*tiow), M_CTL, M_WAITOK | M_ZERO);
1207 TAILQ_INIT(&tiow->run);
1209 TAILQ_INSERT_TAIL(&list->allio, tiow, links);
1210 tiow->io = tpcl_alloc_io();
1211 if (tiow->io == NULL) {
1215 ctl_scsi_read_write(tiow->io,
1216 /*data_ptr*/ &list->buf[donebytes],
1217 /*data_len*/ roundbytes,
1220 /*minimum_cdb_size*/ 0,
1221 /*lba*/ dstlba + donebytes / dstblock,
1222 /*num_blocks*/ roundbytes / dstblock,
1223 /*tag_type*/ CTL_TAG_SIMPLE,
1225 tiow->io->io_hdr.retries = 3;
1226 tiow->lun = list->lun->lun;
1227 tiow->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tiow;
1229 TAILQ_INSERT_TAIL(&tior->run, tiow, rlinks);
1230 TAILQ_INSERT_TAIL(prun, tior, rlinks);
1232 donebytes += roundbytes;
1235 while ((tior = TAILQ_FIRST(&run)) != NULL) {
1236 TAILQ_REMOVE(&run, tior, rlinks);
1237 if (tpcl_queue(tior->io, tior->lun) != CTL_RETVAL_COMPLETE)
1238 panic("tpcl_queue() error");
1242 return (CTL_RETVAL_QUEUED);
1246 tpc_process_zero_wut(struct tpc_list *list)
1248 struct tpc_io *tio, *tiow;
1249 struct runl run, *prun;
1251 uint32_t dstblock, len;
1253 if (list->stage > 0) {
1255 /* Cleanup after previous rounds. */
1256 while ((tio = TAILQ_FIRST(&list->allio)) != NULL) {
1257 TAILQ_REMOVE(&list->allio, tio, links);
1258 ctl_free_io(tio->io);
1261 free(list->buf, M_CTL);
1263 ctl_set_task_aborted(list->ctsio);
1264 return (CTL_RETVAL_ERROR);
1265 } else if (list->error) {
1266 ctl_set_sense(list->ctsio, /*current_error*/ 1,
1267 /*sense_key*/ SSD_KEY_COPY_ABORTED,
1268 /*asc*/ 0x0d, /*ascq*/ 0x01, SSD_ELEM_NONE);
1269 return (CTL_RETVAL_ERROR);
1271 list->cursectors += list->segsectors;
1272 list->curbytes += list->segbytes;
1273 return (CTL_RETVAL_COMPLETE);
1276 dstblock = list->lun->be_lun->blocksize;
1277 list->buf = malloc(dstblock, M_CTL, M_WAITOK | M_ZERO);
1281 TAILQ_INIT(&list->allio);
1282 list->segsectors = 0;
1283 for (r = 0; r < list->nrange; r++) {
1284 len = scsi_4btoul(list->range[r].length);
1288 tiow = malloc(sizeof(*tiow), M_CTL, M_WAITOK | M_ZERO);
1289 TAILQ_INIT(&tiow->run);
1291 TAILQ_INSERT_TAIL(&list->allio, tiow, links);
1292 tiow->io = tpcl_alloc_io();
1293 if (tiow->io == NULL) {
1297 ctl_scsi_write_same(tiow->io,
1298 /*data_ptr*/ list->buf,
1299 /*data_len*/ dstblock,
1301 /*lba*/ scsi_8btou64(list->range[r].lba),
1303 /*tag_type*/ CTL_TAG_SIMPLE,
1305 tiow->io->io_hdr.retries = 3;
1306 tiow->lun = list->lun->lun;
1307 tiow->io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = tiow;
1309 TAILQ_INSERT_TAIL(prun, tiow, rlinks);
1311 list->segsectors += len;
1313 list->segbytes = list->segsectors * dstblock;
1315 if (TAILQ_EMPTY(&run))
1318 while ((tiow = TAILQ_FIRST(&run)) != NULL) {
1319 TAILQ_REMOVE(&run, tiow, rlinks);
1320 if (tpcl_queue(tiow->io, tiow->lun) != CTL_RETVAL_COMPLETE)
1321 panic("tpcl_queue() error");
1325 return (CTL_RETVAL_QUEUED);
1329 tpc_process(struct tpc_list *list)
1331 struct ctl_lun *lun = list->lun;
1332 struct scsi_ec_segment *seg;
1333 struct ctl_scsiio *ctsio = list->ctsio;
1334 int retval = CTL_RETVAL_COMPLETE;
1336 if (list->service_action == EC_WUT) {
1337 if (list->token != NULL)
1338 retval = tpc_process_wut(list);
1340 retval = tpc_process_zero_wut(list);
1341 if (retval == CTL_RETVAL_QUEUED)
1343 if (retval == CTL_RETVAL_ERROR) {
1348 //printf("ZZZ %d cscd, %d segs\n", list->ncscd, list->nseg);
1349 while (list->curseg < list->nseg) {
1350 seg = list->seg[list->curseg];
1351 switch (seg->type_code) {
1353 retval = tpc_process_b2b(list);
1356 retval = tpc_process_verify(list);
1358 case EC_SEG_REGISTER_KEY:
1359 retval = tpc_process_register_key(list);
1362 ctl_set_sense(ctsio, /*current_error*/ 1,
1363 /*sense_key*/ SSD_KEY_COPY_ABORTED,
1364 /*asc*/ 0x26, /*ascq*/ 0x09, SSD_ELEM_NONE);
1367 if (retval == CTL_RETVAL_QUEUED)
1369 if (retval == CTL_RETVAL_ERROR) {
1378 ctl_set_success(ctsio);
1381 //printf("ZZZ done\n");
1382 free(list->params, M_CTL);
1383 list->params = NULL;
1385 mtx_lock(&control_softc->ctl_lock);
1386 if (--list->token->active == 0)
1387 list->token->last_active = time_uptime;
1388 mtx_unlock(&control_softc->ctl_lock);
1391 mtx_lock(&lun->lun_lock);
1392 if ((list->flags & EC_LIST_ID_USAGE_MASK) == EC_LIST_ID_USAGE_NONE) {
1393 TAILQ_REMOVE(&lun->tpc_lists, list, links);
1396 list->completed = 1;
1397 list->last_active = time_uptime;
1398 list->sense_data = ctsio->sense_data;
1399 list->sense_len = ctsio->sense_len;
1400 list->scsi_status = ctsio->scsi_status;
1402 mtx_unlock(&lun->lun_lock);
1404 ctl_done((union ctl_io *)ctsio);
1408 * For any sort of check condition, busy, etc., we just retry. We do not
1409 * decrement the retry count for unit attention type errors. These are
1410 * normal, and we want to save the retry count for "real" errors. Otherwise,
1411 * we could end up with situations where a command will succeed in some
1412 * situations and fail in others, depending on whether a unit attention is
1413 * pending. Also, some of our error recovery actions, most notably the
1414 * LUN reset action, will cause a unit attention.
1416 * We can add more detail here later if necessary.
1418 static tpc_error_action
1419 tpc_checkcond_parse(union ctl_io *io)
1421 tpc_error_action error_action;
1422 int error_code, sense_key, asc, ascq;
1425 * Default to retrying the command.
1427 error_action = TPC_ERR_RETRY;
1429 scsi_extract_sense_len(&io->scsiio.sense_data,
1430 io->scsiio.sense_len,
1437 switch (error_code) {
1438 case SSD_DEFERRED_ERROR:
1439 case SSD_DESC_DEFERRED_ERROR:
1440 error_action |= TPC_ERR_NO_DECREMENT;
1442 case SSD_CURRENT_ERROR:
1443 case SSD_DESC_CURRENT_ERROR:
1445 switch (sense_key) {
1446 case SSD_KEY_UNIT_ATTENTION:
1447 error_action |= TPC_ERR_NO_DECREMENT;
1449 case SSD_KEY_HARDWARE_ERROR:
1451 * This is our generic "something bad happened"
1452 * error code. It often isn't recoverable.
1454 if ((asc == 0x44) && (ascq == 0x00))
1455 error_action = TPC_ERR_FAIL;
1457 case SSD_KEY_NOT_READY:
1459 * If the LUN is powered down, there likely isn't
1460 * much point in retrying right now.
1462 if ((asc == 0x04) && (ascq == 0x02))
1463 error_action = TPC_ERR_FAIL;
1465 * If the LUN is offline, there probably isn't much
1466 * point in retrying, either.
1468 if ((asc == 0x04) && (ascq == 0x03))
1469 error_action = TPC_ERR_FAIL;
1473 return (error_action);
1476 static tpc_error_action
1477 tpc_error_parse(union ctl_io *io)
1479 tpc_error_action error_action = TPC_ERR_RETRY;
1481 switch (io->io_hdr.io_type) {
1483 switch (io->io_hdr.status & CTL_STATUS_MASK) {
1484 case CTL_SCSI_ERROR:
1485 switch (io->scsiio.scsi_status) {
1486 case SCSI_STATUS_CHECK_COND:
1487 error_action = tpc_checkcond_parse(io);
1500 panic("%s: invalid ctl_io type %d\n", __func__,
1501 io->io_hdr.io_type);
1504 return (error_action);
1508 tpc_done(union ctl_io *io)
1510 struct tpc_io *tio, *tior;
1513 * Very minimal retry logic. We basically retry if we got an error
1514 * back, and the retry count is greater than 0. If we ever want
1515 * more sophisticated initiator type behavior, the CAM error
1516 * recovery code in ../common might be helpful.
1518 // if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
1519 // ctl_io_error_print(io, NULL);
1520 tio = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1521 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
1522 && (io->io_hdr.retries > 0)) {
1523 ctl_io_status old_status;
1524 tpc_error_action error_action;
1526 error_action = tpc_error_parse(io);
1527 switch (error_action & TPC_ERR_MASK) {
1532 if ((error_action & TPC_ERR_NO_DECREMENT) == 0)
1533 io->io_hdr.retries--;
1534 old_status = io->io_hdr.status;
1535 io->io_hdr.status = CTL_STATUS_NONE;
1536 io->io_hdr.flags &= ~CTL_FLAG_ABORT;
1537 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1538 if (tpcl_queue(io, tio->lun) != CTL_RETVAL_COMPLETE) {
1539 printf("%s: error returned from ctl_queue()!\n",
1541 io->io_hdr.status = old_status;
1547 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
1548 tio->list->error = 1;
1550 atomic_add_int(&tio->list->curops, 1);
1551 if (!tio->list->error && !tio->list->abort) {
1552 while ((tior = TAILQ_FIRST(&tio->run)) != NULL) {
1553 TAILQ_REMOVE(&tio->run, tior, rlinks);
1554 atomic_add_int(&tio->list->tbdio, 1);
1555 if (tpcl_queue(tior->io, tior->lun) != CTL_RETVAL_COMPLETE)
1556 panic("tpcl_queue() error");
1559 if (atomic_fetchadd_int(&tio->list->tbdio, -1) == 1)
1560 tpc_process(tio->list);
1564 ctl_extended_copy_lid1(struct ctl_scsiio *ctsio)
1566 struct scsi_extended_copy *cdb;
1567 struct scsi_extended_copy_lid1_data *data;
1568 struct ctl_lun *lun;
1569 struct tpc_list *list, *tlist;
1572 int len, off, lencscd, lenseg, leninl, nseg;
1574 CTL_DEBUG_PRINT(("ctl_extended_copy_lid1\n"));
1576 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
1577 cdb = (struct scsi_extended_copy *)ctsio->cdb;
1578 len = scsi_4btoul(cdb->length);
1580 if (len < sizeof(struct scsi_extended_copy_lid1_data) ||
1581 len > sizeof(struct scsi_extended_copy_lid1_data) +
1582 TPC_MAX_LIST + TPC_MAX_INLINE) {
1583 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
1584 /*field*/ 9, /*bit_valid*/ 0, /*bit*/ 0);
1589 * If we've got a kernel request that hasn't been malloced yet,
1590 * malloc it and tell the caller the data buffer is here.
1592 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
1593 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
1594 ctsio->kern_data_len = len;
1595 ctsio->kern_total_len = len;
1596 ctsio->kern_data_resid = 0;
1597 ctsio->kern_rel_offset = 0;
1598 ctsio->kern_sg_entries = 0;
1599 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
1600 ctsio->be_move_done = ctl_config_move_done;
1601 ctl_datamove((union ctl_io *)ctsio);
1603 return (CTL_RETVAL_COMPLETE);
1606 data = (struct scsi_extended_copy_lid1_data *)ctsio->kern_data_ptr;
1607 lencscd = scsi_2btoul(data->cscd_list_length);
1608 lenseg = scsi_4btoul(data->segment_list_length);
1609 leninl = scsi_4btoul(data->inline_data_length);
1610 if (len < sizeof(struct scsi_extended_copy_lid1_data) +
1611 lencscd + lenseg + leninl ||
1612 leninl > TPC_MAX_INLINE) {
1613 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 0,
1614 /*field*/ 2, /*bit_valid*/ 0, /*bit*/ 0);
1617 if (lencscd > TPC_MAX_CSCDS * sizeof(struct scsi_ec_cscd)) {
1618 ctl_set_sense(ctsio, /*current_error*/ 1,
1619 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1620 /*asc*/ 0x26, /*ascq*/ 0x06, SSD_ELEM_NONE);
1623 if (lencscd + lenseg > TPC_MAX_LIST) {
1624 ctl_set_param_len_error(ctsio);
1628 list = malloc(sizeof(struct tpc_list), M_CTL, M_WAITOK | M_ZERO);
1629 list->service_action = cdb->service_action;
1630 value = ctl_get_opt(&lun->be_lun->options, "insecure_tpc");
1631 if (value != NULL && strcmp(value, "on") == 0)
1632 list->init_port = -1;
1634 list->init_port = ctsio->io_hdr.nexus.targ_port;
1635 list->init_idx = ctl_get_resindex(&ctsio->io_hdr.nexus);
1636 list->list_id = data->list_identifier;
1637 list->flags = data->flags;
1638 list->params = ctsio->kern_data_ptr;
1639 list->cscd = (struct scsi_ec_cscd *)&data->data[0];
1640 ptr = &data->data[lencscd];
1641 for (nseg = 0, off = 0; off < lenseg; nseg++) {
1642 if (nseg >= TPC_MAX_SEGS) {
1644 ctl_set_sense(ctsio, /*current_error*/ 1,
1645 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1646 /*asc*/ 0x26, /*ascq*/ 0x08, SSD_ELEM_NONE);
1649 list->seg[nseg] = (struct scsi_ec_segment *)(ptr + off);
1650 off += sizeof(struct scsi_ec_segment) +
1651 scsi_2btoul(list->seg[nseg]->descr_length);
1653 list->inl = &data->data[lencscd + lenseg];
1654 list->ncscd = lencscd / sizeof(struct scsi_ec_cscd);
1656 list->leninl = leninl;
1657 list->ctsio = ctsio;
1659 mtx_lock(&lun->lun_lock);
1660 if ((list->flags & EC_LIST_ID_USAGE_MASK) != EC_LIST_ID_USAGE_NONE) {
1661 tlist = tpc_find_list(lun, list->list_id, list->init_idx);
1662 if (tlist != NULL && !tlist->completed) {
1663 mtx_unlock(&lun->lun_lock);
1665 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
1666 /*command*/ 0, /*field*/ 0, /*bit_valid*/ 0,
1670 if (tlist != NULL) {
1671 TAILQ_REMOVE(&lun->tpc_lists, tlist, links);
1675 TAILQ_INSERT_TAIL(&lun->tpc_lists, list, links);
1676 mtx_unlock(&lun->lun_lock);
1679 return (CTL_RETVAL_COMPLETE);
1682 ctl_done((union ctl_io *)ctsio);
1683 return (CTL_RETVAL_COMPLETE);
1687 ctl_extended_copy_lid4(struct ctl_scsiio *ctsio)
1689 struct scsi_extended_copy *cdb;
1690 struct scsi_extended_copy_lid4_data *data;
1691 struct ctl_lun *lun;
1692 struct tpc_list *list, *tlist;
1695 int len, off, lencscd, lenseg, leninl, nseg;
1697 CTL_DEBUG_PRINT(("ctl_extended_copy_lid4\n"));
1699 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
1700 cdb = (struct scsi_extended_copy *)ctsio->cdb;
1701 len = scsi_4btoul(cdb->length);
1703 if (len < sizeof(struct scsi_extended_copy_lid4_data) ||
1704 len > sizeof(struct scsi_extended_copy_lid4_data) +
1705 TPC_MAX_LIST + TPC_MAX_INLINE) {
1706 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
1707 /*field*/ 9, /*bit_valid*/ 0, /*bit*/ 0);
1712 * If we've got a kernel request that hasn't been malloced yet,
1713 * malloc it and tell the caller the data buffer is here.
1715 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
1716 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
1717 ctsio->kern_data_len = len;
1718 ctsio->kern_total_len = len;
1719 ctsio->kern_data_resid = 0;
1720 ctsio->kern_rel_offset = 0;
1721 ctsio->kern_sg_entries = 0;
1722 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
1723 ctsio->be_move_done = ctl_config_move_done;
1724 ctl_datamove((union ctl_io *)ctsio);
1726 return (CTL_RETVAL_COMPLETE);
1729 data = (struct scsi_extended_copy_lid4_data *)ctsio->kern_data_ptr;
1730 lencscd = scsi_2btoul(data->cscd_list_length);
1731 lenseg = scsi_2btoul(data->segment_list_length);
1732 leninl = scsi_2btoul(data->inline_data_length);
1733 if (len < sizeof(struct scsi_extended_copy_lid4_data) +
1734 lencscd + lenseg + leninl ||
1735 leninl > TPC_MAX_INLINE) {
1736 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 0,
1737 /*field*/ 2, /*bit_valid*/ 0, /*bit*/ 0);
1740 if (lencscd > TPC_MAX_CSCDS * sizeof(struct scsi_ec_cscd)) {
1741 ctl_set_sense(ctsio, /*current_error*/ 1,
1742 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1743 /*asc*/ 0x26, /*ascq*/ 0x06, SSD_ELEM_NONE);
1746 if (lencscd + lenseg > TPC_MAX_LIST) {
1747 ctl_set_param_len_error(ctsio);
1751 list = malloc(sizeof(struct tpc_list), M_CTL, M_WAITOK | M_ZERO);
1752 list->service_action = cdb->service_action;
1753 value = ctl_get_opt(&lun->be_lun->options, "insecure_tpc");
1754 if (value != NULL && strcmp(value, "on") == 0)
1755 list->init_port = -1;
1757 list->init_port = ctsio->io_hdr.nexus.targ_port;
1758 list->init_idx = ctl_get_resindex(&ctsio->io_hdr.nexus);
1759 list->list_id = scsi_4btoul(data->list_identifier);
1760 list->flags = data->flags;
1761 list->params = ctsio->kern_data_ptr;
1762 list->cscd = (struct scsi_ec_cscd *)&data->data[0];
1763 ptr = &data->data[lencscd];
1764 for (nseg = 0, off = 0; off < lenseg; nseg++) {
1765 if (nseg >= TPC_MAX_SEGS) {
1767 ctl_set_sense(ctsio, /*current_error*/ 1,
1768 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1769 /*asc*/ 0x26, /*ascq*/ 0x08, SSD_ELEM_NONE);
1772 list->seg[nseg] = (struct scsi_ec_segment *)(ptr + off);
1773 off += sizeof(struct scsi_ec_segment) +
1774 scsi_2btoul(list->seg[nseg]->descr_length);
1776 list->inl = &data->data[lencscd + lenseg];
1777 list->ncscd = lencscd / sizeof(struct scsi_ec_cscd);
1779 list->leninl = leninl;
1780 list->ctsio = ctsio;
1782 mtx_lock(&lun->lun_lock);
1783 if ((list->flags & EC_LIST_ID_USAGE_MASK) != EC_LIST_ID_USAGE_NONE) {
1784 tlist = tpc_find_list(lun, list->list_id, list->init_idx);
1785 if (tlist != NULL && !tlist->completed) {
1786 mtx_unlock(&lun->lun_lock);
1788 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
1789 /*command*/ 0, /*field*/ 0, /*bit_valid*/ 0,
1793 if (tlist != NULL) {
1794 TAILQ_REMOVE(&lun->tpc_lists, tlist, links);
1798 TAILQ_INSERT_TAIL(&lun->tpc_lists, list, links);
1799 mtx_unlock(&lun->lun_lock);
1802 return (CTL_RETVAL_COMPLETE);
1805 ctl_done((union ctl_io *)ctsio);
1806 return (CTL_RETVAL_COMPLETE);
1810 tpc_create_token(struct ctl_lun *lun, struct ctl_port *port, off_t len,
1811 struct scsi_token *token)
1814 struct scsi_vpd_id_descriptor *idd = NULL;
1817 scsi_ulto4b(ROD_TYPE_AUR, token->type);
1818 scsi_ulto2b(0x01f8, token->length);
1819 scsi_u64to8b(atomic_fetchadd_int(&id, 1), &token->body[0]);
1821 idd = scsi_get_devid_desc((struct scsi_vpd_id_descriptor *)
1822 lun->lun_devid->data, lun->lun_devid->len,
1823 scsi_devid_is_lun_naa);
1824 if (idd == NULL && lun->lun_devid)
1825 idd = scsi_get_devid_desc((struct scsi_vpd_id_descriptor *)
1826 lun->lun_devid->data, lun->lun_devid->len,
1827 scsi_devid_is_lun_eui64);
1829 memcpy(&token->body[8], idd, 4 + idd->length);
1830 scsi_u64to8b(0, &token->body[40]);
1831 scsi_u64to8b(len, &token->body[48]);
1832 if (port->target_devid) {
1833 targid_len = port->target_devid->len;
1834 memcpy(&token->body[120], port->target_devid->data, targid_len);
1837 arc4rand(&token->body[120 + targid_len], 384 - targid_len, 0);
1841 ctl_populate_token(struct ctl_scsiio *ctsio)
1843 struct scsi_populate_token *cdb;
1844 struct scsi_populate_token_data *data;
1845 struct ctl_lun *lun;
1846 struct ctl_port *port;
1847 struct tpc_list *list, *tlist;
1848 struct tpc_token *token;
1851 CTL_DEBUG_PRINT(("ctl_populate_token\n"));
1853 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
1854 port = control_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
1855 cdb = (struct scsi_populate_token *)ctsio->cdb;
1856 len = scsi_4btoul(cdb->length);
1858 if (len < sizeof(struct scsi_populate_token_data) ||
1859 len > sizeof(struct scsi_populate_token_data) +
1860 TPC_MAX_SEGS * sizeof(struct scsi_range_desc)) {
1861 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
1862 /*field*/ 9, /*bit_valid*/ 0, /*bit*/ 0);
1867 * If we've got a kernel request that hasn't been malloced yet,
1868 * malloc it and tell the caller the data buffer is here.
1870 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
1871 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
1872 ctsio->kern_data_len = len;
1873 ctsio->kern_total_len = len;
1874 ctsio->kern_data_resid = 0;
1875 ctsio->kern_rel_offset = 0;
1876 ctsio->kern_sg_entries = 0;
1877 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
1878 ctsio->be_move_done = ctl_config_move_done;
1879 ctl_datamove((union ctl_io *)ctsio);
1881 return (CTL_RETVAL_COMPLETE);
1884 data = (struct scsi_populate_token_data *)ctsio->kern_data_ptr;
1885 lendesc = scsi_2btoul(data->range_descriptor_length);
1886 if (len < sizeof(struct scsi_populate_token_data) + lendesc) {
1887 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 0,
1888 /*field*/ 2, /*bit_valid*/ 0, /*bit*/ 0);
1892 printf("PT(list=%u) flags=%x to=%d rt=%x len=%x\n",
1893 scsi_4btoul(cdb->list_identifier),
1894 data->flags, scsi_4btoul(data->inactivity_timeout),
1895 scsi_4btoul(data->rod_type),
1896 scsi_2btoul(data->range_descriptor_length));
1898 if ((data->flags & EC_PT_RTV) &&
1899 scsi_4btoul(data->rod_type) != ROD_TYPE_AUR) {
1900 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 0,
1901 /*field*/ 8, /*bit_valid*/ 0, /*bit*/ 0);
1905 list = malloc(sizeof(struct tpc_list), M_CTL, M_WAITOK | M_ZERO);
1906 list->service_action = cdb->service_action;
1907 list->init_port = ctsio->io_hdr.nexus.targ_port;
1908 list->init_idx = ctl_get_resindex(&ctsio->io_hdr.nexus);
1909 list->list_id = scsi_4btoul(cdb->list_identifier);
1910 list->flags = data->flags;
1911 list->ctsio = ctsio;
1913 mtx_lock(&lun->lun_lock);
1914 tlist = tpc_find_list(lun, list->list_id, list->init_idx);
1915 if (tlist != NULL && !tlist->completed) {
1916 mtx_unlock(&lun->lun_lock);
1918 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
1919 /*command*/ 0, /*field*/ 0, /*bit_valid*/ 0,
1923 if (tlist != NULL) {
1924 TAILQ_REMOVE(&lun->tpc_lists, tlist, links);
1927 TAILQ_INSERT_TAIL(&lun->tpc_lists, list, links);
1928 mtx_unlock(&lun->lun_lock);
1930 token = malloc(sizeof(*token), M_CTL, M_WAITOK | M_ZERO);
1931 token->lun = lun->lun;
1932 token->blocksize = lun->be_lun->blocksize;
1933 token->params = ctsio->kern_data_ptr;
1934 token->range = &data->desc[0];
1935 token->nrange = scsi_2btoul(data->range_descriptor_length) /
1936 sizeof(struct scsi_range_desc);
1937 tpc_create_token(lun, port, list->curbytes,
1938 (struct scsi_token *)token->token);
1940 token->last_active = time_uptime;
1941 token->timeout = scsi_4btoul(data->inactivity_timeout);
1942 if (token->timeout == 0)
1943 token->timeout = TPC_DFL_TOKEN_TIMEOUT;
1944 else if (token->timeout < TPC_MIN_TOKEN_TIMEOUT)
1945 token->timeout = TPC_MIN_TOKEN_TIMEOUT;
1946 else if (token->timeout > TPC_MAX_TOKEN_TIMEOUT) {
1947 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
1948 /*command*/ 0, /*field*/ 4, /*bit_valid*/ 0,
1951 memcpy(list->res_token, token->token, sizeof(list->res_token));
1952 list->res_token_valid = 1;
1953 list->cursectors = tpc_ranges_length(token->range, token->nrange);
1954 list->curbytes = (off_t)list->cursectors * lun->be_lun->blocksize;
1956 list->completed = 1;
1957 list->last_active = time_uptime;
1958 mtx_lock(&control_softc->ctl_lock);
1959 TAILQ_INSERT_TAIL(&control_softc->tpc_tokens, token, links);
1960 mtx_unlock(&control_softc->ctl_lock);
1961 ctl_set_success(ctsio);
1962 ctl_done((union ctl_io *)ctsio);
1963 return (CTL_RETVAL_COMPLETE);
1966 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED)
1967 free(ctsio->kern_data_ptr, M_CTL);
1968 ctl_done((union ctl_io *)ctsio);
1969 return (CTL_RETVAL_COMPLETE);
1973 ctl_write_using_token(struct ctl_scsiio *ctsio)
1975 struct scsi_write_using_token *cdb;
1976 struct scsi_write_using_token_data *data;
1977 struct ctl_lun *lun;
1978 struct tpc_list *list, *tlist;
1979 struct tpc_token *token;
1982 CTL_DEBUG_PRINT(("ctl_write_using_token\n"));
1984 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
1985 cdb = (struct scsi_write_using_token *)ctsio->cdb;
1986 len = scsi_4btoul(cdb->length);
1988 if (len < sizeof(struct scsi_populate_token_data) ||
1989 len > sizeof(struct scsi_populate_token_data) +
1990 TPC_MAX_SEGS * sizeof(struct scsi_range_desc)) {
1991 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
1992 /*field*/ 9, /*bit_valid*/ 0, /*bit*/ 0);
1997 * If we've got a kernel request that hasn't been malloced yet,
1998 * malloc it and tell the caller the data buffer is here.
2000 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
2001 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
2002 ctsio->kern_data_len = len;
2003 ctsio->kern_total_len = len;
2004 ctsio->kern_data_resid = 0;
2005 ctsio->kern_rel_offset = 0;
2006 ctsio->kern_sg_entries = 0;
2007 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
2008 ctsio->be_move_done = ctl_config_move_done;
2009 ctl_datamove((union ctl_io *)ctsio);
2011 return (CTL_RETVAL_COMPLETE);
2014 data = (struct scsi_write_using_token_data *)ctsio->kern_data_ptr;
2015 lendesc = scsi_2btoul(data->range_descriptor_length);
2016 if (len < sizeof(struct scsi_populate_token_data) + lendesc) {
2017 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 0,
2018 /*field*/ 2, /*bit_valid*/ 0, /*bit*/ 0);
2022 printf("WUT(list=%u) flags=%x off=%ju len=%x\n",
2023 scsi_4btoul(cdb->list_identifier),
2024 data->flags, scsi_8btou64(data->offset_into_rod),
2025 scsi_2btoul(data->range_descriptor_length));
2027 list = malloc(sizeof(struct tpc_list), M_CTL, M_WAITOK | M_ZERO);
2028 list->service_action = cdb->service_action;
2029 list->init_port = ctsio->io_hdr.nexus.targ_port;
2030 list->init_idx = ctl_get_resindex(&ctsio->io_hdr.nexus);
2031 list->list_id = scsi_4btoul(cdb->list_identifier);
2032 list->flags = data->flags;
2033 list->params = ctsio->kern_data_ptr;
2034 list->range = &data->desc[0];
2035 list->nrange = scsi_2btoul(data->range_descriptor_length) /
2036 sizeof(struct scsi_range_desc);
2037 list->offset_into_rod = scsi_8btou64(data->offset_into_rod);
2038 list->ctsio = ctsio;
2040 mtx_lock(&lun->lun_lock);
2041 tlist = tpc_find_list(lun, list->list_id, list->init_idx);
2042 if (tlist != NULL && !tlist->completed) {
2043 mtx_unlock(&lun->lun_lock);
2045 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
2046 /*command*/ 0, /*field*/ 0, /*bit_valid*/ 0,
2050 if (tlist != NULL) {
2051 TAILQ_REMOVE(&lun->tpc_lists, tlist, links);
2054 TAILQ_INSERT_TAIL(&lun->tpc_lists, list, links);
2055 mtx_unlock(&lun->lun_lock);
2057 /* Block device zero ROD token -> no token. */
2058 if (scsi_4btoul(data->rod_token) == ROD_TYPE_BLOCK_ZERO) {
2060 return (CTL_RETVAL_COMPLETE);
2063 mtx_lock(&control_softc->ctl_lock);
2064 TAILQ_FOREACH(token, &control_softc->tpc_tokens, links) {
2065 if (memcmp(token->token, data->rod_token,
2066 sizeof(data->rod_token)) == 0)
2069 if (token != NULL) {
2071 list->token = token;
2072 if (data->flags & EC_WUT_DEL_TKN)
2075 mtx_unlock(&control_softc->ctl_lock);
2076 if (token == NULL) {
2077 mtx_lock(&lun->lun_lock);
2078 TAILQ_REMOVE(&lun->tpc_lists, list, links);
2079 mtx_unlock(&lun->lun_lock);
2081 ctl_set_sense(ctsio, /*current_error*/ 1,
2082 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
2083 /*asc*/ 0x23, /*ascq*/ 0x04, SSD_ELEM_NONE);
2088 return (CTL_RETVAL_COMPLETE);
2091 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED)
2092 free(ctsio->kern_data_ptr, M_CTL);
2093 ctl_done((union ctl_io *)ctsio);
2094 return (CTL_RETVAL_COMPLETE);
2098 ctl_receive_rod_token_information(struct ctl_scsiio *ctsio)
2100 struct ctl_lun *lun;
2101 struct scsi_receive_rod_token_information *cdb;
2102 struct scsi_receive_copy_status_lid4_data *data;
2103 struct tpc_list *list;
2104 struct tpc_list list_copy;
2107 int alloc_len, total_len, token_len;
2110 CTL_DEBUG_PRINT(("ctl_receive_rod_token_information\n"));
2112 cdb = (struct scsi_receive_rod_token_information *)ctsio->cdb;
2113 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
2115 retval = CTL_RETVAL_COMPLETE;
2117 list_id = scsi_4btoul(cdb->list_identifier);
2118 mtx_lock(&lun->lun_lock);
2119 list = tpc_find_list(lun, list_id,
2120 ctl_get_resindex(&ctsio->io_hdr.nexus));
2122 mtx_unlock(&lun->lun_lock);
2123 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
2124 /*command*/ 1, /*field*/ 2, /*bit_valid*/ 0,
2126 ctl_done((union ctl_io *)ctsio);
2130 if (list->completed) {
2131 TAILQ_REMOVE(&lun->tpc_lists, list, links);
2134 mtx_unlock(&lun->lun_lock);
2136 token_len = list_copy.res_token_valid ? 2 + sizeof(list_copy.res_token) : 0;
2137 total_len = sizeof(*data) + list_copy.sense_len + 4 + token_len;
2138 alloc_len = scsi_4btoul(cdb->length);
2140 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
2142 ctsio->kern_sg_entries = 0;
2144 if (total_len < alloc_len) {
2145 ctsio->residual = alloc_len - total_len;
2146 ctsio->kern_data_len = total_len;
2147 ctsio->kern_total_len = total_len;
2149 ctsio->residual = 0;
2150 ctsio->kern_data_len = alloc_len;
2151 ctsio->kern_total_len = alloc_len;
2153 ctsio->kern_data_resid = 0;
2154 ctsio->kern_rel_offset = 0;
2156 data = (struct scsi_receive_copy_status_lid4_data *)ctsio->kern_data_ptr;
2157 scsi_ulto4b(sizeof(*data) - 4 + list_copy.sense_len +
2158 4 + token_len, data->available_data);
2159 data->response_to_service_action = list_copy.service_action;
2160 if (list_copy.completed) {
2161 if (list_copy.error)
2162 data->copy_command_status = RCS_CCS_ERROR;
2163 else if (list_copy.abort)
2164 data->copy_command_status = RCS_CCS_ABORTED;
2166 data->copy_command_status = RCS_CCS_COMPLETED;
2168 data->copy_command_status = RCS_CCS_INPROG_FG;
2169 scsi_ulto2b(list_copy.curops, data->operation_counter);
2170 scsi_ulto4b(UINT32_MAX, data->estimated_status_update_delay);
2171 data->transfer_count_units = RCS_TC_LBAS;
2172 scsi_u64to8b(list_copy.cursectors, data->transfer_count);
2173 scsi_ulto2b(list_copy.curseg, data->segments_processed);
2174 data->length_of_the_sense_data_field = list_copy.sense_len;
2175 data->sense_data_length = list_copy.sense_len;
2176 memcpy(data->sense_data, &list_copy.sense_data, list_copy.sense_len);
2178 ptr = &data->sense_data[data->length_of_the_sense_data_field];
2179 scsi_ulto4b(token_len, &ptr[0]);
2180 if (list_copy.res_token_valid) {
2181 scsi_ulto2b(0, &ptr[4]);
2182 memcpy(&ptr[6], list_copy.res_token, sizeof(list_copy.res_token));
2185 printf("RRTI(list=%u) valid=%d\n",
2186 scsi_4btoul(cdb->list_identifier), list_copy.res_token_valid);
2188 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
2189 ctsio->be_move_done = ctl_config_move_done;
2191 ctl_datamove((union ctl_io *)ctsio);
2196 ctl_report_all_rod_tokens(struct ctl_scsiio *ctsio)
2198 struct ctl_lun *lun;
2199 struct scsi_report_all_rod_tokens *cdb;
2200 struct scsi_report_all_rod_tokens_data *data;
2201 struct tpc_token *token;
2203 int alloc_len, total_len, tokens, i;
2205 CTL_DEBUG_PRINT(("ctl_receive_rod_token_information\n"));
2207 cdb = (struct scsi_report_all_rod_tokens *)ctsio->cdb;
2208 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
2210 retval = CTL_RETVAL_COMPLETE;
2213 mtx_lock(&control_softc->ctl_lock);
2214 TAILQ_FOREACH(token, &control_softc->tpc_tokens, links)
2216 mtx_unlock(&control_softc->ctl_lock);
2220 total_len = sizeof(*data) + tokens * 96;
2221 alloc_len = scsi_4btoul(cdb->length);
2223 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
2225 ctsio->kern_sg_entries = 0;
2227 if (total_len < alloc_len) {
2228 ctsio->residual = alloc_len - total_len;
2229 ctsio->kern_data_len = total_len;
2230 ctsio->kern_total_len = total_len;
2232 ctsio->residual = 0;
2233 ctsio->kern_data_len = alloc_len;
2234 ctsio->kern_total_len = alloc_len;
2236 ctsio->kern_data_resid = 0;
2237 ctsio->kern_rel_offset = 0;
2239 data = (struct scsi_report_all_rod_tokens_data *)ctsio->kern_data_ptr;
2241 mtx_lock(&control_softc->ctl_lock);
2242 TAILQ_FOREACH(token, &control_softc->tpc_tokens, links) {
2245 memcpy(&data->rod_management_token_list[i * 96],
2249 mtx_unlock(&control_softc->ctl_lock);
2250 scsi_ulto4b(sizeof(*data) - 4 + i * 96, data->available_data);
2252 printf("RART tokens=%d\n", i);
2254 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
2255 ctsio->be_move_done = ctl_config_move_done;
2257 ctl_datamove((union ctl_io *)ctsio);