xref: /freebsd/sys/cam/cam_periph.h (revision d38677d23c2545b2d98c93ef13a6c9af9c2d28cc)
1898b0535SWarner Losh /*-
28b8a9b1dSJustin T. Gibbs  * Data structures and definitions for CAM peripheral ("type") drivers.
38b8a9b1dSJustin T. Gibbs  *
4bec9534dSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5bec9534dSPedro F. Giffuni  *
68b8a9b1dSJustin T. Gibbs  * Copyright (c) 1997, 1998 Justin T. Gibbs.
78b8a9b1dSJustin T. Gibbs  * All rights reserved.
88b8a9b1dSJustin T. Gibbs  *
98b8a9b1dSJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
108b8a9b1dSJustin T. Gibbs  * modification, are permitted provided that the following conditions
118b8a9b1dSJustin T. Gibbs  * are met:
128b8a9b1dSJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
138b8a9b1dSJustin T. Gibbs  *    notice, this list of conditions, and the following disclaimer,
148b8a9b1dSJustin T. Gibbs  *    without modification, immediately at the beginning of the file.
158b8a9b1dSJustin T. Gibbs  * 2. The name of the author may not be used to endorse or promote products
168b8a9b1dSJustin T. Gibbs  *    derived from this software without specific prior written permission.
178b8a9b1dSJustin T. Gibbs  *
188b8a9b1dSJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
198b8a9b1dSJustin T. Gibbs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
208b8a9b1dSJustin T. Gibbs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
218b8a9b1dSJustin T. Gibbs  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
228b8a9b1dSJustin T. Gibbs  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
238b8a9b1dSJustin T. Gibbs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
248b8a9b1dSJustin T. Gibbs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
258b8a9b1dSJustin T. Gibbs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
268b8a9b1dSJustin T. Gibbs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
278b8a9b1dSJustin T. Gibbs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
288b8a9b1dSJustin T. Gibbs  * SUCH DAMAGE.
298b8a9b1dSJustin T. Gibbs  *
30c3aac50fSPeter Wemm  * $FreeBSD$
318b8a9b1dSJustin T. Gibbs  */
328b8a9b1dSJustin T. Gibbs 
338b8a9b1dSJustin T. Gibbs #ifndef _CAM_CAM_PERIPH_H
348b8a9b1dSJustin T. Gibbs #define _CAM_CAM_PERIPH_H 1
358b8a9b1dSJustin T. Gibbs 
368b8a9b1dSJustin T. Gibbs #include <sys/queue.h>
3711e4faceSScott Long #include <cam/cam_sim.h>
388b8a9b1dSJustin T. Gibbs 
39c4473420SPeter Wemm #ifdef _KERNEL
40*d38677d2SWarner Losh #include <sys/sysctl.h>
41227d67aaSAlexander Motin #include <sys/taskqueue.h>
428b8a9b1dSJustin T. Gibbs 
4303efffd1SMarius Strobl #include <cam/cam_xpt.h>
4403efffd1SMarius Strobl 
4560794e04SPoul-Henning Kamp struct devstat;
4660794e04SPoul-Henning Kamp 
479a1c8571SNick Hibma extern struct cam_periph *xpt_periph;
48540d9130SNick Hibma 
490b7c27b9SPeter Wemm extern struct periph_driver **periph_drivers;
500b7c27b9SPeter Wemm void periphdriver_register(void *);
5194173c3cSAlexander Motin int periphdriver_unregister(void *);
5283c5d981SAlexander Motin void periphdriver_init(int level);
530b7c27b9SPeter Wemm 
540b7c27b9SPeter Wemm #include <sys/module.h>
550b7c27b9SPeter Wemm #define PERIPHDRIVER_DECLARE(name, driver) \
560b7c27b9SPeter Wemm 	static int name ## _modevent(module_t mod, int type, void *data) \
570b7c27b9SPeter Wemm 	{ \
580b7c27b9SPeter Wemm 		switch (type) { \
590b7c27b9SPeter Wemm 		case MOD_LOAD: \
600b7c27b9SPeter Wemm 			periphdriver_register(data); \
610b7c27b9SPeter Wemm 			break; \
620b7c27b9SPeter Wemm 		case MOD_UNLOAD: \
6394173c3cSAlexander Motin 			return (periphdriver_unregister(data)); \
643e019deaSPoul-Henning Kamp 		default: \
653e019deaSPoul-Henning Kamp 			return EOPNOTSUPP; \
660b7c27b9SPeter Wemm 		} \
670b7c27b9SPeter Wemm 		return 0; \
680b7c27b9SPeter Wemm 	} \
690b7c27b9SPeter Wemm 	static moduledata_t name ## _mod = { \
700b7c27b9SPeter Wemm 		#name, \
710b7c27b9SPeter Wemm 		name ## _modevent, \
720b7c27b9SPeter Wemm 		(void *)&driver \
730b7c27b9SPeter Wemm 	}; \
740b7c27b9SPeter Wemm 	DECLARE_MODULE(name, name ## _mod, SI_SUB_DRIVERS, SI_ORDER_ANY); \
750b7c27b9SPeter Wemm 	MODULE_DEPEND(name, cam, 1, 1, 1)
768b8a9b1dSJustin T. Gibbs 
7794173c3cSAlexander Motin /*
7894173c3cSAlexander Motin  * Callback informing the peripheral driver it can perform it's
7994173c3cSAlexander Motin  * initialization since the XPT is now fully initialized.
808b8a9b1dSJustin T. Gibbs  */
8194173c3cSAlexander Motin typedef void (periph_init_t)(void);
8294173c3cSAlexander Motin 
8394173c3cSAlexander Motin /*
8494173c3cSAlexander Motin  * Callback requesting the peripheral driver to remove its instances
8594173c3cSAlexander Motin  * and shutdown, if possible.
8694173c3cSAlexander Motin  */
8794173c3cSAlexander Motin typedef int (periph_deinit_t)(void);
888b8a9b1dSJustin T. Gibbs 
898b8a9b1dSJustin T. Gibbs struct periph_driver {
9094173c3cSAlexander Motin 	periph_init_t		*init;
918b8a9b1dSJustin T. Gibbs 	char			*driver_name;
92e3975643SJake Burkholder 	TAILQ_HEAD(,cam_periph)	 units;
938b8a9b1dSJustin T. Gibbs 	u_int			 generation;
941e637ba6SAlexander Motin 	u_int			 flags;
951e637ba6SAlexander Motin #define CAM_PERIPH_DRV_EARLY		0x01
9694173c3cSAlexander Motin 	periph_deinit_t		*deinit;
978b8a9b1dSJustin T. Gibbs };
988b8a9b1dSJustin T. Gibbs 
998b8a9b1dSJustin T. Gibbs typedef enum {
1002e8f0ae6SScott Long 	CAM_PERIPH_BIO
1018b8a9b1dSJustin T. Gibbs } cam_periph_type;
1028b8a9b1dSJustin T. Gibbs 
1038fb3bbe7SGabor Kovesdan /* Generically useful offsets into the peripheral private area */
1048b8a9b1dSJustin T. Gibbs #define ppriv_ptr0 periph_priv.entries[0].ptr
1058b8a9b1dSJustin T. Gibbs #define ppriv_ptr1 periph_priv.entries[1].ptr
1068b8a9b1dSJustin T. Gibbs #define ppriv_field0 periph_priv.entries[0].field
1078b8a9b1dSJustin T. Gibbs #define ppriv_field1 periph_priv.entries[1].field
1088b8a9b1dSJustin T. Gibbs 
1098b8a9b1dSJustin T. Gibbs typedef void		periph_start_t (struct cam_periph *periph,
1108b8a9b1dSJustin T. Gibbs 					union ccb *start_ccb);
1118b8a9b1dSJustin T. Gibbs typedef cam_status	periph_ctor_t (struct cam_periph *periph,
1128b8a9b1dSJustin T. Gibbs 				       void *arg);
113ee9c90c7SKenneth D. Merry typedef void		periph_oninv_t (struct cam_periph *periph);
1148b8a9b1dSJustin T. Gibbs typedef void		periph_dtor_t (struct cam_periph *periph);
1158b8a9b1dSJustin T. Gibbs struct cam_periph {
1168b8a9b1dSJustin T. Gibbs 	periph_start_t		*periph_start;
117ee9c90c7SKenneth D. Merry 	periph_oninv_t		*periph_oninval;
1188b8a9b1dSJustin T. Gibbs 	periph_dtor_t		*periph_dtor;
1198b8a9b1dSJustin T. Gibbs 	char			*periph_name;
1208b8a9b1dSJustin T. Gibbs 	struct cam_path		*path;	/* Compiled path to device */
1218b8a9b1dSJustin T. Gibbs 	void			*softc;
1222b83592fSScott Long 	struct cam_sim		*sim;
1238b8a9b1dSJustin T. Gibbs 	u_int32_t		 unit_number;
1248b8a9b1dSJustin T. Gibbs 	cam_periph_type		 type;
1258b8a9b1dSJustin T. Gibbs 	u_int32_t		 flags;
1268b8a9b1dSJustin T. Gibbs #define CAM_PERIPH_RUNNING		0x01
1278b8a9b1dSJustin T. Gibbs #define CAM_PERIPH_LOCKED		0x02
1288b8a9b1dSJustin T. Gibbs #define CAM_PERIPH_LOCK_WANTED		0x04
1298b8a9b1dSJustin T. Gibbs #define CAM_PERIPH_INVALID		0x08
1308b8a9b1dSJustin T. Gibbs #define CAM_PERIPH_NEW_DEV_FOUND	0x10
13160a899a0SKenneth D. Merry #define CAM_PERIPH_RECOVERY_INPROG	0x20
132227d67aaSAlexander Motin #define CAM_PERIPH_RUN_TASK		0x40
1338900f4b8SKenneth D. Merry #define CAM_PERIPH_FREE			0x80
1348d36a71bSAlexander Motin #define CAM_PERIPH_ANNOUNCED		0x100
135227d67aaSAlexander Motin 	uint32_t		 scheduled_priority;
136227d67aaSAlexander Motin 	uint32_t		 immediate_priority;
137227d67aaSAlexander Motin 	int			 periph_allocating;
138227d67aaSAlexander Motin 	int			 periph_allocated;
1398b8a9b1dSJustin T. Gibbs 	u_int32_t		 refcount;
140e3975643SJake Burkholder 	SLIST_HEAD(, ccb_hdr)	 ccb_list;	/* For "immediate" requests */
141e3975643SJake Burkholder 	SLIST_ENTRY(cam_periph)  periph_links;
142e3975643SJake Burkholder 	TAILQ_ENTRY(cam_periph)  unit_links;
1438b8a9b1dSJustin T. Gibbs 	ac_callback_t		*deferred_callback;
1448b8a9b1dSJustin T. Gibbs 	ac_code			 deferred_ac;
145227d67aaSAlexander Motin 	struct task		 periph_run_task;
1468b8a9b1dSJustin T. Gibbs };
1478b8a9b1dSJustin T. Gibbs 
1488b8a9b1dSJustin T. Gibbs #define CAM_PERIPH_MAXMAPS	2
1498b8a9b1dSJustin T. Gibbs 
1508b8a9b1dSJustin T. Gibbs struct cam_periph_map_info {
1518b8a9b1dSJustin T. Gibbs 	int		num_bufs_used;
1528b8a9b1dSJustin T. Gibbs 	struct buf	*bp[CAM_PERIPH_MAXMAPS];
1538b8a9b1dSJustin T. Gibbs };
1548b8a9b1dSJustin T. Gibbs 
155ee9c90c7SKenneth D. Merry cam_status cam_periph_alloc(periph_ctor_t *periph_ctor,
156ee9c90c7SKenneth D. Merry 			    periph_oninv_t *periph_oninvalidate,
157ee9c90c7SKenneth D. Merry 			    periph_dtor_t *periph_dtor,
158ee9c90c7SKenneth D. Merry 			    periph_start_t *periph_start,
1590cdabce0SNick Hibma 			    char *name, cam_periph_type type, struct cam_path *,
1600cdabce0SNick Hibma 			    ac_callback_t *, ac_code, void *arg);
1618b8a9b1dSJustin T. Gibbs struct cam_periph *cam_periph_find(struct cam_path *path, char *name);
1623501942bSJustin T. Gibbs int		cam_periph_list(struct cam_path *, struct sbuf *);
16399e7a4adSScott Long int		cam_periph_acquire(struct cam_periph *periph);
164c33e4029SAlexander Motin void		cam_periph_doacquire(struct cam_periph *periph);
1658b8a9b1dSJustin T. Gibbs void		cam_periph_release(struct cam_periph *periph);
16624ebf566SEdward Tomasz Napierala void		cam_periph_release_locked(struct cam_periph *periph);
1678900f4b8SKenneth D. Merry void		cam_periph_release_locked_buses(struct cam_periph *periph);
1682b83592fSScott Long int		cam_periph_hold(struct cam_periph *periph, int priority);
1692b83592fSScott Long void		cam_periph_unhold(struct cam_periph *periph);
1708b8a9b1dSJustin T. Gibbs void		cam_periph_invalidate(struct cam_periph *periph);
1718b8a9b1dSJustin T. Gibbs int		cam_periph_mapmem(union ccb *ccb,
172de239312SAlexander Motin 				  struct cam_periph_map_info *mapinfo,
173de239312SAlexander Motin 				  u_int maxmap);
1748b8a9b1dSJustin T. Gibbs void		cam_periph_unmapmem(union ccb *ccb,
1758b8a9b1dSJustin T. Gibbs 				    struct cam_periph_map_info *mapinfo);
1768b8a9b1dSJustin T. Gibbs union ccb	*cam_periph_getccb(struct cam_periph *periph,
1778b8a9b1dSJustin T. Gibbs 				   u_int32_t priority);
1788b8a9b1dSJustin T. Gibbs int		cam_periph_runccb(union ccb *ccb,
1798b8a9b1dSJustin T. Gibbs 				  int (*error_routine)(union ccb *ccb,
1808b8a9b1dSJustin T. Gibbs 						       cam_flags camflags,
1818b8a9b1dSJustin T. Gibbs 						       u_int32_t sense_flags),
1828b8a9b1dSJustin T. Gibbs 				  cam_flags camflags, u_int32_t sense_flags,
1838b8a9b1dSJustin T. Gibbs 				  struct devstat *ds);
184571e8e20SScott Long int		cam_periph_ioctl(struct cam_periph *periph, u_long cmd,
1858b8a9b1dSJustin T. Gibbs 				 caddr_t addr,
1868b8a9b1dSJustin T. Gibbs 				 int (*error_routine)(union ccb *ccb,
1878b8a9b1dSJustin T. Gibbs 						      cam_flags camflags,
1888b8a9b1dSJustin T. Gibbs 						      u_int32_t sense_flags));
18987cfaf0eSJustin T. Gibbs void		cam_freeze_devq(struct cam_path *path);
1908b8a9b1dSJustin T. Gibbs u_int32_t	cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
19183c5d981SAlexander Motin 				 u_int32_t opening_reduction, u_int32_t arg,
1928b8a9b1dSJustin T. Gibbs 				 int getcount_only);
19387cfaf0eSJustin T. Gibbs void		cam_periph_async(struct cam_periph *periph, u_int32_t code,
19487cfaf0eSJustin T. Gibbs 		 		 struct cam_path *path, void *arg);
19587cfaf0eSJustin T. Gibbs void		cam_periph_bus_settle(struct cam_periph *periph,
19687cfaf0eSJustin T. Gibbs 				      u_int bus_settle_ms);
19787cfaf0eSJustin T. Gibbs void		cam_periph_freeze_after_event(struct cam_periph *periph,
19887cfaf0eSJustin T. Gibbs 					      struct timeval* event_time,
19987cfaf0eSJustin T. Gibbs 					      u_int duration_ms);
2008b8a9b1dSJustin T. Gibbs int		cam_periph_error(union ccb *ccb, cam_flags camflags,
201553484aeSWarner Losh 				 u_int32_t sense_flags);
202*d38677d2SWarner Losh int		cam_periph_invalidate_sysctl(SYSCTL_HANDLER_ARGS);
2038b8a9b1dSJustin T. Gibbs 
204227d67aaSAlexander Motin static __inline struct mtx *
205227d67aaSAlexander Motin cam_periph_mtx(struct cam_periph *periph)
20611e4faceSScott Long {
207329e7a8bSScott Long 	if (periph != NULL)
208227d67aaSAlexander Motin 		return (xpt_path_mtx(periph->path));
209329e7a8bSScott Long 	else
210329e7a8bSScott Long 		return (NULL);
21111e4faceSScott Long }
21211e4faceSScott Long 
213227d67aaSAlexander Motin #define cam_periph_owned(periph)					\
214227d67aaSAlexander Motin 	mtx_owned(xpt_path_mtx((periph)->path))
21511e4faceSScott Long 
216227d67aaSAlexander Motin #define cam_periph_lock(periph)						\
217227d67aaSAlexander Motin 	mtx_lock(xpt_path_mtx((periph)->path))
21821cc8587SAlexander Motin 
219227d67aaSAlexander Motin #define cam_periph_unlock(periph)					\
220227d67aaSAlexander Motin 	mtx_unlock(xpt_path_mtx((periph)->path))
221227d67aaSAlexander Motin 
222227d67aaSAlexander Motin #define cam_periph_assert(periph, what)					\
223227d67aaSAlexander Motin 	mtx_assert(xpt_path_mtx((periph)->path), (what))
224227d67aaSAlexander Motin 
225227d67aaSAlexander Motin #define cam_periph_sleep(periph, chan, priority, wmesg, timo)		\
226227d67aaSAlexander Motin 	xpt_path_sleep((periph)->path, (chan), (priority), (wmesg), (timo))
2273501942bSJustin T. Gibbs 
228f371c9e2SAlexander Motin static inline struct cam_periph *
229f371c9e2SAlexander Motin cam_periph_acquire_first(struct periph_driver *driver)
230f371c9e2SAlexander Motin {
231f371c9e2SAlexander Motin 	struct cam_periph *periph;
232f371c9e2SAlexander Motin 
233f371c9e2SAlexander Motin 	xpt_lock_buses();
234f371c9e2SAlexander Motin 	periph = TAILQ_FIRST(&driver->units);
235f371c9e2SAlexander Motin 	while (periph != NULL && (periph->flags & CAM_PERIPH_INVALID) != 0)
236f371c9e2SAlexander Motin 		periph = TAILQ_NEXT(periph, unit_links);
237f371c9e2SAlexander Motin 	if (periph != NULL)
238f371c9e2SAlexander Motin 		periph->refcount++;
239f371c9e2SAlexander Motin 	xpt_unlock_buses();
240f371c9e2SAlexander Motin 	return (periph);
241f371c9e2SAlexander Motin }
242f371c9e2SAlexander Motin 
243f371c9e2SAlexander Motin static inline struct cam_periph *
244f371c9e2SAlexander Motin cam_periph_acquire_next(struct cam_periph *pperiph)
245f371c9e2SAlexander Motin {
246f371c9e2SAlexander Motin 	struct cam_periph *periph = pperiph;
247f371c9e2SAlexander Motin 
248227d67aaSAlexander Motin 	cam_periph_assert(pperiph, MA_NOTOWNED);
249f371c9e2SAlexander Motin 	xpt_lock_buses();
250f371c9e2SAlexander Motin 	do {
251f371c9e2SAlexander Motin 		periph = TAILQ_NEXT(periph, unit_links);
252f371c9e2SAlexander Motin 	} while (periph != NULL && (periph->flags & CAM_PERIPH_INVALID) != 0);
253f371c9e2SAlexander Motin 	if (periph != NULL)
254f371c9e2SAlexander Motin 		periph->refcount++;
255f371c9e2SAlexander Motin 	xpt_unlock_buses();
256f371c9e2SAlexander Motin 	cam_periph_release(pperiph);
257f371c9e2SAlexander Motin 	return (periph);
258f371c9e2SAlexander Motin }
259f371c9e2SAlexander Motin 
260f371c9e2SAlexander Motin #define CAM_PERIPH_FOREACH(periph, driver)				\
261f371c9e2SAlexander Motin 	for ((periph) = cam_periph_acquire_first(driver);		\
262f371c9e2SAlexander Motin 	    (periph) != NULL;						\
263f371c9e2SAlexander Motin 	    (periph) = cam_periph_acquire_next(periph))
264f371c9e2SAlexander Motin 
26515747cacSScott Long #define CAM_PERIPH_PRINT(p, msg, args...)				\
26615747cacSScott Long     printf("%s%d:" msg, (periph)->periph_name, (periph)->unit_number, ##args)
26715747cacSScott Long 
268c4473420SPeter Wemm #endif /* _KERNEL */
2698b8a9b1dSJustin T. Gibbs #endif /* _CAM_CAM_PERIPH_H */
270