xref: /freebsd/sys/cam/scsi/scsi_pass.c (revision 4fd0d10e0fe684211328bc148edf89a792425b39)
1 /*-
2  * Copyright (c) 1997, 1998, 2000 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 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/types.h>
35 #include <sys/bio.h>
36 #include <sys/malloc.h>
37 #include <sys/fcntl.h>
38 #include <sys/conf.h>
39 #include <sys/errno.h>
40 #include <sys/devicestat.h>
41 #include <sys/proc.h>
42 #include <sys/taskqueue.h>
43 
44 #include <cam/cam.h>
45 #include <cam/cam_ccb.h>
46 #include <cam/cam_periph.h>
47 #include <cam/cam_queue.h>
48 #include <cam/cam_xpt_periph.h>
49 #include <cam/cam_debug.h>
50 #include <cam/cam_sim.h>
51 #include <cam/cam_compat.h>
52 
53 #include <cam/scsi/scsi_all.h>
54 #include <cam/scsi/scsi_pass.h>
55 
56 typedef enum {
57 	PASS_FLAG_OPEN			= 0x01,
58 	PASS_FLAG_LOCKED		= 0x02,
59 	PASS_FLAG_INVALID		= 0x04,
60 	PASS_FLAG_INITIAL_PHYSPATH	= 0x08
61 } pass_flags;
62 
63 typedef enum {
64 	PASS_STATE_NORMAL
65 } pass_state;
66 
67 typedef enum {
68 	PASS_CCB_BUFFER_IO,
69 	PASS_CCB_WAITING
70 } pass_ccb_types;
71 
72 #define ccb_type	ppriv_field0
73 #define ccb_bp		ppriv_ptr1
74 
75 struct pass_softc {
76 	pass_state	 state;
77 	pass_flags	 flags;
78 	u_int8_t	 pd_type;
79 	union ccb	 saved_ccb;
80 	int		 open_count;
81 	struct devstat	*device_stats;
82 	struct cdev	*dev;
83 	struct cdev	*alias_dev;
84 	struct task	 add_physpath_task;
85 };
86 
87 
88 static	d_open_t	passopen;
89 static	d_close_t	passclose;
90 static	d_ioctl_t	passioctl;
91 static	d_ioctl_t	passdoioctl;
92 
93 static	periph_init_t	passinit;
94 static	periph_ctor_t	passregister;
95 static	periph_oninv_t	passoninvalidate;
96 static	periph_dtor_t	passcleanup;
97 static	periph_start_t	passstart;
98 static void		pass_add_physpath(void *context, int pending);
99 static	void		passasync(void *callback_arg, u_int32_t code,
100 				  struct cam_path *path, void *arg);
101 static	void		passdone(struct cam_periph *periph,
102 				 union ccb *done_ccb);
103 static	int		passerror(union ccb *ccb, u_int32_t cam_flags,
104 				  u_int32_t sense_flags);
105 static 	int		passsendccb(struct cam_periph *periph, union ccb *ccb,
106 				    union ccb *inccb);
107 
108 static struct periph_driver passdriver =
109 {
110 	passinit, "pass",
111 	TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0
112 };
113 
114 PERIPHDRIVER_DECLARE(pass, passdriver);
115 
116 static struct cdevsw pass_cdevsw = {
117 	.d_version =	D_VERSION,
118 	.d_flags =	D_TRACKCLOSE,
119 	.d_open =	passopen,
120 	.d_close =	passclose,
121 	.d_ioctl =	passioctl,
122 	.d_name =	"pass",
123 };
124 
125 static void
126 passinit(void)
127 {
128 	cam_status status;
129 
130 	/*
131 	 * Install a global async callback.  This callback will
132 	 * receive async callbacks like "new device found".
133 	 */
134 	status = xpt_register_async(AC_FOUND_DEVICE, passasync, NULL, NULL);
135 
136 	if (status != CAM_REQ_CMP) {
137 		printf("pass: Failed to attach master async callback "
138 		       "due to status 0x%x!\n", status);
139 	}
140 
141 }
142 
143 static void
144 passdevgonecb(void *arg)
145 {
146 	struct cam_sim    *sim;
147 	struct cam_periph *periph;
148 	struct pass_softc *softc;
149 	int i;
150 
151 	periph = (struct cam_periph *)arg;
152 	sim = periph->sim;
153 	softc = (struct pass_softc *)periph->softc;
154 
155 	KASSERT(softc->open_count >= 0, ("Negative open count %d",
156 		softc->open_count));
157 
158 	mtx_lock(sim->mtx);
159 
160 	/*
161 	 * When we get this callback, we will get no more close calls from
162 	 * devfs.  So if we have any dangling opens, we need to release the
163 	 * reference held for that particular context.
164 	 */
165 	for (i = 0; i < softc->open_count; i++)
166 		cam_periph_release_locked(periph);
167 
168 	softc->open_count = 0;
169 
170 	/*
171 	 * Release the reference held for the device node, it is gone now.
172 	 */
173 	cam_periph_release_locked(periph);
174 
175 	/*
176 	 * We reference the SIM lock directly here, instead of using
177 	 * cam_periph_unlock().  The reason is that the final call to
178 	 * cam_periph_release_locked() above could result in the periph
179 	 * getting freed.  If that is the case, dereferencing the periph
180 	 * with a cam_periph_unlock() call would cause a page fault.
181 	 */
182 	mtx_unlock(sim->mtx);
183 }
184 
185 static void
186 passoninvalidate(struct cam_periph *periph)
187 {
188 	struct pass_softc *softc;
189 
190 	softc = (struct pass_softc *)periph->softc;
191 
192 	/*
193 	 * De-register any async callbacks.
194 	 */
195 	xpt_register_async(0, passasync, periph, periph->path);
196 
197 	softc->flags |= PASS_FLAG_INVALID;
198 
199 	/*
200 	 * Tell devfs this device has gone away, and ask for a callback
201 	 * when it has cleaned up its state.
202 	 */
203 	destroy_dev_sched_cb(softc->dev, passdevgonecb, periph);
204 
205 	/*
206 	 * XXX Return all queued I/O with ENXIO.
207 	 * XXX Handle any transactions queued to the card
208 	 *     with XPT_ABORT_CCB.
209 	 */
210 
211 	if (bootverbose) {
212 		xpt_print(periph->path, "lost device\n");
213 	}
214 
215 }
216 
217 static void
218 passcleanup(struct cam_periph *periph)
219 {
220 	struct pass_softc *softc;
221 
222 	softc = (struct pass_softc *)periph->softc;
223 
224 	if (bootverbose)
225 		xpt_print(periph->path, "removing device entry\n");
226 	devstat_remove_entry(softc->device_stats);
227 
228 	cam_periph_unlock(periph);
229 	taskqueue_drain(taskqueue_thread, &softc->add_physpath_task);
230 
231 	cam_periph_lock(periph);
232 
233 	free(softc, M_DEVBUF);
234 }
235 
236 static void
237 pass_add_physpath(void *context, int pending)
238 {
239 	struct cam_periph *periph;
240 	struct pass_softc *softc;
241 	char *physpath;
242 
243 	/*
244 	 * If we have one, create a devfs alias for our
245 	 * physical path.
246 	 */
247 	periph = context;
248 	softc = periph->softc;
249 	physpath = malloc(MAXPATHLEN, M_DEVBUF, M_WAITOK);
250 	cam_periph_lock(periph);
251 	if (periph->flags & CAM_PERIPH_INVALID) {
252 		cam_periph_unlock(periph);
253 		goto out;
254 	}
255 	if (xpt_getattr(physpath, MAXPATHLEN,
256 			"GEOM::physpath", periph->path) == 0
257 	 && strlen(physpath) != 0) {
258 
259 		cam_periph_unlock(periph);
260 		make_dev_physpath_alias(MAKEDEV_WAITOK, &softc->alias_dev,
261 					softc->dev, softc->alias_dev, physpath);
262 		cam_periph_lock(periph);
263 	}
264 
265 	/*
266 	 * Now that we've made our alias, we no longer have to have a
267 	 * reference to the device.
268 	 */
269 	if ((softc->flags & PASS_FLAG_INITIAL_PHYSPATH) == 0) {
270 		softc->flags |= PASS_FLAG_INITIAL_PHYSPATH;
271 		cam_periph_unlock(periph);
272 		dev_rel(softc->dev);
273 	}
274 	else
275 		cam_periph_unlock(periph);
276 
277 out:
278 	free(physpath, M_DEVBUF);
279 }
280 
281 static void
282 passasync(void *callback_arg, u_int32_t code,
283 	  struct cam_path *path, void *arg)
284 {
285 	struct cam_periph *periph;
286 
287 	periph = (struct cam_periph *)callback_arg;
288 
289 	switch (code) {
290 	case AC_FOUND_DEVICE:
291 	{
292 		struct ccb_getdev *cgd;
293 		cam_status status;
294 
295 		cgd = (struct ccb_getdev *)arg;
296 		if (cgd == NULL)
297 			break;
298 
299 		/*
300 		 * Allocate a peripheral instance for
301 		 * this device and start the probe
302 		 * process.
303 		 */
304 		status = cam_periph_alloc(passregister, passoninvalidate,
305 					  passcleanup, passstart, "pass",
306 					  CAM_PERIPH_BIO, cgd->ccb_h.path,
307 					  passasync, AC_FOUND_DEVICE, cgd);
308 
309 		if (status != CAM_REQ_CMP
310 		 && status != CAM_REQ_INPROG) {
311 			const struct cam_status_entry *entry;
312 
313 			entry = cam_fetch_status_entry(status);
314 
315 			printf("passasync: Unable to attach new device "
316 			       "due to status %#x: %s\n", status, entry ?
317 			       entry->status_text : "Unknown");
318 		}
319 
320 		break;
321 	}
322 	case AC_ADVINFO_CHANGED:
323 	{
324 		uintptr_t buftype;
325 
326 		buftype = (uintptr_t)arg;
327 		if (buftype == CDAI_TYPE_PHYS_PATH) {
328 			struct pass_softc *softc;
329 
330 			softc = (struct pass_softc *)periph->softc;
331 			taskqueue_enqueue(taskqueue_thread,
332 					  &softc->add_physpath_task);
333 		}
334 		break;
335 	}
336 	default:
337 		cam_periph_async(periph, code, path, arg);
338 		break;
339 	}
340 }
341 
342 static cam_status
343 passregister(struct cam_periph *periph, void *arg)
344 {
345 	struct pass_softc *softc;
346 	struct ccb_getdev *cgd;
347 	struct ccb_pathinq cpi;
348 	int    no_tags;
349 
350 	cgd = (struct ccb_getdev *)arg;
351 	if (cgd == NULL) {
352 		printf("%s: no getdev CCB, can't register device\n", __func__);
353 		return(CAM_REQ_CMP_ERR);
354 	}
355 
356 	softc = (struct pass_softc *)malloc(sizeof(*softc),
357 					    M_DEVBUF, M_NOWAIT);
358 
359 	if (softc == NULL) {
360 		printf("%s: Unable to probe new device. "
361 		       "Unable to allocate softc\n", __func__);
362 		return(CAM_REQ_CMP_ERR);
363 	}
364 
365 	bzero(softc, sizeof(*softc));
366 	softc->state = PASS_STATE_NORMAL;
367 	if (cgd->protocol == PROTO_SCSI || cgd->protocol == PROTO_ATAPI)
368 		softc->pd_type = SID_TYPE(&cgd->inq_data);
369 	else if (cgd->protocol == PROTO_SATAPM)
370 		softc->pd_type = T_ENCLOSURE;
371 	else
372 		softc->pd_type = T_DIRECT;
373 
374 	periph->softc = softc;
375 
376 	bzero(&cpi, sizeof(cpi));
377 	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
378 	cpi.ccb_h.func_code = XPT_PATH_INQ;
379 	xpt_action((union ccb *)&cpi);
380 
381 	/*
382 	 * We pass in 0 for a blocksize, since we don't
383 	 * know what the blocksize of this device is, if
384 	 * it even has a blocksize.
385 	 */
386 	cam_periph_unlock(periph);
387 	no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0;
388 	softc->device_stats = devstat_new_entry("pass",
389 			  periph->unit_number, 0,
390 			  DEVSTAT_NO_BLOCKSIZE
391 			  | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0),
392 			  softc->pd_type |
393 			  XPORT_DEVSTAT_TYPE(cpi.transport) |
394 			  DEVSTAT_TYPE_PASS,
395 			  DEVSTAT_PRIORITY_PASS);
396 
397 	/*
398 	 * Acquire a reference to the periph before we create the devfs
399 	 * instance for it.  We'll release this reference once the devfs
400 	 * instance has been freed.
401 	 */
402 	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
403 		xpt_print(periph->path, "%s: lost periph during "
404 			  "registration!\n", __func__);
405 		cam_periph_lock(periph);
406 		return (CAM_REQ_CMP_ERR);
407 	}
408 
409 	/* Register the device */
410 	softc->dev = make_dev(&pass_cdevsw, periph->unit_number,
411 			      UID_ROOT, GID_OPERATOR, 0600, "%s%d",
412 			      periph->periph_name, periph->unit_number);
413 
414 	/*
415 	 * Now that we have made the devfs instance, hold a reference to it
416 	 * until the task queue has run to setup the physical path alias.
417 	 * That way devfs won't get rid of the device before we add our
418 	 * alias.
419 	 */
420 	dev_ref(softc->dev);
421 
422 	cam_periph_lock(periph);
423 	softc->dev->si_drv1 = periph;
424 
425 	TASK_INIT(&softc->add_physpath_task, /*priority*/0,
426 		  pass_add_physpath, periph);
427 
428 	/*
429 	 * See if physical path information is already available.
430 	 */
431 	taskqueue_enqueue(taskqueue_thread, &softc->add_physpath_task);
432 
433 	/*
434 	 * Add an async callback so that we get notified if
435 	 * this device goes away or its physical path
436 	 * (stored in the advanced info data of the EDT) has
437 	 * changed.
438 	 */
439 	xpt_register_async(AC_LOST_DEVICE | AC_ADVINFO_CHANGED,
440 			   passasync, periph, periph->path);
441 
442 	if (bootverbose)
443 		xpt_announce_periph(periph, NULL);
444 
445 	return(CAM_REQ_CMP);
446 }
447 
448 static int
449 passopen(struct cdev *dev, int flags, int fmt, struct thread *td)
450 {
451 	struct cam_periph *periph;
452 	struct pass_softc *softc;
453 	int error;
454 
455 	periph = (struct cam_periph *)dev->si_drv1;
456 	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
457 		return (ENXIO);
458 
459 	cam_periph_lock(periph);
460 
461 	softc = (struct pass_softc *)periph->softc;
462 
463 	if (softc->flags & PASS_FLAG_INVALID) {
464 		cam_periph_release_locked(periph);
465 		cam_periph_unlock(periph);
466 		return(ENXIO);
467 	}
468 
469 	/*
470 	 * Don't allow access when we're running at a high securelevel.
471 	 */
472 	error = securelevel_gt(td->td_ucred, 1);
473 	if (error) {
474 		cam_periph_release_locked(periph);
475 		cam_periph_unlock(periph);
476 		return(error);
477 	}
478 
479 	/*
480 	 * Only allow read-write access.
481 	 */
482 	if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) {
483 		cam_periph_release_locked(periph);
484 		cam_periph_unlock(periph);
485 		return(EPERM);
486 	}
487 
488 	/*
489 	 * We don't allow nonblocking access.
490 	 */
491 	if ((flags & O_NONBLOCK) != 0) {
492 		xpt_print(periph->path, "can't do nonblocking access\n");
493 		cam_periph_release_locked(periph);
494 		cam_periph_unlock(periph);
495 		return(EINVAL);
496 	}
497 
498 	softc->open_count++;
499 
500 	cam_periph_unlock(periph);
501 
502 	return (error);
503 }
504 
505 static int
506 passclose(struct cdev *dev, int flag, int fmt, struct thread *td)
507 {
508 	struct  cam_sim    *sim;
509 	struct 	cam_periph *periph;
510 	struct  pass_softc *softc;
511 
512 	periph = (struct cam_periph *)dev->si_drv1;
513 	if (periph == NULL)
514 		return (ENXIO);
515 
516 	sim = periph->sim;
517 	softc = periph->softc;
518 
519 	mtx_lock(sim->mtx);
520 
521 	softc->open_count--;
522 
523 	cam_periph_release_locked(periph);
524 
525 	/*
526 	 * We reference the SIM lock directly here, instead of using
527 	 * cam_periph_unlock().  The reason is that the call to
528 	 * cam_periph_release_locked() above could result in the periph
529 	 * getting freed.  If that is the case, dereferencing the periph
530 	 * with a cam_periph_unlock() call would cause a page fault.
531 	 *
532 	 * cam_periph_release() avoids this problem using the same method,
533 	 * but we're manually acquiring and dropping the lock here to
534 	 * protect the open count and avoid another lock acquisition and
535 	 * release.
536 	 */
537 	mtx_unlock(sim->mtx);
538 
539 	return (0);
540 }
541 
542 static void
543 passstart(struct cam_periph *periph, union ccb *start_ccb)
544 {
545 	struct pass_softc *softc;
546 
547 	softc = (struct pass_softc *)periph->softc;
548 
549 	switch (softc->state) {
550 	case PASS_STATE_NORMAL:
551 		start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING;
552 		SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
553 				  periph_links.sle);
554 		periph->immediate_priority = CAM_PRIORITY_NONE;
555 		wakeup(&periph->ccb_list);
556 		break;
557 	}
558 }
559 
560 static void
561 passdone(struct cam_periph *periph, union ccb *done_ccb)
562 {
563 	struct pass_softc *softc;
564 	struct ccb_scsiio *csio;
565 
566 	softc = (struct pass_softc *)periph->softc;
567 	csio = &done_ccb->csio;
568 	switch (csio->ccb_h.ccb_type) {
569 	case PASS_CCB_WAITING:
570 		/* Caller will release the CCB */
571 		wakeup(&done_ccb->ccb_h.cbfcnp);
572 		return;
573 	}
574 	xpt_release_ccb(done_ccb);
575 }
576 
577 static int
578 passioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
579 {
580 	int error;
581 
582 	if ((error = passdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
583 		error = cam_compat_ioctl(dev, &cmd, &addr, &flag, td);
584 		if (error == EAGAIN)
585 			return (passdoioctl(dev, cmd, addr, flag, td));
586 	}
587 	return (error);
588 }
589 
590 static int
591 passdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
592 {
593 	struct	cam_periph *periph;
594 	struct	pass_softc *softc;
595 	int	error;
596 	uint32_t priority;
597 
598 	periph = (struct cam_periph *)dev->si_drv1;
599 	if (periph == NULL)
600 		return(ENXIO);
601 
602 	cam_periph_lock(periph);
603 	softc = (struct pass_softc *)periph->softc;
604 
605 	error = 0;
606 
607 	switch (cmd) {
608 
609 	case CAMIOCOMMAND:
610 	{
611 		union ccb *inccb;
612 		union ccb *ccb;
613 		int ccb_malloced;
614 
615 		inccb = (union ccb *)addr;
616 
617 		/*
618 		 * Some CCB types, like scan bus and scan lun can only go
619 		 * through the transport layer device.
620 		 */
621 		if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
622 			xpt_print(periph->path, "CCB function code %#x is "
623 			    "restricted to the XPT device\n",
624 			    inccb->ccb_h.func_code);
625 			error = ENODEV;
626 			break;
627 		}
628 
629 		/* Compatibility for RL/priority-unaware code. */
630 		priority = inccb->ccb_h.pinfo.priority;
631 		if (priority <= CAM_PRIORITY_OOB)
632 		    priority += CAM_PRIORITY_OOB + 1;
633 
634 		/*
635 		 * Non-immediate CCBs need a CCB from the per-device pool
636 		 * of CCBs, which is scheduled by the transport layer.
637 		 * Immediate CCBs and user-supplied CCBs should just be
638 		 * malloced.
639 		 */
640 		if ((inccb->ccb_h.func_code & XPT_FC_QUEUED)
641 		 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) {
642 			ccb = cam_periph_getccb(periph, priority);
643 			ccb_malloced = 0;
644 		} else {
645 			ccb = xpt_alloc_ccb_nowait();
646 
647 			if (ccb != NULL)
648 				xpt_setup_ccb(&ccb->ccb_h, periph->path,
649 					      priority);
650 			ccb_malloced = 1;
651 		}
652 
653 		if (ccb == NULL) {
654 			xpt_print(periph->path, "unable to allocate CCB\n");
655 			error = ENOMEM;
656 			break;
657 		}
658 
659 		error = passsendccb(periph, ccb, inccb);
660 
661 		if (ccb_malloced)
662 			xpt_free_ccb(ccb);
663 		else
664 			xpt_release_ccb(ccb);
665 
666 		break;
667 	}
668 	default:
669 		error = cam_periph_ioctl(periph, cmd, addr, passerror);
670 		break;
671 	}
672 
673 	cam_periph_unlock(periph);
674 	return(error);
675 }
676 
677 /*
678  * Generally, "ccb" should be the CCB supplied by the kernel.  "inccb"
679  * should be the CCB that is copied in from the user.
680  */
681 static int
682 passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
683 {
684 	struct pass_softc *softc;
685 	struct cam_periph_map_info mapinfo;
686 	xpt_opcode fc;
687 	int error;
688 
689 	softc = (struct pass_softc *)periph->softc;
690 
691 	/*
692 	 * There are some fields in the CCB header that need to be
693 	 * preserved, the rest we get from the user.
694 	 */
695 	xpt_merge_ccb(ccb, inccb);
696 
697 	/*
698 	 * There's no way for the user to have a completion
699 	 * function, so we put our own completion function in here.
700 	 */
701 	ccb->ccb_h.cbfcnp = passdone;
702 
703 	/*
704 	 * Let cam_periph_mapmem do a sanity check on the data pointer format.
705 	 * Even if no data transfer is needed, it's a cheap check and it
706 	 * simplifies the code.
707 	 */
708 	fc = ccb->ccb_h.func_code;
709 	if ((fc == XPT_SCSI_IO) || (fc == XPT_ATA_IO) || (fc == XPT_SMP_IO)
710 	 || (fc == XPT_DEV_MATCH) || (fc == XPT_DEV_ADVINFO)) {
711 		bzero(&mapinfo, sizeof(mapinfo));
712 
713 		/*
714 		 * cam_periph_mapmem calls into proc and vm functions that can
715 		 * sleep as well as trigger I/O, so we can't hold the lock.
716 		 * Dropping it here is reasonably safe.
717 		 */
718 		cam_periph_unlock(periph);
719 		error = cam_periph_mapmem(ccb, &mapinfo);
720 		cam_periph_lock(periph);
721 
722 		/*
723 		 * cam_periph_mapmem returned an error, we can't continue.
724 		 * Return the error to the user.
725 		 */
726 		if (error)
727 			return(error);
728 	} else
729 		/* Ensure that the unmap call later on is a no-op. */
730 		mapinfo.num_bufs_used = 0;
731 
732 	/*
733 	 * If the user wants us to perform any error recovery, then honor
734 	 * that request.  Otherwise, it's up to the user to perform any
735 	 * error recovery.
736 	 */
737 	cam_periph_runccb(ccb, passerror, /* cam_flags */ CAM_RETRY_SELTO,
738 	    /* sense_flags */ ((ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ?
739 	     SF_RETRY_UA : SF_NO_RECOVERY) | SF_NO_PRINT,
740 	    softc->device_stats);
741 
742 	cam_periph_unmapmem(ccb, &mapinfo);
743 
744 	ccb->ccb_h.cbfcnp = NULL;
745 	ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
746 	bcopy(ccb, inccb, sizeof(union ccb));
747 
748 	return(0);
749 }
750 
751 static int
752 passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
753 {
754 	struct cam_periph *periph;
755 	struct pass_softc *softc;
756 
757 	periph = xpt_path_periph(ccb->ccb_h.path);
758 	softc = (struct pass_softc *)periph->softc;
759 
760 	return(cam_periph_error(ccb, cam_flags, sense_flags,
761 				 &softc->saved_ccb));
762 }
763