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