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