summaryrefslogtreecommitdiffstats
path: root/sys/cam/cam_periph.h
blob: ebcf1a42bdafa085ac291ed639dca1fdfef05f82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*-
 * Data structures and definitions for CAM peripheral ("type") drivers.
 *
 * Copyright (c) 1997, 1998 Justin T. Gibbs.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification, immediately at the beginning of the file.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#ifndef _CAM_CAM_PERIPH_H
#define _CAM_CAM_PERIPH_H 1

#include <sys/queue.h>
#include <cam/cam_sim.h>

#ifdef _KERNEL
#include <sys/taskqueue.h>

#include <cam/cam_xpt.h>

struct devstat;

extern struct cam_periph *xpt_periph;

extern struct periph_driver **periph_drivers;
void periphdriver_register(void *);
void periphdriver_init(int level);

#include <sys/module.h>
#define PERIPHDRIVER_DECLARE(name, driver) \
	static int name ## _modevent(module_t mod, int type, void *data) \
	{ \
		switch (type) { \
		case MOD_LOAD: \
			periphdriver_register(data); \
			break; \
		case MOD_UNLOAD: \
			printf(#name " module unload - not possible for this module type\n"); \
			return EINVAL; \
		default: \
			return EOPNOTSUPP; \
		} \
		return 0; \
	} \
	static moduledata_t name ## _mod = { \
		#name, \
		name ## _modevent, \
		(void *)&driver \
	}; \
	DECLARE_MODULE(name, name ## _mod, SI_SUB_DRIVERS, SI_ORDER_ANY); \
	MODULE_DEPEND(name, cam, 1, 1, 1)

typedef void (periph_init_t)(void); /*
				     * Callback informing the peripheral driver
				     * it can perform it's initialization since
				     * the XPT is now fully initialized.
				     */
typedef periph_init_t *periph_init_func_t;

struct periph_driver {
	periph_init_func_t	 init;
	char			 *driver_name;
	TAILQ_HEAD(,cam_periph)	 units;
	u_int			 generation;
	u_int			 flags;
#define CAM_PERIPH_DRV_EARLY		0x01
};

typedef enum {
	CAM_PERIPH_BIO
} cam_periph_type;

/* Generically useful offsets into the peripheral private area */
#define ppriv_ptr0 periph_priv.entries[0].ptr
#define ppriv_ptr1 periph_priv.entries[1].ptr
#define ppriv_field0 periph_priv.entries[0].field
#define ppriv_field1 periph_priv.entries[1].field

typedef void		periph_start_t (struct cam_periph *periph,
					union ccb *start_ccb);
typedef cam_status	periph_ctor_t (struct cam_periph *periph,
				       void *arg);
typedef void		periph_oninv_t (struct cam_periph *periph);
typedef void		periph_dtor_t (struct cam_periph *periph);
struct cam_periph {
	periph_start_t		*periph_start;
	periph_oninv_t		*periph_oninval;
	periph_dtor_t		*periph_dtor;
	char			*periph_name;
	struct cam_path		*path;	/* Compiled path to device */
	void			*softc;
	struct cam_sim		*sim;
	u_int32_t		 unit_number;
	cam_periph_type		 type;
	u_int32_t		 flags;
#define CAM_PERIPH_RUNNING		0x01
#define CAM_PERIPH_LOCKED		0x02
#define CAM_PERIPH_LOCK_WANTED		0x04
#define CAM_PERIPH_INVALID		0x08
#define CAM_PERIPH_NEW_DEV_FOUND	0x10
#define CAM_PERIPH_RECOVERY_INPROG	0x20
#define CAM_PERIPH_RUN_TASK		0x40
#define CAM_PERIPH_FREE			0x80
#define CAM_PERIPH_ANNOUNCED		0x100
	uint32_t		 scheduled_priority;
	uint32_t		 immediate_priority;
	int			 periph_allocating;
	int			 periph_allocated;
	u_int32_t		 refcount;
	SLIST_HEAD(, ccb_hdr)	 ccb_list;	/* For "immediate" requests */
	SLIST_ENTRY(cam_periph)  periph_links;
	TAILQ_ENTRY(cam_periph)  unit_links;
	ac_callback_t		*deferred_callback; 
	ac_code			 deferred_ac;
	struct task		 periph_run_task;
};

#define CAM_PERIPH_MAXMAPS	2

struct cam_periph_map_info {
	int		num_bufs_used;
	struct buf	*bp[CAM_PERIPH_MAXMAPS];
};

cam_status cam_periph_alloc(periph_ctor_t *periph_ctor,
			    periph_oninv_t *periph_oninvalidate,
			    periph_dtor_t *periph_dtor,
			    periph_start_t *periph_start,
			    char *name, cam_periph_type type, struct cam_path *,
			    ac_callback_t *, ac_code, void *arg);
struct cam_periph *cam_periph_find(struct cam_path *path, char *name);
int		cam_periph_list(struct cam_path *, struct sbuf *);
cam_status	cam_periph_acquire(struct cam_periph *periph);
void		cam_periph_doacquire(struct cam_periph *periph);
void		cam_periph_release(struct cam_periph *periph);
void		cam_periph_release_locked(struct cam_periph *periph);
void		cam_periph_release_locked_buses(struct cam_periph *periph);
int		cam_periph_hold(struct cam_periph *periph, int priority);
void		cam_periph_unhold(struct cam_periph *periph);
void		cam_periph_invalidate(struct cam_periph *periph);
int		cam_periph_mapmem(union ccb *ccb,
				  struct cam_periph_map_info *mapinfo);
void		cam_periph_unmapmem(union ccb *ccb,
				    struct cam_periph_map_info *mapinfo);
union ccb	*cam_periph_getccb(struct cam_periph *periph,
				   u_int32_t priority);
void		cam_periph_ccbwait(union ccb *ccb);
int		cam_periph_runccb(union ccb *ccb,
				  int (*error_routine)(union ccb *ccb,
						       cam_flags camflags,
						       u_int32_t sense_flags),
				  cam_flags camflags, u_int32_t sense_flags,
				  struct devstat *ds);
int		cam_periph_ioctl(struct cam_periph *periph, u_long cmd, 
				 caddr_t addr,
				 int (*error_routine)(union ccb *ccb,
						      cam_flags camflags,
						      u_int32_t sense_flags));
void		cam_freeze_devq(struct cam_path *path);
u_int32_t	cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
				 u_int32_t opening_reduction, u_int32_t arg,
				 int getcount_only);
void		cam_periph_async(struct cam_periph *periph, u_int32_t code,
		 		 struct cam_path *path, void *arg);
void		cam_periph_bus_settle(struct cam_periph *periph,
				      u_int bus_settle_ms);
void		cam_periph_freeze_after_event(struct cam_periph *periph,
					      struct timeval* event_time,
					      u_int duration_ms);
int		cam_periph_error(union ccb *ccb, cam_flags camflags,
				 u_int32_t sense_flags, union ccb *save_ccb);

static __inline struct mtx *
cam_periph_mtx(struct cam_periph *periph)
{
	return (xpt_path_mtx(periph->path));
}

#define cam_periph_owned(periph)					\
	mtx_owned(xpt_path_mtx((periph)->path))

#define cam_periph_lock(periph)						\
	mtx_lock(xpt_path_mtx((periph)->path))

#define cam_periph_unlock(periph)					\
	mtx_unlock(xpt_path_mtx((periph)->path))

#define cam_periph_assert(periph, what)					\
	mtx_assert(xpt_path_mtx((periph)->path), (what))

#define cam_periph_sleep(periph, chan, priority, wmesg, timo)		\
	xpt_path_sleep((periph)->path, (chan), (priority), (wmesg), (timo))

static inline struct cam_periph *
cam_periph_acquire_first(struct periph_driver *driver)
{
	struct cam_periph *periph;

	xpt_lock_buses();
	periph = TAILQ_FIRST(&driver->units);
	while (periph != NULL && (periph->flags & CAM_PERIPH_INVALID) != 0)
		periph = TAILQ_NEXT(periph, unit_links);
	if (periph != NULL)
		periph->refcount++;
	xpt_unlock_buses();
	return (periph);
}

static inline struct cam_periph *
cam_periph_acquire_next(struct cam_periph *pperiph)
{
	struct cam_periph *periph = pperiph;

	cam_periph_assert(pperiph, MA_NOTOWNED);
	xpt_lock_buses();
	do {
		periph = TAILQ_NEXT(periph, unit_links);
	} while (periph != NULL && (periph->flags & CAM_PERIPH_INVALID) != 0);
	if (periph != NULL)
		periph->refcount++;
	xpt_unlock_buses();
	cam_periph_release(pperiph);
	return (periph);
}

#define CAM_PERIPH_FOREACH(periph, driver)				\
	for ((periph) = cam_periph_acquire_first(driver);		\
	    (periph) != NULL;						\
	    (periph) = cam_periph_acquire_next(periph))

#endif /* _KERNEL */
#endif /* _CAM_CAM_PERIPH_H */
OpenPOWER on IntegriCloud