x86: constify stacktrace_ops
[linux-2.6] / drivers / net / forcedeth.c
1 /*
2  * forcedeth: Ethernet driver for NVIDIA nForce media access controllers.
3  *
4  * Note: This driver is a cleanroom reimplementation based on reverse
5  *      engineered documentation written by Carl-Daniel Hailfinger
6  *      and Andrew de Quincey.
7  *
8  * NVIDIA, nForce and other NVIDIA marks are trademarks or registered
9  * trademarks of NVIDIA Corporation in the United States and other
10  * countries.
11  *
12  * Copyright (C) 2003,4,5 Manfred Spraul
13  * Copyright (C) 2004 Andrew de Quincey (wol support)
14  * Copyright (C) 2004 Carl-Daniel Hailfinger (invalid MAC handling, insane
15  *              IRQ rate fixes, bigendian fixes, cleanups, verification)
16  * Copyright (c) 2004,5,6 NVIDIA Corporation
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
31  *
32  * Changelog:
33  *      0.01: 05 Oct 2003: First release that compiles without warnings.
34  *      0.02: 05 Oct 2003: Fix bug for nv_drain_tx: do not try to free NULL skbs.
35  *                         Check all PCI BARs for the register window.
36  *                         udelay added to mii_rw.
37  *      0.03: 06 Oct 2003: Initialize dev->irq.
38  *      0.04: 07 Oct 2003: Initialize np->lock, reduce handled irqs, add printks.
39  *      0.05: 09 Oct 2003: printk removed again, irq status print tx_timeout.
40  *      0.06: 10 Oct 2003: MAC Address read updated, pff flag generation updated,
41  *                         irq mask updated
42  *      0.07: 14 Oct 2003: Further irq mask updates.
43  *      0.08: 20 Oct 2003: rx_desc.Length initialization added, nv_alloc_rx refill
44  *                         added into irq handler, NULL check for drain_ring.
45  *      0.09: 20 Oct 2003: Basic link speed irq implementation. Only handle the
46  *                         requested interrupt sources.
47  *      0.10: 20 Oct 2003: First cleanup for release.
48  *      0.11: 21 Oct 2003: hexdump for tx added, rx buffer sizes increased.
49  *                         MAC Address init fix, set_multicast cleanup.
50  *      0.12: 23 Oct 2003: Cleanups for release.
51  *      0.13: 25 Oct 2003: Limit for concurrent tx packets increased to 10.
52  *                         Set link speed correctly. start rx before starting
53  *                         tx (nv_start_rx sets the link speed).
54  *      0.14: 25 Oct 2003: Nic dependant irq mask.
55  *      0.15: 08 Nov 2003: fix smp deadlock with set_multicast_list during
56  *                         open.
57  *      0.16: 15 Nov 2003: include file cleanup for ppc64, rx buffer size
58  *                         increased to 1628 bytes.
59  *      0.17: 16 Nov 2003: undo rx buffer size increase. Substract 1 from
60  *                         the tx length.
61  *      0.18: 17 Nov 2003: fix oops due to late initialization of dev_stats
62  *      0.19: 29 Nov 2003: Handle RxNoBuf, detect & handle invalid mac
63  *                         addresses, really stop rx if already running
64  *                         in nv_start_rx, clean up a bit.
65  *      0.20: 07 Dec 2003: alloc fixes
66  *      0.21: 12 Jan 2004: additional alloc fix, nic polling fix.
67  *      0.22: 19 Jan 2004: reprogram timer to a sane rate, avoid lockup
68  *                         on close.
69  *      0.23: 26 Jan 2004: various small cleanups
70  *      0.24: 27 Feb 2004: make driver even less anonymous in backtraces
71  *      0.25: 09 Mar 2004: wol support
72  *      0.26: 03 Jun 2004: netdriver specific annotation, sparse-related fixes
73  *      0.27: 19 Jun 2004: Gigabit support, new descriptor rings,
74  *                         added CK804/MCP04 device IDs, code fixes
75  *                         for registers, link status and other minor fixes.
76  *      0.28: 21 Jun 2004: Big cleanup, making driver mostly endian safe
77  *      0.29: 31 Aug 2004: Add backup timer for link change notification.
78  *      0.30: 25 Sep 2004: rx checksum support for nf 250 Gb. Add rx reset
79  *                         into nv_close, otherwise reenabling for wol can
80  *                         cause DMA to kfree'd memory.
81  *      0.31: 14 Nov 2004: ethtool support for getting/setting link
82  *                         capabilities.
83  *      0.32: 16 Apr 2005: RX_ERROR4 handling added.
84  *      0.33: 16 May 2005: Support for MCP51 added.
85  *      0.34: 18 Jun 2005: Add DEV_NEED_LINKTIMER to all nForce nics.
86  *      0.35: 26 Jun 2005: Support for MCP55 added.
87  *      0.36: 28 Jun 2005: Add jumbo frame support.
88  *      0.37: 10 Jul 2005: Additional ethtool support, cleanup of pci id list
89  *      0.38: 16 Jul 2005: tx irq rewrite: Use global flags instead of
90  *                         per-packet flags.
91  *      0.39: 18 Jul 2005: Add 64bit descriptor support.
92  *      0.40: 19 Jul 2005: Add support for mac address change.
93  *      0.41: 30 Jul 2005: Write back original MAC in nv_close instead
94  *                         of nv_remove
95  *      0.42: 06 Aug 2005: Fix lack of link speed initialization
96  *                         in the second (and later) nv_open call
97  *      0.43: 10 Aug 2005: Add support for tx checksum.
98  *      0.44: 20 Aug 2005: Add support for scatter gather and segmentation.
99  *      0.45: 18 Sep 2005: Remove nv_stop/start_rx from every link check
100  *      0.46: 20 Oct 2005: Add irq optimization modes.
101  *      0.47: 26 Oct 2005: Add phyaddr 0 in phy scan.
102  *      0.48: 24 Dec 2005: Disable TSO, bugfix for pci_map_single
103  *      0.49: 10 Dec 2005: Fix tso for large buffers.
104  *      0.50: 20 Jan 2006: Add 8021pq tagging support.
105  *      0.51: 20 Jan 2006: Add 64bit consistent memory allocation for rings.
106  *      0.52: 20 Jan 2006: Add MSI/MSIX support.
107  *      0.53: 19 Mar 2006: Fix init from low power mode and add hw reset.
108  *      0.54: 21 Mar 2006: Fix spin locks for multi irqs and cleanup.
109  *      0.55: 22 Mar 2006: Add flow control (pause frame).
110  *      0.56: 22 Mar 2006: Additional ethtool config and moduleparam support.
111  *      0.57: 14 May 2006: Mac address set in probe/remove and order corrections.
112  *      0.58: 30 Oct 2006: Added support for sideband management unit.
113  *      0.59: 30 Oct 2006: Added support for recoverable error.
114  *      0.60: 20 Jan 2007: Code optimizations for rings, rx & tx data paths, and stats.
115  *
116  * Known bugs:
117  * We suspect that on some hardware no TX done interrupts are generated.
118  * This means recovery from netif_stop_queue only happens if the hw timer
119  * interrupt fires (100 times/second, configurable with NVREG_POLL_DEFAULT)
120  * and the timer is active in the IRQMask, or if a rx packet arrives by chance.
121  * If your hardware reliably generates tx done interrupts, then you can remove
122  * DEV_NEED_TIMERIRQ from the driver_data flags.
123  * DEV_NEED_TIMERIRQ will not harm you on sane hardware, only generating a few
124  * superfluous timer interrupts from the nic.
125  */
126 #ifdef CONFIG_FORCEDETH_NAPI
127 #define DRIVERNAPI "-NAPI"
128 #else
129 #define DRIVERNAPI
130 #endif
131 #define FORCEDETH_VERSION               "0.61"
132 #define DRV_NAME                        "forcedeth"
133
134 #include <linux/module.h>
135 #include <linux/types.h>
136 #include <linux/pci.h>
137 #include <linux/interrupt.h>
138 #include <linux/netdevice.h>
139 #include <linux/etherdevice.h>
140 #include <linux/delay.h>
141 #include <linux/spinlock.h>
142 #include <linux/ethtool.h>
143 #include <linux/timer.h>
144 #include <linux/skbuff.h>
145 #include <linux/mii.h>
146 #include <linux/random.h>
147 #include <linux/init.h>
148 #include <linux/if_vlan.h>
149 #include <linux/dma-mapping.h>
150
151 #include <asm/irq.h>
152 #include <asm/io.h>
153 #include <asm/uaccess.h>
154 #include <asm/system.h>
155
156 #if 0
157 #define dprintk                 printk
158 #else
159 #define dprintk(x...)           do { } while (0)
160 #endif
161
162 #define TX_WORK_PER_LOOP  64
163 #define RX_WORK_PER_LOOP  64
164
165 /*
166  * Hardware access:
167  */
168
169 #define DEV_NEED_TIMERIRQ       0x0001  /* set the timer irq flag in the irq mask */
170 #define DEV_NEED_LINKTIMER      0x0002  /* poll link settings. Relies on the timer irq */
171 #define DEV_HAS_LARGEDESC       0x0004  /* device supports jumbo frames and needs packet format 2 */
172 #define DEV_HAS_HIGH_DMA        0x0008  /* device supports 64bit dma */
173 #define DEV_HAS_CHECKSUM        0x0010  /* device supports tx and rx checksum offloads */
174 #define DEV_HAS_VLAN            0x0020  /* device supports vlan tagging and striping */
175 #define DEV_HAS_MSI             0x0040  /* device supports MSI */
176 #define DEV_HAS_MSI_X           0x0080  /* device supports MSI-X */
177 #define DEV_HAS_POWER_CNTRL     0x0100  /* device supports power savings */
178 #define DEV_HAS_PAUSEFRAME_TX   0x0200  /* device supports tx pause frames */
179 #define DEV_HAS_STATISTICS_V1   0x0400  /* device supports hw statistics version 1 */
180 #define DEV_HAS_STATISTICS_V2   0x0800  /* device supports hw statistics version 2 */
181 #define DEV_HAS_TEST_EXTENDED   0x1000  /* device supports extended diagnostic test */
182 #define DEV_HAS_MGMT_UNIT       0x2000  /* device supports management unit */
183 #define DEV_HAS_CORRECT_MACADDR 0x4000  /* device supports correct mac address order */
184
185 enum {
186         NvRegIrqStatus = 0x000,
187 #define NVREG_IRQSTAT_MIIEVENT  0x040
188 #define NVREG_IRQSTAT_MASK              0x81ff
189         NvRegIrqMask = 0x004,
190 #define NVREG_IRQ_RX_ERROR              0x0001
191 #define NVREG_IRQ_RX                    0x0002
192 #define NVREG_IRQ_RX_NOBUF              0x0004
193 #define NVREG_IRQ_TX_ERR                0x0008
194 #define NVREG_IRQ_TX_OK                 0x0010
195 #define NVREG_IRQ_TIMER                 0x0020
196 #define NVREG_IRQ_LINK                  0x0040
197 #define NVREG_IRQ_RX_FORCED             0x0080
198 #define NVREG_IRQ_TX_FORCED             0x0100
199 #define NVREG_IRQ_RECOVER_ERROR         0x8000
200 #define NVREG_IRQMASK_THROUGHPUT        0x00df
201 #define NVREG_IRQMASK_CPU               0x0060
202 #define NVREG_IRQ_TX_ALL                (NVREG_IRQ_TX_ERR|NVREG_IRQ_TX_OK|NVREG_IRQ_TX_FORCED)
203 #define NVREG_IRQ_RX_ALL                (NVREG_IRQ_RX_ERROR|NVREG_IRQ_RX|NVREG_IRQ_RX_NOBUF|NVREG_IRQ_RX_FORCED)
204 #define NVREG_IRQ_OTHER                 (NVREG_IRQ_TIMER|NVREG_IRQ_LINK|NVREG_IRQ_RECOVER_ERROR)
205
206 #define NVREG_IRQ_UNKNOWN       (~(NVREG_IRQ_RX_ERROR|NVREG_IRQ_RX|NVREG_IRQ_RX_NOBUF|NVREG_IRQ_TX_ERR| \
207                                         NVREG_IRQ_TX_OK|NVREG_IRQ_TIMER|NVREG_IRQ_LINK|NVREG_IRQ_RX_FORCED| \
208                                         NVREG_IRQ_TX_FORCED|NVREG_IRQ_RECOVER_ERROR))
209
210         NvRegUnknownSetupReg6 = 0x008,
211 #define NVREG_UNKSETUP6_VAL             3
212
213 /*
214  * NVREG_POLL_DEFAULT is the interval length of the timer source on the nic
215  * NVREG_POLL_DEFAULT=97 would result in an interval length of 1 ms
216  */
217         NvRegPollingInterval = 0x00c,
218 #define NVREG_POLL_DEFAULT_THROUGHPUT   970 /* backup tx cleanup if loop max reached */
219 #define NVREG_POLL_DEFAULT_CPU  13
220         NvRegMSIMap0 = 0x020,
221         NvRegMSIMap1 = 0x024,
222         NvRegMSIIrqMask = 0x030,
223 #define NVREG_MSI_VECTOR_0_ENABLED 0x01
224         NvRegMisc1 = 0x080,
225 #define NVREG_MISC1_PAUSE_TX    0x01
226 #define NVREG_MISC1_HD          0x02
227 #define NVREG_MISC1_FORCE       0x3b0f3c
228
229         NvRegMacReset = 0x3c,
230 #define NVREG_MAC_RESET_ASSERT  0x0F3
231         NvRegTransmitterControl = 0x084,
232 #define NVREG_XMITCTL_START     0x01
233 #define NVREG_XMITCTL_MGMT_ST   0x40000000
234 #define NVREG_XMITCTL_SYNC_MASK         0x000f0000
235 #define NVREG_XMITCTL_SYNC_NOT_READY    0x0
236 #define NVREG_XMITCTL_SYNC_PHY_INIT     0x00040000
237 #define NVREG_XMITCTL_MGMT_SEMA_MASK    0x00000f00
238 #define NVREG_XMITCTL_MGMT_SEMA_FREE    0x0
239 #define NVREG_XMITCTL_HOST_SEMA_MASK    0x0000f000
240 #define NVREG_XMITCTL_HOST_SEMA_ACQ     0x0000f000
241 #define NVREG_XMITCTL_HOST_LOADED       0x00004000
242 #define NVREG_XMITCTL_TX_PATH_EN        0x01000000
243         NvRegTransmitterStatus = 0x088,
244 #define NVREG_XMITSTAT_BUSY     0x01
245
246         NvRegPacketFilterFlags = 0x8c,
247 #define NVREG_PFF_PAUSE_RX      0x08
248 #define NVREG_PFF_ALWAYS        0x7F0000
249 #define NVREG_PFF_PROMISC       0x80
250 #define NVREG_PFF_MYADDR        0x20
251 #define NVREG_PFF_LOOPBACK      0x10
252
253         NvRegOffloadConfig = 0x90,
254 #define NVREG_OFFLOAD_HOMEPHY   0x601
255 #define NVREG_OFFLOAD_NORMAL    RX_NIC_BUFSIZE
256         NvRegReceiverControl = 0x094,
257 #define NVREG_RCVCTL_START      0x01
258 #define NVREG_RCVCTL_RX_PATH_EN 0x01000000
259         NvRegReceiverStatus = 0x98,
260 #define NVREG_RCVSTAT_BUSY      0x01
261
262         NvRegRandomSeed = 0x9c,
263 #define NVREG_RNDSEED_MASK      0x00ff
264 #define NVREG_RNDSEED_FORCE     0x7f00
265 #define NVREG_RNDSEED_FORCE2    0x2d00
266 #define NVREG_RNDSEED_FORCE3    0x7400
267
268         NvRegTxDeferral = 0xA0,
269 #define NVREG_TX_DEFERRAL_DEFAULT       0x15050f
270 #define NVREG_TX_DEFERRAL_RGMII_10_100  0x16070f
271 #define NVREG_TX_DEFERRAL_RGMII_1000    0x14050f
272         NvRegRxDeferral = 0xA4,
273 #define NVREG_RX_DEFERRAL_DEFAULT       0x16
274         NvRegMacAddrA = 0xA8,
275         NvRegMacAddrB = 0xAC,
276         NvRegMulticastAddrA = 0xB0,
277 #define NVREG_MCASTADDRA_FORCE  0x01
278         NvRegMulticastAddrB = 0xB4,
279         NvRegMulticastMaskA = 0xB8,
280         NvRegMulticastMaskB = 0xBC,
281
282         NvRegPhyInterface = 0xC0,
283 #define PHY_RGMII               0x10000000
284
285         NvRegTxRingPhysAddr = 0x100,
286         NvRegRxRingPhysAddr = 0x104,
287         NvRegRingSizes = 0x108,
288 #define NVREG_RINGSZ_TXSHIFT 0
289 #define NVREG_RINGSZ_RXSHIFT 16
290         NvRegTransmitPoll = 0x10c,
291 #define NVREG_TRANSMITPOLL_MAC_ADDR_REV 0x00008000
292         NvRegLinkSpeed = 0x110,
293 #define NVREG_LINKSPEED_FORCE 0x10000
294 #define NVREG_LINKSPEED_10      1000
295 #define NVREG_LINKSPEED_100     100
296 #define NVREG_LINKSPEED_1000    50
297 #define NVREG_LINKSPEED_MASK    (0xFFF)
298         NvRegUnknownSetupReg5 = 0x130,
299 #define NVREG_UNKSETUP5_BIT31   (1<<31)
300         NvRegTxWatermark = 0x13c,
301 #define NVREG_TX_WM_DESC1_DEFAULT       0x0200010
302 #define NVREG_TX_WM_DESC2_3_DEFAULT     0x1e08000
303 #define NVREG_TX_WM_DESC2_3_1000        0xfe08000
304         NvRegTxRxControl = 0x144,
305 #define NVREG_TXRXCTL_KICK      0x0001
306 #define NVREG_TXRXCTL_BIT1      0x0002
307 #define NVREG_TXRXCTL_BIT2      0x0004
308 #define NVREG_TXRXCTL_IDLE      0x0008
309 #define NVREG_TXRXCTL_RESET     0x0010
310 #define NVREG_TXRXCTL_RXCHECK   0x0400
311 #define NVREG_TXRXCTL_DESC_1    0
312 #define NVREG_TXRXCTL_DESC_2    0x002100
313 #define NVREG_TXRXCTL_DESC_3    0xc02200
314 #define NVREG_TXRXCTL_VLANSTRIP 0x00040
315 #define NVREG_TXRXCTL_VLANINS   0x00080
316         NvRegTxRingPhysAddrHigh = 0x148,
317         NvRegRxRingPhysAddrHigh = 0x14C,
318         NvRegTxPauseFrame = 0x170,
319 #define NVREG_TX_PAUSEFRAME_DISABLE     0x1ff0080
320 #define NVREG_TX_PAUSEFRAME_ENABLE      0x0c00030
321         NvRegMIIStatus = 0x180,
322 #define NVREG_MIISTAT_ERROR             0x0001
323 #define NVREG_MIISTAT_LINKCHANGE        0x0008
324 #define NVREG_MIISTAT_MASK              0x000f
325 #define NVREG_MIISTAT_MASK2             0x000f
326         NvRegMIIMask = 0x184,
327 #define NVREG_MII_LINKCHANGE            0x0008
328
329         NvRegAdapterControl = 0x188,
330 #define NVREG_ADAPTCTL_START    0x02
331 #define NVREG_ADAPTCTL_LINKUP   0x04
332 #define NVREG_ADAPTCTL_PHYVALID 0x40000
333 #define NVREG_ADAPTCTL_RUNNING  0x100000
334 #define NVREG_ADAPTCTL_PHYSHIFT 24
335         NvRegMIISpeed = 0x18c,
336 #define NVREG_MIISPEED_BIT8     (1<<8)
337 #define NVREG_MIIDELAY  5
338         NvRegMIIControl = 0x190,
339 #define NVREG_MIICTL_INUSE      0x08000
340 #define NVREG_MIICTL_WRITE      0x00400
341 #define NVREG_MIICTL_ADDRSHIFT  5
342         NvRegMIIData = 0x194,
343         NvRegWakeUpFlags = 0x200,
344 #define NVREG_WAKEUPFLAGS_VAL           0x7770
345 #define NVREG_WAKEUPFLAGS_BUSYSHIFT     24
346 #define NVREG_WAKEUPFLAGS_ENABLESHIFT   16
347 #define NVREG_WAKEUPFLAGS_D3SHIFT       12
348 #define NVREG_WAKEUPFLAGS_D2SHIFT       8
349 #define NVREG_WAKEUPFLAGS_D1SHIFT       4
350 #define NVREG_WAKEUPFLAGS_D0SHIFT       0
351 #define NVREG_WAKEUPFLAGS_ACCEPT_MAGPAT         0x01
352 #define NVREG_WAKEUPFLAGS_ACCEPT_WAKEUPPAT      0x02
353 #define NVREG_WAKEUPFLAGS_ACCEPT_LINKCHANGE     0x04
354 #define NVREG_WAKEUPFLAGS_ENABLE        0x1111
355
356         NvRegPatternCRC = 0x204,
357         NvRegPatternMask = 0x208,
358         NvRegPowerCap = 0x268,
359 #define NVREG_POWERCAP_D3SUPP   (1<<30)
360 #define NVREG_POWERCAP_D2SUPP   (1<<26)
361 #define NVREG_POWERCAP_D1SUPP   (1<<25)
362         NvRegPowerState = 0x26c,
363 #define NVREG_POWERSTATE_POWEREDUP      0x8000
364 #define NVREG_POWERSTATE_VALID          0x0100
365 #define NVREG_POWERSTATE_MASK           0x0003
366 #define NVREG_POWERSTATE_D0             0x0000
367 #define NVREG_POWERSTATE_D1             0x0001
368 #define NVREG_POWERSTATE_D2             0x0002
369 #define NVREG_POWERSTATE_D3             0x0003
370         NvRegTxCnt = 0x280,
371         NvRegTxZeroReXmt = 0x284,
372         NvRegTxOneReXmt = 0x288,
373         NvRegTxManyReXmt = 0x28c,
374         NvRegTxLateCol = 0x290,
375         NvRegTxUnderflow = 0x294,
376         NvRegTxLossCarrier = 0x298,
377         NvRegTxExcessDef = 0x29c,
378         NvRegTxRetryErr = 0x2a0,
379         NvRegRxFrameErr = 0x2a4,
380         NvRegRxExtraByte = 0x2a8,
381         NvRegRxLateCol = 0x2ac,
382         NvRegRxRunt = 0x2b0,
383         NvRegRxFrameTooLong = 0x2b4,
384         NvRegRxOverflow = 0x2b8,
385         NvRegRxFCSErr = 0x2bc,
386         NvRegRxFrameAlignErr = 0x2c0,
387         NvRegRxLenErr = 0x2c4,
388         NvRegRxUnicast = 0x2c8,
389         NvRegRxMulticast = 0x2cc,
390         NvRegRxBroadcast = 0x2d0,
391         NvRegTxDef = 0x2d4,
392         NvRegTxFrame = 0x2d8,
393         NvRegRxCnt = 0x2dc,
394         NvRegTxPause = 0x2e0,
395         NvRegRxPause = 0x2e4,
396         NvRegRxDropFrame = 0x2e8,
397         NvRegVlanControl = 0x300,
398 #define NVREG_VLANCONTROL_ENABLE        0x2000
399         NvRegMSIXMap0 = 0x3e0,
400         NvRegMSIXMap1 = 0x3e4,
401         NvRegMSIXIrqStatus = 0x3f0,
402
403         NvRegPowerState2 = 0x600,
404 #define NVREG_POWERSTATE2_POWERUP_MASK          0x0F11
405 #define NVREG_POWERSTATE2_POWERUP_REV_A3        0x0001
406 };
407
408 /* Big endian: should work, but is untested */
409 struct ring_desc {
410         __le32 buf;
411         __le32 flaglen;
412 };
413
414 struct ring_desc_ex {
415         __le32 bufhigh;
416         __le32 buflow;
417         __le32 txvlan;
418         __le32 flaglen;
419 };
420
421 union ring_type {
422         struct ring_desc* orig;
423         struct ring_desc_ex* ex;
424 };
425
426 #define FLAG_MASK_V1 0xffff0000
427 #define FLAG_MASK_V2 0xffffc000
428 #define LEN_MASK_V1 (0xffffffff ^ FLAG_MASK_V1)
429 #define LEN_MASK_V2 (0xffffffff ^ FLAG_MASK_V2)
430
431 #define NV_TX_LASTPACKET        (1<<16)
432 #define NV_TX_RETRYERROR        (1<<19)
433 #define NV_TX_FORCED_INTERRUPT  (1<<24)
434 #define NV_TX_DEFERRED          (1<<26)
435 #define NV_TX_CARRIERLOST       (1<<27)
436 #define NV_TX_LATECOLLISION     (1<<28)
437 #define NV_TX_UNDERFLOW         (1<<29)
438 #define NV_TX_ERROR             (1<<30)
439 #define NV_TX_VALID             (1<<31)
440
441 #define NV_TX2_LASTPACKET       (1<<29)
442 #define NV_TX2_RETRYERROR       (1<<18)
443 #define NV_TX2_FORCED_INTERRUPT (1<<30)
444 #define NV_TX2_DEFERRED         (1<<25)
445 #define NV_TX2_CARRIERLOST      (1<<26)
446 #define NV_TX2_LATECOLLISION    (1<<27)
447 #define NV_TX2_UNDERFLOW        (1<<28)
448 /* error and valid are the same for both */
449 #define NV_TX2_ERROR            (1<<30)
450 #define NV_TX2_VALID            (1<<31)
451 #define NV_TX2_TSO              (1<<28)
452 #define NV_TX2_TSO_SHIFT        14
453 #define NV_TX2_TSO_MAX_SHIFT    14
454 #define NV_TX2_TSO_MAX_SIZE     (1<<NV_TX2_TSO_MAX_SHIFT)
455 #define NV_TX2_CHECKSUM_L3      (1<<27)
456 #define NV_TX2_CHECKSUM_L4      (1<<26)
457
458 #define NV_TX3_VLAN_TAG_PRESENT (1<<18)
459
460 #define NV_RX_DESCRIPTORVALID   (1<<16)
461 #define NV_RX_MISSEDFRAME       (1<<17)
462 #define NV_RX_SUBSTRACT1        (1<<18)
463 #define NV_RX_ERROR1            (1<<23)
464 #define NV_RX_ERROR2            (1<<24)
465 #define NV_RX_ERROR3            (1<<25)
466 #define NV_RX_ERROR4            (1<<26)
467 #define NV_RX_CRCERR            (1<<27)
468 #define NV_RX_OVERFLOW          (1<<28)
469 #define NV_RX_FRAMINGERR        (1<<29)
470 #define NV_RX_ERROR             (1<<30)
471 #define NV_RX_AVAIL             (1<<31)
472
473 #define NV_RX2_CHECKSUMMASK     (0x1C000000)
474 #define NV_RX2_CHECKSUMOK1      (0x10000000)
475 #define NV_RX2_CHECKSUMOK2      (0x14000000)
476 #define NV_RX2_CHECKSUMOK3      (0x18000000)
477 #define NV_RX2_DESCRIPTORVALID  (1<<29)
478 #define NV_RX2_SUBSTRACT1       (1<<25)
479 #define NV_RX2_ERROR1           (1<<18)
480 #define NV_RX2_ERROR2           (1<<19)
481 #define NV_RX2_ERROR3           (1<<20)
482 #define NV_RX2_ERROR4           (1<<21)
483 #define NV_RX2_CRCERR           (1<<22)
484 #define NV_RX2_OVERFLOW         (1<<23)
485 #define NV_RX2_FRAMINGERR       (1<<24)
486 /* error and avail are the same for both */
487 #define NV_RX2_ERROR            (1<<30)
488 #define NV_RX2_AVAIL            (1<<31)
489
490 #define NV_RX3_VLAN_TAG_PRESENT (1<<16)
491 #define NV_RX3_VLAN_TAG_MASK    (0x0000FFFF)
492
493 /* Miscelaneous hardware related defines: */
494 #define NV_PCI_REGSZ_VER1       0x270
495 #define NV_PCI_REGSZ_VER2       0x2d4
496 #define NV_PCI_REGSZ_VER3       0x604
497
498 /* various timeout delays: all in usec */
499 #define NV_TXRX_RESET_DELAY     4
500 #define NV_TXSTOP_DELAY1        10
501 #define NV_TXSTOP_DELAY1MAX     500000
502 #define NV_TXSTOP_DELAY2        100
503 #define NV_RXSTOP_DELAY1        10
504 #define NV_RXSTOP_DELAY1MAX     500000
505 #define NV_RXSTOP_DELAY2        100
506 #define NV_SETUP5_DELAY         5
507 #define NV_SETUP5_DELAYMAX      50000
508 #define NV_POWERUP_DELAY        5
509 #define NV_POWERUP_DELAYMAX     5000
510 #define NV_MIIBUSY_DELAY        50
511 #define NV_MIIPHY_DELAY 10
512 #define NV_MIIPHY_DELAYMAX      10000
513 #define NV_MAC_RESET_DELAY      64
514
515 #define NV_WAKEUPPATTERNS       5
516 #define NV_WAKEUPMASKENTRIES    4
517
518 /* General driver defaults */
519 #define NV_WATCHDOG_TIMEO       (5*HZ)
520
521 #define RX_RING_DEFAULT         128
522 #define TX_RING_DEFAULT         256
523 #define RX_RING_MIN             128
524 #define TX_RING_MIN             64
525 #define RING_MAX_DESC_VER_1     1024
526 #define RING_MAX_DESC_VER_2_3   16384
527
528 /* rx/tx mac addr + type + vlan + align + slack*/
529 #define NV_RX_HEADERS           (64)
530 /* even more slack. */
531 #define NV_RX_ALLOC_PAD         (64)
532
533 /* maximum mtu size */
534 #define NV_PKTLIMIT_1   ETH_DATA_LEN    /* hard limit not known */
535 #define NV_PKTLIMIT_2   9100    /* Actual limit according to NVidia: 9202 */
536
537 #define OOM_REFILL      (1+HZ/20)
538 #define POLL_WAIT       (1+HZ/100)
539 #define LINK_TIMEOUT    (3*HZ)
540 #define STATS_INTERVAL  (10*HZ)
541
542 /*
543  * desc_ver values:
544  * The nic supports three different descriptor types:
545  * - DESC_VER_1: Original
546  * - DESC_VER_2: support for jumbo frames.
547  * - DESC_VER_3: 64-bit format.
548  */
549 #define DESC_VER_1      1
550 #define DESC_VER_2      2
551 #define DESC_VER_3      3
552
553 /* PHY defines */
554 #define PHY_OUI_MARVELL 0x5043
555 #define PHY_OUI_CICADA  0x03f1
556 #define PHY_OUI_VITESSE 0x01c1
557 #define PHY_OUI_REALTEK 0x0732
558 #define PHYID1_OUI_MASK 0x03ff
559 #define PHYID1_OUI_SHFT 6
560 #define PHYID2_OUI_MASK 0xfc00
561 #define PHYID2_OUI_SHFT 10
562 #define PHYID2_MODEL_MASK               0x03f0
563 #define PHY_MODEL_MARVELL_E3016         0x220
564 #define PHY_MARVELL_E3016_INITMASK      0x0300
565 #define PHY_CICADA_INIT1        0x0f000
566 #define PHY_CICADA_INIT2        0x0e00
567 #define PHY_CICADA_INIT3        0x01000
568 #define PHY_CICADA_INIT4        0x0200
569 #define PHY_CICADA_INIT5        0x0004
570 #define PHY_CICADA_INIT6        0x02000
571 #define PHY_VITESSE_INIT_REG1   0x1f
572 #define PHY_VITESSE_INIT_REG2   0x10
573 #define PHY_VITESSE_INIT_REG3   0x11
574 #define PHY_VITESSE_INIT_REG4   0x12
575 #define PHY_VITESSE_INIT_MSK1   0xc
576 #define PHY_VITESSE_INIT_MSK2   0x0180
577 #define PHY_VITESSE_INIT1       0x52b5
578 #define PHY_VITESSE_INIT2       0xaf8a
579 #define PHY_VITESSE_INIT3       0x8
580 #define PHY_VITESSE_INIT4       0x8f8a
581 #define PHY_VITESSE_INIT5       0xaf86
582 #define PHY_VITESSE_INIT6       0x8f86
583 #define PHY_VITESSE_INIT7       0xaf82
584 #define PHY_VITESSE_INIT8       0x0100
585 #define PHY_VITESSE_INIT9       0x8f82
586 #define PHY_VITESSE_INIT10      0x0
587 #define PHY_REALTEK_INIT_REG1   0x1f
588 #define PHY_REALTEK_INIT_REG2   0x19
589 #define PHY_REALTEK_INIT_REG3   0x13
590 #define PHY_REALTEK_INIT1       0x0000
591 #define PHY_REALTEK_INIT2       0x8e00
592 #define PHY_REALTEK_INIT3       0x0001
593 #define PHY_REALTEK_INIT4       0xad17
594
595 #define PHY_GIGABIT     0x0100
596
597 #define PHY_TIMEOUT     0x1
598 #define PHY_ERROR       0x2
599
600 #define PHY_100 0x1
601 #define PHY_1000        0x2
602 #define PHY_HALF        0x100
603
604 #define NV_PAUSEFRAME_RX_CAPABLE 0x0001
605 #define NV_PAUSEFRAME_TX_CAPABLE 0x0002
606 #define NV_PAUSEFRAME_RX_ENABLE  0x0004
607 #define NV_PAUSEFRAME_TX_ENABLE  0x0008
608 #define NV_PAUSEFRAME_RX_REQ     0x0010
609 #define NV_PAUSEFRAME_TX_REQ     0x0020
610 #define NV_PAUSEFRAME_AUTONEG    0x0040
611
612 /* MSI/MSI-X defines */
613 #define NV_MSI_X_MAX_VECTORS  8
614 #define NV_MSI_X_VECTORS_MASK 0x000f
615 #define NV_MSI_CAPABLE        0x0010
616 #define NV_MSI_X_CAPABLE      0x0020
617 #define NV_MSI_ENABLED        0x0040
618 #define NV_MSI_X_ENABLED      0x0080
619
620 #define NV_MSI_X_VECTOR_ALL   0x0
621 #define NV_MSI_X_VECTOR_RX    0x0
622 #define NV_MSI_X_VECTOR_TX    0x1
623 #define NV_MSI_X_VECTOR_OTHER 0x2
624
625 /* statistics */
626 struct nv_ethtool_str {
627         char name[ETH_GSTRING_LEN];
628 };
629
630 static const struct nv_ethtool_str nv_estats_str[] = {
631         { "tx_bytes" },
632         { "tx_zero_rexmt" },
633         { "tx_one_rexmt" },
634         { "tx_many_rexmt" },
635         { "tx_late_collision" },
636         { "tx_fifo_errors" },
637         { "tx_carrier_errors" },
638         { "tx_excess_deferral" },
639         { "tx_retry_error" },
640         { "rx_frame_error" },
641         { "rx_extra_byte" },
642         { "rx_late_collision" },
643         { "rx_runt" },
644         { "rx_frame_too_long" },
645         { "rx_over_errors" },
646         { "rx_crc_errors" },
647         { "rx_frame_align_error" },
648         { "rx_length_error" },
649         { "rx_unicast" },
650         { "rx_multicast" },
651         { "rx_broadcast" },
652         { "rx_packets" },
653         { "rx_errors_total" },
654         { "tx_errors_total" },
655
656         /* version 2 stats */
657         { "tx_deferral" },
658         { "tx_packets" },
659         { "rx_bytes" },
660         { "tx_pause" },
661         { "rx_pause" },
662         { "rx_drop_frame" }
663 };
664
665 struct nv_ethtool_stats {
666         u64 tx_bytes;
667         u64 tx_zero_rexmt;
668         u64 tx_one_rexmt;
669         u64 tx_many_rexmt;
670         u64 tx_late_collision;
671         u64 tx_fifo_errors;
672         u64 tx_carrier_errors;
673         u64 tx_excess_deferral;
674         u64 tx_retry_error;
675         u64 rx_frame_error;
676         u64 rx_extra_byte;
677         u64 rx_late_collision;
678         u64 rx_runt;
679         u64 rx_frame_too_long;
680         u64 rx_over_errors;
681         u64 rx_crc_errors;
682         u64 rx_frame_align_error;
683         u64 rx_length_error;
684         u64 rx_unicast;
685         u64 rx_multicast;
686         u64 rx_broadcast;
687         u64 rx_packets;
688         u64 rx_errors_total;
689         u64 tx_errors_total;
690
691         /* version 2 stats */
692         u64 tx_deferral;
693         u64 tx_packets;
694         u64 rx_bytes;
695         u64 tx_pause;
696         u64 rx_pause;
697         u64 rx_drop_frame;
698 };
699
700 #define NV_DEV_STATISTICS_V2_COUNT (sizeof(struct nv_ethtool_stats)/sizeof(u64))
701 #define NV_DEV_STATISTICS_V1_COUNT (NV_DEV_STATISTICS_V2_COUNT - 6)
702
703 /* diagnostics */
704 #define NV_TEST_COUNT_BASE 3
705 #define NV_TEST_COUNT_EXTENDED 4
706
707 static const struct nv_ethtool_str nv_etests_str[] = {
708         { "link      (online/offline)" },
709         { "register  (offline)       " },
710         { "interrupt (offline)       " },
711         { "loopback  (offline)       " }
712 };
713
714 struct register_test {
715         __le32 reg;
716         __le32 mask;
717 };
718
719 static const struct register_test nv_registers_test[] = {
720         { NvRegUnknownSetupReg6, 0x01 },
721         { NvRegMisc1, 0x03c },
722         { NvRegOffloadConfig, 0x03ff },
723         { NvRegMulticastAddrA, 0xffffffff },
724         { NvRegTxWatermark, 0x0ff },
725         { NvRegWakeUpFlags, 0x07777 },
726         { 0,0 }
727 };
728
729 struct nv_skb_map {
730         struct sk_buff *skb;
731         dma_addr_t dma;
732         unsigned int dma_len;
733 };
734
735 /*
736  * SMP locking:
737  * All hardware access under dev->priv->lock, except the performance
738  * critical parts:
739  * - rx is (pseudo-) lockless: it relies on the single-threading provided
740  *      by the arch code for interrupts.
741  * - tx setup is lockless: it relies on netif_tx_lock. Actual submission
742  *      needs dev->priv->lock :-(
743  * - set_multicast_list: preparation lockless, relies on netif_tx_lock.
744  */
745
746 /* in dev: base, irq */
747 struct fe_priv {
748         spinlock_t lock;
749
750         struct net_device *dev;
751         struct napi_struct napi;
752
753         /* General data:
754          * Locking: spin_lock(&np->lock); */
755         struct nv_ethtool_stats estats;
756         int in_shutdown;
757         u32 linkspeed;
758         int duplex;
759         int autoneg;
760         int fixed_mode;
761         int phyaddr;
762         int wolenabled;
763         unsigned int phy_oui;
764         unsigned int phy_model;
765         u16 gigabit;
766         int intr_test;
767         int recover_error;
768
769         /* General data: RO fields */
770         dma_addr_t ring_addr;
771         struct pci_dev *pci_dev;
772         u32 orig_mac[2];
773         u32 irqmask;
774         u32 desc_ver;
775         u32 txrxctl_bits;
776         u32 vlanctl_bits;
777         u32 driver_data;
778         u32 register_size;
779         int rx_csum;
780         u32 mac_in_use;
781
782         void __iomem *base;
783
784         /* rx specific fields.
785          * Locking: Within irq hander or disable_irq+spin_lock(&np->lock);
786          */
787         union ring_type get_rx, put_rx, first_rx, last_rx;
788         struct nv_skb_map *get_rx_ctx, *put_rx_ctx;
789         struct nv_skb_map *first_rx_ctx, *last_rx_ctx;
790         struct nv_skb_map *rx_skb;
791
792         union ring_type rx_ring;
793         unsigned int rx_buf_sz;
794         unsigned int pkt_limit;
795         struct timer_list oom_kick;
796         struct timer_list nic_poll;
797         struct timer_list stats_poll;
798         u32 nic_poll_irq;
799         int rx_ring_size;
800
801         /* media detection workaround.
802          * Locking: Within irq hander or disable_irq+spin_lock(&np->lock);
803          */
804         int need_linktimer;
805         unsigned long link_timeout;
806         /*
807          * tx specific fields.
808          */
809         union ring_type get_tx, put_tx, first_tx, last_tx;
810         struct nv_skb_map *get_tx_ctx, *put_tx_ctx;
811         struct nv_skb_map *first_tx_ctx, *last_tx_ctx;
812         struct nv_skb_map *tx_skb;
813
814         union ring_type tx_ring;
815         u32 tx_flags;
816         int tx_ring_size;
817         int tx_stop;
818
819         /* vlan fields */
820         struct vlan_group *vlangrp;
821
822         /* msi/msi-x fields */
823         u32 msi_flags;
824         struct msix_entry msi_x_entry[NV_MSI_X_MAX_VECTORS];
825
826         /* flow control */
827         u32 pause_flags;
828 };
829
830 /*
831  * Maximum number of loops until we assume that a bit in the irq mask
832  * is stuck. Overridable with module param.
833  */
834 static int max_interrupt_work = 5;
835
836 /*
837  * Optimization can be either throuput mode or cpu mode
838  *
839  * Throughput Mode: Every tx and rx packet will generate an interrupt.
840  * CPU Mode: Interrupts are controlled by a timer.
841  */
842 enum {
843         NV_OPTIMIZATION_MODE_THROUGHPUT,
844         NV_OPTIMIZATION_MODE_CPU
845 };
846 static int optimization_mode = NV_OPTIMIZATION_MODE_THROUGHPUT;
847
848 /*
849  * Poll interval for timer irq
850  *
851  * This interval determines how frequent an interrupt is generated.
852  * The is value is determined by [(time_in_micro_secs * 100) / (2^10)]
853  * Min = 0, and Max = 65535
854  */
855 static int poll_interval = -1;
856
857 /*
858  * MSI interrupts
859  */
860 enum {
861         NV_MSI_INT_DISABLED,
862         NV_MSI_INT_ENABLED
863 };
864 static int msi = NV_MSI_INT_ENABLED;
865
866 /*
867  * MSIX interrupts
868  */
869 enum {
870         NV_MSIX_INT_DISABLED,
871         NV_MSIX_INT_ENABLED
872 };
873 static int msix = NV_MSIX_INT_DISABLED;
874
875 /*
876  * DMA 64bit
877  */
878 enum {
879         NV_DMA_64BIT_DISABLED,
880         NV_DMA_64BIT_ENABLED
881 };
882 static int dma_64bit = NV_DMA_64BIT_ENABLED;
883
884 static inline struct fe_priv *get_nvpriv(struct net_device *dev)
885 {
886         return netdev_priv(dev);
887 }
888
889 static inline u8 __iomem *get_hwbase(struct net_device *dev)
890 {
891         return ((struct fe_priv *)netdev_priv(dev))->base;
892 }
893
894 static inline void pci_push(u8 __iomem *base)
895 {
896         /* force out pending posted writes */
897         readl(base);
898 }
899
900 static inline u32 nv_descr_getlength(struct ring_desc *prd, u32 v)
901 {
902         return le32_to_cpu(prd->flaglen)
903                 & ((v == DESC_VER_1) ? LEN_MASK_V1 : LEN_MASK_V2);
904 }
905
906 static inline u32 nv_descr_getlength_ex(struct ring_desc_ex *prd, u32 v)
907 {
908         return le32_to_cpu(prd->flaglen) & LEN_MASK_V2;
909 }
910
911 static int reg_delay(struct net_device *dev, int offset, u32 mask, u32 target,
912                                 int delay, int delaymax, const char *msg)
913 {
914         u8 __iomem *base = get_hwbase(dev);
915
916         pci_push(base);
917         do {
918                 udelay(delay);
919                 delaymax -= delay;
920                 if (delaymax < 0) {
921                         if (msg)
922                                 printk(msg);
923                         return 1;
924                 }
925         } while ((readl(base + offset) & mask) != target);
926         return 0;
927 }
928
929 #define NV_SETUP_RX_RING 0x01
930 #define NV_SETUP_TX_RING 0x02
931
932 static void setup_hw_rings(struct net_device *dev, int rxtx_flags)
933 {
934         struct fe_priv *np = get_nvpriv(dev);
935         u8 __iomem *base = get_hwbase(dev);
936
937         if (np->desc_ver == DESC_VER_1 || np->desc_ver == DESC_VER_2) {
938                 if (rxtx_flags & NV_SETUP_RX_RING) {
939                         writel((u32) cpu_to_le64(np->ring_addr), base + NvRegRxRingPhysAddr);
940                 }
941                 if (rxtx_flags & NV_SETUP_TX_RING) {
942                         writel((u32) cpu_to_le64(np->ring_addr + np->rx_ring_size*sizeof(struct ring_desc)), base + NvRegTxRingPhysAddr);
943                 }
944         } else {
945                 if (rxtx_flags & NV_SETUP_RX_RING) {
946                         writel((u32) cpu_to_le64(np->ring_addr), base + NvRegRxRingPhysAddr);
947                         writel((u32) (cpu_to_le64(np->ring_addr) >> 32), base + NvRegRxRingPhysAddrHigh);
948                 }
949                 if (rxtx_flags & NV_SETUP_TX_RING) {
950                         writel((u32) cpu_to_le64(np->ring_addr + np->rx_ring_size*sizeof(struct ring_desc_ex)), base + NvRegTxRingPhysAddr);
951                         writel((u32) (cpu_to_le64(np->ring_addr + np->rx_ring_size*sizeof(struct ring_desc_ex)) >> 32), base + NvRegTxRingPhysAddrHigh);
952                 }
953         }
954 }
955
956 static void free_rings(struct net_device *dev)
957 {
958         struct fe_priv *np = get_nvpriv(dev);
959
960         if (np->desc_ver == DESC_VER_1 || np->desc_ver == DESC_VER_2) {
961                 if (np->rx_ring.orig)
962                         pci_free_consistent(np->pci_dev, sizeof(struct ring_desc) * (np->rx_ring_size + np->tx_ring_size),
963                                             np->rx_ring.orig, np->ring_addr);
964         } else {
965                 if (np->rx_ring.ex)
966                         pci_free_consistent(np->pci_dev, sizeof(struct ring_desc_ex) * (np->rx_ring_size + np->tx_ring_size),
967                                             np->rx_ring.ex, np->ring_addr);
968         }
969         if (np->rx_skb)
970                 kfree(np->rx_skb);
971         if (np->tx_skb)
972                 kfree(np->tx_skb);
973 }
974
975 static int using_multi_irqs(struct net_device *dev)
976 {
977         struct fe_priv *np = get_nvpriv(dev);
978
979         if (!(np->msi_flags & NV_MSI_X_ENABLED) ||
980             ((np->msi_flags & NV_MSI_X_ENABLED) &&
981              ((np->msi_flags & NV_MSI_X_VECTORS_MASK) == 0x1)))
982                 return 0;
983         else
984                 return 1;
985 }
986
987 static void nv_enable_irq(struct net_device *dev)
988 {
989         struct fe_priv *np = get_nvpriv(dev);
990
991         if (!using_multi_irqs(dev)) {
992                 if (np->msi_flags & NV_MSI_X_ENABLED)
993                         enable_irq(np->msi_x_entry[NV_MSI_X_VECTOR_ALL].vector);
994                 else
995                         enable_irq(dev->irq);
996         } else {
997                 enable_irq(np->msi_x_entry[NV_MSI_X_VECTOR_RX].vector);
998                 enable_irq(np->msi_x_entry[NV_MSI_X_VECTOR_TX].vector);
999                 enable_irq(np->msi_x_entry[NV_MSI_X_VECTOR_OTHER].vector);
1000         }
1001 }
1002
1003 static void nv_disable_irq(struct net_device *dev)
1004 {
1005         struct fe_priv *np = get_nvpriv(dev);
1006
1007         if (!using_multi_irqs(dev)) {
1008                 if (np->msi_flags & NV_MSI_X_ENABLED)
1009                         disable_irq(np->msi_x_entry[NV_MSI_X_VECTOR_ALL].vector);
1010                 else
1011                         disable_irq(dev->irq);
1012         } else {
1013                 disable_irq(np->msi_x_entry[NV_MSI_X_VECTOR_RX].vector);
1014                 disable_irq(np->msi_x_entry[NV_MSI_X_VECTOR_TX].vector);
1015                 disable_irq(np->msi_x_entry[NV_MSI_X_VECTOR_OTHER].vector);
1016         }
1017 }
1018
1019 /* In MSIX mode, a write to irqmask behaves as XOR */
1020 static void nv_enable_hw_interrupts(struct net_device *dev, u32 mask)
1021 {
1022         u8 __iomem *base = get_hwbase(dev);
1023
1024         writel(mask, base + NvRegIrqMask);
1025 }
1026
1027 static void nv_disable_hw_interrupts(struct net_device *dev, u32 mask)
1028 {
1029         struct fe_priv *np = get_nvpriv(dev);
1030         u8 __iomem *base = get_hwbase(dev);
1031
1032         if (np->msi_flags & NV_MSI_X_ENABLED) {
1033                 writel(mask, base + NvRegIrqMask);
1034         } else {
1035                 if (np->msi_flags & NV_MSI_ENABLED)
1036                         writel(0, base + NvRegMSIIrqMask);
1037                 writel(0, base + NvRegIrqMask);
1038         }
1039 }
1040
1041 #define MII_READ        (-1)
1042 /* mii_rw: read/write a register on the PHY.
1043  *
1044  * Caller must guarantee serialization
1045  */
1046 static int mii_rw(struct net_device *dev, int addr, int miireg, int value)
1047 {
1048         u8 __iomem *base = get_hwbase(dev);
1049         u32 reg;
1050         int retval;
1051
1052         writel(NVREG_MIISTAT_MASK, base + NvRegMIIStatus);
1053
1054         reg = readl(base + NvRegMIIControl);
1055         if (reg & NVREG_MIICTL_INUSE) {
1056                 writel(NVREG_MIICTL_INUSE, base + NvRegMIIControl);
1057                 udelay(NV_MIIBUSY_DELAY);
1058         }
1059
1060         reg = (addr << NVREG_MIICTL_ADDRSHIFT) | miireg;
1061         if (value != MII_READ) {
1062                 writel(value, base + NvRegMIIData);
1063                 reg |= NVREG_MIICTL_WRITE;
1064         }
1065         writel(reg, base + NvRegMIIControl);
1066
1067         if (reg_delay(dev, NvRegMIIControl, NVREG_MIICTL_INUSE, 0,
1068                         NV_MIIPHY_DELAY, NV_MIIPHY_DELAYMAX, NULL)) {
1069                 dprintk(KERN_DEBUG "%s: mii_rw of reg %d at PHY %d timed out.\n",
1070                                 dev->name, miireg, addr);
1071                 retval = -1;
1072         } else if (value != MII_READ) {
1073                 /* it was a write operation - fewer failures are detectable */
1074                 dprintk(KERN_DEBUG "%s: mii_rw wrote 0x%x to reg %d at PHY %d\n",
1075                                 dev->name, value, miireg, addr);
1076                 retval = 0;
1077         } else if (readl(base + NvRegMIIStatus) & NVREG_MIISTAT_ERROR) {
1078                 dprintk(KERN_DEBUG "%s: mii_rw of reg %d at PHY %d failed.\n",
1079                                 dev->name, miireg, addr);
1080                 retval = -1;
1081         } else {
1082                 retval = readl(base + NvRegMIIData);
1083                 dprintk(KERN_DEBUG "%s: mii_rw read from reg %d at PHY %d: 0x%x.\n",
1084                                 dev->name, miireg, addr, retval);
1085         }
1086
1087         return retval;
1088 }
1089
1090 static int phy_reset(struct net_device *dev, u32 bmcr_setup)
1091 {
1092         struct fe_priv *np = netdev_priv(dev);
1093         u32 miicontrol;
1094         unsigned int tries = 0;
1095
1096         miicontrol = BMCR_RESET | bmcr_setup;
1097         if (mii_rw(dev, np->phyaddr, MII_BMCR, miicontrol)) {
1098                 return -1;
1099         }
1100
1101         /* wait for 500ms */
1102         msleep(500);
1103
1104         /* must wait till reset is deasserted */
1105         while (miicontrol & BMCR_RESET) {
1106                 msleep(10);
1107                 miicontrol = mii_rw(dev, np->phyaddr, MII_BMCR, MII_READ);
1108                 /* FIXME: 100 tries seem excessive */
1109                 if (tries++ > 100)
1110                         return -1;
1111         }
1112         return 0;
1113 }
1114
1115 static int phy_init(struct net_device *dev)
1116 {
1117         struct fe_priv *np = get_nvpriv(dev);
1118         u8 __iomem *base = get_hwbase(dev);
1119         u32 phyinterface, phy_reserved, mii_status, mii_control, mii_control_1000,reg;
1120
1121         /* phy errata for E3016 phy */
1122         if (np->phy_model == PHY_MODEL_MARVELL_E3016) {
1123                 reg = mii_rw(dev, np->phyaddr, MII_NCONFIG, MII_READ);
1124                 reg &= ~PHY_MARVELL_E3016_INITMASK;
1125                 if (mii_rw(dev, np->phyaddr, MII_NCONFIG, reg)) {
1126                         printk(KERN_INFO "%s: phy write to errata reg failed.\n", pci_name(np->pci_dev));
1127                         return PHY_ERROR;
1128                 }
1129         }
1130         if (np->phy_oui == PHY_OUI_REALTEK) {
1131                 if (mii_rw(dev, np->phyaddr, PHY_REALTEK_INIT_REG1, PHY_REALTEK_INIT1)) {
1132                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1133                         return PHY_ERROR;
1134                 }
1135                 if (mii_rw(dev, np->phyaddr, PHY_REALTEK_INIT_REG2, PHY_REALTEK_INIT2)) {
1136                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1137                         return PHY_ERROR;
1138                 }
1139                 if (mii_rw(dev, np->phyaddr, PHY_REALTEK_INIT_REG1, PHY_REALTEK_INIT3)) {
1140                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1141                         return PHY_ERROR;
1142                 }
1143                 if (mii_rw(dev, np->phyaddr, PHY_REALTEK_INIT_REG3, PHY_REALTEK_INIT4)) {
1144                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1145                         return PHY_ERROR;
1146                 }
1147                 if (mii_rw(dev, np->phyaddr, PHY_REALTEK_INIT_REG1, PHY_REALTEK_INIT1)) {
1148                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1149                         return PHY_ERROR;
1150                 }
1151         }
1152
1153         /* set advertise register */
1154         reg = mii_rw(dev, np->phyaddr, MII_ADVERTISE, MII_READ);
1155         reg |= (ADVERTISE_10HALF|ADVERTISE_10FULL|ADVERTISE_100HALF|ADVERTISE_100FULL|ADVERTISE_PAUSE_ASYM|ADVERTISE_PAUSE_CAP);
1156         if (mii_rw(dev, np->phyaddr, MII_ADVERTISE, reg)) {
1157                 printk(KERN_INFO "%s: phy write to advertise failed.\n", pci_name(np->pci_dev));
1158                 return PHY_ERROR;
1159         }
1160
1161         /* get phy interface type */
1162         phyinterface = readl(base + NvRegPhyInterface);
1163
1164         /* see if gigabit phy */
1165         mii_status = mii_rw(dev, np->phyaddr, MII_BMSR, MII_READ);
1166         if (mii_status & PHY_GIGABIT) {
1167                 np->gigabit = PHY_GIGABIT;
1168                 mii_control_1000 = mii_rw(dev, np->phyaddr, MII_CTRL1000, MII_READ);
1169                 mii_control_1000 &= ~ADVERTISE_1000HALF;
1170                 if (phyinterface & PHY_RGMII)
1171                         mii_control_1000 |= ADVERTISE_1000FULL;
1172                 else
1173                         mii_control_1000 &= ~ADVERTISE_1000FULL;
1174
1175                 if (mii_rw(dev, np->phyaddr, MII_CTRL1000, mii_control_1000)) {
1176                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1177                         return PHY_ERROR;
1178                 }
1179         }
1180         else
1181                 np->gigabit = 0;
1182
1183         mii_control = mii_rw(dev, np->phyaddr, MII_BMCR, MII_READ);
1184         mii_control |= BMCR_ANENABLE;
1185
1186         /* reset the phy
1187          * (certain phys need bmcr to be setup with reset)
1188          */
1189         if (phy_reset(dev, mii_control)) {
1190                 printk(KERN_INFO "%s: phy reset failed\n", pci_name(np->pci_dev));
1191                 return PHY_ERROR;
1192         }
1193
1194         /* phy vendor specific configuration */
1195         if ((np->phy_oui == PHY_OUI_CICADA) && (phyinterface & PHY_RGMII) ) {
1196                 phy_reserved = mii_rw(dev, np->phyaddr, MII_RESV1, MII_READ);
1197                 phy_reserved &= ~(PHY_CICADA_INIT1 | PHY_CICADA_INIT2);
1198                 phy_reserved |= (PHY_CICADA_INIT3 | PHY_CICADA_INIT4);
1199                 if (mii_rw(dev, np->phyaddr, MII_RESV1, phy_reserved)) {
1200                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1201                         return PHY_ERROR;
1202                 }
1203                 phy_reserved = mii_rw(dev, np->phyaddr, MII_NCONFIG, MII_READ);
1204                 phy_reserved |= PHY_CICADA_INIT5;
1205                 if (mii_rw(dev, np->phyaddr, MII_NCONFIG, phy_reserved)) {
1206                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1207                         return PHY_ERROR;
1208                 }
1209         }
1210         if (np->phy_oui == PHY_OUI_CICADA) {
1211                 phy_reserved = mii_rw(dev, np->phyaddr, MII_SREVISION, MII_READ);
1212                 phy_reserved |= PHY_CICADA_INIT6;
1213                 if (mii_rw(dev, np->phyaddr, MII_SREVISION, phy_reserved)) {
1214                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1215                         return PHY_ERROR;
1216                 }
1217         }
1218         if (np->phy_oui == PHY_OUI_VITESSE) {
1219                 if (mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG1, PHY_VITESSE_INIT1)) {
1220                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1221                         return PHY_ERROR;
1222                 }
1223                 if (mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG2, PHY_VITESSE_INIT2)) {
1224                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1225                         return PHY_ERROR;
1226                 }
1227                 phy_reserved = mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG4, MII_READ);
1228                 if (mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG4, phy_reserved)) {
1229                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1230                         return PHY_ERROR;
1231                 }
1232                 phy_reserved = mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG3, MII_READ);
1233                 phy_reserved &= ~PHY_VITESSE_INIT_MSK1;
1234                 phy_reserved |= PHY_VITESSE_INIT3;
1235                 if (mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG3, phy_reserved)) {
1236                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1237                         return PHY_ERROR;
1238                 }
1239                 if (mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG2, PHY_VITESSE_INIT4)) {
1240                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1241                         return PHY_ERROR;
1242                 }
1243                 if (mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG2, PHY_VITESSE_INIT5)) {
1244                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1245                         return PHY_ERROR;
1246                 }
1247                 phy_reserved = mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG4, MII_READ);
1248                 phy_reserved &= ~PHY_VITESSE_INIT_MSK1;
1249                 phy_reserved |= PHY_VITESSE_INIT3;
1250                 if (mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG4, phy_reserved)) {
1251                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1252                         return PHY_ERROR;
1253                 }
1254                 phy_reserved = mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG3, MII_READ);
1255                 if (mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG3, phy_reserved)) {
1256                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1257                         return PHY_ERROR;
1258                 }
1259                 if (mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG2, PHY_VITESSE_INIT6)) {
1260                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1261                         return PHY_ERROR;
1262                 }
1263                 if (mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG2, PHY_VITESSE_INIT7)) {
1264                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1265                         return PHY_ERROR;
1266                 }
1267                 phy_reserved = mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG4, MII_READ);
1268                 if (mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG4, phy_reserved)) {
1269                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1270                         return PHY_ERROR;
1271                 }
1272                 phy_reserved = mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG3, MII_READ);
1273                 phy_reserved &= ~PHY_VITESSE_INIT_MSK2;
1274                 phy_reserved |= PHY_VITESSE_INIT8;
1275                 if (mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG3, phy_reserved)) {
1276                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1277                         return PHY_ERROR;
1278                 }
1279                 if (mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG2, PHY_VITESSE_INIT9)) {
1280                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1281                         return PHY_ERROR;
1282                 }
1283                 if (mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG1, PHY_VITESSE_INIT10)) {
1284                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1285                         return PHY_ERROR;
1286                 }
1287         }
1288         if (np->phy_oui == PHY_OUI_REALTEK) {
1289                 /* reset could have cleared these out, set them back */
1290                 if (mii_rw(dev, np->phyaddr, PHY_REALTEK_INIT_REG1, PHY_REALTEK_INIT1)) {
1291                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1292                         return PHY_ERROR;
1293                 }
1294                 if (mii_rw(dev, np->phyaddr, PHY_REALTEK_INIT_REG2, PHY_REALTEK_INIT2)) {
1295                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1296                         return PHY_ERROR;
1297                 }
1298                 if (mii_rw(dev, np->phyaddr, PHY_REALTEK_INIT_REG1, PHY_REALTEK_INIT3)) {
1299                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1300                         return PHY_ERROR;
1301                 }
1302                 if (mii_rw(dev, np->phyaddr, PHY_REALTEK_INIT_REG3, PHY_REALTEK_INIT4)) {
1303                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1304                         return PHY_ERROR;
1305                 }
1306                 if (mii_rw(dev, np->phyaddr, PHY_REALTEK_INIT_REG1, PHY_REALTEK_INIT1)) {
1307                         printk(KERN_INFO "%s: phy init failed.\n", pci_name(np->pci_dev));
1308                         return PHY_ERROR;
1309                 }
1310         }
1311
1312         /* some phys clear out pause advertisment on reset, set it back */
1313         mii_rw(dev, np->phyaddr, MII_ADVERTISE, reg);
1314
1315         /* restart auto negotiation */
1316         mii_control = mii_rw(dev, np->phyaddr, MII_BMCR, MII_READ);
1317         mii_control |= (BMCR_ANRESTART | BMCR_ANENABLE);
1318         if (mii_rw(dev, np->phyaddr, MII_BMCR, mii_control)) {
1319                 return PHY_ERROR;
1320         }
1321
1322         return 0;
1323 }
1324
1325 static void nv_start_rx(struct net_device *dev)
1326 {
1327         struct fe_priv *np = netdev_priv(dev);
1328         u8 __iomem *base = get_hwbase(dev);
1329         u32 rx_ctrl = readl(base + NvRegReceiverControl);
1330
1331         dprintk(KERN_DEBUG "%s: nv_start_rx\n", dev->name);
1332         /* Already running? Stop it. */
1333         if ((readl(base + NvRegReceiverControl) & NVREG_RCVCTL_START) && !np->mac_in_use) {
1334                 rx_ctrl &= ~NVREG_RCVCTL_START;
1335                 writel(rx_ctrl, base + NvRegReceiverControl);
1336                 pci_push(base);
1337         }
1338         writel(np->linkspeed, base + NvRegLinkSpeed);
1339         pci_push(base);
1340         rx_ctrl |= NVREG_RCVCTL_START;
1341         if (np->mac_in_use)
1342                 rx_ctrl &= ~NVREG_RCVCTL_RX_PATH_EN;
1343         writel(rx_ctrl, base + NvRegReceiverControl);
1344         dprintk(KERN_DEBUG "%s: nv_start_rx to duplex %d, speed 0x%08x.\n",
1345                                 dev->name, np->duplex, np->linkspeed);
1346         pci_push(base);
1347 }
1348
1349 static void nv_stop_rx(struct net_device *dev)
1350 {
1351         struct fe_priv *np = netdev_priv(dev);
1352         u8 __iomem *base = get_hwbase(dev);
1353         u32 rx_ctrl = readl(base + NvRegReceiverControl);
1354
1355         dprintk(KERN_DEBUG "%s: nv_stop_rx\n", dev->name);
1356         if (!np->mac_in_use)
1357                 rx_ctrl &= ~NVREG_RCVCTL_START;
1358         else
1359                 rx_ctrl |= NVREG_RCVCTL_RX_PATH_EN;
1360         writel(rx_ctrl, base + NvRegReceiverControl);
1361         reg_delay(dev, NvRegReceiverStatus, NVREG_RCVSTAT_BUSY, 0,
1362                         NV_RXSTOP_DELAY1, NV_RXSTOP_DELAY1MAX,
1363                         KERN_INFO "nv_stop_rx: ReceiverStatus remained busy");
1364
1365         udelay(NV_RXSTOP_DELAY2);
1366         if (!np->mac_in_use)
1367                 writel(0, base + NvRegLinkSpeed);
1368 }
1369
1370 static void nv_start_tx(struct net_device *dev)
1371 {
1372         struct fe_priv *np = netdev_priv(dev);
1373         u8 __iomem *base = get_hwbase(dev);
1374         u32 tx_ctrl = readl(base + NvRegTransmitterControl);
1375
1376         dprintk(KERN_DEBUG "%s: nv_start_tx\n", dev->name);
1377         tx_ctrl |= NVREG_XMITCTL_START;
1378         if (np->mac_in_use)
1379                 tx_ctrl &= ~NVREG_XMITCTL_TX_PATH_EN;
1380         writel(tx_ctrl, base + NvRegTransmitterControl);
1381         pci_push(base);
1382 }
1383
1384 static void nv_stop_tx(struct net_device *dev)
1385 {
1386         struct fe_priv *np = netdev_priv(dev);
1387         u8 __iomem *base = get_hwbase(dev);
1388         u32 tx_ctrl = readl(base + NvRegTransmitterControl);
1389
1390         dprintk(KERN_DEBUG "%s: nv_stop_tx\n", dev->name);
1391         if (!np->mac_in_use)
1392                 tx_ctrl &= ~NVREG_XMITCTL_START;
1393         else
1394                 tx_ctrl |= NVREG_XMITCTL_TX_PATH_EN;
1395         writel(tx_ctrl, base + NvRegTransmitterControl);
1396         reg_delay(dev, NvRegTransmitterStatus, NVREG_XMITSTAT_BUSY, 0,
1397                         NV_TXSTOP_DELAY1, NV_TXSTOP_DELAY1MAX,
1398                         KERN_INFO "nv_stop_tx: TransmitterStatus remained busy");
1399
1400         udelay(NV_TXSTOP_DELAY2);
1401         if (!np->mac_in_use)
1402                 writel(readl(base + NvRegTransmitPoll) & NVREG_TRANSMITPOLL_MAC_ADDR_REV,
1403                        base + NvRegTransmitPoll);
1404 }
1405
1406 static void nv_txrx_reset(struct net_device *dev)
1407 {
1408         struct fe_priv *np = netdev_priv(dev);
1409         u8 __iomem *base = get_hwbase(dev);
1410
1411         dprintk(KERN_DEBUG "%s: nv_txrx_reset\n", dev->name);
1412         writel(NVREG_TXRXCTL_BIT2 | NVREG_TXRXCTL_RESET | np->txrxctl_bits, base + NvRegTxRxControl);
1413         pci_push(base);
1414         udelay(NV_TXRX_RESET_DELAY);
1415         writel(NVREG_TXRXCTL_BIT2 | np->txrxctl_bits, base + NvRegTxRxControl);
1416         pci_push(base);
1417 }
1418
1419 static void nv_mac_reset(struct net_device *dev)
1420 {
1421         struct fe_priv *np = netdev_priv(dev);
1422         u8 __iomem *base = get_hwbase(dev);
1423
1424         dprintk(KERN_DEBUG "%s: nv_mac_reset\n", dev->name);
1425         writel(NVREG_TXRXCTL_BIT2 | NVREG_TXRXCTL_RESET | np->txrxctl_bits, base + NvRegTxRxControl);
1426         pci_push(base);
1427         writel(NVREG_MAC_RESET_ASSERT, base + NvRegMacReset);
1428         pci_push(base);
1429         udelay(NV_MAC_RESET_DELAY);
1430         writel(0, base + NvRegMacReset);
1431         pci_push(base);
1432         udelay(NV_MAC_RESET_DELAY);
1433         writel(NVREG_TXRXCTL_BIT2 | np->txrxctl_bits, base + NvRegTxRxControl);
1434         pci_push(base);
1435 }
1436
1437 static void nv_get_hw_stats(struct net_device *dev)
1438 {
1439         struct fe_priv *np = netdev_priv(dev);
1440         u8 __iomem *base = get_hwbase(dev);
1441
1442         np->estats.tx_bytes += readl(base + NvRegTxCnt);
1443         np->estats.tx_zero_rexmt += readl(base + NvRegTxZeroReXmt);
1444         np->estats.tx_one_rexmt += readl(base + NvRegTxOneReXmt);
1445         np->estats.tx_many_rexmt += readl(base + NvRegTxManyReXmt);
1446         np->estats.tx_late_collision += readl(base + NvRegTxLateCol);
1447         np->estats.tx_fifo_errors += readl(base + NvRegTxUnderflow);
1448         np->estats.tx_carrier_errors += readl(base + NvRegTxLossCarrier);
1449         np->estats.tx_excess_deferral += readl(base + NvRegTxExcessDef);
1450         np->estats.tx_retry_error += readl(base + NvRegTxRetryErr);
1451         np->estats.rx_frame_error += readl(base + NvRegRxFrameErr);
1452         np->estats.rx_extra_byte += readl(base + NvRegRxExtraByte);
1453         np->estats.rx_late_collision += readl(base + NvRegRxLateCol);
1454         np->estats.rx_runt += readl(base + NvRegRxRunt);
1455         np->estats.rx_frame_too_long += readl(base + NvRegRxFrameTooLong);
1456         np->estats.rx_over_errors += readl(base + NvRegRxOverflow);
1457         np->estats.rx_crc_errors += readl(base + NvRegRxFCSErr);
1458         np->estats.rx_frame_align_error += readl(base + NvRegRxFrameAlignErr);
1459         np->estats.rx_length_error += readl(base + NvRegRxLenErr);
1460         np->estats.rx_unicast += readl(base + NvRegRxUnicast);
1461         np->estats.rx_multicast += readl(base + NvRegRxMulticast);
1462         np->estats.rx_broadcast += readl(base + NvRegRxBroadcast);
1463         np->estats.rx_packets =
1464                 np->estats.rx_unicast +
1465                 np->estats.rx_multicast +
1466                 np->estats.rx_broadcast;
1467         np->estats.rx_errors_total =
1468                 np->estats.rx_crc_errors +
1469                 np->estats.rx_over_errors +
1470                 np->estats.rx_frame_error +
1471                 (np->estats.rx_frame_align_error - np->estats.rx_extra_byte) +
1472                 np->estats.rx_late_collision +
1473                 np->estats.rx_runt +
1474                 np->estats.rx_frame_too_long;
1475         np->estats.tx_errors_total =
1476                 np->estats.tx_late_collision +
1477                 np->estats.tx_fifo_errors +
1478                 np->estats.tx_carrier_errors +
1479                 np->estats.tx_excess_deferral +
1480                 np->estats.tx_retry_error;
1481
1482         if (np->driver_data & DEV_HAS_STATISTICS_V2) {
1483                 np->estats.tx_deferral += readl(base + NvRegTxDef);
1484                 np->estats.tx_packets += readl(base + NvRegTxFrame);
1485                 np->estats.rx_bytes += readl(base + NvRegRxCnt);
1486                 np->estats.tx_pause += readl(base + NvRegTxPause);
1487                 np->estats.rx_pause += readl(base + NvRegRxPause);
1488                 np->estats.rx_drop_frame += readl(base + NvRegRxDropFrame);
1489         }
1490 }
1491
1492 /*
1493  * nv_get_stats: dev->get_stats function
1494  * Get latest stats value from the nic.
1495  * Called with read_lock(&dev_base_lock) held for read -
1496  * only synchronized against unregister_netdevice.
1497  */
1498 static struct net_device_stats *nv_get_stats(struct net_device *dev)
1499 {
1500         struct fe_priv *np = netdev_priv(dev);
1501
1502         /* If the nic supports hw counters then retrieve latest values */
1503         if (np->driver_data & (DEV_HAS_STATISTICS_V1|DEV_HAS_STATISTICS_V2)) {
1504                 nv_get_hw_stats(dev);
1505
1506                 /* copy to net_device stats */
1507                 dev->stats.tx_bytes = np->estats.tx_bytes;
1508                 dev->stats.tx_fifo_errors = np->estats.tx_fifo_errors;
1509                 dev->stats.tx_carrier_errors = np->estats.tx_carrier_errors;
1510                 dev->stats.rx_crc_errors = np->estats.rx_crc_errors;
1511                 dev->stats.rx_over_errors = np->estats.rx_over_errors;
1512                 dev->stats.rx_errors = np->estats.rx_errors_total;
1513                 dev->stats.tx_errors = np->estats.tx_errors_total;
1514         }
1515
1516         return &dev->stats;
1517 }
1518
1519 /*
1520  * nv_alloc_rx: fill rx ring entries.
1521  * Return 1 if the allocations for the skbs failed and the
1522  * rx engine is without Available descriptors
1523  */
1524 static int nv_alloc_rx(struct net_device *dev)
1525 {
1526         struct fe_priv *np = netdev_priv(dev);
1527         struct ring_desc* less_rx;
1528
1529         less_rx = np->get_rx.orig;
1530         if (less_rx-- == np->first_rx.orig)
1531                 less_rx = np->last_rx.orig;
1532
1533         while (np->put_rx.orig != less_rx) {
1534                 struct sk_buff *skb = dev_alloc_skb(np->rx_buf_sz + NV_RX_ALLOC_PAD);
1535                 if (skb) {
1536                         np->put_rx_ctx->skb = skb;
1537                         np->put_rx_ctx->dma = pci_map_single(np->pci_dev,
1538                                                              skb->data,
1539                                                              skb_tailroom(skb),
1540                                                              PCI_DMA_FROMDEVICE);
1541                         np->put_rx_ctx->dma_len = skb_tailroom(skb);
1542                         np->put_rx.orig->buf = cpu_to_le32(np->put_rx_ctx->dma);
1543                         wmb();
1544                         np->put_rx.orig->flaglen = cpu_to_le32(np->rx_buf_sz | NV_RX_AVAIL);
1545                         if (unlikely(np->put_rx.orig++ == np->last_rx.orig))
1546                                 np->put_rx.orig = np->first_rx.orig;
1547                         if (unlikely(np->put_rx_ctx++ == np->last_rx_ctx))
1548                                 np->put_rx_ctx = np->first_rx_ctx;
1549                 } else {
1550                         return 1;
1551                 }
1552         }
1553         return 0;
1554 }
1555
1556 static int nv_alloc_rx_optimized(struct net_device *dev)
1557 {
1558         struct fe_priv *np = netdev_priv(dev);
1559         struct ring_desc_ex* less_rx;
1560
1561         less_rx = np->get_rx.ex;
1562         if (less_rx-- == np->first_rx.ex)
1563                 less_rx = np->last_rx.ex;
1564
1565         while (np->put_rx.ex != less_rx) {
1566                 struct sk_buff *skb = dev_alloc_skb(np->rx_buf_sz + NV_RX_ALLOC_PAD);
1567                 if (skb) {
1568                         np->put_rx_ctx->skb = skb;
1569                         np->put_rx_ctx->dma = pci_map_single(np->pci_dev,
1570                                                              skb->data,
1571                                                              skb_tailroom(skb),
1572                                                              PCI_DMA_FROMDEVICE);
1573                         np->put_rx_ctx->dma_len = skb_tailroom(skb);
1574                         np->put_rx.ex->bufhigh = cpu_to_le64(np->put_rx_ctx->dma) >> 32;
1575                         np->put_rx.ex->buflow = cpu_to_le64(np->put_rx_ctx->dma) & 0x0FFFFFFFF;
1576                         wmb();
1577                         np->put_rx.ex->flaglen = cpu_to_le32(np->rx_buf_sz | NV_RX2_AVAIL);
1578                         if (unlikely(np->put_rx.ex++ == np->last_rx.ex))
1579                                 np->put_rx.ex = np->first_rx.ex;
1580                         if (unlikely(np->put_rx_ctx++ == np->last_rx_ctx))
1581                                 np->put_rx_ctx = np->first_rx_ctx;
1582                 } else {
1583                         return 1;
1584                 }
1585         }
1586         return 0;
1587 }
1588
1589 /* If rx bufs are exhausted called after 50ms to attempt to refresh */
1590 #ifdef CONFIG_FORCEDETH_NAPI
1591 static void nv_do_rx_refill(unsigned long data)
1592 {
1593         struct net_device *dev = (struct net_device *) data;
1594         struct fe_priv *np = netdev_priv(dev);
1595
1596         /* Just reschedule NAPI rx processing */
1597         netif_rx_schedule(dev, &np->napi);
1598 }
1599 #else
1600 static void nv_do_rx_refill(unsigned long data)
1601 {
1602         struct net_device *dev = (struct net_device *) data;
1603         struct fe_priv *np = netdev_priv(dev);
1604         int retcode;
1605
1606         if (!using_multi_irqs(dev)) {
1607                 if (np->msi_flags & NV_MSI_X_ENABLED)
1608                         disable_irq(np->msi_x_entry[NV_MSI_X_VECTOR_ALL].vector);
1609                 else
1610                         disable_irq(dev->irq);
1611         } else {
1612                 disable_irq(np->msi_x_entry[NV_MSI_X_VECTOR_RX].vector);
1613         }
1614         if (np->desc_ver == DESC_VER_1 || np->desc_ver == DESC_VER_2)
1615                 retcode = nv_alloc_rx(dev);
1616         else
1617                 retcode = nv_alloc_rx_optimized(dev);
1618         if (retcode) {
1619                 spin_lock_irq(&np->lock);
1620                 if (!np->in_shutdown)
1621                         mod_timer(&np->oom_kick, jiffies + OOM_REFILL);
1622                 spin_unlock_irq(&np->lock);
1623         }
1624         if (!using_multi_irqs(dev)) {
1625                 if (np->msi_flags & NV_MSI_X_ENABLED)
1626                         enable_irq(np->msi_x_entry[NV_MSI_X_VECTOR_ALL].vector);
1627                 else
1628                         enable_irq(dev->irq);
1629         } else {
1630                 enable_irq(np->msi_x_entry[NV_MSI_X_VECTOR_RX].vector);
1631         }
1632 }
1633 #endif
1634
1635 static void nv_init_rx(struct net_device *dev)
1636 {
1637         struct fe_priv *np = netdev_priv(dev);
1638         int i;
1639         np->get_rx = np->put_rx = np->first_rx = np->rx_ring;
1640         if (np->desc_ver == DESC_VER_1 || np->desc_ver == DESC_VER_2)
1641                 np->last_rx.orig = &np->rx_ring.orig[np->rx_ring_size-1];
1642         else
1643                 np->last_rx.ex = &np->rx_ring.ex[np->rx_ring_size-1];
1644         np->get_rx_ctx = np->put_rx_ctx = np->first_rx_ctx = np->rx_skb;
1645         np->last_rx_ctx = &np->rx_skb[np->rx_ring_size-1];
1646
1647         for (i = 0; i < np->rx_ring_size; i++) {
1648                 if (np->desc_ver == DESC_VER_1 || np->desc_ver == DESC_VER_2) {
1649                         np->rx_ring.orig[i].flaglen = 0;
1650                         np->rx_ring.orig[i].buf = 0;
1651                 } else {
1652                         np->rx_ring.ex[i].flaglen = 0;
1653                         np->rx_ring.ex[i].txvlan = 0;
1654                         np->rx_ring.ex[i].bufhigh = 0;
1655                         np->rx_ring.ex[i].buflow = 0;
1656                 }
1657                 np->rx_skb[i].skb = NULL;
1658                 np->rx_skb[i].dma = 0;
1659         }
1660 }
1661
1662 static void nv_init_tx(struct net_device *dev)
1663 {
1664         struct fe_priv *np = netdev_priv(dev);
1665         int i;
1666         np->get_tx = np->put_tx = np->first_tx = np->tx_ring;
1667         if (np->desc_ver == DESC_VER_1 || np->desc_ver == DESC_VER_2)
1668                 np->last_tx.orig = &np->tx_ring.orig[np->tx_ring_size-1];
1669         else
1670                 np->last_tx.ex = &np->tx_ring.ex[np->tx_ring_size-1];
1671         np->get_tx_ctx = np->put_tx_ctx = np->first_tx_ctx = np->tx_skb;
1672         np->last_tx_ctx = &np->tx_skb[np->tx_ring_size-1];
1673
1674         for (i = 0; i < np->tx_ring_size; i++) {
1675                 if (np->desc_ver == DESC_VER_1 || np->desc_ver == DESC_VER_2) {
1676                         np->tx_ring.orig[i].flaglen = 0;
1677                         np->tx_ring.orig[i].buf = 0;
1678                 } else {
1679                         np->tx_ring.ex[i].flaglen = 0;
1680                         np->tx_ring.ex[i].txvlan = 0;
1681                         np->tx_ring.ex[i].bufhigh = 0;
1682                         np->tx_ring.ex[i].buflow = 0;
1683                 }
1684                 np->tx_skb[i].skb = NULL;
1685                 np->tx_skb[i].dma = 0;
1686         }
1687 }
1688
1689 static int nv_init_ring(struct net_device *dev)
1690 {
1691         struct fe_priv *np = netdev_priv(dev);
1692
1693         nv_init_tx(dev);
1694         nv_init_rx(dev);
1695         if (np->desc_ver == DESC_VER_1 || np->desc_ver == DESC_VER_2)
1696                 return nv_alloc_rx(dev);
1697         else
1698                 return nv_alloc_rx_optimized(dev);
1699 }
1700
1701 static int nv_release_txskb(struct net_device *dev, struct nv_skb_map* tx_skb)
1702 {
1703         struct fe_priv *np = netdev_priv(dev);
1704
1705         if (tx_skb->dma) {
1706                 pci_unmap_page(np->pci_dev, tx_skb->dma,
1707                                tx_skb->dma_len,
1708                                PCI_DMA_TODEVICE);
1709                 tx_skb->dma = 0;
1710         }
1711         if (tx_skb->skb) {
1712                 dev_kfree_skb_any(tx_skb->skb);
1713                 tx_skb->skb = NULL;
1714                 return 1;
1715         } else {
1716                 return 0;
1717         }
1718 }
1719
1720 static void nv_drain_tx(struct net_device *dev)
1721 {
1722         struct fe_priv *np = netdev_priv(dev);
1723         unsigned int i;
1724
1725         for (i = 0; i < np->tx_ring_size; i++) {
1726                 if (np->desc_ver == DESC_VER_1 || np->desc_ver == DESC_VER_2) {
1727                         np->tx_ring.orig[i].flaglen = 0;
1728                         np->tx_ring.orig[i].buf = 0;
1729                 } else {
1730                         np->tx_ring.ex[i].flaglen = 0;
1731                         np->tx_ring.ex[i].txvlan = 0;
1732                         np->tx_ring.ex[i].bufhigh = 0;
1733                         np->tx_ring.ex[i].buflow = 0;
1734                 }
1735                 if (nv_release_txskb(dev, &np->tx_skb[i]))
1736                         dev->stats.tx_dropped++;
1737         }
1738 }
1739
1740 static void nv_drain_rx(struct net_device *dev)
1741 {
1742         struct fe_priv *np = netdev_priv(dev);
1743         int i;
1744
1745         for (i = 0; i < np->rx_ring_size; i++) {
1746                 if (np->desc_ver == DESC_VER_1 || np->desc_ver == DESC_VER_2) {
1747                         np->rx_ring.orig[i].flaglen = 0;
1748                         np->rx_ring.orig[i].buf = 0;
1749                 } else {
1750                         np->rx_ring.ex[i].flaglen = 0;
1751                         np->rx_ring.ex[i].txvlan = 0;
1752                         np->rx_ring.ex[i].bufhigh = 0;
1753                         np->rx_ring.ex[i].buflow = 0;
1754                 }
1755                 wmb();
1756                 if (np->rx_skb[i].skb) {
1757                         pci_unmap_single(np->pci_dev, np->rx_skb[i].dma,
1758                                          (skb_end_pointer(np->rx_skb[i].skb) -
1759                                           np->rx_skb[i].skb->data),
1760                                          PCI_DMA_FROMDEVICE);
1761                         dev_kfree_skb(np->rx_skb[i].skb);
1762                         np->rx_skb[i].skb = NULL;
1763                 }
1764         }
1765 }
1766
1767 static void drain_ring(struct net_device *dev)
1768 {
1769         nv_drain_tx(dev);
1770         nv_drain_rx(dev);
1771 }
1772
1773 static inline u32 nv_get_empty_tx_slots(struct fe_priv *np)
1774 {
1775         return (u32)(np->tx_ring_size - ((np->tx_ring_size + (np->put_tx_ctx - np->get_tx_ctx)) % np->tx_ring_size));
1776 }
1777
1778 /*
1779  * nv_start_xmit: dev->hard_start_xmit function
1780  * Called with netif_tx_lock held.
1781  */
1782 static int nv_start_xmit(struct sk_buff *skb, struct net_device *dev)
1783 {
1784         struct fe_priv *np = netdev_priv(dev);
1785         u32 tx_flags = 0;
1786         u32 tx_flags_extra = (np->desc_ver == DESC_VER_1 ? NV_TX_LASTPACKET : NV_TX2_LASTPACKET);
1787         unsigned int fragments = skb_shinfo(skb)->nr_frags;
1788         unsigned int i;
1789         u32 offset = 0;
1790         u32 bcnt;
1791         u32 size = skb->len-skb->data_len;
1792         u32 entries = (size >> NV_TX2_TSO_MAX_SHIFT) + ((size & (NV_TX2_TSO_MAX_SIZE-1)) ? 1 : 0);
1793         u32 empty_slots;
1794         struct ring_desc* put_tx;
1795         struct ring_desc* start_tx;
1796         struct ring_desc* prev_tx;
1797         struct nv_skb_map* prev_tx_ctx;
1798
1799         /* add fragments to entries count */
1800         for (i = 0; i < fragments; i++) {
1801                 entries += (skb_shinfo(skb)->frags[i].size >> NV_TX2_TSO_MAX_SHIFT) +
1802                            ((skb_shinfo(skb)->frags[i].size & (NV_TX2_TSO_MAX_SIZE-1)) ? 1 : 0);
1803         }
1804
1805         empty_slots = nv_get_empty_tx_slots(np);
1806         if (unlikely(empty_slots <= entries)) {
1807                 spin_lock_irq(&np->lock);
1808                 netif_stop_queue(dev);
1809                 np->tx_stop = 1;
1810                 spin_unlock_irq(&np->lock);
1811                 return NETDEV_TX_BUSY;
1812         }
1813
1814         start_tx = put_tx = np->put_tx.orig;
1815
1816         /* setup the header buffer */
1817         do {
1818                 prev_tx = put_tx;
1819                 prev_tx_ctx = np->put_tx_ctx;
1820                 bcnt = (size > NV_TX2_TSO_MAX_SIZE) ? NV_TX2_TSO_MAX_SIZE : size;
1821                 np->put_tx_ctx->dma = pci_map_single(np->pci_dev, skb->data + offset, bcnt,
1822                                                 PCI_DMA_TODEVICE);
1823                 np->put_tx_ctx->dma_len = bcnt;
1824                 put_tx->buf = cpu_to_le32(np->put_tx_ctx->dma);
1825                 put_tx->flaglen = cpu_to_le32((bcnt-1) | tx_flags);
1826
1827                 tx_flags = np->tx_flags;
1828                 offset += bcnt;
1829                 size -= bcnt;
1830                 if (unlikely(put_tx++ == np->last_tx.orig))
1831                         put_tx = np->first_tx.orig;
1832                 if (unlikely(np->put_tx_ctx++ == np->last_tx_ctx))
1833                         np->put_tx_ctx = np->first_tx_ctx;
1834         } while (size);
1835
1836         /* setup the fragments */
1837         for (i = 0; i < fragments; i++) {
1838                 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1839                 u32 size = frag->size;
1840                 offset = 0;
1841
1842                 do {
1843                         prev_tx = put_tx;
1844                         prev_tx_ctx = np->put_tx_ctx;
1845                         bcnt = (size > NV_TX2_TSO_MAX_SIZE) ? NV_TX2_TSO_MAX_SIZE : size;
1846                         np->put_tx_ctx->dma = pci_map_page(np->pci_dev, frag->page, frag->page_offset+offset, bcnt,
1847                                                            PCI_DMA_TODEVICE);
1848                         np->put_tx_ctx->dma_len = bcnt;
1849                         put_tx->buf = cpu_to_le32(np->put_tx_ctx->dma);
1850                         put_tx->flaglen = cpu_to_le32((bcnt-1) | tx_flags);
1851
1852                         offset += bcnt;
1853                         size -= bcnt;
1854                         if (unlikely(put_tx++ == np->last_tx.orig))
1855                                 put_tx = np->first_tx.orig;
1856                         if (unlikely(np->put_tx_ctx++ == np->last_tx_ctx))
1857                                 np->put_tx_ctx = np->first_tx_ctx;
1858                 } while (size);
1859         }
1860
1861         /* set last fragment flag  */
1862         prev_tx->flaglen |= cpu_to_le32(tx_flags_extra);
1863
1864         /* save skb in this slot's context area */
1865         prev_tx_ctx->skb = skb;
1866
1867         if (skb_is_gso(skb))
1868                 tx_flags_extra = NV_TX2_TSO | (skb_shinfo(skb)->gso_size << NV_TX2_TSO_SHIFT);
1869         else
1870                 tx_flags_extra = skb->ip_summed == CHECKSUM_PARTIAL ?
1871                          NV_TX2_CHECKSUM_L3 | NV_TX2_CHECKSUM_L4 : 0;
1872
1873         spin_lock_irq(&np->lock);
1874
1875         /* set tx flags */
1876         start_tx->flaglen |= cpu_to_le32(tx_flags | tx_flags_extra);
1877         np->put_tx.orig = put_tx;
1878
1879         spin_unlock_irq(&np->lock);
1880
1881         dprintk(KERN_DEBUG "%s: nv_start_xmit: entries %d queued for transmission. tx_flags_extra: %x\n",
1882                 dev->name, entries, tx_flags_extra);
1883         {
1884                 int j;
1885                 for (j=0; j<64; j++) {
1886                         if ((j%16) == 0)
1887                                 dprintk("\n%03x:", j);
1888                         dprintk(" %02x", ((unsigned char*)skb->data)[j]);
1889                 }
1890                 dprintk("\n");
1891         }
1892
1893         dev->trans_start = jiffies;
1894         writel(NVREG_TXRXCTL_KICK|np->txrxctl_bits, get_hwbase(dev) + NvRegTxRxControl);
1895         return NETDEV_TX_OK;
1896 }
1897
1898 static int nv_start_xmit_optimized(struct sk_buff *skb, struct net_device *dev)
1899 {
1900         struct fe_priv *np = netdev_priv(dev);
1901         u32 tx_flags = 0;
1902         u32 tx_flags_extra;
1903         unsigned int fragments = skb_shinfo(skb)->nr_frags;
1904         unsigned int i;
1905         u32 offset = 0;
1906         u32 bcnt;
1907         u32 size = skb->len-skb->data_len;
1908         u32 entries = (size >> NV_TX2_TSO_MAX_SHIFT) + ((size & (NV_TX2_TSO_MAX_SIZE-1)) ? 1 : 0);
1909         u32 empty_slots;
1910         struct ring_desc_ex* put_tx;
1911         struct ring_desc_ex* start_tx;
1912         struct ring_desc_ex* prev_tx;
1913         struct nv_skb_map* prev_tx_ctx;
1914
1915         /* add fragments to entries count */
1916         for (i = 0; i < fragments; i++) {
1917                 entries += (skb_shinfo(skb)->frags[i].size >> NV_TX2_TSO_MAX_SHIFT) +
1918                            ((skb_shinfo(skb)->frags[i].size & (NV_TX2_TSO_MAX_SIZE-1)) ? 1 : 0);
1919         }
1920
1921         empty_slots = nv_get_empty_tx_slots(np);
1922         if (unlikely(empty_slots <= entries)) {
1923                 spin_lock_irq(&np->lock);
1924                 netif_stop_queue(dev);
1925                 np->tx_stop = 1;
1926                 spin_unlock_irq(&np->lock);
1927                 return NETDEV_TX_BUSY;
1928         }
1929
1930         start_tx = put_tx = np->put_tx.ex;
1931
1932         /* setup the header buffer */
1933         do {
1934                 prev_tx = put_tx;
1935                 prev_tx_ctx = np->put_tx_ctx;
1936                 bcnt = (size > NV_TX2_TSO_MAX_SIZE) ? NV_TX2_TSO_MAX_SIZE : size;
1937                 np->put_tx_ctx->dma = pci_map_single(np->pci_dev, skb->data + offset, bcnt,
1938                                                 PCI_DMA_TODEVICE);
1939                 np->put_tx_ctx->dma_len = bcnt;
1940                 put_tx->bufhigh = cpu_to_le64(np->put_tx_ctx->dma) >> 32;
1941                 put_tx->buflow = cpu_to_le64(np->put_tx_ctx->dma) & 0x0FFFFFFFF;
1942                 put_tx->flaglen = cpu_to_le32((bcnt-1) | tx_flags);
1943
1944                 tx_flags = NV_TX2_VALID;
1945                 offset += bcnt;
1946                 size -= bcnt;
1947                 if (unlikely(put_tx++ == np->last_tx.ex))
1948                         put_tx = np->first_tx.ex;
1949                 if (unlikely(np->put_tx_ctx++ == np->last_tx_ctx))
1950                         np->put_tx_ctx = np->first_tx_ctx;
1951         } while (size);
1952
1953         /* setup the fragments */
1954         for (i = 0; i < fragments; i++) {
1955                 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1956                 u32 size = frag->size;
1957                 offset = 0;
1958
1959                 do {
1960                         prev_tx = put_tx;
1961                         prev_tx_ctx = np->put_tx_ctx;
1962                         bcnt = (size > NV_TX2_TSO_MAX_SIZE) ? NV_TX2_TSO_MAX_SIZE : size;
1963                         np->put_tx_ctx->dma = pci_map_page(np->pci_dev, frag->page, frag->page_offset+offset, bcnt,
1964                                                            PCI_DMA_TODEVICE);
1965                         np->put_tx_ctx->dma_len = bcnt;
1966                         put_tx->bufhigh = cpu_to_le64(np->put_tx_ctx->dma) >> 32;
1967                         put_tx->buflow = cpu_to_le64(np->put_tx_ctx->dma) & 0x0FFFFFFFF;
1968                         put_tx->flaglen = cpu_to_le32((bcnt-1) | tx_flags);
1969
1970                         offset += bcnt;
1971                         size -= bcnt;
1972                         if (unlikely(put_tx++ == np->last_tx.ex))
1973                                 put_tx = np->first_tx.ex;
1974                         if (unlikely(np->put_tx_ctx++ == np->last_tx_ctx))
1975                                 np->put_tx_ctx = np->first_tx_ctx;
1976                 } while (size);
1977         }
1978
1979         /* set last fragment flag  */
1980         prev_tx->flaglen |= cpu_to_le32(NV_TX2_LASTPACKET);
1981
1982         /* save skb in this slot's context area */
1983         prev_tx_ctx->skb = skb;
1984
1985         if (skb_is_gso(skb))
1986                 tx_flags_extra = NV_TX2_TSO | (skb_shinfo(skb)->gso_size << NV_TX2_TSO_SHIFT);
1987         else
1988                 tx_flags_extra = skb->ip_summed == CHECKSUM_PARTIAL ?
1989                          NV_TX2_CHECKSUM_L3 | NV_TX2_CHECKSUM_L4 : 0;
1990
1991         /* vlan tag */
1992         if (likely(!np->vlangrp)) {
1993                 start_tx->txvlan = 0;
1994         } else {
1995                 if (vlan_tx_tag_present(skb))
1996                         start_tx->txvlan = cpu_to_le32(NV_TX3_VLAN_TAG_PRESENT | vlan_tx_tag_get(skb));
1997                 else
1998                         start_tx->txvlan = 0;
1999         }
2000
2001         spin_lock_irq(&np->lock);
2002
2003         /* set tx flags */
2004         start_tx->flaglen |= cpu_to_le32(tx_flags | tx_flags_extra);
2005         np->put_tx.ex = put_tx;
2006
2007         spin_unlock_irq(&np->lock);
2008
2009         dprintk(KERN_DEBUG "%s: nv_start_xmit_optimized: entries %d queued for transmission. tx_flags_extra: %x\n",
2010                 dev->name, entries, tx_flags_extra);
2011         {
2012                 int j;
2013                 for (j=0; j<64; j++) {
2014                         if ((j%16) == 0)
2015                                 dprintk("\n%03x:", j);
2016                         dprintk(" %02x", ((unsigned char*)skb->data)[j]);
2017                 }
2018                 dprintk("\n");
2019         }
2020
2021         dev->trans_start = jiffies;
2022         writel(NVREG_TXRXCTL_KICK|np->txrxctl_bits, get_hwbase(dev) + NvRegTxRxControl);
2023         return NETDEV_TX_OK;
2024 }
2025
2026 /*
2027  * nv_tx_done: check for completed packets, release the skbs.
2028  *
2029  * Caller must own np->lock.
2030  */
2031 static void nv_tx_done(struct net_device *dev)
2032 {
2033         struct fe_priv *np = netdev_priv(dev);
2034         u32 flags;
2035         struct ring_desc* orig_get_tx = np->get_tx.orig;
2036
2037         while ((np->get_tx.orig != np->put_tx.orig) &&
2038                !((flags = le32_to_cpu(np->get_tx.orig->flaglen)) & NV_TX_VALID)) {
2039
2040                 dprintk(KERN_DEBUG "%s: nv_tx_done: flags 0x%x.\n",
2041                                         dev->name, flags);
2042
2043                 pci_unmap_page(np->pci_dev, np->get_tx_ctx->dma,
2044                                np->get_tx_ctx->dma_len,
2045                                PCI_DMA_TODEVICE);
2046                 np->get_tx_ctx->dma = 0;
2047
2048                 if (np->desc_ver == DESC_VER_1) {
2049                         if (flags & NV_TX_LASTPACKET) {
2050                                 if (flags & NV_TX_ERROR) {
2051                                         if (flags & NV_TX_UNDERFLOW)
2052                                                 dev->stats.tx_fifo_errors++;
2053                                         if (flags & NV_TX_CARRIERLOST)
2054                                                 dev->stats.tx_carrier_errors++;
2055                                         dev->stats.tx_errors++;
2056                                 } else {
2057                                         dev->stats.tx_packets++;
2058                                         dev->stats.tx_bytes += np->get_tx_ctx->skb->len;
2059                                 }
2060                                 dev_kfree_skb_any(np->get_tx_ctx->skb);
2061                                 np->get_tx_ctx->skb = NULL;
2062                         }
2063                 } else {
2064                         if (flags & NV_TX2_LASTPACKET) {
2065                                 if (flags & NV_TX2_ERROR) {
2066                                         if (flags & NV_TX2_UNDERFLOW)
2067                                                 dev->stats.tx_fifo_errors++;
2068                                         if (flags & NV_TX2_CARRIERLOST)
2069                                                 dev->stats.tx_carrier_errors++;
2070                                         dev->stats.tx_errors++;
2071                                 } else {
2072                                         dev->stats.tx_packets++;
2073                                         dev->stats.tx_bytes += np->get_tx_ctx->skb->len;
2074                                 }
2075                                 dev_kfree_skb_any(np->get_tx_ctx->skb);
2076                                 np->get_tx_ctx->skb = NULL;
2077                         }
2078                 }
2079                 if (unlikely(np->get_tx.orig++ == np->last_tx.orig))
2080                         np->get_tx.orig = np->first_tx.orig;
2081                 if (unlikely(np->get_tx_ctx++ == np->last_tx_ctx))
2082                         np->get_tx_ctx = np->first_tx_ctx;
2083         }
2084         if (unlikely((np->tx_stop == 1) && (np->get_tx.orig != orig_get_tx))) {
2085                 np->tx_stop = 0;
2086                 netif_wake_queue(dev);
2087         }
2088 }
2089
2090 static void nv_tx_done_optimized(struct net_device *dev, int limit)
2091 {
2092         struct fe_priv *np = netdev_priv(dev);
2093         u32 flags;
2094         struct ring_desc_ex* orig_get_tx = np->get_tx.ex;
2095
2096         while ((np->get_tx.ex != np->put_tx.ex) &&
2097                !((flags = le32_to_cpu(np->get_tx.ex->flaglen)) & NV_TX_VALID) &&
2098                (limit-- > 0)) {
2099
2100                 dprintk(KERN_DEBUG "%s: nv_tx_done_optimized: flags 0x%x.\n",
2101                                         dev->name, flags);
2102
2103                 pci_unmap_page(np->pci_dev, np->get_tx_ctx->dma,
2104                                np->get_tx_ctx->dma_len,
2105                                PCI_DMA_TODEVICE);
2106                 np->get_tx_ctx->dma = 0;
2107
2108                 if (flags & NV_TX2_LASTPACKET) {
2109                         if (!(flags & NV_TX2_ERROR))
2110                                 dev->stats.tx_packets++;
2111                         dev_kfree_skb_any(np->get_tx_ctx->skb);
2112                         np->get_tx_ctx->skb = NULL;
2113                 }
2114                 if (unlikely(np->get_tx.ex++ == np->last_tx.ex))
2115                         np->get_tx.ex = np->first_tx.ex;
2116                 if (unlikely(np->get_tx_ctx++ == np->last_tx_ctx))
2117                         np->get_tx_ctx = np->first_tx_ctx;
2118         }
2119         if (unlikely((np->tx_stop == 1) && (np->get_tx.ex != orig_get_tx))) {
2120                 np->tx_stop = 0;
2121                 netif_wake_queue(dev);
2122         }
2123 }
2124
2125 /*
2126  * nv_tx_timeout: dev->tx_timeout function
2127  * Called with netif_tx_lock held.
2128  */
2129 static void nv_tx_timeout(struct net_device *dev)
2130 {
2131         struct fe_priv *np = netdev_priv(dev);
2132         u8 __iomem *base = get_hwbase(dev);
2133         u32 status;
2134
2135         if (np->msi_flags & NV_MSI_X_ENABLED)
2136                 status = readl(base + NvRegMSIXIrqStatus) & NVREG_IRQSTAT_MASK;
2137         else
2138                 status = readl(base + NvRegIrqStatus) & NVREG_IRQSTAT_MASK;
2139
2140         printk(KERN_INFO "%s: Got tx_timeout. irq: %08x\n", dev->name, status);
2141
2142         {
2143                 int i;
2144
2145                 printk(KERN_INFO "%s: Ring at %lx\n",
2146                        dev->name, (unsigned long)np->ring_addr);
2147                 printk(KERN_INFO "%s: Dumping tx registers\n", dev->name);
2148                 for (i=0;i<=np->register_size;i+= 32) {
2149                         printk(KERN_INFO "%3x: %08x %08x %08x %08x %08x %08x %08x %08x\n",
2150                                         i,
2151                                         readl(base + i + 0), readl(base + i + 4),
2152                                         readl(base + i + 8), readl(base + i + 12),
2153                                         readl(base + i + 16), readl(base + i + 20),
2154                                         readl(base + i + 24), readl(base + i + 28));
2155                 }
2156                 printk(KERN_INFO "%s: Dumping tx ring\n", dev->name);
2157                 for (i=0;i<np->tx_ring_size;i+= 4) {
2158                         if (np->desc_ver == DESC_VER_1 || np->desc_ver == DESC_VER_2) {
2159                                 printk(KERN_INFO "%03x: %08x %08x // %08x %08x // %08x %08x // %08x %08x\n",
2160                                        i,
2161                                        le32_to_cpu(np->tx_ring.orig[i].buf),
2162                                        le32_to_cpu(np->tx_ring.orig[i].flaglen),
2163                                        le32_to_cpu(np->tx_ring.orig[i+1].buf),
2164                                        le32_to_cpu(np->tx_ring.orig[i+1].flaglen),
2165                                        le32_to_cpu(np->tx_ring.orig[i+2].buf),
2166                                        le32_to_cpu(np->tx_ring.orig[i+2].flaglen),
2167                                        le32_to_cpu(np->tx_ring.orig[i+3].buf),
2168                                        le32_to_cpu(np->tx_ring.orig[i+3].flaglen));
2169                         } else {
2170                                 printk(KERN_INFO "%03x: %08x %08x %08x // %08x %08x %08x // %08x %08x %08x // %08x %08x %08x\n",
2171                                        i,
2172                                        le32_to_cpu(np->tx_ring.ex[i].bufhigh),
2173                                        le32_to_cpu(np->tx_ring.ex[i].buflow),
2174                                        le32_to_cpu(np->tx_ring.ex[i].flaglen),
2175                                        le32_to_cpu(np->tx_ring.ex[i+1].bufhigh),
2176                                        le32_to_cpu(np->tx_ring.ex[i+1].buflow),
2177                                        le32_to_cpu(np->tx_ring.ex[i+1].flaglen),
2178                                        le32_to_cpu(np->tx_ring.ex[i+2].bufhigh),
2179                                        le32_to_cpu(np->tx_ring.ex[i+2].buflow),
2180                                        le32_to_cpu(np->tx_ring.ex[i+2].flaglen),
2181                                        le32_to_cpu(np->tx_ring.ex[i+3].bufhigh),
2182                                        le32_to_cpu(np->tx_ring.ex[i+3].buflow),
2183                                        le32_to_cpu(np->tx_ring.ex[i+3].flaglen));
2184                         }
2185                 }
2186         }
2187
2188         spin_lock_irq(&np->lock);
2189
2190         /* 1) stop tx engine */
2191         nv_stop_tx(dev);
2192
2193         /* 2) check that the packets were not sent already: */
2194         if (np->desc_ver == DESC_VER_1 || np->desc_ver == DESC_VER_2)
2195                 nv_tx_done(dev);
2196         else
2197                 nv_tx_done_optimized(dev, np->tx_ring_size);
2198
2199         /* 3) if there are dead entries: clear everything */
2200         if (np->get_tx_ctx != np->put_tx_ctx) {
2201                 printk(KERN_DEBUG "%s: tx_timeout: dead entries!\n", dev->name);
2202                 nv_drain_tx(dev);
2203                 nv_init_tx(dev);
2204                 setup_hw_rings(dev, NV_SETUP_TX_RING);
2205         }
2206
2207         netif_wake_queue(dev);
2208
2209         /* 4) restart tx engine */
2210         nv_start_tx(dev);
2211         spin_unlock_irq(&np->lock);
2212 }
2213
2214 /*
2215  * Called when the nic notices a mismatch between the actual data len on the
2216  * wire and the len indicated in the 802 header
2217  */
2218 static int nv_getlen(struct net_device *dev, void *packet, int datalen)
2219 {
2220         int hdrlen;     /* length of the 802 header */
2221         int protolen;   /* length as stored in the proto field */
2222
2223         /* 1) calculate len according to header */
2224         if ( ((struct vlan_ethhdr *)packet)->h_vlan_proto == htons(ETH_P_8021Q)) {
2225                 protolen = ntohs( ((struct vlan_ethhdr *)packet)->h_vlan_encapsulated_proto );
2226                 hdrlen = VLAN_HLEN;
2227         } else {
2228                 protolen = ntohs( ((struct ethhdr *)packet)->h_proto);
2229                 hdrlen = ETH_HLEN;
2230         }
2231         dprintk(KERN_DEBUG "%s: nv_getlen: datalen %d, protolen %d, hdrlen %d\n",
2232                                 dev->name, datalen, protolen, hdrlen);
2233         if (protolen > ETH_DATA_LEN)
2234                 return datalen; /* Value in proto field not a len, no checks possible */
2235
2236         protolen += hdrlen;
2237         /* consistency checks: */
2238         if (datalen > ETH_ZLEN) {
2239                 if (datalen >= protolen) {
2240                         /* more data on wire than in 802 header, trim of
2241                          * additional data.
2242                          */
2243                         dprintk(KERN_DEBUG "%s: nv_getlen: accepting %d bytes.\n",
2244                                         dev->name, protolen);
2245                         return protolen;
2246                 } else {
2247                         /* less data on wire than mentioned in header.
2248                          * Discard the packet.
2249                          */
2250                         dprintk(KERN_DEBUG "%s: nv_getlen: discarding long packet.\n",
2251                                         dev->name);
2252                         return -1;
2253                 }
2254         } else {
2255                 /* short packet. Accept only if 802 values are also short */
2256                 if (protolen > ETH_ZLEN) {
2257                         dprintk(KERN_DEBUG "%s: nv_getlen: discarding short packet.\n",
2258                                         dev->name);
2259                         return -1;
2260                 }
2261                 dprintk(KERN_DEBUG "%s: nv_getlen: accepting %d bytes.\n",
2262                                 dev->name, datalen);
2263                 return datalen;
2264         }
2265 }
2266
2267 static int nv_rx_process(struct net_device *dev, int limit)
2268 {
2269         struct fe_priv *np = netdev_priv(dev);
2270         u32 flags;
2271         int rx_work = 0;
2272         struct sk_buff *skb;
2273         int len;
2274
2275         while((np->get_rx.orig != np->put_rx.orig) &&
2276               !((flags = le32_to_cpu(np->get_rx.orig->flaglen)) & NV_RX_AVAIL) &&
2277                 (rx_work < limit)) {
2278
2279                 dprintk(KERN_DEBUG "%s: nv_rx_process: flags 0x%x.\n",
2280                                         dev->name, flags);
2281
2282                 /*
2283                  * the packet is for us - immediately tear down the pci mapping.
2284                  * TODO: check if a prefetch of the first cacheline improves
2285                  * the performance.
2286                  */
2287                 pci_unmap_single(np->pci_dev, np->get_rx_ctx->dma,
2288                                 np->get_rx_ctx->dma_len,
2289                                 PCI_DMA_FROMDEVICE);
2290                 skb = np->get_rx_ctx->skb;
2291                 np->get_rx_ctx->skb = NULL;
2292
2293                 {
2294                         int j;
2295                         dprintk(KERN_DEBUG "Dumping packet (flags 0x%x).",flags);
2296                         for (j=0; j<64; j++) {
2297                                 if ((j%16) == 0)
2298                                         dprintk("\n%03x:", j);
2299                                 dprintk(" %02x", ((unsigned char*)skb->data)[j]);
2300                         }
2301                         dprintk("\n");
2302                 }
2303                 /* look at what we actually got: */
2304                 if (np->desc_ver == DESC_VER_1) {
2305                         if (likely(flags & NV_RX_DESCRIPTORVALID)) {
2306                                 len = flags & LEN_MASK_V1;
2307                                 if (unlikely(flags & NV_RX_ERROR)) {
2308                                         if (flags & NV_RX_ERROR4) {
2309                                                 len = nv_getlen(dev, skb->data, len);
2310                                                 if (len < 0) {
2311                                                         dev->stats.rx_errors++;
2312                                                         dev_kfree_skb(skb);
2313                                                         goto next_pkt;
2314                                                 }
2315                                         }
2316                                         /* framing errors are soft errors */
2317                                         else if (flags & NV_RX_FRAMINGERR) {
2318                                                 if (flags & NV_RX_SUBSTRACT1) {
2319                                                         len--;
2320                                                 }
2321                                         }
2322                                         /* the rest are hard errors */
2323                                         else {
2324                                                 if (flags & NV_RX_MISSEDFRAME)
2325                                                         dev->stats.rx_missed_errors++;
2326                                                 if (flags & NV_RX_CRCERR)
2327                                                         dev->stats.rx_crc_errors++;
2328                                                 if (flags & NV_RX_OVERFLOW)
2329                                                         dev->stats.rx_over_errors++;
2330                                                 dev->stats.rx_errors++;
2331                                                 dev_kfree_skb(skb);
2332                                                 goto next_pkt;
2333                                         }
2334                                 }
2335                         } else {
2336                                 dev_kfree_skb(skb);
2337                                 goto next_pkt;
2338                         }
2339                 } else {
2340                         if (likely(flags & NV_RX2_DESCRIPTORVALID)) {
2341                                 len = flags & LEN_MASK_V2;
2342                                 if (unlikely(flags & NV_RX2_ERROR)) {
2343                                         if (flags & NV_RX2_ERROR4) {
2344                                                 len = nv_getlen(dev, skb->data, len);
2345                                                 if (len < 0) {
2346                                                         dev->stats.rx_errors++;
2347                                                         dev_kfree_skb(skb);
2348                                                         goto next_pkt;
2349                                                 }
2350                                         }
2351                                         /* framing errors are soft errors */
2352                                         else if (flags & NV_RX2_FRAMINGERR) {
2353                                                 if (flags & NV_RX2_SUBSTRACT1) {
2354                                                         len--;
2355                                                 }
2356                                         }
2357                                         /* the rest are hard errors */
2358                                         else {
2359                                                 if (flags & NV_RX2_CRCERR)
2360                                                         dev->stats.rx_crc_errors++;
2361                                                 if (flags & NV_RX2_OVERFLOW)
2362                                                         dev->stats.rx_over_errors++;
2363                                                 dev->stats.rx_errors++;
2364                                                 dev_kfree_skb(skb);
2365                                                 goto next_pkt;
2366                                         }
2367                                 }
2368                                 if ((flags & NV_RX2_CHECKSUMMASK) == NV_RX2_CHECKSUMOK2)/*ip and tcp */ {
2369                                         skb->ip_summed = CHECKSUM_UNNECESSARY;
2370                                 } else {
2371                                         if ((flags & NV_RX2_CHECKSUMMASK) == NV_RX2_CHECKSUMOK1 ||
2372                                             (flags & NV_RX2_CHECKSUMMASK) == NV_RX2_CHECKSUMOK3) {
2373                                                 skb->ip_summed = CHECKSUM_UNNECESSARY;
2374                                         }
2375                                 }
2376                         } else {
2377                                 dev_kfree_skb(skb);
2378                                 goto next_pkt;
2379                         }
2380                 }
2381                 /* got a valid packet - forward it to the network core */
2382                 skb_put(skb, len);
2383                 skb->protocol = eth_type_trans(skb, dev);
2384                 dprintk(KERN_DEBUG "%s: nv_rx_process: %d bytes, proto %d accepted.\n",
2385                                         dev->name, len, skb->protocol);
2386 #ifdef CONFIG_FORCEDETH_NAPI
2387                 netif_receive_skb(skb);
2388 #else
2389                 netif_rx(skb);
2390 #endif
2391                 dev->last_rx = jiffies;
2392                 dev->stats.rx_packets++;
2393                 dev->stats.rx_bytes += len;
2394 next_pkt:
2395                 if (unlikely(np->get_rx.orig++ == np->last_rx.orig))
2396                         np->get_rx.orig = np->first_rx.orig;
2397                 if (unlikely(np->get_rx_ctx++ == np->last_rx_ctx))
2398                         np->get_rx_ctx = np->first_rx_ctx;
2399
2400                 rx_work++;
2401         }
2402
2403         return rx_work;
2404 }
2405
2406 static int nv_rx_process_optimized(struct net_device *dev, int limit)
2407 {
2408         struct fe_priv *np = netdev_priv(dev);
2409         u32 flags;
2410         u32 vlanflags = 0;
2411         u32 rx_processed_cnt = 0;
2412         struct sk_buff *skb;
2413         int len;
2414
2415         while((np->get_rx.ex != np->put_rx.ex) &&
2416               !((flags = le32_to_cpu(np->get_rx.ex->flaglen)) & NV_RX2_AVAIL) &&
2417               (rx_processed_cnt++ < limit)) {
2418
2419                 dprintk(KERN_DEBUG "%s: nv_rx_process_optimized: flags 0x%x.\n",
2420                                         dev->name, flags);
2421
2422                 /*
2423                  * the packet is for us - immediately tear down the pci mapping.
2424                  * TODO: check if a prefetch of the first cacheline improves
2425                  * the performance.
2426                  */
2427                 pci_unmap_single(np->pci_dev, np->get_rx_ctx->dma,
2428                                 np->get_rx_ctx->dma_len,
2429                                 PCI_DMA_FROMDEVICE);
2430                 skb = np->get_rx_ctx->skb;
2431                 np->get_rx_ctx->skb = NULL;
2432
2433                 {
2434                         int j;
2435                         dprintk(KERN_DEBUG "Dumping packet (flags 0x%x).",flags);
2436                         for (j=0; j<64; j++) {
2437                                 if ((j%16) == 0)
2438                                         dprintk("\n%03x:", j);
2439                                 dprintk(" %02x", ((unsigned char*)skb->data)[j]);
2440                         }
2441                         dprintk("\n");
2442                 }
2443                 /* look at what we actually got: */
2444                 if (likely(flags & NV_RX2_DESCRIPTORVALID)) {
2445                         len = flags & LEN_MASK_V2;
2446                         if (unlikely(flags & NV_RX2_ERROR)) {
2447                                 if (flags & NV_RX2_ERROR4) {
2448                                         len = nv_getlen(dev, skb->data, len);
2449                                         if (len < 0) {
2450                                                 dev_kfree_skb(skb);
2451                                                 goto next_pkt;
2452                                         }
2453                                 }
2454                                 /* framing errors are soft errors */
2455                                 else if (flags & NV_RX2_FRAMINGERR) {
2456                                         if (flags & NV_RX2_SUBSTRACT1) {
2457                                                 len--;
2458                                         }
2459                                 }
2460                                 /* the rest are hard errors */
2461                                 else {
2462                                         dev_kfree_skb(skb);
2463                                         goto next_pkt;
2464                                 }
2465                         }
2466
2467                         if ((flags & NV_RX2_CHECKSUMMASK) == NV_RX2_CHECKSUMOK2)/*ip and tcp */ {
2468                                 skb->ip_summed = CHECKSUM_UNNECESSARY;
2469                         } else {
2470                                 if ((flags & NV_RX2_CHECKSUMMASK) == NV_RX2_CHECKSUMOK1 ||
2471                                     (flags & NV_RX2_CHECKSUMMASK) == NV_RX2_CHECKSUMOK3) {
2472                                         skb->ip_summed = CHECKSUM_UNNECESSARY;
2473                                 }
2474                         }
2475
2476                         /* got a valid packet - forward it to the network core */
2477                         skb_put(skb, len);
2478                         skb->protocol = eth_type_trans(skb, dev);
2479                         prefetch(skb->data);
2480
2481                         dprintk(KERN_DEBUG "%s: nv_rx_process_optimized: %d bytes, proto %d accepted.\n",
2482                                 dev->name, len, skb->protocol);
2483
2484                         if (likely(!np->vlangrp)) {
2485 #ifdef CONFIG_FORCEDETH_NAPI
2486                                 netif_receive_skb(skb);
2487 #else
2488                                 netif_rx(skb);
2489 #endif
2490                         } else {
2491                                 vlanflags = le32_to_cpu(np->get_rx.ex->buflow);
2492                                 if (vlanflags & NV_RX3_VLAN_TAG_PRESENT) {
2493 #ifdef CONFIG_FORCEDETH_NAPI
2494                                         vlan_hwaccel_receive_skb(skb, np->vlangrp,
2495                                                                  vlanflags & NV_RX3_VLAN_TAG_MASK);
2496 #else
2497                                         vlan_hwaccel_rx(skb, np->vlangrp,
2498                                                         vlanflags & NV_RX3_VLAN_TAG_MASK);
2499 #endif
2500                                 } else {
2501 #ifdef CONFIG_FORCEDETH_NAPI
2502                                         netif_receive_skb(skb);
2503 #else
2504                                         netif_rx(skb);
2505 #endif
2506                                 }
2507                         }
2508
2509                         dev->last_rx = jiffies;
2510                         dev->stats.rx_packets++;
2511                         dev->stats.rx_bytes += len;
2512                 } else {
2513                         dev_kfree_skb(skb);
2514                 }
2515 next_pkt:
2516                 if (unlikely(np->get_rx.ex++ == np->last_rx.ex))
2517                         np->get_rx.ex = np->first_rx.ex;
2518                 if (unlikely(np->get_rx_ctx++ == np->last_rx_ctx))
2519                         np->get_rx_ctx = np->first_rx_ctx;
2520         }
2521
2522         return rx_processed_cnt;
2523 }
2524
2525 static void set_bufsize(struct net_device *dev)
2526 {
2527         struct fe_priv *np = netdev_priv(dev);
2528
2529         if (dev->mtu <= ETH_DATA_LEN)
2530                 np->rx_buf_sz = ETH_DATA_LEN + NV_RX_HEADERS;
2531         else
2532                 np->rx_buf_sz = dev->mtu + NV_RX_HEADERS;
2533 }
2534
2535 /*
2536  * nv_change_mtu: dev->change_mtu function
2537  * Called with dev_base_lock held for read.
2538  */
2539 static int nv_change_mtu(struct net_device *dev, int new_mtu)
2540 {
2541         struct fe_priv *np = netdev_priv(dev);
2542         int old_mtu;
2543
2544         if (new_mtu < 64 || new_mtu > np->pkt_limit)
2545                 return -EINVAL;
2546
2547         old_mtu = dev->mtu;
2548         dev->mtu = new_mtu;
2549
2550         /* return early if the buffer sizes will not change */
2551         if (old_mtu <= ETH_DATA_LEN && new_mtu <= ETH_DATA_LEN)
2552                 return 0;
2553         if (old_mtu == new_mtu)
2554                 return 0;
2555
2556         /* synchronized against open : rtnl_lock() held by caller */
2557         if (netif_running(dev)) {
2558                 u8 __iomem *base = get_hwbase(dev);
2559                 /*
2560                  * It seems that the nic preloads valid ring entries into an
2561                  * internal buffer. The procedure for flushing everything is
2562                  * guessed, there is probably a simpler approach.
2563                  * Changing the MTU is a rare event, it shouldn't matter.
2564                  */
2565                 nv_disable_irq(dev);
2566                 netif_tx_lock_bh(dev);
2567                 spin_lock(&np->lock);
2568                 /* stop engines */
2569                 nv_stop_rx(dev);
2570                 nv_stop_tx(dev);
2571                 nv_txrx_reset(dev);
2572                 /* drain rx queue */
2573                 nv_drain_rx(dev);
2574                 nv_drain_tx(dev);
2575                 /* reinit driver view of the rx queue */
2576                 set_bufsize(dev);
2577                 if (nv_init_ring(dev)) {
2578                         if (!np->in_shutdown)
2579                                 mod_timer(&np->oom_kick, jiffies + OOM_REFILL);
2580                 }
2581                 /* reinit nic view of the rx queue */
2582                 writel(np->rx_buf_sz, base + NvRegOffloadConfig);
2583                 setup_hw_rings(dev, NV_SETUP_RX_RING | NV_SETUP_TX_RING);
2584                 writel( ((np->rx_ring_size-1) << NVREG_RINGSZ_RXSHIFT) + ((np->tx_ring_size-1) << NVREG_RINGSZ_TXSHIFT),
2585                         base + NvRegRingSizes);
2586                 pci_push(base);
2587                 writel(NVREG_TXRXCTL_KICK|np->txrxctl_bits, get_hwbase(dev) + NvRegTxRxControl);
2588                 pci_push(base);
2589
2590                 /* restart rx engine */
2591                 nv_start_rx(dev);
2592                 nv_start_tx(dev);
2593                 spin_unlock(&np->lock);
2594                 netif_tx_unlock_bh(dev);
2595                 nv_enable_irq(dev);
2596         }
2597         return 0;
2598 }
2599
2600 static void nv_copy_mac_to_hw(struct net_device *dev)
2601 {
2602         u8 __iomem *base = get_hwbase(dev);
2603         u32 mac[2];
2604
2605         mac[0] = (dev->dev_addr[0] << 0) + (dev->dev_addr[1] << 8) +
2606                         (dev->dev_addr[2] << 16) + (dev->dev_addr[3] << 24);
2607         mac[1] = (dev->dev_addr[4] << 0) + (dev->dev_addr[5] << 8);
2608
2609         writel(mac[0], base + NvRegMacAddrA);
2610         writel(mac[1], base + NvRegMacAddrB);
2611 }
2612
2613 /*
2614  * nv_set_mac_address: dev->set_mac_address function
2615  * Called with rtnl_lock() held.
2616  */
2617 static int nv_set_mac_address(struct net_device *dev, void *addr)
2618 {
2619         struct fe_priv *np = netdev_priv(dev);
2620         struct sockaddr *macaddr = (struct sockaddr*)addr;
2621
2622         if (!is_valid_ether_addr(macaddr->sa_data))
2623                 return -EADDRNOTAVAIL;
2624
2625         /* synchronized against open : rtnl_lock() held by caller */
2626         memcpy(dev->dev_addr, macaddr->sa_data, ETH_ALEN);
2627
2628         if (netif_running(dev)) {
2629                 netif_tx_lock_bh(dev);
2630                 spin_lock_irq(&np->lock);
2631
2632                 /* stop rx engine */
2633                 nv_stop_rx(dev);
2634
2635                 /* set mac address */
2636                 nv_copy_mac_to_hw(dev);
2637
2638                 /* restart rx engine */
2639                 nv_start_rx(dev);
2640                 spin_unlock_irq(&np->lock);
2641                 netif_tx_unlock_bh(dev);
2642         } else {
2643                 nv_copy_mac_to_hw(dev);
2644         }
2645         return 0;
2646 }
2647
2648 /*
2649  * nv_set_multicast: dev->set_multicast function
2650  * Called with netif_tx_lock held.
2651  */
2652 static void nv_set_multicast(struct net_device *dev)
2653 {
2654         struct fe_priv *np = netdev_priv(dev);
2655         u8 __iomem *base = get_hwbase(dev);
2656         u32 addr[2];
2657         u32 mask[2];
2658         u32 pff = readl(base + NvRegPacketFilterFlags) & NVREG_PFF_PAUSE_RX;
2659
2660         memset(addr, 0, sizeof(addr));
2661         memset(mask, 0, sizeof(mask));
2662
2663         if (dev->flags & IFF_PROMISC) {
2664                 pff |= NVREG_PFF_PROMISC;
2665         } else {
2666                 pff |= NVREG_PFF_MYADDR;
2667
2668                 if (dev->flags & IFF_ALLMULTI || dev->mc_list) {
2669                         u32 alwaysOff[2];
2670                         u32 alwaysOn[2];
2671
2672                         alwaysOn[0] = alwaysOn[1] = alwaysOff[0] = alwaysOff[1] = 0xffffffff;
2673                         if (dev->flags & IFF_ALLMULTI) {
2674                                 alwaysOn[0] = alwaysOn[1] = alwaysOff[0] = alwaysOff[1] = 0;
2675                         } else {
2676                                 struct dev_mc_list *walk;
2677
2678                                 walk = dev->mc_list;
2679                                 while (walk != NULL) {
2680                                         u32 a, b;
2681                                         a = le32_to_cpu(*(u32 *) walk->dmi_addr);
2682                                         b = le16_to_cpu(*(u16 *) (&walk->dmi_addr[4]));
2683                                         alwaysOn[0] &= a;
2684                                         alwaysOff[0] &= ~a;
2685                                         alwaysOn[1] &= b;
2686                                         alwaysOff[1] &= ~b;
2687                                         walk = walk->next;
2688                                 }
2689                         }
2690                         addr[0] = alwaysOn[0];
2691                         addr[1] = alwaysOn[1];
2692                         mask[0] = alwaysOn[0] | alwaysOff[0];
2693                         mask[1] = alwaysOn[1] | alwaysOff[1];
2694                 }
2695         }
2696         addr[0] |= NVREG_MCASTADDRA_FORCE;
2697         pff |= NVREG_PFF_ALWAYS;
2698         spin_lock_irq(&np->lock);
2699         nv_stop_rx(dev);
2700         writel(addr[0], base + NvRegMulticastAddrA);
2701         writel(addr[1], base + NvRegMulticastAddrB);
2702         writel(mask[0], base + NvRegMulticastMaskA);
2703         writel(mask[1], base + NvRegMulticastMaskB);
2704         writel(pff, base + NvRegPacketFilterFlags);
2705         dprintk(KERN_INFO "%s: reconfiguration for multicast lists.\n",
2706                 dev->name);
2707         nv_start_rx(dev);
2708         spin_unlock_irq(&np->lock);
2709 }
2710
2711 static void nv_update_pause(struct net_device *dev, u32 pause_flags)
2712 {
2713         struct fe_priv *np = netdev_priv(dev);
2714         u8 __iomem *base = get_hwbase(dev);
2715
2716         np->pause_flags &= ~(NV_PAUSEFRAME_TX_ENABLE | NV_PAUSEFRAME_RX_ENABLE);
2717
2718         if (np->pause_flags & NV_PAUSEFRAME_RX_CAPABLE) {
2719                 u32 pff = readl(base + NvRegPacketFilterFlags) & ~NVREG_PFF_PAUSE_RX;
2720                 if (pause_flags & NV_PAUSEFRAME_RX_ENABLE) {
2721                         writel(pff|NVREG_PFF_PAUSE_RX, base + NvRegPacketFilterFlags);
2722                         np->pause_flags |= NV_PAUSEFRAME_RX_ENABLE;
2723                 } else {
2724                         writel(pff, base + NvRegPacketFilterFlags);
2725                 }
2726         }
2727         if (np->pause_flags & NV_PAUSEFRAME_TX_CAPABLE) {
2728                 u32 regmisc = readl(base + NvRegMisc1) & ~NVREG_MISC1_PAUSE_TX;
2729                 if (pause_flags & NV_PAUSEFRAME_TX_ENABLE) {
2730                         writel(NVREG_TX_PAUSEFRAME_ENABLE,  base + NvRegTxPauseFrame);
2731                         writel(regmisc|NVREG_MISC1_PAUSE_TX, base + NvRegMisc1);
2732                         np->pause_flags |= NV_PAUSEFRAME_TX_ENABLE;
2733                 } else {
2734                         writel(NVREG_TX_PAUSEFRAME_DISABLE,  base + NvRegTxPauseFrame);
2735                         writel(regmisc, base + NvRegMisc1);
2736                 }
2737         }
2738 }
2739
2740 /**
2741  * nv_update_linkspeed: Setup the MAC according to the link partner
2742  * @dev: Network device to be configured
2743  *
2744  * The function queries the PHY and checks if there is a link partner.
2745  * If yes, then it sets up the MAC accordingly. Otherwise, the MAC is
2746  * set to 10 MBit HD.
2747  *
2748  * The function returns 0 if there is no link partner and 1 if there is
2749  * a good link partner.
2750  */
2751 static int nv_update_linkspeed(struct net_device *dev)
2752 {
2753         struct fe_priv *np = netdev_priv(dev);
2754         u8 __iomem *base = get_hwbase(dev);
2755         int adv = 0;
2756         int lpa = 0;
2757         int adv_lpa, adv_pause, lpa_pause;
2758         int newls = np->linkspeed;
2759         int newdup = np->duplex;
2760         int mii_status;
2761         int retval = 0;
2762         u32 control_1000, status_1000, phyreg, pause_flags, txreg;
2763
2764         /* BMSR_LSTATUS is latched, read it twice:
2765          * we want the current value.
2766          */
2767         mii_rw(dev, np->phyaddr, MII_BMSR, MII_READ);
2768         mii_status = mii_rw(dev, np->phyaddr, MII_BMSR, MII_READ);
2769
2770         if (!(mii_status & BMSR_LSTATUS)) {
2771                 dprintk(KERN_DEBUG "%s: no link detected by phy - falling back to 10HD.\n",
2772                                 dev->name);
2773                 newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_10;
2774                 newdup = 0;
2775                 retval = 0;
2776                 goto set_speed;
2777         }
2778
2779         if (np->autoneg == 0) {
2780                 dprintk(KERN_DEBUG "%s: nv_update_linkspeed: autoneg off, PHY set to 0x%04x.\n",
2781                                 dev->name, np->fixed_mode);
2782                 if (np->fixed_mode & LPA_100FULL) {
2783                         newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_100;
2784                         newdup = 1;
2785                 } else if (np->fixed_mode & LPA_100HALF) {
2786                         newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_100;
2787                         newdup = 0;
2788                 } else if (np->fixed_mode & LPA_10FULL) {
2789                         newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_10;
2790                         newdup = 1;
2791                 } else {
2792                         newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_10;
2793                         newdup = 0;
2794                 }
2795                 retval = 1;
2796                 goto set_speed;
2797         }
2798         /* check auto negotiation is complete */
2799         if (!(mii_status & BMSR_ANEGCOMPLETE)) {
2800                 /* still in autonegotiation - configure nic for 10 MBit HD and wait. */
2801                 newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_10;
2802                 newdup = 0;
2803                 retval = 0;
2804                 dprintk(KERN_DEBUG "%s: autoneg not completed - falling back to 10HD.\n", dev->name);
2805                 goto set_speed;
2806         }
2807
2808         adv = mii_rw(dev, np->phyaddr, MII_ADVERTISE, MII_READ);
2809         lpa = mii_rw(dev, np->phyaddr, MII_LPA, MII_READ);
2810         dprintk(KERN_DEBUG "%s: nv_update_linkspeed: PHY advertises 0x%04x, lpa 0x%04x.\n",
2811                                 dev->name, adv, lpa);
2812
2813         retval = 1;
2814         if (np->gigabit == PHY_GIGABIT) {
2815                 control_1000 = mii_rw(dev, np->phyaddr, MII_CTRL1000, MII_READ);
2816                 status_1000 = mii_rw(dev, np->phyaddr, MII_STAT1000, MII_READ);
2817
2818                 if ((control_1000 & ADVERTISE_1000FULL) &&
2819                         (status_1000 & LPA_1000FULL)) {
2820                         dprintk(KERN_DEBUG "%s: nv_update_linkspeed: GBit ethernet detected.\n",
2821                                 dev->name);
2822                         newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_1000;
2823                         newdup = 1;
2824                         goto set_speed;
2825                 }
2826         }
2827
2828         /* FIXME: handle parallel detection properly */
2829         adv_lpa = lpa & adv;
2830         if (adv_lpa & LPA_100FULL) {
2831                 newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_100;
2832                 newdup = 1;
2833         } else if (adv_lpa & LPA_100HALF) {
2834                 newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_100;
2835                 newdup = 0;
2836         } else if (adv_lpa & LPA_10FULL) {
2837                 newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_10;
2838                 newdup = 1;
2839         } else if (adv_lpa & LPA_10HALF) {
2840                 newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_10;
2841                 newdup = 0;
2842         } else {
2843                 dprintk(KERN_DEBUG "%s: bad ability %04x - falling back to 10HD.\n", dev->name, adv_lpa);
2844                 newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_10;
2845                 newdup = 0;
2846         }
2847
2848 set_speed:
2849         if (np->duplex == newdup && np->linkspeed == newls)
2850                 return retval;
2851
2852         dprintk(KERN_INFO "%s: changing link setting from %d/%d to %d/%d.\n",
2853                         dev->name, np->linkspeed, np->duplex, newls, newdup);
2854
2855         np->duplex = newdup;
2856         np->linkspeed = newls;
2857
2858         if (np->gigabit == PHY_GIGABIT) {
2859                 phyreg = readl(base + NvRegRandomSeed);
2860                 phyreg &= ~(0x3FF00);
2861                 if ((np->linkspeed & 0xFFF) == NVREG_LINKSPEED_10)
2862                         phyreg |= NVREG_RNDSEED_FORCE3;
2863                 else if ((np->linkspeed & 0xFFF) == NVREG_LINKSPEED_100)
2864                         phyreg |= NVREG_RNDSEED_FORCE2;
2865                 else if ((np->linkspeed & 0xFFF) == NVREG_LINKSPEED_1000)
2866                         phyreg |= NVREG_RNDSEED_FORCE;
2867                 writel(phyreg, base + NvRegRandomSeed);
2868         }
2869
2870         phyreg = readl(base + NvRegPhyInterface);
2871         phyreg &= ~(PHY_HALF|PHY_100|PHY_1000);
2872         if (np->duplex == 0)
2873                 phyreg |= PHY_HALF;
2874         if ((np->linkspeed & NVREG_LINKSPEED_MASK) == NVREG_LINKSPEED_100)
2875                 phyreg |= PHY_100;
2876         else if ((np->linkspeed & NVREG_LINKSPEED_MASK) == NVREG_LINKSPEED_1000)
2877                 phyreg |= PHY_1000;
2878         writel(phyreg, base + NvRegPhyInterface);
2879
2880         if (phyreg & PHY_RGMII) {
2881                 if ((np->linkspeed & NVREG_LINKSPEED_MASK) == NVREG_LINKSPEED_1000)
2882                         txreg = NVREG_TX_DEFERRAL_RGMII_1000;
2883                 else
2884                         txreg = NVREG_TX_DEFERRAL_RGMII_10_100;
2885         } else {
2886                 txreg = NVREG_TX_DEFERRAL_DEFAULT;
2887         }
2888         writel(txreg, base + NvRegTxDeferral);
2889
2890         if (np->desc_ver == DESC_VER_1) {
2891                 txreg = NVREG_TX_WM_DESC1_DEFAULT;
2892         } else {
2893                 if ((np->linkspeed & NVREG_LINKSPEED_MASK) == NVREG_LINKSPEED_1000)
2894                         txreg = NVREG_TX_WM_DESC2_3_1000;
2895                 else
2896                         txreg = NVREG_TX_WM_DESC2_3_DEFAULT;
2897         }
2898         writel(txreg, base + NvRegTxWatermark);
2899
2900         writel(NVREG_MISC1_FORCE | ( np->duplex ? 0 : NVREG_MISC1_HD),
2901                 base + NvRegMisc1);
2902         pci_push(base);
2903         writel(np->linkspeed, base + NvRegLinkSpeed);
2904         pci_push(base);
2905
2906         pause_flags = 0;
2907         /* setup pause frame */
2908         if (np->duplex != 0) {
2909                 if (np->autoneg && np->pause_flags & NV_PAUSEFRAME_AUTONEG) {
2910                         adv_pause = adv & (ADVERTISE_PAUSE_CAP| ADVERTISE_PAUSE_ASYM);
2911                         lpa_pause = lpa & (LPA_PAUSE_CAP| LPA_PAUSE_ASYM);
2912
2913                         switch (adv_pause) {
2914                         case ADVERTISE_PAUSE_CAP:
2915                                 if (lpa_pause & LPA_PAUSE_CAP) {
2916                                         pause_flags |= NV_PAUSEFRAME_RX_ENABLE;
2917                                         if (np->pause_flags & NV_PAUSEFRAME_TX_REQ)
2918                                                 pause_flags |= NV_PAUSEFRAME_TX_ENABLE;
2919                                 }
2920                                 break;
2921                         case ADVERTISE_PAUSE_ASYM:
2922                                 if (lpa_pause == (LPA_PAUSE_CAP| LPA_PAUSE_ASYM))
2923                                 {
2924                                         pause_flags |= NV_PAUSEFRAME_TX_ENABLE;
2925                                 }
2926                                 break;
2927                         case ADVERTISE_PAUSE_CAP| ADVERTISE_PAUSE_ASYM:
2928                                 if (lpa_pause & LPA_PAUSE_CAP)
2929                                 {
2930                                         pause_flags |=  NV_PAUSEFRAME_RX_ENABLE;
2931                                         if (np->pause_flags & NV_PAUSEFRAME_TX_REQ)
2932                                                 pause_flags |= NV_PAUSEFRAME_TX_ENABLE;
2933                                 }
2934                                 if (lpa_pause == LPA_PAUSE_ASYM)
2935                                 {
2936                                         pause_flags |= NV_PAUSEFRAME_RX_ENABLE;
2937                                 }
2938                                 break;
2939                         }
2940                 } else {
2941                         pause_flags = np->pause_flags;
2942                 }
2943         }
2944         nv_update_pause(dev, pause_flags);
2945
2946         return retval;
2947 }
2948
2949 static void nv_linkchange(struct net_device *dev)
2950 {
2951         if (nv_update_linkspeed(dev)) {
2952                 if (!netif_carrier_ok(dev)) {
2953                         netif_carrier_on(dev);
2954                         printk(KERN_INFO "%s: link up.\n", dev->name);
2955                         nv_start_rx(dev);
2956                 }
2957         } else {
2958                 if (netif_carrier_ok(dev)) {
2959                         netif_carrier_off(dev);
2960                         printk(KERN_INFO "%s: link down.\n", dev->name);
2961                         nv_stop_rx(dev);
2962                 }
2963         }
2964 }
2965
2966 static void nv_link_irq(struct net_device *dev)
2967 {
2968         u8 __iomem *base = get_hwbase(dev);
2969         u32 miistat;
2970
2971         miistat = readl(base + NvRegMIIStatus);
2972         writel(NVREG_MIISTAT_MASK, base + NvRegMIIStatus);
2973         dprintk(KERN_INFO "%s: link change irq, status 0x%x.\n", dev->name, miistat);
2974
2975         if (miistat & (NVREG_MIISTAT_LINKCHANGE))
2976                 nv_linkchange(dev);
2977         dprintk(KERN_DEBUG "%s: link change notification done.\n", dev->name);
2978 }
2979
2980 static irqreturn_t nv_nic_irq(int foo, void *data)
2981 {
2982         struct net_device *dev = (struct net_device *) data;
2983         struct fe_priv *np = netdev_priv(dev);
2984         u8 __iomem *base = get_hwbase(dev);
2985         u32 events;
2986         int i;
2987
2988         dprintk(KERN_DEBUG "%s: nv_nic_irq\n", dev->name);
2989
2990         for (i=0; ; i++) {
2991                 if (!(np->msi_flags & NV_MSI_X_ENABLED)) {
2992                         events = readl(base + NvRegIrqStatus) & NVREG_IRQSTAT_MASK;
2993                         writel(NVREG_IRQSTAT_MASK, base + NvRegIrqStatus);
2994                 } else {
2995                         events = readl(base + NvRegMSIXIrqStatus) & NVREG_IRQSTAT_MASK;
2996                         writel(NVREG_IRQSTAT_MASK, base + NvRegMSIXIrqStatus);
2997                 }
2998                 dprintk(KERN_DEBUG "%s: irq: %08x\n", dev->name, events);
2999                 if (!(events & np->irqmask))
3000                         break;
3001
3002                 spin_lock(&np->lock);
3003                 nv_tx_done(dev);
3004                 spin_unlock(&np->lock);
3005
3006 #ifdef CONFIG_FORCEDETH_NAPI
3007                 if (events & NVREG_IRQ_RX_ALL) {
3008                         netif_rx_schedule(dev, &np->napi);
3009
3010                         /* Disable furthur receive irq's */
3011                         spin_lock(&np->lock);
3012                         np->irqmask &= ~NVREG_IRQ_RX_ALL;
3013
3014                         if (np->msi_flags & NV_MSI_X_ENABLED)
3015                                 writel(NVREG_IRQ_RX_ALL, base + NvRegIrqMask);
3016                         else
3017                                 writel(np->irqmask, base + NvRegIrqMask);
3018                         spin_unlock(&np->lock);
3019                 }
3020 #else
3021                 if (nv_rx_process(dev, RX_WORK_PER_LOOP)) {
3022                         if (unlikely(nv_alloc_rx(dev))) {
3023                                 spin_lock(&np->lock);
3024                                 if (!np->in_shutdown)
3025                                         mod_timer(&np->oom_kick, jiffies + OOM_REFILL);
3026                                 spin_unlock(&np->lock);
3027                         }
3028                 }
3029 #endif
3030                 if (unlikely(events & NVREG_IRQ_LINK)) {
3031                         spin_lock(&np->lock);
3032                         nv_link_irq(dev);
3033                         spin_unlock(&np->lock);
3034                 }
3035                 if (unlikely(np->need_linktimer && time_after(jiffies, np->link_timeout))) {
3036                         spin_lock(&np->lock);
3037                         nv_linkchange(dev);
3038                         spin_unlock(&np->lock);
3039                         np->link_timeout = jiffies + LINK_TIMEOUT;
3040                 }
3041                 if (unlikely(events & (NVREG_IRQ_TX_ERR))) {
3042                         dprintk(KERN_DEBUG "%s: received irq with events 0x%x. Probably TX fail.\n",
3043                                                 dev->name, events);
3044                 }
3045                 if (unlikely(events & (NVREG_IRQ_UNKNOWN))) {
3046                         printk(KERN_DEBUG "%s: received irq with unknown events 0x%x. Please report\n",
3047                                                 dev->name, events);
3048                 }
3049                 if (unlikely(events & NVREG_IRQ_RECOVER_ERROR)) {
3050                         spin_lock(&np->lock);
3051                         /* disable interrupts on the nic */
3052                         if (!(np->msi_flags & NV_MSI_X_ENABLED))
3053                                 writel(0, base + NvRegIrqMask);
3054                         else
3055                                 writel(np->irqmask, base + NvRegIrqMask);
3056                         pci_push(base);
3057
3058                         if (!np->in_shutdown) {
3059                                 np->nic_poll_irq = np->irqmask;
3060                                 np->recover_error = 1;
3061                                 mod_timer(&np->nic_poll, jiffies + POLL_WAIT);
3062                         }
3063                         spin_unlock(&np->lock);
3064                         break;
3065                 }
3066                 if (unlikely(i > max_interrupt_work)) {
3067                         spin_lock(&np->lock);
3068                         /* disable interrupts on the nic */
3069                         if (!(np->msi_flags & NV_MSI_X_ENABLED))
3070                                 writel(0, base + NvRegIrqMask);
3071                         else
3072                                 writel(np->irqmask, base + NvRegIrqMask);
3073                         pci_push(base);
3074
3075                         if (!np->in_shutdown) {
3076                                 np->nic_poll_irq = np->irqmask;
3077                                 mod_timer(&np->nic_poll, jiffies + POLL_WAIT);
3078                         }
3079                         spin_unlock(&np->lock);
3080                         printk(KERN_DEBUG "%s: too many iterations (%d) in nv_nic_irq.\n", dev->name, i);
3081                         break;
3082                 }
3083
3084         }
3085         dprintk(KERN_DEBUG "%s: nv_nic_irq completed\n", dev->name);
3086
3087         return IRQ_RETVAL(i);
3088 }
3089
3090 /**
3091  * All _optimized functions are used to help increase performance
3092  * (reduce CPU and increase throughput). They use descripter version 3,
3093  * compiler directives, and reduce memory accesses.
3094  */
3095 static irqreturn_t nv_nic_irq_optimized(int foo, void *data)
3096 {
3097         struct net_device *dev = (struct net_device *) data;
3098         struct fe_priv *np = netdev_priv(dev);
3099         u8 __iomem *base = get_hwbase(dev);
3100         u32 events;
3101         int i;
3102
3103         dprintk(KERN_DEBUG "%s: nv_nic_irq_optimized\n", dev->name);
3104
3105         for (i=0; ; i++) {
3106                 if (!(np->msi_flags & NV_MSI_X_ENABLED)) {
3107                         events = readl(base + NvRegIrqStatus) & NVREG_IRQSTAT_MASK;
3108                         writel(NVREG_IRQSTAT_MASK, base + NvRegIrqStatus);
3109                 } else {
3110                         events = readl(base + NvRegMSIXIrqStatus) & NVREG_IRQSTAT_MASK;
3111                         writel(NVREG_IRQSTAT_MASK, base + NvRegMSIXIrqStatus);
3112                 }
3113                 dprintk(KERN_DEBUG "%s: irq: %08x\n", dev->name, events);
3114                 if (!(events & np->irqmask))
3115                         break;
3116
3117                 spin_lock(&np->lock);
3118                 nv_tx_done_optimized(dev, TX_WORK_PER_LOOP);
3119                 spin_unlock(&np->lock);
3120
3121 #ifdef CONFIG_FORCEDETH_NAPI
3122                 if (events & NVREG_IRQ_RX_ALL) {
3123                         netif_rx_schedule(dev, &np->napi);
3124
3125                         /* Disable furthur receive irq's */
3126                         spin_lock(&np->lock);
3127                         np->irqmask &= ~NVREG_IRQ_RX_ALL;
3128
3129                         if (np->msi_flags & NV_MSI_X_ENABLED)
3130                                 writel(NVREG_IRQ_RX_ALL, base + NvRegIrqMask);
3131                         else
3132                                 writel(np->irqmask, base + NvRegIrqMask);
3133                         spin_unlock(&np->lock);
3134                 }
3135 #else
3136                 if (nv_rx_process_optimized(dev, RX_WORK_PER_LOOP)) {
3137                         if (unlikely(nv_alloc_rx_optimized(dev))) {
3138                                 spin_lock(&np->lock);
3139                                 if (!np->in_shutdown)
3140                                         mod_timer(&np->oom_kick, jiffies + OOM_REFILL);
3141                                 spin_unlock(&np->lock);
3142                         }
3143                 }
3144 #endif
3145                 if (unlikely(events & NVREG_IRQ_LINK)) {
3146                         spin_lock(&np->lock);
3147                         nv_link_irq(dev);
3148                         spin_unlock(&np->lock);
3149                 }
3150                 if (unlikely(np->need_linktimer && time_after(jiffies, np->link_timeout))) {
3151                         spin_lock(&np->lock);
3152                         nv_linkchange(dev);
3153                         spin_unlock(&np->lock);
3154                         np->link_timeout = jiffies + LINK_TIMEOUT;
3155                 }
3156                 if (unlikely(events & (NVREG_IRQ_TX_ERR))) {
3157                         dprintk(KERN_DEBUG "%s: received irq with events 0x%x. Probably TX fail.\n",
3158                                                 dev->name, events);
3159                 }
3160                 if (unlikely(events & (NVREG_IRQ_UNKNOWN))) {
3161                         printk(KERN_DEBUG "%s: received irq with unknown events 0x%x. Please report\n",
3162                                                 dev->name, events);
3163                 }
3164                 if (unlikely(events & NVREG_IRQ_RECOVER_ERROR)) {
3165                         spin_lock(&np->lock);
3166                         /* disable interrupts on the nic */
3167                         if (!(np->msi_flags & NV_MSI_X_ENABLED))
3168                                 writel(0, base + NvRegIrqMask);
3169                         else
3170                                 writel(np->irqmask, base + NvRegIrqMask);
3171                         pci_push(base);
3172
3173                         if (!np->in_shutdown) {
3174                                 np->nic_poll_irq = np->irqmask;
3175                                 np->recover_error = 1;
3176                                 mod_timer(&np->nic_poll, jiffies + POLL_WAIT);
3177                         }
3178                         spin_unlock(&np->lock);
3179                         break;
3180                 }
3181
3182                 if (unlikely(i > max_interrupt_work)) {
3183                         spin_lock(&np->lock);
3184                         /* disable interrupts on the nic */
3185                         if (!(np->msi_flags & NV_MSI_X_ENABLED))
3186                                 writel(0, base + NvRegIrqMask);
3187                         else
3188                                 writel(np->irqmask, base + NvRegIrqMask);
3189                         pci_push(base);
3190
3191                         if (!np->in_shutdown) {
3192                                 np->nic_poll_irq = np->irqmask;
3193                                 mod_timer(&np->nic_poll, jiffies + POLL_WAIT);
3194                         }
3195                         spin_unlock(&np->lock);
3196                         printk(KERN_DEBUG "%s: too many iterations (%d) in nv_nic_irq.\n", dev->name, i);
3197                         break;
3198                 }
3199
3200         }
3201         dprintk(KERN_DEBUG "%s: nv_nic_irq_optimized completed\n", dev->name);
3202
3203         return IRQ_RETVAL(i);
3204 }
3205
3206 static irqreturn_t nv_nic_irq_tx(int foo, void *data)
3207 {
3208         struct net_device *dev = (struct net_device *) data;
3209         struct fe_priv *np = netdev_priv(dev);
3210         u8 __iomem *base = get_hwbase(dev);
3211         u32 events;
3212         int i;
3213         unsigned long flags;
3214
3215         dprintk(KERN_DEBUG "%s: nv_nic_irq_tx\n", dev->name);
3216
3217         for (i=0; ; i++) {
3218                 events = readl(base + NvRegMSIXIrqStatus) & NVREG_IRQ_TX_ALL;
3219                 writel(NVREG_IRQ_TX_ALL, base + NvRegMSIXIrqStatus);
3220                 dprintk(KERN_DEBUG "%s: tx irq: %08x\n", dev->name, events);
3221                 if (!(events & np->irqmask))
3222                         break;
3223
3224                 spin_lock_irqsave(&np->lock, flags);
3225                 nv_tx_done_optimized(dev, TX_WORK_PER_LOOP);
3226                 spin_unlock_irqrestore(&np->lock, flags);
3227
3228                 if (unlikely(events & (NVREG_IRQ_TX_ERR))) {
3229                         dprintk(KERN_DEBUG "%s: received irq with events 0x%x. Probably TX fail.\n",
3230                                                 dev->name, events);
3231                 }
3232                 if (unlikely(i > max_interrupt_work)) {
3233                         spin_lock_irqsave(&np->lock, flags);
3234                         /* disable interrupts on the nic */
3235                         writel(NVREG_IRQ_TX_ALL, base + NvRegIrqMask);
3236                         pci_push(base);
3237
3238                         if (!np->in_shutdown) {
3239                                 np->nic_poll_irq |= NVREG_IRQ_TX_ALL;
3240                                 mod_timer(&np->nic_poll, jiffies + POLL_WAIT);
3241                         }
3242                         spin_unlock_irqrestore(&np->lock, flags);
3243                         printk(KERN_DEBUG "%s: too many iterations (%d) in nv_nic_irq_tx.\n", dev->name, i);
3244                         break;
3245                 }
3246
3247         }
3248         dprintk(KERN_DEBUG "%s: nv_nic_irq_tx completed\n", dev->name);
3249
3250         return IRQ_RETVAL(i);
3251 }
3252
3253 #ifdef CONFIG_FORCEDETH_NAPI
3254 static int nv_napi_poll(struct napi_struct *napi, int budget)
3255 {
3256         struct fe_priv *np = container_of(napi, struct fe_priv, napi);
3257         struct net_device *dev = np->dev;
3258         u8 __iomem *base = get_hwbase(dev);
3259         unsigned long flags;
3260         int pkts, retcode;
3261
3262         if (np->desc_ver == DESC_VER_1 || np->desc_ver == DESC_VER_2) {
3263                 pkts = nv_rx_process(dev, budget);
3264                 retcode = nv_alloc_rx(dev);
3265         } else {
3266                 pkts = nv_rx_process_optimized(dev, budget);
3267                 retcode = nv_alloc_rx_optimized(dev);
3268         }
3269
3270         if (retcode) {
3271                 spin_lock_irqsave(&np->lock, flags);
3272                 if (!np->in_shutdown)
3273                         mod_timer(&np->oom_kick, jiffies + OOM_REFILL);
3274                 spin_unlock_irqrestore(&np->lock, flags);
3275         }
3276
3277         if (pkts < budget) {
3278                 /* re-enable receive interrupts */
3279                 spin_lock_irqsave(&np->lock, flags);
3280
3281                 __netif_rx_complete(dev, napi);
3282
3283                 np->irqmask |= NVREG_IRQ_RX_ALL;
3284                 if (np->msi_flags & NV_MSI_X_ENABLED)
3285                         writel(NVREG_IRQ_RX_ALL, base + NvRegIrqMask);
3286                 else
3287                         writel(np->irqmask, base + NvRegIrqMask);
3288
3289                 spin_unlock_irqrestore(&np->lock, flags);
3290         }
3291         return pkts;
3292 }
3293 #endif
3294
3295 #ifdef CONFIG_FORCEDETH_NAPI
3296 static irqreturn_t nv_nic_irq_rx(int foo, void *data)
3297 {
3298         struct net_device *dev = (struct net_device *) data;
3299         struct fe_priv *np = netdev_priv(dev);
3300         u8 __iomem *base = get_hwbase(dev);
3301         u32 events;
3302
3303         events = readl(base + NvRegMSIXIrqStatus) & NVREG_IRQ_RX_ALL;
3304         writel(NVREG_IRQ_RX_ALL, base + NvRegMSIXIrqStatus);
3305
3306         if (events) {
3307                 netif_rx_schedule(dev, &np->napi);
3308                 /* disable receive interrupts on the nic */
3309                 writel(NVREG_IRQ_RX_ALL, base + NvRegIrqMask);
3310                 pci_push(base);
3311         }
3312         return IRQ_HANDLED;
3313 }
3314 #else
3315 static irqreturn_t nv_nic_irq_rx(int foo, void *data)
3316 {
3317         struct net_device *dev = (struct net_device *) data;
3318         struct fe_priv *np = netdev_priv(dev);
3319         u8 __iomem *base = get_hwbase(dev);
3320         u32 events;
3321         int i;
3322         unsigned long flags;
3323
3324         dprintk(KERN_DEBUG "%s: nv_nic_irq_rx\n", dev->name);
3325
3326         for (i=0; ; i++) {
3327                 events = readl(base + NvRegMSIXIrqStatus) & NVREG_IRQ_RX_ALL;
3328                 writel(NVREG_IRQ_RX_ALL, base + NvRegMSIXIrqStatus);
3329                 dprintk(KERN_DEBUG "%s: rx irq: %08x\n", dev->name, events);
3330                 if (!(events & np->irqmask))
3331                         break;
3332
3333                 if (nv_rx_process_optimized(dev, RX_WORK_PER_LOOP)) {
3334                         if (unlikely(nv_alloc_rx_optimized(dev))) {
3335                                 spin_lock_irqsave(&np->lock, flags);
3336                                 if (!np->in_shutdown)
3337                                         mod_timer(&np->oom_kick, jiffies + OOM_REFILL);
3338                                 spin_unlock_irqrestore(&np->lock, flags);
3339                         }
3340                 }
3341
3342                 if (unlikely(i > max_interrupt_work)) {
3343                         spin_lock_irqsave(&np->lock, flags);
3344                         /* disable interrupts on the nic */
3345                         writel(NVREG_IRQ_RX_ALL, base + NvRegIrqMask);
3346                         pci_push(base);
3347
3348                         if (!np->in_shutdown) {
3349                                 np->nic_poll_irq |= NVREG_IRQ_RX_ALL;
3350                                 mod_timer(&np->nic_poll, jiffies + POLL_WAIT);
3351                         }
3352                         spin_unlock_irqrestore(&np->lock, flags);
3353                         printk(KERN_DEBUG "%s: too many iterations (%d) in nv_nic_irq_rx.\n", dev->name, i);
3354                         break;
3355                 }
3356         }
3357         dprintk(KERN_DEBUG "%s: nv_nic_irq_rx completed\n", dev->name);
3358
3359         return IRQ_RETVAL(i);
3360 }
3361 #endif
3362
3363 static irqreturn_t nv_nic_irq_other(int foo, void *data)
3364 {
3365         struct net_device *dev = (struct net_device *) data;
3366         struct fe_priv *np = netdev_priv(dev);
3367         u8 __iomem *base = get_hwbase(dev);
3368         u32 events;
3369         int i;
3370         unsigned long flags;
3371
3372         dprintk(KERN_DEBUG "%s: nv_nic_irq_other\n", dev->name);
3373
3374         for (i=0; ; i++) {
3375                 events = readl(base + NvRegMSIXIrqStatus) & NVREG_IRQ_OTHER;
3376                 writel(NVREG_IRQ_OTHER, base + NvRegMSIXIrqStatus);
3377                 dprintk(KERN_DEBUG "%s: irq: %08x\n", dev->name, events);
3378                 if (!(events & np->irqmask))
3379                         break;
3380
3381                 /* check tx in case we reached max loop limit in tx isr */
3382                 spin_lock_irqsave(&np->lock, flags);
3383                 nv_tx_done_optimized(dev, TX_WORK_PER_LOOP);
3384                 spin_unlock_irqrestore(&np->lock, flags);
3385
3386                 if (events & NVREG_IRQ_LINK) {
3387                         spin_lock_irqsave(&np->lock, flags);
3388                         nv_link_irq(dev);
3389                         spin_unlock_irqrestore(&np->lock, flags);
3390                 }
3391                 if (np->need_linktimer && time_after(jiffies, np->link_timeout)) {
3392                         spin_lock_irqsave(&np->lock, flags);
3393                         nv_linkchange(dev);
3394                         spin_unlock_irqrestore(&np->lock, flags);
3395                         np->link_timeout = jiffies + LINK_TIMEOUT;
3396                 }
3397                 if (events & NVREG_IRQ_RECOVER_ERROR) {
3398                         spin_lock_irq(&np->lock);
3399                         /* disable interrupts on the nic */
3400                         writel(NVREG_IRQ_OTHER, base + NvRegIrqMask);
3401                         pci_push(base);
3402
3403                         if (!np->in_shutdown) {
3404                                 np->nic_poll_irq |= NVREG_IRQ_OTHER;
3405                                 np->recover_error = 1;
3406                                 mod_timer(&np->nic_poll, jiffies + POLL_WAIT);
3407                         }
3408                         spin_unlock_irq(&np->lock);
3409                         break;
3410                 }
3411                 if (events & (NVREG_IRQ_UNKNOWN)) {
3412                         printk(KERN_DEBUG "%s: received irq with unknown events 0x%x. Please report\n",
3413                                                 dev->name, events);
3414                 }
3415                 if (unlikely(i > max_interrupt_work)) {
3416                         spin_lock_irqsave(&np->lock, flags);
3417                         /* disable interrupts on the nic */
3418                         writel(NVREG_IRQ_OTHER, base + NvRegIrqMask);
3419                         pci_push(base);
3420
3421                         if (!np->in_shutdown) {
3422                                 np->nic_poll_irq |= NVREG_IRQ_OTHER;
3423                                 mod_timer(&np->nic_poll, jiffies + POLL_WAIT);
3424                         }
3425                         spin_unlock_irqrestore(&np->lock, flags);
3426                         printk(KERN_DEBUG "%s: too many iterations (%d) in nv_nic_irq_other.\n", dev->name, i);
3427                         break;
3428                 }
3429
3430         }
3431         dprintk(KERN_DEBUG "%s: nv_nic_irq_other completed\n", dev->name);
3432
3433         return IRQ_RETVAL(i);
3434 }
3435
3436 static irqreturn_t nv_nic_irq_test(int foo, void *data)
3437 {
3438         struct net_device *dev = (struct net_device *) data;
3439         struct fe_priv *np = netdev_priv(dev);
3440         u8 __iomem *base = get_hwbase(dev);
3441         u32 events;
3442
3443         dprintk(KERN_DEBUG "%s: nv_nic_irq_test\n", dev->name);
3444
3445         if (!(np->msi_flags & NV_MSI_X_ENABLED)) {
3446                 events = readl(base + NvRegIrqStatus) & NVREG_IRQSTAT_MASK;
3447                 writel(NVREG_IRQ_TIMER, base + NvRegIrqStatus);
3448         } else {
3449                 events = readl(base + NvRegMSIXIrqStatus) & NVREG_IRQSTAT_MASK;
3450                 writel(NVREG_IRQ_TIMER, base + NvRegMSIXIrqStatus);
3451         }
3452         pci_push(base);
3453         dprintk(KERN_DEBUG "%s: irq: %08x\n", dev->name, events);
3454         if (!(events & NVREG_IRQ_TIMER))
3455                 return IRQ_RETVAL(0);
3456
3457         spin_lock(&np->lock);
3458         np->intr_test = 1;
3459         spin_unlock(&np->lock);
3460
3461         dprintk(KERN_DEBUG "%s: nv_nic_irq_test completed\n", dev->name);
3462
3463         return IRQ_RETVAL(1);
3464 }
3465
3466 static void set_msix_vector_map(struct net_device *dev, u32 vector, u32 irqmask)
3467 {
3468         u8 __iomem *base = get_hwbase(dev);
3469         int i;
3470         u32 msixmap = 0;
3471
3472         /* Each interrupt bit can be mapped to a MSIX vector (4 bits).
3473          * MSIXMap0 represents the first 8 interrupts and MSIXMap1 represents
3474          * the remaining 8 interrupts.
3475          */
3476         for (i = 0; i < 8; i++) {
3477                 if ((irqmask >> i) & 0x1) {
3478                         msixmap |= vector << (i << 2);
3479                 }
3480         }
3481         writel(readl(base + NvRegMSIXMap0) | msixmap, base + NvRegMSIXMap0);
3482
3483         msixmap = 0;
3484         for (i = 0; i < 8; i++) {
3485                 if ((irqmask >> (i + 8)) & 0x1) {
3486                         msixmap |= vector << (i << 2);
3487                 }
3488         }
3489         writel(readl(base + NvRegMSIXMap1) | msixmap, base + NvRegMSIXMap1);
3490 }
3491
3492 static int nv_request_irq(struct net_device *dev, int intr_test)
3493 {
3494         struct fe_priv *np = get_nvpriv(dev);
3495         u8 __iomem *base = get_hwbase(dev);
3496         int ret = 1;
3497         int i;
3498         irqreturn_t (*handler)(int foo, void *data);
3499
3500         if (intr_test) {
3501                 handler = nv_nic_irq_test;
3502         } else {
3503                 if (np->desc_ver == DESC_VER_3)
3504                         handler = nv_nic_irq_optimized;
3505                 else
3506                         handler = nv_nic_irq;
3507         }
3508
3509         if (np->msi_flags & NV_MSI_X_CAPABLE) {
3510                 for (i = 0; i < (np->msi_flags & NV_MSI_X_VECTORS_MASK); i++) {
3511                         np->msi_x_entry[i].entry = i;
3512                 }
3513                 if ((ret = pci_enable_msix(np->pci_dev, np->msi_x_entry, (np->msi_flags & NV_MSI_X_VECTORS_MASK))) == 0) {
3514                         np->msi_flags |= NV_MSI_X_ENABLED;
3515                         if (optimization_mode == NV_OPTIMIZATION_MODE_THROUGHPUT && !intr_test) {
3516                                 /* Request irq for rx handling */
3517                                 if (request_irq(np->msi_x_entry[NV_MSI_X_VECTOR_RX].vector, &nv_nic_irq_rx, IRQF_SHARED, dev->name, dev) != 0) {
3518                                         printk(KERN_INFO "forcedeth: request_irq failed for rx %d\n", ret);
3519                                         pci_disable_msix(np->pci_dev);
3520                                         np->msi_flags &= ~NV_MSI_X_ENABLED;
3521                                         goto out_err;
3522                                 }
3523                                 /* Request irq for tx handling */
3524                                 if (request_irq(np->msi_x_entry[NV_MSI_X_VECTOR_TX].vector, &nv_nic_irq_tx, IRQF_SHARED, dev->name, dev) != 0) {
3525                                         printk(KERN_INFO "forcedeth: request_irq failed for tx %d\n", ret);
3526                                         pci_disable_msix(np->pci_dev);
3527                                         np->msi_flags &= ~NV_MSI_X_ENABLED;
3528                                         goto out_free_rx;
3529                                 }
3530                                 /* Request irq for link and timer handling */
3531                                 if (request_irq(np->msi_x_entry[NV_MSI_X_VECTOR_OTHER].vector, &nv_nic_irq_other, IRQF_SHARED, dev->name, dev) != 0) {
3532                                         printk(KERN_INFO "forcedeth: request_irq failed for link %d\n", ret);
3533                                         pci_disable_msix(np->pci_dev);
3534                                         np->msi_flags &= ~NV_MSI_X_ENABLED;
3535                                         goto out_free_tx;
3536                                 }
3537                                 /* map interrupts to their respective vector */
3538                                 writel(0, base + NvRegMSIXMap0);
3539                                 writel(0, base + NvRegMSIXMap1);
3540                                 set_msix_vector_map(dev, NV_MSI_X_VECTOR_RX, NVREG_IRQ_RX_ALL);
3541                                 set_msix_vector_map(dev, NV_MSI_X_VECTOR_TX, NVREG_IRQ_TX_ALL);
3542                                 set_msix_vector_map(dev, NV_MSI_X_VECTOR_OTHER, NVREG_IRQ_OTHER);
3543                         } else {
3544                                 /* Request irq for all interrupts */
3545                                 if (request_irq(np->msi_x_entry[NV_MSI_X_VECTOR_ALL].vector, handler, IRQF_SHARED, dev->name, dev) != 0) {
3546                                         printk(KERN_INFO "forcedeth: request_irq failed %d\n", ret);
3547                                         pci_disable_msix(np->pci_dev);
3548                                         np->msi_flags &= ~NV_MSI_X_ENABLED;
3549                                         goto out_err;
3550                                 }
3551
3552                                 /* map interrupts to vector 0 */
3553                                 writel(0, base + NvRegMSIXMap0);
3554                                 writel(0, base + NvRegMSIXMap1);
3555                         }
3556                 }
3557         }
3558         if (ret != 0 && np->msi_flags & NV_MSI_CAPABLE) {
3559                 if ((ret = pci_enable_msi(np->pci_dev)) == 0) {
3560                         np->msi_flags |= NV_MSI_ENABLED;
3561                         if (request_irq(np->pci_dev->irq, handler, IRQF_SHARED, dev->name, dev) != 0) {
3562                                 printk(KERN_INFO "forcedeth: request_irq failed %d\n", ret);
3563                                 pci_disable_msi(np->pci_dev);
3564                                 np->msi_flags &= ~NV_MSI_ENABLED;
3565                                 goto out_err;
3566                         }
3567
3568                         /* map interrupts to vector 0 */
3569                         writel(0, base + NvRegMSIMap0);
3570                         writel(0, base + NvRegMSIMap1);
3571                         /* enable msi vector 0 */
3572                         writel(NVREG_MSI_VECTOR_0_ENABLED, base + NvRegMSIIrqMask);
3573                 }
3574         }
3575         if (ret != 0) {
3576                 if (request_irq(np->pci_dev->irq, handler, IRQF_SHARED, dev->name, dev) != 0)
3577                         goto out_err;
3578
3579         }
3580
3581         return 0;
3582 out_free_tx:
3583         free_irq(np->msi_x_entry[NV_MSI_X_VECTOR_TX].vector, dev);
3584 out_free_rx:
3585         free_irq(np->msi_x_entry[NV_MSI_X_VECTOR_RX].vector, dev);
3586 out_err:
3587         return 1;
3588 }
3589
3590 static void nv_free_irq(struct net_device *dev)
3591 {
3592         struct fe_priv *np = get_nvpriv(dev);
3593         int i;
3594
3595         if (np->msi_flags & NV_MSI_X_ENABLED) {
3596                 for (i = 0; i < (np->msi_flags & NV_MSI_X_VECTORS_MASK); i++) {
3597                         free_irq(np->msi_x_entry[i].vector, dev);
3598                 }
3599                 pci_disable_msix(np->pci_dev);
3600                 np->msi_flags &= ~NV_MSI_X_ENABLED;
3601         } else {
3602                 free_irq(np->pci_dev->irq, dev);
3603                 if (np->msi_flags & NV_MSI_ENABLED) {
3604                         pci_disable_msi(np->pci_dev);
3605                         np->msi_flags &= ~NV_MSI_ENABLED;
3606                 }
3607         }
3608 }
3609
3610 static void nv_do_nic_poll(unsigned long data)
3611 {
3612         struct net_device *dev = (struct net_device *) data;
3613         struct fe_priv *np = netdev_priv(dev);
3614         u8 __iomem *base = get_hwbase(dev);
3615         u32 mask = 0;
3616
3617         /*
3618          * First disable irq(s) and then
3619          * reenable interrupts on the nic, we have to do this before calling
3620          * nv_nic_irq because that may decide to do otherwise
3621          */
3622
3623         if (!using_multi_irqs(dev)) {
3624                 if (np->msi_flags & NV_MSI_X_ENABLED)
3625                         disable_irq_lockdep(np->msi_x_entry[NV_MSI_X_VECTOR_ALL].vector);
3626                 else
3627                         disable_irq_lockdep(dev->irq);
3628                 mask = np->irqmask;
3629         } else {
3630                 if (np->nic_poll_irq & NVREG_IRQ_RX_ALL) {
3631                         disable_irq_lockdep(np->msi_x_entry[NV_MSI_X_VECTOR_RX].vector);
3632                         mask |= NVREG_IRQ_RX_ALL;
3633                 }
3634                 if (np->nic_poll_irq & NVREG_IRQ_TX_ALL) {
3635                         disable_irq_lockdep(np->msi_x_entry[NV_MSI_X_VECTOR_TX].vector);
3636                         mask |= NVREG_IRQ_TX_ALL;
3637                 }
3638                 if (np->nic_poll_irq & NVREG_IRQ_OTHER) {
3639                         disable_irq_lockdep(np->msi_x_entry[NV_MSI_X_VECTOR_OTHER].vector);
3640                         mask |= NVREG_IRQ_OTHER;
3641                 }
3642         }
3643         np->nic_poll_irq = 0;
3644
3645         if (np->recover_error) {
3646                 np->recover_error = 0;
3647                 printk(KERN_INFO "forcedeth: MAC in recoverable error state\n");
3648                 if (netif_running(dev)) {
3649                         netif_tx_lock_bh(dev);
3650                         spin_lock(&np->lock);
3651                         /* stop engines */
3652                         nv_stop_rx(dev);
3653                         nv_stop_tx(dev);
3654                         nv_txrx_reset(dev);
3655                         /* drain rx queue */
3656                         nv_drain_rx(dev);
3657                         nv_drain_tx(dev);
3658                         /* reinit driver view of the rx queue */
3659                         set_bufsize(dev);
3660                         if (nv_init_ring(dev)) {
3661                                 if (!np->in_shutdown)
3662                                         mod_timer(&np->oom_kick, jiffies + OOM_REFILL);
3663                         }
3664                         /* reinit nic view of the rx queue */
3665                         writel(np->rx_buf_sz, base + NvRegOffloadConfig);
3666                         setup_hw_rings(dev, NV_SETUP_RX_RING | NV_SETUP_TX_RING);
3667                         writel( ((np->rx_ring_size-1) << NVREG_RINGSZ_RXSHIFT) + ((np->tx_ring_size-1) << NVREG_RINGSZ_TXSHIFT),
3668                                 base + NvRegRingSizes);
3669                         pci_push(base);
3670                         writel(NVREG_TXRXCTL_KICK|np->txrxctl_bits, get_hwbase(dev) + NvRegTxRxControl);
3671                         pci_push(base);
3672
3673                         /* restart rx engine */
3674                         nv_start_rx(dev);
3675                         nv_start_tx(dev);
3676                         spin_unlock(&np->lock);
3677                         netif_tx_unlock_bh(dev);
3678                 }
3679         }
3680
3681         /* FIXME: Do we need synchronize_irq(dev->irq) here? */
3682
3683         writel(mask, base + NvRegIrqMask);
3684         pci_push(base);
3685
3686         if (!using_multi_irqs(dev)) {
3687                 if (np->desc_ver == DESC_VER_3)
3688                         nv_nic_irq_optimized(0, dev);
3689                 else
3690                         nv_nic_irq(0, dev);
3691                 if (np->msi_flags & NV_MSI_X_ENABLED)
3692                         enable_irq_lockdep(np->msi_x_entry[NV_MSI_X_VECTOR_ALL].vector);
3693                 else
3694                         enable_irq_lockdep(dev->irq);
3695         } else {
3696                 if (np->nic_poll_irq & NVREG_IRQ_RX_ALL) {
3697                         nv_nic_irq_rx(0, dev);
3698                         enable_irq_lockdep(np->msi_x_entry[NV_MSI_X_VECTOR_RX].vector);
3699                 }
3700                 if (np->nic_poll_irq & NVREG_IRQ_TX_ALL) {
3701                         nv_nic_irq_tx(0, dev);
3702                         enable_irq_lockdep(np->msi_x_entry[NV_MSI_X_VECTOR_TX].vector);
3703                 }
3704                 if (np->nic_poll_irq & NVREG_IRQ_OTHER) {
3705                         nv_nic_irq_other(0, dev);
3706                         enable_irq_lockdep(np->msi_x_entry[NV_MSI_X_VECTOR_OTHER].vector);
3707                 }
3708         }
3709 }
3710
3711 #ifdef CONFIG_NET_POLL_CONTROLLER
3712 static void nv_poll_controller(struct net_device *dev)
3713 {
3714         nv_do_nic_poll((unsigned long) dev);
3715 }
3716 #endif
3717
3718 static void nv_do_stats_poll(unsigned long data)
3719 {
3720         struct net_device *dev = (struct net_device *) data;
3721         struct fe_priv *np = netdev_priv(dev);
3722
3723         nv_get_hw_stats(dev);
3724
3725         if (!np->in_shutdown)
3726                 mod_timer(&np->stats_poll, jiffies + STATS_INTERVAL);
3727 }
3728
3729 static void nv_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
3730 {
3731         struct fe_priv *np = netdev_priv(dev);
3732         strcpy(info->driver, DRV_NAME);
3733         strcpy(info->version, FORCEDETH_VERSION);
3734         strcpy(info->bus_info, pci_name(np->pci_dev));
3735 }
3736
3737 static void nv_get_wol(struct net_device *dev, struct ethtool_wolinfo *wolinfo)
3738 {
3739         struct fe_priv *np = netdev_priv(dev);
3740         wolinfo->supported = WAKE_MAGIC;
3741
3742         spin_lock_irq(&np->lock);
3743         if (np->wolenabled)
3744                 wolinfo->wolopts = WAKE_MAGIC;
3745         spin_unlock_irq(&np->lock);
3746 }
3747
3748 static int nv_set_wol(struct net_device *dev, struct ethtool_wolinfo *wolinfo)
3749 {
3750         struct fe_priv *np = netdev_priv(dev);
3751         u8 __iomem *base = get_hwbase(dev);
3752         u32 flags = 0;
3753
3754         if (wolinfo->wolopts == 0) {
3755                 np->wolenabled = 0;
3756         } else if (wolinfo->wolopts & WAKE_MAGIC) {
3757                 np->wolenabled = 1;
3758                 flags = NVREG_WAKEUPFLAGS_ENABLE;
3759         }
3760         if (netif_running(dev)) {
3761                 spin_lock_irq(&np->lock);
3762                 writel(flags, base + NvRegWakeUpFlags);
3763                 spin_unlock_irq(&np->lock);
3764         }
3765         return 0;
3766 }
3767
3768 static int nv_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
3769 {
3770         struct fe_priv *np = netdev_priv(dev);
3771         int adv;
3772
3773         spin_lock_irq(&np->lock);
3774         ecmd->port = PORT_MII;
3775         if (!netif_running(dev)) {
3776                 /* We do not track link speed / duplex setting if the
3777                  * interface is disabled. Force a link check */
3778                 if (nv_update_linkspeed(dev)) {
3779                         if (!netif_carrier_ok(dev))
3780                                 netif_carrier_on(dev);
3781                 } else {
3782                         if (netif_carrier_ok(dev))
3783                                 netif_carrier_off(dev);
3784                 }
3785         }
3786
3787         if (netif_carrier_ok(dev)) {
3788                 switch(np->linkspeed & (NVREG_LINKSPEED_MASK)) {
3789                 case NVREG_LINKSPEED_10:
3790                         ecmd->speed = SPEED_10;
3791                         break;
3792                 case NVREG_LINKSPEED_100:
3793                         ecmd->speed = SPEED_100;
3794                         break;
3795                 case NVREG_LINKSPEED_1000:
3796                         ecmd->speed = SPEED_1000;
3797                         break;
3798                 }
3799                 ecmd->duplex = DUPLEX_HALF;
3800                 if (np->duplex)
3801                         ecmd->duplex = DUPLEX_FULL;
3802         } else {
3803                 ecmd->speed = -1;
3804                 ecmd->duplex = -1;
3805         }
3806
3807         ecmd->autoneg = np->autoneg;
3808
3809         ecmd->advertising = ADVERTISED_MII;
3810         if (np->autoneg) {
3811                 ecmd->advertising |= ADVERTISED_Autoneg;
3812                 adv = mii_rw(dev, np->phyaddr, MII_ADVERTISE, MII_READ);
3813                 if (adv & ADVERTISE_10HALF)
3814                         ecmd->advertising |= ADVERTISED_10baseT_Half;
3815                 if (adv & ADVERTISE_10FULL)
3816                         ecmd->advertising |= ADVERTISED_10baseT_Full;
3817                 if (adv & ADVERTISE_100HALF)
3818                         ecmd->advertising |= ADVERTISED_100baseT_Half;
3819                 if (adv & ADVERTISE_100FULL)
3820                         ecmd->advertising |= ADVERTISED_100baseT_Full;
3821                 if (np->gigabit == PHY_GIGABIT) {
3822                         adv = mii_rw(dev, np->phyaddr, MII_CTRL1000, MII_READ);
3823                         if (adv & ADVERTISE_1000FULL)
3824                                 ecmd->advertising |= ADVERTISED_1000baseT_Full;
3825                 }
3826         }
3827         ecmd->supported = (SUPPORTED_Autoneg |
3828                 SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
3829                 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
3830                 SUPPORTED_MII);
3831         if (np->gigabit == PHY_GIGABIT)
3832                 ecmd->supported |= SUPPORTED_1000baseT_Full;
3833
3834         ecmd->phy_address = np->phyaddr;
3835         ecmd->transceiver = XCVR_EXTERNAL;
3836
3837         /* ignore maxtxpkt, maxrxpkt for now */
3838         spin_unlock_irq(&np->lock);
3839         return 0;
3840 }
3841
3842 static int nv_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
3843 {
3844         struct fe_priv *np = netdev_priv(dev);
3845
3846         if (ecmd->port != PORT_MII)
3847                 return -EINVAL;
3848         if (ecmd->transceiver != XCVR_EXTERNAL)
3849                 return -EINVAL;
3850         if (ecmd->phy_address != np->phyaddr) {
3851                 /* TODO: support switching between multiple phys. Should be
3852                  * trivial, but not enabled due to lack of test hardware. */
3853                 return -EINVAL;
3854         }
3855         if (ecmd->autoneg == AUTONEG_ENABLE) {
3856                 u32 mask;
3857
3858                 mask = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3859                           ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
3860                 if (np->gigabit == PHY_GIGABIT)
3861                         mask |= ADVERTISED_1000baseT_Full;
3862
3863                 if ((ecmd->advertising & mask) == 0)
3864                         return -EINVAL;
3865
3866         } else if (ecmd->autoneg == AUTONEG_DISABLE) {
3867                 /* Note: autonegotiation disable, speed 1000 intentionally
3868                  * forbidden - noone should need that. */
3869
3870                 if (ecmd->speed != SPEED_10 && ecmd->speed != SPEED_100)
3871                         return -EINVAL;
3872                 if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL)
3873                         return -EINVAL;
3874         } else {
3875                 return -EINVAL;
3876         }
3877
3878         netif_carrier_off(dev);
3879         if (netif_running(dev)) {
3880                 nv_disable_irq(dev);
3881                 netif_tx_lock_bh(dev);
3882                 spin_lock(&np->lock);
3883                 /* stop engines */
3884                 nv_stop_rx(dev);
3885                 nv_stop_tx(dev);
3886                 spin_unlock(&np->lock);
3887                 netif_tx_unlock_bh(dev);
3888         }
3889
3890         if (ecmd->autoneg == AUTONEG_ENABLE) {
3891                 int adv, bmcr;
3892
3893                 np->autoneg = 1;
3894
3895                 /* advertise only what has been requested */
3896                 adv = mii_rw(dev, np->phyaddr, MII_ADVERTISE, MII_READ);
3897                 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3898                 if (ecmd->advertising & ADVERTISED_10baseT_Half)
3899                         adv |= ADVERTISE_10HALF;
3900                 if (ecmd->advertising & ADVERTISED_10baseT_Full)
3901                         adv |= ADVERTISE_10FULL;
3902                 if (ecmd->advertising & ADVERTISED_100baseT_Half)
3903                         adv |= ADVERTISE_100HALF;
3904                 if (ecmd->advertising & ADVERTISED_100baseT_Full)
3905                         adv |= ADVERTISE_100FULL;
3906                 if (np->pause_flags & NV_PAUSEFRAME_RX_REQ)  /* for rx we set both advertisments but disable tx pause */
3907                         adv |=  ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3908                 if (np->pause_flags & NV_PAUSEFRAME_TX_REQ)
3909                         adv |=  ADVERTISE_PAUSE_ASYM;
3910                 mii_rw(dev, np->phyaddr, MII_ADVERTISE, adv);
3911
3912                 if (np->gigabit == PHY_GIGABIT) {
3913                         adv = mii_rw(dev, np->phyaddr, MII_CTRL1000, MII_READ);
3914                         adv &= ~ADVERTISE_1000FULL;
3915                         if (ecmd->advertising & ADVERTISED_1000baseT_Full)
3916                                 adv |= ADVERTISE_1000FULL;
3917                         mii_rw(dev, np->phyaddr, MII_CTRL1000, adv);
3918                 }
3919
3920                 if (netif_running(dev))
3921                         printk(KERN_INFO "%s: link down.\n", dev->name);
3922                 bmcr = mii_rw(dev, np->phyaddr, MII_BMCR, MII_READ);
3923                 if (np->phy_model == PHY_MODEL_MARVELL_E3016) {
3924                         bmcr |= BMCR_ANENABLE;
3925                         /* reset the phy in order for settings to stick,
3926                          * and cause autoneg to start */
3927                         if (phy_reset(dev, bmcr)) {
3928                                 printk(KERN_INFO "%s: phy reset failed\n", dev->name);
3929                                 return -EINVAL;
3930                         }
3931                 } else {
3932                         bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
3933                         mii_rw(dev, np->phyaddr, MII_BMCR, bmcr);
3934                 }
3935         } else {
3936                 int adv, bmcr;
3937
3938                 np->autoneg = 0;
3939
3940                 adv = mii_rw(dev, np->phyaddr, MII_ADVERTISE, MII_READ);
3941                 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3942                 if (ecmd->speed == SPEED_10 && ecmd->duplex == DUPLEX_HALF)
3943                         adv |= ADVERTISE_10HALF;
3944                 if (ecmd->speed == SPEED_10 && ecmd->duplex == DUPLEX_FULL)
3945                         adv |= ADVERTISE_10FULL;
3946                 if (ecmd->speed == SPEED_100 && ecmd->duplex == DUPLEX_HALF)
3947                         adv |= ADVERTISE_100HALF;
3948                 if (ecmd->speed == SPEED_100 && ecmd->duplex == DUPLEX_FULL)
3949                         adv |= ADVERTISE_100FULL;
3950                 np->pause_flags &= ~(NV_PAUSEFRAME_AUTONEG|NV_PAUSEFRAME_RX_ENABLE|NV_PAUSEFRAME_TX_ENABLE);
3951                 if (np->pause_flags & NV_PAUSEFRAME_RX_REQ) {/* for rx we set both advertisments but disable tx pause */
3952                         adv |=  ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3953                         np->pause_flags |= NV_PAUSEFRAME_RX_ENABLE;
3954                 }
3955                 if (np->pause_flags & NV_PAUSEFRAME_TX_REQ) {
3956                         adv |=  ADVERTISE_PAUSE_ASYM;
3957                         np->pause_flags |= NV_PAUSEFRAME_TX_ENABLE;
3958                 }
3959                 mii_rw(dev, np->phyaddr, MII_ADVERTISE, adv);
3960                 np->fixed_mode = adv;
3961
3962                 if (np->gigabit == PHY_GIGABIT) {
3963                         adv = mii_rw(dev, np->phyaddr, MII_CTRL1000, MII_READ);
3964                         adv &= ~ADVERTISE_1000FULL;
3965                         mii_rw(dev, np->phyaddr, MII_CTRL1000, adv);
3966                 }
3967
3968                 bmcr = mii_rw(dev, np->phyaddr, MII_BMCR, MII_READ);
3969                 bmcr &= ~(BMCR_ANENABLE|BMCR_SPEED100|BMCR_SPEED1000|BMCR_FULLDPLX);
3970                 if (np->fixed_mode & (ADVERTISE_10FULL|ADVERTISE_100FULL))
3971                         bmcr |= BMCR_FULLDPLX;
3972                 if (np->fixed_mode & (ADVERTISE_100HALF|ADVERTISE_100FULL))
3973                         bmcr |= BMCR_SPEED100;
3974                 if (np->phy_oui == PHY_OUI_MARVELL) {
3975                         /* reset the phy in order for forced mode settings to stick */
3976                         if (phy_reset(dev, bmcr)) {
3977                                 printk(KERN_INFO "%s: phy reset failed\n", dev->name);
3978                                 return -EINVAL;
3979                         }
3980                 } else {
3981                         mii_rw(dev, np->phyaddr, MII_BMCR, bmcr);
3982                         if (netif_running(dev)) {
3983                                 /* Wait a bit and then reconfigure the nic. */
3984                                 udelay(10);
3985                                 nv_linkchange(dev);
3986                         }
3987                 }
3988         }
3989
3990         if (netif_running(dev)) {
3991                 nv_start_rx(dev);
3992                 nv_start_tx(dev);
3993                 nv_enable_irq(dev);
3994         }
3995
3996         return 0;
3997 }
3998
3999 #define FORCEDETH_REGS_VER      1
4000
4001 static int nv_get_regs_len(struct net_device *dev)
4002 {
4003         struct fe_priv *np = netdev_priv(dev);
4004         return np->register_size;
4005 }
4006
4007 static void nv_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *buf)
4008 {
4009         struct fe_priv *np = netdev_priv(dev);
4010         u8 __iomem *base = get_hwbase(dev);
4011         u32 *rbuf = buf;
4012         int i;
4013
4014         regs->version = FORCEDETH_REGS_VER;
4015         spin_lock_irq(&np->lock);
4016         for (i = 0;i <= np->register_size/sizeof(u32); i++)
4017                 rbuf[i] = readl(base + i*sizeof(u32));
4018         spin_unlock_irq(&np->lock);
4019 }
4020
4021 static int nv_nway_reset(struct net_device *dev)
4022 {
4023         struct fe_priv *np = netdev_priv(dev);
4024         int ret;
4025
4026         if (np->autoneg) {
4027                 int bmcr;
4028
4029                 netif_carrier_off(dev);
4030                 if (netif_running(dev)) {
4031                         nv_disable_irq(dev);
4032                         netif_tx_lock_bh(dev);
4033                         spin_lock(&np->lock);
4034                         /* stop engines */
4035                         nv_stop_rx(dev);
4036                         nv_stop_tx(dev);
4037                         spin_unlock(&np->lock);
4038                         netif_tx_unlock_bh(dev);
4039                         printk(KERN_INFO "%s: link down.\n", dev->name);
4040                 }
4041
4042                 bmcr = mii_rw(dev, np->phyaddr, MII_BMCR, MII_READ);
4043                 if (np->phy_model == PHY_MODEL_MARVELL_E3016) {
4044                         bmcr |= BMCR_ANENABLE;
4045                         /* reset the phy in order for settings to stick*/
4046                         if (phy_reset(dev, bmcr)) {
4047                                 printk(KERN_INFO "%s: phy reset failed\n", dev->name);
4048                                 return -EINVAL;
4049                         }
4050                 } else {
4051                         bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
4052                         mii_rw(dev, np->phyaddr, MII_BMCR, bmcr);
4053                 }
4054
4055                 if (netif_running(dev)) {
4056                         nv_start_rx(dev);
4057                         nv_start_tx(dev);
4058                         nv_enable_irq(dev);
4059                 }
4060                 ret = 0;
4061         } else {
4062                 ret = -EINVAL;
4063         }
4064
4065         return ret;
4066 }
4067
4068 static int nv_set_tso(struct net_device *dev, u32 value)
4069 {
4070         struct fe_priv *np = netdev_priv(dev);
4071
4072         if ((np->driver_data & DEV_HAS_CHECKSUM))
4073                 return ethtool_op_set_tso(dev, value);
4074         else
4075                 return -EOPNOTSUPP;
4076 }
4077
4078 static void nv_get_ringparam(struct net_device *dev, struct ethtool_ringparam* ring)
4079 {
4080         struct fe_priv *np = netdev_priv(dev);
4081
4082         ring->rx_max_pending = (np->desc_ver == DESC_VER_1) ? RING_MAX_DESC_VER_1 : RING_MAX_DESC_VER_2_3;
4083         ring->rx_mini_max_pending = 0;
4084         ring->rx_jumbo_max_pending = 0;
4085         ring->tx_max_pending = (np->desc_ver == DESC_VER_1) ? RING_MAX_DESC_VER_1 : RING_MAX_DESC_VER_2_3;
4086
4087         ring->rx_pending = np->rx_ring_size;
4088         ring->rx_mini_pending = 0;
4089         ring->rx_jumbo_pending = 0;
4090         ring->tx_pending = np->tx_ring_size;
4091 }
4092
4093 static int nv_set_ringparam(struct net_device *dev, struct ethtool_ringparam* ring)
4094 {
4095         struct fe_priv *np = netdev_priv(dev);
4096         u8 __iomem *base = get_hwbase(dev);
4097         u8 *rxtx_ring, *rx_skbuff, *tx_skbuff;
4098         dma_addr_t ring_addr;
4099
4100         if (ring->rx_pending < RX_RING_MIN ||
4101             ring->tx_pending < TX_RING_MIN ||
4102             ring->rx_mini_pending != 0 ||
4103             ring->rx_jumbo_pending != 0 ||
4104             (np->desc_ver == DESC_VER_1 &&
4105              (ring->rx_pending > RING_MAX_DESC_VER_1 ||
4106               ring->tx_pending > RING_MAX_DESC_VER_1)) ||
4107             (np->desc_ver != DESC_VER_1 &&
4108              (ring->rx_pending > RING_MAX_DESC_VER_2_3 ||
4109               ring->tx_pending > RING_MAX_DESC_VER_2_3))) {
4110                 return -EINVAL;
4111         }
4112
4113         /* allocate new rings */
4114         if (np->desc_ver == DESC_VER_1 || np->desc_ver == DESC_VER_2) {
4115                 rxtx_ring = pci_alloc_consistent(np->pci_dev,
4116                                             sizeof(struct ring_desc) * (ring->rx_pending + ring->tx_pending),
4117                                             &ring_addr);
4118         } else {
4119                 rxtx_ring = pci_alloc_consistent(np->pci_dev,
4120                                             sizeof(struct ring_desc_ex) * (ring->rx_pending + ring->tx_pending),
4121                                             &ring_addr);
4122         }
4123         rx_skbuff = kmalloc(sizeof(struct nv_skb_map) * ring->rx_pending, GFP_KERNEL);
4124         tx_skbuff = kmalloc(sizeof(struct nv_skb_map) * ring->tx_pending, GFP_KERNEL);
4125         if (!rxtx_ring || !rx_skbuff || !tx_skbuff) {
4126                 /* fall back to old rings */
4127                 if (np->desc_ver == DESC_VER_1 || np->desc_ver == DESC_VER_2) {
4128                         if (rxtx_ring)
4129                                 pci_free_consistent(np->pci_dev, sizeof(struct ring_desc) * (ring->rx_pending + ring->tx_pending),
4130                                                     rxtx_ring, ring_addr);
4131                 } else {
4132                         if (rxtx_ring)
4133                                 pci_free_consistent(np->pci_dev, sizeof(struct ring_desc_ex) * (ring->rx_pending + ring->tx_pending),
4134                                                     rxtx_ring, ring_addr);
4135                 }
4136                 if (rx_skbuff)
4137                         kfree(rx_skbuff);
4138                 if (tx_skbuff)
4139                         kfree(tx_skbuff);
4140                 goto exit;
4141         }
4142
4143         if (netif_running(dev)) {
4144                 nv_disable_irq(dev);
4145                 netif_tx_lock_bh(dev);
4146                 spin_lock(&np->lock);
4147                 /* stop engines */
4148                 nv_stop_rx(dev);
4149                 nv_stop_tx(dev);
4150                 nv_txrx_reset(dev);
4151                 /* drain queues */
4152                 nv_drain_rx(dev);
4153                 nv_drain_tx(dev);
4154                 /* delete queues */
4155                 free_rings(dev);
4156         }
4157
4158         /* set new values */
4159         np->rx_ring_size = ring->rx_pending;
4160         np->tx_ring_size = ring->tx_pending;
4161         if (np->desc_ver == DESC_VER_1 || np->desc_ver == DESC_VER_2) {
4162                 np->rx_ring.orig = (struct ring_desc*)rxtx_ring;
4163                 np->tx_ring.orig = &np->rx_ring.orig[np->rx_ring_size];
4164         } else {
4165                 np->rx_ring.ex = (struct ring_desc_ex*)rxtx_ring;
4166                 np->tx_ring.ex = &np->rx_ring.ex[np->rx_ring_size];
4167         }
4168         np->rx_skb = (struct nv_skb_map*)rx_skbuff;
4169         np->tx_skb = (struct nv_skb_map*)tx_skbuff;
4170         np->ring_addr = ring_addr;
4171
4172         memset(np->rx_skb, 0, sizeof(struct nv_skb_map) * np->rx_ring_size);
4173         memset(np->tx_skb, 0, sizeof(struct nv_skb_map) * np->tx_ring_size);
4174
4175         if (netif_running(dev)) {
4176                 /* reinit driver view of the queues */
4177                 set_bufsize(dev);
4178                 if (nv_init_ring(dev)) {
4179                         if (!np->in_shutdown)
4180                                 mod_timer(&np->oom_kick, jiffies + OOM_REFILL);
4181                 }
4182
4183                 /* reinit nic view of the queues */
4184                 writel(np->rx_buf_sz, base + NvRegOffloadConfig);
4185                 setup_hw_rings(dev, NV_SETUP_RX_RING | NV_SETUP_TX_RING);
4186                 writel( ((np->rx_ring_size-1) << NVREG_RINGSZ_RXSHIFT) + ((np->tx_ring_size-1) << NVREG_RINGSZ_TXSHIFT),
4187                         base + NvRegRingSizes);
4188                 pci_push(base);
4189                 writel(NVREG_TXRXCTL_KICK|np->txrxctl_bits, get_hwbase(dev) + NvRegTxRxControl);
4190                 pci_push(base);
4191
4192                 /* restart engines */
4193                 nv_start_rx(dev);
4194                 nv_start_tx(dev);
4195                 spin_unlock(&np->lock);
4196                 netif_tx_unlock_bh(dev);
4197                 nv_enable_irq(dev);
4198         }
4199         return 0;
4200 exit:
4201         return -ENOMEM;
4202 }
4203
4204 static void nv_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam* pause)
4205 {
4206         struct fe_priv *np = netdev_priv(dev);
4207
4208         pause->autoneg = (np->pause_flags & NV_PAUSEFRAME_AUTONEG) != 0;
4209         pause->rx_pause = (np->pause_flags & NV_PAUSEFRAME_RX_ENABLE) != 0;
4210         pause->tx_pause = (np->pause_flags & NV_PAUSEFRAME_TX_ENABLE) != 0;
4211 }
4212
4213 static int nv_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam* pause)
4214 {
4215         struct fe_priv *np = netdev_priv(dev);
4216         int adv, bmcr;
4217
4218         if ((!np->autoneg && np->duplex == 0) ||
4219             (np->autoneg && !pause->autoneg && np->duplex == 0)) {
4220                 printk(KERN_INFO "%s: can not set pause settings when forced link is in half duplex.\n",
4221                        dev->name);
4222                 return -EINVAL;
4223         }
4224         if (pause->tx_pause && !(np->pause_flags & NV_PAUSEFRAME_TX_CAPABLE)) {
4225                 printk(KERN_INFO "%s: hardware does not support tx pause frames.\n", dev->name);
4226                 return -EINVAL;
4227         }
4228
4229         netif_carrier_off(dev);
4230         if (netif_running(dev)) {
4231                 nv_disable_irq(dev);
4232                 netif_tx_lock_bh(dev);
4233                 spin_lock(&np->lock);
4234                 /* stop engines */
4235                 nv_stop_rx(dev);
4236                 nv_stop_tx(dev);
4237                 spin_unlock(&np->lock);
4238                 netif_tx_unlock_bh(dev);
4239         }
4240
4241         np->pause_flags &= ~(NV_PAUSEFRAME_RX_REQ|NV_PAUSEFRAME_TX_REQ);
4242         if (pause->rx_pause)
4243                 np->pause_flags |= NV_PAUSEFRAME_RX_REQ;
4244         if (pause->tx_pause)
4245                 np->pause_flags |= NV_PAUSEFRAME_TX_REQ;
4246
4247         if (np->autoneg && pause->autoneg) {
4248                 np->pause_flags |= NV_PAUSEFRAME_AUTONEG;
4249
4250                 adv = mii_rw(dev, np->phyaddr, MII_ADVERTISE, MII_READ);
4251                 adv &= ~(ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
4252                 if (np->pause_flags & NV_PAUSEFRAME_RX_REQ) /* for rx we set both advertisments but disable tx pause */
4253                         adv |=  ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4254                 if (np->pause_flags & NV_PAUSEFRAME_TX_REQ)
4255                         adv |=  ADVERTISE_PAUSE_ASYM;
4256                 mii_rw(dev, np->phyaddr, MII_ADVERTISE, adv);
4257
4258                 if (netif_running(dev))
4259                         printk(KERN_INFO "%s: link down.\n", dev->name);
4260                 bmcr = mii_rw(dev, np->phyaddr, MII_BMCR, MII_READ);
4261                 bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
4262                 mii_rw(dev, np->phyaddr, MII_BMCR, bmcr);
4263         } else {
4264                 np->pause_flags &= ~(NV_PAUSEFRAME_AUTONEG|NV_PAUSEFRAME_RX_ENABLE|NV_PAUSEFRAME_TX_ENABLE);
4265                 if (pause->rx_pause)
4266                         np->pause_flags |= NV_PAUSEFRAME_RX_ENABLE;
4267                 if (pause->tx_pause)
4268                         np->pause_flags |= NV_PAUSEFRAME_TX_ENABLE;
4269
4270                 if (!netif_running(dev))
4271                         nv_update_linkspeed(dev);
4272                 else
4273                         nv_update_pause(dev, np->pause_flags);
4274         }
4275
4276         if (netif_running(dev)) {
4277                 nv_start_rx(dev);
4278                 nv_start_tx(dev);
4279                 nv_enable_irq(dev);
4280         }
4281         return 0;
4282 }
4283
4284 static u32 nv_get_rx_csum(struct net_device *dev)
4285 {
4286         struct fe_priv *np = netdev_priv(dev);
4287         return (np->rx_csum) != 0;
4288 }
4289
4290 static int nv_set_rx_csum(struct net_device *dev, u32 data)
4291 {
4292         struct fe_priv *np = netdev_priv(dev);
4293         u8 __iomem *base = get_hwbase(dev);
4294         int retcode = 0;
4295
4296         if (np->driver_data & DEV_HAS_CHECKSUM) {
4297                 if (data) {
4298                         np->rx_csum = 1;
4299                         np->txrxctl_bits |= NVREG_TXRXCTL_RXCHECK;
4300                 } else {
4301                         np->rx_csum = 0;
4302                         /* vlan is dependent on rx checksum offload */
4303                         if (!(np->vlanctl_bits & NVREG_VLANCONTROL_ENABLE))
4304                                 np->txrxctl_bits &= ~NVREG_TXRXCTL_RXCHECK;
4305                 }
4306                 if (netif_running(dev)) {
4307                         spin_lock_irq(&np->lock);
4308                         writel(np->txrxctl_bits, base + NvRegTxRxControl);
4309                         spin_unlock_irq(&np->lock);
4310                 }
4311         } else {
4312                 return -EINVAL;
4313         }
4314
4315         return retcode;
4316 }
4317
4318 static int nv_set_tx_csum(struct net_device *dev, u32 data)
4319 {
4320         struct fe_priv *np = netdev_priv(dev);
4321
4322         if (np->driver_data & DEV_HAS_CHECKSUM)
4323                 return ethtool_op_set_tx_hw_csum(dev, data);
4324         else
4325                 return -EOPNOTSUPP;
4326 }
4327
4328 static int nv_set_sg(struct net_device *dev, u32 data)
4329 {
4330         struct fe_priv *np = netdev_priv(dev);
4331
4332         if (np->driver_data & DEV_HAS_CHECKSUM)
4333                 return ethtool_op_set_sg(dev, data);
4334         else
4335                 return -EOPNOTSUPP;
4336 }
4337
4338 static int nv_get_sset_count(struct net_device *dev, int sset)
4339 {
4340         struct fe_priv *np = netdev_priv(dev);
4341
4342         switch (sset) {
4343         case ETH_SS_TEST:
4344                 if (np->driver_data & DEV_HAS_TEST_EXTENDED)
4345                         return NV_TEST_COUNT_EXTENDED;
4346                 else
4347                         return NV_TEST_COUNT_BASE;
4348         case ETH_SS_STATS:
4349                 if (np->driver_data & DEV_HAS_STATISTICS_V1)
4350                         return NV_DEV_STATISTICS_V1_COUNT;
4351                 else if (np->driver_data & DEV_HAS_STATISTICS_V2)
4352                         return NV_DEV_STATISTICS_V2_COUNT;
4353                 else
4354                         return 0;
4355         default:
4356                 return -EOPNOTSUPP;
4357         }
4358 }
4359
4360 static void nv_get_ethtool_stats(struct net_device *dev, struct ethtool_stats *estats, u64 *buffer)
4361 {
4362         struct fe_priv *np = netdev_priv(dev);
4363
4364         /* update stats */
4365         nv_do_stats_poll((unsigned long)dev);
4366
4367         memcpy(buffer, &np->estats, nv_get_sset_count(dev, ETH_SS_STATS)*sizeof(u64));
4368 }
4369
4370 static int nv_link_test(struct net_device *dev)
4371 {
4372         struct fe_priv *np = netdev_priv(dev);
4373         int mii_status;
4374
4375         mii_rw(dev, np->phyaddr, MII_BMSR, MII_READ);
4376         mii_status = mii_rw(dev, np->phyaddr, MII_BMSR, MII_READ);
4377
4378         /* check phy link status */
4379         if (!(mii_status & BMSR_LSTATUS))
4380                 return 0;
4381         else
4382                 return 1;
4383 }
4384
4385 static int nv_register_test(struct net_device *dev)
4386 {
4387         u8 __iomem *base = get_hwbase(dev);
4388         int i = 0;
4389         u32 orig_read, new_read;
4390
4391         do {
4392                 orig_read = readl(base + nv_registers_test[i].reg);
4393
4394                 /* xor with mask to toggle bits */
4395                 orig_read ^= nv_registers_test[i].mask;
4396
4397                 writel(orig_read, base + nv_registers_test[i].reg);
4398
4399                 new_read = readl(base + nv_registers_test[i].reg);
4400
4401                 if ((new_read & nv_registers_test[i].mask) != (orig_read & nv_registers_test[i].mask))
4402                         return 0;
4403
4404                 /* restore original value */
4405                 orig_read ^= nv_registers_test[i].mask;
4406                 writel(orig_read, base + nv_registers_test[i].reg);
4407
4408         } while (nv_registers_test[++i].reg != 0);
4409
4410         return 1;
4411 }
4412
4413 static int nv_interrupt_test(struct net_device *dev)
4414 {
4415         struct fe_priv *np = netdev_priv(dev);
4416         u8 __iomem *base = get_hwbase(dev);
4417         int ret = 1;
4418         int testcnt;
4419         u32 save_msi_flags, save_poll_interval = 0;
4420
4421         if (netif_running(dev)) {
4422                 /* free current irq */
4423                 nv_free_irq(dev);
4424                 save_poll_interval = readl(base+NvRegPollingInterval);
4425         }
4426
4427         /* flag to test interrupt handler */
4428         np->intr_test = 0;
4429
4430         /* setup test irq */
4431         save_msi_flags = np->msi_flags;
4432         np->msi_flags &= ~NV_MSI_X_VECTORS_MASK;
4433         np->msi_flags |= 0x001; /* setup 1 vector */
4434         if (nv_request_irq(dev, 1))
4435                 return 0;
4436
4437         /* setup timer interrupt */
4438         writel(NVREG_POLL_DEFAULT_CPU, base + NvRegPollingInterval);
4439         writel(NVREG_UNKSETUP6_VAL, base + NvRegUnknownSetupReg6);
4440
4441         nv_enable_hw_interrupts(dev, NVREG_IRQ_TIMER);
4442
4443         /* wait for at least one interrupt */
4444         msleep(100);
4445
4446         spin_lock_irq(&np->lock);
4447
4448         /* flag should be set within ISR */
4449         testcnt = np->intr_test;
4450         if (!testcnt)
4451                 ret = 2;
4452
4453         nv_disable_hw_interrupts(dev, NVREG_IRQ_TIMER);
4454         if (!(np->msi_flags & NV_MSI_X_ENABLED))
4455                 writel(NVREG_IRQSTAT_MASK, base + NvRegIrqStatus);
4456         else
4457                 writel(NVREG_IRQSTAT_MASK, base + NvRegMSIXIrqStatus);
4458
4459         spin_unlock_irq(&np->lock);
4460
4461         nv_free_irq(dev);
4462
4463         np->msi_flags = save_msi_flags;
4464
4465         if (netif_running(dev)) {
4466                 writel(save_poll_interval, base + NvRegPollingInterval);
4467                 writel(NVREG_UNKSETUP6_VAL, base + NvRegUnknownSetupReg6);
4468                 /* restore original irq */
4469                 if (nv_request_irq(dev, 0))
4470                         return 0;
4471         }
4472
4473         return ret;
4474 }
4475
4476 static int nv_loopback_test(struct net_device *dev)
4477 {
4478         struct fe_priv *np = netdev_priv(dev);
4479         u8 __iomem *base = get_hwbase(dev);
4480         struct sk_buff *tx_skb, *rx_skb;
4481         dma_addr_t test_dma_addr;
4482         u32 tx_flags_extra = (np->desc_ver == DESC_VER_1 ? NV_TX_LASTPACKET : NV_TX2_LASTPACKET);
4483         u32 flags;
4484         int len, i, pkt_len;
4485         u8 *pkt_data;
4486         u32 filter_flags = 0;
4487         u32 misc1_flags = 0;
4488         int ret = 1;
4489
4490         if (netif_running(dev)) {
4491                 nv_disable_irq(dev);
4492                 filter_flags = readl(base + NvRegPacketFilterFlags);
4493                 misc1_flags = readl(base + NvRegMisc1);
4494         } else {
4495                 nv_txrx_reset(dev);
4496         }
4497
4498         /* reinit driver view of the rx queue */
4499         set_bufsize(dev);
4500         nv_init_ring(dev);
4501
4502         /* setup hardware for loopback */
4503         writel(NVREG_MISC1_FORCE, base + NvRegMisc1);
4504         writel(NVREG_PFF_ALWAYS | NVREG_PFF_LOOPBACK, base + NvRegPacketFilterFlags);
4505
4506         /* reinit nic view of the rx queue */
4507         writel(np->rx_buf_sz, base + NvRegOffloadConfig);
4508         setup_hw_rings(dev, NV_SETUP_RX_RING | NV_SETUP_TX_RING);
4509         writel( ((np->rx_ring_size-1) << NVREG_RINGSZ_RXSHIFT) + ((np->tx_ring_size-1) << NVREG_RINGSZ_TXSHIFT),
4510                 base + NvRegRingSizes);
4511         pci_push(base);
4512
4513         /* restart rx engine */
4514         nv_start_rx(dev);
4515         nv_start_tx(dev);
4516
4517         /* setup packet for tx */
4518         pkt_len = ETH_DATA_LEN;
4519         tx_skb = dev_alloc_skb(pkt_len);
4520         if (!tx_skb) {
4521                 printk(KERN_ERR "dev_alloc_skb() failed during loopback test"
4522                          " of %s\n", dev->name);
4523                 ret = 0;
4524                 goto out;
4525         }
4526         test_dma_addr = pci_map_single(np->pci_dev, tx_skb->data,
4527                                        skb_tailroom(tx_skb),
4528                                        PCI_DMA_FROMDEVICE);
4529         pkt_data = skb_put(tx_skb, pkt_len);
4530         for (i = 0; i < pkt_len; i++)
4531                 pkt_data[i] = (u8)(i & 0xff);
4532
4533         if (np->desc_ver == DESC_VER_1 || np->desc_ver == DESC_VER_2) {
4534                 np->tx_ring.orig[0].buf = cpu_to_le32(test_dma_addr);
4535                 np->tx_ring.orig[0].flaglen = cpu_to_le32((pkt_len-1) | np->tx_flags | tx_flags_extra);
4536         } else {
4537                 np->tx_ring.ex[0].bufhigh = cpu_to_le64(test_dma_addr) >> 32;
4538                 np->tx_ring.ex[0].buflow = cpu_to_le64(test_dma_addr) & 0x0FFFFFFFF;
4539                 np->tx_ring.ex[0].flaglen = cpu_to_le32((pkt_len-1) | np->tx_flags | tx_flags_extra);
4540         }
4541         writel(NVREG_TXRXCTL_KICK|np->txrxctl_bits, get_hwbase(dev) + NvRegTxRxControl);
4542         pci_push(get_hwbase(dev));
4543
4544         msleep(500);
4545
4546         /* check for rx of the packet */
4547         if (np->desc_ver == DESC_VER_1 || np->desc_ver == DESC_VER_2) {
4548                 flags = le32_to_cpu(np->rx_ring.orig[0].flaglen);
4549                 len = nv_descr_getlength(&np->rx_ring.orig[0], np->desc_ver);
4550
4551         } else {
4552                 flags = le32_to_cpu(np->rx_ring.ex[0].flaglen);
4553                 len = nv_descr_getlength_ex(&np->rx_ring.ex[0], np->desc_ver);
4554         }
4555
4556         if (flags & NV_RX_AVAIL) {
4557                 ret = 0;
4558         } else if (np->desc_ver == DESC_VER_1) {
4559                 if (flags & NV_RX_ERROR)
4560                         ret = 0;
4561         } else {
4562                 if (flags & NV_RX2_ERROR) {
4563                         ret = 0;
4564                 }
4565         }
4566
4567         if (ret) {
4568                 if (len != pkt_len) {
4569                         ret = 0;
4570                         dprintk(KERN_DEBUG "%s: loopback len mismatch %d vs %d\n",
4571                                 dev->name, len, pkt_len);
4572                 } else {
4573                         rx_skb = np->rx_skb[0].skb;
4574                         for (i = 0; i < pkt_len; i++) {
4575                                 if (rx_skb->data[i] != (u8)(i & 0xff)) {
4576                                         ret = 0;
4577                                         dprintk(KERN_DEBUG "%s: loopback pattern check failed on byte %d\n",
4578                                                 dev->name, i);
4579                                         break;
4580                                 }
4581                         }
4582                 }
4583         } else {
4584                 dprintk(KERN_DEBUG "%s: loopback - did not receive test packet\n", dev->name);
4585         }
4586
4587         pci_unmap_page(np->pci_dev, test_dma_addr,
4588                        (skb_end_pointer(tx_skb) - tx_skb->data),
4589                        PCI_DMA_TODEVICE);
4590         dev_kfree_skb_any(tx_skb);
4591  out:
4592         /* stop engines */
4593         nv_stop_rx(dev);
4594         nv_stop_tx(dev);
4595         nv_txrx_reset(dev);
4596         /* drain rx queue */
4597         nv_drain_rx(dev);
4598         nv_drain_tx(dev);
4599
4600         if (netif_running(dev)) {
4601                 writel(misc1_flags, base + NvRegMisc1);
4602                 writel(filter_flags, base + NvRegPacketFilterFlags);
4603                 nv_enable_irq(dev);
4604         }
4605
4606         return ret;
4607 }
4608
4609 static void nv_self_test(struct net_device *dev, struct ethtool_test *test, u64 *buffer)
4610 {
4611         struct fe_priv *np = netdev_priv(dev);
4612         u8 __iomem *base = get_hwbase(dev);
4613         int result;
4614         memset(buffer, 0, nv_get_sset_count(dev, ETH_SS_TEST)*sizeof(u64));
4615
4616         if (!nv_link_test(dev)) {
4617                 test->flags |= ETH_TEST_FL_FAILED;
4618                 buffer[0] = 1;
4619         }
4620
4621         if (test->flags & ETH_TEST_FL_OFFLINE) {
4622                 if (netif_running(dev)) {
4623                         netif_stop_queue(dev);
4624 #ifdef CONFIG_FORCEDETH_NAPI
4625                         napi_disable(&np->napi);
4626 #endif
4627                         netif_tx_lock_bh(dev);
4628                         spin_lock_irq(&np->lock);
4629                         nv_disable_hw_interrupts(dev, np->irqmask);
4630                         if (!(np->msi_flags & NV_MSI_X_ENABLED)) {
4631                                 writel(NVREG_IRQSTAT_MASK, base + NvRegIrqStatus);
4632                         } else {
4633                                 writel(NVREG_IRQSTAT_MASK, base + NvRegMSIXIrqStatus);
4634                         }
4635                         /* stop engines */
4636                         nv_stop_rx(dev);
4637                         nv_stop_tx(dev);
4638                         nv_txrx_reset(dev);
4639                         /* drain rx queue */
4640                         nv_drain_rx(dev);
4641                         nv_drain_tx(dev);
4642                         spin_unlock_irq(&np->lock);
4643                         netif_tx_unlock_bh(dev);
4644                 }
4645
4646                 if (!nv_register_test(dev)) {
4647                         test->flags |= ETH_TEST_FL_FAILED;
4648                         buffer[1] = 1;
4649                 }
4650
4651                 result = nv_interrupt_test(dev);
4652                 if (result != 1) {
4653                         test->flags |= ETH_TEST_FL_FAILED;
4654                         buffer[2] = 1;
4655                 }
4656                 if (result == 0) {
4657                         /* bail out */
4658                         return;
4659                 }
4660
4661                 if (!nv_loopback_test(dev)) {
4662                         test->flags |= ETH_TEST_FL_FAILED;
4663                         buffer[3] = 1;
4664                 }
4665
4666                 if (netif_running(dev)) {
4667                         /* reinit driver view of the rx queue */
4668                         set_bufsize(dev);
4669                         if (nv_init_ring(dev)) {
4670                                 if (!np->in_shutdown)
4671                                         mod_timer(&np->oom_kick, jiffies + OOM_REFILL);
4672                         }
4673                         /* reinit nic view of the rx queue */
4674                         writel(np->rx_buf_sz, base + NvRegOffloadConfig);
4675                         setup_hw_rings(dev, NV_SETUP_RX_RING | NV_SETUP_TX_RING);
4676                         writel( ((np->rx_ring_size-1) << NVREG_RINGSZ_RXSHIFT) + ((np->tx_ring_size-1) << NVREG_RINGSZ_TXSHIFT),
4677                                 base + NvRegRingSizes);
4678                         pci_push(base);
4679                         writel(NVREG_TXRXCTL_KICK|np->txrxctl_bits, get_hwbase(dev) + NvRegTxRxControl);
4680                         pci_push(base);
4681                         /* restart rx engine */
4682                         nv_start_rx(dev);
4683                         nv_start_tx(dev);
4684                         netif_start_queue(dev);
4685 #ifdef CONFIG_FORCEDETH_NAPI
4686                         napi_enable(&np->napi);
4687 #endif
4688                         nv_enable_hw_interrupts(dev, np->irqmask);
4689                 }
4690         }
4691 }
4692
4693 static void nv_get_strings(struct net_device *dev, u32 stringset, u8 *buffer)
4694 {
4695         switch (stringset) {
4696         case ETH_SS_STATS:
4697                 memcpy(buffer, &nv_estats_str, nv_get_sset_count(dev, ETH_SS_STATS)*sizeof(struct nv_ethtool_str));
4698                 break;
4699         case ETH_SS_TEST:
4700                 memcpy(buffer, &nv_etests_str, nv_get_sset_count(dev, ETH_SS_TEST)*sizeof(struct nv_ethtool_str));
4701                 break;
4702         }
4703 }
4704
4705 static const struct ethtool_ops ops = {
4706         .get_drvinfo = nv_get_drvinfo,
4707         .get_link = ethtool_op_get_link,
4708         .get_wol = nv_get_wol,
4709         .set_wol = nv_set_wol,
4710         .get_settings = nv_get_settings,
4711         .set_settings = nv_set_settings,
4712         .get_regs_len = nv_get_regs_len,
4713         .get_regs = nv_get_regs,
4714         .nway_reset = nv_nway_reset,
4715         .set_tso = nv_set_tso,
4716         .get_ringparam = nv_get_ringparam,
4717         .set_ringparam = nv_set_ringparam,
4718         .get_pauseparam = nv_get_pauseparam,
4719         .set_pauseparam = nv_set_pauseparam,
4720         .get_rx_csum = nv_get_rx_csum,
4721         .set_rx_csum = nv_set_rx_csum,
4722         .set_tx_csum = nv_set_tx_csum,
4723         .set_sg = nv_set_sg,
4724         .get_strings = nv_get_strings,
4725         .get_ethtool_stats = nv_get_ethtool_stats,
4726         .get_sset_count = nv_get_sset_count,
4727         .self_test = nv_self_test,
4728 };
4729
4730 static void nv_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
4731 {
4732         struct fe_priv *np = get_nvpriv(dev);
4733
4734         spin_lock_irq(&np->lock);
4735
4736         /* save vlan group */
4737         np->vlangrp = grp;
4738
4739         if (grp) {
4740                 /* enable vlan on MAC */
4741                 np->txrxctl_bits |= NVREG_TXRXCTL_VLANSTRIP | NVREG_TXRXCTL_VLANINS;
4742         } else {
4743                 /* disable vlan on MAC */
4744                 np->txrxctl_bits &= ~NVREG_TXRXCTL_VLANSTRIP;
4745                 np->txrxctl_bits &= ~NVREG_TXRXCTL_VLANINS;
4746         }
4747
4748         writel(np->txrxctl_bits, get_hwbase(dev) + NvRegTxRxControl);
4749
4750         spin_unlock_irq(&np->lock);
4751 }
4752
4753 /* The mgmt unit and driver use a semaphore to access the phy during init */
4754 static int nv_mgmt_acquire_sema(struct net_device *dev)
4755 {
4756         u8 __iomem *base = get_hwbase(dev);
4757         int i;
4758         u32 tx_ctrl, mgmt_sema;
4759
4760         for (i = 0; i < 10; i++) {
4761                 mgmt_sema = readl(base + NvRegTransmitterControl) & NVREG_XMITCTL_MGMT_SEMA_MASK;
4762                 if (mgmt_sema == NVREG_XMITCTL_MGMT_SEMA_FREE)
4763                         break;
4764                 msleep(500);
4765         }
4766
4767         if (mgmt_sema != NVREG_XMITCTL_MGMT_SEMA_FREE)
4768                 return 0;
4769
4770         for (i = 0; i < 2; i++) {
4771                 tx_ctrl = readl(base + NvRegTransmitterControl);
4772                 tx_ctrl |= NVREG_XMITCTL_HOST_SEMA_ACQ;
4773                 writel(tx_ctrl, base + NvRegTransmitterControl);
4774
4775                 /* verify that semaphore was acquired */
4776                 tx_ctrl = readl(base + NvRegTransmitterControl);
4777                 if (((tx_ctrl & NVREG_XMITCTL_HOST_SEMA_MASK) == NVREG_XMITCTL_HOST_SEMA_ACQ) &&
4778                     ((tx_ctrl & NVREG_XMITCTL_MGMT_SEMA_MASK) == NVREG_XMITCTL_MGMT_SEMA_FREE))
4779                         return 1;
4780                 else
4781                         udelay(50);
4782         }
4783
4784         return 0;
4785 }
4786
4787 static int nv_open(struct net_device *dev)
4788 {
4789         struct fe_priv *np = netdev_priv(dev);
4790         u8 __iomem *base = get_hwbase(dev);
4791         int ret = 1;
4792         int oom, i;
4793
4794         dprintk(KERN_DEBUG "nv_open: begin\n");
4795
4796         /* erase previous misconfiguration */
4797         if (np->driver_data & DEV_HAS_POWER_CNTRL)
4798                 nv_mac_reset(dev);
4799         writel(NVREG_MCASTADDRA_FORCE, base + NvRegMulticastAddrA);
4800         writel(0, base + NvRegMulticastAddrB);
4801         writel(0, base + NvRegMulticastMaskA);
4802         writel(0, base + NvRegMulticastMaskB);
4803         writel(0, base + NvRegPacketFilterFlags);
4804
4805         writel(0, base + NvRegTransmitterControl);
4806         writel(0, base + NvRegReceiverControl);
4807
4808         writel(0, base + NvRegAdapterControl);
4809
4810         if (np->pause_flags & NV_PAUSEFRAME_TX_CAPABLE)
4811                 writel(NVREG_TX_PAUSEFRAME_DISABLE,  base + NvRegTxPauseFrame);
4812
4813         /* initialize descriptor rings */
4814         set_bufsize(dev);
4815         oom = nv_init_ring(dev);
4816
4817         writel(0, base + NvRegLinkSpeed);
4818         writel(readl(base + NvRegTransmitPoll) & NVREG_TRANSMITPOLL_MAC_ADDR_REV, base + NvRegTransmitPoll);
4819         nv_txrx_reset(dev);
4820         writel(0, base + NvRegUnknownSetupReg6);
4821
4822         np->in_shutdown = 0;
4823
4824         /* give hw rings */
4825         setup_hw_rings(dev, NV_SETUP_RX_RING | NV_SETUP_TX_RING);
4826         writel( ((np->rx_ring_size-1) << NVREG_RINGSZ_RXSHIFT) + ((np->tx_ring_size-1) << NVREG_RINGSZ_TXSHIFT),
4827                 base + NvRegRingSizes);
4828
4829         writel(np->linkspeed, base + NvRegLinkSpeed);
4830         if (np->desc_ver == DESC_VER_1)
4831                 writel(NVREG_TX_WM_DESC1_DEFAULT, base + NvRegTxWatermark);
4832         else
4833                 writel(NVREG_TX_WM_DESC2_3_DEFAULT, base + NvRegTxWatermark);
4834         writel(np->txrxctl_bits, base + NvRegTxRxControl);
4835         writel(np->vlanctl_bits, base + NvRegVlanControl);
4836         pci_push(base);
4837         writel(NVREG_TXRXCTL_BIT1|np->txrxctl_bits, base + NvRegTxRxControl);
4838         reg_delay(dev, NvRegUnknownSetupReg5, NVREG_UNKSETUP5_BIT31, NVREG_UNKSETUP5_BIT31,
4839                         NV_SETUP5_DELAY, NV_SETUP5_DELAYMAX,
4840                         KERN_INFO "open: SetupReg5, Bit 31 remained off\n");
4841
4842         writel(0, base + NvRegMIIMask);
4843         writel(NVREG_IRQSTAT_MASK, base + NvRegIrqStatus);
4844         writel(NVREG_MIISTAT_MASK2, base + NvRegMIIStatus);
4845
4846         writel(NVREG_MISC1_FORCE | NVREG_MISC1_HD, base + NvRegMisc1);
4847         writel(readl(base + NvRegTransmitterStatus), base + NvRegTransmitterStatus);
4848         writel(NVREG_PFF_ALWAYS, base + NvRegPacketFilterFlags);
4849         writel(np->rx_buf_sz, base + NvRegOffloadConfig);
4850
4851         writel(readl(base + NvRegReceiverStatus), base + NvRegReceiverStatus);
4852         get_random_bytes(&i, sizeof(i));
4853         writel(NVREG_RNDSEED_FORCE | (i&NVREG_RNDSEED_MASK), base + NvRegRandomSeed);
4854         writel(NVREG_TX_DEFERRAL_DEFAULT, base + NvRegTxDeferral);
4855         writel(NVREG_RX_DEFERRAL_DEFAULT, base + NvRegRxDeferral);
4856         if (poll_interval == -1) {
4857                 if (optimization_mode == NV_OPTIMIZATION_MODE_THROUGHPUT)
4858                         writel(NVREG_POLL_DEFAULT_THROUGHPUT, base + NvRegPollingInterval);
4859                 else
4860                         writel(NVREG_POLL_DEFAULT_CPU, base + NvRegPollingInterval);
4861         }
4862         else
4863                 writel(poll_interval & 0xFFFF, base + NvRegPollingInterval);
4864         writel(NVREG_UNKSETUP6_VAL, base + NvRegUnknownSetupReg6);
4865         writel((np->phyaddr << NVREG_ADAPTCTL_PHYSHIFT)|NVREG_ADAPTCTL_PHYVALID|NVREG_ADAPTCTL_RUNNING,
4866                         base + NvRegAdapterControl);
4867         writel(NVREG_MIISPEED_BIT8|NVREG_MIIDELAY, base + NvRegMIISpeed);
4868         writel(NVREG_MII_LINKCHANGE, base + NvRegMIIMask);
4869         if (np->wolenabled)
4870                 writel(NVREG_WAKEUPFLAGS_ENABLE , base + NvRegWakeUpFlags);
4871
4872         i = readl(base + NvRegPowerState);
4873         if ( (i & NVREG_POWERSTATE_POWEREDUP) == 0)
4874                 writel(NVREG_POWERSTATE_POWEREDUP|i, base + NvRegPowerState);
4875
4876         pci_push(base);
4877         udelay(10);
4878         writel(readl(base + NvRegPowerState) | NVREG_POWERSTATE_VALID, base + NvRegPowerState);
4879
4880         nv_disable_hw_interrupts(dev, np->irqmask);
4881         pci_push(base);
4882         writel(NVREG_MIISTAT_MASK2, base + NvRegMIIStatus);
4883         writel(NVREG_IRQSTAT_MASK, base + NvRegIrqStatus);
4884         pci_push(base);
4885
4886         if (nv_request_irq(dev, 0)) {
4887                 goto out_drain;
4888         }
4889
4890         /* ask for interrupts */
4891         nv_enable_hw_interrupts(dev, np->irqmask);
4892
4893         spin_lock_irq(&np->lock);
4894         writel(NVREG_MCASTADDRA_FORCE, base + NvRegMulticastAddrA);
4895         writel(0, base + NvRegMulticastAddrB);
4896         writel(0, base + NvRegMulticastMaskA);
4897         writel(0, base + NvRegMulticastMaskB);
4898         writel(NVREG_PFF_ALWAYS|NVREG_PFF_MYADDR, base + NvRegPacketFilterFlags);
4899         /* One manual link speed update: Interrupts are enabled, future link
4900          * speed changes cause interrupts and are handled by nv_link_irq().
4901          */
4902         {
4903                 u32 miistat;
4904                 miistat = readl(base + NvRegMIIStatus);
4905                 writel(NVREG_MIISTAT_MASK, base + NvRegMIIStatus);
4906                 dprintk(KERN_INFO "startup: got 0x%08x.\n", miistat);
4907         }
4908         /* set linkspeed to invalid value, thus force nv_update_linkspeed
4909          * to init hw */
4910         np->linkspeed = 0;
4911         ret = nv_update_linkspeed(dev);
4912         nv_start_rx(dev);
4913         nv_start_tx(dev);
4914         netif_start_queue(dev);
4915 #ifdef CONFIG_FORCEDETH_NAPI
4916         napi_enable(&np->napi);
4917 #endif
4918
4919         if (ret) {
4920                 netif_carrier_on(dev);
4921         } else {
4922                 printk(KERN_INFO "%s: no link during initialization.\n", dev->name);
4923                 netif_carrier_off(dev);
4924         }
4925         if (oom)
4926                 mod_timer(&np->oom_kick, jiffies + OOM_REFILL);
4927
4928         /* start statistics timer */
4929         if (np->driver_data & (DEV_HAS_STATISTICS_V1|DEV_HAS_STATISTICS_V2))
4930                 mod_timer(&np->stats_poll, jiffies + STATS_INTERVAL);
4931
4932         spin_unlock_irq(&np->lock);
4933
4934         return 0;
4935 out_drain:
4936         drain_ring(dev);
4937         return ret;
4938 }
4939
4940 static int nv_close(struct net_device *dev)
4941 {
4942         struct fe_priv *np = netdev_priv(dev);
4943         u8 __iomem *base;
4944
4945         spin_lock_irq(&np->lock);
4946         np->in_shutdown = 1;
4947         spin_unlock_irq(&np->lock);
4948 #ifdef CONFIG_FORCEDETH_NAPI
4949         napi_disable(&np->napi);
4950 #endif
4951         synchronize_irq(dev->irq);
4952
4953         del_timer_sync(&np->oom_kick);
4954         del_timer_sync(&np->nic_poll);
4955         del_timer_sync(&np->stats_poll);
4956
4957         netif_stop_queue(dev);
4958         spin_lock_irq(&np->lock);
4959         nv_stop_tx(dev);
4960         nv_stop_rx(dev);
4961         nv_txrx_reset(dev);
4962
4963         /* disable interrupts on the nic or we will lock up */
4964         base = get_hwbase(dev);
4965         nv_disable_hw_interrupts(dev, np->irqmask);
4966         pci_push(base);
4967         dprintk(KERN_INFO "%s: Irqmask is zero again\n", dev->name);
4968
4969         spin_unlock_irq(&np->lock);
4970
4971         nv_free_irq(dev);
4972
4973         drain_ring(dev);
4974
4975         if (np->wolenabled) {
4976                 writel(NVREG_PFF_ALWAYS|NVREG_PFF_MYADDR, base + NvRegPacketFilterFlags);
4977                 nv_start_rx(dev);
4978         }
4979
4980         /* FIXME: power down nic */
4981
4982         return 0;
4983 }
4984
4985 static int __devinit nv_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
4986 {
4987         struct net_device *dev;
4988         struct fe_priv *np;
4989         unsigned long addr;
4990         u8 __iomem *base;
4991         int err, i;
4992         u32 powerstate, txreg;
4993         u32 phystate_orig = 0, phystate;
4994         int phyinitialized = 0;
4995         DECLARE_MAC_BUF(mac);
4996         static int printed_version;
4997
4998         if (!printed_version++)
4999                 printk(KERN_INFO "%s: Reverse Engineered nForce ethernet"
5000                        " driver. Version %s.\n", DRV_NAME, FORCEDETH_VERSION);
5001
5002         dev = alloc_etherdev(sizeof(struct fe_priv));
5003         err = -ENOMEM;
5004         if (!dev)
5005                 goto out;
5006
5007         np = netdev_priv(dev);
5008         np->dev = dev;
5009         np->pci_dev = pci_dev;
5010         spin_lock_init(&np->lock);
5011         SET_NETDEV_DEV(dev, &pci_dev->dev);
5012
5013         init_timer(&np->oom_kick);
5014         np->oom_kick.data = (unsigned long) dev;
5015         np->oom_kick.function = &nv_do_rx_refill;       /* timer handler */
5016         init_timer(&np->nic_poll);
5017         np->nic_poll.data = (unsigned long) dev;
5018         np->nic_poll.function = &nv_do_nic_poll;        /* timer handler */
5019         init_timer(&np->stats_poll);
5020         np->stats_poll.data = (unsigned long) dev;
5021         np->stats_poll.function = &nv_do_stats_poll;    /* timer handler */
5022
5023         err = pci_enable_device(pci_dev);
5024         if (err)
5025                 goto out_free;
5026
5027         pci_set_master(pci_dev);
5028
5029         err = pci_request_regions(pci_dev, DRV_NAME);
5030         if (err < 0)
5031                 goto out_disable;
5032
5033         if (id->driver_data & (DEV_HAS_VLAN|DEV_HAS_MSI_X|DEV_HAS_POWER_CNTRL|DEV_HAS_STATISTICS_V2))
5034                 np->register_size = NV_PCI_REGSZ_VER3;
5035         else if (id->driver_data & DEV_HAS_STATISTICS_V1)
5036                 np->register_size = NV_PCI_REGSZ_VER2;
5037         else
5038                 np->register_size = NV_PCI_REGSZ_VER1;
5039
5040         err = -EINVAL;
5041         addr = 0;
5042         for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
5043                 dprintk(KERN_DEBUG "%s: resource %d start %p len %ld flags 0x%08lx.\n",
5044                                 pci_name(pci_dev), i, (void*)pci_resource_start(pci_dev, i),
5045                                 pci_resource_len(pci_dev, i),
5046                                 pci_resource_flags(pci_dev, i));
5047                 if (pci_resource_flags(pci_dev, i) & IORESOURCE_MEM &&
5048                                 pci_resource_len(pci_dev, i) >= np->register_size) {
5049                         addr = pci_resource_start(pci_dev, i);
5050                         break;
5051                 }
5052         }
5053         if (i == DEVICE_COUNT_RESOURCE) {
5054                 dev_printk(KERN_INFO, &pci_dev->dev,
5055                            "Couldn't find register window\n");
5056                 goto out_relreg;
5057         }
5058
5059         /* copy of driver data */
5060         np->driver_data = id->driver_data;
5061
5062         /* handle different descriptor versions */
5063         if (id->driver_data & DEV_HAS_HIGH_DMA) {
5064                 /* packet format 3: supports 40-bit addressing */
5065                 np->desc_ver = DESC_VER_3;
5066                 np->txrxctl_bits = NVREG_TXRXCTL_DESC_3;
5067                 if (dma_64bit) {
5068                         if (pci_set_dma_mask(pci_dev, DMA_39BIT_MASK))
5069                                 dev_printk(KERN_INFO, &pci_dev->dev,
5070                                         "64-bit DMA failed, using 32-bit addressing\n");
5071                         else
5072                                 dev->features |= NETIF_F_HIGHDMA;
5073                         if (pci_set_consistent_dma_mask(pci_dev, DMA_39BIT_MASK)) {
5074                                 dev_printk(KERN_INFO, &pci_dev->dev,
5075                                         "64-bit DMA (consistent) failed, using 32-bit ring buffers\n");
5076                         }
5077                 }
5078         } else if (id->driver_data & DEV_HAS_LARGEDESC) {
5079                 /* packet format 2: supports jumbo frames */
5080                 np->desc_ver = DESC_VER_2;
5081                 np->txrxctl_bits = NVREG_TXRXCTL_DESC_2;
5082         } else {
5083                 /* original packet format */
5084                 np->desc_ver = DESC_VER_1;
5085                 np->txrxctl_bits = NVREG_TXRXCTL_DESC_1;
5086         }
5087
5088         np->pkt_limit = NV_PKTLIMIT_1;
5089         if (id->driver_data & DEV_HAS_LARGEDESC)
5090                 np->pkt_limit = NV_PKTLIMIT_2;
5091
5092         if (id->driver_data & DEV_HAS_CHECKSUM) {
5093                 np->rx_csum = 1;
5094                 np->txrxctl_bits |= NVREG_TXRXCTL_RXCHECK;
5095                 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
5096                 dev->features |= NETIF_F_TSO;
5097         }
5098
5099         np->vlanctl_bits = 0;
5100         if (id->driver_data & DEV_HAS_VLAN) {
5101                 np->vlanctl_bits = NVREG_VLANCONTROL_ENABLE;
5102                 dev->features |= NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX;
5103                 dev->vlan_rx_register = nv_vlan_rx_register;
5104         }
5105
5106         np->msi_flags = 0;
5107         if ((id->driver_data & DEV_HAS_MSI) && msi) {
5108                 np->msi_flags |= NV_MSI_CAPABLE;
5109         }
5110         if ((id->driver_data & DEV_HAS_MSI_X) && msix) {
5111                 np->msi_flags |= NV_MSI_X_CAPABLE;
5112         }
5113
5114         np->pause_flags = NV_PAUSEFRAME_RX_CAPABLE | NV_PAUSEFRAME_RX_REQ | NV_PAUSEFRAME_AUTONEG;
5115         if (id->driver_data & DEV_HAS_PAUSEFRAME_TX) {
5116                 np->pause_flags |= NV_PAUSEFRAME_TX_CAPABLE | NV_PAUSEFRAME_TX_REQ;
5117         }
5118
5119
5120         err = -ENOMEM;
5121         np->base = ioremap(addr, np->register_size);
5122         if (!np->base)
5123                 goto out_relreg;
5124         dev->base_addr = (unsigned long)np->base;
5125
5126         dev->irq = pci_dev->irq;
5127
5128         np->rx_ring_size = RX_RING_DEFAULT;
5129         np->tx_ring_size = TX_RING_DEFAULT;
5130
5131         if (np->desc_ver == DESC_VER_1 || np->desc_ver == DESC_VER_2) {
5132                 np->rx_ring.orig = pci_alloc_consistent(pci_dev,
5133                                         sizeof(struct ring_desc) * (np->rx_ring_size + np->tx_ring_size),
5134                                         &np->ring_addr);
5135                 if (!np->rx_ring.orig)
5136                         goto out_unmap;
5137                 np->tx_ring.orig = &np->rx_ring.orig[np->rx_ring_size];
5138         } else {
5139                 np->rx_ring.ex = pci_alloc_consistent(pci_dev,
5140                                         sizeof(struct ring_desc_ex) * (np->rx_ring_size + np->tx_ring_size),
5141                                         &np->ring_addr);
5142                 if (!np->rx_ring.ex)
5143                         goto out_unmap;
5144                 np->tx_ring.ex = &np->rx_ring.ex[np->rx_ring_size];
5145         }
5146         np->rx_skb = kcalloc(np->rx_ring_size, sizeof(struct nv_skb_map), GFP_KERNEL);
5147         np->tx_skb = kcalloc(np->tx_ring_size, sizeof(struct nv_skb_map), GFP_KERNEL);
5148         if (!np->rx_skb || !np->tx_skb)
5149                 goto out_freering;
5150
5151         dev->open = nv_open;
5152         dev->stop = nv_close;
5153         if (np->desc_ver == DESC_VER_1 || np->desc_ver == DESC_VER_2)
5154                 dev->hard_start_xmit = nv_start_xmit;
5155         else
5156                 dev->hard_start_xmit = nv_start_xmit_optimized;
5157         dev->get_stats = nv_get_stats;
5158         dev->change_mtu = nv_change_mtu;
5159         dev->set_mac_address = nv_set_mac_address;
5160         dev->set_multicast_list = nv_set_multicast;
5161 #ifdef CONFIG_NET_POLL_CONTROLLER
5162         dev->poll_controller = nv_poll_controller;
5163 #endif
5164 #ifdef CONFIG_FORCEDETH_NAPI
5165         netif_napi_add(dev, &np->napi, nv_napi_poll, RX_WORK_PER_LOOP);
5166 #endif
5167         SET_ETHTOOL_OPS(dev, &ops);
5168         dev->tx_timeout = nv_tx_timeout;
5169         dev->watchdog_timeo = NV_WATCHDOG_TIMEO;
5170
5171         pci_set_drvdata(pci_dev, dev);
5172
5173         /* read the mac address */
5174         base = get_hwbase(dev);
5175         np->orig_mac[0] = readl(base + NvRegMacAddrA);
5176         np->orig_mac[1] = readl(base + NvRegMacAddrB);
5177
5178         /* check the workaround bit for correct mac address order */
5179         txreg = readl(base + NvRegTransmitPoll);
5180         if ((txreg & NVREG_TRANSMITPOLL_MAC_ADDR_REV) ||
5181             (id->driver_data & DEV_HAS_CORRECT_MACADDR)) {
5182                 /* mac address is already in correct order */
5183                 dev->dev_addr[0] = (np->orig_mac[0] >>  0) & 0xff;
5184                 dev->dev_addr[1] = (np->orig_mac[0] >>  8) & 0xff;
5185                 dev->dev_addr[2] = (np->orig_mac[0] >> 16) & 0xff;
5186                 dev->dev_addr[3] = (np->orig_mac[0] >> 24) & 0xff;
5187                 dev->dev_addr[4] = (np->orig_mac[1] >>  0) & 0xff;
5188                 dev->dev_addr[5] = (np->orig_mac[1] >>  8) & 0xff;
5189         } else {
5190                 /* need to reverse mac address to correct order */
5191                 dev->dev_addr[0] = (np->orig_mac[1] >>  8) & 0xff;
5192                 dev->dev_addr[1] = (np->orig_mac[1] >>  0) & 0xff;
5193                 dev->dev_addr[2] = (np->orig_mac[0] >> 24) & 0xff;
5194                 dev->dev_addr[3] = (np->orig_mac[0] >> 16) & 0xff;
5195                 dev->dev_addr[4] = (np->orig_mac[0] >>  8) & 0xff;
5196                 dev->dev_addr[5] = (np->orig_mac[0] >>  0) & 0xff;
5197                 /* set permanent address to be correct aswell */
5198                 np->orig_mac[0] = (dev->dev_addr[0] << 0) + (dev->dev_addr[1] << 8) +
5199                         (dev->dev_addr[2] << 16) + (dev->dev_addr[3] << 24);
5200                 np->orig_mac[1] = (dev->dev_addr[4] << 0) + (dev->dev_addr[5] << 8);
5201                 writel(txreg|NVREG_TRANSMITPOLL_MAC_ADDR_REV, base + NvRegTransmitPoll);
5202         }
5203         memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
5204
5205         if (!is_valid_ether_addr(dev->perm_addr)) {
5206                 /*
5207                  * Bad mac address. At least one bios sets the mac address
5208                  * to 01:23:45:67:89:ab
5209                  */
5210                 dev_printk(KERN_ERR, &pci_dev->dev,
5211                         "Invalid Mac address detected: %s\n",
5212                         print_mac(mac, dev->dev_addr));
5213                 dev_printk(KERN_ERR, &pci_dev->dev,
5214                         "Please complain to your hardware vendor. Switching to a random MAC.\n");
5215                 dev->dev_addr[0] = 0x00;
5216                 dev->dev_addr[1] = 0x00;
5217                 dev->dev_addr[2] = 0x6c;
5218                 get_random_bytes(&dev->dev_addr[3], 3);
5219         }
5220
5221         dprintk(KERN_DEBUG "%s: MAC Address %s\n",
5222                 pci_name(pci_dev), print_mac(mac, dev->dev_addr));
5223
5224         /* set mac address */
5225         nv_copy_mac_to_hw(dev);
5226
5227         /* disable WOL */
5228         writel(0, base + NvRegWakeUpFlags);
5229         np->wolenabled = 0;
5230
5231         if (id->driver_data & DEV_HAS_POWER_CNTRL) {
5232
5233                 /* take phy and nic out of low power mode */
5234                 powerstate = readl(base + NvRegPowerState2);
5235                 powerstate &= ~NVREG_POWERSTATE2_POWERUP_MASK;
5236                 if ((id->device == PCI_DEVICE_ID_NVIDIA_NVENET_12 ||
5237                      id->device == PCI_DEVICE_ID_NVIDIA_NVENET_13) &&
5238                     pci_dev->revision >= 0xA3)
5239                         powerstate |= NVREG_POWERSTATE2_POWERUP_REV_A3;
5240                 writel(powerstate, base + NvRegPowerState2);
5241         }
5242
5243         if (np->desc_ver == DESC_VER_1) {
5244                 np->tx_flags = NV_TX_VALID;
5245         } else {
5246                 np->tx_flags = NV_TX2_VALID;
5247         }
5248         if (optimization_mode == NV_OPTIMIZATION_MODE_THROUGHPUT) {
5249                 np->irqmask = NVREG_IRQMASK_THROUGHPUT;
5250                 if (np->msi_flags & NV_MSI_X_CAPABLE) /* set number of vectors */
5251                         np->msi_flags |= 0x0003;
5252         } else {
5253                 np->irqmask = NVREG_IRQMASK_CPU;
5254                 if (np->msi_flags & NV_MSI_X_CAPABLE) /* set number of vectors */
5255                         np->msi_flags |= 0x0001;
5256         }
5257
5258         if (id->driver_data & DEV_NEED_TIMERIRQ)
5259                 np->irqmask |= NVREG_IRQ_TIMER;
5260         if (id->driver_data & DEV_NEED_LINKTIMER) {
5261                 dprintk(KERN_INFO "%s: link timer on.\n", pci_name(pci_dev));
5262                 np->need_linktimer = 1;
5263                 np->link_timeout = jiffies + LINK_TIMEOUT;
5264         } else {
5265                 dprintk(KERN_INFO "%s: link timer off.\n", pci_name(pci_dev));
5266                 np->need_linktimer = 0;
5267         }
5268
5269         /* clear phy state and temporarily halt phy interrupts */
5270         writel(0, base + NvRegMIIMask);
5271         phystate = readl(base + NvRegAdapterControl);
5272         if (phystate & NVREG_ADAPTCTL_RUNNING) {
5273                 phystate_orig = 1;
5274                 phystate &= ~NVREG_ADAPTCTL_RUNNING;
5275                 writel(phystate, base + NvRegAdapterControl);
5276         }
5277         writel(NVREG_MIISTAT_MASK, base + NvRegMIIStatus);
5278
5279         if (id->driver_data & DEV_HAS_MGMT_UNIT) {
5280                 /* management unit running on the mac? */
5281                 if (readl(base + NvRegTransmitterControl) & NVREG_XMITCTL_SYNC_PHY_INIT) {
5282                         np->mac_in_use = readl(base + NvRegTransmitterControl) & NVREG_XMITCTL_MGMT_ST;
5283                         dprintk(KERN_INFO "%s: mgmt unit is running. mac in use %x.\n", pci_name(pci_dev), np->mac_in_use);
5284                         for (i = 0; i < 5000; i++) {
5285                                 msleep(1);
5286                                 if (nv_mgmt_acquire_sema(dev)) {
5287                                         /* management unit setup the phy already? */
5288                                         if ((readl(base + NvRegTransmitterControl) & NVREG_XMITCTL_SYNC_MASK) ==
5289                                             NVREG_XMITCTL_SYNC_PHY_INIT) {
5290                                                 /* phy is inited by mgmt unit */
5291                                                 phyinitialized = 1;
5292                                                 dprintk(KERN_INFO "%s: Phy already initialized by mgmt unit.\n", pci_name(pci_dev));
5293                                         } else {
5294                                                 /* we need to init the phy */
5295                                         }
5296                                         break;
5297                                 }
5298                         }
5299                 }
5300         }
5301
5302         /* find a suitable phy */
5303         for (i = 1; i <= 32; i++) {
5304                 int id1, id2;
5305                 int phyaddr = i & 0x1F;
5306
5307                 spin_lock_irq(&np->lock);
5308                 id1 = mii_rw(dev, phyaddr, MII_PHYSID1, MII_READ);
5309                 spin_unlock_irq(&np->lock);
5310                 if (id1 < 0 || id1 == 0xffff)
5311                         continue;
5312                 spin_lock_irq(&np->lock);
5313                 id2 = mii_rw(dev, phyaddr, MII_PHYSID2, MII_READ);
5314                 spin_unlock_irq(&np->lock);
5315                 if (id2 < 0 || id2 == 0xffff)
5316                         continue;
5317
5318                 np->phy_model = id2 & PHYID2_MODEL_MASK;
5319                 id1 = (id1 & PHYID1_OUI_MASK) << PHYID1_OUI_SHFT;
5320                 id2 = (id2 & PHYID2_OUI_MASK) >> PHYID2_OUI_SHFT;
5321                 dprintk(KERN_DEBUG "%s: open: Found PHY %04x:%04x at address %d.\n",
5322                         pci_name(pci_dev), id1, id2, phyaddr);
5323                 np->phyaddr = phyaddr;
5324                 np->phy_oui = id1 | id2;
5325                 break;
5326         }
5327         if (i == 33) {
5328                 dev_printk(KERN_INFO, &pci_dev->dev,
5329                         "open: Could not find a valid PHY.\n");
5330                 goto out_error;
5331         }
5332
5333         if (!phyinitialized) {
5334                 /* reset it */
5335                 phy_init(dev);
5336         } else {
5337                 /* see if it is a gigabit phy */
5338                 u32 mii_status = mii_rw(dev, np->phyaddr, MII_BMSR, MII_READ);
5339                 if (mii_status & PHY_GIGABIT) {
5340                         np->gigabit = PHY_GIGABIT;
5341                 }
5342         }
5343
5344         /* set default link speed settings */
5345         np->linkspeed = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_10;
5346         np->duplex = 0;
5347         np->autoneg = 1;
5348
5349         err = register_netdev(dev);
5350         if (err) {
5351                 dev_printk(KERN_INFO, &pci_dev->dev,
5352                            "unable to register netdev: %d\n", err);
5353                 goto out_error;
5354         }
5355
5356         dev_printk(KERN_INFO, &pci_dev->dev, "ifname %s, PHY OUI 0x%x @ %d, "
5357                    "addr %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
5358                    dev->name,
5359                    np->phy_oui,
5360                    np->phyaddr,
5361                    dev->dev_addr[0],
5362                    dev->dev_addr[1],
5363                    dev->dev_addr[2],
5364                    dev->dev_addr[3],
5365                    dev->dev_addr[4],
5366                    dev->dev_addr[5]);
5367
5368         dev_printk(KERN_INFO, &pci_dev->dev, "%s%s%s%s%s%s%s%s%s%sdesc-v%u\n",
5369                    dev->features & NETIF_F_HIGHDMA ? "highdma " : "",
5370                    dev->features & (NETIF_F_HW_CSUM | NETIF_F_SG) ?
5371                         "csum " : "",
5372                    dev->features & (NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX) ?
5373                         "vlan " : "",
5374                    id->driver_data & DEV_HAS_POWER_CNTRL ? "pwrctl " : "",
5375                    id->driver_data & DEV_HAS_MGMT_UNIT ? "mgmt " : "",
5376                    id->driver_data & DEV_NEED_TIMERIRQ ? "timirq " : "",
5377                    np->gigabit == PHY_GIGABIT ? "gbit " : "",
5378                    np->need_linktimer ? "lnktim " : "",
5379                    np->msi_flags & NV_MSI_CAPABLE ? "msi " : "",
5380                    np->msi_flags & NV_MSI_X_CAPABLE ? "msi-x " : "",
5381                    np->desc_ver);
5382
5383         return 0;
5384
5385 out_error:
5386         if (phystate_orig)
5387                 writel(phystate|NVREG_ADAPTCTL_RUNNING, base + NvRegAdapterControl);
5388         pci_set_drvdata(pci_dev, NULL);
5389 out_freering:
5390         free_rings(dev);
5391 out_unmap:
5392         iounmap(get_hwbase(dev));
5393 out_relreg:
5394         pci_release_regions(pci_dev);
5395 out_disable:
5396         pci_disable_device(pci_dev);
5397 out_free:
5398         free_netdev(dev);
5399 out:
5400         return err;
5401 }
5402
5403 static void __devexit nv_remove(struct pci_dev *pci_dev)
5404 {
5405         struct net_device *dev = pci_get_drvdata(pci_dev);
5406         struct fe_priv *np = netdev_priv(dev);
5407         u8 __iomem *base = get_hwbase(dev);
5408
5409         unregister_netdev(dev);
5410
5411         /* special op: write back the misordered MAC address - otherwise
5412          * the next nv_probe would see a wrong address.
5413          */
5414         writel(np->orig_mac[0], base + NvRegMacAddrA);
5415         writel(np->orig_mac[1], base + NvRegMacAddrB);
5416
5417         /* free all structures */
5418         free_rings(dev);
5419         iounmap(get_hwbase(dev));
5420         pci_release_regions(pci_dev);
5421         pci_disable_device(pci_dev);
5422         free_netdev(dev);
5423         pci_set_drvdata(pci_dev, NULL);
5424 }
5425
5426 #ifdef CONFIG_PM
5427 static int nv_suspend(struct pci_dev *pdev, pm_message_t state)
5428 {
5429         struct net_device *dev = pci_get_drvdata(pdev);
5430         struct fe_priv *np = netdev_priv(dev);
5431
5432         if (!netif_running(dev))
5433                 goto out;
5434
5435         netif_device_detach(dev);
5436
5437         // Gross.
5438         nv_close(dev);
5439
5440         pci_save_state(pdev);
5441         pci_enable_wake(pdev, pci_choose_state(pdev, state), np->wolenabled);
5442         pci_set_power_state(pdev, pci_choose_state(pdev, state));
5443 out:
5444         return 0;
5445 }
5446
5447 static int nv_resume(struct pci_dev *pdev)
5448 {
5449         struct net_device *dev = pci_get_drvdata(pdev);
5450         int rc = 0;
5451
5452         if (!netif_running(dev))
5453                 goto out;
5454
5455         netif_device_attach(dev);
5456
5457         pci_set_power_state(pdev, PCI_D0);
5458         pci_restore_state(pdev);
5459         pci_enable_wake(pdev, PCI_D0, 0);
5460
5461         rc = nv_open(dev);
5462 out:
5463         return rc;
5464 }
5465 #else
5466 #define nv_suspend NULL
5467 #define nv_resume NULL
5468 #endif /* CONFIG_PM */
5469
5470 static struct pci_device_id pci_tbl[] = {
5471         {       /* nForce Ethernet Controller */
5472                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_1),
5473                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER,
5474         },
5475         {       /* nForce2 Ethernet Controller */
5476                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_2),
5477                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER,
5478         },
5479         {       /* nForce3 Ethernet Controller */
5480                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_3),
5481                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER,
5482         },
5483         {       /* nForce3 Ethernet Controller */
5484                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_4),
5485                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM,
5486         },
5487         {       /* nForce3 Ethernet Controller */
5488                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_5),
5489                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM,
5490         },
5491         {       /* nForce3 Ethernet Controller */
5492                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_6),
5493                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM,
5494         },
5495         {       /* nForce3 Ethernet Controller */
5496                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_7),
5497                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM,
5498         },
5499         {       /* CK804 Ethernet Controller */
5500                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_8),
5501                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM|DEV_HAS_HIGH_DMA|DEV_HAS_STATISTICS_V1,
5502         },
5503         {       /* CK804 Ethernet Controller */
5504                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_9),
5505                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM|DEV_HAS_HIGH_DMA|DEV_HAS_STATISTICS_V1,
5506         },
5507         {       /* MCP04 Ethernet Controller */
5508                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_10),
5509                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM|DEV_HAS_HIGH_DMA|DEV_HAS_STATISTICS_V1,
5510         },
5511         {       /* MCP04 Ethernet Controller */
5512                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_11),
5513                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM|DEV_HAS_HIGH_DMA|DEV_HAS_STATISTICS_V1,
5514         },
5515         {       /* MCP51 Ethernet Controller */
5516                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_12),
5517                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_STATISTICS_V1,
5518         },
5519         {       /* MCP51 Ethernet Controller */
5520                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_13),
5521                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_STATISTICS_V1,
5522         },
5523         {       /* MCP55 Ethernet Controller */
5524                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_14),
5525                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM|DEV_HAS_HIGH_DMA|DEV_HAS_VLAN|DEV_HAS_MSI|DEV_HAS_MSI_X|DEV_HAS_POWER_CNTRL|DEV_HAS_PAUSEFRAME_TX|DEV_HAS_STATISTICS_V2|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT,
5526         },
5527         {       /* MCP55 Ethernet Controller */
5528                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_15),
5529                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM|DEV_HAS_HIGH_DMA|DEV_HAS_VLAN|DEV_HAS_MSI|DEV_HAS_MSI_X|DEV_HAS_POWER_CNTRL|DEV_HAS_PAUSEFRAME_TX|DEV_HAS_STATISTICS_V2|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT,
5530         },
5531         {       /* MCP61 Ethernet Controller */
5532                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_16),
5533                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX|DEV_HAS_STATISTICS_V2|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR,
5534         },
5535         {       /* MCP61 Ethernet Controller */
5536                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_17),
5537                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX|DEV_HAS_STATISTICS_V2|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR,
5538         },
5539         {       /* MCP61 Ethernet Controller */
5540                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_18),
5541                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX|DEV_HAS_STATISTICS_V2|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR,
5542         },
5543         {       /* MCP61 Ethernet Controller */
5544                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_19),
5545                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX|DEV_HAS_STATISTICS_V2|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR,
5546         },
5547         {       /* MCP65 Ethernet Controller */
5548                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_20),
5549                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX|DEV_HAS_STATISTICS_V2|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR,
5550         },
5551         {       /* MCP65 Ethernet Controller */
5552                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_21),
5553                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX|DEV_HAS_STATISTICS_V2|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR,
5554         },
5555         {       /* MCP65 Ethernet Controller */
5556                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_22),
5557                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX|DEV_HAS_STATISTICS_V2|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR,
5558         },
5559         {       /* MCP65 Ethernet Controller */
5560                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_23),
5561                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX|DEV_HAS_STATISTICS_V2|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR,
5562         },
5563         {       /* MCP67 Ethernet Controller */
5564                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_24),
5565                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX|DEV_HAS_STATISTICS_V2|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR,
5566         },
5567         {       /* MCP67 Ethernet Controller */
5568                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_25),
5569                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX|DEV_HAS_STATISTICS_V2|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR,
5570         },
5571         {       /* MCP67 Ethernet Controller */
5572                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_26),
5573                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX|DEV_HAS_STATISTICS_V2|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR,
5574         },
5575         {       /* MCP67 Ethernet Controller */
5576                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_27),
5577                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX|DEV_HAS_STATISTICS_V2|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR,
5578         },
5579         {       /* MCP73 Ethernet Controller */
5580                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_28),
5581                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX|DEV_HAS_STATISTICS_V2|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR,
5582         },
5583         {       /* MCP73 Ethernet Controller */
5584                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_29),
5585                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX|DEV_HAS_STATISTICS_V2|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR,
5586         },
5587         {       /* MCP73 Ethernet Controller */
5588                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_30),
5589                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX|DEV_HAS_STATISTICS_V2|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR,
5590         },
5591         {       /* MCP73 Ethernet Controller */
5592                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NVENET_31),
5593                 .driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX|DEV_HAS_STATISTICS_V2|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR,
5594         },
5595         {0,},
5596 };
5597
5598 static struct pci_driver driver = {
5599         .name           = DRV_NAME,
5600         .id_table       = pci_tbl,
5601         .probe          = nv_probe,
5602         .remove         = __devexit_p(nv_remove),
5603         .suspend        = nv_suspend,
5604         .resume         = nv_resume,
5605 };
5606
5607 static int __init init_nic(void)
5608 {
5609         return pci_register_driver(&driver);
5610 }
5611
5612 static void __exit exit_nic(void)
5613 {
5614         pci_unregister_driver(&driver);
5615 }
5616
5617 module_param(max_interrupt_work, int, 0);
5618 MODULE_PARM_DESC(max_interrupt_work, "forcedeth maximum events handled per interrupt");
5619 module_param(optimization_mode, int, 0);
5620 MODULE_PARM_DESC(optimization_mode, "In throughput mode (0), every tx & rx packet will generate an interrupt. In CPU mode (1), interrupts are controlled by a timer.");
5621 module_param(poll_interval, int, 0);
5622 MODULE_PARM_DESC(poll_interval, "Interval determines how frequent timer interrupt is generated by [(time_in_micro_secs * 100) / (2^10)]. Min is 0 and Max is 65535.");
5623 module_param(msi, int, 0);
5624 MODULE_PARM_DESC(msi, "MSI interrupts are enabled by setting to 1 and disabled by setting to 0.");
5625 module_param(msix, int, 0);
5626 MODULE_PARM_DESC(msix, "MSIX interrupts are enabled by setting to 1 and disabled by setting to 0.");
5627 module_param(dma_64bit, int, 0);
5628 MODULE_PARM_DESC(dma_64bit, "High DMA is enabled by setting to 1 and disabled by setting to 0.");
5629
5630 MODULE_AUTHOR("Manfred Spraul <manfred@colorfullife.com>");
5631 MODULE_DESCRIPTION("Reverse Engineered nForce ethernet driver");
5632 MODULE_LICENSE("GPL");
5633
5634 MODULE_DEVICE_TABLE(pci, pci_tbl);
5635
5636 module_init(init_nic);
5637 module_exit(exit_nic);