1c38b150aSNate Lawson /* 2c38b150aSNate Lawson * SCSI Disk Emulator 3c38b150aSNate Lawson * 4c38b150aSNate Lawson * Copyright (c) 2002 Nate Lawson. 5c38b150aSNate Lawson * All rights reserved. 6c38b150aSNate Lawson * 7c38b150aSNate Lawson * Redistribution and use in source and binary forms, with or without 8c38b150aSNate Lawson * modification, are permitted provided that the following conditions 9c38b150aSNate Lawson * are met: 10c38b150aSNate Lawson * 1. Redistributions of source code must retain the above copyright 11c38b150aSNate Lawson * notice, this list of conditions, and the following disclaimer, 12c38b150aSNate Lawson * without modification, immediately at the beginning of the file. 13c38b150aSNate Lawson * 2. The name of the author may not be used to endorse or promote products 14c38b150aSNate Lawson * derived from this software without specific prior written permission. 15c38b150aSNate Lawson * 16c38b150aSNate Lawson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17c38b150aSNate Lawson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18c38b150aSNate Lawson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19c38b150aSNate Lawson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20c38b150aSNate Lawson * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21c38b150aSNate Lawson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22c38b150aSNate Lawson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23c38b150aSNate Lawson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24c38b150aSNate Lawson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25c38b150aSNate Lawson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26c38b150aSNate Lawson * SUCH DAMAGE. 27c38b150aSNate Lawson * 28c38b150aSNate Lawson * $FreeBSD$ 29c38b150aSNate Lawson */ 30c38b150aSNate Lawson 31c38b150aSNate Lawson #include <stdio.h> 32c38b150aSNate Lawson #include <stddef.h> 33c38b150aSNate Lawson #include <stdarg.h> 34c38b150aSNate Lawson #include <stdlib.h> 35c38b150aSNate Lawson #include <string.h> 36c38b150aSNate Lawson #include <err.h> 37c38b150aSNate Lawson #include <aio.h> 38cd6dedfcSMatt Jacob #include <unistd.h> 39c38b150aSNate Lawson #include <assert.h> 40068d70baSHidetoshi Shimokawa #include <sys/param.h> 41c38b150aSNate Lawson #include <sys/types.h> 42c38b150aSNate Lawson 43c38b150aSNate Lawson #include <cam/cam.h> 44c38b150aSNate Lawson #include <cam/cam_ccb.h> 45c38b150aSNate Lawson #include <cam/scsi/scsi_all.h> 46c38b150aSNate Lawson #include <cam/scsi/scsi_targetio.h> 47c38b150aSNate Lawson #include "scsi_target.h" 48c38b150aSNate Lawson 49c38b150aSNate Lawson typedef int targ_start_func(struct ccb_accept_tio *, struct ccb_scsiio *); 50c38b150aSNate Lawson typedef void targ_done_func(struct ccb_accept_tio *, struct ccb_scsiio *, 51c38b150aSNate Lawson io_ops); 520fa27452SMatt Jacob #ifndef REPORT_LUNS 530fa27452SMatt Jacob #define REPORT_LUNS 0xa0 540fa27452SMatt Jacob #endif 55c38b150aSNate Lawson 56c38b150aSNate Lawson struct targ_cdb_handlers { 57c38b150aSNate Lawson u_int8_t cmd; 58c38b150aSNate Lawson targ_start_func *start; 59c38b150aSNate Lawson targ_done_func *done; 60c38b150aSNate Lawson #define ILLEGAL_CDB 0xFF 61c38b150aSNate Lawson }; 62c38b150aSNate Lawson 63c38b150aSNate Lawson static targ_start_func tcmd_inquiry; 64c38b150aSNate Lawson static targ_start_func tcmd_req_sense; 65c38b150aSNate Lawson static targ_start_func tcmd_rd_cap; 66068d70baSHidetoshi Shimokawa #ifdef READ_16 67068d70baSHidetoshi Shimokawa static targ_start_func tcmd_rd_cap16; 68068d70baSHidetoshi Shimokawa #endif 69c38b150aSNate Lawson static targ_start_func tcmd_rdwr; 70c38b150aSNate Lawson static targ_start_func tcmd_rdwr_decode; 71c38b150aSNate Lawson static targ_done_func tcmd_rdwr_done; 72c38b150aSNate Lawson static targ_start_func tcmd_null_ok; 73c38b150aSNate Lawson static targ_start_func tcmd_illegal_req; 74c38b150aSNate Lawson static int start_io(struct ccb_accept_tio *atio, 75c38b150aSNate Lawson struct ccb_scsiio *ctio, int dir); 76c38b150aSNate Lawson static int init_inquiry(u_int16_t req_flags, u_int16_t sim_flags); 77c38b150aSNate Lawson static struct initiator_state * 78c38b150aSNate Lawson tcmd_get_istate(u_int init_id); 79c38b150aSNate Lawson static void cdb_debug(u_int8_t *cdb, const char *msg, ...); 80c38b150aSNate Lawson 81c38b150aSNate Lawson static struct targ_cdb_handlers cdb_handlers[] = { 82c38b150aSNate Lawson { READ_10, tcmd_rdwr, tcmd_rdwr_done }, 83c38b150aSNate Lawson { WRITE_10, tcmd_rdwr, tcmd_rdwr_done }, 84c38b150aSNate Lawson { READ_6, tcmd_rdwr, tcmd_rdwr_done }, 85c38b150aSNate Lawson { WRITE_6, tcmd_rdwr, tcmd_rdwr_done }, 86c38b150aSNate Lawson { INQUIRY, tcmd_inquiry, NULL }, 87c38b150aSNate Lawson { REQUEST_SENSE, tcmd_req_sense, NULL }, 88c38b150aSNate Lawson { READ_CAPACITY, tcmd_rd_cap, NULL }, 89c38b150aSNate Lawson { TEST_UNIT_READY, tcmd_null_ok, NULL }, 90c38b150aSNate Lawson { START_STOP_UNIT, tcmd_null_ok, NULL }, 91c38b150aSNate Lawson { SYNCHRONIZE_CACHE, tcmd_null_ok, NULL }, 92c38b150aSNate Lawson { MODE_SENSE_6, tcmd_illegal_req, NULL }, 93c38b150aSNate Lawson { MODE_SELECT_6, tcmd_illegal_req, NULL }, 940fa27452SMatt Jacob { REPORT_LUNS, tcmd_illegal_req, NULL }, 95068d70baSHidetoshi Shimokawa #ifdef READ_16 96068d70baSHidetoshi Shimokawa { READ_16, tcmd_rdwr, tcmd_rdwr_done }, 97068d70baSHidetoshi Shimokawa { WRITE_16, tcmd_rdwr, tcmd_rdwr_done }, 98068d70baSHidetoshi Shimokawa { SERVICE_ACTION_IN, tcmd_rd_cap16, NULL }, 99068d70baSHidetoshi Shimokawa #endif 100c38b150aSNate Lawson { ILLEGAL_CDB, NULL, NULL } 101c38b150aSNate Lawson }; 102c38b150aSNate Lawson 103c38b150aSNate Lawson static struct scsi_inquiry_data inq_data; 104c38b150aSNate Lawson static struct initiator_state istates[MAX_INITIATORS]; 105c38b150aSNate Lawson extern int debug; 106068d70baSHidetoshi Shimokawa extern uint64_t volume_size; 107c38b150aSNate Lawson extern size_t sector_size; 108c38b150aSNate Lawson extern size_t buf_size; 109c38b150aSNate Lawson 110c38b150aSNate Lawson cam_status 111c38b150aSNate Lawson tcmd_init(u_int16_t req_inq_flags, u_int16_t sim_inq_flags) 112c38b150aSNate Lawson { 113c38b150aSNate Lawson struct initiator_state *istate; 114c38b150aSNate Lawson int i, ret; 115c38b150aSNate Lawson 116c38b150aSNate Lawson /* Initialize our inquiry data */ 117c38b150aSNate Lawson ret = init_inquiry(req_inq_flags, sim_inq_flags); 118c38b150aSNate Lawson if (ret != 0) 119c38b150aSNate Lawson return (ret); 120c38b150aSNate Lawson 121c38b150aSNate Lawson /* We start out life with a UA to indicate power-on/reset. */ 122c38b150aSNate Lawson for (i = 0; i < MAX_INITIATORS; i++) { 123c38b150aSNate Lawson istate = tcmd_get_istate(i); 124c38b150aSNate Lawson bzero(istate, sizeof(*istate)); 125c38b150aSNate Lawson istate->pending_ua = UA_POWER_ON; 126c38b150aSNate Lawson } 127c38b150aSNate Lawson 128c38b150aSNate Lawson return (0); 129c38b150aSNate Lawson } 130c38b150aSNate Lawson 131c38b150aSNate Lawson /* Caller allocates CTIO, sets its init_id 132c38b150aSNate Lawson return 0 if done, 1 if more processing needed 133c38b150aSNate Lawson on 0, caller sets SEND_STATUS */ 134c38b150aSNate Lawson int 135c38b150aSNate Lawson tcmd_handle(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio, io_ops event) 136c38b150aSNate Lawson { 137c38b150aSNate Lawson static struct targ_cdb_handlers *last_cmd; 138c38b150aSNate Lawson struct initiator_state *istate; 139c38b150aSNate Lawson struct atio_descr *a_descr; 140c38b150aSNate Lawson int ret; 141c38b150aSNate Lawson 142031bacf8SNate Lawson if (debug) { 143c38b150aSNate Lawson warnx("tcmd_handle atio %p ctio %p atioflags %#x", atio, ctio, 144c38b150aSNate Lawson atio->ccb_h.flags); 145031bacf8SNate Lawson } 146c38b150aSNate Lawson ret = 0; 147c38b150aSNate Lawson a_descr = (struct atio_descr *)atio->ccb_h.targ_descr; 148c38b150aSNate Lawson 149c38b150aSNate Lawson /* Do a full lookup if one-behind cache failed */ 150c38b150aSNate Lawson if (last_cmd == NULL || last_cmd->cmd != a_descr->cdb[0]) { 151c38b150aSNate Lawson struct targ_cdb_handlers *h; 152c38b150aSNate Lawson 153c38b150aSNate Lawson for (h = cdb_handlers; h->cmd != ILLEGAL_CDB; h++) { 154c38b150aSNate Lawson if (a_descr->cdb[0] == h->cmd) 155c38b150aSNate Lawson break; 156c38b150aSNate Lawson } 157c38b150aSNate Lawson last_cmd = h; 158c38b150aSNate Lawson } 159441201ceSMatt Jacob 160441201ceSMatt Jacob /* call completion and exit */ 161441201ceSMatt Jacob if (event != ATIO_WORK) { 162441201ceSMatt Jacob if (last_cmd->done != NULL) 163441201ceSMatt Jacob last_cmd->done(atio, ctio, event); 164441201ceSMatt Jacob else 165441201ceSMatt Jacob free_ccb((union ccb *)ctio); 166441201ceSMatt Jacob return (1); 167441201ceSMatt Jacob } 168441201ceSMatt Jacob 169c38b150aSNate Lawson if (last_cmd->cmd == ILLEGAL_CDB) { 170c38b150aSNate Lawson if (event != ATIO_WORK) { 171c38b150aSNate Lawson warnx("no done func for %#x???", a_descr->cdb[0]); 172c38b150aSNate Lawson abort(); 173c38b150aSNate Lawson } 174c38b150aSNate Lawson /* Not found, return illegal request */ 175c38b150aSNate Lawson warnx("cdb %#x not handled", a_descr->cdb[0]); 176c38b150aSNate Lawson tcmd_illegal_req(atio, ctio); 177c38b150aSNate Lawson send_ccb((union ccb *)ctio, /*priority*/1); 178c38b150aSNate Lawson return (0); 179c38b150aSNate Lawson } 180c38b150aSNate Lawson 181c38b150aSNate Lawson istate = tcmd_get_istate(ctio->init_id); 182c38b150aSNate Lawson if (istate == NULL) { 183c38b150aSNate Lawson tcmd_illegal_req(atio, ctio); 184c38b150aSNate Lawson send_ccb((union ccb *)ctio, /*priority*/1); 185c38b150aSNate Lawson return (0); 186c38b150aSNate Lawson } 187c38b150aSNate Lawson 188c38b150aSNate Lawson if (istate->pending_ca == 0 && istate->pending_ua != 0 && 189c38b150aSNate Lawson a_descr->cdb[0] != INQUIRY) { 190c38b150aSNate Lawson tcmd_sense(ctio->init_id, ctio, SSD_KEY_UNIT_ATTENTION, 191c38b150aSNate Lawson 0x29, istate->pending_ua == UA_POWER_ON ? 1 : 2); 192c38b150aSNate Lawson istate->pending_ca = CA_UNIT_ATTN; 193c38b150aSNate Lawson if (debug) { 194c38b150aSNate Lawson cdb_debug(a_descr->cdb, "UA active for %u: ", 195c38b150aSNate Lawson atio->init_id); 196c38b150aSNate Lawson } 197c38b150aSNate Lawson send_ccb((union ccb *)ctio, /*priority*/1); 198c38b150aSNate Lawson return (0); 199c38b150aSNate Lawson } 200c38b150aSNate Lawson 201c38b150aSNate Lawson /* Store current CA and UA for later */ 202c38b150aSNate Lawson istate->orig_ua = istate->pending_ua; 203c38b150aSNate Lawson istate->orig_ca = istate->pending_ca; 204c38b150aSNate Lawson 205c38b150aSNate Lawson /* 206c38b150aSNate Lawson * As per SAM2, any command that occurs 207c38b150aSNate Lawson * after a CA is reported, clears the CA. We must 208c38b150aSNate Lawson * also clear the UA condition, if any, that caused 209c38b150aSNate Lawson * the CA to occur assuming the UA is not for a 210c38b150aSNate Lawson * persistent condition. 211c38b150aSNate Lawson */ 212c38b150aSNate Lawson istate->pending_ca = CA_NONE; 213c38b150aSNate Lawson if (istate->orig_ca == CA_UNIT_ATTN) 214c38b150aSNate Lawson istate->pending_ua = UA_NONE; 215c38b150aSNate Lawson 216c38b150aSNate Lawson /* If we have a valid handler, call start or completion function */ 217c38b150aSNate Lawson if (last_cmd->cmd != ILLEGAL_CDB) { 218c38b150aSNate Lawson ret = last_cmd->start(atio, ctio); 219c38b150aSNate Lawson /* XXX hack */ 220c38b150aSNate Lawson if (last_cmd->start != tcmd_rdwr) { 221c38b150aSNate Lawson a_descr->init_req += ctio->dxfer_len; 222c38b150aSNate Lawson send_ccb((union ccb *)ctio, /*priority*/1); 223c38b150aSNate Lawson } 224c38b150aSNate Lawson } 225c38b150aSNate Lawson 226c38b150aSNate Lawson return (ret); 227c38b150aSNate Lawson } 228c38b150aSNate Lawson 229c38b150aSNate Lawson static struct initiator_state * 230c38b150aSNate Lawson tcmd_get_istate(u_int init_id) 231c38b150aSNate Lawson { 232c38b150aSNate Lawson if (init_id >= MAX_INITIATORS) { 233c38b150aSNate Lawson warnx("illegal init_id %d, max %d", init_id, MAX_INITIATORS - 1); 234c38b150aSNate Lawson return (NULL); 235c38b150aSNate Lawson } else { 236c38b150aSNate Lawson return (&istates[init_id]); 237c38b150aSNate Lawson } 238c38b150aSNate Lawson } 239c38b150aSNate Lawson 240c38b150aSNate Lawson void 241c38b150aSNate Lawson tcmd_sense(u_int init_id, struct ccb_scsiio *ctio, u_int8_t flags, 242c38b150aSNate Lawson u_int8_t asc, u_int8_t ascq) 243c38b150aSNate Lawson { 244c38b150aSNate Lawson struct initiator_state *istate; 245c38b150aSNate Lawson struct scsi_sense_data *sense; 246c38b150aSNate Lawson 247c38b150aSNate Lawson /* Set our initiator's istate */ 248c38b150aSNate Lawson istate = tcmd_get_istate(init_id); 249c38b150aSNate Lawson if (istate == NULL) 250c38b150aSNate Lawson return; 251c38b150aSNate Lawson istate->pending_ca |= CA_CMD_SENSE; /* XXX set instead of or? */ 252c38b150aSNate Lawson sense = &istate->sense_data; 253c38b150aSNate Lawson bzero(sense, sizeof(*sense)); 254c38b150aSNate Lawson sense->error_code = SSD_CURRENT_ERROR; 255c38b150aSNate Lawson sense->flags = flags; 256c38b150aSNate Lawson sense->add_sense_code = asc; 257c38b150aSNate Lawson sense->add_sense_code_qual = ascq; 258c38b150aSNate Lawson sense->extra_len = 259c38b150aSNate Lawson offsetof(struct scsi_sense_data, sense_key_spec[2]) - 260c38b150aSNate Lawson offsetof(struct scsi_sense_data, extra_len); 261c38b150aSNate Lawson 262c38b150aSNate Lawson /* Fill out the supplied CTIO */ 263c38b150aSNate Lawson if (ctio != NULL) { 264c38b150aSNate Lawson bcopy(sense, &ctio->sense_data, sizeof(*sense)); 265c4b3637bSHidetoshi Shimokawa ctio->sense_len = sizeof(*sense); /* XXX */ 266c38b150aSNate Lawson ctio->ccb_h.flags &= ~CAM_DIR_MASK; 2674c1cc94eSNate Lawson ctio->ccb_h.flags |= CAM_DIR_NONE | CAM_SEND_SENSE | 268c38b150aSNate Lawson CAM_SEND_STATUS; 269c38b150aSNate Lawson ctio->dxfer_len = 0; 270c38b150aSNate Lawson ctio->scsi_status = SCSI_STATUS_CHECK_COND; 271c38b150aSNate Lawson } 272c38b150aSNate Lawson } 273c38b150aSNate Lawson 274c38b150aSNate Lawson void 275c38b150aSNate Lawson tcmd_ua(u_int init_id, ua_types new_ua) 276c38b150aSNate Lawson { 277c38b150aSNate Lawson struct initiator_state *istate; 278c38b150aSNate Lawson u_int start, end; 279c38b150aSNate Lawson 280c38b150aSNate Lawson if (init_id == CAM_TARGET_WILDCARD) { 281c38b150aSNate Lawson start = 0; 282c38b150aSNate Lawson end = MAX_INITIATORS - 1; 283c38b150aSNate Lawson } else { 284c38b150aSNate Lawson start = end = init_id; 285c38b150aSNate Lawson } 286c38b150aSNate Lawson 287c38b150aSNate Lawson for (; start <= end; start++) { 288c38b150aSNate Lawson istate = tcmd_get_istate(start); 289c38b150aSNate Lawson if (istate == NULL) 290c38b150aSNate Lawson break; 291c38b150aSNate Lawson istate->pending_ua = new_ua; 292c38b150aSNate Lawson } 293c38b150aSNate Lawson } 294c38b150aSNate Lawson 295c38b150aSNate Lawson static int 296c38b150aSNate Lawson tcmd_inquiry(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio) 297c38b150aSNate Lawson { 298c38b150aSNate Lawson struct scsi_inquiry *inq; 299c38b150aSNate Lawson struct atio_descr *a_descr; 300c38b150aSNate Lawson struct initiator_state *istate; 301c38b150aSNate Lawson struct scsi_sense_data *sense; 302c38b150aSNate Lawson 303c38b150aSNate Lawson a_descr = (struct atio_descr *)atio->ccb_h.targ_descr; 304c38b150aSNate Lawson inq = (struct scsi_inquiry *)a_descr->cdb; 305c38b150aSNate Lawson 306c38b150aSNate Lawson if (debug) 307c38b150aSNate Lawson cdb_debug(a_descr->cdb, "INQUIRY from %u: ", atio->init_id); 308c38b150aSNate Lawson /* 309c38b150aSNate Lawson * Validate the command. We don't support any VPD pages, so 310c38b150aSNate Lawson * complain if EVPD or CMDDT is set. 311c38b150aSNate Lawson */ 312c38b150aSNate Lawson istate = tcmd_get_istate(ctio->init_id); 313c38b150aSNate Lawson sense = &istate->sense_data; 314c38b150aSNate Lawson if ((inq->byte2 & SI_EVPD) != 0) { 315c38b150aSNate Lawson tcmd_illegal_req(atio, ctio); 316c38b150aSNate Lawson sense->sense_key_spec[0] = SSD_SCS_VALID | SSD_FIELDPTR_CMD | 317c38b150aSNate Lawson SSD_BITPTR_VALID | /*bit value*/1; 318c38b150aSNate Lawson sense->sense_key_spec[1] = 0; 319c38b150aSNate Lawson sense->sense_key_spec[2] = 320c38b150aSNate Lawson offsetof(struct scsi_inquiry, byte2); 321c38b150aSNate Lawson } else if (inq->page_code != 0) { 322c38b150aSNate Lawson tcmd_illegal_req(atio, ctio); 323c38b150aSNate Lawson sense->sense_key_spec[0] = SSD_SCS_VALID | SSD_FIELDPTR_CMD; 324c38b150aSNate Lawson sense->sense_key_spec[1] = 0; 325c38b150aSNate Lawson sense->sense_key_spec[2] = 326c38b150aSNate Lawson offsetof(struct scsi_inquiry, page_code); 327c38b150aSNate Lawson } else { 328c38b150aSNate Lawson bcopy(&inq_data, ctio->data_ptr, sizeof(inq_data)); 329c38b150aSNate Lawson ctio->dxfer_len = inq_data.additional_length + 4; 330c38b150aSNate Lawson ctio->dxfer_len = min(ctio->dxfer_len, 331c38b150aSNate Lawson SCSI_CDB6_LEN(inq->length)); 332c38b150aSNate Lawson ctio->ccb_h.flags |= CAM_DIR_IN | CAM_SEND_STATUS; 333c38b150aSNate Lawson ctio->scsi_status = SCSI_STATUS_OK; 334c38b150aSNate Lawson } 335c38b150aSNate Lawson return (0); 336c38b150aSNate Lawson } 337c38b150aSNate Lawson 338c38b150aSNate Lawson /* Initialize the inquiry response structure with the requested flags */ 339c38b150aSNate Lawson static int 340c38b150aSNate Lawson init_inquiry(u_int16_t req_flags, u_int16_t sim_flags) 341c38b150aSNate Lawson { 342c38b150aSNate Lawson struct scsi_inquiry_data *inq; 343c38b150aSNate Lawson 344c38b150aSNate Lawson inq = &inq_data; 345c38b150aSNate Lawson bzero(inq, sizeof(*inq)); 346c38b150aSNate Lawson inq->device = T_DIRECT | (SID_QUAL_LU_CONNECTED << 5); 347c4b3637bSHidetoshi Shimokawa #ifdef SCSI_REV_SPC 348c38b150aSNate Lawson inq->version = SCSI_REV_SPC; /* was 2 */ 349c4b3637bSHidetoshi Shimokawa #else 350c4b3637bSHidetoshi Shimokawa inq->version = SCSI_REV_3; /* was 2 */ 351c4b3637bSHidetoshi Shimokawa #endif 352c38b150aSNate Lawson 353c38b150aSNate Lawson /* 354c38b150aSNate Lawson * XXX cpi.hba_inquiry doesn't support Addr16 so we give the 355c38b150aSNate Lawson * user what they want if they ask for it. 356c38b150aSNate Lawson */ 357c38b150aSNate Lawson if ((req_flags & SID_Addr16) != 0) { 358c38b150aSNate Lawson sim_flags |= SID_Addr16; 359c38b150aSNate Lawson warnx("Not sure SIM supports Addr16 but enabling it anyway"); 360c38b150aSNate Lawson } 361c38b150aSNate Lawson 362c38b150aSNate Lawson /* Advertise only what the SIM can actually support */ 363c38b150aSNate Lawson req_flags &= sim_flags; 364cd6dedfcSMatt Jacob scsi_ulto2b(req_flags, &inq->spc2_flags); 365c38b150aSNate Lawson 366c38b150aSNate Lawson inq->response_format = 2; /* SCSI2 Inquiry Format */ 367c38b150aSNate Lawson inq->additional_length = SHORT_INQUIRY_LENGTH - 368c38b150aSNate Lawson offsetof(struct scsi_inquiry_data, additional_length); 369c38b150aSNate Lawson bcopy("FreeBSD ", inq->vendor, SID_VENDOR_SIZE); 370c38b150aSNate Lawson bcopy("Emulated Disk ", inq->product, SID_PRODUCT_SIZE); 371c38b150aSNate Lawson bcopy("0.1 ", inq->revision, SID_REVISION_SIZE); 372c38b150aSNate Lawson return (0); 373c38b150aSNate Lawson } 374c38b150aSNate Lawson 375c38b150aSNate Lawson static int 376c38b150aSNate Lawson tcmd_req_sense(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio) 377c38b150aSNate Lawson { 378c38b150aSNate Lawson struct scsi_request_sense *rsense; 379c38b150aSNate Lawson struct scsi_sense_data *sense; 380c38b150aSNate Lawson struct initiator_state *istate; 381c38b150aSNate Lawson size_t dlen; 382c38b150aSNate Lawson struct atio_descr *a_descr; 383c38b150aSNate Lawson 384c38b150aSNate Lawson a_descr = (struct atio_descr *)atio->ccb_h.targ_descr; 385c38b150aSNate Lawson rsense = (struct scsi_request_sense *)a_descr->cdb; 386c38b150aSNate Lawson 387c38b150aSNate Lawson istate = tcmd_get_istate(ctio->init_id); 388c38b150aSNate Lawson sense = &istate->sense_data; 389c38b150aSNate Lawson 390c38b150aSNate Lawson if (debug) { 391c38b150aSNate Lawson cdb_debug(a_descr->cdb, "REQ SENSE from %u: ", atio->init_id); 392c38b150aSNate Lawson warnx("Sending sense: %#x %#x %#x", sense->flags, 393c38b150aSNate Lawson sense->add_sense_code, sense->add_sense_code_qual); 394c38b150aSNate Lawson } 395c38b150aSNate Lawson 396c38b150aSNate Lawson if (istate->orig_ca == 0) { 397c38b150aSNate Lawson tcmd_sense(ctio->init_id, NULL, SSD_KEY_NO_SENSE, 0, 0); 398c38b150aSNate Lawson warnx("REQUEST SENSE from %u but no pending CA!", 399c38b150aSNate Lawson ctio->init_id); 400c38b150aSNate Lawson } 401c38b150aSNate Lawson 402c38b150aSNate Lawson bcopy(sense, ctio->data_ptr, sizeof(struct scsi_sense_data)); 403c38b150aSNate Lawson dlen = offsetof(struct scsi_sense_data, extra_len) + 404c38b150aSNate Lawson sense->extra_len + 1; 405c38b150aSNate Lawson ctio->dxfer_len = min(dlen, SCSI_CDB6_LEN(rsense->length)); 406c38b150aSNate Lawson ctio->ccb_h.flags |= CAM_DIR_IN | CAM_SEND_STATUS; 407c38b150aSNate Lawson ctio->scsi_status = SCSI_STATUS_OK; 408c38b150aSNate Lawson return (0); 409c38b150aSNate Lawson } 410c38b150aSNate Lawson 411c38b150aSNate Lawson static int 412c38b150aSNate Lawson tcmd_rd_cap(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio) 413c38b150aSNate Lawson { 414c38b150aSNate Lawson struct scsi_read_capacity_data *srp; 415c38b150aSNate Lawson struct atio_descr *a_descr; 416068d70baSHidetoshi Shimokawa uint32_t vsize; 417c38b150aSNate Lawson 418c38b150aSNate Lawson a_descr = (struct atio_descr *)atio->ccb_h.targ_descr; 419c38b150aSNate Lawson srp = (struct scsi_read_capacity_data *)ctio->data_ptr; 420c38b150aSNate Lawson 421068d70baSHidetoshi Shimokawa if (volume_size > 0xffffffff) 422068d70baSHidetoshi Shimokawa vsize = 0xffffffff; 423068d70baSHidetoshi Shimokawa else 424068d70baSHidetoshi Shimokawa vsize = (uint32_t)(volume_size - 1); 425068d70baSHidetoshi Shimokawa 426c38b150aSNate Lawson if (debug) { 427c38b150aSNate Lawson cdb_debug(a_descr->cdb, "READ CAP from %u (%u, %u): ", 428068d70baSHidetoshi Shimokawa atio->init_id, vsize, sector_size); 429c38b150aSNate Lawson } 430c38b150aSNate Lawson 431c38b150aSNate Lawson bzero(srp, sizeof(*srp)); 432068d70baSHidetoshi Shimokawa scsi_ulto4b(vsize, srp->addr); 433c38b150aSNate Lawson scsi_ulto4b(sector_size, srp->length); 434c38b150aSNate Lawson 435c38b150aSNate Lawson ctio->dxfer_len = sizeof(*srp); 436c38b150aSNate Lawson ctio->ccb_h.flags |= CAM_DIR_IN | CAM_SEND_STATUS; 437c38b150aSNate Lawson ctio->scsi_status = SCSI_STATUS_OK; 438c38b150aSNate Lawson return (0); 439c38b150aSNate Lawson } 440c38b150aSNate Lawson 441068d70baSHidetoshi Shimokawa #ifdef READ_16 442068d70baSHidetoshi Shimokawa static int 443068d70baSHidetoshi Shimokawa tcmd_rd_cap16(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio) 444068d70baSHidetoshi Shimokawa { 445068d70baSHidetoshi Shimokawa struct scsi_read_capacity_16 *scsi_cmd; 446068d70baSHidetoshi Shimokawa struct scsi_read_capacity_data_long *srp; 447068d70baSHidetoshi Shimokawa struct atio_descr *a_descr; 448068d70baSHidetoshi Shimokawa 449068d70baSHidetoshi Shimokawa a_descr = (struct atio_descr *)atio->ccb_h.targ_descr; 450068d70baSHidetoshi Shimokawa scsi_cmd = (struct scsi_read_capacity_16 *)a_descr->cdb; 451068d70baSHidetoshi Shimokawa srp = (struct scsi_read_capacity_data_long *)ctio->data_ptr; 452068d70baSHidetoshi Shimokawa 453068d70baSHidetoshi Shimokawa if (scsi_cmd->service_action != SRC16_SERVICE_ACTION) { 454068d70baSHidetoshi Shimokawa tcmd_illegal_req(atio, ctio); 455068d70baSHidetoshi Shimokawa return (0); 456068d70baSHidetoshi Shimokawa } 457068d70baSHidetoshi Shimokawa 458068d70baSHidetoshi Shimokawa if (debug) { 459068d70baSHidetoshi Shimokawa cdb_debug(a_descr->cdb, "READ CAP16 from %u (%u, %u): ", 460068d70baSHidetoshi Shimokawa atio->init_id, volume_size - 1, sector_size); 461068d70baSHidetoshi Shimokawa } 462068d70baSHidetoshi Shimokawa 463068d70baSHidetoshi Shimokawa bzero(srp, sizeof(*srp)); 464068d70baSHidetoshi Shimokawa scsi_u64to8b(volume_size - 1, srp->addr); 465068d70baSHidetoshi Shimokawa scsi_ulto4b(sector_size, srp->length); 466068d70baSHidetoshi Shimokawa 467068d70baSHidetoshi Shimokawa ctio->dxfer_len = sizeof(*srp); 468068d70baSHidetoshi Shimokawa ctio->ccb_h.flags |= CAM_DIR_IN | CAM_SEND_STATUS; 469068d70baSHidetoshi Shimokawa ctio->scsi_status = SCSI_STATUS_OK; 470068d70baSHidetoshi Shimokawa return (0); 471068d70baSHidetoshi Shimokawa } 472068d70baSHidetoshi Shimokawa #endif 473068d70baSHidetoshi Shimokawa 474c38b150aSNate Lawson static int 475c38b150aSNate Lawson tcmd_rdwr(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio) 476c38b150aSNate Lawson { 477c38b150aSNate Lawson struct atio_descr *a_descr; 478c38b150aSNate Lawson struct ctio_descr *c_descr; 479c38b150aSNate Lawson int ret; 480c38b150aSNate Lawson 481c38b150aSNate Lawson a_descr = (struct atio_descr *)atio->ccb_h.targ_descr; 482c38b150aSNate Lawson c_descr = (struct ctio_descr *)ctio->ccb_h.targ_descr; 483c38b150aSNate Lawson 484c38b150aSNate Lawson /* Command needs to be decoded */ 485c38b150aSNate Lawson if ((a_descr->flags & CAM_DIR_MASK) == CAM_DIR_RESV) { 486c38b150aSNate Lawson if (debug) 487c38b150aSNate Lawson warnx("Calling rdwr_decode"); 488c38b150aSNate Lawson ret = tcmd_rdwr_decode(atio, ctio); 489c38b150aSNate Lawson if (ret == 0) { 490c38b150aSNate Lawson send_ccb((union ccb *)ctio, /*priority*/1); 491c38b150aSNate Lawson return (0); 492c38b150aSNate Lawson } 493c38b150aSNate Lawson } 494c38b150aSNate Lawson ctio->ccb_h.flags |= a_descr->flags; 495c38b150aSNate Lawson 496c38b150aSNate Lawson /* Call appropriate work function */ 497c38b150aSNate Lawson if ((a_descr->flags & CAM_DIR_IN) != 0) { 498c38b150aSNate Lawson ret = start_io(atio, ctio, CAM_DIR_IN); 499c38b150aSNate Lawson if (debug) 500cd6dedfcSMatt Jacob warnx("Starting %p DIR_IN @" OFF_FMT ":%u", 501cd6dedfcSMatt Jacob a_descr, c_descr->offset, a_descr->targ_req); 502c38b150aSNate Lawson } else { 503c38b150aSNate Lawson ret = start_io(atio, ctio, CAM_DIR_OUT); 504c38b150aSNate Lawson if (debug) 505cd6dedfcSMatt Jacob warnx("Starting %p DIR_OUT @" OFF_FMT ":%u", 506cd6dedfcSMatt Jacob a_descr, c_descr->offset, a_descr->init_req); 507c38b150aSNate Lawson } 508c38b150aSNate Lawson 509c38b150aSNate Lawson return (ret); 510c38b150aSNate Lawson } 511c38b150aSNate Lawson 512c38b150aSNate Lawson static int 513c38b150aSNate Lawson tcmd_rdwr_decode(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio) 514c38b150aSNate Lawson { 515068d70baSHidetoshi Shimokawa uint64_t blkno; 516068d70baSHidetoshi Shimokawa uint32_t count; 517c38b150aSNate Lawson struct atio_descr *a_descr; 518c38b150aSNate Lawson u_int8_t *cdb; 519c38b150aSNate Lawson 520c38b150aSNate Lawson a_descr = (struct atio_descr *)atio->ccb_h.targ_descr; 521c38b150aSNate Lawson cdb = a_descr->cdb; 522c38b150aSNate Lawson if (debug) 523c38b150aSNate Lawson cdb_debug(cdb, "R/W from %u: ", atio->init_id); 524c38b150aSNate Lawson 525068d70baSHidetoshi Shimokawa switch (cdb[0]) { 526068d70baSHidetoshi Shimokawa case READ_6: 527068d70baSHidetoshi Shimokawa case WRITE_6: 528068d70baSHidetoshi Shimokawa { 529c38b150aSNate Lawson struct scsi_rw_6 *rw_6 = (struct scsi_rw_6 *)cdb; 530c38b150aSNate Lawson blkno = scsi_3btoul(rw_6->addr); 531c38b150aSNate Lawson count = rw_6->length; 532068d70baSHidetoshi Shimokawa break; 533068d70baSHidetoshi Shimokawa } 534068d70baSHidetoshi Shimokawa case READ_10: 535068d70baSHidetoshi Shimokawa case WRITE_10: 536068d70baSHidetoshi Shimokawa { 537c38b150aSNate Lawson struct scsi_rw_10 *rw_10 = (struct scsi_rw_10 *)cdb; 538c38b150aSNate Lawson blkno = scsi_4btoul(rw_10->addr); 539c38b150aSNate Lawson count = scsi_2btoul(rw_10->length); 540068d70baSHidetoshi Shimokawa break; 541068d70baSHidetoshi Shimokawa } 542068d70baSHidetoshi Shimokawa #ifdef READ_16 543068d70baSHidetoshi Shimokawa case READ_16: 544068d70baSHidetoshi Shimokawa case WRITE_16: 545068d70baSHidetoshi Shimokawa { 546068d70baSHidetoshi Shimokawa struct scsi_rw_16 *rw_16 = (struct scsi_rw_16 *)cdb; 547068d70baSHidetoshi Shimokawa blkno = scsi_8btou64(rw_16->addr); 548068d70baSHidetoshi Shimokawa count = scsi_4btoul(rw_16->length); 549068d70baSHidetoshi Shimokawa break; 550068d70baSHidetoshi Shimokawa } 551068d70baSHidetoshi Shimokawa #endif 552068d70baSHidetoshi Shimokawa default: 553068d70baSHidetoshi Shimokawa tcmd_illegal_req(atio, ctio); 554068d70baSHidetoshi Shimokawa return (0); 555c38b150aSNate Lawson } 556c38b150aSNate Lawson if (blkno + count > volume_size) { 557c38b150aSNate Lawson warnx("Attempt to access past end of volume"); 558c38b150aSNate Lawson tcmd_sense(ctio->init_id, ctio, 559c38b150aSNate Lawson SSD_KEY_ILLEGAL_REQUEST, 0x21, 0); 560c38b150aSNate Lawson return (0); 561c38b150aSNate Lawson } 562c38b150aSNate Lawson 563c38b150aSNate Lawson /* Get an (overall) data length and set direction */ 564c38b150aSNate Lawson a_descr->base_off = ((off_t)blkno) * sector_size; 565c38b150aSNate Lawson a_descr->total_len = count * sector_size; 566c38b150aSNate Lawson if (a_descr->total_len == 0) { 567c38b150aSNate Lawson if (debug) 568cd6dedfcSMatt Jacob warnx("r/w 0 blocks @ blkno " OFF_FMT, blkno); 569c38b150aSNate Lawson tcmd_null_ok(atio, ctio); 570c38b150aSNate Lawson return (0); 571c38b150aSNate Lawson } else if (cdb[0] == WRITE_6 || cdb[0] == WRITE_10) { 572c38b150aSNate Lawson a_descr->flags |= CAM_DIR_OUT; 573c38b150aSNate Lawson if (debug) 574cd6dedfcSMatt Jacob warnx("write %u blocks @ blkno " OFF_FMT, count, blkno); 575c38b150aSNate Lawson } else { 576c38b150aSNate Lawson a_descr->flags |= CAM_DIR_IN; 577c38b150aSNate Lawson if (debug) 578cd6dedfcSMatt Jacob warnx("read %u blocks @ blkno " OFF_FMT, count, blkno); 579c38b150aSNate Lawson } 580c38b150aSNate Lawson return (1); 581c38b150aSNate Lawson } 582c38b150aSNate Lawson 583c38b150aSNate Lawson static int 584c38b150aSNate Lawson start_io(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio, int dir) 585c38b150aSNate Lawson { 586c38b150aSNate Lawson struct atio_descr *a_descr; 587c38b150aSNate Lawson struct ctio_descr *c_descr; 588c38b150aSNate Lawson int ret; 589c38b150aSNate Lawson 590c38b150aSNate Lawson /* Set up common structures */ 591c38b150aSNate Lawson a_descr = (struct atio_descr *)atio->ccb_h.targ_descr; 592c38b150aSNate Lawson c_descr = (struct ctio_descr *)ctio->ccb_h.targ_descr; 593c38b150aSNate Lawson 594c38b150aSNate Lawson if (dir == CAM_DIR_IN) { 595c38b150aSNate Lawson c_descr->offset = a_descr->base_off + a_descr->targ_req; 596c38b150aSNate Lawson ctio->dxfer_len = a_descr->total_len - a_descr->targ_req; 597c38b150aSNate Lawson } else { 598c38b150aSNate Lawson c_descr->offset = a_descr->base_off + a_descr->init_req; 599c38b150aSNate Lawson ctio->dxfer_len = a_descr->total_len - a_descr->init_req; 600c38b150aSNate Lawson } 601c38b150aSNate Lawson ctio->dxfer_len = min(ctio->dxfer_len, buf_size); 602c38b150aSNate Lawson assert(ctio->dxfer_len >= 0); 603c38b150aSNate Lawson 604c38b150aSNate Lawson c_descr->aiocb.aio_offset = c_descr->offset; 605c38b150aSNate Lawson c_descr->aiocb.aio_nbytes = ctio->dxfer_len; 606c38b150aSNate Lawson 607c38b150aSNate Lawson /* If DIR_IN, start read from target, otherwise begin CTIO xfer. */ 608c38b150aSNate Lawson ret = 1; 609c38b150aSNate Lawson if (dir == CAM_DIR_IN) { 610cd6dedfcSMatt Jacob if (notaio) { 611cd6dedfcSMatt Jacob if (debug) 612cd6dedfcSMatt Jacob warnx("read sync %lud @ block " OFF_FMT, 613cd6dedfcSMatt Jacob (unsigned long) 614cd6dedfcSMatt Jacob (ctio->dxfer_len / sector_size), 615cd6dedfcSMatt Jacob c_descr->offset / sector_size); 616cd6dedfcSMatt Jacob if (lseek(c_descr->aiocb.aio_fildes, 617cd6dedfcSMatt Jacob c_descr->aiocb.aio_offset, SEEK_SET) < 0) { 618cd6dedfcSMatt Jacob perror("lseek"); 619cd6dedfcSMatt Jacob err(1, "lseek"); 620cd6dedfcSMatt Jacob } 621cd6dedfcSMatt Jacob if (read(c_descr->aiocb.aio_fildes, 622cd6dedfcSMatt Jacob (void *)c_descr->aiocb.aio_buf, 623cd6dedfcSMatt Jacob ctio->dxfer_len) != ctio->dxfer_len) { 624cd6dedfcSMatt Jacob err(1, "read"); 625cd6dedfcSMatt Jacob } 626cd6dedfcSMatt Jacob } else { 627cd6dedfcSMatt Jacob if (debug) 628cd6dedfcSMatt Jacob warnx("read async %lud @ block " OFF_FMT, 629cd6dedfcSMatt Jacob (unsigned long) 630cd6dedfcSMatt Jacob (ctio->dxfer_len / sector_size), 631cd6dedfcSMatt Jacob c_descr->offset / sector_size); 632cd6dedfcSMatt Jacob if (aio_read(&c_descr->aiocb) < 0) { 633c38b150aSNate Lawson err(1, "aio_read"); /* XXX */ 634cd6dedfcSMatt Jacob } 635cd6dedfcSMatt Jacob } 636c38b150aSNate Lawson a_descr->targ_req += ctio->dxfer_len; 637cd6dedfcSMatt Jacob /* if we're done, we can mark the CCB as to send status */ 638c38b150aSNate Lawson if (a_descr->targ_req == a_descr->total_len) { 639c38b150aSNate Lawson ctio->ccb_h.flags |= CAM_SEND_STATUS; 640c38b150aSNate Lawson ctio->scsi_status = SCSI_STATUS_OK; 641c38b150aSNate Lawson ret = 0; 642c38b150aSNate Lawson } 643cd6dedfcSMatt Jacob if (notaio) 644cd6dedfcSMatt Jacob tcmd_rdwr_done(atio, ctio, AIO_DONE); 645c38b150aSNate Lawson } else { 646c38b150aSNate Lawson if (a_descr->targ_ack == a_descr->total_len) 647c38b150aSNate Lawson tcmd_null_ok(atio, ctio); 648c38b150aSNate Lawson a_descr->init_req += ctio->dxfer_len; 649c38b150aSNate Lawson if (a_descr->init_req == a_descr->total_len && 650c38b150aSNate Lawson ctio->dxfer_len > 0) { 651c38b150aSNate Lawson /* 652c38b150aSNate Lawson * If data phase done, remove atio from workq. 653c38b150aSNate Lawson * The completion handler will call work_atio to 654c38b150aSNate Lawson * send the final status. 655c38b150aSNate Lawson */ 656c38b150aSNate Lawson ret = 0; 657c38b150aSNate Lawson } 658c38b150aSNate Lawson send_ccb((union ccb *)ctio, /*priority*/1); 659c38b150aSNate Lawson } 660c38b150aSNate Lawson 661c38b150aSNate Lawson return (ret); 662c38b150aSNate Lawson } 663c38b150aSNate Lawson 664c38b150aSNate Lawson static void 665c38b150aSNate Lawson tcmd_rdwr_done(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio, 666c38b150aSNate Lawson io_ops event) 667c38b150aSNate Lawson { 668c38b150aSNate Lawson struct atio_descr *a_descr; 669c38b150aSNate Lawson struct ctio_descr *c_descr; 670c38b150aSNate Lawson 671c38b150aSNate Lawson a_descr = (struct atio_descr *)atio->ccb_h.targ_descr; 672c38b150aSNate Lawson c_descr = (struct ctio_descr *)ctio->ccb_h.targ_descr; 673c38b150aSNate Lawson 674c38b150aSNate Lawson switch (event) { 675c38b150aSNate Lawson case AIO_DONE: 676cd6dedfcSMatt Jacob if (!notaio && aio_return(&c_descr->aiocb) < 0) { 677c38b150aSNate Lawson warn("aio_return error"); 678c38b150aSNate Lawson /* XXX */ 679c38b150aSNate Lawson tcmd_sense(ctio->init_id, ctio, 680c38b150aSNate Lawson SSD_KEY_MEDIUM_ERROR, 0, 0); 681c38b150aSNate Lawson send_ccb((union ccb *)ctio, /*priority*/1); 682c38b150aSNate Lawson break; 683c38b150aSNate Lawson } 684c38b150aSNate Lawson a_descr->targ_ack += ctio->dxfer_len; 685c38b150aSNate Lawson if ((a_descr->flags & CAM_DIR_IN) != 0) { 686cd6dedfcSMatt Jacob if (debug) { 687cd6dedfcSMatt Jacob if (notaio) 688c38b150aSNate Lawson warnx("sending CTIO for AIO read"); 689cd6dedfcSMatt Jacob else 690cd6dedfcSMatt Jacob warnx("sending CTIO for sync read"); 691cd6dedfcSMatt Jacob } 692c38b150aSNate Lawson a_descr->init_req += ctio->dxfer_len; 693c38b150aSNate Lawson send_ccb((union ccb *)ctio, /*priority*/1); 694c38b150aSNate Lawson } else { 695c38b150aSNate Lawson /* Use work function to send final status */ 696c38b150aSNate Lawson if (a_descr->init_req == a_descr->total_len) 697c38b150aSNate Lawson work_atio(atio); 698c38b150aSNate Lawson if (debug) 699c38b150aSNate Lawson warnx("AIO done freeing CTIO"); 700c38b150aSNate Lawson free_ccb((union ccb *)ctio); 701c38b150aSNate Lawson } 702c38b150aSNate Lawson break; 703c38b150aSNate Lawson case CTIO_DONE: 7046070eb3fSMatt Jacob switch (ctio->ccb_h.status & CAM_STATUS_MASK) { 7056070eb3fSMatt Jacob case CAM_REQ_CMP: 7066070eb3fSMatt Jacob break; 7076070eb3fSMatt Jacob case CAM_REQUEUE_REQ: 7086070eb3fSMatt Jacob warnx("requeueing request"); 7096070eb3fSMatt Jacob if ((a_descr->flags & CAM_DIR_MASK) == CAM_DIR_OUT) { 7106070eb3fSMatt Jacob if (aio_write(&c_descr->aiocb) < 0) { 7116070eb3fSMatt Jacob err(1, "aio_write"); /* XXX */ 7126070eb3fSMatt Jacob } 7136070eb3fSMatt Jacob } else { 7146070eb3fSMatt Jacob if (aio_read(&c_descr->aiocb) < 0) { 7156070eb3fSMatt Jacob err(1, "aio_read"); /* XXX */ 7166070eb3fSMatt Jacob } 7176070eb3fSMatt Jacob } 7186070eb3fSMatt Jacob return; 7196070eb3fSMatt Jacob default: 720c38b150aSNate Lawson errx(1, "CTIO failed, status %#x", ctio->ccb_h.status); 721c38b150aSNate Lawson } 722c38b150aSNate Lawson a_descr->init_ack += ctio->dxfer_len; 723c38b150aSNate Lawson if ((a_descr->flags & CAM_DIR_MASK) == CAM_DIR_OUT && 724c38b150aSNate Lawson ctio->dxfer_len > 0) { 725c38b150aSNate Lawson a_descr->targ_req += ctio->dxfer_len; 726cd6dedfcSMatt Jacob if (notaio) { 727cd6dedfcSMatt Jacob if (debug) 728cd6dedfcSMatt Jacob warnx("write sync %lud @ block " 729cd6dedfcSMatt Jacob OFF_FMT, (unsigned long) 730cd6dedfcSMatt Jacob (ctio->dxfer_len / sector_size), 731cd6dedfcSMatt Jacob c_descr->offset / sector_size); 732cd6dedfcSMatt Jacob if (lseek(c_descr->aiocb.aio_fildes, 733cd6dedfcSMatt Jacob c_descr->aiocb.aio_offset, SEEK_SET) < 0) { 734cd6dedfcSMatt Jacob perror("lseek"); 735cd6dedfcSMatt Jacob err(1, "lseek"); 736cd6dedfcSMatt Jacob } 737cd6dedfcSMatt Jacob if (write(c_descr->aiocb.aio_fildes, 738cd6dedfcSMatt Jacob (void *) c_descr->aiocb.aio_buf, 739cd6dedfcSMatt Jacob ctio->dxfer_len) != ctio->dxfer_len) { 740cd6dedfcSMatt Jacob err(1, "write"); 741cd6dedfcSMatt Jacob } 742cd6dedfcSMatt Jacob tcmd_rdwr_done(atio, ctio, AIO_DONE); 743cd6dedfcSMatt Jacob } else { 744cd6dedfcSMatt Jacob if (debug) 745cd6dedfcSMatt Jacob warnx("write async %lud @ block " 746cd6dedfcSMatt Jacob OFF_FMT, (unsigned long) 747cd6dedfcSMatt Jacob (ctio->dxfer_len / sector_size), 748cd6dedfcSMatt Jacob c_descr->offset / sector_size); 749cd6dedfcSMatt Jacob if (aio_write(&c_descr->aiocb) < 0) { 750c38b150aSNate Lawson err(1, "aio_write"); /* XXX */ 751cd6dedfcSMatt Jacob } 752cd6dedfcSMatt Jacob } 753c38b150aSNate Lawson } else { 754c38b150aSNate Lawson if (debug) 755c38b150aSNate Lawson warnx("CTIO done freeing CTIO"); 756c38b150aSNate Lawson free_ccb((union ccb *)ctio); 757c38b150aSNate Lawson } 758c38b150aSNate Lawson break; 759c38b150aSNate Lawson default: 760c38b150aSNate Lawson warnx("Unknown completion code %d", event); 761c38b150aSNate Lawson abort(); 762c38b150aSNate Lawson /* NOTREACHED */ 763c38b150aSNate Lawson } 764c38b150aSNate Lawson } 765c38b150aSNate Lawson 766c38b150aSNate Lawson /* Simple ok message used by TUR, SYNC_CACHE, etc. */ 767c38b150aSNate Lawson static int 768c38b150aSNate Lawson tcmd_null_ok(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio) 769c38b150aSNate Lawson { 770c38b150aSNate Lawson if (debug) { 771c38b150aSNate Lawson struct atio_descr *a_descr; 772c38b150aSNate Lawson 773c38b150aSNate Lawson a_descr = (struct atio_descr *)atio->ccb_h.targ_descr; 774c38b150aSNate Lawson cdb_debug(a_descr->cdb, "Sending null ok to %u : ", atio->init_id); 775c38b150aSNate Lawson } 776c38b150aSNate Lawson 777c38b150aSNate Lawson ctio->dxfer_len = 0; 778c38b150aSNate Lawson ctio->ccb_h.flags &= ~CAM_DIR_MASK; 779c38b150aSNate Lawson ctio->ccb_h.flags |= CAM_DIR_NONE | CAM_SEND_STATUS; 780c38b150aSNate Lawson ctio->scsi_status = SCSI_STATUS_OK; 781c38b150aSNate Lawson return (0); 782c38b150aSNate Lawson } 783c38b150aSNate Lawson 784c38b150aSNate Lawson /* Simple illegal request message used by MODE SENSE, etc. */ 785c38b150aSNate Lawson static int 786c38b150aSNate Lawson tcmd_illegal_req(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio) 787c38b150aSNate Lawson { 788c38b150aSNate Lawson if (debug) { 789c38b150aSNate Lawson struct atio_descr *a_descr; 790c38b150aSNate Lawson 791c38b150aSNate Lawson a_descr = (struct atio_descr *)atio->ccb_h.targ_descr; 792c38b150aSNate Lawson cdb_debug(a_descr->cdb, "Sending ill req to %u: ", atio->init_id); 793c38b150aSNate Lawson } 794c38b150aSNate Lawson 795c38b150aSNate Lawson tcmd_sense(atio->init_id, ctio, SSD_KEY_ILLEGAL_REQUEST, 796c38b150aSNate Lawson /*asc*/0x24, /*ascq*/0); 797c38b150aSNate Lawson return (0); 798c38b150aSNate Lawson } 799c38b150aSNate Lawson 800c38b150aSNate Lawson static void 801c38b150aSNate Lawson cdb_debug(u_int8_t *cdb, const char *msg, ...) 802c38b150aSNate Lawson { 803c38b150aSNate Lawson char msg_buf[512]; 804c38b150aSNate Lawson int len; 805c38b150aSNate Lawson va_list ap; 806c38b150aSNate Lawson 807c38b150aSNate Lawson va_start(ap, msg); 808c38b150aSNate Lawson vsnprintf(msg_buf, sizeof(msg_buf), msg, ap); 809c38b150aSNate Lawson va_end(ap); 810c38b150aSNate Lawson len = strlen(msg_buf); 811c38b150aSNate Lawson scsi_cdb_string(cdb, msg_buf + len, sizeof(msg_buf) - len); 812c38b150aSNate Lawson warnx("%s", msg_buf); 813c38b150aSNate Lawson } 814