#define NCR_DATE "pl30 98/1/1"
#define NCR_VERSION (2)
#define MAX_UNITS (16)
#define NCR_GETCC_WITHMSG
#if (defined(__DragonFly__) || defined (__FreeBSD__)) && defined(_KERNEL)
#include "opt_ncr.h"
#endif
#ifndef SCSI_NCR_MYADDR
#define SCSI_NCR_MYADDR (7)
#endif
#ifndef SCSI_NCR_MAX_SYNC
#ifndef SCSI_NCR_DFLT_SYNC
#define SCSI_NCR_DFLT_SYNC (12)
#endif
#else
#if SCSI_NCR_MAX_SYNC == 0
#define SCSI_NCR_DFLT_SYNC 0
#else
#define SCSI_NCR_DFLT_SYNC (250000 / SCSI_NCR_MAX_SYNC)
#endif
#endif
#ifndef SCSI_NCR_MIN_ASYNC
#define SCSI_NCR_MIN_ASYNC (40)
#endif
#ifndef SCSI_NCR_MAX_WIDE
#define SCSI_NCR_MAX_WIDE (1)
#endif
#define MAX_TARGET (16)
#ifndef MAX_LUN
#define MAX_LUN (8)
#endif
#define MAX_START (256)
#define MAX_SCATTER (33)
#define MAX_SIZE ((MAX_SCATTER-1) * (long) PAGE_SIZE)
#define NCR_SNOOP_TIMEOUT (1000000)
#include <sys/param.h>
#include <sys/time.h>
#ifdef _KERNEL
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/buf.h>
#include <sys/kernel.h>
#include <sys/sysctl.h>
#include <sys/bus.h>
#include <sys/thread2.h>
#include <machine/clock.h>
#include <machine/md_var.h>
#include <sys/rman.h>
#include <vm/vm.h>
#include <vm/pmap.h>
#include <vm/vm_extern.h>
#endif
#include <bus/pci/pcivar.h>
#include <bus/pci/pcireg.h>
#include "ncrreg.h"
#include <bus/cam/cam.h>
#include <bus/cam/cam_ccb.h>
#include <bus/cam/cam_sim.h>
#include <bus/cam/cam_xpt.h>
#include <bus/cam/cam_xpt_sim.h>
#include <bus/cam/cam_xpt_periph.h>
#include <bus/cam/cam_debug.h>
#include <bus/cam/scsi/scsi_all.h>
#include <bus/cam/scsi/scsi_message.h>
#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_FREEZE (0x0800)
#define DEBUG_RESTART (0x1000)
#ifdef SCSI_NCR_DEBUG
#define DEBUG_FLAGS ncr_debug
#else
#define SCSI_NCR_DEBUG 0
#define DEBUG_FLAGS 0
#endif
#ifdef DIAGNOSTIC
#define assert(expression) { \
if (!(expression)) { \
(void)kprintf("assertion \"%s\" failed: " \
"file \"%s\", line %d\n", \
#expression, __FILE__, __LINE__); \
Debugger(""); \
} \
}
#else
#define assert(expression) { \
if (!(expression)) { \
(void)kprintf("assertion \"%s\" failed: " \
"file \"%s\", line %d\n", \
#expression, __FILE__, __LINE__); \
} \
}
#endif
#define INB(r) bus_space_read_1(np->bst, np->bsh, offsetof(struct ncr_reg, r))
#define INW(r) bus_space_read_2(np->bst, np->bsh, offsetof(struct ncr_reg, r))
#define INL(r) bus_space_read_4(np->bst, np->bsh, offsetof(struct ncr_reg, r))
#define OUTB(r, val) bus_space_write_1(np->bst, np->bsh, \
offsetof(struct ncr_reg, r), val)
#define OUTW(r, val) bus_space_write_2(np->bst, np->bsh, \
offsetof(struct ncr_reg, r), val)
#define OUTL(r, val) bus_space_write_4(np->bst, np->bsh, \
offsetof(struct ncr_reg, r), val)
#define OUTL_OFF(o, val) bus_space_write_4(np->bst, np->bsh, o, val)
#define INB_OFF(o) bus_space_read_1(np->bst, np->bsh, o)
#define INW_OFF(o) bus_space_read_2(np->bst, np->bsh, o)
#define INL_OFF(o) bus_space_read_4(np->bst, np->bsh, o)
#define READSCRIPT_OFF(base, off) \
(base ? *((volatile u_int32_t *)((volatile char *)base + (off))) : \
bus_space_read_4(np->bst2, np->bsh2, off))
#define WRITESCRIPT_OFF(base, off, val) \
do { \
if (base) \
*((volatile u_int32_t *) \
((volatile char *)base + (off))) = (val); \
else \
bus_space_write_4(np->bst2, np->bsh2, off, val); \
} while (0)
#define READSCRIPT(r) \
READSCRIPT_OFF(np->script, offsetof(struct script, r))
#define WRITESCRIPT(r, val) \
WRITESCRIPT_OFF(np->script, offsetof(struct script, r), val)
#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 HS_IDLE (0)
#define HS_BUSY (1)
#define HS_NEGOTIATE (2)
#define HS_DISCONNECT (3)
#define HS_COMPLETE (4)
#define HS_SEL_TIMEOUT (5)
#define HS_RESET (6)
#define HS_ABORTED (7)
#define HS_TIMEOUT (8)
#define HS_FAIL (9)
#define HS_UNEXPECTED (10)
#define HS_STALL (11)
#define HS_DONEMASK (0xfc)
#define SIR_SENSE_RESTART (1)
#define SIR_SENSE_FAILED (2)
#define SIR_STALL_RESTART (3)
#define SIR_STALL_QUEUE (4)
#define SIR_NEGO_SYNC (5)
#define SIR_NEGO_WIDE (6)
#define SIR_NEGO_FAILED (7)
#define SIR_NEGO_PROTO (8)
#define SIR_REJECT_RECEIVED (9)
#define SIR_REJECT_SENT (10)
#define SIR_IGN_RESIDUE (11)
#define SIR_MISSING_SAVE (12)
#define SIR_MAX (12)
#define XE_OK (0)
#define XE_EXTRA_DATA (1)
#define XE_BAD_PHASE (2)
#define NS_SYNC (1)
#define NS_WIDE (2)
#define QUIRK_AUTOSAVE (0x01)
#define QUIRK_NOMSG (0x02)
#define QUIRK_NOSYNC (0x10)
#define QUIRK_NOWIDE16 (0x20)
#define QUIRK_NOTAGS (0x40)
#define QUIRK_UPDATE (0x80)
#define CCB_MAGIC (0xf2691ad2)
#define MAX_TAGS (32)
#define PRINT_ADDR(ccb) xpt_print_path((ccb)->ccb_h.path)
struct tcb;
struct lcb;
struct nccb;
struct ncb;
struct script;
typedef struct ncb * ncb_p;
typedef struct tcb * tcb_p;
typedef struct lcb * lcb_p;
typedef struct nccb * nccb_p;
struct link {
ncrcmd l_cmd;
ncrcmd l_paddr;
};
struct usrcmd {
u_long target;
u_long lun;
u_long data;
u_long cmd;
};
#define UC_SETSYNC 10
#define UC_SETTAGS 11
#define UC_SETDEBUG 12
#define UC_SETORDER 13
#define UC_SETWIDE 14
#define UC_SETFLAG 15
#define UF_TRACE (0x01)
typedef int ticks_t;
struct tstamp {
ticks_t start;
ticks_t end;
ticks_t select;
ticks_t command;
ticks_t data;
ticks_t status;
ticks_t disconnect;
};
struct profile {
u_long num_trans;
u_long num_bytes;
u_long num_disc;
u_long num_break;
u_long num_int;
u_long num_fly;
u_long ms_setup;
u_long ms_data;
u_long ms_disc;
u_long ms_post;
};
#define NCR_TRANS_CUR 0x01
#define NCR_TRANS_ACTIVE 0x03
#define NCR_TRANS_GOAL 0x04
#define NCR_TRANS_USER 0x08
struct ncr_transinfo {
u_int8_t width;
u_int8_t period;
u_int8_t offset;
};
struct ncr_target_tinfo {
u_int8_t disc_tag;
#define NCR_CUR_DISCENB 0x01
#define NCR_CUR_TAGENB 0x02
#define NCR_USR_DISCENB 0x04
#define NCR_USR_TAGENB 0x08
u_int8_t sval;
struct ncr_transinfo current;
struct ncr_transinfo goal;
struct ncr_transinfo user;
u_int8_t wval;
};
struct tcb {
struct link jump_tcb;
ncrcmd getscr[6];
struct link call_lun;
struct link jump_lcb;
nccb_p hold_cp;
nccb_p nego_cp;
u_long transfers;
u_long bytes;
struct ncr_target_tinfo tinfo;
lcb_p lp[MAX_LUN];
};
struct lcb {
struct link jump_lcb;
struct link call_tag;
struct link jump_nccb;
nccb_p next_nccb;
u_char reqnccbs;
u_char reqlink;
u_char actlink;
u_char usetags;
u_char lasttag;
};
struct head {
struct link launch;
u_int32_t savep;
u_int32_t lastp;
u_int32_t goalp;
nccb_p cp;
struct tstamp stamp;
u_char status[8];
};
#define QU_REG scr0
#define HS_REG scr1
#define HS_PRT nc_scr1
#define SS_REG scr2
#define PS_REG scr3
#define actualquirks phys.header.status[0]
#define host_status phys.header.status[1]
#define s_status phys.header.status[2]
#define parity_status phys.header.status[3]
#define xerr_st header.status[4]
#define sync_st header.status[5]
#define nego_st header.status[6]
#define wide_st header.status[7]
#define xerr_status phys.xerr_st
#define sync_status phys.sync_st
#define nego_status phys.nego_st
#define wide_status phys.wide_st
struct dsb {
struct head header;
struct scr_tblsel select;
struct scr_tblmove smsg ;
struct scr_tblmove smsg2 ;
struct scr_tblmove cmd ;
struct scr_tblmove scmd ;
struct scr_tblmove sense ;
struct scr_tblmove data [MAX_SCATTER];
};
struct nccb {
ncrcmd filler[4];
struct link jump_nccb;
struct link call_tmp;
struct dsb phys;
ncrcmd patch[8];
union ccb *ccb;
u_char scsi_smsg [8];
u_char scsi_smsg2[8];
u_long magic;
u_long p_nccb;
time_t tlimit;
nccb_p link_nccb;
nccb_p next_nccb;
u_char sensecmd[6];
u_char tag;
};
#define CCB_PHYS(cp,lbl) (cp->p_nccb + offsetof(struct nccb, lbl))
struct ncb {
struct head header;
int unit;
struct link jump_tcb;
int reg_rid;
struct resource *reg_res;
bus_space_tag_t bst;
bus_space_handle_t bsh;
int sram_rid;
struct resource *sram_res;
bus_space_tag_t bst2;
bus_space_handle_t bsh2;
struct resource *irq_res;
void *irq_handle;
struct script *script;
struct scripth *scripth;
u_long p_script;
u_long p_scripth;
u_char myaddr;
u_char minsync;
u_char maxsync;
u_char maxoffs;
u_char clock_divn;
u_long clock_khz;
u_long features;
u_char multiplier;
u_char maxburst;
u_char rv_scntl3;
u_char rv_dcntl;
u_char rv_dmode;
u_char rv_ctest3;
u_char rv_ctest4;
u_char rv_ctest5;
u_char rv_gpcntl;
u_char rv_stest2;
struct cam_sim *sim;
struct cam_path *path;
struct usrcmd user;
struct tcb target[MAX_TARGET];
u_int32_t squeue [MAX_START];
u_short squeueput;
time_t heartbeat;
u_short ticks;
u_short latetime;
time_t lasttime;
struct callout timeout_ch;
struct ncr_reg regdump;
time_t regtime;
struct profile profile;
u_long disc_phys;
u_long disc_ref;
nccb_p link_nccb;
u_char msgout[8];
u_char msgin [8];
u_int32_t lastmsg;
u_char scratch;
u_char maxwide;
#ifdef NCR_IOMAPPED
pci_port_t port;
#endif
};
#define NCB_SCRIPT_PHYS(np,lbl) (np->p_script + offsetof (struct script, lbl))
#define NCB_SCRIPTH_PHYS(np,lbl) (np->p_scripth + offsetof (struct scripth,lbl))
struct script {
ncrcmd start [ 7];
ncrcmd start0 [ 2];
ncrcmd start1 [ 3];
ncrcmd startpos [ 1];
ncrcmd trysel [ 8];
ncrcmd skip [ 8];
ncrcmd skip2 [ 3];
ncrcmd idle [ 2];
ncrcmd select [ 18];
ncrcmd prepare [ 4];
ncrcmd loadpos [ 14];
ncrcmd prepare2 [ 24];
ncrcmd setmsg [ 5];
ncrcmd clrack [ 2];
ncrcmd dispatch [ 33];
ncrcmd no_data [ 17];
ncrcmd checkatn [ 10];
ncrcmd command [ 15];
ncrcmd status [ 27];
ncrcmd msg_in [ 26];
ncrcmd msg_bad [ 6];
ncrcmd complete [ 13];
ncrcmd cleanup [ 12];
ncrcmd cleanup0 [ 9];
ncrcmd signal [ 12];
ncrcmd save_dp [ 5];
ncrcmd restore_dp [ 5];
ncrcmd disconnect [ 12];
ncrcmd disconnect0 [ 5];
ncrcmd disconnect1 [ 23];
ncrcmd msg_out [ 9];
ncrcmd msg_out_done [ 7];
ncrcmd badgetcc [ 6];
ncrcmd reselect [ 8];
ncrcmd reselect1 [ 8];
ncrcmd reselect2 [ 8];
ncrcmd resel_tmp [ 5];
ncrcmd resel_lun [ 18];
ncrcmd resel_tag [ 24];
ncrcmd data_in [MAX_SCATTER * 4 + 7];
ncrcmd data_out [MAX_SCATTER * 4 + 7];
};
struct scripth {
ncrcmd tryloop [MAX_START*5+2];
ncrcmd msg_parity [ 6];
ncrcmd msg_reject [ 8];
ncrcmd msg_ign_residue [ 32];
ncrcmd msg_extended [ 18];
ncrcmd msg_ext_2 [ 18];
ncrcmd msg_wdtr [ 27];
ncrcmd msg_ext_3 [ 18];
ncrcmd msg_sdtr [ 27];
ncrcmd msg_out_abort [ 10];
ncrcmd getcc [ 4];
ncrcmd getcc1 [ 5];
#ifdef NCR_GETCC_WITHMSG
ncrcmd getcc2 [ 29];
#else
ncrcmd getcc2 [ 14];
#endif
ncrcmd getcc3 [ 6];
ncrcmd aborttag [ 4];
ncrcmd abort [ 22];
ncrcmd snooptest [ 9];
ncrcmd snoopend [ 2];
};
#ifdef _KERNEL
static nccb_p ncr_alloc_nccb (ncb_p np, u_long target, u_long lun);
static void ncr_complete (ncb_p np, nccb_p cp);
static int ncr_delta (int * from, int * to);
static void ncr_exception (ncb_p np);
static void ncr_free_nccb (ncb_p np, nccb_p cp);
static void ncr_freeze_devq (ncb_p np, struct cam_path *path);
static void ncr_selectclock (ncb_p np, u_char scntl3);
static void ncr_getclock (ncb_p np, u_char multiplier);
static nccb_p ncr_get_nccb (ncb_p np, u_long t,u_long l);
#if 0
static u_int32_t ncr_info (int unit);
#endif
static void ncr_init (ncb_p np, char * msg, u_long code);
static void ncr_intr (void *vnp);
static void ncr_int_ma (ncb_p np, u_char dstat);
static void ncr_int_sir (ncb_p np);
static void ncr_int_sto (ncb_p np);
#if 0
static void ncr_min_phys (struct buf *bp);
#endif
static void ncr_poll (struct cam_sim *sim);
static void ncb_profile (ncb_p np, nccb_p cp);
static void ncr_script_copy_and_bind
(ncb_p np, ncrcmd *src, ncrcmd *dst, int len);
static void ncr_script_fill (struct script * scr, struct scripth *scrh);
static int ncr_scatter (struct dsb* phys, vm_offset_t vaddr,
vm_size_t datalen);
static void ncr_getsync (ncb_p np, u_char sfac, u_char *fakp,
u_char *scntl3p);
static void ncr_setsync (ncb_p np, nccb_p cp,u_char scntl3,u_char sxfer,
u_char period);
static void ncr_setwide (ncb_p np, nccb_p cp, u_char wide, u_char ack);
static int ncr_show_msg (u_char * msg);
static int ncr_snooptest (ncb_p np);
static void ncr_action (struct cam_sim *sim, union ccb *ccb);
static void ncr_timeout (void *arg);
static void ncr_wakeup (ncb_p np, u_long code);
static int ncr_probe (device_t dev);
static int ncr_attach (device_t dev);
#endif
#ifdef _KERNEL
static int ncr_debug = SCSI_NCR_DEBUG;
SYSCTL_INT(_debug, OID_AUTO, ncr_debug, CTLFLAG_RW, &ncr_debug, 0,
"Driver debug flags");
static int ncr_cache;
#define NCR_810_ID (0x00011000ul)
#define NCR_815_ID (0x00041000ul)
#define NCR_820_ID (0x00021000ul)
#define NCR_825_ID (0x00031000ul)
#define NCR_860_ID (0x00061000ul)
#define NCR_875_ID (0x000f1000ul)
#define NCR_875_ID2 (0x008f1000ul)
#define NCR_885_ID (0x000d1000ul)
#define NCR_895_ID (0x000c1000ul)
#define NCR_896_ID (0x000b1000ul)
#define NCR_895A_ID (0x00121000ul)
#define NCR_1510D_ID (0x000a1000ul)
static char *ncr_name (ncb_p np)
{
static char name[10];
ksnprintf(name, sizeof(name), "ncr%d", np->unit);
return (name);
}
#define RELOC_SOFTC 0x40000000
#define RELOC_LABEL 0x50000000
#define RELOC_REGISTER 0x60000000
#define RELOC_KVAR 0x70000000
#define RELOC_LABELH 0x80000000
#define RELOC_MASK 0xf0000000
#define NADDR(label) (RELOC_SOFTC | offsetof(struct ncb, label))
#define PADDR(label) (RELOC_LABEL | offsetof(struct script, label))
#define PADDRH(label) (RELOC_LABELH | offsetof(struct scripth, label))
#define RADDR(label) (RELOC_REGISTER | REG(label))
#define FADDR(label,ofs)(RELOC_REGISTER | ((REG(label))+(ofs)))
#define KVAR(which) (RELOC_KVAR | (which))
#define KVAR_SECOND (0)
#define KVAR_TICKS (1)
#define KVAR_NCR_CACHE (2)
#define SCRIPT_KVAR_FIRST (0)
#define SCRIPT_KVAR_LAST (3)
static void *script_kvars[] =
{ &time_uptime, &ticks, &ncr_cache };
static struct script script0 = {
{
SCR_COPY (sizeof (((struct ncb *)0)->heartbeat)),
KVAR (KVAR_SECOND),
NADDR (heartbeat),
SCR_LOAD_REG (dsa, 0xff),
0,
SCR_FROM_REG (ctest2),
0,
},{
SCR_INT ^ IFFALSE (0),
SIR_SENSE_RESTART,
},{
SCR_INT ^ IFFALSE (0),
SIR_STALL_RESTART,
SCR_JUMP,
},{
PADDRH(tryloop),
},{
SCR_COPY (4),
RADDR (temp),
RADDR (scratcha),
SCR_COPY (4),
RADDR (dsa),
RADDR (temp),
SCR_RETURN,
0
},{
SCR_COPY (4),
RADDR (scratcha),
PADDR (startpos),
SCR_COPY_F (4),
RADDR (dsa),
PADDR (skip2),
SCR_COPY (8),
PADDR (idle),
},{
0,
SCR_JUMP,
PADDR(start),
},{
SCR_JUMP,
PADDR(reselect),
},{
SCR_CLR (SCR_TRG),
0,
SCR_LOAD_REG (HS_REG, 0xff),
0,
SCR_SEL_TBL_ATN ^ offsetof (struct dsb, select),
PADDR (reselect),
SCR_JUMPR ^ IFTRUE (WHEN (SCR_MSG_IN)),
0,
SCR_MOVE_TBL ^ SCR_MSG_OUT,
offsetof (struct dsb, smsg),
#ifdef undef
SCR_JUMPR ^ IFTRUE (WHEN (SCR_MSG_OUT)),
-16,
#endif
SCR_CLR (SCR_ATN),
0,
SCR_COPY (1),
RADDR (sfbr),
NADDR (lastmsg),
SCR_COPY (4),
RADDR (scratcha),
PADDR (startpos),
},{
SCR_COPY_F (4),
RADDR (dsa),
PADDR (loadpos),
SCR_COPY (sizeof (struct head)),
},{
0,
NADDR (header),
SCR_COPY (8),
PADDR (idle),
NADDR (header.launch),
SCR_COPY (sizeof (ticks)),
KVAR (KVAR_TICKS),
NADDR (header.stamp.select),
SCR_COPY (4),
NADDR (header.savep),
RADDR (temp),
SCR_COPY (4),
NADDR (header.status),
RADDR (scr0),
},{
SCR_COPY (1),
NADDR (sync_st),
RADDR (sxfer),
SCR_COPY (1),
NADDR (wide_st),
RADDR (scntl3),
SCR_LOAD_REG (scratcha, MSG_NOOP),
0,
SCR_COPY (1),
RADDR (scratcha),
NADDR (msgout),
SCR_COPY (1),
RADDR (scratcha),
NADDR (msgin),
SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
PADDR (dispatch),
SCR_FROM_REG (sbdl),
0,
SCR_JUMP ^ IFTRUE (DATA (MSG_EXTENDED)),
PADDR (msg_in),
SCR_JUMP ^ IFTRUE (DATA (MSG_MESSAGE_REJECT)),
PADDRH (msg_reject),
SCR_JUMP,
PADDR (dispatch),
},{
SCR_COPY (1),
RADDR (scratcha),
NADDR (msgout),
SCR_SET (SCR_ATN),
0,
},{
SCR_CLR (SCR_ACK),
0,
},{
SCR_FROM_REG (HS_REG),
0,
SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
SIR_NEGO_FAILED,
SCR_REG_REG (socl, SCR_AND, CACK|CATN),
0,
SCR_RETURN ^ IFTRUE (WHEN (SCR_DATA_OUT)),
0,
SCR_RETURN ^ IFTRUE (IF (SCR_DATA_IN)),
0,
SCR_JUMP ^ IFTRUE (IF (SCR_MSG_OUT)),
PADDR (msg_out),
SCR_JUMP ^ IFTRUE (IF (SCR_MSG_IN)),
PADDR (msg_in),
SCR_JUMP ^ IFTRUE (IF (SCR_COMMAND)),
PADDR (command),
SCR_JUMP ^ IFTRUE (IF (SCR_STATUS)),
PADDR (status),
SCR_LOAD_REG (scratcha, XE_BAD_PHASE),
0,
SCR_COPY (1),
RADDR (scratcha),
NADDR (xerr_st),
SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_OUT)),
8,
SCR_MOVE_ABS (1) ^ SCR_ILG_OUT,
NADDR (scratch),
SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_IN)),
8,
SCR_MOVE_ABS (1) ^ SCR_ILG_IN,
NADDR (scratch),
SCR_JUMP,
PADDR (dispatch),
},{
SCR_LOAD_REG (scratcha, XE_EXTRA_DATA),
0,
SCR_COPY (1),
RADDR (scratcha),
NADDR (xerr_st),
SCR_JUMPR ^ IFFALSE (WHEN (SCR_DATA_OUT)),
8,
SCR_MOVE_ABS (1) ^ SCR_DATA_OUT,
NADDR (scratch),
SCR_JUMPR ^ IFFALSE (IF (SCR_DATA_IN)),
8,
SCR_MOVE_ABS (1) ^ SCR_DATA_IN,
NADDR (scratch),
SCR_CALL,
PADDR (dispatch),
SCR_JUMP,
PADDR (no_data),
},{
SCR_FROM_REG (socl),
0,
SCR_JUMP ^ IFFALSE (MASK (CATN, CATN)),
PADDR (dispatch),
SCR_REG_REG (PS_REG, SCR_ADD, 1),
0,
SCR_LOAD_REG (scratcha, MSG_INITIATOR_DET_ERR),
0,
SCR_JUMP,
PADDR (setmsg),
},{
SCR_FROM_REG (SS_REG),
0,
SCR_JUMPR ^ IFTRUE (DATA (SCSI_STATUS_CHECK_COND)),
28,
SCR_COPY (sizeof (ticks)),
KVAR (KVAR_TICKS),
NADDR (header.stamp.command),
SCR_MOVE_TBL ^ SCR_COMMAND,
offsetof (struct dsb, cmd),
SCR_JUMP,
PADDR (dispatch),
SCR_MOVE_TBL ^ SCR_COMMAND,
offsetof (struct dsb, scmd),
SCR_JUMP,
PADDR (dispatch),
},{
SCR_COPY (sizeof (ticks)),
KVAR (KVAR_TICKS),
NADDR (header.stamp.status),
SCR_FROM_REG (SS_REG),
0,
SCR_JUMPR ^ IFFALSE (DATA (SCSI_STATUS_CHECK_COND)),
40,
SCR_MOVE_ABS (1) ^ SCR_STATUS,
NADDR (scratch),
SCR_TO_REG (SS_REG),
0,
SCR_REG_REG (SS_REG, SCR_OR, SCSI_STATUS_SENSE),
0,
SCR_LOAD_REG (HS_REG, HS_COMPLETE),
0,
SCR_JUMP,
PADDR (checkatn),
SCR_MOVE_ABS (1) ^ SCR_STATUS,
NADDR (scratch),
SCR_TO_REG (SS_REG),
0,
SCR_JUMP ^ IFTRUE (DATA (SCSI_STATUS_CHECK_COND)),
PADDR (checkatn),
SCR_LOAD_REG (HS_REG, HS_COMPLETE),
0,
SCR_JUMP,
PADDR (checkatn),
},{
SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
NADDR (msgin[0]),
SCR_TO_REG (scratcha),
0,
SCR_FROM_REG (socl),
0,
SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
PADDRH (msg_parity),
SCR_FROM_REG (scratcha),
0,
SCR_JUMP ^ IFTRUE (DATA (MSG_CMDCOMPLETE)),
PADDR (complete),
SCR_JUMP ^ IFTRUE (DATA (MSG_SAVEDATAPOINTER)),
PADDR (save_dp),
SCR_JUMP ^ IFTRUE (DATA (MSG_RESTOREPOINTERS)),
PADDR (restore_dp),
SCR_JUMP ^ IFTRUE (DATA (MSG_DISCONNECT)),
PADDR (disconnect),
SCR_JUMP ^ IFTRUE (DATA (MSG_EXTENDED)),
PADDRH (msg_extended),
SCR_JUMP ^ IFTRUE (DATA (MSG_NOOP)),
PADDR (clrack),
SCR_JUMP ^ IFTRUE (DATA (MSG_MESSAGE_REJECT)),
PADDRH (msg_reject),
SCR_JUMP ^ IFTRUE (DATA (MSG_IGN_WIDE_RESIDUE)),
PADDRH (msg_ign_residue),
},{
SCR_INT,
SIR_REJECT_SENT,
SCR_LOAD_REG (scratcha, MSG_MESSAGE_REJECT),
0,
SCR_JUMP,
PADDR (setmsg),
},{
SCR_FROM_REG (SS_REG),
0,
SCR_JUMPR ^ IFTRUE (MASK (SCSI_STATUS_SENSE, SCSI_STATUS_SENSE)),
12,
SCR_COPY (4),
RADDR (temp),
NADDR (header.lastp),
SCR_REG_REG (scntl2, SCR_AND, 0x7f),
0,
SCR_CLR (SCR_ACK|SCR_ATN),
0,
SCR_WAIT_DISC,
0,
},{
SCR_FROM_REG (dsa),
0,
SCR_JUMP ^ IFTRUE (DATA (0xff)),
PADDR (signal),
SCR_COPY (4),
RADDR (scr0),
NADDR (header.status),
SCR_COPY_F (4),
RADDR (dsa),
PADDR (cleanup0),
SCR_COPY (sizeof (struct head)),
NADDR (header),
},{
0,
SCR_FROM_REG (HS_REG),
0,
SCR_JUMPR ^ IFFALSE (MASK (0, HS_DONEMASK)),
16,
SCR_FROM_REG (SS_REG),
0,
SCR_JUMP ^ IFTRUE (DATA (SCSI_STATUS_CHECK_COND)),
PADDRH(getcc2),
},{
SCR_FROM_REG (SS_REG),
0,
SCR_INT ^ IFTRUE (DATA (SCSI_STATUS_QUEUE_FULL)),
SIR_STALL_QUEUE,
SCR_LOAD_REG (dsa, 0xff),
0,
SCR_FROM_REG (HS_REG),
0,
SCR_INT_FLY ^ IFFALSE (MASK (0, HS_DONEMASK)),
0,
SCR_JUMP,
PADDR(start),
},{
SCR_COPY (4),
RADDR (temp),
NADDR (header.savep),
SCR_JUMP,
PADDR (clrack),
},{
SCR_COPY (4),
NADDR (header.savep),
RADDR (temp),
SCR_JUMP,
PADDR (clrack),
},{
SCR_FROM_REG (QU_REG),
0,
SCR_JUMPR ^ IFFALSE (MASK (QUIRK_AUTOSAVE, QUIRK_AUTOSAVE)),
12,
SCR_COPY (4),
RADDR (temp),
NADDR (header.savep),
SCR_FROM_REG (temp),
0,
SCR_COPY_F (1),
NADDR (header.savep),
PADDR (disconnect0),
},{
SCR_JUMPR ^ IFTRUE (DATA (1)),
20,
SCR_COPY_F (1),
NADDR (header.goalp),
PADDR (disconnect1),
},{
SCR_INT ^ IFFALSE (DATA (1)),
SIR_MISSING_SAVE,
SCR_REG_REG (scntl2, SCR_AND, 0x7f),
0,
SCR_CLR (SCR_ACK|SCR_ATN),
0,
SCR_WAIT_DISC,
0,
SCR_COPY (sizeof (ticks)),
KVAR (KVAR_TICKS),
NADDR (header.stamp.disconnect),
SCR_COPY (4),
NADDR (disc_phys),
RADDR (temp),
SCR_REG_REG (temp, SCR_ADD, 0x01),
0,
SCR_COPY (4),
RADDR (temp),
NADDR (disc_phys),
SCR_LOAD_REG (HS_REG, HS_DISCONNECT),
0,
SCR_JUMP,
PADDR (cleanup),
},{
SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
NADDR (msgout),
SCR_COPY (1),
RADDR (sfbr),
NADDR (lastmsg),
SCR_JUMP ^ IFTRUE (DATA (MSG_ABORT)),
PADDRH (msg_out_abort),
SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_OUT)),
PADDR (msg_out),
},{
SCR_LOAD_REG (scratcha, MSG_NOOP),
0,
SCR_COPY (4),
RADDR (scratcha),
NADDR (msgout),
SCR_JUMP,
PADDR (dispatch),
},{
SCR_FROM_REG (ctest2),
0,
SCR_JUMP ^ IFTRUE (MASK (CSIGP,CSIGP)),
PADDRH (getcc2),
SCR_INT,
SIR_SENSE_FAILED,
},{
SCR_NO_OP,
0,
SCR_LOAD_REG (dsa, 0xff),
0,
SCR_CLR (SCR_TRG),
0,
SCR_WAIT_RESEL,
PADDR(reselect2),
},{
SCR_NO_OP,
0,
SCR_REG_SFBR (ssid, SCR_AND, 0x8F),
0,
SCR_TO_REG (sdid),
0,
SCR_JUMP,
NADDR (jump_tcb),
},{
SCR_NO_OP,
0,
SCR_FROM_REG (ctest2),
0,
SCR_JUMP ^ IFTRUE (MASK (CSIGP,CSIGP)),
PADDR (start),
SCR_JUMP,
PADDR (reselect),
},{
SCR_COPY (4),
RADDR (temp),
RADDR (dsa),
SCR_JUMP,
PADDR (prepare),
},{
SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_IN)),
48,
SCR_FROM_REG (sbdl),
0,
SCR_JUMPR ^ IFFALSE (MASK (MSG_IDENTIFYFLAG, 0x98)),
32,
SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
NADDR (msgin),
SCR_CLR (SCR_ACK),
0,
SCR_REG_REG (sfbr, SCR_AND, 0x07),
0,
SCR_RETURN,
0,
SCR_LOAD_SFBR (0),
0,
SCR_RETURN,
0,
},{
SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_IN)),
64,
SCR_FROM_REG (sbdl),
0,
SCR_JUMPR ^ IFFALSE (DATA (MSG_SIMPLE_Q_TAG)),
48,
SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
NADDR (msgin),
SCR_CLR (SCR_ACK),
0,
SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_IN)),
24,
SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
NADDR (msgin),
SCR_CLR (SCR_ACK|SCR_CARRY),
0,
SCR_RETURN,
0,
SCR_LOAD_SFBR (0),
0,
SCR_SET (SCR_CARRY),
0,
SCR_RETURN,
0,
},{
0
},{
(u_long)0
}
};
static struct scripth scripth0 = {
{
0
},{
SCR_REG_REG (PS_REG, SCR_ADD, 0x01),
0,
SCR_LOAD_REG (scratcha, MSG_PARITY_ERROR),
0,
SCR_JUMP,
PADDR (setmsg),
},{
SCR_FROM_REG (HS_REG),
0,
SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
SIR_NEGO_FAILED,
SCR_INT ^ IFFALSE (DATA (HS_NEGOTIATE)),
SIR_REJECT_RECEIVED,
SCR_JUMP,
PADDR (clrack),
},{
SCR_CLR (SCR_ACK),
0,
SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
PADDR (dispatch),
SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
NADDR (msgin[1]),
SCR_TO_REG (scratcha),
0,
SCR_FROM_REG (socl),
0,
SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
PADDRH (msg_parity),
SCR_FROM_REG (scratcha),
0,
SCR_JUMP ^ IFTRUE (DATA (0)),
PADDR (clrack),
SCR_JUMPR ^ IFFALSE (DATA (1)),
40,
SCR_FROM_REG (scntl2),
0,
SCR_JUMPR ^ IFFALSE (MASK (WSR, WSR)),
16,
SCR_REG_REG (scntl2, SCR_OR, WSR),
0,
SCR_JUMP,
PADDR (clrack),
SCR_FROM_REG (scratcha),
0,
SCR_INT,
SIR_IGN_RESIDUE,
SCR_JUMP,
PADDR (clrack),
},{
SCR_CLR (SCR_ACK),
0,
SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
PADDR (dispatch),
SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
NADDR (msgin[1]),
SCR_TO_REG (scratcha),
0,
SCR_FROM_REG (socl),
0,
SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
PADDRH (msg_parity),
SCR_FROM_REG (scratcha),
0,
SCR_JUMP ^ IFTRUE (DATA (3)),
PADDRH (msg_ext_3),
SCR_JUMP ^ IFFALSE (DATA (2)),
PADDR (msg_bad),
},{
SCR_CLR (SCR_ACK),
0,
SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
PADDR (dispatch),
SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
NADDR (msgin[2]),
SCR_TO_REG (scratcha),
0,
SCR_FROM_REG (socl),
0,
SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
PADDRH (msg_parity),
SCR_FROM_REG (scratcha),
0,
SCR_JUMP ^ IFTRUE (DATA (MSG_EXT_WDTR)),
PADDRH (msg_wdtr),
SCR_JUMP,
PADDR (msg_bad)
},{
SCR_CLR (SCR_ACK),
0,
SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
PADDR (dispatch),
SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
NADDR (msgin[3]),
SCR_FROM_REG (socl),
0,
SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
PADDRH (msg_parity),
SCR_INT,
SIR_NEGO_WIDE,
SCR_SET (SCR_ATN),
0,
SCR_CLR (SCR_ACK),
0,
SCR_INT ^ IFFALSE (WHEN (SCR_MSG_OUT)),
SIR_NEGO_PROTO,
SCR_MOVE_ABS (4) ^ SCR_MSG_OUT,
NADDR (msgout),
SCR_CLR (SCR_ATN),
0,
SCR_COPY (1),
RADDR (sfbr),
NADDR (lastmsg),
SCR_JUMP,
PADDR (msg_out_done),
},{
SCR_CLR (SCR_ACK),
0,
SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
PADDR (dispatch),
SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
NADDR (msgin[2]),
SCR_TO_REG (scratcha),
0,
SCR_FROM_REG (socl),
0,
SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
PADDRH (msg_parity),
SCR_FROM_REG (scratcha),
0,
SCR_JUMP ^ IFTRUE (DATA (MSG_EXT_SDTR)),
PADDRH (msg_sdtr),
SCR_JUMP,
PADDR (msg_bad)
},{
SCR_CLR (SCR_ACK),
0,
SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
PADDR (dispatch),
SCR_MOVE_ABS (2) ^ SCR_MSG_IN,
NADDR (msgin[3]),
SCR_FROM_REG (socl),
0,
SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
PADDRH (msg_parity),
SCR_INT,
SIR_NEGO_SYNC,
SCR_SET (SCR_ATN),
0,
SCR_CLR (SCR_ACK),
0,
SCR_INT ^ IFFALSE (WHEN (SCR_MSG_OUT)),
SIR_NEGO_PROTO,
SCR_MOVE_ABS (5) ^ SCR_MSG_OUT,
NADDR (msgout),
SCR_CLR (SCR_ATN),
0,
SCR_COPY (1),
RADDR (sfbr),
NADDR (lastmsg),
SCR_JUMP,
PADDR (msg_out_done),
},{
SCR_REG_REG (scntl2, SCR_AND, 0x7f),
0,
SCR_CLR (SCR_ACK|SCR_ATN),
0,
SCR_WAIT_DISC,
0,
SCR_LOAD_REG (HS_REG, HS_ABORTED),
0,
SCR_JUMP,
PADDR (cleanup),
},{
SCR_COPY_F (4),
RADDR (dsa),
PADDRH (getcc1),
SCR_COPY (sizeof (struct head)),
},{
0,
NADDR (header),
SCR_COPY (4),
NADDR (header.status),
RADDR (scr0),
},{
SCR_CALLR,
24,
SCR_MOVE_TBL ^ SCR_DATA_IN,
offsetof (struct dsb, sense),
SCR_CALL,
PADDR (checkatn),
SCR_JUMP,
PADDR (no_data),
SCR_COPY (4),
RADDR (temp),
NADDR (header.savep),
SCR_COPY (4),
PADDR (startpos),
RADDR (scratcha),
#ifdef NCR_GETCC_WITHMSG
SCR_FROM_REG (QU_REG),
0,
SCR_JUMP ^ IFTRUE (MASK (QUIRK_NOMSG, QUIRK_NOMSG)),
PADDRH(getcc3),
SCR_SEL_TBL_ATN ^ offsetof (struct dsb, select),
PADDR(badgetcc),
SCR_MOVE_TBL ^ SCR_MSG_OUT,
offsetof (struct dsb, smsg2),
SCR_CLR (SCR_ATN),
0,
SCR_COPY (1),
RADDR (sfbr),
NADDR (lastmsg),
SCR_JUMP,
PADDR (prepare2),
#endif
},{
SCR_SEL_TBL ^ offsetof (struct dsb, select),
PADDR(badgetcc),
SCR_JUMPR ^ IFTRUE (WHEN (SCR_MSG_IN)),
0,
SCR_JUMP,
PADDR (prepare2),
},{
SCR_LOAD_REG (scratcha, MSG_ABORT_TAG),
0,
SCR_JUMPR ^ IFFALSE (CARRYSET),
8,
},{
SCR_LOAD_REG (scratcha, MSG_ABORT),
0,
SCR_COPY (1),
RADDR (scratcha),
NADDR (msgout),
SCR_SET (SCR_ATN),
0,
SCR_CLR (SCR_ACK),
0,
SCR_REG_REG (scntl2, SCR_AND, 0x7f),
0,
SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
NADDR (msgout),
SCR_COPY (1),
RADDR (sfbr),
NADDR (lastmsg),
SCR_CLR (SCR_ACK|SCR_ATN),
0,
SCR_WAIT_DISC,
0,
SCR_JUMP,
PADDR (start),
},{
SCR_COPY (4),
KVAR (KVAR_NCR_CACHE),
RADDR (scratcha),
SCR_COPY (4),
RADDR (temp),
KVAR (KVAR_NCR_CACHE),
SCR_COPY (4),
KVAR (KVAR_NCR_CACHE),
RADDR (temp),
},{
SCR_INT,
99,
}
};
static void
ncr_script_fill (struct script * scr, struct scripth * scrh)
{
int i;
ncrcmd *p;
p = scrh->tryloop;
for (i=0; i<MAX_START; i++) {
*p++ =SCR_COPY (4);
*p++ =NADDR (squeue[i]);
*p++ =RADDR (dsa);
*p++ =SCR_CALL;
*p++ =PADDR (trysel);
}
*p++ =SCR_JUMP;
*p++ =PADDRH(tryloop);
assert ((char *)p == (char *)&scrh->tryloop + sizeof (scrh->tryloop));
p = scr->data_in;
*p++ =SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_IN));
*p++ =PADDR (no_data);
*p++ =SCR_COPY (sizeof (ticks));
*p++ =(ncrcmd) KVAR (KVAR_TICKS);
*p++ =NADDR (header.stamp.data);
*p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
*p++ =offsetof (struct dsb, data[ 0]);
for (i=1; i<MAX_SCATTER; i++) {
*p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN));
*p++ =PADDR (checkatn);
*p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
*p++ =offsetof (struct dsb, data[i]);
}
*p++ =SCR_CALL;
*p++ =PADDR (checkatn);
*p++ =SCR_JUMP;
*p++ =PADDR (no_data);
assert ((char *)p == (char *)&scr->data_in + sizeof (scr->data_in));
p = scr->data_out;
*p++ =SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_OUT));
*p++ =PADDR (no_data);
*p++ =SCR_COPY (sizeof (ticks));
*p++ =(ncrcmd) KVAR (KVAR_TICKS);
*p++ =NADDR (header.stamp.data);
*p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
*p++ =offsetof (struct dsb, data[ 0]);
for (i=1; i<MAX_SCATTER; i++) {
*p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT));
*p++ =PADDR (dispatch);
*p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
*p++ =offsetof (struct dsb, data[i]);
}
*p++ =SCR_CALL;
*p++ =PADDR (dispatch);
*p++ =SCR_JUMP;
*p++ =PADDR (no_data);
assert ((char *)p == (char *)&scr->data_out + sizeof (scr->data_out));
}
static void ncr_script_copy_and_bind (ncb_p np, ncrcmd *src, ncrcmd *dst, int len)
{
ncrcmd opcode, new, old, tmp1, tmp2;
ncrcmd *start, *end;
int relocs, offset;
start = src;
end = src + len/4;
offset = 0;
while (src < end) {
opcode = *src++;
WRITESCRIPT_OFF(dst, offset, opcode);
offset += 4;
if (opcode == 0) {
kprintf ("%s: ERROR0 IN SCRIPT at %d.\n",
ncr_name(np), (int) (src-start-1));
DELAY (1000000);
}
if (DEBUG_FLAGS & DEBUG_SCRIPT)
kprintf ("%p: <%x>\n",
(src-1), (unsigned)opcode);
switch (opcode >> 28) {
case 0xc:
relocs = 2;
tmp1 = src[0];
if ((tmp1 & RELOC_MASK) == RELOC_KVAR)
tmp1 = 0;
tmp2 = src[1];
if ((tmp2 & RELOC_MASK) == RELOC_KVAR)
tmp2 = 0;
if ((tmp1 ^ tmp2) & 3) {
kprintf ("%s: ERROR1 IN SCRIPT at %d.\n",
ncr_name(np), (int) (src-start-1));
DELAY (1000000);
}
if ((opcode & SCR_NO_FLUSH) && !(np->features&FE_PFEN))
WRITESCRIPT_OFF(dst, offset - 4,
(opcode & ~SCR_NO_FLUSH));
break;
case 0x0:
relocs = 1;
break;
case 0x8:
if (opcode & 0x00800000)
relocs = 0;
else
relocs = 1;
break;
case 0x4:
case 0x5:
case 0x6:
case 0x7:
relocs = 1;
break;
default:
relocs = 0;
break;
}
if (relocs) {
while (relocs--) {
old = *src++;
switch (old & RELOC_MASK) {
case RELOC_REGISTER:
new = (old & ~RELOC_MASK) + rman_get_start(np->reg_res);
break;
case RELOC_LABEL:
new = (old & ~RELOC_MASK) + np->p_script;
break;
case RELOC_LABELH:
new = (old & ~RELOC_MASK) + np->p_scripth;
break;
case RELOC_SOFTC:
new = (old & ~RELOC_MASK) + vtophys(np);
break;
case RELOC_KVAR:
if (((old & ~RELOC_MASK) <
SCRIPT_KVAR_FIRST) ||
((old & ~RELOC_MASK) >
SCRIPT_KVAR_LAST))
panic("ncr KVAR out of range");
new = vtophys(script_kvars[old &
~RELOC_MASK]);
break;
case 0:
if (old == 0) {
new = old;
break;
}
default:
panic("ncr_script_copy_and_bind: weird relocation %x @ %d", old, (int)(src - start));
break;
}
WRITESCRIPT_OFF(dst, offset, new);
offset += 4;
}
} else {
WRITESCRIPT_OFF(dst, offset, *src++);
offset += 4;
}
}
}
#if 0
void ncr_min_phys (struct buf *bp)
{
if ((unsigned long)bp->b_bcount > MAX_SIZE) bp->b_bcount = MAX_SIZE;
}
#endif
#if 0
u_int32_t ncr_info (int unit)
{
return (1);
}
#endif
typedef struct {
unsigned long device_id;
unsigned short minrevid;
char *name;
unsigned char maxburst;
unsigned char maxoffs;
unsigned char clock_divn;
unsigned int features;
} ncr_chip;
static ncr_chip ncr_chip_table[] = {
{NCR_810_ID, 0x00, "ncr 53c810 fast10 scsi", 4, 8, 4,
FE_ERL}
,
{NCR_810_ID, 0x10, "ncr 53c810a fast10 scsi", 4, 8, 4,
FE_ERL|FE_LDSTR|FE_PFEN|FE_BOF}
,
{NCR_815_ID, 0x00, "ncr 53c815 fast10 scsi", 4, 8, 4,
FE_ERL|FE_BOF}
,
{NCR_820_ID, 0x00, "ncr 53c820 fast10 wide scsi", 4, 8, 4,
FE_WIDE|FE_ERL}
,
{NCR_825_ID, 0x00, "ncr 53c825 fast10 wide scsi", 4, 8, 4,
FE_WIDE|FE_ERL|FE_BOF}
,
{NCR_825_ID, 0x10, "ncr 53c825a fast10 wide scsi", 7, 8, 4,
FE_WIDE|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
,
{NCR_860_ID, 0x00, "ncr 53c860 fast20 scsi", 4, 8, 5,
FE_ULTRA|FE_CLK80|FE_CACHE_SET|FE_LDSTR|FE_PFEN}
,
{NCR_875_ID, 0x00, "ncr 53c875 fast20 wide scsi", 7, 16, 5,
FE_WIDE|FE_ULTRA|FE_CLK80|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
,
{NCR_875_ID, 0x02, "ncr 53c875 fast20 wide scsi", 7, 16, 5,
FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
,
{NCR_875_ID2, 0x00, "ncr 53c875j fast20 wide scsi", 7, 16, 5,
FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
,
{NCR_885_ID, 0x00, "ncr 53c885 fast20 wide scsi", 7, 16, 5,
FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
,
{NCR_895_ID, 0x00, "ncr 53c895 fast40 wide scsi", 7, 31, 7,
FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
,
{NCR_896_ID, 0x00, "ncr 53c896 fast40 wide scsi", 7, 31, 7,
FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
,
{NCR_895A_ID, 0x00, "ncr 53c895a fast40 wide scsi", 7, 31, 7,
FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
,
{NCR_1510D_ID, 0x00, "ncr 53c1510d fast40 wide scsi", 7, 31, 7,
FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
};
static int ncr_chip_lookup(u_long device_id, u_char revision_id)
{
int i, found;
found = -1;
for (i = 0; i < NELEM(ncr_chip_table); i++) {
if (device_id == ncr_chip_table[i].device_id &&
ncr_chip_table[i].minrevid <= revision_id) {
if (found < 0 ||
ncr_chip_table[found].minrevid
< ncr_chip_table[i].minrevid) {
found = i;
}
}
}
return found;
}
static int ncr_probe (device_t dev)
{
int i;
i = ncr_chip_lookup(pci_get_devid(dev), pci_get_revid(dev));
if (i >= 0) {
device_set_desc(dev, ncr_chip_table[i].name);
return (-1000);
}
return (ENXIO);
}
#define _5M 5000000
static u_long 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 void
ncr_init_burst(ncb_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 int
ncr_attach (device_t dev)
{
ncb_p np = (struct ncb*) device_get_softc(dev);
u_char rev = 0;
u_long period;
int i, rid;
u_int8_t usrsync;
u_int8_t usrwide;
struct cam_devq *devq;
np->unit = device_get_unit(dev);
np->reg_rid = 0x14;
np->reg_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &np->reg_rid,
0, ~0, 1, RF_ACTIVE);
if (!np->reg_res) {
device_printf(dev, "could not map memory\n");
return ENXIO;
}
np->bst = rman_get_bustag(np->reg_res);
np->bsh = rman_get_bushandle(np->reg_res);
#ifdef NCR_IOMAPPED
if (!pci_map_port (config_id, 0x10, &np->port))
return;
#endif
np->rv_scntl3 = INB(nc_scntl3) & 0x77;
np->rv_dmode = INB(nc_dmode) & 0xce;
np->rv_dcntl = INB(nc_dcntl) & 0xa9;
np->rv_ctest3 = INB(nc_ctest3) & 0x01;
np->rv_ctest4 = INB(nc_ctest4) & 0x88;
np->rv_ctest5 = INB(nc_ctest5) & 0x24;
np->rv_gpcntl = INB(nc_gpcntl);
np->rv_stest2 = INB(nc_stest2) & 0x20;
if (bootverbose >= 2) {
kprintf ("\tBIOS values: SCNTL3:%02x DMODE:%02x DCNTL:%02x\n",
np->rv_scntl3, np->rv_dmode, np->rv_dcntl);
kprintf ("\t CTEST3:%02x CTEST4:%02x CTEST5:%02x\n",
np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
}
np->rv_dcntl |= NOCOM;
rev = pci_get_revid(dev);
i = ncr_chip_lookup(pci_get_devid(dev), rev);
if (i >= 0) {
np->maxburst = ncr_chip_table[i].maxburst;
np->maxoffs = ncr_chip_table[i].maxoffs;
np->clock_divn = ncr_chip_table[i].clock_divn;
np->features = ncr_chip_table[i].features;
} else {
np->maxburst = 4;
np->maxoffs = 8;
np->clock_divn = 4;
np->features = FE_ERL;
}
np->maxwide = np->features & FE_WIDE ? 1 : 0;
np->clock_khz = np->features & FE_CLK80 ? 80000 : 40000;
if (np->features & FE_QUAD) np->multiplier = 4;
else if (np->features & FE_DBLR) np->multiplier = 2;
else np->multiplier = 1;
if (np->features & (FE_ULTRA|FE_ULTRA2))
ncr_getclock(np, np->multiplier);
#ifdef NCR_TEKRAM_EEPROM
if (bootverbose) {
kprintf ("%s: Tekram EEPROM read %s\n",
ncr_name(np),
read_tekram_eeprom (np, NULL) ?
"succeeded" : "failed");
}
#endif
if (np->rv_scntl3)
np->features |= FE_BIOS;
i = np->clock_divn - 1;
while (i >= 0) {
--i;
if (10ul * SCSI_NCR_MIN_ASYNC * np->clock_khz > div_10M[i]) {
++i;
break;
}
}
np->rv_scntl3 = i+1;
period = (4 * div_10M[0] + np->clock_khz - 1) / 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 = (period + 40 - 1) / 40;
if (np->minsync < 25 && !(np->features & (FE_ULTRA|FE_ULTRA2)))
np->minsync = 25;
else if (np->minsync < 12 && !(np->features & FE_ULTRA2))
np->minsync = 12;
period = (11 * div_10M[np->clock_divn - 1]) / (4 * np->clock_khz);
np->maxsync = period > 2540 ? 254 : period / 10;
#ifdef SCSI_NCR_SYMBIOS_COMPAT
if (!(np->rv_gpcntl & 0x01))
np->features |= FE_LED0;
#if 0
if (!(INB(nc_gpreg) & 0x08))
np->features |= FE_DIFF;
#endif
#endif
#ifdef SCSI_NCR_TRUST_BIOS
if (!(np->features & FE_BIOS)) {
#else
if (1) {
#endif
np->rv_dmode = 0;
np->rv_dcntl = NOCOM;
np->rv_ctest3 = 0;
np->rv_ctest4 = MPEE;
np->rv_ctest5 = 0;
np->rv_stest2 = 0;
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 (np->features & FE_CLSE)
np->rv_dcntl |= CLSE;
if (np->features & FE_WRIE)
np->rv_ctest3 |= WRIE;
if (np->features & FE_PFEN)
np->rv_dcntl |= PFEN;
if (np->features & FE_DFS)
np->rv_ctest5 |= DFS;
if (np->features & FE_DIFF)
np->rv_stest2 |= 0x20;
ncr_init_burst(np, np->maxburst);
} else {
np->maxburst =
burst_code(np->rv_dmode, np->rv_ctest4, np->rv_ctest5);
}
if ((np->features & FE_RAM) && sizeof(struct script) <= 4096) {
np->sram_rid = 0x18;
np->sram_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
&np->sram_rid,
0, ~0, 1, RF_ACTIVE);
}
if (np->sram_res != NULL) {
np->script = NULL;
np->p_script = rman_get_start(np->sram_res);
np->bst2 = rman_get_bustag(np->sram_res);
np->bsh2 = rman_get_bushandle(np->sram_res);
} else if (sizeof (struct script) > PAGE_SIZE) {
np->script = (struct script*) kmem_alloc_contig
(round_page(sizeof (struct script)),
0, 0xffffffff, PAGE_SIZE);
} else {
np->script = (struct script *)
kmalloc (sizeof (struct script), M_DEVBUF, M_WAITOK);
}
if (sizeof (struct scripth) > PAGE_SIZE) {
np->scripth = (struct scripth*) kmem_alloc_contig
(round_page(sizeof (struct scripth)),
0, 0xffffffff, PAGE_SIZE);
} else
{
np->scripth = (struct scripth *)
kmalloc (sizeof (struct scripth), M_DEVBUF, M_WAITOK);
}
#ifdef SCSI_NCR_PCI_CONFIG_FIXUP
#ifdef PCIR_CACHELNSZ
{
u_char cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
u_short command = pci_read_config(dev, PCIR_COMMAND, 2);
if (!cachelnsz) {
cachelnsz = 8;
kprintf("%s: setting PCI cache line size register to %d.\n",
ncr_name(np), (int)cachelnsz);
pci_write_config(dev, PCIR_CACHELNSZ, cachelnsz, 1);
}
if (!(command & (1<<4))) {
command |= (1<<4);
kprintf("%s: setting PCI command write and invalidate.\n",
ncr_name(np));
pci_write_config(dev, PCIR_COMMAND, command, 2);
}
}
#endif
#endif
usrsync = 0;
if (SCSI_NCR_DFLT_SYNC) {
usrsync = SCSI_NCR_DFLT_SYNC;
if (usrsync > np->maxsync)
usrsync = np->maxsync;
if (usrsync < np->minsync)
usrsync = np->minsync;
}
usrwide = (SCSI_NCR_MAX_WIDE);
if (usrwide > np->maxwide) usrwide=np->maxwide;
for (i=0;i<MAX_TARGET;i++) {
tcb_p tp = &np->target[i];
tp->tinfo.user.period = usrsync;
tp->tinfo.user.offset = usrsync != 0 ? np->maxoffs : 0;
tp->tinfo.user.width = usrwide;
tp->tinfo.disc_tag = NCR_CUR_DISCENB
| NCR_CUR_TAGENB
| NCR_USR_DISCENB
| NCR_USR_TAGENB;
}
if (bootverbose)
kprintf("%s: minsync=%d, maxsync=%d, maxoffs=%d, %d dwords burst, %s dma fifo\n",
ncr_name(np), np->minsync, np->maxsync, np->maxoffs,
burst_length(np->maxburst),
(np->rv_ctest5 & DFS) ? "large" : "normal");
if (bootverbose)
kprintf("%s: %s, %s IRQ driver%s\n",
ncr_name(np),
np->rv_stest2 & 0x20 ? "differential" : "single-ended",
np->rv_dcntl & IRQM ? "totem pole" : "open drain",
np->sram_res ? ", using on-chip SRAM" : "");
ncr_script_fill (&script0, &scripth0);
if (np->script)
np->p_script = vtophys(np->script);
np->p_scripth = vtophys(np->scripth);
ncr_script_copy_and_bind (np, (ncrcmd *) &script0,
(ncrcmd *) np->script, sizeof(struct script));
ncr_script_copy_and_bind (np, (ncrcmd *) &scripth0,
(ncrcmd *) np->scripth, sizeof(struct scripth));
if (np->features & FE_LED0) {
WRITESCRIPT(reselect[0], SCR_REG_REG(gpreg, SCR_OR, 0x01));
WRITESCRIPT(reselect1[0], SCR_REG_REG(gpreg, SCR_AND, 0xfe));
WRITESCRIPT(reselect2[0], SCR_REG_REG(gpreg, SCR_AND, 0xfe));
}
np->jump_tcb.l_cmd = SCR_JUMP;
np->jump_tcb.l_paddr = NCB_SCRIPTH_PHYS (np, abort);
np->myaddr = INB(nc_scid) & 0x07;
if (!np->myaddr) np->myaddr = SCSI_NCR_MYADDR;
#ifdef NCR_DUMP_REG
{
int reg;
for (reg=0; reg<256; reg+=4) {
if (reg%16==0) kprintf ("reg[%2x]", reg);
kprintf (" %08x", (int)pci_conf_read (config_id, reg));
if (reg%16==12) kprintf ("\n");
}
}
#endif
OUTB (nc_istat, SRST);
DELAY (1000);
OUTB (nc_istat, 0 );
if (ncr_snooptest (np)) {
kprintf ("CACHE INCORRECTLY CONFIGURED.\n");
return EINVAL;
}
rid = 0;
np->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
RF_SHAREABLE | RF_ACTIVE);
if (np->irq_res == NULL) {
device_printf(dev,
"interruptless mode: reduced performance.\n");
} else {
bus_setup_intr(dev, np->irq_res, 0,
ncr_intr, np, &np->irq_handle, NULL);
}
devq = cam_simq_alloc(MAX_START - 1);
if (devq == NULL)
return ENOMEM;
np->sim = cam_sim_alloc(ncr_action, ncr_poll, "ncr", np, np->unit,
&sim_mplock, 1, MAX_TAGS, devq);
cam_simq_release(devq);
if (np->sim == NULL)
return ENOMEM;
if (xpt_bus_register(np->sim, 0) != CAM_SUCCESS) {
cam_sim_free(np->sim);
return ENOMEM;
}
if (xpt_create_path(&np->path, NULL,
cam_sim_path(np->sim), CAM_TARGET_WILDCARD,
CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
xpt_bus_deregister(cam_sim_path(np->sim));
cam_sim_free(np->sim);
return ENOMEM;
}
callout_init(&np->timeout_ch);
ncr_timeout (np);
np->lasttime=0;
return 0;
}
static void
ncr_intr(void *vnp)
{
ncb_p np = vnp;
crit_enter();
if (DEBUG_FLAGS & DEBUG_TINY) kprintf ("[");
if (INB(nc_istat) & (INTF|SIP|DIP)) {
do {
ncr_exception (np);
} while (INB(nc_istat) & (INTF|SIP|DIP));
np->ticks = 100;
}
if (DEBUG_FLAGS & DEBUG_TINY) kprintf ("]\n");
crit_exit();
}
static void
ncr_action (struct cam_sim *sim, union ccb *ccb)
{
ncb_p np;
np = (ncb_p) cam_sim_softc(sim);
switch (ccb->ccb_h.func_code) {
case XPT_SCSI_IO:
{
nccb_p cp;
lcb_p lp;
tcb_p tp;
struct ccb_scsiio *csio;
u_int8_t *msgptr;
u_int msglen;
u_int msglen2;
int segments;
u_int8_t nego;
u_int8_t idmsg;
int qidx;
tp = &np->target[ccb->ccb_h.target_id];
csio = &ccb->csio;
crit_enter();
if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
xpt_done(ccb);
crit_exit();
return;
}
ccb->ccb_h.status |= CAM_SIM_QUEUED;
cp = ncr_get_nccb (np, ccb->ccb_h.target_id,
ccb->ccb_h.target_lun);
if (cp == NULL) {
ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
xpt_done(ccb);
crit_exit();
return;
}
cp->ccb = ccb;
bzero (&cp->phys.header.stamp, sizeof (struct tstamp));
cp->phys.header.stamp.start = ticks;
nego = 0;
if (tp->nego_cp == NULL) {
if (tp->tinfo.current.width
!= tp->tinfo.goal.width) {
tp->nego_cp = cp;
nego = NS_WIDE;
} else if ((tp->tinfo.current.period
!= tp->tinfo.goal.period)
|| (tp->tinfo.current.offset
!= tp->tinfo.goal.offset)) {
tp->nego_cp = cp;
nego = NS_SYNC;
}
}
lp = tp->lp[ccb->ccb_h.target_lun];
if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0
&& (ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
&& (nego == 0)) {
while (!cp->tag) {
nccb_p cp2 = lp->next_nccb;
lp->lasttag = lp->lasttag % 255 + 1;
while (cp2 && cp2->tag != lp->lasttag)
cp2 = cp2->next_nccb;
if (cp2) continue;
cp->tag=lp->lasttag;
if (DEBUG_FLAGS & DEBUG_TAGS) {
PRINT_ADDR(ccb);
kprintf ("using tag #%d.\n", cp->tag);
}
}
} else {
cp->tag=0;
}
idmsg = MSG_IDENTIFYFLAG | ccb->ccb_h.target_lun;
if (tp->tinfo.disc_tag & NCR_CUR_DISCENB)
idmsg |= MSG_IDENTIFY_DISCFLAG;
msgptr = cp->scsi_smsg;
msglen = 0;
msgptr[msglen++] = idmsg;
if (cp->tag) {
msgptr[msglen++] = ccb->csio.tag_action;
msgptr[msglen++] = cp->tag;
}
switch (nego) {
case NS_SYNC:
msgptr[msglen++] = MSG_EXTENDED;
msgptr[msglen++] = MSG_EXT_SDTR_LEN;
msgptr[msglen++] = MSG_EXT_SDTR;
msgptr[msglen++] = tp->tinfo.goal.period;
msgptr[msglen++] = tp->tinfo.goal.offset;
if (DEBUG_FLAGS & DEBUG_NEGO) {
PRINT_ADDR(ccb);
kprintf ("sync msgout: ");
ncr_show_msg (&cp->scsi_smsg [msglen-5]);
kprintf (".\n");
};
break;
case NS_WIDE:
msgptr[msglen++] = MSG_EXTENDED;
msgptr[msglen++] = MSG_EXT_WDTR_LEN;
msgptr[msglen++] = MSG_EXT_WDTR;
msgptr[msglen++] = tp->tinfo.goal.width;
if (DEBUG_FLAGS & DEBUG_NEGO) {
PRINT_ADDR(ccb);
kprintf ("wide msgout: ");
ncr_show_msg (&cp->scsi_smsg [msglen-4]);
kprintf (".\n");
};
break;
}
cp->scsi_smsg2 [0] = idmsg;
msglen2 = 1;
if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
segments = ncr_scatter(&cp->phys,
(vm_offset_t)csio->data_ptr,
(vm_size_t)csio->dxfer_len);
if (segments < 0) {
ccb->ccb_h.status = CAM_REQ_TOO_BIG;
ncr_free_nccb(np, cp);
crit_exit();
xpt_done(ccb);
return;
}
if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
cp->phys.header.savep = NCB_SCRIPT_PHYS (np, data_in);
cp->phys.header.goalp = cp->phys.header.savep +20 +segments*16;
} else {
cp->phys.header.savep = NCB_SCRIPT_PHYS (np, data_out);
cp->phys.header.goalp = cp->phys.header.savep +20 +segments*16;
}
} else {
cp->phys.header.savep = NCB_SCRIPT_PHYS (np, no_data);
cp->phys.header.goalp = cp->phys.header.savep;
}
cp->phys.header.lastp = cp->phys.header.savep;
cp->phys.header.cp = cp;
cp->phys.header.launch.l_paddr = NCB_SCRIPT_PHYS (np, select);
cp->phys.header.launch.l_cmd = SCR_JUMP;
cp->phys.select.sel_id = ccb->ccb_h.target_id;
cp->phys.select.sel_scntl3 = tp->tinfo.wval;
cp->phys.select.sel_sxfer = tp->tinfo.sval;
cp->phys.smsg.addr = CCB_PHYS (cp, scsi_smsg);
cp->phys.smsg.size = msglen;
cp->phys.smsg2.addr = CCB_PHYS (cp, scsi_smsg2);
cp->phys.smsg2.size = msglen2;
cp->phys.cmd.addr = vtophys (csio->cdb_io.cdb_bytes);
cp->phys.cmd.size = csio->cdb_len;
cp->phys.scmd.addr = CCB_PHYS (cp, sensecmd);
cp->phys.scmd.size = 6;
cp->sensecmd[0] = 0x03;
cp->sensecmd[1] = ccb->ccb_h.target_lun << 5;
cp->sensecmd[4] = csio->sense_len;
cp->phys.sense.addr = vtophys (&csio->sense_data);
cp->phys.sense.size = csio->sense_len;
cp->actualquirks = QUIRK_NOMSG;
cp->host_status = nego ? HS_NEGOTIATE : HS_BUSY;
cp->s_status = SCSI_STATUS_ILLEGAL;
cp->parity_status = 0;
cp->xerr_status = XE_OK;
cp->sync_status = tp->tinfo.sval;
cp->nego_status = nego;
cp->wide_status = tp->tinfo.wval;
cp->jump_nccb.l_cmd = (SCR_JUMP ^ IFFALSE (DATA (cp->tag)));
cp->tlimit = time_uptime
+ ccb->ccb_h.timeout / 1000 + 2;
cp->magic = CCB_MAGIC;
qidx = np->squeueput + 1;
if (qidx >= MAX_START)
qidx = 0;
np->squeue [qidx ] = NCB_SCRIPT_PHYS (np, idle);
np->squeue [np->squeueput] = CCB_PHYS (cp, phys);
np->squeueput = qidx;
if(DEBUG_FLAGS & DEBUG_QUEUE)
kprintf("%s: queuepos=%d tryoffset=%d.\n",
ncr_name (np), np->squeueput,
(unsigned)(READSCRIPT(startpos[0]) -
(NCB_SCRIPTH_PHYS (np, tryloop))));
OUTB (nc_istat, SIGP);
crit_exit();
break;
}
case XPT_RESET_DEV:
case XPT_EN_LUN:
case XPT_TARGET_IO:
case XPT_ACCEPT_TARGET_IO:
case XPT_CONT_TARGET_IO:
case XPT_ABORT:
ccb->ccb_h.status = CAM_REQ_INVALID;
xpt_done(ccb);
break;
case XPT_SET_TRAN_SETTINGS:
{
struct ccb_trans_settings *cts = &ccb->cts;
tcb_p tp;
u_int update_type;
struct ccb_trans_settings_scsi *scsi =
&cts->proto_specific.scsi;
struct ccb_trans_settings_spi *spi =
&cts->xport_specific.spi;
update_type = 0;
if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
update_type |= NCR_TRANS_GOAL;
if (cts->type == CTS_TYPE_USER_SETTINGS)
update_type |= NCR_TRANS_USER;
crit_enter();
tp = &np->target[ccb->ccb_h.target_id];
if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
if (update_type & NCR_TRANS_GOAL) {
if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
tp->tinfo.disc_tag |= NCR_CUR_DISCENB;
else
tp->tinfo.disc_tag &= ~NCR_CUR_DISCENB;
}
if (update_type & NCR_TRANS_USER) {
if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
tp->tinfo.disc_tag |= NCR_USR_DISCENB;
else
tp->tinfo.disc_tag &= ~NCR_USR_DISCENB;
}
}
if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
if (update_type & NCR_TRANS_GOAL) {
if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
tp->tinfo.disc_tag |= NCR_CUR_TAGENB;
else
tp->tinfo.disc_tag &= ~NCR_CUR_TAGENB;
}
if (update_type & NCR_TRANS_USER) {
if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
tp->tinfo.disc_tag |= NCR_USR_TAGENB;
else
tp->tinfo.disc_tag &= ~NCR_USR_TAGENB;
}
}
if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
if (spi->bus_width > np->maxwide)
spi->bus_width = np->maxwide;
}
if (((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
|| ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)) {
if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) {
if (spi->sync_period != 0
&& (spi->sync_period < np->minsync))
spi->sync_period = np->minsync;
}
if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0) {
if (spi->sync_offset == 0)
spi->sync_period = 0;
if (spi->sync_offset > np->maxoffs)
spi->sync_offset = np->maxoffs;
}
}
if ((update_type & NCR_TRANS_USER) != 0) {
if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
tp->tinfo.user.period = spi->sync_period;
if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
tp->tinfo.user.offset = spi->sync_offset;
if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
tp->tinfo.user.width = spi->bus_width;
}
if ((update_type & NCR_TRANS_GOAL) != 0) {
if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
tp->tinfo.goal.period = spi->sync_period;
if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
tp->tinfo.goal.offset = spi->sync_offset;
if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
tp->tinfo.goal.width = spi->bus_width;
}
crit_exit();
ccb->ccb_h.status = CAM_REQ_CMP;
xpt_done(ccb);
break;
}
case XPT_GET_TRAN_SETTINGS:
{
struct ccb_trans_settings *cts = &ccb->cts;
struct ncr_transinfo *tinfo;
tcb_p tp = &np->target[ccb->ccb_h.target_id];
struct ccb_trans_settings_scsi *scsi =
&cts->proto_specific.scsi;
struct ccb_trans_settings_spi *spi =
&cts->xport_specific.spi;
cts->protocol = PROTO_SCSI;
cts->protocol_version = SCSI_REV_2;
cts->transport = XPORT_SPI;
cts->transport_version = 2;
crit_enter();
if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
tinfo = &tp->tinfo.current;
if (tp->tinfo.disc_tag & NCR_CUR_DISCENB)
spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
else
spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
if (tp->tinfo.disc_tag & NCR_CUR_TAGENB)
scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
else
scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
} else {
tinfo = &tp->tinfo.user;
if (tp->tinfo.disc_tag & NCR_USR_DISCENB)
spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
else
spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
if (tp->tinfo.disc_tag & NCR_USR_TAGENB)
scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
else
scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
}
spi->sync_period = tinfo->period;
spi->sync_offset = tinfo->offset;
spi->bus_width = tinfo->width;
crit_exit();
spi->valid = CTS_SPI_VALID_SYNC_RATE
| CTS_SPI_VALID_SYNC_OFFSET
| CTS_SPI_VALID_BUS_WIDTH
| CTS_SPI_VALID_DISC;
scsi->valid = CTS_SCSI_VALID_TQ;
ccb->ccb_h.status = CAM_REQ_CMP;
xpt_done(ccb);
break;
}
case XPT_CALC_GEOMETRY:
{
struct ccb_calc_geometry *ccg;
u_int32_t size_mb;
u_int32_t secs_per_cylinder;
int extended;
extended = 1;
ccg = &ccb->ccg;
size_mb = ccg->volume_size
/ ((1024L * 1024L) / ccg->block_size);
if (size_mb > 1024 && extended) {
ccg->heads = 255;
ccg->secs_per_track = 63;
} else {
ccg->heads = 64;
ccg->secs_per_track = 32;
}
secs_per_cylinder = ccg->heads * ccg->secs_per_track;
ccg->cylinders = ccg->volume_size / secs_per_cylinder;
ccb->ccb_h.status = CAM_REQ_CMP;
xpt_done(ccb);
break;
}
case XPT_RESET_BUS:
{
OUTB (nc_scntl1, CRST);
ccb->ccb_h.status = CAM_REQ_CMP;
DELAY(10000);
xpt_done(ccb);
break;
}
case XPT_TERM_IO:
ccb->ccb_h.status = CAM_REQ_INVALID;
xpt_done(ccb);
break;
case XPT_PATH_INQ:
{
struct ccb_pathinq *cpi = &ccb->cpi;
cpi->version_num = 1;
cpi->hba_inquiry = 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 = 0;
cpi->hba_eng_cnt = 0;
cpi->max_target = (np->features & FE_WIDE) ? 15 : 7;
cpi->max_lun = MAX_LUN - 1;
cpi->initiator_id = np->myaddr;
cpi->bus_id = cam_sim_bus(sim);
cpi->base_transfer_speed = 3300;
strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
strncpy(cpi->hba_vid, "Symbios", HBA_IDLEN);
strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
cpi->unit_number = cam_sim_unit(sim);
cpi->transport = XPORT_SPI;
cpi->transport_version = 2;
cpi->protocol = PROTO_SCSI;
cpi->protocol_version = SCSI_REV_2;
cpi->ccb_h.status = CAM_REQ_CMP;
xpt_done(ccb);
break;
}
default:
ccb->ccb_h.status = CAM_REQ_INVALID;
xpt_done(ccb);
break;
}
}
static void
ncr_complete (ncb_p np, nccb_p cp)
{
union ccb *ccb;
tcb_p tp;
if (!cp || (cp->magic!=CCB_MAGIC) || !cp->ccb) return;
cp->magic = 1;
cp->tlimit= 0;
cp->jump_nccb.l_cmd = (SCR_JUMP);
cp->phys.header.launch.l_paddr= NCB_SCRIPT_PHYS (np, idle);
ncb_profile (np, cp);
if (DEBUG_FLAGS & DEBUG_TINY)
kprintf ("CCB=%x STAT=%x/%x\n", (int)(intptr_t)cp & 0xfff,
cp->host_status,cp->s_status);
ccb = cp->ccb;
cp->ccb = NULL;
tp = &np->target[ccb->ccb_h.target_id];
if (cp == tp->nego_cp)
tp->nego_cp = NULL;
if (cp->parity_status) {
PRINT_ADDR(ccb);
kprintf ("%d parity error(s), fallback.\n", cp->parity_status);
tp->tinfo.goal.period = 0;
tp->tinfo.goal.offset = 0;
}
if (cp->xerr_status != XE_OK) {
PRINT_ADDR(ccb);
switch (cp->xerr_status) {
case XE_EXTRA_DATA:
kprintf ("extraneous data discarded.\n");
break;
case XE_BAD_PHASE:
kprintf ("illegal scsi phase (4/5).\n");
break;
default:
kprintf ("extended error %d.\n", cp->xerr_status);
break;
}
if (cp->host_status==HS_COMPLETE)
cp->host_status = HS_FAIL;
}
if (cp->host_status == HS_COMPLETE) {
if (cp->s_status == SCSI_STATUS_OK) {
tp->bytes += ccb->csio.dxfer_len;
tp->transfers ++;
ccb->ccb_h.status = CAM_REQ_CMP;
} else if ((cp->s_status & SCSI_STATUS_SENSE) != 0) {
ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
cp->s_status &= ~SCSI_STATUS_SENSE;
if (cp->s_status == SCSI_STATUS_OK) {
ccb->ccb_h.status =
CAM_AUTOSNS_VALID|CAM_SCSI_STATUS_ERROR;
} else {
ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
}
} else {
ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
ccb->csio.scsi_status = cp->s_status;
}
} else if (cp->host_status == HS_SEL_TIMEOUT) {
ccb->ccb_h.status = CAM_SEL_TIMEOUT;
} else if (cp->host_status == HS_TIMEOUT) {
ccb->ccb_h.status = CAM_CMD_TIMEOUT;
} else if (cp->host_status == HS_STALL) {
ccb->ccb_h.status = CAM_REQUEUE_REQ;
} else {
PRINT_ADDR(ccb);
kprintf ("COMMAND FAILED (%x %x) @%p.\n",
cp->host_status, cp->s_status, cp);
ccb->ccb_h.status = CAM_CMD_TIMEOUT;
}
if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
xpt_freeze_devq(ccb->ccb_h.path, 1);
ccb->ccb_h.status |= CAM_DEV_QFRZN;
}
ncr_free_nccb (np, cp);
xpt_done (ccb);
}
static void
ncr_wakeup (ncb_p np, u_long code)
{
nccb_p cp = np->link_nccb;
while (cp) {
switch (cp->host_status) {
case HS_IDLE:
break;
case HS_DISCONNECT:
if(DEBUG_FLAGS & DEBUG_TINY) kprintf ("D");
case HS_BUSY:
case HS_NEGOTIATE:
if (!code) break;
cp->host_status = code;
default:
ncr_complete (np, cp);
break;
}
cp = cp -> link_nccb;
}
}
static void
ncr_freeze_devq (ncb_p np, struct cam_path *path)
{
nccb_p cp;
int i;
int count;
int firstskip;
cp = np->link_nccb;
count = 0;
firstskip = 0;
while (cp) {
switch (cp->host_status) {
case HS_BUSY:
case HS_NEGOTIATE:
if ((cp->phys.header.launch.l_paddr
== NCB_SCRIPT_PHYS (np, select))
&& (xpt_path_comp(path, cp->ccb->ccb_h.path) >= 0)) {
for (i = 1; i < MAX_START; i++) {
int idx;
idx = np->squeueput - i;
if (idx < 0)
idx = MAX_START + idx;
if (np->squeue[idx]
== CCB_PHYS(cp, phys)) {
np->squeue[idx] =
NCB_SCRIPT_PHYS (np, skip);
if (i > firstskip)
firstskip = i;
break;
}
}
cp->host_status=HS_STALL;
ncr_complete (np, cp);
count++;
}
break;
default:
break;
}
cp = cp->link_nccb;
}
if (count > 0) {
int j;
int bidx;
j = 0;
bidx = np->squeueput;
i = np->squeueput - firstskip;
if (i < 0)
i = MAX_START + i;
for (;;) {
bidx = i - j;
if (bidx < 0)
bidx = MAX_START + bidx;
if (np->squeue[i] == NCB_SCRIPT_PHYS (np, skip)) {
j++;
} else if (j != 0) {
np->squeue[bidx] = np->squeue[i];
if (np->squeue[bidx]
== NCB_SCRIPT_PHYS(np, idle))
break;
}
i = (i + 1) % MAX_START;
}
np->squeueput = bidx;
}
}
static void
ncr_init(ncb_p np, char * msg, u_long code)
{
int i;
OUTB (nc_istat, SRST);
DELAY (1000);
OUTB (nc_istat, 0);
if (msg) kprintf ("%s: restart (%s).\n", ncr_name (np), msg);
for (i=0;i<MAX_START;i++)
np -> squeue [i] = NCB_SCRIPT_PHYS (np, idle);
np->squeueput = 0;
WRITESCRIPT(startpos[0], NCB_SCRIPTH_PHYS (np, tryloop));
WRITESCRIPT(start0 [0], SCR_INT ^ IFFALSE (0));
ncr_wakeup (np, code);
OUTB (nc_istat, 0x00 );
OUTB (nc_scntl0, 0xca );
OUTB (nc_scntl1, 0x00 );
ncr_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_dcntl , np->rv_dcntl);
OUTB (nc_ctest3, np->rv_ctest3);
OUTB (nc_ctest5, np->rv_ctest5);
OUTB (nc_ctest4, np->rv_ctest4);
OUTB (nc_stest2, np->rv_stest2|EXT);
OUTB (nc_stest3, TE );
OUTB (nc_stime0, 0x0b );
if (bootverbose >= 2) {
kprintf ("\tACTUAL values:SCNTL3:%02x DMODE:%02x DCNTL:%02x\n",
np->rv_scntl3, np->rv_dmode, np->rv_dcntl);
kprintf ("\t CTEST3:%02x CTEST4:%02x CTEST5:%02x\n",
np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
}
if (np->features & FE_LED0) {
OUTOFFB (nc_gpcntl, 0x01);
}
for (i=0;i<MAX_TARGET;i++) {
tcb_p tp = &np->target[i];
tp->tinfo.sval = 0;
tp->tinfo.wval = np->rv_scntl3;
tp->tinfo.current.period = 0;
tp->tinfo.current.offset = 0;
tp->tinfo.current.width = MSG_EXT_WDTR_BUS_8_BIT;
}
OUTW (nc_sien , STO|HTH|MA|SGE|UDC|RST);
OUTB (nc_dien , MDPE|BF|ABRT|SSI|SIR|IID);
OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, start));
if (code == HS_RESET)
xpt_async(AC_BUS_RESET, np->path, NULL);
}
static void
ncr_poll(struct cam_sim *sim)
{
ncr_intr(cam_sim_softc(sim));
}
static void ncr_getsync(ncb_p np, u_char sfac, u_char *fakp, u_char *scntl3p)
{
u_long clk = np->clock_khz;
int div = np->clock_divn;
u_long fak;
u_long per;
u_long kpc;
if (sfac <= 10) per = 250;
else if (sfac == 11) per = 303;
else if (sfac == 12) per = 500;
else per = 40 * sfac;
kpc = per * clk;
while (--div >= 0)
if (kpc >= (div_10M[div] * 4)) break;
fak = (kpc - 1) / div_10M[div] + 1;
#if 0
per = (fak * div_10M[div]) / clk;
if (div >= 1 && fak < 6) {
u_long fak2, per2;
fak2 = (kpc - 1) / div_10M[div-1] + 1;
per2 = (fak2 * div_10M[div-1]) / clk;
if (per2 < per && fak2 <= 6) {
fak = fak2;
per = per2;
--div;
}
}
#endif
if (fak < 4) fak = 4;
*fakp = fak - 4;
*scntl3p = ((div+1) << 4) + (sfac < 25 ? 0x80 : 0);
}
static void
ncr_setsync(ncb_p np, nccb_p cp, u_char scntl3, u_char sxfer, u_char period)
{
union ccb *ccb;
struct ccb_trans_settings *neg;
tcb_p tp;
int div;
u_int target = INB (nc_sdid) & 0x0f;
u_int period_10ns;
assert (cp);
if (!cp) return;
ccb = cp->ccb;
assert (ccb);
if (!ccb) return;
assert (target == ccb->ccb_h.target_id);
tp = &np->target[target];
if (!scntl3 || !(sxfer & 0x1f))
scntl3 = np->rv_scntl3;
scntl3 = (scntl3 & 0xf0) | (tp->tinfo.wval & EWS)
| (np->rv_scntl3 & 0x07);
div = ((scntl3 >> 4) & 0x7);
if ((sxfer & 0x1f) && div)
period_10ns =
(((sxfer>>5)+4)*div_10M[div-1])/np->clock_khz;
else
period_10ns = 0;
tp->tinfo.goal.period = period;
tp->tinfo.goal.offset = sxfer & 0x1f;
tp->tinfo.current.period = period;
tp->tinfo.current.offset = sxfer & 0x1f;
if (tp->tinfo.sval == sxfer && tp->tinfo.wval == scntl3) return;
tp->tinfo.sval = sxfer;
tp->tinfo.wval = scntl3;
if (sxfer & 0x1f) {
if (period_10ns <= 2000) OUTOFFB (nc_stest2, EXT);
}
neg = &xpt_alloc_ccb()->cts;
neg->protocol = PROTO_SCSI;
neg->protocol_version = SCSI_REV_2;
neg->transport = XPORT_SPI;
neg->transport_version = 2;
neg->xport_specific.spi.sync_period = period;
neg->xport_specific.spi.sync_offset = sxfer & 0x1f;
neg->xport_specific.spi.valid = CTS_SPI_VALID_SYNC_RATE
| CTS_SPI_VALID_SYNC_OFFSET;
xpt_setup_ccb(&neg->ccb_h, ccb->ccb_h.path, 1);
xpt_async(AC_TRANSFER_NEG, ccb->ccb_h.path, neg);
OUTB (nc_sxfer, sxfer);
np->sync_st = sxfer;
OUTB (nc_scntl3, scntl3);
np->wide_st = scntl3;
for (cp = np->link_nccb; cp; cp = cp->link_nccb) {
if (!cp->ccb) continue;
if (cp->ccb->ccb_h.target_id != target) continue;
cp->sync_status = sxfer;
cp->wide_status = scntl3;
}
xpt_free_ccb(&neg->ccb_h);
}
static void ncr_setwide (ncb_p np, nccb_p cp, u_char wide, u_char ack)
{
union ccb *ccb;
struct ccb_trans_settings *neg;
u_int target = INB (nc_sdid) & 0x0f;
tcb_p tp;
u_char scntl3;
u_char sxfer;
assert (cp);
if (!cp) return;
ccb = cp->ccb;
assert (ccb);
if (!ccb) return;
assert (target == ccb->ccb_h.target_id);
tp = &np->target[target];
tp->tinfo.current.width = wide;
tp->tinfo.goal.width = wide;
tp->tinfo.current.period = 0;
tp->tinfo.current.offset = 0;
scntl3 = (tp->tinfo.wval & (~EWS)) | (wide ? EWS : 0);
sxfer = ack ? 0 : tp->tinfo.sval;
if (tp->tinfo.sval == sxfer && tp->tinfo.wval == scntl3) return;
tp->tinfo.sval = sxfer;
tp->tinfo.wval = scntl3;
neg = &xpt_alloc_ccb()->cts;
neg->protocol = PROTO_SCSI;
neg->protocol_version = SCSI_REV_2;
neg->transport = XPORT_SPI;
neg->transport_version = 2;
neg->xport_specific.spi.bus_width = (scntl3 & EWS) ?
MSG_EXT_WDTR_BUS_16_BIT : MSG_EXT_WDTR_BUS_8_BIT;
neg->xport_specific.spi.sync_period = 0;
neg->xport_specific.spi.sync_offset = 0;
neg->xport_specific.spi.valid = CTS_SPI_VALID_SYNC_RATE
| CTS_SPI_VALID_SYNC_OFFSET
| CTS_SPI_VALID_BUS_WIDTH;
xpt_setup_ccb(&neg->ccb_h, ccb->ccb_h.path, 1);
xpt_async(AC_TRANSFER_NEG, ccb->ccb_h.path, neg);
OUTB (nc_sxfer, sxfer);
np->sync_st = sxfer;
OUTB (nc_scntl3, scntl3);
np->wide_st = scntl3;
for (cp = np->link_nccb; cp; cp = cp->link_nccb) {
if (!cp->ccb) continue;
if (cp->ccb->ccb_h.target_id != target) continue;
cp->sync_status = sxfer;
cp->wide_status = scntl3;
}
xpt_free_ccb(&neg->ccb_h);
}
static void
ncr_timeout (void *arg)
{
ncb_p np = arg;
time_t thistime = time_uptime;
ticks_t step = np->ticks;
long t;
nccb_p cp;
if (np->lasttime != thistime) {
crit_enter();
np->lasttime = thistime;
t = thistime - np->heartbeat;
if (t<2) np->latetime=0; else np->latetime++;
if (np->latetime>2) {
OUTB (nc_istat, SIGP);
}
for (cp=np->link_nccb; cp; cp=cp->link_nccb) {
if (!cp->host_status) continue;
if (cp->tlimit > thistime) continue;
cp->jump_nccb.l_cmd = (SCR_JUMP);
if (cp->phys.header.launch.l_paddr ==
NCB_SCRIPT_PHYS (np, select)) {
kprintf ("%s: timeout nccb=%p (skip)\n",
ncr_name (np), cp);
cp->phys.header.launch.l_paddr
= NCB_SCRIPT_PHYS (np, skip);
}
switch (cp->host_status) {
case HS_BUSY:
case HS_NEGOTIATE:
case HS_DISCONNECT:
cp->host_status=HS_TIMEOUT;
}
cp->tag = 0;
ncr_complete (np, cp);
}
crit_exit();
}
callout_reset(&np->timeout_ch, step ? step : 1, ncr_timeout, np);
if (INB(nc_istat) & (INTF|SIP|DIP)) {
crit_enter();
if (DEBUG_FLAGS & DEBUG_TINY) kprintf ("{");
ncr_exception (np);
if (DEBUG_FLAGS & DEBUG_TINY) kprintf ("}");
crit_exit();
}
}
static void ncr_log_hard_error(ncb_p np, u_short sist, u_char dstat)
{
u_int32_t dsp;
int script_ofs;
int script_size;
char *script_name;
u_char *script_base;
int i;
dsp = INL (nc_dsp);
if (np->p_script < dsp &&
dsp <= np->p_script + sizeof(struct script)) {
script_ofs = dsp - np->p_script;
script_size = sizeof(struct script);
script_base = (u_char *) np->script;
script_name = "script";
}
else if (np->p_scripth < dsp &&
dsp <= np->p_scripth + sizeof(struct scripth)) {
script_ofs = dsp - np->p_scripth;
script_size = sizeof(struct scripth);
script_base = (u_char *) np->scripth;
script_name = "scripth";
} else {
script_ofs = dsp;
script_size = 0;
script_base = NULL;
script_name = "mem";
}
kprintf ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x) @ (%s %x:%08x).\n",
ncr_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) {
kprintf ("%s: script cmd = %08x\n", ncr_name(np),
(int)READSCRIPT_OFF(script_base, script_ofs));
}
kprintf ("%s: regdump:", ncr_name(np));
for (i=0; i<16;i++)
kprintf (" %02x", (unsigned)INB_OFF(i));
kprintf (".\n");
}
static void ncr_exception (ncb_p np)
{
u_char istat, dstat;
u_short sist;
while ((istat = INB (nc_istat)) & INTF) {
if (DEBUG_FLAGS & DEBUG_TINY) kprintf ("F ");
OUTB (nc_istat, INTF);
np->profile.num_fly++;
ncr_wakeup (np, 0);
}
if (!(istat & (SIP|DIP))) {
return;
}
sist = (istat & SIP) ? INW (nc_sist) : 0;
dstat = (istat & DIP) ? INB (nc_dstat) : 0;
np->profile.num_int++;
if (DEBUG_FLAGS & DEBUG_TINY)
kprintf ("<%d|%x:%x|%x:%x>",
INB(nc_scr0),
dstat,sist,
(unsigned)INL(nc_dsp),
(unsigned)INL(nc_dbc));
if ((dstat==DFE) && (sist==PAR)) return;
if (sist & RST) {
ncr_init (np, bootverbose ? "scsi reset" : NULL, HS_RESET);
return;
}
if ((sist & STO) &&
!(sist & (GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
!(dstat & (MDPE|BF|ABRT|SIR))) {
ncr_int_sto (np);
return;
}
if ((sist & MA) &&
!(sist & (STO|GEN|HTH|SGE|UDC|RST|PAR)) &&
!(dstat & (MDPE|BF|ABRT|SIR|IID))) {
ncr_int_ma (np, dstat);
return;
}
if ((dstat & IID) &&
!(sist & (STO|GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
!(dstat & (MDPE|BF|ABRT|SIR)) &&
((INL(nc_dbc) & 0xf8000000) == SCR_MOVE_TBL)) {
OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, no_data));
return;
}
if ((dstat & SIR) &&
!(sist & (STO|GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
!(dstat & (MDPE|BF|ABRT|IID)) &&
(INB(nc_dsps) <= SIR_MAX)) {
ncr_int_sir (np);
return;
}
ncr_log_hard_error(np, sist, dstat);
if (time_uptime - np->regtime > 10) {
int i;
np->regtime = time_uptime;
for (i=0; i<sizeof(np->regdump); i++)
((volatile char*)&np->regdump)[i] = INB_OFF(i);
np->regdump.nc_dstat = dstat;
np->regdump.nc_sist = sist;
}
if ( (INB(nc_sstat0) & (ILF|ORF|OLF) ) ||
(INB(nc_sstat1) & (FF3210) ) ||
(INB(nc_sstat2) & (ILF1|ORF1|OLF1)) ||
!(dstat & DFE)) {
kprintf ("%s: have to clear fifos.\n", ncr_name (np));
OUTB (nc_stest3, TE|CSF);
OUTB (nc_ctest3, np->rv_ctest3 | CLF);
}
if (sist & HTH) {
kprintf ("%s: handshake timeout\n", ncr_name(np));
OUTB (nc_scntl1, CRST);
DELAY (1000);
OUTB (nc_scntl1, 0x00);
OUTB (nc_scr0, HS_FAIL);
OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, cleanup));
return;
}
if ((sist & UDC) &&
!(sist & (STO|GEN|HTH|MA|SGE|RST|PAR)) &&
!(dstat & (MDPE|BF|ABRT|SIR|IID))) {
OUTB (nc_scr0, HS_UNEXPECTED);
OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, cleanup));
return;
}
if ((dstat & IID) &&
!(sist & (STO|GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
!(dstat & (MDPE|BF|ABRT|SIR)) &&
((INL(nc_dbc) & 0xf8000000) == SCR_WAIT_DISC)) {
if (INB(nc_sstat2) & LDSC) {
OUTB (nc_dcntl, np->rv_dcntl | STD);
kprintf ("%s: INFO: LDSC while IID.\n",
ncr_name (np));
return;
}
kprintf ("%s: target %d doesn't release the bus.\n",
ncr_name (np), INB (nc_sdid)&0x0f);
return;
}
if ((dstat & SSI) &&
!(sist & (STO|GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
!(dstat & (MDPE|BF|ABRT|SIR|IID))) {
OUTB (nc_dcntl, np->rv_dcntl | STD);
return;
}
if (sist & SGE) {
OUTB (nc_ctest3, np->rv_ctest3 | CLF);
}
if (DEBUG_FLAGS & DEBUG_FREEZE) {
int i;
unsigned char val;
for (i=0; i<0x60; i++) {
switch (i%16) {
case 0:
kprintf ("%s: reg[%d0]: ",
ncr_name(np),i/16);
break;
case 4:
case 8:
case 12:
kprintf (" ");
break;
}
val = bus_space_read_1(np->bst, np->bsh, i);
kprintf (" %x%x", val/16, val%16);
if (i%16==15) kprintf (".\n");
}
callout_stop(&np->timeout_ch);
kprintf ("%s: halted!\n", ncr_name(np));
OUTB (nc_istat, SRST);
return;
}
#ifdef NCR_FREEZE
kprintf ("ncr: fatal error: system halted - press reset to reboot ...");
crit_enter();
for (;;);
#endif
ncr_init (np, "fatal error", HS_FAIL);
}
static void ncr_int_sto (ncb_p np)
{
u_long dsa, scratcha, diff;
nccb_p cp;
if (DEBUG_FLAGS & DEBUG_TINY) kprintf ("T");
dsa = INL (nc_dsa);
cp = np->link_nccb;
while (cp && (CCB_PHYS (cp, phys) != dsa))
cp = cp->link_nccb;
if (cp) {
cp-> host_status = HS_SEL_TIMEOUT;
ncr_complete (np, cp);
}
scratcha = INL (nc_scratcha);
diff = scratcha - NCB_SCRIPTH_PHYS (np, tryloop);
if ((diff <= MAX_START * 20) && !(diff % 20)) {
WRITESCRIPT(startpos[0], scratcha);
OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, start));
return;
}
ncr_init (np, "selection timeout", HS_FAIL);
}
static void ncr_int_ma (ncb_p np, u_char dstat)
{
u_int32_t dbc;
u_int32_t rest;
u_int32_t dsa;
u_int32_t dsp;
u_int32_t nxtdsp;
volatile void *vdsp_base;
size_t vdsp_off;
u_int32_t oadr, olen;
u_int32_t *tblp, *newcmd;
u_char cmd, sbcl, ss0, ss2, ctest5;
u_short delta;
nccb_p cp;
dsp = INL (nc_dsp);
dsa = INL (nc_dsa);
dbc = INL (nc_dbc);
ss0 = INB (nc_sstat0);
ss2 = INB (nc_sstat2);
sbcl= INB (nc_sbcl);
cmd = dbc >> 24;
rest= dbc & 0xffffff;
ctest5 = (np->rv_ctest5 & DFS) ? INB (nc_ctest5) : 0;
if (ctest5 & DFS)
delta=(((ctest5<<8) | (INB (nc_dfifo) & 0xff)) - rest) & 0x3ff;
else
delta=(INB (nc_dfifo) - rest) & 0x7f;
if (!(dstat & DFE)) rest += delta;
if (ss0 & OLF) rest++;
if (ss0 & ORF) rest++;
if (INB(nc_scntl3) & EWS) {
if (ss2 & OLF1) rest++;
if (ss2 & ORF1) rest++;
}
OUTB (nc_ctest3, np->rv_ctest3 | CLF);
OUTB (nc_stest3, TE|CSF);
cp = np->link_nccb;
while (cp && (CCB_PHYS (cp, phys) != dsa))
cp = cp->link_nccb;
if (!cp) {
kprintf ("%s: SCSI phase error fixup: CCB already dequeued (%p)\n",
ncr_name (np), (void *) np->header.cp);
return;
}
if (cp != np->header.cp) {
kprintf ("%s: SCSI phase error fixup: CCB address mismatch "
"(%p != %p) np->nccb = %p\n",
ncr_name (np), (void *)cp, (void *)np->header.cp,
(void *)np->link_nccb);
}
if (dsp == vtophys (&cp->patch[2])) {
vdsp_base = cp;
vdsp_off = offsetof(struct nccb, patch[0]);
nxtdsp = READSCRIPT_OFF(vdsp_base, vdsp_off + 3*4);
} else if (dsp == vtophys (&cp->patch[6])) {
vdsp_base = cp;
vdsp_off = offsetof(struct nccb, patch[4]);
nxtdsp = READSCRIPT_OFF(vdsp_base, vdsp_off + 3*4);
} else if (dsp > np->p_script &&
dsp <= np->p_script + sizeof(struct script)) {
vdsp_base = np->script;
vdsp_off = dsp - np->p_script - 8;
nxtdsp = dsp;
} else {
vdsp_base = np->scripth;
vdsp_off = dsp - np->p_scripth - 8;
nxtdsp = dsp;
}
if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE)) {
kprintf ("P%x%x ",cmd&7, sbcl&7);
kprintf ("RL=%d D=%d SS0=%x ",
(unsigned) rest, (unsigned) delta, ss0);
}
if (DEBUG_FLAGS & DEBUG_PHASE) {
kprintf ("\nCP=%p CP2=%p DSP=%x NXT=%x VDSP=%p CMD=%x ",
cp, np->header.cp,
dsp,
nxtdsp, (volatile char*)vdsp_base+vdsp_off, cmd);
}
oadr = READSCRIPT_OFF(vdsp_base, vdsp_off + 1*4);
if (cmd & 0x10) {
tblp = (u_int32_t *) ((char*) &cp->phys + oadr);
olen = tblp[0];
oadr = tblp[1];
} else {
tblp = NULL;
olen = READSCRIPT_OFF(vdsp_base, vdsp_off) & 0xffffff;
}
if (DEBUG_FLAGS & DEBUG_PHASE) {
kprintf ("OCMD=%x\nTBLP=%p OLEN=%lx OADR=%lx\n",
(unsigned) (READSCRIPT_OFF(vdsp_base, vdsp_off) >> 24),
(void *) tblp,
(u_long) olen,
(u_long) oadr);
}
if (cmd != (READSCRIPT_OFF(vdsp_base, vdsp_off) >> 24)) {
PRINT_ADDR(cp->ccb);
kprintf ("internal error: cmd=%02x != %02x=(vdsp[0] >> 24)\n",
(unsigned)cmd,
(unsigned)READSCRIPT_OFF(vdsp_base, vdsp_off) >> 24);
return;
}
if (cmd & 0x06) {
PRINT_ADDR(cp->ccb);
kprintf ("phase change %x-%x %d@%08x resid=%d.\n",
cmd&7, sbcl&7, (unsigned)olen,
(unsigned)oadr, (unsigned)rest);
OUTB (nc_dcntl, np->rv_dcntl | STD);
return;
}
newcmd = cp->patch;
if (cp->phys.header.savep == vtophys (newcmd)) newcmd+=4;
newcmd[0] = ((cmd & 0x0f) << 24) | rest;
newcmd[1] = oadr + olen - rest;
newcmd[2] = SCR_JUMP;
newcmd[3] = nxtdsp;
if (DEBUG_FLAGS & DEBUG_PHASE) {
PRINT_ADDR(cp->ccb);
kprintf ("newcmd[%d] %x %x %x %x.\n",
(int)(newcmd - cp->patch),
(unsigned)newcmd[0],
(unsigned)newcmd[1],
(unsigned)newcmd[2],
(unsigned)newcmd[3]);
}
np->profile.num_break++;
OUTL (nc_temp, vtophys (newcmd));
if ((cmd & 7) == 0)
OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, dispatch));
else
OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, checkatn));
}
static int ncr_show_msg (u_char * msg)
{
u_char i;
kprintf ("%x",*msg);
if (*msg==MSG_EXTENDED) {
for (i=1;i<8;i++) {
if (i-1>msg[1]) break;
kprintf ("-%x",msg[i]);
}
return (i+1);
} else if ((*msg & 0xf0) == 0x20) {
kprintf ("-%x",msg[1]);
return (2);
}
return (1);
}
static void ncr_int_sir (ncb_p np)
{
u_char scntl3;
u_char chg, ofs, per, fak, wide;
u_char num = INB (nc_dsps);
nccb_p cp=NULL;
u_long dsa;
u_int target = INB (nc_sdid) & 0x0f;
tcb_p tp = &np->target[target];
int i;
if (DEBUG_FLAGS & DEBUG_TINY) kprintf ("I#%d", num);
switch (num) {
case SIR_SENSE_RESTART:
case SIR_STALL_RESTART:
break;
default:
dsa = INL (nc_dsa);
cp = np->link_nccb;
while (cp && (CCB_PHYS (cp, phys) != dsa))
cp = cp->link_nccb;
assert (cp);
if (!cp)
goto out;
assert (cp == np->header.cp);
if (cp != np->header.cp)
goto out;
}
switch (num) {
case SIR_SENSE_RESTART:
if (DEBUG_FLAGS & DEBUG_RESTART)
kprintf ("%s: int#%d",ncr_name (np),num);
cp = (nccb_p) 0;
for (i=0; i<MAX_TARGET; i++) {
if (DEBUG_FLAGS & DEBUG_RESTART) kprintf (" t%d", i);
tp = &np->target[i];
if (DEBUG_FLAGS & DEBUG_RESTART) kprintf ("+");
cp = tp->hold_cp;
if (!cp) continue;
if (DEBUG_FLAGS & DEBUG_RESTART) kprintf ("+");
if ((cp->host_status==HS_BUSY) &&
(cp->s_status==SCSI_STATUS_CHECK_COND))
break;
if (DEBUG_FLAGS & DEBUG_RESTART) kprintf ("- (remove)");
tp->hold_cp = cp = (nccb_p) 0;
}
if (cp) {
if (DEBUG_FLAGS & DEBUG_RESTART)
kprintf ("+ restart job ..\n");
OUTL (nc_dsa, CCB_PHYS (cp, phys));
OUTL (nc_dsp, NCB_SCRIPTH_PHYS (np, getcc));
return;
}
if (DEBUG_FLAGS & DEBUG_RESTART) kprintf (" -- remove trap\n");
WRITESCRIPT(start0[0], SCR_INT ^ IFFALSE (0));
break;
case SIR_SENSE_FAILED:
if (DEBUG_FLAGS & DEBUG_RESTART) {
PRINT_ADDR(cp->ccb);
kprintf ("in getcc reselect by t%d.\n",
INB(nc_ssid) & 0x0f);
}
cp->host_status = HS_BUSY;
cp->s_status = SCSI_STATUS_CHECK_COND;
np->target[cp->ccb->ccb_h.target_id].hold_cp = cp;
WRITESCRIPT(start0[0], SCR_INT);
break;
case SIR_NEGO_FAILED:
OUTB (HS_PRT, HS_BUSY);
case SIR_NEGO_PROTO:
if (DEBUG_FLAGS & DEBUG_NEGO) {
PRINT_ADDR(cp->ccb);
kprintf ("negotiation failed sir=%x status=%x.\n",
num, cp->nego_status);
}
switch (cp->nego_status) {
case NS_SYNC:
ncr_setsync (np, cp, 0, 0xe0, 0);
break;
case NS_WIDE:
ncr_setwide (np, cp, 0, 0);
break;
}
np->msgin [0] = MSG_NOOP;
np->msgout[0] = MSG_NOOP;
cp->nego_status = 0;
OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, dispatch));
break;
case SIR_NEGO_SYNC:
if (DEBUG_FLAGS & DEBUG_NEGO) {
PRINT_ADDR(cp->ccb);
kprintf ("sync msgin: ");
(void) ncr_show_msg (np->msgin);
kprintf (".\n");
}
chg = 0;
per = np->msgin[3];
ofs = np->msgin[4];
if (ofs==0) per=255;
if (per < np->minsync)
{chg = 1; per = np->minsync;}
if (per < tp->tinfo.user.period)
{chg = 1; per = tp->tinfo.user.period;}
if (ofs > tp->tinfo.user.offset)
{chg = 1; ofs = tp->tinfo.user.offset;}
fak = 7;
scntl3 = 0;
if (ofs != 0) {
ncr_getsync(np, per, &fak, &scntl3);
if (fak > 7) {
chg = 1;
ofs = 0;
}
}
if (ofs == 0) {
fak = 7;
per = 0;
scntl3 = 0;
}
if (DEBUG_FLAGS & DEBUG_NEGO) {
PRINT_ADDR(cp->ccb);
kprintf ("sync: per=%d scntl3=0x%x ofs=%d fak=%d chg=%d.\n",
per, scntl3, ofs, fak, chg);
}
if (INB (HS_PRT) == HS_NEGOTIATE) {
OUTB (HS_PRT, HS_BUSY);
switch (cp->nego_status) {
case NS_SYNC:
if (chg) {
ncr_setsync (np, cp, 0, 0xe0, 0);
OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
} else {
ncr_setsync (np,cp,scntl3,(fak<<5)|ofs, per);
OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, clrack));
}
return;
case NS_WIDE:
ncr_setwide (np, cp, 0, 0);
break;
}
}
ncr_setsync (np, cp, scntl3, (fak<<5)|ofs, per);
np->msgout[0] = MSG_EXTENDED;
np->msgout[1] = 3;
np->msgout[2] = MSG_EXT_SDTR;
np->msgout[3] = per;
np->msgout[4] = ofs;
cp->nego_status = NS_SYNC;
if (DEBUG_FLAGS & DEBUG_NEGO) {
PRINT_ADDR(cp->ccb);
kprintf ("sync msgout: ");
(void) ncr_show_msg (np->msgout);
kprintf (".\n");
}
if (!ofs) {
OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
return;
}
np->msgin [0] = MSG_NOOP;
break;
case SIR_NEGO_WIDE:
if (DEBUG_FLAGS & DEBUG_NEGO) {
PRINT_ADDR(cp->ccb);
kprintf ("wide msgin: ");
(void) ncr_show_msg (np->msgin);
kprintf (".\n");
}
chg = 0;
wide = np->msgin[3];
if (wide > tp->tinfo.user.width)
{chg = 1; wide = tp->tinfo.user.width;}
if (DEBUG_FLAGS & DEBUG_NEGO) {
PRINT_ADDR(cp->ccb);
kprintf ("wide: wide=%d chg=%d.\n", wide, chg);
}
if (INB (HS_PRT) == HS_NEGOTIATE) {
OUTB (HS_PRT, HS_BUSY);
switch (cp->nego_status) {
case NS_WIDE:
if (chg) {
ncr_setwide (np, cp, 0, 1);
OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
} else {
ncr_setwide (np, cp, wide, 1);
OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, clrack));
}
return;
case NS_SYNC:
ncr_setsync (np, cp, 0, 0xe0, 0);
break;
}
}
ncr_setwide (np, cp, wide, 1);
np->msgout[0] = MSG_EXTENDED;
np->msgout[1] = 2;
np->msgout[2] = MSG_EXT_WDTR;
np->msgout[3] = wide;
np->msgin [0] = MSG_NOOP;
cp->nego_status = NS_WIDE;
if (DEBUG_FLAGS & DEBUG_NEGO) {
PRINT_ADDR(cp->ccb);
kprintf ("wide msgout: ");
(void) ncr_show_msg (np->msgout);
kprintf (".\n");
}
break;
case SIR_REJECT_RECEIVED:
PRINT_ADDR(cp->ccb);
kprintf ("MSG_MESSAGE_REJECT received (%x:%x).\n",
(unsigned)np->lastmsg, np->msgout[0]);
break;
case SIR_REJECT_SENT:
PRINT_ADDR(cp->ccb);
kprintf ("MSG_MESSAGE_REJECT sent for ");
(void) ncr_show_msg (np->msgin);
kprintf (".\n");
break;
case SIR_IGN_RESIDUE:
PRINT_ADDR(cp->ccb);
kprintf ("MSG_IGN_WIDE_RESIDUE received, but not yet implemented.\n");
break;
case SIR_MISSING_SAVE:
PRINT_ADDR(cp->ccb);
kprintf ("MSG_DISCONNECT received, but datapointer not saved:\n"
"\tdata=%x save=%x goal=%x.\n",
(unsigned) INL (nc_temp),
(unsigned) np->header.savep,
(unsigned) np->header.goalp);
break;
case SIR_STALL_QUEUE:
cp->xerr_status = XE_OK;
cp->host_status = HS_COMPLETE;
cp->s_status = SCSI_STATUS_QUEUE_FULL;
ncr_freeze_devq(np, cp->ccb->ccb_h.path);
ncr_complete(np, cp);
case SIR_STALL_RESTART:
cp = np->link_nccb;
while (cp && cp->host_status != HS_DISCONNECT)
cp = cp->link_nccb;
if (cp) {
OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, reselect));
return;
}
kprintf ("%s: queue empty.\n", ncr_name (np));
WRITESCRIPT(start1[0], SCR_INT ^ IFFALSE (0));
break;
}
out:
OUTB (nc_dcntl, np->rv_dcntl | STD);
}
static nccb_p ncr_get_nccb
(ncb_p np, u_long target, u_long lun)
{
lcb_p lp;
nccb_p cp = NULL;
crit_enter();
lp = np->target[target].lp[lun];
if (lp) {
cp = lp->next_nccb;
while (cp && cp->magic) {
cp = cp->next_nccb;
}
}
if (cp == NULL)
cp = ncr_alloc_nccb(np, target, lun);
if (cp != NULL) {
if (cp->magic) {
kprintf("%s: Bogus free cp found\n", ncr_name(np));
crit_exit();
return (NULL);
}
cp->magic = 1;
}
crit_exit();
return (cp);
}
static void ncr_free_nccb (ncb_p np, nccb_p cp)
{
assert (cp != NULL);
cp -> host_status = HS_IDLE;
cp -> magic = 0;
}
static nccb_p
ncr_alloc_nccb (ncb_p np, u_long target, u_long lun)
{
tcb_p tp;
lcb_p lp;
nccb_p cp;
assert (np != NULL);
if (target>=MAX_TARGET) return(NULL);
if (lun >=MAX_LUN ) return(NULL);
tp=&np->target[target];
if (!tp->jump_tcb.l_cmd) {
tp->jump_tcb.l_cmd = (SCR_JUMP^IFFALSE (DATA (0x80 + target)));
tp->jump_tcb.l_paddr = np->jump_tcb.l_paddr;
tp->getscr[0] =
(np->features & FE_PFEN)? SCR_COPY(1) : SCR_COPY_F(1);
tp->getscr[1] = vtophys (&tp->tinfo.sval);
tp->getscr[2] = rman_get_start(np->reg_res) + offsetof (struct ncr_reg, nc_sxfer);
tp->getscr[3] =
(np->features & FE_PFEN)? SCR_COPY(1) : SCR_COPY_F(1);
tp->getscr[4] = vtophys (&tp->tinfo.wval);
tp->getscr[5] = rman_get_start(np->reg_res) + offsetof (struct ncr_reg, nc_scntl3);
assert (((offsetof(struct ncr_reg, nc_sxfer) ^
(offsetof(struct tcb ,tinfo)
+ offsetof(struct ncr_target_tinfo, sval))) & 3) == 0);
assert (((offsetof(struct ncr_reg, nc_scntl3) ^
(offsetof(struct tcb, tinfo)
+ offsetof(struct ncr_target_tinfo, wval))) &3) == 0);
tp->call_lun.l_cmd = (SCR_CALL);
tp->call_lun.l_paddr = NCB_SCRIPT_PHYS (np, resel_lun);
tp->jump_lcb.l_cmd = (SCR_JUMP);
tp->jump_lcb.l_paddr = NCB_SCRIPTH_PHYS (np, abort);
np->jump_tcb.l_paddr = vtophys (&tp->jump_tcb);
}
lp = tp->lp[lun];
if (!lp) {
lp = kmalloc (sizeof (struct lcb), M_DEVBUF, M_WAITOK | M_ZERO);
lp->jump_lcb.l_cmd = (SCR_JUMP ^ IFFALSE (DATA (lun)));
lp->jump_lcb.l_paddr = tp->jump_lcb.l_paddr;
lp->call_tag.l_cmd = (SCR_CALL);
lp->call_tag.l_paddr = NCB_SCRIPT_PHYS (np, resel_tag);
lp->jump_nccb.l_cmd = (SCR_JUMP);
lp->jump_nccb.l_paddr = NCB_SCRIPTH_PHYS (np, aborttag);
lp->actlink = 1;
tp->jump_lcb.l_paddr = vtophys (&lp->jump_lcb);
tp->lp[lun] = lp;
}
cp = kmalloc (sizeof (struct nccb), M_DEVBUF, M_WAITOK | M_ZERO);
if (DEBUG_FLAGS & DEBUG_ALLOC) {
kprintf ("new nccb @%p.\n", cp);
}
cp->p_nccb = vtophys (cp);
cp->jump_nccb.l_cmd = SCR_JUMP;
cp->jump_nccb.l_paddr = lp->jump_nccb.l_paddr;
lp->jump_nccb.l_paddr = CCB_PHYS (cp, jump_nccb);
cp->call_tmp.l_cmd = SCR_CALL;
cp->call_tmp.l_paddr = NCB_SCRIPT_PHYS (np, resel_tmp);
cp->link_nccb = np->link_nccb;
np->link_nccb = cp;
cp->next_nccb = lp->next_nccb;
lp->next_nccb = cp;
return (cp);
}
static int ncr_scatter
(struct dsb* phys, vm_offset_t vaddr, vm_size_t datalen)
{
u_long paddr, pnext;
u_short segment = 0;
u_long segsize, segaddr;
u_long size, csize = 0;
u_long chunk = MAX_SIZE;
int free;
bzero (&phys->data, sizeof (phys->data));
if (!datalen) return (0);
paddr = vtophys (vaddr);
free = MAX_SCATTER - 1;
if (vaddr & PAGE_MASK) free -= datalen / PAGE_SIZE;
if (free>1)
while ((chunk * free >= 2 * datalen) && (chunk>=1024))
chunk /= 2;
if(DEBUG_FLAGS & DEBUG_SCATTER)
kprintf("ncr?:\tscattering virtual=%p size=%d chunk=%d.\n",
(void *) vaddr, (unsigned) datalen, (unsigned) chunk);
while (datalen && (segment < MAX_SCATTER)) {
segsize = 0;
segaddr = paddr;
pnext = paddr;
if (!csize) csize = chunk;
while ((datalen) && (paddr == pnext) && (csize)) {
pnext = (paddr & (~PAGE_MASK)) + PAGE_SIZE;
size = pnext - paddr;
if (size > datalen) size = datalen;
if (size > csize ) size = csize ;
segsize += size;
vaddr += size;
csize -= size;
datalen -= size;
paddr = vtophys (vaddr);
}
if(DEBUG_FLAGS & DEBUG_SCATTER)
kprintf ("\tseg #%d addr=%x size=%d (rest=%d).\n",
segment,
(unsigned) segaddr,
(unsigned) segsize,
(unsigned) datalen);
phys->data[segment].addr = segaddr;
phys->data[segment].size = segsize;
segment++;
}
if (datalen) {
kprintf("ncr?: scatter/gather failed (residue=%d).\n",
(unsigned) datalen);
return (-1);
}
return (segment);
}
#ifndef NCR_IOMAPPED
static int ncr_regtest (struct ncb* np)
{
volatile u_int32_t data;
data = 0xffffffff;
OUTL_OFF(offsetof(struct ncr_reg, nc_dstat), data);
data = INL_OFF(offsetof(struct ncr_reg, nc_dstat));
#if 1
if (data == 0xffffffff) {
#else
if ((data & 0xe2f0fffd) != 0x02000080) {
#endif
kprintf ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
(unsigned) data);
return (0x10);
}
return (0);
}
#endif
static int ncr_snooptest (struct ncb* np)
{
u_int32_t ncr_rd, ncr_wr, ncr_bk, host_rd, host_wr, pc;
int i, err=0;
#ifndef NCR_IOMAPPED
err |= ncr_regtest (np);
if (err) return (err);
#endif
pc = NCB_SCRIPTH_PHYS (np, snooptest);
host_wr = 1;
ncr_wr = 2;
ncr_cache = host_wr;
OUTL (nc_temp, ncr_wr);
OUTL (nc_dsp, pc);
for (i=0; i<NCR_SNOOP_TIMEOUT; i++)
if (INB(nc_istat) & (INTF|SIP|DIP))
break;
pc = INL (nc_dsp);
host_rd = ncr_cache;
ncr_rd = INL (nc_scratcha);
ncr_bk = INL (nc_temp);
OUTB (nc_istat, SRST);
DELAY (1000);
OUTB (nc_istat, 0 );
if (i>=NCR_SNOOP_TIMEOUT) {
kprintf ("CACHE TEST FAILED: timeout.\n");
return (0x20);
}
if (pc != NCB_SCRIPTH_PHYS (np, snoopend)+8) {
kprintf ("CACHE TEST FAILED: script execution failed.\n");
kprintf ("start=%08lx, pc=%08lx, end=%08lx\n",
(u_long) NCB_SCRIPTH_PHYS (np, snooptest), (u_long) pc,
(u_long) NCB_SCRIPTH_PHYS (np, snoopend) +8);
return (0x40);
}
if (host_wr != ncr_rd) {
kprintf ("CACHE TEST FAILED: host wrote %d, ncr read %d.\n",
(int) host_wr, (int) ncr_rd);
err |= 1;
}
if (host_rd != ncr_wr) {
kprintf ("CACHE TEST FAILED: ncr wrote %d, host read %d.\n",
(int) ncr_wr, (int) host_rd);
err |= 2;
}
if (ncr_bk != ncr_wr) {
kprintf ("CACHE TEST FAILED: ncr wrote %d, read back %d.\n",
(int) ncr_wr, (int) ncr_bk);
err |= 4;
}
return (err);
}
static int ncr_delta (int *from, int *to)
{
if (!from) return (-1);
if (!to) return (-2);
return ((to - from) * 1000 / hz);
}
#define PROFILE cp->phys.header.stamp
static void ncb_profile (ncb_p np, nccb_p cp)
{
int co, da, st, en, di, se, post,work,disc;
u_long diff;
PROFILE.end = ticks;
st = ncr_delta (&PROFILE.start,&PROFILE.status);
if (st<0) return;
da = ncr_delta (&PROFILE.start,&PROFILE.data);
if (da<0) return;
co = ncr_delta (&PROFILE.start,&PROFILE.command);
if (co<0) return;
en = ncr_delta (&PROFILE.start,&PROFILE.end),
di = ncr_delta (&PROFILE.start,&PROFILE.disconnect),
se = ncr_delta (&PROFILE.start,&PROFILE.select);
post = en - st;
if (di>=0) disc = se-di; else disc = 0;
work = (st - co) - disc;
diff = (np->disc_phys - np->disc_ref) & 0xff;
np->disc_ref += diff;
np->profile.num_trans += 1;
if (cp->ccb)
np->profile.num_bytes += cp->ccb->csio.dxfer_len;
np->profile.num_disc += diff;
np->profile.ms_setup += co;
np->profile.ms_data += work;
np->profile.ms_disc += disc;
np->profile.ms_post += post;
}
#undef PROFILE
static void ncr_selectclock(ncb_p np, u_char scntl3)
{
if (np->multiplier < 2) {
OUTB(nc_scntl3, scntl3);
return;
}
if (bootverbose >= 2)
kprintf ("%s: enabling clock multiplier\n", ncr_name(np));
OUTB(nc_stest1, DBLEN);
if (np->multiplier > 2) {
int i = 20;
while (!(INB(nc_stest4) & LCKFRQ) && --i > 0)
DELAY(20);
if (!i)
kprintf("%s: the chip cannot lock the frequency\n", ncr_name(np));
} else
DELAY(20);
OUTB(nc_stest3, HSC);
OUTB(nc_scntl3, scntl3);
OUTB(nc_stest1, (DBLEN|DBLSEL));
OUTB(nc_stest3, 0x00);
}
static unsigned
ncrgetfreq (ncb_p np, int gen)
{
int ms = 0;
OUTB (nc_stest1, 0);
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++ < 1000)
DELAY(1000);
OUTB (nc_stime1, 0);
OUTB (nc_scntl3, 0);
OUTB (nc_scntl3, 0);
if (bootverbose >= 2)
kprintf ("\tDelay (GEN=%d): %u msec\n", gen, ms);
return ms ? ((1 << gen) * 4440) / ms : 0;
}
static void ncr_getclock (ncb_p np, u_char multiplier)
{
unsigned char scntl3;
unsigned char stest1;
scntl3 = INB(nc_scntl3);
stest1 = INB(nc_stest1);
np->multiplier = 1;
if (multiplier > 1) {
np->multiplier = multiplier;
np->clock_khz = 40000 * multiplier;
} else {
if ((scntl3 & 7) == 0) {
unsigned f1, f2;
(void) ncrgetfreq (np, 11);
f1 = ncrgetfreq (np, 11);
f2 = ncrgetfreq (np, 11);
if (bootverbose >= 2)
kprintf ("\tNCR clock is %uKHz, %uKHz\n", f1, f2);
if (f1 > f2) f1 = f2;
if (f1 > 45000) {
scntl3 = 5;
} else {
scntl3 = 3;
}
}
else if ((scntl3 & 7) == 5)
np->clock_khz = 80000;
}
}
#ifdef NCR_TEKRAM_EEPROM
struct tekram_eeprom_dev {
u_char devmode;
#define TKR_PARCHK 0x01
#define TKR_TRYSYNC 0x02
#define TKR_ENDISC 0x04
#define TKR_STARTUNIT 0x08
#define TKR_USETAGS 0x10
#define TKR_TRYWIDE 0x20
u_char syncparam;
u_char filler1;
u_char filler2;
};
struct tekram_eeprom {
struct tekram_eeprom_dev
dev[16];
u_char adaptid;
u_char adaptmode;
#define TKR_ADPT_GT2DRV 0x01
#define TKR_ADPT_GT1GB 0x02
#define TKR_ADPT_RSTBUS 0x04
#define TKR_ADPT_ACTNEG 0x08
#define TKR_ADPT_NOSEEK 0x10
#define TKR_ADPT_MORLUN 0x20
u_char delay;
u_char tags;
u_char filler[60];
};
static void
tekram_write_bit (ncb_p np, int bit)
{
u_char val = 0x10 + ((bit & 1) << 1);
DELAY(10);
OUTB (nc_gpreg, val);
DELAY(10);
OUTB (nc_gpreg, val | 0x04);
DELAY(10);
OUTB (nc_gpreg, val);
DELAY(10);
}
static int
tekram_read_bit (ncb_p np)
{
OUTB (nc_gpreg, 0x10);
DELAY(10);
OUTB (nc_gpreg, 0x14);
DELAY(10);
return INB (nc_gpreg) & 1;
}
static u_short
read_tekram_eeprom_reg (ncb_p np, int reg)
{
int bit;
u_short result = 0;
int cmd = 0x80 | reg;
OUTB (nc_gpreg, 0x10);
tekram_write_bit (np, 1);
for (bit = 7; bit >= 0; bit--)
{
tekram_write_bit (np, cmd >> bit);
}
for (bit = 0; bit < 16; bit++)
{
result <<= 1;
result |= tekram_read_bit (np);
}
OUTB (nc_gpreg, 0x00);
return result;
}
static int
read_tekram_eeprom(ncb_p np, struct tekram_eeprom *buffer)
{
u_short *p = (u_short *) buffer;
u_short sum = 0;
int i;
if (INB (nc_gpcntl) != 0x09)
{
return 0;
}
for (i = 0; i < 64; i++)
{
u_short val;
if((i&0x0f) == 0) kprintf ("%02x:", i*2);
val = read_tekram_eeprom_reg (np, i);
if (p)
*p++ = val;
sum += val;
if((i&0x01) == 0x00) kprintf (" ");
kprintf ("%02x%02x", val & 0xff, (val >> 8) & 0xff);
if((i&0x0f) == 0x0f) kprintf ("\n");
}
kprintf ("Sum = %04x\n", sum);
return sum == 0x1234;
}
#endif
static device_method_t ncr_methods[] = {
DEVMETHOD(device_probe, ncr_probe),
DEVMETHOD(device_attach, ncr_attach),
DEVMETHOD_END
};
static driver_t ncr_driver = {
"ncr",
ncr_methods,
sizeof(struct ncb),
};
static devclass_t ncr_devclass;
DRIVER_MODULE(if_ncr, pci, ncr_driver, ncr_devclass, NULL, NULL);
#endif