xref: /dragonfly/sys/bus/cam/cam_periph.c (revision dcd37f7d)
1 /*
2  * Common functions for CAM "type" (peripheral) drivers.
3  *
4  * Copyright (c) 1997, 1998 Justin T. Gibbs.
5  * Copyright (c) 1997, 1998, 1999, 2000 Kenneth D. Merry.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification, immediately at the beginning of the file.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sys/cam/cam_periph.c,v 1.70 2008/02/12 11:07:33 raj Exp $
30  * $DragonFly: src/sys/bus/cam/cam_periph.c,v 1.41 2008/07/18 00:07:21 dillon Exp $
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/types.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/buf.h>
40 #include <sys/proc.h>
41 #include <sys/devicestat.h>
42 #include <sys/bus.h>
43 #include <vm/vm.h>
44 #include <vm/vm_extern.h>
45 
46 #include <sys/thread2.h>
47 
48 #include "cam.h"
49 #include "cam_ccb.h"
50 #include "cam_xpt_periph.h"
51 #include "cam_periph.h"
52 #include "cam_debug.h"
53 #include "cam_sim.h"
54 
55 #include <bus/cam/scsi/scsi_all.h>
56 #include <bus/cam/scsi/scsi_message.h>
57 #include <bus/cam/scsi/scsi_pass.h>
58 
59 static	u_int		camperiphnextunit(struct periph_driver *p_drv,
60 					  u_int newunit, int wired,
61 					  path_id_t pathid, target_id_t target,
62 					  lun_id_t lun);
63 static	u_int		camperiphunit(struct periph_driver *p_drv,
64 				      struct cam_sim *sim, path_id_t pathid,
65 				      target_id_t target, lun_id_t lun);
66 static	void		camperiphdone(struct cam_periph *periph,
67 					union ccb *done_ccb);
68 static  void		camperiphfree(struct cam_periph *periph);
69 static int		camperiphscsistatuserror(union ccb *ccb,
70 						 cam_flags camflags,
71 						 u_int32_t sense_flags,
72 						 union ccb *save_ccb,
73 						 int *openings,
74 						 u_int32_t *relsim_flags,
75 						 u_int32_t *timeout);
76 static	int		camperiphscsisenseerror(union ccb *ccb,
77 					        cam_flags camflags,
78 					        u_int32_t sense_flags,
79 					        union ccb *save_ccb,
80 					        int *openings,
81 					        u_int32_t *relsim_flags,
82 					        u_int32_t *timeout);
83 static void cam_periph_unmapbufs(struct cam_periph_map_info *mapinfo,
84 				 u_int8_t ***data_ptrs, int numbufs);
85 
86 static int nperiph_drivers;
87 struct periph_driver **periph_drivers;
88 
89 MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers");
90 
91 static int periph_selto_delay = 1000;
92 TUNABLE_INT("kern.cam.periph_selto_delay", &periph_selto_delay);
93 static int periph_noresrc_delay = 500;
94 TUNABLE_INT("kern.cam.periph_noresrc_delay", &periph_noresrc_delay);
95 static int periph_busy_delay = 500;
96 TUNABLE_INT("kern.cam.periph_busy_delay", &periph_busy_delay);
97 
98 
99 void
100 periphdriver_register(void *data)
101 {
102 	struct periph_driver **newdrivers, **old;
103 	int ndrivers;
104 
105 	ndrivers = nperiph_drivers + 2;
106 	newdrivers = kmalloc(sizeof(*newdrivers) * ndrivers, M_CAMPERIPH,
107 			     M_WAITOK);
108 	if (periph_drivers)
109 		bcopy(periph_drivers, newdrivers,
110 		      sizeof(*newdrivers) * nperiph_drivers);
111 	newdrivers[nperiph_drivers] = (struct periph_driver *)data;
112 	newdrivers[nperiph_drivers + 1] = NULL;
113 	old = periph_drivers;
114 	periph_drivers = newdrivers;
115 	if (old)
116 		kfree(old, M_CAMPERIPH);
117 	nperiph_drivers++;
118 }
119 
120 cam_status
121 cam_periph_alloc(periph_ctor_t *periph_ctor,
122 		 periph_oninv_t *periph_oninvalidate,
123 		 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
124 		 char *name, cam_periph_type type, struct cam_path *path,
125 		 ac_callback_t *ac_callback, ac_code code, void *arg)
126 {
127 	struct		periph_driver **p_drv;
128 	struct		cam_sim *sim;
129 	struct		cam_periph *periph;
130 	struct		cam_periph *cur_periph;
131 	path_id_t	path_id;
132 	target_id_t	target_id;
133 	lun_id_t	lun_id;
134 	cam_status	status;
135 	u_int		init_level;
136 
137 	init_level = 0;
138 	/*
139 	 * Handle Hot-Plug scenarios.  If there is already a peripheral
140 	 * of our type assigned to this path, we are likely waiting for
141 	 * final close on an old, invalidated, peripheral.  If this is
142 	 * the case, queue up a deferred call to the peripheral's async
143 	 * handler.  If it looks like a mistaken re-allocation, complain.
144 	 */
145 	if ((periph = cam_periph_find(path, name)) != NULL) {
146 
147 		if ((periph->flags & CAM_PERIPH_INVALID) != 0
148 		 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
149 			periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
150 			periph->deferred_callback = ac_callback;
151 			periph->deferred_ac = code;
152 			return (CAM_REQ_INPROG);
153 		} else {
154 			kprintf("cam_periph_alloc: attempt to re-allocate "
155 			       "valid device %s%d rejected\n",
156 			       periph->periph_name, periph->unit_number);
157 		}
158 		return (CAM_REQ_INVALID);
159 	}
160 
161 	periph = kmalloc(sizeof(*periph), M_CAMPERIPH, M_INTWAIT | M_ZERO);
162 
163 	init_level++;
164 
165 	xpt_lock_buses();
166 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
167 		if (strcmp((*p_drv)->driver_name, name) == 0)
168 			break;
169 	}
170 	xpt_unlock_buses();
171 
172 	sim = xpt_path_sim(path);
173 	path_id = xpt_path_path_id(path);
174 	target_id = xpt_path_target_id(path);
175 	lun_id = xpt_path_lun_id(path);
176 	cam_init_pinfo(&periph->pinfo);
177 	periph->periph_start = periph_start;
178 	periph->periph_dtor = periph_dtor;
179 	periph->periph_oninval = periph_oninvalidate;
180 	periph->type = type;
181 	periph->periph_name = name;
182 	periph->unit_number = camperiphunit(*p_drv, sim, path_id,
183 					    target_id, lun_id);
184 	periph->immediate_priority = CAM_PRIORITY_NONE;
185 	periph->refcount = 0;
186 	periph->sim = sim;
187 	SLIST_INIT(&periph->ccb_list);
188 	status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
189 	if (status != CAM_REQ_CMP)
190 		goto failure;
191 
192 	periph->path = path;
193 	init_level++;
194 
195 	status = xpt_add_periph(periph);
196 
197 	if (status != CAM_REQ_CMP)
198 		goto failure;
199 
200 	cur_periph = TAILQ_FIRST(&(*p_drv)->units);
201 	while (cur_periph != NULL
202 	    && cur_periph->unit_number < periph->unit_number)
203 		cur_periph = TAILQ_NEXT(cur_periph, unit_links);
204 
205 	if (cur_periph != NULL)
206 		TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
207 	else {
208 		TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
209 		(*p_drv)->generation++;
210 	}
211 
212 	init_level++;
213 
214 	status = periph_ctor(periph, arg);
215 
216 	if (status == CAM_REQ_CMP)
217 		init_level++;
218 
219 failure:
220 	switch (init_level) {
221 	case 4:
222 		/* Initialized successfully */
223 		break;
224 	case 3:
225 		TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
226 		xpt_remove_periph(periph);
227 		/* FALLTHROUGH */
228 	case 2:
229 		xpt_free_path(periph->path);
230 		/* FALLTHROUGH */
231 	case 1:
232 		kfree(periph, M_CAMPERIPH);
233 		/* FALLTHROUGH */
234 	case 0:
235 		/* No cleanup to perform. */
236 		break;
237 	default:
238 		panic("cam_periph_alloc: Unknown init level");
239 	}
240 	return(status);
241 }
242 
243 /*
244  * Find a peripheral structure with the specified path, target, lun,
245  * and (optionally) type.  If the name is NULL, this function will return
246  * the first peripheral driver that matches the specified path.
247  */
248 struct cam_periph *
249 cam_periph_find(struct cam_path *path, char *name)
250 {
251 	struct periph_driver **p_drv;
252 	struct cam_periph *periph;
253 
254 	xpt_lock_buses();
255 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
256 		if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
257 			continue;
258 
259 		TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
260 			if (xpt_path_comp(periph->path, path) == 0) {
261 				xpt_unlock_buses();
262 				return(periph);
263 			}
264 		}
265 		if (name != NULL) {
266 			xpt_unlock_buses();
267 			return(NULL);
268 		}
269 	}
270 	xpt_unlock_buses();
271 	return(NULL);
272 }
273 
274 cam_status
275 cam_periph_acquire(struct cam_periph *periph)
276 {
277 	if (periph == NULL)
278 		return(CAM_REQ_CMP_ERR);
279 
280 	xpt_lock_buses();
281 	periph->refcount++;
282 	xpt_unlock_buses();
283 
284 	return(CAM_REQ_CMP);
285 }
286 
287 /*
288  * Release the peripheral.  The XPT is not locked and the SIM may or may
289  * not be locked on entry.
290  *
291  * The last release on a peripheral marked invalid frees it.  In this
292  * case we must be sure to hold both the XPT lock and the SIM lock,
293  * requiring a bit of fancy footwork if the SIM lock already happens
294  * to be held.
295  */
296 void
297 cam_periph_release(struct cam_periph *periph)
298 {
299 	struct cam_sim *sim;
300 	int doun;
301 
302 	while (periph) {
303 		/*
304 		 * First try the critical path case
305 		 */
306 		sim = periph->sim;
307 		xpt_lock_buses();
308 		if ((periph->flags & CAM_PERIPH_INVALID) == 0 ||
309 		    periph->refcount != 1) {
310 			--periph->refcount;
311 			xpt_unlock_buses();
312 			break;
313 		}
314 
315 		/*
316 		 * Otherwise we also need to free the peripheral and must
317 		 * acquire the sim lock and xpt lock in the correct order
318 		 * to do so.
319 		 *
320 		 * The condition must be re-checked after the locks have
321 		 * been reacquired.
322 		 */
323 		xpt_unlock_buses();
324 		doun = CAM_SIM_COND_LOCK(sim);
325 		xpt_lock_buses();
326 		--periph->refcount;
327 		if ((periph->flags & CAM_PERIPH_INVALID) &&
328 		    periph->refcount == 0) {
329 			camperiphfree(periph);
330 		}
331 		xpt_unlock_buses();
332 		CAM_SIM_COND_UNLOCK(sim, doun);
333 		break;
334 	}
335 }
336 
337 int
338 cam_periph_hold(struct cam_periph *periph, int flags)
339 {
340 	int error;
341 
342 	sim_lock_assert_owned(periph->sim->lock);
343 
344 	/*
345 	 * Increment the reference count on the peripheral
346 	 * while we wait for our lock attempt to succeed
347 	 * to ensure the peripheral doesn't disappear out
348 	 * from user us while we sleep.
349 	 */
350 
351 	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
352 		return (ENXIO);
353 
354 	while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
355 		periph->flags |= CAM_PERIPH_LOCK_WANTED;
356 		if ((error = sim_lock_sleep(periph, flags, "caplck", 0,
357 					    periph->sim->lock)) != 0) {
358 			cam_periph_release(periph);
359 			return (error);
360 		}
361 	}
362 
363 	periph->flags |= CAM_PERIPH_LOCKED;
364 	return (0);
365 }
366 
367 void
368 cam_periph_unhold(struct cam_periph *periph, int unlock)
369 {
370 	struct cam_sim *sim;
371 
372 	sim_lock_assert_owned(periph->sim->lock);
373 	periph->flags &= ~CAM_PERIPH_LOCKED;
374 	if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
375 		periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
376 		wakeup(periph);
377 	}
378 	if (unlock) {
379 		sim = periph->sim;
380 		cam_periph_release(periph);
381 		/* periph may be garbage now */
382 		CAM_SIM_UNLOCK(sim);
383 	} else {
384 		cam_periph_release(periph);
385 	}
386 }
387 
388 /*
389  * Look for the next unit number that is not currently in use for this
390  * peripheral type starting at "newunit".  Also exclude unit numbers that
391  * are reserved by for future "hardwiring" unless we already know that this
392  * is a potential wired device.  Only assume that the device is "wired" the
393  * first time through the loop since after that we'll be looking at unit
394  * numbers that did not match a wiring entry.
395  */
396 static u_int
397 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
398 		  path_id_t pathid, target_id_t target, lun_id_t lun)
399 {
400 	struct	cam_periph *periph;
401 	char	*periph_name, *strval;
402 	int	i, val, dunit;
403 	const char *dname;
404 
405 	periph_name = p_drv->driver_name;
406 	for (;;newunit++) {
407 
408 		for (periph = TAILQ_FIRST(&p_drv->units);
409 		     periph != NULL && periph->unit_number != newunit;
410 		     periph = TAILQ_NEXT(periph, unit_links))
411 			;
412 
413 		if (periph != NULL && periph->unit_number == newunit) {
414 			if (wired != 0) {
415 				xpt_print(periph->path, "Duplicate Wired "
416 				    "Device entry!\n");
417 				xpt_print(periph->path, "Second device (%s "
418 				    "device at scbus%d target %d lun %d) will "
419 				    "not be wired\n", periph_name, pathid,
420 				    target, lun);
421 				wired = 0;
422 			}
423 			continue;
424 		}
425 		if (wired)
426 			break;
427 
428 		/*
429 		 * Don't match entries like "da 4" as a wired down
430 		 * device, but do match entries like "da 4 target 5"
431 		 * or even "da 4 scbus 1".
432 		 */
433 		i = -1;
434 		while ((i = resource_locate(i, periph_name)) != -1) {
435 			dname = resource_query_name(i);
436 			dunit = resource_query_unit(i);
437 			/* if no "target" and no specific scbus, skip */
438 			if (resource_int_value(dname, dunit, "target", &val) &&
439 			    (resource_string_value(dname, dunit, "at",&strval)||
440 			     strcmp(strval, "scbus") == 0))
441 				continue;
442 			if (newunit == dunit)
443 				break;
444 		}
445 		if (i == -1)
446 			break;
447 	}
448 	return (newunit);
449 }
450 
451 static u_int
452 camperiphunit(struct periph_driver *p_drv,
453 	      struct cam_sim *sim, path_id_t pathid,
454 	      target_id_t target, lun_id_t lun)
455 {
456 	u_int	unit;
457 	int	hit, i, val, dunit;
458 	const char *dname;
459 	char	pathbuf[32], *strval, *periph_name;
460 
461 	unit = 0;
462 
463 	periph_name = p_drv->driver_name;
464 	ksnprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
465 	i = -1;
466 	for (hit = 0; (i = resource_locate(i, periph_name)) != -1; hit = 0) {
467 		dname = resource_query_name(i);
468 		dunit = resource_query_unit(i);
469 		if (resource_string_value(dname, dunit, "at", &strval) == 0) {
470 			if (strcmp(strval, pathbuf) != 0)
471 				continue;
472 			hit++;
473 		}
474 		if (resource_int_value(dname, dunit, "target", &val) == 0) {
475 			if (val != target)
476 				continue;
477 			hit++;
478 		}
479 		if (resource_int_value(dname, dunit, "lun", &val) == 0) {
480 			if (val != lun)
481 				continue;
482 			hit++;
483 		}
484 		if (hit != 0) {
485 			unit = dunit;
486 			break;
487 		}
488 	}
489 
490 	/*
491 	 * If no wired units are in the kernel config do an auto unit
492 	 * start selection.  We want usb mass storage out of the way
493 	 * so it doesn't steal low numbered da%d slots from ahci, sili,
494 	 * or other scsi attachments.
495 	 */
496 	if (hit == 0 && sim) {
497 		if (strncmp(sim->sim_name, "umass", 4) == 0 && unit < 8)
498 			unit = 8;
499 	}
500 
501 	/*
502 	 * Either start from 0 looking for the next unit or from
503 	 * the unit number given in the resource config.  This way,
504 	 * if we have wildcard matches, we don't return the same
505 	 * unit number twice.
506 	 */
507 	unit = camperiphnextunit(p_drv, unit, /*wired*/hit, pathid,
508 				 target, lun);
509 
510 	return (unit);
511 }
512 
513 void
514 cam_periph_invalidate(struct cam_periph *periph)
515 {
516 	/*
517 	 * We only call this routine the first time a peripheral is
518 	 * invalidated.
519 	 */
520 	if (((periph->flags & CAM_PERIPH_INVALID) == 0)
521 	 && (periph->periph_oninval != NULL))
522 		periph->periph_oninval(periph);
523 
524 	periph->flags |= CAM_PERIPH_INVALID;
525 	periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
526 
527 	xpt_lock_buses();
528 	if (periph->refcount == 0)
529 		camperiphfree(periph);
530 	else if (periph->refcount < 0)
531 		kprintf("cam_invalidate_periph: refcount < 0!!\n");
532 	xpt_unlock_buses();
533 }
534 
535 static void
536 camperiphfree(struct cam_periph *periph)
537 {
538 	struct periph_driver **p_drv;
539 
540 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
541 		if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
542 			break;
543 	}
544 
545 	if (*p_drv == NULL) {
546 		kprintf("camperiphfree: attempt to free non-existent periph\n");
547 		return;
548 	}
549 
550 	TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
551 	(*p_drv)->generation++;
552 	xpt_unlock_buses();
553 
554 	if (periph->periph_dtor != NULL)
555 		periph->periph_dtor(periph);
556 	xpt_remove_periph(periph);
557 
558 	if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
559 		union ccb ccb;
560 		void *arg;
561 
562 		switch (periph->deferred_ac) {
563 		case AC_FOUND_DEVICE:
564 			ccb.ccb_h.func_code = XPT_GDEV_TYPE;
565 			xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
566 			xpt_action(&ccb);
567 			arg = &ccb;
568 			break;
569 		case AC_PATH_REGISTERED:
570 			ccb.ccb_h.func_code = XPT_PATH_INQ;
571 			xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
572 			xpt_action(&ccb);
573 			arg = &ccb;
574 			break;
575 		default:
576 			arg = NULL;
577 			break;
578 		}
579 		periph->deferred_callback(NULL, periph->deferred_ac,
580 					  periph->path, arg);
581 	}
582 	xpt_free_path(periph->path);
583 	kfree(periph, M_CAMPERIPH);
584 	xpt_lock_buses();
585 }
586 
587 /*
588  * Map user virtual pointers into kernel virtual address space, so we can
589  * access the memory.  This won't work on physical pointers, for now it's
590  * up to the caller to check for that.  (XXX KDM -- should we do that here
591  * instead?)  This also only works for up to MAXPHYS memory.  Since we use
592  * buffers to map stuff in and out, we're limited to the buffer size.
593  */
594 int
595 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
596 {
597 	buf_cmd_t cmd[CAM_PERIPH_MAXMAPS];
598 	u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
599 	u_int32_t lengths[CAM_PERIPH_MAXMAPS];
600 	int numbufs;
601 	int error;
602 	int i;
603 	struct buf *bp;
604 
605 	switch(ccb->ccb_h.func_code) {
606 	case XPT_DEV_MATCH:
607 		if (ccb->cdm.match_buf_len == 0) {
608 			kprintf("cam_periph_mapmem: invalid match buffer "
609 			       "length 0\n");
610 			return(EINVAL);
611 		}
612 		if (ccb->cdm.pattern_buf_len > 0) {
613 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
614 			lengths[0] = ccb->cdm.pattern_buf_len;
615 			mapinfo->dirs[0] = CAM_DIR_OUT;
616 			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
617 			lengths[1] = ccb->cdm.match_buf_len;
618 			mapinfo->dirs[1] = CAM_DIR_IN;
619 			numbufs = 2;
620 		} else {
621 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
622 			lengths[0] = ccb->cdm.match_buf_len;
623 			mapinfo->dirs[0] = CAM_DIR_IN;
624 			numbufs = 1;
625 		}
626 		break;
627 	case XPT_SCSI_IO:
628 	case XPT_CONT_TARGET_IO:
629 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
630 			return(0);
631 
632 		data_ptrs[0] = &ccb->csio.data_ptr;
633 		lengths[0] = ccb->csio.dxfer_len;
634 		mapinfo->dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
635 		numbufs = 1;
636 		break;
637 	default:
638 		return(EINVAL);
639 		break; /* NOTREACHED */
640 	}
641 
642 	/*
643 	 * Check the transfer length and permissions first, so we don't
644 	 * have to unmap any previously mapped buffers.
645 	 */
646 	for (i = 0; i < numbufs; i++) {
647 		/*
648 		 * Its kinda bogus, we need a R+W command.  For now the
649 		 * buffer needs some sort of command.  Use BUF_CMD_WRITE
650 		 * to indicate a write and BUF_CMD_READ to indicate R+W.
651 		 */
652 		cmd[i] = BUF_CMD_WRITE;
653 
654 		/*
655 		 * The userland data pointer passed in may not be page
656 		 * aligned.  vmapbuf() truncates the address to a page
657 		 * boundary, so if the address isn't page aligned, we'll
658 		 * need enough space for the given transfer length, plus
659 		 * whatever extra space is necessary to make it to the page
660 		 * boundary.
661 		 */
662 		if ((lengths[i] +
663 		    (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > DFLTPHYS){
664 			kprintf("cam_periph_mapmem: attempt to map %lu bytes, "
665 			       "which is greater than DFLTPHYS(%d)\n",
666 			       (long)(lengths[i] +
667 			       (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
668 			       DFLTPHYS);
669 			return(E2BIG);
670 		}
671 
672 		if (mapinfo->dirs[i] & CAM_DIR_OUT) {
673 			if (!useracc(*data_ptrs[i], lengths[i],
674 				     VM_PROT_READ)) {
675 				kprintf("cam_periph_mapmem: error, "
676 					"address %p, length %lu isn't "
677 					"user accessible for READ\n",
678 					(void *)*data_ptrs[i],
679 					(u_long)lengths[i]);
680 				return(EACCES);
681 			}
682 		}
683 
684 		if (mapinfo->dirs[i] & CAM_DIR_IN) {
685 			cmd[i] = BUF_CMD_READ;
686 			if (!useracc(*data_ptrs[i], lengths[i],
687 				     VM_PROT_WRITE)) {
688 				kprintf("cam_periph_mapmem: error, "
689 					"address %p, length %lu isn't "
690 					"user accessible for WRITE\n",
691 					(void *)*data_ptrs[i],
692 					(u_long)lengths[i]);
693 
694 				return(EACCES);
695 			}
696 		}
697 
698 	}
699 
700 	for (i = 0; i < numbufs; i++) {
701 		/*
702 		 * Get the buffer.
703 		 */
704 		bp = getpbuf(NULL);
705 
706 		/* save the original user pointer */
707 		mapinfo->saved_ptrs[i] = *data_ptrs[i];
708 
709 		/* set the flags */
710 		bp->b_cmd = cmd[i];
711 
712 		/*
713 		 * Require 16-byte alignment and bounce if we don't get it.
714 		 * (NATA does not realign buffers for DMA).
715 		 */
716 		if ((intptr_t)*data_ptrs[i] & 15)
717 			mapinfo->bounce[i] = 1;
718 		else
719 			mapinfo->bounce[i] = 0;
720 
721 		/*
722 		 * Map the user buffer into kernel memory.  If the user
723 		 * buffer is not aligned we have to allocate a bounce buffer
724 		 * and copy.
725 		 */
726 		if (mapinfo->bounce[i]) {
727 			bp->b_data = bp->b_kvabase;
728 			bp->b_bcount = lengths[i];
729 			vm_hold_load_pages(bp, (vm_offset_t)bp->b_data,
730 				       (vm_offset_t)bp->b_data + bp->b_bcount);
731 			if (mapinfo->dirs[i] & CAM_DIR_OUT) {
732 				error = copyin(*data_ptrs[i], bp->b_data, bp->b_bcount);
733 				if (error) {
734 					vm_hold_free_pages(bp, (vm_offset_t)bp->b_data, (vm_offset_t)bp->b_data + bp->b_bcount);
735 				}
736 			} else {
737 				error = 0;
738 			}
739 		} else if (vmapbuf(bp, *data_ptrs[i], lengths[i]) < 0) {
740 			kprintf("cam_periph_mapmem: error, "
741 				"address %p, length %lu isn't "
742 				"user accessible any more\n",
743 				(void *)*data_ptrs[i],
744 				(u_long)lengths[i]);
745 			error = EACCES;
746 		} else {
747 			error = 0;
748 		}
749 		if (error) {
750 			relpbuf(bp, NULL);
751 			cam_periph_unmapbufs(mapinfo, data_ptrs, i);
752 			mapinfo->num_bufs_used -= i;
753 			return(error);
754 		}
755 
756 		/* set our pointer to the new mapped area */
757 		*data_ptrs[i] = bp->b_data;
758 
759 		mapinfo->bp[i] = bp;
760 		mapinfo->num_bufs_used++;
761 	}
762 
763 	return(0);
764 }
765 
766 /*
767  * Unmap memory segments mapped into kernel virtual address space by
768  * cam_periph_mapmem().
769  */
770 void
771 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
772 {
773 	int numbufs;
774 	u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
775 
776 	if (mapinfo->num_bufs_used <= 0) {
777 		/* allow ourselves to be swapped once again */
778 		return;
779 	}
780 
781 	switch (ccb->ccb_h.func_code) {
782 	case XPT_DEV_MATCH:
783 		numbufs = min(mapinfo->num_bufs_used, 2);
784 
785 		if (numbufs == 1) {
786 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
787 		} else {
788 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
789 			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
790 		}
791 		break;
792 	case XPT_SCSI_IO:
793 	case XPT_CONT_TARGET_IO:
794 		data_ptrs[0] = &ccb->csio.data_ptr;
795 		numbufs = min(mapinfo->num_bufs_used, 1);
796 		break;
797 	default:
798 		/* allow ourselves to be swapped once again */
799 		return;
800 		break; /* NOTREACHED */
801 	}
802 	cam_periph_unmapbufs(mapinfo, data_ptrs, numbufs);
803 }
804 
805 static void
806 cam_periph_unmapbufs(struct cam_periph_map_info *mapinfo,
807 		     u_int8_t ***data_ptrs, int numbufs)
808 {
809 	struct buf *bp;
810 	int i;
811 
812 	for (i = 0; i < numbufs; i++) {
813 		bp = mapinfo->bp[i];
814 
815 		/* Set the user's pointer back to the original value */
816 		*data_ptrs[i] = mapinfo->saved_ptrs[i];
817 
818 		/* unmap the buffer */
819 		if (mapinfo->bounce[i]) {
820 			if (mapinfo->dirs[i] & CAM_DIR_IN) {
821 				/* XXX return error */
822 				copyout(bp->b_data, *data_ptrs[i],
823 					bp->b_bcount);
824 			}
825 			vm_hold_free_pages(bp, (vm_offset_t)bp->b_data,
826 				   (vm_offset_t)bp->b_data + bp->b_bcount);
827 		} else {
828 			vunmapbuf(bp);
829 		}
830 		relpbuf(bp, NULL);
831 		mapinfo->bp[i] = NULL;
832 	}
833 }
834 
835 union ccb *
836 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
837 {
838 	struct ccb_hdr *ccb_h;
839 
840 	sim_lock_assert_owned(periph->sim->lock);
841 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
842 
843 	while (SLIST_FIRST(&periph->ccb_list) == NULL) {
844 		if (periph->immediate_priority > priority)
845 			periph->immediate_priority = priority;
846 		xpt_schedule(periph, priority);
847 		if ((SLIST_FIRST(&periph->ccb_list) != NULL)
848 		 && (SLIST_FIRST(&periph->ccb_list)->pinfo.priority == priority))
849 			break;
850 		sim_lock_sleep(&periph->ccb_list, 0, "cgticb", 0,
851 			       periph->sim->lock);
852 	}
853 
854 	ccb_h = SLIST_FIRST(&periph->ccb_list);
855 	SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
856 	return ((union ccb *)ccb_h);
857 }
858 
859 void
860 cam_periph_ccbwait(union ccb *ccb)
861 {
862 	struct cam_sim *sim;
863 
864 	sim = xpt_path_sim(ccb->ccb_h.path);
865 	while ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
866 	 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)) {
867 		sim_lock_sleep(&ccb->ccb_h.cbfcnp, 0, "cbwait", 0, sim->lock);
868 	}
869 }
870 
871 int
872 cam_periph_ioctl(struct cam_periph *periph, int cmd, caddr_t addr,
873 		 int (*error_routine)(union ccb *ccb,
874 				      cam_flags camflags,
875 				      u_int32_t sense_flags))
876 {
877 	union ccb 	     *ccb;
878 	int 		     error;
879 	int		     found;
880 
881 	error = found = 0;
882 
883 	switch(cmd){
884 	case CAMGETPASSTHRU:
885 		ccb = cam_periph_getccb(periph, /* priority */ 1);
886 		xpt_setup_ccb(&ccb->ccb_h,
887 			      ccb->ccb_h.path,
888 			      /*priority*/1);
889 		ccb->ccb_h.func_code = XPT_GDEVLIST;
890 
891 		/*
892 		 * Basically, the point of this is that we go through
893 		 * getting the list of devices, until we find a passthrough
894 		 * device.  In the current version of the CAM code, the
895 		 * only way to determine what type of device we're dealing
896 		 * with is by its name.
897 		 */
898 		while (found == 0) {
899 			ccb->cgdl.index = 0;
900 			ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
901 			while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
902 
903 				/* we want the next device in the list */
904 				xpt_action(ccb);
905 				if (strncmp(ccb->cgdl.periph_name,
906 				    "pass", 4) == 0){
907 					found = 1;
908 					break;
909 				}
910 			}
911 			if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
912 			    (found == 0)) {
913 				ccb->cgdl.periph_name[0] = '\0';
914 				ccb->cgdl.unit_number = 0;
915 				break;
916 			}
917 		}
918 
919 		/* copy the result back out */
920 		bcopy(ccb, addr, sizeof(union ccb));
921 
922 		/* and release the ccb */
923 		xpt_release_ccb(ccb);
924 
925 		break;
926 	default:
927 		error = ENOTTY;
928 		break;
929 	}
930 	return(error);
931 }
932 
933 int
934 cam_periph_runccb(union ccb *ccb,
935 		  int (*error_routine)(union ccb *ccb,
936 				       cam_flags camflags,
937 				       u_int32_t sense_flags),
938 		  cam_flags camflags, u_int32_t sense_flags,
939 		  struct devstat *ds)
940 {
941 	struct cam_sim *sim;
942 	int error;
943 
944 	error = 0;
945 	sim = xpt_path_sim(ccb->ccb_h.path);
946 	sim_lock_assert_owned(sim->lock);
947 
948 	/*
949 	 * If the user has supplied a stats structure, and if we understand
950 	 * this particular type of ccb, record the transaction start.
951 	 */
952 	if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
953 		devstat_start_transaction(ds);
954 
955 	xpt_action(ccb);
956 
957 	do {
958 		cam_periph_ccbwait(ccb);
959 		if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
960 			error = 0;
961 		else if (error_routine != NULL)
962 			error = (*error_routine)(ccb, camflags, sense_flags);
963 		else
964 			error = 0;
965 
966 	} while (error == ERESTART);
967 
968 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
969 		cam_release_devq(ccb->ccb_h.path,
970 				 /* relsim_flags */0,
971 				 /* openings */0,
972 				 /* timeout */0,
973 				 /* getcount_only */ FALSE);
974 
975 	if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
976 		devstat_end_transaction(ds,
977 					ccb->csio.dxfer_len,
978 					ccb->csio.tag_action & 0xf,
979 					((ccb->ccb_h.flags & CAM_DIR_MASK) ==
980 					CAM_DIR_NONE) ?  DEVSTAT_NO_DATA :
981 					(ccb->ccb_h.flags & CAM_DIR_OUT) ?
982 					DEVSTAT_WRITE :
983 					DEVSTAT_READ);
984 
985 	return(error);
986 }
987 
988 void
989 cam_freeze_devq(struct cam_path *path)
990 {
991 	struct ccb_hdr ccb_h;
992 
993 	xpt_setup_ccb(&ccb_h, path, /*priority*/1);
994 	ccb_h.func_code = XPT_NOOP;
995 	ccb_h.flags = CAM_DEV_QFREEZE;
996 	xpt_action((union ccb *)&ccb_h);
997 }
998 
999 u_int32_t
1000 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
1001 		 u_int32_t openings, u_int32_t timeout,
1002 		 int getcount_only)
1003 {
1004 	struct ccb_relsim crs;
1005 
1006 	xpt_setup_ccb(&crs.ccb_h, path,
1007 		      /*priority*/1);
1008 	crs.ccb_h.func_code = XPT_REL_SIMQ;
1009 	crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
1010 	crs.release_flags = relsim_flags;
1011 	crs.openings = openings;
1012 	crs.release_timeout = timeout;
1013 	xpt_action((union ccb *)&crs);
1014 	return (crs.qfrozen_cnt);
1015 }
1016 
1017 #define saved_ccb_ptr ppriv_ptr0
1018 static void
1019 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
1020 {
1021 	union ccb      *saved_ccb;
1022 	cam_status	status;
1023 	int		frozen;
1024 	int		sense;
1025 	struct scsi_start_stop_unit *scsi_cmd;
1026 	u_int32_t	relsim_flags, timeout;
1027 	u_int32_t	qfrozen_cnt;
1028 	int		xpt_done_ccb;
1029 
1030 	xpt_done_ccb = FALSE;
1031 	status = done_ccb->ccb_h.status;
1032 	frozen = (status & CAM_DEV_QFRZN) != 0;
1033 	sense  = (status & CAM_AUTOSNS_VALID) != 0;
1034 	status &= CAM_STATUS_MASK;
1035 
1036 	timeout = 0;
1037 	relsim_flags = 0;
1038 	saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
1039 
1040 	/*
1041 	 * Unfreeze the queue once if it is already frozen..
1042 	 */
1043 	if (frozen != 0) {
1044 		qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
1045 					      /*relsim_flags*/0,
1046 					      /*openings*/0,
1047 					      /*timeout*/0,
1048 					      /*getcount_only*/0);
1049 	}
1050 
1051 	switch (status) {
1052 	case CAM_REQ_CMP:
1053 	{
1054 		/*
1055 		 * If we have successfully taken a device from the not
1056 		 * ready to ready state, re-scan the device and re-get
1057 		 * the inquiry information.  Many devices (mostly disks)
1058 		 * don't properly report their inquiry information unless
1059 		 * they are spun up.
1060 		 *
1061 		 * If we manually retrieved sense into a CCB and got
1062 		 * something other than "NO SENSE" send the updated CCB
1063 		 * back to the client via xpt_done() to be processed via
1064 		 * the error recovery code again.
1065 		 */
1066 		if (done_ccb->ccb_h.func_code == XPT_SCSI_IO) {
1067 			scsi_cmd = (struct scsi_start_stop_unit *)
1068 					&done_ccb->csio.cdb_io.cdb_bytes;
1069 
1070 		 	if (scsi_cmd->opcode == START_STOP_UNIT)
1071 				xpt_async(AC_INQ_CHANGED,
1072 					  done_ccb->ccb_h.path, NULL);
1073 			if (scsi_cmd->opcode == REQUEST_SENSE) {
1074 				u_int sense_key;
1075 
1076 				sense_key = saved_ccb->csio.sense_data.flags;
1077 				sense_key &= SSD_KEY;
1078 				if (sense_key != SSD_KEY_NO_SENSE) {
1079 					saved_ccb->ccb_h.status |=
1080 					    CAM_AUTOSNS_VALID;
1081 #if 0
1082 					xpt_print(saved_ccb->ccb_h.path,
1083 					    "Recovered Sense\n");
1084 					scsi_sense_print(&saved_ccb->csio);
1085 					cam_error_print(saved_ccb, CAM_ESF_ALL,
1086 							CAM_EPF_ALL);
1087 #endif
1088 					xpt_done_ccb = TRUE;
1089 				}
1090 			}
1091 		}
1092 		bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
1093 		      sizeof(union ccb));
1094 
1095 		periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1096 
1097 		if (xpt_done_ccb == FALSE)
1098 			xpt_action(done_ccb);
1099 
1100 		break;
1101 	}
1102 	case CAM_SCSI_STATUS_ERROR:
1103 		scsi_cmd = (struct scsi_start_stop_unit *)
1104 				&done_ccb->csio.cdb_io.cdb_bytes;
1105 		if (sense != 0) {
1106 			struct ccb_getdev cgd;
1107 			struct scsi_sense_data *sense;
1108 			int    error_code, sense_key, asc, ascq;
1109 			scsi_sense_action err_action;
1110 
1111 			sense = &done_ccb->csio.sense_data;
1112 			scsi_extract_sense(sense, &error_code,
1113 					   &sense_key, &asc, &ascq);
1114 
1115 			/*
1116 			 * Grab the inquiry data for this device.
1117 			 */
1118 			xpt_setup_ccb(&cgd.ccb_h, done_ccb->ccb_h.path,
1119 				      /*priority*/ 1);
1120 			cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1121 			xpt_action((union ccb *)&cgd);
1122 			err_action = scsi_error_action(&done_ccb->csio,
1123 						       &cgd.inq_data, 0);
1124 
1125 			/*
1126 	 		 * If the error is "invalid field in CDB",
1127 			 * and the load/eject flag is set, turn the
1128 			 * flag off and try again.  This is just in
1129 			 * case the drive in question barfs on the
1130 			 * load eject flag.  The CAM code should set
1131 			 * the load/eject flag by default for
1132 			 * removable media.
1133 			 */
1134 
1135 			/* XXX KDM
1136 			 * Should we check to see what the specific
1137 			 * scsi status is??  Or does it not matter
1138 			 * since we already know that there was an
1139 			 * error, and we know what the specific
1140 			 * error code was, and we know what the
1141 			 * opcode is..
1142 			 */
1143 			if ((scsi_cmd->opcode == START_STOP_UNIT) &&
1144 			    ((scsi_cmd->how & SSS_LOEJ) != 0) &&
1145 			     (asc == 0x24) && (ascq == 0x00) &&
1146 			     (done_ccb->ccb_h.retry_count > 0)) {
1147 
1148 				scsi_cmd->how &= ~SSS_LOEJ;
1149 
1150 				xpt_action(done_ccb);
1151 
1152 			} else if ((done_ccb->ccb_h.retry_count > 1)
1153 				&& ((err_action & SS_MASK) != SS_FAIL)) {
1154 
1155 				/*
1156 				 * In this case, the error recovery
1157 				 * command failed, but we've got
1158 				 * some retries left on it.  Give
1159 				 * it another try unless this is an
1160 				 * unretryable error.
1161 				 */
1162 
1163 				/* set the timeout to .5 sec */
1164 				relsim_flags =
1165 					RELSIM_RELEASE_AFTER_TIMEOUT;
1166 				timeout = 500;
1167 
1168 				xpt_action(done_ccb);
1169 
1170 				break;
1171 
1172 			} else {
1173 				/*
1174 				 * Perform the final retry with the original
1175 				 * CCB so that final error processing is
1176 				 * performed by the owner of the CCB.
1177 				 */
1178 				bcopy(done_ccb->ccb_h.saved_ccb_ptr,
1179 				      done_ccb, sizeof(union ccb));
1180 
1181 				periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1182 
1183 				xpt_action(done_ccb);
1184 			}
1185 		} else {
1186 			/*
1187 			 * Eh??  The command failed, but we don't
1188 			 * have any sense.  What's up with that?
1189 			 * Fire the CCB again to return it to the
1190 			 * caller.
1191 			 */
1192 			bcopy(done_ccb->ccb_h.saved_ccb_ptr,
1193 			      done_ccb, sizeof(union ccb));
1194 
1195 			periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1196 
1197 			xpt_action(done_ccb);
1198 
1199 		}
1200 		break;
1201 	default:
1202 		bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
1203 		      sizeof(union ccb));
1204 
1205 		periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1206 
1207 		xpt_action(done_ccb);
1208 
1209 		break;
1210 	}
1211 
1212 	/* decrement the retry count */
1213 	/*
1214 	 * XXX This isn't appropriate in all cases.  Restructure,
1215 	 *     so that the retry count is only decremented on an
1216 	 *     actual retry.  Remeber that the orignal ccb had its
1217 	 *     retry count dropped before entering recovery, so
1218 	 *     doing it again is a bug.
1219 	 */
1220 	if (done_ccb->ccb_h.retry_count > 0)
1221 		done_ccb->ccb_h.retry_count--;
1222 
1223 	qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
1224 				      /*relsim_flags*/relsim_flags,
1225 				      /*openings*/0,
1226 				      /*timeout*/timeout,
1227 				      /*getcount_only*/0);
1228 	if (xpt_done_ccb == TRUE)
1229 		(*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
1230 }
1231 
1232 /*
1233  * Generic Async Event handler.  Peripheral drivers usually
1234  * filter out the events that require personal attention,
1235  * and leave the rest to this function.
1236  */
1237 void
1238 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1239 		 struct cam_path *path, void *arg)
1240 {
1241 	switch (code) {
1242 	case AC_LOST_DEVICE:
1243 		cam_periph_invalidate(periph);
1244 		break;
1245 	case AC_SENT_BDR:
1246 	case AC_BUS_RESET:
1247 	{
1248 		cam_periph_bus_settle(periph, scsi_delay);
1249 		break;
1250 	}
1251 	default:
1252 		break;
1253 	}
1254 }
1255 
1256 void
1257 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1258 {
1259 	struct ccb_getdevstats cgds;
1260 
1261 	xpt_setup_ccb(&cgds.ccb_h, periph->path, /*priority*/1);
1262 	cgds.ccb_h.func_code = XPT_GDEV_STATS;
1263 	xpt_action((union ccb *)&cgds);
1264 	cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1265 }
1266 
1267 void
1268 cam_periph_freeze_after_event(struct cam_periph *periph,
1269 			      struct timeval* event_time, u_int duration_ms)
1270 {
1271 	struct timeval delta;
1272 	struct timeval duration_tv;
1273 
1274 	microuptime(&delta);
1275 	timevalsub(&delta, event_time);
1276 	duration_tv.tv_sec = duration_ms / 1000;
1277 	duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1278 	if (timevalcmp(&delta, &duration_tv, <)) {
1279 		timevalsub(&duration_tv, &delta);
1280 
1281 		duration_ms = duration_tv.tv_sec * 1000;
1282 		duration_ms += duration_tv.tv_usec / 1000;
1283 		cam_freeze_devq(periph->path);
1284 		cam_release_devq(periph->path,
1285 				RELSIM_RELEASE_AFTER_TIMEOUT,
1286 				/*reduction*/0,
1287 				/*timeout*/duration_ms,
1288 				/*getcount_only*/0);
1289 	}
1290 
1291 }
1292 
1293 static int
1294 camperiphscsistatuserror(union ccb *ccb, cam_flags camflags,
1295 			 u_int32_t sense_flags, union ccb *save_ccb,
1296 			 int *openings, u_int32_t *relsim_flags,
1297 			 u_int32_t *timeout)
1298 {
1299 	int error;
1300 
1301 	switch (ccb->csio.scsi_status) {
1302 	case SCSI_STATUS_OK:
1303 	case SCSI_STATUS_COND_MET:
1304 	case SCSI_STATUS_INTERMED:
1305 	case SCSI_STATUS_INTERMED_COND_MET:
1306 		error = 0;
1307 		break;
1308 	case SCSI_STATUS_CMD_TERMINATED:
1309 	case SCSI_STATUS_CHECK_COND:
1310 		error = camperiphscsisenseerror(ccb,
1311 					        camflags,
1312 					        sense_flags,
1313 					        save_ccb,
1314 					        openings,
1315 					        relsim_flags,
1316 					        timeout);
1317 		break;
1318 	case SCSI_STATUS_QUEUE_FULL:
1319 	{
1320 		/* no decrement */
1321 		struct ccb_getdevstats cgds;
1322 
1323 		/*
1324 		 * First off, find out what the current
1325 		 * transaction counts are.
1326 		 */
1327 		xpt_setup_ccb(&cgds.ccb_h,
1328 			      ccb->ccb_h.path,
1329 			      /*priority*/1);
1330 		cgds.ccb_h.func_code = XPT_GDEV_STATS;
1331 		xpt_action((union ccb *)&cgds);
1332 
1333 		/*
1334 		 * If we were the only transaction active, treat
1335 		 * the QUEUE FULL as if it were a BUSY condition.
1336 		 */
1337 		if (cgds.dev_active != 0) {
1338 			int total_openings;
1339 
1340 			/*
1341 			 * Reduce the number of openings to
1342 			 * be 1 less than the amount it took
1343 			 * to get a queue full bounded by the
1344 			 * minimum allowed tag count for this
1345 			 * device.
1346 			 */
1347 			total_openings = cgds.dev_active + cgds.dev_openings;
1348 			*openings = cgds.dev_active;
1349 			if (*openings < cgds.mintags)
1350 				*openings = cgds.mintags;
1351 			if (*openings < total_openings)
1352 				*relsim_flags = RELSIM_ADJUST_OPENINGS;
1353 			else {
1354 				/*
1355 				 * Some devices report queue full for
1356 				 * temporary resource shortages.  For
1357 				 * this reason, we allow a minimum
1358 				 * tag count to be entered via a
1359 				 * quirk entry to prevent the queue
1360 				 * count on these devices from falling
1361 				 * to a pessimisticly low value.  We
1362 				 * still wait for the next successful
1363 				 * completion, however, before queueing
1364 				 * more transactions to the device.
1365 				 */
1366 				*relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
1367 			}
1368 			*timeout = 0;
1369 			error = ERESTART;
1370 			if (bootverbose) {
1371 				xpt_print(ccb->ccb_h.path, "Queue Full\n");
1372 			}
1373 			break;
1374 		}
1375 		/* FALLTHROUGH */
1376 	}
1377 	case SCSI_STATUS_BUSY:
1378 		/*
1379 		 * Restart the queue after either another
1380 		 * command completes or a 1 second timeout.
1381 		 */
1382 		if (bootverbose) {
1383 			xpt_print(ccb->ccb_h.path, "Device Busy\n");
1384 		}
1385 		if (ccb->ccb_h.retry_count > 0) {
1386 			ccb->ccb_h.retry_count--;
1387 			error = ERESTART;
1388 			*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1389 				      | RELSIM_RELEASE_AFTER_CMDCMPLT;
1390 			*timeout = 1000;
1391 		} else {
1392 			error = EIO;
1393 		}
1394 		break;
1395 	case SCSI_STATUS_RESERV_CONFLICT:
1396 		xpt_print(ccb->ccb_h.path, "Reservation Conflict\n");
1397 		error = EIO;
1398 		break;
1399 	default:
1400 		xpt_print(ccb->ccb_h.path, "SCSI Status 0x%x\n",
1401 		    ccb->csio.scsi_status);
1402 		error = EIO;
1403 		break;
1404 	}
1405 	return (error);
1406 }
1407 
1408 static int
1409 camperiphscsisenseerror(union ccb *ccb, cam_flags camflags,
1410 			u_int32_t sense_flags, union ccb *save_ccb,
1411 		       int *openings, u_int32_t *relsim_flags,
1412 		       u_int32_t *timeout)
1413 {
1414 	struct cam_periph *periph;
1415 	int error;
1416 
1417 	periph = xpt_path_periph(ccb->ccb_h.path);
1418 	if (periph->flags & CAM_PERIPH_RECOVERY_INPROG) {
1419 
1420 		/*
1421 		 * If error recovery is already in progress, don't attempt
1422 		 * to process this error, but requeue it unconditionally
1423 		 * and attempt to process it once error recovery has
1424 		 * completed.  This failed command is probably related to
1425 		 * the error that caused the currently active error recovery
1426 		 * action so our  current recovery efforts should also
1427 		 * address this command.  Be aware that the error recovery
1428 		 * code assumes that only one recovery action is in progress
1429 		 * on a particular peripheral instance at any given time
1430 		 * (e.g. only one saved CCB for error recovery) so it is
1431 		 * imperitive that we don't violate this assumption.
1432 		 */
1433 		error = ERESTART;
1434 	} else {
1435 		scsi_sense_action err_action;
1436 		struct ccb_getdev cgd;
1437 		const char *action_string;
1438 		union ccb* print_ccb;
1439 
1440 		/* A description of the error recovery action performed */
1441 		action_string = NULL;
1442 
1443 		/*
1444 		 * The location of the orignal ccb
1445 		 * for sense printing purposes.
1446 		 */
1447 		print_ccb = ccb;
1448 
1449 		/*
1450 		 * Grab the inquiry data for this device.
1451 		 */
1452 		xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, /*priority*/ 1);
1453 		cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1454 		xpt_action((union ccb *)&cgd);
1455 
1456 		if ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)
1457 			err_action = scsi_error_action(&ccb->csio,
1458 						       &cgd.inq_data,
1459 						       sense_flags);
1460 		else if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
1461 			err_action = SS_REQSENSE;
1462 		else
1463 			err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
1464 
1465 		error = err_action & SS_ERRMASK;
1466 
1467 		/*
1468 		 * If the recovery action will consume a retry,
1469 		 * make sure we actually have retries available.
1470 		 */
1471 		if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
1472 			if (ccb->ccb_h.retry_count > 0)
1473 				ccb->ccb_h.retry_count--;
1474 			else {
1475 				action_string = "Retries Exhausted";
1476 				goto sense_error_done;
1477 			}
1478 		}
1479 
1480 		if ((err_action & SS_MASK) >= SS_START) {
1481 			/*
1482 			 * Do common portions of commands that
1483 			 * use recovery CCBs.
1484 			 */
1485 			if (save_ccb == NULL) {
1486 				action_string = "No recovery CCB supplied";
1487 				goto sense_error_done;
1488 			}
1489 			bcopy(ccb, save_ccb, sizeof(*save_ccb));
1490 			print_ccb = save_ccb;
1491 			periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1492 		}
1493 
1494 		switch (err_action & SS_MASK) {
1495 		case SS_NOP:
1496 			action_string = "No Recovery Action Needed";
1497 			error = 0;
1498 			break;
1499 		case SS_RETRY:
1500 			action_string = "Retrying Command (per Sense Data)";
1501 			error = ERESTART;
1502 			break;
1503 		case SS_FAIL:
1504 			action_string = "Unretryable error";
1505 			break;
1506 		case SS_START:
1507 		{
1508 			int le;
1509 
1510 			/*
1511 			 * Send a start unit command to the device, and
1512 			 * then retry the command.
1513 			 */
1514 			action_string = "Attempting to Start Unit";
1515 
1516 			/*
1517 			 * Check for removable media and set
1518 			 * load/eject flag appropriately.
1519 			 */
1520 			if (SID_IS_REMOVABLE(&cgd.inq_data))
1521 				le = TRUE;
1522 			else
1523 				le = FALSE;
1524 
1525 			scsi_start_stop(&ccb->csio,
1526 					/*retries*/1,
1527 					camperiphdone,
1528 					MSG_SIMPLE_Q_TAG,
1529 					/*start*/TRUE,
1530 					/*load/eject*/le,
1531 					/*immediate*/FALSE,
1532 					SSD_FULL_SIZE,
1533 					/*timeout*/50000);
1534 			break;
1535 		}
1536 		case SS_TUR:
1537 		{
1538 			/*
1539 			 * Send a Test Unit Ready to the device.
1540 			 * If the 'many' flag is set, we send 120
1541 			 * test unit ready commands, one every half
1542 			 * second.  Otherwise, we just send one TUR.
1543 			 * We only want to do this if the retry
1544 			 * count has not been exhausted.
1545 			 */
1546 			int retries;
1547 
1548 			if ((err_action & SSQ_MANY) != 0) {
1549 				action_string = "Polling device for readiness";
1550 				retries = 120;
1551 			} else {
1552 				action_string = "Testing device for readiness";
1553 				retries = 1;
1554 			}
1555 			scsi_test_unit_ready(&ccb->csio,
1556 					     retries,
1557 					     camperiphdone,
1558 					     MSG_SIMPLE_Q_TAG,
1559 					     SSD_FULL_SIZE,
1560 					     /*timeout*/5000);
1561 
1562 			/*
1563 			 * Accomplish our 500ms delay by deferring
1564 			 * the release of our device queue appropriately.
1565 			 */
1566 			*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1567 			*timeout = 500;
1568 			break;
1569 		}
1570 		case SS_REQSENSE:
1571 		{
1572 			/*
1573 			 * Send a Request Sense to the device.  We
1574 			 * assume that we are in a contingent allegiance
1575 			 * condition so we do not tag this request.
1576 			 */
1577 			scsi_request_sense(&ccb->csio, /*retries*/1,
1578 					   camperiphdone,
1579 					   &save_ccb->csio.sense_data,
1580 					   sizeof(save_ccb->csio.sense_data),
1581 					   CAM_TAG_ACTION_NONE,
1582 					   /*sense_len*/SSD_FULL_SIZE,
1583 					   /*timeout*/5000);
1584 			break;
1585 		}
1586 		default:
1587 			panic("Unhandled error action %x", err_action);
1588 		}
1589 
1590 		if ((err_action & SS_MASK) >= SS_START) {
1591 			/*
1592 			 * Drop the priority to 0 so that the recovery
1593 			 * CCB is the first to execute.  Freeze the queue
1594 			 * after this command is sent so that we can
1595 			 * restore the old csio and have it queued in
1596 			 * the proper order before we release normal
1597 			 * transactions to the device.
1598 			 */
1599 			ccb->ccb_h.pinfo.priority = 0;
1600 			ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1601 			ccb->ccb_h.saved_ccb_ptr = save_ccb;
1602 			error = ERESTART;
1603 		}
1604 
1605 sense_error_done:
1606 		if ((err_action & SSQ_PRINT_SENSE) != 0
1607 		 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0) {
1608 			cam_error_print(print_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1609 			xpt_print_path(ccb->ccb_h.path);
1610 			if (bootverbose)
1611 				scsi_sense_print(&print_ccb->csio);
1612 			kprintf("%s\n", action_string);
1613 		}
1614 	}
1615 	return (error);
1616 }
1617 
1618 /*
1619  * Generic error handler.  Peripheral drivers usually filter
1620  * out the errors that they handle in a unique mannor, then
1621  * call this function.
1622  */
1623 int
1624 cam_periph_error(union ccb *ccb, cam_flags camflags,
1625 		 u_int32_t sense_flags, union ccb *save_ccb)
1626 {
1627 	const char *action_string;
1628 	cam_status  status;
1629 	int	    frozen;
1630 	int	    error, printed = 0;
1631 	int         openings;
1632 	u_int32_t   relsim_flags;
1633 	u_int32_t   timeout = 0;
1634 
1635 	action_string = NULL;
1636 	status = ccb->ccb_h.status;
1637 	frozen = (status & CAM_DEV_QFRZN) != 0;
1638 	status &= CAM_STATUS_MASK;
1639 	openings = relsim_flags = 0;
1640 
1641 	switch (status) {
1642 	case CAM_REQ_CMP:
1643 		error = 0;
1644 		break;
1645 	case CAM_SCSI_STATUS_ERROR:
1646 		error = camperiphscsistatuserror(ccb,
1647 						 camflags,
1648 						 sense_flags,
1649 						 save_ccb,
1650 						 &openings,
1651 						 &relsim_flags,
1652 						 &timeout);
1653 		break;
1654 	case CAM_AUTOSENSE_FAIL:
1655 		xpt_print(ccb->ccb_h.path, "AutoSense Failed\n");
1656 		error = EIO;	/* we have to kill the command */
1657 		break;
1658 	case CAM_REQ_CMP_ERR:
1659 		if (bootverbose && printed == 0) {
1660 			xpt_print(ccb->ccb_h.path,
1661 			    "Request completed with CAM_REQ_CMP_ERR\n");
1662 			printed++;
1663 		}
1664 		/* FALLTHROUGH */
1665 	case CAM_CMD_TIMEOUT:
1666 		if (bootverbose && printed == 0) {
1667 			xpt_print(ccb->ccb_h.path, "Command timed out\n");
1668 			printed++;
1669 		}
1670 		/* FALLTHROUGH */
1671 	case CAM_UNEXP_BUSFREE:
1672 		if (bootverbose && printed == 0) {
1673 			xpt_print(ccb->ccb_h.path, "Unexpected Bus Free\n");
1674 			printed++;
1675 		}
1676 		/* FALLTHROUGH */
1677 	case CAM_UNCOR_PARITY:
1678 		if (bootverbose && printed == 0) {
1679 			xpt_print(ccb->ccb_h.path,
1680 			    "Uncorrected Parity Error\n");
1681 			printed++;
1682 		}
1683 		/* FALLTHROUGH */
1684 	case CAM_DATA_RUN_ERR:
1685 		if (bootverbose && printed == 0) {
1686 			xpt_print(ccb->ccb_h.path, "Data Overrun\n");
1687 			printed++;
1688 		}
1689 		error = EIO;	/* we have to kill the command */
1690 		/* decrement the number of retries */
1691 		if (ccb->ccb_h.retry_count > 0) {
1692 			ccb->ccb_h.retry_count--;
1693 			error = ERESTART;
1694 		} else {
1695 			action_string = "Retries Exhausted";
1696 			error = EIO;
1697 		}
1698 		break;
1699 	case CAM_UA_ABORT:
1700 	case CAM_UA_TERMIO:
1701 	case CAM_MSG_REJECT_REC:
1702 		/* XXX Don't know that these are correct */
1703 		error = EIO;
1704 		break;
1705 	case CAM_SEL_TIMEOUT:
1706 	{
1707 		struct cam_path *newpath;
1708 
1709 		if ((camflags & CAM_RETRY_SELTO) != 0) {
1710 			if (ccb->ccb_h.retry_count > 0) {
1711 
1712 				ccb->ccb_h.retry_count--;
1713 				error = ERESTART;
1714 				if (bootverbose && printed == 0) {
1715 					xpt_print(ccb->ccb_h.path,
1716 					    "Selection Timeout\n");
1717 					printed++;
1718 				}
1719 
1720 				/*
1721 				 * Wait a bit to give the device
1722 				 * time to recover before we try again.
1723 				 */
1724 				relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1725 				timeout = periph_selto_delay;
1726 				break;
1727 			}
1728 		}
1729 		error = ENXIO;
1730 		/* Should we do more if we can't create the path?? */
1731 		if (xpt_create_path(&newpath, xpt_path_periph(ccb->ccb_h.path),
1732 				    xpt_path_path_id(ccb->ccb_h.path),
1733 				    xpt_path_target_id(ccb->ccb_h.path),
1734 				    CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1735 			break;
1736 
1737 		/*
1738 		 * Let peripheral drivers know that this device has gone
1739 		 * away.
1740 		 */
1741 		xpt_async(AC_LOST_DEVICE, newpath, NULL);
1742 		xpt_free_path(newpath);
1743 		break;
1744 	}
1745 	case CAM_REQ_INVALID:
1746 	case CAM_PATH_INVALID:
1747 	case CAM_DEV_NOT_THERE:
1748 	case CAM_NO_HBA:
1749 	case CAM_PROVIDE_FAIL:
1750 	case CAM_REQ_TOO_BIG:
1751 	case CAM_LUN_INVALID:
1752 	case CAM_TID_INVALID:
1753 		error = EINVAL;
1754 		break;
1755 	case CAM_SCSI_BUS_RESET:
1756 	case CAM_BDR_SENT:
1757 		/*
1758 		 * Commands that repeatedly timeout and cause these
1759 		 * kinds of error recovery actions, should return
1760 		 * CAM_CMD_TIMEOUT, which allows us to safely assume
1761 		 * that this command was an innocent bystander to
1762 		 * these events and should be unconditionally
1763 		 * retried.
1764 		 */
1765 		if (bootverbose && printed == 0) {
1766 			xpt_print_path(ccb->ccb_h.path);
1767 			if (status == CAM_BDR_SENT)
1768 				kprintf("Bus Device Reset sent\n");
1769 			else
1770 				kprintf("Bus Reset issued\n");
1771 			printed++;
1772 		}
1773 		/* FALLTHROUGH */
1774 	case CAM_REQUEUE_REQ:
1775 		/* Unconditional requeue */
1776 		error = ERESTART;
1777 		if (bootverbose && printed == 0) {
1778 			xpt_print(ccb->ccb_h.path, "Request Requeued\n");
1779 			printed++;
1780 		}
1781 		break;
1782 	case CAM_RESRC_UNAVAIL:
1783 		/* Wait a bit for the resource shortage to abate. */
1784 		timeout = periph_noresrc_delay;
1785 		/* FALLTHROUGH */
1786 	case CAM_BUSY:
1787 		if (timeout == 0) {
1788 			/* Wait a bit for the busy condition to abate. */
1789 			timeout = periph_busy_delay;
1790 		}
1791 		relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1792 		/* FALLTHROUGH */
1793 	default:
1794 		/* decrement the number of retries */
1795 		if (ccb->ccb_h.retry_count > 0) {
1796 			ccb->ccb_h.retry_count--;
1797 			error = ERESTART;
1798 			if (bootverbose && printed == 0) {
1799 				xpt_print(ccb->ccb_h.path, "CAM Status 0x%x\n",
1800 				    status);
1801 				printed++;
1802 			}
1803 		} else {
1804 			error = EIO;
1805 			action_string = "Retries Exhausted";
1806 		}
1807 		break;
1808 	}
1809 
1810 	/* Attempt a retry */
1811 	if (error == ERESTART || error == 0) {
1812 		if (frozen != 0)
1813 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1814 
1815 		if (error == ERESTART) {
1816 			action_string = "Retrying Command";
1817 			xpt_action(ccb);
1818 		}
1819 
1820 		if (frozen != 0)
1821 			cam_release_devq(ccb->ccb_h.path,
1822 					 relsim_flags,
1823 					 openings,
1824 					 timeout,
1825 					 /*getcount_only*/0);
1826 	}
1827 
1828 	/*
1829 	 * If we have an error and are booting verbosely, whine
1830 	 * *unless* this was a non-retryable selection timeout.
1831 	 */
1832 	if (error != 0 && bootverbose &&
1833 	    !(status == CAM_SEL_TIMEOUT && (camflags & CAM_RETRY_SELTO) == 0)) {
1834 
1835 
1836 		if (action_string == NULL)
1837 			action_string = "Unretryable Error";
1838 		if (error != ERESTART) {
1839 			xpt_print(ccb->ccb_h.path, "error %d\n", error);
1840 		}
1841 		xpt_print(ccb->ccb_h.path, "%s\n", action_string);
1842 	}
1843 
1844 	return (error);
1845 }
1846