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