if_ei.c
上传用户:luoyougen
上传日期:2008-05-12
资源大小:23136k
文件大小:58k
源码类别:

VxWorks

开发平台:

C/C++

  1. /* if_ei.c - Intel 82596 Ethernet network interface driver */
  2. /* Copyright 1989-1997 Wind River Systems, Inc. */
  3. #include "copyright_wrs.h"
  4. /*
  5. modification history
  6. --------------------
  7. 03i,15jul97,spm  added ARP request to SIOCSIFADDR ioctl handler
  8. 03i,29aug97,dgp  doc: SPR 9184 - promiscuous mode not supported
  9. 03h,05feb97,dat  fixed wdog reset action to use netJobAdd(). SPR 7771
  10. 03j,07apr97,spm  code cleanup, corrected statistics, and upgraded to BSD 4.4
  11. 03i,12nov94,tmk  removed some IMPORTed function decs and included unixLib.h
  12. 03h,10oct94,caf  suppress buffer loaning for MIPS targets due to
  13.  problem with misaligned accesses.
  14. 03g,14oct93,wmd  Added initialization of pointer pDrvCtrl in eiWatchDog(),
  15.  SPR #2567.
  16. 03f,30sep93,wmd  Added changes to include timeout check of the receiver 
  17.  as done in the 5.0.6 release. Recreated eiInit() to allow
  18.  resetting the 596 if it locks up.
  19. 03e,11aug93,jmm  Changed ioctl.h and socket.h to sys/ioctl.h and sys/socket.h
  20. 03d,19feb93,jdi  documentation cleanup.
  21. 03c,15jan93,wmd  added setting of errno to S_ioslib_INVALID_ETHERNET_ADDRESS.
  22. 03b,11dec92,rfs  Removed dummy eiChipReset(), no longer needed for TP41.
  23. 03a,05oct92,rfs  Added documentation.
  24. 02z,02oct92,rfs  Made multiple attach calls per unit return OK, not ERROR.
  25. 02y,01oct92,rfs  Added 16 byte page alignment for SCP.
  26. 02x,28sep92,rfs  Modified the eiAction() routine to clear all events.  This
  27.                  makes the diagnostics pass, for some undocumented reason.
  28. 02w,09sep92,gae  documentation tweaks.
  29. 02v,11aug92,rfs  Slight change to memory carving.  Also fixed receive bug
  30.                  that was due to device manual incorrectly describing bit 7
  31.                  of config byte 10 and added error checks to RFDs.  Changed
  32.                  the device startup routine.
  33. 02u,26aug92,kdl  moved internal notes to file header (were just #ifdef'd out).
  34. 02t,19aug92,smb  changed systime.h to sys/times.h
  35. 02s,31jul92,dave Changed to new cacheLib.
  36. 02r,28jul92,rfs  Adapted to latest cache coherency model.
  37.                  Renamed driver control structure and moved it to bss.
  38.                  Combined the init() routine with the attach() routine and
  39.                  made attach() reject multiple invocations.  Rewrote the
  40.                  ioctl() routine to do the right stuff.  Reworked the action
  41.                  command routine and the primary command routine.  Other misc.
  42. 02q,16jul92,rfs  Moved driver specific items from header file to here,
  43.                  where they belong.
  44. 02p,16jul92,rfs  Moved internal comments to end of file, where they belong.
  45. 02o,03jul92,jwt  converted cacheClearEntry() calls to cacheClear() for 5.1.
  46. 02n,26jun92,rrr  make eiBusSnoop global again (needed by bsps)
  47. 02m,25jun92,rfs  backed out last mod; the manual is incorrect about the
  48.                  state of the bit that configures CRC inclusion
  49. 02l,25jun92,rfs  bug fix, changed a receive config parm that selects whether
  50.                  the CRC is included in the buffer
  51. 02k,23jun92,ajm  made eiChipReset global for tp41 BSP
  52. 02j,08jun92,ajm  modified so eiattach is only external function
  53.                  got rid of MEM_ROUND_UP macro now pulled in with vxWorks.h
  54.                  got rid of many ansi complaints
  55. 02i,26may92,rrr  the tree shuffle
  56.                  -changed includes to have absolute path from h/
  57. 02h,22feb92,wmd  added conditional compiles for 040, ansi fixes.
  58. 02g,07jan92,shl  made driver cache safe. added eiBusSnoop(). eliminated some
  59.                  ANSI warnings.
  60. 02f,12nov91,wmd  Added fix, courtesy of jrb-Intel, which added a watchdog to
  61.                  timeout any stalls due to hardware lockup and resets the
  62.                  82596.
  63. 02e,25oct91,rfs  Changed eiChipReset() to not depend on valid *es, and
  64.                  made it global for TP41 usage. Also eliminated ANSI
  65.                  warnings.
  66. 02d,04oct91,rrr  passed through the ansification filter
  67.                  -changed functions to ansi style
  68.                  -changed includes to have absolute path from h/
  69.                  -changed VOID to void
  70.                  -changed copyright notice
  71. 02c,15sep91,rfs  Bug fix. Eliminated lock condition when output queue full
  72.                  and out of TFDs. Added major comment to eiTxStartup. Also,
  73.                  major changes to eiChipReset() to make more robust. All
  74.                  instances of intLock()/intUnlock() changed to
  75.                  sys596IntDisable()/sys596IntEnable respectively. Statistic
  76.                  counters now being used, which required moderate changes to
  77.                  the transmit side design. More comments added.
  78. 02b,21aug91,rfs  changed method of obtaining Ethernet address.
  79. 02a,22jul91,jcf  completely rewritten to utilize simplified mode,
  80.             rfs  added big endian support, reworked buffer loaning.
  81. 01h,01may91,elh  fixed ethernet addressing problem, upgraded to 5.0,
  82.                  added buffer loanouts, fixed ifAddrSet bug by disabling
  83.                  interrupts in eiHardReset.
  84. 01g,28apr91,del  added #define of ETHER_HEADER_SIZE as workaround for
  85.                  gnu960 pading of struct ether_header.
  86. 01f,05jan91,gae  added global eiSCPSysConfig for board specific SCP parameters.
  87.                  moved CDELAY here from if_ei.h and renamed eichkcca.
  88. 01e,03jan91,gae  more cleanup and merged with Intel's fixes.
  89.                  Ethernet address (eiEnetAddr) is set in sysHwInit.
  90.                  cleaned up error printing & no carrier message always printed.
  91.                  changed printf's to printErr's (logMsg if at int level).
  92.                  made most routines local -- not available outside this module.
  93. 01d,31dec90,gae  removed 68K stuff.
  94. 01c,14dec90,elh  more cleanup.
  95. 01b,12dec90,elh  fixed buffer problems, cleaned up, added etherhooks.
  96. 01a,06feb90,djk  written
  97. */
  98. /*
  99. This module implements the Intel 82596 Ethernet network interface driver.
  100. This driver is designed to be moderately generic, operating unmodified
  101. across the range of architectures and targets supported by VxWorks.  
  102. To achieve this, this driver must be given several
  103. target-specific parameters, and some external support routines must be
  104. provided.  These parameters, and the mechanisms used to communicate them to the
  105. driver, are detailed below.
  106. This driver can run with the device configured in either
  107. big-endian or little-endian modes.  Error recovery code has been added to
  108. deal with some of the known errata in the A0 version of the device.  This
  109. driver supports up to four individual units per CPU.
  110. BOARD LAYOUT
  111. This device is on-board.  No jumpering diagram is necessary.
  112. EXTERNAL INTERFACE
  113. This driver provides the standard external interface with the following
  114. exceptions.  All initialization is performed within the attach routine;
  115. there is no separate initialization routine.  Therefore, in the global interface
  116. structure, the function pointer to the initialization routine is NULL.
  117. The only user-callable routine is eiattach(), which publishes the `ei'
  118. interface and initializes the driver and device.
  119. TARGET-SPECIFIC PARAMETERS
  120. .iP "the <sysbus> value"
  121. This parameter is passed to the driver by eiattach().
  122. The Intel 82596 requires this parameter during initialization.  This
  123. parameter tells the device about the system bus, hence the name "sysbus."
  124. To determine the correct value for a target, refer to the document
  125. .I "Intel 32-bit Local Area Network (LAN) Component User's Manual."
  126. .iP "interrupt vector"
  127. This parameter is passed to the driver by eiattach().
  128. The Intel 82596 generates hardware interrupts for various events within
  129. the device; thus it contains an interrupt handler routine.
  130. This driver calls intConnect() to connect its interrupt handler to the 
  131. interrupt vector generated as a result of the 82596 interrupt.
  132. .iP "shared memory address"
  133. This parameter is passed to the driver by eiattach().
  134. The Intel 82596 device is a DMA type device and typically shares
  135. access to some region of memory with the CPU.  This driver is designed
  136. for systems that directly share memory between the CPU and the 82596.
  137. This parameter can be used to specify an explicit memory region for use
  138. by the 82596.  This should be done on targets that restrict the 82596
  139. to a particular memory region.  The constant NONE can be used to indicate
  140. that there are no memory limitations, in which case, the driver 
  141. attempts to allocate the shared memory from the system space.
  142. .iP "number of Receive and Transmit Frame Descriptors"
  143. These parameters are passed to the driver by eiattach().
  144. The Intel 82596 accesses frame descriptors in memory for each frame
  145. transmitted or received.  The number of frame descriptors 
  146. at run-time can be configured using these parameters.
  147. .iP "Ethernet address"
  148. This parameter is obtained by a call to an external support routine.
  149. During initialization, the driver needs to know the Ethernet address for
  150. the Intel 82596 device.  The driver calls the external support routine,
  151. sysEnetAddrGet(), to obtain the Ethernet address.  For a description of
  152. sysEnetAddrGet(), see "External Support Requirements" below.
  153. .LP
  154. EXTERNAL SUPPORT REQUIREMENTS
  155. This driver requires seven external support functions:
  156. .iP "STATUS sysEnetAddrGet (int unit, char *pCopy)" "" 9 -1
  157. This routine provides the six-byte Ethernet address
  158. used by <unit>.  It must copy the six-byte address
  159. to the space provided by <pCopy>.  This routine 
  160. returns OK, or ERROR if it fails.  The driver calls this 
  161. routine, once per unit, using eiattach().
  162. .iP "STATUS sys596Init (int unit)"
  163. This routine performs any target-specific initialization
  164. required before the 82596 is initialized.  Typically, it is empty.  
  165. This routine must return OK, or ERROR if it fails.
  166. The driver calls this routine, once per unit, using eiattach().
  167. .iP "void sys596Port (int unit, int cmd, UINT32 addr)"
  168. This routine provides access to the special port
  169. function of the 82596.  It delivers the command and
  170. address arguments to the port of the specified unit.
  171. The driver calls this routine primarily during
  172. initialization, but may also call it during error
  173. recovery procedures.
  174. .iP "void sys596ChanAtn (int unit)"
  175. This routine provides the channel attention signal to
  176. the 82596, for the specified <unit>.  The driver calls
  177. this routine frequently throughout all phases of
  178. operation.
  179. .iP "void sys596IntEnable (int unit), void sys596IntDisable (int unit)"
  180. These routines enable or disable the interrupt from
  181. the 82596 for the specified <unit>.  Typically, this
  182. involves interrupt controller hardware, either
  183. internal or external to the CPU.  Since the 82596
  184. itself has no mechanism for controlling its interrupt
  185. activity, these routines are vital to the correct
  186. operation of the driver.  The driver calls these
  187. routines throughout normal operation to protect
  188. certain critical sections of code from interrupt
  189. handler intervention.
  190. .iP "void sys596IntAck (int unit)"
  191. This routine must perform any required interrupt acknowledgment 
  192. or clearing.  Typically, this involves an operation to some interrupt
  193. control hardware.  Note that the INT signal from the 82596
  194. behaves in an "edge-triggered" mode; therefore, this routine 
  195. typically clears a latch within the control circuitry.
  196. The driver calls this routine from the interrupt handler.
  197. .LP
  198. SYSTEM RESOURCE USAGE
  199. When implemented, this driver requires the following system resources:
  200.     - one mutual exclusion semaphore
  201.     - one interrupt vector
  202.     - one watchdog timer.
  203.     - 8 bytes in the initialized data section (data)
  204.     - 912 bytes in the uninitialized data section (BSS)
  205. The above data and BSS requirements are for the MC68020 architecture 
  206. and may vary for other architectures.  Code size (text) varies greatly between
  207. architectures and is therefore not quoted here.
  208. The driver uses cacheDmaMalloc() to allocate memory to share with the 82596.  
  209. The fixed-size pieces in this area total 160 bytes.  The variable-size 
  210. pieces in this area are affected by the configuration parameters specified 
  211. in the eiattach() call.  The size of one RFD (Receive Frame
  212. Descriptor) is 1536 bytes.  The size of one TFD (Transmit Frame Descriptor) 
  213. is 1534 bytes.  For more information about RFDs and TFDs, see the 
  214. .I "Intel 82596 User's Manual."
  215. The 82596 can be operated only if this shared memory region is non-cacheable
  216. or if the hardware implements bus snooping.  The driver cannot maintain
  217. cache coherency for the device because fields within the command
  218. structures are asynchronously modified by both the driver and the device,
  219. and these fields may share the same cache line.
  220. TUNING HINTS
  221. The only adjustable parameters are the number of TFDs and RFDs that will be
  222. created at run-time.  These parameters are given to the driver when eiattach()
  223. is called.  There is one TFD and one RFD associated with each transmitted
  224. frame and each received frame respectively.  For memory-limited applications,
  225. decreasing the number of TFDs and RFDs may be desirable.  Increasing the number
  226. of TFDs will provide no performance benefit after a certain point.
  227. Increasing the number of RFDs will provide more buffering before packets are
  228. dropped.  This can be useful if there are tasks running at a higher priority
  229. than the net task.
  230. CAVEAT
  231. This driver does not support promiscuous mode.
  232. SEE ALSO: ifLib,
  233. .I "Intel 82596 User's Manual,"
  234. .I "Intel 32-bit Local Area Network (LAN) Component User's Manual"
  235. */
  236. #include "vxWorks.h"
  237. #include "wdLib.h"
  238. #include "iv.h"
  239. #include "vme.h"
  240. #include "net/mbuf.h"
  241. #include "net/unixLib.h"
  242. #include "net/protosw.h"
  243. #include "sys/socket.h"
  244. #include "sys/ioctl.h"
  245. #include "errno.h"
  246. #include "memLib.h"
  247. #include "intLib.h"
  248. #include "net/if.h"
  249. #include "net/route.h"
  250. #include "iosLib.h"
  251. #include "errnoLib.h"
  252. #include "cacheLib.h"
  253. #include "logLib.h"
  254. #include "netLib.h"
  255. #include "stdio.h"
  256. #include "stdlib.h"
  257. #include "sysLib.h"
  258. #ifdef  INET
  259. #include "netinet/in.h"
  260. #include "netinet/in_systm.h"
  261. #include "netinet/in_var.h"
  262. #include "netinet/ip.h"
  263. #include "netinet/if_ether.h"
  264. #endif  /* INET */
  265. #include "etherLib.h"
  266. #include "net/systm.h"
  267. #include "sys/times.h"
  268. #include "drv/netif/if_ei.h"
  269. #include "net/if_subr.h"
  270. /***** LOCAL DEFINITIONS *****/
  271. #undef EI_DEBUG                             /* Compiles debug output */
  272. #define MAX_UNITS       4                   /* maximum units to support */
  273. #define DEF_NUM_TFDS    32                  /* default number of TFDs */
  274. #define DEF_NUM_RFDS    32                  /* default number of RFDs */
  275. #if     (CPU_FAMILY == MIPS)
  276. #define MAX_RFDS_LOANED 0                   /* suppress buffer loaning */
  277. #else
  278. #define MAX_RFDS_LOANED 8                   /* max RFDs that can be loaned */
  279. #endif  /* (CPU_FAMILY == MIPS) */
  280. /* Typedefs for external structures that are not typedef'd in their .h files */
  281. typedef struct mbuf MBUF;
  282. typedef struct arpcom IDR;                  /* Interface Data Record wrapper */
  283. typedef struct ifnet IFNET;                 /* real Interface Data Record */
  284. typedef struct sockaddr SOCK;
  285. typedef struct ether_header ETH_HDR; 
  286. /* The definition of our linked list management structure */
  287. typedef struct ei_list                       /* EI_LIST - 82596 queue head */
  288.     {
  289.     volatile EI_NODE *  head;       /* header of list */
  290.     volatile EI_NODE *  tail;       /* tail of list */
  291.     } EI_LIST;
  292. /* The definition of the driver control structure */
  293. typedef struct drv_ctrl
  294.     {
  295.     IDR                 idr;        /* interface data record */
  296.     BOOL                attached;   /* indicates attach() called */
  297.     char                *memBase;   /* 82596 memory pool base */
  298.     SCP                 *pScp;      /* SCP ptr */
  299.     ISCP                *pIscp;     /* ISCP ptr */
  300.     SCB                 *pScb;      /* SCB ptr */
  301.     CFD                 *pCfd;      /* synchronous command frame */
  302.     RFD                 *rfdPool;   /* RFD pool */
  303.     TFD                 *tfdPool;   /* TFD pool */
  304.     RFD                 *pFreeRfd;  /* first empty RFD in rxQueue */
  305.     volatile EI_LIST    rxQueue;    /* receive queue */
  306.     volatile EI_LIST    txQueue;    /* to be sent queue */
  307.     volatile EI_LIST    tfdQueue;   /* free transmit frame descriptors */
  308.     volatile EI_LIST    cblQueue;   /* actual chip transmit queue */
  309.     volatile EI_LIST    cleanQueue; /* queue of TFDs to cleanup */
  310.     volatile BOOL       rcvHandling;/* flag, indicates netTask active */
  311.     volatile BOOL       txCleaning; /* flag, indicates netTask active */
  312.     volatile BOOL       txIdle;     /* flag, indicates idle transmitter */
  313.     int                 nTFDs;      /* how many TFDs to create */
  314.     int                 nRFDs;      /* how many RFDs to create */
  315.     int                 nLoanRfds;  /* number of loanable RFDs left */
  316.     UINT8               sysbus;     /* SCP sysbus value */
  317.     int                 ivec;       /* interrupt vector */
  318.     CACHE_FUNCS         cacheFuncs; /* cache descriptor */
  319.     WDOG_ID             wid;        /* watchdog timer for transmit */
  320.     int                 wdInterval; /* ticks between eiWatchDog runs */
  321.     long recvLocks;  /* count for receive lockup failures */
  322.     long                transLocks; /* count for transmit lockup failures */
  323.     volatile char wdTxTimeout;/* watchdog runs with transmit hung */
  324.     volatile char wdRxTimeout;/* watchdog runs with receive hung */
  325.     } DRV_CTRL;
  326. #define DRV_CTRL_SIZ  sizeof(DRV_CTRL)
  327. #define EI_RX_TIMEOUT 3         /* # watchdog runs for receive timeout */
  328. #define EI_TX_TIMEOUT 2         /* # watchdog runs for transmit timeout */
  329. /***** GLOBALS *****/
  330. /* Function declarations not in any header files */
  331. IMPORT STATUS sysEnetAddrGet (int unit, char addr[]);
  332. IMPORT STATUS sys596Init (int unit);
  333. IMPORT STATUS sys596IntAck (int unit);
  334. IMPORT STATUS sys596IntEnable (int unit);
  335. IMPORT void   sys596IntDisable (int unit);
  336. IMPORT void   sys596Port (int unit, int cmd, UINT32 addr);
  337. IMPORT void   sys596ChanAtn (int unit);
  338. /***** LOCALS *****/
  339. /* The array of driver control structs */
  340. LOCAL DRV_CTRL drvCtrl [MAX_UNITS];
  341. /* forward function declarations */
  342. static STATUS eiInit (int unit);
  343. static void eiReset (int unit);
  344. static int eiIoctl (IFNET *pIfNet, int cmd, caddr_t data);
  345. static void eiTxStartup (DRV_CTRL * pDrvCtrl);
  346. static void eiInt (DRV_CTRL *pDrvCtrl);
  347. static void eiTxCleanQ (DRV_CTRL *pDrvCtrl);
  348. static void eiHandleRecvInt (DRV_CTRL *pDrvCtrl);
  349. static STATUS eiReceive (DRV_CTRL *pDrvCtrl, RFD *pRfd);
  350. static void eiLoanFree (DRV_CTRL *pDrvCtrl, RFD *pRfd);
  351. static void eiDiag (int unit);
  352. static void eiConfig (int unit);
  353. static void eiIASetup (int unit);
  354. static void eiRxStartup (DRV_CTRL *pDrvCtrl);
  355. static void eiAction (int unit, UINT16 action);
  356. static STATUS eiCommand (DRV_CTRL *pDrvCtrl, UINT16 cmd);
  357. static void eiTxQPut (DRV_CTRL *pDrvCtrl, TFD *pTfd);
  358. static void eiTxQFlush (DRV_CTRL *pDrvCtrl);
  359. static void eiRxQPut (DRV_CTRL *pDrvCtrl, RFD *pRfd);
  360. static RFD *eiRxQGet (DRV_CTRL *pDrvCtrl);
  361. static BOOL eiRxQFull (DRV_CTRL *pDrvCtrl);
  362. static void eiQInit (EI_LIST *pHead);
  363. static EI_NODE *eiQGet (EI_LIST *pQueue);
  364. static void eiQPut (int unit, EI_LIST *pQueue, EI_NODE *pNode);
  365. static STATUS eiDeviceStart (int unit);
  366. static void eiWatchDog(int unit);
  367. /*******************************************************************************
  368. *
  369. * eiattach - publish the `ei' network interface and initialize the driver and device
  370. *
  371. * This routine publishes the `ei' interface by filling in a network interface
  372. * record and adding this record to the system list.  This routine also
  373. * initializes the driver and the device to the operational state.
  374. *
  375. * The 82596 shares a region of memory with the driver.  The caller of this
  376. * routine can specify the address of this memory region, or can specify that
  377. * the driver must obtain this memory region from the system resources.
  378. *
  379. * The <sysbus> parameter accepts values as described in the Intel manual
  380. * for the 82596.  A default number of transmit/receive frames of 32 can be
  381. * selected by passing zero in the parameters <nTfds> and <nRfds>.
  382. * In other cases, the number of frames selected should be greater than two.
  383. *
  384. * The <memBase> parameter is used to inform the driver about the shared
  385. * memory region.  If this parameter is set to the constant "NONE," then this
  386. * routine will attempt to allocate the shared memory from the system.  Any
  387. * other value for this parameter is interpreted by this routine as the address
  388. * of the shared memory region to be used.
  389. *
  390. * If the caller provides the shared memory region, then the driver assumes
  391. * that this region does not require cache coherency operations, nor does it
  392. * require conversions between virtual and physical addresses.
  393. *
  394. * If the caller indicates that this routine must allocate the shared memory
  395. * region, then this routine will use cacheDmaMalloc() to obtain
  396. * some  non-cacheable memory.  The attributes of this memory will be checked,
  397. * and if the memory is not both read and write coherent, this routine will
  398. * abort and return ERROR.
  399. *
  400. * RETURNS: OK or ERROR.
  401. *
  402. * SEE ALSO: ifLib,
  403. * .I "Intel 82596 User's Manual"
  404. */
  405. STATUS eiattach
  406.     (
  407.     int         unit,       /* unit number */
  408.     int         ivec,       /* interrupt vector number */
  409.     UINT8       sysbus,     /* sysbus field of SCP */
  410.     char *      memBase,    /* address of memory pool or NONE */
  411.     int         nTfds,      /* no. of transmit frames (0 = default) */
  412.     int         nRfds       /* no. of receive frames (0 = default) */
  413.     )
  414.     {
  415.     DRV_CTRL    *pDrvCtrl;
  416.     UINT        size;                           /* temporary size holder */
  417.     UINT        sizeScp;
  418.     UINT        sizeIscp;
  419.     UINT        sizeScb;
  420.     UINT        sizeCfd;
  421.     RFD *       pRfd;                   /* pointer to RFD's */
  422.     int         ix;
  423.     static char *errMsg1 = "neiattach: could not obtain memoryn";
  424.     static char *errMsg2 = "neiattach: shared memory not cache coherentn";
  425.     sizeScp  = MEM_ROUND_UP (sizeof (SCP));
  426.     sizeIscp = MEM_ROUND_UP (sizeof (ISCP));
  427.     sizeScb  = MEM_ROUND_UP (sizeof (SCB));
  428.     sizeCfd  = MEM_ROUND_UP (sizeof (CFD));
  429.     /* Sanity check the unit number */
  430.     if (unit < 0 || unit >= MAX_UNITS)
  431.         return (ERROR);
  432.     /* Ensure single invocation per system life */
  433.     pDrvCtrl = & drvCtrl [unit];
  434.     if (pDrvCtrl->attached)
  435.         return (OK);
  436.     /* Determine number of Tx and Rx descriptors to use */
  437.     pDrvCtrl->nTFDs = nTfds ? nTfds : DEF_NUM_TFDS;
  438.     pDrvCtrl->nRFDs = nRfds ? nRfds : DEF_NUM_RFDS;
  439.     /* calculate the total size of 82596 memory pool */
  440.     size =
  441.             16 +                                /* allow for alignment */
  442.             sizeScp +
  443.             sizeIscp +
  444.             sizeScb +
  445.             sizeCfd +                           /* synch'ed command frame */
  446.             (sizeof (RFD) * pDrvCtrl->nRFDs) +  /* pool of receive frames */
  447.             (sizeof (TFD) * pDrvCtrl->nTFDs);   /* pool of transmit frames */
  448.     /* Establish the memory area that we will share with the device.  If
  449.      * the caller has provided an area, then we assume it is non-cacheable
  450.      * and will not require the use of the special cache routines.
  451.      * If the caller did not provide an area, then we must obtain it from
  452.      * the system, using the cache savvy allocation routine.
  453.      */
  454.     switch ((int) memBase)
  455.         {
  456.         case NONE :                            /* we must obtain it */
  457.             /* this driver can't handle incoherent caches */
  458.             if (!CACHE_DMA_IS_WRITE_COHERENT () ||
  459.                 !CACHE_DMA_IS_READ_COHERENT ())
  460.                 {
  461.                 printf (errMsg2);
  462.                 goto error;
  463.                 }
  464.             pDrvCtrl->memBase = cacheDmaMalloc (size);
  465.             if (pDrvCtrl->memBase == NULL)    /* no memory available */
  466.                 {
  467.                 printf (errMsg1);
  468.                 goto error;
  469.                 }
  470.             pDrvCtrl->cacheFuncs = cacheDmaFuncs;
  471.             break;
  472.         default :                               /* the user provided an area */
  473.             pDrvCtrl->memBase = memBase;        /* use the provided address */
  474.             pDrvCtrl->cacheFuncs = cacheNullFuncs;
  475.             break;
  476.         }
  477.     /* Save other passed-in parameters */
  478.     pDrvCtrl->sysbus = sysbus;                  /* remember sysbus value */
  479.     pDrvCtrl->ivec = ivec;                      /* interrupt vector */
  480.     /* Carve up the shared memory region into the specific sections */
  481.     bzero ((char *) pDrvCtrl->memBase, size);
  482.     pDrvCtrl->pScp  = (SCP *)                /* align to first 16 byte page */
  483.                       ( ((u_long) pDrvCtrl->memBase + 0xf) & ~0xf );
  484.     pDrvCtrl->pIscp = (ISCP *)((int)pDrvCtrl->pScp   + sizeScp);
  485.     pDrvCtrl->pScb  = (SCB *) ((int)pDrvCtrl->pIscp  + sizeIscp);
  486.     pDrvCtrl->pCfd  = (CFD *) ((int)pDrvCtrl->pScb   + sizeScb);
  487.     pDrvCtrl->rfdPool   = (RFD *) ((int)pDrvCtrl->pCfd   + sizeCfd);
  488.     pDrvCtrl->tfdPool = (TFD *)
  489.             ((int)pDrvCtrl->rfdPool+ (sizeof (RFD) * pDrvCtrl->nRFDs));
  490.     for (ix = 0, pRfd = pDrvCtrl->rfdPool; ix < DEF_NUM_RFDS; ix++, pRfd++)
  491.         {
  492.         pRfd->refCnt = 0;                    /* initialize ref cnt */
  493.         }
  494.     /* Init the watchdog that will patrol for device lockups */
  495.  
  496.     pDrvCtrl->transLocks = 0;
  497.     pDrvCtrl->recvLocks = 0;
  498.     pDrvCtrl->wid = wdCreate ();                      /* create watchdog */
  499.     if (pDrvCtrl->wid == NULL)                        /* no resource */
  500.         goto error;
  501.     pDrvCtrl->wdInterval = sysClkRateGet() >> 1;
  502.     /* get our enet addr */
  503.     if (sysEnetAddrGet (unit, (char *)pDrvCtrl->idr.ac_enaddr) == ERROR)
  504. {
  505. errnoSet (S_iosLib_INVALID_ETHERNET_ADDRESS);
  506.         goto error;
  507. }
  508.     /* Publish the interface record */
  509.     ether_attach    (
  510.                     (IFNET *) & pDrvCtrl->idr,
  511.                     unit,
  512.                     "ei",
  513.                     (FUNCPTR) NULL,
  514.                     (FUNCPTR) eiIoctl,
  515.                     (FUNCPTR) ether_output, /* generic ether_output */
  516.                     (FUNCPTR) eiReset
  517.                     );
  518.     pDrvCtrl->idr.ac_if.if_start = (FUNCPTR)eiTxStartup;
  519.     /* initialize the unit */
  520.     if (eiInit (unit) == ERROR)
  521.         goto error;
  522.     /* Set our flag */
  523.     pDrvCtrl->attached = TRUE;
  524.     /* Set status flags in IDR */
  525.     pDrvCtrl->idr.ac_if.if_flags |= (IFF_UP | IFF_RUNNING | IFF_NOTRAILERS);
  526.     /* Successful return */
  527.     return (OK);
  528.     /***** Handle error cases *****/
  529.     error:
  530.         {
  531.         /* Release system memory */
  532.         if (((int) memBase == NONE) && ((int) pDrvCtrl->memBase != NULL))
  533.             cacheDmaFree (pDrvCtrl->memBase);
  534.         /* Release watchdog */
  535.         if (pDrvCtrl->wid != NULL)
  536.             wdDelete (pDrvCtrl->wid);
  537.         return (ERROR);
  538.         }
  539.     }
  540. /*******************************************************************************
  541. *
  542. * eiInit - Initialize the interface.
  543. *
  544. * Initialization of interface; clear recorded pending operations.
  545. * Called at boot time and by eiWatchDog() if a reset is required.
  546. *
  547. * RETURNS: OK or ERROR
  548. */
  549. static STATUS eiInit 
  550.     (
  551.     int unit /* unit number */
  552.     )
  553.     {
  554.     DRV_CTRL    *pDrvCtrl = & drvCtrl [unit];
  555.     pDrvCtrl->rcvHandling   = FALSE;  /* netTask not running our receive job */
  556.     pDrvCtrl->txCleaning    = FALSE;  /* netTask not running our clean job */
  557.     pDrvCtrl->txIdle        = TRUE;         /* transmitter idle */
  558.     eiQInit ((EI_LIST *)&pDrvCtrl->rxQueue);    /* to be received queue */
  559.     eiQInit ((EI_LIST *)&pDrvCtrl->txQueue);    /* to be sent queue */
  560.     eiQInit ((EI_LIST *)&pDrvCtrl->tfdQueue);   /* free tfds to add to send q */
  561.     eiQInit ((EI_LIST *)&pDrvCtrl->cblQueue);   /* actively sending queue */
  562.     eiQInit ((EI_LIST *)&pDrvCtrl->cleanQueue); /* queue of TFDs to clean */
  563.     pDrvCtrl->wdTxTimeout = 0;
  564.     pDrvCtrl->wdRxTimeout = 0;
  565.     { /***** Perform device initialization *****/
  566.     /* Block local variables */
  567.     int ix;
  568.     sys596IntDisable (unit);            /* disable device interrupts */
  569.     (void) sys596Init (unit);           /* do board specific init */
  570.     /* Connect the interrupt handler */
  571.     if (intConnect ((VOIDFUNCPTR *)INUM_TO_IVEC (pDrvCtrl->ivec),
  572.     eiInt, (int)pDrvCtrl) == ERROR)
  573.         return (ERROR);
  574.     /* Start the device */
  575.     if ( eiDeviceStart (unit) == ERROR )
  576.         return (ERROR);
  577.     eiDiag (unit);                             /* run diagnostics */
  578.     eiConfig (unit);                           /* configure chip */
  579.     eiIASetup (unit);                          /* setup address */
  580.     for (ix = 0; ix < pDrvCtrl->nTFDs; ix ++)  /* tank up the free tfd queue */
  581.         {
  582.         eiQPut  (
  583.                 unit,
  584.                 (EI_LIST *) & pDrvCtrl->tfdQueue,
  585.                 (EI_NODE *) & pDrvCtrl->tfdPool [ix]
  586.                 );
  587.         }
  588.     pDrvCtrl->pFreeRfd  = NULL;                  /* first free RFD */
  589.     pDrvCtrl->nLoanRfds = MAX_RFDS_LOANED;       /* number of loanable RFD's */
  590.     for (ix = 0; ix < pDrvCtrl->nRFDs; ix ++)   /* tank up the receive queue */
  591. {
  592. if (pDrvCtrl->rfdPool[ix].refCnt == 0)
  593.         eiRxQPut (pDrvCtrl, & pDrvCtrl->rfdPool [ix]);
  594. }
  595.     sys596IntAck (unit);                     /* clear any pended dev ints */
  596.     sys596IntEnable (unit);                  /* enable interrupts at system */
  597.     } /* End block */
  598.     wdCancel(pDrvCtrl->wid);                  /* in case re-initializing */
  599.     wdStart(pDrvCtrl->wid, 
  600.     (int) pDrvCtrl->wdInterval, 
  601.     (FUNCPTR) eiWatchDog, 
  602.     pDrvCtrl->idr.ac_if.if_unit
  603.    );
  604.     return (OK);
  605.     }
  606. /*******************************************************************************
  607. *
  608. * eiReset - reset the ei interface
  609. *
  610. * Mark interface as inactive and reset the chip.
  611. */
  612. static void eiReset
  613.     (
  614.     int unit
  615.     )
  616.     {
  617.     DRV_CTRL *pDrvCtrl;
  618.     pDrvCtrl = & drvCtrl [unit];
  619.     pDrvCtrl->idr.ac_if.if_flags = 0;
  620.     sys596IntDisable (unit);                    /* disable ints from the dev */
  621.     sys596Port (unit, PORT_RESET, NULL);        /* reset the 596 chip */
  622.     }
  623. /*******************************************************************************
  624. *
  625. * eiIoctl - interface ioctl procedure
  626. *
  627. * Process an interface ioctl request.
  628. */
  629. static int eiIoctl
  630.     (
  631.     IFNET *  pIfNet,
  632.     int  cmd,
  633.     caddr_t  data
  634.     )
  635.     {
  636.     int error;
  637. #ifdef EI_DEBUG
  638.     printf ("ei: ioctl: pIDR=%x cmd=%x data=%xn", pIDR, cmd, data);
  639. #endif /* EI_DEBUG */
  640.     error = 0;
  641.     switch (cmd)
  642.         {
  643.         case SIOCSIFADDR:
  644.             ((struct arpcom *)pIfNet)->ac_ipaddr = IA_SIN (data)->sin_addr;
  645.             arpwhohas (pIfNet, &IA_SIN (data)->sin_addr);
  646.             break;
  647.         case SIOCSIFFLAGS:
  648.             /* Flags are set outside this module. No additional work to do. */
  649.             break;
  650.         default:
  651.             error = EINVAL;
  652.         }
  653.     return (error);
  654.     }
  655. /*******************************************************************************
  656. *
  657. * eiTxStartup - start output on the chip
  658. *
  659. * Looks for any action on the queue, and begins output if there is anything
  660. * there.  This routine is called from several possible threads.  Each will be
  661. * described below.
  662. *
  663. * The first, and most common thread, is when a user task requests the
  664. * transmission of data.  This will cause eiOutput() to be called, which will
  665. * cause ether_output() to be called, which will cause this routine to be
  666. * called (usually).  This routine will not be called if ether_output() finds
  667. * that our interface output queue is full.  In this case, the outgoing data
  668. * will be thrown out.
  669. *
  670. * The second, and most obscure thread, is when the reception of certain
  671. * packets causes an immediate (attempted) response.  For example, ICMP echo
  672. * packets (ping), and ICMP "no listener on that port" notifications.  All
  673. * functions in this driver that handle the reception side are executed in the
  674. * context of netTask().  Always.  So, in the case being discussed, netTask() 
  675. * will receive these certain packets, cause IP to be stimulated, and cause the
  676. * generation of a response to be sent.  We then find ourselves following the
  677. * thread explained in the second example, with the important distinction that
  678. * the context is that of netTask().
  679. *
  680. * The third thread occurs when this routine runs out of TFDs and returns.  If
  681. * this occurs when our output queue is not empty, this routine would typically
  682. * not get called again until new output was requested.  Even worse, if the
  683. * output queue was also full, this routine would never get called again and
  684. * we would have a lock state.  It DOES happen.  To guard against this, the
  685. * transmit clean-up handler detects the out-of-TFDs state and calls this
  686. * function.  The clean-up handler also runs from netTask.
  687. *
  688. * Note that this function is ALWAYS called between an splnet() and an splx().
  689. * This is true because netTask(), and ether_output() take care of
  690. * this when calling this function.  Therefore, no calls to these spl functions
  691. * are needed anywhere in this output thread.
  692. */
  693. void eiTxStartup
  694.     (
  695.     DRV_CTRL *pDrvCtrl
  696.     )
  697.     {
  698.     MBUF * pMbuf;
  699.     int length;
  700.     TFD * pTfd;
  701.     /*
  702.      * Loop until there are no more packets ready to send or we
  703.      * have insufficient resources left to send another one.
  704.      */
  705.     while (pDrvCtrl->idr.ac_if.if_snd.ifq_head)
  706.         {
  707.         IF_DEQUEUE (&pDrvCtrl->idr.ac_if.if_snd, pMbuf);  /* dequeue a packet */
  708.         /* get free tfd */
  709.         pTfd = (TFD *) eiQGet ((EI_LIST *)&pDrvCtrl->tfdQueue);
  710.         if (pTfd == NULL)
  711.     {
  712.     m_freem (pMbuf);
  713.             break;                              /* out of TFD's */
  714.     }
  715.         copy_from_mbufs ((ETH_HDR *)pTfd->enetHdr, pMbuf, length);
  716.         length = max (ETHERSMALL, length);
  717.         if ((etherOutputHookRtn != NULL) &&
  718.             (* etherOutputHookRtn)
  719.             (&pDrvCtrl->idr, (ETH_HDR *)pTfd->enetHdr, length))
  720.             continue;
  721.         pTfd->count = length & ~0xc000;     /* fill in length */
  722.         eiTxQPut (pDrvCtrl, pTfd);
  723.         }
  724.     }
  725. /*******************************************************************************
  726. *
  727. * eiInt - entry point for handling interrupts from the 82596
  728. *
  729. * The interrupting events are acknowledged to the device, so that the device
  730. * will deassert its interrupt signal.  The amount of work done here is kept
  731. * to a minimum; the bulk of the work is defered to the netTask.  Several flags
  732. * are used here to synchronize with task level code and eliminate races.
  733. */
  734. static void eiInt
  735.     (
  736.     DRV_CTRL *pDrvCtrl
  737.     )
  738.     {
  739.     UINT16  event;
  740.     SCB *pScb;
  741.     int unit;
  742.     pScb = pDrvCtrl->pScb;
  743.     unit = pDrvCtrl->idr.ac_if.if_unit;
  744.     event = pScb->scbStatus & (SCB_S_CX | SCB_S_FR | SCB_S_CNA | SCB_S_RNR);
  745. #ifdef EI_DEBUG
  746.     logMsg ("ei: interrupt: event=%xn", event, 0, 0, 0, 0, 0);
  747. #endif /* EI_DEBUG */
  748.     eiCommand (pDrvCtrl, event);                        /* ack the events */
  749.     sys596IntAck (unit);                                /* ack 596 interrupt */
  750.     /* Handle transmitter interrupt */
  751.     if (event & SCB_S_CNA)                              /* reclaim tx tfds */
  752.         {
  753.         pDrvCtrl->wdTxTimeout = 0;              /* reset timeout count */
  754.         if (pDrvCtrl->cleanQueue.head == NULL)      /* clean queue empty */
  755.             {
  756.             pDrvCtrl->cleanQueue.head = pDrvCtrl->cblQueue.head; /* new head */
  757.             pDrvCtrl->cleanQueue.tail = pDrvCtrl->cblQueue.tail; /* new tail */
  758.             }
  759.         else                                            /* concatenate queues */
  760.             {
  761.             pDrvCtrl->cleanQueue.tail->pNext =
  762.                         (EI_NODE*) pDrvCtrl->cblQueue.head;
  763.             pDrvCtrl->cleanQueue.tail = pDrvCtrl->cblQueue.tail;
  764.             }
  765.         if (!pDrvCtrl->txCleaning)                          /* not cleaning? */
  766.             {
  767.             pDrvCtrl->txCleaning = TRUE;                    /* set flag */
  768.             netJobAdd ((FUNCPTR) eiTxCleanQ, (int) pDrvCtrl, 0, 0, 0, 0);
  769.                             /* defer cleanup */
  770.             }
  771.         if (pDrvCtrl->txQueue.head != NULL)             /* anything to flush? */
  772.             eiTxQFlush (pDrvCtrl);                      /* flush the tx q */
  773.         else
  774.             pDrvCtrl->txIdle = TRUE;                    /* transmitter idle */
  775.         }
  776.     /* Handle receiver interrupt */
  777.     if (event & SCB_S_FR)
  778. {
  779. pDrvCtrl->wdRxTimeout = 0;                  /* reset timeout count */
  780. if (!(pDrvCtrl->rcvHandling))
  781.             {
  782.             pDrvCtrl->rcvHandling = TRUE;
  783.             (void) netJobAdd ((FUNCPTR) eiHandleRecvInt, (int) pDrvCtrl,0, 0, 0, 0);         /* netTask processes */
  784.     }
  785.         }
  786.     }
  787. /*******************************************************************************
  788. *
  789. * eiTxCleanQ - checks errors in completed TFDs and moves TFDs to free queue
  790. *
  791. * This routine is executed by netTask.  It "cleans" the TFDs on the clean-up
  792. * queue by checking each one for errors and then returning the TFD to the
  793. * "free TFD" queue.  The startup routine is sometimes called here to eliminate
  794. * the lock-out case where the driver input queue is full but there are no
  795. * TFDs available.
  796. */
  797. static void eiTxCleanQ
  798.     (
  799.     DRV_CTRL *pDrvCtrl
  800.     )
  801.     {
  802.     TFD *pTfd;
  803.     BOOL needTxStart;
  804.     int unit;
  805.     unit = pDrvCtrl->idr.ac_if.if_unit;
  806.     do
  807.         {
  808.         pDrvCtrl->txCleaning = TRUE;
  809.         if (pDrvCtrl->tfdQueue.head == NULL)              /* tfd queue empty */
  810.             needTxStart = TRUE;                           /* set flag */
  811.         else
  812.             needTxStart = FALSE;
  813.         /* process transmitted frames */
  814.         while (1)
  815.             {
  816.             /* Get TFD. No ints allowed while manipulating this queue. */
  817.             sys596IntDisable (unit);
  818.             pTfd = (TFD*) eiQGet ((EI_LIST *)&pDrvCtrl->cleanQueue);
  819.             sys596IntEnable (unit);
  820.             if (pTfd == NULL)
  821.                 break;
  822.             pDrvCtrl->idr.ac_if.if_collisions +=         /* add any colls */
  823.                 (pTfd->status & CFD_S_RETRY) ? 16 :  /* excessive colls */
  824.                 (pTfd->status & CFD_S_COLL_MASK);    /* some colls */
  825.             if (!(pTfd->status & CFD_S_OK))          /* packet not sent */
  826.                 {
  827.                 pDrvCtrl->idr.ac_if.if_oerrors++;        /* incr err count */
  828.                 pDrvCtrl->idr.ac_if.if_opackets--;       /* decr sent count */
  829.                 }
  830.             /* return to tfdQ */
  831.             eiQPut (unit,(EI_LIST *)&pDrvCtrl->tfdQueue, (EI_NODE*)pTfd);
  832.             }
  833.         if (needTxStart)                                  /* check flag */
  834.             eiTxStartup (pDrvCtrl);
  835.         pDrvCtrl->txCleaning = FALSE;
  836.         }
  837.     while (pDrvCtrl->cleanQueue.head != NULL);            /* check again */
  838.     }
  839. /*******************************************************************************
  840. *
  841. * eiHandleRecvInt - task level interrupt service for input packets
  842. *
  843. * This routine is called at task level indirectly by the interrupt
  844. * service routine to do any message received processing.
  845. */
  846. static void eiHandleRecvInt
  847.     (
  848.     DRV_CTRL *pDrvCtrl
  849.     )
  850.     {
  851.     RFD *pRfd;
  852.     do
  853.         {
  854.         pDrvCtrl->rcvHandling = TRUE;             /* interlock with eiInt() */
  855.         while ((pRfd = eiRxQGet (pDrvCtrl)) != NULL)
  856.             if (eiReceive (pDrvCtrl, pRfd) == OK)
  857.                 eiRxQPut (pDrvCtrl, pRfd);
  858.         pDrvCtrl->rcvHandling = FALSE;            /* interlock with eiInt() */
  859.         }
  860.     while (eiRxQFull (pDrvCtrl));              /* make sure rx q still empty */
  861.     }
  862. /*******************************************************************************
  863. *
  864. * eiReceive - pass a received frame to the next layer up
  865. *
  866. * Strips the Ethernet header and passes the packet to the appropriate
  867. * protocol.  The return value indicates if buffer loaning was used to hold
  868. * the data.  A return value of OK means that loaning was not done, and it
  869. * is therefore 'ok' to return the RFD to the Rx queue.  A return value of ERROR
  870. * means that buffer loaning was employed, and so the RFD is still in use and
  871. * should not be returned to the Rx queue.  In this latter case, the RFD will
  872. * eventually be returned by the protocol, via a call to our eiLoanFree().
  873. */
  874. static STATUS eiReceive
  875.     (
  876.     DRV_CTRL *pDrvCtrl,
  877.     RFD *pRfd
  878.     )
  879.     {
  880.     ETH_HDR     *pEh;
  881.     u_char      *pData;
  882.     int         len;
  883.     UINT16      etherType;
  884.     MBUF        *m = NULL;
  885.     BOOL        rfdLoaned = FALSE;
  886.     /* Check packet for errors.  This should be completely unnecessary,
  887.      * but since Intel does not seem capable of explaining the exact
  888.      * functioning of the 'save bad frames' config bit, we will look for
  889.      * errors.
  890.      */
  891.     if  (
  892.         ( pRfd->status & ( RFD_S_OK | RFD_S_COMPLETE ) ) !=
  893.         ( RFD_S_OK | RFD_S_COMPLETE )
  894.         )
  895.         {
  896.         ++pDrvCtrl->idr.ac_if.if_ierrors;            /* bump error counter */
  897.         eiRxQPut (pDrvCtrl, pRfd);                   /* free the RFD */
  898.         return (ERROR);
  899.         }
  900.     /* Bump input packet counter. */
  901.     ++pDrvCtrl->idr.ac_if.if_ipackets;
  902.     len = pRfd->actualCnt & ~0xc000;        /* get frame length */
  903.     pEh = (ETH_HDR *)pRfd->enetHdr;         /* get ptr to ethernet header */
  904.     /* Service input hook */
  905.     if ( (etherInputHookRtn != NULL) )
  906.         {
  907.         if  ( (* etherInputHookRtn) (&pDrvCtrl->idr, (char *)pEh, len) )
  908.             {
  909.             eiRxQPut (pDrvCtrl, pRfd);                  /* free the RFD */
  910.             return (OK);
  911.             }
  912.         }
  913.     len -= EH_SIZE;
  914.     pData = (u_char *) pRfd->enetData;
  915.     etherType = ntohs (pEh->ether_type);
  916.     /* we can loan out receive frames from 82596 receive queue if:
  917.      *
  918.      * 1) the threshold of loanable frames has not been exceeded
  919.      * 2) size of the input ethernet frame is large enough to be used with
  920.      *    clustering.
  921.      */
  922.     if ((pDrvCtrl->nLoanRfds > 0) && 
  923.         ((m = build_cluster (pData, len, &pDrvCtrl->idr, MC_EI, &pRfd->refCnt,
  924.                  eiLoanFree, (int) pDrvCtrl, (int) pRfd, NULL)) != NULL))
  925.         {
  926.         pDrvCtrl->nLoanRfds --;             /* one less to loan */
  927.         rfdLoaned = TRUE;               /* we loaned a frame */
  928.         }
  929.     else 
  930.         m = copy_to_mbufs (pData, len, 0, &pDrvCtrl->idr);
  931.     if (m != NULL)
  932. #ifdef BSD43_DRIVER
  933.         do_protocol_with_type (etherType, m, &pDrvCtrl->idr, len);
  934. #else
  935.         do_protocol (pEh, m, &pDrvCtrl->idr, len);
  936. #endif 
  937.     else
  938.         pDrvCtrl->idr.ac_if.if_ierrors++;
  939.         
  940.     return ((rfdLoaned) ? ERROR : OK);
  941.     }
  942. /*******************************************************************************
  943. *
  944. * eiLoanFree - return a loaned receive frame descriptor
  945. *
  946. * This routine is called by the protocol code when it has completed use of
  947. * an RFD that we loaned to it.
  948. */
  949. static void eiLoanFree
  950.     (
  951.     DRV_CTRL *pDrvCtrl,
  952.     RFD *pRfd
  953.     )
  954.     {
  955.     eiRxQPut (pDrvCtrl, pRfd);
  956.     pDrvCtrl->nLoanRfds ++;
  957.     }
  958. /*******************************************************************************
  959. *
  960. * eiDeviceStart - reset and start the device
  961. *
  962. * This routine assumes interrupts from the device have been disabled, and
  963. * that the driver control structure has been initialized.
  964. */
  965. static STATUS eiDeviceStart
  966.     (
  967.     int unit                              /* physical unit number */
  968.     )
  969.     {
  970.     void     *pTemp;
  971.     DRV_CTRL *pDrvCtrl;
  972.     SCP      *pScp;                       /* system config ptr */
  973.     ISCP     *pIscp;                      /* intermediate system config ptr */
  974.     SCB      *pScb;                       /* system control block ptr */
  975.     /* Get pointers */
  976.     pDrvCtrl = & drvCtrl [unit];
  977.     pScp = pDrvCtrl->pScp;
  978.     pIscp = pDrvCtrl->pIscp;
  979.     pScb = pDrvCtrl->pScb;
  980.     /* Issue the reset operation to the device */
  981.     sys596Port (unit, PORT_RESET, NULL);
  982.     /* Initialize the SCP */
  983.     pScp->scpRsv1 =
  984.     pScp->scpRsv2 =
  985.     pScp->scpRsv3 = 0;
  986.     pScp->scpSysbus = pDrvCtrl->sysbus;
  987.     pTemp = CACHE_DRV_VIRT_TO_PHYS (&pDrvCtrl->cacheFuncs, pIscp);
  988.     LINK_WR (&pScp->pIscp, pTemp);              /* point SCP to ISCP */
  989.     /* Initialize the ISCP */
  990.     pIscp->iscpBusy = 1;
  991.     pIscp->iscpRsv1 = 0;
  992.     pTemp = CACHE_DRV_VIRT_TO_PHYS (&pDrvCtrl->cacheFuncs, pScb);
  993.     LINK_WR (&pIscp->pScb, pTemp);              /* point ISCP to SCB */
  994.     /* Initialize the SCB */
  995.     bzero ((char *)pScb, sizeof (SCB));
  996.     /* Tell the device where the SCP is located */
  997.     pTemp = CACHE_DRV_VIRT_TO_PHYS (&pDrvCtrl->cacheFuncs, pScp);
  998.     sys596Port (unit, PORT_NEWSCP, (UINT32) pTemp);
  999.     sys596ChanAtn (unit);
  1000.     /*
  1001.      * The device will now read our SCP and ISCP. It will clear the busy
  1002.      * flag in the ISCP.
  1003.      */
  1004.     taskDelay (50);
  1005.     if ( pIscp->iscpBusy == 1 )
  1006.         {
  1007.         printf ("nei: device did not initializen");
  1008.         return (ERROR);
  1009.         }
  1010.     return (OK);
  1011.     }
  1012. /*******************************************************************************
  1013. *
  1014. * eiDiag - format and issue a diagnostic command
  1015. */
  1016. static void eiDiag
  1017.     (
  1018.     int unit
  1019.     )
  1020.     {
  1021.     DRV_CTRL *pDrvCtrl;
  1022.     pDrvCtrl = & drvCtrl [unit];
  1023.     bzero ((char *)pDrvCtrl->pCfd, sizeof (CFD));       /* zero command frame */
  1024.     eiAction (unit, CFD_C_DIAG);                /* run diagnostics */
  1025.     if (!(pDrvCtrl->pCfd->cfdStatus & CFD_S_OK))
  1026.         printErr ("eiDiag: i82596 diagnostics failed.n");
  1027.     }
  1028. /*******************************************************************************
  1029. *
  1030. * eiConfig - format and issue a config command
  1031. */
  1032. static void eiConfig
  1033.     (
  1034.     int unit
  1035.     )
  1036.     {
  1037.     DRV_CTRL *pDrvCtrl;
  1038.     pDrvCtrl = & drvCtrl [unit];
  1039.     bzero ((char *)pDrvCtrl->pCfd, sizeof (CFD));       /* zero command frame */
  1040.     /* Recommeded i82596 User's Manual configuration values.  Note that
  1041.      * the original manual, #296443-001, was full of errors.  Errors in the
  1042.      * description of the config bytes that I am aware of are noted below.
  1043.      * It is possible there are further errors.  Intel has replaced the
  1044.      * manual with #296853-001, which does correct some errors, but not all.
  1045.      */
  1046.     pDrvCtrl->pCfd->cfdConfig.ccByte8  = 0x8e;
  1047.     pDrvCtrl->pCfd->cfdConfig.ccByte9  = 0xc8;
  1048.     /* The manual is wrong about bit 7 in byte 10.
  1049.      * A '0' allows reception of bad packets.
  1050.      * A '1' causes rejection of bad packets.
  1051.      */
  1052.     pDrvCtrl->pCfd->cfdConfig.ccByte10 = 0xc0;
  1053.     pDrvCtrl->pCfd->cfdConfig.ccByte11 = 0x2e;      /* loopback, NSAI */
  1054.     pDrvCtrl->pCfd->cfdConfig.ccByte12 = 0x00;
  1055.     pDrvCtrl->pCfd->cfdConfig.ccByte13 = 0x60;
  1056.     pDrvCtrl->pCfd->cfdConfig.ccByte14 = 0x00;
  1057.     pDrvCtrl->pCfd->cfdConfig.ccByte15 = 0xf2;
  1058.     pDrvCtrl->pCfd->cfdConfig.ccByte16 = 0x00;      /* promiscuous off */
  1059.     pDrvCtrl->pCfd->cfdConfig.ccByte17 = 0x00;
  1060.     pDrvCtrl->pCfd->cfdConfig.ccByte18 = 0x40;
  1061.     /* The manual is wrong about 2 bits in byte 19.
  1062.      * Bit 5, multicast, a '1' disables.
  1063.      * Bit 2, include CRC, a '1' disables.
  1064.      */
  1065.     pDrvCtrl->pCfd->cfdConfig.ccByte19 = 0xff;
  1066.     pDrvCtrl->pCfd->cfdConfig.ccByte20 = 0x00;
  1067.     pDrvCtrl->pCfd->cfdConfig.ccByte21 = 0x3f;
  1068.     eiAction (unit, CFD_C_CONFIG);          /* configure the chip */
  1069.     }
  1070. /*******************************************************************************
  1071. *
  1072. * eiIASetup - format and issue an interface address command
  1073. */
  1074. static void eiIASetup
  1075.     (
  1076.     int unit
  1077.     )
  1078.     {
  1079.     DRV_CTRL *pDrvCtrl;
  1080.     pDrvCtrl = & drvCtrl [unit];
  1081.     bzero ((char *)pDrvCtrl->pCfd, sizeof (CFD));       /* zero command frame */
  1082.     bcopy   (
  1083.             (char *)pDrvCtrl->idr.ac_enaddr,
  1084.             (char *)pDrvCtrl->pCfd->cfdIASetup.ciAddress,
  1085.             6
  1086.             );
  1087.     eiAction (unit, CFD_C_IASETUP);         /* setup the address */
  1088.     }
  1089. /*******************************************************************************
  1090. *
  1091. * eiRxStartup - start up the Receive Unit
  1092. *
  1093. * Starts up the Receive Unit.  Assumes that the receive structures are set up.
  1094. */
  1095. static void eiRxStartup
  1096.     (
  1097.     DRV_CTRL *pDrvCtrl
  1098.     )
  1099.     {
  1100.     SCB *pScb = pDrvCtrl->pScb;
  1101.     void * pTemp;
  1102.     if (pScb->scbStatus & SCB_S_RUREADY)        /* already running */
  1103.         return;
  1104.     pTemp = CACHE_DRV_VIRT_TO_PHYS (&pDrvCtrl->cacheFuncs, pDrvCtrl->pFreeRfd);
  1105.     LINK_WR (&pScb->pRF, pTemp);            /* point to free Rfd */
  1106.     sys596IntDisable (pDrvCtrl->idr.ac_if.if_unit);
  1107.     if ((pScb->scbStatus & SCB_S_RUMASK) != SCB_S_RUIDLE)
  1108.         eiCommand (pDrvCtrl, SCB_C_RUABORT);            /* abort if not idle */
  1109.     eiCommand (pDrvCtrl, SCB_C_RUSTART);              /* start receive unit */
  1110.     sys596IntEnable (pDrvCtrl->idr.ac_if.if_unit);
  1111.     }
  1112. /*******************************************************************************
  1113. *
  1114. * eiAction - execute the specified action with the CFD pointed to in pDrvCtrl
  1115. *
  1116. * Do the command contained in the CFD synchronously, so that we know
  1117. * it's complete upon return.  This routine assumes that interrupts from the
  1118. * device have been disabled.
  1119. */
  1120. static void eiAction
  1121.     (
  1122.     int    unit,
  1123.     UINT16 action
  1124.     )
  1125.     {
  1126.     void     *pTemp;
  1127.     CFD      *pCfd;
  1128.     SCB      *pScb;
  1129.     DRV_CTRL *pDrvCtrl;
  1130.     pDrvCtrl = & drvCtrl [unit];
  1131.     pCfd = pDrvCtrl->pCfd;
  1132.     pScb = pDrvCtrl->pScb;
  1133.     while (1)                   /* wait for idle command unit */
  1134.         {
  1135.         if ((pScb->scbStatus & SCB_S_CUMASK) == SCB_S_CUIDLE)
  1136.             break;
  1137.         }
  1138.     { /* Prepare and issue the command to the device */
  1139.     /* fill in CFD */
  1140.     pCfd->cfdStatus  = 0;                       /* clear status */
  1141.     pCfd->cfdCommand = CFD_C_EL | action;       /* fill in action */
  1142.     LINK_WR (&pCfd->link, NULL);                /* terminate link */
  1143.     /* and the SCB */
  1144.     pScb->scbCommand =
  1145.                         SCB_S_CX |              /* ack any events */
  1146.                         SCB_S_FR |
  1147.                         SCB_S_CNA |
  1148.                         SCB_S_RNR |
  1149.                         SCB_C_CUSTART;          /* start command unit */
  1150.     pTemp = CACHE_DRV_VIRT_TO_PHYS (&pDrvCtrl->cacheFuncs, pCfd);
  1151.     LINK_WR (&pScb->pCB, pTemp);                /* point chip at CFD */
  1152.     sys596ChanAtn (unit);               /* notify device of new command */
  1153.     }
  1154.     while (1)               /* wait for command acceptance and interrupt */
  1155.         {
  1156.         if ((pScb->scbCommand == 0) && (pScb->scbStatus & SCB_S_CNA))
  1157.             break;
  1158.         }
  1159.     /* Acknowledge the event to the device */
  1160.     pScb->scbCommand = (SCB_S_CX | SCB_S_CNA);
  1161.     sys596ChanAtn (unit);
  1162.     while (1)               /* wait for acknowledge acceptance */
  1163.         {
  1164.         if (pScb->scbCommand == 0)
  1165.             break;
  1166.         }
  1167.     }
  1168. /*******************************************************************************
  1169. *
  1170. * eiCommand - deliver a command to the 82596 via SCB
  1171. *
  1172. * This function causes the device to execute a command.  It should be called
  1173. * with interrupts from the device disabled.  An error status is returned if
  1174. * the command field does not return to zero, from a previous command, in a
  1175. * reasonable amount of time.
  1176. */
  1177. static STATUS eiCommand
  1178.     (
  1179.     DRV_CTRL *pDrvCtrl,
  1180.     UINT16    cmd
  1181.     )
  1182.     {
  1183.     int loopy;
  1184.     SCB * pScb;
  1185.     pScb = pDrvCtrl->pScb;
  1186.     for (loopy = 0x8000; loopy--;)
  1187.         {
  1188.         if (pScb->scbCommand == 0)                  /* wait for cmd zero */
  1189.             break;
  1190.         }
  1191.     if (loopy > 0)
  1192.         {
  1193.         pScb->scbCommand = cmd;                     /* fill in command */
  1194.         sys596ChanAtn (pDrvCtrl->idr.ac_if.if_unit);    /* channel attention */
  1195.         return (OK);
  1196.         }
  1197.     else
  1198.         {
  1199.         logMsg("ei driver: command field frozenn", 0, 0, 0, 0, 0, 0);
  1200.         return (ERROR);
  1201.         }
  1202.     }
  1203. /*******************************************************************************
  1204. *
  1205. * eiTxQPut - place a transmit frame on the transmit queue
  1206. *
  1207. * The TFD has been filled in with the network pertinent data.  This
  1208. * routine will enqueue the TFD for transmission and attempt to feed
  1209. * the queue to the device.
  1210. */
  1211. static void eiTxQPut
  1212.     (
  1213.     DRV_CTRL *pDrvCtrl,
  1214.     TFD *pTfd
  1215.     )
  1216.     {
  1217.     int unit;
  1218.     unit = pDrvCtrl->idr.ac_if.if_unit;
  1219.     pTfd->status    = 0;                    /* fill in TFD fields */
  1220.     pTfd->command   = CFD_C_XMIT;           /* EL set later */
  1221.     pTfd->count     |= TFD_CNT_EOF;         /* data kept in frame */
  1222.     pTfd->reserved  = 0;                    /* must be zero */
  1223.     LINK_WR (& pTfd->lBufDesc, NULL);                /* TBDs not used */
  1224.     sys596IntDisable (pDrvCtrl->idr.ac_if.if_unit);  /* disable dev ints */
  1225.     /* enqueue the TFD */
  1226.     eiQPut (unit,(EI_LIST *)&pDrvCtrl->txQueue, (EI_NODE*)pTfd);
  1227.     if (pDrvCtrl->txIdle)                             /* transmitter idle */
  1228.         eiTxQFlush (pDrvCtrl);                        /* flush txQueue */
  1229.     sys596IntEnable (pDrvCtrl->idr.ac_if.if_unit);  /* enable dev ints */
  1230.     }
  1231. /*******************************************************************************
  1232. *
  1233. * eiTxQFlush - make cmd unit of device start processing cmds
  1234. *
  1235. * This routine flushes the contents of the txQ to the cblQ and starts the
  1236. * device transmitting the cblQ.  Called only if transmit queue is not empty.
  1237. * Sometimes called from interrupt handler.
  1238. */
  1239. static void eiTxQFlush
  1240.     (
  1241.     DRV_CTRL *pDrvCtrl
  1242.     )
  1243.     {
  1244.     void * pTemp;
  1245.     extern int sysClkRateGet();     /* we call this */
  1246.     ((TFD*)pDrvCtrl->txQueue.tail)->command |= CFD_C_EL;  /* EL terminate q */
  1247.     pDrvCtrl->cblQueue.head = pDrvCtrl->txQueue.head;   /* remember cbl head */
  1248.     pDrvCtrl->cblQueue.tail = pDrvCtrl->txQueue.tail;   /* remember cbl tail */
  1249.     eiQInit ((EI_LIST *)&pDrvCtrl->txQueue);            /* tx queue now empty */
  1250.     pTemp = CACHE_DRV_VIRT_TO_PHYS (&pDrvCtrl->cacheFuncs,
  1251.                                     pDrvCtrl->cblQueue.head);
  1252.     LINK_WR (&pDrvCtrl->pScb->pCB, pTemp);          /* point CU to head */
  1253.     pDrvCtrl->txIdle = FALSE;                   /* transmitter busy */
  1254.     /* start command unit */
  1255.     eiCommand (pDrvCtrl, SCB_C_CUSTART);
  1256.     }
  1257. /*******************************************************************************
  1258. *
  1259. * eiRxQPut - return a RFD to the receive queue for use by the device
  1260. */
  1261. static void eiRxQPut
  1262.     (
  1263.     DRV_CTRL *pDrvCtrl,
  1264.     RFD *pRfd
  1265.     )
  1266.     {
  1267.     int unit;
  1268.     RFD *pTail;
  1269.     unit = pDrvCtrl->idr.ac_if.if_unit;
  1270.     pRfd->status    = 0;                    /* clear status */
  1271.     pRfd->command   = CFD_C_EL;             /* new end of list */
  1272.     pRfd->actualCnt  = 0;                   /* clear actual count */
  1273.     pRfd->bufSize   = ETHERMTU + EH_SIZE;   /* fill in size */
  1274.     pRfd->refCnt    = 0;                    /* initialize ref cnt */
  1275.     LINK_WR (& pRfd->lBufDesc, 0xffffffff);         /* no RBD used */
  1276.     pTail = (RFD *) pDrvCtrl->rxQueue.tail;         /* remember tail */
  1277.     /* Put the RFD on the list */
  1278.     eiQPut (unit, (EI_LIST *) & pDrvCtrl->rxQueue, (EI_NODE *) pRfd);
  1279.     if (pTail != NULL)
  1280.         {
  1281.         pDrvCtrl->wdRxTimeout = 0;          /* reset timeout count */
  1282.         pTail->command &= ~CFD_C_EL;        /* clear old tail EL */
  1283.         if (pTail->status & (CFD_S_COMPLETE | CFD_S_BUSY))
  1284.             {
  1285.             pDrvCtrl->pFreeRfd = pRfd;          /* link questionable */
  1286.             }
  1287.         else if (!(pDrvCtrl->pScb->scbStatus & SCB_S_RUREADY))
  1288.             /* receiver dormant */
  1289.             {
  1290.             eiRxStartup (pDrvCtrl);             /* start receive unit */
  1291.             }
  1292.         }
  1293.     else
  1294.         {
  1295.         pDrvCtrl->pFreeRfd = pRfd;              /* first free RFD */
  1296.         }
  1297.     }
  1298. /*******************************************************************************
  1299. *
  1300. * eiRxQGet - get a successfully received frame from the receive queue
  1301. *
  1302. * RETURNS: ptr to valid RFD, or NULL if none available
  1303. */
  1304. static RFD *eiRxQGet
  1305.     (
  1306.     DRV_CTRL *pDrvCtrl
  1307.     )
  1308.     {
  1309.     RFD *pRfd = NULL;
  1310.     if (eiRxQFull (pDrvCtrl))
  1311.         pRfd = (RFD *) eiQGet ((EI_LIST *)&pDrvCtrl->rxQueue);
  1312.     return (pRfd);
  1313.     }
  1314. /*******************************************************************************
  1315. *
  1316. * eiRxQFull - boolean function to determine fullness of receive queue
  1317. *
  1318. * RETURNS: TRUE if completely received frame is available, FALSE otherwise.
  1319. */
  1320. static BOOL eiRxQFull
  1321.     (
  1322.     DRV_CTRL *pDrvCtrl
  1323.     )
  1324.     {
  1325.     return ((pDrvCtrl->rxQueue.head != NULL) &&
  1326.         (((RFD*)pDrvCtrl->rxQueue.head)->status & CFD_S_COMPLETE));
  1327.     }
  1328. /*******************************************************************************
  1329. *
  1330. * eiQInit - initialize a singly linked node queue
  1331. */
  1332. static void eiQInit
  1333.     (
  1334.     EI_LIST *pQueue
  1335.     )
  1336.     {
  1337.     pQueue->head = pQueue->tail = NULL;         /* init head & tail */
  1338.     }
  1339. /*******************************************************************************
  1340. *
  1341. * eiQGet - get a node from the head of a node queue
  1342. *
  1343. * RETURNS: ptr to useable node, or NULL ptr if none available
  1344. */
  1345. static EI_NODE *eiQGet
  1346.     (
  1347.     EI_LIST *pQueue
  1348.     )
  1349.     {
  1350.     EI_NODE *pNode;
  1351.     if ((pNode = (EI_NODE *) pQueue->head) != NULL)     /* if list not empty */
  1352.         pQueue->head = pNode->pNext;                    /* advance ptr */
  1353.     return (pNode);
  1354.     }
  1355. /*******************************************************************************
  1356. *
  1357. * eiQPut - put a node on the tail of a node queue
  1358. */
  1359. static void eiQPut
  1360.     (
  1361.     int unit,
  1362.     EI_LIST *pQueue,
  1363.     EI_NODE *pNode
  1364.     )
  1365.     {
  1366.     void * pTemp;
  1367.     DRV_CTRL *pDrvCtrl;
  1368.     pDrvCtrl = & drvCtrl [unit];
  1369.     LINK_WR (&pNode->lNext, NULL);                    /* mark "end of list" */
  1370.     pNode->pNext = NULL;
  1371.     if (pQueue->head == NULL)                        /* if list empty */
  1372.         pQueue->tail = pQueue->head = pNode;         /* set both ptrs */
  1373.     else
  1374.         {
  1375.         pTemp = CACHE_DRV_VIRT_TO_PHYS (&pDrvCtrl->cacheFuncs, pNode);
  1376.         LINK_WR (&pQueue->tail->lNext, pTemp);       /* link node on tail */
  1377.         pQueue->tail->pNext = pNode;
  1378.         pQueue->tail = pNode;                        /* update tail ptr */
  1379.         }
  1380.     }
  1381. /*******************************************************************************
  1382. *
  1383. * eiWatchDog - if the watchdog timer fired off, we've hung during a transmit
  1384. *
  1385. * Check the scb command to verify and if so, reinit.
  1386. */
  1387. static void eiWatchDog
  1388.     (
  1389.     int unit        /* unit number */
  1390.     )
  1391.     {
  1392.     DRV_CTRL *pDrvCtrl = &drvCtrl [unit];
  1393.     SCB *pScb;
  1394.     int reset = FALSE;
  1395.     pScb = pDrvCtrl->pScb;
  1396.     /* Test for transmit timeout.
  1397.      *
  1398.      * Timeout occurs if the scb status indicates that CU (transmit) 
  1399.      * remains active for EI_TX_TIMEOUT iterations of eiWatchDog.
  1400.      * It might make sense to loop through the cfd's to look for
  1401.      * a complete bit as a sanity check, but given that transmit
  1402.      * was active, we will go ahead and do a reset.
  1403.      */
  1404.     if ((pDrvCtrl->txIdle == FALSE)
  1405.         && (pScb->scbStatus & SCB_S_CUACTIVE))
  1406.         {
  1407.         if (++(pDrvCtrl->wdTxTimeout) > EI_TX_TIMEOUT)
  1408.             {
  1409.             pDrvCtrl->transLocks++;   /* failure count */
  1410.             pDrvCtrl->idr.ac_if.if_oerrors++;             /* incr err count */
  1411.             pDrvCtrl->idr.ac_if.if_opackets--;            /* decr sent count */
  1412.             reset = TRUE;
  1413.             }
  1414.         }
  1415.     /* Test for receive timeout.
  1416.      *
  1417.      * Timeout occurs if the scb status indicates that RU (receive) 
  1418.      * remains out of resources for EI_RX_TIMEOUT iterations of eiWatchDog.
  1419.      */
  1420.     if (pScb->scbStatus & SCB_S_RUNORSRC)
  1421.         {
  1422.         if (++(pDrvCtrl->wdRxTimeout) > EI_RX_TIMEOUT)
  1423.             {
  1424.             pDrvCtrl->recvLocks++;   /* failure count */
  1425.             reset = TRUE;
  1426.             }
  1427.         }
  1428.     /* reinitialize the unit or restart the watchdog */
  1429.     if (reset)
  1430. netJobAdd ((FUNCPTR) eiInit, unit, 0, 0, 0, 0);
  1431.     else
  1432.         wdStart (pDrvCtrl->wid, 
  1433. (int) pDrvCtrl->wdInterval, 
  1434. (FUNCPTR) eiWatchDog, 
  1435. unit);
  1436.     }
  1437. /******************************************************************************/
  1438. /* END OF FILE */