xref: /freebsd/sys/cam/scsi/scsi_pass.c (revision a8445737e740901f5f2c8d24c12ef7fc8b00134e)
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$
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 	 * We don't allow nonblocking access.
394 	 */
395 	if ((flags & O_NONBLOCK) != 0) {
396 		printf("%s%d: can't do nonblocking accesss\n",
397 			periph->periph_name,
398 			periph->unit_number);
399 		return(ENODEV);
400 	}
401 
402 	if ((error = cam_periph_lock(periph, PRIBIO | PCATCH)) != 0)
403 		return (error);
404 
405 	if ((softc->flags & PASS_FLAG_OPEN) == 0) {
406 		if (cam_periph_acquire(periph) != CAM_REQ_CMP)
407 			return(ENXIO);
408 		softc->flags |= PASS_FLAG_OPEN;
409 	}
410 
411 	cam_periph_unlock(periph);
412 
413 	return (error);
414 }
415 
416 static int
417 passclose(dev_t dev, int flag, int fmt, struct proc *p)
418 {
419 	struct 	cam_periph *periph;
420 	struct	pass_softc *softc;
421 	int	unit, error;
422 
423 	/* unit = dkunit(dev); */
424 	/* XXX KDM fix this */
425 	unit = minor(dev) & 0xff;
426 
427 	periph = cam_extend_get(passperiphs, unit);
428 	if (periph == NULL)
429 		return (ENXIO);
430 
431 	softc = (struct pass_softc *)periph->softc;
432 
433 	if ((error = cam_periph_lock(periph, PRIBIO)) != 0)
434 		return (error);
435 
436 	softc->flags &= ~PASS_FLAG_OPEN;
437 
438 	cam_periph_unlock(periph);
439 	cam_periph_release(periph);
440 
441 	return (0);
442 }
443 
444 static int
445 passread(dev_t dev, struct uio *uio, int ioflag)
446 {
447 	return(physio(passstrategy, NULL, dev, 1, minphys, uio));
448 }
449 
450 static int
451 passwrite(dev_t dev, struct uio *uio, int ioflag)
452 {
453 	return(physio(passstrategy, NULL, dev, 0, minphys, uio));
454 }
455 
456 /*
457  * Actually translate the requested transfer into one the physical driver
458  * can understand.  The transfer is described by a buf and will include
459  * only one physical transfer.
460  */
461 static void
462 passstrategy(struct buf *bp)
463 {
464 	struct cam_periph *periph;
465 	struct pass_softc *softc;
466 	u_int  unit;
467 	int    s;
468 
469 	/*
470 	 * The read/write interface for the passthrough driver doesn't
471 	 * really work right now.  So, we just pass back EINVAL to tell the
472 	 * user to go away.
473 	 */
474 	bp->b_error = EINVAL;
475 	goto bad;
476 
477 	/* unit = dkunit(bp->b_dev); */
478 	/* XXX KDM fix this */
479 	unit = minor(bp->b_dev) & 0xff;
480 
481 	periph = cam_extend_get(passperiphs, unit);
482 	if (periph == NULL) {
483 		bp->b_error = ENXIO;
484 		goto bad;
485 	}
486 	softc = (struct pass_softc *)periph->softc;
487 
488 	/*
489 	 * Odd number of bytes or negative offset
490 	 */
491 	/* valid request?  */
492 	if (bp->b_blkno < 0) {
493 		bp->b_error = EINVAL;
494 		goto bad;
495         }
496 
497 	/*
498 	 * Mask interrupts so that the pack cannot be invalidated until
499 	 * after we are in the queue.  Otherwise, we might not properly
500 	 * clean up one of the buffers.
501 	 */
502 	s = splbio();
503 
504 	bufq_insert_tail(&softc->buf_queue, bp);
505 
506 	splx(s);
507 
508 	/*
509 	 * Schedule ourselves for performing the work.
510 	 */
511 	xpt_schedule(periph, /* XXX priority */1);
512 
513 	return;
514 bad:
515 	bp->b_flags |= B_ERROR;
516 
517 	/*
518 	 * Correctly set the buf to indicate a completed xfer
519 	 */
520 	bp->b_resid = bp->b_bcount;
521 	biodone(bp);
522 	return;
523 }
524 
525 static void
526 passstart(struct cam_periph *periph, union ccb *start_ccb)
527 {
528 	struct pass_softc *softc;
529 	int s;
530 
531 	softc = (struct pass_softc *)periph->softc;
532 
533 	switch (softc->state) {
534 	case PASS_STATE_NORMAL:
535 	{
536 		struct buf *bp;
537 
538 		s = splbio();
539 		bp = bufq_first(&softc->buf_queue);
540 		if (periph->immediate_priority <= periph->pinfo.priority) {
541 			start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING;
542 			SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
543 					  periph_links.sle);
544 			periph->immediate_priority = CAM_PRIORITY_NONE;
545 			splx(s);
546 			wakeup(&periph->ccb_list);
547 		} else if (bp == NULL) {
548 			splx(s);
549 			xpt_release_ccb(start_ccb);
550 		} else {
551 
552 			bufq_remove(&softc->buf_queue, bp);
553 
554 			devstat_start_transaction(&softc->device_stats);
555 
556 			/*
557 			 * XXX JGibbs -
558 			 * Interpret the contents of the bp as a CCB
559 			 * and pass it to a routine shared by our ioctl
560 			 * code and passtart.
561 			 * For now, just biodone it with EIO so we don't
562 			 * hang.
563 			 */
564 			bp->b_error = EIO;
565 			bp->b_flags |= B_ERROR;
566 			bp->b_resid = bp->b_bcount;
567 			biodone(bp);
568 			bp = bufq_first(&softc->buf_queue);
569 			splx(s);
570 
571 			xpt_action(start_ccb);
572 
573 		}
574 		if (bp != NULL) {
575 			/* Have more work to do, so ensure we stay scheduled */
576 			xpt_schedule(periph, /* XXX priority */1);
577 		}
578 		break;
579 	}
580 	}
581 }
582 static void
583 passdone(struct cam_periph *periph, union ccb *done_ccb)
584 {
585 	struct pass_softc *softc;
586 	struct ccb_scsiio *csio;
587 
588 	softc = (struct pass_softc *)periph->softc;
589 	csio = &done_ccb->csio;
590 	switch (csio->ccb_h.ccb_type) {
591 	case PASS_CCB_BUFFER_IO:
592 	{
593 		struct buf		*bp;
594 		cam_status		status;
595 		u_int8_t		scsi_status;
596 		devstat_trans_flags	ds_flags;
597 
598 		status = done_ccb->ccb_h.status;
599 		scsi_status = done_ccb->csio.scsi_status;
600 		bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
601 		/* XXX handle errors */
602 		if (!(((status & CAM_STATUS_MASK) == CAM_REQ_CMP)
603 		  && (scsi_status == SCSI_STATUS_OK))) {
604 			int error;
605 
606 			if ((error = passerror(done_ccb, 0, 0)) == ERESTART) {
607 				/*
608 				 * A retry was scheuled, so
609 				 * just return.
610 				 */
611 				return;
612 			}
613 
614 			/*
615 			 * XXX unfreeze the queue after we complete
616 			 * the abort process
617 			 */
618 			bp->b_error = error;
619 			bp->b_flags |= B_ERROR;
620 		}
621 
622 		if ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
623 			ds_flags = DEVSTAT_READ;
624 		else if ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
625 			ds_flags = DEVSTAT_WRITE;
626 		else
627 			ds_flags = DEVSTAT_NO_DATA;
628 
629 		devstat_end_transaction(&softc->device_stats, bp->b_bcount,
630 					done_ccb->csio.tag_action & 0xf,
631 					ds_flags);
632 
633 		biodone(bp);
634 		break;
635 	}
636 	case PASS_CCB_WAITING:
637 	{
638 		/* Caller will release the CCB */
639 		wakeup(&done_ccb->ccb_h.cbfcnp);
640 		return;
641 	}
642 	}
643 	xpt_release_ccb(done_ccb);
644 }
645 
646 static int
647 passioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
648 {
649 	struct 	cam_periph *periph;
650 	struct	pass_softc *softc;
651 	u_int8_t unit;
652 	int      error;
653 
654 
655 	/* unit = dkunit(dev); */
656 	/* XXX KDM fix this */
657 	unit = minor(dev) & 0xff;
658 
659 	periph = cam_extend_get(passperiphs, unit);
660 
661 	if (periph == NULL)
662 		return(ENXIO);
663 
664 	softc = (struct pass_softc *)periph->softc;
665 
666 	error = 0;
667 
668 	switch (cmd) {
669 
670 	case CAMIOCOMMAND:
671 	{
672 		union ccb *inccb;
673 		union ccb *ccb;
674 
675 		inccb = (union ccb *)addr;
676 		ccb = cam_periph_getccb(periph, inccb->ccb_h.pinfo.priority);
677 
678 		error = passsendccb(periph, ccb, inccb);
679 
680 		xpt_release_ccb(ccb);
681 
682 		break;
683 	}
684 	default:
685 		error = cam_periph_ioctl(periph, cmd, addr, passerror);
686 		break;
687 	}
688 
689 	return(error);
690 }
691 
692 /*
693  * Generally, "ccb" should be the CCB supplied by the kernel.  "inccb"
694  * should be the CCB that is copied in from the user.
695  */
696 static int
697 passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
698 {
699 	struct buf *bp[2];
700 	struct pass_softc *softc;
701 	struct cam_periph_map_info mapinfo;
702 	int error, need_unmap;
703 
704 	softc = (struct pass_softc *)periph->softc;
705 
706 	need_unmap = 0;
707 
708 	/*
709 	 * There are some fields in the CCB header that need to be
710 	 * preserved, the rest we get from the user.
711 	 */
712 	xpt_merge_ccb(ccb, inccb);
713 
714 	/*
715 	 * There's no way for the user to have a completion
716 	 * function, so we put our own completion function in here.
717 	 */
718 	ccb->ccb_h.cbfcnp = passdone;
719 
720 	/*
721 	 * We only attempt to map the user memory into kernel space
722 	 * if they haven't passed in a physical memory pointer,
723 	 * and if there is actually an I/O operation to perform.
724 	 * Right now cam_periph_mapmem() only supports SCSI and device
725 	 * match CCBs.  For the SCSI CCBs, we only pass the CCB in if
726 	 * there's actually data to map.  cam_periph_mapmem() will do the
727 	 * right thing, even if there isn't data to map, but since CCBs
728 	 * without data are a reasonably common occurance (e.g. test unit
729 	 * ready), it will save a few cycles if we check for it here.
730 	 */
731 	if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0)
732 	 && (((ccb->ccb_h.func_code == XPT_SCSI_IO)
733 	    && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE))
734 	  || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) {
735 
736 		bzero(&mapinfo, sizeof(mapinfo));
737 
738 		error = cam_periph_mapmem(ccb, &mapinfo);
739 
740 		/*
741 		 * cam_periph_mapmem returned an error, we can't continue.
742 		 * Return the error to the user.
743 		 */
744 		if (error)
745 			return(error);
746 
747 		/*
748 		 * We successfully mapped the memory in, so we need to
749 		 * unmap it when the transaction is done.
750 		 */
751 		need_unmap = 1;
752 	}
753 
754 	/*
755 	 * If the user wants us to perform any error recovery, then honor
756 	 * that request.  Otherwise, it's up to the user to perform any
757 	 * error recovery.
758 	 */
759 	error = cam_periph_runccb(ccb,
760 				  (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ?
761 				  passerror : NULL,
762 				  /* cam_flags */ 0,
763 				  /* sense_flags */SF_RETRY_UA,
764 				  &softc->device_stats);
765 
766 	if (need_unmap != 0)
767 		cam_periph_unmapmem(ccb, &mapinfo);
768 
769 	ccb->ccb_h.cbfcnp = NULL;
770 	ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
771 	bcopy(ccb, inccb, sizeof(union ccb));
772 
773 	return(error);
774 }
775 
776 static int
777 passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
778 {
779 	struct cam_periph *periph;
780 	struct pass_softc *softc;
781 
782 	periph = xpt_path_periph(ccb->ccb_h.path);
783 	softc = (struct pass_softc *)periph->softc;
784 
785 	return(cam_periph_error(ccb, cam_flags, sense_flags,
786 				 &softc->saved_ccb));
787 }
788