scsi_pass.c revision 46625
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.7 1999/05/06 20:16:05 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	case AC_LOST_DEVICE:
300		cam_periph_invalidate(periph);
301		break;
302	case AC_TRANSFER_NEG:
303	case AC_SENT_BDR:
304	case AC_SCSI_AEN:
305	case AC_UNSOL_RESEL:
306	case AC_BUS_RESET:
307	default:
308		break;
309	}
310}
311
312static cam_status
313passregister(struct cam_periph *periph, void *arg)
314{
315	struct pass_softc *softc;
316	struct ccb_setasync csa;
317	struct ccb_getdev *cgd;
318
319	cgd = (struct ccb_getdev *)arg;
320	if (periph == NULL) {
321		printf("passregister: periph was NULL!!\n");
322		return(CAM_REQ_CMP_ERR);
323	}
324
325	if (cgd == NULL) {
326		printf("passregister: no getdev CCB, can't register device\n");
327		return(CAM_REQ_CMP_ERR);
328	}
329
330	softc = (struct pass_softc *)malloc(sizeof(*softc),
331					    M_DEVBUF, M_NOWAIT);
332
333	if (softc == NULL) {
334		printf("passregister: Unable to probe new device. "
335		       "Unable to allocate softc\n");
336		return(CAM_REQ_CMP_ERR);
337	}
338
339	bzero(softc, sizeof(*softc));
340	softc->state = PASS_STATE_NORMAL;
341	softc->pd_type = cgd->pd_type;
342	bufq_init(&softc->buf_queue);
343
344	periph->softc = softc;
345
346	cam_extend_set(passperiphs, periph->unit_number, periph);
347	/*
348	 * We pass in 0 for a blocksize, since we don't
349	 * know what the blocksize of this device is, if
350	 * it even has a blocksize.
351	 */
352	devstat_add_entry(&softc->device_stats, "pass", periph->unit_number,
353			  0, DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
354			  cgd->pd_type |
355			  DEVSTAT_TYPE_IF_SCSI |
356			  DEVSTAT_TYPE_PASS,
357			  DEVSTAT_PRIORITY_PASS);
358	/*
359	 * Add an async callback so that we get
360	 * notified if this device goes away.
361	 */
362	xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
363	csa.ccb_h.func_code = XPT_SASYNC_CB;
364	csa.event_enable = AC_LOST_DEVICE;
365	csa.callback = passasync;
366	csa.callback_arg = periph;
367	xpt_action((union ccb *)&csa);
368
369	if (bootverbose)
370		xpt_announce_periph(periph, NULL);
371
372	return(CAM_REQ_CMP);
373}
374
375static int
376passopen(dev_t dev, int flags, int fmt, struct proc *p)
377{
378	struct cam_periph *periph;
379	struct pass_softc *softc;
380	int unit, error;
381	int s;
382
383	error = 0; /* default to no error */
384
385	/* unit = dkunit(dev); */
386	/* XXX KDM fix this */
387	unit = minor(dev) & 0xff;
388
389	periph = cam_extend_get(passperiphs, unit);
390
391	if (periph == NULL)
392		return (ENXIO);
393
394	softc = (struct pass_softc *)periph->softc;
395
396	s = splsoftcam();
397	if (softc->flags & PASS_FLAG_INVALID) {
398		splx(s);
399		return(ENXIO);
400	}
401
402	/*
403	 * Don't allow access when we're running at a high securelvel.
404	 */
405	if (securelevel > 1) {
406		splx(s);
407		return(EPERM);
408	}
409
410	/*
411	 * Only allow read-write access.
412	 */
413	if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) {
414		splx(s);
415		return(EPERM);
416	}
417
418	/*
419	 * We don't allow nonblocking access.
420	 */
421	if ((flags & O_NONBLOCK) != 0) {
422		xpt_print_path(periph->path);
423		printf("can't do nonblocking accesss\n");
424		splx(s);
425		return(EINVAL);
426	}
427
428	if ((error = cam_periph_lock(periph, PRIBIO | PCATCH)) != 0) {
429		splx(s);
430		return (error);
431	}
432
433	splx(s);
434
435	if ((softc->flags & PASS_FLAG_OPEN) == 0) {
436		if (cam_periph_acquire(periph) != CAM_REQ_CMP)
437			return(ENXIO);
438		softc->flags |= PASS_FLAG_OPEN;
439	}
440
441	cam_periph_unlock(periph);
442
443	return (error);
444}
445
446static int
447passclose(dev_t dev, int flag, int fmt, struct proc *p)
448{
449	struct 	cam_periph *periph;
450	struct	pass_softc *softc;
451	int	unit, error;
452
453	/* unit = dkunit(dev); */
454	/* XXX KDM fix this */
455	unit = minor(dev) & 0xff;
456
457	periph = cam_extend_get(passperiphs, unit);
458	if (periph == NULL)
459		return (ENXIO);
460
461	softc = (struct pass_softc *)periph->softc;
462
463	if ((error = cam_periph_lock(periph, PRIBIO)) != 0)
464		return (error);
465
466	softc->flags &= ~PASS_FLAG_OPEN;
467
468	cam_periph_unlock(periph);
469	cam_periph_release(periph);
470
471	return (0);
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 */
479static void
480passstrategy(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;
532bad:
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
543static void
544passstart(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}
600static void
601passdone(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
664static int
665passioctl(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		int ccb_malloced;
693
694		inccb = (union ccb *)addr;
695
696		/*
697		 * Some CCB types, like scan bus and scan lun can only go
698		 * through the transport layer device.
699		 */
700		if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
701			xpt_print_path(periph->path);
702			printf("CCB function code %#x is restricted to the "
703			       "XPT device\n", inccb->ccb_h.func_code);
704			error = ENODEV;
705			break;
706		}
707
708		/*
709		 * Non-immediate CCBs need a CCB from the per-device pool
710		 * of CCBs, which is scheduled by the transport layer.
711		 * Immediate CCBs and user-supplied CCBs should just be
712		 * malloced.
713		 */
714		if ((inccb->ccb_h.func_code & XPT_FC_QUEUED)
715		 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) {
716			ccb = cam_periph_getccb(periph,
717						inccb->ccb_h.pinfo.priority);
718			ccb_malloced = 0;
719		} else {
720			ccb = xpt_alloc_ccb();
721
722			if (ccb != NULL)
723				xpt_setup_ccb(&ccb->ccb_h, periph->path,
724					      inccb->ccb_h.pinfo.priority);
725			ccb_malloced = 1;
726		}
727
728		if (ccb == NULL) {
729			xpt_print_path(periph->path);
730			printf("unable to allocate CCB\n");
731			error = ENOMEM;
732			break;
733		}
734
735		error = passsendccb(periph, ccb, inccb);
736
737		if (ccb_malloced)
738			xpt_free_ccb(ccb);
739		else
740			xpt_release_ccb(ccb);
741
742		break;
743	}
744	default:
745		error = cam_periph_ioctl(periph, cmd, addr, passerror);
746		break;
747	}
748
749	return(error);
750}
751
752/*
753 * Generally, "ccb" should be the CCB supplied by the kernel.  "inccb"
754 * should be the CCB that is copied in from the user.
755 */
756static int
757passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
758{
759	struct pass_softc *softc;
760	struct cam_periph_map_info mapinfo;
761	int error, need_unmap;
762
763	softc = (struct pass_softc *)periph->softc;
764
765	need_unmap = 0;
766
767	/*
768	 * There are some fields in the CCB header that need to be
769	 * preserved, the rest we get from the user.
770	 */
771	xpt_merge_ccb(ccb, inccb);
772
773	/*
774	 * There's no way for the user to have a completion
775	 * function, so we put our own completion function in here.
776	 */
777	ccb->ccb_h.cbfcnp = passdone;
778
779	/*
780	 * We only attempt to map the user memory into kernel space
781	 * if they haven't passed in a physical memory pointer,
782	 * and if there is actually an I/O operation to perform.
783	 * Right now cam_periph_mapmem() only supports SCSI and device
784	 * match CCBs.  For the SCSI CCBs, we only pass the CCB in if
785	 * there's actually data to map.  cam_periph_mapmem() will do the
786	 * right thing, even if there isn't data to map, but since CCBs
787	 * without data are a reasonably common occurance (e.g. test unit
788	 * ready), it will save a few cycles if we check for it here.
789	 */
790	if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0)
791	 && (((ccb->ccb_h.func_code == XPT_SCSI_IO)
792	    && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE))
793	  || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) {
794
795		bzero(&mapinfo, sizeof(mapinfo));
796
797		error = cam_periph_mapmem(ccb, &mapinfo);
798
799		/*
800		 * cam_periph_mapmem returned an error, we can't continue.
801		 * Return the error to the user.
802		 */
803		if (error)
804			return(error);
805
806		/*
807		 * We successfully mapped the memory in, so we need to
808		 * unmap it when the transaction is done.
809		 */
810		need_unmap = 1;
811	}
812
813	/*
814	 * If the user wants us to perform any error recovery, then honor
815	 * that request.  Otherwise, it's up to the user to perform any
816	 * error recovery.
817	 */
818	error = cam_periph_runccb(ccb,
819				  (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ?
820				  passerror : NULL,
821				  /* cam_flags */ 0,
822				  /* sense_flags */SF_RETRY_UA,
823				  &softc->device_stats);
824
825	if (need_unmap != 0)
826		cam_periph_unmapmem(ccb, &mapinfo);
827
828	ccb->ccb_h.cbfcnp = NULL;
829	ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
830	bcopy(ccb, inccb, sizeof(union ccb));
831
832	return(error);
833}
834
835static int
836passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
837{
838	struct cam_periph *periph;
839	struct pass_softc *softc;
840
841	periph = xpt_path_periph(ccb->ccb_h.path);
842	softc = (struct pass_softc *)periph->softc;
843
844	return(cam_periph_error(ccb, cam_flags, sense_flags,
845				 &softc->saved_ccb));
846}
847