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