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