1139743Simp/*-
239212Sgibbs * Data structures and definitions for CAM peripheral ("type") drivers.
339212Sgibbs *
439212Sgibbs * Copyright (c) 1997, 1998 Justin T. Gibbs.
539212Sgibbs * All rights reserved.
639212Sgibbs *
739212Sgibbs * Redistribution and use in source and binary forms, with or without
839212Sgibbs * modification, are permitted provided that the following conditions
939212Sgibbs * are met:
1039212Sgibbs * 1. Redistributions of source code must retain the above copyright
1139212Sgibbs *    notice, this list of conditions, and the following disclaimer,
1239212Sgibbs *    without modification, immediately at the beginning of the file.
1339212Sgibbs * 2. The name of the author may not be used to endorse or promote products
1439212Sgibbs *    derived from this software without specific prior written permission.
1539212Sgibbs *
1639212Sgibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1739212Sgibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1839212Sgibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1939212Sgibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
2039212Sgibbs * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2139212Sgibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2239212Sgibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2339212Sgibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2439212Sgibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2539212Sgibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2639212Sgibbs * SUCH DAMAGE.
2739212Sgibbs *
2850477Speter * $FreeBSD$
2939212Sgibbs */
3039212Sgibbs
3139212Sgibbs#ifndef _CAM_CAM_PERIPH_H
3239212Sgibbs#define _CAM_CAM_PERIPH_H 1
3339212Sgibbs
3439212Sgibbs#include <sys/queue.h>
35168876Sscottl#include <cam/cam_sim.h>
3639212Sgibbs
3755206Speter#ifdef _KERNEL
38260387Sscottl#include <sys/taskqueue.h>
3939212Sgibbs
40248874Smarius#include <cam/cam_xpt.h>
41248874Smarius
42111979Sphkstruct devstat;
43111979Sphk
4460168Sn_hibmaextern struct cam_periph *xpt_periph;
4558972Sn_hibma
4672119Speterextern struct periph_driver **periph_drivers;
4772119Spetervoid periphdriver_register(void *);
48203108Smavvoid periphdriver_init(int level);
4939212Sgibbs
5072119Speter#include <sys/module.h>
5172119Speter#define PERIPHDRIVER_DECLARE(name, driver) \
5272119Speter	static int name ## _modevent(module_t mod, int type, void *data) \
5372119Speter	{ \
5472119Speter		switch (type) { \
5572119Speter		case MOD_LOAD: \
5672119Speter			periphdriver_register(data); \
5772119Speter			break; \
5872119Speter		case MOD_UNLOAD: \
5972119Speter			printf(#name " module unload - not possible for this module type\n"); \
6072119Speter			return EINVAL; \
61132199Sphk		default: \
62132199Sphk			return EOPNOTSUPP; \
6372119Speter		} \
6472119Speter		return 0; \
6572119Speter	} \
6672119Speter	static moduledata_t name ## _mod = { \
6772119Speter		#name, \
6872119Speter		name ## _modevent, \
6972119Speter		(void *)&driver \
7072119Speter	}; \
7172119Speter	DECLARE_MODULE(name, name ## _mod, SI_SUB_DRIVERS, SI_ORDER_ANY); \
7272119Speter	MODULE_DEPEND(name, cam, 1, 1, 1)
7372119Speter
7439212Sgibbstypedef void (periph_init_t)(void); /*
7539212Sgibbs				     * Callback informing the peripheral driver
7639212Sgibbs				     * it can perform it's initialization since
7739212Sgibbs				     * the XPT is now fully initialized.
7839212Sgibbs				     */
7939212Sgibbstypedef periph_init_t *periph_init_func_t;
8039212Sgibbs
8139212Sgibbsstruct periph_driver {
8239212Sgibbs	periph_init_func_t	 init;
8339212Sgibbs	char			 *driver_name;
8460938Sjake	TAILQ_HEAD(,cam_periph)	 units;
8539212Sgibbs	u_int			 generation;
86198708Smav	u_int			 flags;
87198708Smav#define CAM_PERIPH_DRV_EARLY		0x01
8839212Sgibbs};
8939212Sgibbs
9039212Sgibbstypedef enum {
91136132Sscottl	CAM_PERIPH_BIO
9239212Sgibbs} cam_periph_type;
9339212Sgibbs
94249585Sgabor/* Generically useful offsets into the peripheral private area */
9539212Sgibbs#define ppriv_ptr0 periph_priv.entries[0].ptr
9639212Sgibbs#define ppriv_ptr1 periph_priv.entries[1].ptr
9739212Sgibbs#define ppriv_field0 periph_priv.entries[0].field
9839212Sgibbs#define ppriv_field1 periph_priv.entries[1].field
9939212Sgibbs
10039212Sgibbstypedef void		periph_start_t (struct cam_periph *periph,
10139212Sgibbs					union ccb *start_ccb);
10239212Sgibbstypedef cam_status	periph_ctor_t (struct cam_periph *periph,
10339212Sgibbs				       void *arg);
10440603Skentypedef void		periph_oninv_t (struct cam_periph *periph);
10539212Sgibbstypedef void		periph_dtor_t (struct cam_periph *periph);
10639212Sgibbsstruct cam_periph {
10739212Sgibbs	periph_start_t		*periph_start;
10840603Sken	periph_oninv_t		*periph_oninval;
10939212Sgibbs	periph_dtor_t		*periph_dtor;
11039212Sgibbs	char			*periph_name;
11139212Sgibbs	struct cam_path		*path;	/* Compiled path to device */
11239212Sgibbs	void			*softc;
113168752Sscottl	struct cam_sim		*sim;
11439212Sgibbs	u_int32_t		 unit_number;
11539212Sgibbs	cam_periph_type		 type;
11639212Sgibbs	u_int32_t		 flags;
11739212Sgibbs#define CAM_PERIPH_RUNNING		0x01
11839212Sgibbs#define CAM_PERIPH_LOCKED		0x02
11939212Sgibbs#define CAM_PERIPH_LOCK_WANTED		0x04
12039212Sgibbs#define CAM_PERIPH_INVALID		0x08
12139212Sgibbs#define CAM_PERIPH_NEW_DEV_FOUND	0x10
12240318Sken#define CAM_PERIPH_RECOVERY_INPROG	0x20
123260387Sscottl#define CAM_PERIPH_RUN_TASK		0x40
124230000Sken#define CAM_PERIPH_FREE			0x80
125257049Smav#define CAM_PERIPH_ANNOUNCED		0x100
126260387Sscottl	uint32_t		 scheduled_priority;
127260387Sscottl	uint32_t		 immediate_priority;
128260387Sscottl	int			 periph_allocating;
129260387Sscottl	int			 periph_allocated;
13039212Sgibbs	u_int32_t		 refcount;
13160938Sjake	SLIST_HEAD(, ccb_hdr)	 ccb_list;	/* For "immediate" requests */
13260938Sjake	SLIST_ENTRY(cam_periph)  periph_links;
13360938Sjake	TAILQ_ENTRY(cam_periph)  unit_links;
13439212Sgibbs	ac_callback_t		*deferred_callback;
13539212Sgibbs	ac_code			 deferred_ac;
136260387Sscottl	struct task		 periph_run_task;
13739212Sgibbs};
13839212Sgibbs
13939212Sgibbs#define CAM_PERIPH_MAXMAPS	2
14039212Sgibbs
14139212Sgibbsstruct cam_periph_map_info {
14239212Sgibbs	int		num_bufs_used;
14339212Sgibbs	struct buf	*bp[CAM_PERIPH_MAXMAPS];
14439212Sgibbs};
14539212Sgibbs
14640603Skencam_status cam_periph_alloc(periph_ctor_t *periph_ctor,
14740603Sken			    periph_oninv_t *periph_oninvalidate,
14840603Sken			    periph_dtor_t *periph_dtor,
14940603Sken			    periph_start_t *periph_start,
15058111Sn_hibma			    char *name, cam_periph_type type, struct cam_path *,
15158111Sn_hibma			    ac_callback_t *, ac_code, void *arg);
15239212Sgibbsstruct cam_periph *cam_periph_find(struct cam_path *path, char *name);
153223081Sgibbsint		cam_periph_list(struct cam_path *, struct sbuf *);
15439212Sgibbscam_status	cam_periph_acquire(struct cam_periph *periph);
155260626Smavvoid		cam_periph_doacquire(struct cam_periph *periph);
15639212Sgibbsvoid		cam_periph_release(struct cam_periph *periph);
157186319Straszvoid		cam_periph_release_locked(struct cam_periph *periph);
158230000Skenvoid		cam_periph_release_locked_buses(struct cam_periph *periph);
159168752Sscottlint		cam_periph_hold(struct cam_periph *periph, int priority);
160168752Sscottlvoid		cam_periph_unhold(struct cam_periph *periph);
16139212Sgibbsvoid		cam_periph_invalidate(struct cam_periph *periph);
16239212Sgibbsint		cam_periph_mapmem(union ccb *ccb,
163288817Smav				  struct cam_periph_map_info *mapinfo,
164288817Smav				  u_int maxmap);
16539212Sgibbsvoid		cam_periph_unmapmem(union ccb *ccb,
16639212Sgibbs				    struct cam_periph_map_info *mapinfo);
16739212Sgibbsunion ccb	*cam_periph_getccb(struct cam_periph *periph,
16839212Sgibbs				   u_int32_t priority);
16939212Sgibbsvoid		cam_periph_ccbwait(union ccb *ccb);
17039212Sgibbsint		cam_periph_runccb(union ccb *ccb,
17139212Sgibbs				  int (*error_routine)(union ccb *ccb,
17239212Sgibbs						       cam_flags camflags,
17339212Sgibbs						       u_int32_t sense_flags),
17439212Sgibbs				  cam_flags camflags, u_int32_t sense_flags,
17539212Sgibbs				  struct devstat *ds);
176194627Sscottlint		cam_periph_ioctl(struct cam_periph *periph, u_long cmd,
17739212Sgibbs				 caddr_t addr,
17839212Sgibbs				 int (*error_routine)(union ccb *ccb,
17939212Sgibbs						      cam_flags camflags,
18039212Sgibbs						      u_int32_t sense_flags));
18147412Sgibbsvoid		cam_freeze_devq(struct cam_path *path);
18239212Sgibbsu_int32_t	cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
183203108Smav				 u_int32_t opening_reduction, u_int32_t arg,
18439212Sgibbs				 int getcount_only);
18547412Sgibbsvoid		cam_periph_async(struct cam_periph *periph, u_int32_t code,
18647412Sgibbs		 		 struct cam_path *path, void *arg);
18747412Sgibbsvoid		cam_periph_bus_settle(struct cam_periph *periph,
18847412Sgibbs				      u_int bus_settle_ms);
18947412Sgibbsvoid		cam_periph_freeze_after_event(struct cam_periph *periph,
19047412Sgibbs					      struct timeval* event_time,
19147412Sgibbs					      u_int duration_ms);
19239212Sgibbsint		cam_periph_error(union ccb *ccb, cam_flags camflags,
19339212Sgibbs				 u_int32_t sense_flags, union ccb *save_ccb);
19439212Sgibbs
195260387Sscottlstatic __inline struct mtx *
196260387Sscottlcam_periph_mtx(struct cam_periph *periph)
197168876Sscottl{
198260387Sscottl	return (xpt_path_mtx(periph->path));
199168876Sscottl}
200168876Sscottl
201260387Sscottl#define cam_periph_owned(periph)					\
202260387Sscottl	mtx_owned(xpt_path_mtx((periph)->path))
203168876Sscottl
204260387Sscottl#define cam_periph_lock(periph)						\
205260387Sscottl	mtx_lock(xpt_path_mtx((periph)->path))
206200180Smav
207260387Sscottl#define cam_periph_unlock(periph)					\
208260387Sscottl	mtx_unlock(xpt_path_mtx((periph)->path))
209223081Sgibbs
210260387Sscottl#define cam_periph_assert(periph, what)					\
211260387Sscottl	mtx_assert(xpt_path_mtx((periph)->path), (what))
212260387Sscottl
213260387Sscottl#define cam_periph_sleep(periph, chan, priority, wmesg, timo)		\
214260387Sscottl	xpt_path_sleep((periph)->path, (chan), (priority), (wmesg), (timo))
215260387Sscottl
216248868Smavstatic inline struct cam_periph *
217248868Smavcam_periph_acquire_first(struct periph_driver *driver)
218248868Smav{
219248868Smav	struct cam_periph *periph;
220248868Smav
221248868Smav	xpt_lock_buses();
222248868Smav	periph = TAILQ_FIRST(&driver->units);
223248868Smav	while (periph != NULL && (periph->flags & CAM_PERIPH_INVALID) != 0)
224248868Smav		periph = TAILQ_NEXT(periph, unit_links);
225248868Smav	if (periph != NULL)
226248868Smav		periph->refcount++;
227248868Smav	xpt_unlock_buses();
228248868Smav	return (periph);
229248868Smav}
230248868Smav
231248868Smavstatic inline struct cam_periph *
232248868Smavcam_periph_acquire_next(struct cam_periph *pperiph)
233248868Smav{
234248868Smav	struct cam_periph *periph = pperiph;
235248868Smav
236260387Sscottl	cam_periph_assert(pperiph, MA_NOTOWNED);
237248868Smav	xpt_lock_buses();
238248868Smav	do {
239248868Smav		periph = TAILQ_NEXT(periph, unit_links);
240248868Smav	} while (periph != NULL && (periph->flags & CAM_PERIPH_INVALID) != 0);
241248868Smav	if (periph != NULL)
242248868Smav		periph->refcount++;
243248868Smav	xpt_unlock_buses();
244248868Smav	cam_periph_release(pperiph);
245248868Smav	return (periph);
246248868Smav}
247248868Smav
248248868Smav#define CAM_PERIPH_FOREACH(periph, driver)				\
249248868Smav	for ((periph) = cam_periph_acquire_first(driver);		\
250248868Smav	    (periph) != NULL;						\
251248868Smav	    (periph) = cam_periph_acquire_next(periph))
252248868Smav
25355206Speter#endif /* _KERNEL */
25439212Sgibbs#endif /* _CAM_CAM_PERIPH_H */
255