#include <sys/bio.h>
#include <sys/callout.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/selinfo.h>
#include <sys/sysctl.h>
#include <sys/taskqueue.h>
#include <geom/geom_disk.h>
SYSCTL_DECL(_hw_aac);
#define AAC_TYPE_DEVO 1
#define AAC_TYPE_ALPHA 2
#define AAC_TYPE_BETA 3
#define AAC_TYPE_RELEASE 4
#define AAC_DRIVER_MAJOR_VERSION 2
#define AAC_DRIVER_MINOR_VERSION 1
#define AAC_DRIVER_BUGFIX_LEVEL 9
#define AAC_DRIVER_TYPE AAC_TYPE_RELEASE
#ifndef AAC_DRIVER_BUILD
# define AAC_DRIVER_BUILD 1
#endif
#define AAC_MAXSGENTRIES 64
#define AAC_ADAPTER_FIBS 8
#define AAC_AIFQ_LENGTH 64
#define AAC_PRINTF_BUFSIZE 256
#define AAC_BOOT_TIMEOUT (3 * 60)
#define AAC_IMMEDIATE_TIMEOUT 30
#define AAC_CMD_TIMEOUT 120
#define AAC_PERIODIC_INTERVAL 20
struct aac_container
{
struct aac_mntobj co_mntobj;
device_t co_disk;
int co_found;
TAILQ_ENTRY(aac_container) co_link;
};
struct aac_cam;
struct aac_sim
{
device_t sim_dev;
int TargetsPerBus;
int BusNumber;
int InitiatorBusId;
struct aac_softc *aac_sc;
struct aac_cam *aac_cam;
TAILQ_ENTRY(aac_sim) sim_link;
};
struct aac_disk
{
device_t ad_dev;
struct aac_softc *ad_controller;
struct aac_container *ad_container;
struct disk *ad_disk;
int ad_flags;
#define AAC_DISK_OPEN (1<<0)
int ad_cylinders;
int ad_heads;
int ad_sectors;
u_int64_t ad_size;
int unit;
};
struct aac_command
{
TAILQ_ENTRY(aac_command) cm_link;
struct aac_softc *cm_sc;
struct aac_fib *cm_fib;
u_int64_t cm_fibphys;
void *cm_data;
u_int32_t cm_datalen;
bus_dmamap_t cm_datamap;
struct aac_sg_table *cm_sgtable;
u_int cm_flags;
#define AAC_CMD_MAPPED (1<<0)
#define AAC_CMD_DATAIN (1<<1)
#define AAC_CMD_DATAOUT (1<<2)
#define AAC_CMD_COMPLETED (1<<3)
#define AAC_CMD_TIMEDOUT (1<<4)
#define AAC_ON_AACQ_FREE (1<<5)
#define AAC_ON_AACQ_READY (1<<6)
#define AAC_ON_AACQ_BUSY (1<<7)
#define AAC_ON_AACQ_AIF (1<<8)
#define AAC_ON_AACQ_NORM (1<<10)
#define AAC_ON_AACQ_MASK ((1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<10))
#define AAC_QUEUE_FRZN (1<<9)
#define AAC_REQ_BIO (1 << 11)
#define AAC_REQ_CCB (1 << 12)
void (*cm_complete)(struct aac_command *cm);
void *cm_private;
time_t cm_timestamp;
int cm_queue;
int cm_index;
};
struct aac_fibmap {
TAILQ_ENTRY(aac_fibmap) fm_link;
struct aac_fib *aac_fibs;
bus_dmamap_t aac_fibmap;
struct aac_command *aac_commands;
};
struct aac_common {
struct aac_fib ac_fibs[AAC_ADAPTER_FIBS];
struct aac_adapter_init ac_init;
u_int8_t ac_qbuf[sizeof(struct aac_queue_table) +
AAC_QUEUE_ALIGN];
char ac_printf[AAC_PRINTF_BUFSIZE];
struct aac_fib ac_sync_fib;
};
struct aac_interface
{
int (*aif_get_fwstatus)(struct aac_softc *sc);
void (*aif_qnotify)(struct aac_softc *sc, int qbit);
int (*aif_get_istatus)(struct aac_softc *sc);
void (*aif_clr_istatus)(struct aac_softc *sc, int mask);
void (*aif_set_mailbox)(struct aac_softc *sc, u_int32_t command,
u_int32_t arg0, u_int32_t arg1,
u_int32_t arg2, u_int32_t arg3);
int (*aif_get_mailbox)(struct aac_softc *sc, int mb);
void (*aif_set_interrupts)(struct aac_softc *sc, int enable);
int (*aif_send_command)(struct aac_softc *sc, struct aac_command *cm);
int (*aif_get_outb_queue)(struct aac_softc *sc);
void (*aif_set_outb_queue)(struct aac_softc *sc, int index);
};
extern const struct aac_interface aac_rx_interface;
extern const struct aac_interface aac_sa_interface;
extern const struct aac_interface aac_fa_interface;
extern const struct aac_interface aac_rkt_interface;
#define AAC_GET_FWSTATUS(sc) ((sc)->aac_if->aif_get_fwstatus((sc)))
#define AAC_QNOTIFY(sc, qbit) ((sc)->aac_if->aif_qnotify((sc), (qbit)))
#define AAC_GET_ISTATUS(sc) ((sc)->aac_if->aif_get_istatus((sc)))
#define AAC_CLEAR_ISTATUS(sc, mask) ((sc)->aac_if->aif_clr_istatus((sc), \
(mask)))
#define AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3) \
((sc)->aac_if->aif_set_mailbox((sc), (command), (arg0), (arg1), (arg2), \
(arg3)))
#define AAC_GET_MAILBOX(sc, mb) ((sc)->aac_if->aif_get_mailbox((sc), \
(mb)))
#define AAC_MASK_INTERRUPTS(sc) ((sc)->aac_if->aif_set_interrupts((sc), \
0))
#define AAC_UNMASK_INTERRUPTS(sc) ((sc)->aac_if->aif_set_interrupts((sc), \
1))
#define AAC_SEND_COMMAND(sc, cm) ((sc)->aac_if->aif_send_command((sc), (cm)))
#define AAC_GET_OUTB_QUEUE(sc) ((sc)->aac_if->aif_get_outb_queue((sc)))
#define AAC_SET_OUTB_QUEUE(sc, idx) ((sc)->aac_if->aif_set_outb_queue((sc), (idx)))
#define AAC_MEM0_SETREG4(sc, reg, val) bus_space_write_4(sc->aac_btag0, \
sc->aac_bhandle0, reg, val)
#define AAC_MEM0_GETREG4(sc, reg) bus_space_read_4(sc->aac_btag0, \
sc->aac_bhandle0, reg)
#define AAC_MEM0_SETREG2(sc, reg, val) bus_space_write_2(sc->aac_btag0, \
sc->aac_bhandle0, reg, val)
#define AAC_MEM0_GETREG2(sc, reg) bus_space_read_2(sc->aac_btag0, \
sc->aac_bhandle0, reg)
#define AAC_MEM0_SETREG1(sc, reg, val) bus_space_write_1(sc->aac_btag0, \
sc->aac_bhandle0, reg, val)
#define AAC_MEM0_GETREG1(sc, reg) bus_space_read_1(sc->aac_btag0, \
sc->aac_bhandle0, reg)
#define AAC_MEM1_SETREG4(sc, reg, val) bus_space_write_4(sc->aac_btag1, \
sc->aac_bhandle1, reg, val)
#define AAC_MEM1_GETREG4(sc, reg) bus_space_read_4(sc->aac_btag1, \
sc->aac_bhandle1, reg)
#define AAC_MEM1_SETREG2(sc, reg, val) bus_space_write_2(sc->aac_btag1, \
sc->aac_bhandle1, reg, val)
#define AAC_MEM1_GETREG2(sc, reg) bus_space_read_2(sc->aac_btag1, \
sc->aac_bhandle1, reg)
#define AAC_MEM1_SETREG1(sc, reg, val) bus_space_write_1(sc->aac_btag1, \
sc->aac_bhandle1, reg, val)
#define AAC_MEM1_GETREG1(sc, reg) bus_space_read_1(sc->aac_btag1, \
sc->aac_bhandle1, reg)
struct aac_fib_context {
u_int32_t unique;
int ctx_idx;
int ctx_wrap;
struct aac_fib_context *next, *prev;
};
struct aac_softc
{
device_t aac_dev;
struct resource *aac_regs_res0, *aac_regs_res1;
bus_space_handle_t aac_bhandle0, aac_bhandle1;
bus_space_tag_t aac_btag0, aac_btag1;
bus_dma_tag_t aac_parent_dmat;
bus_dma_tag_t aac_buffer_dmat;
struct resource *aac_irq;
void *aac_intr;
eventhandler_tag eh;
int aac_state;
#define AAC_STATE_SUSPEND (1<<0)
#define AAC_STATE_UNUSED0 (1<<1)
#define AAC_STATE_INTERRUPTS_ON (1<<2)
#define AAC_STATE_AIF_SLEEPER (1<<3)
struct FsaRevision aac_revision;
int aac_hwif;
#define AAC_HWIF_I960RX 0
#define AAC_HWIF_STRONGARM 1
#define AAC_HWIF_RKT 3
#define AAC_HWIF_NARK 4
#define AAC_HWIF_UNKNOWN -1
bus_dma_tag_t aac_common_dmat;
bus_dmamap_t aac_common_dmamap;
struct aac_common *aac_common;
u_int32_t aac_common_busaddr;
const struct aac_interface *aac_if;
bus_dma_tag_t aac_fib_dmat;
TAILQ_HEAD(,aac_fibmap) aac_fibmap_tqh;
u_int total_fibs;
struct aac_command *aac_commands;
TAILQ_HEAD(,aac_command) aac_free;
TAILQ_HEAD(,aac_command) aac_ready;
TAILQ_HEAD(,aac_command) aac_busy;
TAILQ_HEAD(,aac_event) aac_ev_cmfree;
struct bio_queue_head aac_bioq;
struct aac_queue_table *aac_queues;
struct aac_queue_entry *aac_qentries[AAC_QUEUE_COUNT];
struct aac_qstat aac_qstat[AACQ_COUNT];
TAILQ_HEAD(,aac_container) aac_container_tqh;
struct mtx aac_container_lock;
struct mtx aac_io_lock;
struct task aac_task_complete;
struct intr_config_hook aac_ich;
struct cdev *aac_dev_t;
struct mtx aac_aifq_lock;
struct aac_fib aac_aifq[AAC_AIFQ_LENGTH];
int aifq_idx;
int aifq_filled;
struct aac_fib_context *fibctx;
struct selinfo rcv_select;
struct proc *aifthread;
int aifflags;
#define AAC_AIFFLAGS_RUNNING (1 << 0)
#define AAC_AIFFLAGS_UNUSED0 (1 << 1)
#define AAC_AIFFLAGS_EXIT (1 << 2)
#define AAC_AIFFLAGS_EXITED (1 << 3)
#define AAC_AIFFLAGS_UNUSED1 (1 << 4)
#define AAC_AIFFLAGS_ALLOCFIBS (1 << 5)
#define AAC_AIFFLAGS_PENDING AAC_AIFFLAGS_ALLOCFIBS
u_int32_t flags;
#define AAC_FLAGS_PERC2QC (1 << 0)
#define AAC_FLAGS_ENABLE_CAM (1 << 1)
#define AAC_FLAGS_CAM_NORESET (1 << 2)
#define AAC_FLAGS_CAM_PASSONLY (1 << 3)
#define AAC_FLAGS_SG_64BIT (1 << 4)
#define AAC_FLAGS_4GB_WINDOW (1 << 5)
#define AAC_FLAGS_NO4GB (1 << 6)
#define AAC_FLAGS_256FIBS (1 << 7)
#define AAC_FLAGS_BROKEN_MEMMAP (1 << 8)
#define AAC_FLAGS_SLAVE (1 << 9)
#define AAC_FLAGS_MASTER (1 << 10)
#define AAC_FLAGS_NEW_COMM (1 << 11)
#define AAC_FLAGS_RAW_IO (1 << 12)
#define AAC_FLAGS_ARRAY_64BIT (1 << 13)
#define AAC_FLAGS_LBA_64BIT (1 << 14)
#define AAC_FLAGS_NOMSI (1U << 31)
u_int32_t supported_options;
u_int32_t scsi_method_id;
TAILQ_HEAD(,aac_sim) aac_sim_tqh;
struct callout aac_daemontime;
u_int32_t aac_max_fibs;
u_int32_t aac_max_fibs_alloc;
u_int32_t aac_max_fib_size;
u_int32_t aac_sg_tablesize;
u_int32_t aac_max_sectors;
#define AAC_CAM_TARGET_WILDCARD ~0
void (*cam_rescan_cb)(struct aac_softc *, uint32_t,
uint32_t);
};
#define AAC_EVENT_NONE 0x00
#define AAC_EVENT_CMFREE 0x01
#define AAC_EVENT_MASK 0xff
#define AAC_EVENT_REPEAT 0x100
typedef void aac_event_cb_t(struct aac_softc *sc, struct aac_event *event,
void *arg);
struct aac_event {
TAILQ_ENTRY(aac_event) ev_links;
int ev_type;
aac_event_cb_t *ev_callback;
void *ev_arg;
};
extern void aac_free(struct aac_softc *sc);
extern int aac_attach(struct aac_softc *sc);
extern int aac_detach(device_t dev);
extern int aac_shutdown(device_t dev);
extern int aac_suspend(device_t dev);
extern int aac_resume(device_t dev);
extern void aac_new_intr(void *arg);
extern int aac_filter(void *arg);
extern void aac_submit_bio(struct bio *bp);
extern void aac_biodone(struct bio *bp);
extern void aac_startio(struct aac_softc *sc);
extern int aac_alloc_command(struct aac_softc *sc,
struct aac_command **cmp);
extern void aac_release_command(struct aac_command *cm);
extern int aac_sync_fib(struct aac_softc *sc, u_int32_t command,
u_int32_t xferstate, struct aac_fib *fib,
u_int16_t datasize);
extern void aac_add_event(struct aac_softc *sc, struct aac_event
*event);
#ifdef AAC_DEBUG
extern int aac_debug_enable;
# define fwprintf(sc, flags, fmt, args...) \
do { \
if (!aac_debug_enable) \
break; \
if (sc != NULL) \
device_printf(((struct aac_softc *)sc)->aac_dev, \
"%s: " fmt "\n", __func__, ##args); \
else \
printf("%s: " fmt "\n", __func__, ##args); \
} while(0)
extern void aac_print_queues(struct aac_softc *sc);
extern void aac_panic(struct aac_softc *sc, char *reason);
extern void aac_print_fib(struct aac_softc *sc, struct aac_fib *fib,
const char *caller);
extern void aac_print_aif(struct aac_softc *sc,
struct aac_aif_command *aif);
#define AAC_PRINT_FIB(sc, fib) aac_print_fib(sc, fib, __func__)
#else
# define fwprintf(sc, flags, fmt, args...)
# define aac_print_queues(sc)
# define aac_panic(sc, reason)
# define AAC_PRINT_FIB(sc, fib)
# define aac_print_aif(sc, aac_aif_command)
#endif
struct aac_code_lookup {
const char *string;
u_int32_t code;
};
#define AACQ_ADD(sc, qname) \
do { \
struct aac_qstat *qs; \
\
qs = &(sc)->aac_qstat[qname]; \
\
qs->q_length++; \
if (qs->q_length > qs->q_max) \
qs->q_max = qs->q_length; \
} while (0)
#define AACQ_REMOVE(sc, qname) (sc)->aac_qstat[qname].q_length--
#define AACQ_INIT(sc, qname) \
do { \
sc->aac_qstat[qname].q_length = 0; \
sc->aac_qstat[qname].q_max = 0; \
} while (0)
#define AACQ_COMMAND_QUEUE(name, index) \
static __inline void \
aac_initq_ ## name (struct aac_softc *sc) \
{ \
TAILQ_INIT(&sc->aac_ ## name); \
AACQ_INIT(sc, index); \
} \
static __inline void \
aac_enqueue_ ## name (struct aac_command *cm) \
{ \
if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) { \
panic("aac: command %p is on another queue, flags = %#x", \
cm, cm->cm_flags); \
} \
TAILQ_INSERT_TAIL(&cm->cm_sc->aac_ ## name, cm, cm_link); \
cm->cm_flags |= AAC_ON_ ## index; \
AACQ_ADD(cm->cm_sc, index); \
} \
static __inline void \
aac_requeue_ ## name (struct aac_command *cm) \
{ \
if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) { \
panic("aac: command %p is on another queue, flags = %#x", \
cm, cm->cm_flags); \
} \
TAILQ_INSERT_HEAD(&cm->cm_sc->aac_ ## name, cm, cm_link); \
cm->cm_flags |= AAC_ON_ ## index; \
AACQ_ADD(cm->cm_sc, index); \
} \
static __inline struct aac_command * \
aac_dequeue_ ## name (struct aac_softc *sc) \
{ \
struct aac_command *cm; \
\
if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) { \
if ((cm->cm_flags & AAC_ON_ ## index) == 0) { \
panic("aac: command %p not in queue, flags = %#x, bit = %#x", \
cm, cm->cm_flags, AAC_ON_ ## index); \
} \
TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link); \
cm->cm_flags &= ~AAC_ON_ ## index; \
AACQ_REMOVE(sc, index); \
} \
return(cm); \
} \
static __inline void \
aac_remove_ ## name (struct aac_command *cm) \
{ \
if ((cm->cm_flags & AAC_ON_ ## index) == 0) { \
panic("aac: command %p not in queue, flags = %#x, bit = %#x", \
cm, cm->cm_flags, AAC_ON_ ## index); \
} \
TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link); \
cm->cm_flags &= ~AAC_ON_ ## index; \
AACQ_REMOVE(cm->cm_sc, index); \
} \
AACQ_COMMAND_QUEUE(free, AACQ_FREE);
AACQ_COMMAND_QUEUE(ready, AACQ_READY);
AACQ_COMMAND_QUEUE(busy, AACQ_BUSY);
static __inline void
aac_initq_bio(struct aac_softc *sc)
{
bioq_init(&sc->aac_bioq);
AACQ_INIT(sc, AACQ_BIO);
}
static __inline void
aac_enqueue_bio(struct aac_softc *sc, struct bio *bp)
{
bioq_insert_tail(&sc->aac_bioq, bp);
AACQ_ADD(sc, AACQ_BIO);
}
static __inline struct bio *
aac_dequeue_bio(struct aac_softc *sc)
{
struct bio *bp;
if ((bp = bioq_first(&sc->aac_bioq)) != NULL) {
bioq_remove(&sc->aac_bioq, bp);
AACQ_REMOVE(sc, AACQ_BIO);
}
return(bp);
}
static __inline void
aac_print_printf(struct aac_softc *sc)
{
device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE,
sc->aac_common->ac_printf);
sc->aac_common->ac_printf[0] = 0;
AAC_QNOTIFY(sc, AAC_DB_PRINTF);
}
static __inline int
aac_alloc_sync_fib(struct aac_softc *sc, struct aac_fib **fib)
{
mtx_assert(&sc->aac_io_lock, MA_OWNED);
*fib = &sc->aac_common->ac_sync_fib;
return (0);
}
static __inline void
aac_release_sync_fib(struct aac_softc *sc)
{
mtx_assert(&sc->aac_io_lock, MA_OWNED);
}