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