xref: /freebsd/sys/cam/cam_periph.c (revision f15e18a642cb3f7ebc747f8e9cdf11274140107d)
1 /*-
2  * Common functions for CAM "type" (peripheral) drivers.
3  *
4  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5  *
6  * Copyright (c) 1997, 1998 Justin T. Gibbs.
7  * Copyright (c) 1997, 1998, 1999, 2000 Kenneth D. Merry.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions, and the following disclaimer,
15  *    without modification, immediately at the beginning of the file.
16  * 2. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/types.h>
38 #include <sys/malloc.h>
39 #include <sys/kernel.h>
40 #include <sys/bio.h>
41 #include <sys/conf.h>
42 #include <sys/devctl.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/buf.h>
46 #include <sys/proc.h>
47 #include <sys/devicestat.h>
48 #include <sys/sbuf.h>
49 #include <sys/sysctl.h>
50 #include <vm/vm.h>
51 #include <vm/vm_extern.h>
52 
53 #include <cam/cam.h>
54 #include <cam/cam_ccb.h>
55 #include <cam/cam_queue.h>
56 #include <cam/cam_xpt_periph.h>
57 #include <cam/cam_xpt_internal.h>
58 #include <cam/cam_periph.h>
59 #include <cam/cam_debug.h>
60 #include <cam/cam_sim.h>
61 
62 #include <cam/scsi/scsi_all.h>
63 #include <cam/scsi/scsi_message.h>
64 #include <cam/scsi/scsi_pass.h>
65 
66 static	u_int		camperiphnextunit(struct periph_driver *p_drv,
67 					  u_int newunit, int wired,
68 					  path_id_t pathid, target_id_t target,
69 					  lun_id_t lun);
70 static	u_int		camperiphunit(struct periph_driver *p_drv,
71 				      path_id_t pathid, target_id_t target,
72 				      lun_id_t lun);
73 static	void		camperiphdone(struct cam_periph *periph,
74 					union ccb *done_ccb);
75 static  void		camperiphfree(struct cam_periph *periph);
76 static int		camperiphscsistatuserror(union ccb *ccb,
77 					        union ccb **orig_ccb,
78 						 cam_flags camflags,
79 						 u_int32_t sense_flags,
80 						 int *openings,
81 						 u_int32_t *relsim_flags,
82 						 u_int32_t *timeout,
83 						 u_int32_t  *action,
84 						 const char **action_string);
85 static	int		camperiphscsisenseerror(union ccb *ccb,
86 					        union ccb **orig_ccb,
87 					        cam_flags camflags,
88 					        u_int32_t sense_flags,
89 					        int *openings,
90 					        u_int32_t *relsim_flags,
91 					        u_int32_t *timeout,
92 					        u_int32_t *action,
93 					        const char **action_string);
94 static void		cam_periph_devctl_notify(union ccb *ccb);
95 
96 static int nperiph_drivers;
97 static int initialized = 0;
98 struct periph_driver **periph_drivers;
99 
100 static MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers");
101 
102 static int periph_selto_delay = 1000;
103 TUNABLE_INT("kern.cam.periph_selto_delay", &periph_selto_delay);
104 static int periph_noresrc_delay = 500;
105 TUNABLE_INT("kern.cam.periph_noresrc_delay", &periph_noresrc_delay);
106 static int periph_busy_delay = 500;
107 TUNABLE_INT("kern.cam.periph_busy_delay", &periph_busy_delay);
108 
109 static u_int periph_mapmem_thresh = 65536;
110 SYSCTL_UINT(_kern_cam, OID_AUTO, mapmem_thresh, CTLFLAG_RWTUN,
111     &periph_mapmem_thresh, 0, "Threshold for user-space buffer mapping");
112 
113 void
114 periphdriver_register(void *data)
115 {
116 	struct periph_driver *drv = (struct periph_driver *)data;
117 	struct periph_driver **newdrivers, **old;
118 	int ndrivers;
119 
120 again:
121 	ndrivers = nperiph_drivers + 2;
122 	newdrivers = malloc(sizeof(*newdrivers) * ndrivers, M_CAMPERIPH,
123 			    M_WAITOK);
124 	xpt_lock_buses();
125 	if (ndrivers != nperiph_drivers + 2) {
126 		/*
127 		 * Lost race against itself; go around.
128 		 */
129 		xpt_unlock_buses();
130 		free(newdrivers, M_CAMPERIPH);
131 		goto again;
132 	}
133 	if (periph_drivers)
134 		bcopy(periph_drivers, newdrivers,
135 		      sizeof(*newdrivers) * nperiph_drivers);
136 	newdrivers[nperiph_drivers] = drv;
137 	newdrivers[nperiph_drivers + 1] = NULL;
138 	old = periph_drivers;
139 	periph_drivers = newdrivers;
140 	nperiph_drivers++;
141 	xpt_unlock_buses();
142 	if (old)
143 		free(old, M_CAMPERIPH);
144 	/* If driver marked as early or it is late now, initialize it. */
145 	if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
146 	    initialized > 1)
147 		(*drv->init)();
148 }
149 
150 int
151 periphdriver_unregister(void *data)
152 {
153 	struct periph_driver *drv = (struct periph_driver *)data;
154 	int error, n;
155 
156 	/* If driver marked as early or it is late now, deinitialize it. */
157 	if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
158 	    initialized > 1) {
159 		if (drv->deinit == NULL) {
160 			printf("CAM periph driver '%s' doesn't have deinit.\n",
161 			    drv->driver_name);
162 			return (EOPNOTSUPP);
163 		}
164 		error = drv->deinit();
165 		if (error != 0)
166 			return (error);
167 	}
168 
169 	xpt_lock_buses();
170 	for (n = 0; n < nperiph_drivers && periph_drivers[n] != drv; n++)
171 		;
172 	KASSERT(n < nperiph_drivers,
173 	    ("Periph driver '%s' was not registered", drv->driver_name));
174 	for (; n + 1 < nperiph_drivers; n++)
175 		periph_drivers[n] = periph_drivers[n + 1];
176 	periph_drivers[n + 1] = NULL;
177 	nperiph_drivers--;
178 	xpt_unlock_buses();
179 	return (0);
180 }
181 
182 void
183 periphdriver_init(int level)
184 {
185 	int	i, early;
186 
187 	initialized = max(initialized, level);
188 	for (i = 0; periph_drivers[i] != NULL; i++) {
189 		early = (periph_drivers[i]->flags & CAM_PERIPH_DRV_EARLY) ? 1 : 2;
190 		if (early == initialized)
191 			(*periph_drivers[i]->init)();
192 	}
193 }
194 
195 cam_status
196 cam_periph_alloc(periph_ctor_t *periph_ctor,
197 		 periph_oninv_t *periph_oninvalidate,
198 		 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
199 		 char *name, cam_periph_type type, struct cam_path *path,
200 		 ac_callback_t *ac_callback, ac_code code, void *arg)
201 {
202 	struct		periph_driver **p_drv;
203 	struct		cam_sim *sim;
204 	struct		cam_periph *periph;
205 	struct		cam_periph *cur_periph;
206 	path_id_t	path_id;
207 	target_id_t	target_id;
208 	lun_id_t	lun_id;
209 	cam_status	status;
210 	u_int		init_level;
211 
212 	init_level = 0;
213 	/*
214 	 * Handle Hot-Plug scenarios.  If there is already a peripheral
215 	 * of our type assigned to this path, we are likely waiting for
216 	 * final close on an old, invalidated, peripheral.  If this is
217 	 * the case, queue up a deferred call to the peripheral's async
218 	 * handler.  If it looks like a mistaken re-allocation, complain.
219 	 */
220 	if ((periph = cam_periph_find(path, name)) != NULL) {
221 		if ((periph->flags & CAM_PERIPH_INVALID) != 0
222 		 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
223 			periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
224 			periph->deferred_callback = ac_callback;
225 			periph->deferred_ac = code;
226 			return (CAM_REQ_INPROG);
227 		} else {
228 			printf("cam_periph_alloc: attempt to re-allocate "
229 			       "valid device %s%d rejected flags %#x "
230 			       "refcount %d\n", periph->periph_name,
231 			       periph->unit_number, periph->flags,
232 			       periph->refcount);
233 		}
234 		return (CAM_REQ_INVALID);
235 	}
236 
237 	periph = (struct cam_periph *)malloc(sizeof(*periph), M_CAMPERIPH,
238 					     M_NOWAIT|M_ZERO);
239 
240 	if (periph == NULL)
241 		return (CAM_RESRC_UNAVAIL);
242 
243 	init_level++;
244 
245 	sim = xpt_path_sim(path);
246 	path_id = xpt_path_path_id(path);
247 	target_id = xpt_path_target_id(path);
248 	lun_id = xpt_path_lun_id(path);
249 	periph->periph_start = periph_start;
250 	periph->periph_dtor = periph_dtor;
251 	periph->periph_oninval = periph_oninvalidate;
252 	periph->type = type;
253 	periph->periph_name = name;
254 	periph->scheduled_priority = CAM_PRIORITY_NONE;
255 	periph->immediate_priority = CAM_PRIORITY_NONE;
256 	periph->refcount = 1;		/* Dropped by invalidation. */
257 	periph->sim = sim;
258 	SLIST_INIT(&periph->ccb_list);
259 	status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
260 	if (status != CAM_REQ_CMP)
261 		goto failure;
262 	periph->path = path;
263 
264 	xpt_lock_buses();
265 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
266 		if (strcmp((*p_drv)->driver_name, name) == 0)
267 			break;
268 	}
269 	if (*p_drv == NULL) {
270 		printf("cam_periph_alloc: invalid periph name '%s'\n", name);
271 		xpt_unlock_buses();
272 		xpt_free_path(periph->path);
273 		free(periph, M_CAMPERIPH);
274 		return (CAM_REQ_INVALID);
275 	}
276 	periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
277 	cur_periph = TAILQ_FIRST(&(*p_drv)->units);
278 	while (cur_periph != NULL
279 	    && cur_periph->unit_number < periph->unit_number)
280 		cur_periph = TAILQ_NEXT(cur_periph, unit_links);
281 	if (cur_periph != NULL) {
282 		KASSERT(cur_periph->unit_number != periph->unit_number, ("duplicate units on periph list"));
283 		TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
284 	} else {
285 		TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
286 		(*p_drv)->generation++;
287 	}
288 	xpt_unlock_buses();
289 
290 	init_level++;
291 
292 	status = xpt_add_periph(periph);
293 	if (status != CAM_REQ_CMP)
294 		goto failure;
295 
296 	init_level++;
297 	CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph created\n"));
298 
299 	status = periph_ctor(periph, arg);
300 
301 	if (status == CAM_REQ_CMP)
302 		init_level++;
303 
304 failure:
305 	switch (init_level) {
306 	case 4:
307 		/* Initialized successfully */
308 		break;
309 	case 3:
310 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
311 		xpt_remove_periph(periph);
312 		/* FALLTHROUGH */
313 	case 2:
314 		xpt_lock_buses();
315 		TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
316 		xpt_unlock_buses();
317 		xpt_free_path(periph->path);
318 		/* FALLTHROUGH */
319 	case 1:
320 		free(periph, M_CAMPERIPH);
321 		/* FALLTHROUGH */
322 	case 0:
323 		/* No cleanup to perform. */
324 		break;
325 	default:
326 		panic("%s: Unknown init level", __func__);
327 	}
328 	return(status);
329 }
330 
331 /*
332  * Find a peripheral structure with the specified path, target, lun,
333  * and (optionally) type.  If the name is NULL, this function will return
334  * the first peripheral driver that matches the specified path.
335  */
336 struct cam_periph *
337 cam_periph_find(struct cam_path *path, char *name)
338 {
339 	struct periph_driver **p_drv;
340 	struct cam_periph *periph;
341 
342 	xpt_lock_buses();
343 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
344 		if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
345 			continue;
346 
347 		TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
348 			if (xpt_path_comp(periph->path, path) == 0) {
349 				xpt_unlock_buses();
350 				cam_periph_assert(periph, MA_OWNED);
351 				return(periph);
352 			}
353 		}
354 		if (name != NULL) {
355 			xpt_unlock_buses();
356 			return(NULL);
357 		}
358 	}
359 	xpt_unlock_buses();
360 	return(NULL);
361 }
362 
363 /*
364  * Find peripheral driver instances attached to the specified path.
365  */
366 int
367 cam_periph_list(struct cam_path *path, struct sbuf *sb)
368 {
369 	struct sbuf local_sb;
370 	struct periph_driver **p_drv;
371 	struct cam_periph *periph;
372 	int count;
373 	int sbuf_alloc_len;
374 
375 	sbuf_alloc_len = 16;
376 retry:
377 	sbuf_new(&local_sb, NULL, sbuf_alloc_len, SBUF_FIXEDLEN);
378 	count = 0;
379 	xpt_lock_buses();
380 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
381 		TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
382 			if (xpt_path_comp(periph->path, path) != 0)
383 				continue;
384 
385 			if (sbuf_len(&local_sb) != 0)
386 				sbuf_cat(&local_sb, ",");
387 
388 			sbuf_printf(&local_sb, "%s%d", periph->periph_name,
389 				    periph->unit_number);
390 
391 			if (sbuf_error(&local_sb) == ENOMEM) {
392 				sbuf_alloc_len *= 2;
393 				xpt_unlock_buses();
394 				sbuf_delete(&local_sb);
395 				goto retry;
396 			}
397 			count++;
398 		}
399 	}
400 	xpt_unlock_buses();
401 	sbuf_finish(&local_sb);
402 	if (sbuf_len(sb) != 0)
403 		sbuf_cat(sb, ",");
404 	sbuf_cat(sb, sbuf_data(&local_sb));
405 	sbuf_delete(&local_sb);
406 	return (count);
407 }
408 
409 int
410 cam_periph_acquire(struct cam_periph *periph)
411 {
412 	int status;
413 
414 	if (periph == NULL)
415 		return (EINVAL);
416 
417 	status = ENOENT;
418 	xpt_lock_buses();
419 	if ((periph->flags & CAM_PERIPH_INVALID) == 0) {
420 		periph->refcount++;
421 		status = 0;
422 	}
423 	xpt_unlock_buses();
424 
425 	return (status);
426 }
427 
428 void
429 cam_periph_doacquire(struct cam_periph *periph)
430 {
431 
432 	xpt_lock_buses();
433 	KASSERT(periph->refcount >= 1,
434 	    ("cam_periph_doacquire() with refcount == %d", periph->refcount));
435 	periph->refcount++;
436 	xpt_unlock_buses();
437 }
438 
439 void
440 cam_periph_release_locked_buses(struct cam_periph *periph)
441 {
442 
443 	cam_periph_assert(periph, MA_OWNED);
444 	KASSERT(periph->refcount >= 1, ("periph->refcount >= 1"));
445 	if (--periph->refcount == 0)
446 		camperiphfree(periph);
447 }
448 
449 void
450 cam_periph_release_locked(struct cam_periph *periph)
451 {
452 
453 	if (periph == NULL)
454 		return;
455 
456 	xpt_lock_buses();
457 	cam_periph_release_locked_buses(periph);
458 	xpt_unlock_buses();
459 }
460 
461 void
462 cam_periph_release(struct cam_periph *periph)
463 {
464 	struct mtx *mtx;
465 
466 	if (periph == NULL)
467 		return;
468 
469 	cam_periph_assert(periph, MA_NOTOWNED);
470 	mtx = cam_periph_mtx(periph);
471 	mtx_lock(mtx);
472 	cam_periph_release_locked(periph);
473 	mtx_unlock(mtx);
474 }
475 
476 /*
477  * hold/unhold act as mutual exclusion for sections of the code that
478  * need to sleep and want to make sure that other sections that
479  * will interfere are held off. This only protects exclusive sections
480  * from each other.
481  */
482 int
483 cam_periph_hold(struct cam_periph *periph, int priority)
484 {
485 	int error;
486 
487 	/*
488 	 * Increment the reference count on the peripheral
489 	 * while we wait for our lock attempt to succeed
490 	 * to ensure the peripheral doesn't disappear out
491 	 * from user us while we sleep.
492 	 */
493 
494 	if (cam_periph_acquire(periph) != 0)
495 		return (ENXIO);
496 
497 	cam_periph_assert(periph, MA_OWNED);
498 	while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
499 		periph->flags |= CAM_PERIPH_LOCK_WANTED;
500 		if ((error = cam_periph_sleep(periph, periph, priority,
501 		    "caplck", 0)) != 0) {
502 			cam_periph_release_locked(periph);
503 			return (error);
504 		}
505 		if (periph->flags & CAM_PERIPH_INVALID) {
506 			cam_periph_release_locked(periph);
507 			return (ENXIO);
508 		}
509 	}
510 
511 	periph->flags |= CAM_PERIPH_LOCKED;
512 	return (0);
513 }
514 
515 void
516 cam_periph_unhold(struct cam_periph *periph)
517 {
518 
519 	cam_periph_assert(periph, MA_OWNED);
520 
521 	periph->flags &= ~CAM_PERIPH_LOCKED;
522 	if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
523 		periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
524 		wakeup(periph);
525 	}
526 
527 	cam_periph_release_locked(periph);
528 }
529 
530 /*
531  * Look for the next unit number that is not currently in use for this
532  * peripheral type starting at "newunit".  Also exclude unit numbers that
533  * are reserved by for future "hardwiring" unless we already know that this
534  * is a potential wired device.  Only assume that the device is "wired" the
535  * first time through the loop since after that we'll be looking at unit
536  * numbers that did not match a wiring entry.
537  */
538 static u_int
539 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
540 		  path_id_t pathid, target_id_t target, lun_id_t lun)
541 {
542 	struct	cam_periph *periph;
543 	char	*periph_name;
544 	int	i, val, dunit, r;
545 	const char *dname, *strval;
546 
547 	periph_name = p_drv->driver_name;
548 	for (;;newunit++) {
549 		for (periph = TAILQ_FIRST(&p_drv->units);
550 		     periph != NULL && periph->unit_number != newunit;
551 		     periph = TAILQ_NEXT(periph, unit_links))
552 			;
553 
554 		if (periph != NULL && periph->unit_number == newunit) {
555 			if (wired != 0) {
556 				xpt_print(periph->path, "Duplicate Wired "
557 				    "Device entry!\n");
558 				xpt_print(periph->path, "Second device (%s "
559 				    "device at scbus%d target %d lun %d) will "
560 				    "not be wired\n", periph_name, pathid,
561 				    target, lun);
562 				wired = 0;
563 			}
564 			continue;
565 		}
566 		if (wired)
567 			break;
568 
569 		/*
570 		 * Don't match entries like "da 4" as a wired down
571 		 * device, but do match entries like "da 4 target 5"
572 		 * or even "da 4 scbus 1".
573 		 */
574 		i = 0;
575 		dname = periph_name;
576 		for (;;) {
577 			r = resource_find_dev(&i, dname, &dunit, NULL, NULL);
578 			if (r != 0)
579 				break;
580 			/* if no "target" and no specific scbus, skip */
581 			if (resource_int_value(dname, dunit, "target", &val) &&
582 			    (resource_string_value(dname, dunit, "at",&strval)||
583 			     strcmp(strval, "scbus") == 0))
584 				continue;
585 			if (newunit == dunit)
586 				break;
587 		}
588 		if (r != 0)
589 			break;
590 	}
591 	return (newunit);
592 }
593 
594 static u_int
595 camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
596 	      target_id_t target, lun_id_t lun)
597 {
598 	u_int	unit;
599 	int	wired, i, val, dunit;
600 	const char *dname, *strval;
601 	char	pathbuf[32], *periph_name;
602 
603 	periph_name = p_drv->driver_name;
604 	snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
605 	unit = 0;
606 	i = 0;
607 	dname = periph_name;
608 	for (wired = 0; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0;
609 	     wired = 0) {
610 		if (resource_string_value(dname, dunit, "at", &strval) == 0) {
611 			if (strcmp(strval, pathbuf) != 0)
612 				continue;
613 			wired++;
614 		}
615 		if (resource_int_value(dname, dunit, "target", &val) == 0) {
616 			if (val != target)
617 				continue;
618 			wired++;
619 		}
620 		if (resource_int_value(dname, dunit, "lun", &val) == 0) {
621 			if (val != lun)
622 				continue;
623 			wired++;
624 		}
625 		if (wired != 0) {
626 			unit = dunit;
627 			break;
628 		}
629 	}
630 
631 	/*
632 	 * Either start from 0 looking for the next unit or from
633 	 * the unit number given in the resource config.  This way,
634 	 * if we have wildcard matches, we don't return the same
635 	 * unit number twice.
636 	 */
637 	unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun);
638 
639 	return (unit);
640 }
641 
642 void
643 cam_periph_invalidate(struct cam_periph *periph)
644 {
645 
646 	cam_periph_assert(periph, MA_OWNED);
647 	/*
648 	 * We only tear down the device the first time a peripheral is
649 	 * invalidated.
650 	 */
651 	if ((periph->flags & CAM_PERIPH_INVALID) != 0)
652 		return;
653 
654 	CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph invalidated\n"));
655 	if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting) {
656 		struct sbuf sb;
657 		char buffer[160];
658 
659 		sbuf_new(&sb, buffer, 160, SBUF_FIXEDLEN);
660 		xpt_denounce_periph_sbuf(periph, &sb);
661 		sbuf_finish(&sb);
662 		sbuf_putbuf(&sb);
663 	}
664 	periph->flags |= CAM_PERIPH_INVALID;
665 	periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
666 	if (periph->periph_oninval != NULL)
667 		periph->periph_oninval(periph);
668 	cam_periph_release_locked(periph);
669 }
670 
671 static void
672 camperiphfree(struct cam_periph *periph)
673 {
674 	struct periph_driver **p_drv;
675 	struct periph_driver *drv;
676 
677 	cam_periph_assert(periph, MA_OWNED);
678 	KASSERT(periph->periph_allocating == 0, ("%s%d: freed while allocating",
679 	    periph->periph_name, periph->unit_number));
680 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
681 		if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
682 			break;
683 	}
684 	if (*p_drv == NULL) {
685 		printf("camperiphfree: attempt to free non-existant periph\n");
686 		return;
687 	}
688 	/*
689 	 * Cache a pointer to the periph_driver structure.  If a
690 	 * periph_driver is added or removed from the array (see
691 	 * periphdriver_register()) while we drop the toplogy lock
692 	 * below, p_drv may change.  This doesn't protect against this
693 	 * particular periph_driver going away.  That will require full
694 	 * reference counting in the periph_driver infrastructure.
695 	 */
696 	drv = *p_drv;
697 
698 	/*
699 	 * We need to set this flag before dropping the topology lock, to
700 	 * let anyone who is traversing the list that this peripheral is
701 	 * about to be freed, and there will be no more reference count
702 	 * checks.
703 	 */
704 	periph->flags |= CAM_PERIPH_FREE;
705 
706 	/*
707 	 * The peripheral destructor semantics dictate calling with only the
708 	 * SIM mutex held.  Since it might sleep, it should not be called
709 	 * with the topology lock held.
710 	 */
711 	xpt_unlock_buses();
712 
713 	/*
714 	 * We need to call the peripheral destructor prior to removing the
715 	 * peripheral from the list.  Otherwise, we risk running into a
716 	 * scenario where the peripheral unit number may get reused
717 	 * (because it has been removed from the list), but some resources
718 	 * used by the peripheral are still hanging around.  In particular,
719 	 * the devfs nodes used by some peripherals like the pass(4) driver
720 	 * aren't fully cleaned up until the destructor is run.  If the
721 	 * unit number is reused before the devfs instance is fully gone,
722 	 * devfs will panic.
723 	 */
724 	if (periph->periph_dtor != NULL)
725 		periph->periph_dtor(periph);
726 
727 	/*
728 	 * The peripheral list is protected by the topology lock. We have to
729 	 * remove the periph from the drv list before we call deferred_ac. The
730 	 * AC_FOUND_DEVICE callback won't create a new periph if it's still there.
731 	 */
732 	xpt_lock_buses();
733 
734 	TAILQ_REMOVE(&drv->units, periph, unit_links);
735 	drv->generation++;
736 
737 	xpt_remove_periph(periph);
738 
739 	xpt_unlock_buses();
740 	if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting)
741 		xpt_print(periph->path, "Periph destroyed\n");
742 	else
743 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
744 
745 	if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
746 		union ccb ccb;
747 		void *arg;
748 
749 		switch (periph->deferred_ac) {
750 		case AC_FOUND_DEVICE:
751 			ccb.ccb_h.func_code = XPT_GDEV_TYPE;
752 			xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
753 			xpt_action(&ccb);
754 			arg = &ccb;
755 			break;
756 		case AC_PATH_REGISTERED:
757 			xpt_path_inq(&ccb.cpi, periph->path);
758 			arg = &ccb;
759 			break;
760 		default:
761 			arg = NULL;
762 			break;
763 		}
764 		periph->deferred_callback(NULL, periph->deferred_ac,
765 					  periph->path, arg);
766 	}
767 	xpt_free_path(periph->path);
768 	free(periph, M_CAMPERIPH);
769 	xpt_lock_buses();
770 }
771 
772 /*
773  * Map user virtual pointers into kernel virtual address space, so we can
774  * access the memory.  This is now a generic function that centralizes most
775  * of the sanity checks on the data flags, if any.
776  * This also only works for up to maxphys memory.  Since we use
777  * buffers to map stuff in and out, we're limited to the buffer size.
778  */
779 int
780 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo,
781     u_int maxmap)
782 {
783 	int numbufs, i;
784 	u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
785 	u_int32_t lengths[CAM_PERIPH_MAXMAPS];
786 	u_int32_t dirs[CAM_PERIPH_MAXMAPS];
787 
788 	bzero(mapinfo, sizeof(*mapinfo));
789 	if (maxmap == 0)
790 		maxmap = DFLTPHYS;	/* traditional default */
791 	else if (maxmap > maxphys)
792 		maxmap = maxphys;	/* for safety */
793 	switch(ccb->ccb_h.func_code) {
794 	case XPT_DEV_MATCH:
795 		if (ccb->cdm.match_buf_len == 0) {
796 			printf("cam_periph_mapmem: invalid match buffer "
797 			       "length 0\n");
798 			return(EINVAL);
799 		}
800 		if (ccb->cdm.pattern_buf_len > 0) {
801 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
802 			lengths[0] = ccb->cdm.pattern_buf_len;
803 			dirs[0] = CAM_DIR_OUT;
804 			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
805 			lengths[1] = ccb->cdm.match_buf_len;
806 			dirs[1] = CAM_DIR_IN;
807 			numbufs = 2;
808 		} else {
809 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
810 			lengths[0] = ccb->cdm.match_buf_len;
811 			dirs[0] = CAM_DIR_IN;
812 			numbufs = 1;
813 		}
814 		/*
815 		 * This request will not go to the hardware, no reason
816 		 * to be so strict. vmapbuf() is able to map up to maxphys.
817 		 */
818 		maxmap = maxphys;
819 		break;
820 	case XPT_SCSI_IO:
821 	case XPT_CONT_TARGET_IO:
822 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
823 			return(0);
824 		if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
825 			return (EINVAL);
826 		data_ptrs[0] = &ccb->csio.data_ptr;
827 		lengths[0] = ccb->csio.dxfer_len;
828 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
829 		numbufs = 1;
830 		break;
831 	case XPT_ATA_IO:
832 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
833 			return(0);
834 		if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
835 			return (EINVAL);
836 		data_ptrs[0] = &ccb->ataio.data_ptr;
837 		lengths[0] = ccb->ataio.dxfer_len;
838 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
839 		numbufs = 1;
840 		break;
841 	case XPT_MMC_IO:
842 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
843 			return(0);
844 		/* Two mappings: one for cmd->data and one for cmd->data->data */
845 		data_ptrs[0] = (unsigned char **)&ccb->mmcio.cmd.data;
846 		lengths[0] = sizeof(struct mmc_data *);
847 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
848 		data_ptrs[1] = (unsigned char **)&ccb->mmcio.cmd.data->data;
849 		lengths[1] = ccb->mmcio.cmd.data->len;
850 		dirs[1] = ccb->ccb_h.flags & CAM_DIR_MASK;
851 		numbufs = 2;
852 		break;
853 	case XPT_SMP_IO:
854 		data_ptrs[0] = &ccb->smpio.smp_request;
855 		lengths[0] = ccb->smpio.smp_request_len;
856 		dirs[0] = CAM_DIR_OUT;
857 		data_ptrs[1] = &ccb->smpio.smp_response;
858 		lengths[1] = ccb->smpio.smp_response_len;
859 		dirs[1] = CAM_DIR_IN;
860 		numbufs = 2;
861 		break;
862 	case XPT_NVME_IO:
863 	case XPT_NVME_ADMIN:
864 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
865 			return (0);
866 		if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
867 			return (EINVAL);
868 		data_ptrs[0] = &ccb->nvmeio.data_ptr;
869 		lengths[0] = ccb->nvmeio.dxfer_len;
870 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
871 		numbufs = 1;
872 		break;
873 	case XPT_DEV_ADVINFO:
874 		if (ccb->cdai.bufsiz == 0)
875 			return (0);
876 
877 		data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
878 		lengths[0] = ccb->cdai.bufsiz;
879 		dirs[0] = CAM_DIR_IN;
880 		numbufs = 1;
881 
882 		/*
883 		 * This request will not go to the hardware, no reason
884 		 * to be so strict. vmapbuf() is able to map up to maxphys.
885 		 */
886 		maxmap = maxphys;
887 		break;
888 	default:
889 		return(EINVAL);
890 		break; /* NOTREACHED */
891 	}
892 
893 	/*
894 	 * Check the transfer length and permissions first, so we don't
895 	 * have to unmap any previously mapped buffers.
896 	 */
897 	for (i = 0; i < numbufs; i++) {
898 		if (lengths[i] > maxmap) {
899 			printf("cam_periph_mapmem: attempt to map %lu bytes, "
900 			       "which is greater than %lu\n",
901 			       (long)(lengths[i]), (u_long)maxmap);
902 			return (E2BIG);
903 		}
904 	}
905 
906 	/*
907 	 * This keeps the kernel stack of current thread from getting
908 	 * swapped.  In low-memory situations where the kernel stack might
909 	 * otherwise get swapped out, this holds it and allows the thread
910 	 * to make progress and release the kernel mapped pages sooner.
911 	 *
912 	 * XXX KDM should I use P_NOSWAP instead?
913 	 */
914 	PHOLD(curproc);
915 
916 	for (i = 0; i < numbufs; i++) {
917 		/* Save the user's data address. */
918 		mapinfo->orig[i] = *data_ptrs[i];
919 
920 		/*
921 		 * For small buffers use malloc+copyin/copyout instead of
922 		 * mapping to KVA to avoid expensive TLB shootdowns.  For
923 		 * small allocations malloc is backed by UMA, and so much
924 		 * cheaper on SMP systems.
925 		 */
926 		if (lengths[i] <= periph_mapmem_thresh &&
927 		    ccb->ccb_h.func_code != XPT_MMC_IO) {
928 			*data_ptrs[i] = malloc(lengths[i], M_CAMPERIPH,
929 			    M_WAITOK);
930 			if (dirs[i] != CAM_DIR_IN) {
931 				if (copyin(mapinfo->orig[i], *data_ptrs[i],
932 				    lengths[i]) != 0) {
933 					free(*data_ptrs[i], M_CAMPERIPH);
934 					*data_ptrs[i] = mapinfo->orig[i];
935 					goto fail;
936 				}
937 			} else
938 				bzero(*data_ptrs[i], lengths[i]);
939 			continue;
940 		}
941 
942 		/*
943 		 * Get the buffer.
944 		 */
945 		mapinfo->bp[i] = uma_zalloc(pbuf_zone, M_WAITOK);
946 
947 		/* set the direction */
948 		mapinfo->bp[i]->b_iocmd = (dirs[i] == CAM_DIR_OUT) ?
949 		    BIO_WRITE : BIO_READ;
950 
951 		/* Map the buffer into kernel memory. */
952 		if (vmapbuf(mapinfo->bp[i], *data_ptrs[i], lengths[i], 1) < 0) {
953 			uma_zfree(pbuf_zone, mapinfo->bp[i]);
954 			goto fail;
955 		}
956 
957 		/* set our pointer to the new mapped area */
958 		*data_ptrs[i] = mapinfo->bp[i]->b_data;
959 	}
960 
961 	/*
962 	 * Now that we've gotten this far, change ownership to the kernel
963 	 * of the buffers so that we don't run afoul of returning to user
964 	 * space with locks (on the buffer) held.
965 	 */
966 	for (i = 0; i < numbufs; i++) {
967 		if (mapinfo->bp[i])
968 			BUF_KERNPROC(mapinfo->bp[i]);
969 	}
970 
971 	mapinfo->num_bufs_used = numbufs;
972 	return(0);
973 
974 fail:
975 	for (i--; i >= 0; i--) {
976 		if (mapinfo->bp[i]) {
977 			vunmapbuf(mapinfo->bp[i]);
978 			uma_zfree(pbuf_zone, mapinfo->bp[i]);
979 		} else
980 			free(*data_ptrs[i], M_CAMPERIPH);
981 		*data_ptrs[i] = mapinfo->orig[i];
982 	}
983 	PRELE(curproc);
984 	return(EACCES);
985 }
986 
987 /*
988  * Unmap memory segments mapped into kernel virtual address space by
989  * cam_periph_mapmem().
990  */
991 void
992 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
993 {
994 	int numbufs, i;
995 	u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
996 	u_int32_t lengths[CAM_PERIPH_MAXMAPS];
997 	u_int32_t dirs[CAM_PERIPH_MAXMAPS];
998 
999 	if (mapinfo->num_bufs_used <= 0) {
1000 		/* nothing to free and the process wasn't held. */
1001 		return;
1002 	}
1003 
1004 	switch (ccb->ccb_h.func_code) {
1005 	case XPT_DEV_MATCH:
1006 		if (ccb->cdm.pattern_buf_len > 0) {
1007 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
1008 			lengths[0] = ccb->cdm.pattern_buf_len;
1009 			dirs[0] = CAM_DIR_OUT;
1010 			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
1011 			lengths[1] = ccb->cdm.match_buf_len;
1012 			dirs[1] = CAM_DIR_IN;
1013 			numbufs = 2;
1014 		} else {
1015 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
1016 			lengths[0] = ccb->cdm.match_buf_len;
1017 			dirs[0] = CAM_DIR_IN;
1018 			numbufs = 1;
1019 		}
1020 		break;
1021 	case XPT_SCSI_IO:
1022 	case XPT_CONT_TARGET_IO:
1023 		data_ptrs[0] = &ccb->csio.data_ptr;
1024 		lengths[0] = ccb->csio.dxfer_len;
1025 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
1026 		numbufs = 1;
1027 		break;
1028 	case XPT_ATA_IO:
1029 		data_ptrs[0] = &ccb->ataio.data_ptr;
1030 		lengths[0] = ccb->ataio.dxfer_len;
1031 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
1032 		numbufs = 1;
1033 		break;
1034 	case XPT_MMC_IO:
1035 		data_ptrs[0] = (u_int8_t **)&ccb->mmcio.cmd.data;
1036 		lengths[0] = sizeof(struct mmc_data *);
1037 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
1038 		data_ptrs[1] = (u_int8_t **)&ccb->mmcio.cmd.data->data;
1039 		lengths[1] = ccb->mmcio.cmd.data->len;
1040 		dirs[1] = ccb->ccb_h.flags & CAM_DIR_MASK;
1041 		numbufs = 2;
1042 		break;
1043 	case XPT_SMP_IO:
1044 		data_ptrs[0] = &ccb->smpio.smp_request;
1045 		lengths[0] = ccb->smpio.smp_request_len;
1046 		dirs[0] = CAM_DIR_OUT;
1047 		data_ptrs[1] = &ccb->smpio.smp_response;
1048 		lengths[1] = ccb->smpio.smp_response_len;
1049 		dirs[1] = CAM_DIR_IN;
1050 		numbufs = 2;
1051 		break;
1052 	case XPT_NVME_IO:
1053 	case XPT_NVME_ADMIN:
1054 		data_ptrs[0] = &ccb->nvmeio.data_ptr;
1055 		lengths[0] = ccb->nvmeio.dxfer_len;
1056 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
1057 		numbufs = 1;
1058 		break;
1059 	case XPT_DEV_ADVINFO:
1060 		data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
1061 		lengths[0] = ccb->cdai.bufsiz;
1062 		dirs[0] = CAM_DIR_IN;
1063 		numbufs = 1;
1064 		break;
1065 	default:
1066 		/* allow ourselves to be swapped once again */
1067 		PRELE(curproc);
1068 		return;
1069 		break; /* NOTREACHED */
1070 	}
1071 
1072 	for (i = 0; i < numbufs; i++) {
1073 		if (mapinfo->bp[i]) {
1074 			/* unmap the buffer */
1075 			vunmapbuf(mapinfo->bp[i]);
1076 
1077 			/* release the buffer */
1078 			uma_zfree(pbuf_zone, mapinfo->bp[i]);
1079 		} else {
1080 			if (dirs[i] != CAM_DIR_OUT) {
1081 				copyout(*data_ptrs[i], mapinfo->orig[i],
1082 				    lengths[i]);
1083 			}
1084 			free(*data_ptrs[i], M_CAMPERIPH);
1085 		}
1086 
1087 		/* Set the user's pointer back to the original value */
1088 		*data_ptrs[i] = mapinfo->orig[i];
1089 	}
1090 
1091 	/* allow ourselves to be swapped once again */
1092 	PRELE(curproc);
1093 }
1094 
1095 int
1096 cam_periph_ioctl(struct cam_periph *periph, u_long cmd, caddr_t addr,
1097 		 int (*error_routine)(union ccb *ccb,
1098 				      cam_flags camflags,
1099 				      u_int32_t sense_flags))
1100 {
1101 	union ccb 	     *ccb;
1102 	int 		     error;
1103 	int		     found;
1104 
1105 	error = found = 0;
1106 
1107 	switch(cmd){
1108 	case CAMGETPASSTHRU:
1109 		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1110 		xpt_setup_ccb(&ccb->ccb_h,
1111 			      ccb->ccb_h.path,
1112 			      CAM_PRIORITY_NORMAL);
1113 		ccb->ccb_h.func_code = XPT_GDEVLIST;
1114 
1115 		/*
1116 		 * Basically, the point of this is that we go through
1117 		 * getting the list of devices, until we find a passthrough
1118 		 * device.  In the current version of the CAM code, the
1119 		 * only way to determine what type of device we're dealing
1120 		 * with is by its name.
1121 		 */
1122 		while (found == 0) {
1123 			ccb->cgdl.index = 0;
1124 			ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
1125 			while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
1126 				/* we want the next device in the list */
1127 				xpt_action(ccb);
1128 				if (strncmp(ccb->cgdl.periph_name,
1129 				    "pass", 4) == 0){
1130 					found = 1;
1131 					break;
1132 				}
1133 			}
1134 			if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
1135 			    (found == 0)) {
1136 				ccb->cgdl.periph_name[0] = '\0';
1137 				ccb->cgdl.unit_number = 0;
1138 				break;
1139 			}
1140 		}
1141 
1142 		/* copy the result back out */
1143 		bcopy(ccb, addr, sizeof(union ccb));
1144 
1145 		/* and release the ccb */
1146 		xpt_release_ccb(ccb);
1147 
1148 		break;
1149 	default:
1150 		error = ENOTTY;
1151 		break;
1152 	}
1153 	return(error);
1154 }
1155 
1156 static void
1157 cam_periph_done_panic(struct cam_periph *periph, union ccb *done_ccb)
1158 {
1159 
1160 	panic("%s: already done with ccb %p", __func__, done_ccb);
1161 }
1162 
1163 static void
1164 cam_periph_done(struct cam_periph *periph, union ccb *done_ccb)
1165 {
1166 
1167 	/* Caller will release the CCB */
1168 	xpt_path_assert(done_ccb->ccb_h.path, MA_OWNED);
1169 	done_ccb->ccb_h.cbfcnp = cam_periph_done_panic;
1170 	wakeup(&done_ccb->ccb_h.cbfcnp);
1171 }
1172 
1173 static void
1174 cam_periph_ccbwait(union ccb *ccb)
1175 {
1176 
1177 	if ((ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
1178 		while (ccb->ccb_h.cbfcnp != cam_periph_done_panic)
1179 			xpt_path_sleep(ccb->ccb_h.path, &ccb->ccb_h.cbfcnp,
1180 			    PRIBIO, "cbwait", 0);
1181 	}
1182 	KASSERT(ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX &&
1183 	    (ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG,
1184 	    ("%s: proceeding with incomplete ccb: ccb=%p, func_code=%#x, "
1185 	     "status=%#x, index=%d", __func__, ccb, ccb->ccb_h.func_code,
1186 	     ccb->ccb_h.status, ccb->ccb_h.pinfo.index));
1187 }
1188 
1189 /*
1190  * Dispatch a CCB and wait for it to complete.  If the CCB has set a
1191  * callback function (ccb->ccb_h.cbfcnp), it will be overwritten and lost.
1192  */
1193 int
1194 cam_periph_runccb(union ccb *ccb,
1195 		  int (*error_routine)(union ccb *ccb,
1196 				       cam_flags camflags,
1197 				       u_int32_t sense_flags),
1198 		  cam_flags camflags, u_int32_t sense_flags,
1199 		  struct devstat *ds)
1200 {
1201 	struct bintime *starttime;
1202 	struct bintime ltime;
1203 	int error;
1204 	bool must_poll;
1205 	uint32_t timeout = 1;
1206 
1207 	starttime = NULL;
1208 	xpt_path_assert(ccb->ccb_h.path, MA_OWNED);
1209 	KASSERT((ccb->ccb_h.flags & CAM_UNLOCKED) == 0,
1210 	    ("%s: ccb=%p, func_code=%#x, flags=%#x", __func__, ccb,
1211 	     ccb->ccb_h.func_code, ccb->ccb_h.flags));
1212 
1213 	/*
1214 	 * If the user has supplied a stats structure, and if we understand
1215 	 * this particular type of ccb, record the transaction start.
1216 	 */
1217 	if (ds != NULL &&
1218 	    (ccb->ccb_h.func_code == XPT_SCSI_IO ||
1219 	    ccb->ccb_h.func_code == XPT_ATA_IO ||
1220 	    ccb->ccb_h.func_code == XPT_NVME_IO)) {
1221 		starttime = &ltime;
1222 		binuptime(starttime);
1223 		devstat_start_transaction(ds, starttime);
1224 	}
1225 
1226 	/*
1227 	 * We must poll the I/O while we're dumping. The scheduler is normally
1228 	 * stopped for dumping, except when we call doadump from ddb. While the
1229 	 * scheduler is running in this case, we still need to poll the I/O to
1230 	 * avoid sleeping waiting for the ccb to complete.
1231 	 *
1232 	 * A panic triggered dump stops the scheduler, any callback from the
1233 	 * shutdown_post_sync event will run with the scheduler stopped, but
1234 	 * before we're officially dumping. To avoid hanging in adashutdown
1235 	 * initiated commands (or other similar situations), we have to test for
1236 	 * either SCHEDULER_STOPPED() here as well.
1237 	 *
1238 	 * To avoid locking problems, dumping/polling callers must call
1239 	 * without a periph lock held.
1240 	 */
1241 	must_poll = dumping || SCHEDULER_STOPPED();
1242 	ccb->ccb_h.cbfcnp = cam_periph_done;
1243 
1244 	/*
1245 	 * If we're polling, then we need to ensure that we have ample resources
1246 	 * in the periph.  cam_periph_error can reschedule the ccb by calling
1247 	 * xpt_action and returning ERESTART, so we have to effect the polling
1248 	 * in the do loop below.
1249 	 */
1250 	if (must_poll) {
1251 		if (cam_sim_pollable(ccb->ccb_h.path->bus->sim))
1252 			timeout = xpt_poll_setup(ccb);
1253 		else
1254 			timeout = 0;
1255 	}
1256 
1257 	if (timeout == 0) {
1258 		ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1259 		error = EBUSY;
1260 	} else {
1261 		xpt_action(ccb);
1262 		do {
1263 			if (must_poll) {
1264 				xpt_pollwait(ccb, timeout);
1265 				timeout = ccb->ccb_h.timeout * 10;
1266 			} else {
1267 				cam_periph_ccbwait(ccb);
1268 			}
1269 			if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1270 				error = 0;
1271 			else if (error_routine != NULL) {
1272 				ccb->ccb_h.cbfcnp = cam_periph_done;
1273 				error = (*error_routine)(ccb, camflags, sense_flags);
1274 			} else
1275 				error = 0;
1276 		} while (error == ERESTART);
1277 	}
1278 
1279 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1280 		cam_release_devq(ccb->ccb_h.path,
1281 				 /* relsim_flags */0,
1282 				 /* openings */0,
1283 				 /* timeout */0,
1284 				 /* getcount_only */ FALSE);
1285 		ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1286 	}
1287 
1288 	if (ds != NULL) {
1289 		uint32_t bytes;
1290 		devstat_tag_type tag;
1291 		bool valid = true;
1292 
1293 		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1294 			bytes = ccb->csio.dxfer_len - ccb->csio.resid;
1295 			tag = (devstat_tag_type)(ccb->csio.tag_action & 0x3);
1296 		} else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1297 			bytes = ccb->ataio.dxfer_len - ccb->ataio.resid;
1298 			tag = (devstat_tag_type)0;
1299 		} else if (ccb->ccb_h.func_code == XPT_NVME_IO) {
1300 			bytes = ccb->nvmeio.dxfer_len; /* NB: resid no possible */
1301 			tag = (devstat_tag_type)0;
1302 		} else {
1303 			valid = false;
1304 		}
1305 		if (valid)
1306 			devstat_end_transaction(ds, bytes, tag,
1307 			    ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) ?
1308 			    DEVSTAT_NO_DATA : (ccb->ccb_h.flags & CAM_DIR_OUT) ?
1309 			    DEVSTAT_WRITE : DEVSTAT_READ, NULL, starttime);
1310 	}
1311 
1312 	return(error);
1313 }
1314 
1315 void
1316 cam_freeze_devq(struct cam_path *path)
1317 {
1318 	struct ccb_hdr ccb_h;
1319 
1320 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_freeze_devq\n"));
1321 	xpt_setup_ccb(&ccb_h, path, /*priority*/1);
1322 	ccb_h.func_code = XPT_NOOP;
1323 	ccb_h.flags = CAM_DEV_QFREEZE;
1324 	xpt_action((union ccb *)&ccb_h);
1325 }
1326 
1327 u_int32_t
1328 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
1329 		 u_int32_t openings, u_int32_t arg,
1330 		 int getcount_only)
1331 {
1332 	struct ccb_relsim crs;
1333 
1334 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_release_devq(%u, %u, %u, %d)\n",
1335 	    relsim_flags, openings, arg, getcount_only));
1336 	xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
1337 	crs.ccb_h.func_code = XPT_REL_SIMQ;
1338 	crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
1339 	crs.release_flags = relsim_flags;
1340 	crs.openings = openings;
1341 	crs.release_timeout = arg;
1342 	xpt_action((union ccb *)&crs);
1343 	return (crs.qfrozen_cnt);
1344 }
1345 
1346 #define saved_ccb_ptr ppriv_ptr0
1347 static void
1348 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
1349 {
1350 	union ccb      *saved_ccb;
1351 	cam_status	status;
1352 	struct scsi_start_stop_unit *scsi_cmd;
1353 	int		error = 0, error_code, sense_key, asc, ascq;
1354 
1355 	scsi_cmd = (struct scsi_start_stop_unit *)
1356 	    &done_ccb->csio.cdb_io.cdb_bytes;
1357 	status = done_ccb->ccb_h.status;
1358 
1359 	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1360 		if (scsi_extract_sense_ccb(done_ccb,
1361 		    &error_code, &sense_key, &asc, &ascq)) {
1362 			/*
1363 			 * If the error is "invalid field in CDB",
1364 			 * and the load/eject flag is set, turn the
1365 			 * flag off and try again.  This is just in
1366 			 * case the drive in question barfs on the
1367 			 * load eject flag.  The CAM code should set
1368 			 * the load/eject flag by default for
1369 			 * removable media.
1370 			 */
1371 			if ((scsi_cmd->opcode == START_STOP_UNIT) &&
1372 			    ((scsi_cmd->how & SSS_LOEJ) != 0) &&
1373 			     (asc == 0x24) && (ascq == 0x00)) {
1374 				scsi_cmd->how &= ~SSS_LOEJ;
1375 				if (status & CAM_DEV_QFRZN) {
1376 					cam_release_devq(done_ccb->ccb_h.path,
1377 					    0, 0, 0, 0);
1378 					done_ccb->ccb_h.status &=
1379 					    ~CAM_DEV_QFRZN;
1380 				}
1381 				xpt_action(done_ccb);
1382 				goto out;
1383 			}
1384 		}
1385 		error = cam_periph_error(done_ccb, 0,
1386 		    SF_RETRY_UA | SF_NO_PRINT);
1387 		if (error == ERESTART)
1388 			goto out;
1389 		if (done_ccb->ccb_h.status & CAM_DEV_QFRZN) {
1390 			cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1391 			done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1392 		}
1393 	} else {
1394 		/*
1395 		 * If we have successfully taken a device from the not
1396 		 * ready to ready state, re-scan the device and re-get
1397 		 * the inquiry information.  Many devices (mostly disks)
1398 		 * don't properly report their inquiry information unless
1399 		 * they are spun up.
1400 		 */
1401 		if (scsi_cmd->opcode == START_STOP_UNIT)
1402 			xpt_async(AC_INQ_CHANGED, done_ccb->ccb_h.path, NULL);
1403 	}
1404 
1405 	/* If we tried long wait and still failed, remember that. */
1406 	if ((periph->flags & CAM_PERIPH_RECOVERY_WAIT) &&
1407 	    (done_ccb->csio.cdb_io.cdb_bytes[0] == TEST_UNIT_READY)) {
1408 		periph->flags &= ~CAM_PERIPH_RECOVERY_WAIT;
1409 		if (error != 0 && done_ccb->ccb_h.retry_count == 0)
1410 			periph->flags |= CAM_PERIPH_RECOVERY_WAIT_FAILED;
1411 	}
1412 
1413 	/*
1414 	 * After recovery action(s) completed, return to the original CCB.
1415 	 * If the recovery CCB has failed, considering its own possible
1416 	 * retries and recovery, assume we are back in state where we have
1417 	 * been originally, but without recovery hopes left.  In such case,
1418 	 * after the final attempt below, we cancel any further retries,
1419 	 * blocking by that also any new recovery attempts for this CCB,
1420 	 * and the result will be the final one returned to the CCB owher.
1421 	 */
1422 	saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
1423 	bcopy(saved_ccb, done_ccb, sizeof(*done_ccb));
1424 	xpt_free_ccb(saved_ccb);
1425 	if (done_ccb->ccb_h.cbfcnp != camperiphdone)
1426 		periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
1427 	if (error != 0)
1428 		done_ccb->ccb_h.retry_count = 0;
1429 	xpt_action(done_ccb);
1430 
1431 out:
1432 	/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1433 	cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1434 }
1435 
1436 /*
1437  * Generic Async Event handler.  Peripheral drivers usually
1438  * filter out the events that require personal attention,
1439  * and leave the rest to this function.
1440  */
1441 void
1442 cam_periph_async(struct cam_periph *periph, u_int32_t code,
1443 		 struct cam_path *path, void *arg)
1444 {
1445 	switch (code) {
1446 	case AC_LOST_DEVICE:
1447 		cam_periph_invalidate(periph);
1448 		break;
1449 	default:
1450 		break;
1451 	}
1452 }
1453 
1454 void
1455 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
1456 {
1457 	struct ccb_getdevstats cgds;
1458 
1459 	xpt_setup_ccb(&cgds.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1460 	cgds.ccb_h.func_code = XPT_GDEV_STATS;
1461 	xpt_action((union ccb *)&cgds);
1462 	cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
1463 }
1464 
1465 void
1466 cam_periph_freeze_after_event(struct cam_periph *periph,
1467 			      struct timeval* event_time, u_int duration_ms)
1468 {
1469 	struct timeval delta;
1470 	struct timeval duration_tv;
1471 
1472 	if (!timevalisset(event_time))
1473 		return;
1474 
1475 	microtime(&delta);
1476 	timevalsub(&delta, event_time);
1477 	duration_tv.tv_sec = duration_ms / 1000;
1478 	duration_tv.tv_usec = (duration_ms % 1000) * 1000;
1479 	if (timevalcmp(&delta, &duration_tv, <)) {
1480 		timevalsub(&duration_tv, &delta);
1481 
1482 		duration_ms = duration_tv.tv_sec * 1000;
1483 		duration_ms += duration_tv.tv_usec / 1000;
1484 		cam_freeze_devq(periph->path);
1485 		cam_release_devq(periph->path,
1486 				RELSIM_RELEASE_AFTER_TIMEOUT,
1487 				/*reduction*/0,
1488 				/*timeout*/duration_ms,
1489 				/*getcount_only*/0);
1490 	}
1491 
1492 }
1493 
1494 static int
1495 camperiphscsistatuserror(union ccb *ccb, union ccb **orig_ccb,
1496     cam_flags camflags, u_int32_t sense_flags,
1497     int *openings, u_int32_t *relsim_flags,
1498     u_int32_t *timeout, u_int32_t *action, const char **action_string)
1499 {
1500 	struct cam_periph *periph;
1501 	int error;
1502 
1503 	switch (ccb->csio.scsi_status) {
1504 	case SCSI_STATUS_OK:
1505 	case SCSI_STATUS_COND_MET:
1506 	case SCSI_STATUS_INTERMED:
1507 	case SCSI_STATUS_INTERMED_COND_MET:
1508 		error = 0;
1509 		break;
1510 	case SCSI_STATUS_CMD_TERMINATED:
1511 	case SCSI_STATUS_CHECK_COND:
1512 		error = camperiphscsisenseerror(ccb, orig_ccb,
1513 					        camflags,
1514 					        sense_flags,
1515 					        openings,
1516 					        relsim_flags,
1517 					        timeout,
1518 					        action,
1519 					        action_string);
1520 		break;
1521 	case SCSI_STATUS_QUEUE_FULL:
1522 	{
1523 		/* no decrement */
1524 		struct ccb_getdevstats cgds;
1525 
1526 		/*
1527 		 * First off, find out what the current
1528 		 * transaction counts are.
1529 		 */
1530 		xpt_setup_ccb(&cgds.ccb_h,
1531 			      ccb->ccb_h.path,
1532 			      CAM_PRIORITY_NORMAL);
1533 		cgds.ccb_h.func_code = XPT_GDEV_STATS;
1534 		xpt_action((union ccb *)&cgds);
1535 
1536 		/*
1537 		 * If we were the only transaction active, treat
1538 		 * the QUEUE FULL as if it were a BUSY condition.
1539 		 */
1540 		if (cgds.dev_active != 0) {
1541 			int total_openings;
1542 
1543 			/*
1544 		 	 * Reduce the number of openings to
1545 			 * be 1 less than the amount it took
1546 			 * to get a queue full bounded by the
1547 			 * minimum allowed tag count for this
1548 			 * device.
1549 		 	 */
1550 			total_openings = cgds.dev_active + cgds.dev_openings;
1551 			*openings = cgds.dev_active;
1552 			if (*openings < cgds.mintags)
1553 				*openings = cgds.mintags;
1554 			if (*openings < total_openings)
1555 				*relsim_flags = RELSIM_ADJUST_OPENINGS;
1556 			else {
1557 				/*
1558 				 * Some devices report queue full for
1559 				 * temporary resource shortages.  For
1560 				 * this reason, we allow a minimum
1561 				 * tag count to be entered via a
1562 				 * quirk entry to prevent the queue
1563 				 * count on these devices from falling
1564 				 * to a pessimisticly low value.  We
1565 				 * still wait for the next successful
1566 				 * completion, however, before queueing
1567 				 * more transactions to the device.
1568 				 */
1569 				*relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
1570 			}
1571 			*timeout = 0;
1572 			error = ERESTART;
1573 			*action &= ~SSQ_PRINT_SENSE;
1574 			break;
1575 		}
1576 		/* FALLTHROUGH */
1577 	}
1578 	case SCSI_STATUS_BUSY:
1579 		/*
1580 		 * Restart the queue after either another
1581 		 * command completes or a 1 second timeout.
1582 		 */
1583 		periph = xpt_path_periph(ccb->ccb_h.path);
1584 		if (periph->flags & CAM_PERIPH_INVALID) {
1585 			error = EIO;
1586 			*action_string = "Periph was invalidated";
1587 		} else if ((sense_flags & SF_RETRY_BUSY) != 0 ||
1588 		    ccb->ccb_h.retry_count > 0) {
1589 			if ((sense_flags & SF_RETRY_BUSY) == 0)
1590 				ccb->ccb_h.retry_count--;
1591 			error = ERESTART;
1592 			*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
1593 				      | RELSIM_RELEASE_AFTER_CMDCMPLT;
1594 			*timeout = 1000;
1595 		} else {
1596 			error = EIO;
1597 			*action_string = "Retries exhausted";
1598 		}
1599 		break;
1600 	case SCSI_STATUS_RESERV_CONFLICT:
1601 	default:
1602 		error = EIO;
1603 		break;
1604 	}
1605 	return (error);
1606 }
1607 
1608 static int
1609 camperiphscsisenseerror(union ccb *ccb, union ccb **orig,
1610     cam_flags camflags, u_int32_t sense_flags,
1611     int *openings, u_int32_t *relsim_flags,
1612     u_int32_t *timeout, u_int32_t *action, const char **action_string)
1613 {
1614 	struct cam_periph *periph;
1615 	union ccb *orig_ccb = ccb;
1616 	int error, recoveryccb;
1617 
1618 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
1619 	if (ccb->ccb_h.func_code == XPT_SCSI_IO && ccb->csio.bio != NULL)
1620 		biotrack(ccb->csio.bio, __func__);
1621 #endif
1622 
1623 	periph = xpt_path_periph(ccb->ccb_h.path);
1624 	recoveryccb = (ccb->ccb_h.cbfcnp == camperiphdone);
1625 	if ((periph->flags & CAM_PERIPH_RECOVERY_INPROG) && !recoveryccb) {
1626 		/*
1627 		 * If error recovery is already in progress, don't attempt
1628 		 * to process this error, but requeue it unconditionally
1629 		 * and attempt to process it once error recovery has
1630 		 * completed.  This failed command is probably related to
1631 		 * the error that caused the currently active error recovery
1632 		 * action so our  current recovery efforts should also
1633 		 * address this command.  Be aware that the error recovery
1634 		 * code assumes that only one recovery action is in progress
1635 		 * on a particular peripheral instance at any given time
1636 		 * (e.g. only one saved CCB for error recovery) so it is
1637 		 * imperitive that we don't violate this assumption.
1638 		 */
1639 		error = ERESTART;
1640 		*action &= ~SSQ_PRINT_SENSE;
1641 	} else {
1642 		scsi_sense_action err_action;
1643 		struct ccb_getdev cgd;
1644 
1645 		/*
1646 		 * Grab the inquiry data for this device.
1647 		 */
1648 		xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, CAM_PRIORITY_NORMAL);
1649 		cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1650 		xpt_action((union ccb *)&cgd);
1651 
1652 		err_action = scsi_error_action(&ccb->csio, &cgd.inq_data,
1653 		    sense_flags);
1654 		error = err_action & SS_ERRMASK;
1655 
1656 		/*
1657 		 * Do not autostart sequential access devices
1658 		 * to avoid unexpected tape loading.
1659 		 */
1660 		if ((err_action & SS_MASK) == SS_START &&
1661 		    SID_TYPE(&cgd.inq_data) == T_SEQUENTIAL) {
1662 			*action_string = "Will not autostart a "
1663 			    "sequential access device";
1664 			goto sense_error_done;
1665 		}
1666 
1667 		/*
1668 		 * Avoid recovery recursion if recovery action is the same.
1669 		 */
1670 		if ((err_action & SS_MASK) >= SS_START && recoveryccb) {
1671 			if (((err_action & SS_MASK) == SS_START &&
1672 			     ccb->csio.cdb_io.cdb_bytes[0] == START_STOP_UNIT) ||
1673 			    ((err_action & SS_MASK) == SS_TUR &&
1674 			     (ccb->csio.cdb_io.cdb_bytes[0] == TEST_UNIT_READY))) {
1675 				err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
1676 				*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1677 				*timeout = 500;
1678 			}
1679 		}
1680 
1681 		/*
1682 		 * If the recovery action will consume a retry,
1683 		 * make sure we actually have retries available.
1684 		 */
1685 		if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
1686 		 	if (ccb->ccb_h.retry_count > 0 &&
1687 			    (periph->flags & CAM_PERIPH_INVALID) == 0)
1688 		 		ccb->ccb_h.retry_count--;
1689 			else {
1690 				*action_string = "Retries exhausted";
1691 				goto sense_error_done;
1692 			}
1693 		}
1694 
1695 		if ((err_action & SS_MASK) >= SS_START) {
1696 			/*
1697 			 * Do common portions of commands that
1698 			 * use recovery CCBs.
1699 			 */
1700 			orig_ccb = xpt_alloc_ccb_nowait();
1701 			if (orig_ccb == NULL) {
1702 				*action_string = "Can't allocate recovery CCB";
1703 				goto sense_error_done;
1704 			}
1705 			/*
1706 			 * Clear freeze flag for original request here, as
1707 			 * this freeze will be dropped as part of ERESTART.
1708 			 */
1709 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1710 			bcopy(ccb, orig_ccb, sizeof(*orig_ccb));
1711 		}
1712 
1713 		switch (err_action & SS_MASK) {
1714 		case SS_NOP:
1715 			*action_string = "No recovery action needed";
1716 			error = 0;
1717 			break;
1718 		case SS_RETRY:
1719 			*action_string = "Retrying command (per sense data)";
1720 			error = ERESTART;
1721 			break;
1722 		case SS_FAIL:
1723 			*action_string = "Unretryable error";
1724 			break;
1725 		case SS_START:
1726 		{
1727 			int le;
1728 
1729 			/*
1730 			 * Send a start unit command to the device, and
1731 			 * then retry the command.
1732 			 */
1733 			*action_string = "Attempting to start unit";
1734 			periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1735 
1736 			/*
1737 			 * Check for removable media and set
1738 			 * load/eject flag appropriately.
1739 			 */
1740 			if (SID_IS_REMOVABLE(&cgd.inq_data))
1741 				le = TRUE;
1742 			else
1743 				le = FALSE;
1744 
1745 			scsi_start_stop(&ccb->csio,
1746 					/*retries*/1,
1747 					camperiphdone,
1748 					MSG_SIMPLE_Q_TAG,
1749 					/*start*/TRUE,
1750 					/*load/eject*/le,
1751 					/*immediate*/FALSE,
1752 					SSD_FULL_SIZE,
1753 					/*timeout*/50000);
1754 			break;
1755 		}
1756 		case SS_TUR:
1757 		{
1758 			/*
1759 			 * Send a Test Unit Ready to the device.
1760 			 * If the 'many' flag is set, we send 120
1761 			 * test unit ready commands, one every half
1762 			 * second.  Otherwise, we just send one TUR.
1763 			 * We only want to do this if the retry
1764 			 * count has not been exhausted.
1765 			 */
1766 			int retries;
1767 
1768 			if ((err_action & SSQ_MANY) != 0 && (periph->flags &
1769 			     CAM_PERIPH_RECOVERY_WAIT_FAILED) == 0) {
1770 				periph->flags |= CAM_PERIPH_RECOVERY_WAIT;
1771 				*action_string = "Polling device for readiness";
1772 				retries = 120;
1773 			} else {
1774 				*action_string = "Testing device for readiness";
1775 				retries = 1;
1776 			}
1777 			periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
1778 			scsi_test_unit_ready(&ccb->csio,
1779 					     retries,
1780 					     camperiphdone,
1781 					     MSG_SIMPLE_Q_TAG,
1782 					     SSD_FULL_SIZE,
1783 					     /*timeout*/5000);
1784 
1785 			/*
1786 			 * Accomplish our 500ms delay by deferring
1787 			 * the release of our device queue appropriately.
1788 			 */
1789 			*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1790 			*timeout = 500;
1791 			break;
1792 		}
1793 		default:
1794 			panic("Unhandled error action %x", err_action);
1795 		}
1796 
1797 		if ((err_action & SS_MASK) >= SS_START) {
1798 			/*
1799 			 * Drop the priority, so that the recovery
1800 			 * CCB is the first to execute.  Freeze the queue
1801 			 * after this command is sent so that we can
1802 			 * restore the old csio and have it queued in
1803 			 * the proper order before we release normal
1804 			 * transactions to the device.
1805 			 */
1806 			ccb->ccb_h.pinfo.priority--;
1807 			ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1808 			ccb->ccb_h.saved_ccb_ptr = orig_ccb;
1809 			error = ERESTART;
1810 			*orig = orig_ccb;
1811 		}
1812 
1813 sense_error_done:
1814 		*action = err_action;
1815 	}
1816 	return (error);
1817 }
1818 
1819 /*
1820  * Generic error handler.  Peripheral drivers usually filter
1821  * out the errors that they handle in a unique manner, then
1822  * call this function.
1823  */
1824 int
1825 cam_periph_error(union ccb *ccb, cam_flags camflags,
1826 		 u_int32_t sense_flags)
1827 {
1828 	struct cam_path *newpath;
1829 	union ccb  *orig_ccb, *scan_ccb;
1830 	struct cam_periph *periph;
1831 	const char *action_string;
1832 	cam_status  status;
1833 	int	    frozen, error, openings, devctl_err;
1834 	u_int32_t   action, relsim_flags, timeout;
1835 
1836 	action = SSQ_PRINT_SENSE;
1837 	periph = xpt_path_periph(ccb->ccb_h.path);
1838 	action_string = NULL;
1839 	status = ccb->ccb_h.status;
1840 	frozen = (status & CAM_DEV_QFRZN) != 0;
1841 	status &= CAM_STATUS_MASK;
1842 	devctl_err = openings = relsim_flags = timeout = 0;
1843 	orig_ccb = ccb;
1844 
1845 	/* Filter the errors that should be reported via devctl */
1846 	switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
1847 	case CAM_CMD_TIMEOUT:
1848 	case CAM_REQ_ABORTED:
1849 	case CAM_REQ_CMP_ERR:
1850 	case CAM_REQ_TERMIO:
1851 	case CAM_UNREC_HBA_ERROR:
1852 	case CAM_DATA_RUN_ERR:
1853 	case CAM_SCSI_STATUS_ERROR:
1854 	case CAM_ATA_STATUS_ERROR:
1855 	case CAM_SMP_STATUS_ERROR:
1856 		devctl_err++;
1857 		break;
1858 	default:
1859 		break;
1860 	}
1861 
1862 	switch (status) {
1863 	case CAM_REQ_CMP:
1864 		error = 0;
1865 		action &= ~SSQ_PRINT_SENSE;
1866 		break;
1867 	case CAM_SCSI_STATUS_ERROR:
1868 		error = camperiphscsistatuserror(ccb, &orig_ccb,
1869 		    camflags, sense_flags, &openings, &relsim_flags,
1870 		    &timeout, &action, &action_string);
1871 		break;
1872 	case CAM_AUTOSENSE_FAIL:
1873 		error = EIO;	/* we have to kill the command */
1874 		break;
1875 	case CAM_UA_ABORT:
1876 	case CAM_UA_TERMIO:
1877 	case CAM_MSG_REJECT_REC:
1878 		/* XXX Don't know that these are correct */
1879 		error = EIO;
1880 		break;
1881 	case CAM_SEL_TIMEOUT:
1882 		if ((camflags & CAM_RETRY_SELTO) != 0) {
1883 			if (ccb->ccb_h.retry_count > 0 &&
1884 			    (periph->flags & CAM_PERIPH_INVALID) == 0) {
1885 				ccb->ccb_h.retry_count--;
1886 				error = ERESTART;
1887 
1888 				/*
1889 				 * Wait a bit to give the device
1890 				 * time to recover before we try again.
1891 				 */
1892 				relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1893 				timeout = periph_selto_delay;
1894 				break;
1895 			}
1896 			action_string = "Retries exhausted";
1897 		}
1898 		/* FALLTHROUGH */
1899 	case CAM_DEV_NOT_THERE:
1900 		error = ENXIO;
1901 		action = SSQ_LOST;
1902 		break;
1903 	case CAM_REQ_INVALID:
1904 	case CAM_PATH_INVALID:
1905 	case CAM_NO_HBA:
1906 	case CAM_PROVIDE_FAIL:
1907 	case CAM_REQ_TOO_BIG:
1908 	case CAM_LUN_INVALID:
1909 	case CAM_TID_INVALID:
1910 	case CAM_FUNC_NOTAVAIL:
1911 		error = EINVAL;
1912 		break;
1913 	case CAM_SCSI_BUS_RESET:
1914 	case CAM_BDR_SENT:
1915 		/*
1916 		 * Commands that repeatedly timeout and cause these
1917 		 * kinds of error recovery actions, should return
1918 		 * CAM_CMD_TIMEOUT, which allows us to safely assume
1919 		 * that this command was an innocent bystander to
1920 		 * these events and should be unconditionally
1921 		 * retried.
1922 		 */
1923 	case CAM_REQUEUE_REQ:
1924 		/* Unconditional requeue if device is still there */
1925 		if (periph->flags & CAM_PERIPH_INVALID) {
1926 			action_string = "Periph was invalidated";
1927 			error = EIO;
1928 		} else if (sense_flags & SF_NO_RETRY) {
1929 			error = EIO;
1930 			action_string = "Retry was blocked";
1931 		} else {
1932 			error = ERESTART;
1933 			action &= ~SSQ_PRINT_SENSE;
1934 		}
1935 		break;
1936 	case CAM_RESRC_UNAVAIL:
1937 		/* Wait a bit for the resource shortage to abate. */
1938 		timeout = periph_noresrc_delay;
1939 		/* FALLTHROUGH */
1940 	case CAM_BUSY:
1941 		if (timeout == 0) {
1942 			/* Wait a bit for the busy condition to abate. */
1943 			timeout = periph_busy_delay;
1944 		}
1945 		relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
1946 		/* FALLTHROUGH */
1947 	case CAM_ATA_STATUS_ERROR:
1948 	case CAM_REQ_CMP_ERR:
1949 	case CAM_CMD_TIMEOUT:
1950 	case CAM_UNEXP_BUSFREE:
1951 	case CAM_UNCOR_PARITY:
1952 	case CAM_DATA_RUN_ERR:
1953 	default:
1954 		if (periph->flags & CAM_PERIPH_INVALID) {
1955 			error = EIO;
1956 			action_string = "Periph was invalidated";
1957 		} else if (ccb->ccb_h.retry_count == 0) {
1958 			error = EIO;
1959 			action_string = "Retries exhausted";
1960 		} else if (sense_flags & SF_NO_RETRY) {
1961 			error = EIO;
1962 			action_string = "Retry was blocked";
1963 		} else {
1964 			ccb->ccb_h.retry_count--;
1965 			error = ERESTART;
1966 		}
1967 		break;
1968 	}
1969 
1970 	if ((sense_flags & SF_PRINT_ALWAYS) ||
1971 	    CAM_DEBUGGED(ccb->ccb_h.path, CAM_DEBUG_INFO))
1972 		action |= SSQ_PRINT_SENSE;
1973 	else if (sense_flags & SF_NO_PRINT)
1974 		action &= ~SSQ_PRINT_SENSE;
1975 	if ((action & SSQ_PRINT_SENSE) != 0)
1976 		cam_error_print(orig_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1977 	if (error != 0 && (action & SSQ_PRINT_SENSE) != 0) {
1978 		if (error != ERESTART) {
1979 			if (action_string == NULL)
1980 				action_string = "Unretryable error";
1981 			xpt_print(ccb->ccb_h.path, "Error %d, %s\n",
1982 			    error, action_string);
1983 		} else if (action_string != NULL)
1984 			xpt_print(ccb->ccb_h.path, "%s\n", action_string);
1985 		else {
1986 			xpt_print(ccb->ccb_h.path,
1987 			    "Retrying command, %d more tries remain\n",
1988 			    ccb->ccb_h.retry_count);
1989 		}
1990 	}
1991 
1992 	if (devctl_err && (error != 0 || (action & SSQ_PRINT_SENSE) != 0))
1993 		cam_periph_devctl_notify(orig_ccb);
1994 
1995 	if ((action & SSQ_LOST) != 0) {
1996 		lun_id_t lun_id;
1997 
1998 		/*
1999 		 * For a selection timeout, we consider all of the LUNs on
2000 		 * the target to be gone.  If the status is CAM_DEV_NOT_THERE,
2001 		 * then we only get rid of the device(s) specified by the
2002 		 * path in the original CCB.
2003 		 */
2004 		if (status == CAM_SEL_TIMEOUT)
2005 			lun_id = CAM_LUN_WILDCARD;
2006 		else
2007 			lun_id = xpt_path_lun_id(ccb->ccb_h.path);
2008 
2009 		/* Should we do more if we can't create the path?? */
2010 		if (xpt_create_path(&newpath, periph,
2011 				    xpt_path_path_id(ccb->ccb_h.path),
2012 				    xpt_path_target_id(ccb->ccb_h.path),
2013 				    lun_id) == CAM_REQ_CMP) {
2014 			/*
2015 			 * Let peripheral drivers know that this
2016 			 * device has gone away.
2017 			 */
2018 			xpt_async(AC_LOST_DEVICE, newpath, NULL);
2019 			xpt_free_path(newpath);
2020 		}
2021 	}
2022 
2023 	/* Broadcast UNIT ATTENTIONs to all periphs. */
2024 	if ((action & SSQ_UA) != 0)
2025 		xpt_async(AC_UNIT_ATTENTION, orig_ccb->ccb_h.path, orig_ccb);
2026 
2027 	/* Rescan target on "Reported LUNs data has changed" */
2028 	if ((action & SSQ_RESCAN) != 0) {
2029 		if (xpt_create_path(&newpath, NULL,
2030 				    xpt_path_path_id(ccb->ccb_h.path),
2031 				    xpt_path_target_id(ccb->ccb_h.path),
2032 				    CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
2033 			scan_ccb = xpt_alloc_ccb_nowait();
2034 			if (scan_ccb != NULL) {
2035 				scan_ccb->ccb_h.path = newpath;
2036 				scan_ccb->ccb_h.func_code = XPT_SCAN_TGT;
2037 				scan_ccb->crcn.flags = 0;
2038 				xpt_rescan(scan_ccb);
2039 			} else {
2040 				xpt_print(newpath,
2041 				    "Can't allocate CCB to rescan target\n");
2042 				xpt_free_path(newpath);
2043 			}
2044 		}
2045 	}
2046 
2047 	/* Attempt a retry */
2048 	if (error == ERESTART || error == 0) {
2049 		if (frozen != 0)
2050 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
2051 		if (error == ERESTART)
2052 			xpt_action(ccb);
2053 		if (frozen != 0)
2054 			cam_release_devq(ccb->ccb_h.path,
2055 					 relsim_flags,
2056 					 openings,
2057 					 timeout,
2058 					 /*getcount_only*/0);
2059 	}
2060 
2061 	return (error);
2062 }
2063 
2064 #define CAM_PERIPH_DEVD_MSG_SIZE	256
2065 
2066 static void
2067 cam_periph_devctl_notify(union ccb *ccb)
2068 {
2069 	struct cam_periph *periph;
2070 	struct ccb_getdev *cgd;
2071 	struct sbuf sb;
2072 	int serr, sk, asc, ascq;
2073 	char *sbmsg, *type;
2074 
2075 	sbmsg = malloc(CAM_PERIPH_DEVD_MSG_SIZE, M_CAMPERIPH, M_NOWAIT);
2076 	if (sbmsg == NULL)
2077 		return;
2078 
2079 	sbuf_new(&sb, sbmsg, CAM_PERIPH_DEVD_MSG_SIZE, SBUF_FIXEDLEN);
2080 
2081 	periph = xpt_path_periph(ccb->ccb_h.path);
2082 	sbuf_printf(&sb, "device=%s%d ", periph->periph_name,
2083 	    periph->unit_number);
2084 
2085 	sbuf_printf(&sb, "serial=\"");
2086 	if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) != NULL) {
2087 		xpt_setup_ccb(&cgd->ccb_h, ccb->ccb_h.path,
2088 		    CAM_PRIORITY_NORMAL);
2089 		cgd->ccb_h.func_code = XPT_GDEV_TYPE;
2090 		xpt_action((union ccb *)cgd);
2091 
2092 		if (cgd->ccb_h.status == CAM_REQ_CMP)
2093 			sbuf_bcat(&sb, cgd->serial_num, cgd->serial_num_len);
2094 		xpt_free_ccb((union ccb *)cgd);
2095 	}
2096 	sbuf_printf(&sb, "\" ");
2097 	sbuf_printf(&sb, "cam_status=\"0x%x\" ", ccb->ccb_h.status);
2098 
2099 	switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
2100 	case CAM_CMD_TIMEOUT:
2101 		sbuf_printf(&sb, "timeout=%d ", ccb->ccb_h.timeout);
2102 		type = "timeout";
2103 		break;
2104 	case CAM_SCSI_STATUS_ERROR:
2105 		sbuf_printf(&sb, "scsi_status=%d ", ccb->csio.scsi_status);
2106 		if (scsi_extract_sense_ccb(ccb, &serr, &sk, &asc, &ascq))
2107 			sbuf_printf(&sb, "scsi_sense=\"%02x %02x %02x %02x\" ",
2108 			    serr, sk, asc, ascq);
2109 		type = "error";
2110 		break;
2111 	case CAM_ATA_STATUS_ERROR:
2112 		sbuf_printf(&sb, "RES=\"");
2113 		ata_res_sbuf(&ccb->ataio.res, &sb);
2114 		sbuf_printf(&sb, "\" ");
2115 		type = "error";
2116 		break;
2117 	default:
2118 		type = "error";
2119 		break;
2120 	}
2121 
2122 	if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
2123 		sbuf_printf(&sb, "CDB=\"");
2124 		scsi_cdb_sbuf(scsiio_cdb_ptr(&ccb->csio), &sb);
2125 		sbuf_printf(&sb, "\" ");
2126 	} else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
2127 		sbuf_printf(&sb, "ACB=\"");
2128 		ata_cmd_sbuf(&ccb->ataio.cmd, &sb);
2129 		sbuf_printf(&sb, "\" ");
2130 	}
2131 
2132 	if (sbuf_finish(&sb) == 0)
2133 		devctl_notify("CAM", "periph", type, sbuf_data(&sb));
2134 	sbuf_delete(&sb);
2135 	free(sbmsg, M_CAMPERIPH);
2136 }
2137 
2138 /*
2139  * Sysctl to force an invalidation of the drive right now. Can be
2140  * called with CTLFLAG_MPSAFE since we take periph lock.
2141  */
2142 int
2143 cam_periph_invalidate_sysctl(SYSCTL_HANDLER_ARGS)
2144 {
2145 	struct cam_periph *periph;
2146 	int error, value;
2147 
2148 	periph = arg1;
2149 	value = 0;
2150 	error = sysctl_handle_int(oidp, &value, 0, req);
2151 	if (error != 0 || req->newptr == NULL || value != 1)
2152 		return (error);
2153 
2154 	cam_periph_lock(periph);
2155 	cam_periph_invalidate(periph);
2156 	cam_periph_unlock(periph);
2157 
2158 	return (0);
2159 }
2160