xref: /freebsd/sys/cam/cam_periph.c (revision 1ffe58516c27eb9321539a1b98bbde8d8fa5dc9e)
1898b0535SWarner Losh /*-
28b8a9b1dSJustin T. Gibbs  * Common functions for CAM "type" (peripheral) drivers.
38b8a9b1dSJustin T. Gibbs  *
48b8a9b1dSJustin T. Gibbs  * Copyright (c) 1997, 1998 Justin T. Gibbs.
5501468a5SKenneth D. Merry  * Copyright (c) 1997, 1998, 1999, 2000 Kenneth D. Merry.
68b8a9b1dSJustin T. Gibbs  * All rights reserved.
78b8a9b1dSJustin T. Gibbs  *
88b8a9b1dSJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
98b8a9b1dSJustin T. Gibbs  * modification, are permitted provided that the following conditions
108b8a9b1dSJustin T. Gibbs  * are met:
118b8a9b1dSJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
128b8a9b1dSJustin T. Gibbs  *    notice, this list of conditions, and the following disclaimer,
138b8a9b1dSJustin T. Gibbs  *    without modification, immediately at the beginning of the file.
148b8a9b1dSJustin T. Gibbs  * 2. The name of the author may not be used to endorse or promote products
158b8a9b1dSJustin T. Gibbs  *    derived from this software without specific prior written permission.
168b8a9b1dSJustin T. Gibbs  *
178b8a9b1dSJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
188b8a9b1dSJustin T. Gibbs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
198b8a9b1dSJustin T. Gibbs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
208b8a9b1dSJustin T. Gibbs  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
218b8a9b1dSJustin T. Gibbs  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
228b8a9b1dSJustin T. Gibbs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
238b8a9b1dSJustin T. Gibbs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
248b8a9b1dSJustin T. Gibbs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
258b8a9b1dSJustin T. Gibbs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
268b8a9b1dSJustin T. Gibbs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
278b8a9b1dSJustin T. Gibbs  * SUCH DAMAGE.
288b8a9b1dSJustin T. Gibbs  */
298b8a9b1dSJustin T. Gibbs 
309c963d87SDavid E. O'Brien #include <sys/cdefs.h>
319c963d87SDavid E. O'Brien __FBSDID("$FreeBSD$");
329c963d87SDavid E. O'Brien 
338b8a9b1dSJustin T. Gibbs #include <sys/param.h>
348b8a9b1dSJustin T. Gibbs #include <sys/systm.h>
358b8a9b1dSJustin T. Gibbs #include <sys/types.h>
368b8a9b1dSJustin T. Gibbs #include <sys/malloc.h>
37362abc44STai-hwa Liang #include <sys/kernel.h>
389626b608SPoul-Henning Kamp #include <sys/bio.h>
39f34fa851SJohn Baldwin #include <sys/lock.h>
40f34fa851SJohn Baldwin #include <sys/mutex.h>
418b8a9b1dSJustin T. Gibbs #include <sys/buf.h>
428b8a9b1dSJustin T. Gibbs #include <sys/proc.h>
438b8a9b1dSJustin T. Gibbs #include <sys/devicestat.h>
4475f51904SPeter Wemm #include <sys/bus.h>
453501942bSJustin T. Gibbs #include <sys/sbuf.h>
468b8a9b1dSJustin T. Gibbs #include <vm/vm.h>
478b8a9b1dSJustin T. Gibbs #include <vm/vm_extern.h>
488b8a9b1dSJustin T. Gibbs 
498b8a9b1dSJustin T. Gibbs #include <cam/cam.h>
508b8a9b1dSJustin T. Gibbs #include <cam/cam_ccb.h>
5152c9ce25SScott Long #include <cam/cam_queue.h>
528b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt_periph.h>
538b8a9b1dSJustin T. Gibbs #include <cam/cam_periph.h>
548b8a9b1dSJustin T. Gibbs #include <cam/cam_debug.h>
552b83592fSScott Long #include <cam/cam_sim.h>
568b8a9b1dSJustin T. Gibbs 
578b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_all.h>
588b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_message.h>
598b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_pass.h>
608b8a9b1dSJustin T. Gibbs 
618b8a9b1dSJustin T. Gibbs static	u_int		camperiphnextunit(struct periph_driver *p_drv,
62501468a5SKenneth D. Merry 					  u_int newunit, int wired,
63501468a5SKenneth D. Merry 					  path_id_t pathid, target_id_t target,
64501468a5SKenneth D. Merry 					  lun_id_t lun);
658b8a9b1dSJustin T. Gibbs static	u_int		camperiphunit(struct periph_driver *p_drv,
66501468a5SKenneth D. Merry 				      path_id_t pathid, target_id_t target,
67501468a5SKenneth D. Merry 				      lun_id_t lun);
688b8a9b1dSJustin T. Gibbs static	void		camperiphdone(struct cam_periph *periph,
698b8a9b1dSJustin T. Gibbs 					union ccb *done_ccb);
708b8a9b1dSJustin T. Gibbs static  void		camperiphfree(struct cam_periph *periph);
713393f8daSKenneth D. Merry static int		camperiphscsistatuserror(union ccb *ccb,
720191d9b3SAlexander Motin 					        union ccb **orig_ccb,
733393f8daSKenneth D. Merry 						 cam_flags camflags,
743393f8daSKenneth D. Merry 						 u_int32_t sense_flags,
753393f8daSKenneth D. Merry 						 int *openings,
763393f8daSKenneth D. Merry 						 u_int32_t *relsim_flags,
7783c5d981SAlexander Motin 						 u_int32_t *timeout,
780181d54bSAlexander Motin 						 u_int32_t  *action,
7983c5d981SAlexander Motin 						 const char **action_string);
803393f8daSKenneth D. Merry static	int		camperiphscsisenseerror(union ccb *ccb,
810191d9b3SAlexander Motin 					        union ccb **orig_ccb,
823393f8daSKenneth D. Merry 					        cam_flags camflags,
833393f8daSKenneth D. Merry 					        u_int32_t sense_flags,
843393f8daSKenneth D. Merry 					        int *openings,
853393f8daSKenneth D. Merry 					        u_int32_t *relsim_flags,
8683c5d981SAlexander Motin 					        u_int32_t *timeout,
870181d54bSAlexander Motin 					        u_int32_t *action,
8883c5d981SAlexander Motin 					        const char **action_string);
89a30ecd42SScott Long static void		cam_periph_devctl_notify(union ccb *ccb);
908b8a9b1dSJustin T. Gibbs 
910b7c27b9SPeter Wemm static int nperiph_drivers;
9283c5d981SAlexander Motin static int initialized = 0;
930b7c27b9SPeter Wemm struct periph_driver **periph_drivers;
940b7c27b9SPeter Wemm 
95d745c852SEd Schouten static MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers");
96362abc44STai-hwa Liang 
9773cf209fSMatt Jacob static int periph_selto_delay = 1000;
9873cf209fSMatt Jacob TUNABLE_INT("kern.cam.periph_selto_delay", &periph_selto_delay);
9973cf209fSMatt Jacob static int periph_noresrc_delay = 500;
10073cf209fSMatt Jacob TUNABLE_INT("kern.cam.periph_noresrc_delay", &periph_noresrc_delay);
10173cf209fSMatt Jacob static int periph_busy_delay = 500;
10273cf209fSMatt Jacob TUNABLE_INT("kern.cam.periph_busy_delay", &periph_busy_delay);
10373cf209fSMatt Jacob 
10473cf209fSMatt Jacob 
1050b7c27b9SPeter Wemm void
1060b7c27b9SPeter Wemm periphdriver_register(void *data)
1070b7c27b9SPeter Wemm {
10883c5d981SAlexander Motin 	struct periph_driver *drv = (struct periph_driver *)data;
1090b7c27b9SPeter Wemm 	struct periph_driver **newdrivers, **old;
1100b7c27b9SPeter Wemm 	int ndrivers;
1110b7c27b9SPeter Wemm 
1125719711fSEdward Tomasz Napierala again:
1130b7c27b9SPeter Wemm 	ndrivers = nperiph_drivers + 2;
1140dd50e9bSScott Long 	newdrivers = malloc(sizeof(*newdrivers) * ndrivers, M_CAMPERIPH,
1150dd50e9bSScott Long 			    M_WAITOK);
1165719711fSEdward Tomasz Napierala 	xpt_lock_buses();
1175719711fSEdward Tomasz Napierala 	if (ndrivers != nperiph_drivers + 2) {
1185719711fSEdward Tomasz Napierala 		/*
1195719711fSEdward Tomasz Napierala 		 * Lost race against itself; go around.
1205719711fSEdward Tomasz Napierala 		 */
1215719711fSEdward Tomasz Napierala 		xpt_unlock_buses();
1225719711fSEdward Tomasz Napierala 		free(newdrivers, M_CAMPERIPH);
1235719711fSEdward Tomasz Napierala 		goto again;
1245719711fSEdward Tomasz Napierala 	}
1250b7c27b9SPeter Wemm 	if (periph_drivers)
1260b7c27b9SPeter Wemm 		bcopy(periph_drivers, newdrivers,
127623db360SKenneth D. Merry 		      sizeof(*newdrivers) * nperiph_drivers);
12883c5d981SAlexander Motin 	newdrivers[nperiph_drivers] = drv;
1290b7c27b9SPeter Wemm 	newdrivers[nperiph_drivers + 1] = NULL;
1300b7c27b9SPeter Wemm 	old = periph_drivers;
1310b7c27b9SPeter Wemm 	periph_drivers = newdrivers;
1325719711fSEdward Tomasz Napierala 	nperiph_drivers++;
1335719711fSEdward Tomasz Napierala 	xpt_unlock_buses();
1340b7c27b9SPeter Wemm 	if (old)
1350dd50e9bSScott Long 		free(old, M_CAMPERIPH);
13683c5d981SAlexander Motin 	/* If driver marked as early or it is late now, initialize it. */
13783c5d981SAlexander Motin 	if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
13883c5d981SAlexander Motin 	    initialized > 1)
13983c5d981SAlexander Motin 		(*drv->init)();
14083c5d981SAlexander Motin }
14183c5d981SAlexander Motin 
14283c5d981SAlexander Motin void
14383c5d981SAlexander Motin periphdriver_init(int level)
14483c5d981SAlexander Motin {
14583c5d981SAlexander Motin 	int	i, early;
14683c5d981SAlexander Motin 
14783c5d981SAlexander Motin 	initialized = max(initialized, level);
14883c5d981SAlexander Motin 	for (i = 0; periph_drivers[i] != NULL; i++) {
14983c5d981SAlexander Motin 		early = (periph_drivers[i]->flags & CAM_PERIPH_DRV_EARLY) ? 1 : 2;
15083c5d981SAlexander Motin 		if (early == initialized)
15183c5d981SAlexander Motin 			(*periph_drivers[i]->init)();
15283c5d981SAlexander Motin 	}
1530b7c27b9SPeter Wemm }
1540b7c27b9SPeter Wemm 
1558b8a9b1dSJustin T. Gibbs cam_status
156ee9c90c7SKenneth D. Merry cam_periph_alloc(periph_ctor_t *periph_ctor,
157ee9c90c7SKenneth D. Merry 		 periph_oninv_t *periph_oninvalidate,
158ee9c90c7SKenneth D. Merry 		 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
159ee9c90c7SKenneth D. Merry 		 char *name, cam_periph_type type, struct cam_path *path,
160ee9c90c7SKenneth D. Merry 		 ac_callback_t *ac_callback, ac_code code, void *arg)
1618b8a9b1dSJustin T. Gibbs {
1628b8a9b1dSJustin T. Gibbs 	struct		periph_driver **p_drv;
1632b83592fSScott Long 	struct		cam_sim *sim;
1648b8a9b1dSJustin T. Gibbs 	struct		cam_periph *periph;
1658b8a9b1dSJustin T. Gibbs 	struct		cam_periph *cur_periph;
1668b8a9b1dSJustin T. Gibbs 	path_id_t	path_id;
1678b8a9b1dSJustin T. Gibbs 	target_id_t	target_id;
1688b8a9b1dSJustin T. Gibbs 	lun_id_t	lun_id;
1698b8a9b1dSJustin T. Gibbs 	cam_status	status;
1708b8a9b1dSJustin T. Gibbs 	u_int		init_level;
1718b8a9b1dSJustin T. Gibbs 
1728b8a9b1dSJustin T. Gibbs 	init_level = 0;
1738b8a9b1dSJustin T. Gibbs 	/*
1748b8a9b1dSJustin T. Gibbs 	 * Handle Hot-Plug scenarios.  If there is already a peripheral
1758b8a9b1dSJustin T. Gibbs 	 * of our type assigned to this path, we are likely waiting for
1768b8a9b1dSJustin T. Gibbs 	 * final close on an old, invalidated, peripheral.  If this is
1778b8a9b1dSJustin T. Gibbs 	 * the case, queue up a deferred call to the peripheral's async
17874c91ec5SJustin T. Gibbs 	 * handler.  If it looks like a mistaken re-allocation, complain.
1798b8a9b1dSJustin T. Gibbs 	 */
1808b8a9b1dSJustin T. Gibbs 	if ((periph = cam_periph_find(path, name)) != NULL) {
1818b8a9b1dSJustin T. Gibbs 
1828b8a9b1dSJustin T. Gibbs 		if ((periph->flags & CAM_PERIPH_INVALID) != 0
1838b8a9b1dSJustin T. Gibbs 		 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
1848b8a9b1dSJustin T. Gibbs 			periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
1858b8a9b1dSJustin T. Gibbs 			periph->deferred_callback = ac_callback;
1868b8a9b1dSJustin T. Gibbs 			periph->deferred_ac = code;
1878b8a9b1dSJustin T. Gibbs 			return (CAM_REQ_INPROG);
1888b8a9b1dSJustin T. Gibbs 		} else {
1898b8a9b1dSJustin T. Gibbs 			printf("cam_periph_alloc: attempt to re-allocate "
1908900f4b8SKenneth D. Merry 			       "valid device %s%d rejected flags %#x "
1918900f4b8SKenneth D. Merry 			       "refcount %d\n", periph->periph_name,
1928900f4b8SKenneth D. Merry 			       periph->unit_number, periph->flags,
1938900f4b8SKenneth D. Merry 			       periph->refcount);
1948b8a9b1dSJustin T. Gibbs 		}
1958b8a9b1dSJustin T. Gibbs 		return (CAM_REQ_INVALID);
1968b8a9b1dSJustin T. Gibbs 	}
1978b8a9b1dSJustin T. Gibbs 
198362abc44STai-hwa Liang 	periph = (struct cam_periph *)malloc(sizeof(*periph), M_CAMPERIPH,
1998900f4b8SKenneth D. Merry 					     M_NOWAIT|M_ZERO);
2008b8a9b1dSJustin T. Gibbs 
2018b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
2028b8a9b1dSJustin T. Gibbs 		return (CAM_RESRC_UNAVAIL);
2038b8a9b1dSJustin T. Gibbs 
2048b8a9b1dSJustin T. Gibbs 	init_level++;
2058b8a9b1dSJustin T. Gibbs 
2068b8a9b1dSJustin T. Gibbs 
2072b83592fSScott Long 	sim = xpt_path_sim(path);
2088b8a9b1dSJustin T. Gibbs 	path_id = xpt_path_path_id(path);
2098b8a9b1dSJustin T. Gibbs 	target_id = xpt_path_target_id(path);
2108b8a9b1dSJustin T. Gibbs 	lun_id = xpt_path_lun_id(path);
2118b8a9b1dSJustin T. Gibbs 	periph->periph_start = periph_start;
2128b8a9b1dSJustin T. Gibbs 	periph->periph_dtor = periph_dtor;
213ee9c90c7SKenneth D. Merry 	periph->periph_oninval = periph_oninvalidate;
2148b8a9b1dSJustin T. Gibbs 	periph->type = type;
2158b8a9b1dSJustin T. Gibbs 	periph->periph_name = name;
216227d67aaSAlexander Motin 	periph->scheduled_priority = CAM_PRIORITY_NONE;
2178b8a9b1dSJustin T. Gibbs 	periph->immediate_priority = CAM_PRIORITY_NONE;
218d38c0e53SAlexander Motin 	periph->refcount = 1;		/* Dropped by invalidation. */
2192b83592fSScott Long 	periph->sim = sim;
2208b8a9b1dSJustin T. Gibbs 	SLIST_INIT(&periph->ccb_list);
2218b8a9b1dSJustin T. Gibbs 	status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
2228b8a9b1dSJustin T. Gibbs 	if (status != CAM_REQ_CMP)
2238b8a9b1dSJustin T. Gibbs 		goto failure;
2248b8a9b1dSJustin T. Gibbs 	periph->path = path;
2258b8a9b1dSJustin T. Gibbs 
226f1e2546aSMatt Jacob 	xpt_lock_buses();
227f1e2546aSMatt Jacob 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
228f1e2546aSMatt Jacob 		if (strcmp((*p_drv)->driver_name, name) == 0)
229f1e2546aSMatt Jacob 			break;
230f1e2546aSMatt Jacob 	}
231f1e2546aSMatt Jacob 	if (*p_drv == NULL) {
232f1e2546aSMatt Jacob 		printf("cam_periph_alloc: invalid periph name '%s'\n", name);
2339e0d30e2SEdward Tomasz Napierala 		xpt_unlock_buses();
234f1e2546aSMatt Jacob 		xpt_free_path(periph->path);
235f1e2546aSMatt Jacob 		free(periph, M_CAMPERIPH);
236f1e2546aSMatt Jacob 		return (CAM_REQ_INVALID);
237f1e2546aSMatt Jacob 	}
238f1e2546aSMatt Jacob 	periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id);
2398b8a9b1dSJustin T. Gibbs 	cur_periph = TAILQ_FIRST(&(*p_drv)->units);
2408b8a9b1dSJustin T. Gibbs 	while (cur_periph != NULL
2418b8a9b1dSJustin T. Gibbs 	    && cur_periph->unit_number < periph->unit_number)
2428b8a9b1dSJustin T. Gibbs 		cur_periph = TAILQ_NEXT(cur_periph, unit_links);
243f1e2546aSMatt Jacob 	if (cur_periph != NULL) {
244f1e2546aSMatt Jacob 		KASSERT(cur_periph->unit_number != periph->unit_number, ("duplicate units on periph list"));
2458b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
246f1e2546aSMatt Jacob 	} else {
2478b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
2488b8a9b1dSJustin T. Gibbs 		(*p_drv)->generation++;
2498b8a9b1dSJustin T. Gibbs 	}
250f1e2546aSMatt Jacob 	xpt_unlock_buses();
251f1e2546aSMatt Jacob 
252f1e2546aSMatt Jacob 	init_level++;
253f1e2546aSMatt Jacob 
254f1e2546aSMatt Jacob 	status = xpt_add_periph(periph);
255f1e2546aSMatt Jacob 	if (status != CAM_REQ_CMP)
256f1e2546aSMatt Jacob 		goto failure;
2578b8a9b1dSJustin T. Gibbs 
2588b8a9b1dSJustin T. Gibbs 	init_level++;
25922c7d606SAlexander Motin 	CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph created\n"));
2608b8a9b1dSJustin T. Gibbs 
2618b8a9b1dSJustin T. Gibbs 	status = periph_ctor(periph, arg);
2628b8a9b1dSJustin T. Gibbs 
2638b8a9b1dSJustin T. Gibbs 	if (status == CAM_REQ_CMP)
2648b8a9b1dSJustin T. Gibbs 		init_level++;
2658b8a9b1dSJustin T. Gibbs 
2668b8a9b1dSJustin T. Gibbs failure:
2678b8a9b1dSJustin T. Gibbs 	switch (init_level) {
2688b8a9b1dSJustin T. Gibbs 	case 4:
2698b8a9b1dSJustin T. Gibbs 		/* Initialized successfully */
2708b8a9b1dSJustin T. Gibbs 		break;
2718b8a9b1dSJustin T. Gibbs 	case 3:
27222c7d606SAlexander Motin 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
273a29779e8SAlexander Motin 		xpt_remove_periph(periph);
2747379c88fSPoul-Henning Kamp 		/* FALLTHROUGH */
2758b8a9b1dSJustin T. Gibbs 	case 2:
276f1e2546aSMatt Jacob 		xpt_lock_buses();
277f1e2546aSMatt Jacob 		TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
278f1e2546aSMatt Jacob 		xpt_unlock_buses();
2798b8a9b1dSJustin T. Gibbs 		xpt_free_path(periph->path);
2807379c88fSPoul-Henning Kamp 		/* FALLTHROUGH */
2818b8a9b1dSJustin T. Gibbs 	case 1:
282362abc44STai-hwa Liang 		free(periph, M_CAMPERIPH);
2837379c88fSPoul-Henning Kamp 		/* FALLTHROUGH */
2848b8a9b1dSJustin T. Gibbs 	case 0:
2858b8a9b1dSJustin T. Gibbs 		/* No cleanup to perform. */
2868b8a9b1dSJustin T. Gibbs 		break;
2878b8a9b1dSJustin T. Gibbs 	default:
288905cb85dSKenneth D. Merry 		panic("%s: Unknown init level", __func__);
2898b8a9b1dSJustin T. Gibbs 	}
2908b8a9b1dSJustin T. Gibbs 	return(status);
2918b8a9b1dSJustin T. Gibbs }
2928b8a9b1dSJustin T. Gibbs 
2938b8a9b1dSJustin T. Gibbs /*
2948b8a9b1dSJustin T. Gibbs  * Find a peripheral structure with the specified path, target, lun,
2958b8a9b1dSJustin T. Gibbs  * and (optionally) type.  If the name is NULL, this function will return
2968b8a9b1dSJustin T. Gibbs  * the first peripheral driver that matches the specified path.
2978b8a9b1dSJustin T. Gibbs  */
2988b8a9b1dSJustin T. Gibbs struct cam_periph *
2998b8a9b1dSJustin T. Gibbs cam_periph_find(struct cam_path *path, char *name)
3008b8a9b1dSJustin T. Gibbs {
3018b8a9b1dSJustin T. Gibbs 	struct periph_driver **p_drv;
3028b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph;
3038b8a9b1dSJustin T. Gibbs 
30477dc25ccSScott Long 	xpt_lock_buses();
3050b7c27b9SPeter Wemm 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
3068b8a9b1dSJustin T. Gibbs 
3078b8a9b1dSJustin T. Gibbs 		if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
3088b8a9b1dSJustin T. Gibbs 			continue;
3098b8a9b1dSJustin T. Gibbs 
31037d40066SPoul-Henning Kamp 		TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
3118b8a9b1dSJustin T. Gibbs 			if (xpt_path_comp(periph->path, path) == 0) {
31277dc25ccSScott Long 				xpt_unlock_buses();
313227d67aaSAlexander Motin 				cam_periph_assert(periph, MA_OWNED);
3148b8a9b1dSJustin T. Gibbs 				return(periph);
3158b8a9b1dSJustin T. Gibbs 			}
3168b8a9b1dSJustin T. Gibbs 		}
31777dc25ccSScott Long 		if (name != NULL) {
31877dc25ccSScott Long 			xpt_unlock_buses();
3198b8a9b1dSJustin T. Gibbs 			return(NULL);
3208b8a9b1dSJustin T. Gibbs 		}
32177dc25ccSScott Long 	}
32277dc25ccSScott Long 	xpt_unlock_buses();
3238b8a9b1dSJustin T. Gibbs 	return(NULL);
3248b8a9b1dSJustin T. Gibbs }
3258b8a9b1dSJustin T. Gibbs 
3263501942bSJustin T. Gibbs /*
3278900f4b8SKenneth D. Merry  * Find peripheral driver instances attached to the specified path.
3283501942bSJustin T. Gibbs  */
3293501942bSJustin T. Gibbs int
3303501942bSJustin T. Gibbs cam_periph_list(struct cam_path *path, struct sbuf *sb)
3313501942bSJustin T. Gibbs {
3328900f4b8SKenneth D. Merry 	struct sbuf local_sb;
3333501942bSJustin T. Gibbs 	struct periph_driver **p_drv;
3343501942bSJustin T. Gibbs 	struct cam_periph *periph;
3353501942bSJustin T. Gibbs 	int count;
3368900f4b8SKenneth D. Merry 	int sbuf_alloc_len;
3373501942bSJustin T. Gibbs 
3388900f4b8SKenneth D. Merry 	sbuf_alloc_len = 16;
3398900f4b8SKenneth D. Merry retry:
3408900f4b8SKenneth D. Merry 	sbuf_new(&local_sb, NULL, sbuf_alloc_len, SBUF_FIXEDLEN);
3413501942bSJustin T. Gibbs 	count = 0;
3423501942bSJustin T. Gibbs 	xpt_lock_buses();
3433501942bSJustin T. Gibbs 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
3443501942bSJustin T. Gibbs 
3453501942bSJustin T. Gibbs 		TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
3463501942bSJustin T. Gibbs 			if (xpt_path_comp(periph->path, path) != 0)
3473501942bSJustin T. Gibbs 				continue;
3483501942bSJustin T. Gibbs 
3498900f4b8SKenneth D. Merry 			if (sbuf_len(&local_sb) != 0)
3508900f4b8SKenneth D. Merry 				sbuf_cat(&local_sb, ",");
3513501942bSJustin T. Gibbs 
3528900f4b8SKenneth D. Merry 			sbuf_printf(&local_sb, "%s%d", periph->periph_name,
3533501942bSJustin T. Gibbs 				    periph->unit_number);
3548900f4b8SKenneth D. Merry 
3558900f4b8SKenneth D. Merry 			if (sbuf_error(&local_sb) == ENOMEM) {
3568900f4b8SKenneth D. Merry 				sbuf_alloc_len *= 2;
3578900f4b8SKenneth D. Merry 				xpt_unlock_buses();
3588900f4b8SKenneth D. Merry 				sbuf_delete(&local_sb);
3598900f4b8SKenneth D. Merry 				goto retry;
3608900f4b8SKenneth D. Merry 			}
3613501942bSJustin T. Gibbs 			count++;
3623501942bSJustin T. Gibbs 		}
3633501942bSJustin T. Gibbs 	}
3643501942bSJustin T. Gibbs 	xpt_unlock_buses();
3658900f4b8SKenneth D. Merry 	sbuf_finish(&local_sb);
3668900f4b8SKenneth D. Merry 	sbuf_cpy(sb, sbuf_data(&local_sb));
3678900f4b8SKenneth D. Merry 	sbuf_delete(&local_sb);
3683501942bSJustin T. Gibbs 	return (count);
3693501942bSJustin T. Gibbs }
3703501942bSJustin T. Gibbs 
3718b8a9b1dSJustin T. Gibbs cam_status
3728b8a9b1dSJustin T. Gibbs cam_periph_acquire(struct cam_periph *periph)
3738b8a9b1dSJustin T. Gibbs {
3748900f4b8SKenneth D. Merry 	cam_status status;
3758b8a9b1dSJustin T. Gibbs 
3768900f4b8SKenneth D. Merry 	status = CAM_REQ_CMP_ERR;
3778b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
3788900f4b8SKenneth D. Merry 		return (status);
3798b8a9b1dSJustin T. Gibbs 
3802b83592fSScott Long 	xpt_lock_buses();
3818900f4b8SKenneth D. Merry 	if ((periph->flags & CAM_PERIPH_INVALID) == 0) {
3828b8a9b1dSJustin T. Gibbs 		periph->refcount++;
3838900f4b8SKenneth D. Merry 		status = CAM_REQ_CMP;
3848900f4b8SKenneth D. Merry 	}
3852b83592fSScott Long 	xpt_unlock_buses();
3868b8a9b1dSJustin T. Gibbs 
3878900f4b8SKenneth D. Merry 	return (status);
3888900f4b8SKenneth D. Merry }
3898900f4b8SKenneth D. Merry 
3908900f4b8SKenneth D. Merry void
391c33e4029SAlexander Motin cam_periph_doacquire(struct cam_periph *periph)
392c33e4029SAlexander Motin {
393c33e4029SAlexander Motin 
394c33e4029SAlexander Motin 	xpt_lock_buses();
395c33e4029SAlexander Motin 	KASSERT(periph->refcount >= 1,
396c33e4029SAlexander Motin 	    ("cam_periph_doacquire() with refcount == %d", periph->refcount));
397c33e4029SAlexander Motin 	periph->refcount++;
398c33e4029SAlexander Motin 	xpt_unlock_buses();
399c33e4029SAlexander Motin }
400c33e4029SAlexander Motin 
401c33e4029SAlexander Motin void
4028900f4b8SKenneth D. Merry cam_periph_release_locked_buses(struct cam_periph *periph)
4038900f4b8SKenneth D. Merry {
404dcdf6e74SAlexander Motin 
405227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
406dcdf6e74SAlexander Motin 	KASSERT(periph->refcount >= 1, ("periph->refcount >= 1"));
407d38c0e53SAlexander Motin 	if (--periph->refcount == 0)
4088900f4b8SKenneth D. Merry 		camperiphfree(periph);
4098900f4b8SKenneth D. Merry }
4108b8a9b1dSJustin T. Gibbs 
4118b8a9b1dSJustin T. Gibbs void
41224ebf566SEdward Tomasz Napierala cam_periph_release_locked(struct cam_periph *periph)
4138b8a9b1dSJustin T. Gibbs {
4148b8a9b1dSJustin T. Gibbs 
4158b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
4168b8a9b1dSJustin T. Gibbs 		return;
4178b8a9b1dSJustin T. Gibbs 
4182b83592fSScott Long 	xpt_lock_buses();
4198900f4b8SKenneth D. Merry 	cam_periph_release_locked_buses(periph);
4202b83592fSScott Long 	xpt_unlock_buses();
42124ebf566SEdward Tomasz Napierala }
4228b8a9b1dSJustin T. Gibbs 
42324ebf566SEdward Tomasz Napierala void
42424ebf566SEdward Tomasz Napierala cam_periph_release(struct cam_periph *periph)
42524ebf566SEdward Tomasz Napierala {
426227d67aaSAlexander Motin 	struct mtx *mtx;
42724ebf566SEdward Tomasz Napierala 
42824ebf566SEdward Tomasz Napierala 	if (periph == NULL)
42924ebf566SEdward Tomasz Napierala 		return;
43024ebf566SEdward Tomasz Napierala 
431227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_NOTOWNED);
432227d67aaSAlexander Motin 	mtx = cam_periph_mtx(periph);
433227d67aaSAlexander Motin 	mtx_lock(mtx);
43424ebf566SEdward Tomasz Napierala 	cam_periph_release_locked(periph);
435227d67aaSAlexander Motin 	mtx_unlock(mtx);
4368b8a9b1dSJustin T. Gibbs }
4378b8a9b1dSJustin T. Gibbs 
4382b83592fSScott Long int
4392b83592fSScott Long cam_periph_hold(struct cam_periph *periph, int priority)
4402b83592fSScott Long {
4412b83592fSScott Long 	int error;
4422b83592fSScott Long 
4432b83592fSScott Long 	/*
4442b83592fSScott Long 	 * Increment the reference count on the peripheral
4452b83592fSScott Long 	 * while we wait for our lock attempt to succeed
4462b83592fSScott Long 	 * to ensure the peripheral doesn't disappear out
4472b83592fSScott Long 	 * from user us while we sleep.
4482b83592fSScott Long 	 */
4492b83592fSScott Long 
4502b83592fSScott Long 	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
4512b83592fSScott Long 		return (ENXIO);
4522b83592fSScott Long 
453227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
4542b83592fSScott Long 	while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
4552b83592fSScott Long 		periph->flags |= CAM_PERIPH_LOCK_WANTED;
456227d67aaSAlexander Motin 		if ((error = cam_periph_sleep(periph, periph, priority,
4571fa738c2SJohn Baldwin 		    "caplck", 0)) != 0) {
45824ebf566SEdward Tomasz Napierala 			cam_periph_release_locked(periph);
4592b83592fSScott Long 			return (error);
4602b83592fSScott Long 		}
461aed9c88cSAlexander Motin 		if (periph->flags & CAM_PERIPH_INVALID) {
462aed9c88cSAlexander Motin 			cam_periph_release_locked(periph);
463aed9c88cSAlexander Motin 			return (ENXIO);
464aed9c88cSAlexander Motin 		}
4652b83592fSScott Long 	}
4662b83592fSScott Long 
4672b83592fSScott Long 	periph->flags |= CAM_PERIPH_LOCKED;
4682b83592fSScott Long 	return (0);
4692b83592fSScott Long }
4702b83592fSScott Long 
4712b83592fSScott Long void
4722b83592fSScott Long cam_periph_unhold(struct cam_periph *periph)
4732b83592fSScott Long {
4742b83592fSScott Long 
475227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
4762b83592fSScott Long 
4772b83592fSScott Long 	periph->flags &= ~CAM_PERIPH_LOCKED;
4782b83592fSScott Long 	if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
4792b83592fSScott Long 		periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
4802b83592fSScott Long 		wakeup(periph);
4812b83592fSScott Long 	}
4822b83592fSScott Long 
48324ebf566SEdward Tomasz Napierala 	cam_periph_release_locked(periph);
4842b83592fSScott Long }
4852b83592fSScott Long 
4868b8a9b1dSJustin T. Gibbs /*
4878b8a9b1dSJustin T. Gibbs  * Look for the next unit number that is not currently in use for this
4888b8a9b1dSJustin T. Gibbs  * peripheral type starting at "newunit".  Also exclude unit numbers that
4898b8a9b1dSJustin T. Gibbs  * are reserved by for future "hardwiring" unless we already know that this
4908b8a9b1dSJustin T. Gibbs  * is a potential wired device.  Only assume that the device is "wired" the
4918b8a9b1dSJustin T. Gibbs  * first time through the loop since after that we'll be looking at unit
4928b8a9b1dSJustin T. Gibbs  * numbers that did not match a wiring entry.
4938b8a9b1dSJustin T. Gibbs  */
4948b8a9b1dSJustin T. Gibbs static u_int
495501468a5SKenneth D. Merry camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired,
496501468a5SKenneth D. Merry 		  path_id_t pathid, target_id_t target, lun_id_t lun)
4978b8a9b1dSJustin T. Gibbs {
4988b8a9b1dSJustin T. Gibbs 	struct	cam_periph *periph;
4992398f0cdSPeter Wemm 	char	*periph_name;
5002398f0cdSPeter Wemm 	int	i, val, dunit, r;
5012398f0cdSPeter Wemm 	const char *dname, *strval;
5028b8a9b1dSJustin T. Gibbs 
5038b8a9b1dSJustin T. Gibbs 	periph_name = p_drv->driver_name;
5048b8a9b1dSJustin T. Gibbs 	for (;;newunit++) {
5058b8a9b1dSJustin T. Gibbs 
5068b8a9b1dSJustin T. Gibbs 		for (periph = TAILQ_FIRST(&p_drv->units);
5078b8a9b1dSJustin T. Gibbs 		     periph != NULL && periph->unit_number != newunit;
5088b8a9b1dSJustin T. Gibbs 		     periph = TAILQ_NEXT(periph, unit_links))
5098b8a9b1dSJustin T. Gibbs 			;
5108b8a9b1dSJustin T. Gibbs 
5118b8a9b1dSJustin T. Gibbs 		if (periph != NULL && periph->unit_number == newunit) {
5128b8a9b1dSJustin T. Gibbs 			if (wired != 0) {
513f0d9af51SMatt Jacob 				xpt_print(periph->path, "Duplicate Wired "
514f0d9af51SMatt Jacob 				    "Device entry!\n");
515f0d9af51SMatt Jacob 				xpt_print(periph->path, "Second device (%s "
516f0d9af51SMatt Jacob 				    "device at scbus%d target %d lun %d) will "
517f0d9af51SMatt Jacob 				    "not be wired\n", periph_name, pathid,
518f0d9af51SMatt Jacob 				    target, lun);
5198b8a9b1dSJustin T. Gibbs 				wired = 0;
5208b8a9b1dSJustin T. Gibbs 			}
5218b8a9b1dSJustin T. Gibbs 			continue;
5228b8a9b1dSJustin T. Gibbs 		}
52375f51904SPeter Wemm 		if (wired)
52475f51904SPeter Wemm 			break;
5258b8a9b1dSJustin T. Gibbs 
5268b8a9b1dSJustin T. Gibbs 		/*
5278b8a9b1dSJustin T. Gibbs 		 * Don't match entries like "da 4" as a wired down
5288b8a9b1dSJustin T. Gibbs 		 * device, but do match entries like "da 4 target 5"
5298b8a9b1dSJustin T. Gibbs 		 * or even "da 4 scbus 1".
5308b8a9b1dSJustin T. Gibbs 		 */
5312398f0cdSPeter Wemm 		i = 0;
5322398f0cdSPeter Wemm 		dname = periph_name;
5332398f0cdSPeter Wemm 		for (;;) {
5342398f0cdSPeter Wemm 			r = resource_find_dev(&i, dname, &dunit, NULL, NULL);
5352398f0cdSPeter Wemm 			if (r != 0)
5362398f0cdSPeter Wemm 				break;
53775f51904SPeter Wemm 			/* if no "target" and no specific scbus, skip */
53875f51904SPeter Wemm 			if (resource_int_value(dname, dunit, "target", &val) &&
53975f51904SPeter Wemm 			    (resource_string_value(dname, dunit, "at",&strval)||
54075f51904SPeter Wemm 			     strcmp(strval, "scbus") == 0))
54175f51904SPeter Wemm 				continue;
54275f51904SPeter Wemm 			if (newunit == dunit)
5438b8a9b1dSJustin T. Gibbs 				break;
5448b8a9b1dSJustin T. Gibbs 		}
5452398f0cdSPeter Wemm 		if (r != 0)
5468b8a9b1dSJustin T. Gibbs 			break;
5478b8a9b1dSJustin T. Gibbs 	}
5488b8a9b1dSJustin T. Gibbs 	return (newunit);
5498b8a9b1dSJustin T. Gibbs }
5508b8a9b1dSJustin T. Gibbs 
5518b8a9b1dSJustin T. Gibbs static u_int
5528b8a9b1dSJustin T. Gibbs camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
5538b8a9b1dSJustin T. Gibbs 	      target_id_t target, lun_id_t lun)
5548b8a9b1dSJustin T. Gibbs {
5558b8a9b1dSJustin T. Gibbs 	u_int	unit;
556c1b81613SPeter Wemm 	int	wired, i, val, dunit;
5572398f0cdSPeter Wemm 	const char *dname, *strval;
5582398f0cdSPeter Wemm 	char	pathbuf[32], *periph_name;
5598b8a9b1dSJustin T. Gibbs 
56075f51904SPeter Wemm 	periph_name = p_drv->driver_name;
56175f51904SPeter Wemm 	snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
562c1b81613SPeter Wemm 	unit = 0;
5632398f0cdSPeter Wemm 	i = 0;
5642398f0cdSPeter Wemm 	dname = periph_name;
565c1b81613SPeter Wemm 	for (wired = 0; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0;
566c1b81613SPeter Wemm 	     wired = 0) {
56775f51904SPeter Wemm 		if (resource_string_value(dname, dunit, "at", &strval) == 0) {
56875f51904SPeter Wemm 			if (strcmp(strval, pathbuf) != 0)
5698b8a9b1dSJustin T. Gibbs 				continue;
570c1b81613SPeter Wemm 			wired++;
5718b8a9b1dSJustin T. Gibbs 		}
57275f51904SPeter Wemm 		if (resource_int_value(dname, dunit, "target", &val) == 0) {
57375f51904SPeter Wemm 			if (val != target)
5748b8a9b1dSJustin T. Gibbs 				continue;
575c1b81613SPeter Wemm 			wired++;
5768b8a9b1dSJustin T. Gibbs 		}
57775f51904SPeter Wemm 		if (resource_int_value(dname, dunit, "lun", &val) == 0) {
57875f51904SPeter Wemm 			if (val != lun)
5798b8a9b1dSJustin T. Gibbs 				continue;
580c1b81613SPeter Wemm 			wired++;
5818b8a9b1dSJustin T. Gibbs 		}
582c1b81613SPeter Wemm 		if (wired != 0) {
58375f51904SPeter Wemm 			unit = dunit;
5848b8a9b1dSJustin T. Gibbs 			break;
5858b8a9b1dSJustin T. Gibbs 		}
5868b8a9b1dSJustin T. Gibbs 	}
5878b8a9b1dSJustin T. Gibbs 
5888b8a9b1dSJustin T. Gibbs 	/*
5898b8a9b1dSJustin T. Gibbs 	 * Either start from 0 looking for the next unit or from
59075f51904SPeter Wemm 	 * the unit number given in the resource config.  This way,
5918b8a9b1dSJustin T. Gibbs 	 * if we have wildcard matches, we don't return the same
5928b8a9b1dSJustin T. Gibbs 	 * unit number twice.
5938b8a9b1dSJustin T. Gibbs 	 */
594c1b81613SPeter Wemm 	unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun);
5958b8a9b1dSJustin T. Gibbs 
5968b8a9b1dSJustin T. Gibbs 	return (unit);
5978b8a9b1dSJustin T. Gibbs }
5988b8a9b1dSJustin T. Gibbs 
5998b8a9b1dSJustin T. Gibbs void
6008b8a9b1dSJustin T. Gibbs cam_periph_invalidate(struct cam_periph *periph)
6018b8a9b1dSJustin T. Gibbs {
6028b8a9b1dSJustin T. Gibbs 
603227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
604ee9c90c7SKenneth D. Merry 	/*
605ee9c90c7SKenneth D. Merry 	 * We only call this routine the first time a peripheral is
60677dc25ccSScott Long 	 * invalidated.
607ee9c90c7SKenneth D. Merry 	 */
608d38c0e53SAlexander Motin 	if ((periph->flags & CAM_PERIPH_INVALID) != 0)
609d38c0e53SAlexander Motin 		return;
610ee9c90c7SKenneth D. Merry 
611d38c0e53SAlexander Motin 	CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph invalidated\n"));
612de6a705eSMarius Strobl 	if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting)
6138d36a71bSAlexander Motin 		xpt_denounce_periph(periph);
6148b8a9b1dSJustin T. Gibbs 	periph->flags |= CAM_PERIPH_INVALID;
6158b8a9b1dSJustin T. Gibbs 	periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
616d38c0e53SAlexander Motin 	if (periph->periph_oninval != NULL)
617d38c0e53SAlexander Motin 		periph->periph_oninval(periph);
618d38c0e53SAlexander Motin 	cam_periph_release_locked(periph);
6198b8a9b1dSJustin T. Gibbs }
6208b8a9b1dSJustin T. Gibbs 
6218b8a9b1dSJustin T. Gibbs static void
6228b8a9b1dSJustin T. Gibbs camperiphfree(struct cam_periph *periph)
6238b8a9b1dSJustin T. Gibbs {
6248b8a9b1dSJustin T. Gibbs 	struct periph_driver **p_drv;
6258b8a9b1dSJustin T. Gibbs 
626227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
627227d67aaSAlexander Motin 	KASSERT(periph->periph_allocating == 0, ("%s%d: freed while allocating",
628227d67aaSAlexander Motin 	    periph->periph_name, periph->unit_number));
6290b7c27b9SPeter Wemm 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
6308b8a9b1dSJustin T. Gibbs 		if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
6318b8a9b1dSJustin T. Gibbs 			break;
6328b8a9b1dSJustin T. Gibbs 	}
633661658a6SScott Long 	if (*p_drv == NULL) {
634661658a6SScott Long 		printf("camperiphfree: attempt to free non-existant periph\n");
635661658a6SScott Long 		return;
636661658a6SScott Long 	}
6378b8a9b1dSJustin T. Gibbs 
638ea37f519SKenneth D. Merry 	/*
63933a38f74SKenneth D. Merry 	 * We need to set this flag before dropping the topology lock, to
64033a38f74SKenneth D. Merry 	 * let anyone who is traversing the list that this peripheral is
64133a38f74SKenneth D. Merry 	 * about to be freed, and there will be no more reference count
64233a38f74SKenneth D. Merry 	 * checks.
64333a38f74SKenneth D. Merry 	 */
64433a38f74SKenneth D. Merry 	periph->flags |= CAM_PERIPH_FREE;
64533a38f74SKenneth D. Merry 
64633a38f74SKenneth D. Merry 	/*
647ea37f519SKenneth D. Merry 	 * The peripheral destructor semantics dictate calling with only the
648ea37f519SKenneth D. Merry 	 * SIM mutex held.  Since it might sleep, it should not be called
649ea37f519SKenneth D. Merry 	 * with the topology lock held.
650ea37f519SKenneth D. Merry 	 */
651f3548746SScott Long 	xpt_unlock_buses();
6528b8a9b1dSJustin T. Gibbs 
653ea37f519SKenneth D. Merry 	/*
654ea37f519SKenneth D. Merry 	 * We need to call the peripheral destructor prior to removing the
655ea37f519SKenneth D. Merry 	 * peripheral from the list.  Otherwise, we risk running into a
656ea37f519SKenneth D. Merry 	 * scenario where the peripheral unit number may get reused
657ea37f519SKenneth D. Merry 	 * (because it has been removed from the list), but some resources
658ea37f519SKenneth D. Merry 	 * used by the peripheral are still hanging around.  In particular,
659ea37f519SKenneth D. Merry 	 * the devfs nodes used by some peripherals like the pass(4) driver
660ea37f519SKenneth D. Merry 	 * aren't fully cleaned up until the destructor is run.  If the
661ea37f519SKenneth D. Merry 	 * unit number is reused before the devfs instance is fully gone,
662ea37f519SKenneth D. Merry 	 * devfs will panic.
663ea37f519SKenneth D. Merry 	 */
664f3548746SScott Long 	if (periph->periph_dtor != NULL)
665f3548746SScott Long 		periph->periph_dtor(periph);
666ea37f519SKenneth D. Merry 
667ea37f519SKenneth D. Merry 	/*
668ea37f519SKenneth D. Merry 	 * The peripheral list is protected by the topology lock.
669ea37f519SKenneth D. Merry 	 */
670ea37f519SKenneth D. Merry 	xpt_lock_buses();
671ea37f519SKenneth D. Merry 
672ea37f519SKenneth D. Merry 	TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
673ea37f519SKenneth D. Merry 	(*p_drv)->generation++;
674ea37f519SKenneth D. Merry 
675a29779e8SAlexander Motin 	xpt_remove_periph(periph);
676ea37f519SKenneth D. Merry 
677ea37f519SKenneth D. Merry 	xpt_unlock_buses();
678de6a705eSMarius Strobl 	if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting)
6798d36a71bSAlexander Motin 		xpt_print(periph->path, "Periph destroyed\n");
680de6a705eSMarius Strobl 	else
68122c7d606SAlexander Motin 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
6828b8a9b1dSJustin T. Gibbs 
6838b8a9b1dSJustin T. Gibbs 	if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
6848b8a9b1dSJustin T. Gibbs 		union ccb ccb;
6858b8a9b1dSJustin T. Gibbs 		void *arg;
6868b8a9b1dSJustin T. Gibbs 
6878b8a9b1dSJustin T. Gibbs 		switch (periph->deferred_ac) {
6888b8a9b1dSJustin T. Gibbs 		case AC_FOUND_DEVICE:
6898b8a9b1dSJustin T. Gibbs 			ccb.ccb_h.func_code = XPT_GDEV_TYPE;
690bbfa4aa1SAlexander Motin 			xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
6918b8a9b1dSJustin T. Gibbs 			xpt_action(&ccb);
6928b8a9b1dSJustin T. Gibbs 			arg = &ccb;
6938b8a9b1dSJustin T. Gibbs 			break;
6948b8a9b1dSJustin T. Gibbs 		case AC_PATH_REGISTERED:
6958b8a9b1dSJustin T. Gibbs 			ccb.ccb_h.func_code = XPT_PATH_INQ;
696bbfa4aa1SAlexander Motin 			xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
6978b8a9b1dSJustin T. Gibbs 			xpt_action(&ccb);
6988b8a9b1dSJustin T. Gibbs 			arg = &ccb;
6998b8a9b1dSJustin T. Gibbs 			break;
7008b8a9b1dSJustin T. Gibbs 		default:
7018b8a9b1dSJustin T. Gibbs 			arg = NULL;
7028b8a9b1dSJustin T. Gibbs 			break;
7038b8a9b1dSJustin T. Gibbs 		}
7048b8a9b1dSJustin T. Gibbs 		periph->deferred_callback(NULL, periph->deferred_ac,
7058b8a9b1dSJustin T. Gibbs 					  periph->path, arg);
7068b8a9b1dSJustin T. Gibbs 	}
7078b8a9b1dSJustin T. Gibbs 	xpt_free_path(periph->path);
708362abc44STai-hwa Liang 	free(periph, M_CAMPERIPH);
709f3548746SScott Long 	xpt_lock_buses();
7108b8a9b1dSJustin T. Gibbs }
7118b8a9b1dSJustin T. Gibbs 
7128b8a9b1dSJustin T. Gibbs /*
7138b8a9b1dSJustin T. Gibbs  * Map user virtual pointers into kernel virtual address space, so we can
71495fbded6SScott Long  * access the memory.  This is now a generic function that centralizes most
71595fbded6SScott Long  * of the sanity checks on the data flags, if any.
71695fbded6SScott Long  * This also only works for up to MAXPHYS memory.  Since we use
7178b8a9b1dSJustin T. Gibbs  * buffers to map stuff in and out, we're limited to the buffer size.
7188b8a9b1dSJustin T. Gibbs  */
7198b8a9b1dSJustin T. Gibbs int
720de239312SAlexander Motin cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo,
721de239312SAlexander Motin     u_int maxmap)
7228b8a9b1dSJustin T. Gibbs {
7232d5c7e45SMatthew Dillon 	int numbufs, i, j;
72479d49a06SKenneth D. Merry 	int flags[CAM_PERIPH_MAXMAPS];
7258b8a9b1dSJustin T. Gibbs 	u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
7268b8a9b1dSJustin T. Gibbs 	u_int32_t lengths[CAM_PERIPH_MAXMAPS];
7278b8a9b1dSJustin T. Gibbs 	u_int32_t dirs[CAM_PERIPH_MAXMAPS];
7288b8a9b1dSJustin T. Gibbs 
729de239312SAlexander Motin 	if (maxmap == 0)
730de239312SAlexander Motin 		maxmap = DFLTPHYS;	/* traditional default */
731de239312SAlexander Motin 	else if (maxmap > MAXPHYS)
732de239312SAlexander Motin 		maxmap = MAXPHYS;	/* for safety */
7338b8a9b1dSJustin T. Gibbs 	switch(ccb->ccb_h.func_code) {
7348b8a9b1dSJustin T. Gibbs 	case XPT_DEV_MATCH:
7358b8a9b1dSJustin T. Gibbs 		if (ccb->cdm.match_buf_len == 0) {
7368b8a9b1dSJustin T. Gibbs 			printf("cam_periph_mapmem: invalid match buffer "
7378b8a9b1dSJustin T. Gibbs 			       "length 0\n");
7388b8a9b1dSJustin T. Gibbs 			return(EINVAL);
7398b8a9b1dSJustin T. Gibbs 		}
7408b8a9b1dSJustin T. Gibbs 		if (ccb->cdm.pattern_buf_len > 0) {
7418b8a9b1dSJustin T. Gibbs 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
7428b8a9b1dSJustin T. Gibbs 			lengths[0] = ccb->cdm.pattern_buf_len;
7438b8a9b1dSJustin T. Gibbs 			dirs[0] = CAM_DIR_OUT;
7448b8a9b1dSJustin T. Gibbs 			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
7458b8a9b1dSJustin T. Gibbs 			lengths[1] = ccb->cdm.match_buf_len;
7468b8a9b1dSJustin T. Gibbs 			dirs[1] = CAM_DIR_IN;
7478b8a9b1dSJustin T. Gibbs 			numbufs = 2;
7488b8a9b1dSJustin T. Gibbs 		} else {
7498b8a9b1dSJustin T. Gibbs 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
7508b8a9b1dSJustin T. Gibbs 			lengths[0] = ccb->cdm.match_buf_len;
7518b8a9b1dSJustin T. Gibbs 			dirs[0] = CAM_DIR_IN;
7528b8a9b1dSJustin T. Gibbs 			numbufs = 1;
7538b8a9b1dSJustin T. Gibbs 		}
75452c9ce25SScott Long 		/*
75552c9ce25SScott Long 		 * This request will not go to the hardware, no reason
75652c9ce25SScott Long 		 * to be so strict. vmapbuf() is able to map up to MAXPHYS.
75752c9ce25SScott Long 		 */
75852c9ce25SScott Long 		maxmap = MAXPHYS;
7598b8a9b1dSJustin T. Gibbs 		break;
7608b8a9b1dSJustin T. Gibbs 	case XPT_SCSI_IO:
76187cfaf0eSJustin T. Gibbs 	case XPT_CONT_TARGET_IO:
7628b8a9b1dSJustin T. Gibbs 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
7638b8a9b1dSJustin T. Gibbs 			return(0);
76495fbded6SScott Long 		if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
76595fbded6SScott Long 			return (EINVAL);
7668b8a9b1dSJustin T. Gibbs 		data_ptrs[0] = &ccb->csio.data_ptr;
76779d49a06SKenneth D. Merry 		lengths[0] = ccb->csio.dxfer_len;
7688b8a9b1dSJustin T. Gibbs 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
7698b8a9b1dSJustin T. Gibbs 		numbufs = 1;
7708b8a9b1dSJustin T. Gibbs 		break;
77152c9ce25SScott Long 	case XPT_ATA_IO:
77252c9ce25SScott Long 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
77352c9ce25SScott Long 			return(0);
77495fbded6SScott Long 		if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
77595fbded6SScott Long 			return (EINVAL);
77652c9ce25SScott Long 		data_ptrs[0] = &ccb->ataio.data_ptr;
77752c9ce25SScott Long 		lengths[0] = ccb->ataio.dxfer_len;
77852c9ce25SScott Long 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
77952c9ce25SScott Long 		numbufs = 1;
78052c9ce25SScott Long 		break;
78106e79492SKenneth D. Merry 	case XPT_SMP_IO:
78206e79492SKenneth D. Merry 		data_ptrs[0] = &ccb->smpio.smp_request;
78306e79492SKenneth D. Merry 		lengths[0] = ccb->smpio.smp_request_len;
78406e79492SKenneth D. Merry 		dirs[0] = CAM_DIR_OUT;
78506e79492SKenneth D. Merry 		data_ptrs[1] = &ccb->smpio.smp_response;
78606e79492SKenneth D. Merry 		lengths[1] = ccb->smpio.smp_response_len;
78706e79492SKenneth D. Merry 		dirs[1] = CAM_DIR_IN;
78806e79492SKenneth D. Merry 		numbufs = 2;
78906e79492SKenneth D. Merry 		break;
7903501942bSJustin T. Gibbs 	case XPT_DEV_ADVINFO:
7913501942bSJustin T. Gibbs 		if (ccb->cdai.bufsiz == 0)
7927c103ddeSKenneth D. Merry 			return (0);
7937c103ddeSKenneth D. Merry 
7943501942bSJustin T. Gibbs 		data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
7953501942bSJustin T. Gibbs 		lengths[0] = ccb->cdai.bufsiz;
79606e79492SKenneth D. Merry 		dirs[0] = CAM_DIR_IN;
79706e79492SKenneth D. Merry 		numbufs = 1;
7987c103ddeSKenneth D. Merry 
7997c103ddeSKenneth D. Merry 		/*
8007c103ddeSKenneth D. Merry 		 * This request will not go to the hardware, no reason
8017c103ddeSKenneth D. Merry 		 * to be so strict. vmapbuf() is able to map up to MAXPHYS.
8027c103ddeSKenneth D. Merry 		 */
8037c103ddeSKenneth D. Merry 		maxmap = MAXPHYS;
80406e79492SKenneth D. Merry 		break;
8058b8a9b1dSJustin T. Gibbs 	default:
8068b8a9b1dSJustin T. Gibbs 		return(EINVAL);
8078b8a9b1dSJustin T. Gibbs 		break; /* NOTREACHED */
8088b8a9b1dSJustin T. Gibbs 	}
8098b8a9b1dSJustin T. Gibbs 
8108b8a9b1dSJustin T. Gibbs 	/*
81179d49a06SKenneth D. Merry 	 * Check the transfer length and permissions first, so we don't
81279d49a06SKenneth D. Merry 	 * have to unmap any previously mapped buffers.
8138b8a9b1dSJustin T. Gibbs 	 */
8148b8a9b1dSJustin T. Gibbs 	for (i = 0; i < numbufs; i++) {
81579d49a06SKenneth D. Merry 
81679d49a06SKenneth D. Merry 		flags[i] = 0;
81779d49a06SKenneth D. Merry 
81879d49a06SKenneth D. Merry 		/*
81979d49a06SKenneth D. Merry 		 * The userland data pointer passed in may not be page
82079d49a06SKenneth D. Merry 		 * aligned.  vmapbuf() truncates the address to a page
82179d49a06SKenneth D. Merry 		 * boundary, so if the address isn't page aligned, we'll
82279d49a06SKenneth D. Merry 		 * need enough space for the given transfer length, plus
82379d49a06SKenneth D. Merry 		 * whatever extra space is necessary to make it to the page
82479d49a06SKenneth D. Merry 		 * boundary.
82579d49a06SKenneth D. Merry 		 */
82679d49a06SKenneth D. Merry 		if ((lengths[i] +
82752c9ce25SScott Long 		    (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > maxmap){
8286d7b539aSPeter Wemm 			printf("cam_periph_mapmem: attempt to map %lu bytes, "
82952c9ce25SScott Long 			       "which is greater than %lu\n",
8306d7b539aSPeter Wemm 			       (long)(lengths[i] +
8316d7b539aSPeter Wemm 			       (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)),
83252c9ce25SScott Long 			       (u_long)maxmap);
83379d49a06SKenneth D. Merry 			return(E2BIG);
83479d49a06SKenneth D. Merry 		}
8358b8a9b1dSJustin T. Gibbs 
836edd24ab7SKenneth D. Merry 		if (dirs[i] & CAM_DIR_OUT) {
83721144e3bSPoul-Henning Kamp 			flags[i] = BIO_WRITE;
8388b8a9b1dSJustin T. Gibbs 		}
8398b8a9b1dSJustin T. Gibbs 
840edd24ab7SKenneth D. Merry 		if (dirs[i] & CAM_DIR_IN) {
84121144e3bSPoul-Henning Kamp 			flags[i] = BIO_READ;
8428b8a9b1dSJustin T. Gibbs 		}
8438b8a9b1dSJustin T. Gibbs 
84479d49a06SKenneth D. Merry 	}
84579d49a06SKenneth D. Merry 
84679d49a06SKenneth D. Merry 	/*
84795fbded6SScott Long 	 * This keeps the the kernel stack of current thread from getting
84895fbded6SScott Long 	 * swapped.  In low-memory situations where the kernel stack might
84995fbded6SScott Long 	 * otherwise get swapped out, this holds it and allows the thread
85095fbded6SScott Long 	 * to make progress and release the kernel mapped pages sooner.
85195fbded6SScott Long 	 *
85279d49a06SKenneth D. Merry 	 * XXX KDM should I use P_NOSWAP instead?
85379d49a06SKenneth D. Merry 	 */
8540cbbb7bfSPeter Wemm 	PHOLD(curproc);
85579d49a06SKenneth D. Merry 
85679d49a06SKenneth D. Merry 	for (i = 0; i < numbufs; i++) {
8578b8a9b1dSJustin T. Gibbs 		/*
8588b8a9b1dSJustin T. Gibbs 		 * Get the buffer.
8598b8a9b1dSJustin T. Gibbs 		 */
8601c7c3c6aSMatthew Dillon 		mapinfo->bp[i] = getpbuf(NULL);
8618b8a9b1dSJustin T. Gibbs 
8628b8a9b1dSJustin T. Gibbs 		/* put our pointer in the data slot */
8638b8a9b1dSJustin T. Gibbs 		mapinfo->bp[i]->b_data = *data_ptrs[i];
8648b8a9b1dSJustin T. Gibbs 
865fade8dd7SJeff Roberson 		/* save the user's data address */
866fade8dd7SJeff Roberson 		mapinfo->bp[i]->b_caller1 = *data_ptrs[i];
867fade8dd7SJeff Roberson 
86852c9ce25SScott Long 		/* set the transfer length, we know it's < MAXPHYS */
8698b8a9b1dSJustin T. Gibbs 		mapinfo->bp[i]->b_bufsize = lengths[i];
8708b8a9b1dSJustin T. Gibbs 
87121144e3bSPoul-Henning Kamp 		/* set the direction */
87221144e3bSPoul-Henning Kamp 		mapinfo->bp[i]->b_iocmd = flags[i];
8738b8a9b1dSJustin T. Gibbs 
8742d5c7e45SMatthew Dillon 		/*
8752d5c7e45SMatthew Dillon 		 * Map the buffer into kernel memory.
8762d5c7e45SMatthew Dillon 		 *
8772d5c7e45SMatthew Dillon 		 * Note that useracc() alone is not a  sufficient test.
8782d5c7e45SMatthew Dillon 		 * vmapbuf() can still fail due to a smaller file mapped
8792d5c7e45SMatthew Dillon 		 * into a larger area of VM, or if userland races against
8802d5c7e45SMatthew Dillon 		 * vmapbuf() after the useracc() check.
8812d5c7e45SMatthew Dillon 		 */
882e81ff91eSKonstantin Belousov 		if (vmapbuf(mapinfo->bp[i], 1) < 0) {
8832d5c7e45SMatthew Dillon 			for (j = 0; j < i; ++j) {
884fade8dd7SJeff Roberson 				*data_ptrs[j] = mapinfo->bp[j]->b_caller1;
8858373f36dSAlan Cox 				vunmapbuf(mapinfo->bp[j]);
8862d5c7e45SMatthew Dillon 				relpbuf(mapinfo->bp[j], NULL);
8872d5c7e45SMatthew Dillon 			}
8888373f36dSAlan Cox 			relpbuf(mapinfo->bp[i], NULL);
8892d5c7e45SMatthew Dillon 			PRELE(curproc);
8902d5c7e45SMatthew Dillon 			return(EACCES);
8912d5c7e45SMatthew Dillon 		}
8928b8a9b1dSJustin T. Gibbs 
8938b8a9b1dSJustin T. Gibbs 		/* set our pointer to the new mapped area */
8948b8a9b1dSJustin T. Gibbs 		*data_ptrs[i] = mapinfo->bp[i]->b_data;
8958b8a9b1dSJustin T. Gibbs 
8968b8a9b1dSJustin T. Gibbs 		mapinfo->num_bufs_used++;
8978b8a9b1dSJustin T. Gibbs 	}
8988b8a9b1dSJustin T. Gibbs 
899a62525f3SMatt Jacob 	/*
900a62525f3SMatt Jacob 	 * Now that we've gotten this far, change ownership to the kernel
901a62525f3SMatt Jacob 	 * of the buffers so that we don't run afoul of returning to user
902a62525f3SMatt Jacob 	 * space with locks (on the buffer) held.
903a62525f3SMatt Jacob 	 */
904a62525f3SMatt Jacob 	for (i = 0; i < numbufs; i++) {
905a62525f3SMatt Jacob 		BUF_KERNPROC(mapinfo->bp[i]);
906a62525f3SMatt Jacob 	}
907a62525f3SMatt Jacob 
908a62525f3SMatt Jacob 
9098b8a9b1dSJustin T. Gibbs 	return(0);
9108b8a9b1dSJustin T. Gibbs }
9118b8a9b1dSJustin T. Gibbs 
9128b8a9b1dSJustin T. Gibbs /*
9138b8a9b1dSJustin T. Gibbs  * Unmap memory segments mapped into kernel virtual address space by
9148b8a9b1dSJustin T. Gibbs  * cam_periph_mapmem().
9158b8a9b1dSJustin T. Gibbs  */
9168b8a9b1dSJustin T. Gibbs void
9178b8a9b1dSJustin T. Gibbs cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
9188b8a9b1dSJustin T. Gibbs {
9198b8a9b1dSJustin T. Gibbs 	int numbufs, i;
9208b8a9b1dSJustin T. Gibbs 	u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
9218b8a9b1dSJustin T. Gibbs 
9228b8a9b1dSJustin T. Gibbs 	if (mapinfo->num_bufs_used <= 0) {
92395fbded6SScott Long 		/* nothing to free and the process wasn't held. */
9248b8a9b1dSJustin T. Gibbs 		return;
9258b8a9b1dSJustin T. Gibbs 	}
9268b8a9b1dSJustin T. Gibbs 
9278b8a9b1dSJustin T. Gibbs 	switch (ccb->ccb_h.func_code) {
9288b8a9b1dSJustin T. Gibbs 	case XPT_DEV_MATCH:
9298b8a9b1dSJustin T. Gibbs 		numbufs = min(mapinfo->num_bufs_used, 2);
9308b8a9b1dSJustin T. Gibbs 
9318b8a9b1dSJustin T. Gibbs 		if (numbufs == 1) {
9328b8a9b1dSJustin T. Gibbs 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
9338b8a9b1dSJustin T. Gibbs 		} else {
9348b8a9b1dSJustin T. Gibbs 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
9358b8a9b1dSJustin T. Gibbs 			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
9368b8a9b1dSJustin T. Gibbs 		}
9378b8a9b1dSJustin T. Gibbs 		break;
9388b8a9b1dSJustin T. Gibbs 	case XPT_SCSI_IO:
9399911ecf9SJustin T. Gibbs 	case XPT_CONT_TARGET_IO:
9408b8a9b1dSJustin T. Gibbs 		data_ptrs[0] = &ccb->csio.data_ptr;
9418b8a9b1dSJustin T. Gibbs 		numbufs = min(mapinfo->num_bufs_used, 1);
9428b8a9b1dSJustin T. Gibbs 		break;
94352c9ce25SScott Long 	case XPT_ATA_IO:
94452c9ce25SScott Long 		data_ptrs[0] = &ccb->ataio.data_ptr;
94552c9ce25SScott Long 		numbufs = min(mapinfo->num_bufs_used, 1);
94652c9ce25SScott Long 		break;
94706e79492SKenneth D. Merry 	case XPT_SMP_IO:
94806e79492SKenneth D. Merry 		numbufs = min(mapinfo->num_bufs_used, 2);
94906e79492SKenneth D. Merry 		data_ptrs[0] = &ccb->smpio.smp_request;
95006e79492SKenneth D. Merry 		data_ptrs[1] = &ccb->smpio.smp_response;
95106e79492SKenneth D. Merry 		break;
9523501942bSJustin T. Gibbs 	case XPT_DEV_ADVINFO:
95306e79492SKenneth D. Merry 		numbufs = min(mapinfo->num_bufs_used, 1);
9543501942bSJustin T. Gibbs 		data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
95506e79492SKenneth D. Merry 		break;
9568b8a9b1dSJustin T. Gibbs 	default:
9578b8a9b1dSJustin T. Gibbs 		/* allow ourselves to be swapped once again */
9580cbbb7bfSPeter Wemm 		PRELE(curproc);
9598b8a9b1dSJustin T. Gibbs 		return;
9608b8a9b1dSJustin T. Gibbs 		break; /* NOTREACHED */
9618b8a9b1dSJustin T. Gibbs 	}
9628b8a9b1dSJustin T. Gibbs 
9638b8a9b1dSJustin T. Gibbs 	for (i = 0; i < numbufs; i++) {
9648b8a9b1dSJustin T. Gibbs 		/* Set the user's pointer back to the original value */
965fade8dd7SJeff Roberson 		*data_ptrs[i] = mapinfo->bp[i]->b_caller1;
9668b8a9b1dSJustin T. Gibbs 
9678b8a9b1dSJustin T. Gibbs 		/* unmap the buffer */
9688b8a9b1dSJustin T. Gibbs 		vunmapbuf(mapinfo->bp[i]);
9698b8a9b1dSJustin T. Gibbs 
9708b8a9b1dSJustin T. Gibbs 		/* release the buffer */
9711c7c3c6aSMatthew Dillon 		relpbuf(mapinfo->bp[i], NULL);
9728b8a9b1dSJustin T. Gibbs 	}
9738b8a9b1dSJustin T. Gibbs 
9748b8a9b1dSJustin T. Gibbs 	/* allow ourselves to be swapped once again */
9750cbbb7bfSPeter Wemm 	PRELE(curproc);
9768b8a9b1dSJustin T. Gibbs }
9778b8a9b1dSJustin T. Gibbs 
9788b8a9b1dSJustin T. Gibbs void
9798b8a9b1dSJustin T. Gibbs cam_periph_ccbwait(union ccb *ccb)
9808b8a9b1dSJustin T. Gibbs {
9818b8a9b1dSJustin T. Gibbs 
9828b8a9b1dSJustin T. Gibbs 	if ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX)
9838b8a9b1dSJustin T. Gibbs 	 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG))
984227d67aaSAlexander Motin 		xpt_path_sleep(ccb->ccb_h.path, &ccb->ccb_h.cbfcnp, PRIBIO,
985227d67aaSAlexander Motin 		    "cbwait", 0);
9868b8a9b1dSJustin T. Gibbs }
9878b8a9b1dSJustin T. Gibbs 
9888b8a9b1dSJustin T. Gibbs int
989571e8e20SScott Long cam_periph_ioctl(struct cam_periph *periph, u_long cmd, caddr_t addr,
9908b8a9b1dSJustin T. Gibbs 		 int (*error_routine)(union ccb *ccb,
9918b8a9b1dSJustin T. Gibbs 				      cam_flags camflags,
9928b8a9b1dSJustin T. Gibbs 				      u_int32_t sense_flags))
9938b8a9b1dSJustin T. Gibbs {
9948b8a9b1dSJustin T. Gibbs 	union ccb 	     *ccb;
9958b8a9b1dSJustin T. Gibbs 	int 		     error;
9968b8a9b1dSJustin T. Gibbs 	int		     found;
9978b8a9b1dSJustin T. Gibbs 
9988b8a9b1dSJustin T. Gibbs 	error = found = 0;
9998b8a9b1dSJustin T. Gibbs 
10008b8a9b1dSJustin T. Gibbs 	switch(cmd){
10018b8a9b1dSJustin T. Gibbs 	case CAMGETPASSTHRU:
1002bbfa4aa1SAlexander Motin 		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
10038b8a9b1dSJustin T. Gibbs 		xpt_setup_ccb(&ccb->ccb_h,
10048b8a9b1dSJustin T. Gibbs 			      ccb->ccb_h.path,
1005bbfa4aa1SAlexander Motin 			      CAM_PRIORITY_NORMAL);
10068b8a9b1dSJustin T. Gibbs 		ccb->ccb_h.func_code = XPT_GDEVLIST;
10078b8a9b1dSJustin T. Gibbs 
10088b8a9b1dSJustin T. Gibbs 		/*
10098b8a9b1dSJustin T. Gibbs 		 * Basically, the point of this is that we go through
10108b8a9b1dSJustin T. Gibbs 		 * getting the list of devices, until we find a passthrough
10118b8a9b1dSJustin T. Gibbs 		 * device.  In the current version of the CAM code, the
10128b8a9b1dSJustin T. Gibbs 		 * only way to determine what type of device we're dealing
10138b8a9b1dSJustin T. Gibbs 		 * with is by its name.
10148b8a9b1dSJustin T. Gibbs 		 */
10158b8a9b1dSJustin T. Gibbs 		while (found == 0) {
10168b8a9b1dSJustin T. Gibbs 			ccb->cgdl.index = 0;
10178b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
10188b8a9b1dSJustin T. Gibbs 			while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
10198b8a9b1dSJustin T. Gibbs 
10208b8a9b1dSJustin T. Gibbs 				/* we want the next device in the list */
10218b8a9b1dSJustin T. Gibbs 				xpt_action(ccb);
10228b8a9b1dSJustin T. Gibbs 				if (strncmp(ccb->cgdl.periph_name,
10238b8a9b1dSJustin T. Gibbs 				    "pass", 4) == 0){
10248b8a9b1dSJustin T. Gibbs 					found = 1;
10258b8a9b1dSJustin T. Gibbs 					break;
10268b8a9b1dSJustin T. Gibbs 				}
10278b8a9b1dSJustin T. Gibbs 			}
10288b8a9b1dSJustin T. Gibbs 			if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
10298b8a9b1dSJustin T. Gibbs 			    (found == 0)) {
10308b8a9b1dSJustin T. Gibbs 				ccb->cgdl.periph_name[0] = '\0';
10318b8a9b1dSJustin T. Gibbs 				ccb->cgdl.unit_number = 0;
10328b8a9b1dSJustin T. Gibbs 				break;
10338b8a9b1dSJustin T. Gibbs 			}
10348b8a9b1dSJustin T. Gibbs 		}
10358b8a9b1dSJustin T. Gibbs 
10368b8a9b1dSJustin T. Gibbs 		/* copy the result back out */
10378b8a9b1dSJustin T. Gibbs 		bcopy(ccb, addr, sizeof(union ccb));
10388b8a9b1dSJustin T. Gibbs 
10398b8a9b1dSJustin T. Gibbs 		/* and release the ccb */
10408b8a9b1dSJustin T. Gibbs 		xpt_release_ccb(ccb);
10418b8a9b1dSJustin T. Gibbs 
10428b8a9b1dSJustin T. Gibbs 		break;
10438b8a9b1dSJustin T. Gibbs 	default:
10448b8a9b1dSJustin T. Gibbs 		error = ENOTTY;
10458b8a9b1dSJustin T. Gibbs 		break;
10468b8a9b1dSJustin T. Gibbs 	}
10478b8a9b1dSJustin T. Gibbs 	return(error);
10488b8a9b1dSJustin T. Gibbs }
10498b8a9b1dSJustin T. Gibbs 
1050227d67aaSAlexander Motin static void
1051227d67aaSAlexander Motin cam_periph_done(struct cam_periph *periph, union ccb *done_ccb)
1052227d67aaSAlexander Motin {
1053227d67aaSAlexander Motin 
1054227d67aaSAlexander Motin 	/* Caller will release the CCB */
1055227d67aaSAlexander Motin 	wakeup(&done_ccb->ccb_h.cbfcnp);
1056227d67aaSAlexander Motin }
1057227d67aaSAlexander Motin 
10588b8a9b1dSJustin T. Gibbs int
10598b8a9b1dSJustin T. Gibbs cam_periph_runccb(union ccb *ccb,
10608b8a9b1dSJustin T. Gibbs 		  int (*error_routine)(union ccb *ccb,
10618b8a9b1dSJustin T. Gibbs 				       cam_flags camflags,
10628b8a9b1dSJustin T. Gibbs 				       u_int32_t sense_flags),
10638b8a9b1dSJustin T. Gibbs 		  cam_flags camflags, u_int32_t sense_flags,
10648b8a9b1dSJustin T. Gibbs 		  struct devstat *ds)
10658b8a9b1dSJustin T. Gibbs {
1066fd8be18fSKenneth D. Merry 	struct bintime *starttime;
1067fd8be18fSKenneth D. Merry 	struct bintime ltime;
10688b8a9b1dSJustin T. Gibbs 	int error;
10698b8a9b1dSJustin T. Gibbs 
1070fd8be18fSKenneth D. Merry 	starttime = NULL;
1071227d67aaSAlexander Motin 	xpt_path_assert(ccb->ccb_h.path, MA_OWNED);
10728b8a9b1dSJustin T. Gibbs 
10738b8a9b1dSJustin T. Gibbs 	/*
10748b8a9b1dSJustin T. Gibbs 	 * If the user has supplied a stats structure, and if we understand
10758b8a9b1dSJustin T. Gibbs 	 * this particular type of ccb, record the transaction start.
10768b8a9b1dSJustin T. Gibbs 	 */
1077379ad358SAlexander Motin 	if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO ||
1078fd8be18fSKenneth D. Merry 	    ccb->ccb_h.func_code == XPT_ATA_IO)) {
1079fd8be18fSKenneth D. Merry 		starttime = &ltime;
1080fd8be18fSKenneth D. Merry 		binuptime(starttime);
1081fd8be18fSKenneth D. Merry 		devstat_start_transaction(ds, starttime);
1082fd8be18fSKenneth D. Merry 	}
10838b8a9b1dSJustin T. Gibbs 
1084227d67aaSAlexander Motin 	ccb->ccb_h.cbfcnp = cam_periph_done;
10858b8a9b1dSJustin T. Gibbs 	xpt_action(ccb);
10868b8a9b1dSJustin T. Gibbs 
10878b8a9b1dSJustin T. Gibbs 	do {
10888b8a9b1dSJustin T. Gibbs 		cam_periph_ccbwait(ccb);
10898b8a9b1dSJustin T. Gibbs 		if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
10908b8a9b1dSJustin T. Gibbs 			error = 0;
10918b8a9b1dSJustin T. Gibbs 		else if (error_routine != NULL)
10928b8a9b1dSJustin T. Gibbs 			error = (*error_routine)(ccb, camflags, sense_flags);
10938b8a9b1dSJustin T. Gibbs 		else
10948b8a9b1dSJustin T. Gibbs 			error = 0;
10958b8a9b1dSJustin T. Gibbs 
10968b8a9b1dSJustin T. Gibbs 	} while (error == ERESTART);
10978b8a9b1dSJustin T. Gibbs 
109883c5d981SAlexander Motin 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
10998b8a9b1dSJustin T. Gibbs 		cam_release_devq(ccb->ccb_h.path,
11008b8a9b1dSJustin T. Gibbs 				 /* relsim_flags */0,
11018b8a9b1dSJustin T. Gibbs 				 /* openings */0,
11028b8a9b1dSJustin T. Gibbs 				 /* timeout */0,
11038b8a9b1dSJustin T. Gibbs 				 /* getcount_only */ FALSE);
110483c5d981SAlexander Motin 		ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
110583c5d981SAlexander Motin 	}
11068b8a9b1dSJustin T. Gibbs 
1107379ad358SAlexander Motin 	if (ds != NULL) {
1108379ad358SAlexander Motin 		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
11098b8a9b1dSJustin T. Gibbs 			devstat_end_transaction(ds,
1110fd8be18fSKenneth D. Merry 					ccb->csio.dxfer_len - ccb->csio.resid,
1111379ad358SAlexander Motin 					ccb->csio.tag_action & 0x3,
11128b8a9b1dSJustin T. Gibbs 					((ccb->ccb_h.flags & CAM_DIR_MASK) ==
11138b8a9b1dSJustin T. Gibbs 					CAM_DIR_NONE) ?  DEVSTAT_NO_DATA :
11148b8a9b1dSJustin T. Gibbs 					(ccb->ccb_h.flags & CAM_DIR_OUT) ?
11158b8a9b1dSJustin T. Gibbs 					DEVSTAT_WRITE :
1116fd8be18fSKenneth D. Merry 					DEVSTAT_READ, NULL, starttime);
1117379ad358SAlexander Motin 		} else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1118379ad358SAlexander Motin 			devstat_end_transaction(ds,
1119fd8be18fSKenneth D. Merry 					ccb->ataio.dxfer_len - ccb->ataio.resid,
1120e4cc6558SWarner Losh 					0, /* Not used in ATA */
1121379ad358SAlexander Motin 					((ccb->ccb_h.flags & CAM_DIR_MASK) ==
1122379ad358SAlexander Motin 					CAM_DIR_NONE) ?  DEVSTAT_NO_DATA :
1123379ad358SAlexander Motin 					(ccb->ccb_h.flags & CAM_DIR_OUT) ?
1124379ad358SAlexander Motin 					DEVSTAT_WRITE :
1125fd8be18fSKenneth D. Merry 					DEVSTAT_READ, NULL, starttime);
1126379ad358SAlexander Motin 		}
1127379ad358SAlexander Motin 	}
11288b8a9b1dSJustin T. Gibbs 
11298b8a9b1dSJustin T. Gibbs 	return(error);
11308b8a9b1dSJustin T. Gibbs }
11318b8a9b1dSJustin T. Gibbs 
113287cfaf0eSJustin T. Gibbs void
113387cfaf0eSJustin T. Gibbs cam_freeze_devq(struct cam_path *path)
113487cfaf0eSJustin T. Gibbs {
1135cccf4220SAlexander Motin 	struct ccb_hdr ccb_h;
113687cfaf0eSJustin T. Gibbs 
11370d4f3c31SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_freeze_devq\n"));
1138cccf4220SAlexander Motin 	xpt_setup_ccb(&ccb_h, path, /*priority*/1);
1139cccf4220SAlexander Motin 	ccb_h.func_code = XPT_NOOP;
1140cccf4220SAlexander Motin 	ccb_h.flags = CAM_DEV_QFREEZE;
1141cccf4220SAlexander Motin 	xpt_action((union ccb *)&ccb_h);
114287cfaf0eSJustin T. Gibbs }
114387cfaf0eSJustin T. Gibbs 
11448b8a9b1dSJustin T. Gibbs u_int32_t
11458b8a9b1dSJustin T. Gibbs cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
114683c5d981SAlexander Motin 		 u_int32_t openings, u_int32_t arg,
11478b8a9b1dSJustin T. Gibbs 		 int getcount_only)
11488b8a9b1dSJustin T. Gibbs {
11498b8a9b1dSJustin T. Gibbs 	struct ccb_relsim crs;
11508b8a9b1dSJustin T. Gibbs 
11510d4f3c31SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_release_devq(%u, %u, %u, %d)\n",
11520d4f3c31SAlexander Motin 	    relsim_flags, openings, arg, getcount_only));
1153bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
11548b8a9b1dSJustin T. Gibbs 	crs.ccb_h.func_code = XPT_REL_SIMQ;
11558b8a9b1dSJustin T. Gibbs 	crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
11568b8a9b1dSJustin T. Gibbs 	crs.release_flags = relsim_flags;
11578b8a9b1dSJustin T. Gibbs 	crs.openings = openings;
115883c5d981SAlexander Motin 	crs.release_timeout = arg;
11598b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&crs);
11608b8a9b1dSJustin T. Gibbs 	return (crs.qfrozen_cnt);
11618b8a9b1dSJustin T. Gibbs }
11628b8a9b1dSJustin T. Gibbs 
11638b8a9b1dSJustin T. Gibbs #define saved_ccb_ptr ppriv_ptr0
116483c5d981SAlexander Motin static void
116583c5d981SAlexander Motin camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
116683c5d981SAlexander Motin {
11670191d9b3SAlexander Motin 	union ccb      *saved_ccb;
116883c5d981SAlexander Motin 	cam_status	status;
116983c5d981SAlexander Motin 	struct scsi_start_stop_unit *scsi_cmd;
1170e7493b28SAlexander Motin 	int    error_code, sense_key, asc, ascq;
117183c5d981SAlexander Motin 
11720191d9b3SAlexander Motin 	scsi_cmd = (struct scsi_start_stop_unit *)
11730191d9b3SAlexander Motin 	    &done_ccb->csio.cdb_io.cdb_bytes;
117483c5d981SAlexander Motin 	status = done_ccb->ccb_h.status;
11758b8a9b1dSJustin T. Gibbs 
11760191d9b3SAlexander Motin 	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1177e7493b28SAlexander Motin 		if (scsi_extract_sense_ccb(done_ccb,
1178e7493b28SAlexander Motin 		    &error_code, &sense_key, &asc, &ascq)) {
117965afe1f4SKenneth D. Merry 			/*
11808b8a9b1dSJustin T. Gibbs 			 * If the error is "invalid field in CDB",
11818b8a9b1dSJustin T. Gibbs 			 * and the load/eject flag is set, turn the
11828b8a9b1dSJustin T. Gibbs 			 * flag off and try again.  This is just in
11838b8a9b1dSJustin T. Gibbs 			 * case the drive in question barfs on the
11848b8a9b1dSJustin T. Gibbs 			 * load eject flag.  The CAM code should set
11858b8a9b1dSJustin T. Gibbs 			 * the load/eject flag by default for
11868b8a9b1dSJustin T. Gibbs 			 * removable media.
11878b8a9b1dSJustin T. Gibbs 			 */
11888b8a9b1dSJustin T. Gibbs 			if ((scsi_cmd->opcode == START_STOP_UNIT) &&
11898b8a9b1dSJustin T. Gibbs 			    ((scsi_cmd->how & SSS_LOEJ) != 0) &&
11900191d9b3SAlexander Motin 			     (asc == 0x24) && (ascq == 0x00)) {
11918b8a9b1dSJustin T. Gibbs 				scsi_cmd->how &= ~SSS_LOEJ;
11920191d9b3SAlexander Motin 				if (status & CAM_DEV_QFRZN) {
11930191d9b3SAlexander Motin 					cam_release_devq(done_ccb->ccb_h.path,
11940191d9b3SAlexander Motin 					    0, 0, 0, 0);
11950191d9b3SAlexander Motin 					done_ccb->ccb_h.status &=
11960191d9b3SAlexander Motin 					    ~CAM_DEV_QFRZN;
11970191d9b3SAlexander Motin 				}
11988b8a9b1dSJustin T. Gibbs 				xpt_action(done_ccb);
11990191d9b3SAlexander Motin 				goto out;
12000191d9b3SAlexander Motin 			}
12010191d9b3SAlexander Motin 		}
12020191d9b3SAlexander Motin 		if (cam_periph_error(done_ccb,
12030191d9b3SAlexander Motin 		    0, SF_RETRY_UA | SF_NO_PRINT, NULL) == ERESTART)
12040191d9b3SAlexander Motin 			goto out;
12050191d9b3SAlexander Motin 		if (done_ccb->ccb_h.status & CAM_DEV_QFRZN) {
12060191d9b3SAlexander Motin 			cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
12070191d9b3SAlexander Motin 			done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
12080191d9b3SAlexander Motin 		}
12090191d9b3SAlexander Motin 	} else {
12100191d9b3SAlexander Motin 		/*
12110191d9b3SAlexander Motin 		 * If we have successfully taken a device from the not
12120191d9b3SAlexander Motin 		 * ready to ready state, re-scan the device and re-get
12130191d9b3SAlexander Motin 		 * the inquiry information.  Many devices (mostly disks)
12140191d9b3SAlexander Motin 		 * don't properly report their inquiry information unless
12150191d9b3SAlexander Motin 		 * they are spun up.
12160191d9b3SAlexander Motin 		 */
12170191d9b3SAlexander Motin 		if (scsi_cmd->opcode == START_STOP_UNIT)
12180191d9b3SAlexander Motin 			xpt_async(AC_INQ_CHANGED, done_ccb->ccb_h.path, NULL);
12190191d9b3SAlexander Motin 	}
122065afe1f4SKenneth D. Merry 
12218b8a9b1dSJustin T. Gibbs 	/*
12220191d9b3SAlexander Motin 	 * Perform the final retry with the original CCB so that final
12230191d9b3SAlexander Motin 	 * error processing is performed by the owner of the CCB.
12248b8a9b1dSJustin T. Gibbs 	 */
12250191d9b3SAlexander Motin 	saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
122683c5d981SAlexander Motin 	bcopy(saved_ccb, done_ccb, sizeof(*done_ccb));
122783c5d981SAlexander Motin 	xpt_free_ccb(saved_ccb);
12280191d9b3SAlexander Motin 	if (done_ccb->ccb_h.cbfcnp != camperiphdone)
122960a899a0SKenneth D. Merry 		periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
12308b8a9b1dSJustin T. Gibbs 	xpt_action(done_ccb);
12318b8a9b1dSJustin T. Gibbs 
12320191d9b3SAlexander Motin out:
12330191d9b3SAlexander Motin 	/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
12340191d9b3SAlexander Motin 	cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1235d84c90a6SAlexander Motin }
12368b8a9b1dSJustin T. Gibbs 
12378b8a9b1dSJustin T. Gibbs /*
123887cfaf0eSJustin T. Gibbs  * Generic Async Event handler.  Peripheral drivers usually
123987cfaf0eSJustin T. Gibbs  * filter out the events that require personal attention,
124087cfaf0eSJustin T. Gibbs  * and leave the rest to this function.
124187cfaf0eSJustin T. Gibbs  */
124287cfaf0eSJustin T. Gibbs void
124387cfaf0eSJustin T. Gibbs cam_periph_async(struct cam_periph *periph, u_int32_t code,
124487cfaf0eSJustin T. Gibbs 		 struct cam_path *path, void *arg)
124587cfaf0eSJustin T. Gibbs {
124687cfaf0eSJustin T. Gibbs 	switch (code) {
124787cfaf0eSJustin T. Gibbs 	case AC_LOST_DEVICE:
124887cfaf0eSJustin T. Gibbs 		cam_periph_invalidate(periph);
124987cfaf0eSJustin T. Gibbs 		break;
125087cfaf0eSJustin T. Gibbs 	default:
125187cfaf0eSJustin T. Gibbs 		break;
125287cfaf0eSJustin T. Gibbs 	}
125387cfaf0eSJustin T. Gibbs }
125487cfaf0eSJustin T. Gibbs 
125587cfaf0eSJustin T. Gibbs void
125687cfaf0eSJustin T. Gibbs cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
125787cfaf0eSJustin T. Gibbs {
125887cfaf0eSJustin T. Gibbs 	struct ccb_getdevstats cgds;
125987cfaf0eSJustin T. Gibbs 
1260bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cgds.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
126187cfaf0eSJustin T. Gibbs 	cgds.ccb_h.func_code = XPT_GDEV_STATS;
126287cfaf0eSJustin T. Gibbs 	xpt_action((union ccb *)&cgds);
126387cfaf0eSJustin T. Gibbs 	cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
126487cfaf0eSJustin T. Gibbs }
126587cfaf0eSJustin T. Gibbs 
126687cfaf0eSJustin T. Gibbs void
126787cfaf0eSJustin T. Gibbs cam_periph_freeze_after_event(struct cam_periph *periph,
126887cfaf0eSJustin T. Gibbs 			      struct timeval* event_time, u_int duration_ms)
126987cfaf0eSJustin T. Gibbs {
127087cfaf0eSJustin T. Gibbs 	struct timeval delta;
127187cfaf0eSJustin T. Gibbs 	struct timeval duration_tv;
127287cfaf0eSJustin T. Gibbs 
1273f8ff57d2SAlexander Motin 	if (!timevalisset(event_time))
1274f8ff57d2SAlexander Motin 		return;
1275f8ff57d2SAlexander Motin 
127687cfaf0eSJustin T. Gibbs 	microtime(&delta);
127787cfaf0eSJustin T. Gibbs 	timevalsub(&delta, event_time);
127887cfaf0eSJustin T. Gibbs 	duration_tv.tv_sec = duration_ms / 1000;
127987cfaf0eSJustin T. Gibbs 	duration_tv.tv_usec = (duration_ms % 1000) * 1000;
128087cfaf0eSJustin T. Gibbs 	if (timevalcmp(&delta, &duration_tv, <)) {
128187cfaf0eSJustin T. Gibbs 		timevalsub(&duration_tv, &delta);
128287cfaf0eSJustin T. Gibbs 
128387cfaf0eSJustin T. Gibbs 		duration_ms = duration_tv.tv_sec * 1000;
128487cfaf0eSJustin T. Gibbs 		duration_ms += duration_tv.tv_usec / 1000;
128587cfaf0eSJustin T. Gibbs 		cam_freeze_devq(periph->path);
128687cfaf0eSJustin T. Gibbs 		cam_release_devq(periph->path,
128787cfaf0eSJustin T. Gibbs 				RELSIM_RELEASE_AFTER_TIMEOUT,
128887cfaf0eSJustin T. Gibbs 				/*reduction*/0,
128987cfaf0eSJustin T. Gibbs 				/*timeout*/duration_ms,
129087cfaf0eSJustin T. Gibbs 				/*getcount_only*/0);
129187cfaf0eSJustin T. Gibbs 	}
129287cfaf0eSJustin T. Gibbs 
129387cfaf0eSJustin T. Gibbs }
129487cfaf0eSJustin T. Gibbs 
12953393f8daSKenneth D. Merry static int
12960191d9b3SAlexander Motin camperiphscsistatuserror(union ccb *ccb, union ccb **orig_ccb,
12970191d9b3SAlexander Motin     cam_flags camflags, u_int32_t sense_flags,
12983393f8daSKenneth D. Merry     int *openings, u_int32_t *relsim_flags,
12990181d54bSAlexander Motin     u_int32_t *timeout, u_int32_t *action, const char **action_string)
13008b8a9b1dSJustin T. Gibbs {
13018b8a9b1dSJustin T. Gibbs 	int error;
13028b8a9b1dSJustin T. Gibbs 
13038b8a9b1dSJustin T. Gibbs 	switch (ccb->csio.scsi_status) {
13048b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_OK:
13058b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_COND_MET:
13068b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_INTERMED:
13078b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_INTERMED_COND_MET:
13088b8a9b1dSJustin T. Gibbs 		error = 0;
13098b8a9b1dSJustin T. Gibbs 		break;
13108b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_CMD_TERMINATED:
13118b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_CHECK_COND:
13120191d9b3SAlexander Motin 		error = camperiphscsisenseerror(ccb, orig_ccb,
13133393f8daSKenneth D. Merry 					        camflags,
13148b8a9b1dSJustin T. Gibbs 					        sense_flags,
13153393f8daSKenneth D. Merry 					        openings,
13163393f8daSKenneth D. Merry 					        relsim_flags,
131783c5d981SAlexander Motin 					        timeout,
13180181d54bSAlexander Motin 					        action,
131983c5d981SAlexander Motin 					        action_string);
13208b8a9b1dSJustin T. Gibbs 		break;
13218b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_QUEUE_FULL:
13228b8a9b1dSJustin T. Gibbs 	{
13238b8a9b1dSJustin T. Gibbs 		/* no decrement */
132482815562SJustin T. Gibbs 		struct ccb_getdevstats cgds;
13258b8a9b1dSJustin T. Gibbs 
13268b8a9b1dSJustin T. Gibbs 		/*
13278b8a9b1dSJustin T. Gibbs 		 * First off, find out what the current
13288b8a9b1dSJustin T. Gibbs 		 * transaction counts are.
13298b8a9b1dSJustin T. Gibbs 		 */
133082815562SJustin T. Gibbs 		xpt_setup_ccb(&cgds.ccb_h,
13318b8a9b1dSJustin T. Gibbs 			      ccb->ccb_h.path,
1332bbfa4aa1SAlexander Motin 			      CAM_PRIORITY_NORMAL);
133382815562SJustin T. Gibbs 		cgds.ccb_h.func_code = XPT_GDEV_STATS;
133482815562SJustin T. Gibbs 		xpt_action((union ccb *)&cgds);
13358b8a9b1dSJustin T. Gibbs 
13368b8a9b1dSJustin T. Gibbs 		/*
13378b8a9b1dSJustin T. Gibbs 		 * If we were the only transaction active, treat
13388b8a9b1dSJustin T. Gibbs 		 * the QUEUE FULL as if it were a BUSY condition.
13398b8a9b1dSJustin T. Gibbs 		 */
134082815562SJustin T. Gibbs 		if (cgds.dev_active != 0) {
134182815562SJustin T. Gibbs 			int total_openings;
134282815562SJustin T. Gibbs 
13438b8a9b1dSJustin T. Gibbs 			/*
13448b8a9b1dSJustin T. Gibbs 		 	 * Reduce the number of openings to
13458b8a9b1dSJustin T. Gibbs 			 * be 1 less than the amount it took
13468b8a9b1dSJustin T. Gibbs 			 * to get a queue full bounded by the
13478b8a9b1dSJustin T. Gibbs 			 * minimum allowed tag count for this
13488b8a9b1dSJustin T. Gibbs 			 * device.
13498b8a9b1dSJustin T. Gibbs 		 	 */
13503393f8daSKenneth D. Merry 			total_openings = cgds.dev_active + cgds.dev_openings;
13513393f8daSKenneth D. Merry 			*openings = cgds.dev_active;
13523393f8daSKenneth D. Merry 			if (*openings < cgds.mintags)
13533393f8daSKenneth D. Merry 				*openings = cgds.mintags;
13543393f8daSKenneth D. Merry 			if (*openings < total_openings)
13553393f8daSKenneth D. Merry 				*relsim_flags = RELSIM_ADJUST_OPENINGS;
13568b8a9b1dSJustin T. Gibbs 			else {
13578b8a9b1dSJustin T. Gibbs 				/*
13588b8a9b1dSJustin T. Gibbs 				 * Some devices report queue full for
13598b8a9b1dSJustin T. Gibbs 				 * temporary resource shortages.  For
13608b8a9b1dSJustin T. Gibbs 				 * this reason, we allow a minimum
13618b8a9b1dSJustin T. Gibbs 				 * tag count to be entered via a
13628b8a9b1dSJustin T. Gibbs 				 * quirk entry to prevent the queue
13638b8a9b1dSJustin T. Gibbs 				 * count on these devices from falling
13648b8a9b1dSJustin T. Gibbs 				 * to a pessimisticly low value.  We
13658b8a9b1dSJustin T. Gibbs 				 * still wait for the next successful
13668b8a9b1dSJustin T. Gibbs 				 * completion, however, before queueing
13678b8a9b1dSJustin T. Gibbs 				 * more transactions to the device.
13688b8a9b1dSJustin T. Gibbs 				 */
13693393f8daSKenneth D. Merry 				*relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
13708b8a9b1dSJustin T. Gibbs 			}
13713393f8daSKenneth D. Merry 			*timeout = 0;
13728b8a9b1dSJustin T. Gibbs 			error = ERESTART;
13730181d54bSAlexander Motin 			*action &= ~SSQ_PRINT_SENSE;
13748b8a9b1dSJustin T. Gibbs 			break;
13758b8a9b1dSJustin T. Gibbs 		}
13768b8a9b1dSJustin T. Gibbs 		/* FALLTHROUGH */
13778b8a9b1dSJustin T. Gibbs 	}
13788b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_BUSY:
13798b8a9b1dSJustin T. Gibbs 		/*
13808b8a9b1dSJustin T. Gibbs 		 * Restart the queue after either another
13818b8a9b1dSJustin T. Gibbs 		 * command completes or a 1 second timeout.
13828b8a9b1dSJustin T. Gibbs 		 */
1383174b32ceSAlexander Motin 		if ((sense_flags & SF_RETRY_BUSY) != 0 ||
1384174b32ceSAlexander Motin 		    (ccb->ccb_h.retry_count--) > 0) {
13858b8a9b1dSJustin T. Gibbs 			error = ERESTART;
13863393f8daSKenneth D. Merry 			*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
13878b8a9b1dSJustin T. Gibbs 				      | RELSIM_RELEASE_AFTER_CMDCMPLT;
13883393f8daSKenneth D. Merry 			*timeout = 1000;
1389af51b059SMatt Jacob 		} else {
1390af51b059SMatt Jacob 			error = EIO;
1391af51b059SMatt Jacob 		}
13928b8a9b1dSJustin T. Gibbs 		break;
13938b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_RESERV_CONFLICT:
13948b8a9b1dSJustin T. Gibbs 	default:
13958b8a9b1dSJustin T. Gibbs 		error = EIO;
13968b8a9b1dSJustin T. Gibbs 		break;
13978b8a9b1dSJustin T. Gibbs 	}
13983393f8daSKenneth D. Merry 	return (error);
13993393f8daSKenneth D. Merry }
14003393f8daSKenneth D. Merry 
14013393f8daSKenneth D. Merry static int
14020191d9b3SAlexander Motin camperiphscsisenseerror(union ccb *ccb, union ccb **orig,
14030191d9b3SAlexander Motin     cam_flags camflags, u_int32_t sense_flags,
14043393f8daSKenneth D. Merry     int *openings, u_int32_t *relsim_flags,
14050181d54bSAlexander Motin     u_int32_t *timeout, u_int32_t *action, const char **action_string)
14063393f8daSKenneth D. Merry {
14073393f8daSKenneth D. Merry 	struct cam_periph *periph;
140883c5d981SAlexander Motin 	union ccb *orig_ccb = ccb;
14090191d9b3SAlexander Motin 	int error, recoveryccb;
14103393f8daSKenneth D. Merry 
14113393f8daSKenneth D. Merry 	periph = xpt_path_periph(ccb->ccb_h.path);
14120191d9b3SAlexander Motin 	recoveryccb = (ccb->ccb_h.cbfcnp == camperiphdone);
14130191d9b3SAlexander Motin 	if ((periph->flags & CAM_PERIPH_RECOVERY_INPROG) && !recoveryccb) {
14143393f8daSKenneth D. Merry 		/*
14153393f8daSKenneth D. Merry 		 * If error recovery is already in progress, don't attempt
14163393f8daSKenneth D. Merry 		 * to process this error, but requeue it unconditionally
14173393f8daSKenneth D. Merry 		 * and attempt to process it once error recovery has
14183393f8daSKenneth D. Merry 		 * completed.  This failed command is probably related to
14193393f8daSKenneth D. Merry 		 * the error that caused the currently active error recovery
14203393f8daSKenneth D. Merry 		 * action so our  current recovery efforts should also
14213393f8daSKenneth D. Merry 		 * address this command.  Be aware that the error recovery
14223393f8daSKenneth D. Merry 		 * code assumes that only one recovery action is in progress
14233393f8daSKenneth D. Merry 		 * on a particular peripheral instance at any given time
14243393f8daSKenneth D. Merry 		 * (e.g. only one saved CCB for error recovery) so it is
14253393f8daSKenneth D. Merry 		 * imperitive that we don't violate this assumption.
14263393f8daSKenneth D. Merry 		 */
14273393f8daSKenneth D. Merry 		error = ERESTART;
14280181d54bSAlexander Motin 		*action &= ~SSQ_PRINT_SENSE;
14293393f8daSKenneth D. Merry 	} else {
14303393f8daSKenneth D. Merry 		scsi_sense_action err_action;
14313393f8daSKenneth D. Merry 		struct ccb_getdev cgd;
14323393f8daSKenneth D. Merry 
14333393f8daSKenneth D. Merry 		/*
14343393f8daSKenneth D. Merry 		 * Grab the inquiry data for this device.
14353393f8daSKenneth D. Merry 		 */
1436bbfa4aa1SAlexander Motin 		xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, CAM_PRIORITY_NORMAL);
14373393f8daSKenneth D. Merry 		cgd.ccb_h.func_code = XPT_GDEV_TYPE;
14383393f8daSKenneth D. Merry 		xpt_action((union ccb *)&cgd);
14393393f8daSKenneth D. Merry 
1440e7493b28SAlexander Motin 		err_action = scsi_error_action(&ccb->csio, &cgd.inq_data,
14413393f8daSKenneth D. Merry 		    sense_flags);
14423393f8daSKenneth D. Merry 		error = err_action & SS_ERRMASK;
14433393f8daSKenneth D. Merry 
14443393f8daSKenneth D. Merry 		/*
14450191d9b3SAlexander Motin 		 * Do not autostart sequential access devices
14460191d9b3SAlexander Motin 		 * to avoid unexpected tape loading.
14470191d9b3SAlexander Motin 		 */
14480191d9b3SAlexander Motin 		if ((err_action & SS_MASK) == SS_START &&
14490191d9b3SAlexander Motin 		    SID_TYPE(&cgd.inq_data) == T_SEQUENTIAL) {
14500191d9b3SAlexander Motin 			*action_string = "Will not autostart a "
14510191d9b3SAlexander Motin 			    "sequential access device";
14520191d9b3SAlexander Motin 			goto sense_error_done;
14530191d9b3SAlexander Motin 		}
14540191d9b3SAlexander Motin 
14550191d9b3SAlexander Motin 		/*
14560191d9b3SAlexander Motin 		 * Avoid recovery recursion if recovery action is the same.
14570191d9b3SAlexander Motin 		 */
14580191d9b3SAlexander Motin 		if ((err_action & SS_MASK) >= SS_START && recoveryccb) {
14590191d9b3SAlexander Motin 			if (((err_action & SS_MASK) == SS_START &&
14600191d9b3SAlexander Motin 			     ccb->csio.cdb_io.cdb_bytes[0] == START_STOP_UNIT) ||
14610191d9b3SAlexander Motin 			    ((err_action & SS_MASK) == SS_TUR &&
14620191d9b3SAlexander Motin 			     (ccb->csio.cdb_io.cdb_bytes[0] == TEST_UNIT_READY))) {
14630191d9b3SAlexander Motin 				err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
14640191d9b3SAlexander Motin 				*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
14650191d9b3SAlexander Motin 				*timeout = 500;
14660191d9b3SAlexander Motin 			}
14670191d9b3SAlexander Motin 		}
14680191d9b3SAlexander Motin 
14690191d9b3SAlexander Motin 		/*
14703393f8daSKenneth D. Merry 		 * If the recovery action will consume a retry,
14713393f8daSKenneth D. Merry 		 * make sure we actually have retries available.
14723393f8daSKenneth D. Merry 		 */
14733393f8daSKenneth D. Merry 		if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
1474a07e846bSAlexander Motin 		 	if (ccb->ccb_h.retry_count > 0 &&
1475a07e846bSAlexander Motin 			    (periph->flags & CAM_PERIPH_INVALID) == 0)
14763393f8daSKenneth D. Merry 		 		ccb->ccb_h.retry_count--;
14773393f8daSKenneth D. Merry 			else {
147883c5d981SAlexander Motin 				*action_string = "Retries exhausted";
14793393f8daSKenneth D. Merry 				goto sense_error_done;
14803393f8daSKenneth D. Merry 			}
14813393f8daSKenneth D. Merry 		}
14823393f8daSKenneth D. Merry 
14833393f8daSKenneth D. Merry 		if ((err_action & SS_MASK) >= SS_START) {
14843393f8daSKenneth D. Merry 			/*
14853393f8daSKenneth D. Merry 			 * Do common portions of commands that
14863393f8daSKenneth D. Merry 			 * use recovery CCBs.
14873393f8daSKenneth D. Merry 			 */
148883c5d981SAlexander Motin 			orig_ccb = xpt_alloc_ccb_nowait();
148983c5d981SAlexander Motin 			if (orig_ccb == NULL) {
149083c5d981SAlexander Motin 				*action_string = "Can't allocate recovery CCB";
14913393f8daSKenneth D. Merry 				goto sense_error_done;
14923393f8daSKenneth D. Merry 			}
14931f1158b2SAlexander Motin 			/*
14941f1158b2SAlexander Motin 			 * Clear freeze flag for original request here, as
14951f1158b2SAlexander Motin 			 * this freeze will be dropped as part of ERESTART.
14961f1158b2SAlexander Motin 			 */
14971f1158b2SAlexander Motin 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
149883c5d981SAlexander Motin 			bcopy(ccb, orig_ccb, sizeof(*orig_ccb));
14993393f8daSKenneth D. Merry 		}
15003393f8daSKenneth D. Merry 
15013393f8daSKenneth D. Merry 		switch (err_action & SS_MASK) {
15023393f8daSKenneth D. Merry 		case SS_NOP:
150383c5d981SAlexander Motin 			*action_string = "No recovery action needed";
150400e54d14SKenneth D. Merry 			error = 0;
150500e54d14SKenneth D. Merry 			break;
15063393f8daSKenneth D. Merry 		case SS_RETRY:
150783c5d981SAlexander Motin 			*action_string = "Retrying command (per sense data)";
15083393f8daSKenneth D. Merry 			error = ERESTART;
15098b8a9b1dSJustin T. Gibbs 			break;
15103393f8daSKenneth D. Merry 		case SS_FAIL:
151183c5d981SAlexander Motin 			*action_string = "Unretryable error";
15123393f8daSKenneth D. Merry 			break;
15133393f8daSKenneth D. Merry 		case SS_START:
15143393f8daSKenneth D. Merry 		{
15153393f8daSKenneth D. Merry 			int le;
15163393f8daSKenneth D. Merry 
15173393f8daSKenneth D. Merry 			/*
15183393f8daSKenneth D. Merry 			 * Send a start unit command to the device, and
15193393f8daSKenneth D. Merry 			 * then retry the command.
15203393f8daSKenneth D. Merry 			 */
152183c5d981SAlexander Motin 			*action_string = "Attempting to start unit";
152283c5d981SAlexander Motin 			periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
15233393f8daSKenneth D. Merry 
15243393f8daSKenneth D. Merry 			/*
15253393f8daSKenneth D. Merry 			 * Check for removable media and set
15263393f8daSKenneth D. Merry 			 * load/eject flag appropriately.
15273393f8daSKenneth D. Merry 			 */
15283393f8daSKenneth D. Merry 			if (SID_IS_REMOVABLE(&cgd.inq_data))
15293393f8daSKenneth D. Merry 				le = TRUE;
15303393f8daSKenneth D. Merry 			else
15313393f8daSKenneth D. Merry 				le = FALSE;
15323393f8daSKenneth D. Merry 
15333393f8daSKenneth D. Merry 			scsi_start_stop(&ccb->csio,
15343393f8daSKenneth D. Merry 					/*retries*/1,
15353393f8daSKenneth D. Merry 					camperiphdone,
15363393f8daSKenneth D. Merry 					MSG_SIMPLE_Q_TAG,
15373393f8daSKenneth D. Merry 					/*start*/TRUE,
15383393f8daSKenneth D. Merry 					/*load/eject*/le,
15393393f8daSKenneth D. Merry 					/*immediate*/FALSE,
15403393f8daSKenneth D. Merry 					SSD_FULL_SIZE,
15413393f8daSKenneth D. Merry 					/*timeout*/50000);
15423393f8daSKenneth D. Merry 			break;
15433393f8daSKenneth D. Merry 		}
15443393f8daSKenneth D. Merry 		case SS_TUR:
15453393f8daSKenneth D. Merry 		{
15463393f8daSKenneth D. Merry 			/*
15473393f8daSKenneth D. Merry 			 * Send a Test Unit Ready to the device.
15483393f8daSKenneth D. Merry 			 * If the 'many' flag is set, we send 120
15493393f8daSKenneth D. Merry 			 * test unit ready commands, one every half
15503393f8daSKenneth D. Merry 			 * second.  Otherwise, we just send one TUR.
15513393f8daSKenneth D. Merry 			 * We only want to do this if the retry
15523393f8daSKenneth D. Merry 			 * count has not been exhausted.
15533393f8daSKenneth D. Merry 			 */
15543393f8daSKenneth D. Merry 			int retries;
15553393f8daSKenneth D. Merry 
15563393f8daSKenneth D. Merry 			if ((err_action & SSQ_MANY) != 0) {
155783c5d981SAlexander Motin 				*action_string = "Polling device for readiness";
15583393f8daSKenneth D. Merry 				retries = 120;
15593393f8daSKenneth D. Merry 			} else {
156083c5d981SAlexander Motin 				*action_string = "Testing device for readiness";
15613393f8daSKenneth D. Merry 				retries = 1;
15623393f8daSKenneth D. Merry 			}
156383c5d981SAlexander Motin 			periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
15643393f8daSKenneth D. Merry 			scsi_test_unit_ready(&ccb->csio,
15653393f8daSKenneth D. Merry 					     retries,
15663393f8daSKenneth D. Merry 					     camperiphdone,
15673393f8daSKenneth D. Merry 					     MSG_SIMPLE_Q_TAG,
15683393f8daSKenneth D. Merry 					     SSD_FULL_SIZE,
15693393f8daSKenneth D. Merry 					     /*timeout*/5000);
15703393f8daSKenneth D. Merry 
15713393f8daSKenneth D. Merry 			/*
15723393f8daSKenneth D. Merry 			 * Accomplish our 500ms delay by deferring
15733393f8daSKenneth D. Merry 			 * the release of our device queue appropriately.
15743393f8daSKenneth D. Merry 			 */
15753393f8daSKenneth D. Merry 			*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
15763393f8daSKenneth D. Merry 			*timeout = 500;
15773393f8daSKenneth D. Merry 			break;
15783393f8daSKenneth D. Merry 		}
15793393f8daSKenneth D. Merry 		default:
1580e3c29144SWarner Losh 			panic("Unhandled error action %x", err_action);
15813393f8daSKenneth D. Merry 		}
15823393f8daSKenneth D. Merry 
15833393f8daSKenneth D. Merry 		if ((err_action & SS_MASK) >= SS_START) {
15843393f8daSKenneth D. Merry 			/*
1585bbfa4aa1SAlexander Motin 			 * Drop the priority, so that the recovery
15863393f8daSKenneth D. Merry 			 * CCB is the first to execute.  Freeze the queue
15873393f8daSKenneth D. Merry 			 * after this command is sent so that we can
15883393f8daSKenneth D. Merry 			 * restore the old csio and have it queued in
15893393f8daSKenneth D. Merry 			 * the proper order before we release normal
15903393f8daSKenneth D. Merry 			 * transactions to the device.
15913393f8daSKenneth D. Merry 			 */
159283c5d981SAlexander Motin 			ccb->ccb_h.pinfo.priority--;
15933393f8daSKenneth D. Merry 			ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
159483c5d981SAlexander Motin 			ccb->ccb_h.saved_ccb_ptr = orig_ccb;
15953393f8daSKenneth D. Merry 			error = ERESTART;
15960191d9b3SAlexander Motin 			*orig = orig_ccb;
15973393f8daSKenneth D. Merry 		}
15983393f8daSKenneth D. Merry 
15993393f8daSKenneth D. Merry sense_error_done:
16000181d54bSAlexander Motin 		*action = err_action;
16013393f8daSKenneth D. Merry 	}
16023393f8daSKenneth D. Merry 	return (error);
16033393f8daSKenneth D. Merry }
16043393f8daSKenneth D. Merry 
16053393f8daSKenneth D. Merry /*
16063393f8daSKenneth D. Merry  * Generic error handler.  Peripheral drivers usually filter
1607*1ffe5851SPedro F. Giffuni  * out the errors that they handle in a unique manner, then
16083393f8daSKenneth D. Merry  * call this function.
16093393f8daSKenneth D. Merry  */
16103393f8daSKenneth D. Merry int
16113393f8daSKenneth D. Merry cam_periph_error(union ccb *ccb, cam_flags camflags,
16123393f8daSKenneth D. Merry 		 u_int32_t sense_flags, union ccb *save_ccb)
16133393f8daSKenneth D. Merry {
16140181d54bSAlexander Motin 	struct cam_path *newpath;
16150181d54bSAlexander Motin 	union ccb  *orig_ccb, *scan_ccb;
1616a07e846bSAlexander Motin 	struct cam_periph *periph;
16173393f8daSKenneth D. Merry 	const char *action_string;
16183393f8daSKenneth D. Merry 	cam_status  status;
1619a30ecd42SScott Long 	int	    frozen, error, openings, devctl_err;
16200181d54bSAlexander Motin 	u_int32_t   action, relsim_flags, timeout;
16213393f8daSKenneth D. Merry 
16220181d54bSAlexander Motin 	action = SSQ_PRINT_SENSE;
1623a07e846bSAlexander Motin 	periph = xpt_path_periph(ccb->ccb_h.path);
16243393f8daSKenneth D. Merry 	action_string = NULL;
16253393f8daSKenneth D. Merry 	status = ccb->ccb_h.status;
16263393f8daSKenneth D. Merry 	frozen = (status & CAM_DEV_QFRZN) != 0;
16273393f8daSKenneth D. Merry 	status &= CAM_STATUS_MASK;
1628a30ecd42SScott Long 	devctl_err = openings = relsim_flags = timeout = 0;
16290191d9b3SAlexander Motin 	orig_ccb = ccb;
16303393f8daSKenneth D. Merry 
1631a30ecd42SScott Long 	/* Filter the errors that should be reported via devctl */
1632a30ecd42SScott Long 	switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
1633a30ecd42SScott Long 	case CAM_CMD_TIMEOUT:
1634a30ecd42SScott Long 	case CAM_REQ_ABORTED:
1635a30ecd42SScott Long 	case CAM_REQ_CMP_ERR:
1636a30ecd42SScott Long 	case CAM_REQ_TERMIO:
1637a30ecd42SScott Long 	case CAM_UNREC_HBA_ERROR:
1638a30ecd42SScott Long 	case CAM_DATA_RUN_ERR:
1639a30ecd42SScott Long 	case CAM_SCSI_STATUS_ERROR:
1640a30ecd42SScott Long 	case CAM_ATA_STATUS_ERROR:
1641a30ecd42SScott Long 	case CAM_SMP_STATUS_ERROR:
1642a30ecd42SScott Long 		devctl_err++;
1643a30ecd42SScott Long 		break;
1644a30ecd42SScott Long 	default:
1645a30ecd42SScott Long 		break;
1646a30ecd42SScott Long 	}
1647a30ecd42SScott Long 
16483393f8daSKenneth D. Merry 	switch (status) {
16493393f8daSKenneth D. Merry 	case CAM_REQ_CMP:
16503393f8daSKenneth D. Merry 		error = 0;
16510181d54bSAlexander Motin 		action &= ~SSQ_PRINT_SENSE;
16523393f8daSKenneth D. Merry 		break;
16533393f8daSKenneth D. Merry 	case CAM_SCSI_STATUS_ERROR:
16540191d9b3SAlexander Motin 		error = camperiphscsistatuserror(ccb, &orig_ccb,
16550191d9b3SAlexander Motin 		    camflags, sense_flags, &openings, &relsim_flags,
16560181d54bSAlexander Motin 		    &timeout, &action, &action_string);
16573393f8daSKenneth D. Merry 		break;
16583393f8daSKenneth D. Merry 	case CAM_AUTOSENSE_FAIL:
1659c7ec4390SMatt Jacob 		error = EIO;	/* we have to kill the command */
1660c7ec4390SMatt Jacob 		break;
16618b8a9b1dSJustin T. Gibbs 	case CAM_UA_ABORT:
16628b8a9b1dSJustin T. Gibbs 	case CAM_UA_TERMIO:
16638b8a9b1dSJustin T. Gibbs 	case CAM_MSG_REJECT_REC:
16648b8a9b1dSJustin T. Gibbs 		/* XXX Don't know that these are correct */
16658b8a9b1dSJustin T. Gibbs 		error = EIO;
16668b8a9b1dSJustin T. Gibbs 		break;
16678b8a9b1dSJustin T. Gibbs 	case CAM_SEL_TIMEOUT:
16683393f8daSKenneth D. Merry 		if ((camflags & CAM_RETRY_SELTO) != 0) {
1669a07e846bSAlexander Motin 			if (ccb->ccb_h.retry_count > 0 &&
1670a07e846bSAlexander Motin 			    (periph->flags & CAM_PERIPH_INVALID) == 0) {
16713393f8daSKenneth D. Merry 				ccb->ccb_h.retry_count--;
16723393f8daSKenneth D. Merry 				error = ERESTART;
16733393f8daSKenneth D. Merry 
16743393f8daSKenneth D. Merry 				/*
167573cf209fSMatt Jacob 				 * Wait a bit to give the device
16763393f8daSKenneth D. Merry 				 * time to recover before we try again.
16773393f8daSKenneth D. Merry 				 */
16783393f8daSKenneth D. Merry 				relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
167973cf209fSMatt Jacob 				timeout = periph_selto_delay;
16803393f8daSKenneth D. Merry 				break;
16813393f8daSKenneth D. Merry 			}
1682a07e846bSAlexander Motin 			action_string = "Retries exhausted";
16833393f8daSKenneth D. Merry 		}
16848900f4b8SKenneth D. Merry 		/* FALLTHROUGH */
16858900f4b8SKenneth D. Merry 	case CAM_DEV_NOT_THERE:
16863393f8daSKenneth D. Merry 		error = ENXIO;
16870181d54bSAlexander Motin 		action = SSQ_LOST;
16888b8a9b1dSJustin T. Gibbs 		break;
16898b8a9b1dSJustin T. Gibbs 	case CAM_REQ_INVALID:
16908b8a9b1dSJustin T. Gibbs 	case CAM_PATH_INVALID:
16918b8a9b1dSJustin T. Gibbs 	case CAM_NO_HBA:
16928b8a9b1dSJustin T. Gibbs 	case CAM_PROVIDE_FAIL:
16938b8a9b1dSJustin T. Gibbs 	case CAM_REQ_TOO_BIG:
16949a014e6fSIan Dowse 	case CAM_LUN_INVALID:
16959a014e6fSIan Dowse 	case CAM_TID_INVALID:
1696357478a5SAlexander Motin 	case CAM_FUNC_NOTAVAIL:
16978b8a9b1dSJustin T. Gibbs 		error = EINVAL;
16988b8a9b1dSJustin T. Gibbs 		break;
16998b8a9b1dSJustin T. Gibbs 	case CAM_SCSI_BUS_RESET:
17008b8a9b1dSJustin T. Gibbs 	case CAM_BDR_SENT:
17013393f8daSKenneth D. Merry 		/*
17023393f8daSKenneth D. Merry 		 * Commands that repeatedly timeout and cause these
17033393f8daSKenneth D. Merry 		 * kinds of error recovery actions, should return
17043393f8daSKenneth D. Merry 		 * CAM_CMD_TIMEOUT, which allows us to safely assume
17053393f8daSKenneth D. Merry 		 * that this command was an innocent bystander to
17063393f8daSKenneth D. Merry 		 * these events and should be unconditionally
17073393f8daSKenneth D. Merry 		 * retried.
17083393f8daSKenneth D. Merry 		 */
17098b8a9b1dSJustin T. Gibbs 	case CAM_REQUEUE_REQ:
17100191d9b3SAlexander Motin 		/* Unconditional requeue if device is still there */
17110191d9b3SAlexander Motin 		if (periph->flags & CAM_PERIPH_INVALID) {
17120191d9b3SAlexander Motin 			action_string = "Periph was invalidated";
1713a07e846bSAlexander Motin 			error = EIO;
17140191d9b3SAlexander Motin 		} else if (sense_flags & SF_NO_RETRY) {
17150191d9b3SAlexander Motin 			error = EIO;
17160191d9b3SAlexander Motin 			action_string = "Retry was blocked";
171784c4de2dSAlexander Motin 		} else {
17180191d9b3SAlexander Motin 			error = ERESTART;
17190181d54bSAlexander Motin 			action &= ~SSQ_PRINT_SENSE;
172084c4de2dSAlexander Motin 		}
17218b8a9b1dSJustin T. Gibbs 		break;
17228b8a9b1dSJustin T. Gibbs 	case CAM_RESRC_UNAVAIL:
172373cf209fSMatt Jacob 		/* Wait a bit for the resource shortage to abate. */
172473cf209fSMatt Jacob 		timeout = periph_noresrc_delay;
172573cf209fSMatt Jacob 		/* FALLTHROUGH */
17268b8a9b1dSJustin T. Gibbs 	case CAM_BUSY:
172773cf209fSMatt Jacob 		if (timeout == 0) {
172873cf209fSMatt Jacob 			/* Wait a bit for the busy condition to abate. */
172973cf209fSMatt Jacob 			timeout = periph_busy_delay;
173073cf209fSMatt Jacob 		}
173173cf209fSMatt Jacob 		relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
173273cf209fSMatt Jacob 		/* FALLTHROUGH */
17330191d9b3SAlexander Motin 	case CAM_ATA_STATUS_ERROR:
17340191d9b3SAlexander Motin 	case CAM_REQ_CMP_ERR:
17350191d9b3SAlexander Motin 	case CAM_CMD_TIMEOUT:
17360191d9b3SAlexander Motin 	case CAM_UNEXP_BUSFREE:
17370191d9b3SAlexander Motin 	case CAM_UNCOR_PARITY:
17380191d9b3SAlexander Motin 	case CAM_DATA_RUN_ERR:
17398b8a9b1dSJustin T. Gibbs 	default:
17400191d9b3SAlexander Motin 		if (periph->flags & CAM_PERIPH_INVALID) {
17410191d9b3SAlexander Motin 			error = EIO;
17420191d9b3SAlexander Motin 			action_string = "Periph was invalidated";
17430191d9b3SAlexander Motin 		} else if (ccb->ccb_h.retry_count == 0) {
17448b8a9b1dSJustin T. Gibbs 			error = EIO;
174583c5d981SAlexander Motin 			action_string = "Retries exhausted";
17460191d9b3SAlexander Motin 		} else if (sense_flags & SF_NO_RETRY) {
17470191d9b3SAlexander Motin 			error = EIO;
17480191d9b3SAlexander Motin 			action_string = "Retry was blocked";
17490191d9b3SAlexander Motin 		} else {
17500191d9b3SAlexander Motin 			ccb->ccb_h.retry_count--;
17510191d9b3SAlexander Motin 			error = ERESTART;
17528b8a9b1dSJustin T. Gibbs 		}
17538b8a9b1dSJustin T. Gibbs 		break;
17548b8a9b1dSJustin T. Gibbs 	}
17558b8a9b1dSJustin T. Gibbs 
17560191d9b3SAlexander Motin 	if ((sense_flags & SF_PRINT_ALWAYS) ||
17570191d9b3SAlexander Motin 	    CAM_DEBUGGED(ccb->ccb_h.path, CAM_DEBUG_INFO))
17580181d54bSAlexander Motin 		action |= SSQ_PRINT_SENSE;
17590191d9b3SAlexander Motin 	else if (sense_flags & SF_NO_PRINT)
17600181d54bSAlexander Motin 		action &= ~SSQ_PRINT_SENSE;
17610181d54bSAlexander Motin 	if ((action & SSQ_PRINT_SENSE) != 0)
17620191d9b3SAlexander Motin 		cam_error_print(orig_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
17630181d54bSAlexander Motin 	if (error != 0 && (action & SSQ_PRINT_SENSE) != 0) {
17640f3e2159SAlexander Motin 		if (error != ERESTART) {
17653393f8daSKenneth D. Merry 			if (action_string == NULL)
176683c5d981SAlexander Motin 				action_string = "Unretryable error";
176783c5d981SAlexander Motin 			xpt_print(ccb->ccb_h.path, "Error %d, %s\n",
176883c5d981SAlexander Motin 			    error, action_string);
176983c5d981SAlexander Motin 		} else if (action_string != NULL)
1770f0d9af51SMatt Jacob 			xpt_print(ccb->ccb_h.path, "%s\n", action_string);
177183c5d981SAlexander Motin 		else
177283c5d981SAlexander Motin 			xpt_print(ccb->ccb_h.path, "Retrying command\n");
17730f3e2159SAlexander Motin 	}
17740f3e2159SAlexander Motin 
1775a30ecd42SScott Long 	if (devctl_err)
1776a30ecd42SScott Long 		cam_periph_devctl_notify(orig_ccb);
1777a30ecd42SScott Long 
17780181d54bSAlexander Motin 	if ((action & SSQ_LOST) != 0) {
17790191d9b3SAlexander Motin 		lun_id_t lun_id;
17800191d9b3SAlexander Motin 
17810191d9b3SAlexander Motin 		/*
17820191d9b3SAlexander Motin 		 * For a selection timeout, we consider all of the LUNs on
17830191d9b3SAlexander Motin 		 * the target to be gone.  If the status is CAM_DEV_NOT_THERE,
17840191d9b3SAlexander Motin 		 * then we only get rid of the device(s) specified by the
17850191d9b3SAlexander Motin 		 * path in the original CCB.
17860191d9b3SAlexander Motin 		 */
17870181d54bSAlexander Motin 		if (status == CAM_SEL_TIMEOUT)
17880191d9b3SAlexander Motin 			lun_id = CAM_LUN_WILDCARD;
17890181d54bSAlexander Motin 		else
17900181d54bSAlexander Motin 			lun_id = xpt_path_lun_id(ccb->ccb_h.path);
17910191d9b3SAlexander Motin 
17920191d9b3SAlexander Motin 		/* Should we do more if we can't create the path?? */
17930191d9b3SAlexander Motin 		if (xpt_create_path(&newpath, periph,
17940191d9b3SAlexander Motin 				    xpt_path_path_id(ccb->ccb_h.path),
17950191d9b3SAlexander Motin 				    xpt_path_target_id(ccb->ccb_h.path),
17960191d9b3SAlexander Motin 				    lun_id) == CAM_REQ_CMP) {
17970191d9b3SAlexander Motin 
17980191d9b3SAlexander Motin 			/*
17990191d9b3SAlexander Motin 			 * Let peripheral drivers know that this
18000191d9b3SAlexander Motin 			 * device has gone away.
18010191d9b3SAlexander Motin 			 */
18020191d9b3SAlexander Motin 			xpt_async(AC_LOST_DEVICE, newpath, NULL);
18030191d9b3SAlexander Motin 			xpt_free_path(newpath);
18040191d9b3SAlexander Motin 		}
18050181d54bSAlexander Motin 	}
18063631c638SAlexander Motin 
18073631c638SAlexander Motin 	/* Broadcast UNIT ATTENTIONs to all periphs. */
18080181d54bSAlexander Motin 	if ((action & SSQ_UA) != 0)
18093631c638SAlexander Motin 		xpt_async(AC_UNIT_ATTENTION, orig_ccb->ccb_h.path, orig_ccb);
18100181d54bSAlexander Motin 
18110181d54bSAlexander Motin 	/* Rescan target on "Reported LUNs data has changed" */
18120181d54bSAlexander Motin 	if ((action & SSQ_RESCAN) != 0) {
18130181d54bSAlexander Motin 		if (xpt_create_path(&newpath, NULL,
18140181d54bSAlexander Motin 				    xpt_path_path_id(ccb->ccb_h.path),
18150181d54bSAlexander Motin 				    xpt_path_target_id(ccb->ccb_h.path),
1816e5736ac8SAlexander Motin 				    CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
18170181d54bSAlexander Motin 
18180181d54bSAlexander Motin 			scan_ccb = xpt_alloc_ccb_nowait();
1819e5736ac8SAlexander Motin 			if (scan_ccb != NULL) {
18200181d54bSAlexander Motin 				scan_ccb->ccb_h.path = newpath;
1821e5736ac8SAlexander Motin 				scan_ccb->ccb_h.func_code = XPT_SCAN_TGT;
18220181d54bSAlexander Motin 				scan_ccb->crcn.flags = 0;
18230181d54bSAlexander Motin 				xpt_rescan(scan_ccb);
18243231e8bdSAlexander Motin 			} else {
1825e5736ac8SAlexander Motin 				xpt_print(newpath,
1826e5736ac8SAlexander Motin 				    "Can't allocate CCB to rescan target\n");
18273231e8bdSAlexander Motin 				xpt_free_path(newpath);
18283231e8bdSAlexander Motin 			}
18290181d54bSAlexander Motin 		}
18300191d9b3SAlexander Motin 	}
18310191d9b3SAlexander Motin 
18320f3e2159SAlexander Motin 	/* Attempt a retry */
18330f3e2159SAlexander Motin 	if (error == ERESTART || error == 0) {
18340f3e2159SAlexander Motin 		if (frozen != 0)
18350f3e2159SAlexander Motin 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
18360f3e2159SAlexander Motin 		if (error == ERESTART)
18370f3e2159SAlexander Motin 			xpt_action(ccb);
18380f3e2159SAlexander Motin 		if (frozen != 0)
18390f3e2159SAlexander Motin 			cam_release_devq(ccb->ccb_h.path,
18400f3e2159SAlexander Motin 					 relsim_flags,
18410f3e2159SAlexander Motin 					 openings,
18420f3e2159SAlexander Motin 					 timeout,
18430f3e2159SAlexander Motin 					 /*getcount_only*/0);
18443393f8daSKenneth D. Merry 	}
18458b8a9b1dSJustin T. Gibbs 
18468b8a9b1dSJustin T. Gibbs 	return (error);
18478b8a9b1dSJustin T. Gibbs }
1848a30ecd42SScott Long 
1849a30ecd42SScott Long #define CAM_PERIPH_DEVD_MSG_SIZE	256
1850a30ecd42SScott Long 
1851a30ecd42SScott Long static void
1852a30ecd42SScott Long cam_periph_devctl_notify(union ccb *ccb)
1853a30ecd42SScott Long {
1854a30ecd42SScott Long 	struct cam_periph *periph;
1855a30ecd42SScott Long 	struct ccb_getdev *cgd;
1856a30ecd42SScott Long 	struct sbuf sb;
1857a30ecd42SScott Long 	int serr, sk, asc, ascq;
1858a30ecd42SScott Long 	char *sbmsg, *type;
1859a30ecd42SScott Long 
1860a30ecd42SScott Long 	sbmsg = malloc(CAM_PERIPH_DEVD_MSG_SIZE, M_CAMPERIPH, M_NOWAIT);
1861a30ecd42SScott Long 	if (sbmsg == NULL)
1862a30ecd42SScott Long 		return;
1863a30ecd42SScott Long 
1864a30ecd42SScott Long 	sbuf_new(&sb, sbmsg, CAM_PERIPH_DEVD_MSG_SIZE, SBUF_FIXEDLEN);
1865a30ecd42SScott Long 
1866a30ecd42SScott Long 	periph = xpt_path_periph(ccb->ccb_h.path);
1867a30ecd42SScott Long 	sbuf_printf(&sb, "device=%s%d ", periph->periph_name,
1868a30ecd42SScott Long 	    periph->unit_number);
1869a30ecd42SScott Long 
1870a30ecd42SScott Long 	sbuf_printf(&sb, "serial=\"");
1871a30ecd42SScott Long 	if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) != NULL) {
1872a30ecd42SScott Long 		xpt_setup_ccb(&cgd->ccb_h, ccb->ccb_h.path,
1873a30ecd42SScott Long 		    CAM_PRIORITY_NORMAL);
1874a30ecd42SScott Long 		cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1875a30ecd42SScott Long 		xpt_action((union ccb *)cgd);
1876a30ecd42SScott Long 
1877a30ecd42SScott Long 		if (cgd->ccb_h.status == CAM_REQ_CMP)
1878a30ecd42SScott Long 			sbuf_bcat(&sb, cgd->serial_num, cgd->serial_num_len);
1879a30ecd42SScott Long 	}
1880a30ecd42SScott Long 	sbuf_printf(&sb, "\" ");
1881a30ecd42SScott Long 	sbuf_printf(&sb, "cam_status=\"0x%x\" ", ccb->ccb_h.status);
1882a30ecd42SScott Long 
1883a30ecd42SScott Long 	switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
1884a30ecd42SScott Long 	case CAM_CMD_TIMEOUT:
1885a30ecd42SScott Long 		sbuf_printf(&sb, "timeout=%d ", ccb->ccb_h.timeout);
1886a30ecd42SScott Long 		type = "timeout";
1887a30ecd42SScott Long 		break;
1888a30ecd42SScott Long 	case CAM_SCSI_STATUS_ERROR:
1889a30ecd42SScott Long 		sbuf_printf(&sb, "scsi_status=%d ", ccb->csio.scsi_status);
1890a30ecd42SScott Long 		if (scsi_extract_sense_ccb(ccb, &serr, &sk, &asc, &ascq))
1891a30ecd42SScott Long 			sbuf_printf(&sb, "scsi_sense=\"%02x %02x %02x %02x\" ",
1892a30ecd42SScott Long 			    serr, sk, asc, ascq);
1893a30ecd42SScott Long 		type = "error";
1894a30ecd42SScott Long 		break;
1895a30ecd42SScott Long 	case CAM_ATA_STATUS_ERROR:
1896a30ecd42SScott Long 		sbuf_printf(&sb, "RES=\"");
1897a30ecd42SScott Long 		ata_res_sbuf(&ccb->ataio.res, &sb);
1898a30ecd42SScott Long 		sbuf_printf(&sb, "\" ");
1899a30ecd42SScott Long 		type = "error";
1900a30ecd42SScott Long 		break;
1901a30ecd42SScott Long 	default:
1902a30ecd42SScott Long 		type = "error";
1903a30ecd42SScott Long 		break;
1904a30ecd42SScott Long 	}
1905a30ecd42SScott Long 
1906a30ecd42SScott Long 	if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1907a30ecd42SScott Long 		sbuf_printf(&sb, "CDB=\"");
1908a30ecd42SScott Long 		if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0)
1909a30ecd42SScott Long 			scsi_cdb_sbuf(ccb->csio.cdb_io.cdb_ptr, &sb);
1910a30ecd42SScott Long 		else
1911a30ecd42SScott Long 			scsi_cdb_sbuf(ccb->csio.cdb_io.cdb_bytes, &sb);
1912a30ecd42SScott Long 		sbuf_printf(&sb, "\" ");
1913a30ecd42SScott Long 	} else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1914a30ecd42SScott Long 		sbuf_printf(&sb, "ACB=\"");
1915a30ecd42SScott Long 		ata_cmd_sbuf(&ccb->ataio.cmd, &sb);
1916a30ecd42SScott Long 		sbuf_printf(&sb, "\" ");
1917a30ecd42SScott Long 	}
1918a30ecd42SScott Long 
1919a30ecd42SScott Long 	if (sbuf_finish(&sb) == 0)
1920a30ecd42SScott Long 		devctl_notify("CAM", "periph", type, sbuf_data(&sb));
1921a30ecd42SScott Long 	sbuf_delete(&sb);
1922a30ecd42SScott Long 	free(sbmsg, M_CAMPERIPH);
1923a30ecd42SScott Long }
1924a30ecd42SScott Long 
1925