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