4 * Copyright (c) 2002 Nate Lawson.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
28 * $FreeBSD: src/share/examples/scsi_target/scsi_cmds.c,v 1.2.2.1 2003/02/18 22:07:10 njl Exp $
38 #include <sys/types.h>
40 #include <bus/cam/cam.h>
41 #include <bus/cam/cam_ccb.h>
42 #include <bus/cam/scsi/scsi_all.h>
43 #include <bus/cam/scsi/scsi_targetio.h>
44 #include "scsi_target.h"
46 typedef int targ_start_func(struct ccb_accept_tio *, struct ccb_scsiio *);
47 typedef void targ_done_func(struct ccb_accept_tio *, struct ccb_scsiio *,
50 struct targ_cdb_handlers {
52 targ_start_func *start;
54 #define ILLEGAL_CDB 0xFF
57 static targ_start_func tcmd_inquiry;
58 static targ_start_func tcmd_req_sense;
59 static targ_start_func tcmd_rd_cap;
60 static targ_start_func tcmd_rdwr;
61 static targ_start_func tcmd_rdwr_decode;
62 static targ_done_func tcmd_rdwr_done;
63 static targ_start_func tcmd_null_ok;
64 static targ_start_func tcmd_illegal_req;
65 static int start_io(struct ccb_accept_tio *atio,
66 struct ccb_scsiio *ctio, int dir);
67 static int init_inquiry(u_int16_t req_flags, u_int16_t sim_flags);
68 static struct initiator_state *
69 tcmd_get_istate(u_int init_id);
70 static void cdb_debug(u_int8_t *cdb, const char *msg, ...);
72 static struct targ_cdb_handlers cdb_handlers[] = {
73 { READ_10, tcmd_rdwr, tcmd_rdwr_done },
74 { WRITE_10, tcmd_rdwr, tcmd_rdwr_done },
75 { READ_6, tcmd_rdwr, tcmd_rdwr_done },
76 { WRITE_6, tcmd_rdwr, tcmd_rdwr_done },
77 { INQUIRY, tcmd_inquiry, NULL },
78 { REQUEST_SENSE, tcmd_req_sense, NULL },
79 { READ_CAPACITY, tcmd_rd_cap, NULL },
80 { TEST_UNIT_READY, tcmd_null_ok, NULL },
81 { START_STOP_UNIT, tcmd_null_ok, NULL },
82 { SYNCHRONIZE_CACHE, tcmd_null_ok, NULL },
83 { MODE_SENSE_6, tcmd_illegal_req, NULL },
84 { MODE_SELECT_6, tcmd_illegal_req, NULL },
85 { ILLEGAL_CDB, NULL, NULL }
88 static struct scsi_inquiry_data inq_data;
89 static struct initiator_state istates[MAX_INITIATORS];
91 extern u_int32_t volume_size;
92 extern size_t sector_size;
93 extern size_t buf_size;
96 tcmd_init(u_int16_t req_inq_flags, u_int16_t sim_inq_flags)
98 struct initiator_state *istate;
101 /* Initialize our inquiry data */
102 ret = init_inquiry(req_inq_flags, sim_inq_flags);
106 /* We start out life with a UA to indicate power-on/reset. */
107 for (i = 0; i < MAX_INITIATORS; i++) {
108 istate = tcmd_get_istate(i);
109 bzero(istate, sizeof(*istate));
110 istate->pending_ua = UA_POWER_ON;
116 /* Caller allocates CTIO, sets its init_id
117 return 0 if done, 1 if more processing needed
118 on 0, caller sets SEND_STATUS */
120 tcmd_handle(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio, io_ops event)
122 static struct targ_cdb_handlers *last_cmd;
123 struct initiator_state *istate;
124 struct atio_descr *a_descr;
128 warnx("tcmd_handle atio %p ctio %p atioflags %#x", atio, ctio,
132 a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
134 /* Do a full lookup if one-behind cache failed */
135 if (last_cmd == NULL || last_cmd->cmd != a_descr->cdb[0]) {
136 struct targ_cdb_handlers *h;
138 for (h = cdb_handlers; h->cmd != ILLEGAL_CDB; h++) {
139 if (a_descr->cdb[0] == h->cmd)
144 if (last_cmd->cmd == ILLEGAL_CDB) {
145 if (event != ATIO_WORK) {
146 warnx("no done func for %#x???", a_descr->cdb[0]);
149 /* Not found, return illegal request */
150 warnx("cdb %#x not handled", a_descr->cdb[0]);
151 tcmd_illegal_req(atio, ctio);
152 send_ccb((union ccb *)ctio, /*priority*/1);
156 /* call completion and exit */
157 if (event != ATIO_WORK) {
158 if (last_cmd->done != NULL)
159 last_cmd->done(atio, ctio, event);
161 free_ccb((union ccb *)ctio);
165 istate = tcmd_get_istate(ctio->init_id);
166 if (istate == NULL) {
167 tcmd_illegal_req(atio, ctio);
168 send_ccb((union ccb *)ctio, /*priority*/1);
172 if (istate->pending_ca == 0 && istate->pending_ua != 0 &&
173 a_descr->cdb[0] != INQUIRY) {
174 tcmd_sense(ctio->init_id, ctio, SSD_KEY_UNIT_ATTENTION,
175 0x29, istate->pending_ua == UA_POWER_ON ? 1 : 2);
176 istate->pending_ca = CA_UNIT_ATTN;
178 cdb_debug(a_descr->cdb, "UA active for %u: ",
181 send_ccb((union ccb *)ctio, /*priority*/1);
185 /* Store current CA and UA for later */
186 istate->orig_ua = istate->pending_ua;
187 istate->orig_ca = istate->pending_ca;
190 * As per SAM2, any command that occurs
191 * after a CA is reported, clears the CA. We must
192 * also clear the UA condition, if any, that caused
193 * the CA to occur assuming the UA is not for a
194 * persistent condition.
196 istate->pending_ca = CA_NONE;
197 if (istate->orig_ca == CA_UNIT_ATTN)
198 istate->pending_ua = UA_NONE;
200 /* If we have a valid handler, call start or completion function */
201 if (last_cmd->cmd != ILLEGAL_CDB) {
202 ret = last_cmd->start(atio, ctio);
204 if (last_cmd->start != tcmd_rdwr) {
205 a_descr->init_req += ctio->dxfer_len;
206 send_ccb((union ccb *)ctio, /*priority*/1);
213 static struct initiator_state *
214 tcmd_get_istate(u_int init_id)
216 if (init_id >= MAX_INITIATORS) {
217 warnx("illegal init_id %d, max %d", init_id, MAX_INITIATORS - 1);
220 return (&istates[init_id]);
225 tcmd_sense(u_int init_id, struct ccb_scsiio *ctio, u_int8_t flags,
226 u_int8_t asc, u_int8_t ascq)
228 struct initiator_state *istate;
229 struct scsi_sense_data *sense;
231 /* Set our initiator's istate */
232 istate = tcmd_get_istate(init_id);
235 istate->pending_ca |= CA_CMD_SENSE; /* XXX set instead of or? */
236 sense = &istate->sense_data;
237 bzero(sense, sizeof(*sense));
238 sense->error_code = SSD_CURRENT_ERROR;
239 sense->flags = flags;
240 sense->add_sense_code = asc;
241 sense->add_sense_code_qual = ascq;
243 offsetof(struct scsi_sense_data, sense_key_spec[2]) -
244 offsetof(struct scsi_sense_data, extra_len);
246 /* Fill out the supplied CTIO */
249 bcopy(sense, &ctio->sense_data, sizeof(*sense));
250 ctio->sense_len = sizeof(*sense); XXX
252 ctio->ccb_h.flags &= ~CAM_DIR_MASK;
253 ctio->ccb_h.flags |= CAM_DIR_NONE | /* CAM_SEND_SENSE | */
256 ctio->scsi_status = SCSI_STATUS_CHECK_COND;
261 tcmd_ua(u_int init_id, ua_types new_ua)
263 struct initiator_state *istate;
266 if (init_id == CAM_TARGET_WILDCARD) {
268 end = MAX_INITIATORS - 1;
270 start = end = init_id;
273 for (; start <= end; start++) {
274 istate = tcmd_get_istate(start);
277 istate->pending_ua = new_ua;
282 tcmd_inquiry(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio)
284 struct scsi_inquiry *inq;
285 struct atio_descr *a_descr;
286 struct initiator_state *istate;
287 struct scsi_sense_data *sense;
289 a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
290 inq = (struct scsi_inquiry *)a_descr->cdb;
293 cdb_debug(a_descr->cdb, "INQUIRY from %u: ", atio->init_id);
295 * Validate the command. We don't support any VPD pages, so
296 * complain if EVPD or CMDDT is set.
298 istate = tcmd_get_istate(ctio->init_id);
299 sense = &istate->sense_data;
300 if ((inq->byte2 & SI_EVPD) != 0) {
301 tcmd_illegal_req(atio, ctio);
302 sense->sense_key_spec[0] = SSD_SCS_VALID | SSD_FIELDPTR_CMD |
303 SSD_BITPTR_VALID | /*bit value*/1;
304 sense->sense_key_spec[1] = 0;
305 sense->sense_key_spec[2] =
306 offsetof(struct scsi_inquiry, byte2);
307 } else if (inq->page_code != 0) {
308 tcmd_illegal_req(atio, ctio);
309 sense->sense_key_spec[0] = SSD_SCS_VALID | SSD_FIELDPTR_CMD;
310 sense->sense_key_spec[1] = 0;
311 sense->sense_key_spec[2] =
312 offsetof(struct scsi_inquiry, page_code);
314 bcopy(&inq_data, ctio->data_ptr, sizeof(inq_data));
315 ctio->dxfer_len = inq_data.additional_length + 4;
316 ctio->dxfer_len = min(ctio->dxfer_len,
317 SCSI_CDB6_LEN(inq->length));
318 ctio->ccb_h.flags |= CAM_DIR_IN | CAM_SEND_STATUS;
319 ctio->scsi_status = SCSI_STATUS_OK;
324 /* Initialize the inquiry response structure with the requested flags */
326 init_inquiry(u_int16_t req_flags, u_int16_t sim_flags)
328 struct scsi_inquiry_data *inq;
331 bzero(inq, sizeof(*inq));
332 inq->device = T_DIRECT | (SID_QUAL_LU_CONNECTED << 5);
333 inq->version = SCSI_REV_3; /* was 2 */
336 * XXX cpi.hba_inquiry doesn't support Addr16 so we give the
337 * user what they want if they ask for it.
339 if ((req_flags & SID_Addr16) != 0) {
340 sim_flags |= SID_Addr16;
341 warnx("Not sure SIM supports Addr16 but enabling it anyway");
344 /* Advertise only what the SIM can actually support */
345 req_flags &= sim_flags;
346 scsi_ulto2b(req_flags, &inq->reserved[1]);
348 inq->response_format = 2; /* SCSI2 Inquiry Format */
349 inq->additional_length = SHORT_INQUIRY_LENGTH -
350 offsetof(struct scsi_inquiry_data, additional_length);
351 bcopy("FreeBSD ", inq->vendor, SID_VENDOR_SIZE);
352 bcopy("Emulated Disk ", inq->product, SID_PRODUCT_SIZE);
353 bcopy("0.1 ", inq->revision, SID_REVISION_SIZE);
358 tcmd_req_sense(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio)
360 struct scsi_request_sense *rsense;
361 struct scsi_sense_data *sense;
362 struct initiator_state *istate;
364 struct atio_descr *a_descr;
366 a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
367 rsense = (struct scsi_request_sense *)a_descr->cdb;
369 istate = tcmd_get_istate(ctio->init_id);
370 sense = &istate->sense_data;
373 cdb_debug(a_descr->cdb, "REQ SENSE from %u: ", atio->init_id);
374 warnx("Sending sense: %#x %#x %#x", sense->flags,
375 sense->add_sense_code, sense->add_sense_code_qual);
378 if (istate->orig_ca == 0) {
379 tcmd_sense(ctio->init_id, NULL, SSD_KEY_NO_SENSE, 0, 0);
380 warnx("REQUEST SENSE from %u but no pending CA!",
384 bcopy(sense, ctio->data_ptr, sizeof(struct scsi_sense_data));
385 dlen = offsetof(struct scsi_sense_data, extra_len) +
386 sense->extra_len + 1;
387 ctio->dxfer_len = min(dlen, SCSI_CDB6_LEN(rsense->length));
388 ctio->ccb_h.flags |= CAM_DIR_IN | CAM_SEND_STATUS;
389 ctio->scsi_status = SCSI_STATUS_OK;
394 tcmd_rd_cap(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio)
396 struct scsi_read_capacity_data *srp;
397 struct atio_descr *a_descr;
399 a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
400 srp = (struct scsi_read_capacity_data *)ctio->data_ptr;
403 cdb_debug(a_descr->cdb, "READ CAP from %u (%u, %u): ",
404 atio->init_id, volume_size - 1, sector_size);
407 bzero(srp, sizeof(*srp));
408 scsi_ulto4b(volume_size - 1, srp->addr);
409 scsi_ulto4b(sector_size, srp->length);
411 ctio->dxfer_len = sizeof(*srp);
412 ctio->ccb_h.flags |= CAM_DIR_IN | CAM_SEND_STATUS;
413 ctio->scsi_status = SCSI_STATUS_OK;
418 tcmd_rdwr(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio)
420 struct atio_descr *a_descr;
421 struct ctio_descr *c_descr;
424 a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
425 c_descr = (struct ctio_descr *)ctio->ccb_h.targ_descr;
427 /* Command needs to be decoded */
428 if ((a_descr->flags & CAM_DIR_MASK) == CAM_DIR_RESV) {
430 warnx("Calling rdwr_decode");
431 ret = tcmd_rdwr_decode(atio, ctio);
433 send_ccb((union ccb *)ctio, /*priority*/1);
437 ctio->ccb_h.flags |= a_descr->flags;
439 /* Call appropriate work function */
440 if ((a_descr->flags & CAM_DIR_IN) != 0) {
441 ret = start_io(atio, ctio, CAM_DIR_IN);
443 warnx("Starting DIR_IN @%lld:%u", c_descr->offset,
446 ret = start_io(atio, ctio, CAM_DIR_OUT);
448 warnx("Starting DIR_OUT @%lld:%u", c_descr->offset,
456 tcmd_rdwr_decode(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio)
458 u_int32_t blkno, count;
459 struct atio_descr *a_descr;
462 a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
465 cdb_debug(cdb, "R/W from %u: ", atio->init_id);
467 if (cdb[0] == READ_6 || cdb[0] == WRITE_6) {
468 struct scsi_rw_6 *rw_6 = (struct scsi_rw_6 *)cdb;
469 blkno = scsi_3btoul(rw_6->addr);
470 count = rw_6->length;
472 struct scsi_rw_10 *rw_10 = (struct scsi_rw_10 *)cdb;
473 blkno = scsi_4btoul(rw_10->addr);
474 count = scsi_2btoul(rw_10->length);
476 if (blkno + count > volume_size) {
477 warnx("Attempt to access past end of volume");
478 tcmd_sense(ctio->init_id, ctio,
479 SSD_KEY_ILLEGAL_REQUEST, 0x21, 0);
483 /* Get an (overall) data length and set direction */
484 a_descr->base_off = ((off_t)blkno) * sector_size;
485 a_descr->total_len = count * sector_size;
486 if (a_descr->total_len == 0) {
488 warnx("r/w 0 blocks @ blkno %u", blkno);
489 tcmd_null_ok(atio, ctio);
491 } else if (cdb[0] == WRITE_6 || cdb[0] == WRITE_10) {
492 a_descr->flags |= CAM_DIR_OUT;
494 warnx("write %u blocks @ blkno %u", count, blkno);
496 a_descr->flags |= CAM_DIR_IN;
498 warnx("read %u blocks @ blkno %u", count, blkno);
504 start_io(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio, int dir)
506 struct atio_descr *a_descr;
507 struct ctio_descr *c_descr;
510 /* Set up common structures */
511 a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
512 c_descr = (struct ctio_descr *)ctio->ccb_h.targ_descr;
514 if (dir == CAM_DIR_IN) {
515 c_descr->offset = a_descr->base_off + a_descr->targ_req;
516 ctio->dxfer_len = a_descr->total_len - a_descr->targ_req;
518 c_descr->offset = a_descr->base_off + a_descr->init_req;
519 ctio->dxfer_len = a_descr->total_len - a_descr->init_req;
521 ctio->dxfer_len = min(ctio->dxfer_len, buf_size);
522 assert(ctio->dxfer_len >= 0);
524 c_descr->aiocb.aio_offset = c_descr->offset;
525 c_descr->aiocb.aio_nbytes = ctio->dxfer_len;
527 /* If DIR_IN, start read from target, otherwise begin CTIO xfer. */
529 if (dir == CAM_DIR_IN) {
530 if (aio_read(&c_descr->aiocb) < 0)
531 err(1, "aio_read"); /* XXX */
532 a_descr->targ_req += ctio->dxfer_len;
533 if (a_descr->targ_req == a_descr->total_len) {
534 ctio->ccb_h.flags |= CAM_SEND_STATUS;
535 ctio->scsi_status = SCSI_STATUS_OK;
539 if (a_descr->targ_ack == a_descr->total_len)
540 tcmd_null_ok(atio, ctio);
541 a_descr->init_req += ctio->dxfer_len;
542 if (a_descr->init_req == a_descr->total_len &&
543 ctio->dxfer_len > 0) {
545 * If data phase done, remove atio from workq.
546 * The completion handler will call work_atio to
547 * send the final status.
551 send_ccb((union ccb *)ctio, /*priority*/1);
558 tcmd_rdwr_done(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio,
561 struct atio_descr *a_descr;
562 struct ctio_descr *c_descr;
564 a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
565 c_descr = (struct ctio_descr *)ctio->ccb_h.targ_descr;
569 if (aio_return(&c_descr->aiocb) < 0) {
570 warn("aio_return error");
572 tcmd_sense(ctio->init_id, ctio,
573 SSD_KEY_MEDIUM_ERROR, 0, 0);
574 send_ccb((union ccb *)ctio, /*priority*/1);
577 a_descr->targ_ack += ctio->dxfer_len;
578 if ((a_descr->flags & CAM_DIR_IN) != 0) {
580 warnx("sending CTIO for AIO read");
581 a_descr->init_req += ctio->dxfer_len;
582 send_ccb((union ccb *)ctio, /*priority*/1);
584 /* Use work function to send final status */
585 if (a_descr->init_req == a_descr->total_len)
588 warnx("AIO done freeing CTIO");
589 free_ccb((union ccb *)ctio);
593 if (ctio->ccb_h.status != CAM_REQ_CMP) {
595 errx(1, "CTIO failed, status %#x", ctio->ccb_h.status);
597 a_descr->init_ack += ctio->dxfer_len;
598 if ((a_descr->flags & CAM_DIR_MASK) == CAM_DIR_OUT &&
599 ctio->dxfer_len > 0) {
601 warnx("sending AIO for CTIO write");
602 a_descr->targ_req += ctio->dxfer_len;
603 if (aio_write(&c_descr->aiocb) < 0)
604 err(1, "aio_write"); /* XXX */
607 warnx("CTIO done freeing CTIO");
608 free_ccb((union ccb *)ctio);
612 warnx("Unknown completion code %d", event);
618 /* Simple ok message used by TUR, SYNC_CACHE, etc. */
620 tcmd_null_ok(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio)
623 struct atio_descr *a_descr;
625 a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
626 cdb_debug(a_descr->cdb, "Sending null ok to %u : ", atio->init_id);
630 ctio->ccb_h.flags &= ~CAM_DIR_MASK;
631 ctio->ccb_h.flags |= CAM_DIR_NONE | CAM_SEND_STATUS;
632 ctio->scsi_status = SCSI_STATUS_OK;
636 /* Simple illegal request message used by MODE SENSE, etc. */
638 tcmd_illegal_req(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio)
641 struct atio_descr *a_descr;
643 a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
644 cdb_debug(a_descr->cdb, "Sending ill req to %u: ", atio->init_id);
647 tcmd_sense(atio->init_id, ctio, SSD_KEY_ILLEGAL_REQUEST,
648 /*asc*/0x24, /*ascq*/0);
653 cdb_debug(u_int8_t *cdb, const char *msg, ...)
660 vsnprintf(msg_buf, sizeof(msg_buf), msg, ap);
662 len = strlen(msg_buf);
663 scsi_cdb_string(cdb, msg_buf + len, sizeof(msg_buf) - len);
664 warnx("%s", msg_buf);