4 * Copyright (c) 2002 Nate Lawson.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/types.h>
46 #include <sys/queue.h>
47 #include <sys/event.h>
48 #include <sys/param.h>
50 #include <cam/cam_queue.h>
51 #include <cam/scsi/scsi_all.h>
52 #include <cam/scsi/scsi_targetio.h>
53 #include <cam/scsi/scsi_message.h>
54 #include "scsi_target.h"
56 /* Maximum amount to transfer per CTIO */
57 #define MAX_XFER MAXPHYS
58 /* Maximum number of allocated CTIOs */
60 /* Maximum sector size for emulated volume */
61 #define MAX_SECTOR 32768
63 /* Global variables */
75 static struct ccb_queue pending_queue
;
76 static struct ccb_queue work_queue
;
77 static struct ioc_enable_lun ioc_enlun
= {
84 static void cleanup(void);
85 static int init_ccbs(void);
86 static void request_loop(void);
87 static void handle_read(void);
88 /* static int work_atio(struct ccb_accept_tio *); */
89 static void queue_io(struct ccb_scsiio
*);
90 static int run_queue(struct ccb_accept_tio
*);
91 static int work_inot(struct ccb_immed_notify
*);
92 static struct ccb_scsiio
*
94 /* static void free_ccb(union ccb *); */
95 static cam_status
get_sim_flags(u_int16_t
*);
96 static void rel_simq(void);
97 static void abort_all_pending(void);
98 static void usage(void);
101 main(int argc
, char *argv
[])
104 char *file_name
, targname
[16];
105 u_int16_t req_flags
, sim_flags
;
110 req_flags
= sim_flags
= 0;
112 targ_fd
= file_fd
= kq_fd
= -1;
114 sector_size
= SECTOR_SIZE
;
117 /* Prepare resource pools */
118 TAILQ_INIT(&pending_queue
);
119 TAILQ_INIT(&work_queue
);
121 while ((ch
= getopt(argc
, argv
, "AdSTYb:c:s:W:")) != -1) {
124 req_flags
|= SID_Addr16
;
130 req_flags
|= SID_Sync
;
133 req_flags
|= SID_CmdQue
;
136 buf_size
= atoi(optarg
);
137 if (buf_size
< 256 || buf_size
> MAX_XFER
)
138 errx(1, "Unreasonable buf size: %s", optarg
);
141 sector_size
= atoi(optarg
);
142 if (sector_size
< 512 || sector_size
> MAX_SECTOR
)
143 errx(1, "Unreasonable sector size: %s", optarg
);
149 last
= strlen(optarg
) - 1;
151 switch (tolower(optarg
[last
])) {
173 user_size
= strtoll(optarg
, (char **)NULL
, /*base*/10);
176 errx(1, "Unreasonable volume size: %s", optarg
);
180 req_flags
&= ~(SID_WBus16
| SID_WBus32
);
181 switch (atoi(optarg
)) {
183 /* Leave req_flags zeroed */
186 req_flags
|= SID_WBus16
;
189 req_flags
|= SID_WBus32
;
192 warnx("Width %s not supported", optarg
);
211 sscanf(argv
[0], "%u:%u:%u", &ioc_enlun
.path_id
, &ioc_enlun
.target_id
,
215 if (ioc_enlun
.path_id
== CAM_BUS_WILDCARD
||
216 ioc_enlun
.target_id
== CAM_TARGET_WILDCARD
||
217 ioc_enlun
.lun_id
== CAM_LUN_WILDCARD
) {
218 warnx("Incomplete target path specified");
222 /* We don't support any vendor-specific commands */
223 ioc_enlun
.grp6_len
= 0;
224 ioc_enlun
.grp7_len
= 0;
226 /* Open backing store for IO */
227 file_fd
= open(file_name
, O_RDWR
);
229 err(1, "open backing store file");
231 /* Check backing store size or use the size user gave us */
232 if (user_size
== 0) {
235 if (fstat(file_fd
, &st
) < 0)
236 err(1, "fstat file");
237 #if __FreeBSD_version >= 500000
238 if ((st
.st_mode
& S_IFCHR
) != 0) {
241 if (ioctl(file_fd
, DIOCGMEDIASIZE
, &mediasize
) < 0)
242 err(1, "DIOCGMEDIASIZE");
244 /* XXX get sector size by ioctl()?? */
245 volume_size
= mediasize
/ sector_size
;
248 volume_size
= st
.st_size
/ sector_size
;
250 volume_size
= user_size
/ sector_size
;
253 warnx("volume_size: %d bytes x " OFF_FMT
" sectors",
254 sector_size
, volume_size
);
256 if (volume_size
<= 0)
257 errx(1, "volume must be larger than %d", sector_size
);
260 struct aiocb aio
, *aiop
;
262 /* See if we have we have working AIO support */
263 memset(&aio
, 0, sizeof(aio
));
264 aio
.aio_buf
= malloc(sector_size
);
265 if (aio
.aio_buf
== NULL
)
267 aio
.aio_fildes
= file_fd
;
269 aio
.aio_nbytes
= sector_size
;
270 signal(SIGSYS
, SIG_IGN
);
271 if (aio_read(&aio
) != 0) {
272 printf("AIO support is not available- switchin to"
273 " single-threaded mode.\n");
276 if (aio_waitcomplete(&aiop
, NULL
) != sector_size
)
277 err(1, "aio_waitcomplete");
278 assert(aiop
== &aio
);
279 signal(SIGSYS
, SIG_DFL
);
281 free((void *)aio
.aio_buf
);
282 if (debug
&& notaio
== 0)
283 warnx("aio support tested ok");
286 /* Go through all the control devices and find one that isn't busy. */
289 snprintf(targname
, sizeof(targname
), "/dev/targ%d", unit
++);
290 targ_fd
= open(targname
, O_RDWR
);
291 } while (targ_fd
< 0 && errno
== EBUSY
);
294 err(1, "Tried to open %d devices, none available", unit
);
296 /* The first three are handled by kevent() later */
297 signal(SIGHUP
, SIG_IGN
);
298 signal(SIGINT
, SIG_IGN
);
299 signal(SIGTERM
, SIG_IGN
);
300 signal(SIGPROF
, SIG_IGN
);
301 signal(SIGALRM
, SIG_IGN
);
302 signal(SIGSTOP
, SIG_IGN
);
303 signal(SIGTSTP
, SIG_IGN
);
305 /* Register a cleanup handler to run when exiting */
308 /* Enable listening on the specified LUN */
309 if (ioctl(targ_fd
, TARGIOCENABLE
, &ioc_enlun
) != 0)
310 err(1, "TARGIOCENABLE");
312 /* Enable debugging if requested */
314 if (ioctl(targ_fd
, TARGIOCDEBUG
, &debug
) != 0)
315 warnx("TARGIOCDEBUG");
318 /* Set up inquiry data according to what SIM supports */
319 if (get_sim_flags(&sim_flags
) != CAM_REQ_CMP
)
320 errx(1, "get_sim_flags");
321 if (tcmd_init(req_flags
, sim_flags
) != 0)
322 errx(1, "Initializing tcmd subsystem failed");
324 /* Queue ATIOs and INOTs on descriptor */
325 if (init_ccbs() != 0)
326 errx(1, "init_ccbs failed");
329 warnx("main loop beginning");
338 struct ccb_hdr
*ccb_h
;
341 warnx("cleanup called");
343 ioctl(targ_fd
, TARGIOCDEBUG
, &debug
);
345 ioctl(targ_fd
, TARGIOCDISABLE
, NULL
);
348 while ((ccb_h
= TAILQ_FIRST(&pending_queue
)) != NULL
) {
349 TAILQ_REMOVE(&pending_queue
, ccb_h
, periph_links
.tqe
);
350 free_ccb((union ccb
*)ccb_h
);
352 while ((ccb_h
= TAILQ_FIRST(&work_queue
)) != NULL
) {
353 TAILQ_REMOVE(&work_queue
, ccb_h
, periph_links
.tqe
);
354 free_ccb((union ccb
*)ccb_h
);
361 /* Allocate ATIOs/INOTs and queue on HBA */
367 for (i
= 0; i
< MAX_INITIATORS
; i
++) {
368 struct ccb_accept_tio
*atio
;
369 struct atio_descr
*a_descr
;
370 struct ccb_immed_notify
*inot
;
372 atio
= (struct ccb_accept_tio
*)malloc(sizeof(*atio
));
377 a_descr
= (struct atio_descr
*)malloc(sizeof(*a_descr
));
378 if (a_descr
== NULL
) {
380 warn("malloc atio_descr");
383 atio
->ccb_h
.func_code
= XPT_ACCEPT_TARGET_IO
;
384 atio
->ccb_h
.targ_descr
= a_descr
;
385 send_ccb((union ccb
*)atio
, /*priority*/1);
387 inot
= (struct ccb_immed_notify
*)malloc(sizeof(*inot
));
392 inot
->ccb_h
.func_code
= XPT_IMMED_NOTIFY
;
393 send_ccb((union ccb
*)inot
, /*priority*/1);
402 struct kevent events
[MAX_EVENTS
];
403 struct timespec ts
, *tptr
;
406 /* Register kqueue for event notification */
407 if ((kq_fd
= kqueue()) < 0)
408 err(1, "init kqueue");
410 /* Set up some default events */
411 EV_SET(&events
[0], SIGHUP
, EVFILT_SIGNAL
, EV_ADD
|EV_ENABLE
, 0, 0, 0);
412 EV_SET(&events
[1], SIGINT
, EVFILT_SIGNAL
, EV_ADD
|EV_ENABLE
, 0, 0, 0);
413 EV_SET(&events
[2], SIGTERM
, EVFILT_SIGNAL
, EV_ADD
|EV_ENABLE
, 0, 0, 0);
414 EV_SET(&events
[3], targ_fd
, EVFILT_READ
, EV_ADD
|EV_ENABLE
, 0, 0, 0);
415 if (kevent(kq_fd
, events
, 4, NULL
, 0, NULL
) < 0)
416 err(1, "kevent signal registration");
423 /* Loop until user signal */
426 struct ccb_hdr
*ccb_h
;
428 /* Check for the next signal, read ready, or AIO completion */
429 retval
= kevent(kq_fd
, NULL
, 0, events
, MAX_EVENTS
, tptr
);
431 if (errno
== EINTR
) {
433 warnx("EINTR, looping");
437 err(1, "kevent failed");
439 } else if (retval
> MAX_EVENTS
) {
440 errx(1, "kevent returned more events than allocated?");
443 /* Process all received events. */
444 for (oo
= i
= 0; i
< retval
; i
++) {
445 if ((events
[i
].flags
& EV_ERROR
) != 0)
446 errx(1, "kevent registration failed");
448 switch (events
[i
].filter
) {
456 struct ccb_scsiio
*ctio
;
457 struct ctio_descr
*c_descr
;
461 ctio
= (struct ccb_scsiio
*)events
[i
].udata
;
462 c_descr
= (struct ctio_descr
*)
463 ctio
->ccb_h
.targ_descr
;
464 c_descr
->event
= AIO_DONE
;
465 /* Queue on the appropriate ATIO */
467 /* Process any queued completions. */
468 oo
+= run_queue(c_descr
->atio
);
473 warnx("signal ready, setting quit");
477 warnx("unknown event %d", events
[i
].filter
);
482 warnx("event %d done", events
[i
].filter
);
490 /* Grab the first CCB and perform one work unit. */
491 if ((ccb_h
= TAILQ_FIRST(&work_queue
)) != NULL
) {
494 ccb
= (union ccb
*)ccb_h
;
495 switch (ccb_h
->func_code
) {
496 case XPT_ACCEPT_TARGET_IO
:
497 /* Start one more transfer. */
498 retval
= work_atio(&ccb
->atio
);
500 case XPT_IMMED_NOTIFY
:
501 retval
= work_inot(&ccb
->cin
);
504 warnx("Unhandled ccb type %#x on workq",
510 /* Assume work function handled the exception */
511 if ((ccb_h
->status
& CAM_DEV_QFRZN
) != 0) {
513 warnx("Queue frozen receiving CCB, "
519 /* No more work needed for this command. */
521 TAILQ_REMOVE(&work_queue
, ccb_h
,
527 * Poll for new events (i.e. completions) while we
528 * are processing CCBs on the work_queue. Once it's
529 * empty, use an infinite wait.
531 if (!TAILQ_EMPTY(&work_queue
))
538 /* CCBs are ready from the kernel */
542 union ccb
*ccb_array
[MAX_INITIATORS
], *ccb
;
543 int ccb_count
, i
, oo
;
545 ccb_count
= read(targ_fd
, ccb_array
, sizeof(ccb_array
));
546 if (ccb_count
<= 0) {
547 warn("read ccb ptrs");
550 ccb_count
/= sizeof(union ccb
*);
552 warnx("truncated read ccb ptr?");
556 for (i
= 0; i
< ccb_count
; i
++) {
558 TAILQ_REMOVE(&pending_queue
, &ccb
->ccb_h
, periph_links
.tqe
);
560 switch (ccb
->ccb_h
.func_code
) {
561 case XPT_ACCEPT_TARGET_IO
:
563 struct ccb_accept_tio
*atio
;
564 struct atio_descr
*a_descr
;
566 /* Initialize ATIO descr for this transaction */
568 a_descr
= (struct atio_descr
*)atio
->ccb_h
.targ_descr
;
569 bzero(a_descr
, sizeof(*a_descr
));
570 TAILQ_INIT(&a_descr
->cmplt_io
);
571 a_descr
->flags
= atio
->ccb_h
.flags
&
572 (CAM_DIS_DISCONNECT
| CAM_TAG_ACTION_VALID
);
573 /* XXX add a_descr->priority */
574 if ((atio
->ccb_h
.flags
& CAM_CDB_POINTER
) == 0)
575 a_descr
->cdb
= atio
->cdb_io
.cdb_bytes
;
577 a_descr
->cdb
= atio
->cdb_io
.cdb_ptr
;
579 /* ATIOs are processed in FIFO order */
580 TAILQ_INSERT_TAIL(&work_queue
, &ccb
->ccb_h
,
584 case XPT_CONT_TARGET_IO
:
586 struct ccb_scsiio
*ctio
;
587 struct ctio_descr
*c_descr
;
590 c_descr
= (struct ctio_descr
*)ctio
->ccb_h
.targ_descr
;
591 c_descr
->event
= CTIO_DONE
;
592 /* Queue on the appropriate ATIO */
594 /* Process any queued completions. */
595 oo
+= run_queue(c_descr
->atio
);
598 case XPT_IMMED_NOTIFY
:
599 /* INOTs are handled with priority */
600 TAILQ_INSERT_HEAD(&work_queue
, &ccb
->ccb_h
,
604 warnx("Unhandled ccb type %#x in handle_read",
605 ccb
->ccb_h
.func_code
);
611 /* Process an ATIO CCB from the kernel */
613 work_atio(struct ccb_accept_tio
*atio
)
615 struct ccb_scsiio
*ctio
;
616 struct atio_descr
*a_descr
;
617 struct ctio_descr
*c_descr
;
622 warnx("Working on ATIO %p", atio
);
624 a_descr
= (struct atio_descr
*)atio
->ccb_h
.targ_descr
;
626 /* Get a CTIO and initialize it according to our known parameters */
632 ctio
->ccb_h
.flags
= a_descr
->flags
;
633 ctio
->tag_id
= atio
->tag_id
;
634 ctio
->init_id
= atio
->init_id
;
635 /* XXX priority needs to be added to a_descr */
636 c_descr
= (struct ctio_descr
*)ctio
->ccb_h
.targ_descr
;
637 c_descr
->atio
= atio
;
638 if ((a_descr
->flags
& CAM_DIR_IN
) != 0)
639 c_descr
->offset
= a_descr
->base_off
+ a_descr
->targ_req
;
640 else if ((a_descr
->flags
& CAM_DIR_MASK
) == CAM_DIR_OUT
)
641 c_descr
->offset
= a_descr
->base_off
+ a_descr
->init_req
;
643 c_descr
->offset
= a_descr
->base_off
;
646 * Return a check condition if there was an error while
647 * receiving this ATIO.
649 if (atio
->sense_len
!= 0) {
650 struct scsi_sense_data
*sense
;
653 warnx("ATIO with %u bytes sense received",
656 sense
= &atio
->sense_data
;
657 tcmd_sense(ctio
->init_id
, ctio
, sense
->flags
,
658 sense
->add_sense_code
, sense
->add_sense_code_qual
);
659 send_ccb((union ccb
*)ctio
, /*priority*/1);
663 status
= atio
->ccb_h
.status
& CAM_STATUS_MASK
;
666 ret
= tcmd_handle(atio
, ctio
, ATIO_WORK
);
668 case CAM_REQ_ABORTED
:
669 warn("ATIO %p aborted", a_descr
);
671 TAILQ_REMOVE(&work_queue
, &atio
->ccb_h
, periph_links
.tqe
);
672 send_ccb((union ccb
*)atio
, /*priority*/1);
676 warnx("ATIO completed with unhandled status %#x", status
);
686 queue_io(struct ccb_scsiio
*ctio
)
688 struct ccb_hdr
*ccb_h
;
689 struct io_queue
*ioq
;
690 struct ctio_descr
*c_descr
;
692 c_descr
= (struct ctio_descr
*)ctio
->ccb_h
.targ_descr
;
693 if (c_descr
->atio
== NULL
) {
694 errx(1, "CTIO %p has NULL ATIO", ctio
);
696 ioq
= &((struct atio_descr
*)c_descr
->atio
->ccb_h
.targ_descr
)->cmplt_io
;
698 if (TAILQ_EMPTY(ioq
)) {
699 TAILQ_INSERT_HEAD(ioq
, &ctio
->ccb_h
, periph_links
.tqe
);
703 TAILQ_FOREACH_REVERSE(ccb_h
, ioq
, io_queue
, periph_links
.tqe
) {
704 struct ctio_descr
*curr_descr
=
705 (struct ctio_descr
*)ccb_h
->targ_descr
;
706 if (curr_descr
->offset
<= c_descr
->offset
) {
712 TAILQ_INSERT_AFTER(ioq
, ccb_h
, &ctio
->ccb_h
, periph_links
.tqe
);
714 TAILQ_INSERT_HEAD(ioq
, &ctio
->ccb_h
, periph_links
.tqe
);
719 * Go through all completed AIO/CTIOs for a given ATIO and advance data
720 * counts, start continuation IO, etc.
723 run_queue(struct ccb_accept_tio
*atio
)
725 struct atio_descr
*a_descr
;
726 struct ccb_hdr
*ccb_h
;
727 int sent_status
, event
;
732 a_descr
= (struct atio_descr
*)atio
->ccb_h
.targ_descr
;
734 while ((ccb_h
= TAILQ_FIRST(&a_descr
->cmplt_io
)) != NULL
) {
735 struct ccb_scsiio
*ctio
;
736 struct ctio_descr
*c_descr
;
738 ctio
= (struct ccb_scsiio
*)ccb_h
;
739 c_descr
= (struct ctio_descr
*)ctio
->ccb_h
.targ_descr
;
741 if (ctio
->ccb_h
.status
== CAM_REQ_ABORTED
) {
742 TAILQ_REMOVE(&a_descr
->cmplt_io
, ccb_h
,
744 free_ccb((union ccb
*)ctio
);
745 send_ccb((union ccb
*)atio
, /*priority*/1);
749 /* If completed item is in range, call handler */
750 if ((c_descr
->event
== AIO_DONE
&&
751 c_descr
->offset
== a_descr
->base_off
+ a_descr
->targ_ack
)
752 || (c_descr
->event
== CTIO_DONE
&&
753 c_descr
->offset
== a_descr
->base_off
+ a_descr
->init_ack
)) {
754 sent_status
= (ccb_h
->flags
& CAM_SEND_STATUS
) != 0;
755 event
= c_descr
->event
;
757 TAILQ_REMOVE(&a_descr
->cmplt_io
, ccb_h
,
759 tcmd_handle(atio
, ctio
, c_descr
->event
);
761 /* If entire transfer complete, send back ATIO */
762 if (sent_status
!= 0 && event
== CTIO_DONE
)
763 send_ccb((union ccb
*)atio
, /*priority*/1);
765 /* Gap in offsets so wait until later callback */
767 warnx("IO %p:%p out of order %s", ccb_h
,
768 a_descr
, c_descr
->event
== AIO_DONE
?
777 work_inot(struct ccb_immed_notify
*inot
)
783 warnx("Working on INOT %p", inot
);
785 status
= inot
->ccb_h
.status
;
786 sense
= (status
& CAM_AUTOSNS_VALID
) != 0;
787 status
&= CAM_STATUS_MASK
;
790 case CAM_SCSI_BUS_RESET
:
791 tcmd_ua(CAM_TARGET_WILDCARD
, UA_BUS_RESET
);
795 tcmd_ua(CAM_TARGET_WILDCARD
, UA_BDR
);
798 case CAM_MESSAGE_RECV
:
799 switch (inot
->message_args
[0]) {
800 case MSG_TASK_COMPLETE
:
801 case MSG_INITIATOR_DET_ERR
:
802 case MSG_ABORT_TASK_SET
:
803 case MSG_MESSAGE_REJECT
:
805 case MSG_PARITY_ERROR
:
806 case MSG_TARGET_RESET
:
808 case MSG_CLEAR_TASK_SET
:
810 warnx("INOT message %#x", inot
->message_args
[0]);
814 case CAM_REQ_ABORTED
:
815 warnx("INOT %p aborted", inot
);
818 warnx("Unhandled INOT status %#x", status
);
822 /* If there is sense data, use it */
824 struct scsi_sense_data
*sense
;
826 sense
= &inot
->sense_data
;
827 tcmd_sense(inot
->initiator_id
, NULL
, sense
->flags
,
828 sense
->add_sense_code
, sense
->add_sense_code_qual
);
830 warnx("INOT has sense: %#x", sense
->flags
);
834 TAILQ_REMOVE(&work_queue
, &inot
->ccb_h
, periph_links
.tqe
);
835 send_ccb((union ccb
*)inot
, /*priority*/1);
841 send_ccb(union ccb
*ccb
, int priority
)
844 warnx("sending ccb (%#x)", ccb
->ccb_h
.func_code
);
845 ccb
->ccb_h
.pinfo
.priority
= priority
;
846 if (XPT_FC_IS_QUEUED(ccb
)) {
847 TAILQ_INSERT_TAIL(&pending_queue
, &ccb
->ccb_h
,
850 if (write(targ_fd
, &ccb
, sizeof(ccb
)) != sizeof(ccb
)) {
852 ccb
->ccb_h
.status
= CAM_PROVIDE_FAIL
;
856 /* Return a CTIO/descr/buf combo from the freelist or malloc one */
857 static struct ccb_scsiio
*
860 struct ccb_scsiio
*ctio
;
861 struct ctio_descr
*c_descr
;
864 if (num_ctios
== MAX_CTIOS
) {
865 warnx("at CTIO max");
869 ctio
= (struct ccb_scsiio
*)malloc(sizeof(*ctio
));
874 c_descr
= (struct ctio_descr
*)malloc(sizeof(*c_descr
));
875 if (c_descr
== NULL
) {
877 warn("malloc ctio_descr");
880 c_descr
->buf
= malloc(buf_size
);
881 if (c_descr
->buf
== NULL
) {
884 warn("malloc backing store");
889 /* Initialize CTIO, CTIO descr, and AIO */
890 ctio
->ccb_h
.func_code
= XPT_CONT_TARGET_IO
;
891 ctio
->ccb_h
.retry_count
= 2;
892 ctio
->ccb_h
.timeout
= CAM_TIME_INFINITY
;
893 ctio
->data_ptr
= c_descr
->buf
;
894 ctio
->ccb_h
.targ_descr
= c_descr
;
895 c_descr
->aiocb
.aio_buf
= c_descr
->buf
;
896 c_descr
->aiocb
.aio_fildes
= file_fd
;
897 se
= &c_descr
->aiocb
.aio_sigevent
;
898 se
->sigev_notify
= SIGEV_KEVENT
;
899 se
->sigev_notify_kqueue
= kq_fd
;
900 se
->sigev_value
.sival_ptr
= ctio
;
906 free_ccb(union ccb
*ccb
)
908 switch (ccb
->ccb_h
.func_code
) {
909 case XPT_CONT_TARGET_IO
:
911 struct ctio_descr
*c_descr
;
913 c_descr
= (struct ctio_descr
*)ccb
->ccb_h
.targ_descr
;
918 case XPT_ACCEPT_TARGET_IO
:
919 free(ccb
->ccb_h
.targ_descr
);
921 case XPT_IMMED_NOTIFY
:
929 get_sim_flags(u_int16_t
*flags
)
931 struct ccb_pathinq cpi
;
934 /* Find SIM capabilities */
935 bzero(&cpi
, sizeof(cpi
));
936 cpi
.ccb_h
.func_code
= XPT_PATH_INQ
;
937 send_ccb((union ccb
*)&cpi
, /*priority*/1);
938 status
= cpi
.ccb_h
.status
& CAM_STATUS_MASK
;
939 if (status
!= CAM_REQ_CMP
) {
940 fprintf(stderr
, "CPI failed, status %#x\n", status
);
944 /* Can only enable on controllers that support target mode */
945 if ((cpi
.target_sprt
& PIT_PROCESSOR
) == 0) {
946 fprintf(stderr
, "HBA does not support target mode\n");
947 status
= CAM_PATH_INVALID
;
951 *flags
= cpi
.hba_inquiry
;
958 struct ccb_relsim crs
;
960 bzero(&crs
, sizeof(crs
));
961 crs
.ccb_h
.func_code
= XPT_REL_SIMQ
;
962 crs
.release_flags
= RELSIM_RELEASE_AFTER_QEMPTY
;
964 crs
.release_timeout
= 0;
966 send_ccb((union ccb
*)&crs
, /*priority*/0);
969 /* Cancel all pending CCBs. */
973 struct ccb_abort cab
;
974 struct ccb_hdr
*ccb_h
;
977 warnx("abort_all_pending");
979 bzero(&cab
, sizeof(cab
));
980 cab
.ccb_h
.func_code
= XPT_ABORT
;
981 TAILQ_FOREACH(ccb_h
, &pending_queue
, periph_links
.tqe
) {
983 warnx("Aborting pending CCB %p\n", ccb_h
);
984 cab
.abort_ccb
= (union ccb
*)ccb_h
;
985 send_ccb((union ccb
*)&cab
, /*priority*/1);
986 if (cab
.ccb_h
.status
!= CAM_REQ_CMP
) {
987 warnx("Unable to abort CCB, status %#x\n",
997 "Usage: scsi_target [-AdSTY] [-b bufsize] [-c sectorsize]\n"
998 "\t\t[-r numbufs] [-s volsize] [-W 8,16,32]\n"
999 "\t\tbus:target:lun filename\n");