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