xref: /freebsd/sys/cam/cam_periph.c (revision 65afe1f4543b9729b5824980a99eca58ddc48f61)
18b8a9b1dSJustin T. Gibbs /*
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>
370ec81012SJohn Polstra #include <sys/linker_set.h>
389626b608SPoul-Henning Kamp #include <sys/bio.h>
39f34fa851SJohn Baldwin #include <sys/lock.h>
40f34fa851SJohn Baldwin #include <sys/mutex.h>
418b8a9b1dSJustin T. Gibbs #include <sys/buf.h>
428b8a9b1dSJustin T. Gibbs #include <sys/proc.h>
438b8a9b1dSJustin T. Gibbs #include <sys/devicestat.h>
4475f51904SPeter Wemm #include <sys/bus.h>
458b8a9b1dSJustin T. Gibbs #include <vm/vm.h>
468b8a9b1dSJustin T. Gibbs #include <vm/vm_extern.h>
478b8a9b1dSJustin T. Gibbs 
488b8a9b1dSJustin T. Gibbs #include <cam/cam.h>
498b8a9b1dSJustin T. Gibbs #include <cam/cam_ccb.h>
508b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt_periph.h>
518b8a9b1dSJustin T. Gibbs #include <cam/cam_periph.h>
528b8a9b1dSJustin T. Gibbs #include <cam/cam_debug.h>
538b8a9b1dSJustin T. Gibbs 
548b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_all.h>
558b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_message.h>
568b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_pass.h>
578b8a9b1dSJustin T. Gibbs 
588b8a9b1dSJustin T. Gibbs static	u_int		camperiphnextunit(struct periph_driver *p_drv,
59501468a5SKenneth D. Merry 					  u_int newunit, int wired,
60501468a5SKenneth D. Merry 					  path_id_t pathid, target_id_t target,
61501468a5SKenneth D. Merry 					  lun_id_t lun);
628b8a9b1dSJustin T. Gibbs static	u_int		camperiphunit(struct periph_driver *p_drv,
63501468a5SKenneth D. Merry 				      path_id_t pathid, target_id_t target,
64501468a5SKenneth D. Merry 				      lun_id_t lun);
658b8a9b1dSJustin T. Gibbs static	void		camperiphdone(struct cam_periph *periph,
668b8a9b1dSJustin T. Gibbs 					union ccb *done_ccb);
678b8a9b1dSJustin T. Gibbs static  void		camperiphfree(struct cam_periph *periph);
683393f8daSKenneth D. Merry static int		camperiphscsistatuserror(union ccb *ccb,
693393f8daSKenneth D. Merry 						 cam_flags camflags,
703393f8daSKenneth D. Merry 						 u_int32_t sense_flags,
713393f8daSKenneth D. Merry 						 union ccb *save_ccb,
723393f8daSKenneth D. Merry 						 int *openings,
733393f8daSKenneth D. Merry 						 u_int32_t *relsim_flags,
743393f8daSKenneth D. Merry 						 u_int32_t *timeout);
753393f8daSKenneth D. Merry static	int		camperiphscsisenseerror(union ccb *ccb,
763393f8daSKenneth D. Merry 					        cam_flags camflags,
773393f8daSKenneth D. Merry 					        u_int32_t sense_flags,
783393f8daSKenneth D. Merry 					        union ccb *save_ccb,
793393f8daSKenneth D. Merry 					        int *openings,
803393f8daSKenneth D. Merry 					        u_int32_t *relsim_flags,
813393f8daSKenneth D. Merry 					        u_int32_t *timeout);
828b8a9b1dSJustin T. Gibbs 
830b7c27b9SPeter Wemm static int nperiph_drivers;
840b7c27b9SPeter Wemm struct periph_driver **periph_drivers;
850b7c27b9SPeter Wemm 
860b7c27b9SPeter Wemm void
870b7c27b9SPeter Wemm periphdriver_register(void *data)
880b7c27b9SPeter Wemm {
890b7c27b9SPeter Wemm 	struct periph_driver **newdrivers, **old;
900b7c27b9SPeter Wemm 	int ndrivers;
910b7c27b9SPeter Wemm 
920b7c27b9SPeter Wemm 	ndrivers = nperiph_drivers + 2;
93a163d034SWarner Losh 	newdrivers = malloc(sizeof(*newdrivers) * ndrivers, M_TEMP, M_WAITOK);
940b7c27b9SPeter Wemm 	if (periph_drivers)
950b7c27b9SPeter Wemm 		bcopy(periph_drivers, newdrivers,
96623db360SKenneth D. Merry 		      sizeof(*newdrivers) * nperiph_drivers);
970b7c27b9SPeter Wemm 	newdrivers[nperiph_drivers] = (struct periph_driver *)data;
980b7c27b9SPeter Wemm 	newdrivers[nperiph_drivers + 1] = NULL;
990b7c27b9SPeter Wemm 	old = periph_drivers;
1000b7c27b9SPeter Wemm 	periph_drivers = newdrivers;
1010b7c27b9SPeter Wemm 	if (old)
1020b7c27b9SPeter Wemm 		free(old, M_TEMP);
1030b7c27b9SPeter Wemm 	nperiph_drivers++;
1040b7c27b9SPeter Wemm }
1050b7c27b9SPeter Wemm 
1068b8a9b1dSJustin T. Gibbs cam_status
107ee9c90c7SKenneth D. Merry cam_periph_alloc(periph_ctor_t *periph_ctor,
108ee9c90c7SKenneth D. Merry 		 periph_oninv_t *periph_oninvalidate,
109ee9c90c7SKenneth D. Merry 		 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
110ee9c90c7SKenneth D. Merry 		 char *name, cam_periph_type type, struct cam_path *path,
111ee9c90c7SKenneth D. Merry 		 ac_callback_t *ac_callback, ac_code code, void *arg)
1128b8a9b1dSJustin T. Gibbs {
1138b8a9b1dSJustin T. Gibbs 	struct		periph_driver **p_drv;
1148b8a9b1dSJustin T. Gibbs 	struct		cam_periph *periph;
1158b8a9b1dSJustin T. Gibbs 	struct		cam_periph *cur_periph;
1168b8a9b1dSJustin T. Gibbs 	path_id_t	path_id;
1178b8a9b1dSJustin T. Gibbs 	target_id_t	target_id;
1188b8a9b1dSJustin T. Gibbs 	lun_id_t	lun_id;
1198b8a9b1dSJustin T. Gibbs 	cam_status	status;
1208b8a9b1dSJustin T. Gibbs 	u_int		init_level;
1218b8a9b1dSJustin T. Gibbs 	int s;
1228b8a9b1dSJustin T. Gibbs 
1238b8a9b1dSJustin T. Gibbs 	init_level = 0;
1248b8a9b1dSJustin T. Gibbs 	/*
1258b8a9b1dSJustin T. Gibbs 	 * Handle Hot-Plug scenarios.  If there is already a peripheral
1268b8a9b1dSJustin T. Gibbs 	 * of our type assigned to this path, we are likely waiting for
1278b8a9b1dSJustin T. Gibbs 	 * final close on an old, invalidated, peripheral.  If this is
1288b8a9b1dSJustin T. Gibbs 	 * the case, queue up a deferred call to the peripheral's async
1298b8a9b1dSJustin T. Gibbs 	 * handler.  If it looks like a mistaken re-alloation, complain.
1308b8a9b1dSJustin T. Gibbs 	 */
1318b8a9b1dSJustin T. Gibbs 	if ((periph = cam_periph_find(path, name)) != NULL) {
1328b8a9b1dSJustin T. Gibbs 
1338b8a9b1dSJustin T. Gibbs 		if ((periph->flags & CAM_PERIPH_INVALID) != 0
1348b8a9b1dSJustin T. Gibbs 		 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
1358b8a9b1dSJustin T. Gibbs 			periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
1368b8a9b1dSJustin T. Gibbs 			periph->deferred_callback = ac_callback;
1378b8a9b1dSJustin T. Gibbs 			periph->deferred_ac = code;
1388b8a9b1dSJustin T. Gibbs 			return (CAM_REQ_INPROG);
1398b8a9b1dSJustin T. Gibbs 		} else {
1408b8a9b1dSJustin T. Gibbs 			printf("cam_periph_alloc: attempt to re-allocate "
1418b8a9b1dSJustin T. Gibbs 			       "valid device %s%d rejected\n",
1428b8a9b1dSJustin T. Gibbs 			       periph->periph_name, periph->unit_number);
1438b8a9b1dSJustin T. Gibbs 		}
1448b8a9b1dSJustin T. Gibbs 		return (CAM_REQ_INVALID);
1458b8a9b1dSJustin T. Gibbs 	}
1468b8a9b1dSJustin T. Gibbs 
1478b8a9b1dSJustin T. Gibbs 	periph = (struct cam_periph *)malloc(sizeof(*periph), M_DEVBUF,
1488b8a9b1dSJustin T. Gibbs 					     M_NOWAIT);
1498b8a9b1dSJustin T. Gibbs 
1508b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
1518b8a9b1dSJustin T. Gibbs 		return (CAM_RESRC_UNAVAIL);
1528b8a9b1dSJustin T. Gibbs 
1538b8a9b1dSJustin T. Gibbs 	init_level++;
1548b8a9b1dSJustin T. Gibbs 
1550b7c27b9SPeter Wemm 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
1568b8a9b1dSJustin T. Gibbs 		if (strcmp((*p_drv)->driver_name, name) == 0)
1578b8a9b1dSJustin T. Gibbs 			break;
1588b8a9b1dSJustin T. Gibbs 	}
1598b8a9b1dSJustin T. Gibbs 
1608b8a9b1dSJustin T. Gibbs 	path_id = xpt_path_path_id(path);
1618b8a9b1dSJustin T. Gibbs 	target_id = xpt_path_target_id(path);
1628b8a9b1dSJustin T. Gibbs 	lun_id = xpt_path_lun_id(path);
1638b8a9b1dSJustin T. Gibbs 	bzero(periph, sizeof(*periph));
1648b8a9b1dSJustin T. Gibbs 	cam_init_pinfo(&periph->pinfo);
1658b8a9b1dSJustin T. Gibbs 	periph->periph_start = periph_start;
1668b8a9b1dSJustin T. Gibbs 	periph->periph_dtor = periph_dtor;
167ee9c90c7SKenneth D. Merry 	periph->periph_oninval = periph_oninvalidate;
1688b8a9b1dSJustin T. Gibbs 	periph->type = type;
1698b8a9b1dSJustin T. Gibbs 	periph->periph_name = name;
1708b8a9b1dSJustin T. Gibbs 	periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
1718b8a9b1dSJustin T. Gibbs 	periph->immediate_priority = CAM_PRIORITY_NONE;
1728b8a9b1dSJustin T. Gibbs 	periph->refcount = 0;
1738b8a9b1dSJustin T. Gibbs 	SLIST_INIT(&periph->ccb_list);
1748b8a9b1dSJustin T. Gibbs 	status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
1758b8a9b1dSJustin T. Gibbs 	if (status != CAM_REQ_CMP)
1768b8a9b1dSJustin T. Gibbs 		goto failure;
1778b8a9b1dSJustin T. Gibbs 
1788b8a9b1dSJustin T. Gibbs 	periph->path = path;
1798b8a9b1dSJustin T. Gibbs 	init_level++;
1808b8a9b1dSJustin T. Gibbs 
1818b8a9b1dSJustin T. Gibbs 	status = xpt_add_periph(periph);
1828b8a9b1dSJustin T. Gibbs 
1838b8a9b1dSJustin T. Gibbs 	if (status != CAM_REQ_CMP)
1848b8a9b1dSJustin T. Gibbs 		goto failure;
1858b8a9b1dSJustin T. Gibbs 
1868b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
1878b8a9b1dSJustin T. Gibbs 	cur_periph = TAILQ_FIRST(&(*p_drv)->units);
1888b8a9b1dSJustin T. Gibbs 	while (cur_periph != NULL
1898b8a9b1dSJustin T. Gibbs 	    && cur_periph->unit_number < periph->unit_number)
1908b8a9b1dSJustin T. Gibbs 		cur_periph = TAILQ_NEXT(cur_periph, unit_links);
1918b8a9b1dSJustin T. Gibbs 
1928b8a9b1dSJustin T. Gibbs 	if (cur_periph != NULL)
1938b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
1948b8a9b1dSJustin T. Gibbs 	else {
1958b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
1968b8a9b1dSJustin T. Gibbs 		(*p_drv)->generation++;
1978b8a9b1dSJustin T. Gibbs 	}
1988b8a9b1dSJustin T. Gibbs 
1998b8a9b1dSJustin T. Gibbs 	splx(s);
2008b8a9b1dSJustin T. Gibbs 
2018b8a9b1dSJustin T. Gibbs 	init_level++;
2028b8a9b1dSJustin T. Gibbs 
2038b8a9b1dSJustin T. Gibbs 	status = periph_ctor(periph, arg);
2048b8a9b1dSJustin T. Gibbs 
2058b8a9b1dSJustin T. Gibbs 	if (status == CAM_REQ_CMP)
2068b8a9b1dSJustin T. Gibbs 		init_level++;
2078b8a9b1dSJustin T. Gibbs 
2088b8a9b1dSJustin T. Gibbs failure:
2098b8a9b1dSJustin T. Gibbs 	switch (init_level) {
2108b8a9b1dSJustin T. Gibbs 	case 4:
2118b8a9b1dSJustin T. Gibbs 		/* Initialized successfully */
2128b8a9b1dSJustin T. Gibbs 		break;
2138b8a9b1dSJustin T. Gibbs 	case 3:
2148b8a9b1dSJustin T. Gibbs 		s = splsoftcam();
2158b8a9b1dSJustin T. Gibbs 		TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
2168b8a9b1dSJustin T. Gibbs 		splx(s);
2178b8a9b1dSJustin T. Gibbs 		xpt_remove_periph(periph);
2187379c88fSPoul-Henning Kamp 		/* FALLTHROUGH */
2198b8a9b1dSJustin T. Gibbs 	case 2:
2208b8a9b1dSJustin T. Gibbs 		xpt_free_path(periph->path);
2217379c88fSPoul-Henning Kamp 		/* FALLTHROUGH */
2228b8a9b1dSJustin T. Gibbs 	case 1:
2238b8a9b1dSJustin T. Gibbs 		free(periph, M_DEVBUF);
2247379c88fSPoul-Henning Kamp 		/* FALLTHROUGH */
2258b8a9b1dSJustin T. Gibbs 	case 0:
2268b8a9b1dSJustin T. Gibbs 		/* No cleanup to perform. */
2278b8a9b1dSJustin T. Gibbs 		break;
2288b8a9b1dSJustin T. Gibbs 	default:
2298b8a9b1dSJustin T. Gibbs 		panic("cam_periph_alloc: Unkown init level");
2308b8a9b1dSJustin T. Gibbs 	}
2318b8a9b1dSJustin T. Gibbs 	return(status);
2328b8a9b1dSJustin T. Gibbs }
2338b8a9b1dSJustin T. Gibbs 
2348b8a9b1dSJustin T. Gibbs /*
2358b8a9b1dSJustin T. Gibbs  * Find a peripheral structure with the specified path, target, lun,
2368b8a9b1dSJustin T. Gibbs  * and (optionally) type.  If the name is NULL, this function will return
2378b8a9b1dSJustin T. Gibbs  * the first peripheral driver that matches the specified path.
2388b8a9b1dSJustin T. Gibbs  */
2398b8a9b1dSJustin T. Gibbs struct cam_periph *
2408b8a9b1dSJustin T. Gibbs cam_periph_find(struct cam_path *path, char *name)
2418b8a9b1dSJustin T. Gibbs {
2428b8a9b1dSJustin T. Gibbs 	struct periph_driver **p_drv;
2438b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph;
2448b8a9b1dSJustin T. Gibbs 	int s;
2458b8a9b1dSJustin T. Gibbs 
2460b7c27b9SPeter Wemm 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
2478b8a9b1dSJustin T. Gibbs 
2488b8a9b1dSJustin T. Gibbs 		if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
2498b8a9b1dSJustin T. Gibbs 			continue;
2508b8a9b1dSJustin T. Gibbs 
2518b8a9b1dSJustin T. Gibbs 		s = splsoftcam();
25237d40066SPoul-Henning Kamp 		TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
2538b8a9b1dSJustin T. Gibbs 			if (xpt_path_comp(periph->path, path) == 0) {
2548b8a9b1dSJustin T. Gibbs 				splx(s);
2558b8a9b1dSJustin T. Gibbs 				return(periph);
2568b8a9b1dSJustin T. Gibbs 			}
2578b8a9b1dSJustin T. Gibbs 		}
2588b8a9b1dSJustin T. Gibbs 		splx(s);
2598b8a9b1dSJustin T. Gibbs 		if (name != NULL)
2608b8a9b1dSJustin T. Gibbs 			return(NULL);
2618b8a9b1dSJustin T. Gibbs 	}
2628b8a9b1dSJustin T. Gibbs 	return(NULL);
2638b8a9b1dSJustin T. Gibbs }
2648b8a9b1dSJustin T. Gibbs 
2658b8a9b1dSJustin T. Gibbs cam_status
2668b8a9b1dSJustin T. Gibbs cam_periph_acquire(struct cam_periph *periph)
2678b8a9b1dSJustin T. Gibbs {
2688b8a9b1dSJustin T. Gibbs 	int s;
2698b8a9b1dSJustin T. Gibbs 
2708b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
2718b8a9b1dSJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
2728b8a9b1dSJustin T. Gibbs 
2738b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
2748b8a9b1dSJustin T. Gibbs 	periph->refcount++;
2758b8a9b1dSJustin T. Gibbs 	splx(s);
2768b8a9b1dSJustin T. Gibbs 
2778b8a9b1dSJustin T. Gibbs 	return(CAM_REQ_CMP);
2788b8a9b1dSJustin T. Gibbs }
2798b8a9b1dSJustin T. Gibbs 
2808b8a9b1dSJustin T. Gibbs void
2818b8a9b1dSJustin T. Gibbs cam_periph_release(struct cam_periph *periph)
2828b8a9b1dSJustin T. Gibbs {
2838b8a9b1dSJustin T. Gibbs 	int s;
2848b8a9b1dSJustin T. Gibbs 
2858b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
2868b8a9b1dSJustin T. Gibbs 		return;
2878b8a9b1dSJustin T. Gibbs 
2888b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
2898b8a9b1dSJustin T. Gibbs 	if ((--periph->refcount == 0)
2908b8a9b1dSJustin T. Gibbs 	 && (periph->flags & CAM_PERIPH_INVALID)) {
2918b8a9b1dSJustin T. Gibbs 		camperiphfree(periph);
2928b8a9b1dSJustin T. Gibbs 	}
2938b8a9b1dSJustin T. Gibbs 	splx(s);
2948b8a9b1dSJustin T. Gibbs 
2958b8a9b1dSJustin T. Gibbs }
2968b8a9b1dSJustin T. Gibbs 
2978b8a9b1dSJustin T. Gibbs /*
2988b8a9b1dSJustin T. Gibbs  * Look for the next unit number that is not currently in use for this
2998b8a9b1dSJustin T. Gibbs  * peripheral type starting at "newunit".  Also exclude unit numbers that
3008b8a9b1dSJustin T. Gibbs  * are reserved by for future "hardwiring" unless we already know that this
3018b8a9b1dSJustin T. Gibbs  * is a potential wired device.  Only assume that the device is "wired" the
3028b8a9b1dSJustin T. Gibbs  * first time through the loop since after that we'll be looking at unit
3038b8a9b1dSJustin T. Gibbs  * numbers that did not match a wiring entry.
3048b8a9b1dSJustin T. Gibbs  */
3058b8a9b1dSJustin T. Gibbs static u_int
306501468a5SKenneth D. Merry camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
307501468a5SKenneth D. Merry 		  path_id_t pathid, target_id_t target, lun_id_t lun)
3088b8a9b1dSJustin T. Gibbs {
3098b8a9b1dSJustin T. Gibbs 	struct	cam_periph *periph;
3102398f0cdSPeter Wemm 	char	*periph_name;
3118b8a9b1dSJustin T. Gibbs 	int	s;
3122398f0cdSPeter Wemm 	int	i, val, dunit, r;
3132398f0cdSPeter Wemm 	const char *dname, *strval;
3148b8a9b1dSJustin T. Gibbs 
3158b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
3168b8a9b1dSJustin T. Gibbs 	periph_name = p_drv->driver_name;
3178b8a9b1dSJustin T. Gibbs 	for (;;newunit++) {
3188b8a9b1dSJustin T. Gibbs 
3198b8a9b1dSJustin T. Gibbs 		for (periph = TAILQ_FIRST(&p_drv->units);
3208b8a9b1dSJustin T. Gibbs 		     periph != NULL && periph->unit_number != newunit;
3218b8a9b1dSJustin T. Gibbs 		     periph = TAILQ_NEXT(periph, unit_links))
3228b8a9b1dSJustin T. Gibbs 			;
3238b8a9b1dSJustin T. Gibbs 
3248b8a9b1dSJustin T. Gibbs 		if (periph != NULL && periph->unit_number == newunit) {
3258b8a9b1dSJustin T. Gibbs 			if (wired != 0) {
3268b8a9b1dSJustin T. Gibbs 				xpt_print_path(periph->path);
3278b8a9b1dSJustin T. Gibbs 				printf("Duplicate Wired Device entry!\n");
3288b8a9b1dSJustin T. Gibbs 				xpt_print_path(periph->path);
329501468a5SKenneth D. Merry 				printf("Second device (%s device at scbus%d "
330501468a5SKenneth D. Merry 				       "target %d lun %d) will not be wired\n",
331501468a5SKenneth D. Merry 				       periph_name, pathid, target, lun);
3328b8a9b1dSJustin T. Gibbs 				wired = 0;
3338b8a9b1dSJustin T. Gibbs 			}
3348b8a9b1dSJustin T. Gibbs 			continue;
3358b8a9b1dSJustin T. Gibbs 		}
33675f51904SPeter Wemm 		if (wired)
33775f51904SPeter Wemm 			break;
3388b8a9b1dSJustin T. Gibbs 
3398b8a9b1dSJustin T. Gibbs 		/*
3408b8a9b1dSJustin T. Gibbs 		 * Don't match entries like "da 4" as a wired down
3418b8a9b1dSJustin T. Gibbs 		 * device, but do match entries like "da 4 target 5"
3428b8a9b1dSJustin T. Gibbs 		 * or even "da 4 scbus 1".
3438b8a9b1dSJustin T. Gibbs 		 */
3442398f0cdSPeter Wemm 		i = 0;
3452398f0cdSPeter Wemm 		dname = periph_name;
3462398f0cdSPeter Wemm 		for (;;) {
3472398f0cdSPeter Wemm 			r = resource_find_dev(&i, dname, &dunit, NULL, NULL);
3482398f0cdSPeter Wemm 			if (r != 0)
3492398f0cdSPeter Wemm 				break;
35075f51904SPeter Wemm 			/* if no "target" and no specific scbus, skip */
35175f51904SPeter Wemm 			if (resource_int_value(dname, dunit, "target", &val) &&
35275f51904SPeter Wemm 			    (resource_string_value(dname, dunit, "at",&strval)||
35375f51904SPeter Wemm 			     strcmp(strval, "scbus") == 0))
35475f51904SPeter Wemm 				continue;
35575f51904SPeter Wemm 			if (newunit == dunit)
3568b8a9b1dSJustin T. Gibbs 				break;
3578b8a9b1dSJustin T. Gibbs 		}
3582398f0cdSPeter Wemm 		if (r != 0)
3598b8a9b1dSJustin T. Gibbs 			break;
3608b8a9b1dSJustin T. Gibbs 	}
3618b8a9b1dSJustin T. Gibbs 	splx(s);
3628b8a9b1dSJustin T. Gibbs 	return (newunit);
3638b8a9b1dSJustin T. Gibbs }
3648b8a9b1dSJustin T. Gibbs 
3658b8a9b1dSJustin T. Gibbs static u_int
3668b8a9b1dSJustin T. Gibbs camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
3678b8a9b1dSJustin T. Gibbs 	      target_id_t target, lun_id_t lun)
3688b8a9b1dSJustin T. Gibbs {
3698b8a9b1dSJustin T. Gibbs 	u_int	unit;
370c1b81613SPeter Wemm 	int	wired, i, val, dunit;
3712398f0cdSPeter Wemm 	const char *dname, *strval;
3722398f0cdSPeter Wemm 	char	pathbuf[32], *periph_name;
3738b8a9b1dSJustin T. Gibbs 
37475f51904SPeter Wemm 	periph_name = p_drv->driver_name;
37575f51904SPeter Wemm 	snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
376c1b81613SPeter Wemm 	unit = 0;
3772398f0cdSPeter Wemm 	i = 0;
3782398f0cdSPeter Wemm 	dname = periph_name;
379c1b81613SPeter Wemm 	for (wired = 0; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0;
380c1b81613SPeter Wemm 	     wired = 0) {
38175f51904SPeter Wemm 		if (resource_string_value(dname, dunit, "at", &strval) == 0) {
38275f51904SPeter Wemm 			if (strcmp(strval, pathbuf) != 0)
3838b8a9b1dSJustin T. Gibbs 				continue;
384c1b81613SPeter Wemm 			wired++;
3858b8a9b1dSJustin T. Gibbs 		}
38675f51904SPeter Wemm 		if (resource_int_value(dname, dunit, "target", &val) == 0) {
38775f51904SPeter Wemm 			if (val != target)
3888b8a9b1dSJustin T. Gibbs 				continue;
389c1b81613SPeter Wemm 			wired++;
3908b8a9b1dSJustin T. Gibbs 		}
39175f51904SPeter Wemm 		if (resource_int_value(dname, dunit, "lun", &val) == 0) {
39275f51904SPeter Wemm 			if (val != lun)
3938b8a9b1dSJustin T. Gibbs 				continue;
394c1b81613SPeter Wemm 			wired++;
3958b8a9b1dSJustin T. Gibbs 		}
396c1b81613SPeter Wemm 		if (wired != 0) {
39775f51904SPeter Wemm 			unit = dunit;
3988b8a9b1dSJustin T. Gibbs 			break;
3998b8a9b1dSJustin T. Gibbs 		}
4008b8a9b1dSJustin T. Gibbs 	}
4018b8a9b1dSJustin T. Gibbs 
4028b8a9b1dSJustin T. Gibbs 	/*
4038b8a9b1dSJustin T. Gibbs 	 * Either start from 0 looking for the next unit or from
40475f51904SPeter Wemm 	 * the unit number given in the resource config.  This way,
4058b8a9b1dSJustin T. Gibbs 	 * if we have wildcard matches, we don't return the same
4068b8a9b1dSJustin T. Gibbs 	 * unit number twice.
4078b8a9b1dSJustin T. Gibbs 	 */
408c1b81613SPeter Wemm 	unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun);
4098b8a9b1dSJustin T. Gibbs 
4108b8a9b1dSJustin T. Gibbs 	return (unit);
4118b8a9b1dSJustin T. Gibbs }
4128b8a9b1dSJustin T. Gibbs 
4138b8a9b1dSJustin T. Gibbs void
4148b8a9b1dSJustin T. Gibbs cam_periph_invalidate(struct cam_periph *periph)
4158b8a9b1dSJustin T. Gibbs {
4168b8a9b1dSJustin T. Gibbs 	int s;
4178b8a9b1dSJustin T. Gibbs 
418ee9c90c7SKenneth D. Merry 	s = splsoftcam();
419ee9c90c7SKenneth D. Merry 	/*
420ee9c90c7SKenneth D. Merry 	 * We only call this routine the first time a peripheral is
421ee9c90c7SKenneth D. Merry 	 * invalidated.  The oninvalidate() routine is always called at
422ee9c90c7SKenneth D. Merry 	 * splsoftcam().
423ee9c90c7SKenneth D. Merry 	 */
424ee9c90c7SKenneth D. Merry 	if (((periph->flags & CAM_PERIPH_INVALID) == 0)
425ee9c90c7SKenneth D. Merry 	 && (periph->periph_oninval != NULL))
426ee9c90c7SKenneth D. Merry 		periph->periph_oninval(periph);
427ee9c90c7SKenneth D. Merry 
4288b8a9b1dSJustin T. Gibbs 	periph->flags |= CAM_PERIPH_INVALID;
4298b8a9b1dSJustin T. Gibbs 	periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
4308b8a9b1dSJustin T. Gibbs 
4318b8a9b1dSJustin T. Gibbs 	if (periph->refcount == 0)
4328b8a9b1dSJustin T. Gibbs 		camperiphfree(periph);
4338b8a9b1dSJustin T. Gibbs 	else if (periph->refcount < 0)
4348b8a9b1dSJustin T. Gibbs 		printf("cam_invalidate_periph: refcount < 0!!\n");
4358b8a9b1dSJustin T. Gibbs 	splx(s);
4368b8a9b1dSJustin T. Gibbs }
4378b8a9b1dSJustin T. Gibbs 
4388b8a9b1dSJustin T. Gibbs static void
4398b8a9b1dSJustin T. Gibbs camperiphfree(struct cam_periph *periph)
4408b8a9b1dSJustin T. Gibbs {
4418b8a9b1dSJustin T. Gibbs 	int s;
4428b8a9b1dSJustin T. Gibbs 	struct periph_driver **p_drv;
4438b8a9b1dSJustin T. Gibbs 
4440b7c27b9SPeter Wemm 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
4458b8a9b1dSJustin T. Gibbs 		if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
4468b8a9b1dSJustin T. Gibbs 			break;
4478b8a9b1dSJustin T. Gibbs 	}
4488b8a9b1dSJustin T. Gibbs 
4498b8a9b1dSJustin T. Gibbs 	if (periph->periph_dtor != NULL)
4508b8a9b1dSJustin T. Gibbs 		periph->periph_dtor(periph);
4518b8a9b1dSJustin T. Gibbs 
4528b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
4538b8a9b1dSJustin T. Gibbs 	TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
4548b8a9b1dSJustin T. Gibbs 	(*p_drv)->generation++;
4558b8a9b1dSJustin T. Gibbs 	splx(s);
4568b8a9b1dSJustin T. Gibbs 
4578b8a9b1dSJustin T. Gibbs 	xpt_remove_periph(periph);
4588b8a9b1dSJustin T. Gibbs 
4598b8a9b1dSJustin T. Gibbs 	if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
4608b8a9b1dSJustin T. Gibbs 		union ccb ccb;
4618b8a9b1dSJustin T. Gibbs 		void *arg;
4628b8a9b1dSJustin T. Gibbs 
4638b8a9b1dSJustin T. Gibbs 		switch (periph->deferred_ac) {
4648b8a9b1dSJustin T. Gibbs 		case AC_FOUND_DEVICE:
4658b8a9b1dSJustin T. Gibbs 			ccb.ccb_h.func_code = XPT_GDEV_TYPE;
4668b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
4678b8a9b1dSJustin T. Gibbs 			xpt_action(&ccb);
4688b8a9b1dSJustin T. Gibbs 			arg = &ccb;
4698b8a9b1dSJustin T. Gibbs 			break;
4708b8a9b1dSJustin T. Gibbs 		case AC_PATH_REGISTERED:
4718b8a9b1dSJustin T. Gibbs 			ccb.ccb_h.func_code = XPT_PATH_INQ;
4728b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/ 1);
4738b8a9b1dSJustin T. Gibbs 			xpt_action(&ccb);
4748b8a9b1dSJustin T. Gibbs 			arg = &ccb;
4758b8a9b1dSJustin T. Gibbs 			break;
4768b8a9b1dSJustin T. Gibbs 		default:
4778b8a9b1dSJustin T. Gibbs 			arg = NULL;
4788b8a9b1dSJustin T. Gibbs 			break;
4798b8a9b1dSJustin T. Gibbs 		}
4808b8a9b1dSJustin T. Gibbs 		periph->deferred_callback(NULL, periph->deferred_ac,
4818b8a9b1dSJustin T. Gibbs 					  periph->path, arg);
4828b8a9b1dSJustin T. Gibbs 	}
4838b8a9b1dSJustin T. Gibbs 	xpt_free_path(periph->path);
4848b8a9b1dSJustin T. Gibbs 	free(periph, M_DEVBUF);
4858b8a9b1dSJustin T. Gibbs }
4868b8a9b1dSJustin T. Gibbs 
4878b8a9b1dSJustin T. Gibbs /*
4888b8a9b1dSJustin T. Gibbs  * Wait interruptibly for an exclusive lock.
4898b8a9b1dSJustin T. Gibbs  */
4908b8a9b1dSJustin T. Gibbs int
4918b8a9b1dSJustin T. Gibbs cam_periph_lock(struct cam_periph *periph, int priority)
4928b8a9b1dSJustin T. Gibbs {
4938b8a9b1dSJustin T. Gibbs 	int error;
4948b8a9b1dSJustin T. Gibbs 
4953393f8daSKenneth D. Merry 	/*
4963393f8daSKenneth D. Merry 	 * Increment the reference count on the peripheral
4973393f8daSKenneth D. Merry 	 * while we wait for our lock attempt to succeed
498c7ec4390SMatt Jacob 	 * to ensure the peripheral doesn't disappear out
499c7ec4390SMatt Jacob 	 * from under us while we sleep.
5003393f8daSKenneth D. Merry 	 */
5018b8a9b1dSJustin T. Gibbs 	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
5028b8a9b1dSJustin T. Gibbs 		return(ENXIO);
5038b8a9b1dSJustin T. Gibbs 
5043393f8daSKenneth D. Merry 	while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
5053393f8daSKenneth D. Merry 		periph->flags |= CAM_PERIPH_LOCK_WANTED;
5063393f8daSKenneth D. Merry 		if ((error = tsleep(periph, priority, "caplck", 0)) != 0) {
5073393f8daSKenneth D. Merry 			cam_periph_release(periph);
5083393f8daSKenneth D. Merry 			return error;
5093393f8daSKenneth D. Merry 		}
5103393f8daSKenneth D. Merry 	}
5113393f8daSKenneth D. Merry 
5128b8a9b1dSJustin T. Gibbs 	periph->flags |= CAM_PERIPH_LOCKED;
5138b8a9b1dSJustin T. Gibbs 	return 0;
5148b8a9b1dSJustin T. Gibbs }
5158b8a9b1dSJustin T. Gibbs 
5168b8a9b1dSJustin T. Gibbs /*
5178b8a9b1dSJustin T. Gibbs  * Unlock and wake up any waiters.
5188b8a9b1dSJustin T. Gibbs  */
5198b8a9b1dSJustin T. Gibbs void
5208b8a9b1dSJustin T. Gibbs cam_periph_unlock(struct cam_periph *periph)
5218b8a9b1dSJustin T. Gibbs {
5228b8a9b1dSJustin T. Gibbs 	periph->flags &= ~CAM_PERIPH_LOCKED;
5238b8a9b1dSJustin T. Gibbs 	if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
5248b8a9b1dSJustin T. Gibbs 		periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
5258b8a9b1dSJustin T. Gibbs 		wakeup(periph);
5268b8a9b1dSJustin T. Gibbs 	}
5278b8a9b1dSJustin T. Gibbs 
5288b8a9b1dSJustin T. Gibbs 	cam_periph_release(periph);
5298b8a9b1dSJustin T. Gibbs }
5308b8a9b1dSJustin T. Gibbs 
5318b8a9b1dSJustin T. Gibbs /*
5328b8a9b1dSJustin T. Gibbs  * Map user virtual pointers into kernel virtual address space, so we can
5338b8a9b1dSJustin T. Gibbs  * access the memory.  This won't work on physical pointers, for now it's
5348b8a9b1dSJustin T. Gibbs  * up to the caller to check for that.  (XXX KDM -- should we do that here
5358b8a9b1dSJustin T. Gibbs  * instead?)  This also only works for up to MAXPHYS memory.  Since we use
5368b8a9b1dSJustin T. Gibbs  * buffers to map stuff in and out, we're limited to the buffer size.
5378b8a9b1dSJustin T. Gibbs  */
5388b8a9b1dSJustin T. Gibbs int
5398b8a9b1dSJustin T. Gibbs cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
5408b8a9b1dSJustin T. Gibbs {
5412d5c7e45SMatthew Dillon 	int numbufs, i, j;
54279d49a06SKenneth D. Merry 	int flags[CAM_PERIPH_MAXMAPS];
5438b8a9b1dSJustin T. Gibbs 	u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
5448b8a9b1dSJustin T. Gibbs 	u_int32_t lengths[CAM_PERIPH_MAXMAPS];
5458b8a9b1dSJustin T. Gibbs 	u_int32_t dirs[CAM_PERIPH_MAXMAPS];
5468b8a9b1dSJustin T. Gibbs 
5478b8a9b1dSJustin T. Gibbs 	switch(ccb->ccb_h.func_code) {
5488b8a9b1dSJustin T. Gibbs 	case XPT_DEV_MATCH:
5498b8a9b1dSJustin T. Gibbs 		if (ccb->cdm.match_buf_len == 0) {
5508b8a9b1dSJustin T. Gibbs 			printf("cam_periph_mapmem: invalid match buffer "
5518b8a9b1dSJustin T. Gibbs 			       "length 0\n");
5528b8a9b1dSJustin T. Gibbs 			return(EINVAL);
5538b8a9b1dSJustin T. Gibbs 		}
5548b8a9b1dSJustin T. Gibbs 		if (ccb->cdm.pattern_buf_len > 0) {
5558b8a9b1dSJustin T. Gibbs 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
5568b8a9b1dSJustin T. Gibbs 			lengths[0] = ccb->cdm.pattern_buf_len;
5578b8a9b1dSJustin T. Gibbs 			dirs[0] = CAM_DIR_OUT;
5588b8a9b1dSJustin T. Gibbs 			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
5598b8a9b1dSJustin T. Gibbs 			lengths[1] = ccb->cdm.match_buf_len;
5608b8a9b1dSJustin T. Gibbs 			dirs[1] = CAM_DIR_IN;
5618b8a9b1dSJustin T. Gibbs 			numbufs = 2;
5628b8a9b1dSJustin T. Gibbs 		} else {
5638b8a9b1dSJustin T. Gibbs 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
5648b8a9b1dSJustin T. Gibbs 			lengths[0] = ccb->cdm.match_buf_len;
5658b8a9b1dSJustin T. Gibbs 			dirs[0] = CAM_DIR_IN;
5668b8a9b1dSJustin T. Gibbs 			numbufs = 1;
5678b8a9b1dSJustin T. Gibbs 		}
5688b8a9b1dSJustin T. Gibbs 		break;
5698b8a9b1dSJustin T. Gibbs 	case XPT_SCSI_IO:
57087cfaf0eSJustin T. Gibbs 	case XPT_CONT_TARGET_IO:
5718b8a9b1dSJustin T. Gibbs 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
5728b8a9b1dSJustin T. Gibbs 			return(0);
5738b8a9b1dSJustin T. Gibbs 
5748b8a9b1dSJustin T. Gibbs 		data_ptrs[0] = &ccb->csio.data_ptr;
57579d49a06SKenneth D. Merry 		lengths[0] = ccb->csio.dxfer_len;
5768b8a9b1dSJustin T. Gibbs 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
5778b8a9b1dSJustin T. Gibbs 		numbufs = 1;
5788b8a9b1dSJustin T. Gibbs 		break;
5798b8a9b1dSJustin T. Gibbs 	default:
5808b8a9b1dSJustin T. Gibbs 		return(EINVAL);
5818b8a9b1dSJustin T. Gibbs 		break; /* NOTREACHED */
5828b8a9b1dSJustin T. Gibbs 	}
5838b8a9b1dSJustin T. Gibbs 
5848b8a9b1dSJustin T. Gibbs 	/*
58579d49a06SKenneth D. Merry 	 * Check the transfer length and permissions first, so we don't
58679d49a06SKenneth D. Merry 	 * have to unmap any previously mapped buffers.
5878b8a9b1dSJustin T. Gibbs 	 */
5888b8a9b1dSJustin T. Gibbs 	for (i = 0; i < numbufs; i++) {
58979d49a06SKenneth D. Merry 
59079d49a06SKenneth D. Merry 		flags[i] = 0;
59179d49a06SKenneth D. Merry 
59279d49a06SKenneth D. Merry 		/*
59379d49a06SKenneth D. Merry 		 * The userland data pointer passed in may not be page
59479d49a06SKenneth D. Merry 		 * aligned.  vmapbuf() truncates the address to a page
59579d49a06SKenneth D. Merry 		 * boundary, so if the address isn't page aligned, we'll
59679d49a06SKenneth D. Merry 		 * need enough space for the given transfer length, plus
59779d49a06SKenneth D. Merry 		 * whatever extra space is necessary to make it to the page
59879d49a06SKenneth D. Merry 		 * boundary.
59979d49a06SKenneth D. Merry 		 */
60079d49a06SKenneth D. Merry 		if ((lengths[i] +
601ff1fe75fSKenneth D. Merry 		    (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > DFLTPHYS){
6026d7b539aSPeter Wemm 			printf("cam_periph_mapmem: attempt to map %lu bytes, "
603ff1fe75fSKenneth D. Merry 			       "which is greater than DFLTPHYS(%d)\n",
6046d7b539aSPeter Wemm 			       (long)(lengths[i] +
6056d7b539aSPeter Wemm 			       (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
606ff1fe75fSKenneth D. Merry 			       DFLTPHYS);
60779d49a06SKenneth D. Merry 			return(E2BIG);
60879d49a06SKenneth D. Merry 		}
6098b8a9b1dSJustin T. Gibbs 
610edd24ab7SKenneth D. Merry 		if (dirs[i] & CAM_DIR_OUT) {
61121144e3bSPoul-Henning Kamp 			flags[i] = BIO_WRITE;
6128b8a9b1dSJustin T. Gibbs 		}
6138b8a9b1dSJustin T. Gibbs 
614edd24ab7SKenneth D. Merry 		if (dirs[i] & CAM_DIR_IN) {
61521144e3bSPoul-Henning Kamp 			flags[i] = BIO_READ;
6168b8a9b1dSJustin T. Gibbs 		}
6178b8a9b1dSJustin T. Gibbs 
61879d49a06SKenneth D. Merry 	}
61979d49a06SKenneth D. Merry 
62079d49a06SKenneth D. Merry 	/* this keeps the current process from getting swapped */
62179d49a06SKenneth D. Merry 	/*
62279d49a06SKenneth D. Merry 	 * XXX KDM should I use P_NOSWAP instead?
62379d49a06SKenneth D. Merry 	 */
6240cbbb7bfSPeter Wemm 	PHOLD(curproc);
62579d49a06SKenneth D. Merry 
62679d49a06SKenneth D. Merry 	for (i = 0; i < numbufs; i++) {
6278b8a9b1dSJustin T. Gibbs 		/*
6288b8a9b1dSJustin T. Gibbs 		 * Get the buffer.
6298b8a9b1dSJustin T. Gibbs 		 */
6301c7c3c6aSMatthew Dillon 		mapinfo->bp[i] = getpbuf(NULL);
6318b8a9b1dSJustin T. Gibbs 
6328b8a9b1dSJustin T. Gibbs 		/* save the buffer's data address */
6338b8a9b1dSJustin T. Gibbs 		mapinfo->bp[i]->b_saveaddr = mapinfo->bp[i]->b_data;
6348b8a9b1dSJustin T. Gibbs 
6358b8a9b1dSJustin T. Gibbs 		/* put our pointer in the data slot */
6368b8a9b1dSJustin T. Gibbs 		mapinfo->bp[i]->b_data = *data_ptrs[i];
6378b8a9b1dSJustin T. Gibbs 
638ff1fe75fSKenneth D. Merry 		/* set the transfer length, we know it's < DFLTPHYS */
6398b8a9b1dSJustin T. Gibbs 		mapinfo->bp[i]->b_bufsize = lengths[i];
6408b8a9b1dSJustin T. Gibbs 
64121144e3bSPoul-Henning Kamp 		/* set the direction */
64221144e3bSPoul-Henning Kamp 		mapinfo->bp[i]->b_iocmd = flags[i];
6438b8a9b1dSJustin T. Gibbs 
6442d5c7e45SMatthew Dillon 		/*
6452d5c7e45SMatthew Dillon 		 * Map the buffer into kernel memory.
6462d5c7e45SMatthew Dillon 		 *
6472d5c7e45SMatthew Dillon 		 * Note that useracc() alone is not a  sufficient test.
6482d5c7e45SMatthew Dillon 		 * vmapbuf() can still fail due to a smaller file mapped
6492d5c7e45SMatthew Dillon 		 * into a larger area of VM, or if userland races against
6502d5c7e45SMatthew Dillon 		 * vmapbuf() after the useracc() check.
6512d5c7e45SMatthew Dillon 		 */
6522d5c7e45SMatthew Dillon 		if (vmapbuf(mapinfo->bp[i]) < 0) {
6532d5c7e45SMatthew Dillon 			for (j = 0; j < i; ++j) {
6542d5c7e45SMatthew Dillon 				*data_ptrs[j] = mapinfo->bp[j]->b_saveaddr;
6558373f36dSAlan Cox 				vunmapbuf(mapinfo->bp[j]);
6562d5c7e45SMatthew Dillon 				relpbuf(mapinfo->bp[j], NULL);
6572d5c7e45SMatthew Dillon 			}
6588373f36dSAlan Cox 			relpbuf(mapinfo->bp[i], NULL);
6592d5c7e45SMatthew Dillon 			PRELE(curproc);
6602d5c7e45SMatthew Dillon 			return(EACCES);
6612d5c7e45SMatthew Dillon 		}
6628b8a9b1dSJustin T. Gibbs 
6638b8a9b1dSJustin T. Gibbs 		/* set our pointer to the new mapped area */
6648b8a9b1dSJustin T. Gibbs 		*data_ptrs[i] = mapinfo->bp[i]->b_data;
6658b8a9b1dSJustin T. Gibbs 
6668b8a9b1dSJustin T. Gibbs 		mapinfo->num_bufs_used++;
6678b8a9b1dSJustin T. Gibbs 	}
6688b8a9b1dSJustin T. Gibbs 
6698b8a9b1dSJustin T. Gibbs 	return(0);
6708b8a9b1dSJustin T. Gibbs }
6718b8a9b1dSJustin T. Gibbs 
6728b8a9b1dSJustin T. Gibbs /*
6738b8a9b1dSJustin T. Gibbs  * Unmap memory segments mapped into kernel virtual address space by
6748b8a9b1dSJustin T. Gibbs  * cam_periph_mapmem().
6758b8a9b1dSJustin T. Gibbs  */
6768b8a9b1dSJustin T. Gibbs void
6778b8a9b1dSJustin T. Gibbs cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
6788b8a9b1dSJustin T. Gibbs {
6798b8a9b1dSJustin T. Gibbs 	int numbufs, i;
6808b8a9b1dSJustin T. Gibbs 	u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
6818b8a9b1dSJustin T. Gibbs 
6828b8a9b1dSJustin T. Gibbs 	if (mapinfo->num_bufs_used <= 0) {
6838b8a9b1dSJustin T. Gibbs 		/* allow ourselves to be swapped once again */
6840cbbb7bfSPeter Wemm 		PRELE(curproc);
6858b8a9b1dSJustin T. Gibbs 		return;
6868b8a9b1dSJustin T. Gibbs 	}
6878b8a9b1dSJustin T. Gibbs 
6888b8a9b1dSJustin T. Gibbs 	switch (ccb->ccb_h.func_code) {
6898b8a9b1dSJustin T. Gibbs 	case XPT_DEV_MATCH:
6908b8a9b1dSJustin T. Gibbs 		numbufs = min(mapinfo->num_bufs_used, 2);
6918b8a9b1dSJustin T. Gibbs 
6928b8a9b1dSJustin T. Gibbs 		if (numbufs == 1) {
6938b8a9b1dSJustin T. Gibbs 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
6948b8a9b1dSJustin T. Gibbs 		} else {
6958b8a9b1dSJustin T. Gibbs 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
6968b8a9b1dSJustin T. Gibbs 			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
6978b8a9b1dSJustin T. Gibbs 		}
6988b8a9b1dSJustin T. Gibbs 		break;
6998b8a9b1dSJustin T. Gibbs 	case XPT_SCSI_IO:
7009911ecf9SJustin T. Gibbs 	case XPT_CONT_TARGET_IO:
7018b8a9b1dSJustin T. Gibbs 		data_ptrs[0] = &ccb->csio.data_ptr;
7028b8a9b1dSJustin T. Gibbs 		numbufs = min(mapinfo->num_bufs_used, 1);
7038b8a9b1dSJustin T. Gibbs 		break;
7048b8a9b1dSJustin T. Gibbs 	default:
7058b8a9b1dSJustin T. Gibbs 		/* allow ourselves to be swapped once again */
7060cbbb7bfSPeter Wemm 		PRELE(curproc);
7078b8a9b1dSJustin T. Gibbs 		return;
7088b8a9b1dSJustin T. Gibbs 		break; /* NOTREACHED */
7098b8a9b1dSJustin T. Gibbs 	}
7108b8a9b1dSJustin T. Gibbs 
7118b8a9b1dSJustin T. Gibbs 	for (i = 0; i < numbufs; i++) {
7128b8a9b1dSJustin T. Gibbs 		/* Set the user's pointer back to the original value */
7138b8a9b1dSJustin T. Gibbs 		*data_ptrs[i] = mapinfo->bp[i]->b_saveaddr;
7148b8a9b1dSJustin T. Gibbs 
7158b8a9b1dSJustin T. Gibbs 		/* unmap the buffer */
7168b8a9b1dSJustin T. Gibbs 		vunmapbuf(mapinfo->bp[i]);
7178b8a9b1dSJustin T. Gibbs 
7188b8a9b1dSJustin T. Gibbs 		/* release the buffer */
7191c7c3c6aSMatthew Dillon 		relpbuf(mapinfo->bp[i], NULL);
7208b8a9b1dSJustin T. Gibbs 	}
7218b8a9b1dSJustin T. Gibbs 
7228b8a9b1dSJustin T. Gibbs 	/* allow ourselves to be swapped once again */
7230cbbb7bfSPeter Wemm 	PRELE(curproc);
7248b8a9b1dSJustin T. Gibbs }
7258b8a9b1dSJustin T. Gibbs 
7268b8a9b1dSJustin T. Gibbs union ccb *
7278b8a9b1dSJustin T. Gibbs cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
7288b8a9b1dSJustin T. Gibbs {
7298b8a9b1dSJustin T. Gibbs 	struct ccb_hdr *ccb_h;
7308b8a9b1dSJustin T. Gibbs 	int s;
7318b8a9b1dSJustin T. Gibbs 
7328b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n"));
7338b8a9b1dSJustin T. Gibbs 
7348b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
7358b8a9b1dSJustin T. Gibbs 
736fc2ffbe6SPoul-Henning Kamp 	while (SLIST_FIRST(&periph->ccb_list) == NULL) {
7378b8a9b1dSJustin T. Gibbs 		if (periph->immediate_priority > priority)
7388b8a9b1dSJustin T. Gibbs 			periph->immediate_priority = priority;
7398b8a9b1dSJustin T. Gibbs 		xpt_schedule(periph, priority);
740fc2ffbe6SPoul-Henning Kamp 		if ((SLIST_FIRST(&periph->ccb_list) != NULL)
741fc2ffbe6SPoul-Henning Kamp 		 && (SLIST_FIRST(&periph->ccb_list)->pinfo.priority == priority))
7428b8a9b1dSJustin T. Gibbs 			break;
7438b8a9b1dSJustin T. Gibbs 		tsleep(&periph->ccb_list, PRIBIO, "cgticb", 0);
7448b8a9b1dSJustin T. Gibbs 	}
7458b8a9b1dSJustin T. Gibbs 
746fc2ffbe6SPoul-Henning Kamp 	ccb_h = SLIST_FIRST(&periph->ccb_list);
7478b8a9b1dSJustin T. Gibbs 	SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
7488b8a9b1dSJustin T. Gibbs 	splx(s);
7498b8a9b1dSJustin T. Gibbs 	return ((union ccb *)ccb_h);
7508b8a9b1dSJustin T. Gibbs }
7518b8a9b1dSJustin T. Gibbs 
7528b8a9b1dSJustin T. Gibbs void
7538b8a9b1dSJustin T. Gibbs cam_periph_ccbwait(union ccb *ccb)
7548b8a9b1dSJustin T. Gibbs {
7558b8a9b1dSJustin T. Gibbs 	int s;
7568b8a9b1dSJustin T. Gibbs 
7578b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
7588b8a9b1dSJustin T. Gibbs 	if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
7598b8a9b1dSJustin T. Gibbs 	 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
7608b8a9b1dSJustin T. Gibbs 		tsleep(&ccb->ccb_h.cbfcnp, PRIBIO, "cbwait", 0);
7618b8a9b1dSJustin T. Gibbs 
7628b8a9b1dSJustin T. Gibbs 	splx(s);
7638b8a9b1dSJustin T. Gibbs }
7648b8a9b1dSJustin T. Gibbs 
7658b8a9b1dSJustin T. Gibbs int
7668b8a9b1dSJustin T. Gibbs cam_periph_ioctl(struct cam_periph *periph, int cmd, caddr_t addr,
7678b8a9b1dSJustin T. Gibbs 		 int (*error_routine)(union ccb *ccb,
7688b8a9b1dSJustin T. Gibbs 				      cam_flags camflags,
7698b8a9b1dSJustin T. Gibbs 				      u_int32_t sense_flags))
7708b8a9b1dSJustin T. Gibbs {
7718b8a9b1dSJustin T. Gibbs 	union ccb 	     *ccb;
7728b8a9b1dSJustin T. Gibbs 	int 		     error;
7738b8a9b1dSJustin T. Gibbs 	int		     found;
7748b8a9b1dSJustin T. Gibbs 
7758b8a9b1dSJustin T. Gibbs 	error = found = 0;
7768b8a9b1dSJustin T. Gibbs 
7778b8a9b1dSJustin T. Gibbs 	switch(cmd){
7788b8a9b1dSJustin T. Gibbs 	case CAMGETPASSTHRU:
7798b8a9b1dSJustin T. Gibbs 		ccb = cam_periph_getccb(periph, /* priority */ 1);
7808b8a9b1dSJustin T. Gibbs 		xpt_setup_ccb(&ccb->ccb_h,
7818b8a9b1dSJustin T. Gibbs 			      ccb->ccb_h.path,
7828b8a9b1dSJustin T. Gibbs 			      /*priority*/1);
7838b8a9b1dSJustin T. Gibbs 		ccb->ccb_h.func_code = XPT_GDEVLIST;
7848b8a9b1dSJustin T. Gibbs 
7858b8a9b1dSJustin T. Gibbs 		/*
7868b8a9b1dSJustin T. Gibbs 		 * Basically, the point of this is that we go through
7878b8a9b1dSJustin T. Gibbs 		 * getting the list of devices, until we find a passthrough
7888b8a9b1dSJustin T. Gibbs 		 * device.  In the current version of the CAM code, the
7898b8a9b1dSJustin T. Gibbs 		 * only way to determine what type of device we're dealing
7908b8a9b1dSJustin T. Gibbs 		 * with is by its name.
7918b8a9b1dSJustin T. Gibbs 		 */
7928b8a9b1dSJustin T. Gibbs 		while (found == 0) {
7938b8a9b1dSJustin T. Gibbs 			ccb->cgdl.index = 0;
7948b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
7958b8a9b1dSJustin T. Gibbs 			while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
7968b8a9b1dSJustin T. Gibbs 
7978b8a9b1dSJustin T. Gibbs 				/* we want the next device in the list */
7988b8a9b1dSJustin T. Gibbs 				xpt_action(ccb);
7998b8a9b1dSJustin T. Gibbs 				if (strncmp(ccb->cgdl.periph_name,
8008b8a9b1dSJustin T. Gibbs 				    "pass", 4) == 0){
8018b8a9b1dSJustin T. Gibbs 					found = 1;
8028b8a9b1dSJustin T. Gibbs 					break;
8038b8a9b1dSJustin T. Gibbs 				}
8048b8a9b1dSJustin T. Gibbs 			}
8058b8a9b1dSJustin T. Gibbs 			if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
8068b8a9b1dSJustin T. Gibbs 			    (found == 0)) {
8078b8a9b1dSJustin T. Gibbs 				ccb->cgdl.periph_name[0] = '\0';
8088b8a9b1dSJustin T. Gibbs 				ccb->cgdl.unit_number = 0;
8098b8a9b1dSJustin T. Gibbs 				break;
8108b8a9b1dSJustin T. Gibbs 			}
8118b8a9b1dSJustin T. Gibbs 		}
8128b8a9b1dSJustin T. Gibbs 
8138b8a9b1dSJustin T. Gibbs 		/* copy the result back out */
8148b8a9b1dSJustin T. Gibbs 		bcopy(ccb, addr, sizeof(union ccb));
8158b8a9b1dSJustin T. Gibbs 
8168b8a9b1dSJustin T. Gibbs 		/* and release the ccb */
8178b8a9b1dSJustin T. Gibbs 		xpt_release_ccb(ccb);
8188b8a9b1dSJustin T. Gibbs 
8198b8a9b1dSJustin T. Gibbs 		break;
8208b8a9b1dSJustin T. Gibbs 	default:
8218b8a9b1dSJustin T. Gibbs 		error = ENOTTY;
8228b8a9b1dSJustin T. Gibbs 		break;
8238b8a9b1dSJustin T. Gibbs 	}
8248b8a9b1dSJustin T. Gibbs 	return(error);
8258b8a9b1dSJustin T. Gibbs }
8268b8a9b1dSJustin T. Gibbs 
8278b8a9b1dSJustin T. Gibbs int
8288b8a9b1dSJustin T. Gibbs cam_periph_runccb(union ccb *ccb,
8298b8a9b1dSJustin T. Gibbs 		  int (*error_routine)(union ccb *ccb,
8308b8a9b1dSJustin T. Gibbs 				       cam_flags camflags,
8318b8a9b1dSJustin T. Gibbs 				       u_int32_t sense_flags),
8328b8a9b1dSJustin T. Gibbs 		  cam_flags camflags, u_int32_t sense_flags,
8338b8a9b1dSJustin T. Gibbs 		  struct devstat *ds)
8348b8a9b1dSJustin T. Gibbs {
8358b8a9b1dSJustin T. Gibbs 	int error;
8368b8a9b1dSJustin T. Gibbs 
8378b8a9b1dSJustin T. Gibbs 	error = 0;
8388b8a9b1dSJustin T. Gibbs 
8398b8a9b1dSJustin T. Gibbs 	/*
8408b8a9b1dSJustin T. Gibbs 	 * If the user has supplied a stats structure, and if we understand
8418b8a9b1dSJustin T. Gibbs 	 * this particular type of ccb, record the transaction start.
8428b8a9b1dSJustin T. Gibbs 	 */
8438b8a9b1dSJustin T. Gibbs 	if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
8447194d335SPoul-Henning Kamp 		devstat_start_transaction(ds, NULL);
8458b8a9b1dSJustin T. Gibbs 
8468b8a9b1dSJustin T. Gibbs 	xpt_action(ccb);
8478b8a9b1dSJustin T. Gibbs 
8488b8a9b1dSJustin T. Gibbs 	do {
8498b8a9b1dSJustin T. Gibbs 		cam_periph_ccbwait(ccb);
8508b8a9b1dSJustin T. Gibbs 		if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
8518b8a9b1dSJustin T. Gibbs 			error = 0;
8528b8a9b1dSJustin T. Gibbs 		else if (error_routine != NULL)
8538b8a9b1dSJustin T. Gibbs 			error = (*error_routine)(ccb, camflags, sense_flags);
8548b8a9b1dSJustin T. Gibbs 		else
8558b8a9b1dSJustin T. Gibbs 			error = 0;
8568b8a9b1dSJustin T. Gibbs 
8578b8a9b1dSJustin T. Gibbs 	} while (error == ERESTART);
8588b8a9b1dSJustin T. Gibbs 
8598b8a9b1dSJustin T. Gibbs 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
8608b8a9b1dSJustin T. Gibbs 		cam_release_devq(ccb->ccb_h.path,
8618b8a9b1dSJustin T. Gibbs 				 /* relsim_flags */0,
8628b8a9b1dSJustin T. Gibbs 				 /* openings */0,
8638b8a9b1dSJustin T. Gibbs 				 /* timeout */0,
8648b8a9b1dSJustin T. Gibbs 				 /* getcount_only */ FALSE);
8658b8a9b1dSJustin T. Gibbs 
8668b8a9b1dSJustin T. Gibbs 	if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO))
8678b8a9b1dSJustin T. Gibbs 		devstat_end_transaction(ds,
8688b8a9b1dSJustin T. Gibbs 					ccb->csio.dxfer_len,
8698b8a9b1dSJustin T. Gibbs 					ccb->csio.tag_action & 0xf,
8708b8a9b1dSJustin T. Gibbs 					((ccb->ccb_h.flags & CAM_DIR_MASK) ==
8718b8a9b1dSJustin T. Gibbs 					CAM_DIR_NONE) ?  DEVSTAT_NO_DATA :
8728b8a9b1dSJustin T. Gibbs 					(ccb->ccb_h.flags & CAM_DIR_OUT) ?
8738b8a9b1dSJustin T. Gibbs 					DEVSTAT_WRITE :
8747194d335SPoul-Henning Kamp 					DEVSTAT_READ, NULL, NULL);
8758b8a9b1dSJustin T. Gibbs 
8768b8a9b1dSJustin T. Gibbs 	return(error);
8778b8a9b1dSJustin T. Gibbs }
8788b8a9b1dSJustin T. Gibbs 
87987cfaf0eSJustin T. Gibbs void
88087cfaf0eSJustin T. Gibbs cam_freeze_devq(struct cam_path *path)
88187cfaf0eSJustin T. Gibbs {
88287cfaf0eSJustin T. Gibbs 	struct ccb_hdr ccb_h;
88387cfaf0eSJustin T. Gibbs 
88487cfaf0eSJustin T. Gibbs 	xpt_setup_ccb(&ccb_h, path, /*priority*/1);
88587cfaf0eSJustin T. Gibbs 	ccb_h.func_code = XPT_NOOP;
88687cfaf0eSJustin T. Gibbs 	ccb_h.flags = CAM_DEV_QFREEZE;
88787cfaf0eSJustin T. Gibbs 	xpt_action((union ccb *)&ccb_h);
88887cfaf0eSJustin T. Gibbs }
88987cfaf0eSJustin T. Gibbs 
8908b8a9b1dSJustin T. Gibbs u_int32_t
8918b8a9b1dSJustin T. Gibbs cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
8928b8a9b1dSJustin T. Gibbs 		 u_int32_t openings, u_int32_t timeout,
8938b8a9b1dSJustin T. Gibbs 		 int getcount_only)
8948b8a9b1dSJustin T. Gibbs {
8958b8a9b1dSJustin T. Gibbs 	struct ccb_relsim crs;
8968b8a9b1dSJustin T. Gibbs 
8978b8a9b1dSJustin T. Gibbs 	xpt_setup_ccb(&crs.ccb_h, path,
8988b8a9b1dSJustin T. Gibbs 		      /*priority*/1);
8998b8a9b1dSJustin T. Gibbs 	crs.ccb_h.func_code = XPT_REL_SIMQ;
9008b8a9b1dSJustin T. Gibbs 	crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
9018b8a9b1dSJustin T. Gibbs 	crs.release_flags = relsim_flags;
9028b8a9b1dSJustin T. Gibbs 	crs.openings = openings;
9038b8a9b1dSJustin T. Gibbs 	crs.release_timeout = timeout;
9048b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&crs);
9058b8a9b1dSJustin T. Gibbs 	return (crs.qfrozen_cnt);
9068b8a9b1dSJustin T. Gibbs }
9078b8a9b1dSJustin T. Gibbs 
9088b8a9b1dSJustin T. Gibbs #define saved_ccb_ptr ppriv_ptr0
9098b8a9b1dSJustin T. Gibbs static void
9108b8a9b1dSJustin T. Gibbs camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
9118b8a9b1dSJustin T. Gibbs {
9123393f8daSKenneth D. Merry 	union ccb      *saved_ccb;
9138b8a9b1dSJustin T. Gibbs 	cam_status	status;
9148b8a9b1dSJustin T. Gibbs 	int		frozen;
9158b8a9b1dSJustin T. Gibbs 	int		sense;
9168b8a9b1dSJustin T. Gibbs 	struct scsi_start_stop_unit *scsi_cmd;
9178b8a9b1dSJustin T. Gibbs 	u_int32_t	relsim_flags, timeout;
9188b8a9b1dSJustin T. Gibbs 	u_int32_t	qfrozen_cnt;
9193393f8daSKenneth D. Merry 	int		xpt_done_ccb;
9208b8a9b1dSJustin T. Gibbs 
9213393f8daSKenneth D. Merry 	xpt_done_ccb = FALSE;
9228b8a9b1dSJustin T. Gibbs 	status = done_ccb->ccb_h.status;
9238b8a9b1dSJustin T. Gibbs 	frozen = (status & CAM_DEV_QFRZN) != 0;
9248b8a9b1dSJustin T. Gibbs 	sense  = (status & CAM_AUTOSNS_VALID) != 0;
9258b8a9b1dSJustin T. Gibbs 	status &= CAM_STATUS_MASK;
9268b8a9b1dSJustin T. Gibbs 
9278b8a9b1dSJustin T. Gibbs 	timeout = 0;
9288b8a9b1dSJustin T. Gibbs 	relsim_flags = 0;
9293393f8daSKenneth D. Merry 	saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
9308b8a9b1dSJustin T. Gibbs 
9318b8a9b1dSJustin T. Gibbs 	/*
9328b8a9b1dSJustin T. Gibbs 	 * Unfreeze the queue once if it is already frozen..
9338b8a9b1dSJustin T. Gibbs 	 */
9348b8a9b1dSJustin T. Gibbs 	if (frozen != 0) {
9358b8a9b1dSJustin T. Gibbs 		qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
9368b8a9b1dSJustin T. Gibbs 					      /*relsim_flags*/0,
9378b8a9b1dSJustin T. Gibbs 					      /*openings*/0,
9388b8a9b1dSJustin T. Gibbs 					      /*timeout*/0,
9398b8a9b1dSJustin T. Gibbs 					      /*getcount_only*/0);
9408b8a9b1dSJustin T. Gibbs 	}
9418b8a9b1dSJustin T. Gibbs 
9428b8a9b1dSJustin T. Gibbs 	switch (status) {
9438b8a9b1dSJustin T. Gibbs 	case CAM_REQ_CMP:
9443393f8daSKenneth D. Merry 	{
9458b8a9b1dSJustin T. Gibbs 		/*
9468b8a9b1dSJustin T. Gibbs 		 * If we have successfully taken a device from the not
9473393f8daSKenneth D. Merry 		 * ready to ready state, re-scan the device and re-get
9483393f8daSKenneth D. Merry 		 * the inquiry information.  Many devices (mostly disks)
9493393f8daSKenneth D. Merry 		 * don't properly report their inquiry information unless
9503393f8daSKenneth D. Merry 		 * they are spun up.
9513393f8daSKenneth D. Merry 		 *
9523393f8daSKenneth D. Merry 		 * If we manually retrieved sense into a CCB and got
9533393f8daSKenneth D. Merry 		 * something other than "NO SENSE" send the updated CCB
9543393f8daSKenneth D. Merry 		 * back to the client via xpt_done() to be processed via
9553393f8daSKenneth D. Merry 		 * the error recovery code again.
9568b8a9b1dSJustin T. Gibbs 		 */
9578b8a9b1dSJustin T. Gibbs 		if (done_ccb->ccb_h.func_code == XPT_SCSI_IO) {
9588b8a9b1dSJustin T. Gibbs 			scsi_cmd = (struct scsi_start_stop_unit *)
9598b8a9b1dSJustin T. Gibbs 					&done_ccb->csio.cdb_io.cdb_bytes;
9608b8a9b1dSJustin T. Gibbs 
9618b8a9b1dSJustin T. Gibbs 		 	if (scsi_cmd->opcode == START_STOP_UNIT)
9628b8a9b1dSJustin T. Gibbs 				xpt_async(AC_INQ_CHANGED,
9638b8a9b1dSJustin T. Gibbs 					  done_ccb->ccb_h.path, NULL);
9643393f8daSKenneth D. Merry 			if (scsi_cmd->opcode == REQUEST_SENSE) {
9653393f8daSKenneth D. Merry 				u_int sense_key;
9663393f8daSKenneth D. Merry 
9673393f8daSKenneth D. Merry 				sense_key = saved_ccb->csio.sense_data.flags;
9683393f8daSKenneth D. Merry 				sense_key &= SSD_KEY;
9693393f8daSKenneth D. Merry 				if (sense_key != SSD_KEY_NO_SENSE) {
97086addc52SThomas Quinot 					saved_ccb->ccb_h.status |=
9713393f8daSKenneth D. Merry 					    CAM_AUTOSNS_VALID;
9723393f8daSKenneth D. Merry 					xpt_print_path(saved_ccb->ccb_h.path);
9733393f8daSKenneth D. Merry 					printf("Recovered Sense\n");
9743393f8daSKenneth D. Merry #if 0
9753393f8daSKenneth D. Merry 					scsi_sense_print(&saved_ccb->csio);
9763393f8daSKenneth D. Merry #endif
9773393f8daSKenneth D. Merry 					cam_error_print(saved_ccb, CAM_ESF_ALL,
9783393f8daSKenneth D. Merry 							CAM_EPF_ALL);
9793393f8daSKenneth D. Merry 					xpt_done_ccb = TRUE;
9803393f8daSKenneth D. Merry 				}
9813393f8daSKenneth D. Merry 			}
9828b8a9b1dSJustin T. Gibbs 		}
9838b8a9b1dSJustin T. Gibbs 		bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
9848b8a9b1dSJustin T. Gibbs 		      sizeof(union ccb));
9858b8a9b1dSJustin T. Gibbs 
98660a899a0SKenneth D. Merry 		periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
98760a899a0SKenneth D. Merry 
9883393f8daSKenneth D. Merry 		if (xpt_done_ccb == FALSE)
9898b8a9b1dSJustin T. Gibbs 			xpt_action(done_ccb);
9908b8a9b1dSJustin T. Gibbs 
9918b8a9b1dSJustin T. Gibbs 		break;
9923393f8daSKenneth D. Merry 	}
9938b8a9b1dSJustin T. Gibbs 	case CAM_SCSI_STATUS_ERROR:
9948b8a9b1dSJustin T. Gibbs 		scsi_cmd = (struct scsi_start_stop_unit *)
9958b8a9b1dSJustin T. Gibbs 				&done_ccb->csio.cdb_io.cdb_bytes;
9968b8a9b1dSJustin T. Gibbs 		if (sense != 0) {
99765afe1f4SKenneth D. Merry 			struct ccb_getdev cgd;
9988b8a9b1dSJustin T. Gibbs 			struct scsi_sense_data *sense;
9998b8a9b1dSJustin T. Gibbs 			int    error_code, sense_key, asc, ascq;
100065afe1f4SKenneth D. Merry 			scsi_sense_action err_action;
10018b8a9b1dSJustin T. Gibbs 
10028b8a9b1dSJustin T. Gibbs 			sense = &done_ccb->csio.sense_data;
10038b8a9b1dSJustin T. Gibbs 			scsi_extract_sense(sense, &error_code,
10048b8a9b1dSJustin T. Gibbs 					   &sense_key, &asc, &ascq);
10058b8a9b1dSJustin T. Gibbs 
10068b8a9b1dSJustin T. Gibbs 			/*
100765afe1f4SKenneth D. Merry 			 * Grab the inquiry data for this device.
100865afe1f4SKenneth D. Merry 			 */
100965afe1f4SKenneth D. Merry 			xpt_setup_ccb(&cgd.ccb_h, done_ccb->ccb_h.path,
101065afe1f4SKenneth D. Merry 				      /*priority*/ 1);
101165afe1f4SKenneth D. Merry 			cgd.ccb_h.func_code = XPT_GDEV_TYPE;
101265afe1f4SKenneth D. Merry 			xpt_action((union ccb *)&cgd);
101365afe1f4SKenneth D. Merry 			err_action = scsi_error_action(&done_ccb->csio,
101465afe1f4SKenneth D. Merry 						       &cgd.inq_data, 0);
101565afe1f4SKenneth D. Merry 
101665afe1f4SKenneth D. Merry 			/*
10178b8a9b1dSJustin T. Gibbs 	 		 * If the error is "invalid field in CDB",
10188b8a9b1dSJustin T. Gibbs 			 * and the load/eject flag is set, turn the
10198b8a9b1dSJustin T. Gibbs 			 * flag off and try again.  This is just in
10208b8a9b1dSJustin T. Gibbs 			 * case the drive in question barfs on the
10218b8a9b1dSJustin T. Gibbs 			 * load eject flag.  The CAM code should set
10228b8a9b1dSJustin T. Gibbs 			 * the load/eject flag by default for
10238b8a9b1dSJustin T. Gibbs 			 * removable media.
10248b8a9b1dSJustin T. Gibbs 			 */
10258b8a9b1dSJustin T. Gibbs 
10268b8a9b1dSJustin T. Gibbs 			/* XXX KDM
10278b8a9b1dSJustin T. Gibbs 			 * Should we check to see what the specific
10288b8a9b1dSJustin T. Gibbs 			 * scsi status is??  Or does it not matter
10298b8a9b1dSJustin T. Gibbs 			 * since we already know that there was an
10308b8a9b1dSJustin T. Gibbs 			 * error, and we know what the specific
10318b8a9b1dSJustin T. Gibbs 			 * error code was, and we know what the
10328b8a9b1dSJustin T. Gibbs 			 * opcode is..
10338b8a9b1dSJustin T. Gibbs 			 */
10348b8a9b1dSJustin T. Gibbs 			if ((scsi_cmd->opcode == START_STOP_UNIT) &&
10358b8a9b1dSJustin T. Gibbs 			    ((scsi_cmd->how & SSS_LOEJ) != 0) &&
10368b8a9b1dSJustin T. Gibbs 			     (asc == 0x24) && (ascq == 0x00) &&
10378b8a9b1dSJustin T. Gibbs 			     (done_ccb->ccb_h.retry_count > 0)) {
10388b8a9b1dSJustin T. Gibbs 
10398b8a9b1dSJustin T. Gibbs 				scsi_cmd->how &= ~SSS_LOEJ;
10408b8a9b1dSJustin T. Gibbs 
10418b8a9b1dSJustin T. Gibbs 				xpt_action(done_ccb);
10428b8a9b1dSJustin T. Gibbs 
104365afe1f4SKenneth D. Merry 			} else if ((done_ccb->ccb_h.retry_count > 1)
104465afe1f4SKenneth D. Merry 				&& ((err_action & SS_MASK) != SS_FAIL)) {
104565afe1f4SKenneth D. Merry 
10468b8a9b1dSJustin T. Gibbs 				/*
10478b8a9b1dSJustin T. Gibbs 				 * In this case, the error recovery
10488b8a9b1dSJustin T. Gibbs 				 * command failed, but we've got
10498b8a9b1dSJustin T. Gibbs 				 * some retries left on it.  Give
105065afe1f4SKenneth D. Merry 				 * it another try unless this is an
105165afe1f4SKenneth D. Merry 				 * unretryable error.
10528b8a9b1dSJustin T. Gibbs 				 */
10538b8a9b1dSJustin T. Gibbs 
10548b8a9b1dSJustin T. Gibbs 				/* set the timeout to .5 sec */
10558b8a9b1dSJustin T. Gibbs 				relsim_flags =
10568b8a9b1dSJustin T. Gibbs 					RELSIM_RELEASE_AFTER_TIMEOUT;
10578b8a9b1dSJustin T. Gibbs 				timeout = 500;
10588b8a9b1dSJustin T. Gibbs 
10598b8a9b1dSJustin T. Gibbs 				xpt_action(done_ccb);
10608b8a9b1dSJustin T. Gibbs 
10618b8a9b1dSJustin T. Gibbs 				break;
10628b8a9b1dSJustin T. Gibbs 
10638b8a9b1dSJustin T. Gibbs 			} else {
10648b8a9b1dSJustin T. Gibbs 				/*
10653393f8daSKenneth D. Merry 				 * Perform the final retry with the original
10663393f8daSKenneth D. Merry 				 * CCB so that final error processing is
10673393f8daSKenneth D. Merry 				 * performed by the owner of the CCB.
10688b8a9b1dSJustin T. Gibbs 				 */
10698b8a9b1dSJustin T. Gibbs 				bcopy(done_ccb->ccb_h.saved_ccb_ptr,
10708b8a9b1dSJustin T. Gibbs 				      done_ccb, sizeof(union ccb));
10718b8a9b1dSJustin T. Gibbs 
107260a899a0SKenneth D. Merry 				periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
107360a899a0SKenneth D. Merry 
10748b8a9b1dSJustin T. Gibbs 				xpt_action(done_ccb);
10758b8a9b1dSJustin T. Gibbs 			}
10768b8a9b1dSJustin T. Gibbs 		} else {
10778b8a9b1dSJustin T. Gibbs 			/*
10788b8a9b1dSJustin T. Gibbs 			 * Eh??  The command failed, but we don't
10798b8a9b1dSJustin T. Gibbs 			 * have any sense.  What's up with that?
10808b8a9b1dSJustin T. Gibbs 			 * Fire the CCB again to return it to the
10818b8a9b1dSJustin T. Gibbs 			 * caller.
10828b8a9b1dSJustin T. Gibbs 			 */
10838b8a9b1dSJustin T. Gibbs 			bcopy(done_ccb->ccb_h.saved_ccb_ptr,
10848b8a9b1dSJustin T. Gibbs 			      done_ccb, sizeof(union ccb));
10858b8a9b1dSJustin T. Gibbs 
108660a899a0SKenneth D. Merry 			periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
108760a899a0SKenneth D. Merry 
10888b8a9b1dSJustin T. Gibbs 			xpt_action(done_ccb);
10898b8a9b1dSJustin T. Gibbs 
10908b8a9b1dSJustin T. Gibbs 		}
10918b8a9b1dSJustin T. Gibbs 		break;
10928b8a9b1dSJustin T. Gibbs 	default:
10938b8a9b1dSJustin T. Gibbs 		bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb,
10948b8a9b1dSJustin T. Gibbs 		      sizeof(union ccb));
10958b8a9b1dSJustin T. Gibbs 
109660a899a0SKenneth D. Merry 		periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
109760a899a0SKenneth D. Merry 
10988b8a9b1dSJustin T. Gibbs 		xpt_action(done_ccb);
10998b8a9b1dSJustin T. Gibbs 
11008b8a9b1dSJustin T. Gibbs 		break;
11018b8a9b1dSJustin T. Gibbs 	}
11028b8a9b1dSJustin T. Gibbs 
11038b8a9b1dSJustin T. Gibbs 	/* decrement the retry count */
11043393f8daSKenneth D. Merry 	/*
11053393f8daSKenneth D. Merry 	 * XXX This isn't appropriate in all cases.  Restructure,
11063393f8daSKenneth D. Merry 	 *     so that the retry count is only decremented on an
11073393f8daSKenneth D. Merry 	 *     actual retry.  Remeber that the orignal ccb had its
11083393f8daSKenneth D. Merry 	 *     retry count dropped before entering recovery, so
11093393f8daSKenneth D. Merry 	 *     doing it again is a bug.
11103393f8daSKenneth D. Merry 	 */
11118b8a9b1dSJustin T. Gibbs 	if (done_ccb->ccb_h.retry_count > 0)
11128b8a9b1dSJustin T. Gibbs 		done_ccb->ccb_h.retry_count--;
11138b8a9b1dSJustin T. Gibbs 
11148b8a9b1dSJustin T. Gibbs 	qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path,
11158b8a9b1dSJustin T. Gibbs 				      /*relsim_flags*/relsim_flags,
11168b8a9b1dSJustin T. Gibbs 				      /*openings*/0,
11178b8a9b1dSJustin T. Gibbs 				      /*timeout*/timeout,
11188b8a9b1dSJustin T. Gibbs 				      /*getcount_only*/0);
11193393f8daSKenneth D. Merry 	if (xpt_done_ccb == TRUE)
11203393f8daSKenneth D. Merry 		(*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
11218b8a9b1dSJustin T. Gibbs }
11228b8a9b1dSJustin T. Gibbs 
11238b8a9b1dSJustin T. Gibbs /*
112487cfaf0eSJustin T. Gibbs  * Generic Async Event handler.  Peripheral drivers usually
112587cfaf0eSJustin T. Gibbs  * filter out the events that require personal attention,
112687cfaf0eSJustin T. Gibbs  * and leave the rest to this function.
112787cfaf0eSJustin T. Gibbs  */
112887cfaf0eSJustin T. Gibbs void
112987cfaf0eSJustin T. Gibbs cam_periph_async(struct cam_periph *periph, u_int32_t code,
113087cfaf0eSJustin T. Gibbs 		 struct cam_path *path, void *arg)
113187cfaf0eSJustin T. Gibbs {
113287cfaf0eSJustin T. Gibbs 	switch (code) {
113387cfaf0eSJustin T. Gibbs 	case AC_LOST_DEVICE:
113487cfaf0eSJustin T. Gibbs 		cam_periph_invalidate(periph);
113587cfaf0eSJustin T. Gibbs 		break;
113687cfaf0eSJustin T. Gibbs 	case AC_SENT_BDR:
113787cfaf0eSJustin T. Gibbs 	case AC_BUS_RESET:
113887cfaf0eSJustin T. Gibbs 	{
11393a937198SBrooks Davis 		cam_periph_bus_settle(periph, scsi_delay);
114087cfaf0eSJustin T. Gibbs 		break;
114187cfaf0eSJustin T. Gibbs 	}
114287cfaf0eSJustin T. Gibbs 	default:
114387cfaf0eSJustin T. Gibbs 		break;
114487cfaf0eSJustin T. Gibbs 	}
114587cfaf0eSJustin T. Gibbs }
114687cfaf0eSJustin T. Gibbs 
114787cfaf0eSJustin T. Gibbs void
114887cfaf0eSJustin T. Gibbs cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
114987cfaf0eSJustin T. Gibbs {
115087cfaf0eSJustin T. Gibbs 	struct ccb_getdevstats cgds;
115187cfaf0eSJustin T. Gibbs 
115287cfaf0eSJustin T. Gibbs 	xpt_setup_ccb(&cgds.ccb_h, periph->path, /*priority*/1);
115387cfaf0eSJustin T. Gibbs 	cgds.ccb_h.func_code = XPT_GDEV_STATS;
115487cfaf0eSJustin T. Gibbs 	xpt_action((union ccb *)&cgds);
115587cfaf0eSJustin T. Gibbs 	cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
115687cfaf0eSJustin T. Gibbs }
115787cfaf0eSJustin T. Gibbs 
115887cfaf0eSJustin T. Gibbs void
115987cfaf0eSJustin T. Gibbs cam_periph_freeze_after_event(struct cam_periph *periph,
116087cfaf0eSJustin T. Gibbs 			      struct timeval* event_time, u_int duration_ms)
116187cfaf0eSJustin T. Gibbs {
116287cfaf0eSJustin T. Gibbs 	struct timeval delta;
116387cfaf0eSJustin T. Gibbs 	struct timeval duration_tv;
116487cfaf0eSJustin T. Gibbs 	int s;
116587cfaf0eSJustin T. Gibbs 
116687cfaf0eSJustin T. Gibbs 	s = splclock();
116787cfaf0eSJustin T. Gibbs 	microtime(&delta);
116887cfaf0eSJustin T. Gibbs 	splx(s);
116987cfaf0eSJustin T. Gibbs 	timevalsub(&delta, event_time);
117087cfaf0eSJustin T. Gibbs 	duration_tv.tv_sec = duration_ms / 1000;
117187cfaf0eSJustin T. Gibbs 	duration_tv.tv_usec = (duration_ms % 1000) * 1000;
117287cfaf0eSJustin T. Gibbs 	if (timevalcmp(&delta, &duration_tv, <)) {
117387cfaf0eSJustin T. Gibbs 		timevalsub(&duration_tv, &delta);
117487cfaf0eSJustin T. Gibbs 
117587cfaf0eSJustin T. Gibbs 		duration_ms = duration_tv.tv_sec * 1000;
117687cfaf0eSJustin T. Gibbs 		duration_ms += duration_tv.tv_usec / 1000;
117787cfaf0eSJustin T. Gibbs 		cam_freeze_devq(periph->path);
117887cfaf0eSJustin T. Gibbs 		cam_release_devq(periph->path,
117987cfaf0eSJustin T. Gibbs 				RELSIM_RELEASE_AFTER_TIMEOUT,
118087cfaf0eSJustin T. Gibbs 				/*reduction*/0,
118187cfaf0eSJustin T. Gibbs 				/*timeout*/duration_ms,
118287cfaf0eSJustin T. Gibbs 				/*getcount_only*/0);
118387cfaf0eSJustin T. Gibbs 	}
118487cfaf0eSJustin T. Gibbs 
118587cfaf0eSJustin T. Gibbs }
118687cfaf0eSJustin T. Gibbs 
11873393f8daSKenneth D. Merry static int
11883393f8daSKenneth D. Merry camperiphscsistatuserror(union ccb *ccb, cam_flags camflags,
11893393f8daSKenneth D. Merry 			 u_int32_t sense_flags, union ccb *save_ccb,
11903393f8daSKenneth D. Merry 			 int *openings, u_int32_t *relsim_flags,
11913393f8daSKenneth D. Merry 			 u_int32_t *timeout)
11928b8a9b1dSJustin T. Gibbs {
11938b8a9b1dSJustin T. Gibbs 	int error;
11948b8a9b1dSJustin T. Gibbs 
11958b8a9b1dSJustin T. Gibbs 	switch (ccb->csio.scsi_status) {
11968b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_OK:
11978b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_COND_MET:
11988b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_INTERMED:
11998b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_INTERMED_COND_MET:
12008b8a9b1dSJustin T. Gibbs 		error = 0;
12018b8a9b1dSJustin T. Gibbs 		break;
12028b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_CMD_TERMINATED:
12038b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_CHECK_COND:
12043393f8daSKenneth D. Merry 		error = camperiphscsisenseerror(ccb,
12053393f8daSKenneth D. Merry 					        camflags,
12068b8a9b1dSJustin T. Gibbs 					        sense_flags,
12073393f8daSKenneth D. Merry 					        save_ccb,
12083393f8daSKenneth D. Merry 					        openings,
12093393f8daSKenneth D. Merry 					        relsim_flags,
12103393f8daSKenneth D. Merry 					        timeout);
12118b8a9b1dSJustin T. Gibbs 		break;
12128b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_QUEUE_FULL:
12138b8a9b1dSJustin T. Gibbs 	{
12148b8a9b1dSJustin T. Gibbs 		/* no decrement */
121582815562SJustin T. Gibbs 		struct ccb_getdevstats cgds;
12168b8a9b1dSJustin T. Gibbs 
12178b8a9b1dSJustin T. Gibbs 		/*
12188b8a9b1dSJustin T. Gibbs 		 * First off, find out what the current
12198b8a9b1dSJustin T. Gibbs 		 * transaction counts are.
12208b8a9b1dSJustin T. Gibbs 		 */
122182815562SJustin T. Gibbs 		xpt_setup_ccb(&cgds.ccb_h,
12228b8a9b1dSJustin T. Gibbs 			      ccb->ccb_h.path,
12238b8a9b1dSJustin T. Gibbs 			      /*priority*/1);
122482815562SJustin T. Gibbs 		cgds.ccb_h.func_code = XPT_GDEV_STATS;
122582815562SJustin T. Gibbs 		xpt_action((union ccb *)&cgds);
12268b8a9b1dSJustin T. Gibbs 
12278b8a9b1dSJustin T. Gibbs 		/*
12288b8a9b1dSJustin T. Gibbs 		 * If we were the only transaction active, treat
12298b8a9b1dSJustin T. Gibbs 		 * the QUEUE FULL as if it were a BUSY condition.
12308b8a9b1dSJustin T. Gibbs 		 */
123182815562SJustin T. Gibbs 		if (cgds.dev_active != 0) {
123282815562SJustin T. Gibbs 			int total_openings;
123382815562SJustin T. Gibbs 
12348b8a9b1dSJustin T. Gibbs 			/*
12358b8a9b1dSJustin T. Gibbs 		 	 * Reduce the number of openings to
12368b8a9b1dSJustin T. Gibbs 			 * be 1 less than the amount it took
12378b8a9b1dSJustin T. Gibbs 			 * to get a queue full bounded by the
12388b8a9b1dSJustin T. Gibbs 			 * minimum allowed tag count for this
12398b8a9b1dSJustin T. Gibbs 			 * device.
12408b8a9b1dSJustin T. Gibbs 		 	 */
12413393f8daSKenneth D. Merry 			total_openings = cgds.dev_active + cgds.dev_openings;
12423393f8daSKenneth D. Merry 			*openings = cgds.dev_active;
12433393f8daSKenneth D. Merry 			if (*openings < cgds.mintags)
12443393f8daSKenneth D. Merry 				*openings = cgds.mintags;
12453393f8daSKenneth D. Merry 			if (*openings < total_openings)
12463393f8daSKenneth D. Merry 				*relsim_flags = RELSIM_ADJUST_OPENINGS;
12478b8a9b1dSJustin T. Gibbs 			else {
12488b8a9b1dSJustin T. Gibbs 				/*
12498b8a9b1dSJustin T. Gibbs 				 * Some devices report queue full for
12508b8a9b1dSJustin T. Gibbs 				 * temporary resource shortages.  For
12518b8a9b1dSJustin T. Gibbs 				 * this reason, we allow a minimum
12528b8a9b1dSJustin T. Gibbs 				 * tag count to be entered via a
12538b8a9b1dSJustin T. Gibbs 				 * quirk entry to prevent the queue
12548b8a9b1dSJustin T. Gibbs 				 * count on these devices from falling
12558b8a9b1dSJustin T. Gibbs 				 * to a pessimisticly low value.  We
12568b8a9b1dSJustin T. Gibbs 				 * still wait for the next successful
12578b8a9b1dSJustin T. Gibbs 				 * completion, however, before queueing
12588b8a9b1dSJustin T. Gibbs 				 * more transactions to the device.
12598b8a9b1dSJustin T. Gibbs 				 */
12603393f8daSKenneth D. Merry 				*relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
12618b8a9b1dSJustin T. Gibbs 			}
12623393f8daSKenneth D. Merry 			*timeout = 0;
12638b8a9b1dSJustin T. Gibbs 			error = ERESTART;
126496d333b7SMatt Jacob 			if (bootverbose) {
126596d333b7SMatt Jacob 				xpt_print_path(ccb->ccb_h.path);
126696d333b7SMatt Jacob 				printf("Queue Full\n");
126796d333b7SMatt Jacob 			}
12688b8a9b1dSJustin T. Gibbs 			break;
12698b8a9b1dSJustin T. Gibbs 		}
12708b8a9b1dSJustin T. Gibbs 		/* FALLTHROUGH */
12718b8a9b1dSJustin T. Gibbs 	}
12728b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_BUSY:
12738b8a9b1dSJustin T. Gibbs 		/*
12748b8a9b1dSJustin T. Gibbs 		 * Restart the queue after either another
12758b8a9b1dSJustin T. Gibbs 		 * command completes or a 1 second timeout.
12768b8a9b1dSJustin T. Gibbs 		 */
127796d333b7SMatt Jacob 		if (bootverbose) {
127896d333b7SMatt Jacob 			xpt_print_path(ccb->ccb_h.path);
127996d333b7SMatt Jacob 			printf("Device Busy\n");
128096d333b7SMatt Jacob 		}
12813393f8daSKenneth D. Merry 	 	if (ccb->ccb_h.retry_count > 0) {
1282af51b059SMatt Jacob 	 		ccb->ccb_h.retry_count--;
12838b8a9b1dSJustin T. Gibbs 			error = ERESTART;
12843393f8daSKenneth D. Merry 			*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
12858b8a9b1dSJustin T. Gibbs 				      | RELSIM_RELEASE_AFTER_CMDCMPLT;
12863393f8daSKenneth D. Merry 			*timeout = 1000;
1287af51b059SMatt Jacob 		} else {
1288af51b059SMatt Jacob 			error = EIO;
1289af51b059SMatt Jacob 		}
12908b8a9b1dSJustin T. Gibbs 		break;
12918b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_RESERV_CONFLICT:
129296d333b7SMatt Jacob 		xpt_print_path(ccb->ccb_h.path);
129396d333b7SMatt Jacob 		printf("Reservation Conflict\n");
12948b8a9b1dSJustin T. Gibbs 		error = EIO;
12958b8a9b1dSJustin T. Gibbs 		break;
12968b8a9b1dSJustin T. Gibbs 	default:
129796d333b7SMatt Jacob 		xpt_print_path(ccb->ccb_h.path);
129896d333b7SMatt Jacob 		printf("SCSI Status 0x%x\n", ccb->csio.scsi_status);
12998b8a9b1dSJustin T. Gibbs 		error = EIO;
13008b8a9b1dSJustin T. Gibbs 		break;
13018b8a9b1dSJustin T. Gibbs 	}
13023393f8daSKenneth D. Merry 	return (error);
13033393f8daSKenneth D. Merry }
13043393f8daSKenneth D. Merry 
13053393f8daSKenneth D. Merry static int
13063393f8daSKenneth D. Merry camperiphscsisenseerror(union ccb *ccb, cam_flags camflags,
13073393f8daSKenneth D. Merry 			u_int32_t sense_flags, union ccb *save_ccb,
13083393f8daSKenneth D. Merry 		       int *openings, u_int32_t *relsim_flags,
13093393f8daSKenneth D. Merry 		       u_int32_t *timeout)
13103393f8daSKenneth D. Merry {
13113393f8daSKenneth D. Merry 	struct cam_periph *periph;
13123393f8daSKenneth D. Merry 	int error;
13133393f8daSKenneth D. Merry 
13143393f8daSKenneth D. Merry 	periph = xpt_path_periph(ccb->ccb_h.path);
13153393f8daSKenneth D. Merry 	if (periph->flags & CAM_PERIPH_RECOVERY_INPROG) {
13163393f8daSKenneth D. Merry 
13173393f8daSKenneth D. Merry 		/*
13183393f8daSKenneth D. Merry 		 * If error recovery is already in progress, don't attempt
13193393f8daSKenneth D. Merry 		 * to process this error, but requeue it unconditionally
13203393f8daSKenneth D. Merry 		 * and attempt to process it once error recovery has
13213393f8daSKenneth D. Merry 		 * completed.  This failed command is probably related to
13223393f8daSKenneth D. Merry 		 * the error that caused the currently active error recovery
13233393f8daSKenneth D. Merry 		 * action so our  current recovery efforts should also
13243393f8daSKenneth D. Merry 		 * address this command.  Be aware that the error recovery
13253393f8daSKenneth D. Merry 		 * code assumes that only one recovery action is in progress
13263393f8daSKenneth D. Merry 		 * on a particular peripheral instance at any given time
13273393f8daSKenneth D. Merry 		 * (e.g. only one saved CCB for error recovery) so it is
13283393f8daSKenneth D. Merry 		 * imperitive that we don't violate this assumption.
13293393f8daSKenneth D. Merry 		 */
13303393f8daSKenneth D. Merry 		error = ERESTART;
13313393f8daSKenneth D. Merry 	} else {
13323393f8daSKenneth D. Merry 		scsi_sense_action err_action;
13333393f8daSKenneth D. Merry 		struct ccb_getdev cgd;
13343393f8daSKenneth D. Merry 		const char *action_string;
13353393f8daSKenneth D. Merry 		union ccb* print_ccb;
13363393f8daSKenneth D. Merry 
13373393f8daSKenneth D. Merry 		/* A description of the error recovery action performed */
13383393f8daSKenneth D. Merry 		action_string = NULL;
13393393f8daSKenneth D. Merry 
13403393f8daSKenneth D. Merry 		/*
13413393f8daSKenneth D. Merry 		 * The location of the orignal ccb
13423393f8daSKenneth D. Merry 		 * for sense printing purposes.
13433393f8daSKenneth D. Merry 		 */
13443393f8daSKenneth D. Merry 		print_ccb = ccb;
13453393f8daSKenneth D. Merry 
13463393f8daSKenneth D. Merry 		/*
13473393f8daSKenneth D. Merry 		 * Grab the inquiry data for this device.
13483393f8daSKenneth D. Merry 		 */
13493393f8daSKenneth D. Merry 		xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, /*priority*/ 1);
13503393f8daSKenneth D. Merry 		cgd.ccb_h.func_code = XPT_GDEV_TYPE;
13513393f8daSKenneth D. Merry 		xpt_action((union ccb *)&cgd);
13523393f8daSKenneth D. Merry 
13533393f8daSKenneth D. Merry 		if ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)
13543393f8daSKenneth D. Merry 			err_action = scsi_error_action(&ccb->csio,
13553393f8daSKenneth D. Merry 						       &cgd.inq_data,
13563393f8daSKenneth D. Merry 						       sense_flags);
13573393f8daSKenneth D. Merry 		else if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
13583393f8daSKenneth D. Merry 			err_action = SS_REQSENSE;
13593393f8daSKenneth D. Merry 		else
13603393f8daSKenneth D. Merry 			err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
13613393f8daSKenneth D. Merry 
13623393f8daSKenneth D. Merry 		error = err_action & SS_ERRMASK;
13633393f8daSKenneth D. Merry 
13643393f8daSKenneth D. Merry 		/*
13653393f8daSKenneth D. Merry 		 * If the recovery action will consume a retry,
13663393f8daSKenneth D. Merry 		 * make sure we actually have retries available.
13673393f8daSKenneth D. Merry 		 */
13683393f8daSKenneth D. Merry 		if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
13693393f8daSKenneth D. Merry 		 	if (ccb->ccb_h.retry_count > 0)
13703393f8daSKenneth D. Merry 		 		ccb->ccb_h.retry_count--;
13713393f8daSKenneth D. Merry 			else {
13723393f8daSKenneth D. Merry 				action_string = "Retries Exhausted";
13733393f8daSKenneth D. Merry 				goto sense_error_done;
13743393f8daSKenneth D. Merry 			}
13753393f8daSKenneth D. Merry 		}
13763393f8daSKenneth D. Merry 
13773393f8daSKenneth D. Merry 		if ((err_action & SS_MASK) >= SS_START) {
13783393f8daSKenneth D. Merry 			/*
13793393f8daSKenneth D. Merry 			 * Do common portions of commands that
13803393f8daSKenneth D. Merry 			 * use recovery CCBs.
13813393f8daSKenneth D. Merry 			 */
13823393f8daSKenneth D. Merry 			if (save_ccb == NULL) {
13833393f8daSKenneth D. Merry 				action_string = "No recovery CCB supplied";
13843393f8daSKenneth D. Merry 				goto sense_error_done;
13853393f8daSKenneth D. Merry 			}
13863393f8daSKenneth D. Merry 			bcopy(ccb, save_ccb, sizeof(*save_ccb));
13873393f8daSKenneth D. Merry 			print_ccb = save_ccb;
13883393f8daSKenneth D. Merry 			periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
13893393f8daSKenneth D. Merry 		}
13903393f8daSKenneth D. Merry 
13913393f8daSKenneth D. Merry 		switch (err_action & SS_MASK) {
13923393f8daSKenneth D. Merry 		case SS_NOP:
139300e54d14SKenneth D. Merry 			action_string = "No Recovery Action Needed";
139400e54d14SKenneth D. Merry 			error = 0;
139500e54d14SKenneth D. Merry 			break;
13963393f8daSKenneth D. Merry 		case SS_RETRY:
139796d333b7SMatt Jacob 			action_string = "Retrying Command (per Sense Data)";
13983393f8daSKenneth D. Merry 			error = ERESTART;
13998b8a9b1dSJustin T. Gibbs 			break;
14003393f8daSKenneth D. Merry 		case SS_FAIL:
14013393f8daSKenneth D. Merry 			action_string = "Unretryable error";
14023393f8daSKenneth D. Merry 			break;
14033393f8daSKenneth D. Merry 		case SS_START:
14043393f8daSKenneth D. Merry 		{
14053393f8daSKenneth D. Merry 			int le;
14063393f8daSKenneth D. Merry 
14073393f8daSKenneth D. Merry 			/*
14083393f8daSKenneth D. Merry 			 * Send a start unit command to the device, and
14093393f8daSKenneth D. Merry 			 * then retry the command.
14103393f8daSKenneth D. Merry 			 */
14113393f8daSKenneth D. Merry 			action_string = "Attempting to Start Unit";
14123393f8daSKenneth D. Merry 
14133393f8daSKenneth D. Merry 			/*
14143393f8daSKenneth D. Merry 			 * Check for removable media and set
14153393f8daSKenneth D. Merry 			 * load/eject flag appropriately.
14163393f8daSKenneth D. Merry 			 */
14173393f8daSKenneth D. Merry 			if (SID_IS_REMOVABLE(&cgd.inq_data))
14183393f8daSKenneth D. Merry 				le = TRUE;
14193393f8daSKenneth D. Merry 			else
14203393f8daSKenneth D. Merry 				le = FALSE;
14213393f8daSKenneth D. Merry 
14223393f8daSKenneth D. Merry 			scsi_start_stop(&ccb->csio,
14233393f8daSKenneth D. Merry 					/*retries*/1,
14243393f8daSKenneth D. Merry 					camperiphdone,
14253393f8daSKenneth D. Merry 					MSG_SIMPLE_Q_TAG,
14263393f8daSKenneth D. Merry 					/*start*/TRUE,
14273393f8daSKenneth D. Merry 					/*load/eject*/le,
14283393f8daSKenneth D. Merry 					/*immediate*/FALSE,
14293393f8daSKenneth D. Merry 					SSD_FULL_SIZE,
14303393f8daSKenneth D. Merry 					/*timeout*/50000);
14313393f8daSKenneth D. Merry 			break;
14323393f8daSKenneth D. Merry 		}
14333393f8daSKenneth D. Merry 		case SS_TUR:
14343393f8daSKenneth D. Merry 		{
14353393f8daSKenneth D. Merry 			/*
14363393f8daSKenneth D. Merry 			 * Send a Test Unit Ready to the device.
14373393f8daSKenneth D. Merry 			 * If the 'many' flag is set, we send 120
14383393f8daSKenneth D. Merry 			 * test unit ready commands, one every half
14393393f8daSKenneth D. Merry 			 * second.  Otherwise, we just send one TUR.
14403393f8daSKenneth D. Merry 			 * We only want to do this if the retry
14413393f8daSKenneth D. Merry 			 * count has not been exhausted.
14423393f8daSKenneth D. Merry 			 */
14433393f8daSKenneth D. Merry 			int retries;
14443393f8daSKenneth D. Merry 
14453393f8daSKenneth D. Merry 			if ((err_action & SSQ_MANY) != 0) {
14463393f8daSKenneth D. Merry 				action_string = "Polling device for readiness";
14473393f8daSKenneth D. Merry 				retries = 120;
14483393f8daSKenneth D. Merry 			} else {
14493393f8daSKenneth D. Merry 				action_string = "Testing device for readiness";
14503393f8daSKenneth D. Merry 				retries = 1;
14513393f8daSKenneth D. Merry 			}
14523393f8daSKenneth D. Merry 			scsi_test_unit_ready(&ccb->csio,
14533393f8daSKenneth D. Merry 					     retries,
14543393f8daSKenneth D. Merry 					     camperiphdone,
14553393f8daSKenneth D. Merry 					     MSG_SIMPLE_Q_TAG,
14563393f8daSKenneth D. Merry 					     SSD_FULL_SIZE,
14573393f8daSKenneth D. Merry 					     /*timeout*/5000);
14583393f8daSKenneth D. Merry 
14593393f8daSKenneth D. Merry 			/*
14603393f8daSKenneth D. Merry 			 * Accomplish our 500ms delay by deferring
14613393f8daSKenneth D. Merry 			 * the release of our device queue appropriately.
14623393f8daSKenneth D. Merry 			 */
14633393f8daSKenneth D. Merry 			*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
14643393f8daSKenneth D. Merry 			*timeout = 500;
14653393f8daSKenneth D. Merry 			break;
14663393f8daSKenneth D. Merry 		}
14673393f8daSKenneth D. Merry 		case SS_REQSENSE:
14683393f8daSKenneth D. Merry 		{
14693393f8daSKenneth D. Merry 			/*
14703393f8daSKenneth D. Merry 			 * Send a Request Sense to the device.  We
14713393f8daSKenneth D. Merry 			 * assume that we are in a contingent allegiance
14723393f8daSKenneth D. Merry 			 * condition so we do not tag this request.
14733393f8daSKenneth D. Merry 			 */
14743393f8daSKenneth D. Merry 			scsi_request_sense(&ccb->csio, /*retries*/1,
14753393f8daSKenneth D. Merry 					   camperiphdone,
14763393f8daSKenneth D. Merry 					   &save_ccb->csio.sense_data,
14773393f8daSKenneth D. Merry 					   sizeof(save_ccb->csio.sense_data),
14783393f8daSKenneth D. Merry 					   CAM_TAG_ACTION_NONE,
14793393f8daSKenneth D. Merry 					   /*sense_len*/SSD_FULL_SIZE,
14803393f8daSKenneth D. Merry 					   /*timeout*/5000);
14813393f8daSKenneth D. Merry 			break;
14823393f8daSKenneth D. Merry 		}
14833393f8daSKenneth D. Merry 		default:
1484e3c29144SWarner Losh 			panic("Unhandled error action %x", err_action);
14853393f8daSKenneth D. Merry 		}
14863393f8daSKenneth D. Merry 
14873393f8daSKenneth D. Merry 		if ((err_action & SS_MASK) >= SS_START) {
14883393f8daSKenneth D. Merry 			/*
14893393f8daSKenneth D. Merry 			 * Drop the priority to 0 so that the recovery
14903393f8daSKenneth D. Merry 			 * CCB is the first to execute.  Freeze the queue
14913393f8daSKenneth D. Merry 			 * after this command is sent so that we can
14923393f8daSKenneth D. Merry 			 * restore the old csio and have it queued in
14933393f8daSKenneth D. Merry 			 * the proper order before we release normal
14943393f8daSKenneth D. Merry 			 * transactions to the device.
14953393f8daSKenneth D. Merry 			 */
14963393f8daSKenneth D. Merry 			ccb->ccb_h.pinfo.priority = 0;
14973393f8daSKenneth D. Merry 			ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
14983393f8daSKenneth D. Merry 			ccb->ccb_h.saved_ccb_ptr = save_ccb;
14993393f8daSKenneth D. Merry 			error = ERESTART;
15003393f8daSKenneth D. Merry 		}
15013393f8daSKenneth D. Merry 
15023393f8daSKenneth D. Merry sense_error_done:
15033393f8daSKenneth D. Merry 		if ((err_action & SSQ_PRINT_SENSE) != 0
15043393f8daSKenneth D. Merry 		 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0) {
15053393f8daSKenneth D. Merry 			cam_error_print(print_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
15063393f8daSKenneth D. Merry 			xpt_print_path(ccb->ccb_h.path);
150796d333b7SMatt Jacob 			if (bootverbose)
150896d333b7SMatt Jacob 				scsi_sense_print(&print_ccb->csio);
15093393f8daSKenneth D. Merry 			printf("%s\n", action_string);
15103393f8daSKenneth D. Merry 		}
15113393f8daSKenneth D. Merry 	}
15123393f8daSKenneth D. Merry 	return (error);
15133393f8daSKenneth D. Merry }
15143393f8daSKenneth D. Merry 
15153393f8daSKenneth D. Merry /*
15163393f8daSKenneth D. Merry  * Generic error handler.  Peripheral drivers usually filter
15173393f8daSKenneth D. Merry  * out the errors that they handle in a unique mannor, then
15183393f8daSKenneth D. Merry  * call this function.
15193393f8daSKenneth D. Merry  */
15203393f8daSKenneth D. Merry int
15213393f8daSKenneth D. Merry cam_periph_error(union ccb *ccb, cam_flags camflags,
15223393f8daSKenneth D. Merry 		 u_int32_t sense_flags, union ccb *save_ccb)
15233393f8daSKenneth D. Merry {
15243393f8daSKenneth D. Merry 	const char *action_string;
15253393f8daSKenneth D. Merry 	cam_status  status;
15263393f8daSKenneth D. Merry 	int	    frozen;
152796d333b7SMatt Jacob 	int	    error, printed = 0;
15283393f8daSKenneth D. Merry 	int         openings;
15293393f8daSKenneth D. Merry 	u_int32_t   relsim_flags;
15303393f8daSKenneth D. Merry 	u_int32_t   timeout;
15313393f8daSKenneth D. Merry 
15323393f8daSKenneth D. Merry 	action_string = NULL;
15333393f8daSKenneth D. Merry 	status = ccb->ccb_h.status;
15343393f8daSKenneth D. Merry 	frozen = (status & CAM_DEV_QFRZN) != 0;
15353393f8daSKenneth D. Merry 	status &= CAM_STATUS_MASK;
1536c7ec4390SMatt Jacob 	openings = relsim_flags = 0;
15373393f8daSKenneth D. Merry 
15383393f8daSKenneth D. Merry 	switch (status) {
15393393f8daSKenneth D. Merry 	case CAM_REQ_CMP:
15403393f8daSKenneth D. Merry 		error = 0;
15413393f8daSKenneth D. Merry 		break;
15423393f8daSKenneth D. Merry 	case CAM_SCSI_STATUS_ERROR:
15433393f8daSKenneth D. Merry 		error = camperiphscsistatuserror(ccb,
15443393f8daSKenneth D. Merry 						 camflags,
15453393f8daSKenneth D. Merry 						 sense_flags,
15463393f8daSKenneth D. Merry 						 save_ccb,
15473393f8daSKenneth D. Merry 						 &openings,
15483393f8daSKenneth D. Merry 						 &relsim_flags,
15493393f8daSKenneth D. Merry 						 &timeout);
15503393f8daSKenneth D. Merry 		break;
15513393f8daSKenneth D. Merry 	case CAM_AUTOSENSE_FAIL:
15523393f8daSKenneth D. Merry 		xpt_print_path(ccb->ccb_h.path);
15533393f8daSKenneth D. Merry 		printf("AutoSense Failed\n");
1554c7ec4390SMatt Jacob 		error = EIO;	/* we have to kill the command */
1555c7ec4390SMatt Jacob 		break;
15568b8a9b1dSJustin T. Gibbs 	case CAM_REQ_CMP_ERR:
155796d333b7SMatt Jacob 		if (bootverbose && printed == 0) {
155896d333b7SMatt Jacob 			xpt_print_path(ccb->ccb_h.path);
155996d333b7SMatt Jacob 			printf("Request completed with CAM_REQ_CMP_ERR\n");
156096d333b7SMatt Jacob 			printed++;
156196d333b7SMatt Jacob 		}
156205867503SPoul-Henning Kamp 		/* FALLTHROUGH */
15638b8a9b1dSJustin T. Gibbs 	case CAM_CMD_TIMEOUT:
156496d333b7SMatt Jacob 		if (bootverbose && printed == 0) {
156596d333b7SMatt Jacob 			xpt_print_path(ccb->ccb_h.path);
1566a5074f70SPeter Wemm 			printf("Command timed out\n");
156796d333b7SMatt Jacob 			printed++;
156896d333b7SMatt Jacob 		}
156905867503SPoul-Henning Kamp 		/* FALLTHROUGH */
15708b8a9b1dSJustin T. Gibbs 	case CAM_UNEXP_BUSFREE:
157196d333b7SMatt Jacob 		if (bootverbose && printed == 0) {
157296d333b7SMatt Jacob 			xpt_print_path(ccb->ccb_h.path);
1573a5074f70SPeter Wemm 			printf("Unexpected Bus Free\n");
157496d333b7SMatt Jacob 			printed++;
157596d333b7SMatt Jacob 		}
157605867503SPoul-Henning Kamp 		/* FALLTHROUGH */
15778b8a9b1dSJustin T. Gibbs 	case CAM_UNCOR_PARITY:
157896d333b7SMatt Jacob 		if (bootverbose && printed == 0) {
157996d333b7SMatt Jacob 			xpt_print_path(ccb->ccb_h.path);
1580a5074f70SPeter Wemm 			printf("Uncorrected Parity Error\n");
158196d333b7SMatt Jacob 			printed++;
158296d333b7SMatt Jacob 		}
158305867503SPoul-Henning Kamp 		/* FALLTHROUGH */
15848b8a9b1dSJustin T. Gibbs 	case CAM_DATA_RUN_ERR:
158596d333b7SMatt Jacob 		if (bootverbose && printed == 0) {
158696d333b7SMatt Jacob 			xpt_print_path(ccb->ccb_h.path);
1587a5074f70SPeter Wemm 			printf("Data Overrun\n");
158896d333b7SMatt Jacob 			printed++;
158996d333b7SMatt Jacob 		}
159096d333b7SMatt Jacob 		error = EIO;	/* we have to kill the command */
15918b8a9b1dSJustin T. Gibbs 		/* decrement the number of retries */
15923393f8daSKenneth D. Merry 		if (ccb->ccb_h.retry_count > 0) {
15938b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.retry_count--;
15948b8a9b1dSJustin T. Gibbs 			error = ERESTART;
15958b8a9b1dSJustin T. Gibbs 		} else {
15963393f8daSKenneth D. Merry 			action_string = "Retries Exausted";
15978b8a9b1dSJustin T. Gibbs 			error = EIO;
15988b8a9b1dSJustin T. Gibbs 		}
15998b8a9b1dSJustin T. Gibbs 		break;
16008b8a9b1dSJustin T. Gibbs 	case CAM_UA_ABORT:
16018b8a9b1dSJustin T. Gibbs 	case CAM_UA_TERMIO:
16028b8a9b1dSJustin T. Gibbs 	case CAM_MSG_REJECT_REC:
16038b8a9b1dSJustin T. Gibbs 		/* XXX Don't know that these are correct */
16048b8a9b1dSJustin T. Gibbs 		error = EIO;
16058b8a9b1dSJustin T. Gibbs 		break;
16068b8a9b1dSJustin T. Gibbs 	case CAM_SEL_TIMEOUT:
16078b8a9b1dSJustin T. Gibbs 	{
16088b8a9b1dSJustin T. Gibbs 		struct cam_path *newpath;
16098b8a9b1dSJustin T. Gibbs 
16103393f8daSKenneth D. Merry 		if ((camflags & CAM_RETRY_SELTO) != 0) {
16113393f8daSKenneth D. Merry 			if (ccb->ccb_h.retry_count > 0) {
16123393f8daSKenneth D. Merry 
16133393f8daSKenneth D. Merry 				ccb->ccb_h.retry_count--;
16143393f8daSKenneth D. Merry 				error = ERESTART;
161596d333b7SMatt Jacob 				if (bootverbose && printed == 0) {
161696d333b7SMatt Jacob 					xpt_print_path(ccb->ccb_h.path);
1617a5074f70SPeter Wemm 					printf("Selection Timeout\n");
161896d333b7SMatt Jacob 					printed++;
161996d333b7SMatt Jacob 				}
16203393f8daSKenneth D. Merry 
16213393f8daSKenneth D. Merry 				/*
16223393f8daSKenneth D. Merry 				 * Wait a second to give the device
16233393f8daSKenneth D. Merry 				 * time to recover before we try again.
16243393f8daSKenneth D. Merry 				 */
16253393f8daSKenneth D. Merry 				relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
16263393f8daSKenneth D. Merry 				timeout = 1000;
16273393f8daSKenneth D. Merry 				break;
16283393f8daSKenneth D. Merry 			}
16293393f8daSKenneth D. Merry 		}
16303393f8daSKenneth D. Merry 		error = ENXIO;
16318b8a9b1dSJustin T. Gibbs 		/* Should we do more if we can't create the path?? */
16328b8a9b1dSJustin T. Gibbs 		if (xpt_create_path(&newpath, xpt_path_periph(ccb->ccb_h.path),
16338b8a9b1dSJustin T. Gibbs 				    xpt_path_path_id(ccb->ccb_h.path),
16348b8a9b1dSJustin T. Gibbs 				    xpt_path_target_id(ccb->ccb_h.path),
16358b8a9b1dSJustin T. Gibbs 				    CAM_LUN_WILDCARD) != CAM_REQ_CMP)
16368b8a9b1dSJustin T. Gibbs 			break;
16373393f8daSKenneth D. Merry 
16388b8a9b1dSJustin T. Gibbs 		/*
16398b8a9b1dSJustin T. Gibbs 		 * Let peripheral drivers know that this device has gone
16408b8a9b1dSJustin T. Gibbs 		 * away.
16418b8a9b1dSJustin T. Gibbs 		 */
16428b8a9b1dSJustin T. Gibbs 		xpt_async(AC_LOST_DEVICE, newpath, NULL);
16438b8a9b1dSJustin T. Gibbs 		xpt_free_path(newpath);
16448b8a9b1dSJustin T. Gibbs 		break;
16458b8a9b1dSJustin T. Gibbs 	}
16468b8a9b1dSJustin T. Gibbs 	case CAM_REQ_INVALID:
16478b8a9b1dSJustin T. Gibbs 	case CAM_PATH_INVALID:
16488b8a9b1dSJustin T. Gibbs 	case CAM_DEV_NOT_THERE:
16498b8a9b1dSJustin T. Gibbs 	case CAM_NO_HBA:
16508b8a9b1dSJustin T. Gibbs 	case CAM_PROVIDE_FAIL:
16518b8a9b1dSJustin T. Gibbs 	case CAM_REQ_TOO_BIG:
16528b8a9b1dSJustin T. Gibbs 		error = EINVAL;
16538b8a9b1dSJustin T. Gibbs 		break;
16548b8a9b1dSJustin T. Gibbs 	case CAM_SCSI_BUS_RESET:
16558b8a9b1dSJustin T. Gibbs 	case CAM_BDR_SENT:
16563393f8daSKenneth D. Merry 		/*
16573393f8daSKenneth D. Merry 		 * Commands that repeatedly timeout and cause these
16583393f8daSKenneth D. Merry 		 * kinds of error recovery actions, should return
16593393f8daSKenneth D. Merry 		 * CAM_CMD_TIMEOUT, which allows us to safely assume
16603393f8daSKenneth D. Merry 		 * that this command was an innocent bystander to
16613393f8daSKenneth D. Merry 		 * these events and should be unconditionally
16623393f8daSKenneth D. Merry 		 * retried.
16633393f8daSKenneth D. Merry 		 */
166496d333b7SMatt Jacob 		if (bootverbose && printed == 0) {
166596d333b7SMatt Jacob 			xpt_print_path(ccb->ccb_h.path);
166696d333b7SMatt Jacob 			if (status == CAM_BDR_SENT)
166796d333b7SMatt Jacob 				printf("Bus Device Reset sent\n");
166896d333b7SMatt Jacob 			else
166996d333b7SMatt Jacob 				printf("Bus Reset issued\n");
167096d333b7SMatt Jacob 			printed++;
167196d333b7SMatt Jacob 		}
16723393f8daSKenneth D. Merry 		/* FALLTHROUGH */
16738b8a9b1dSJustin T. Gibbs 	case CAM_REQUEUE_REQ:
16743393f8daSKenneth D. Merry 		/* Unconditional requeue */
16758b8a9b1dSJustin T. Gibbs 		error = ERESTART;
167696d333b7SMatt Jacob 		if (bootverbose && printed == 0) {
167796d333b7SMatt Jacob 			xpt_print_path(ccb->ccb_h.path);
167896d333b7SMatt Jacob 			printf("Request Requeued\n");
167996d333b7SMatt Jacob 			printed++;
168096d333b7SMatt Jacob 		}
16818b8a9b1dSJustin T. Gibbs 		break;
16828b8a9b1dSJustin T. Gibbs 	case CAM_RESRC_UNAVAIL:
16838b8a9b1dSJustin T. Gibbs 	case CAM_BUSY:
16848b8a9b1dSJustin T. Gibbs 		/* timeout??? */
16858b8a9b1dSJustin T. Gibbs 	default:
16868b8a9b1dSJustin T. Gibbs 		/* decrement the number of retries */
16873393f8daSKenneth D. Merry 		if (ccb->ccb_h.retry_count > 0) {
16888b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.retry_count--;
16898b8a9b1dSJustin T. Gibbs 			error = ERESTART;
169096d333b7SMatt Jacob 			if (bootverbose && printed == 0) {
169196d333b7SMatt Jacob 				xpt_print_path(ccb->ccb_h.path);
169296d333b7SMatt Jacob 				printf("CAM Status 0x%x\n", status);
169396d333b7SMatt Jacob 				printed++;
169496d333b7SMatt Jacob 			}
16958b8a9b1dSJustin T. Gibbs 		} else {
16968b8a9b1dSJustin T. Gibbs 			error = EIO;
16973393f8daSKenneth D. Merry 			action_string = "Retries Exhausted";
16988b8a9b1dSJustin T. Gibbs 		}
16998b8a9b1dSJustin T. Gibbs 		break;
17008b8a9b1dSJustin T. Gibbs 	}
17018b8a9b1dSJustin T. Gibbs 
17028b8a9b1dSJustin T. Gibbs 	/* Attempt a retry */
17038b8a9b1dSJustin T. Gibbs 	if (error == ERESTART || error == 0) {
17048b8a9b1dSJustin T. Gibbs 		if (frozen != 0)
17058b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
17068b8a9b1dSJustin T. Gibbs 
17073393f8daSKenneth D. Merry 		if (error == ERESTART) {
17083393f8daSKenneth D. Merry 			action_string = "Retrying Command";
17098b8a9b1dSJustin T. Gibbs 			xpt_action(ccb);
17103393f8daSKenneth D. Merry 		}
17118b8a9b1dSJustin T. Gibbs 
17123393f8daSKenneth D. Merry 		if (frozen != 0)
17138b8a9b1dSJustin T. Gibbs 			cam_release_devq(ccb->ccb_h.path,
17148b8a9b1dSJustin T. Gibbs 					 relsim_flags,
17158b8a9b1dSJustin T. Gibbs 					 openings,
17168b8a9b1dSJustin T. Gibbs 					 timeout,
17178b8a9b1dSJustin T. Gibbs 					 /*getcount_only*/0);
17188b8a9b1dSJustin T. Gibbs 	}
17198b8a9b1dSJustin T. Gibbs 
172034707c9fSMatt Jacob 	/*
172134707c9fSMatt Jacob 	 * If we have and error and are booting verbosely, whine
172234707c9fSMatt Jacob 	 * *unless* this was a non-retryable selection timeout.
172334707c9fSMatt Jacob 	 */
172434707c9fSMatt Jacob 	if (error != 0 && bootverbose &&
172534707c9fSMatt Jacob 	    !(status == CAM_SEL_TIMEOUT && (camflags & CAM_RETRY_SELTO) == 0)) {
172634707c9fSMatt Jacob 
17273393f8daSKenneth D. Merry 
17283393f8daSKenneth D. Merry 		if (action_string == NULL)
17293393f8daSKenneth D. Merry 			action_string = "Unretryable Error";
17303393f8daSKenneth D. Merry 		if (error != ERESTART) {
17313393f8daSKenneth D. Merry 			xpt_print_path(ccb->ccb_h.path);
17323393f8daSKenneth D. Merry 			printf("error %d\n", error);
17333393f8daSKenneth D. Merry 		}
17343393f8daSKenneth D. Merry 		xpt_print_path(ccb->ccb_h.path);
17353393f8daSKenneth D. Merry 		printf("%s\n", action_string);
17363393f8daSKenneth D. Merry 	}
17378b8a9b1dSJustin T. Gibbs 
17388b8a9b1dSJustin T. Gibbs 	return (error);
17398b8a9b1dSJustin T. Gibbs }
1740