xref: /freebsd/sys/cam/cam_periph.c (revision 774ab87cf27beb3669693f14b9c89ebaf542eaf3)
1898b0535SWarner Losh /*-
28b8a9b1dSJustin T. Gibbs  * Common functions for CAM "type" (peripheral) drivers.
38b8a9b1dSJustin T. Gibbs  *
44d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
5bec9534dSPedro F. Giffuni  *
68b8a9b1dSJustin T. Gibbs  * Copyright (c) 1997, 1998 Justin T. Gibbs.
7501468a5SKenneth D. Merry  * Copyright (c) 1997, 1998, 1999, 2000 Kenneth D. Merry.
88b8a9b1dSJustin T. Gibbs  * All rights reserved.
98b8a9b1dSJustin T. Gibbs  *
108b8a9b1dSJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
118b8a9b1dSJustin T. Gibbs  * modification, are permitted provided that the following conditions
128b8a9b1dSJustin T. Gibbs  * are met:
138b8a9b1dSJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
148b8a9b1dSJustin T. Gibbs  *    notice, this list of conditions, and the following disclaimer,
158b8a9b1dSJustin T. Gibbs  *    without modification, immediately at the beginning of the file.
168b8a9b1dSJustin T. Gibbs  * 2. The name of the author may not be used to endorse or promote products
178b8a9b1dSJustin T. Gibbs  *    derived from this software without specific prior written permission.
188b8a9b1dSJustin T. Gibbs  *
198b8a9b1dSJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
208b8a9b1dSJustin T. Gibbs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
218b8a9b1dSJustin T. Gibbs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
228b8a9b1dSJustin T. Gibbs  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
238b8a9b1dSJustin T. Gibbs  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
248b8a9b1dSJustin T. Gibbs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
258b8a9b1dSJustin T. Gibbs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
268b8a9b1dSJustin T. Gibbs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
278b8a9b1dSJustin T. Gibbs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
288b8a9b1dSJustin T. Gibbs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
298b8a9b1dSJustin T. Gibbs  * SUCH DAMAGE.
308b8a9b1dSJustin T. Gibbs  */
318b8a9b1dSJustin T. Gibbs 
329c963d87SDavid E. O'Brien #include <sys/cdefs.h>
339c963d87SDavid E. O'Brien __FBSDID("$FreeBSD$");
349c963d87SDavid E. O'Brien 
358b8a9b1dSJustin T. Gibbs #include <sys/param.h>
368b8a9b1dSJustin T. Gibbs #include <sys/systm.h>
378b8a9b1dSJustin T. Gibbs #include <sys/types.h>
388b8a9b1dSJustin T. Gibbs #include <sys/malloc.h>
39362abc44STai-hwa Liang #include <sys/kernel.h>
409626b608SPoul-Henning Kamp #include <sys/bio.h>
415cf3cd10SWarner Losh #include <sys/conf.h>
42773e541eSWarner Losh #include <sys/devctl.h>
43f34fa851SJohn Baldwin #include <sys/lock.h>
44f34fa851SJohn Baldwin #include <sys/mutex.h>
458b8a9b1dSJustin T. Gibbs #include <sys/buf.h>
468b8a9b1dSJustin T. Gibbs #include <sys/proc.h>
478b8a9b1dSJustin T. Gibbs #include <sys/devicestat.h>
483501942bSJustin T. Gibbs #include <sys/sbuf.h>
49b059686aSAlexander Motin #include <sys/sysctl.h>
508b8a9b1dSJustin T. Gibbs #include <vm/vm.h>
518b8a9b1dSJustin T. Gibbs #include <vm/vm_extern.h>
528b8a9b1dSJustin T. Gibbs 
538b8a9b1dSJustin T. Gibbs #include <cam/cam.h>
548b8a9b1dSJustin T. Gibbs #include <cam/cam_ccb.h>
550cd631eeSMark Johnston #include <cam/cam_compat.h>
5652c9ce25SScott Long #include <cam/cam_queue.h>
578b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt_periph.h>
58447b3557SJohn Baldwin #include <cam/cam_xpt_internal.h>
598b8a9b1dSJustin T. Gibbs #include <cam/cam_periph.h>
608b8a9b1dSJustin T. Gibbs #include <cam/cam_debug.h>
612b83592fSScott Long #include <cam/cam_sim.h>
628b8a9b1dSJustin T. Gibbs 
638b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_all.h>
648b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_message.h>
658b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_pass.h>
668b8a9b1dSJustin T. Gibbs 
678b8a9b1dSJustin T. Gibbs static	u_int		camperiphnextunit(struct periph_driver *p_drv,
68d836c48eSWarner Losh 					  u_int newunit, bool wired,
69501468a5SKenneth D. Merry 					  path_id_t pathid, target_id_t target,
70501468a5SKenneth D. Merry 					  lun_id_t lun);
718b8a9b1dSJustin T. Gibbs static	u_int		camperiphunit(struct periph_driver *p_drv,
72501468a5SKenneth D. Merry 				      path_id_t pathid, target_id_t target,
73577f9aa2SWarner Losh 				      lun_id_t lun,
74577f9aa2SWarner Losh 				      const char *sn);
758b8a9b1dSJustin T. Gibbs static	void		camperiphdone(struct cam_periph *periph,
768b8a9b1dSJustin T. Gibbs 					union ccb *done_ccb);
778b8a9b1dSJustin T. Gibbs static  void		camperiphfree(struct cam_periph *periph);
783393f8daSKenneth D. Merry static int		camperiphscsistatuserror(union ccb *ccb,
790191d9b3SAlexander Motin 					        union ccb **orig_ccb,
803393f8daSKenneth D. Merry 						 cam_flags camflags,
813393f8daSKenneth D. Merry 						 u_int32_t sense_flags,
823393f8daSKenneth D. Merry 						 int *openings,
833393f8daSKenneth D. Merry 						 u_int32_t *relsim_flags,
8483c5d981SAlexander Motin 						 u_int32_t *timeout,
850181d54bSAlexander Motin 						 u_int32_t  *action,
8683c5d981SAlexander Motin 						 const char **action_string);
873393f8daSKenneth D. Merry static	int		camperiphscsisenseerror(union ccb *ccb,
880191d9b3SAlexander Motin 					        union ccb **orig_ccb,
893393f8daSKenneth D. Merry 					        cam_flags camflags,
903393f8daSKenneth D. Merry 					        u_int32_t sense_flags,
913393f8daSKenneth D. Merry 					        int *openings,
923393f8daSKenneth D. Merry 					        u_int32_t *relsim_flags,
9383c5d981SAlexander Motin 					        u_int32_t *timeout,
940181d54bSAlexander Motin 					        u_int32_t *action,
9583c5d981SAlexander Motin 					        const char **action_string);
96a30ecd42SScott Long static void		cam_periph_devctl_notify(union ccb *ccb);
978b8a9b1dSJustin T. Gibbs 
980b7c27b9SPeter Wemm static int nperiph_drivers;
9983c5d981SAlexander Motin static int initialized = 0;
1000b7c27b9SPeter Wemm struct periph_driver **periph_drivers;
1010b7c27b9SPeter Wemm 
102d745c852SEd Schouten static MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers");
103362abc44STai-hwa Liang 
10473cf209fSMatt Jacob static int periph_selto_delay = 1000;
10573cf209fSMatt Jacob TUNABLE_INT("kern.cam.periph_selto_delay", &periph_selto_delay);
10673cf209fSMatt Jacob static int periph_noresrc_delay = 500;
10773cf209fSMatt Jacob TUNABLE_INT("kern.cam.periph_noresrc_delay", &periph_noresrc_delay);
10873cf209fSMatt Jacob static int periph_busy_delay = 500;
10973cf209fSMatt Jacob TUNABLE_INT("kern.cam.periph_busy_delay", &periph_busy_delay);
11073cf209fSMatt Jacob 
111b059686aSAlexander Motin static u_int periph_mapmem_thresh = 65536;
112b059686aSAlexander Motin SYSCTL_UINT(_kern_cam, OID_AUTO, mapmem_thresh, CTLFLAG_RWTUN,
113b059686aSAlexander Motin     &periph_mapmem_thresh, 0, "Threshold for user-space buffer mapping");
11473cf209fSMatt Jacob 
1150b7c27b9SPeter Wemm void
1160b7c27b9SPeter Wemm periphdriver_register(void *data)
1170b7c27b9SPeter Wemm {
11883c5d981SAlexander Motin 	struct periph_driver *drv = (struct periph_driver *)data;
1190b7c27b9SPeter Wemm 	struct periph_driver **newdrivers, **old;
1200b7c27b9SPeter Wemm 	int ndrivers;
1210b7c27b9SPeter Wemm 
1225719711fSEdward Tomasz Napierala again:
1230b7c27b9SPeter Wemm 	ndrivers = nperiph_drivers + 2;
1240dd50e9bSScott Long 	newdrivers = malloc(sizeof(*newdrivers) * ndrivers, M_CAMPERIPH,
1250dd50e9bSScott Long 			    M_WAITOK);
1265719711fSEdward Tomasz Napierala 	xpt_lock_buses();
1275719711fSEdward Tomasz Napierala 	if (ndrivers != nperiph_drivers + 2) {
1285719711fSEdward Tomasz Napierala 		/*
1295719711fSEdward Tomasz Napierala 		 * Lost race against itself; go around.
1305719711fSEdward Tomasz Napierala 		 */
1315719711fSEdward Tomasz Napierala 		xpt_unlock_buses();
1325719711fSEdward Tomasz Napierala 		free(newdrivers, M_CAMPERIPH);
1335719711fSEdward Tomasz Napierala 		goto again;
1345719711fSEdward Tomasz Napierala 	}
1350b7c27b9SPeter Wemm 	if (periph_drivers)
1360b7c27b9SPeter Wemm 		bcopy(periph_drivers, newdrivers,
137623db360SKenneth D. Merry 		      sizeof(*newdrivers) * nperiph_drivers);
13883c5d981SAlexander Motin 	newdrivers[nperiph_drivers] = drv;
1390b7c27b9SPeter Wemm 	newdrivers[nperiph_drivers + 1] = NULL;
1400b7c27b9SPeter Wemm 	old = periph_drivers;
1410b7c27b9SPeter Wemm 	periph_drivers = newdrivers;
1425719711fSEdward Tomasz Napierala 	nperiph_drivers++;
1435719711fSEdward Tomasz Napierala 	xpt_unlock_buses();
1440b7c27b9SPeter Wemm 	if (old)
1450dd50e9bSScott Long 		free(old, M_CAMPERIPH);
14683c5d981SAlexander Motin 	/* If driver marked as early or it is late now, initialize it. */
14783c5d981SAlexander Motin 	if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
14883c5d981SAlexander Motin 	    initialized > 1)
14983c5d981SAlexander Motin 		(*drv->init)();
15083c5d981SAlexander Motin }
15183c5d981SAlexander Motin 
15294173c3cSAlexander Motin int
15394173c3cSAlexander Motin periphdriver_unregister(void *data)
15494173c3cSAlexander Motin {
15594173c3cSAlexander Motin 	struct periph_driver *drv = (struct periph_driver *)data;
15694173c3cSAlexander Motin 	int error, n;
15794173c3cSAlexander Motin 
15894173c3cSAlexander Motin 	/* If driver marked as early or it is late now, deinitialize it. */
15994173c3cSAlexander Motin 	if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
16094173c3cSAlexander Motin 	    initialized > 1) {
16194173c3cSAlexander Motin 		if (drv->deinit == NULL) {
16294173c3cSAlexander Motin 			printf("CAM periph driver '%s' doesn't have deinit.\n",
16394173c3cSAlexander Motin 			    drv->driver_name);
16494173c3cSAlexander Motin 			return (EOPNOTSUPP);
16594173c3cSAlexander Motin 		}
16694173c3cSAlexander Motin 		error = drv->deinit();
16794173c3cSAlexander Motin 		if (error != 0)
16894173c3cSAlexander Motin 			return (error);
16994173c3cSAlexander Motin 	}
17094173c3cSAlexander Motin 
17194173c3cSAlexander Motin 	xpt_lock_buses();
17294173c3cSAlexander Motin 	for (n = 0; n < nperiph_drivers && periph_drivers[n] != drv; n++)
17394173c3cSAlexander Motin 		;
17494173c3cSAlexander Motin 	KASSERT(n < nperiph_drivers,
17594173c3cSAlexander Motin 	    ("Periph driver '%s' was not registered", drv->driver_name));
17694173c3cSAlexander Motin 	for (; n + 1 < nperiph_drivers; n++)
17794173c3cSAlexander Motin 		periph_drivers[n] = periph_drivers[n + 1];
17894173c3cSAlexander Motin 	periph_drivers[n + 1] = NULL;
17994173c3cSAlexander Motin 	nperiph_drivers--;
18094173c3cSAlexander Motin 	xpt_unlock_buses();
18194173c3cSAlexander Motin 	return (0);
18294173c3cSAlexander Motin }
18394173c3cSAlexander Motin 
18483c5d981SAlexander Motin void
18583c5d981SAlexander Motin periphdriver_init(int level)
18683c5d981SAlexander Motin {
18783c5d981SAlexander Motin 	int	i, early;
18883c5d981SAlexander Motin 
18983c5d981SAlexander Motin 	initialized = max(initialized, level);
19083c5d981SAlexander Motin 	for (i = 0; periph_drivers[i] != NULL; i++) {
19183c5d981SAlexander Motin 		early = (periph_drivers[i]->flags & CAM_PERIPH_DRV_EARLY) ? 1 : 2;
19283c5d981SAlexander Motin 		if (early == initialized)
19383c5d981SAlexander Motin 			(*periph_drivers[i]->init)();
19483c5d981SAlexander Motin 	}
1950b7c27b9SPeter Wemm }
1960b7c27b9SPeter Wemm 
1978b8a9b1dSJustin T. Gibbs cam_status
198ee9c90c7SKenneth D. Merry cam_periph_alloc(periph_ctor_t *periph_ctor,
199ee9c90c7SKenneth D. Merry 		 periph_oninv_t *periph_oninvalidate,
200ee9c90c7SKenneth D. Merry 		 periph_dtor_t *periph_dtor, periph_start_t *periph_start,
201ee9c90c7SKenneth D. Merry 		 char *name, cam_periph_type type, struct cam_path *path,
202ee9c90c7SKenneth D. Merry 		 ac_callback_t *ac_callback, ac_code code, void *arg)
2038b8a9b1dSJustin T. Gibbs {
2048b8a9b1dSJustin T. Gibbs 	struct		periph_driver **p_drv;
2052b83592fSScott Long 	struct		cam_sim *sim;
2068b8a9b1dSJustin T. Gibbs 	struct		cam_periph *periph;
2078b8a9b1dSJustin T. Gibbs 	struct		cam_periph *cur_periph;
2088b8a9b1dSJustin T. Gibbs 	path_id_t	path_id;
2098b8a9b1dSJustin T. Gibbs 	target_id_t	target_id;
2108b8a9b1dSJustin T. Gibbs 	lun_id_t	lun_id;
2118b8a9b1dSJustin T. Gibbs 	cam_status	status;
2128b8a9b1dSJustin T. Gibbs 	u_int		init_level;
2138b8a9b1dSJustin T. Gibbs 
2148b8a9b1dSJustin T. Gibbs 	init_level = 0;
2158b8a9b1dSJustin T. Gibbs 	/*
2168b8a9b1dSJustin T. Gibbs 	 * Handle Hot-Plug scenarios.  If there is already a peripheral
2178b8a9b1dSJustin T. Gibbs 	 * of our type assigned to this path, we are likely waiting for
2188b8a9b1dSJustin T. Gibbs 	 * final close on an old, invalidated, peripheral.  If this is
2198b8a9b1dSJustin T. Gibbs 	 * the case, queue up a deferred call to the peripheral's async
22074c91ec5SJustin T. Gibbs 	 * handler.  If it looks like a mistaken re-allocation, complain.
2218b8a9b1dSJustin T. Gibbs 	 */
2228b8a9b1dSJustin T. Gibbs 	if ((periph = cam_periph_find(path, name)) != NULL) {
2238b8a9b1dSJustin T. Gibbs 		if ((periph->flags & CAM_PERIPH_INVALID) != 0
2248b8a9b1dSJustin T. Gibbs 		 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) {
2258b8a9b1dSJustin T. Gibbs 			periph->flags |= CAM_PERIPH_NEW_DEV_FOUND;
2268b8a9b1dSJustin T. Gibbs 			periph->deferred_callback = ac_callback;
2278b8a9b1dSJustin T. Gibbs 			periph->deferred_ac = code;
2288b8a9b1dSJustin T. Gibbs 			return (CAM_REQ_INPROG);
2298b8a9b1dSJustin T. Gibbs 		} else {
2308b8a9b1dSJustin T. Gibbs 			printf("cam_periph_alloc: attempt to re-allocate "
2318900f4b8SKenneth D. Merry 			       "valid device %s%d rejected flags %#x "
2328900f4b8SKenneth D. Merry 			       "refcount %d\n", periph->periph_name,
2338900f4b8SKenneth D. Merry 			       periph->unit_number, periph->flags,
2348900f4b8SKenneth D. Merry 			       periph->refcount);
2358b8a9b1dSJustin T. Gibbs 		}
2368b8a9b1dSJustin T. Gibbs 		return (CAM_REQ_INVALID);
2378b8a9b1dSJustin T. Gibbs 	}
2388b8a9b1dSJustin T. Gibbs 
239362abc44STai-hwa Liang 	periph = (struct cam_periph *)malloc(sizeof(*periph), M_CAMPERIPH,
2408900f4b8SKenneth D. Merry 					     M_NOWAIT|M_ZERO);
2418b8a9b1dSJustin T. Gibbs 
2428b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
2438b8a9b1dSJustin T. Gibbs 		return (CAM_RESRC_UNAVAIL);
2448b8a9b1dSJustin T. Gibbs 
2458b8a9b1dSJustin T. Gibbs 	init_level++;
2468b8a9b1dSJustin T. Gibbs 
2472b83592fSScott Long 	sim = xpt_path_sim(path);
2488b8a9b1dSJustin T. Gibbs 	path_id = xpt_path_path_id(path);
2498b8a9b1dSJustin T. Gibbs 	target_id = xpt_path_target_id(path);
2508b8a9b1dSJustin T. Gibbs 	lun_id = xpt_path_lun_id(path);
2518b8a9b1dSJustin T. Gibbs 	periph->periph_start = periph_start;
2528b8a9b1dSJustin T. Gibbs 	periph->periph_dtor = periph_dtor;
253ee9c90c7SKenneth D. Merry 	periph->periph_oninval = periph_oninvalidate;
2548b8a9b1dSJustin T. Gibbs 	periph->type = type;
2558b8a9b1dSJustin T. Gibbs 	periph->periph_name = name;
256227d67aaSAlexander Motin 	periph->scheduled_priority = CAM_PRIORITY_NONE;
2578b8a9b1dSJustin T. Gibbs 	periph->immediate_priority = CAM_PRIORITY_NONE;
258d38c0e53SAlexander Motin 	periph->refcount = 1;		/* Dropped by invalidation. */
2592b83592fSScott Long 	periph->sim = sim;
2608b8a9b1dSJustin T. Gibbs 	SLIST_INIT(&periph->ccb_list);
2618b8a9b1dSJustin T. Gibbs 	status = xpt_create_path(&path, periph, path_id, target_id, lun_id);
2628b8a9b1dSJustin T. Gibbs 	if (status != CAM_REQ_CMP)
2638b8a9b1dSJustin T. Gibbs 		goto failure;
2648b8a9b1dSJustin T. Gibbs 	periph->path = path;
2658b8a9b1dSJustin T. Gibbs 
266f1e2546aSMatt Jacob 	xpt_lock_buses();
267f1e2546aSMatt Jacob 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
268f1e2546aSMatt Jacob 		if (strcmp((*p_drv)->driver_name, name) == 0)
269f1e2546aSMatt Jacob 			break;
270f1e2546aSMatt Jacob 	}
271f1e2546aSMatt Jacob 	if (*p_drv == NULL) {
272f1e2546aSMatt Jacob 		printf("cam_periph_alloc: invalid periph name '%s'\n", name);
2739e0d30e2SEdward Tomasz Napierala 		xpt_unlock_buses();
274f1e2546aSMatt Jacob 		xpt_free_path(periph->path);
275f1e2546aSMatt Jacob 		free(periph, M_CAMPERIPH);
276f1e2546aSMatt Jacob 		return (CAM_REQ_INVALID);
277f1e2546aSMatt Jacob 	}
278577f9aa2SWarner Losh 	periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id,
279577f9aa2SWarner Losh 	    path->device->serial_num);
2808b8a9b1dSJustin T. Gibbs 	cur_periph = TAILQ_FIRST(&(*p_drv)->units);
2818b8a9b1dSJustin T. Gibbs 	while (cur_periph != NULL
2828b8a9b1dSJustin T. Gibbs 	    && cur_periph->unit_number < periph->unit_number)
2838b8a9b1dSJustin T. Gibbs 		cur_periph = TAILQ_NEXT(cur_periph, unit_links);
284f1e2546aSMatt Jacob 	if (cur_periph != NULL) {
285dbfe5dd3SWarner Losh 		KASSERT(cur_periph->unit_number != periph->unit_number,
286dbfe5dd3SWarner Losh 		    ("duplicate units on periph list"));
2878b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links);
288f1e2546aSMatt Jacob 	} else {
2898b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links);
2908b8a9b1dSJustin T. Gibbs 		(*p_drv)->generation++;
2918b8a9b1dSJustin T. Gibbs 	}
292f1e2546aSMatt Jacob 	xpt_unlock_buses();
293f1e2546aSMatt Jacob 
294f1e2546aSMatt Jacob 	init_level++;
295f1e2546aSMatt Jacob 
296f1e2546aSMatt Jacob 	status = xpt_add_periph(periph);
297f1e2546aSMatt Jacob 	if (status != CAM_REQ_CMP)
298f1e2546aSMatt Jacob 		goto failure;
2998b8a9b1dSJustin T. Gibbs 
3008b8a9b1dSJustin T. Gibbs 	init_level++;
30122c7d606SAlexander Motin 	CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph created\n"));
3028b8a9b1dSJustin T. Gibbs 
3038b8a9b1dSJustin T. Gibbs 	status = periph_ctor(periph, arg);
3048b8a9b1dSJustin T. Gibbs 
3058b8a9b1dSJustin T. Gibbs 	if (status == CAM_REQ_CMP)
3068b8a9b1dSJustin T. Gibbs 		init_level++;
3078b8a9b1dSJustin T. Gibbs 
3088b8a9b1dSJustin T. Gibbs failure:
3098b8a9b1dSJustin T. Gibbs 	switch (init_level) {
3108b8a9b1dSJustin T. Gibbs 	case 4:
3118b8a9b1dSJustin T. Gibbs 		/* Initialized successfully */
3128b8a9b1dSJustin T. Gibbs 		break;
3138b8a9b1dSJustin T. Gibbs 	case 3:
31422c7d606SAlexander Motin 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
315a29779e8SAlexander Motin 		xpt_remove_periph(periph);
3167379c88fSPoul-Henning Kamp 		/* FALLTHROUGH */
3178b8a9b1dSJustin T. Gibbs 	case 2:
318f1e2546aSMatt Jacob 		xpt_lock_buses();
319f1e2546aSMatt Jacob 		TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links);
320f1e2546aSMatt Jacob 		xpt_unlock_buses();
3218b8a9b1dSJustin T. Gibbs 		xpt_free_path(periph->path);
3227379c88fSPoul-Henning Kamp 		/* FALLTHROUGH */
3238b8a9b1dSJustin T. Gibbs 	case 1:
324362abc44STai-hwa Liang 		free(periph, M_CAMPERIPH);
3257379c88fSPoul-Henning Kamp 		/* FALLTHROUGH */
3268b8a9b1dSJustin T. Gibbs 	case 0:
3278b8a9b1dSJustin T. Gibbs 		/* No cleanup to perform. */
3288b8a9b1dSJustin T. Gibbs 		break;
3298b8a9b1dSJustin T. Gibbs 	default:
330905cb85dSKenneth D. Merry 		panic("%s: Unknown init level", __func__);
3318b8a9b1dSJustin T. Gibbs 	}
3328b8a9b1dSJustin T. Gibbs 	return(status);
3338b8a9b1dSJustin T. Gibbs }
3348b8a9b1dSJustin T. Gibbs 
3358b8a9b1dSJustin T. Gibbs /*
3368b8a9b1dSJustin T. Gibbs  * Find a peripheral structure with the specified path, target, lun,
3378b8a9b1dSJustin T. Gibbs  * and (optionally) type.  If the name is NULL, this function will return
3388b8a9b1dSJustin T. Gibbs  * the first peripheral driver that matches the specified path.
3398b8a9b1dSJustin T. Gibbs  */
3408b8a9b1dSJustin T. Gibbs struct cam_periph *
3418b8a9b1dSJustin T. Gibbs cam_periph_find(struct cam_path *path, char *name)
3428b8a9b1dSJustin T. Gibbs {
3438b8a9b1dSJustin T. Gibbs 	struct periph_driver **p_drv;
3448b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph;
3458b8a9b1dSJustin T. Gibbs 
34677dc25ccSScott Long 	xpt_lock_buses();
3470b7c27b9SPeter Wemm 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
3488b8a9b1dSJustin T. Gibbs 		if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0))
3498b8a9b1dSJustin T. Gibbs 			continue;
3508b8a9b1dSJustin T. Gibbs 
35137d40066SPoul-Henning Kamp 		TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
3528b8a9b1dSJustin T. Gibbs 			if (xpt_path_comp(periph->path, path) == 0) {
35377dc25ccSScott Long 				xpt_unlock_buses();
354227d67aaSAlexander Motin 				cam_periph_assert(periph, MA_OWNED);
3558b8a9b1dSJustin T. Gibbs 				return(periph);
3568b8a9b1dSJustin T. Gibbs 			}
3578b8a9b1dSJustin T. Gibbs 		}
35877dc25ccSScott Long 		if (name != NULL) {
35977dc25ccSScott Long 			xpt_unlock_buses();
3608b8a9b1dSJustin T. Gibbs 			return(NULL);
3618b8a9b1dSJustin T. Gibbs 		}
36277dc25ccSScott Long 	}
36377dc25ccSScott Long 	xpt_unlock_buses();
3648b8a9b1dSJustin T. Gibbs 	return(NULL);
3658b8a9b1dSJustin T. Gibbs }
3668b8a9b1dSJustin T. Gibbs 
3673501942bSJustin T. Gibbs /*
3688900f4b8SKenneth D. Merry  * Find peripheral driver instances attached to the specified path.
3693501942bSJustin T. Gibbs  */
3703501942bSJustin T. Gibbs int
3713501942bSJustin T. Gibbs cam_periph_list(struct cam_path *path, struct sbuf *sb)
3723501942bSJustin T. Gibbs {
3738900f4b8SKenneth D. Merry 	struct sbuf local_sb;
3743501942bSJustin T. Gibbs 	struct periph_driver **p_drv;
3753501942bSJustin T. Gibbs 	struct cam_periph *periph;
3763501942bSJustin T. Gibbs 	int count;
3778900f4b8SKenneth D. Merry 	int sbuf_alloc_len;
3783501942bSJustin T. Gibbs 
3798900f4b8SKenneth D. Merry 	sbuf_alloc_len = 16;
3808900f4b8SKenneth D. Merry retry:
3818900f4b8SKenneth D. Merry 	sbuf_new(&local_sb, NULL, sbuf_alloc_len, SBUF_FIXEDLEN);
3823501942bSJustin T. Gibbs 	count = 0;
3833501942bSJustin T. Gibbs 	xpt_lock_buses();
3843501942bSJustin T. Gibbs 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
3853501942bSJustin T. Gibbs 		TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
3863501942bSJustin T. Gibbs 			if (xpt_path_comp(periph->path, path) != 0)
3873501942bSJustin T. Gibbs 				continue;
3883501942bSJustin T. Gibbs 
3898900f4b8SKenneth D. Merry 			if (sbuf_len(&local_sb) != 0)
3908900f4b8SKenneth D. Merry 				sbuf_cat(&local_sb, ",");
3913501942bSJustin T. Gibbs 
3928900f4b8SKenneth D. Merry 			sbuf_printf(&local_sb, "%s%d", periph->periph_name,
3933501942bSJustin T. Gibbs 				    periph->unit_number);
3948900f4b8SKenneth D. Merry 
3958900f4b8SKenneth D. Merry 			if (sbuf_error(&local_sb) == ENOMEM) {
3968900f4b8SKenneth D. Merry 				sbuf_alloc_len *= 2;
3978900f4b8SKenneth D. Merry 				xpt_unlock_buses();
3988900f4b8SKenneth D. Merry 				sbuf_delete(&local_sb);
3998900f4b8SKenneth D. Merry 				goto retry;
4008900f4b8SKenneth D. Merry 			}
4013501942bSJustin T. Gibbs 			count++;
4023501942bSJustin T. Gibbs 		}
4033501942bSJustin T. Gibbs 	}
4043501942bSJustin T. Gibbs 	xpt_unlock_buses();
4058900f4b8SKenneth D. Merry 	sbuf_finish(&local_sb);
40607f7e4c8SAlexander Motin 	if (sbuf_len(sb) != 0)
40707f7e4c8SAlexander Motin 		sbuf_cat(sb, ",");
40807f7e4c8SAlexander Motin 	sbuf_cat(sb, sbuf_data(&local_sb));
4098900f4b8SKenneth D. Merry 	sbuf_delete(&local_sb);
4103501942bSJustin T. Gibbs 	return (count);
4113501942bSJustin T. Gibbs }
4123501942bSJustin T. Gibbs 
41399e7a4adSScott Long int
4148b8a9b1dSJustin T. Gibbs cam_periph_acquire(struct cam_periph *periph)
4158b8a9b1dSJustin T. Gibbs {
41699e7a4adSScott Long 	int status;
4178b8a9b1dSJustin T. Gibbs 
4188b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
41999e7a4adSScott Long 		return (EINVAL);
4208b8a9b1dSJustin T. Gibbs 
42199e7a4adSScott Long 	status = ENOENT;
4222b83592fSScott Long 	xpt_lock_buses();
4238900f4b8SKenneth D. Merry 	if ((periph->flags & CAM_PERIPH_INVALID) == 0) {
4248b8a9b1dSJustin T. Gibbs 		periph->refcount++;
42599e7a4adSScott Long 		status = 0;
4268900f4b8SKenneth D. Merry 	}
4272b83592fSScott Long 	xpt_unlock_buses();
4288b8a9b1dSJustin T. Gibbs 
4298900f4b8SKenneth D. Merry 	return (status);
4308900f4b8SKenneth D. Merry }
4318900f4b8SKenneth D. Merry 
4328900f4b8SKenneth D. Merry void
433c33e4029SAlexander Motin cam_periph_doacquire(struct cam_periph *periph)
434c33e4029SAlexander Motin {
435c33e4029SAlexander Motin 
436c33e4029SAlexander Motin 	xpt_lock_buses();
437c33e4029SAlexander Motin 	KASSERT(periph->refcount >= 1,
438c33e4029SAlexander Motin 	    ("cam_periph_doacquire() with refcount == %d", periph->refcount));
439c33e4029SAlexander Motin 	periph->refcount++;
440c33e4029SAlexander Motin 	xpt_unlock_buses();
441c33e4029SAlexander Motin }
442c33e4029SAlexander Motin 
443c33e4029SAlexander Motin void
4448900f4b8SKenneth D. Merry cam_periph_release_locked_buses(struct cam_periph *periph)
4458900f4b8SKenneth D. Merry {
446dcdf6e74SAlexander Motin 
447227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
448dcdf6e74SAlexander Motin 	KASSERT(periph->refcount >= 1, ("periph->refcount >= 1"));
449d38c0e53SAlexander Motin 	if (--periph->refcount == 0)
4508900f4b8SKenneth D. Merry 		camperiphfree(periph);
4518900f4b8SKenneth D. Merry }
4528b8a9b1dSJustin T. Gibbs 
4538b8a9b1dSJustin T. Gibbs void
45424ebf566SEdward Tomasz Napierala cam_periph_release_locked(struct cam_periph *periph)
4558b8a9b1dSJustin T. Gibbs {
4568b8a9b1dSJustin T. Gibbs 
4578b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
4588b8a9b1dSJustin T. Gibbs 		return;
4598b8a9b1dSJustin T. Gibbs 
4602b83592fSScott Long 	xpt_lock_buses();
4618900f4b8SKenneth D. Merry 	cam_periph_release_locked_buses(periph);
4622b83592fSScott Long 	xpt_unlock_buses();
46324ebf566SEdward Tomasz Napierala }
4648b8a9b1dSJustin T. Gibbs 
46524ebf566SEdward Tomasz Napierala void
46624ebf566SEdward Tomasz Napierala cam_periph_release(struct cam_periph *periph)
46724ebf566SEdward Tomasz Napierala {
468227d67aaSAlexander Motin 	struct mtx *mtx;
46924ebf566SEdward Tomasz Napierala 
47024ebf566SEdward Tomasz Napierala 	if (periph == NULL)
47124ebf566SEdward Tomasz Napierala 		return;
47224ebf566SEdward Tomasz Napierala 
473227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_NOTOWNED);
474227d67aaSAlexander Motin 	mtx = cam_periph_mtx(periph);
475227d67aaSAlexander Motin 	mtx_lock(mtx);
47624ebf566SEdward Tomasz Napierala 	cam_periph_release_locked(periph);
477227d67aaSAlexander Motin 	mtx_unlock(mtx);
4788b8a9b1dSJustin T. Gibbs }
4798b8a9b1dSJustin T. Gibbs 
4809385e92bSWarner Losh /*
4819385e92bSWarner Losh  * hold/unhold act as mutual exclusion for sections of the code that
4829385e92bSWarner Losh  * need to sleep and want to make sure that other sections that
4839385e92bSWarner Losh  * will interfere are held off. This only protects exclusive sections
4849385e92bSWarner Losh  * from each other.
4859385e92bSWarner Losh  */
4862b83592fSScott Long int
4872b83592fSScott Long cam_periph_hold(struct cam_periph *periph, int priority)
4882b83592fSScott Long {
4892b83592fSScott Long 	int error;
4902b83592fSScott Long 
4912b83592fSScott Long 	/*
4922b83592fSScott Long 	 * Increment the reference count on the peripheral
4932b83592fSScott Long 	 * while we wait for our lock attempt to succeed
4942b83592fSScott Long 	 * to ensure the peripheral doesn't disappear out
4952b83592fSScott Long 	 * from user us while we sleep.
4962b83592fSScott Long 	 */
4972b83592fSScott Long 
49899e7a4adSScott Long 	if (cam_periph_acquire(periph) != 0)
4992b83592fSScott Long 		return (ENXIO);
5002b83592fSScott Long 
501227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
5022b83592fSScott Long 	while ((periph->flags & CAM_PERIPH_LOCKED) != 0) {
5032b83592fSScott Long 		periph->flags |= CAM_PERIPH_LOCK_WANTED;
504227d67aaSAlexander Motin 		if ((error = cam_periph_sleep(periph, periph, priority,
5051fa738c2SJohn Baldwin 		    "caplck", 0)) != 0) {
50624ebf566SEdward Tomasz Napierala 			cam_periph_release_locked(periph);
5072b83592fSScott Long 			return (error);
5082b83592fSScott Long 		}
509aed9c88cSAlexander Motin 		if (periph->flags & CAM_PERIPH_INVALID) {
510aed9c88cSAlexander Motin 			cam_periph_release_locked(periph);
511aed9c88cSAlexander Motin 			return (ENXIO);
512aed9c88cSAlexander Motin 		}
5132b83592fSScott Long 	}
5142b83592fSScott Long 
5152b83592fSScott Long 	periph->flags |= CAM_PERIPH_LOCKED;
5162b83592fSScott Long 	return (0);
5172b83592fSScott Long }
5182b83592fSScott Long 
5192b83592fSScott Long void
5202b83592fSScott Long cam_periph_unhold(struct cam_periph *periph)
5212b83592fSScott Long {
5222b83592fSScott Long 
523227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
5242b83592fSScott Long 
5252b83592fSScott Long 	periph->flags &= ~CAM_PERIPH_LOCKED;
5262b83592fSScott Long 	if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) {
5272b83592fSScott Long 		periph->flags &= ~CAM_PERIPH_LOCK_WANTED;
5282b83592fSScott Long 		wakeup(periph);
5292b83592fSScott Long 	}
5302b83592fSScott Long 
53124ebf566SEdward Tomasz Napierala 	cam_periph_release_locked(periph);
5322b83592fSScott Long }
5332b83592fSScott Long 
53490bcc81bSAlexander Motin void
53590bcc81bSAlexander Motin cam_periph_hold_boot(struct cam_periph *periph)
53690bcc81bSAlexander Motin {
53790bcc81bSAlexander Motin 
53890bcc81bSAlexander Motin 	root_mount_hold_token(periph->periph_name, &periph->periph_rootmount);
53990bcc81bSAlexander Motin }
54090bcc81bSAlexander Motin 
54190bcc81bSAlexander Motin void
54290bcc81bSAlexander Motin cam_periph_release_boot(struct cam_periph *periph)
54390bcc81bSAlexander Motin {
54490bcc81bSAlexander Motin 
54590bcc81bSAlexander Motin 	root_mount_rel(&periph->periph_rootmount);
54690bcc81bSAlexander Motin }
54790bcc81bSAlexander Motin 
5488b8a9b1dSJustin T. Gibbs /*
5498b8a9b1dSJustin T. Gibbs  * Look for the next unit number that is not currently in use for this
5508b8a9b1dSJustin T. Gibbs  * peripheral type starting at "newunit".  Also exclude unit numbers that
5518b8a9b1dSJustin T. Gibbs  * are reserved by for future "hardwiring" unless we already know that this
5528b8a9b1dSJustin T. Gibbs  * is a potential wired device.  Only assume that the device is "wired" the
5538b8a9b1dSJustin T. Gibbs  * first time through the loop since after that we'll be looking at unit
5548b8a9b1dSJustin T. Gibbs  * numbers that did not match a wiring entry.
5558b8a9b1dSJustin T. Gibbs  */
5568b8a9b1dSJustin T. Gibbs static u_int
557d836c48eSWarner Losh camperiphnextunit(struct periph_driver *p_drv, u_int newunit, bool wired,
558501468a5SKenneth D. Merry 		  path_id_t pathid, target_id_t target, lun_id_t lun)
5598b8a9b1dSJustin T. Gibbs {
5608b8a9b1dSJustin T. Gibbs 	struct	cam_periph *periph;
5612398f0cdSPeter Wemm 	char	*periph_name;
5622398f0cdSPeter Wemm 	int	i, val, dunit, r;
5632398f0cdSPeter Wemm 	const char *dname, *strval;
5648b8a9b1dSJustin T. Gibbs 
5658b8a9b1dSJustin T. Gibbs 	periph_name = p_drv->driver_name;
5668b8a9b1dSJustin T. Gibbs 	for (;;newunit++) {
5678b8a9b1dSJustin T. Gibbs 		for (periph = TAILQ_FIRST(&p_drv->units);
5688b8a9b1dSJustin T. Gibbs 		     periph != NULL && periph->unit_number != newunit;
5698b8a9b1dSJustin T. Gibbs 		     periph = TAILQ_NEXT(periph, unit_links))
5708b8a9b1dSJustin T. Gibbs 			;
5718b8a9b1dSJustin T. Gibbs 
5728b8a9b1dSJustin T. Gibbs 		if (periph != NULL && periph->unit_number == newunit) {
573d836c48eSWarner Losh 			if (wired) {
574f0d9af51SMatt Jacob 				xpt_print(periph->path, "Duplicate Wired "
575f0d9af51SMatt Jacob 				    "Device entry!\n");
576f0d9af51SMatt Jacob 				xpt_print(periph->path, "Second device (%s "
577f0d9af51SMatt Jacob 				    "device at scbus%d target %d lun %d) will "
578f0d9af51SMatt Jacob 				    "not be wired\n", periph_name, pathid,
579f0d9af51SMatt Jacob 				    target, lun);
580d836c48eSWarner Losh 				wired = false;
5818b8a9b1dSJustin T. Gibbs 			}
5828b8a9b1dSJustin T. Gibbs 			continue;
5838b8a9b1dSJustin T. Gibbs 		}
58475f51904SPeter Wemm 		if (wired)
58575f51904SPeter Wemm 			break;
5868b8a9b1dSJustin T. Gibbs 
5878b8a9b1dSJustin T. Gibbs 		/*
58800f79c97SWarner Losh 		 * Don't allow the mere presence of any attributes of a device
58900f79c97SWarner Losh 		 * means that it is for a wired down entry. Instead, insist that
59000f79c97SWarner Losh 		 * one of the matching criteria from camperiphunit be present
59100f79c97SWarner Losh 		 * for the device.
5928b8a9b1dSJustin T. Gibbs 		 */
5932398f0cdSPeter Wemm 		i = 0;
5942398f0cdSPeter Wemm 		dname = periph_name;
5952398f0cdSPeter Wemm 		for (;;) {
5962398f0cdSPeter Wemm 			r = resource_find_dev(&i, dname, &dunit, NULL, NULL);
5972398f0cdSPeter Wemm 			if (r != 0)
5982398f0cdSPeter Wemm 				break;
599bee0133fSWarner Losh 
600bee0133fSWarner Losh 			if (newunit != dunit)
60175f51904SPeter Wemm 				continue;
602577f9aa2SWarner Losh 			if (resource_string_value(dname, dunit, "sn", &strval) == 0 ||
603577f9aa2SWarner Losh 			    resource_int_value(dname, dunit, "lun", &val) == 0 ||
604710a519eSWarner Losh 			    resource_int_value(dname, dunit, "target", &val) == 0 ||
605bee0133fSWarner Losh 			    resource_string_value(dname, dunit, "at", &strval) == 0)
6068b8a9b1dSJustin T. Gibbs 				break;
6078b8a9b1dSJustin T. Gibbs 		}
6082398f0cdSPeter Wemm 		if (r != 0)
6098b8a9b1dSJustin T. Gibbs 			break;
6108b8a9b1dSJustin T. Gibbs 	}
6118b8a9b1dSJustin T. Gibbs 	return (newunit);
6128b8a9b1dSJustin T. Gibbs }
6138b8a9b1dSJustin T. Gibbs 
6148b8a9b1dSJustin T. Gibbs static u_int
6158b8a9b1dSJustin T. Gibbs camperiphunit(struct periph_driver *p_drv, path_id_t pathid,
616577f9aa2SWarner Losh     target_id_t target, lun_id_t lun, const char *sn)
6178b8a9b1dSJustin T. Gibbs {
6183846662dSWarner Losh 	bool	wired = false;
6198b8a9b1dSJustin T. Gibbs 	u_int	unit;
620d836c48eSWarner Losh 	int	i, val, dunit;
6212398f0cdSPeter Wemm 	const char *dname, *strval;
6222398f0cdSPeter Wemm 	char	pathbuf[32], *periph_name;
6238b8a9b1dSJustin T. Gibbs 
62475f51904SPeter Wemm 	periph_name = p_drv->driver_name;
62575f51904SPeter Wemm 	snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid);
626c1b81613SPeter Wemm 	unit = 0;
6272398f0cdSPeter Wemm 	i = 0;
6282398f0cdSPeter Wemm 	dname = periph_name;
629272e4f53SWarner Losh 
630272e4f53SWarner Losh 	for (wired = false; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0;
631272e4f53SWarner Losh 	     wired = false) {
63275f51904SPeter Wemm 		if (resource_string_value(dname, dunit, "at", &strval) == 0) {
63375f51904SPeter Wemm 			if (strcmp(strval, pathbuf) != 0)
6348b8a9b1dSJustin T. Gibbs 				continue;
635d836c48eSWarner Losh 			wired = true;
6368b8a9b1dSJustin T. Gibbs 		}
63775f51904SPeter Wemm 		if (resource_int_value(dname, dunit, "target", &val) == 0) {
63875f51904SPeter Wemm 			if (val != target)
6398b8a9b1dSJustin T. Gibbs 				continue;
640d836c48eSWarner Losh 			wired = true;
6418b8a9b1dSJustin T. Gibbs 		}
64275f51904SPeter Wemm 		if (resource_int_value(dname, dunit, "lun", &val) == 0) {
64375f51904SPeter Wemm 			if (val != lun)
6448b8a9b1dSJustin T. Gibbs 				continue;
645d836c48eSWarner Losh 			wired = true;
6468b8a9b1dSJustin T. Gibbs 		}
647577f9aa2SWarner Losh 		if (resource_string_value(dname, dunit, "sn", &strval) == 0) {
648577f9aa2SWarner Losh 			if (sn == NULL || strcmp(strval, sn) != 0)
649577f9aa2SWarner Losh 				continue;
650d836c48eSWarner Losh 			wired = true;
651577f9aa2SWarner Losh 		}
652d836c48eSWarner Losh 		if (wired) {
65375f51904SPeter Wemm 			unit = dunit;
6548b8a9b1dSJustin T. Gibbs 			break;
6558b8a9b1dSJustin T. Gibbs 		}
6568b8a9b1dSJustin T. Gibbs 	}
6578b8a9b1dSJustin T. Gibbs 
6588b8a9b1dSJustin T. Gibbs 	/*
6598b8a9b1dSJustin T. Gibbs 	 * Either start from 0 looking for the next unit or from
66075f51904SPeter Wemm 	 * the unit number given in the resource config.  This way,
6618b8a9b1dSJustin T. Gibbs 	 * if we have wildcard matches, we don't return the same
6628b8a9b1dSJustin T. Gibbs 	 * unit number twice.
6638b8a9b1dSJustin T. Gibbs 	 */
664c1b81613SPeter Wemm 	unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun);
6658b8a9b1dSJustin T. Gibbs 
6668b8a9b1dSJustin T. Gibbs 	return (unit);
6678b8a9b1dSJustin T. Gibbs }
6688b8a9b1dSJustin T. Gibbs 
6698b8a9b1dSJustin T. Gibbs void
6708b8a9b1dSJustin T. Gibbs cam_periph_invalidate(struct cam_periph *periph)
6718b8a9b1dSJustin T. Gibbs {
6728b8a9b1dSJustin T. Gibbs 
673227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
674ee9c90c7SKenneth D. Merry 	/*
6756fda2c54SWarner Losh 	 * We only tear down the device the first time a peripheral is
67677dc25ccSScott Long 	 * invalidated.
677ee9c90c7SKenneth D. Merry 	 */
678d38c0e53SAlexander Motin 	if ((periph->flags & CAM_PERIPH_INVALID) != 0)
679d38c0e53SAlexander Motin 		return;
680ee9c90c7SKenneth D. Merry 
681d38c0e53SAlexander Motin 	CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph invalidated\n"));
6825d01277fSScott Long 	if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting) {
6835d01277fSScott Long 		struct sbuf sb;
684da0d7209SScott Long 		char buffer[160];
6855d01277fSScott Long 
686da0d7209SScott Long 		sbuf_new(&sb, buffer, 160, SBUF_FIXEDLEN);
6875d01277fSScott Long 		xpt_denounce_periph_sbuf(periph, &sb);
6885d01277fSScott Long 		sbuf_finish(&sb);
6895d01277fSScott Long 		sbuf_putbuf(&sb);
6905d01277fSScott Long 	}
6918b8a9b1dSJustin T. Gibbs 	periph->flags |= CAM_PERIPH_INVALID;
6928b8a9b1dSJustin T. Gibbs 	periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND;
693d38c0e53SAlexander Motin 	if (periph->periph_oninval != NULL)
694d38c0e53SAlexander Motin 		periph->periph_oninval(periph);
695d38c0e53SAlexander Motin 	cam_periph_release_locked(periph);
6968b8a9b1dSJustin T. Gibbs }
6978b8a9b1dSJustin T. Gibbs 
6988b8a9b1dSJustin T. Gibbs static void
6998b8a9b1dSJustin T. Gibbs camperiphfree(struct cam_periph *periph)
7008b8a9b1dSJustin T. Gibbs {
7018b8a9b1dSJustin T. Gibbs 	struct periph_driver **p_drv;
70259fe7664SKenneth D. Merry 	struct periph_driver *drv;
7038b8a9b1dSJustin T. Gibbs 
704227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
705227d67aaSAlexander Motin 	KASSERT(periph->periph_allocating == 0, ("%s%d: freed while allocating",
706227d67aaSAlexander Motin 	    periph->periph_name, periph->unit_number));
7070b7c27b9SPeter Wemm 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
7088b8a9b1dSJustin T. Gibbs 		if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0)
7098b8a9b1dSJustin T. Gibbs 			break;
7108b8a9b1dSJustin T. Gibbs 	}
711661658a6SScott Long 	if (*p_drv == NULL) {
712661658a6SScott Long 		printf("camperiphfree: attempt to free non-existant periph\n");
713661658a6SScott Long 		return;
714661658a6SScott Long 	}
71559fe7664SKenneth D. Merry 	/*
71659fe7664SKenneth D. Merry 	 * Cache a pointer to the periph_driver structure.  If a
71759fe7664SKenneth D. Merry 	 * periph_driver is added or removed from the array (see
71859fe7664SKenneth D. Merry 	 * periphdriver_register()) while we drop the toplogy lock
71959fe7664SKenneth D. Merry 	 * below, p_drv may change.  This doesn't protect against this
72059fe7664SKenneth D. Merry 	 * particular periph_driver going away.  That will require full
72159fe7664SKenneth D. Merry 	 * reference counting in the periph_driver infrastructure.
72259fe7664SKenneth D. Merry 	 */
72359fe7664SKenneth D. Merry 	drv = *p_drv;
7248b8a9b1dSJustin T. Gibbs 
725ea37f519SKenneth D. Merry 	/*
72633a38f74SKenneth D. Merry 	 * We need to set this flag before dropping the topology lock, to
72733a38f74SKenneth D. Merry 	 * let anyone who is traversing the list that this peripheral is
72833a38f74SKenneth D. Merry 	 * about to be freed, and there will be no more reference count
72933a38f74SKenneth D. Merry 	 * checks.
73033a38f74SKenneth D. Merry 	 */
73133a38f74SKenneth D. Merry 	periph->flags |= CAM_PERIPH_FREE;
73233a38f74SKenneth D. Merry 
73333a38f74SKenneth D. Merry 	/*
734ea37f519SKenneth D. Merry 	 * The peripheral destructor semantics dictate calling with only the
735ea37f519SKenneth D. Merry 	 * SIM mutex held.  Since it might sleep, it should not be called
736ea37f519SKenneth D. Merry 	 * with the topology lock held.
737ea37f519SKenneth D. Merry 	 */
738f3548746SScott Long 	xpt_unlock_buses();
7398b8a9b1dSJustin T. Gibbs 
740ea37f519SKenneth D. Merry 	/*
741ea37f519SKenneth D. Merry 	 * We need to call the peripheral destructor prior to removing the
742ea37f519SKenneth D. Merry 	 * peripheral from the list.  Otherwise, we risk running into a
743ea37f519SKenneth D. Merry 	 * scenario where the peripheral unit number may get reused
744ea37f519SKenneth D. Merry 	 * (because it has been removed from the list), but some resources
745ea37f519SKenneth D. Merry 	 * used by the peripheral are still hanging around.  In particular,
746ea37f519SKenneth D. Merry 	 * the devfs nodes used by some peripherals like the pass(4) driver
747ea37f519SKenneth D. Merry 	 * aren't fully cleaned up until the destructor is run.  If the
748ea37f519SKenneth D. Merry 	 * unit number is reused before the devfs instance is fully gone,
749ea37f519SKenneth D. Merry 	 * devfs will panic.
750ea37f519SKenneth D. Merry 	 */
751f3548746SScott Long 	if (periph->periph_dtor != NULL)
752f3548746SScott Long 		periph->periph_dtor(periph);
753ea37f519SKenneth D. Merry 
754ea37f519SKenneth D. Merry 	/*
75581490edaSWarner Losh 	 * The peripheral list is protected by the topology lock. We have to
75681490edaSWarner Losh 	 * remove the periph from the drv list before we call deferred_ac. The
75781490edaSWarner Losh 	 * AC_FOUND_DEVICE callback won't create a new periph if it's still there.
758ea37f519SKenneth D. Merry 	 */
759ea37f519SKenneth D. Merry 	xpt_lock_buses();
760ea37f519SKenneth D. Merry 
76159fe7664SKenneth D. Merry 	TAILQ_REMOVE(&drv->units, periph, unit_links);
76259fe7664SKenneth D. Merry 	drv->generation++;
763ea37f519SKenneth D. Merry 
764a29779e8SAlexander Motin 	xpt_remove_periph(periph);
765ea37f519SKenneth D. Merry 
766ea37f519SKenneth D. Merry 	xpt_unlock_buses();
767de6a705eSMarius Strobl 	if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting)
7688d36a71bSAlexander Motin 		xpt_print(periph->path, "Periph destroyed\n");
769de6a705eSMarius Strobl 	else
77022c7d606SAlexander Motin 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n"));
7718b8a9b1dSJustin T. Gibbs 
7728b8a9b1dSJustin T. Gibbs 	if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) {
7738b8a9b1dSJustin T. Gibbs 		union ccb ccb;
7748b8a9b1dSJustin T. Gibbs 		void *arg;
7758b8a9b1dSJustin T. Gibbs 
776076686feSEdward Tomasz Napierala 		memset(&ccb, 0, sizeof(ccb));
7778b8a9b1dSJustin T. Gibbs 		switch (periph->deferred_ac) {
7788b8a9b1dSJustin T. Gibbs 		case AC_FOUND_DEVICE:
7798b8a9b1dSJustin T. Gibbs 			ccb.ccb_h.func_code = XPT_GDEV_TYPE;
780bbfa4aa1SAlexander Motin 			xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
7818b8a9b1dSJustin T. Gibbs 			xpt_action(&ccb);
7828b8a9b1dSJustin T. Gibbs 			arg = &ccb;
7838b8a9b1dSJustin T. Gibbs 			break;
7848b8a9b1dSJustin T. Gibbs 		case AC_PATH_REGISTERED:
785762a7f4fSWarner Losh 			xpt_path_inq(&ccb.cpi, periph->path);
7868b8a9b1dSJustin T. Gibbs 			arg = &ccb;
7878b8a9b1dSJustin T. Gibbs 			break;
7888b8a9b1dSJustin T. Gibbs 		default:
7898b8a9b1dSJustin T. Gibbs 			arg = NULL;
7908b8a9b1dSJustin T. Gibbs 			break;
7918b8a9b1dSJustin T. Gibbs 		}
7928b8a9b1dSJustin T. Gibbs 		periph->deferred_callback(NULL, periph->deferred_ac,
7938b8a9b1dSJustin T. Gibbs 					  periph->path, arg);
7948b8a9b1dSJustin T. Gibbs 	}
7958b8a9b1dSJustin T. Gibbs 	xpt_free_path(periph->path);
796362abc44STai-hwa Liang 	free(periph, M_CAMPERIPH);
797f3548746SScott Long 	xpt_lock_buses();
7988b8a9b1dSJustin T. Gibbs }
7998b8a9b1dSJustin T. Gibbs 
8008b8a9b1dSJustin T. Gibbs /*
8018b8a9b1dSJustin T. Gibbs  * Map user virtual pointers into kernel virtual address space, so we can
80295fbded6SScott Long  * access the memory.  This is now a generic function that centralizes most
80395fbded6SScott Long  * of the sanity checks on the data flags, if any.
804cd853791SKonstantin Belousov  * This also only works for up to maxphys memory.  Since we use
8058b8a9b1dSJustin T. Gibbs  * buffers to map stuff in and out, we're limited to the buffer size.
8068b8a9b1dSJustin T. Gibbs  */
8078b8a9b1dSJustin T. Gibbs int
808de239312SAlexander Motin cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo,
809de239312SAlexander Motin     u_int maxmap)
8108b8a9b1dSJustin T. Gibbs {
811b059686aSAlexander Motin 	int numbufs, i;
8128b8a9b1dSJustin T. Gibbs 	u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
8138b8a9b1dSJustin T. Gibbs 	u_int32_t lengths[CAM_PERIPH_MAXMAPS];
8148b8a9b1dSJustin T. Gibbs 	u_int32_t dirs[CAM_PERIPH_MAXMAPS];
8158b8a9b1dSJustin T. Gibbs 
816b059686aSAlexander Motin 	bzero(mapinfo, sizeof(*mapinfo));
817de239312SAlexander Motin 	if (maxmap == 0)
818de239312SAlexander Motin 		maxmap = DFLTPHYS;	/* traditional default */
819cd853791SKonstantin Belousov 	else if (maxmap > maxphys)
820cd853791SKonstantin Belousov 		maxmap = maxphys;	/* for safety */
8218b8a9b1dSJustin T. Gibbs 	switch(ccb->ccb_h.func_code) {
8228b8a9b1dSJustin T. Gibbs 	case XPT_DEV_MATCH:
8238b8a9b1dSJustin T. Gibbs 		if (ccb->cdm.match_buf_len == 0) {
8248b8a9b1dSJustin T. Gibbs 			printf("cam_periph_mapmem: invalid match buffer "
8258b8a9b1dSJustin T. Gibbs 			       "length 0\n");
8268b8a9b1dSJustin T. Gibbs 			return(EINVAL);
8278b8a9b1dSJustin T. Gibbs 		}
8288b8a9b1dSJustin T. Gibbs 		if (ccb->cdm.pattern_buf_len > 0) {
8298b8a9b1dSJustin T. Gibbs 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
8308b8a9b1dSJustin T. Gibbs 			lengths[0] = ccb->cdm.pattern_buf_len;
8318b8a9b1dSJustin T. Gibbs 			dirs[0] = CAM_DIR_OUT;
8328b8a9b1dSJustin T. Gibbs 			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
8338b8a9b1dSJustin T. Gibbs 			lengths[1] = ccb->cdm.match_buf_len;
8348b8a9b1dSJustin T. Gibbs 			dirs[1] = CAM_DIR_IN;
8358b8a9b1dSJustin T. Gibbs 			numbufs = 2;
8368b8a9b1dSJustin T. Gibbs 		} else {
8378b8a9b1dSJustin T. Gibbs 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
8388b8a9b1dSJustin T. Gibbs 			lengths[0] = ccb->cdm.match_buf_len;
8398b8a9b1dSJustin T. Gibbs 			dirs[0] = CAM_DIR_IN;
8408b8a9b1dSJustin T. Gibbs 			numbufs = 1;
8418b8a9b1dSJustin T. Gibbs 		}
84252c9ce25SScott Long 		/*
84352c9ce25SScott Long 		 * This request will not go to the hardware, no reason
844cd853791SKonstantin Belousov 		 * to be so strict. vmapbuf() is able to map up to maxphys.
84552c9ce25SScott Long 		 */
846cd853791SKonstantin Belousov 		maxmap = maxphys;
8478b8a9b1dSJustin T. Gibbs 		break;
8488b8a9b1dSJustin T. Gibbs 	case XPT_SCSI_IO:
84987cfaf0eSJustin T. Gibbs 	case XPT_CONT_TARGET_IO:
8508b8a9b1dSJustin T. Gibbs 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
8518b8a9b1dSJustin T. Gibbs 			return(0);
85295fbded6SScott Long 		if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
85395fbded6SScott Long 			return (EINVAL);
8548b8a9b1dSJustin T. Gibbs 		data_ptrs[0] = &ccb->csio.data_ptr;
85579d49a06SKenneth D. Merry 		lengths[0] = ccb->csio.dxfer_len;
8568b8a9b1dSJustin T. Gibbs 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
8578b8a9b1dSJustin T. Gibbs 		numbufs = 1;
8588b8a9b1dSJustin T. Gibbs 		break;
85952c9ce25SScott Long 	case XPT_ATA_IO:
86052c9ce25SScott Long 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
86152c9ce25SScott Long 			return(0);
86295fbded6SScott Long 		if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
86395fbded6SScott Long 			return (EINVAL);
86452c9ce25SScott Long 		data_ptrs[0] = &ccb->ataio.data_ptr;
86552c9ce25SScott Long 		lengths[0] = ccb->ataio.dxfer_len;
86652c9ce25SScott Long 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
86752c9ce25SScott Long 		numbufs = 1;
86852c9ce25SScott Long 		break;
869a94a63f0SWarner Losh 	case XPT_MMC_IO:
870a94a63f0SWarner Losh 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
871a94a63f0SWarner Losh 			return(0);
872a94a63f0SWarner Losh 		/* Two mappings: one for cmd->data and one for cmd->data->data */
873a94a63f0SWarner Losh 		data_ptrs[0] = (unsigned char **)&ccb->mmcio.cmd.data;
874a94a63f0SWarner Losh 		lengths[0] = sizeof(struct mmc_data *);
875a94a63f0SWarner Losh 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
876a94a63f0SWarner Losh 		data_ptrs[1] = (unsigned char **)&ccb->mmcio.cmd.data->data;
877a94a63f0SWarner Losh 		lengths[1] = ccb->mmcio.cmd.data->len;
878a94a63f0SWarner Losh 		dirs[1] = ccb->ccb_h.flags & CAM_DIR_MASK;
879a94a63f0SWarner Losh 		numbufs = 2;
880a94a63f0SWarner Losh 		break;
88106e79492SKenneth D. Merry 	case XPT_SMP_IO:
88206e79492SKenneth D. Merry 		data_ptrs[0] = &ccb->smpio.smp_request;
88306e79492SKenneth D. Merry 		lengths[0] = ccb->smpio.smp_request_len;
88406e79492SKenneth D. Merry 		dirs[0] = CAM_DIR_OUT;
88506e79492SKenneth D. Merry 		data_ptrs[1] = &ccb->smpio.smp_response;
88606e79492SKenneth D. Merry 		lengths[1] = ccb->smpio.smp_response_len;
88706e79492SKenneth D. Merry 		dirs[1] = CAM_DIR_IN;
88806e79492SKenneth D. Merry 		numbufs = 2;
88906e79492SKenneth D. Merry 		break;
890df424515SWarner Losh 	case XPT_NVME_IO:
891df424515SWarner Losh 	case XPT_NVME_ADMIN:
892df424515SWarner Losh 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
893df424515SWarner Losh 			return (0);
894df424515SWarner Losh 		if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
895df424515SWarner Losh 			return (EINVAL);
896df424515SWarner Losh 		data_ptrs[0] = &ccb->nvmeio.data_ptr;
897df424515SWarner Losh 		lengths[0] = ccb->nvmeio.dxfer_len;
898df424515SWarner Losh 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
899df424515SWarner Losh 		numbufs = 1;
900df424515SWarner Losh 		break;
9013501942bSJustin T. Gibbs 	case XPT_DEV_ADVINFO:
9023501942bSJustin T. Gibbs 		if (ccb->cdai.bufsiz == 0)
9037c103ddeSKenneth D. Merry 			return (0);
9047c103ddeSKenneth D. Merry 
9053501942bSJustin T. Gibbs 		data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
9063501942bSJustin T. Gibbs 		lengths[0] = ccb->cdai.bufsiz;
90706e79492SKenneth D. Merry 		dirs[0] = CAM_DIR_IN;
90806e79492SKenneth D. Merry 		numbufs = 1;
9097c103ddeSKenneth D. Merry 
9107c103ddeSKenneth D. Merry 		/*
9117c103ddeSKenneth D. Merry 		 * This request will not go to the hardware, no reason
912cd853791SKonstantin Belousov 		 * to be so strict. vmapbuf() is able to map up to maxphys.
9137c103ddeSKenneth D. Merry 		 */
914cd853791SKonstantin Belousov 		maxmap = maxphys;
91506e79492SKenneth D. Merry 		break;
9168b8a9b1dSJustin T. Gibbs 	default:
9178b8a9b1dSJustin T. Gibbs 		return(EINVAL);
9188b8a9b1dSJustin T. Gibbs 		break; /* NOTREACHED */
9198b8a9b1dSJustin T. Gibbs 	}
9208b8a9b1dSJustin T. Gibbs 
9218b8a9b1dSJustin T. Gibbs 	/*
92279d49a06SKenneth D. Merry 	 * Check the transfer length and permissions first, so we don't
92379d49a06SKenneth D. Merry 	 * have to unmap any previously mapped buffers.
9248b8a9b1dSJustin T. Gibbs 	 */
9258b8a9b1dSJustin T. Gibbs 	for (i = 0; i < numbufs; i++) {
926c389a786SAlexander Motin 		if (lengths[i] > maxmap) {
927c389a786SAlexander Motin 			printf("cam_periph_mapmem: attempt to map %lu bytes, "
928c389a786SAlexander Motin 			       "which is greater than %lu\n",
929c389a786SAlexander Motin 			       (long)(lengths[i]), (u_long)maxmap);
930c389a786SAlexander Motin 			return (E2BIG);
931c389a786SAlexander Motin 		}
93279d49a06SKenneth D. Merry 	}
93379d49a06SKenneth D. Merry 
93479d49a06SKenneth D. Merry 	/*
935cef367e6SEitan Adler 	 * This keeps the kernel stack of current thread from getting
93695fbded6SScott Long 	 * swapped.  In low-memory situations where the kernel stack might
93795fbded6SScott Long 	 * otherwise get swapped out, this holds it and allows the thread
93895fbded6SScott Long 	 * to make progress and release the kernel mapped pages sooner.
93995fbded6SScott Long 	 *
94079d49a06SKenneth D. Merry 	 * XXX KDM should I use P_NOSWAP instead?
94179d49a06SKenneth D. Merry 	 */
9420cbbb7bfSPeter Wemm 	PHOLD(curproc);
94379d49a06SKenneth D. Merry 
94479d49a06SKenneth D. Merry 	for (i = 0; i < numbufs; i++) {
945b059686aSAlexander Motin 		/* Save the user's data address. */
946b059686aSAlexander Motin 		mapinfo->orig[i] = *data_ptrs[i];
947b059686aSAlexander Motin 
948b059686aSAlexander Motin 		/*
949b059686aSAlexander Motin 		 * For small buffers use malloc+copyin/copyout instead of
950b059686aSAlexander Motin 		 * mapping to KVA to avoid expensive TLB shootdowns.  For
951b059686aSAlexander Motin 		 * small allocations malloc is backed by UMA, and so much
952b059686aSAlexander Motin 		 * cheaper on SMP systems.
953b059686aSAlexander Motin 		 */
9549093e27cSAlexander Motin 		if (lengths[i] <= periph_mapmem_thresh &&
955b059686aSAlexander Motin 		    ccb->ccb_h.func_code != XPT_MMC_IO) {
956b059686aSAlexander Motin 			*data_ptrs[i] = malloc(lengths[i], M_CAMPERIPH,
957b059686aSAlexander Motin 			    M_WAITOK);
958b059686aSAlexander Motin 			if (dirs[i] != CAM_DIR_IN) {
959b059686aSAlexander Motin 				if (copyin(mapinfo->orig[i], *data_ptrs[i],
960b059686aSAlexander Motin 				    lengths[i]) != 0) {
961b059686aSAlexander Motin 					free(*data_ptrs[i], M_CAMPERIPH);
962b059686aSAlexander Motin 					*data_ptrs[i] = mapinfo->orig[i];
963b059686aSAlexander Motin 					goto fail;
964b059686aSAlexander Motin 				}
965b059686aSAlexander Motin 			} else
966b059686aSAlexander Motin 				bzero(*data_ptrs[i], lengths[i]);
967b059686aSAlexander Motin 			continue;
968b059686aSAlexander Motin 		}
969b059686aSAlexander Motin 
9708b8a9b1dSJustin T. Gibbs 		/*
9718b8a9b1dSJustin T. Gibbs 		 * Get the buffer.
9728b8a9b1dSJustin T. Gibbs 		 */
973756a5412SGleb Smirnoff 		mapinfo->bp[i] = uma_zalloc(pbuf_zone, M_WAITOK);
9748b8a9b1dSJustin T. Gibbs 
97521144e3bSPoul-Henning Kamp 		/* set the direction */
976b059686aSAlexander Motin 		mapinfo->bp[i]->b_iocmd = (dirs[i] == CAM_DIR_OUT) ?
977b059686aSAlexander Motin 		    BIO_WRITE : BIO_READ;
9788b8a9b1dSJustin T. Gibbs 
979450a2e2aSWarner Losh 		/* Map the buffer into kernel memory. */
98044ca4575SBrooks Davis 		if (vmapbuf(mapinfo->bp[i], *data_ptrs[i], lengths[i], 1) < 0) {
981756a5412SGleb Smirnoff 			uma_zfree(pbuf_zone, mapinfo->bp[i]);
982b059686aSAlexander Motin 			goto fail;
9832d5c7e45SMatthew Dillon 		}
9848b8a9b1dSJustin T. Gibbs 
9858b8a9b1dSJustin T. Gibbs 		/* set our pointer to the new mapped area */
9868b8a9b1dSJustin T. Gibbs 		*data_ptrs[i] = mapinfo->bp[i]->b_data;
9878b8a9b1dSJustin T. Gibbs 	}
9888b8a9b1dSJustin T. Gibbs 
989a62525f3SMatt Jacob 	/*
990a62525f3SMatt Jacob 	 * Now that we've gotten this far, change ownership to the kernel
991a62525f3SMatt Jacob 	 * of the buffers so that we don't run afoul of returning to user
992a62525f3SMatt Jacob 	 * space with locks (on the buffer) held.
993a62525f3SMatt Jacob 	 */
994a62525f3SMatt Jacob 	for (i = 0; i < numbufs; i++) {
995b059686aSAlexander Motin 		if (mapinfo->bp[i])
996a62525f3SMatt Jacob 			BUF_KERNPROC(mapinfo->bp[i]);
997a62525f3SMatt Jacob 	}
998a62525f3SMatt Jacob 
999b059686aSAlexander Motin 	mapinfo->num_bufs_used = numbufs;
10008b8a9b1dSJustin T. Gibbs 	return(0);
1001b059686aSAlexander Motin 
1002b059686aSAlexander Motin fail:
1003b059686aSAlexander Motin 	for (i--; i >= 0; i--) {
1004b059686aSAlexander Motin 		if (mapinfo->bp[i]) {
1005b059686aSAlexander Motin 			vunmapbuf(mapinfo->bp[i]);
1006b059686aSAlexander Motin 			uma_zfree(pbuf_zone, mapinfo->bp[i]);
1007b059686aSAlexander Motin 		} else
1008b059686aSAlexander Motin 			free(*data_ptrs[i], M_CAMPERIPH);
1009b059686aSAlexander Motin 		*data_ptrs[i] = mapinfo->orig[i];
1010b059686aSAlexander Motin 	}
1011b059686aSAlexander Motin 	PRELE(curproc);
1012b059686aSAlexander Motin 	return(EACCES);
10138b8a9b1dSJustin T. Gibbs }
10148b8a9b1dSJustin T. Gibbs 
10158b8a9b1dSJustin T. Gibbs /*
10168b8a9b1dSJustin T. Gibbs  * Unmap memory segments mapped into kernel virtual address space by
10178b8a9b1dSJustin T. Gibbs  * cam_periph_mapmem().
10188b8a9b1dSJustin T. Gibbs  */
10198b8a9b1dSJustin T. Gibbs void
10208b8a9b1dSJustin T. Gibbs cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
10218b8a9b1dSJustin T. Gibbs {
10228b8a9b1dSJustin T. Gibbs 	int numbufs, i;
10238b8a9b1dSJustin T. Gibbs 	u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
1024b059686aSAlexander Motin 	u_int32_t lengths[CAM_PERIPH_MAXMAPS];
1025b059686aSAlexander Motin 	u_int32_t dirs[CAM_PERIPH_MAXMAPS];
10268b8a9b1dSJustin T. Gibbs 
10278b8a9b1dSJustin T. Gibbs 	if (mapinfo->num_bufs_used <= 0) {
102895fbded6SScott Long 		/* nothing to free and the process wasn't held. */
10298b8a9b1dSJustin T. Gibbs 		return;
10308b8a9b1dSJustin T. Gibbs 	}
10318b8a9b1dSJustin T. Gibbs 
10328b8a9b1dSJustin T. Gibbs 	switch (ccb->ccb_h.func_code) {
10338b8a9b1dSJustin T. Gibbs 	case XPT_DEV_MATCH:
1034b059686aSAlexander Motin 		if (ccb->cdm.pattern_buf_len > 0) {
10358b8a9b1dSJustin T. Gibbs 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
1036b059686aSAlexander Motin 			lengths[0] = ccb->cdm.pattern_buf_len;
1037b059686aSAlexander Motin 			dirs[0] = CAM_DIR_OUT;
10388b8a9b1dSJustin T. Gibbs 			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
1039b059686aSAlexander Motin 			lengths[1] = ccb->cdm.match_buf_len;
1040b059686aSAlexander Motin 			dirs[1] = CAM_DIR_IN;
1041b059686aSAlexander Motin 			numbufs = 2;
1042b059686aSAlexander Motin 		} else {
1043b059686aSAlexander Motin 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
1044b059686aSAlexander Motin 			lengths[0] = ccb->cdm.match_buf_len;
1045b059686aSAlexander Motin 			dirs[0] = CAM_DIR_IN;
1046b059686aSAlexander Motin 			numbufs = 1;
10478b8a9b1dSJustin T. Gibbs 		}
10488b8a9b1dSJustin T. Gibbs 		break;
10498b8a9b1dSJustin T. Gibbs 	case XPT_SCSI_IO:
10509911ecf9SJustin T. Gibbs 	case XPT_CONT_TARGET_IO:
10518b8a9b1dSJustin T. Gibbs 		data_ptrs[0] = &ccb->csio.data_ptr;
1052b059686aSAlexander Motin 		lengths[0] = ccb->csio.dxfer_len;
1053b059686aSAlexander Motin 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
1054b059686aSAlexander Motin 		numbufs = 1;
10558b8a9b1dSJustin T. Gibbs 		break;
105652c9ce25SScott Long 	case XPT_ATA_IO:
105752c9ce25SScott Long 		data_ptrs[0] = &ccb->ataio.data_ptr;
1058b059686aSAlexander Motin 		lengths[0] = ccb->ataio.dxfer_len;
1059b059686aSAlexander Motin 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
1060b059686aSAlexander Motin 		numbufs = 1;
1061b059686aSAlexander Motin 		break;
1062b059686aSAlexander Motin 	case XPT_MMC_IO:
1063b059686aSAlexander Motin 		data_ptrs[0] = (u_int8_t **)&ccb->mmcio.cmd.data;
1064b059686aSAlexander Motin 		lengths[0] = sizeof(struct mmc_data *);
1065b059686aSAlexander Motin 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
1066b059686aSAlexander Motin 		data_ptrs[1] = (u_int8_t **)&ccb->mmcio.cmd.data->data;
1067b059686aSAlexander Motin 		lengths[1] = ccb->mmcio.cmd.data->len;
1068b059686aSAlexander Motin 		dirs[1] = ccb->ccb_h.flags & CAM_DIR_MASK;
1069b059686aSAlexander Motin 		numbufs = 2;
107052c9ce25SScott Long 		break;
107106e79492SKenneth D. Merry 	case XPT_SMP_IO:
107206e79492SKenneth D. Merry 		data_ptrs[0] = &ccb->smpio.smp_request;
1073b059686aSAlexander Motin 		lengths[0] = ccb->smpio.smp_request_len;
1074b059686aSAlexander Motin 		dirs[0] = CAM_DIR_OUT;
107506e79492SKenneth D. Merry 		data_ptrs[1] = &ccb->smpio.smp_response;
1076b059686aSAlexander Motin 		lengths[1] = ccb->smpio.smp_response_len;
1077b059686aSAlexander Motin 		dirs[1] = CAM_DIR_IN;
1078b059686aSAlexander Motin 		numbufs = 2;
107906e79492SKenneth D. Merry 		break;
1080df424515SWarner Losh 	case XPT_NVME_IO:
1081df424515SWarner Losh 	case XPT_NVME_ADMIN:
1082df424515SWarner Losh 		data_ptrs[0] = &ccb->nvmeio.data_ptr;
1083b059686aSAlexander Motin 		lengths[0] = ccb->nvmeio.dxfer_len;
1084b059686aSAlexander Motin 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
1085b059686aSAlexander Motin 		numbufs = 1;
1086b059686aSAlexander Motin 		break;
1087b059686aSAlexander Motin 	case XPT_DEV_ADVINFO:
1088b059686aSAlexander Motin 		data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
1089b059686aSAlexander Motin 		lengths[0] = ccb->cdai.bufsiz;
1090b059686aSAlexander Motin 		dirs[0] = CAM_DIR_IN;
1091b059686aSAlexander Motin 		numbufs = 1;
1092df424515SWarner Losh 		break;
10938b8a9b1dSJustin T. Gibbs 	default:
10948b8a9b1dSJustin T. Gibbs 		/* allow ourselves to be swapped once again */
10950cbbb7bfSPeter Wemm 		PRELE(curproc);
10968b8a9b1dSJustin T. Gibbs 		return;
10978b8a9b1dSJustin T. Gibbs 		break; /* NOTREACHED */
10988b8a9b1dSJustin T. Gibbs 	}
10998b8a9b1dSJustin T. Gibbs 
11008b8a9b1dSJustin T. Gibbs 	for (i = 0; i < numbufs; i++) {
1101b059686aSAlexander Motin 		if (mapinfo->bp[i]) {
11028b8a9b1dSJustin T. Gibbs 			/* unmap the buffer */
11038b8a9b1dSJustin T. Gibbs 			vunmapbuf(mapinfo->bp[i]);
11048b8a9b1dSJustin T. Gibbs 
11058b8a9b1dSJustin T. Gibbs 			/* release the buffer */
1106756a5412SGleb Smirnoff 			uma_zfree(pbuf_zone, mapinfo->bp[i]);
1107b059686aSAlexander Motin 		} else {
1108b059686aSAlexander Motin 			if (dirs[i] != CAM_DIR_OUT) {
1109b059686aSAlexander Motin 				copyout(*data_ptrs[i], mapinfo->orig[i],
1110b059686aSAlexander Motin 				    lengths[i]);
1111b059686aSAlexander Motin 			}
1112b059686aSAlexander Motin 			free(*data_ptrs[i], M_CAMPERIPH);
1113b059686aSAlexander Motin 		}
1114b059686aSAlexander Motin 
1115b059686aSAlexander Motin 		/* Set the user's pointer back to the original value */
1116b059686aSAlexander Motin 		*data_ptrs[i] = mapinfo->orig[i];
11178b8a9b1dSJustin T. Gibbs 	}
11188b8a9b1dSJustin T. Gibbs 
11198b8a9b1dSJustin T. Gibbs 	/* allow ourselves to be swapped once again */
11200cbbb7bfSPeter Wemm 	PRELE(curproc);
11218b8a9b1dSJustin T. Gibbs }
11228b8a9b1dSJustin T. Gibbs 
11238b8a9b1dSJustin T. Gibbs int
1124571e8e20SScott Long cam_periph_ioctl(struct cam_periph *periph, u_long cmd, caddr_t addr,
11258b8a9b1dSJustin T. Gibbs 		 int (*error_routine)(union ccb *ccb,
11268b8a9b1dSJustin T. Gibbs 				      cam_flags camflags,
11278b8a9b1dSJustin T. Gibbs 				      u_int32_t sense_flags))
11288b8a9b1dSJustin T. Gibbs {
11298b8a9b1dSJustin T. Gibbs 	union ccb 	     *ccb;
11308b8a9b1dSJustin T. Gibbs 	int 		     error;
11318b8a9b1dSJustin T. Gibbs 	int		     found;
11328b8a9b1dSJustin T. Gibbs 
11338b8a9b1dSJustin T. Gibbs 	error = found = 0;
11348b8a9b1dSJustin T. Gibbs 
11358b8a9b1dSJustin T. Gibbs 	switch(cmd){
11360cd631eeSMark Johnston 	case CAMGETPASSTHRU_0x19:
11378b8a9b1dSJustin T. Gibbs 	case CAMGETPASSTHRU:
1138bbfa4aa1SAlexander Motin 		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
11398b8a9b1dSJustin T. Gibbs 		xpt_setup_ccb(&ccb->ccb_h,
11408b8a9b1dSJustin T. Gibbs 			      ccb->ccb_h.path,
1141bbfa4aa1SAlexander Motin 			      CAM_PRIORITY_NORMAL);
11428b8a9b1dSJustin T. Gibbs 		ccb->ccb_h.func_code = XPT_GDEVLIST;
11438b8a9b1dSJustin T. Gibbs 
11448b8a9b1dSJustin T. Gibbs 		/*
11458b8a9b1dSJustin T. Gibbs 		 * Basically, the point of this is that we go through
11468b8a9b1dSJustin T. Gibbs 		 * getting the list of devices, until we find a passthrough
11478b8a9b1dSJustin T. Gibbs 		 * device.  In the current version of the CAM code, the
11488b8a9b1dSJustin T. Gibbs 		 * only way to determine what type of device we're dealing
11498b8a9b1dSJustin T. Gibbs 		 * with is by its name.
11508b8a9b1dSJustin T. Gibbs 		 */
11518b8a9b1dSJustin T. Gibbs 		while (found == 0) {
11528b8a9b1dSJustin T. Gibbs 			ccb->cgdl.index = 0;
11538b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
11548b8a9b1dSJustin T. Gibbs 			while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
11558b8a9b1dSJustin T. Gibbs 				/* we want the next device in the list */
11568b8a9b1dSJustin T. Gibbs 				xpt_action(ccb);
11578b8a9b1dSJustin T. Gibbs 				if (strncmp(ccb->cgdl.periph_name,
11588b8a9b1dSJustin T. Gibbs 				    "pass", 4) == 0){
11598b8a9b1dSJustin T. Gibbs 					found = 1;
11608b8a9b1dSJustin T. Gibbs 					break;
11618b8a9b1dSJustin T. Gibbs 				}
11628b8a9b1dSJustin T. Gibbs 			}
11638b8a9b1dSJustin T. Gibbs 			if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) &&
11648b8a9b1dSJustin T. Gibbs 			    (found == 0)) {
11658b8a9b1dSJustin T. Gibbs 				ccb->cgdl.periph_name[0] = '\0';
11668b8a9b1dSJustin T. Gibbs 				ccb->cgdl.unit_number = 0;
11678b8a9b1dSJustin T. Gibbs 				break;
11688b8a9b1dSJustin T. Gibbs 			}
11698b8a9b1dSJustin T. Gibbs 		}
11708b8a9b1dSJustin T. Gibbs 
11718b8a9b1dSJustin T. Gibbs 		/* copy the result back out */
11728b8a9b1dSJustin T. Gibbs 		bcopy(ccb, addr, sizeof(union ccb));
11738b8a9b1dSJustin T. Gibbs 
11748b8a9b1dSJustin T. Gibbs 		/* and release the ccb */
11758b8a9b1dSJustin T. Gibbs 		xpt_release_ccb(ccb);
11768b8a9b1dSJustin T. Gibbs 
11778b8a9b1dSJustin T. Gibbs 		break;
11788b8a9b1dSJustin T. Gibbs 	default:
11798b8a9b1dSJustin T. Gibbs 		error = ENOTTY;
11808b8a9b1dSJustin T. Gibbs 		break;
11818b8a9b1dSJustin T. Gibbs 	}
11828b8a9b1dSJustin T. Gibbs 	return(error);
11838b8a9b1dSJustin T. Gibbs }
11848b8a9b1dSJustin T. Gibbs 
1185227d67aaSAlexander Motin static void
1186991b5d26SMark Johnston cam_periph_done_panic(struct cam_periph *periph, union ccb *done_ccb)
1187991b5d26SMark Johnston {
1188991b5d26SMark Johnston 
1189991b5d26SMark Johnston 	panic("%s: already done with ccb %p", __func__, done_ccb);
1190991b5d26SMark Johnston }
1191991b5d26SMark Johnston 
1192991b5d26SMark Johnston static void
1193227d67aaSAlexander Motin cam_periph_done(struct cam_periph *periph, union ccb *done_ccb)
1194227d67aaSAlexander Motin {
1195227d67aaSAlexander Motin 
1196227d67aaSAlexander Motin 	/* Caller will release the CCB */
1197991b5d26SMark Johnston 	xpt_path_assert(done_ccb->ccb_h.path, MA_OWNED);
1198991b5d26SMark Johnston 	done_ccb->ccb_h.cbfcnp = cam_periph_done_panic;
1199227d67aaSAlexander Motin 	wakeup(&done_ccb->ccb_h.cbfcnp);
1200227d67aaSAlexander Motin }
1201227d67aaSAlexander Motin 
1202991b5d26SMark Johnston static void
1203991b5d26SMark Johnston cam_periph_ccbwait(union ccb *ccb)
1204991b5d26SMark Johnston {
1205991b5d26SMark Johnston 
1206991b5d26SMark Johnston 	if ((ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
1207991b5d26SMark Johnston 		while (ccb->ccb_h.cbfcnp != cam_periph_done_panic)
1208991b5d26SMark Johnston 			xpt_path_sleep(ccb->ccb_h.path, &ccb->ccb_h.cbfcnp,
1209991b5d26SMark Johnston 			    PRIBIO, "cbwait", 0);
1210991b5d26SMark Johnston 	}
1211991b5d26SMark Johnston 	KASSERT(ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX &&
1212991b5d26SMark Johnston 	    (ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG,
1213991b5d26SMark Johnston 	    ("%s: proceeding with incomplete ccb: ccb=%p, func_code=%#x, "
1214991b5d26SMark Johnston 	     "status=%#x, index=%d", __func__, ccb, ccb->ccb_h.func_code,
1215991b5d26SMark Johnston 	     ccb->ccb_h.status, ccb->ccb_h.pinfo.index));
1216991b5d26SMark Johnston }
1217991b5d26SMark Johnston 
121876314772SScott Long /*
121976314772SScott Long  * Dispatch a CCB and wait for it to complete.  If the CCB has set a
122076314772SScott Long  * callback function (ccb->ccb_h.cbfcnp), it will be overwritten and lost.
122176314772SScott Long  */
12228b8a9b1dSJustin T. Gibbs int
12238b8a9b1dSJustin T. Gibbs cam_periph_runccb(union ccb *ccb,
12248b8a9b1dSJustin T. Gibbs 		  int (*error_routine)(union ccb *ccb,
12258b8a9b1dSJustin T. Gibbs 				       cam_flags camflags,
12268b8a9b1dSJustin T. Gibbs 				       u_int32_t sense_flags),
12278b8a9b1dSJustin T. Gibbs 		  cam_flags camflags, u_int32_t sense_flags,
12288b8a9b1dSJustin T. Gibbs 		  struct devstat *ds)
12298b8a9b1dSJustin T. Gibbs {
1230fd8be18fSKenneth D. Merry 	struct bintime *starttime;
1231fd8be18fSKenneth D. Merry 	struct bintime ltime;
12328b8a9b1dSJustin T. Gibbs 	int error;
12335cf3cd10SWarner Losh 	bool must_poll;
1234f93a843cSWarner Losh 	uint32_t timeout = 1;
12358b8a9b1dSJustin T. Gibbs 
1236fd8be18fSKenneth D. Merry 	starttime = NULL;
1237227d67aaSAlexander Motin 	xpt_path_assert(ccb->ccb_h.path, MA_OWNED);
1238991b5d26SMark Johnston 	KASSERT((ccb->ccb_h.flags & CAM_UNLOCKED) == 0,
1239991b5d26SMark Johnston 	    ("%s: ccb=%p, func_code=%#x, flags=%#x", __func__, ccb,
1240991b5d26SMark Johnston 	     ccb->ccb_h.func_code, ccb->ccb_h.flags));
12418b8a9b1dSJustin T. Gibbs 
12428b8a9b1dSJustin T. Gibbs 	/*
12438b8a9b1dSJustin T. Gibbs 	 * If the user has supplied a stats structure, and if we understand
12448b8a9b1dSJustin T. Gibbs 	 * this particular type of ccb, record the transaction start.
12458b8a9b1dSJustin T. Gibbs 	 */
1246d2f3208dSWarner Losh 	if (ds != NULL &&
1247d2f3208dSWarner Losh 	    (ccb->ccb_h.func_code == XPT_SCSI_IO ||
1248d2f3208dSWarner Losh 	    ccb->ccb_h.func_code == XPT_ATA_IO ||
1249d2f3208dSWarner Losh 	    ccb->ccb_h.func_code == XPT_NVME_IO)) {
1250fd8be18fSKenneth D. Merry 		starttime = &ltime;
1251fd8be18fSKenneth D. Merry 		binuptime(starttime);
1252fd8be18fSKenneth D. Merry 		devstat_start_transaction(ds, starttime);
1253fd8be18fSKenneth D. Merry 	}
12548b8a9b1dSJustin T. Gibbs 
12555cf3cd10SWarner Losh 	/*
12565cf3cd10SWarner Losh 	 * We must poll the I/O while we're dumping. The scheduler is normally
12575cf3cd10SWarner Losh 	 * stopped for dumping, except when we call doadump from ddb. While the
12585cf3cd10SWarner Losh 	 * scheduler is running in this case, we still need to poll the I/O to
12595cf3cd10SWarner Losh 	 * avoid sleeping waiting for the ccb to complete.
126004e814aeSScott Long 	 *
1261045f8bc8SWarner Losh 	 * A panic triggered dump stops the scheduler, any callback from the
1262045f8bc8SWarner Losh 	 * shutdown_post_sync event will run with the scheduler stopped, but
1263045f8bc8SWarner Losh 	 * before we're officially dumping. To avoid hanging in adashutdown
1264045f8bc8SWarner Losh 	 * initiated commands (or other similar situations), we have to test for
1265045f8bc8SWarner Losh 	 * either SCHEDULER_STOPPED() here as well.
1266045f8bc8SWarner Losh 	 *
1267045f8bc8SWarner Losh 	 * To avoid locking problems, dumping/polling callers must call
126804e814aeSScott Long 	 * without a periph lock held.
12695cf3cd10SWarner Losh 	 */
1270045f8bc8SWarner Losh 	must_poll = dumping || SCHEDULER_STOPPED();
1271227d67aaSAlexander Motin 	ccb->ccb_h.cbfcnp = cam_periph_done;
12728b8a9b1dSJustin T. Gibbs 
1273f93a843cSWarner Losh 	/*
1274f93a843cSWarner Losh 	 * If we're polling, then we need to ensure that we have ample resources
127576314772SScott Long 	 * in the periph.  cam_periph_error can reschedule the ccb by calling
127676314772SScott Long 	 * xpt_action and returning ERESTART, so we have to effect the polling
127776314772SScott Long 	 * in the do loop below.
1278f93a843cSWarner Losh 	 */
12795cf3cd10SWarner Losh 	if (must_poll) {
1280447b3557SJohn Baldwin 		if (cam_sim_pollable(ccb->ccb_h.path->bus->sim))
1281f93a843cSWarner Losh 			timeout = xpt_poll_setup(ccb);
1282447b3557SJohn Baldwin 		else
1283447b3557SJohn Baldwin 			timeout = 0;
1284f93a843cSWarner Losh 	}
1285f93a843cSWarner Losh 
1286f93a843cSWarner Losh 	if (timeout == 0) {
1287f93a843cSWarner Losh 		ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1288f93a843cSWarner Losh 		error = EBUSY;
1289f93a843cSWarner Losh 	} else {
1290f93a843cSWarner Losh 		xpt_action(ccb);
12918b8a9b1dSJustin T. Gibbs 		do {
12925cf3cd10SWarner Losh 			if (must_poll) {
1293f93a843cSWarner Losh 				xpt_pollwait(ccb, timeout);
1294f93a843cSWarner Losh 				timeout = ccb->ccb_h.timeout * 10;
12955cf3cd10SWarner Losh 			} else {
12965cf3cd10SWarner Losh 				cam_periph_ccbwait(ccb);
1297f93a843cSWarner Losh 			}
12988b8a9b1dSJustin T. Gibbs 			if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
12998b8a9b1dSJustin T. Gibbs 				error = 0;
1300991b5d26SMark Johnston 			else if (error_routine != NULL) {
1301991b5d26SMark Johnston 				ccb->ccb_h.cbfcnp = cam_periph_done;
13028b8a9b1dSJustin T. Gibbs 				error = (*error_routine)(ccb, camflags, sense_flags);
1303991b5d26SMark Johnston 			} else
13048b8a9b1dSJustin T. Gibbs 				error = 0;
13058b8a9b1dSJustin T. Gibbs 		} while (error == ERESTART);
1306f93a843cSWarner Losh 	}
1307f93a843cSWarner Losh 
130883c5d981SAlexander Motin 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
13098b8a9b1dSJustin T. Gibbs 		cam_release_devq(ccb->ccb_h.path,
13108b8a9b1dSJustin T. Gibbs 				 /* relsim_flags */0,
13118b8a9b1dSJustin T. Gibbs 				 /* openings */0,
13128b8a9b1dSJustin T. Gibbs 				 /* timeout */0,
13138b8a9b1dSJustin T. Gibbs 				 /* getcount_only */ FALSE);
131483c5d981SAlexander Motin 		ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
131583c5d981SAlexander Motin 	}
13168b8a9b1dSJustin T. Gibbs 
1317379ad358SAlexander Motin 	if (ds != NULL) {
1318d2f3208dSWarner Losh 		uint32_t bytes;
1319d2f3208dSWarner Losh 		devstat_tag_type tag;
1320d2f3208dSWarner Losh 		bool valid = true;
1321d2f3208dSWarner Losh 
1322379ad358SAlexander Motin 		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1323d2f3208dSWarner Losh 			bytes = ccb->csio.dxfer_len - ccb->csio.resid;
1324d2f3208dSWarner Losh 			tag = (devstat_tag_type)(ccb->csio.tag_action & 0x3);
1325379ad358SAlexander Motin 		} else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1326d2f3208dSWarner Losh 			bytes = ccb->ataio.dxfer_len - ccb->ataio.resid;
1327d2f3208dSWarner Losh 			tag = (devstat_tag_type)0;
1328d2f3208dSWarner Losh 		} else if (ccb->ccb_h.func_code == XPT_NVME_IO) {
1329d2f3208dSWarner Losh 			bytes = ccb->nvmeio.dxfer_len; /* NB: resid no possible */
1330d2f3208dSWarner Losh 			tag = (devstat_tag_type)0;
1331d2f3208dSWarner Losh 		} else {
1332d2f3208dSWarner Losh 			valid = false;
1333379ad358SAlexander Motin 		}
1334d2f3208dSWarner Losh 		if (valid)
1335d2f3208dSWarner Losh 			devstat_end_transaction(ds, bytes, tag,
1336d2f3208dSWarner Losh 			    ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) ?
1337d2f3208dSWarner Losh 			    DEVSTAT_NO_DATA : (ccb->ccb_h.flags & CAM_DIR_OUT) ?
1338d2f3208dSWarner Losh 			    DEVSTAT_WRITE : DEVSTAT_READ, NULL, starttime);
1339379ad358SAlexander Motin 	}
13408b8a9b1dSJustin T. Gibbs 
13418b8a9b1dSJustin T. Gibbs 	return(error);
13428b8a9b1dSJustin T. Gibbs }
13438b8a9b1dSJustin T. Gibbs 
134487cfaf0eSJustin T. Gibbs void
134587cfaf0eSJustin T. Gibbs cam_freeze_devq(struct cam_path *path)
134687cfaf0eSJustin T. Gibbs {
1347cccf4220SAlexander Motin 	struct ccb_hdr ccb_h;
134887cfaf0eSJustin T. Gibbs 
13490d4f3c31SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_freeze_devq\n"));
1350076686feSEdward Tomasz Napierala 	memset(&ccb_h, 0, sizeof(ccb_h));
1351cccf4220SAlexander Motin 	xpt_setup_ccb(&ccb_h, path, /*priority*/1);
1352cccf4220SAlexander Motin 	ccb_h.func_code = XPT_NOOP;
1353cccf4220SAlexander Motin 	ccb_h.flags = CAM_DEV_QFREEZE;
1354cccf4220SAlexander Motin 	xpt_action((union ccb *)&ccb_h);
135587cfaf0eSJustin T. Gibbs }
135687cfaf0eSJustin T. Gibbs 
13578b8a9b1dSJustin T. Gibbs u_int32_t
13588b8a9b1dSJustin T. Gibbs cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
135983c5d981SAlexander Motin 		 u_int32_t openings, u_int32_t arg,
13608b8a9b1dSJustin T. Gibbs 		 int getcount_only)
13618b8a9b1dSJustin T. Gibbs {
13628b8a9b1dSJustin T. Gibbs 	struct ccb_relsim crs;
13638b8a9b1dSJustin T. Gibbs 
13640d4f3c31SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_release_devq(%u, %u, %u, %d)\n",
13650d4f3c31SAlexander Motin 	    relsim_flags, openings, arg, getcount_only));
1366076686feSEdward Tomasz Napierala 	memset(&crs, 0, sizeof(crs));
1367bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
13688b8a9b1dSJustin T. Gibbs 	crs.ccb_h.func_code = XPT_REL_SIMQ;
13698b8a9b1dSJustin T. Gibbs 	crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
13708b8a9b1dSJustin T. Gibbs 	crs.release_flags = relsim_flags;
13718b8a9b1dSJustin T. Gibbs 	crs.openings = openings;
137283c5d981SAlexander Motin 	crs.release_timeout = arg;
13738b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&crs);
13748b8a9b1dSJustin T. Gibbs 	return (crs.qfrozen_cnt);
13758b8a9b1dSJustin T. Gibbs }
13768b8a9b1dSJustin T. Gibbs 
13778b8a9b1dSJustin T. Gibbs #define saved_ccb_ptr ppriv_ptr0
137883c5d981SAlexander Motin static void
137983c5d981SAlexander Motin camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
138083c5d981SAlexander Motin {
13810191d9b3SAlexander Motin 	union ccb      *saved_ccb;
138283c5d981SAlexander Motin 	cam_status	status;
138383c5d981SAlexander Motin 	struct scsi_start_stop_unit *scsi_cmd;
138479fab7d4SAlexander Motin 	int		error = 0, error_code, sense_key, asc, ascq;
138513aa56fcSEdward Tomasz Napierala 	u_int16_t	done_flags;
138683c5d981SAlexander Motin 
13870191d9b3SAlexander Motin 	scsi_cmd = (struct scsi_start_stop_unit *)
13880191d9b3SAlexander Motin 	    &done_ccb->csio.cdb_io.cdb_bytes;
138983c5d981SAlexander Motin 	status = done_ccb->ccb_h.status;
13908b8a9b1dSJustin T. Gibbs 
13910191d9b3SAlexander Motin 	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1392e7493b28SAlexander Motin 		if (scsi_extract_sense_ccb(done_ccb,
1393e7493b28SAlexander Motin 		    &error_code, &sense_key, &asc, &ascq)) {
139465afe1f4SKenneth D. Merry 			/*
13958b8a9b1dSJustin T. Gibbs 			 * If the error is "invalid field in CDB",
13968b8a9b1dSJustin T. Gibbs 			 * and the load/eject flag is set, turn the
13978b8a9b1dSJustin T. Gibbs 			 * flag off and try again.  This is just in
13988b8a9b1dSJustin T. Gibbs 			 * case the drive in question barfs on the
13998b8a9b1dSJustin T. Gibbs 			 * load eject flag.  The CAM code should set
14008b8a9b1dSJustin T. Gibbs 			 * the load/eject flag by default for
14018b8a9b1dSJustin T. Gibbs 			 * removable media.
14028b8a9b1dSJustin T. Gibbs 			 */
14038b8a9b1dSJustin T. Gibbs 			if ((scsi_cmd->opcode == START_STOP_UNIT) &&
14048b8a9b1dSJustin T. Gibbs 			    ((scsi_cmd->how & SSS_LOEJ) != 0) &&
14050191d9b3SAlexander Motin 			     (asc == 0x24) && (ascq == 0x00)) {
14068b8a9b1dSJustin T. Gibbs 				scsi_cmd->how &= ~SSS_LOEJ;
14070191d9b3SAlexander Motin 				if (status & CAM_DEV_QFRZN) {
14080191d9b3SAlexander Motin 					cam_release_devq(done_ccb->ccb_h.path,
14090191d9b3SAlexander Motin 					    0, 0, 0, 0);
14100191d9b3SAlexander Motin 					done_ccb->ccb_h.status &=
14110191d9b3SAlexander Motin 					    ~CAM_DEV_QFRZN;
14120191d9b3SAlexander Motin 				}
14138b8a9b1dSJustin T. Gibbs 				xpt_action(done_ccb);
14140191d9b3SAlexander Motin 				goto out;
14150191d9b3SAlexander Motin 			}
14160191d9b3SAlexander Motin 		}
141779fab7d4SAlexander Motin 		error = cam_periph_error(done_ccb, 0,
141879fab7d4SAlexander Motin 		    SF_RETRY_UA | SF_NO_PRINT);
141979fab7d4SAlexander Motin 		if (error == ERESTART)
14200191d9b3SAlexander Motin 			goto out;
14210191d9b3SAlexander Motin 		if (done_ccb->ccb_h.status & CAM_DEV_QFRZN) {
14220191d9b3SAlexander Motin 			cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
14230191d9b3SAlexander Motin 			done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
14240191d9b3SAlexander Motin 		}
14250191d9b3SAlexander Motin 	} else {
14260191d9b3SAlexander Motin 		/*
14270191d9b3SAlexander Motin 		 * If we have successfully taken a device from the not
14280191d9b3SAlexander Motin 		 * ready to ready state, re-scan the device and re-get
14290191d9b3SAlexander Motin 		 * the inquiry information.  Many devices (mostly disks)
14300191d9b3SAlexander Motin 		 * don't properly report their inquiry information unless
14310191d9b3SAlexander Motin 		 * they are spun up.
14320191d9b3SAlexander Motin 		 */
14330191d9b3SAlexander Motin 		if (scsi_cmd->opcode == START_STOP_UNIT)
14340191d9b3SAlexander Motin 			xpt_async(AC_INQ_CHANGED, done_ccb->ccb_h.path, NULL);
14350191d9b3SAlexander Motin 	}
143665afe1f4SKenneth D. Merry 
1437bae3729bSAlexander Motin 	/* If we tried long wait and still failed, remember that. */
1438bae3729bSAlexander Motin 	if ((periph->flags & CAM_PERIPH_RECOVERY_WAIT) &&
1439bae3729bSAlexander Motin 	    (done_ccb->csio.cdb_io.cdb_bytes[0] == TEST_UNIT_READY)) {
1440bae3729bSAlexander Motin 		periph->flags &= ~CAM_PERIPH_RECOVERY_WAIT;
1441bae3729bSAlexander Motin 		if (error != 0 && done_ccb->ccb_h.retry_count == 0)
1442bae3729bSAlexander Motin 			periph->flags |= CAM_PERIPH_RECOVERY_WAIT_FAILED;
1443bae3729bSAlexander Motin 	}
1444bae3729bSAlexander Motin 
14458b8a9b1dSJustin T. Gibbs 	/*
144679fab7d4SAlexander Motin 	 * After recovery action(s) completed, return to the original CCB.
144779fab7d4SAlexander Motin 	 * If the recovery CCB has failed, considering its own possible
144879fab7d4SAlexander Motin 	 * retries and recovery, assume we are back in state where we have
144979fab7d4SAlexander Motin 	 * been originally, but without recovery hopes left.  In such case,
145079fab7d4SAlexander Motin 	 * after the final attempt below, we cancel any further retries,
145179fab7d4SAlexander Motin 	 * blocking by that also any new recovery attempts for this CCB,
145279fab7d4SAlexander Motin 	 * and the result will be the final one returned to the CCB owher.
14538b8a9b1dSJustin T. Gibbs 	 */
14540191d9b3SAlexander Motin 	saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
145513aa56fcSEdward Tomasz Napierala 	KASSERT(saved_ccb->ccb_h.func_code == XPT_SCSI_IO,
145613aa56fcSEdward Tomasz Napierala 	    ("%s: saved_ccb func_code %#x != XPT_SCSI_IO",
145713aa56fcSEdward Tomasz Napierala 	     __func__, saved_ccb->ccb_h.func_code));
145813aa56fcSEdward Tomasz Napierala 	KASSERT(done_ccb->ccb_h.func_code == XPT_SCSI_IO,
145913aa56fcSEdward Tomasz Napierala 	    ("%s: done_ccb func_code %#x != XPT_SCSI_IO",
146013aa56fcSEdward Tomasz Napierala 	     __func__, done_ccb->ccb_h.func_code));
1461404f0011SAlexander Motin 	saved_ccb->ccb_h.periph_links = done_ccb->ccb_h.periph_links;
146213aa56fcSEdward Tomasz Napierala 	done_flags = done_ccb->ccb_h.alloc_flags;
146313aa56fcSEdward Tomasz Napierala 	bcopy(saved_ccb, done_ccb, sizeof(struct ccb_scsiio));
146413aa56fcSEdward Tomasz Napierala 	done_ccb->ccb_h.alloc_flags = done_flags;
146583c5d981SAlexander Motin 	xpt_free_ccb(saved_ccb);
14660191d9b3SAlexander Motin 	if (done_ccb->ccb_h.cbfcnp != camperiphdone)
146760a899a0SKenneth D. Merry 		periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG;
146879fab7d4SAlexander Motin 	if (error != 0)
146979fab7d4SAlexander Motin 		done_ccb->ccb_h.retry_count = 0;
14708b8a9b1dSJustin T. Gibbs 	xpt_action(done_ccb);
14718b8a9b1dSJustin T. Gibbs 
14720191d9b3SAlexander Motin out:
14730191d9b3SAlexander Motin 	/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
14740191d9b3SAlexander Motin 	cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1475d84c90a6SAlexander Motin }
14768b8a9b1dSJustin T. Gibbs 
14778b8a9b1dSJustin T. Gibbs /*
147887cfaf0eSJustin T. Gibbs  * Generic Async Event handler.  Peripheral drivers usually
147987cfaf0eSJustin T. Gibbs  * filter out the events that require personal attention,
148087cfaf0eSJustin T. Gibbs  * and leave the rest to this function.
148187cfaf0eSJustin T. Gibbs  */
148287cfaf0eSJustin T. Gibbs void
148387cfaf0eSJustin T. Gibbs cam_periph_async(struct cam_periph *periph, u_int32_t code,
148487cfaf0eSJustin T. Gibbs 		 struct cam_path *path, void *arg)
148587cfaf0eSJustin T. Gibbs {
148687cfaf0eSJustin T. Gibbs 	switch (code) {
148787cfaf0eSJustin T. Gibbs 	case AC_LOST_DEVICE:
148887cfaf0eSJustin T. Gibbs 		cam_periph_invalidate(periph);
148987cfaf0eSJustin T. Gibbs 		break;
149087cfaf0eSJustin T. Gibbs 	default:
149187cfaf0eSJustin T. Gibbs 		break;
149287cfaf0eSJustin T. Gibbs 	}
149387cfaf0eSJustin T. Gibbs }
149487cfaf0eSJustin T. Gibbs 
149587cfaf0eSJustin T. Gibbs void
149687cfaf0eSJustin T. Gibbs cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle)
149787cfaf0eSJustin T. Gibbs {
149887cfaf0eSJustin T. Gibbs 	struct ccb_getdevstats cgds;
149987cfaf0eSJustin T. Gibbs 
1500076686feSEdward Tomasz Napierala 	memset(&cgds, 0, sizeof(cgds));
1501bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cgds.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
150287cfaf0eSJustin T. Gibbs 	cgds.ccb_h.func_code = XPT_GDEV_STATS;
150387cfaf0eSJustin T. Gibbs 	xpt_action((union ccb *)&cgds);
150487cfaf0eSJustin T. Gibbs 	cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle);
150587cfaf0eSJustin T. Gibbs }
150687cfaf0eSJustin T. Gibbs 
150787cfaf0eSJustin T. Gibbs void
150887cfaf0eSJustin T. Gibbs cam_periph_freeze_after_event(struct cam_periph *periph,
150987cfaf0eSJustin T. Gibbs 			      struct timeval* event_time, u_int duration_ms)
151087cfaf0eSJustin T. Gibbs {
151187cfaf0eSJustin T. Gibbs 	struct timeval delta;
151287cfaf0eSJustin T. Gibbs 	struct timeval duration_tv;
151387cfaf0eSJustin T. Gibbs 
1514f8ff57d2SAlexander Motin 	if (!timevalisset(event_time))
1515f8ff57d2SAlexander Motin 		return;
1516f8ff57d2SAlexander Motin 
151787cfaf0eSJustin T. Gibbs 	microtime(&delta);
151887cfaf0eSJustin T. Gibbs 	timevalsub(&delta, event_time);
151987cfaf0eSJustin T. Gibbs 	duration_tv.tv_sec = duration_ms / 1000;
152087cfaf0eSJustin T. Gibbs 	duration_tv.tv_usec = (duration_ms % 1000) * 1000;
152187cfaf0eSJustin T. Gibbs 	if (timevalcmp(&delta, &duration_tv, <)) {
152287cfaf0eSJustin T. Gibbs 		timevalsub(&duration_tv, &delta);
152387cfaf0eSJustin T. Gibbs 
152487cfaf0eSJustin T. Gibbs 		duration_ms = duration_tv.tv_sec * 1000;
152587cfaf0eSJustin T. Gibbs 		duration_ms += duration_tv.tv_usec / 1000;
152687cfaf0eSJustin T. Gibbs 		cam_freeze_devq(periph->path);
152787cfaf0eSJustin T. Gibbs 		cam_release_devq(periph->path,
152887cfaf0eSJustin T. Gibbs 				RELSIM_RELEASE_AFTER_TIMEOUT,
152987cfaf0eSJustin T. Gibbs 				/*reduction*/0,
153087cfaf0eSJustin T. Gibbs 				/*timeout*/duration_ms,
153187cfaf0eSJustin T. Gibbs 				/*getcount_only*/0);
153287cfaf0eSJustin T. Gibbs 	}
153387cfaf0eSJustin T. Gibbs 
153487cfaf0eSJustin T. Gibbs }
153587cfaf0eSJustin T. Gibbs 
15363393f8daSKenneth D. Merry static int
15370191d9b3SAlexander Motin camperiphscsistatuserror(union ccb *ccb, union ccb **orig_ccb,
15380191d9b3SAlexander Motin     cam_flags camflags, u_int32_t sense_flags,
15393393f8daSKenneth D. Merry     int *openings, u_int32_t *relsim_flags,
15400181d54bSAlexander Motin     u_int32_t *timeout, u_int32_t *action, const char **action_string)
15418b8a9b1dSJustin T. Gibbs {
154299bad9caSAlexander Motin 	struct cam_periph *periph;
15438b8a9b1dSJustin T. Gibbs 	int error;
15448b8a9b1dSJustin T. Gibbs 
15458b8a9b1dSJustin T. Gibbs 	switch (ccb->csio.scsi_status) {
15468b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_OK:
15478b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_COND_MET:
15488b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_INTERMED:
15498b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_INTERMED_COND_MET:
15508b8a9b1dSJustin T. Gibbs 		error = 0;
15518b8a9b1dSJustin T. Gibbs 		break;
15528b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_CMD_TERMINATED:
15538b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_CHECK_COND:
15540191d9b3SAlexander Motin 		error = camperiphscsisenseerror(ccb, orig_ccb,
15553393f8daSKenneth D. Merry 					        camflags,
15568b8a9b1dSJustin T. Gibbs 					        sense_flags,
15573393f8daSKenneth D. Merry 					        openings,
15583393f8daSKenneth D. Merry 					        relsim_flags,
155983c5d981SAlexander Motin 					        timeout,
15600181d54bSAlexander Motin 					        action,
156183c5d981SAlexander Motin 					        action_string);
15628b8a9b1dSJustin T. Gibbs 		break;
15638b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_QUEUE_FULL:
15648b8a9b1dSJustin T. Gibbs 	{
15658b8a9b1dSJustin T. Gibbs 		/* no decrement */
156682815562SJustin T. Gibbs 		struct ccb_getdevstats cgds;
15678b8a9b1dSJustin T. Gibbs 
15688b8a9b1dSJustin T. Gibbs 		/*
15698b8a9b1dSJustin T. Gibbs 		 * First off, find out what the current
15708b8a9b1dSJustin T. Gibbs 		 * transaction counts are.
15718b8a9b1dSJustin T. Gibbs 		 */
1572076686feSEdward Tomasz Napierala 		memset(&cgds, 0, sizeof(cgds));
157382815562SJustin T. Gibbs 		xpt_setup_ccb(&cgds.ccb_h,
15748b8a9b1dSJustin T. Gibbs 			      ccb->ccb_h.path,
1575bbfa4aa1SAlexander Motin 			      CAM_PRIORITY_NORMAL);
157682815562SJustin T. Gibbs 		cgds.ccb_h.func_code = XPT_GDEV_STATS;
157782815562SJustin T. Gibbs 		xpt_action((union ccb *)&cgds);
15788b8a9b1dSJustin T. Gibbs 
15798b8a9b1dSJustin T. Gibbs 		/*
15808b8a9b1dSJustin T. Gibbs 		 * If we were the only transaction active, treat
15818b8a9b1dSJustin T. Gibbs 		 * the QUEUE FULL as if it were a BUSY condition.
15828b8a9b1dSJustin T. Gibbs 		 */
158382815562SJustin T. Gibbs 		if (cgds.dev_active != 0) {
158482815562SJustin T. Gibbs 			int total_openings;
158582815562SJustin T. Gibbs 
15868b8a9b1dSJustin T. Gibbs 			/*
15878b8a9b1dSJustin T. Gibbs 		 	 * Reduce the number of openings to
15888b8a9b1dSJustin T. Gibbs 			 * be 1 less than the amount it took
15898b8a9b1dSJustin T. Gibbs 			 * to get a queue full bounded by the
15908b8a9b1dSJustin T. Gibbs 			 * minimum allowed tag count for this
15918b8a9b1dSJustin T. Gibbs 			 * device.
15928b8a9b1dSJustin T. Gibbs 		 	 */
15933393f8daSKenneth D. Merry 			total_openings = cgds.dev_active + cgds.dev_openings;
15943393f8daSKenneth D. Merry 			*openings = cgds.dev_active;
15953393f8daSKenneth D. Merry 			if (*openings < cgds.mintags)
15963393f8daSKenneth D. Merry 				*openings = cgds.mintags;
15973393f8daSKenneth D. Merry 			if (*openings < total_openings)
15983393f8daSKenneth D. Merry 				*relsim_flags = RELSIM_ADJUST_OPENINGS;
15998b8a9b1dSJustin T. Gibbs 			else {
16008b8a9b1dSJustin T. Gibbs 				/*
16018b8a9b1dSJustin T. Gibbs 				 * Some devices report queue full for
16028b8a9b1dSJustin T. Gibbs 				 * temporary resource shortages.  For
16038b8a9b1dSJustin T. Gibbs 				 * this reason, we allow a minimum
16048b8a9b1dSJustin T. Gibbs 				 * tag count to be entered via a
16058b8a9b1dSJustin T. Gibbs 				 * quirk entry to prevent the queue
16068b8a9b1dSJustin T. Gibbs 				 * count on these devices from falling
16078b8a9b1dSJustin T. Gibbs 				 * to a pessimisticly low value.  We
16088b8a9b1dSJustin T. Gibbs 				 * still wait for the next successful
16098b8a9b1dSJustin T. Gibbs 				 * completion, however, before queueing
16108b8a9b1dSJustin T. Gibbs 				 * more transactions to the device.
16118b8a9b1dSJustin T. Gibbs 				 */
16123393f8daSKenneth D. Merry 				*relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT;
16138b8a9b1dSJustin T. Gibbs 			}
16143393f8daSKenneth D. Merry 			*timeout = 0;
16158b8a9b1dSJustin T. Gibbs 			error = ERESTART;
16160181d54bSAlexander Motin 			*action &= ~SSQ_PRINT_SENSE;
16178b8a9b1dSJustin T. Gibbs 			break;
16188b8a9b1dSJustin T. Gibbs 		}
16198b8a9b1dSJustin T. Gibbs 		/* FALLTHROUGH */
16208b8a9b1dSJustin T. Gibbs 	}
16218b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_BUSY:
16228b8a9b1dSJustin T. Gibbs 		/*
16238b8a9b1dSJustin T. Gibbs 		 * Restart the queue after either another
16248b8a9b1dSJustin T. Gibbs 		 * command completes or a 1 second timeout.
16258b8a9b1dSJustin T. Gibbs 		 */
162699bad9caSAlexander Motin 		periph = xpt_path_periph(ccb->ccb_h.path);
162799bad9caSAlexander Motin 		if (periph->flags & CAM_PERIPH_INVALID) {
16289fb40bafSWarner Losh 			error = ENXIO;
162999bad9caSAlexander Motin 			*action_string = "Periph was invalidated";
163099bad9caSAlexander Motin 		} else if ((sense_flags & SF_RETRY_BUSY) != 0 ||
163199bad9caSAlexander Motin 		    ccb->ccb_h.retry_count > 0) {
163299bad9caSAlexander Motin 			if ((sense_flags & SF_RETRY_BUSY) == 0)
163399bad9caSAlexander Motin 				ccb->ccb_h.retry_count--;
16348b8a9b1dSJustin T. Gibbs 			error = ERESTART;
16353393f8daSKenneth D. Merry 			*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT
16368b8a9b1dSJustin T. Gibbs 				      | RELSIM_RELEASE_AFTER_CMDCMPLT;
16373393f8daSKenneth D. Merry 			*timeout = 1000;
1638af51b059SMatt Jacob 		} else {
1639af51b059SMatt Jacob 			error = EIO;
164099bad9caSAlexander Motin 			*action_string = "Retries exhausted";
1641af51b059SMatt Jacob 		}
16428b8a9b1dSJustin T. Gibbs 		break;
16438b8a9b1dSJustin T. Gibbs 	case SCSI_STATUS_RESERV_CONFLICT:
16448b8a9b1dSJustin T. Gibbs 	default:
16458b8a9b1dSJustin T. Gibbs 		error = EIO;
16468b8a9b1dSJustin T. Gibbs 		break;
16478b8a9b1dSJustin T. Gibbs 	}
16483393f8daSKenneth D. Merry 	return (error);
16493393f8daSKenneth D. Merry }
16503393f8daSKenneth D. Merry 
16513393f8daSKenneth D. Merry static int
16520191d9b3SAlexander Motin camperiphscsisenseerror(union ccb *ccb, union ccb **orig,
16530191d9b3SAlexander Motin     cam_flags camflags, u_int32_t sense_flags,
16543393f8daSKenneth D. Merry     int *openings, u_int32_t *relsim_flags,
16550181d54bSAlexander Motin     u_int32_t *timeout, u_int32_t *action, const char **action_string)
16563393f8daSKenneth D. Merry {
16573393f8daSKenneth D. Merry 	struct cam_periph *periph;
165883c5d981SAlexander Motin 	union ccb *orig_ccb = ccb;
16590191d9b3SAlexander Motin 	int error, recoveryccb;
166013aa56fcSEdward Tomasz Napierala 	u_int16_t flags;
16613393f8daSKenneth D. Merry 
16628532d381SConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
16638532d381SConrad Meyer 	if (ccb->ccb_h.func_code == XPT_SCSI_IO && ccb->csio.bio != NULL)
16648532d381SConrad Meyer 		biotrack(ccb->csio.bio, __func__);
16658532d381SConrad Meyer #endif
16668532d381SConrad Meyer 
16673393f8daSKenneth D. Merry 	periph = xpt_path_periph(ccb->ccb_h.path);
16680191d9b3SAlexander Motin 	recoveryccb = (ccb->ccb_h.cbfcnp == camperiphdone);
16690191d9b3SAlexander Motin 	if ((periph->flags & CAM_PERIPH_RECOVERY_INPROG) && !recoveryccb) {
16703393f8daSKenneth D. Merry 		/*
16713393f8daSKenneth D. Merry 		 * If error recovery is already in progress, don't attempt
16723393f8daSKenneth D. Merry 		 * to process this error, but requeue it unconditionally
16733393f8daSKenneth D. Merry 		 * and attempt to process it once error recovery has
16743393f8daSKenneth D. Merry 		 * completed.  This failed command is probably related to
16753393f8daSKenneth D. Merry 		 * the error that caused the currently active error recovery
16763393f8daSKenneth D. Merry 		 * action so our  current recovery efforts should also
16773393f8daSKenneth D. Merry 		 * address this command.  Be aware that the error recovery
16783393f8daSKenneth D. Merry 		 * code assumes that only one recovery action is in progress
16793393f8daSKenneth D. Merry 		 * on a particular peripheral instance at any given time
16803393f8daSKenneth D. Merry 		 * (e.g. only one saved CCB for error recovery) so it is
16813393f8daSKenneth D. Merry 		 * imperitive that we don't violate this assumption.
16823393f8daSKenneth D. Merry 		 */
16833393f8daSKenneth D. Merry 		error = ERESTART;
16840181d54bSAlexander Motin 		*action &= ~SSQ_PRINT_SENSE;
16853393f8daSKenneth D. Merry 	} else {
16863393f8daSKenneth D. Merry 		scsi_sense_action err_action;
16873393f8daSKenneth D. Merry 		struct ccb_getdev cgd;
16883393f8daSKenneth D. Merry 
16893393f8daSKenneth D. Merry 		/*
16903393f8daSKenneth D. Merry 		 * Grab the inquiry data for this device.
16913393f8daSKenneth D. Merry 		 */
1692076686feSEdward Tomasz Napierala 		memset(&cgd, 0, sizeof(cgd));
1693bbfa4aa1SAlexander Motin 		xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, CAM_PRIORITY_NORMAL);
16943393f8daSKenneth D. Merry 		cgd.ccb_h.func_code = XPT_GDEV_TYPE;
16953393f8daSKenneth D. Merry 		xpt_action((union ccb *)&cgd);
16963393f8daSKenneth D. Merry 
1697e7493b28SAlexander Motin 		err_action = scsi_error_action(&ccb->csio, &cgd.inq_data,
16983393f8daSKenneth D. Merry 		    sense_flags);
16993393f8daSKenneth D. Merry 		error = err_action & SS_ERRMASK;
17003393f8daSKenneth D. Merry 
17013393f8daSKenneth D. Merry 		/*
17020191d9b3SAlexander Motin 		 * Do not autostart sequential access devices
17030191d9b3SAlexander Motin 		 * to avoid unexpected tape loading.
17040191d9b3SAlexander Motin 		 */
17050191d9b3SAlexander Motin 		if ((err_action & SS_MASK) == SS_START &&
17060191d9b3SAlexander Motin 		    SID_TYPE(&cgd.inq_data) == T_SEQUENTIAL) {
17070191d9b3SAlexander Motin 			*action_string = "Will not autostart a "
17080191d9b3SAlexander Motin 			    "sequential access device";
17090191d9b3SAlexander Motin 			goto sense_error_done;
17100191d9b3SAlexander Motin 		}
17110191d9b3SAlexander Motin 
17120191d9b3SAlexander Motin 		/*
17130191d9b3SAlexander Motin 		 * Avoid recovery recursion if recovery action is the same.
17140191d9b3SAlexander Motin 		 */
17150191d9b3SAlexander Motin 		if ((err_action & SS_MASK) >= SS_START && recoveryccb) {
17160191d9b3SAlexander Motin 			if (((err_action & SS_MASK) == SS_START &&
17170191d9b3SAlexander Motin 			     ccb->csio.cdb_io.cdb_bytes[0] == START_STOP_UNIT) ||
17180191d9b3SAlexander Motin 			    ((err_action & SS_MASK) == SS_TUR &&
17190191d9b3SAlexander Motin 			     (ccb->csio.cdb_io.cdb_bytes[0] == TEST_UNIT_READY))) {
17200191d9b3SAlexander Motin 				err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO;
17210191d9b3SAlexander Motin 				*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
17220191d9b3SAlexander Motin 				*timeout = 500;
17230191d9b3SAlexander Motin 			}
17240191d9b3SAlexander Motin 		}
17250191d9b3SAlexander Motin 
17260191d9b3SAlexander Motin 		/*
17273393f8daSKenneth D. Merry 		 * If the recovery action will consume a retry,
17283393f8daSKenneth D. Merry 		 * make sure we actually have retries available.
17293393f8daSKenneth D. Merry 		 */
17303393f8daSKenneth D. Merry 		if ((err_action & SSQ_DECREMENT_COUNT) != 0) {
1731a07e846bSAlexander Motin 		 	if (ccb->ccb_h.retry_count > 0 &&
1732a07e846bSAlexander Motin 			    (periph->flags & CAM_PERIPH_INVALID) == 0)
17333393f8daSKenneth D. Merry 		 		ccb->ccb_h.retry_count--;
17343393f8daSKenneth D. Merry 			else {
173583c5d981SAlexander Motin 				*action_string = "Retries exhausted";
17363393f8daSKenneth D. Merry 				goto sense_error_done;
17373393f8daSKenneth D. Merry 			}
17383393f8daSKenneth D. Merry 		}
17393393f8daSKenneth D. Merry 
17403393f8daSKenneth D. Merry 		if ((err_action & SS_MASK) >= SS_START) {
17413393f8daSKenneth D. Merry 			/*
17423393f8daSKenneth D. Merry 			 * Do common portions of commands that
17433393f8daSKenneth D. Merry 			 * use recovery CCBs.
17443393f8daSKenneth D. Merry 			 */
174583c5d981SAlexander Motin 			orig_ccb = xpt_alloc_ccb_nowait();
174683c5d981SAlexander Motin 			if (orig_ccb == NULL) {
174783c5d981SAlexander Motin 				*action_string = "Can't allocate recovery CCB";
17483393f8daSKenneth D. Merry 				goto sense_error_done;
17493393f8daSKenneth D. Merry 			}
17501f1158b2SAlexander Motin 			/*
17511f1158b2SAlexander Motin 			 * Clear freeze flag for original request here, as
17521f1158b2SAlexander Motin 			 * this freeze will be dropped as part of ERESTART.
17531f1158b2SAlexander Motin 			 */
17541f1158b2SAlexander Motin 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
175513aa56fcSEdward Tomasz Napierala 
175613aa56fcSEdward Tomasz Napierala 			KASSERT(ccb->ccb_h.func_code == XPT_SCSI_IO,
175713aa56fcSEdward Tomasz Napierala 			    ("%s: ccb func_code %#x != XPT_SCSI_IO",
175813aa56fcSEdward Tomasz Napierala 			     __func__, ccb->ccb_h.func_code));
175913aa56fcSEdward Tomasz Napierala 			flags = orig_ccb->ccb_h.alloc_flags;
176013aa56fcSEdward Tomasz Napierala 			bcopy(ccb, orig_ccb, sizeof(struct ccb_scsiio));
176113aa56fcSEdward Tomasz Napierala 			orig_ccb->ccb_h.alloc_flags = flags;
17623393f8daSKenneth D. Merry 		}
17633393f8daSKenneth D. Merry 
17643393f8daSKenneth D. Merry 		switch (err_action & SS_MASK) {
17653393f8daSKenneth D. Merry 		case SS_NOP:
176683c5d981SAlexander Motin 			*action_string = "No recovery action needed";
176700e54d14SKenneth D. Merry 			error = 0;
176800e54d14SKenneth D. Merry 			break;
17693393f8daSKenneth D. Merry 		case SS_RETRY:
177083c5d981SAlexander Motin 			*action_string = "Retrying command (per sense data)";
17713393f8daSKenneth D. Merry 			error = ERESTART;
17728b8a9b1dSJustin T. Gibbs 			break;
17733393f8daSKenneth D. Merry 		case SS_FAIL:
177483c5d981SAlexander Motin 			*action_string = "Unretryable error";
17753393f8daSKenneth D. Merry 			break;
17763393f8daSKenneth D. Merry 		case SS_START:
17773393f8daSKenneth D. Merry 		{
17783393f8daSKenneth D. Merry 			int le;
17793393f8daSKenneth D. Merry 
17803393f8daSKenneth D. Merry 			/*
17813393f8daSKenneth D. Merry 			 * Send a start unit command to the device, and
17823393f8daSKenneth D. Merry 			 * then retry the command.
17833393f8daSKenneth D. Merry 			 */
178483c5d981SAlexander Motin 			*action_string = "Attempting to start unit";
178583c5d981SAlexander Motin 			periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
17863393f8daSKenneth D. Merry 
17873393f8daSKenneth D. Merry 			/*
17883393f8daSKenneth D. Merry 			 * Check for removable media and set
17893393f8daSKenneth D. Merry 			 * load/eject flag appropriately.
17903393f8daSKenneth D. Merry 			 */
17913393f8daSKenneth D. Merry 			if (SID_IS_REMOVABLE(&cgd.inq_data))
17923393f8daSKenneth D. Merry 				le = TRUE;
17933393f8daSKenneth D. Merry 			else
17943393f8daSKenneth D. Merry 				le = FALSE;
17953393f8daSKenneth D. Merry 
17963393f8daSKenneth D. Merry 			scsi_start_stop(&ccb->csio,
17973393f8daSKenneth D. Merry 					/*retries*/1,
17983393f8daSKenneth D. Merry 					camperiphdone,
17993393f8daSKenneth D. Merry 					MSG_SIMPLE_Q_TAG,
18003393f8daSKenneth D. Merry 					/*start*/TRUE,
18013393f8daSKenneth D. Merry 					/*load/eject*/le,
18023393f8daSKenneth D. Merry 					/*immediate*/FALSE,
18033393f8daSKenneth D. Merry 					SSD_FULL_SIZE,
18043393f8daSKenneth D. Merry 					/*timeout*/50000);
18053393f8daSKenneth D. Merry 			break;
18063393f8daSKenneth D. Merry 		}
18073393f8daSKenneth D. Merry 		case SS_TUR:
18083393f8daSKenneth D. Merry 		{
18093393f8daSKenneth D. Merry 			/*
18103393f8daSKenneth D. Merry 			 * Send a Test Unit Ready to the device.
18113393f8daSKenneth D. Merry 			 * If the 'many' flag is set, we send 120
18123393f8daSKenneth D. Merry 			 * test unit ready commands, one every half
18133393f8daSKenneth D. Merry 			 * second.  Otherwise, we just send one TUR.
18143393f8daSKenneth D. Merry 			 * We only want to do this if the retry
18153393f8daSKenneth D. Merry 			 * count has not been exhausted.
18163393f8daSKenneth D. Merry 			 */
18173393f8daSKenneth D. Merry 			int retries;
18183393f8daSKenneth D. Merry 
1819bae3729bSAlexander Motin 			if ((err_action & SSQ_MANY) != 0 && (periph->flags &
1820bae3729bSAlexander Motin 			     CAM_PERIPH_RECOVERY_WAIT_FAILED) == 0) {
1821bae3729bSAlexander Motin 				periph->flags |= CAM_PERIPH_RECOVERY_WAIT;
182283c5d981SAlexander Motin 				*action_string = "Polling device for readiness";
18233393f8daSKenneth D. Merry 				retries = 120;
18243393f8daSKenneth D. Merry 			} else {
182583c5d981SAlexander Motin 				*action_string = "Testing device for readiness";
18263393f8daSKenneth D. Merry 				retries = 1;
18273393f8daSKenneth D. Merry 			}
182883c5d981SAlexander Motin 			periph->flags |= CAM_PERIPH_RECOVERY_INPROG;
18293393f8daSKenneth D. Merry 			scsi_test_unit_ready(&ccb->csio,
18303393f8daSKenneth D. Merry 					     retries,
18313393f8daSKenneth D. Merry 					     camperiphdone,
18323393f8daSKenneth D. Merry 					     MSG_SIMPLE_Q_TAG,
18333393f8daSKenneth D. Merry 					     SSD_FULL_SIZE,
18343393f8daSKenneth D. Merry 					     /*timeout*/5000);
18353393f8daSKenneth D. Merry 
18363393f8daSKenneth D. Merry 			/*
18373393f8daSKenneth D. Merry 			 * Accomplish our 500ms delay by deferring
18383393f8daSKenneth D. Merry 			 * the release of our device queue appropriately.
18393393f8daSKenneth D. Merry 			 */
18403393f8daSKenneth D. Merry 			*relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
18413393f8daSKenneth D. Merry 			*timeout = 500;
18423393f8daSKenneth D. Merry 			break;
18433393f8daSKenneth D. Merry 		}
18443393f8daSKenneth D. Merry 		default:
1845e3c29144SWarner Losh 			panic("Unhandled error action %x", err_action);
18463393f8daSKenneth D. Merry 		}
18473393f8daSKenneth D. Merry 
18483393f8daSKenneth D. Merry 		if ((err_action & SS_MASK) >= SS_START) {
18493393f8daSKenneth D. Merry 			/*
1850bbfa4aa1SAlexander Motin 			 * Drop the priority, so that the recovery
18513393f8daSKenneth D. Merry 			 * CCB is the first to execute.  Freeze the queue
18523393f8daSKenneth D. Merry 			 * after this command is sent so that we can
18533393f8daSKenneth D. Merry 			 * restore the old csio and have it queued in
18543393f8daSKenneth D. Merry 			 * the proper order before we release normal
18553393f8daSKenneth D. Merry 			 * transactions to the device.
18563393f8daSKenneth D. Merry 			 */
185783c5d981SAlexander Motin 			ccb->ccb_h.pinfo.priority--;
18583393f8daSKenneth D. Merry 			ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
185983c5d981SAlexander Motin 			ccb->ccb_h.saved_ccb_ptr = orig_ccb;
18603393f8daSKenneth D. Merry 			error = ERESTART;
18610191d9b3SAlexander Motin 			*orig = orig_ccb;
18623393f8daSKenneth D. Merry 		}
18633393f8daSKenneth D. Merry 
18643393f8daSKenneth D. Merry sense_error_done:
18650181d54bSAlexander Motin 		*action = err_action;
18663393f8daSKenneth D. Merry 	}
18673393f8daSKenneth D. Merry 	return (error);
18683393f8daSKenneth D. Merry }
18693393f8daSKenneth D. Merry 
18703393f8daSKenneth D. Merry /*
18713393f8daSKenneth D. Merry  * Generic error handler.  Peripheral drivers usually filter
18721ffe5851SPedro F. Giffuni  * out the errors that they handle in a unique manner, then
18733393f8daSKenneth D. Merry  * call this function.
18743393f8daSKenneth D. Merry  */
18753393f8daSKenneth D. Merry int
18763393f8daSKenneth D. Merry cam_periph_error(union ccb *ccb, cam_flags camflags,
1877553484aeSWarner Losh 		 u_int32_t sense_flags)
18783393f8daSKenneth D. Merry {
18790181d54bSAlexander Motin 	struct cam_path *newpath;
18800181d54bSAlexander Motin 	union ccb  *orig_ccb, *scan_ccb;
1881a07e846bSAlexander Motin 	struct cam_periph *periph;
18823393f8daSKenneth D. Merry 	const char *action_string;
18833393f8daSKenneth D. Merry 	cam_status  status;
1884a30ecd42SScott Long 	int	    frozen, error, openings, devctl_err;
18850181d54bSAlexander Motin 	u_int32_t   action, relsim_flags, timeout;
18863393f8daSKenneth D. Merry 
18870181d54bSAlexander Motin 	action = SSQ_PRINT_SENSE;
1888a07e846bSAlexander Motin 	periph = xpt_path_periph(ccb->ccb_h.path);
18893393f8daSKenneth D. Merry 	action_string = NULL;
18903393f8daSKenneth D. Merry 	status = ccb->ccb_h.status;
18913393f8daSKenneth D. Merry 	frozen = (status & CAM_DEV_QFRZN) != 0;
18923393f8daSKenneth D. Merry 	status &= CAM_STATUS_MASK;
1893a30ecd42SScott Long 	devctl_err = openings = relsim_flags = timeout = 0;
18940191d9b3SAlexander Motin 	orig_ccb = ccb;
18953393f8daSKenneth D. Merry 
1896a30ecd42SScott Long 	/* Filter the errors that should be reported via devctl */
1897a30ecd42SScott Long 	switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
1898a30ecd42SScott Long 	case CAM_CMD_TIMEOUT:
1899a30ecd42SScott Long 	case CAM_REQ_ABORTED:
1900a30ecd42SScott Long 	case CAM_REQ_CMP_ERR:
1901a30ecd42SScott Long 	case CAM_REQ_TERMIO:
1902a30ecd42SScott Long 	case CAM_UNREC_HBA_ERROR:
1903a30ecd42SScott Long 	case CAM_DATA_RUN_ERR:
1904a30ecd42SScott Long 	case CAM_SCSI_STATUS_ERROR:
1905a30ecd42SScott Long 	case CAM_ATA_STATUS_ERROR:
1906a30ecd42SScott Long 	case CAM_SMP_STATUS_ERROR:
1907a30ecd42SScott Long 		devctl_err++;
1908a30ecd42SScott Long 		break;
1909a30ecd42SScott Long 	default:
1910a30ecd42SScott Long 		break;
1911a30ecd42SScott Long 	}
1912a30ecd42SScott Long 
19133393f8daSKenneth D. Merry 	switch (status) {
19143393f8daSKenneth D. Merry 	case CAM_REQ_CMP:
19153393f8daSKenneth D. Merry 		error = 0;
19160181d54bSAlexander Motin 		action &= ~SSQ_PRINT_SENSE;
19173393f8daSKenneth D. Merry 		break;
19183393f8daSKenneth D. Merry 	case CAM_SCSI_STATUS_ERROR:
19190191d9b3SAlexander Motin 		error = camperiphscsistatuserror(ccb, &orig_ccb,
19200191d9b3SAlexander Motin 		    camflags, sense_flags, &openings, &relsim_flags,
19210181d54bSAlexander Motin 		    &timeout, &action, &action_string);
19223393f8daSKenneth D. Merry 		break;
19233393f8daSKenneth D. Merry 	case CAM_AUTOSENSE_FAIL:
1924c7ec4390SMatt Jacob 		error = EIO;	/* we have to kill the command */
1925c7ec4390SMatt Jacob 		break;
19268b8a9b1dSJustin T. Gibbs 	case CAM_UA_ABORT:
19278b8a9b1dSJustin T. Gibbs 	case CAM_UA_TERMIO:
19288b8a9b1dSJustin T. Gibbs 	case CAM_MSG_REJECT_REC:
19298b8a9b1dSJustin T. Gibbs 		/* XXX Don't know that these are correct */
19308b8a9b1dSJustin T. Gibbs 		error = EIO;
19318b8a9b1dSJustin T. Gibbs 		break;
19328b8a9b1dSJustin T. Gibbs 	case CAM_SEL_TIMEOUT:
19333393f8daSKenneth D. Merry 		if ((camflags & CAM_RETRY_SELTO) != 0) {
1934a07e846bSAlexander Motin 			if (ccb->ccb_h.retry_count > 0 &&
1935a07e846bSAlexander Motin 			    (periph->flags & CAM_PERIPH_INVALID) == 0) {
19363393f8daSKenneth D. Merry 				ccb->ccb_h.retry_count--;
19373393f8daSKenneth D. Merry 				error = ERESTART;
19383393f8daSKenneth D. Merry 
19393393f8daSKenneth D. Merry 				/*
194073cf209fSMatt Jacob 				 * Wait a bit to give the device
19413393f8daSKenneth D. Merry 				 * time to recover before we try again.
19423393f8daSKenneth D. Merry 				 */
19433393f8daSKenneth D. Merry 				relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
194473cf209fSMatt Jacob 				timeout = periph_selto_delay;
19453393f8daSKenneth D. Merry 				break;
19463393f8daSKenneth D. Merry 			}
1947a07e846bSAlexander Motin 			action_string = "Retries exhausted";
19483393f8daSKenneth D. Merry 		}
19498900f4b8SKenneth D. Merry 		/* FALLTHROUGH */
19508900f4b8SKenneth D. Merry 	case CAM_DEV_NOT_THERE:
19513393f8daSKenneth D. Merry 		error = ENXIO;
19520181d54bSAlexander Motin 		action = SSQ_LOST;
19538b8a9b1dSJustin T. Gibbs 		break;
19548b8a9b1dSJustin T. Gibbs 	case CAM_REQ_INVALID:
19558b8a9b1dSJustin T. Gibbs 	case CAM_PATH_INVALID:
19568b8a9b1dSJustin T. Gibbs 	case CAM_NO_HBA:
19578b8a9b1dSJustin T. Gibbs 	case CAM_PROVIDE_FAIL:
19588b8a9b1dSJustin T. Gibbs 	case CAM_REQ_TOO_BIG:
19599a014e6fSIan Dowse 	case CAM_LUN_INVALID:
19609a014e6fSIan Dowse 	case CAM_TID_INVALID:
1961357478a5SAlexander Motin 	case CAM_FUNC_NOTAVAIL:
19628b8a9b1dSJustin T. Gibbs 		error = EINVAL;
19638b8a9b1dSJustin T. Gibbs 		break;
19648b8a9b1dSJustin T. Gibbs 	case CAM_SCSI_BUS_RESET:
19658b8a9b1dSJustin T. Gibbs 	case CAM_BDR_SENT:
19663393f8daSKenneth D. Merry 		/*
19673393f8daSKenneth D. Merry 		 * Commands that repeatedly timeout and cause these
19683393f8daSKenneth D. Merry 		 * kinds of error recovery actions, should return
19693393f8daSKenneth D. Merry 		 * CAM_CMD_TIMEOUT, which allows us to safely assume
19703393f8daSKenneth D. Merry 		 * that this command was an innocent bystander to
19713393f8daSKenneth D. Merry 		 * these events and should be unconditionally
19723393f8daSKenneth D. Merry 		 * retried.
19733393f8daSKenneth D. Merry 		 */
19748b8a9b1dSJustin T. Gibbs 	case CAM_REQUEUE_REQ:
19750191d9b3SAlexander Motin 		/* Unconditional requeue if device is still there */
19760191d9b3SAlexander Motin 		if (periph->flags & CAM_PERIPH_INVALID) {
19770191d9b3SAlexander Motin 			action_string = "Periph was invalidated";
19789fb40bafSWarner Losh 			error = ENXIO;
19790191d9b3SAlexander Motin 		} else if (sense_flags & SF_NO_RETRY) {
19800191d9b3SAlexander Motin 			error = EIO;
19810191d9b3SAlexander Motin 			action_string = "Retry was blocked";
198284c4de2dSAlexander Motin 		} else {
19830191d9b3SAlexander Motin 			error = ERESTART;
19840181d54bSAlexander Motin 			action &= ~SSQ_PRINT_SENSE;
198584c4de2dSAlexander Motin 		}
19868b8a9b1dSJustin T. Gibbs 		break;
19878b8a9b1dSJustin T. Gibbs 	case CAM_RESRC_UNAVAIL:
198873cf209fSMatt Jacob 		/* Wait a bit for the resource shortage to abate. */
198973cf209fSMatt Jacob 		timeout = periph_noresrc_delay;
199073cf209fSMatt Jacob 		/* FALLTHROUGH */
19918b8a9b1dSJustin T. Gibbs 	case CAM_BUSY:
199273cf209fSMatt Jacob 		if (timeout == 0) {
199373cf209fSMatt Jacob 			/* Wait a bit for the busy condition to abate. */
199473cf209fSMatt Jacob 			timeout = periph_busy_delay;
199573cf209fSMatt Jacob 		}
199673cf209fSMatt Jacob 		relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT;
199773cf209fSMatt Jacob 		/* FALLTHROUGH */
19980191d9b3SAlexander Motin 	case CAM_ATA_STATUS_ERROR:
1999*774ab87cSWarner Losh 	case CAM_NVME_STATUS_ERROR:
200033734ddfSWarner Losh 	case CAM_SMP_STATUS_ERROR:
20010191d9b3SAlexander Motin 	case CAM_REQ_CMP_ERR:
20020191d9b3SAlexander Motin 	case CAM_CMD_TIMEOUT:
20030191d9b3SAlexander Motin 	case CAM_UNEXP_BUSFREE:
20040191d9b3SAlexander Motin 	case CAM_UNCOR_PARITY:
20050191d9b3SAlexander Motin 	case CAM_DATA_RUN_ERR:
20068b8a9b1dSJustin T. Gibbs 	default:
20070191d9b3SAlexander Motin 		if (periph->flags & CAM_PERIPH_INVALID) {
20089fb40bafSWarner Losh 			error = ENXIO;
20090191d9b3SAlexander Motin 			action_string = "Periph was invalidated";
20100191d9b3SAlexander Motin 		} else if (ccb->ccb_h.retry_count == 0) {
20118b8a9b1dSJustin T. Gibbs 			error = EIO;
201283c5d981SAlexander Motin 			action_string = "Retries exhausted";
20130191d9b3SAlexander Motin 		} else if (sense_flags & SF_NO_RETRY) {
20140191d9b3SAlexander Motin 			error = EIO;
20150191d9b3SAlexander Motin 			action_string = "Retry was blocked";
20160191d9b3SAlexander Motin 		} else {
20170191d9b3SAlexander Motin 			ccb->ccb_h.retry_count--;
20180191d9b3SAlexander Motin 			error = ERESTART;
20198b8a9b1dSJustin T. Gibbs 		}
20208b8a9b1dSJustin T. Gibbs 		break;
20218b8a9b1dSJustin T. Gibbs 	}
20228b8a9b1dSJustin T. Gibbs 
20230191d9b3SAlexander Motin 	if ((sense_flags & SF_PRINT_ALWAYS) ||
20240191d9b3SAlexander Motin 	    CAM_DEBUGGED(ccb->ccb_h.path, CAM_DEBUG_INFO))
20250181d54bSAlexander Motin 		action |= SSQ_PRINT_SENSE;
20260191d9b3SAlexander Motin 	else if (sense_flags & SF_NO_PRINT)
20270181d54bSAlexander Motin 		action &= ~SSQ_PRINT_SENSE;
20280181d54bSAlexander Motin 	if ((action & SSQ_PRINT_SENSE) != 0)
20290191d9b3SAlexander Motin 		cam_error_print(orig_ccb, CAM_ESF_ALL, CAM_EPF_ALL);
20300181d54bSAlexander Motin 	if (error != 0 && (action & SSQ_PRINT_SENSE) != 0) {
20310f3e2159SAlexander Motin 		if (error != ERESTART) {
20323393f8daSKenneth D. Merry 			if (action_string == NULL)
203383c5d981SAlexander Motin 				action_string = "Unretryable error";
203483c5d981SAlexander Motin 			xpt_print(ccb->ccb_h.path, "Error %d, %s\n",
203583c5d981SAlexander Motin 			    error, action_string);
203683c5d981SAlexander Motin 		} else if (action_string != NULL)
2037f0d9af51SMatt Jacob 			xpt_print(ccb->ccb_h.path, "%s\n", action_string);
2038bc40691eSWarner Losh 		else {
2039bc40691eSWarner Losh 			xpt_print(ccb->ccb_h.path,
2040bc40691eSWarner Losh 			    "Retrying command, %d more tries remain\n",
2041bc40691eSWarner Losh 			    ccb->ccb_h.retry_count);
2042bc40691eSWarner Losh 		}
20430f3e2159SAlexander Motin 	}
20440f3e2159SAlexander Motin 
2045cd375264SAlexander Motin 	if (devctl_err && (error != 0 || (action & SSQ_PRINT_SENSE) != 0))
2046a30ecd42SScott Long 		cam_periph_devctl_notify(orig_ccb);
2047a30ecd42SScott Long 
20480181d54bSAlexander Motin 	if ((action & SSQ_LOST) != 0) {
20490191d9b3SAlexander Motin 		lun_id_t lun_id;
20500191d9b3SAlexander Motin 
20510191d9b3SAlexander Motin 		/*
20520191d9b3SAlexander Motin 		 * For a selection timeout, we consider all of the LUNs on
20530191d9b3SAlexander Motin 		 * the target to be gone.  If the status is CAM_DEV_NOT_THERE,
20540191d9b3SAlexander Motin 		 * then we only get rid of the device(s) specified by the
20550191d9b3SAlexander Motin 		 * path in the original CCB.
20560191d9b3SAlexander Motin 		 */
20570181d54bSAlexander Motin 		if (status == CAM_SEL_TIMEOUT)
20580191d9b3SAlexander Motin 			lun_id = CAM_LUN_WILDCARD;
20590181d54bSAlexander Motin 		else
20600181d54bSAlexander Motin 			lun_id = xpt_path_lun_id(ccb->ccb_h.path);
20610191d9b3SAlexander Motin 
20620191d9b3SAlexander Motin 		/* Should we do more if we can't create the path?? */
20630191d9b3SAlexander Motin 		if (xpt_create_path(&newpath, periph,
20640191d9b3SAlexander Motin 				    xpt_path_path_id(ccb->ccb_h.path),
20650191d9b3SAlexander Motin 				    xpt_path_target_id(ccb->ccb_h.path),
20660191d9b3SAlexander Motin 				    lun_id) == CAM_REQ_CMP) {
20670191d9b3SAlexander Motin 			/*
20680191d9b3SAlexander Motin 			 * Let peripheral drivers know that this
20690191d9b3SAlexander Motin 			 * device has gone away.
20700191d9b3SAlexander Motin 			 */
20710191d9b3SAlexander Motin 			xpt_async(AC_LOST_DEVICE, newpath, NULL);
20720191d9b3SAlexander Motin 			xpt_free_path(newpath);
20730191d9b3SAlexander Motin 		}
20740181d54bSAlexander Motin 	}
20753631c638SAlexander Motin 
20763631c638SAlexander Motin 	/* Broadcast UNIT ATTENTIONs to all periphs. */
20770181d54bSAlexander Motin 	if ((action & SSQ_UA) != 0)
20783631c638SAlexander Motin 		xpt_async(AC_UNIT_ATTENTION, orig_ccb->ccb_h.path, orig_ccb);
20790181d54bSAlexander Motin 
20800181d54bSAlexander Motin 	/* Rescan target on "Reported LUNs data has changed" */
20810181d54bSAlexander Motin 	if ((action & SSQ_RESCAN) != 0) {
20820181d54bSAlexander Motin 		if (xpt_create_path(&newpath, NULL,
20830181d54bSAlexander Motin 				    xpt_path_path_id(ccb->ccb_h.path),
20840181d54bSAlexander Motin 				    xpt_path_target_id(ccb->ccb_h.path),
2085e5736ac8SAlexander Motin 				    CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
20860181d54bSAlexander Motin 			scan_ccb = xpt_alloc_ccb_nowait();
2087e5736ac8SAlexander Motin 			if (scan_ccb != NULL) {
20880181d54bSAlexander Motin 				scan_ccb->ccb_h.path = newpath;
2089e5736ac8SAlexander Motin 				scan_ccb->ccb_h.func_code = XPT_SCAN_TGT;
20900181d54bSAlexander Motin 				scan_ccb->crcn.flags = 0;
20910181d54bSAlexander Motin 				xpt_rescan(scan_ccb);
20923231e8bdSAlexander Motin 			} else {
2093e5736ac8SAlexander Motin 				xpt_print(newpath,
2094e5736ac8SAlexander Motin 				    "Can't allocate CCB to rescan target\n");
20953231e8bdSAlexander Motin 				xpt_free_path(newpath);
20963231e8bdSAlexander Motin 			}
20970181d54bSAlexander Motin 		}
20980191d9b3SAlexander Motin 	}
20990191d9b3SAlexander Motin 
21000f3e2159SAlexander Motin 	/* Attempt a retry */
21010f3e2159SAlexander Motin 	if (error == ERESTART || error == 0) {
21020f3e2159SAlexander Motin 		if (frozen != 0)
21030f3e2159SAlexander Motin 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
21040f3e2159SAlexander Motin 		if (error == ERESTART)
21050f3e2159SAlexander Motin 			xpt_action(ccb);
21060f3e2159SAlexander Motin 		if (frozen != 0)
21070f3e2159SAlexander Motin 			cam_release_devq(ccb->ccb_h.path,
21080f3e2159SAlexander Motin 					 relsim_flags,
21090f3e2159SAlexander Motin 					 openings,
21100f3e2159SAlexander Motin 					 timeout,
21110f3e2159SAlexander Motin 					 /*getcount_only*/0);
21123393f8daSKenneth D. Merry 	}
21138b8a9b1dSJustin T. Gibbs 
21148b8a9b1dSJustin T. Gibbs 	return (error);
21158b8a9b1dSJustin T. Gibbs }
2116a30ecd42SScott Long 
2117a30ecd42SScott Long #define CAM_PERIPH_DEVD_MSG_SIZE	256
2118a30ecd42SScott Long 
2119a30ecd42SScott Long static void
2120a30ecd42SScott Long cam_periph_devctl_notify(union ccb *ccb)
2121a30ecd42SScott Long {
2122a30ecd42SScott Long 	struct cam_periph *periph;
2123a30ecd42SScott Long 	struct ccb_getdev *cgd;
2124a30ecd42SScott Long 	struct sbuf sb;
2125a30ecd42SScott Long 	int serr, sk, asc, ascq;
2126a30ecd42SScott Long 	char *sbmsg, *type;
2127a30ecd42SScott Long 
2128a30ecd42SScott Long 	sbmsg = malloc(CAM_PERIPH_DEVD_MSG_SIZE, M_CAMPERIPH, M_NOWAIT);
2129a30ecd42SScott Long 	if (sbmsg == NULL)
2130a30ecd42SScott Long 		return;
2131a30ecd42SScott Long 
2132a30ecd42SScott Long 	sbuf_new(&sb, sbmsg, CAM_PERIPH_DEVD_MSG_SIZE, SBUF_FIXEDLEN);
2133a30ecd42SScott Long 
2134a30ecd42SScott Long 	periph = xpt_path_periph(ccb->ccb_h.path);
2135a30ecd42SScott Long 	sbuf_printf(&sb, "device=%s%d ", periph->periph_name,
2136a30ecd42SScott Long 	    periph->unit_number);
2137a30ecd42SScott Long 
2138a30ecd42SScott Long 	sbuf_printf(&sb, "serial=\"");
2139a30ecd42SScott Long 	if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) != NULL) {
2140a30ecd42SScott Long 		xpt_setup_ccb(&cgd->ccb_h, ccb->ccb_h.path,
2141a30ecd42SScott Long 		    CAM_PRIORITY_NORMAL);
2142a30ecd42SScott Long 		cgd->ccb_h.func_code = XPT_GDEV_TYPE;
2143a30ecd42SScott Long 		xpt_action((union ccb *)cgd);
2144a30ecd42SScott Long 
2145a30ecd42SScott Long 		if (cgd->ccb_h.status == CAM_REQ_CMP)
2146a30ecd42SScott Long 			sbuf_bcat(&sb, cgd->serial_num, cgd->serial_num_len);
2147de482e7bSScott Long 		xpt_free_ccb((union ccb *)cgd);
2148a30ecd42SScott Long 	}
2149a30ecd42SScott Long 	sbuf_printf(&sb, "\" ");
2150a30ecd42SScott Long 	sbuf_printf(&sb, "cam_status=\"0x%x\" ", ccb->ccb_h.status);
2151a30ecd42SScott Long 
2152a30ecd42SScott Long 	switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
2153a30ecd42SScott Long 	case CAM_CMD_TIMEOUT:
2154a30ecd42SScott Long 		sbuf_printf(&sb, "timeout=%d ", ccb->ccb_h.timeout);
2155a30ecd42SScott Long 		type = "timeout";
2156a30ecd42SScott Long 		break;
2157a30ecd42SScott Long 	case CAM_SCSI_STATUS_ERROR:
2158a30ecd42SScott Long 		sbuf_printf(&sb, "scsi_status=%d ", ccb->csio.scsi_status);
2159a30ecd42SScott Long 		if (scsi_extract_sense_ccb(ccb, &serr, &sk, &asc, &ascq))
2160a30ecd42SScott Long 			sbuf_printf(&sb, "scsi_sense=\"%02x %02x %02x %02x\" ",
2161a30ecd42SScott Long 			    serr, sk, asc, ascq);
2162a30ecd42SScott Long 		type = "error";
2163a30ecd42SScott Long 		break;
2164a30ecd42SScott Long 	case CAM_ATA_STATUS_ERROR:
2165a30ecd42SScott Long 		sbuf_printf(&sb, "RES=\"");
2166a30ecd42SScott Long 		ata_res_sbuf(&ccb->ataio.res, &sb);
2167a30ecd42SScott Long 		sbuf_printf(&sb, "\" ");
2168a30ecd42SScott Long 		type = "error";
2169a30ecd42SScott Long 		break;
2170a30ecd42SScott Long 	default:
2171a30ecd42SScott Long 		type = "error";
2172a30ecd42SScott Long 		break;
2173a30ecd42SScott Long 	}
2174a30ecd42SScott Long 
2175a30ecd42SScott Long 	if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
2176a30ecd42SScott Long 		sbuf_printf(&sb, "CDB=\"");
21774902e14dSAlexander Motin 		scsi_cdb_sbuf(scsiio_cdb_ptr(&ccb->csio), &sb);
2178a30ecd42SScott Long 		sbuf_printf(&sb, "\" ");
2179a30ecd42SScott Long 	} else if (ccb->ccb_h.func_code == XPT_ATA_IO) {
2180a30ecd42SScott Long 		sbuf_printf(&sb, "ACB=\"");
2181a30ecd42SScott Long 		ata_cmd_sbuf(&ccb->ataio.cmd, &sb);
2182a30ecd42SScott Long 		sbuf_printf(&sb, "\" ");
2183a30ecd42SScott Long 	}
2184a30ecd42SScott Long 
2185a30ecd42SScott Long 	if (sbuf_finish(&sb) == 0)
2186a30ecd42SScott Long 		devctl_notify("CAM", "periph", type, sbuf_data(&sb));
2187a30ecd42SScott Long 	sbuf_delete(&sb);
2188a30ecd42SScott Long 	free(sbmsg, M_CAMPERIPH);
2189a30ecd42SScott Long }
2190a30ecd42SScott Long 
2191d38677d2SWarner Losh /*
2192d38677d2SWarner Losh  * Sysctl to force an invalidation of the drive right now. Can be
2193d38677d2SWarner Losh  * called with CTLFLAG_MPSAFE since we take periph lock.
2194d38677d2SWarner Losh  */
2195d38677d2SWarner Losh int
2196d38677d2SWarner Losh cam_periph_invalidate_sysctl(SYSCTL_HANDLER_ARGS)
2197d38677d2SWarner Losh {
2198d38677d2SWarner Losh 	struct cam_periph *periph;
2199d38677d2SWarner Losh 	int error, value;
2200d38677d2SWarner Losh 
2201d38677d2SWarner Losh 	periph = arg1;
2202d38677d2SWarner Losh 	value = 0;
2203d38677d2SWarner Losh 	error = sysctl_handle_int(oidp, &value, 0, req);
2204d38677d2SWarner Losh 	if (error != 0 || req->newptr == NULL || value != 1)
2205d38677d2SWarner Losh 		return (error);
2206d38677d2SWarner Losh 
2207d38677d2SWarner Losh 	cam_periph_lock(periph);
2208d38677d2SWarner Losh 	cam_periph_invalidate(periph);
2209d38677d2SWarner Losh 	cam_periph_unlock(periph);
2210d38677d2SWarner Losh 
2211d38677d2SWarner Losh 	return (0);
2212d38677d2SWarner Losh }
2213