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