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