xref: /freebsd/sys/cam/cam_periph.c (revision f1e2546a8c3744e4d8644f967117f9d51fbf5678)
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>
380ec81012SJohn Polstra #include <sys/linker_set.h>
399626b608SPoul-Henning Kamp #include <sys/bio.h>
40f34fa851SJohn Baldwin #include <sys/lock.h>
41f34fa851SJohn Baldwin #include <sys/mutex.h>
428b8a9b1dSJustin T. Gibbs #include <sys/buf.h>
438b8a9b1dSJustin T. Gibbs #include <sys/proc.h>
448b8a9b1dSJustin T. Gibbs #include <sys/devicestat.h>
4575f51904SPeter Wemm #include <sys/bus.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,
723393f8daSKenneth D. Merry 						 cam_flags camflags,
733393f8daSKenneth D. Merry 						 u_int32_t sense_flags,
743393f8daSKenneth D. Merry 						 int *openings,
753393f8daSKenneth D. Merry 						 u_int32_t *relsim_flags,
7683c5d981SAlexander Motin 						 u_int32_t *timeout,
7783c5d981SAlexander Motin 						 const char **action_string);
783393f8daSKenneth D. Merry static	int		camperiphscsisenseerror(union ccb *ccb,
793393f8daSKenneth D. Merry 					        cam_flags camflags,
803393f8daSKenneth D. Merry 					        u_int32_t sense_flags,
813393f8daSKenneth D. Merry 					        int *openings,
823393f8daSKenneth D. Merry 					        u_int32_t *relsim_flags,
8383c5d981SAlexander Motin 					        u_int32_t *timeout,
8483c5d981SAlexander Motin 					        const char **action_string);
858b8a9b1dSJustin T. Gibbs 
860b7c27b9SPeter Wemm static int nperiph_drivers;
8783c5d981SAlexander Motin static int initialized = 0;
880b7c27b9SPeter Wemm struct periph_driver **periph_drivers;
890b7c27b9SPeter Wemm 
90362abc44STai-hwa Liang MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers");
91362abc44STai-hwa Liang 
9273cf209fSMatt Jacob static int periph_selto_delay = 1000;
9373cf209fSMatt Jacob TUNABLE_INT("kern.cam.periph_selto_delay", &periph_selto_delay);
9473cf209fSMatt Jacob static int periph_noresrc_delay = 500;
9573cf209fSMatt Jacob TUNABLE_INT("kern.cam.periph_noresrc_delay", &periph_noresrc_delay);
9673cf209fSMatt Jacob static int periph_busy_delay = 500;
9773cf209fSMatt Jacob TUNABLE_INT("kern.cam.periph_busy_delay", &periph_busy_delay);
9873cf209fSMatt Jacob 
9973cf209fSMatt Jacob 
1000b7c27b9SPeter Wemm void
1010b7c27b9SPeter Wemm periphdriver_register(void *data)
1020b7c27b9SPeter Wemm {
10383c5d981SAlexander Motin 	struct periph_driver *drv = (struct periph_driver *)data;
1040b7c27b9SPeter Wemm 	struct periph_driver **newdrivers, **old;
1050b7c27b9SPeter Wemm 	int ndrivers;
1060b7c27b9SPeter Wemm 
1070b7c27b9SPeter Wemm 	ndrivers = nperiph_drivers + 2;
1080dd50e9bSScott Long 	newdrivers = malloc(sizeof(*newdrivers) * ndrivers, M_CAMPERIPH,
1090dd50e9bSScott Long 			    M_WAITOK);
1100b7c27b9SPeter Wemm 	if (periph_drivers)
1110b7c27b9SPeter Wemm 		bcopy(periph_drivers, newdrivers,
112623db360SKenneth D. Merry 		      sizeof(*newdrivers) * nperiph_drivers);
11383c5d981SAlexander Motin 	newdrivers[nperiph_drivers] = drv;
1140b7c27b9SPeter Wemm 	newdrivers[nperiph_drivers + 1] = NULL;
1150b7c27b9SPeter Wemm 	old = periph_drivers;
1160b7c27b9SPeter Wemm 	periph_drivers = newdrivers;
1170b7c27b9SPeter Wemm 	if (old)
1180dd50e9bSScott Long 		free(old, M_CAMPERIPH);
1190b7c27b9SPeter Wemm 	nperiph_drivers++;
12083c5d981SAlexander Motin 	/* If driver marked as early or it is late now, initialize it. */
12183c5d981SAlexander Motin 	if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
12283c5d981SAlexander Motin 	    initialized > 1)
12383c5d981SAlexander Motin 		(*drv->init)();
12483c5d981SAlexander Motin }
12583c5d981SAlexander Motin 
12683c5d981SAlexander Motin void
12783c5d981SAlexander Motin periphdriver_init(int level)
12883c5d981SAlexander Motin {
12983c5d981SAlexander Motin 	int	i, early;
13083c5d981SAlexander Motin 
13183c5d981SAlexander Motin 	initialized = max(initialized, level);
13283c5d981SAlexander Motin 	for (i = 0; periph_drivers[i] != NULL; i++) {
13383c5d981SAlexander Motin 		early = (periph_drivers[i]->flags & CAM_PERIPH_DRV_EARLY) ? 1 : 2;
13483c5d981SAlexander Motin 		if (early == initialized)
13583c5d981SAlexander Motin 			(*periph_drivers[i]->init)();
13683c5d981SAlexander Motin 	}
1370b7c27b9SPeter Wemm }
1380b7c27b9SPeter Wemm 
1398b8a9b1dSJustin T. Gibbs cam_status
140ee9c90c7SKenneth D. Merry cam_periph_alloc(periph_ctor_t *periph_ctor,
141ee9c90c7SKenneth D. Merry 		 periph_oninv_t *periph_oninvalidate,
142ee9c90c7SKenneth D. Merry 		 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
143ee9c90c7SKenneth D. Merry 		 char *name, cam_periph_type type, struct cam_path *path,
144ee9c90c7SKenneth D. Merry 		 ac_callback_t *ac_callback, ac_code code, void *arg)
1458b8a9b1dSJustin T. Gibbs {
1468b8a9b1dSJustin T. Gibbs 	struct		periph_driver **p_drv;
1472b83592fSScott Long 	struct		cam_sim *sim;
1488b8a9b1dSJustin T. Gibbs 	struct		cam_periph *periph;
1498b8a9b1dSJustin T. Gibbs 	struct		cam_periph *cur_periph;
1508b8a9b1dSJustin T. Gibbs 	path_id_t	path_id;
1518b8a9b1dSJustin T. Gibbs 	target_id_t	target_id;
1528b8a9b1dSJustin T. Gibbs 	lun_id_t	lun_id;
1538b8a9b1dSJustin T. Gibbs 	cam_status	status;
1548b8a9b1dSJustin T. Gibbs 	u_int		init_level;
1558b8a9b1dSJustin T. Gibbs 
1568b8a9b1dSJustin T. Gibbs 	init_level = 0;
1578b8a9b1dSJustin T. Gibbs 	/*
1588b8a9b1dSJustin T. Gibbs 	 * Handle Hot-Plug scenarios.  If there is already a peripheral
1598b8a9b1dSJustin T. Gibbs 	 * of our type assigned to this path, we are likely waiting for
1608b8a9b1dSJustin T. Gibbs 	 * final close on an old, invalidated, peripheral.  If this is
1618b8a9b1dSJustin T. Gibbs 	 * the case, queue up a deferred call to the peripheral's async
16274c91ec5SJustin T. Gibbs 	 * handler.  If it looks like a mistaken re-allocation, complain.
1638b8a9b1dSJustin T. Gibbs 	 */
1648b8a9b1dSJustin T. Gibbs 	if ((periph = cam_periph_find(path, name)) != NULL) {
1658b8a9b1dSJustin T. Gibbs 
1668b8a9b1dSJustin T. Gibbs 		if ((periph->flags & CAM_PERIPH_INVALID) != 0
1678b8a9b1dSJustin T. Gibbs 		 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
1688b8a9b1dSJustin T. Gibbs 			periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
1698b8a9b1dSJustin T. Gibbs 			periph->deferred_callback = ac_callback;
1708b8a9b1dSJustin T. Gibbs 			periph->deferred_ac = code;
1718b8a9b1dSJustin T. Gibbs 			return (CAM_REQ_INPROG);
1728b8a9b1dSJustin T. Gibbs 		} else {
1738b8a9b1dSJustin T. Gibbs 			printf("cam_periph_alloc: attempt to re-allocate "
1748b8a9b1dSJustin T. Gibbs 			       "valid device %s%d rejected\n",
1758b8a9b1dSJustin T. Gibbs 			       periph->periph_name, periph->unit_number);
1768b8a9b1dSJustin T. Gibbs 		}
1778b8a9b1dSJustin T. Gibbs 		return (CAM_REQ_INVALID);
1788b8a9b1dSJustin T. Gibbs 	}
1798b8a9b1dSJustin T. Gibbs 
180362abc44STai-hwa Liang 	periph = (struct cam_periph *)malloc(sizeof(*periph), M_CAMPERIPH,
1818b8a9b1dSJustin T. Gibbs 					     M_NOWAIT);
1828b8a9b1dSJustin T. Gibbs 
1838b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
1848b8a9b1dSJustin T. Gibbs 		return (CAM_RESRC_UNAVAIL);
1858b8a9b1dSJustin T. Gibbs 
1868b8a9b1dSJustin T. Gibbs 	init_level++;
1878b8a9b1dSJustin T. Gibbs 
1888b8a9b1dSJustin T. Gibbs 
1892b83592fSScott Long 	sim = xpt_path_sim(path);
1908b8a9b1dSJustin T. Gibbs 	path_id = xpt_path_path_id(path);
1918b8a9b1dSJustin T. Gibbs 	target_id = xpt_path_target_id(path);
1928b8a9b1dSJustin T. Gibbs 	lun_id = xpt_path_lun_id(path);
1938b8a9b1dSJustin T. Gibbs 	bzero(periph, sizeof(*periph));
1948b8a9b1dSJustin T. Gibbs 	cam_init_pinfo(&periph->pinfo);
1958b8a9b1dSJustin T. Gibbs 	periph->periph_start = periph_start;
1968b8a9b1dSJustin T. Gibbs 	periph->periph_dtor = periph_dtor;
197ee9c90c7SKenneth D. Merry 	periph->periph_oninval = periph_oninvalidate;
1988b8a9b1dSJustin T. Gibbs 	periph->type = type;
1998b8a9b1dSJustin T. Gibbs 	periph->periph_name = name;
2008b8a9b1dSJustin T. Gibbs 	periph->immediate_priority = CAM_PRIORITY_NONE;
2018b8a9b1dSJustin T. Gibbs 	periph->refcount = 0;
2022b83592fSScott Long 	periph->sim = sim;
2038b8a9b1dSJustin T. Gibbs 	SLIST_INIT(&periph->ccb_list);
2048b8a9b1dSJustin T. Gibbs 	status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
2058b8a9b1dSJustin T. Gibbs 	if (status != CAM_REQ_CMP)
2068b8a9b1dSJustin T. Gibbs 		goto failure;
2078b8a9b1dSJustin T. Gibbs 	periph->path = path;
2088b8a9b1dSJustin T. Gibbs 
209*f1e2546aSMatt Jacob 	xpt_lock_buses();
210*f1e2546aSMatt Jacob 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
211*f1e2546aSMatt Jacob 		if (strcmp((*p_drv)->driver_name, name) == 0)
212*f1e2546aSMatt Jacob 			break;
213*f1e2546aSMatt Jacob 	}
214*f1e2546aSMatt Jacob 	if (*p_drv == NULL) {
215*f1e2546aSMatt Jacob 		printf("cam_periph_alloc: invalid periph name '%s'\n", name);
216*f1e2546aSMatt Jacob 		xpt_free_path(periph->path);
217*f1e2546aSMatt Jacob 		free(periph, M_CAMPERIPH);
218*f1e2546aSMatt Jacob 		xpt_unlock_buses();
219*f1e2546aSMatt Jacob 		return (CAM_REQ_INVALID);
220*f1e2546aSMatt Jacob 	}
221*f1e2546aSMatt Jacob 	periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
2228b8a9b1dSJustin T. Gibbs 	cur_periph = TAILQ_FIRST(&(*p_drv)->units);
2238b8a9b1dSJustin T. Gibbs 	while (cur_periph != NULL
2248b8a9b1dSJustin T. Gibbs 	    && cur_periph->unit_number < periph->unit_number)
2258b8a9b1dSJustin T. Gibbs 		cur_periph = TAILQ_NEXT(cur_periph, unit_links);
226*f1e2546aSMatt Jacob 	if (cur_periph != NULL) {
227*f1e2546aSMatt Jacob 		KASSERT(cur_periph->unit_number != periph->unit_number, ("duplicate units on periph list"));
2288b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
229*f1e2546aSMatt Jacob 	} else {
2308b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
2318b8a9b1dSJustin T. Gibbs 		(*p_drv)->generation++;
2328b8a9b1dSJustin T. Gibbs 	}
233*f1e2546aSMatt Jacob 	xpt_unlock_buses();
234*f1e2546aSMatt Jacob 
235*f1e2546aSMatt Jacob 	init_level++;
236*f1e2546aSMatt Jacob 
237*f1e2546aSMatt Jacob 	status = xpt_add_periph(periph);
238*f1e2546aSMatt Jacob 	if (status != CAM_REQ_CMP)
239*f1e2546aSMatt Jacob 		goto failure;
2408b8a9b1dSJustin T. Gibbs 
2418b8a9b1dSJustin T. Gibbs 	init_level++;
2428b8a9b1dSJustin T. Gibbs 
2438b8a9b1dSJustin T. Gibbs 	status = periph_ctor(periph, arg);
2448b8a9b1dSJustin T. Gibbs 
2458b8a9b1dSJustin T. Gibbs 	if (status == CAM_REQ_CMP)
2468b8a9b1dSJustin T. Gibbs 		init_level++;
2478b8a9b1dSJustin T. Gibbs 
2488b8a9b1dSJustin T. Gibbs failure:
2498b8a9b1dSJustin T. Gibbs 	switch (init_level) {
2508b8a9b1dSJustin T. Gibbs 	case 4:
2518b8a9b1dSJustin T. Gibbs 		/* Initialized successfully */
2528b8a9b1dSJustin T. Gibbs 		break;
2538b8a9b1dSJustin T. Gibbs 	case 3:
2548b8a9b1dSJustin T. Gibbs 		xpt_remove_periph(periph);
2557379c88fSPoul-Henning Kamp 		/* FALLTHROUGH */
2568b8a9b1dSJustin T. Gibbs 	case 2:
257*f1e2546aSMatt Jacob 		xpt_lock_buses();
258*f1e2546aSMatt Jacob 		TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
259*f1e2546aSMatt Jacob 		xpt_unlock_buses();
2608b8a9b1dSJustin T. Gibbs 		xpt_free_path(periph->path);
2617379c88fSPoul-Henning Kamp 		/* FALLTHROUGH */
2628b8a9b1dSJustin T. Gibbs 	case 1:
263362abc44STai-hwa Liang 		free(periph, M_CAMPERIPH);
2647379c88fSPoul-Henning Kamp 		/* FALLTHROUGH */
2658b8a9b1dSJustin T. Gibbs 	case 0:
2668b8a9b1dSJustin T. Gibbs 		/* No cleanup to perform. */
2678b8a9b1dSJustin T. Gibbs 		break;
2688b8a9b1dSJustin T. Gibbs 	default:
2698b8a9b1dSJustin T. Gibbs 		panic("cam_periph_alloc: Unkown init level");
2708b8a9b1dSJustin T. Gibbs 	}
2718b8a9b1dSJustin T. Gibbs 	return(status);
2728b8a9b1dSJustin T. Gibbs }
2738b8a9b1dSJustin T. Gibbs 
2748b8a9b1dSJustin T. Gibbs /*
2758b8a9b1dSJustin T. Gibbs  * Find a peripheral structure with the specified path, target, lun,
2768b8a9b1dSJustin T. Gibbs  * and (optionally) type.  If the name is NULL, this function will return
2778b8a9b1dSJustin T. Gibbs  * the first peripheral driver that matches the specified path.
2788b8a9b1dSJustin T. Gibbs  */
2798b8a9b1dSJustin T. Gibbs struct cam_periph *
2808b8a9b1dSJustin T. Gibbs cam_periph_find(struct cam_path *path, char *name)
2818b8a9b1dSJustin T. Gibbs {
2828b8a9b1dSJustin T. Gibbs 	struct periph_driver **p_drv;
2838b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph;
2848b8a9b1dSJustin T. Gibbs 
28577dc25ccSScott Long 	xpt_lock_buses();
2860b7c27b9SPeter Wemm 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
2878b8a9b1dSJustin T. Gibbs 
2888b8a9b1dSJustin T. Gibbs 		if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
2898b8a9b1dSJustin T. Gibbs 			continue;
2908b8a9b1dSJustin T. Gibbs 
29137d40066SPoul-Henning Kamp 		TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
2928b8a9b1dSJustin T. Gibbs 			if (xpt_path_comp(periph->path, path) == 0) {
29377dc25ccSScott Long 				xpt_unlock_buses();
294*f1e2546aSMatt Jacob 				mtx_assert(periph->sim->mtx, MA_OWNED);
2958b8a9b1dSJustin T. Gibbs 				return(periph);
2968b8a9b1dSJustin T. Gibbs 			}
2978b8a9b1dSJustin T. Gibbs 		}
29877dc25ccSScott Long 		if (name != NULL) {
29977dc25ccSScott Long 			xpt_unlock_buses();
3008b8a9b1dSJustin T. Gibbs 			return(NULL);
3018b8a9b1dSJustin T. Gibbs 		}
30277dc25ccSScott Long 	}
30377dc25ccSScott Long 	xpt_unlock_buses();
3048b8a9b1dSJustin T. Gibbs 	return(NULL);
3058b8a9b1dSJustin T. Gibbs }
3068b8a9b1dSJustin T. Gibbs 
3078b8a9b1dSJustin T. Gibbs cam_status
3088b8a9b1dSJustin T. Gibbs cam_periph_acquire(struct cam_periph *periph)
3098b8a9b1dSJustin T. Gibbs {
3108b8a9b1dSJustin T. Gibbs 
3118b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
3128b8a9b1dSJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
3138b8a9b1dSJustin T. Gibbs 
3142b83592fSScott Long 	xpt_lock_buses();
3158b8a9b1dSJustin T. Gibbs 	periph->refcount++;
3162b83592fSScott Long 	xpt_unlock_buses();
3178b8a9b1dSJustin T. Gibbs 
3188b8a9b1dSJustin T. Gibbs 	return(CAM_REQ_CMP);
3198b8a9b1dSJustin T. Gibbs }
3208b8a9b1dSJustin T. Gibbs 
3218b8a9b1dSJustin T. Gibbs void
32224ebf566SEdward Tomasz Napierala cam_periph_release_locked(struct cam_periph *periph)
3238b8a9b1dSJustin T. Gibbs {
3248b8a9b1dSJustin T. Gibbs 
3258b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
3268b8a9b1dSJustin T. Gibbs 		return;
3278b8a9b1dSJustin T. Gibbs 
3282b83592fSScott Long 	xpt_lock_buses();
329*f1e2546aSMatt Jacob 	if (periph->refcount != 0) {
330*f1e2546aSMatt Jacob 		periph->refcount--;
331*f1e2546aSMatt Jacob 	} else {
332*f1e2546aSMatt Jacob 		xpt_print(periph->path, "%s: release %p when refcount is zero\n ", __func__, periph);
333*f1e2546aSMatt Jacob 	}
334*f1e2546aSMatt Jacob 	if (periph->refcount == 0
3358b8a9b1dSJustin T. Gibbs 	    && (periph->flags & CAM_PERIPH_INVALID)) {
3368b8a9b1dSJustin T. Gibbs 		camperiphfree(periph);
3378b8a9b1dSJustin T. Gibbs 	}
3382b83592fSScott Long 	xpt_unlock_buses();
33924ebf566SEdward Tomasz Napierala }
3408b8a9b1dSJustin T. Gibbs 
34124ebf566SEdward Tomasz Napierala void
34224ebf566SEdward Tomasz Napierala cam_periph_release(struct cam_periph *periph)
34324ebf566SEdward Tomasz Napierala {
34424ebf566SEdward Tomasz Napierala 	struct cam_sim *sim;
34524ebf566SEdward Tomasz Napierala 
34624ebf566SEdward Tomasz Napierala 	if (periph == NULL)
34724ebf566SEdward Tomasz Napierala 		return;
34824ebf566SEdward Tomasz Napierala 
34924ebf566SEdward Tomasz Napierala 	sim = periph->sim;
35024ebf566SEdward Tomasz Napierala 	mtx_assert(sim->mtx, MA_NOTOWNED);
35124ebf566SEdward Tomasz Napierala 	mtx_lock(sim->mtx);
35224ebf566SEdward Tomasz Napierala 	cam_periph_release_locked(periph);
35324ebf566SEdward Tomasz Napierala 	mtx_unlock(sim->mtx);
3548b8a9b1dSJustin T. Gibbs }
3558b8a9b1dSJustin T. Gibbs 
3562b83592fSScott Long int
3572b83592fSScott Long cam_periph_hold(struct cam_periph *periph, int priority)
3582b83592fSScott Long {
3592b83592fSScott Long 	int error;
3602b83592fSScott Long 
3612b83592fSScott Long 	/*
3622b83592fSScott Long 	 * Increment the reference count on the peripheral
3632b83592fSScott Long 	 * while we wait for our lock attempt to succeed
3642b83592fSScott Long 	 * to ensure the peripheral doesn't disappear out
3652b83592fSScott Long 	 * from user us while we sleep.
3662b83592fSScott Long 	 */
3672b83592fSScott Long 
3682b83592fSScott Long 	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
3692b83592fSScott Long 		return (ENXIO);
3702b83592fSScott Long 
3711fa738c2SJohn Baldwin 	mtx_assert(periph->sim->mtx, MA_OWNED);
3722b83592fSScott Long 	while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
3732b83592fSScott Long 		periph->flags |= CAM_PERIPH_LOCK_WANTED;
3741fa738c2SJohn Baldwin 		if ((error = mtx_sleep(periph, periph->sim->mtx, priority,
3751fa738c2SJohn Baldwin 		    "caplck", 0)) != 0) {
37624ebf566SEdward Tomasz Napierala 			cam_periph_release_locked(periph);
3772b83592fSScott Long 			return (error);
3782b83592fSScott Long 		}
3792b83592fSScott Long 	}
3802b83592fSScott Long 
3812b83592fSScott Long 	periph->flags |= CAM_PERIPH_LOCKED;
3822b83592fSScott Long 	return (0);
3832b83592fSScott Long }
3842b83592fSScott Long 
3852b83592fSScott Long void
3862b83592fSScott Long cam_periph_unhold(struct cam_periph *periph)
3872b83592fSScott Long {
3882b83592fSScott Long 
3892b83592fSScott Long 	mtx_assert(periph->sim->mtx, MA_OWNED);
3902b83592fSScott Long 
3912b83592fSScott Long 	periph->flags &= ~CAM_PERIPH_LOCKED;
3922b83592fSScott Long 	if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
3932b83592fSScott Long 		periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
3942b83592fSScott Long 		wakeup(periph);
3952b83592fSScott Long 	}
3962b83592fSScott Long 
39724ebf566SEdward Tomasz Napierala 	cam_periph_release_locked(periph);
3982b83592fSScott Long }
3992b83592fSScott Long 
4008b8a9b1dSJustin T. Gibbs /*
4018b8a9b1dSJustin T. Gibbs  * Look for the next unit number that is not currently in use for this
4028b8a9b1dSJustin T. Gibbs  * peripheral type starting at "newunit".  Also exclude unit numbers that
4038b8a9b1dSJustin T. Gibbs  * are reserved by for future "hardwiring" unless we already know that this
4048b8a9b1dSJustin T. Gibbs  * is a potential wired device.  Only assume that the device is "wired" the
4058b8a9b1dSJustin T. Gibbs  * first time through the loop since after that we'll be looking at unit
4068b8a9b1dSJustin T. Gibbs  * numbers that did not match a wiring entry.
4078b8a9b1dSJustin T. Gibbs  */
4088b8a9b1dSJustin T. Gibbs static u_int
409501468a5SKenneth D. Merry camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
410501468a5SKenneth D. Merry 		  path_id_t pathid, target_id_t target, lun_id_t lun)
4118b8a9b1dSJustin T. Gibbs {
4128b8a9b1dSJustin T. Gibbs 	struct	cam_periph *periph;
4132398f0cdSPeter Wemm 	char	*periph_name;
4142398f0cdSPeter Wemm 	int	i, val, dunit, r;
4152398f0cdSPeter Wemm 	const char *dname, *strval;
4168b8a9b1dSJustin T. Gibbs 
4178b8a9b1dSJustin T. Gibbs 	periph_name = p_drv->driver_name;
4188b8a9b1dSJustin T. Gibbs 	for (;;newunit++) {
4198b8a9b1dSJustin T. Gibbs 
4208b8a9b1dSJustin T. Gibbs 		for (periph = TAILQ_FIRST(&p_drv->units);
4218b8a9b1dSJustin T. Gibbs 		     periph != NULL && periph->unit_number != newunit;
4228b8a9b1dSJustin T. Gibbs 		     periph = TAILQ_NEXT(periph, unit_links))
4238b8a9b1dSJustin T. Gibbs 			;
4248b8a9b1dSJustin T. Gibbs 
4258b8a9b1dSJustin T. Gibbs 		if (periph != NULL && periph->unit_number == newunit) {
4268b8a9b1dSJustin T. Gibbs 			if (wired != 0) {
427f0d9af51SMatt Jacob 				xpt_print(periph->path, "Duplicate Wired "
428f0d9af51SMatt Jacob 				    "Device entry!\n");
429f0d9af51SMatt Jacob 				xpt_print(periph->path, "Second device (%s "
430f0d9af51SMatt Jacob 				    "device at scbus%d target %d lun %d) will "
431f0d9af51SMatt Jacob 				    "not be wired\n", periph_name, pathid,
432f0d9af51SMatt Jacob 				    target, lun);
4338b8a9b1dSJustin T. Gibbs 				wired = 0;
4348b8a9b1dSJustin T. Gibbs 			}
4358b8a9b1dSJustin T. Gibbs 			continue;
4368b8a9b1dSJustin T. Gibbs 		}
43775f51904SPeter Wemm 		if (wired)
43875f51904SPeter Wemm 			break;
4398b8a9b1dSJustin T. Gibbs 
4408b8a9b1dSJustin T. Gibbs 		/*
4418b8a9b1dSJustin T. Gibbs 		 * Don't match entries like "da 4" as a wired down
4428b8a9b1dSJustin T. Gibbs 		 * device, but do match entries like "da 4 target 5"
4438b8a9b1dSJustin T. Gibbs 		 * or even "da 4 scbus 1".
4448b8a9b1dSJustin T. Gibbs 		 */
4452398f0cdSPeter Wemm 		i = 0;
4462398f0cdSPeter Wemm 		dname = periph_name;
4472398f0cdSPeter Wemm 		for (;;) {
4482398f0cdSPeter Wemm 			r = resource_find_dev(&i, dname, &dunit, NULL, NULL);
4492398f0cdSPeter Wemm 			if (r != 0)
4502398f0cdSPeter Wemm 				break;
45175f51904SPeter Wemm 			/* if no "target" and no specific scbus, skip */
45275f51904SPeter Wemm 			if (resource_int_value(dname, dunit, "target", &val) &&
45375f51904SPeter Wemm 			    (resource_string_value(dname, dunit, "at",&strval)||
45475f51904SPeter Wemm 			     strcmp(strval, "scbus") == 0))
45575f51904SPeter Wemm 				continue;
45675f51904SPeter Wemm 			if (newunit == dunit)
4578b8a9b1dSJustin T. Gibbs 				break;
4588b8a9b1dSJustin T. Gibbs 		}
4592398f0cdSPeter Wemm 		if (r != 0)
4608b8a9b1dSJustin T. Gibbs 			break;
4618b8a9b1dSJustin T. Gibbs 	}
4628b8a9b1dSJustin T. Gibbs 	return (newunit);
4638b8a9b1dSJustin T. Gibbs }
4648b8a9b1dSJustin T. Gibbs 
4658b8a9b1dSJustin T. Gibbs static u_int
4668b8a9b1dSJustin T. Gibbs camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
4678b8a9b1dSJustin T. Gibbs 	      target_id_t target, lun_id_t lun)
4688b8a9b1dSJustin T. Gibbs {
4698b8a9b1dSJustin T. Gibbs 	u_int	unit;
470c1b81613SPeter Wemm 	int	wired, i, val, dunit;
4712398f0cdSPeter Wemm 	const char *dname, *strval;
4722398f0cdSPeter Wemm 	char	pathbuf[32], *periph_name;
4738b8a9b1dSJustin T. Gibbs 
47475f51904SPeter Wemm 	periph_name = p_drv->driver_name;
47575f51904SPeter Wemm 	snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
476c1b81613SPeter Wemm 	unit = 0;
4772398f0cdSPeter Wemm 	i = 0;
4782398f0cdSPeter Wemm 	dname = periph_name;
479c1b81613SPeter Wemm 	for (wired = 0; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0;
480c1b81613SPeter Wemm 	     wired = 0) {
48175f51904SPeter Wemm 		if (resource_string_value(dname, dunit, "at", &strval) == 0) {
48275f51904SPeter Wemm 			if (strcmp(strval, pathbuf) != 0)
4838b8a9b1dSJustin T. Gibbs 				continue;
484c1b81613SPeter Wemm 			wired++;
4858b8a9b1dSJustin T. Gibbs 		}
48675f51904SPeter Wemm 		if (resource_int_value(dname, dunit, "target", &val) == 0) {
48775f51904SPeter Wemm 			if (val != target)
4888b8a9b1dSJustin T. Gibbs 				continue;
489c1b81613SPeter Wemm 			wired++;
4908b8a9b1dSJustin T. Gibbs 		}
49175f51904SPeter Wemm 		if (resource_int_value(dname, dunit, "lun", &val) == 0) {
49275f51904SPeter Wemm 			if (val != lun)
4938b8a9b1dSJustin T. Gibbs 				continue;
494c1b81613SPeter Wemm 			wired++;
4958b8a9b1dSJustin T. Gibbs 		}
496c1b81613SPeter Wemm 		if (wired != 0) {
49775f51904SPeter Wemm 			unit = dunit;
4988b8a9b1dSJustin T. Gibbs 			break;
4998b8a9b1dSJustin T. Gibbs 		}
5008b8a9b1dSJustin T. Gibbs 	}
5018b8a9b1dSJustin T. Gibbs 
5028b8a9b1dSJustin T. Gibbs 	/*
5038b8a9b1dSJustin T. Gibbs 	 * Either start from 0 looking for the next unit or from
50475f51904SPeter Wemm 	 * the unit number given in the resource config.  This way,
5058b8a9b1dSJustin T. Gibbs 	 * if we have wildcard matches, we don't return the same
5068b8a9b1dSJustin T. Gibbs 	 * unit number twice.
5078b8a9b1dSJustin T. Gibbs 	 */
508c1b81613SPeter Wemm 	unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun);
5098b8a9b1dSJustin T. Gibbs 
5108b8a9b1dSJustin T. Gibbs 	return (unit);
5118b8a9b1dSJustin T. Gibbs }
5128b8a9b1dSJustin T. Gibbs 
5138b8a9b1dSJustin T. Gibbs void
5148b8a9b1dSJustin T. Gibbs cam_periph_invalidate(struct cam_periph *periph)
5158b8a9b1dSJustin T. Gibbs {
5168b8a9b1dSJustin T. Gibbs 
517ee9c90c7SKenneth D. Merry 	/*
518ee9c90c7SKenneth D. Merry 	 * We only call this routine the first time a peripheral is
51977dc25ccSScott Long 	 * invalidated.
520ee9c90c7SKenneth D. Merry 	 */
521ee9c90c7SKenneth D. Merry 	if (((periph->flags & CAM_PERIPH_INVALID) == 0)
522ee9c90c7SKenneth D. Merry 	 && (periph->periph_oninval != NULL))
523ee9c90c7SKenneth D. Merry 		periph->periph_oninval(periph);
524ee9c90c7SKenneth D. Merry 
5258b8a9b1dSJustin T. Gibbs 	periph->flags |= CAM_PERIPH_INVALID;
5268b8a9b1dSJustin T. Gibbs 	periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
5278b8a9b1dSJustin T. Gibbs 
5282b83592fSScott Long 	xpt_lock_buses();
5298b8a9b1dSJustin T. Gibbs 	if (periph->refcount == 0)
5308b8a9b1dSJustin T. Gibbs 		camperiphfree(periph);
5318b8a9b1dSJustin T. Gibbs 	else if (periph->refcount < 0)
5328b8a9b1dSJustin T. Gibbs 		printf("cam_invalidate_periph: refcount < 0!!\n");
5332b83592fSScott Long 	xpt_unlock_buses();
5348b8a9b1dSJustin T. Gibbs }
5358b8a9b1dSJustin T. Gibbs 
5368b8a9b1dSJustin T. Gibbs static void
5378b8a9b1dSJustin T. Gibbs camperiphfree(struct cam_periph *periph)
5388b8a9b1dSJustin T. Gibbs {
5398b8a9b1dSJustin T. Gibbs 	struct periph_driver **p_drv;
5408b8a9b1dSJustin T. Gibbs 
5410b7c27b9SPeter Wemm 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
5428b8a9b1dSJustin T. Gibbs 		if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
5438b8a9b1dSJustin T. Gibbs 			break;
5448b8a9b1dSJustin T. Gibbs 	}
545661658a6SScott Long 	if (*p_drv == NULL) {
546661658a6SScott Long 		printf("camperiphfree: attempt to free non-existant periph\n");
547661658a6SScott Long 		return;
548661658a6SScott Long 	}
5498b8a9b1dSJustin T. Gibbs 
5508b8a9b1dSJustin T. Gibbs 	TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
5518b8a9b1dSJustin T. Gibbs 	(*p_drv)->generation++;
552f3548746SScott Long 	xpt_unlock_buses();
5538b8a9b1dSJustin T. Gibbs 
554f3548746SScott Long 	if (periph->periph_dtor != NULL)
555f3548746SScott Long 		periph->periph_dtor(periph);
5568b8a9b1dSJustin T. Gibbs 	xpt_remove_periph(periph);
5578b8a9b1dSJustin T. Gibbs 
5588b8a9b1dSJustin T. Gibbs 	if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
5598b8a9b1dSJustin T. Gibbs 		union ccb ccb;
5608b8a9b1dSJustin T. Gibbs 		void *arg;
5618b8a9b1dSJustin T. Gibbs 
5628b8a9b1dSJustin T. Gibbs 		switch (periph->deferred_ac) {
5638b8a9b1dSJustin T. Gibbs 		case AC_FOUND_DEVICE:
5648b8a9b1dSJustin T. Gibbs 			ccb.ccb_h.func_code = XPT_GDEV_TYPE;
565bbfa4aa1SAlexander Motin 			xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
5668b8a9b1dSJustin T. Gibbs 			xpt_action(&ccb);
5678b8a9b1dSJustin T. Gibbs 			arg = &ccb;
5688b8a9b1dSJustin T. Gibbs 			break;
5698b8a9b1dSJustin T. Gibbs 		case AC_PATH_REGISTERED:
5708b8a9b1dSJustin T. Gibbs 			ccb.ccb_h.func_code = XPT_PATH_INQ;
571bbfa4aa1SAlexander Motin 			xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
5728b8a9b1dSJustin T. Gibbs 			xpt_action(&ccb);
5738b8a9b1dSJustin T. Gibbs 			arg = &ccb;
5748b8a9b1dSJustin T. Gibbs 			break;
5758b8a9b1dSJustin T. Gibbs 		default:
5768b8a9b1dSJustin T. Gibbs 			arg = NULL;
5778b8a9b1dSJustin T. Gibbs 			break;
5788b8a9b1dSJustin T. Gibbs 		}
5798b8a9b1dSJustin T. Gibbs 		periph->deferred_callback(NULL, periph->deferred_ac,
5808b8a9b1dSJustin T. Gibbs 					  periph->path, arg);
5818b8a9b1dSJustin T. Gibbs 	}
5828b8a9b1dSJustin T. Gibbs 	xpt_free_path(periph->path);
583362abc44STai-hwa Liang 	free(periph, M_CAMPERIPH);
584f3548746SScott Long 	xpt_lock_buses();
5858b8a9b1dSJustin T. Gibbs }
5868b8a9b1dSJustin T. Gibbs 
5878b8a9b1dSJustin T. Gibbs /*
5888b8a9b1dSJustin T. Gibbs  * Map user virtual pointers into kernel virtual address space, so we can
5898b8a9b1dSJustin T. Gibbs  * access the memory.  This won't work on physical pointers, for now it's
5908b8a9b1dSJustin T. Gibbs  * up to the caller to check for that.  (XXX KDM -- should we do that here
5918b8a9b1dSJustin T. Gibbs  * instead?)  This also only works for up to MAXPHYS memory.  Since we use
5928b8a9b1dSJustin T. Gibbs  * buffers to map stuff in and out, we're limited to the buffer size.
5938b8a9b1dSJustin T. Gibbs  */
5948b8a9b1dSJustin T. Gibbs int
5958b8a9b1dSJustin T. Gibbs cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
5968b8a9b1dSJustin T. Gibbs {
5972d5c7e45SMatthew Dillon 	int numbufs, i, j;
59879d49a06SKenneth D. Merry 	int flags[CAM_PERIPH_MAXMAPS];
5998b8a9b1dSJustin T. Gibbs 	u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
6008b8a9b1dSJustin T. Gibbs 	u_int32_t lengths[CAM_PERIPH_MAXMAPS];
6018b8a9b1dSJustin T. Gibbs 	u_int32_t dirs[CAM_PERIPH_MAXMAPS];
60252c9ce25SScott Long 	/* Some controllers may not be able to handle more data. */
60352c9ce25SScott Long 	size_t maxmap = DFLTPHYS;
6048b8a9b1dSJustin T. Gibbs 
6058b8a9b1dSJustin T. Gibbs 	switch(ccb->ccb_h.func_code) {
6068b8a9b1dSJustin T. Gibbs 	case XPT_DEV_MATCH:
6078b8a9b1dSJustin T. Gibbs 		if (ccb->cdm.match_buf_len == 0) {
6088b8a9b1dSJustin T. Gibbs 			printf("cam_periph_mapmem: invalid match buffer "
6098b8a9b1dSJustin T. Gibbs 			       "length 0\n");
6108b8a9b1dSJustin T. Gibbs 			return(EINVAL);
6118b8a9b1dSJustin T. Gibbs 		}
6128b8a9b1dSJustin T. Gibbs 		if (ccb->cdm.pattern_buf_len > 0) {
6138b8a9b1dSJustin T. Gibbs 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
6148b8a9b1dSJustin T. Gibbs 			lengths[0] = ccb->cdm.pattern_buf_len;
6158b8a9b1dSJustin T. Gibbs 			dirs[0] = CAM_DIR_OUT;
6168b8a9b1dSJustin T. Gibbs 			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
6178b8a9b1dSJustin T. Gibbs 			lengths[1] = ccb->cdm.match_buf_len;
6188b8a9b1dSJustin T. Gibbs 			dirs[1] = CAM_DIR_IN;
6198b8a9b1dSJustin T. Gibbs 			numbufs = 2;
6208b8a9b1dSJustin T. Gibbs 		} else {
6218b8a9b1dSJustin T. Gibbs 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
6228b8a9b1dSJustin T. Gibbs 			lengths[0] = ccb->cdm.match_buf_len;
6238b8a9b1dSJustin T. Gibbs 			dirs[0] = CAM_DIR_IN;
6248b8a9b1dSJustin T. Gibbs 			numbufs = 1;
6258b8a9b1dSJustin T. Gibbs 		}
62652c9ce25SScott Long 		/*
62752c9ce25SScott Long 		 * This request will not go to the hardware, no reason
62852c9ce25SScott Long 		 * to be so strict. vmapbuf() is able to map up to MAXPHYS.
62952c9ce25SScott Long 		 */
63052c9ce25SScott Long 		maxmap = MAXPHYS;
6318b8a9b1dSJustin T. Gibbs 		break;
6328b8a9b1dSJustin T. Gibbs 	case XPT_SCSI_IO:
63387cfaf0eSJustin T. Gibbs 	case XPT_CONT_TARGET_IO:
6348b8a9b1dSJustin T. Gibbs 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
6358b8a9b1dSJustin T. Gibbs 			return(0);
6368b8a9b1dSJustin T. Gibbs 
6378b8a9b1dSJustin T. Gibbs 		data_ptrs[0] = &ccb->csio.data_ptr;
63879d49a06SKenneth D. Merry 		lengths[0] = ccb->csio.dxfer_len;
6398b8a9b1dSJustin T. Gibbs 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
6408b8a9b1dSJustin T. Gibbs 		numbufs = 1;
6418b8a9b1dSJustin T. Gibbs 		break;
64252c9ce25SScott Long 	case XPT_ATA_IO:
64352c9ce25SScott Long 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
64452c9ce25SScott Long 			return(0);
64552c9ce25SScott Long 
64652c9ce25SScott Long 		data_ptrs[0] = &ccb->ataio.data_ptr;
64752c9ce25SScott Long 		lengths[0] = ccb->ataio.dxfer_len;
64852c9ce25SScott Long 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
64952c9ce25SScott Long 		numbufs = 1;
65052c9ce25SScott Long 		break;
6518b8a9b1dSJustin T. Gibbs 	default:
6528b8a9b1dSJustin T. Gibbs 		return(EINVAL);
6538b8a9b1dSJustin T. Gibbs 		break; /* NOTREACHED */
6548b8a9b1dSJustin T. Gibbs 	}
6558b8a9b1dSJustin T. Gibbs 
6568b8a9b1dSJustin T. Gibbs 	/*
65779d49a06SKenneth D. Merry 	 * Check the transfer length and permissions first, so we don't
65879d49a06SKenneth D. Merry 	 * have to unmap any previously mapped buffers.
6598b8a9b1dSJustin T. Gibbs 	 */
6608b8a9b1dSJustin T. Gibbs 	for (i = 0; i < numbufs; i++) {
66179d49a06SKenneth D. Merry 
66279d49a06SKenneth D. Merry 		flags[i] = 0;
66379d49a06SKenneth D. Merry 
66479d49a06SKenneth D. Merry 		/*
66579d49a06SKenneth D. Merry 		 * The userland data pointer passed in may not be page
66679d49a06SKenneth D. Merry 		 * aligned.  vmapbuf() truncates the address to a page
66779d49a06SKenneth D. Merry 		 * boundary, so if the address isn't page aligned, we'll
66879d49a06SKenneth D. Merry 		 * need enough space for the given transfer length, plus
66979d49a06SKenneth D. Merry 		 * whatever extra space is necessary to make it to the page
67079d49a06SKenneth D. Merry 		 * boundary.
67179d49a06SKenneth D. Merry 		 */
67279d49a06SKenneth D. Merry 		if ((lengths[i] +
67352c9ce25SScott Long 		    (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > maxmap){
6746d7b539aSPeter Wemm 			printf("cam_periph_mapmem: attempt to map %lu bytes, "
67552c9ce25SScott Long 			       "which is greater than %lu\n",
6766d7b539aSPeter Wemm 			       (long)(lengths[i] +
6776d7b539aSPeter Wemm 			       (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
67852c9ce25SScott Long 			       (u_long)maxmap);
67979d49a06SKenneth D. Merry 			return(E2BIG);
68079d49a06SKenneth D. Merry 		}
6818b8a9b1dSJustin T. Gibbs 
682edd24ab7SKenneth D. Merry 		if (dirs[i] & CAM_DIR_OUT) {
68321144e3bSPoul-Henning Kamp 			flags[i] = BIO_WRITE;
6848b8a9b1dSJustin T. Gibbs 		}
6858b8a9b1dSJustin T. Gibbs 
686edd24ab7SKenneth D. Merry 		if (dirs[i] & CAM_DIR_IN) {
68721144e3bSPoul-Henning Kamp 			flags[i] = BIO_READ;
6888b8a9b1dSJustin T. Gibbs 		}
6898b8a9b1dSJustin T. Gibbs 
69079d49a06SKenneth D. Merry 	}
69179d49a06SKenneth D. Merry 
69279d49a06SKenneth D. Merry 	/* this keeps the current process from getting swapped */
69379d49a06SKenneth D. Merry 	/*
69479d49a06SKenneth D. Merry 	 * XXX KDM should I use P_NOSWAP instead?
69579d49a06SKenneth D. Merry 	 */
6960cbbb7bfSPeter Wemm 	PHOLD(curproc);
69779d49a06SKenneth D. Merry 
69879d49a06SKenneth D. Merry 	for (i = 0; i < numbufs; i++) {
6998b8a9b1dSJustin T. Gibbs 		/*
7008b8a9b1dSJustin T. Gibbs 		 * Get the buffer.
7018b8a9b1dSJustin T. Gibbs 		 */
7021c7c3c6aSMatthew Dillon 		mapinfo->bp[i] = getpbuf(NULL);
7038b8a9b1dSJustin T. Gibbs 
7048b8a9b1dSJustin T. Gibbs 		/* save the buffer's data address */
7058b8a9b1dSJustin T. Gibbs 		mapinfo->bp[i]->b_saveaddr = mapinfo->bp[i]->b_data;
7068b8a9b1dSJustin T. Gibbs 
7078b8a9b1dSJustin T. Gibbs 		/* put our pointer in the data slot */
7088b8a9b1dSJustin T. Gibbs 		mapinfo->bp[i]->b_data = *data_ptrs[i];
7098b8a9b1dSJustin T. Gibbs 
71052c9ce25SScott Long 		/* set the transfer length, we know it's < MAXPHYS */
7118b8a9b1dSJustin T. Gibbs 		mapinfo->bp[i]->b_bufsize = lengths[i];
7128b8a9b1dSJustin T. Gibbs 
71321144e3bSPoul-Henning Kamp 		/* set the direction */
71421144e3bSPoul-Henning Kamp 		mapinfo->bp[i]->b_iocmd = flags[i];
7158b8a9b1dSJustin T. Gibbs 
7162d5c7e45SMatthew Dillon 		/*
7172d5c7e45SMatthew Dillon 		 * Map the buffer into kernel memory.
7182d5c7e45SMatthew Dillon 		 *
7192d5c7e45SMatthew Dillon 		 * Note that useracc() alone is not a  sufficient test.
7202d5c7e45SMatthew Dillon 		 * vmapbuf() can still fail due to a smaller file mapped
7212d5c7e45SMatthew Dillon 		 * into a larger area of VM, or if userland races against
7222d5c7e45SMatthew Dillon 		 * vmapbuf() after the useracc() check.
7232d5c7e45SMatthew Dillon 		 */
7242d5c7e45SMatthew Dillon 		if (vmapbuf(mapinfo->bp[i]) < 0) {
7252d5c7e45SMatthew Dillon 			for (j = 0; j < i; ++j) {
7262d5c7e45SMatthew Dillon 				*data_ptrs[j] = mapinfo->bp[j]->b_saveaddr;
7278373f36dSAlan Cox 				vunmapbuf(mapinfo->bp[j]);
7282d5c7e45SMatthew Dillon 				relpbuf(mapinfo->bp[j], NULL);
7292d5c7e45SMatthew Dillon 			}
7308373f36dSAlan Cox 			relpbuf(mapinfo->bp[i], NULL);
7312d5c7e45SMatthew Dillon 			PRELE(curproc);
7322d5c7e45SMatthew Dillon 			return(EACCES);
7332d5c7e45SMatthew Dillon 		}
7348b8a9b1dSJustin T. Gibbs 
7358b8a9b1dSJustin T. Gibbs 		/* set our pointer to the new mapped area */
7368b8a9b1dSJustin T. Gibbs 		*data_ptrs[i] = mapinfo->bp[i]->b_data;
7378b8a9b1dSJustin T. Gibbs 
7388b8a9b1dSJustin T. Gibbs 		mapinfo->num_bufs_used++;
7398b8a9b1dSJustin T. Gibbs 	}
7408b8a9b1dSJustin T. Gibbs 
741a62525f3SMatt Jacob 	/*
742a62525f3SMatt Jacob 	 * Now that we've gotten this far, change ownership to the kernel
743a62525f3SMatt Jacob 	 * of the buffers so that we don't run afoul of returning to user
744a62525f3SMatt Jacob 	 * space with locks (on the buffer) held.
745a62525f3SMatt Jacob 	 */
746a62525f3SMatt Jacob 	for (i = 0; i < numbufs; i++) {
747a62525f3SMatt Jacob 		BUF_KERNPROC(mapinfo->bp[i]);
748a62525f3SMatt Jacob 	}
749a62525f3SMatt Jacob 
750a62525f3SMatt Jacob 
7518b8a9b1dSJustin T. Gibbs 	return(0);
7528b8a9b1dSJustin T. Gibbs }
7538b8a9b1dSJustin T. Gibbs 
7548b8a9b1dSJustin T. Gibbs /*
7558b8a9b1dSJustin T. Gibbs  * Unmap memory segments mapped into kernel virtual address space by
7568b8a9b1dSJustin T. Gibbs  * cam_periph_mapmem().
7578b8a9b1dSJustin T. Gibbs  */
7588b8a9b1dSJustin T. Gibbs void
7598b8a9b1dSJustin T. Gibbs cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
7608b8a9b1dSJustin T. Gibbs {
7618b8a9b1dSJustin T. Gibbs 	int numbufs, i;
7628b8a9b1dSJustin T. Gibbs 	u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
7638b8a9b1dSJustin T. Gibbs 
7648b8a9b1dSJustin T. Gibbs 	if (mapinfo->num_bufs_used <= 0) {
7658b8a9b1dSJustin T. Gibbs 		/* allow ourselves to be swapped once again */
7660cbbb7bfSPeter Wemm 		PRELE(curproc);
7678b8a9b1dSJustin T. Gibbs 		return;
7688b8a9b1dSJustin T. Gibbs 	}
7698b8a9b1dSJustin T. Gibbs 
7708b8a9b1dSJustin T. Gibbs 	switch (ccb->ccb_h.func_code) {
7718b8a9b1dSJustin T. Gibbs 	case XPT_DEV_MATCH:
7728b8a9b1dSJustin T. Gibbs 		numbufs = min(mapinfo->num_bufs_used, 2);
7738b8a9b1dSJustin T. Gibbs 
7748b8a9b1dSJustin T. Gibbs 		if (numbufs == 1) {
7758b8a9b1dSJustin T. Gibbs 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
7768b8a9b1dSJustin T. Gibbs 		} else {
7778b8a9b1dSJustin T. Gibbs 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
7788b8a9b1dSJustin T. Gibbs 			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
7798b8a9b1dSJustin T. Gibbs 		}
7808b8a9b1dSJustin T. Gibbs 		break;
7818b8a9b1dSJustin T. Gibbs 	case XPT_SCSI_IO:
7829911ecf9SJustin T. Gibbs 	case XPT_CONT_TARGET_IO:
7838b8a9b1dSJustin T. Gibbs 		data_ptrs[0] = &ccb->csio.data_ptr;
7848b8a9b1dSJustin T. Gibbs 		numbufs = min(mapinfo->num_bufs_used, 1);
7858b8a9b1dSJustin T. Gibbs 		break;
78652c9ce25SScott Long 	case XPT_ATA_IO:
78752c9ce25SScott Long 		data_ptrs[0] = &ccb->ataio.data_ptr;
78852c9ce25SScott Long 		numbufs = min(mapinfo->num_bufs_used, 1);
78952c9ce25SScott Long 		break;
7908b8a9b1dSJustin T. Gibbs 	default:
7918b8a9b1dSJustin T. Gibbs 		/* allow ourselves to be swapped once again */
7920cbbb7bfSPeter Wemm 		PRELE(curproc);
7938b8a9b1dSJustin T. Gibbs 		return;
7948b8a9b1dSJustin T. Gibbs 		break; /* NOTREACHED */
7958b8a9b1dSJustin T. Gibbs 	}
7968b8a9b1dSJustin T. Gibbs 
7978b8a9b1dSJustin T. Gibbs 	for (i = 0; i < numbufs; i++) {
7988b8a9b1dSJustin T. Gibbs 		/* Set the user's pointer back to the original value */
7998b8a9b1dSJustin T. Gibbs 		*data_ptrs[i] = mapinfo->bp[i]->b_saveaddr;
8008b8a9b1dSJustin T. Gibbs 
8018b8a9b1dSJustin T. Gibbs 		/* unmap the buffer */
8028b8a9b1dSJustin T. Gibbs 		vunmapbuf(mapinfo->bp[i]);
8038b8a9b1dSJustin T. Gibbs 
8048b8a9b1dSJustin T. Gibbs 		/* release the buffer */
8051c7c3c6aSMatthew Dillon 		relpbuf(mapinfo->bp[i], NULL);
8068b8a9b1dSJustin T. Gibbs 	}
8078b8a9b1dSJustin T. Gibbs 
8088b8a9b1dSJustin T. Gibbs 	/* allow ourselves to be swapped once again */
8090cbbb7bfSPeter Wemm 	PRELE(curproc);
8108b8a9b1dSJustin T. Gibbs }
8118b8a9b1dSJustin T. Gibbs 
8128b8a9b1dSJustin T. Gibbs union ccb *
8138b8a9b1dSJustin T. Gibbs cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
8148b8a9b1dSJustin T. Gibbs {
8158b8a9b1dSJustin T. Gibbs 	struct ccb_hdr *ccb_h;
8168b8a9b1dSJustin T. Gibbs 
8172b83592fSScott Long 	mtx_assert(periph->sim->mtx, MA_OWNED);
8188b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
8198b8a9b1dSJustin T. Gibbs 
820fc2ffbe6SPoul-Henning Kamp 	while (SLIST_FIRST(&periph->ccb_list) == NULL) {
8218b8a9b1dSJustin T. Gibbs 		if (periph->immediate_priority > priority)
8228b8a9b1dSJustin T. Gibbs 			periph->immediate_priority = priority;
8238b8a9b1dSJustin T. Gibbs 		xpt_schedule(periph, priority);
824fc2ffbe6SPoul-Henning Kamp 		if ((SLIST_FIRST(&periph->ccb_list) != NULL)
825fc2ffbe6SPoul-Henning Kamp 		 && (SLIST_FIRST(&periph->ccb_list)->pinfo.priority == priority))
8268b8a9b1dSJustin T. Gibbs 			break;
8272b83592fSScott Long 		mtx_assert(periph->sim->mtx, MA_OWNED);
8281fa738c2SJohn Baldwin 		mtx_sleep(&periph->ccb_list, periph->sim->mtx, PRIBIO, "cgticb",
8291fa738c2SJohn Baldwin 		    0);
8308b8a9b1dSJustin T. Gibbs 	}
8318b8a9b1dSJustin T. Gibbs 
832fc2ffbe6SPoul-Henning Kamp 	ccb_h = SLIST_FIRST(&periph->ccb_list);
8338b8a9b1dSJustin T. Gibbs 	SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
8348b8a9b1dSJustin T. Gibbs 	return ((union ccb *)ccb_h);
8358b8a9b1dSJustin T. Gibbs }
8368b8a9b1dSJustin T. Gibbs 
8378b8a9b1dSJustin T. Gibbs void
8388b8a9b1dSJustin T. Gibbs cam_periph_ccbwait(union ccb *ccb)
8398b8a9b1dSJustin T. Gibbs {
8402b83592fSScott Long 	struct cam_sim *sim;
8418b8a9b1dSJustin T. Gibbs 
8422b83592fSScott Long 	sim = xpt_path_sim(ccb->ccb_h.path);
8438b8a9b1dSJustin T. Gibbs 	if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
8448b8a9b1dSJustin T. Gibbs 	 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
8451fa738c2SJohn Baldwin 		mtx_sleep(&ccb->ccb_h.cbfcnp, sim->mtx, PRIBIO, "cbwait", 0);
8468b8a9b1dSJustin T. Gibbs }
8478b8a9b1dSJustin T. Gibbs 
8488b8a9b1dSJustin T. Gibbs int
849571e8e20SScott Long cam_periph_ioctl(struct cam_periph *periph, u_long cmd, caddr_t addr,
8508b8a9b1dSJustin T. Gibbs 		 int (*error_routine)(union ccb *ccb,
8518b8a9b1dSJustin T. Gibbs 				      cam_flags camflags,
8528b8a9b1dSJustin T. Gibbs 				      u_int32_t sense_flags))
8538b8a9b1dSJustin T. Gibbs {
8548b8a9b1dSJustin T. Gibbs 	union ccb 	     *ccb;
8558b8a9b1dSJustin T. Gibbs 	int 		     error;
8568b8a9b1dSJustin T. Gibbs 	int		     found;
8578b8a9b1dSJustin T. Gibbs 
8588b8a9b1dSJustin T. Gibbs 	error = found = 0;
8598b8a9b1dSJustin T. Gibbs 
8608b8a9b1dSJustin T. Gibbs 	switch(cmd){
8618b8a9b1dSJustin T. Gibbs 	case CAMGETPASSTHRU:
862bbfa4aa1SAlexander Motin 		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
8638b8a9b1dSJustin T. Gibbs 		xpt_setup_ccb(&ccb->ccb_h,
8648b8a9b1dSJustin T. Gibbs 			      ccb->ccb_h.path,
865bbfa4aa1SAlexander Motin 			      CAM_PRIORITY_NORMAL);
8668b8a9b1dSJustin T. Gibbs 		ccb->ccb_h.func_code = XPT_GDEVLIST;
8678b8a9b1dSJustin T. Gibbs 
8688b8a9b1dSJustin T. Gibbs 		/*
8698b8a9b1dSJustin T. Gibbs 		 * Basically, the point of this is that we go through
8708b8a9b1dSJustin T. Gibbs 		 * getting the list of devices, until we find a passthrough
8718b8a9b1dSJustin T. Gibbs 		 * device.  In the current version of the CAM code, the
8728b8a9b1dSJustin T. Gibbs 		 * only way to determine what type of device we're dealing
8738b8a9b1dSJustin T. Gibbs 		 * with is by its name.
8748b8a9b1dSJustin T. Gibbs 		 */
8758b8a9b1dSJustin T. Gibbs 		while (found == 0) {
8768b8a9b1dSJustin T. Gibbs 			ccb->cgdl.index = 0;
8778b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
8788b8a9b1dSJustin T. Gibbs 			while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
8798b8a9b1dSJustin T. Gibbs 
8808b8a9b1dSJustin T. Gibbs 				/* we want the next device in the list */
8818b8a9b1dSJustin T. Gibbs 				xpt_action(ccb);
8828b8a9b1dSJustin T. Gibbs 				if (strncmp(ccb->cgdl.periph_name,
8838b8a9b1dSJustin T. Gibbs 				    "pass", 4) == 0){
8848b8a9b1dSJustin T. Gibbs 					found = 1;
8858b8a9b1dSJustin T. Gibbs 					break;
8868b8a9b1dSJustin T. Gibbs 				}
8878b8a9b1dSJustin T. Gibbs 			}
8888b8a9b1dSJustin T. Gibbs 			if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
8898b8a9b1dSJustin T. Gibbs 			    (found == 0)) {
8908b8a9b1dSJustin T. Gibbs 				ccb->cgdl.periph_name[0] = '\0';
8918b8a9b1dSJustin T. Gibbs 				ccb->cgdl.unit_number = 0;
8928b8a9b1dSJustin T. Gibbs 				break;
8938b8a9b1dSJustin T. Gibbs 			}
8948b8a9b1dSJustin T. Gibbs 		}
8958b8a9b1dSJustin T. Gibbs 
8968b8a9b1dSJustin T. Gibbs 		/* copy the result back out */
8978b8a9b1dSJustin T. Gibbs 		bcopy(ccb, addr, sizeof(union ccb));
8988b8a9b1dSJustin T. Gibbs 
8998b8a9b1dSJustin T. Gibbs 		/* and release the ccb */
9008b8a9b1dSJustin T. Gibbs 		xpt_release_ccb(ccb);
9018b8a9b1dSJustin T. Gibbs 
9028b8a9b1dSJustin T. Gibbs 		break;
9038b8a9b1dSJustin T. Gibbs 	default:
9048b8a9b1dSJustin T. Gibbs 		error = ENOTTY;
9058b8a9b1dSJustin T. Gibbs 		break;
9068b8a9b1dSJustin T. Gibbs 	}
9078b8a9b1dSJustin T. Gibbs 	return(error);
9088b8a9b1dSJustin T. Gibbs }
9098b8a9b1dSJustin T. Gibbs 
9108b8a9b1dSJustin T. Gibbs int
9118b8a9b1dSJustin T. Gibbs cam_periph_runccb(union ccb *ccb,
9128b8a9b1dSJustin T. Gibbs 		  int (*error_routine)(union ccb *ccb,
9138b8a9b1dSJustin T. Gibbs 				       cam_flags camflags,
9148b8a9b1dSJustin T. Gibbs 				       u_int32_t sense_flags),
9158b8a9b1dSJustin T. Gibbs 		  cam_flags camflags, u_int32_t sense_flags,
9168b8a9b1dSJustin T. Gibbs 		  struct devstat *ds)
9178b8a9b1dSJustin T. Gibbs {
9182b83592fSScott Long 	struct cam_sim *sim;
9198b8a9b1dSJustin T. Gibbs 	int error;
9208b8a9b1dSJustin T. Gibbs 
9218b8a9b1dSJustin T. Gibbs 	error = 0;
9222b83592fSScott Long 	sim = xpt_path_sim(ccb->ccb_h.path);
9232b83592fSScott Long 	mtx_assert(sim->mtx, MA_OWNED);
9248b8a9b1dSJustin T. Gibbs 
9258b8a9b1dSJustin T. Gibbs 	/*
9268b8a9b1dSJustin T. Gibbs 	 * If the user has supplied a stats structure, and if we understand
9278b8a9b1dSJustin T. Gibbs 	 * this particular type of ccb, record the transaction start.
9288b8a9b1dSJustin T. Gibbs 	 */
929379ad358SAlexander Motin 	if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO ||
930379ad358SAlexander Motin 	    ccb->ccb_h.func_code == XPT_ATA_IO))
9317194d335SPoul-Henning Kamp 		devstat_start_transaction(ds, NULL);
9328b8a9b1dSJustin T. Gibbs 
9338b8a9b1dSJustin T. Gibbs 	xpt_action(ccb);
9348b8a9b1dSJustin T. Gibbs 
9358b8a9b1dSJustin T. Gibbs 	do {
9368b8a9b1dSJustin T. Gibbs 		cam_periph_ccbwait(ccb);
9378b8a9b1dSJustin T. Gibbs 		if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
9388b8a9b1dSJustin T. Gibbs 			error = 0;
9398b8a9b1dSJustin T. Gibbs 		else if (error_routine != NULL)
9408b8a9b1dSJustin T. Gibbs 			error = (*error_routine)(ccb, camflags, sense_flags);
9418b8a9b1dSJustin T. Gibbs 		else
9428b8a9b1dSJustin T. Gibbs 			error = 0;
9438b8a9b1dSJustin T. Gibbs 
9448b8a9b1dSJustin T. Gibbs 	} while (error == ERESTART);
9458b8a9b1dSJustin T. Gibbs 
94683c5d981SAlexander Motin 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
9478b8a9b1dSJustin T. Gibbs 		cam_release_devq(ccb->ccb_h.path,
9488b8a9b1dSJustin T. Gibbs 				 /* relsim_flags */0,
9498b8a9b1dSJustin T. Gibbs 				 /* openings */0,
9508b8a9b1dSJustin T. Gibbs 				 /* timeout */0,
9518b8a9b1dSJustin T. Gibbs 				 /* getcount_only */ FALSE);
95283c5d981SAlexander Motin 		ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
95383c5d981SAlexander Motin 	}
9548b8a9b1dSJustin T. Gibbs 
955379ad358SAlexander Motin 	if (ds != NULL) {
956379ad358SAlexander Motin 		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
9578b8a9b1dSJustin T. Gibbs 			devstat_end_transaction(ds,
9588b8a9b1dSJustin T. Gibbs 					ccb->csio.dxfer_len,
959379ad358SAlexander Motin 					ccb->csio.tag_action & 0x3,
9608b8a9b1dSJustin T. Gibbs 					((ccb->ccb_h.flags & CAM_DIR_MASK) ==
9618b8a9b1dSJustin T. Gibbs 					CAM_DIR_NONE) ?  DEVSTAT_NO_DATA :
9628b8a9b1dSJustin T. Gibbs 					(ccb->ccb_h.flags & CAM_DIR_OUT) ?
9638b8a9b1dSJustin T. Gibbs 					DEVSTAT_WRITE :
9647194d335SPoul-Henning Kamp 					DEVSTAT_READ, NULL, NULL);
965379ad358SAlexander Motin 		} else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
966379ad358SAlexander Motin 			devstat_end_transaction(ds,
967379ad358SAlexander Motin 					ccb->ataio.dxfer_len,
968379ad358SAlexander Motin 					ccb->ataio.tag_action & 0x3,
969379ad358SAlexander Motin 					((ccb->ccb_h.flags & CAM_DIR_MASK) ==
970379ad358SAlexander Motin 					CAM_DIR_NONE) ?  DEVSTAT_NO_DATA :
971379ad358SAlexander Motin 					(ccb->ccb_h.flags & CAM_DIR_OUT) ?
972379ad358SAlexander Motin 					DEVSTAT_WRITE :
973379ad358SAlexander Motin 					DEVSTAT_READ, NULL, NULL);
974379ad358SAlexander Motin 		}
975379ad358SAlexander Motin 	}
9768b8a9b1dSJustin T. Gibbs 
9778b8a9b1dSJustin T. Gibbs 	return(error);
9788b8a9b1dSJustin T. Gibbs }
9798b8a9b1dSJustin T. Gibbs 
98087cfaf0eSJustin T. Gibbs void
98187cfaf0eSJustin T. Gibbs cam_freeze_devq(struct cam_path *path)
98287cfaf0eSJustin T. Gibbs {
98387cfaf0eSJustin T. Gibbs 
98483c5d981SAlexander Motin 	cam_freeze_devq_arg(path, 0, 0);
98583c5d981SAlexander Motin }
98683c5d981SAlexander Motin 
98783c5d981SAlexander Motin void
98883c5d981SAlexander Motin cam_freeze_devq_arg(struct cam_path *path, uint32_t flags, uint32_t arg)
98983c5d981SAlexander Motin {
99083c5d981SAlexander Motin 	struct ccb_relsim crs;
99183c5d981SAlexander Motin 
99283c5d981SAlexander Motin 	xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NONE);
99383c5d981SAlexander Motin 	crs.ccb_h.func_code = XPT_FREEZE_QUEUE;
99483c5d981SAlexander Motin 	crs.release_flags = flags;
99583c5d981SAlexander Motin 	crs.openings = arg;
99683c5d981SAlexander Motin 	crs.release_timeout = arg;
99783c5d981SAlexander Motin 	xpt_action((union ccb *)&crs);
99887cfaf0eSJustin T. Gibbs }
99987cfaf0eSJustin T. Gibbs 
10008b8a9b1dSJustin T. Gibbs u_int32_t
10018b8a9b1dSJustin T. Gibbs cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
100283c5d981SAlexander Motin 		 u_int32_t openings, u_int32_t arg,
10038b8a9b1dSJustin T. Gibbs 		 int getcount_only)
10048b8a9b1dSJustin T. Gibbs {
10058b8a9b1dSJustin T. Gibbs 	struct ccb_relsim crs;
10068b8a9b1dSJustin T. Gibbs 
1007bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
10088b8a9b1dSJustin T. Gibbs 	crs.ccb_h.func_code = XPT_REL_SIMQ;
10098b8a9b1dSJustin T. Gibbs 	crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
10108b8a9b1dSJustin T. Gibbs 	crs.release_flags = relsim_flags;
10118b8a9b1dSJustin T. Gibbs 	crs.openings = openings;
101283c5d981SAlexander Motin 	crs.release_timeout = arg;
10138b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&crs);
10148b8a9b1dSJustin T. Gibbs 	return (crs.qfrozen_cnt);
10158b8a9b1dSJustin T. Gibbs }
10168b8a9b1dSJustin T. Gibbs 
10178b8a9b1dSJustin T. Gibbs #define saved_ccb_ptr ppriv_ptr0
101883c5d981SAlexander Motin #define recovery_depth ppriv_field1
10198b8a9b1dSJustin T. Gibbs static void
102083c5d981SAlexander Motin camperiphsensedone(struct cam_periph *periph, union ccb *done_ccb)
10218b8a9b1dSJustin T. Gibbs {
102283c5d981SAlexander Motin 	union ccb      *saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
10238b8a9b1dSJustin T. Gibbs 	cam_status	status;
1024d84c90a6SAlexander Motin 	int		frozen = 0;
102583c5d981SAlexander Motin 	u_int		sense_key;
102683c5d981SAlexander Motin 	int		depth = done_ccb->ccb_h.recovery_depth;
10278b8a9b1dSJustin T. Gibbs 
10288b8a9b1dSJustin T. Gibbs 	status = done_ccb->ccb_h.status;
1029d84c90a6SAlexander Motin 	if (status & CAM_DEV_QFRZN) {
1030d84c90a6SAlexander Motin 		frozen = 1;
1031d84c90a6SAlexander Motin 		/*
1032d84c90a6SAlexander Motin 		 * Clear freeze flag now for case of retry,
1033d84c90a6SAlexander Motin 		 * freeze will be dropped later.
1034d84c90a6SAlexander Motin 		 */
1035d84c90a6SAlexander Motin 		done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1036d84c90a6SAlexander Motin 	}
10378b8a9b1dSJustin T. Gibbs 	status &= CAM_STATUS_MASK;
103883c5d981SAlexander Motin 	switch (status) {
103983c5d981SAlexander Motin 	case CAM_REQ_CMP:
104083c5d981SAlexander Motin 	{
104183c5d981SAlexander Motin 		/*
104283c5d981SAlexander Motin 		 * If we manually retrieved sense into a CCB and got
104383c5d981SAlexander Motin 		 * something other than "NO SENSE" send the updated CCB
104483c5d981SAlexander Motin 		 * back to the client via xpt_done() to be processed via
104583c5d981SAlexander Motin 		 * the error recovery code again.
104683c5d981SAlexander Motin 		 */
104783c5d981SAlexander Motin 		sense_key = saved_ccb->csio.sense_data.flags;
104883c5d981SAlexander Motin 		sense_key &= SSD_KEY;
104983c5d981SAlexander Motin 		if (sense_key != SSD_KEY_NO_SENSE) {
105083c5d981SAlexander Motin 			saved_ccb->ccb_h.status |=
105183c5d981SAlexander Motin 			    CAM_AUTOSNS_VALID;
105283c5d981SAlexander Motin 		} else {
105383c5d981SAlexander Motin 			saved_ccb->ccb_h.status &=
105483c5d981SAlexander Motin 			    ~CAM_STATUS_MASK;
105583c5d981SAlexander Motin 			saved_ccb->ccb_h.status |=
105683c5d981SAlexander Motin 			    CAM_AUTOSENSE_FAIL;
105783c5d981SAlexander Motin 		}
105883c5d981SAlexander Motin 		bcopy(saved_ccb, done_ccb, sizeof(union ccb));
105983c5d981SAlexander Motin 		xpt_free_ccb(saved_ccb);
106083c5d981SAlexander Motin 		break;
106183c5d981SAlexander Motin 	}
106283c5d981SAlexander Motin 	default:
106383c5d981SAlexander Motin 		bcopy(saved_ccb, done_ccb, sizeof(union ccb));
106483c5d981SAlexander Motin 		xpt_free_ccb(saved_ccb);
106583c5d981SAlexander Motin 		done_ccb->ccb_h.status &= ~CAM_STATUS_MASK;
106683c5d981SAlexander Motin 		done_ccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
106783c5d981SAlexander Motin 		break;
106883c5d981SAlexander Motin 	}
106983c5d981SAlexander Motin 	periph->flags &= ~CAM_PERIPH_SENSE_INPROG;
107083c5d981SAlexander Motin 	/*
107183c5d981SAlexander Motin 	 * If it is the end of recovery, drop freeze, taken due to
107283c5d981SAlexander Motin 	 * CAM_DEV_QFREEZE flag, set on recovery request.
107383c5d981SAlexander Motin 	 */
107483c5d981SAlexander Motin 	if (depth == 0) {
107583c5d981SAlexander Motin 		cam_release_devq(done_ccb->ccb_h.path,
107683c5d981SAlexander Motin 			 /*relsim_flags*/0,
107783c5d981SAlexander Motin 			 /*openings*/0,
107883c5d981SAlexander Motin 			 /*timeout*/0,
107983c5d981SAlexander Motin 			 /*getcount_only*/0);
108083c5d981SAlexander Motin 	}
108183c5d981SAlexander Motin 	/*
108283c5d981SAlexander Motin 	 * Copy frozen flag from recovery request if it is set there
108383c5d981SAlexander Motin 	 * for some reason.
108483c5d981SAlexander Motin 	 */
108583c5d981SAlexander Motin 	if (frozen != 0)
108683c5d981SAlexander Motin 		done_ccb->ccb_h.status |= CAM_DEV_QFRZN;
108783c5d981SAlexander Motin 	(*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
108883c5d981SAlexander Motin }
108983c5d981SAlexander Motin 
109083c5d981SAlexander Motin static void
109183c5d981SAlexander Motin camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
109283c5d981SAlexander Motin {
109383c5d981SAlexander Motin 	union ccb      *saved_ccb, *save_ccb;
109483c5d981SAlexander Motin 	cam_status	status;
109583c5d981SAlexander Motin 	int		frozen = 0;
109683c5d981SAlexander Motin 	struct scsi_start_stop_unit *scsi_cmd;
109783c5d981SAlexander Motin 	u_int32_t	relsim_flags, timeout;
109883c5d981SAlexander Motin 
109983c5d981SAlexander Motin 	status = done_ccb->ccb_h.status;
110083c5d981SAlexander Motin 	if (status & CAM_DEV_QFRZN) {
110183c5d981SAlexander Motin 		frozen = 1;
110283c5d981SAlexander Motin 		/*
110383c5d981SAlexander Motin 		 * Clear freeze flag now for case of retry,
110483c5d981SAlexander Motin 		 * freeze will be dropped later.
110583c5d981SAlexander Motin 		 */
110683c5d981SAlexander Motin 		done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
110783c5d981SAlexander Motin 	}
11088b8a9b1dSJustin T. Gibbs 
11098b8a9b1dSJustin T. Gibbs 	timeout = 0;
11108b8a9b1dSJustin T. Gibbs 	relsim_flags = 0;
11113393f8daSKenneth D. Merry 	saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
11128b8a9b1dSJustin T. Gibbs 
111383c5d981SAlexander Motin 	switch (status & CAM_STATUS_MASK) {
11148b8a9b1dSJustin T. Gibbs 	case CAM_REQ_CMP:
11153393f8daSKenneth D. Merry 	{
11168b8a9b1dSJustin T. Gibbs 		/*
11178b8a9b1dSJustin T. Gibbs 		 * If we have successfully taken a device from the not
11183393f8daSKenneth D. Merry 		 * ready to ready state, re-scan the device and re-get
11193393f8daSKenneth D. Merry 		 * the inquiry information.  Many devices (mostly disks)
11203393f8daSKenneth D. Merry 		 * don't properly report their inquiry information unless
11213393f8daSKenneth D. Merry 		 * they are spun up.
11228b8a9b1dSJustin T. Gibbs 		 */
11238b8a9b1dSJustin T. Gibbs 		scsi_cmd = (struct scsi_start_stop_unit *)
11248b8a9b1dSJustin T. Gibbs 				&done_ccb->csio.cdb_io.cdb_bytes;
11258b8a9b1dSJustin T. Gibbs 
11268b8a9b1dSJustin T. Gibbs 	 	if (scsi_cmd->opcode == START_STOP_UNIT)
11278b8a9b1dSJustin T. Gibbs 			xpt_async(AC_INQ_CHANGED,
11288b8a9b1dSJustin T. Gibbs 				  done_ccb->ccb_h.path, NULL);
112983c5d981SAlexander Motin 		goto final;
11303393f8daSKenneth D. Merry 	}
11318b8a9b1dSJustin T. Gibbs 	case CAM_SCSI_STATUS_ERROR:
11328b8a9b1dSJustin T. Gibbs 		scsi_cmd = (struct scsi_start_stop_unit *)
11338b8a9b1dSJustin T. Gibbs 				&done_ccb->csio.cdb_io.cdb_bytes;
113483c5d981SAlexander Motin 		if (status & CAM_AUTOSNS_VALID) {
113565afe1f4SKenneth D. Merry 			struct ccb_getdev cgd;
11368b8a9b1dSJustin T. Gibbs 			struct scsi_sense_data *sense;
11378b8a9b1dSJustin T. Gibbs 			int    error_code, sense_key, asc, ascq;
113865afe1f4SKenneth D. Merry 			scsi_sense_action err_action;
11398b8a9b1dSJustin T. Gibbs 
11408b8a9b1dSJustin T. Gibbs 			sense = &done_ccb->csio.sense_data;
11418b8a9b1dSJustin T. Gibbs 			scsi_extract_sense(sense, &error_code,
11428b8a9b1dSJustin T. Gibbs 					   &sense_key, &asc, &ascq);
11438b8a9b1dSJustin T. Gibbs 			/*
114465afe1f4SKenneth D. Merry 			 * Grab the inquiry data for this device.
114565afe1f4SKenneth D. Merry 			 */
114665afe1f4SKenneth D. Merry 			xpt_setup_ccb(&cgd.ccb_h, done_ccb->ccb_h.path,
1147bbfa4aa1SAlexander Motin 			    CAM_PRIORITY_NORMAL);
114865afe1f4SKenneth D. Merry 			cgd.ccb_h.func_code = XPT_GDEV_TYPE;
114965afe1f4SKenneth D. Merry 			xpt_action((union ccb *)&cgd);
115065afe1f4SKenneth D. Merry 			err_action = scsi_error_action(&done_ccb->csio,
115165afe1f4SKenneth D. Merry 						       &cgd.inq_data, 0);
115265afe1f4SKenneth D. Merry 			/*
11538b8a9b1dSJustin T. Gibbs 	 		 * If the error is "invalid field in CDB",
11548b8a9b1dSJustin T. Gibbs 			 * and the load/eject flag is set, turn the
11558b8a9b1dSJustin T. Gibbs 			 * flag off and try again.  This is just in
11568b8a9b1dSJustin T. Gibbs 			 * case the drive in question barfs on the
11578b8a9b1dSJustin T. Gibbs 			 * load eject flag.  The CAM code should set
11588b8a9b1dSJustin T. Gibbs 			 * the load/eject flag by default for
11598b8a9b1dSJustin T. Gibbs 			 * removable media.
11608b8a9b1dSJustin T. Gibbs 			 */
11618b8a9b1dSJustin T. Gibbs 			/* XXX KDM
11628b8a9b1dSJustin T. Gibbs 			 * Should we check to see what the specific
11638b8a9b1dSJustin T. Gibbs 			 * scsi status is??  Or does it not matter
11648b8a9b1dSJustin T. Gibbs 			 * since we already know that there was an
11658b8a9b1dSJustin T. Gibbs 			 * error, and we know what the specific
11668b8a9b1dSJustin T. Gibbs 			 * error code was, and we know what the
11678b8a9b1dSJustin T. Gibbs 			 * opcode is..
11688b8a9b1dSJustin T. Gibbs 			 */
11698b8a9b1dSJustin T. Gibbs 			if ((scsi_cmd->opcode == START_STOP_UNIT) &&
11708b8a9b1dSJustin T. Gibbs 			    ((scsi_cmd->how & SSS_LOEJ) != 0) &&
11718b8a9b1dSJustin T. Gibbs 			     (asc == 0x24) && (ascq == 0x00) &&
11728b8a9b1dSJustin T. Gibbs 			     (done_ccb->ccb_h.retry_count > 0)) {
11738b8a9b1dSJustin T. Gibbs 
11748b8a9b1dSJustin T. Gibbs 				scsi_cmd->how &= ~SSS_LOEJ;
11758b8a9b1dSJustin T. Gibbs 				xpt_action(done_ccb);
117665afe1f4SKenneth D. Merry 			} else if ((done_ccb->ccb_h.retry_count > 1)
117765afe1f4SKenneth D. Merry 				&& ((err_action & SS_MASK) != SS_FAIL)) {
117865afe1f4SKenneth D. Merry 
11798b8a9b1dSJustin T. Gibbs 				/*
11808b8a9b1dSJustin T. Gibbs 				 * In this case, the error recovery
11818b8a9b1dSJustin T. Gibbs 				 * command failed, but we've got
11828b8a9b1dSJustin T. Gibbs 				 * some retries left on it.  Give
118365afe1f4SKenneth D. Merry 				 * it another try unless this is an
118465afe1f4SKenneth D. Merry 				 * unretryable error.
11858b8a9b1dSJustin T. Gibbs 				 */
11868b8a9b1dSJustin T. Gibbs 				/* set the timeout to .5 sec */
11878b8a9b1dSJustin T. Gibbs 				relsim_flags =
11888b8a9b1dSJustin T. Gibbs 					RELSIM_RELEASE_AFTER_TIMEOUT;
11898b8a9b1dSJustin T. Gibbs 				timeout = 500;
11908b8a9b1dSJustin T. Gibbs 				xpt_action(done_ccb);
11918b8a9b1dSJustin T. Gibbs 				break;
11928b8a9b1dSJustin T. Gibbs 			} else {
11938b8a9b1dSJustin T. Gibbs 				/*
11943393f8daSKenneth D. Merry 				 * Perform the final retry with the original
11953393f8daSKenneth D. Merry 				 * CCB so that final error processing is
11963393f8daSKenneth D. Merry 				 * performed by the owner of the CCB.
11978b8a9b1dSJustin T. Gibbs 				 */
119883c5d981SAlexander Motin 				goto final;
11998b8a9b1dSJustin T. Gibbs 			}
12008b8a9b1dSJustin T. Gibbs 		} else {
120183c5d981SAlexander Motin 			save_ccb = xpt_alloc_ccb_nowait();
120283c5d981SAlexander Motin 			if (save_ccb == NULL)
120383c5d981SAlexander Motin 				goto final;
120483c5d981SAlexander Motin 			bcopy(done_ccb, save_ccb, sizeof(*save_ccb));
120583c5d981SAlexander Motin 			periph->flags |= CAM_PERIPH_SENSE_INPROG;
12068b8a9b1dSJustin T. Gibbs 			/*
120783c5d981SAlexander Motin 			 * Send a Request Sense to the device.  We
120883c5d981SAlexander Motin 			 * assume that we are in a contingent allegiance
120983c5d981SAlexander Motin 			 * condition so we do not tag this request.
12108b8a9b1dSJustin T. Gibbs 			 */
121183c5d981SAlexander Motin 			scsi_request_sense(&done_ccb->csio, /*retries*/1,
121283c5d981SAlexander Motin 					   camperiphsensedone,
121383c5d981SAlexander Motin 					   &save_ccb->csio.sense_data,
121483c5d981SAlexander Motin 					   sizeof(save_ccb->csio.sense_data),
121583c5d981SAlexander Motin 					   CAM_TAG_ACTION_NONE,
121683c5d981SAlexander Motin 					   /*sense_len*/SSD_FULL_SIZE,
121783c5d981SAlexander Motin 					   /*timeout*/5000);
121883c5d981SAlexander Motin 			done_ccb->ccb_h.pinfo.priority--;
121983c5d981SAlexander Motin 			done_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
122083c5d981SAlexander Motin 			done_ccb->ccb_h.saved_ccb_ptr = save_ccb;
122183c5d981SAlexander Motin 			done_ccb->ccb_h.recovery_depth++;
12228b8a9b1dSJustin T. Gibbs 			xpt_action(done_ccb);
12238b8a9b1dSJustin T. Gibbs 		}
12248b8a9b1dSJustin T. Gibbs 		break;
12258b8a9b1dSJustin T. Gibbs 	default:
122683c5d981SAlexander Motin final:
122783c5d981SAlexander Motin 		bcopy(saved_ccb, done_ccb, sizeof(*done_ccb));
122883c5d981SAlexander Motin 		xpt_free_ccb(saved_ccb);
122960a899a0SKenneth D. Merry 		periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
12308b8a9b1dSJustin T. Gibbs 		xpt_action(done_ccb);
12318b8a9b1dSJustin T. Gibbs 		break;
12328b8a9b1dSJustin T. Gibbs 	}
12338b8a9b1dSJustin T. Gibbs 
12348b8a9b1dSJustin T. Gibbs 	/* decrement the retry count */
12353393f8daSKenneth D. Merry 	/*
12363393f8daSKenneth D. Merry 	 * XXX This isn't appropriate in all cases.  Restructure,
12373393f8daSKenneth D. Merry 	 *     so that the retry count is only decremented on an
12383393f8daSKenneth D. Merry 	 *     actual retry.  Remeber that the orignal ccb had its
12393393f8daSKenneth D. Merry 	 *     retry count dropped before entering recovery, so
12403393f8daSKenneth D. Merry 	 *     doing it again is a bug.
12413393f8daSKenneth D. Merry 	 */
12428b8a9b1dSJustin T. Gibbs 	if (done_ccb->ccb_h.retry_count > 0)
12438b8a9b1dSJustin T. Gibbs 		done_ccb->ccb_h.retry_count--;
1244d84c90a6SAlexander Motin 	/*
1245d84c90a6SAlexander Motin 	 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on recovery
1246d84c90a6SAlexander Motin 	 * request.
1247d84c90a6SAlexander Motin 	 */
1248d84c90a6SAlexander Motin 	cam_release_devq(done_ccb->ccb_h.path,
12498b8a9b1dSJustin T. Gibbs 			 /*relsim_flags*/relsim_flags,
12508b8a9b1dSJustin T. Gibbs 			 /*openings*/0,
12518b8a9b1dSJustin T. Gibbs 			 /*timeout*/timeout,
12528b8a9b1dSJustin T. Gibbs 			 /*getcount_only*/0);
1253d84c90a6SAlexander Motin 	/* Drop freeze taken, if this recovery request got error. */
1254d84c90a6SAlexander Motin 	if (frozen != 0) {
1255d84c90a6SAlexander Motin 		cam_release_devq(done_ccb->ccb_h.path,
1256d84c90a6SAlexander Motin 			 /*relsim_flags*/0,
1257d84c90a6SAlexander Motin 			 /*openings*/0,
1258d84c90a6SAlexander Motin 			 /*timeout*/0,
1259d84c90a6SAlexander Motin 			 /*getcount_only*/0);
1260d84c90a6SAlexander Motin 	}
1261d84c90a6SAlexander Motin }
12628b8a9b1dSJustin T. Gibbs 
12638b8a9b1dSJustin T. Gibbs /*
126487cfaf0eSJustin T. Gibbs  * Generic Async Event handler.  Peripheral drivers usually
126587cfaf0eSJustin T. Gibbs  * filter out the events that require personal attention,
126687cfaf0eSJustin T. Gibbs  * and leave the rest to this function.
126787cfaf0eSJustin T. Gibbs  */
126887cfaf0eSJustin T. Gibbs void
126987cfaf0eSJustin T. Gibbs cam_periph_async(struct cam_periph *periph, u_int32_t code,
127087cfaf0eSJustin T. Gibbs 		 struct cam_path *path, void *arg)
127187cfaf0eSJustin T. Gibbs {
127287cfaf0eSJustin T. Gibbs 	switch (code) {
127387cfaf0eSJustin T. Gibbs 	case AC_LOST_DEVICE:
127487cfaf0eSJustin T. Gibbs 		cam_periph_invalidate(periph);
127587cfaf0eSJustin T. Gibbs 		break;
127687cfaf0eSJustin T. Gibbs 	default:
127787cfaf0eSJustin T. Gibbs 		break;
127887cfaf0eSJustin T. Gibbs 	}
127987cfaf0eSJustin T. Gibbs }
128087cfaf0eSJustin T. Gibbs 
128187cfaf0eSJustin T. Gibbs void
128287cfaf0eSJustin T. Gibbs cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
128387cfaf0eSJustin T. Gibbs {
128487cfaf0eSJustin T. Gibbs 	struct ccb_getdevstats cgds;
128587cfaf0eSJustin T. Gibbs 
1286bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cgds.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
128787cfaf0eSJustin T. Gibbs 	cgds.ccb_h.func_code = XPT_GDEV_STATS;
128887cfaf0eSJustin T. Gibbs 	xpt_action((union ccb *)&cgds);
128987cfaf0eSJustin T. Gibbs 	cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
129087cfaf0eSJustin T. Gibbs }
129187cfaf0eSJustin T. Gibbs 
129287cfaf0eSJustin T. Gibbs void
129387cfaf0eSJustin T. Gibbs cam_periph_freeze_after_event(struct cam_periph *periph,
129487cfaf0eSJustin T. Gibbs 			      struct timeval* event_time, u_int duration_ms)
129587cfaf0eSJustin T. Gibbs {
129687cfaf0eSJustin T. Gibbs 	struct timeval delta;
129787cfaf0eSJustin T. Gibbs 	struct timeval duration_tv;
129887cfaf0eSJustin T. Gibbs 
129987cfaf0eSJustin T. Gibbs 	microtime(&delta);
130087cfaf0eSJustin T. Gibbs 	timevalsub(&delta, event_time);
130187cfaf0eSJustin T. Gibbs 	duration_tv.tv_sec = duration_ms / 1000;
130287cfaf0eSJustin T. Gibbs 	duration_tv.tv_usec = (duration_ms % 1000) * 1000;
130387cfaf0eSJustin T. Gibbs 	if (timevalcmp(&delta, &duration_tv, <)) {
130487cfaf0eSJustin T. Gibbs 		timevalsub(&duration_tv, &delta);
130587cfaf0eSJustin T. Gibbs 
130687cfaf0eSJustin T. Gibbs 		duration_ms = duration_tv.tv_sec * 1000;
130787cfaf0eSJustin T. Gibbs 		duration_ms += duration_tv.tv_usec / 1000;
130887cfaf0eSJustin T. Gibbs 		cam_freeze_devq(periph->path);
130987cfaf0eSJustin T. Gibbs 		cam_release_devq(periph->path,
131087cfaf0eSJustin T. Gibbs 				RELSIM_RELEASE_AFTER_TIMEOUT,
131187cfaf0eSJustin T. Gibbs 				/*reduction*/0,
131287cfaf0eSJustin T. Gibbs 				/*timeout*/duration_ms,
131387cfaf0eSJustin T. Gibbs 				/*getcount_only*/0);
131487cfaf0eSJustin T. Gibbs 	}
131587cfaf0eSJustin T. Gibbs 
131687cfaf0eSJustin T. Gibbs }
131787cfaf0eSJustin T. Gibbs 
13183393f8daSKenneth D. Merry static int
13193393f8daSKenneth D. Merry camperiphscsistatuserror(union ccb *ccb, cam_flags camflags,
132083c5d981SAlexander Motin 			 u_int32_t sense_flags,
13213393f8daSKenneth D. Merry 			 int *openings, u_int32_t *relsim_flags,
132283c5d981SAlexander Motin 			 u_int32_t *timeout, const char **action_string)
13238b8a9b1dSJustin T. Gibbs {
13248b8a9b1dSJustin T. Gibbs 	int error;
13258b8a9b1dSJustin T. Gibbs 
13268b8a9b1dSJustin T. Gibbs 	switch (ccb->csio.scsi_status) {
13278b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_OK:
13288b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_COND_MET:
13298b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_INTERMED:
13308b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_INTERMED_COND_MET:
13318b8a9b1dSJustin T. Gibbs 		error = 0;
13328b8a9b1dSJustin T. Gibbs 		break;
13338b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_CMD_TERMINATED:
13348b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_CHECK_COND:
133583c5d981SAlexander Motin 		if (bootverbose)
133683c5d981SAlexander Motin 			xpt_print(ccb->ccb_h.path, "SCSI status error\n");
13373393f8daSKenneth D. Merry 		error = camperiphscsisenseerror(ccb,
13383393f8daSKenneth D. Merry 					        camflags,
13398b8a9b1dSJustin T. Gibbs 					        sense_flags,
13403393f8daSKenneth D. Merry 					        openings,
13413393f8daSKenneth D. Merry 					        relsim_flags,
134283c5d981SAlexander Motin 					        timeout,
134383c5d981SAlexander Motin 					        action_string);
13448b8a9b1dSJustin T. Gibbs 		break;
13458b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_QUEUE_FULL:
13468b8a9b1dSJustin T. Gibbs 	{
13478b8a9b1dSJustin T. Gibbs 		/* no decrement */
134882815562SJustin T. Gibbs 		struct ccb_getdevstats cgds;
13498b8a9b1dSJustin T. Gibbs 
13508b8a9b1dSJustin T. Gibbs 		/*
13518b8a9b1dSJustin T. Gibbs 		 * First off, find out what the current
13528b8a9b1dSJustin T. Gibbs 		 * transaction counts are.
13538b8a9b1dSJustin T. Gibbs 		 */
135482815562SJustin T. Gibbs 		xpt_setup_ccb(&cgds.ccb_h,
13558b8a9b1dSJustin T. Gibbs 			      ccb->ccb_h.path,
1356bbfa4aa1SAlexander Motin 			      CAM_PRIORITY_NORMAL);
135782815562SJustin T. Gibbs 		cgds.ccb_h.func_code = XPT_GDEV_STATS;
135882815562SJustin T. Gibbs 		xpt_action((union ccb *)&cgds);
13598b8a9b1dSJustin T. Gibbs 
13608b8a9b1dSJustin T. Gibbs 		/*
13618b8a9b1dSJustin T. Gibbs 		 * If we were the only transaction active, treat
13628b8a9b1dSJustin T. Gibbs 		 * the QUEUE FULL as if it were a BUSY condition.
13638b8a9b1dSJustin T. Gibbs 		 */
136482815562SJustin T. Gibbs 		if (cgds.dev_active != 0) {
136582815562SJustin T. Gibbs 			int total_openings;
136682815562SJustin T. Gibbs 
13678b8a9b1dSJustin T. Gibbs 			/*
13688b8a9b1dSJustin T. Gibbs 		 	 * Reduce the number of openings to
13698b8a9b1dSJustin T. Gibbs 			 * be 1 less than the amount it took
13708b8a9b1dSJustin T. Gibbs 			 * to get a queue full bounded by the
13718b8a9b1dSJustin T. Gibbs 			 * minimum allowed tag count for this
13728b8a9b1dSJustin T. Gibbs 			 * device.
13738b8a9b1dSJustin T. Gibbs 		 	 */
13743393f8daSKenneth D. Merry 			total_openings = cgds.dev_active + cgds.dev_openings;
13753393f8daSKenneth D. Merry 			*openings = cgds.dev_active;
13763393f8daSKenneth D. Merry 			if (*openings < cgds.mintags)
13773393f8daSKenneth D. Merry 				*openings = cgds.mintags;
13783393f8daSKenneth D. Merry 			if (*openings < total_openings)
13793393f8daSKenneth D. Merry 				*relsim_flags = RELSIM_ADJUST_OPENINGS;
13808b8a9b1dSJustin T. Gibbs 			else {
13818b8a9b1dSJustin T. Gibbs 				/*
13828b8a9b1dSJustin T. Gibbs 				 * Some devices report queue full for
13838b8a9b1dSJustin T. Gibbs 				 * temporary resource shortages.  For
13848b8a9b1dSJustin T. Gibbs 				 * this reason, we allow a minimum
13858b8a9b1dSJustin T. Gibbs 				 * tag count to be entered via a
13868b8a9b1dSJustin T. Gibbs 				 * quirk entry to prevent the queue
13878b8a9b1dSJustin T. Gibbs 				 * count on these devices from falling
13888b8a9b1dSJustin T. Gibbs 				 * to a pessimisticly low value.  We
13898b8a9b1dSJustin T. Gibbs 				 * still wait for the next successful
13908b8a9b1dSJustin T. Gibbs 				 * completion, however, before queueing
13918b8a9b1dSJustin T. Gibbs 				 * more transactions to the device.
13928b8a9b1dSJustin T. Gibbs 				 */
13933393f8daSKenneth D. Merry 				*relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
13948b8a9b1dSJustin T. Gibbs 			}
13953393f8daSKenneth D. Merry 			*timeout = 0;
13968b8a9b1dSJustin T. Gibbs 			error = ERESTART;
139796d333b7SMatt Jacob 			if (bootverbose) {
139883c5d981SAlexander Motin 				xpt_print(ccb->ccb_h.path, "Queue full\n");
139996d333b7SMatt Jacob 			}
14008b8a9b1dSJustin T. Gibbs 			break;
14018b8a9b1dSJustin T. Gibbs 		}
14028b8a9b1dSJustin T. Gibbs 		/* FALLTHROUGH */
14038b8a9b1dSJustin T. Gibbs 	}
14048b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_BUSY:
14058b8a9b1dSJustin T. Gibbs 		/*
14068b8a9b1dSJustin T. Gibbs 		 * Restart the queue after either another
14078b8a9b1dSJustin T. Gibbs 		 * command completes or a 1 second timeout.
14088b8a9b1dSJustin T. Gibbs 		 */
140996d333b7SMatt Jacob 		if (bootverbose) {
141083c5d981SAlexander Motin 			xpt_print(ccb->ccb_h.path, "Device busy\n");
141196d333b7SMatt Jacob 		}
14123393f8daSKenneth D. Merry 	 	if (ccb->ccb_h.retry_count > 0) {
1413af51b059SMatt Jacob 	 		ccb->ccb_h.retry_count--;
14148b8a9b1dSJustin T. Gibbs 			error = ERESTART;
14153393f8daSKenneth D. Merry 			*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
14168b8a9b1dSJustin T. Gibbs 				      | RELSIM_RELEASE_AFTER_CMDCMPLT;
14173393f8daSKenneth D. Merry 			*timeout = 1000;
1418af51b059SMatt Jacob 		} else {
1419af51b059SMatt Jacob 			error = EIO;
1420af51b059SMatt Jacob 		}
14218b8a9b1dSJustin T. Gibbs 		break;
14228b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_RESERV_CONFLICT:
142383c5d981SAlexander Motin 		xpt_print(ccb->ccb_h.path, "Reservation conflict\n");
14248b8a9b1dSJustin T. Gibbs 		error = EIO;
14258b8a9b1dSJustin T. Gibbs 		break;
14268b8a9b1dSJustin T. Gibbs 	default:
142783c5d981SAlexander Motin 		xpt_print(ccb->ccb_h.path, "SCSI status 0x%x\n",
1428f0d9af51SMatt Jacob 		    ccb->csio.scsi_status);
14298b8a9b1dSJustin T. Gibbs 		error = EIO;
14308b8a9b1dSJustin T. Gibbs 		break;
14318b8a9b1dSJustin T. Gibbs 	}
14323393f8daSKenneth D. Merry 	return (error);
14333393f8daSKenneth D. Merry }
14343393f8daSKenneth D. Merry 
14353393f8daSKenneth D. Merry static int
14363393f8daSKenneth D. Merry camperiphscsisenseerror(union ccb *ccb, cam_flags camflags,
143783c5d981SAlexander Motin 			u_int32_t sense_flags,
14383393f8daSKenneth D. Merry 		       int *openings, u_int32_t *relsim_flags,
143983c5d981SAlexander Motin 		       u_int32_t *timeout, const char **action_string)
14403393f8daSKenneth D. Merry {
14413393f8daSKenneth D. Merry 	struct cam_periph *periph;
144283c5d981SAlexander Motin 	union ccb *orig_ccb = ccb;
14433393f8daSKenneth D. Merry 	int error;
14443393f8daSKenneth D. Merry 
14453393f8daSKenneth D. Merry 	periph = xpt_path_periph(ccb->ccb_h.path);
144683c5d981SAlexander Motin 	if (periph->flags &
144783c5d981SAlexander Motin 	    (CAM_PERIPH_RECOVERY_INPROG | CAM_PERIPH_SENSE_INPROG)) {
14483393f8daSKenneth D. Merry 		/*
14493393f8daSKenneth D. Merry 		 * If error recovery is already in progress, don't attempt
14503393f8daSKenneth D. Merry 		 * to process this error, but requeue it unconditionally
14513393f8daSKenneth D. Merry 		 * and attempt to process it once error recovery has
14523393f8daSKenneth D. Merry 		 * completed.  This failed command is probably related to
14533393f8daSKenneth D. Merry 		 * the error that caused the currently active error recovery
14543393f8daSKenneth D. Merry 		 * action so our  current recovery efforts should also
14553393f8daSKenneth D. Merry 		 * address this command.  Be aware that the error recovery
14563393f8daSKenneth D. Merry 		 * code assumes that only one recovery action is in progress
14573393f8daSKenneth D. Merry 		 * on a particular peripheral instance at any given time
14583393f8daSKenneth D. Merry 		 * (e.g. only one saved CCB for error recovery) so it is
14593393f8daSKenneth D. Merry 		 * imperitive that we don't violate this assumption.
14603393f8daSKenneth D. Merry 		 */
14613393f8daSKenneth D. Merry 		error = ERESTART;
14623393f8daSKenneth D. Merry 	} else {
14633393f8daSKenneth D. Merry 		scsi_sense_action err_action;
14643393f8daSKenneth D. Merry 		struct ccb_getdev cgd;
14653393f8daSKenneth D. Merry 
14663393f8daSKenneth D. Merry 		/*
14673393f8daSKenneth D. Merry 		 * Grab the inquiry data for this device.
14683393f8daSKenneth D. Merry 		 */
1469bbfa4aa1SAlexander Motin 		xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, CAM_PRIORITY_NORMAL);
14703393f8daSKenneth D. Merry 		cgd.ccb_h.func_code = XPT_GDEV_TYPE;
14713393f8daSKenneth D. Merry 		xpt_action((union ccb *)&cgd);
14723393f8daSKenneth D. Merry 
14733393f8daSKenneth D. Merry 		if ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)
14743393f8daSKenneth D. Merry 			err_action = scsi_error_action(&ccb->csio,
14753393f8daSKenneth D. Merry 						       &cgd.inq_data,
14763393f8daSKenneth D. Merry 						       sense_flags);
14773393f8daSKenneth D. Merry 		else if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
14783393f8daSKenneth D. Merry 			err_action = SS_REQSENSE;
14793393f8daSKenneth D. Merry 		else
14803393f8daSKenneth D. Merry 			err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
14813393f8daSKenneth D. Merry 
14823393f8daSKenneth D. Merry 		error = err_action & SS_ERRMASK;
14833393f8daSKenneth D. Merry 
14843393f8daSKenneth D. Merry 		/*
14853393f8daSKenneth D. Merry 		 * If the recovery action will consume a retry,
14863393f8daSKenneth D. Merry 		 * make sure we actually have retries available.
14873393f8daSKenneth D. Merry 		 */
14883393f8daSKenneth D. Merry 		if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
14893393f8daSKenneth D. Merry 		 	if (ccb->ccb_h.retry_count > 0)
14903393f8daSKenneth D. Merry 		 		ccb->ccb_h.retry_count--;
14913393f8daSKenneth D. Merry 			else {
149283c5d981SAlexander Motin 				*action_string = "Retries exhausted";
14933393f8daSKenneth D. Merry 				goto sense_error_done;
14943393f8daSKenneth D. Merry 			}
14953393f8daSKenneth D. Merry 		}
14963393f8daSKenneth D. Merry 
14973393f8daSKenneth D. Merry 		if ((err_action & SS_MASK) >= SS_START) {
14983393f8daSKenneth D. Merry 			/*
14993393f8daSKenneth D. Merry 			 * Do common portions of commands that
15003393f8daSKenneth D. Merry 			 * use recovery CCBs.
15013393f8daSKenneth D. Merry 			 */
150283c5d981SAlexander Motin 			orig_ccb = xpt_alloc_ccb_nowait();
150383c5d981SAlexander Motin 			if (orig_ccb == NULL) {
150483c5d981SAlexander Motin 				*action_string = "Can't allocate recovery CCB";
15053393f8daSKenneth D. Merry 				goto sense_error_done;
15063393f8daSKenneth D. Merry 			}
15071f1158b2SAlexander Motin 			/*
15081f1158b2SAlexander Motin 			 * Clear freeze flag for original request here, as
15091f1158b2SAlexander Motin 			 * this freeze will be dropped as part of ERESTART.
15101f1158b2SAlexander Motin 			 */
15111f1158b2SAlexander Motin 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
151283c5d981SAlexander Motin 			bcopy(ccb, orig_ccb, sizeof(*orig_ccb));
15133393f8daSKenneth D. Merry 		}
15143393f8daSKenneth D. Merry 
15153393f8daSKenneth D. Merry 		switch (err_action & SS_MASK) {
15163393f8daSKenneth D. Merry 		case SS_NOP:
151783c5d981SAlexander Motin 			*action_string = "No recovery action needed";
151800e54d14SKenneth D. Merry 			error = 0;
151900e54d14SKenneth D. Merry 			break;
15203393f8daSKenneth D. Merry 		case SS_RETRY:
152183c5d981SAlexander Motin 			*action_string = "Retrying command (per sense data)";
15223393f8daSKenneth D. Merry 			error = ERESTART;
15238b8a9b1dSJustin T. Gibbs 			break;
15243393f8daSKenneth D. Merry 		case SS_FAIL:
152583c5d981SAlexander Motin 			*action_string = "Unretryable error";
15263393f8daSKenneth D. Merry 			break;
15273393f8daSKenneth D. Merry 		case SS_START:
15283393f8daSKenneth D. Merry 		{
15293393f8daSKenneth D. Merry 			int le;
15303393f8daSKenneth D. Merry 
15313393f8daSKenneth D. Merry 			/*
15323393f8daSKenneth D. Merry 			 * Send a start unit command to the device, and
15333393f8daSKenneth D. Merry 			 * then retry the command.
15343393f8daSKenneth D. Merry 			 */
153583c5d981SAlexander Motin 			*action_string = "Attempting to start unit";
153683c5d981SAlexander Motin 			periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
15373393f8daSKenneth D. Merry 
15383393f8daSKenneth D. Merry 			/*
15393393f8daSKenneth D. Merry 			 * Check for removable media and set
15403393f8daSKenneth D. Merry 			 * load/eject flag appropriately.
15413393f8daSKenneth D. Merry 			 */
15423393f8daSKenneth D. Merry 			if (SID_IS_REMOVABLE(&cgd.inq_data))
15433393f8daSKenneth D. Merry 				le = TRUE;
15443393f8daSKenneth D. Merry 			else
15453393f8daSKenneth D. Merry 				le = FALSE;
15463393f8daSKenneth D. Merry 
15473393f8daSKenneth D. Merry 			scsi_start_stop(&ccb->csio,
15483393f8daSKenneth D. Merry 					/*retries*/1,
15493393f8daSKenneth D. Merry 					camperiphdone,
15503393f8daSKenneth D. Merry 					MSG_SIMPLE_Q_TAG,
15513393f8daSKenneth D. Merry 					/*start*/TRUE,
15523393f8daSKenneth D. Merry 					/*load/eject*/le,
15533393f8daSKenneth D. Merry 					/*immediate*/FALSE,
15543393f8daSKenneth D. Merry 					SSD_FULL_SIZE,
15553393f8daSKenneth D. Merry 					/*timeout*/50000);
15563393f8daSKenneth D. Merry 			break;
15573393f8daSKenneth D. Merry 		}
15583393f8daSKenneth D. Merry 		case SS_TUR:
15593393f8daSKenneth D. Merry 		{
15603393f8daSKenneth D. Merry 			/*
15613393f8daSKenneth D. Merry 			 * Send a Test Unit Ready to the device.
15623393f8daSKenneth D. Merry 			 * If the 'many' flag is set, we send 120
15633393f8daSKenneth D. Merry 			 * test unit ready commands, one every half
15643393f8daSKenneth D. Merry 			 * second.  Otherwise, we just send one TUR.
15653393f8daSKenneth D. Merry 			 * We only want to do this if the retry
15663393f8daSKenneth D. Merry 			 * count has not been exhausted.
15673393f8daSKenneth D. Merry 			 */
15683393f8daSKenneth D. Merry 			int retries;
15693393f8daSKenneth D. Merry 
15703393f8daSKenneth D. Merry 			if ((err_action & SSQ_MANY) != 0) {
157183c5d981SAlexander Motin 				*action_string = "Polling device for readiness";
15723393f8daSKenneth D. Merry 				retries = 120;
15733393f8daSKenneth D. Merry 			} else {
157483c5d981SAlexander Motin 				*action_string = "Testing device for readiness";
15753393f8daSKenneth D. Merry 				retries = 1;
15763393f8daSKenneth D. Merry 			}
157783c5d981SAlexander Motin 			periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
15783393f8daSKenneth D. Merry 			scsi_test_unit_ready(&ccb->csio,
15793393f8daSKenneth D. Merry 					     retries,
15803393f8daSKenneth D. Merry 					     camperiphdone,
15813393f8daSKenneth D. Merry 					     MSG_SIMPLE_Q_TAG,
15823393f8daSKenneth D. Merry 					     SSD_FULL_SIZE,
15833393f8daSKenneth D. Merry 					     /*timeout*/5000);
15843393f8daSKenneth D. Merry 
15853393f8daSKenneth D. Merry 			/*
15863393f8daSKenneth D. Merry 			 * Accomplish our 500ms delay by deferring
15873393f8daSKenneth D. Merry 			 * the release of our device queue appropriately.
15883393f8daSKenneth D. Merry 			 */
15893393f8daSKenneth D. Merry 			*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
15903393f8daSKenneth D. Merry 			*timeout = 500;
15913393f8daSKenneth D. Merry 			break;
15923393f8daSKenneth D. Merry 		}
15933393f8daSKenneth D. Merry 		case SS_REQSENSE:
15943393f8daSKenneth D. Merry 		{
159583c5d981SAlexander Motin 			*action_string = "Requesting SCSI sense data";
159683c5d981SAlexander Motin 			periph->flags |= CAM_PERIPH_SENSE_INPROG;
15973393f8daSKenneth D. Merry 			/*
15983393f8daSKenneth D. Merry 			 * Send a Request Sense to the device.  We
15993393f8daSKenneth D. Merry 			 * assume that we are in a contingent allegiance
16003393f8daSKenneth D. Merry 			 * condition so we do not tag this request.
16013393f8daSKenneth D. Merry 			 */
16023393f8daSKenneth D. Merry 			scsi_request_sense(&ccb->csio, /*retries*/1,
160383c5d981SAlexander Motin 					   camperiphsensedone,
160483c5d981SAlexander Motin 					   &orig_ccb->csio.sense_data,
160583c5d981SAlexander Motin 					   sizeof(orig_ccb->csio.sense_data),
16063393f8daSKenneth D. Merry 					   CAM_TAG_ACTION_NONE,
16073393f8daSKenneth D. Merry 					   /*sense_len*/SSD_FULL_SIZE,
16083393f8daSKenneth D. Merry 					   /*timeout*/5000);
16093393f8daSKenneth D. Merry 			break;
16103393f8daSKenneth D. Merry 		}
16113393f8daSKenneth D. Merry 		default:
1612e3c29144SWarner Losh 			panic("Unhandled error action %x", err_action);
16133393f8daSKenneth D. Merry 		}
16143393f8daSKenneth D. Merry 
16153393f8daSKenneth D. Merry 		if ((err_action & SS_MASK) >= SS_START) {
16163393f8daSKenneth D. Merry 			/*
1617bbfa4aa1SAlexander Motin 			 * Drop the priority, so that the recovery
16183393f8daSKenneth D. Merry 			 * CCB is the first to execute.  Freeze the queue
16193393f8daSKenneth D. Merry 			 * after this command is sent so that we can
16203393f8daSKenneth D. Merry 			 * restore the old csio and have it queued in
16213393f8daSKenneth D. Merry 			 * the proper order before we release normal
16223393f8daSKenneth D. Merry 			 * transactions to the device.
16233393f8daSKenneth D. Merry 			 */
162483c5d981SAlexander Motin 			ccb->ccb_h.pinfo.priority--;
16253393f8daSKenneth D. Merry 			ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
162683c5d981SAlexander Motin 			ccb->ccb_h.saved_ccb_ptr = orig_ccb;
162783c5d981SAlexander Motin 			ccb->ccb_h.recovery_depth = 0;
16283393f8daSKenneth D. Merry 			error = ERESTART;
16293393f8daSKenneth D. Merry 		}
16303393f8daSKenneth D. Merry 
16313393f8daSKenneth D. Merry sense_error_done:
16323393f8daSKenneth D. Merry 		if ((err_action & SSQ_PRINT_SENSE) != 0
163383c5d981SAlexander Motin 		 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)
163483c5d981SAlexander Motin 			cam_error_print(orig_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
16353393f8daSKenneth D. Merry 	}
16363393f8daSKenneth D. Merry 	return (error);
16373393f8daSKenneth D. Merry }
16383393f8daSKenneth D. Merry 
16393393f8daSKenneth D. Merry /*
16403393f8daSKenneth D. Merry  * Generic error handler.  Peripheral drivers usually filter
16413393f8daSKenneth D. Merry  * out the errors that they handle in a unique mannor, then
16423393f8daSKenneth D. Merry  * call this function.
16433393f8daSKenneth D. Merry  */
16443393f8daSKenneth D. Merry int
16453393f8daSKenneth D. Merry cam_periph_error(union ccb *ccb, cam_flags camflags,
16463393f8daSKenneth D. Merry 		 u_int32_t sense_flags, union ccb *save_ccb)
16473393f8daSKenneth D. Merry {
16483393f8daSKenneth D. Merry 	const char *action_string;
16493393f8daSKenneth D. Merry 	cam_status  status;
16503393f8daSKenneth D. Merry 	int	    frozen;
165196d333b7SMatt Jacob 	int	    error, printed = 0;
16523393f8daSKenneth D. Merry 	int         openings;
16533393f8daSKenneth D. Merry 	u_int32_t   relsim_flags;
165473cf209fSMatt Jacob 	u_int32_t   timeout = 0;
16553393f8daSKenneth D. Merry 
16563393f8daSKenneth D. Merry 	action_string = NULL;
16573393f8daSKenneth D. Merry 	status = ccb->ccb_h.status;
16583393f8daSKenneth D. Merry 	frozen = (status & CAM_DEV_QFRZN) != 0;
16593393f8daSKenneth D. Merry 	status &= CAM_STATUS_MASK;
1660c7ec4390SMatt Jacob 	openings = relsim_flags = 0;
16613393f8daSKenneth D. Merry 
16623393f8daSKenneth D. Merry 	switch (status) {
16633393f8daSKenneth D. Merry 	case CAM_REQ_CMP:
16643393f8daSKenneth D. Merry 		error = 0;
16653393f8daSKenneth D. Merry 		break;
16663393f8daSKenneth D. Merry 	case CAM_SCSI_STATUS_ERROR:
16673393f8daSKenneth D. Merry 		error = camperiphscsistatuserror(ccb,
16683393f8daSKenneth D. Merry 						 camflags,
16693393f8daSKenneth D. Merry 						 sense_flags,
16703393f8daSKenneth D. Merry 						 &openings,
16713393f8daSKenneth D. Merry 						 &relsim_flags,
167283c5d981SAlexander Motin 						 &timeout,
167383c5d981SAlexander Motin 						 &action_string);
16743393f8daSKenneth D. Merry 		break;
16753393f8daSKenneth D. Merry 	case CAM_AUTOSENSE_FAIL:
167683c5d981SAlexander Motin 		xpt_print(ccb->ccb_h.path, "AutoSense failed\n");
1677c7ec4390SMatt Jacob 		error = EIO;	/* we have to kill the command */
1678c7ec4390SMatt Jacob 		break;
167952c9ce25SScott Long 	case CAM_ATA_STATUS_ERROR:
168052c9ce25SScott Long 		if (bootverbose && printed == 0) {
168183c5d981SAlexander Motin 			xpt_print(ccb->ccb_h.path, "ATA status error\n");
16828691755dSAlexander Motin 			cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
168352c9ce25SScott Long 			printed++;
168452c9ce25SScott Long 		}
168552c9ce25SScott Long 		/* FALLTHROUGH */
16868b8a9b1dSJustin T. Gibbs 	case CAM_REQ_CMP_ERR:
168796d333b7SMatt Jacob 		if (bootverbose && printed == 0) {
1688f0d9af51SMatt Jacob 			xpt_print(ccb->ccb_h.path,
1689f0d9af51SMatt Jacob 			    "Request completed with CAM_REQ_CMP_ERR\n");
169096d333b7SMatt Jacob 			printed++;
169196d333b7SMatt Jacob 		}
169205867503SPoul-Henning Kamp 		/* FALLTHROUGH */
16938b8a9b1dSJustin T. Gibbs 	case CAM_CMD_TIMEOUT:
169496d333b7SMatt Jacob 		if (bootverbose && printed == 0) {
1695f0d9af51SMatt Jacob 			xpt_print(ccb->ccb_h.path, "Command timed out\n");
169696d333b7SMatt Jacob 			printed++;
169796d333b7SMatt Jacob 		}
169805867503SPoul-Henning Kamp 		/* FALLTHROUGH */
16998b8a9b1dSJustin T. Gibbs 	case CAM_UNEXP_BUSFREE:
170096d333b7SMatt Jacob 		if (bootverbose && printed == 0) {
1701f0d9af51SMatt Jacob 			xpt_print(ccb->ccb_h.path, "Unexpected Bus Free\n");
170296d333b7SMatt Jacob 			printed++;
170396d333b7SMatt Jacob 		}
170405867503SPoul-Henning Kamp 		/* FALLTHROUGH */
17058b8a9b1dSJustin T. Gibbs 	case CAM_UNCOR_PARITY:
170696d333b7SMatt Jacob 		if (bootverbose && printed == 0) {
1707f0d9af51SMatt Jacob 			xpt_print(ccb->ccb_h.path,
170883c5d981SAlexander Motin 			    "Uncorrected parity error\n");
170996d333b7SMatt Jacob 			printed++;
171096d333b7SMatt Jacob 		}
171105867503SPoul-Henning Kamp 		/* FALLTHROUGH */
17128b8a9b1dSJustin T. Gibbs 	case CAM_DATA_RUN_ERR:
171396d333b7SMatt Jacob 		if (bootverbose && printed == 0) {
171483c5d981SAlexander Motin 			xpt_print(ccb->ccb_h.path, "Data overrun\n");
171596d333b7SMatt Jacob 			printed++;
171696d333b7SMatt Jacob 		}
171796d333b7SMatt Jacob 		error = EIO;	/* we have to kill the command */
17188b8a9b1dSJustin T. Gibbs 		/* decrement the number of retries */
17193393f8daSKenneth D. Merry 		if (ccb->ccb_h.retry_count > 0) {
17208b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.retry_count--;
17218b8a9b1dSJustin T. Gibbs 			error = ERESTART;
17228b8a9b1dSJustin T. Gibbs 		} else {
172383c5d981SAlexander Motin 			action_string = "Retries exhausted";
17248b8a9b1dSJustin T. Gibbs 			error = EIO;
17258b8a9b1dSJustin T. Gibbs 		}
17268b8a9b1dSJustin T. Gibbs 		break;
17278b8a9b1dSJustin T. Gibbs 	case CAM_UA_ABORT:
17288b8a9b1dSJustin T. Gibbs 	case CAM_UA_TERMIO:
17298b8a9b1dSJustin T. Gibbs 	case CAM_MSG_REJECT_REC:
17308b8a9b1dSJustin T. Gibbs 		/* XXX Don't know that these are correct */
17318b8a9b1dSJustin T. Gibbs 		error = EIO;
17328b8a9b1dSJustin T. Gibbs 		break;
17338b8a9b1dSJustin T. Gibbs 	case CAM_SEL_TIMEOUT:
17348b8a9b1dSJustin T. Gibbs 	{
17358b8a9b1dSJustin T. Gibbs 		struct cam_path *newpath;
17368b8a9b1dSJustin T. Gibbs 
17373393f8daSKenneth D. Merry 		if ((camflags & CAM_RETRY_SELTO) != 0) {
17383393f8daSKenneth D. Merry 			if (ccb->ccb_h.retry_count > 0) {
17393393f8daSKenneth D. Merry 
17403393f8daSKenneth D. Merry 				ccb->ccb_h.retry_count--;
17413393f8daSKenneth D. Merry 				error = ERESTART;
174296d333b7SMatt Jacob 				if (bootverbose && printed == 0) {
1743f0d9af51SMatt Jacob 					xpt_print(ccb->ccb_h.path,
174483c5d981SAlexander Motin 					    "Selection timeout\n");
174596d333b7SMatt Jacob 					printed++;
174696d333b7SMatt Jacob 				}
17473393f8daSKenneth D. Merry 
17483393f8daSKenneth D. Merry 				/*
174973cf209fSMatt Jacob 				 * Wait a bit to give the device
17503393f8daSKenneth D. Merry 				 * time to recover before we try again.
17513393f8daSKenneth D. Merry 				 */
17523393f8daSKenneth D. Merry 				relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
175373cf209fSMatt Jacob 				timeout = periph_selto_delay;
17543393f8daSKenneth D. Merry 				break;
17553393f8daSKenneth D. Merry 			}
17563393f8daSKenneth D. Merry 		}
17573393f8daSKenneth D. Merry 		error = ENXIO;
17588b8a9b1dSJustin T. Gibbs 		/* Should we do more if we can't create the path?? */
17598b8a9b1dSJustin T. Gibbs 		if (xpt_create_path(&newpath, xpt_path_periph(ccb->ccb_h.path),
17608b8a9b1dSJustin T. Gibbs 				    xpt_path_path_id(ccb->ccb_h.path),
17618b8a9b1dSJustin T. Gibbs 				    xpt_path_target_id(ccb->ccb_h.path),
17628b8a9b1dSJustin T. Gibbs 				    CAM_LUN_WILDCARD) != CAM_REQ_CMP)
17638b8a9b1dSJustin T. Gibbs 			break;
17643393f8daSKenneth D. Merry 
17658b8a9b1dSJustin T. Gibbs 		/*
17668b8a9b1dSJustin T. Gibbs 		 * Let peripheral drivers know that this device has gone
17678b8a9b1dSJustin T. Gibbs 		 * away.
17688b8a9b1dSJustin T. Gibbs 		 */
17698b8a9b1dSJustin T. Gibbs 		xpt_async(AC_LOST_DEVICE, newpath, NULL);
17708b8a9b1dSJustin T. Gibbs 		xpt_free_path(newpath);
17718b8a9b1dSJustin T. Gibbs 		break;
17728b8a9b1dSJustin T. Gibbs 	}
17738b8a9b1dSJustin T. Gibbs 	case CAM_REQ_INVALID:
17748b8a9b1dSJustin T. Gibbs 	case CAM_PATH_INVALID:
17758b8a9b1dSJustin T. Gibbs 	case CAM_DEV_NOT_THERE:
17768b8a9b1dSJustin T. Gibbs 	case CAM_NO_HBA:
17778b8a9b1dSJustin T. Gibbs 	case CAM_PROVIDE_FAIL:
17788b8a9b1dSJustin T. Gibbs 	case CAM_REQ_TOO_BIG:
17799a014e6fSIan Dowse 	case CAM_LUN_INVALID:
17809a014e6fSIan Dowse 	case CAM_TID_INVALID:
17818b8a9b1dSJustin T. Gibbs 		error = EINVAL;
17828b8a9b1dSJustin T. Gibbs 		break;
17838b8a9b1dSJustin T. Gibbs 	case CAM_SCSI_BUS_RESET:
17848b8a9b1dSJustin T. Gibbs 	case CAM_BDR_SENT:
17853393f8daSKenneth D. Merry 		/*
17863393f8daSKenneth D. Merry 		 * Commands that repeatedly timeout and cause these
17873393f8daSKenneth D. Merry 		 * kinds of error recovery actions, should return
17883393f8daSKenneth D. Merry 		 * CAM_CMD_TIMEOUT, which allows us to safely assume
17893393f8daSKenneth D. Merry 		 * that this command was an innocent bystander to
17903393f8daSKenneth D. Merry 		 * these events and should be unconditionally
17913393f8daSKenneth D. Merry 		 * retried.
17923393f8daSKenneth D. Merry 		 */
179396d333b7SMatt Jacob 		if (bootverbose && printed == 0) {
179496d333b7SMatt Jacob 			xpt_print_path(ccb->ccb_h.path);
179596d333b7SMatt Jacob 			if (status == CAM_BDR_SENT)
179696d333b7SMatt Jacob 				printf("Bus Device Reset sent\n");
179796d333b7SMatt Jacob 			else
179896d333b7SMatt Jacob 				printf("Bus Reset issued\n");
179996d333b7SMatt Jacob 			printed++;
180096d333b7SMatt Jacob 		}
18013393f8daSKenneth D. Merry 		/* FALLTHROUGH */
18028b8a9b1dSJustin T. Gibbs 	case CAM_REQUEUE_REQ:
18033393f8daSKenneth D. Merry 		/* Unconditional requeue */
18048b8a9b1dSJustin T. Gibbs 		error = ERESTART;
180596d333b7SMatt Jacob 		if (bootverbose && printed == 0) {
180683c5d981SAlexander Motin 			xpt_print(ccb->ccb_h.path, "Request requeued\n");
180796d333b7SMatt Jacob 			printed++;
180896d333b7SMatt Jacob 		}
18098b8a9b1dSJustin T. Gibbs 		break;
18108b8a9b1dSJustin T. Gibbs 	case CAM_RESRC_UNAVAIL:
181173cf209fSMatt Jacob 		/* Wait a bit for the resource shortage to abate. */
181273cf209fSMatt Jacob 		timeout = periph_noresrc_delay;
181373cf209fSMatt Jacob 		/* FALLTHROUGH */
18148b8a9b1dSJustin T. Gibbs 	case CAM_BUSY:
181573cf209fSMatt Jacob 		if (timeout == 0) {
181673cf209fSMatt Jacob 			/* Wait a bit for the busy condition to abate. */
181773cf209fSMatt Jacob 			timeout = periph_busy_delay;
181873cf209fSMatt Jacob 		}
181973cf209fSMatt Jacob 		relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
182073cf209fSMatt Jacob 		/* FALLTHROUGH */
18218b8a9b1dSJustin T. Gibbs 	default:
18228b8a9b1dSJustin T. Gibbs 		/* decrement the number of retries */
18233393f8daSKenneth D. Merry 		if (ccb->ccb_h.retry_count > 0) {
18248b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.retry_count--;
18258b8a9b1dSJustin T. Gibbs 			error = ERESTART;
182696d333b7SMatt Jacob 			if (bootverbose && printed == 0) {
182783c5d981SAlexander Motin 				xpt_print(ccb->ccb_h.path, "CAM status 0x%x\n",
1828f0d9af51SMatt Jacob 				    status);
182996d333b7SMatt Jacob 				printed++;
183096d333b7SMatt Jacob 			}
18318b8a9b1dSJustin T. Gibbs 		} else {
18328b8a9b1dSJustin T. Gibbs 			error = EIO;
183383c5d981SAlexander Motin 			action_string = "Retries exhausted";
18348b8a9b1dSJustin T. Gibbs 		}
18358b8a9b1dSJustin T. Gibbs 		break;
18368b8a9b1dSJustin T. Gibbs 	}
18378b8a9b1dSJustin T. Gibbs 
183834707c9fSMatt Jacob 	/*
183934707c9fSMatt Jacob 	 * If we have and error and are booting verbosely, whine
184034707c9fSMatt Jacob 	 * *unless* this was a non-retryable selection timeout.
184134707c9fSMatt Jacob 	 */
184234707c9fSMatt Jacob 	if (error != 0 && bootverbose &&
184334707c9fSMatt Jacob 	    !(status == CAM_SEL_TIMEOUT && (camflags & CAM_RETRY_SELTO) == 0)) {
18440f3e2159SAlexander Motin 		if (error != ERESTART) {
18453393f8daSKenneth D. Merry 			if (action_string == NULL)
184683c5d981SAlexander Motin 				action_string = "Unretryable error";
184783c5d981SAlexander Motin 			xpt_print(ccb->ccb_h.path, "Error %d, %s\n",
184883c5d981SAlexander Motin 			    error, action_string);
184983c5d981SAlexander Motin 		} else if (action_string != NULL)
1850f0d9af51SMatt Jacob 			xpt_print(ccb->ccb_h.path, "%s\n", action_string);
185183c5d981SAlexander Motin 		else
185283c5d981SAlexander Motin 			xpt_print(ccb->ccb_h.path, "Retrying command\n");
18530f3e2159SAlexander Motin 	}
18540f3e2159SAlexander Motin 
18550f3e2159SAlexander Motin 	/* Attempt a retry */
18560f3e2159SAlexander Motin 	if (error == ERESTART || error == 0) {
18570f3e2159SAlexander Motin 		if (frozen != 0)
18580f3e2159SAlexander Motin 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
18590f3e2159SAlexander Motin 		if (error == ERESTART)
18600f3e2159SAlexander Motin 			xpt_action(ccb);
18610f3e2159SAlexander Motin 		if (frozen != 0)
18620f3e2159SAlexander Motin 			cam_release_devq(ccb->ccb_h.path,
18630f3e2159SAlexander Motin 					 relsim_flags,
18640f3e2159SAlexander Motin 					 openings,
18650f3e2159SAlexander Motin 					 timeout,
18660f3e2159SAlexander Motin 					 /*getcount_only*/0);
18673393f8daSKenneth D. Merry 	}
18688b8a9b1dSJustin T. Gibbs 
18698b8a9b1dSJustin T. Gibbs 	return (error);
18708b8a9b1dSJustin T. Gibbs }
1871