Path: blob/main/share/examples/scsi_target/scsi_cmds.c
39476 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* SCSI Disk Emulator4*5* Copyright (c) 2002 Nate Lawson.6* All rights reserved.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions, and the following disclaimer,13* without modification, immediately at the beginning of the file.14* 2. The name of the author may not be used to endorse or promote products15* derived from this software without specific prior written permission.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR21* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*/2930#include <stdio.h>31#include <stddef.h>32#include <stdarg.h>33#include <stdlib.h>34#include <string.h>35#include <err.h>36#include <aio.h>37#include <unistd.h>38#include <assert.h>39#include <sys/param.h>40#include <sys/types.h>4142#include <cam/cam.h>43#include <cam/cam_ccb.h>44#include <cam/scsi/scsi_all.h>45#include <cam/scsi/scsi_targetio.h>46#include "scsi_target.h"4748typedef int targ_start_func(struct ccb_accept_tio *, struct ccb_scsiio *);49typedef void targ_done_func(struct ccb_accept_tio *, struct ccb_scsiio *,50io_ops);51#ifndef REPORT_LUNS52#define REPORT_LUNS 0xa053#endif5455struct targ_cdb_handlers {56u_int8_t cmd;57targ_start_func *start;58targ_done_func *done;59#define ILLEGAL_CDB 0xFF60};6162static targ_start_func tcmd_inquiry;63static targ_start_func tcmd_req_sense;64static targ_start_func tcmd_rd_cap;65#ifdef READ_1666static targ_start_func tcmd_rd_cap16;67#endif68static targ_start_func tcmd_rdwr;69static targ_start_func tcmd_rdwr_decode;70static targ_done_func tcmd_rdwr_done;71static targ_start_func tcmd_null_ok;72static targ_start_func tcmd_illegal_req;73static int start_io(struct ccb_accept_tio *atio,74struct ccb_scsiio *ctio, int dir);75static int init_inquiry(u_int16_t req_flags, u_int16_t sim_flags);76static struct initiator_state *77tcmd_get_istate(u_int init_id);78static void cdb_debug(u_int8_t *cdb, const char *msg, ...);7980static struct targ_cdb_handlers cdb_handlers[] = {81{ READ_10, tcmd_rdwr, tcmd_rdwr_done },82{ WRITE_10, tcmd_rdwr, tcmd_rdwr_done },83{ READ_6, tcmd_rdwr, tcmd_rdwr_done },84{ WRITE_6, tcmd_rdwr, tcmd_rdwr_done },85{ INQUIRY, tcmd_inquiry, NULL },86{ REQUEST_SENSE, tcmd_req_sense, NULL },87{ READ_CAPACITY, tcmd_rd_cap, NULL },88{ TEST_UNIT_READY, tcmd_null_ok, NULL },89{ START_STOP_UNIT, tcmd_null_ok, NULL },90{ SYNCHRONIZE_CACHE, tcmd_null_ok, NULL },91{ MODE_SENSE_6, tcmd_illegal_req, NULL },92{ MODE_SELECT_6, tcmd_illegal_req, NULL },93{ REPORT_LUNS, tcmd_illegal_req, NULL },94#ifdef READ_1695{ READ_16, tcmd_rdwr, tcmd_rdwr_done },96{ WRITE_16, tcmd_rdwr, tcmd_rdwr_done },97{ SERVICE_ACTION_IN, tcmd_rd_cap16, NULL },98#endif99{ ILLEGAL_CDB, NULL, NULL }100};101102static struct scsi_inquiry_data inq_data;103static struct initiator_state istates[MAX_INITIATORS];104105cam_status106tcmd_init(u_int16_t req_inq_flags, u_int16_t sim_inq_flags)107{108struct initiator_state *istate;109int i, ret;110111/* Initialize our inquiry data */112ret = init_inquiry(req_inq_flags, sim_inq_flags);113if (ret != 0)114return (ret);115116/* We start out life with a UA to indicate power-on/reset. */117for (i = 0; i < MAX_INITIATORS; i++) {118istate = tcmd_get_istate(i);119bzero(istate, sizeof(*istate));120istate->pending_ua = UA_POWER_ON;121}122123return (0);124}125126/* Caller allocates CTIO, sets its init_id127return 0 if done, 1 if more processing needed128on 0, caller sets SEND_STATUS */129int130tcmd_handle(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio, io_ops event)131{132static struct targ_cdb_handlers *last_cmd;133struct initiator_state *istate;134struct atio_descr *a_descr;135int ret;136137if (debug) {138warnx("tcmd_handle atio %p ctio %p atioflags %#x", atio, ctio,139atio->ccb_h.flags);140}141ret = 0;142a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;143144/* Do a full lookup if one-behind cache failed */145if (last_cmd == NULL || last_cmd->cmd != a_descr->cdb[0]) {146struct targ_cdb_handlers *h;147148for (h = cdb_handlers; h->cmd != ILLEGAL_CDB; h++) {149if (a_descr->cdb[0] == h->cmd)150break;151}152last_cmd = h;153}154155/* call completion and exit */156if (event != ATIO_WORK) {157if (last_cmd->done != NULL)158last_cmd->done(atio, ctio, event);159else160free_ccb((union ccb *)ctio);161return (1);162}163164if (last_cmd->cmd == ILLEGAL_CDB) {165if (event != ATIO_WORK) {166warnx("no done func for %#x???", a_descr->cdb[0]);167abort();168}169/* Not found, return illegal request */170warnx("cdb %#x not handled", a_descr->cdb[0]);171tcmd_illegal_req(atio, ctio);172send_ccb((union ccb *)ctio, /*priority*/1);173return (0);174}175176istate = tcmd_get_istate(ctio->init_id);177if (istate == NULL) {178tcmd_illegal_req(atio, ctio);179send_ccb((union ccb *)ctio, /*priority*/1);180return (0);181}182183if (istate->pending_ca == 0 && istate->pending_ua != 0 &&184a_descr->cdb[0] != INQUIRY) {185tcmd_sense(ctio->init_id, ctio, SSD_KEY_UNIT_ATTENTION,1860x29, istate->pending_ua == UA_POWER_ON ? 1 : 2);187istate->pending_ca = CA_UNIT_ATTN;188if (debug) {189cdb_debug(a_descr->cdb, "UA active for %u: ",190atio->init_id);191}192send_ccb((union ccb *)ctio, /*priority*/1);193return (0);194}195196/* Store current CA and UA for later */197istate->orig_ua = istate->pending_ua;198istate->orig_ca = istate->pending_ca;199200/*201* As per SAM2, any command that occurs202* after a CA is reported, clears the CA. We must203* also clear the UA condition, if any, that caused204* the CA to occur assuming the UA is not for a205* persistent condition.206*/207istate->pending_ca = CA_NONE;208if (istate->orig_ca == CA_UNIT_ATTN)209istate->pending_ua = UA_NONE;210211/* If we have a valid handler, call start or completion function */212if (last_cmd->cmd != ILLEGAL_CDB) {213ret = last_cmd->start(atio, ctio);214/* XXX hack */215if (last_cmd->start != tcmd_rdwr) {216a_descr->init_req += ctio->dxfer_len;217send_ccb((union ccb *)ctio, /*priority*/1);218}219}220221return (ret);222}223224static struct initiator_state *225tcmd_get_istate(u_int init_id)226{227if (init_id >= MAX_INITIATORS) {228warnx("illegal init_id %d, max %d", init_id, MAX_INITIATORS - 1);229return (NULL);230} else {231return (&istates[init_id]);232}233}234235void236tcmd_sense(u_int init_id, struct ccb_scsiio *ctio, u_int8_t flags,237u_int8_t asc, u_int8_t ascq)238{239struct initiator_state *istate;240struct scsi_sense_data_fixed *sense;241242/* Set our initiator's istate */243istate = tcmd_get_istate(init_id);244if (istate == NULL)245return;246istate->pending_ca |= CA_CMD_SENSE; /* XXX set instead of or? */247sense = (struct scsi_sense_data_fixed *)&istate->sense_data;248bzero(sense, sizeof(*sense));249sense->error_code = SSD_CURRENT_ERROR;250sense->flags = flags;251sense->add_sense_code = asc;252sense->add_sense_code_qual = ascq;253sense->extra_len =254offsetof(struct scsi_sense_data_fixed, sense_key_spec[2]) -255offsetof(struct scsi_sense_data_fixed, extra_len);256257/* Fill out the supplied CTIO */258if (ctio != NULL) {259bcopy(sense, &ctio->sense_data, sizeof(*sense));260ctio->sense_len = sizeof(*sense); /* XXX */261ctio->ccb_h.flags &= ~CAM_DIR_MASK;262ctio->ccb_h.flags |= CAM_DIR_NONE | CAM_SEND_SENSE |263CAM_SEND_STATUS;264ctio->dxfer_len = 0;265ctio->scsi_status = SCSI_STATUS_CHECK_COND;266}267}268269void270tcmd_ua(u_int init_id, ua_types new_ua)271{272struct initiator_state *istate;273u_int start, end;274275if (init_id == CAM_TARGET_WILDCARD) {276start = 0;277end = MAX_INITIATORS - 1;278} else {279start = end = init_id;280}281282for (; start <= end; start++) {283istate = tcmd_get_istate(start);284if (istate == NULL)285break;286istate->pending_ua = new_ua;287}288}289290static int291tcmd_inquiry(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio)292{293struct scsi_inquiry *inq;294struct atio_descr *a_descr;295struct initiator_state *istate;296struct scsi_sense_data_fixed *sense;297298a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;299inq = (struct scsi_inquiry *)a_descr->cdb;300301if (debug)302cdb_debug(a_descr->cdb, "INQUIRY from %u: ", atio->init_id);303/*304* Validate the command. We don't support any VPD pages, so305* complain if EVPD or CMDDT is set.306*/307istate = tcmd_get_istate(ctio->init_id);308sense = (struct scsi_sense_data_fixed *)&istate->sense_data;309if ((inq->byte2 & SI_EVPD) != 0) {310tcmd_illegal_req(atio, ctio);311sense->sense_key_spec[0] = SSD_SCS_VALID | SSD_FIELDPTR_CMD |312SSD_BITPTR_VALID | /*bit value*/1;313sense->sense_key_spec[1] = 0;314sense->sense_key_spec[2] =315offsetof(struct scsi_inquiry, byte2);316} else if (inq->page_code != 0) {317tcmd_illegal_req(atio, ctio);318sense->sense_key_spec[0] = SSD_SCS_VALID | SSD_FIELDPTR_CMD;319sense->sense_key_spec[1] = 0;320sense->sense_key_spec[2] =321offsetof(struct scsi_inquiry, page_code);322} else {323bcopy(&inq_data, ctio->data_ptr, sizeof(inq_data));324ctio->dxfer_len = inq_data.additional_length + 4;325ctio->dxfer_len = min(ctio->dxfer_len,326scsi_2btoul(inq->length));327ctio->ccb_h.flags |= CAM_DIR_IN | CAM_SEND_STATUS;328ctio->scsi_status = SCSI_STATUS_OK;329}330return (0);331}332333/* Initialize the inquiry response structure with the requested flags */334static int335init_inquiry(u_int16_t req_flags, u_int16_t sim_flags)336{337struct scsi_inquiry_data *inq;338339inq = &inq_data;340bzero(inq, sizeof(*inq));341inq->device = T_DIRECT | (SID_QUAL_LU_CONNECTED << 5);342#ifdef SCSI_REV_SPC343inq->version = SCSI_REV_SPC; /* was 2 */344#else345inq->version = SCSI_REV_3; /* was 2 */346#endif347348/*349* XXX cpi.hba_inquiry doesn't support Addr16 so we give the350* user what they want if they ask for it.351*/352if ((req_flags & SID_Addr16) != 0) {353sim_flags |= SID_Addr16;354warnx("Not sure SIM supports Addr16 but enabling it anyway");355}356357/* Advertise only what the SIM can actually support */358req_flags &= sim_flags;359scsi_ulto2b(req_flags, &inq->spc2_flags);360361inq->response_format = 2; /* SCSI2 Inquiry Format */362inq->additional_length = SHORT_INQUIRY_LENGTH -363offsetof(struct scsi_inquiry_data, additional_length);364bcopy("FreeBSD ", inq->vendor, SID_VENDOR_SIZE);365bcopy("Emulated Disk ", inq->product, SID_PRODUCT_SIZE);366bcopy("0.1 ", inq->revision, SID_REVISION_SIZE);367return (0);368}369370static int371tcmd_req_sense(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio)372{373struct scsi_request_sense *rsense;374struct scsi_sense_data_fixed *sense;375struct initiator_state *istate;376size_t dlen;377struct atio_descr *a_descr;378379a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;380rsense = (struct scsi_request_sense *)a_descr->cdb;381382istate = tcmd_get_istate(ctio->init_id);383sense = (struct scsi_sense_data_fixed *)&istate->sense_data;384385if (debug) {386cdb_debug(a_descr->cdb, "REQ SENSE from %u: ", atio->init_id);387warnx("Sending sense: %#x %#x %#x", sense->flags,388sense->add_sense_code, sense->add_sense_code_qual);389}390391if (istate->orig_ca == 0) {392tcmd_sense(ctio->init_id, NULL, SSD_KEY_NO_SENSE, 0, 0);393warnx("REQUEST SENSE from %u but no pending CA!",394ctio->init_id);395}396397bcopy(sense, ctio->data_ptr, sizeof(struct scsi_sense_data));398dlen = offsetof(struct scsi_sense_data_fixed, extra_len) +399sense->extra_len + 1;400ctio->dxfer_len = min(dlen, SCSI_CDB6_LEN(rsense->length));401ctio->ccb_h.flags |= CAM_DIR_IN | CAM_SEND_STATUS;402ctio->scsi_status = SCSI_STATUS_OK;403return (0);404}405406static int407tcmd_rd_cap(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio)408{409struct scsi_read_capacity_data *srp;410struct atio_descr *a_descr;411uint32_t vsize;412413a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;414srp = (struct scsi_read_capacity_data *)ctio->data_ptr;415416if (volume_size > 0xffffffff)417vsize = 0xffffffff;418else419vsize = (uint32_t)(volume_size - 1);420421if (debug) {422cdb_debug(a_descr->cdb, "READ CAP from %u (%u, %u): ",423atio->init_id, vsize, sector_size);424}425426bzero(srp, sizeof(*srp));427scsi_ulto4b(vsize, srp->addr);428scsi_ulto4b(sector_size, srp->length);429430ctio->dxfer_len = sizeof(*srp);431ctio->ccb_h.flags |= CAM_DIR_IN | CAM_SEND_STATUS;432ctio->scsi_status = SCSI_STATUS_OK;433return (0);434}435436#ifdef READ_16437static int438tcmd_rd_cap16(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio)439{440struct scsi_read_capacity_16 *scsi_cmd;441struct scsi_read_capacity_data_long *srp;442struct atio_descr *a_descr;443444a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;445scsi_cmd = (struct scsi_read_capacity_16 *)a_descr->cdb;446srp = (struct scsi_read_capacity_data_long *)ctio->data_ptr;447448if (scsi_cmd->service_action != SRC16_SERVICE_ACTION) {449tcmd_illegal_req(atio, ctio);450return (0);451}452453if (debug) {454cdb_debug(a_descr->cdb, "READ CAP16 from %u (%u, %u): ",455atio->init_id, volume_size - 1, sector_size);456}457458bzero(srp, sizeof(*srp));459scsi_u64to8b(volume_size - 1, srp->addr);460scsi_ulto4b(sector_size, srp->length);461462ctio->dxfer_len = sizeof(*srp);463ctio->ccb_h.flags |= CAM_DIR_IN | CAM_SEND_STATUS;464ctio->scsi_status = SCSI_STATUS_OK;465return (0);466}467#endif468469static int470tcmd_rdwr(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio)471{472struct atio_descr *a_descr;473struct ctio_descr *c_descr;474int ret;475476a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;477c_descr = (struct ctio_descr *)ctio->ccb_h.targ_descr;478479/* Command needs to be decoded */480if ((a_descr->flags & CAM_DIR_MASK) == CAM_DIR_BOTH) {481if (debug)482warnx("Calling rdwr_decode");483ret = tcmd_rdwr_decode(atio, ctio);484if (ret == 0) {485send_ccb((union ccb *)ctio, /*priority*/1);486return (0);487}488}489ctio->ccb_h.flags |= a_descr->flags;490491/* Call appropriate work function */492if ((a_descr->flags & CAM_DIR_IN) != 0) {493ret = start_io(atio, ctio, CAM_DIR_IN);494if (debug)495warnx("Starting %p DIR_IN @" OFF_FMT ":%u",496a_descr, c_descr->offset, a_descr->targ_req);497} else {498ret = start_io(atio, ctio, CAM_DIR_OUT);499if (debug)500warnx("Starting %p DIR_OUT @" OFF_FMT ":%u",501a_descr, c_descr->offset, a_descr->init_req);502}503504return (ret);505}506507static int508tcmd_rdwr_decode(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio)509{510uint64_t blkno;511uint32_t count;512struct atio_descr *a_descr;513u_int8_t *cdb;514515a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;516cdb = a_descr->cdb;517if (debug)518cdb_debug(cdb, "R/W from %u: ", atio->init_id);519520switch (cdb[0]) {521case READ_6:522case WRITE_6:523{524struct scsi_rw_6 *rw_6 = (struct scsi_rw_6 *)cdb;525blkno = scsi_3btoul(rw_6->addr);526count = rw_6->length;527break;528}529case READ_10:530case WRITE_10:531{532struct scsi_rw_10 *rw_10 = (struct scsi_rw_10 *)cdb;533blkno = scsi_4btoul(rw_10->addr);534count = scsi_2btoul(rw_10->length);535break;536}537#ifdef READ_16538case READ_16:539case WRITE_16:540{541struct scsi_rw_16 *rw_16 = (struct scsi_rw_16 *)cdb;542blkno = scsi_8btou64(rw_16->addr);543count = scsi_4btoul(rw_16->length);544break;545}546#endif547default:548tcmd_illegal_req(atio, ctio);549return (0);550}551if (((off_t)(blkno + count)) > volume_size) {552warnx("Attempt to access past end of volume");553tcmd_sense(ctio->init_id, ctio,554SSD_KEY_ILLEGAL_REQUEST, 0x21, 0);555return (0);556}557558/* Get an (overall) data length and set direction */559a_descr->base_off = ((off_t)blkno) * sector_size;560a_descr->total_len = count * sector_size;561if (a_descr->total_len == 0) {562if (debug)563warnx("r/w 0 blocks @ blkno " OFF_FMT, blkno);564tcmd_null_ok(atio, ctio);565return (0);566} else if (cdb[0] == WRITE_6 || cdb[0] == WRITE_10) {567a_descr->flags |= CAM_DIR_OUT;568if (debug)569warnx("write %u blocks @ blkno " OFF_FMT, count, blkno);570} else {571a_descr->flags |= CAM_DIR_IN;572if (debug)573warnx("read %u blocks @ blkno " OFF_FMT, count, blkno);574}575return (1);576}577578static int579start_io(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio, int dir)580{581struct atio_descr *a_descr;582struct ctio_descr *c_descr;583int ret;584585/* Set up common structures */586a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;587c_descr = (struct ctio_descr *)ctio->ccb_h.targ_descr;588589if (dir == CAM_DIR_IN) {590c_descr->offset = a_descr->base_off + a_descr->targ_req;591ctio->dxfer_len = a_descr->total_len - a_descr->targ_req;592} else {593c_descr->offset = a_descr->base_off + a_descr->init_req;594ctio->dxfer_len = a_descr->total_len - a_descr->init_req;595}596ctio->dxfer_len = min(ctio->dxfer_len, buf_size);597assert(ctio->dxfer_len >= 0);598599c_descr->aiocb.aio_offset = c_descr->offset;600c_descr->aiocb.aio_nbytes = ctio->dxfer_len;601602/* If DIR_IN, start read from target, otherwise begin CTIO xfer. */603ret = 1;604if (dir == CAM_DIR_IN) {605if (notaio) {606if (debug)607warnx("read sync %lu @ block " OFF_FMT,608(unsigned long)609(ctio->dxfer_len / sector_size),610c_descr->offset / sector_size);611if (lseek(c_descr->aiocb.aio_fildes,612c_descr->aiocb.aio_offset, SEEK_SET) < 0) {613perror("lseek");614err(1, "lseek");615}616if (read(c_descr->aiocb.aio_fildes,617(void *)c_descr->aiocb.aio_buf,618ctio->dxfer_len) != ctio->dxfer_len) {619err(1, "read");620}621} else {622if (debug)623warnx("read async %lu @ block " OFF_FMT,624(unsigned long)625(ctio->dxfer_len / sector_size),626c_descr->offset / sector_size);627if (aio_read(&c_descr->aiocb) < 0) {628err(1, "aio_read"); /* XXX */629}630}631a_descr->targ_req += ctio->dxfer_len;632/* if we're done, we can mark the CCB as to send status */633if (a_descr->targ_req == a_descr->total_len) {634ctio->ccb_h.flags |= CAM_SEND_STATUS;635ctio->scsi_status = SCSI_STATUS_OK;636ret = 0;637}638if (notaio)639tcmd_rdwr_done(atio, ctio, AIO_DONE);640} else {641if (a_descr->targ_ack == a_descr->total_len)642tcmd_null_ok(atio, ctio);643a_descr->init_req += ctio->dxfer_len;644if (a_descr->init_req == a_descr->total_len &&645ctio->dxfer_len > 0) {646/*647* If data phase done, remove atio from workq.648* The completion handler will call work_atio to649* send the final status.650*/651ret = 0;652}653send_ccb((union ccb *)ctio, /*priority*/1);654}655656return (ret);657}658659static void660tcmd_rdwr_done(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio,661io_ops event)662{663struct atio_descr *a_descr;664struct ctio_descr *c_descr;665666a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;667c_descr = (struct ctio_descr *)ctio->ccb_h.targ_descr;668669switch (event) {670case AIO_DONE:671if (!notaio && aio_return(&c_descr->aiocb) < 0) {672warn("aio_return error");673/* XXX */674tcmd_sense(ctio->init_id, ctio,675SSD_KEY_MEDIUM_ERROR, 0, 0);676send_ccb((union ccb *)ctio, /*priority*/1);677break;678}679a_descr->targ_ack += ctio->dxfer_len;680if ((a_descr->flags & CAM_DIR_IN) != 0) {681if (debug) {682if (notaio)683warnx("sending CTIO for AIO read");684else685warnx("sending CTIO for sync read");686}687a_descr->init_req += ctio->dxfer_len;688send_ccb((union ccb *)ctio, /*priority*/1);689} else {690/* Use work function to send final status */691if (a_descr->init_req == a_descr->total_len)692work_atio(atio);693if (debug)694warnx("AIO done freeing CTIO");695free_ccb((union ccb *)ctio);696}697break;698case CTIO_DONE:699switch (ctio->ccb_h.status & CAM_STATUS_MASK) {700case CAM_REQ_CMP:701break;702case CAM_REQUEUE_REQ:703warnx("requeueing request");704if ((a_descr->flags & CAM_DIR_MASK) == CAM_DIR_OUT) {705if (aio_write(&c_descr->aiocb) < 0) {706err(1, "aio_write"); /* XXX */707}708} else {709if (aio_read(&c_descr->aiocb) < 0) {710err(1, "aio_read"); /* XXX */711}712}713return;714default:715errx(1, "CTIO failed, status %#x", ctio->ccb_h.status);716}717a_descr->init_ack += ctio->dxfer_len;718if ((a_descr->flags & CAM_DIR_MASK) == CAM_DIR_OUT &&719ctio->dxfer_len > 0) {720a_descr->targ_req += ctio->dxfer_len;721if (notaio) {722if (debug)723warnx("write sync %lu @ block "724OFF_FMT, (unsigned long)725(ctio->dxfer_len / sector_size),726c_descr->offset / sector_size);727if (lseek(c_descr->aiocb.aio_fildes,728c_descr->aiocb.aio_offset, SEEK_SET) < 0) {729perror("lseek");730err(1, "lseek");731}732if (write(c_descr->aiocb.aio_fildes,733(void *) c_descr->aiocb.aio_buf,734ctio->dxfer_len) != ctio->dxfer_len) {735err(1, "write");736}737tcmd_rdwr_done(atio, ctio, AIO_DONE);738} else {739if (debug)740warnx("write async %lu @ block "741OFF_FMT, (unsigned long)742(ctio->dxfer_len / sector_size),743c_descr->offset / sector_size);744if (aio_write(&c_descr->aiocb) < 0) {745err(1, "aio_write"); /* XXX */746}747}748} else {749if (debug)750warnx("CTIO done freeing CTIO");751free_ccb((union ccb *)ctio);752}753break;754default:755warnx("Unknown completion code %d", event);756abort();757/* NOTREACHED */758}759}760761/* Simple ok message used by TUR, SYNC_CACHE, etc. */762static int763tcmd_null_ok(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio)764{765if (debug) {766struct atio_descr *a_descr;767768a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;769cdb_debug(a_descr->cdb, "Sending null ok to %u : ", atio->init_id);770}771772ctio->dxfer_len = 0;773ctio->ccb_h.flags &= ~CAM_DIR_MASK;774ctio->ccb_h.flags |= CAM_DIR_NONE | CAM_SEND_STATUS;775ctio->scsi_status = SCSI_STATUS_OK;776return (0);777}778779/* Simple illegal request message used by MODE SENSE, etc. */780static int781tcmd_illegal_req(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio)782{783if (debug) {784struct atio_descr *a_descr;785786a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;787cdb_debug(a_descr->cdb, "Sending ill req to %u: ", atio->init_id);788}789790tcmd_sense(atio->init_id, ctio, SSD_KEY_ILLEGAL_REQUEST,791/*asc*/0x24, /*ascq*/0);792return (0);793}794795static void796cdb_debug(u_int8_t *cdb, const char *msg, ...)797{798char msg_buf[512];799int len;800va_list ap;801802va_start(ap, msg);803vsnprintf(msg_buf, sizeof(msg_buf), msg, ap);804va_end(ap);805len = strlen(msg_buf);806scsi_cdb_string(cdb, msg_buf + len, sizeof(msg_buf) - len);807warnx("%s", msg_buf);808}809810811