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