OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [devs/] [eth/] [intel/] [i82559/] [v2_0/] [src/] [if_i82559.c] - Blame information for rev 174

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.