]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/cam/scsi/scsi_target.c
MFC r234177, r234374 (by trasz):
[FreeBSD/stable/9.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                 struct ccb_debug cdbg;
309
310                 /* If no periph available, disallow debugging changes */
311                 if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) {
312                         status = CAM_DEV_NOT_THERE;
313                         break;
314                 }
315                 bzero(&cdbg, sizeof cdbg);
316                 if (*((int *)addr) != 0)
317                         cdbg.flags = CAM_DEBUG_PERIPH;
318                 else
319                         cdbg.flags = CAM_DEBUG_NONE;
320                 cam_periph_lock(softc->periph);
321                 xpt_setup_ccb(&cdbg.ccb_h, softc->path, CAM_PRIORITY_NORMAL);
322                 cdbg.ccb_h.func_code = XPT_DEBUG;
323                 cdbg.ccb_h.cbfcnp = targdone;
324
325                 xpt_action((union ccb *)&cdbg);
326                 cam_periph_unlock(softc->periph);
327                 status = cdbg.ccb_h.status & CAM_STATUS_MASK;
328                 break;
329         }
330         default:
331                 status = CAM_PROVIDE_FAIL;
332                 break;
333         }
334
335         return (targcamstatus(status));
336 }
337
338 /* Writes are always ready, reads wait for user_ccb_queue or abort_queue */
339 static int
340 targpoll(struct cdev *dev, int poll_events, struct thread *td)
341 {
342         struct targ_softc *softc;
343         int     revents;
344
345         softc = (struct targ_softc *)dev->si_drv1;
346
347         /* Poll for write() is always ok. */
348         revents = poll_events & (POLLOUT | POLLWRNORM);
349         if ((poll_events & (POLLIN | POLLRDNORM)) != 0) {
350                 /* Poll for read() depends on user and abort queues. */
351                 cam_periph_lock(softc->periph);
352                 if (!TAILQ_EMPTY(&softc->user_ccb_queue) ||
353                     !TAILQ_EMPTY(&softc->abort_queue)) {
354                         revents |= poll_events & (POLLIN | POLLRDNORM);
355                 }
356                 cam_periph_unlock(softc->periph);
357                 /* Only sleep if the user didn't poll for write. */
358                 if (revents == 0)
359                         selrecord(td, &softc->read_select);
360         }
361
362         return (revents);
363 }
364
365 static int
366 targkqfilter(struct cdev *dev, struct knote *kn)
367 {
368         struct  targ_softc *softc;
369
370         softc = (struct targ_softc *)dev->si_drv1;
371         kn->kn_hook = (caddr_t)softc;
372         kn->kn_fop = &targread_filtops;
373         knlist_add(&softc->read_select.si_note, kn, 0);
374         return (0);
375 }
376
377 static void
378 targreadfiltdetach(struct knote *kn)
379 {
380         struct  targ_softc *softc;
381
382         softc = (struct targ_softc *)kn->kn_hook;
383         knlist_remove(&softc->read_select.si_note, kn, 0);
384 }
385
386 /* Notify the user's kqueue when the user queue or abort queue gets a CCB */
387 static int
388 targreadfilt(struct knote *kn, long hint)
389 {
390         struct targ_softc *softc;
391         int     retval;
392
393         softc = (struct targ_softc *)kn->kn_hook;
394         cam_periph_lock(softc->periph);
395         retval = !TAILQ_EMPTY(&softc->user_ccb_queue) ||
396                  !TAILQ_EMPTY(&softc->abort_queue);
397         cam_periph_unlock(softc->periph);
398         return (retval);
399 }
400
401 /* Send the HBA the enable/disable message */
402 static cam_status
403 targendislun(struct cam_path *path, int enable, int grp6_len, int grp7_len)
404 {
405         struct ccb_en_lun en_ccb;
406         cam_status        status;
407
408         /* Tell the lun to begin answering selects */
409         xpt_setup_ccb(&en_ccb.ccb_h, path, CAM_PRIORITY_NORMAL);
410         en_ccb.ccb_h.func_code = XPT_EN_LUN;
411         /* Don't need support for any vendor specific commands */
412         en_ccb.grp6_len = grp6_len;
413         en_ccb.grp7_len = grp7_len;
414         en_ccb.enable = enable ? 1 : 0;
415         xpt_action((union ccb *)&en_ccb);
416         status = en_ccb.ccb_h.status & CAM_STATUS_MASK;
417         if (status != CAM_REQ_CMP) {
418                 xpt_print(path, "%sable lun CCB rejected, status %#x\n",
419                     enable ? "en" : "dis", status);
420         }
421         return (status);
422 }
423
424 /* Enable target mode on a LUN, given its path */
425 static cam_status
426 targenable(struct targ_softc *softc, struct cam_path *path, int grp6_len,
427            int grp7_len)
428 {
429         struct cam_periph *periph;
430         struct ccb_pathinq cpi;
431         cam_status         status;
432
433         if ((softc->state & TARG_STATE_LUN_ENABLED) != 0)
434                 return (CAM_LUN_ALRDY_ENA);
435
436         /* Make sure SIM supports target mode */
437         xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
438         cpi.ccb_h.func_code = XPT_PATH_INQ;
439         xpt_action((union ccb *)&cpi);
440         status = cpi.ccb_h.status & CAM_STATUS_MASK;
441         if (status != CAM_REQ_CMP) {
442                 printf("pathinq failed, status %#x\n", status);
443                 goto enable_fail;
444         }
445         if ((cpi.target_sprt & PIT_PROCESSOR) == 0) {
446                 printf("controller does not support target mode\n");
447                 status = CAM_FUNC_NOTAVAIL;
448                 goto enable_fail;
449         }
450
451         /* Destroy any periph on our path if it is disabled */
452         periph = cam_periph_find(path, "targ");
453         if (periph != NULL) {
454                 struct targ_softc *del_softc;
455
456                 del_softc = (struct targ_softc *)periph->softc;
457                 if ((del_softc->state & TARG_STATE_LUN_ENABLED) == 0) {
458                         cam_periph_invalidate(del_softc->periph);
459                         del_softc->periph = NULL;
460                 } else {
461                         printf("Requested path still in use by targ%d\n",
462                                periph->unit_number);
463                         status = CAM_LUN_ALRDY_ENA;
464                         goto enable_fail;
465                 }
466         }
467
468         /* Create a periph instance attached to this path */
469         status = cam_periph_alloc(targctor, NULL, targdtor, targstart,
470                         "targ", CAM_PERIPH_BIO, path, targasync, 0, softc);
471         if (status != CAM_REQ_CMP) {
472                 printf("cam_periph_alloc failed, status %#x\n", status);
473                 goto enable_fail;
474         }
475
476         /* Ensure that the periph now exists. */
477         if (cam_periph_find(path, "targ") == NULL) {
478                 panic("targenable: succeeded but no periph?");
479                 /* NOTREACHED */
480         }
481
482         /* Send the enable lun message */
483         status = targendislun(path, /*enable*/1, grp6_len, grp7_len);
484         if (status != CAM_REQ_CMP) {
485                 printf("enable lun failed, status %#x\n", status);
486                 goto enable_fail;
487         }
488         softc->state |= TARG_STATE_LUN_ENABLED;
489
490 enable_fail:
491         return (status);
492 }
493
494 /* Disable this softc's target instance if enabled */
495 static cam_status
496 targdisable(struct targ_softc *softc)
497 {
498         cam_status status;
499
500         if ((softc->state & TARG_STATE_LUN_ENABLED) == 0)
501                 return (CAM_REQ_CMP);
502
503         CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targdisable\n"));
504
505         /* Abort any ccbs pending on the controller */
506         abort_all_pending(softc);
507
508         /* Disable this lun */
509         status = targendislun(softc->path, /*enable*/0,
510                               /*grp6_len*/0, /*grp7_len*/0);
511         if (status == CAM_REQ_CMP)
512                 softc->state &= ~TARG_STATE_LUN_ENABLED;
513         else
514                 printf("Disable lun failed, status %#x\n", status);
515
516         return (status);
517 }
518
519 /* Initialize a periph (called from cam_periph_alloc) */
520 static cam_status
521 targctor(struct cam_periph *periph, void *arg)
522 {
523         struct targ_softc *softc;
524
525         /* Store pointer to softc for periph-driven routines */
526         softc = (struct targ_softc *)arg;
527         periph->softc = softc;
528         softc->periph = periph;
529         softc->path = periph->path;
530         return (CAM_REQ_CMP);
531 }
532
533 static void
534 targdtor(struct cam_periph *periph)
535 {
536         struct targ_softc     *softc;
537         struct ccb_hdr        *ccb_h;
538         struct targ_cmd_descr *descr;
539
540         softc = (struct targ_softc *)periph->softc;
541
542         /* 
543          * targdisable() aborts CCBs back to the user and leaves them
544          * on user_ccb_queue and abort_queue in case the user is still
545          * interested in them.  We free them now.
546          */
547         while ((ccb_h = TAILQ_FIRST(&softc->user_ccb_queue)) != NULL) {
548                 TAILQ_REMOVE(&softc->user_ccb_queue, ccb_h, periph_links.tqe);
549                 targfreeccb(softc, (union ccb *)ccb_h);
550         }
551         while ((descr = TAILQ_FIRST(&softc->abort_queue)) != NULL) {
552                 TAILQ_REMOVE(&softc->abort_queue, descr, tqe);
553                 free(descr, M_TARG);
554         }
555
556         softc->periph = NULL;
557         softc->path = NULL;
558         periph->softc = NULL;
559 }
560
561 /* Receive CCBs from user mode proc and send them to the HBA */
562 static int
563 targwrite(struct cdev *dev, struct uio *uio, int ioflag)
564 {
565         union ccb *user_ccb;
566         struct targ_softc *softc;
567         struct targ_cmd_descr *descr;
568         int write_len, error;
569         int func_code, priority;
570
571         softc = (struct targ_softc *)dev->si_drv1;
572         write_len = error = 0;
573         CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
574                   ("write - uio_resid %zd\n", uio->uio_resid));
575         while (uio->uio_resid >= sizeof(user_ccb) && error == 0) {
576                 union ccb *ccb;
577
578                 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
579                 if (error != 0) {
580                         CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
581                                   ("write - uiomove failed (%d)\n", error));
582                         break;
583                 }
584                 priority = fuword32(&user_ccb->ccb_h.pinfo.priority);
585                 if (priority == CAM_PRIORITY_NONE) {
586                         error = EINVAL;
587                         break;
588                 }
589                 func_code = fuword32(&user_ccb->ccb_h.func_code);
590                 switch (func_code) {
591                 case XPT_ACCEPT_TARGET_IO:
592                 case XPT_IMMED_NOTIFY:
593                         cam_periph_lock(softc->periph);
594                         ccb = targgetccb(softc, func_code, priority);
595                         descr = (struct targ_cmd_descr *)ccb->ccb_h.targ_descr;
596                         descr->user_ccb = user_ccb;
597                         descr->func_code = func_code;
598                         CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
599                                   ("Sent ATIO/INOT (%p)\n", user_ccb));
600                         xpt_action(ccb);
601                         TAILQ_INSERT_TAIL(&softc->pending_ccb_queue,
602                                           &ccb->ccb_h,
603                                           periph_links.tqe);
604                         cam_periph_unlock(softc->periph);
605                         break;
606                 default:
607                         cam_periph_lock(softc->periph);
608                         if ((func_code & XPT_FC_QUEUED) != 0) {
609                                 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
610                                           ("Sending queued ccb %#x (%p)\n",
611                                           func_code, user_ccb));
612                                 descr = targgetdescr(softc);
613                                 descr->user_ccb = user_ccb;
614                                 descr->priority = priority;
615                                 descr->func_code = func_code;
616                                 TAILQ_INSERT_TAIL(&softc->work_queue,
617                                                   descr, tqe);
618                                 xpt_schedule(softc->periph, priority);
619                         } else {
620                                 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
621                                           ("Sending inline ccb %#x (%p)\n",
622                                           func_code, user_ccb));
623                                 ccb = targgetccb(softc, func_code, priority);
624                                 descr = (struct targ_cmd_descr *)
625                                          ccb->ccb_h.targ_descr;
626                                 descr->user_ccb = user_ccb;
627                                 descr->priority = priority;
628                                 descr->func_code = func_code;
629                                 if (targusermerge(softc, descr, ccb) != EFAULT)
630                                         targsendccb(softc, ccb, descr);
631                                 targreturnccb(softc, ccb);
632                         }
633                         cam_periph_unlock(softc->periph);
634                         break;
635                 }
636                 write_len += sizeof(user_ccb);
637         }
638         
639         /*
640          * If we've successfully taken in some amount of
641          * data, return success for that data first.  If
642          * an error is persistent, it will be reported
643          * on the next write.
644          */
645         if (error != 0 && write_len == 0)
646                 return (error);
647         if (write_len == 0 && uio->uio_resid != 0)
648                 return (ENOSPC);
649         return (0);
650 }
651
652 /* Process requests (descrs) via the periph-supplied CCBs */
653 static void
654 targstart(struct cam_periph *periph, union ccb *start_ccb)
655 {
656         struct targ_softc *softc;
657         struct targ_cmd_descr *descr, *next_descr;
658         int error;
659
660         softc = (struct targ_softc *)periph->softc;
661         CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targstart %p\n", start_ccb));
662
663         descr = TAILQ_FIRST(&softc->work_queue);
664         if (descr == NULL) {
665                 xpt_release_ccb(start_ccb);
666         } else {
667                 TAILQ_REMOVE(&softc->work_queue, descr, tqe);
668                 next_descr = TAILQ_FIRST(&softc->work_queue);
669
670                 /* Initiate a transaction using the descr and supplied CCB */
671                 error = targusermerge(softc, descr, start_ccb);
672                 if (error == 0)
673                         error = targsendccb(softc, start_ccb, descr);
674                 if (error != 0) {
675                         xpt_print(periph->path,
676                             "targsendccb failed, err %d\n", error);
677                         xpt_release_ccb(start_ccb);
678                         suword(&descr->user_ccb->ccb_h.status,
679                                CAM_REQ_CMP_ERR);
680                         TAILQ_INSERT_TAIL(&softc->abort_queue, descr, tqe);
681                         notify_user(softc);
682                 }
683
684                 /* If we have more work to do, stay scheduled */
685                 if (next_descr != NULL)
686                         xpt_schedule(periph, next_descr->priority);
687         }
688 }
689
690 static int
691 targusermerge(struct targ_softc *softc, struct targ_cmd_descr *descr,
692               union ccb *ccb)
693 {
694         struct ccb_hdr *u_ccbh, *k_ccbh;
695         size_t ccb_len;
696         int error;
697
698         u_ccbh = &descr->user_ccb->ccb_h;
699         k_ccbh = &ccb->ccb_h;
700
701         /*
702          * There are some fields in the CCB header that need to be
703          * preserved, the rest we get from the user ccb. (See xpt_merge_ccb)
704          */
705         xpt_setup_ccb(k_ccbh, softc->path, descr->priority);
706         k_ccbh->retry_count = fuword32(&u_ccbh->retry_count);
707         k_ccbh->func_code = descr->func_code;
708         k_ccbh->flags = fuword32(&u_ccbh->flags);
709         k_ccbh->timeout = fuword32(&u_ccbh->timeout);
710         ccb_len = targccblen(k_ccbh->func_code) - sizeof(struct ccb_hdr);
711         error = copyin(u_ccbh + 1, k_ccbh + 1, ccb_len);
712         if (error != 0) {
713                 k_ccbh->status = CAM_REQ_CMP_ERR;
714                 return (error);
715         }
716
717         /* Translate usermode abort_ccb pointer to its kernel counterpart */
718         if (k_ccbh->func_code == XPT_ABORT) {
719                 struct ccb_abort *cab;
720                 struct ccb_hdr *ccb_h;
721
722                 cab = (struct ccb_abort *)ccb;
723                 TAILQ_FOREACH(ccb_h, &softc->pending_ccb_queue,
724                     periph_links.tqe) {
725                         struct targ_cmd_descr *ab_descr;
726
727                         ab_descr = (struct targ_cmd_descr *)ccb_h->targ_descr;
728                         if (ab_descr->user_ccb == cab->abort_ccb) {
729                                 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
730                                           ("Changing abort for %p to %p\n",
731                                           cab->abort_ccb, ccb_h));
732                                 cab->abort_ccb = (union ccb *)ccb_h;
733                                 break;
734                         }
735                 }
736                 /* CCB not found, set appropriate status */
737                 if (ccb_h == NULL) {
738                         k_ccbh->status = CAM_PATH_INVALID;
739                         error = ESRCH;
740                 }
741         }
742
743         return (error);
744 }
745
746 /* Build and send a kernel CCB formed from descr->user_ccb */
747 static int
748 targsendccb(struct targ_softc *softc, union ccb *ccb,
749             struct targ_cmd_descr *descr)
750 {
751         struct cam_periph_map_info *mapinfo;
752         struct ccb_hdr *ccb_h;
753         int error;
754
755         ccb_h = &ccb->ccb_h;
756         mapinfo = &descr->mapinfo;
757         mapinfo->num_bufs_used = 0;
758
759         /*
760          * There's no way for the user to have a completion
761          * function, so we put our own completion function in here.
762          * We also stash in a reference to our descriptor so targreturnccb()
763          * can find our mapping info.
764          */
765         ccb_h->cbfcnp = targdone;
766         ccb_h->targ_descr = descr;
767
768         /*
769          * We only attempt to map the user memory into kernel space
770          * if they haven't passed in a physical memory pointer,
771          * and if there is actually an I/O operation to perform.
772          * Right now cam_periph_mapmem() only supports SCSI and device
773          * match CCBs.  For the SCSI CCBs, we only pass the CCB in if
774          * there's actually data to map.  cam_periph_mapmem() will do the
775          * right thing, even if there isn't data to map, but since CCBs
776          * without data are a reasonably common occurance (e.g. test unit
777          * ready), it will save a few cycles if we check for it here.
778          */
779         if (((ccb_h->flags & CAM_DATA_PHYS) == 0)
780          && (((ccb_h->func_code == XPT_CONT_TARGET_IO)
781             && ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE))
782           || (ccb_h->func_code == XPT_DEV_MATCH))) {
783
784                 error = cam_periph_mapmem(ccb, mapinfo);
785
786                 /*
787                  * cam_periph_mapmem returned an error, we can't continue.
788                  * Return the error to the user.
789                  */
790                 if (error) {
791                         ccb_h->status = CAM_REQ_CMP_ERR;
792                         mapinfo->num_bufs_used = 0;
793                         return (error);
794                 }
795         }
796
797         /*
798          * Once queued on the pending CCB list, this CCB will be protected
799          * by our error recovery handler.
800          */
801         CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("sendccb %p\n", ccb));
802         if (XPT_FC_IS_QUEUED(ccb)) {
803                 TAILQ_INSERT_TAIL(&softc->pending_ccb_queue, ccb_h,
804                                   periph_links.tqe);
805         }
806         xpt_action(ccb);
807
808         return (0);
809 }
810
811 /* Completion routine for CCBs (called at splsoftcam) */
812 static void
813 targdone(struct cam_periph *periph, union ccb *done_ccb)
814 {
815         struct targ_softc *softc;
816         cam_status status;
817
818         CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("targdone %p\n", done_ccb));
819         softc = (struct targ_softc *)periph->softc;
820         TAILQ_REMOVE(&softc->pending_ccb_queue, &done_ccb->ccb_h,
821                      periph_links.tqe);
822         status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
823
824         /* If we're no longer enabled, throw away CCB */
825         if ((softc->state & TARG_STATE_LUN_ENABLED) == 0) {
826                 targfreeccb(softc, done_ccb);
827                 return;
828         }
829         /* abort_all_pending() waits for pending queue to be empty */
830         if (TAILQ_EMPTY(&softc->pending_ccb_queue))
831                 wakeup(&softc->pending_ccb_queue);
832
833         switch (done_ccb->ccb_h.func_code) {
834         /* All FC_*_QUEUED CCBs go back to userland */
835         case XPT_IMMED_NOTIFY:
836         case XPT_ACCEPT_TARGET_IO:
837         case XPT_CONT_TARGET_IO:
838                 TAILQ_INSERT_TAIL(&softc->user_ccb_queue, &done_ccb->ccb_h,
839                                   periph_links.tqe);
840                 cam_periph_unlock(softc->periph);
841                 notify_user(softc);
842                 cam_periph_lock(softc->periph);
843                 break;
844         default:
845                 panic("targdone: impossible xpt opcode %#x",
846                       done_ccb->ccb_h.func_code);
847                 /* NOTREACHED */
848         }
849 }
850
851 /* Return CCBs to the user from the user queue and abort queue */
852 static int
853 targread(struct cdev *dev, struct uio *uio, int ioflag)
854 {
855         struct descr_queue      *abort_queue;
856         struct targ_cmd_descr   *user_descr;
857         struct targ_softc       *softc;
858         struct ccb_queue  *user_queue;
859         struct ccb_hdr    *ccb_h;
860         union  ccb        *user_ccb;
861         int                read_len, error;
862
863         error = 0;
864         read_len = 0;
865         softc = (struct targ_softc *)dev->si_drv1;
866         user_queue = &softc->user_ccb_queue;
867         abort_queue = &softc->abort_queue;
868         CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targread\n"));
869
870         /* If no data is available, wait or return immediately */
871         cam_periph_lock(softc->periph);
872         ccb_h = TAILQ_FIRST(user_queue);
873         user_descr = TAILQ_FIRST(abort_queue);
874         while (ccb_h == NULL && user_descr == NULL) {
875                 if ((ioflag & IO_NDELAY) == 0) {
876                         error = msleep(user_queue, softc->periph->sim->mtx,
877                             PRIBIO | PCATCH, "targrd", 0);
878                         ccb_h = TAILQ_FIRST(user_queue);
879                         user_descr = TAILQ_FIRST(abort_queue);
880                         if (error != 0) {
881                                 if (error == ERESTART) {
882                                         continue;
883                                 } else {
884                                         goto read_fail;
885                                 }
886                         }
887                 } else {
888                         cam_periph_unlock(softc->periph);
889                         return (EAGAIN);
890                 }
891         }
892
893         /* Data is available so fill the user's buffer */
894         while (ccb_h != NULL) {
895                 struct targ_cmd_descr *descr;
896
897                 if (uio->uio_resid < sizeof(user_ccb))
898                         break;
899                 TAILQ_REMOVE(user_queue, ccb_h, periph_links.tqe);
900                 descr = (struct targ_cmd_descr *)ccb_h->targ_descr;
901                 user_ccb = descr->user_ccb;
902                 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
903                           ("targread ccb %p (%p)\n", ccb_h, user_ccb));
904                 error = targreturnccb(softc, (union ccb *)ccb_h);
905                 if (error != 0)
906                         goto read_fail;
907                 cam_periph_unlock(softc->periph);
908                 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
909                 cam_periph_lock(softc->periph);
910                 if (error != 0)
911                         goto read_fail;
912                 read_len += sizeof(user_ccb);
913
914                 ccb_h = TAILQ_FIRST(user_queue);
915         }
916
917         /* Flush out any aborted descriptors */
918         while (user_descr != NULL) {
919                 if (uio->uio_resid < sizeof(user_ccb))
920                         break;
921                 TAILQ_REMOVE(abort_queue, user_descr, tqe);
922                 user_ccb = user_descr->user_ccb;
923                 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
924                           ("targread aborted descr %p (%p)\n",
925                           user_descr, user_ccb));
926                 suword(&user_ccb->ccb_h.status, CAM_REQ_ABORTED);
927                 cam_periph_unlock(softc->periph);
928                 error = uiomove((caddr_t)&user_ccb, sizeof(user_ccb), uio);
929                 cam_periph_lock(softc->periph);
930                 if (error != 0)
931                         goto read_fail;
932                 read_len += sizeof(user_ccb);
933
934                 user_descr = TAILQ_FIRST(abort_queue);
935         }
936
937         /*
938          * If we've successfully read some amount of data, don't report an
939          * error.  If the error is persistent, it will be reported on the
940          * next read().
941          */
942         if (read_len == 0 && uio->uio_resid != 0)
943                 error = ENOSPC;
944
945 read_fail:
946         cam_periph_unlock(softc->periph);
947         return (error);
948 }
949
950 /* Copy completed ccb back to the user */
951 static int
952 targreturnccb(struct targ_softc *softc, union ccb *ccb)
953 {
954         struct targ_cmd_descr *descr;
955         struct ccb_hdr *u_ccbh;
956         size_t ccb_len;
957         int error;
958
959         CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("targreturnccb %p\n", ccb));
960         descr = (struct targ_cmd_descr *)ccb->ccb_h.targ_descr;
961         u_ccbh = &descr->user_ccb->ccb_h;
962
963         /* Copy out the central portion of the ccb_hdr */
964         copyout(&ccb->ccb_h.retry_count, &u_ccbh->retry_count,
965                 offsetof(struct ccb_hdr, periph_priv) -
966                 offsetof(struct ccb_hdr, retry_count));
967
968         /* Copy out the rest of the ccb (after the ccb_hdr) */
969         ccb_len = targccblen(ccb->ccb_h.func_code) - sizeof(struct ccb_hdr);
970         if (descr->mapinfo.num_bufs_used != 0)
971                 cam_periph_unmapmem(ccb, &descr->mapinfo);
972         error = copyout(&ccb->ccb_h + 1, u_ccbh + 1, ccb_len);
973         if (error != 0) {
974                 xpt_print(softc->path,
975                     "targreturnccb - CCB copyout failed (%d)\n", error);
976         }
977         /* Free CCB or send back to devq. */
978         targfreeccb(softc, ccb);
979
980         return (error);
981 }
982
983 static union ccb *
984 targgetccb(struct targ_softc *softc, xpt_opcode type, int priority)
985 {
986         union ccb *ccb;
987         int ccb_len;
988
989         ccb_len = targccblen(type);
990         ccb = malloc(ccb_len, M_TARG, M_NOWAIT);
991         CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("getccb %p\n", ccb));
992         if (ccb == NULL) {
993                 return (ccb);
994         }
995         xpt_setup_ccb(&ccb->ccb_h, softc->path, priority);
996         ccb->ccb_h.func_code = type;
997         ccb->ccb_h.cbfcnp = targdone;
998         ccb->ccb_h.targ_descr = targgetdescr(softc);
999         if (ccb->ccb_h.targ_descr == NULL) {
1000                 free (ccb, M_TARG);
1001                 ccb = NULL;
1002         }
1003         return (ccb);
1004 }
1005
1006 static void
1007 targfreeccb(struct targ_softc *softc, union ccb *ccb)
1008 {
1009         CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, ("targfreeccb descr %p and\n",
1010                         ccb->ccb_h.targ_descr));
1011         free(ccb->ccb_h.targ_descr, M_TARG);
1012
1013         switch (ccb->ccb_h.func_code) {
1014         case XPT_ACCEPT_TARGET_IO:
1015         case XPT_IMMED_NOTIFY:
1016                 CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH, ("freeing ccb %p\n", ccb));
1017                 free(ccb, M_TARG);
1018                 break;
1019         default:
1020                 /* Send back CCB if we got it from the periph */
1021                 if (XPT_FC_IS_QUEUED(ccb)) {
1022                         CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH,
1023                                         ("returning queued ccb %p\n", ccb));
1024                         xpt_release_ccb(ccb);
1025                 } else {
1026                         CAM_DEBUG_PRINT(CAM_DEBUG_PERIPH,
1027                                         ("freeing ccb %p\n", ccb));
1028                         free(ccb, M_TARG);
1029                 }
1030                 break;
1031         }
1032 }
1033
1034 static struct targ_cmd_descr *
1035 targgetdescr(struct targ_softc *softc)
1036 {
1037         struct targ_cmd_descr *descr;
1038
1039         descr = malloc(sizeof(*descr), M_TARG,
1040                M_NOWAIT);
1041         if (descr) {
1042                 descr->mapinfo.num_bufs_used = 0;
1043         }
1044         return (descr);
1045 }
1046
1047 static void
1048 targinit(void)
1049 {
1050         EVENTHANDLER_REGISTER(dev_clone, targclone, 0, 1000);
1051 }
1052
1053 static void
1054 targclone(void *arg, struct ucred *cred, char *name, int namelen,
1055     struct cdev **dev)
1056 {
1057         int u;
1058
1059         if (*dev != NULL)
1060                 return;
1061         if (dev_stdclone(name, NULL, "targ", &u) != 1)
1062                 return;
1063         *dev = make_dev(&targ_cdevsw, u, UID_ROOT, GID_WHEEL,
1064                         0600, "targ%d", u);
1065         dev_ref(*dev);
1066         (*dev)->si_flags |= SI_CHEAPCLONE;
1067 }
1068
1069 static void
1070 targasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
1071 {
1072         /* All events are handled in usermode by INOTs */
1073         panic("targasync() called, should be an INOT instead");
1074 }
1075
1076 /* Cancel all pending requests and CCBs awaiting work. */
1077 static void
1078 abort_all_pending(struct targ_softc *softc)
1079 {
1080         struct targ_cmd_descr   *descr;
1081         struct ccb_abort         cab;
1082         struct ccb_hdr          *ccb_h;
1083         struct cam_sim          *sim;
1084
1085         CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH, ("abort_all_pending\n"));
1086
1087         /* First abort the descriptors awaiting resources */
1088         while ((descr = TAILQ_FIRST(&softc->work_queue)) != NULL) {
1089                 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
1090                           ("Aborting descr from workq %p\n", descr));
1091                 TAILQ_REMOVE(&softc->work_queue, descr, tqe);
1092                 TAILQ_INSERT_TAIL(&softc->abort_queue, descr, tqe);
1093         }
1094
1095         /* 
1096          * Then abort all pending CCBs.
1097          * targdone() will return the aborted CCB via user_ccb_queue
1098          */
1099         xpt_setup_ccb(&cab.ccb_h, softc->path, CAM_PRIORITY_NORMAL);
1100         cab.ccb_h.func_code = XPT_ABORT;
1101         cab.ccb_h.status = CAM_REQ_CMP_ERR;
1102         TAILQ_FOREACH(ccb_h, &softc->pending_ccb_queue, periph_links.tqe) {
1103                 CAM_DEBUG(softc->path, CAM_DEBUG_PERIPH,
1104                           ("Aborting pending CCB %p\n", ccb_h));
1105                 cab.abort_ccb = (union ccb *)ccb_h;
1106                 xpt_action((union ccb *)&cab);
1107                 if (cab.ccb_h.status != CAM_REQ_CMP) {
1108                         xpt_print(cab.ccb_h.path,
1109                             "Unable to abort CCB, status %#x\n",
1110                             cab.ccb_h.status);
1111                 }
1112         }
1113
1114         /* If we aborted at least one pending CCB ok, wait for it. */
1115         if (cab.ccb_h.status == CAM_REQ_CMP) {
1116                 sim = xpt_path_sim(softc->path);
1117                 msleep(&softc->pending_ccb_queue, sim->mtx,
1118                        PRIBIO | PCATCH, "tgabrt", 0);
1119         }
1120
1121         /* If we aborted anything from the work queue, wakeup user. */
1122         if (!TAILQ_EMPTY(&softc->user_ccb_queue)
1123          || !TAILQ_EMPTY(&softc->abort_queue)) {
1124                 cam_periph_unlock(softc->periph);
1125                 notify_user(softc);
1126                 cam_periph_lock(softc->periph);
1127         }
1128 }
1129
1130 /* Notify the user that data is ready */
1131 static void
1132 notify_user(struct targ_softc *softc)
1133 {
1134         /*
1135          * Notify users sleeping via poll(), kqueue(), and
1136          * blocking read().
1137          */
1138         selwakeuppri(&softc->read_select, PRIBIO);
1139         KNOTE_UNLOCKED(&softc->read_select.si_note, 0);
1140         wakeup(&softc->user_ccb_queue);
1141 }
1142
1143 /* Convert CAM status to errno values */
1144 static int
1145 targcamstatus(cam_status status)
1146 {
1147         switch (status & CAM_STATUS_MASK) {
1148         case CAM_REQ_CMP:       /* CCB request completed without error */
1149                 return (0);
1150         case CAM_REQ_INPROG:    /* CCB request is in progress */
1151                 return (EINPROGRESS);
1152         case CAM_REQ_CMP_ERR:   /* CCB request completed with an error */
1153                 return (EIO);
1154         case CAM_PROVIDE_FAIL:  /* Unable to provide requested capability */
1155                 return (ENOTTY);
1156         case CAM_FUNC_NOTAVAIL: /* The requested function is not available */
1157                 return (ENOTSUP);
1158         case CAM_LUN_ALRDY_ENA: /* LUN is already enabled for target mode */
1159                 return (EADDRINUSE);
1160         case CAM_PATH_INVALID:  /* Supplied Path ID is invalid */
1161         case CAM_DEV_NOT_THERE: /* SCSI Device Not Installed/there */
1162                 return (ENOENT);
1163         case CAM_REQ_ABORTED:   /* CCB request aborted by the host */
1164                 return (ECANCELED);
1165         case CAM_CMD_TIMEOUT:   /* Command timeout */
1166                 return (ETIMEDOUT);
1167         case CAM_REQUEUE_REQ:   /* Requeue to preserve transaction ordering */
1168                 return (EAGAIN);
1169         case CAM_REQ_INVALID:   /* CCB request was invalid */
1170                 return (EINVAL);
1171         case CAM_RESRC_UNAVAIL: /* Resource Unavailable */
1172                 return (ENOMEM);
1173         case CAM_BUSY:          /* CAM subsystem is busy */
1174         case CAM_UA_ABORT:      /* Unable to abort CCB request */
1175                 return (EBUSY);
1176         default:
1177                 return (ENXIO);
1178         }
1179 }
1180
1181 static size_t
1182 targccblen(xpt_opcode func_code)
1183 {
1184         int len;
1185
1186         /* Codes we expect to see as a target */
1187         switch (func_code) {
1188         case XPT_CONT_TARGET_IO:
1189         case XPT_SCSI_IO:
1190                 len = sizeof(struct ccb_scsiio);
1191                 break;
1192         case XPT_ACCEPT_TARGET_IO:
1193                 len = sizeof(struct ccb_accept_tio);
1194                 break;
1195         case XPT_IMMED_NOTIFY:
1196                 len = sizeof(struct ccb_immed_notify);
1197                 break;
1198         case XPT_REL_SIMQ:
1199                 len = sizeof(struct ccb_relsim);
1200                 break;
1201         case XPT_PATH_INQ:
1202                 len = sizeof(struct ccb_pathinq);
1203                 break;
1204         case XPT_DEBUG:
1205                 len = sizeof(struct ccb_debug);
1206                 break;
1207         case XPT_ABORT:
1208                 len = sizeof(struct ccb_abort);
1209                 break;
1210         case XPT_EN_LUN:
1211                 len = sizeof(struct ccb_en_lun);
1212                 break;
1213         default:
1214                 len = sizeof(union ccb);
1215                 break;
1216         }
1217
1218         return (len);
1219 }
1220
1221 /*
1222  * work around to destroy targ device
1223  * outside of targclose
1224  */
1225 static void
1226 targdestroy(void *dev)
1227 {
1228         struct cdev *device = (struct cdev *)dev;
1229         struct targ_softc *softc = (struct targ_softc *)device->si_drv1;
1230
1231 #if 0
1232         callout_stop(&softc->destroy_dev_callout);
1233 #endif
1234
1235         mtx_unlock(&softc->destroy_mtx);
1236         mtx_destroy(&softc->destroy_mtx);
1237         free(softc, M_TARG);
1238         device->si_drv1 = 0;
1239         destroy_dev(device);
1240         printf("%s: destroyed dev\n", __func__);
1241 }