scsi_target.c revision 121184
139215Sgibbs/*
2107178Snjl * SCSI Disk Emulator
339215Sgibbs *
4107178Snjl * Copyright (c) 2002 Nate Lawson.
539215Sgibbs * All rights reserved.
639215Sgibbs *
739215Sgibbs * Redistribution and use in source and binary forms, with or without
839215Sgibbs * modification, are permitted provided that the following conditions
939215Sgibbs * are met:
1039215Sgibbs * 1. Redistributions of source code must retain the above copyright
1139215Sgibbs *    notice, this list of conditions, and the following disclaimer,
1239215Sgibbs *    without modification, immediately at the beginning of the file.
1339215Sgibbs * 2. The name of the author may not be used to endorse or promote products
1439215Sgibbs *    derived from this software without specific prior written permission.
1539215Sgibbs *
1639215Sgibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1739215Sgibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1839215Sgibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1939215Sgibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
2039215Sgibbs * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2139215Sgibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2239215Sgibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2339215Sgibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2439215Sgibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2539215Sgibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2639215Sgibbs * SUCH DAMAGE.
2739215Sgibbs *
2850476Speter * $FreeBSD: head/share/examples/scsi_target/scsi_target.c 121184 2003-10-18 04:54:08Z simokawa $
2939215Sgibbs */
3039215Sgibbs
3139215Sgibbs#include <sys/types.h>
32121184Ssimokawa#include <ctype.h>
3344498Sgibbs#include <errno.h>
34107178Snjl#include <err.h>
3539215Sgibbs#include <fcntl.h>
3644498Sgibbs#include <signal.h>
3739215Sgibbs#include <stddef.h>
3839215Sgibbs#include <stdio.h>
3939215Sgibbs#include <stdlib.h>
40107178Snjl#include <string.h>
4139215Sgibbs#include <sysexits.h>
4239215Sgibbs#include <unistd.h>
43107178Snjl#include <aio.h>
44109161Snjl#include <assert.h>
45107178Snjl#include <sys/stat.h>
46107178Snjl#include <sys/queue.h>
47107178Snjl#include <sys/event.h>
48107178Snjl#include <sys/param.h>
49120428Ssimokawa#include <sys/disk.h>
50107178Snjl#include <cam/cam_queue.h>
5139215Sgibbs#include <cam/scsi/scsi_all.h>
52107178Snjl#include <cam/scsi/scsi_targetio.h>
5339215Sgibbs#include <cam/scsi/scsi_message.h>
54107178Snjl#include "scsi_target.h"
5539215Sgibbs
56107178Snjl/* Maximum amount to transfer per CTIO */
57107178Snjl#define MAX_XFER	MAXPHYS
58107178Snjl/* Maximum number of allocated CTIOs */
59107178Snjl#define MAX_CTIOS	32
60107178Snjl/* Maximum sector size for emulated volume */
61107178Snjl#define MAX_SECTOR	32768
62107178Snjl
63107178Snjl/* Global variables */
64107178Snjlint		debug;
65121184Ssimokawaoff_t		volume_size;
66121184Ssimokawau_int		sector_size;
67107178Snjlsize_t		buf_size;
68107178Snjl
69107178Snjl/* Local variables */
70107178Snjlstatic int    targ_fd;
71107178Snjlstatic int    kq_fd;
72107178Snjlstatic int    file_fd;
73107178Snjlstatic int    num_ctios;
74107178Snjlstatic struct ccb_queue		pending_queue;
75107178Snjlstatic struct ccb_queue		work_queue;
76107178Snjlstatic struct ioc_enable_lun	ioc_enlun = {
7744498Sgibbs	CAM_BUS_WILDCARD,
7844498Sgibbs	CAM_TARGET_WILDCARD,
7944498Sgibbs	CAM_LUN_WILDCARD
8044498Sgibbs};
8139215Sgibbs
82107178Snjl/* Local functions */
83107178Snjlstatic void		cleanup(void);
84107178Snjlstatic int		init_ccbs(void);
85107178Snjlstatic void		request_loop(void);
86107178Snjlstatic void		handle_read(void);
87107178Snjl/* static int		work_atio(struct ccb_accept_tio *); */
88107178Snjlstatic void		queue_io(struct ccb_scsiio *);
89107178Snjlstatic void		run_queue(struct ccb_accept_tio *);
90107178Snjlstatic int		work_inot(struct ccb_immed_notify *);
91107178Snjlstatic struct ccb_scsiio *
92107178Snjl			get_ctio(void);
93107178Snjl/* static void		free_ccb(union ccb *); */
94107178Snjlstatic cam_status	get_sim_flags(u_int16_t *);
95107178Snjlstatic void		rel_simq(void);
96107178Snjlstatic void		abort_all_pending(void);
97107178Snjlstatic void		usage(void);
9839215Sgibbs
9939215Sgibbsint
10039215Sgibbsmain(int argc, char *argv[])
10139215Sgibbs{
102107178Snjl	int ch, unit;
103107178Snjl	char *file_name, targname[16];
104107178Snjl	u_int16_t req_flags, sim_flags;
105107178Snjl	off_t user_size;
10639215Sgibbs
107107178Snjl	/* Initialize */
108107178Snjl	debug = 0;
109107178Snjl	req_flags = sim_flags = 0;
110107178Snjl	user_size = 0;
111107178Snjl	targ_fd = file_fd = kq_fd = -1;
112107178Snjl	num_ctios = 0;
113107178Snjl	sector_size = SECTOR_SIZE;
114107178Snjl	buf_size = DFLTPHYS;
115107178Snjl
116107178Snjl	/* Prepare resource pools */
117107178Snjl	TAILQ_INIT(&pending_queue);
118107178Snjl	TAILQ_INIT(&work_queue);
119107178Snjl
120107178Snjl	while ((ch = getopt(argc, argv, "AdSTb:c:s:W:")) != -1) {
12139215Sgibbs		switch(ch) {
122107178Snjl		case 'A':
123107178Snjl			req_flags |= SID_Addr16;
12439215Sgibbs			break;
125107178Snjl		case 'd':
126107178Snjl			debug = 1;
12739215Sgibbs			break;
128107178Snjl		case 'S':
129107178Snjl			req_flags |= SID_Sync;
13044498Sgibbs			break;
131107178Snjl		case 'T':
132107178Snjl			req_flags |= SID_CmdQue;
13344498Sgibbs			break;
134107178Snjl		case 'b':
135107178Snjl			buf_size = atoi(optarg);
136107178Snjl			if (buf_size < 256 || buf_size > MAX_XFER)
137107178Snjl				errx(1, "Unreasonable buf size: %s", optarg);
13844498Sgibbs			break;
139107178Snjl		case 'c':
140107178Snjl			sector_size = atoi(optarg);
141107178Snjl			if (sector_size < 512 || sector_size > MAX_SECTOR)
142107178Snjl				errx(1, "Unreasonable sector size: %s", optarg);
14363185Smjacob			break;
144107178Snjl		case 's':
145121184Ssimokawa		{
146121184Ssimokawa			int last, shift = 0;
147121184Ssimokawa
148121184Ssimokawa			last = strlen(optarg) - 1;
149121184Ssimokawa			if (last > 0) {
150121184Ssimokawa				switch (tolower(optarg[last])) {
151121184Ssimokawa				case 'e':
152121184Ssimokawa					shift += 10;
153121184Ssimokawa					/* FALLTHROUGH */
154121184Ssimokawa				case 'p':
155121184Ssimokawa					shift += 10;
156121184Ssimokawa					/* FALLTHROUGH */
157121184Ssimokawa				case 't':
158121184Ssimokawa					shift += 10;
159121184Ssimokawa					/* FALLTHROUGH */
160121184Ssimokawa				case 'g':
161121184Ssimokawa					shift += 10;
162121184Ssimokawa					/* FALLTHROUGH */
163121184Ssimokawa				case 'm':
164121184Ssimokawa					shift += 10;
165121184Ssimokawa					/* FALLTHROUGH */
166121184Ssimokawa				case 'k':
167121184Ssimokawa					shift += 10;
168121184Ssimokawa					optarg[last] = 0;
169121184Ssimokawa					break;
170121184Ssimokawa				}
171121184Ssimokawa			}
172107178Snjl			user_size = strtoll(optarg, (char **)NULL, /*base*/10);
173121184Ssimokawa			user_size <<= shift;
174107178Snjl			if (user_size < 0)
175107178Snjl				errx(1, "Unreasonable volume size: %s", optarg);
176107178Snjl			break;
177121184Ssimokawa		}
178107178Snjl		case 'W':
179107178Snjl			req_flags &= ~(SID_WBus16 | SID_WBus32);
180107178Snjl			switch (atoi(optarg)) {
181107178Snjl			case 8:
182107178Snjl				/* Leave req_flags zeroed */
183107178Snjl				break;
184107178Snjl			case 16:
185107178Snjl				req_flags |= SID_WBus16;
186107178Snjl				break;
187107178Snjl			case 32:
188107178Snjl				req_flags |= SID_WBus32;
189107178Snjl				break;
190107178Snjl			default:
191107178Snjl				warnx("Width %s not supported", optarg);
192107178Snjl				usage();
193107178Snjl				/* NOTREACHED */
194107178Snjl			}
195107178Snjl			break;
19639215Sgibbs		default:
19739215Sgibbs			usage();
19839215Sgibbs			/* NOTREACHED */
19939215Sgibbs		}
20039215Sgibbs	}
20139215Sgibbs	argc -= optind;
20239215Sgibbs	argv += optind;
203107178Snjl
204107178Snjl	if (argc != 2)
20539215Sgibbs		usage();
20639215Sgibbs
207107178Snjl	sscanf(argv[0], "%u:%u:%u", &ioc_enlun.path_id, &ioc_enlun.target_id,
208107178Snjl	       &ioc_enlun.lun_id);
209107178Snjl	file_name = argv[1];
210107178Snjl
211107178Snjl	if (ioc_enlun.path_id == CAM_BUS_WILDCARD ||
212107178Snjl	    ioc_enlun.target_id == CAM_TARGET_WILDCARD ||
213107178Snjl	    ioc_enlun.lun_id == CAM_LUN_WILDCARD) {
214107178Snjl		warnx("Incomplete target path specified");
21544498Sgibbs		usage();
21644498Sgibbs		/* NOTREACHED */
21744498Sgibbs	}
218107178Snjl	/* We don't support any vendor-specific commands */
219107178Snjl	ioc_enlun.grp6_len = 0;
220107178Snjl	ioc_enlun.grp7_len = 0;
22144498Sgibbs
222107178Snjl	/* Open backing store for IO */
223107178Snjl	file_fd = open(file_name, O_RDWR);
224107178Snjl	if (file_fd < 0)
225107178Snjl		err(1, "open backing store file");
22644498Sgibbs
227107178Snjl	/* Check backing store size or use the size user gave us */
228107178Snjl	if (user_size == 0) {
229107178Snjl		struct stat st;
230107178Snjl
231107178Snjl		if (fstat(file_fd, &st) < 0)
232107178Snjl			err(1, "fstat file");
233120428Ssimokawa#if __FreeBSD_version >= 500000
234120428Ssimokawa		if ((st.st_mode & S_IFCHR) != 0) {
235120428Ssimokawa			/* raw device */
236120428Ssimokawa			off_t mediasize;
237120428Ssimokawa			if (ioctl(file_fd, DIOCGMEDIASIZE, &mediasize) < 0)
238120428Ssimokawa				err(1, "DIOCGMEDIASIZE");
239120428Ssimokawa
240120428Ssimokawa			/* XXX get sector size by ioctl()?? */
241120428Ssimokawa			volume_size = mediasize / sector_size;
242120428Ssimokawa		} else
243120428Ssimokawa#endif
244120428Ssimokawa			volume_size = st.st_size / sector_size;
245107178Snjl	} else {
246107178Snjl		volume_size = user_size / sector_size;
24744498Sgibbs	}
248121184Ssimokawa	if (debug)
249121184Ssimokawa#if __FreeBSD_version >= 500000
250121184Ssimokawa		warnx("volume_size: %d bytes x %jd sectors",
251121184Ssimokawa#else
252121184Ssimokawa		warnx("volume_size: %d bytes x %lld sectors",
253121184Ssimokawa#endif
254121184Ssimokawa		    sector_size, volume_size);
255121184Ssimokawa
256107178Snjl	if (volume_size <= 0)
257107178Snjl		errx(1, "volume must be larger than %d", sector_size);
25844498Sgibbs
259109161Snjl	{
260109161Snjl		struct aiocb aio, *aiop;
261109161Snjl
262109161Snjl		/* Make sure we have working AIO support */
263109161Snjl		memset(&aio, 0, sizeof(aio));
264109161Snjl		aio.aio_buf = malloc(sector_size);
265109161Snjl		if (aio.aio_buf == NULL)
266109161Snjl			err(1, "malloc");
267109161Snjl		aio.aio_fildes = file_fd;
268109161Snjl		aio.aio_offset = 0;
269109161Snjl		aio.aio_nbytes = sector_size;
270109161Snjl		signal(SIGSYS, SIG_IGN);
271109161Snjl		if (aio_read(&aio) != 0) {
272109161Snjl			printf("You must enable VFS_AIO in your kernel "
273109161Snjl			       "or load the aio(4) module.\n");
274109161Snjl			err(1, "aio_read");
275109161Snjl		}
276109161Snjl		if (aio_waitcomplete(&aiop, NULL) != sector_size)
277109161Snjl			err(1, "aio_waitcomplete");
278109161Snjl		assert(aiop == &aio);
279109161Snjl		signal(SIGSYS, SIG_DFL);
280109161Snjl		free((void *)aio.aio_buf);
281109161Snjl		if (debug)
282109161Snjl			warnx("aio support tested ok");
283109161Snjl	}
284109161Snjl
285107178Snjl	/* Go through all the control devices and find one that isn't busy. */
286107178Snjl	unit = 0;
287107178Snjl	do {
288107178Snjl		snprintf(targname, sizeof(targname), "/dev/targ%d", unit++);
289107178Snjl    		targ_fd = open(targname, O_RDWR);
290107178Snjl	} while (targ_fd < 0 && errno == EBUSY);
29144498Sgibbs
292107178Snjl	if (targ_fd < 0)
293107178Snjl    	    err(1, "Tried to open %d devices, none available", unit);
29463185Smjacob
295107178Snjl	/* The first three are handled by kevent() later */
296107178Snjl	signal(SIGHUP, SIG_IGN);
297107178Snjl	signal(SIGINT, SIG_IGN);
298107178Snjl	signal(SIGTERM, SIG_IGN);
299107178Snjl	signal(SIGPROF, SIG_IGN);
300107178Snjl	signal(SIGALRM, SIG_IGN);
301107178Snjl	signal(SIGSTOP, SIG_IGN);
302107178Snjl	signal(SIGTSTP, SIG_IGN);
30339215Sgibbs
304107178Snjl	/* Register a cleanup handler to run when exiting */
305107178Snjl	atexit(cleanup);
306107178Snjl
307107178Snjl	/* Enable listening on the specified LUN */
308107178Snjl	if (ioctl(targ_fd, TARGIOCENABLE, &ioc_enlun) != 0)
309107178Snjl		err(1, "TARGIOCENABLE");
310107178Snjl
311107178Snjl	/* Enable debugging if requested */
312107178Snjl	if (debug) {
313107178Snjl		if (ioctl(targ_fd, TARGIOCDEBUG, &debug) != 0)
314107178Snjl			err(1, "TARGIOCDEBUG");
31539215Sgibbs	}
31639215Sgibbs
317107178Snjl	/* Set up inquiry data according to what SIM supports */
318107178Snjl	if (get_sim_flags(&sim_flags) != CAM_REQ_CMP)
319107178Snjl		errx(1, "get_sim_flags");
320107178Snjl	if (tcmd_init(req_flags, sim_flags) != 0)
321107178Snjl		errx(1, "Initializing tcmd subsystem failed");
32244498Sgibbs
323107178Snjl	/* Queue ATIOs and INOTs on descriptor */
324107178Snjl	if (init_ccbs() != 0)
325107178Snjl		errx(1, "init_ccbs failed");
32649935Sgibbs
327107178Snjl	if (debug)
328107178Snjl		warnx("main loop beginning");
329107178Snjl	request_loop();
33039215Sgibbs
331107178Snjl	exit(0);
33249935Sgibbs}
33349935Sgibbs
33449935Sgibbsstatic void
33549935Sgibbscleanup()
33649935Sgibbs{
337107178Snjl	struct ccb_hdr *ccb_h;
338107178Snjl
33963290Smjacob	if (debug) {
340107178Snjl		warnx("cleanup called");
34163290Smjacob		debug = 0;
342107178Snjl		ioctl(targ_fd, TARGIOCDEBUG, &debug);
34363290Smjacob	}
344107178Snjl	ioctl(targ_fd, TARGIOCDISABLE, NULL);
345107178Snjl	close(targ_fd);
346107178Snjl
347107178Snjl	while ((ccb_h = TAILQ_FIRST(&pending_queue)) != NULL) {
348107178Snjl		TAILQ_REMOVE(&pending_queue, ccb_h, periph_links.tqe);
349107178Snjl		free_ccb((union ccb *)ccb_h);
35044498Sgibbs	}
351107178Snjl	while ((ccb_h = TAILQ_FIRST(&work_queue)) != NULL) {
352107178Snjl		TAILQ_REMOVE(&work_queue, ccb_h, periph_links.tqe);
353107178Snjl		free_ccb((union ccb *)ccb_h);
354107178Snjl	}
355107178Snjl
356107178Snjl	if (kq_fd != -1)
357107178Snjl		close(kq_fd);
35839215Sgibbs}
35939215Sgibbs
360107178Snjl/* Allocate ATIOs/INOTs and queue on HBA */
361107178Snjlstatic int
362107178Snjlinit_ccbs()
363107178Snjl{
364107178Snjl	int i;
365107178Snjl
366107178Snjl	for (i = 0; i < MAX_INITIATORS; i++) {
367107178Snjl		struct ccb_accept_tio *atio;
368107178Snjl		struct atio_descr *a_descr;
369107178Snjl		struct ccb_immed_notify *inot;
370107178Snjl
371107178Snjl		atio = (struct ccb_accept_tio *)malloc(sizeof(*atio));
372107178Snjl		if (atio == NULL) {
373107178Snjl			warn("malloc ATIO");
374107178Snjl			return (-1);
375107178Snjl		}
376107178Snjl		a_descr = (struct atio_descr *)malloc(sizeof(*a_descr));
377107178Snjl		if (a_descr == NULL) {
378107178Snjl			free(atio);
379107178Snjl			warn("malloc atio_descr");
380107178Snjl			return (-1);
381107178Snjl		}
382107178Snjl		atio->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
383107178Snjl		atio->ccb_h.targ_descr = a_descr;
384107178Snjl		send_ccb((union ccb *)atio, /*priority*/1);
385107178Snjl
386107178Snjl		inot = (struct ccb_immed_notify *)malloc(sizeof(*inot));
387107178Snjl		if (inot == NULL) {
388107178Snjl			warn("malloc INOT");
389107178Snjl			return (-1);
390107178Snjl		}
391107178Snjl		inot->ccb_h.func_code = XPT_IMMED_NOTIFY;
392107178Snjl		send_ccb((union ccb *)inot, /*priority*/1);
393107178Snjl	}
394107178Snjl
395107178Snjl	return (0);
396107178Snjl}
397107178Snjl
39839215Sgibbsstatic void
399107178Snjlrequest_loop()
40039215Sgibbs{
401107178Snjl	struct kevent events[MAX_EVENTS];
402107178Snjl	struct timespec ts, *tptr;
403107178Snjl	int quit;
40439215Sgibbs
405107178Snjl	/* Register kqueue for event notification */
406107178Snjl	if ((kq_fd = kqueue()) < 0)
407107178Snjl		err(1, "init kqueue");
40839215Sgibbs
409107178Snjl	/* Set up some default events */
410107178Snjl	EV_SET(&events[0], SIGHUP, EVFILT_SIGNAL, EV_ADD|EV_ENABLE, 0, 0, 0);
411107178Snjl	EV_SET(&events[1], SIGINT, EVFILT_SIGNAL, EV_ADD|EV_ENABLE, 0, 0, 0);
412107178Snjl	EV_SET(&events[2], SIGTERM, EVFILT_SIGNAL, EV_ADD|EV_ENABLE, 0, 0, 0);
413107178Snjl	EV_SET(&events[3], targ_fd, EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, 0);
414107178Snjl	if (kevent(kq_fd, events, 4, NULL, 0, NULL) < 0)
415107178Snjl		err(1, "kevent signal registration");
416107178Snjl
417107178Snjl	ts.tv_sec = 0;
418107178Snjl	ts.tv_nsec = 0;
419107178Snjl	tptr = NULL;
420107178Snjl	quit = 0;
421107178Snjl
422107178Snjl	/* Loop until user signal */
42344498Sgibbs	while (quit == 0) {
424107178Snjl		int retval, i;
425107178Snjl		struct ccb_hdr *ccb_h;
42639215Sgibbs
427107178Snjl		/* Check for the next signal, read ready, or AIO completion */
428107178Snjl		retval = kevent(kq_fd, NULL, 0, events, MAX_EVENTS, tptr);
429107178Snjl		if (retval < 0) {
430107178Snjl			if (errno == EINTR) {
431107178Snjl				if (debug)
432107178Snjl					warnx("EINTR, looping");
43344498Sgibbs				continue;
434107178Snjl            		}
435107178Snjl			else {
436107178Snjl				err(1, "kevent failed");
437107178Snjl			}
438107178Snjl		} else if (retval > MAX_EVENTS) {
439107178Snjl			errx(1, "kevent returned more events than allocated?");
44039215Sgibbs		}
44139215Sgibbs
442107178Snjl		/* Process all received events. */
443107178Snjl		for (i = 0; i < retval; i++) {
444107178Snjl			if ((events[i].flags & EV_ERROR) != 0)
445107178Snjl				errx(1, "kevent registration failed");
44639215Sgibbs
447107178Snjl			switch (events[i].filter) {
448107178Snjl			case EVFILT_READ:
449107178Snjl				if (debug)
450107178Snjl					warnx("read ready");
451107178Snjl				handle_read();
452107178Snjl				break;
453107178Snjl			case EVFILT_AIO:
454107178Snjl			{
455107178Snjl				struct ccb_scsiio *ctio;
456107178Snjl				struct ctio_descr *c_descr;
457107178Snjl				if (debug)
458107178Snjl					warnx("aio ready");
45939215Sgibbs
460107178Snjl				ctio = (struct ccb_scsiio *)events[i].udata;
461107178Snjl				c_descr = (struct ctio_descr *)
462107178Snjl					  ctio->ccb_h.targ_descr;
463107178Snjl				c_descr->event = AIO_DONE;
464107178Snjl				/* Queue on the appropriate ATIO */
465107178Snjl				queue_io(ctio);
466107178Snjl				/* Process any queued completions. */
467107178Snjl				run_queue(c_descr->atio);
468107178Snjl				break;
469107178Snjl			}
470107178Snjl			case EVFILT_SIGNAL:
471107178Snjl				if (debug)
472107178Snjl					warnx("signal ready, setting quit");
473107178Snjl				quit = 1;
474107178Snjl				break;
475107178Snjl			default:
476107178Snjl				warnx("unknown event %#x", events[i].filter);
477107178Snjl				break;
478107178Snjl			}
479107178Snjl
480107178Snjl			if (debug)
481107178Snjl				warnx("event done");
48239215Sgibbs		}
48339215Sgibbs
484107178Snjl		/* Grab the first CCB and perform one work unit. */
485107178Snjl		if ((ccb_h = TAILQ_FIRST(&work_queue)) != NULL) {
486107178Snjl			union ccb *ccb;
48739215Sgibbs
488107178Snjl			ccb = (union ccb *)ccb_h;
489107178Snjl			switch (ccb_h->func_code) {
490107178Snjl			case XPT_ACCEPT_TARGET_IO:
491107178Snjl				/* Start one more transfer. */
492107178Snjl				retval = work_atio(&ccb->atio);
493107178Snjl				break;
494107178Snjl			case XPT_IMMED_NOTIFY:
495107178Snjl				retval = work_inot(&ccb->cin);
496107178Snjl				break;
497107178Snjl			default:
498107178Snjl				warnx("Unhandled ccb type %#x on workq",
499107178Snjl				      ccb_h->func_code);
500107178Snjl				abort();
501107178Snjl				/* NOTREACHED */
50239215Sgibbs			}
50339215Sgibbs
504107178Snjl			/* Assume work function handled the exception */
505107178Snjl			if ((ccb_h->status & CAM_DEV_QFRZN) != 0) {
506109345Snjl				if (debug) {
507109345Snjl					warnx("Queue frozen receiving CCB, "
508109345Snjl					      "releasing");
509109345Snjl				}
510107178Snjl				rel_simq();
51139215Sgibbs			}
51239215Sgibbs
513107178Snjl			/* No more work needed for this command. */
514107178Snjl			if (retval == 0) {
515107178Snjl				TAILQ_REMOVE(&work_queue, ccb_h,
516107178Snjl					     periph_links.tqe);
51739215Sgibbs			}
518107178Snjl		}
51939215Sgibbs
520107178Snjl		/*
521107178Snjl		 * Poll for new events (i.e. completions) while we
522107178Snjl		 * are processing CCBs on the work_queue. Once it's
523107178Snjl		 * empty, use an infinite wait.
524107178Snjl		 */
525107178Snjl		if (!TAILQ_EMPTY(&work_queue))
526107178Snjl			tptr = &ts;
527107178Snjl		else
528107178Snjl			tptr = NULL;
52939215Sgibbs	}
53039215Sgibbs}
53139215Sgibbs
532107178Snjl/* CCBs are ready from the kernel */
53339215Sgibbsstatic void
534107178Snjlhandle_read()
53539215Sgibbs{
536107178Snjl	union ccb *ccb_array[MAX_INITIATORS], *ccb;
537107178Snjl	int ccb_count, i;
53839215Sgibbs
539107178Snjl	ccb_count = read(targ_fd, ccb_array, sizeof(ccb_array));
540107178Snjl	if (ccb_count <= 0) {
541107178Snjl		warn("read ccb ptrs");
542107178Snjl		return;
54339215Sgibbs	}
544107178Snjl	ccb_count /= sizeof(union ccb *);
545107178Snjl	if (ccb_count < 1) {
546107178Snjl		warnx("truncated read ccb ptr?");
547107178Snjl		return;
548107178Snjl	}
54939215Sgibbs
550107178Snjl	for (i = 0; i < ccb_count; i++) {
551107178Snjl		ccb = ccb_array[i];
552107178Snjl		TAILQ_REMOVE(&pending_queue, &ccb->ccb_h, periph_links.tqe);
553107178Snjl
554107178Snjl		switch (ccb->ccb_h.func_code) {
555107178Snjl		case XPT_ACCEPT_TARGET_IO:
556107178Snjl		{
557107178Snjl			struct ccb_accept_tio *atio;
558107178Snjl			struct atio_descr *a_descr;
559107178Snjl
560107178Snjl			/* Initialize ATIO descr for this transaction */
561107178Snjl			atio = &ccb->atio;
562107178Snjl			a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
563107178Snjl			bzero(a_descr, sizeof(*a_descr));
564107178Snjl			TAILQ_INIT(&a_descr->cmplt_io);
565107178Snjl			a_descr->flags = atio->ccb_h.flags &
566107178Snjl				(CAM_DIS_DISCONNECT | CAM_TAG_ACTION_VALID);
567107178Snjl			/* XXX add a_descr->priority */
568107178Snjl			if ((atio->ccb_h.flags & CAM_CDB_POINTER) == 0)
569107178Snjl				a_descr->cdb = atio->cdb_io.cdb_bytes;
570107178Snjl			else
571107178Snjl				a_descr->cdb = atio->cdb_io.cdb_ptr;
572107178Snjl
573107178Snjl			/* ATIOs are processed in FIFO order */
574107178Snjl			TAILQ_INSERT_TAIL(&work_queue, &ccb->ccb_h,
575107178Snjl					  periph_links.tqe);
576107178Snjl			break;
577107178Snjl		}
578107178Snjl		case XPT_CONT_TARGET_IO:
579107178Snjl		{
580107178Snjl			struct ccb_scsiio *ctio;
581107178Snjl			struct ctio_descr *c_descr;
582107178Snjl
583107178Snjl			ctio = &ccb->ctio;
584107178Snjl			c_descr = (struct ctio_descr *)ctio->ccb_h.targ_descr;
585107178Snjl			c_descr->event = CTIO_DONE;
586107178Snjl			/* Queue on the appropriate ATIO */
587107178Snjl			queue_io(ctio);
588107178Snjl			/* Process any queued completions. */
589107178Snjl			run_queue(c_descr->atio);
590107178Snjl			break;
591107178Snjl		}
592107178Snjl		case XPT_IMMED_NOTIFY:
593107178Snjl			/* INOTs are handled with priority */
594107178Snjl			TAILQ_INSERT_HEAD(&work_queue, &ccb->ccb_h,
595107178Snjl					  periph_links.tqe);
596107178Snjl			break;
597107178Snjl		default:
598107178Snjl			warnx("Unhandled ccb type %#x in handle_read",
599107178Snjl			      ccb->ccb_h.func_code);
600107178Snjl			break;
601107178Snjl		}
60239215Sgibbs	}
603107178Snjl}
60439215Sgibbs
605107178Snjl/* Process an ATIO CCB from the kernel */
606107178Snjlint
607107178Snjlwork_atio(struct ccb_accept_tio *atio)
608107178Snjl{
609107178Snjl	struct ccb_scsiio *ctio;
610107178Snjl	struct atio_descr *a_descr;
611107178Snjl	struct ctio_descr *c_descr;
612107178Snjl	cam_status status;
613107178Snjl	int ret;
614107178Snjl
615107178Snjl	if (debug)
616107178Snjl		warnx("Working on ATIO %p", atio);
617107178Snjl
618107178Snjl	a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
619107178Snjl
620107178Snjl	/* Get a CTIO and initialize it according to our known parameters */
621107178Snjl	ctio = get_ctio();
622107178Snjl	if (ctio == NULL)
623107178Snjl		return (1);
624107178Snjl	ret = 0;
625107178Snjl	ctio->ccb_h.flags = a_descr->flags;
626107178Snjl	ctio->tag_id = atio->tag_id;
627107178Snjl	ctio->init_id = atio->init_id;
628107178Snjl	/* XXX priority needs to be added to a_descr */
629107178Snjl	c_descr = (struct ctio_descr *)ctio->ccb_h.targ_descr;
630107178Snjl	c_descr->atio = atio;
631107178Snjl	if ((a_descr->flags & CAM_DIR_IN) != 0)
632107178Snjl		c_descr->offset = a_descr->base_off + a_descr->targ_req;
633107178Snjl	else if ((a_descr->flags & CAM_DIR_MASK) == CAM_DIR_OUT)
634107178Snjl		c_descr->offset = a_descr->base_off + a_descr->init_req;
635120428Ssimokawa	else
636120428Ssimokawa		c_descr->offset = a_descr->base_off;
637107178Snjl
638107178Snjl	/*
639107178Snjl	 * Return a check condition if there was an error while
640107178Snjl	 * receiving this ATIO.
641107178Snjl	 */
642107178Snjl	if (atio->sense_len != 0) {
64339215Sgibbs		struct scsi_sense_data *sense;
64439215Sgibbs
645107178Snjl		if (debug) {
646107178Snjl			warnx("ATIO with %u bytes sense received",
647107178Snjl			      atio->sense_len);
64839215Sgibbs		}
649107178Snjl		sense = &atio->sense_data;
650107178Snjl		tcmd_sense(ctio->init_id, ctio, sense->flags,
651107178Snjl			   sense->add_sense_code, sense->add_sense_code_qual);
652107178Snjl		send_ccb((union ccb *)ctio, /*priority*/1);
653107178Snjl		return (0);
654107178Snjl	}
65539215Sgibbs
656107178Snjl	status = atio->ccb_h.status & CAM_STATUS_MASK;
657107178Snjl	switch (status) {
658107178Snjl	case CAM_CDB_RECVD:
659107178Snjl		ret = tcmd_handle(atio, ctio, ATIO_WORK);
660107178Snjl		break;
661107178Snjl	case CAM_REQ_ABORTED:
662107178Snjl		/* Requeue on HBA */
663107178Snjl		TAILQ_REMOVE(&work_queue, &atio->ccb_h, periph_links.tqe);
664107178Snjl		send_ccb((union ccb *)atio, /*priority*/1);
665107178Snjl		ret = 1;
666107178Snjl		break;
667107178Snjl	default:
668107178Snjl		warnx("ATIO completed with unhandled status %#x", status);
669107178Snjl		abort();
670107178Snjl		/* NOTREACHED */
671107178Snjl		break;
672107178Snjl	}
67339215Sgibbs
674107178Snjl	return (ret);
675107178Snjl}
67639215Sgibbs
677107178Snjlstatic void
678107178Snjlqueue_io(struct ccb_scsiio *ctio)
679107178Snjl{
680107178Snjl	struct ccb_hdr *ccb_h;
681107178Snjl	struct io_queue *ioq;
682107178Snjl	struct ctio_descr *c_descr, *curr_descr;
683107178Snjl
684107178Snjl	c_descr = (struct ctio_descr *)ctio->ccb_h.targ_descr;
685107178Snjl	/* If the completion is for a specific ATIO, queue in order */
686107178Snjl	if (c_descr->atio != NULL) {
687107178Snjl		struct atio_descr *a_descr;
68839215Sgibbs
689107178Snjl		a_descr = (struct atio_descr *)c_descr->atio->ccb_h.targ_descr;
690107178Snjl		ioq = &a_descr->cmplt_io;
691107178Snjl	} else {
692107178Snjl		errx(1, "CTIO %p has NULL ATIO", ctio);
693107178Snjl	}
694107178Snjl
695107178Snjl	/* Insert in order, sorted by offset */
696107178Snjl	if (!TAILQ_EMPTY(ioq)) {
697107178Snjl		TAILQ_FOREACH_REVERSE(ccb_h, ioq, io_queue, periph_links.tqe) {
698107178Snjl			curr_descr = (struct ctio_descr *)ccb_h->targ_descr;
699107178Snjl			if (curr_descr->offset <= c_descr->offset) {
700107178Snjl				TAILQ_INSERT_AFTER(ioq, ccb_h, &ctio->ccb_h,
701107178Snjl						   periph_links.tqe);
702107178Snjl				break;
703107178Snjl			}
704107178Snjl			if (TAILQ_PREV(ccb_h, io_queue, periph_links.tqe)
705107178Snjl			    == NULL) {
706107178Snjl				TAILQ_INSERT_BEFORE(ccb_h, &ctio->ccb_h,
707107178Snjl						    periph_links.tqe);
708107178Snjl				break;
709107178Snjl			}
71039215Sgibbs		}
711107178Snjl	} else {
712107178Snjl		TAILQ_INSERT_HEAD(ioq, &ctio->ccb_h, periph_links.tqe);
713107178Snjl	}
714107178Snjl}
71539215Sgibbs
716107178Snjl/*
717107178Snjl * Go through all completed AIO/CTIOs for a given ATIO and advance data
718107178Snjl * counts, start continuation IO, etc.
719107178Snjl */
720107178Snjlstatic void
721107178Snjlrun_queue(struct ccb_accept_tio *atio)
722107178Snjl{
723107178Snjl	struct atio_descr *a_descr;
724107178Snjl	struct ccb_hdr *ccb_h;
725107178Snjl	int sent_status, event;
726107178Snjl
727107178Snjl	if (atio == NULL)
728107178Snjl		return;
729107178Snjl
730107178Snjl	a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
731107178Snjl
732107178Snjl	while ((ccb_h = TAILQ_FIRST(&a_descr->cmplt_io)) != NULL) {
733107178Snjl		struct ccb_scsiio *ctio;
734107178Snjl		struct ctio_descr *c_descr;
735107178Snjl
736107178Snjl		ctio = (struct ccb_scsiio *)ccb_h;
737107178Snjl		c_descr = (struct ctio_descr *)ctio->ccb_h.targ_descr;
738107178Snjl
739120428Ssimokawa		if (ctio->ccb_h.status == CAM_REQ_ABORTED) {
740120428Ssimokawa			TAILQ_REMOVE(&a_descr->cmplt_io, ccb_h,
741120428Ssimokawa				     periph_links.tqe);
742120428Ssimokawa			free_ccb((union ccb *)ctio);
743120428Ssimokawa			send_ccb((union ccb *)atio, /*priority*/1);
744120428Ssimokawa			continue;
745120428Ssimokawa		}
746120428Ssimokawa
747107178Snjl		/* If completed item is in range, call handler */
748107178Snjl		if ((c_descr->event == AIO_DONE &&
749107178Snjl		    c_descr->offset == a_descr->base_off + a_descr->targ_ack)
750107178Snjl		 || (c_descr->event == CTIO_DONE &&
751107178Snjl		    c_descr->offset == a_descr->base_off + a_descr->init_ack)) {
752107178Snjl			sent_status = (ccb_h->flags & CAM_SEND_STATUS) != 0;
753107178Snjl			event = c_descr->event;
754107178Snjl
755107178Snjl			TAILQ_REMOVE(&a_descr->cmplt_io, ccb_h,
756107178Snjl				     periph_links.tqe);
757107178Snjl			tcmd_handle(atio, ctio, c_descr->event);
758107178Snjl
759107178Snjl			/* If entire transfer complete, send back ATIO */
760107178Snjl			if (sent_status != 0 && event == CTIO_DONE)
761107178Snjl				send_ccb((union ccb *)atio, /*priority*/1);
762107178Snjl		} else {
763107178Snjl			/* Gap in offsets so wait until later callback */
764107178Snjl			if (debug)
765107178Snjl				warnx("IO %p out of order", ccb_h);
766107178Snjl			break;
76763185Smjacob		}
768107178Snjl	}
769107178Snjl}
77063185Smjacob
771107178Snjlstatic int
772107178Snjlwork_inot(struct ccb_immed_notify *inot)
773107178Snjl{
774107178Snjl	cam_status status;
775107178Snjl	int sense;
77663185Smjacob
777107178Snjl	if (debug)
778107178Snjl		warnx("Working on INOT %p", inot);
779107178Snjl
780107178Snjl	status = inot->ccb_h.status;
781107178Snjl	sense = (status & CAM_AUTOSNS_VALID) != 0;
782107178Snjl	status &= CAM_STATUS_MASK;
783107178Snjl
784107178Snjl	switch (status) {
785107178Snjl	case CAM_SCSI_BUS_RESET:
786107178Snjl		tcmd_ua(CAM_TARGET_WILDCARD, UA_BUS_RESET);
787107178Snjl		abort_all_pending();
788107178Snjl		break;
789107178Snjl	case CAM_BDR_SENT:
790107178Snjl		tcmd_ua(CAM_TARGET_WILDCARD, UA_BDR);
791107178Snjl		abort_all_pending();
792107178Snjl		break;
793107178Snjl	case CAM_MESSAGE_RECV:
794107178Snjl		switch (inot->message_args[0]) {
795107178Snjl		case MSG_TASK_COMPLETE:
796107178Snjl		case MSG_INITIATOR_DET_ERR:
797107178Snjl		case MSG_ABORT_TASK_SET:
798107178Snjl		case MSG_MESSAGE_REJECT:
799107178Snjl		case MSG_NOOP:
800107178Snjl		case MSG_PARITY_ERROR:
801107178Snjl		case MSG_TARGET_RESET:
802107178Snjl		case MSG_ABORT_TASK:
803107178Snjl		case MSG_CLEAR_TASK_SET:
804107178Snjl		default:
805107178Snjl			warnx("INOT message %#x", inot->message_args[0]);
806107178Snjl			break;
80739215Sgibbs		}
808107178Snjl		break;
809107178Snjl	case CAM_REQ_ABORTED:
810107178Snjl		warnx("INOT %p aborted", inot);
811107178Snjl		break;
812107178Snjl	default:
813107178Snjl		warnx("Unhandled INOT status %#x", status);
814107178Snjl		break;
81539215Sgibbs	}
81639215Sgibbs
817107178Snjl	/* If there is sense data, use it */
818107178Snjl	if (sense != 0) {
819107178Snjl		struct scsi_sense_data *sense;
820107178Snjl
821107178Snjl		sense = &inot->sense_data;
822107178Snjl		tcmd_sense(inot->initiator_id, NULL, sense->flags,
823107178Snjl			   sense->add_sense_code, sense->add_sense_code_qual);
824107178Snjl		if (debug)
825107178Snjl			warnx("INOT has sense: %#x", sense->flags);
826107178Snjl	}
827107178Snjl
828107178Snjl	/* Requeue on SIM */
829107178Snjl	TAILQ_REMOVE(&work_queue, &inot->ccb_h, periph_links.tqe);
830107178Snjl	send_ccb((union ccb *)inot, /*priority*/1);
831107178Snjl
832107178Snjl	return (1);
83339215Sgibbs}
83439215Sgibbs
835107178Snjlvoid
836107178Snjlsend_ccb(union ccb *ccb, int priority)
837107178Snjl{
838107178Snjl	if (debug)
839107178Snjl		warnx("sending ccb (%#x)", ccb->ccb_h.func_code);
840107178Snjl	ccb->ccb_h.pinfo.priority = priority;
841107178Snjl	if (XPT_FC_IS_QUEUED(ccb)) {
842107178Snjl		TAILQ_INSERT_TAIL(&pending_queue, &ccb->ccb_h,
843107178Snjl				  periph_links.tqe);
844107178Snjl	}
845107178Snjl	if (write(targ_fd, &ccb, sizeof(ccb)) != sizeof(ccb)) {
846107178Snjl		warn("write ccb");
847107178Snjl		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
848107178Snjl	}
849107178Snjl}
850107178Snjl
851107178Snjl/* Return a CTIO/descr/buf combo from the freelist or malloc one */
852107178Snjlstatic struct ccb_scsiio *
853107178Snjlget_ctio()
854107178Snjl{
855107178Snjl	struct ccb_scsiio *ctio;
856107178Snjl	struct ctio_descr *c_descr;
857107178Snjl	struct sigevent *se;
858107178Snjl
859107178Snjl	if (num_ctios == MAX_CTIOS)
860107178Snjl		return (NULL);
861107178Snjl
862107178Snjl	ctio = (struct ccb_scsiio *)malloc(sizeof(*ctio));
863107178Snjl	if (ctio == NULL) {
864107178Snjl		warn("malloc CTIO");
865107178Snjl		return (NULL);
866107178Snjl	}
867107178Snjl	c_descr = (struct ctio_descr *)malloc(sizeof(*c_descr));
868107178Snjl	if (c_descr == NULL) {
869107178Snjl		free(ctio);
870107178Snjl		warn("malloc ctio_descr");
871107178Snjl		return (NULL);
872107178Snjl	}
873107178Snjl	c_descr->buf = malloc(buf_size);
874107178Snjl	if (c_descr->buf == NULL) {
875107178Snjl		free(c_descr);
876107178Snjl		free(ctio);
877107178Snjl		warn("malloc backing store");
878107178Snjl		return (NULL);
879107178Snjl	}
880107178Snjl	num_ctios++;
881107178Snjl
882107178Snjl	/* Initialize CTIO, CTIO descr, and AIO */
883107178Snjl	ctio->ccb_h.func_code = XPT_CONT_TARGET_IO;
884107178Snjl	ctio->ccb_h.retry_count = 2;
885109345Snjl	ctio->ccb_h.timeout = CAM_TIME_INFINITY;
886107178Snjl	ctio->data_ptr = c_descr->buf;
887107178Snjl	ctio->ccb_h.targ_descr = c_descr;
888107178Snjl	c_descr->aiocb.aio_buf = c_descr->buf;
889107178Snjl	c_descr->aiocb.aio_fildes = file_fd;
890107178Snjl	se = &c_descr->aiocb.aio_sigevent;
891107178Snjl	se->sigev_notify = SIGEV_KEVENT;
892107178Snjl	se->sigev_notify_kqueue = kq_fd;
893107178Snjl	se->sigev_value.sigval_ptr = ctio;
894107178Snjl
895107178Snjl	return (ctio);
896107178Snjl}
897107178Snjl
898107178Snjlvoid
899107178Snjlfree_ccb(union ccb *ccb)
900107178Snjl{
901107178Snjl	switch (ccb->ccb_h.func_code) {
902107178Snjl	case XPT_CONT_TARGET_IO:
903107178Snjl	{
904107178Snjl		struct ctio_descr *c_descr;
905107178Snjl
906107178Snjl		c_descr = (struct ctio_descr *)ccb->ccb_h.targ_descr;
907107178Snjl		free(c_descr->buf);
908107178Snjl		num_ctios--;
909107178Snjl		/* FALLTHROUGH */
910107178Snjl	}
911107178Snjl	case XPT_ACCEPT_TARGET_IO:
912107178Snjl		free(ccb->ccb_h.targ_descr);
913107178Snjl		/* FALLTHROUGH */
914107178Snjl	case XPT_IMMED_NOTIFY:
915107178Snjl	default:
916107178Snjl		free(ccb);
917107178Snjl		break;
918107178Snjl	}
919107178Snjl}
920107178Snjl
921107178Snjlstatic cam_status
922107178Snjlget_sim_flags(u_int16_t *flags)
923107178Snjl{
924107178Snjl	struct ccb_pathinq cpi;
925107178Snjl	cam_status status;
926107178Snjl
927107178Snjl	/* Find SIM capabilities */
928107178Snjl	bzero(&cpi, sizeof(cpi));
929107178Snjl	cpi.ccb_h.func_code = XPT_PATH_INQ;
930107178Snjl	send_ccb((union ccb *)&cpi, /*priority*/1);
931107178Snjl	status = cpi.ccb_h.status & CAM_STATUS_MASK;
932107178Snjl	if (status != CAM_REQ_CMP) {
933107178Snjl		fprintf(stderr, "CPI failed, status %#x\n", status);
934107178Snjl		return (status);
935107178Snjl	}
936107178Snjl
937107178Snjl	/* Can only enable on controllers that support target mode */
938107178Snjl	if ((cpi.target_sprt & PIT_PROCESSOR) == 0) {
939107178Snjl		fprintf(stderr, "HBA does not support target mode\n");
940107178Snjl		status = CAM_PATH_INVALID;
941107178Snjl		return (status);
942107178Snjl	}
943107178Snjl
944107178Snjl	*flags = cpi.hba_inquiry;
945107178Snjl	return (status);
946107178Snjl}
947107178Snjl
94839215Sgibbsstatic void
949107178Snjlrel_simq()
95044498Sgibbs{
951107178Snjl	struct ccb_relsim crs;
952107178Snjl
953107178Snjl	bzero(&crs, sizeof(crs));
954107178Snjl	crs.ccb_h.func_code = XPT_REL_SIMQ;
955107178Snjl	crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
956107178Snjl	crs.openings = 0;
957107178Snjl	crs.release_timeout = 0;
958107178Snjl	crs.qfrozen_cnt = 0;
959107178Snjl	send_ccb((union ccb *)&crs, /*priority*/0);
96044498Sgibbs}
96144498Sgibbs
962107178Snjl/* Cancel all pending CCBs. */
96344498Sgibbsstatic void
964107178Snjlabort_all_pending()
96539215Sgibbs{
966107178Snjl	struct ccb_abort	 cab;
967107178Snjl	struct ccb_hdr		*ccb_h;
96839215Sgibbs
969107178Snjl	if (debug)
970107178Snjl		  warnx("abort_all_pending");
97139215Sgibbs
972107178Snjl	bzero(&cab, sizeof(cab));
973107178Snjl	cab.ccb_h.func_code = XPT_ABORT;
974107178Snjl	TAILQ_FOREACH(ccb_h, &pending_queue, periph_links.tqe) {
975107178Snjl		if (debug)
976107178Snjl			  warnx("Aborting pending CCB %p\n", ccb_h);
977107178Snjl		cab.abort_ccb = (union ccb *)ccb_h;
978107178Snjl		send_ccb((union ccb *)&cab, /*priority*/1);
979107178Snjl		if (cab.ccb_h.status != CAM_REQ_CMP) {
980107178Snjl			warnx("Unable to abort CCB, status %#x\n",
981107178Snjl			       cab.ccb_h.status);
982107178Snjl		}
983107178Snjl	}
98439215Sgibbs}
98539215Sgibbs
986107178Snjlstatic void
987107178Snjlusage()
988107178Snjl{
989107178Snjl	fprintf(stderr,
990107178Snjl		"Usage: scsi_target [-AdST] [-b bufsize] [-c sectorsize]\n"
991107178Snjl		"\t\t[-r numbufs] [-s volsize] [-W 8,16,32]\n"
992107178Snjl		"\t\tbus:target:lun filename\n");
993107178Snjl	exit(1);
994107178Snjl}
995