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 3839212Sgibbs 39249610Smav#include <cam/cam_xpt.h> 40249610Smav 41111979Sphkstruct devstat; 42111979Sphk 4360168Sn_hibmaextern struct cam_periph *xpt_periph; 4458972Sn_hibma 4572119Speterextern struct periph_driver **periph_drivers; 4672119Spetervoid periphdriver_register(void *); 47203108Smavvoid periphdriver_init(int level); 4839212Sgibbs 4972119Speter#include <sys/module.h> 5072119Speter#define PERIPHDRIVER_DECLARE(name, driver) \ 5172119Speter static int name ## _modevent(module_t mod, int type, void *data) \ 5272119Speter { \ 5372119Speter switch (type) { \ 5472119Speter case MOD_LOAD: \ 5572119Speter periphdriver_register(data); \ 5672119Speter break; \ 5772119Speter case MOD_UNLOAD: \ 5872119Speter printf(#name " module unload - not possible for this module type\n"); \ 5972119Speter return EINVAL; \ 60132199Sphk default: \ 61132199Sphk return EOPNOTSUPP; \ 6272119Speter } \ 6372119Speter return 0; \ 6472119Speter } \ 6572119Speter static moduledata_t name ## _mod = { \ 6672119Speter #name, \ 6772119Speter name ## _modevent, \ 6872119Speter (void *)&driver \ 6972119Speter }; \ 7072119Speter DECLARE_MODULE(name, name ## _mod, SI_SUB_DRIVERS, SI_ORDER_ANY); \ 7172119Speter MODULE_DEPEND(name, cam, 1, 1, 1) 7272119Speter 7339212Sgibbstypedef void (periph_init_t)(void); /* 7439212Sgibbs * Callback informing the peripheral driver 7539212Sgibbs * it can perform it's initialization since 7639212Sgibbs * the XPT is now fully initialized. 7739212Sgibbs */ 7839212Sgibbstypedef periph_init_t *periph_init_func_t; 7939212Sgibbs 8039212Sgibbsstruct periph_driver { 8139212Sgibbs periph_init_func_t init; 8239212Sgibbs char *driver_name; 8360938Sjake TAILQ_HEAD(,cam_periph) units; 8439212Sgibbs u_int generation; 85198708Smav u_int flags; 86198708Smav#define CAM_PERIPH_DRV_EARLY 0x01 8739212Sgibbs}; 8839212Sgibbs 8939212Sgibbstypedef enum { 90136132Sscottl CAM_PERIPH_BIO 9139212Sgibbs} cam_periph_type; 9239212Sgibbs 9339212Sgibbs/* Generically usefull offsets into the peripheral private area */ 9439212Sgibbs#define ppriv_ptr0 periph_priv.entries[0].ptr 9539212Sgibbs#define ppriv_ptr1 periph_priv.entries[1].ptr 9639212Sgibbs#define ppriv_field0 periph_priv.entries[0].field 9739212Sgibbs#define ppriv_field1 periph_priv.entries[1].field 9839212Sgibbs 9939212Sgibbstypedef void periph_start_t (struct cam_periph *periph, 10039212Sgibbs union ccb *start_ccb); 10139212Sgibbstypedef cam_status periph_ctor_t (struct cam_periph *periph, 10239212Sgibbs void *arg); 10340603Skentypedef void periph_oninv_t (struct cam_periph *periph); 10439212Sgibbstypedef void periph_dtor_t (struct cam_periph *periph); 10539212Sgibbsstruct cam_periph { 10639212Sgibbs cam_pinfo pinfo; 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 123230849Sken#define CAM_PERIPH_FREE 0x80 124257050Smav#define CAM_PERIPH_ANNOUNCED 0x100 12539212Sgibbs u_int32_t immediate_priority; 12639212Sgibbs u_int32_t refcount; 12760938Sjake SLIST_HEAD(, ccb_hdr) ccb_list; /* For "immediate" requests */ 12860938Sjake SLIST_ENTRY(cam_periph) periph_links; 12960938Sjake TAILQ_ENTRY(cam_periph) unit_links; 13039212Sgibbs ac_callback_t *deferred_callback; 13139212Sgibbs ac_code deferred_ac; 13239212Sgibbs}; 13339212Sgibbs 13439212Sgibbs#define CAM_PERIPH_MAXMAPS 2 13539212Sgibbs 13639212Sgibbsstruct cam_periph_map_info { 13739212Sgibbs int num_bufs_used; 13839212Sgibbs struct buf *bp[CAM_PERIPH_MAXMAPS]; 13939212Sgibbs}; 14039212Sgibbs 14140603Skencam_status cam_periph_alloc(periph_ctor_t *periph_ctor, 14240603Sken periph_oninv_t *periph_oninvalidate, 14340603Sken periph_dtor_t *periph_dtor, 14440603Sken periph_start_t *periph_start, 14558111Sn_hibma char *name, cam_periph_type type, struct cam_path *, 14658111Sn_hibma ac_callback_t *, ac_code, void *arg); 14739212Sgibbsstruct cam_periph *cam_periph_find(struct cam_path *path, char *name); 148223081Sgibbsint cam_periph_list(struct cam_path *, struct sbuf *); 14939212Sgibbscam_status cam_periph_acquire(struct cam_periph *periph); 15039212Sgibbsvoid cam_periph_release(struct cam_periph *periph); 151186319Straszvoid cam_periph_release_locked(struct cam_periph *periph); 152230849Skenvoid cam_periph_release_locked_buses(struct cam_periph *periph); 153168752Sscottlint cam_periph_hold(struct cam_periph *periph, int priority); 154168752Sscottlvoid cam_periph_unhold(struct cam_periph *periph); 15539212Sgibbsvoid cam_periph_invalidate(struct cam_periph *periph); 15639212Sgibbsint cam_periph_mapmem(union ccb *ccb, 15739212Sgibbs struct cam_periph_map_info *mapinfo); 15839212Sgibbsvoid cam_periph_unmapmem(union ccb *ccb, 15939212Sgibbs struct cam_periph_map_info *mapinfo); 16039212Sgibbsunion ccb *cam_periph_getccb(struct cam_periph *periph, 16139212Sgibbs u_int32_t priority); 16239212Sgibbsvoid cam_periph_ccbwait(union ccb *ccb); 16339212Sgibbsint cam_periph_runccb(union ccb *ccb, 16439212Sgibbs int (*error_routine)(union ccb *ccb, 16539212Sgibbs cam_flags camflags, 16639212Sgibbs u_int32_t sense_flags), 16739212Sgibbs cam_flags camflags, u_int32_t sense_flags, 16839212Sgibbs struct devstat *ds); 169194627Sscottlint cam_periph_ioctl(struct cam_periph *periph, u_long cmd, 17039212Sgibbs caddr_t addr, 17139212Sgibbs int (*error_routine)(union ccb *ccb, 17239212Sgibbs cam_flags camflags, 17339212Sgibbs u_int32_t sense_flags)); 17447412Sgibbsvoid cam_freeze_devq(struct cam_path *path); 17539212Sgibbsu_int32_t cam_release_devq(struct cam_path *path, u_int32_t relsim_flags, 176203108Smav u_int32_t opening_reduction, u_int32_t arg, 17739212Sgibbs int getcount_only); 17847412Sgibbsvoid cam_periph_async(struct cam_periph *periph, u_int32_t code, 17947412Sgibbs struct cam_path *path, void *arg); 18047412Sgibbsvoid cam_periph_bus_settle(struct cam_periph *periph, 18147412Sgibbs u_int bus_settle_ms); 18247412Sgibbsvoid cam_periph_freeze_after_event(struct cam_periph *periph, 18347412Sgibbs struct timeval* event_time, 18447412Sgibbs u_int duration_ms); 18539212Sgibbsint cam_periph_error(union ccb *ccb, cam_flags camflags, 18639212Sgibbs u_int32_t sense_flags, union ccb *save_ccb); 18739212Sgibbs 188168876Sscottlstatic __inline void 189168876Sscottlcam_periph_lock(struct cam_periph *periph) 190168876Sscottl{ 191168876Sscottl mtx_lock(periph->sim->mtx); 192168876Sscottl} 193168876Sscottl 194168876Sscottlstatic __inline void 195168876Sscottlcam_periph_unlock(struct cam_periph *periph) 196168876Sscottl{ 197168876Sscottl mtx_unlock(periph->sim->mtx); 198168876Sscottl} 199168876Sscottl 200200180Smavstatic __inline int 201200180Smavcam_periph_owned(struct cam_periph *periph) 202200180Smav{ 203200180Smav return (mtx_owned(periph->sim->mtx)); 204200180Smav} 205200180Smav 206223081Sgibbsstatic __inline int 207223081Sgibbscam_periph_sleep(struct cam_periph *periph, void *chan, int priority, 208223081Sgibbs const char *wmesg, int timo) 209223081Sgibbs{ 210223081Sgibbs return (msleep(chan, periph->sim->mtx, priority, wmesg, timo)); 211223081Sgibbs} 212223081Sgibbs 213249610Smavstatic inline struct cam_periph * 214249610Smavcam_periph_acquire_first(struct periph_driver *driver) 215249610Smav{ 216249610Smav struct cam_periph *periph; 217249610Smav 218249610Smav xpt_lock_buses(); 219249610Smav periph = TAILQ_FIRST(&driver->units); 220249610Smav while (periph != NULL && (periph->flags & CAM_PERIPH_INVALID) != 0) 221249610Smav periph = TAILQ_NEXT(periph, unit_links); 222249610Smav if (periph != NULL) 223249610Smav periph->refcount++; 224249610Smav xpt_unlock_buses(); 225249610Smav return (periph); 226249610Smav} 227249610Smav 228249610Smavstatic inline struct cam_periph * 229249610Smavcam_periph_acquire_next(struct cam_periph *pperiph) 230249610Smav{ 231249610Smav struct cam_periph *periph = pperiph; 232249610Smav 233249610Smav mtx_assert(pperiph->sim->mtx, MA_NOTOWNED); 234249610Smav xpt_lock_buses(); 235249610Smav do { 236249610Smav periph = TAILQ_NEXT(periph, unit_links); 237249610Smav } while (periph != NULL && (periph->flags & CAM_PERIPH_INVALID) != 0); 238249610Smav if (periph != NULL) 239249610Smav periph->refcount++; 240249610Smav xpt_unlock_buses(); 241249610Smav cam_periph_release(pperiph); 242249610Smav return (periph); 243249610Smav} 244249610Smav 245249610Smav#define CAM_PERIPH_FOREACH(periph, driver) \ 246249610Smav for ((periph) = cam_periph_acquire_first(driver); \ 247249610Smav (periph) != NULL; \ 248249610Smav (periph) = cam_periph_acquire_next(periph)) 249249610Smav 25055206Speter#endif /* _KERNEL */ 25139212Sgibbs#endif /* _CAM_CAM_PERIPH_H */ 252