]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/devs/eth/intel/i82559/v2_0/src/if_i82559.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / devs / eth / intel / i82559 / v2_0 / src / if_i82559.c
1 //==========================================================================
2 //
3 //      if_i82559.c
4 //
5 //      Intel 82559 ethernet driver
6 //
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Red Hat, Inc.
12 // Copyright (C) 2002, 2003 Gary Thomas
13 //
14 // eCos is free software; you can redistribute it and/or modify it under
15 // the terms of the GNU General Public License as published by the Free
16 // Software Foundation; either version 2 or (at your option) any later version.
17 //
18 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21 // for more details.
22 //
23 // You should have received a copy of the GNU General Public License along
24 // with eCos; if not, write to the Free Software Foundation, Inc.,
25 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 //
27 // As a special exception, if other files instantiate templates or use macros
28 // or inline functions from this file, or you compile this file and link it
29 // with other works to produce a work based on this file, this file does not
30 // by itself cause the resulting work to be covered by the GNU General Public
31 // License. However the source code for this file must still be made available
32 // in accordance with section (3) of the GNU General Public License.
33 //
34 // This exception does not invalidate any other reasons why a work based on
35 // this file might be covered by the GNU General Public License.
36 //
37 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38 // at http://sources.redhat.com/ecos/ecos-license/
39 // -------------------------------------------
40 //####ECOSGPLCOPYRIGHTEND####
41 //####BSDCOPYRIGHTBEGIN####
42 //
43 // -------------------------------------------
44 //
45 // Portions of this software may have been derived from OpenBSD or
46 // other sources, and are covered by the appropriate copyright
47 // disclaimers included herein.
48 //
49 // -------------------------------------------
50 //
51 //####BSDCOPYRIGHTEND####
52 //==========================================================================
53 //#####DESCRIPTIONBEGIN####
54 //
55 // Author(s):    hmt, gthomas
56 // Contributors: Ron Spence, Pacific Softworks, jskov
57 // Date:         2000-02-01
58 // Purpose:      
59 // Description:  hardware driver for 82559 Intel PRO/100+ ethernet
60 // Notes:        CU commands such as dump and config should, according
61 //               to the docs, set the CU active state while executing.
62 //               That does not seem to be the case though, and the
63 //               driver polls the completion bit in the packet status
64 //               word instead.
65 //
66 //               Platform code may provide this vector:
67 //               CYGNUM_DEVS_ETH_INTEL_I82559_SEPARATE_MUX_INTERRUPT if it
68 //               requires the interrupts to be handled via demuxers
69 //               attached to a distinct interrupt.
70 //
71 //               Platform code may alternatively define:
72 //               CYGHWR_DEVS_ETH_INTEL_I82559_DEMUX_ALL if it is necessary
73 //               to demux all interrupt sources - for example if they are
74 //               wire-or'd together on some hardware but distinct on
75 //               others.  In this circumstance it is permitted for
76 //               cyg_pci_translate_interrupt [HAL_PCI_TRANSLATE_INTERRUPT]
77 //               to return invalid for 2nd and subsequent devices.
78 //
79 //               Platform code can also define these three:
80 //               CYGPRI_DEVS_ETH_INTEL_I82559_MASK_INTERRUPTS(p_i82559,old)
81 //               CYGPRI_DEVS_ETH_INTEL_I82559_UNMASK_INTERRUPTS(p_i82559,old)
82 //               CYGPRI_DEVS_ETH_INTEL_I82559_ACK_INTERRUPTS(p_i82559)
83 //               which are particularly useful when nested interrupt
84 //               management is needed (which is always IMHO).
85 //
86 //               Platform code can define this:
87 //               CYGHWR_DEVS_ETH_INTEL_I82559_MISSED_INTERRUPT(p_i82559)
88 //               to detect a dropped interrupt and loop again or
89 //               direct-call the DSR to reschedule the delivery routine.
90 //               Only a problem on edge-triggered interrupt systems.
91 //
92 //               Platform code can also provide this macro:
93 //               CYGPRI_DEVS_ETH_INTEL_I82559_INTERRUPT_ACK_LOOP(p_i82559)
94 //               to handle delaying for acks to register on the interrupt
95 //               controller as necessary on the EBSA.
96 //
97 //               Platform can define CYGHWR_DEVS_ETH_INTEL_I82559_GET_ESA()
98 //               as an external means to get ESAs, possibly from RedBoot
99 //               configuration info that's stored in flash memory.
100 //
101 //               Platform def CYGHWR_DEVS_ETH_INTEL_I82559_HAS_NO_EEPROM
102 //               removes all code for dealing with the EEPROM for those
103 //               targets where there is none fitted.  Either an external
104 //               means to get ESAs should be used, or we must rely on
105 //               hard-wiring the ESA's into each executable by means of the
106 //               usual CDL configuration.
107 //
108 //               Platform def CYGHWR_DEVS_ETH_INTEL_I82559_HAS_ONE_EEPROM
109 //               is for hardware with multiple devices, but only one with a
110 //               serial EEPROM installed.  The 2nd device would get either
111 //               the same ESA - because they are certain to be on different
112 //               segment and internets - or the same ESA incremented by
113 //               CYGHWR_DEVS_ETH_INTEL_I82559_HAS_ONE_EEPROM_MAC_ADJUST.
114 //               CYGHWR_DEVS_ETH_INTEL_I82559_HAS_ONE_EEPROM should be the
115 //               number (0 or 1) of the device that does have the EEPROM.
116 //
117 //               CYGHWR_DEVS_ETH_INTEL_I82559_PCIMEM_DISCONTIGUOUS enables
118 //               checking code for breaks in the physical address of PCI
119 //               window memory.  This can happen on some boards where a
120 //               smaller SDRAM is fitted than the hardware allows, so some
121 //               higher-order address bits are ignored.  We make SDRAM
122 //               contiguous in mapped memory, but what the i82559 sees
123 //               might be discontiguous.  The checking code skips any
124 //               allocated chunk who appears to contain such a break, and
125 //               tries again.
126 //
127 //               CYGHWR_DEVS_ETH_INTEL_I82559_RESET_TIMEOUT( int32 )
128 //               CYGHWR_DEVS_ETH_INTEL_I82559_TIMEOUT_FIRED( int32 ) if
129 //               both defined give the driver a means to detect that we
130 //               have been fixated on the same transmit operation for too
131 //               long - we missed an interrupt or the device crashed.  The
132 //               int32 argument is used to hold a eg. the value of a
133 //               fast-running hardware timer.
134 //
135 //               CYGHWR_DEVS_ETH_INTEL_I82559_ENDIAN_NEUTRAL_IO if PCI IO
136 //               access is not affected by CPU endianess.
137 //
138 //        FIXME: replace -1/-2 return values with proper E-defines
139 //        FIXME: For 82557/8 compatibility i82559_configure() function
140 //               probably needs some tweaking - config bits differ
141 //               slightly but crucially.
142 //        FIXME: EEPROM code not tested on a BE system.
143 //
144 //####DESCRIPTIONEND####
145 //
146 //==========================================================================
147
148 #include <pkgconf/system.h>
149 #ifdef CYGPKG_IO_ETH_DRIVERS
150 #include <pkgconf/io_eth_drivers.h>
151 #endif
152 #include <pkgconf/devs_eth_intel_i82559.h>
153
154 #include <cyg/infra/cyg_type.h>
155 #include <cyg/infra/cyg_ass.h>
156 #include <cyg/hal/hal_arch.h>
157 #include <cyg/hal/hal_intr.h>
158 #include <cyg/infra/diag.h>
159 #include <cyg/hal/hal_if.h>
160 #include <cyg/hal/drv_api.h>
161 #include <cyg/io/eth/netdev.h>
162 #include <cyg/io/eth/eth_drv.h>
163
164 #include <string.h>
165
166 #ifdef CYGPKG_NET
167 #include <pkgconf/net.h>
168 #include <net/if.h>  /* Needed for struct ifnet */
169 #endif
170
171 #ifdef CYGPKG_IO_PCI
172 #include <cyg/io/pci.h>
173 // So we can check the validity of the PCI window against the MLTs opinion,
174 // and thereby what the malloc heap consumes willy-nilly:
175 #include CYGHWR_MEMORY_LAYOUT_H
176 #else
177 #error "Need PCI package here"
178 #endif
179
180 // Exported statistics and the like
181 #include <cyg/devs/eth/i82559_info.h>
182 #include <cyg/io/eth/eth_drv_stats.h>
183 #include CYGDAT_DEVS_ETH_INTEL_I82559_INL
184
185 #include <cyg/hal/hal_if.h>
186
187 // Use with care!  Local variable defined!
188 #define START_CONSOLE()                                                                 \
189 {   /* NEW BLOCK */                                                                     \
190     int _cur_console =                                                                  \
191         CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);      \
192     {                                                                                   \
193         int i;                                                                          \
194         if ( CYGACC_CALL_IF_FLASH_CFG_OP( CYGNUM_CALL_IF_FLASH_CFG_GET,                 \
195                                           "info_console_force", &i,                     \
196                                           CYGNUM_FLASH_CFG_TYPE_CONFIG_BOOL ) ) {       \
197             if ( i ) {                                                                  \
198                 if ( CYGACC_CALL_IF_FLASH_CFG_OP( CYGNUM_CALL_IF_FLASH_CFG_GET,         \
199                                                   "info_console_number", &i,            \
200                                                   CYGNUM_FLASH_CFG_TYPE_CONFIG_INT ) ){ \
201                     /* Then i is the console to force it to: */                         \
202                     CYGACC_CALL_IF_SET_CONSOLE_COMM( i );                               \
203                 }                                                                       \
204             }                                                                           \
205         }                                                                               \
206     }
207
208 #define END_CONSOLE()                                   \
209     CYGACC_CALL_IF_SET_CONSOLE_COMM(_cur_console);      \
210 }   /* END BLOCK */
211
212 void CheckRxRing(struct i82559* p_i82559, const char * func, int line);
213
214 // ------------------------------------------------------------------------
215 // Check on the environment.
216 // 
217 // These are not CDL type config points; they are set up for your platform
218 // in the platform driver's include file and that's that.  These messages
219 // are for the eCos driver writer, not config tool users.
220
221 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_DEMUX_ALL
222 #ifdef CYGNUM_DEVS_ETH_INTEL_I82559_SEPARATE_MUX_INTERRUPT
223 #error Both a separate demux interrupt *and* DEMUX_ALL are defined
224 #endif
225 #endif
226
227 #ifdef CYGPKG_DEVS_ETH_INTEL_I82559_WRITE_EEPROM
228 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_HAS_NO_EEPROM
229 #error This platform has no EEPROM, yet WRITE_EEPROM is enabled
230 #endif
231 #endif
232
233 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_HAS_NO_EEPROM
234 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_HAS_ONE_EEPROM
235 #error This platform has no EEPROM, yet it also HAS_ONE_EEPROM
236 #endif
237 #endif
238
239 // ------------------------------------------------------------------------
240
241 #ifdef CYGDBG_DEVS_ETH_INTEL_I82559_CHATTER
242 #define DEBUG          // Startup printing mainly
243 #define DEBUG_EE       // Some EEPROM specific retries &c
244 #if (CYGDBG_DEVS_ETH_INTEL_I82559_CHATTER > 1)
245 #define DEBUG_82559 // This one prints stuff as packets come and go
246 #endif
247 #endif
248
249 #ifdef CYGDBG_USE_ASSERTS
250 static struct {
251     int can_send;
252     int deliver;
253     int stats;
254     int waitcmd_timeouts;
255     int waitcmd_timeouts_cu;
256     int lockup_timeouts;
257     int bad_cu_idles;
258 } missed_interrupt = { 0,0,0, 0,0, 0, 0 };
259 #endif
260
261 int 
262 console_printf(const char *fmt, ...)
263 {
264     va_list ap;
265     int ret;
266     
267     START_CONSOLE();
268
269     va_start(ap, fmt);
270     ret = diag_vprintf(fmt, ap);
271     va_end(ap);
272     
273     END_CONSOLE();
274     return (ret);
275 }
276
277 #define os_printf console_printf
278 #define db_printf console_printf
279
280 // ------------------------------------------------------------------------
281 //
282 //                             MEMORY ADDRESSING
283 // 
284 // There is scope for confusion here; we deal with LE/BE systems and
285 // addressing issues in two separate ways depending on the type of access
286 // in question.
287 //
288 // 1) IO-style access to the device regsiters over the PCI bus
289 // 2) Memory access to build and read the structures in shared memory
290 // 
291 // In detail:
292 // 
293 // 1) IO-style access to the device regsiters over the PCI bus
294 // 
295 // All such access is via macros which perform byte-swapping as necessary
296 // for the endianness of the CPU.  These macros are called INL, INW, INB
297 // and OUTL, OUTW, OUTB - for Long (32) Word (16) and Byte (8).  Intel
298 // nomenclature seems to have crept in here for shorts.
299 // 
300 // Consequently, all the constants only have to be expressed once, in their
301 // true LE format - bit 15 is 0x8000, bit 0 is 1.
302 // 
303 // All the offsets are also only expressed once.  This is OK so long as GIB
304 // endian addressing (sic, see below) is not employed - or so long as is
305 // does not affect the PCI bus accesses.
306 //
307 // 
308 // 2) Memory access to build and read the structures in shared memory
309 // 
310 // These accesses are by means of peek and poke to an address created from
311 // a base + offset.  No swapping occurs within the access so all constants
312 // and flags need to be defined twice, once for BE and once for LE
313 // accesses.  Worse, for BE, constants need to be defined in flavours for
314 // 16-bit versus 32-bit accesses, ie. 0x8000 sets bit 7 only in BE; for a
315 // 32-bit access you must instead write 0x80000000 to set bit 7.
316 //
317 // Thus all constants are defined twice depending on the CPU's endianness.
318 //
319 // For most BE/LE machines, this is sufficient; the layout of memory is the
320 // same.  Specifically, within a 32-bit word, byte[0] will be data[0:7],
321 // short[0] will be data [0:15] and so on.  &byte[0] == &short[0] == &word
322 // regardless.  But data[0:7] *OF THE MEMORY SYSTEM* will hold either the
323 // LSbyte (0xFF) on a LE machine, and the MSbyte (0xFF000000) on a BE
324 // machine, for a 32-bit access.
325 // 
326 // It is in terms of the memory system that the i82559 defines its view of
327 // the world.
328 // 
329 // Therefore the structure layouts remain the same for both BE and LE
330 // machines.  This means that the offsets for, specifically, the status
331 // word in command blocks is always zero, and the offset for the command
332 // word is always two.
333 //
334 // But there is one furter variant: so-called GIB endian.  (BIG endian
335 // backwards) Some architectures support BE only for software
336 // compatibility; they allow code to run which relies on overlaid C
337 // structures behaving in a certain way; specifically
338 //     *(char *)&i == (i >> 24)
339 // ARM's BE mode is an example of this.
340 // 
341 // But while such an operation would use data[0:7] for the char access in a
342 // true BE or any LE system, in a GE system, data[24:31] are used here.
343 // The implementation is that for memory accesses, A0 and A1 are inverted
344 // before going outside to the memory system.  So if &i == 0x1000,
345 // accessing i uses address 0x1000, A0 and A1 being ignored for a 32-bit
346 // access.  But the 8-bit access to *(char *)&i also uses 0x1000 for the
347 // address as the code sees it, the memory system sees a byte request for
348 // address 0x1003, thus picking up the MSbyte, from data[24:31].
349 //
350 // For such addressing, offsets need to be redefined to swap bytes and
351 // shorts within words.  Therefore offsets are defined twice, once for
352 // "normal" addressing, and once for "GIB endian" addressing.
353 //
354 // FIXME: this BE/GE configuration probably won't work with an ARM in its
355 // BE mode - because that will define the global BE flags, yet it's not
356 // true BE, it's GE.
357 // Perhaps a solution whereby the GE flag CYG_ADDRESSING_IS_GIBENDIAN
358 // overrides the BYTEORDER choices would be good; we want the constants to
359 // be LE, but address offsets to be swapped per GE.
360 //
361 // Essay ends.
362 //
363 // ------------------------------------------------------------------------
364 // I/O access macros as inlines for type safety
365
366 #if (CYG_BYTEORDER == CYG_MSBFIRST)
367
368 #define HAL_CTOLE32(x)  ((((x) & 0xff) << 24) | (((x) & 0xff00) << 8) | (((x) & 0xff0000) >> 8) | (((x) >> 24) & 0xff))
369 #define HAL_LE32TOC(x)  ((((x) & 0xff) << 24) | (((x) & 0xff00) << 8) | (((x) & 0xff0000) >> 8) | (((x) >> 24) & 0xff))
370
371 #define HAL_CTOLE16(x)  ((((x) & 0xff) << 8) | (((x) & 0xff00) >> 8))
372 #define HAL_LE16TOC(x)  ((((x) & 0xff) << 8) | (((x) & 0xff00) >> 8))
373
374 #else
375 // Maintaining the same styleee as above...
376 #define HAL_CTOLE32(x)  ((((x))))
377 #define HAL_LE32TOC(x)  ((((x))))
378
379 #define HAL_CTOLE16(x)  ((((x))))
380 #define HAL_LE16TOC(x)  ((((x))))
381
382 #endif
383
384
385 #if (CYG_BYTEORDER == CYG_MSBFIRST) && !defined(CYGHWR_DEVS_ETH_INTEL_I82559_ENDIAN_NEUTRAL_IO)
386
387 static inline void OUTB(cyg_uint8 value, cyg_uint32 io_address)
388 {
389     HAL_WRITE_UINT8( io_address, value);
390 }
391
392 static inline void OUTW(cyg_uint16 value, cyg_uint32 io_address)
393 {
394     HAL_WRITE_UINT16( io_address, (((value & 0xff) << 8) | ((value & 0xff00) >> 8)) );
395 }
396
397 static inline void OUTL(cyg_uint32 value, cyg_uint32 io_address)
398 {
399     HAL_WRITE_UINT32( io_address,
400                       ((((value) & 0xff) << 24) | (((value) & 0xff00) << 8) | (((value) & 0xff0000) >> 8) | (((value) >> 24) & 0xff)) );
401 }
402
403 static inline cyg_uint8 INB(cyg_uint32 io_address)
404 {   
405     cyg_uint8 d;
406     HAL_READ_UINT8( io_address, d );
407     return d;
408 }
409
410 static inline cyg_uint16 INW(cyg_uint32 io_address)
411 {
412     cyg_uint16 d;
413     HAL_READ_UINT16( io_address, d );
414     return (((d & 0xff) << 8) | ((d & 0xff00) >> 8));
415 }
416
417 static inline cyg_uint32 INL(cyg_uint32 io_address)
418 {
419     cyg_uint32 d;
420     HAL_READ_UINT32( io_address, d );
421     return ((((d) & 0xff) << 24) | (((d) & 0xff00) << 8) | (((d) & 0xff0000) >> 8) | (((d) >> 24) & 0xff));
422 }
423 #else
424
425 static inline void OUTB(cyg_uint8  value, cyg_uint32 io_address)
426 {   HAL_WRITE_UINT8( io_address, value );   }
427
428 static inline void OUTW(cyg_uint16 value, cyg_uint32 io_address)
429 {   HAL_WRITE_UINT16( io_address, value );   }
430
431 static inline void OUTL(cyg_uint32 value, cyg_uint32 io_address)
432 {   HAL_WRITE_UINT32( io_address, value );   }
433
434 static inline cyg_uint8  INB(cyg_uint32 io_address)
435  {   cyg_uint8  _t_; HAL_READ_UINT8(  io_address, _t_ ); return _t_;   }
436
437 static inline cyg_uint16 INW(cyg_uint32 io_address)
438  {   cyg_uint16 _t_; HAL_READ_UINT16( io_address, _t_ ); return _t_;   }
439
440 static inline cyg_uint32 INL(cyg_uint32 io_address)
441  {   cyg_uint32 _t_; HAL_READ_UINT32( io_address, _t_ ); return _t_;   }
442
443 #endif // byteorder
444
445 // ------------------------------------------------------------------------
446 // Macros for writing shared memory structures - no need for byte flipping
447
448 #define READMEM8(   _reg_, _val_ ) ((_val_) = *((volatile CYG_BYTE *)(_reg_)))
449 #define WRITEMEM8(  _reg_, _val_ ) (*((volatile CYG_BYTE *)(_reg_)) = (_val_))
450 #define READMEM16(  _reg_, _val_ ) ((_val_) = *((volatile CYG_WORD16 *)(_reg_)))
451 #define WRITEMEM16( _reg_, _val_ ) (*((volatile CYG_WORD16 *)(_reg_)) = (_val_))
452 #define READMEM32(  _reg_, _val_ ) ((_val_) = *((volatile CYG_WORD32 *)(_reg_)))
453 #define WRITEMEM32( _reg_, _val_ ) (*((volatile CYG_WORD32 *)(_reg_)) = (_val_))
454
455 // ------------------------------------------------------------------------
456 // Map from CPU-view addresses to PCI-bus master's view - however that is:
457
458 #ifdef CYGHWR_INTEL_I82559_PCI_VIRT_TO_BUS
459
460 #define VIRT_TO_BUS( _x_ ) CYGHWR_INTEL_I82559_PCI_VIRT_TO_BUS( _x_ )
461
462 #else // use default mappings: get a physical address to give to the device
463
464 #define VIRT_TO_BUS( _x_ ) virt_to_bus((cyg_uint32)(_x_))
465 static inline cyg_uint32 virt_to_bus(cyg_uint32 p_memory)
466 { return CYGARC_PHYSICAL_ADDRESS(p_memory);    }
467
468 #endif // not defined CYGHWR_INTEL_I82559_PCI_VIRT_TO_BUS
469
470 // ------------------------------------------------------------------------
471 //                                                                      
472 //                   82559 REGISTER OFFSETS (I/O SPACE)                 
473 //                                                                      
474 // ------------------------------------------------------------------------
475 #define SCBStatus       0               // Rx/Command Unit Status *Word*
476 #define SCBIntAckByte   1               // Rx/Command Unit STAT/ACK byte
477 #define SCBCmd          2               // Rx/Command Unit Command *Word*
478 #define SCBIntrCtlByte  3               // Rx/Command Unit Intr.Control Byte
479 #define SCBPointer      4               // General purpose pointer.
480 #define SCBPort         8               // Misc. commands and operands.
481 #define SCBflash        12              // Flash memory control.
482 #define SCBeeprom       14              // EEPROM memory control.
483 #define SCBCtrlMDI      16              // MDI interface control.
484 #define SCBEarlyRx      20              // Early receive byte count.
485 #define SCBGenControl   28              // 82559 General Control Register
486 #define SCBGenStatus    29              // 82559 General Status register
487
488
489 // ------------------------------------------------------------------------
490 //
491 //               82559 SCB STATUS WORD DEFNITIONS
492 //
493 // ------------------------------------------------------------------------
494 #define SCB_STATUS_CX   0x8000          // CU finished command (transmit)
495 #define SCB_STATUS_FR   0x4000          // frame received
496 #define SCB_STATUS_CNA  0x2000          // CU left active state
497 #define SCB_STATUS_RNR  0x1000          // receiver left ready state
498 #define SCB_STATUS_MDI  0x0800          // MDI read/write cycle done
499 #define SCB_STATUS_SWI  0x0400          // software generated interrupt
500 #define SCB_STATUS_FCP  0x0100          // flow control pause interrupt
501
502 #define SCB_INTACK_MASK 0xFD00          // all the above
503 #define SCB_INTACK_MASK_BYTE 0xFD       // all the above
504
505 #define SCB_INTACK_TX (SCB_STATUS_CX | SCB_STATUS_CNA)
506 #define SCB_INTACK_RX (SCB_STATUS_FR | SCB_STATUS_RNR)
507
508 // ------------------------------------------------------------------------
509 //
510 //               SYSTEM CONTROL BLOCK COMMANDS
511 //
512 // ------------------------------------------------------------------------
513 // CU COMMANDS
514 #define CU_NOP          0x0000
515 #define CU_START        0x0010
516 #define CU_RESUME       0x0020
517 #define CU_STATSADDR    0x0040          // Load Dump Statistics ctrs addr
518 #define CU_SHOWSTATS    0x0050          // Dump statistics counters.
519 #define CU_ADDR_LOAD    0x0060          // Base address to add to CU commands
520 #define CU_DUMPSTATS    0x0070          // Dump then reset stats counters.
521
522 // RUC COMMANDS
523 #define RUC_NOP         0x0000
524 #define RUC_START       0x0001
525 #define RUC_RESUME      0x0002
526 #define RUC_ABORT       0x0004
527 #define RUC_ADDR_LOAD   0x0006          // (seems not to clear on acceptance)
528 #define RUC_RESUMENR    0x0007
529
530 #define CU_CMD_MASK     0x00f0
531 #define RU_CMD_MASK     0x0007
532
533 #define SCB_M           0x0100          // 0 = enable interrupt, 1 = disable
534 #define SCB_SWI         0x0200          // 1 - cause device to interrupt
535 #define SCB_BYTE_M        0x01          // 0 = enable interrupt, 1 = disable
536 #define SCB_BYTE_SWI      0x02          // 1 - cause device to interrupt
537
538 #define CU_STATUS_MASK  0x00C0
539 #define RU_STATUS_MASK  0x003C
540
541 #define RU_STATUS_IDLE  (0<<2)
542 #define RU_STATUS_SUS   (1<<2)
543 #define RU_STATUS_NORES (2<<2)
544 #define RU_STATUS_READY (4<<2)
545 #define RU_STATUS_NO_RBDS_SUS   ((1<<2)|(8<<2))
546 #define RU_STATUS_NO_RBDS_NORES ((2<<2)|(8<<2))
547 #define RU_STATUS_NO_RBDS_READY ((4<<2)|(8<<2))
548
549 #define MAX_MEM_RESERVED_IOCTL 1000
550
551 // ------------------------------------------------------------------------
552 //
553 //               82559 PORT INTERFACE COMMANDS
554 //
555 // ------------------------------------------------------------------------
556 #define I82559_RESET            0x00000000 // software reset
557 #define I82559_SELFTEST         0x00000001 // 82559 selftest command
558 #define I82559_SELECTIVE_RESET  0x00000002
559 #define I82559_DUMP             0x00000003
560 #define I82559_DUMP_WAKEUP      0x00000007
561
562
563 // ------------------------------------------------------------------------
564 //
565 //                   82559 EEPROM INTERFACE
566 //
567 // ------------------------------------------------------------------------
568 //  EEPROM_Ctrl bits.
569 #define EE_SHIFT_CLK    0x01            // EEPROM shift clock.
570 #define EE_CS           0x02            // EEPROM chip select.
571 #define EE_DATA_WRITE   0x04            // EEPROM chip data in.
572 #define EE_DATA_READ    0x08            // EEPROM chip data out.
573 #define EE_ENB          (0x4800 | EE_CS)
574
575 // Delay between EEPROM clock transitions.
576 #define eeprom_delay(usec)              udelay(usec);
577
578 // The EEPROM commands include the always-set leading bit.
579 #define EE_WRITE_CMD(a)     (5 << (a))
580 #define EE_READ_CMD(a)      (6 << (a))
581 #define EE_ERASE_CMD(a)     (7 << (a))
582 #define EE_WRITE_EN_CMD(a)  (19 << ((a)-2))
583 #define EE_WRITE_DIS_CMD(a) (16 << ((a)-2))
584 #define EE_ERASE_ALL_CMD(a) (18 << ((a)-2))
585
586 #define EE_TOP_CMD_BIT(a)      ((a)+2) // Counts down to zero
587 #define EE_TOP_DATA_BIT        (15)    // Counts down to zero
588
589 #define EEPROM_ENABLE_DELAY (10) // Delay at chip select
590
591 #define EEPROM_SK_DELAY  (4) // Delay between clock edges *and* data
592                              // read or transition; 3 of these per bit.
593 #define EEPROM_DONE_DELAY (100) // Delay when all done
594
595
596 // ------------------------------------------------------------------------
597 //
598 //               RECEIVE FRAME DESCRIPTORS
599 //
600 // ------------------------------------------------------------------------
601
602 // The status is split into two shorts to get atomic access to the EL bit;
603 // the upper word is not written by the device, so we can just hit it,
604 // leaving the lower word (which the device updates) alone.  Otherwise
605 // there's a race condition between software moving the end-of-list (EL)
606 // bit round and the device writing into the previous slot.
607
608 #if (CYG_BYTEORDER == CYG_MSBFIRST)
609
610 // Note that status bits are endian converted - so we don't need to
611 // fiddle the byteorder when accessing the status word!
612 #define RFD_STATUS_EL   0x00000080      // 1=last RFD in RFA
613 #define RFD_STATUS_S    0x00000040      // 1=suspend RU after receiving frame
614 #define RFD_STATUS_H    0x00001000      // 1=RFD is a header RFD
615 #define RFD_STATUS_SF   0x00000800      // 0=simplified, 1=flexible mode
616 #define RFD_STATUS_C    0x00800000      // completion of received frame
617 #define RFD_STATUS_OK   0x00200000      // frame received with no errors
618
619 #define RFD_STATUS_HI_EL   0x0080       // 1=last RFD in RFA
620 #define RFD_STATUS_HI_S    0x0040       // 1=suspend RU after receiving frame
621 #define RFD_STATUS_HI_H    0x1000       // 1=RFD is a header RFD
622 #define RFD_STATUS_HI_SF   0x0800       // 0=simplified, 1=flexible mode
623
624 #define RFD_STATUS_LO_C    0x0080       // completion of received frame
625 #define RFD_STATUS_LO_OK   0x0020       // frame received with no errors
626
627
628 #define RFD_COUNT_MASK     0x3fff
629 #define RFD_COUNT_F        0x4000
630 #define RFD_COUNT_EOF      0x8000
631
632 #define RFD_RX_CRC          0x00080000  // crc error
633 #define RFD_RX_ALIGNMENT    0x00040000  // alignment error
634 #define RFD_RX_RESOURCE     0x00020000  // out of space, no resources
635 #define RFD_RX_DMA_OVER     0x00010000  // DMA overrun
636 #define RFD_RX_SHORT        0x80000000  // short frame error
637 #define RFD_RX_LENGTH       0x20000000  //
638 #define RFD_RX_ERROR        0x10000000  // receive error
639 #define RFD_RX_NO_ADR_MATCH 0x04000000  // no address match
640 #define RFD_RX_IA_MATCH     0x02000000  // individual address does not match
641 #define RFD_RX_TCO          0x01000000  // TCO indication
642
643 #else // Little-endian
644
645 #define RFD_STATUS_EL   0x80000000      // 1=last RFD in RFA
646 #define RFD_STATUS_S    0x40000000      // 1=suspend RU after receiving frame
647 #define RFD_STATUS_H    0x00100000      // 1=RFD is a header RFD
648 #define RFD_STATUS_SF   0x00080000      // 0=simplified, 1=flexible mode
649 #define RFD_STATUS_C    0x00008000      // completion of received frame
650 #define RFD_STATUS_OK   0x00002000      // frame received with no errors
651
652 #define RFD_STATUS_HI_EL   0x8000       // 1=last RFD in RFA
653 #define RFD_STATUS_HI_S    0x4000       // 1=suspend RU after receiving frame
654 #define RFD_STATUS_HI_H    0x0010       // 1=RFD is a header RFD
655 #define RFD_STATUS_HI_SF   0x0008       // 0=simplified, 1=flexible mode
656
657 #define RFD_STATUS_LO_C    0x8000       // completion of received frame
658 #define RFD_STATUS_LO_OK   0x2000       // frame received with no errors
659
660 #define RFD_COUNT_MASK     0x3fff
661 #define RFD_COUNT_F        0x4000
662 #define RFD_COUNT_EOF      0x8000
663
664 #define RFD_RX_CRC          0x00000800  // crc error
665 #define RFD_RX_ALIGNMENT    0x00000400  // alignment error
666 #define RFD_RX_RESOURCE     0x00000200  // out of space, no resources
667 #define RFD_RX_DMA_OVER     0x00000100  // DMA overrun
668 #define RFD_RX_SHORT        0x00000080  // short frame error
669 #define RFD_RX_LENGTH       0x00000020  //
670 #define RFD_RX_ERROR        0x00000010  // receive error
671 #define RFD_RX_NO_ADR_MATCH 0x00000004  // no address match
672 #define RFD_RX_IA_MATCH     0x00000002  // individual address does not match
673 #define RFD_RX_TCO          0x00000001  // TCO indication
674
675 #endif // CYG_BYTEORDER
676
677 #ifndef CYG_ADDRESSING_IS_GIBENDIAN
678
679 // Normal addressing
680 #define RFD_STATUS       0
681 #define RFD_STATUS_LO    0
682 #define RFD_STATUS_HI    2
683 #define RFD_LINK         4
684 #define RFD_RDB_ADDR     8
685 #define RFD_COUNT       12
686 #define RFD_SIZE        14
687 #define RFD_BUFFER      16
688 #define RFD_SIZEOF      16
689
690 #else // CYG_ADDRESSING_IS_GIBENDIAN
691
692 // GIBENDIAN addressing; A0 and A1 are flipped:
693 #define RFD_STATUS       0
694 #define RFD_STATUS_LO    2              // swapped
695 #define RFD_STATUS_HI    0              // swapped
696 #define RFD_LINK         4
697 #define RFD_RDB_ADDR     8
698 #define RFD_COUNT       14              // swapped
699 #define RFD_SIZE        12              // swapped
700 #define RFD_BUFFER      16
701 #define RFD_SIZEOF      16
702
703 #endif // CYG_ADDRESSING_IS_GIBENDIAN
704
705
706
707 // ------------------------------------------------------------------------
708 //
709 //               TRANSMIT FRAME DESCRIPTORS
710 //
711 // ------------------------------------------------------------------------
712
713 #if (CYG_BYTEORDER == CYG_MSBFIRST)
714
715 // Note that CMD/STATUS bits are endian converted - so we don't need
716 // to fiddle the byteorder when accessing the CMD/STATUS word!
717
718 #define TxCB_CMD_TRANSMIT   0x0400      // transmit command
719 #define TxCB_CMD_SF         0x0800      // 0=simplified, 1=flexible mode
720 #define TxCB_CMD_NC         0x0010      // 0=CRC insert by controller
721 #define TxCB_CMD_I          0x0020      // generate interrupt on completion
722 #define TxCB_CMD_S          0x0040      // suspend on completion
723 #define TxCB_CMD_EL         0x0080      // last command block in CBL
724
725 #define TxCB_COUNT_MASK     0x3fff
726 #define TxCB_COUNT_EOF      0x8000
727
728 #else // Little-endian layout
729
730 #define TxCB_COUNT_MASK     0x3fff
731 #define TxCB_COUNT_EOF      0x8000
732
733 #define TxCB_CMD_TRANSMIT   0x0004      // transmit command
734 #define TxCB_CMD_SF         0x0008      // 0=simplified, 1=flexible mode
735 #define TxCB_CMD_NC         0x0010      // 0=CRC insert by controller
736 #define TxCB_CMD_I          0x2000      // generate interrupt on completion
737 #define TxCB_CMD_S          0x4000      // suspend on completion
738 #define TxCB_CMD_EL         0x8000      // last command block in CBL
739
740 #endif // CYG_BYTEORDER
741
742 #ifndef CYG_ADDRESSING_IS_GIBENDIAN
743
744 // Normal addressing
745 #define TxCB_STATUS          0
746 #define TxCB_CMD             2
747 #define TxCB_LINK            4
748 #define TxCB_TBD_ADDR        8
749 #define TxCB_COUNT          12
750 #define TxCB_TX_THRESHOLD   14
751 #define TxCB_TBD_NUMBER     15
752 #define TxCB_BUFFER         16
753 #define TxCB_SIZEOF         16
754
755 #else // CYG_ADDRESSING_IS_GIBENDIAN
756
757 // GIBENDIAN addressing; A0 and A1 are flipped:
758 #define TxCB_STATUS          2          // swapped
759 #define TxCB_CMD             0          // swapped
760 #define TxCB_LINK            4
761 #define TxCB_TBD_ADDR        8
762 #define TxCB_COUNT          14          // swapped
763 #define TxCB_TX_THRESHOLD   13          // swapped
764 #define TxCB_TBD_NUMBER     12          // swapped
765 #define TxCB_BUFFER         16
766 #define TxCB_SIZEOF         16
767 #endif // CYG_ADDRESSING_IS_GIBENDIAN
768
769 // ------------------------------------------------------------------------
770 //
771 //                   STRUCTURES ADDED FOR PROMISCUOUS MODE
772 //
773 // ------------------------------------------------------------------------
774
775 #if (CYG_BYTEORDER == CYG_MSBFIRST)
776
777 // Note CFG CMD and STATUS swapped, so no need for endian conversion
778 // in code.
779 #define CFG_CMD_EL         0x0080
780 #define CFG_CMD_SUSPEND    0x0040
781 #define CFG_CMD_INT        0x0020
782 #define CFG_CMD_IAS        0x0100       // individual address setup
783 #define CFG_CMD_CONFIGURE  0x0200       // configure
784 #define CFG_CMD_MULTICAST  0x0300       // Multicast-Setup
785
786 #define CFG_STATUS_C       0x0080
787 #define CFG_STATUS_OK      0x0020
788
789 #else // Little-endian 
790
791 #define CFG_CMD_EL         0x8000
792 #define CFG_CMD_SUSPEND    0x4000
793 #define CFG_CMD_INT        0x2000
794 #define CFG_CMD_IAS        0x0001       // individual address setup
795 #define CFG_CMD_CONFIGURE  0x0002       // configure
796 #define CFG_CMD_MULTICAST  0x0003       // Multicast-Setup
797
798 #define CFG_STATUS_C       0x8000
799 #define CFG_STATUS_OK      0x2000
800
801 #endif  // CYG_BYTEORDER
802
803 #ifndef CYG_ADDRESSING_IS_GIBENDIAN
804
805 // Normal addressing
806 #define CFG_STATUS          0
807 #define CFG_CMD             2
808 #define CFG_CB_LINK_OFFSET  4
809 #define CFG_BYTES           8
810 #define CFG_SIZEOF          32
811
812 #else // CYG_ADDRESSING_IS_GIBENDIAN
813
814 // GIBENDIAN addressing; A0 and A1 are flipped:
815 #define CFG_STATUS          2
816 #define CFG_CMD             0
817 #define CFG_CB_LINK_OFFSET  4
818 #define CFG_BYTES           8
819 #define CFG_SIZEOF          32
820
821 #endif // CYG_ADDRESSING_IS_GIBENDIAN
822
823 // Normal addressing
824 #define CFG_MC_LIST_BYTES 8
825 #define CFG_MC_LIST_DATA 10
826
827 // ------------------------------------------------------------------------
828 //
829 //                       STATISTICAL COUNTER STRUCTURE
830 //
831 // ------------------------------------------------------------------------
832 #ifdef KEEP_STATISTICS
833 STATISTICS statistics[CYGNUM_DEVS_ETH_INTEL_I82559_DEV_COUNT];
834 I82559_COUNTERS i82559_counters[CYGNUM_DEVS_ETH_INTEL_I82559_DEV_COUNT];
835 #endif // KEEP_STATISTICS
836
837 // ------------------------------------------------------------------------
838 //
839 //                      DEVICES AND PACKET QUEUES
840 //
841 // ------------------------------------------------------------------------
842
843 #define MAX_RX_PACKET_SIZE  1536        // maximum Rx packet size
844 #define MAX_TX_PACKET_SIZE  1536        // maximum Tx packet size
845
846
847 // ------------------------------------------------------------------------
848 // Use arrays provided by platform header to verify pointers.
849 #ifdef CYGDBG_USE_ASSERTS
850 #define CHECK_NDP_SC_LINK()                                             \
851     CYG_MACRO_START                                                     \
852     int i, valid_netdev = 0, valid_sc = 0;                              \
853     for(i = 0; i < CYGNUM_DEVS_ETH_INTEL_I82559_DEV_COUNT; i++) {       \
854         if (i82559_netdev_array[i] == ndp) valid_netdev = 1;            \
855         if (i82559_sc_array[i] == sc) valid_sc = 1;                     \
856         if (valid_sc || valid_netdev) break;                            \
857     }                                                                   \
858     CYG_ASSERT( valid_netdev, "Bad ndp" );                              \
859     CYG_ASSERT( valid_sc, "Bad sc" );                                   \
860     CYG_ASSERT( (void *)p_i82559 == i82559_sc_array[i]->driver_private, \
861                 "sc pointer bad" );                                     \
862     CYG_MACRO_END
863 #else
864 #define CHECK_NDP_SC_LINK()
865 #endif
866
867 #define IF_BAD_82559( _p_ )                                             \
868 if (({                                                                  \
869     int i, valid_p = 0;                                                 \
870     for(i = 0; i < CYGNUM_DEVS_ETH_INTEL_I82559_DEV_COUNT; i++) {       \
871         if (i82559_priv_array[i] == (_p_)) {                            \
872             valid_p = 1;                                                \
873             break;                                                      \
874         }                                                               \
875     }                                                                   \
876     CYG_ASSERT(valid_p, "Bad pointer-to-i82559");                       \
877     (!valid_p);                                                         \
878 }))
879
880 // ------------------------------------------------------------------------
881 //
882 // Managing the memory that is windowed onto the PCI bus
883 //
884 // ------------------------------------------------------------------------
885
886 static cyg_uint32 i82559_heap_size;
887 static cyg_uint8 *i82559_heap_base;
888 static cyg_uint8 *i82559_heap_free;
889
890 static void *mem_reserved_ioctl = (void*)0;
891 // uncacheable memory reserved for ioctl calls
892
893 // ------------------------------------------------------------------------
894 //
895 //                       FUNCTION PROTOTYPES
896 //
897 // ------------------------------------------------------------------------
898
899 static int pci_init_find_82559s(void);
900
901 static void i82559_reset(struct i82559* p_i82559);
902 static void i82559_restart(struct i82559 *p_i82559);
903 static int eth_set_mac_address(struct i82559* p_i82559, cyg_uint8 *addr, int eeprom );
904
905 static void InitRxRing(struct i82559* p_i82559);
906 static void ResetRxRing(struct i82559* p_i82559);
907 static void InitTxRing(struct i82559* p_i82559);
908 static void ResetTxRing(struct i82559* p_i82559);
909
910 #if defined(CYGHWR_DEVS_ETH_INTEL_I82559_MISSED_INTERRUPT) || \
911   defined(CYGNUM_DEVS_ETH_INTEL_I82559_SEPARATE_MUX_INTERRUPT) || \
912   !defined(CYGPKG_IO_ETH_DRIVERS_STAND_ALONE)
913 static void
914 eth_dsr(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data);
915 #endif
916 static cyg_uint32
917 eth_isr(cyg_vector_t vector, cyg_addrword_t data);
918
919 static int i82559_configure(struct i82559* p_i82559, int promisc,
920                             int oversized, int multicast_all);
921 #ifdef ETH_DRV_SET_MC_LIST
922 static int i82559_set_multicast(struct i82559* p_i82559,
923                                 int num_addrs,
924                                 cyg_uint8 *address_list );
925 #endif // ETH_DRV_SET_MC_ALL
926 #ifdef CYGPKG_DEVS_ETH_INTEL_I82559_WRITE_EEPROM
927 static void
928 program_eeprom(cyg_uint32 ioaddr, cyg_uint32 eeprom_size, cyg_uint8 *data);
929 static void
930 write_eeprom(long ioaddr, int location, int addr_len, unsigned short value);
931 static int
932 write_enable_eeprom(long ioaddr,  int addr_len);
933 #endif
934
935 // debugging/logging only:
936 void dump_txcb(TxCB* p_txcb);
937 void DisplayStatistics(void);
938 void update_statistics(struct i82559* p_i82559);
939 void dump_rfd(RFD* p_rfd, int anyway );
940 void dump_all_rfds( int intf );
941 void dump_packet(cyg_uint8 *p_buffer, int length);
942
943 static void i82559_stop( struct eth_drv_sc *sc );
944
945 // ------------------------------------------------------------------------
946 // utilities
947 // ------------------------------------------------------------------------
948
949 typedef enum {
950     WAIT_RU,                            // wait before RU cmd
951     WAIT_CU                             // wait before CU cmd
952 } cmd_wait_t;
953
954 static inline
955 void 
956 wait_for_cmd_done(long scb_ioaddr, cmd_wait_t type)
957 {
958     register int status;
959     register int wait;
960
961     wait = 0x100000;
962     do status = INW(scb_ioaddr + SCBCmd) /* nothing */ ;
963     while( (0 != ((CU_CMD_MASK | RU_CMD_MASK) & status)) && --wait >= 0);
964
965     if ( wait <= 0 ) {
966         // Then we timed out; reset the device and try again...
967         OUTL(I82559_SELECTIVE_RESET, scb_ioaddr + SCBPort);
968 #ifdef CYGDBG_USE_ASSERTS
969         missed_interrupt.waitcmd_timeouts++;
970 #endif
971         wait = 0x100000;
972         do status = INW(scb_ioaddr + SCBCmd) /* nothing */;
973         while( (0 != ((CU_CMD_MASK | RU_CMD_MASK) & status)) && --wait >= 0);
974     }
975
976     // special case - don't complain about RUC_ADDR_LOAD as it doesn't clear
977     // on some silicon
978     if ( RUC_ADDR_LOAD != (status & RU_CMD_MASK) )
979         CYG_ASSERT( wait > 0, "wait_for_cmd_done: cmd busy" );
980
981     if (WAIT_CU == type) {
982         // Also check CU is idle
983         wait = 0x100000;
984         do status = INW(scb_ioaddr + SCBStatus) /* nothing */;
985         while( (status & CU_STATUS_MASK) && --wait >= 0);
986         if ( wait <= 0 ) {
987             // Then we timed out; reset the device and try again...
988             OUTL(I82559_SELECTIVE_RESET, scb_ioaddr + SCBPort);
989 #ifdef CYGDBG_USE_ASSERTS
990             missed_interrupt.waitcmd_timeouts_cu++;
991 #endif
992             wait = 0x100000;
993             do status = INW(scb_ioaddr + SCBCmd) /* nothing */;
994             while( (0 != ((CU_CMD_MASK | RU_CMD_MASK) & status)) && --wait >= 0);
995         }
996
997         CYG_ASSERT( wait > 0, "wait_for_cmd_done: CU busy" );
998
999     } else if (WAIT_RU == type) {
1000         // Can't see any active state in the doc to check for 
1001     }
1002 }
1003
1004 // ------------------------------------------------------------------------
1005
1006 static void
1007 udelay(int delay)
1008 {
1009     CYGACC_CALL_IF_DELAY_US(delay);
1010 }
1011
1012 // ------------------------------------------------------------------------
1013 // If we are demuxing for all interrupt sources, we must mask and unmask
1014 // *all* interrupt sources together.
1015
1016 static inline int 
1017 Mask82559Interrupt(struct i82559* p_i82559)
1018 {
1019     int old = 0;
1020
1021 #ifdef CYGPRI_DEVS_ETH_INTEL_I82559_MASK_INTERRUPTS
1022     CYGPRI_DEVS_ETH_INTEL_I82559_MASK_INTERRUPTS(p_i82559,old);
1023 #else
1024 //    if (query_enabled)
1025     old |= 1;
1026     cyg_drv_interrupt_mask(p_i82559->vector);
1027 #ifdef CYGNUM_DEVS_ETH_INTEL_I82559_SEPARATE_MUX_INTERRUPT
1028 //    if (query_enabled)
1029     old |= 2;
1030     cyg_drv_interrupt_mask(CYGNUM_DEVS_ETH_INTEL_I82559_SEPARATE_MUX_INTERRUPT);
1031 #endif
1032 #endif
1033     /* it may prove necessary to do something like this
1034     if ( mybits != (mybits & old) )
1035         /# then at least one of them was disabled, so
1036          # omit both from any restoration: #/
1037         old = 0;
1038     */
1039
1040     return old;
1041 }
1042
1043 static inline void
1044 UnMask82559Interrupt(struct i82559* p_i82559, int old)
1045 {
1046 #ifdef CYGPRI_DEVS_ETH_INTEL_I82559_UNMASK_INTERRUPTS
1047     CYGPRI_DEVS_ETH_INTEL_I82559_UNMASK_INTERRUPTS(p_i82559,old);
1048 #else
1049     // We must only unmask (enable) those which were unmasked before,
1050     // according to the bits in old.
1051     if (old & 1)
1052         cyg_drv_interrupt_unmask(p_i82559->vector);
1053 #ifdef CYGNUM_DEVS_ETH_INTEL_I82559_SEPARATE_MUX_INTERRUPT
1054     if (old & 2)
1055         cyg_drv_interrupt_mask(CYGNUM_DEVS_ETH_INTEL_I82559_SEPARATE_MUX_INTERRUPT);
1056 #endif
1057 #endif
1058 }
1059
1060
1061 static inline void
1062 Acknowledge82559Interrupt(struct i82559* p_i82559)
1063 {
1064 #ifdef CYGPRI_DEVS_ETH_INTEL_I82559_ACK_INTERRUPTS
1065     CYGPRI_DEVS_ETH_INTEL_I82559_ACK_INTERRUPTS(p_i82559);
1066 #else
1067     cyg_drv_interrupt_acknowledge(p_i82559->vector);
1068 #ifdef CYGNUM_DEVS_ETH_INTEL_I82559_SEPARATE_MUX_INTERRUPT
1069     cyg_drv_interrupt_acknowledge(CYGNUM_DEVS_ETH_INTEL_I82559_SEPARATE_MUX_INTERRUPT);
1070 #endif
1071 #endif
1072
1073 #ifdef CYGPRI_DEVS_ETH_INTEL_I82559_INTERRUPT_ACK_LOOP
1074     CYGPRI_DEVS_ETH_INTEL_I82559_INTERRUPT_ACK_LOOP(p_i82559);
1075 #endif
1076 }
1077
1078
1079 static inline void
1080 Check82559TxLockupTimeout(struct i82559* p_i82559)
1081 {
1082 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_RESET_TIMEOUT
1083 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_TIMEOUT_FIRED
1084     int ints = Mask82559Interrupt(p_i82559);
1085     if ( p_i82559->tx_in_progress &&
1086         (p_i82559->tx_descriptor_timeout == p_i82559->tx_descriptor_active) ) {
1087         if ( CYGHWR_DEVS_ETH_INTEL_I82559_TIMEOUT_FIRED( p_i82559->platform_timeout ) ) {
1088             // Then reset the device; the TX unit is locked up.
1089             cyg_uint32 ioaddr = p_i82559->io_address;
1090             // Wait some time afterwards for chip to settle.
1091             OUTL(I82559_SELECTIVE_RESET, ioaddr + SCBPort);
1092 #ifdef CYGDBG_USE_ASSERTS
1093             missed_interrupt.lockup_timeouts++;
1094 #endif
1095             udelay(20);
1096         }
1097     }
1098     else {
1099         // All is well:
1100         CYGHWR_DEVS_ETH_INTEL_I82559_RESET_TIMEOUT( p_i82559->platform_timeout );
1101         p_i82559->tx_descriptor_timeout = p_i82559->tx_descriptor_active;
1102     }
1103     UnMask82559Interrupt(p_i82559,ints);
1104 #endif
1105 #endif
1106 }
1107
1108 // ------------------------------------------------------------------------
1109 // Memory management
1110 //
1111 // Simply carve off from the front of the PCI mapped window into real memory
1112
1113 static void*
1114 pciwindow_mem_alloc(int size)
1115 {
1116     void *p_memory;
1117     int _size = size;
1118
1119     CYG_ASSERT(
1120         (CYGHWR_INTEL_I82559_PCI_MEM_MAP_BASE <= (int)i82559_heap_free)
1121         &&
1122         ((CYGHWR_INTEL_I82559_PCI_MEM_MAP_BASE + 
1123           CYGHWR_INTEL_I82559_PCI_MEM_MAP_SIZE) > (int)i82559_heap_free)
1124         &&
1125         (0 < i82559_heap_size)
1126         &&
1127         (CYGHWR_INTEL_I82559_PCI_MEM_MAP_SIZE >= i82559_heap_size)
1128         &&
1129         (CYGHWR_INTEL_I82559_PCI_MEM_MAP_BASE == (int)i82559_heap_base),
1130         "Heap variables corrupted" );
1131
1132     p_memory = (void *)0;
1133     size = (size + 3) & ~3;
1134     if ( (i82559_heap_free+size) < (i82559_heap_base+i82559_heap_size) ) {
1135         cyg_uint32 *p;
1136         p_memory = (void *)i82559_heap_free;
1137         i82559_heap_free += size;
1138         for ( p = (cyg_uint32 *)p_memory; _size > 0; _size -= 4 )
1139             *p++ = 0;
1140     }
1141
1142 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_PCIMEM_DISCONTIGUOUS
1143     // Then the underlying physical address can have breaks in it.
1144     // We cannot use such a block, clearly, so we just discard and re-do.
1145     if ( p_memory ) {
1146         char *bpm = (char *)VIRT_TO_BUS( p_memory );
1147         char *bmf = (char *)VIRT_TO_BUS( i82559_heap_free );
1148         
1149         if ( bpm + size != bmf ) {
1150             // then we found a break; retry:
1151             if ( (i82559_heap_free+size) < (i82559_heap_base+i82559_heap_size) ) {
1152                 cyg_uint32 *p;
1153                 p_memory = (void *)i82559_heap_free;
1154                 i82559_heap_free += size;
1155                 for ( p = (cyg_uint32 *)p_memory; _size > 0; _size -= 4 )
1156                     *p++ = 0;
1157             }
1158         }
1159     }
1160 #endif
1161     CYG_ASSERT(
1162         NULL == p_memory ||
1163         VIRT_TO_BUS( p_memory ) + size == VIRT_TO_BUS( i82559_heap_free ),
1164         "Discontiguous PCI memory in real addresses" );
1165
1166     return p_memory;
1167 }
1168
1169
1170 // ------------------------------------------------------------------------
1171 //
1172 //                       GET EEPROM SIZE
1173 //
1174 // ------------------------------------------------------------------------
1175 #ifndef CYGHWR_DEVS_ETH_INTEL_I82559_HAS_NO_EEPROM
1176 static int
1177 get_eeprom_size(long ioaddr)
1178 {
1179     unsigned short retval = 0;
1180     int ee_addr = ioaddr + SCBeeprom;
1181     int i, addrbits;
1182
1183     // Should already be not-selected, but anyway:
1184     OUTW(EE_ENB & ~EE_CS, ee_addr);
1185     eeprom_delay(EEPROM_ENABLE_DELAY);
1186     OUTW(EE_ENB, ee_addr);
1187     eeprom_delay(EEPROM_ENABLE_DELAY);
1188     
1189     // Shift the read command bits out.
1190     for (i = 2; i >= 0; i--) {
1191         short dataval = (6 & (1 << i)) ? EE_DATA_WRITE : 0;
1192         OUTW(EE_ENB | dataval               , ee_addr);
1193         eeprom_delay(EEPROM_SK_DELAY);
1194         OUTW(EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
1195         eeprom_delay(EEPROM_SK_DELAY);
1196         OUTW(EE_ENB | dataval               , ee_addr);
1197         eeprom_delay(EEPROM_SK_DELAY);
1198     }
1199     // Now clock out address zero, looking for the dummy 0 data bit
1200     for ( i = 1; i <= 12; i++ ) {
1201         OUTW(EE_ENB               , ee_addr);
1202         eeprom_delay(EEPROM_SK_DELAY);
1203         OUTW(EE_ENB | EE_SHIFT_CLK, ee_addr);
1204         eeprom_delay(EEPROM_SK_DELAY);
1205         OUTW(EE_ENB               , ee_addr);
1206         eeprom_delay(EEPROM_SK_DELAY);
1207         retval = INW(ee_addr) & EE_DATA_READ;
1208         if ( 0 == retval )
1209             break; // The dummy zero est arrive'
1210     }
1211
1212 #ifdef DEBUG_EE
1213     os_printf( "eeprom data bits %d (ioaddr %x)\n", i, ee_addr );
1214 #endif
1215     
1216     if ( 6 != i && 8 != i && 1 != i) {
1217 #ifdef DEBUG_EE
1218         os_printf( "*****EEPROM data bits not 6, 8 or 1*****\n" );
1219 #endif
1220         i = 6; // guess, to complete this routine
1221         addrbits = 1; // Flag no eeprom here.
1222     }
1223     else
1224         addrbits = i;
1225
1226     // clear the dataval, leave the clock low to read in the data regardless
1227     OUTW(EE_ENB, ee_addr);
1228     eeprom_delay(1);
1229     
1230     retval = INW(ee_addr);
1231     if ( (EE_DATA_READ & retval) != 0 ) {
1232 #ifdef DEBUG_EE
1233         os_printf( "Size EEPROM: Dummy data bit not 0, reg %x\n" , retval );
1234 #endif
1235     }
1236     eeprom_delay(1);
1237     
1238     for (i = EE_TOP_DATA_BIT; i >= 0; i--) {
1239         OUTW(EE_ENB | EE_SHIFT_CLK, ee_addr);
1240         eeprom_delay(EEPROM_SK_DELAY);
1241         retval = INW(ee_addr);
1242         eeprom_delay(EEPROM_SK_DELAY);
1243         OUTW(EE_ENB, ee_addr);
1244         eeprom_delay(EEPROM_SK_DELAY);
1245     }
1246     
1247     // Terminate the EEPROM access.
1248     OUTW(EE_ENB & ~EE_CS, ee_addr);
1249     eeprom_delay(EEPROM_DONE_DELAY);
1250     
1251     return addrbits;
1252 }
1253
1254
1255 // ------------------------------------------------------------------------
1256 //
1257 //                       READ EEPROM
1258 //
1259 // ------------------------------------------------------------------------
1260 static int
1261 read_eeprom(long ioaddr, int location, int addr_len)
1262 {
1263     unsigned short retval = 0;
1264     int ee_addr = ioaddr + SCBeeprom;
1265     int read_cmd = location | EE_READ_CMD(addr_len);
1266     int i, tries = 10;
1267
1268  try_again:
1269     // Should already be not-selected, but anyway:
1270     OUTW(EE_ENB & ~EE_CS, ee_addr);
1271     eeprom_delay(EEPROM_ENABLE_DELAY);
1272     OUTW(EE_ENB, ee_addr);
1273     eeprom_delay(EEPROM_ENABLE_DELAY);
1274     
1275     // Shift the read command bits out, changing only one bit per time.
1276     for (i = EE_TOP_CMD_BIT(addr_len); i >= 0; i--) {
1277         short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
1278         OUTW(EE_ENB | dataval               , ee_addr);
1279         eeprom_delay(EEPROM_SK_DELAY);
1280         OUTW(EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
1281         eeprom_delay(EEPROM_SK_DELAY);
1282         OUTW(EE_ENB | dataval               , ee_addr);
1283         eeprom_delay(EEPROM_SK_DELAY);
1284     }
1285
1286     // clear the dataval, leave the clock low
1287     OUTW(EE_ENB, ee_addr);
1288     eeprom_delay(1);
1289     
1290     retval = INW(ee_addr);
1291     // This should show a zero in the data read bit to confirm that the
1292     // address transfer is compelete.  If not, go to the start and try
1293     // again!
1294     if ( (0 != (retval & EE_DATA_READ)) && (tries-- > 0) ) {
1295     // Terminate the EEPROM access.
1296         OUTW(EE_ENB & ~EE_CS, ee_addr);
1297         eeprom_delay(EEPROM_DONE_DELAY);
1298 #ifdef DEBUG_EE
1299         os_printf( "Warning: Retrying EEPROM read word %d, address %x, try %d\n",
1300                    location,  ee_addr, tries+1 );
1301 #endif
1302         goto try_again;
1303     }
1304
1305     // This fires with one device on one of the customer boards!
1306     // (but is OK on all other h/w.  Worrying huh.)
1307     if ( (EE_DATA_READ & retval) != 0 ) {
1308 #ifdef DEBUG_EE
1309         os_printf( "Read EEPROM: Dummy data bit not 0, reg %x\n" , retval );
1310 #endif
1311     }
1312     eeprom_delay(1);
1313     retval = 0;
1314
1315     for (i = EE_TOP_DATA_BIT; i >= 0; i--) {
1316         OUTW(EE_ENB | EE_SHIFT_CLK, ee_addr);
1317         eeprom_delay(EEPROM_SK_DELAY);
1318         retval = (retval << 1) | ((INW(ee_addr) & EE_DATA_READ) ? 1 : 0);
1319         eeprom_delay(EEPROM_SK_DELAY);
1320         OUTW(EE_ENB, ee_addr);
1321         eeprom_delay(EEPROM_SK_DELAY);
1322     }
1323     
1324     // Terminate the EEPROM access.
1325     OUTW(EE_ENB & ~EE_CS, ee_addr);
1326     eeprom_delay(EEPROM_DONE_DELAY);
1327     
1328     return retval;
1329 }
1330
1331 static int
1332 read_eeprom_esa(struct i82559 *p_i82559, cyg_uint8 *addr)
1333 {
1334     int addr_length, i, count;
1335     cyg_uint16 checksum;
1336     cyg_uint32 ioaddr = p_i82559->io_address;
1337
1338     // read eeprom and get 82559's mac address
1339     addr_length = get_eeprom_size(ioaddr);
1340     // (this is the length of the *EEPROM*s address, not MAC address)
1341
1342     // If length is 1, it _probably_ means there's no EEPROM
1343     // present.  Couldn't find an explicit mention of this in the
1344     // docs, but length=1 appears to be the behaviour in that case.
1345     if (1 == addr_length) {
1346 #ifdef DEBUG_EE
1347         os_printf("Error: No EEPROM present for device %d\n", 
1348                   p_i82559->index);
1349 #endif
1350     } else {
1351         for (checksum = 0, i = 0, count = 0; count < (1 << addr_length); count++) {
1352             cyg_uint16 value;
1353             // read word from eeprom
1354             value = read_eeprom(ioaddr, count, addr_length);
1355 #ifdef DEBUG_EE
1356             os_printf( "%2d: %04x\n", count, value );
1357 #endif
1358             checksum += value;
1359             if (count < 3) {
1360                 addr[i++] = value & 0xFF;
1361                 addr[i++] = (value >> 8) & 0xFF;
1362             }
1363         }
1364
1365 #ifndef CYGHWR_DEVS_ETH_INTEL_I82559_HAS_ONE_EEPROM_WITHOUT_CRC
1366         // If the EEPROM checksum is wrong, the MAC address read
1367         // from the EEPROM is probably wrong as well. In that
1368         // case, we don't set mac_addr_ok, but continue the
1369         // initialization. If then somebody calls i82559_start
1370         // without calling eth_set_mac_address() first, we refuse
1371         // to bring up the interface, because running with an
1372         // invalid MAC address is not a very brilliant idea.
1373         
1374         if ((checksum & 0xFFFF) != 0xBABA)  {
1375             // selftest verified checksum, verify again
1376 #ifdef DEBUG_EE
1377             os_printf("Warning: Invalid EEPROM checksum %04X for device %d\n",
1378                       checksum, p_i82559->index);
1379 #endif
1380         } else // trailing block
1381 #endif // ! CYGHWR_DEVS_ETH_INTEL_I82559_HAS_ONE_EEPROM_WITHOUT_CRC
1382         {
1383 #ifdef DEBUG_EE
1384             os_printf("Valid EEPROM checksum\n");
1385 #endif
1386             return 1;
1387         }
1388     }
1389     return 0;
1390 }
1391 #endif // ! CYGHWR_DEVS_ETH_INTEL_I82559_HAS_NO_EEPROM
1392
1393 // ------------------------------------------------------------------------
1394 //
1395 //                NETWORK INTERFACE INITIALIZATION
1396 //
1397 //  Function : Init82559
1398 //
1399 //  Description :
1400 //       This routine resets, configures, and initializes the chip.
1401 //       It also clears the ethernet statistics structure, and selects
1402 //       which statistics are supported by this driver.
1403 //
1404 // ------------------------------------------------------------------------
1405 static bool
1406 i82559_init(struct cyg_netdevtab_entry * ndp)
1407 {
1408     static int initialized = 0; // only probe PCI et al *once*
1409
1410     struct eth_drv_sc *sc;
1411     cyg_uint32 selftest;
1412     volatile cyg_uint32 *p_selftest;
1413     cyg_uint32 ioaddr;
1414     int count;
1415     int ints;
1416     struct i82559 *p_i82559;
1417     cyg_uint8 mac_address[ETHER_ADDR_LEN];
1418
1419 #ifdef DEBUG
1420     //    db_printf("intel_i82559_init\n");
1421 #endif
1422
1423     sc = (struct eth_drv_sc *)(ndp->device_instance);
1424     p_i82559 = (struct i82559 *)(sc->driver_private);
1425
1426     IF_BAD_82559( p_i82559 ) {
1427 #ifdef DEBUG
1428         os_printf( "Bad device private pointer %x\n", sc->driver_private );
1429 #endif
1430         return 0;
1431     }
1432
1433     CHECK_NDP_SC_LINK();
1434
1435     if ( 0 == initialized++ ) {
1436         // then this is the first time ever:
1437         if ( ! pci_init_find_82559s() ) {
1438 #ifdef DEBUG
1439             os_printf( "pci_init_find_82559s failed\n" );
1440 #endif
1441             return 0;
1442         }
1443     }
1444
1445     // If this device is not present, exit
1446     if (0 == p_i82559->found)
1447         return 0;
1448
1449     p_i82559->mac_addr_ok = 0;
1450
1451     ioaddr = p_i82559->io_address; // get I/O address for 82559
1452
1453 #ifdef DEBUG
1454     os_printf("Init82559 %d @ %x\n82559 Self Test\n",
1455               p_i82559->index, (int)ndp);
1456 #endif
1457
1458     ints = Mask82559Interrupt(p_i82559);
1459
1460     // Reset device
1461     i82559_reset(p_i82559);
1462
1463     // Perform a system self-test. (get enough mem to round address)
1464     if ( (selftest = (cyg_uint32)pciwindow_mem_alloc(32) ) == 0)
1465         return (0);
1466     p_selftest = (cyg_uint32 *) ((selftest + 15) & ~0xf);
1467     p_selftest[0] = p_selftest[1] = -1;
1468
1469     OUTL( (VIRT_TO_BUS(p_selftest)) | I82559_SELFTEST, ioaddr + SCBPort);
1470     count = 0x7FFFF;                // Timeout for self-test.
1471     do {
1472         udelay(10);
1473     } while ( (p_selftest[1] == -1)  &&  (--count >= 0) );
1474
1475     // Reset device again after selftest
1476     i82559_reset(p_i82559);
1477
1478     Acknowledge82559Interrupt(p_i82559);
1479     UnMask82559Interrupt(p_i82559, ints );
1480     
1481     if (count < 0) {
1482         // Test timed out.
1483 #ifdef DEBUG
1484         os_printf("Self test failed\n");
1485 #endif
1486         return (0);
1487     }
1488 #ifdef DEBUG
1489     os_printf("  General self-test: %s.\n"
1490               "  Serial sub-system self-test: %s.\n"
1491               "  Internal registers self-test: %s.\n"
1492               "  ROM checksum self-test: %s (%08X).\n",
1493               HAL_LE32TOC(p_selftest[1]) & 0x1000 ? "failed" : "passed",
1494               HAL_LE32TOC(p_selftest[1]) & 0x0020 ? "failed" : "passed",
1495               HAL_LE32TOC(p_selftest[1]) & 0x0008 ? "failed" : "passed",
1496               HAL_LE32TOC(p_selftest[1]) & 0x0004 ? "failed" : "passed",
1497               HAL_LE32TOC(p_selftest[0]));
1498 #endif
1499
1500     // free self-test memory?
1501     // No, there's no point: this "heap" does not support free.
1502
1503     if (p_i82559->hardwired_esa) {
1504         // Hardwire the address without consulting the EEPROM.
1505         // When this flag is set, the p_i82559 will already contain
1506         // the ESA. Copy it to a mac_address for call to set_mac_addr
1507         mac_address[0] = p_i82559->mac_address[0];
1508         mac_address[1] = p_i82559->mac_address[1];
1509         mac_address[2] = p_i82559->mac_address[2];
1510         mac_address[3] = p_i82559->mac_address[3];
1511         mac_address[4] = p_i82559->mac_address[4];
1512         mac_address[5] = p_i82559->mac_address[5];
1513
1514         eth_set_mac_address(p_i82559, mac_address, 0);
1515
1516     } else {
1517
1518         // Acquire the ESA either from extenal means (probably RedBoot
1519         // variables) or from the attached EEPROM - if there is one.
1520
1521 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_GET_ESA
1522         int ok = false;
1523         int wflag = 0;
1524         CYGHWR_DEVS_ETH_INTEL_I82559_GET_ESA( p_i82559, mac_address, ok );
1525         if ( ok ) {
1526 #ifndef CYGHWR_DEVS_ETH_INTEL_I82559_HAS_NO_EEPROM
1527 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_HAS_ONE_EEPROM
1528             if ( CYGHWR_DEVS_ETH_INTEL_I82559_HAS_ONE_EEPROM == p_i82559->index )
1529 #endif // CYGHWR_DEVS_ETH_INTEL_I82559_HAS_ONE_EEPROM
1530             {
1531                 cyg_uint8 tmp_addr[ETHER_ADDR_LEN];
1532
1533                 // write eeprom address unless it is already there
1534                 wflag = 1;
1535                 if (read_eeprom_esa(p_i82559, tmp_addr)) {
1536                     int i;
1537                     for (i = 0; i < ETHER_ADDR_LEN; i++)
1538                         if (tmp_addr[i] != mac_address[i])
1539                             break;
1540                     if (i >= ETHER_ADDR_LEN)
1541                         wflag = 0;
1542                 }
1543             }
1544 #endif // ! CYGHWR_DEVS_ETH_INTEL_I82559_HAS_NO_EEPROM
1545             eth_set_mac_address(p_i82559, mac_address, wflag);
1546         }
1547 #else // ! CYGHWR_DEVS_ETH_INTEL_I82559_GET_ESA
1548
1549 #ifndef CYGHWR_DEVS_ETH_INTEL_I82559_HAS_NO_EEPROM
1550
1551 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_HAS_ONE_EEPROM
1552         if ( CYGHWR_DEVS_ETH_INTEL_I82559_HAS_ONE_EEPROM == p_i82559->index ) {
1553 #endif // CYGHWR_DEVS_ETH_INTEL_I82559_HAS_ONE_EEPROM
1554
1555             if (read_eeprom_esa(p_i82559, mac_address)) {
1556                 // record the MAC address in the device structure
1557                 p_i82559->mac_address[0] = mac_address[0];
1558                 p_i82559->mac_address[1] = mac_address[1];
1559                 p_i82559->mac_address[2] = mac_address[2];
1560                 p_i82559->mac_address[3] = mac_address[3];
1561                 p_i82559->mac_address[4] = mac_address[4];
1562                 p_i82559->mac_address[5] = mac_address[5];
1563
1564                 p_i82559->mac_addr_ok = 1;
1565
1566                 eth_set_mac_address(p_i82559, mac_address, 0);
1567             }
1568
1569 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_HAS_ONE_EEPROM
1570         }
1571         else { // We are now "in" another device
1572 #if 1 < CYGNUM_DEVS_ETH_INTEL_I82559_DEV_COUNT
1573             struct i82559 *other; // The one that *is* set up from EEPROM
1574             other = i82559_priv_array[CYGHWR_DEVS_ETH_INTEL_I82559_HAS_ONE_EEPROM];
1575             if ( other->mac_addr_ok ) {
1576                 mac_address[0] = other->mac_address[0];
1577                 mac_address[1] = other->mac_address[1];
1578                 mac_address[2] = other->mac_address[2];
1579                 mac_address[3] = other->mac_address[3];
1580                 mac_address[4] = other->mac_address[4];
1581                 mac_address[5] = other->mac_address[5];
1582
1583 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_HAS_ONE_EEPROM_MAC_ADJUST
1584                 mac_address[5] += CYGHWR_DEVS_ETH_INTEL_I82559_HAS_ONE_EEPROM_MAC_ADJUST;
1585 #endif
1586                 eth_set_mac_address(p_i82559, mac_address, 0);
1587             }
1588 #endif // 1 < CYGNUM_DEVS_ETH_INTEL_I82559_DEV_COUNT
1589         }
1590 #endif // CYGHWR_DEVS_ETH_INTEL_I82559_HAS_ONE_EEPROM
1591
1592 #endif // ! CYGHWR_DEVS_ETH_INTEL_I82559_HAS_NO_EEPROM
1593 #endif // ! CYGHWR_DEVS_ETH_INTEL_I82559_GET_ESA
1594     }
1595
1596 #ifdef DEBUG
1597     os_printf("i82559_init: MAC Address = %02X %02X %02X %02X %02X %02X\n",
1598               p_i82559->mac_address[0], p_i82559->mac_address[1],
1599               p_i82559->mac_address[2], p_i82559->mac_address[3],
1600               p_i82559->mac_address[4], p_i82559->mac_address[5]);
1601 #endif
1602     
1603     // and record the net dev pointer
1604     p_i82559->ndp = (void *)ndp;
1605     
1606     p_i82559->within_send = 0; // init recursion level
1607
1608     p_i82559->promisc = 0;  // None of these initially
1609     p_i82559->multicast_all = 0;
1610     p_i82559->oversized = 1;    // Enable this for VLAN mode by default
1611
1612     InitRxRing(p_i82559);
1613     InitTxRing(p_i82559);
1614
1615     CheckRxRing(p_i82559,__FUNCTION__,__LINE__);
1616
1617     // Initialize upper level driver
1618     if ( p_i82559->mac_addr_ok )
1619         (sc->funs->eth_drv->init)(sc, &(p_i82559->mac_address[0]) );
1620     else
1621         (sc->funs->eth_drv->init)(sc, NULL );
1622
1623     CheckRxRing(p_i82559,__FUNCTION__,__LINE__);
1624
1625     return (1);
1626 }
1627
1628 // ------------------------------------------------------------------------
1629 //
1630 //  Function : i82559_start
1631 //
1632 // ------------------------------------------------------------------------
1633 static void 
1634 i82559_start( struct eth_drv_sc *sc, unsigned char *enaddr, int flags )
1635 {
1636     struct i82559 *p_i82559;
1637     cyg_uint32 ioaddr;
1638 #ifdef KEEP_STATISTICS
1639     void *p_statistics;
1640 #endif
1641 #ifdef CYGPKG_NET
1642     struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1643 #endif
1644
1645     p_i82559 = (struct i82559 *)sc->driver_private;
1646     ioaddr = p_i82559->io_address; // get 82559's I/O address
1647     
1648     IF_BAD_82559( p_i82559 ) {
1649 #ifdef DEBUG
1650         os_printf( "i82559_start: Bad device pointer %x\n", p_i82559 );
1651 #endif
1652         return;
1653     }
1654
1655     if ( ! p_i82559->mac_addr_ok ) {
1656 #ifdef DEBUG
1657         os_printf("i82559_start %d: invalid MAC address, "
1658                   "can't bring up interface\n",
1659                   p_i82559->index );
1660 #endif
1661         return;
1662     }
1663
1664     if ( p_i82559->active )
1665         i82559_stop( sc );
1666
1667 #ifdef KEEP_STATISTICS
1668 #ifdef CYGDBG_DEVS_ETH_INTEL_I82559_KEEP_STATISTICS
1669     p_statistics = p_i82559->p_statistics;
1670     memset(p_statistics, 0xFFFFFFFF, sizeof(I82559_COUNTERS));
1671     // set statistics dump address
1672     wait_for_cmd_done(ioaddr, WAIT_CU);
1673     OUTL(VIRT_TO_BUS(p_statistics), ioaddr + SCBPointer);
1674     OUTW(SCB_M | CU_STATSADDR, ioaddr + SCBCmd);
1675     // Start dump command
1676     wait_for_cmd_done(ioaddr, WAIT_CU);
1677     OUTW(SCB_M | CU_DUMPSTATS, ioaddr + SCBCmd); // start register dump
1678     // ...and wait for it to complete operation
1679
1680     // The code to wait was bogus; it was looking at the structure in the
1681     // wrong way.  In any case, there is no need to wait, the
1682     // wait_for_cmd_done() in any following activity is good enough.
1683
1684 #endif
1685 #endif
1686
1687     // Enable device
1688     p_i82559->active = 1;
1689
1690     /* Enable promiscuous mode if requested, reception of oversized frames always.
1691      * The latter is needed for VLAN support and shouldn't hurt even if we're not
1692      * using VLANs.  Reset multicastALL reception choice.
1693      */
1694
1695     p_i82559->promisc = 0
1696 #ifdef CYGPKG_NET
1697                      || !!(ifp->if_flags & IFF_PROMISC)
1698 #endif
1699 #ifdef ETH_DRV_FLAGS_PROMISC_MODE
1700                      || !!(flags & ETH_DRV_FLAGS_PROMISC_MODE)
1701 #endif
1702             ;
1703
1704     p_i82559->multicast_all = 0;
1705
1706     i82559_configure(p_i82559,
1707                      p_i82559->promisc,
1708                      p_i82559->oversized,
1709                      p_i82559->multicast_all );
1710
1711 #ifdef DEBUG
1712     {
1713         int status = i82559_status( sc );
1714         os_printf("i82559_start %d flg %x Link = %s, %s Mbps, %s Duplex\n",
1715                   p_i82559->index,
1716                   *(int *)p_i82559,
1717                   status & GEN_STATUS_LINK ? "Up" : "Down",
1718                   status & GEN_STATUS_100MBPS ?  "100" : "10",
1719                   status & GEN_STATUS_FDX ? "Full" : "Half");
1720     }
1721 #endif
1722
1723     i82559_restart(p_i82559);
1724     CheckRxRing(p_i82559,__FUNCTION__,__LINE__);
1725 }
1726
1727 static void i82559_restart(struct i82559 *p_i82559)
1728 {
1729     cyg_uint32 ioaddr;
1730     ioaddr = p_i82559->io_address; // get 82559's I/O address
1731
1732     // Load pointer to Rx Ring and enable receiver
1733     wait_for_cmd_done(ioaddr, WAIT_RU);
1734     OUTL(VIRT_TO_BUS(p_i82559->rx_ring[0]), ioaddr + SCBPointer);
1735     OUTW(RUC_START, ioaddr + SCBCmd);
1736 }
1737
1738 // ------------------------------------------------------------------------
1739 //
1740 //  Function : i82559_status
1741 //
1742 // ------------------------------------------------------------------------
1743 int
1744 i82559_status( struct eth_drv_sc *sc )
1745 {
1746     int status;
1747     struct i82559 *p_i82559;
1748     cyg_uint32 ioaddr;
1749     p_i82559 = (struct i82559 *)sc->driver_private;
1750     
1751     IF_BAD_82559( p_i82559 ) {
1752 #ifdef DEBUG
1753         os_printf( "i82559_status: Bad device pointer %x\n", p_i82559 );
1754 #endif
1755         return 0;
1756     }
1757
1758     ioaddr = p_i82559->io_address; // get 82559's I/O address
1759
1760     status = INB(ioaddr + SCBGenStatus);
1761
1762     return status;
1763 }
1764
1765 // ------------------------------------------------------------------------
1766 //
1767 //  Function : BringDown82559
1768 //
1769 // ------------------------------------------------------------------------
1770
1771 static void
1772 i82559_stop( struct eth_drv_sc *sc )
1773 {
1774     struct i82559 *p_i82559;
1775
1776     p_i82559 = (struct i82559 *)sc->driver_private;
1777
1778     IF_BAD_82559( p_i82559 ) {
1779 #ifdef DEBUG
1780         os_printf( "i82559_stop: Bad device pointer %x\n", p_i82559 );
1781 #endif
1782         return;
1783     }
1784    
1785 #ifdef DEBUG
1786     os_printf("i82559_stop %d flg %x\n", p_i82559->index, *(int *)p_i82559 );
1787 #endif
1788
1789     p_i82559->active = 0;               // stop people tormenting it
1790     i82559_reset(p_i82559);             // that should stop it
1791
1792     // Now that it's inactive, return all pending tx status to the higher
1793     // layers:
1794     // "Done" txen are from here to active, OR 
1795     // the remove one if the queue is full AND its status is nonzero:
1796     while ( 1 ) {
1797         int tx_descriptor_remove = p_i82559->tx_descriptor_remove;
1798         unsigned long key = p_i82559->tx_keys[ tx_descriptor_remove ];
1799
1800         // Break out if "remove" is the active slot
1801         // (AND the Q is not full, or the Tx is not complete yet)
1802         if ( (tx_descriptor_remove == p_i82559->tx_descriptor_active) &&
1803              ( ! p_i82559->tx_queue_full) )
1804             break;
1805             
1806         // Zero the key in global state before the callback:
1807         p_i82559->tx_keys[ tx_descriptor_remove ] = 0;
1808
1809 #ifdef DEBUG_82559
1810         os_printf("Stop: TxDone %d %x: KEY %x TxCB %x\n",
1811                   p_i82559->index, (int)p_i82559, key, 
1812                   p_i82559->tx_ring[ tx_descriptor_remove ]);
1813 #endif
1814         // tx_done() can now cope with a NULL key, no guard needed here
1815         (sc->funs->eth_drv->tx_done)( sc, key, 1 /* status */ );
1816
1817         if ( ++tx_descriptor_remove >= MAX_TX_DESCRIPTORS )
1818             tx_descriptor_remove = 0;
1819         p_i82559->tx_descriptor_remove = tx_descriptor_remove;
1820         p_i82559->tx_queue_full = 0;
1821     }
1822
1823     ResetRxRing( p_i82559 );
1824     ResetTxRing( p_i82559 );
1825     CheckRxRing(p_i82559,__FUNCTION__,__LINE__);
1826 }
1827
1828
1829 // ------------------------------------------------------------------------
1830 //
1831 //  Function : InitRxRing
1832 //
1833 // ------------------------------------------------------------------------
1834 static void
1835 InitRxRing(struct i82559* p_i82559)
1836 {
1837     int i;
1838     RFD *rfd;
1839     RFD *p_rfd = 0;
1840 #ifdef DEBUG_82559
1841     os_printf("InitRxRing %d\n", p_i82559->index);
1842 #endif
1843     for ( i = 0; i < MAX_RX_DESCRIPTORS; i++ ) {
1844         rfd = (RFD *)pciwindow_mem_alloc(RFD_SIZEOF + MAX_RX_PACKET_SIZE);
1845         p_i82559->rx_ring[i] = rfd;
1846         if ( i )
1847             WRITEMEM32(p_rfd+RFD_LINK, HAL_CTOLE32(VIRT_TO_BUS(rfd)));
1848         p_rfd = (RFD *)rfd;
1849     }
1850     // link last RFD to first:
1851     WRITEMEM32(p_rfd+RFD_LINK,
1852                      HAL_CTOLE32(VIRT_TO_BUS(p_i82559->rx_ring[0])));
1853
1854     CheckRxRing(p_i82559,__FUNCTION__,__LINE__);
1855     ResetRxRing( p_i82559 );
1856 }
1857
1858 // ------------------------------------------------------------------------
1859 //
1860 //  Function : ResetRxRing
1861 //
1862 // ------------------------------------------------------------------------
1863 static void
1864 ResetRxRing(struct i82559* p_i82559)
1865 {
1866     RFD *p_rfd;
1867     int i;
1868 #ifdef DEBUG_82559
1869     os_printf("ResetRxRing %d\n", p_i82559->index);
1870 #endif
1871     for ( i = 0; i < MAX_RX_DESCRIPTORS; i++ ) {
1872         p_rfd = p_i82559->rx_ring[i];
1873         CYG_ASSERT( (cyg_uint8 *)p_rfd >= i82559_heap_base, "rfd under" );
1874         CYG_ASSERT( (cyg_uint8 *)p_rfd <  i82559_heap_free, "rfd over" );
1875 #ifdef CYGDBG_USE_ASSERTS
1876         {
1877           RFD *p_rfd2;
1878           cyg_uint32 link;
1879           p_rfd2 = p_i82559->rx_ring[ ( i ? (i-1) : (MAX_RX_DESCRIPTORS-1) ) ];
1880           READMEM32(p_rfd2 + RFD_LINK, link);
1881           if (!(HAL_LE32TOC(link) == VIRT_TO_BUS(p_rfd))) {
1882             START_CONSOLE();
1883             diag_printf("Bad link eth%d %p %p %d %p\n",
1884                         p_i82559->index,
1885                         HAL_LE32TOC(link), VIRT_TO_BUS(p_rfd),
1886                         i, __builtin_return_address(0));
1887             END_CONSOLE();
1888           }
1889           CYG_ASSERT( HAL_LE32TOC(link) == VIRT_TO_BUS(p_rfd), 
1890                       "rfd linked list broken" );
1891         }
1892 #endif
1893         WRITEMEM32(p_rfd + RFD_STATUS, 0 ); // do NOT suspend after just one rx
1894         WRITEMEM16(p_rfd + RFD_COUNT, 0);
1895         WRITEMEM32(p_rfd + RFD_RDB_ADDR, HAL_CTOLE32(0xFFFFFFFF));
1896         WRITEMEM16(p_rfd + RFD_SIZE, HAL_CTOLE16(MAX_RX_PACKET_SIZE));
1897     }
1898     p_i82559->next_rx_descriptor = 0;
1899     // And set an end-of-list marker in the previous one.
1900     WRITEMEM32(p_rfd + RFD_STATUS, RFD_STATUS_EL);
1901     CheckRxRing(p_i82559,__FUNCTION__,__LINE__);
1902 }
1903
1904 // ------------------------------------------------------------------------
1905 //
1906 //  Function : CheckRxRing
1907 //
1908 // ------------------------------------------------------------------------
1909 void
1910 CheckRxRing(struct i82559* p_i82559, const char * func, int line)
1911 {
1912     RFD *p_rfd;
1913     int i;
1914     RFD *p_rfd2;
1915     cyg_uint32 link;
1916     
1917     //    console_printf("%s:%d(eth%d)\n",func,line,p_i82559->index);
1918
1919     for ( i = 0; i < MAX_RX_DESCRIPTORS; i++ ) {
1920       p_rfd = p_i82559->rx_ring[i];
1921       if (! ((cyg_uint8 *)p_rfd >= i82559_heap_base))
1922         console_printf("rfd under: %s:%d\n", func, line);
1923       if (! ((cyg_uint8 *)p_rfd <  i82559_heap_free)) 
1924         console_printf("rfd over: %s:%d\n", func, line );
1925       
1926       p_rfd2 = p_i82559->rx_ring[ ( i ? (i-1) : (MAX_RX_DESCRIPTORS-1) ) ];
1927       READMEM32(p_rfd2 + RFD_LINK, link);
1928       if (!(HAL_LE32TOC(link) == VIRT_TO_BUS(p_rfd))) {
1929         console_printf("Bad link eth%d %p %p %d %p: %s:%d\n",
1930                        p_i82559->index,
1931                        HAL_LE32TOC(link), VIRT_TO_BUS(p_rfd),
1932                        i, __builtin_return_address(0),
1933                        func,line);
1934         CYG_ASSERT(HAL_LE32TOC(link) == VIRT_TO_BUS(p_rfd),"Bad Link");
1935       }
1936     }
1937 }
1938
1939 // ------------------------------------------------------------------------
1940 //
1941 //  Function : PacketRxReady     (Called from delivery thread & foreground)
1942 //
1943 // ------------------------------------------------------------------------
1944 static void
1945 PacketRxReady(struct i82559* p_i82559)
1946 {
1947     RFD *p_rfd;
1948     int next_descriptor;
1949     int length, ints;
1950     struct cyg_netdevtab_entry *ndp;
1951     struct eth_drv_sc *sc;
1952     cyg_uint32 ioaddr;
1953     cyg_uint16 status;
1954
1955     ndp = (struct cyg_netdevtab_entry *)(p_i82559->ndp);
1956     sc = (struct eth_drv_sc *)(ndp->device_instance);
1957
1958     CheckRxRing(p_i82559,__FUNCTION__,__LINE__);
1959
1960     CHECK_NDP_SC_LINK();
1961
1962     ioaddr = p_i82559->io_address;
1963
1964     next_descriptor = p_i82559->next_rx_descriptor;
1965     p_rfd = p_i82559->rx_ring[next_descriptor];
1966
1967     CYG_ASSERT( (cyg_uint8 *)p_rfd >= i82559_heap_base, "rfd under" );
1968     CYG_ASSERT( (cyg_uint8 *)p_rfd <  i82559_heap_free, "rfd over" );
1969
1970     while ( 1 ) {
1971         cyg_uint32 rxstatus;
1972         cyg_uint16 rxstatus_hi;
1973         READMEM32(p_rfd + RFD_STATUS, rxstatus);
1974         if (0 == (rxstatus & RFD_STATUS_C))
1975             break;
1976
1977         READMEM16(p_rfd + RFD_STATUS_HI, rxstatus_hi);
1978         rxstatus_hi |= RFD_STATUS_HI_EL;
1979         WRITEMEM16(p_rfd + RFD_STATUS_HI, rxstatus_hi);
1980
1981         READMEM16(p_rfd + RFD_COUNT, length);
1982         length = HAL_LE16TOC(length);
1983         length &= RFD_COUNT_MASK;
1984
1985 #ifdef DEBUG_82559
1986         os_printf( "Device %d (eth%d), rx descriptor %d:\n", 
1987                    p_i82559->index, p_i82559->index, next_descriptor );
1988 //        dump_rfd( p_rfd, 1 );
1989 #endif
1990
1991         p_i82559->next_rx_descriptor = next_descriptor;
1992         // Check for bogusly short packets; can happen in promisc mode:
1993         // Asserted against and checked by upper layer driver.
1994 #ifdef CYGPKG_NET
1995         if ( length > sizeof( struct ether_header ) )
1996             // then it is acceptable; offer the data to the network stack
1997 #endif
1998         (sc->funs->eth_drv->recv)( sc, length );
1999
2000         WRITEMEM16(p_rfd + RFD_COUNT, 0);
2001         WRITEMEM16(p_rfd + RFD_STATUS_LO, 0);
2002
2003         // The just-emptied slot is now ready for re-use and already marked EL;
2004         // we can now remove the EL marker from the previous one.
2005         if ( 0 == next_descriptor )
2006             p_rfd = p_i82559->rx_ring[ MAX_RX_DESCRIPTORS-1 ];
2007         else
2008             p_rfd = p_i82559->rx_ring[ next_descriptor-1 ];
2009         // The previous one: check it *was* marked before clearing.
2010         READMEM16(p_rfd + RFD_STATUS_HI, rxstatus_hi);
2011         CYG_ASSERT( rxstatus_hi & RFD_STATUS_HI_EL, "No prev EL" );
2012         // that word is not written by the device.
2013         WRITEMEM16(p_rfd + RFD_STATUS_HI, 0);
2014
2015 #ifdef KEEP_STATISTICS
2016         statistics[p_i82559->index].rx_deliver++;
2017 #endif
2018         if (++next_descriptor >= MAX_RX_DESCRIPTORS)
2019             next_descriptor = 0;
2020         p_rfd = p_i82559->rx_ring[next_descriptor];
2021
2022         CYG_ASSERT( (cyg_uint8 *)p_rfd >= i82559_heap_base, "rfd under" );
2023         CYG_ASSERT( (cyg_uint8 *)p_rfd <  i82559_heap_free, "rfd over" );
2024
2025         CheckRxRing(p_i82559,__FUNCTION__,__LINE__);
2026
2027 #ifdef CYGPKG_IO_ETH_DRIVERS_STAND_ALONE
2028         // Can't deliver more than one packet in polled standalone mode
2029         break;
2030 #endif
2031     }
2032
2033     CheckRxRing(p_i82559,__FUNCTION__,__LINE__);
2034     
2035     // See if the RU has gone idle (usually because of out of resource
2036     // condition) and restart it if needs be.
2037     ints = Mask82559Interrupt(p_i82559);
2038     status = INW(ioaddr + SCBStatus);
2039     if ( RU_STATUS_READY != (status & RU_STATUS_MASK) ) {
2040         // Acknowledge the RX INT sources
2041         OUTW( SCB_INTACK_RX, ioaddr + SCBStatus);
2042         // (see pages 6-10 & 6-90)
2043
2044 #ifdef KEEP_STATISTICS
2045         statistics[p_i82559->index].rx_restart++;
2046 #endif
2047         // There's an end-of-list marker out there somewhere...
2048         // So mop it up; it takes a little time but this is infrequent.
2049         ResetRxRing( p_i82559 );  
2050         next_descriptor = 0;        // re-initialize next desc.
2051         // load pointer to Rx Ring
2052         wait_for_cmd_done(ioaddr, WAIT_RU);
2053         OUTL(VIRT_TO_BUS(p_i82559->rx_ring[0]),
2054              ioaddr + SCBPointer);
2055         OUTW(RUC_START, ioaddr + SCBCmd);
2056         Acknowledge82559Interrupt(p_i82559);
2057     }
2058     UnMask82559Interrupt(p_i82559, ints);
2059
2060     p_i82559->next_rx_descriptor = next_descriptor;
2061 }
2062
2063 // and the callback function
2064
2065 static void 
2066 i82559_recv( struct eth_drv_sc *sc, struct eth_drv_sg *sg_list, int sg_len )
2067 {
2068     struct i82559 *p_i82559;
2069     RFD *p_rfd;
2070     int next_descriptor;
2071     int total_len;
2072     struct eth_drv_sg *last_sg;
2073     volatile cyg_uint8 *from_p;
2074     cyg_uint32 rxstatus;
2075     cyg_uint16 rxstatus16;
2076
2077     p_i82559 = (struct i82559 *)sc->driver_private;
2078     
2079     IF_BAD_82559( p_i82559 ) {
2080 #ifdef DEBUG
2081         os_printf( "i82559_recv: Bad device pointer %x\n", p_i82559 );
2082 #endif
2083         return;
2084     }
2085
2086     next_descriptor = p_i82559->next_rx_descriptor;
2087     p_rfd = p_i82559->rx_ring[next_descriptor];
2088     
2089     CYG_ASSERT( (cyg_uint8 *)p_rfd >= i82559_heap_base, "rfd under" );
2090     CYG_ASSERT( (cyg_uint8 *)p_rfd <  i82559_heap_free, "rfd over" );
2091
2092     READMEM32(p_rfd + RFD_STATUS, rxstatus);
2093     CYG_ASSERT( rxstatus & RFD_STATUS_C, "No complete frame" );
2094     CYG_ASSERT( rxstatus & RFD_STATUS_EL, "No marked frame" );
2095     READMEM16(p_rfd + RFD_STATUS_LO, rxstatus16 );
2096     CYG_ASSERT( rxstatus16 & RFD_STATUS_LO_C, "No complete frame 2" );
2097     READMEM16(p_rfd + RFD_STATUS_HI, rxstatus16 );
2098     CYG_ASSERT( rxstatus16 & RFD_STATUS_HI_EL, "No marked frame 2" );
2099
2100     CheckRxRing(p_i82559,__FUNCTION__,__LINE__);
2101     
2102     if ( 0 == (rxstatus & RFD_STATUS_C) )
2103         return;
2104
2105     READMEM16(p_rfd + RFD_COUNT, total_len);
2106     total_len = HAL_LE16TOC(total_len);
2107     total_len &= RFD_COUNT_MASK;
2108     
2109 #ifdef DEBUG_82559
2110     os_printf("Rx %d %x (status %x): %d sg's, %d bytes\n",
2111               p_i82559->index, (int)p_i82559, 
2112               HAL_LE32TOC(rxstatus), sg_len, total_len);
2113 #endif
2114
2115     // Copy the data to the network stack
2116     from_p = p_rfd + RFD_BUFFER;
2117
2118     // check we have memory to copy into; we would be called even if
2119     // caller was out of memory in order to maintain our state.
2120     if ( 0 == sg_len || 0 == sg_list )
2121         return; // caller was out of mbufs
2122
2123     CYG_ASSERT( 0 < sg_len, "sg_len underflow" );
2124     CYG_ASSERT( MAX_ETH_DRV_SG >= sg_len, "sg_len overflow" );
2125
2126     for ( last_sg = &sg_list[sg_len]; sg_list < last_sg; sg_list++ ) {
2127         cyg_uint8 *to_p;
2128         int l;
2129             
2130         to_p = (cyg_uint8 *)(sg_list->buf);
2131         l = sg_list->len;
2132
2133         CYG_ASSERT( 0 <= l, "sg length -ve" );
2134
2135         if ( 0 >= l || 0 == to_p )
2136             return; // caller was out of mbufs
2137
2138         if ( l > total_len )
2139             l = total_len;
2140
2141         memcpy( to_p, (unsigned char *)from_p, l );
2142         from_p += l;
2143         total_len -= l;
2144     }
2145
2146     CYG_ASSERT( 0 == total_len, "total_len mismatch in rx" );
2147     CYG_ASSERT( last_sg == sg_list, "sg count mismatch in rx" );
2148     CYG_ASSERT( p_rfd + RFD_BUFFER < from_p, "from_p wild in rx" );
2149     CYG_ASSERT( p_rfd + RFD_BUFFER + MAX_RX_PACKET_SIZE >= from_p,
2150                 "from_p overflow in rx" );
2151 }
2152
2153
2154 // ------------------------------------------------------------------------
2155 //
2156 //  Function : InitTxRing
2157 //
2158 // ------------------------------------------------------------------------
2159 static void
2160 InitTxRing(struct i82559* p_i82559)
2161 {
2162     int i;
2163     cyg_uint32 ioaddr;
2164
2165 #ifdef DEBUG_82559
2166     os_printf("InitTxRing %d\n", p_i82559->index);
2167 #endif
2168     ioaddr = p_i82559->io_address;
2169     for ( i = 0; i < MAX_TX_DESCRIPTORS; i++) {
2170         p_i82559->tx_ring[i] = (TxCB *)pciwindow_mem_alloc(
2171             TxCB_SIZEOF + MAX_TX_PACKET_SIZE);
2172     }
2173
2174     ResetTxRing(p_i82559);
2175 }
2176
2177 // ------------------------------------------------------------------------
2178 //
2179 //  Function : ResetTxRing
2180 //
2181 // ------------------------------------------------------------------------
2182 static void
2183 ResetTxRing(struct i82559* p_i82559)
2184 {
2185     int i;
2186     cyg_uint32 ioaddr;
2187
2188 #ifdef DEBUG_82559
2189     os_printf("ResetTxRing %d\n", p_i82559->index);
2190 #endif
2191     ioaddr = p_i82559->io_address;
2192     p_i82559->tx_descriptor_add =
2193         p_i82559->tx_descriptor_active = 
2194         p_i82559->tx_descriptor_remove = 0;
2195     p_i82559->tx_in_progress =
2196         p_i82559->tx_queue_full = 0;
2197
2198     for ( i = 0; i < MAX_TX_DESCRIPTORS; i++) {
2199         TxCB *p_txcb = p_i82559->tx_ring[i];
2200         CYG_ASSERT( (cyg_uint8 *)p_txcb >= i82559_heap_base, "txcb under" );
2201         CYG_ASSERT( (cyg_uint8 *)p_txcb <  i82559_heap_free, "txcb over" );
2202
2203         WRITEMEM16(p_txcb + TxCB_STATUS, 0);
2204         WRITEMEM16(p_txcb + TxCB_CMD, 0);
2205         WRITEMEM32(p_txcb + TxCB_LINK,
2206                          HAL_CTOLE32(VIRT_TO_BUS((cyg_uint32)p_txcb)));
2207         WRITEMEM32(p_txcb + TxCB_TBD_ADDR, HAL_CTOLE32(0xFFFFFFFF));
2208         WRITEMEM8(p_txcb + TxCB_TBD_NUMBER, 0);
2209         WRITEMEM8(p_txcb + TxCB_TX_THRESHOLD, 16);
2210         WRITEMEM16(p_txcb + TxCB_COUNT,
2211                          HAL_CTOLE16(TxCB_COUNT_EOF | 0));
2212         p_i82559->tx_keys[i] = 0;
2213     }
2214     
2215     wait_for_cmd_done(ioaddr,WAIT_CU);
2216     OUTL(0, ioaddr + SCBPointer);
2217     OUTW(SCB_M | CU_ADDR_LOAD, ioaddr + SCBCmd);
2218     CheckRxRing(p_i82559,__FUNCTION__,__LINE__);
2219 }
2220
2221 // ------------------------------------------------------------------------
2222 //
2223 //  Function : TxMachine          (Called from FG & ISR)
2224 //
2225 // This steps the Tx Machine onto the next record if necessary - allowing
2226 // for missed interrupts, and so on.
2227 // ------------------------------------------------------------------------
2228
2229 static void
2230 TxMachine(struct i82559* p_i82559)
2231 {
2232     int tx_descriptor_active;
2233     cyg_uint32 ioaddr;
2234
2235     tx_descriptor_active = p_i82559->tx_descriptor_active;
2236     ioaddr = p_i82559->io_address;  
2237     
2238     CheckRxRing(p_i82559,__FUNCTION__,__LINE__);
2239     // See if the CU is idle when we think it isn't; this is the only place
2240     // tx_descriptor_active is advanced. (Also recovers from a dropped intr)
2241     if ( p_i82559->tx_in_progress ) {
2242         cyg_uint16 status;
2243         status = INW(ioaddr + SCBStatus);
2244         if ( 0 == (status & CU_STATUS_MASK) ) {
2245             // It is idle.  So ack the TX interrupts
2246             OUTW( SCB_INTACK_TX, ioaddr + SCBStatus);
2247             // (see pages 6-10 & 6-90)
2248
2249             // and step on to the next queued tx.
2250             p_i82559->tx_in_progress = 0;
2251             if ( ++tx_descriptor_active >= MAX_TX_DESCRIPTORS )
2252                 tx_descriptor_active = 0;
2253             p_i82559->tx_descriptor_active = tx_descriptor_active;
2254         }
2255     }
2256
2257     // is the CU idle, and there a next tx to set going?
2258     if ( ( ! p_i82559->tx_in_progress )
2259          && (( p_i82559->tx_descriptor_add != tx_descriptor_active )
2260             || p_i82559->tx_queue_full ) )  {
2261         TxCB *p_txcb = p_i82559->tx_ring[tx_descriptor_active];
2262         cyg_uint16 status;
2263
2264         // start Tx operation
2265         wait_for_cmd_done(ioaddr, WAIT_CU);
2266         status = INW(ioaddr + SCBStatus);
2267         // The following assert sometimes fires here, apparantly harmless!
2268         // So check a second time to let the cursed thing settle and not
2269         // tell us lies.
2270         if ( 0 != (status & CU_STATUS_MASK) ) {
2271 #ifdef CYGDBG_USE_ASSERTS
2272             missed_interrupt.bad_cu_idles++;
2273 #endif
2274             wait_for_cmd_done(ioaddr, WAIT_CU);
2275             status = INW(ioaddr + SCBStatus);
2276         }
2277         CYG_ASSERT( ( 0 == (status & CU_STATUS_MASK)), "CU not idle");
2278         CYG_ASSERT( (cyg_uint8 *)p_txcb >= i82559_heap_base, "txcb under" );
2279         CYG_ASSERT( (cyg_uint8 *)p_txcb <  i82559_heap_free, "txcb over" );
2280 #ifdef DEBUG_82559
2281         {
2282             unsigned long key = p_i82559->tx_keys[ tx_descriptor_active ];
2283             os_printf("Tx %d %x: Starting Engines: KEY %x TxCB %x\n",
2284                       p_i82559->index, (int)p_i82559, key, p_txcb);
2285         }
2286 #endif
2287
2288         OUTL(VIRT_TO_BUS(p_txcb), ioaddr + SCBPointer);
2289         OUTW(CU_START, ioaddr + SCBCmd);
2290         p_i82559->tx_in_progress = 1;
2291     }
2292     CheckRxRing(p_i82559,__FUNCTION__,__LINE__);
2293 }
2294
2295 // ------------------------------------------------------------------------
2296 //
2297 //  Function : TxDone          (Called from delivery thread)
2298 //
2299 // This returns Tx's from the Tx Machine to the stack (ie. reports
2300 // completion) - allowing for missed interrupts, and so on.
2301 // ------------------------------------------------------------------------
2302
2303 static void
2304 TxDone(struct i82559* p_i82559)
2305 {
2306     struct cyg_netdevtab_entry *ndp;
2307     struct eth_drv_sc *sc;
2308     int tx_descriptor_remove = p_i82559->tx_descriptor_remove;
2309
2310     ndp = (struct cyg_netdevtab_entry *)(p_i82559->ndp);
2311     sc = (struct eth_drv_sc *)(ndp->device_instance);
2312
2313     CHECK_NDP_SC_LINK();
2314     
2315     // "Done" txen are from here to active, OR 
2316     // the remove one if the queue is full AND its status is nonzero:
2317     while ( 1 ) {
2318         cyg_uint16 txstatus;
2319         TxCB *p_txcb = p_i82559->tx_ring[ tx_descriptor_remove ];
2320         unsigned long key = p_i82559->tx_keys[ tx_descriptor_remove ];
2321
2322         READMEM16(p_txcb + TxCB_STATUS, txstatus);
2323
2324         // Break out if "remove" is the active slot
2325         // (AND the Q is not full, or the Tx is not complete yet)
2326         if ( (tx_descriptor_remove == p_i82559->tx_descriptor_active) &&
2327              ( ( ! p_i82559->tx_queue_full) || (0 == txstatus) ) )
2328             break;
2329
2330         // Zero the key in global state before the callback:
2331         p_i82559->tx_keys[ tx_descriptor_remove ] = 0;
2332
2333 #ifdef DEBUG_82559
2334         os_printf("TxDone %d %x: KEY %x TxCB %x\n",
2335                   p_i82559->index, (int)p_i82559, key, p_txcb );
2336 #endif
2337         // tx_done() can now cope with a NULL key, no guard needed here
2338         (sc->funs->eth_drv->tx_done)( sc, key, 1 /* status */ );
2339         
2340         if ( ++tx_descriptor_remove >= MAX_TX_DESCRIPTORS )
2341             tx_descriptor_remove = 0;
2342         p_i82559->tx_descriptor_remove = tx_descriptor_remove;
2343         p_i82559->tx_queue_full = 0;
2344     }
2345 }
2346
2347
2348 // ------------------------------------------------------------------------
2349 //
2350 //  Function : i82559_can_send
2351 //
2352 // ------------------------------------------------------------------------
2353
2354 static int 
2355 i82559_can_send(struct eth_drv_sc *sc)
2356 {
2357     struct i82559 *p_i82559;
2358     int ints;
2359
2360     p_i82559 = (struct i82559 *)sc->driver_private;
2361
2362     IF_BAD_82559( p_i82559 ) {
2363 #ifdef DEBUG
2364         os_printf( "i82559_send: Bad device pointer %x\n", p_i82559 );
2365 #endif
2366         return 0;
2367     }
2368     
2369     // Advance TxMachine atomically
2370     ints = Mask82559Interrupt(p_i82559);
2371
2372     // This helps unstick deadly embraces.
2373     CYG_ASSERT( p_i82559->within_send < 10, "send: Excess send recursions" );
2374     p_i82559->within_send++;
2375
2376 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_DEMUX_ALL
2377     {
2378         int i;
2379         // The problem is, if DEMUX_ALL, either device can eat the other's
2380         // interrupts; so we must poll them *both*:
2381         for (i = 0; i < CYGNUM_DEVS_ETH_INTEL_I82559_DEV_COUNT; i++) {
2382             p_i82559 = i82559_priv_array[i];
2383             if ( p_i82559->active ) {
2384                 // See if the Tx machine is wedged - reset if so:
2385                 Check82559TxLockupTimeout(p_i82559);
2386                 TxMachine(p_i82559);
2387                 Acknowledge82559Interrupt(p_i82559);
2388 #ifndef CYGPKG_IO_ETH_DRIVERS_STAND_ALONE
2389                 // We are not prepared to receive a packet now if we are in a polled
2390                 // standalone configuration.
2391                 PacketRxReady(p_i82559);
2392 #endif
2393 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_MISSED_INTERRUPT
2394                 if ( CYGHWR_DEVS_ETH_INTEL_I82559_MISSED_INTERRUPT(p_i82559) ) {
2395 #ifdef CYGDBG_USE_ASSERTS
2396                     missed_interrupt.can_send++;
2397 #endif
2398                     eth_isr( p_i82559->vector, (cyg_addrword_t)p_i82559 );
2399                     eth_dsr( p_i82559->vector, 1, (cyg_addrword_t)p_i82559 );
2400                 }
2401 #endif
2402             }
2403         }
2404     }
2405     // ensure we look at the correct device at the end
2406     p_i82559 = (struct i82559 *)sc->driver_private;
2407 #else // no CYGHWR_DEVS_ETH_INTEL_I82559_DEMUX_ALL
2408
2409     // See if the Tx machine is wedged - reset if so:
2410     Check82559TxLockupTimeout(p_i82559);
2411     TxMachine(p_i82559);
2412     Acknowledge82559Interrupt(p_i82559); // This can eat an Rx interrupt, so
2413 #ifndef CYGPKG_IO_ETH_DRIVERS_STAND_ALONE
2414     // We are not prepared to receive a packet now if we are in a polled
2415     // standalone configuration.
2416     PacketRxReady(p_i82559);
2417 #endif
2418 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_MISSED_INTERRUPT
2419     if ( CYGHWR_DEVS_ETH_INTEL_I82559_MISSED_INTERRUPT(p_i82559) ) {
2420 #ifdef CYGDBG_USE_ASSERTS
2421         missed_interrupt.can_send++;
2422 #endif
2423         eth_isr( p_i82559->vector, (cyg_addrword_t)p_i82559 );
2424         eth_dsr( p_i82559->vector, 1, (cyg_addrword_t)p_i82559 );
2425     }
2426 #endif
2427 #endif // no CYGHWR_DEVS_ETH_INTEL_I82559_DEMUX_ALL
2428
2429     p_i82559->within_send--;
2430     UnMask82559Interrupt(p_i82559,ints);
2431
2432     return (0 == p_i82559->within_send) && ! p_i82559->tx_queue_full;
2433 }
2434
2435 // ------------------------------------------------------------------------
2436 //
2437 //  Function : i82559_send
2438 //
2439 // ------------------------------------------------------------------------
2440
2441 static void 
2442 i82559_send(struct eth_drv_sc *sc,
2443             struct eth_drv_sg *sg_list, int sg_len, int total_len,
2444             unsigned long key)
2445 {
2446     struct i82559 *p_i82559;
2447     int tx_descriptor_add, ints;
2448     TxCB *p_txcb;
2449     cyg_uint32 ioaddr;
2450
2451     p_i82559 = (struct i82559 *)sc->driver_private;
2452
2453     IF_BAD_82559( p_i82559 ) {
2454 #ifdef DEBUG
2455         os_printf( "i82559_send: Bad device pointer %x\n", p_i82559 );
2456 #endif
2457         return;
2458     }
2459
2460 #ifdef DEBUG_82559
2461     os_printf("Tx %d %x: %d sg's, %d bytes, KEY %x\n",
2462               p_i82559->index, (int)p_i82559, sg_len, total_len, key );
2463 #endif
2464
2465     if ( ! p_i82559->active )
2466         return;                         // device inactive, no return
2467 #ifdef KEEP_STATISTICS
2468     statistics[p_i82559->index].tx_count++;
2469 #endif
2470     ioaddr = p_i82559->io_address;      // get device I/O address
2471
2472     // See if the Tx machine is wedged - reset if so:
2473     Check82559TxLockupTimeout(p_i82559);
2474
2475     if ( p_i82559->tx_queue_full ) {
2476 #ifdef KEEP_STATISTICS
2477         statistics[p_i82559->index].tx_dropped++;
2478 #endif
2479 #ifdef DEBUG_82559
2480         os_printf( "i82559_send: Queue full, device %x, key %x\n",
2481                    p_i82559, key );
2482 #endif
2483     }
2484     else {
2485         struct eth_drv_sg *last_sg;
2486         volatile cyg_uint8 *to_p;
2487
2488         tx_descriptor_add = p_i82559->tx_descriptor_add;
2489
2490         p_i82559->tx_keys[tx_descriptor_add] = key;
2491
2492         p_txcb = p_i82559->tx_ring[tx_descriptor_add];
2493
2494         CYG_ASSERT( (cyg_uint8 *)p_txcb >= i82559_heap_base, "txcb under" );
2495         CYG_ASSERT( (cyg_uint8 *)p_txcb <  i82559_heap_free, "txcb over" );
2496
2497         WRITEMEM16(p_txcb + TxCB_STATUS, 0);
2498         WRITEMEM16(p_txcb + TxCB_CMD,
2499                          (TxCB_CMD_TRANSMIT | TxCB_CMD_S
2500                           | TxCB_CMD_I | TxCB_CMD_EL));
2501         WRITEMEM32(p_txcb + TxCB_LINK, 
2502                          HAL_CTOLE32(VIRT_TO_BUS((cyg_uint32)p_txcb)));
2503         WRITEMEM32(p_txcb + TxCB_TBD_ADDR,
2504                          HAL_CTOLE32(0xFFFFFFFF));
2505         WRITEMEM8(p_txcb + TxCB_TBD_NUMBER, 0);
2506         WRITEMEM8(p_txcb + TxCB_TX_THRESHOLD, 16);
2507         WRITEMEM16(p_txcb + TxCB_COUNT,
2508                          HAL_CTOLE16(TxCB_COUNT_EOF | total_len));
2509
2510         // Copy from the sglist into the txcb
2511         to_p = p_txcb + TxCB_BUFFER;
2512
2513         CYG_ASSERT( 0 < sg_len, "sg_len underflow" );
2514         CYG_ASSERT( MAX_ETH_DRV_SG >= sg_len, "sg_len overflow" );
2515
2516         for ( last_sg = &sg_list[sg_len]; sg_list < last_sg; sg_list++ ) {
2517             cyg_uint8 *from_p;
2518             int l;
2519             
2520             from_p = (cyg_uint8 *)(sg_list->buf);
2521             l = sg_list->len;
2522
2523             if ( l > total_len )
2524                 l = total_len;
2525
2526             memcpy( (unsigned char *)to_p, from_p, l );
2527             to_p += l;
2528             total_len -= l;
2529
2530             if ( 0 > total_len ) 
2531                 break; // Should exit via sg_last normally
2532         }
2533
2534         CYG_ASSERT( 0 == total_len, "length mismatch in tx" );
2535         CYG_ASSERT( last_sg == sg_list, "sg count mismatch in tx" );
2536         CYG_ASSERT( p_txcb + TxCB_BUFFER < to_p, "to_p wild in tx" );
2537         CYG_ASSERT( p_txcb + TxCB_BUFFER + MAX_TX_PACKET_SIZE >= to_p,
2538                     "to_p overflow in tx" );
2539   
2540         // Next descriptor
2541         if ( ++tx_descriptor_add >= MAX_TX_DESCRIPTORS)
2542             tx_descriptor_add = 0;
2543         p_i82559->tx_descriptor_add = tx_descriptor_add;
2544
2545         // From this instant, interrupts can advance the world and start,
2546         // even complete, this tx request...
2547
2548         if ( p_i82559->tx_descriptor_remove == tx_descriptor_add )
2549             p_i82559->tx_queue_full = 1;
2550     }
2551
2552     // Try advancing the Tx Machine regardless
2553
2554     // no more interrupts until started
2555     ints = Mask82559Interrupt(p_i82559);
2556
2557     // This helps unstick deadly embraces.
2558     CYG_ASSERT( p_i82559->within_send < 10, "send: Excess send recursions" );
2559     p_i82559->within_send++;
2560
2561     // Check that either:
2562     //     tx is already active, there is other stuff queued,
2563     // OR  this tx just added is the current active one
2564     // OR  this tx just added is already complete
2565     CYG_ASSERT(
2566         // The machine is busy:
2567         (p_i82559->tx_in_progress == 1) ||
2568         // or: The machine is idle and this just added is the next one
2569         (((p_i82559->tx_descriptor_add-1) == p_i82559->tx_descriptor_active)
2570          || ((0 == p_i82559->tx_descriptor_add) &&
2571              ((MAX_TX_DESCRIPTORS-1) == p_i82559->tx_descriptor_active))) ||
2572         // or: This tx is already complete
2573         (p_i82559->tx_descriptor_add == p_i82559->tx_descriptor_active),
2574                 "Active/add mismatch" );
2575
2576 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_DEMUX_ALL
2577     {
2578         int i;
2579         // The problem is, if DEMUX_ALL, either device can eat the other's
2580         // interrupts; so we must poll them *both*:
2581         for (i = 0; i < CYGNUM_DEVS_ETH_INTEL_I82559_DEV_COUNT; i++) {
2582             p_i82559 = i82559_priv_array[i];
2583             if ( p_i82559->active ) {
2584                 TxMachine(p_i82559);
2585                 Acknowledge82559Interrupt(p_i82559);
2586 #ifndef CYGPKG_IO_ETH_DRIVERS_STAND_ALONE
2587                 // We are not prepared to receive a packet now if we are in a polled
2588                 // standalone configuration.
2589                 PacketRxReady(p_i82559);
2590 #endif
2591             }
2592         }
2593     }
2594     // ensure we look at the correct device at the end
2595     p_i82559 = (struct i82559 *)sc->driver_private;
2596 #else // no CYGHWR_DEVS_ETH_INTEL_I82559_DEMUX_ALL
2597     // Advance TxMachine atomically
2598     TxMachine(p_i82559);
2599     Acknowledge82559Interrupt(p_i82559); // This can eat an Rx interrupt, so
2600 #ifndef CYGPKG_IO_ETH_DRIVERS_STAND_ALONE
2601     // We are not prepared to receive a packet now if we are in a polled
2602     // standalone configuration.
2603     PacketRxReady(p_i82559);
2604 #endif
2605 #endif // no CYGHWR_DEVS_ETH_INTEL_I82559_DEMUX_ALL
2606
2607     p_i82559->within_send--;
2608     UnMask82559Interrupt(p_i82559, ints);
2609 }
2610
2611 // ------------------------------------------------------------------------
2612 //
2613 //  Function : i82559_reset
2614 //
2615 // ------------------------------------------------------------------------
2616 static void
2617 i82559_reset(struct i82559* p_i82559)
2618 {
2619     cyg_uint32 ioaddr = p_i82559->io_address;
2620
2621     // First do soft reset. This must be done before the hard reset,
2622     // otherwise we may create havoc on the PCI bus.
2623     // Wait 20 uSecs afterwards for chip to settle.
2624     OUTL(I82559_SELECTIVE_RESET, ioaddr + SCBPort);
2625     udelay(20);
2626   
2627     // Do hard reset now that the controller is off the PCI bus.
2628     // Wait 20 uSecs afterwards for chip to settle.
2629     OUTL(I82559_RESET, ioaddr + SCBPort);
2630     udelay(20);
2631
2632     // Set the base addresses
2633     wait_for_cmd_done(ioaddr, WAIT_RU);
2634     OUTL(0, ioaddr + SCBPointer);
2635     OUTW(SCB_M | RUC_ADDR_LOAD, ioaddr + SCBCmd);
2636
2637     wait_for_cmd_done(ioaddr, WAIT_CU);
2638     OUTL(0, ioaddr + SCBPointer);
2639     OUTW(SCB_M | CU_ADDR_LOAD, ioaddr + SCBCmd);
2640 }
2641
2642 // ------------------------------------------------------------------------
2643 //
2644 //                       INTERRUPT HANDLERS
2645 //
2646 // ------------------------------------------------------------------------
2647
2648 static cyg_uint32
2649 eth_isr(cyg_vector_t vector, cyg_addrword_t data)
2650 {
2651     struct i82559* p_i82559 = (struct i82559 *)data;
2652     cyg_uint16 status;
2653     cyg_uint32 ioaddr;
2654
2655     IF_BAD_82559( p_i82559 ) {
2656 #ifdef DEBUG
2657         os_printf( "i82559_isr: Bad device pointer %x\n", p_i82559 );
2658 #endif
2659         return 0;
2660     }
2661
2662     ioaddr = p_i82559->io_address;
2663     status = INW(ioaddr + SCBStatus);
2664
2665     // Acknowledge all INT sources that were active
2666     OUTW( status & SCB_INTACK_MASK, ioaddr + SCBStatus);
2667     // (see pages 6-10 & 6-90)
2668
2669 #ifdef KEEP_STATISTICS
2670     statistics[p_i82559->index].interrupts++;
2671
2672     // receiver left ready state ?
2673     if ( status & SCB_STATUS_RNR )
2674         statistics[p_i82559->index].rx_resource++;
2675
2676     // frame receive interrupt ?
2677     if ( status & SCB_STATUS_FR )
2678         statistics[p_i82559->index].rx_count++;
2679
2680     // transmit interrupt ?
2681     if ( status & SCB_STATUS_CX )
2682         statistics[p_i82559->index].tx_complete++;
2683 #endif
2684
2685     // Advance the Tx Machine regardless
2686     TxMachine(p_i82559);
2687
2688     // it should have settled down now...
2689     Acknowledge82559Interrupt(p_i82559);
2690
2691     return CYG_ISR_CALL_DSR;        // schedule DSR
2692 }
2693
2694
2695 // ------------------------------------------------------------------------
2696 #if defined( CYGNUM_DEVS_ETH_INTEL_I82559_SEPARATE_MUX_INTERRUPT ) || \
2697     defined( CYGHWR_DEVS_ETH_INTEL_I82559_DEMUX_ALL )
2698 static cyg_uint32
2699 eth_mux_isr(cyg_vector_t vector, cyg_addrword_t data)
2700 {
2701     int i;
2702     struct i82559* p_i82559;
2703
2704     for (i = 0; i < CYGNUM_DEVS_ETH_INTEL_I82559_DEV_COUNT; i++) {
2705         p_i82559 = i82559_priv_array[i];
2706         if ( p_i82559->active )
2707             (void)eth_isr( vector, (cyg_addrword_t)p_i82559 );
2708     }
2709
2710     return CYG_ISR_CALL_DSR;
2711 }
2712 #endif
2713
2714 // ------------------------------------------------------------------------
2715
2716 #if defined(CYGHWR_DEVS_ETH_INTEL_I82559_MISSED_INTERRUPT) || \
2717   defined(CYGNUM_DEVS_ETH_INTEL_I82559_SEPARATE_MUX_INTERRUPT) || \
2718   !defined(CYGPKG_IO_ETH_DRIVERS_STAND_ALONE)
2719 static void
2720 eth_dsr(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
2721 {
2722     // This conditioning out is necessary because of explicit calls to this
2723     // DSR - which would not ever be called in the case of a polled mode
2724     // usage ie. in RedBoot.
2725 #ifdef CYGINT_IO_ETH_INT_SUPPORT_REQUIRED
2726     struct i82559* p_i82559 = (struct i82559 *)data;
2727     struct cyg_netdevtab_entry *ndp =
2728         (struct cyg_netdevtab_entry *)(p_i82559->ndp);
2729     struct eth_drv_sc *sc = (struct eth_drv_sc *)(ndp->device_instance);
2730
2731     // but here, it must be a *sc:
2732     eth_drv_dsr( vector, count, (cyg_addrword_t)sc );
2733 #else
2734 # ifndef CYGPKG_REDBOOT
2735 #  error Empty i82559 ethernet DSR is compiled.  Is this what you want?
2736 # endif
2737 #endif
2738 }
2739 #endif
2740
2741 // ------------------------------------------------------------------------
2742 // Deliver routine:
2743 #if defined( CYGNUM_DEVS_ETH_INTEL_I82559_SEPARATE_MUX_INTERRUPT ) || \
2744     defined( CYGHWR_DEVS_ETH_INTEL_I82559_DEMUX_ALL )
2745
2746 void
2747 i82559_deliver(struct eth_drv_sc *sc)
2748 {
2749     int i;
2750     struct i82559* p_i82559;
2751
2752 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_MISSED_INTERRUPT
2753     again:
2754 #endif
2755
2756     // Since this must mux all devices, the incoming arg is ignored.
2757     for (i = 0; i < CYGNUM_DEVS_ETH_INTEL_I82559_DEV_COUNT; i++) {
2758         p_i82559 = i82559_priv_array[i];
2759         if ( p_i82559->active ) {
2760
2761             // See if the Tx machine is wedged - reset if so:
2762             Check82559TxLockupTimeout(p_i82559);
2763
2764             // First pass any rx data up the stack
2765             PacketRxReady(p_i82559);
2766
2767             // Then scan for completed Txen and inform the stack
2768             TxDone(p_i82559);
2769         }
2770     }
2771
2772 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_MISSED_INTERRUPT
2773     {
2774         int retry = 0;
2775         for (i = 0; i < CYGNUM_DEVS_ETH_INTEL_I82559_DEV_COUNT; i++) {
2776             p_i82559 = i82559_priv_array[i];
2777             if ( p_i82559->active ) {
2778                 if ( CYGHWR_DEVS_ETH_INTEL_I82559_MISSED_INTERRUPT(p_i82559) ) {
2779                     int ints = Mask82559Interrupt(p_i82559);
2780                     retry++;
2781 #ifdef CYGDBG_USE_ASSERTS
2782                     missed_interrupt.deliver++;
2783 #endif
2784                     eth_isr( p_i82559->vector, (cyg_addrword_t)p_i82559 );
2785                     UnMask82559Interrupt(p_i82559,ints);
2786                 }
2787             }
2788             
2789         }
2790         if ( retry )
2791             goto again;
2792     }
2793 #endif
2794 }
2795
2796 #else // no CYGHWR_DEVS_ETH_INTEL_I82559_DEMUX_ALL : Simplex version:
2797
2798 void
2799 i82559_deliver(struct eth_drv_sc *sc)
2800 {
2801     struct i82559* p_i82559 = (struct i82559 *)(sc->driver_private);
2802
2803 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_MISSED_INTERRUPT
2804  again:
2805 #endif
2806     // See if the Tx machine is wedged - reset if so:
2807     Check82559TxLockupTimeout(p_i82559);
2808
2809     // First pass any rx data up the stack
2810     PacketRxReady(p_i82559);
2811
2812     // Then scan for completed Txen and inform the stack
2813     TxDone(p_i82559);
2814
2815 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_MISSED_INTERRUPT
2816     if ( CYGHWR_DEVS_ETH_INTEL_I82559_MISSED_INTERRUPT(p_i82559) ) {
2817         int ints = Mask82559Interrupt(p_i82559);
2818 #ifdef CYGDBG_USE_ASSERTS
2819         missed_interrupt.deliver++;
2820 #endif
2821         eth_isr( p_i82559->vector, (cyg_addrword_t)p_i82559 );
2822         UnMask82559Interrupt(p_i82559,ints);
2823         goto again;
2824     }
2825 #endif
2826 }
2827
2828 #endif // no CYGHWR_DEVS_ETH_INTEL_I82559_DEMUX_ALL
2829
2830 // ------------------------------------------------------------------------
2831 // Device table entry to operate the chip in a polled mode.
2832 // Only diddle the interface we were asked to!
2833
2834 void
2835 i82559_poll(struct eth_drv_sc *sc)
2836 {
2837     struct i82559 *p_i82559;
2838     int ints;
2839     p_i82559 = (struct i82559 *)sc->driver_private;
2840     
2841     IF_BAD_82559( p_i82559 ) {
2842 #ifdef DEBUG
2843         os_printf( "i82559_poll: Bad device pointer %x\n", p_i82559 );
2844 #endif
2845         return;
2846     }
2847
2848     // Do these atomically
2849     ints = Mask82559Interrupt(p_i82559);
2850
2851     // As it happens, this driver always requests the DSR to be called:
2852     (void)eth_isr( p_i82559->vector, (cyg_addrword_t)p_i82559 );
2853
2854     // (no harm in calling this ints-off also, when polled)
2855     i82559_deliver( sc );
2856
2857     Acknowledge82559Interrupt(p_i82559);
2858     UnMask82559Interrupt(p_i82559, ints);
2859 }
2860
2861 // ------------------------------------------------------------------------
2862 // Determine interrupt vector used by a device - for attaching GDB stubs
2863 // packet handler.
2864 int
2865 i82559_int_vector(struct eth_drv_sc *sc)
2866 {
2867     struct i82559 *p_i82559;
2868     p_i82559 = (struct i82559 *)sc->driver_private;
2869     return (p_i82559->vector);
2870 }
2871
2872 #if 0
2873 int
2874 i82559_int_op( struct eth_drv_sc *sc, int mask)
2875 {
2876     struct i82559 *p_i82559;
2877     p_i82559 = (struct i82559 *)sc->driver_private;
2878
2879     if ( 1 == mask )
2880         return Mask82559Interrupt( p_i82559 );
2881
2882     if ( 0 == mask )
2883         UnMask82559Interrupt( p_i82559, 0x0fffffff ); // enable all
2884
2885     return 0;
2886 }
2887 #endif
2888     
2889
2890 // ------------------------------------------------------------------------
2891 //
2892 //  Function : pci_init_find_82559s
2893 //
2894 // This is called exactly once at the start of time to:
2895 //  o scan the PCI bus for objects
2896 //  o record them in the device table
2897 //  o acquire all the info needed for the driver to access them
2898 //  o instantiate interrupts for them
2899 //  o attach those interrupts appropriately
2900 // ------------------------------------------------------------------------
2901 static cyg_pci_match_func find_82559s_match_func;
2902
2903 // Intel 82559 and 82557 are virtually identical, with different
2904 // dev codes; also 82559ER (cutdown) = 0x1209.
2905 static cyg_bool
2906 find_82559s_match_func( cyg_uint16 v, cyg_uint16 d, cyg_uint32 c, void *p )
2907 {
2908     return
2909         (0x8086 == v) &&
2910         ((0x1030 == d) ||
2911          (0x1229 == d) ||
2912          (0x1209 == d) ||
2913          (0x1029 == d) ||
2914          (0x2449 == d));
2915 }
2916
2917 static int
2918 pci_init_find_82559s( void )
2919 {
2920     cyg_pci_device_id devid;
2921     cyg_pci_device dev_info;
2922     cyg_uint16 cmd;
2923     int device_index;
2924     int found_devices = 0;
2925
2926 #ifdef CYGNUM_DEVS_ETH_INTEL_I82559_SEPARATE_MUX_INTERRUPT
2927     static cyg_handle_t mux_interrupt_handle = 0;
2928  #endif
2929
2930 #ifdef DEBUG
2931     db_printf("pci_init_find_82559s()\n");
2932 #endif
2933
2934     // allocate memory to be used in ioctls later
2935     if (mem_reserved_ioctl != (void*)0) {
2936 #ifdef DEBUG
2937         db_printf("pci_init_find_82559s() called > once\n");
2938 #endif
2939         return 0;
2940     }
2941
2942     // First initialize the heap in PCI window'd memory
2943     i82559_heap_size = CYGHWR_INTEL_I82559_PCI_MEM_MAP_SIZE;
2944     i82559_heap_base = (cyg_uint8 *)CYGHWR_INTEL_I82559_PCI_MEM_MAP_BASE;
2945     i82559_heap_free = i82559_heap_base;
2946
2947     mem_reserved_ioctl = pciwindow_mem_alloc(MAX_MEM_RESERVED_IOCTL);     
2948
2949     cyg_pci_init();
2950 #ifdef DEBUG
2951     db_printf("Finished cyg_pci_init();\n");
2952 #endif
2953
2954     devid = CYG_PCI_NULL_DEVID;
2955
2956     for (device_index = 0; 
2957          device_index < CYGNUM_DEVS_ETH_INTEL_I82559_DEV_COUNT;
2958          device_index++) {
2959         struct i82559 *p_i82559 = i82559_priv_array[device_index];
2960
2961         p_i82559->index = device_index;
2962
2963         // See above for find_82559s_match_func - it selects any of several
2964         // variants.  This is necessary in case we have multiple mixed-type
2965         // devices on one board in arbitrary orders.
2966         if (cyg_pci_find_matching( &find_82559s_match_func, NULL, &devid )) {
2967 #ifdef DEBUG
2968             db_printf("eth%d = 82559\n", device_index);
2969 #endif
2970             // Allocate it a stats window:
2971             p_i82559->p_statistics = pciwindow_mem_alloc(sizeof(I82559_COUNTERS));
2972
2973             cyg_pci_get_device_info(devid, &dev_info);
2974
2975             p_i82559->interrupt_handle = 0; // Flag not attached.
2976             if (cyg_pci_translate_interrupt(&dev_info, &p_i82559->vector)) {
2977 #ifdef DEBUG
2978                 db_printf(" Wired to HAL vector %d\n", p_i82559->vector);
2979 #endif
2980
2981 #ifndef CYGPKG_IO_ETH_DRIVERS_STAND_ALONE
2982                 cyg_drv_interrupt_create(
2983                     p_i82559->vector,
2984                     0,                  // Priority - unused
2985                     (CYG_ADDRWORD)p_i82559, // Data item passed to ISR & DSR
2986 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_DEMUX_ALL
2987                     eth_mux_isr,        // ISR
2988 #else
2989                     eth_isr,            // ISR
2990 #endif
2991                     eth_dsr,            // DSR
2992                     &p_i82559->interrupt_handle, // handle to intr obj
2993                     &p_i82559->interrupt_object ); // space for int obj
2994
2995                 cyg_drv_interrupt_attach(p_i82559->interrupt_handle);
2996
2997                 // Don't unmask the interrupt yet, that could get us into a
2998                 // race.
2999 #ifdef CYGNUM_DEVS_ETH_INTEL_I82559_SEPARATE_MUX_INTERRUPT
3000                 // ALSO attach it to MUX interrupt for multiplexed
3001                 // interrupts.  This is for certain boards where the
3002                 // PCI backplane is wired "straight through" instead of
3003                 // with a rotation of interrupt lines in the different
3004                 // slots.
3005                 {
3006                     static cyg_interrupt mux_interrupt_object;
3007
3008                     if ( ! mux_interrupt_handle ) {
3009 #ifdef DEBUG
3010                         db_printf(" Also attaching to HAL vector %d\n", 
3011                                   CYGNUM_DEVS_ETH_INTEL_I82559_SEPARATE_MUX_INTERRUPT);
3012 #endif
3013                         cyg_drv_interrupt_create(
3014                             CYGNUM_DEVS_ETH_INTEL_I82559_SEPARATE_MUX_INTERRUPT,
3015                             0,              // Priority - unused
3016                             (CYG_ADDRWORD)p_i82559,// Data item passed to ISR and DSR
3017                             eth_mux_isr,    // ISR
3018                             eth_dsr,        // DSR
3019                             &mux_interrupt_handle,
3020                             &mux_interrupt_object );
3021                         
3022                         cyg_drv_interrupt_attach(mux_interrupt_handle);
3023                     }
3024                 }
3025 #endif // CYGNUM_DEVS_ETH_INTEL_I82559_SEPARATE_MUX_INTERRUPT
3026 #endif // !CYGPKG_IO_ETH_DRIVERS_STAND_ALONE
3027             }
3028             else {
3029                 p_i82559->vector=0;
3030 #ifdef DEBUG
3031                 db_printf(" Does not generate interrupts.\n");
3032 #endif
3033             }
3034
3035             if (cyg_pci_configure_device(&dev_info)) {
3036 #ifdef DEBUG
3037                 int i;
3038                 db_printf("Found device on bus %d, devfn 0x%02x:\n",
3039                           CYG_PCI_DEV_GET_BUS(devid),
3040                           CYG_PCI_DEV_GET_DEVFN(devid));
3041
3042                 if (dev_info.command & CYG_PCI_CFG_COMMAND_ACTIVE) {
3043                     db_printf(" Note that board is active. Probed"
3044                               " sizes and CPU addresses invalid!\n");
3045                 }
3046                 db_printf(" Vendor    0x%04x", dev_info.vendor);
3047                 db_printf("\n Device    0x%04x", dev_info.device);
3048                 db_printf("\n Command   0x%04x, Status 0x%04x\n",
3049                           dev_info.command, dev_info.status);
3050                 
3051                 db_printf(" Class/Rev 0x%08x", dev_info.class_rev);
3052                 db_printf("\n Header 0x%02x\n", dev_info.header_type);
3053
3054                 db_printf(" SubVendor 0x%04x, Sub ID 0x%04x\n",
3055                           dev_info.header.normal.sub_vendor, 
3056                           dev_info.header.normal.sub_id);
3057
3058                 for(i = 0; i < CYG_PCI_MAX_BAR; i++) {
3059                     db_printf(" BAR[%d]    0x%08x /", i, dev_info.base_address[i]);
3060                     db_printf(" probed size 0x%08x / CPU addr 0x%08x\n",
3061                               dev_info.base_size[i], dev_info.base_map[i]);
3062                 }
3063                 db_printf(" eth%d configured\n", device_index);
3064 #endif
3065                 found_devices++;
3066                 p_i82559->found = 1;
3067                 p_i82559->active = 0;
3068                 p_i82559->devid = devid;
3069                 p_i82559->memory_address = dev_info.base_map[0];
3070                 p_i82559->io_address = dev_info.base_map[1];
3071 #ifdef DEBUG
3072                 db_printf(" memory address = 0x%08x\n", dev_info.base_map[0]);
3073                 db_printf(" I/O address = 0x%08x\n", dev_info.base_map[1]);
3074 #endif
3075 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_USE_MEMORY
3076                 // Use the memory address instead of I/O.  Some devices just
3077                 // don't want to talk using the I/O registers :-(
3078                 p_i82559->io_address = dev_info.base_map[0];
3079 #endif
3080
3081                 // Don't use cyg_pci_set_device_info since it clears
3082                 // some of the fields we want to print out below.
3083                 cyg_pci_read_config_uint16(dev_info.devid,
3084                                            CYG_PCI_CFG_COMMAND, &cmd);
3085                 cmd |= (CYG_PCI_CFG_COMMAND_IO         // enable I/O space
3086                         | CYG_PCI_CFG_COMMAND_MEMORY   // enable memory space
3087                         | CYG_PCI_CFG_COMMAND_MASTER); // enable bus master
3088                 cyg_pci_write_config_uint16(dev_info.devid,
3089                                             CYG_PCI_CFG_COMMAND, cmd);
3090
3091                 // Now the PCI part of the device is configured, reset
3092                 // it. This should make it safe to enable the
3093                 // interrupt
3094                 i82559_reset(p_i82559);
3095
3096                 // This is the indicator for "uses an interrupt"
3097                 if (p_i82559->interrupt_handle != 0) {
3098                     cyg_drv_interrupt_acknowledge(p_i82559->vector);
3099 #ifndef CYGPKG_IO_ETH_DRIVERS_STAND_ALONE
3100                     cyg_drv_interrupt_unmask(p_i82559->vector);
3101 #endif // !CYGPKG_IO_ETH_DRIVERS_STAND_ALONE
3102                 }
3103 #ifdef DEBUG
3104                 db_printf(" **** Device enabled for I/O and Memory "
3105                           "and Bus Master\n");
3106 #endif
3107             }
3108             else {
3109                 p_i82559->found = 0;
3110                 p_i82559->active = 0;
3111 #ifdef DEBUG
3112                 db_printf("Failed to configure device %d\n",device_index);
3113 #endif
3114             }
3115         }
3116         else {
3117             p_i82559->found = 0;
3118             p_i82559->active = 0;
3119 #ifdef DEBUG
3120             db_printf("eth%d not found\n", device_index);
3121 #endif
3122         }
3123     }
3124
3125     if (0 == found_devices)
3126         return 0;
3127
3128 #ifdef CYGNUM_DEVS_ETH_INTEL_I82559_SEPARATE_MUX_INTERRUPT
3129     // Now enable the mux shared interrupt if it is in use
3130     if (mux_interrupt_handle) {
3131         cyg_drv_interrupt_acknowledge(CYGNUM_DEVS_ETH_INTEL_I82559_SEPARATE_MUX_INTERRUPT);
3132         cyg_drv_interrupt_unmask(CYGNUM_DEVS_ETH_INTEL_I82559_SEPARATE_MUX_INTERRUPT);
3133     }
3134 #endif
3135
3136     // Now a delay to ensure the hardware has "come up" before you try to
3137     // use it.  Yes, really, the full 2 seconds.  It's only really
3138     // necessary if DEBUG is off - otherwise all that printout wastes
3139     // enough time.  No kidding.
3140     udelay( 2000000 );
3141     return 1;
3142 }
3143
3144 // ------------------------------------------------------------------------
3145 //
3146 //  Function : i82559_set_multicast
3147 //
3148 // ------------------------------------------------------------------------
3149 #ifdef ETH_DRV_SET_MC_LIST
3150 static int i82559_set_multicast(struct i82559* p_i82559,
3151                                 int num_addrs,
3152                                 cyg_uint8 *address_list )
3153 {
3154     cyg_uint32  ioaddr;
3155     volatile CFG *ccs;
3156     volatile cyg_uint8* config_bytes;
3157     cyg_uint16 status;
3158     int count;
3159     int i;
3160
3161     IF_BAD_82559( p_i82559 ) {
3162 #ifdef DEBUG
3163         os_printf( "eth_set_promiscuos_mode: Bad device pointer %x\n",
3164                    p_i82559 );
3165 #endif
3166         return -1;
3167     }
3168
3169     ioaddr = p_i82559->io_address;  
3170     wait_for_cmd_done(ioaddr, WAIT_CU); 
3171     // load cu base address = 0 */ 
3172     OUTL(0, ioaddr + SCBPointer);         
3173     // 32 bit linear addressing used
3174     OUTW(SCB_M | CU_ADDR_LOAD, ioaddr + SCBCmd);
3175     // wait for SCB command complete
3176     wait_for_cmd_done(ioaddr, WAIT_CU);   
3177   
3178     // Check the malloc we did earlier worked
3179     ccs = (CFG *)mem_reserved_ioctl;
3180     if (ccs == (void*)0) 
3181         return 2; // Failed
3182
3183     // Prepare header
3184     WRITEMEM16(ccs + CFG_CMD, 
3185                      (CFG_CMD_EL | CFG_CMD_SUSPEND | CFG_CMD_MULTICAST));
3186     WRITEMEM16(ccs + CFG_STATUS, 0);
3187     WRITEMEM32(ccs + CFG_CB_LINK_OFFSET, 
3188                      HAL_CTOLE32(VIRT_TO_BUS((cyg_uint32)ccs)));
3189
3190     count = 6 * num_addrs; // byte count
3191
3192     WRITEMEM16(ccs + CFG_MC_LIST_BYTES,
3193                      HAL_CTOLE16( count ) );
3194                
3195     config_bytes = ccs + CFG_MC_LIST_DATA;
3196     
3197     for ( i = 0; i < count; i++ )
3198         config_bytes[i] = address_list[i];
3199     
3200     // Let chip read configuration
3201     wait_for_cmd_done(ioaddr, WAIT_CU);
3202
3203     OUTL(VIRT_TO_BUS(ccs), ioaddr + SCBPointer);
3204     OUTW(SCB_M | CU_START, ioaddr + SCBCmd);
3205
3206     // ...and wait for it to complete operation
3207     count = 10000;
3208     do {
3209       udelay(1);
3210       READMEM16(ccs + CFG_STATUS, status);
3211     } while (0 == (status & CFG_STATUS_C) && (count-- > 0));
3212
3213     // Check status
3214     if ((status & (CFG_STATUS_C | CFG_STATUS_OK)) 
3215         != (CFG_STATUS_C | CFG_STATUS_OK)) {
3216         // Failed!
3217 #ifdef DEBUG
3218         os_printf("%s:%d Multicast setup failed\n", __FUNCTION__, __LINE__);
3219 #endif
3220         return 1;
3221     }
3222
3223     wait_for_cmd_done(ioaddr, WAIT_CU);
3224     /* load pointer to Rx Ring */
3225     
3226     OUTL(VIRT_TO_BUS(p_i82559->rx_ring[0]), ioaddr + SCBPointer);
3227     OUTW(RUC_START, ioaddr + SCBCmd);
3228
3229     return 0;
3230 }
3231 #endif // ETH_DRV_SET_MC_ALL
3232
3233 // ------------------------------------------------------------------------
3234 //
3235 //  Function : i82559_configure
3236 //
3237 //  Return : 0 = It worked.
3238 //           non0 = It failed.
3239 // ------------------------------------------------------------------------
3240
3241 static int i82559_configure(struct i82559* p_i82559, int promisc,
3242                             int oversized, int multicast_all)
3243 {
3244     cyg_uint32  ioaddr;
3245     volatile CFG *ccs;
3246     volatile cyg_uint8* config_bytes;
3247     cyg_uint16 status;
3248     int count;
3249
3250     IF_BAD_82559( p_i82559 ) {
3251 #ifdef DEBUG
3252         os_printf( "eth_set_promiscuos_mode: Bad device pointer %x\n",
3253                    p_i82559 );
3254 #endif
3255         return -1;
3256     }
3257
3258     ioaddr = p_i82559->io_address;  
3259     wait_for_cmd_done(ioaddr, WAIT_CU); 
3260     // load cu base address = 0 */ 
3261     OUTL(0, ioaddr + SCBPointer);         
3262     // 32 bit linear addressing used
3263     OUTW(SCB_M | CU_ADDR_LOAD, ioaddr + SCBCmd);
3264     // wait for SCB command complete
3265     wait_for_cmd_done(ioaddr, WAIT_CU);   
3266   
3267     // Check the malloc we did earlier worked
3268     ccs = (CFG *)mem_reserved_ioctl;
3269     if (ccs == (void*)0) 
3270         return 2; // Failed
3271
3272     // Prepare header
3273     WRITEMEM16(ccs + CFG_CMD, 
3274                      (CFG_CMD_EL | CFG_CMD_SUSPEND | CFG_CMD_CONFIGURE));
3275     WRITEMEM16(ccs + CFG_STATUS, 0);
3276     WRITEMEM32(ccs + CFG_CB_LINK_OFFSET, 
3277                      HAL_CTOLE32(VIRT_TO_BUS((cyg_uint32)ccs)));
3278
3279     // Default values from the Intel Manual
3280     config_bytes = ccs + CFG_BYTES;
3281
3282     config_bytes[0]= 22; // All 22 bytes
3283     config_bytes[1]=0xc;
3284     config_bytes[2]=0x0;
3285     config_bytes[3]=0x0;
3286     config_bytes[4]=0x0;
3287     config_bytes[5]=0x0;
3288     config_bytes[6]=0x32 | (promisc ? 0x80 : 0x00); //   | 0x32 for small stats,
3289     config_bytes[7]=0x00 | (promisc ? 0x00 : 0x01); //\  | 0x12 for stats with PAUSE stats
3290     config_bytes[8]=0x01; // [7]:discard short frames  \ | 0x16 for PAUSE + TCO stats
3291     config_bytes[9]=0x0;
3292     config_bytes[10]=0x28;
3293     config_bytes[11]=0x0;
3294     config_bytes[12]=0x60;
3295     config_bytes[13]=0x0;          // arp
3296     config_bytes[14]=0x0;          // arp
3297     
3298     config_bytes[15]=0x80 | (promisc ? 1 : 0); // 0x81: promiscuous mode set
3299                                                // 0x80: normal mode
3300     config_bytes[16]=0x0;
3301     config_bytes[17]=0x40;
3302     config_bytes[18]=0x72 | (oversized ? 8 : 0); // Keep the Padding Enable bit
3303
3304     config_bytes[19]=0x80; // FDX pin enable is the default
3305     config_bytes[20]=0x3f; // the default
3306     config_bytes[21]=0x05 | (multicast_all ? 8 : 0); // Bit 3 is MultiCastALL enable
3307     
3308     // Let chip read configuration
3309     wait_for_cmd_done(ioaddr, WAIT_CU);
3310
3311     OUTL(VIRT_TO_BUS(ccs), ioaddr + SCBPointer);
3312     OUTW(SCB_M | CU_START, ioaddr + SCBCmd);
3313
3314     // ...and wait for it to complete operation
3315     count = 10000;
3316     do {
3317       udelay(1);
3318       READMEM16(ccs + CFG_STATUS, status);
3319     } while (0 == (status & CFG_STATUS_C) && (count-- > 0));
3320
3321     // Check status
3322     if ((status & (CFG_STATUS_C | CFG_STATUS_OK)) 
3323         != (CFG_STATUS_C | CFG_STATUS_OK)) {
3324         // Failed!
3325 #ifdef DEBUG
3326         os_printf("%s:%d Config update failed\n", __FUNCTION__, __LINE__);
3327 #endif
3328         return 1;
3329     }
3330
3331     wait_for_cmd_done(ioaddr, WAIT_CU);
3332     /* load pointer to Rx Ring */
3333     
3334     OUTL(VIRT_TO_BUS(p_i82559->rx_ring[0]), ioaddr + SCBPointer);
3335     OUTW(RUC_START, ioaddr + SCBCmd);
3336
3337     return 0;
3338 }
3339
3340 // ------------------------------------------------------------------------
3341 #if 0
3342 // The following table below doesn't really work with all cards.
3343 // The safest way to proceed is to do a read-modify-write of the
3344 // EEPROM contents rather than relying on a static table.
3345
3346 #ifdef CYGPKG_DEVS_ETH_INTEL_I82559_WRITE_EEPROM
3347 // We use this as a templete when writing a new MAC address into the
3348 // eeproms. The MAC address in the first few bytes is over written
3349 // with the correct MAC address and then the whole lot is programmed
3350 // into the serial EEPROM. The checksum is calculated on the fly and
3351 // sent instead of the last two bytes.
3352 // The values are copied from the Intel EtherPro10/100+ &c devices
3353 // in the EBSA boards.
3354 static cyg_uint16 eeprom_burn[64] = { 
3355 /* halfword addresses! */
3356 /*  0: */  0x9000, 0x8c27, 0x8257, 0x0203,
3357 /*  4: */  0x0000, 0x0201, 0x4701, 0x0000,
3358 /*  8: */  0x7213, 0x8306, 0x40A2, 0x000c,
3359 /*  C: */  0x8086, 0x0000, 0x0000, 0x0000,
3360 /* 10: */  0x0000, 0x0000, 0x0000, 0x0000,
3361 /* 14: */  0x0000, 0x0000, 0x0000, 0x0000,
3362 /* 18: */  0x0000, 0x0000, 0x0000, 0x0000,
3363 /* 1C: */  0x0000, 0x0000, 0x0000, 0x0000,
3364 /* 20: */  0x0000, 0x0000, 0x0000, 0x0000,
3365 /* 24: */  0x0000, 0x0000, 0x0000, 0x0000,
3366 /* 28: */  0x0000, 0x0000, 0x0000, 0x0000,
3367 /* 2C: */  0x0000, 0x0000, 0x0000, 0x0000,
3368 /* 30: */  0x0128, 0x0000, 0x0000, 0x0000,
3369 /* 34: */  0x0000, 0x0000, 0x0000, 0x0000,
3370 /* 38: */  0x0000, 0x0000, 0x0000, 0x0000,
3371 /* 3C: */  0x0000, 0x0000, 0x0000, 0x0000,
3372 };
3373 #endif
3374 #endif
3375
3376 // ------------------------------------------------------------------------
3377 //
3378 //  Function : eth_set_mac_address
3379 //
3380 //  Return : 0 = It worked.
3381 //           non0 = It failed.
3382 // ------------------------------------------------------------------------
3383 static int
3384 eth_set_mac_address(struct i82559* p_i82559, cyg_uint8 *addr, int eeprom)
3385 {
3386     cyg_uint32  ioaddr;
3387     cyg_uint16 status;
3388     volatile CFG *ccs;
3389     volatile cyg_uint8* config_bytes;
3390     int count;
3391
3392     IF_BAD_82559( p_i82559 ) {
3393 #ifdef DEBUG
3394         os_printf( "eth_set_mac_address : Bad device pointer %x\n",
3395                    p_i82559 );
3396 #endif
3397         return -1;
3398     }
3399
3400     ioaddr = p_i82559->io_address;      
3401     
3402     ccs = (CFG *)mem_reserved_ioctl;
3403     if (ccs == (void*)0)
3404         return 2;
3405
3406     WRITEMEM16(ccs + CFG_CMD, 
3407                      (CFG_CMD_EL | CFG_CMD_SUSPEND | CFG_CMD_IAS));
3408     WRITEMEM16(ccs + CFG_STATUS, 0);
3409     WRITEMEM32(ccs + CFG_CB_LINK_OFFSET, 
3410                      HAL_CTOLE32(VIRT_TO_BUS((cyg_uint32)ccs)));
3411
3412     config_bytes = ccs + CFG_BYTES;
3413     memcpy((char *)(config_bytes),addr,6);
3414     config_bytes[6]=0x0;
3415     config_bytes[7]=0x0;
3416
3417     // Let chip read new ESA
3418     wait_for_cmd_done(ioaddr, WAIT_CU);
3419     OUTL(VIRT_TO_BUS(ccs), ioaddr + SCBPointer); 
3420     OUTW(SCB_M | CU_START, ioaddr + SCBCmd);    
3421
3422     // ...and wait for it to complete operation
3423     count = 1000;
3424     do {
3425       READMEM16(ccs + CFG_STATUS, status);
3426       udelay(1);
3427     } while (0 == (status & CFG_STATUS_C) && (count-- > 0));
3428
3429     // Check status
3430     READMEM16(ccs + CFG_STATUS, status);
3431     if ((status & (CFG_STATUS_C | CFG_STATUS_OK)) 
3432         != (CFG_STATUS_C | CFG_STATUS_OK)) {
3433 #ifdef DEBUG
3434         os_printf("%s:%d ESA update failed\n", __FUNCTION__, __LINE__);
3435 #endif
3436         return 3;
3437     }
3438
3439 #ifdef CYGPKG_DEVS_ETH_INTEL_I82559_WRITE_EEPROM
3440     if ( 0 == eeprom 
3441 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_HAS_ONE_EEPROM
3442     || CYGHWR_DEVS_ETH_INTEL_I82559_HAS_ONE_EEPROM != p_i82559->index
3443 #endif
3444         ) {
3445         // record the MAC address in the device structure
3446         p_i82559->mac_address[0] = addr[0];
3447         p_i82559->mac_address[1] = addr[1];
3448         p_i82559->mac_address[2] = addr[2];
3449         p_i82559->mac_address[3] = addr[3];
3450         p_i82559->mac_address[4] = addr[4];
3451         p_i82559->mac_address[5] = addr[5];
3452         p_i82559->mac_addr_ok = 1;
3453
3454 #ifdef DEBUG
3455         os_printf( "No EEPROM write: MAC Address = %02X %02X %02X %02X %02X %02X (ok %d)\n",
3456                    p_i82559->mac_address[0],
3457                    p_i82559->mac_address[1],
3458                    p_i82559->mac_address[2],
3459                    p_i82559->mac_address[3],
3460                    p_i82559->mac_address[4],
3461                    p_i82559->mac_address[5],
3462                    p_i82559->mac_addr_ok       );
3463 #endif
3464     } else {
3465         int checksum, i, count;
3466         // (this is the length of the *EEPROM*s address, not MAC address)
3467         int addr_length;
3468         cyg_uint16 eeprom_burn[64];
3469
3470         addr_length = get_eeprom_size( ioaddr );
3471
3472         for (i = 0; i < (1 << addr_length); i++)
3473             eeprom_burn[i] = read_eeprom( ioaddr, i, addr_length );
3474
3475         // now set this address in the device eeprom ....
3476         eeprom_burn[0] = addr[0] | (addr[1] << 8);
3477         eeprom_burn[1] = addr[2] | (addr[3] << 8);
3478         eeprom_burn[2] = addr[4] | (addr[5] << 8);
3479
3480         // No idea what these were for...
3481         // eeprom_burn[20] &= 0xfe;   
3482         // eeprom_burn[20] |= p_i82559->index;   
3483         
3484         program_eeprom( ioaddr, addr_length, eeprom_burn );
3485    
3486         // update 82559 driver data structure ...
3487         udelay( 100000 );
3488
3489         // by reading EEPROM to get the mac address back
3490         for (checksum = 0, i = 0, count = 0; count < (1 << addr_length); count++) {
3491             cyg_uint16 value;
3492             // read word from eeprom
3493             value = read_eeprom(ioaddr, count, addr_length);
3494             checksum += value;
3495             if (count < 3) {
3496                 p_i82559->mac_address[i++] = value & 0xFF;
3497                 p_i82559->mac_address[i++] = (value >> 8) & 0xFF;
3498             }
3499         }
3500     
3501 #ifdef DEBUG
3502         os_printf("eth_set_mac_address[WRITE_EEPROM]: MAC Address = %02X %02X %02X %02X %02X %02X\n",
3503                   p_i82559->mac_address[0], p_i82559->mac_address[1],
3504                   p_i82559->mac_address[2], p_i82559->mac_address[3],
3505                   p_i82559->mac_address[4], p_i82559->mac_address[5]);
3506 #endif
3507
3508         p_i82559->mac_addr_ok = 1;
3509
3510         for ( i = 0, count = 0; i < 6; i++ )
3511             if ( p_i82559->mac_address[i] != addr[i] )
3512                 count++;
3513
3514         if ( count ) {
3515 #ifdef DEBUG
3516             os_printf( "Warning: MAC Address read back wrong!  %d bytes differ.\n",
3517                        count );
3518 #endif
3519             p_i82559->mac_addr_ok = 0;
3520         }
3521
3522         // If the EEPROM checksum is wrong, the MAC address read from
3523         // the EEPROM is probably wrong as well. In that case, we
3524         // don't set mac_addr_ok.
3525         if ((checksum & 0xFFFF) != 0xBABA)  {
3526 #ifdef DEBUG
3527             os_printf( "Warning: Invalid EEPROM checksum %04X for device %d\n",
3528                        checksum, p_i82559->index);
3529 #endif
3530             p_i82559->mac_addr_ok = 0;
3531         }
3532     }
3533 #else // CYGPKG_DEVS_ETH_INTEL_I82559_WRITE_EEPROM
3534
3535     // record the MAC address in the device structure
3536     p_i82559->mac_address[0] = addr[0];
3537     p_i82559->mac_address[1] = addr[1];
3538     p_i82559->mac_address[2] = addr[2];
3539     p_i82559->mac_address[3] = addr[3];
3540     p_i82559->mac_address[4] = addr[4];
3541     p_i82559->mac_address[5] = addr[5];
3542     p_i82559->mac_addr_ok = 1;
3543
3544 #ifdef DEBUG
3545     os_printf( "Set MAC Address = %02X %02X %02X %02X %02X %02X (ok %d)\n",
3546                p_i82559->mac_address[0],
3547                p_i82559->mac_address[1],
3548                p_i82559->mac_address[2],
3549                p_i82559->mac_address[3],
3550                p_i82559->mac_address[4],
3551                p_i82559->mac_address[5],
3552                p_i82559->mac_addr_ok       );
3553 #endif
3554
3555 #endif // ! CYGPKG_DEVS_ETH_INTEL_I82559_WRITE_EEPROM
3556
3557     return p_i82559->mac_addr_ok ? 0 : 1;
3558 }
3559
3560 #ifdef CYGPKG_DEVS_ETH_INTEL_I82559_WRITE_EEPROM
3561 // ------------------------------------------------------------------------
3562 static void
3563 write_eeprom(long ioaddr, int location, int addr_len, unsigned short value)
3564 {
3565     int ee_addr = ioaddr + SCBeeprom;
3566     int write_cmd = location | EE_WRITE_CMD(addr_len); 
3567     int i;
3568     
3569     OUTW(EE_ENB & ~EE_CS, ee_addr);
3570     eeprom_delay( 100 );
3571     OUTW(EE_ENB, ee_addr);
3572     eeprom_delay( 100 );
3573
3574 //    os_printf("\n write_eeprom : write_cmd : %x",write_cmd);  
3575 //    os_printf("\n addr_len : %x  value : %x ",addr_len,value);  
3576
3577     /* Shift the write command bits out. */
3578     for (i = (addr_len+2); i >= 0; i--) {
3579         short dataval = (write_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
3580         OUTW(EE_ENB | dataval, ee_addr);
3581         eeprom_delay(100);
3582         OUTW(EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
3583         eeprom_delay(150);
3584     }
3585     OUTW(EE_ENB, ee_addr);
3586         
3587     for (i = 15; i >= 0; i--) {
3588         short dataval = (value & (1 << i)) ? EE_DATA_WRITE : 0;
3589         OUTW(EE_ENB | dataval, ee_addr);
3590         eeprom_delay(100);
3591         OUTW(EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
3592         eeprom_delay(150);
3593     }
3594
3595     /* Terminate the EEPROM access. */
3596     OUTW(EE_ENB & ~EE_CS, ee_addr);
3597     eeprom_delay(150000); // let the write take effect
3598 }
3599
3600 // ------------------------------------------------------------------------
3601 static int
3602 write_enable_eeprom(long ioaddr,  int addr_len)
3603 {
3604     int ee_addr = ioaddr + SCBeeprom;
3605     int write_en_cmd = EE_WRITE_EN_CMD(addr_len); 
3606     int i;
3607
3608     OUTW(EE_ENB & ~EE_CS, ee_addr);
3609     OUTW(EE_ENB, ee_addr);
3610
3611 #ifdef DEBUG_82559
3612     os_printf("write_en_cmd : %x",write_en_cmd);
3613 #endif
3614
3615     // Shift the wr/er enable command bits out.
3616     for (i = (addr_len+2); i >= 0; i--) {
3617         short dataval = (write_en_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
3618         OUTW(EE_ENB | dataval, ee_addr);
3619         eeprom_delay(100);
3620         OUTW(EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
3621         eeprom_delay(150);
3622     }
3623
3624     // Terminate the EEPROM access.
3625     OUTW(EE_ENB & ~EE_CS, ee_addr);
3626     eeprom_delay(EEPROM_DONE_DELAY);
3627 }
3628
3629
3630 // ------------------------------------------------------------------------
3631 static void
3632 program_eeprom(cyg_uint32 ioaddr, cyg_uint32 eeprom_size, cyg_uint8 *data)
3633 {
3634   cyg_uint32 i;
3635   cyg_uint16 checksum = 0;
3636   cyg_uint16 value;
3637
3638   // First enable erase/write operations on the eeprom.
3639   // This is done through the EWEN instruction.
3640   write_enable_eeprom( ioaddr, eeprom_size );
3641
3642   for (i=0 ; i< (1 << eeprom_size) ; i++) {
3643     value = ((unsigned short *)data)[i];
3644     checksum += value;
3645 #ifdef DEBUG_82559
3646     os_printf("\n i : %x ... value to be written : %x",i,value);
3647 #endif
3648     write_eeprom( ioaddr, i, eeprom_size, value);
3649 #ifdef DEBUG_82559
3650     os_printf("\n val read : %x ",read_eeprom(ioaddr,i,eeprom_size));
3651 #endif
3652   }
3653   value = 0xBABA - checksum;
3654 #ifdef DEBUG_82559
3655   os_printf("\n i : %x ... checksum adjustment val to be written : %x",i,value);
3656 #endif
3657   write_eeprom( ioaddr, i, eeprom_size, value );
3658 }
3659
3660 // ------------------------------------------------------------------------
3661 #endif // ! CYGPKG_DEVS_ETH_INTEL_I82559_WRITE_EEPROM
3662
3663
3664 // ------------------------------------------------------------------------
3665 //
3666 //  Function : eth_get_mac_address
3667 //
3668 // ------------------------------------------------------------------------
3669 #ifdef ETH_DRV_GET_MAC_ADDRESS
3670 static int
3671 eth_get_mac_address(struct i82559* p_i82559, char *addr)
3672 {
3673     IF_BAD_82559( p_i82559 ) {
3674 #ifdef DEBUG
3675         os_printf( "eth_get_mac_address : Bad device pointer %x\n",
3676                    p_i82559 );
3677 #endif
3678         return -1;
3679     }
3680
3681     memcpy( addr, (char *)(&p_i82559->mac_address[0]), 6 );
3682     return 0;
3683 }
3684 #endif
3685 // ------------------------------------------------------------------------
3686 //
3687 //  Function : i82559_ioctl
3688 //
3689 // ------------------------------------------------------------------------
3690 static int
3691 i82559_ioctl(struct eth_drv_sc *sc, unsigned long key,
3692              void *data, int data_length)
3693 {
3694     struct i82559 *p_i82559;
3695
3696     p_i82559 = (struct i82559 *)sc->driver_private;
3697
3698     IF_BAD_82559( p_i82559 ) {
3699 #ifdef DEBUG
3700         os_printf( "i82559_ioctl/control: Bad device pointer %x\n", p_i82559 );
3701 #endif
3702         return -1;
3703     }
3704
3705 #ifdef ioctlDEBUG
3706     db_printf( "i82559_ioctl: device eth%d at %x; key is 0x%x, data at %x[%d]\n",
3707                p_i82559->index, p_i82559, key, data, data_length );
3708 #endif
3709
3710     switch ( key ) {
3711
3712 #ifdef ETH_DRV_SET_MAC_ADDRESS
3713     case ETH_DRV_SET_MAC_ADDRESS:
3714         if ( 6 != data_length )
3715             return -2;
3716         return eth_set_mac_address( p_i82559, data, 1 /* do write eeprom */ );
3717 #endif
3718
3719 #ifdef ETH_DRV_GET_MAC_ADDRESS
3720     case ETH_DRV_GET_MAC_ADDRESS:
3721         return eth_get_mac_address( p_i82559, data );
3722 #endif
3723
3724 #ifdef ETH_DRV_GET_IF_STATS_UD
3725     case ETH_DRV_GET_IF_STATS_UD: // UD == UPDATE
3726         ETH_STATS_INIT( sc );    // so UPDATE the statistics structure
3727 #endif
3728         // drop through
3729 #ifdef ETH_DRV_GET_IF_STATS
3730     case ETH_DRV_GET_IF_STATS:
3731 #endif
3732 #if defined(ETH_DRV_GET_IF_STATS) || defined (ETH_DRV_GET_IF_STATS_UD)
3733     {
3734         struct ether_drv_stats *p = (struct ether_drv_stats *)data;
3735         int i;
3736         static unsigned char my_chipset[]
3737             = { ETH_DEV_DOT3STATSETHERCHIPSET };
3738
3739         strcpy( p->description, CYGDAT_DEVS_ETH_DESCRIPTION );
3740         CYG_ASSERT( 48 > strlen(p->description), "Description too long" );
3741
3742         for ( i = 0; i < SNMP_CHIPSET_LEN; i++ )
3743             if ( 0 == (p->snmp_chipset[i] = my_chipset[i]) )
3744                 break;
3745
3746         i = i82559_status( sc );
3747
3748         if ( !( i & GEN_STATUS_LINK) ) {
3749             p->operational = 2;         // LINK DOWN
3750             p->duplex = 1;              // UNKNOWN
3751             p->speed = 0;
3752         }
3753         else {
3754             p->operational = 3;            // LINK UP
3755             p->duplex = (i & GEN_STATUS_FDX) ? 3 : 2; // 2 = SIMPLEX, 3 = DUPLEX
3756             p->speed = ((i & GEN_STATUS_100MBPS) ? 100 : 10) * 1000000;
3757         }
3758
3759 #ifdef KEEP_STATISTICS
3760         {
3761             I82559_COUNTERS *pc = &i82559_counters[ p_i82559->index ];
3762             STATISTICS      *ps = &statistics[      p_i82559->index ];
3763
3764             // Admit to it...
3765             p->supports_dot3        = true;
3766
3767             // Those commented out are not available on this chip.
3768
3769             p->tx_good              = pc->tx_good             ;
3770             p->tx_max_collisions    = pc->tx_max_collisions   ;
3771             p->tx_late_collisions   = pc->tx_late_collisions  ;
3772             p->tx_underrun          = pc->tx_underrun         ;
3773             p->tx_carrier_loss      = pc->tx_carrier_loss     ;
3774             p->tx_deferred          = pc->tx_deferred         ;
3775             //p->tx_sqetesterrors   = pc->tx_sqetesterrors    ;
3776             p->tx_single_collisions = pc->tx_single_collisions;
3777             p->tx_mult_collisions   = pc->tx_mult_collisions  ;
3778             p->tx_total_collisions  = pc->tx_total_collisions ;
3779             p->rx_good              = pc->rx_good             ;
3780             p->rx_crc_errors        = pc->rx_crc_errors       ;
3781             p->rx_align_errors      = pc->rx_align_errors     ;
3782             p->rx_resource_errors   = pc->rx_resource_errors  ;
3783             p->rx_overrun_errors    = pc->rx_overrun_errors   ;
3784             p->rx_collisions        = pc->rx_collisions       ;
3785             p->rx_short_frames      = pc->rx_short_frames     ;
3786             //p->rx_too_long_frames = pc->rx_too_long_frames  ;
3787             //p->rx_symbol_errors   = pc->rx_symbol_errors    ;
3788         
3789             p->interrupts           = ps->interrupts          ;
3790             p->rx_count             = ps->rx_count            ;
3791             p->rx_deliver           = ps->rx_deliver          ;
3792             p->rx_resource          = ps->rx_resource         ;
3793             p->rx_restart           = ps->rx_restart          ;
3794             p->tx_count             = ps->tx_count            ;
3795             p->tx_complete          = ps->tx_complete         ;
3796             p->tx_dropped           = ps->tx_dropped          ;
3797         }
3798 #endif // KEEP_STATISTICS
3799
3800         p->tx_queue_len = MAX_TX_DESCRIPTORS;
3801
3802         return 0; // OK
3803     }
3804 #endif
3805
3806 #ifdef ETH_DRV_SET_MC_LIST
3807     case ETH_DRV_SET_MC_LIST:    {
3808             struct eth_drv_mc_list *mcl = (struct eth_drv_mc_list *)data;
3809
3810             i82559_reset(p_i82559);
3811             ResetRxRing( p_i82559 );
3812             ResetTxRing( p_i82559 );
3813
3814             p_i82559->multicast_all = 0;
3815
3816             i82559_configure(p_i82559,
3817                              p_i82559->promisc,
3818                              p_i82559->oversized,
3819                              p_i82559->multicast_all );
3820             
3821             i82559_set_multicast( p_i82559,
3822                                   mcl->len,
3823                                   &(mcl->addrs[0][0]) );
3824
3825             i82559_restart(p_i82559);
3826             return 0;
3827     }
3828 #endif // ETH_DRV_SET_MC_LIST
3829
3830 #ifdef ETH_DRV_SET_MC_ALL
3831     case ETH_DRV_SET_MC_ALL:
3832             i82559_reset(p_i82559);
3833             ResetRxRing( p_i82559 );
3834             ResetTxRing( p_i82559 );
3835
3836             p_i82559->multicast_all = 1;
3837
3838             i82559_configure(p_i82559,
3839                              p_i82559->promisc,
3840                              p_i82559->oversized,
3841                              p_i82559->multicast_all );
3842             
3843             i82559_restart(p_i82559);
3844             return 0;
3845 #endif // ETH_DRV_SET_MC_ALL
3846
3847     default:
3848         break;
3849     }
3850     return -1;
3851 }
3852
3853 // ------------------------------------------------------------------------
3854 //
3855 // Statistics update...
3856 //
3857 // ------------------------------------------------------------------------
3858
3859 #ifdef KEEP_STATISTICS
3860 #ifdef CYGDBG_DEVS_ETH_INTEL_I82559_KEEP_82559_STATISTICS
3861 void
3862 update_statistics(struct i82559* p_i82559)
3863 {
3864     I82559_COUNTERS *p_statistics;
3865     cyg_uint32 *p_counter;
3866     cyg_uint32 *p_register;
3867     int reg_count, ints;
3868 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_DEMUX_ALL
3869     struct i82559* op_i82559 = p_i82559;
3870 #endif
3871
3872     ints = Mask82559Interrupt(p_i82559);
3873
3874     // This points to the shared memory stats area/command block
3875     p_statistics = (I82559_COUNTERS *)(p_i82559->p_statistics);
3876
3877     if ( (p_statistics->done & 0xFFFF) == 0xA007 ) {
3878         p_counter = (cyg_uint32 *)&i82559_counters[ p_i82559->index ];
3879         p_register = (cyg_uint32 *)p_statistics;
3880         for ( reg_count = 0;
3881               reg_count < sizeof( I82559_COUNTERS ) / sizeof( cyg_uint32 ) - 1;
3882               reg_count++ ) {
3883             *p_counter += *p_register;
3884             p_counter++;
3885             p_register++;
3886         }
3887         p_statistics->done = 0;
3888         // start register dump
3889         wait_for_cmd_done(p_i82559->io_address, WAIT_CU);
3890         OUTW(CU_DUMPSTATS, p_i82559->io_address + SCBCmd);
3891     }
3892
3893 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_DEMUX_ALL
3894     {
3895         int i;
3896         // The problem is, if DEMUX_ALL, either device can eat the other's
3897         // interrupts; so we must poll them *both*:
3898         for (i = 0; i < CYGNUM_DEVS_ETH_INTEL_I82559_DEV_COUNT; i++) {
3899             p_i82559 = i82559_priv_array[i];
3900             if ( p_i82559->active ) {
3901                 // See if the Tx machine is wedged - reset if so:
3902                 Check82559TxLockupTimeout(p_i82559);
3903                 TxMachine(p_i82559);
3904                 Acknowledge82559Interrupt(p_i82559);
3905                 PacketRxReady(p_i82559);
3906 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_MISSED_INTERRUPT
3907                 if ( CYGHWR_DEVS_ETH_INTEL_I82559_MISSED_INTERRUPT(p_i82559) ) {
3908 #ifdef CYGDBG_USE_ASSERTS
3909                     missed_interrupt.stats++;
3910 #endif
3911                     eth_isr( p_i82559->vector, (cyg_addrword_t)p_i82559 );
3912                     eth_dsr( p_i82559->vector, 1, (cyg_addrword_t)p_i82559 );
3913                 }
3914 #endif
3915             }
3916         }
3917     }
3918     // ensure we look at the correct device at the end
3919     p_i82559 = op_i82559;
3920 #else // no CYGHWR_DEVS_ETH_INTEL_I82559_DEMUX_ALL
3921     // See if the Tx machine is wedged - reset if so:
3922     Check82559TxLockupTimeout(p_i82559);
3923     TxMachine(p_i82559);
3924     Acknowledge82559Interrupt(p_i82559); // This can eat an Rx interrupt, so
3925     PacketRxReady(p_i82559);
3926 #ifdef CYGHWR_DEVS_ETH_INTEL_I82559_MISSED_INTERRUPT
3927     if ( CYGHWR_DEVS_ETH_INTEL_I82559_MISSED_INTERRUPT(p_i82559) ) {
3928 #ifdef CYGDBG_USE_ASSERTS
3929         missed_interrupt.stats++;
3930 #endif
3931         eth_isr( p_i82559->vector, (cyg_addrword_t)p_i82559 );
3932         eth_dsr( p_i82559->vector, 1, (cyg_addrword_t)p_i82559 );
3933     }
3934 #endif
3935 #endif // no CYGHWR_DEVS_ETH_INTEL_I82559_DEMUX_ALL
3936
3937     UnMask82559Interrupt(p_i82559, ints);
3938 }
3939 #endif
3940 #endif // KEEP_STATISTICS
3941
3942 // ------------------------------------------------------------------------
3943
3944 // EOF if_i82559.c