Linux 2.6.20.7
[linux/fpc-iii.git] / drivers / net / ns83820.c
blob568daeb3e9d8f56ac0e4b37249c5d2b9507dc66f
1 #define VERSION "0.22"
2 /* ns83820.c by Benjamin LaHaise with contributions.
4 * Questions/comments/discussion to linux-ns83820@kvack.org.
6 * $Revision: 1.34.2.23 $
8 * Copyright 2001 Benjamin LaHaise.
9 * Copyright 2001, 2002 Red Hat.
11 * Mmmm, chocolate vanilla mocha...
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 * ChangeLog
30 * =========
31 * 20010414 0.1 - created
32 * 20010622 0.2 - basic rx and tx.
33 * 20010711 0.3 - added duplex and link state detection support.
34 * 20010713 0.4 - zero copy, no hangs.
35 * 0.5 - 64 bit dma support (davem will hate me for this)
36 * - disable jumbo frames to avoid tx hangs
37 * - work around tx deadlocks on my 1.02 card via
38 * fiddling with TXCFG
39 * 20010810 0.6 - use pci dma api for ringbuffers, work on ia64
40 * 20010816 0.7 - misc cleanups
41 * 20010826 0.8 - fix critical zero copy bugs
42 * 0.9 - internal experiment
43 * 20010827 0.10 - fix ia64 unaligned access.
44 * 20010906 0.11 - accept all packets with checksum errors as
45 * otherwise fragments get lost
46 * - fix >> 32 bugs
47 * 0.12 - add statistics counters
48 * - add allmulti/promisc support
49 * 20011009 0.13 - hotplug support, other smaller pci api cleanups
50 * 20011204 0.13a - optical transceiver support added
51 * by Michael Clark <michael@metaparadigm.com>
52 * 20011205 0.13b - call register_netdev earlier in initialization
53 * suppress duplicate link status messages
54 * 20011117 0.14 - ethtool GDRVINFO, GLINK support from jgarzik
55 * 20011204 0.15 get ppc (big endian) working
56 * 20011218 0.16 various cleanups
57 * 20020310 0.17 speedups
58 * 20020610 0.18 - actually use the pci dma api for highmem
59 * - remove pci latency register fiddling
60 * 0.19 - better bist support
61 * - add ihr and reset_phy parameters
62 * - gmii bus probing
63 * - fix missed txok introduced during performance
64 * tuning
65 * 0.20 - fix stupid RFEN thinko. i am such a smurf.
66 * 20040828 0.21 - add hardware vlan accleration
67 * by Neil Horman <nhorman@redhat.com>
68 * 20050406 0.22 - improved DAC ifdefs from Andi Kleen
69 * - removal of dead code from Adrian Bunk
70 * - fix half duplex collision behaviour
71 * Driver Overview
72 * ===============
74 * This driver was originally written for the National Semiconductor
75 * 83820 chip, a 10/100/1000 Mbps 64 bit PCI ethernet NIC. Hopefully
76 * this code will turn out to be a) clean, b) correct, and c) fast.
77 * With that in mind, I'm aiming to split the code up as much as
78 * reasonably possible. At present there are X major sections that
79 * break down into a) packet receive, b) packet transmit, c) link
80 * management, d) initialization and configuration. Where possible,
81 * these code paths are designed to run in parallel.
83 * This driver has been tested and found to work with the following
84 * cards (in no particular order):
86 * Cameo SOHO-GA2000T SOHO-GA2500T
87 * D-Link DGE-500T
88 * PureData PDP8023Z-TG
89 * SMC SMC9452TX SMC9462TX
90 * Netgear GA621
92 * Special thanks to SMC for providing hardware to test this driver on.
94 * Reports of success or failure would be greatly appreciated.
96 //#define dprintk printk
97 #define dprintk(x...) do { } while (0)
99 #include <linux/module.h>
100 #include <linux/moduleparam.h>
101 #include <linux/types.h>
102 #include <linux/pci.h>
103 #include <linux/dma-mapping.h>
104 #include <linux/netdevice.h>
105 #include <linux/etherdevice.h>
106 #include <linux/delay.h>
107 #include <linux/smp_lock.h>
108 #include <linux/workqueue.h>
109 #include <linux/init.h>
110 #include <linux/ip.h> /* for iph */
111 #include <linux/in.h> /* for IPPROTO_... */
112 #include <linux/compiler.h>
113 #include <linux/prefetch.h>
114 #include <linux/ethtool.h>
115 #include <linux/timer.h>
116 #include <linux/if_vlan.h>
117 #include <linux/rtnetlink.h>
118 #include <linux/jiffies.h>
120 #include <asm/io.h>
121 #include <asm/uaccess.h>
122 #include <asm/system.h>
124 #define DRV_NAME "ns83820"
126 /* Global parameters. See module_param near the bottom. */
127 static int ihr = 2;
128 static int reset_phy = 0;
129 static int lnksts = 0; /* CFG_LNKSTS bit polarity */
131 /* Dprintk is used for more interesting debug events */
132 #undef Dprintk
133 #define Dprintk dprintk
135 /* tunables */
136 #define RX_BUF_SIZE 1500 /* 8192 */
137 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
138 #define NS83820_VLAN_ACCEL_SUPPORT
139 #endif
141 /* Must not exceed ~65000. */
142 #define NR_RX_DESC 64
143 #define NR_TX_DESC 128
145 /* not tunable */
146 #define REAL_RX_BUF_SIZE (RX_BUF_SIZE + 14) /* rx/tx mac addr + type */
148 #define MIN_TX_DESC_FREE 8
150 /* register defines */
151 #define CFGCS 0x04
153 #define CR_TXE 0x00000001
154 #define CR_TXD 0x00000002
155 /* Ramit : Here's a tip, don't do a RXD immediately followed by an RXE
156 * The Receive engine skips one descriptor and moves
157 * onto the next one!! */
158 #define CR_RXE 0x00000004
159 #define CR_RXD 0x00000008
160 #define CR_TXR 0x00000010
161 #define CR_RXR 0x00000020
162 #define CR_SWI 0x00000080
163 #define CR_RST 0x00000100
165 #define PTSCR_EEBIST_FAIL 0x00000001
166 #define PTSCR_EEBIST_EN 0x00000002
167 #define PTSCR_EELOAD_EN 0x00000004
168 #define PTSCR_RBIST_FAIL 0x000001b8
169 #define PTSCR_RBIST_DONE 0x00000200
170 #define PTSCR_RBIST_EN 0x00000400
171 #define PTSCR_RBIST_RST 0x00002000
173 #define MEAR_EEDI 0x00000001
174 #define MEAR_EEDO 0x00000002
175 #define MEAR_EECLK 0x00000004
176 #define MEAR_EESEL 0x00000008
177 #define MEAR_MDIO 0x00000010
178 #define MEAR_MDDIR 0x00000020
179 #define MEAR_MDC 0x00000040
181 #define ISR_TXDESC3 0x40000000
182 #define ISR_TXDESC2 0x20000000
183 #define ISR_TXDESC1 0x10000000
184 #define ISR_TXDESC0 0x08000000
185 #define ISR_RXDESC3 0x04000000
186 #define ISR_RXDESC2 0x02000000
187 #define ISR_RXDESC1 0x01000000
188 #define ISR_RXDESC0 0x00800000
189 #define ISR_TXRCMP 0x00400000
190 #define ISR_RXRCMP 0x00200000
191 #define ISR_DPERR 0x00100000
192 #define ISR_SSERR 0x00080000
193 #define ISR_RMABT 0x00040000
194 #define ISR_RTABT 0x00020000
195 #define ISR_RXSOVR 0x00010000
196 #define ISR_HIBINT 0x00008000
197 #define ISR_PHY 0x00004000
198 #define ISR_PME 0x00002000
199 #define ISR_SWI 0x00001000
200 #define ISR_MIB 0x00000800
201 #define ISR_TXURN 0x00000400
202 #define ISR_TXIDLE 0x00000200
203 #define ISR_TXERR 0x00000100
204 #define ISR_TXDESC 0x00000080
205 #define ISR_TXOK 0x00000040
206 #define ISR_RXORN 0x00000020
207 #define ISR_RXIDLE 0x00000010
208 #define ISR_RXEARLY 0x00000008
209 #define ISR_RXERR 0x00000004
210 #define ISR_RXDESC 0x00000002
211 #define ISR_RXOK 0x00000001
213 #define TXCFG_CSI 0x80000000
214 #define TXCFG_HBI 0x40000000
215 #define TXCFG_MLB 0x20000000
216 #define TXCFG_ATP 0x10000000
217 #define TXCFG_ECRETRY 0x00800000
218 #define TXCFG_BRST_DIS 0x00080000
219 #define TXCFG_MXDMA1024 0x00000000
220 #define TXCFG_MXDMA512 0x00700000
221 #define TXCFG_MXDMA256 0x00600000
222 #define TXCFG_MXDMA128 0x00500000
223 #define TXCFG_MXDMA64 0x00400000
224 #define TXCFG_MXDMA32 0x00300000
225 #define TXCFG_MXDMA16 0x00200000
226 #define TXCFG_MXDMA8 0x00100000
228 #define CFG_LNKSTS 0x80000000
229 #define CFG_SPDSTS 0x60000000
230 #define CFG_SPDSTS1 0x40000000
231 #define CFG_SPDSTS0 0x20000000
232 #define CFG_DUPSTS 0x10000000
233 #define CFG_TBI_EN 0x01000000
234 #define CFG_MODE_1000 0x00400000
235 /* Ramit : Dont' ever use AUTO_1000, it never works and is buggy.
236 * Read the Phy response and then configure the MAC accordingly */
237 #define CFG_AUTO_1000 0x00200000
238 #define CFG_PINT_CTL 0x001c0000
239 #define CFG_PINT_DUPSTS 0x00100000
240 #define CFG_PINT_LNKSTS 0x00080000
241 #define CFG_PINT_SPDSTS 0x00040000
242 #define CFG_TMRTEST 0x00020000
243 #define CFG_MRM_DIS 0x00010000
244 #define CFG_MWI_DIS 0x00008000
245 #define CFG_T64ADDR 0x00004000
246 #define CFG_PCI64_DET 0x00002000
247 #define CFG_DATA64_EN 0x00001000
248 #define CFG_M64ADDR 0x00000800
249 #define CFG_PHY_RST 0x00000400
250 #define CFG_PHY_DIS 0x00000200
251 #define CFG_EXTSTS_EN 0x00000100
252 #define CFG_REQALG 0x00000080
253 #define CFG_SB 0x00000040
254 #define CFG_POW 0x00000020
255 #define CFG_EXD 0x00000010
256 #define CFG_PESEL 0x00000008
257 #define CFG_BROM_DIS 0x00000004
258 #define CFG_EXT_125 0x00000002
259 #define CFG_BEM 0x00000001
261 #define EXTSTS_UDPPKT 0x00200000
262 #define EXTSTS_TCPPKT 0x00080000
263 #define EXTSTS_IPPKT 0x00020000
264 #define EXTSTS_VPKT 0x00010000
265 #define EXTSTS_VTG_MASK 0x0000ffff
267 #define SPDSTS_POLARITY (CFG_SPDSTS1 | CFG_SPDSTS0 | CFG_DUPSTS | (lnksts ? CFG_LNKSTS : 0))
269 #define MIBC_MIBS 0x00000008
270 #define MIBC_ACLR 0x00000004
271 #define MIBC_FRZ 0x00000002
272 #define MIBC_WRN 0x00000001
274 #define PCR_PSEN (1 << 31)
275 #define PCR_PS_MCAST (1 << 30)
276 #define PCR_PS_DA (1 << 29)
277 #define PCR_STHI_8 (3 << 23)
278 #define PCR_STLO_4 (1 << 23)
279 #define PCR_FFHI_8K (3 << 21)
280 #define PCR_FFLO_4K (1 << 21)
281 #define PCR_PAUSE_CNT 0xFFFE
283 #define RXCFG_AEP 0x80000000
284 #define RXCFG_ARP 0x40000000
285 #define RXCFG_STRIPCRC 0x20000000
286 #define RXCFG_RX_FD 0x10000000
287 #define RXCFG_ALP 0x08000000
288 #define RXCFG_AIRL 0x04000000
289 #define RXCFG_MXDMA512 0x00700000
290 #define RXCFG_DRTH 0x0000003e
291 #define RXCFG_DRTH0 0x00000002
293 #define RFCR_RFEN 0x80000000
294 #define RFCR_AAB 0x40000000
295 #define RFCR_AAM 0x20000000
296 #define RFCR_AAU 0x10000000
297 #define RFCR_APM 0x08000000
298 #define RFCR_APAT 0x07800000
299 #define RFCR_APAT3 0x04000000
300 #define RFCR_APAT2 0x02000000
301 #define RFCR_APAT1 0x01000000
302 #define RFCR_APAT0 0x00800000
303 #define RFCR_AARP 0x00400000
304 #define RFCR_MHEN 0x00200000
305 #define RFCR_UHEN 0x00100000
306 #define RFCR_ULM 0x00080000
308 #define VRCR_RUDPE 0x00000080
309 #define VRCR_RTCPE 0x00000040
310 #define VRCR_RIPE 0x00000020
311 #define VRCR_IPEN 0x00000010
312 #define VRCR_DUTF 0x00000008
313 #define VRCR_DVTF 0x00000004
314 #define VRCR_VTREN 0x00000002
315 #define VRCR_VTDEN 0x00000001
317 #define VTCR_PPCHK 0x00000008
318 #define VTCR_GCHK 0x00000004
319 #define VTCR_VPPTI 0x00000002
320 #define VTCR_VGTI 0x00000001
322 #define CR 0x00
323 #define CFG 0x04
324 #define MEAR 0x08
325 #define PTSCR 0x0c
326 #define ISR 0x10
327 #define IMR 0x14
328 #define IER 0x18
329 #define IHR 0x1c
330 #define TXDP 0x20
331 #define TXDP_HI 0x24
332 #define TXCFG 0x28
333 #define GPIOR 0x2c
334 #define RXDP 0x30
335 #define RXDP_HI 0x34
336 #define RXCFG 0x38
337 #define PQCR 0x3c
338 #define WCSR 0x40
339 #define PCR 0x44
340 #define RFCR 0x48
341 #define RFDR 0x4c
343 #define SRR 0x58
345 #define VRCR 0xbc
346 #define VTCR 0xc0
347 #define VDR 0xc4
348 #define CCSR 0xcc
350 #define TBICR 0xe0
351 #define TBISR 0xe4
352 #define TANAR 0xe8
353 #define TANLPAR 0xec
354 #define TANER 0xf0
355 #define TESR 0xf4
357 #define TBICR_MR_AN_ENABLE 0x00001000
358 #define TBICR_MR_RESTART_AN 0x00000200
360 #define TBISR_MR_LINK_STATUS 0x00000020
361 #define TBISR_MR_AN_COMPLETE 0x00000004
363 #define TANAR_PS2 0x00000100
364 #define TANAR_PS1 0x00000080
365 #define TANAR_HALF_DUP 0x00000040
366 #define TANAR_FULL_DUP 0x00000020
368 #define GPIOR_GP5_OE 0x00000200
369 #define GPIOR_GP4_OE 0x00000100
370 #define GPIOR_GP3_OE 0x00000080
371 #define GPIOR_GP2_OE 0x00000040
372 #define GPIOR_GP1_OE 0x00000020
373 #define GPIOR_GP3_OUT 0x00000004
374 #define GPIOR_GP1_OUT 0x00000001
376 #define LINK_AUTONEGOTIATE 0x01
377 #define LINK_DOWN 0x02
378 #define LINK_UP 0x04
380 #define HW_ADDR_LEN sizeof(dma_addr_t)
381 #define desc_addr_set(desc, addr) \
382 do { \
383 ((desc)[0] = cpu_to_le32(addr)); \
384 if (HW_ADDR_LEN == 8) \
385 (desc)[1] = cpu_to_le32(((u64)addr) >> 32); \
386 } while(0)
387 #define desc_addr_get(desc) \
388 (le32_to_cpu((desc)[0]) | \
389 (HW_ADDR_LEN == 8 ? ((dma_addr_t)le32_to_cpu((desc)[1]))<<32 : 0))
391 #define DESC_LINK 0
392 #define DESC_BUFPTR (DESC_LINK + HW_ADDR_LEN/4)
393 #define DESC_CMDSTS (DESC_BUFPTR + HW_ADDR_LEN/4)
394 #define DESC_EXTSTS (DESC_CMDSTS + 4/4)
396 #define CMDSTS_OWN 0x80000000
397 #define CMDSTS_MORE 0x40000000
398 #define CMDSTS_INTR 0x20000000
399 #define CMDSTS_ERR 0x10000000
400 #define CMDSTS_OK 0x08000000
401 #define CMDSTS_RUNT 0x00200000
402 #define CMDSTS_LEN_MASK 0x0000ffff
404 #define CMDSTS_DEST_MASK 0x01800000
405 #define CMDSTS_DEST_SELF 0x00800000
406 #define CMDSTS_DEST_MULTI 0x01000000
408 #define DESC_SIZE 8 /* Should be cache line sized */
410 struct rx_info {
411 spinlock_t lock;
412 int up;
413 long idle;
415 struct sk_buff *skbs[NR_RX_DESC];
417 __le32 *next_rx_desc;
418 u16 next_rx, next_empty;
420 __le32 *descs;
421 dma_addr_t phy_descs;
425 struct ns83820 {
426 struct net_device_stats stats;
427 u8 __iomem *base;
429 struct pci_dev *pci_dev;
430 struct net_device *ndev;
432 #ifdef NS83820_VLAN_ACCEL_SUPPORT
433 struct vlan_group *vlgrp;
434 #endif
436 struct rx_info rx_info;
437 struct tasklet_struct rx_tasklet;
439 unsigned ihr;
440 struct work_struct tq_refill;
442 /* protects everything below. irqsave when using. */
443 spinlock_t misc_lock;
445 u32 CFG_cache;
447 u32 MEAR_cache;
448 u32 IMR_cache;
450 unsigned linkstate;
452 spinlock_t tx_lock;
454 u16 tx_done_idx;
455 u16 tx_idx;
456 volatile u16 tx_free_idx; /* idx of free desc chain */
457 u16 tx_intr_idx;
459 atomic_t nr_tx_skbs;
460 struct sk_buff *tx_skbs[NR_TX_DESC];
462 char pad[16] __attribute__((aligned(16)));
463 __le32 *tx_descs;
464 dma_addr_t tx_phy_descs;
466 struct timer_list tx_watchdog;
469 static inline struct ns83820 *PRIV(struct net_device *dev)
471 return netdev_priv(dev);
474 #define __kick_rx(dev) writel(CR_RXE, dev->base + CR)
476 static inline void kick_rx(struct net_device *ndev)
478 struct ns83820 *dev = PRIV(ndev);
479 dprintk("kick_rx: maybe kicking\n");
480 if (test_and_clear_bit(0, &dev->rx_info.idle)) {
481 dprintk("actually kicking\n");
482 writel(dev->rx_info.phy_descs +
483 (4 * DESC_SIZE * dev->rx_info.next_rx),
484 dev->base + RXDP);
485 if (dev->rx_info.next_rx == dev->rx_info.next_empty)
486 printk(KERN_DEBUG "%s: uh-oh: next_rx == next_empty???\n",
487 ndev->name);
488 __kick_rx(dev);
492 //free = (tx_done_idx + NR_TX_DESC-2 - free_idx) % NR_TX_DESC
493 #define start_tx_okay(dev) \
494 (((NR_TX_DESC-2 + dev->tx_done_idx - dev->tx_free_idx) % NR_TX_DESC) > MIN_TX_DESC_FREE)
497 #ifdef NS83820_VLAN_ACCEL_SUPPORT
498 static void ns83820_vlan_rx_register(struct net_device *ndev, struct vlan_group *grp)
500 struct ns83820 *dev = PRIV(ndev);
502 spin_lock_irq(&dev->misc_lock);
503 spin_lock(&dev->tx_lock);
505 dev->vlgrp = grp;
507 spin_unlock(&dev->tx_lock);
508 spin_unlock_irq(&dev->misc_lock);
511 static void ns83820_vlan_rx_kill_vid(struct net_device *ndev, unsigned short vid)
513 struct ns83820 *dev = PRIV(ndev);
515 spin_lock_irq(&dev->misc_lock);
516 spin_lock(&dev->tx_lock);
517 if (dev->vlgrp)
518 dev->vlgrp->vlan_devices[vid] = NULL;
519 spin_unlock(&dev->tx_lock);
520 spin_unlock_irq(&dev->misc_lock);
522 #endif
524 /* Packet Receiver
526 * The hardware supports linked lists of receive descriptors for
527 * which ownership is transfered back and forth by means of an
528 * ownership bit. While the hardware does support the use of a
529 * ring for receive descriptors, we only make use of a chain in
530 * an attempt to reduce bus traffic under heavy load scenarios.
531 * This will also make bugs a bit more obvious. The current code
532 * only makes use of a single rx chain; I hope to implement
533 * priority based rx for version 1.0. Goal: even under overload
534 * conditions, still route realtime traffic with as low jitter as
535 * possible.
537 static inline void build_rx_desc(struct ns83820 *dev, __le32 *desc, dma_addr_t link, dma_addr_t buf, u32 cmdsts, u32 extsts)
539 desc_addr_set(desc + DESC_LINK, link);
540 desc_addr_set(desc + DESC_BUFPTR, buf);
541 desc[DESC_EXTSTS] = cpu_to_le32(extsts);
542 mb();
543 desc[DESC_CMDSTS] = cpu_to_le32(cmdsts);
546 #define nr_rx_empty(dev) ((NR_RX_DESC-2 + dev->rx_info.next_rx - dev->rx_info.next_empty) % NR_RX_DESC)
547 static inline int ns83820_add_rx_skb(struct ns83820 *dev, struct sk_buff *skb)
549 unsigned next_empty;
550 u32 cmdsts;
551 __le32 *sg;
552 dma_addr_t buf;
554 next_empty = dev->rx_info.next_empty;
556 /* don't overrun last rx marker */
557 if (unlikely(nr_rx_empty(dev) <= 2)) {
558 kfree_skb(skb);
559 return 1;
562 #if 0
563 dprintk("next_empty[%d] nr_used[%d] next_rx[%d]\n",
564 dev->rx_info.next_empty,
565 dev->rx_info.nr_used,
566 dev->rx_info.next_rx
568 #endif
570 sg = dev->rx_info.descs + (next_empty * DESC_SIZE);
571 BUG_ON(NULL != dev->rx_info.skbs[next_empty]);
572 dev->rx_info.skbs[next_empty] = skb;
574 dev->rx_info.next_empty = (next_empty + 1) % NR_RX_DESC;
575 cmdsts = REAL_RX_BUF_SIZE | CMDSTS_INTR;
576 buf = pci_map_single(dev->pci_dev, skb->data,
577 REAL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
578 build_rx_desc(dev, sg, 0, buf, cmdsts, 0);
579 /* update link of previous rx */
580 if (likely(next_empty != dev->rx_info.next_rx))
581 dev->rx_info.descs[((NR_RX_DESC + next_empty - 1) % NR_RX_DESC) * DESC_SIZE] = cpu_to_le32(dev->rx_info.phy_descs + (next_empty * DESC_SIZE * 4));
583 return 0;
586 static inline int rx_refill(struct net_device *ndev, gfp_t gfp)
588 struct ns83820 *dev = PRIV(ndev);
589 unsigned i;
590 unsigned long flags = 0;
592 if (unlikely(nr_rx_empty(dev) <= 2))
593 return 0;
595 dprintk("rx_refill(%p)\n", ndev);
596 if (gfp == GFP_ATOMIC)
597 spin_lock_irqsave(&dev->rx_info.lock, flags);
598 for (i=0; i<NR_RX_DESC; i++) {
599 struct sk_buff *skb;
600 long res;
601 /* extra 16 bytes for alignment */
602 skb = __dev_alloc_skb(REAL_RX_BUF_SIZE+16, gfp);
603 if (unlikely(!skb))
604 break;
606 res = (long)skb->data & 0xf;
607 res = 0x10 - res;
608 res &= 0xf;
609 skb_reserve(skb, res);
611 skb->dev = ndev;
612 if (gfp != GFP_ATOMIC)
613 spin_lock_irqsave(&dev->rx_info.lock, flags);
614 res = ns83820_add_rx_skb(dev, skb);
615 if (gfp != GFP_ATOMIC)
616 spin_unlock_irqrestore(&dev->rx_info.lock, flags);
617 if (res) {
618 i = 1;
619 break;
622 if (gfp == GFP_ATOMIC)
623 spin_unlock_irqrestore(&dev->rx_info.lock, flags);
625 return i ? 0 : -ENOMEM;
628 static void FASTCALL(rx_refill_atomic(struct net_device *ndev));
629 static void fastcall rx_refill_atomic(struct net_device *ndev)
631 rx_refill(ndev, GFP_ATOMIC);
634 /* REFILL */
635 static inline void queue_refill(struct work_struct *work)
637 struct ns83820 *dev = container_of(work, struct ns83820, tq_refill);
638 struct net_device *ndev = dev->ndev;
640 rx_refill(ndev, GFP_KERNEL);
641 if (dev->rx_info.up)
642 kick_rx(ndev);
645 static inline void clear_rx_desc(struct ns83820 *dev, unsigned i)
647 build_rx_desc(dev, dev->rx_info.descs + (DESC_SIZE * i), 0, 0, CMDSTS_OWN, 0);
650 static void FASTCALL(phy_intr(struct net_device *ndev));
651 static void fastcall phy_intr(struct net_device *ndev)
653 struct ns83820 *dev = PRIV(ndev);
654 static const char *speeds[] = { "10", "100", "1000", "1000(?)", "1000F" };
655 u32 cfg, new_cfg;
656 u32 tbisr, tanar, tanlpar;
657 int speed, fullduplex, newlinkstate;
659 cfg = readl(dev->base + CFG) ^ SPDSTS_POLARITY;
661 if (dev->CFG_cache & CFG_TBI_EN) {
662 /* we have an optical transceiver */
663 tbisr = readl(dev->base + TBISR);
664 tanar = readl(dev->base + TANAR);
665 tanlpar = readl(dev->base + TANLPAR);
666 dprintk("phy_intr: tbisr=%08x, tanar=%08x, tanlpar=%08x\n",
667 tbisr, tanar, tanlpar);
669 if ( (fullduplex = (tanlpar & TANAR_FULL_DUP)
670 && (tanar & TANAR_FULL_DUP)) ) {
672 /* both of us are full duplex */
673 writel(readl(dev->base + TXCFG)
674 | TXCFG_CSI | TXCFG_HBI | TXCFG_ATP,
675 dev->base + TXCFG);
676 writel(readl(dev->base + RXCFG) | RXCFG_RX_FD,
677 dev->base + RXCFG);
678 /* Light up full duplex LED */
679 writel(readl(dev->base + GPIOR) | GPIOR_GP1_OUT,
680 dev->base + GPIOR);
682 } else if(((tanlpar & TANAR_HALF_DUP)
683 && (tanar & TANAR_HALF_DUP))
684 || ((tanlpar & TANAR_FULL_DUP)
685 && (tanar & TANAR_HALF_DUP))
686 || ((tanlpar & TANAR_HALF_DUP)
687 && (tanar & TANAR_FULL_DUP))) {
689 /* one or both of us are half duplex */
690 writel((readl(dev->base + TXCFG)
691 & ~(TXCFG_CSI | TXCFG_HBI)) | TXCFG_ATP,
692 dev->base + TXCFG);
693 writel(readl(dev->base + RXCFG) & ~RXCFG_RX_FD,
694 dev->base + RXCFG);
695 /* Turn off full duplex LED */
696 writel(readl(dev->base + GPIOR) & ~GPIOR_GP1_OUT,
697 dev->base + GPIOR);
700 speed = 4; /* 1000F */
702 } else {
703 /* we have a copper transceiver */
704 new_cfg = dev->CFG_cache & ~(CFG_SB | CFG_MODE_1000 | CFG_SPDSTS);
706 if (cfg & CFG_SPDSTS1)
707 new_cfg |= CFG_MODE_1000;
708 else
709 new_cfg &= ~CFG_MODE_1000;
711 speed = ((cfg / CFG_SPDSTS0) & 3);
712 fullduplex = (cfg & CFG_DUPSTS);
714 if (fullduplex) {
715 new_cfg |= CFG_SB;
716 writel(readl(dev->base + TXCFG)
717 | TXCFG_CSI | TXCFG_HBI,
718 dev->base + TXCFG);
719 writel(readl(dev->base + RXCFG) | RXCFG_RX_FD,
720 dev->base + RXCFG);
721 } else {
722 writel(readl(dev->base + TXCFG)
723 & ~(TXCFG_CSI | TXCFG_HBI),
724 dev->base + TXCFG);
725 writel(readl(dev->base + RXCFG) & ~(RXCFG_RX_FD),
726 dev->base + RXCFG);
729 if ((cfg & CFG_LNKSTS) &&
730 ((new_cfg ^ dev->CFG_cache) != 0)) {
731 writel(new_cfg, dev->base + CFG);
732 dev->CFG_cache = new_cfg;
735 dev->CFG_cache &= ~CFG_SPDSTS;
736 dev->CFG_cache |= cfg & CFG_SPDSTS;
739 newlinkstate = (cfg & CFG_LNKSTS) ? LINK_UP : LINK_DOWN;
741 if (newlinkstate & LINK_UP
742 && dev->linkstate != newlinkstate) {
743 netif_start_queue(ndev);
744 netif_wake_queue(ndev);
745 printk(KERN_INFO "%s: link now %s mbps, %s duplex and up.\n",
746 ndev->name,
747 speeds[speed],
748 fullduplex ? "full" : "half");
749 } else if (newlinkstate & LINK_DOWN
750 && dev->linkstate != newlinkstate) {
751 netif_stop_queue(ndev);
752 printk(KERN_INFO "%s: link now down.\n", ndev->name);
755 dev->linkstate = newlinkstate;
758 static int ns83820_setup_rx(struct net_device *ndev)
760 struct ns83820 *dev = PRIV(ndev);
761 unsigned i;
762 int ret;
764 dprintk("ns83820_setup_rx(%p)\n", ndev);
766 dev->rx_info.idle = 1;
767 dev->rx_info.next_rx = 0;
768 dev->rx_info.next_rx_desc = dev->rx_info.descs;
769 dev->rx_info.next_empty = 0;
771 for (i=0; i<NR_RX_DESC; i++)
772 clear_rx_desc(dev, i);
774 writel(0, dev->base + RXDP_HI);
775 writel(dev->rx_info.phy_descs, dev->base + RXDP);
777 ret = rx_refill(ndev, GFP_KERNEL);
778 if (!ret) {
779 dprintk("starting receiver\n");
780 /* prevent the interrupt handler from stomping on us */
781 spin_lock_irq(&dev->rx_info.lock);
783 writel(0x0001, dev->base + CCSR);
784 writel(0, dev->base + RFCR);
785 writel(0x7fc00000, dev->base + RFCR);
786 writel(0xffc00000, dev->base + RFCR);
788 dev->rx_info.up = 1;
790 phy_intr(ndev);
792 /* Okay, let it rip */
793 spin_lock_irq(&dev->misc_lock);
794 dev->IMR_cache |= ISR_PHY;
795 dev->IMR_cache |= ISR_RXRCMP;
796 //dev->IMR_cache |= ISR_RXERR;
797 //dev->IMR_cache |= ISR_RXOK;
798 dev->IMR_cache |= ISR_RXORN;
799 dev->IMR_cache |= ISR_RXSOVR;
800 dev->IMR_cache |= ISR_RXDESC;
801 dev->IMR_cache |= ISR_RXIDLE;
802 dev->IMR_cache |= ISR_TXDESC;
803 dev->IMR_cache |= ISR_TXIDLE;
805 writel(dev->IMR_cache, dev->base + IMR);
806 writel(1, dev->base + IER);
807 spin_unlock(&dev->misc_lock);
809 kick_rx(ndev);
811 spin_unlock_irq(&dev->rx_info.lock);
813 return ret;
816 static void ns83820_cleanup_rx(struct ns83820 *dev)
818 unsigned i;
819 unsigned long flags;
821 dprintk("ns83820_cleanup_rx(%p)\n", dev);
823 /* disable receive interrupts */
824 spin_lock_irqsave(&dev->misc_lock, flags);
825 dev->IMR_cache &= ~(ISR_RXOK | ISR_RXDESC | ISR_RXERR | ISR_RXEARLY | ISR_RXIDLE);
826 writel(dev->IMR_cache, dev->base + IMR);
827 spin_unlock_irqrestore(&dev->misc_lock, flags);
829 /* synchronize with the interrupt handler and kill it */
830 dev->rx_info.up = 0;
831 synchronize_irq(dev->pci_dev->irq);
833 /* touch the pci bus... */
834 readl(dev->base + IMR);
836 /* assumes the transmitter is already disabled and reset */
837 writel(0, dev->base + RXDP_HI);
838 writel(0, dev->base + RXDP);
840 for (i=0; i<NR_RX_DESC; i++) {
841 struct sk_buff *skb = dev->rx_info.skbs[i];
842 dev->rx_info.skbs[i] = NULL;
843 clear_rx_desc(dev, i);
844 if (skb)
845 kfree_skb(skb);
849 static void FASTCALL(ns83820_rx_kick(struct net_device *ndev));
850 static void fastcall ns83820_rx_kick(struct net_device *ndev)
852 struct ns83820 *dev = PRIV(ndev);
853 /*if (nr_rx_empty(dev) >= NR_RX_DESC/4)*/ {
854 if (dev->rx_info.up) {
855 rx_refill_atomic(ndev);
856 kick_rx(ndev);
860 if (dev->rx_info.up && nr_rx_empty(dev) > NR_RX_DESC*3/4)
861 schedule_work(&dev->tq_refill);
862 else
863 kick_rx(ndev);
864 if (dev->rx_info.idle)
865 printk(KERN_DEBUG "%s: BAD\n", ndev->name);
868 /* rx_irq
871 static void FASTCALL(rx_irq(struct net_device *ndev));
872 static void fastcall rx_irq(struct net_device *ndev)
874 struct ns83820 *dev = PRIV(ndev);
875 struct rx_info *info = &dev->rx_info;
876 unsigned next_rx;
877 int rx_rc, len;
878 u32 cmdsts;
879 __le32 *desc;
880 unsigned long flags;
881 int nr = 0;
883 dprintk("rx_irq(%p)\n", ndev);
884 dprintk("rxdp: %08x, descs: %08lx next_rx[%d]: %p next_empty[%d]: %p\n",
885 readl(dev->base + RXDP),
886 (long)(dev->rx_info.phy_descs),
887 (int)dev->rx_info.next_rx,
888 (dev->rx_info.descs + (DESC_SIZE * dev->rx_info.next_rx)),
889 (int)dev->rx_info.next_empty,
890 (dev->rx_info.descs + (DESC_SIZE * dev->rx_info.next_empty))
893 spin_lock_irqsave(&info->lock, flags);
894 if (!info->up)
895 goto out;
897 dprintk("walking descs\n");
898 next_rx = info->next_rx;
899 desc = info->next_rx_desc;
900 while ((CMDSTS_OWN & (cmdsts = le32_to_cpu(desc[DESC_CMDSTS]))) &&
901 (cmdsts != CMDSTS_OWN)) {
902 struct sk_buff *skb;
903 u32 extsts = le32_to_cpu(desc[DESC_EXTSTS]);
904 dma_addr_t bufptr = desc_addr_get(desc + DESC_BUFPTR);
906 dprintk("cmdsts: %08x\n", cmdsts);
907 dprintk("link: %08x\n", cpu_to_le32(desc[DESC_LINK]));
908 dprintk("extsts: %08x\n", extsts);
910 skb = info->skbs[next_rx];
911 info->skbs[next_rx] = NULL;
912 info->next_rx = (next_rx + 1) % NR_RX_DESC;
914 mb();
915 clear_rx_desc(dev, next_rx);
917 pci_unmap_single(dev->pci_dev, bufptr,
918 RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
919 len = cmdsts & CMDSTS_LEN_MASK;
920 #ifdef NS83820_VLAN_ACCEL_SUPPORT
921 /* NH: As was mentioned below, this chip is kinda
922 * brain dead about vlan tag stripping. Frames
923 * that are 64 bytes with a vlan header appended
924 * like arp frames, or pings, are flagged as Runts
925 * when the tag is stripped and hardware. This
926 * also means that the OK bit in the descriptor
927 * is cleared when the frame comes in so we have
928 * to do a specific length check here to make sure
929 * the frame would have been ok, had we not stripped
930 * the tag.
932 if (likely((CMDSTS_OK & cmdsts) ||
933 ((cmdsts & CMDSTS_RUNT) && len >= 56))) {
934 #else
935 if (likely(CMDSTS_OK & cmdsts)) {
936 #endif
937 skb_put(skb, len);
938 if (unlikely(!skb))
939 goto netdev_mangle_me_harder_failed;
940 if (cmdsts & CMDSTS_DEST_MULTI)
941 dev->stats.multicast ++;
942 dev->stats.rx_packets ++;
943 dev->stats.rx_bytes += len;
944 if ((extsts & 0x002a0000) && !(extsts & 0x00540000)) {
945 skb->ip_summed = CHECKSUM_UNNECESSARY;
946 } else {
947 skb->ip_summed = CHECKSUM_NONE;
949 skb->protocol = eth_type_trans(skb, ndev);
950 #ifdef NS83820_VLAN_ACCEL_SUPPORT
951 if(extsts & EXTSTS_VPKT) {
952 unsigned short tag;
953 tag = ntohs(extsts & EXTSTS_VTG_MASK);
954 rx_rc = vlan_hwaccel_rx(skb,dev->vlgrp,tag);
955 } else {
956 rx_rc = netif_rx(skb);
958 #else
959 rx_rc = netif_rx(skb);
960 #endif
961 if (NET_RX_DROP == rx_rc) {
962 netdev_mangle_me_harder_failed:
963 dev->stats.rx_dropped ++;
965 } else {
966 kfree_skb(skb);
969 nr++;
970 next_rx = info->next_rx;
971 desc = info->descs + (DESC_SIZE * next_rx);
973 info->next_rx = next_rx;
974 info->next_rx_desc = info->descs + (DESC_SIZE * next_rx);
976 out:
977 if (0 && !nr) {
978 Dprintk("dazed: cmdsts_f: %08x\n", cmdsts);
981 spin_unlock_irqrestore(&info->lock, flags);
984 static void rx_action(unsigned long _dev)
986 struct net_device *ndev = (void *)_dev;
987 struct ns83820 *dev = PRIV(ndev);
988 rx_irq(ndev);
989 writel(ihr, dev->base + IHR);
991 spin_lock_irq(&dev->misc_lock);
992 dev->IMR_cache |= ISR_RXDESC;
993 writel(dev->IMR_cache, dev->base + IMR);
994 spin_unlock_irq(&dev->misc_lock);
996 rx_irq(ndev);
997 ns83820_rx_kick(ndev);
1000 /* Packet Transmit code
1002 static inline void kick_tx(struct ns83820 *dev)
1004 dprintk("kick_tx(%p): tx_idx=%d free_idx=%d\n",
1005 dev, dev->tx_idx, dev->tx_free_idx);
1006 writel(CR_TXE, dev->base + CR);
1009 /* No spinlock needed on the transmit irq path as the interrupt handler is
1010 * serialized.
1012 static void do_tx_done(struct net_device *ndev)
1014 struct ns83820 *dev = PRIV(ndev);
1015 u32 cmdsts, tx_done_idx;
1016 __le32 *desc;
1018 dprintk("do_tx_done(%p)\n", ndev);
1019 tx_done_idx = dev->tx_done_idx;
1020 desc = dev->tx_descs + (tx_done_idx * DESC_SIZE);
1022 dprintk("tx_done_idx=%d free_idx=%d cmdsts=%08x\n",
1023 tx_done_idx, dev->tx_free_idx, le32_to_cpu(desc[DESC_CMDSTS]));
1024 while ((tx_done_idx != dev->tx_free_idx) &&
1025 !(CMDSTS_OWN & (cmdsts = le32_to_cpu(desc[DESC_CMDSTS]))) ) {
1026 struct sk_buff *skb;
1027 unsigned len;
1028 dma_addr_t addr;
1030 if (cmdsts & CMDSTS_ERR)
1031 dev->stats.tx_errors ++;
1032 if (cmdsts & CMDSTS_OK)
1033 dev->stats.tx_packets ++;
1034 if (cmdsts & CMDSTS_OK)
1035 dev->stats.tx_bytes += cmdsts & 0xffff;
1037 dprintk("tx_done_idx=%d free_idx=%d cmdsts=%08x\n",
1038 tx_done_idx, dev->tx_free_idx, cmdsts);
1039 skb = dev->tx_skbs[tx_done_idx];
1040 dev->tx_skbs[tx_done_idx] = NULL;
1041 dprintk("done(%p)\n", skb);
1043 len = cmdsts & CMDSTS_LEN_MASK;
1044 addr = desc_addr_get(desc + DESC_BUFPTR);
1045 if (skb) {
1046 pci_unmap_single(dev->pci_dev,
1047 addr,
1048 len,
1049 PCI_DMA_TODEVICE);
1050 dev_kfree_skb_irq(skb);
1051 atomic_dec(&dev->nr_tx_skbs);
1052 } else
1053 pci_unmap_page(dev->pci_dev,
1054 addr,
1055 len,
1056 PCI_DMA_TODEVICE);
1058 tx_done_idx = (tx_done_idx + 1) % NR_TX_DESC;
1059 dev->tx_done_idx = tx_done_idx;
1060 desc[DESC_CMDSTS] = cpu_to_le32(0);
1061 mb();
1062 desc = dev->tx_descs + (tx_done_idx * DESC_SIZE);
1065 /* Allow network stack to resume queueing packets after we've
1066 * finished transmitting at least 1/4 of the packets in the queue.
1068 if (netif_queue_stopped(ndev) && start_tx_okay(dev)) {
1069 dprintk("start_queue(%p)\n", ndev);
1070 netif_start_queue(ndev);
1071 netif_wake_queue(ndev);
1075 static void ns83820_cleanup_tx(struct ns83820 *dev)
1077 unsigned i;
1079 for (i=0; i<NR_TX_DESC; i++) {
1080 struct sk_buff *skb = dev->tx_skbs[i];
1081 dev->tx_skbs[i] = NULL;
1082 if (skb) {
1083 __le32 *desc = dev->tx_descs + (i * DESC_SIZE);
1084 pci_unmap_single(dev->pci_dev,
1085 desc_addr_get(desc + DESC_BUFPTR),
1086 le32_to_cpu(desc[DESC_CMDSTS]) & CMDSTS_LEN_MASK,
1087 PCI_DMA_TODEVICE);
1088 dev_kfree_skb_irq(skb);
1089 atomic_dec(&dev->nr_tx_skbs);
1093 memset(dev->tx_descs, 0, NR_TX_DESC * DESC_SIZE * 4);
1096 /* transmit routine. This code relies on the network layer serializing
1097 * its calls in, but will run happily in parallel with the interrupt
1098 * handler. This code currently has provisions for fragmenting tx buffers
1099 * while trying to track down a bug in either the zero copy code or
1100 * the tx fifo (hence the MAX_FRAG_LEN).
1102 static int ns83820_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1104 struct ns83820 *dev = PRIV(ndev);
1105 u32 free_idx, cmdsts, extsts;
1106 int nr_free, nr_frags;
1107 unsigned tx_done_idx, last_idx;
1108 dma_addr_t buf;
1109 unsigned len;
1110 skb_frag_t *frag;
1111 int stopped = 0;
1112 int do_intr = 0;
1113 volatile __le32 *first_desc;
1115 dprintk("ns83820_hard_start_xmit\n");
1117 nr_frags = skb_shinfo(skb)->nr_frags;
1118 again:
1119 if (unlikely(dev->CFG_cache & CFG_LNKSTS)) {
1120 netif_stop_queue(ndev);
1121 if (unlikely(dev->CFG_cache & CFG_LNKSTS))
1122 return 1;
1123 netif_start_queue(ndev);
1126 last_idx = free_idx = dev->tx_free_idx;
1127 tx_done_idx = dev->tx_done_idx;
1128 nr_free = (tx_done_idx + NR_TX_DESC-2 - free_idx) % NR_TX_DESC;
1129 nr_free -= 1;
1130 if (nr_free <= nr_frags) {
1131 dprintk("stop_queue - not enough(%p)\n", ndev);
1132 netif_stop_queue(ndev);
1134 /* Check again: we may have raced with a tx done irq */
1135 if (dev->tx_done_idx != tx_done_idx) {
1136 dprintk("restart queue(%p)\n", ndev);
1137 netif_start_queue(ndev);
1138 goto again;
1140 return 1;
1143 if (free_idx == dev->tx_intr_idx) {
1144 do_intr = 1;
1145 dev->tx_intr_idx = (dev->tx_intr_idx + NR_TX_DESC/4) % NR_TX_DESC;
1148 nr_free -= nr_frags;
1149 if (nr_free < MIN_TX_DESC_FREE) {
1150 dprintk("stop_queue - last entry(%p)\n", ndev);
1151 netif_stop_queue(ndev);
1152 stopped = 1;
1155 frag = skb_shinfo(skb)->frags;
1156 if (!nr_frags)
1157 frag = NULL;
1158 extsts = 0;
1159 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1160 extsts |= EXTSTS_IPPKT;
1161 if (IPPROTO_TCP == skb->nh.iph->protocol)
1162 extsts |= EXTSTS_TCPPKT;
1163 else if (IPPROTO_UDP == skb->nh.iph->protocol)
1164 extsts |= EXTSTS_UDPPKT;
1167 #ifdef NS83820_VLAN_ACCEL_SUPPORT
1168 if(vlan_tx_tag_present(skb)) {
1169 /* fetch the vlan tag info out of the
1170 * ancilliary data if the vlan code
1171 * is using hw vlan acceleration
1173 short tag = vlan_tx_tag_get(skb);
1174 extsts |= (EXTSTS_VPKT | htons(tag));
1176 #endif
1178 len = skb->len;
1179 if (nr_frags)
1180 len -= skb->data_len;
1181 buf = pci_map_single(dev->pci_dev, skb->data, len, PCI_DMA_TODEVICE);
1183 first_desc = dev->tx_descs + (free_idx * DESC_SIZE);
1185 for (;;) {
1186 volatile __le32 *desc = dev->tx_descs + (free_idx * DESC_SIZE);
1188 dprintk("frag[%3u]: %4u @ 0x%08Lx\n", free_idx, len,
1189 (unsigned long long)buf);
1190 last_idx = free_idx;
1191 free_idx = (free_idx + 1) % NR_TX_DESC;
1192 desc[DESC_LINK] = cpu_to_le32(dev->tx_phy_descs + (free_idx * DESC_SIZE * 4));
1193 desc_addr_set(desc + DESC_BUFPTR, buf);
1194 desc[DESC_EXTSTS] = cpu_to_le32(extsts);
1196 cmdsts = ((nr_frags) ? CMDSTS_MORE : do_intr ? CMDSTS_INTR : 0);
1197 cmdsts |= (desc == first_desc) ? 0 : CMDSTS_OWN;
1198 cmdsts |= len;
1199 desc[DESC_CMDSTS] = cpu_to_le32(cmdsts);
1201 if (!nr_frags)
1202 break;
1204 buf = pci_map_page(dev->pci_dev, frag->page,
1205 frag->page_offset,
1206 frag->size, PCI_DMA_TODEVICE);
1207 dprintk("frag: buf=%08Lx page=%08lx offset=%08lx\n",
1208 (long long)buf, (long) page_to_pfn(frag->page),
1209 frag->page_offset);
1210 len = frag->size;
1211 frag++;
1212 nr_frags--;
1214 dprintk("done pkt\n");
1216 spin_lock_irq(&dev->tx_lock);
1217 dev->tx_skbs[last_idx] = skb;
1218 first_desc[DESC_CMDSTS] |= cpu_to_le32(CMDSTS_OWN);
1219 dev->tx_free_idx = free_idx;
1220 atomic_inc(&dev->nr_tx_skbs);
1221 spin_unlock_irq(&dev->tx_lock);
1223 kick_tx(dev);
1225 /* Check again: we may have raced with a tx done irq */
1226 if (stopped && (dev->tx_done_idx != tx_done_idx) && start_tx_okay(dev))
1227 netif_start_queue(ndev);
1229 /* set the transmit start time to catch transmit timeouts */
1230 ndev->trans_start = jiffies;
1231 return 0;
1234 static void ns83820_update_stats(struct ns83820 *dev)
1236 u8 __iomem *base = dev->base;
1238 /* the DP83820 will freeze counters, so we need to read all of them */
1239 dev->stats.rx_errors += readl(base + 0x60) & 0xffff;
1240 dev->stats.rx_crc_errors += readl(base + 0x64) & 0xffff;
1241 dev->stats.rx_missed_errors += readl(base + 0x68) & 0xffff;
1242 dev->stats.rx_frame_errors += readl(base + 0x6c) & 0xffff;
1243 /*dev->stats.rx_symbol_errors +=*/ readl(base + 0x70);
1244 dev->stats.rx_length_errors += readl(base + 0x74) & 0xffff;
1245 dev->stats.rx_length_errors += readl(base + 0x78) & 0xffff;
1246 /*dev->stats.rx_badopcode_errors += */ readl(base + 0x7c);
1247 /*dev->stats.rx_pause_count += */ readl(base + 0x80);
1248 /*dev->stats.tx_pause_count += */ readl(base + 0x84);
1249 dev->stats.tx_carrier_errors += readl(base + 0x88) & 0xff;
1252 static struct net_device_stats *ns83820_get_stats(struct net_device *ndev)
1254 struct ns83820 *dev = PRIV(ndev);
1256 /* somewhat overkill */
1257 spin_lock_irq(&dev->misc_lock);
1258 ns83820_update_stats(dev);
1259 spin_unlock_irq(&dev->misc_lock);
1261 return &dev->stats;
1264 static void ns83820_get_drvinfo(struct net_device *ndev, struct ethtool_drvinfo *info)
1266 struct ns83820 *dev = PRIV(ndev);
1267 strcpy(info->driver, "ns83820");
1268 strcpy(info->version, VERSION);
1269 strcpy(info->bus_info, pci_name(dev->pci_dev));
1272 static u32 ns83820_get_link(struct net_device *ndev)
1274 struct ns83820 *dev = PRIV(ndev);
1275 u32 cfg = readl(dev->base + CFG) ^ SPDSTS_POLARITY;
1276 return cfg & CFG_LNKSTS ? 1 : 0;
1279 static const struct ethtool_ops ops = {
1280 .get_drvinfo = ns83820_get_drvinfo,
1281 .get_link = ns83820_get_link
1284 /* this function is called in irq context from the ISR */
1285 static void ns83820_mib_isr(struct ns83820 *dev)
1287 unsigned long flags;
1288 spin_lock_irqsave(&dev->misc_lock, flags);
1289 ns83820_update_stats(dev);
1290 spin_unlock_irqrestore(&dev->misc_lock, flags);
1293 static void ns83820_do_isr(struct net_device *ndev, u32 isr);
1294 static irqreturn_t ns83820_irq(int foo, void *data)
1296 struct net_device *ndev = data;
1297 struct ns83820 *dev = PRIV(ndev);
1298 u32 isr;
1299 dprintk("ns83820_irq(%p)\n", ndev);
1301 dev->ihr = 0;
1303 isr = readl(dev->base + ISR);
1304 dprintk("irq: %08x\n", isr);
1305 ns83820_do_isr(ndev, isr);
1306 return IRQ_HANDLED;
1309 static void ns83820_do_isr(struct net_device *ndev, u32 isr)
1311 struct ns83820 *dev = PRIV(ndev);
1312 unsigned long flags;
1314 #ifdef DEBUG
1315 if (isr & ~(ISR_PHY | ISR_RXDESC | ISR_RXEARLY | ISR_RXOK | ISR_RXERR | ISR_TXIDLE | ISR_TXOK | ISR_TXDESC))
1316 Dprintk("odd isr? 0x%08x\n", isr);
1317 #endif
1319 if (ISR_RXIDLE & isr) {
1320 dev->rx_info.idle = 1;
1321 Dprintk("oh dear, we are idle\n");
1322 ns83820_rx_kick(ndev);
1325 if ((ISR_RXDESC | ISR_RXOK) & isr) {
1326 prefetch(dev->rx_info.next_rx_desc);
1328 spin_lock_irqsave(&dev->misc_lock, flags);
1329 dev->IMR_cache &= ~(ISR_RXDESC | ISR_RXOK);
1330 writel(dev->IMR_cache, dev->base + IMR);
1331 spin_unlock_irqrestore(&dev->misc_lock, flags);
1333 tasklet_schedule(&dev->rx_tasklet);
1334 //rx_irq(ndev);
1335 //writel(4, dev->base + IHR);
1338 if ((ISR_RXIDLE | ISR_RXORN | ISR_RXDESC | ISR_RXOK | ISR_RXERR) & isr)
1339 ns83820_rx_kick(ndev);
1341 if (unlikely(ISR_RXSOVR & isr)) {
1342 //printk("overrun: rxsovr\n");
1343 dev->stats.rx_fifo_errors ++;
1346 if (unlikely(ISR_RXORN & isr)) {
1347 //printk("overrun: rxorn\n");
1348 dev->stats.rx_fifo_errors ++;
1351 if ((ISR_RXRCMP & isr) && dev->rx_info.up)
1352 writel(CR_RXE, dev->base + CR);
1354 if (ISR_TXIDLE & isr) {
1355 u32 txdp;
1356 txdp = readl(dev->base + TXDP);
1357 dprintk("txdp: %08x\n", txdp);
1358 txdp -= dev->tx_phy_descs;
1359 dev->tx_idx = txdp / (DESC_SIZE * 4);
1360 if (dev->tx_idx >= NR_TX_DESC) {
1361 printk(KERN_ALERT "%s: BUG -- txdp out of range\n", ndev->name);
1362 dev->tx_idx = 0;
1364 /* The may have been a race between a pci originated read
1365 * and the descriptor update from the cpu. Just in case,
1366 * kick the transmitter if the hardware thinks it is on a
1367 * different descriptor than we are.
1369 if (dev->tx_idx != dev->tx_free_idx)
1370 kick_tx(dev);
1373 /* Defer tx ring processing until more than a minimum amount of
1374 * work has accumulated
1376 if ((ISR_TXDESC | ISR_TXIDLE | ISR_TXOK | ISR_TXERR) & isr) {
1377 spin_lock_irqsave(&dev->tx_lock, flags);
1378 do_tx_done(ndev);
1379 spin_unlock_irqrestore(&dev->tx_lock, flags);
1381 /* Disable TxOk if there are no outstanding tx packets.
1383 if ((dev->tx_done_idx == dev->tx_free_idx) &&
1384 (dev->IMR_cache & ISR_TXOK)) {
1385 spin_lock_irqsave(&dev->misc_lock, flags);
1386 dev->IMR_cache &= ~ISR_TXOK;
1387 writel(dev->IMR_cache, dev->base + IMR);
1388 spin_unlock_irqrestore(&dev->misc_lock, flags);
1392 /* The TxIdle interrupt can come in before the transmit has
1393 * completed. Normally we reap packets off of the combination
1394 * of TxDesc and TxIdle and leave TxOk disabled (since it
1395 * occurs on every packet), but when no further irqs of this
1396 * nature are expected, we must enable TxOk.
1398 if ((ISR_TXIDLE & isr) && (dev->tx_done_idx != dev->tx_free_idx)) {
1399 spin_lock_irqsave(&dev->misc_lock, flags);
1400 dev->IMR_cache |= ISR_TXOK;
1401 writel(dev->IMR_cache, dev->base + IMR);
1402 spin_unlock_irqrestore(&dev->misc_lock, flags);
1405 /* MIB interrupt: one of the statistics counters is about to overflow */
1406 if (unlikely(ISR_MIB & isr))
1407 ns83820_mib_isr(dev);
1409 /* PHY: Link up/down/negotiation state change */
1410 if (unlikely(ISR_PHY & isr))
1411 phy_intr(ndev);
1413 #if 0 /* Still working on the interrupt mitigation strategy */
1414 if (dev->ihr)
1415 writel(dev->ihr, dev->base + IHR);
1416 #endif
1419 static void ns83820_do_reset(struct ns83820 *dev, u32 which)
1421 Dprintk("resetting chip...\n");
1422 writel(which, dev->base + CR);
1423 do {
1424 schedule();
1425 } while (readl(dev->base + CR) & which);
1426 Dprintk("okay!\n");
1429 static int ns83820_stop(struct net_device *ndev)
1431 struct ns83820 *dev = PRIV(ndev);
1433 /* FIXME: protect against interrupt handler? */
1434 del_timer_sync(&dev->tx_watchdog);
1436 /* disable interrupts */
1437 writel(0, dev->base + IMR);
1438 writel(0, dev->base + IER);
1439 readl(dev->base + IER);
1441 dev->rx_info.up = 0;
1442 synchronize_irq(dev->pci_dev->irq);
1444 ns83820_do_reset(dev, CR_RST);
1446 synchronize_irq(dev->pci_dev->irq);
1448 spin_lock_irq(&dev->misc_lock);
1449 dev->IMR_cache &= ~(ISR_TXURN | ISR_TXIDLE | ISR_TXERR | ISR_TXDESC | ISR_TXOK);
1450 spin_unlock_irq(&dev->misc_lock);
1452 ns83820_cleanup_rx(dev);
1453 ns83820_cleanup_tx(dev);
1455 return 0;
1458 static void ns83820_tx_timeout(struct net_device *ndev)
1460 struct ns83820 *dev = PRIV(ndev);
1461 u32 tx_done_idx;
1462 __le32 *desc;
1463 unsigned long flags;
1465 spin_lock_irqsave(&dev->tx_lock, flags);
1467 tx_done_idx = dev->tx_done_idx;
1468 desc = dev->tx_descs + (tx_done_idx * DESC_SIZE);
1470 printk(KERN_INFO "%s: tx_timeout: tx_done_idx=%d free_idx=%d cmdsts=%08x\n",
1471 ndev->name,
1472 tx_done_idx, dev->tx_free_idx, le32_to_cpu(desc[DESC_CMDSTS]));
1474 #if defined(DEBUG)
1476 u32 isr;
1477 isr = readl(dev->base + ISR);
1478 printk("irq: %08x imr: %08x\n", isr, dev->IMR_cache);
1479 ns83820_do_isr(ndev, isr);
1481 #endif
1483 do_tx_done(ndev);
1485 tx_done_idx = dev->tx_done_idx;
1486 desc = dev->tx_descs + (tx_done_idx * DESC_SIZE);
1488 printk(KERN_INFO "%s: after: tx_done_idx=%d free_idx=%d cmdsts=%08x\n",
1489 ndev->name,
1490 tx_done_idx, dev->tx_free_idx, le32_to_cpu(desc[DESC_CMDSTS]));
1492 spin_unlock_irqrestore(&dev->tx_lock, flags);
1495 static void ns83820_tx_watch(unsigned long data)
1497 struct net_device *ndev = (void *)data;
1498 struct ns83820 *dev = PRIV(ndev);
1500 #if defined(DEBUG)
1501 printk("ns83820_tx_watch: %u %u %d\n",
1502 dev->tx_done_idx, dev->tx_free_idx, atomic_read(&dev->nr_tx_skbs)
1504 #endif
1506 if (time_after(jiffies, ndev->trans_start + 1*HZ) &&
1507 dev->tx_done_idx != dev->tx_free_idx) {
1508 printk(KERN_DEBUG "%s: ns83820_tx_watch: %u %u %d\n",
1509 ndev->name,
1510 dev->tx_done_idx, dev->tx_free_idx,
1511 atomic_read(&dev->nr_tx_skbs));
1512 ns83820_tx_timeout(ndev);
1515 mod_timer(&dev->tx_watchdog, jiffies + 2*HZ);
1518 static int ns83820_open(struct net_device *ndev)
1520 struct ns83820 *dev = PRIV(ndev);
1521 unsigned i;
1522 u32 desc;
1523 int ret;
1525 dprintk("ns83820_open\n");
1527 writel(0, dev->base + PQCR);
1529 ret = ns83820_setup_rx(ndev);
1530 if (ret)
1531 goto failed;
1533 memset(dev->tx_descs, 0, 4 * NR_TX_DESC * DESC_SIZE);
1534 for (i=0; i<NR_TX_DESC; i++) {
1535 dev->tx_descs[(i * DESC_SIZE) + DESC_LINK]
1536 = cpu_to_le32(
1537 dev->tx_phy_descs
1538 + ((i+1) % NR_TX_DESC) * DESC_SIZE * 4);
1541 dev->tx_idx = 0;
1542 dev->tx_done_idx = 0;
1543 desc = dev->tx_phy_descs;
1544 writel(0, dev->base + TXDP_HI);
1545 writel(desc, dev->base + TXDP);
1547 init_timer(&dev->tx_watchdog);
1548 dev->tx_watchdog.data = (unsigned long)ndev;
1549 dev->tx_watchdog.function = ns83820_tx_watch;
1550 mod_timer(&dev->tx_watchdog, jiffies + 2*HZ);
1552 netif_start_queue(ndev); /* FIXME: wait for phy to come up */
1554 return 0;
1556 failed:
1557 ns83820_stop(ndev);
1558 return ret;
1561 static void ns83820_getmac(struct ns83820 *dev, u8 *mac)
1563 unsigned i;
1564 for (i=0; i<3; i++) {
1565 u32 data;
1567 /* Read from the perfect match memory: this is loaded by
1568 * the chip from the EEPROM via the EELOAD self test.
1570 writel(i*2, dev->base + RFCR);
1571 data = readl(dev->base + RFDR);
1573 *mac++ = data;
1574 *mac++ = data >> 8;
1578 static int ns83820_change_mtu(struct net_device *ndev, int new_mtu)
1580 if (new_mtu > RX_BUF_SIZE)
1581 return -EINVAL;
1582 ndev->mtu = new_mtu;
1583 return 0;
1586 static void ns83820_set_multicast(struct net_device *ndev)
1588 struct ns83820 *dev = PRIV(ndev);
1589 u8 __iomem *rfcr = dev->base + RFCR;
1590 u32 and_mask = 0xffffffff;
1591 u32 or_mask = 0;
1592 u32 val;
1594 if (ndev->flags & IFF_PROMISC)
1595 or_mask |= RFCR_AAU | RFCR_AAM;
1596 else
1597 and_mask &= ~(RFCR_AAU | RFCR_AAM);
1599 if (ndev->flags & IFF_ALLMULTI)
1600 or_mask |= RFCR_AAM;
1601 else
1602 and_mask &= ~RFCR_AAM;
1604 spin_lock_irq(&dev->misc_lock);
1605 val = (readl(rfcr) & and_mask) | or_mask;
1606 /* Ramit : RFCR Write Fix doc says RFEN must be 0 modify other bits */
1607 writel(val & ~RFCR_RFEN, rfcr);
1608 writel(val, rfcr);
1609 spin_unlock_irq(&dev->misc_lock);
1612 static void ns83820_run_bist(struct net_device *ndev, const char *name, u32 enable, u32 done, u32 fail)
1614 struct ns83820 *dev = PRIV(ndev);
1615 int timed_out = 0;
1616 unsigned long start;
1617 u32 status;
1618 int loops = 0;
1620 dprintk("%s: start %s\n", ndev->name, name);
1622 start = jiffies;
1624 writel(enable, dev->base + PTSCR);
1625 for (;;) {
1626 loops++;
1627 status = readl(dev->base + PTSCR);
1628 if (!(status & enable))
1629 break;
1630 if (status & done)
1631 break;
1632 if (status & fail)
1633 break;
1634 if (time_after_eq(jiffies, start + HZ)) {
1635 timed_out = 1;
1636 break;
1638 schedule_timeout_uninterruptible(1);
1641 if (status & fail)
1642 printk(KERN_INFO "%s: %s failed! (0x%08x & 0x%08x)\n",
1643 ndev->name, name, status, fail);
1644 else if (timed_out)
1645 printk(KERN_INFO "%s: run_bist %s timed out! (%08x)\n",
1646 ndev->name, name, status);
1648 dprintk("%s: done %s in %d loops\n", ndev->name, name, loops);
1651 #ifdef PHY_CODE_IS_FINISHED
1652 static void ns83820_mii_write_bit(struct ns83820 *dev, int bit)
1654 /* drive MDC low */
1655 dev->MEAR_cache &= ~MEAR_MDC;
1656 writel(dev->MEAR_cache, dev->base + MEAR);
1657 readl(dev->base + MEAR);
1659 /* enable output, set bit */
1660 dev->MEAR_cache |= MEAR_MDDIR;
1661 if (bit)
1662 dev->MEAR_cache |= MEAR_MDIO;
1663 else
1664 dev->MEAR_cache &= ~MEAR_MDIO;
1666 /* set the output bit */
1667 writel(dev->MEAR_cache, dev->base + MEAR);
1668 readl(dev->base + MEAR);
1670 /* Wait. Max clock rate is 2.5MHz, this way we come in under 1MHz */
1671 udelay(1);
1673 /* drive MDC high causing the data bit to be latched */
1674 dev->MEAR_cache |= MEAR_MDC;
1675 writel(dev->MEAR_cache, dev->base + MEAR);
1676 readl(dev->base + MEAR);
1678 /* Wait again... */
1679 udelay(1);
1682 static int ns83820_mii_read_bit(struct ns83820 *dev)
1684 int bit;
1686 /* drive MDC low, disable output */
1687 dev->MEAR_cache &= ~MEAR_MDC;
1688 dev->MEAR_cache &= ~MEAR_MDDIR;
1689 writel(dev->MEAR_cache, dev->base + MEAR);
1690 readl(dev->base + MEAR);
1692 /* Wait. Max clock rate is 2.5MHz, this way we come in under 1MHz */
1693 udelay(1);
1695 /* drive MDC high causing the data bit to be latched */
1696 bit = (readl(dev->base + MEAR) & MEAR_MDIO) ? 1 : 0;
1697 dev->MEAR_cache |= MEAR_MDC;
1698 writel(dev->MEAR_cache, dev->base + MEAR);
1700 /* Wait again... */
1701 udelay(1);
1703 return bit;
1706 static unsigned ns83820_mii_read_reg(struct ns83820 *dev, unsigned phy, unsigned reg)
1708 unsigned data = 0;
1709 int i;
1711 /* read some garbage so that we eventually sync up */
1712 for (i=0; i<64; i++)
1713 ns83820_mii_read_bit(dev);
1715 ns83820_mii_write_bit(dev, 0); /* start */
1716 ns83820_mii_write_bit(dev, 1);
1717 ns83820_mii_write_bit(dev, 1); /* opcode read */
1718 ns83820_mii_write_bit(dev, 0);
1720 /* write out the phy address: 5 bits, msb first */
1721 for (i=0; i<5; i++)
1722 ns83820_mii_write_bit(dev, phy & (0x10 >> i));
1724 /* write out the register address, 5 bits, msb first */
1725 for (i=0; i<5; i++)
1726 ns83820_mii_write_bit(dev, reg & (0x10 >> i));
1728 ns83820_mii_read_bit(dev); /* turn around cycles */
1729 ns83820_mii_read_bit(dev);
1731 /* read in the register data, 16 bits msb first */
1732 for (i=0; i<16; i++) {
1733 data <<= 1;
1734 data |= ns83820_mii_read_bit(dev);
1737 return data;
1740 static unsigned ns83820_mii_write_reg(struct ns83820 *dev, unsigned phy, unsigned reg, unsigned data)
1742 int i;
1744 /* read some garbage so that we eventually sync up */
1745 for (i=0; i<64; i++)
1746 ns83820_mii_read_bit(dev);
1748 ns83820_mii_write_bit(dev, 0); /* start */
1749 ns83820_mii_write_bit(dev, 1);
1750 ns83820_mii_write_bit(dev, 0); /* opcode read */
1751 ns83820_mii_write_bit(dev, 1);
1753 /* write out the phy address: 5 bits, msb first */
1754 for (i=0; i<5; i++)
1755 ns83820_mii_write_bit(dev, phy & (0x10 >> i));
1757 /* write out the register address, 5 bits, msb first */
1758 for (i=0; i<5; i++)
1759 ns83820_mii_write_bit(dev, reg & (0x10 >> i));
1761 ns83820_mii_read_bit(dev); /* turn around cycles */
1762 ns83820_mii_read_bit(dev);
1764 /* read in the register data, 16 bits msb first */
1765 for (i=0; i<16; i++)
1766 ns83820_mii_write_bit(dev, (data >> (15 - i)) & 1);
1768 return data;
1771 static void ns83820_probe_phy(struct net_device *ndev)
1773 struct ns83820 *dev = PRIV(ndev);
1774 static int first;
1775 int i;
1776 #define MII_PHYIDR1 0x02
1777 #define MII_PHYIDR2 0x03
1779 #if 0
1780 if (!first) {
1781 unsigned tmp;
1782 ns83820_mii_read_reg(dev, 1, 0x09);
1783 ns83820_mii_write_reg(dev, 1, 0x10, 0x0d3e);
1785 tmp = ns83820_mii_read_reg(dev, 1, 0x00);
1786 ns83820_mii_write_reg(dev, 1, 0x00, tmp | 0x8000);
1787 udelay(1300);
1788 ns83820_mii_read_reg(dev, 1, 0x09);
1790 #endif
1791 first = 1;
1793 for (i=1; i<2; i++) {
1794 int j;
1795 unsigned a, b;
1796 a = ns83820_mii_read_reg(dev, i, MII_PHYIDR1);
1797 b = ns83820_mii_read_reg(dev, i, MII_PHYIDR2);
1799 //printk("%s: phy %d: 0x%04x 0x%04x\n",
1800 // ndev->name, i, a, b);
1802 for (j=0; j<0x16; j+=4) {
1803 dprintk("%s: [0x%02x] %04x %04x %04x %04x\n",
1804 ndev->name, j,
1805 ns83820_mii_read_reg(dev, i, 0 + j),
1806 ns83820_mii_read_reg(dev, i, 1 + j),
1807 ns83820_mii_read_reg(dev, i, 2 + j),
1808 ns83820_mii_read_reg(dev, i, 3 + j)
1813 unsigned a, b;
1814 /* read firmware version: memory addr is 0x8402 and 0x8403 */
1815 ns83820_mii_write_reg(dev, 1, 0x16, 0x000d);
1816 ns83820_mii_write_reg(dev, 1, 0x1e, 0x810e);
1817 a = ns83820_mii_read_reg(dev, 1, 0x1d);
1819 ns83820_mii_write_reg(dev, 1, 0x16, 0x000d);
1820 ns83820_mii_write_reg(dev, 1, 0x1e, 0x810e);
1821 b = ns83820_mii_read_reg(dev, 1, 0x1d);
1822 dprintk("version: 0x%04x 0x%04x\n", a, b);
1825 #endif
1827 static int __devinit ns83820_init_one(struct pci_dev *pci_dev, const struct pci_device_id *id)
1829 struct net_device *ndev;
1830 struct ns83820 *dev;
1831 long addr;
1832 int err;
1833 int using_dac = 0;
1835 /* See if we can set the dma mask early on; failure is fatal. */
1836 if (sizeof(dma_addr_t) == 8 &&
1837 !pci_set_dma_mask(pci_dev, DMA_64BIT_MASK)) {
1838 using_dac = 1;
1839 } else if (!pci_set_dma_mask(pci_dev, DMA_32BIT_MASK)) {
1840 using_dac = 0;
1841 } else {
1842 dev_warn(&pci_dev->dev, "pci_set_dma_mask failed!\n");
1843 return -ENODEV;
1846 ndev = alloc_etherdev(sizeof(struct ns83820));
1847 dev = PRIV(ndev);
1848 dev->ndev = ndev;
1849 err = -ENOMEM;
1850 if (!dev)
1851 goto out;
1853 spin_lock_init(&dev->rx_info.lock);
1854 spin_lock_init(&dev->tx_lock);
1855 spin_lock_init(&dev->misc_lock);
1856 dev->pci_dev = pci_dev;
1858 SET_MODULE_OWNER(ndev);
1859 SET_NETDEV_DEV(ndev, &pci_dev->dev);
1861 INIT_WORK(&dev->tq_refill, queue_refill);
1862 tasklet_init(&dev->rx_tasklet, rx_action, (unsigned long)ndev);
1864 err = pci_enable_device(pci_dev);
1865 if (err) {
1866 dev_info(&pci_dev->dev, "pci_enable_dev failed: %d\n", err);
1867 goto out_free;
1870 pci_set_master(pci_dev);
1871 addr = pci_resource_start(pci_dev, 1);
1872 dev->base = ioremap_nocache(addr, PAGE_SIZE);
1873 dev->tx_descs = pci_alloc_consistent(pci_dev,
1874 4 * DESC_SIZE * NR_TX_DESC, &dev->tx_phy_descs);
1875 dev->rx_info.descs = pci_alloc_consistent(pci_dev,
1876 4 * DESC_SIZE * NR_RX_DESC, &dev->rx_info.phy_descs);
1877 err = -ENOMEM;
1878 if (!dev->base || !dev->tx_descs || !dev->rx_info.descs)
1879 goto out_disable;
1881 dprintk("%p: %08lx %p: %08lx\n",
1882 dev->tx_descs, (long)dev->tx_phy_descs,
1883 dev->rx_info.descs, (long)dev->rx_info.phy_descs);
1885 /* disable interrupts */
1886 writel(0, dev->base + IMR);
1887 writel(0, dev->base + IER);
1888 readl(dev->base + IER);
1890 dev->IMR_cache = 0;
1892 err = request_irq(pci_dev->irq, ns83820_irq, IRQF_SHARED,
1893 DRV_NAME, ndev);
1894 if (err) {
1895 dev_info(&pci_dev->dev, "unable to register irq %d, err %d\n",
1896 pci_dev->irq, err);
1897 goto out_disable;
1901 * FIXME: we are holding rtnl_lock() over obscenely long area only
1902 * because some of the setup code uses dev->name. It's Wrong(tm) -
1903 * we should be using driver-specific names for all that stuff.
1904 * For now that will do, but we really need to come back and kill
1905 * most of the dev_alloc_name() users later.
1907 rtnl_lock();
1908 err = dev_alloc_name(ndev, ndev->name);
1909 if (err < 0) {
1910 dev_info(&pci_dev->dev, "unable to get netdev name: %d\n", err);
1911 goto out_free_irq;
1914 printk("%s: ns83820.c: 0x22c: %08x, subsystem: %04x:%04x\n",
1915 ndev->name, le32_to_cpu(readl(dev->base + 0x22c)),
1916 pci_dev->subsystem_vendor, pci_dev->subsystem_device);
1918 ndev->open = ns83820_open;
1919 ndev->stop = ns83820_stop;
1920 ndev->hard_start_xmit = ns83820_hard_start_xmit;
1921 ndev->get_stats = ns83820_get_stats;
1922 ndev->change_mtu = ns83820_change_mtu;
1923 ndev->set_multicast_list = ns83820_set_multicast;
1924 SET_ETHTOOL_OPS(ndev, &ops);
1925 ndev->tx_timeout = ns83820_tx_timeout;
1926 ndev->watchdog_timeo = 5 * HZ;
1927 pci_set_drvdata(pci_dev, ndev);
1929 ns83820_do_reset(dev, CR_RST);
1931 /* Must reset the ram bist before running it */
1932 writel(PTSCR_RBIST_RST, dev->base + PTSCR);
1933 ns83820_run_bist(ndev, "sram bist", PTSCR_RBIST_EN,
1934 PTSCR_RBIST_DONE, PTSCR_RBIST_FAIL);
1935 ns83820_run_bist(ndev, "eeprom bist", PTSCR_EEBIST_EN, 0,
1936 PTSCR_EEBIST_FAIL);
1937 ns83820_run_bist(ndev, "eeprom load", PTSCR_EELOAD_EN, 0, 0);
1939 /* I love config registers */
1940 dev->CFG_cache = readl(dev->base + CFG);
1942 if ((dev->CFG_cache & CFG_PCI64_DET)) {
1943 printk(KERN_INFO "%s: detected 64 bit PCI data bus.\n",
1944 ndev->name);
1945 /*dev->CFG_cache |= CFG_DATA64_EN;*/
1946 if (!(dev->CFG_cache & CFG_DATA64_EN))
1947 printk(KERN_INFO "%s: EEPROM did not enable 64 bit bus. Disabled.\n",
1948 ndev->name);
1949 } else
1950 dev->CFG_cache &= ~(CFG_DATA64_EN);
1952 dev->CFG_cache &= (CFG_TBI_EN | CFG_MRM_DIS | CFG_MWI_DIS |
1953 CFG_T64ADDR | CFG_DATA64_EN | CFG_EXT_125 |
1954 CFG_M64ADDR);
1955 dev->CFG_cache |= CFG_PINT_DUPSTS | CFG_PINT_LNKSTS | CFG_PINT_SPDSTS |
1956 CFG_EXTSTS_EN | CFG_EXD | CFG_PESEL;
1957 dev->CFG_cache |= CFG_REQALG;
1958 dev->CFG_cache |= CFG_POW;
1959 dev->CFG_cache |= CFG_TMRTEST;
1961 /* When compiled with 64 bit addressing, we must always enable
1962 * the 64 bit descriptor format.
1964 if (sizeof(dma_addr_t) == 8)
1965 dev->CFG_cache |= CFG_M64ADDR;
1966 if (using_dac)
1967 dev->CFG_cache |= CFG_T64ADDR;
1969 /* Big endian mode does not seem to do what the docs suggest */
1970 dev->CFG_cache &= ~CFG_BEM;
1972 /* setup optical transceiver if we have one */
1973 if (dev->CFG_cache & CFG_TBI_EN) {
1974 printk(KERN_INFO "%s: enabling optical transceiver\n",
1975 ndev->name);
1976 writel(readl(dev->base + GPIOR) | 0x3e8, dev->base + GPIOR);
1978 /* setup auto negotiation feature advertisement */
1979 writel(readl(dev->base + TANAR)
1980 | TANAR_HALF_DUP | TANAR_FULL_DUP,
1981 dev->base + TANAR);
1983 /* start auto negotiation */
1984 writel(TBICR_MR_AN_ENABLE | TBICR_MR_RESTART_AN,
1985 dev->base + TBICR);
1986 writel(TBICR_MR_AN_ENABLE, dev->base + TBICR);
1987 dev->linkstate = LINK_AUTONEGOTIATE;
1989 dev->CFG_cache |= CFG_MODE_1000;
1992 writel(dev->CFG_cache, dev->base + CFG);
1993 dprintk("CFG: %08x\n", dev->CFG_cache);
1995 if (reset_phy) {
1996 printk(KERN_INFO "%s: resetting phy\n", ndev->name);
1997 writel(dev->CFG_cache | CFG_PHY_RST, dev->base + CFG);
1998 msleep(10);
1999 writel(dev->CFG_cache, dev->base + CFG);
2002 #if 0 /* Huh? This sets the PCI latency register. Should be done via
2003 * the PCI layer. FIXME.
2005 if (readl(dev->base + SRR))
2006 writel(readl(dev->base+0x20c) | 0xfe00, dev->base + 0x20c);
2007 #endif
2009 /* Note! The DMA burst size interacts with packet
2010 * transmission, such that the largest packet that
2011 * can be transmitted is 8192 - FLTH - burst size.
2012 * If only the transmit fifo was larger...
2014 /* Ramit : 1024 DMA is not a good idea, it ends up banging
2015 * some DELL and COMPAQ SMP systems */
2016 writel(TXCFG_CSI | TXCFG_HBI | TXCFG_ATP | TXCFG_MXDMA512
2017 | ((1600 / 32) * 0x100),
2018 dev->base + TXCFG);
2020 /* Flush the interrupt holdoff timer */
2021 writel(0x000, dev->base + IHR);
2022 writel(0x100, dev->base + IHR);
2023 writel(0x000, dev->base + IHR);
2025 /* Set Rx to full duplex, don't accept runt, errored, long or length
2026 * range errored packets. Use 512 byte DMA.
2028 /* Ramit : 1024 DMA is not a good idea, it ends up banging
2029 * some DELL and COMPAQ SMP systems
2030 * Turn on ALP, only we are accpeting Jumbo Packets */
2031 writel(RXCFG_AEP | RXCFG_ARP | RXCFG_AIRL | RXCFG_RX_FD
2032 | RXCFG_STRIPCRC
2033 //| RXCFG_ALP
2034 | (RXCFG_MXDMA512) | 0, dev->base + RXCFG);
2036 /* Disable priority queueing */
2037 writel(0, dev->base + PQCR);
2039 /* Enable IP checksum validation and detetion of VLAN headers.
2040 * Note: do not set the reject options as at least the 0x102
2041 * revision of the chip does not properly accept IP fragments
2042 * at least for UDP.
2044 /* Ramit : Be sure to turn on RXCFG_ARP if VLAN's are enabled, since
2045 * the MAC it calculates the packetsize AFTER stripping the VLAN
2046 * header, and if a VLAN Tagged packet of 64 bytes is received (like
2047 * a ping with a VLAN header) then the card, strips the 4 byte VLAN
2048 * tag and then checks the packet size, so if RXCFG_ARP is not enabled,
2049 * it discrards it!. These guys......
2050 * also turn on tag stripping if hardware acceleration is enabled
2052 #ifdef NS83820_VLAN_ACCEL_SUPPORT
2053 #define VRCR_INIT_VALUE (VRCR_IPEN|VRCR_VTDEN|VRCR_VTREN)
2054 #else
2055 #define VRCR_INIT_VALUE (VRCR_IPEN|VRCR_VTDEN)
2056 #endif
2057 writel(VRCR_INIT_VALUE, dev->base + VRCR);
2059 /* Enable per-packet TCP/UDP/IP checksumming
2060 * and per packet vlan tag insertion if
2061 * vlan hardware acceleration is enabled
2063 #ifdef NS83820_VLAN_ACCEL_SUPPORT
2064 #define VTCR_INIT_VALUE (VTCR_PPCHK|VTCR_VPPTI)
2065 #else
2066 #define VTCR_INIT_VALUE VTCR_PPCHK
2067 #endif
2068 writel(VTCR_INIT_VALUE, dev->base + VTCR);
2070 /* Ramit : Enable async and sync pause frames */
2071 /* writel(0, dev->base + PCR); */
2072 writel((PCR_PS_MCAST | PCR_PS_DA | PCR_PSEN | PCR_FFLO_4K |
2073 PCR_FFHI_8K | PCR_STLO_4 | PCR_STHI_8 | PCR_PAUSE_CNT),
2074 dev->base + PCR);
2076 /* Disable Wake On Lan */
2077 writel(0, dev->base + WCSR);
2079 ns83820_getmac(dev, ndev->dev_addr);
2081 /* Yes, we support dumb IP checksum on transmit */
2082 ndev->features |= NETIF_F_SG;
2083 ndev->features |= NETIF_F_IP_CSUM;
2085 #ifdef NS83820_VLAN_ACCEL_SUPPORT
2086 /* We also support hardware vlan acceleration */
2087 ndev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
2088 ndev->vlan_rx_register = ns83820_vlan_rx_register;
2089 ndev->vlan_rx_kill_vid = ns83820_vlan_rx_kill_vid;
2090 #endif
2092 if (using_dac) {
2093 printk(KERN_INFO "%s: using 64 bit addressing.\n",
2094 ndev->name);
2095 ndev->features |= NETIF_F_HIGHDMA;
2098 printk(KERN_INFO "%s: ns83820 v" VERSION ": DP83820 v%u.%u: %02x:%02x:%02x:%02x:%02x:%02x io=0x%08lx irq=%d f=%s\n",
2099 ndev->name,
2100 (unsigned)readl(dev->base + SRR) >> 8,
2101 (unsigned)readl(dev->base + SRR) & 0xff,
2102 ndev->dev_addr[0], ndev->dev_addr[1],
2103 ndev->dev_addr[2], ndev->dev_addr[3],
2104 ndev->dev_addr[4], ndev->dev_addr[5],
2105 addr, pci_dev->irq,
2106 (ndev->features & NETIF_F_HIGHDMA) ? "h,sg" : "sg"
2109 #ifdef PHY_CODE_IS_FINISHED
2110 ns83820_probe_phy(ndev);
2111 #endif
2113 err = register_netdevice(ndev);
2114 if (err) {
2115 printk(KERN_INFO "ns83820: unable to register netdev: %d\n", err);
2116 goto out_cleanup;
2118 rtnl_unlock();
2120 return 0;
2122 out_cleanup:
2123 writel(0, dev->base + IMR); /* paranoia */
2124 writel(0, dev->base + IER);
2125 readl(dev->base + IER);
2126 out_free_irq:
2127 rtnl_unlock();
2128 free_irq(pci_dev->irq, ndev);
2129 out_disable:
2130 if (dev->base)
2131 iounmap(dev->base);
2132 pci_free_consistent(pci_dev, 4 * DESC_SIZE * NR_TX_DESC, dev->tx_descs, dev->tx_phy_descs);
2133 pci_free_consistent(pci_dev, 4 * DESC_SIZE * NR_RX_DESC, dev->rx_info.descs, dev->rx_info.phy_descs);
2134 pci_disable_device(pci_dev);
2135 out_free:
2136 free_netdev(ndev);
2137 pci_set_drvdata(pci_dev, NULL);
2138 out:
2139 return err;
2142 static void __devexit ns83820_remove_one(struct pci_dev *pci_dev)
2144 struct net_device *ndev = pci_get_drvdata(pci_dev);
2145 struct ns83820 *dev = PRIV(ndev); /* ok even if NULL */
2147 if (!ndev) /* paranoia */
2148 return;
2150 writel(0, dev->base + IMR); /* paranoia */
2151 writel(0, dev->base + IER);
2152 readl(dev->base + IER);
2154 unregister_netdev(ndev);
2155 free_irq(dev->pci_dev->irq, ndev);
2156 iounmap(dev->base);
2157 pci_free_consistent(dev->pci_dev, 4 * DESC_SIZE * NR_TX_DESC,
2158 dev->tx_descs, dev->tx_phy_descs);
2159 pci_free_consistent(dev->pci_dev, 4 * DESC_SIZE * NR_RX_DESC,
2160 dev->rx_info.descs, dev->rx_info.phy_descs);
2161 pci_disable_device(dev->pci_dev);
2162 free_netdev(ndev);
2163 pci_set_drvdata(pci_dev, NULL);
2166 static struct pci_device_id ns83820_pci_tbl[] = {
2167 { 0x100b, 0x0022, PCI_ANY_ID, PCI_ANY_ID, 0, .driver_data = 0, },
2168 { 0, },
2171 static struct pci_driver driver = {
2172 .name = "ns83820",
2173 .id_table = ns83820_pci_tbl,
2174 .probe = ns83820_init_one,
2175 .remove = __devexit_p(ns83820_remove_one),
2176 #if 0 /* FIXME: implement */
2177 .suspend = ,
2178 .resume = ,
2179 #endif
2183 static int __init ns83820_init(void)
2185 printk(KERN_INFO "ns83820.c: National Semiconductor DP83820 10/100/1000 driver.\n");
2186 return pci_register_driver(&driver);
2189 static void __exit ns83820_exit(void)
2191 pci_unregister_driver(&driver);
2194 MODULE_AUTHOR("Benjamin LaHaise <bcrl@kvack.org>");
2195 MODULE_DESCRIPTION("National Semiconductor DP83820 10/100/1000 driver");
2196 MODULE_LICENSE("GPL");
2198 MODULE_DEVICE_TABLE(pci, ns83820_pci_tbl);
2200 module_param(lnksts, int, 0);
2201 MODULE_PARM_DESC(lnksts, "Polarity of LNKSTS bit");
2203 module_param(ihr, int, 0);
2204 MODULE_PARM_DESC(ihr, "Time in 100 us increments to delay interrupts (range 0-127)");
2206 module_param(reset_phy, int, 0);
2207 MODULE_PARM_DESC(reset_phy, "Set to 1 to reset the PHY on startup");
2209 module_init(ns83820_init);
2210 module_exit(ns83820_exit);