2 * Generic SCSI Target Kernel Mode Driver
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 2002 Nate Lawson.
7 * Copyright (c) 1998, 1999, 2001, 2002 Justin T. Gibbs.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification, immediately at the beginning of the file.
16 * 2. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/malloc.h>
42 #include <sys/vnode.h>
44 #include <sys/mutex.h>
45 #include <sys/devicestat.h>
47 /* Includes to support callout */
48 #include <sys/types.h>
49 #include <sys/systm.h>
52 #include <cam/cam_ccb.h>
53 #include <cam/cam_periph.h>
54 #include <cam/cam_xpt_periph.h>
55 #include <cam/cam_sim.h>
56 #include <cam/scsi/scsi_targetio.h>
59 /* Transaction information attached to each CCB sent by the user */
60 struct targ_cmd_descr {
61 struct cam_periph_map_info mapinfo;
62 TAILQ_ENTRY(targ_cmd_descr) tqe;
68 /* Offset into the private CCB area for storing our descriptor */
69 #define targ_descr periph_priv.entries[1].ptr
71 TAILQ_HEAD(descr_queue, targ_cmd_descr);
74 TARG_STATE_RESV = 0x00, /* Invalid state */
75 TARG_STATE_OPENED = 0x01, /* Device opened, softc initialized */
76 TARG_STATE_LUN_ENABLED = 0x02 /* Device enabled for a path */
79 /* Per-instance device software context */
81 /* CCBs (CTIOs, ATIOs, INOTs) pending on the controller */
82 struct ccb_queue pending_ccb_queue;
84 /* Command descriptors awaiting CTIO resources from the XPT */
85 struct descr_queue work_queue;
87 /* Command descriptors that have been aborted back to the user. */
88 struct descr_queue abort_queue;
91 * Queue of CCBs that have been copied out to userland, but our
92 * userland daemon has not yet seen.
94 struct ccb_queue user_ccb_queue;
96 struct cam_periph *periph;
97 struct cam_path *path;
100 struct selinfo read_select;
101 struct devstat device_stats;
104 static d_open_t targopen;
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 = {
114 .f_detach = targreadfiltdetach,
115 .f_event = targreadfilt,
118 static struct cdevsw targ_cdevsw = {
119 .d_version = D_VERSION,
120 .d_flags = D_NEEDGIANT,
123 .d_write = targwrite,
124 .d_ioctl = targioctl,
127 .d_kqfilter = targkqfilter
130 static cam_status targendislun(struct cam_path *path, int enable,
131 int grp6_len, int grp7_len);
132 static cam_status targenable(struct targ_softc *softc,
133 struct cam_path *path,
134 int grp6_len, int grp7_len);
135 static cam_status targdisable(struct targ_softc *softc);
136 static periph_ctor_t targctor;
137 static periph_dtor_t targdtor;
138 static periph_start_t targstart;
139 static int targusermerge(struct targ_softc *softc,
140 struct targ_cmd_descr *descr,
142 static int targsendccb(struct targ_softc *softc, union ccb *ccb,
143 struct targ_cmd_descr *descr);
144 static void targdone(struct cam_periph *periph,
145 union ccb *done_ccb);
146 static int targreturnccb(struct targ_softc *softc,
148 static union ccb * targgetccb(struct targ_softc *softc, xpt_opcode type,
150 static void targfreeccb(struct targ_softc *softc, union ccb *ccb);
151 static struct targ_cmd_descr *
152 targgetdescr(struct targ_softc *softc);
153 static periph_init_t targinit;
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);
161 static struct periph_driver targdriver =
164 TAILQ_HEAD_INITIALIZER(targdriver.units), /* generation */ 0
166 PERIPHDRIVER_DECLARE(targ, targdriver);
168 static MALLOC_DEFINE(M_TARG, "TARG", "TARG data");
170 /* Disable LUN if enabled and teardown softc */
172 targcdevdtor(void *data)
174 struct targ_softc *softc;
175 struct cam_periph *periph;
178 if (softc->periph == NULL) {
179 printf("%s: destroying non-enabled target\n", __func__);
185 * Acquire a hold on the periph so that it doesn't go away before
186 * we are ready at the end of the function.
188 periph = softc->periph;
189 cam_periph_acquire(periph);
190 cam_periph_lock(periph);
191 (void)targdisable(softc);
192 if (softc->periph != NULL) {
193 cam_periph_invalidate(softc->periph);
194 softc->periph = NULL;
196 cam_periph_unlock(periph);
197 cam_periph_release(periph);
202 * Create softc and initialize it. There is no locking here because a
203 * periph doesn't get created until an ioctl is issued to do so, and
204 * that can't happen until this method returns.
207 targopen(struct cdev *dev, int flags, int fmt, struct thread *td)
209 struct targ_softc *softc;
211 /* Allocate its softc, initialize it */
212 softc = malloc(sizeof(*softc), M_TARG,
214 softc->state = TARG_STATE_OPENED;
215 softc->periph = NULL;
218 TAILQ_INIT(&softc->pending_ccb_queue);
219 TAILQ_INIT(&softc->work_queue);
220 TAILQ_INIT(&softc->abort_queue);
221 TAILQ_INIT(&softc->user_ccb_queue);
222 knlist_init_mtx(&softc->read_select.si_note, NULL);
224 devfs_set_cdevpriv(softc, targcdevdtor);
228 /* Enable/disable LUNs, set debugging level */
230 targioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
232 struct targ_softc *softc;
235 devfs_get_cdevpriv((void **)&softc);
240 struct ioc_enable_lun *new_lun;
241 struct cam_path *path;
243 new_lun = (struct ioc_enable_lun *)addr;
244 status = xpt_create_path(&path, /*periph*/NULL,
248 if (status != CAM_REQ_CMP) {
249 printf("Couldn't create path, status %#x\n", status);
253 status = targenable(softc, path, new_lun->grp6_len,
255 xpt_path_unlock(path);
260 if (softc->periph == NULL) {
261 status = CAM_DEV_NOT_THERE;
264 cam_periph_lock(softc->periph);
265 status = targdisable(softc);
266 cam_periph_unlock(softc->periph);
270 struct ccb_debug cdbg;
272 /* If no periph available, disallow debugging changes */
273 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) {
274 status = CAM_DEV_NOT_THERE;
277 bzero(&cdbg, sizeof cdbg);
278 if (*((int *)addr) != 0)
279 cdbg.flags = CAM_DEBUG_PERIPH;
281 cdbg.flags = CAM_DEBUG_NONE;
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;
285 xpt_action((union ccb *)&cdbg);
286 status = cdbg.ccb_h.status & CAM_STATUS_MASK;
290 status = CAM_PROVIDE_FAIL;
294 return (targcamstatus(status));
297 /* Writes are always ready, reads wait for user_ccb_queue or abort_queue */
299 targpoll(struct cdev *dev, int poll_events, struct thread *td)
301 struct targ_softc *softc;
304 devfs_get_cdevpriv((void **)&softc);
306 /* Poll for write() is always ok. */
307 revents = poll_events & (POLLOUT | POLLWRNORM);
308 if ((poll_events & (POLLIN | POLLRDNORM)) != 0) {
309 /* Poll for read() depends on user and abort queues. */
310 cam_periph_lock(softc->periph);
311 if (!TAILQ_EMPTY(&softc->user_ccb_queue) ||
312 !TAILQ_EMPTY(&softc->abort_queue)) {
313 revents |= poll_events & (POLLIN | POLLRDNORM);
315 cam_periph_unlock(softc->periph);
316 /* Only sleep if the user didn't poll for write. */
318 selrecord(td, &softc->read_select);
325 targkqfilter(struct cdev *dev, struct knote *kn)
327 struct targ_softc *softc;
329 devfs_get_cdevpriv((void **)&softc);
330 kn->kn_hook = (caddr_t)softc;
331 kn->kn_fop = &targread_filtops;
332 knlist_add(&softc->read_select.si_note, kn, 0);
337 targreadfiltdetach(struct knote *kn)
339 struct targ_softc *softc;
341 softc = (struct targ_softc *)kn->kn_hook;
342 knlist_remove(&softc->read_select.si_note, kn, 0);
345 /* Notify the user's kqueue when the user queue or abort queue gets a CCB */
347 targreadfilt(struct knote *kn, long hint)
349 struct targ_softc *softc;
352 softc = (struct targ_softc *)kn->kn_hook;
353 cam_periph_lock(softc->periph);
354 retval = !TAILQ_EMPTY(&softc->user_ccb_queue) ||
355 !TAILQ_EMPTY(&softc->abort_queue);
356 cam_periph_unlock(softc->periph);
360 /* Send the HBA the enable/disable message */
362 targendislun(struct cam_path *path, int enable, int grp6_len, int grp7_len)
364 struct ccb_en_lun en_ccb;
367 /* Tell the lun to begin answering selects */
368 xpt_setup_ccb(&en_ccb.ccb_h, path, CAM_PRIORITY_NORMAL);
369 en_ccb.ccb_h.func_code = XPT_EN_LUN;
370 /* Don't need support for any vendor specific commands */
371 en_ccb.grp6_len = grp6_len;
372 en_ccb.grp7_len = grp7_len;
373 en_ccb.enable = enable ? 1 : 0;
374 xpt_action((union ccb *)&en_ccb);
375 status = en_ccb.ccb_h.status & CAM_STATUS_MASK;
376 if (status != CAM_REQ_CMP) {
377 xpt_print(path, "%sable lun CCB rejected, status %#x\n",
378 enable ? "en" : "dis", status);
383 /* Enable target mode on a LUN, given its path */
385 targenable(struct targ_softc *softc, struct cam_path *path, int grp6_len,
388 struct cam_periph *periph;
389 struct ccb_pathinq cpi;
392 if ((softc->state & TARG_STATE_LUN_ENABLED) != 0)
393 return (CAM_LUN_ALRDY_ENA);
395 /* Make sure SIM supports target mode */
396 xpt_path_inq(&cpi, path);
397 status = cpi.ccb_h.status & CAM_STATUS_MASK;
398 if (status != CAM_REQ_CMP) {
399 printf("pathinq failed, status %#x\n", status);
402 if ((cpi.target_sprt & PIT_PROCESSOR) == 0) {
403 printf("controller does not support target mode\n");
404 status = CAM_FUNC_NOTAVAIL;
408 softc->maxio = DFLTPHYS; /* traditional default */
409 else if (cpi.maxio > MAXPHYS)
410 softc->maxio = MAXPHYS; /* for safety */
412 softc->maxio = cpi.maxio; /* real value */
414 /* Destroy any periph on our path if it is disabled */
415 periph = cam_periph_find(path, "targ");
416 if (periph != NULL) {
417 struct targ_softc *del_softc;
419 del_softc = (struct targ_softc *)periph->softc;
420 if ((del_softc->state & TARG_STATE_LUN_ENABLED) == 0) {
421 cam_periph_invalidate(del_softc->periph);
422 del_softc->periph = NULL;
424 printf("Requested path still in use by targ%d\n",
425 periph->unit_number);
426 status = CAM_LUN_ALRDY_ENA;
431 /* Create a periph instance attached to this path */
432 status = cam_periph_alloc(targctor, NULL, targdtor, targstart,
433 "targ", CAM_PERIPH_BIO, path, targasync, 0, softc);
434 if (status != CAM_REQ_CMP) {
435 printf("cam_periph_alloc failed, status %#x\n", status);
439 /* Ensure that the periph now exists. */
440 if (cam_periph_find(path, "targ") == NULL) {
441 panic("targenable: succeeded but no periph?");
445 /* Send the enable lun message */
446 status = targendislun(path, /*enable*/1, grp6_len, grp7_len);
447 if (status != CAM_REQ_CMP) {
448 printf("enable lun failed, status %#x\n", status);
451 softc->state |= TARG_STATE_LUN_ENABLED;
457 /* Disable this softc's target instance if enabled */
459 targdisable(struct targ_softc *softc)
463 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0)
464 return (CAM_REQ_CMP);
466 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targdisable\n"));
468 /* Abort any ccbs pending on the controller */
469 abort_all_pending(softc);
471 /* Disable this lun */
472 status = targendislun(softc->path, /*enable*/0,
473 /*grp6_len*/0, /*grp7_len*/0);
474 if (status == CAM_REQ_CMP)
475 softc->state &= ~TARG_STATE_LUN_ENABLED;
477 printf("Disable lun failed, status %#x\n", status);
482 /* Initialize a periph (called from cam_periph_alloc) */
484 targctor(struct cam_periph *periph, void *arg)
486 struct targ_softc *softc;
488 /* Store pointer to softc for periph-driven routines */
489 softc = (struct targ_softc *)arg;
490 periph->softc = softc;
491 softc->periph = periph;
492 softc->path = periph->path;
493 return (CAM_REQ_CMP);
497 targdtor(struct cam_periph *periph)
499 struct targ_softc *softc;
500 struct ccb_hdr *ccb_h;
501 struct targ_cmd_descr *descr;
503 softc = (struct targ_softc *)periph->softc;
506 * targdisable() aborts CCBs back to the user and leaves them
507 * on user_ccb_queue and abort_queue in case the user is still
508 * interested in them. We free them now.
510 while ((ccb_h = TAILQ_FIRST(&softc->user_ccb_queue)) != NULL) {
511 TAILQ_REMOVE(&softc->user_ccb_queue, ccb_h, periph_links.tqe);
512 targfreeccb(softc, (union ccb *)ccb_h);
514 while ((descr = TAILQ_FIRST(&softc->abort_queue)) != NULL) {
515 TAILQ_REMOVE(&softc->abort_queue, descr, tqe);
519 softc->periph = NULL;
521 periph->softc = NULL;
524 /* Receive CCBs from user mode proc and send them to the HBA */
526 targwrite(struct cdev *dev, struct uio *uio, int ioflag)
529 struct targ_softc *softc;
530 struct targ_cmd_descr *descr;
531 int write_len, error;
532 int func_code, priority;
534 devfs_get_cdevpriv((void **)&softc);
535 write_len = error = 0;
536 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
537 ("write - uio_resid %zd\n", uio->uio_resid));
538 while (uio->uio_resid >= sizeof(user_ccb) && error == 0) {
541 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
543 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
544 ("write - uiomove failed (%d)\n", error));
547 priority = fuword32(&user_ccb->ccb_h.pinfo.priority);
548 if (priority == CAM_PRIORITY_NONE) {
552 func_code = fuword32(&user_ccb->ccb_h.func_code);
554 case XPT_ACCEPT_TARGET_IO:
555 case XPT_IMMED_NOTIFY:
556 case XPT_IMMEDIATE_NOTIFY:
557 cam_periph_lock(softc->periph);
558 ccb = targgetccb(softc, func_code, priority);
559 descr = (struct targ_cmd_descr *)ccb->ccb_h.targ_descr;
560 descr->user_ccb = user_ccb;
561 descr->func_code = func_code;
562 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
563 ("Sent ATIO/INOT (%p)\n", user_ccb));
565 TAILQ_INSERT_TAIL(&softc->pending_ccb_queue,
568 cam_periph_unlock(softc->periph);
571 cam_periph_lock(softc->periph);
572 if ((func_code & XPT_FC_QUEUED) != 0) {
573 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
574 ("Sending queued ccb %#x (%p)\n",
575 func_code, user_ccb));
576 descr = targgetdescr(softc);
577 descr->user_ccb = user_ccb;
578 descr->priority = priority;
579 descr->func_code = func_code;
580 TAILQ_INSERT_TAIL(&softc->work_queue,
582 xpt_schedule(softc->periph, priority);
584 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
585 ("Sending inline ccb %#x (%p)\n",
586 func_code, user_ccb));
587 ccb = targgetccb(softc, func_code, priority);
588 descr = (struct targ_cmd_descr *)
589 ccb->ccb_h.targ_descr;
590 descr->user_ccb = user_ccb;
591 descr->priority = priority;
592 descr->func_code = func_code;
593 if (targusermerge(softc, descr, ccb) != EFAULT)
594 targsendccb(softc, ccb, descr);
595 targreturnccb(softc, ccb);
597 cam_periph_unlock(softc->periph);
600 write_len += sizeof(user_ccb);
604 * If we've successfully taken in some amount of
605 * data, return success for that data first. If
606 * an error is persistent, it will be reported
609 if (error != 0 && write_len == 0)
611 if (write_len == 0 && uio->uio_resid != 0)
616 /* Process requests (descrs) via the periph-supplied CCBs */
618 targstart(struct cam_periph *periph, union ccb *start_ccb)
620 struct targ_softc *softc;
621 struct targ_cmd_descr *descr, *next_descr;
624 softc = (struct targ_softc *)periph->softc;
625 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targstart %p\n", start_ccb));
627 descr = TAILQ_FIRST(&softc->work_queue);
629 xpt_release_ccb(start_ccb);
631 TAILQ_REMOVE(&softc->work_queue, descr, tqe);
632 next_descr = TAILQ_FIRST(&softc->work_queue);
634 /* Initiate a transaction using the descr and supplied CCB */
635 error = targusermerge(softc, descr, start_ccb);
637 error = targsendccb(softc, start_ccb, descr);
639 xpt_print(periph->path,
640 "targsendccb failed, err %d\n", error);
641 xpt_release_ccb(start_ccb);
642 suword(&descr->user_ccb->ccb_h.status,
644 TAILQ_INSERT_TAIL(&softc->abort_queue, descr, tqe);
648 /* If we have more work to do, stay scheduled */
649 if (next_descr != NULL)
650 xpt_schedule(periph, next_descr->priority);
655 targusermerge(struct targ_softc *softc, struct targ_cmd_descr *descr,
658 struct ccb_hdr *u_ccbh, *k_ccbh;
662 u_ccbh = &descr->user_ccb->ccb_h;
663 k_ccbh = &ccb->ccb_h;
666 * There are some fields in the CCB header that need to be
667 * preserved, the rest we get from the user ccb. (See xpt_merge_ccb)
669 xpt_setup_ccb(k_ccbh, softc->path, descr->priority);
670 k_ccbh->retry_count = fuword32(&u_ccbh->retry_count);
671 k_ccbh->func_code = descr->func_code;
672 k_ccbh->flags = fuword32(&u_ccbh->flags);
673 k_ccbh->timeout = fuword32(&u_ccbh->timeout);
674 ccb_len = targccblen(k_ccbh->func_code) - sizeof(struct ccb_hdr);
675 error = copyin(u_ccbh + 1, k_ccbh + 1, ccb_len);
677 k_ccbh->status = CAM_REQ_CMP_ERR;
681 /* Translate usermode abort_ccb pointer to its kernel counterpart */
682 if (k_ccbh->func_code == XPT_ABORT) {
683 struct ccb_abort *cab;
684 struct ccb_hdr *ccb_h;
686 cab = (struct ccb_abort *)ccb;
687 TAILQ_FOREACH(ccb_h, &softc->pending_ccb_queue,
689 struct targ_cmd_descr *ab_descr;
691 ab_descr = (struct targ_cmd_descr *)ccb_h->targ_descr;
692 if (ab_descr->user_ccb == cab->abort_ccb) {
693 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
694 ("Changing abort for %p to %p\n",
695 cab->abort_ccb, ccb_h));
696 cab->abort_ccb = (union ccb *)ccb_h;
700 /* CCB not found, set appropriate status */
702 k_ccbh->status = CAM_PATH_INVALID;
710 /* Build and send a kernel CCB formed from descr->user_ccb */
712 targsendccb(struct targ_softc *softc, union ccb *ccb,
713 struct targ_cmd_descr *descr)
715 struct cam_periph_map_info *mapinfo;
716 struct ccb_hdr *ccb_h;
720 mapinfo = &descr->mapinfo;
721 mapinfo->num_bufs_used = 0;
724 * There's no way for the user to have a completion
725 * function, so we put our own completion function in here.
726 * We also stash in a reference to our descriptor so targreturnccb()
727 * can find our mapping info.
729 ccb_h->cbfcnp = targdone;
730 ccb_h->targ_descr = descr;
732 if ((ccb_h->func_code == XPT_CONT_TARGET_IO) ||
733 (ccb_h->func_code == XPT_DEV_MATCH)) {
735 error = cam_periph_mapmem(ccb, mapinfo, softc->maxio);
738 * cam_periph_mapmem returned an error, we can't continue.
739 * Return the error to the user.
742 ccb_h->status = CAM_REQ_CMP_ERR;
743 mapinfo->num_bufs_used = 0;
749 * Once queued on the pending CCB list, this CCB will be protected
750 * by our error recovery handler.
752 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("sendccb %p\n", ccb));
753 if (XPT_FC_IS_QUEUED(ccb)) {
754 TAILQ_INSERT_TAIL(&softc->pending_ccb_queue, ccb_h,
762 /* Completion routine for CCBs (called at splsoftcam) */
764 targdone(struct cam_periph *periph, union ccb *done_ccb)
766 struct targ_softc *softc;
769 CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("targdone %p\n", done_ccb));
770 softc = (struct targ_softc *)periph->softc;
771 TAILQ_REMOVE(&softc->pending_ccb_queue, &done_ccb->ccb_h,
773 status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
775 /* If we're no longer enabled, throw away CCB */
776 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) {
777 targfreeccb(softc, done_ccb);
780 /* abort_all_pending() waits for pending queue to be empty */
781 if (TAILQ_EMPTY(&softc->pending_ccb_queue))
782 wakeup(&softc->pending_ccb_queue);
784 switch (done_ccb->ccb_h.func_code) {
785 /* All FC_*_QUEUED CCBs go back to userland */
786 case XPT_IMMED_NOTIFY:
787 case XPT_IMMEDIATE_NOTIFY:
788 case XPT_ACCEPT_TARGET_IO:
789 case XPT_CONT_TARGET_IO:
790 TAILQ_INSERT_TAIL(&softc->user_ccb_queue, &done_ccb->ccb_h,
792 cam_periph_unlock(softc->periph);
794 cam_periph_lock(softc->periph);
797 panic("targdone: impossible xpt opcode %#x",
798 done_ccb->ccb_h.func_code);
803 /* Return CCBs to the user from the user queue and abort queue */
805 targread(struct cdev *dev, struct uio *uio, int ioflag)
807 struct descr_queue *abort_queue;
808 struct targ_cmd_descr *user_descr;
809 struct targ_softc *softc;
810 struct ccb_queue *user_queue;
811 struct ccb_hdr *ccb_h;
817 devfs_get_cdevpriv((void **)&softc);
818 user_queue = &softc->user_ccb_queue;
819 abort_queue = &softc->abort_queue;
820 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targread\n"));
822 /* If no data is available, wait or return immediately */
823 cam_periph_lock(softc->periph);
824 ccb_h = TAILQ_FIRST(user_queue);
825 user_descr = TAILQ_FIRST(abort_queue);
826 while (ccb_h == NULL && user_descr == NULL) {
827 if ((ioflag & IO_NDELAY) == 0) {
828 error = cam_periph_sleep(softc->periph, user_queue,
829 PRIBIO | PCATCH, "targrd", 0);
830 ccb_h = TAILQ_FIRST(user_queue);
831 user_descr = TAILQ_FIRST(abort_queue);
833 if (error == ERESTART) {
840 cam_periph_unlock(softc->periph);
845 /* Data is available so fill the user's buffer */
846 while (ccb_h != NULL) {
847 struct targ_cmd_descr *descr;
849 if (uio->uio_resid < sizeof(user_ccb))
851 TAILQ_REMOVE(user_queue, ccb_h, periph_links.tqe);
852 descr = (struct targ_cmd_descr *)ccb_h->targ_descr;
853 user_ccb = descr->user_ccb;
854 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
855 ("targread ccb %p (%p)\n", ccb_h, user_ccb));
856 error = targreturnccb(softc, (union ccb *)ccb_h);
859 cam_periph_unlock(softc->periph);
860 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
861 cam_periph_lock(softc->periph);
864 read_len += sizeof(user_ccb);
866 ccb_h = TAILQ_FIRST(user_queue);
869 /* Flush out any aborted descriptors */
870 while (user_descr != NULL) {
871 if (uio->uio_resid < sizeof(user_ccb))
873 TAILQ_REMOVE(abort_queue, user_descr, tqe);
874 user_ccb = user_descr->user_ccb;
875 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
876 ("targread aborted descr %p (%p)\n",
877 user_descr, user_ccb));
878 suword(&user_ccb->ccb_h.status, CAM_REQ_ABORTED);
879 cam_periph_unlock(softc->periph);
880 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
881 cam_periph_lock(softc->periph);
884 read_len += sizeof(user_ccb);
886 user_descr = TAILQ_FIRST(abort_queue);
890 * If we've successfully read some amount of data, don't report an
891 * error. If the error is persistent, it will be reported on the
894 if (read_len == 0 && uio->uio_resid != 0)
898 cam_periph_unlock(softc->periph);
902 /* Copy completed ccb back to the user */
904 targreturnccb(struct targ_softc *softc, union ccb *ccb)
906 struct targ_cmd_descr *descr;
907 struct ccb_hdr *u_ccbh;
911 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targreturnccb %p\n", ccb));
912 descr = (struct targ_cmd_descr *)ccb->ccb_h.targ_descr;
913 u_ccbh = &descr->user_ccb->ccb_h;
915 /* Copy out the central portion of the ccb_hdr */
916 copyout(&ccb->ccb_h.retry_count, &u_ccbh->retry_count,
917 offsetof(struct ccb_hdr, periph_priv) -
918 offsetof(struct ccb_hdr, retry_count));
920 /* Copy out the rest of the ccb (after the ccb_hdr) */
921 ccb_len = targccblen(ccb->ccb_h.func_code) - sizeof(struct ccb_hdr);
922 if (descr->mapinfo.num_bufs_used != 0)
923 cam_periph_unmapmem(ccb, &descr->mapinfo);
924 error = copyout(&ccb->ccb_h + 1, u_ccbh + 1, ccb_len);
926 xpt_print(softc->path,
927 "targreturnccb - CCB copyout failed (%d)\n", error);
929 /* Free CCB or send back to devq. */
930 targfreeccb(softc, ccb);
936 targgetccb(struct targ_softc *softc, xpt_opcode type, int priority)
941 ccb_len = targccblen(type);
942 ccb = malloc(ccb_len, M_TARG, M_NOWAIT);
943 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("getccb %p\n", ccb));
947 xpt_setup_ccb(&ccb->ccb_h, softc->path, priority);
948 ccb->ccb_h.func_code = type;
949 ccb->ccb_h.cbfcnp = targdone;
950 ccb->ccb_h.targ_descr = targgetdescr(softc);
951 if (ccb->ccb_h.targ_descr == NULL) {
959 targfreeccb(struct targ_softc *softc, union ccb *ccb)
961 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, ("targfreeccb descr %p and\n",
962 ccb->ccb_h.targ_descr));
963 free(ccb->ccb_h.targ_descr, M_TARG);
965 switch (ccb->ccb_h.func_code) {
966 case XPT_ACCEPT_TARGET_IO:
967 case XPT_IMMED_NOTIFY:
968 case XPT_IMMEDIATE_NOTIFY:
969 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, ("freeing ccb %p\n", ccb));
973 /* Send back CCB if we got it from the periph */
974 if (XPT_FC_IS_QUEUED(ccb)) {
975 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH,
976 ("returning queued ccb %p\n", ccb));
977 xpt_release_ccb(ccb);
979 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH,
980 ("freeing ccb %p\n", ccb));
987 static struct targ_cmd_descr *
988 targgetdescr(struct targ_softc *softc)
990 struct targ_cmd_descr *descr;
992 descr = malloc(sizeof(*descr), M_TARG,
995 descr->mapinfo.num_bufs_used = 0;
1005 /* Add symbolic link to targ0 for compatibility. */
1006 dev = make_dev(&targ_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "targ");
1007 make_dev_alias(dev, "targ0");
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, CAM_PRIORITY_NORMAL);
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(cab.ccb_h.path,
1049 "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 cam_periph_sleep(softc->periph, &softc->pending_ccb_queue,
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);