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