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