]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/cam/scsi/scsi_target.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / cam / scsi / scsi_target.c
1 /*-
2  * Generic SCSI Target Kernel Mode Driver
3  *
4  * Copyright (c) 2002 Nate Lawson.
5  * Copyright (c) 1998, 1999, 2001, 2002 Justin T. Gibbs.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/conf.h>
38 #include <sys/malloc.h>
39 #include <sys/poll.h>
40 #include <sys/vnode.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/devicestat.h>
44 #include <sys/proc.h>
45 /* Includes to support callout */
46 #include <sys/types.h>
47 #include <sys/systm.h>
48
49 #include <cam/cam.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>
55
56
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;
61         union ccb *user_ccb;
62         int        priority;
63         int        func_code;
64 };
65
66 /* Offset into the private CCB area for storing our descriptor */
67 #define targ_descr      periph_priv.entries[1].ptr
68
69 TAILQ_HEAD(descr_queue, targ_cmd_descr);
70
71 typedef enum {
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 */
75 } targ_state;
76
77 /* Per-instance device software context */
78 struct targ_softc {
79         /* CCBs (CTIOs, ATIOs, INOTs) pending on the controller */
80         struct ccb_queue         pending_ccb_queue;
81
82         /* Command descriptors awaiting CTIO resources from the XPT */
83         struct descr_queue       work_queue;
84
85         /* Command descriptors that have been aborted back to the user. */
86         struct descr_queue       abort_queue;
87
88         /*
89          * Queue of CCBs that have been copied out to userland, but our
90          * userland daemon has not yet seen.
91          */
92         struct ccb_queue         user_ccb_queue;
93
94         struct cam_periph       *periph;
95         struct cam_path         *path;
96         targ_state               state;
97         struct selinfo           read_select;
98         struct devstat           device_stats;
99         struct callout          destroy_dev_callout;
100         struct mtx              destroy_mtx;
101 };
102
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         .f_isfd = 1,
114         .f_detach = targreadfiltdetach,
115         .f_event = targreadfilt,
116 };
117
118 static struct cdevsw targ_cdevsw = {
119         .d_version =    D_VERSION,
120         .d_flags =      D_NEEDGIANT,
121         .d_open =       targopen,
122         .d_close =      targclose,
123         .d_read =       targread,
124         .d_write =      targwrite,
125         .d_ioctl =      targioctl,
126         .d_poll =       targpoll,
127         .d_name =       "targ",
128         .d_kqfilter =   targkqfilter
129 };
130
131 static cam_status       targendislun(struct cam_path *path, int enable,
132                                      int grp6_len, int grp7_len);
133 static cam_status       targenable(struct targ_softc *softc,
134                                    struct cam_path *path,
135                                    int grp6_len, int grp7_len);
136 static cam_status       targdisable(struct targ_softc *softc);
137 static periph_ctor_t    targctor;
138 static periph_dtor_t    targdtor;
139 static periph_start_t   targstart;
140 static int              targusermerge(struct targ_softc *softc,
141                                       struct targ_cmd_descr *descr,
142                                       union ccb *ccb);
143 static int              targsendccb(struct targ_softc *softc, union ccb *ccb,
144                                     struct targ_cmd_descr *descr);
145 static void             targdone(struct cam_periph *periph,
146                                  union  ccb *done_ccb);
147 static int              targreturnccb(struct targ_softc *softc,
148                                       union  ccb *ccb);
149 static union ccb *      targgetccb(struct targ_softc *softc, xpt_opcode type,
150                                    int priority);
151 static void             targfreeccb(struct targ_softc *softc, union ccb *ccb);
152 static struct targ_cmd_descr *
153                         targgetdescr(struct targ_softc *softc);
154 static periph_init_t    targinit;
155 static void             targclone(void *arg, struct ucred *cred, char *name,
156                                   int namelen, struct cdev **dev);
157 static void             targasync(void *callback_arg, u_int32_t code,
158                                   struct cam_path *path, void *arg);
159 static void             abort_all_pending(struct targ_softc *softc);
160 static void             notify_user(struct targ_softc *softc);
161 static int              targcamstatus(cam_status status);
162 static size_t           targccblen(xpt_opcode func_code);
163 static void             targdestroy(void *);
164
165 static struct periph_driver targdriver =
166 {
167         targinit, "targ",
168         TAILQ_HEAD_INITIALIZER(targdriver.units), /* generation */ 0
169 };
170 PERIPHDRIVER_DECLARE(targ, targdriver);
171
172 static MALLOC_DEFINE(M_TARG, "TARG", "TARG data");
173
174 /*
175  * Create softc and initialize it. Only one proc can open each targ device.
176  * There is no locking here because a periph doesn't get created until an
177  * ioctl is issued to do so, and that can't happen until this method returns.
178  */
179 static int
180 targopen(struct cdev *dev, int flags, int fmt, struct thread *td)
181 {
182         struct targ_softc *softc;
183
184         if (dev->si_drv1 != 0) {
185                 return (EBUSY);
186         }
187         
188         /* Mark device busy before any potentially blocking operations */
189         dev->si_drv1 = (void *)~0;
190
191         /* Create the targ device, allocate its softc, initialize it */
192         if ((dev->si_flags & SI_NAMED) == 0) {
193                 make_dev(&targ_cdevsw, dev2unit(dev), UID_ROOT, GID_WHEEL, 0600,
194                          "targ%d", dev2unit(dev));
195         }
196         softc = malloc(sizeof(*softc), M_TARG,
197                M_WAITOK | M_ZERO);
198         dev->si_drv1 = softc;
199         softc->state = TARG_STATE_OPENED;
200         softc->periph = NULL;
201         softc->path = NULL;
202
203         TAILQ_INIT(&softc->pending_ccb_queue);
204         TAILQ_INIT(&softc->work_queue);
205         TAILQ_INIT(&softc->abort_queue);
206         TAILQ_INIT(&softc->user_ccb_queue);
207         knlist_init_mtx(&softc->read_select.si_note, NULL);
208
209         return (0);
210 }
211
212 /* Disable LUN if enabled and teardown softc */
213 static int
214 targclose(struct cdev *dev, int flag, int fmt, struct thread *td)
215 {
216         struct targ_softc     *softc;
217         struct cam_periph     *periph;
218         int    error;
219
220         softc = (struct targ_softc *)dev->si_drv1;
221         mtx_init(&softc->destroy_mtx, "targ_destroy", "SCSI Target dev destroy", MTX_DEF);
222         callout_init_mtx(&softc->destroy_dev_callout, &softc->destroy_mtx, CALLOUT_RETURNUNLOCKED);
223         if (softc->periph == NULL) {
224 #if 0
225                 destroy_dev(dev);
226                 free(softc, M_TARG);
227 #endif
228                 printf("%s: destroying non-enabled target\n", __func__);
229                 mtx_lock(&softc->destroy_mtx);
230                 callout_reset(&softc->destroy_dev_callout, hz / 2,
231                         (void *)targdestroy, (void *)dev);
232                 mtx_unlock(&softc->destroy_mtx);
233                 return (0);
234         }
235
236         /*
237          * Acquire a hold on the periph so that it doesn't go away before
238          * we are ready at the end of the function.
239          */
240         periph = softc->periph;
241         cam_periph_acquire(periph);
242         cam_periph_lock(periph);
243         error = targdisable(softc);
244         if (softc->periph != NULL) {
245                 cam_periph_invalidate(softc->periph);
246                 softc->periph = NULL;
247         }
248         cam_periph_unlock(periph);
249         cam_periph_release(periph);
250
251 #if 0
252         destroy_dev(dev);
253         free(softc, M_TARG);
254 #endif
255
256         printf("%s: close finished error(%d)\n", __func__, error);
257         mtx_lock(&softc->destroy_mtx);
258         callout_reset(&softc->destroy_dev_callout, hz / 2,
259                 (void *)targdestroy, (void *)dev);
260         mtx_unlock(&softc->destroy_mtx);
261         return (error);
262 }
263
264 /* Enable/disable LUNs, set debugging level */
265 static int
266 targioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
267 {
268         struct targ_softc *softc;
269         cam_status         status;
270
271         softc = (struct targ_softc *)dev->si_drv1;
272
273         switch (cmd) {
274         case TARGIOCENABLE:
275         {
276                 struct ioc_enable_lun   *new_lun;
277                 struct cam_path         *path;
278                 struct cam_sim          *sim;
279
280                 new_lun = (struct ioc_enable_lun *)addr;
281                 status = xpt_create_path_unlocked(&path, /*periph*/NULL,
282                                                   new_lun->path_id,
283                                                   new_lun->target_id,
284                                                   new_lun->lun_id);
285                 if (status != CAM_REQ_CMP) {
286                         printf("Couldn't create path, status %#x\n", status);
287                         break;
288                 }
289                 sim = xpt_path_sim(path);
290                 mtx_lock(sim->mtx);
291                 status = targenable(softc, path, new_lun->grp6_len,
292                                     new_lun->grp7_len);
293                 xpt_free_path(path);
294                 mtx_unlock(sim->mtx);
295                 break;
296         }
297         case TARGIOCDISABLE:
298                 if (softc->periph == NULL) {
299                         status = CAM_DEV_NOT_THERE;
300                         break;
301                 }
302                 cam_periph_lock(softc->periph);
303                 status = targdisable(softc);
304                 cam_periph_unlock(softc->periph);
305                 break;
306         case TARGIOCDEBUG:
307         {
308 #ifdef  CAMDEBUG
309                 struct ccb_debug cdbg;
310
311                 /* If no periph available, disallow debugging changes */
312                 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) {
313                         status = CAM_DEV_NOT_THERE;
314                         break;
315                 }
316                 bzero(&cdbg, sizeof cdbg);
317                 if (*((int *)addr) != 0)
318                         cdbg.flags = CAM_DEBUG_PERIPH;
319                 else
320                         cdbg.flags = CAM_DEBUG_NONE;
321                 cam_periph_lock(softc->periph);
322                 xpt_setup_ccb(&cdbg.ccb_h, softc->path, CAM_PRIORITY_NORMAL);
323                 cdbg.ccb_h.func_code = XPT_DEBUG;
324                 cdbg.ccb_h.cbfcnp = targdone;
325
326                 xpt_action((union ccb *)&cdbg);
327                 cam_periph_unlock(softc->periph);
328                 status = cdbg.ccb_h.status & CAM_STATUS_MASK;
329 #else
330                 status = CAM_FUNC_NOTAVAIL;
331 #endif
332                 break;
333         }
334         default:
335                 status = CAM_PROVIDE_FAIL;
336                 break;
337         }
338
339         return (targcamstatus(status));
340 }
341
342 /* Writes are always ready, reads wait for user_ccb_queue or abort_queue */
343 static int
344 targpoll(struct cdev *dev, int poll_events, struct thread *td)
345 {
346         struct targ_softc *softc;
347         int     revents;
348
349         softc = (struct targ_softc *)dev->si_drv1;
350
351         /* Poll for write() is always ok. */
352         revents = poll_events & (POLLOUT | POLLWRNORM);
353         if ((poll_events & (POLLIN | POLLRDNORM)) != 0) {
354                 /* Poll for read() depends on user and abort queues. */
355                 cam_periph_lock(softc->periph);
356                 if (!TAILQ_EMPTY(&softc->user_ccb_queue) ||
357                     !TAILQ_EMPTY(&softc->abort_queue)) {
358                         revents |= poll_events & (POLLIN | POLLRDNORM);
359                 }
360                 cam_periph_unlock(softc->periph);
361                 /* Only sleep if the user didn't poll for write. */
362                 if (revents == 0)
363                         selrecord(td, &softc->read_select);
364         }
365
366         return (revents);
367 }
368
369 static int
370 targkqfilter(struct cdev *dev, struct knote *kn)
371 {
372         struct  targ_softc *softc;
373
374         softc = (struct targ_softc *)dev->si_drv1;
375         kn->kn_hook = (caddr_t)softc;
376         kn->kn_fop = &targread_filtops;
377         knlist_add(&softc->read_select.si_note, kn, 0);
378         return (0);
379 }
380
381 static void
382 targreadfiltdetach(struct knote *kn)
383 {
384         struct  targ_softc *softc;
385
386         softc = (struct targ_softc *)kn->kn_hook;
387         knlist_remove(&softc->read_select.si_note, kn, 0);
388 }
389
390 /* Notify the user's kqueue when the user queue or abort queue gets a CCB */
391 static int
392 targreadfilt(struct knote *kn, long hint)
393 {
394         struct targ_softc *softc;
395         int     retval;
396
397         softc = (struct targ_softc *)kn->kn_hook;
398         cam_periph_lock(softc->periph);
399         retval = !TAILQ_EMPTY(&softc->user_ccb_queue) ||
400                  !TAILQ_EMPTY(&softc->abort_queue);
401         cam_periph_unlock(softc->periph);
402         return (retval);
403 }
404
405 /* Send the HBA the enable/disable message */
406 static cam_status
407 targendislun(struct cam_path *path, int enable, int grp6_len, int grp7_len)
408 {
409         struct ccb_en_lun en_ccb;
410         cam_status        status;
411
412         /* Tell the lun to begin answering selects */
413         xpt_setup_ccb(&en_ccb.ccb_h, path, CAM_PRIORITY_NORMAL);
414         en_ccb.ccb_h.func_code = XPT_EN_LUN;
415         /* Don't need support for any vendor specific commands */
416         en_ccb.grp6_len = grp6_len;
417         en_ccb.grp7_len = grp7_len;
418         en_ccb.enable = enable ? 1 : 0;
419         xpt_action((union ccb *)&en_ccb);
420         status = en_ccb.ccb_h.status & CAM_STATUS_MASK;
421         if (status != CAM_REQ_CMP) {
422                 xpt_print(path, "%sable lun CCB rejected, status %#x\n",
423                     enable ? "en" : "dis", status);
424         }
425         return (status);
426 }
427
428 /* Enable target mode on a LUN, given its path */
429 static cam_status
430 targenable(struct targ_softc *softc, struct cam_path *path, int grp6_len,
431            int grp7_len)
432 {
433         struct cam_periph *periph;
434         struct ccb_pathinq cpi;
435         cam_status         status;
436
437         if ((softc->state & TARG_STATE_LUN_ENABLED) != 0)
438                 return (CAM_LUN_ALRDY_ENA);
439
440         /* Make sure SIM supports target mode */
441         xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
442         cpi.ccb_h.func_code = XPT_PATH_INQ;
443         xpt_action((union ccb *)&cpi);
444         status = cpi.ccb_h.status & CAM_STATUS_MASK;
445         if (status != CAM_REQ_CMP) {
446                 printf("pathinq failed, status %#x\n", status);
447                 goto enable_fail;
448         }
449         if ((cpi.target_sprt & PIT_PROCESSOR) == 0) {
450                 printf("controller does not support target mode\n");
451                 status = CAM_FUNC_NOTAVAIL;
452                 goto enable_fail;
453         }
454
455         /* Destroy any periph on our path if it is disabled */
456         periph = cam_periph_find(path, "targ");
457         if (periph != NULL) {
458                 struct targ_softc *del_softc;
459
460                 del_softc = (struct targ_softc *)periph->softc;
461                 if ((del_softc->state & TARG_STATE_LUN_ENABLED) == 0) {
462                         cam_periph_invalidate(del_softc->periph);
463                         del_softc->periph = NULL;
464                 } else {
465                         printf("Requested path still in use by targ%d\n",
466                                periph->unit_number);
467                         status = CAM_LUN_ALRDY_ENA;
468                         goto enable_fail;
469                 }
470         }
471
472         /* Create a periph instance attached to this path */
473         status = cam_periph_alloc(targctor, NULL, targdtor, targstart,
474                         "targ", CAM_PERIPH_BIO, path, targasync, 0, softc);
475         if (status != CAM_REQ_CMP) {
476                 printf("cam_periph_alloc failed, status %#x\n", status);
477                 goto enable_fail;
478         }
479
480         /* Ensure that the periph now exists. */
481         if (cam_periph_find(path, "targ") == NULL) {
482                 panic("targenable: succeeded but no periph?");
483                 /* NOTREACHED */
484         }
485
486         /* Send the enable lun message */
487         status = targendislun(path, /*enable*/1, grp6_len, grp7_len);
488         if (status != CAM_REQ_CMP) {
489                 printf("enable lun failed, status %#x\n", status);
490                 goto enable_fail;
491         }
492         softc->state |= TARG_STATE_LUN_ENABLED;
493
494 enable_fail:
495         return (status);
496 }
497
498 /* Disable this softc's target instance if enabled */
499 static cam_status
500 targdisable(struct targ_softc *softc)
501 {
502         cam_status status;
503
504         if ((softc->state & TARG_STATE_LUN_ENABLED) == 0)
505                 return (CAM_REQ_CMP);
506
507         CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targdisable\n"));
508
509         /* Abort any ccbs pending on the controller */
510         abort_all_pending(softc);
511
512         /* Disable this lun */
513         status = targendislun(softc->path, /*enable*/0,
514                               /*grp6_len*/0, /*grp7_len*/0);
515         if (status == CAM_REQ_CMP)
516                 softc->state &= ~TARG_STATE_LUN_ENABLED;
517         else
518                 printf("Disable lun failed, status %#x\n", status);
519
520         return (status);
521 }
522
523 /* Initialize a periph (called from cam_periph_alloc) */
524 static cam_status
525 targctor(struct cam_periph *periph, void *arg)
526 {
527         struct targ_softc *softc;
528
529         /* Store pointer to softc for periph-driven routines */
530         softc = (struct targ_softc *)arg;
531         periph->softc = softc;
532         softc->periph = periph;
533         softc->path = periph->path;
534         return (CAM_REQ_CMP);
535 }
536
537 static void
538 targdtor(struct cam_periph *periph)
539 {
540         struct targ_softc     *softc;
541         struct ccb_hdr        *ccb_h;
542         struct targ_cmd_descr *descr;
543
544         softc = (struct targ_softc *)periph->softc;
545
546         /* 
547          * targdisable() aborts CCBs back to the user and leaves them
548          * on user_ccb_queue and abort_queue in case the user is still
549          * interested in them.  We free them now.
550          */
551         while ((ccb_h = TAILQ_FIRST(&softc->user_ccb_queue)) != NULL) {
552                 TAILQ_REMOVE(&softc->user_ccb_queue, ccb_h, periph_links.tqe);
553                 targfreeccb(softc, (union ccb *)ccb_h);
554         }
555         while ((descr = TAILQ_FIRST(&softc->abort_queue)) != NULL) {
556                 TAILQ_REMOVE(&softc->abort_queue, descr, tqe);
557                 free(descr, M_TARG);
558         }
559
560         softc->periph = NULL;
561         softc->path = NULL;
562         periph->softc = NULL;
563 }
564
565 /* Receive CCBs from user mode proc and send them to the HBA */
566 static int
567 targwrite(struct cdev *dev, struct uio *uio, int ioflag)
568 {
569         union ccb *user_ccb;
570         struct targ_softc *softc;
571         struct targ_cmd_descr *descr;
572         int write_len, error;
573         int func_code, priority;
574
575         softc = (struct targ_softc *)dev->si_drv1;
576         write_len = error = 0;
577         CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
578                   ("write - uio_resid %zd\n", uio->uio_resid));
579         while (uio->uio_resid >= sizeof(user_ccb) && error == 0) {
580                 union ccb *ccb;
581
582                 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
583                 if (error != 0) {
584                         CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
585                                   ("write - uiomove failed (%d)\n", error));
586                         break;
587                 }
588                 priority = fuword32(&user_ccb->ccb_h.pinfo.priority);
589                 if (priority == CAM_PRIORITY_NONE) {
590                         error = EINVAL;
591                         break;
592                 }
593                 func_code = fuword32(&user_ccb->ccb_h.func_code);
594                 switch (func_code) {
595                 case XPT_ACCEPT_TARGET_IO:
596                 case XPT_IMMED_NOTIFY:
597                         cam_periph_lock(softc->periph);
598                         ccb = targgetccb(softc, func_code, priority);
599                         descr = (struct targ_cmd_descr *)ccb->ccb_h.targ_descr;
600                         descr->user_ccb = user_ccb;
601                         descr->func_code = func_code;
602                         CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
603                                   ("Sent ATIO/INOT (%p)\n", user_ccb));
604                         xpt_action(ccb);
605                         TAILQ_INSERT_TAIL(&softc->pending_ccb_queue,
606                                           &ccb->ccb_h,
607                                           periph_links.tqe);
608                         cam_periph_unlock(softc->periph);
609                         break;
610                 default:
611                         cam_periph_lock(softc->periph);
612                         if ((func_code & XPT_FC_QUEUED) != 0) {
613                                 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
614                                           ("Sending queued ccb %#x (%p)\n",
615                                           func_code, user_ccb));
616                                 descr = targgetdescr(softc);
617                                 descr->user_ccb = user_ccb;
618                                 descr->priority = priority;
619                                 descr->func_code = func_code;
620                                 TAILQ_INSERT_TAIL(&softc->work_queue,
621                                                   descr, tqe);
622                                 xpt_schedule(softc->periph, priority);
623                         } else {
624                                 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
625                                           ("Sending inline ccb %#x (%p)\n",
626                                           func_code, user_ccb));
627                                 ccb = targgetccb(softc, func_code, priority);
628                                 descr = (struct targ_cmd_descr *)
629                                          ccb->ccb_h.targ_descr;
630                                 descr->user_ccb = user_ccb;
631                                 descr->priority = priority;
632                                 descr->func_code = func_code;
633                                 if (targusermerge(softc, descr, ccb) != EFAULT)
634                                         targsendccb(softc, ccb, descr);
635                                 targreturnccb(softc, ccb);
636                         }
637                         cam_periph_unlock(softc->periph);
638                         break;
639                 }
640                 write_len += sizeof(user_ccb);
641         }
642         
643         /*
644          * If we've successfully taken in some amount of
645          * data, return success for that data first.  If
646          * an error is persistent, it will be reported
647          * on the next write.
648          */
649         if (error != 0 && write_len == 0)
650                 return (error);
651         if (write_len == 0 && uio->uio_resid != 0)
652                 return (ENOSPC);
653         return (0);
654 }
655
656 /* Process requests (descrs) via the periph-supplied CCBs */
657 static void
658 targstart(struct cam_periph *periph, union ccb *start_ccb)
659 {
660         struct targ_softc *softc;
661         struct targ_cmd_descr *descr, *next_descr;
662         int error;
663
664         softc = (struct targ_softc *)periph->softc;
665         CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targstart %p\n", start_ccb));
666
667         descr = TAILQ_FIRST(&softc->work_queue);
668         if (descr == NULL) {
669                 xpt_release_ccb(start_ccb);
670         } else {
671                 TAILQ_REMOVE(&softc->work_queue, descr, tqe);
672                 next_descr = TAILQ_FIRST(&softc->work_queue);
673
674                 /* Initiate a transaction using the descr and supplied CCB */
675                 error = targusermerge(softc, descr, start_ccb);
676                 if (error == 0)
677                         error = targsendccb(softc, start_ccb, descr);
678                 if (error != 0) {
679                         xpt_print(periph->path,
680                             "targsendccb failed, err %d\n", error);
681                         xpt_release_ccb(start_ccb);
682                         suword(&descr->user_ccb->ccb_h.status,
683                                CAM_REQ_CMP_ERR);
684                         TAILQ_INSERT_TAIL(&softc->abort_queue, descr, tqe);
685                         notify_user(softc);
686                 }
687
688                 /* If we have more work to do, stay scheduled */
689                 if (next_descr != NULL)
690                         xpt_schedule(periph, next_descr->priority);
691         }
692 }
693
694 static int
695 targusermerge(struct targ_softc *softc, struct targ_cmd_descr *descr,
696               union ccb *ccb)
697 {
698         struct ccb_hdr *u_ccbh, *k_ccbh;
699         size_t ccb_len;
700         int error;
701
702         u_ccbh = &descr->user_ccb->ccb_h;
703         k_ccbh = &ccb->ccb_h;
704
705         /*
706          * There are some fields in the CCB header that need to be
707          * preserved, the rest we get from the user ccb. (See xpt_merge_ccb)
708          */
709         xpt_setup_ccb(k_ccbh, softc->path, descr->priority);
710         k_ccbh->retry_count = fuword32(&u_ccbh->retry_count);
711         k_ccbh->func_code = descr->func_code;
712         k_ccbh->flags = fuword32(&u_ccbh->flags);
713         k_ccbh->timeout = fuword32(&u_ccbh->timeout);
714         ccb_len = targccblen(k_ccbh->func_code) - sizeof(struct ccb_hdr);
715         error = copyin(u_ccbh + 1, k_ccbh + 1, ccb_len);
716         if (error != 0) {
717                 k_ccbh->status = CAM_REQ_CMP_ERR;
718                 return (error);
719         }
720
721         /* Translate usermode abort_ccb pointer to its kernel counterpart */
722         if (k_ccbh->func_code == XPT_ABORT) {
723                 struct ccb_abort *cab;
724                 struct ccb_hdr *ccb_h;
725
726                 cab = (struct ccb_abort *)ccb;
727                 TAILQ_FOREACH(ccb_h, &softc->pending_ccb_queue,
728                     periph_links.tqe) {
729                         struct targ_cmd_descr *ab_descr;
730
731                         ab_descr = (struct targ_cmd_descr *)ccb_h->targ_descr;
732                         if (ab_descr->user_ccb == cab->abort_ccb) {
733                                 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
734                                           ("Changing abort for %p to %p\n",
735                                           cab->abort_ccb, ccb_h));
736                                 cab->abort_ccb = (union ccb *)ccb_h;
737                                 break;
738                         }
739                 }
740                 /* CCB not found, set appropriate status */
741                 if (ccb_h == NULL) {
742                         k_ccbh->status = CAM_PATH_INVALID;
743                         error = ESRCH;
744                 }
745         }
746
747         return (error);
748 }
749
750 /* Build and send a kernel CCB formed from descr->user_ccb */
751 static int
752 targsendccb(struct targ_softc *softc, union ccb *ccb,
753             struct targ_cmd_descr *descr)
754 {
755         struct cam_periph_map_info *mapinfo;
756         struct ccb_hdr *ccb_h;
757         int error;
758
759         ccb_h = &ccb->ccb_h;
760         mapinfo = &descr->mapinfo;
761         mapinfo->num_bufs_used = 0;
762
763         /*
764          * There's no way for the user to have a completion
765          * function, so we put our own completion function in here.
766          * We also stash in a reference to our descriptor so targreturnccb()
767          * can find our mapping info.
768          */
769         ccb_h->cbfcnp = targdone;
770         ccb_h->targ_descr = descr;
771
772         /*
773          * We only attempt to map the user memory into kernel space
774          * if they haven't passed in a physical memory pointer,
775          * and if there is actually an I/O operation to perform.
776          * Right now cam_periph_mapmem() only supports SCSI and device
777          * match CCBs.  For the SCSI CCBs, we only pass the CCB in if
778          * there's actually data to map.  cam_periph_mapmem() will do the
779          * right thing, even if there isn't data to map, but since CCBs
780          * without data are a reasonably common occurance (e.g. test unit
781          * ready), it will save a few cycles if we check for it here.
782          */
783         if (((ccb_h->flags & CAM_DATA_PHYS) == 0)
784          && (((ccb_h->func_code == XPT_CONT_TARGET_IO)
785             && ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE))
786           || (ccb_h->func_code == XPT_DEV_MATCH))) {
787
788                 error = cam_periph_mapmem(ccb, mapinfo);
789
790                 /*
791                  * cam_periph_mapmem returned an error, we can't continue.
792                  * Return the error to the user.
793                  */
794                 if (error) {
795                         ccb_h->status = CAM_REQ_CMP_ERR;
796                         mapinfo->num_bufs_used = 0;
797                         return (error);
798                 }
799         }
800
801         /*
802          * Once queued on the pending CCB list, this CCB will be protected
803          * by our error recovery handler.
804          */
805         CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("sendccb %p\n", ccb));
806         if (XPT_FC_IS_QUEUED(ccb)) {
807                 TAILQ_INSERT_TAIL(&softc->pending_ccb_queue, ccb_h,
808                                   periph_links.tqe);
809         }
810         xpt_action(ccb);
811
812         return (0);
813 }
814
815 /* Completion routine for CCBs (called at splsoftcam) */
816 static void
817 targdone(struct cam_periph *periph, union ccb *done_ccb)
818 {
819         struct targ_softc *softc;
820         cam_status status;
821
822         CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("targdone %p\n", done_ccb));
823         softc = (struct targ_softc *)periph->softc;
824         TAILQ_REMOVE(&softc->pending_ccb_queue, &done_ccb->ccb_h,
825                      periph_links.tqe);
826         status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
827
828         /* If we're no longer enabled, throw away CCB */
829         if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) {
830                 targfreeccb(softc, done_ccb);
831                 return;
832         }
833         /* abort_all_pending() waits for pending queue to be empty */
834         if (TAILQ_EMPTY(&softc->pending_ccb_queue))
835                 wakeup(&softc->pending_ccb_queue);
836
837         switch (done_ccb->ccb_h.func_code) {
838         /* All FC_*_QUEUED CCBs go back to userland */
839         case XPT_IMMED_NOTIFY:
840         case XPT_ACCEPT_TARGET_IO:
841         case XPT_CONT_TARGET_IO:
842                 TAILQ_INSERT_TAIL(&softc->user_ccb_queue, &done_ccb->ccb_h,
843                                   periph_links.tqe);
844                 cam_periph_unlock(softc->periph);
845                 notify_user(softc);
846                 cam_periph_lock(softc->periph);
847                 break;
848         default:
849                 panic("targdone: impossible xpt opcode %#x",
850                       done_ccb->ccb_h.func_code);
851                 /* NOTREACHED */
852         }
853 }
854
855 /* Return CCBs to the user from the user queue and abort queue */
856 static int
857 targread(struct cdev *dev, struct uio *uio, int ioflag)
858 {
859         struct descr_queue      *abort_queue;
860         struct targ_cmd_descr   *user_descr;
861         struct targ_softc       *softc;
862         struct ccb_queue  *user_queue;
863         struct ccb_hdr    *ccb_h;
864         union  ccb        *user_ccb;
865         int                read_len, error;
866
867         error = 0;
868         read_len = 0;
869         softc = (struct targ_softc *)dev->si_drv1;
870         user_queue = &softc->user_ccb_queue;
871         abort_queue = &softc->abort_queue;
872         CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targread\n"));
873
874         /* If no data is available, wait or return immediately */
875         cam_periph_lock(softc->periph);
876         ccb_h = TAILQ_FIRST(user_queue);
877         user_descr = TAILQ_FIRST(abort_queue);
878         while (ccb_h == NULL && user_descr == NULL) {
879                 if ((ioflag & IO_NDELAY) == 0) {
880                         error = msleep(user_queue, softc->periph->sim->mtx,
881                             PRIBIO | PCATCH, "targrd", 0);
882                         ccb_h = TAILQ_FIRST(user_queue);
883                         user_descr = TAILQ_FIRST(abort_queue);
884                         if (error != 0) {
885                                 if (error == ERESTART) {
886                                         continue;
887                                 } else {
888                                         goto read_fail;
889                                 }
890                         }
891                 } else {
892                         cam_periph_unlock(softc->periph);
893                         return (EAGAIN);
894                 }
895         }
896
897         /* Data is available so fill the user's buffer */
898         while (ccb_h != NULL) {
899                 struct targ_cmd_descr *descr;
900
901                 if (uio->uio_resid < sizeof(user_ccb))
902                         break;
903                 TAILQ_REMOVE(user_queue, ccb_h, periph_links.tqe);
904                 descr = (struct targ_cmd_descr *)ccb_h->targ_descr;
905                 user_ccb = descr->user_ccb;
906                 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
907                           ("targread ccb %p (%p)\n", ccb_h, user_ccb));
908                 error = targreturnccb(softc, (union ccb *)ccb_h);
909                 if (error != 0)
910                         goto read_fail;
911                 cam_periph_unlock(softc->periph);
912                 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
913                 cam_periph_lock(softc->periph);
914                 if (error != 0)
915                         goto read_fail;
916                 read_len += sizeof(user_ccb);
917
918                 ccb_h = TAILQ_FIRST(user_queue);
919         }
920
921         /* Flush out any aborted descriptors */
922         while (user_descr != NULL) {
923                 if (uio->uio_resid < sizeof(user_ccb))
924                         break;
925                 TAILQ_REMOVE(abort_queue, user_descr, tqe);
926                 user_ccb = user_descr->user_ccb;
927                 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
928                           ("targread aborted descr %p (%p)\n",
929                           user_descr, user_ccb));
930                 suword(&user_ccb->ccb_h.status, CAM_REQ_ABORTED);
931                 cam_periph_unlock(softc->periph);
932                 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
933                 cam_periph_lock(softc->periph);
934                 if (error != 0)
935                         goto read_fail;
936                 read_len += sizeof(user_ccb);
937
938                 user_descr = TAILQ_FIRST(abort_queue);
939         }
940
941         /*
942          * If we've successfully read some amount of data, don't report an
943          * error.  If the error is persistent, it will be reported on the
944          * next read().
945          */
946         if (read_len == 0 && uio->uio_resid != 0)
947                 error = ENOSPC;
948
949 read_fail:
950         cam_periph_unlock(softc->periph);
951         return (error);
952 }
953
954 /* Copy completed ccb back to the user */
955 static int
956 targreturnccb(struct targ_softc *softc, union ccb *ccb)
957 {
958         struct targ_cmd_descr *descr;
959         struct ccb_hdr *u_ccbh;
960         size_t ccb_len;
961         int error;
962
963         CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targreturnccb %p\n", ccb));
964         descr = (struct targ_cmd_descr *)ccb->ccb_h.targ_descr;
965         u_ccbh = &descr->user_ccb->ccb_h;
966
967         /* Copy out the central portion of the ccb_hdr */
968         copyout(&ccb->ccb_h.retry_count, &u_ccbh->retry_count,
969                 offsetof(struct ccb_hdr, periph_priv) -
970                 offsetof(struct ccb_hdr, retry_count));
971
972         /* Copy out the rest of the ccb (after the ccb_hdr) */
973         ccb_len = targccblen(ccb->ccb_h.func_code) - sizeof(struct ccb_hdr);
974         if (descr->mapinfo.num_bufs_used != 0)
975                 cam_periph_unmapmem(ccb, &descr->mapinfo);
976         error = copyout(&ccb->ccb_h + 1, u_ccbh + 1, ccb_len);
977         if (error != 0) {
978                 xpt_print(softc->path,
979                     "targreturnccb - CCB copyout failed (%d)\n", error);
980         }
981         /* Free CCB or send back to devq. */
982         targfreeccb(softc, ccb);
983
984         return (error);
985 }
986
987 static union ccb *
988 targgetccb(struct targ_softc *softc, xpt_opcode type, int priority)
989 {
990         union ccb *ccb;
991         int ccb_len;
992
993         ccb_len = targccblen(type);
994         ccb = malloc(ccb_len, M_TARG, M_NOWAIT);
995         CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("getccb %p\n", ccb));
996         if (ccb == NULL) {
997                 return (ccb);
998         }
999         xpt_setup_ccb(&ccb->ccb_h, softc->path, priority);
1000         ccb->ccb_h.func_code = type;
1001         ccb->ccb_h.cbfcnp = targdone;
1002         ccb->ccb_h.targ_descr = targgetdescr(softc);
1003         if (ccb->ccb_h.targ_descr == NULL) {
1004                 free (ccb, M_TARG);
1005                 ccb = NULL;
1006         }
1007         return (ccb);
1008 }
1009
1010 static void
1011 targfreeccb(struct targ_softc *softc, union ccb *ccb)
1012 {
1013         CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, ("targfreeccb descr %p and\n",
1014                         ccb->ccb_h.targ_descr));
1015         free(ccb->ccb_h.targ_descr, M_TARG);
1016
1017         switch (ccb->ccb_h.func_code) {
1018         case XPT_ACCEPT_TARGET_IO:
1019         case XPT_IMMED_NOTIFY:
1020                 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, ("freeing ccb %p\n", ccb));
1021                 free(ccb, M_TARG);
1022                 break;
1023         default:
1024                 /* Send back CCB if we got it from the periph */
1025                 if (XPT_FC_IS_QUEUED(ccb)) {
1026                         CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH,
1027                                         ("returning queued ccb %p\n", ccb));
1028                         xpt_release_ccb(ccb);
1029                 } else {
1030                         CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH,
1031                                         ("freeing ccb %p\n", ccb));
1032                         free(ccb, M_TARG);
1033                 }
1034                 break;
1035         }
1036 }
1037
1038 static struct targ_cmd_descr *
1039 targgetdescr(struct targ_softc *softc)
1040 {
1041         struct targ_cmd_descr *descr;
1042
1043         descr = malloc(sizeof(*descr), M_TARG,
1044                M_NOWAIT);
1045         if (descr) {
1046                 descr->mapinfo.num_bufs_used = 0;
1047         }
1048         return (descr);
1049 }
1050
1051 static void
1052 targinit(void)
1053 {
1054         EVENTHANDLER_REGISTER(dev_clone, targclone, 0, 1000);
1055 }
1056
1057 static void
1058 targclone(void *arg, struct ucred *cred, char *name, int namelen,
1059     struct cdev **dev)
1060 {
1061         int u;
1062
1063         if (*dev != NULL)
1064                 return;
1065         if (dev_stdclone(name, NULL, "targ", &u) != 1)
1066                 return;
1067         *dev = make_dev(&targ_cdevsw, u, UID_ROOT, GID_WHEEL,
1068                         0600, "targ%d", u);
1069         dev_ref(*dev);
1070         (*dev)->si_flags |= SI_CHEAPCLONE;
1071 }
1072
1073 static void
1074 targasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
1075 {
1076         /* All events are handled in usermode by INOTs */
1077         panic("targasync() called, should be an INOT instead");
1078 }
1079
1080 /* Cancel all pending requests and CCBs awaiting work. */
1081 static void
1082 abort_all_pending(struct targ_softc *softc)
1083 {
1084         struct targ_cmd_descr   *descr;
1085         struct ccb_abort         cab;
1086         struct ccb_hdr          *ccb_h;
1087         struct cam_sim          *sim;
1088
1089         CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("abort_all_pending\n"));
1090
1091         /* First abort the descriptors awaiting resources */
1092         while ((descr = TAILQ_FIRST(&softc->work_queue)) != NULL) {
1093                 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
1094                           ("Aborting descr from workq %p\n", descr));
1095                 TAILQ_REMOVE(&softc->work_queue, descr, tqe);
1096                 TAILQ_INSERT_TAIL(&softc->abort_queue, descr, tqe);
1097         }
1098
1099         /* 
1100          * Then abort all pending CCBs.
1101          * targdone() will return the aborted CCB via user_ccb_queue
1102          */
1103         xpt_setup_ccb(&cab.ccb_h, softc->path, CAM_PRIORITY_NORMAL);
1104         cab.ccb_h.func_code = XPT_ABORT;
1105         cab.ccb_h.status = CAM_REQ_CMP_ERR;
1106         TAILQ_FOREACH(ccb_h, &softc->pending_ccb_queue, periph_links.tqe) {
1107                 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
1108                           ("Aborting pending CCB %p\n", ccb_h));
1109                 cab.abort_ccb = (union ccb *)ccb_h;
1110                 xpt_action((union ccb *)&cab);
1111                 if (cab.ccb_h.status != CAM_REQ_CMP) {
1112                         xpt_print(cab.ccb_h.path,
1113                             "Unable to abort CCB, status %#x\n",
1114                             cab.ccb_h.status);
1115                 }
1116         }
1117
1118         /* If we aborted at least one pending CCB ok, wait for it. */
1119         if (cab.ccb_h.status == CAM_REQ_CMP) {
1120                 sim = xpt_path_sim(softc->path);
1121                 msleep(&softc->pending_ccb_queue, sim->mtx,
1122                        PRIBIO | PCATCH, "tgabrt", 0);
1123         }
1124
1125         /* If we aborted anything from the work queue, wakeup user. */
1126         if (!TAILQ_EMPTY(&softc->user_ccb_queue)
1127          || !TAILQ_EMPTY(&softc->abort_queue)) {
1128                 cam_periph_unlock(softc->periph);
1129                 notify_user(softc);
1130                 cam_periph_lock(softc->periph);
1131         }
1132 }
1133
1134 /* Notify the user that data is ready */
1135 static void
1136 notify_user(struct targ_softc *softc)
1137 {
1138         /*
1139          * Notify users sleeping via poll(), kqueue(), and
1140          * blocking read().
1141          */
1142         selwakeuppri(&softc->read_select, PRIBIO);
1143         KNOTE_UNLOCKED(&softc->read_select.si_note, 0);
1144         wakeup(&softc->user_ccb_queue);
1145 }
1146
1147 /* Convert CAM status to errno values */
1148 static int
1149 targcamstatus(cam_status status)
1150 {
1151         switch (status & CAM_STATUS_MASK) {
1152         case CAM_REQ_CMP:       /* CCB request completed without error */
1153                 return (0);
1154         case CAM_REQ_INPROG:    /* CCB request is in progress */
1155                 return (EINPROGRESS);
1156         case CAM_REQ_CMP_ERR:   /* CCB request completed with an error */
1157                 return (EIO);
1158         case CAM_PROVIDE_FAIL:  /* Unable to provide requested capability */
1159                 return (ENOTTY);
1160         case CAM_FUNC_NOTAVAIL: /* The requested function is not available */
1161                 return (ENOTSUP);
1162         case CAM_LUN_ALRDY_ENA: /* LUN is already enabled for target mode */
1163                 return (EADDRINUSE);
1164         case CAM_PATH_INVALID:  /* Supplied Path ID is invalid */
1165         case CAM_DEV_NOT_THERE: /* SCSI Device Not Installed/there */
1166                 return (ENOENT);
1167         case CAM_REQ_ABORTED:   /* CCB request aborted by the host */
1168                 return (ECANCELED);
1169         case CAM_CMD_TIMEOUT:   /* Command timeout */
1170                 return (ETIMEDOUT);
1171         case CAM_REQUEUE_REQ:   /* Requeue to preserve transaction ordering */
1172                 return (EAGAIN);
1173         case CAM_REQ_INVALID:   /* CCB request was invalid */
1174                 return (EINVAL);
1175         case CAM_RESRC_UNAVAIL: /* Resource Unavailable */
1176                 return (ENOMEM);
1177         case CAM_BUSY:          /* CAM subsystem is busy */
1178         case CAM_UA_ABORT:      /* Unable to abort CCB request */
1179                 return (EBUSY);
1180         default:
1181                 return (ENXIO);
1182         }
1183 }
1184
1185 static size_t
1186 targccblen(xpt_opcode func_code)
1187 {
1188         int len;
1189
1190         /* Codes we expect to see as a target */
1191         switch (func_code) {
1192         case XPT_CONT_TARGET_IO:
1193         case XPT_SCSI_IO:
1194                 len = sizeof(struct ccb_scsiio);
1195                 break;
1196         case XPT_ACCEPT_TARGET_IO:
1197                 len = sizeof(struct ccb_accept_tio);
1198                 break;
1199         case XPT_IMMED_NOTIFY:
1200                 len = sizeof(struct ccb_immed_notify);
1201                 break;
1202         case XPT_REL_SIMQ:
1203                 len = sizeof(struct ccb_relsim);
1204                 break;
1205         case XPT_PATH_INQ:
1206                 len = sizeof(struct ccb_pathinq);
1207                 break;
1208         case XPT_DEBUG:
1209                 len = sizeof(struct ccb_debug);
1210                 break;
1211         case XPT_ABORT:
1212                 len = sizeof(struct ccb_abort);
1213                 break;
1214         case XPT_EN_LUN:
1215                 len = sizeof(struct ccb_en_lun);
1216                 break;
1217         default:
1218                 len = sizeof(union ccb);
1219                 break;
1220         }
1221
1222         return (len);
1223 }
1224
1225 /*
1226  * work around to destroy targ device
1227  * outside of targclose
1228  */
1229 static void
1230 targdestroy(void *dev)
1231 {
1232         struct cdev *device = (struct cdev *)dev;
1233         struct targ_softc *softc = (struct targ_softc *)device->si_drv1;
1234
1235 #if 0
1236         callout_stop(&softc->destroy_dev_callout);
1237 #endif
1238
1239         mtx_unlock(&softc->destroy_mtx);
1240         mtx_destroy(&softc->destroy_mtx);
1241         free(softc, M_TARG);
1242         device->si_drv1 = 0;
1243         destroy_dev(device);
1244         printf("%s: destroyed dev\n", __func__);
1245 }