proc->thread stage 2: MAJOR revamping of system calls, ucred, jail API,
[dragonfly.git] / sys / bus / cam / scsi / scsi_pass.c
1 /*
2  * Copyright (c) 1997, 1998 Justin T. Gibbs.
3  * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer,
11  *    without modification, immediately at the beginning of the file.
12  * 2. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/cam/scsi/scsi_pass.c,v 1.19 2000/01/17 06:27:37 mjacob Exp $
28  * $DragonFly: src/sys/bus/cam/scsi/scsi_pass.c,v 1.4 2003/06/23 17:55:26 dillon Exp $
29  */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/types.h>
35 #include <sys/buf.h>
36 #include <sys/malloc.h>
37 #include <sys/fcntl.h>
38 #include <sys/stat.h>
39 #include <sys/conf.h>
40 #include <sys/buf.h>
41 #include <sys/proc.h>
42 #include <sys/errno.h>
43 #include <sys/devicestat.h>
44 #include <sys/proc.h>
45 #include <sys/buf2.h>
46
47 #include <cam/cam.h>
48 #include <cam/cam_ccb.h>
49 #include <cam/cam_extend.h>
50 #include <cam/cam_periph.h>
51 #include <cam/cam_xpt_periph.h>
52 #include <cam/cam_debug.h>
53
54 #include <cam/scsi/scsi_all.h>
55 #include <cam/scsi/scsi_message.h>
56 #include <cam/scsi/scsi_da.h>
57 #include <cam/scsi/scsi_pass.h>
58
59 typedef enum {
60         PASS_FLAG_OPEN                  = 0x01,
61         PASS_FLAG_LOCKED                = 0x02,
62         PASS_FLAG_INVALID               = 0x04
63 } pass_flags;
64
65 typedef enum {
66         PASS_STATE_NORMAL
67 } pass_state;
68
69 typedef enum {
70         PASS_CCB_BUFFER_IO,
71         PASS_CCB_WAITING
72 } pass_ccb_types;
73
74 #define ccb_type        ppriv_field0
75 #define ccb_bp          ppriv_ptr1
76
77 struct pass_softc {
78         pass_state      state;
79         pass_flags      flags;
80         u_int8_t        pd_type;
81         struct          buf_queue_head buf_queue;
82         union ccb       saved_ccb;
83         struct devstat  device_stats;
84         dev_t           dev;
85 };
86
87 #ifndef MIN
88 #define MIN(x,y) ((x<y) ? x : y)
89 #endif
90
91 #define PASS_CDEV_MAJOR 31
92
93 static  d_open_t        passopen;
94 static  d_close_t       passclose;
95 static  d_ioctl_t       passioctl;
96 static  d_strategy_t    passstrategy;
97
98 static  periph_init_t   passinit;
99 static  periph_ctor_t   passregister;
100 static  periph_oninv_t  passoninvalidate;
101 static  periph_dtor_t   passcleanup;
102 static  periph_start_t  passstart;
103 static  void            passasync(void *callback_arg, u_int32_t code,
104                                   struct cam_path *path, void *arg);
105 static  void            passdone(struct cam_periph *periph, 
106                                  union ccb *done_ccb);
107 static  int             passerror(union ccb *ccb, u_int32_t cam_flags, 
108                                   u_int32_t sense_flags);
109 static  int             passsendccb(struct cam_periph *periph, union ccb *ccb,
110                                     union ccb *inccb);
111
112 static struct periph_driver passdriver =
113 {
114         passinit, "pass",
115         TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0
116 };
117
118 DATA_SET(periphdriver_set, passdriver);
119
120 static struct cdevsw pass_cdevsw = {
121         /* open */      passopen,
122         /* close */     passclose,
123         /* read */      physread,
124         /* write */     physwrite,
125         /* ioctl */     passioctl,
126         /* poll */      nopoll,
127         /* mmap */      nommap,
128         /* strategy */  passstrategy,
129         /* name */      "pass",
130         /* maj */       PASS_CDEV_MAJOR,
131         /* dump */      nodump,
132         /* psize */     nopsize,
133         /* flags */     0,
134         /* bmaj */      -1
135 };
136
137 static struct extend_array *passperiphs;
138
139 static void
140 passinit(void)
141 {
142         cam_status status;
143         struct cam_path *path;
144
145         /*
146          * Create our extend array for storing the devices we attach to.
147          */
148         passperiphs = cam_extend_new();
149         if (passperiphs == NULL) {
150                 printf("passm: Failed to alloc extend array!\n");
151                 return;
152         }
153
154         /*
155          * Install a global async callback.  This callback will
156          * receive async callbacks like "new device found".
157          */
158         status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
159                                  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
160
161         if (status == CAM_REQ_CMP) {
162                 struct ccb_setasync csa;
163
164                 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
165                 csa.ccb_h.func_code = XPT_SASYNC_CB;
166                 csa.event_enable = AC_FOUND_DEVICE;
167                 csa.callback = passasync;
168                 csa.callback_arg = NULL;
169                 xpt_action((union ccb *)&csa);
170                 status = csa.ccb_h.status;
171                 xpt_free_path(path);
172         }
173
174         if (status != CAM_REQ_CMP) {
175                 printf("pass: Failed to attach master async callback "
176                        "due to status 0x%x!\n", status);
177         }
178         
179 }
180
181 static void
182 passoninvalidate(struct cam_periph *periph)
183 {
184         int s;
185         struct pass_softc *softc;
186         struct buf *q_bp;
187         struct ccb_setasync csa;
188
189         softc = (struct pass_softc *)periph->softc;
190
191         /*
192          * De-register any async callbacks.
193          */
194         xpt_setup_ccb(&csa.ccb_h, periph->path,
195                       /* priority */ 5);
196         csa.ccb_h.func_code = XPT_SASYNC_CB;
197         csa.event_enable = 0;
198         csa.callback = passasync;
199         csa.callback_arg = periph;
200         xpt_action((union ccb *)&csa);
201
202         softc->flags |= PASS_FLAG_INVALID;
203
204         /*
205          * Although the oninvalidate() routines are always called at
206          * splsoftcam, we need to be at splbio() here to keep the buffer
207          * queue from being modified while we traverse it.
208          */
209         s = splbio();
210
211         /*
212          * Return all queued I/O with ENXIO.
213          * XXX Handle any transactions queued to the card
214          *     with XPT_ABORT_CCB.
215          */
216         while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
217                 bufq_remove(&softc->buf_queue, q_bp);
218                 q_bp->b_resid = q_bp->b_bcount;
219                 q_bp->b_error = ENXIO;
220                 q_bp->b_flags |= B_ERROR;
221                 biodone(q_bp);
222         }
223         splx(s);
224
225         if (bootverbose) {
226                 xpt_print_path(periph->path);
227                 printf("lost device\n");
228         }
229
230 }
231
232 static void
233 passcleanup(struct cam_periph *periph)
234 {
235         struct pass_softc *softc;
236
237         softc = (struct pass_softc *)periph->softc;
238
239         devstat_remove_entry(&softc->device_stats);
240
241         destroy_dev(softc->dev);
242
243         cam_extend_release(passperiphs, periph->unit_number);
244
245         if (bootverbose) {
246                 xpt_print_path(periph->path);
247                 printf("removing device entry\n");
248         }
249         free(softc, M_DEVBUF);
250 }
251
252 static void
253 passasync(void *callback_arg, u_int32_t code,
254           struct cam_path *path, void *arg)
255 {
256         struct cam_periph *periph;
257
258         periph = (struct cam_periph *)callback_arg;
259
260         switch (code) {
261         case AC_FOUND_DEVICE:
262         {
263                 struct ccb_getdev *cgd;
264                 cam_status status;
265  
266                 cgd = (struct ccb_getdev *)arg;
267
268                 /*
269                  * Allocate a peripheral instance for
270                  * this device and start the probe
271                  * process.
272                  */
273                 status = cam_periph_alloc(passregister, passoninvalidate,
274                                           passcleanup, passstart, "pass",
275                                           CAM_PERIPH_BIO, cgd->ccb_h.path,
276                                           passasync, AC_FOUND_DEVICE, cgd);
277
278                 if (status != CAM_REQ_CMP
279                  && status != CAM_REQ_INPROG)
280                         printf("passasync: Unable to attach new device "
281                                 "due to status 0x%x\n", status);
282
283                 break;
284         }
285         default:
286                 cam_periph_async(periph, code, path, arg);
287                 break;
288         }
289 }
290
291 static cam_status
292 passregister(struct cam_periph *periph, void *arg)
293 {
294         struct pass_softc *softc;
295         struct ccb_setasync csa;
296         struct ccb_getdev *cgd;
297
298         cgd = (struct ccb_getdev *)arg;
299         if (periph == NULL) {
300                 printf("passregister: periph was NULL!!\n");
301                 return(CAM_REQ_CMP_ERR);
302         }
303
304         if (cgd == NULL) {
305                 printf("passregister: no getdev CCB, can't register device\n");
306                 return(CAM_REQ_CMP_ERR);
307         }
308
309         softc = (struct pass_softc *)malloc(sizeof(*softc),
310                                             M_DEVBUF, M_NOWAIT);
311
312         if (softc == NULL) {
313                 printf("passregister: Unable to probe new device. "
314                        "Unable to allocate softc\n");                           
315                 return(CAM_REQ_CMP_ERR);
316         }
317
318         bzero(softc, sizeof(*softc));
319         softc->state = PASS_STATE_NORMAL;
320         softc->pd_type = SID_TYPE(&cgd->inq_data);
321         bufq_init(&softc->buf_queue);
322
323         periph->softc = softc;
324
325         cam_extend_set(passperiphs, periph->unit_number, periph);
326         /*
327          * We pass in 0 for a blocksize, since we don't 
328          * know what the blocksize of this device is, if 
329          * it even has a blocksize.
330          */
331         devstat_add_entry(&softc->device_stats, "pass", periph->unit_number,
332                           0, DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
333                           softc->pd_type |
334                           DEVSTAT_TYPE_IF_SCSI |
335                           DEVSTAT_TYPE_PASS,
336                           DEVSTAT_PRIORITY_PASS);
337
338         /* Register the device */
339         softc->dev = make_dev(&pass_cdevsw, periph->unit_number, UID_ROOT,
340                               GID_OPERATOR, 0600, "%s%d", periph->periph_name,
341                               periph->unit_number);
342
343         /*
344          * Add an async callback so that we get
345          * notified if this device goes away.
346          */
347         xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
348         csa.ccb_h.func_code = XPT_SASYNC_CB;
349         csa.event_enable = AC_LOST_DEVICE;
350         csa.callback = passasync;
351         csa.callback_arg = periph;
352         xpt_action((union ccb *)&csa);
353
354         if (bootverbose)
355                 xpt_announce_periph(periph, NULL);
356
357         return(CAM_REQ_CMP);
358 }
359
360 static int
361 passopen(dev_t dev, int flags, int fmt, struct thread *td)
362 {
363         struct cam_periph *periph;
364         struct pass_softc *softc;
365         int unit, error;
366         int s;
367
368         error = 0; /* default to no error */
369
370         /* unit = dkunit(dev); */
371         /* XXX KDM fix this */
372         unit = minor(dev) & 0xff;
373
374         periph = cam_extend_get(passperiphs, unit);
375
376         if (periph == NULL)
377                 return (ENXIO);
378
379         softc = (struct pass_softc *)periph->softc;
380
381         s = splsoftcam();
382         if (softc->flags & PASS_FLAG_INVALID) {
383                 splx(s);
384                 return(ENXIO);
385         }
386
387         /*
388          * Don't allow access when we're running at a high securelvel.
389          */
390         if (securelevel > 1) {
391                 splx(s);
392                 return(EPERM);
393         }
394
395         /*
396          * Only allow read-write access.
397          */
398         if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) {
399                 splx(s);
400                 return(EPERM);
401         }
402
403         /*
404          * We don't allow nonblocking access.
405          */
406         if ((flags & O_NONBLOCK) != 0) {
407                 xpt_print_path(periph->path);
408                 printf("can't do nonblocking accesss\n");
409                 splx(s);
410                 return(EINVAL);
411         }
412
413         if ((error = cam_periph_lock(periph, PRIBIO | PCATCH)) != 0) {
414                 splx(s);
415                 return (error);
416         }
417
418         splx(s);
419
420         if ((softc->flags & PASS_FLAG_OPEN) == 0) {
421                 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
422                         return(ENXIO);
423                 softc->flags |= PASS_FLAG_OPEN;
424         }
425
426         cam_periph_unlock(periph);
427
428         return (error);
429 }
430
431 static int
432 passclose(dev_t dev, int flag, int fmt, struct thread *td)
433 {
434         struct  cam_periph *periph;
435         struct  pass_softc *softc;
436         int     unit, error;
437
438         /* unit = dkunit(dev); */
439         /* XXX KDM fix this */
440         unit = minor(dev) & 0xff;
441
442         periph = cam_extend_get(passperiphs, unit);
443         if (periph == NULL)
444                 return (ENXIO); 
445
446         softc = (struct pass_softc *)periph->softc;
447
448         if ((error = cam_periph_lock(periph, PRIBIO)) != 0)
449                 return (error);
450
451         softc->flags &= ~PASS_FLAG_OPEN;
452
453         cam_periph_unlock(periph);
454         cam_periph_release(periph);
455
456         return (0);
457 }
458
459 /*
460  * Actually translate the requested transfer into one the physical driver
461  * can understand.  The transfer is described by a buf and will include
462  * only one physical transfer.
463  */
464 static void
465 passstrategy(struct buf *bp)
466 {
467         struct cam_periph *periph;
468         struct pass_softc *softc;
469         u_int  unit;
470         int    s;
471
472         /*
473          * The read/write interface for the passthrough driver doesn't
474          * really work right now.  So, we just pass back EINVAL to tell the
475          * user to go away.
476          */
477         bp->b_error = EINVAL;
478         goto bad;
479
480         /* unit = dkunit(bp->b_dev); */
481         /* XXX KDM fix this */
482         unit = minor(bp->b_dev) & 0xff;
483
484         periph = cam_extend_get(passperiphs, unit);
485         if (periph == NULL) {
486                 bp->b_error = ENXIO;
487                 goto bad;
488         }
489         softc = (struct pass_softc *)periph->softc;
490
491         /*
492          * Odd number of bytes or negative offset
493          */
494         /* valid request?  */
495         if (bp->b_blkno < 0) {
496                 bp->b_error = EINVAL;
497                 goto bad;
498         }
499         
500         /*
501          * Mask interrupts so that the pack cannot be invalidated until
502          * after we are in the queue.  Otherwise, we might not properly
503          * clean up one of the buffers.
504          */
505         s = splbio();
506         
507         bufq_insert_tail(&softc->buf_queue, bp);
508
509         splx(s);
510         
511         /*
512          * Schedule ourselves for performing the work.
513          */
514         xpt_schedule(periph, /* XXX priority */1);
515
516         return;
517 bad:
518         bp->b_flags |= B_ERROR;
519
520         /*
521          * Correctly set the buf to indicate a completed xfer
522          */
523         bp->b_resid = bp->b_bcount;
524         biodone(bp);
525         return;
526 }
527
528 static void
529 passstart(struct cam_periph *periph, union ccb *start_ccb)
530 {
531         struct pass_softc *softc;
532         int s;
533
534         softc = (struct pass_softc *)periph->softc;
535
536         switch (softc->state) {
537         case PASS_STATE_NORMAL:
538         {
539                 struct buf *bp;
540
541                 s = splbio();
542                 bp = bufq_first(&softc->buf_queue);
543                 if (periph->immediate_priority <= periph->pinfo.priority) {
544                         start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING;                   
545                         SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
546                                           periph_links.sle);
547                         periph->immediate_priority = CAM_PRIORITY_NONE;
548                         splx(s);
549                         wakeup(&periph->ccb_list);
550                 } else if (bp == NULL) {
551                         splx(s);
552                         xpt_release_ccb(start_ccb);
553                 } else {
554
555                         bufq_remove(&softc->buf_queue, bp);
556
557                         devstat_start_transaction(&softc->device_stats);
558
559                         /*
560                          * XXX JGibbs -
561                          * Interpret the contents of the bp as a CCB
562                          * and pass it to a routine shared by our ioctl
563                          * code and passtart.
564                          * For now, just biodone it with EIO so we don't
565                          * hang.
566                          */
567                         bp->b_error = EIO;
568                         bp->b_flags |= B_ERROR;
569                         bp->b_resid = bp->b_bcount;
570                         biodone(bp);
571                         bp = bufq_first(&softc->buf_queue);
572                         splx(s);
573   
574                         xpt_action(start_ccb);
575
576                 }
577                 if (bp != NULL) {
578                         /* Have more work to do, so ensure we stay scheduled */
579                         xpt_schedule(periph, /* XXX priority */1);
580                 }
581                 break;
582         }
583         }
584 }
585 static void
586 passdone(struct cam_periph *periph, union ccb *done_ccb)
587
588         struct pass_softc *softc;
589         struct ccb_scsiio *csio;
590
591         softc = (struct pass_softc *)periph->softc;
592         csio = &done_ccb->csio;
593         switch (csio->ccb_h.ccb_type) {
594         case PASS_CCB_BUFFER_IO:
595         {
596                 struct buf              *bp;
597                 cam_status              status;
598                 u_int8_t                scsi_status;
599                 devstat_trans_flags     ds_flags;
600
601                 status = done_ccb->ccb_h.status;
602                 scsi_status = done_ccb->csio.scsi_status;
603                 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
604                 /* XXX handle errors */
605                 if (!(((status & CAM_STATUS_MASK) == CAM_REQ_CMP)
606                   && (scsi_status == SCSI_STATUS_OK))) {
607                         int error;
608                         
609                         if ((error = passerror(done_ccb, 0, 0)) == ERESTART) {
610                                 /*
611                                  * A retry was scheuled, so
612                                  * just return.
613                                  */
614                                 return;
615                         }
616
617                         /*
618                          * XXX unfreeze the queue after we complete
619                          * the abort process
620                          */
621                         bp->b_error = error;
622                         bp->b_flags |= B_ERROR;
623                 }
624
625                 if ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
626                         ds_flags = DEVSTAT_READ;
627                 else if ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
628                         ds_flags = DEVSTAT_WRITE;
629                 else
630                         ds_flags = DEVSTAT_NO_DATA;
631
632                 devstat_end_transaction_buf(&softc->device_stats, bp);
633                 biodone(bp);
634                 break;
635         }
636         case PASS_CCB_WAITING:
637         {
638                 /* Caller will release the CCB */
639                 wakeup(&done_ccb->ccb_h.cbfcnp);
640                 return;
641         }
642         }
643         xpt_release_ccb(done_ccb);
644 }
645
646 static int
647 passioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
648 {
649         struct  cam_periph *periph;
650         struct  pass_softc *softc;
651         u_int8_t unit;
652         int      error;
653
654
655         /* unit = dkunit(dev); */
656         /* XXX KDM fix this */
657         unit = minor(dev) & 0xff;
658
659         periph = cam_extend_get(passperiphs, unit);
660
661         if (periph == NULL)
662                 return(ENXIO);
663
664         softc = (struct pass_softc *)periph->softc;
665
666         error = 0;
667
668         switch (cmd) {
669
670         case CAMIOCOMMAND:
671         {
672                 union ccb *inccb;
673                 union ccb *ccb;
674                 int ccb_malloced;
675
676                 inccb = (union ccb *)addr;
677
678                 /*
679                  * Some CCB types, like scan bus and scan lun can only go
680                  * through the transport layer device.
681                  */
682                 if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
683                         xpt_print_path(periph->path);
684                         printf("CCB function code %#x is restricted to the "
685                                "XPT device\n", inccb->ccb_h.func_code);
686                         error = ENODEV;
687                         break;
688                 }
689
690                 /*
691                  * Non-immediate CCBs need a CCB from the per-device pool
692                  * of CCBs, which is scheduled by the transport layer.
693                  * Immediate CCBs and user-supplied CCBs should just be
694                  * malloced.
695                  */
696                 if ((inccb->ccb_h.func_code & XPT_FC_QUEUED)
697                  && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) {
698                         ccb = cam_periph_getccb(periph,
699                                                 inccb->ccb_h.pinfo.priority);
700                         ccb_malloced = 0;
701                 } else {
702                         ccb = xpt_alloc_ccb();
703
704                         if (ccb != NULL)
705                                 xpt_setup_ccb(&ccb->ccb_h, periph->path,
706                                               inccb->ccb_h.pinfo.priority);
707                         ccb_malloced = 1;
708                 }
709
710                 if (ccb == NULL) {
711                         xpt_print_path(periph->path);
712                         printf("unable to allocate CCB\n");
713                         error = ENOMEM;
714                         break;
715                 }
716
717                 error = passsendccb(periph, ccb, inccb);
718
719                 if (ccb_malloced)
720                         xpt_free_ccb(ccb);
721                 else
722                         xpt_release_ccb(ccb);
723
724                 break;
725         }
726         default:
727                 error = cam_periph_ioctl(periph, cmd, addr, passerror);
728                 break;
729         }
730
731         return(error);
732 }
733
734 /*
735  * Generally, "ccb" should be the CCB supplied by the kernel.  "inccb"
736  * should be the CCB that is copied in from the user.
737  */
738 static int
739 passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
740 {
741         struct pass_softc *softc;
742         struct cam_periph_map_info mapinfo;
743         int error, need_unmap;
744
745         softc = (struct pass_softc *)periph->softc;
746
747         need_unmap = 0;
748
749         /*
750          * There are some fields in the CCB header that need to be
751          * preserved, the rest we get from the user.
752          */
753         xpt_merge_ccb(ccb, inccb);
754
755         /*
756          * There's no way for the user to have a completion
757          * function, so we put our own completion function in here.
758          */
759         ccb->ccb_h.cbfcnp = passdone;
760
761         /*
762          * We only attempt to map the user memory into kernel space
763          * if they haven't passed in a physical memory pointer,
764          * and if there is actually an I/O operation to perform.
765          * Right now cam_periph_mapmem() only supports SCSI and device
766          * match CCBs.  For the SCSI CCBs, we only pass the CCB in if
767          * there's actually data to map.  cam_periph_mapmem() will do the
768          * right thing, even if there isn't data to map, but since CCBs
769          * without data are a reasonably common occurance (e.g. test unit
770          * ready), it will save a few cycles if we check for it here.
771          */
772         if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0)
773          && (((ccb->ccb_h.func_code == XPT_SCSI_IO)
774             && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE))
775           || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) {
776
777                 bzero(&mapinfo, sizeof(mapinfo));
778
779                 error = cam_periph_mapmem(ccb, &mapinfo); 
780
781                 /*
782                  * cam_periph_mapmem returned an error, we can't continue.
783                  * Return the error to the user.
784                  */
785                 if (error)
786                         return(error);
787
788                 /*
789                  * We successfully mapped the memory in, so we need to
790                  * unmap it when the transaction is done.
791                  */
792                 need_unmap = 1;
793         }
794
795         /*
796          * If the user wants us to perform any error recovery, then honor
797          * that request.  Otherwise, it's up to the user to perform any
798          * error recovery.
799          */
800         error = cam_periph_runccb(ccb,
801                                   (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ?
802                                   passerror : NULL,
803                                   /* cam_flags */ 0,
804                                   /* sense_flags */SF_RETRY_UA | SF_RETRY_SELTO,
805                                   &softc->device_stats);
806
807         if (need_unmap != 0)
808                 cam_periph_unmapmem(ccb, &mapinfo);
809
810         ccb->ccb_h.cbfcnp = NULL;
811         ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
812         bcopy(ccb, inccb, sizeof(union ccb));
813
814         return(error);
815 }
816
817 static int
818 passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
819 {
820         struct cam_periph *periph;
821         struct pass_softc *softc;
822
823         periph = xpt_path_periph(ccb->ccb_h.path);
824         softc = (struct pass_softc *)periph->softc;
825         
826         return(cam_periph_error(ccb, cam_flags, sense_flags, 
827                                  &softc->saved_ccb));
828 }