xref: /freebsd/sys/cam/cam_periph.c (revision 8d36a71b76fb4a7da95ecaf21d2f3b37af3159bd)
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);
898b8a9b1dSJustin T. Gibbs 
900b7c27b9SPeter Wemm static int nperiph_drivers;
9183c5d981SAlexander Motin static int initialized = 0;
920b7c27b9SPeter Wemm struct periph_driver **periph_drivers;
930b7c27b9SPeter Wemm 
94d745c852SEd Schouten static MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers");
95362abc44STai-hwa Liang 
9673cf209fSMatt Jacob static int periph_selto_delay = 1000;
9773cf209fSMatt Jacob TUNABLE_INT("kern.cam.periph_selto_delay", &periph_selto_delay);
9873cf209fSMatt Jacob static int periph_noresrc_delay = 500;
9973cf209fSMatt Jacob TUNABLE_INT("kern.cam.periph_noresrc_delay", &periph_noresrc_delay);
10073cf209fSMatt Jacob static int periph_busy_delay = 500;
10173cf209fSMatt Jacob TUNABLE_INT("kern.cam.periph_busy_delay", &periph_busy_delay);
10273cf209fSMatt Jacob 
10373cf209fSMatt Jacob 
1040b7c27b9SPeter Wemm void
1050b7c27b9SPeter Wemm periphdriver_register(void *data)
1060b7c27b9SPeter Wemm {
10783c5d981SAlexander Motin 	struct periph_driver *drv = (struct periph_driver *)data;
1080b7c27b9SPeter Wemm 	struct periph_driver **newdrivers, **old;
1090b7c27b9SPeter Wemm 	int ndrivers;
1100b7c27b9SPeter Wemm 
1110b7c27b9SPeter Wemm 	ndrivers = nperiph_drivers + 2;
1120dd50e9bSScott Long 	newdrivers = malloc(sizeof(*newdrivers) * ndrivers, M_CAMPERIPH,
1130dd50e9bSScott Long 			    M_WAITOK);
1140b7c27b9SPeter Wemm 	if (periph_drivers)
1150b7c27b9SPeter Wemm 		bcopy(periph_drivers, newdrivers,
116623db360SKenneth D. Merry 		      sizeof(*newdrivers) * nperiph_drivers);
11783c5d981SAlexander Motin 	newdrivers[nperiph_drivers] = drv;
1180b7c27b9SPeter Wemm 	newdrivers[nperiph_drivers + 1] = NULL;
1190b7c27b9SPeter Wemm 	old = periph_drivers;
1200b7c27b9SPeter Wemm 	periph_drivers = newdrivers;
1210b7c27b9SPeter Wemm 	if (old)
1220dd50e9bSScott Long 		free(old, M_CAMPERIPH);
1230b7c27b9SPeter Wemm 	nperiph_drivers++;
12483c5d981SAlexander Motin 	/* If driver marked as early or it is late now, initialize it. */
12583c5d981SAlexander Motin 	if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
12683c5d981SAlexander Motin 	    initialized > 1)
12783c5d981SAlexander Motin 		(*drv->init)();
12883c5d981SAlexander Motin }
12983c5d981SAlexander Motin 
13083c5d981SAlexander Motin void
13183c5d981SAlexander Motin periphdriver_init(int level)
13283c5d981SAlexander Motin {
13383c5d981SAlexander Motin 	int	i, early;
13483c5d981SAlexander Motin 
13583c5d981SAlexander Motin 	initialized = max(initialized, level);
13683c5d981SAlexander Motin 	for (i = 0; periph_drivers[i] != NULL; i++) {
13783c5d981SAlexander Motin 		early = (periph_drivers[i]->flags & CAM_PERIPH_DRV_EARLY) ? 1 : 2;
13883c5d981SAlexander Motin 		if (early == initialized)
13983c5d981SAlexander Motin 			(*periph_drivers[i]->init)();
14083c5d981SAlexander Motin 	}
1410b7c27b9SPeter Wemm }
1420b7c27b9SPeter Wemm 
1438b8a9b1dSJustin T. Gibbs cam_status
144ee9c90c7SKenneth D. Merry cam_periph_alloc(periph_ctor_t *periph_ctor,
145ee9c90c7SKenneth D. Merry 		 periph_oninv_t *periph_oninvalidate,
146ee9c90c7SKenneth D. Merry 		 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
147ee9c90c7SKenneth D. Merry 		 char *name, cam_periph_type type, struct cam_path *path,
148ee9c90c7SKenneth D. Merry 		 ac_callback_t *ac_callback, ac_code code, void *arg)
1498b8a9b1dSJustin T. Gibbs {
1508b8a9b1dSJustin T. Gibbs 	struct		periph_driver **p_drv;
1512b83592fSScott Long 	struct		cam_sim *sim;
1528b8a9b1dSJustin T. Gibbs 	struct		cam_periph *periph;
1538b8a9b1dSJustin T. Gibbs 	struct		cam_periph *cur_periph;
1548b8a9b1dSJustin T. Gibbs 	path_id_t	path_id;
1558b8a9b1dSJustin T. Gibbs 	target_id_t	target_id;
1568b8a9b1dSJustin T. Gibbs 	lun_id_t	lun_id;
1578b8a9b1dSJustin T. Gibbs 	cam_status	status;
1588b8a9b1dSJustin T. Gibbs 	u_int		init_level;
1598b8a9b1dSJustin T. Gibbs 
1608b8a9b1dSJustin T. Gibbs 	init_level = 0;
1618b8a9b1dSJustin T. Gibbs 	/*
1628b8a9b1dSJustin T. Gibbs 	 * Handle Hot-Plug scenarios.  If there is already a peripheral
1638b8a9b1dSJustin T. Gibbs 	 * of our type assigned to this path, we are likely waiting for
1648b8a9b1dSJustin T. Gibbs 	 * final close on an old, invalidated, peripheral.  If this is
1658b8a9b1dSJustin T. Gibbs 	 * the case, queue up a deferred call to the peripheral's async
16674c91ec5SJustin T. Gibbs 	 * handler.  If it looks like a mistaken re-allocation, complain.
1678b8a9b1dSJustin T. Gibbs 	 */
1688b8a9b1dSJustin T. Gibbs 	if ((periph = cam_periph_find(path, name)) != NULL) {
1698b8a9b1dSJustin T. Gibbs 
1708b8a9b1dSJustin T. Gibbs 		if ((periph->flags & CAM_PERIPH_INVALID) != 0
1718b8a9b1dSJustin T. Gibbs 		 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
1728b8a9b1dSJustin T. Gibbs 			periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
1738b8a9b1dSJustin T. Gibbs 			periph->deferred_callback = ac_callback;
1748b8a9b1dSJustin T. Gibbs 			periph->deferred_ac = code;
1758b8a9b1dSJustin T. Gibbs 			return (CAM_REQ_INPROG);
1768b8a9b1dSJustin T. Gibbs 		} else {
1778b8a9b1dSJustin T. Gibbs 			printf("cam_periph_alloc: attempt to re-allocate "
1788900f4b8SKenneth D. Merry 			       "valid device %s%d rejected flags %#x "
1798900f4b8SKenneth D. Merry 			       "refcount %d\n", periph->periph_name,
1808900f4b8SKenneth D. Merry 			       periph->unit_number, periph->flags,
1818900f4b8SKenneth D. Merry 			       periph->refcount);
1828b8a9b1dSJustin T. Gibbs 		}
1838b8a9b1dSJustin T. Gibbs 		return (CAM_REQ_INVALID);
1848b8a9b1dSJustin T. Gibbs 	}
1858b8a9b1dSJustin T. Gibbs 
186362abc44STai-hwa Liang 	periph = (struct cam_periph *)malloc(sizeof(*periph), M_CAMPERIPH,
1878900f4b8SKenneth D. Merry 					     M_NOWAIT|M_ZERO);
1888b8a9b1dSJustin T. Gibbs 
1898b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
1908b8a9b1dSJustin T. Gibbs 		return (CAM_RESRC_UNAVAIL);
1918b8a9b1dSJustin T. Gibbs 
1928b8a9b1dSJustin T. Gibbs 	init_level++;
1938b8a9b1dSJustin T. Gibbs 
1948b8a9b1dSJustin T. Gibbs 
1952b83592fSScott Long 	sim = xpt_path_sim(path);
1968b8a9b1dSJustin T. Gibbs 	path_id = xpt_path_path_id(path);
1978b8a9b1dSJustin T. Gibbs 	target_id = xpt_path_target_id(path);
1988b8a9b1dSJustin T. Gibbs 	lun_id = xpt_path_lun_id(path);
1998b8a9b1dSJustin T. Gibbs 	cam_init_pinfo(&periph->pinfo);
2008b8a9b1dSJustin T. Gibbs 	periph->periph_start = periph_start;
2018b8a9b1dSJustin T. Gibbs 	periph->periph_dtor = periph_dtor;
202ee9c90c7SKenneth D. Merry 	periph->periph_oninval = periph_oninvalidate;
2038b8a9b1dSJustin T. Gibbs 	periph->type = type;
2048b8a9b1dSJustin T. Gibbs 	periph->periph_name = name;
2058b8a9b1dSJustin T. Gibbs 	periph->immediate_priority = CAM_PRIORITY_NONE;
206d38c0e53SAlexander Motin 	periph->refcount = 1;		/* Dropped by invalidation. */
2072b83592fSScott Long 	periph->sim = sim;
2088b8a9b1dSJustin T. Gibbs 	SLIST_INIT(&periph->ccb_list);
2098b8a9b1dSJustin T. Gibbs 	status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
2108b8a9b1dSJustin T. Gibbs 	if (status != CAM_REQ_CMP)
2118b8a9b1dSJustin T. Gibbs 		goto failure;
2128b8a9b1dSJustin T. Gibbs 	periph->path = path;
2138b8a9b1dSJustin T. Gibbs 
214f1e2546aSMatt Jacob 	xpt_lock_buses();
215f1e2546aSMatt Jacob 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
216f1e2546aSMatt Jacob 		if (strcmp((*p_drv)->driver_name, name) == 0)
217f1e2546aSMatt Jacob 			break;
218f1e2546aSMatt Jacob 	}
219f1e2546aSMatt Jacob 	if (*p_drv == NULL) {
220f1e2546aSMatt Jacob 		printf("cam_periph_alloc: invalid periph name '%s'\n", name);
2219e0d30e2SEdward Tomasz Napierala 		xpt_unlock_buses();
222f1e2546aSMatt Jacob 		xpt_free_path(periph->path);
223f1e2546aSMatt Jacob 		free(periph, M_CAMPERIPH);
224f1e2546aSMatt Jacob 		return (CAM_REQ_INVALID);
225f1e2546aSMatt Jacob 	}
226f1e2546aSMatt Jacob 	periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
2278b8a9b1dSJustin T. Gibbs 	cur_periph = TAILQ_FIRST(&(*p_drv)->units);
2288b8a9b1dSJustin T. Gibbs 	while (cur_periph != NULL
2298b8a9b1dSJustin T. Gibbs 	    && cur_periph->unit_number < periph->unit_number)
2308b8a9b1dSJustin T. Gibbs 		cur_periph = TAILQ_NEXT(cur_periph, unit_links);
231f1e2546aSMatt Jacob 	if (cur_periph != NULL) {
232f1e2546aSMatt Jacob 		KASSERT(cur_periph->unit_number != periph->unit_number, ("duplicate units on periph list"));
2338b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
234f1e2546aSMatt Jacob 	} else {
2358b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
2368b8a9b1dSJustin T. Gibbs 		(*p_drv)->generation++;
2378b8a9b1dSJustin T. Gibbs 	}
238f1e2546aSMatt Jacob 	xpt_unlock_buses();
239f1e2546aSMatt Jacob 
240f1e2546aSMatt Jacob 	init_level++;
241f1e2546aSMatt Jacob 
242f1e2546aSMatt Jacob 	status = xpt_add_periph(periph);
243f1e2546aSMatt Jacob 	if (status != CAM_REQ_CMP)
244f1e2546aSMatt Jacob 		goto failure;
2458b8a9b1dSJustin T. Gibbs 
2468b8a9b1dSJustin T. Gibbs 	init_level++;
24722c7d606SAlexander Motin 	CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph created\n"));
2488b8a9b1dSJustin T. Gibbs 
2498b8a9b1dSJustin T. Gibbs 	status = periph_ctor(periph, arg);
2508b8a9b1dSJustin T. Gibbs 
2518b8a9b1dSJustin T. Gibbs 	if (status == CAM_REQ_CMP)
2528b8a9b1dSJustin T. Gibbs 		init_level++;
2538b8a9b1dSJustin T. Gibbs 
2548b8a9b1dSJustin T. Gibbs failure:
2558b8a9b1dSJustin T. Gibbs 	switch (init_level) {
2568b8a9b1dSJustin T. Gibbs 	case 4:
2578b8a9b1dSJustin T. Gibbs 		/* Initialized successfully */
2588b8a9b1dSJustin T. Gibbs 		break;
2598b8a9b1dSJustin T. Gibbs 	case 3:
26022c7d606SAlexander Motin 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
261a29779e8SAlexander Motin 		xpt_remove_periph(periph);
2627379c88fSPoul-Henning Kamp 		/* FALLTHROUGH */
2638b8a9b1dSJustin T. Gibbs 	case 2:
264f1e2546aSMatt Jacob 		xpt_lock_buses();
265f1e2546aSMatt Jacob 		TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
266f1e2546aSMatt Jacob 		xpt_unlock_buses();
2678b8a9b1dSJustin T. Gibbs 		xpt_free_path(periph->path);
2687379c88fSPoul-Henning Kamp 		/* FALLTHROUGH */
2698b8a9b1dSJustin T. Gibbs 	case 1:
270362abc44STai-hwa Liang 		free(periph, M_CAMPERIPH);
2717379c88fSPoul-Henning Kamp 		/* FALLTHROUGH */
2728b8a9b1dSJustin T. Gibbs 	case 0:
2738b8a9b1dSJustin T. Gibbs 		/* No cleanup to perform. */
2748b8a9b1dSJustin T. Gibbs 		break;
2758b8a9b1dSJustin T. Gibbs 	default:
276905cb85dSKenneth D. Merry 		panic("%s: Unknown init level", __func__);
2778b8a9b1dSJustin T. Gibbs 	}
2788b8a9b1dSJustin T. Gibbs 	return(status);
2798b8a9b1dSJustin T. Gibbs }
2808b8a9b1dSJustin T. Gibbs 
2818b8a9b1dSJustin T. Gibbs /*
2828b8a9b1dSJustin T. Gibbs  * Find a peripheral structure with the specified path, target, lun,
2838b8a9b1dSJustin T. Gibbs  * and (optionally) type.  If the name is NULL, this function will return
2848b8a9b1dSJustin T. Gibbs  * the first peripheral driver that matches the specified path.
2858b8a9b1dSJustin T. Gibbs  */
2868b8a9b1dSJustin T. Gibbs struct cam_periph *
2878b8a9b1dSJustin T. Gibbs cam_periph_find(struct cam_path *path, char *name)
2888b8a9b1dSJustin T. Gibbs {
2898b8a9b1dSJustin T. Gibbs 	struct periph_driver **p_drv;
2908b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph;
2918b8a9b1dSJustin T. Gibbs 
29277dc25ccSScott Long 	xpt_lock_buses();
2930b7c27b9SPeter Wemm 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
2948b8a9b1dSJustin T. Gibbs 
2958b8a9b1dSJustin T. Gibbs 		if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
2968b8a9b1dSJustin T. Gibbs 			continue;
2978b8a9b1dSJustin T. Gibbs 
29837d40066SPoul-Henning Kamp 		TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
2998b8a9b1dSJustin T. Gibbs 			if (xpt_path_comp(periph->path, path) == 0) {
30077dc25ccSScott Long 				xpt_unlock_buses();
301f1e2546aSMatt Jacob 				mtx_assert(periph->sim->mtx, MA_OWNED);
3028b8a9b1dSJustin T. Gibbs 				return(periph);
3038b8a9b1dSJustin T. Gibbs 			}
3048b8a9b1dSJustin T. Gibbs 		}
30577dc25ccSScott Long 		if (name != NULL) {
30677dc25ccSScott Long 			xpt_unlock_buses();
3078b8a9b1dSJustin T. Gibbs 			return(NULL);
3088b8a9b1dSJustin T. Gibbs 		}
30977dc25ccSScott Long 	}
31077dc25ccSScott Long 	xpt_unlock_buses();
3118b8a9b1dSJustin T. Gibbs 	return(NULL);
3128b8a9b1dSJustin T. Gibbs }
3138b8a9b1dSJustin T. Gibbs 
3143501942bSJustin T. Gibbs /*
3158900f4b8SKenneth D. Merry  * Find peripheral driver instances attached to the specified path.
3163501942bSJustin T. Gibbs  */
3173501942bSJustin T. Gibbs int
3183501942bSJustin T. Gibbs cam_periph_list(struct cam_path *path, struct sbuf *sb)
3193501942bSJustin T. Gibbs {
3208900f4b8SKenneth D. Merry 	struct sbuf local_sb;
3213501942bSJustin T. Gibbs 	struct periph_driver **p_drv;
3223501942bSJustin T. Gibbs 	struct cam_periph *periph;
3233501942bSJustin T. Gibbs 	int count;
3248900f4b8SKenneth D. Merry 	int sbuf_alloc_len;
3253501942bSJustin T. Gibbs 
3268900f4b8SKenneth D. Merry 	sbuf_alloc_len = 16;
3278900f4b8SKenneth D. Merry retry:
3288900f4b8SKenneth D. Merry 	sbuf_new(&local_sb, NULL, sbuf_alloc_len, SBUF_FIXEDLEN);
3293501942bSJustin T. Gibbs 	count = 0;
3303501942bSJustin T. Gibbs 	xpt_lock_buses();
3313501942bSJustin T. Gibbs 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
3323501942bSJustin T. Gibbs 
3333501942bSJustin T. Gibbs 		TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
3343501942bSJustin T. Gibbs 			if (xpt_path_comp(periph->path, path) != 0)
3353501942bSJustin T. Gibbs 				continue;
3363501942bSJustin T. Gibbs 
3378900f4b8SKenneth D. Merry 			if (sbuf_len(&local_sb) != 0)
3388900f4b8SKenneth D. Merry 				sbuf_cat(&local_sb, ",");
3393501942bSJustin T. Gibbs 
3408900f4b8SKenneth D. Merry 			sbuf_printf(&local_sb, "%s%d", periph->periph_name,
3413501942bSJustin T. Gibbs 				    periph->unit_number);
3428900f4b8SKenneth D. Merry 
3438900f4b8SKenneth D. Merry 			if (sbuf_error(&local_sb) == ENOMEM) {
3448900f4b8SKenneth D. Merry 				sbuf_alloc_len *= 2;
3458900f4b8SKenneth D. Merry 				xpt_unlock_buses();
3468900f4b8SKenneth D. Merry 				sbuf_delete(&local_sb);
3478900f4b8SKenneth D. Merry 				goto retry;
3488900f4b8SKenneth D. Merry 			}
3493501942bSJustin T. Gibbs 			count++;
3503501942bSJustin T. Gibbs 		}
3513501942bSJustin T. Gibbs 	}
3523501942bSJustin T. Gibbs 	xpt_unlock_buses();
3538900f4b8SKenneth D. Merry 	sbuf_finish(&local_sb);
3548900f4b8SKenneth D. Merry 	sbuf_cpy(sb, sbuf_data(&local_sb));
3558900f4b8SKenneth D. Merry 	sbuf_delete(&local_sb);
3563501942bSJustin T. Gibbs 	return (count);
3573501942bSJustin T. Gibbs }
3583501942bSJustin T. Gibbs 
3598b8a9b1dSJustin T. Gibbs cam_status
3608b8a9b1dSJustin T. Gibbs cam_periph_acquire(struct cam_periph *periph)
3618b8a9b1dSJustin T. Gibbs {
3628900f4b8SKenneth D. Merry 	cam_status status;
3638b8a9b1dSJustin T. Gibbs 
3648900f4b8SKenneth D. Merry 	status = CAM_REQ_CMP_ERR;
3658b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
3668900f4b8SKenneth D. Merry 		return (status);
3678b8a9b1dSJustin T. Gibbs 
3682b83592fSScott Long 	xpt_lock_buses();
3698900f4b8SKenneth D. Merry 	if ((periph->flags & CAM_PERIPH_INVALID) == 0) {
3708b8a9b1dSJustin T. Gibbs 		periph->refcount++;
3718900f4b8SKenneth D. Merry 		status = CAM_REQ_CMP;
3728900f4b8SKenneth D. Merry 	}
3732b83592fSScott Long 	xpt_unlock_buses();
3748b8a9b1dSJustin T. Gibbs 
3758900f4b8SKenneth D. Merry 	return (status);
3768900f4b8SKenneth D. Merry }
3778900f4b8SKenneth D. Merry 
3788900f4b8SKenneth D. Merry void
3798900f4b8SKenneth D. Merry cam_periph_release_locked_buses(struct cam_periph *periph)
3808900f4b8SKenneth D. Merry {
381dcdf6e74SAlexander Motin 
382dcdf6e74SAlexander Motin 	mtx_assert(periph->sim->mtx, MA_OWNED);
383dcdf6e74SAlexander Motin 	KASSERT(periph->refcount >= 1, ("periph->refcount >= 1"));
384d38c0e53SAlexander Motin 	if (--periph->refcount == 0)
3858900f4b8SKenneth D. Merry 		camperiphfree(periph);
3868900f4b8SKenneth D. Merry }
3878b8a9b1dSJustin T. Gibbs 
3888b8a9b1dSJustin T. Gibbs void
38924ebf566SEdward Tomasz Napierala cam_periph_release_locked(struct cam_periph *periph)
3908b8a9b1dSJustin T. Gibbs {
3918b8a9b1dSJustin T. Gibbs 
3928b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
3938b8a9b1dSJustin T. Gibbs 		return;
3948b8a9b1dSJustin T. Gibbs 
3952b83592fSScott Long 	xpt_lock_buses();
3968900f4b8SKenneth D. Merry 	cam_periph_release_locked_buses(periph);
3972b83592fSScott Long 	xpt_unlock_buses();
39824ebf566SEdward Tomasz Napierala }
3998b8a9b1dSJustin T. Gibbs 
40024ebf566SEdward Tomasz Napierala void
40124ebf566SEdward Tomasz Napierala cam_periph_release(struct cam_periph *periph)
40224ebf566SEdward Tomasz Napierala {
40324ebf566SEdward Tomasz Napierala 	struct cam_sim *sim;
40424ebf566SEdward Tomasz Napierala 
40524ebf566SEdward Tomasz Napierala 	if (periph == NULL)
40624ebf566SEdward Tomasz Napierala 		return;
40724ebf566SEdward Tomasz Napierala 
40824ebf566SEdward Tomasz Napierala 	sim = periph->sim;
40924ebf566SEdward Tomasz Napierala 	mtx_assert(sim->mtx, MA_NOTOWNED);
41024ebf566SEdward Tomasz Napierala 	mtx_lock(sim->mtx);
41124ebf566SEdward Tomasz Napierala 	cam_periph_release_locked(periph);
41224ebf566SEdward Tomasz Napierala 	mtx_unlock(sim->mtx);
4138b8a9b1dSJustin T. Gibbs }
4148b8a9b1dSJustin T. Gibbs 
4152b83592fSScott Long int
4162b83592fSScott Long cam_periph_hold(struct cam_periph *periph, int priority)
4172b83592fSScott Long {
4182b83592fSScott Long 	int error;
4192b83592fSScott Long 
4202b83592fSScott Long 	/*
4212b83592fSScott Long 	 * Increment the reference count on the peripheral
4222b83592fSScott Long 	 * while we wait for our lock attempt to succeed
4232b83592fSScott Long 	 * to ensure the peripheral doesn't disappear out
4242b83592fSScott Long 	 * from user us while we sleep.
4252b83592fSScott Long 	 */
4262b83592fSScott Long 
4272b83592fSScott Long 	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
4282b83592fSScott Long 		return (ENXIO);
4292b83592fSScott Long 
4301fa738c2SJohn Baldwin 	mtx_assert(periph->sim->mtx, MA_OWNED);
4312b83592fSScott Long 	while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
4322b83592fSScott Long 		periph->flags |= CAM_PERIPH_LOCK_WANTED;
4331fa738c2SJohn Baldwin 		if ((error = mtx_sleep(periph, periph->sim->mtx, priority,
4341fa738c2SJohn Baldwin 		    "caplck", 0)) != 0) {
43524ebf566SEdward Tomasz Napierala 			cam_periph_release_locked(periph);
4362b83592fSScott Long 			return (error);
4372b83592fSScott Long 		}
438aed9c88cSAlexander Motin 		if (periph->flags & CAM_PERIPH_INVALID) {
439aed9c88cSAlexander Motin 			cam_periph_release_locked(periph);
440aed9c88cSAlexander Motin 			return (ENXIO);
441aed9c88cSAlexander Motin 		}
4422b83592fSScott Long 	}
4432b83592fSScott Long 
4442b83592fSScott Long 	periph->flags |= CAM_PERIPH_LOCKED;
4452b83592fSScott Long 	return (0);
4462b83592fSScott Long }
4472b83592fSScott Long 
4482b83592fSScott Long void
4492b83592fSScott Long cam_periph_unhold(struct cam_periph *periph)
4502b83592fSScott Long {
4512b83592fSScott Long 
4522b83592fSScott Long 	mtx_assert(periph->sim->mtx, MA_OWNED);
4532b83592fSScott Long 
4542b83592fSScott Long 	periph->flags &= ~CAM_PERIPH_LOCKED;
4552b83592fSScott Long 	if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
4562b83592fSScott Long 		periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
4572b83592fSScott Long 		wakeup(periph);
4582b83592fSScott Long 	}
4592b83592fSScott Long 
46024ebf566SEdward Tomasz Napierala 	cam_periph_release_locked(periph);
4612b83592fSScott Long }
4622b83592fSScott Long 
4638b8a9b1dSJustin T. Gibbs /*
4648b8a9b1dSJustin T. Gibbs  * Look for the next unit number that is not currently in use for this
4658b8a9b1dSJustin T. Gibbs  * peripheral type starting at "newunit".  Also exclude unit numbers that
4668b8a9b1dSJustin T. Gibbs  * are reserved by for future "hardwiring" unless we already know that this
4678b8a9b1dSJustin T. Gibbs  * is a potential wired device.  Only assume that the device is "wired" the
4688b8a9b1dSJustin T. Gibbs  * first time through the loop since after that we'll be looking at unit
4698b8a9b1dSJustin T. Gibbs  * numbers that did not match a wiring entry.
4708b8a9b1dSJustin T. Gibbs  */
4718b8a9b1dSJustin T. Gibbs static u_int
472501468a5SKenneth D. Merry camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
473501468a5SKenneth D. Merry 		  path_id_t pathid, target_id_t target, lun_id_t lun)
4748b8a9b1dSJustin T. Gibbs {
4758b8a9b1dSJustin T. Gibbs 	struct	cam_periph *periph;
4762398f0cdSPeter Wemm 	char	*periph_name;
4772398f0cdSPeter Wemm 	int	i, val, dunit, r;
4782398f0cdSPeter Wemm 	const char *dname, *strval;
4798b8a9b1dSJustin T. Gibbs 
4808b8a9b1dSJustin T. Gibbs 	periph_name = p_drv->driver_name;
4818b8a9b1dSJustin T. Gibbs 	for (;;newunit++) {
4828b8a9b1dSJustin T. Gibbs 
4838b8a9b1dSJustin T. Gibbs 		for (periph = TAILQ_FIRST(&p_drv->units);
4848b8a9b1dSJustin T. Gibbs 		     periph != NULL && periph->unit_number != newunit;
4858b8a9b1dSJustin T. Gibbs 		     periph = TAILQ_NEXT(periph, unit_links))
4868b8a9b1dSJustin T. Gibbs 			;
4878b8a9b1dSJustin T. Gibbs 
4888b8a9b1dSJustin T. Gibbs 		if (periph != NULL && periph->unit_number == newunit) {
4898b8a9b1dSJustin T. Gibbs 			if (wired != 0) {
490f0d9af51SMatt Jacob 				xpt_print(periph->path, "Duplicate Wired "
491f0d9af51SMatt Jacob 				    "Device entry!\n");
492f0d9af51SMatt Jacob 				xpt_print(periph->path, "Second device (%s "
493f0d9af51SMatt Jacob 				    "device at scbus%d target %d lun %d) will "
494f0d9af51SMatt Jacob 				    "not be wired\n", periph_name, pathid,
495f0d9af51SMatt Jacob 				    target, lun);
4968b8a9b1dSJustin T. Gibbs 				wired = 0;
4978b8a9b1dSJustin T. Gibbs 			}
4988b8a9b1dSJustin T. Gibbs 			continue;
4998b8a9b1dSJustin T. Gibbs 		}
50075f51904SPeter Wemm 		if (wired)
50175f51904SPeter Wemm 			break;
5028b8a9b1dSJustin T. Gibbs 
5038b8a9b1dSJustin T. Gibbs 		/*
5048b8a9b1dSJustin T. Gibbs 		 * Don't match entries like "da 4" as a wired down
5058b8a9b1dSJustin T. Gibbs 		 * device, but do match entries like "da 4 target 5"
5068b8a9b1dSJustin T. Gibbs 		 * or even "da 4 scbus 1".
5078b8a9b1dSJustin T. Gibbs 		 */
5082398f0cdSPeter Wemm 		i = 0;
5092398f0cdSPeter Wemm 		dname = periph_name;
5102398f0cdSPeter Wemm 		for (;;) {
5112398f0cdSPeter Wemm 			r = resource_find_dev(&i, dname, &dunit, NULL, NULL);
5122398f0cdSPeter Wemm 			if (r != 0)
5132398f0cdSPeter Wemm 				break;
51475f51904SPeter Wemm 			/* if no "target" and no specific scbus, skip */
51575f51904SPeter Wemm 			if (resource_int_value(dname, dunit, "target", &val) &&
51675f51904SPeter Wemm 			    (resource_string_value(dname, dunit, "at",&strval)||
51775f51904SPeter Wemm 			     strcmp(strval, "scbus") == 0))
51875f51904SPeter Wemm 				continue;
51975f51904SPeter Wemm 			if (newunit == dunit)
5208b8a9b1dSJustin T. Gibbs 				break;
5218b8a9b1dSJustin T. Gibbs 		}
5222398f0cdSPeter Wemm 		if (r != 0)
5238b8a9b1dSJustin T. Gibbs 			break;
5248b8a9b1dSJustin T. Gibbs 	}
5258b8a9b1dSJustin T. Gibbs 	return (newunit);
5268b8a9b1dSJustin T. Gibbs }
5278b8a9b1dSJustin T. Gibbs 
5288b8a9b1dSJustin T. Gibbs static u_int
5298b8a9b1dSJustin T. Gibbs camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
5308b8a9b1dSJustin T. Gibbs 	      target_id_t target, lun_id_t lun)
5318b8a9b1dSJustin T. Gibbs {
5328b8a9b1dSJustin T. Gibbs 	u_int	unit;
533c1b81613SPeter Wemm 	int	wired, i, val, dunit;
5342398f0cdSPeter Wemm 	const char *dname, *strval;
5352398f0cdSPeter Wemm 	char	pathbuf[32], *periph_name;
5368b8a9b1dSJustin T. Gibbs 
53775f51904SPeter Wemm 	periph_name = p_drv->driver_name;
53875f51904SPeter Wemm 	snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
539c1b81613SPeter Wemm 	unit = 0;
5402398f0cdSPeter Wemm 	i = 0;
5412398f0cdSPeter Wemm 	dname = periph_name;
542c1b81613SPeter Wemm 	for (wired = 0; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0;
543c1b81613SPeter Wemm 	     wired = 0) {
54475f51904SPeter Wemm 		if (resource_string_value(dname, dunit, "at", &strval) == 0) {
54575f51904SPeter Wemm 			if (strcmp(strval, pathbuf) != 0)
5468b8a9b1dSJustin T. Gibbs 				continue;
547c1b81613SPeter Wemm 			wired++;
5488b8a9b1dSJustin T. Gibbs 		}
54975f51904SPeter Wemm 		if (resource_int_value(dname, dunit, "target", &val) == 0) {
55075f51904SPeter Wemm 			if (val != target)
5518b8a9b1dSJustin T. Gibbs 				continue;
552c1b81613SPeter Wemm 			wired++;
5538b8a9b1dSJustin T. Gibbs 		}
55475f51904SPeter Wemm 		if (resource_int_value(dname, dunit, "lun", &val) == 0) {
55575f51904SPeter Wemm 			if (val != lun)
5568b8a9b1dSJustin T. Gibbs 				continue;
557c1b81613SPeter Wemm 			wired++;
5588b8a9b1dSJustin T. Gibbs 		}
559c1b81613SPeter Wemm 		if (wired != 0) {
56075f51904SPeter Wemm 			unit = dunit;
5618b8a9b1dSJustin T. Gibbs 			break;
5628b8a9b1dSJustin T. Gibbs 		}
5638b8a9b1dSJustin T. Gibbs 	}
5648b8a9b1dSJustin T. Gibbs 
5658b8a9b1dSJustin T. Gibbs 	/*
5668b8a9b1dSJustin T. Gibbs 	 * Either start from 0 looking for the next unit or from
56775f51904SPeter Wemm 	 * the unit number given in the resource config.  This way,
5688b8a9b1dSJustin T. Gibbs 	 * if we have wildcard matches, we don't return the same
5698b8a9b1dSJustin T. Gibbs 	 * unit number twice.
5708b8a9b1dSJustin T. Gibbs 	 */
571c1b81613SPeter Wemm 	unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun);
5728b8a9b1dSJustin T. Gibbs 
5738b8a9b1dSJustin T. Gibbs 	return (unit);
5748b8a9b1dSJustin T. Gibbs }
5758b8a9b1dSJustin T. Gibbs 
5768b8a9b1dSJustin T. Gibbs void
5778b8a9b1dSJustin T. Gibbs cam_periph_invalidate(struct cam_periph *periph)
5788b8a9b1dSJustin T. Gibbs {
5798b8a9b1dSJustin T. Gibbs 
580dcdf6e74SAlexander Motin 	mtx_assert(periph->sim->mtx, MA_OWNED);
581ee9c90c7SKenneth D. Merry 	/*
582ee9c90c7SKenneth D. Merry 	 * We only call this routine the first time a peripheral is
58377dc25ccSScott Long 	 * invalidated.
584ee9c90c7SKenneth D. Merry 	 */
585d38c0e53SAlexander Motin 	if ((periph->flags & CAM_PERIPH_INVALID) != 0)
586d38c0e53SAlexander Motin 		return;
587ee9c90c7SKenneth D. Merry 
588d38c0e53SAlexander Motin 	CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph invalidated\n"));
589*8d36a71bSAlexander Motin 	if (periph->flags & CAM_PERIPH_ANNOUNCED)
590*8d36a71bSAlexander Motin 		xpt_denounce_periph(periph);
5918b8a9b1dSJustin T. Gibbs 	periph->flags |= CAM_PERIPH_INVALID;
5928b8a9b1dSJustin T. Gibbs 	periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
593d38c0e53SAlexander Motin 	if (periph->periph_oninval != NULL)
594d38c0e53SAlexander Motin 		periph->periph_oninval(periph);
595d38c0e53SAlexander Motin 	cam_periph_release_locked(periph);
5968b8a9b1dSJustin T. Gibbs }
5978b8a9b1dSJustin T. Gibbs 
5988b8a9b1dSJustin T. Gibbs static void
5998b8a9b1dSJustin T. Gibbs camperiphfree(struct cam_periph *periph)
6008b8a9b1dSJustin T. Gibbs {
6018b8a9b1dSJustin T. Gibbs 	struct periph_driver **p_drv;
6028b8a9b1dSJustin T. Gibbs 
603dcdf6e74SAlexander Motin 	mtx_assert(periph->sim->mtx, MA_OWNED);
6040b7c27b9SPeter Wemm 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
6058b8a9b1dSJustin T. Gibbs 		if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
6068b8a9b1dSJustin T. Gibbs 			break;
6078b8a9b1dSJustin T. Gibbs 	}
608661658a6SScott Long 	if (*p_drv == NULL) {
609661658a6SScott Long 		printf("camperiphfree: attempt to free non-existant periph\n");
610661658a6SScott Long 		return;
611661658a6SScott Long 	}
6128b8a9b1dSJustin T. Gibbs 
613ea37f519SKenneth D. Merry 	/*
61433a38f74SKenneth D. Merry 	 * We need to set this flag before dropping the topology lock, to
61533a38f74SKenneth D. Merry 	 * let anyone who is traversing the list that this peripheral is
61633a38f74SKenneth D. Merry 	 * about to be freed, and there will be no more reference count
61733a38f74SKenneth D. Merry 	 * checks.
61833a38f74SKenneth D. Merry 	 */
61933a38f74SKenneth D. Merry 	periph->flags |= CAM_PERIPH_FREE;
62033a38f74SKenneth D. Merry 
62133a38f74SKenneth D. Merry 	/*
622ea37f519SKenneth D. Merry 	 * The peripheral destructor semantics dictate calling with only the
623ea37f519SKenneth D. Merry 	 * SIM mutex held.  Since it might sleep, it should not be called
624ea37f519SKenneth D. Merry 	 * with the topology lock held.
625ea37f519SKenneth D. Merry 	 */
626f3548746SScott Long 	xpt_unlock_buses();
6278b8a9b1dSJustin T. Gibbs 
628ea37f519SKenneth D. Merry 	/*
629ea37f519SKenneth D. Merry 	 * We need to call the peripheral destructor prior to removing the
630ea37f519SKenneth D. Merry 	 * peripheral from the list.  Otherwise, we risk running into a
631ea37f519SKenneth D. Merry 	 * scenario where the peripheral unit number may get reused
632ea37f519SKenneth D. Merry 	 * (because it has been removed from the list), but some resources
633ea37f519SKenneth D. Merry 	 * used by the peripheral are still hanging around.  In particular,
634ea37f519SKenneth D. Merry 	 * the devfs nodes used by some peripherals like the pass(4) driver
635ea37f519SKenneth D. Merry 	 * aren't fully cleaned up until the destructor is run.  If the
636ea37f519SKenneth D. Merry 	 * unit number is reused before the devfs instance is fully gone,
637ea37f519SKenneth D. Merry 	 * devfs will panic.
638ea37f519SKenneth D. Merry 	 */
639f3548746SScott Long 	if (periph->periph_dtor != NULL)
640f3548746SScott Long 		periph->periph_dtor(periph);
641ea37f519SKenneth D. Merry 
642ea37f519SKenneth D. Merry 	/*
643ea37f519SKenneth D. Merry 	 * The peripheral list is protected by the topology lock.
644ea37f519SKenneth D. Merry 	 */
645ea37f519SKenneth D. Merry 	xpt_lock_buses();
646ea37f519SKenneth D. Merry 
647ea37f519SKenneth D. Merry 	TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
648ea37f519SKenneth D. Merry 	(*p_drv)->generation++;
649ea37f519SKenneth D. Merry 
650a29779e8SAlexander Motin 	xpt_remove_periph(periph);
651ea37f519SKenneth D. Merry 
652ea37f519SKenneth D. Merry 	xpt_unlock_buses();
653*8d36a71bSAlexander Motin 	if (periph->flags & CAM_PERIPH_ANNOUNCED) {
654*8d36a71bSAlexander Motin 		xpt_print(periph->path, "Periph destroyed\n");
655*8d36a71bSAlexander Motin 	} else
65622c7d606SAlexander Motin 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
6578b8a9b1dSJustin T. Gibbs 
6588b8a9b1dSJustin T. Gibbs 	if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
6598b8a9b1dSJustin T. Gibbs 		union ccb ccb;
6608b8a9b1dSJustin T. Gibbs 		void *arg;
6618b8a9b1dSJustin T. Gibbs 
6628b8a9b1dSJustin T. Gibbs 		switch (periph->deferred_ac) {
6638b8a9b1dSJustin T. Gibbs 		case AC_FOUND_DEVICE:
6648b8a9b1dSJustin T. Gibbs 			ccb.ccb_h.func_code = XPT_GDEV_TYPE;
665bbfa4aa1SAlexander Motin 			xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
6668b8a9b1dSJustin T. Gibbs 			xpt_action(&ccb);
6678b8a9b1dSJustin T. Gibbs 			arg = &ccb;
6688b8a9b1dSJustin T. Gibbs 			break;
6698b8a9b1dSJustin T. Gibbs 		case AC_PATH_REGISTERED:
6708b8a9b1dSJustin T. Gibbs 			ccb.ccb_h.func_code = XPT_PATH_INQ;
671bbfa4aa1SAlexander Motin 			xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
6728b8a9b1dSJustin T. Gibbs 			xpt_action(&ccb);
6738b8a9b1dSJustin T. Gibbs 			arg = &ccb;
6748b8a9b1dSJustin T. Gibbs 			break;
6758b8a9b1dSJustin T. Gibbs 		default:
6768b8a9b1dSJustin T. Gibbs 			arg = NULL;
6778b8a9b1dSJustin T. Gibbs 			break;
6788b8a9b1dSJustin T. Gibbs 		}
6798b8a9b1dSJustin T. Gibbs 		periph->deferred_callback(NULL, periph->deferred_ac,
6808b8a9b1dSJustin T. Gibbs 					  periph->path, arg);
6818b8a9b1dSJustin T. Gibbs 	}
6828b8a9b1dSJustin T. Gibbs 	xpt_free_path(periph->path);
683362abc44STai-hwa Liang 	free(periph, M_CAMPERIPH);
684f3548746SScott Long 	xpt_lock_buses();
6858b8a9b1dSJustin T. Gibbs }
6868b8a9b1dSJustin T. Gibbs 
6878b8a9b1dSJustin T. Gibbs /*
6888b8a9b1dSJustin T. Gibbs  * Map user virtual pointers into kernel virtual address space, so we can
68995fbded6SScott Long  * access the memory.  This is now a generic function that centralizes most
69095fbded6SScott Long  * of the sanity checks on the data flags, if any.
69195fbded6SScott Long  * This also only works for up to MAXPHYS memory.  Since we use
6928b8a9b1dSJustin T. Gibbs  * buffers to map stuff in and out, we're limited to the buffer size.
6938b8a9b1dSJustin T. Gibbs  */
6948b8a9b1dSJustin T. Gibbs int
6958b8a9b1dSJustin T. Gibbs cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
6968b8a9b1dSJustin T. Gibbs {
6972d5c7e45SMatthew Dillon 	int numbufs, i, j;
69879d49a06SKenneth D. Merry 	int flags[CAM_PERIPH_MAXMAPS];
6998b8a9b1dSJustin T. Gibbs 	u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
7008b8a9b1dSJustin T. Gibbs 	u_int32_t lengths[CAM_PERIPH_MAXMAPS];
7018b8a9b1dSJustin T. Gibbs 	u_int32_t dirs[CAM_PERIPH_MAXMAPS];
70252c9ce25SScott Long 	/* Some controllers may not be able to handle more data. */
70352c9ce25SScott Long 	size_t maxmap = DFLTPHYS;
7048b8a9b1dSJustin T. Gibbs 
7058b8a9b1dSJustin T. Gibbs 	switch(ccb->ccb_h.func_code) {
7068b8a9b1dSJustin T. Gibbs 	case XPT_DEV_MATCH:
7078b8a9b1dSJustin T. Gibbs 		if (ccb->cdm.match_buf_len == 0) {
7088b8a9b1dSJustin T. Gibbs 			printf("cam_periph_mapmem: invalid match buffer "
7098b8a9b1dSJustin T. Gibbs 			       "length 0\n");
7108b8a9b1dSJustin T. Gibbs 			return(EINVAL);
7118b8a9b1dSJustin T. Gibbs 		}
7128b8a9b1dSJustin T. Gibbs 		if (ccb->cdm.pattern_buf_len > 0) {
7138b8a9b1dSJustin T. Gibbs 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
7148b8a9b1dSJustin T. Gibbs 			lengths[0] = ccb->cdm.pattern_buf_len;
7158b8a9b1dSJustin T. Gibbs 			dirs[0] = CAM_DIR_OUT;
7168b8a9b1dSJustin T. Gibbs 			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
7178b8a9b1dSJustin T. Gibbs 			lengths[1] = ccb->cdm.match_buf_len;
7188b8a9b1dSJustin T. Gibbs 			dirs[1] = CAM_DIR_IN;
7198b8a9b1dSJustin T. Gibbs 			numbufs = 2;
7208b8a9b1dSJustin T. Gibbs 		} else {
7218b8a9b1dSJustin T. Gibbs 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
7228b8a9b1dSJustin T. Gibbs 			lengths[0] = ccb->cdm.match_buf_len;
7238b8a9b1dSJustin T. Gibbs 			dirs[0] = CAM_DIR_IN;
7248b8a9b1dSJustin T. Gibbs 			numbufs = 1;
7258b8a9b1dSJustin T. Gibbs 		}
72652c9ce25SScott Long 		/*
72752c9ce25SScott Long 		 * This request will not go to the hardware, no reason
72852c9ce25SScott Long 		 * to be so strict. vmapbuf() is able to map up to MAXPHYS.
72952c9ce25SScott Long 		 */
73052c9ce25SScott Long 		maxmap = MAXPHYS;
7318b8a9b1dSJustin T. Gibbs 		break;
7328b8a9b1dSJustin T. Gibbs 	case XPT_SCSI_IO:
73387cfaf0eSJustin T. Gibbs 	case XPT_CONT_TARGET_IO:
7348b8a9b1dSJustin T. Gibbs 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
7358b8a9b1dSJustin T. Gibbs 			return(0);
73695fbded6SScott Long 		if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
73795fbded6SScott Long 			return (EINVAL);
7388b8a9b1dSJustin T. Gibbs 		data_ptrs[0] = &ccb->csio.data_ptr;
73979d49a06SKenneth D. Merry 		lengths[0] = ccb->csio.dxfer_len;
7408b8a9b1dSJustin T. Gibbs 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
7418b8a9b1dSJustin T. Gibbs 		numbufs = 1;
7428b8a9b1dSJustin T. Gibbs 		break;
74352c9ce25SScott Long 	case XPT_ATA_IO:
74452c9ce25SScott Long 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
74552c9ce25SScott Long 			return(0);
74695fbded6SScott Long 		if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
74795fbded6SScott Long 			return (EINVAL);
74852c9ce25SScott Long 		data_ptrs[0] = &ccb->ataio.data_ptr;
74952c9ce25SScott Long 		lengths[0] = ccb->ataio.dxfer_len;
75052c9ce25SScott Long 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
75152c9ce25SScott Long 		numbufs = 1;
75252c9ce25SScott Long 		break;
75306e79492SKenneth D. Merry 	case XPT_SMP_IO:
75406e79492SKenneth D. Merry 		data_ptrs[0] = &ccb->smpio.smp_request;
75506e79492SKenneth D. Merry 		lengths[0] = ccb->smpio.smp_request_len;
75606e79492SKenneth D. Merry 		dirs[0] = CAM_DIR_OUT;
75706e79492SKenneth D. Merry 		data_ptrs[1] = &ccb->smpio.smp_response;
75806e79492SKenneth D. Merry 		lengths[1] = ccb->smpio.smp_response_len;
75906e79492SKenneth D. Merry 		dirs[1] = CAM_DIR_IN;
76006e79492SKenneth D. Merry 		numbufs = 2;
76106e79492SKenneth D. Merry 		break;
7623501942bSJustin T. Gibbs 	case XPT_DEV_ADVINFO:
7633501942bSJustin T. Gibbs 		if (ccb->cdai.bufsiz == 0)
7647c103ddeSKenneth D. Merry 			return (0);
7657c103ddeSKenneth D. Merry 
7663501942bSJustin T. Gibbs 		data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
7673501942bSJustin T. Gibbs 		lengths[0] = ccb->cdai.bufsiz;
76806e79492SKenneth D. Merry 		dirs[0] = CAM_DIR_IN;
76906e79492SKenneth D. Merry 		numbufs = 1;
7707c103ddeSKenneth D. Merry 
7717c103ddeSKenneth D. Merry 		/*
7727c103ddeSKenneth D. Merry 		 * This request will not go to the hardware, no reason
7737c103ddeSKenneth D. Merry 		 * to be so strict. vmapbuf() is able to map up to MAXPHYS.
7747c103ddeSKenneth D. Merry 		 */
7757c103ddeSKenneth D. Merry 		maxmap = MAXPHYS;
77606e79492SKenneth D. Merry 		break;
7778b8a9b1dSJustin T. Gibbs 	default:
7788b8a9b1dSJustin T. Gibbs 		return(EINVAL);
7798b8a9b1dSJustin T. Gibbs 		break; /* NOTREACHED */
7808b8a9b1dSJustin T. Gibbs 	}
7818b8a9b1dSJustin T. Gibbs 
7828b8a9b1dSJustin T. Gibbs 	/*
78379d49a06SKenneth D. Merry 	 * Check the transfer length and permissions first, so we don't
78479d49a06SKenneth D. Merry 	 * have to unmap any previously mapped buffers.
7858b8a9b1dSJustin T. Gibbs 	 */
7868b8a9b1dSJustin T. Gibbs 	for (i = 0; i < numbufs; i++) {
78779d49a06SKenneth D. Merry 
78879d49a06SKenneth D. Merry 		flags[i] = 0;
78979d49a06SKenneth D. Merry 
79079d49a06SKenneth D. Merry 		/*
79179d49a06SKenneth D. Merry 		 * The userland data pointer passed in may not be page
79279d49a06SKenneth D. Merry 		 * aligned.  vmapbuf() truncates the address to a page
79379d49a06SKenneth D. Merry 		 * boundary, so if the address isn't page aligned, we'll
79479d49a06SKenneth D. Merry 		 * need enough space for the given transfer length, plus
79579d49a06SKenneth D. Merry 		 * whatever extra space is necessary to make it to the page
79679d49a06SKenneth D. Merry 		 * boundary.
79779d49a06SKenneth D. Merry 		 */
79879d49a06SKenneth D. Merry 		if ((lengths[i] +
79952c9ce25SScott Long 		    (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > maxmap){
8006d7b539aSPeter Wemm 			printf("cam_periph_mapmem: attempt to map %lu bytes, "
80152c9ce25SScott Long 			       "which is greater than %lu\n",
8026d7b539aSPeter Wemm 			       (long)(lengths[i] +
8036d7b539aSPeter Wemm 			       (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
80452c9ce25SScott Long 			       (u_long)maxmap);
80579d49a06SKenneth D. Merry 			return(E2BIG);
80679d49a06SKenneth D. Merry 		}
8078b8a9b1dSJustin T. Gibbs 
808edd24ab7SKenneth D. Merry 		if (dirs[i] & CAM_DIR_OUT) {
80921144e3bSPoul-Henning Kamp 			flags[i] = BIO_WRITE;
8108b8a9b1dSJustin T. Gibbs 		}
8118b8a9b1dSJustin T. Gibbs 
812edd24ab7SKenneth D. Merry 		if (dirs[i] & CAM_DIR_IN) {
81321144e3bSPoul-Henning Kamp 			flags[i] = BIO_READ;
8148b8a9b1dSJustin T. Gibbs 		}
8158b8a9b1dSJustin T. Gibbs 
81679d49a06SKenneth D. Merry 	}
81779d49a06SKenneth D. Merry 
81879d49a06SKenneth D. Merry 	/*
81995fbded6SScott Long 	 * This keeps the the kernel stack of current thread from getting
82095fbded6SScott Long 	 * swapped.  In low-memory situations where the kernel stack might
82195fbded6SScott Long 	 * otherwise get swapped out, this holds it and allows the thread
82295fbded6SScott Long 	 * to make progress and release the kernel mapped pages sooner.
82395fbded6SScott Long 	 *
82479d49a06SKenneth D. Merry 	 * XXX KDM should I use P_NOSWAP instead?
82579d49a06SKenneth D. Merry 	 */
8260cbbb7bfSPeter Wemm 	PHOLD(curproc);
82779d49a06SKenneth D. Merry 
82879d49a06SKenneth D. Merry 	for (i = 0; i < numbufs; i++) {
8298b8a9b1dSJustin T. Gibbs 		/*
8308b8a9b1dSJustin T. Gibbs 		 * Get the buffer.
8318b8a9b1dSJustin T. Gibbs 		 */
8321c7c3c6aSMatthew Dillon 		mapinfo->bp[i] = getpbuf(NULL);
8338b8a9b1dSJustin T. Gibbs 
8348b8a9b1dSJustin T. Gibbs 		/* save the buffer's data address */
8358b8a9b1dSJustin T. Gibbs 		mapinfo->bp[i]->b_saveaddr = mapinfo->bp[i]->b_data;
8368b8a9b1dSJustin T. Gibbs 
8378b8a9b1dSJustin T. Gibbs 		/* put our pointer in the data slot */
8388b8a9b1dSJustin T. Gibbs 		mapinfo->bp[i]->b_data = *data_ptrs[i];
8398b8a9b1dSJustin T. Gibbs 
84052c9ce25SScott Long 		/* set the transfer length, we know it's < MAXPHYS */
8418b8a9b1dSJustin T. Gibbs 		mapinfo->bp[i]->b_bufsize = lengths[i];
8428b8a9b1dSJustin T. Gibbs 
84321144e3bSPoul-Henning Kamp 		/* set the direction */
84421144e3bSPoul-Henning Kamp 		mapinfo->bp[i]->b_iocmd = flags[i];
8458b8a9b1dSJustin T. Gibbs 
8462d5c7e45SMatthew Dillon 		/*
8472d5c7e45SMatthew Dillon 		 * Map the buffer into kernel memory.
8482d5c7e45SMatthew Dillon 		 *
8492d5c7e45SMatthew Dillon 		 * Note that useracc() alone is not a  sufficient test.
8502d5c7e45SMatthew Dillon 		 * vmapbuf() can still fail due to a smaller file mapped
8512d5c7e45SMatthew Dillon 		 * into a larger area of VM, or if userland races against
8522d5c7e45SMatthew Dillon 		 * vmapbuf() after the useracc() check.
8532d5c7e45SMatthew Dillon 		 */
854e81ff91eSKonstantin Belousov 		if (vmapbuf(mapinfo->bp[i], 1) < 0) {
8552d5c7e45SMatthew Dillon 			for (j = 0; j < i; ++j) {
8562d5c7e45SMatthew Dillon 				*data_ptrs[j] = mapinfo->bp[j]->b_saveaddr;
8578373f36dSAlan Cox 				vunmapbuf(mapinfo->bp[j]);
8582d5c7e45SMatthew Dillon 				relpbuf(mapinfo->bp[j], NULL);
8592d5c7e45SMatthew Dillon 			}
8608373f36dSAlan Cox 			relpbuf(mapinfo->bp[i], NULL);
8612d5c7e45SMatthew Dillon 			PRELE(curproc);
8622d5c7e45SMatthew Dillon 			return(EACCES);
8632d5c7e45SMatthew Dillon 		}
8648b8a9b1dSJustin T. Gibbs 
8658b8a9b1dSJustin T. Gibbs 		/* set our pointer to the new mapped area */
8668b8a9b1dSJustin T. Gibbs 		*data_ptrs[i] = mapinfo->bp[i]->b_data;
8678b8a9b1dSJustin T. Gibbs 
8688b8a9b1dSJustin T. Gibbs 		mapinfo->num_bufs_used++;
8698b8a9b1dSJustin T. Gibbs 	}
8708b8a9b1dSJustin T. Gibbs 
871a62525f3SMatt Jacob 	/*
872a62525f3SMatt Jacob 	 * Now that we've gotten this far, change ownership to the kernel
873a62525f3SMatt Jacob 	 * of the buffers so that we don't run afoul of returning to user
874a62525f3SMatt Jacob 	 * space with locks (on the buffer) held.
875a62525f3SMatt Jacob 	 */
876a62525f3SMatt Jacob 	for (i = 0; i < numbufs; i++) {
877a62525f3SMatt Jacob 		BUF_KERNPROC(mapinfo->bp[i]);
878a62525f3SMatt Jacob 	}
879a62525f3SMatt Jacob 
880a62525f3SMatt Jacob 
8818b8a9b1dSJustin T. Gibbs 	return(0);
8828b8a9b1dSJustin T. Gibbs }
8838b8a9b1dSJustin T. Gibbs 
8848b8a9b1dSJustin T. Gibbs /*
8858b8a9b1dSJustin T. Gibbs  * Unmap memory segments mapped into kernel virtual address space by
8868b8a9b1dSJustin T. Gibbs  * cam_periph_mapmem().
8878b8a9b1dSJustin T. Gibbs  */
8888b8a9b1dSJustin T. Gibbs void
8898b8a9b1dSJustin T. Gibbs cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
8908b8a9b1dSJustin T. Gibbs {
8918b8a9b1dSJustin T. Gibbs 	int numbufs, i;
8928b8a9b1dSJustin T. Gibbs 	u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
8938b8a9b1dSJustin T. Gibbs 
8948b8a9b1dSJustin T. Gibbs 	if (mapinfo->num_bufs_used <= 0) {
89595fbded6SScott Long 		/* nothing to free and the process wasn't held. */
8968b8a9b1dSJustin T. Gibbs 		return;
8978b8a9b1dSJustin T. Gibbs 	}
8988b8a9b1dSJustin T. Gibbs 
8998b8a9b1dSJustin T. Gibbs 	switch (ccb->ccb_h.func_code) {
9008b8a9b1dSJustin T. Gibbs 	case XPT_DEV_MATCH:
9018b8a9b1dSJustin T. Gibbs 		numbufs = min(mapinfo->num_bufs_used, 2);
9028b8a9b1dSJustin T. Gibbs 
9038b8a9b1dSJustin T. Gibbs 		if (numbufs == 1) {
9048b8a9b1dSJustin T. Gibbs 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
9058b8a9b1dSJustin T. Gibbs 		} else {
9068b8a9b1dSJustin T. Gibbs 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
9078b8a9b1dSJustin T. Gibbs 			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
9088b8a9b1dSJustin T. Gibbs 		}
9098b8a9b1dSJustin T. Gibbs 		break;
9108b8a9b1dSJustin T. Gibbs 	case XPT_SCSI_IO:
9119911ecf9SJustin T. Gibbs 	case XPT_CONT_TARGET_IO:
9128b8a9b1dSJustin T. Gibbs 		data_ptrs[0] = &ccb->csio.data_ptr;
9138b8a9b1dSJustin T. Gibbs 		numbufs = min(mapinfo->num_bufs_used, 1);
9148b8a9b1dSJustin T. Gibbs 		break;
91552c9ce25SScott Long 	case XPT_ATA_IO:
91652c9ce25SScott Long 		data_ptrs[0] = &ccb->ataio.data_ptr;
91752c9ce25SScott Long 		numbufs = min(mapinfo->num_bufs_used, 1);
91852c9ce25SScott Long 		break;
91906e79492SKenneth D. Merry 	case XPT_SMP_IO:
92006e79492SKenneth D. Merry 		numbufs = min(mapinfo->num_bufs_used, 2);
92106e79492SKenneth D. Merry 		data_ptrs[0] = &ccb->smpio.smp_request;
92206e79492SKenneth D. Merry 		data_ptrs[1] = &ccb->smpio.smp_response;
92306e79492SKenneth D. Merry 		break;
9243501942bSJustin T. Gibbs 	case XPT_DEV_ADVINFO:
92506e79492SKenneth D. Merry 		numbufs = min(mapinfo->num_bufs_used, 1);
9263501942bSJustin T. Gibbs 		data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
92706e79492SKenneth D. Merry 		break;
9288b8a9b1dSJustin T. Gibbs 	default:
9298b8a9b1dSJustin T. Gibbs 		/* allow ourselves to be swapped once again */
9300cbbb7bfSPeter Wemm 		PRELE(curproc);
9318b8a9b1dSJustin T. Gibbs 		return;
9328b8a9b1dSJustin T. Gibbs 		break; /* NOTREACHED */
9338b8a9b1dSJustin T. Gibbs 	}
9348b8a9b1dSJustin T. Gibbs 
9358b8a9b1dSJustin T. Gibbs 	for (i = 0; i < numbufs; i++) {
9368b8a9b1dSJustin T. Gibbs 		/* Set the user's pointer back to the original value */
9378b8a9b1dSJustin T. Gibbs 		*data_ptrs[i] = mapinfo->bp[i]->b_saveaddr;
9388b8a9b1dSJustin T. Gibbs 
9398b8a9b1dSJustin T. Gibbs 		/* unmap the buffer */
9408b8a9b1dSJustin T. Gibbs 		vunmapbuf(mapinfo->bp[i]);
9418b8a9b1dSJustin T. Gibbs 
9428b8a9b1dSJustin T. Gibbs 		/* release the buffer */
9431c7c3c6aSMatthew Dillon 		relpbuf(mapinfo->bp[i], NULL);
9448b8a9b1dSJustin T. Gibbs 	}
9458b8a9b1dSJustin T. Gibbs 
9468b8a9b1dSJustin T. Gibbs 	/* allow ourselves to be swapped once again */
9470cbbb7bfSPeter Wemm 	PRELE(curproc);
9488b8a9b1dSJustin T. Gibbs }
9498b8a9b1dSJustin T. Gibbs 
9508b8a9b1dSJustin T. Gibbs union ccb *
9518b8a9b1dSJustin T. Gibbs cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
9528b8a9b1dSJustin T. Gibbs {
9538b8a9b1dSJustin T. Gibbs 	struct ccb_hdr *ccb_h;
9548b8a9b1dSJustin T. Gibbs 
9552b83592fSScott Long 	mtx_assert(periph->sim->mtx, MA_OWNED);
9568b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
9578b8a9b1dSJustin T. Gibbs 
958fc2ffbe6SPoul-Henning Kamp 	while (SLIST_FIRST(&periph->ccb_list) == NULL) {
9598b8a9b1dSJustin T. Gibbs 		if (periph->immediate_priority > priority)
9608b8a9b1dSJustin T. Gibbs 			periph->immediate_priority = priority;
9618b8a9b1dSJustin T. Gibbs 		xpt_schedule(periph, priority);
962fc2ffbe6SPoul-Henning Kamp 		if ((SLIST_FIRST(&periph->ccb_list) != NULL)
963fc2ffbe6SPoul-Henning Kamp 		 && (SLIST_FIRST(&periph->ccb_list)->pinfo.priority == priority))
9648b8a9b1dSJustin T. Gibbs 			break;
9652b83592fSScott Long 		mtx_assert(periph->sim->mtx, MA_OWNED);
9661fa738c2SJohn Baldwin 		mtx_sleep(&periph->ccb_list, periph->sim->mtx, PRIBIO, "cgticb",
9671fa738c2SJohn Baldwin 		    0);
9688b8a9b1dSJustin T. Gibbs 	}
9698b8a9b1dSJustin T. Gibbs 
970fc2ffbe6SPoul-Henning Kamp 	ccb_h = SLIST_FIRST(&periph->ccb_list);
9718b8a9b1dSJustin T. Gibbs 	SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
9728b8a9b1dSJustin T. Gibbs 	return ((union ccb *)ccb_h);
9738b8a9b1dSJustin T. Gibbs }
9748b8a9b1dSJustin T. Gibbs 
9758b8a9b1dSJustin T. Gibbs void
9768b8a9b1dSJustin T. Gibbs cam_periph_ccbwait(union ccb *ccb)
9778b8a9b1dSJustin T. Gibbs {
9782b83592fSScott Long 	struct cam_sim *sim;
9798b8a9b1dSJustin T. Gibbs 
9802b83592fSScott Long 	sim = xpt_path_sim(ccb->ccb_h.path);
9818b8a9b1dSJustin T. Gibbs 	if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
9828b8a9b1dSJustin T. Gibbs 	 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
9831fa738c2SJohn Baldwin 		mtx_sleep(&ccb->ccb_h.cbfcnp, sim->mtx, PRIBIO, "cbwait", 0);
9848b8a9b1dSJustin T. Gibbs }
9858b8a9b1dSJustin T. Gibbs 
9868b8a9b1dSJustin T. Gibbs int
987571e8e20SScott Long cam_periph_ioctl(struct cam_periph *periph, u_long cmd, caddr_t addr,
9888b8a9b1dSJustin T. Gibbs 		 int (*error_routine)(union ccb *ccb,
9898b8a9b1dSJustin T. Gibbs 				      cam_flags camflags,
9908b8a9b1dSJustin T. Gibbs 				      u_int32_t sense_flags))
9918b8a9b1dSJustin T. Gibbs {
9928b8a9b1dSJustin T. Gibbs 	union ccb 	     *ccb;
9938b8a9b1dSJustin T. Gibbs 	int 		     error;
9948b8a9b1dSJustin T. Gibbs 	int		     found;
9958b8a9b1dSJustin T. Gibbs 
9968b8a9b1dSJustin T. Gibbs 	error = found = 0;
9978b8a9b1dSJustin T. Gibbs 
9988b8a9b1dSJustin T. Gibbs 	switch(cmd){
9998b8a9b1dSJustin T. Gibbs 	case CAMGETPASSTHRU:
1000bbfa4aa1SAlexander Motin 		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
10018b8a9b1dSJustin T. Gibbs 		xpt_setup_ccb(&ccb->ccb_h,
10028b8a9b1dSJustin T. Gibbs 			      ccb->ccb_h.path,
1003bbfa4aa1SAlexander Motin 			      CAM_PRIORITY_NORMAL);
10048b8a9b1dSJustin T. Gibbs 		ccb->ccb_h.func_code = XPT_GDEVLIST;
10058b8a9b1dSJustin T. Gibbs 
10068b8a9b1dSJustin T. Gibbs 		/*
10078b8a9b1dSJustin T. Gibbs 		 * Basically, the point of this is that we go through
10088b8a9b1dSJustin T. Gibbs 		 * getting the list of devices, until we find a passthrough
10098b8a9b1dSJustin T. Gibbs 		 * device.  In the current version of the CAM code, the
10108b8a9b1dSJustin T. Gibbs 		 * only way to determine what type of device we're dealing
10118b8a9b1dSJustin T. Gibbs 		 * with is by its name.
10128b8a9b1dSJustin T. Gibbs 		 */
10138b8a9b1dSJustin T. Gibbs 		while (found == 0) {
10148b8a9b1dSJustin T. Gibbs 			ccb->cgdl.index = 0;
10158b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
10168b8a9b1dSJustin T. Gibbs 			while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
10178b8a9b1dSJustin T. Gibbs 
10188b8a9b1dSJustin T. Gibbs 				/* we want the next device in the list */
10198b8a9b1dSJustin T. Gibbs 				xpt_action(ccb);
10208b8a9b1dSJustin T. Gibbs 				if (strncmp(ccb->cgdl.periph_name,
10218b8a9b1dSJustin T. Gibbs 				    "pass", 4) == 0){
10228b8a9b1dSJustin T. Gibbs 					found = 1;
10238b8a9b1dSJustin T. Gibbs 					break;
10248b8a9b1dSJustin T. Gibbs 				}
10258b8a9b1dSJustin T. Gibbs 			}
10268b8a9b1dSJustin T. Gibbs 			if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
10278b8a9b1dSJustin T. Gibbs 			    (found == 0)) {
10288b8a9b1dSJustin T. Gibbs 				ccb->cgdl.periph_name[0] = '\0';
10298b8a9b1dSJustin T. Gibbs 				ccb->cgdl.unit_number = 0;
10308b8a9b1dSJustin T. Gibbs 				break;
10318b8a9b1dSJustin T. Gibbs 			}
10328b8a9b1dSJustin T. Gibbs 		}
10338b8a9b1dSJustin T. Gibbs 
10348b8a9b1dSJustin T. Gibbs 		/* copy the result back out */
10358b8a9b1dSJustin T. Gibbs 		bcopy(ccb, addr, sizeof(union ccb));
10368b8a9b1dSJustin T. Gibbs 
10378b8a9b1dSJustin T. Gibbs 		/* and release the ccb */
10388b8a9b1dSJustin T. Gibbs 		xpt_release_ccb(ccb);
10398b8a9b1dSJustin T. Gibbs 
10408b8a9b1dSJustin T. Gibbs 		break;
10418b8a9b1dSJustin T. Gibbs 	default:
10428b8a9b1dSJustin T. Gibbs 		error = ENOTTY;
10438b8a9b1dSJustin T. Gibbs 		break;
10448b8a9b1dSJustin T. Gibbs 	}
10458b8a9b1dSJustin T. Gibbs 	return(error);
10468b8a9b1dSJustin T. Gibbs }
10478b8a9b1dSJustin T. Gibbs 
10488b8a9b1dSJustin T. Gibbs int
10498b8a9b1dSJustin T. Gibbs cam_periph_runccb(union ccb *ccb,
10508b8a9b1dSJustin T. Gibbs 		  int (*error_routine)(union ccb *ccb,
10518b8a9b1dSJustin T. Gibbs 				       cam_flags camflags,
10528b8a9b1dSJustin T. Gibbs 				       u_int32_t sense_flags),
10538b8a9b1dSJustin T. Gibbs 		  cam_flags camflags, u_int32_t sense_flags,
10548b8a9b1dSJustin T. Gibbs 		  struct devstat *ds)
10558b8a9b1dSJustin T. Gibbs {
10562b83592fSScott Long 	struct cam_sim *sim;
10578b8a9b1dSJustin T. Gibbs 	int error;
10588b8a9b1dSJustin T. Gibbs 
10598b8a9b1dSJustin T. Gibbs 	error = 0;
10602b83592fSScott Long 	sim = xpt_path_sim(ccb->ccb_h.path);
10612b83592fSScott Long 	mtx_assert(sim->mtx, MA_OWNED);
10628b8a9b1dSJustin T. Gibbs 
10638b8a9b1dSJustin T. Gibbs 	/*
10648b8a9b1dSJustin T. Gibbs 	 * If the user has supplied a stats structure, and if we understand
10658b8a9b1dSJustin T. Gibbs 	 * this particular type of ccb, record the transaction start.
10668b8a9b1dSJustin T. Gibbs 	 */
1067379ad358SAlexander Motin 	if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO ||
1068379ad358SAlexander Motin 	    ccb->ccb_h.func_code == XPT_ATA_IO))
10697194d335SPoul-Henning Kamp 		devstat_start_transaction(ds, NULL);
10708b8a9b1dSJustin T. Gibbs 
10718b8a9b1dSJustin T. Gibbs 	xpt_action(ccb);
10728b8a9b1dSJustin T. Gibbs 
10738b8a9b1dSJustin T. Gibbs 	do {
10748b8a9b1dSJustin T. Gibbs 		cam_periph_ccbwait(ccb);
10758b8a9b1dSJustin T. Gibbs 		if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
10768b8a9b1dSJustin T. Gibbs 			error = 0;
10778b8a9b1dSJustin T. Gibbs 		else if (error_routine != NULL)
10788b8a9b1dSJustin T. Gibbs 			error = (*error_routine)(ccb, camflags, sense_flags);
10798b8a9b1dSJustin T. Gibbs 		else
10808b8a9b1dSJustin T. Gibbs 			error = 0;
10818b8a9b1dSJustin T. Gibbs 
10828b8a9b1dSJustin T. Gibbs 	} while (error == ERESTART);
10838b8a9b1dSJustin T. Gibbs 
108483c5d981SAlexander Motin 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
10858b8a9b1dSJustin T. Gibbs 		cam_release_devq(ccb->ccb_h.path,
10868b8a9b1dSJustin T. Gibbs 				 /* relsim_flags */0,
10878b8a9b1dSJustin T. Gibbs 				 /* openings */0,
10888b8a9b1dSJustin T. Gibbs 				 /* timeout */0,
10898b8a9b1dSJustin T. Gibbs 				 /* getcount_only */ FALSE);
109083c5d981SAlexander Motin 		ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
109183c5d981SAlexander Motin 	}
10928b8a9b1dSJustin T. Gibbs 
1093379ad358SAlexander Motin 	if (ds != NULL) {
1094379ad358SAlexander Motin 		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
10958b8a9b1dSJustin T. Gibbs 			devstat_end_transaction(ds,
10968b8a9b1dSJustin T. Gibbs 					ccb->csio.dxfer_len,
1097379ad358SAlexander Motin 					ccb->csio.tag_action & 0x3,
10988b8a9b1dSJustin T. Gibbs 					((ccb->ccb_h.flags & CAM_DIR_MASK) ==
10998b8a9b1dSJustin T. Gibbs 					CAM_DIR_NONE) ?  DEVSTAT_NO_DATA :
11008b8a9b1dSJustin T. Gibbs 					(ccb->ccb_h.flags & CAM_DIR_OUT) ?
11018b8a9b1dSJustin T. Gibbs 					DEVSTAT_WRITE :
11027194d335SPoul-Henning Kamp 					DEVSTAT_READ, NULL, NULL);
1103379ad358SAlexander Motin 		} else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1104379ad358SAlexander Motin 			devstat_end_transaction(ds,
1105379ad358SAlexander Motin 					ccb->ataio.dxfer_len,
1106379ad358SAlexander Motin 					ccb->ataio.tag_action & 0x3,
1107379ad358SAlexander Motin 					((ccb->ccb_h.flags & CAM_DIR_MASK) ==
1108379ad358SAlexander Motin 					CAM_DIR_NONE) ?  DEVSTAT_NO_DATA :
1109379ad358SAlexander Motin 					(ccb->ccb_h.flags & CAM_DIR_OUT) ?
1110379ad358SAlexander Motin 					DEVSTAT_WRITE :
1111379ad358SAlexander Motin 					DEVSTAT_READ, NULL, NULL);
1112379ad358SAlexander Motin 		}
1113379ad358SAlexander Motin 	}
11148b8a9b1dSJustin T. Gibbs 
11158b8a9b1dSJustin T. Gibbs 	return(error);
11168b8a9b1dSJustin T. Gibbs }
11178b8a9b1dSJustin T. Gibbs 
111887cfaf0eSJustin T. Gibbs void
111987cfaf0eSJustin T. Gibbs cam_freeze_devq(struct cam_path *path)
112087cfaf0eSJustin T. Gibbs {
1121cccf4220SAlexander Motin 	struct ccb_hdr ccb_h;
112287cfaf0eSJustin T. Gibbs 
11230d4f3c31SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_freeze_devq\n"));
1124cccf4220SAlexander Motin 	xpt_setup_ccb(&ccb_h, path, /*priority*/1);
1125cccf4220SAlexander Motin 	ccb_h.func_code = XPT_NOOP;
1126cccf4220SAlexander Motin 	ccb_h.flags = CAM_DEV_QFREEZE;
1127cccf4220SAlexander Motin 	xpt_action((union ccb *)&ccb_h);
112887cfaf0eSJustin T. Gibbs }
112987cfaf0eSJustin T. Gibbs 
11308b8a9b1dSJustin T. Gibbs u_int32_t
11318b8a9b1dSJustin T. Gibbs cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
113283c5d981SAlexander Motin 		 u_int32_t openings, u_int32_t arg,
11338b8a9b1dSJustin T. Gibbs 		 int getcount_only)
11348b8a9b1dSJustin T. Gibbs {
11358b8a9b1dSJustin T. Gibbs 	struct ccb_relsim crs;
11368b8a9b1dSJustin T. Gibbs 
11370d4f3c31SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_release_devq(%u, %u, %u, %d)\n",
11380d4f3c31SAlexander Motin 	    relsim_flags, openings, arg, getcount_only));
1139bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
11408b8a9b1dSJustin T. Gibbs 	crs.ccb_h.func_code = XPT_REL_SIMQ;
11418b8a9b1dSJustin T. Gibbs 	crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
11428b8a9b1dSJustin T. Gibbs 	crs.release_flags = relsim_flags;
11438b8a9b1dSJustin T. Gibbs 	crs.openings = openings;
114483c5d981SAlexander Motin 	crs.release_timeout = arg;
11458b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&crs);
11468b8a9b1dSJustin T. Gibbs 	return (crs.qfrozen_cnt);
11478b8a9b1dSJustin T. Gibbs }
11488b8a9b1dSJustin T. Gibbs 
11498b8a9b1dSJustin T. Gibbs #define saved_ccb_ptr ppriv_ptr0
115083c5d981SAlexander Motin static void
115183c5d981SAlexander Motin camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
115283c5d981SAlexander Motin {
11530191d9b3SAlexander Motin 	union ccb      *saved_ccb;
115483c5d981SAlexander Motin 	cam_status	status;
115583c5d981SAlexander Motin 	struct scsi_start_stop_unit *scsi_cmd;
1156e7493b28SAlexander Motin 	int    error_code, sense_key, asc, ascq;
115783c5d981SAlexander Motin 
11580191d9b3SAlexander Motin 	scsi_cmd = (struct scsi_start_stop_unit *)
11590191d9b3SAlexander Motin 	    &done_ccb->csio.cdb_io.cdb_bytes;
116083c5d981SAlexander Motin 	status = done_ccb->ccb_h.status;
11618b8a9b1dSJustin T. Gibbs 
11620191d9b3SAlexander Motin 	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1163e7493b28SAlexander Motin 		if (scsi_extract_sense_ccb(done_ccb,
1164e7493b28SAlexander Motin 		    &error_code, &sense_key, &asc, &ascq)) {
116565afe1f4SKenneth D. Merry 			/*
11668b8a9b1dSJustin T. Gibbs 			 * If the error is "invalid field in CDB",
11678b8a9b1dSJustin T. Gibbs 			 * and the load/eject flag is set, turn the
11688b8a9b1dSJustin T. Gibbs 			 * flag off and try again.  This is just in
11698b8a9b1dSJustin T. Gibbs 			 * case the drive in question barfs on the
11708b8a9b1dSJustin T. Gibbs 			 * load eject flag.  The CAM code should set
11718b8a9b1dSJustin T. Gibbs 			 * the load/eject flag by default for
11728b8a9b1dSJustin T. Gibbs 			 * removable media.
11738b8a9b1dSJustin T. Gibbs 			 */
11748b8a9b1dSJustin T. Gibbs 			if ((scsi_cmd->opcode == START_STOP_UNIT) &&
11758b8a9b1dSJustin T. Gibbs 			    ((scsi_cmd->how & SSS_LOEJ) != 0) &&
11760191d9b3SAlexander Motin 			     (asc == 0x24) && (ascq == 0x00)) {
11778b8a9b1dSJustin T. Gibbs 				scsi_cmd->how &= ~SSS_LOEJ;
11780191d9b3SAlexander Motin 				if (status & CAM_DEV_QFRZN) {
11790191d9b3SAlexander Motin 					cam_release_devq(done_ccb->ccb_h.path,
11800191d9b3SAlexander Motin 					    0, 0, 0, 0);
11810191d9b3SAlexander Motin 					done_ccb->ccb_h.status &=
11820191d9b3SAlexander Motin 					    ~CAM_DEV_QFRZN;
11830191d9b3SAlexander Motin 				}
11848b8a9b1dSJustin T. Gibbs 				xpt_action(done_ccb);
11850191d9b3SAlexander Motin 				goto out;
11860191d9b3SAlexander Motin 			}
11870191d9b3SAlexander Motin 		}
11880191d9b3SAlexander Motin 		if (cam_periph_error(done_ccb,
11890191d9b3SAlexander Motin 		    0, SF_RETRY_UA | SF_NO_PRINT, NULL) == ERESTART)
11900191d9b3SAlexander Motin 			goto out;
11910191d9b3SAlexander Motin 		if (done_ccb->ccb_h.status & CAM_DEV_QFRZN) {
11920191d9b3SAlexander Motin 			cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
11930191d9b3SAlexander Motin 			done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
11940191d9b3SAlexander Motin 		}
11950191d9b3SAlexander Motin 	} else {
11960191d9b3SAlexander Motin 		/*
11970191d9b3SAlexander Motin 		 * If we have successfully taken a device from the not
11980191d9b3SAlexander Motin 		 * ready to ready state, re-scan the device and re-get
11990191d9b3SAlexander Motin 		 * the inquiry information.  Many devices (mostly disks)
12000191d9b3SAlexander Motin 		 * don't properly report their inquiry information unless
12010191d9b3SAlexander Motin 		 * they are spun up.
12020191d9b3SAlexander Motin 		 */
12030191d9b3SAlexander Motin 		if (scsi_cmd->opcode == START_STOP_UNIT)
12040191d9b3SAlexander Motin 			xpt_async(AC_INQ_CHANGED, done_ccb->ccb_h.path, NULL);
12050191d9b3SAlexander Motin 	}
120665afe1f4SKenneth D. Merry 
12078b8a9b1dSJustin T. Gibbs 	/*
12080191d9b3SAlexander Motin 	 * Perform the final retry with the original CCB so that final
12090191d9b3SAlexander Motin 	 * error processing is performed by the owner of the CCB.
12108b8a9b1dSJustin T. Gibbs 	 */
12110191d9b3SAlexander Motin 	saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
121283c5d981SAlexander Motin 	bcopy(saved_ccb, done_ccb, sizeof(*done_ccb));
121383c5d981SAlexander Motin 	xpt_free_ccb(saved_ccb);
12140191d9b3SAlexander Motin 	if (done_ccb->ccb_h.cbfcnp != camperiphdone)
121560a899a0SKenneth D. Merry 		periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
12168b8a9b1dSJustin T. Gibbs 	xpt_action(done_ccb);
12178b8a9b1dSJustin T. Gibbs 
12180191d9b3SAlexander Motin out:
12190191d9b3SAlexander Motin 	/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
12200191d9b3SAlexander Motin 	cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1221d84c90a6SAlexander Motin }
12228b8a9b1dSJustin T. Gibbs 
12238b8a9b1dSJustin T. Gibbs /*
122487cfaf0eSJustin T. Gibbs  * Generic Async Event handler.  Peripheral drivers usually
122587cfaf0eSJustin T. Gibbs  * filter out the events that require personal attention,
122687cfaf0eSJustin T. Gibbs  * and leave the rest to this function.
122787cfaf0eSJustin T. Gibbs  */
122887cfaf0eSJustin T. Gibbs void
122987cfaf0eSJustin T. Gibbs cam_periph_async(struct cam_periph *periph, u_int32_t code,
123087cfaf0eSJustin T. Gibbs 		 struct cam_path *path, void *arg)
123187cfaf0eSJustin T. Gibbs {
123287cfaf0eSJustin T. Gibbs 	switch (code) {
123387cfaf0eSJustin T. Gibbs 	case AC_LOST_DEVICE:
123487cfaf0eSJustin T. Gibbs 		cam_periph_invalidate(periph);
123587cfaf0eSJustin T. Gibbs 		break;
123687cfaf0eSJustin T. Gibbs 	default:
123787cfaf0eSJustin T. Gibbs 		break;
123887cfaf0eSJustin T. Gibbs 	}
123987cfaf0eSJustin T. Gibbs }
124087cfaf0eSJustin T. Gibbs 
124187cfaf0eSJustin T. Gibbs void
124287cfaf0eSJustin T. Gibbs cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
124387cfaf0eSJustin T. Gibbs {
124487cfaf0eSJustin T. Gibbs 	struct ccb_getdevstats cgds;
124587cfaf0eSJustin T. Gibbs 
1246bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cgds.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
124787cfaf0eSJustin T. Gibbs 	cgds.ccb_h.func_code = XPT_GDEV_STATS;
124887cfaf0eSJustin T. Gibbs 	xpt_action((union ccb *)&cgds);
124987cfaf0eSJustin T. Gibbs 	cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
125087cfaf0eSJustin T. Gibbs }
125187cfaf0eSJustin T. Gibbs 
125287cfaf0eSJustin T. Gibbs void
125387cfaf0eSJustin T. Gibbs cam_periph_freeze_after_event(struct cam_periph *periph,
125487cfaf0eSJustin T. Gibbs 			      struct timeval* event_time, u_int duration_ms)
125587cfaf0eSJustin T. Gibbs {
125687cfaf0eSJustin T. Gibbs 	struct timeval delta;
125787cfaf0eSJustin T. Gibbs 	struct timeval duration_tv;
125887cfaf0eSJustin T. Gibbs 
1259f8ff57d2SAlexander Motin 	if (!timevalisset(event_time))
1260f8ff57d2SAlexander Motin 		return;
1261f8ff57d2SAlexander Motin 
126287cfaf0eSJustin T. Gibbs 	microtime(&delta);
126387cfaf0eSJustin T. Gibbs 	timevalsub(&delta, event_time);
126487cfaf0eSJustin T. Gibbs 	duration_tv.tv_sec = duration_ms / 1000;
126587cfaf0eSJustin T. Gibbs 	duration_tv.tv_usec = (duration_ms % 1000) * 1000;
126687cfaf0eSJustin T. Gibbs 	if (timevalcmp(&delta, &duration_tv, <)) {
126787cfaf0eSJustin T. Gibbs 		timevalsub(&duration_tv, &delta);
126887cfaf0eSJustin T. Gibbs 
126987cfaf0eSJustin T. Gibbs 		duration_ms = duration_tv.tv_sec * 1000;
127087cfaf0eSJustin T. Gibbs 		duration_ms += duration_tv.tv_usec / 1000;
127187cfaf0eSJustin T. Gibbs 		cam_freeze_devq(periph->path);
127287cfaf0eSJustin T. Gibbs 		cam_release_devq(periph->path,
127387cfaf0eSJustin T. Gibbs 				RELSIM_RELEASE_AFTER_TIMEOUT,
127487cfaf0eSJustin T. Gibbs 				/*reduction*/0,
127587cfaf0eSJustin T. Gibbs 				/*timeout*/duration_ms,
127687cfaf0eSJustin T. Gibbs 				/*getcount_only*/0);
127787cfaf0eSJustin T. Gibbs 	}
127887cfaf0eSJustin T. Gibbs 
127987cfaf0eSJustin T. Gibbs }
128087cfaf0eSJustin T. Gibbs 
12813393f8daSKenneth D. Merry static int
12820191d9b3SAlexander Motin camperiphscsistatuserror(union ccb *ccb, union ccb **orig_ccb,
12830191d9b3SAlexander Motin     cam_flags camflags, u_int32_t sense_flags,
12843393f8daSKenneth D. Merry     int *openings, u_int32_t *relsim_flags,
12850181d54bSAlexander Motin     u_int32_t *timeout, u_int32_t *action, const char **action_string)
12868b8a9b1dSJustin T. Gibbs {
12878b8a9b1dSJustin T. Gibbs 	int error;
12888b8a9b1dSJustin T. Gibbs 
12898b8a9b1dSJustin T. Gibbs 	switch (ccb->csio.scsi_status) {
12908b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_OK:
12918b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_COND_MET:
12928b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_INTERMED:
12938b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_INTERMED_COND_MET:
12948b8a9b1dSJustin T. Gibbs 		error = 0;
12958b8a9b1dSJustin T. Gibbs 		break;
12968b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_CMD_TERMINATED:
12978b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_CHECK_COND:
12980191d9b3SAlexander Motin 		error = camperiphscsisenseerror(ccb, orig_ccb,
12993393f8daSKenneth D. Merry 					        camflags,
13008b8a9b1dSJustin T. Gibbs 					        sense_flags,
13013393f8daSKenneth D. Merry 					        openings,
13023393f8daSKenneth D. Merry 					        relsim_flags,
130383c5d981SAlexander Motin 					        timeout,
13040181d54bSAlexander Motin 					        action,
130583c5d981SAlexander Motin 					        action_string);
13068b8a9b1dSJustin T. Gibbs 		break;
13078b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_QUEUE_FULL:
13088b8a9b1dSJustin T. Gibbs 	{
13098b8a9b1dSJustin T. Gibbs 		/* no decrement */
131082815562SJustin T. Gibbs 		struct ccb_getdevstats cgds;
13118b8a9b1dSJustin T. Gibbs 
13128b8a9b1dSJustin T. Gibbs 		/*
13138b8a9b1dSJustin T. Gibbs 		 * First off, find out what the current
13148b8a9b1dSJustin T. Gibbs 		 * transaction counts are.
13158b8a9b1dSJustin T. Gibbs 		 */
131682815562SJustin T. Gibbs 		xpt_setup_ccb(&cgds.ccb_h,
13178b8a9b1dSJustin T. Gibbs 			      ccb->ccb_h.path,
1318bbfa4aa1SAlexander Motin 			      CAM_PRIORITY_NORMAL);
131982815562SJustin T. Gibbs 		cgds.ccb_h.func_code = XPT_GDEV_STATS;
132082815562SJustin T. Gibbs 		xpt_action((union ccb *)&cgds);
13218b8a9b1dSJustin T. Gibbs 
13228b8a9b1dSJustin T. Gibbs 		/*
13238b8a9b1dSJustin T. Gibbs 		 * If we were the only transaction active, treat
13248b8a9b1dSJustin T. Gibbs 		 * the QUEUE FULL as if it were a BUSY condition.
13258b8a9b1dSJustin T. Gibbs 		 */
132682815562SJustin T. Gibbs 		if (cgds.dev_active != 0) {
132782815562SJustin T. Gibbs 			int total_openings;
132882815562SJustin T. Gibbs 
13298b8a9b1dSJustin T. Gibbs 			/*
13308b8a9b1dSJustin T. Gibbs 		 	 * Reduce the number of openings to
13318b8a9b1dSJustin T. Gibbs 			 * be 1 less than the amount it took
13328b8a9b1dSJustin T. Gibbs 			 * to get a queue full bounded by the
13338b8a9b1dSJustin T. Gibbs 			 * minimum allowed tag count for this
13348b8a9b1dSJustin T. Gibbs 			 * device.
13358b8a9b1dSJustin T. Gibbs 		 	 */
13363393f8daSKenneth D. Merry 			total_openings = cgds.dev_active + cgds.dev_openings;
13373393f8daSKenneth D. Merry 			*openings = cgds.dev_active;
13383393f8daSKenneth D. Merry 			if (*openings < cgds.mintags)
13393393f8daSKenneth D. Merry 				*openings = cgds.mintags;
13403393f8daSKenneth D. Merry 			if (*openings < total_openings)
13413393f8daSKenneth D. Merry 				*relsim_flags = RELSIM_ADJUST_OPENINGS;
13428b8a9b1dSJustin T. Gibbs 			else {
13438b8a9b1dSJustin T. Gibbs 				/*
13448b8a9b1dSJustin T. Gibbs 				 * Some devices report queue full for
13458b8a9b1dSJustin T. Gibbs 				 * temporary resource shortages.  For
13468b8a9b1dSJustin T. Gibbs 				 * this reason, we allow a minimum
13478b8a9b1dSJustin T. Gibbs 				 * tag count to be entered via a
13488b8a9b1dSJustin T. Gibbs 				 * quirk entry to prevent the queue
13498b8a9b1dSJustin T. Gibbs 				 * count on these devices from falling
13508b8a9b1dSJustin T. Gibbs 				 * to a pessimisticly low value.  We
13518b8a9b1dSJustin T. Gibbs 				 * still wait for the next successful
13528b8a9b1dSJustin T. Gibbs 				 * completion, however, before queueing
13538b8a9b1dSJustin T. Gibbs 				 * more transactions to the device.
13548b8a9b1dSJustin T. Gibbs 				 */
13553393f8daSKenneth D. Merry 				*relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
13568b8a9b1dSJustin T. Gibbs 			}
13573393f8daSKenneth D. Merry 			*timeout = 0;
13588b8a9b1dSJustin T. Gibbs 			error = ERESTART;
13590181d54bSAlexander Motin 			*action &= ~SSQ_PRINT_SENSE;
13608b8a9b1dSJustin T. Gibbs 			break;
13618b8a9b1dSJustin T. Gibbs 		}
13628b8a9b1dSJustin T. Gibbs 		/* FALLTHROUGH */
13638b8a9b1dSJustin T. Gibbs 	}
13648b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_BUSY:
13658b8a9b1dSJustin T. Gibbs 		/*
13668b8a9b1dSJustin T. Gibbs 		 * Restart the queue after either another
13678b8a9b1dSJustin T. Gibbs 		 * command completes or a 1 second timeout.
13688b8a9b1dSJustin T. Gibbs 		 */
13693393f8daSKenneth D. Merry 	 	if (ccb->ccb_h.retry_count > 0) {
1370af51b059SMatt Jacob 	 		ccb->ccb_h.retry_count--;
13718b8a9b1dSJustin T. Gibbs 			error = ERESTART;
13723393f8daSKenneth D. Merry 			*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
13738b8a9b1dSJustin T. Gibbs 				      | RELSIM_RELEASE_AFTER_CMDCMPLT;
13743393f8daSKenneth D. Merry 			*timeout = 1000;
1375af51b059SMatt Jacob 		} else {
1376af51b059SMatt Jacob 			error = EIO;
1377af51b059SMatt Jacob 		}
13788b8a9b1dSJustin T. Gibbs 		break;
13798b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_RESERV_CONFLICT:
13808b8a9b1dSJustin T. Gibbs 	default:
13818b8a9b1dSJustin T. Gibbs 		error = EIO;
13828b8a9b1dSJustin T. Gibbs 		break;
13838b8a9b1dSJustin T. Gibbs 	}
13843393f8daSKenneth D. Merry 	return (error);
13853393f8daSKenneth D. Merry }
13863393f8daSKenneth D. Merry 
13873393f8daSKenneth D. Merry static int
13880191d9b3SAlexander Motin camperiphscsisenseerror(union ccb *ccb, union ccb **orig,
13890191d9b3SAlexander Motin     cam_flags camflags, u_int32_t sense_flags,
13903393f8daSKenneth D. Merry     int *openings, u_int32_t *relsim_flags,
13910181d54bSAlexander Motin     u_int32_t *timeout, u_int32_t *action, const char **action_string)
13923393f8daSKenneth D. Merry {
13933393f8daSKenneth D. Merry 	struct cam_periph *periph;
139483c5d981SAlexander Motin 	union ccb *orig_ccb = ccb;
13950191d9b3SAlexander Motin 	int error, recoveryccb;
13963393f8daSKenneth D. Merry 
13973393f8daSKenneth D. Merry 	periph = xpt_path_periph(ccb->ccb_h.path);
13980191d9b3SAlexander Motin 	recoveryccb = (ccb->ccb_h.cbfcnp == camperiphdone);
13990191d9b3SAlexander Motin 	if ((periph->flags & CAM_PERIPH_RECOVERY_INPROG) && !recoveryccb) {
14003393f8daSKenneth D. Merry 		/*
14013393f8daSKenneth D. Merry 		 * If error recovery is already in progress, don't attempt
14023393f8daSKenneth D. Merry 		 * to process this error, but requeue it unconditionally
14033393f8daSKenneth D. Merry 		 * and attempt to process it once error recovery has
14043393f8daSKenneth D. Merry 		 * completed.  This failed command is probably related to
14053393f8daSKenneth D. Merry 		 * the error that caused the currently active error recovery
14063393f8daSKenneth D. Merry 		 * action so our  current recovery efforts should also
14073393f8daSKenneth D. Merry 		 * address this command.  Be aware that the error recovery
14083393f8daSKenneth D. Merry 		 * code assumes that only one recovery action is in progress
14093393f8daSKenneth D. Merry 		 * on a particular peripheral instance at any given time
14103393f8daSKenneth D. Merry 		 * (e.g. only one saved CCB for error recovery) so it is
14113393f8daSKenneth D. Merry 		 * imperitive that we don't violate this assumption.
14123393f8daSKenneth D. Merry 		 */
14133393f8daSKenneth D. Merry 		error = ERESTART;
14140181d54bSAlexander Motin 		*action &= ~SSQ_PRINT_SENSE;
14153393f8daSKenneth D. Merry 	} else {
14163393f8daSKenneth D. Merry 		scsi_sense_action err_action;
14173393f8daSKenneth D. Merry 		struct ccb_getdev cgd;
14183393f8daSKenneth D. Merry 
14193393f8daSKenneth D. Merry 		/*
14203393f8daSKenneth D. Merry 		 * Grab the inquiry data for this device.
14213393f8daSKenneth D. Merry 		 */
1422bbfa4aa1SAlexander Motin 		xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, CAM_PRIORITY_NORMAL);
14233393f8daSKenneth D. Merry 		cgd.ccb_h.func_code = XPT_GDEV_TYPE;
14243393f8daSKenneth D. Merry 		xpt_action((union ccb *)&cgd);
14253393f8daSKenneth D. Merry 
1426e7493b28SAlexander Motin 		err_action = scsi_error_action(&ccb->csio, &cgd.inq_data,
14273393f8daSKenneth D. Merry 		    sense_flags);
14283393f8daSKenneth D. Merry 		error = err_action & SS_ERRMASK;
14293393f8daSKenneth D. Merry 
14303393f8daSKenneth D. Merry 		/*
14310191d9b3SAlexander Motin 		 * Do not autostart sequential access devices
14320191d9b3SAlexander Motin 		 * to avoid unexpected tape loading.
14330191d9b3SAlexander Motin 		 */
14340191d9b3SAlexander Motin 		if ((err_action & SS_MASK) == SS_START &&
14350191d9b3SAlexander Motin 		    SID_TYPE(&cgd.inq_data) == T_SEQUENTIAL) {
14360191d9b3SAlexander Motin 			*action_string = "Will not autostart a "
14370191d9b3SAlexander Motin 			    "sequential access device";
14380191d9b3SAlexander Motin 			goto sense_error_done;
14390191d9b3SAlexander Motin 		}
14400191d9b3SAlexander Motin 
14410191d9b3SAlexander Motin 		/*
14420191d9b3SAlexander Motin 		 * Avoid recovery recursion if recovery action is the same.
14430191d9b3SAlexander Motin 		 */
14440191d9b3SAlexander Motin 		if ((err_action & SS_MASK) >= SS_START && recoveryccb) {
14450191d9b3SAlexander Motin 			if (((err_action & SS_MASK) == SS_START &&
14460191d9b3SAlexander Motin 			     ccb->csio.cdb_io.cdb_bytes[0] == START_STOP_UNIT) ||
14470191d9b3SAlexander Motin 			    ((err_action & SS_MASK) == SS_TUR &&
14480191d9b3SAlexander Motin 			     (ccb->csio.cdb_io.cdb_bytes[0] == TEST_UNIT_READY))) {
14490191d9b3SAlexander Motin 				err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
14500191d9b3SAlexander Motin 				*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
14510191d9b3SAlexander Motin 				*timeout = 500;
14520191d9b3SAlexander Motin 			}
14530191d9b3SAlexander Motin 		}
14540191d9b3SAlexander Motin 
14550191d9b3SAlexander Motin 		/*
14563393f8daSKenneth D. Merry 		 * If the recovery action will consume a retry,
14573393f8daSKenneth D. Merry 		 * make sure we actually have retries available.
14583393f8daSKenneth D. Merry 		 */
14593393f8daSKenneth D. Merry 		if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
1460a07e846bSAlexander Motin 		 	if (ccb->ccb_h.retry_count > 0 &&
1461a07e846bSAlexander Motin 			    (periph->flags & CAM_PERIPH_INVALID) == 0)
14623393f8daSKenneth D. Merry 		 		ccb->ccb_h.retry_count--;
14633393f8daSKenneth D. Merry 			else {
146483c5d981SAlexander Motin 				*action_string = "Retries exhausted";
14653393f8daSKenneth D. Merry 				goto sense_error_done;
14663393f8daSKenneth D. Merry 			}
14673393f8daSKenneth D. Merry 		}
14683393f8daSKenneth D. Merry 
14693393f8daSKenneth D. Merry 		if ((err_action & SS_MASK) >= SS_START) {
14703393f8daSKenneth D. Merry 			/*
14713393f8daSKenneth D. Merry 			 * Do common portions of commands that
14723393f8daSKenneth D. Merry 			 * use recovery CCBs.
14733393f8daSKenneth D. Merry 			 */
147483c5d981SAlexander Motin 			orig_ccb = xpt_alloc_ccb_nowait();
147583c5d981SAlexander Motin 			if (orig_ccb == NULL) {
147683c5d981SAlexander Motin 				*action_string = "Can't allocate recovery CCB";
14773393f8daSKenneth D. Merry 				goto sense_error_done;
14783393f8daSKenneth D. Merry 			}
14791f1158b2SAlexander Motin 			/*
14801f1158b2SAlexander Motin 			 * Clear freeze flag for original request here, as
14811f1158b2SAlexander Motin 			 * this freeze will be dropped as part of ERESTART.
14821f1158b2SAlexander Motin 			 */
14831f1158b2SAlexander Motin 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
148483c5d981SAlexander Motin 			bcopy(ccb, orig_ccb, sizeof(*orig_ccb));
14853393f8daSKenneth D. Merry 		}
14863393f8daSKenneth D. Merry 
14873393f8daSKenneth D. Merry 		switch (err_action & SS_MASK) {
14883393f8daSKenneth D. Merry 		case SS_NOP:
148983c5d981SAlexander Motin 			*action_string = "No recovery action needed";
149000e54d14SKenneth D. Merry 			error = 0;
149100e54d14SKenneth D. Merry 			break;
14923393f8daSKenneth D. Merry 		case SS_RETRY:
149383c5d981SAlexander Motin 			*action_string = "Retrying command (per sense data)";
14943393f8daSKenneth D. Merry 			error = ERESTART;
14958b8a9b1dSJustin T. Gibbs 			break;
14963393f8daSKenneth D. Merry 		case SS_FAIL:
149783c5d981SAlexander Motin 			*action_string = "Unretryable error";
14983393f8daSKenneth D. Merry 			break;
14993393f8daSKenneth D. Merry 		case SS_START:
15003393f8daSKenneth D. Merry 		{
15013393f8daSKenneth D. Merry 			int le;
15023393f8daSKenneth D. Merry 
15033393f8daSKenneth D. Merry 			/*
15043393f8daSKenneth D. Merry 			 * Send a start unit command to the device, and
15053393f8daSKenneth D. Merry 			 * then retry the command.
15063393f8daSKenneth D. Merry 			 */
150783c5d981SAlexander Motin 			*action_string = "Attempting to start unit";
150883c5d981SAlexander Motin 			periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
15093393f8daSKenneth D. Merry 
15103393f8daSKenneth D. Merry 			/*
15113393f8daSKenneth D. Merry 			 * Check for removable media and set
15123393f8daSKenneth D. Merry 			 * load/eject flag appropriately.
15133393f8daSKenneth D. Merry 			 */
15143393f8daSKenneth D. Merry 			if (SID_IS_REMOVABLE(&cgd.inq_data))
15153393f8daSKenneth D. Merry 				le = TRUE;
15163393f8daSKenneth D. Merry 			else
15173393f8daSKenneth D. Merry 				le = FALSE;
15183393f8daSKenneth D. Merry 
15193393f8daSKenneth D. Merry 			scsi_start_stop(&ccb->csio,
15203393f8daSKenneth D. Merry 					/*retries*/1,
15213393f8daSKenneth D. Merry 					camperiphdone,
15223393f8daSKenneth D. Merry 					MSG_SIMPLE_Q_TAG,
15233393f8daSKenneth D. Merry 					/*start*/TRUE,
15243393f8daSKenneth D. Merry 					/*load/eject*/le,
15253393f8daSKenneth D. Merry 					/*immediate*/FALSE,
15263393f8daSKenneth D. Merry 					SSD_FULL_SIZE,
15273393f8daSKenneth D. Merry 					/*timeout*/50000);
15283393f8daSKenneth D. Merry 			break;
15293393f8daSKenneth D. Merry 		}
15303393f8daSKenneth D. Merry 		case SS_TUR:
15313393f8daSKenneth D. Merry 		{
15323393f8daSKenneth D. Merry 			/*
15333393f8daSKenneth D. Merry 			 * Send a Test Unit Ready to the device.
15343393f8daSKenneth D. Merry 			 * If the 'many' flag is set, we send 120
15353393f8daSKenneth D. Merry 			 * test unit ready commands, one every half
15363393f8daSKenneth D. Merry 			 * second.  Otherwise, we just send one TUR.
15373393f8daSKenneth D. Merry 			 * We only want to do this if the retry
15383393f8daSKenneth D. Merry 			 * count has not been exhausted.
15393393f8daSKenneth D. Merry 			 */
15403393f8daSKenneth D. Merry 			int retries;
15413393f8daSKenneth D. Merry 
15423393f8daSKenneth D. Merry 			if ((err_action & SSQ_MANY) != 0) {
154383c5d981SAlexander Motin 				*action_string = "Polling device for readiness";
15443393f8daSKenneth D. Merry 				retries = 120;
15453393f8daSKenneth D. Merry 			} else {
154683c5d981SAlexander Motin 				*action_string = "Testing device for readiness";
15473393f8daSKenneth D. Merry 				retries = 1;
15483393f8daSKenneth D. Merry 			}
154983c5d981SAlexander Motin 			periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
15503393f8daSKenneth D. Merry 			scsi_test_unit_ready(&ccb->csio,
15513393f8daSKenneth D. Merry 					     retries,
15523393f8daSKenneth D. Merry 					     camperiphdone,
15533393f8daSKenneth D. Merry 					     MSG_SIMPLE_Q_TAG,
15543393f8daSKenneth D. Merry 					     SSD_FULL_SIZE,
15553393f8daSKenneth D. Merry 					     /*timeout*/5000);
15563393f8daSKenneth D. Merry 
15573393f8daSKenneth D. Merry 			/*
15583393f8daSKenneth D. Merry 			 * Accomplish our 500ms delay by deferring
15593393f8daSKenneth D. Merry 			 * the release of our device queue appropriately.
15603393f8daSKenneth D. Merry 			 */
15613393f8daSKenneth D. Merry 			*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
15623393f8daSKenneth D. Merry 			*timeout = 500;
15633393f8daSKenneth D. Merry 			break;
15643393f8daSKenneth D. Merry 		}
15653393f8daSKenneth D. Merry 		default:
1566e3c29144SWarner Losh 			panic("Unhandled error action %x", err_action);
15673393f8daSKenneth D. Merry 		}
15683393f8daSKenneth D. Merry 
15693393f8daSKenneth D. Merry 		if ((err_action & SS_MASK) >= SS_START) {
15703393f8daSKenneth D. Merry 			/*
1571bbfa4aa1SAlexander Motin 			 * Drop the priority, so that the recovery
15723393f8daSKenneth D. Merry 			 * CCB is the first to execute.  Freeze the queue
15733393f8daSKenneth D. Merry 			 * after this command is sent so that we can
15743393f8daSKenneth D. Merry 			 * restore the old csio and have it queued in
15753393f8daSKenneth D. Merry 			 * the proper order before we release normal
15763393f8daSKenneth D. Merry 			 * transactions to the device.
15773393f8daSKenneth D. Merry 			 */
157883c5d981SAlexander Motin 			ccb->ccb_h.pinfo.priority--;
15793393f8daSKenneth D. Merry 			ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
158083c5d981SAlexander Motin 			ccb->ccb_h.saved_ccb_ptr = orig_ccb;
15813393f8daSKenneth D. Merry 			error = ERESTART;
15820191d9b3SAlexander Motin 			*orig = orig_ccb;
15833393f8daSKenneth D. Merry 		}
15843393f8daSKenneth D. Merry 
15853393f8daSKenneth D. Merry sense_error_done:
15860181d54bSAlexander Motin 		*action = err_action;
15873393f8daSKenneth D. Merry 	}
15883393f8daSKenneth D. Merry 	return (error);
15893393f8daSKenneth D. Merry }
15903393f8daSKenneth D. Merry 
15913393f8daSKenneth D. Merry /*
15923393f8daSKenneth D. Merry  * Generic error handler.  Peripheral drivers usually filter
15933393f8daSKenneth D. Merry  * out the errors that they handle in a unique mannor, then
15943393f8daSKenneth D. Merry  * call this function.
15953393f8daSKenneth D. Merry  */
15963393f8daSKenneth D. Merry int
15973393f8daSKenneth D. Merry cam_periph_error(union ccb *ccb, cam_flags camflags,
15983393f8daSKenneth D. Merry 		 u_int32_t sense_flags, union ccb *save_ccb)
15993393f8daSKenneth D. Merry {
16000181d54bSAlexander Motin 	struct cam_path *newpath;
16010181d54bSAlexander Motin 	union ccb  *orig_ccb, *scan_ccb;
1602a07e846bSAlexander Motin 	struct cam_periph *periph;
16033393f8daSKenneth D. Merry 	const char *action_string;
16043393f8daSKenneth D. Merry 	cam_status  status;
16050181d54bSAlexander Motin 	int	    frozen, error, openings;
16060181d54bSAlexander Motin 	u_int32_t   action, relsim_flags, timeout;
16073393f8daSKenneth D. Merry 
16080181d54bSAlexander Motin 	action = SSQ_PRINT_SENSE;
1609a07e846bSAlexander Motin 	periph = xpt_path_periph(ccb->ccb_h.path);
16103393f8daSKenneth D. Merry 	action_string = NULL;
16113393f8daSKenneth D. Merry 	status = ccb->ccb_h.status;
16123393f8daSKenneth D. Merry 	frozen = (status & CAM_DEV_QFRZN) != 0;
16133393f8daSKenneth D. Merry 	status &= CAM_STATUS_MASK;
16140181d54bSAlexander Motin 	openings = relsim_flags = timeout = 0;
16150191d9b3SAlexander Motin 	orig_ccb = ccb;
16163393f8daSKenneth D. Merry 
16173393f8daSKenneth D. Merry 	switch (status) {
16183393f8daSKenneth D. Merry 	case CAM_REQ_CMP:
16193393f8daSKenneth D. Merry 		error = 0;
16200181d54bSAlexander Motin 		action &= ~SSQ_PRINT_SENSE;
16213393f8daSKenneth D. Merry 		break;
16223393f8daSKenneth D. Merry 	case CAM_SCSI_STATUS_ERROR:
16230191d9b3SAlexander Motin 		error = camperiphscsistatuserror(ccb, &orig_ccb,
16240191d9b3SAlexander Motin 		    camflags, sense_flags, &openings, &relsim_flags,
16250181d54bSAlexander Motin 		    &timeout, &action, &action_string);
16263393f8daSKenneth D. Merry 		break;
16273393f8daSKenneth D. Merry 	case CAM_AUTOSENSE_FAIL:
1628c7ec4390SMatt Jacob 		error = EIO;	/* we have to kill the command */
1629c7ec4390SMatt Jacob 		break;
16308b8a9b1dSJustin T. Gibbs 	case CAM_UA_ABORT:
16318b8a9b1dSJustin T. Gibbs 	case CAM_UA_TERMIO:
16328b8a9b1dSJustin T. Gibbs 	case CAM_MSG_REJECT_REC:
16338b8a9b1dSJustin T. Gibbs 		/* XXX Don't know that these are correct */
16348b8a9b1dSJustin T. Gibbs 		error = EIO;
16358b8a9b1dSJustin T. Gibbs 		break;
16368b8a9b1dSJustin T. Gibbs 	case CAM_SEL_TIMEOUT:
16373393f8daSKenneth D. Merry 		if ((camflags & CAM_RETRY_SELTO) != 0) {
1638a07e846bSAlexander Motin 			if (ccb->ccb_h.retry_count > 0 &&
1639a07e846bSAlexander Motin 			    (periph->flags & CAM_PERIPH_INVALID) == 0) {
16403393f8daSKenneth D. Merry 				ccb->ccb_h.retry_count--;
16413393f8daSKenneth D. Merry 				error = ERESTART;
16423393f8daSKenneth D. Merry 
16433393f8daSKenneth D. Merry 				/*
164473cf209fSMatt Jacob 				 * Wait a bit to give the device
16453393f8daSKenneth D. Merry 				 * time to recover before we try again.
16463393f8daSKenneth D. Merry 				 */
16473393f8daSKenneth D. Merry 				relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
164873cf209fSMatt Jacob 				timeout = periph_selto_delay;
16493393f8daSKenneth D. Merry 				break;
16503393f8daSKenneth D. Merry 			}
1651a07e846bSAlexander Motin 			action_string = "Retries exhausted";
16523393f8daSKenneth D. Merry 		}
16538900f4b8SKenneth D. Merry 		/* FALLTHROUGH */
16548900f4b8SKenneth D. Merry 	case CAM_DEV_NOT_THERE:
16553393f8daSKenneth D. Merry 		error = ENXIO;
16560181d54bSAlexander Motin 		action = SSQ_LOST;
16578b8a9b1dSJustin T. Gibbs 		break;
16588b8a9b1dSJustin T. Gibbs 	case CAM_REQ_INVALID:
16598b8a9b1dSJustin T. Gibbs 	case CAM_PATH_INVALID:
16608b8a9b1dSJustin T. Gibbs 	case CAM_NO_HBA:
16618b8a9b1dSJustin T. Gibbs 	case CAM_PROVIDE_FAIL:
16628b8a9b1dSJustin T. Gibbs 	case CAM_REQ_TOO_BIG:
16639a014e6fSIan Dowse 	case CAM_LUN_INVALID:
16649a014e6fSIan Dowse 	case CAM_TID_INVALID:
16658b8a9b1dSJustin T. Gibbs 		error = EINVAL;
16668b8a9b1dSJustin T. Gibbs 		break;
16678b8a9b1dSJustin T. Gibbs 	case CAM_SCSI_BUS_RESET:
16688b8a9b1dSJustin T. Gibbs 	case CAM_BDR_SENT:
16693393f8daSKenneth D. Merry 		/*
16703393f8daSKenneth D. Merry 		 * Commands that repeatedly timeout and cause these
16713393f8daSKenneth D. Merry 		 * kinds of error recovery actions, should return
16723393f8daSKenneth D. Merry 		 * CAM_CMD_TIMEOUT, which allows us to safely assume
16733393f8daSKenneth D. Merry 		 * that this command was an innocent bystander to
16743393f8daSKenneth D. Merry 		 * these events and should be unconditionally
16753393f8daSKenneth D. Merry 		 * retried.
16763393f8daSKenneth D. Merry 		 */
16778b8a9b1dSJustin T. Gibbs 	case CAM_REQUEUE_REQ:
16780191d9b3SAlexander Motin 		/* Unconditional requeue if device is still there */
16790191d9b3SAlexander Motin 		if (periph->flags & CAM_PERIPH_INVALID) {
16800191d9b3SAlexander Motin 			action_string = "Periph was invalidated";
1681a07e846bSAlexander Motin 			error = EIO;
16820191d9b3SAlexander Motin 		} else if (sense_flags & SF_NO_RETRY) {
16830191d9b3SAlexander Motin 			error = EIO;
16840191d9b3SAlexander Motin 			action_string = "Retry was blocked";
168584c4de2dSAlexander Motin 		} else {
16860191d9b3SAlexander Motin 			error = ERESTART;
16870181d54bSAlexander Motin 			action &= ~SSQ_PRINT_SENSE;
168884c4de2dSAlexander Motin 		}
16898b8a9b1dSJustin T. Gibbs 		break;
16908b8a9b1dSJustin T. Gibbs 	case CAM_RESRC_UNAVAIL:
169173cf209fSMatt Jacob 		/* Wait a bit for the resource shortage to abate. */
169273cf209fSMatt Jacob 		timeout = periph_noresrc_delay;
169373cf209fSMatt Jacob 		/* FALLTHROUGH */
16948b8a9b1dSJustin T. Gibbs 	case CAM_BUSY:
169573cf209fSMatt Jacob 		if (timeout == 0) {
169673cf209fSMatt Jacob 			/* Wait a bit for the busy condition to abate. */
169773cf209fSMatt Jacob 			timeout = periph_busy_delay;
169873cf209fSMatt Jacob 		}
169973cf209fSMatt Jacob 		relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
170073cf209fSMatt Jacob 		/* FALLTHROUGH */
17010191d9b3SAlexander Motin 	case CAM_ATA_STATUS_ERROR:
17020191d9b3SAlexander Motin 	case CAM_REQ_CMP_ERR:
17030191d9b3SAlexander Motin 	case CAM_CMD_TIMEOUT:
17040191d9b3SAlexander Motin 	case CAM_UNEXP_BUSFREE:
17050191d9b3SAlexander Motin 	case CAM_UNCOR_PARITY:
17060191d9b3SAlexander Motin 	case CAM_DATA_RUN_ERR:
17078b8a9b1dSJustin T. Gibbs 	default:
17080191d9b3SAlexander Motin 		if (periph->flags & CAM_PERIPH_INVALID) {
17090191d9b3SAlexander Motin 			error = EIO;
17100191d9b3SAlexander Motin 			action_string = "Periph was invalidated";
17110191d9b3SAlexander Motin 		} else if (ccb->ccb_h.retry_count == 0) {
17128b8a9b1dSJustin T. Gibbs 			error = EIO;
171383c5d981SAlexander Motin 			action_string = "Retries exhausted";
17140191d9b3SAlexander Motin 		} else if (sense_flags & SF_NO_RETRY) {
17150191d9b3SAlexander Motin 			error = EIO;
17160191d9b3SAlexander Motin 			action_string = "Retry was blocked";
17170191d9b3SAlexander Motin 		} else {
17180191d9b3SAlexander Motin 			ccb->ccb_h.retry_count--;
17190191d9b3SAlexander Motin 			error = ERESTART;
17208b8a9b1dSJustin T. Gibbs 		}
17218b8a9b1dSJustin T. Gibbs 		break;
17228b8a9b1dSJustin T. Gibbs 	}
17238b8a9b1dSJustin T. Gibbs 
17240191d9b3SAlexander Motin 	if ((sense_flags & SF_PRINT_ALWAYS) ||
17250191d9b3SAlexander Motin 	    CAM_DEBUGGED(ccb->ccb_h.path, CAM_DEBUG_INFO))
17260181d54bSAlexander Motin 		action |= SSQ_PRINT_SENSE;
17270191d9b3SAlexander Motin 	else if (sense_flags & SF_NO_PRINT)
17280181d54bSAlexander Motin 		action &= ~SSQ_PRINT_SENSE;
17290181d54bSAlexander Motin 	if ((action & SSQ_PRINT_SENSE) != 0)
17300191d9b3SAlexander Motin 		cam_error_print(orig_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
17310181d54bSAlexander Motin 	if (error != 0 && (action & SSQ_PRINT_SENSE) != 0) {
17320f3e2159SAlexander Motin 		if (error != ERESTART) {
17333393f8daSKenneth D. Merry 			if (action_string == NULL)
173483c5d981SAlexander Motin 				action_string = "Unretryable error";
173583c5d981SAlexander Motin 			xpt_print(ccb->ccb_h.path, "Error %d, %s\n",
173683c5d981SAlexander Motin 			    error, action_string);
173783c5d981SAlexander Motin 		} else if (action_string != NULL)
1738f0d9af51SMatt Jacob 			xpt_print(ccb->ccb_h.path, "%s\n", action_string);
173983c5d981SAlexander Motin 		else
174083c5d981SAlexander Motin 			xpt_print(ccb->ccb_h.path, "Retrying command\n");
17410f3e2159SAlexander Motin 	}
17420f3e2159SAlexander Motin 
17430181d54bSAlexander Motin 	if ((action & SSQ_LOST) != 0) {
17440191d9b3SAlexander Motin 		lun_id_t lun_id;
17450191d9b3SAlexander Motin 
17460191d9b3SAlexander Motin 		/*
17470191d9b3SAlexander Motin 		 * For a selection timeout, we consider all of the LUNs on
17480191d9b3SAlexander Motin 		 * the target to be gone.  If the status is CAM_DEV_NOT_THERE,
17490191d9b3SAlexander Motin 		 * then we only get rid of the device(s) specified by the
17500191d9b3SAlexander Motin 		 * path in the original CCB.
17510191d9b3SAlexander Motin 		 */
17520181d54bSAlexander Motin 		if (status == CAM_SEL_TIMEOUT)
17530191d9b3SAlexander Motin 			lun_id = CAM_LUN_WILDCARD;
17540181d54bSAlexander Motin 		else
17550181d54bSAlexander Motin 			lun_id = xpt_path_lun_id(ccb->ccb_h.path);
17560191d9b3SAlexander Motin 
17570191d9b3SAlexander Motin 		/* Should we do more if we can't create the path?? */
17580191d9b3SAlexander Motin 		if (xpt_create_path(&newpath, periph,
17590191d9b3SAlexander Motin 				    xpt_path_path_id(ccb->ccb_h.path),
17600191d9b3SAlexander Motin 				    xpt_path_target_id(ccb->ccb_h.path),
17610191d9b3SAlexander Motin 				    lun_id) == CAM_REQ_CMP) {
17620191d9b3SAlexander Motin 
17630191d9b3SAlexander Motin 			/*
17640191d9b3SAlexander Motin 			 * Let peripheral drivers know that this
17650191d9b3SAlexander Motin 			 * device has gone away.
17660191d9b3SAlexander Motin 			 */
17670191d9b3SAlexander Motin 			xpt_async(AC_LOST_DEVICE, newpath, NULL);
17680191d9b3SAlexander Motin 			xpt_free_path(newpath);
17690191d9b3SAlexander Motin 		}
17700181d54bSAlexander Motin 	}
17713631c638SAlexander Motin 
17723631c638SAlexander Motin 	/* Broadcast UNIT ATTENTIONs to all periphs. */
17730181d54bSAlexander Motin 	if ((action & SSQ_UA) != 0)
17743631c638SAlexander Motin 		xpt_async(AC_UNIT_ATTENTION, orig_ccb->ccb_h.path, orig_ccb);
17750181d54bSAlexander Motin 
17760181d54bSAlexander Motin 	/* Rescan target on "Reported LUNs data has changed" */
17770181d54bSAlexander Motin 	if ((action & SSQ_RESCAN) != 0) {
17780181d54bSAlexander Motin 		if (xpt_create_path(&newpath, NULL,
17790181d54bSAlexander Motin 				    xpt_path_path_id(ccb->ccb_h.path),
17800181d54bSAlexander Motin 				    xpt_path_target_id(ccb->ccb_h.path),
1781e5736ac8SAlexander Motin 				    CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
17820181d54bSAlexander Motin 
17830181d54bSAlexander Motin 			scan_ccb = xpt_alloc_ccb_nowait();
1784e5736ac8SAlexander Motin 			if (scan_ccb != NULL) {
17850181d54bSAlexander Motin 				scan_ccb->ccb_h.path = newpath;
1786e5736ac8SAlexander Motin 				scan_ccb->ccb_h.func_code = XPT_SCAN_TGT;
17870181d54bSAlexander Motin 				scan_ccb->crcn.flags = 0;
17880181d54bSAlexander Motin 				xpt_rescan(scan_ccb);
1789e5736ac8SAlexander Motin 			} else
1790e5736ac8SAlexander Motin 				xpt_print(newpath,
1791e5736ac8SAlexander Motin 				    "Can't allocate CCB to rescan target\n");
17920181d54bSAlexander Motin 		}
17930191d9b3SAlexander Motin 	}
17940191d9b3SAlexander Motin 
17950f3e2159SAlexander Motin 	/* Attempt a retry */
17960f3e2159SAlexander Motin 	if (error == ERESTART || error == 0) {
17970f3e2159SAlexander Motin 		if (frozen != 0)
17980f3e2159SAlexander Motin 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
17990f3e2159SAlexander Motin 		if (error == ERESTART)
18000f3e2159SAlexander Motin 			xpt_action(ccb);
18010f3e2159SAlexander Motin 		if (frozen != 0)
18020f3e2159SAlexander Motin 			cam_release_devq(ccb->ccb_h.path,
18030f3e2159SAlexander Motin 					 relsim_flags,
18040f3e2159SAlexander Motin 					 openings,
18050f3e2159SAlexander Motin 					 timeout,
18060f3e2159SAlexander Motin 					 /*getcount_only*/0);
18073393f8daSKenneth D. Merry 	}
18088b8a9b1dSJustin T. Gibbs 
18098b8a9b1dSJustin T. Gibbs 	return (error);
18108b8a9b1dSJustin T. Gibbs }
1811