blob: 4daafe3033589c49c6637baf95c004e20d5982fc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* pcnet32.c: An AMD PCnet32 ethernet driver for linux. */
2/*
3 * Copyright 1996-1999 Thomas Bogendoerfer
4 *
5 * Derived from the lance driver written 1993,1994,1995 by Donald Becker.
6 *
7 * Copyright 1993 United States Government as represented by the
8 * Director, National Security Agency.
9 *
10 * This software may be used and distributed according to the terms
11 * of the GNU General Public License, incorporated herein by reference.
12 *
13 * This driver is for PCnet32 and PCnetPCI based ethercards
14 */
15/**************************************************************************
16 * 23 Oct, 2000.
17 * Fixed a few bugs, related to running the controller in 32bit mode.
18 *
19 * Carsten Langgaard, carstenl@mips.com
20 * Copyright (C) 2000 MIPS Technologies, Inc. All rights reserved.
21 *
22 *************************************************************************/
23
24#define DRV_NAME "pcnet32"
Don Fryac62ef02006-03-20 15:26:03 -080025#define DRV_VERSION "1.32"
26#define DRV_RELDATE "18.Mar.2006"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#define PFX DRV_NAME ": "
28
Jeff Garzik4a5e8e22006-03-21 16:15:44 -050029static const char *const version =
30 DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " tsbogend@alpha.franken.de\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <linux/module.h>
33#include <linux/kernel.h>
34#include <linux/string.h>
35#include <linux/errno.h>
36#include <linux/ioport.h>
37#include <linux/slab.h>
38#include <linux/interrupt.h>
39#include <linux/pci.h>
40#include <linux/delay.h>
41#include <linux/init.h>
42#include <linux/ethtool.h>
43#include <linux/mii.h>
44#include <linux/crc32.h>
45#include <linux/netdevice.h>
46#include <linux/etherdevice.h>
47#include <linux/skbuff.h>
48#include <linux/spinlock.h>
49#include <linux/moduleparam.h>
50#include <linux/bitops.h>
51
52#include <asm/dma.h>
53#include <asm/io.h>
54#include <asm/uaccess.h>
55#include <asm/irq.h>
56
57/*
58 * PCI device identifiers for "new style" Linux PCI Device Drivers
59 */
60static struct pci_device_id pcnet32_pci_tbl[] = {
Don Fryf2622a22006-06-29 13:52:58 -070061 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE_HOME), },
62 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE), },
Jeff Garzik4a5e8e22006-03-21 16:15:44 -050063
64 /*
65 * Adapters that were sold with IBM's RS/6000 or pSeries hardware have
66 * the incorrect vendor id.
67 */
Don Fryf2622a22006-06-29 13:52:58 -070068 { PCI_DEVICE(PCI_VENDOR_ID_TRIDENT, PCI_DEVICE_ID_AMD_LANCE),
69 .class = (PCI_CLASS_NETWORK_ETHERNET << 8), .class_mask = 0xffff00, },
Jeff Garzik4a5e8e22006-03-21 16:15:44 -050070
71 { } /* terminate list */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
Jeff Garzik4a5e8e22006-03-21 16:15:44 -050074MODULE_DEVICE_TABLE(pci, pcnet32_pci_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76static int cards_found;
77
78/*
79 * VLB I/O addresses
80 */
81static unsigned int pcnet32_portlist[] __initdata =
Jeff Garzik4a5e8e22006-03-21 16:15:44 -050082 { 0x300, 0x320, 0x340, 0x360, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84static int pcnet32_debug = 0;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -050085static int tx_start = 1; /* Mapping -- 0:20, 1:64, 2:128, 3:~220 (depends on chip vers) */
86static int pcnet32vlb; /* check for VLB cards ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88static struct net_device *pcnet32_dev;
89
90static int max_interrupt_work = 2;
91static int rx_copybreak = 200;
92
93#define PCNET32_PORT_AUI 0x00
94#define PCNET32_PORT_10BT 0x01
95#define PCNET32_PORT_GPSI 0x02
96#define PCNET32_PORT_MII 0x03
97
98#define PCNET32_PORT_PORTSEL 0x03
99#define PCNET32_PORT_ASEL 0x04
100#define PCNET32_PORT_100 0x40
101#define PCNET32_PORT_FD 0x80
102
103#define PCNET32_DMA_MASK 0xffffffff
104
105#define PCNET32_WATCHDOG_TIMEOUT (jiffies + (2 * HZ))
106#define PCNET32_BLINK_TIMEOUT (jiffies + (HZ/4))
107
108/*
109 * table to translate option values from tulip
110 * to internal options
111 */
Arjan van de Venf71e1302006-03-03 21:33:57 -0500112static const unsigned char options_mapping[] = {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500113 PCNET32_PORT_ASEL, /* 0 Auto-select */
114 PCNET32_PORT_AUI, /* 1 BNC/AUI */
115 PCNET32_PORT_AUI, /* 2 AUI/BNC */
116 PCNET32_PORT_ASEL, /* 3 not supported */
117 PCNET32_PORT_10BT | PCNET32_PORT_FD, /* 4 10baseT-FD */
118 PCNET32_PORT_ASEL, /* 5 not supported */
119 PCNET32_PORT_ASEL, /* 6 not supported */
120 PCNET32_PORT_ASEL, /* 7 not supported */
121 PCNET32_PORT_ASEL, /* 8 not supported */
122 PCNET32_PORT_MII, /* 9 MII 10baseT */
123 PCNET32_PORT_MII | PCNET32_PORT_FD, /* 10 MII 10baseT-FD */
124 PCNET32_PORT_MII, /* 11 MII (autosel) */
125 PCNET32_PORT_10BT, /* 12 10BaseT */
126 PCNET32_PORT_MII | PCNET32_PORT_100, /* 13 MII 100BaseTx */
127 /* 14 MII 100BaseTx-FD */
128 PCNET32_PORT_MII | PCNET32_PORT_100 | PCNET32_PORT_FD,
129 PCNET32_PORT_ASEL /* 15 not supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130};
131
132static const char pcnet32_gstrings_test[][ETH_GSTRING_LEN] = {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500133 "Loopback test (offline)"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#define PCNET32_TEST_LEN (sizeof(pcnet32_gstrings_test) / ETH_GSTRING_LEN)
137
Don Fryac62ef02006-03-20 15:26:03 -0800138#define PCNET32_NUM_REGS 136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500140#define MAX_UNITS 8 /* More are supported, limit only on options */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static int options[MAX_UNITS];
142static int full_duplex[MAX_UNITS];
143static int homepna[MAX_UNITS];
144
145/*
146 * Theory of Operation
147 *
148 * This driver uses the same software structure as the normal lance
149 * driver. So look for a verbose description in lance.c. The differences
150 * to the normal lance driver is the use of the 32bit mode of PCnet32
151 * and PCnetPCI chips. Because these chips are 32bit chips, there is no
152 * 16MB limitation and we don't need bounce buffers.
153 */
154
155/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * Set the number of Tx and Rx buffers, using Log_2(# buffers).
157 * Reasonable default values are 4 Tx buffers, and 16 Rx buffers.
158 * That translates to 2 (4 == 2^^2) and 4 (16 == 2^^4).
159 */
160#ifndef PCNET32_LOG_TX_BUFFERS
Hubert WS Lineabf0412005-09-14 11:39:25 -0700161#define PCNET32_LOG_TX_BUFFERS 4
162#define PCNET32_LOG_RX_BUFFERS 5
163#define PCNET32_LOG_MAX_TX_BUFFERS 9 /* 2^9 == 512 */
164#define PCNET32_LOG_MAX_RX_BUFFERS 9
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#endif
166
167#define TX_RING_SIZE (1 << (PCNET32_LOG_TX_BUFFERS))
Hubert WS Lineabf0412005-09-14 11:39:25 -0700168#define TX_MAX_RING_SIZE (1 << (PCNET32_LOG_MAX_TX_BUFFERS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170#define RX_RING_SIZE (1 << (PCNET32_LOG_RX_BUFFERS))
Hubert WS Lineabf0412005-09-14 11:39:25 -0700171#define RX_MAX_RING_SIZE (1 << (PCNET32_LOG_MAX_RX_BUFFERS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173#define PKT_BUF_SZ 1544
174
175/* Offsets from base I/O address. */
176#define PCNET32_WIO_RDP 0x10
177#define PCNET32_WIO_RAP 0x12
178#define PCNET32_WIO_RESET 0x14
179#define PCNET32_WIO_BDP 0x16
180
181#define PCNET32_DWIO_RDP 0x10
182#define PCNET32_DWIO_RAP 0x14
183#define PCNET32_DWIO_RESET 0x18
184#define PCNET32_DWIO_BDP 0x1C
185
186#define PCNET32_TOTAL_SIZE 0x20
187
Don Fry06c87852006-06-29 13:54:38 -0700188#define CSR0 0
189#define CSR0_INIT 0x1
190#define CSR0_START 0x2
191#define CSR0_STOP 0x4
192#define CSR0_TXPOLL 0x8
193#define CSR0_INTEN 0x40
194#define CSR0_IDON 0x0100
195#define CSR0_NORMAL (CSR0_START | CSR0_INTEN)
196#define PCNET32_INIT_LOW 1
197#define PCNET32_INIT_HIGH 2
198#define CSR3 3
199#define CSR4 4
200#define CSR5 5
201#define CSR5_SUSPEND 0x0001
202#define CSR15 15
203#define PCNET32_MC_FILTER 8
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/* The PCNET32 Rx and Tx ring descriptors. */
206struct pcnet32_rx_head {
Jeff Garzik0b5bf222006-03-21 16:22:47 -0500207 u32 base;
208 s16 buf_length;
209 s16 status;
210 u32 msg_length;
211 u32 reserved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212};
213
214struct pcnet32_tx_head {
Jeff Garzik0b5bf222006-03-21 16:22:47 -0500215 u32 base;
216 s16 length;
217 s16 status;
218 u32 misc;
219 u32 reserved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220};
221
222/* The PCNET32 32-Bit initialization block, described in databook. */
223struct pcnet32_init_block {
Jeff Garzik0b5bf222006-03-21 16:22:47 -0500224 u16 mode;
225 u16 tlen_rlen;
226 u8 phys_addr[6];
227 u16 reserved;
228 u32 filter[2];
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500229 /* Receive and transmit ring base, along with extra bits. */
Jeff Garzik0b5bf222006-03-21 16:22:47 -0500230 u32 rx_ring;
231 u32 tx_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232};
233
234/* PCnet32 access functions */
235struct pcnet32_access {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500236 u16 (*read_csr) (unsigned long, int);
237 void (*write_csr) (unsigned long, int, u16);
238 u16 (*read_bcr) (unsigned long, int);
239 void (*write_bcr) (unsigned long, int, u16);
240 u16 (*read_rap) (unsigned long);
241 void (*write_rap) (unsigned long, u16);
242 void (*reset) (unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243};
244
245/*
Hubert WS Lin76209922005-09-14 11:39:27 -0700246 * The first field of pcnet32_private is read by the ethernet device
247 * so the structure should be allocated using pci_alloc_consistent().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 */
249struct pcnet32_private {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500250 struct pcnet32_init_block init_block;
251 /* The Tx and Rx ring entries must be aligned on 16-byte boundaries in 32bit mode. */
Jeff Garzik0b5bf222006-03-21 16:22:47 -0500252 struct pcnet32_rx_head *rx_ring;
253 struct pcnet32_tx_head *tx_ring;
254 dma_addr_t dma_addr;/* DMA address of beginning of this
255 object, returned by pci_alloc_consistent */
256 struct pci_dev *pci_dev;
257 const char *name;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500258 /* The saved address of a sent-in-place packet/buffer, for skfree(). */
Jeff Garzik0b5bf222006-03-21 16:22:47 -0500259 struct sk_buff **tx_skbuff;
260 struct sk_buff **rx_skbuff;
261 dma_addr_t *tx_dma_addr;
262 dma_addr_t *rx_dma_addr;
263 struct pcnet32_access a;
264 spinlock_t lock; /* Guard lock */
265 unsigned int cur_rx, cur_tx; /* The next free ring entry */
266 unsigned int rx_ring_size; /* current rx ring size */
267 unsigned int tx_ring_size; /* current tx ring size */
268 unsigned int rx_mod_mask; /* rx ring modular mask */
269 unsigned int tx_mod_mask; /* tx ring modular mask */
270 unsigned short rx_len_bits;
271 unsigned short tx_len_bits;
272 dma_addr_t rx_ring_dma_addr;
273 dma_addr_t tx_ring_dma_addr;
274 unsigned int dirty_rx, /* ring entries to be freed. */
275 dirty_tx;
276
277 struct net_device_stats stats;
278 char tx_full;
279 char phycount; /* number of phys found */
280 int options;
281 unsigned int shared_irq:1, /* shared irq possible */
282 dxsuflo:1, /* disable transmit stop on uflo */
283 mii:1; /* mii port available */
284 struct net_device *next;
285 struct mii_if_info mii_if;
286 struct timer_list watchdog_timer;
287 struct timer_list blink_timer;
288 u32 msg_enable; /* debug message level */
Don Fryac62ef02006-03-20 15:26:03 -0800289
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500290 /* each bit indicates an available PHY */
Jeff Garzik0b5bf222006-03-21 16:22:47 -0500291 u32 phymask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292};
293
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500294static int pcnet32_probe_pci(struct pci_dev *, const struct pci_device_id *);
295static int pcnet32_probe1(unsigned long, int, struct pci_dev *);
296static int pcnet32_open(struct net_device *);
297static int pcnet32_init_ring(struct net_device *);
298static int pcnet32_start_xmit(struct sk_buff *, struct net_device *);
299static int pcnet32_rx(struct net_device *);
300static void pcnet32_tx_timeout(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301static irqreturn_t pcnet32_interrupt(int, void *, struct pt_regs *);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500302static int pcnet32_close(struct net_device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303static struct net_device_stats *pcnet32_get_stats(struct net_device *);
304static void pcnet32_load_multicast(struct net_device *dev);
305static void pcnet32_set_multicast_list(struct net_device *);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500306static int pcnet32_ioctl(struct net_device *, struct ifreq *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307static void pcnet32_watchdog(struct net_device *);
308static int mdio_read(struct net_device *dev, int phy_id, int reg_num);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500309static void mdio_write(struct net_device *dev, int phy_id, int reg_num,
310 int val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311static void pcnet32_restart(struct net_device *dev, unsigned int csr0_bits);
312static void pcnet32_ethtool_test(struct net_device *dev,
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500313 struct ethtool_test *eth_test, u64 * data);
314static int pcnet32_loopback_test(struct net_device *dev, uint64_t * data1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315static int pcnet32_phys_id(struct net_device *dev, u32 data);
316static void pcnet32_led_blink_callback(struct net_device *dev);
317static int pcnet32_get_regs_len(struct net_device *dev);
318static void pcnet32_get_regs(struct net_device *dev, struct ethtool_regs *regs,
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500319 void *ptr);
Don Fry1bcd3152005-04-29 14:51:17 -0700320static void pcnet32_purge_tx_ring(struct net_device *dev);
Don Frya88c8442005-11-01 12:04:33 -0800321static int pcnet32_alloc_ring(struct net_device *dev, char *name);
Hubert WS Lineabf0412005-09-14 11:39:25 -0700322static void pcnet32_free_ring(struct net_device *dev);
Don Fryac62ef02006-03-20 15:26:03 -0800323static void pcnet32_check_media(struct net_device *dev, int verbose);
Hubert WS Lineabf0412005-09-14 11:39:25 -0700324
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500325static u16 pcnet32_wio_read_csr(unsigned long addr, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500327 outw(index, addr + PCNET32_WIO_RAP);
328 return inw(addr + PCNET32_WIO_RDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500331static void pcnet32_wio_write_csr(unsigned long addr, int index, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500333 outw(index, addr + PCNET32_WIO_RAP);
334 outw(val, addr + PCNET32_WIO_RDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500337static u16 pcnet32_wio_read_bcr(unsigned long addr, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500339 outw(index, addr + PCNET32_WIO_RAP);
340 return inw(addr + PCNET32_WIO_BDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500343static void pcnet32_wio_write_bcr(unsigned long addr, int index, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500345 outw(index, addr + PCNET32_WIO_RAP);
346 outw(val, addr + PCNET32_WIO_BDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500349static u16 pcnet32_wio_read_rap(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500351 return inw(addr + PCNET32_WIO_RAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500354static void pcnet32_wio_write_rap(unsigned long addr, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500356 outw(val, addr + PCNET32_WIO_RAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500359static void pcnet32_wio_reset(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500361 inw(addr + PCNET32_WIO_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500364static int pcnet32_wio_check(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500366 outw(88, addr + PCNET32_WIO_RAP);
367 return (inw(addr + PCNET32_WIO_RAP) == 88);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
370static struct pcnet32_access pcnet32_wio = {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500371 .read_csr = pcnet32_wio_read_csr,
372 .write_csr = pcnet32_wio_write_csr,
373 .read_bcr = pcnet32_wio_read_bcr,
374 .write_bcr = pcnet32_wio_write_bcr,
375 .read_rap = pcnet32_wio_read_rap,
376 .write_rap = pcnet32_wio_write_rap,
377 .reset = pcnet32_wio_reset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378};
379
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500380static u16 pcnet32_dwio_read_csr(unsigned long addr, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500382 outl(index, addr + PCNET32_DWIO_RAP);
383 return (inl(addr + PCNET32_DWIO_RDP) & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500386static void pcnet32_dwio_write_csr(unsigned long addr, int index, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500388 outl(index, addr + PCNET32_DWIO_RAP);
389 outl(val, addr + PCNET32_DWIO_RDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500392static u16 pcnet32_dwio_read_bcr(unsigned long addr, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500394 outl(index, addr + PCNET32_DWIO_RAP);
395 return (inl(addr + PCNET32_DWIO_BDP) & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500398static void pcnet32_dwio_write_bcr(unsigned long addr, int index, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500400 outl(index, addr + PCNET32_DWIO_RAP);
401 outl(val, addr + PCNET32_DWIO_BDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500404static u16 pcnet32_dwio_read_rap(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500406 return (inl(addr + PCNET32_DWIO_RAP) & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500409static void pcnet32_dwio_write_rap(unsigned long addr, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500411 outl(val, addr + PCNET32_DWIO_RAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500414static void pcnet32_dwio_reset(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500416 inl(addr + PCNET32_DWIO_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500419static int pcnet32_dwio_check(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500421 outl(88, addr + PCNET32_DWIO_RAP);
422 return ((inl(addr + PCNET32_DWIO_RAP) & 0xffff) == 88);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
425static struct pcnet32_access pcnet32_dwio = {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500426 .read_csr = pcnet32_dwio_read_csr,
427 .write_csr = pcnet32_dwio_write_csr,
428 .read_bcr = pcnet32_dwio_read_bcr,
429 .write_bcr = pcnet32_dwio_write_bcr,
430 .read_rap = pcnet32_dwio_read_rap,
431 .write_rap = pcnet32_dwio_write_rap,
432 .reset = pcnet32_dwio_reset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433};
434
Don Fry06c87852006-06-29 13:54:38 -0700435static void pcnet32_netif_stop(struct net_device *dev)
436{
437 dev->trans_start = jiffies;
438 netif_poll_disable(dev);
439 netif_tx_disable(dev);
440}
441
442static void pcnet32_netif_start(struct net_device *dev)
443{
444 netif_wake_queue(dev);
445 netif_poll_enable(dev);
446}
447
448/*
449 * Allocate space for the new sized tx ring.
450 * Free old resources
451 * Save new resources.
452 * Any failure keeps old resources.
453 * Must be called with lp->lock held.
454 */
455static void pcnet32_realloc_tx_ring(struct net_device *dev,
456 struct pcnet32_private *lp,
457 unsigned int size)
458{
459 dma_addr_t new_ring_dma_addr;
460 dma_addr_t *new_dma_addr_list;
461 struct pcnet32_tx_head *new_tx_ring;
462 struct sk_buff **new_skb_list;
463
464 pcnet32_purge_tx_ring(dev);
465
466 new_tx_ring = pci_alloc_consistent(lp->pci_dev,
467 sizeof(struct pcnet32_tx_head) *
468 (1 << size),
469 &new_ring_dma_addr);
470 if (new_tx_ring == NULL) {
471 if (netif_msg_drv(lp))
472 printk("\n" KERN_ERR
473 "%s: Consistent memory allocation failed.\n",
474 dev->name);
475 return;
476 }
477 memset(new_tx_ring, 0, sizeof(struct pcnet32_tx_head) * (1 << size));
478
479 new_dma_addr_list = kcalloc((1 << size), sizeof(dma_addr_t),
480 GFP_ATOMIC);
481 if (!new_dma_addr_list) {
482 if (netif_msg_drv(lp))
483 printk("\n" KERN_ERR
484 "%s: Memory allocation failed.\n", dev->name);
485 goto free_new_tx_ring;
486 }
487
488 new_skb_list = kcalloc((1 << size), sizeof(struct sk_buff *),
489 GFP_ATOMIC);
490 if (!new_skb_list) {
491 if (netif_msg_drv(lp))
492 printk("\n" KERN_ERR
493 "%s: Memory allocation failed.\n", dev->name);
494 goto free_new_lists;
495 }
496
497 kfree(lp->tx_skbuff);
498 kfree(lp->tx_dma_addr);
499 pci_free_consistent(lp->pci_dev,
500 sizeof(struct pcnet32_tx_head) *
501 lp->tx_ring_size, lp->tx_ring,
502 lp->tx_ring_dma_addr);
503
504 lp->tx_ring_size = (1 << size);
505 lp->tx_mod_mask = lp->tx_ring_size - 1;
506 lp->tx_len_bits = (size << 12);
507 lp->tx_ring = new_tx_ring;
508 lp->tx_ring_dma_addr = new_ring_dma_addr;
509 lp->tx_dma_addr = new_dma_addr_list;
510 lp->tx_skbuff = new_skb_list;
511 return;
512
513 free_new_lists:
514 kfree(new_dma_addr_list);
515 free_new_tx_ring:
516 pci_free_consistent(lp->pci_dev,
517 sizeof(struct pcnet32_tx_head) *
518 (1 << size),
519 new_tx_ring,
520 new_ring_dma_addr);
521 return;
522}
523
524/*
525 * Allocate space for the new sized rx ring.
526 * Re-use old receive buffers.
527 * alloc extra buffers
528 * free unneeded buffers
529 * free unneeded buffers
530 * Save new resources.
531 * Any failure keeps old resources.
532 * Must be called with lp->lock held.
533 */
534static void pcnet32_realloc_rx_ring(struct net_device *dev,
535 struct pcnet32_private *lp,
536 unsigned int size)
537{
538 dma_addr_t new_ring_dma_addr;
539 dma_addr_t *new_dma_addr_list;
540 struct pcnet32_rx_head *new_rx_ring;
541 struct sk_buff **new_skb_list;
542 int new, overlap;
543
544 new_rx_ring = pci_alloc_consistent(lp->pci_dev,
545 sizeof(struct pcnet32_rx_head) *
546 (1 << size),
547 &new_ring_dma_addr);
548 if (new_rx_ring == NULL) {
549 if (netif_msg_drv(lp))
550 printk("\n" KERN_ERR
551 "%s: Consistent memory allocation failed.\n",
552 dev->name);
553 return;
554 }
555 memset(new_rx_ring, 0, sizeof(struct pcnet32_rx_head) * (1 << size));
556
557 new_dma_addr_list = kcalloc((1 << size), sizeof(dma_addr_t),
558 GFP_ATOMIC);
559 if (!new_dma_addr_list) {
560 if (netif_msg_drv(lp))
561 printk("\n" KERN_ERR
562 "%s: Memory allocation failed.\n", dev->name);
563 goto free_new_rx_ring;
564 }
565
566 new_skb_list = kcalloc((1 << size), sizeof(struct sk_buff *),
567 GFP_ATOMIC);
568 if (!new_skb_list) {
569 if (netif_msg_drv(lp))
570 printk("\n" KERN_ERR
571 "%s: Memory allocation failed.\n", dev->name);
572 goto free_new_lists;
573 }
574
575 /* first copy the current receive buffers */
576 overlap = min(size, lp->rx_ring_size);
577 for (new = 0; new < overlap; new++) {
578 new_rx_ring[new] = lp->rx_ring[new];
579 new_dma_addr_list[new] = lp->rx_dma_addr[new];
580 new_skb_list[new] = lp->rx_skbuff[new];
581 }
582 /* now allocate any new buffers needed */
583 for (; new < size; new++ ) {
584 struct sk_buff *rx_skbuff;
585 new_skb_list[new] = dev_alloc_skb(PKT_BUF_SZ);
586 if (!(rx_skbuff = new_skb_list[new])) {
587 /* keep the original lists and buffers */
588 if (netif_msg_drv(lp))
589 printk(KERN_ERR
590 "%s: pcnet32_realloc_rx_ring dev_alloc_skb failed.\n",
591 dev->name);
592 goto free_all_new;
593 }
594 skb_reserve(rx_skbuff, 2);
595
596 new_dma_addr_list[new] =
597 pci_map_single(lp->pci_dev, rx_skbuff->data,
598 PKT_BUF_SZ - 2, PCI_DMA_FROMDEVICE);
599 new_rx_ring[new].base = (u32) le32_to_cpu(new_dma_addr_list[new]);
600 new_rx_ring[new].buf_length = le16_to_cpu(2 - PKT_BUF_SZ);
601 new_rx_ring[new].status = le16_to_cpu(0x8000);
602 }
603 /* and free any unneeded buffers */
604 for (; new < lp->rx_ring_size; new++) {
605 if (lp->rx_skbuff[new]) {
606 pci_unmap_single(lp->pci_dev, lp->rx_dma_addr[new],
607 PKT_BUF_SZ - 2, PCI_DMA_FROMDEVICE);
608 dev_kfree_skb(lp->rx_skbuff[new]);
609 }
610 }
611
612 kfree(lp->rx_skbuff);
613 kfree(lp->rx_dma_addr);
614 pci_free_consistent(lp->pci_dev,
615 sizeof(struct pcnet32_rx_head) *
616 lp->rx_ring_size, lp->rx_ring,
617 lp->rx_ring_dma_addr);
618
619 lp->rx_ring_size = (1 << size);
620 lp->rx_mod_mask = lp->rx_ring_size - 1;
621 lp->rx_len_bits = (size << 4);
622 lp->rx_ring = new_rx_ring;
623 lp->rx_ring_dma_addr = new_ring_dma_addr;
624 lp->rx_dma_addr = new_dma_addr_list;
625 lp->rx_skbuff = new_skb_list;
626 return;
627
628 free_all_new:
629 for (; --new >= lp->rx_ring_size; ) {
630 if (new_skb_list[new]) {
631 pci_unmap_single(lp->pci_dev, new_dma_addr_list[new],
632 PKT_BUF_SZ - 2, PCI_DMA_FROMDEVICE);
633 dev_kfree_skb(new_skb_list[new]);
634 }
635 }
636 kfree(new_skb_list);
637 free_new_lists:
638 kfree(new_dma_addr_list);
639 free_new_rx_ring:
640 pci_free_consistent(lp->pci_dev,
641 sizeof(struct pcnet32_rx_head) *
642 (1 << size),
643 new_rx_ring,
644 new_ring_dma_addr);
645 return;
646}
647
Don Fryac5bfe42006-06-29 13:55:27 -0700648static void pcnet32_purge_rx_ring(struct net_device *dev)
649{
650 struct pcnet32_private *lp = dev->priv;
651 int i;
652
653 /* free all allocated skbuffs */
654 for (i = 0; i < lp->rx_ring_size; i++) {
655 lp->rx_ring[i].status = 0; /* CPU owns buffer */
656 wmb(); /* Make sure adapter sees owner change */
657 if (lp->rx_skbuff[i]) {
658 pci_unmap_single(lp->pci_dev, lp->rx_dma_addr[i],
659 PKT_BUF_SZ - 2, PCI_DMA_FROMDEVICE);
660 dev_kfree_skb_any(lp->rx_skbuff[i]);
661 }
662 lp->rx_skbuff[i] = NULL;
663 lp->rx_dma_addr[i] = 0;
664 }
665}
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667#ifdef CONFIG_NET_POLL_CONTROLLER
668static void pcnet32_poll_controller(struct net_device *dev)
669{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500670 disable_irq(dev->irq);
671 pcnet32_interrupt(0, dev, NULL);
672 enable_irq(dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674#endif
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676static int pcnet32_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
677{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500678 struct pcnet32_private *lp = dev->priv;
679 unsigned long flags;
680 int r = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500682 if (lp->mii) {
683 spin_lock_irqsave(&lp->lock, flags);
684 mii_ethtool_gset(&lp->mii_if, cmd);
685 spin_unlock_irqrestore(&lp->lock, flags);
686 r = 0;
687 }
688 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
691static int pcnet32_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
692{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500693 struct pcnet32_private *lp = dev->priv;
694 unsigned long flags;
695 int r = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500697 if (lp->mii) {
698 spin_lock_irqsave(&lp->lock, flags);
699 r = mii_ethtool_sset(&lp->mii_if, cmd);
700 spin_unlock_irqrestore(&lp->lock, flags);
701 }
702 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500705static void pcnet32_get_drvinfo(struct net_device *dev,
706 struct ethtool_drvinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500708 struct pcnet32_private *lp = dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500710 strcpy(info->driver, DRV_NAME);
711 strcpy(info->version, DRV_VERSION);
712 if (lp->pci_dev)
713 strcpy(info->bus_info, pci_name(lp->pci_dev));
714 else
715 sprintf(info->bus_info, "VLB 0x%lx", dev->base_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718static u32 pcnet32_get_link(struct net_device *dev)
719{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500720 struct pcnet32_private *lp = dev->priv;
721 unsigned long flags;
722 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500724 spin_lock_irqsave(&lp->lock, flags);
725 if (lp->mii) {
726 r = mii_link_ok(&lp->mii_if);
727 } else {
728 ulong ioaddr = dev->base_addr; /* card base I/O address */
729 r = (lp->a.read_bcr(ioaddr, 4) != 0xc0);
730 }
731 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500733 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
736static u32 pcnet32_get_msglevel(struct net_device *dev)
737{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500738 struct pcnet32_private *lp = dev->priv;
739 return lp->msg_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
742static void pcnet32_set_msglevel(struct net_device *dev, u32 value)
743{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500744 struct pcnet32_private *lp = dev->priv;
745 lp->msg_enable = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
748static int pcnet32_nway_reset(struct net_device *dev)
749{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500750 struct pcnet32_private *lp = dev->priv;
751 unsigned long flags;
752 int r = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500754 if (lp->mii) {
755 spin_lock_irqsave(&lp->lock, flags);
756 r = mii_nway_restart(&lp->mii_if);
757 spin_unlock_irqrestore(&lp->lock, flags);
758 }
759 return r;
760}
761
762static void pcnet32_get_ringparam(struct net_device *dev,
763 struct ethtool_ringparam *ering)
764{
765 struct pcnet32_private *lp = dev->priv;
766
Don Fry6dcd60c2006-06-29 13:53:23 -0700767 ering->tx_max_pending = TX_MAX_RING_SIZE;
768 ering->tx_pending = lp->tx_ring_size;
769 ering->rx_max_pending = RX_MAX_RING_SIZE;
770 ering->rx_pending = lp->rx_ring_size;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500771}
772
773static int pcnet32_set_ringparam(struct net_device *dev,
774 struct ethtool_ringparam *ering)
775{
776 struct pcnet32_private *lp = dev->priv;
777 unsigned long flags;
Don Fry06c87852006-06-29 13:54:38 -0700778 unsigned int size;
779 ulong ioaddr = dev->base_addr;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500780 int i;
781
782 if (ering->rx_mini_pending || ering->rx_jumbo_pending)
783 return -EINVAL;
784
785 if (netif_running(dev))
Don Fry06c87852006-06-29 13:54:38 -0700786 pcnet32_netif_stop(dev);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 spin_lock_irqsave(&lp->lock, flags);
Don Fry06c87852006-06-29 13:54:38 -0700789 lp->a.write_csr(ioaddr, CSR0, CSR0_STOP); /* stop the chip */
790
791 size = min(ering->tx_pending, (unsigned int)TX_MAX_RING_SIZE);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500792
793 /* set the minimum ring size to 4, to allow the loopback test to work
794 * unchanged.
795 */
796 for (i = 2; i <= PCNET32_LOG_MAX_TX_BUFFERS; i++) {
Don Fry06c87852006-06-29 13:54:38 -0700797 if (size <= (1 << i))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500798 break;
799 }
Don Fry06c87852006-06-29 13:54:38 -0700800 if ((1 << i) != lp->tx_ring_size)
801 pcnet32_realloc_tx_ring(dev, lp, i);
802
803 size = min(ering->rx_pending, (unsigned int)RX_MAX_RING_SIZE);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500804 for (i = 2; i <= PCNET32_LOG_MAX_RX_BUFFERS; i++) {
Don Fry06c87852006-06-29 13:54:38 -0700805 if (size <= (1 << i))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500806 break;
807 }
Don Fry06c87852006-06-29 13:54:38 -0700808 if ((1 << i) != lp->rx_ring_size)
809 pcnet32_realloc_rx_ring(dev, lp, i);
810
811 dev->weight = lp->rx_ring_size / 2;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500812
Don Fry06c87852006-06-29 13:54:38 -0700813 if (netif_running(dev)) {
814 pcnet32_netif_start(dev);
815 pcnet32_restart(dev, CSR0_NORMAL);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500816 }
817
Don Frya88c8442005-11-01 12:04:33 -0800818 spin_unlock_irqrestore(&lp->lock, flags);
Hubert WS Lineabf0412005-09-14 11:39:25 -0700819
Don Fry06c87852006-06-29 13:54:38 -0700820 if (netif_msg_drv(lp))
821 printk(KERN_INFO
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500822 "%s: Ring Param Settings: RX: %d, TX: %d\n", dev->name,
823 lp->rx_ring_size, lp->tx_ring_size);
Hubert WS Lineabf0412005-09-14 11:39:25 -0700824
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500825 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500828static void pcnet32_get_strings(struct net_device *dev, u32 stringset,
829 u8 * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500831 memcpy(data, pcnet32_gstrings_test, sizeof(pcnet32_gstrings_test));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
834static int pcnet32_self_test_count(struct net_device *dev)
835{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500836 return PCNET32_TEST_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
839static void pcnet32_ethtool_test(struct net_device *dev,
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500840 struct ethtool_test *test, u64 * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500842 struct pcnet32_private *lp = dev->priv;
843 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500845 if (test->flags == ETH_TEST_FL_OFFLINE) {
846 rc = pcnet32_loopback_test(dev, data);
847 if (rc) {
848 if (netif_msg_hw(lp))
849 printk(KERN_DEBUG "%s: Loopback test failed.\n",
850 dev->name);
851 test->flags |= ETH_TEST_FL_FAILED;
852 } else if (netif_msg_hw(lp))
853 printk(KERN_DEBUG "%s: Loopback test passed.\n",
854 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 } else if (netif_msg_hw(lp))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500856 printk(KERN_DEBUG
857 "%s: No tests to run (specify 'Offline' on ethtool).",
858 dev->name);
859} /* end pcnet32_ethtool_test */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500861static int pcnet32_loopback_test(struct net_device *dev, uint64_t * data1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500863 struct pcnet32_private *lp = dev->priv;
864 struct pcnet32_access *a = &lp->a; /* access to registers */
865 ulong ioaddr = dev->base_addr; /* card base I/O address */
866 struct sk_buff *skb; /* sk buff */
867 int x, i; /* counters */
868 int numbuffs = 4; /* number of TX/RX buffers and descs */
869 u16 status = 0x8300; /* TX ring status */
870 u16 teststatus; /* test of ring status */
871 int rc; /* return code */
872 int size; /* size of packets */
873 unsigned char *packet; /* source packet data */
874 static const int data_len = 60; /* length of source packets */
875 unsigned long flags;
876 unsigned long ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500878 rc = 1; /* default to fail */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500880 if (netif_running(dev))
881 pcnet32_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500883 spin_lock_irqsave(&lp->lock, flags);
Don Fryac5bfe42006-06-29 13:55:27 -0700884 lp->a.write_csr(ioaddr, CSR0, CSR0_STOP); /* stop the chip */
885
886 numbuffs = min(numbuffs, (int)min(lp->rx_ring_size, lp->tx_ring_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500888 /* Reset the PCNET32 */
889 lp->a.reset(ioaddr);
Don Fryac5bfe42006-06-29 13:55:27 -0700890 lp->a.write_csr(ioaddr, CSR4, 0x0915);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500892 /* switch pcnet32 to 32bit mode */
893 lp->a.write_bcr(ioaddr, 20, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500895 /* purge & init rings but don't actually restart */
896 pcnet32_restart(dev, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Don Fryac5bfe42006-06-29 13:55:27 -0700898 lp->a.write_csr(ioaddr, CSR0, CSR0_STOP); /* Set STOP bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500900 /* Initialize Transmit buffers. */
901 size = data_len + 15;
902 for (x = 0; x < numbuffs; x++) {
903 if (!(skb = dev_alloc_skb(size))) {
904 if (netif_msg_hw(lp))
905 printk(KERN_DEBUG
906 "%s: Cannot allocate skb at line: %d!\n",
907 dev->name, __LINE__);
908 goto clean_up;
909 } else {
910 packet = skb->data;
911 skb_put(skb, size); /* create space for data */
912 lp->tx_skbuff[x] = skb;
913 lp->tx_ring[x].length = le16_to_cpu(-skb->len);
914 lp->tx_ring[x].misc = 0;
915
916 /* put DA and SA into the skb */
917 for (i = 0; i < 6; i++)
918 *packet++ = dev->dev_addr[i];
919 for (i = 0; i < 6; i++)
920 *packet++ = dev->dev_addr[i];
921 /* type */
922 *packet++ = 0x08;
923 *packet++ = 0x06;
924 /* packet number */
925 *packet++ = x;
926 /* fill packet with data */
927 for (i = 0; i < data_len; i++)
928 *packet++ = i;
929
930 lp->tx_dma_addr[x] =
931 pci_map_single(lp->pci_dev, skb->data, skb->len,
932 PCI_DMA_TODEVICE);
933 lp->tx_ring[x].base =
934 (u32) le32_to_cpu(lp->tx_dma_addr[x]);
935 wmb(); /* Make sure owner changes after all others are visible */
936 lp->tx_ring[x].status = le16_to_cpu(status);
937 }
938 }
939
Don Fryac5bfe42006-06-29 13:55:27 -0700940 x = a->read_bcr(ioaddr, 32); /* set internal loopback in BCR32 */
941 a->write_bcr(ioaddr, 32, x | 0x0002);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500942
Don Fryac5bfe42006-06-29 13:55:27 -0700943 /* set int loopback in CSR15 */
944 x = a->read_csr(ioaddr, CSR15) & 0xfffc;
945 lp->a.write_csr(ioaddr, CSR15, x | 0x0044);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500946
947 teststatus = le16_to_cpu(0x8000);
Don Fryac5bfe42006-06-29 13:55:27 -0700948 lp->a.write_csr(ioaddr, CSR0, CSR0_START); /* Set STRT bit */
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500949
950 /* Check status of descriptors */
951 for (x = 0; x < numbuffs; x++) {
952 ticks = 0;
953 rmb();
954 while ((lp->rx_ring[x].status & teststatus) && (ticks < 200)) {
955 spin_unlock_irqrestore(&lp->lock, flags);
Don Fryac5bfe42006-06-29 13:55:27 -0700956 msleep(1);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500957 spin_lock_irqsave(&lp->lock, flags);
958 rmb();
959 ticks++;
960 }
961 if (ticks == 200) {
962 if (netif_msg_hw(lp))
963 printk("%s: Desc %d failed to reset!\n",
964 dev->name, x);
965 break;
966 }
967 }
968
Don Fryac5bfe42006-06-29 13:55:27 -0700969 lp->a.write_csr(ioaddr, CSR0, CSR0_STOP); /* Set STOP bit */
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500970 wmb();
971 if (netif_msg_hw(lp) && netif_msg_pktdata(lp)) {
972 printk(KERN_DEBUG "%s: RX loopback packets:\n", dev->name);
973
974 for (x = 0; x < numbuffs; x++) {
975 printk(KERN_DEBUG "%s: Packet %d:\n", dev->name, x);
976 skb = lp->rx_skbuff[x];
977 for (i = 0; i < size; i++) {
978 printk("%02x ", *(skb->data + i));
979 }
980 printk("\n");
981 }
982 }
983
984 x = 0;
985 rc = 0;
986 while (x < numbuffs && !rc) {
987 skb = lp->rx_skbuff[x];
988 packet = lp->tx_skbuff[x]->data;
989 for (i = 0; i < size; i++) {
990 if (*(skb->data + i) != packet[i]) {
991 if (netif_msg_hw(lp))
992 printk(KERN_DEBUG
993 "%s: Error in compare! %2x - %02x %02x\n",
994 dev->name, i, *(skb->data + i),
995 packet[i]);
996 rc = 1;
997 break;
998 }
999 }
1000 x++;
1001 }
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001002
1003 clean_up:
Don Fryac5bfe42006-06-29 13:55:27 -07001004 *data1 = rc;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001005 pcnet32_purge_tx_ring(dev);
Don Fryac5bfe42006-06-29 13:55:27 -07001006
1007 x = a->read_csr(ioaddr, CSR15);
1008 a->write_csr(ioaddr, CSR15, (x & ~0x0044)); /* reset bits 6 and 2 */
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001009
1010 x = a->read_bcr(ioaddr, 32); /* reset internal loopback */
Don Fryac5bfe42006-06-29 13:55:27 -07001011 a->write_bcr(ioaddr, 32, (x & ~0x0002));
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001012
1013 if (netif_running(dev)) {
Don Fryac5bfe42006-06-29 13:55:27 -07001014 spin_unlock_irqrestore(&lp->lock, flags);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001015 pcnet32_open(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 } else {
Don Fryac5bfe42006-06-29 13:55:27 -07001017 pcnet32_purge_rx_ring(dev);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001018 lp->a.write_bcr(ioaddr, 20, 4); /* return to 16bit mode */
Don Fryac5bfe42006-06-29 13:55:27 -07001019 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001022 return (rc);
1023} /* end pcnet32_loopback_test */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025static void pcnet32_led_blink_callback(struct net_device *dev)
1026{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001027 struct pcnet32_private *lp = dev->priv;
1028 struct pcnet32_access *a = &lp->a;
1029 ulong ioaddr = dev->base_addr;
1030 unsigned long flags;
1031 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001033 spin_lock_irqsave(&lp->lock, flags);
1034 for (i = 4; i < 8; i++) {
1035 a->write_bcr(ioaddr, i, a->read_bcr(ioaddr, i) ^ 0x4000);
1036 }
1037 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001039 mod_timer(&lp->blink_timer, PCNET32_BLINK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
1042static int pcnet32_phys_id(struct net_device *dev, u32 data)
1043{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001044 struct pcnet32_private *lp = dev->priv;
1045 struct pcnet32_access *a = &lp->a;
1046 ulong ioaddr = dev->base_addr;
1047 unsigned long flags;
1048 int i, regs[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001050 if (!lp->blink_timer.function) {
1051 init_timer(&lp->blink_timer);
1052 lp->blink_timer.function = (void *)pcnet32_led_blink_callback;
1053 lp->blink_timer.data = (unsigned long)dev;
1054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001056 /* Save the current value of the bcrs */
1057 spin_lock_irqsave(&lp->lock, flags);
1058 for (i = 4; i < 8; i++) {
1059 regs[i - 4] = a->read_bcr(ioaddr, i);
1060 }
1061 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001063 mod_timer(&lp->blink_timer, jiffies);
1064 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001066 if ((!data) || (data > (u32) (MAX_SCHEDULE_TIMEOUT / HZ)))
1067 data = (u32) (MAX_SCHEDULE_TIMEOUT / HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001069 msleep_interruptible(data * 1000);
1070 del_timer_sync(&lp->blink_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001072 /* Restore the original value of the bcrs */
1073 spin_lock_irqsave(&lp->lock, flags);
1074 for (i = 4; i < 8; i++) {
1075 a->write_bcr(ioaddr, i, regs[i - 4]);
1076 }
1077 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001079 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Don Frydf27f4a2006-06-29 13:55:02 -07001082/*
1083 * lp->lock must be held.
1084 */
1085static int pcnet32_suspend(struct net_device *dev, unsigned long *flags,
1086 int can_sleep)
1087{
1088 int csr5;
1089 struct pcnet32_private *lp = dev->priv;
1090 struct pcnet32_access *a = &lp->a;
1091 ulong ioaddr = dev->base_addr;
1092 int ticks;
1093
1094 /* set SUSPEND (SPND) - CSR5 bit 0 */
1095 csr5 = a->read_csr(ioaddr, CSR5);
1096 a->write_csr(ioaddr, CSR5, csr5 | CSR5_SUSPEND);
1097
1098 /* poll waiting for bit to be set */
1099 ticks = 0;
1100 while (!(a->read_csr(ioaddr, CSR5) & CSR5_SUSPEND)) {
1101 spin_unlock_irqrestore(&lp->lock, *flags);
1102 if (can_sleep)
1103 msleep(1);
1104 else
1105 mdelay(1);
1106 spin_lock_irqsave(&lp->lock, *flags);
1107 ticks++;
1108 if (ticks > 200) {
1109 if (netif_msg_hw(lp))
1110 printk(KERN_DEBUG
1111 "%s: Error getting into suspend!\n",
1112 dev->name);
1113 return 0;
1114 }
1115 }
1116 return 1;
1117}
1118
Don Fryac62ef02006-03-20 15:26:03 -08001119#define PCNET32_REGS_PER_PHY 32
1120#define PCNET32_MAX_PHYS 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121static int pcnet32_get_regs_len(struct net_device *dev)
1122{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001123 struct pcnet32_private *lp = dev->priv;
1124 int j = lp->phycount * PCNET32_REGS_PER_PHY;
Don Fryac62ef02006-03-20 15:26:03 -08001125
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001126 return ((PCNET32_NUM_REGS + j) * sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
1129static void pcnet32_get_regs(struct net_device *dev, struct ethtool_regs *regs,
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001130 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001132 int i, csr0;
1133 u16 *buff = ptr;
1134 struct pcnet32_private *lp = dev->priv;
1135 struct pcnet32_access *a = &lp->a;
1136 ulong ioaddr = dev->base_addr;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001137 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001139 spin_lock_irqsave(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Don Frydf27f4a2006-06-29 13:55:02 -07001141 csr0 = a->read_csr(ioaddr, CSR0);
1142 if (!(csr0 & CSR0_STOP)) /* If not stopped */
1143 pcnet32_suspend(dev, &flags, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001145 /* read address PROM */
1146 for (i = 0; i < 16; i += 2)
1147 *buff++ = inw(ioaddr + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001149 /* read control and status registers */
1150 for (i = 0; i < 90; i++) {
1151 *buff++ = a->read_csr(ioaddr, i);
1152 }
1153
1154 *buff++ = a->read_csr(ioaddr, 112);
1155 *buff++ = a->read_csr(ioaddr, 114);
1156
1157 /* read bus configuration registers */
1158 for (i = 0; i < 30; i++) {
1159 *buff++ = a->read_bcr(ioaddr, i);
1160 }
1161 *buff++ = 0; /* skip bcr30 so as not to hang 79C976 */
1162 for (i = 31; i < 36; i++) {
1163 *buff++ = a->read_bcr(ioaddr, i);
1164 }
1165
1166 /* read mii phy registers */
1167 if (lp->mii) {
1168 int j;
1169 for (j = 0; j < PCNET32_MAX_PHYS; j++) {
1170 if (lp->phymask & (1 << j)) {
1171 for (i = 0; i < PCNET32_REGS_PER_PHY; i++) {
1172 lp->a.write_bcr(ioaddr, 33,
1173 (j << 5) | i);
1174 *buff++ = lp->a.read_bcr(ioaddr, 34);
1175 }
1176 }
1177 }
1178 }
1179
Don Frydf27f4a2006-06-29 13:55:02 -07001180 if (!(csr0 & CSR0_STOP)) { /* If not stopped */
1181 int csr5;
1182
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001183 /* clear SUSPEND (SPND) - CSR5 bit 0 */
Don Frydf27f4a2006-06-29 13:55:02 -07001184 csr5 = a->read_csr(ioaddr, CSR5);
1185 a->write_csr(ioaddr, CSR5, csr5 & (~CSR5_SUSPEND));
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001186 }
1187
1188 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
1191static struct ethtool_ops pcnet32_ethtool_ops = {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001192 .get_settings = pcnet32_get_settings,
1193 .set_settings = pcnet32_set_settings,
1194 .get_drvinfo = pcnet32_get_drvinfo,
1195 .get_msglevel = pcnet32_get_msglevel,
1196 .set_msglevel = pcnet32_set_msglevel,
1197 .nway_reset = pcnet32_nway_reset,
1198 .get_link = pcnet32_get_link,
1199 .get_ringparam = pcnet32_get_ringparam,
1200 .set_ringparam = pcnet32_set_ringparam,
1201 .get_tx_csum = ethtool_op_get_tx_csum,
1202 .get_sg = ethtool_op_get_sg,
1203 .get_tso = ethtool_op_get_tso,
1204 .get_strings = pcnet32_get_strings,
1205 .self_test_count = pcnet32_self_test_count,
1206 .self_test = pcnet32_ethtool_test,
1207 .phys_id = pcnet32_phys_id,
1208 .get_regs_len = pcnet32_get_regs_len,
1209 .get_regs = pcnet32_get_regs,
1210 .get_perm_addr = ethtool_op_get_perm_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211};
1212
1213/* only probes for non-PCI devices, the rest are handled by
1214 * pci_register_driver via pcnet32_probe_pci */
1215
Don Frydcaf9762006-06-29 13:52:29 -07001216static void __devinit pcnet32_probe_vlbus(unsigned int *pcnet32_portlist)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001218 unsigned int *port, ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001220 /* search for PCnet32 VLB cards at known addresses */
1221 for (port = pcnet32_portlist; (ioaddr = *port); port++) {
1222 if (request_region
1223 (ioaddr, PCNET32_TOTAL_SIZE, "pcnet32_probe_vlbus")) {
1224 /* check if there is really a pcnet chip on that ioaddr */
1225 if ((inb(ioaddr + 14) == 0x57)
1226 && (inb(ioaddr + 15) == 0x57)) {
1227 pcnet32_probe1(ioaddr, 0, NULL);
1228 } else {
1229 release_region(ioaddr, PCNET32_TOTAL_SIZE);
1230 }
1231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235static int __devinit
1236pcnet32_probe_pci(struct pci_dev *pdev, const struct pci_device_id *ent)
1237{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001238 unsigned long ioaddr;
1239 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001241 err = pci_enable_device(pdev);
1242 if (err < 0) {
1243 if (pcnet32_debug & NETIF_MSG_PROBE)
1244 printk(KERN_ERR PFX
1245 "failed to enable device -- err=%d\n", err);
1246 return err;
1247 }
1248 pci_set_master(pdev);
1249
1250 ioaddr = pci_resource_start(pdev, 0);
1251 if (!ioaddr) {
1252 if (pcnet32_debug & NETIF_MSG_PROBE)
1253 printk(KERN_ERR PFX
1254 "card has no PCI IO resources, aborting\n");
1255 return -ENODEV;
1256 }
1257
1258 if (!pci_dma_supported(pdev, PCNET32_DMA_MASK)) {
1259 if (pcnet32_debug & NETIF_MSG_PROBE)
1260 printk(KERN_ERR PFX
1261 "architecture does not support 32bit PCI busmaster DMA\n");
1262 return -ENODEV;
1263 }
1264 if (request_region(ioaddr, PCNET32_TOTAL_SIZE, "pcnet32_probe_pci") ==
1265 NULL) {
1266 if (pcnet32_debug & NETIF_MSG_PROBE)
1267 printk(KERN_ERR PFX
1268 "io address range already allocated\n");
1269 return -EBUSY;
1270 }
1271
1272 err = pcnet32_probe1(ioaddr, 1, pdev);
1273 if (err < 0) {
1274 pci_disable_device(pdev);
1275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279/* pcnet32_probe1
1280 * Called from both pcnet32_probe_vlbus and pcnet_probe_pci.
1281 * pdev will be NULL when called from pcnet32_probe_vlbus.
1282 */
1283static int __devinit
1284pcnet32_probe1(unsigned long ioaddr, int shared, struct pci_dev *pdev)
1285{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001286 struct pcnet32_private *lp;
1287 dma_addr_t lp_dma_addr;
1288 int i, media;
1289 int fdx, mii, fset, dxsuflo;
1290 int chip_version;
1291 char *chipname;
1292 struct net_device *dev;
1293 struct pcnet32_access *a = NULL;
1294 u8 promaddr[6];
1295 int ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001297 /* reset the chip */
1298 pcnet32_wio_reset(ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001300 /* NOTE: 16-bit check is first, otherwise some older PCnet chips fail */
1301 if (pcnet32_wio_read_csr(ioaddr, 0) == 4 && pcnet32_wio_check(ioaddr)) {
1302 a = &pcnet32_wio;
1303 } else {
1304 pcnet32_dwio_reset(ioaddr);
1305 if (pcnet32_dwio_read_csr(ioaddr, 0) == 4
1306 && pcnet32_dwio_check(ioaddr)) {
1307 a = &pcnet32_dwio;
1308 } else
1309 goto err_release_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001312 chip_version =
1313 a->read_csr(ioaddr, 88) | (a->read_csr(ioaddr, 89) << 16);
1314 if ((pcnet32_debug & NETIF_MSG_PROBE) && (pcnet32_debug & NETIF_MSG_HW))
1315 printk(KERN_INFO " PCnet chip version is %#x.\n",
1316 chip_version);
1317 if ((chip_version & 0xfff) != 0x003) {
1318 if (pcnet32_debug & NETIF_MSG_PROBE)
1319 printk(KERN_INFO PFX "Unsupported chip version.\n");
1320 goto err_release_region;
1321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001323 /* initialize variables */
1324 fdx = mii = fset = dxsuflo = 0;
1325 chip_version = (chip_version >> 12) & 0xffff;
1326
1327 switch (chip_version) {
1328 case 0x2420:
1329 chipname = "PCnet/PCI 79C970"; /* PCI */
1330 break;
1331 case 0x2430:
1332 if (shared)
1333 chipname = "PCnet/PCI 79C970"; /* 970 gives the wrong chip id back */
1334 else
1335 chipname = "PCnet/32 79C965"; /* 486/VL bus */
1336 break;
1337 case 0x2621:
1338 chipname = "PCnet/PCI II 79C970A"; /* PCI */
1339 fdx = 1;
1340 break;
1341 case 0x2623:
1342 chipname = "PCnet/FAST 79C971"; /* PCI */
1343 fdx = 1;
1344 mii = 1;
1345 fset = 1;
1346 break;
1347 case 0x2624:
1348 chipname = "PCnet/FAST+ 79C972"; /* PCI */
1349 fdx = 1;
1350 mii = 1;
1351 fset = 1;
1352 break;
1353 case 0x2625:
1354 chipname = "PCnet/FAST III 79C973"; /* PCI */
1355 fdx = 1;
1356 mii = 1;
1357 break;
1358 case 0x2626:
1359 chipname = "PCnet/Home 79C978"; /* PCI */
1360 fdx = 1;
1361 /*
1362 * This is based on specs published at www.amd.com. This section
1363 * assumes that a card with a 79C978 wants to go into standard
1364 * ethernet mode. The 79C978 can also go into 1Mb HomePNA mode,
1365 * and the module option homepna=1 can select this instead.
1366 */
1367 media = a->read_bcr(ioaddr, 49);
1368 media &= ~3; /* default to 10Mb ethernet */
1369 if (cards_found < MAX_UNITS && homepna[cards_found])
1370 media |= 1; /* switch to home wiring mode */
1371 if (pcnet32_debug & NETIF_MSG_PROBE)
1372 printk(KERN_DEBUG PFX "media set to %sMbit mode.\n",
1373 (media & 1) ? "1" : "10");
1374 a->write_bcr(ioaddr, 49, media);
1375 break;
1376 case 0x2627:
1377 chipname = "PCnet/FAST III 79C975"; /* PCI */
1378 fdx = 1;
1379 mii = 1;
1380 break;
1381 case 0x2628:
1382 chipname = "PCnet/PRO 79C976";
1383 fdx = 1;
1384 mii = 1;
1385 break;
1386 default:
1387 if (pcnet32_debug & NETIF_MSG_PROBE)
1388 printk(KERN_INFO PFX
1389 "PCnet version %#x, no PCnet32 chip.\n",
1390 chip_version);
1391 goto err_release_region;
1392 }
1393
1394 /*
1395 * On selected chips turn on the BCR18:NOUFLO bit. This stops transmit
1396 * starting until the packet is loaded. Strike one for reliability, lose
1397 * one for latency - although on PCI this isnt a big loss. Older chips
1398 * have FIFO's smaller than a packet, so you can't do this.
1399 * Turn on BCR18:BurstRdEn and BCR18:BurstWrEn.
1400 */
1401
1402 if (fset) {
1403 a->write_bcr(ioaddr, 18, (a->read_bcr(ioaddr, 18) | 0x0860));
1404 a->write_csr(ioaddr, 80,
1405 (a->read_csr(ioaddr, 80) & 0x0C00) | 0x0c00);
1406 dxsuflo = 1;
1407 }
1408
1409 dev = alloc_etherdev(0);
1410 if (!dev) {
1411 if (pcnet32_debug & NETIF_MSG_PROBE)
1412 printk(KERN_ERR PFX "Memory allocation failed.\n");
1413 ret = -ENOMEM;
1414 goto err_release_region;
1415 }
1416 SET_NETDEV_DEV(dev, &pdev->dev);
1417
1418 if (pcnet32_debug & NETIF_MSG_PROBE)
1419 printk(KERN_INFO PFX "%s at %#3lx,", chipname, ioaddr);
1420
1421 /* In most chips, after a chip reset, the ethernet address is read from the
1422 * station address PROM at the base address and programmed into the
1423 * "Physical Address Registers" CSR12-14.
1424 * As a precautionary measure, we read the PROM values and complain if
Linas Vepstasbc0e1fc2006-03-28 16:36:23 -06001425 * they disagree with the CSRs. If they miscompare, and the PROM addr
1426 * is valid, then the PROM addr is used.
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001427 */
1428 for (i = 0; i < 3; i++) {
1429 unsigned int val;
1430 val = a->read_csr(ioaddr, i + 12) & 0x0ffff;
1431 /* There may be endianness issues here. */
1432 dev->dev_addr[2 * i] = val & 0x0ff;
1433 dev->dev_addr[2 * i + 1] = (val >> 8) & 0x0ff;
1434 }
1435
1436 /* read PROM address and compare with CSR address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 for (i = 0; i < 6; i++)
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001438 promaddr[i] = inb(ioaddr + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001440 if (memcmp(promaddr, dev->dev_addr, 6)
1441 || !is_valid_ether_addr(dev->dev_addr)) {
1442 if (is_valid_ether_addr(promaddr)) {
1443 if (pcnet32_debug & NETIF_MSG_PROBE) {
1444 printk(" warning: CSR address invalid,\n");
1445 printk(KERN_INFO
1446 " using instead PROM address of");
1447 }
1448 memcpy(dev->dev_addr, promaddr, 6);
1449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001451 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001453 /* if the ethernet address is not valid, force to 00:00:00:00:00:00 */
1454 if (!is_valid_ether_addr(dev->perm_addr))
1455 memset(dev->dev_addr, 0, sizeof(dev->dev_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001457 if (pcnet32_debug & NETIF_MSG_PROBE) {
1458 for (i = 0; i < 6; i++)
1459 printk(" %2.2x", dev->dev_addr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001461 /* Version 0x2623 and 0x2624 */
1462 if (((chip_version + 1) & 0xfffe) == 0x2624) {
1463 i = a->read_csr(ioaddr, 80) & 0x0C00; /* Check tx_start_pt */
1464 printk("\n" KERN_INFO " tx_start_pt(0x%04x):", i);
1465 switch (i >> 10) {
1466 case 0:
1467 printk(" 20 bytes,");
1468 break;
1469 case 1:
1470 printk(" 64 bytes,");
1471 break;
1472 case 2:
1473 printk(" 128 bytes,");
1474 break;
1475 case 3:
1476 printk("~220 bytes,");
1477 break;
1478 }
1479 i = a->read_bcr(ioaddr, 18); /* Check Burst/Bus control */
1480 printk(" BCR18(%x):", i & 0xffff);
1481 if (i & (1 << 5))
1482 printk("BurstWrEn ");
1483 if (i & (1 << 6))
1484 printk("BurstRdEn ");
1485 if (i & (1 << 7))
1486 printk("DWordIO ");
1487 if (i & (1 << 11))
1488 printk("NoUFlow ");
1489 i = a->read_bcr(ioaddr, 25);
1490 printk("\n" KERN_INFO " SRAMSIZE=0x%04x,", i << 8);
1491 i = a->read_bcr(ioaddr, 26);
1492 printk(" SRAM_BND=0x%04x,", i << 8);
1493 i = a->read_bcr(ioaddr, 27);
1494 if (i & (1 << 14))
1495 printk("LowLatRx");
1496 }
1497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001499 dev->base_addr = ioaddr;
1500 /* pci_alloc_consistent returns page-aligned memory, so we do not have to check the alignment */
1501 if ((lp =
1502 pci_alloc_consistent(pdev, sizeof(*lp), &lp_dma_addr)) == NULL) {
1503 if (pcnet32_debug & NETIF_MSG_PROBE)
1504 printk(KERN_ERR PFX
1505 "Consistent memory allocation failed.\n");
1506 ret = -ENOMEM;
1507 goto err_free_netdev;
1508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001510 memset(lp, 0, sizeof(*lp));
1511 lp->dma_addr = lp_dma_addr;
1512 lp->pci_dev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001514 spin_lock_init(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001516 SET_MODULE_OWNER(dev);
1517 SET_NETDEV_DEV(dev, &pdev->dev);
1518 dev->priv = lp;
1519 lp->name = chipname;
1520 lp->shared_irq = shared;
1521 lp->tx_ring_size = TX_RING_SIZE; /* default tx ring size */
1522 lp->rx_ring_size = RX_RING_SIZE; /* default rx ring size */
1523 lp->tx_mod_mask = lp->tx_ring_size - 1;
1524 lp->rx_mod_mask = lp->rx_ring_size - 1;
1525 lp->tx_len_bits = (PCNET32_LOG_TX_BUFFERS << 12);
1526 lp->rx_len_bits = (PCNET32_LOG_RX_BUFFERS << 4);
1527 lp->mii_if.full_duplex = fdx;
1528 lp->mii_if.phy_id_mask = 0x1f;
1529 lp->mii_if.reg_num_mask = 0x1f;
1530 lp->dxsuflo = dxsuflo;
1531 lp->mii = mii;
1532 lp->msg_enable = pcnet32_debug;
1533 if ((cards_found >= MAX_UNITS)
1534 || (options[cards_found] > sizeof(options_mapping)))
1535 lp->options = PCNET32_PORT_ASEL;
1536 else
1537 lp->options = options_mapping[options[cards_found]];
1538 lp->mii_if.dev = dev;
1539 lp->mii_if.mdio_read = mdio_read;
1540 lp->mii_if.mdio_write = mdio_write;
1541
1542 if (fdx && !(lp->options & PCNET32_PORT_ASEL) &&
1543 ((cards_found >= MAX_UNITS) || full_duplex[cards_found]))
1544 lp->options |= PCNET32_PORT_FD;
1545
1546 if (!a) {
1547 if (pcnet32_debug & NETIF_MSG_PROBE)
1548 printk(KERN_ERR PFX "No access methods\n");
1549 ret = -ENODEV;
1550 goto err_free_consistent;
1551 }
1552 lp->a = *a;
1553
1554 /* prior to register_netdev, dev->name is not yet correct */
1555 if (pcnet32_alloc_ring(dev, pci_name(lp->pci_dev))) {
1556 ret = -ENOMEM;
1557 goto err_free_ring;
1558 }
1559 /* detect special T1/E1 WAN card by checking for MAC address */
1560 if (dev->dev_addr[0] == 0x00 && dev->dev_addr[1] == 0xe0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 && dev->dev_addr[2] == 0x75)
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001562 lp->options = PCNET32_PORT_FD | PCNET32_PORT_GPSI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001564 lp->init_block.mode = le16_to_cpu(0x0003); /* Disable Rx and Tx. */
1565 lp->init_block.tlen_rlen =
1566 le16_to_cpu(lp->tx_len_bits | lp->rx_len_bits);
1567 for (i = 0; i < 6; i++)
1568 lp->init_block.phys_addr[i] = dev->dev_addr[i];
1569 lp->init_block.filter[0] = 0x00000000;
1570 lp->init_block.filter[1] = 0x00000000;
1571 lp->init_block.rx_ring = (u32) le32_to_cpu(lp->rx_ring_dma_addr);
1572 lp->init_block.tx_ring = (u32) le32_to_cpu(lp->tx_ring_dma_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001574 /* switch pcnet32 to 32bit mode */
1575 a->write_bcr(ioaddr, 20, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001577 a->write_csr(ioaddr, 1, (lp->dma_addr + offsetof(struct pcnet32_private,
1578 init_block)) & 0xffff);
1579 a->write_csr(ioaddr, 2, (lp->dma_addr + offsetof(struct pcnet32_private,
1580 init_block)) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001582 if (pdev) { /* use the IRQ provided by PCI */
1583 dev->irq = pdev->irq;
1584 if (pcnet32_debug & NETIF_MSG_PROBE)
1585 printk(" assigned IRQ %d.\n", dev->irq);
1586 } else {
1587 unsigned long irq_mask = probe_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001589 /*
1590 * To auto-IRQ we enable the initialization-done and DMA error
1591 * interrupts. For ISA boards we get a DMA error, but VLB and PCI
1592 * boards will work.
1593 */
1594 /* Trigger an initialization just for the interrupt. */
1595 a->write_csr(ioaddr, 0, 0x41);
1596 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001598 dev->irq = probe_irq_off(irq_mask);
1599 if (!dev->irq) {
1600 if (pcnet32_debug & NETIF_MSG_PROBE)
1601 printk(", failed to detect IRQ line.\n");
1602 ret = -ENODEV;
1603 goto err_free_ring;
1604 }
1605 if (pcnet32_debug & NETIF_MSG_PROBE)
1606 printk(", probed IRQ %d.\n", dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001609 /* Set the mii phy_id so that we can query the link state */
1610 if (lp->mii) {
1611 /* lp->phycount and lp->phymask are set to 0 by memset above */
Don Fryac62ef02006-03-20 15:26:03 -08001612
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001613 lp->mii_if.phy_id = ((lp->a.read_bcr(ioaddr, 33)) >> 5) & 0x1f;
1614 /* scan for PHYs */
1615 for (i = 0; i < PCNET32_MAX_PHYS; i++) {
1616 unsigned short id1, id2;
Don Fryac62ef02006-03-20 15:26:03 -08001617
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001618 id1 = mdio_read(dev, i, MII_PHYSID1);
1619 if (id1 == 0xffff)
1620 continue;
1621 id2 = mdio_read(dev, i, MII_PHYSID2);
1622 if (id2 == 0xffff)
1623 continue;
1624 if (i == 31 && ((chip_version + 1) & 0xfffe) == 0x2624)
1625 continue; /* 79C971 & 79C972 have phantom phy at id 31 */
1626 lp->phycount++;
1627 lp->phymask |= (1 << i);
1628 lp->mii_if.phy_id = i;
1629 if (pcnet32_debug & NETIF_MSG_PROBE)
1630 printk(KERN_INFO PFX
1631 "Found PHY %04x:%04x at address %d.\n",
1632 id1, id2, i);
1633 }
1634 lp->a.write_bcr(ioaddr, 33, (lp->mii_if.phy_id) << 5);
1635 if (lp->phycount > 1) {
1636 lp->options |= PCNET32_PORT_MII;
1637 }
Don Fryac62ef02006-03-20 15:26:03 -08001638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001640 init_timer(&lp->watchdog_timer);
1641 lp->watchdog_timer.data = (unsigned long)dev;
1642 lp->watchdog_timer.function = (void *)&pcnet32_watchdog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001644 /* The PCNET32-specific entries in the device structure. */
1645 dev->open = &pcnet32_open;
1646 dev->hard_start_xmit = &pcnet32_start_xmit;
1647 dev->stop = &pcnet32_close;
1648 dev->get_stats = &pcnet32_get_stats;
1649 dev->set_multicast_list = &pcnet32_set_multicast_list;
1650 dev->do_ioctl = &pcnet32_ioctl;
1651 dev->ethtool_ops = &pcnet32_ethtool_ops;
1652 dev->tx_timeout = pcnet32_tx_timeout;
1653 dev->watchdog_timeo = (5 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655#ifdef CONFIG_NET_POLL_CONTROLLER
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001656 dev->poll_controller = pcnet32_poll_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657#endif
1658
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001659 /* Fill in the generic fields of the device structure. */
1660 if (register_netdev(dev))
1661 goto err_free_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001663 if (pdev) {
1664 pci_set_drvdata(pdev, dev);
1665 } else {
1666 lp->next = pcnet32_dev;
1667 pcnet32_dev = dev;
1668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001670 if (pcnet32_debug & NETIF_MSG_PROBE)
1671 printk(KERN_INFO "%s: registered as %s\n", dev->name, lp->name);
1672 cards_found++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001674 /* enable LED writes */
1675 a->write_bcr(ioaddr, 2, a->read_bcr(ioaddr, 2) | 0x1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001677 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001679 err_free_ring:
1680 pcnet32_free_ring(dev);
1681 err_free_consistent:
1682 pci_free_consistent(lp->pci_dev, sizeof(*lp), lp, lp->dma_addr);
1683 err_free_netdev:
1684 free_netdev(dev);
1685 err_release_region:
1686 release_region(ioaddr, PCNET32_TOTAL_SIZE);
1687 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
Don Frya88c8442005-11-01 12:04:33 -08001690/* if any allocation fails, caller must also call pcnet32_free_ring */
1691static int pcnet32_alloc_ring(struct net_device *dev, char *name)
Hubert WS Lineabf0412005-09-14 11:39:25 -07001692{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001693 struct pcnet32_private *lp = dev->priv;
Hubert WS Lineabf0412005-09-14 11:39:25 -07001694
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001695 lp->tx_ring = pci_alloc_consistent(lp->pci_dev,
1696 sizeof(struct pcnet32_tx_head) *
1697 lp->tx_ring_size,
1698 &lp->tx_ring_dma_addr);
1699 if (lp->tx_ring == NULL) {
Don Fry12fa30f2006-06-29 13:53:48 -07001700 if (netif_msg_drv(lp))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001701 printk("\n" KERN_ERR PFX
1702 "%s: Consistent memory allocation failed.\n",
1703 name);
1704 return -ENOMEM;
1705 }
Hubert WS Lineabf0412005-09-14 11:39:25 -07001706
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001707 lp->rx_ring = pci_alloc_consistent(lp->pci_dev,
1708 sizeof(struct pcnet32_rx_head) *
1709 lp->rx_ring_size,
1710 &lp->rx_ring_dma_addr);
1711 if (lp->rx_ring == NULL) {
Don Fry12fa30f2006-06-29 13:53:48 -07001712 if (netif_msg_drv(lp))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001713 printk("\n" KERN_ERR PFX
1714 "%s: Consistent memory allocation failed.\n",
1715 name);
1716 return -ENOMEM;
1717 }
Hubert WS Lineabf0412005-09-14 11:39:25 -07001718
Don Fry12fa30f2006-06-29 13:53:48 -07001719 lp->tx_dma_addr = kcalloc(lp->tx_ring_size, sizeof(dma_addr_t),
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001720 GFP_ATOMIC);
1721 if (!lp->tx_dma_addr) {
Don Fry12fa30f2006-06-29 13:53:48 -07001722 if (netif_msg_drv(lp))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001723 printk("\n" KERN_ERR PFX
1724 "%s: Memory allocation failed.\n", name);
1725 return -ENOMEM;
1726 }
Hubert WS Lineabf0412005-09-14 11:39:25 -07001727
Don Fry12fa30f2006-06-29 13:53:48 -07001728 lp->rx_dma_addr = kcalloc(lp->rx_ring_size, sizeof(dma_addr_t),
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001729 GFP_ATOMIC);
1730 if (!lp->rx_dma_addr) {
Don Fry12fa30f2006-06-29 13:53:48 -07001731 if (netif_msg_drv(lp))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001732 printk("\n" KERN_ERR PFX
1733 "%s: Memory allocation failed.\n", name);
1734 return -ENOMEM;
1735 }
Hubert WS Lineabf0412005-09-14 11:39:25 -07001736
Don Fry12fa30f2006-06-29 13:53:48 -07001737 lp->tx_skbuff = kcalloc(lp->tx_ring_size, sizeof(struct sk_buff *),
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001738 GFP_ATOMIC);
1739 if (!lp->tx_skbuff) {
Don Fry12fa30f2006-06-29 13:53:48 -07001740 if (netif_msg_drv(lp))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001741 printk("\n" KERN_ERR PFX
1742 "%s: Memory allocation failed.\n", name);
1743 return -ENOMEM;
1744 }
Hubert WS Lineabf0412005-09-14 11:39:25 -07001745
Don Fry12fa30f2006-06-29 13:53:48 -07001746 lp->rx_skbuff = kcalloc(lp->rx_ring_size, sizeof(struct sk_buff *),
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001747 GFP_ATOMIC);
1748 if (!lp->rx_skbuff) {
Don Fry12fa30f2006-06-29 13:53:48 -07001749 if (netif_msg_drv(lp))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001750 printk("\n" KERN_ERR PFX
1751 "%s: Memory allocation failed.\n", name);
1752 return -ENOMEM;
1753 }
Hubert WS Lineabf0412005-09-14 11:39:25 -07001754
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001755 return 0;
Hubert WS Lineabf0412005-09-14 11:39:25 -07001756}
1757
Hubert WS Lineabf0412005-09-14 11:39:25 -07001758static void pcnet32_free_ring(struct net_device *dev)
1759{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001760 struct pcnet32_private *lp = dev->priv;
Hubert WS Lineabf0412005-09-14 11:39:25 -07001761
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001762 kfree(lp->tx_skbuff);
1763 lp->tx_skbuff = NULL;
Hubert WS Lineabf0412005-09-14 11:39:25 -07001764
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001765 kfree(lp->rx_skbuff);
1766 lp->rx_skbuff = NULL;
Hubert WS Lineabf0412005-09-14 11:39:25 -07001767
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001768 kfree(lp->tx_dma_addr);
1769 lp->tx_dma_addr = NULL;
Hubert WS Lineabf0412005-09-14 11:39:25 -07001770
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001771 kfree(lp->rx_dma_addr);
1772 lp->rx_dma_addr = NULL;
Hubert WS Lineabf0412005-09-14 11:39:25 -07001773
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001774 if (lp->tx_ring) {
1775 pci_free_consistent(lp->pci_dev,
1776 sizeof(struct pcnet32_tx_head) *
1777 lp->tx_ring_size, lp->tx_ring,
1778 lp->tx_ring_dma_addr);
1779 lp->tx_ring = NULL;
1780 }
Hubert WS Lineabf0412005-09-14 11:39:25 -07001781
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001782 if (lp->rx_ring) {
1783 pci_free_consistent(lp->pci_dev,
1784 sizeof(struct pcnet32_rx_head) *
1785 lp->rx_ring_size, lp->rx_ring,
1786 lp->rx_ring_dma_addr);
1787 lp->rx_ring = NULL;
1788 }
Hubert WS Lineabf0412005-09-14 11:39:25 -07001789}
1790
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001791static int pcnet32_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001793 struct pcnet32_private *lp = dev->priv;
1794 unsigned long ioaddr = dev->base_addr;
1795 u16 val;
1796 int i;
1797 int rc;
1798 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001800 if (request_irq(dev->irq, &pcnet32_interrupt,
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07001801 lp->shared_irq ? IRQF_SHARED : 0, dev->name,
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001802 (void *)dev)) {
1803 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001806 spin_lock_irqsave(&lp->lock, flags);
1807 /* Check for a valid station address */
1808 if (!is_valid_ether_addr(dev->dev_addr)) {
1809 rc = -EINVAL;
1810 goto err_free_irq;
1811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001813 /* Reset the PCNET32 */
1814 lp->a.reset(ioaddr);
1815
1816 /* switch pcnet32 to 32bit mode */
1817 lp->a.write_bcr(ioaddr, 20, 2);
1818
1819 if (netif_msg_ifup(lp))
1820 printk(KERN_DEBUG
1821 "%s: pcnet32_open() irq %d tx/rx rings %#x/%#x init %#x.\n",
1822 dev->name, dev->irq, (u32) (lp->tx_ring_dma_addr),
1823 (u32) (lp->rx_ring_dma_addr),
1824 (u32) (lp->dma_addr +
1825 offsetof(struct pcnet32_private, init_block)));
1826
1827 /* set/reset autoselect bit */
1828 val = lp->a.read_bcr(ioaddr, 2) & ~2;
1829 if (lp->options & PCNET32_PORT_ASEL)
1830 val |= 2;
1831 lp->a.write_bcr(ioaddr, 2, val);
1832
1833 /* handle full duplex setting */
1834 if (lp->mii_if.full_duplex) {
1835 val = lp->a.read_bcr(ioaddr, 9) & ~3;
1836 if (lp->options & PCNET32_PORT_FD) {
1837 val |= 1;
1838 if (lp->options == (PCNET32_PORT_FD | PCNET32_PORT_AUI))
1839 val |= 2;
1840 } else if (lp->options & PCNET32_PORT_ASEL) {
1841 /* workaround of xSeries250, turn on for 79C975 only */
1842 i = ((lp->a.read_csr(ioaddr, 88) |
1843 (lp->a.
1844 read_csr(ioaddr, 89) << 16)) >> 12) & 0xffff;
1845 if (i == 0x2627)
1846 val |= 3;
1847 }
1848 lp->a.write_bcr(ioaddr, 9, val);
1849 }
1850
1851 /* set/reset GPSI bit in test register */
1852 val = lp->a.read_csr(ioaddr, 124) & ~0x10;
1853 if ((lp->options & PCNET32_PORT_PORTSEL) == PCNET32_PORT_GPSI)
1854 val |= 0x10;
1855 lp->a.write_csr(ioaddr, 124, val);
1856
1857 /* Allied Telesyn AT 2700/2701 FX are 100Mbit only and do not negotiate */
1858 if (lp->pci_dev->subsystem_vendor == PCI_VENDOR_ID_AT &&
Don Fry2964bbd2005-11-01 12:50:57 -08001859 (lp->pci_dev->subsystem_device == PCI_SUBDEVICE_ID_AT_2700FX ||
1860 lp->pci_dev->subsystem_device == PCI_SUBDEVICE_ID_AT_2701FX)) {
Don Fryac62ef02006-03-20 15:26:03 -08001861 if (lp->options & PCNET32_PORT_ASEL) {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001862 lp->options = PCNET32_PORT_FD | PCNET32_PORT_100;
1863 if (netif_msg_link(lp))
1864 printk(KERN_DEBUG
1865 "%s: Setting 100Mb-Full Duplex.\n",
1866 dev->name);
Don Fryac62ef02006-03-20 15:26:03 -08001867 }
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001868 }
1869 if (lp->phycount < 2) {
1870 /*
1871 * 24 Jun 2004 according AMD, in order to change the PHY,
1872 * DANAS (or DISPM for 79C976) must be set; then select the speed,
1873 * duplex, and/or enable auto negotiation, and clear DANAS
1874 */
1875 if (lp->mii && !(lp->options & PCNET32_PORT_ASEL)) {
1876 lp->a.write_bcr(ioaddr, 32,
1877 lp->a.read_bcr(ioaddr, 32) | 0x0080);
1878 /* disable Auto Negotiation, set 10Mpbs, HD */
1879 val = lp->a.read_bcr(ioaddr, 32) & ~0xb8;
1880 if (lp->options & PCNET32_PORT_FD)
1881 val |= 0x10;
1882 if (lp->options & PCNET32_PORT_100)
1883 val |= 0x08;
1884 lp->a.write_bcr(ioaddr, 32, val);
1885 } else {
1886 if (lp->options & PCNET32_PORT_ASEL) {
1887 lp->a.write_bcr(ioaddr, 32,
1888 lp->a.read_bcr(ioaddr,
1889 32) | 0x0080);
1890 /* enable auto negotiate, setup, disable fd */
1891 val = lp->a.read_bcr(ioaddr, 32) & ~0x98;
1892 val |= 0x20;
1893 lp->a.write_bcr(ioaddr, 32, val);
1894 }
1895 }
1896 } else {
1897 int first_phy = -1;
1898 u16 bmcr;
1899 u32 bcr9;
1900 struct ethtool_cmd ecmd;
1901
1902 /*
1903 * There is really no good other way to handle multiple PHYs
1904 * other than turning off all automatics
1905 */
1906 val = lp->a.read_bcr(ioaddr, 2);
1907 lp->a.write_bcr(ioaddr, 2, val & ~2);
1908 val = lp->a.read_bcr(ioaddr, 32);
1909 lp->a.write_bcr(ioaddr, 32, val & ~(1 << 7)); /* stop MII manager */
1910
1911 if (!(lp->options & PCNET32_PORT_ASEL)) {
1912 /* setup ecmd */
1913 ecmd.port = PORT_MII;
1914 ecmd.transceiver = XCVR_INTERNAL;
1915 ecmd.autoneg = AUTONEG_DISABLE;
1916 ecmd.speed =
1917 lp->
1918 options & PCNET32_PORT_100 ? SPEED_100 : SPEED_10;
1919 bcr9 = lp->a.read_bcr(ioaddr, 9);
1920
1921 if (lp->options & PCNET32_PORT_FD) {
1922 ecmd.duplex = DUPLEX_FULL;
1923 bcr9 |= (1 << 0);
1924 } else {
1925 ecmd.duplex = DUPLEX_HALF;
1926 bcr9 |= ~(1 << 0);
1927 }
1928 lp->a.write_bcr(ioaddr, 9, bcr9);
1929 }
1930
1931 for (i = 0; i < PCNET32_MAX_PHYS; i++) {
1932 if (lp->phymask & (1 << i)) {
1933 /* isolate all but the first PHY */
1934 bmcr = mdio_read(dev, i, MII_BMCR);
1935 if (first_phy == -1) {
1936 first_phy = i;
1937 mdio_write(dev, i, MII_BMCR,
1938 bmcr & ~BMCR_ISOLATE);
1939 } else {
1940 mdio_write(dev, i, MII_BMCR,
1941 bmcr | BMCR_ISOLATE);
1942 }
1943 /* use mii_ethtool_sset to setup PHY */
1944 lp->mii_if.phy_id = i;
1945 ecmd.phy_address = i;
1946 if (lp->options & PCNET32_PORT_ASEL) {
1947 mii_ethtool_gset(&lp->mii_if, &ecmd);
1948 ecmd.autoneg = AUTONEG_ENABLE;
1949 }
1950 mii_ethtool_sset(&lp->mii_if, &ecmd);
1951 }
1952 }
1953 lp->mii_if.phy_id = first_phy;
1954 if (netif_msg_link(lp))
1955 printk(KERN_INFO "%s: Using PHY number %d.\n",
1956 dev->name, first_phy);
1957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959#ifdef DO_DXSUFLO
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001960 if (lp->dxsuflo) { /* Disable transmit stop on underflow */
1961 val = lp->a.read_csr(ioaddr, 3);
1962 val |= 0x40;
1963 lp->a.write_csr(ioaddr, 3, val);
1964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965#endif
1966
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001967 lp->init_block.mode =
1968 le16_to_cpu((lp->options & PCNET32_PORT_PORTSEL) << 7);
1969 pcnet32_load_multicast(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001971 if (pcnet32_init_ring(dev)) {
1972 rc = -ENOMEM;
1973 goto err_free_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 }
Hubert WS Lineabf0412005-09-14 11:39:25 -07001975
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001976 /* Re-initialize the PCNET32, and start it when done. */
1977 lp->a.write_csr(ioaddr, 1, (lp->dma_addr +
1978 offsetof(struct pcnet32_private,
1979 init_block)) & 0xffff);
1980 lp->a.write_csr(ioaddr, 2,
1981 (lp->dma_addr +
1982 offsetof(struct pcnet32_private, init_block)) >> 16);
Hubert WS Lineabf0412005-09-14 11:39:25 -07001983
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001984 lp->a.write_csr(ioaddr, 4, 0x0915);
1985 lp->a.write_csr(ioaddr, 0, 0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001987 netif_start_queue(dev);
1988
1989 /* Print the link status and start the watchdog */
1990 pcnet32_check_media(dev, 1);
1991 mod_timer(&(lp->watchdog_timer), PCNET32_WATCHDOG_TIMEOUT);
1992
1993 i = 0;
1994 while (i++ < 100)
1995 if (lp->a.read_csr(ioaddr, 0) & 0x0100)
1996 break;
1997 /*
1998 * We used to clear the InitDone bit, 0x0100, here but Mark Stockton
1999 * reports that doing so triggers a bug in the '974.
2000 */
2001 lp->a.write_csr(ioaddr, 0, 0x0042);
2002
2003 if (netif_msg_ifup(lp))
2004 printk(KERN_DEBUG
2005 "%s: pcnet32 open after %d ticks, init block %#x csr0 %4.4x.\n",
2006 dev->name, i,
2007 (u32) (lp->dma_addr +
2008 offsetof(struct pcnet32_private, init_block)),
2009 lp->a.read_csr(ioaddr, 0));
2010
2011 spin_unlock_irqrestore(&lp->lock, flags);
2012
2013 return 0; /* Always succeed */
2014
2015 err_free_ring:
2016 /* free any allocated skbuffs */
Don Fryac5bfe42006-06-29 13:55:27 -07002017 pcnet32_purge_rx_ring(dev);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002018
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002019 /*
2020 * Switch back to 16bit mode to avoid problems with dumb
2021 * DOS packet driver after a warm reboot
2022 */
2023 lp->a.write_bcr(ioaddr, 20, 4);
2024
2025 err_free_irq:
2026 spin_unlock_irqrestore(&lp->lock, flags);
2027 free_irq(dev->irq, dev);
2028 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029}
2030
2031/*
2032 * The LANCE has been halted for one reason or another (busmaster memory
2033 * arbitration error, Tx FIFO underflow, driver stopped it to reconfigure,
2034 * etc.). Modern LANCE variants always reload their ring-buffer
2035 * configuration when restarted, so we must reinitialize our ring
2036 * context before restarting. As part of this reinitialization,
2037 * find all packets still on the Tx ring and pretend that they had been
2038 * sent (in effect, drop the packets on the floor) - the higher-level
2039 * protocols will time out and retransmit. It'd be better to shuffle
2040 * these skbs to a temp list and then actually re-Tx them after
2041 * restarting the chip, but I'm too lazy to do so right now. dplatt@3do.com
2042 */
2043
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002044static void pcnet32_purge_tx_ring(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002046 struct pcnet32_private *lp = dev->priv;
2047 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002049 for (i = 0; i < lp->tx_ring_size; i++) {
2050 lp->tx_ring[i].status = 0; /* CPU owns buffer */
2051 wmb(); /* Make sure adapter sees owner change */
2052 if (lp->tx_skbuff[i]) {
2053 pci_unmap_single(lp->pci_dev, lp->tx_dma_addr[i],
2054 lp->tx_skbuff[i]->len,
2055 PCI_DMA_TODEVICE);
2056 dev_kfree_skb_any(lp->tx_skbuff[i]);
2057 }
2058 lp->tx_skbuff[i] = NULL;
2059 lp->tx_dma_addr[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061}
2062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063/* Initialize the PCNET32 Rx and Tx rings. */
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002064static int pcnet32_init_ring(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002066 struct pcnet32_private *lp = dev->priv;
2067 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002069 lp->tx_full = 0;
2070 lp->cur_rx = lp->cur_tx = 0;
2071 lp->dirty_rx = lp->dirty_tx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002073 for (i = 0; i < lp->rx_ring_size; i++) {
2074 struct sk_buff *rx_skbuff = lp->rx_skbuff[i];
2075 if (rx_skbuff == NULL) {
2076 if (!
2077 (rx_skbuff = lp->rx_skbuff[i] =
2078 dev_alloc_skb(PKT_BUF_SZ))) {
2079 /* there is not much, we can do at this point */
2080 if (pcnet32_debug & NETIF_MSG_DRV)
2081 printk(KERN_ERR
2082 "%s: pcnet32_init_ring dev_alloc_skb failed.\n",
2083 dev->name);
2084 return -1;
2085 }
2086 skb_reserve(rx_skbuff, 2);
2087 }
2088
2089 rmb();
2090 if (lp->rx_dma_addr[i] == 0)
2091 lp->rx_dma_addr[i] =
2092 pci_map_single(lp->pci_dev, rx_skbuff->data,
2093 PKT_BUF_SZ - 2, PCI_DMA_FROMDEVICE);
2094 lp->rx_ring[i].base = (u32) le32_to_cpu(lp->rx_dma_addr[i]);
2095 lp->rx_ring[i].buf_length = le16_to_cpu(2 - PKT_BUF_SZ);
2096 wmb(); /* Make sure owner changes after all others are visible */
2097 lp->rx_ring[i].status = le16_to_cpu(0x8000);
2098 }
2099 /* The Tx buffer address is filled in as needed, but we do need to clear
2100 * the upper ownership bit. */
2101 for (i = 0; i < lp->tx_ring_size; i++) {
2102 lp->tx_ring[i].status = 0; /* CPU owns buffer */
2103 wmb(); /* Make sure adapter sees owner change */
2104 lp->tx_ring[i].base = 0;
2105 lp->tx_dma_addr[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
2107
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002108 lp->init_block.tlen_rlen =
2109 le16_to_cpu(lp->tx_len_bits | lp->rx_len_bits);
2110 for (i = 0; i < 6; i++)
2111 lp->init_block.phys_addr[i] = dev->dev_addr[i];
2112 lp->init_block.rx_ring = (u32) le32_to_cpu(lp->rx_ring_dma_addr);
2113 lp->init_block.tx_ring = (u32) le32_to_cpu(lp->tx_ring_dma_addr);
2114 wmb(); /* Make sure all changes are visible */
2115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117
2118/* the pcnet32 has been issued a stop or reset. Wait for the stop bit
2119 * then flush the pending transmit operations, re-initialize the ring,
2120 * and tell the chip to initialize.
2121 */
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002122static void pcnet32_restart(struct net_device *dev, unsigned int csr0_bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002124 struct pcnet32_private *lp = dev->priv;
2125 unsigned long ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002128 /* wait for stop */
2129 for (i = 0; i < 100; i++)
2130 if (lp->a.read_csr(ioaddr, 0) & 0x0004)
2131 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002133 if (i >= 100 && netif_msg_drv(lp))
2134 printk(KERN_ERR
2135 "%s: pcnet32_restart timed out waiting for stop.\n",
2136 dev->name);
2137
2138 pcnet32_purge_tx_ring(dev);
2139 if (pcnet32_init_ring(dev))
2140 return;
2141
2142 /* ReInit Ring */
2143 lp->a.write_csr(ioaddr, 0, 1);
2144 i = 0;
2145 while (i++ < 1000)
2146 if (lp->a.read_csr(ioaddr, 0) & 0x0100)
2147 break;
2148
2149 lp->a.write_csr(ioaddr, 0, csr0_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002152static void pcnet32_tx_timeout(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002154 struct pcnet32_private *lp = dev->priv;
2155 unsigned long ioaddr = dev->base_addr, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002157 spin_lock_irqsave(&lp->lock, flags);
2158 /* Transmitter timeout, serious problems. */
2159 if (pcnet32_debug & NETIF_MSG_DRV)
2160 printk(KERN_ERR
2161 "%s: transmit timed out, status %4.4x, resetting.\n",
2162 dev->name, lp->a.read_csr(ioaddr, 0));
2163 lp->a.write_csr(ioaddr, 0, 0x0004);
2164 lp->stats.tx_errors++;
2165 if (netif_msg_tx_err(lp)) {
2166 int i;
2167 printk(KERN_DEBUG
2168 " Ring data dump: dirty_tx %d cur_tx %d%s cur_rx %d.",
2169 lp->dirty_tx, lp->cur_tx, lp->tx_full ? " (full)" : "",
2170 lp->cur_rx);
2171 for (i = 0; i < lp->rx_ring_size; i++)
2172 printk("%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
2173 le32_to_cpu(lp->rx_ring[i].base),
2174 (-le16_to_cpu(lp->rx_ring[i].buf_length)) &
2175 0xffff, le32_to_cpu(lp->rx_ring[i].msg_length),
2176 le16_to_cpu(lp->rx_ring[i].status));
2177 for (i = 0; i < lp->tx_ring_size; i++)
2178 printk("%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
2179 le32_to_cpu(lp->tx_ring[i].base),
2180 (-le16_to_cpu(lp->tx_ring[i].length)) & 0xffff,
2181 le32_to_cpu(lp->tx_ring[i].misc),
2182 le16_to_cpu(lp->tx_ring[i].status));
2183 printk("\n");
2184 }
2185 pcnet32_restart(dev, 0x0042);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002187 dev->trans_start = jiffies;
2188 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002190 spin_unlock_irqrestore(&lp->lock, flags);
2191}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002193static int pcnet32_start_xmit(struct sk_buff *skb, struct net_device *dev)
2194{
2195 struct pcnet32_private *lp = dev->priv;
2196 unsigned long ioaddr = dev->base_addr;
2197 u16 status;
2198 int entry;
2199 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002201 spin_lock_irqsave(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002203 if (netif_msg_tx_queued(lp)) {
2204 printk(KERN_DEBUG
2205 "%s: pcnet32_start_xmit() called, csr0 %4.4x.\n",
2206 dev->name, lp->a.read_csr(ioaddr, 0));
2207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002209 /* Default status -- will not enable Successful-TxDone
2210 * interrupt when that option is available to us.
2211 */
2212 status = 0x8300;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002214 /* Fill in a Tx ring entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002216 /* Mask to ring buffer boundary. */
2217 entry = lp->cur_tx & lp->tx_mod_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002219 /* Caution: the write order is important here, set the status
2220 * with the "ownership" bits last. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002222 lp->tx_ring[entry].length = le16_to_cpu(-skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002224 lp->tx_ring[entry].misc = 0x00000000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002226 lp->tx_skbuff[entry] = skb;
2227 lp->tx_dma_addr[entry] =
2228 pci_map_single(lp->pci_dev, skb->data, skb->len, PCI_DMA_TODEVICE);
2229 lp->tx_ring[entry].base = (u32) le32_to_cpu(lp->tx_dma_addr[entry]);
2230 wmb(); /* Make sure owner changes after all others are visible */
2231 lp->tx_ring[entry].status = le16_to_cpu(status);
2232
2233 lp->cur_tx++;
2234 lp->stats.tx_bytes += skb->len;
2235
2236 /* Trigger an immediate send poll. */
2237 lp->a.write_csr(ioaddr, 0, 0x0048);
2238
2239 dev->trans_start = jiffies;
2240
2241 if (lp->tx_ring[(entry + 1) & lp->tx_mod_mask].base != 0) {
2242 lp->tx_full = 1;
2243 netif_stop_queue(dev);
2244 }
2245 spin_unlock_irqrestore(&lp->lock, flags);
2246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247}
2248
2249/* The PCNET32 interrupt handler. */
2250static irqreturn_t
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002251pcnet32_interrupt(int irq, void *dev_id, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002253 struct net_device *dev = dev_id;
2254 struct pcnet32_private *lp;
2255 unsigned long ioaddr;
2256 u16 csr0, rap;
2257 int boguscnt = max_interrupt_work;
2258 int must_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002260 if (!dev) {
2261 if (pcnet32_debug & NETIF_MSG_INTR)
2262 printk(KERN_DEBUG "%s(): irq %d for unknown device\n",
2263 __FUNCTION__, irq);
2264 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002267 ioaddr = dev->base_addr;
2268 lp = dev->priv;
2269
2270 spin_lock(&lp->lock);
2271
2272 rap = lp->a.read_rap(ioaddr);
2273 while ((csr0 = lp->a.read_csr(ioaddr, 0)) & 0x8f00 && --boguscnt >= 0) {
2274 if (csr0 == 0xffff) {
2275 break; /* PCMCIA remove happened */
2276 }
2277 /* Acknowledge all of the current interrupt sources ASAP. */
2278 lp->a.write_csr(ioaddr, 0, csr0 & ~0x004f);
2279
2280 must_restart = 0;
2281
2282 if (netif_msg_intr(lp))
2283 printk(KERN_DEBUG
2284 "%s: interrupt csr0=%#2.2x new csr=%#2.2x.\n",
2285 dev->name, csr0, lp->a.read_csr(ioaddr, 0));
2286
2287 if (csr0 & 0x0400) /* Rx interrupt */
2288 pcnet32_rx(dev);
2289
2290 if (csr0 & 0x0200) { /* Tx-done interrupt */
2291 unsigned int dirty_tx = lp->dirty_tx;
2292 int delta;
2293
2294 while (dirty_tx != lp->cur_tx) {
2295 int entry = dirty_tx & lp->tx_mod_mask;
2296 int status =
2297 (short)le16_to_cpu(lp->tx_ring[entry].
2298 status);
2299
2300 if (status < 0)
2301 break; /* It still hasn't been Txed */
2302
2303 lp->tx_ring[entry].base = 0;
2304
2305 if (status & 0x4000) {
2306 /* There was an major error, log it. */
2307 int err_status =
2308 le32_to_cpu(lp->tx_ring[entry].
2309 misc);
2310 lp->stats.tx_errors++;
2311 if (netif_msg_tx_err(lp))
2312 printk(KERN_ERR
2313 "%s: Tx error status=%04x err_status=%08x\n",
2314 dev->name, status,
2315 err_status);
2316 if (err_status & 0x04000000)
2317 lp->stats.tx_aborted_errors++;
2318 if (err_status & 0x08000000)
2319 lp->stats.tx_carrier_errors++;
2320 if (err_status & 0x10000000)
2321 lp->stats.tx_window_errors++;
2322#ifndef DO_DXSUFLO
2323 if (err_status & 0x40000000) {
2324 lp->stats.tx_fifo_errors++;
2325 /* Ackk! On FIFO errors the Tx unit is turned off! */
2326 /* Remove this verbosity later! */
2327 if (netif_msg_tx_err(lp))
2328 printk(KERN_ERR
2329 "%s: Tx FIFO error! CSR0=%4.4x\n",
2330 dev->name, csr0);
2331 must_restart = 1;
2332 }
2333#else
2334 if (err_status & 0x40000000) {
2335 lp->stats.tx_fifo_errors++;
2336 if (!lp->dxsuflo) { /* If controller doesn't recover ... */
2337 /* Ackk! On FIFO errors the Tx unit is turned off! */
2338 /* Remove this verbosity later! */
2339 if (netif_msg_tx_err
2340 (lp))
2341 printk(KERN_ERR
2342 "%s: Tx FIFO error! CSR0=%4.4x\n",
2343 dev->
2344 name,
2345 csr0);
2346 must_restart = 1;
2347 }
2348 }
2349#endif
2350 } else {
2351 if (status & 0x1800)
2352 lp->stats.collisions++;
2353 lp->stats.tx_packets++;
2354 }
2355
2356 /* We must free the original skb */
2357 if (lp->tx_skbuff[entry]) {
2358 pci_unmap_single(lp->pci_dev,
2359 lp->tx_dma_addr[entry],
2360 lp->tx_skbuff[entry]->
2361 len, PCI_DMA_TODEVICE);
2362 dev_kfree_skb_irq(lp->tx_skbuff[entry]);
2363 lp->tx_skbuff[entry] = NULL;
2364 lp->tx_dma_addr[entry] = 0;
2365 }
2366 dirty_tx++;
2367 }
2368
2369 delta =
2370 (lp->cur_tx - dirty_tx) & (lp->tx_mod_mask +
2371 lp->tx_ring_size);
2372 if (delta > lp->tx_ring_size) {
2373 if (netif_msg_drv(lp))
2374 printk(KERN_ERR
2375 "%s: out-of-sync dirty pointer, %d vs. %d, full=%d.\n",
2376 dev->name, dirty_tx, lp->cur_tx,
2377 lp->tx_full);
2378 dirty_tx += lp->tx_ring_size;
2379 delta -= lp->tx_ring_size;
2380 }
2381
2382 if (lp->tx_full &&
2383 netif_queue_stopped(dev) &&
2384 delta < lp->tx_ring_size - 2) {
2385 /* The ring is no longer full, clear tbusy. */
2386 lp->tx_full = 0;
2387 netif_wake_queue(dev);
2388 }
2389 lp->dirty_tx = dirty_tx;
2390 }
2391
2392 /* Log misc errors. */
2393 if (csr0 & 0x4000)
2394 lp->stats.tx_errors++; /* Tx babble. */
2395 if (csr0 & 0x1000) {
2396 /*
2397 * this happens when our receive ring is full. This shouldn't
2398 * be a problem as we will see normal rx interrupts for the frames
2399 * in the receive ring. But there are some PCI chipsets (I can
2400 * reproduce this on SP3G with Intel saturn chipset) which have
2401 * sometimes problems and will fill up the receive ring with
2402 * error descriptors. In this situation we don't get a rx
2403 * interrupt, but a missed frame interrupt sooner or later.
2404 * So we try to clean up our receive ring here.
2405 */
2406 pcnet32_rx(dev);
2407 lp->stats.rx_errors++; /* Missed a Rx frame. */
2408 }
2409 if (csr0 & 0x0800) {
2410 if (netif_msg_drv(lp))
2411 printk(KERN_ERR
2412 "%s: Bus master arbitration failure, status %4.4x.\n",
2413 dev->name, csr0);
2414 /* unlike for the lance, there is no restart needed */
2415 }
2416
2417 if (must_restart) {
2418 /* reset the chip to clear the error condition, then restart */
2419 lp->a.reset(ioaddr);
2420 lp->a.write_csr(ioaddr, 4, 0x0915);
2421 pcnet32_restart(dev, 0x0002);
2422 netif_wake_queue(dev);
2423 }
2424 }
2425
2426 /* Set interrupt enable. */
2427 lp->a.write_csr(ioaddr, 0, 0x0040);
2428 lp->a.write_rap(ioaddr, rap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
2430 if (netif_msg_intr(lp))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002431 printk(KERN_DEBUG "%s: exiting interrupt, csr0=%#4.4x.\n",
2432 dev->name, lp->a.read_csr(ioaddr, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002434 spin_unlock(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002436 return IRQ_HANDLED;
2437}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002439static int pcnet32_rx(struct net_device *dev)
2440{
2441 struct pcnet32_private *lp = dev->priv;
2442 int entry = lp->cur_rx & lp->rx_mod_mask;
2443 int boguscnt = lp->rx_ring_size / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002445 /* If we own the next entry, it's a new packet. Send it up. */
2446 while ((short)le16_to_cpu(lp->rx_ring[entry].status) >= 0) {
2447 int status = (short)le16_to_cpu(lp->rx_ring[entry].status) >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002449 if (status != 0x03) { /* There was an error. */
2450 /*
2451 * There is a tricky error noted by John Murphy,
2452 * <murf@perftech.com> to Russ Nelson: Even with full-sized
2453 * buffers it's possible for a jabber packet to use two
2454 * buffers, with only the last correctly noting the error.
2455 */
2456 if (status & 0x01) /* Only count a general error at the */
2457 lp->stats.rx_errors++; /* end of a packet. */
2458 if (status & 0x20)
2459 lp->stats.rx_frame_errors++;
2460 if (status & 0x10)
2461 lp->stats.rx_over_errors++;
2462 if (status & 0x08)
2463 lp->stats.rx_crc_errors++;
2464 if (status & 0x04)
2465 lp->stats.rx_fifo_errors++;
2466 lp->rx_ring[entry].status &= le16_to_cpu(0x03ff);
2467 } else {
2468 /* Malloc up new buffer, compatible with net-2e. */
2469 short pkt_len =
2470 (le32_to_cpu(lp->rx_ring[entry].msg_length) & 0xfff)
2471 - 4;
2472 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002474 /* Discard oversize frames. */
2475 if (unlikely(pkt_len > PKT_BUF_SZ - 2)) {
2476 if (netif_msg_drv(lp))
2477 printk(KERN_ERR
2478 "%s: Impossible packet size %d!\n",
2479 dev->name, pkt_len);
2480 lp->stats.rx_errors++;
2481 } else if (pkt_len < 60) {
2482 if (netif_msg_rx_err(lp))
2483 printk(KERN_ERR "%s: Runt packet!\n",
2484 dev->name);
2485 lp->stats.rx_errors++;
2486 } else {
2487 int rx_in_place = 0;
2488
2489 if (pkt_len > rx_copybreak) {
2490 struct sk_buff *newskb;
2491
2492 if ((newskb =
2493 dev_alloc_skb(PKT_BUF_SZ))) {
2494 skb_reserve(newskb, 2);
2495 skb = lp->rx_skbuff[entry];
2496 pci_unmap_single(lp->pci_dev,
2497 lp->
2498 rx_dma_addr
2499 [entry],
2500 PKT_BUF_SZ - 2,
2501 PCI_DMA_FROMDEVICE);
2502 skb_put(skb, pkt_len);
2503 lp->rx_skbuff[entry] = newskb;
2504 newskb->dev = dev;
2505 lp->rx_dma_addr[entry] =
2506 pci_map_single(lp->pci_dev,
2507 newskb->data,
2508 PKT_BUF_SZ -
2509 2,
2510 PCI_DMA_FROMDEVICE);
2511 lp->rx_ring[entry].base =
2512 le32_to_cpu(lp->
2513 rx_dma_addr
2514 [entry]);
2515 rx_in_place = 1;
2516 } else
2517 skb = NULL;
2518 } else {
2519 skb = dev_alloc_skb(pkt_len + 2);
2520 }
2521
2522 if (skb == NULL) {
2523 int i;
2524 if (netif_msg_drv(lp))
2525 printk(KERN_ERR
2526 "%s: Memory squeeze, deferring packet.\n",
2527 dev->name);
2528 for (i = 0; i < lp->rx_ring_size; i++)
2529 if ((short)
2530 le16_to_cpu(lp->
2531 rx_ring[(entry +
2532 i)
2533 & lp->
2534 rx_mod_mask].
2535 status) < 0)
2536 break;
2537
2538 if (i > lp->rx_ring_size - 2) {
2539 lp->stats.rx_dropped++;
2540 lp->rx_ring[entry].status |=
2541 le16_to_cpu(0x8000);
2542 wmb(); /* Make sure adapter sees owner change */
2543 lp->cur_rx++;
2544 }
2545 break;
2546 }
2547 skb->dev = dev;
2548 if (!rx_in_place) {
2549 skb_reserve(skb, 2); /* 16 byte align */
2550 skb_put(skb, pkt_len); /* Make room */
2551 pci_dma_sync_single_for_cpu(lp->pci_dev,
2552 lp->
2553 rx_dma_addr
2554 [entry],
2555 PKT_BUF_SZ -
2556 2,
2557 PCI_DMA_FROMDEVICE);
2558 eth_copy_and_sum(skb,
2559 (unsigned char *)(lp->
2560 rx_skbuff
2561 [entry]->
2562 data),
2563 pkt_len, 0);
2564 pci_dma_sync_single_for_device(lp->
2565 pci_dev,
2566 lp->
2567 rx_dma_addr
2568 [entry],
2569 PKT_BUF_SZ
2570 - 2,
2571 PCI_DMA_FROMDEVICE);
2572 }
2573 lp->stats.rx_bytes += skb->len;
2574 skb->protocol = eth_type_trans(skb, dev);
2575 netif_rx(skb);
2576 dev->last_rx = jiffies;
2577 lp->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 }
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002580 /*
2581 * The docs say that the buffer length isn't touched, but Andrew Boyd
2582 * of QNX reports that some revs of the 79C965 clear it.
2583 */
2584 lp->rx_ring[entry].buf_length = le16_to_cpu(2 - PKT_BUF_SZ);
2585 wmb(); /* Make sure owner changes after all others are visible */
2586 lp->rx_ring[entry].status |= le16_to_cpu(0x8000);
2587 entry = (++lp->cur_rx) & lp->rx_mod_mask;
2588 if (--boguscnt <= 0)
2589 break; /* don't stay in loop forever */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 }
2591
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002592 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593}
2594
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002595static int pcnet32_close(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002597 unsigned long ioaddr = dev->base_addr;
2598 struct pcnet32_private *lp = dev->priv;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002599 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002601 del_timer_sync(&lp->watchdog_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002603 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002605 spin_lock_irqsave(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002607 lp->stats.rx_missed_errors = lp->a.read_csr(ioaddr, 112);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002609 if (netif_msg_ifdown(lp))
2610 printk(KERN_DEBUG
2611 "%s: Shutting down ethercard, status was %2.2x.\n",
2612 dev->name, lp->a.read_csr(ioaddr, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002614 /* We stop the PCNET32 here -- it occasionally polls memory if we don't. */
2615 lp->a.write_csr(ioaddr, 0, 0x0004);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 /*
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002618 * Switch back to 16bit mode to avoid problems with dumb
2619 * DOS packet driver after a warm reboot
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 */
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002621 lp->a.write_bcr(ioaddr, 20, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002623 spin_unlock_irqrestore(&lp->lock, flags);
2624
2625 free_irq(dev->irq, dev);
2626
2627 spin_lock_irqsave(&lp->lock, flags);
2628
Don Fryac5bfe42006-06-29 13:55:27 -07002629 pcnet32_purge_rx_ring(dev);
2630 pcnet32_purge_tx_ring(dev);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002631
2632 spin_unlock_irqrestore(&lp->lock, flags);
2633
2634 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635}
2636
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002637static struct net_device_stats *pcnet32_get_stats(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002639 struct pcnet32_private *lp = dev->priv;
2640 unsigned long ioaddr = dev->base_addr;
2641 u16 saved_addr;
2642 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002644 spin_lock_irqsave(&lp->lock, flags);
2645 saved_addr = lp->a.read_rap(ioaddr);
2646 lp->stats.rx_missed_errors = lp->a.read_csr(ioaddr, 112);
2647 lp->a.write_rap(ioaddr, saved_addr);
2648 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002650 return &lp->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651}
2652
2653/* taken from the sunlance driver, which it took from the depca driver */
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002654static void pcnet32_load_multicast(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002656 struct pcnet32_private *lp = dev->priv;
2657 volatile struct pcnet32_init_block *ib = &lp->init_block;
2658 volatile u16 *mcast_table = (u16 *) & ib->filter;
2659 struct dev_mc_list *dmi = dev->mc_list;
Don Frydf27f4a2006-06-29 13:55:02 -07002660 unsigned long ioaddr = dev->base_addr;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002661 char *addrs;
2662 int i;
2663 u32 crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002665 /* set all multicast bits */
2666 if (dev->flags & IFF_ALLMULTI) {
2667 ib->filter[0] = 0xffffffff;
2668 ib->filter[1] = 0xffffffff;
Don Frydf27f4a2006-06-29 13:55:02 -07002669 lp->a.write_csr(ioaddr, PCNET32_MC_FILTER, 0xffff);
2670 lp->a.write_csr(ioaddr, PCNET32_MC_FILTER+1, 0xffff);
2671 lp->a.write_csr(ioaddr, PCNET32_MC_FILTER+2, 0xffff);
2672 lp->a.write_csr(ioaddr, PCNET32_MC_FILTER+3, 0xffff);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002673 return;
2674 }
2675 /* clear the multicast filter */
2676 ib->filter[0] = 0;
2677 ib->filter[1] = 0;
2678
2679 /* Add addresses */
2680 for (i = 0; i < dev->mc_count; i++) {
2681 addrs = dmi->dmi_addr;
2682 dmi = dmi->next;
2683
2684 /* multicast address? */
2685 if (!(*addrs & 1))
2686 continue;
2687
2688 crc = ether_crc_le(6, addrs);
2689 crc = crc >> 26;
2690 mcast_table[crc >> 4] =
2691 le16_to_cpu(le16_to_cpu(mcast_table[crc >> 4]) |
2692 (1 << (crc & 0xf)));
2693 }
Don Frydf27f4a2006-06-29 13:55:02 -07002694 for (i = 0; i < 4; i++)
2695 lp->a.write_csr(ioaddr, PCNET32_MC_FILTER + i,
2696 le16_to_cpu(mcast_table[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698}
2699
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700/*
2701 * Set or clear the multicast filter for this adaptor.
2702 */
2703static void pcnet32_set_multicast_list(struct net_device *dev)
2704{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002705 unsigned long ioaddr = dev->base_addr, flags;
2706 struct pcnet32_private *lp = dev->priv;
Don Frydf27f4a2006-06-29 13:55:02 -07002707 int csr15, suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002709 spin_lock_irqsave(&lp->lock, flags);
Don Frydf27f4a2006-06-29 13:55:02 -07002710 suspended = pcnet32_suspend(dev, &flags, 0);
2711 csr15 = lp->a.read_csr(ioaddr, CSR15);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002712 if (dev->flags & IFF_PROMISC) {
2713 /* Log any net taps. */
2714 if (netif_msg_hw(lp))
2715 printk(KERN_INFO "%s: Promiscuous mode enabled.\n",
2716 dev->name);
2717 lp->init_block.mode =
2718 le16_to_cpu(0x8000 | (lp->options & PCNET32_PORT_PORTSEL) <<
2719 7);
Don Frydf27f4a2006-06-29 13:55:02 -07002720 lp->a.write_csr(ioaddr, CSR15, csr15 | 0x8000);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002721 } else {
2722 lp->init_block.mode =
2723 le16_to_cpu((lp->options & PCNET32_PORT_PORTSEL) << 7);
Don Frydf27f4a2006-06-29 13:55:02 -07002724 lp->a.write_csr(ioaddr, CSR15, csr15 & 0x7fff);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002725 pcnet32_load_multicast(dev);
2726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
Don Frydf27f4a2006-06-29 13:55:02 -07002728 if (suspended) {
2729 int csr5;
2730 /* clear SUSPEND (SPND) - CSR5 bit 0 */
2731 csr5 = lp->a.read_csr(ioaddr, CSR5);
2732 lp->a.write_csr(ioaddr, CSR5, csr5 & (~CSR5_SUSPEND));
2733 } else {
2734 lp->a.write_csr(ioaddr, CSR0, CSR0_STOP);
2735 pcnet32_restart(dev, CSR0_NORMAL);
2736 netif_wake_queue(dev);
2737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002739 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740}
2741
2742/* This routine assumes that the lp->lock is held */
2743static int mdio_read(struct net_device *dev, int phy_id, int reg_num)
2744{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002745 struct pcnet32_private *lp = dev->priv;
2746 unsigned long ioaddr = dev->base_addr;
2747 u16 val_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002749 if (!lp->mii)
2750 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002752 lp->a.write_bcr(ioaddr, 33, ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
2753 val_out = lp->a.read_bcr(ioaddr, 34);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002755 return val_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756}
2757
2758/* This routine assumes that the lp->lock is held */
2759static void mdio_write(struct net_device *dev, int phy_id, int reg_num, int val)
2760{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002761 struct pcnet32_private *lp = dev->priv;
2762 unsigned long ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002764 if (!lp->mii)
2765 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002767 lp->a.write_bcr(ioaddr, 33, ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
2768 lp->a.write_bcr(ioaddr, 34, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769}
2770
2771static int pcnet32_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2772{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002773 struct pcnet32_private *lp = dev->priv;
2774 int rc;
2775 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002777 /* SIOC[GS]MIIxxx ioctls */
2778 if (lp->mii) {
2779 spin_lock_irqsave(&lp->lock, flags);
2780 rc = generic_mii_ioctl(&lp->mii_if, if_mii(rq), cmd, NULL);
2781 spin_unlock_irqrestore(&lp->lock, flags);
2782 } else {
2783 rc = -EOPNOTSUPP;
2784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002786 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787}
2788
Don Fryac62ef02006-03-20 15:26:03 -08002789static int pcnet32_check_otherphy(struct net_device *dev)
2790{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002791 struct pcnet32_private *lp = dev->priv;
2792 struct mii_if_info mii = lp->mii_if;
2793 u16 bmcr;
2794 int i;
Don Fryac62ef02006-03-20 15:26:03 -08002795
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002796 for (i = 0; i < PCNET32_MAX_PHYS; i++) {
2797 if (i == lp->mii_if.phy_id)
2798 continue; /* skip active phy */
2799 if (lp->phymask & (1 << i)) {
2800 mii.phy_id = i;
2801 if (mii_link_ok(&mii)) {
2802 /* found PHY with active link */
2803 if (netif_msg_link(lp))
2804 printk(KERN_INFO
2805 "%s: Using PHY number %d.\n",
2806 dev->name, i);
Don Fryac62ef02006-03-20 15:26:03 -08002807
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002808 /* isolate inactive phy */
2809 bmcr =
2810 mdio_read(dev, lp->mii_if.phy_id, MII_BMCR);
2811 mdio_write(dev, lp->mii_if.phy_id, MII_BMCR,
2812 bmcr | BMCR_ISOLATE);
Don Fryac62ef02006-03-20 15:26:03 -08002813
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002814 /* de-isolate new phy */
2815 bmcr = mdio_read(dev, i, MII_BMCR);
2816 mdio_write(dev, i, MII_BMCR,
2817 bmcr & ~BMCR_ISOLATE);
Don Fryac62ef02006-03-20 15:26:03 -08002818
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002819 /* set new phy address */
2820 lp->mii_if.phy_id = i;
2821 return 1;
2822 }
2823 }
Don Fryac62ef02006-03-20 15:26:03 -08002824 }
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002825 return 0;
Don Fryac62ef02006-03-20 15:26:03 -08002826}
2827
2828/*
2829 * Show the status of the media. Similar to mii_check_media however it
2830 * correctly shows the link speed for all (tested) pcnet32 variants.
2831 * Devices with no mii just report link state without speed.
2832 *
2833 * Caller is assumed to hold and release the lp->lock.
2834 */
2835
2836static void pcnet32_check_media(struct net_device *dev, int verbose)
2837{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002838 struct pcnet32_private *lp = dev->priv;
2839 int curr_link;
2840 int prev_link = netif_carrier_ok(dev) ? 1 : 0;
2841 u32 bcr9;
Don Fryac62ef02006-03-20 15:26:03 -08002842
Don Fryac62ef02006-03-20 15:26:03 -08002843 if (lp->mii) {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002844 curr_link = mii_link_ok(&lp->mii_if);
Don Fryac62ef02006-03-20 15:26:03 -08002845 } else {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002846 ulong ioaddr = dev->base_addr; /* card base I/O address */
2847 curr_link = (lp->a.read_bcr(ioaddr, 4) != 0xc0);
Don Fryac62ef02006-03-20 15:26:03 -08002848 }
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002849 if (!curr_link) {
2850 if (prev_link || verbose) {
2851 netif_carrier_off(dev);
2852 if (netif_msg_link(lp))
2853 printk(KERN_INFO "%s: link down\n", dev->name);
2854 }
2855 if (lp->phycount > 1) {
2856 curr_link = pcnet32_check_otherphy(dev);
2857 prev_link = 0;
2858 }
2859 } else if (verbose || !prev_link) {
2860 netif_carrier_on(dev);
2861 if (lp->mii) {
2862 if (netif_msg_link(lp)) {
2863 struct ethtool_cmd ecmd;
2864 mii_ethtool_gset(&lp->mii_if, &ecmd);
2865 printk(KERN_INFO
2866 "%s: link up, %sMbps, %s-duplex\n",
2867 dev->name,
2868 (ecmd.speed == SPEED_100) ? "100" : "10",
2869 (ecmd.duplex ==
2870 DUPLEX_FULL) ? "full" : "half");
2871 }
2872 bcr9 = lp->a.read_bcr(dev->base_addr, 9);
2873 if ((bcr9 & (1 << 0)) != lp->mii_if.full_duplex) {
2874 if (lp->mii_if.full_duplex)
2875 bcr9 |= (1 << 0);
2876 else
2877 bcr9 &= ~(1 << 0);
2878 lp->a.write_bcr(dev->base_addr, 9, bcr9);
2879 }
2880 } else {
2881 if (netif_msg_link(lp))
2882 printk(KERN_INFO "%s: link up\n", dev->name);
2883 }
2884 }
Don Fryac62ef02006-03-20 15:26:03 -08002885}
2886
2887/*
2888 * Check for loss of link and link establishment.
2889 * Can not use mii_check_media because it does nothing if mode is forced.
2890 */
2891
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892static void pcnet32_watchdog(struct net_device *dev)
2893{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002894 struct pcnet32_private *lp = dev->priv;
2895 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002897 /* Print the link status if it has changed */
2898 spin_lock_irqsave(&lp->lock, flags);
2899 pcnet32_check_media(dev, 0);
2900 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002902 mod_timer(&(lp->watchdog_timer), PCNET32_WATCHDOG_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903}
2904
2905static void __devexit pcnet32_remove_one(struct pci_dev *pdev)
2906{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002907 struct net_device *dev = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002909 if (dev) {
2910 struct pcnet32_private *lp = dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002912 unregister_netdev(dev);
2913 pcnet32_free_ring(dev);
2914 release_region(dev->base_addr, PCNET32_TOTAL_SIZE);
2915 pci_free_consistent(lp->pci_dev, sizeof(*lp), lp, lp->dma_addr);
2916 free_netdev(dev);
2917 pci_disable_device(pdev);
2918 pci_set_drvdata(pdev, NULL);
2919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920}
2921
2922static struct pci_driver pcnet32_driver = {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002923 .name = DRV_NAME,
2924 .probe = pcnet32_probe_pci,
2925 .remove = __devexit_p(pcnet32_remove_one),
2926 .id_table = pcnet32_pci_tbl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927};
2928
2929/* An additional parameter that may be passed in... */
2930static int debug = -1;
2931static int tx_start_pt = -1;
2932static int pcnet32_have_pci;
2933
2934module_param(debug, int, 0);
2935MODULE_PARM_DESC(debug, DRV_NAME " debug level");
2936module_param(max_interrupt_work, int, 0);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002937MODULE_PARM_DESC(max_interrupt_work,
2938 DRV_NAME " maximum events handled per interrupt");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939module_param(rx_copybreak, int, 0);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002940MODULE_PARM_DESC(rx_copybreak,
2941 DRV_NAME " copy breakpoint for copy-only-tiny-frames");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942module_param(tx_start_pt, int, 0);
2943MODULE_PARM_DESC(tx_start_pt, DRV_NAME " transmit start point (0-3)");
2944module_param(pcnet32vlb, int, 0);
2945MODULE_PARM_DESC(pcnet32vlb, DRV_NAME " Vesa local bus (VLB) support (0/1)");
2946module_param_array(options, int, NULL, 0);
2947MODULE_PARM_DESC(options, DRV_NAME " initial option setting(s) (0-15)");
2948module_param_array(full_duplex, int, NULL, 0);
2949MODULE_PARM_DESC(full_duplex, DRV_NAME " full duplex setting(s) (1)");
2950/* Module Parameter for HomePNA cards added by Patrick Simmons, 2004 */
2951module_param_array(homepna, int, NULL, 0);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002952MODULE_PARM_DESC(homepna,
2953 DRV_NAME
2954 " mode for 79C978 cards (1 for HomePNA, 0 for Ethernet, default Ethernet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
2956MODULE_AUTHOR("Thomas Bogendoerfer");
2957MODULE_DESCRIPTION("Driver for PCnet32 and PCnetPCI based ethercards");
2958MODULE_LICENSE("GPL");
2959
2960#define PCNET32_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
2961
2962static int __init pcnet32_init_module(void)
2963{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002964 printk(KERN_INFO "%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002966 pcnet32_debug = netif_msg_init(debug, PCNET32_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002968 if ((tx_start_pt >= 0) && (tx_start_pt <= 3))
2969 tx_start = tx_start_pt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002971 /* find the PCI devices */
2972 if (!pci_module_init(&pcnet32_driver))
2973 pcnet32_have_pci = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002975 /* should we find any remaining VLbus devices ? */
2976 if (pcnet32vlb)
Don Frydcaf9762006-06-29 13:52:29 -07002977 pcnet32_probe_vlbus(pcnet32_portlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002979 if (cards_found && (pcnet32_debug & NETIF_MSG_PROBE))
2980 printk(KERN_INFO PFX "%d cards_found.\n", cards_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002982 return (pcnet32_have_pci + cards_found) ? 0 : -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983}
2984
2985static void __exit pcnet32_cleanup_module(void)
2986{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002987 struct net_device *next_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002989 while (pcnet32_dev) {
2990 struct pcnet32_private *lp = pcnet32_dev->priv;
2991 next_dev = lp->next;
2992 unregister_netdev(pcnet32_dev);
2993 pcnet32_free_ring(pcnet32_dev);
2994 release_region(pcnet32_dev->base_addr, PCNET32_TOTAL_SIZE);
2995 pci_free_consistent(lp->pci_dev, sizeof(*lp), lp, lp->dma_addr);
2996 free_netdev(pcnet32_dev);
2997 pcnet32_dev = next_dev;
2998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05003000 if (pcnet32_have_pci)
3001 pci_unregister_driver(&pcnet32_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002}
3003
3004module_init(pcnet32_init_module);
3005module_exit(pcnet32_cleanup_module);
3006
3007/*
3008 * Local variables:
3009 * c-indent-level: 4
3010 * tab-width: 8
3011 * End:
3012 */