1/*-
2 * Data structures and definitions for CAM peripheral ("type") drivers.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 *
6 * Copyright (c) 1997, 1998 Justin T. Gibbs.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions, and the following disclaimer,
14 *    without modification, immediately at the beginning of the file.
15 * 2. The name of the author may not be used to endorse or promote products
16 *    derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * $FreeBSD$
31 */
32
33#ifndef _CAM_CAM_PERIPH_H
34#define _CAM_CAM_PERIPH_H 1
35
36#include <sys/queue.h>
37#include <cam/cam_sim.h>
38
39#ifdef _KERNEL
40#include <sys/lock.h>
41#include <sys/mutex.h>
42#include <sys/sysctl.h>
43#include <sys/taskqueue.h>
44
45#include <cam/cam_xpt.h>
46
47struct devstat;
48
49extern struct cam_periph *xpt_periph;
50
51extern struct periph_driver **periph_drivers;
52void periphdriver_register(void *);
53int periphdriver_unregister(void *);
54void periphdriver_init(int level);
55
56#include <sys/module.h>
57#define PERIPHDRIVER_DECLARE(name, driver) \
58	static int name ## _modevent(module_t mod, int type, void *data) \
59	{ \
60		switch (type) { \
61		case MOD_LOAD: \
62			periphdriver_register(data); \
63			break; \
64		case MOD_UNLOAD: \
65			return (periphdriver_unregister(data)); \
66		default: \
67			return EOPNOTSUPP; \
68		} \
69		return 0; \
70	} \
71	static moduledata_t name ## _mod = { \
72		#name, \
73		name ## _modevent, \
74		(void *)&driver \
75	}; \
76	DECLARE_MODULE(name, name ## _mod, SI_SUB_DRIVERS, SI_ORDER_ANY); \
77	MODULE_DEPEND(name, cam, 1, 1, 1)
78
79/*
80 * Callback informing the peripheral driver it can perform it's
81 * initialization since the XPT is now fully initialized.
82 */
83typedef void (periph_init_t)(void);
84
85/*
86 * Callback requesting the peripheral driver to remove its instances
87 * and shutdown, if possible.
88 */
89typedef int (periph_deinit_t)(void);
90
91struct periph_driver {
92	periph_init_t		*init;
93	char			*driver_name;
94	TAILQ_HEAD(,cam_periph)	 units;
95	u_int			 generation;
96	u_int			 flags;
97#define CAM_PERIPH_DRV_EARLY		0x01
98	periph_deinit_t		*deinit;
99};
100
101typedef enum {
102	CAM_PERIPH_BIO
103} cam_periph_type;
104
105/* Generically useful offsets into the peripheral private area */
106#define ppriv_ptr0 periph_priv.entries[0].ptr
107#define ppriv_ptr1 periph_priv.entries[1].ptr
108#define ppriv_field0 periph_priv.entries[0].field
109#define ppriv_field1 periph_priv.entries[1].field
110
111typedef void		periph_start_t (struct cam_periph *periph,
112					union ccb *start_ccb);
113typedef cam_status	periph_ctor_t (struct cam_periph *periph,
114				       void *arg);
115typedef void		periph_oninv_t (struct cam_periph *periph);
116typedef void		periph_dtor_t (struct cam_periph *periph);
117struct cam_periph {
118	periph_start_t		*periph_start;
119	periph_oninv_t		*periph_oninval;
120	periph_dtor_t		*periph_dtor;
121	char			*periph_name;
122	struct cam_path		*path;	/* Compiled path to device */
123	void			*softc;
124	struct cam_sim		*sim;
125	u_int32_t		 unit_number;
126	cam_periph_type		 type;
127	u_int32_t		 flags;
128#define CAM_PERIPH_RUNNING		0x01
129#define CAM_PERIPH_LOCKED		0x02
130#define CAM_PERIPH_LOCK_WANTED		0x04
131#define CAM_PERIPH_INVALID		0x08
132#define CAM_PERIPH_NEW_DEV_FOUND	0x10
133#define CAM_PERIPH_RECOVERY_INPROG	0x20
134#define CAM_PERIPH_RUN_TASK		0x40
135#define CAM_PERIPH_FREE			0x80
136#define CAM_PERIPH_ANNOUNCED		0x100
137#define CAM_PERIPH_RECOVERY_WAIT	0x200
138#define CAM_PERIPH_RECOVERY_WAIT_FAILED	0x400
139	uint32_t		 scheduled_priority;
140	uint32_t		 immediate_priority;
141	int			 periph_allocating;
142	int			 periph_allocated;
143	u_int32_t		 refcount;
144	SLIST_HEAD(, ccb_hdr)	 ccb_list;	/* For "immediate" requests */
145	SLIST_ENTRY(cam_periph)  periph_links;
146	TAILQ_ENTRY(cam_periph)  unit_links;
147	ac_callback_t		*deferred_callback;
148	ac_code			 deferred_ac;
149	struct task		 periph_run_task;
150};
151
152#define CAM_PERIPH_MAXMAPS	2
153
154struct cam_periph_map_info {
155	int		num_bufs_used;
156	void		*orig[CAM_PERIPH_MAXMAPS];
157	struct buf	*bp[CAM_PERIPH_MAXMAPS];
158};
159
160cam_status cam_periph_alloc(periph_ctor_t *periph_ctor,
161			    periph_oninv_t *periph_oninvalidate,
162			    periph_dtor_t *periph_dtor,
163			    periph_start_t *periph_start,
164			    char *name, cam_periph_type type, struct cam_path *,
165			    ac_callback_t *, ac_code, void *arg);
166struct cam_periph *cam_periph_find(struct cam_path *path, char *name);
167int		cam_periph_list(struct cam_path *, struct sbuf *);
168int		cam_periph_acquire(struct cam_periph *periph);
169void		cam_periph_doacquire(struct cam_periph *periph);
170void		cam_periph_release(struct cam_periph *periph);
171void		cam_periph_release_locked(struct cam_periph *periph);
172void		cam_periph_release_locked_buses(struct cam_periph *periph);
173int		cam_periph_hold(struct cam_periph *periph, int priority);
174void		cam_periph_unhold(struct cam_periph *periph);
175void		cam_periph_invalidate(struct cam_periph *periph);
176int		cam_periph_mapmem(union ccb *ccb,
177				  struct cam_periph_map_info *mapinfo,
178				  u_int maxmap);
179void		cam_periph_unmapmem(union ccb *ccb,
180				    struct cam_periph_map_info *mapinfo);
181union ccb	*cam_periph_getccb(struct cam_periph *periph,
182				   u_int32_t priority);
183int		cam_periph_runccb(union ccb *ccb,
184				  int (*error_routine)(union ccb *ccb,
185						       cam_flags camflags,
186						       u_int32_t sense_flags),
187				  cam_flags camflags, u_int32_t sense_flags,
188				  struct devstat *ds);
189int		cam_periph_ioctl(struct cam_periph *periph, u_long cmd,
190				 caddr_t addr,
191				 int (*error_routine)(union ccb *ccb,
192						      cam_flags camflags,
193						      u_int32_t sense_flags));
194void		cam_freeze_devq(struct cam_path *path);
195u_int32_t	cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
196				 u_int32_t opening_reduction, u_int32_t arg,
197				 int getcount_only);
198void		cam_periph_async(struct cam_periph *periph, u_int32_t code,
199		 		 struct cam_path *path, void *arg);
200void		cam_periph_bus_settle(struct cam_periph *periph,
201				      u_int bus_settle_ms);
202void		cam_periph_freeze_after_event(struct cam_periph *periph,
203					      struct timeval* event_time,
204					      u_int duration_ms);
205int		cam_periph_error(union ccb *ccb, cam_flags camflags,
206				 u_int32_t sense_flags);
207int		cam_periph_invalidate_sysctl(SYSCTL_HANDLER_ARGS);
208
209static __inline struct mtx *
210cam_periph_mtx(struct cam_periph *periph)
211{
212	if (periph != NULL)
213		return (xpt_path_mtx(periph->path));
214	else
215		return (NULL);
216}
217
218#define cam_periph_owned(periph)					\
219	mtx_owned(xpt_path_mtx((periph)->path))
220
221#define cam_periph_lock(periph)						\
222	mtx_lock(xpt_path_mtx((periph)->path))
223
224#define cam_periph_unlock(periph)					\
225	mtx_unlock(xpt_path_mtx((periph)->path))
226
227#define cam_periph_assert(periph, what)					\
228	mtx_assert(xpt_path_mtx((periph)->path), (what))
229
230#define cam_periph_sleep(periph, chan, priority, wmesg, timo)		\
231	xpt_path_sleep((periph)->path, (chan), (priority), (wmesg), (timo))
232
233static inline struct cam_periph *
234cam_periph_acquire_first(struct periph_driver *driver)
235{
236	struct cam_periph *periph;
237
238	xpt_lock_buses();
239	periph = TAILQ_FIRST(&driver->units);
240	while (periph != NULL && (periph->flags & CAM_PERIPH_INVALID) != 0)
241		periph = TAILQ_NEXT(periph, unit_links);
242	if (periph != NULL)
243		periph->refcount++;
244	xpt_unlock_buses();
245	return (periph);
246}
247
248static inline struct cam_periph *
249cam_periph_acquire_next(struct cam_periph *pperiph)
250{
251	struct cam_periph *periph = pperiph;
252
253	cam_periph_assert(pperiph, MA_NOTOWNED);
254	xpt_lock_buses();
255	do {
256		periph = TAILQ_NEXT(periph, unit_links);
257	} while (periph != NULL && (periph->flags & CAM_PERIPH_INVALID) != 0);
258	if (periph != NULL)
259		periph->refcount++;
260	xpt_unlock_buses();
261	cam_periph_release(pperiph);
262	return (periph);
263}
264
265#define CAM_PERIPH_FOREACH(periph, driver)				\
266	for ((periph) = cam_periph_acquire_first(driver);		\
267	    (periph) != NULL;						\
268	    (periph) = cam_periph_acquire_next(periph))
269
270#define CAM_PERIPH_PRINT(p, msg, args...)				\
271    printf("%s%d:" msg, (periph)->periph_name, (periph)->unit_number, ##args)
272
273#endif /* _KERNEL */
274#endif /* _CAM_CAM_PERIPH_H */
275