#ifndef TW_OSL_H
#define TW_OSL_H
#define TW_OSLI_DEVICE_NAME "3ware 9000 series Storage Controller"
#define TW_OSLI_MALLOC_CLASS M_TWA
#define TW_OSLI_MAX_NUM_REQUESTS TW_CL_MAX_SIMULTANEOUS_REQUESTS
#define TW_OSLI_MAX_NUM_IOS (TW_OSLI_MAX_NUM_REQUESTS - 2)
#define TW_OSLI_MAX_NUM_AENS 0x100
#define TW_OSLI_DMA_BOUNDARY ((bus_size_t)((uint64_t)1 << 32))
#define TW_OSLI_REQ_STATE_INIT 0x0
#define TW_OSLI_REQ_STATE_BUSY 0x1
#define TW_OSLI_REQ_STATE_PENDING 0x2
#define TW_OSLI_REQ_STATE_COMPLETE 0x3
#define TW_OSLI_REQ_FLAGS_DATA_IN (1<<0)
#define TW_OSLI_REQ_FLAGS_DATA_OUT (1<<1)
#define TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED (1<<2)
#define TW_OSLI_REQ_FLAGS_MAPPED (1<<3)
#define TW_OSLI_REQ_FLAGS_IN_PROGRESS (1<<4)
#define TW_OSLI_REQ_FLAGS_PASSTHRU (1<<5)
#define TW_OSLI_REQ_FLAGS_SLEEPING (1<<6)
#define TW_OSLI_REQ_FLAGS_FAILED (1<<7)
#ifdef TW_OSL_DEBUG
struct tw_osli_q_stats {
TW_UINT32 cur_len;
TW_UINT32 max_len;
};
#endif
#define TW_OSLI_FREE_Q 0
#define TW_OSLI_BUSY_Q 1
#define TW_OSLI_Q_COUNT 2
struct tw_osli_req_context {
struct tw_cl_req_handle req_handle;
struct lock ioctl_wake_timeout_lock_handle;
struct lock *ioctl_wake_timeout_lock;
struct twa_softc *ctlr;
TW_VOID *data;
TW_UINT32 length;
TW_UINT64 deadline;
TW_VOID *real_data;
TW_UINT32 real_length;
TW_UINT32 state;
TW_UINT32 flags;
TW_UINT32 error_code;
TW_VOID *orig_req;
struct tw_cl_link link;
bus_dmamap_t dma_map;
struct tw_cl_req_packet req_pkt;
};
struct twa_softc {
struct tw_cl_ctlr_handle ctlr_handle;
struct tw_osli_req_context *req_ctx_buf;
TW_UINT8 open;
TW_UINT32 flags;
TW_INT32 device_id;
TW_UINT32 alignment;
TW_UINT32 sg_size_factor;
TW_VOID *non_dma_mem;
TW_VOID *dma_mem;
TW_UINT64 dma_mem_phys;
struct tw_cl_link req_q_head[TW_OSLI_Q_COUNT];
struct task deferred_intr_callback;
struct spinlock io_lock_handle;
struct spinlock *io_lock;
struct spinlock q_lock_handle;
struct spinlock *q_lock;
struct lock sim_lock_handle;
struct lock *sim_lock;
struct callout watchdog_callout[2];
TW_UINT32 watchdog_index;
#ifdef TW_OSL_DEBUG
struct tw_osli_q_stats q_stats[TW_OSLI_Q_COUNT];
#endif
device_t bus_dev;
struct cdev *ctrl_dev;
struct resource *reg_res;
TW_INT32 reg_res_id;
bus_space_handle_t bus_handle;
bus_space_tag_t bus_tag;
bus_dma_tag_t parent_tag;
bus_dma_tag_t cmd_tag;
bus_dma_tag_t dma_tag;
bus_dma_tag_t ioctl_tag;
bus_dmamap_t cmd_map;
bus_dmamap_t ioctl_map;
struct resource *irq_res;
TW_INT32 irq_res_id;
TW_INT32 irq_type;
TW_VOID *intr_handle;
struct cam_sim *sim;
struct cam_path *path;
};
#ifdef TW_OSL_DEBUG
#define TW_OSLI_Q_INIT(sc, q_type) do { \
(sc)->q_stats[q_type].cur_len = 0; \
(sc)->q_stats[q_type].max_len = 0; \
} while(0)
#define TW_OSLI_Q_INSERT(sc, q_type) do { \
struct tw_osli_q_stats *q_stats = &((sc)->q_stats[q_type]); \
\
if (++(q_stats->cur_len) > q_stats->max_len) \
q_stats->max_len = q_stats->cur_len; \
} while(0)
#define TW_OSLI_Q_REMOVE(sc, q_type) \
(sc)->q_stats[q_type].cur_len--
#else
#define TW_OSLI_Q_INIT(sc, q_index)
#define TW_OSLI_Q_INSERT(sc, q_index)
#define TW_OSLI_Q_REMOVE(sc, q_index)
#endif
static __inline TW_VOID
tw_osli_req_q_init(struct twa_softc *sc, TW_UINT8 q_type)
{
TW_CL_Q_INIT(&(sc->req_q_head[q_type]));
TW_OSLI_Q_INIT(sc, q_type);
}
static __inline TW_VOID
tw_osli_req_q_insert_head(struct tw_osli_req_context *req, TW_UINT8 q_type)
{
spin_lock(req->ctlr->q_lock);
TW_CL_Q_INSERT_HEAD(&(req->ctlr->req_q_head[q_type]), &(req->link));
TW_OSLI_Q_INSERT(req->ctlr, q_type);
spin_unlock(req->ctlr->q_lock);
}
static __inline TW_VOID
tw_osli_req_q_insert_tail(struct tw_osli_req_context *req, TW_UINT8 q_type)
{
spin_lock(req->ctlr->q_lock);
TW_CL_Q_INSERT_TAIL(&(req->ctlr->req_q_head[q_type]), &(req->link));
TW_OSLI_Q_INSERT(req->ctlr, q_type);
spin_unlock(req->ctlr->q_lock);
}
static __inline struct tw_osli_req_context *
tw_osli_req_q_remove_head(struct twa_softc *sc, TW_UINT8 q_type)
{
struct tw_osli_req_context *req = NULL;
struct tw_cl_link *link;
spin_lock(sc->q_lock);
if ((link = TW_CL_Q_FIRST_ITEM(&(sc->req_q_head[q_type]))) !=
TW_CL_NULL) {
req = TW_CL_STRUCT_HEAD(link,
struct tw_osli_req_context, link);
TW_CL_Q_REMOVE_ITEM(&(sc->req_q_head[q_type]), &(req->link));
TW_OSLI_Q_REMOVE(sc, q_type);
}
spin_unlock(sc->q_lock);
return(req);
}
static __inline TW_VOID
tw_osli_req_q_remove_item(struct tw_osli_req_context *req, TW_UINT8 q_type)
{
spin_lock(req->ctlr->q_lock);
TW_CL_Q_REMOVE_ITEM(&(req->ctlr->req_q_head[q_type]), &(req->link));
TW_OSLI_Q_REMOVE(req->ctlr, q_type);
spin_unlock(req->ctlr->q_lock);
}
#ifdef TW_OSL_DEBUG
extern TW_INT32 TW_DEBUG_LEVEL_FOR_OSL;
#define tw_osli_dbg_dprintf(dbg_level, sc, fmt, args...) \
if (dbg_level <= TW_DEBUG_LEVEL_FOR_OSL) \
device_printf(sc->bus_dev, "%s: " fmt "\n", \
__func__, ##args)
#define tw_osli_dbg_printf(dbg_level, fmt, args...) \
if (dbg_level <= TW_DEBUG_LEVEL_FOR_OSL) \
kprintf("%s: " fmt "\n", __func__, ##args)
#else
#define tw_osli_dbg_dprintf(dbg_level, sc, fmt, args...) do { } while (0)
#define tw_osli_dbg_printf(dbg_level, fmt, args...) do { } while (0)
#endif
#define twa_printf(sc, fmt, args...) \
device_printf(((struct twa_softc *)(sc))->bus_dev, fmt, ##args)
#define tw_osli_printf(sc, err_specific_desc, args...) \
device_printf((sc)->bus_dev, \
"%s: (0x%02X: 0x%04X): %s: " err_specific_desc "\n", ##args)
#endif