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