scsi_cmds.c revision 120428
1/* 2 * SCSI Disk Emulator 3 * 4 * Copyright (c) 2002 Nate Lawson. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions, and the following disclaimer, 12 * without modification, immediately at the beginning of the file. 13 * 2. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD: head/share/examples/scsi_target/scsi_cmds.c 120428 2003-09-25 05:43:26Z simokawa $ 29 */ 30 31#include <stdio.h> 32#include <stddef.h> 33#include <stdarg.h> 34#include <stdlib.h> 35#include <string.h> 36#include <err.h> 37#include <aio.h> 38#include <assert.h> 39#include <sys/types.h> 40 41#include <cam/cam.h> 42#include <cam/cam_ccb.h> 43#include <cam/scsi/scsi_all.h> 44#include <cam/scsi/scsi_targetio.h> 45#include "scsi_target.h" 46 47typedef int targ_start_func(struct ccb_accept_tio *, struct ccb_scsiio *); 48typedef void targ_done_func(struct ccb_accept_tio *, struct ccb_scsiio *, 49 io_ops); 50 51struct targ_cdb_handlers { 52 u_int8_t cmd; 53 targ_start_func *start; 54 targ_done_func *done; 55#define ILLEGAL_CDB 0xFF 56}; 57 58static targ_start_func tcmd_inquiry; 59static targ_start_func tcmd_req_sense; 60static targ_start_func tcmd_rd_cap; 61static targ_start_func tcmd_rdwr; 62static targ_start_func tcmd_rdwr_decode; 63static targ_done_func tcmd_rdwr_done; 64static targ_start_func tcmd_null_ok; 65static targ_start_func tcmd_illegal_req; 66static int start_io(struct ccb_accept_tio *atio, 67 struct ccb_scsiio *ctio, int dir); 68static int init_inquiry(u_int16_t req_flags, u_int16_t sim_flags); 69static struct initiator_state * 70 tcmd_get_istate(u_int init_id); 71static void cdb_debug(u_int8_t *cdb, const char *msg, ...); 72 73static struct targ_cdb_handlers cdb_handlers[] = { 74 { READ_10, tcmd_rdwr, tcmd_rdwr_done }, 75 { WRITE_10, tcmd_rdwr, tcmd_rdwr_done }, 76 { READ_6, tcmd_rdwr, tcmd_rdwr_done }, 77 { WRITE_6, tcmd_rdwr, tcmd_rdwr_done }, 78 { INQUIRY, tcmd_inquiry, NULL }, 79 { REQUEST_SENSE, tcmd_req_sense, NULL }, 80 { READ_CAPACITY, tcmd_rd_cap, NULL }, 81 { TEST_UNIT_READY, tcmd_null_ok, NULL }, 82 { START_STOP_UNIT, tcmd_null_ok, NULL }, 83 { SYNCHRONIZE_CACHE, tcmd_null_ok, NULL }, 84 { MODE_SENSE_6, tcmd_illegal_req, NULL }, 85 { MODE_SELECT_6, tcmd_illegal_req, NULL }, 86 { ILLEGAL_CDB, NULL, NULL } 87}; 88 89static struct scsi_inquiry_data inq_data; 90static struct initiator_state istates[MAX_INITIATORS]; 91extern int debug; 92extern u_int32_t volume_size; 93extern size_t sector_size; 94extern size_t buf_size; 95 96cam_status 97tcmd_init(u_int16_t req_inq_flags, u_int16_t sim_inq_flags) 98{ 99 struct initiator_state *istate; 100 int i, ret; 101 102 /* Initialize our inquiry data */ 103 ret = init_inquiry(req_inq_flags, sim_inq_flags); 104 if (ret != 0) 105 return (ret); 106 107 /* We start out life with a UA to indicate power-on/reset. */ 108 for (i = 0; i < MAX_INITIATORS; i++) { 109 istate = tcmd_get_istate(i); 110 bzero(istate, sizeof(*istate)); 111 istate->pending_ua = UA_POWER_ON; 112 } 113 114 return (0); 115} 116 117/* Caller allocates CTIO, sets its init_id 118return 0 if done, 1 if more processing needed 119on 0, caller sets SEND_STATUS */ 120int 121tcmd_handle(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio, io_ops event) 122{ 123 static struct targ_cdb_handlers *last_cmd; 124 struct initiator_state *istate; 125 struct atio_descr *a_descr; 126 int ret; 127 128 if (debug) { 129 warnx("tcmd_handle atio %p ctio %p atioflags %#x", atio, ctio, 130 atio->ccb_h.flags); 131 } 132 ret = 0; 133 a_descr = (struct atio_descr *)atio->ccb_h.targ_descr; 134 135 /* Do a full lookup if one-behind cache failed */ 136 if (last_cmd == NULL || last_cmd->cmd != a_descr->cdb[0]) { 137 struct targ_cdb_handlers *h; 138 139 for (h = cdb_handlers; h->cmd != ILLEGAL_CDB; h++) { 140 if (a_descr->cdb[0] == h->cmd) 141 break; 142 } 143 last_cmd = h; 144 } 145 if (last_cmd->cmd == ILLEGAL_CDB) { 146 if (event != ATIO_WORK) { 147 warnx("no done func for %#x???", a_descr->cdb[0]); 148 abort(); 149 } 150 /* Not found, return illegal request */ 151 warnx("cdb %#x not handled", a_descr->cdb[0]); 152 tcmd_illegal_req(atio, ctio); 153 send_ccb((union ccb *)ctio, /*priority*/1); 154 return (0); 155 } 156 157 /* call completion and exit */ 158 if (event != ATIO_WORK) { 159 if (last_cmd->done != NULL) 160 last_cmd->done(atio, ctio, event); 161 else 162 free_ccb((union ccb *)ctio); 163 return (1); 164 } 165 166 istate = tcmd_get_istate(ctio->init_id); 167 if (istate == NULL) { 168 tcmd_illegal_req(atio, ctio); 169 send_ccb((union ccb *)ctio, /*priority*/1); 170 return (0); 171 } 172 173 if (istate->pending_ca == 0 && istate->pending_ua != 0 && 174 a_descr->cdb[0] != INQUIRY) { 175 tcmd_sense(ctio->init_id, ctio, SSD_KEY_UNIT_ATTENTION, 176 0x29, istate->pending_ua == UA_POWER_ON ? 1 : 2); 177 istate->pending_ca = CA_UNIT_ATTN; 178 if (debug) { 179 cdb_debug(a_descr->cdb, "UA active for %u: ", 180 atio->init_id); 181 } 182 send_ccb((union ccb *)ctio, /*priority*/1); 183 return (0); 184 } 185 186 /* Store current CA and UA for later */ 187 istate->orig_ua = istate->pending_ua; 188 istate->orig_ca = istate->pending_ca; 189 190 /* 191 * As per SAM2, any command that occurs 192 * after a CA is reported, clears the CA. We must 193 * also clear the UA condition, if any, that caused 194 * the CA to occur assuming the UA is not for a 195 * persistent condition. 196 */ 197 istate->pending_ca = CA_NONE; 198 if (istate->orig_ca == CA_UNIT_ATTN) 199 istate->pending_ua = UA_NONE; 200 201 /* If we have a valid handler, call start or completion function */ 202 if (last_cmd->cmd != ILLEGAL_CDB) { 203 ret = last_cmd->start(atio, ctio); 204 /* XXX hack */ 205 if (last_cmd->start != tcmd_rdwr) { 206 a_descr->init_req += ctio->dxfer_len; 207 send_ccb((union ccb *)ctio, /*priority*/1); 208 } 209 } 210 211 return (ret); 212} 213 214static struct initiator_state * 215tcmd_get_istate(u_int init_id) 216{ 217 if (init_id >= MAX_INITIATORS) { 218 warnx("illegal init_id %d, max %d", init_id, MAX_INITIATORS - 1); 219 return (NULL); 220 } else { 221 return (&istates[init_id]); 222 } 223} 224 225void 226tcmd_sense(u_int init_id, struct ccb_scsiio *ctio, u_int8_t flags, 227 u_int8_t asc, u_int8_t ascq) 228{ 229 struct initiator_state *istate; 230 struct scsi_sense_data *sense; 231 232 /* Set our initiator's istate */ 233 istate = tcmd_get_istate(init_id); 234 if (istate == NULL) 235 return; 236 istate->pending_ca |= CA_CMD_SENSE; /* XXX set instead of or? */ 237 sense = &istate->sense_data; 238 bzero(sense, sizeof(*sense)); 239 sense->error_code = SSD_CURRENT_ERROR; 240 sense->flags = flags; 241 sense->add_sense_code = asc; 242 sense->add_sense_code_qual = ascq; 243 sense->extra_len = 244 offsetof(struct scsi_sense_data, sense_key_spec[2]) - 245 offsetof(struct scsi_sense_data, extra_len); 246 247 /* Fill out the supplied CTIO */ 248 if (ctio != NULL) { 249 bcopy(sense, &ctio->sense_data, sizeof(*sense)); 250 ctio->sense_len = sizeof(*sense); /* XXX */ 251 ctio->ccb_h.flags &= ~CAM_DIR_MASK; 252 ctio->ccb_h.flags |= CAM_DIR_NONE | /* CAM_SEND_SENSE | */ 253 CAM_SEND_STATUS; 254 ctio->dxfer_len = 0; 255 ctio->scsi_status = SCSI_STATUS_CHECK_COND; 256 } 257} 258 259void 260tcmd_ua(u_int init_id, ua_types new_ua) 261{ 262 struct initiator_state *istate; 263 u_int start, end; 264 265 if (init_id == CAM_TARGET_WILDCARD) { 266 start = 0; 267 end = MAX_INITIATORS - 1; 268 } else { 269 start = end = init_id; 270 } 271 272 for (; start <= end; start++) { 273 istate = tcmd_get_istate(start); 274 if (istate == NULL) 275 break; 276 istate->pending_ua = new_ua; 277 } 278} 279 280static int 281tcmd_inquiry(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio) 282{ 283 struct scsi_inquiry *inq; 284 struct atio_descr *a_descr; 285 struct initiator_state *istate; 286 struct scsi_sense_data *sense; 287 288 a_descr = (struct atio_descr *)atio->ccb_h.targ_descr; 289 inq = (struct scsi_inquiry *)a_descr->cdb; 290 291 if (debug) 292 cdb_debug(a_descr->cdb, "INQUIRY from %u: ", atio->init_id); 293 /* 294 * Validate the command. We don't support any VPD pages, so 295 * complain if EVPD or CMDDT is set. 296 */ 297 istate = tcmd_get_istate(ctio->init_id); 298 sense = &istate->sense_data; 299 if ((inq->byte2 & SI_EVPD) != 0) { 300 tcmd_illegal_req(atio, ctio); 301 sense->sense_key_spec[0] = SSD_SCS_VALID | SSD_FIELDPTR_CMD | 302 SSD_BITPTR_VALID | /*bit value*/1; 303 sense->sense_key_spec[1] = 0; 304 sense->sense_key_spec[2] = 305 offsetof(struct scsi_inquiry, byte2); 306 } else if (inq->page_code != 0) { 307 tcmd_illegal_req(atio, ctio); 308 sense->sense_key_spec[0] = SSD_SCS_VALID | SSD_FIELDPTR_CMD; 309 sense->sense_key_spec[1] = 0; 310 sense->sense_key_spec[2] = 311 offsetof(struct scsi_inquiry, page_code); 312 } else { 313 bcopy(&inq_data, ctio->data_ptr, sizeof(inq_data)); 314 ctio->dxfer_len = inq_data.additional_length + 4; 315 ctio->dxfer_len = min(ctio->dxfer_len, 316 SCSI_CDB6_LEN(inq->length)); 317 ctio->ccb_h.flags |= CAM_DIR_IN | CAM_SEND_STATUS; 318 ctio->scsi_status = SCSI_STATUS_OK; 319 } 320 return (0); 321} 322 323/* Initialize the inquiry response structure with the requested flags */ 324static int 325init_inquiry(u_int16_t req_flags, u_int16_t sim_flags) 326{ 327 struct scsi_inquiry_data *inq; 328 329 inq = &inq_data; 330 bzero(inq, sizeof(*inq)); 331 inq->device = T_DIRECT | (SID_QUAL_LU_CONNECTED << 5); 332#ifdef SCSI_REV_SPC 333 inq->version = SCSI_REV_SPC; /* was 2 */ 334#else 335 inq->version = SCSI_REV_3; /* was 2 */ 336#endif 337 338 /* 339 * XXX cpi.hba_inquiry doesn't support Addr16 so we give the 340 * user what they want if they ask for it. 341 */ 342 if ((req_flags & SID_Addr16) != 0) { 343 sim_flags |= SID_Addr16; 344 warnx("Not sure SIM supports Addr16 but enabling it anyway"); 345 } 346 347 /* Advertise only what the SIM can actually support */ 348 req_flags &= sim_flags; 349 scsi_ulto2b(req_flags, &inq->reserved[1]); 350 351 inq->response_format = 2; /* SCSI2 Inquiry Format */ 352 inq->additional_length = SHORT_INQUIRY_LENGTH - 353 offsetof(struct scsi_inquiry_data, additional_length); 354 bcopy("FreeBSD ", inq->vendor, SID_VENDOR_SIZE); 355 bcopy("Emulated Disk ", inq->product, SID_PRODUCT_SIZE); 356 bcopy("0.1 ", inq->revision, SID_REVISION_SIZE); 357 return (0); 358} 359 360static int 361tcmd_req_sense(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio) 362{ 363 struct scsi_request_sense *rsense; 364 struct scsi_sense_data *sense; 365 struct initiator_state *istate; 366 size_t dlen; 367 struct atio_descr *a_descr; 368 369 a_descr = (struct atio_descr *)atio->ccb_h.targ_descr; 370 rsense = (struct scsi_request_sense *)a_descr->cdb; 371 372 istate = tcmd_get_istate(ctio->init_id); 373 sense = &istate->sense_data; 374 375 if (debug) { 376 cdb_debug(a_descr->cdb, "REQ SENSE from %u: ", atio->init_id); 377 warnx("Sending sense: %#x %#x %#x", sense->flags, 378 sense->add_sense_code, sense->add_sense_code_qual); 379 } 380 381 if (istate->orig_ca == 0) { 382 tcmd_sense(ctio->init_id, NULL, SSD_KEY_NO_SENSE, 0, 0); 383 warnx("REQUEST SENSE from %u but no pending CA!", 384 ctio->init_id); 385 } 386 387 bcopy(sense, ctio->data_ptr, sizeof(struct scsi_sense_data)); 388 dlen = offsetof(struct scsi_sense_data, extra_len) + 389 sense->extra_len + 1; 390 ctio->dxfer_len = min(dlen, SCSI_CDB6_LEN(rsense->length)); 391 ctio->ccb_h.flags |= CAM_DIR_IN | CAM_SEND_STATUS; 392 ctio->scsi_status = SCSI_STATUS_OK; 393 return (0); 394} 395 396static int 397tcmd_rd_cap(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio) 398{ 399 struct scsi_read_capacity_data *srp; 400 struct atio_descr *a_descr; 401 402 a_descr = (struct atio_descr *)atio->ccb_h.targ_descr; 403 srp = (struct scsi_read_capacity_data *)ctio->data_ptr; 404 405 if (debug) { 406 cdb_debug(a_descr->cdb, "READ CAP from %u (%u, %u): ", 407 atio->init_id, volume_size - 1, sector_size); 408 } 409 410 bzero(srp, sizeof(*srp)); 411 scsi_ulto4b(volume_size - 1, srp->addr); 412 scsi_ulto4b(sector_size, srp->length); 413 414 ctio->dxfer_len = sizeof(*srp); 415 ctio->ccb_h.flags |= CAM_DIR_IN | CAM_SEND_STATUS; 416 ctio->scsi_status = SCSI_STATUS_OK; 417 return (0); 418} 419 420static int 421tcmd_rdwr(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio) 422{ 423 struct atio_descr *a_descr; 424 struct ctio_descr *c_descr; 425 int ret; 426 427 a_descr = (struct atio_descr *)atio->ccb_h.targ_descr; 428 c_descr = (struct ctio_descr *)ctio->ccb_h.targ_descr; 429 430 /* Command needs to be decoded */ 431 if ((a_descr->flags & CAM_DIR_MASK) == CAM_DIR_RESV) { 432 if (debug) 433 warnx("Calling rdwr_decode"); 434 ret = tcmd_rdwr_decode(atio, ctio); 435 if (ret == 0) { 436 send_ccb((union ccb *)ctio, /*priority*/1); 437 return (0); 438 } 439 } 440 ctio->ccb_h.flags |= a_descr->flags; 441 442 /* Call appropriate work function */ 443 if ((a_descr->flags & CAM_DIR_IN) != 0) { 444 ret = start_io(atio, ctio, CAM_DIR_IN); 445 if (debug) 446 warnx("Starting DIR_IN @%lld:%u", c_descr->offset, 447 a_descr->targ_req); 448 } else { 449 ret = start_io(atio, ctio, CAM_DIR_OUT); 450 if (debug) 451 warnx("Starting DIR_OUT @%lld:%u", c_descr->offset, 452 a_descr->init_req); 453 } 454 455 return (ret); 456} 457 458static int 459tcmd_rdwr_decode(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio) 460{ 461 u_int32_t blkno, count; 462 struct atio_descr *a_descr; 463 u_int8_t *cdb; 464 465 a_descr = (struct atio_descr *)atio->ccb_h.targ_descr; 466 cdb = a_descr->cdb; 467 if (debug) 468 cdb_debug(cdb, "R/W from %u: ", atio->init_id); 469 470 if (cdb[0] == READ_6 || cdb[0] == WRITE_6) { 471 struct scsi_rw_6 *rw_6 = (struct scsi_rw_6 *)cdb; 472 blkno = scsi_3btoul(rw_6->addr); 473 count = rw_6->length; 474 } else { 475 struct scsi_rw_10 *rw_10 = (struct scsi_rw_10 *)cdb; 476 blkno = scsi_4btoul(rw_10->addr); 477 count = scsi_2btoul(rw_10->length); 478 } 479 if (blkno + count > volume_size) { 480 warnx("Attempt to access past end of volume"); 481 tcmd_sense(ctio->init_id, ctio, 482 SSD_KEY_ILLEGAL_REQUEST, 0x21, 0); 483 return (0); 484 } 485 486 /* Get an (overall) data length and set direction */ 487 a_descr->base_off = ((off_t)blkno) * sector_size; 488 a_descr->total_len = count * sector_size; 489 if (a_descr->total_len == 0) { 490 if (debug) 491 warnx("r/w 0 blocks @ blkno %u", blkno); 492 tcmd_null_ok(atio, ctio); 493 return (0); 494 } else if (cdb[0] == WRITE_6 || cdb[0] == WRITE_10) { 495 a_descr->flags |= CAM_DIR_OUT; 496 if (debug) 497 warnx("write %u blocks @ blkno %u", count, blkno); 498 } else { 499 a_descr->flags |= CAM_DIR_IN; 500 if (debug) 501 warnx("read %u blocks @ blkno %u", count, blkno); 502 } 503 return (1); 504} 505 506static int 507start_io(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio, int dir) 508{ 509 struct atio_descr *a_descr; 510 struct ctio_descr *c_descr; 511 int ret; 512 513 /* Set up common structures */ 514 a_descr = (struct atio_descr *)atio->ccb_h.targ_descr; 515 c_descr = (struct ctio_descr *)ctio->ccb_h.targ_descr; 516 517 if (dir == CAM_DIR_IN) { 518 c_descr->offset = a_descr->base_off + a_descr->targ_req; 519 ctio->dxfer_len = a_descr->total_len - a_descr->targ_req; 520 } else { 521 c_descr->offset = a_descr->base_off + a_descr->init_req; 522 ctio->dxfer_len = a_descr->total_len - a_descr->init_req; 523 } 524 ctio->dxfer_len = min(ctio->dxfer_len, buf_size); 525 assert(ctio->dxfer_len >= 0); 526 527 c_descr->aiocb.aio_offset = c_descr->offset; 528 c_descr->aiocb.aio_nbytes = ctio->dxfer_len; 529 530 /* If DIR_IN, start read from target, otherwise begin CTIO xfer. */ 531 ret = 1; 532 if (dir == CAM_DIR_IN) { 533 if (aio_read(&c_descr->aiocb) < 0) 534 err(1, "aio_read"); /* XXX */ 535 a_descr->targ_req += ctio->dxfer_len; 536 if (a_descr->targ_req == a_descr->total_len) { 537 ctio->ccb_h.flags |= CAM_SEND_STATUS; 538 ctio->scsi_status = SCSI_STATUS_OK; 539 ret = 0; 540 } 541 } else { 542 if (a_descr->targ_ack == a_descr->total_len) 543 tcmd_null_ok(atio, ctio); 544 a_descr->init_req += ctio->dxfer_len; 545 if (a_descr->init_req == a_descr->total_len && 546 ctio->dxfer_len > 0) { 547 /* 548 * If data phase done, remove atio from workq. 549 * The completion handler will call work_atio to 550 * send the final status. 551 */ 552 ret = 0; 553 } 554 send_ccb((union ccb *)ctio, /*priority*/1); 555 } 556 557 return (ret); 558} 559 560static void 561tcmd_rdwr_done(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio, 562 io_ops event) 563{ 564 struct atio_descr *a_descr; 565 struct ctio_descr *c_descr; 566 567 a_descr = (struct atio_descr *)atio->ccb_h.targ_descr; 568 c_descr = (struct ctio_descr *)ctio->ccb_h.targ_descr; 569 570 switch (event) { 571 case AIO_DONE: 572 if (aio_return(&c_descr->aiocb) < 0) { 573 warn("aio_return error"); 574 /* XXX */ 575 tcmd_sense(ctio->init_id, ctio, 576 SSD_KEY_MEDIUM_ERROR, 0, 0); 577 send_ccb((union ccb *)ctio, /*priority*/1); 578 break; 579 } 580 a_descr->targ_ack += ctio->dxfer_len; 581 if ((a_descr->flags & CAM_DIR_IN) != 0) { 582 if (debug) 583 warnx("sending CTIO for AIO read"); 584 a_descr->init_req += ctio->dxfer_len; 585 send_ccb((union ccb *)ctio, /*priority*/1); 586 } else { 587 /* Use work function to send final status */ 588 if (a_descr->init_req == a_descr->total_len) 589 work_atio(atio); 590 if (debug) 591 warnx("AIO done freeing CTIO"); 592 free_ccb((union ccb *)ctio); 593 } 594 break; 595 case CTIO_DONE: 596 if (ctio->ccb_h.status != CAM_REQ_CMP) { 597 /* XXX */ 598 errx(1, "CTIO failed, status %#x", ctio->ccb_h.status); 599 } 600 a_descr->init_ack += ctio->dxfer_len; 601 if ((a_descr->flags & CAM_DIR_MASK) == CAM_DIR_OUT && 602 ctio->dxfer_len > 0) { 603 if (debug) 604 warnx("sending AIO for CTIO write"); 605 a_descr->targ_req += ctio->dxfer_len; 606 if (aio_write(&c_descr->aiocb) < 0) 607 err(1, "aio_write"); /* XXX */ 608 } else { 609 if (debug) 610 warnx("CTIO done freeing CTIO"); 611 free_ccb((union ccb *)ctio); 612 } 613 break; 614 default: 615 warnx("Unknown completion code %d", event); 616 abort(); 617 /* NOTREACHED */ 618 } 619} 620 621/* Simple ok message used by TUR, SYNC_CACHE, etc. */ 622static int 623tcmd_null_ok(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio) 624{ 625 if (debug) { 626 struct atio_descr *a_descr; 627 628 a_descr = (struct atio_descr *)atio->ccb_h.targ_descr; 629 cdb_debug(a_descr->cdb, "Sending null ok to %u : ", atio->init_id); 630 } 631 632 ctio->dxfer_len = 0; 633 ctio->ccb_h.flags &= ~CAM_DIR_MASK; 634 ctio->ccb_h.flags |= CAM_DIR_NONE | CAM_SEND_STATUS; 635 ctio->scsi_status = SCSI_STATUS_OK; 636 return (0); 637} 638 639/* Simple illegal request message used by MODE SENSE, etc. */ 640static int 641tcmd_illegal_req(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio) 642{ 643 if (debug) { 644 struct atio_descr *a_descr; 645 646 a_descr = (struct atio_descr *)atio->ccb_h.targ_descr; 647 cdb_debug(a_descr->cdb, "Sending ill req to %u: ", atio->init_id); 648 } 649 650 tcmd_sense(atio->init_id, ctio, SSD_KEY_ILLEGAL_REQUEST, 651 /*asc*/0x24, /*ascq*/0); 652 return (0); 653} 654 655static void 656cdb_debug(u_int8_t *cdb, const char *msg, ...) 657{ 658 char msg_buf[512]; 659 int len; 660 va_list ap; 661 662 va_start(ap, msg); 663 vsnprintf(msg_buf, sizeof(msg_buf), msg, ap); 664 va_end(ap); 665 len = strlen(msg_buf); 666 scsi_cdb_string(cdb, msg_buf + len, sizeof(msg_buf) - len); 667 warnx("%s", msg_buf); 668} 669