#include <sys/cdefs.h>
#include <sys/param.h>
#include "opt_sym.h"
#include <dev/sym/sym_conf.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/endian.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/proc.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <machine/atomic.h>
#include <sys/rman.h>
#include <cam/cam.h>
#include <cam/cam_ccb.h>
#include <cam/cam_sim.h>
#include <cam/cam_xpt_sim.h>
#include <cam/cam_debug.h>
#include <cam/scsi/scsi_all.h>
#include <cam/scsi/scsi_message.h>
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef u_int8_t u8;
typedef u_int16_t u16;
typedef u_int32_t u32;
#include <dev/sym/sym_defs.h>
#include <dev/sym/sym_fw.h>
#if defined __i386__ || defined __amd64__
#define MEMORY_BARRIER() __compiler_membar()
#elif defined __powerpc__
#define MEMORY_BARRIER() __asm__ volatile("eieio; sync" : : : "memory")
#elif defined __arm__
#define MEMORY_BARRIER() dmb()
#elif defined __aarch64__
#define MEMORY_BARRIER() dmb(sy)
#elif defined __riscv
#define MEMORY_BARRIER() fence()
#else
#error "Not supported platform"
#endif
typedef struct sym_quehead {
struct sym_quehead *flink;
struct sym_quehead *blink;
} SYM_QUEHEAD;
#define sym_que_init(ptr) do { \
(ptr)->flink = (ptr); (ptr)->blink = (ptr); \
} while (0)
static __inline void __sym_que_add(struct sym_quehead * new,
struct sym_quehead * blink,
struct sym_quehead * flink)
{
flink->blink = new;
new->flink = flink;
new->blink = blink;
blink->flink = new;
}
static __inline void __sym_que_del(struct sym_quehead * blink,
struct sym_quehead * flink)
{
flink->blink = blink;
blink->flink = flink;
}
static __inline int sym_que_empty(struct sym_quehead *head)
{
return head->flink == head;
}
static __inline void sym_que_splice(struct sym_quehead *list,
struct sym_quehead *head)
{
struct sym_quehead *first = list->flink;
if (first != list) {
struct sym_quehead *last = list->blink;
struct sym_quehead *at = head->flink;
first->blink = head;
head->flink = first;
last->flink = at;
at->blink = last;
}
}
#define sym_que_entry(ptr, type, member) \
((type *)((char *)(ptr)-(size_t)(&((type *)0)->member)))
#define sym_insque(new, pos) __sym_que_add(new, pos, (pos)->flink)
#define sym_remque(el) __sym_que_del((el)->blink, (el)->flink)
#define sym_insque_head(new, head) __sym_que_add(new, head, (head)->flink)
static __inline struct sym_quehead *sym_remque_head(struct sym_quehead *head)
{
struct sym_quehead *elem = head->flink;
if (elem != head)
__sym_que_del(head, elem->flink);
else
elem = NULL;
return elem;
}
#define sym_insque_tail(new, head) __sym_que_add(new, (head)->blink, head)
#define FOR_EACH_QUEUED_ELEMENT(head, qp) \
for (qp = (head)->flink; qp != (head); qp = qp->flink)
#define sym_qptr(p) ((struct sym_quehead *) (p))
#define sym_set_bit(p, n) (((u32 *)(p))[(n)>>5] |= (1<<((n)&0x1f)))
#define sym_clr_bit(p, n) (((u32 *)(p))[(n)>>5] &= ~(1<<((n)&0x1f)))
#define sym_is_bit(p, n) (((u32 *)(p))[(n)>>5] & (1<<((n)&0x1f)))
#if SYM_CONF_MAX_TAG_ORDER > 8
#error "more than 256 tags per logical unit not allowed."
#endif
#define SYM_CONF_MAX_TASK (1<<SYM_CONF_MAX_TAG_ORDER)
#ifndef SYM_CONF_MAX_TAG
#define SYM_CONF_MAX_TAG SYM_CONF_MAX_TASK
#endif
#if SYM_CONF_MAX_TAG > SYM_CONF_MAX_TASK
#undef SYM_CONF_MAX_TAG
#define SYM_CONF_MAX_TAG SYM_CONF_MAX_TASK
#endif
#define NO_TAG (256)
#if SYM_CONF_MAX_TARGET > 16
#error "more than 16 targets not allowed."
#endif
#if SYM_CONF_MAX_LUN > 64
#error "more than 64 logical units per target not allowed."
#endif
#define SYM_CONF_MIN_ASYNC (40)
#ifdef SYM_CONF_MAX_START
#define SYM_CONF_MAX_QUEUE (SYM_CONF_MAX_START+2)
#else
#define SYM_CONF_MAX_QUEUE (7*SYM_CONF_MAX_TASK+2)
#define SYM_CONF_MAX_START (SYM_CONF_MAX_QUEUE-2)
#endif
#if SYM_CONF_MAX_QUEUE > PAGE_SIZE/8
#undef SYM_CONF_MAX_QUEUE
#define SYM_CONF_MAX_QUEUE PAGE_SIZE/8
#undef SYM_CONF_MAX_START
#define SYM_CONF_MAX_START (SYM_CONF_MAX_QUEUE-2)
#endif
#define MAX_QUEUE SYM_CONF_MAX_QUEUE
#define DEBUG_ALLOC (0x0001)
#define DEBUG_PHASE (0x0002)
#define DEBUG_POLL (0x0004)
#define DEBUG_QUEUE (0x0008)
#define DEBUG_RESULT (0x0010)
#define DEBUG_SCATTER (0x0020)
#define DEBUG_SCRIPT (0x0040)
#define DEBUG_TINY (0x0080)
#define DEBUG_TIMING (0x0100)
#define DEBUG_NEGO (0x0200)
#define DEBUG_TAGS (0x0400)
#define DEBUG_POINTER (0x0800)
#if 0
static int sym_debug = 0;
#define DEBUG_FLAGS sym_debug
#else
#define DEBUG_FLAGS (0x0000)
#endif
#define sym_verbose (np->verbose)
static void UDELAY(int us) { DELAY(us); }
static void MDELAY(int ms) { while (ms--) UDELAY(1000); }
#define MEMO_SHIFT 4
#define MEMO_PAGE_ORDER 0
#if 0
#define MEMO_FREE_UNUSED
#endif
#define MEMO_WARN 1
#define MEMO_CLUSTER_SHIFT (PAGE_SHIFT+MEMO_PAGE_ORDER)
#define MEMO_CLUSTER_SIZE (1UL << MEMO_CLUSTER_SHIFT)
#define MEMO_CLUSTER_MASK (MEMO_CLUSTER_SIZE-1)
#define get_pages() malloc(MEMO_CLUSTER_SIZE, M_DEVBUF, M_NOWAIT)
#define free_pages(p) free((p), M_DEVBUF)
typedef u_long m_addr_t;
typedef struct m_link {
struct m_link *next;
} m_link_s;
typedef struct m_vtob {
struct m_vtob *next;
bus_dmamap_t dmamap;
m_addr_t vaddr;
m_addr_t baddr;
} m_vtob_s;
#define VTOB_HASH_SHIFT 5
#define VTOB_HASH_SIZE (1UL << VTOB_HASH_SHIFT)
#define VTOB_HASH_MASK (VTOB_HASH_SIZE-1)
#define VTOB_HASH_CODE(m) \
((((m_addr_t) (m)) >> MEMO_CLUSTER_SHIFT) & VTOB_HASH_MASK)
typedef struct m_pool {
bus_dma_tag_t dev_dmat;
bus_dma_tag_t dmat;
m_addr_t (*getp)(struct m_pool *);
#ifdef MEMO_FREE_UNUSED
void (*freep)(struct m_pool *, m_addr_t);
#endif
#define M_GETP() mp->getp(mp)
#define M_FREEP(p) mp->freep(mp, p)
int nump;
m_vtob_s *(vtob[VTOB_HASH_SIZE]);
struct m_pool *next;
struct m_link h[MEMO_CLUSTER_SHIFT - MEMO_SHIFT + 1];
} m_pool_s;
static void *___sym_malloc(m_pool_s *mp, int size)
{
int i = 0;
int s = (1 << MEMO_SHIFT);
int j;
m_addr_t a;
m_link_s *h = mp->h;
if (size > MEMO_CLUSTER_SIZE)
return NULL;
while (size > s) {
s <<= 1;
++i;
}
j = i;
while (!h[j].next) {
if (s == MEMO_CLUSTER_SIZE) {
h[j].next = (m_link_s *) M_GETP();
if (h[j].next)
h[j].next->next = NULL;
break;
}
++j;
s <<= 1;
}
a = (m_addr_t) h[j].next;
if (a) {
h[j].next = h[j].next->next;
while (j > i) {
j -= 1;
s >>= 1;
h[j].next = (m_link_s *) (a+s);
h[j].next->next = NULL;
}
}
#ifdef DEBUG
printf("___sym_malloc(%d) = %p\n", size, (void *) a);
#endif
return (void *) a;
}
static void ___sym_mfree(m_pool_s *mp, void *ptr, int size)
{
int i = 0;
int s = (1 << MEMO_SHIFT);
m_link_s *q;
m_addr_t a, b;
m_link_s *h = mp->h;
#ifdef DEBUG
printf("___sym_mfree(%p, %d)\n", ptr, size);
#endif
if (size > MEMO_CLUSTER_SIZE)
return;
while (size > s) {
s <<= 1;
++i;
}
a = (m_addr_t) ptr;
while (1) {
#ifdef MEMO_FREE_UNUSED
if (s == MEMO_CLUSTER_SIZE) {
M_FREEP(a);
break;
}
#endif
b = a ^ s;
q = &h[i];
while (q->next && q->next != (m_link_s *) b) {
q = q->next;
}
if (!q->next) {
((m_link_s *) a)->next = h[i].next;
h[i].next = (m_link_s *) a;
break;
}
q->next = q->next->next;
a = a & b;
s <<= 1;
++i;
}
}
static void *__sym_calloc2(m_pool_s *mp, int size, char *name, int uflags)
{
void *p;
p = ___sym_malloc(mp, size);
if (DEBUG_FLAGS & DEBUG_ALLOC)
printf ("new %-10s[%4d] @%p.\n", name, size, p);
if (p)
bzero(p, size);
else if (uflags & MEMO_WARN)
printf ("__sym_calloc2: failed to allocate %s[%d]\n", name, size);
return p;
}
#define __sym_calloc(mp, s, n) __sym_calloc2(mp, s, n, MEMO_WARN)
static void __sym_mfree(m_pool_s *mp, void *ptr, int size, char *name)
{
if (DEBUG_FLAGS & DEBUG_ALLOC)
printf ("freeing %-10s[%4d] @%p.\n", name, size, ptr);
___sym_mfree(mp, ptr, size);
}
static m_addr_t ___mp0_getp(m_pool_s *mp)
{
m_addr_t m = (m_addr_t) get_pages();
if (m)
++mp->nump;
return m;
}
#ifdef MEMO_FREE_UNUSED
static void ___mp0_freep(m_pool_s *mp, m_addr_t m)
{
free_pages(m);
--mp->nump;
}
#endif
#ifdef MEMO_FREE_UNUSED
static m_pool_s mp0 = {0, 0, ___mp0_getp, ___mp0_freep};
#else
static m_pool_s mp0 = {0, 0, ___mp0_getp};
#endif
static void *sym_calloc(int size, char *name)
{
void *m;
m = __sym_calloc(&mp0, size, name);
return m;
}
static void sym_mfree(void *ptr, int size, char *name)
{
__sym_mfree(&mp0, ptr, size, name);
}
static void getbaddrcb(void *arg, bus_dma_segment_t *segs, int nseg __diagused,
int error)
{
bus_addr_t *baddr;
KASSERT(nseg == 1, ("%s: too many DMA segments (%d)", __func__, nseg));
baddr = (bus_addr_t *)arg;
if (error)
*baddr = 0;
else
*baddr = segs->ds_addr;
}
static m_addr_t ___dma_getp(m_pool_s *mp)
{
m_vtob_s *vbp;
void *vaddr = NULL;
bus_addr_t baddr = 0;
vbp = __sym_calloc(&mp0, sizeof(*vbp), "VTOB");
if (!vbp)
goto out_err;
if (bus_dmamem_alloc(mp->dmat, &vaddr,
BUS_DMA_COHERENT | BUS_DMA_NOCACHE | BUS_DMA_WAITOK, &vbp->dmamap))
goto out_err;
bus_dmamap_load(mp->dmat, vbp->dmamap, vaddr, MEMO_CLUSTER_SIZE,
getbaddrcb, &baddr, BUS_DMA_NOWAIT);
if (baddr) {
int hc = VTOB_HASH_CODE(vaddr);
vbp->vaddr = (m_addr_t) vaddr;
vbp->baddr = (m_addr_t) baddr;
vbp->next = mp->vtob[hc];
mp->vtob[hc] = vbp;
++mp->nump;
return (m_addr_t) vaddr;
}
out_err:
if (vaddr)
bus_dmamem_free(mp->dmat, vaddr, vbp->dmamap);
if (vbp)
__sym_mfree(&mp0, vbp, sizeof(*vbp), "VTOB");
return 0;
}
#ifdef MEMO_FREE_UNUSED
static void ___dma_freep(m_pool_s *mp, m_addr_t m)
{
m_vtob_s **vbpp, *vbp;
int hc = VTOB_HASH_CODE(m);
vbpp = &mp->vtob[hc];
while (*vbpp && (*vbpp)->vaddr != m)
vbpp = &(*vbpp)->next;
if (*vbpp) {
vbp = *vbpp;
*vbpp = (*vbpp)->next;
bus_dmamap_unload(mp->dmat, vbp->dmamap);
bus_dmamem_free(mp->dmat, (void *) vbp->vaddr, vbp->dmamap);
__sym_mfree(&mp0, vbp, sizeof(*vbp), "VTOB");
--mp->nump;
}
}
#endif
static __inline m_pool_s *___get_dma_pool(bus_dma_tag_t dev_dmat)
{
m_pool_s *mp;
for (mp = mp0.next; mp && mp->dev_dmat != dev_dmat; mp = mp->next);
return mp;
}
static m_pool_s *___cre_dma_pool(bus_dma_tag_t dev_dmat)
{
m_pool_s *mp = NULL;
mp = __sym_calloc(&mp0, sizeof(*mp), "MPOOL");
if (mp) {
mp->dev_dmat = dev_dmat;
if (!bus_dma_tag_create(dev_dmat, 1, MEMO_CLUSTER_SIZE,
BUS_SPACE_MAXADDR_32BIT,
BUS_SPACE_MAXADDR,
NULL, NULL, MEMO_CLUSTER_SIZE, 1,
MEMO_CLUSTER_SIZE, 0,
NULL, NULL, &mp->dmat)) {
mp->getp = ___dma_getp;
#ifdef MEMO_FREE_UNUSED
mp->freep = ___dma_freep;
#endif
mp->next = mp0.next;
mp0.next = mp;
return mp;
}
}
if (mp)
__sym_mfree(&mp0, mp, sizeof(*mp), "MPOOL");
return NULL;
}
#ifdef MEMO_FREE_UNUSED
static void ___del_dma_pool(m_pool_s *p)
{
struct m_pool **pp = &mp0.next;
while (*pp && *pp != p)
pp = &(*pp)->next;
if (*pp) {
*pp = (*pp)->next;
bus_dma_tag_destroy(p->dmat);
__sym_mfree(&mp0, p, sizeof(*p), "MPOOL");
}
}
#endif
static void *__sym_calloc_dma(bus_dma_tag_t dev_dmat, int size, char *name)
{
struct m_pool *mp;
void *m = NULL;
mp = ___get_dma_pool(dev_dmat);
if (!mp)
mp = ___cre_dma_pool(dev_dmat);
if (mp)
m = __sym_calloc(mp, size, name);
#ifdef MEMO_FREE_UNUSED
if (mp && !mp->nump)
___del_dma_pool(mp);
#endif
return m;
}
static void
__sym_mfree_dma(bus_dma_tag_t dev_dmat, void *m, int size, char *name)
{
struct m_pool *mp;
mp = ___get_dma_pool(dev_dmat);
if (mp)
__sym_mfree(mp, m, size, name);
#ifdef MEMO_FREE_UNUSED
if (mp && !mp->nump)
___del_dma_pool(mp);
#endif
}
static m_addr_t __vtobus(bus_dma_tag_t dev_dmat, void *m)
{
m_pool_s *mp;
int hc = VTOB_HASH_CODE(m);
m_vtob_s *vp = NULL;
m_addr_t a = ((m_addr_t) m) & ~MEMO_CLUSTER_MASK;
mp = ___get_dma_pool(dev_dmat);
if (mp) {
vp = mp->vtob[hc];
while (vp && (m_addr_t) vp->vaddr != a)
vp = vp->next;
}
if (!vp)
panic("sym: VTOBUS FAILED!\n");
return vp ? vp->baddr + (((m_addr_t) m) - a) : 0;
}
#define _uvptv_(p) ((void *)((vm_offset_t)(p)))
#define _sym_calloc_dma(np, s, n) __sym_calloc_dma(np->bus_dmat, s, n)
#define _sym_mfree_dma(np, p, s, n) \
__sym_mfree_dma(np->bus_dmat, _uvptv_(p), s, n)
#define sym_calloc_dma(s, n) _sym_calloc_dma(np, s, n)
#define sym_mfree_dma(p, s, n) _sym_mfree_dma(np, p, s, n)
#define _vtobus(np, p) __vtobus(np->bus_dmat, _uvptv_(p))
#define vtobus(p) _vtobus(np, p)
static void sym_printb_hex (u_char *p, int n)
{
while (n-- > 0)
printf (" %x", *p++);
}
static void sym_printl_hex (char *label, u_char *p, int n)
{
printf ("%s", label);
sym_printb_hex (p, n);
printf (".\n");
}
static const char *sym_scsi_bus_mode(int mode)
{
switch(mode) {
case SMODE_HVD: return "HVD";
case SMODE_SE: return "SE";
case SMODE_LVD: return "LVD";
}
return "??";
}
#ifdef SYM_CONF_NVRAM_SUPPORT
static const u_char Tekram_sync[16] =
{25,31,37,43, 50,62,75,125, 12,15,18,21, 6,7,9,10};
#endif
struct sym_nvram {
int type;
#define SYM_SYMBIOS_NVRAM (1)
#define SYM_TEKRAM_NVRAM (2)
#ifdef SYM_CONF_NVRAM_SUPPORT
union {
Symbios_nvram Symbios;
Tekram_nvram Tekram;
} data;
#endif
};
#ifndef assert
#define assert(expression) { \
if (!(expression)) { \
(void)panic( \
"assertion \"%s\" failed: file \"%s\", line %d\n", \
#expression, \
__FILE__, __LINE__); \
} \
}
#endif
#define sym_offb(o) (o)
#define sym_offw(o) (o)
#define cpu_to_scr(dw) htole32(dw)
#define scr_to_cpu(dw) le32toh(dw)
#if defined(SYM_CONF_IOMAPPED)
#define INB_OFF(o) bus_read_1(np->io_res, (o))
#define INW_OFF(o) bus_read_2(np->io_res, (o))
#define INL_OFF(o) bus_read_4(np->io_res, (o))
#define OUTB_OFF(o, v) bus_write_1(np->io_res, (o), (v))
#define OUTW_OFF(o, v) bus_write_2(np->io_res, (o), (v))
#define OUTL_OFF(o, v) bus_write_4(np->io_res, (o), (v))
#else
#define INB_OFF(o) bus_read_1(np->mmio_res, (o))
#define INW_OFF(o) bus_read_2(np->mmio_res, (o))
#define INL_OFF(o) bus_read_4(np->mmio_res, (o))
#define OUTB_OFF(o, v) bus_write_1(np->mmio_res, (o), (v))
#define OUTW_OFF(o, v) bus_write_2(np->mmio_res, (o), (v))
#define OUTL_OFF(o, v) bus_write_4(np->mmio_res, (o), (v))
#endif
#define OUTRAM_OFF(o, a, l) \
bus_write_region_1(np->ram_res, (o), (a), (l))
#define INB(r) INB_OFF(offsetof(struct sym_reg,r))
#define INW(r) INW_OFF(offsetof(struct sym_reg,r))
#define INL(r) INL_OFF(offsetof(struct sym_reg,r))
#define OUTB(r, v) OUTB_OFF(offsetof(struct sym_reg,r), (v))
#define OUTW(r, v) OUTW_OFF(offsetof(struct sym_reg,r), (v))
#define OUTL(r, v) OUTL_OFF(offsetof(struct sym_reg,r), (v))
#define OUTONB(r, m) OUTB(r, INB(r) | (m))
#define OUTOFFB(r, m) OUTB(r, INB(r) & ~(m))
#define OUTONW(r, m) OUTW(r, INW(r) | (m))
#define OUTOFFW(r, m) OUTW(r, INW(r) & ~(m))
#define OUTONL(r, m) OUTL(r, INL(r) | (m))
#define OUTOFFL(r, m) OUTL(r, INL(r) & ~(m))
#define OUTL_DSP(v) \
do { \
MEMORY_BARRIER(); \
OUTL (nc_dsp, (v)); \
} while (0)
#define OUTONB_STD() \
do { \
MEMORY_BARRIER(); \
OUTONB (nc_dcntl, (STD|NOCOM)); \
} while (0)
#define HS_IDLE (0)
#define HS_BUSY (1)
#define HS_NEGOTIATE (2)
#define HS_DISCONNECT (3)
#define HS_WAIT (4)
#define HS_DONEMASK (0x80)
#define HS_COMPLETE (4|HS_DONEMASK)
#define HS_SEL_TIMEOUT (5|HS_DONEMASK)
#define HS_UNEXPECTED (6|HS_DONEMASK)
#define HS_COMP_ERR (7|HS_DONEMASK)
#define SIR_BAD_SCSI_STATUS (1)
#define SIR_SEL_ATN_NO_MSG_OUT (2)
#define SIR_MSG_RECEIVED (3)
#define SIR_MSG_WEIRD (4)
#define SIR_NEGO_FAILED (5)
#define SIR_NEGO_PROTO (6)
#define SIR_SCRIPT_STOPPED (7)
#define SIR_REJECT_TO_SEND (8)
#define SIR_SWIDE_OVERRUN (9)
#define SIR_SODL_UNDERRUN (10)
#define SIR_RESEL_NO_MSG_IN (11)
#define SIR_RESEL_NO_IDENTIFY (12)
#define SIR_RESEL_BAD_LUN (13)
#define SIR_TARGET_SELECTED (14)
#define SIR_RESEL_BAD_I_T_L (15)
#define SIR_RESEL_BAD_I_T_L_Q (16)
#define SIR_ABORT_SENT (17)
#define SIR_RESEL_ABORTED (18)
#define SIR_MSG_OUT_DONE (19)
#define SIR_COMPLETE_ERROR (20)
#define SIR_DATA_OVERRUN (21)
#define SIR_BAD_PHASE (22)
#define SIR_MAX (22)
#define XE_EXTRA_DATA (1)
#define XE_BAD_PHASE (1<<1)
#define XE_PARITY_ERR (1<<2)
#define XE_SODL_UNRUN (1<<3)
#define XE_SWIDE_OVRUN (1<<4)
#define NS_SYNC (1)
#define NS_WIDE (2)
#define NS_PPR (3)
#define CCB_HASH_SHIFT 8
#define CCB_HASH_SIZE (1UL << CCB_HASH_SHIFT)
#define CCB_HASH_MASK (CCB_HASH_SIZE-1)
#define CCB_HASH_CODE(dsa) (((dsa) >> 9) & CCB_HASH_MASK)
#define SYM_DISC_ENABLED (1)
#define SYM_TAGS_ENABLED (1<<1)
#define SYM_SCAN_BOOT_DISABLED (1<<2)
#define SYM_SCAN_LUNS_DISABLED (1<<3)
#define SYM_AVOID_BUS_RESET (1)
#define SYM_SCAN_TARGETS_HILO (1<<1)
#define SYM_QUIRK_AUTOSAVE 1
#define SYM_LOCK() mtx_lock(&np->mtx)
#define SYM_LOCK_ASSERT(_what) mtx_assert(&np->mtx, (_what))
#define SYM_LOCK_DESTROY() mtx_destroy(&np->mtx)
#define SYM_LOCK_INIT() mtx_init(&np->mtx, "sym_lock", NULL, MTX_DEF)
#define SYM_LOCK_INITIALIZED() mtx_initialized(&np->mtx)
#define SYM_UNLOCK() mtx_unlock(&np->mtx)
#define SYM_SNOOP_TIMEOUT (10000000)
#define SYM_PCI_IO PCIR_BAR(0)
#define SYM_PCI_MMIO PCIR_BAR(1)
#define SYM_PCI_RAM PCIR_BAR(2)
#define SYM_PCI_RAM64 PCIR_BAR(3)
#define sym_hcb_ptr spriv_ptr0
typedef struct sym_tcb *tcb_p;
typedef struct sym_lcb *lcb_p;
typedef struct sym_ccb *ccb_p;
typedef struct sym_hcb *hcb_p;
struct sym_trans {
u8 scsi_version;
u8 spi_version;
u8 period;
u8 offset;
u8 width;
u8 options;
};
struct sym_tinfo {
struct sym_trans current;
struct sym_trans goal;
struct sym_trans user;
};
#define BUS_8_BIT MSG_EXT_WDTR_BUS_8_BIT
#define BUS_16_BIT MSG_EXT_WDTR_BUS_16_BIT
struct sym_tcbh {
u32 luntbl_sa;
u32 lun0_sa;
u_char uval;
u_char sval;
u_char filler1;
u_char wval;
};
struct sym_tcb {
struct sym_tcbh head;
u32 *luntbl;
lcb_p lun0p;
#if SYM_CONF_MAX_LUN > 1
lcb_p *lunmp;
#endif
u32 lun_map[(SYM_CONF_MAX_LUN+31)/32];
u32 busy0_map[(SYM_CONF_MAX_LUN+31)/32];
struct sym_tinfo tinfo;
ccb_p nego_cp;
u_char to_reset;
u_char usrflags;
u_short usrtags;
};
CTASSERT(((offsetof(struct sym_reg, nc_sxfer) ^
offsetof(struct sym_tcb, head.sval)) &3) == 0);
CTASSERT(((offsetof(struct sym_reg, nc_scntl3) ^
offsetof(struct sym_tcb, head.wval)) &3) == 0);
struct sym_lcbh {
u32 resel_sa;
u32 itl_task_sa;
u32 itlq_tbl_sa;
};
struct sym_lcb {
struct sym_lcbh head;
u32 *itlq_tbl;
u_short busy_itlq;
u_short busy_itl;
u_short ia_tag;
u_short if_tag;
u_char *cb_tags;
u_char to_clear;
u_char user_flags;
u_char current_flags;
};
struct sym_actscr {
u32 start;
u32 restart;
};
struct sym_pmc {
struct sym_tblmove sg;
u32 ret;
};
#if SYM_CONF_MAX_LUN <= 1
#define sym_lp(tp, lun) (!lun) ? (tp)->lun0p : 0
#else
#define sym_lp(tp, lun) \
(!lun) ? (tp)->lun0p : (tp)->lunmp ? (tp)->lunmp[(lun)] : 0
#endif
#define QU_REG scr0
#define HS_REG scr1
#define HS_PRT nc_scr1
#define SS_REG scr2
#define SS_PRT nc_scr2
#define HF_REG scr3
#define HF_PRT nc_scr3
#define actualquirks phys.head.status[0]
#define host_status phys.head.status[1]
#define ssss_status phys.head.status[2]
#define host_flags phys.head.status[3]
#define HF_IN_PM0 1u
#define HF_IN_PM1 (1u<<1)
#define HF_ACT_PM (1u<<2)
#define HF_DP_SAVED (1u<<3)
#define HF_SENSE (1u<<4)
#define HF_EXT_ERR (1u<<5)
#define HF_DATA_IN (1u<<6)
#ifdef SYM_CONF_IARB_SUPPORT
#define HF_HINT_IARB (1u<<7)
#endif
struct sym_ccbh {
struct sym_actscr go;
u32 savep;
u32 lastp;
u32 goalp;
u8 status[4];
};
struct sym_dsb {
struct sym_ccbh head;
struct sym_pmc pm0;
struct sym_pmc pm1;
struct sym_tblsel select;
struct sym_tblmove smsg;
struct sym_tblmove smsg_ext;
struct sym_tblmove cmd;
struct sym_tblmove sense;
struct sym_tblmove wresid;
struct sym_tblmove data [SYM_CONF_MAX_SG];
};
struct sym_ccb {
struct sym_dsb phys;
struct callout ch;
union ccb *cam_ccb;
u8 cdb_buf[16];
u8 *sns_bbuf;
#define SYM_SNS_BBUF_LEN sizeof(struct scsi_sense_data)
int data_len;
int segments;
u_char nego_status;
u_char xerr_status;
u32 extra_bytes;
u_char scsi_smsg [12];
u_char scsi_smsg2[12];
u_char sensecmd[6];
u_char sv_scsi_status;
u_char sv_xerr_status;
int sv_resid;
void *arg;
bus_dmamap_t dmamap;
u_char dmamapped;
#define SYM_DMA_NONE 0
#define SYM_DMA_READ 1
#define SYM_DMA_WRITE 2
u32 ccb_ba;
u_short tag;
u_char target;
u_char lun;
ccb_p link_ccbh;
SYM_QUEHEAD
link_ccbq;
u32 startp;
int ext_sg;
int ext_ofs;
u_char to_abort;
};
#define CCB_BA(cp,lbl) (cp->ccb_ba + offsetof(struct sym_ccb, lbl))
struct sym_hcb {
struct mtx mtx;
#ifdef SYM_CONF_GENERIC_SUPPORT
struct sym_ccbh ccb_head;
struct sym_tcbh tcb_head;
struct sym_lcbh lcb_head;
#endif
struct sym_actscr idletask, notask, bad_itl, bad_itlq;
vm_offset_t idletask_ba, notask_ba, bad_itl_ba, bad_itlq_ba;
u32 *badluntbl;
u32 badlun_sa;
u32 hcb_ba;
u32 scr_ram_seg;
device_t device;
u_char sv_scntl0, sv_scntl3, sv_dmode, sv_dcntl, sv_ctest3, sv_ctest4,
sv_ctest5, sv_gpcntl, sv_stest2, sv_stest4, sv_scntl4,
sv_stest1;
u_char rv_scntl0, rv_scntl3, rv_dmode, rv_dcntl, rv_ctest3, rv_ctest4,
rv_ctest5, rv_stest2, rv_ccntl0, rv_ccntl1, rv_scntl4;
#ifdef __amd64__
struct sym_tcb *target;
#else
struct sym_tcb target[SYM_CONF_MAX_TARGET];
#endif
u32 *targtbl;
u32 targtbl_ba;
struct cam_sim *sim;
struct cam_path *path;
struct resource *irq_res;
struct resource *io_res;
struct resource *mmio_res;
struct resource *ram_res;
void *intr;
bus_dma_tag_t bus_dmat;
bus_dma_tag_t data_dmat;
vm_offset_t mmio_ba;
vm_offset_t ram_ba;
int ram_ws;
u_char *scripta0;
u_char *scriptb0;
vm_offset_t scripta_ba;
vm_offset_t scriptb_ba;
vm_offset_t scriptb0_ba;
u_short scripta_sz;
u_short scriptb_sz;
struct sym_fwa_ba fwa_bas;
struct sym_fwb_ba fwb_bas;
void (*fw_setup)(hcb_p np, const struct sym_fw *fw);
void (*fw_patch)(hcb_p np);
const char *fw_name;
u_short device_id;
u_char revision_id;
u_int features;
u_char myaddr;
u_char maxburst;
u_char maxwide;
u_char minsync;
u_char maxsync;
u_char maxoffs;
u_char minsync_dt;
u_char maxsync_dt;
u_char maxoffs_dt;
u_char multiplier;
u_char clock_divn;
u32 clock_khz;
u32 pciclk_khz;
volatile
u32 *squeue;
u32 squeue_ba;
u_short squeueput;
u_short actccbs;
u_short dqueueget;
volatile
u32 *dqueue;
u32 dqueue_ba;
u_char msgout[8];
u_char msgin [8];
u32 lastmsg;
u_char scratch;
u_char usrflags;
u_char scsi_mode;
u_char verbose;
u32 cache;
ccb_p ccbh[CCB_HASH_SIZE];
SYM_QUEHEAD free_ccbq;
SYM_QUEHEAD busy_ccbq;
SYM_QUEHEAD comp_ccbq;
SYM_QUEHEAD cam_ccbq;
#ifdef SYM_CONF_IARB_SUPPORT
u_short iarb_max;
u_short iarb_count;
ccb_p last_cp;
#endif
u_char abrt_msg[4];
struct sym_tblmove abrt_tbl;
struct sym_tblsel abrt_sel;
u_char istat_sem;
};
#define HCB_BA(np, lbl) (np->hcb_ba + offsetof(struct sym_hcb, lbl))
static __inline const char *sym_name(hcb_p np)
{
return device_get_nameunit(np->device);
}
#define SYM_GEN_A(s, label) ((short) offsetof(s, label)),
#define SYM_GEN_B(s, label) ((short) offsetof(s, label)),
#define PADDR_A(label) SYM_GEN_PADDR_A(struct SYM_FWA_SCR, label)
#define PADDR_B(label) SYM_GEN_PADDR_B(struct SYM_FWB_SCR, label)
#ifdef SYM_CONF_GENERIC_SUPPORT
#define SYM_FWA_SCR sym_fw1a_scr
#define SYM_FWB_SCR sym_fw1b_scr
#include <dev/sym/sym_fw1.h>
static const struct sym_fwa_ofs sym_fw1a_ofs = {
SYM_GEN_FW_A(struct SYM_FWA_SCR)
};
static const struct sym_fwb_ofs sym_fw1b_ofs = {
SYM_GEN_FW_B(struct SYM_FWB_SCR)
};
#undef SYM_FWA_SCR
#undef SYM_FWB_SCR
#endif
#define SYM_FWA_SCR sym_fw2a_scr
#define SYM_FWB_SCR sym_fw2b_scr
#include <dev/sym/sym_fw2.h>
static const struct sym_fwa_ofs sym_fw2a_ofs = {
SYM_GEN_FW_A(struct SYM_FWA_SCR)
};
static const struct sym_fwb_ofs sym_fw2b_ofs = {
SYM_GEN_FW_B(struct SYM_FWB_SCR)
SYM_GEN_B(struct SYM_FWB_SCR, start64)
SYM_GEN_B(struct SYM_FWB_SCR, pm_handle)
};
#undef SYM_FWA_SCR
#undef SYM_FWB_SCR
#undef SYM_GEN_A
#undef SYM_GEN_B
#undef PADDR_A
#undef PADDR_B
#ifdef SYM_CONF_GENERIC_SUPPORT
static void
sym_fw1_patch(hcb_p np)
{
struct sym_fw1a_scr *scripta0;
struct sym_fw1b_scr *scriptb0;
scripta0 = (struct sym_fw1a_scr *) np->scripta0;
scriptb0 = (struct sym_fw1b_scr *) np->scriptb0;
if (!(np->features & FE_LED0)) {
scripta0->idle[0] = cpu_to_scr(SCR_NO_OP);
scripta0->reselected[0] = cpu_to_scr(SCR_NO_OP);
scripta0->start[0] = cpu_to_scr(SCR_NO_OP);
}
#ifdef SYM_CONF_IARB_SUPPORT
if (!SYM_CONF_SET_IARB_ON_ARB_LOST)
scripta0->ungetjob[0] = cpu_to_scr(SCR_NO_OP);
#endif
scriptb0->startpos[0] = cpu_to_scr(np->squeue_ba);
scriptb0->done_pos[0] = cpu_to_scr(np->dqueue_ba);
scriptb0->targtbl[0] = cpu_to_scr(np->targtbl_ba);
}
#endif
static void
sym_fw2_patch(hcb_p np)
{
struct sym_fw2a_scr *scripta0;
struct sym_fw2b_scr *scriptb0;
scripta0 = (struct sym_fw2a_scr *) np->scripta0;
scriptb0 = (struct sym_fw2b_scr *) np->scriptb0;
if (!(np->features & FE_LED0)) {
scripta0->idle[0] = cpu_to_scr(SCR_NO_OP);
scripta0->reselected[0] = cpu_to_scr(SCR_NO_OP);
scripta0->start[0] = cpu_to_scr(SCR_NO_OP);
}
#ifdef SYM_CONF_IARB_SUPPORT
if (!SYM_CONF_SET_IARB_ON_ARB_LOST)
scripta0->ungetjob[0] = cpu_to_scr(SCR_NO_OP);
#endif
scriptb0->startpos[0] = cpu_to_scr(np->squeue_ba);
scriptb0->done_pos[0] = cpu_to_scr(np->dqueue_ba);
scriptb0->targtbl[0] = cpu_to_scr(np->targtbl_ba);
if (!(np->features & FE_C10)) {
scripta0->resel_scntl4[0] = cpu_to_scr(SCR_NO_OP);
scripta0->resel_scntl4[1] = cpu_to_scr(0);
}
if (!(np->device_id == PCI_ID_LSI53C1010_2 &&
np->revision_id < 0x1 &&
np->pciclk_khz < 60000)) {
scripta0->datao_phase[0] = cpu_to_scr(SCR_NO_OP);
scripta0->datao_phase[1] = cpu_to_scr(0);
}
if (!(np->device_id == PCI_ID_LSI53C1010 &&
1)) {
scripta0->sel_done[0] = cpu_to_scr(SCR_NO_OP);
scripta0->sel_done[1] = cpu_to_scr(0);
}
scriptb0->pm0_data_addr[0] =
cpu_to_scr(np->scripta_ba +
offsetof(struct sym_fw2a_scr, pm0_data));
scriptb0->pm1_data_addr[0] =
cpu_to_scr(np->scripta_ba +
offsetof(struct sym_fw2a_scr, pm1_data));
}
static void
sym_fw_fill_data (u32 *in, u32 *out)
{
int i;
for (i = 0; i < SYM_CONF_MAX_SG; i++) {
*in++ = SCR_CHMOV_TBL ^ SCR_DATA_IN;
*in++ = offsetof (struct sym_dsb, data[i]);
*out++ = SCR_CHMOV_TBL ^ SCR_DATA_OUT;
*out++ = offsetof (struct sym_dsb, data[i]);
}
}
static void
sym_fw_setup_bus_addresses(hcb_p np, const struct sym_fw *fw)
{
u32 *pa;
const u_short *po;
int i;
po = (const u_short *) fw->a_ofs;
pa = (u32 *) &np->fwa_bas;
for (i = 0 ; i < sizeof(np->fwa_bas)/sizeof(u32) ; i++)
pa[i] = np->scripta_ba + po[i];
po = (const u_short *) fw->b_ofs;
pa = (u32 *) &np->fwb_bas;
for (i = 0 ; i < sizeof(np->fwb_bas)/sizeof(u32) ; i++)
pa[i] = np->scriptb_ba + po[i];
}
#ifdef SYM_CONF_GENERIC_SUPPORT
static void
sym_fw1_setup(hcb_p np, const struct sym_fw *fw)
{
struct sym_fw1a_scr *scripta0;
scripta0 = (struct sym_fw1a_scr *) np->scripta0;
sym_fw_fill_data(scripta0->data_in, scripta0->data_out);
sym_fw_setup_bus_addresses(np, fw);
}
#endif
static void
sym_fw2_setup(hcb_p np, const struct sym_fw *fw)
{
struct sym_fw2a_scr *scripta0;
scripta0 = (struct sym_fw2a_scr *) np->scripta0;
sym_fw_fill_data(scripta0->data_in, scripta0->data_out);
sym_fw_setup_bus_addresses(np, fw);
}
#ifdef SYM_CONF_GENERIC_SUPPORT
static const struct sym_fw sym_fw1 = SYM_FW_ENTRY(sym_fw1, "NCR-generic");
#endif
static const struct sym_fw sym_fw2 = SYM_FW_ENTRY(sym_fw2, "LOAD/STORE-based");
static const struct sym_fw *
sym_find_firmware(const struct sym_pci_chip *chip)
{
if (chip->features & FE_LDSTR)
return &sym_fw2;
#ifdef SYM_CONF_GENERIC_SUPPORT
else if (!(chip->features & (FE_PFEN|FE_NOPM|FE_DAC)))
return &sym_fw1;
#endif
else
return NULL;
}
static void sym_fw_bind_script (hcb_p np, u32 *start, int len)
{
u32 opcode, new, old, tmp1, tmp2;
u32 *end, *cur;
int relocs;
cur = start;
end = start + len/4;
while (cur < end) {
opcode = *cur;
if (opcode == 0) {
device_printf(np->device, "ERROR0 IN SCRIPT at %d.\n",
(int)(cur-start));
MDELAY (10000);
++cur;
continue;
}
if (opcode == SCR_DATA_ZERO) {
*cur++ = 0;
continue;
}
if (DEBUG_FLAGS & DEBUG_SCRIPT)
printf ("%d: <%x>\n", (int) (cur-start),
(unsigned)opcode);
switch (opcode >> 28) {
case 0xf:
relocs = 0;
break;
case 0xe:
relocs = 1;
break;
case 0xc:
relocs = 2;
tmp1 = cur[1];
tmp2 = cur[2];
if ((tmp1 ^ tmp2) & 3) {
device_printf(np->device,
"ERROR1 IN SCRIPT at %d.\n",
(int)(cur-start));
MDELAY (10000);
}
if ((opcode & SCR_NO_FLUSH) &&
!(np->features & FE_PFEN)) {
opcode = (opcode & ~SCR_NO_FLUSH);
}
break;
case 0x0:
if (!(np->features & FE_WIDE))
opcode = (opcode | OPC_MOVE);
relocs = 1;
break;
case 0x1:
if (!(np->features & FE_WIDE))
opcode = (opcode | OPC_MOVE);
relocs = 0;
break;
case 0x8:
if (opcode & 0x00800000)
relocs = 0;
else if ((opcode & 0xf8400000) == 0x80400000)
relocs = 2;
else
relocs = 1;
break;
case 0x4:
case 0x5:
case 0x6:
case 0x7:
relocs = 1;
break;
default:
relocs = 0;
break;
}
*cur++ = cpu_to_scr(opcode);
if (!relocs) {
*cur = cpu_to_scr(*cur);
++cur;
continue;
}
while (relocs--) {
old = *cur;
switch (old & RELOC_MASK) {
case RELOC_REGISTER:
new = (old & ~RELOC_MASK) + np->mmio_ba;
break;
case RELOC_LABEL_A:
new = (old & ~RELOC_MASK) + np->scripta_ba;
break;
case RELOC_LABEL_B:
new = (old & ~RELOC_MASK) + np->scriptb_ba;
break;
case RELOC_SOFTC:
new = (old & ~RELOC_MASK) + np->hcb_ba;
break;
case 0:
if (old == 0) {
new = old;
break;
}
default:
new = 0;
panic("sym_fw_bind_script: "
"weird relocation %x\n", old);
break;
}
*cur++ = cpu_to_scr(new);
}
}
}
static void sym_save_initial_setting (hcb_p np);
static int sym_prepare_setting (hcb_p np, struct sym_nvram *nvram);
static int sym_prepare_nego (hcb_p np, ccb_p cp, int nego, u_char *msgptr);
static void sym_put_start_queue (hcb_p np, ccb_p cp);
static void sym_chip_reset (hcb_p np);
static void sym_soft_reset (hcb_p np);
static void sym_start_reset (hcb_p np);
static int sym_reset_scsi_bus (hcb_p np, int enab_int);
static int sym_wakeup_done (hcb_p np);
static void sym_flush_busy_queue (hcb_p np, int cam_status);
static void sym_flush_comp_queue (hcb_p np, int cam_status);
static void sym_init (hcb_p np, int reason);
static int sym_getsync(hcb_p np, u_char dt, u_char sfac, u_char *divp,
u_char *fakp);
static void sym_setsync (hcb_p np, ccb_p cp, u_char ofs, u_char per,
u_char div, u_char fak);
static void sym_setwide (hcb_p np, ccb_p cp, u_char wide);
static void sym_setpprot(hcb_p np, ccb_p cp, u_char dt, u_char ofs,
u_char per, u_char wide, u_char div, u_char fak);
static void sym_settrans(hcb_p np, ccb_p cp, u_char dt, u_char ofs,
u_char per, u_char wide, u_char div, u_char fak);
static void sym_log_hard_error (hcb_p np, u_short sist, u_char dstat);
static void sym_intr (void *arg);
static void sym_poll (struct cam_sim *sim);
static void sym_recover_scsi_int (hcb_p np, u_char hsts);
static void sym_int_sto (hcb_p np);
static void sym_int_udc (hcb_p np);
static void sym_int_sbmc (hcb_p np);
static void sym_int_par (hcb_p np, u_short sist);
static void sym_int_ma (hcb_p np);
static int sym_dequeue_from_squeue(hcb_p np, int i, int target, int lun,
int task);
static void sym_sir_bad_scsi_status (hcb_p np, ccb_p cp);
static int sym_clear_tasks (hcb_p np, int status, int targ, int lun, int task);
static void sym_sir_task_recovery (hcb_p np, int num);
static int sym_evaluate_dp (hcb_p np, ccb_p cp, u32 scr, int *ofs);
static void sym_modify_dp(hcb_p np, ccb_p cp, int ofs);
static int sym_compute_residual (hcb_p np, ccb_p cp);
static int sym_show_msg (u_char * msg);
static void sym_print_msg (ccb_p cp, char *label, u_char *msg);
static void sym_sync_nego (hcb_p np, tcb_p tp, ccb_p cp);
static void sym_ppr_nego (hcb_p np, tcb_p tp, ccb_p cp);
static void sym_wide_nego (hcb_p np, tcb_p tp, ccb_p cp);
static void sym_nego_default (hcb_p np, tcb_p tp, ccb_p cp);
static void sym_nego_rejected (hcb_p np, tcb_p tp, ccb_p cp);
static void sym_int_sir (hcb_p np);
static void sym_free_ccb (hcb_p np, ccb_p cp);
static ccb_p sym_get_ccb (hcb_p np, u_char tn, u_char ln, u_char tag_order);
static ccb_p sym_alloc_ccb (hcb_p np);
static ccb_p sym_ccb_from_dsa (hcb_p np, u32 dsa);
static lcb_p sym_alloc_lcb (hcb_p np, u_char tn, u_char ln);
static void sym_alloc_lcb_tags (hcb_p np, u_char tn, u_char ln);
static int sym_snooptest (hcb_p np);
static void sym_selectclock(hcb_p np, u_char scntl3);
static void sym_getclock (hcb_p np, int mult);
static int sym_getpciclock (hcb_p np);
static void sym_complete_ok (hcb_p np, ccb_p cp);
static void sym_complete_error (hcb_p np, ccb_p cp);
static void sym_callout (void *arg);
static int sym_abort_scsiio (hcb_p np, union ccb *ccb, int timed_out);
static void sym_reset_dev (hcb_p np, union ccb *ccb);
static void sym_action (struct cam_sim *sim, union ccb *ccb);
static int sym_setup_cdb (hcb_p np, struct ccb_scsiio *csio, ccb_p cp);
static void sym_setup_data_and_start (hcb_p np, struct ccb_scsiio *csio,
ccb_p cp);
static int sym_fast_scatter_sg_physical(hcb_p np, ccb_p cp,
bus_dma_segment_t *psegs, int nsegs);
static int sym_scatter_sg_physical (hcb_p np, ccb_p cp,
bus_dma_segment_t *psegs, int nsegs);
static void sym_action2 (struct cam_sim *sim, union ccb *ccb);
static void sym_update_trans(hcb_p np, struct sym_trans *tip,
struct ccb_trans_settings *cts);
static void sym_update_dflags(hcb_p np, u_char *flags,
struct ccb_trans_settings *cts);
static const struct sym_pci_chip *sym_find_pci_chip (device_t dev);
static device_probe_t sym_pci_probe;
static device_attach_t sym_pci_attach;
static device_detach_t sym_pci_detach;
static int sym_cam_attach (hcb_p np);
static void sym_cam_free (hcb_p np);
static void sym_nvram_setup_host (hcb_p np, struct sym_nvram *nvram);
static void sym_nvram_setup_target (hcb_p np, int targ, struct sym_nvram *nvp);
static int sym_read_nvram (hcb_p np, struct sym_nvram *nvp);
static void PRINT_TARGET (hcb_p np, int target)
{
printf ("%s:%d:", sym_name(np), target);
}
static void PRINT_LUN(hcb_p np, int target, int lun)
{
printf ("%s:%d:%d:", sym_name(np), target, lun);
}
static void PRINT_ADDR (ccb_p cp)
{
if (cp && cp->cam_ccb)
xpt_print_path(cp->cam_ccb->ccb_h.path);
}
static void sym_freeze_cam_ccb(union ccb *ccb)
{
if (!(ccb->ccb_h.flags & CAM_DEV_QFRZDIS)) {
if (!(ccb->ccb_h.status & CAM_DEV_QFRZN)) {
ccb->ccb_h.status |= CAM_DEV_QFRZN;
xpt_freeze_devq(ccb->ccb_h.path, 1);
}
}
}
static __inline void sym_set_cam_status(union ccb *ccb, cam_status status)
{
ccb->ccb_h.status &= ~CAM_STATUS_MASK;
ccb->ccb_h.status |= status;
}
static __inline int sym_get_cam_status(union ccb *ccb)
{
return ccb->ccb_h.status & CAM_STATUS_MASK;
}
static void sym_enqueue_cam_ccb(ccb_p cp)
{
hcb_p np;
union ccb *ccb;
ccb = cp->cam_ccb;
np = (hcb_p) cp->arg;
assert(!(ccb->ccb_h.status & CAM_SIM_QUEUED));
ccb->ccb_h.status = CAM_REQ_INPROG;
callout_reset_sbt(&cp->ch, SBT_1MS * ccb->ccb_h.timeout, 0, sym_callout,
(caddr_t)ccb, 0);
ccb->ccb_h.status |= CAM_SIM_QUEUED;
ccb->ccb_h.sym_hcb_ptr = np;
sym_insque_tail(sym_qptr(&ccb->ccb_h.sim_links), &np->cam_ccbq);
}
static void sym_xpt_done(hcb_p np, union ccb *ccb, ccb_p cp)
{
SYM_LOCK_ASSERT(MA_OWNED);
if (ccb->ccb_h.status & CAM_SIM_QUEUED) {
callout_stop(&cp->ch);
sym_remque(sym_qptr(&ccb->ccb_h.sim_links));
ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
ccb->ccb_h.sym_hcb_ptr = NULL;
}
xpt_done(ccb);
}
static void sym_xpt_done2(hcb_p np, union ccb *ccb, int cam_status)
{
SYM_LOCK_ASSERT(MA_OWNED);
sym_set_cam_status(ccb, cam_status);
xpt_done(ccb);
}
#define _5M 5000000
static const u32 div_10M[] =
{2*_5M, 3*_5M, 4*_5M, 6*_5M, 8*_5M, 12*_5M, 16*_5M};
#define burst_length(bc) (!(bc))? 0 : 1 << (bc)
#define burst_code(dmode, ctest4, ctest5) \
(ctest4) & 0x80? 0 : (((dmode) & 0xc0) >> 6) + ((ctest5) & 0x04) + 1
static __inline void sym_init_burst(hcb_p np, u_char bc)
{
np->rv_ctest4 &= ~0x80;
np->rv_dmode &= ~(0x3 << 6);
np->rv_ctest5 &= ~0x4;
if (!bc) {
np->rv_ctest4 |= 0x80;
}
else {
--bc;
np->rv_dmode |= ((bc & 0x3) << 6);
np->rv_ctest5 |= (bc & 0x4);
}
}
static void sym_print_targets_flag(hcb_p np, int mask, char *msg)
{
int cnt;
int i;
for (cnt = 0, i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
if (i == np->myaddr)
continue;
if (np->target[i].usrflags & mask) {
if (!cnt++)
device_printf(np->device,
"%s disabled for targets", msg);
printf(" %d", i);
}
}
if (cnt)
printf(".\n");
}
static void sym_save_initial_setting (hcb_p np)
{
np->sv_scntl0 = INB(nc_scntl0) & 0x0a;
np->sv_scntl3 = INB(nc_scntl3) & 0x07;
np->sv_dmode = INB(nc_dmode) & 0xce;
np->sv_dcntl = INB(nc_dcntl) & 0xa8;
np->sv_ctest3 = INB(nc_ctest3) & 0x01;
np->sv_ctest4 = INB(nc_ctest4) & 0x80;
np->sv_gpcntl = INB(nc_gpcntl);
np->sv_stest1 = INB(nc_stest1);
np->sv_stest2 = INB(nc_stest2) & 0x20;
np->sv_stest4 = INB(nc_stest4);
if (np->features & FE_C10) {
np->sv_scntl4 = INB(nc_scntl4);
np->sv_ctest5 = INB(nc_ctest5) & 0x04;
}
else
np->sv_ctest5 = INB(nc_ctest5) & 0x24;
}
static int sym_prepare_setting(hcb_p np, struct sym_nvram *nvram)
{
u_char burst_max;
u32 period;
int i;
np->maxwide = (np->features & FE_WIDE)? 1 : 0;
if (np->features & FE_QUAD)
np->multiplier = 4;
else if (np->features & FE_DBLR)
np->multiplier = 2;
else
np->multiplier = 1;
np->clock_khz = (np->features & FE_CLK80)? 80000 : 40000;
np->clock_khz *= np->multiplier;
if (np->clock_khz != 40000)
sym_getclock(np, np->multiplier);
i = np->clock_divn - 1;
while (--i >= 0) {
if (10ul * SYM_CONF_MIN_ASYNC * np->clock_khz > div_10M[i]) {
++i;
break;
}
}
np->rv_scntl3 = i+1;
if (np->features & FE_C10)
np->rv_scntl3 = 0;
period = howmany(4 * div_10M[0], np->clock_khz);
if (period <= 250) np->minsync = 10;
else if (period <= 303) np->minsync = 11;
else if (period <= 500) np->minsync = 12;
else np->minsync = howmany(period, 40);
if (np->minsync < 25 &&
!(np->features & (FE_ULTRA|FE_ULTRA2|FE_ULTRA3)))
np->minsync = 25;
else if (np->minsync < 12 &&
!(np->features & (FE_ULTRA2|FE_ULTRA3)))
np->minsync = 12;
period = (11 * div_10M[np->clock_divn - 1]) / (4 * np->clock_khz);
np->maxsync = period > 2540 ? 254 : period / 10;
if ((np->features & (FE_C10|FE_ULTRA3)) == (FE_C10|FE_ULTRA3)) {
if (np->clock_khz == 160000) {
np->minsync_dt = 9;
np->maxsync_dt = 50;
np->maxoffs_dt = 62;
}
}
if (np->features & FE_DAC)
#ifdef __LP64__
np->rv_ccntl1 |= (XTIMOD | EXTIBMV);
#else
np->rv_ccntl1 |= (DDAC);
#endif
if (np->features & FE_NOPM)
np->rv_ccntl0 |= (ENPMJ);
if (np->device_id == PCI_ID_LSI53C1010 &&
np->revision_id < 0x2)
np->rv_ccntl0 |= DILS;
burst_max = SYM_SETUP_BURST_ORDER;
if (burst_max == 255)
burst_max = burst_code(np->sv_dmode, np->sv_ctest4,
np->sv_ctest5);
if (burst_max > 7)
burst_max = 7;
if (burst_max > np->maxburst)
burst_max = np->maxburst;
if ((np->device_id == PCI_ID_SYM53C810 &&
np->revision_id >= 0x10 && np->revision_id <= 0x11) ||
(np->device_id == PCI_ID_SYM53C860 &&
np->revision_id <= 0x1))
np->features &= ~(FE_WRIE|FE_ERL|FE_ERMP);
if (np->features & FE_ERL)
np->rv_dmode |= ERL;
if (np->features & FE_BOF)
np->rv_dmode |= BOF;
if (np->features & FE_ERMP)
np->rv_dmode |= ERMP;
#if 1
if ((np->features & FE_PFEN) && !np->ram_ba)
#else
if (np->features & FE_PFEN)
#endif
np->rv_dcntl |= PFEN;
if (np->features & FE_CLSE)
np->rv_dcntl |= CLSE;
if (np->features & FE_WRIE)
np->rv_ctest3 |= WRIE;
if (np->features & FE_DFS)
np->rv_ctest5 |= DFS;
if (SYM_SETUP_PCI_PARITY)
np->rv_ctest4 |= MPEE;
if (SYM_SETUP_SCSI_PARITY)
np->rv_scntl0 |= 0x0a;
np->myaddr = 255;
sym_nvram_setup_host (np, nvram);
if (np->myaddr == 255) {
np->myaddr = INB(nc_scid) & 0x07;
if (!np->myaddr)
np->myaddr = SYM_SETUP_HOST_ID;
}
sym_init_burst(np, burst_max);
np->scsi_mode = SMODE_SE;
if (np->features & (FE_ULTRA2|FE_ULTRA3))
np->scsi_mode = (np->sv_stest4 & SMODE);
else if (np->features & FE_DIFF) {
if (SYM_SETUP_SCSI_DIFF == 1) {
if (np->sv_scntl3) {
if (np->sv_stest2 & 0x20)
np->scsi_mode = SMODE_HVD;
}
else if (nvram->type == SYM_SYMBIOS_NVRAM) {
if (!(INB(nc_gpreg) & 0x08))
np->scsi_mode = SMODE_HVD;
}
}
else if (SYM_SETUP_SCSI_DIFF == 2)
np->scsi_mode = SMODE_HVD;
}
if (np->scsi_mode == SMODE_HVD)
np->rv_stest2 |= 0x20;
if ((SYM_SETUP_SCSI_LED ||
(nvram->type == SYM_SYMBIOS_NVRAM ||
(nvram->type == SYM_TEKRAM_NVRAM &&
np->device_id == PCI_ID_SYM53C895))) &&
!(np->features & FE_LEDC) && !(np->sv_gpcntl & 0x01))
np->features |= FE_LED0;
switch(SYM_SETUP_IRQ_MODE & 3) {
case 2:
np->rv_dcntl |= IRQM;
break;
case 1:
np->rv_dcntl |= (np->sv_dcntl & IRQM);
break;
default:
break;
}
for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
tcb_p tp = &np->target[i];
tp->tinfo.user.scsi_version = tp->tinfo.current.scsi_version= 2;
tp->tinfo.user.spi_version = tp->tinfo.current.spi_version = 2;
tp->tinfo.user.period = np->minsync;
if (np->features & FE_ULTRA3)
tp->tinfo.user.period = np->minsync_dt;
tp->tinfo.user.offset = np->maxoffs;
tp->tinfo.user.width = np->maxwide ? BUS_16_BIT : BUS_8_BIT;
tp->usrflags |= (SYM_DISC_ENABLED | SYM_TAGS_ENABLED);
tp->usrtags = SYM_SETUP_MAX_TAG;
sym_nvram_setup_target (np, i, nvram);
if (np->features & FE_ULTRA3) {
if (tp->tinfo.user.period <= 9 &&
tp->tinfo.user.width == BUS_16_BIT) {
tp->tinfo.user.options |= PPR_OPT_DT;
tp->tinfo.user.offset = np->maxoffs_dt;
tp->tinfo.user.spi_version = 3;
}
}
if (!tp->usrtags)
tp->usrflags &= ~SYM_TAGS_ENABLED;
}
i = nvram->type;
device_printf(np->device, "%s NVRAM, ID %d, Fast-%d, %s, %s\n",
i == SYM_SYMBIOS_NVRAM ? "Symbios" :
(i == SYM_TEKRAM_NVRAM ? "Tekram" : "No"),
np->myaddr,
(np->features & FE_ULTRA3) ? 80 :
(np->features & FE_ULTRA2) ? 40 :
(np->features & FE_ULTRA) ? 20 : 10,
sym_scsi_bus_mode(np->scsi_mode),
(np->rv_scntl0 & 0xa) ? "parity checking" : "NO parity");
if (sym_verbose) {
device_printf(np->device, "%s IRQ line driver%s\n",
np->rv_dcntl & IRQM ? "totem pole" : "open drain",
np->ram_ba ? ", using on-chip SRAM" : "");
device_printf(np->device, "using %s firmware.\n", np->fw_name);
if (np->features & FE_NOPM)
device_printf(np->device,
"handling phase mismatch from SCRIPTS.\n");
}
if (sym_verbose > 1) {
device_printf(np->device,
"initial SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
"(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
np->sv_scntl3, np->sv_dmode, np->sv_dcntl, np->sv_ctest3,
np->sv_ctest4, np->sv_ctest5);
device_printf(np->device,
"final SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
"(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
np->rv_scntl3, np->rv_dmode, np->rv_dcntl,
np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
}
sym_print_targets_flag(np, SYM_SCAN_BOOT_DISABLED, "SCAN AT BOOT");
if (sym_verbose)
sym_print_targets_flag(np, SYM_SCAN_LUNS_DISABLED,
"SCAN FOR LUNS");
return 0;
}
static int sym_prepare_nego(hcb_p np, ccb_p cp, int nego, u_char *msgptr)
{
tcb_p tp = &np->target[cp->target];
int msglen = 0;
if (!(np->features & FE_U3EN))
tp->tinfo.goal.options = 0;
if (tp->tinfo.goal.options & PPR_OPT_MASK)
nego = NS_PPR;
else if (tp->tinfo.current.width != tp->tinfo.goal.width)
nego = NS_WIDE;
else if (tp->tinfo.current.period != tp->tinfo.goal.period ||
tp->tinfo.current.offset != tp->tinfo.goal.offset)
nego = NS_SYNC;
switch (nego) {
case NS_SYNC:
msgptr[msglen++] = M_EXTENDED;
msgptr[msglen++] = 3;
msgptr[msglen++] = M_X_SYNC_REQ;
msgptr[msglen++] = tp->tinfo.goal.period;
msgptr[msglen++] = tp->tinfo.goal.offset;
break;
case NS_WIDE:
msgptr[msglen++] = M_EXTENDED;
msgptr[msglen++] = 2;
msgptr[msglen++] = M_X_WIDE_REQ;
msgptr[msglen++] = tp->tinfo.goal.width;
break;
case NS_PPR:
msgptr[msglen++] = M_EXTENDED;
msgptr[msglen++] = 6;
msgptr[msglen++] = M_X_PPR_REQ;
msgptr[msglen++] = tp->tinfo.goal.period;
msgptr[msglen++] = 0;
msgptr[msglen++] = tp->tinfo.goal.offset;
msgptr[msglen++] = tp->tinfo.goal.width;
msgptr[msglen++] = tp->tinfo.goal.options & PPR_OPT_DT;
break;
}
cp->nego_status = nego;
if (nego) {
tp->nego_cp = cp;
if (DEBUG_FLAGS & DEBUG_NEGO) {
sym_print_msg(cp, nego == NS_SYNC ? "sync msgout" :
nego == NS_WIDE ? "wide msgout" :
"ppr msgout", msgptr);
}
}
return msglen;
}
static void sym_put_start_queue(hcb_p np, ccb_p cp)
{
u_short qidx;
#ifdef SYM_CONF_IARB_SUPPORT
if (np->last_cp && np->iarb_count < np->iarb_max) {
np->last_cp->host_flags |= HF_HINT_IARB;
++np->iarb_count;
}
else
np->iarb_count = 0;
np->last_cp = cp;
#endif
qidx = np->squeueput + 2;
if (qidx >= MAX_QUEUE*2) qidx = 0;
np->squeue [qidx] = cpu_to_scr(np->idletask_ba);
MEMORY_BARRIER();
np->squeue [np->squeueput] = cpu_to_scr(cp->ccb_ba);
np->squeueput = qidx;
if (DEBUG_FLAGS & DEBUG_QUEUE)
device_printf(np->device, "queuepos=%d.\n", np->squeueput);
MEMORY_BARRIER();
OUTB (nc_istat, SIGP|np->istat_sem);
}
static void sym_chip_reset (hcb_p np)
{
OUTB (nc_istat, SRST);
UDELAY (10);
OUTB (nc_istat, 0);
UDELAY(2000);
}
static void sym_soft_reset (hcb_p np)
{
u_char istat;
int i;
OUTB (nc_istat, CABRT);
for (i = 1000000 ; i ; --i) {
istat = INB (nc_istat);
if (istat & SIP) {
INW (nc_sist);
continue;
}
if (istat & DIP) {
OUTB (nc_istat, 0);
INB (nc_dstat);
break;
}
}
if (!i)
device_printf(np->device,
"unable to abort current chip operation.\n");
sym_chip_reset (np);
}
static void sym_start_reset(hcb_p np)
{
(void) sym_reset_scsi_bus(np, 1);
}
static int sym_reset_scsi_bus(hcb_p np, int enab_int)
{
u32 term;
int retv = 0;
sym_soft_reset(np);
if (enab_int)
OUTW (nc_sien, RST);
OUTB (nc_stest3, TE);
OUTB (nc_dcntl, (np->rv_dcntl & IRQM));
OUTB (nc_scntl1, CRST);
UDELAY (200);
if (!SYM_SETUP_SCSI_BUS_CHECK)
goto out;
term = INB(nc_sstat0);
term = ((term & 2) << 7) + ((term & 1) << 17);
term |= ((INB(nc_sstat2) & 0x01) << 26) |
((INW(nc_sbdl) & 0xff) << 9) |
((INW(nc_sbdl) & 0xff00) << 10) |
INB(nc_sbcl);
if (!(np->features & FE_WIDE))
term &= 0x3ffff;
if (term != (2<<7)) {
device_printf(np->device,
"suspicious SCSI data while resetting the BUS.\n");
device_printf(np->device,
"%sdp0,d7-0,rst,req,ack,bsy,sel,atn,msg,c/d,i/o = "
"0x%lx, expecting 0x%lx\n", (np->features & FE_WIDE) ?
"dp1,d15-8," : "", (u_long)term, (u_long)(2 << 7));
if (SYM_SETUP_SCSI_BUS_CHECK == 1)
retv = 1;
}
out:
OUTB (nc_scntl1, 0);
return retv;
}
static int sym_wakeup_done (hcb_p np)
{
ccb_p cp;
int i, n;
u32 dsa;
SYM_LOCK_ASSERT(MA_OWNED);
n = 0;
i = np->dqueueget;
while (1) {
dsa = scr_to_cpu(np->dqueue[i]);
if (!dsa)
break;
np->dqueue[i] = 0;
if ((i = i+2) >= MAX_QUEUE*2)
i = 0;
cp = sym_ccb_from_dsa(np, dsa);
if (cp) {
MEMORY_BARRIER();
sym_complete_ok (np, cp);
++n;
} else
device_printf(np->device,
"bad DSA (%x) in done queue.\n", (u_int)dsa);
}
np->dqueueget = i;
return n;
}
static void sym_flush_busy_queue (hcb_p np, int cam_status)
{
sym_que_splice(&np->busy_ccbq, &np->comp_ccbq);
sym_que_init(&np->busy_ccbq);
sym_flush_comp_queue(np, cam_status);
}
static void sym_init (hcb_p np, int reason)
{
int i;
u32 phys;
SYM_LOCK_ASSERT(MA_OWNED);
if (reason == 1)
sym_soft_reset(np);
else {
OUTB (nc_stest3, TE|CSF);
OUTONB (nc_ctest3, CLF);
}
phys = np->squeue_ba;
for (i = 0; i < MAX_QUEUE*2; i += 2) {
np->squeue[i] = cpu_to_scr(np->idletask_ba);
np->squeue[i+1] = cpu_to_scr(phys + (i+2)*4);
}
np->squeue[MAX_QUEUE*2-1] = cpu_to_scr(phys);
np->squeueput = 0;
phys = np->dqueue_ba;
for (i = 0; i < MAX_QUEUE*2; i += 2) {
np->dqueue[i] = 0;
np->dqueue[i+1] = cpu_to_scr(phys + (i+2)*4);
}
np->dqueue[MAX_QUEUE*2-1] = cpu_to_scr(phys);
np->dqueueget = 0;
np->fw_patch(np);
sym_flush_busy_queue(np, CAM_SCSI_BUS_RESET);
OUTB (nc_istat, 0x00 );
UDELAY (2000);
OUTB (nc_scntl0, np->rv_scntl0 | 0xc0);
OUTB (nc_scntl1, 0x00);
sym_selectclock(np, np->rv_scntl3);
OUTB (nc_scid , RRE|np->myaddr);
OUTW (nc_respid, 1ul<<np->myaddr);
OUTB (nc_istat , SIGP );
OUTB (nc_dmode , np->rv_dmode);
OUTB (nc_ctest5, np->rv_ctest5);
OUTB (nc_dcntl , NOCOM|np->rv_dcntl);
OUTB (nc_ctest3, np->rv_ctest3);
OUTB (nc_ctest4, np->rv_ctest4);
if (np->features & FE_C10)
OUTB (nc_stest2, np->rv_stest2);
else
OUTB (nc_stest2, EXT|np->rv_stest2);
OUTB (nc_stest3, TE);
OUTB (nc_stime0, 0x0c);
if (np->device_id == PCI_ID_LSI53C1010_2)
OUTB (nc_aipcntl1, DISAIP);
if (np->device_id == PCI_ID_LSI53C1010 &&
1)
OUTB (nc_stest1, INB(nc_stest1) | 0x30);
if (np->device_id == PCI_ID_SYM53C875)
OUTB (nc_ctest0, (1<<5));
else if (np->device_id == PCI_ID_SYM53C896)
np->rv_ccntl0 |= DPR;
if (np->features & (FE_DAC|FE_NOPM)) {
OUTB (nc_ccntl0, np->rv_ccntl0);
OUTB (nc_ccntl1, np->rv_ccntl1);
}
if (np->features & FE_NOPM) {
OUTL (nc_pmjad1, SCRIPTB_BA (np, pm_handle));
OUTL (nc_pmjad2, SCRIPTB_BA (np, pm_handle));
}
if (np->features & FE_LED0)
OUTB(nc_gpcntl, INB(nc_gpcntl) & ~0x01);
else if (np->features & FE_LEDC)
OUTB(nc_gpcntl, (INB(nc_gpcntl) & ~0x41) | 0x20);
OUTW (nc_sien , STO|HTH|MA|SGE|UDC|RST|PAR);
OUTB (nc_dien , MDPE|BF|SSI|SIR|IID);
if (np->features & (FE_ULTRA2|FE_ULTRA3)) {
OUTONW (nc_sien, SBMC);
if (reason == 0) {
MDELAY(100);
INW (nc_sist);
}
np->scsi_mode = INB (nc_stest4) & SMODE;
}
for (i = 0; i < SYM_CONF_MAX_TARGET; i++) {
tcb_p tp = &np->target[i];
tp->to_reset = 0;
tp->head.sval = 0;
tp->head.wval = np->rv_scntl3;
tp->head.uval = 0;
tp->tinfo.current.period = 0;
tp->tinfo.current.offset = 0;
tp->tinfo.current.width = BUS_8_BIT;
tp->tinfo.current.options = 0;
}
if (np->ram_ba) {
if (sym_verbose > 1)
device_printf(np->device,
"Downloading SCSI SCRIPTS.\n");
if (np->ram_ws == 8192) {
OUTRAM_OFF(4096, np->scriptb0, np->scriptb_sz);
OUTL (nc_mmws, np->scr_ram_seg);
OUTL (nc_mmrs, np->scr_ram_seg);
OUTL (nc_sfs, np->scr_ram_seg);
phys = SCRIPTB_BA (np, start64);
}
else
phys = SCRIPTA_BA (np, init);
OUTRAM_OFF(0, np->scripta0, np->scripta_sz);
}
else
phys = SCRIPTA_BA (np, init);
np->istat_sem = 0;
OUTL (nc_dsa, np->hcb_ba);
OUTL_DSP (phys);
if (reason != 0)
xpt_async(AC_BUS_RESET, np->path, NULL);
}
static int
sym_getsync(hcb_p np, u_char dt, u_char sfac, u_char *divp, u_char *fakp)
{
u32 clk = np->clock_khz;
int div = np->clock_divn;
u32 fak;
u32 per;
u32 kpc;
int ret;
if (dt && sfac <= 9) per = 125;
else if (sfac <= 10) per = 250;
else if (sfac == 11) per = 303;
else if (sfac == 12) per = 500;
else per = 40 * sfac;
ret = per;
kpc = per * clk;
if (dt)
kpc <<= 1;
#if 1
if ((np->features & (FE_C10|FE_U3EN)) == FE_C10) {
while (div > 0) {
--div;
if (kpc > (div_10M[div] << 2)) {
++div;
break;
}
}
fak = 0;
if (div == np->clock_divn) {
ret = -1;
}
*divp = div;
*fakp = fak;
return ret;
}
#endif
while (div-- > 0)
if (kpc >= (div_10M[div] << 2)) break;
if (dt) {
fak = (kpc - 1) / (div_10M[div] << 1) + 1 - 2;
}
else {
fak = (kpc - 1) / div_10M[div] + 1 - 4;
}
if (fak > 2) {fak = 2; ret = -1;}
*divp = div;
*fakp = fak;
return ret;
}
static void
sym_xpt_async_transfer_neg(hcb_p np, int target, u_int spi_valid)
{
struct ccb_trans_settings cts;
struct cam_path *path;
int sts;
tcb_p tp = &np->target[target];
sts = xpt_create_path(&path, NULL, cam_sim_path(np->sim), target,
CAM_LUN_WILDCARD);
if (sts != CAM_REQ_CMP)
return;
bzero(&cts, sizeof(cts));
#define cts__scsi (cts.proto_specific.scsi)
#define cts__spi (cts.xport_specific.spi)
cts.type = CTS_TYPE_CURRENT_SETTINGS;
cts.protocol = PROTO_SCSI;
cts.transport = XPORT_SPI;
cts.protocol_version = tp->tinfo.current.scsi_version;
cts.transport_version = tp->tinfo.current.spi_version;
cts__spi.valid = spi_valid;
if (spi_valid & CTS_SPI_VALID_SYNC_RATE)
cts__spi.sync_period = tp->tinfo.current.period;
if (spi_valid & CTS_SPI_VALID_SYNC_OFFSET)
cts__spi.sync_offset = tp->tinfo.current.offset;
if (spi_valid & CTS_SPI_VALID_BUS_WIDTH)
cts__spi.bus_width = tp->tinfo.current.width;
if (spi_valid & CTS_SPI_VALID_PPR_OPTIONS)
cts__spi.ppr_options = tp->tinfo.current.options;
#undef cts__spi
#undef cts__scsi
xpt_setup_ccb(&cts.ccb_h, path, 1);
xpt_async(AC_TRANSFER_NEG, path, &cts);
xpt_free_path(path);
}
#define SYM_SPI_VALID_WDTR \
CTS_SPI_VALID_BUS_WIDTH | \
CTS_SPI_VALID_SYNC_RATE | \
CTS_SPI_VALID_SYNC_OFFSET
#define SYM_SPI_VALID_SDTR \
CTS_SPI_VALID_SYNC_RATE | \
CTS_SPI_VALID_SYNC_OFFSET
#define SYM_SPI_VALID_PPR \
CTS_SPI_VALID_PPR_OPTIONS | \
CTS_SPI_VALID_BUS_WIDTH | \
CTS_SPI_VALID_SYNC_RATE | \
CTS_SPI_VALID_SYNC_OFFSET
static void sym_setwide(hcb_p np, ccb_p cp, u_char wide)
{
tcb_p tp = &np->target[cp->target];
sym_settrans(np, cp, 0, 0, 0, wide, 0, 0);
tp->tinfo.goal.width = tp->tinfo.current.width = wide;
tp->tinfo.current.offset = 0;
tp->tinfo.current.period = 0;
tp->tinfo.current.options = 0;
sym_xpt_async_transfer_neg(np, cp->target, SYM_SPI_VALID_WDTR);
}
static void
sym_setsync(hcb_p np, ccb_p cp, u_char ofs, u_char per, u_char div, u_char fak)
{
tcb_p tp = &np->target[cp->target];
u_char wide = (cp->phys.select.sel_scntl3 & EWS) ? 1 : 0;
sym_settrans(np, cp, 0, ofs, per, wide, div, fak);
tp->tinfo.goal.period = tp->tinfo.current.period = per;
tp->tinfo.goal.offset = tp->tinfo.current.offset = ofs;
tp->tinfo.goal.options = tp->tinfo.current.options = 0;
sym_xpt_async_transfer_neg(np, cp->target, SYM_SPI_VALID_SDTR);
}
static void sym_setpprot(hcb_p np, ccb_p cp, u_char dt, u_char ofs,
u_char per, u_char wide, u_char div, u_char fak)
{
tcb_p tp = &np->target[cp->target];
sym_settrans(np, cp, dt, ofs, per, wide, div, fak);
tp->tinfo.goal.width = tp->tinfo.current.width = wide;
tp->tinfo.goal.period = tp->tinfo.current.period = per;
tp->tinfo.goal.offset = tp->tinfo.current.offset = ofs;
tp->tinfo.goal.options = tp->tinfo.current.options = dt;
sym_xpt_async_transfer_neg(np, cp->target, SYM_SPI_VALID_PPR);
}
static void sym_settrans(hcb_p np, ccb_p cp, u_char dt, u_char ofs,
u_char per, u_char wide, u_char div, u_char fak)
{
SYM_QUEHEAD *qp;
union ccb *ccb;
tcb_p tp;
u_char target = INB (nc_sdid) & 0x0f;
u_char sval, wval, uval;
assert (cp);
if (!cp) return;
ccb = cp->cam_ccb;
assert (ccb);
if (!ccb) return;
assert (target == (cp->target & 0xf));
tp = &np->target[target];
sval = tp->head.sval;
wval = tp->head.wval;
uval = tp->head.uval;
#if 0
printf("XXXX sval=%x wval=%x uval=%x (%x)\n",
sval, wval, uval, np->rv_scntl3);
#endif
if (!(np->features & FE_C10))
sval = (sval & ~0x1f) | ofs;
else
sval = (sval & ~0x3f) | ofs;
if (ofs != 0) {
wval = (wval & ~0x70) | ((div+1) << 4);
if (!(np->features & FE_C10))
sval = (sval & ~0xe0) | (fak << 5);
else {
uval = uval & ~(XCLKH_ST|XCLKH_DT|XCLKS_ST|XCLKS_DT);
if (fak >= 1) uval |= (XCLKH_ST|XCLKH_DT);
if (fak >= 2) uval |= (XCLKS_ST|XCLKS_DT);
}
}
wval = wval & ~EWS;
if (wide != 0)
wval |= EWS;
if (np->features & FE_C10) {
uval = uval & ~(U3EN|AIPCKEN);
if (dt) {
assert(np->features & FE_U3EN);
uval |= U3EN;
}
}
else {
wval = wval & ~ULTRA;
if (per <= 12) wval |= ULTRA;
}
if (tp->head.sval == sval &&
tp->head.wval == wval &&
tp->head.uval == uval)
return;
tp->head.sval = sval;
tp->head.wval = wval;
tp->head.uval = uval;
if (per < 50 && !(np->features & FE_C10))
OUTOFFB (nc_stest2, EXT);
OUTB (nc_sxfer, tp->head.sval);
OUTB (nc_scntl3, tp->head.wval);
if (np->features & FE_C10) {
OUTB (nc_scntl4, tp->head.uval);
}
FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
if (cp->target != target)
continue;
cp->phys.select.sel_scntl3 = tp->head.wval;
cp->phys.select.sel_sxfer = tp->head.sval;
if (np->features & FE_C10) {
cp->phys.select.sel_scntl4 = tp->head.uval;
}
}
}
static void sym_log_hard_error(hcb_p np, u_short sist, u_char dstat)
{
u32 dsp;
int script_ofs;
int script_size;
char *script_name;
u_char *script_base;
int i;
dsp = INL (nc_dsp);
if (dsp > np->scripta_ba &&
dsp <= np->scripta_ba + np->scripta_sz) {
script_ofs = dsp - np->scripta_ba;
script_size = np->scripta_sz;
script_base = (u_char *) np->scripta0;
script_name = "scripta";
}
else if (np->scriptb_ba < dsp &&
dsp <= np->scriptb_ba + np->scriptb_sz) {
script_ofs = dsp - np->scriptb_ba;
script_size = np->scriptb_sz;
script_base = (u_char *) np->scriptb0;
script_name = "scriptb";
} else {
script_ofs = dsp;
script_size = 0;
script_base = NULL;
script_name = "mem";
}
printf ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x) @ (%s %x:%08x).\n",
sym_name (np), (unsigned)INB (nc_sdid)&0x0f, dstat, sist,
(unsigned)INB (nc_socl), (unsigned)INB (nc_sbcl),
(unsigned)INB (nc_sbdl), (unsigned)INB (nc_sxfer),
(unsigned)INB (nc_scntl3), script_name, script_ofs,
(unsigned)INL (nc_dbc));
if (((script_ofs & 3) == 0) &&
(unsigned)script_ofs < script_size) {
device_printf(np->device, "script cmd = %08x\n",
scr_to_cpu((int) *(u32 *)(script_base + script_ofs)));
}
device_printf(np->device, "regdump:");
for (i = 0; i < 24; i++)
printf (" %02x", (unsigned)INB_OFF(i));
printf (".\n");
if (dstat & (MDPE|BF)) {
u_short pci_sts;
pci_sts = pci_read_config(np->device, PCIR_STATUS, 2);
if (pci_sts & 0xf900) {
pci_write_config(np->device, PCIR_STATUS, pci_sts, 2);
device_printf(np->device, "PCI STATUS = 0x%04x\n",
pci_sts & 0xf900);
}
}
}
static void sym_intr1 (hcb_p np)
{
u_char istat, istatc;
u_char dstat;
u_short sist;
SYM_LOCK_ASSERT(MA_OWNED);
istat = INB (nc_istat);
if (istat & INTF) {
OUTB (nc_istat, (istat & SIGP) | INTF | np->istat_sem);
istat = INB (nc_istat);
if (DEBUG_FLAGS & DEBUG_TINY) printf ("F ");
(void)sym_wakeup_done (np);
}
if (!(istat & (SIP|DIP)))
return;
#if 0
if (istat & CABRT)
OUTB (nc_istat, CABRT);
#endif
sist = 0;
dstat = 0;
istatc = istat;
do {
if (istatc & SIP)
sist |= INW (nc_sist);
if (istatc & DIP)
dstat |= INB (nc_dstat);
istatc = INB (nc_istat);
istat |= istatc;
} while (istatc & (SIP|DIP));
if (DEBUG_FLAGS & DEBUG_TINY)
printf ("<%d|%x:%x|%x:%x>",
(int)INB(nc_scr0),
dstat,sist,
(unsigned)INL(nc_dsp),
(unsigned)INL(nc_dbc));
MEMORY_BARRIER();
if (!(sist & (STO|GEN|HTH|SGE|UDC|SBMC|RST)) &&
!(dstat & (MDPE|BF|ABRT|IID))) {
if (sist & PAR) sym_int_par (np, sist);
else if (sist & MA) sym_int_ma (np);
else if (dstat & SIR) sym_int_sir (np);
else if (dstat & SSI) OUTONB_STD ();
else goto unknown_int;
return;
}
if (sist & RST) {
xpt_print_path(np->path);
printf("SCSI BUS reset detected.\n");
sym_init (np, 1);
return;
}
OUTB (nc_ctest3, np->rv_ctest3 | CLF);
OUTB (nc_stest3, TE|CSF);
if (!(sist & (GEN|HTH|SGE)) &&
!(dstat & (MDPE|BF|ABRT|IID))) {
if (sist & SBMC) sym_int_sbmc (np);
else if (sist & STO) sym_int_sto (np);
else if (sist & UDC) sym_int_udc (np);
else goto unknown_int;
return;
}
sym_log_hard_error(np, sist, dstat);
if ((sist & (GEN|HTH|SGE)) ||
(dstat & (MDPE|BF|ABRT|IID))) {
sym_start_reset(np);
return;
}
unknown_int:
device_printf(np->device,
"unknown interrupt(s) ignored, ISTAT=0x%x DSTAT=0x%x SIST=0x%x\n",
istat, dstat, sist);
}
static void sym_intr(void *arg)
{
hcb_p np = arg;
SYM_LOCK();
if (DEBUG_FLAGS & DEBUG_TINY) printf ("[");
sym_intr1((hcb_p) arg);
if (DEBUG_FLAGS & DEBUG_TINY) printf ("]");
SYM_UNLOCK();
}
static void sym_poll(struct cam_sim *sim)
{
sym_intr1(cam_sim_softc(sim));
}
static void sym_recover_scsi_int (hcb_p np, u_char hsts)
{
u32 dsp = INL (nc_dsp);
u32 dsa = INL (nc_dsa);
ccb_p cp = sym_ccb_from_dsa(np, dsa);
if ((!(dsp > SCRIPTA_BA (np, getjob_begin) &&
dsp < SCRIPTA_BA (np, getjob_end) + 1)) &&
(!(dsp > SCRIPTA_BA (np, ungetjob) &&
dsp < SCRIPTA_BA (np, reselect) + 1)) &&
(!(dsp > SCRIPTB_BA (np, sel_for_abort) &&
dsp < SCRIPTB_BA (np, sel_for_abort_1) + 1)) &&
(!(dsp > SCRIPTA_BA (np, done) &&
dsp < SCRIPTA_BA (np, done_end) + 1))) {
OUTB (nc_ctest3, np->rv_ctest3 | CLF);
OUTB (nc_stest3, TE|CSF);
if (cp) {
cp->host_status = hsts;
OUTL_DSP (SCRIPTA_BA (np, complete_error));
}
else {
OUTL (nc_dsa, 0xffffff);
OUTL_DSP (SCRIPTA_BA (np, start));
}
}
else
goto reset_all;
return;
reset_all:
sym_start_reset(np);
}
static void sym_int_sto (hcb_p np)
{
u32 dsp = INL (nc_dsp);
if (DEBUG_FLAGS & DEBUG_TINY) printf ("T");
if (dsp == SCRIPTA_BA (np, wf_sel_done) + 8)
sym_recover_scsi_int(np, HS_SEL_TIMEOUT);
else
sym_start_reset(np);
}
static void sym_int_udc (hcb_p np)
{
device_printf(np->device, "unexpected disconnect\n");
sym_recover_scsi_int(np, HS_UNEXPECTED);
}
static void sym_int_sbmc (hcb_p np)
{
u_char scsi_mode = INB (nc_stest4) & SMODE;
xpt_print_path(np->path);
printf("SCSI BUS mode change from %s to %s.\n",
sym_scsi_bus_mode(np->scsi_mode), sym_scsi_bus_mode(scsi_mode));
sym_init (np, 2);
}
static void sym_int_par (hcb_p np, u_short sist)
{
u_char hsts = INB (HS_PRT);
u32 dsp = INL (nc_dsp);
u32 dbc = INL (nc_dbc);
u32 dsa = INL (nc_dsa);
u_char sbcl = INB (nc_sbcl);
u_char cmd = dbc >> 24;
int phase = cmd & 7;
ccb_p cp = sym_ccb_from_dsa(np, dsa);
device_printf(np->device,
"SCSI parity error detected: SCR1=%d DBC=%x SBCL=%x\n", hsts, dbc,
sbcl);
if (!(INB (nc_scntl1) & ISCON)) {
sym_recover_scsi_int(np, HS_UNEXPECTED);
return;
}
if (!cp)
goto reset_all;
if ((cmd & 0xc0) || !(phase & 1) || !(sbcl & 0x8))
goto reset_all;
OUTONB (HF_PRT, HF_EXT_ERR);
cp->xerr_status |= XE_PARITY_ERR;
np->msgout[0] = (phase == 7) ? M_PARITY : M_ID_ERROR;
if (phase == 1 || phase == 5) {
if (dsp == SCRIPTB_BA (np, pm_handle))
OUTL_DSP (dsp);
else if (sist & MA)
sym_int_ma (np);
else {
OUTL (nc_temp, dsp);
OUTL_DSP (SCRIPTA_BA (np, dispatch));
}
}
else
OUTL_DSP (SCRIPTA_BA (np, clrack));
return;
reset_all:
sym_start_reset(np);
}
static void sym_int_ma (hcb_p np)
{
u32 dbc;
u32 rest;
u32 dsp;
u32 dsa;
u32 nxtdsp;
u32 *vdsp;
u32 oadr, olen;
u32 *tblp;
u32 newcmd;
u_int delta;
u_char cmd;
u_char hflags, hflags0;
struct sym_pmc *pm;
ccb_p cp;
dsp = INL (nc_dsp);
dbc = INL (nc_dbc);
dsa = INL (nc_dsa);
cmd = dbc >> 24;
rest = dbc & 0xffffff;
delta = 0;
cp = sym_ccb_from_dsa(np, dsa);
if ((cmd & 7) != 1 && (cmd & 7) != 5) {
u_char ss0, ss2;
if (np->features & FE_DFBC)
delta = INW (nc_dfbc);
else {
u32 dfifo;
dfifo = INL(nc_dfifo);
if (dfifo & (DFS << 16))
delta = ((((dfifo >> 8) & 0x300) |
(dfifo & 0xff)) - rest) & 0x3ff;
else
delta = ((dfifo & 0xff) - rest) & 0x7f;
}
rest += delta;
ss0 = INB (nc_sstat0);
if (ss0 & OLF) rest++;
if (!(np->features & FE_C10))
if (ss0 & ORF) rest++;
if (cp && (cp->phys.select.sel_scntl3 & EWS)) {
ss2 = INB (nc_sstat2);
if (ss2 & OLF1) rest++;
if (!(np->features & FE_C10))
if (ss2 & ORF1) rest++;
}
OUTB (nc_ctest3, np->rv_ctest3 | CLF);
OUTB (nc_stest3, TE|CSF);
}
if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
printf ("P%x%x RL=%d D=%d ", cmd&7, INB(nc_sbcl)&7,
(unsigned) rest, (unsigned) delta);
vdsp = NULL;
nxtdsp = 0;
if (dsp > np->scripta_ba &&
dsp <= np->scripta_ba + np->scripta_sz) {
vdsp = (u32 *)((char*)np->scripta0 + (dsp-np->scripta_ba-8));
nxtdsp = dsp;
}
else if (dsp > np->scriptb_ba &&
dsp <= np->scriptb_ba + np->scriptb_sz) {
vdsp = (u32 *)((char*)np->scriptb0 + (dsp-np->scriptb_ba-8));
nxtdsp = dsp;
}
if (DEBUG_FLAGS & DEBUG_PHASE) {
printf ("\nCP=%p DSP=%x NXT=%x VDSP=%p CMD=%x ",
cp, (unsigned)dsp, (unsigned)nxtdsp, vdsp, cmd);
}
if (!vdsp) {
device_printf(np->device,
"interrupted SCRIPT address not found.\n");
goto reset_all;
}
if (!cp) {
device_printf(np->device,
"SCSI phase error fixup: CCB already dequeued.\n");
goto reset_all;
}
oadr = scr_to_cpu(vdsp[1]);
if (cmd & 0x10) {
tblp = (u32 *) ((char*) &cp->phys + oadr);
olen = scr_to_cpu(tblp[0]);
oadr = scr_to_cpu(tblp[1]);
} else {
tblp = (u32 *) 0;
olen = scr_to_cpu(vdsp[0]) & 0xffffff;
}
if (DEBUG_FLAGS & DEBUG_PHASE) {
printf ("OCMD=%x\nTBLP=%p OLEN=%x OADR=%x\n",
(unsigned) (scr_to_cpu(vdsp[0]) >> 24),
tblp,
(unsigned) olen,
(unsigned) oadr);
}
if (((cmd & 2) ? cmd : (cmd & ~4)) != (scr_to_cpu(vdsp[0]) >> 24)) {
PRINT_ADDR(cp);
printf ("internal error: cmd=%02x != %02x=(vdsp[0] >> 24)\n",
(unsigned)cmd, (unsigned)scr_to_cpu(vdsp[0]) >> 24);
goto reset_all;
}
if (cmd & 2) {
PRINT_ADDR(cp);
printf ("phase change %x-%x %d@%08x resid=%d.\n",
cmd&7, INB(nc_sbcl)&7, (unsigned)olen,
(unsigned)oadr, (unsigned)rest);
goto unexpected_phase;
}
hflags0 = INB (HF_PRT);
hflags = hflags0;
if (hflags & (HF_IN_PM0 | HF_IN_PM1 | HF_DP_SAVED)) {
if (hflags & HF_IN_PM0)
nxtdsp = scr_to_cpu(cp->phys.pm0.ret);
else if (hflags & HF_IN_PM1)
nxtdsp = scr_to_cpu(cp->phys.pm1.ret);
if (hflags & HF_DP_SAVED)
hflags ^= HF_ACT_PM;
}
if (!(hflags & HF_ACT_PM)) {
pm = &cp->phys.pm0;
newcmd = SCRIPTA_BA (np, pm0_data);
}
else {
pm = &cp->phys.pm1;
newcmd = SCRIPTA_BA (np, pm1_data);
}
hflags &= ~(HF_IN_PM0 | HF_IN_PM1 | HF_DP_SAVED);
if (hflags != hflags0)
OUTB (HF_PRT, hflags);
pm->sg.addr = cpu_to_scr(oadr + olen - rest);
pm->sg.size = cpu_to_scr(rest);
pm->ret = cpu_to_scr(nxtdsp);
nxtdsp = SCRIPTA_BA (np, dispatch);
if ((cmd & 7) == 1 && cp && (cp->phys.select.sel_scntl3 & EWS) &&
(INB (nc_scntl2) & WSR)) {
u32 tmp;
tmp = scr_to_cpu(pm->sg.addr);
cp->phys.wresid.addr = cpu_to_scr(tmp);
pm->sg.addr = cpu_to_scr(tmp + 1);
tmp = scr_to_cpu(pm->sg.size);
cp->phys.wresid.size = cpu_to_scr((tmp&0xff000000) | 1);
pm->sg.size = cpu_to_scr(tmp - 1);
if ((tmp&0xffffff) == 1)
newcmd = pm->ret;
nxtdsp = SCRIPTB_BA (np, wsr_ma_helper);
}
if (DEBUG_FLAGS & DEBUG_PHASE) {
PRINT_ADDR(cp);
printf ("PM %x %x %x / %x %x %x.\n",
hflags0, hflags, newcmd,
(unsigned)scr_to_cpu(pm->sg.addr),
(unsigned)scr_to_cpu(pm->sg.size),
(unsigned)scr_to_cpu(pm->ret));
}
OUTL (nc_temp, newcmd);
OUTL_DSP (nxtdsp);
return;
unexpected_phase:
dsp -= 8;
nxtdsp = 0;
switch (cmd & 7) {
case 2:
nxtdsp = SCRIPTA_BA (np, dispatch);
break;
#if 0
case 3:
nxtdsp = SCRIPTA_BA (np, dispatch);
break;
#endif
case 6:
if (dsp == SCRIPTA_BA (np, send_ident)) {
if (cp->tag != NO_TAG && olen - rest <= 3) {
cp->host_status = HS_BUSY;
np->msgout[0] = M_IDENTIFY | cp->lun;
nxtdsp = SCRIPTB_BA (np, ident_break_atn);
}
else
nxtdsp = SCRIPTB_BA (np, ident_break);
}
else if (dsp == SCRIPTB_BA (np, send_wdtr) ||
dsp == SCRIPTB_BA (np, send_sdtr) ||
dsp == SCRIPTB_BA (np, send_ppr)) {
nxtdsp = SCRIPTB_BA (np, nego_bad_phase);
}
break;
#if 0
case 7:
nxtdsp = SCRIPTA_BA (np, clrack);
break;
#endif
}
if (nxtdsp) {
OUTL_DSP (nxtdsp);
return;
}
reset_all:
sym_start_reset(np);
}
static int
sym_dequeue_from_squeue(hcb_p np, int i, int target, int lun, int task)
{
int j;
ccb_p cp;
assert((i >= 0) && (i < 2*MAX_QUEUE));
j = i;
while (i != np->squeueput) {
cp = sym_ccb_from_dsa(np, scr_to_cpu(np->squeue[i]));
assert(cp);
#ifdef SYM_CONF_IARB_SUPPORT
cp->host_flags &= ~HF_HINT_IARB;
#endif
if ((target == -1 || cp->target == target) &&
(lun == -1 || cp->lun == lun) &&
(task == -1 || cp->tag == task)) {
sym_set_cam_status(cp->cam_ccb, CAM_REQUEUE_REQ);
sym_remque(&cp->link_ccbq);
sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
}
else {
if (i != j)
np->squeue[j] = np->squeue[i];
if ((j += 2) >= MAX_QUEUE*2) j = 0;
}
if ((i += 2) >= MAX_QUEUE*2) i = 0;
}
if (i != j)
np->squeue[j] = np->squeue[i];
np->squeueput = j;
return (i - j) / 2;
}
static void
sym_flush_comp_queue(hcb_p np, int cam_status)
{
SYM_QUEHEAD *qp;
ccb_p cp;
while ((qp = sym_remque_head(&np->comp_ccbq)) != NULL) {
union ccb *ccb;
cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
if (cp->host_status == HS_WAIT)
continue;
ccb = cp->cam_ccb;
if (cam_status)
sym_set_cam_status(ccb, cam_status);
sym_freeze_cam_ccb(ccb);
sym_xpt_done(np, ccb, cp);
sym_free_ccb(np, cp);
}
}
static void sym_sir_bad_scsi_status(hcb_p np, ccb_p cp)
{
tcb_p tp = &np->target[cp->target];
u32 startp;
u_char s_status = cp->ssss_status;
u_char h_flags = cp->host_flags;
int msglen;
int nego;
int i;
SYM_LOCK_ASSERT(MA_OWNED);
i = (INL (nc_scratcha) - np->squeue_ba) / 4;
#ifdef SYM_CONF_IARB_SUPPORT
if (np->last_cp)
np->last_cp = NULL;
#endif
switch(s_status) {
case S_BUSY:
case S_QUEUE_FULL:
if (sym_verbose >= 2) {
PRINT_ADDR(cp);
printf (s_status == S_BUSY ? "BUSY" : "QUEUE FULL\n");
}
default:
sym_complete_error (np, cp);
break;
case S_TERMINATED:
case S_CHECK_COND:
if (h_flags & HF_SENSE) {
sym_complete_error (np, cp);
break;
}
(void) sym_dequeue_from_squeue(np, i, cp->target, cp->lun, -1);
OUTL_DSP (SCRIPTA_BA (np, start));
cp->sv_scsi_status = cp->ssss_status;
cp->sv_xerr_status = cp->xerr_status;
cp->sv_resid = sym_compute_residual(np, cp);
cp->scsi_smsg2[0] = M_IDENTIFY | cp->lun;
msglen = 1;
cp->nego_status = 0;
nego = 0;
if (tp->tinfo.current.options & PPR_OPT_MASK)
nego = NS_PPR;
else if (tp->tinfo.current.width != BUS_8_BIT)
nego = NS_WIDE;
else if (tp->tinfo.current.offset != 0)
nego = NS_SYNC;
if (nego)
msglen +=
sym_prepare_nego (np,cp, nego, &cp->scsi_smsg2[msglen]);
cp->phys.smsg.addr = cpu_to_scr(CCB_BA (cp, scsi_smsg2));
cp->phys.smsg.size = cpu_to_scr(msglen);
cp->phys.cmd.addr = cpu_to_scr(CCB_BA (cp, sensecmd));
cp->phys.cmd.size = cpu_to_scr(6);
cp->sensecmd[0] = 0x03;
cp->sensecmd[1] = cp->lun << 5;
if (tp->tinfo.current.scsi_version > 2 || cp->lun > 7)
cp->sensecmd[1] = 0;
cp->sensecmd[4] = SYM_SNS_BBUF_LEN;
cp->data_len = SYM_SNS_BBUF_LEN;
bzero(cp->sns_bbuf, SYM_SNS_BBUF_LEN);
cp->phys.sense.addr = cpu_to_scr(vtobus(cp->sns_bbuf));
cp->phys.sense.size = cpu_to_scr(SYM_SNS_BBUF_LEN);
startp = SCRIPTB_BA (np, sdata_in);
cp->phys.head.savep = cpu_to_scr(startp);
cp->phys.head.goalp = cpu_to_scr(startp + 16);
cp->phys.head.lastp = cpu_to_scr(startp);
cp->startp = cpu_to_scr(startp);
cp->actualquirks = SYM_QUIRK_AUTOSAVE;
cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
cp->ssss_status = S_ILLEGAL;
cp->host_flags = (HF_SENSE|HF_DATA_IN);
cp->xerr_status = 0;
cp->extra_bytes = 0;
cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA (np, select));
sym_put_start_queue(np, cp);
sym_flush_comp_queue(np, 0);
break;
}
}
static int
sym_clear_tasks(hcb_p np, int cam_status, int target, int lun, int task)
{
SYM_QUEHEAD qtmp, *qp;
int i = 0;
ccb_p cp;
sym_que_init(&qtmp);
sym_que_splice(&np->busy_ccbq, &qtmp);
sym_que_init(&np->busy_ccbq);
while ((qp = sym_remque_head(&qtmp)) != NULL) {
union ccb *ccb;
cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
ccb = cp->cam_ccb;
if (cp->host_status != HS_DISCONNECT ||
cp->target != target ||
(lun != -1 && cp->lun != lun) ||
(task != -1 &&
(cp->tag != NO_TAG && cp->scsi_smsg[2] != task))) {
sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
continue;
}
sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
if (sym_get_cam_status(ccb) != CAM_CMD_TIMEOUT)
sym_set_cam_status(ccb, cam_status);
++i;
#if 0
printf("XXXX TASK @%p CLEARED\n", cp);
#endif
}
return i;
}
static void sym_sir_task_recovery(hcb_p np, int num)
{
SYM_QUEHEAD *qp;
ccb_p cp;
tcb_p tp;
int target=-1, lun=-1, task;
int i, k;
switch(num) {
case SIR_SCRIPT_STOPPED:
for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
tp = &np->target[i];
if (tp->to_reset ||
(tp->lun0p && tp->lun0p->to_clear)) {
target = i;
break;
}
if (!tp->lunmp)
continue;
for (k = 1 ; k < SYM_CONF_MAX_LUN ; k++) {
if (tp->lunmp[k] && tp->lunmp[k]->to_clear) {
target = i;
break;
}
}
if (target != -1)
break;
}
if (target == -1) {
FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
cp = sym_que_entry(qp,struct sym_ccb,link_ccbq);
if (cp->host_status != HS_DISCONNECT)
continue;
if (cp->to_abort) {
target = cp->target;
break;
}
}
}
if (target != -1) {
tp = &np->target[target];
np->abrt_sel.sel_id = target;
np->abrt_sel.sel_scntl3 = tp->head.wval;
np->abrt_sel.sel_sxfer = tp->head.sval;
OUTL(nc_dsa, np->hcb_ba);
OUTL_DSP (SCRIPTB_BA (np, sel_for_abort));
return;
}
i = 0;
cp = NULL;
FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
if (cp->host_status != HS_BUSY &&
cp->host_status != HS_NEGOTIATE)
continue;
if (!cp->to_abort)
continue;
#ifdef SYM_CONF_IARB_SUPPORT
if (cp == np->last_cp) {
cp->to_abort = 0;
continue;
}
#endif
i = 1;
break;
}
if (!i) {
np->istat_sem = 0;
OUTB (nc_istat, SIGP);
break;
}
i = (INL (nc_scratcha) - np->squeue_ba) / 4;
i = sym_dequeue_from_squeue(np, i, cp->target, cp->lun, -1);
assert(i && sym_get_cam_status(cp->cam_ccb) == CAM_REQUEUE_REQ);
if (cp->to_abort == 2)
sym_set_cam_status(cp->cam_ccb, CAM_CMD_TIMEOUT);
else
sym_set_cam_status(cp->cam_ccb, CAM_REQ_ABORTED);
sym_flush_comp_queue(np, 0);
break;
case SIR_TARGET_SELECTED:
target = (INB (nc_sdid) & 0xf);
tp = &np->target[target];
np->abrt_tbl.addr = cpu_to_scr(vtobus(np->abrt_msg));
if (tp->to_reset) {
np->abrt_msg[0] = M_RESET;
np->abrt_tbl.size = 1;
tp->to_reset = 0;
break;
}
if (tp->lun0p && tp->lun0p->to_clear)
lun = 0;
else if (tp->lunmp) {
for (k = 1 ; k < SYM_CONF_MAX_LUN ; k++) {
if (tp->lunmp[k] && tp->lunmp[k]->to_clear) {
lun = k;
break;
}
}
}
if (lun != -1) {
lcb_p lp = sym_lp(tp, lun);
lp->to_clear = 0;
np->abrt_msg[0] = M_IDENTIFY | lun;
np->abrt_msg[1] = M_ABORT;
np->abrt_tbl.size = 2;
break;
}
i = 0;
cp = NULL;
FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
if (cp->host_status != HS_DISCONNECT)
continue;
if (cp->target != target)
continue;
if (!cp->to_abort)
continue;
i = 1;
break;
}
if (!i) {
np->abrt_msg[0] = M_ABORT;
np->abrt_tbl.size = 1;
break;
}
np->abrt_msg[0] = M_IDENTIFY | cp->lun;
if (cp->tag == NO_TAG) {
np->abrt_msg[1] = M_ABORT;
np->abrt_tbl.size = 2;
}
else {
np->abrt_msg[1] = cp->scsi_smsg[1];
np->abrt_msg[2] = cp->scsi_smsg[2];
np->abrt_msg[3] = M_ABORT_TAG;
np->abrt_tbl.size = 4;
}
if (cp->to_abort == 2)
sym_set_cam_status(cp->cam_ccb, CAM_CMD_TIMEOUT);
cp->to_abort = 0;
break;
case SIR_ABORT_SENT:
target = (INB (nc_sdid) & 0xf);
tp = &np->target[target];
if (np->abrt_msg[0] == M_ABORT)
break;
lun = -1;
task = -1;
if (np->abrt_msg[0] == M_RESET) {
tp->head.sval = 0;
tp->head.wval = np->rv_scntl3;
tp->head.uval = 0;
tp->tinfo.current.period = 0;
tp->tinfo.current.offset = 0;
tp->tinfo.current.width = BUS_8_BIT;
tp->tinfo.current.options = 0;
}
else {
lun = np->abrt_msg[0] & 0x3f;
if (np->abrt_msg[1] == M_ABORT_TAG)
task = np->abrt_msg[2];
}
i = (INL (nc_scratcha) - np->squeue_ba) / 4;
(void) sym_dequeue_from_squeue(np, i, target, lun, -1);
(void) sym_clear_tasks(np, CAM_REQ_ABORTED, target, lun, task);
sym_flush_comp_queue(np, 0);
if (np->abrt_msg[0] == M_RESET)
xpt_async(AC_SENT_BDR, np->path, NULL);
break;
}
if (num == SIR_TARGET_SELECTED) {
PRINT_TARGET(np, target);
sym_printl_hex("control msgout:", np->abrt_msg,
np->abrt_tbl.size);
np->abrt_tbl.size = cpu_to_scr(np->abrt_tbl.size);
}
OUTONB_STD ();
}
static int sym_evaluate_dp(hcb_p np, ccb_p cp, u32 scr, int *ofs)
{
u32 dp_scr;
int dp_ofs, dp_sg, dp_sgmin;
int tmp;
struct sym_pmc *pm;
dp_scr = scr;
dp_ofs = *ofs;
if (dp_scr == SCRIPTA_BA (np, pm0_data))
pm = &cp->phys.pm0;
else if (dp_scr == SCRIPTA_BA (np, pm1_data))
pm = &cp->phys.pm1;
else
pm = NULL;
if (pm) {
dp_scr = scr_to_cpu(pm->ret);
dp_ofs -= scr_to_cpu(pm->sg.size);
}
if (cp->host_flags & HF_SENSE) {
*ofs = dp_ofs;
return 0;
}
tmp = scr_to_cpu(cp->phys.head.goalp);
dp_sg = SYM_CONF_MAX_SG;
if (dp_scr != tmp)
dp_sg -= (tmp - 8 - (int)dp_scr) / (2*4);
dp_sgmin = SYM_CONF_MAX_SG - cp->segments;
if (dp_ofs < 0) {
int n;
while (dp_sg > dp_sgmin) {
--dp_sg;
tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
n = dp_ofs + (tmp & 0xffffff);
if (n > 0) {
++dp_sg;
break;
}
dp_ofs = n;
}
}
else if (dp_ofs > 0) {
while (dp_sg < SYM_CONF_MAX_SG) {
tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
dp_ofs -= (tmp & 0xffffff);
++dp_sg;
if (dp_ofs <= 0)
break;
}
}
if (dp_sg < dp_sgmin || (dp_sg == dp_sgmin && dp_ofs < 0))
goto out_err;
else if (dp_sg > SYM_CONF_MAX_SG ||
(dp_sg == SYM_CONF_MAX_SG && dp_ofs > 0))
goto out_err;
if (dp_sg > cp->ext_sg ||
(dp_sg == cp->ext_sg && dp_ofs > cp->ext_ofs)) {
cp->ext_sg = dp_sg;
cp->ext_ofs = dp_ofs;
}
*ofs = dp_ofs;
return dp_sg;
out_err:
return -1;
}
static void sym_modify_dp(hcb_p np, ccb_p cp, int ofs)
{
int dp_ofs = ofs;
u32 dp_scr = INL (nc_temp);
u32 dp_ret;
u32 tmp;
u_char hflags;
int dp_sg;
struct sym_pmc *pm;
if (cp->host_flags & HF_SENSE)
goto out_reject;
dp_sg = sym_evaluate_dp(np, cp, dp_scr, &dp_ofs);
if (dp_sg < 0)
goto out_reject;
dp_ret = cpu_to_scr(cp->phys.head.goalp);
dp_ret = dp_ret - 8 - (SYM_CONF_MAX_SG - dp_sg) * (2*4);
if (dp_ofs == 0) {
dp_scr = dp_ret;
goto out_ok;
}
hflags = INB (HF_PRT);
if (hflags & HF_DP_SAVED)
hflags ^= HF_ACT_PM;
if (!(hflags & HF_ACT_PM)) {
pm = &cp->phys.pm0;
dp_scr = SCRIPTA_BA (np, pm0_data);
}
else {
pm = &cp->phys.pm1;
dp_scr = SCRIPTA_BA (np, pm1_data);
}
hflags &= ~(HF_DP_SAVED);
OUTB (HF_PRT, hflags);
pm->ret = cpu_to_scr(dp_ret);
tmp = scr_to_cpu(cp->phys.data[dp_sg-1].addr);
tmp += scr_to_cpu(cp->phys.data[dp_sg-1].size) + dp_ofs;
pm->sg.addr = cpu_to_scr(tmp);
pm->sg.size = cpu_to_scr(-dp_ofs);
out_ok:
OUTL (nc_temp, dp_scr);
OUTL_DSP (SCRIPTA_BA (np, clrack));
return;
out_reject:
OUTL_DSP (SCRIPTB_BA (np, msg_bad));
}
static int sym_compute_residual(hcb_p np, ccb_p cp)
{
int dp_sg, resid = 0;
int dp_ofs = 0;
if (cp->xerr_status & (XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN)) {
if (cp->xerr_status & XE_EXTRA_DATA)
resid -= cp->extra_bytes;
if (cp->xerr_status & XE_SODL_UNRUN)
++resid;
if (cp->xerr_status & XE_SWIDE_OVRUN)
--resid;
}
if (cp->phys.head.lastp == cp->phys.head.goalp)
return resid;
if (cp->startp == cp->phys.head.lastp ||
sym_evaluate_dp(np, cp, scr_to_cpu(cp->phys.head.lastp),
&dp_ofs) < 0) {
return cp->data_len;
}
if (cp->host_flags & HF_SENSE) {
return -dp_ofs;
}
resid = -cp->ext_ofs;
for (dp_sg = cp->ext_sg; dp_sg < SYM_CONF_MAX_SG; ++dp_sg) {
u_int tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
resid += (tmp & 0xffffff);
}
return resid;
}
static int sym_show_msg (u_char * msg)
{
u_char i;
printf ("%x",*msg);
if (*msg==M_EXTENDED) {
for (i = 1; i < 8; i++) {
if (i - 1 > msg[1]) break;
printf ("-%x",msg[i]);
}
return (i+1);
} else if ((*msg & 0xf0) == 0x20) {
printf ("-%x",msg[1]);
return (2);
}
return (1);
}
static void sym_print_msg (ccb_p cp, char *label, u_char *msg)
{
PRINT_ADDR(cp);
if (label)
printf ("%s: ", label);
(void) sym_show_msg (msg);
printf (".\n");
}
static void sym_sync_nego(hcb_p np, tcb_p tp, ccb_p cp)
{
u_char chg, ofs, per, fak, div;
int req = 1;
if (DEBUG_FLAGS & DEBUG_NEGO) {
sym_print_msg(cp, "sync msgin", np->msgin);
}
if (INB (HS_PRT) == HS_NEGOTIATE) {
OUTB (HS_PRT, HS_BUSY);
if (cp->nego_status && cp->nego_status != NS_SYNC)
goto reject_it;
req = 0;
}
chg = 0;
per = np->msgin[3];
ofs = np->msgin[4];
if (ofs) {
if (ofs > np->maxoffs)
{chg = 1; ofs = np->maxoffs;}
if (req) {
if (ofs > tp->tinfo.user.offset)
{chg = 1; ofs = tp->tinfo.user.offset;}
}
}
if (ofs) {
if (per < np->minsync)
{chg = 1; per = np->minsync;}
if (req) {
if (per < tp->tinfo.user.period)
{chg = 1; per = tp->tinfo.user.period;}
}
}
div = fak = 0;
if (ofs && sym_getsync(np, 0, per, &div, &fak) < 0)
goto reject_it;
if (DEBUG_FLAGS & DEBUG_NEGO) {
PRINT_ADDR(cp);
printf ("sdtr: ofs=%d per=%d div=%d fak=%d chg=%d.\n",
ofs, per, div, fak, chg);
}
if (req == 0) {
if (chg)
goto reject_it;
sym_setsync (np, cp, ofs, per, div, fak);
OUTL_DSP (SCRIPTA_BA (np, clrack));
return;
}
sym_setsync (np, cp, ofs, per, div, fak);
np->msgout[0] = M_EXTENDED;
np->msgout[1] = 3;
np->msgout[2] = M_X_SYNC_REQ;
np->msgout[3] = per;
np->msgout[4] = ofs;
cp->nego_status = NS_SYNC;
if (DEBUG_FLAGS & DEBUG_NEGO) {
sym_print_msg(cp, "sync msgout", np->msgout);
}
np->msgin [0] = M_NOOP;
OUTL_DSP (SCRIPTB_BA (np, sdtr_resp));
return;
reject_it:
sym_setsync (np, cp, 0, 0, 0, 0);
OUTL_DSP (SCRIPTB_BA (np, msg_bad));
}
static void sym_ppr_nego(hcb_p np, tcb_p tp, ccb_p cp)
{
u_char chg, ofs, per, fak, dt, div, wide;
int req = 1;
if (DEBUG_FLAGS & DEBUG_NEGO) {
sym_print_msg(cp, "ppr msgin", np->msgin);
}
chg = 0;
per = np->msgin[3];
ofs = np->msgin[5];
wide = np->msgin[6];
dt = np->msgin[7] & PPR_OPT_DT;
if (INB (HS_PRT) == HS_NEGOTIATE) {
OUTB (HS_PRT, HS_BUSY);
if (cp->nego_status && cp->nego_status != NS_PPR)
goto reject_it;
req = 0;
}
if (wide > np->maxwide)
{chg = 1; wide = np->maxwide;}
if (!wide || !(np->features & FE_ULTRA3))
dt &= ~PPR_OPT_DT;
if (req) {
if (wide > tp->tinfo.user.width)
{chg = 1; wide = tp->tinfo.user.width;}
}
if (!(np->features & FE_U3EN))
dt &= ~PPR_OPT_DT;
if (dt != (np->msgin[7] & PPR_OPT_MASK)) chg = 1;
if (ofs) {
if (dt) {
if (ofs > np->maxoffs_dt)
{chg = 1; ofs = np->maxoffs_dt;}
}
else if (ofs > np->maxoffs)
{chg = 1; ofs = np->maxoffs;}
if (req) {
if (ofs > tp->tinfo.user.offset)
{chg = 1; ofs = tp->tinfo.user.offset;}
}
}
if (ofs) {
if (dt) {
if (per < np->minsync_dt)
{chg = 1; per = np->minsync_dt;}
}
else if (per < np->minsync)
{chg = 1; per = np->minsync;}
if (req) {
if (per < tp->tinfo.user.period)
{chg = 1; per = tp->tinfo.user.period;}
}
}
div = fak = 0;
if (ofs && sym_getsync(np, dt, per, &div, &fak) < 0)
goto reject_it;
if (DEBUG_FLAGS & DEBUG_NEGO) {
PRINT_ADDR(cp);
printf ("ppr: "
"dt=%x ofs=%d per=%d wide=%d div=%d fak=%d chg=%d.\n",
dt, ofs, per, wide, div, fak, chg);
}
if (req == 0) {
if (chg)
goto reject_it;
sym_setpprot (np, cp, dt, ofs, per, wide, div, fak);
OUTL_DSP (SCRIPTA_BA (np, clrack));
return;
}
sym_setpprot (np, cp, dt, ofs, per, wide, div, fak);
np->msgout[0] = M_EXTENDED;
np->msgout[1] = 6;
np->msgout[2] = M_X_PPR_REQ;
np->msgout[3] = per;
np->msgout[4] = 0;
np->msgout[5] = ofs;
np->msgout[6] = wide;
np->msgout[7] = dt;
cp->nego_status = NS_PPR;
if (DEBUG_FLAGS & DEBUG_NEGO) {
sym_print_msg(cp, "ppr msgout", np->msgout);
}
np->msgin [0] = M_NOOP;
OUTL_DSP (SCRIPTB_BA (np, ppr_resp));
return;
reject_it:
sym_setpprot (np, cp, 0, 0, 0, 0, 0, 0);
OUTL_DSP (SCRIPTB_BA (np, msg_bad));
if (!req && !dt) {
tp->tinfo.goal.options = 0;
tp->tinfo.goal.width = wide;
tp->tinfo.goal.period = per;
tp->tinfo.goal.offset = ofs;
}
}
static void sym_wide_nego(hcb_p np, tcb_p tp, ccb_p cp)
{
u_char chg, wide;
int req = 1;
if (DEBUG_FLAGS & DEBUG_NEGO) {
sym_print_msg(cp, "wide msgin", np->msgin);
}
if (INB (HS_PRT) == HS_NEGOTIATE) {
OUTB (HS_PRT, HS_BUSY);
if (cp->nego_status && cp->nego_status != NS_WIDE)
goto reject_it;
req = 0;
}
chg = 0;
wide = np->msgin[3];
if (wide > np->maxwide)
{chg = 1; wide = np->maxwide;}
if (req) {
if (wide > tp->tinfo.user.width)
{chg = 1; wide = tp->tinfo.user.width;}
}
if (DEBUG_FLAGS & DEBUG_NEGO) {
PRINT_ADDR(cp);
printf ("wdtr: wide=%d chg=%d.\n", wide, chg);
}
if (req == 0) {
if (chg)
goto reject_it;
sym_setwide (np, cp, wide);
if (tp->tinfo.goal.offset) {
np->msgout[0] = M_EXTENDED;
np->msgout[1] = 3;
np->msgout[2] = M_X_SYNC_REQ;
np->msgout[3] = tp->tinfo.goal.period;
np->msgout[4] = tp->tinfo.goal.offset;
if (DEBUG_FLAGS & DEBUG_NEGO) {
sym_print_msg(cp, "sync msgout", np->msgout);
}
cp->nego_status = NS_SYNC;
OUTB (HS_PRT, HS_NEGOTIATE);
OUTL_DSP (SCRIPTB_BA (np, sdtr_resp));
return;
}
OUTL_DSP (SCRIPTA_BA (np, clrack));
return;
}
sym_setwide (np, cp, wide);
np->msgout[0] = M_EXTENDED;
np->msgout[1] = 2;
np->msgout[2] = M_X_WIDE_REQ;
np->msgout[3] = wide;
np->msgin [0] = M_NOOP;
cp->nego_status = NS_WIDE;
if (DEBUG_FLAGS & DEBUG_NEGO) {
sym_print_msg(cp, "wide msgout", np->msgout);
}
OUTL_DSP (SCRIPTB_BA (np, wdtr_resp));
return;
reject_it:
OUTL_DSP (SCRIPTB_BA (np, msg_bad));
}
static void sym_nego_default(hcb_p np, tcb_p tp, ccb_p cp)
{
switch (cp->nego_status) {
case NS_PPR:
#if 0
sym_setpprot (np, cp, 0, 0, 0, 0, 0, 0);
#else
tp->tinfo.goal.options = 0;
if (tp->tinfo.goal.period < np->minsync)
tp->tinfo.goal.period = np->minsync;
if (tp->tinfo.goal.offset > np->maxoffs)
tp->tinfo.goal.offset = np->maxoffs;
#endif
break;
case NS_SYNC:
sym_setsync (np, cp, 0, 0, 0, 0);
break;
case NS_WIDE:
sym_setwide (np, cp, 0);
break;
}
np->msgin [0] = M_NOOP;
np->msgout[0] = M_NOOP;
cp->nego_status = 0;
}
static void sym_nego_rejected(hcb_p np, tcb_p tp, ccb_p cp)
{
sym_nego_default(np, tp, cp);
OUTB (HS_PRT, HS_BUSY);
}
static void sym_int_sir (hcb_p np)
{
u_char num = INB (nc_dsps);
u32 dsa = INL (nc_dsa);
ccb_p cp = sym_ccb_from_dsa(np, dsa);
u_char target = INB (nc_sdid) & 0x0f;
tcb_p tp = &np->target[target];
int tmp;
SYM_LOCK_ASSERT(MA_OWNED);
if (DEBUG_FLAGS & DEBUG_TINY) printf ("I#%d", num);
switch (num) {
case SIR_COMPLETE_ERROR:
if (!cp)
goto out;
sym_complete_error(np, cp);
return;
case SIR_SCRIPT_STOPPED:
case SIR_TARGET_SELECTED:
case SIR_ABORT_SENT:
sym_sir_task_recovery(np, num);
return;
case SIR_SEL_ATN_NO_MSG_OUT:
printf ("%s:%d: No MSG OUT phase after selection with ATN.\n",
sym_name (np), target);
goto out_stuck;
case SIR_RESEL_NO_MSG_IN:
printf ("%s:%d: No MSG IN phase after reselection.\n",
sym_name (np), target);
goto out_stuck;
case SIR_RESEL_NO_IDENTIFY:
printf ("%s:%d: No IDENTIFY after reselection.\n",
sym_name (np), target);
goto out_stuck;
case SIR_RESEL_BAD_LUN:
np->msgout[0] = M_RESET;
goto out;
case SIR_RESEL_BAD_I_T_L:
np->msgout[0] = M_ABORT;
goto out;
case SIR_RESEL_BAD_I_T_L_Q:
np->msgout[0] = M_ABORT_TAG;
goto out;
case SIR_RESEL_ABORTED:
np->lastmsg = np->msgout[0];
np->msgout[0] = M_NOOP;
printf ("%s:%d: message %x sent on bad reselection.\n",
sym_name (np), target, np->lastmsg);
goto out;
case SIR_MSG_OUT_DONE:
np->lastmsg = np->msgout[0];
np->msgout[0] = M_NOOP;
if (np->lastmsg == M_PARITY || np->lastmsg == M_ID_ERROR) {
if (cp) {
cp->xerr_status &= ~XE_PARITY_ERR;
if (!cp->xerr_status)
OUTOFFB (HF_PRT, HF_EXT_ERR);
}
}
goto out;
case SIR_BAD_SCSI_STATUS:
if (!cp)
goto out;
sym_sir_bad_scsi_status(np, cp);
return;
case SIR_REJECT_TO_SEND:
sym_print_msg(cp, "M_REJECT to send for ", np->msgin);
np->msgout[0] = M_REJECT;
goto out;
case SIR_SWIDE_OVERRUN:
if (cp) {
OUTONB (HF_PRT, HF_EXT_ERR);
cp->xerr_status |= XE_SWIDE_OVRUN;
}
goto out;
case SIR_SODL_UNDERRUN:
if (cp) {
OUTONB (HF_PRT, HF_EXT_ERR);
cp->xerr_status |= XE_SODL_UNRUN;
}
goto out;
case SIR_DATA_OVERRUN:
if (cp) {
OUTONB (HF_PRT, HF_EXT_ERR);
cp->xerr_status |= XE_EXTRA_DATA;
cp->extra_bytes += INL (nc_scratcha);
}
goto out;
case SIR_BAD_PHASE:
if (cp) {
OUTONB (HF_PRT, HF_EXT_ERR);
cp->xerr_status |= XE_BAD_PHASE;
}
goto out;
case SIR_MSG_RECEIVED:
if (!cp)
goto out_stuck;
switch (np->msgin [0]) {
case M_EXTENDED:
switch (np->msgin [2]) {
case M_X_MODIFY_DP:
if (DEBUG_FLAGS & DEBUG_POINTER)
sym_print_msg(cp,"modify DP",np->msgin);
tmp = (np->msgin[3]<<24) + (np->msgin[4]<<16) +
(np->msgin[5]<<8) + (np->msgin[6]);
sym_modify_dp(np, cp, tmp);
return;
case M_X_SYNC_REQ:
sym_sync_nego(np, tp, cp);
return;
case M_X_PPR_REQ:
sym_ppr_nego(np, tp, cp);
return;
case M_X_WIDE_REQ:
sym_wide_nego(np, tp, cp);
return;
default:
goto out_reject;
}
break;
case M_IGN_RESIDUE:
if (DEBUG_FLAGS & DEBUG_POINTER)
sym_print_msg(cp,"ign wide residue", np->msgin);
sym_modify_dp(np, cp, -1);
return;
case M_REJECT:
if (INB (HS_PRT) == HS_NEGOTIATE)
sym_nego_rejected(np, tp, cp);
else {
PRINT_ADDR(cp);
printf ("M_REJECT received (%x:%x).\n",
scr_to_cpu(np->lastmsg), np->msgout[0]);
}
goto out_clrack;
break;
default:
goto out_reject;
}
break;
case SIR_MSG_WEIRD:
sym_print_msg(cp, "WEIRD message received", np->msgin);
OUTL_DSP (SCRIPTB_BA (np, msg_weird));
return;
case SIR_NEGO_FAILED:
OUTB (HS_PRT, HS_BUSY);
case SIR_NEGO_PROTO:
if (!cp)
goto out;
sym_nego_default(np, tp, cp);
goto out;
}
out:
OUTONB_STD ();
return;
out_reject:
OUTL_DSP (SCRIPTB_BA (np, msg_bad));
return;
out_clrack:
OUTL_DSP (SCRIPTA_BA (np, clrack));
return;
out_stuck:
return;
}
static ccb_p sym_get_ccb (hcb_p np, u_char tn, u_char ln, u_char tag_order)
{
tcb_p tp = &np->target[tn];
lcb_p lp = sym_lp(tp, ln);
u_short tag = NO_TAG;
SYM_QUEHEAD *qp;
ccb_p cp = (ccb_p) NULL;
if (sym_que_empty(&np->free_ccbq))
goto out;
qp = sym_remque_head(&np->free_ccbq);
if (!qp)
goto out;
cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
if (!lp && sym_is_bit(tp->lun_map, ln)) {
lp = sym_alloc_lcb(np, tn, ln);
if (!lp)
goto out_free;
}
if (!lp) {
if (!sym_is_bit(tp->busy0_map, ln))
sym_set_bit(tp->busy0_map, ln);
else
goto out_free;
} else {
if (tag_order) {
if (lp->busy_itl != 0)
goto out_free;
if (!lp->cb_tags) {
sym_alloc_lcb_tags(np, tn, ln);
if (!lp->cb_tags)
goto out_free;
}
if (lp->busy_itlq < SYM_CONF_MAX_TASK) {
tag = lp->cb_tags[lp->ia_tag];
if (++lp->ia_tag == SYM_CONF_MAX_TASK)
lp->ia_tag = 0;
lp->itlq_tbl[tag] = cpu_to_scr(cp->ccb_ba);
++lp->busy_itlq;
lp->head.resel_sa =
cpu_to_scr(SCRIPTA_BA (np, resel_tag));
}
else
goto out_free;
}
else {
if (lp->busy_itlq != 0 || lp->busy_itl != 0)
goto out_free;
lp->busy_itl = 1;
lp->head.itl_task_sa = cpu_to_scr(cp->ccb_ba);
lp->head.resel_sa =
cpu_to_scr(SCRIPTA_BA (np, resel_no_tag));
}
}
sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
cp->to_abort = 0;
cp->tag = tag;
cp->target = tn;
cp->lun = ln;
if (DEBUG_FLAGS & DEBUG_TAGS) {
PRINT_LUN(np, tn, ln);
printf ("ccb @%p using tag %d.\n", cp, tag);
}
out:
return cp;
out_free:
sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
return NULL;
}
static void sym_free_ccb(hcb_p np, ccb_p cp)
{
tcb_p tp = &np->target[cp->target];
lcb_p lp = sym_lp(tp, cp->lun);
if (DEBUG_FLAGS & DEBUG_TAGS) {
PRINT_LUN(np, cp->target, cp->lun);
printf ("ccb @%p freeing tag %d.\n", cp, cp->tag);
}
if (lp) {
if (cp->tag != NO_TAG) {
lp->cb_tags[lp->if_tag] = cp->tag;
if (++lp->if_tag == SYM_CONF_MAX_TASK)
lp->if_tag = 0;
lp->itlq_tbl[cp->tag] = cpu_to_scr(np->bad_itlq_ba);
--lp->busy_itlq;
} else {
lp->head.itl_task_sa = cpu_to_scr(np->bad_itl_ba);
lp->busy_itl = 0;
}
if (lp->busy_itlq == 0 && lp->busy_itl == 0)
lp->head.resel_sa =
cpu_to_scr(SCRIPTB_BA (np, resel_bad_lun));
}
else
sym_clr_bit(tp->busy0_map, cp->lun);
if (cp == tp->nego_cp)
tp->nego_cp = NULL;
#ifdef SYM_CONF_IARB_SUPPORT
if (cp == np->last_cp)
np->last_cp = NULL;
#endif
if (cp->dmamapped) {
bus_dmamap_unload(np->data_dmat, cp->dmamap);
cp->dmamapped = 0;
}
cp->cam_ccb = NULL;
cp->host_status = HS_IDLE;
sym_remque(&cp->link_ccbq);
sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
}
static ccb_p sym_alloc_ccb(hcb_p np)
{
ccb_p cp = NULL;
int hcode;
SYM_LOCK_ASSERT(MA_NOTOWNED);
if (np->actccbs >= SYM_CONF_MAX_START)
return NULL;
cp = sym_calloc_dma(sizeof(struct sym_ccb), "CCB");
if (!cp)
return NULL;
cp->sns_bbuf = sym_calloc_dma(SYM_SNS_BBUF_LEN, "SNS_BBUF");
if (!cp->sns_bbuf)
goto out_free;
if (bus_dmamap_create(np->data_dmat, 0, &cp->dmamap))
goto out_free;
np->actccbs++;
callout_init(&cp->ch, 1);
cp->ccb_ba = vtobus(cp);
hcode = CCB_HASH_CODE(cp->ccb_ba);
cp->link_ccbh = np->ccbh[hcode];
np->ccbh[hcode] = cp;
cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA (np, idle));
cp->phys.head.go.restart = cpu_to_scr(SCRIPTB_BA (np, bad_i_t_l));
cp->phys.smsg_ext.addr = cpu_to_scr(HCB_BA(np, msgin[2]));
sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
return cp;
out_free:
if (cp->sns_bbuf)
sym_mfree_dma(cp->sns_bbuf, SYM_SNS_BBUF_LEN, "SNS_BBUF");
sym_mfree_dma(cp, sizeof(*cp), "CCB");
return NULL;
}
static ccb_p sym_ccb_from_dsa(hcb_p np, u32 dsa)
{
int hcode;
ccb_p cp;
hcode = CCB_HASH_CODE(dsa);
cp = np->ccbh[hcode];
while (cp) {
if (cp->ccb_ba == dsa)
break;
cp = cp->link_ccbh;
}
return cp;
}
static lcb_p sym_alloc_lcb (hcb_p np, u_char tn, u_char ln)
{
tcb_p tp = &np->target[tn];
lcb_p lp = sym_lp(tp, ln);
if (lp)
return lp;
assert(!sym_is_bit(tp->busy0_map, ln));
if (ln && !tp->luntbl) {
int i;
tp->luntbl = sym_calloc_dma(256, "LUNTBL");
if (!tp->luntbl)
goto fail;
for (i = 0 ; i < 64 ; i++)
tp->luntbl[i] = cpu_to_scr(vtobus(&np->badlun_sa));
tp->head.luntbl_sa = cpu_to_scr(vtobus(tp->luntbl));
}
if (ln && !tp->lunmp) {
tp->lunmp = sym_calloc(SYM_CONF_MAX_LUN * sizeof(lcb_p),
"LUNMP");
if (!tp->lunmp)
goto fail;
}
lp = sym_calloc_dma(sizeof(struct sym_lcb), "LCB");
if (!lp)
goto fail;
if (ln) {
tp->lunmp[ln] = lp;
tp->luntbl[ln] = cpu_to_scr(vtobus(lp));
}
else {
tp->lun0p = lp;
tp->head.lun0_sa = cpu_to_scr(vtobus(lp));
}
lp->head.itl_task_sa = cpu_to_scr(np->bad_itl_ba);
lp->head.resel_sa = cpu_to_scr(SCRIPTB_BA (np, resel_bad_lun));
lp->user_flags = tp->usrflags & (SYM_DISC_ENABLED | SYM_TAGS_ENABLED);
fail:
return lp;
}
static void sym_alloc_lcb_tags (hcb_p np, u_char tn, u_char ln)
{
tcb_p tp = &np->target[tn];
lcb_p lp = sym_lp(tp, ln);
int i;
if (!lp && !(lp = sym_alloc_lcb(np, tn, ln)))
return;
lp->itlq_tbl = sym_calloc_dma(SYM_CONF_MAX_TASK*4, "ITLQ_TBL");
if (!lp->itlq_tbl)
return;
lp->cb_tags = sym_calloc(SYM_CONF_MAX_TASK, "CB_TAGS");
if (!lp->cb_tags) {
sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK*4, "ITLQ_TBL");
lp->itlq_tbl = NULL;
return;
}
for (i = 0 ; i < SYM_CONF_MAX_TASK ; i++)
lp->itlq_tbl[i] = cpu_to_scr(np->notask_ba);
for (i = 0 ; i < SYM_CONF_MAX_TASK ; i++)
lp->cb_tags[i] = i;
lp->head.itlq_tbl_sa = cpu_to_scr(vtobus(lp->itlq_tbl));
}
#ifndef SYM_CONF_IOMAPPED
static int sym_regtest (hcb_p np)
{
register volatile u32 data;
data = 0xffffffff;
OUTL_OFF(offsetof(struct sym_reg, nc_dstat), data);
data = INL_OFF(offsetof(struct sym_reg, nc_dstat));
#if 1
if (data == 0xffffffff) {
#else
if ((data & 0xe2f0fffd) != 0x02000080) {
#endif
printf ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
(unsigned) data);
return (0x10);
}
return (0);
}
#endif
static int sym_snooptest (hcb_p np)
{
u32 sym_rd, sym_wr, sym_bk, host_rd, host_wr, pc, dstat;
int i, err=0;
#ifndef SYM_CONF_IOMAPPED
err |= sym_regtest (np);
if (err) return (err);
#endif
restart_test:
OUTB (nc_ctest4, (np->rv_ctest4 & MPEE));
pc = SCRIPTB0_BA (np, snooptest);
host_wr = 1;
sym_wr = 2;
np->cache = cpu_to_scr(host_wr);
OUTL (nc_temp, sym_wr);
OUTL (nc_dsa, np->hcb_ba);
OUTL_DSP (pc);
for (i = 0; i < SYM_SNOOP_TIMEOUT; i++)
if (INB(nc_istat) & (INTF|SIP|DIP))
break;
if (i >= SYM_SNOOP_TIMEOUT) {
printf ("CACHE TEST FAILED: timeout.\n");
return (0x20);
}
dstat = INB (nc_dstat);
#if 1
if ((dstat & MDPE) && (np->rv_ctest4 & MPEE)) {
device_printf(np->device, "PCI DATA PARITY ERROR DETECTED - "
"DISABLING MASTER DATA PARITY CHECKING.\n");
np->rv_ctest4 &= ~MPEE;
goto restart_test;
}
#endif
if (dstat & (MDPE|BF|IID)) {
device_printf(np->device,
"CACHE TEST FAILED: DMA error (dstat=0x%02x).\n", dstat);
return (0x80);
}
pc = INL (nc_dsp);
host_rd = scr_to_cpu(np->cache);
sym_rd = INL (nc_scratcha);
sym_bk = INL (nc_temp);
if (pc != SCRIPTB0_BA (np, snoopend)+8) {
device_printf(np->device,
"CACHE TEST FAILED: script execution failed.\n");
device_printf(np->device, "start=%08lx, pc=%08lx, end=%08lx\n",
(u_long)SCRIPTB0_BA(np, snooptest), (u_long)pc,
(u_long)SCRIPTB0_BA(np, snoopend) + 8);
return (0x40);
}
if (host_wr != sym_rd) {
device_printf(np->device,
"CACHE TEST FAILED: host wrote %d, chip read %d.\n",
(int)host_wr, (int)sym_rd);
err |= 1;
}
if (host_rd != sym_wr) {
device_printf(np->device,
"CACHE TEST FAILED: chip wrote %d, host read %d.\n",
(int)sym_wr, (int)host_rd);
err |= 2;
}
if (sym_bk != sym_wr) {
device_printf(np->device,
"CACHE TEST FAILED: chip wrote %d, read back %d.\n",
(int)sym_wr, (int)sym_bk);
err |= 4;
}
return (err);
}
static void sym_selectclock(hcb_p np, u_char scntl3)
{
if (np->multiplier <= 1) {
OUTB(nc_scntl3, scntl3);
return;
}
if (sym_verbose >= 2)
device_printf(np->device, "enabling clock multiplier\n");
OUTB(nc_stest1, DBLEN);
if (np->features & FE_LCKFRQ) {
int i = 20;
while (!(INB(nc_stest4) & LCKFRQ) && --i > 0)
UDELAY (20);
if (!i)
device_printf(np->device,
"the chip cannot lock the frequency\n");
} else
UDELAY (20);
OUTB(nc_stest3, HSC);
OUTB(nc_scntl3, scntl3);
OUTB(nc_stest1, (DBLEN|DBLSEL));
OUTB(nc_stest3, 0x00);
}
static unsigned getfreq (hcb_p np, int gen)
{
unsigned int ms = 0;
unsigned int f;
OUTW (nc_sien , 0);
(void) INW (nc_sist);
OUTB (nc_dien , 0);
(void) INW (nc_sist);
OUTB (nc_scntl3, 4);
OUTB (nc_stime1, 0);
OUTB (nc_stime1, gen);
while (!(INW(nc_sist) & GEN) && ms++ < 100000)
UDELAY (1000);
OUTB (nc_stime1, 0);
OUTB (nc_scntl3, 0);
f = ms ? ((1 << gen) * 4340) / ms : 0;
if (sym_verbose >= 2)
device_printf(np->device, "Delay (GEN=%d): %u msec, %u KHz\n",
gen, ms, f);
return f;
}
static unsigned sym_getfreq (hcb_p np)
{
u_int f1, f2;
int gen = 11;
(void) getfreq (np, gen);
f1 = getfreq (np, gen);
f2 = getfreq (np, gen);
if (f1 > f2) f1 = f2;
return f1;
}
static void sym_getclock (hcb_p np, int mult)
{
unsigned char scntl3 = np->sv_scntl3;
unsigned char stest1 = np->sv_stest1;
unsigned f1;
if (np->features & FE_C10) {
np->multiplier = mult;
np->clock_khz = 40000 * mult;
return;
}
np->multiplier = 1;
f1 = 40000;
if (mult > 1 && (stest1 & (DBLEN+DBLSEL)) == DBLEN+DBLSEL) {
if (sym_verbose >= 2)
device_printf(np->device, "clock multiplier found\n");
np->multiplier = mult;
}
if (np->multiplier != mult || (scntl3 & 7) < 3 || !(scntl3 & 1)) {
OUTB (nc_stest1, 0);
f1 = sym_getfreq (np);
if (sym_verbose)
device_printf(np->device, "chip clock is %uKHz\n", f1);
if (f1 < 45000) f1 = 40000;
else if (f1 < 55000) f1 = 50000;
else f1 = 80000;
if (f1 < 80000 && mult > 1) {
if (sym_verbose >= 2)
device_printf(np->device,
"clock multiplier assumed\n");
np->multiplier = mult;
}
} else {
if ((scntl3 & 7) == 3) f1 = 40000;
else if ((scntl3 & 7) == 5) f1 = 80000;
else f1 = 160000;
f1 /= np->multiplier;
}
f1 *= np->multiplier;
np->clock_khz = f1;
}
static int sym_getpciclock (hcb_p np)
{
int f = 0;
if (!(np->features & FE_C10)) {
OUTB (nc_stest1, SCLK);
f = (int) sym_getfreq (np);
OUTB (nc_stest1, 0);
}
np->pciclk_khz = f;
return f;
}
static void sym_print_xerr(ccb_p cp, int x_status)
{
if (x_status & XE_PARITY_ERR) {
PRINT_ADDR(cp);
printf ("unrecovered SCSI parity error.\n");
}
if (x_status & XE_EXTRA_DATA) {
PRINT_ADDR(cp);
printf ("extraneous data discarded.\n");
}
if (x_status & XE_BAD_PHASE) {
PRINT_ADDR(cp);
printf ("illegal scsi phase (4/5).\n");
}
if (x_status & XE_SODL_UNRUN) {
PRINT_ADDR(cp);
printf ("ODD transfer in DATA OUT phase.\n");
}
if (x_status & XE_SWIDE_OVRUN) {
PRINT_ADDR(cp);
printf ("ODD transfer in DATA IN phase.\n");
}
}
static int sym_xerr_cam_status(int cam_status, int x_status)
{
if (x_status) {
if (x_status & XE_PARITY_ERR)
cam_status = CAM_UNCOR_PARITY;
else if (x_status &(XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN))
cam_status = CAM_DATA_RUN_ERR;
else if (x_status & XE_BAD_PHASE)
cam_status = CAM_REQ_CMP_ERR;
else
cam_status = CAM_REQ_CMP_ERR;
}
return cam_status;
}
static void sym_complete_error (hcb_p np, ccb_p cp)
{
struct ccb_scsiio *csio;
u_int cam_status;
int i, sense_returned;
SYM_LOCK_ASSERT(MA_OWNED);
if (!cp || !cp->cam_ccb)
return;
if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_RESULT)) {
printf ("CCB=%lx STAT=%x/%x/%x DEV=%d/%d\n", (unsigned long)cp,
cp->host_status, cp->ssss_status, cp->host_flags,
cp->target, cp->lun);
MDELAY(100);
}
csio = &cp->cam_ccb->csio;
if (cp->xerr_status) {
if (sym_verbose)
sym_print_xerr(cp, cp->xerr_status);
if (cp->host_status == HS_COMPLETE)
cp->host_status = HS_COMP_ERR;
}
csio->sense_resid = 0;
csio->resid = sym_compute_residual(np, cp);
if (!SYM_CONF_RESIDUAL_SUPPORT) {
csio->resid = 0;
cp->sv_resid = 0;
}
if (cp->host_flags & HF_SENSE) {
csio->scsi_status = cp->sv_scsi_status;
csio->sense_resid = csio->resid;
csio->resid = cp->sv_resid;
cp->sv_resid = 0;
if (sym_verbose && cp->sv_xerr_status)
sym_print_xerr(cp, cp->sv_xerr_status);
if (cp->host_status == HS_COMPLETE &&
cp->ssss_status == S_GOOD &&
cp->xerr_status == 0) {
cam_status = sym_xerr_cam_status(CAM_SCSI_STATUS_ERROR,
cp->sv_xerr_status);
cam_status |= CAM_AUTOSNS_VALID;
bzero(&csio->sense_data, sizeof(csio->sense_data));
sense_returned = SYM_SNS_BBUF_LEN - csio->sense_resid;
if (sense_returned < csio->sense_len)
csio->sense_resid = csio->sense_len -
sense_returned;
else
csio->sense_resid = 0;
memcpy(&csio->sense_data, cp->sns_bbuf,
MIN(csio->sense_len, sense_returned));
#if 0
if (1) {
u_char *p;
p = (u_char *) csio->sense_data;
if (p[0]==0x70 && p[2]==0x6 && p[12]==0x29)
sym_clear_tasks(np, CAM_REQ_ABORTED,
cp->target,cp->lun, -1);
}
#endif
}
else
cam_status = CAM_AUTOSENSE_FAIL;
}
else if (cp->host_status == HS_COMPLETE) {
csio->scsi_status = cp->ssss_status;
cam_status = CAM_SCSI_STATUS_ERROR;
}
else if (cp->host_status == HS_SEL_TIMEOUT)
cam_status = CAM_SEL_TIMEOUT;
else if (cp->host_status == HS_UNEXPECTED)
cam_status = CAM_UNEXP_BUSFREE;
else {
if (sym_verbose) {
PRINT_ADDR(cp);
printf ("COMMAND FAILED (%x %x %x).\n",
cp->host_status, cp->ssss_status,
cp->xerr_status);
}
csio->scsi_status = cp->ssss_status;
cam_status = sym_xerr_cam_status(CAM_REQ_CMP_ERR,
cp->xerr_status);
}
i = (INL (nc_scratcha) - np->squeue_ba) / 4;
(void) sym_dequeue_from_squeue(np, i, cp->target, cp->lun, -1);
OUTL_DSP (SCRIPTA_BA (np, start));
if (cp->dmamapped) {
bus_dmamap_sync(np->data_dmat, cp->dmamap,
(cp->dmamapped == SYM_DMA_READ ?
BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE));
}
sym_set_cam_status((union ccb *) csio, cam_status);
sym_remque(&cp->link_ccbq);
sym_insque_head(&cp->link_ccbq, &np->comp_ccbq);
sym_flush_comp_queue(np, 0);
}
static void sym_complete_ok (hcb_p np, ccb_p cp)
{
struct ccb_scsiio *csio;
tcb_p tp;
lcb_p lp;
SYM_LOCK_ASSERT(MA_OWNED);
if (!cp || !cp->cam_ccb)
return;
assert (cp->host_status == HS_COMPLETE);
csio = &cp->cam_ccb->csio;
tp = &np->target[cp->target];
lp = sym_lp(tp, cp->lun);
if (!lp)
sym_set_bit(tp->lun_map, cp->lun);
csio->resid = 0;
if (cp->phys.head.lastp != cp->phys.head.goalp)
csio->resid = sym_compute_residual(np, cp);
if (!SYM_CONF_RESIDUAL_SUPPORT)
csio->resid = 0;
if (cp->dmamapped) {
bus_dmamap_sync(np->data_dmat, cp->dmamap,
(cp->dmamapped == SYM_DMA_READ ?
BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE));
}
csio->scsi_status = cp->ssss_status;
sym_set_cam_status((union ccb *) csio, CAM_REQ_CMP);
sym_xpt_done(np, (union ccb *) csio, cp);
sym_free_ccb(np, cp);
}
static void sym_callout(void *arg)
{
union ccb *ccb = (union ccb *) arg;
hcb_p np = ccb->ccb_h.sym_hcb_ptr;
if (!np)
return;
SYM_LOCK();
switch(ccb->ccb_h.func_code) {
case XPT_SCSI_IO:
(void) sym_abort_scsiio(np, ccb, 1);
break;
default:
break;
}
SYM_UNLOCK();
}
static int sym_abort_scsiio(hcb_p np, union ccb *ccb, int timed_out)
{
ccb_p cp;
SYM_QUEHEAD *qp;
SYM_LOCK_ASSERT(MA_OWNED);
cp = NULL;
FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
ccb_p cp2 = sym_que_entry(qp, struct sym_ccb, link_ccbq);
if (cp2->cam_ccb == ccb) {
cp = cp2;
break;
}
}
if (!cp || cp->host_status == HS_WAIT)
return -1;
if (cp->to_abort) {
sym_reset_scsi_bus(np, 1);
return 0;
}
cp->to_abort = timed_out ? 2 : 1;
callout_reset(&cp->ch, 10 * hz, sym_callout, (caddr_t) ccb);
np->istat_sem = SEM;
OUTB (nc_istat, SIGP|SEM);
return 0;
}
static void sym_reset_dev(hcb_p np, union ccb *ccb)
{
tcb_p tp;
struct ccb_hdr *ccb_h = &ccb->ccb_h;
SYM_LOCK_ASSERT(MA_OWNED);
if (ccb_h->target_id == np->myaddr ||
ccb_h->target_id >= SYM_CONF_MAX_TARGET ||
ccb_h->target_lun >= SYM_CONF_MAX_LUN) {
sym_xpt_done2(np, ccb, CAM_DEV_NOT_THERE);
return;
}
tp = &np->target[ccb_h->target_id];
tp->to_reset = 1;
sym_xpt_done2(np, ccb, CAM_REQ_CMP);
np->istat_sem = SEM;
OUTB (nc_istat, SIGP|SEM);
}
static void sym_action(struct cam_sim *sim, union ccb *ccb)
{
hcb_p np;
tcb_p tp;
lcb_p lp;
ccb_p cp;
int tmp;
u_char idmsg, *msgptr;
u_int msglen;
struct ccb_scsiio *csio;
struct ccb_hdr *ccb_h;
CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("sym_action\n"));
np = (hcb_p) cam_sim_softc(sim);
SYM_LOCK_ASSERT(MA_OWNED);
if (ccb->ccb_h.func_code != XPT_SCSI_IO) {
sym_action2(sim, ccb);
return;
}
csio = &ccb->csio;
ccb_h = &csio->ccb_h;
if ((ccb_h->status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
xpt_done(ccb);
return;
}
if (ccb_h->target_id == np->myaddr ||
ccb_h->target_id >= SYM_CONF_MAX_TARGET ||
ccb_h->target_lun >= SYM_CONF_MAX_LUN) {
sym_xpt_done2(np, ccb, CAM_DEV_NOT_THERE);
return;
}
tp = &np->target[ccb_h->target_id];
lp = sym_lp(tp, ccb_h->target_lun);
if (!(ccb_h->flags & CAM_CDB_PHYS) &&
(0x12 == ((ccb_h->flags & CAM_CDB_POINTER) ?
csio->cdb_io.cdb_ptr[0] : csio->cdb_io.cdb_bytes[0]))) {
if ((tp->usrflags & SYM_SCAN_BOOT_DISABLED) ||
((tp->usrflags & SYM_SCAN_LUNS_DISABLED) &&
ccb_h->target_lun != 0)) {
tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
sym_xpt_done2(np, ccb, CAM_DEV_NOT_THERE);
return;
}
}
tmp = ((ccb_h->flags & CAM_TAG_ACTION_VALID) != 0);
cp = sym_get_ccb(np, ccb_h->target_id, ccb_h->target_lun, tmp);
if (!cp) {
sym_xpt_done2(np, ccb, CAM_RESRC_UNAVAIL);
return;
}
cp->cam_ccb = ccb;
idmsg = M_IDENTIFY | cp->lun;
if (cp->tag != NO_TAG || (lp && (lp->current_flags & SYM_DISC_ENABLED)))
idmsg |= 0x40;
msgptr = cp->scsi_smsg;
msglen = 0;
msgptr[msglen++] = idmsg;
if (cp->tag != NO_TAG) {
u_char order = csio->tag_action;
switch(order) {
case M_ORDERED_TAG:
break;
case M_HEAD_TAG:
break;
default:
order = M_SIMPLE_TAG;
}
msgptr[msglen++] = order;
#if SYM_CONF_MAX_TASK > (512/4)
msgptr[msglen++] = cp->tag;
#else
msgptr[msglen++] = (cp->tag << 1) + 1;
#endif
}
cp->nego_status = 0;
if (tp->tinfo.current.width != tp->tinfo.goal.width ||
tp->tinfo.current.period != tp->tinfo.goal.period ||
tp->tinfo.current.offset != tp->tinfo.goal.offset ||
tp->tinfo.current.options != tp->tinfo.goal.options) {
if (!tp->nego_cp && lp)
msglen += sym_prepare_nego(np, cp, 0, msgptr + msglen);
}
cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA (np, select));
cp->phys.head.go.restart = cpu_to_scr(SCRIPTA_BA (np, resel_dsa));
cp->phys.select.sel_id = cp->target;
cp->phys.select.sel_scntl3 = tp->head.wval;
cp->phys.select.sel_sxfer = tp->head.sval;
cp->phys.select.sel_scntl4 = tp->head.uval;
cp->phys.smsg.addr = cpu_to_scr(CCB_BA (cp, scsi_smsg));
cp->phys.smsg.size = cpu_to_scr(msglen);
if (sym_setup_cdb(np, csio, cp) < 0) {
sym_xpt_done(np, ccb, cp);
sym_free_ccb(np, cp);
return;
}
#if 0
cp->actualquirks = tp->quirks;
#endif
cp->actualquirks = SYM_QUIRK_AUTOSAVE;
cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
cp->ssss_status = S_ILLEGAL;
cp->xerr_status = 0;
cp->host_flags = 0;
cp->extra_bytes = 0;
cp->ext_sg = -1;
cp->ext_ofs = 0;
sym_setup_data_and_start(np, csio, cp);
}
static int sym_setup_cdb(hcb_p np, struct ccb_scsiio *csio, ccb_p cp)
{
struct ccb_hdr *ccb_h;
u32 cmd_ba;
int cmd_len;
SYM_LOCK_ASSERT(MA_OWNED);
ccb_h = &csio->ccb_h;
if (csio->cdb_len > sizeof(cp->cdb_buf)) {
sym_set_cam_status(cp->cam_ccb, CAM_REQ_INVALID);
return -1;
}
cmd_len = csio->cdb_len;
if (ccb_h->flags & CAM_CDB_POINTER) {
if (!(ccb_h->flags & CAM_CDB_PHYS)) {
memcpy(cp->cdb_buf, csio->cdb_io.cdb_ptr, cmd_len);
cmd_ba = CCB_BA (cp, cdb_buf[0]);
} else {
#if 0
cmd_ba = ((u32)csio->cdb_io.cdb_ptr) & 0xffffffff;
#else
sym_set_cam_status(cp->cam_ccb, CAM_REQ_INVALID);
return -1;
#endif
}
} else {
memcpy(cp->cdb_buf, csio->cdb_io.cdb_bytes, cmd_len);
cmd_ba = CCB_BA (cp, cdb_buf[0]);
}
cp->phys.cmd.addr = cpu_to_scr(cmd_ba);
cp->phys.cmd.size = cpu_to_scr(cmd_len);
return 0;
}
static void __inline
sym_setup_data_pointers(hcb_p np, ccb_p cp, int dir)
{
u32 lastp, goalp;
SYM_LOCK_ASSERT(MA_OWNED);
if (!cp->segments)
dir = CAM_DIR_NONE;
switch(dir) {
case CAM_DIR_OUT:
goalp = SCRIPTA_BA (np, data_out2) + 8;
lastp = goalp - 8 - (cp->segments * (2*4));
break;
case CAM_DIR_IN:
cp->host_flags |= HF_DATA_IN;
goalp = SCRIPTA_BA (np, data_in2) + 8;
lastp = goalp - 8 - (cp->segments * (2*4));
break;
case CAM_DIR_NONE:
default:
lastp = goalp = SCRIPTB_BA (np, no_data);
break;
}
cp->phys.head.lastp = cpu_to_scr(lastp);
cp->phys.head.goalp = cpu_to_scr(goalp);
cp->phys.head.savep = cpu_to_scr(lastp);
cp->startp = cp->phys.head.savep;
}
static void
sym_execute_ccb(void *arg, bus_dma_segment_t *psegs, int nsegs, int error)
{
ccb_p cp;
hcb_p np;
union ccb *ccb;
cp = (ccb_p) arg;
ccb = cp->cam_ccb;
np = (hcb_p) cp->arg;
SYM_LOCK_ASSERT(MA_OWNED);
if (sym_get_cam_status(ccb) != CAM_REQ_INPROG)
goto out_abort;
if (error) {
cp->dmamapped = 0;
sym_set_cam_status(cp->cam_ccb, CAM_REQ_ABORTED);
goto out_abort;
}
if (nsegs) {
int retv;
if (np->device_id == PCI_ID_SYM53C896 && np->revision_id <= 1)
retv = sym_scatter_sg_physical(np, cp, psegs, nsegs);
else
retv = sym_fast_scatter_sg_physical(np,cp, psegs,nsegs);
if (retv < 0) {
sym_set_cam_status(cp->cam_ccb, CAM_REQ_TOO_BIG);
goto out_abort;
}
}
if (cp->dmamapped) {
bus_dmamap_sync(np->data_dmat, cp->dmamap,
(cp->dmamapped == SYM_DMA_READ ?
BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
}
cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
sym_setup_data_pointers(np, cp, (ccb->ccb_h.flags & CAM_DIR_MASK));
sym_enqueue_cam_ccb(cp);
#if 0
switch (cp->cdb_buf[0]) {
case 0x0A: case 0x2A: case 0xAA:
panic("XXXXXXXXXXXXX WRITE NOT YET ALLOWED XXXXXXXXXXXXXX\n");
MDELAY(10000);
break;
default:
break;
}
#endif
sym_put_start_queue(np, cp);
return;
out_abort:
sym_xpt_done(np, ccb, cp);
sym_free_ccb(np, cp);
}
static void
sym_setup_data_and_start(hcb_p np, struct ccb_scsiio *csio, ccb_p cp)
{
struct ccb_hdr *ccb_h;
int dir, retv;
SYM_LOCK_ASSERT(MA_OWNED);
ccb_h = &csio->ccb_h;
cp->data_len = csio->dxfer_len;
cp->arg = np;
dir = (ccb_h->flags & CAM_DIR_MASK);
if (dir == CAM_DIR_NONE) {
sym_execute_ccb(cp, NULL, 0, 0);
return;
}
cp->dmamapped = (dir == CAM_DIR_IN) ? SYM_DMA_READ : SYM_DMA_WRITE;
retv = bus_dmamap_load_ccb(np->data_dmat, cp->dmamap,
(union ccb *)csio, sym_execute_ccb, cp, 0);
if (retv == EINPROGRESS) {
cp->host_status = HS_WAIT;
xpt_freeze_simq(np->sim, 1);
csio->ccb_h.status |= CAM_RELEASE_SIMQ;
}
}
static int
sym_fast_scatter_sg_physical(hcb_p np, ccb_p cp,
bus_dma_segment_t *psegs, int nsegs)
{
struct sym_tblmove *data;
bus_dma_segment_t *psegs2;
SYM_LOCK_ASSERT(MA_OWNED);
if (nsegs > SYM_CONF_MAX_SG)
return -1;
data = &cp->phys.data[SYM_CONF_MAX_SG-1];
psegs2 = &psegs[nsegs-1];
cp->segments = nsegs;
while (1) {
data->addr = cpu_to_scr(psegs2->ds_addr);
data->size = cpu_to_scr(psegs2->ds_len);
if (DEBUG_FLAGS & DEBUG_SCATTER) {
device_printf(np->device,
"scatter: paddr=%lx len=%ld\n",
(long)psegs2->ds_addr, (long)psegs2->ds_len);
}
if (psegs2 != psegs) {
--data;
--psegs2;
continue;
}
break;
}
return 0;
}
static int
sym_scatter_sg_physical(hcb_p np, ccb_p cp, bus_dma_segment_t *psegs, int nsegs)
{
u_long ps, pe, pn;
u_long k;
int s, t;
SYM_LOCK_ASSERT(MA_OWNED);
s = SYM_CONF_MAX_SG - 1;
t = nsegs - 1;
ps = psegs[t].ds_addr;
pe = ps + psegs[t].ds_len;
while (s >= 0) {
pn = rounddown2(pe - 1, SYM_CONF_DMA_BOUNDARY);
if (pn <= ps)
pn = ps;
k = pe - pn;
if (DEBUG_FLAGS & DEBUG_SCATTER) {
device_printf(np->device,
"scatter: paddr=%lx len=%ld\n", pn, k);
}
cp->phys.data[s].addr = cpu_to_scr(pn);
cp->phys.data[s].size = cpu_to_scr(k);
--s;
if (pn == ps) {
if (--t < 0)
break;
ps = psegs[t].ds_addr;
pe = ps + psegs[t].ds_len;
}
else
pe = pn;
}
cp->segments = SYM_CONF_MAX_SG - 1 - s;
return t >= 0 ? -1 : 0;
}
static void sym_action2(struct cam_sim *sim, union ccb *ccb)
{
union ccb *abort_ccb;
struct ccb_hdr *ccb_h;
struct ccb_pathinq *cpi;
struct ccb_trans_settings *cts;
struct sym_trans *tip;
hcb_p np;
tcb_p tp;
lcb_p lp;
u_char dflags;
np = (hcb_p) cam_sim_softc(sim);
SYM_LOCK_ASSERT(MA_OWNED);
ccb_h = &ccb->ccb_h;
switch (ccb_h->func_code) {
case XPT_SET_TRAN_SETTINGS:
cts = &ccb->cts;
tp = &np->target[ccb_h->target_id];
lp = sym_lp(tp, ccb_h->target_lun);
if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
sym_update_trans(np, &tp->tinfo.goal, cts);
if (lp)
sym_update_dflags(np, &lp->current_flags, cts);
}
if (cts->type == CTS_TYPE_USER_SETTINGS) {
sym_update_trans(np, &tp->tinfo.user, cts);
if (lp)
sym_update_dflags(np, &lp->user_flags, cts);
}
sym_xpt_done2(np, ccb, CAM_REQ_CMP);
break;
case XPT_GET_TRAN_SETTINGS:
cts = &ccb->cts;
tp = &np->target[ccb_h->target_id];
lp = sym_lp(tp, ccb_h->target_lun);
#define cts__scsi (&cts->proto_specific.scsi)
#define cts__spi (&cts->xport_specific.spi)
if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
tip = &tp->tinfo.current;
dflags = lp ? lp->current_flags : 0;
}
else {
tip = &tp->tinfo.user;
dflags = lp ? lp->user_flags : tp->usrflags;
}
cts->protocol = PROTO_SCSI;
cts->transport = XPORT_SPI;
cts->protocol_version = tip->scsi_version;
cts->transport_version = tip->spi_version;
cts__spi->sync_period = tip->period;
cts__spi->sync_offset = tip->offset;
cts__spi->bus_width = tip->width;
cts__spi->ppr_options = tip->options;
cts__spi->valid = CTS_SPI_VALID_SYNC_RATE
| CTS_SPI_VALID_SYNC_OFFSET
| CTS_SPI_VALID_BUS_WIDTH
| CTS_SPI_VALID_PPR_OPTIONS;
cts__spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
if (dflags & SYM_DISC_ENABLED)
cts__spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
cts__spi->valid |= CTS_SPI_VALID_DISC;
cts__scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
if (dflags & SYM_TAGS_ENABLED)
cts__scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
cts__scsi->valid |= CTS_SCSI_VALID_TQ;
#undef cts__spi
#undef cts__scsi
sym_xpt_done2(np, ccb, CAM_REQ_CMP);
break;
case XPT_CALC_GEOMETRY:
cam_calc_geometry(&ccb->ccg, 1);
sym_xpt_done2(np, ccb, CAM_REQ_CMP);
break;
case XPT_PATH_INQ:
cpi = &ccb->cpi;
cpi->version_num = 1;
cpi->hba_inquiry = PI_MDP_ABLE|PI_SDTR_ABLE|PI_TAG_ABLE;
if ((np->features & FE_WIDE) != 0)
cpi->hba_inquiry |= PI_WIDE_16;
cpi->target_sprt = 0;
cpi->hba_misc = PIM_UNMAPPED;
if (np->usrflags & SYM_SCAN_TARGETS_HILO)
cpi->hba_misc |= PIM_SCANHILO;
if (np->usrflags & SYM_AVOID_BUS_RESET)
cpi->hba_misc |= PIM_NOBUSRESET;
cpi->hba_eng_cnt = 0;
cpi->max_target = (np->features & FE_WIDE) ? 15 : 7;
cpi->max_lun = SYM_CONF_MAX_LUN-1;
if (SYM_SETUP_MAX_LUN < SYM_CONF_MAX_LUN)
cpi->max_lun = SYM_SETUP_MAX_LUN-1;
cpi->bus_id = cam_sim_bus(sim);
cpi->initiator_id = np->myaddr;
cpi->base_transfer_speed = 3300;
strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
strlcpy(cpi->hba_vid, "Symbios", HBA_IDLEN);
strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
cpi->unit_number = cam_sim_unit(sim);
cpi->protocol = PROTO_SCSI;
cpi->protocol_version = SCSI_REV_2;
cpi->transport = XPORT_SPI;
cpi->transport_version = 2;
cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_ST;
if (np->features & FE_ULTRA3) {
cpi->transport_version = 3;
cpi->xport_specific.spi.ppr_options =
SID_SPI_CLOCK_DT_ST;
}
cpi->maxio = SYM_CONF_MAX_SG * PAGE_SIZE;
sym_xpt_done2(np, ccb, CAM_REQ_CMP);
break;
case XPT_ABORT:
abort_ccb = ccb->cab.abort_ccb;
switch(abort_ccb->ccb_h.func_code) {
case XPT_SCSI_IO:
if (sym_abort_scsiio(np, abort_ccb, 0) == 0) {
sym_xpt_done2(np, ccb, CAM_REQ_CMP);
break;
}
default:
sym_xpt_done2(np, ccb, CAM_UA_ABORT);
break;
}
break;
case XPT_RESET_DEV:
sym_reset_dev(np, ccb);
break;
case XPT_RESET_BUS:
sym_reset_scsi_bus(np, 0);
if (sym_verbose) {
xpt_print_path(np->path);
printf("SCSI BUS reset delivered.\n");
}
sym_init (np, 1);
sym_xpt_done2(np, ccb, CAM_REQ_CMP);
break;
case XPT_TERM_IO:
default:
sym_xpt_done2(np, ccb, CAM_REQ_INVALID);
break;
}
}
static void
sym_async(void *cb_arg, u32 code, struct cam_path *path, void *args __unused)
{
hcb_p np;
struct cam_sim *sim;
u_int tn;
tcb_p tp;
sim = (struct cam_sim *) cb_arg;
np = (hcb_p) cam_sim_softc(sim);
SYM_LOCK_ASSERT(MA_OWNED);
switch (code) {
case AC_LOST_DEVICE:
tn = xpt_path_target_id(path);
if (tn >= SYM_CONF_MAX_TARGET)
break;
tp = &np->target[tn];
tp->to_reset = 0;
tp->head.sval = 0;
tp->head.wval = np->rv_scntl3;
tp->head.uval = 0;
tp->tinfo.current.period = tp->tinfo.goal.period = 0;
tp->tinfo.current.offset = tp->tinfo.goal.offset = 0;
tp->tinfo.current.width = tp->tinfo.goal.width = BUS_8_BIT;
tp->tinfo.current.options = tp->tinfo.goal.options = 0;
break;
default:
break;
}
}
static void sym_update_trans(hcb_p np, struct sym_trans *tip,
struct ccb_trans_settings *cts)
{
SYM_LOCK_ASSERT(MA_OWNED);
#define cts__spi (&cts->xport_specific.spi)
if ((cts__spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
tip->width = cts__spi->bus_width;
if ((cts__spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
tip->offset = cts__spi->sync_offset;
if ((cts__spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
tip->period = cts__spi->sync_period;
if ((cts__spi->valid & CTS_SPI_VALID_PPR_OPTIONS) != 0)
tip->options = (cts__spi->ppr_options & PPR_OPT_DT);
if (cts->protocol_version != PROTO_VERSION_UNSPECIFIED &&
cts->protocol_version != PROTO_VERSION_UNKNOWN)
tip->scsi_version = cts->protocol_version;
if (cts->transport_version != XPORT_VERSION_UNSPECIFIED &&
cts->transport_version != XPORT_VERSION_UNKNOWN)
tip->spi_version = cts->transport_version;
#undef cts__spi
if (tip->width > SYM_SETUP_MAX_WIDE) tip->width = SYM_SETUP_MAX_WIDE;
if (tip->period && tip->offset) {
if (tip->offset > SYM_SETUP_MAX_OFFS) tip->offset = SYM_SETUP_MAX_OFFS;
if (tip->period < SYM_SETUP_MIN_SYNC) tip->period = SYM_SETUP_MIN_SYNC;
} else {
tip->offset = 0;
tip->period = 0;
}
if (tip->width > np->maxwide)
tip->width = np->maxwide;
if (!((np->features & (FE_C10|FE_ULTRA3)) == (FE_C10|FE_ULTRA3)) ||
!(tip->width == BUS_16_BIT && tip->offset)) {
tip->options &= ~PPR_OPT_DT;
}
if (tip->offset && tip->period) {
if (tip->options & PPR_OPT_DT) {
if (tip->period < np->minsync_dt)
tip->period = np->minsync_dt;
if (tip->period > np->maxsync_dt)
tip->period = np->maxsync_dt;
if (tip->offset > np->maxoffs_dt)
tip->offset = np->maxoffs_dt;
}
else {
if (tip->period < np->minsync)
tip->period = np->minsync;
if (tip->period > np->maxsync)
tip->period = np->maxsync;
if (tip->offset > np->maxoffs)
tip->offset = np->maxoffs;
}
}
}
static void
sym_update_dflags(hcb_p np, u_char *flags, struct ccb_trans_settings *cts)
{
SYM_LOCK_ASSERT(MA_OWNED);
#define cts__scsi (&cts->proto_specific.scsi)
#define cts__spi (&cts->xport_specific.spi)
if ((cts__spi->valid & CTS_SPI_VALID_DISC) != 0) {
if ((cts__spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
*flags |= SYM_DISC_ENABLED;
else
*flags &= ~SYM_DISC_ENABLED;
}
if ((cts__scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
if ((cts__scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
*flags |= SYM_TAGS_ENABLED;
else
*flags &= ~SYM_TAGS_ENABLED;
}
#undef cts__spi
#undef cts__scsi
}
static device_method_t sym_pci_methods[] = {
DEVMETHOD(device_probe, sym_pci_probe),
DEVMETHOD(device_attach, sym_pci_attach),
DEVMETHOD(device_detach, sym_pci_detach),
DEVMETHOD_END
};
static driver_t sym_pci_driver = {
"sym",
sym_pci_methods,
1
};
DRIVER_MODULE(sym, pci, sym_pci_driver, NULL, NULL);
MODULE_DEPEND(sym, cam, 1, 1, 1);
MODULE_DEPEND(sym, pci, 1, 1, 1);
static const struct sym_pci_chip sym_pci_dev_table[] = {
{PCI_ID_SYM53C810, 0x0f, "810", 4, 8, 4, 64,
FE_ERL}
,
#ifdef SYM_DEBUG_GENERIC_SUPPORT
{PCI_ID_SYM53C810, 0xff, "810a", 4, 8, 4, 1,
FE_BOF}
,
#else
{PCI_ID_SYM53C810, 0xff, "810a", 4, 8, 4, 1,
FE_CACHE_SET|FE_LDSTR|FE_PFEN|FE_BOF}
,
#endif
{PCI_ID_SYM53C815, 0xff, "815", 4, 8, 4, 64,
FE_BOF|FE_ERL}
,
{PCI_ID_SYM53C825, 0x0f, "825", 6, 8, 4, 64,
FE_WIDE|FE_BOF|FE_ERL|FE_DIFF}
,
{PCI_ID_SYM53C825, 0xff, "825a", 6, 8, 4, 2,
FE_WIDE|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM|FE_DIFF}
,
{PCI_ID_SYM53C860, 0xff, "860", 4, 8, 5, 1,
FE_ULTRA|FE_CLK80|FE_CACHE_SET|FE_BOF|FE_LDSTR|FE_PFEN}
,
{PCI_ID_SYM53C875, 0x01, "875", 6, 16, 5, 2,
FE_WIDE|FE_ULTRA|FE_CLK80|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
FE_RAM|FE_DIFF}
,
{PCI_ID_SYM53C875, 0xff, "875", 6, 16, 5, 2,
FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
FE_RAM|FE_DIFF}
,
{PCI_ID_SYM53C875_2, 0xff, "875", 6, 16, 5, 2,
FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
FE_RAM|FE_DIFF}
,
{PCI_ID_SYM53C885, 0xff, "885", 6, 16, 5, 2,
FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
FE_RAM|FE_DIFF}
,
#ifdef SYM_DEBUG_GENERIC_SUPPORT
{PCI_ID_SYM53C895, 0xff, "895", 6, 31, 7, 2,
FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|
FE_RAM|FE_LCKFRQ}
,
#else
{PCI_ID_SYM53C895, 0xff, "895", 6, 31, 7, 2,
FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
FE_RAM|FE_LCKFRQ}
,
#endif
{PCI_ID_SYM53C896, 0xff, "896", 6, 31, 7, 4,
FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
,
{PCI_ID_SYM53C895A, 0xff, "895a", 6, 31, 7, 4,
FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
FE_RAM|FE_RAM8K|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
,
{PCI_ID_LSI53C1010, 0x00, "1010-33", 6, 31, 7, 8,
FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_CRC|
FE_C10}
,
{PCI_ID_LSI53C1010, 0xff, "1010-33", 6, 31, 7, 8,
FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_CRC|
FE_C10|FE_U3EN}
,
{PCI_ID_LSI53C1010_2, 0xff, "1010-66", 6, 31, 7, 8,
FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_66MHZ|FE_CRC|
FE_C10|FE_U3EN}
,
{PCI_ID_LSI53C1510D, 0xff, "1510d", 6, 31, 7, 4,
FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
FE_RAM|FE_IO256|FE_LEDC}
};
static const struct sym_pci_chip *
sym_find_pci_chip(device_t dev)
{
const struct sym_pci_chip *chip;
int i;
u_short device_id;
u_char revision;
if (pci_get_vendor(dev) != PCI_VENDOR_NCR)
return NULL;
device_id = pci_get_device(dev);
revision = pci_get_revid(dev);
for (i = 0; i < nitems(sym_pci_dev_table); i++) {
chip = &sym_pci_dev_table[i];
if (device_id != chip->device_id)
continue;
if (revision > chip->revision_id)
continue;
return chip;
}
return NULL;
}
static int
sym_pci_probe(device_t dev)
{
const struct sym_pci_chip *chip;
chip = sym_find_pci_chip(dev);
if (chip && sym_find_firmware(chip)) {
device_set_desc(dev, chip->name);
return BUS_PROBE_DEFAULT;
}
return ENXIO;
}
static int
sym_pci_attach(device_t dev)
{
const struct sym_pci_chip *chip;
u_short command;
u_char cachelnsz;
struct sym_hcb *np = NULL;
struct sym_nvram nvram;
const struct sym_fw *fw = NULL;
int i;
bus_dma_tag_t bus_dmat;
bus_dmat = bus_get_dma_tag(dev);
chip = sym_find_pci_chip(dev);
if (chip == NULL || (fw = sym_find_firmware(chip)) == NULL)
return (ENXIO);
np = __sym_calloc_dma(bus_dmat, sizeof(*np), "HCB");
if (np)
np->bus_dmat = bus_dmat;
else
return (ENXIO);
device_set_softc(dev, np);
SYM_LOCK_INIT();
np->hcb_ba = vtobus(np);
np->verbose = bootverbose;
np->device = dev;
np->device_id = pci_get_device(dev);
np->revision_id = pci_get_revid(dev);
np->features = chip->features;
np->clock_divn = chip->nr_divisor;
np->maxoffs = chip->offset_max;
np->maxburst = chip->burst_max;
np->scripta_sz = fw->a_size;
np->scriptb_sz = fw->b_size;
np->fw_setup = fw->setup;
np->fw_patch = fw->patch;
np->fw_name = fw->name;
#ifdef __amd64__
np->target = sym_calloc_dma(SYM_CONF_MAX_TARGET * sizeof(*(np->target)),
"TARGET");
if (!np->target)
goto attach_failed;
#endif
sym_que_init(&np->free_ccbq);
sym_que_init(&np->busy_ccbq);
sym_que_init(&np->comp_ccbq);
sym_que_init(&np->cam_ccbq);
if (bus_dma_tag_create(np->bus_dmat, 1, SYM_CONF_DMA_BOUNDARY,
BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
BUS_SPACE_MAXSIZE_32BIT, SYM_CONF_MAX_SG, SYM_CONF_DMA_BOUNDARY,
0, busdma_lock_mutex, &np->mtx, &np->data_dmat)) {
device_printf(dev, "failed to create DMA tag.\n");
goto attach_failed;
}
command = pci_read_config(dev, PCIR_COMMAND, 2);
command |= PCIM_CMD_BUSMASTEREN | PCIM_CMD_PERRESPEN |
PCIM_CMD_MWRICEN;
pci_write_config(dev, PCIR_COMMAND, command, 2);
cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
if (!cachelnsz) {
cachelnsz = 8;
pci_write_config(dev, PCIR_CACHELNSZ, cachelnsz, 1);
}
i = SYM_PCI_MMIO;
np->mmio_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &i,
RF_ACTIVE);
if (!np->mmio_res) {
device_printf(dev, "failed to allocate MMIO resources\n");
goto attach_failed;
}
np->mmio_ba = rman_get_start(np->mmio_res);
i = 0;
np->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i,
RF_ACTIVE | RF_SHAREABLE);
if (!np->irq_res) {
device_printf(dev, "failed to allocate IRQ resource\n");
goto attach_failed;
}
#ifdef SYM_CONF_IOMAPPED
i = SYM_PCI_IO;
np->io_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &i, RF_ACTIVE);
if (!np->io_res) {
device_printf(dev, "failed to allocate IO resources\n");
goto attach_failed;
}
#endif
if (np->features & (FE_RAM|FE_RAM8K)) {
i = SYM_PCI_RAM;
if (np->features & FE_64BIT)
i = SYM_PCI_RAM64;
np->ram_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &i,
RF_ACTIVE);
if (!np->ram_res) {
device_printf(dev,"failed to allocate RAM resources\n");
goto attach_failed;
}
np->ram_ba = rman_get_start(np->ram_res);
}
sym_save_initial_setting (np);
sym_chip_reset (np);
(void) sym_read_nvram(np, &nvram);
(void) sym_prepare_setting(np, &nvram);
i = sym_getpciclock(np);
if (i > 37000)
device_printf(dev, "PCI BUS clock seems too high: %u KHz.\n",i);
np->squeue = (u32 *) sym_calloc_dma(sizeof(u32)*(MAX_QUEUE*2),"SQUEUE");
if (!np->squeue)
goto attach_failed;
np->squeue_ba = vtobus(np->squeue);
np->dqueue = (u32 *) sym_calloc_dma(sizeof(u32)*(MAX_QUEUE*2),"DQUEUE");
if (!np->dqueue)
goto attach_failed;
np->dqueue_ba = vtobus(np->dqueue);
np->targtbl = (u32 *) sym_calloc_dma(256, "TARGTBL");
if (!np->targtbl)
goto attach_failed;
np->targtbl_ba = vtobus(np->targtbl);
np->scripta0 = sym_calloc_dma(np->scripta_sz, "SCRIPTA0");
np->scriptb0 = sym_calloc_dma(np->scriptb_sz, "SCRIPTB0");
if (!np->scripta0 || !np->scriptb0)
goto attach_failed;
for (i = 0; sym_alloc_ccb(np) != NULL; i++)
;
if (i < 1)
goto attach_failed;
np->scripta_ba = vtobus(np->scripta0);
np->scriptb_ba = vtobus(np->scriptb0);
np->scriptb0_ba = np->scriptb_ba;
if (np->ram_ba) {
np->scripta_ba = np->ram_ba;
if (np->features & FE_RAM8K) {
np->ram_ws = 8192;
np->scriptb_ba = np->scripta_ba + 4096;
#ifdef __LP64__
np->scr_ram_seg = cpu_to_scr(np->scripta_ba >> 32);
#endif
}
else
np->ram_ws = 4096;
}
memcpy(np->scripta0, fw->a_base, np->scripta_sz);
memcpy(np->scriptb0, fw->b_base, np->scriptb_sz);
np->fw_setup(np, fw);
sym_fw_bind_script(np, (u32 *) np->scripta0, np->scripta_sz);
sym_fw_bind_script(np, (u32 *) np->scriptb0, np->scriptb_sz);
#ifdef SYM_CONF_IARB_SUPPORT
#ifdef SYM_SETUP_IARB_MAX
np->iarb_max = SYM_SETUP_IARB_MAX;
#else
np->iarb_max = 4;
#endif
#endif
np->idletask.start = cpu_to_scr(SCRIPTA_BA (np, idle));
np->idletask.restart = cpu_to_scr(SCRIPTB_BA (np, bad_i_t_l));
np->idletask_ba = vtobus(&np->idletask);
np->notask.start = cpu_to_scr(SCRIPTA_BA (np, idle));
np->notask.restart = cpu_to_scr(SCRIPTB_BA (np, bad_i_t_l));
np->notask_ba = vtobus(&np->notask);
np->bad_itl.start = cpu_to_scr(SCRIPTA_BA (np, idle));
np->bad_itl.restart = cpu_to_scr(SCRIPTB_BA (np, bad_i_t_l));
np->bad_itl_ba = vtobus(&np->bad_itl);
np->bad_itlq.start = cpu_to_scr(SCRIPTA_BA (np, idle));
np->bad_itlq.restart = cpu_to_scr(SCRIPTB_BA (np,bad_i_t_l_q));
np->bad_itlq_ba = vtobus(&np->bad_itlq);
np->badluntbl = sym_calloc_dma(256, "BADLUNTBL");
if (!np->badluntbl)
goto attach_failed;
np->badlun_sa = cpu_to_scr(SCRIPTB_BA (np, resel_bad_lun));
for (i = 0 ; i < 64 ; i++)
np->badluntbl[i] = cpu_to_scr(vtobus(&np->badlun_sa));
for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
np->targtbl[i] = cpu_to_scr(vtobus(&np->target[i]));
np->target[i].head.luntbl_sa =
cpu_to_scr(vtobus(np->badluntbl));
np->target[i].head.lun0_sa =
cpu_to_scr(vtobus(&np->badlun_sa));
}
if (sym_snooptest (np)) {
device_printf(dev, "CACHE INCORRECTLY CONFIGURED.\n");
goto attach_failed;
}
if (!sym_cam_attach(np))
goto attach_failed;
return 0;
attach_failed:
if (np)
sym_pci_detach(dev);
return ENXIO;
}
static int
sym_pci_detach(device_t dev)
{
hcb_p np;
SYM_QUEHEAD *qp;
ccb_p cp;
tcb_p tp;
lcb_p lp;
int target, lun;
np = device_get_softc(dev);
sym_cam_free(np);
if (np->ram_res)
bus_release_resource(np->device, SYS_RES_MEMORY,
rman_get_rid(np->ram_res), np->ram_res);
if (np->mmio_res)
bus_release_resource(np->device, SYS_RES_MEMORY,
rman_get_rid(np->mmio_res), np->mmio_res);
if (np->io_res)
bus_release_resource(np->device, SYS_RES_IOPORT,
rman_get_rid(np->io_res), np->io_res);
if (np->irq_res)
bus_release_resource(np->device, SYS_RES_IRQ, 0, np->irq_res);
if (np->scriptb0)
sym_mfree_dma(np->scriptb0, np->scriptb_sz, "SCRIPTB0");
if (np->scripta0)
sym_mfree_dma(np->scripta0, np->scripta_sz, "SCRIPTA0");
if (np->squeue)
sym_mfree_dma(np->squeue, sizeof(u32)*(MAX_QUEUE*2), "SQUEUE");
if (np->dqueue)
sym_mfree_dma(np->dqueue, sizeof(u32)*(MAX_QUEUE*2), "DQUEUE");
while ((qp = sym_remque_head(&np->free_ccbq)) != NULL) {
cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
bus_dmamap_destroy(np->data_dmat, cp->dmamap);
sym_mfree_dma(cp->sns_bbuf, SYM_SNS_BBUF_LEN, "SNS_BBUF");
sym_mfree_dma(cp, sizeof(*cp), "CCB");
}
if (np->badluntbl)
sym_mfree_dma(np->badluntbl, 256,"BADLUNTBL");
for (target = 0; target < SYM_CONF_MAX_TARGET ; target++) {
tp = &np->target[target];
for (lun = 0 ; lun < SYM_CONF_MAX_LUN ; lun++) {
lp = sym_lp(tp, lun);
if (!lp)
continue;
if (lp->itlq_tbl)
sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK*4,
"ITLQ_TBL");
if (lp->cb_tags)
sym_mfree(lp->cb_tags, SYM_CONF_MAX_TASK,
"CB_TAGS");
sym_mfree_dma(lp, sizeof(*lp), "LCB");
}
#if SYM_CONF_MAX_LUN > 1
if (tp->lunmp)
sym_mfree(tp->lunmp, SYM_CONF_MAX_LUN*sizeof(lcb_p),
"LUNMP");
#endif
}
#ifdef __amd64__
if (np->target)
sym_mfree_dma(np->target,
SYM_CONF_MAX_TARGET * sizeof(*(np->target)), "TARGET");
#endif
if (np->targtbl)
sym_mfree_dma(np->targtbl, 256, "TARGTBL");
if (np->data_dmat)
bus_dma_tag_destroy(np->data_dmat);
if (SYM_LOCK_INITIALIZED() != 0)
SYM_LOCK_DESTROY();
device_set_softc(np->device, NULL);
sym_mfree_dma(np, sizeof(*np), "HCB");
return (0);
}
static int sym_cam_attach(hcb_p np)
{
struct cam_devq *devq = NULL;
struct cam_sim *sim = NULL;
struct cam_path *path = NULL;
int err;
err = bus_setup_intr(np->device, np->irq_res,
INTR_ENTROPY | INTR_MPSAFE | INTR_TYPE_CAM,
NULL, sym_intr, np, &np->intr);
if (err) {
device_printf(np->device, "bus_setup_intr() failed: %d\n",
err);
goto fail;
}
devq = cam_simq_alloc(SYM_CONF_MAX_START);
if (!devq)
goto fail;
sim = cam_sim_alloc(sym_action, sym_poll, "sym", np,
device_get_unit(np->device),
&np->mtx, 1, SYM_SETUP_MAX_TAG, devq);
if (!sim)
goto fail;
SYM_LOCK();
if (xpt_bus_register(sim, np->device, 0) != CAM_SUCCESS)
goto fail;
np->sim = sim;
sim = NULL;
if (xpt_create_path(&path, NULL,
cam_sim_path(np->sim), CAM_TARGET_WILDCARD,
CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
goto fail;
}
np->path = path;
if (xpt_register_async(AC_LOST_DEVICE, sym_async, np->sim, path) !=
CAM_REQ_CMP)
goto fail;
sym_init (np, 0);
SYM_UNLOCK();
return 1;
fail:
SYM_UNLOCK();
sym_cam_free(np);
return 0;
}
static void sym_cam_free(hcb_p np)
{
SYM_LOCK_ASSERT(MA_NOTOWNED);
if (np->intr) {
bus_teardown_intr(np->device, np->irq_res, np->intr);
np->intr = NULL;
}
SYM_LOCK();
if (np->path) {
xpt_async(AC_LOST_DEVICE, np->path, NULL);
xpt_free_path(np->path);
np->path = NULL;
}
if (np->sim) {
xpt_bus_deregister(cam_sim_path(np->sim));
cam_sim_free(np->sim, TRUE);
np->sim = NULL;
}
SYM_UNLOCK();
}
static void sym_nvram_setup_host (hcb_p np, struct sym_nvram *nvram)
{
#ifdef SYM_CONF_NVRAM_SUPPORT
switch(nvram->type) {
case SYM_SYMBIOS_NVRAM:
if (!(nvram->data.Symbios.flags & SYMBIOS_PARITY_ENABLE))
np->rv_scntl0 &= ~0x0a;
np->myaddr = nvram->data.Symbios.host_id & 0x0f;
if (nvram->data.Symbios.flags & SYMBIOS_VERBOSE_MSGS)
np->verbose += 1;
if (nvram->data.Symbios.flags1 & SYMBIOS_SCAN_HI_LO)
np->usrflags |= SYM_SCAN_TARGETS_HILO;
if (nvram->data.Symbios.flags2 & SYMBIOS_AVOID_BUS_RESET)
np->usrflags |= SYM_AVOID_BUS_RESET;
break;
case SYM_TEKRAM_NVRAM:
np->myaddr = nvram->data.Tekram.host_id & 0x0f;
break;
default:
break;
}
#endif
}
#ifdef SYM_CONF_NVRAM_SUPPORT
static void sym_Symbios_setup_target(hcb_p np,int target, Symbios_nvram *nvram);
static void sym_Tekram_setup_target(hcb_p np,int target, Tekram_nvram *nvram);
#endif
static void
sym_nvram_setup_target (hcb_p np, int target, struct sym_nvram *nvp)
{
#ifdef SYM_CONF_NVRAM_SUPPORT
switch(nvp->type) {
case SYM_SYMBIOS_NVRAM:
sym_Symbios_setup_target (np, target, &nvp->data.Symbios);
break;
case SYM_TEKRAM_NVRAM:
sym_Tekram_setup_target (np, target, &nvp->data.Tekram);
break;
default:
break;
}
#endif
}
#ifdef SYM_CONF_NVRAM_SUPPORT
static void
sym_Symbios_setup_target(hcb_p np, int target, Symbios_nvram *nvram)
{
tcb_p tp = &np->target[target];
Symbios_target *tn = &nvram->target[target];
tp->tinfo.user.period = tn->sync_period ? (tn->sync_period + 3) / 4 : 0;
tp->tinfo.user.width = tn->bus_width == 0x10 ? BUS_16_BIT : BUS_8_BIT;
tp->usrtags =
(tn->flags & SYMBIOS_QUEUE_TAGS_ENABLED)? SYM_SETUP_MAX_TAG : 0;
if (!(tn->flags & SYMBIOS_DISCONNECT_ENABLE))
tp->usrflags &= ~SYM_DISC_ENABLED;
if (!(tn->flags & SYMBIOS_SCAN_AT_BOOT_TIME))
tp->usrflags |= SYM_SCAN_BOOT_DISABLED;
if (!(tn->flags & SYMBIOS_SCAN_LUNS))
tp->usrflags |= SYM_SCAN_LUNS_DISABLED;
}
static void
sym_Tekram_setup_target(hcb_p np, int target, Tekram_nvram *nvram)
{
tcb_p tp = &np->target[target];
struct Tekram_target *tn = &nvram->target[target];
int i;
if (tn->flags & TEKRAM_SYNC_NEGO) {
i = tn->sync_index & 0xf;
tp->tinfo.user.period = Tekram_sync[i];
}
tp->tinfo.user.width =
(tn->flags & TEKRAM_WIDE_NEGO) ? BUS_16_BIT : BUS_8_BIT;
if (tn->flags & TEKRAM_TAGGED_COMMANDS) {
tp->usrtags = 2 << nvram->max_tags_index;
}
if (tn->flags & TEKRAM_DISCONNECT_ENABLE)
tp->usrflags |= SYM_DISC_ENABLED;
if (!(tn->flags & TEKRAM_PARITY_CHECK))
np->rv_scntl0 &= ~0x0a;
}
#ifdef SYM_CONF_DEBUG_NVRAM
static void sym_display_Symbios_nvram(hcb_p np, Symbios_nvram *nvram)
{
int i;
device_printf(np->device, "HOST ID=%d%s%s%s%s%s%s\n",
nvram->host_id & 0x0f,
(nvram->flags & SYMBIOS_SCAM_ENABLE) ? " SCAM" : "",
(nvram->flags & SYMBIOS_PARITY_ENABLE) ? " PARITY" : "",
(nvram->flags & SYMBIOS_VERBOSE_MSGS) ? " VERBOSE" : "",
(nvram->flags & SYMBIOS_CHS_MAPPING) ? " CHS_ALT" : "",
(nvram->flags2 & SYMBIOS_AVOID_BUS_RESET) ? " NO_RESET" : "",
(nvram->flags1 & SYMBIOS_SCAN_HI_LO) ? " HI_LO" : "");
for (i = 0 ; i < 15 ; i++) {
struct Symbios_target *tn = &nvram->target[i];
printf("%s-%d:%s%s%s%s WIDTH=%d SYNC=%d TMO=%d\n",
sym_name(np), i,
(tn->flags & SYMBIOS_DISCONNECT_ENABLE) ? " DISC" : "",
(tn->flags & SYMBIOS_SCAN_AT_BOOT_TIME) ? " SCAN_BOOT" : "",
(tn->flags & SYMBIOS_SCAN_LUNS) ? " SCAN_LUNS" : "",
(tn->flags & SYMBIOS_QUEUE_TAGS_ENABLED)? " TCQ" : "",
tn->bus_width,
tn->sync_period / 4,
tn->timeout);
}
}
static const u_char Tekram_boot_delay[7] = {3, 5, 10, 20, 30, 60, 120};
static void sym_display_Tekram_nvram(hcb_p np, Tekram_nvram *nvram)
{
int i, tags, boot_delay;
char *rem;
tags = 2 << nvram->max_tags_index;
boot_delay = 0;
if (nvram->boot_delay_index < 6)
boot_delay = Tekram_boot_delay[nvram->boot_delay_index];
switch((nvram->flags & TEKRAM_REMOVABLE_FLAGS) >> 6) {
default:
case 0: rem = ""; break;
case 1: rem = " REMOVABLE=boot device"; break;
case 2: rem = " REMOVABLE=all"; break;
}
device_printf(np->device,
"HOST ID=%d%s%s%s%s%s%s%s%s%s BOOT DELAY=%d tags=%d\n",
nvram->host_id & 0x0f,
(nvram->flags1 & SYMBIOS_SCAM_ENABLE) ? " SCAM" : "",
(nvram->flags & TEKRAM_MORE_THAN_2_DRIVES) ? " >2DRIVES" : "",
(nvram->flags & TEKRAM_DRIVES_SUP_1GB) ? " >1GB" : "",
(nvram->flags & TEKRAM_RESET_ON_POWER_ON) ? " RESET" : "",
(nvram->flags & TEKRAM_ACTIVE_NEGATION) ? " ACT_NEG" : "",
(nvram->flags & TEKRAM_IMMEDIATE_SEEK) ? " IMM_SEEK" : "",
(nvram->flags & TEKRAM_SCAN_LUNS) ? " SCAN_LUNS" : "",
(nvram->flags1 & TEKRAM_F2_F6_ENABLED) ? " F2_F6" : "",
rem, boot_delay, tags);
for (i = 0; i <= 15; i++) {
int sync, j;
struct Tekram_target *tn = &nvram->target[i];
j = tn->sync_index & 0xf;
sync = Tekram_sync[j];
printf("%s-%d:%s%s%s%s%s%s PERIOD=%d\n",
sym_name(np), i,
(tn->flags & TEKRAM_PARITY_CHECK) ? " PARITY" : "",
(tn->flags & TEKRAM_SYNC_NEGO) ? " SYNC" : "",
(tn->flags & TEKRAM_DISCONNECT_ENABLE) ? " DISC" : "",
(tn->flags & TEKRAM_START_CMD) ? " START" : "",
(tn->flags & TEKRAM_TAGGED_COMMANDS) ? " TCQ" : "",
(tn->flags & TEKRAM_WIDE_NEGO) ? " WIDE" : "",
sync);
}
}
#endif
#endif
#ifdef SYM_CONF_NVRAM_SUPPORT
static int sym_read_Symbios_nvram (hcb_p np, Symbios_nvram *nvram);
static int sym_read_Tekram_nvram (hcb_p np, Tekram_nvram *nvram);
#endif
static int sym_read_nvram(hcb_p np, struct sym_nvram *nvp)
{
#ifdef SYM_CONF_NVRAM_SUPPORT
if (SYM_SETUP_SYMBIOS_NVRAM &&
!sym_read_Symbios_nvram (np, &nvp->data.Symbios)) {
nvp->type = SYM_SYMBIOS_NVRAM;
#ifdef SYM_CONF_DEBUG_NVRAM
sym_display_Symbios_nvram(np, &nvp->data.Symbios);
#endif
}
else if (SYM_SETUP_TEKRAM_NVRAM &&
!sym_read_Tekram_nvram (np, &nvp->data.Tekram)) {
nvp->type = SYM_TEKRAM_NVRAM;
#ifdef SYM_CONF_DEBUG_NVRAM
sym_display_Tekram_nvram(np, &nvp->data.Tekram);
#endif
}
else
nvp->type = 0;
#else
nvp->type = 0;
#endif
return nvp->type;
}
#ifdef SYM_CONF_NVRAM_SUPPORT
#define SET_BIT 0
#define CLR_BIT 1
#define SET_CLK 2
#define CLR_CLK 3
static void S24C16_set_bit(hcb_p np, u_char write_bit, u_char *gpreg,
int bit_mode)
{
UDELAY (5);
switch (bit_mode){
case SET_BIT:
*gpreg |= write_bit;
break;
case CLR_BIT:
*gpreg &= 0xfe;
break;
case SET_CLK:
*gpreg |= 0x02;
break;
case CLR_CLK:
*gpreg &= 0xfd;
break;
}
OUTB (nc_gpreg, *gpreg);
UDELAY (5);
}
static void S24C16_start(hcb_p np, u_char *gpreg)
{
S24C16_set_bit(np, 1, gpreg, SET_BIT);
S24C16_set_bit(np, 0, gpreg, SET_CLK);
S24C16_set_bit(np, 0, gpreg, CLR_BIT);
S24C16_set_bit(np, 0, gpreg, CLR_CLK);
}
static void S24C16_stop(hcb_p np, u_char *gpreg)
{
S24C16_set_bit(np, 0, gpreg, SET_CLK);
S24C16_set_bit(np, 1, gpreg, SET_BIT);
}
static void S24C16_do_bit(hcb_p np, u_char *read_bit, u_char write_bit,
u_char *gpreg)
{
S24C16_set_bit(np, write_bit, gpreg, SET_BIT);
S24C16_set_bit(np, 0, gpreg, SET_CLK);
if (read_bit)
*read_bit = INB (nc_gpreg);
S24C16_set_bit(np, 0, gpreg, CLR_CLK);
S24C16_set_bit(np, 0, gpreg, CLR_BIT);
}
static void S24C16_write_ack(hcb_p np, u_char write_bit, u_char *gpreg,
u_char *gpcntl)
{
OUTB (nc_gpcntl, *gpcntl & 0xfe);
S24C16_do_bit(np, 0, write_bit, gpreg);
OUTB (nc_gpcntl, *gpcntl);
}
static void S24C16_read_ack(hcb_p np, u_char *read_bit, u_char *gpreg,
u_char *gpcntl)
{
OUTB (nc_gpcntl, *gpcntl | 0x01);
S24C16_do_bit(np, read_bit, 1, gpreg);
OUTB (nc_gpcntl, *gpcntl);
}
static void S24C16_write_byte(hcb_p np, u_char *ack_data, u_char write_data,
u_char *gpreg, u_char *gpcntl)
{
int x;
for (x = 0; x < 8; x++)
S24C16_do_bit(np, 0, (write_data >> (7 - x)) & 0x01, gpreg);
S24C16_read_ack(np, ack_data, gpreg, gpcntl);
}
static void S24C16_read_byte(hcb_p np, u_char *read_data, u_char ack_data,
u_char *gpreg, u_char *gpcntl)
{
int x;
u_char read_bit;
*read_data = 0;
for (x = 0; x < 8; x++) {
S24C16_do_bit(np, &read_bit, 1, gpreg);
*read_data |= ((read_bit & 0x01) << (7 - x));
}
S24C16_write_ack(np, ack_data, gpreg, gpcntl);
}
static int sym_read_S24C16_nvram (hcb_p np, int offset, u_char *data, int len)
{
u_char gpcntl, gpreg;
u_char old_gpcntl, old_gpreg;
u_char ack_data;
int retv = 1;
int x;
old_gpreg = INB (nc_gpreg);
old_gpcntl = INB (nc_gpcntl);
gpcntl = old_gpcntl & 0x1c;
OUTB (nc_gpreg, old_gpreg);
OUTB (nc_gpcntl, gpcntl);
gpreg = old_gpreg;
S24C16_set_bit(np, 0, &gpreg, CLR_CLK);
S24C16_set_bit(np, 0, &gpreg, CLR_BIT);
S24C16_stop(np, &gpreg);
S24C16_start(np, &gpreg);
S24C16_write_byte(np, &ack_data,
0xa0 | ((offset >> 7) & 0x0e), &gpreg, &gpcntl);
if (ack_data & 0x01)
goto out;
S24C16_write_byte(np, &ack_data,
offset & 0xff, &gpreg, &gpcntl);
if (ack_data & 0x01)
goto out;
S24C16_start(np, &gpreg);
S24C16_write_byte(np, &ack_data,
0xa1 | ((offset >> 7) & 0x0e), &gpreg, &gpcntl);
if (ack_data & 0x01)
goto out;
gpcntl |= 0x01;
OUTB (nc_gpcntl, gpcntl);
for (x = 0; x < len; x++)
S24C16_read_byte(np, &data[x], (x == (len-1)), &gpreg, &gpcntl);
gpcntl &= 0xfe;
OUTB (nc_gpcntl, gpcntl);
S24C16_stop(np, &gpreg);
retv = 0;
out:
OUTB (nc_gpcntl, old_gpcntl);
OUTB (nc_gpreg, old_gpreg);
return retv;
}
#undef SET_BIT
#undef CLR_BIT
#undef SET_CLK
#undef CLR_CLK
static int sym_read_Symbios_nvram (hcb_p np, Symbios_nvram *nvram)
{
static u_char Symbios_trailer[6] = {0xfe, 0xfe, 0, 0, 0, 0};
u_char *data = (u_char *) nvram;
int len = sizeof(*nvram);
u_short csum;
int x;
if (sym_read_S24C16_nvram (np, SYMBIOS_NVRAM_ADDRESS, data, len))
return 1;
if (nvram->type != 0 ||
bcmp(nvram->trailer, Symbios_trailer, 6) ||
nvram->byte_count != len - 12)
return 1;
for (x = 6, csum = 0; x < len - 6; x++)
csum += data[x];
if (csum != nvram->checksum)
return 1;
return 0;
}
static void T93C46_Clk(hcb_p np, u_char *gpreg)
{
OUTB (nc_gpreg, *gpreg | 0x04);
UDELAY (2);
OUTB (nc_gpreg, *gpreg);
}
static void T93C46_Read_Bit(hcb_p np, u_char *read_bit, u_char *gpreg)
{
UDELAY (2);
T93C46_Clk(np, gpreg);
*read_bit = INB (nc_gpreg);
}
static void T93C46_Write_Bit(hcb_p np, u_char write_bit, u_char *gpreg)
{
if (write_bit & 0x01)
*gpreg |= 0x02;
else
*gpreg &= 0xfd;
*gpreg |= 0x10;
OUTB (nc_gpreg, *gpreg);
UDELAY (2);
T93C46_Clk(np, gpreg);
}
static void T93C46_Stop(hcb_p np, u_char *gpreg)
{
*gpreg &= 0xef;
OUTB (nc_gpreg, *gpreg);
UDELAY (2);
T93C46_Clk(np, gpreg);
}
static void T93C46_Send_Command(hcb_p np, u_short write_data,
u_char *read_bit, u_char *gpreg)
{
int x;
for (x = 0; x < 9; x++)
T93C46_Write_Bit(np, (u_char) (write_data >> (8 - x)), gpreg);
*read_bit = INB (nc_gpreg);
}
static void T93C46_Read_Word(hcb_p np, u_short *nvram_data, u_char *gpreg)
{
int x;
u_char read_bit;
*nvram_data = 0;
for (x = 0; x < 16; x++) {
T93C46_Read_Bit(np, &read_bit, gpreg);
if (read_bit & 0x01)
*nvram_data |= (0x01 << (15 - x));
else
*nvram_data &= ~(0x01 << (15 - x));
}
}
static int T93C46_Read_Data(hcb_p np, u_short *data,int len,u_char *gpreg)
{
u_char read_bit;
int x;
for (x = 0; x < len; x++) {
T93C46_Send_Command(np, 0x180 | x, &read_bit, gpreg);
if (read_bit & 0x01)
return 1;
T93C46_Read_Word(np, &data[x], gpreg);
T93C46_Stop(np, gpreg);
}
return 0;
}
static int sym_read_T93C46_nvram (hcb_p np, Tekram_nvram *nvram)
{
u_char gpcntl, gpreg;
u_char old_gpcntl, old_gpreg;
int retv = 1;
old_gpreg = INB (nc_gpreg);
old_gpcntl = INB (nc_gpcntl);
gpreg = old_gpreg & 0xe9;
OUTB (nc_gpreg, gpreg);
gpcntl = (old_gpcntl & 0xe9) | 0x09;
OUTB (nc_gpcntl, gpcntl);
retv = T93C46_Read_Data(np, (u_short *) nvram,
sizeof(*nvram) / sizeof(short), &gpreg);
OUTB (nc_gpcntl, old_gpcntl);
OUTB (nc_gpreg, old_gpreg);
return retv;
}
static int sym_read_Tekram_nvram (hcb_p np, Tekram_nvram *nvram)
{
u_char *data = (u_char *) nvram;
int len = sizeof(*nvram);
u_short csum;
int x;
switch (np->device_id) {
case PCI_ID_SYM53C885:
case PCI_ID_SYM53C895:
case PCI_ID_SYM53C896:
x = sym_read_S24C16_nvram(np, TEKRAM_24C16_NVRAM_ADDRESS,
data, len);
break;
case PCI_ID_SYM53C875:
x = sym_read_S24C16_nvram(np, TEKRAM_24C16_NVRAM_ADDRESS,
data, len);
if (!x)
break;
default:
x = sym_read_T93C46_nvram(np, nvram);
break;
}
if (x)
return 1;
for (x = 0, csum = 0; x < len - 1; x += 2)
csum += data[x] + (data[x+1] << 8);
if (csum != 0x1234)
return 1;
return 0;
}
#endif