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