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>
47 #include <cam/cam_ccb.h>
48 #include <cam/cam_periph.h>
49 #include <cam/cam_xpt_periph.h>
50 #include <cam/cam_sim.h>
51 #include <cam/scsi/scsi_targetio.h>
53 /* Transaction information attached to each CCB sent by the user */
54 struct targ_cmd_descr {
55 struct cam_periph_map_info mapinfo;
56 TAILQ_ENTRY(targ_cmd_descr) tqe;
62 /* Offset into the private CCB area for storing our descriptor */
63 #define targ_descr periph_priv.entries[1].ptr
65 TAILQ_HEAD(descr_queue, targ_cmd_descr);
68 TARG_STATE_RESV = 0x00, /* Invalid state */
69 TARG_STATE_OPENED = 0x01, /* Device opened, softc initialized */
70 TARG_STATE_LUN_ENABLED = 0x02 /* Device enabled for a path */
73 /* Per-instance device software context */
75 /* CCBs (CTIOs, ATIOs, INOTs) pending on the controller */
76 struct ccb_queue pending_ccb_queue;
78 /* Command descriptors awaiting CTIO resources from the XPT */
79 struct descr_queue work_queue;
81 /* Command descriptors that have been aborted back to the user. */
82 struct descr_queue abort_queue;
85 * Queue of CCBs that have been copied out to userland, but our
86 * userland daemon has not yet seen.
88 struct ccb_queue user_ccb_queue;
90 struct cam_periph *periph;
91 struct cam_path *path;
93 struct selinfo read_select;
94 struct devstat device_stats;
97 static d_open_t targopen;
98 static d_close_t targclose;
99 static d_read_t targread;
100 static d_write_t targwrite;
101 static d_ioctl_t targioctl;
102 static d_poll_t targpoll;
103 static d_kqfilter_t targkqfilter;
104 static void targreadfiltdetach(struct knote *kn);
105 static int targreadfilt(struct knote *kn, long hint);
106 static struct filterops targread_filtops =
107 { 1, NULL, targreadfiltdetach, targreadfilt };
109 static struct cdevsw targ_cdevsw = {
110 .d_version = D_VERSION,
111 .d_flags = D_NEEDGIANT,
113 .d_close = targclose,
115 .d_write = targwrite,
116 .d_ioctl = targioctl,
119 .d_kqfilter = targkqfilter
122 static cam_status targendislun(struct cam_path *path, int enable,
123 int grp6_len, int grp7_len);
124 static cam_status targenable(struct targ_softc *softc,
125 struct cam_path *path,
126 int grp6_len, int grp7_len);
127 static cam_status targdisable(struct targ_softc *softc);
128 static periph_ctor_t targctor;
129 static periph_dtor_t targdtor;
130 static periph_start_t targstart;
131 static int targusermerge(struct targ_softc *softc,
132 struct targ_cmd_descr *descr,
134 static int targsendccb(struct targ_softc *softc, union ccb *ccb,
135 struct targ_cmd_descr *descr);
136 static void targdone(struct cam_periph *periph,
137 union ccb *done_ccb);
138 static int targreturnccb(struct targ_softc *softc,
140 static union ccb * targgetccb(struct targ_softc *softc, xpt_opcode type,
142 static void targfreeccb(struct targ_softc *softc, union ccb *ccb);
143 static struct targ_cmd_descr *
144 targgetdescr(struct targ_softc *softc);
145 static periph_init_t targinit;
146 static void targclone(void *arg, struct ucred *cred, char *name,
147 int namelen, struct cdev **dev);
148 static void targasync(void *callback_arg, u_int32_t code,
149 struct cam_path *path, void *arg);
150 static void abort_all_pending(struct targ_softc *softc);
151 static void notify_user(struct targ_softc *softc);
152 static int targcamstatus(cam_status status);
153 static size_t targccblen(xpt_opcode func_code);
155 static struct periph_driver targdriver =
158 TAILQ_HEAD_INITIALIZER(targdriver.units), /* generation */ 0
160 PERIPHDRIVER_DECLARE(targ, targdriver);
162 static MALLOC_DEFINE(M_TARG, "TARG", "TARG data");
165 * Create softc and initialize it. Only one proc can open each targ device.
166 * There is no locking here because a periph doesn't get created until an
167 * ioctl is issued to do so, and that can't happen until this method returns.
170 targopen(struct cdev *dev, int flags, int fmt, struct thread *td)
172 struct targ_softc *softc;
174 if (dev->si_drv1 != 0) {
178 /* Mark device busy before any potentially blocking operations */
179 dev->si_drv1 = (void *)~0;
181 /* Create the targ device, allocate its softc, initialize it */
182 if ((dev->si_flags & SI_NAMED) == 0) {
183 make_dev(&targ_cdevsw, dev2unit(dev), UID_ROOT, GID_WHEEL, 0600,
184 "targ%d", dev2unit(dev));
186 softc = malloc(sizeof(*softc), M_TARG,
188 dev->si_drv1 = softc;
189 softc->state = TARG_STATE_OPENED;
190 softc->periph = NULL;
193 TAILQ_INIT(&softc->pending_ccb_queue);
194 TAILQ_INIT(&softc->work_queue);
195 TAILQ_INIT(&softc->abort_queue);
196 TAILQ_INIT(&softc->user_ccb_queue);
197 knlist_init_mtx(&softc->read_select.si_note, NULL);
202 /* Disable LUN if enabled and teardown softc */
204 targclose(struct cdev *dev, int flag, int fmt, struct thread *td)
206 struct targ_softc *softc;
207 struct cam_periph *periph;
210 softc = (struct targ_softc *)dev->si_drv1;
211 if ((softc->periph == NULL) ||
212 (softc->state & TARG_STATE_LUN_ENABLED) == 0) {
219 * Acquire a hold on the periph so that it doesn't go away before
220 * we are ready at the end of the function.
222 periph = softc->periph;
223 cam_periph_acquire(periph);
224 cam_periph_lock(periph);
225 error = targdisable(softc);
226 if (error == CAM_REQ_CMP) {
228 if (softc->periph != NULL) {
229 cam_periph_invalidate(softc->periph);
230 softc->periph = NULL;
235 cam_periph_unlock(periph);
236 cam_periph_release(periph);
241 /* Enable/disable LUNs, set debugging level */
243 targioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
245 struct targ_softc *softc;
248 softc = (struct targ_softc *)dev->si_drv1;
253 struct ioc_enable_lun *new_lun;
254 struct cam_path *path;
257 new_lun = (struct ioc_enable_lun *)addr;
258 status = xpt_create_path_unlocked(&path, /*periph*/NULL,
262 if (status != CAM_REQ_CMP) {
263 printf("Couldn't create path, status %#x\n", status);
266 sim = xpt_path_sim(path);
268 status = targenable(softc, path, new_lun->grp6_len,
271 mtx_unlock(sim->mtx);
275 if (softc->periph == NULL) {
276 status = CAM_DEV_NOT_THERE;
279 cam_periph_lock(softc->periph);
280 status = targdisable(softc);
281 cam_periph_unlock(softc->periph);
286 struct ccb_debug cdbg;
288 /* If no periph available, disallow debugging changes */
289 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) {
290 status = CAM_DEV_NOT_THERE;
293 bzero(&cdbg, sizeof cdbg);
294 if (*((int *)addr) != 0)
295 cdbg.flags = CAM_DEBUG_PERIPH;
297 cdbg.flags = CAM_DEBUG_NONE;
298 cam_periph_lock(softc->periph);
299 xpt_setup_ccb(&cdbg.ccb_h, softc->path, /*priority*/0);
300 cdbg.ccb_h.func_code = XPT_DEBUG;
301 cdbg.ccb_h.cbfcnp = targdone;
303 xpt_action((union ccb *)&cdbg);
304 cam_periph_unlock(softc->periph);
305 status = cdbg.ccb_h.status & CAM_STATUS_MASK;
307 status = CAM_FUNC_NOTAVAIL;
312 status = CAM_PROVIDE_FAIL;
316 return (targcamstatus(status));
319 /* Writes are always ready, reads wait for user_ccb_queue or abort_queue */
321 targpoll(struct cdev *dev, int poll_events, struct thread *td)
323 struct targ_softc *softc;
326 softc = (struct targ_softc *)dev->si_drv1;
328 /* Poll for write() is always ok. */
329 revents = poll_events & (POLLOUT | POLLWRNORM);
330 if ((poll_events & (POLLIN | POLLRDNORM)) != 0) {
331 /* Poll for read() depends on user and abort queues. */
332 cam_periph_lock(softc->periph);
333 if (!TAILQ_EMPTY(&softc->user_ccb_queue) ||
334 !TAILQ_EMPTY(&softc->abort_queue)) {
335 revents |= poll_events & (POLLIN | POLLRDNORM);
337 cam_periph_unlock(softc->periph);
338 /* Only sleep if the user didn't poll for write. */
340 selrecord(td, &softc->read_select);
347 targkqfilter(struct cdev *dev, struct knote *kn)
349 struct targ_softc *softc;
351 softc = (struct targ_softc *)dev->si_drv1;
352 kn->kn_hook = (caddr_t)softc;
353 kn->kn_fop = &targread_filtops;
354 knlist_add(&softc->read_select.si_note, kn, 0);
359 targreadfiltdetach(struct knote *kn)
361 struct targ_softc *softc;
363 softc = (struct targ_softc *)kn->kn_hook;
364 knlist_remove(&softc->read_select.si_note, kn, 0);
367 /* Notify the user's kqueue when the user queue or abort queue gets a CCB */
369 targreadfilt(struct knote *kn, long hint)
371 struct targ_softc *softc;
374 softc = (struct targ_softc *)kn->kn_hook;
375 cam_periph_lock(softc->periph);
376 retval = !TAILQ_EMPTY(&softc->user_ccb_queue) ||
377 !TAILQ_EMPTY(&softc->abort_queue);
378 cam_periph_unlock(softc->periph);
382 /* Send the HBA the enable/disable message */
384 targendislun(struct cam_path *path, int enable, int grp6_len, int grp7_len)
386 struct ccb_en_lun en_ccb;
389 /* Tell the lun to begin answering selects */
390 xpt_setup_ccb(&en_ccb.ccb_h, path, /*priority*/1);
391 en_ccb.ccb_h.func_code = XPT_EN_LUN;
392 /* Don't need support for any vendor specific commands */
393 en_ccb.grp6_len = grp6_len;
394 en_ccb.grp7_len = grp7_len;
395 en_ccb.enable = enable ? 1 : 0;
396 xpt_action((union ccb *)&en_ccb);
397 status = en_ccb.ccb_h.status & CAM_STATUS_MASK;
398 if (status != CAM_REQ_CMP) {
399 xpt_print(path, "%sable lun CCB rejected, status %#x\n",
400 enable ? "en" : "dis", status);
405 /* Enable target mode on a LUN, given its path */
407 targenable(struct targ_softc *softc, struct cam_path *path, int grp6_len,
410 struct cam_periph *periph;
411 struct ccb_pathinq cpi;
414 if ((softc->state & TARG_STATE_LUN_ENABLED) != 0)
415 return (CAM_LUN_ALRDY_ENA);
417 /* Make sure SIM supports target mode */
418 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
419 cpi.ccb_h.func_code = XPT_PATH_INQ;
420 xpt_action((union ccb *)&cpi);
421 status = cpi.ccb_h.status & CAM_STATUS_MASK;
422 if (status != CAM_REQ_CMP) {
423 printf("pathinq failed, status %#x\n", status);
426 if ((cpi.target_sprt & PIT_PROCESSOR) == 0) {
427 printf("controller does not support target mode\n");
428 status = CAM_FUNC_NOTAVAIL;
432 /* Destroy any periph on our path if it is disabled */
433 periph = cam_periph_find(path, "targ");
434 if (periph != NULL) {
435 struct targ_softc *del_softc;
437 del_softc = (struct targ_softc *)periph->softc;
438 if ((del_softc->state & TARG_STATE_LUN_ENABLED) == 0) {
439 cam_periph_invalidate(del_softc->periph);
440 del_softc->periph = NULL;
442 printf("Requested path still in use by targ%d\n",
443 periph->unit_number);
444 status = CAM_LUN_ALRDY_ENA;
449 /* Create a periph instance attached to this path */
450 status = cam_periph_alloc(targctor, NULL, targdtor, targstart,
451 "targ", CAM_PERIPH_BIO, path, targasync, 0, softc);
452 if (status != CAM_REQ_CMP) {
453 printf("cam_periph_alloc failed, status %#x\n", status);
457 /* Ensure that the periph now exists. */
458 if (cam_periph_find(path, "targ") == NULL) {
459 panic("targenable: succeeded but no periph?");
463 /* Send the enable lun message */
464 status = targendislun(path, /*enable*/1, grp6_len, grp7_len);
465 if (status != CAM_REQ_CMP) {
466 printf("enable lun failed, status %#x\n", status);
469 softc->state |= TARG_STATE_LUN_ENABLED;
475 /* Disable this softc's target instance if enabled */
477 targdisable(struct targ_softc *softc)
481 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0)
482 return (CAM_REQ_CMP);
484 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targdisable\n"));
486 /* Abort any ccbs pending on the controller */
487 abort_all_pending(softc);
489 /* Disable this lun */
490 status = targendislun(softc->path, /*enable*/0,
491 /*grp6_len*/0, /*grp7_len*/0);
492 if (status == CAM_REQ_CMP)
493 softc->state &= ~TARG_STATE_LUN_ENABLED;
495 printf("Disable lun failed, status %#x\n", status);
500 /* Initialize a periph (called from cam_periph_alloc) */
502 targctor(struct cam_periph *periph, void *arg)
504 struct targ_softc *softc;
506 /* Store pointer to softc for periph-driven routines */
507 softc = (struct targ_softc *)arg;
508 periph->softc = softc;
509 softc->periph = periph;
510 softc->path = periph->path;
511 return (CAM_REQ_CMP);
515 targdtor(struct cam_periph *periph)
517 struct targ_softc *softc;
518 struct ccb_hdr *ccb_h;
519 struct targ_cmd_descr *descr;
521 softc = (struct targ_softc *)periph->softc;
524 * targdisable() aborts CCBs back to the user and leaves them
525 * on user_ccb_queue and abort_queue in case the user is still
526 * interested in them. We free them now.
528 while ((ccb_h = TAILQ_FIRST(&softc->user_ccb_queue)) != NULL) {
529 TAILQ_REMOVE(&softc->user_ccb_queue, ccb_h, periph_links.tqe);
530 targfreeccb(softc, (union ccb *)ccb_h);
532 while ((descr = TAILQ_FIRST(&softc->abort_queue)) != NULL) {
533 TAILQ_REMOVE(&softc->abort_queue, descr, tqe);
537 softc->periph = NULL;
539 periph->softc = NULL;
542 /* Receive CCBs from user mode proc and send them to the HBA */
544 targwrite(struct cdev *dev, struct uio *uio, int ioflag)
547 struct targ_softc *softc;
548 struct targ_cmd_descr *descr;
549 int write_len, error;
550 int func_code, priority;
552 softc = (struct targ_softc *)dev->si_drv1;
553 write_len = error = 0;
554 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
555 ("write - uio_resid %zd\n", uio->uio_resid));
556 while (uio->uio_resid >= sizeof(user_ccb) && error == 0) {
559 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
561 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
562 ("write - uiomove failed (%d)\n", error));
565 priority = fuword32(&user_ccb->ccb_h.pinfo.priority);
566 if (priority == -1) {
570 func_code = fuword32(&user_ccb->ccb_h.func_code);
572 case XPT_ACCEPT_TARGET_IO:
573 case XPT_IMMED_NOTIFY:
574 cam_periph_lock(softc->periph);
575 ccb = targgetccb(softc, func_code, priority);
576 descr = (struct targ_cmd_descr *)ccb->ccb_h.targ_descr;
577 descr->user_ccb = user_ccb;
578 descr->func_code = func_code;
579 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
580 ("Sent ATIO/INOT (%p)\n", user_ccb));
582 TAILQ_INSERT_TAIL(&softc->pending_ccb_queue,
585 cam_periph_unlock(softc->periph);
588 cam_periph_lock(softc->periph);
589 if ((func_code & XPT_FC_QUEUED) != 0) {
590 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
591 ("Sending queued ccb %#x (%p)\n",
592 func_code, user_ccb));
593 descr = targgetdescr(softc);
594 descr->user_ccb = user_ccb;
595 descr->priority = priority;
596 descr->func_code = func_code;
597 TAILQ_INSERT_TAIL(&softc->work_queue,
599 xpt_schedule(softc->periph, priority);
601 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
602 ("Sending inline ccb %#x (%p)\n",
603 func_code, user_ccb));
604 ccb = targgetccb(softc, func_code, priority);
605 descr = (struct targ_cmd_descr *)
606 ccb->ccb_h.targ_descr;
607 descr->user_ccb = user_ccb;
608 descr->priority = priority;
609 descr->func_code = func_code;
610 if (targusermerge(softc, descr, ccb) != EFAULT)
611 targsendccb(softc, ccb, descr);
612 targreturnccb(softc, ccb);
614 cam_periph_unlock(softc->periph);
617 write_len += sizeof(user_ccb);
621 * If we've successfully taken in some amount of
622 * data, return success for that data first. If
623 * an error is persistent, it will be reported
626 if (error != 0 && write_len == 0)
628 if (write_len == 0 && uio->uio_resid != 0)
633 /* Process requests (descrs) via the periph-supplied CCBs */
635 targstart(struct cam_periph *periph, union ccb *start_ccb)
637 struct targ_softc *softc;
638 struct targ_cmd_descr *descr, *next_descr;
641 softc = (struct targ_softc *)periph->softc;
642 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targstart %p\n", start_ccb));
644 descr = TAILQ_FIRST(&softc->work_queue);
646 xpt_release_ccb(start_ccb);
648 TAILQ_REMOVE(&softc->work_queue, descr, tqe);
649 next_descr = TAILQ_FIRST(&softc->work_queue);
651 /* Initiate a transaction using the descr and supplied CCB */
652 error = targusermerge(softc, descr, start_ccb);
654 error = targsendccb(softc, start_ccb, descr);
656 xpt_print(periph->path,
657 "targsendccb failed, err %d\n", error);
658 xpt_release_ccb(start_ccb);
659 suword(&descr->user_ccb->ccb_h.status,
661 TAILQ_INSERT_TAIL(&softc->abort_queue, descr, tqe);
665 /* If we have more work to do, stay scheduled */
666 if (next_descr != NULL)
667 xpt_schedule(periph, next_descr->priority);
672 targusermerge(struct targ_softc *softc, struct targ_cmd_descr *descr,
675 struct ccb_hdr *u_ccbh, *k_ccbh;
679 u_ccbh = &descr->user_ccb->ccb_h;
680 k_ccbh = &ccb->ccb_h;
683 * There are some fields in the CCB header that need to be
684 * preserved, the rest we get from the user ccb. (See xpt_merge_ccb)
686 xpt_setup_ccb(k_ccbh, softc->path, descr->priority);
687 k_ccbh->retry_count = fuword32(&u_ccbh->retry_count);
688 k_ccbh->func_code = descr->func_code;
689 k_ccbh->flags = fuword32(&u_ccbh->flags);
690 k_ccbh->timeout = fuword32(&u_ccbh->timeout);
691 ccb_len = targccblen(k_ccbh->func_code) - sizeof(struct ccb_hdr);
692 error = copyin(u_ccbh + 1, k_ccbh + 1, ccb_len);
694 k_ccbh->status = CAM_REQ_CMP_ERR;
698 /* Translate usermode abort_ccb pointer to its kernel counterpart */
699 if (k_ccbh->func_code == XPT_ABORT) {
700 struct ccb_abort *cab;
701 struct ccb_hdr *ccb_h;
703 cab = (struct ccb_abort *)ccb;
704 TAILQ_FOREACH(ccb_h, &softc->pending_ccb_queue,
706 struct targ_cmd_descr *ab_descr;
708 ab_descr = (struct targ_cmd_descr *)ccb_h->targ_descr;
709 if (ab_descr->user_ccb == cab->abort_ccb) {
710 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
711 ("Changing abort for %p to %p\n",
712 cab->abort_ccb, ccb_h));
713 cab->abort_ccb = (union ccb *)ccb_h;
717 /* CCB not found, set appropriate status */
719 k_ccbh->status = CAM_PATH_INVALID;
727 /* Build and send a kernel CCB formed from descr->user_ccb */
729 targsendccb(struct targ_softc *softc, union ccb *ccb,
730 struct targ_cmd_descr *descr)
732 struct cam_periph_map_info *mapinfo;
733 struct ccb_hdr *ccb_h;
737 mapinfo = &descr->mapinfo;
738 mapinfo->num_bufs_used = 0;
741 * There's no way for the user to have a completion
742 * function, so we put our own completion function in here.
743 * We also stash in a reference to our descriptor so targreturnccb()
744 * can find our mapping info.
746 ccb_h->cbfcnp = targdone;
747 ccb_h->targ_descr = descr;
750 * We only attempt to map the user memory into kernel space
751 * if they haven't passed in a physical memory pointer,
752 * and if there is actually an I/O operation to perform.
753 * Right now cam_periph_mapmem() only supports SCSI and device
754 * match CCBs. For the SCSI CCBs, we only pass the CCB in if
755 * there's actually data to map. cam_periph_mapmem() will do the
756 * right thing, even if there isn't data to map, but since CCBs
757 * without data are a reasonably common occurance (e.g. test unit
758 * ready), it will save a few cycles if we check for it here.
760 if (((ccb_h->flags & CAM_DATA_PHYS) == 0)
761 && (((ccb_h->func_code == XPT_CONT_TARGET_IO)
762 && ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE))
763 || (ccb_h->func_code == XPT_DEV_MATCH))) {
765 error = cam_periph_mapmem(ccb, mapinfo);
768 * cam_periph_mapmem returned an error, we can't continue.
769 * Return the error to the user.
772 ccb_h->status = CAM_REQ_CMP_ERR;
773 mapinfo->num_bufs_used = 0;
779 * Once queued on the pending CCB list, this CCB will be protected
780 * by our error recovery handler.
782 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("sendccb %p\n", ccb));
783 if (XPT_FC_IS_QUEUED(ccb)) {
784 TAILQ_INSERT_TAIL(&softc->pending_ccb_queue, ccb_h,
792 /* Completion routine for CCBs (called at splsoftcam) */
794 targdone(struct cam_periph *periph, union ccb *done_ccb)
796 struct targ_softc *softc;
799 CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("targdone %p\n", done_ccb));
800 softc = (struct targ_softc *)periph->softc;
801 TAILQ_REMOVE(&softc->pending_ccb_queue, &done_ccb->ccb_h,
803 status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
805 /* If we're no longer enabled, throw away CCB */
806 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) {
807 targfreeccb(softc, done_ccb);
810 /* abort_all_pending() waits for pending queue to be empty */
811 if (TAILQ_EMPTY(&softc->pending_ccb_queue))
812 wakeup(&softc->pending_ccb_queue);
814 switch (done_ccb->ccb_h.func_code) {
815 /* All FC_*_QUEUED CCBs go back to userland */
816 case XPT_IMMED_NOTIFY:
817 case XPT_ACCEPT_TARGET_IO:
818 case XPT_CONT_TARGET_IO:
819 TAILQ_INSERT_TAIL(&softc->user_ccb_queue, &done_ccb->ccb_h,
824 panic("targdone: impossible xpt opcode %#x",
825 done_ccb->ccb_h.func_code);
830 /* Return CCBs to the user from the user queue and abort queue */
832 targread(struct cdev *dev, struct uio *uio, int ioflag)
834 struct descr_queue *abort_queue;
835 struct targ_cmd_descr *user_descr;
836 struct targ_softc *softc;
837 struct ccb_queue *user_queue;
838 struct ccb_hdr *ccb_h;
844 softc = (struct targ_softc *)dev->si_drv1;
845 user_queue = &softc->user_ccb_queue;
846 abort_queue = &softc->abort_queue;
847 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targread\n"));
849 /* If no data is available, wait or return immediately */
850 cam_periph_lock(softc->periph);
851 ccb_h = TAILQ_FIRST(user_queue);
852 user_descr = TAILQ_FIRST(abort_queue);
853 while (ccb_h == NULL && user_descr == NULL) {
854 if ((ioflag & IO_NDELAY) == 0) {
855 error = msleep(user_queue, softc->periph->sim->mtx,
856 PRIBIO | PCATCH, "targrd", 0);
857 ccb_h = TAILQ_FIRST(user_queue);
858 user_descr = TAILQ_FIRST(abort_queue);
860 if (error == ERESTART) {
867 cam_periph_unlock(softc->periph);
872 /* Data is available so fill the user's buffer */
873 while (ccb_h != NULL) {
874 struct targ_cmd_descr *descr;
876 if (uio->uio_resid < sizeof(user_ccb))
878 TAILQ_REMOVE(user_queue, ccb_h, periph_links.tqe);
879 descr = (struct targ_cmd_descr *)ccb_h->targ_descr;
880 user_ccb = descr->user_ccb;
881 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
882 ("targread ccb %p (%p)\n", ccb_h, user_ccb));
883 error = targreturnccb(softc, (union ccb *)ccb_h);
886 cam_periph_unlock(softc->periph);
887 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
888 cam_periph_lock(softc->periph);
891 read_len += sizeof(user_ccb);
893 ccb_h = TAILQ_FIRST(user_queue);
896 /* Flush out any aborted descriptors */
897 while (user_descr != NULL) {
898 if (uio->uio_resid < sizeof(user_ccb))
900 TAILQ_REMOVE(abort_queue, user_descr, tqe);
901 user_ccb = user_descr->user_ccb;
902 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
903 ("targread aborted descr %p (%p)\n",
904 user_descr, user_ccb));
905 suword(&user_ccb->ccb_h.status, CAM_REQ_ABORTED);
906 cam_periph_unlock(softc->periph);
907 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
908 cam_periph_lock(softc->periph);
911 read_len += sizeof(user_ccb);
913 user_descr = TAILQ_FIRST(abort_queue);
917 * If we've successfully read some amount of data, don't report an
918 * error. If the error is persistent, it will be reported on the
921 if (read_len == 0 && uio->uio_resid != 0)
925 cam_periph_unlock(softc->periph);
929 /* Copy completed ccb back to the user */
931 targreturnccb(struct targ_softc *softc, union ccb *ccb)
933 struct targ_cmd_descr *descr;
934 struct ccb_hdr *u_ccbh;
938 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targreturnccb %p\n", ccb));
939 descr = (struct targ_cmd_descr *)ccb->ccb_h.targ_descr;
940 u_ccbh = &descr->user_ccb->ccb_h;
942 /* Copy out the central portion of the ccb_hdr */
943 copyout(&ccb->ccb_h.retry_count, &u_ccbh->retry_count,
944 offsetof(struct ccb_hdr, periph_priv) -
945 offsetof(struct ccb_hdr, retry_count));
947 /* Copy out the rest of the ccb (after the ccb_hdr) */
948 ccb_len = targccblen(ccb->ccb_h.func_code) - sizeof(struct ccb_hdr);
949 if (descr->mapinfo.num_bufs_used != 0)
950 cam_periph_unmapmem(ccb, &descr->mapinfo);
951 error = copyout(&ccb->ccb_h + 1, u_ccbh + 1, ccb_len);
953 xpt_print(softc->path,
954 "targreturnccb - CCB copyout failed (%d)\n", error);
956 /* Free CCB or send back to devq. */
957 targfreeccb(softc, ccb);
963 targgetccb(struct targ_softc *softc, xpt_opcode type, int priority)
968 ccb_len = targccblen(type);
969 ccb = malloc(ccb_len, M_TARG, M_WAITOK);
970 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("getccb %p\n", ccb));
972 xpt_setup_ccb(&ccb->ccb_h, softc->path, priority);
973 ccb->ccb_h.func_code = type;
974 ccb->ccb_h.cbfcnp = targdone;
975 ccb->ccb_h.targ_descr = targgetdescr(softc);
980 targfreeccb(struct targ_softc *softc, union ccb *ccb)
982 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, ("targfreeccb descr %p and\n",
983 ccb->ccb_h.targ_descr));
984 free(ccb->ccb_h.targ_descr, M_TARG);
986 switch (ccb->ccb_h.func_code) {
987 case XPT_ACCEPT_TARGET_IO:
988 case XPT_IMMED_NOTIFY:
989 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, ("freeing ccb %p\n", ccb));
993 /* Send back CCB if we got it from the periph */
994 if (XPT_FC_IS_QUEUED(ccb)) {
995 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH,
996 ("returning queued ccb %p\n", ccb));
997 xpt_release_ccb(ccb);
999 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH,
1000 ("freeing ccb %p\n", ccb));
1007 static struct targ_cmd_descr *
1008 targgetdescr(struct targ_softc *softc)
1010 struct targ_cmd_descr *descr;
1012 descr = malloc(sizeof(*descr), M_TARG,
1014 descr->mapinfo.num_bufs_used = 0;
1021 EVENTHANDLER_REGISTER(dev_clone, targclone, 0, 1000);
1025 targclone(void *arg, struct ucred *cred, char *name, int namelen,
1032 if (dev_stdclone(name, NULL, "targ", &u) != 1)
1034 *dev = make_dev(&targ_cdevsw, u, UID_ROOT, GID_WHEEL,
1037 (*dev)->si_flags |= SI_CHEAPCLONE;
1041 targasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
1043 /* All events are handled in usermode by INOTs */
1044 panic("targasync() called, should be an INOT instead");
1047 /* Cancel all pending requests and CCBs awaiting work. */
1049 abort_all_pending(struct targ_softc *softc)
1051 struct targ_cmd_descr *descr;
1052 struct ccb_abort cab;
1053 struct ccb_hdr *ccb_h;
1054 struct cam_sim *sim;
1056 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("abort_all_pending\n"));
1058 /* First abort the descriptors awaiting resources */
1059 while ((descr = TAILQ_FIRST(&softc->work_queue)) != NULL) {
1060 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
1061 ("Aborting descr from workq %p\n", descr));
1062 TAILQ_REMOVE(&softc->work_queue, descr, tqe);
1063 TAILQ_INSERT_TAIL(&softc->abort_queue, descr, tqe);
1067 * Then abort all pending CCBs.
1068 * targdone() will return the aborted CCB via user_ccb_queue
1070 xpt_setup_ccb(&cab.ccb_h, softc->path, /*priority*/0);
1071 cab.ccb_h.func_code = XPT_ABORT;
1072 cab.ccb_h.status = CAM_REQ_CMP_ERR;
1073 TAILQ_FOREACH(ccb_h, &softc->pending_ccb_queue, periph_links.tqe) {
1074 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
1075 ("Aborting pending CCB %p\n", ccb_h));
1076 cab.abort_ccb = (union ccb *)ccb_h;
1077 xpt_action((union ccb *)&cab);
1078 if (cab.ccb_h.status != CAM_REQ_CMP) {
1079 xpt_print(cab.ccb_h.path,
1080 "Unable to abort CCB, status %#x\n",
1085 /* If we aborted at least one pending CCB ok, wait for it. */
1086 if (cab.ccb_h.status == CAM_REQ_CMP) {
1087 sim = xpt_path_sim(softc->path);
1088 msleep(&softc->pending_ccb_queue, sim->mtx,
1089 PRIBIO | PCATCH, "tgabrt", 0);
1092 /* If we aborted anything from the work queue, wakeup user. */
1093 if (!TAILQ_EMPTY(&softc->user_ccb_queue)
1094 || !TAILQ_EMPTY(&softc->abort_queue))
1098 /* Notify the user that data is ready */
1100 notify_user(struct targ_softc *softc)
1103 * Notify users sleeping via poll(), kqueue(), and
1106 selwakeuppri(&softc->read_select, PRIBIO);
1107 KNOTE_UNLOCKED(&softc->read_select.si_note, 0);
1108 wakeup(&softc->user_ccb_queue);
1111 /* Convert CAM status to errno values */
1113 targcamstatus(cam_status status)
1115 switch (status & CAM_STATUS_MASK) {
1116 case CAM_REQ_CMP: /* CCB request completed without error */
1118 case CAM_REQ_INPROG: /* CCB request is in progress */
1119 return (EINPROGRESS);
1120 case CAM_REQ_CMP_ERR: /* CCB request completed with an error */
1122 case CAM_PROVIDE_FAIL: /* Unable to provide requested capability */
1124 case CAM_FUNC_NOTAVAIL: /* The requested function is not available */
1126 case CAM_LUN_ALRDY_ENA: /* LUN is already enabled for target mode */
1127 return (EADDRINUSE);
1128 case CAM_PATH_INVALID: /* Supplied Path ID is invalid */
1129 case CAM_DEV_NOT_THERE: /* SCSI Device Not Installed/there */
1131 case CAM_REQ_ABORTED: /* CCB request aborted by the host */
1133 case CAM_CMD_TIMEOUT: /* Command timeout */
1135 case CAM_REQUEUE_REQ: /* Requeue to preserve transaction ordering */
1137 case CAM_REQ_INVALID: /* CCB request was invalid */
1139 case CAM_RESRC_UNAVAIL: /* Resource Unavailable */
1141 case CAM_BUSY: /* CAM subsytem is busy */
1142 case CAM_UA_ABORT: /* Unable to abort CCB request */
1150 targccblen(xpt_opcode func_code)
1154 /* Codes we expect to see as a target */
1155 switch (func_code) {
1156 case XPT_CONT_TARGET_IO:
1158 len = sizeof(struct ccb_scsiio);
1160 case XPT_ACCEPT_TARGET_IO:
1161 len = sizeof(struct ccb_accept_tio);
1163 case XPT_IMMED_NOTIFY:
1164 len = sizeof(struct ccb_immed_notify);
1167 len = sizeof(struct ccb_relsim);
1170 len = sizeof(struct ccb_pathinq);
1173 len = sizeof(struct ccb_debug);
1176 len = sizeof(struct ccb_abort);
1179 len = sizeof(struct ccb_en_lun);
1182 len = sizeof(union ccb);