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