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