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;
101 static d_open_t targopen;
102 static d_read_t targread;
103 static d_write_t targwrite;
104 static d_ioctl_t targioctl;
105 static d_poll_t targpoll;
106 static d_kqfilter_t targkqfilter;
107 static void targreadfiltdetach(struct knote *kn);
108 static int targreadfilt(struct knote *kn, long hint);
109 static struct filterops targread_filtops = {
111 .f_detach = targreadfiltdetach,
112 .f_event = targreadfilt,
115 static struct cdevsw targ_cdevsw = {
116 .d_version = D_VERSION,
117 .d_flags = D_NEEDGIANT,
120 .d_write = targwrite,
121 .d_ioctl = targioctl,
124 .d_kqfilter = targkqfilter
127 static cam_status targendislun(struct cam_path *path, int enable,
128 int grp6_len, int grp7_len);
129 static cam_status targenable(struct targ_softc *softc,
130 struct cam_path *path,
131 int grp6_len, int grp7_len);
132 static cam_status targdisable(struct targ_softc *softc);
133 static periph_ctor_t targctor;
134 static periph_dtor_t targdtor;
135 static periph_start_t targstart;
136 static int targusermerge(struct targ_softc *softc,
137 struct targ_cmd_descr *descr,
139 static int targsendccb(struct targ_softc *softc, union ccb *ccb,
140 struct targ_cmd_descr *descr);
141 static void targdone(struct cam_periph *periph,
142 union ccb *done_ccb);
143 static int targreturnccb(struct targ_softc *softc,
145 static union ccb * targgetccb(struct targ_softc *softc, xpt_opcode type,
147 static void targfreeccb(struct targ_softc *softc, union ccb *ccb);
148 static struct targ_cmd_descr *
149 targgetdescr(struct targ_softc *softc);
150 static periph_init_t targinit;
151 static void targasync(void *callback_arg, u_int32_t code,
152 struct cam_path *path, void *arg);
153 static void abort_all_pending(struct targ_softc *softc);
154 static void notify_user(struct targ_softc *softc);
155 static int targcamstatus(cam_status status);
156 static size_t targccblen(xpt_opcode func_code);
158 static struct periph_driver targdriver =
161 TAILQ_HEAD_INITIALIZER(targdriver.units), /* generation */ 0
163 PERIPHDRIVER_DECLARE(targ, targdriver);
165 static MALLOC_DEFINE(M_TARG, "TARG", "TARG data");
167 /* Disable LUN if enabled and teardown softc */
169 targcdevdtor(void *data)
171 struct targ_softc *softc;
172 struct cam_periph *periph;
175 if (softc->periph == NULL) {
176 printf("%s: destroying non-enabled target\n", __func__);
182 * Acquire a hold on the periph so that it doesn't go away before
183 * we are ready at the end of the function.
185 periph = softc->periph;
186 cam_periph_acquire(periph);
187 cam_periph_lock(periph);
188 (void)targdisable(softc);
189 if (softc->periph != NULL) {
190 cam_periph_invalidate(softc->periph);
191 softc->periph = NULL;
193 cam_periph_unlock(periph);
194 cam_periph_release(periph);
199 * Create softc and initialize it. There is no locking here because a
200 * periph doesn't get created until an ioctl is issued to do so, and
201 * that can't happen until this method returns.
204 targopen(struct cdev *dev, int flags, int fmt, struct thread *td)
206 struct targ_softc *softc;
208 /* Allocate its softc, initialize it */
209 softc = malloc(sizeof(*softc), M_TARG,
211 softc->state = TARG_STATE_OPENED;
212 softc->periph = NULL;
215 TAILQ_INIT(&softc->pending_ccb_queue);
216 TAILQ_INIT(&softc->work_queue);
217 TAILQ_INIT(&softc->abort_queue);
218 TAILQ_INIT(&softc->user_ccb_queue);
219 knlist_init_mtx(&softc->read_select.si_note, NULL);
221 devfs_set_cdevpriv(softc, targcdevdtor);
225 /* Enable/disable LUNs, set debugging level */
227 targioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
229 struct targ_softc *softc;
232 devfs_get_cdevpriv((void **)&softc);
237 struct ioc_enable_lun *new_lun;
238 struct cam_path *path;
241 new_lun = (struct ioc_enable_lun *)addr;
242 status = xpt_create_path_unlocked(&path, /*periph*/NULL,
246 if (status != CAM_REQ_CMP) {
247 printf("Couldn't create path, status %#x\n", status);
250 sim = xpt_path_sim(path);
252 status = targenable(softc, path, new_lun->grp6_len,
255 mtx_unlock(sim->mtx);
259 if (softc->periph == NULL) {
260 status = CAM_DEV_NOT_THERE;
263 cam_periph_lock(softc->periph);
264 status = targdisable(softc);
265 cam_periph_unlock(softc->periph);
269 struct ccb_debug cdbg;
271 /* If no periph available, disallow debugging changes */
272 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) {
273 status = CAM_DEV_NOT_THERE;
276 bzero(&cdbg, sizeof cdbg);
277 if (*((int *)addr) != 0)
278 cdbg.flags = CAM_DEBUG_PERIPH;
280 cdbg.flags = CAM_DEBUG_NONE;
281 cam_periph_lock(softc->periph);
282 xpt_setup_ccb(&cdbg.ccb_h, softc->path, CAM_PRIORITY_NORMAL);
283 cdbg.ccb_h.func_code = XPT_DEBUG;
284 cdbg.ccb_h.cbfcnp = targdone;
286 xpt_action((union ccb *)&cdbg);
287 cam_periph_unlock(softc->periph);
288 status = cdbg.ccb_h.status & CAM_STATUS_MASK;
292 status = CAM_PROVIDE_FAIL;
296 return (targcamstatus(status));
299 /* Writes are always ready, reads wait for user_ccb_queue or abort_queue */
301 targpoll(struct cdev *dev, int poll_events, struct thread *td)
303 struct targ_softc *softc;
306 devfs_get_cdevpriv((void **)&softc);
308 /* Poll for write() is always ok. */
309 revents = poll_events & (POLLOUT | POLLWRNORM);
310 if ((poll_events & (POLLIN | POLLRDNORM)) != 0) {
311 /* Poll for read() depends on user and abort queues. */
312 cam_periph_lock(softc->periph);
313 if (!TAILQ_EMPTY(&softc->user_ccb_queue) ||
314 !TAILQ_EMPTY(&softc->abort_queue)) {
315 revents |= poll_events & (POLLIN | POLLRDNORM);
317 cam_periph_unlock(softc->periph);
318 /* Only sleep if the user didn't poll for write. */
320 selrecord(td, &softc->read_select);
327 targkqfilter(struct cdev *dev, struct knote *kn)
329 struct targ_softc *softc;
331 devfs_get_cdevpriv((void **)&softc);
332 kn->kn_hook = (caddr_t)softc;
333 kn->kn_fop = &targread_filtops;
334 knlist_add(&softc->read_select.si_note, kn, 0);
339 targreadfiltdetach(struct knote *kn)
341 struct targ_softc *softc;
343 softc = (struct targ_softc *)kn->kn_hook;
344 knlist_remove(&softc->read_select.si_note, kn, 0);
347 /* Notify the user's kqueue when the user queue or abort queue gets a CCB */
349 targreadfilt(struct knote *kn, long hint)
351 struct targ_softc *softc;
354 softc = (struct targ_softc *)kn->kn_hook;
355 cam_periph_lock(softc->periph);
356 retval = !TAILQ_EMPTY(&softc->user_ccb_queue) ||
357 !TAILQ_EMPTY(&softc->abort_queue);
358 cam_periph_unlock(softc->periph);
362 /* Send the HBA the enable/disable message */
364 targendislun(struct cam_path *path, int enable, int grp6_len, int grp7_len)
366 struct ccb_en_lun en_ccb;
369 /* Tell the lun to begin answering selects */
370 xpt_setup_ccb(&en_ccb.ccb_h, path, CAM_PRIORITY_NORMAL);
371 en_ccb.ccb_h.func_code = XPT_EN_LUN;
372 /* Don't need support for any vendor specific commands */
373 en_ccb.grp6_len = grp6_len;
374 en_ccb.grp7_len = grp7_len;
375 en_ccb.enable = enable ? 1 : 0;
376 xpt_action((union ccb *)&en_ccb);
377 status = en_ccb.ccb_h.status & CAM_STATUS_MASK;
378 if (status != CAM_REQ_CMP) {
379 xpt_print(path, "%sable lun CCB rejected, status %#x\n",
380 enable ? "en" : "dis", status);
385 /* Enable target mode on a LUN, given its path */
387 targenable(struct targ_softc *softc, struct cam_path *path, int grp6_len,
390 struct cam_periph *periph;
391 struct ccb_pathinq cpi;
394 if ((softc->state & TARG_STATE_LUN_ENABLED) != 0)
395 return (CAM_LUN_ALRDY_ENA);
397 /* Make sure SIM supports target mode */
398 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
399 cpi.ccb_h.func_code = XPT_PATH_INQ;
400 xpt_action((union ccb *)&cpi);
401 status = cpi.ccb_h.status & CAM_STATUS_MASK;
402 if (status != CAM_REQ_CMP) {
403 printf("pathinq failed, status %#x\n", status);
406 if ((cpi.target_sprt & PIT_PROCESSOR) == 0) {
407 printf("controller does not support target mode\n");
408 status = CAM_FUNC_NOTAVAIL;
412 /* Destroy any periph on our path if it is disabled */
413 periph = cam_periph_find(path, "targ");
414 if (periph != NULL) {
415 struct targ_softc *del_softc;
417 del_softc = (struct targ_softc *)periph->softc;
418 if ((del_softc->state & TARG_STATE_LUN_ENABLED) == 0) {
419 cam_periph_invalidate(del_softc->periph);
420 del_softc->periph = NULL;
422 printf("Requested path still in use by targ%d\n",
423 periph->unit_number);
424 status = CAM_LUN_ALRDY_ENA;
429 /* Create a periph instance attached to this path */
430 status = cam_periph_alloc(targctor, NULL, targdtor, targstart,
431 "targ", CAM_PERIPH_BIO, path, targasync, 0, softc);
432 if (status != CAM_REQ_CMP) {
433 printf("cam_periph_alloc failed, status %#x\n", status);
437 /* Ensure that the periph now exists. */
438 if (cam_periph_find(path, "targ") == NULL) {
439 panic("targenable: succeeded but no periph?");
443 /* Send the enable lun message */
444 status = targendislun(path, /*enable*/1, grp6_len, grp7_len);
445 if (status != CAM_REQ_CMP) {
446 printf("enable lun failed, status %#x\n", status);
449 softc->state |= TARG_STATE_LUN_ENABLED;
455 /* Disable this softc's target instance if enabled */
457 targdisable(struct targ_softc *softc)
461 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0)
462 return (CAM_REQ_CMP);
464 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targdisable\n"));
466 /* Abort any ccbs pending on the controller */
467 abort_all_pending(softc);
469 /* Disable this lun */
470 status = targendislun(softc->path, /*enable*/0,
471 /*grp6_len*/0, /*grp7_len*/0);
472 if (status == CAM_REQ_CMP)
473 softc->state &= ~TARG_STATE_LUN_ENABLED;
475 printf("Disable lun failed, status %#x\n", status);
480 /* Initialize a periph (called from cam_periph_alloc) */
482 targctor(struct cam_periph *periph, void *arg)
484 struct targ_softc *softc;
486 /* Store pointer to softc for periph-driven routines */
487 softc = (struct targ_softc *)arg;
488 periph->softc = softc;
489 softc->periph = periph;
490 softc->path = periph->path;
491 return (CAM_REQ_CMP);
495 targdtor(struct cam_periph *periph)
497 struct targ_softc *softc;
498 struct ccb_hdr *ccb_h;
499 struct targ_cmd_descr *descr;
501 softc = (struct targ_softc *)periph->softc;
504 * targdisable() aborts CCBs back to the user and leaves them
505 * on user_ccb_queue and abort_queue in case the user is still
506 * interested in them. We free them now.
508 while ((ccb_h = TAILQ_FIRST(&softc->user_ccb_queue)) != NULL) {
509 TAILQ_REMOVE(&softc->user_ccb_queue, ccb_h, periph_links.tqe);
510 targfreeccb(softc, (union ccb *)ccb_h);
512 while ((descr = TAILQ_FIRST(&softc->abort_queue)) != NULL) {
513 TAILQ_REMOVE(&softc->abort_queue, descr, tqe);
517 softc->periph = NULL;
519 periph->softc = NULL;
522 /* Receive CCBs from user mode proc and send them to the HBA */
524 targwrite(struct cdev *dev, struct uio *uio, int ioflag)
527 struct targ_softc *softc;
528 struct targ_cmd_descr *descr;
529 int write_len, error;
530 int func_code, priority;
532 devfs_get_cdevpriv((void **)&softc);
533 write_len = error = 0;
534 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
535 ("write - uio_resid %zd\n", uio->uio_resid));
536 while (uio->uio_resid >= sizeof(user_ccb) && error == 0) {
539 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
541 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
542 ("write - uiomove failed (%d)\n", error));
545 priority = fuword32(&user_ccb->ccb_h.pinfo.priority);
546 if (priority == CAM_PRIORITY_NONE) {
550 func_code = fuword32(&user_ccb->ccb_h.func_code);
552 case XPT_ACCEPT_TARGET_IO:
553 case XPT_IMMED_NOTIFY:
554 case XPT_IMMEDIATE_NOTIFY:
555 cam_periph_lock(softc->periph);
556 ccb = targgetccb(softc, func_code, priority);
557 descr = (struct targ_cmd_descr *)ccb->ccb_h.targ_descr;
558 descr->user_ccb = user_ccb;
559 descr->func_code = func_code;
560 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
561 ("Sent ATIO/INOT (%p)\n", user_ccb));
563 TAILQ_INSERT_TAIL(&softc->pending_ccb_queue,
566 cam_periph_unlock(softc->periph);
569 cam_periph_lock(softc->periph);
570 if ((func_code & XPT_FC_QUEUED) != 0) {
571 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
572 ("Sending queued ccb %#x (%p)\n",
573 func_code, user_ccb));
574 descr = targgetdescr(softc);
575 descr->user_ccb = user_ccb;
576 descr->priority = priority;
577 descr->func_code = func_code;
578 TAILQ_INSERT_TAIL(&softc->work_queue,
580 xpt_schedule(softc->periph, priority);
582 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
583 ("Sending inline ccb %#x (%p)\n",
584 func_code, user_ccb));
585 ccb = targgetccb(softc, func_code, priority);
586 descr = (struct targ_cmd_descr *)
587 ccb->ccb_h.targ_descr;
588 descr->user_ccb = user_ccb;
589 descr->priority = priority;
590 descr->func_code = func_code;
591 if (targusermerge(softc, descr, ccb) != EFAULT)
592 targsendccb(softc, ccb, descr);
593 targreturnccb(softc, ccb);
595 cam_periph_unlock(softc->periph);
598 write_len += sizeof(user_ccb);
602 * If we've successfully taken in some amount of
603 * data, return success for that data first. If
604 * an error is persistent, it will be reported
607 if (error != 0 && write_len == 0)
609 if (write_len == 0 && uio->uio_resid != 0)
614 /* Process requests (descrs) via the periph-supplied CCBs */
616 targstart(struct cam_periph *periph, union ccb *start_ccb)
618 struct targ_softc *softc;
619 struct targ_cmd_descr *descr, *next_descr;
622 softc = (struct targ_softc *)periph->softc;
623 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targstart %p\n", start_ccb));
625 descr = TAILQ_FIRST(&softc->work_queue);
627 xpt_release_ccb(start_ccb);
629 TAILQ_REMOVE(&softc->work_queue, descr, tqe);
630 next_descr = TAILQ_FIRST(&softc->work_queue);
632 /* Initiate a transaction using the descr and supplied CCB */
633 error = targusermerge(softc, descr, start_ccb);
635 error = targsendccb(softc, start_ccb, descr);
637 xpt_print(periph->path,
638 "targsendccb failed, err %d\n", error);
639 xpt_release_ccb(start_ccb);
640 suword(&descr->user_ccb->ccb_h.status,
642 TAILQ_INSERT_TAIL(&softc->abort_queue, descr, tqe);
646 /* If we have more work to do, stay scheduled */
647 if (next_descr != NULL)
648 xpt_schedule(periph, next_descr->priority);
653 targusermerge(struct targ_softc *softc, struct targ_cmd_descr *descr,
656 struct ccb_hdr *u_ccbh, *k_ccbh;
660 u_ccbh = &descr->user_ccb->ccb_h;
661 k_ccbh = &ccb->ccb_h;
664 * There are some fields in the CCB header that need to be
665 * preserved, the rest we get from the user ccb. (See xpt_merge_ccb)
667 xpt_setup_ccb(k_ccbh, softc->path, descr->priority);
668 k_ccbh->retry_count = fuword32(&u_ccbh->retry_count);
669 k_ccbh->func_code = descr->func_code;
670 k_ccbh->flags = fuword32(&u_ccbh->flags);
671 k_ccbh->timeout = fuword32(&u_ccbh->timeout);
672 ccb_len = targccblen(k_ccbh->func_code) - sizeof(struct ccb_hdr);
673 error = copyin(u_ccbh + 1, k_ccbh + 1, ccb_len);
675 k_ccbh->status = CAM_REQ_CMP_ERR;
679 /* Translate usermode abort_ccb pointer to its kernel counterpart */
680 if (k_ccbh->func_code == XPT_ABORT) {
681 struct ccb_abort *cab;
682 struct ccb_hdr *ccb_h;
684 cab = (struct ccb_abort *)ccb;
685 TAILQ_FOREACH(ccb_h, &softc->pending_ccb_queue,
687 struct targ_cmd_descr *ab_descr;
689 ab_descr = (struct targ_cmd_descr *)ccb_h->targ_descr;
690 if (ab_descr->user_ccb == cab->abort_ccb) {
691 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
692 ("Changing abort for %p to %p\n",
693 cab->abort_ccb, ccb_h));
694 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;
730 if ((ccb_h->func_code == XPT_CONT_TARGET_IO) ||
731 (ccb_h->func_code == XPT_DEV_MATCH)) {
733 error = cam_periph_mapmem(ccb, mapinfo);
736 * cam_periph_mapmem returned an error, we can't continue.
737 * Return the error to the user.
740 ccb_h->status = CAM_REQ_CMP_ERR;
741 mapinfo->num_bufs_used = 0;
747 * Once queued on the pending CCB list, this CCB will be protected
748 * by our error recovery handler.
750 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("sendccb %p\n", ccb));
751 if (XPT_FC_IS_QUEUED(ccb)) {
752 TAILQ_INSERT_TAIL(&softc->pending_ccb_queue, ccb_h,
760 /* Completion routine for CCBs (called at splsoftcam) */
762 targdone(struct cam_periph *periph, union ccb *done_ccb)
764 struct targ_softc *softc;
767 CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("targdone %p\n", done_ccb));
768 softc = (struct targ_softc *)periph->softc;
769 TAILQ_REMOVE(&softc->pending_ccb_queue, &done_ccb->ccb_h,
771 status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
773 /* If we're no longer enabled, throw away CCB */
774 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) {
775 targfreeccb(softc, done_ccb);
778 /* abort_all_pending() waits for pending queue to be empty */
779 if (TAILQ_EMPTY(&softc->pending_ccb_queue))
780 wakeup(&softc->pending_ccb_queue);
782 switch (done_ccb->ccb_h.func_code) {
783 /* All FC_*_QUEUED CCBs go back to userland */
784 case XPT_IMMED_NOTIFY:
785 case XPT_IMMEDIATE_NOTIFY:
786 case XPT_ACCEPT_TARGET_IO:
787 case XPT_CONT_TARGET_IO:
788 TAILQ_INSERT_TAIL(&softc->user_ccb_queue, &done_ccb->ccb_h,
790 cam_periph_unlock(softc->periph);
792 cam_periph_lock(softc->periph);
795 panic("targdone: impossible xpt opcode %#x",
796 done_ccb->ccb_h.func_code);
801 /* Return CCBs to the user from the user queue and abort queue */
803 targread(struct cdev *dev, struct uio *uio, int ioflag)
805 struct descr_queue *abort_queue;
806 struct targ_cmd_descr *user_descr;
807 struct targ_softc *softc;
808 struct ccb_queue *user_queue;
809 struct ccb_hdr *ccb_h;
815 devfs_get_cdevpriv((void **)&softc);
816 user_queue = &softc->user_ccb_queue;
817 abort_queue = &softc->abort_queue;
818 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targread\n"));
820 /* If no data is available, wait or return immediately */
821 cam_periph_lock(softc->periph);
822 ccb_h = TAILQ_FIRST(user_queue);
823 user_descr = TAILQ_FIRST(abort_queue);
824 while (ccb_h == NULL && user_descr == NULL) {
825 if ((ioflag & IO_NDELAY) == 0) {
826 error = msleep(user_queue, softc->periph->sim->mtx,
827 PRIBIO | PCATCH, "targrd", 0);
828 ccb_h = TAILQ_FIRST(user_queue);
829 user_descr = TAILQ_FIRST(abort_queue);
831 if (error == ERESTART) {
838 cam_periph_unlock(softc->periph);
843 /* Data is available so fill the user's buffer */
844 while (ccb_h != NULL) {
845 struct targ_cmd_descr *descr;
847 if (uio->uio_resid < sizeof(user_ccb))
849 TAILQ_REMOVE(user_queue, ccb_h, periph_links.tqe);
850 descr = (struct targ_cmd_descr *)ccb_h->targ_descr;
851 user_ccb = descr->user_ccb;
852 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
853 ("targread ccb %p (%p)\n", ccb_h, user_ccb));
854 error = targreturnccb(softc, (union ccb *)ccb_h);
857 cam_periph_unlock(softc->periph);
858 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
859 cam_periph_lock(softc->periph);
862 read_len += sizeof(user_ccb);
864 ccb_h = TAILQ_FIRST(user_queue);
867 /* Flush out any aborted descriptors */
868 while (user_descr != NULL) {
869 if (uio->uio_resid < sizeof(user_ccb))
871 TAILQ_REMOVE(abort_queue, user_descr, tqe);
872 user_ccb = user_descr->user_ccb;
873 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
874 ("targread aborted descr %p (%p)\n",
875 user_descr, user_ccb));
876 suword(&user_ccb->ccb_h.status, CAM_REQ_ABORTED);
877 cam_periph_unlock(softc->periph);
878 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
879 cam_periph_lock(softc->periph);
882 read_len += sizeof(user_ccb);
884 user_descr = TAILQ_FIRST(abort_queue);
888 * If we've successfully read some amount of data, don't report an
889 * error. If the error is persistent, it will be reported on the
892 if (read_len == 0 && uio->uio_resid != 0)
896 cam_periph_unlock(softc->periph);
900 /* Copy completed ccb back to the user */
902 targreturnccb(struct targ_softc *softc, union ccb *ccb)
904 struct targ_cmd_descr *descr;
905 struct ccb_hdr *u_ccbh;
909 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targreturnccb %p\n", ccb));
910 descr = (struct targ_cmd_descr *)ccb->ccb_h.targ_descr;
911 u_ccbh = &descr->user_ccb->ccb_h;
913 /* Copy out the central portion of the ccb_hdr */
914 copyout(&ccb->ccb_h.retry_count, &u_ccbh->retry_count,
915 offsetof(struct ccb_hdr, periph_priv) -
916 offsetof(struct ccb_hdr, retry_count));
918 /* Copy out the rest of the ccb (after the ccb_hdr) */
919 ccb_len = targccblen(ccb->ccb_h.func_code) - sizeof(struct ccb_hdr);
920 if (descr->mapinfo.num_bufs_used != 0)
921 cam_periph_unmapmem(ccb, &descr->mapinfo);
922 error = copyout(&ccb->ccb_h + 1, u_ccbh + 1, ccb_len);
924 xpt_print(softc->path,
925 "targreturnccb - CCB copyout failed (%d)\n", error);
927 /* Free CCB or send back to devq. */
928 targfreeccb(softc, ccb);
934 targgetccb(struct targ_softc *softc, xpt_opcode type, int priority)
939 ccb_len = targccblen(type);
940 ccb = malloc(ccb_len, M_TARG, M_NOWAIT);
941 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("getccb %p\n", ccb));
945 xpt_setup_ccb(&ccb->ccb_h, softc->path, priority);
946 ccb->ccb_h.func_code = type;
947 ccb->ccb_h.cbfcnp = targdone;
948 ccb->ccb_h.targ_descr = targgetdescr(softc);
949 if (ccb->ccb_h.targ_descr == NULL) {
957 targfreeccb(struct targ_softc *softc, union ccb *ccb)
959 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, ("targfreeccb descr %p and\n",
960 ccb->ccb_h.targ_descr));
961 free(ccb->ccb_h.targ_descr, M_TARG);
963 switch (ccb->ccb_h.func_code) {
964 case XPT_ACCEPT_TARGET_IO:
965 case XPT_IMMED_NOTIFY:
966 case XPT_IMMEDIATE_NOTIFY:
967 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, ("freeing ccb %p\n", ccb));
971 /* Send back CCB if we got it from the periph */
972 if (XPT_FC_IS_QUEUED(ccb)) {
973 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH,
974 ("returning queued ccb %p\n", ccb));
975 xpt_release_ccb(ccb);
977 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH,
978 ("freeing ccb %p\n", ccb));
985 static struct targ_cmd_descr *
986 targgetdescr(struct targ_softc *softc)
988 struct targ_cmd_descr *descr;
990 descr = malloc(sizeof(*descr), M_TARG,
993 descr->mapinfo.num_bufs_used = 0;
1003 /* Add symbolic link to targ0 for compatibility. */
1004 dev = make_dev(&targ_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "targ");
1005 make_dev_alias(dev, "targ0");
1009 targasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
1011 /* All events are handled in usermode by INOTs */
1012 panic("targasync() called, should be an INOT instead");
1015 /* Cancel all pending requests and CCBs awaiting work. */
1017 abort_all_pending(struct targ_softc *softc)
1019 struct targ_cmd_descr *descr;
1020 struct ccb_abort cab;
1021 struct ccb_hdr *ccb_h;
1022 struct cam_sim *sim;
1024 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("abort_all_pending\n"));
1026 /* First abort the descriptors awaiting resources */
1027 while ((descr = TAILQ_FIRST(&softc->work_queue)) != NULL) {
1028 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
1029 ("Aborting descr from workq %p\n", descr));
1030 TAILQ_REMOVE(&softc->work_queue, descr, tqe);
1031 TAILQ_INSERT_TAIL(&softc->abort_queue, descr, tqe);
1035 * Then abort all pending CCBs.
1036 * targdone() will return the aborted CCB via user_ccb_queue
1038 xpt_setup_ccb(&cab.ccb_h, softc->path, CAM_PRIORITY_NORMAL);
1039 cab.ccb_h.func_code = XPT_ABORT;
1040 cab.ccb_h.status = CAM_REQ_CMP_ERR;
1041 TAILQ_FOREACH(ccb_h, &softc->pending_ccb_queue, periph_links.tqe) {
1042 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
1043 ("Aborting pending CCB %p\n", ccb_h));
1044 cab.abort_ccb = (union ccb *)ccb_h;
1045 xpt_action((union ccb *)&cab);
1046 if (cab.ccb_h.status != CAM_REQ_CMP) {
1047 xpt_print(cab.ccb_h.path,
1048 "Unable to abort CCB, status %#x\n",
1053 /* If we aborted at least one pending CCB ok, wait for it. */
1054 if (cab.ccb_h.status == CAM_REQ_CMP) {
1055 sim = xpt_path_sim(softc->path);
1056 msleep(&softc->pending_ccb_queue, sim->mtx,
1057 PRIBIO | PCATCH, "tgabrt", 0);
1060 /* If we aborted anything from the work queue, wakeup user. */
1061 if (!TAILQ_EMPTY(&softc->user_ccb_queue)
1062 || !TAILQ_EMPTY(&softc->abort_queue)) {
1063 cam_periph_unlock(softc->periph);
1065 cam_periph_lock(softc->periph);
1069 /* Notify the user that data is ready */
1071 notify_user(struct targ_softc *softc)
1074 * Notify users sleeping via poll(), kqueue(), and
1077 selwakeuppri(&softc->read_select, PRIBIO);
1078 KNOTE_UNLOCKED(&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 subsystem 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);
1137 case XPT_IMMEDIATE_NOTIFY:
1138 len = sizeof(struct ccb_immediate_notify);
1141 len = sizeof(struct ccb_relsim);
1144 len = sizeof(struct ccb_pathinq);
1147 len = sizeof(struct ccb_debug);
1150 len = sizeof(struct ccb_abort);
1153 len = sizeof(struct ccb_en_lun);
1156 len = sizeof(union ccb);