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