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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
40 #include <sys/vnode.h>
42 #include <sys/mutex.h>
43 #include <sys/devicestat.h>
45 /* Includes to support callout */
46 #include <sys/types.h>
47 #include <sys/systm.h>
50 #include <cam/cam_ccb.h>
51 #include <cam/cam_periph.h>
52 #include <cam/cam_xpt_periph.h>
53 #include <cam/cam_sim.h>
54 #include <cam/scsi/scsi_targetio.h>
57 /* Transaction information attached to each CCB sent by the user */
58 struct targ_cmd_descr {
59 struct cam_periph_map_info mapinfo;
60 TAILQ_ENTRY(targ_cmd_descr) tqe;
66 /* Offset into the private CCB area for storing our descriptor */
67 #define targ_descr periph_priv.entries[1].ptr
69 TAILQ_HEAD(descr_queue, targ_cmd_descr);
72 TARG_STATE_RESV = 0x00, /* Invalid state */
73 TARG_STATE_OPENED = 0x01, /* Device opened, softc initialized */
74 TARG_STATE_LUN_ENABLED = 0x02 /* Device enabled for a path */
77 /* Per-instance device software context */
79 /* CCBs (CTIOs, ATIOs, INOTs) pending on the controller */
80 struct ccb_queue pending_ccb_queue;
82 /* Command descriptors awaiting CTIO resources from the XPT */
83 struct descr_queue work_queue;
85 /* Command descriptors that have been aborted back to the user. */
86 struct descr_queue abort_queue;
89 * Queue of CCBs that have been copied out to userland, but our
90 * userland daemon has not yet seen.
92 struct ccb_queue user_ccb_queue;
94 struct cam_periph *periph;
95 struct cam_path *path;
97 struct selinfo read_select;
98 struct devstat device_stats;
99 struct callout destroy_dev_callout;
100 struct mtx destroy_mtx;
103 static d_open_t targopen;
104 static d_close_t targclose;
105 static d_read_t targread;
106 static d_write_t targwrite;
107 static d_ioctl_t targioctl;
108 static d_poll_t targpoll;
109 static d_kqfilter_t targkqfilter;
110 static void targreadfiltdetach(struct knote *kn);
111 static int targreadfilt(struct knote *kn, long hint);
112 static struct filterops targread_filtops =
113 { 1, NULL, targreadfiltdetach, targreadfilt };
115 static struct cdevsw targ_cdevsw = {
116 .d_version = D_VERSION,
117 .d_flags = D_NEEDGIANT,
119 .d_close = targclose,
121 .d_write = targwrite,
122 .d_ioctl = targioctl,
125 .d_kqfilter = targkqfilter
128 static cam_status targendislun(struct cam_path *path, int enable,
129 int grp6_len, int grp7_len);
130 static cam_status targenable(struct targ_softc *softc,
131 struct cam_path *path,
132 int grp6_len, int grp7_len);
133 static cam_status targdisable(struct targ_softc *softc);
134 static periph_ctor_t targctor;
135 static periph_dtor_t targdtor;
136 static periph_start_t targstart;
137 static int targusermerge(struct targ_softc *softc,
138 struct targ_cmd_descr *descr,
140 static int targsendccb(struct targ_softc *softc, union ccb *ccb,
141 struct targ_cmd_descr *descr);
142 static void targdone(struct cam_periph *periph,
143 union ccb *done_ccb);
144 static int targreturnccb(struct targ_softc *softc,
146 static union ccb * targgetccb(struct targ_softc *softc, xpt_opcode type,
148 static void targfreeccb(struct targ_softc *softc, union ccb *ccb);
149 static struct targ_cmd_descr *
150 targgetdescr(struct targ_softc *softc);
151 static periph_init_t targinit;
152 static void targclone(void *arg, struct ucred *cred, char *name,
153 int namelen, struct cdev **dev);
154 static void targasync(void *callback_arg, u_int32_t code,
155 struct cam_path *path, void *arg);
156 static void abort_all_pending(struct targ_softc *softc);
157 static void notify_user(struct targ_softc *softc);
158 static int targcamstatus(cam_status status);
159 static size_t targccblen(xpt_opcode func_code);
160 static void targdestroy(void *);
162 static struct periph_driver targdriver =
165 TAILQ_HEAD_INITIALIZER(targdriver.units), /* generation */ 0
167 PERIPHDRIVER_DECLARE(targ, targdriver);
169 static MALLOC_DEFINE(M_TARG, "TARG", "TARG data");
172 * Create softc and initialize it. Only one proc can open each targ device.
173 * There is no locking here because a periph doesn't get created until an
174 * ioctl is issued to do so, and that can't happen until this method returns.
177 targopen(struct cdev *dev, int flags, int fmt, struct thread *td)
179 struct targ_softc *softc;
181 if (dev->si_drv1 != 0) {
185 /* Mark device busy before any potentially blocking operations */
186 dev->si_drv1 = (void *)~0;
188 /* Create the targ device, allocate its softc, initialize it */
189 if ((dev->si_flags & SI_NAMED) == 0) {
190 make_dev(&targ_cdevsw, dev2unit(dev), UID_ROOT, GID_WHEEL, 0600,
191 "targ%d", dev2unit(dev));
193 softc = malloc(sizeof(*softc), M_TARG,
195 dev->si_drv1 = softc;
196 softc->state = TARG_STATE_OPENED;
197 softc->periph = NULL;
200 TAILQ_INIT(&softc->pending_ccb_queue);
201 TAILQ_INIT(&softc->work_queue);
202 TAILQ_INIT(&softc->abort_queue);
203 TAILQ_INIT(&softc->user_ccb_queue);
204 knlist_init_mtx(&softc->read_select.si_note, NULL);
209 /* Disable LUN if enabled and teardown softc */
211 targclose(struct cdev *dev, int flag, int fmt, struct thread *td)
213 struct targ_softc *softc;
214 struct cam_periph *periph;
217 softc = (struct targ_softc *)dev->si_drv1;
218 mtx_init(&softc->destroy_mtx, "targ_destroy", "SCSI Target dev destroy", MTX_DEF);
219 callout_init_mtx(&softc->destroy_dev_callout, &softc->destroy_mtx, CALLOUT_RETURNUNLOCKED);
220 if (softc->periph == NULL) {
225 printf("%s: destroying non-enabled target\n", __func__);
226 mtx_lock(&softc->destroy_mtx);
227 callout_reset(&softc->destroy_dev_callout, hz / 2,
228 (void *)targdestroy, (void *)dev);
229 mtx_unlock(&softc->destroy_mtx);
234 * Acquire a hold on the periph so that it doesn't go away before
235 * we are ready at the end of the function.
237 periph = softc->periph;
238 cam_periph_acquire(periph);
239 cam_periph_lock(periph);
240 error = targdisable(softc);
241 if (softc->periph != NULL) {
242 cam_periph_invalidate(softc->periph);
243 softc->periph = NULL;
245 cam_periph_unlock(periph);
246 cam_periph_release(periph);
253 printf("%s: close finished error(%d)\n", __func__, error);
254 mtx_lock(&softc->destroy_mtx);
255 callout_reset(&softc->destroy_dev_callout, hz / 2,
256 (void *)targdestroy, (void *)dev);
257 mtx_unlock(&softc->destroy_mtx);
261 /* Enable/disable LUNs, set debugging level */
263 targioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
265 struct targ_softc *softc;
268 softc = (struct targ_softc *)dev->si_drv1;
273 struct ioc_enable_lun *new_lun;
274 struct cam_path *path;
277 new_lun = (struct ioc_enable_lun *)addr;
278 status = xpt_create_path_unlocked(&path, /*periph*/NULL,
282 if (status != CAM_REQ_CMP) {
283 printf("Couldn't create path, status %#x\n", status);
286 sim = xpt_path_sim(path);
288 status = targenable(softc, path, new_lun->grp6_len,
291 mtx_unlock(sim->mtx);
295 if (softc->periph == NULL) {
296 status = CAM_DEV_NOT_THERE;
299 cam_periph_lock(softc->periph);
300 status = targdisable(softc);
301 cam_periph_unlock(softc->periph);
305 struct ccb_debug cdbg;
307 /* If no periph available, disallow debugging changes */
308 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) {
309 status = CAM_DEV_NOT_THERE;
312 bzero(&cdbg, sizeof cdbg);
313 if (*((int *)addr) != 0)
314 cdbg.flags = CAM_DEBUG_PERIPH;
316 cdbg.flags = CAM_DEBUG_NONE;
317 cam_periph_lock(softc->periph);
318 xpt_setup_ccb(&cdbg.ccb_h, softc->path, CAM_PRIORITY_NORMAL);
319 cdbg.ccb_h.func_code = XPT_DEBUG;
320 cdbg.ccb_h.cbfcnp = targdone;
322 xpt_action((union ccb *)&cdbg);
323 cam_periph_unlock(softc->periph);
324 status = cdbg.ccb_h.status & CAM_STATUS_MASK;
328 status = CAM_PROVIDE_FAIL;
332 return (targcamstatus(status));
335 /* Writes are always ready, reads wait for user_ccb_queue or abort_queue */
337 targpoll(struct cdev *dev, int poll_events, struct thread *td)
339 struct targ_softc *softc;
342 softc = (struct targ_softc *)dev->si_drv1;
344 /* Poll for write() is always ok. */
345 revents = poll_events & (POLLOUT | POLLWRNORM);
346 if ((poll_events & (POLLIN | POLLRDNORM)) != 0) {
347 /* Poll for read() depends on user and abort queues. */
348 cam_periph_lock(softc->periph);
349 if (!TAILQ_EMPTY(&softc->user_ccb_queue) ||
350 !TAILQ_EMPTY(&softc->abort_queue)) {
351 revents |= poll_events & (POLLIN | POLLRDNORM);
353 cam_periph_unlock(softc->periph);
354 /* Only sleep if the user didn't poll for write. */
356 selrecord(td, &softc->read_select);
363 targkqfilter(struct cdev *dev, struct knote *kn)
365 struct targ_softc *softc;
367 softc = (struct targ_softc *)dev->si_drv1;
368 kn->kn_hook = (caddr_t)softc;
369 kn->kn_fop = &targread_filtops;
370 knlist_add(&softc->read_select.si_note, kn, 0);
375 targreadfiltdetach(struct knote *kn)
377 struct targ_softc *softc;
379 softc = (struct targ_softc *)kn->kn_hook;
380 knlist_remove(&softc->read_select.si_note, kn, 0);
383 /* Notify the user's kqueue when the user queue or abort queue gets a CCB */
385 targreadfilt(struct knote *kn, long hint)
387 struct targ_softc *softc;
390 softc = (struct targ_softc *)kn->kn_hook;
391 cam_periph_lock(softc->periph);
392 retval = !TAILQ_EMPTY(&softc->user_ccb_queue) ||
393 !TAILQ_EMPTY(&softc->abort_queue);
394 cam_periph_unlock(softc->periph);
398 /* Send the HBA the enable/disable message */
400 targendislun(struct cam_path *path, int enable, int grp6_len, int grp7_len)
402 struct ccb_en_lun en_ccb;
405 /* Tell the lun to begin answering selects */
406 xpt_setup_ccb(&en_ccb.ccb_h, path, CAM_PRIORITY_NORMAL);
407 en_ccb.ccb_h.func_code = XPT_EN_LUN;
408 /* Don't need support for any vendor specific commands */
409 en_ccb.grp6_len = grp6_len;
410 en_ccb.grp7_len = grp7_len;
411 en_ccb.enable = enable ? 1 : 0;
412 xpt_action((union ccb *)&en_ccb);
413 status = en_ccb.ccb_h.status & CAM_STATUS_MASK;
414 if (status != CAM_REQ_CMP) {
415 xpt_print(path, "%sable lun CCB rejected, status %#x\n",
416 enable ? "en" : "dis", status);
421 /* Enable target mode on a LUN, given its path */
423 targenable(struct targ_softc *softc, struct cam_path *path, int grp6_len,
426 struct cam_periph *periph;
427 struct ccb_pathinq cpi;
430 if ((softc->state & TARG_STATE_LUN_ENABLED) != 0)
431 return (CAM_LUN_ALRDY_ENA);
433 /* Make sure SIM supports target mode */
434 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
435 cpi.ccb_h.func_code = XPT_PATH_INQ;
436 xpt_action((union ccb *)&cpi);
437 status = cpi.ccb_h.status & CAM_STATUS_MASK;
438 if (status != CAM_REQ_CMP) {
439 printf("pathinq failed, status %#x\n", status);
442 if ((cpi.target_sprt & PIT_PROCESSOR) == 0) {
443 printf("controller does not support target mode\n");
444 status = CAM_FUNC_NOTAVAIL;
448 /* Destroy any periph on our path if it is disabled */
449 periph = cam_periph_find(path, "targ");
450 if (periph != NULL) {
451 struct targ_softc *del_softc;
453 del_softc = (struct targ_softc *)periph->softc;
454 if ((del_softc->state & TARG_STATE_LUN_ENABLED) == 0) {
455 cam_periph_invalidate(del_softc->periph);
456 del_softc->periph = NULL;
458 printf("Requested path still in use by targ%d\n",
459 periph->unit_number);
460 status = CAM_LUN_ALRDY_ENA;
465 /* Create a periph instance attached to this path */
466 status = cam_periph_alloc(targctor, NULL, targdtor, targstart,
467 "targ", CAM_PERIPH_BIO, path, targasync, 0, softc);
468 if (status != CAM_REQ_CMP) {
469 printf("cam_periph_alloc failed, status %#x\n", status);
473 /* Ensure that the periph now exists. */
474 if (cam_periph_find(path, "targ") == NULL) {
475 panic("targenable: succeeded but no periph?");
479 /* Send the enable lun message */
480 status = targendislun(path, /*enable*/1, grp6_len, grp7_len);
481 if (status != CAM_REQ_CMP) {
482 printf("enable lun failed, status %#x\n", status);
485 softc->state |= TARG_STATE_LUN_ENABLED;
491 /* Disable this softc's target instance if enabled */
493 targdisable(struct targ_softc *softc)
497 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0)
498 return (CAM_REQ_CMP);
500 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targdisable\n"));
502 /* Abort any ccbs pending on the controller */
503 abort_all_pending(softc);
505 /* Disable this lun */
506 status = targendislun(softc->path, /*enable*/0,
507 /*grp6_len*/0, /*grp7_len*/0);
508 if (status == CAM_REQ_CMP)
509 softc->state &= ~TARG_STATE_LUN_ENABLED;
511 printf("Disable lun failed, status %#x\n", status);
516 /* Initialize a periph (called from cam_periph_alloc) */
518 targctor(struct cam_periph *periph, void *arg)
520 struct targ_softc *softc;
522 /* Store pointer to softc for periph-driven routines */
523 softc = (struct targ_softc *)arg;
524 periph->softc = softc;
525 softc->periph = periph;
526 softc->path = periph->path;
527 return (CAM_REQ_CMP);
531 targdtor(struct cam_periph *periph)
533 struct targ_softc *softc;
534 struct ccb_hdr *ccb_h;
535 struct targ_cmd_descr *descr;
537 softc = (struct targ_softc *)periph->softc;
540 * targdisable() aborts CCBs back to the user and leaves them
541 * on user_ccb_queue and abort_queue in case the user is still
542 * interested in them. We free them now.
544 while ((ccb_h = TAILQ_FIRST(&softc->user_ccb_queue)) != NULL) {
545 TAILQ_REMOVE(&softc->user_ccb_queue, ccb_h, periph_links.tqe);
546 targfreeccb(softc, (union ccb *)ccb_h);
548 while ((descr = TAILQ_FIRST(&softc->abort_queue)) != NULL) {
549 TAILQ_REMOVE(&softc->abort_queue, descr, tqe);
553 softc->periph = NULL;
555 periph->softc = NULL;
558 /* Receive CCBs from user mode proc and send them to the HBA */
560 targwrite(struct cdev *dev, struct uio *uio, int ioflag)
563 struct targ_softc *softc;
564 struct targ_cmd_descr *descr;
565 int write_len, error;
566 int func_code, priority;
568 softc = (struct targ_softc *)dev->si_drv1;
569 write_len = error = 0;
570 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
571 ("write - uio_resid %zd\n", uio->uio_resid));
572 while (uio->uio_resid >= sizeof(user_ccb) && error == 0) {
575 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
577 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
578 ("write - uiomove failed (%d)\n", error));
581 priority = fuword32(&user_ccb->ccb_h.pinfo.priority);
582 if (priority == CAM_PRIORITY_NONE) {
586 func_code = fuword32(&user_ccb->ccb_h.func_code);
588 case XPT_ACCEPT_TARGET_IO:
589 case XPT_IMMED_NOTIFY:
590 cam_periph_lock(softc->periph);
591 ccb = targgetccb(softc, func_code, priority);
592 descr = (struct targ_cmd_descr *)ccb->ccb_h.targ_descr;
593 descr->user_ccb = user_ccb;
594 descr->func_code = func_code;
595 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
596 ("Sent ATIO/INOT (%p)\n", user_ccb));
598 TAILQ_INSERT_TAIL(&softc->pending_ccb_queue,
601 cam_periph_unlock(softc->periph);
604 cam_periph_lock(softc->periph);
605 if ((func_code & XPT_FC_QUEUED) != 0) {
606 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
607 ("Sending queued ccb %#x (%p)\n",
608 func_code, user_ccb));
609 descr = targgetdescr(softc);
610 descr->user_ccb = user_ccb;
611 descr->priority = priority;
612 descr->func_code = func_code;
613 TAILQ_INSERT_TAIL(&softc->work_queue,
615 xpt_schedule(softc->periph, priority);
617 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
618 ("Sending inline ccb %#x (%p)\n",
619 func_code, user_ccb));
620 ccb = targgetccb(softc, func_code, priority);
621 descr = (struct targ_cmd_descr *)
622 ccb->ccb_h.targ_descr;
623 descr->user_ccb = user_ccb;
624 descr->priority = priority;
625 descr->func_code = func_code;
626 if (targusermerge(softc, descr, ccb) != EFAULT)
627 targsendccb(softc, ccb, descr);
628 targreturnccb(softc, ccb);
630 cam_periph_unlock(softc->periph);
633 write_len += sizeof(user_ccb);
637 * If we've successfully taken in some amount of
638 * data, return success for that data first. If
639 * an error is persistent, it will be reported
642 if (error != 0 && write_len == 0)
644 if (write_len == 0 && uio->uio_resid != 0)
649 /* Process requests (descrs) via the periph-supplied CCBs */
651 targstart(struct cam_periph *periph, union ccb *start_ccb)
653 struct targ_softc *softc;
654 struct targ_cmd_descr *descr, *next_descr;
657 softc = (struct targ_softc *)periph->softc;
658 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targstart %p\n", start_ccb));
660 descr = TAILQ_FIRST(&softc->work_queue);
662 xpt_release_ccb(start_ccb);
664 TAILQ_REMOVE(&softc->work_queue, descr, tqe);
665 next_descr = TAILQ_FIRST(&softc->work_queue);
667 /* Initiate a transaction using the descr and supplied CCB */
668 error = targusermerge(softc, descr, start_ccb);
670 error = targsendccb(softc, start_ccb, descr);
672 xpt_print(periph->path,
673 "targsendccb failed, err %d\n", error);
674 xpt_release_ccb(start_ccb);
675 suword(&descr->user_ccb->ccb_h.status,
677 TAILQ_INSERT_TAIL(&softc->abort_queue, descr, tqe);
681 /* If we have more work to do, stay scheduled */
682 if (next_descr != NULL)
683 xpt_schedule(periph, next_descr->priority);
688 targusermerge(struct targ_softc *softc, struct targ_cmd_descr *descr,
691 struct ccb_hdr *u_ccbh, *k_ccbh;
695 u_ccbh = &descr->user_ccb->ccb_h;
696 k_ccbh = &ccb->ccb_h;
699 * There are some fields in the CCB header that need to be
700 * preserved, the rest we get from the user ccb. (See xpt_merge_ccb)
702 xpt_setup_ccb(k_ccbh, softc->path, descr->priority);
703 k_ccbh->retry_count = fuword32(&u_ccbh->retry_count);
704 k_ccbh->func_code = descr->func_code;
705 k_ccbh->flags = fuword32(&u_ccbh->flags);
706 k_ccbh->timeout = fuword32(&u_ccbh->timeout);
707 ccb_len = targccblen(k_ccbh->func_code) - sizeof(struct ccb_hdr);
708 error = copyin(u_ccbh + 1, k_ccbh + 1, ccb_len);
710 k_ccbh->status = CAM_REQ_CMP_ERR;
714 /* Translate usermode abort_ccb pointer to its kernel counterpart */
715 if (k_ccbh->func_code == XPT_ABORT) {
716 struct ccb_abort *cab;
717 struct ccb_hdr *ccb_h;
719 cab = (struct ccb_abort *)ccb;
720 TAILQ_FOREACH(ccb_h, &softc->pending_ccb_queue,
722 struct targ_cmd_descr *ab_descr;
724 ab_descr = (struct targ_cmd_descr *)ccb_h->targ_descr;
725 if (ab_descr->user_ccb == cab->abort_ccb) {
726 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
727 ("Changing abort for %p to %p\n",
728 cab->abort_ccb, ccb_h));
729 cab->abort_ccb = (union ccb *)ccb_h;
733 /* CCB not found, set appropriate status */
735 k_ccbh->status = CAM_PATH_INVALID;
743 /* Build and send a kernel CCB formed from descr->user_ccb */
745 targsendccb(struct targ_softc *softc, union ccb *ccb,
746 struct targ_cmd_descr *descr)
748 struct cam_periph_map_info *mapinfo;
749 struct ccb_hdr *ccb_h;
753 mapinfo = &descr->mapinfo;
754 mapinfo->num_bufs_used = 0;
757 * There's no way for the user to have a completion
758 * function, so we put our own completion function in here.
759 * We also stash in a reference to our descriptor so targreturnccb()
760 * can find our mapping info.
762 ccb_h->cbfcnp = targdone;
763 ccb_h->targ_descr = descr;
766 * We only attempt to map the user memory into kernel space
767 * if they haven't passed in a physical memory pointer,
768 * and if there is actually an I/O operation to perform.
769 * Right now cam_periph_mapmem() only supports SCSI and device
770 * match CCBs. For the SCSI CCBs, we only pass the CCB in if
771 * there's actually data to map. cam_periph_mapmem() will do the
772 * right thing, even if there isn't data to map, but since CCBs
773 * without data are a reasonably common occurance (e.g. test unit
774 * ready), it will save a few cycles if we check for it here.
776 if (((ccb_h->flags & CAM_DATA_PHYS) == 0)
777 && (((ccb_h->func_code == XPT_CONT_TARGET_IO)
778 && ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE))
779 || (ccb_h->func_code == XPT_DEV_MATCH))) {
781 error = cam_periph_mapmem(ccb, mapinfo);
784 * cam_periph_mapmem returned an error, we can't continue.
785 * Return the error to the user.
788 ccb_h->status = CAM_REQ_CMP_ERR;
789 mapinfo->num_bufs_used = 0;
795 * Once queued on the pending CCB list, this CCB will be protected
796 * by our error recovery handler.
798 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("sendccb %p\n", ccb));
799 if (XPT_FC_IS_QUEUED(ccb)) {
800 TAILQ_INSERT_TAIL(&softc->pending_ccb_queue, ccb_h,
808 /* Completion routine for CCBs (called at splsoftcam) */
810 targdone(struct cam_periph *periph, union ccb *done_ccb)
812 struct targ_softc *softc;
815 CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("targdone %p\n", done_ccb));
816 softc = (struct targ_softc *)periph->softc;
817 TAILQ_REMOVE(&softc->pending_ccb_queue, &done_ccb->ccb_h,
819 status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
821 /* If we're no longer enabled, throw away CCB */
822 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) {
823 targfreeccb(softc, done_ccb);
826 /* abort_all_pending() waits for pending queue to be empty */
827 if (TAILQ_EMPTY(&softc->pending_ccb_queue))
828 wakeup(&softc->pending_ccb_queue);
830 switch (done_ccb->ccb_h.func_code) {
831 /* All FC_*_QUEUED CCBs go back to userland */
832 case XPT_IMMED_NOTIFY:
833 case XPT_ACCEPT_TARGET_IO:
834 case XPT_CONT_TARGET_IO:
835 TAILQ_INSERT_TAIL(&softc->user_ccb_queue, &done_ccb->ccb_h,
837 cam_periph_unlock(softc->periph);
839 cam_periph_lock(softc->periph);
842 panic("targdone: impossible xpt opcode %#x",
843 done_ccb->ccb_h.func_code);
848 /* Return CCBs to the user from the user queue and abort queue */
850 targread(struct cdev *dev, struct uio *uio, int ioflag)
852 struct descr_queue *abort_queue;
853 struct targ_cmd_descr *user_descr;
854 struct targ_softc *softc;
855 struct ccb_queue *user_queue;
856 struct ccb_hdr *ccb_h;
862 softc = (struct targ_softc *)dev->si_drv1;
863 user_queue = &softc->user_ccb_queue;
864 abort_queue = &softc->abort_queue;
865 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targread\n"));
867 /* If no data is available, wait or return immediately */
868 cam_periph_lock(softc->periph);
869 ccb_h = TAILQ_FIRST(user_queue);
870 user_descr = TAILQ_FIRST(abort_queue);
871 while (ccb_h == NULL && user_descr == NULL) {
872 if ((ioflag & IO_NDELAY) == 0) {
873 error = msleep(user_queue, softc->periph->sim->mtx,
874 PRIBIO | PCATCH, "targrd", 0);
875 ccb_h = TAILQ_FIRST(user_queue);
876 user_descr = TAILQ_FIRST(abort_queue);
878 if (error == ERESTART) {
885 cam_periph_unlock(softc->periph);
890 /* Data is available so fill the user's buffer */
891 while (ccb_h != NULL) {
892 struct targ_cmd_descr *descr;
894 if (uio->uio_resid < sizeof(user_ccb))
896 TAILQ_REMOVE(user_queue, ccb_h, periph_links.tqe);
897 descr = (struct targ_cmd_descr *)ccb_h->targ_descr;
898 user_ccb = descr->user_ccb;
899 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
900 ("targread ccb %p (%p)\n", ccb_h, user_ccb));
901 error = targreturnccb(softc, (union ccb *)ccb_h);
904 cam_periph_unlock(softc->periph);
905 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
906 cam_periph_lock(softc->periph);
909 read_len += sizeof(user_ccb);
911 ccb_h = TAILQ_FIRST(user_queue);
914 /* Flush out any aborted descriptors */
915 while (user_descr != NULL) {
916 if (uio->uio_resid < sizeof(user_ccb))
918 TAILQ_REMOVE(abort_queue, user_descr, tqe);
919 user_ccb = user_descr->user_ccb;
920 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
921 ("targread aborted descr %p (%p)\n",
922 user_descr, user_ccb));
923 suword(&user_ccb->ccb_h.status, CAM_REQ_ABORTED);
924 cam_periph_unlock(softc->periph);
925 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
926 cam_periph_lock(softc->periph);
929 read_len += sizeof(user_ccb);
931 user_descr = TAILQ_FIRST(abort_queue);
935 * If we've successfully read some amount of data, don't report an
936 * error. If the error is persistent, it will be reported on the
939 if (read_len == 0 && uio->uio_resid != 0)
943 cam_periph_unlock(softc->periph);
947 /* Copy completed ccb back to the user */
949 targreturnccb(struct targ_softc *softc, union ccb *ccb)
951 struct targ_cmd_descr *descr;
952 struct ccb_hdr *u_ccbh;
956 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targreturnccb %p\n", ccb));
957 descr = (struct targ_cmd_descr *)ccb->ccb_h.targ_descr;
958 u_ccbh = &descr->user_ccb->ccb_h;
960 /* Copy out the central portion of the ccb_hdr */
961 copyout(&ccb->ccb_h.retry_count, &u_ccbh->retry_count,
962 offsetof(struct ccb_hdr, periph_priv) -
963 offsetof(struct ccb_hdr, retry_count));
965 /* Copy out the rest of the ccb (after the ccb_hdr) */
966 ccb_len = targccblen(ccb->ccb_h.func_code) - sizeof(struct ccb_hdr);
967 if (descr->mapinfo.num_bufs_used != 0)
968 cam_periph_unmapmem(ccb, &descr->mapinfo);
969 error = copyout(&ccb->ccb_h + 1, u_ccbh + 1, ccb_len);
971 xpt_print(softc->path,
972 "targreturnccb - CCB copyout failed (%d)\n", error);
974 /* Free CCB or send back to devq. */
975 targfreeccb(softc, ccb);
981 targgetccb(struct targ_softc *softc, xpt_opcode type, int priority)
986 ccb_len = targccblen(type);
987 ccb = malloc(ccb_len, M_TARG, M_NOWAIT);
988 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("getccb %p\n", ccb));
992 xpt_setup_ccb(&ccb->ccb_h, softc->path, priority);
993 ccb->ccb_h.func_code = type;
994 ccb->ccb_h.cbfcnp = targdone;
995 ccb->ccb_h.targ_descr = targgetdescr(softc);
996 if (ccb->ccb_h.targ_descr == NULL) {
1004 targfreeccb(struct targ_softc *softc, union ccb *ccb)
1006 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, ("targfreeccb descr %p and\n",
1007 ccb->ccb_h.targ_descr));
1008 free(ccb->ccb_h.targ_descr, M_TARG);
1010 switch (ccb->ccb_h.func_code) {
1011 case XPT_ACCEPT_TARGET_IO:
1012 case XPT_IMMED_NOTIFY:
1013 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, ("freeing ccb %p\n", ccb));
1017 /* Send back CCB if we got it from the periph */
1018 if (XPT_FC_IS_QUEUED(ccb)) {
1019 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH,
1020 ("returning queued ccb %p\n", ccb));
1021 xpt_release_ccb(ccb);
1023 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH,
1024 ("freeing ccb %p\n", ccb));
1031 static struct targ_cmd_descr *
1032 targgetdescr(struct targ_softc *softc)
1034 struct targ_cmd_descr *descr;
1036 descr = malloc(sizeof(*descr), M_TARG,
1039 descr->mapinfo.num_bufs_used = 0;
1047 EVENTHANDLER_REGISTER(dev_clone, targclone, 0, 1000);
1051 targclone(void *arg, struct ucred *cred, char *name, int namelen,
1058 if (dev_stdclone(name, NULL, "targ", &u) != 1)
1060 *dev = make_dev(&targ_cdevsw, u, UID_ROOT, GID_WHEEL,
1063 (*dev)->si_flags |= SI_CHEAPCLONE;
1067 targasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
1069 /* All events are handled in usermode by INOTs */
1070 panic("targasync() called, should be an INOT instead");
1073 /* Cancel all pending requests and CCBs awaiting work. */
1075 abort_all_pending(struct targ_softc *softc)
1077 struct targ_cmd_descr *descr;
1078 struct ccb_abort cab;
1079 struct ccb_hdr *ccb_h;
1080 struct cam_sim *sim;
1082 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("abort_all_pending\n"));
1084 /* First abort the descriptors awaiting resources */
1085 while ((descr = TAILQ_FIRST(&softc->work_queue)) != NULL) {
1086 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
1087 ("Aborting descr from workq %p\n", descr));
1088 TAILQ_REMOVE(&softc->work_queue, descr, tqe);
1089 TAILQ_INSERT_TAIL(&softc->abort_queue, descr, tqe);
1093 * Then abort all pending CCBs.
1094 * targdone() will return the aborted CCB via user_ccb_queue
1096 xpt_setup_ccb(&cab.ccb_h, softc->path, CAM_PRIORITY_NORMAL);
1097 cab.ccb_h.func_code = XPT_ABORT;
1098 cab.ccb_h.status = CAM_REQ_CMP_ERR;
1099 TAILQ_FOREACH(ccb_h, &softc->pending_ccb_queue, periph_links.tqe) {
1100 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
1101 ("Aborting pending CCB %p\n", ccb_h));
1102 cab.abort_ccb = (union ccb *)ccb_h;
1103 xpt_action((union ccb *)&cab);
1104 if (cab.ccb_h.status != CAM_REQ_CMP) {
1105 xpt_print(cab.ccb_h.path,
1106 "Unable to abort CCB, status %#x\n",
1111 /* If we aborted at least one pending CCB ok, wait for it. */
1112 if (cab.ccb_h.status == CAM_REQ_CMP) {
1113 sim = xpt_path_sim(softc->path);
1114 msleep(&softc->pending_ccb_queue, sim->mtx,
1115 PRIBIO | PCATCH, "tgabrt", 0);
1118 /* If we aborted anything from the work queue, wakeup user. */
1119 if (!TAILQ_EMPTY(&softc->user_ccb_queue)
1120 || !TAILQ_EMPTY(&softc->abort_queue)) {
1121 cam_periph_unlock(softc->periph);
1123 cam_periph_lock(softc->periph);
1127 /* Notify the user that data is ready */
1129 notify_user(struct targ_softc *softc)
1132 * Notify users sleeping via poll(), kqueue(), and
1135 selwakeuppri(&softc->read_select, PRIBIO);
1136 KNOTE_UNLOCKED(&softc->read_select.si_note, 0);
1137 wakeup(&softc->user_ccb_queue);
1140 /* Convert CAM status to errno values */
1142 targcamstatus(cam_status status)
1144 switch (status & CAM_STATUS_MASK) {
1145 case CAM_REQ_CMP: /* CCB request completed without error */
1147 case CAM_REQ_INPROG: /* CCB request is in progress */
1148 return (EINPROGRESS);
1149 case CAM_REQ_CMP_ERR: /* CCB request completed with an error */
1151 case CAM_PROVIDE_FAIL: /* Unable to provide requested capability */
1153 case CAM_FUNC_NOTAVAIL: /* The requested function is not available */
1155 case CAM_LUN_ALRDY_ENA: /* LUN is already enabled for target mode */
1156 return (EADDRINUSE);
1157 case CAM_PATH_INVALID: /* Supplied Path ID is invalid */
1158 case CAM_DEV_NOT_THERE: /* SCSI Device Not Installed/there */
1160 case CAM_REQ_ABORTED: /* CCB request aborted by the host */
1162 case CAM_CMD_TIMEOUT: /* Command timeout */
1164 case CAM_REQUEUE_REQ: /* Requeue to preserve transaction ordering */
1166 case CAM_REQ_INVALID: /* CCB request was invalid */
1168 case CAM_RESRC_UNAVAIL: /* Resource Unavailable */
1170 case CAM_BUSY: /* CAM subsystem is busy */
1171 case CAM_UA_ABORT: /* Unable to abort CCB request */
1179 targccblen(xpt_opcode func_code)
1183 /* Codes we expect to see as a target */
1184 switch (func_code) {
1185 case XPT_CONT_TARGET_IO:
1187 len = sizeof(struct ccb_scsiio);
1189 case XPT_ACCEPT_TARGET_IO:
1190 len = sizeof(struct ccb_accept_tio);
1192 case XPT_IMMED_NOTIFY:
1193 len = sizeof(struct ccb_immed_notify);
1196 len = sizeof(struct ccb_relsim);
1199 len = sizeof(struct ccb_pathinq);
1202 len = sizeof(struct ccb_debug);
1205 len = sizeof(struct ccb_abort);
1208 len = sizeof(struct ccb_en_lun);
1211 len = sizeof(union ccb);
1219 * work around to destroy targ device
1220 * outside of targclose
1223 targdestroy(void *dev)
1225 struct cdev *device = (struct cdev *)dev;
1226 struct targ_softc *softc = (struct targ_softc *)device->si_drv1;
1229 callout_stop(&softc->destroy_dev_callout);
1232 mtx_unlock(&softc->destroy_mtx);
1233 mtx_destroy(&softc->destroy_mtx);
1234 free(softc, M_TARG);
1235 device->si_drv1 = 0;
1236 destroy_dev(device);
1237 printf("%s: destroyed dev\n", __func__);