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.3 2003/07/19 21:14:15 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/cam_ccb.h>
44 #include <cam/cam_periph.h>
45 #include <cam/cam_xpt_periph.h>
46 #include <cam/scsi/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 /* close */ targclose,
109 /* write */ targwrite,
110 /* ioctl */ targioctl,
113 /* strategy */ nostrategy,
115 /* maj */ TARG_CDEV_MAJOR,
118 /* flags */ D_KQFILTER,
120 /* kqfilter */ targkqfilter
123 static cam_status targendislun(struct cam_path *path, int enable,
124 int grp6_len, int grp7_len);
125 static cam_status targenable(struct targ_softc *softc,
126 struct cam_path *path,
127 int grp6_len, int grp7_len);
128 static cam_status targdisable(struct targ_softc *softc);
129 static periph_ctor_t targctor;
130 static periph_dtor_t targdtor;
131 static periph_start_t targstart;
132 static int targusermerge(struct targ_softc *softc,
133 struct targ_cmd_descr *descr,
135 static int targsendccb(struct targ_softc *softc, union ccb *ccb,
136 struct targ_cmd_descr *descr);
137 static void targdone(struct cam_periph *periph,
138 union ccb *done_ccb);
139 static int targreturnccb(struct targ_softc *softc,
141 static union ccb * targgetccb(struct targ_softc *softc, xpt_opcode type,
143 static void targfreeccb(struct targ_softc *softc, union ccb *ccb);
144 static struct targ_cmd_descr *
145 targgetdescr(struct targ_softc *softc);
146 static periph_init_t targinit;
147 static void targasync(void *callback_arg, u_int32_t code,
148 struct cam_path *path, void *arg);
149 static void abort_all_pending(struct targ_softc *softc);
150 static void notify_user(struct targ_softc *softc);
151 static int targcamstatus(cam_status status);
152 static size_t targccblen(xpt_opcode func_code);
154 static struct periph_driver targdriver =
157 TAILQ_HEAD_INITIALIZER(targdriver.units), /* generation */ 0
159 DATA_SET(periphdriver_set, targdriver);
161 static MALLOC_DEFINE(M_TARG, "TARG", "TARG data");
163 /* Create softc and initialize it. Only one proc can open each targ device. */
165 targopen(dev_t dev, int flags, int fmt, struct proc *p)
167 struct targ_softc *softc;
169 if (dev->si_drv1 != 0) {
173 /* Mark device busy before any potentially blocking operations */
174 dev->si_drv1 = (void *)~0;
176 /* Create the targ device, allocate its softc, initialize it */
177 make_dev(&targ_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
178 "targ%d", lminor(dev));
179 MALLOC(softc, struct targ_softc *, sizeof(*softc), M_TARG,
181 dev->si_drv1 = softc;
182 softc->state = TARG_STATE_OPENED;
183 softc->periph = NULL;
186 TAILQ_INIT(&softc->pending_ccb_queue);
187 TAILQ_INIT(&softc->work_queue);
188 TAILQ_INIT(&softc->abort_queue);
189 TAILQ_INIT(&softc->user_ccb_queue);
194 /* Disable LUN if enabled and teardown softc */
196 targclose(dev_t dev, int flag, int fmt, struct proc *p)
198 struct targ_softc *softc;
201 softc = (struct targ_softc *)dev->si_drv1;
202 error = targdisable(softc);
203 if (error == CAM_REQ_CMP) {
205 if (softc->periph != NULL) {
206 cam_periph_invalidate(softc->periph);
207 softc->periph = NULL;
215 /* Enable/disable LUNs, set debugging level */
217 targioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
219 struct targ_softc *softc;
222 softc = (struct targ_softc *)dev->si_drv1;
227 struct ioc_enable_lun *new_lun;
228 struct cam_path *path;
230 new_lun = (struct ioc_enable_lun *)addr;
231 status = xpt_create_path(&path, /*periph*/NULL,
235 if (status != CAM_REQ_CMP) {
236 printf("Couldn't create path, status %#x\n", status);
239 status = targenable(softc, path, new_lun->grp6_len,
245 status = targdisable(softc);
250 struct ccb_debug cdbg;
252 bzero(&cdbg, sizeof cdbg);
253 if (*((int *)addr) != 0)
254 cdbg.flags = CAM_DEBUG_PERIPH;
256 cdbg.flags = CAM_DEBUG_NONE;
257 xpt_setup_ccb(&cdbg.ccb_h, softc->path, /*priority*/0);
258 cdbg.ccb_h.func_code = XPT_DEBUG;
259 cdbg.ccb_h.cbfcnp = targdone;
261 /* If no periph available, disallow debugging changes */
262 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) {
263 status = CAM_DEV_NOT_THERE;
266 xpt_action((union ccb *)&cdbg);
267 status = cdbg.ccb_h.status & CAM_STATUS_MASK;
269 status = CAM_FUNC_NOTAVAIL;
274 status = CAM_PROVIDE_FAIL;
278 return (targcamstatus(status));
281 /* Writes are always ready, reads wait for user_ccb_queue or abort_queue */
283 targpoll(dev_t dev, int poll_events, struct proc *p)
285 struct targ_softc *softc;
288 softc = (struct targ_softc *)dev->si_drv1;
290 /* Poll for write() is always ok. */
291 revents = poll_events & (POLLOUT | POLLWRNORM);
292 if ((poll_events & (POLLIN | POLLRDNORM)) != 0) {
294 /* Poll for read() depends on user and abort queues. */
295 if (!TAILQ_EMPTY(&softc->user_ccb_queue) ||
296 !TAILQ_EMPTY(&softc->abort_queue)) {
297 revents |= poll_events & (POLLIN | POLLRDNORM);
299 /* Only sleep if the user didn't poll for write. */
301 selrecord(p, &softc->read_select);
309 targkqfilter(dev_t dev, struct knote *kn)
311 struct targ_softc *softc;
314 softc = (struct targ_softc *)dev->si_drv1;
315 kn->kn_hook = (caddr_t)softc;
316 kn->kn_fop = &targread_filtops;
318 SLIST_INSERT_HEAD(&softc->read_select.si_note, kn, kn_selnext);
324 targreadfiltdetach(struct knote *kn)
326 struct targ_softc *softc;
329 softc = (struct targ_softc *)kn->kn_hook;
331 SLIST_REMOVE(&softc->read_select.si_note, kn, knote, kn_selnext);
335 /* Notify the user's kqueue when the user queue or abort queue gets a CCB */
337 targreadfilt(struct knote *kn, long hint)
339 struct targ_softc *softc;
342 softc = (struct targ_softc *)kn->kn_hook;
344 retval = !TAILQ_EMPTY(&softc->user_ccb_queue) ||
345 !TAILQ_EMPTY(&softc->abort_queue);
350 /* Send the HBA the enable/disable message */
352 targendislun(struct cam_path *path, int enable, int grp6_len, int grp7_len)
354 struct ccb_en_lun en_ccb;
357 /* Tell the lun to begin answering selects */
358 xpt_setup_ccb(&en_ccb.ccb_h, path, /*priority*/1);
359 en_ccb.ccb_h.func_code = XPT_EN_LUN;
360 /* Don't need support for any vendor specific commands */
361 en_ccb.grp6_len = grp6_len;
362 en_ccb.grp7_len = grp7_len;
363 en_ccb.enable = enable ? 1 : 0;
364 xpt_action((union ccb *)&en_ccb);
365 status = en_ccb.ccb_h.status & CAM_STATUS_MASK;
366 if (status != CAM_REQ_CMP) {
367 xpt_print_path(path);
368 printf("%sable lun CCB rejected, status %#x\n",
369 enable ? "en" : "dis", status);
374 /* Enable target mode on a LUN, given its path */
376 targenable(struct targ_softc *softc, struct cam_path *path, int grp6_len,
379 struct cam_periph *periph;
380 struct ccb_pathinq cpi;
383 if ((softc->state & TARG_STATE_LUN_ENABLED) != 0)
384 return (CAM_LUN_ALRDY_ENA);
386 /* Make sure SIM supports target mode */
387 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
388 cpi.ccb_h.func_code = XPT_PATH_INQ;
389 xpt_action((union ccb *)&cpi);
390 status = cpi.ccb_h.status & CAM_STATUS_MASK;
391 if (status != CAM_REQ_CMP) {
392 printf("pathinq failed, status %#x\n", status);
395 if ((cpi.target_sprt & PIT_PROCESSOR) == 0) {
396 printf("controller does not support target mode\n");
397 status = CAM_FUNC_NOTAVAIL;
401 /* Destroy any periph on our path if it is disabled */
402 periph = cam_periph_find(path, "targ");
403 if (periph != NULL) {
404 struct targ_softc *del_softc;
406 del_softc = (struct targ_softc *)periph->softc;
407 if ((del_softc->state & TARG_STATE_LUN_ENABLED) == 0) {
408 cam_periph_invalidate(del_softc->periph);
409 del_softc->periph = NULL;
411 printf("Requested path still in use by targ%d\n",
412 periph->unit_number);
413 status = CAM_LUN_ALRDY_ENA;
418 /* Create a periph instance attached to this path */
419 status = cam_periph_alloc(targctor, NULL, targdtor, targstart,
420 "targ", CAM_PERIPH_BIO, path, targasync, 0, softc);
421 if (status != CAM_REQ_CMP) {
422 printf("cam_periph_alloc failed, status %#x\n", status);
426 /* Ensure that the periph now exists. */
427 if (cam_periph_find(path, "targ") == NULL) {
428 panic("targenable: succeeded but no periph?");
432 /* Send the enable lun message */
433 status = targendislun(path, /*enable*/1, grp6_len, grp7_len);
434 if (status != CAM_REQ_CMP) {
435 printf("enable lun failed, status %#x\n", status);
438 softc->state |= TARG_STATE_LUN_ENABLED;
444 /* Disable this softc's target instance if enabled */
446 targdisable(struct targ_softc *softc)
451 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0)
452 return (CAM_REQ_CMP);
454 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targdisable\n"));
456 /* Abort any ccbs pending on the controller */
458 abort_all_pending(softc);
461 /* Disable this lun */
462 status = targendislun(softc->path, /*enable*/0,
463 /*grp6_len*/0, /*grp7_len*/0);
464 if (status == CAM_REQ_CMP)
465 softc->state &= ~TARG_STATE_LUN_ENABLED;
467 printf("Disable lun failed, status %#x\n", status);
472 /* Initialize a periph (called from cam_periph_alloc) */
474 targctor(struct cam_periph *periph, void *arg)
476 struct targ_softc *softc;
478 /* Store pointer to softc for periph-driven routines */
479 softc = (struct targ_softc *)arg;
480 periph->softc = softc;
481 softc->periph = periph;
482 softc->path = periph->path;
483 return (CAM_REQ_CMP);
487 targdtor(struct cam_periph *periph)
489 struct targ_softc *softc;
490 struct ccb_hdr *ccb_h;
491 struct targ_cmd_descr *descr;
493 softc = (struct targ_softc *)periph->softc;
496 * targdisable() aborts CCBs back to the user and leaves them
497 * on user_ccb_queue and abort_queue in case the user is still
498 * interested in them. We free them now.
500 while ((ccb_h = TAILQ_FIRST(&softc->user_ccb_queue)) != NULL) {
501 TAILQ_REMOVE(&softc->user_ccb_queue, ccb_h, periph_links.tqe);
502 targfreeccb(softc, (union ccb *)ccb_h);
504 while ((descr = TAILQ_FIRST(&softc->abort_queue)) != NULL) {
505 TAILQ_REMOVE(&softc->abort_queue, descr, tqe);
509 softc->periph = NULL;
511 periph->softc = NULL;
514 /* Receive CCBs from user mode proc and send them to the HBA */
516 targwrite(dev_t dev, struct uio *uio, int ioflag)
519 struct targ_softc *softc;
520 struct targ_cmd_descr *descr;
521 int write_len, error, s;
522 int func_code, priority;
524 softc = (struct targ_softc *)dev->si_drv1;
525 write_len = error = 0;
526 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
527 ("write - uio_resid %d\n", uio->uio_resid));
528 while (uio->uio_resid >= sizeof(user_ccb) && error == 0) {
532 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
534 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
535 ("write - uiomove failed (%d)\n", error));
538 priority = fuword(&user_ccb->ccb_h.pinfo.priority);
539 if (priority == -1) {
543 func_code = fuword(&user_ccb->ccb_h.func_code);
545 case XPT_ACCEPT_TARGET_IO:
546 case XPT_IMMED_NOTIFY:
547 ccb = targgetccb(softc, func_code, priority);
548 descr = (struct targ_cmd_descr *)ccb->ccb_h.targ_descr;
549 descr->user_ccb = user_ccb;
550 descr->func_code = func_code;
551 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
552 ("Sent ATIO/INOT (%p)\n", user_ccb));
555 TAILQ_INSERT_TAIL(&softc->pending_ccb_queue,
561 if ((func_code & XPT_FC_QUEUED) != 0) {
562 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
563 ("Sending queued ccb %#x (%p)\n",
564 func_code, user_ccb));
565 descr = targgetdescr(softc);
566 descr->user_ccb = user_ccb;
567 descr->priority = priority;
568 descr->func_code = func_code;
570 TAILQ_INSERT_TAIL(&softc->work_queue,
573 xpt_schedule(softc->periph, priority);
575 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
576 ("Sending inline ccb %#x (%p)\n",
577 func_code, user_ccb));
578 ccb = targgetccb(softc, func_code, priority);
579 descr = (struct targ_cmd_descr *)
580 ccb->ccb_h.targ_descr;
581 descr->user_ccb = user_ccb;
582 descr->priority = priority;
583 descr->func_code = func_code;
584 if (targusermerge(softc, descr, ccb) != EFAULT)
585 targsendccb(softc, ccb, descr);
586 targreturnccb(softc, ccb);
590 write_len += sizeof(user_ccb);
594 * If we've successfully taken in some amount of
595 * data, return success for that data first. If
596 * an error is persistent, it will be reported
599 if (error != 0 && write_len == 0)
601 if (write_len == 0 && uio->uio_resid != 0)
606 /* Process requests (descrs) via the periph-supplied CCBs */
608 targstart(struct cam_periph *periph, union ccb *start_ccb)
610 struct targ_softc *softc;
611 struct targ_cmd_descr *descr, *next_descr;
614 softc = (struct targ_softc *)periph->softc;
615 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targstart %p\n", start_ccb));
618 descr = TAILQ_FIRST(&softc->work_queue);
621 xpt_release_ccb(start_ccb);
623 TAILQ_REMOVE(&softc->work_queue, descr, tqe);
624 next_descr = TAILQ_FIRST(&softc->work_queue);
627 /* Initiate a transaction using the descr and supplied CCB */
628 error = targusermerge(softc, descr, start_ccb);
630 error = targsendccb(softc, start_ccb, descr);
632 xpt_print_path(periph->path);
633 printf("targsendccb failed, err %d\n", error);
634 xpt_release_ccb(start_ccb);
635 suword(&descr->user_ccb->ccb_h.status,
638 TAILQ_INSERT_TAIL(&softc->abort_queue, descr, tqe);
643 /* If we have more work to do, stay scheduled */
644 if (next_descr != NULL)
645 xpt_schedule(periph, next_descr->priority);
650 targusermerge(struct targ_softc *softc, struct targ_cmd_descr *descr,
653 struct ccb_hdr *u_ccbh, *k_ccbh;
657 u_ccbh = &descr->user_ccb->ccb_h;
658 k_ccbh = &ccb->ccb_h;
661 * There are some fields in the CCB header that need to be
662 * preserved, the rest we get from the user ccb. (See xpt_merge_ccb)
664 xpt_setup_ccb(k_ccbh, softc->path, descr->priority);
665 k_ccbh->retry_count = fuword(&u_ccbh->retry_count);
666 k_ccbh->func_code = descr->func_code;
667 k_ccbh->flags = fuword(&u_ccbh->flags);
668 k_ccbh->timeout = fuword(&u_ccbh->timeout);
669 ccb_len = targccblen(k_ccbh->func_code) - sizeof(struct ccb_hdr);
670 error = copyin(u_ccbh + 1, k_ccbh + 1, ccb_len);
672 k_ccbh->status = CAM_REQ_CMP_ERR;
676 /* Translate usermode abort_ccb pointer to its kernel counterpart */
677 if (k_ccbh->func_code == XPT_ABORT) {
678 struct ccb_abort *cab;
679 struct ccb_hdr *ccb_h;
682 cab = (struct ccb_abort *)ccb;
684 TAILQ_FOREACH(ccb_h, &softc->pending_ccb_queue,
686 struct targ_cmd_descr *ab_descr;
688 ab_descr = (struct targ_cmd_descr *)ccb_h->targ_descr;
689 if (ab_descr->user_ccb == cab->abort_ccb) {
690 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
691 ("Changing abort for %p to %p\n",
692 cab->abort_ccb, ccb_h));
693 cab->abort_ccb = (union ccb *)ccb_h;
698 /* CCB not found, set appropriate status */
700 k_ccbh->status = CAM_PATH_INVALID;
708 /* Build and send a kernel CCB formed from descr->user_ccb */
710 targsendccb(struct targ_softc *softc, union ccb *ccb,
711 struct targ_cmd_descr *descr)
713 struct cam_periph_map_info *mapinfo;
714 struct ccb_hdr *ccb_h;
718 mapinfo = &descr->mapinfo;
719 mapinfo->num_bufs_used = 0;
722 * There's no way for the user to have a completion
723 * function, so we put our own completion function in here.
724 * We also stash in a reference to our descriptor so targreturnccb()
725 * can find our mapping info.
727 ccb_h->cbfcnp = targdone;
728 ccb_h->targ_descr = descr;
731 * We only attempt to map the user memory into kernel space
732 * if they haven't passed in a physical memory pointer,
733 * and if there is actually an I/O operation to perform.
734 * Right now cam_periph_mapmem() only supports SCSI and device
735 * match CCBs. For the SCSI CCBs, we only pass the CCB in if
736 * there's actually data to map. cam_periph_mapmem() will do the
737 * right thing, even if there isn't data to map, but since CCBs
738 * without data are a reasonably common occurance (e.g. test unit
739 * ready), it will save a few cycles if we check for it here.
741 if (((ccb_h->flags & CAM_DATA_PHYS) == 0)
742 && (((ccb_h->func_code == XPT_CONT_TARGET_IO)
743 && ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE))
744 || (ccb_h->func_code == XPT_DEV_MATCH))) {
746 error = cam_periph_mapmem(ccb, mapinfo);
749 * cam_periph_mapmem returned an error, we can't continue.
750 * Return the error to the user.
753 ccb_h->status = CAM_REQ_CMP_ERR;
754 mapinfo->num_bufs_used = 0;
760 * Once queued on the pending CCB list, this CCB will be protected
761 * by our error recovery handler.
763 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("sendccb %p\n", ccb));
764 if (XPT_FC_IS_QUEUED(ccb)) {
768 TAILQ_INSERT_TAIL(&softc->pending_ccb_queue, ccb_h,
777 /* Completion routine for CCBs (called at splsoftcam) */
779 targdone(struct cam_periph *periph, union ccb *done_ccb)
781 struct targ_softc *softc;
784 CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("targdone %p\n", done_ccb));
785 softc = (struct targ_softc *)periph->softc;
786 TAILQ_REMOVE(&softc->pending_ccb_queue, &done_ccb->ccb_h,
788 status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
790 /* If we're no longer enabled, throw away CCB */
791 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) {
792 targfreeccb(softc, done_ccb);
795 /* abort_all_pending() waits for pending queue to be empty */
796 if (TAILQ_EMPTY(&softc->pending_ccb_queue))
797 wakeup(&softc->pending_ccb_queue);
799 switch (done_ccb->ccb_h.func_code) {
800 /* All FC_*_QUEUED CCBs go back to userland */
801 case XPT_IMMED_NOTIFY:
802 case XPT_ACCEPT_TARGET_IO:
803 case XPT_CONT_TARGET_IO:
804 TAILQ_INSERT_TAIL(&softc->user_ccb_queue, &done_ccb->ccb_h,
809 panic("targdone: impossible xpt opcode %#x",
810 done_ccb->ccb_h.func_code);
815 /* Return CCBs to the user from the user queue and abort queue */
817 targread(dev_t dev, struct uio *uio, int ioflag)
819 struct descr_queue *abort_queue;
820 struct targ_cmd_descr *user_descr;
821 struct targ_softc *softc;
822 struct ccb_queue *user_queue;
823 struct ccb_hdr *ccb_h;
825 int read_len, error, s;
829 softc = (struct targ_softc *)dev->si_drv1;
830 user_queue = &softc->user_ccb_queue;
831 abort_queue = &softc->abort_queue;
832 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targread\n"));
834 /* If no data is available, wait or return immediately */
836 ccb_h = TAILQ_FIRST(user_queue);
837 user_descr = TAILQ_FIRST(abort_queue);
838 while (ccb_h == NULL && user_descr == NULL) {
839 if ((ioflag & IO_NDELAY) == 0) {
840 error = tsleep(user_queue, PCATCH, "targrd", 0);
841 ccb_h = TAILQ_FIRST(user_queue);
842 user_descr = TAILQ_FIRST(abort_queue);
844 if (error == ERESTART) {
857 /* Data is available so fill the user's buffer */
858 while (ccb_h != NULL) {
859 struct targ_cmd_descr *descr;
861 if (uio->uio_resid < sizeof(user_ccb))
863 TAILQ_REMOVE(user_queue, ccb_h, periph_links.tqe);
865 descr = (struct targ_cmd_descr *)ccb_h->targ_descr;
866 user_ccb = descr->user_ccb;
867 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
868 ("targread ccb %p (%p)\n", ccb_h, user_ccb));
869 error = targreturnccb(softc, (union ccb *)ccb_h);
872 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
875 read_len += sizeof(user_ccb);
878 ccb_h = TAILQ_FIRST(user_queue);
881 /* Flush out any aborted descriptors */
882 while (user_descr != NULL) {
883 if (uio->uio_resid < sizeof(user_ccb))
885 TAILQ_REMOVE(abort_queue, user_descr, tqe);
887 user_ccb = user_descr->user_ccb;
888 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
889 ("targread aborted descr %p (%p)\n",
890 user_descr, user_ccb));
891 suword(&user_ccb->ccb_h.status, CAM_REQ_ABORTED);
892 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
895 read_len += sizeof(user_ccb);
898 user_descr = TAILQ_FIRST(abort_queue);
903 * If we've successfully read some amount of data, don't report an
904 * error. If the error is persistent, it will be reported on the
907 if (read_len == 0 && uio->uio_resid != 0)
914 /* Copy completed ccb back to the user */
916 targreturnccb(struct targ_softc *softc, union ccb *ccb)
918 struct targ_cmd_descr *descr;
919 struct ccb_hdr *u_ccbh;
923 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targreturnccb %p\n", ccb));
924 descr = (struct targ_cmd_descr *)ccb->ccb_h.targ_descr;
925 u_ccbh = &descr->user_ccb->ccb_h;
927 /* Copy out the central portion of the ccb_hdr */
928 copyout(&ccb->ccb_h.retry_count, &u_ccbh->retry_count,
929 offsetof(struct ccb_hdr, periph_priv) -
930 offsetof(struct ccb_hdr, retry_count));
932 /* Copy out the rest of the ccb (after the ccb_hdr) */
933 ccb_len = targccblen(ccb->ccb_h.func_code) - sizeof(struct ccb_hdr);
934 if (descr->mapinfo.num_bufs_used != 0)
935 cam_periph_unmapmem(ccb, &descr->mapinfo);
936 error = copyout(&ccb->ccb_h + 1, u_ccbh + 1, ccb_len);
938 xpt_print_path(softc->path);
939 printf("targreturnccb - CCB copyout failed (%d)\n",
942 /* Free CCB or send back to devq. */
943 targfreeccb(softc, ccb);
949 targgetccb(struct targ_softc *softc, xpt_opcode type, int priority)
954 ccb_len = targccblen(type);
955 MALLOC(ccb, union ccb *, ccb_len, M_TARG, M_WAITOK);
956 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("getccb %p\n", ccb));
958 xpt_setup_ccb(&ccb->ccb_h, softc->path, priority);
959 ccb->ccb_h.func_code = type;
960 ccb->ccb_h.cbfcnp = targdone;
961 ccb->ccb_h.targ_descr = targgetdescr(softc);
966 targfreeccb(struct targ_softc *softc, union ccb *ccb)
968 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, ("targfreeccb descr %p and\n",
969 ccb->ccb_h.targ_descr));
970 FREE(ccb->ccb_h.targ_descr, M_TARG);
972 switch (ccb->ccb_h.func_code) {
973 case XPT_ACCEPT_TARGET_IO:
974 case XPT_IMMED_NOTIFY:
975 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, ("freeing ccb %p\n", ccb));
979 /* Send back CCB if we got it from the periph */
980 if (XPT_FC_IS_QUEUED(ccb)) {
981 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH,
982 ("returning queued ccb %p\n", ccb));
983 xpt_release_ccb(ccb);
985 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH,
986 ("freeing ccb %p\n", ccb));
993 static struct targ_cmd_descr *
994 targgetdescr(struct targ_softc *softc)
996 struct targ_cmd_descr *descr;
998 MALLOC(descr, struct targ_cmd_descr *, sizeof(*descr), M_TARG,
1000 descr->mapinfo.num_bufs_used = 0;
1007 cdevsw_add(&targ_cdevsw);
1011 targasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
1013 /* All events are handled in usermode by INOTs */
1014 panic("targasync() called, should be an INOT instead");
1017 /* Cancel all pending requests and CCBs awaiting work. */
1019 abort_all_pending(struct targ_softc *softc)
1021 struct targ_cmd_descr *descr;
1022 struct ccb_abort cab;
1023 struct ccb_hdr *ccb_h;
1025 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("abort_all_pending\n"));
1027 /* First abort the descriptors awaiting resources */
1028 while ((descr = TAILQ_FIRST(&softc->work_queue)) != NULL) {
1029 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
1030 ("Aborting descr from workq %p\n", descr));
1031 TAILQ_REMOVE(&softc->work_queue, descr, tqe);
1032 TAILQ_INSERT_TAIL(&softc->abort_queue, descr, tqe);
1036 * Then abort all pending CCBs.
1037 * targdone() will return the aborted CCB via user_ccb_queue
1039 xpt_setup_ccb(&cab.ccb_h, softc->path, /*priority*/0);
1040 cab.ccb_h.func_code = XPT_ABORT;
1041 cab.ccb_h.status = CAM_REQ_CMP_ERR;
1042 TAILQ_FOREACH(ccb_h, &softc->pending_ccb_queue, periph_links.tqe) {
1043 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
1044 ("Aborting pending CCB %p\n", ccb_h));
1045 cab.abort_ccb = (union ccb *)ccb_h;
1046 xpt_action((union ccb *)&cab);
1047 if (cab.ccb_h.status != CAM_REQ_CMP) {
1048 xpt_print_path(cab.ccb_h.path);
1049 printf("Unable to abort CCB, status %#x\n",
1054 /* If we aborted at least one pending CCB ok, wait for it. */
1055 if (cab.ccb_h.status == CAM_REQ_CMP) {
1056 tsleep(&softc->pending_ccb_queue, PCATCH, "tgabrt", 0);
1059 /* If we aborted anything from the work queue, wakeup user. */
1060 if (!TAILQ_EMPTY(&softc->user_ccb_queue)
1061 || !TAILQ_EMPTY(&softc->abort_queue))
1065 /* Notify the user that data is ready */
1067 notify_user(struct targ_softc *softc)
1070 * Notify users sleeping via poll(), kqueue(), and
1073 selwakeup(&softc->read_select);
1074 KNOTE(&softc->read_select.si_note, 0);
1075 wakeup(&softc->user_ccb_queue);
1078 /* Convert CAM status to errno values */
1080 targcamstatus(cam_status status)
1082 switch (status & CAM_STATUS_MASK) {
1083 case CAM_REQ_CMP: /* CCB request completed without error */
1085 case CAM_REQ_INPROG: /* CCB request is in progress */
1086 return (EINPROGRESS);
1087 case CAM_REQ_CMP_ERR: /* CCB request completed with an error */
1089 case CAM_PROVIDE_FAIL: /* Unable to provide requested capability */
1091 case CAM_FUNC_NOTAVAIL: /* The requested function is not available */
1093 case CAM_LUN_ALRDY_ENA: /* LUN is already enabled for target mode */
1094 return (EADDRINUSE);
1095 case CAM_PATH_INVALID: /* Supplied Path ID is invalid */
1096 case CAM_DEV_NOT_THERE: /* SCSI Device Not Installed/there */
1098 case CAM_REQ_ABORTED: /* CCB request aborted by the host */
1100 case CAM_CMD_TIMEOUT: /* Command timeout */
1102 case CAM_REQUEUE_REQ: /* Requeue to preserve transaction ordering */
1104 case CAM_REQ_INVALID: /* CCB request was invalid */
1106 case CAM_RESRC_UNAVAIL: /* Resource Unavailable */
1108 case CAM_BUSY: /* CAM subsytem is busy */
1109 case CAM_UA_ABORT: /* Unable to abort CCB request */
1117 targccblen(xpt_opcode func_code)
1121 /* Codes we expect to see as a target */
1122 switch (func_code) {
1123 case XPT_CONT_TARGET_IO:
1125 len = sizeof(struct ccb_scsiio);
1127 case XPT_ACCEPT_TARGET_IO:
1128 len = sizeof(struct ccb_accept_tio);
1130 case XPT_IMMED_NOTIFY:
1131 len = sizeof(struct ccb_immed_notify);
1134 len = sizeof(struct ccb_relsim);
1137 len = sizeof(struct ccb_pathinq);
1140 len = sizeof(struct ccb_debug);
1143 len = sizeof(struct ccb_abort);
1146 len = sizeof(struct ccb_en_lun);
1149 len = sizeof(union ccb);