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