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