xref: /freebsd/sys/cam/cam_periph.c (revision 991b5d263036b9ba3b5b5412b4a5d6c33e9e476f)
1898b0535SWarner Losh /*-
28b8a9b1dSJustin T. Gibbs  * Common functions for CAM "type" (peripheral) drivers.
38b8a9b1dSJustin T. Gibbs  *
48b8a9b1dSJustin T. Gibbs  * Copyright (c) 1997, 1998 Justin T. Gibbs.
5501468a5SKenneth D. Merry  * Copyright (c) 1997, 1998, 1999, 2000 Kenneth D. Merry.
68b8a9b1dSJustin T. Gibbs  * All rights reserved.
78b8a9b1dSJustin T. Gibbs  *
88b8a9b1dSJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
98b8a9b1dSJustin T. Gibbs  * modification, are permitted provided that the following conditions
108b8a9b1dSJustin T. Gibbs  * are met:
118b8a9b1dSJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
128b8a9b1dSJustin T. Gibbs  *    notice, this list of conditions, and the following disclaimer,
138b8a9b1dSJustin T. Gibbs  *    without modification, immediately at the beginning of the file.
148b8a9b1dSJustin T. Gibbs  * 2. The name of the author may not be used to endorse or promote products
158b8a9b1dSJustin T. Gibbs  *    derived from this software without specific prior written permission.
168b8a9b1dSJustin T. Gibbs  *
178b8a9b1dSJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
188b8a9b1dSJustin T. Gibbs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
198b8a9b1dSJustin T. Gibbs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
208b8a9b1dSJustin T. Gibbs  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
218b8a9b1dSJustin T. Gibbs  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
228b8a9b1dSJustin T. Gibbs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
238b8a9b1dSJustin T. Gibbs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
248b8a9b1dSJustin T. Gibbs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
258b8a9b1dSJustin T. Gibbs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
268b8a9b1dSJustin T. Gibbs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
278b8a9b1dSJustin T. Gibbs  * SUCH DAMAGE.
288b8a9b1dSJustin T. Gibbs  */
298b8a9b1dSJustin T. Gibbs 
309c963d87SDavid E. O'Brien #include <sys/cdefs.h>
319c963d87SDavid E. O'Brien __FBSDID("$FreeBSD$");
329c963d87SDavid E. O'Brien 
338b8a9b1dSJustin T. Gibbs #include <sys/param.h>
348b8a9b1dSJustin T. Gibbs #include <sys/systm.h>
358b8a9b1dSJustin T. Gibbs #include <sys/types.h>
368b8a9b1dSJustin T. Gibbs #include <sys/malloc.h>
37362abc44STai-hwa Liang #include <sys/kernel.h>
389626b608SPoul-Henning Kamp #include <sys/bio.h>
39f34fa851SJohn Baldwin #include <sys/lock.h>
40f34fa851SJohn Baldwin #include <sys/mutex.h>
418b8a9b1dSJustin T. Gibbs #include <sys/buf.h>
428b8a9b1dSJustin T. Gibbs #include <sys/proc.h>
438b8a9b1dSJustin T. Gibbs #include <sys/devicestat.h>
4475f51904SPeter Wemm #include <sys/bus.h>
453501942bSJustin T. Gibbs #include <sys/sbuf.h>
468b8a9b1dSJustin T. Gibbs #include <vm/vm.h>
478b8a9b1dSJustin T. Gibbs #include <vm/vm_extern.h>
488b8a9b1dSJustin T. Gibbs 
498b8a9b1dSJustin T. Gibbs #include <cam/cam.h>
508b8a9b1dSJustin T. Gibbs #include <cam/cam_ccb.h>
5152c9ce25SScott Long #include <cam/cam_queue.h>
528b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt_periph.h>
538b8a9b1dSJustin T. Gibbs #include <cam/cam_periph.h>
548b8a9b1dSJustin T. Gibbs #include <cam/cam_debug.h>
552b83592fSScott Long #include <cam/cam_sim.h>
568b8a9b1dSJustin T. Gibbs 
578b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_all.h>
588b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_message.h>
598b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_pass.h>
608b8a9b1dSJustin T. Gibbs 
618b8a9b1dSJustin T. Gibbs static	u_int		camperiphnextunit(struct periph_driver *p_drv,
62501468a5SKenneth D. Merry 					  u_int newunit, int wired,
63501468a5SKenneth D. Merry 					  path_id_t pathid, target_id_t target,
64501468a5SKenneth D. Merry 					  lun_id_t lun);
658b8a9b1dSJustin T. Gibbs static	u_int		camperiphunit(struct periph_driver *p_drv,
66501468a5SKenneth D. Merry 				      path_id_t pathid, target_id_t target,
67501468a5SKenneth D. Merry 				      lun_id_t lun);
688b8a9b1dSJustin T. Gibbs static	void		camperiphdone(struct cam_periph *periph,
698b8a9b1dSJustin T. Gibbs 					union ccb *done_ccb);
708b8a9b1dSJustin T. Gibbs static  void		camperiphfree(struct cam_periph *periph);
713393f8daSKenneth D. Merry static int		camperiphscsistatuserror(union ccb *ccb,
720191d9b3SAlexander Motin 					        union ccb **orig_ccb,
733393f8daSKenneth D. Merry 						 cam_flags camflags,
743393f8daSKenneth D. Merry 						 u_int32_t sense_flags,
753393f8daSKenneth D. Merry 						 int *openings,
763393f8daSKenneth D. Merry 						 u_int32_t *relsim_flags,
7783c5d981SAlexander Motin 						 u_int32_t *timeout,
780181d54bSAlexander Motin 						 u_int32_t  *action,
7983c5d981SAlexander Motin 						 const char **action_string);
803393f8daSKenneth D. Merry static	int		camperiphscsisenseerror(union ccb *ccb,
810191d9b3SAlexander Motin 					        union ccb **orig_ccb,
823393f8daSKenneth D. Merry 					        cam_flags camflags,
833393f8daSKenneth D. Merry 					        u_int32_t sense_flags,
843393f8daSKenneth D. Merry 					        int *openings,
853393f8daSKenneth D. Merry 					        u_int32_t *relsim_flags,
8683c5d981SAlexander Motin 					        u_int32_t *timeout,
870181d54bSAlexander Motin 					        u_int32_t *action,
8883c5d981SAlexander Motin 					        const char **action_string);
89a30ecd42SScott Long static void		cam_periph_devctl_notify(union ccb *ccb);
908b8a9b1dSJustin T. Gibbs 
910b7c27b9SPeter Wemm static int nperiph_drivers;
9283c5d981SAlexander Motin static int initialized = 0;
930b7c27b9SPeter Wemm struct periph_driver **periph_drivers;
940b7c27b9SPeter Wemm 
95d745c852SEd Schouten static MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers");
96362abc44STai-hwa Liang 
9773cf209fSMatt Jacob static int periph_selto_delay = 1000;
9873cf209fSMatt Jacob TUNABLE_INT("kern.cam.periph_selto_delay", &periph_selto_delay);
9973cf209fSMatt Jacob static int periph_noresrc_delay = 500;
10073cf209fSMatt Jacob TUNABLE_INT("kern.cam.periph_noresrc_delay", &periph_noresrc_delay);
10173cf209fSMatt Jacob static int periph_busy_delay = 500;
10273cf209fSMatt Jacob TUNABLE_INT("kern.cam.periph_busy_delay", &periph_busy_delay);
10373cf209fSMatt Jacob 
10473cf209fSMatt Jacob 
1050b7c27b9SPeter Wemm void
1060b7c27b9SPeter Wemm periphdriver_register(void *data)
1070b7c27b9SPeter Wemm {
10883c5d981SAlexander Motin 	struct periph_driver *drv = (struct periph_driver *)data;
1090b7c27b9SPeter Wemm 	struct periph_driver **newdrivers, **old;
1100b7c27b9SPeter Wemm 	int ndrivers;
1110b7c27b9SPeter Wemm 
1125719711fSEdward Tomasz Napierala again:
1130b7c27b9SPeter Wemm 	ndrivers = nperiph_drivers + 2;
1140dd50e9bSScott Long 	newdrivers = malloc(sizeof(*newdrivers) * ndrivers, M_CAMPERIPH,
1150dd50e9bSScott Long 			    M_WAITOK);
1165719711fSEdward Tomasz Napierala 	xpt_lock_buses();
1175719711fSEdward Tomasz Napierala 	if (ndrivers != nperiph_drivers + 2) {
1185719711fSEdward Tomasz Napierala 		/*
1195719711fSEdward Tomasz Napierala 		 * Lost race against itself; go around.
1205719711fSEdward Tomasz Napierala 		 */
1215719711fSEdward Tomasz Napierala 		xpt_unlock_buses();
1225719711fSEdward Tomasz Napierala 		free(newdrivers, M_CAMPERIPH);
1235719711fSEdward Tomasz Napierala 		goto again;
1245719711fSEdward Tomasz Napierala 	}
1250b7c27b9SPeter Wemm 	if (periph_drivers)
1260b7c27b9SPeter Wemm 		bcopy(periph_drivers, newdrivers,
127623db360SKenneth D. Merry 		      sizeof(*newdrivers) * nperiph_drivers);
12883c5d981SAlexander Motin 	newdrivers[nperiph_drivers] = drv;
1290b7c27b9SPeter Wemm 	newdrivers[nperiph_drivers + 1] = NULL;
1300b7c27b9SPeter Wemm 	old = periph_drivers;
1310b7c27b9SPeter Wemm 	periph_drivers = newdrivers;
1325719711fSEdward Tomasz Napierala 	nperiph_drivers++;
1335719711fSEdward Tomasz Napierala 	xpt_unlock_buses();
1340b7c27b9SPeter Wemm 	if (old)
1350dd50e9bSScott Long 		free(old, M_CAMPERIPH);
13683c5d981SAlexander Motin 	/* If driver marked as early or it is late now, initialize it. */
13783c5d981SAlexander Motin 	if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
13883c5d981SAlexander Motin 	    initialized > 1)
13983c5d981SAlexander Motin 		(*drv->init)();
14083c5d981SAlexander Motin }
14183c5d981SAlexander Motin 
14283c5d981SAlexander Motin void
14383c5d981SAlexander Motin periphdriver_init(int level)
14483c5d981SAlexander Motin {
14583c5d981SAlexander Motin 	int	i, early;
14683c5d981SAlexander Motin 
14783c5d981SAlexander Motin 	initialized = max(initialized, level);
14883c5d981SAlexander Motin 	for (i = 0; periph_drivers[i] != NULL; i++) {
14983c5d981SAlexander Motin 		early = (periph_drivers[i]->flags & CAM_PERIPH_DRV_EARLY) ? 1 : 2;
15083c5d981SAlexander Motin 		if (early == initialized)
15183c5d981SAlexander Motin 			(*periph_drivers[i]->init)();
15283c5d981SAlexander Motin 	}
1530b7c27b9SPeter Wemm }
1540b7c27b9SPeter Wemm 
1558b8a9b1dSJustin T. Gibbs cam_status
156ee9c90c7SKenneth D. Merry cam_periph_alloc(periph_ctor_t *periph_ctor,
157ee9c90c7SKenneth D. Merry 		 periph_oninv_t *periph_oninvalidate,
158ee9c90c7SKenneth D. Merry 		 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
159ee9c90c7SKenneth D. Merry 		 char *name, cam_periph_type type, struct cam_path *path,
160ee9c90c7SKenneth D. Merry 		 ac_callback_t *ac_callback, ac_code code, void *arg)
1618b8a9b1dSJustin T. Gibbs {
1628b8a9b1dSJustin T. Gibbs 	struct		periph_driver **p_drv;
1632b83592fSScott Long 	struct		cam_sim *sim;
1648b8a9b1dSJustin T. Gibbs 	struct		cam_periph *periph;
1658b8a9b1dSJustin T. Gibbs 	struct		cam_periph *cur_periph;
1668b8a9b1dSJustin T. Gibbs 	path_id_t	path_id;
1678b8a9b1dSJustin T. Gibbs 	target_id_t	target_id;
1688b8a9b1dSJustin T. Gibbs 	lun_id_t	lun_id;
1698b8a9b1dSJustin T. Gibbs 	cam_status	status;
1708b8a9b1dSJustin T. Gibbs 	u_int		init_level;
1718b8a9b1dSJustin T. Gibbs 
1728b8a9b1dSJustin T. Gibbs 	init_level = 0;
1738b8a9b1dSJustin T. Gibbs 	/*
1748b8a9b1dSJustin T. Gibbs 	 * Handle Hot-Plug scenarios.  If there is already a peripheral
1758b8a9b1dSJustin T. Gibbs 	 * of our type assigned to this path, we are likely waiting for
1768b8a9b1dSJustin T. Gibbs 	 * final close on an old, invalidated, peripheral.  If this is
1778b8a9b1dSJustin T. Gibbs 	 * the case, queue up a deferred call to the peripheral's async
17874c91ec5SJustin T. Gibbs 	 * handler.  If it looks like a mistaken re-allocation, complain.
1798b8a9b1dSJustin T. Gibbs 	 */
1808b8a9b1dSJustin T. Gibbs 	if ((periph = cam_periph_find(path, name)) != NULL) {
1818b8a9b1dSJustin T. Gibbs 
1828b8a9b1dSJustin T. Gibbs 		if ((periph->flags & CAM_PERIPH_INVALID) != 0
1838b8a9b1dSJustin T. Gibbs 		 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
1848b8a9b1dSJustin T. Gibbs 			periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
1858b8a9b1dSJustin T. Gibbs 			periph->deferred_callback = ac_callback;
1868b8a9b1dSJustin T. Gibbs 			periph->deferred_ac = code;
1878b8a9b1dSJustin T. Gibbs 			return (CAM_REQ_INPROG);
1888b8a9b1dSJustin T. Gibbs 		} else {
1898b8a9b1dSJustin T. Gibbs 			printf("cam_periph_alloc: attempt to re-allocate "
1908900f4b8SKenneth D. Merry 			       "valid device %s%d rejected flags %#x "
1918900f4b8SKenneth D. Merry 			       "refcount %d\n", periph->periph_name,
1928900f4b8SKenneth D. Merry 			       periph->unit_number, periph->flags,
1938900f4b8SKenneth D. Merry 			       periph->refcount);
1948b8a9b1dSJustin T. Gibbs 		}
1958b8a9b1dSJustin T. Gibbs 		return (CAM_REQ_INVALID);
1968b8a9b1dSJustin T. Gibbs 	}
1978b8a9b1dSJustin T. Gibbs 
198362abc44STai-hwa Liang 	periph = (struct cam_periph *)malloc(sizeof(*periph), M_CAMPERIPH,
1998900f4b8SKenneth D. Merry 					     M_NOWAIT|M_ZERO);
2008b8a9b1dSJustin T. Gibbs 
2018b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
2028b8a9b1dSJustin T. Gibbs 		return (CAM_RESRC_UNAVAIL);
2038b8a9b1dSJustin T. Gibbs 
2048b8a9b1dSJustin T. Gibbs 	init_level++;
2058b8a9b1dSJustin T. Gibbs 
2068b8a9b1dSJustin T. Gibbs 
2072b83592fSScott Long 	sim = xpt_path_sim(path);
2088b8a9b1dSJustin T. Gibbs 	path_id = xpt_path_path_id(path);
2098b8a9b1dSJustin T. Gibbs 	target_id = xpt_path_target_id(path);
2108b8a9b1dSJustin T. Gibbs 	lun_id = xpt_path_lun_id(path);
2118b8a9b1dSJustin T. Gibbs 	periph->periph_start = periph_start;
2128b8a9b1dSJustin T. Gibbs 	periph->periph_dtor = periph_dtor;
213ee9c90c7SKenneth D. Merry 	periph->periph_oninval = periph_oninvalidate;
2148b8a9b1dSJustin T. Gibbs 	periph->type = type;
2158b8a9b1dSJustin T. Gibbs 	periph->periph_name = name;
216227d67aaSAlexander Motin 	periph->scheduled_priority = CAM_PRIORITY_NONE;
2178b8a9b1dSJustin T. Gibbs 	periph->immediate_priority = CAM_PRIORITY_NONE;
218d38c0e53SAlexander Motin 	periph->refcount = 1;		/* Dropped by invalidation. */
2192b83592fSScott Long 	periph->sim = sim;
2208b8a9b1dSJustin T. Gibbs 	SLIST_INIT(&periph->ccb_list);
2218b8a9b1dSJustin T. Gibbs 	status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
2228b8a9b1dSJustin T. Gibbs 	if (status != CAM_REQ_CMP)
2238b8a9b1dSJustin T. Gibbs 		goto failure;
2248b8a9b1dSJustin T. Gibbs 	periph->path = path;
2258b8a9b1dSJustin T. Gibbs 
226f1e2546aSMatt Jacob 	xpt_lock_buses();
227f1e2546aSMatt Jacob 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
228f1e2546aSMatt Jacob 		if (strcmp((*p_drv)->driver_name, name) == 0)
229f1e2546aSMatt Jacob 			break;
230f1e2546aSMatt Jacob 	}
231f1e2546aSMatt Jacob 	if (*p_drv == NULL) {
232f1e2546aSMatt Jacob 		printf("cam_periph_alloc: invalid periph name '%s'\n", name);
2339e0d30e2SEdward Tomasz Napierala 		xpt_unlock_buses();
234f1e2546aSMatt Jacob 		xpt_free_path(periph->path);
235f1e2546aSMatt Jacob 		free(periph, M_CAMPERIPH);
236f1e2546aSMatt Jacob 		return (CAM_REQ_INVALID);
237f1e2546aSMatt Jacob 	}
238f1e2546aSMatt Jacob 	periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
2398b8a9b1dSJustin T. Gibbs 	cur_periph = TAILQ_FIRST(&(*p_drv)->units);
2408b8a9b1dSJustin T. Gibbs 	while (cur_periph != NULL
2418b8a9b1dSJustin T. Gibbs 	    && cur_periph->unit_number < periph->unit_number)
2428b8a9b1dSJustin T. Gibbs 		cur_periph = TAILQ_NEXT(cur_periph, unit_links);
243f1e2546aSMatt Jacob 	if (cur_periph != NULL) {
244f1e2546aSMatt Jacob 		KASSERT(cur_periph->unit_number != periph->unit_number, ("duplicate units on periph list"));
2458b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
246f1e2546aSMatt Jacob 	} else {
2478b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
2488b8a9b1dSJustin T. Gibbs 		(*p_drv)->generation++;
2498b8a9b1dSJustin T. Gibbs 	}
250f1e2546aSMatt Jacob 	xpt_unlock_buses();
251f1e2546aSMatt Jacob 
252f1e2546aSMatt Jacob 	init_level++;
253f1e2546aSMatt Jacob 
254f1e2546aSMatt Jacob 	status = xpt_add_periph(periph);
255f1e2546aSMatt Jacob 	if (status != CAM_REQ_CMP)
256f1e2546aSMatt Jacob 		goto failure;
2578b8a9b1dSJustin T. Gibbs 
2588b8a9b1dSJustin T. Gibbs 	init_level++;
25922c7d606SAlexander Motin 	CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph created\n"));
2608b8a9b1dSJustin T. Gibbs 
2618b8a9b1dSJustin T. Gibbs 	status = periph_ctor(periph, arg);
2628b8a9b1dSJustin T. Gibbs 
2638b8a9b1dSJustin T. Gibbs 	if (status == CAM_REQ_CMP)
2648b8a9b1dSJustin T. Gibbs 		init_level++;
2658b8a9b1dSJustin T. Gibbs 
2668b8a9b1dSJustin T. Gibbs failure:
2678b8a9b1dSJustin T. Gibbs 	switch (init_level) {
2688b8a9b1dSJustin T. Gibbs 	case 4:
2698b8a9b1dSJustin T. Gibbs 		/* Initialized successfully */
2708b8a9b1dSJustin T. Gibbs 		break;
2718b8a9b1dSJustin T. Gibbs 	case 3:
27222c7d606SAlexander Motin 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
273a29779e8SAlexander Motin 		xpt_remove_periph(periph);
2747379c88fSPoul-Henning Kamp 		/* FALLTHROUGH */
2758b8a9b1dSJustin T. Gibbs 	case 2:
276f1e2546aSMatt Jacob 		xpt_lock_buses();
277f1e2546aSMatt Jacob 		TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
278f1e2546aSMatt Jacob 		xpt_unlock_buses();
2798b8a9b1dSJustin T. Gibbs 		xpt_free_path(periph->path);
2807379c88fSPoul-Henning Kamp 		/* FALLTHROUGH */
2818b8a9b1dSJustin T. Gibbs 	case 1:
282362abc44STai-hwa Liang 		free(periph, M_CAMPERIPH);
2837379c88fSPoul-Henning Kamp 		/* FALLTHROUGH */
2848b8a9b1dSJustin T. Gibbs 	case 0:
2858b8a9b1dSJustin T. Gibbs 		/* No cleanup to perform. */
2868b8a9b1dSJustin T. Gibbs 		break;
2878b8a9b1dSJustin T. Gibbs 	default:
288905cb85dSKenneth D. Merry 		panic("%s: Unknown init level", __func__);
2898b8a9b1dSJustin T. Gibbs 	}
2908b8a9b1dSJustin T. Gibbs 	return(status);
2918b8a9b1dSJustin T. Gibbs }
2928b8a9b1dSJustin T. Gibbs 
2938b8a9b1dSJustin T. Gibbs /*
2948b8a9b1dSJustin T. Gibbs  * Find a peripheral structure with the specified path, target, lun,
2958b8a9b1dSJustin T. Gibbs  * and (optionally) type.  If the name is NULL, this function will return
2968b8a9b1dSJustin T. Gibbs  * the first peripheral driver that matches the specified path.
2978b8a9b1dSJustin T. Gibbs  */
2988b8a9b1dSJustin T. Gibbs struct cam_periph *
2998b8a9b1dSJustin T. Gibbs cam_periph_find(struct cam_path *path, char *name)
3008b8a9b1dSJustin T. Gibbs {
3018b8a9b1dSJustin T. Gibbs 	struct periph_driver **p_drv;
3028b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph;
3038b8a9b1dSJustin T. Gibbs 
30477dc25ccSScott Long 	xpt_lock_buses();
3050b7c27b9SPeter Wemm 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
3068b8a9b1dSJustin T. Gibbs 
3078b8a9b1dSJustin T. Gibbs 		if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
3088b8a9b1dSJustin T. Gibbs 			continue;
3098b8a9b1dSJustin T. Gibbs 
31037d40066SPoul-Henning Kamp 		TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
3118b8a9b1dSJustin T. Gibbs 			if (xpt_path_comp(periph->path, path) == 0) {
31277dc25ccSScott Long 				xpt_unlock_buses();
313227d67aaSAlexander Motin 				cam_periph_assert(periph, MA_OWNED);
3148b8a9b1dSJustin T. Gibbs 				return(periph);
3158b8a9b1dSJustin T. Gibbs 			}
3168b8a9b1dSJustin T. Gibbs 		}
31777dc25ccSScott Long 		if (name != NULL) {
31877dc25ccSScott Long 			xpt_unlock_buses();
3198b8a9b1dSJustin T. Gibbs 			return(NULL);
3208b8a9b1dSJustin T. Gibbs 		}
32177dc25ccSScott Long 	}
32277dc25ccSScott Long 	xpt_unlock_buses();
3238b8a9b1dSJustin T. Gibbs 	return(NULL);
3248b8a9b1dSJustin T. Gibbs }
3258b8a9b1dSJustin T. Gibbs 
3263501942bSJustin T. Gibbs /*
3278900f4b8SKenneth D. Merry  * Find peripheral driver instances attached to the specified path.
3283501942bSJustin T. Gibbs  */
3293501942bSJustin T. Gibbs int
3303501942bSJustin T. Gibbs cam_periph_list(struct cam_path *path, struct sbuf *sb)
3313501942bSJustin T. Gibbs {
3328900f4b8SKenneth D. Merry 	struct sbuf local_sb;
3333501942bSJustin T. Gibbs 	struct periph_driver **p_drv;
3343501942bSJustin T. Gibbs 	struct cam_periph *periph;
3353501942bSJustin T. Gibbs 	int count;
3368900f4b8SKenneth D. Merry 	int sbuf_alloc_len;
3373501942bSJustin T. Gibbs 
3388900f4b8SKenneth D. Merry 	sbuf_alloc_len = 16;
3398900f4b8SKenneth D. Merry retry:
3408900f4b8SKenneth D. Merry 	sbuf_new(&local_sb, NULL, sbuf_alloc_len, SBUF_FIXEDLEN);
3413501942bSJustin T. Gibbs 	count = 0;
3423501942bSJustin T. Gibbs 	xpt_lock_buses();
3433501942bSJustin T. Gibbs 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
3443501942bSJustin T. Gibbs 
3453501942bSJustin T. Gibbs 		TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
3463501942bSJustin T. Gibbs 			if (xpt_path_comp(periph->path, path) != 0)
3473501942bSJustin T. Gibbs 				continue;
3483501942bSJustin T. Gibbs 
3498900f4b8SKenneth D. Merry 			if (sbuf_len(&local_sb) != 0)
3508900f4b8SKenneth D. Merry 				sbuf_cat(&local_sb, ",");
3513501942bSJustin T. Gibbs 
3528900f4b8SKenneth D. Merry 			sbuf_printf(&local_sb, "%s%d", periph->periph_name,
3533501942bSJustin T. Gibbs 				    periph->unit_number);
3548900f4b8SKenneth D. Merry 
3558900f4b8SKenneth D. Merry 			if (sbuf_error(&local_sb) == ENOMEM) {
3568900f4b8SKenneth D. Merry 				sbuf_alloc_len *= 2;
3578900f4b8SKenneth D. Merry 				xpt_unlock_buses();
3588900f4b8SKenneth D. Merry 				sbuf_delete(&local_sb);
3598900f4b8SKenneth D. Merry 				goto retry;
3608900f4b8SKenneth D. Merry 			}
3613501942bSJustin T. Gibbs 			count++;
3623501942bSJustin T. Gibbs 		}
3633501942bSJustin T. Gibbs 	}
3643501942bSJustin T. Gibbs 	xpt_unlock_buses();
3658900f4b8SKenneth D. Merry 	sbuf_finish(&local_sb);
3668900f4b8SKenneth D. Merry 	sbuf_cpy(sb, sbuf_data(&local_sb));
3678900f4b8SKenneth D. Merry 	sbuf_delete(&local_sb);
3683501942bSJustin T. Gibbs 	return (count);
3693501942bSJustin T. Gibbs }
3703501942bSJustin T. Gibbs 
3718b8a9b1dSJustin T. Gibbs cam_status
3728b8a9b1dSJustin T. Gibbs cam_periph_acquire(struct cam_periph *periph)
3738b8a9b1dSJustin T. Gibbs {
3748900f4b8SKenneth D. Merry 	cam_status status;
3758b8a9b1dSJustin T. Gibbs 
3768900f4b8SKenneth D. Merry 	status = CAM_REQ_CMP_ERR;
3778b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
3788900f4b8SKenneth D. Merry 		return (status);
3798b8a9b1dSJustin T. Gibbs 
3802b83592fSScott Long 	xpt_lock_buses();
3818900f4b8SKenneth D. Merry 	if ((periph->flags & CAM_PERIPH_INVALID) == 0) {
3828b8a9b1dSJustin T. Gibbs 		periph->refcount++;
3838900f4b8SKenneth D. Merry 		status = CAM_REQ_CMP;
3848900f4b8SKenneth D. Merry 	}
3852b83592fSScott Long 	xpt_unlock_buses();
3868b8a9b1dSJustin T. Gibbs 
3878900f4b8SKenneth D. Merry 	return (status);
3888900f4b8SKenneth D. Merry }
3898900f4b8SKenneth D. Merry 
3908900f4b8SKenneth D. Merry void
391c33e4029SAlexander Motin cam_periph_doacquire(struct cam_periph *periph)
392c33e4029SAlexander Motin {
393c33e4029SAlexander Motin 
394c33e4029SAlexander Motin 	xpt_lock_buses();
395c33e4029SAlexander Motin 	KASSERT(periph->refcount >= 1,
396c33e4029SAlexander Motin 	    ("cam_periph_doacquire() with refcount == %d", periph->refcount));
397c33e4029SAlexander Motin 	periph->refcount++;
398c33e4029SAlexander Motin 	xpt_unlock_buses();
399c33e4029SAlexander Motin }
400c33e4029SAlexander Motin 
401c33e4029SAlexander Motin void
4028900f4b8SKenneth D. Merry cam_periph_release_locked_buses(struct cam_periph *periph)
4038900f4b8SKenneth D. Merry {
404dcdf6e74SAlexander Motin 
405227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
406dcdf6e74SAlexander Motin 	KASSERT(periph->refcount >= 1, ("periph->refcount >= 1"));
407d38c0e53SAlexander Motin 	if (--periph->refcount == 0)
4088900f4b8SKenneth D. Merry 		camperiphfree(periph);
4098900f4b8SKenneth D. Merry }
4108b8a9b1dSJustin T. Gibbs 
4118b8a9b1dSJustin T. Gibbs void
41224ebf566SEdward Tomasz Napierala cam_periph_release_locked(struct cam_periph *periph)
4138b8a9b1dSJustin T. Gibbs {
4148b8a9b1dSJustin T. Gibbs 
4158b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
4168b8a9b1dSJustin T. Gibbs 		return;
4178b8a9b1dSJustin T. Gibbs 
4182b83592fSScott Long 	xpt_lock_buses();
4198900f4b8SKenneth D. Merry 	cam_periph_release_locked_buses(periph);
4202b83592fSScott Long 	xpt_unlock_buses();
42124ebf566SEdward Tomasz Napierala }
4228b8a9b1dSJustin T. Gibbs 
42324ebf566SEdward Tomasz Napierala void
42424ebf566SEdward Tomasz Napierala cam_periph_release(struct cam_periph *periph)
42524ebf566SEdward Tomasz Napierala {
426227d67aaSAlexander Motin 	struct mtx *mtx;
42724ebf566SEdward Tomasz Napierala 
42824ebf566SEdward Tomasz Napierala 	if (periph == NULL)
42924ebf566SEdward Tomasz Napierala 		return;
43024ebf566SEdward Tomasz Napierala 
431227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_NOTOWNED);
432227d67aaSAlexander Motin 	mtx = cam_periph_mtx(periph);
433227d67aaSAlexander Motin 	mtx_lock(mtx);
43424ebf566SEdward Tomasz Napierala 	cam_periph_release_locked(periph);
435227d67aaSAlexander Motin 	mtx_unlock(mtx);
4368b8a9b1dSJustin T. Gibbs }
4378b8a9b1dSJustin T. Gibbs 
4382b83592fSScott Long int
4392b83592fSScott Long cam_periph_hold(struct cam_periph *periph, int priority)
4402b83592fSScott Long {
4412b83592fSScott Long 	int error;
4422b83592fSScott Long 
4432b83592fSScott Long 	/*
4442b83592fSScott Long 	 * Increment the reference count on the peripheral
4452b83592fSScott Long 	 * while we wait for our lock attempt to succeed
4462b83592fSScott Long 	 * to ensure the peripheral doesn't disappear out
4472b83592fSScott Long 	 * from user us while we sleep.
4482b83592fSScott Long 	 */
4492b83592fSScott Long 
4502b83592fSScott Long 	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
4512b83592fSScott Long 		return (ENXIO);
4522b83592fSScott Long 
453227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
4542b83592fSScott Long 	while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
4552b83592fSScott Long 		periph->flags |= CAM_PERIPH_LOCK_WANTED;
456227d67aaSAlexander Motin 		if ((error = cam_periph_sleep(periph, periph, priority,
4571fa738c2SJohn Baldwin 		    "caplck", 0)) != 0) {
45824ebf566SEdward Tomasz Napierala 			cam_periph_release_locked(periph);
4592b83592fSScott Long 			return (error);
4602b83592fSScott Long 		}
461aed9c88cSAlexander Motin 		if (periph->flags & CAM_PERIPH_INVALID) {
462aed9c88cSAlexander Motin 			cam_periph_release_locked(periph);
463aed9c88cSAlexander Motin 			return (ENXIO);
464aed9c88cSAlexander Motin 		}
4652b83592fSScott Long 	}
4662b83592fSScott Long 
4672b83592fSScott Long 	periph->flags |= CAM_PERIPH_LOCKED;
4682b83592fSScott Long 	return (0);
4692b83592fSScott Long }
4702b83592fSScott Long 
4712b83592fSScott Long void
4722b83592fSScott Long cam_periph_unhold(struct cam_periph *periph)
4732b83592fSScott Long {
4742b83592fSScott Long 
475227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
4762b83592fSScott Long 
4772b83592fSScott Long 	periph->flags &= ~CAM_PERIPH_LOCKED;
4782b83592fSScott Long 	if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
4792b83592fSScott Long 		periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
4802b83592fSScott Long 		wakeup(periph);
4812b83592fSScott Long 	}
4822b83592fSScott Long 
48324ebf566SEdward Tomasz Napierala 	cam_periph_release_locked(periph);
4842b83592fSScott Long }
4852b83592fSScott Long 
4868b8a9b1dSJustin T. Gibbs /*
4878b8a9b1dSJustin T. Gibbs  * Look for the next unit number that is not currently in use for this
4888b8a9b1dSJustin T. Gibbs  * peripheral type starting at "newunit".  Also exclude unit numbers that
4898b8a9b1dSJustin T. Gibbs  * are reserved by for future "hardwiring" unless we already know that this
4908b8a9b1dSJustin T. Gibbs  * is a potential wired device.  Only assume that the device is "wired" the
4918b8a9b1dSJustin T. Gibbs  * first time through the loop since after that we'll be looking at unit
4928b8a9b1dSJustin T. Gibbs  * numbers that did not match a wiring entry.
4938b8a9b1dSJustin T. Gibbs  */
4948b8a9b1dSJustin T. Gibbs static u_int
495501468a5SKenneth D. Merry camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
496501468a5SKenneth D. Merry 		  path_id_t pathid, target_id_t target, lun_id_t lun)
4978b8a9b1dSJustin T. Gibbs {
4988b8a9b1dSJustin T. Gibbs 	struct	cam_periph *periph;
4992398f0cdSPeter Wemm 	char	*periph_name;
5002398f0cdSPeter Wemm 	int	i, val, dunit, r;
5012398f0cdSPeter Wemm 	const char *dname, *strval;
5028b8a9b1dSJustin T. Gibbs 
5038b8a9b1dSJustin T. Gibbs 	periph_name = p_drv->driver_name;
5048b8a9b1dSJustin T. Gibbs 	for (;;newunit++) {
5058b8a9b1dSJustin T. Gibbs 
5068b8a9b1dSJustin T. Gibbs 		for (periph = TAILQ_FIRST(&p_drv->units);
5078b8a9b1dSJustin T. Gibbs 		     periph != NULL && periph->unit_number != newunit;
5088b8a9b1dSJustin T. Gibbs 		     periph = TAILQ_NEXT(periph, unit_links))
5098b8a9b1dSJustin T. Gibbs 			;
5108b8a9b1dSJustin T. Gibbs 
5118b8a9b1dSJustin T. Gibbs 		if (periph != NULL && periph->unit_number == newunit) {
5128b8a9b1dSJustin T. Gibbs 			if (wired != 0) {
513f0d9af51SMatt Jacob 				xpt_print(periph->path, "Duplicate Wired "
514f0d9af51SMatt Jacob 				    "Device entry!\n");
515f0d9af51SMatt Jacob 				xpt_print(periph->path, "Second device (%s "
516f0d9af51SMatt Jacob 				    "device at scbus%d target %d lun %d) will "
517f0d9af51SMatt Jacob 				    "not be wired\n", periph_name, pathid,
518f0d9af51SMatt Jacob 				    target, lun);
5198b8a9b1dSJustin T. Gibbs 				wired = 0;
5208b8a9b1dSJustin T. Gibbs 			}
5218b8a9b1dSJustin T. Gibbs 			continue;
5228b8a9b1dSJustin T. Gibbs 		}
52375f51904SPeter Wemm 		if (wired)
52475f51904SPeter Wemm 			break;
5258b8a9b1dSJustin T. Gibbs 
5268b8a9b1dSJustin T. Gibbs 		/*
5278b8a9b1dSJustin T. Gibbs 		 * Don't match entries like "da 4" as a wired down
5288b8a9b1dSJustin T. Gibbs 		 * device, but do match entries like "da 4 target 5"
5298b8a9b1dSJustin T. Gibbs 		 * or even "da 4 scbus 1".
5308b8a9b1dSJustin T. Gibbs 		 */
5312398f0cdSPeter Wemm 		i = 0;
5322398f0cdSPeter Wemm 		dname = periph_name;
5332398f0cdSPeter Wemm 		for (;;) {
5342398f0cdSPeter Wemm 			r = resource_find_dev(&i, dname, &dunit, NULL, NULL);
5352398f0cdSPeter Wemm 			if (r != 0)
5362398f0cdSPeter Wemm 				break;
53775f51904SPeter Wemm 			/* if no "target" and no specific scbus, skip */
53875f51904SPeter Wemm 			if (resource_int_value(dname, dunit, "target", &val) &&
53975f51904SPeter Wemm 			    (resource_string_value(dname, dunit, "at",&strval)||
54075f51904SPeter Wemm 			     strcmp(strval, "scbus") == 0))
54175f51904SPeter Wemm 				continue;
54275f51904SPeter Wemm 			if (newunit == dunit)
5438b8a9b1dSJustin T. Gibbs 				break;
5448b8a9b1dSJustin T. Gibbs 		}
5452398f0cdSPeter Wemm 		if (r != 0)
5468b8a9b1dSJustin T. Gibbs 			break;
5478b8a9b1dSJustin T. Gibbs 	}
5488b8a9b1dSJustin T. Gibbs 	return (newunit);
5498b8a9b1dSJustin T. Gibbs }
5508b8a9b1dSJustin T. Gibbs 
5518b8a9b1dSJustin T. Gibbs static u_int
5528b8a9b1dSJustin T. Gibbs camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
5538b8a9b1dSJustin T. Gibbs 	      target_id_t target, lun_id_t lun)
5548b8a9b1dSJustin T. Gibbs {
5558b8a9b1dSJustin T. Gibbs 	u_int	unit;
556c1b81613SPeter Wemm 	int	wired, i, val, dunit;
5572398f0cdSPeter Wemm 	const char *dname, *strval;
5582398f0cdSPeter Wemm 	char	pathbuf[32], *periph_name;
5598b8a9b1dSJustin T. Gibbs 
56075f51904SPeter Wemm 	periph_name = p_drv->driver_name;
56175f51904SPeter Wemm 	snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
562c1b81613SPeter Wemm 	unit = 0;
5632398f0cdSPeter Wemm 	i = 0;
5642398f0cdSPeter Wemm 	dname = periph_name;
565c1b81613SPeter Wemm 	for (wired = 0; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0;
566c1b81613SPeter Wemm 	     wired = 0) {
56775f51904SPeter Wemm 		if (resource_string_value(dname, dunit, "at", &strval) == 0) {
56875f51904SPeter Wemm 			if (strcmp(strval, pathbuf) != 0)
5698b8a9b1dSJustin T. Gibbs 				continue;
570c1b81613SPeter Wemm 			wired++;
5718b8a9b1dSJustin T. Gibbs 		}
57275f51904SPeter Wemm 		if (resource_int_value(dname, dunit, "target", &val) == 0) {
57375f51904SPeter Wemm 			if (val != target)
5748b8a9b1dSJustin T. Gibbs 				continue;
575c1b81613SPeter Wemm 			wired++;
5768b8a9b1dSJustin T. Gibbs 		}
57775f51904SPeter Wemm 		if (resource_int_value(dname, dunit, "lun", &val) == 0) {
57875f51904SPeter Wemm 			if (val != lun)
5798b8a9b1dSJustin T. Gibbs 				continue;
580c1b81613SPeter Wemm 			wired++;
5818b8a9b1dSJustin T. Gibbs 		}
582c1b81613SPeter Wemm 		if (wired != 0) {
58375f51904SPeter Wemm 			unit = dunit;
5848b8a9b1dSJustin T. Gibbs 			break;
5858b8a9b1dSJustin T. Gibbs 		}
5868b8a9b1dSJustin T. Gibbs 	}
5878b8a9b1dSJustin T. Gibbs 
5888b8a9b1dSJustin T. Gibbs 	/*
5898b8a9b1dSJustin T. Gibbs 	 * Either start from 0 looking for the next unit or from
59075f51904SPeter Wemm 	 * the unit number given in the resource config.  This way,
5918b8a9b1dSJustin T. Gibbs 	 * if we have wildcard matches, we don't return the same
5928b8a9b1dSJustin T. Gibbs 	 * unit number twice.
5938b8a9b1dSJustin T. Gibbs 	 */
594c1b81613SPeter Wemm 	unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun);
5958b8a9b1dSJustin T. Gibbs 
5968b8a9b1dSJustin T. Gibbs 	return (unit);
5978b8a9b1dSJustin T. Gibbs }
5988b8a9b1dSJustin T. Gibbs 
5998b8a9b1dSJustin T. Gibbs void
6008b8a9b1dSJustin T. Gibbs cam_periph_invalidate(struct cam_periph *periph)
6018b8a9b1dSJustin T. Gibbs {
6028b8a9b1dSJustin T. Gibbs 
603227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
604ee9c90c7SKenneth D. Merry 	/*
605ee9c90c7SKenneth D. Merry 	 * We only call this routine the first time a peripheral is
60677dc25ccSScott Long 	 * invalidated.
607ee9c90c7SKenneth D. Merry 	 */
608d38c0e53SAlexander Motin 	if ((periph->flags & CAM_PERIPH_INVALID) != 0)
609d38c0e53SAlexander Motin 		return;
610ee9c90c7SKenneth D. Merry 
611d38c0e53SAlexander Motin 	CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph invalidated\n"));
612de6a705eSMarius Strobl 	if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting)
6138d36a71bSAlexander Motin 		xpt_denounce_periph(periph);
6148b8a9b1dSJustin T. Gibbs 	periph->flags |= CAM_PERIPH_INVALID;
6158b8a9b1dSJustin T. Gibbs 	periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
616d38c0e53SAlexander Motin 	if (periph->periph_oninval != NULL)
617d38c0e53SAlexander Motin 		periph->periph_oninval(periph);
618d38c0e53SAlexander Motin 	cam_periph_release_locked(periph);
6198b8a9b1dSJustin T. Gibbs }
6208b8a9b1dSJustin T. Gibbs 
6218b8a9b1dSJustin T. Gibbs static void
6228b8a9b1dSJustin T. Gibbs camperiphfree(struct cam_periph *periph)
6238b8a9b1dSJustin T. Gibbs {
6248b8a9b1dSJustin T. Gibbs 	struct periph_driver **p_drv;
6258b8a9b1dSJustin T. Gibbs 
626227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
627227d67aaSAlexander Motin 	KASSERT(periph->periph_allocating == 0, ("%s%d: freed while allocating",
628227d67aaSAlexander Motin 	    periph->periph_name, periph->unit_number));
6290b7c27b9SPeter Wemm 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
6308b8a9b1dSJustin T. Gibbs 		if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
6318b8a9b1dSJustin T. Gibbs 			break;
6328b8a9b1dSJustin T. Gibbs 	}
633661658a6SScott Long 	if (*p_drv == NULL) {
634661658a6SScott Long 		printf("camperiphfree: attempt to free non-existant periph\n");
635661658a6SScott Long 		return;
636661658a6SScott Long 	}
6378b8a9b1dSJustin T. Gibbs 
638ea37f519SKenneth D. Merry 	/*
63933a38f74SKenneth D. Merry 	 * We need to set this flag before dropping the topology lock, to
64033a38f74SKenneth D. Merry 	 * let anyone who is traversing the list that this peripheral is
64133a38f74SKenneth D. Merry 	 * about to be freed, and there will be no more reference count
64233a38f74SKenneth D. Merry 	 * checks.
64333a38f74SKenneth D. Merry 	 */
64433a38f74SKenneth D. Merry 	periph->flags |= CAM_PERIPH_FREE;
64533a38f74SKenneth D. Merry 
64633a38f74SKenneth D. Merry 	/*
647ea37f519SKenneth D. Merry 	 * The peripheral destructor semantics dictate calling with only the
648ea37f519SKenneth D. Merry 	 * SIM mutex held.  Since it might sleep, it should not be called
649ea37f519SKenneth D. Merry 	 * with the topology lock held.
650ea37f519SKenneth D. Merry 	 */
651f3548746SScott Long 	xpt_unlock_buses();
6528b8a9b1dSJustin T. Gibbs 
653ea37f519SKenneth D. Merry 	/*
654ea37f519SKenneth D. Merry 	 * We need to call the peripheral destructor prior to removing the
655ea37f519SKenneth D. Merry 	 * peripheral from the list.  Otherwise, we risk running into a
656ea37f519SKenneth D. Merry 	 * scenario where the peripheral unit number may get reused
657ea37f519SKenneth D. Merry 	 * (because it has been removed from the list), but some resources
658ea37f519SKenneth D. Merry 	 * used by the peripheral are still hanging around.  In particular,
659ea37f519SKenneth D. Merry 	 * the devfs nodes used by some peripherals like the pass(4) driver
660ea37f519SKenneth D. Merry 	 * aren't fully cleaned up until the destructor is run.  If the
661ea37f519SKenneth D. Merry 	 * unit number is reused before the devfs instance is fully gone,
662ea37f519SKenneth D. Merry 	 * devfs will panic.
663ea37f519SKenneth D. Merry 	 */
664f3548746SScott Long 	if (periph->periph_dtor != NULL)
665f3548746SScott Long 		periph->periph_dtor(periph);
666ea37f519SKenneth D. Merry 
667ea37f519SKenneth D. Merry 	/*
668ea37f519SKenneth D. Merry 	 * The peripheral list is protected by the topology lock.
669ea37f519SKenneth D. Merry 	 */
670ea37f519SKenneth D. Merry 	xpt_lock_buses();
671ea37f519SKenneth D. Merry 
672ea37f519SKenneth D. Merry 	TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
673ea37f519SKenneth D. Merry 	(*p_drv)->generation++;
674ea37f519SKenneth D. Merry 
675a29779e8SAlexander Motin 	xpt_remove_periph(periph);
676ea37f519SKenneth D. Merry 
677ea37f519SKenneth D. Merry 	xpt_unlock_buses();
678de6a705eSMarius Strobl 	if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting)
6798d36a71bSAlexander Motin 		xpt_print(periph->path, "Periph destroyed\n");
680de6a705eSMarius Strobl 	else
68122c7d606SAlexander Motin 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
6828b8a9b1dSJustin T. Gibbs 
6838b8a9b1dSJustin T. Gibbs 	if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
6848b8a9b1dSJustin T. Gibbs 		union ccb ccb;
6858b8a9b1dSJustin T. Gibbs 		void *arg;
6868b8a9b1dSJustin T. Gibbs 
6878b8a9b1dSJustin T. Gibbs 		switch (periph->deferred_ac) {
6888b8a9b1dSJustin T. Gibbs 		case AC_FOUND_DEVICE:
6898b8a9b1dSJustin T. Gibbs 			ccb.ccb_h.func_code = XPT_GDEV_TYPE;
690bbfa4aa1SAlexander Motin 			xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
6918b8a9b1dSJustin T. Gibbs 			xpt_action(&ccb);
6928b8a9b1dSJustin T. Gibbs 			arg = &ccb;
6938b8a9b1dSJustin T. Gibbs 			break;
6948b8a9b1dSJustin T. Gibbs 		case AC_PATH_REGISTERED:
6958b8a9b1dSJustin T. Gibbs 			ccb.ccb_h.func_code = XPT_PATH_INQ;
696bbfa4aa1SAlexander Motin 			xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
6978b8a9b1dSJustin T. Gibbs 			xpt_action(&ccb);
6988b8a9b1dSJustin T. Gibbs 			arg = &ccb;
6998b8a9b1dSJustin T. Gibbs 			break;
7008b8a9b1dSJustin T. Gibbs 		default:
7018b8a9b1dSJustin T. Gibbs 			arg = NULL;
7028b8a9b1dSJustin T. Gibbs 			break;
7038b8a9b1dSJustin T. Gibbs 		}
7048b8a9b1dSJustin T. Gibbs 		periph->deferred_callback(NULL, periph->deferred_ac,
7058b8a9b1dSJustin T. Gibbs 					  periph->path, arg);
7068b8a9b1dSJustin T. Gibbs 	}
7078b8a9b1dSJustin T. Gibbs 	xpt_free_path(periph->path);
708362abc44STai-hwa Liang 	free(periph, M_CAMPERIPH);
709f3548746SScott Long 	xpt_lock_buses();
7108b8a9b1dSJustin T. Gibbs }
7118b8a9b1dSJustin T. Gibbs 
7128b8a9b1dSJustin T. Gibbs /*
7138b8a9b1dSJustin T. Gibbs  * Map user virtual pointers into kernel virtual address space, so we can
71495fbded6SScott Long  * access the memory.  This is now a generic function that centralizes most
71595fbded6SScott Long  * of the sanity checks on the data flags, if any.
71695fbded6SScott Long  * This also only works for up to MAXPHYS memory.  Since we use
7178b8a9b1dSJustin T. Gibbs  * buffers to map stuff in and out, we're limited to the buffer size.
7188b8a9b1dSJustin T. Gibbs  */
7198b8a9b1dSJustin T. Gibbs int
720de239312SAlexander Motin cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo,
721de239312SAlexander Motin     u_int maxmap)
7228b8a9b1dSJustin T. Gibbs {
7232d5c7e45SMatthew Dillon 	int numbufs, i, j;
72479d49a06SKenneth D. Merry 	int flags[CAM_PERIPH_MAXMAPS];
7258b8a9b1dSJustin T. Gibbs 	u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
7268b8a9b1dSJustin T. Gibbs 	u_int32_t lengths[CAM_PERIPH_MAXMAPS];
7278b8a9b1dSJustin T. Gibbs 	u_int32_t dirs[CAM_PERIPH_MAXMAPS];
7288b8a9b1dSJustin T. Gibbs 
729de239312SAlexander Motin 	if (maxmap == 0)
730de239312SAlexander Motin 		maxmap = DFLTPHYS;	/* traditional default */
731de239312SAlexander Motin 	else if (maxmap > MAXPHYS)
732de239312SAlexander Motin 		maxmap = MAXPHYS;	/* for safety */
7338b8a9b1dSJustin T. Gibbs 	switch(ccb->ccb_h.func_code) {
7348b8a9b1dSJustin T. Gibbs 	case XPT_DEV_MATCH:
7358b8a9b1dSJustin T. Gibbs 		if (ccb->cdm.match_buf_len == 0) {
7368b8a9b1dSJustin T. Gibbs 			printf("cam_periph_mapmem: invalid match buffer "
7378b8a9b1dSJustin T. Gibbs 			       "length 0\n");
7388b8a9b1dSJustin T. Gibbs 			return(EINVAL);
7398b8a9b1dSJustin T. Gibbs 		}
7408b8a9b1dSJustin T. Gibbs 		if (ccb->cdm.pattern_buf_len > 0) {
7418b8a9b1dSJustin T. Gibbs 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
7428b8a9b1dSJustin T. Gibbs 			lengths[0] = ccb->cdm.pattern_buf_len;
7438b8a9b1dSJustin T. Gibbs 			dirs[0] = CAM_DIR_OUT;
7448b8a9b1dSJustin T. Gibbs 			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
7458b8a9b1dSJustin T. Gibbs 			lengths[1] = ccb->cdm.match_buf_len;
7468b8a9b1dSJustin T. Gibbs 			dirs[1] = CAM_DIR_IN;
7478b8a9b1dSJustin T. Gibbs 			numbufs = 2;
7488b8a9b1dSJustin T. Gibbs 		} else {
7498b8a9b1dSJustin T. Gibbs 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
7508b8a9b1dSJustin T. Gibbs 			lengths[0] = ccb->cdm.match_buf_len;
7518b8a9b1dSJustin T. Gibbs 			dirs[0] = CAM_DIR_IN;
7528b8a9b1dSJustin T. Gibbs 			numbufs = 1;
7538b8a9b1dSJustin T. Gibbs 		}
75452c9ce25SScott Long 		/*
75552c9ce25SScott Long 		 * This request will not go to the hardware, no reason
75652c9ce25SScott Long 		 * to be so strict. vmapbuf() is able to map up to MAXPHYS.
75752c9ce25SScott Long 		 */
75852c9ce25SScott Long 		maxmap = MAXPHYS;
7598b8a9b1dSJustin T. Gibbs 		break;
7608b8a9b1dSJustin T. Gibbs 	case XPT_SCSI_IO:
76187cfaf0eSJustin T. Gibbs 	case XPT_CONT_TARGET_IO:
7628b8a9b1dSJustin T. Gibbs 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
7638b8a9b1dSJustin T. Gibbs 			return(0);
76495fbded6SScott Long 		if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
76595fbded6SScott Long 			return (EINVAL);
7668b8a9b1dSJustin T. Gibbs 		data_ptrs[0] = &ccb->csio.data_ptr;
76779d49a06SKenneth D. Merry 		lengths[0] = ccb->csio.dxfer_len;
7688b8a9b1dSJustin T. Gibbs 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
7698b8a9b1dSJustin T. Gibbs 		numbufs = 1;
7708b8a9b1dSJustin T. Gibbs 		break;
77152c9ce25SScott Long 	case XPT_ATA_IO:
77252c9ce25SScott Long 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
77352c9ce25SScott Long 			return(0);
77495fbded6SScott Long 		if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
77595fbded6SScott Long 			return (EINVAL);
77652c9ce25SScott Long 		data_ptrs[0] = &ccb->ataio.data_ptr;
77752c9ce25SScott Long 		lengths[0] = ccb->ataio.dxfer_len;
77852c9ce25SScott Long 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
77952c9ce25SScott Long 		numbufs = 1;
78052c9ce25SScott Long 		break;
78106e79492SKenneth D. Merry 	case XPT_SMP_IO:
78206e79492SKenneth D. Merry 		data_ptrs[0] = &ccb->smpio.smp_request;
78306e79492SKenneth D. Merry 		lengths[0] = ccb->smpio.smp_request_len;
78406e79492SKenneth D. Merry 		dirs[0] = CAM_DIR_OUT;
78506e79492SKenneth D. Merry 		data_ptrs[1] = &ccb->smpio.smp_response;
78606e79492SKenneth D. Merry 		lengths[1] = ccb->smpio.smp_response_len;
78706e79492SKenneth D. Merry 		dirs[1] = CAM_DIR_IN;
78806e79492SKenneth D. Merry 		numbufs = 2;
78906e79492SKenneth D. Merry 		break;
7903501942bSJustin T. Gibbs 	case XPT_DEV_ADVINFO:
7913501942bSJustin T. Gibbs 		if (ccb->cdai.bufsiz == 0)
7927c103ddeSKenneth D. Merry 			return (0);
7937c103ddeSKenneth D. Merry 
7943501942bSJustin T. Gibbs 		data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
7953501942bSJustin T. Gibbs 		lengths[0] = ccb->cdai.bufsiz;
79606e79492SKenneth D. Merry 		dirs[0] = CAM_DIR_IN;
79706e79492SKenneth D. Merry 		numbufs = 1;
7987c103ddeSKenneth D. Merry 
7997c103ddeSKenneth D. Merry 		/*
8007c103ddeSKenneth D. Merry 		 * This request will not go to the hardware, no reason
8017c103ddeSKenneth D. Merry 		 * to be so strict. vmapbuf() is able to map up to MAXPHYS.
8027c103ddeSKenneth D. Merry 		 */
8037c103ddeSKenneth D. Merry 		maxmap = MAXPHYS;
80406e79492SKenneth D. Merry 		break;
8058b8a9b1dSJustin T. Gibbs 	default:
8068b8a9b1dSJustin T. Gibbs 		return(EINVAL);
8078b8a9b1dSJustin T. Gibbs 		break; /* NOTREACHED */
8088b8a9b1dSJustin T. Gibbs 	}
8098b8a9b1dSJustin T. Gibbs 
8108b8a9b1dSJustin T. Gibbs 	/*
81179d49a06SKenneth D. Merry 	 * Check the transfer length and permissions first, so we don't
81279d49a06SKenneth D. Merry 	 * have to unmap any previously mapped buffers.
8138b8a9b1dSJustin T. Gibbs 	 */
8148b8a9b1dSJustin T. Gibbs 	for (i = 0; i < numbufs; i++) {
81579d49a06SKenneth D. Merry 
81679d49a06SKenneth D. Merry 		flags[i] = 0;
81779d49a06SKenneth D. Merry 
81879d49a06SKenneth D. Merry 		/*
81979d49a06SKenneth D. Merry 		 * The userland data pointer passed in may not be page
82079d49a06SKenneth D. Merry 		 * aligned.  vmapbuf() truncates the address to a page
82179d49a06SKenneth D. Merry 		 * boundary, so if the address isn't page aligned, we'll
82279d49a06SKenneth D. Merry 		 * need enough space for the given transfer length, plus
82379d49a06SKenneth D. Merry 		 * whatever extra space is necessary to make it to the page
82479d49a06SKenneth D. Merry 		 * boundary.
82579d49a06SKenneth D. Merry 		 */
82679d49a06SKenneth D. Merry 		if ((lengths[i] +
82752c9ce25SScott Long 		    (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > maxmap){
8286d7b539aSPeter Wemm 			printf("cam_periph_mapmem: attempt to map %lu bytes, "
82952c9ce25SScott Long 			       "which is greater than %lu\n",
8306d7b539aSPeter Wemm 			       (long)(lengths[i] +
8316d7b539aSPeter Wemm 			       (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
83252c9ce25SScott Long 			       (u_long)maxmap);
83379d49a06SKenneth D. Merry 			return(E2BIG);
83479d49a06SKenneth D. Merry 		}
8358b8a9b1dSJustin T. Gibbs 
836edd24ab7SKenneth D. Merry 		if (dirs[i] & CAM_DIR_OUT) {
83721144e3bSPoul-Henning Kamp 			flags[i] = BIO_WRITE;
8388b8a9b1dSJustin T. Gibbs 		}
8398b8a9b1dSJustin T. Gibbs 
840edd24ab7SKenneth D. Merry 		if (dirs[i] & CAM_DIR_IN) {
84121144e3bSPoul-Henning Kamp 			flags[i] = BIO_READ;
8428b8a9b1dSJustin T. Gibbs 		}
8438b8a9b1dSJustin T. Gibbs 
84479d49a06SKenneth D. Merry 	}
84579d49a06SKenneth D. Merry 
84679d49a06SKenneth D. Merry 	/*
847cef367e6SEitan Adler 	 * This keeps the kernel stack of current thread from getting
84895fbded6SScott Long 	 * swapped.  In low-memory situations where the kernel stack might
84995fbded6SScott Long 	 * otherwise get swapped out, this holds it and allows the thread
85095fbded6SScott Long 	 * to make progress and release the kernel mapped pages sooner.
85195fbded6SScott Long 	 *
85279d49a06SKenneth D. Merry 	 * XXX KDM should I use P_NOSWAP instead?
85379d49a06SKenneth D. Merry 	 */
8540cbbb7bfSPeter Wemm 	PHOLD(curproc);
85579d49a06SKenneth D. Merry 
85679d49a06SKenneth D. Merry 	for (i = 0; i < numbufs; i++) {
8578b8a9b1dSJustin T. Gibbs 		/*
8588b8a9b1dSJustin T. Gibbs 		 * Get the buffer.
8598b8a9b1dSJustin T. Gibbs 		 */
8601c7c3c6aSMatthew Dillon 		mapinfo->bp[i] = getpbuf(NULL);
8618b8a9b1dSJustin T. Gibbs 
8628b8a9b1dSJustin T. Gibbs 		/* put our pointer in the data slot */
8638b8a9b1dSJustin T. Gibbs 		mapinfo->bp[i]->b_data = *data_ptrs[i];
8648b8a9b1dSJustin T. Gibbs 
865fade8dd7SJeff Roberson 		/* save the user's data address */
866fade8dd7SJeff Roberson 		mapinfo->bp[i]->b_caller1 = *data_ptrs[i];
867fade8dd7SJeff Roberson 
86852c9ce25SScott Long 		/* set the transfer length, we know it's < MAXPHYS */
8698b8a9b1dSJustin T. Gibbs 		mapinfo->bp[i]->b_bufsize = lengths[i];
8708b8a9b1dSJustin T. Gibbs 
87121144e3bSPoul-Henning Kamp 		/* set the direction */
87221144e3bSPoul-Henning Kamp 		mapinfo->bp[i]->b_iocmd = flags[i];
8738b8a9b1dSJustin T. Gibbs 
8742d5c7e45SMatthew Dillon 		/*
8752d5c7e45SMatthew Dillon 		 * Map the buffer into kernel memory.
8762d5c7e45SMatthew Dillon 		 *
8772d5c7e45SMatthew Dillon 		 * Note that useracc() alone is not a  sufficient test.
8782d5c7e45SMatthew Dillon 		 * vmapbuf() can still fail due to a smaller file mapped
8792d5c7e45SMatthew Dillon 		 * into a larger area of VM, or if userland races against
8802d5c7e45SMatthew Dillon 		 * vmapbuf() after the useracc() check.
8812d5c7e45SMatthew Dillon 		 */
882e81ff91eSKonstantin Belousov 		if (vmapbuf(mapinfo->bp[i], 1) < 0) {
8832d5c7e45SMatthew Dillon 			for (j = 0; j < i; ++j) {
884fade8dd7SJeff Roberson 				*data_ptrs[j] = mapinfo->bp[j]->b_caller1;
8858373f36dSAlan Cox 				vunmapbuf(mapinfo->bp[j]);
8862d5c7e45SMatthew Dillon 				relpbuf(mapinfo->bp[j], NULL);
8872d5c7e45SMatthew Dillon 			}
8888373f36dSAlan Cox 			relpbuf(mapinfo->bp[i], NULL);
8892d5c7e45SMatthew Dillon 			PRELE(curproc);
8902d5c7e45SMatthew Dillon 			return(EACCES);
8912d5c7e45SMatthew Dillon 		}
8928b8a9b1dSJustin T. Gibbs 
8938b8a9b1dSJustin T. Gibbs 		/* set our pointer to the new mapped area */
8948b8a9b1dSJustin T. Gibbs 		*data_ptrs[i] = mapinfo->bp[i]->b_data;
8958b8a9b1dSJustin T. Gibbs 
8968b8a9b1dSJustin T. Gibbs 		mapinfo->num_bufs_used++;
8978b8a9b1dSJustin T. Gibbs 	}
8988b8a9b1dSJustin T. Gibbs 
899a62525f3SMatt Jacob 	/*
900a62525f3SMatt Jacob 	 * Now that we've gotten this far, change ownership to the kernel
901a62525f3SMatt Jacob 	 * of the buffers so that we don't run afoul of returning to user
902a62525f3SMatt Jacob 	 * space with locks (on the buffer) held.
903a62525f3SMatt Jacob 	 */
904a62525f3SMatt Jacob 	for (i = 0; i < numbufs; i++) {
905a62525f3SMatt Jacob 		BUF_KERNPROC(mapinfo->bp[i]);
906a62525f3SMatt Jacob 	}
907a62525f3SMatt Jacob 
908a62525f3SMatt Jacob 
9098b8a9b1dSJustin T. Gibbs 	return(0);
9108b8a9b1dSJustin T. Gibbs }
9118b8a9b1dSJustin T. Gibbs 
9128b8a9b1dSJustin T. Gibbs /*
9138b8a9b1dSJustin T. Gibbs  * Unmap memory segments mapped into kernel virtual address space by
9148b8a9b1dSJustin T. Gibbs  * cam_periph_mapmem().
9158b8a9b1dSJustin T. Gibbs  */
9168b8a9b1dSJustin T. Gibbs void
9178b8a9b1dSJustin T. Gibbs cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
9188b8a9b1dSJustin T. Gibbs {
9198b8a9b1dSJustin T. Gibbs 	int numbufs, i;
9208b8a9b1dSJustin T. Gibbs 	u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
9218b8a9b1dSJustin T. Gibbs 
9228b8a9b1dSJustin T. Gibbs 	if (mapinfo->num_bufs_used <= 0) {
92395fbded6SScott Long 		/* nothing to free and the process wasn't held. */
9248b8a9b1dSJustin T. Gibbs 		return;
9258b8a9b1dSJustin T. Gibbs 	}
9268b8a9b1dSJustin T. Gibbs 
9278b8a9b1dSJustin T. Gibbs 	switch (ccb->ccb_h.func_code) {
9288b8a9b1dSJustin T. Gibbs 	case XPT_DEV_MATCH:
9298b8a9b1dSJustin T. Gibbs 		numbufs = min(mapinfo->num_bufs_used, 2);
9308b8a9b1dSJustin T. Gibbs 
9318b8a9b1dSJustin T. Gibbs 		if (numbufs == 1) {
9328b8a9b1dSJustin T. Gibbs 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
9338b8a9b1dSJustin T. Gibbs 		} else {
9348b8a9b1dSJustin T. Gibbs 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
9358b8a9b1dSJustin T. Gibbs 			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
9368b8a9b1dSJustin T. Gibbs 		}
9378b8a9b1dSJustin T. Gibbs 		break;
9388b8a9b1dSJustin T. Gibbs 	case XPT_SCSI_IO:
9399911ecf9SJustin T. Gibbs 	case XPT_CONT_TARGET_IO:
9408b8a9b1dSJustin T. Gibbs 		data_ptrs[0] = &ccb->csio.data_ptr;
9418b8a9b1dSJustin T. Gibbs 		numbufs = min(mapinfo->num_bufs_used, 1);
9428b8a9b1dSJustin T. Gibbs 		break;
94352c9ce25SScott Long 	case XPT_ATA_IO:
94452c9ce25SScott Long 		data_ptrs[0] = &ccb->ataio.data_ptr;
94552c9ce25SScott Long 		numbufs = min(mapinfo->num_bufs_used, 1);
94652c9ce25SScott Long 		break;
94706e79492SKenneth D. Merry 	case XPT_SMP_IO:
94806e79492SKenneth D. Merry 		numbufs = min(mapinfo->num_bufs_used, 2);
94906e79492SKenneth D. Merry 		data_ptrs[0] = &ccb->smpio.smp_request;
95006e79492SKenneth D. Merry 		data_ptrs[1] = &ccb->smpio.smp_response;
95106e79492SKenneth D. Merry 		break;
9523501942bSJustin T. Gibbs 	case XPT_DEV_ADVINFO:
95306e79492SKenneth D. Merry 		numbufs = min(mapinfo->num_bufs_used, 1);
9543501942bSJustin T. Gibbs 		data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
95506e79492SKenneth D. Merry 		break;
9568b8a9b1dSJustin T. Gibbs 	default:
9578b8a9b1dSJustin T. Gibbs 		/* allow ourselves to be swapped once again */
9580cbbb7bfSPeter Wemm 		PRELE(curproc);
9598b8a9b1dSJustin T. Gibbs 		return;
9608b8a9b1dSJustin T. Gibbs 		break; /* NOTREACHED */
9618b8a9b1dSJustin T. Gibbs 	}
9628b8a9b1dSJustin T. Gibbs 
9638b8a9b1dSJustin T. Gibbs 	for (i = 0; i < numbufs; i++) {
9648b8a9b1dSJustin T. Gibbs 		/* Set the user's pointer back to the original value */
965fade8dd7SJeff Roberson 		*data_ptrs[i] = mapinfo->bp[i]->b_caller1;
9668b8a9b1dSJustin T. Gibbs 
9678b8a9b1dSJustin T. Gibbs 		/* unmap the buffer */
9688b8a9b1dSJustin T. Gibbs 		vunmapbuf(mapinfo->bp[i]);
9698b8a9b1dSJustin T. Gibbs 
9708b8a9b1dSJustin T. Gibbs 		/* release the buffer */
9711c7c3c6aSMatthew Dillon 		relpbuf(mapinfo->bp[i], NULL);
9728b8a9b1dSJustin T. Gibbs 	}
9738b8a9b1dSJustin T. Gibbs 
9748b8a9b1dSJustin T. Gibbs 	/* allow ourselves to be swapped once again */
9750cbbb7bfSPeter Wemm 	PRELE(curproc);
9768b8a9b1dSJustin T. Gibbs }
9778b8a9b1dSJustin T. Gibbs 
9788b8a9b1dSJustin T. Gibbs int
979571e8e20SScott Long cam_periph_ioctl(struct cam_periph *periph, u_long cmd, caddr_t addr,
9808b8a9b1dSJustin T. Gibbs 		 int (*error_routine)(union ccb *ccb,
9818b8a9b1dSJustin T. Gibbs 				      cam_flags camflags,
9828b8a9b1dSJustin T. Gibbs 				      u_int32_t sense_flags))
9838b8a9b1dSJustin T. Gibbs {
9848b8a9b1dSJustin T. Gibbs 	union ccb 	     *ccb;
9858b8a9b1dSJustin T. Gibbs 	int 		     error;
9868b8a9b1dSJustin T. Gibbs 	int		     found;
9878b8a9b1dSJustin T. Gibbs 
9888b8a9b1dSJustin T. Gibbs 	error = found = 0;
9898b8a9b1dSJustin T. Gibbs 
9908b8a9b1dSJustin T. Gibbs 	switch(cmd){
9918b8a9b1dSJustin T. Gibbs 	case CAMGETPASSTHRU:
992bbfa4aa1SAlexander Motin 		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
9938b8a9b1dSJustin T. Gibbs 		xpt_setup_ccb(&ccb->ccb_h,
9948b8a9b1dSJustin T. Gibbs 			      ccb->ccb_h.path,
995bbfa4aa1SAlexander Motin 			      CAM_PRIORITY_NORMAL);
9968b8a9b1dSJustin T. Gibbs 		ccb->ccb_h.func_code = XPT_GDEVLIST;
9978b8a9b1dSJustin T. Gibbs 
9988b8a9b1dSJustin T. Gibbs 		/*
9998b8a9b1dSJustin T. Gibbs 		 * Basically, the point of this is that we go through
10008b8a9b1dSJustin T. Gibbs 		 * getting the list of devices, until we find a passthrough
10018b8a9b1dSJustin T. Gibbs 		 * device.  In the current version of the CAM code, the
10028b8a9b1dSJustin T. Gibbs 		 * only way to determine what type of device we're dealing
10038b8a9b1dSJustin T. Gibbs 		 * with is by its name.
10048b8a9b1dSJustin T. Gibbs 		 */
10058b8a9b1dSJustin T. Gibbs 		while (found == 0) {
10068b8a9b1dSJustin T. Gibbs 			ccb->cgdl.index = 0;
10078b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
10088b8a9b1dSJustin T. Gibbs 			while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
10098b8a9b1dSJustin T. Gibbs 
10108b8a9b1dSJustin T. Gibbs 				/* we want the next device in the list */
10118b8a9b1dSJustin T. Gibbs 				xpt_action(ccb);
10128b8a9b1dSJustin T. Gibbs 				if (strncmp(ccb->cgdl.periph_name,
10138b8a9b1dSJustin T. Gibbs 				    "pass", 4) == 0){
10148b8a9b1dSJustin T. Gibbs 					found = 1;
10158b8a9b1dSJustin T. Gibbs 					break;
10168b8a9b1dSJustin T. Gibbs 				}
10178b8a9b1dSJustin T. Gibbs 			}
10188b8a9b1dSJustin T. Gibbs 			if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
10198b8a9b1dSJustin T. Gibbs 			    (found == 0)) {
10208b8a9b1dSJustin T. Gibbs 				ccb->cgdl.periph_name[0] = '\0';
10218b8a9b1dSJustin T. Gibbs 				ccb->cgdl.unit_number = 0;
10228b8a9b1dSJustin T. Gibbs 				break;
10238b8a9b1dSJustin T. Gibbs 			}
10248b8a9b1dSJustin T. Gibbs 		}
10258b8a9b1dSJustin T. Gibbs 
10268b8a9b1dSJustin T. Gibbs 		/* copy the result back out */
10278b8a9b1dSJustin T. Gibbs 		bcopy(ccb, addr, sizeof(union ccb));
10288b8a9b1dSJustin T. Gibbs 
10298b8a9b1dSJustin T. Gibbs 		/* and release the ccb */
10308b8a9b1dSJustin T. Gibbs 		xpt_release_ccb(ccb);
10318b8a9b1dSJustin T. Gibbs 
10328b8a9b1dSJustin T. Gibbs 		break;
10338b8a9b1dSJustin T. Gibbs 	default:
10348b8a9b1dSJustin T. Gibbs 		error = ENOTTY;
10358b8a9b1dSJustin T. Gibbs 		break;
10368b8a9b1dSJustin T. Gibbs 	}
10378b8a9b1dSJustin T. Gibbs 	return(error);
10388b8a9b1dSJustin T. Gibbs }
10398b8a9b1dSJustin T. Gibbs 
1040227d67aaSAlexander Motin static void
1041*991b5d26SMark Johnston cam_periph_done_panic(struct cam_periph *periph, union ccb *done_ccb)
1042*991b5d26SMark Johnston {
1043*991b5d26SMark Johnston 
1044*991b5d26SMark Johnston 	panic("%s: already done with ccb %p", __func__, done_ccb);
1045*991b5d26SMark Johnston }
1046*991b5d26SMark Johnston 
1047*991b5d26SMark Johnston static void
1048227d67aaSAlexander Motin cam_periph_done(struct cam_periph *periph, union ccb *done_ccb)
1049227d67aaSAlexander Motin {
1050227d67aaSAlexander Motin 
1051227d67aaSAlexander Motin 	/* Caller will release the CCB */
1052*991b5d26SMark Johnston 	xpt_path_assert(done_ccb->ccb_h.path, MA_OWNED);
1053*991b5d26SMark Johnston 	done_ccb->ccb_h.cbfcnp = cam_periph_done_panic;
1054227d67aaSAlexander Motin 	wakeup(&done_ccb->ccb_h.cbfcnp);
1055227d67aaSAlexander Motin }
1056227d67aaSAlexander Motin 
1057*991b5d26SMark Johnston static void
1058*991b5d26SMark Johnston cam_periph_ccbwait(union ccb *ccb)
1059*991b5d26SMark Johnston {
1060*991b5d26SMark Johnston 
1061*991b5d26SMark Johnston 	if ((ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
1062*991b5d26SMark Johnston 		while (ccb->ccb_h.cbfcnp != cam_periph_done_panic)
1063*991b5d26SMark Johnston 			xpt_path_sleep(ccb->ccb_h.path, &ccb->ccb_h.cbfcnp,
1064*991b5d26SMark Johnston 			    PRIBIO, "cbwait", 0);
1065*991b5d26SMark Johnston 	}
1066*991b5d26SMark Johnston 	KASSERT(ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX &&
1067*991b5d26SMark Johnston 	    (ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG,
1068*991b5d26SMark Johnston 	    ("%s: proceeding with incomplete ccb: ccb=%p, func_code=%#x, "
1069*991b5d26SMark Johnston 	     "status=%#x, index=%d", __func__, ccb, ccb->ccb_h.func_code,
1070*991b5d26SMark Johnston 	     ccb->ccb_h.status, ccb->ccb_h.pinfo.index));
1071*991b5d26SMark Johnston }
1072*991b5d26SMark Johnston 
10738b8a9b1dSJustin T. Gibbs int
10748b8a9b1dSJustin T. Gibbs cam_periph_runccb(union ccb *ccb,
10758b8a9b1dSJustin T. Gibbs 		  int (*error_routine)(union ccb *ccb,
10768b8a9b1dSJustin T. Gibbs 				       cam_flags camflags,
10778b8a9b1dSJustin T. Gibbs 				       u_int32_t sense_flags),
10788b8a9b1dSJustin T. Gibbs 		  cam_flags camflags, u_int32_t sense_flags,
10798b8a9b1dSJustin T. Gibbs 		  struct devstat *ds)
10808b8a9b1dSJustin T. Gibbs {
1081fd8be18fSKenneth D. Merry 	struct bintime *starttime;
1082fd8be18fSKenneth D. Merry 	struct bintime ltime;
10838b8a9b1dSJustin T. Gibbs 	int error;
10848b8a9b1dSJustin T. Gibbs 
1085fd8be18fSKenneth D. Merry 	starttime = NULL;
1086227d67aaSAlexander Motin 	xpt_path_assert(ccb->ccb_h.path, MA_OWNED);
1087*991b5d26SMark Johnston 	KASSERT((ccb->ccb_h.flags & CAM_UNLOCKED) == 0,
1088*991b5d26SMark Johnston 	    ("%s: ccb=%p, func_code=%#x, flags=%#x", __func__, ccb,
1089*991b5d26SMark Johnston 	     ccb->ccb_h.func_code, ccb->ccb_h.flags));
10908b8a9b1dSJustin T. Gibbs 
10918b8a9b1dSJustin T. Gibbs 	/*
10928b8a9b1dSJustin T. Gibbs 	 * If the user has supplied a stats structure, and if we understand
10938b8a9b1dSJustin T. Gibbs 	 * this particular type of ccb, record the transaction start.
10948b8a9b1dSJustin T. Gibbs 	 */
1095379ad358SAlexander Motin 	if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO ||
1096fd8be18fSKenneth D. Merry 	    ccb->ccb_h.func_code == XPT_ATA_IO)) {
1097fd8be18fSKenneth D. Merry 		starttime = &ltime;
1098fd8be18fSKenneth D. Merry 		binuptime(starttime);
1099fd8be18fSKenneth D. Merry 		devstat_start_transaction(ds, starttime);
1100fd8be18fSKenneth D. Merry 	}
11018b8a9b1dSJustin T. Gibbs 
1102227d67aaSAlexander Motin 	ccb->ccb_h.cbfcnp = cam_periph_done;
11038b8a9b1dSJustin T. Gibbs 	xpt_action(ccb);
11048b8a9b1dSJustin T. Gibbs 
11058b8a9b1dSJustin T. Gibbs 	do {
11068b8a9b1dSJustin T. Gibbs 		cam_periph_ccbwait(ccb);
11078b8a9b1dSJustin T. Gibbs 		if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
11088b8a9b1dSJustin T. Gibbs 			error = 0;
1109*991b5d26SMark Johnston 		else if (error_routine != NULL) {
1110*991b5d26SMark Johnston 			ccb->ccb_h.cbfcnp = cam_periph_done;
11118b8a9b1dSJustin T. Gibbs 			error = (*error_routine)(ccb, camflags, sense_flags);
1112*991b5d26SMark Johnston 		} else
11138b8a9b1dSJustin T. Gibbs 			error = 0;
11148b8a9b1dSJustin T. Gibbs 
11158b8a9b1dSJustin T. Gibbs 	} while (error == ERESTART);
11168b8a9b1dSJustin T. Gibbs 
111783c5d981SAlexander Motin 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
11188b8a9b1dSJustin T. Gibbs 		cam_release_devq(ccb->ccb_h.path,
11198b8a9b1dSJustin T. Gibbs 				 /* relsim_flags */0,
11208b8a9b1dSJustin T. Gibbs 				 /* openings */0,
11218b8a9b1dSJustin T. Gibbs 				 /* timeout */0,
11228b8a9b1dSJustin T. Gibbs 				 /* getcount_only */ FALSE);
112383c5d981SAlexander Motin 		ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
112483c5d981SAlexander Motin 	}
11258b8a9b1dSJustin T. Gibbs 
1126379ad358SAlexander Motin 	if (ds != NULL) {
1127379ad358SAlexander Motin 		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
11288b8a9b1dSJustin T. Gibbs 			devstat_end_transaction(ds,
1129fd8be18fSKenneth D. Merry 					ccb->csio.dxfer_len - ccb->csio.resid,
1130379ad358SAlexander Motin 					ccb->csio.tag_action & 0x3,
11318b8a9b1dSJustin T. Gibbs 					((ccb->ccb_h.flags & CAM_DIR_MASK) ==
11328b8a9b1dSJustin T. Gibbs 					CAM_DIR_NONE) ?  DEVSTAT_NO_DATA :
11338b8a9b1dSJustin T. Gibbs 					(ccb->ccb_h.flags & CAM_DIR_OUT) ?
11348b8a9b1dSJustin T. Gibbs 					DEVSTAT_WRITE :
1135fd8be18fSKenneth D. Merry 					DEVSTAT_READ, NULL, starttime);
1136379ad358SAlexander Motin 		} else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1137379ad358SAlexander Motin 			devstat_end_transaction(ds,
1138fd8be18fSKenneth D. Merry 					ccb->ataio.dxfer_len - ccb->ataio.resid,
1139e4cc6558SWarner Losh 					0, /* Not used in ATA */
1140379ad358SAlexander Motin 					((ccb->ccb_h.flags & CAM_DIR_MASK) ==
1141379ad358SAlexander Motin 					CAM_DIR_NONE) ?  DEVSTAT_NO_DATA :
1142379ad358SAlexander Motin 					(ccb->ccb_h.flags & CAM_DIR_OUT) ?
1143379ad358SAlexander Motin 					DEVSTAT_WRITE :
1144fd8be18fSKenneth D. Merry 					DEVSTAT_READ, NULL, starttime);
1145379ad358SAlexander Motin 		}
1146379ad358SAlexander Motin 	}
11478b8a9b1dSJustin T. Gibbs 
11488b8a9b1dSJustin T. Gibbs 	return(error);
11498b8a9b1dSJustin T. Gibbs }
11508b8a9b1dSJustin T. Gibbs 
115187cfaf0eSJustin T. Gibbs void
115287cfaf0eSJustin T. Gibbs cam_freeze_devq(struct cam_path *path)
115387cfaf0eSJustin T. Gibbs {
1154cccf4220SAlexander Motin 	struct ccb_hdr ccb_h;
115587cfaf0eSJustin T. Gibbs 
11560d4f3c31SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_freeze_devq\n"));
1157cccf4220SAlexander Motin 	xpt_setup_ccb(&ccb_h, path, /*priority*/1);
1158cccf4220SAlexander Motin 	ccb_h.func_code = XPT_NOOP;
1159cccf4220SAlexander Motin 	ccb_h.flags = CAM_DEV_QFREEZE;
1160cccf4220SAlexander Motin 	xpt_action((union ccb *)&ccb_h);
116187cfaf0eSJustin T. Gibbs }
116287cfaf0eSJustin T. Gibbs 
11638b8a9b1dSJustin T. Gibbs u_int32_t
11648b8a9b1dSJustin T. Gibbs cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
116583c5d981SAlexander Motin 		 u_int32_t openings, u_int32_t arg,
11668b8a9b1dSJustin T. Gibbs 		 int getcount_only)
11678b8a9b1dSJustin T. Gibbs {
11688b8a9b1dSJustin T. Gibbs 	struct ccb_relsim crs;
11698b8a9b1dSJustin T. Gibbs 
11700d4f3c31SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_release_devq(%u, %u, %u, %d)\n",
11710d4f3c31SAlexander Motin 	    relsim_flags, openings, arg, getcount_only));
1172bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
11738b8a9b1dSJustin T. Gibbs 	crs.ccb_h.func_code = XPT_REL_SIMQ;
11748b8a9b1dSJustin T. Gibbs 	crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
11758b8a9b1dSJustin T. Gibbs 	crs.release_flags = relsim_flags;
11768b8a9b1dSJustin T. Gibbs 	crs.openings = openings;
117783c5d981SAlexander Motin 	crs.release_timeout = arg;
11788b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&crs);
11798b8a9b1dSJustin T. Gibbs 	return (crs.qfrozen_cnt);
11808b8a9b1dSJustin T. Gibbs }
11818b8a9b1dSJustin T. Gibbs 
11828b8a9b1dSJustin T. Gibbs #define saved_ccb_ptr ppriv_ptr0
118383c5d981SAlexander Motin static void
118483c5d981SAlexander Motin camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
118583c5d981SAlexander Motin {
11860191d9b3SAlexander Motin 	union ccb      *saved_ccb;
118783c5d981SAlexander Motin 	cam_status	status;
118883c5d981SAlexander Motin 	struct scsi_start_stop_unit *scsi_cmd;
1189e7493b28SAlexander Motin 	int    error_code, sense_key, asc, ascq;
119083c5d981SAlexander Motin 
11910191d9b3SAlexander Motin 	scsi_cmd = (struct scsi_start_stop_unit *)
11920191d9b3SAlexander Motin 	    &done_ccb->csio.cdb_io.cdb_bytes;
119383c5d981SAlexander Motin 	status = done_ccb->ccb_h.status;
11948b8a9b1dSJustin T. Gibbs 
11950191d9b3SAlexander Motin 	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1196e7493b28SAlexander Motin 		if (scsi_extract_sense_ccb(done_ccb,
1197e7493b28SAlexander Motin 		    &error_code, &sense_key, &asc, &ascq)) {
119865afe1f4SKenneth D. Merry 			/*
11998b8a9b1dSJustin T. Gibbs 			 * If the error is "invalid field in CDB",
12008b8a9b1dSJustin T. Gibbs 			 * and the load/eject flag is set, turn the
12018b8a9b1dSJustin T. Gibbs 			 * flag off and try again.  This is just in
12028b8a9b1dSJustin T. Gibbs 			 * case the drive in question barfs on the
12038b8a9b1dSJustin T. Gibbs 			 * load eject flag.  The CAM code should set
12048b8a9b1dSJustin T. Gibbs 			 * the load/eject flag by default for
12058b8a9b1dSJustin T. Gibbs 			 * removable media.
12068b8a9b1dSJustin T. Gibbs 			 */
12078b8a9b1dSJustin T. Gibbs 			if ((scsi_cmd->opcode == START_STOP_UNIT) &&
12088b8a9b1dSJustin T. Gibbs 			    ((scsi_cmd->how & SSS_LOEJ) != 0) &&
12090191d9b3SAlexander Motin 			     (asc == 0x24) && (ascq == 0x00)) {
12108b8a9b1dSJustin T. Gibbs 				scsi_cmd->how &= ~SSS_LOEJ;
12110191d9b3SAlexander Motin 				if (status & CAM_DEV_QFRZN) {
12120191d9b3SAlexander Motin 					cam_release_devq(done_ccb->ccb_h.path,
12130191d9b3SAlexander Motin 					    0, 0, 0, 0);
12140191d9b3SAlexander Motin 					done_ccb->ccb_h.status &=
12150191d9b3SAlexander Motin 					    ~CAM_DEV_QFRZN;
12160191d9b3SAlexander Motin 				}
12178b8a9b1dSJustin T. Gibbs 				xpt_action(done_ccb);
12180191d9b3SAlexander Motin 				goto out;
12190191d9b3SAlexander Motin 			}
12200191d9b3SAlexander Motin 		}
12210191d9b3SAlexander Motin 		if (cam_periph_error(done_ccb,
12220191d9b3SAlexander Motin 		    0, SF_RETRY_UA | SF_NO_PRINT, NULL) == ERESTART)
12230191d9b3SAlexander Motin 			goto out;
12240191d9b3SAlexander Motin 		if (done_ccb->ccb_h.status & CAM_DEV_QFRZN) {
12250191d9b3SAlexander Motin 			cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
12260191d9b3SAlexander Motin 			done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
12270191d9b3SAlexander Motin 		}
12280191d9b3SAlexander Motin 	} else {
12290191d9b3SAlexander Motin 		/*
12300191d9b3SAlexander Motin 		 * If we have successfully taken a device from the not
12310191d9b3SAlexander Motin 		 * ready to ready state, re-scan the device and re-get
12320191d9b3SAlexander Motin 		 * the inquiry information.  Many devices (mostly disks)
12330191d9b3SAlexander Motin 		 * don't properly report their inquiry information unless
12340191d9b3SAlexander Motin 		 * they are spun up.
12350191d9b3SAlexander Motin 		 */
12360191d9b3SAlexander Motin 		if (scsi_cmd->opcode == START_STOP_UNIT)
12370191d9b3SAlexander Motin 			xpt_async(AC_INQ_CHANGED, done_ccb->ccb_h.path, NULL);
12380191d9b3SAlexander Motin 	}
123965afe1f4SKenneth D. Merry 
12408b8a9b1dSJustin T. Gibbs 	/*
12410191d9b3SAlexander Motin 	 * Perform the final retry with the original CCB so that final
12420191d9b3SAlexander Motin 	 * error processing is performed by the owner of the CCB.
12438b8a9b1dSJustin T. Gibbs 	 */
12440191d9b3SAlexander Motin 	saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
124583c5d981SAlexander Motin 	bcopy(saved_ccb, done_ccb, sizeof(*done_ccb));
124683c5d981SAlexander Motin 	xpt_free_ccb(saved_ccb);
12470191d9b3SAlexander Motin 	if (done_ccb->ccb_h.cbfcnp != camperiphdone)
124860a899a0SKenneth D. Merry 		periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
12498b8a9b1dSJustin T. Gibbs 	xpt_action(done_ccb);
12508b8a9b1dSJustin T. Gibbs 
12510191d9b3SAlexander Motin out:
12520191d9b3SAlexander Motin 	/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
12530191d9b3SAlexander Motin 	cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1254d84c90a6SAlexander Motin }
12558b8a9b1dSJustin T. Gibbs 
12568b8a9b1dSJustin T. Gibbs /*
125787cfaf0eSJustin T. Gibbs  * Generic Async Event handler.  Peripheral drivers usually
125887cfaf0eSJustin T. Gibbs  * filter out the events that require personal attention,
125987cfaf0eSJustin T. Gibbs  * and leave the rest to this function.
126087cfaf0eSJustin T. Gibbs  */
126187cfaf0eSJustin T. Gibbs void
126287cfaf0eSJustin T. Gibbs cam_periph_async(struct cam_periph *periph, u_int32_t code,
126387cfaf0eSJustin T. Gibbs 		 struct cam_path *path, void *arg)
126487cfaf0eSJustin T. Gibbs {
126587cfaf0eSJustin T. Gibbs 	switch (code) {
126687cfaf0eSJustin T. Gibbs 	case AC_LOST_DEVICE:
126787cfaf0eSJustin T. Gibbs 		cam_periph_invalidate(periph);
126887cfaf0eSJustin T. Gibbs 		break;
126987cfaf0eSJustin T. Gibbs 	default:
127087cfaf0eSJustin T. Gibbs 		break;
127187cfaf0eSJustin T. Gibbs 	}
127287cfaf0eSJustin T. Gibbs }
127387cfaf0eSJustin T. Gibbs 
127487cfaf0eSJustin T. Gibbs void
127587cfaf0eSJustin T. Gibbs cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
127687cfaf0eSJustin T. Gibbs {
127787cfaf0eSJustin T. Gibbs 	struct ccb_getdevstats cgds;
127887cfaf0eSJustin T. Gibbs 
1279bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cgds.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
128087cfaf0eSJustin T. Gibbs 	cgds.ccb_h.func_code = XPT_GDEV_STATS;
128187cfaf0eSJustin T. Gibbs 	xpt_action((union ccb *)&cgds);
128287cfaf0eSJustin T. Gibbs 	cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
128387cfaf0eSJustin T. Gibbs }
128487cfaf0eSJustin T. Gibbs 
128587cfaf0eSJustin T. Gibbs void
128687cfaf0eSJustin T. Gibbs cam_periph_freeze_after_event(struct cam_periph *periph,
128787cfaf0eSJustin T. Gibbs 			      struct timeval* event_time, u_int duration_ms)
128887cfaf0eSJustin T. Gibbs {
128987cfaf0eSJustin T. Gibbs 	struct timeval delta;
129087cfaf0eSJustin T. Gibbs 	struct timeval duration_tv;
129187cfaf0eSJustin T. Gibbs 
1292f8ff57d2SAlexander Motin 	if (!timevalisset(event_time))
1293f8ff57d2SAlexander Motin 		return;
1294f8ff57d2SAlexander Motin 
129587cfaf0eSJustin T. Gibbs 	microtime(&delta);
129687cfaf0eSJustin T. Gibbs 	timevalsub(&delta, event_time);
129787cfaf0eSJustin T. Gibbs 	duration_tv.tv_sec = duration_ms / 1000;
129887cfaf0eSJustin T. Gibbs 	duration_tv.tv_usec = (duration_ms % 1000) * 1000;
129987cfaf0eSJustin T. Gibbs 	if (timevalcmp(&delta, &duration_tv, <)) {
130087cfaf0eSJustin T. Gibbs 		timevalsub(&duration_tv, &delta);
130187cfaf0eSJustin T. Gibbs 
130287cfaf0eSJustin T. Gibbs 		duration_ms = duration_tv.tv_sec * 1000;
130387cfaf0eSJustin T. Gibbs 		duration_ms += duration_tv.tv_usec / 1000;
130487cfaf0eSJustin T. Gibbs 		cam_freeze_devq(periph->path);
130587cfaf0eSJustin T. Gibbs 		cam_release_devq(periph->path,
130687cfaf0eSJustin T. Gibbs 				RELSIM_RELEASE_AFTER_TIMEOUT,
130787cfaf0eSJustin T. Gibbs 				/*reduction*/0,
130887cfaf0eSJustin T. Gibbs 				/*timeout*/duration_ms,
130987cfaf0eSJustin T. Gibbs 				/*getcount_only*/0);
131087cfaf0eSJustin T. Gibbs 	}
131187cfaf0eSJustin T. Gibbs 
131287cfaf0eSJustin T. Gibbs }
131387cfaf0eSJustin T. Gibbs 
13143393f8daSKenneth D. Merry static int
13150191d9b3SAlexander Motin camperiphscsistatuserror(union ccb *ccb, union ccb **orig_ccb,
13160191d9b3SAlexander Motin     cam_flags camflags, u_int32_t sense_flags,
13173393f8daSKenneth D. Merry     int *openings, u_int32_t *relsim_flags,
13180181d54bSAlexander Motin     u_int32_t *timeout, u_int32_t *action, const char **action_string)
13198b8a9b1dSJustin T. Gibbs {
13208b8a9b1dSJustin T. Gibbs 	int error;
13218b8a9b1dSJustin T. Gibbs 
13228b8a9b1dSJustin T. Gibbs 	switch (ccb->csio.scsi_status) {
13238b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_OK:
13248b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_COND_MET:
13258b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_INTERMED:
13268b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_INTERMED_COND_MET:
13278b8a9b1dSJustin T. Gibbs 		error = 0;
13288b8a9b1dSJustin T. Gibbs 		break;
13298b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_CMD_TERMINATED:
13308b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_CHECK_COND:
13310191d9b3SAlexander Motin 		error = camperiphscsisenseerror(ccb, orig_ccb,
13323393f8daSKenneth D. Merry 					        camflags,
13338b8a9b1dSJustin T. Gibbs 					        sense_flags,
13343393f8daSKenneth D. Merry 					        openings,
13353393f8daSKenneth D. Merry 					        relsim_flags,
133683c5d981SAlexander Motin 					        timeout,
13370181d54bSAlexander Motin 					        action,
133883c5d981SAlexander Motin 					        action_string);
13398b8a9b1dSJustin T. Gibbs 		break;
13408b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_QUEUE_FULL:
13418b8a9b1dSJustin T. Gibbs 	{
13428b8a9b1dSJustin T. Gibbs 		/* no decrement */
134382815562SJustin T. Gibbs 		struct ccb_getdevstats cgds;
13448b8a9b1dSJustin T. Gibbs 
13458b8a9b1dSJustin T. Gibbs 		/*
13468b8a9b1dSJustin T. Gibbs 		 * First off, find out what the current
13478b8a9b1dSJustin T. Gibbs 		 * transaction counts are.
13488b8a9b1dSJustin T. Gibbs 		 */
134982815562SJustin T. Gibbs 		xpt_setup_ccb(&cgds.ccb_h,
13508b8a9b1dSJustin T. Gibbs 			      ccb->ccb_h.path,
1351bbfa4aa1SAlexander Motin 			      CAM_PRIORITY_NORMAL);
135282815562SJustin T. Gibbs 		cgds.ccb_h.func_code = XPT_GDEV_STATS;
135382815562SJustin T. Gibbs 		xpt_action((union ccb *)&cgds);
13548b8a9b1dSJustin T. Gibbs 
13558b8a9b1dSJustin T. Gibbs 		/*
13568b8a9b1dSJustin T. Gibbs 		 * If we were the only transaction active, treat
13578b8a9b1dSJustin T. Gibbs 		 * the QUEUE FULL as if it were a BUSY condition.
13588b8a9b1dSJustin T. Gibbs 		 */
135982815562SJustin T. Gibbs 		if (cgds.dev_active != 0) {
136082815562SJustin T. Gibbs 			int total_openings;
136182815562SJustin T. Gibbs 
13628b8a9b1dSJustin T. Gibbs 			/*
13638b8a9b1dSJustin T. Gibbs 		 	 * Reduce the number of openings to
13648b8a9b1dSJustin T. Gibbs 			 * be 1 less than the amount it took
13658b8a9b1dSJustin T. Gibbs 			 * to get a queue full bounded by the
13668b8a9b1dSJustin T. Gibbs 			 * minimum allowed tag count for this
13678b8a9b1dSJustin T. Gibbs 			 * device.
13688b8a9b1dSJustin T. Gibbs 		 	 */
13693393f8daSKenneth D. Merry 			total_openings = cgds.dev_active + cgds.dev_openings;
13703393f8daSKenneth D. Merry 			*openings = cgds.dev_active;
13713393f8daSKenneth D. Merry 			if (*openings < cgds.mintags)
13723393f8daSKenneth D. Merry 				*openings = cgds.mintags;
13733393f8daSKenneth D. Merry 			if (*openings < total_openings)
13743393f8daSKenneth D. Merry 				*relsim_flags = RELSIM_ADJUST_OPENINGS;
13758b8a9b1dSJustin T. Gibbs 			else {
13768b8a9b1dSJustin T. Gibbs 				/*
13778b8a9b1dSJustin T. Gibbs 				 * Some devices report queue full for
13788b8a9b1dSJustin T. Gibbs 				 * temporary resource shortages.  For
13798b8a9b1dSJustin T. Gibbs 				 * this reason, we allow a minimum
13808b8a9b1dSJustin T. Gibbs 				 * tag count to be entered via a
13818b8a9b1dSJustin T. Gibbs 				 * quirk entry to prevent the queue
13828b8a9b1dSJustin T. Gibbs 				 * count on these devices from falling
13838b8a9b1dSJustin T. Gibbs 				 * to a pessimisticly low value.  We
13848b8a9b1dSJustin T. Gibbs 				 * still wait for the next successful
13858b8a9b1dSJustin T. Gibbs 				 * completion, however, before queueing
13868b8a9b1dSJustin T. Gibbs 				 * more transactions to the device.
13878b8a9b1dSJustin T. Gibbs 				 */
13883393f8daSKenneth D. Merry 				*relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
13898b8a9b1dSJustin T. Gibbs 			}
13903393f8daSKenneth D. Merry 			*timeout = 0;
13918b8a9b1dSJustin T. Gibbs 			error = ERESTART;
13920181d54bSAlexander Motin 			*action &= ~SSQ_PRINT_SENSE;
13938b8a9b1dSJustin T. Gibbs 			break;
13948b8a9b1dSJustin T. Gibbs 		}
13958b8a9b1dSJustin T. Gibbs 		/* FALLTHROUGH */
13968b8a9b1dSJustin T. Gibbs 	}
13978b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_BUSY:
13988b8a9b1dSJustin T. Gibbs 		/*
13998b8a9b1dSJustin T. Gibbs 		 * Restart the queue after either another
14008b8a9b1dSJustin T. Gibbs 		 * command completes or a 1 second timeout.
14018b8a9b1dSJustin T. Gibbs 		 */
1402174b32ceSAlexander Motin 		if ((sense_flags & SF_RETRY_BUSY) != 0 ||
1403174b32ceSAlexander Motin 		    (ccb->ccb_h.retry_count--) > 0) {
14048b8a9b1dSJustin T. Gibbs 			error = ERESTART;
14053393f8daSKenneth D. Merry 			*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
14068b8a9b1dSJustin T. Gibbs 				      | RELSIM_RELEASE_AFTER_CMDCMPLT;
14073393f8daSKenneth D. Merry 			*timeout = 1000;
1408af51b059SMatt Jacob 		} else {
1409af51b059SMatt Jacob 			error = EIO;
1410af51b059SMatt Jacob 		}
14118b8a9b1dSJustin T. Gibbs 		break;
14128b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_RESERV_CONFLICT:
14138b8a9b1dSJustin T. Gibbs 	default:
14148b8a9b1dSJustin T. Gibbs 		error = EIO;
14158b8a9b1dSJustin T. Gibbs 		break;
14168b8a9b1dSJustin T. Gibbs 	}
14173393f8daSKenneth D. Merry 	return (error);
14183393f8daSKenneth D. Merry }
14193393f8daSKenneth D. Merry 
14203393f8daSKenneth D. Merry static int
14210191d9b3SAlexander Motin camperiphscsisenseerror(union ccb *ccb, union ccb **orig,
14220191d9b3SAlexander Motin     cam_flags camflags, u_int32_t sense_flags,
14233393f8daSKenneth D. Merry     int *openings, u_int32_t *relsim_flags,
14240181d54bSAlexander Motin     u_int32_t *timeout, u_int32_t *action, const char **action_string)
14253393f8daSKenneth D. Merry {
14263393f8daSKenneth D. Merry 	struct cam_periph *periph;
142783c5d981SAlexander Motin 	union ccb *orig_ccb = ccb;
14280191d9b3SAlexander Motin 	int error, recoveryccb;
14293393f8daSKenneth D. Merry 
14303393f8daSKenneth D. Merry 	periph = xpt_path_periph(ccb->ccb_h.path);
14310191d9b3SAlexander Motin 	recoveryccb = (ccb->ccb_h.cbfcnp == camperiphdone);
14320191d9b3SAlexander Motin 	if ((periph->flags & CAM_PERIPH_RECOVERY_INPROG) && !recoveryccb) {
14333393f8daSKenneth D. Merry 		/*
14343393f8daSKenneth D. Merry 		 * If error recovery is already in progress, don't attempt
14353393f8daSKenneth D. Merry 		 * to process this error, but requeue it unconditionally
14363393f8daSKenneth D. Merry 		 * and attempt to process it once error recovery has
14373393f8daSKenneth D. Merry 		 * completed.  This failed command is probably related to
14383393f8daSKenneth D. Merry 		 * the error that caused the currently active error recovery
14393393f8daSKenneth D. Merry 		 * action so our  current recovery efforts should also
14403393f8daSKenneth D. Merry 		 * address this command.  Be aware that the error recovery
14413393f8daSKenneth D. Merry 		 * code assumes that only one recovery action is in progress
14423393f8daSKenneth D. Merry 		 * on a particular peripheral instance at any given time
14433393f8daSKenneth D. Merry 		 * (e.g. only one saved CCB for error recovery) so it is
14443393f8daSKenneth D. Merry 		 * imperitive that we don't violate this assumption.
14453393f8daSKenneth D. Merry 		 */
14463393f8daSKenneth D. Merry 		error = ERESTART;
14470181d54bSAlexander Motin 		*action &= ~SSQ_PRINT_SENSE;
14483393f8daSKenneth D. Merry 	} else {
14493393f8daSKenneth D. Merry 		scsi_sense_action err_action;
14503393f8daSKenneth D. Merry 		struct ccb_getdev cgd;
14513393f8daSKenneth D. Merry 
14523393f8daSKenneth D. Merry 		/*
14533393f8daSKenneth D. Merry 		 * Grab the inquiry data for this device.
14543393f8daSKenneth D. Merry 		 */
1455bbfa4aa1SAlexander Motin 		xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, CAM_PRIORITY_NORMAL);
14563393f8daSKenneth D. Merry 		cgd.ccb_h.func_code = XPT_GDEV_TYPE;
14573393f8daSKenneth D. Merry 		xpt_action((union ccb *)&cgd);
14583393f8daSKenneth D. Merry 
1459e7493b28SAlexander Motin 		err_action = scsi_error_action(&ccb->csio, &cgd.inq_data,
14603393f8daSKenneth D. Merry 		    sense_flags);
14613393f8daSKenneth D. Merry 		error = err_action & SS_ERRMASK;
14623393f8daSKenneth D. Merry 
14633393f8daSKenneth D. Merry 		/*
14640191d9b3SAlexander Motin 		 * Do not autostart sequential access devices
14650191d9b3SAlexander Motin 		 * to avoid unexpected tape loading.
14660191d9b3SAlexander Motin 		 */
14670191d9b3SAlexander Motin 		if ((err_action & SS_MASK) == SS_START &&
14680191d9b3SAlexander Motin 		    SID_TYPE(&cgd.inq_data) == T_SEQUENTIAL) {
14690191d9b3SAlexander Motin 			*action_string = "Will not autostart a "
14700191d9b3SAlexander Motin 			    "sequential access device";
14710191d9b3SAlexander Motin 			goto sense_error_done;
14720191d9b3SAlexander Motin 		}
14730191d9b3SAlexander Motin 
14740191d9b3SAlexander Motin 		/*
14750191d9b3SAlexander Motin 		 * Avoid recovery recursion if recovery action is the same.
14760191d9b3SAlexander Motin 		 */
14770191d9b3SAlexander Motin 		if ((err_action & SS_MASK) >= SS_START && recoveryccb) {
14780191d9b3SAlexander Motin 			if (((err_action & SS_MASK) == SS_START &&
14790191d9b3SAlexander Motin 			     ccb->csio.cdb_io.cdb_bytes[0] == START_STOP_UNIT) ||
14800191d9b3SAlexander Motin 			    ((err_action & SS_MASK) == SS_TUR &&
14810191d9b3SAlexander Motin 			     (ccb->csio.cdb_io.cdb_bytes[0] == TEST_UNIT_READY))) {
14820191d9b3SAlexander Motin 				err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
14830191d9b3SAlexander Motin 				*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
14840191d9b3SAlexander Motin 				*timeout = 500;
14850191d9b3SAlexander Motin 			}
14860191d9b3SAlexander Motin 		}
14870191d9b3SAlexander Motin 
14880191d9b3SAlexander Motin 		/*
14893393f8daSKenneth D. Merry 		 * If the recovery action will consume a retry,
14903393f8daSKenneth D. Merry 		 * make sure we actually have retries available.
14913393f8daSKenneth D. Merry 		 */
14923393f8daSKenneth D. Merry 		if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
1493a07e846bSAlexander Motin 		 	if (ccb->ccb_h.retry_count > 0 &&
1494a07e846bSAlexander Motin 			    (periph->flags & CAM_PERIPH_INVALID) == 0)
14953393f8daSKenneth D. Merry 		 		ccb->ccb_h.retry_count--;
14963393f8daSKenneth D. Merry 			else {
149783c5d981SAlexander Motin 				*action_string = "Retries exhausted";
14983393f8daSKenneth D. Merry 				goto sense_error_done;
14993393f8daSKenneth D. Merry 			}
15003393f8daSKenneth D. Merry 		}
15013393f8daSKenneth D. Merry 
15023393f8daSKenneth D. Merry 		if ((err_action & SS_MASK) >= SS_START) {
15033393f8daSKenneth D. Merry 			/*
15043393f8daSKenneth D. Merry 			 * Do common portions of commands that
15053393f8daSKenneth D. Merry 			 * use recovery CCBs.
15063393f8daSKenneth D. Merry 			 */
150783c5d981SAlexander Motin 			orig_ccb = xpt_alloc_ccb_nowait();
150883c5d981SAlexander Motin 			if (orig_ccb == NULL) {
150983c5d981SAlexander Motin 				*action_string = "Can't allocate recovery CCB";
15103393f8daSKenneth D. Merry 				goto sense_error_done;
15113393f8daSKenneth D. Merry 			}
15121f1158b2SAlexander Motin 			/*
15131f1158b2SAlexander Motin 			 * Clear freeze flag for original request here, as
15141f1158b2SAlexander Motin 			 * this freeze will be dropped as part of ERESTART.
15151f1158b2SAlexander Motin 			 */
15161f1158b2SAlexander Motin 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
151783c5d981SAlexander Motin 			bcopy(ccb, orig_ccb, sizeof(*orig_ccb));
15183393f8daSKenneth D. Merry 		}
15193393f8daSKenneth D. Merry 
15203393f8daSKenneth D. Merry 		switch (err_action & SS_MASK) {
15213393f8daSKenneth D. Merry 		case SS_NOP:
152283c5d981SAlexander Motin 			*action_string = "No recovery action needed";
152300e54d14SKenneth D. Merry 			error = 0;
152400e54d14SKenneth D. Merry 			break;
15253393f8daSKenneth D. Merry 		case SS_RETRY:
152683c5d981SAlexander Motin 			*action_string = "Retrying command (per sense data)";
15273393f8daSKenneth D. Merry 			error = ERESTART;
15288b8a9b1dSJustin T. Gibbs 			break;
15293393f8daSKenneth D. Merry 		case SS_FAIL:
153083c5d981SAlexander Motin 			*action_string = "Unretryable error";
15313393f8daSKenneth D. Merry 			break;
15323393f8daSKenneth D. Merry 		case SS_START:
15333393f8daSKenneth D. Merry 		{
15343393f8daSKenneth D. Merry 			int le;
15353393f8daSKenneth D. Merry 
15363393f8daSKenneth D. Merry 			/*
15373393f8daSKenneth D. Merry 			 * Send a start unit command to the device, and
15383393f8daSKenneth D. Merry 			 * then retry the command.
15393393f8daSKenneth D. Merry 			 */
154083c5d981SAlexander Motin 			*action_string = "Attempting to start unit";
154183c5d981SAlexander Motin 			periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
15423393f8daSKenneth D. Merry 
15433393f8daSKenneth D. Merry 			/*
15443393f8daSKenneth D. Merry 			 * Check for removable media and set
15453393f8daSKenneth D. Merry 			 * load/eject flag appropriately.
15463393f8daSKenneth D. Merry 			 */
15473393f8daSKenneth D. Merry 			if (SID_IS_REMOVABLE(&cgd.inq_data))
15483393f8daSKenneth D. Merry 				le = TRUE;
15493393f8daSKenneth D. Merry 			else
15503393f8daSKenneth D. Merry 				le = FALSE;
15513393f8daSKenneth D. Merry 
15523393f8daSKenneth D. Merry 			scsi_start_stop(&ccb->csio,
15533393f8daSKenneth D. Merry 					/*retries*/1,
15543393f8daSKenneth D. Merry 					camperiphdone,
15553393f8daSKenneth D. Merry 					MSG_SIMPLE_Q_TAG,
15563393f8daSKenneth D. Merry 					/*start*/TRUE,
15573393f8daSKenneth D. Merry 					/*load/eject*/le,
15583393f8daSKenneth D. Merry 					/*immediate*/FALSE,
15593393f8daSKenneth D. Merry 					SSD_FULL_SIZE,
15603393f8daSKenneth D. Merry 					/*timeout*/50000);
15613393f8daSKenneth D. Merry 			break;
15623393f8daSKenneth D. Merry 		}
15633393f8daSKenneth D. Merry 		case SS_TUR:
15643393f8daSKenneth D. Merry 		{
15653393f8daSKenneth D. Merry 			/*
15663393f8daSKenneth D. Merry 			 * Send a Test Unit Ready to the device.
15673393f8daSKenneth D. Merry 			 * If the 'many' flag is set, we send 120
15683393f8daSKenneth D. Merry 			 * test unit ready commands, one every half
15693393f8daSKenneth D. Merry 			 * second.  Otherwise, we just send one TUR.
15703393f8daSKenneth D. Merry 			 * We only want to do this if the retry
15713393f8daSKenneth D. Merry 			 * count has not been exhausted.
15723393f8daSKenneth D. Merry 			 */
15733393f8daSKenneth D. Merry 			int retries;
15743393f8daSKenneth D. Merry 
15753393f8daSKenneth D. Merry 			if ((err_action & SSQ_MANY) != 0) {
157683c5d981SAlexander Motin 				*action_string = "Polling device for readiness";
15773393f8daSKenneth D. Merry 				retries = 120;
15783393f8daSKenneth D. Merry 			} else {
157983c5d981SAlexander Motin 				*action_string = "Testing device for readiness";
15803393f8daSKenneth D. Merry 				retries = 1;
15813393f8daSKenneth D. Merry 			}
158283c5d981SAlexander Motin 			periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
15833393f8daSKenneth D. Merry 			scsi_test_unit_ready(&ccb->csio,
15843393f8daSKenneth D. Merry 					     retries,
15853393f8daSKenneth D. Merry 					     camperiphdone,
15863393f8daSKenneth D. Merry 					     MSG_SIMPLE_Q_TAG,
15873393f8daSKenneth D. Merry 					     SSD_FULL_SIZE,
15883393f8daSKenneth D. Merry 					     /*timeout*/5000);
15893393f8daSKenneth D. Merry 
15903393f8daSKenneth D. Merry 			/*
15913393f8daSKenneth D. Merry 			 * Accomplish our 500ms delay by deferring
15923393f8daSKenneth D. Merry 			 * the release of our device queue appropriately.
15933393f8daSKenneth D. Merry 			 */
15943393f8daSKenneth D. Merry 			*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
15953393f8daSKenneth D. Merry 			*timeout = 500;
15963393f8daSKenneth D. Merry 			break;
15973393f8daSKenneth D. Merry 		}
15983393f8daSKenneth D. Merry 		default:
1599e3c29144SWarner Losh 			panic("Unhandled error action %x", err_action);
16003393f8daSKenneth D. Merry 		}
16013393f8daSKenneth D. Merry 
16023393f8daSKenneth D. Merry 		if ((err_action & SS_MASK) >= SS_START) {
16033393f8daSKenneth D. Merry 			/*
1604bbfa4aa1SAlexander Motin 			 * Drop the priority, so that the recovery
16053393f8daSKenneth D. Merry 			 * CCB is the first to execute.  Freeze the queue
16063393f8daSKenneth D. Merry 			 * after this command is sent so that we can
16073393f8daSKenneth D. Merry 			 * restore the old csio and have it queued in
16083393f8daSKenneth D. Merry 			 * the proper order before we release normal
16093393f8daSKenneth D. Merry 			 * transactions to the device.
16103393f8daSKenneth D. Merry 			 */
161183c5d981SAlexander Motin 			ccb->ccb_h.pinfo.priority--;
16123393f8daSKenneth D. Merry 			ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
161383c5d981SAlexander Motin 			ccb->ccb_h.saved_ccb_ptr = orig_ccb;
16143393f8daSKenneth D. Merry 			error = ERESTART;
16150191d9b3SAlexander Motin 			*orig = orig_ccb;
16163393f8daSKenneth D. Merry 		}
16173393f8daSKenneth D. Merry 
16183393f8daSKenneth D. Merry sense_error_done:
16190181d54bSAlexander Motin 		*action = err_action;
16203393f8daSKenneth D. Merry 	}
16213393f8daSKenneth D. Merry 	return (error);
16223393f8daSKenneth D. Merry }
16233393f8daSKenneth D. Merry 
16243393f8daSKenneth D. Merry /*
16253393f8daSKenneth D. Merry  * Generic error handler.  Peripheral drivers usually filter
16261ffe5851SPedro F. Giffuni  * out the errors that they handle in a unique manner, then
16273393f8daSKenneth D. Merry  * call this function.
16283393f8daSKenneth D. Merry  */
16293393f8daSKenneth D. Merry int
16303393f8daSKenneth D. Merry cam_periph_error(union ccb *ccb, cam_flags camflags,
16313393f8daSKenneth D. Merry 		 u_int32_t sense_flags, union ccb *save_ccb)
16323393f8daSKenneth D. Merry {
16330181d54bSAlexander Motin 	struct cam_path *newpath;
16340181d54bSAlexander Motin 	union ccb  *orig_ccb, *scan_ccb;
1635a07e846bSAlexander Motin 	struct cam_periph *periph;
16363393f8daSKenneth D. Merry 	const char *action_string;
16373393f8daSKenneth D. Merry 	cam_status  status;
1638a30ecd42SScott Long 	int	    frozen, error, openings, devctl_err;
16390181d54bSAlexander Motin 	u_int32_t   action, relsim_flags, timeout;
16403393f8daSKenneth D. Merry 
16410181d54bSAlexander Motin 	action = SSQ_PRINT_SENSE;
1642a07e846bSAlexander Motin 	periph = xpt_path_periph(ccb->ccb_h.path);
16433393f8daSKenneth D. Merry 	action_string = NULL;
16443393f8daSKenneth D. Merry 	status = ccb->ccb_h.status;
16453393f8daSKenneth D. Merry 	frozen = (status & CAM_DEV_QFRZN) != 0;
16463393f8daSKenneth D. Merry 	status &= CAM_STATUS_MASK;
1647a30ecd42SScott Long 	devctl_err = openings = relsim_flags = timeout = 0;
16480191d9b3SAlexander Motin 	orig_ccb = ccb;
16493393f8daSKenneth D. Merry 
1650a30ecd42SScott Long 	/* Filter the errors that should be reported via devctl */
1651a30ecd42SScott Long 	switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
1652a30ecd42SScott Long 	case CAM_CMD_TIMEOUT:
1653a30ecd42SScott Long 	case CAM_REQ_ABORTED:
1654a30ecd42SScott Long 	case CAM_REQ_CMP_ERR:
1655a30ecd42SScott Long 	case CAM_REQ_TERMIO:
1656a30ecd42SScott Long 	case CAM_UNREC_HBA_ERROR:
1657a30ecd42SScott Long 	case CAM_DATA_RUN_ERR:
1658a30ecd42SScott Long 	case CAM_SCSI_STATUS_ERROR:
1659a30ecd42SScott Long 	case CAM_ATA_STATUS_ERROR:
1660a30ecd42SScott Long 	case CAM_SMP_STATUS_ERROR:
1661a30ecd42SScott Long 		devctl_err++;
1662a30ecd42SScott Long 		break;
1663a30ecd42SScott Long 	default:
1664a30ecd42SScott Long 		break;
1665a30ecd42SScott Long 	}
1666a30ecd42SScott Long 
16673393f8daSKenneth D. Merry 	switch (status) {
16683393f8daSKenneth D. Merry 	case CAM_REQ_CMP:
16693393f8daSKenneth D. Merry 		error = 0;
16700181d54bSAlexander Motin 		action &= ~SSQ_PRINT_SENSE;
16713393f8daSKenneth D. Merry 		break;
16723393f8daSKenneth D. Merry 	case CAM_SCSI_STATUS_ERROR:
16730191d9b3SAlexander Motin 		error = camperiphscsistatuserror(ccb, &orig_ccb,
16740191d9b3SAlexander Motin 		    camflags, sense_flags, &openings, &relsim_flags,
16750181d54bSAlexander Motin 		    &timeout, &action, &action_string);
16763393f8daSKenneth D. Merry 		break;
16773393f8daSKenneth D. Merry 	case CAM_AUTOSENSE_FAIL:
1678c7ec4390SMatt Jacob 		error = EIO;	/* we have to kill the command */
1679c7ec4390SMatt Jacob 		break;
16808b8a9b1dSJustin T. Gibbs 	case CAM_UA_ABORT:
16818b8a9b1dSJustin T. Gibbs 	case CAM_UA_TERMIO:
16828b8a9b1dSJustin T. Gibbs 	case CAM_MSG_REJECT_REC:
16838b8a9b1dSJustin T. Gibbs 		/* XXX Don't know that these are correct */
16848b8a9b1dSJustin T. Gibbs 		error = EIO;
16858b8a9b1dSJustin T. Gibbs 		break;
16868b8a9b1dSJustin T. Gibbs 	case CAM_SEL_TIMEOUT:
16873393f8daSKenneth D. Merry 		if ((camflags & CAM_RETRY_SELTO) != 0) {
1688a07e846bSAlexander Motin 			if (ccb->ccb_h.retry_count > 0 &&
1689a07e846bSAlexander Motin 			    (periph->flags & CAM_PERIPH_INVALID) == 0) {
16903393f8daSKenneth D. Merry 				ccb->ccb_h.retry_count--;
16913393f8daSKenneth D. Merry 				error = ERESTART;
16923393f8daSKenneth D. Merry 
16933393f8daSKenneth D. Merry 				/*
169473cf209fSMatt Jacob 				 * Wait a bit to give the device
16953393f8daSKenneth D. Merry 				 * time to recover before we try again.
16963393f8daSKenneth D. Merry 				 */
16973393f8daSKenneth D. Merry 				relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
169873cf209fSMatt Jacob 				timeout = periph_selto_delay;
16993393f8daSKenneth D. Merry 				break;
17003393f8daSKenneth D. Merry 			}
1701a07e846bSAlexander Motin 			action_string = "Retries exhausted";
17023393f8daSKenneth D. Merry 		}
17038900f4b8SKenneth D. Merry 		/* FALLTHROUGH */
17048900f4b8SKenneth D. Merry 	case CAM_DEV_NOT_THERE:
17053393f8daSKenneth D. Merry 		error = ENXIO;
17060181d54bSAlexander Motin 		action = SSQ_LOST;
17078b8a9b1dSJustin T. Gibbs 		break;
17088b8a9b1dSJustin T. Gibbs 	case CAM_REQ_INVALID:
17098b8a9b1dSJustin T. Gibbs 	case CAM_PATH_INVALID:
17108b8a9b1dSJustin T. Gibbs 	case CAM_NO_HBA:
17118b8a9b1dSJustin T. Gibbs 	case CAM_PROVIDE_FAIL:
17128b8a9b1dSJustin T. Gibbs 	case CAM_REQ_TOO_BIG:
17139a014e6fSIan Dowse 	case CAM_LUN_INVALID:
17149a014e6fSIan Dowse 	case CAM_TID_INVALID:
1715357478a5SAlexander Motin 	case CAM_FUNC_NOTAVAIL:
17168b8a9b1dSJustin T. Gibbs 		error = EINVAL;
17178b8a9b1dSJustin T. Gibbs 		break;
17188b8a9b1dSJustin T. Gibbs 	case CAM_SCSI_BUS_RESET:
17198b8a9b1dSJustin T. Gibbs 	case CAM_BDR_SENT:
17203393f8daSKenneth D. Merry 		/*
17213393f8daSKenneth D. Merry 		 * Commands that repeatedly timeout and cause these
17223393f8daSKenneth D. Merry 		 * kinds of error recovery actions, should return
17233393f8daSKenneth D. Merry 		 * CAM_CMD_TIMEOUT, which allows us to safely assume
17243393f8daSKenneth D. Merry 		 * that this command was an innocent bystander to
17253393f8daSKenneth D. Merry 		 * these events and should be unconditionally
17263393f8daSKenneth D. Merry 		 * retried.
17273393f8daSKenneth D. Merry 		 */
17288b8a9b1dSJustin T. Gibbs 	case CAM_REQUEUE_REQ:
17290191d9b3SAlexander Motin 		/* Unconditional requeue if device is still there */
17300191d9b3SAlexander Motin 		if (periph->flags & CAM_PERIPH_INVALID) {
17310191d9b3SAlexander Motin 			action_string = "Periph was invalidated";
1732a07e846bSAlexander Motin 			error = EIO;
17330191d9b3SAlexander Motin 		} else if (sense_flags & SF_NO_RETRY) {
17340191d9b3SAlexander Motin 			error = EIO;
17350191d9b3SAlexander Motin 			action_string = "Retry was blocked";
173684c4de2dSAlexander Motin 		} else {
17370191d9b3SAlexander Motin 			error = ERESTART;
17380181d54bSAlexander Motin 			action &= ~SSQ_PRINT_SENSE;
173984c4de2dSAlexander Motin 		}
17408b8a9b1dSJustin T. Gibbs 		break;
17418b8a9b1dSJustin T. Gibbs 	case CAM_RESRC_UNAVAIL:
174273cf209fSMatt Jacob 		/* Wait a bit for the resource shortage to abate. */
174373cf209fSMatt Jacob 		timeout = periph_noresrc_delay;
174473cf209fSMatt Jacob 		/* FALLTHROUGH */
17458b8a9b1dSJustin T. Gibbs 	case CAM_BUSY:
174673cf209fSMatt Jacob 		if (timeout == 0) {
174773cf209fSMatt Jacob 			/* Wait a bit for the busy condition to abate. */
174873cf209fSMatt Jacob 			timeout = periph_busy_delay;
174973cf209fSMatt Jacob 		}
175073cf209fSMatt Jacob 		relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
175173cf209fSMatt Jacob 		/* FALLTHROUGH */
17520191d9b3SAlexander Motin 	case CAM_ATA_STATUS_ERROR:
17530191d9b3SAlexander Motin 	case CAM_REQ_CMP_ERR:
17540191d9b3SAlexander Motin 	case CAM_CMD_TIMEOUT:
17550191d9b3SAlexander Motin 	case CAM_UNEXP_BUSFREE:
17560191d9b3SAlexander Motin 	case CAM_UNCOR_PARITY:
17570191d9b3SAlexander Motin 	case CAM_DATA_RUN_ERR:
17588b8a9b1dSJustin T. Gibbs 	default:
17590191d9b3SAlexander Motin 		if (periph->flags & CAM_PERIPH_INVALID) {
17600191d9b3SAlexander Motin 			error = EIO;
17610191d9b3SAlexander Motin 			action_string = "Periph was invalidated";
17620191d9b3SAlexander Motin 		} else if (ccb->ccb_h.retry_count == 0) {
17638b8a9b1dSJustin T. Gibbs 			error = EIO;
176483c5d981SAlexander Motin 			action_string = "Retries exhausted";
17650191d9b3SAlexander Motin 		} else if (sense_flags & SF_NO_RETRY) {
17660191d9b3SAlexander Motin 			error = EIO;
17670191d9b3SAlexander Motin 			action_string = "Retry was blocked";
17680191d9b3SAlexander Motin 		} else {
17690191d9b3SAlexander Motin 			ccb->ccb_h.retry_count--;
17700191d9b3SAlexander Motin 			error = ERESTART;
17718b8a9b1dSJustin T. Gibbs 		}
17728b8a9b1dSJustin T. Gibbs 		break;
17738b8a9b1dSJustin T. Gibbs 	}
17748b8a9b1dSJustin T. Gibbs 
17750191d9b3SAlexander Motin 	if ((sense_flags & SF_PRINT_ALWAYS) ||
17760191d9b3SAlexander Motin 	    CAM_DEBUGGED(ccb->ccb_h.path, CAM_DEBUG_INFO))
17770181d54bSAlexander Motin 		action |= SSQ_PRINT_SENSE;
17780191d9b3SAlexander Motin 	else if (sense_flags & SF_NO_PRINT)
17790181d54bSAlexander Motin 		action &= ~SSQ_PRINT_SENSE;
17800181d54bSAlexander Motin 	if ((action & SSQ_PRINT_SENSE) != 0)
17810191d9b3SAlexander Motin 		cam_error_print(orig_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
17820181d54bSAlexander Motin 	if (error != 0 && (action & SSQ_PRINT_SENSE) != 0) {
17830f3e2159SAlexander Motin 		if (error != ERESTART) {
17843393f8daSKenneth D. Merry 			if (action_string == NULL)
178583c5d981SAlexander Motin 				action_string = "Unretryable error";
178683c5d981SAlexander Motin 			xpt_print(ccb->ccb_h.path, "Error %d, %s\n",
178783c5d981SAlexander Motin 			    error, action_string);
178883c5d981SAlexander Motin 		} else if (action_string != NULL)
1789f0d9af51SMatt Jacob 			xpt_print(ccb->ccb_h.path, "%s\n", action_string);
179083c5d981SAlexander Motin 		else
179183c5d981SAlexander Motin 			xpt_print(ccb->ccb_h.path, "Retrying command\n");
17920f3e2159SAlexander Motin 	}
17930f3e2159SAlexander Motin 
1794cd375264SAlexander Motin 	if (devctl_err && (error != 0 || (action & SSQ_PRINT_SENSE) != 0))
1795a30ecd42SScott Long 		cam_periph_devctl_notify(orig_ccb);
1796a30ecd42SScott Long 
17970181d54bSAlexander Motin 	if ((action & SSQ_LOST) != 0) {
17980191d9b3SAlexander Motin 		lun_id_t lun_id;
17990191d9b3SAlexander Motin 
18000191d9b3SAlexander Motin 		/*
18010191d9b3SAlexander Motin 		 * For a selection timeout, we consider all of the LUNs on
18020191d9b3SAlexander Motin 		 * the target to be gone.  If the status is CAM_DEV_NOT_THERE,
18030191d9b3SAlexander Motin 		 * then we only get rid of the device(s) specified by the
18040191d9b3SAlexander Motin 		 * path in the original CCB.
18050191d9b3SAlexander Motin 		 */
18060181d54bSAlexander Motin 		if (status == CAM_SEL_TIMEOUT)
18070191d9b3SAlexander Motin 			lun_id = CAM_LUN_WILDCARD;
18080181d54bSAlexander Motin 		else
18090181d54bSAlexander Motin 			lun_id = xpt_path_lun_id(ccb->ccb_h.path);
18100191d9b3SAlexander Motin 
18110191d9b3SAlexander Motin 		/* Should we do more if we can't create the path?? */
18120191d9b3SAlexander Motin 		if (xpt_create_path(&newpath, periph,
18130191d9b3SAlexander Motin 				    xpt_path_path_id(ccb->ccb_h.path),
18140191d9b3SAlexander Motin 				    xpt_path_target_id(ccb->ccb_h.path),
18150191d9b3SAlexander Motin 				    lun_id) == CAM_REQ_CMP) {
18160191d9b3SAlexander Motin 
18170191d9b3SAlexander Motin 			/*
18180191d9b3SAlexander Motin 			 * Let peripheral drivers know that this
18190191d9b3SAlexander Motin 			 * device has gone away.
18200191d9b3SAlexander Motin 			 */
18210191d9b3SAlexander Motin 			xpt_async(AC_LOST_DEVICE, newpath, NULL);
18220191d9b3SAlexander Motin 			xpt_free_path(newpath);
18230191d9b3SAlexander Motin 		}
18240181d54bSAlexander Motin 	}
18253631c638SAlexander Motin 
18263631c638SAlexander Motin 	/* Broadcast UNIT ATTENTIONs to all periphs. */
18270181d54bSAlexander Motin 	if ((action & SSQ_UA) != 0)
18283631c638SAlexander Motin 		xpt_async(AC_UNIT_ATTENTION, orig_ccb->ccb_h.path, orig_ccb);
18290181d54bSAlexander Motin 
18300181d54bSAlexander Motin 	/* Rescan target on "Reported LUNs data has changed" */
18310181d54bSAlexander Motin 	if ((action & SSQ_RESCAN) != 0) {
18320181d54bSAlexander Motin 		if (xpt_create_path(&newpath, NULL,
18330181d54bSAlexander Motin 				    xpt_path_path_id(ccb->ccb_h.path),
18340181d54bSAlexander Motin 				    xpt_path_target_id(ccb->ccb_h.path),
1835e5736ac8SAlexander Motin 				    CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
18360181d54bSAlexander Motin 
18370181d54bSAlexander Motin 			scan_ccb = xpt_alloc_ccb_nowait();
1838e5736ac8SAlexander Motin 			if (scan_ccb != NULL) {
18390181d54bSAlexander Motin 				scan_ccb->ccb_h.path = newpath;
1840e5736ac8SAlexander Motin 				scan_ccb->ccb_h.func_code = XPT_SCAN_TGT;
18410181d54bSAlexander Motin 				scan_ccb->crcn.flags = 0;
18420181d54bSAlexander Motin 				xpt_rescan(scan_ccb);
18433231e8bdSAlexander Motin 			} else {
1844e5736ac8SAlexander Motin 				xpt_print(newpath,
1845e5736ac8SAlexander Motin 				    "Can't allocate CCB to rescan target\n");
18463231e8bdSAlexander Motin 				xpt_free_path(newpath);
18473231e8bdSAlexander Motin 			}
18480181d54bSAlexander Motin 		}
18490191d9b3SAlexander Motin 	}
18500191d9b3SAlexander Motin 
18510f3e2159SAlexander Motin 	/* Attempt a retry */
18520f3e2159SAlexander Motin 	if (error == ERESTART || error == 0) {
18530f3e2159SAlexander Motin 		if (frozen != 0)
18540f3e2159SAlexander Motin 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
18550f3e2159SAlexander Motin 		if (error == ERESTART)
18560f3e2159SAlexander Motin 			xpt_action(ccb);
18570f3e2159SAlexander Motin 		if (frozen != 0)
18580f3e2159SAlexander Motin 			cam_release_devq(ccb->ccb_h.path,
18590f3e2159SAlexander Motin 					 relsim_flags,
18600f3e2159SAlexander Motin 					 openings,
18610f3e2159SAlexander Motin 					 timeout,
18620f3e2159SAlexander Motin 					 /*getcount_only*/0);
18633393f8daSKenneth D. Merry 	}
18648b8a9b1dSJustin T. Gibbs 
18658b8a9b1dSJustin T. Gibbs 	return (error);
18668b8a9b1dSJustin T. Gibbs }
1867a30ecd42SScott Long 
1868a30ecd42SScott Long #define CAM_PERIPH_DEVD_MSG_SIZE	256
1869a30ecd42SScott Long 
1870a30ecd42SScott Long static void
1871a30ecd42SScott Long cam_periph_devctl_notify(union ccb *ccb)
1872a30ecd42SScott Long {
1873a30ecd42SScott Long 	struct cam_periph *periph;
1874a30ecd42SScott Long 	struct ccb_getdev *cgd;
1875a30ecd42SScott Long 	struct sbuf sb;
1876a30ecd42SScott Long 	int serr, sk, asc, ascq;
1877a30ecd42SScott Long 	char *sbmsg, *type;
1878a30ecd42SScott Long 
1879a30ecd42SScott Long 	sbmsg = malloc(CAM_PERIPH_DEVD_MSG_SIZE, M_CAMPERIPH, M_NOWAIT);
1880a30ecd42SScott Long 	if (sbmsg == NULL)
1881a30ecd42SScott Long 		return;
1882a30ecd42SScott Long 
1883a30ecd42SScott Long 	sbuf_new(&sb, sbmsg, CAM_PERIPH_DEVD_MSG_SIZE, SBUF_FIXEDLEN);
1884a30ecd42SScott Long 
1885a30ecd42SScott Long 	periph = xpt_path_periph(ccb->ccb_h.path);
1886a30ecd42SScott Long 	sbuf_printf(&sb, "device=%s%d ", periph->periph_name,
1887a30ecd42SScott Long 	    periph->unit_number);
1888a30ecd42SScott Long 
1889a30ecd42SScott Long 	sbuf_printf(&sb, "serial=\"");
1890a30ecd42SScott Long 	if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) != NULL) {
1891a30ecd42SScott Long 		xpt_setup_ccb(&cgd->ccb_h, ccb->ccb_h.path,
1892a30ecd42SScott Long 		    CAM_PRIORITY_NORMAL);
1893a30ecd42SScott Long 		cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1894a30ecd42SScott Long 		xpt_action((union ccb *)cgd);
1895a30ecd42SScott Long 
1896a30ecd42SScott Long 		if (cgd->ccb_h.status == CAM_REQ_CMP)
1897a30ecd42SScott Long 			sbuf_bcat(&sb, cgd->serial_num, cgd->serial_num_len);
1898de482e7bSScott Long 		xpt_free_ccb((union ccb *)cgd);
1899a30ecd42SScott Long 	}
1900a30ecd42SScott Long 	sbuf_printf(&sb, "\" ");
1901a30ecd42SScott Long 	sbuf_printf(&sb, "cam_status=\"0x%x\" ", ccb->ccb_h.status);
1902a30ecd42SScott Long 
1903a30ecd42SScott Long 	switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
1904a30ecd42SScott Long 	case CAM_CMD_TIMEOUT:
1905a30ecd42SScott Long 		sbuf_printf(&sb, "timeout=%d ", ccb->ccb_h.timeout);
1906a30ecd42SScott Long 		type = "timeout";
1907a30ecd42SScott Long 		break;
1908a30ecd42SScott Long 	case CAM_SCSI_STATUS_ERROR:
1909a30ecd42SScott Long 		sbuf_printf(&sb, "scsi_status=%d ", ccb->csio.scsi_status);
1910a30ecd42SScott Long 		if (scsi_extract_sense_ccb(ccb, &serr, &sk, &asc, &ascq))
1911a30ecd42SScott Long 			sbuf_printf(&sb, "scsi_sense=\"%02x %02x %02x %02x\" ",
1912a30ecd42SScott Long 			    serr, sk, asc, ascq);
1913a30ecd42SScott Long 		type = "error";
1914a30ecd42SScott Long 		break;
1915a30ecd42SScott Long 	case CAM_ATA_STATUS_ERROR:
1916a30ecd42SScott Long 		sbuf_printf(&sb, "RES=\"");
1917a30ecd42SScott Long 		ata_res_sbuf(&ccb->ataio.res, &sb);
1918a30ecd42SScott Long 		sbuf_printf(&sb, "\" ");
1919a30ecd42SScott Long 		type = "error";
1920a30ecd42SScott Long 		break;
1921a30ecd42SScott Long 	default:
1922a30ecd42SScott Long 		type = "error";
1923a30ecd42SScott Long 		break;
1924a30ecd42SScott Long 	}
1925a30ecd42SScott Long 
1926a30ecd42SScott Long 	if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1927a30ecd42SScott Long 		sbuf_printf(&sb, "CDB=\"");
1928a30ecd42SScott Long 		if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0)
1929a30ecd42SScott Long 			scsi_cdb_sbuf(ccb->csio.cdb_io.cdb_ptr, &sb);
1930a30ecd42SScott Long 		else
1931a30ecd42SScott Long 			scsi_cdb_sbuf(ccb->csio.cdb_io.cdb_bytes, &sb);
1932a30ecd42SScott Long 		sbuf_printf(&sb, "\" ");
1933a30ecd42SScott Long 	} else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1934a30ecd42SScott Long 		sbuf_printf(&sb, "ACB=\"");
1935a30ecd42SScott Long 		ata_cmd_sbuf(&ccb->ataio.cmd, &sb);
1936a30ecd42SScott Long 		sbuf_printf(&sb, "\" ");
1937a30ecd42SScott Long 	}
1938a30ecd42SScott Long 
1939a30ecd42SScott Long 	if (sbuf_finish(&sb) == 0)
1940a30ecd42SScott Long 		devctl_notify("CAM", "periph", type, sbuf_data(&sb));
1941a30ecd42SScott Long 	sbuf_delete(&sb);
1942a30ecd42SScott Long 	free(sbmsg, M_CAMPERIPH);
1943a30ecd42SScott Long }
1944a30ecd42SScott Long 
1945