2 * Generic SCSI Target Kernel Mode Driver
4 * Copyright (c) 2002 Nate Lawson.
5 * Copyright (c) 1998, 1999, 2001, 2002 Justin T. Gibbs.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $FreeBSD: src/sys/cam/scsi/scsi_target.c,v 1.22.2.7 2003/02/18 22:07:10 njl Exp $
30 * $DragonFly: src/sys/bus/cam/scsi/scsi_target.c,v 1.10 2005/03/15 20:42:14 dillon Exp $
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/malloc.h>
39 #include <sys/vnode.h>
40 #include <sys/devicestat.h>
43 #include "../cam_ccb.h"
44 #include "../cam_periph.h"
45 #include "../cam_xpt_periph.h"
46 #include "scsi_targetio.h"
48 /* Transaction information attached to each CCB sent by the user */
49 struct targ_cmd_descr {
50 struct cam_periph_map_info mapinfo;
51 TAILQ_ENTRY(targ_cmd_descr) tqe;
57 /* Offset into the private CCB area for storing our descriptor */
58 #define targ_descr periph_priv.entries[1].ptr
60 TAILQ_HEAD(descr_queue, targ_cmd_descr);
63 TARG_STATE_RESV = 0x00, /* Invalid state */
64 TARG_STATE_OPENED = 0x01, /* Device opened, softc initialized */
65 TARG_STATE_LUN_ENABLED = 0x02 /* Device enabled for a path */
68 /* Per-instance device software context */
70 /* CCBs (CTIOs, ATIOs, INOTs) pending on the controller */
71 struct ccb_queue pending_ccb_queue;
73 /* Command descriptors awaiting CTIO resources from the XPT */
74 struct descr_queue work_queue;
76 /* Command descriptors that have been aborted back to the user. */
77 struct descr_queue abort_queue;
80 * Queue of CCBs that have been copied out to userland, but our
81 * userland daemon has not yet seen.
83 struct ccb_queue user_ccb_queue;
85 struct cam_periph *periph;
86 struct cam_path *path;
88 struct selinfo read_select;
89 struct devstat device_stats;
92 static d_open_t targopen;
93 static d_close_t targclose;
94 static d_read_t targread;
95 static d_write_t targwrite;
96 static d_ioctl_t targioctl;
97 static d_poll_t targpoll;
98 static d_kqfilter_t targkqfilter;
99 static void targreadfiltdetach(struct knote *kn);
100 static int targreadfilt(struct knote *kn, long hint);
101 static struct filterops targread_filtops =
102 { 1, NULL, targreadfiltdetach, targreadfilt };
104 #define TARG_CDEV_MAJOR 65
105 static struct cdevsw targ_cdevsw = {
107 /* maj */ TARG_CDEV_MAJOR,
108 /* flags */ D_KQFILTER,
113 /* close */ targclose,
115 /* write */ targwrite,
116 /* ioctl */ targioctl,
119 /* strategy */ nostrategy,
122 /* kqfilter */ targkqfilter
125 static cam_status targendislun(struct cam_path *path, int enable,
126 int grp6_len, int grp7_len);
127 static cam_status targenable(struct targ_softc *softc,
128 struct cam_path *path,
129 int grp6_len, int grp7_len);
130 static cam_status targdisable(struct targ_softc *softc);
131 static periph_ctor_t targctor;
132 static periph_dtor_t targdtor;
133 static periph_start_t targstart;
134 static int targusermerge(struct targ_softc *softc,
135 struct targ_cmd_descr *descr,
137 static int targsendccb(struct targ_softc *softc, union ccb *ccb,
138 struct targ_cmd_descr *descr);
139 static void targdone(struct cam_periph *periph,
140 union ccb *done_ccb);
141 static int targreturnccb(struct targ_softc *softc,
143 static union ccb * targgetccb(struct targ_softc *softc, xpt_opcode type,
145 static void targfreeccb(struct targ_softc *softc, union ccb *ccb);
146 static struct targ_cmd_descr *
147 targgetdescr(struct targ_softc *softc);
148 static periph_init_t targinit;
149 static void targasync(void *callback_arg, u_int32_t code,
150 struct cam_path *path, void *arg);
151 static void abort_all_pending(struct targ_softc *softc);
152 static void notify_user(struct targ_softc *softc);
153 static int targcamstatus(cam_status status);
154 static size_t targccblen(xpt_opcode func_code);
156 static struct periph_driver targdriver =
159 TAILQ_HEAD_INITIALIZER(targdriver.units), /* generation */ 0
161 DATA_SET(periphdriver_set, targdriver);
163 static MALLOC_DEFINE(M_TARG, "TARG", "TARG data");
165 /* Create softc and initialize it. Only one proc can open each targ device. */
167 targopen(dev_t dev, int flags, int fmt, struct proc *p)
169 struct targ_softc *softc;
171 if (dev->si_drv1 != 0) {
175 /* Mark device busy before any potentially blocking operations */
176 dev->si_drv1 = (void *)~0;
177 reference_dev(dev); /* save ref for later destroy_dev() */
179 /* Create the targ device, allocate its softc, initialize it */
180 make_dev(&targ_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
181 "targ%d", lminor(dev));
182 MALLOC(softc, struct targ_softc *, sizeof(*softc), M_TARG,
184 dev->si_drv1 = softc;
185 softc->state = TARG_STATE_OPENED;
186 softc->periph = NULL;
189 TAILQ_INIT(&softc->pending_ccb_queue);
190 TAILQ_INIT(&softc->work_queue);
191 TAILQ_INIT(&softc->abort_queue);
192 TAILQ_INIT(&softc->user_ccb_queue);
197 /* Disable LUN if enabled and teardown softc */
199 targclose(dev_t dev, int flag, int fmt, struct proc *p)
201 struct targ_softc *softc;
204 softc = (struct targ_softc *)dev->si_drv1;
205 error = targdisable(softc);
206 if (error == CAM_REQ_CMP) {
208 if (softc->periph != NULL) {
209 cam_periph_invalidate(softc->periph);
210 softc->periph = NULL;
212 destroy_dev(dev); /* eats the open ref */
220 /* Enable/disable LUNs, set debugging level */
222 targioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
224 struct targ_softc *softc;
227 softc = (struct targ_softc *)dev->si_drv1;
232 struct ioc_enable_lun *new_lun;
233 struct cam_path *path;
235 new_lun = (struct ioc_enable_lun *)addr;
236 status = xpt_create_path(&path, /*periph*/NULL,
240 if (status != CAM_REQ_CMP) {
241 printf("Couldn't create path, status %#x\n", status);
244 status = targenable(softc, path, new_lun->grp6_len,
250 status = targdisable(softc);
255 struct ccb_debug cdbg;
257 bzero(&cdbg, sizeof cdbg);
258 if (*((int *)addr) != 0)
259 cdbg.flags = CAM_DEBUG_PERIPH;
261 cdbg.flags = CAM_DEBUG_NONE;
262 xpt_setup_ccb(&cdbg.ccb_h, softc->path, /*priority*/0);
263 cdbg.ccb_h.func_code = XPT_DEBUG;
264 cdbg.ccb_h.cbfcnp = targdone;
266 /* If no periph available, disallow debugging changes */
267 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) {
268 status = CAM_DEV_NOT_THERE;
271 xpt_action((union ccb *)&cdbg);
272 status = cdbg.ccb_h.status & CAM_STATUS_MASK;
274 status = CAM_FUNC_NOTAVAIL;
279 status = CAM_PROVIDE_FAIL;
283 return (targcamstatus(status));
286 /* Writes are always ready, reads wait for user_ccb_queue or abort_queue */
288 targpoll(dev_t dev, int poll_events, struct proc *p)
290 struct targ_softc *softc;
293 softc = (struct targ_softc *)dev->si_drv1;
295 /* Poll for write() is always ok. */
296 revents = poll_events & (POLLOUT | POLLWRNORM);
297 if ((poll_events & (POLLIN | POLLRDNORM)) != 0) {
299 /* Poll for read() depends on user and abort queues. */
300 if (!TAILQ_EMPTY(&softc->user_ccb_queue) ||
301 !TAILQ_EMPTY(&softc->abort_queue)) {
302 revents |= poll_events & (POLLIN | POLLRDNORM);
304 /* Only sleep if the user didn't poll for write. */
306 selrecord(p, &softc->read_select);
314 targkqfilter(dev_t dev, struct knote *kn)
316 struct targ_softc *softc;
319 softc = (struct targ_softc *)dev->si_drv1;
320 kn->kn_hook = (caddr_t)softc;
321 kn->kn_fop = &targread_filtops;
323 SLIST_INSERT_HEAD(&softc->read_select.si_note, kn, kn_selnext);
329 targreadfiltdetach(struct knote *kn)
331 struct targ_softc *softc;
334 softc = (struct targ_softc *)kn->kn_hook;
336 SLIST_REMOVE(&softc->read_select.si_note, kn, knote, kn_selnext);
340 /* Notify the user's kqueue when the user queue or abort queue gets a CCB */
342 targreadfilt(struct knote *kn, long hint)
344 struct targ_softc *softc;
347 softc = (struct targ_softc *)kn->kn_hook;
349 retval = !TAILQ_EMPTY(&softc->user_ccb_queue) ||
350 !TAILQ_EMPTY(&softc->abort_queue);
355 /* Send the HBA the enable/disable message */
357 targendislun(struct cam_path *path, int enable, int grp6_len, int grp7_len)
359 struct ccb_en_lun en_ccb;
362 /* Tell the lun to begin answering selects */
363 xpt_setup_ccb(&en_ccb.ccb_h, path, /*priority*/1);
364 en_ccb.ccb_h.func_code = XPT_EN_LUN;
365 /* Don't need support for any vendor specific commands */
366 en_ccb.grp6_len = grp6_len;
367 en_ccb.grp7_len = grp7_len;
368 en_ccb.enable = enable ? 1 : 0;
369 xpt_action((union ccb *)&en_ccb);
370 status = en_ccb.ccb_h.status & CAM_STATUS_MASK;
371 if (status != CAM_REQ_CMP) {
372 xpt_print_path(path);
373 printf("%sable lun CCB rejected, status %#x\n",
374 enable ? "en" : "dis", status);
379 /* Enable target mode on a LUN, given its path */
381 targenable(struct targ_softc *softc, struct cam_path *path, int grp6_len,
384 struct cam_periph *periph;
385 struct ccb_pathinq cpi;
388 if ((softc->state & TARG_STATE_LUN_ENABLED) != 0)
389 return (CAM_LUN_ALRDY_ENA);
391 /* Make sure SIM supports target mode */
392 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
393 cpi.ccb_h.func_code = XPT_PATH_INQ;
394 xpt_action((union ccb *)&cpi);
395 status = cpi.ccb_h.status & CAM_STATUS_MASK;
396 if (status != CAM_REQ_CMP) {
397 printf("pathinq failed, status %#x\n", status);
400 if ((cpi.target_sprt & PIT_PROCESSOR) == 0) {
401 printf("controller does not support target mode\n");
402 status = CAM_FUNC_NOTAVAIL;
406 /* Destroy any periph on our path if it is disabled */
407 periph = cam_periph_find(path, "targ");
408 if (periph != NULL) {
409 struct targ_softc *del_softc;
411 del_softc = (struct targ_softc *)periph->softc;
412 if ((del_softc->state & TARG_STATE_LUN_ENABLED) == 0) {
413 cam_periph_invalidate(del_softc->periph);
414 del_softc->periph = NULL;
416 printf("Requested path still in use by targ%d\n",
417 periph->unit_number);
418 status = CAM_LUN_ALRDY_ENA;
423 /* Create a periph instance attached to this path */
424 status = cam_periph_alloc(targctor, NULL, targdtor, targstart,
425 "targ", CAM_PERIPH_BIO, path, targasync, 0, softc);
426 if (status != CAM_REQ_CMP) {
427 printf("cam_periph_alloc failed, status %#x\n", status);
431 /* Ensure that the periph now exists. */
432 if (cam_periph_find(path, "targ") == NULL) {
433 panic("targenable: succeeded but no periph?");
437 /* Send the enable lun message */
438 status = targendislun(path, /*enable*/1, grp6_len, grp7_len);
439 if (status != CAM_REQ_CMP) {
440 printf("enable lun failed, status %#x\n", status);
443 softc->state |= TARG_STATE_LUN_ENABLED;
449 /* Disable this softc's target instance if enabled */
451 targdisable(struct targ_softc *softc)
456 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0)
457 return (CAM_REQ_CMP);
459 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targdisable\n"));
461 /* Abort any ccbs pending on the controller */
463 abort_all_pending(softc);
466 /* Disable this lun */
467 status = targendislun(softc->path, /*enable*/0,
468 /*grp6_len*/0, /*grp7_len*/0);
469 if (status == CAM_REQ_CMP)
470 softc->state &= ~TARG_STATE_LUN_ENABLED;
472 printf("Disable lun failed, status %#x\n", status);
477 /* Initialize a periph (called from cam_periph_alloc) */
479 targctor(struct cam_periph *periph, void *arg)
481 struct targ_softc *softc;
483 /* Store pointer to softc for periph-driven routines */
484 softc = (struct targ_softc *)arg;
485 periph->softc = softc;
486 softc->periph = periph;
487 softc->path = periph->path;
488 return (CAM_REQ_CMP);
492 targdtor(struct cam_periph *periph)
494 struct targ_softc *softc;
495 struct ccb_hdr *ccb_h;
496 struct targ_cmd_descr *descr;
498 softc = (struct targ_softc *)periph->softc;
501 * targdisable() aborts CCBs back to the user and leaves them
502 * on user_ccb_queue and abort_queue in case the user is still
503 * interested in them. We free them now.
505 while ((ccb_h = TAILQ_FIRST(&softc->user_ccb_queue)) != NULL) {
506 TAILQ_REMOVE(&softc->user_ccb_queue, ccb_h, periph_links.tqe);
507 targfreeccb(softc, (union ccb *)ccb_h);
509 while ((descr = TAILQ_FIRST(&softc->abort_queue)) != NULL) {
510 TAILQ_REMOVE(&softc->abort_queue, descr, tqe);
514 softc->periph = NULL;
516 periph->softc = NULL;
519 /* Receive CCBs from user mode proc and send them to the HBA */
521 targwrite(dev_t dev, struct uio *uio, int ioflag)
524 struct targ_softc *softc;
525 struct targ_cmd_descr *descr;
526 int write_len, error, s;
527 int func_code, priority;
529 softc = (struct targ_softc *)dev->si_drv1;
530 write_len = error = 0;
531 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
532 ("write - uio_resid %d\n", uio->uio_resid));
533 while (uio->uio_resid >= sizeof(user_ccb) && error == 0) {
536 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
538 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
539 ("write - uiomove failed (%d)\n", error));
542 priority = fuword(&user_ccb->ccb_h.pinfo.priority);
543 if (priority == -1) {
547 func_code = fuword(&user_ccb->ccb_h.func_code);
549 case XPT_ACCEPT_TARGET_IO:
550 case XPT_IMMED_NOTIFY:
551 ccb = targgetccb(softc, func_code, priority);
552 descr = (struct targ_cmd_descr *)ccb->ccb_h.targ_descr;
553 descr->user_ccb = user_ccb;
554 descr->func_code = func_code;
555 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
556 ("Sent ATIO/INOT (%p)\n", user_ccb));
559 TAILQ_INSERT_TAIL(&softc->pending_ccb_queue,
565 if ((func_code & XPT_FC_QUEUED) != 0) {
566 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
567 ("Sending queued ccb %#x (%p)\n",
568 func_code, user_ccb));
569 descr = targgetdescr(softc);
570 descr->user_ccb = user_ccb;
571 descr->priority = priority;
572 descr->func_code = func_code;
574 TAILQ_INSERT_TAIL(&softc->work_queue,
577 xpt_schedule(softc->periph, priority);
579 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
580 ("Sending inline ccb %#x (%p)\n",
581 func_code, user_ccb));
582 ccb = targgetccb(softc, func_code, priority);
583 descr = (struct targ_cmd_descr *)
584 ccb->ccb_h.targ_descr;
585 descr->user_ccb = user_ccb;
586 descr->priority = priority;
587 descr->func_code = func_code;
588 if (targusermerge(softc, descr, ccb) != EFAULT)
589 targsendccb(softc, ccb, descr);
590 targreturnccb(softc, ccb);
594 write_len += sizeof(user_ccb);
598 * If we've successfully taken in some amount of
599 * data, return success for that data first. If
600 * an error is persistent, it will be reported
603 if (error != 0 && write_len == 0)
605 if (write_len == 0 && uio->uio_resid != 0)
610 /* Process requests (descrs) via the periph-supplied CCBs */
612 targstart(struct cam_periph *periph, union ccb *start_ccb)
614 struct targ_softc *softc;
615 struct targ_cmd_descr *descr, *next_descr;
618 softc = (struct targ_softc *)periph->softc;
619 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targstart %p\n", start_ccb));
622 descr = TAILQ_FIRST(&softc->work_queue);
625 xpt_release_ccb(start_ccb);
627 TAILQ_REMOVE(&softc->work_queue, descr, tqe);
628 next_descr = TAILQ_FIRST(&softc->work_queue);
631 /* Initiate a transaction using the descr and supplied CCB */
632 error = targusermerge(softc, descr, start_ccb);
634 error = targsendccb(softc, start_ccb, descr);
636 xpt_print_path(periph->path);
637 printf("targsendccb failed, err %d\n", error);
638 xpt_release_ccb(start_ccb);
639 suword(&descr->user_ccb->ccb_h.status,
642 TAILQ_INSERT_TAIL(&softc->abort_queue, descr, tqe);
647 /* If we have more work to do, stay scheduled */
648 if (next_descr != NULL)
649 xpt_schedule(periph, next_descr->priority);
654 targusermerge(struct targ_softc *softc, struct targ_cmd_descr *descr,
657 struct ccb_hdr *u_ccbh, *k_ccbh;
661 u_ccbh = &descr->user_ccb->ccb_h;
662 k_ccbh = &ccb->ccb_h;
665 * There are some fields in the CCB header that need to be
666 * preserved, the rest we get from the user ccb. (See xpt_merge_ccb)
668 xpt_setup_ccb(k_ccbh, softc->path, descr->priority);
669 k_ccbh->retry_count = fuword(&u_ccbh->retry_count);
670 k_ccbh->func_code = descr->func_code;
671 k_ccbh->flags = fuword(&u_ccbh->flags);
672 k_ccbh->timeout = fuword(&u_ccbh->timeout);
673 ccb_len = targccblen(k_ccbh->func_code) - sizeof(struct ccb_hdr);
674 error = copyin(u_ccbh + 1, k_ccbh + 1, ccb_len);
676 k_ccbh->status = CAM_REQ_CMP_ERR;
680 /* Translate usermode abort_ccb pointer to its kernel counterpart */
681 if (k_ccbh->func_code == XPT_ABORT) {
682 struct ccb_abort *cab;
683 struct ccb_hdr *ccb_h;
686 cab = (struct ccb_abort *)ccb;
688 TAILQ_FOREACH(ccb_h, &softc->pending_ccb_queue,
690 struct targ_cmd_descr *ab_descr;
692 ab_descr = (struct targ_cmd_descr *)ccb_h->targ_descr;
693 if (ab_descr->user_ccb == cab->abort_ccb) {
694 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
695 ("Changing abort for %p to %p\n",
696 cab->abort_ccb, ccb_h));
697 cab->abort_ccb = (union ccb *)ccb_h;
702 /* CCB not found, set appropriate status */
704 k_ccbh->status = CAM_PATH_INVALID;
712 /* Build and send a kernel CCB formed from descr->user_ccb */
714 targsendccb(struct targ_softc *softc, union ccb *ccb,
715 struct targ_cmd_descr *descr)
717 struct cam_periph_map_info *mapinfo;
718 struct ccb_hdr *ccb_h;
722 mapinfo = &descr->mapinfo;
723 mapinfo->num_bufs_used = 0;
726 * There's no way for the user to have a completion
727 * function, so we put our own completion function in here.
728 * We also stash in a reference to our descriptor so targreturnccb()
729 * can find our mapping info.
731 ccb_h->cbfcnp = targdone;
732 ccb_h->targ_descr = descr;
735 * We only attempt to map the user memory into kernel space
736 * if they haven't passed in a physical memory pointer,
737 * and if there is actually an I/O operation to perform.
738 * Right now cam_periph_mapmem() only supports SCSI and device
739 * match CCBs. For the SCSI CCBs, we only pass the CCB in if
740 * there's actually data to map. cam_periph_mapmem() will do the
741 * right thing, even if there isn't data to map, but since CCBs
742 * without data are a reasonably common occurance (e.g. test unit
743 * ready), it will save a few cycles if we check for it here.
745 if (((ccb_h->flags & CAM_DATA_PHYS) == 0)
746 && (((ccb_h->func_code == XPT_CONT_TARGET_IO)
747 && ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE))
748 || (ccb_h->func_code == XPT_DEV_MATCH))) {
750 error = cam_periph_mapmem(ccb, mapinfo);
753 * cam_periph_mapmem returned an error, we can't continue.
754 * Return the error to the user.
757 ccb_h->status = CAM_REQ_CMP_ERR;
758 mapinfo->num_bufs_used = 0;
764 * Once queued on the pending CCB list, this CCB will be protected
765 * by our error recovery handler.
767 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("sendccb %p\n", ccb));
768 if (XPT_FC_IS_QUEUED(ccb)) {
772 TAILQ_INSERT_TAIL(&softc->pending_ccb_queue, ccb_h,
781 /* Completion routine for CCBs (called at splsoftcam) */
783 targdone(struct cam_periph *periph, union ccb *done_ccb)
785 struct targ_softc *softc;
788 CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("targdone %p\n", done_ccb));
789 softc = (struct targ_softc *)periph->softc;
790 TAILQ_REMOVE(&softc->pending_ccb_queue, &done_ccb->ccb_h,
792 status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
794 /* If we're no longer enabled, throw away CCB */
795 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) {
796 targfreeccb(softc, done_ccb);
799 /* abort_all_pending() waits for pending queue to be empty */
800 if (TAILQ_EMPTY(&softc->pending_ccb_queue))
801 wakeup(&softc->pending_ccb_queue);
803 switch (done_ccb->ccb_h.func_code) {
804 /* All FC_*_QUEUED CCBs go back to userland */
805 case XPT_IMMED_NOTIFY:
806 case XPT_ACCEPT_TARGET_IO:
807 case XPT_CONT_TARGET_IO:
808 TAILQ_INSERT_TAIL(&softc->user_ccb_queue, &done_ccb->ccb_h,
813 panic("targdone: impossible xpt opcode %#x",
814 done_ccb->ccb_h.func_code);
819 /* Return CCBs to the user from the user queue and abort queue */
821 targread(dev_t dev, struct uio *uio, int ioflag)
823 struct descr_queue *abort_queue;
824 struct targ_cmd_descr *user_descr;
825 struct targ_softc *softc;
826 struct ccb_queue *user_queue;
827 struct ccb_hdr *ccb_h;
829 int read_len, error, s;
833 softc = (struct targ_softc *)dev->si_drv1;
834 user_queue = &softc->user_ccb_queue;
835 abort_queue = &softc->abort_queue;
836 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targread\n"));
838 /* If no data is available, wait or return immediately */
840 ccb_h = TAILQ_FIRST(user_queue);
841 user_descr = TAILQ_FIRST(abort_queue);
842 while (ccb_h == NULL && user_descr == NULL) {
843 if ((ioflag & IO_NDELAY) == 0) {
844 error = tsleep(user_queue, PCATCH, "targrd", 0);
845 ccb_h = TAILQ_FIRST(user_queue);
846 user_descr = TAILQ_FIRST(abort_queue);
848 if (error == ERESTART) {
861 /* Data is available so fill the user's buffer */
862 while (ccb_h != NULL) {
863 struct targ_cmd_descr *descr;
865 if (uio->uio_resid < sizeof(user_ccb))
867 TAILQ_REMOVE(user_queue, ccb_h, periph_links.tqe);
869 descr = (struct targ_cmd_descr *)ccb_h->targ_descr;
870 user_ccb = descr->user_ccb;
871 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
872 ("targread ccb %p (%p)\n", ccb_h, user_ccb));
873 error = targreturnccb(softc, (union ccb *)ccb_h);
876 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
879 read_len += sizeof(user_ccb);
882 ccb_h = TAILQ_FIRST(user_queue);
885 /* Flush out any aborted descriptors */
886 while (user_descr != NULL) {
887 if (uio->uio_resid < sizeof(user_ccb))
889 TAILQ_REMOVE(abort_queue, user_descr, tqe);
891 user_ccb = user_descr->user_ccb;
892 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
893 ("targread aborted descr %p (%p)\n",
894 user_descr, user_ccb));
895 suword(&user_ccb->ccb_h.status, CAM_REQ_ABORTED);
896 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
899 read_len += sizeof(user_ccb);
902 user_descr = TAILQ_FIRST(abort_queue);
907 * If we've successfully read some amount of data, don't report an
908 * error. If the error is persistent, it will be reported on the
911 if (read_len == 0 && uio->uio_resid != 0)
918 /* Copy completed ccb back to the user */
920 targreturnccb(struct targ_softc *softc, union ccb *ccb)
922 struct targ_cmd_descr *descr;
923 struct ccb_hdr *u_ccbh;
927 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targreturnccb %p\n", ccb));
928 descr = (struct targ_cmd_descr *)ccb->ccb_h.targ_descr;
929 u_ccbh = &descr->user_ccb->ccb_h;
931 /* Copy out the central portion of the ccb_hdr */
932 copyout(&ccb->ccb_h.retry_count, &u_ccbh->retry_count,
933 offsetof(struct ccb_hdr, periph_priv) -
934 offsetof(struct ccb_hdr, retry_count));
936 /* Copy out the rest of the ccb (after the ccb_hdr) */
937 ccb_len = targccblen(ccb->ccb_h.func_code) - sizeof(struct ccb_hdr);
938 if (descr->mapinfo.num_bufs_used != 0)
939 cam_periph_unmapmem(ccb, &descr->mapinfo);
940 error = copyout(&ccb->ccb_h + 1, u_ccbh + 1, ccb_len);
942 xpt_print_path(softc->path);
943 printf("targreturnccb - CCB copyout failed (%d)\n",
946 /* Free CCB or send back to devq. */
947 targfreeccb(softc, ccb);
953 targgetccb(struct targ_softc *softc, xpt_opcode type, int priority)
958 ccb_len = targccblen(type);
959 MALLOC(ccb, union ccb *, ccb_len, M_TARG, M_INTWAIT);
960 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("getccb %p\n", ccb));
962 xpt_setup_ccb(&ccb->ccb_h, softc->path, priority);
963 ccb->ccb_h.func_code = type;
964 ccb->ccb_h.cbfcnp = targdone;
965 ccb->ccb_h.targ_descr = targgetdescr(softc);
970 targfreeccb(struct targ_softc *softc, union ccb *ccb)
972 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, ("targfreeccb descr %p and\n",
973 ccb->ccb_h.targ_descr));
974 FREE(ccb->ccb_h.targ_descr, M_TARG);
976 switch (ccb->ccb_h.func_code) {
977 case XPT_ACCEPT_TARGET_IO:
978 case XPT_IMMED_NOTIFY:
979 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, ("freeing ccb %p\n", ccb));
983 /* Send back CCB if we got it from the periph */
984 if (XPT_FC_IS_QUEUED(ccb)) {
985 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH,
986 ("returning queued ccb %p\n", ccb));
987 xpt_release_ccb(ccb);
989 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH,
990 ("freeing ccb %p\n", ccb));
997 static struct targ_cmd_descr *
998 targgetdescr(struct targ_softc *softc)
1000 struct targ_cmd_descr *descr;
1002 MALLOC(descr, struct targ_cmd_descr *, sizeof(*descr),
1004 descr->mapinfo.num_bufs_used = 0;
1011 cdevsw_add(&targ_cdevsw, 0, 0);
1015 targasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
1017 /* All events are handled in usermode by INOTs */
1018 panic("targasync() called, should be an INOT instead");
1021 /* Cancel all pending requests and CCBs awaiting work. */
1023 abort_all_pending(struct targ_softc *softc)
1025 struct targ_cmd_descr *descr;
1026 struct ccb_abort cab;
1027 struct ccb_hdr *ccb_h;
1029 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("abort_all_pending\n"));
1031 /* First abort the descriptors awaiting resources */
1032 while ((descr = TAILQ_FIRST(&softc->work_queue)) != NULL) {
1033 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
1034 ("Aborting descr from workq %p\n", descr));
1035 TAILQ_REMOVE(&softc->work_queue, descr, tqe);
1036 TAILQ_INSERT_TAIL(&softc->abort_queue, descr, tqe);
1040 * Then abort all pending CCBs.
1041 * targdone() will return the aborted CCB via user_ccb_queue
1043 xpt_setup_ccb(&cab.ccb_h, softc->path, /*priority*/0);
1044 cab.ccb_h.func_code = XPT_ABORT;
1045 cab.ccb_h.status = CAM_REQ_CMP_ERR;
1046 TAILQ_FOREACH(ccb_h, &softc->pending_ccb_queue, periph_links.tqe) {
1047 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
1048 ("Aborting pending CCB %p\n", ccb_h));
1049 cab.abort_ccb = (union ccb *)ccb_h;
1050 xpt_action((union ccb *)&cab);
1051 if (cab.ccb_h.status != CAM_REQ_CMP) {
1052 xpt_print_path(cab.ccb_h.path);
1053 printf("Unable to abort CCB, status %#x\n",
1058 /* If we aborted at least one pending CCB ok, wait for it. */
1059 if (cab.ccb_h.status == CAM_REQ_CMP) {
1060 tsleep(&softc->pending_ccb_queue, PCATCH, "tgabrt", 0);
1063 /* If we aborted anything from the work queue, wakeup user. */
1064 if (!TAILQ_EMPTY(&softc->user_ccb_queue)
1065 || !TAILQ_EMPTY(&softc->abort_queue))
1069 /* Notify the user that data is ready */
1071 notify_user(struct targ_softc *softc)
1074 * Notify users sleeping via poll(), kqueue(), and
1077 selwakeup(&softc->read_select);
1078 KNOTE(&softc->read_select.si_note, 0);
1079 wakeup(&softc->user_ccb_queue);
1082 /* Convert CAM status to errno values */
1084 targcamstatus(cam_status status)
1086 switch (status & CAM_STATUS_MASK) {
1087 case CAM_REQ_CMP: /* CCB request completed without error */
1089 case CAM_REQ_INPROG: /* CCB request is in progress */
1090 return (EINPROGRESS);
1091 case CAM_REQ_CMP_ERR: /* CCB request completed with an error */
1093 case CAM_PROVIDE_FAIL: /* Unable to provide requested capability */
1095 case CAM_FUNC_NOTAVAIL: /* The requested function is not available */
1097 case CAM_LUN_ALRDY_ENA: /* LUN is already enabled for target mode */
1098 return (EADDRINUSE);
1099 case CAM_PATH_INVALID: /* Supplied Path ID is invalid */
1100 case CAM_DEV_NOT_THERE: /* SCSI Device Not Installed/there */
1102 case CAM_REQ_ABORTED: /* CCB request aborted by the host */
1104 case CAM_CMD_TIMEOUT: /* Command timeout */
1106 case CAM_REQUEUE_REQ: /* Requeue to preserve transaction ordering */
1108 case CAM_REQ_INVALID: /* CCB request was invalid */
1110 case CAM_RESRC_UNAVAIL: /* Resource Unavailable */
1112 case CAM_BUSY: /* CAM subsytem is busy */
1113 case CAM_UA_ABORT: /* Unable to abort CCB request */
1121 targccblen(xpt_opcode func_code)
1125 /* Codes we expect to see as a target */
1126 switch (func_code) {
1127 case XPT_CONT_TARGET_IO:
1129 len = sizeof(struct ccb_scsiio);
1131 case XPT_ACCEPT_TARGET_IO:
1132 len = sizeof(struct ccb_accept_tio);
1134 case XPT_IMMED_NOTIFY:
1135 len = sizeof(struct ccb_immed_notify);
1138 len = sizeof(struct ccb_relsim);
1141 len = sizeof(struct ccb_pathinq);
1144 len = sizeof(struct ccb_debug);
1147 len = sizeof(struct ccb_abort);
1150 len = sizeof(struct ccb_en_lun);
1153 len = sizeof(union ccb);