blob: 07c31f19c6ba40d419b2d022d069af321130f73f [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[] = {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -050061 { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE_HOME,
62 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
63 { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE,
64 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
65
66 /*
67 * Adapters that were sold with IBM's RS/6000 or pSeries hardware have
68 * the incorrect vendor id.
69 */
70 { PCI_VENDOR_ID_TRIDENT, PCI_DEVICE_ID_AMD_LANCE,
71 PCI_ANY_ID, PCI_ANY_ID,
72 PCI_CLASS_NETWORK_ETHERNET << 8, 0xffff00, 0},
73
74 { } /* terminate list */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075};
76
Jeff Garzik4a5e8e22006-03-21 16:15:44 -050077MODULE_DEVICE_TABLE(pci, pcnet32_pci_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79static int cards_found;
80
81/*
82 * VLB I/O addresses
83 */
84static unsigned int pcnet32_portlist[] __initdata =
Jeff Garzik4a5e8e22006-03-21 16:15:44 -050085 { 0x300, 0x320, 0x340, 0x360, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87static int pcnet32_debug = 0;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -050088static int tx_start = 1; /* Mapping -- 0:20, 1:64, 2:128, 3:~220 (depends on chip vers) */
89static int pcnet32vlb; /* check for VLB cards ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91static struct net_device *pcnet32_dev;
92
93static int max_interrupt_work = 2;
94static int rx_copybreak = 200;
95
96#define PCNET32_PORT_AUI 0x00
97#define PCNET32_PORT_10BT 0x01
98#define PCNET32_PORT_GPSI 0x02
99#define PCNET32_PORT_MII 0x03
100
101#define PCNET32_PORT_PORTSEL 0x03
102#define PCNET32_PORT_ASEL 0x04
103#define PCNET32_PORT_100 0x40
104#define PCNET32_PORT_FD 0x80
105
106#define PCNET32_DMA_MASK 0xffffffff
107
108#define PCNET32_WATCHDOG_TIMEOUT (jiffies + (2 * HZ))
109#define PCNET32_BLINK_TIMEOUT (jiffies + (HZ/4))
110
111/*
112 * table to translate option values from tulip
113 * to internal options
114 */
Arjan van de Venf71e1302006-03-03 21:33:57 -0500115static const unsigned char options_mapping[] = {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500116 PCNET32_PORT_ASEL, /* 0 Auto-select */
117 PCNET32_PORT_AUI, /* 1 BNC/AUI */
118 PCNET32_PORT_AUI, /* 2 AUI/BNC */
119 PCNET32_PORT_ASEL, /* 3 not supported */
120 PCNET32_PORT_10BT | PCNET32_PORT_FD, /* 4 10baseT-FD */
121 PCNET32_PORT_ASEL, /* 5 not supported */
122 PCNET32_PORT_ASEL, /* 6 not supported */
123 PCNET32_PORT_ASEL, /* 7 not supported */
124 PCNET32_PORT_ASEL, /* 8 not supported */
125 PCNET32_PORT_MII, /* 9 MII 10baseT */
126 PCNET32_PORT_MII | PCNET32_PORT_FD, /* 10 MII 10baseT-FD */
127 PCNET32_PORT_MII, /* 11 MII (autosel) */
128 PCNET32_PORT_10BT, /* 12 10BaseT */
129 PCNET32_PORT_MII | PCNET32_PORT_100, /* 13 MII 100BaseTx */
130 /* 14 MII 100BaseTx-FD */
131 PCNET32_PORT_MII | PCNET32_PORT_100 | PCNET32_PORT_FD,
132 PCNET32_PORT_ASEL /* 15 not supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
135static const char pcnet32_gstrings_test[][ETH_GSTRING_LEN] = {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500136 "Loopback test (offline)"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#define PCNET32_TEST_LEN (sizeof(pcnet32_gstrings_test) / ETH_GSTRING_LEN)
140
Don Fryac62ef02006-03-20 15:26:03 -0800141#define PCNET32_NUM_REGS 136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500143#define MAX_UNITS 8 /* More are supported, limit only on options */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static int options[MAX_UNITS];
145static int full_duplex[MAX_UNITS];
146static int homepna[MAX_UNITS];
147
148/*
149 * Theory of Operation
150 *
151 * This driver uses the same software structure as the normal lance
152 * driver. So look for a verbose description in lance.c. The differences
153 * to the normal lance driver is the use of the 32bit mode of PCnet32
154 * and PCnetPCI chips. Because these chips are 32bit chips, there is no
155 * 16MB limitation and we don't need bounce buffers.
156 */
157
158/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 * Set the number of Tx and Rx buffers, using Log_2(# buffers).
160 * Reasonable default values are 4 Tx buffers, and 16 Rx buffers.
161 * That translates to 2 (4 == 2^^2) and 4 (16 == 2^^4).
162 */
163#ifndef PCNET32_LOG_TX_BUFFERS
Hubert WS Lineabf0412005-09-14 11:39:25 -0700164#define PCNET32_LOG_TX_BUFFERS 4
165#define PCNET32_LOG_RX_BUFFERS 5
166#define PCNET32_LOG_MAX_TX_BUFFERS 9 /* 2^9 == 512 */
167#define PCNET32_LOG_MAX_RX_BUFFERS 9
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#endif
169
170#define TX_RING_SIZE (1 << (PCNET32_LOG_TX_BUFFERS))
Hubert WS Lineabf0412005-09-14 11:39:25 -0700171#define TX_MAX_RING_SIZE (1 << (PCNET32_LOG_MAX_TX_BUFFERS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173#define RX_RING_SIZE (1 << (PCNET32_LOG_RX_BUFFERS))
Hubert WS Lineabf0412005-09-14 11:39:25 -0700174#define RX_MAX_RING_SIZE (1 << (PCNET32_LOG_MAX_RX_BUFFERS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176#define PKT_BUF_SZ 1544
177
178/* Offsets from base I/O address. */
179#define PCNET32_WIO_RDP 0x10
180#define PCNET32_WIO_RAP 0x12
181#define PCNET32_WIO_RESET 0x14
182#define PCNET32_WIO_BDP 0x16
183
184#define PCNET32_DWIO_RDP 0x10
185#define PCNET32_DWIO_RAP 0x14
186#define PCNET32_DWIO_RESET 0x18
187#define PCNET32_DWIO_BDP 0x1C
188
189#define PCNET32_TOTAL_SIZE 0x20
190
191/* The PCNET32 Rx and Tx ring descriptors. */
192struct pcnet32_rx_head {
Jeff Garzik0b5bf222006-03-21 16:22:47 -0500193 u32 base;
194 s16 buf_length;
195 s16 status;
196 u32 msg_length;
197 u32 reserved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198};
199
200struct pcnet32_tx_head {
Jeff Garzik0b5bf222006-03-21 16:22:47 -0500201 u32 base;
202 s16 length;
203 s16 status;
204 u32 misc;
205 u32 reserved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206};
207
208/* The PCNET32 32-Bit initialization block, described in databook. */
209struct pcnet32_init_block {
Jeff Garzik0b5bf222006-03-21 16:22:47 -0500210 u16 mode;
211 u16 tlen_rlen;
212 u8 phys_addr[6];
213 u16 reserved;
214 u32 filter[2];
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500215 /* Receive and transmit ring base, along with extra bits. */
Jeff Garzik0b5bf222006-03-21 16:22:47 -0500216 u32 rx_ring;
217 u32 tx_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218};
219
220/* PCnet32 access functions */
221struct pcnet32_access {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500222 u16 (*read_csr) (unsigned long, int);
223 void (*write_csr) (unsigned long, int, u16);
224 u16 (*read_bcr) (unsigned long, int);
225 void (*write_bcr) (unsigned long, int, u16);
226 u16 (*read_rap) (unsigned long);
227 void (*write_rap) (unsigned long, u16);
228 void (*reset) (unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229};
230
231/*
Hubert WS Lin76209922005-09-14 11:39:27 -0700232 * The first field of pcnet32_private is read by the ethernet device
233 * so the structure should be allocated using pci_alloc_consistent().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
235struct pcnet32_private {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500236 struct pcnet32_init_block init_block;
237 /* The Tx and Rx ring entries must be aligned on 16-byte boundaries in 32bit mode. */
Jeff Garzik0b5bf222006-03-21 16:22:47 -0500238 struct pcnet32_rx_head *rx_ring;
239 struct pcnet32_tx_head *tx_ring;
240 dma_addr_t dma_addr;/* DMA address of beginning of this
241 object, returned by pci_alloc_consistent */
242 struct pci_dev *pci_dev;
243 const char *name;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500244 /* The saved address of a sent-in-place packet/buffer, for skfree(). */
Jeff Garzik0b5bf222006-03-21 16:22:47 -0500245 struct sk_buff **tx_skbuff;
246 struct sk_buff **rx_skbuff;
247 dma_addr_t *tx_dma_addr;
248 dma_addr_t *rx_dma_addr;
249 struct pcnet32_access a;
250 spinlock_t lock; /* Guard lock */
251 unsigned int cur_rx, cur_tx; /* The next free ring entry */
252 unsigned int rx_ring_size; /* current rx ring size */
253 unsigned int tx_ring_size; /* current tx ring size */
254 unsigned int rx_mod_mask; /* rx ring modular mask */
255 unsigned int tx_mod_mask; /* tx ring modular mask */
256 unsigned short rx_len_bits;
257 unsigned short tx_len_bits;
258 dma_addr_t rx_ring_dma_addr;
259 dma_addr_t tx_ring_dma_addr;
260 unsigned int dirty_rx, /* ring entries to be freed. */
261 dirty_tx;
262
263 struct net_device_stats stats;
264 char tx_full;
265 char phycount; /* number of phys found */
266 int options;
267 unsigned int shared_irq:1, /* shared irq possible */
268 dxsuflo:1, /* disable transmit stop on uflo */
269 mii:1; /* mii port available */
270 struct net_device *next;
271 struct mii_if_info mii_if;
272 struct timer_list watchdog_timer;
273 struct timer_list blink_timer;
274 u32 msg_enable; /* debug message level */
Don Fryac62ef02006-03-20 15:26:03 -0800275
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500276 /* each bit indicates an available PHY */
Jeff Garzik0b5bf222006-03-21 16:22:47 -0500277 u32 phymask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278};
279
280static void pcnet32_probe_vlbus(void);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500281static int pcnet32_probe_pci(struct pci_dev *, const struct pci_device_id *);
282static int pcnet32_probe1(unsigned long, int, struct pci_dev *);
283static int pcnet32_open(struct net_device *);
284static int pcnet32_init_ring(struct net_device *);
285static int pcnet32_start_xmit(struct sk_buff *, struct net_device *);
286static int pcnet32_rx(struct net_device *);
287static void pcnet32_tx_timeout(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288static irqreturn_t pcnet32_interrupt(int, void *, struct pt_regs *);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500289static int pcnet32_close(struct net_device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290static struct net_device_stats *pcnet32_get_stats(struct net_device *);
291static void pcnet32_load_multicast(struct net_device *dev);
292static void pcnet32_set_multicast_list(struct net_device *);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500293static int pcnet32_ioctl(struct net_device *, struct ifreq *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294static void pcnet32_watchdog(struct net_device *);
295static int mdio_read(struct net_device *dev, int phy_id, int reg_num);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500296static void mdio_write(struct net_device *dev, int phy_id, int reg_num,
297 int val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298static void pcnet32_restart(struct net_device *dev, unsigned int csr0_bits);
299static void pcnet32_ethtool_test(struct net_device *dev,
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500300 struct ethtool_test *eth_test, u64 * data);
301static int pcnet32_loopback_test(struct net_device *dev, uint64_t * data1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302static int pcnet32_phys_id(struct net_device *dev, u32 data);
303static void pcnet32_led_blink_callback(struct net_device *dev);
304static int pcnet32_get_regs_len(struct net_device *dev);
305static void pcnet32_get_regs(struct net_device *dev, struct ethtool_regs *regs,
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500306 void *ptr);
Don Fry1bcd3152005-04-29 14:51:17 -0700307static void pcnet32_purge_tx_ring(struct net_device *dev);
Don Frya88c8442005-11-01 12:04:33 -0800308static int pcnet32_alloc_ring(struct net_device *dev, char *name);
Hubert WS Lineabf0412005-09-14 11:39:25 -0700309static void pcnet32_free_ring(struct net_device *dev);
Don Fryac62ef02006-03-20 15:26:03 -0800310static void pcnet32_check_media(struct net_device *dev, int verbose);
Hubert WS Lineabf0412005-09-14 11:39:25 -0700311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312enum pci_flags_bit {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500313 PCI_USES_IO = 1, PCI_USES_MEM = 2, PCI_USES_MASTER = 4,
314 PCI_ADDR0 = 0x10 << 0, PCI_ADDR1 = 0x10 << 1, PCI_ADDR2 =
315 0x10 << 2, PCI_ADDR3 = 0x10 << 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316};
317
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500318static u16 pcnet32_wio_read_csr(unsigned long addr, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500320 outw(index, addr + PCNET32_WIO_RAP);
321 return inw(addr + PCNET32_WIO_RDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500324static void pcnet32_wio_write_csr(unsigned long addr, int index, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500326 outw(index, addr + PCNET32_WIO_RAP);
327 outw(val, addr + PCNET32_WIO_RDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500330static u16 pcnet32_wio_read_bcr(unsigned long addr, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500332 outw(index, addr + PCNET32_WIO_RAP);
333 return inw(addr + PCNET32_WIO_BDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500336static void pcnet32_wio_write_bcr(unsigned long addr, int index, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500338 outw(index, addr + PCNET32_WIO_RAP);
339 outw(val, addr + PCNET32_WIO_BDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500342static u16 pcnet32_wio_read_rap(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500344 return inw(addr + PCNET32_WIO_RAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500347static void pcnet32_wio_write_rap(unsigned long addr, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500349 outw(val, addr + PCNET32_WIO_RAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500352static void pcnet32_wio_reset(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500354 inw(addr + PCNET32_WIO_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500357static int pcnet32_wio_check(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500359 outw(88, addr + PCNET32_WIO_RAP);
360 return (inw(addr + PCNET32_WIO_RAP) == 88);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363static struct pcnet32_access pcnet32_wio = {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500364 .read_csr = pcnet32_wio_read_csr,
365 .write_csr = pcnet32_wio_write_csr,
366 .read_bcr = pcnet32_wio_read_bcr,
367 .write_bcr = pcnet32_wio_write_bcr,
368 .read_rap = pcnet32_wio_read_rap,
369 .write_rap = pcnet32_wio_write_rap,
370 .reset = pcnet32_wio_reset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371};
372
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500373static u16 pcnet32_dwio_read_csr(unsigned long addr, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500375 outl(index, addr + PCNET32_DWIO_RAP);
376 return (inl(addr + PCNET32_DWIO_RDP) & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500379static void pcnet32_dwio_write_csr(unsigned long addr, int index, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500381 outl(index, addr + PCNET32_DWIO_RAP);
382 outl(val, addr + PCNET32_DWIO_RDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500385static u16 pcnet32_dwio_read_bcr(unsigned long addr, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500387 outl(index, addr + PCNET32_DWIO_RAP);
388 return (inl(addr + PCNET32_DWIO_BDP) & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500391static void pcnet32_dwio_write_bcr(unsigned long addr, int index, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500393 outl(index, addr + PCNET32_DWIO_RAP);
394 outl(val, addr + PCNET32_DWIO_BDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500397static u16 pcnet32_dwio_read_rap(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500399 return (inl(addr + PCNET32_DWIO_RAP) & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500402static void pcnet32_dwio_write_rap(unsigned long addr, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500404 outl(val, addr + PCNET32_DWIO_RAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500407static void pcnet32_dwio_reset(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500409 inl(addr + PCNET32_DWIO_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500412static int pcnet32_dwio_check(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500414 outl(88, addr + PCNET32_DWIO_RAP);
415 return ((inl(addr + PCNET32_DWIO_RAP) & 0xffff) == 88);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
418static struct pcnet32_access pcnet32_dwio = {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500419 .read_csr = pcnet32_dwio_read_csr,
420 .write_csr = pcnet32_dwio_write_csr,
421 .read_bcr = pcnet32_dwio_read_bcr,
422 .write_bcr = pcnet32_dwio_write_bcr,
423 .read_rap = pcnet32_dwio_read_rap,
424 .write_rap = pcnet32_dwio_write_rap,
425 .reset = pcnet32_dwio_reset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426};
427
428#ifdef CONFIG_NET_POLL_CONTROLLER
429static void pcnet32_poll_controller(struct net_device *dev)
430{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500431 disable_irq(dev->irq);
432 pcnet32_interrupt(0, dev, NULL);
433 enable_irq(dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435#endif
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437static int pcnet32_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
438{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500439 struct pcnet32_private *lp = dev->priv;
440 unsigned long flags;
441 int r = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500443 if (lp->mii) {
444 spin_lock_irqsave(&lp->lock, flags);
445 mii_ethtool_gset(&lp->mii_if, cmd);
446 spin_unlock_irqrestore(&lp->lock, flags);
447 r = 0;
448 }
449 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
452static int pcnet32_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
453{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500454 struct pcnet32_private *lp = dev->priv;
455 unsigned long flags;
456 int r = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500458 if (lp->mii) {
459 spin_lock_irqsave(&lp->lock, flags);
460 r = mii_ethtool_sset(&lp->mii_if, cmd);
461 spin_unlock_irqrestore(&lp->lock, flags);
462 }
463 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500466static void pcnet32_get_drvinfo(struct net_device *dev,
467 struct ethtool_drvinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500469 struct pcnet32_private *lp = dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500471 strcpy(info->driver, DRV_NAME);
472 strcpy(info->version, DRV_VERSION);
473 if (lp->pci_dev)
474 strcpy(info->bus_info, pci_name(lp->pci_dev));
475 else
476 sprintf(info->bus_info, "VLB 0x%lx", dev->base_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
479static u32 pcnet32_get_link(struct net_device *dev)
480{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500481 struct pcnet32_private *lp = dev->priv;
482 unsigned long flags;
483 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500485 spin_lock_irqsave(&lp->lock, flags);
486 if (lp->mii) {
487 r = mii_link_ok(&lp->mii_if);
488 } else {
489 ulong ioaddr = dev->base_addr; /* card base I/O address */
490 r = (lp->a.read_bcr(ioaddr, 4) != 0xc0);
491 }
492 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500494 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
497static u32 pcnet32_get_msglevel(struct net_device *dev)
498{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500499 struct pcnet32_private *lp = dev->priv;
500 return lp->msg_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
503static void pcnet32_set_msglevel(struct net_device *dev, u32 value)
504{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500505 struct pcnet32_private *lp = dev->priv;
506 lp->msg_enable = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
509static int pcnet32_nway_reset(struct net_device *dev)
510{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500511 struct pcnet32_private *lp = dev->priv;
512 unsigned long flags;
513 int r = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500515 if (lp->mii) {
516 spin_lock_irqsave(&lp->lock, flags);
517 r = mii_nway_restart(&lp->mii_if);
518 spin_unlock_irqrestore(&lp->lock, flags);
519 }
520 return r;
521}
522
523static void pcnet32_get_ringparam(struct net_device *dev,
524 struct ethtool_ringparam *ering)
525{
526 struct pcnet32_private *lp = dev->priv;
527
528 ering->tx_max_pending = TX_MAX_RING_SIZE - 1;
529 ering->tx_pending = lp->tx_ring_size - 1;
530 ering->rx_max_pending = RX_MAX_RING_SIZE - 1;
531 ering->rx_pending = lp->rx_ring_size - 1;
532}
533
534static int pcnet32_set_ringparam(struct net_device *dev,
535 struct ethtool_ringparam *ering)
536{
537 struct pcnet32_private *lp = dev->priv;
538 unsigned long flags;
539 int i;
540
541 if (ering->rx_mini_pending || ering->rx_jumbo_pending)
542 return -EINVAL;
543
544 if (netif_running(dev))
545 pcnet32_close(dev);
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 spin_lock_irqsave(&lp->lock, flags);
Hubert WS Lineabf0412005-09-14 11:39:25 -0700548 pcnet32_free_ring(dev);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500549 lp->tx_ring_size =
550 min(ering->tx_pending, (unsigned int)TX_MAX_RING_SIZE);
551 lp->rx_ring_size =
552 min(ering->rx_pending, (unsigned int)RX_MAX_RING_SIZE);
553
554 /* set the minimum ring size to 4, to allow the loopback test to work
555 * unchanged.
556 */
557 for (i = 2; i <= PCNET32_LOG_MAX_TX_BUFFERS; i++) {
558 if (lp->tx_ring_size <= (1 << i))
559 break;
560 }
561 lp->tx_ring_size = (1 << i);
562 lp->tx_mod_mask = lp->tx_ring_size - 1;
563 lp->tx_len_bits = (i << 12);
564
565 for (i = 2; i <= PCNET32_LOG_MAX_RX_BUFFERS; i++) {
566 if (lp->rx_ring_size <= (1 << i))
567 break;
568 }
569 lp->rx_ring_size = (1 << i);
570 lp->rx_mod_mask = lp->rx_ring_size - 1;
571 lp->rx_len_bits = (i << 4);
572
573 if (pcnet32_alloc_ring(dev, dev->name)) {
574 pcnet32_free_ring(dev);
575 spin_unlock_irqrestore(&lp->lock, flags);
576 return -ENOMEM;
577 }
578
Don Frya88c8442005-11-01 12:04:33 -0800579 spin_unlock_irqrestore(&lp->lock, flags);
Hubert WS Lineabf0412005-09-14 11:39:25 -0700580
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500581 if (pcnet32_debug & NETIF_MSG_DRV)
582 printk(KERN_INFO PFX
583 "%s: Ring Param Settings: RX: %d, TX: %d\n", dev->name,
584 lp->rx_ring_size, lp->tx_ring_size);
Hubert WS Lineabf0412005-09-14 11:39:25 -0700585
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500586 if (netif_running(dev))
587 pcnet32_open(dev);
Hubert WS Lineabf0412005-09-14 11:39:25 -0700588
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500592static void pcnet32_get_strings(struct net_device *dev, u32 stringset,
593 u8 * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500595 memcpy(data, pcnet32_gstrings_test, sizeof(pcnet32_gstrings_test));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
598static int pcnet32_self_test_count(struct net_device *dev)
599{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500600 return PCNET32_TEST_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603static void pcnet32_ethtool_test(struct net_device *dev,
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500604 struct ethtool_test *test, u64 * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500606 struct pcnet32_private *lp = dev->priv;
607 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500609 if (test->flags == ETH_TEST_FL_OFFLINE) {
610 rc = pcnet32_loopback_test(dev, data);
611 if (rc) {
612 if (netif_msg_hw(lp))
613 printk(KERN_DEBUG "%s: Loopback test failed.\n",
614 dev->name);
615 test->flags |= ETH_TEST_FL_FAILED;
616 } else if (netif_msg_hw(lp))
617 printk(KERN_DEBUG "%s: Loopback test passed.\n",
618 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 } else if (netif_msg_hw(lp))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500620 printk(KERN_DEBUG
621 "%s: No tests to run (specify 'Offline' on ethtool).",
622 dev->name);
623} /* end pcnet32_ethtool_test */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500625static int pcnet32_loopback_test(struct net_device *dev, uint64_t * data1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500627 struct pcnet32_private *lp = dev->priv;
628 struct pcnet32_access *a = &lp->a; /* access to registers */
629 ulong ioaddr = dev->base_addr; /* card base I/O address */
630 struct sk_buff *skb; /* sk buff */
631 int x, i; /* counters */
632 int numbuffs = 4; /* number of TX/RX buffers and descs */
633 u16 status = 0x8300; /* TX ring status */
634 u16 teststatus; /* test of ring status */
635 int rc; /* return code */
636 int size; /* size of packets */
637 unsigned char *packet; /* source packet data */
638 static const int data_len = 60; /* length of source packets */
639 unsigned long flags;
640 unsigned long ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500642 *data1 = 1; /* status of test, default to fail */
643 rc = 1; /* default to fail */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500645 if (netif_running(dev))
646 pcnet32_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500648 spin_lock_irqsave(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500650 /* Reset the PCNET32 */
651 lp->a.reset(ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500653 /* switch pcnet32 to 32bit mode */
654 lp->a.write_bcr(ioaddr, 20, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500656 lp->init_block.mode =
657 le16_to_cpu((lp->options & PCNET32_PORT_PORTSEL) << 7);
658 lp->init_block.filter[0] = 0;
659 lp->init_block.filter[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500661 /* purge & init rings but don't actually restart */
662 pcnet32_restart(dev, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500664 lp->a.write_csr(ioaddr, 0, 0x0004); /* Set STOP bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500666 /* Initialize Transmit buffers. */
667 size = data_len + 15;
668 for (x = 0; x < numbuffs; x++) {
669 if (!(skb = dev_alloc_skb(size))) {
670 if (netif_msg_hw(lp))
671 printk(KERN_DEBUG
672 "%s: Cannot allocate skb at line: %d!\n",
673 dev->name, __LINE__);
674 goto clean_up;
675 } else {
676 packet = skb->data;
677 skb_put(skb, size); /* create space for data */
678 lp->tx_skbuff[x] = skb;
679 lp->tx_ring[x].length = le16_to_cpu(-skb->len);
680 lp->tx_ring[x].misc = 0;
681
682 /* put DA and SA into the skb */
683 for (i = 0; i < 6; i++)
684 *packet++ = dev->dev_addr[i];
685 for (i = 0; i < 6; i++)
686 *packet++ = dev->dev_addr[i];
687 /* type */
688 *packet++ = 0x08;
689 *packet++ = 0x06;
690 /* packet number */
691 *packet++ = x;
692 /* fill packet with data */
693 for (i = 0; i < data_len; i++)
694 *packet++ = i;
695
696 lp->tx_dma_addr[x] =
697 pci_map_single(lp->pci_dev, skb->data, skb->len,
698 PCI_DMA_TODEVICE);
699 lp->tx_ring[x].base =
700 (u32) le32_to_cpu(lp->tx_dma_addr[x]);
701 wmb(); /* Make sure owner changes after all others are visible */
702 lp->tx_ring[x].status = le16_to_cpu(status);
703 }
704 }
705
706 x = a->read_bcr(ioaddr, 32); /* set internal loopback in BSR32 */
707 x = x | 0x0002;
708 a->write_bcr(ioaddr, 32, x);
709
710 lp->a.write_csr(ioaddr, 15, 0x0044); /* set int loopback in CSR15 */
711
712 teststatus = le16_to_cpu(0x8000);
713 lp->a.write_csr(ioaddr, 0, 0x0002); /* Set STRT bit */
714
715 /* Check status of descriptors */
716 for (x = 0; x < numbuffs; x++) {
717 ticks = 0;
718 rmb();
719 while ((lp->rx_ring[x].status & teststatus) && (ticks < 200)) {
720 spin_unlock_irqrestore(&lp->lock, flags);
721 mdelay(1);
722 spin_lock_irqsave(&lp->lock, flags);
723 rmb();
724 ticks++;
725 }
726 if (ticks == 200) {
727 if (netif_msg_hw(lp))
728 printk("%s: Desc %d failed to reset!\n",
729 dev->name, x);
730 break;
731 }
732 }
733
734 lp->a.write_csr(ioaddr, 0, 0x0004); /* Set STOP bit */
735 wmb();
736 if (netif_msg_hw(lp) && netif_msg_pktdata(lp)) {
737 printk(KERN_DEBUG "%s: RX loopback packets:\n", dev->name);
738
739 for (x = 0; x < numbuffs; x++) {
740 printk(KERN_DEBUG "%s: Packet %d:\n", dev->name, x);
741 skb = lp->rx_skbuff[x];
742 for (i = 0; i < size; i++) {
743 printk("%02x ", *(skb->data + i));
744 }
745 printk("\n");
746 }
747 }
748
749 x = 0;
750 rc = 0;
751 while (x < numbuffs && !rc) {
752 skb = lp->rx_skbuff[x];
753 packet = lp->tx_skbuff[x]->data;
754 for (i = 0; i < size; i++) {
755 if (*(skb->data + i) != packet[i]) {
756 if (netif_msg_hw(lp))
757 printk(KERN_DEBUG
758 "%s: Error in compare! %2x - %02x %02x\n",
759 dev->name, i, *(skb->data + i),
760 packet[i]);
761 rc = 1;
762 break;
763 }
764 }
765 x++;
766 }
767 if (!rc) {
768 *data1 = 0;
769 }
770
771 clean_up:
772 pcnet32_purge_tx_ring(dev);
773 x = a->read_csr(ioaddr, 15) & 0xFFFF;
774 a->write_csr(ioaddr, 15, (x & ~0x0044)); /* reset bits 6 and 2 */
775
776 x = a->read_bcr(ioaddr, 32); /* reset internal loopback */
777 x = x & ~0x0002;
778 a->write_bcr(ioaddr, 32, x);
779
780 spin_unlock_irqrestore(&lp->lock, flags);
781
782 if (netif_running(dev)) {
783 pcnet32_open(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 } else {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500785 lp->a.write_bcr(ioaddr, 20, 4); /* return to 16bit mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500788 return (rc);
789} /* end pcnet32_loopback_test */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791static void pcnet32_led_blink_callback(struct net_device *dev)
792{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500793 struct pcnet32_private *lp = dev->priv;
794 struct pcnet32_access *a = &lp->a;
795 ulong ioaddr = dev->base_addr;
796 unsigned long flags;
797 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500799 spin_lock_irqsave(&lp->lock, flags);
800 for (i = 4; i < 8; i++) {
801 a->write_bcr(ioaddr, i, a->read_bcr(ioaddr, i) ^ 0x4000);
802 }
803 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500805 mod_timer(&lp->blink_timer, PCNET32_BLINK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
808static int pcnet32_phys_id(struct net_device *dev, u32 data)
809{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500810 struct pcnet32_private *lp = dev->priv;
811 struct pcnet32_access *a = &lp->a;
812 ulong ioaddr = dev->base_addr;
813 unsigned long flags;
814 int i, regs[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500816 if (!lp->blink_timer.function) {
817 init_timer(&lp->blink_timer);
818 lp->blink_timer.function = (void *)pcnet32_led_blink_callback;
819 lp->blink_timer.data = (unsigned long)dev;
820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500822 /* Save the current value of the bcrs */
823 spin_lock_irqsave(&lp->lock, flags);
824 for (i = 4; i < 8; i++) {
825 regs[i - 4] = a->read_bcr(ioaddr, i);
826 }
827 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500829 mod_timer(&lp->blink_timer, jiffies);
830 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500832 if ((!data) || (data > (u32) (MAX_SCHEDULE_TIMEOUT / HZ)))
833 data = (u32) (MAX_SCHEDULE_TIMEOUT / HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500835 msleep_interruptible(data * 1000);
836 del_timer_sync(&lp->blink_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500838 /* Restore the original value of the bcrs */
839 spin_lock_irqsave(&lp->lock, flags);
840 for (i = 4; i < 8; i++) {
841 a->write_bcr(ioaddr, i, regs[i - 4]);
842 }
843 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500845 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Don Fryac62ef02006-03-20 15:26:03 -0800848#define PCNET32_REGS_PER_PHY 32
849#define PCNET32_MAX_PHYS 32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850static int pcnet32_get_regs_len(struct net_device *dev)
851{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500852 struct pcnet32_private *lp = dev->priv;
853 int j = lp->phycount * PCNET32_REGS_PER_PHY;
Don Fryac62ef02006-03-20 15:26:03 -0800854
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500855 return ((PCNET32_NUM_REGS + j) * sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
858static void pcnet32_get_regs(struct net_device *dev, struct ethtool_regs *regs,
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500859 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500861 int i, csr0;
862 u16 *buff = ptr;
863 struct pcnet32_private *lp = dev->priv;
864 struct pcnet32_access *a = &lp->a;
865 ulong ioaddr = dev->base_addr;
866 int ticks;
867 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500869 spin_lock_irqsave(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500871 csr0 = a->read_csr(ioaddr, 0);
872 if (!(csr0 & 0x0004)) { /* If not stopped */
873 /* set SUSPEND (SPND) - CSR5 bit 0 */
874 a->write_csr(ioaddr, 5, 0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500876 /* poll waiting for bit to be set */
877 ticks = 0;
878 while (!(a->read_csr(ioaddr, 5) & 0x0001)) {
879 spin_unlock_irqrestore(&lp->lock, flags);
880 mdelay(1);
881 spin_lock_irqsave(&lp->lock, flags);
882 ticks++;
883 if (ticks > 200) {
884 if (netif_msg_hw(lp))
885 printk(KERN_DEBUG
886 "%s: Error getting into suspend!\n",
887 dev->name);
888 break;
889 }
Don Fryac62ef02006-03-20 15:26:03 -0800890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500893 /* read address PROM */
894 for (i = 0; i < 16; i += 2)
895 *buff++ = inw(ioaddr + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500897 /* read control and status registers */
898 for (i = 0; i < 90; i++) {
899 *buff++ = a->read_csr(ioaddr, i);
900 }
901
902 *buff++ = a->read_csr(ioaddr, 112);
903 *buff++ = a->read_csr(ioaddr, 114);
904
905 /* read bus configuration registers */
906 for (i = 0; i < 30; i++) {
907 *buff++ = a->read_bcr(ioaddr, i);
908 }
909 *buff++ = 0; /* skip bcr30 so as not to hang 79C976 */
910 for (i = 31; i < 36; i++) {
911 *buff++ = a->read_bcr(ioaddr, i);
912 }
913
914 /* read mii phy registers */
915 if (lp->mii) {
916 int j;
917 for (j = 0; j < PCNET32_MAX_PHYS; j++) {
918 if (lp->phymask & (1 << j)) {
919 for (i = 0; i < PCNET32_REGS_PER_PHY; i++) {
920 lp->a.write_bcr(ioaddr, 33,
921 (j << 5) | i);
922 *buff++ = lp->a.read_bcr(ioaddr, 34);
923 }
924 }
925 }
926 }
927
928 if (!(csr0 & 0x0004)) { /* If not stopped */
929 /* clear SUSPEND (SPND) - CSR5 bit 0 */
930 a->write_csr(ioaddr, 5, 0x0000);
931 }
932
933 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
936static struct ethtool_ops pcnet32_ethtool_ops = {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500937 .get_settings = pcnet32_get_settings,
938 .set_settings = pcnet32_set_settings,
939 .get_drvinfo = pcnet32_get_drvinfo,
940 .get_msglevel = pcnet32_get_msglevel,
941 .set_msglevel = pcnet32_set_msglevel,
942 .nway_reset = pcnet32_nway_reset,
943 .get_link = pcnet32_get_link,
944 .get_ringparam = pcnet32_get_ringparam,
945 .set_ringparam = pcnet32_set_ringparam,
946 .get_tx_csum = ethtool_op_get_tx_csum,
947 .get_sg = ethtool_op_get_sg,
948 .get_tso = ethtool_op_get_tso,
949 .get_strings = pcnet32_get_strings,
950 .self_test_count = pcnet32_self_test_count,
951 .self_test = pcnet32_ethtool_test,
952 .phys_id = pcnet32_phys_id,
953 .get_regs_len = pcnet32_get_regs_len,
954 .get_regs = pcnet32_get_regs,
955 .get_perm_addr = ethtool_op_get_perm_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956};
957
958/* only probes for non-PCI devices, the rest are handled by
959 * pci_register_driver via pcnet32_probe_pci */
960
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500961static void __devinit pcnet32_probe_vlbus(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500963 unsigned int *port, ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500965 /* search for PCnet32 VLB cards at known addresses */
966 for (port = pcnet32_portlist; (ioaddr = *port); port++) {
967 if (request_region
968 (ioaddr, PCNET32_TOTAL_SIZE, "pcnet32_probe_vlbus")) {
969 /* check if there is really a pcnet chip on that ioaddr */
970 if ((inb(ioaddr + 14) == 0x57)
971 && (inb(ioaddr + 15) == 0x57)) {
972 pcnet32_probe1(ioaddr, 0, NULL);
973 } else {
974 release_region(ioaddr, PCNET32_TOTAL_SIZE);
975 }
976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980static int __devinit
981pcnet32_probe_pci(struct pci_dev *pdev, const struct pci_device_id *ent)
982{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500983 unsigned long ioaddr;
984 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500986 err = pci_enable_device(pdev);
987 if (err < 0) {
988 if (pcnet32_debug & NETIF_MSG_PROBE)
989 printk(KERN_ERR PFX
990 "failed to enable device -- err=%d\n", err);
991 return err;
992 }
993 pci_set_master(pdev);
994
995 ioaddr = pci_resource_start(pdev, 0);
996 if (!ioaddr) {
997 if (pcnet32_debug & NETIF_MSG_PROBE)
998 printk(KERN_ERR PFX
999 "card has no PCI IO resources, aborting\n");
1000 return -ENODEV;
1001 }
1002
1003 if (!pci_dma_supported(pdev, PCNET32_DMA_MASK)) {
1004 if (pcnet32_debug & NETIF_MSG_PROBE)
1005 printk(KERN_ERR PFX
1006 "architecture does not support 32bit PCI busmaster DMA\n");
1007 return -ENODEV;
1008 }
1009 if (request_region(ioaddr, PCNET32_TOTAL_SIZE, "pcnet32_probe_pci") ==
1010 NULL) {
1011 if (pcnet32_debug & NETIF_MSG_PROBE)
1012 printk(KERN_ERR PFX
1013 "io address range already allocated\n");
1014 return -EBUSY;
1015 }
1016
1017 err = pcnet32_probe1(ioaddr, 1, pdev);
1018 if (err < 0) {
1019 pci_disable_device(pdev);
1020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024/* pcnet32_probe1
1025 * Called from both pcnet32_probe_vlbus and pcnet_probe_pci.
1026 * pdev will be NULL when called from pcnet32_probe_vlbus.
1027 */
1028static int __devinit
1029pcnet32_probe1(unsigned long ioaddr, int shared, struct pci_dev *pdev)
1030{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001031 struct pcnet32_private *lp;
1032 dma_addr_t lp_dma_addr;
1033 int i, media;
1034 int fdx, mii, fset, dxsuflo;
1035 int chip_version;
1036 char *chipname;
1037 struct net_device *dev;
1038 struct pcnet32_access *a = NULL;
1039 u8 promaddr[6];
1040 int ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001042 /* reset the chip */
1043 pcnet32_wio_reset(ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001045 /* NOTE: 16-bit check is first, otherwise some older PCnet chips fail */
1046 if (pcnet32_wio_read_csr(ioaddr, 0) == 4 && pcnet32_wio_check(ioaddr)) {
1047 a = &pcnet32_wio;
1048 } else {
1049 pcnet32_dwio_reset(ioaddr);
1050 if (pcnet32_dwio_read_csr(ioaddr, 0) == 4
1051 && pcnet32_dwio_check(ioaddr)) {
1052 a = &pcnet32_dwio;
1053 } else
1054 goto err_release_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001057 chip_version =
1058 a->read_csr(ioaddr, 88) | (a->read_csr(ioaddr, 89) << 16);
1059 if ((pcnet32_debug & NETIF_MSG_PROBE) && (pcnet32_debug & NETIF_MSG_HW))
1060 printk(KERN_INFO " PCnet chip version is %#x.\n",
1061 chip_version);
1062 if ((chip_version & 0xfff) != 0x003) {
1063 if (pcnet32_debug & NETIF_MSG_PROBE)
1064 printk(KERN_INFO PFX "Unsupported chip version.\n");
1065 goto err_release_region;
1066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001068 /* initialize variables */
1069 fdx = mii = fset = dxsuflo = 0;
1070 chip_version = (chip_version >> 12) & 0xffff;
1071
1072 switch (chip_version) {
1073 case 0x2420:
1074 chipname = "PCnet/PCI 79C970"; /* PCI */
1075 break;
1076 case 0x2430:
1077 if (shared)
1078 chipname = "PCnet/PCI 79C970"; /* 970 gives the wrong chip id back */
1079 else
1080 chipname = "PCnet/32 79C965"; /* 486/VL bus */
1081 break;
1082 case 0x2621:
1083 chipname = "PCnet/PCI II 79C970A"; /* PCI */
1084 fdx = 1;
1085 break;
1086 case 0x2623:
1087 chipname = "PCnet/FAST 79C971"; /* PCI */
1088 fdx = 1;
1089 mii = 1;
1090 fset = 1;
1091 break;
1092 case 0x2624:
1093 chipname = "PCnet/FAST+ 79C972"; /* PCI */
1094 fdx = 1;
1095 mii = 1;
1096 fset = 1;
1097 break;
1098 case 0x2625:
1099 chipname = "PCnet/FAST III 79C973"; /* PCI */
1100 fdx = 1;
1101 mii = 1;
1102 break;
1103 case 0x2626:
1104 chipname = "PCnet/Home 79C978"; /* PCI */
1105 fdx = 1;
1106 /*
1107 * This is based on specs published at www.amd.com. This section
1108 * assumes that a card with a 79C978 wants to go into standard
1109 * ethernet mode. The 79C978 can also go into 1Mb HomePNA mode,
1110 * and the module option homepna=1 can select this instead.
1111 */
1112 media = a->read_bcr(ioaddr, 49);
1113 media &= ~3; /* default to 10Mb ethernet */
1114 if (cards_found < MAX_UNITS && homepna[cards_found])
1115 media |= 1; /* switch to home wiring mode */
1116 if (pcnet32_debug & NETIF_MSG_PROBE)
1117 printk(KERN_DEBUG PFX "media set to %sMbit mode.\n",
1118 (media & 1) ? "1" : "10");
1119 a->write_bcr(ioaddr, 49, media);
1120 break;
1121 case 0x2627:
1122 chipname = "PCnet/FAST III 79C975"; /* PCI */
1123 fdx = 1;
1124 mii = 1;
1125 break;
1126 case 0x2628:
1127 chipname = "PCnet/PRO 79C976";
1128 fdx = 1;
1129 mii = 1;
1130 break;
1131 default:
1132 if (pcnet32_debug & NETIF_MSG_PROBE)
1133 printk(KERN_INFO PFX
1134 "PCnet version %#x, no PCnet32 chip.\n",
1135 chip_version);
1136 goto err_release_region;
1137 }
1138
1139 /*
1140 * On selected chips turn on the BCR18:NOUFLO bit. This stops transmit
1141 * starting until the packet is loaded. Strike one for reliability, lose
1142 * one for latency - although on PCI this isnt a big loss. Older chips
1143 * have FIFO's smaller than a packet, so you can't do this.
1144 * Turn on BCR18:BurstRdEn and BCR18:BurstWrEn.
1145 */
1146
1147 if (fset) {
1148 a->write_bcr(ioaddr, 18, (a->read_bcr(ioaddr, 18) | 0x0860));
1149 a->write_csr(ioaddr, 80,
1150 (a->read_csr(ioaddr, 80) & 0x0C00) | 0x0c00);
1151 dxsuflo = 1;
1152 }
1153
1154 dev = alloc_etherdev(0);
1155 if (!dev) {
1156 if (pcnet32_debug & NETIF_MSG_PROBE)
1157 printk(KERN_ERR PFX "Memory allocation failed.\n");
1158 ret = -ENOMEM;
1159 goto err_release_region;
1160 }
1161 SET_NETDEV_DEV(dev, &pdev->dev);
1162
1163 if (pcnet32_debug & NETIF_MSG_PROBE)
1164 printk(KERN_INFO PFX "%s at %#3lx,", chipname, ioaddr);
1165
1166 /* In most chips, after a chip reset, the ethernet address is read from the
1167 * station address PROM at the base address and programmed into the
1168 * "Physical Address Registers" CSR12-14.
1169 * As a precautionary measure, we read the PROM values and complain if
Linas Vepstasbc0e1fc2006-03-28 16:36:23 -06001170 * they disagree with the CSRs. If they miscompare, and the PROM addr
1171 * is valid, then the PROM addr is used.
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001172 */
1173 for (i = 0; i < 3; i++) {
1174 unsigned int val;
1175 val = a->read_csr(ioaddr, i + 12) & 0x0ffff;
1176 /* There may be endianness issues here. */
1177 dev->dev_addr[2 * i] = val & 0x0ff;
1178 dev->dev_addr[2 * i + 1] = (val >> 8) & 0x0ff;
1179 }
1180
1181 /* read PROM address and compare with CSR address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 for (i = 0; i < 6; i++)
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001183 promaddr[i] = inb(ioaddr + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001185 if (memcmp(promaddr, dev->dev_addr, 6)
1186 || !is_valid_ether_addr(dev->dev_addr)) {
1187 if (is_valid_ether_addr(promaddr)) {
1188 if (pcnet32_debug & NETIF_MSG_PROBE) {
1189 printk(" warning: CSR address invalid,\n");
1190 printk(KERN_INFO
1191 " using instead PROM address of");
1192 }
1193 memcpy(dev->dev_addr, promaddr, 6);
1194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001196 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001198 /* if the ethernet address is not valid, force to 00:00:00:00:00:00 */
1199 if (!is_valid_ether_addr(dev->perm_addr))
1200 memset(dev->dev_addr, 0, sizeof(dev->dev_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001202 if (pcnet32_debug & NETIF_MSG_PROBE) {
1203 for (i = 0; i < 6; i++)
1204 printk(" %2.2x", dev->dev_addr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001206 /* Version 0x2623 and 0x2624 */
1207 if (((chip_version + 1) & 0xfffe) == 0x2624) {
1208 i = a->read_csr(ioaddr, 80) & 0x0C00; /* Check tx_start_pt */
1209 printk("\n" KERN_INFO " tx_start_pt(0x%04x):", i);
1210 switch (i >> 10) {
1211 case 0:
1212 printk(" 20 bytes,");
1213 break;
1214 case 1:
1215 printk(" 64 bytes,");
1216 break;
1217 case 2:
1218 printk(" 128 bytes,");
1219 break;
1220 case 3:
1221 printk("~220 bytes,");
1222 break;
1223 }
1224 i = a->read_bcr(ioaddr, 18); /* Check Burst/Bus control */
1225 printk(" BCR18(%x):", i & 0xffff);
1226 if (i & (1 << 5))
1227 printk("BurstWrEn ");
1228 if (i & (1 << 6))
1229 printk("BurstRdEn ");
1230 if (i & (1 << 7))
1231 printk("DWordIO ");
1232 if (i & (1 << 11))
1233 printk("NoUFlow ");
1234 i = a->read_bcr(ioaddr, 25);
1235 printk("\n" KERN_INFO " SRAMSIZE=0x%04x,", i << 8);
1236 i = a->read_bcr(ioaddr, 26);
1237 printk(" SRAM_BND=0x%04x,", i << 8);
1238 i = a->read_bcr(ioaddr, 27);
1239 if (i & (1 << 14))
1240 printk("LowLatRx");
1241 }
1242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001244 dev->base_addr = ioaddr;
1245 /* pci_alloc_consistent returns page-aligned memory, so we do not have to check the alignment */
1246 if ((lp =
1247 pci_alloc_consistent(pdev, sizeof(*lp), &lp_dma_addr)) == NULL) {
1248 if (pcnet32_debug & NETIF_MSG_PROBE)
1249 printk(KERN_ERR PFX
1250 "Consistent memory allocation failed.\n");
1251 ret = -ENOMEM;
1252 goto err_free_netdev;
1253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001255 memset(lp, 0, sizeof(*lp));
1256 lp->dma_addr = lp_dma_addr;
1257 lp->pci_dev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001259 spin_lock_init(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001261 SET_MODULE_OWNER(dev);
1262 SET_NETDEV_DEV(dev, &pdev->dev);
1263 dev->priv = lp;
1264 lp->name = chipname;
1265 lp->shared_irq = shared;
1266 lp->tx_ring_size = TX_RING_SIZE; /* default tx ring size */
1267 lp->rx_ring_size = RX_RING_SIZE; /* default rx ring size */
1268 lp->tx_mod_mask = lp->tx_ring_size - 1;
1269 lp->rx_mod_mask = lp->rx_ring_size - 1;
1270 lp->tx_len_bits = (PCNET32_LOG_TX_BUFFERS << 12);
1271 lp->rx_len_bits = (PCNET32_LOG_RX_BUFFERS << 4);
1272 lp->mii_if.full_duplex = fdx;
1273 lp->mii_if.phy_id_mask = 0x1f;
1274 lp->mii_if.reg_num_mask = 0x1f;
1275 lp->dxsuflo = dxsuflo;
1276 lp->mii = mii;
1277 lp->msg_enable = pcnet32_debug;
1278 if ((cards_found >= MAX_UNITS)
1279 || (options[cards_found] > sizeof(options_mapping)))
1280 lp->options = PCNET32_PORT_ASEL;
1281 else
1282 lp->options = options_mapping[options[cards_found]];
1283 lp->mii_if.dev = dev;
1284 lp->mii_if.mdio_read = mdio_read;
1285 lp->mii_if.mdio_write = mdio_write;
1286
1287 if (fdx && !(lp->options & PCNET32_PORT_ASEL) &&
1288 ((cards_found >= MAX_UNITS) || full_duplex[cards_found]))
1289 lp->options |= PCNET32_PORT_FD;
1290
1291 if (!a) {
1292 if (pcnet32_debug & NETIF_MSG_PROBE)
1293 printk(KERN_ERR PFX "No access methods\n");
1294 ret = -ENODEV;
1295 goto err_free_consistent;
1296 }
1297 lp->a = *a;
1298
1299 /* prior to register_netdev, dev->name is not yet correct */
1300 if (pcnet32_alloc_ring(dev, pci_name(lp->pci_dev))) {
1301 ret = -ENOMEM;
1302 goto err_free_ring;
1303 }
1304 /* detect special T1/E1 WAN card by checking for MAC address */
1305 if (dev->dev_addr[0] == 0x00 && dev->dev_addr[1] == 0xe0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 && dev->dev_addr[2] == 0x75)
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001307 lp->options = PCNET32_PORT_FD | PCNET32_PORT_GPSI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001309 lp->init_block.mode = le16_to_cpu(0x0003); /* Disable Rx and Tx. */
1310 lp->init_block.tlen_rlen =
1311 le16_to_cpu(lp->tx_len_bits | lp->rx_len_bits);
1312 for (i = 0; i < 6; i++)
1313 lp->init_block.phys_addr[i] = dev->dev_addr[i];
1314 lp->init_block.filter[0] = 0x00000000;
1315 lp->init_block.filter[1] = 0x00000000;
1316 lp->init_block.rx_ring = (u32) le32_to_cpu(lp->rx_ring_dma_addr);
1317 lp->init_block.tx_ring = (u32) le32_to_cpu(lp->tx_ring_dma_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001319 /* switch pcnet32 to 32bit mode */
1320 a->write_bcr(ioaddr, 20, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001322 a->write_csr(ioaddr, 1, (lp->dma_addr + offsetof(struct pcnet32_private,
1323 init_block)) & 0xffff);
1324 a->write_csr(ioaddr, 2, (lp->dma_addr + offsetof(struct pcnet32_private,
1325 init_block)) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001327 if (pdev) { /* use the IRQ provided by PCI */
1328 dev->irq = pdev->irq;
1329 if (pcnet32_debug & NETIF_MSG_PROBE)
1330 printk(" assigned IRQ %d.\n", dev->irq);
1331 } else {
1332 unsigned long irq_mask = probe_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001334 /*
1335 * To auto-IRQ we enable the initialization-done and DMA error
1336 * interrupts. For ISA boards we get a DMA error, but VLB and PCI
1337 * boards will work.
1338 */
1339 /* Trigger an initialization just for the interrupt. */
1340 a->write_csr(ioaddr, 0, 0x41);
1341 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001343 dev->irq = probe_irq_off(irq_mask);
1344 if (!dev->irq) {
1345 if (pcnet32_debug & NETIF_MSG_PROBE)
1346 printk(", failed to detect IRQ line.\n");
1347 ret = -ENODEV;
1348 goto err_free_ring;
1349 }
1350 if (pcnet32_debug & NETIF_MSG_PROBE)
1351 printk(", probed IRQ %d.\n", dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001354 /* Set the mii phy_id so that we can query the link state */
1355 if (lp->mii) {
1356 /* lp->phycount and lp->phymask are set to 0 by memset above */
Don Fryac62ef02006-03-20 15:26:03 -08001357
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001358 lp->mii_if.phy_id = ((lp->a.read_bcr(ioaddr, 33)) >> 5) & 0x1f;
1359 /* scan for PHYs */
1360 for (i = 0; i < PCNET32_MAX_PHYS; i++) {
1361 unsigned short id1, id2;
Don Fryac62ef02006-03-20 15:26:03 -08001362
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001363 id1 = mdio_read(dev, i, MII_PHYSID1);
1364 if (id1 == 0xffff)
1365 continue;
1366 id2 = mdio_read(dev, i, MII_PHYSID2);
1367 if (id2 == 0xffff)
1368 continue;
1369 if (i == 31 && ((chip_version + 1) & 0xfffe) == 0x2624)
1370 continue; /* 79C971 & 79C972 have phantom phy at id 31 */
1371 lp->phycount++;
1372 lp->phymask |= (1 << i);
1373 lp->mii_if.phy_id = i;
1374 if (pcnet32_debug & NETIF_MSG_PROBE)
1375 printk(KERN_INFO PFX
1376 "Found PHY %04x:%04x at address %d.\n",
1377 id1, id2, i);
1378 }
1379 lp->a.write_bcr(ioaddr, 33, (lp->mii_if.phy_id) << 5);
1380 if (lp->phycount > 1) {
1381 lp->options |= PCNET32_PORT_MII;
1382 }
Don Fryac62ef02006-03-20 15:26:03 -08001383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001385 init_timer(&lp->watchdog_timer);
1386 lp->watchdog_timer.data = (unsigned long)dev;
1387 lp->watchdog_timer.function = (void *)&pcnet32_watchdog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001389 /* The PCNET32-specific entries in the device structure. */
1390 dev->open = &pcnet32_open;
1391 dev->hard_start_xmit = &pcnet32_start_xmit;
1392 dev->stop = &pcnet32_close;
1393 dev->get_stats = &pcnet32_get_stats;
1394 dev->set_multicast_list = &pcnet32_set_multicast_list;
1395 dev->do_ioctl = &pcnet32_ioctl;
1396 dev->ethtool_ops = &pcnet32_ethtool_ops;
1397 dev->tx_timeout = pcnet32_tx_timeout;
1398 dev->watchdog_timeo = (5 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400#ifdef CONFIG_NET_POLL_CONTROLLER
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001401 dev->poll_controller = pcnet32_poll_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402#endif
1403
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001404 /* Fill in the generic fields of the device structure. */
1405 if (register_netdev(dev))
1406 goto err_free_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001408 if (pdev) {
1409 pci_set_drvdata(pdev, dev);
1410 } else {
1411 lp->next = pcnet32_dev;
1412 pcnet32_dev = dev;
1413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001415 if (pcnet32_debug & NETIF_MSG_PROBE)
1416 printk(KERN_INFO "%s: registered as %s\n", dev->name, lp->name);
1417 cards_found++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001419 /* enable LED writes */
1420 a->write_bcr(ioaddr, 2, a->read_bcr(ioaddr, 2) | 0x1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001424 err_free_ring:
1425 pcnet32_free_ring(dev);
1426 err_free_consistent:
1427 pci_free_consistent(lp->pci_dev, sizeof(*lp), lp, lp->dma_addr);
1428 err_free_netdev:
1429 free_netdev(dev);
1430 err_release_region:
1431 release_region(ioaddr, PCNET32_TOTAL_SIZE);
1432 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
Don Frya88c8442005-11-01 12:04:33 -08001435/* if any allocation fails, caller must also call pcnet32_free_ring */
1436static int pcnet32_alloc_ring(struct net_device *dev, char *name)
Hubert WS Lineabf0412005-09-14 11:39:25 -07001437{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001438 struct pcnet32_private *lp = dev->priv;
Hubert WS Lineabf0412005-09-14 11:39:25 -07001439
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001440 lp->tx_ring = pci_alloc_consistent(lp->pci_dev,
1441 sizeof(struct pcnet32_tx_head) *
1442 lp->tx_ring_size,
1443 &lp->tx_ring_dma_addr);
1444 if (lp->tx_ring == NULL) {
1445 if (pcnet32_debug & NETIF_MSG_DRV)
1446 printk("\n" KERN_ERR PFX
1447 "%s: Consistent memory allocation failed.\n",
1448 name);
1449 return -ENOMEM;
1450 }
Hubert WS Lineabf0412005-09-14 11:39:25 -07001451
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001452 lp->rx_ring = pci_alloc_consistent(lp->pci_dev,
1453 sizeof(struct pcnet32_rx_head) *
1454 lp->rx_ring_size,
1455 &lp->rx_ring_dma_addr);
1456 if (lp->rx_ring == NULL) {
1457 if (pcnet32_debug & NETIF_MSG_DRV)
1458 printk("\n" KERN_ERR PFX
1459 "%s: Consistent memory allocation failed.\n",
1460 name);
1461 return -ENOMEM;
1462 }
Hubert WS Lineabf0412005-09-14 11:39:25 -07001463
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001464 lp->tx_dma_addr = kmalloc(sizeof(dma_addr_t) * lp->tx_ring_size,
1465 GFP_ATOMIC);
1466 if (!lp->tx_dma_addr) {
1467 if (pcnet32_debug & NETIF_MSG_DRV)
1468 printk("\n" KERN_ERR PFX
1469 "%s: Memory allocation failed.\n", name);
1470 return -ENOMEM;
1471 }
1472 memset(lp->tx_dma_addr, 0, sizeof(dma_addr_t) * lp->tx_ring_size);
Hubert WS Lineabf0412005-09-14 11:39:25 -07001473
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001474 lp->rx_dma_addr = kmalloc(sizeof(dma_addr_t) * lp->rx_ring_size,
1475 GFP_ATOMIC);
1476 if (!lp->rx_dma_addr) {
1477 if (pcnet32_debug & NETIF_MSG_DRV)
1478 printk("\n" KERN_ERR PFX
1479 "%s: Memory allocation failed.\n", name);
1480 return -ENOMEM;
1481 }
1482 memset(lp->rx_dma_addr, 0, sizeof(dma_addr_t) * lp->rx_ring_size);
Hubert WS Lineabf0412005-09-14 11:39:25 -07001483
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001484 lp->tx_skbuff = kmalloc(sizeof(struct sk_buff *) * lp->tx_ring_size,
1485 GFP_ATOMIC);
1486 if (!lp->tx_skbuff) {
1487 if (pcnet32_debug & NETIF_MSG_DRV)
1488 printk("\n" KERN_ERR PFX
1489 "%s: Memory allocation failed.\n", name);
1490 return -ENOMEM;
1491 }
1492 memset(lp->tx_skbuff, 0, sizeof(struct sk_buff *) * lp->tx_ring_size);
Hubert WS Lineabf0412005-09-14 11:39:25 -07001493
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001494 lp->rx_skbuff = kmalloc(sizeof(struct sk_buff *) * lp->rx_ring_size,
1495 GFP_ATOMIC);
1496 if (!lp->rx_skbuff) {
1497 if (pcnet32_debug & NETIF_MSG_DRV)
1498 printk("\n" KERN_ERR PFX
1499 "%s: Memory allocation failed.\n", name);
1500 return -ENOMEM;
1501 }
1502 memset(lp->rx_skbuff, 0, sizeof(struct sk_buff *) * lp->rx_ring_size);
Hubert WS Lineabf0412005-09-14 11:39:25 -07001503
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001504 return 0;
Hubert WS Lineabf0412005-09-14 11:39:25 -07001505}
1506
Hubert WS Lineabf0412005-09-14 11:39:25 -07001507static void pcnet32_free_ring(struct net_device *dev)
1508{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001509 struct pcnet32_private *lp = dev->priv;
Hubert WS Lineabf0412005-09-14 11:39:25 -07001510
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001511 kfree(lp->tx_skbuff);
1512 lp->tx_skbuff = NULL;
Hubert WS Lineabf0412005-09-14 11:39:25 -07001513
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001514 kfree(lp->rx_skbuff);
1515 lp->rx_skbuff = NULL;
Hubert WS Lineabf0412005-09-14 11:39:25 -07001516
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001517 kfree(lp->tx_dma_addr);
1518 lp->tx_dma_addr = NULL;
Hubert WS Lineabf0412005-09-14 11:39:25 -07001519
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001520 kfree(lp->rx_dma_addr);
1521 lp->rx_dma_addr = NULL;
Hubert WS Lineabf0412005-09-14 11:39:25 -07001522
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001523 if (lp->tx_ring) {
1524 pci_free_consistent(lp->pci_dev,
1525 sizeof(struct pcnet32_tx_head) *
1526 lp->tx_ring_size, lp->tx_ring,
1527 lp->tx_ring_dma_addr);
1528 lp->tx_ring = NULL;
1529 }
Hubert WS Lineabf0412005-09-14 11:39:25 -07001530
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001531 if (lp->rx_ring) {
1532 pci_free_consistent(lp->pci_dev,
1533 sizeof(struct pcnet32_rx_head) *
1534 lp->rx_ring_size, lp->rx_ring,
1535 lp->rx_ring_dma_addr);
1536 lp->rx_ring = NULL;
1537 }
Hubert WS Lineabf0412005-09-14 11:39:25 -07001538}
1539
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001540static int pcnet32_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001542 struct pcnet32_private *lp = dev->priv;
1543 unsigned long ioaddr = dev->base_addr;
1544 u16 val;
1545 int i;
1546 int rc;
1547 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001549 if (request_irq(dev->irq, &pcnet32_interrupt,
1550 lp->shared_irq ? SA_SHIRQ : 0, dev->name,
1551 (void *)dev)) {
1552 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001555 spin_lock_irqsave(&lp->lock, flags);
1556 /* Check for a valid station address */
1557 if (!is_valid_ether_addr(dev->dev_addr)) {
1558 rc = -EINVAL;
1559 goto err_free_irq;
1560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001562 /* Reset the PCNET32 */
1563 lp->a.reset(ioaddr);
1564
1565 /* switch pcnet32 to 32bit mode */
1566 lp->a.write_bcr(ioaddr, 20, 2);
1567
1568 if (netif_msg_ifup(lp))
1569 printk(KERN_DEBUG
1570 "%s: pcnet32_open() irq %d tx/rx rings %#x/%#x init %#x.\n",
1571 dev->name, dev->irq, (u32) (lp->tx_ring_dma_addr),
1572 (u32) (lp->rx_ring_dma_addr),
1573 (u32) (lp->dma_addr +
1574 offsetof(struct pcnet32_private, init_block)));
1575
1576 /* set/reset autoselect bit */
1577 val = lp->a.read_bcr(ioaddr, 2) & ~2;
1578 if (lp->options & PCNET32_PORT_ASEL)
1579 val |= 2;
1580 lp->a.write_bcr(ioaddr, 2, val);
1581
1582 /* handle full duplex setting */
1583 if (lp->mii_if.full_duplex) {
1584 val = lp->a.read_bcr(ioaddr, 9) & ~3;
1585 if (lp->options & PCNET32_PORT_FD) {
1586 val |= 1;
1587 if (lp->options == (PCNET32_PORT_FD | PCNET32_PORT_AUI))
1588 val |= 2;
1589 } else if (lp->options & PCNET32_PORT_ASEL) {
1590 /* workaround of xSeries250, turn on for 79C975 only */
1591 i = ((lp->a.read_csr(ioaddr, 88) |
1592 (lp->a.
1593 read_csr(ioaddr, 89) << 16)) >> 12) & 0xffff;
1594 if (i == 0x2627)
1595 val |= 3;
1596 }
1597 lp->a.write_bcr(ioaddr, 9, val);
1598 }
1599
1600 /* set/reset GPSI bit in test register */
1601 val = lp->a.read_csr(ioaddr, 124) & ~0x10;
1602 if ((lp->options & PCNET32_PORT_PORTSEL) == PCNET32_PORT_GPSI)
1603 val |= 0x10;
1604 lp->a.write_csr(ioaddr, 124, val);
1605
1606 /* Allied Telesyn AT 2700/2701 FX are 100Mbit only and do not negotiate */
1607 if (lp->pci_dev->subsystem_vendor == PCI_VENDOR_ID_AT &&
Don Fry2964bbd2005-11-01 12:50:57 -08001608 (lp->pci_dev->subsystem_device == PCI_SUBDEVICE_ID_AT_2700FX ||
1609 lp->pci_dev->subsystem_device == PCI_SUBDEVICE_ID_AT_2701FX)) {
Don Fryac62ef02006-03-20 15:26:03 -08001610 if (lp->options & PCNET32_PORT_ASEL) {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001611 lp->options = PCNET32_PORT_FD | PCNET32_PORT_100;
1612 if (netif_msg_link(lp))
1613 printk(KERN_DEBUG
1614 "%s: Setting 100Mb-Full Duplex.\n",
1615 dev->name);
Don Fryac62ef02006-03-20 15:26:03 -08001616 }
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001617 }
1618 if (lp->phycount < 2) {
1619 /*
1620 * 24 Jun 2004 according AMD, in order to change the PHY,
1621 * DANAS (or DISPM for 79C976) must be set; then select the speed,
1622 * duplex, and/or enable auto negotiation, and clear DANAS
1623 */
1624 if (lp->mii && !(lp->options & PCNET32_PORT_ASEL)) {
1625 lp->a.write_bcr(ioaddr, 32,
1626 lp->a.read_bcr(ioaddr, 32) | 0x0080);
1627 /* disable Auto Negotiation, set 10Mpbs, HD */
1628 val = lp->a.read_bcr(ioaddr, 32) & ~0xb8;
1629 if (lp->options & PCNET32_PORT_FD)
1630 val |= 0x10;
1631 if (lp->options & PCNET32_PORT_100)
1632 val |= 0x08;
1633 lp->a.write_bcr(ioaddr, 32, val);
1634 } else {
1635 if (lp->options & PCNET32_PORT_ASEL) {
1636 lp->a.write_bcr(ioaddr, 32,
1637 lp->a.read_bcr(ioaddr,
1638 32) | 0x0080);
1639 /* enable auto negotiate, setup, disable fd */
1640 val = lp->a.read_bcr(ioaddr, 32) & ~0x98;
1641 val |= 0x20;
1642 lp->a.write_bcr(ioaddr, 32, val);
1643 }
1644 }
1645 } else {
1646 int first_phy = -1;
1647 u16 bmcr;
1648 u32 bcr9;
1649 struct ethtool_cmd ecmd;
1650
1651 /*
1652 * There is really no good other way to handle multiple PHYs
1653 * other than turning off all automatics
1654 */
1655 val = lp->a.read_bcr(ioaddr, 2);
1656 lp->a.write_bcr(ioaddr, 2, val & ~2);
1657 val = lp->a.read_bcr(ioaddr, 32);
1658 lp->a.write_bcr(ioaddr, 32, val & ~(1 << 7)); /* stop MII manager */
1659
1660 if (!(lp->options & PCNET32_PORT_ASEL)) {
1661 /* setup ecmd */
1662 ecmd.port = PORT_MII;
1663 ecmd.transceiver = XCVR_INTERNAL;
1664 ecmd.autoneg = AUTONEG_DISABLE;
1665 ecmd.speed =
1666 lp->
1667 options & PCNET32_PORT_100 ? SPEED_100 : SPEED_10;
1668 bcr9 = lp->a.read_bcr(ioaddr, 9);
1669
1670 if (lp->options & PCNET32_PORT_FD) {
1671 ecmd.duplex = DUPLEX_FULL;
1672 bcr9 |= (1 << 0);
1673 } else {
1674 ecmd.duplex = DUPLEX_HALF;
1675 bcr9 |= ~(1 << 0);
1676 }
1677 lp->a.write_bcr(ioaddr, 9, bcr9);
1678 }
1679
1680 for (i = 0; i < PCNET32_MAX_PHYS; i++) {
1681 if (lp->phymask & (1 << i)) {
1682 /* isolate all but the first PHY */
1683 bmcr = mdio_read(dev, i, MII_BMCR);
1684 if (first_phy == -1) {
1685 first_phy = i;
1686 mdio_write(dev, i, MII_BMCR,
1687 bmcr & ~BMCR_ISOLATE);
1688 } else {
1689 mdio_write(dev, i, MII_BMCR,
1690 bmcr | BMCR_ISOLATE);
1691 }
1692 /* use mii_ethtool_sset to setup PHY */
1693 lp->mii_if.phy_id = i;
1694 ecmd.phy_address = i;
1695 if (lp->options & PCNET32_PORT_ASEL) {
1696 mii_ethtool_gset(&lp->mii_if, &ecmd);
1697 ecmd.autoneg = AUTONEG_ENABLE;
1698 }
1699 mii_ethtool_sset(&lp->mii_if, &ecmd);
1700 }
1701 }
1702 lp->mii_if.phy_id = first_phy;
1703 if (netif_msg_link(lp))
1704 printk(KERN_INFO "%s: Using PHY number %d.\n",
1705 dev->name, first_phy);
1706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708#ifdef DO_DXSUFLO
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001709 if (lp->dxsuflo) { /* Disable transmit stop on underflow */
1710 val = lp->a.read_csr(ioaddr, 3);
1711 val |= 0x40;
1712 lp->a.write_csr(ioaddr, 3, val);
1713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714#endif
1715
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001716 lp->init_block.mode =
1717 le16_to_cpu((lp->options & PCNET32_PORT_PORTSEL) << 7);
1718 pcnet32_load_multicast(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001720 if (pcnet32_init_ring(dev)) {
1721 rc = -ENOMEM;
1722 goto err_free_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
Hubert WS Lineabf0412005-09-14 11:39:25 -07001724
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001725 /* Re-initialize the PCNET32, and start it when done. */
1726 lp->a.write_csr(ioaddr, 1, (lp->dma_addr +
1727 offsetof(struct pcnet32_private,
1728 init_block)) & 0xffff);
1729 lp->a.write_csr(ioaddr, 2,
1730 (lp->dma_addr +
1731 offsetof(struct pcnet32_private, init_block)) >> 16);
Hubert WS Lineabf0412005-09-14 11:39:25 -07001732
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001733 lp->a.write_csr(ioaddr, 4, 0x0915);
1734 lp->a.write_csr(ioaddr, 0, 0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001736 netif_start_queue(dev);
1737
1738 /* Print the link status and start the watchdog */
1739 pcnet32_check_media(dev, 1);
1740 mod_timer(&(lp->watchdog_timer), PCNET32_WATCHDOG_TIMEOUT);
1741
1742 i = 0;
1743 while (i++ < 100)
1744 if (lp->a.read_csr(ioaddr, 0) & 0x0100)
1745 break;
1746 /*
1747 * We used to clear the InitDone bit, 0x0100, here but Mark Stockton
1748 * reports that doing so triggers a bug in the '974.
1749 */
1750 lp->a.write_csr(ioaddr, 0, 0x0042);
1751
1752 if (netif_msg_ifup(lp))
1753 printk(KERN_DEBUG
1754 "%s: pcnet32 open after %d ticks, init block %#x csr0 %4.4x.\n",
1755 dev->name, i,
1756 (u32) (lp->dma_addr +
1757 offsetof(struct pcnet32_private, init_block)),
1758 lp->a.read_csr(ioaddr, 0));
1759
1760 spin_unlock_irqrestore(&lp->lock, flags);
1761
1762 return 0; /* Always succeed */
1763
1764 err_free_ring:
1765 /* free any allocated skbuffs */
1766 for (i = 0; i < lp->rx_ring_size; i++) {
1767 lp->rx_ring[i].status = 0;
1768 if (lp->rx_skbuff[i]) {
1769 pci_unmap_single(lp->pci_dev, lp->rx_dma_addr[i],
1770 PKT_BUF_SZ - 2, PCI_DMA_FROMDEVICE);
1771 dev_kfree_skb(lp->rx_skbuff[i]);
1772 }
1773 lp->rx_skbuff[i] = NULL;
1774 lp->rx_dma_addr[i] = 0;
1775 }
1776
1777 pcnet32_free_ring(dev);
1778
1779 /*
1780 * Switch back to 16bit mode to avoid problems with dumb
1781 * DOS packet driver after a warm reboot
1782 */
1783 lp->a.write_bcr(ioaddr, 20, 4);
1784
1785 err_free_irq:
1786 spin_unlock_irqrestore(&lp->lock, flags);
1787 free_irq(dev->irq, dev);
1788 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
1790
1791/*
1792 * The LANCE has been halted for one reason or another (busmaster memory
1793 * arbitration error, Tx FIFO underflow, driver stopped it to reconfigure,
1794 * etc.). Modern LANCE variants always reload their ring-buffer
1795 * configuration when restarted, so we must reinitialize our ring
1796 * context before restarting. As part of this reinitialization,
1797 * find all packets still on the Tx ring and pretend that they had been
1798 * sent (in effect, drop the packets on the floor) - the higher-level
1799 * protocols will time out and retransmit. It'd be better to shuffle
1800 * these skbs to a temp list and then actually re-Tx them after
1801 * restarting the chip, but I'm too lazy to do so right now. dplatt@3do.com
1802 */
1803
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001804static void pcnet32_purge_tx_ring(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001806 struct pcnet32_private *lp = dev->priv;
1807 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001809 for (i = 0; i < lp->tx_ring_size; i++) {
1810 lp->tx_ring[i].status = 0; /* CPU owns buffer */
1811 wmb(); /* Make sure adapter sees owner change */
1812 if (lp->tx_skbuff[i]) {
1813 pci_unmap_single(lp->pci_dev, lp->tx_dma_addr[i],
1814 lp->tx_skbuff[i]->len,
1815 PCI_DMA_TODEVICE);
1816 dev_kfree_skb_any(lp->tx_skbuff[i]);
1817 }
1818 lp->tx_skbuff[i] = NULL;
1819 lp->tx_dma_addr[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821}
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823/* Initialize the PCNET32 Rx and Tx rings. */
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001824static int pcnet32_init_ring(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001826 struct pcnet32_private *lp = dev->priv;
1827 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001829 lp->tx_full = 0;
1830 lp->cur_rx = lp->cur_tx = 0;
1831 lp->dirty_rx = lp->dirty_tx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001833 for (i = 0; i < lp->rx_ring_size; i++) {
1834 struct sk_buff *rx_skbuff = lp->rx_skbuff[i];
1835 if (rx_skbuff == NULL) {
1836 if (!
1837 (rx_skbuff = lp->rx_skbuff[i] =
1838 dev_alloc_skb(PKT_BUF_SZ))) {
1839 /* there is not much, we can do at this point */
1840 if (pcnet32_debug & NETIF_MSG_DRV)
1841 printk(KERN_ERR
1842 "%s: pcnet32_init_ring dev_alloc_skb failed.\n",
1843 dev->name);
1844 return -1;
1845 }
1846 skb_reserve(rx_skbuff, 2);
1847 }
1848
1849 rmb();
1850 if (lp->rx_dma_addr[i] == 0)
1851 lp->rx_dma_addr[i] =
1852 pci_map_single(lp->pci_dev, rx_skbuff->data,
1853 PKT_BUF_SZ - 2, PCI_DMA_FROMDEVICE);
1854 lp->rx_ring[i].base = (u32) le32_to_cpu(lp->rx_dma_addr[i]);
1855 lp->rx_ring[i].buf_length = le16_to_cpu(2 - PKT_BUF_SZ);
1856 wmb(); /* Make sure owner changes after all others are visible */
1857 lp->rx_ring[i].status = le16_to_cpu(0x8000);
1858 }
1859 /* The Tx buffer address is filled in as needed, but we do need to clear
1860 * the upper ownership bit. */
1861 for (i = 0; i < lp->tx_ring_size; i++) {
1862 lp->tx_ring[i].status = 0; /* CPU owns buffer */
1863 wmb(); /* Make sure adapter sees owner change */
1864 lp->tx_ring[i].base = 0;
1865 lp->tx_dma_addr[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
1867
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001868 lp->init_block.tlen_rlen =
1869 le16_to_cpu(lp->tx_len_bits | lp->rx_len_bits);
1870 for (i = 0; i < 6; i++)
1871 lp->init_block.phys_addr[i] = dev->dev_addr[i];
1872 lp->init_block.rx_ring = (u32) le32_to_cpu(lp->rx_ring_dma_addr);
1873 lp->init_block.tx_ring = (u32) le32_to_cpu(lp->tx_ring_dma_addr);
1874 wmb(); /* Make sure all changes are visible */
1875 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876}
1877
1878/* the pcnet32 has been issued a stop or reset. Wait for the stop bit
1879 * then flush the pending transmit operations, re-initialize the ring,
1880 * and tell the chip to initialize.
1881 */
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001882static void pcnet32_restart(struct net_device *dev, unsigned int csr0_bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001884 struct pcnet32_private *lp = dev->priv;
1885 unsigned long ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001888 /* wait for stop */
1889 for (i = 0; i < 100; i++)
1890 if (lp->a.read_csr(ioaddr, 0) & 0x0004)
1891 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001893 if (i >= 100 && netif_msg_drv(lp))
1894 printk(KERN_ERR
1895 "%s: pcnet32_restart timed out waiting for stop.\n",
1896 dev->name);
1897
1898 pcnet32_purge_tx_ring(dev);
1899 if (pcnet32_init_ring(dev))
1900 return;
1901
1902 /* ReInit Ring */
1903 lp->a.write_csr(ioaddr, 0, 1);
1904 i = 0;
1905 while (i++ < 1000)
1906 if (lp->a.read_csr(ioaddr, 0) & 0x0100)
1907 break;
1908
1909 lp->a.write_csr(ioaddr, 0, csr0_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
1911
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001912static void pcnet32_tx_timeout(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001914 struct pcnet32_private *lp = dev->priv;
1915 unsigned long ioaddr = dev->base_addr, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001917 spin_lock_irqsave(&lp->lock, flags);
1918 /* Transmitter timeout, serious problems. */
1919 if (pcnet32_debug & NETIF_MSG_DRV)
1920 printk(KERN_ERR
1921 "%s: transmit timed out, status %4.4x, resetting.\n",
1922 dev->name, lp->a.read_csr(ioaddr, 0));
1923 lp->a.write_csr(ioaddr, 0, 0x0004);
1924 lp->stats.tx_errors++;
1925 if (netif_msg_tx_err(lp)) {
1926 int i;
1927 printk(KERN_DEBUG
1928 " Ring data dump: dirty_tx %d cur_tx %d%s cur_rx %d.",
1929 lp->dirty_tx, lp->cur_tx, lp->tx_full ? " (full)" : "",
1930 lp->cur_rx);
1931 for (i = 0; i < lp->rx_ring_size; i++)
1932 printk("%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
1933 le32_to_cpu(lp->rx_ring[i].base),
1934 (-le16_to_cpu(lp->rx_ring[i].buf_length)) &
1935 0xffff, le32_to_cpu(lp->rx_ring[i].msg_length),
1936 le16_to_cpu(lp->rx_ring[i].status));
1937 for (i = 0; i < lp->tx_ring_size; i++)
1938 printk("%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
1939 le32_to_cpu(lp->tx_ring[i].base),
1940 (-le16_to_cpu(lp->tx_ring[i].length)) & 0xffff,
1941 le32_to_cpu(lp->tx_ring[i].misc),
1942 le16_to_cpu(lp->tx_ring[i].status));
1943 printk("\n");
1944 }
1945 pcnet32_restart(dev, 0x0042);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001947 dev->trans_start = jiffies;
1948 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001950 spin_unlock_irqrestore(&lp->lock, flags);
1951}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001953static int pcnet32_start_xmit(struct sk_buff *skb, struct net_device *dev)
1954{
1955 struct pcnet32_private *lp = dev->priv;
1956 unsigned long ioaddr = dev->base_addr;
1957 u16 status;
1958 int entry;
1959 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001961 spin_lock_irqsave(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001963 if (netif_msg_tx_queued(lp)) {
1964 printk(KERN_DEBUG
1965 "%s: pcnet32_start_xmit() called, csr0 %4.4x.\n",
1966 dev->name, lp->a.read_csr(ioaddr, 0));
1967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001969 /* Default status -- will not enable Successful-TxDone
1970 * interrupt when that option is available to us.
1971 */
1972 status = 0x8300;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001974 /* Fill in a Tx ring entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001976 /* Mask to ring buffer boundary. */
1977 entry = lp->cur_tx & lp->tx_mod_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001979 /* Caution: the write order is important here, set the status
1980 * with the "ownership" bits last. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001982 lp->tx_ring[entry].length = le16_to_cpu(-skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001984 lp->tx_ring[entry].misc = 0x00000000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001986 lp->tx_skbuff[entry] = skb;
1987 lp->tx_dma_addr[entry] =
1988 pci_map_single(lp->pci_dev, skb->data, skb->len, PCI_DMA_TODEVICE);
1989 lp->tx_ring[entry].base = (u32) le32_to_cpu(lp->tx_dma_addr[entry]);
1990 wmb(); /* Make sure owner changes after all others are visible */
1991 lp->tx_ring[entry].status = le16_to_cpu(status);
1992
1993 lp->cur_tx++;
1994 lp->stats.tx_bytes += skb->len;
1995
1996 /* Trigger an immediate send poll. */
1997 lp->a.write_csr(ioaddr, 0, 0x0048);
1998
1999 dev->trans_start = jiffies;
2000
2001 if (lp->tx_ring[(entry + 1) & lp->tx_mod_mask].base != 0) {
2002 lp->tx_full = 1;
2003 netif_stop_queue(dev);
2004 }
2005 spin_unlock_irqrestore(&lp->lock, flags);
2006 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
2009/* The PCNET32 interrupt handler. */
2010static irqreturn_t
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002011pcnet32_interrupt(int irq, void *dev_id, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002013 struct net_device *dev = dev_id;
2014 struct pcnet32_private *lp;
2015 unsigned long ioaddr;
2016 u16 csr0, rap;
2017 int boguscnt = max_interrupt_work;
2018 int must_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002020 if (!dev) {
2021 if (pcnet32_debug & NETIF_MSG_INTR)
2022 printk(KERN_DEBUG "%s(): irq %d for unknown device\n",
2023 __FUNCTION__, irq);
2024 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002027 ioaddr = dev->base_addr;
2028 lp = dev->priv;
2029
2030 spin_lock(&lp->lock);
2031
2032 rap = lp->a.read_rap(ioaddr);
2033 while ((csr0 = lp->a.read_csr(ioaddr, 0)) & 0x8f00 && --boguscnt >= 0) {
2034 if (csr0 == 0xffff) {
2035 break; /* PCMCIA remove happened */
2036 }
2037 /* Acknowledge all of the current interrupt sources ASAP. */
2038 lp->a.write_csr(ioaddr, 0, csr0 & ~0x004f);
2039
2040 must_restart = 0;
2041
2042 if (netif_msg_intr(lp))
2043 printk(KERN_DEBUG
2044 "%s: interrupt csr0=%#2.2x new csr=%#2.2x.\n",
2045 dev->name, csr0, lp->a.read_csr(ioaddr, 0));
2046
2047 if (csr0 & 0x0400) /* Rx interrupt */
2048 pcnet32_rx(dev);
2049
2050 if (csr0 & 0x0200) { /* Tx-done interrupt */
2051 unsigned int dirty_tx = lp->dirty_tx;
2052 int delta;
2053
2054 while (dirty_tx != lp->cur_tx) {
2055 int entry = dirty_tx & lp->tx_mod_mask;
2056 int status =
2057 (short)le16_to_cpu(lp->tx_ring[entry].
2058 status);
2059
2060 if (status < 0)
2061 break; /* It still hasn't been Txed */
2062
2063 lp->tx_ring[entry].base = 0;
2064
2065 if (status & 0x4000) {
2066 /* There was an major error, log it. */
2067 int err_status =
2068 le32_to_cpu(lp->tx_ring[entry].
2069 misc);
2070 lp->stats.tx_errors++;
2071 if (netif_msg_tx_err(lp))
2072 printk(KERN_ERR
2073 "%s: Tx error status=%04x err_status=%08x\n",
2074 dev->name, status,
2075 err_status);
2076 if (err_status & 0x04000000)
2077 lp->stats.tx_aborted_errors++;
2078 if (err_status & 0x08000000)
2079 lp->stats.tx_carrier_errors++;
2080 if (err_status & 0x10000000)
2081 lp->stats.tx_window_errors++;
2082#ifndef DO_DXSUFLO
2083 if (err_status & 0x40000000) {
2084 lp->stats.tx_fifo_errors++;
2085 /* Ackk! On FIFO errors the Tx unit is turned off! */
2086 /* Remove this verbosity later! */
2087 if (netif_msg_tx_err(lp))
2088 printk(KERN_ERR
2089 "%s: Tx FIFO error! CSR0=%4.4x\n",
2090 dev->name, csr0);
2091 must_restart = 1;
2092 }
2093#else
2094 if (err_status & 0x40000000) {
2095 lp->stats.tx_fifo_errors++;
2096 if (!lp->dxsuflo) { /* If controller doesn't recover ... */
2097 /* Ackk! On FIFO errors the Tx unit is turned off! */
2098 /* Remove this verbosity later! */
2099 if (netif_msg_tx_err
2100 (lp))
2101 printk(KERN_ERR
2102 "%s: Tx FIFO error! CSR0=%4.4x\n",
2103 dev->
2104 name,
2105 csr0);
2106 must_restart = 1;
2107 }
2108 }
2109#endif
2110 } else {
2111 if (status & 0x1800)
2112 lp->stats.collisions++;
2113 lp->stats.tx_packets++;
2114 }
2115
2116 /* We must free the original skb */
2117 if (lp->tx_skbuff[entry]) {
2118 pci_unmap_single(lp->pci_dev,
2119 lp->tx_dma_addr[entry],
2120 lp->tx_skbuff[entry]->
2121 len, PCI_DMA_TODEVICE);
2122 dev_kfree_skb_irq(lp->tx_skbuff[entry]);
2123 lp->tx_skbuff[entry] = NULL;
2124 lp->tx_dma_addr[entry] = 0;
2125 }
2126 dirty_tx++;
2127 }
2128
2129 delta =
2130 (lp->cur_tx - dirty_tx) & (lp->tx_mod_mask +
2131 lp->tx_ring_size);
2132 if (delta > lp->tx_ring_size) {
2133 if (netif_msg_drv(lp))
2134 printk(KERN_ERR
2135 "%s: out-of-sync dirty pointer, %d vs. %d, full=%d.\n",
2136 dev->name, dirty_tx, lp->cur_tx,
2137 lp->tx_full);
2138 dirty_tx += lp->tx_ring_size;
2139 delta -= lp->tx_ring_size;
2140 }
2141
2142 if (lp->tx_full &&
2143 netif_queue_stopped(dev) &&
2144 delta < lp->tx_ring_size - 2) {
2145 /* The ring is no longer full, clear tbusy. */
2146 lp->tx_full = 0;
2147 netif_wake_queue(dev);
2148 }
2149 lp->dirty_tx = dirty_tx;
2150 }
2151
2152 /* Log misc errors. */
2153 if (csr0 & 0x4000)
2154 lp->stats.tx_errors++; /* Tx babble. */
2155 if (csr0 & 0x1000) {
2156 /*
2157 * this happens when our receive ring is full. This shouldn't
2158 * be a problem as we will see normal rx interrupts for the frames
2159 * in the receive ring. But there are some PCI chipsets (I can
2160 * reproduce this on SP3G with Intel saturn chipset) which have
2161 * sometimes problems and will fill up the receive ring with
2162 * error descriptors. In this situation we don't get a rx
2163 * interrupt, but a missed frame interrupt sooner or later.
2164 * So we try to clean up our receive ring here.
2165 */
2166 pcnet32_rx(dev);
2167 lp->stats.rx_errors++; /* Missed a Rx frame. */
2168 }
2169 if (csr0 & 0x0800) {
2170 if (netif_msg_drv(lp))
2171 printk(KERN_ERR
2172 "%s: Bus master arbitration failure, status %4.4x.\n",
2173 dev->name, csr0);
2174 /* unlike for the lance, there is no restart needed */
2175 }
2176
2177 if (must_restart) {
2178 /* reset the chip to clear the error condition, then restart */
2179 lp->a.reset(ioaddr);
2180 lp->a.write_csr(ioaddr, 4, 0x0915);
2181 pcnet32_restart(dev, 0x0002);
2182 netif_wake_queue(dev);
2183 }
2184 }
2185
2186 /* Set interrupt enable. */
2187 lp->a.write_csr(ioaddr, 0, 0x0040);
2188 lp->a.write_rap(ioaddr, rap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 if (netif_msg_intr(lp))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002191 printk(KERN_DEBUG "%s: exiting interrupt, csr0=%#4.4x.\n",
2192 dev->name, lp->a.read_csr(ioaddr, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002194 spin_unlock(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002196 return IRQ_HANDLED;
2197}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002199static int pcnet32_rx(struct net_device *dev)
2200{
2201 struct pcnet32_private *lp = dev->priv;
2202 int entry = lp->cur_rx & lp->rx_mod_mask;
2203 int boguscnt = lp->rx_ring_size / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002205 /* If we own the next entry, it's a new packet. Send it up. */
2206 while ((short)le16_to_cpu(lp->rx_ring[entry].status) >= 0) {
2207 int status = (short)le16_to_cpu(lp->rx_ring[entry].status) >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002209 if (status != 0x03) { /* There was an error. */
2210 /*
2211 * There is a tricky error noted by John Murphy,
2212 * <murf@perftech.com> to Russ Nelson: Even with full-sized
2213 * buffers it's possible for a jabber packet to use two
2214 * buffers, with only the last correctly noting the error.
2215 */
2216 if (status & 0x01) /* Only count a general error at the */
2217 lp->stats.rx_errors++; /* end of a packet. */
2218 if (status & 0x20)
2219 lp->stats.rx_frame_errors++;
2220 if (status & 0x10)
2221 lp->stats.rx_over_errors++;
2222 if (status & 0x08)
2223 lp->stats.rx_crc_errors++;
2224 if (status & 0x04)
2225 lp->stats.rx_fifo_errors++;
2226 lp->rx_ring[entry].status &= le16_to_cpu(0x03ff);
2227 } else {
2228 /* Malloc up new buffer, compatible with net-2e. */
2229 short pkt_len =
2230 (le32_to_cpu(lp->rx_ring[entry].msg_length) & 0xfff)
2231 - 4;
2232 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002234 /* Discard oversize frames. */
2235 if (unlikely(pkt_len > PKT_BUF_SZ - 2)) {
2236 if (netif_msg_drv(lp))
2237 printk(KERN_ERR
2238 "%s: Impossible packet size %d!\n",
2239 dev->name, pkt_len);
2240 lp->stats.rx_errors++;
2241 } else if (pkt_len < 60) {
2242 if (netif_msg_rx_err(lp))
2243 printk(KERN_ERR "%s: Runt packet!\n",
2244 dev->name);
2245 lp->stats.rx_errors++;
2246 } else {
2247 int rx_in_place = 0;
2248
2249 if (pkt_len > rx_copybreak) {
2250 struct sk_buff *newskb;
2251
2252 if ((newskb =
2253 dev_alloc_skb(PKT_BUF_SZ))) {
2254 skb_reserve(newskb, 2);
2255 skb = lp->rx_skbuff[entry];
2256 pci_unmap_single(lp->pci_dev,
2257 lp->
2258 rx_dma_addr
2259 [entry],
2260 PKT_BUF_SZ - 2,
2261 PCI_DMA_FROMDEVICE);
2262 skb_put(skb, pkt_len);
2263 lp->rx_skbuff[entry] = newskb;
2264 newskb->dev = dev;
2265 lp->rx_dma_addr[entry] =
2266 pci_map_single(lp->pci_dev,
2267 newskb->data,
2268 PKT_BUF_SZ -
2269 2,
2270 PCI_DMA_FROMDEVICE);
2271 lp->rx_ring[entry].base =
2272 le32_to_cpu(lp->
2273 rx_dma_addr
2274 [entry]);
2275 rx_in_place = 1;
2276 } else
2277 skb = NULL;
2278 } else {
2279 skb = dev_alloc_skb(pkt_len + 2);
2280 }
2281
2282 if (skb == NULL) {
2283 int i;
2284 if (netif_msg_drv(lp))
2285 printk(KERN_ERR
2286 "%s: Memory squeeze, deferring packet.\n",
2287 dev->name);
2288 for (i = 0; i < lp->rx_ring_size; i++)
2289 if ((short)
2290 le16_to_cpu(lp->
2291 rx_ring[(entry +
2292 i)
2293 & lp->
2294 rx_mod_mask].
2295 status) < 0)
2296 break;
2297
2298 if (i > lp->rx_ring_size - 2) {
2299 lp->stats.rx_dropped++;
2300 lp->rx_ring[entry].status |=
2301 le16_to_cpu(0x8000);
2302 wmb(); /* Make sure adapter sees owner change */
2303 lp->cur_rx++;
2304 }
2305 break;
2306 }
2307 skb->dev = dev;
2308 if (!rx_in_place) {
2309 skb_reserve(skb, 2); /* 16 byte align */
2310 skb_put(skb, pkt_len); /* Make room */
2311 pci_dma_sync_single_for_cpu(lp->pci_dev,
2312 lp->
2313 rx_dma_addr
2314 [entry],
2315 PKT_BUF_SZ -
2316 2,
2317 PCI_DMA_FROMDEVICE);
2318 eth_copy_and_sum(skb,
2319 (unsigned char *)(lp->
2320 rx_skbuff
2321 [entry]->
2322 data),
2323 pkt_len, 0);
2324 pci_dma_sync_single_for_device(lp->
2325 pci_dev,
2326 lp->
2327 rx_dma_addr
2328 [entry],
2329 PKT_BUF_SZ
2330 - 2,
2331 PCI_DMA_FROMDEVICE);
2332 }
2333 lp->stats.rx_bytes += skb->len;
2334 skb->protocol = eth_type_trans(skb, dev);
2335 netif_rx(skb);
2336 dev->last_rx = jiffies;
2337 lp->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 }
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002340 /*
2341 * The docs say that the buffer length isn't touched, but Andrew Boyd
2342 * of QNX reports that some revs of the 79C965 clear it.
2343 */
2344 lp->rx_ring[entry].buf_length = le16_to_cpu(2 - PKT_BUF_SZ);
2345 wmb(); /* Make sure owner changes after all others are visible */
2346 lp->rx_ring[entry].status |= le16_to_cpu(0x8000);
2347 entry = (++lp->cur_rx) & lp->rx_mod_mask;
2348 if (--boguscnt <= 0)
2349 break; /* don't stay in loop forever */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 }
2351
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002352 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353}
2354
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002355static int pcnet32_close(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002357 unsigned long ioaddr = dev->base_addr;
2358 struct pcnet32_private *lp = dev->priv;
2359 int i;
2360 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002362 del_timer_sync(&lp->watchdog_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002364 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002366 spin_lock_irqsave(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002368 lp->stats.rx_missed_errors = lp->a.read_csr(ioaddr, 112);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002370 if (netif_msg_ifdown(lp))
2371 printk(KERN_DEBUG
2372 "%s: Shutting down ethercard, status was %2.2x.\n",
2373 dev->name, lp->a.read_csr(ioaddr, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002375 /* We stop the PCNET32 here -- it occasionally polls memory if we don't. */
2376 lp->a.write_csr(ioaddr, 0, 0x0004);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 /*
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002379 * Switch back to 16bit mode to avoid problems with dumb
2380 * DOS packet driver after a warm reboot
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 */
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002382 lp->a.write_bcr(ioaddr, 20, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002384 spin_unlock_irqrestore(&lp->lock, flags);
2385
2386 free_irq(dev->irq, dev);
2387
2388 spin_lock_irqsave(&lp->lock, flags);
2389
2390 /* free all allocated skbuffs */
2391 for (i = 0; i < lp->rx_ring_size; i++) {
2392 lp->rx_ring[i].status = 0;
2393 wmb(); /* Make sure adapter sees owner change */
2394 if (lp->rx_skbuff[i]) {
2395 pci_unmap_single(lp->pci_dev, lp->rx_dma_addr[i],
2396 PKT_BUF_SZ - 2, PCI_DMA_FROMDEVICE);
2397 dev_kfree_skb(lp->rx_skbuff[i]);
2398 }
2399 lp->rx_skbuff[i] = NULL;
2400 lp->rx_dma_addr[i] = 0;
2401 }
2402
2403 for (i = 0; i < lp->tx_ring_size; i++) {
2404 lp->tx_ring[i].status = 0; /* CPU owns buffer */
2405 wmb(); /* Make sure adapter sees owner change */
2406 if (lp->tx_skbuff[i]) {
2407 pci_unmap_single(lp->pci_dev, lp->tx_dma_addr[i],
2408 lp->tx_skbuff[i]->len,
2409 PCI_DMA_TODEVICE);
2410 dev_kfree_skb(lp->tx_skbuff[i]);
2411 }
2412 lp->tx_skbuff[i] = NULL;
2413 lp->tx_dma_addr[i] = 0;
2414 }
2415
2416 spin_unlock_irqrestore(&lp->lock, flags);
2417
2418 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419}
2420
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002421static struct net_device_stats *pcnet32_get_stats(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002423 struct pcnet32_private *lp = dev->priv;
2424 unsigned long ioaddr = dev->base_addr;
2425 u16 saved_addr;
2426 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002428 spin_lock_irqsave(&lp->lock, flags);
2429 saved_addr = lp->a.read_rap(ioaddr);
2430 lp->stats.rx_missed_errors = lp->a.read_csr(ioaddr, 112);
2431 lp->a.write_rap(ioaddr, saved_addr);
2432 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002434 return &lp->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435}
2436
2437/* taken from the sunlance driver, which it took from the depca driver */
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002438static void pcnet32_load_multicast(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002440 struct pcnet32_private *lp = dev->priv;
2441 volatile struct pcnet32_init_block *ib = &lp->init_block;
2442 volatile u16 *mcast_table = (u16 *) & ib->filter;
2443 struct dev_mc_list *dmi = dev->mc_list;
2444 char *addrs;
2445 int i;
2446 u32 crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002448 /* set all multicast bits */
2449 if (dev->flags & IFF_ALLMULTI) {
2450 ib->filter[0] = 0xffffffff;
2451 ib->filter[1] = 0xffffffff;
2452 return;
2453 }
2454 /* clear the multicast filter */
2455 ib->filter[0] = 0;
2456 ib->filter[1] = 0;
2457
2458 /* Add addresses */
2459 for (i = 0; i < dev->mc_count; i++) {
2460 addrs = dmi->dmi_addr;
2461 dmi = dmi->next;
2462
2463 /* multicast address? */
2464 if (!(*addrs & 1))
2465 continue;
2466
2467 crc = ether_crc_le(6, addrs);
2468 crc = crc >> 26;
2469 mcast_table[crc >> 4] =
2470 le16_to_cpu(le16_to_cpu(mcast_table[crc >> 4]) |
2471 (1 << (crc & 0xf)));
2472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474}
2475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476/*
2477 * Set or clear the multicast filter for this adaptor.
2478 */
2479static void pcnet32_set_multicast_list(struct net_device *dev)
2480{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002481 unsigned long ioaddr = dev->base_addr, flags;
2482 struct pcnet32_private *lp = dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002484 spin_lock_irqsave(&lp->lock, flags);
2485 if (dev->flags & IFF_PROMISC) {
2486 /* Log any net taps. */
2487 if (netif_msg_hw(lp))
2488 printk(KERN_INFO "%s: Promiscuous mode enabled.\n",
2489 dev->name);
2490 lp->init_block.mode =
2491 le16_to_cpu(0x8000 | (lp->options & PCNET32_PORT_PORTSEL) <<
2492 7);
2493 } else {
2494 lp->init_block.mode =
2495 le16_to_cpu((lp->options & PCNET32_PORT_PORTSEL) << 7);
2496 pcnet32_load_multicast(dev);
2497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002499 lp->a.write_csr(ioaddr, 0, 0x0004); /* Temporarily stop the lance. */
2500 pcnet32_restart(dev, 0x0042); /* Resume normal operation */
2501 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002503 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
2505
2506/* This routine assumes that the lp->lock is held */
2507static int mdio_read(struct net_device *dev, int phy_id, int reg_num)
2508{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002509 struct pcnet32_private *lp = dev->priv;
2510 unsigned long ioaddr = dev->base_addr;
2511 u16 val_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002513 if (!lp->mii)
2514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002516 lp->a.write_bcr(ioaddr, 33, ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
2517 val_out = lp->a.read_bcr(ioaddr, 34);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002519 return val_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
2521
2522/* This routine assumes that the lp->lock is held */
2523static void mdio_write(struct net_device *dev, int phy_id, int reg_num, int val)
2524{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002525 struct pcnet32_private *lp = dev->priv;
2526 unsigned long ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002528 if (!lp->mii)
2529 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002531 lp->a.write_bcr(ioaddr, 33, ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
2532 lp->a.write_bcr(ioaddr, 34, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533}
2534
2535static int pcnet32_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2536{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002537 struct pcnet32_private *lp = dev->priv;
2538 int rc;
2539 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002541 /* SIOC[GS]MIIxxx ioctls */
2542 if (lp->mii) {
2543 spin_lock_irqsave(&lp->lock, flags);
2544 rc = generic_mii_ioctl(&lp->mii_if, if_mii(rq), cmd, NULL);
2545 spin_unlock_irqrestore(&lp->lock, flags);
2546 } else {
2547 rc = -EOPNOTSUPP;
2548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002550 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551}
2552
Don Fryac62ef02006-03-20 15:26:03 -08002553static int pcnet32_check_otherphy(struct net_device *dev)
2554{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002555 struct pcnet32_private *lp = dev->priv;
2556 struct mii_if_info mii = lp->mii_if;
2557 u16 bmcr;
2558 int i;
Don Fryac62ef02006-03-20 15:26:03 -08002559
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002560 for (i = 0; i < PCNET32_MAX_PHYS; i++) {
2561 if (i == lp->mii_if.phy_id)
2562 continue; /* skip active phy */
2563 if (lp->phymask & (1 << i)) {
2564 mii.phy_id = i;
2565 if (mii_link_ok(&mii)) {
2566 /* found PHY with active link */
2567 if (netif_msg_link(lp))
2568 printk(KERN_INFO
2569 "%s: Using PHY number %d.\n",
2570 dev->name, i);
Don Fryac62ef02006-03-20 15:26:03 -08002571
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002572 /* isolate inactive phy */
2573 bmcr =
2574 mdio_read(dev, lp->mii_if.phy_id, MII_BMCR);
2575 mdio_write(dev, lp->mii_if.phy_id, MII_BMCR,
2576 bmcr | BMCR_ISOLATE);
Don Fryac62ef02006-03-20 15:26:03 -08002577
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002578 /* de-isolate new phy */
2579 bmcr = mdio_read(dev, i, MII_BMCR);
2580 mdio_write(dev, i, MII_BMCR,
2581 bmcr & ~BMCR_ISOLATE);
Don Fryac62ef02006-03-20 15:26:03 -08002582
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002583 /* set new phy address */
2584 lp->mii_if.phy_id = i;
2585 return 1;
2586 }
2587 }
Don Fryac62ef02006-03-20 15:26:03 -08002588 }
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002589 return 0;
Don Fryac62ef02006-03-20 15:26:03 -08002590}
2591
2592/*
2593 * Show the status of the media. Similar to mii_check_media however it
2594 * correctly shows the link speed for all (tested) pcnet32 variants.
2595 * Devices with no mii just report link state without speed.
2596 *
2597 * Caller is assumed to hold and release the lp->lock.
2598 */
2599
2600static void pcnet32_check_media(struct net_device *dev, int verbose)
2601{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002602 struct pcnet32_private *lp = dev->priv;
2603 int curr_link;
2604 int prev_link = netif_carrier_ok(dev) ? 1 : 0;
2605 u32 bcr9;
Don Fryac62ef02006-03-20 15:26:03 -08002606
Don Fryac62ef02006-03-20 15:26:03 -08002607 if (lp->mii) {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002608 curr_link = mii_link_ok(&lp->mii_if);
Don Fryac62ef02006-03-20 15:26:03 -08002609 } else {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002610 ulong ioaddr = dev->base_addr; /* card base I/O address */
2611 curr_link = (lp->a.read_bcr(ioaddr, 4) != 0xc0);
Don Fryac62ef02006-03-20 15:26:03 -08002612 }
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002613 if (!curr_link) {
2614 if (prev_link || verbose) {
2615 netif_carrier_off(dev);
2616 if (netif_msg_link(lp))
2617 printk(KERN_INFO "%s: link down\n", dev->name);
2618 }
2619 if (lp->phycount > 1) {
2620 curr_link = pcnet32_check_otherphy(dev);
2621 prev_link = 0;
2622 }
2623 } else if (verbose || !prev_link) {
2624 netif_carrier_on(dev);
2625 if (lp->mii) {
2626 if (netif_msg_link(lp)) {
2627 struct ethtool_cmd ecmd;
2628 mii_ethtool_gset(&lp->mii_if, &ecmd);
2629 printk(KERN_INFO
2630 "%s: link up, %sMbps, %s-duplex\n",
2631 dev->name,
2632 (ecmd.speed == SPEED_100) ? "100" : "10",
2633 (ecmd.duplex ==
2634 DUPLEX_FULL) ? "full" : "half");
2635 }
2636 bcr9 = lp->a.read_bcr(dev->base_addr, 9);
2637 if ((bcr9 & (1 << 0)) != lp->mii_if.full_duplex) {
2638 if (lp->mii_if.full_duplex)
2639 bcr9 |= (1 << 0);
2640 else
2641 bcr9 &= ~(1 << 0);
2642 lp->a.write_bcr(dev->base_addr, 9, bcr9);
2643 }
2644 } else {
2645 if (netif_msg_link(lp))
2646 printk(KERN_INFO "%s: link up\n", dev->name);
2647 }
2648 }
Don Fryac62ef02006-03-20 15:26:03 -08002649}
2650
2651/*
2652 * Check for loss of link and link establishment.
2653 * Can not use mii_check_media because it does nothing if mode is forced.
2654 */
2655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656static void pcnet32_watchdog(struct net_device *dev)
2657{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002658 struct pcnet32_private *lp = dev->priv;
2659 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002661 /* Print the link status if it has changed */
2662 spin_lock_irqsave(&lp->lock, flags);
2663 pcnet32_check_media(dev, 0);
2664 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002666 mod_timer(&(lp->watchdog_timer), PCNET32_WATCHDOG_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667}
2668
2669static void __devexit pcnet32_remove_one(struct pci_dev *pdev)
2670{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002671 struct net_device *dev = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002673 if (dev) {
2674 struct pcnet32_private *lp = dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002676 unregister_netdev(dev);
2677 pcnet32_free_ring(dev);
2678 release_region(dev->base_addr, PCNET32_TOTAL_SIZE);
2679 pci_free_consistent(lp->pci_dev, sizeof(*lp), lp, lp->dma_addr);
2680 free_netdev(dev);
2681 pci_disable_device(pdev);
2682 pci_set_drvdata(pdev, NULL);
2683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684}
2685
2686static struct pci_driver pcnet32_driver = {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002687 .name = DRV_NAME,
2688 .probe = pcnet32_probe_pci,
2689 .remove = __devexit_p(pcnet32_remove_one),
2690 .id_table = pcnet32_pci_tbl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691};
2692
2693/* An additional parameter that may be passed in... */
2694static int debug = -1;
2695static int tx_start_pt = -1;
2696static int pcnet32_have_pci;
2697
2698module_param(debug, int, 0);
2699MODULE_PARM_DESC(debug, DRV_NAME " debug level");
2700module_param(max_interrupt_work, int, 0);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002701MODULE_PARM_DESC(max_interrupt_work,
2702 DRV_NAME " maximum events handled per interrupt");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703module_param(rx_copybreak, int, 0);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002704MODULE_PARM_DESC(rx_copybreak,
2705 DRV_NAME " copy breakpoint for copy-only-tiny-frames");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706module_param(tx_start_pt, int, 0);
2707MODULE_PARM_DESC(tx_start_pt, DRV_NAME " transmit start point (0-3)");
2708module_param(pcnet32vlb, int, 0);
2709MODULE_PARM_DESC(pcnet32vlb, DRV_NAME " Vesa local bus (VLB) support (0/1)");
2710module_param_array(options, int, NULL, 0);
2711MODULE_PARM_DESC(options, DRV_NAME " initial option setting(s) (0-15)");
2712module_param_array(full_duplex, int, NULL, 0);
2713MODULE_PARM_DESC(full_duplex, DRV_NAME " full duplex setting(s) (1)");
2714/* Module Parameter for HomePNA cards added by Patrick Simmons, 2004 */
2715module_param_array(homepna, int, NULL, 0);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002716MODULE_PARM_DESC(homepna,
2717 DRV_NAME
2718 " mode for 79C978 cards (1 for HomePNA, 0 for Ethernet, default Ethernet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
2720MODULE_AUTHOR("Thomas Bogendoerfer");
2721MODULE_DESCRIPTION("Driver for PCnet32 and PCnetPCI based ethercards");
2722MODULE_LICENSE("GPL");
2723
2724#define PCNET32_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
2725
2726static int __init pcnet32_init_module(void)
2727{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002728 printk(KERN_INFO "%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002730 pcnet32_debug = netif_msg_init(debug, PCNET32_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002732 if ((tx_start_pt >= 0) && (tx_start_pt <= 3))
2733 tx_start = tx_start_pt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002735 /* find the PCI devices */
2736 if (!pci_module_init(&pcnet32_driver))
2737 pcnet32_have_pci = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002739 /* should we find any remaining VLbus devices ? */
2740 if (pcnet32vlb)
2741 pcnet32_probe_vlbus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002743 if (cards_found && (pcnet32_debug & NETIF_MSG_PROBE))
2744 printk(KERN_INFO PFX "%d cards_found.\n", cards_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002746 return (pcnet32_have_pci + cards_found) ? 0 : -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747}
2748
2749static void __exit pcnet32_cleanup_module(void)
2750{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002751 struct net_device *next_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002753 while (pcnet32_dev) {
2754 struct pcnet32_private *lp = pcnet32_dev->priv;
2755 next_dev = lp->next;
2756 unregister_netdev(pcnet32_dev);
2757 pcnet32_free_ring(pcnet32_dev);
2758 release_region(pcnet32_dev->base_addr, PCNET32_TOTAL_SIZE);
2759 pci_free_consistent(lp->pci_dev, sizeof(*lp), lp, lp->dma_addr);
2760 free_netdev(pcnet32_dev);
2761 pcnet32_dev = next_dev;
2762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002764 if (pcnet32_have_pci)
2765 pci_unregister_driver(&pcnet32_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766}
2767
2768module_init(pcnet32_init_module);
2769module_exit(pcnet32_cleanup_module);
2770
2771/*
2772 * Local variables:
2773 * c-indent-level: 4
2774 * tab-width: 8
2775 * End:
2776 */