blob: bf72aa80ccb6d91e9bf6aef7b13ae251f2c84b4a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* pcnet32.c: An AMD PCnet32 ethernet driver for linux. */
2/*
3 * Copyright 1996-1999 Thomas Bogendoerfer
4 *
5 * Derived from the lance driver written 1993,1994,1995 by Donald Becker.
6 *
7 * Copyright 1993 United States Government as represented by the
8 * Director, National Security Agency.
9 *
10 * This software may be used and distributed according to the terms
11 * of the GNU General Public License, incorporated herein by reference.
12 *
13 * This driver is for PCnet32 and PCnetPCI based ethercards
14 */
15/**************************************************************************
16 * 23 Oct, 2000.
17 * Fixed a few bugs, related to running the controller in 32bit mode.
18 *
19 * Carsten Langgaard, carstenl@mips.com
20 * Copyright (C) 2000 MIPS Technologies, Inc. All rights reserved.
21 *
22 *************************************************************************/
23
24#define DRV_NAME "pcnet32"
Don Fryac62ef02006-03-20 15:26:03 -080025#define DRV_VERSION "1.32"
26#define DRV_RELDATE "18.Mar.2006"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#define PFX DRV_NAME ": "
28
Jeff Garzik4a5e8e22006-03-21 16:15:44 -050029static const char *const version =
30 DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " tsbogend@alpha.franken.de\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <linux/module.h>
33#include <linux/kernel.h>
34#include <linux/string.h>
35#include <linux/errno.h>
36#include <linux/ioport.h>
37#include <linux/slab.h>
38#include <linux/interrupt.h>
39#include <linux/pci.h>
40#include <linux/delay.h>
41#include <linux/init.h>
42#include <linux/ethtool.h>
43#include <linux/mii.h>
44#include <linux/crc32.h>
45#include <linux/netdevice.h>
46#include <linux/etherdevice.h>
47#include <linux/skbuff.h>
48#include <linux/spinlock.h>
49#include <linux/moduleparam.h>
50#include <linux/bitops.h>
51
52#include <asm/dma.h>
53#include <asm/io.h>
54#include <asm/uaccess.h>
55#include <asm/irq.h>
56
57/*
58 * PCI device identifiers for "new style" Linux PCI Device Drivers
59 */
60static struct pci_device_id pcnet32_pci_tbl[] = {
Don Fryf2622a22006-06-29 13:52:58 -070061 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE_HOME), },
62 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE), },
Jeff Garzik4a5e8e22006-03-21 16:15:44 -050063
64 /*
65 * Adapters that were sold with IBM's RS/6000 or pSeries hardware have
66 * the incorrect vendor id.
67 */
Don Fryf2622a22006-06-29 13:52:58 -070068 { PCI_DEVICE(PCI_VENDOR_ID_TRIDENT, PCI_DEVICE_ID_AMD_LANCE),
69 .class = (PCI_CLASS_NETWORK_ETHERNET << 8), .class_mask = 0xffff00, },
Jeff Garzik4a5e8e22006-03-21 16:15:44 -050070
71 { } /* terminate list */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
Jeff Garzik4a5e8e22006-03-21 16:15:44 -050074MODULE_DEVICE_TABLE(pci, pcnet32_pci_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76static int cards_found;
77
78/*
79 * VLB I/O addresses
80 */
81static unsigned int pcnet32_portlist[] __initdata =
Jeff Garzik4a5e8e22006-03-21 16:15:44 -050082 { 0x300, 0x320, 0x340, 0x360, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84static int pcnet32_debug = 0;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -050085static int tx_start = 1; /* Mapping -- 0:20, 1:64, 2:128, 3:~220 (depends on chip vers) */
86static int pcnet32vlb; /* check for VLB cards ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88static struct net_device *pcnet32_dev;
89
90static int max_interrupt_work = 2;
91static int rx_copybreak = 200;
92
93#define PCNET32_PORT_AUI 0x00
94#define PCNET32_PORT_10BT 0x01
95#define PCNET32_PORT_GPSI 0x02
96#define PCNET32_PORT_MII 0x03
97
98#define PCNET32_PORT_PORTSEL 0x03
99#define PCNET32_PORT_ASEL 0x04
100#define PCNET32_PORT_100 0x40
101#define PCNET32_PORT_FD 0x80
102
103#define PCNET32_DMA_MASK 0xffffffff
104
105#define PCNET32_WATCHDOG_TIMEOUT (jiffies + (2 * HZ))
106#define PCNET32_BLINK_TIMEOUT (jiffies + (HZ/4))
107
108/*
109 * table to translate option values from tulip
110 * to internal options
111 */
Arjan van de Venf71e1302006-03-03 21:33:57 -0500112static const unsigned char options_mapping[] = {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500113 PCNET32_PORT_ASEL, /* 0 Auto-select */
114 PCNET32_PORT_AUI, /* 1 BNC/AUI */
115 PCNET32_PORT_AUI, /* 2 AUI/BNC */
116 PCNET32_PORT_ASEL, /* 3 not supported */
117 PCNET32_PORT_10BT | PCNET32_PORT_FD, /* 4 10baseT-FD */
118 PCNET32_PORT_ASEL, /* 5 not supported */
119 PCNET32_PORT_ASEL, /* 6 not supported */
120 PCNET32_PORT_ASEL, /* 7 not supported */
121 PCNET32_PORT_ASEL, /* 8 not supported */
122 PCNET32_PORT_MII, /* 9 MII 10baseT */
123 PCNET32_PORT_MII | PCNET32_PORT_FD, /* 10 MII 10baseT-FD */
124 PCNET32_PORT_MII, /* 11 MII (autosel) */
125 PCNET32_PORT_10BT, /* 12 10BaseT */
126 PCNET32_PORT_MII | PCNET32_PORT_100, /* 13 MII 100BaseTx */
127 /* 14 MII 100BaseTx-FD */
128 PCNET32_PORT_MII | PCNET32_PORT_100 | PCNET32_PORT_FD,
129 PCNET32_PORT_ASEL /* 15 not supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130};
131
132static const char pcnet32_gstrings_test[][ETH_GSTRING_LEN] = {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500133 "Loopback test (offline)"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#define PCNET32_TEST_LEN (sizeof(pcnet32_gstrings_test) / ETH_GSTRING_LEN)
137
Don Fryac62ef02006-03-20 15:26:03 -0800138#define PCNET32_NUM_REGS 136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500140#define MAX_UNITS 8 /* More are supported, limit only on options */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static int options[MAX_UNITS];
142static int full_duplex[MAX_UNITS];
143static int homepna[MAX_UNITS];
144
145/*
146 * Theory of Operation
147 *
148 * This driver uses the same software structure as the normal lance
149 * driver. So look for a verbose description in lance.c. The differences
150 * to the normal lance driver is the use of the 32bit mode of PCnet32
151 * and PCnetPCI chips. Because these chips are 32bit chips, there is no
152 * 16MB limitation and we don't need bounce buffers.
153 */
154
155/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * Set the number of Tx and Rx buffers, using Log_2(# buffers).
157 * Reasonable default values are 4 Tx buffers, and 16 Rx buffers.
158 * That translates to 2 (4 == 2^^2) and 4 (16 == 2^^4).
159 */
160#ifndef PCNET32_LOG_TX_BUFFERS
Hubert WS Lineabf0412005-09-14 11:39:25 -0700161#define PCNET32_LOG_TX_BUFFERS 4
162#define PCNET32_LOG_RX_BUFFERS 5
163#define PCNET32_LOG_MAX_TX_BUFFERS 9 /* 2^9 == 512 */
164#define PCNET32_LOG_MAX_RX_BUFFERS 9
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#endif
166
167#define TX_RING_SIZE (1 << (PCNET32_LOG_TX_BUFFERS))
Hubert WS Lineabf0412005-09-14 11:39:25 -0700168#define TX_MAX_RING_SIZE (1 << (PCNET32_LOG_MAX_TX_BUFFERS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170#define RX_RING_SIZE (1 << (PCNET32_LOG_RX_BUFFERS))
Hubert WS Lineabf0412005-09-14 11:39:25 -0700171#define RX_MAX_RING_SIZE (1 << (PCNET32_LOG_MAX_RX_BUFFERS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173#define PKT_BUF_SZ 1544
174
175/* Offsets from base I/O address. */
176#define PCNET32_WIO_RDP 0x10
177#define PCNET32_WIO_RAP 0x12
178#define PCNET32_WIO_RESET 0x14
179#define PCNET32_WIO_BDP 0x16
180
181#define PCNET32_DWIO_RDP 0x10
182#define PCNET32_DWIO_RAP 0x14
183#define PCNET32_DWIO_RESET 0x18
184#define PCNET32_DWIO_BDP 0x1C
185
186#define PCNET32_TOTAL_SIZE 0x20
187
Don Fry06c87852006-06-29 13:54:38 -0700188#define CSR0 0
189#define CSR0_INIT 0x1
190#define CSR0_START 0x2
191#define CSR0_STOP 0x4
192#define CSR0_TXPOLL 0x8
193#define CSR0_INTEN 0x40
194#define CSR0_IDON 0x0100
195#define CSR0_NORMAL (CSR0_START | CSR0_INTEN)
196#define PCNET32_INIT_LOW 1
197#define PCNET32_INIT_HIGH 2
198#define CSR3 3
199#define CSR4 4
200#define CSR5 5
201#define CSR5_SUSPEND 0x0001
202#define CSR15 15
203#define PCNET32_MC_FILTER 8
204
Don Fry8d916262006-08-22 10:22:37 -0700205#define PCNET32_79C970A 0x2621
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/* The PCNET32 Rx and Tx ring descriptors. */
208struct pcnet32_rx_head {
Jeff Garzik0b5bf222006-03-21 16:22:47 -0500209 u32 base;
Don Fryb368a3f2006-09-13 10:16:07 -0700210 s16 buf_length; /* two`s complement of length */
Jeff Garzik0b5bf222006-03-21 16:22:47 -0500211 s16 status;
212 u32 msg_length;
213 u32 reserved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214};
215
216struct pcnet32_tx_head {
Jeff Garzik0b5bf222006-03-21 16:22:47 -0500217 u32 base;
Don Fryb368a3f2006-09-13 10:16:07 -0700218 s16 length; /* two`s complement of length */
Jeff Garzik0b5bf222006-03-21 16:22:47 -0500219 s16 status;
220 u32 misc;
221 u32 reserved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222};
223
224/* The PCNET32 32-Bit initialization block, described in databook. */
225struct pcnet32_init_block {
Jeff Garzik0b5bf222006-03-21 16:22:47 -0500226 u16 mode;
227 u16 tlen_rlen;
228 u8 phys_addr[6];
229 u16 reserved;
230 u32 filter[2];
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500231 /* Receive and transmit ring base, along with extra bits. */
Jeff Garzik0b5bf222006-03-21 16:22:47 -0500232 u32 rx_ring;
233 u32 tx_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234};
235
236/* PCnet32 access functions */
237struct pcnet32_access {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500238 u16 (*read_csr) (unsigned long, int);
239 void (*write_csr) (unsigned long, int, u16);
240 u16 (*read_bcr) (unsigned long, int);
241 void (*write_bcr) (unsigned long, int, u16);
242 u16 (*read_rap) (unsigned long);
243 void (*write_rap) (unsigned long, u16);
244 void (*reset) (unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245};
246
247/*
Hubert WS Lin76209922005-09-14 11:39:27 -0700248 * The first field of pcnet32_private is read by the ethernet device
249 * so the structure should be allocated using pci_alloc_consistent().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 */
251struct pcnet32_private {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500252 struct pcnet32_init_block init_block;
253 /* The Tx and Rx ring entries must be aligned on 16-byte boundaries in 32bit mode. */
Jeff Garzik0b5bf222006-03-21 16:22:47 -0500254 struct pcnet32_rx_head *rx_ring;
255 struct pcnet32_tx_head *tx_ring;
256 dma_addr_t dma_addr;/* DMA address of beginning of this
257 object, returned by pci_alloc_consistent */
258 struct pci_dev *pci_dev;
259 const char *name;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500260 /* The saved address of a sent-in-place packet/buffer, for skfree(). */
Jeff Garzik0b5bf222006-03-21 16:22:47 -0500261 struct sk_buff **tx_skbuff;
262 struct sk_buff **rx_skbuff;
263 dma_addr_t *tx_dma_addr;
264 dma_addr_t *rx_dma_addr;
265 struct pcnet32_access a;
266 spinlock_t lock; /* Guard lock */
267 unsigned int cur_rx, cur_tx; /* The next free ring entry */
268 unsigned int rx_ring_size; /* current rx ring size */
269 unsigned int tx_ring_size; /* current tx ring size */
270 unsigned int rx_mod_mask; /* rx ring modular mask */
271 unsigned int tx_mod_mask; /* tx ring modular mask */
272 unsigned short rx_len_bits;
273 unsigned short tx_len_bits;
274 dma_addr_t rx_ring_dma_addr;
275 dma_addr_t tx_ring_dma_addr;
276 unsigned int dirty_rx, /* ring entries to be freed. */
277 dirty_tx;
278
279 struct net_device_stats stats;
280 char tx_full;
281 char phycount; /* number of phys found */
282 int options;
283 unsigned int shared_irq:1, /* shared irq possible */
284 dxsuflo:1, /* disable transmit stop on uflo */
285 mii:1; /* mii port available */
286 struct net_device *next;
287 struct mii_if_info mii_if;
288 struct timer_list watchdog_timer;
289 struct timer_list blink_timer;
290 u32 msg_enable; /* debug message level */
Don Fryac62ef02006-03-20 15:26:03 -0800291
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500292 /* each bit indicates an available PHY */
Jeff Garzik0b5bf222006-03-21 16:22:47 -0500293 u32 phymask;
Don Fry8d916262006-08-22 10:22:37 -0700294 unsigned short chip_version; /* which variant this is */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295};
296
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500297static int pcnet32_probe_pci(struct pci_dev *, const struct pci_device_id *);
298static int pcnet32_probe1(unsigned long, int, struct pci_dev *);
299static int pcnet32_open(struct net_device *);
300static int pcnet32_init_ring(struct net_device *);
301static int pcnet32_start_xmit(struct sk_buff *, struct net_device *);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500302static void pcnet32_tx_timeout(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303static irqreturn_t pcnet32_interrupt(int, void *, struct pt_regs *);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500304static int pcnet32_close(struct net_device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305static struct net_device_stats *pcnet32_get_stats(struct net_device *);
306static void pcnet32_load_multicast(struct net_device *dev);
307static void pcnet32_set_multicast_list(struct net_device *);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500308static int pcnet32_ioctl(struct net_device *, struct ifreq *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309static void pcnet32_watchdog(struct net_device *);
310static int mdio_read(struct net_device *dev, int phy_id, int reg_num);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500311static void mdio_write(struct net_device *dev, int phy_id, int reg_num,
312 int val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313static void pcnet32_restart(struct net_device *dev, unsigned int csr0_bits);
314static void pcnet32_ethtool_test(struct net_device *dev,
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500315 struct ethtool_test *eth_test, u64 * data);
316static int pcnet32_loopback_test(struct net_device *dev, uint64_t * data1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317static int pcnet32_phys_id(struct net_device *dev, u32 data);
318static void pcnet32_led_blink_callback(struct net_device *dev);
319static int pcnet32_get_regs_len(struct net_device *dev);
320static void pcnet32_get_regs(struct net_device *dev, struct ethtool_regs *regs,
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500321 void *ptr);
Don Fry1bcd3152005-04-29 14:51:17 -0700322static void pcnet32_purge_tx_ring(struct net_device *dev);
Don Frya88c8442005-11-01 12:04:33 -0800323static int pcnet32_alloc_ring(struct net_device *dev, char *name);
Hubert WS Lineabf0412005-09-14 11:39:25 -0700324static void pcnet32_free_ring(struct net_device *dev);
Don Fryac62ef02006-03-20 15:26:03 -0800325static void pcnet32_check_media(struct net_device *dev, int verbose);
Hubert WS Lineabf0412005-09-14 11:39:25 -0700326
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500327static u16 pcnet32_wio_read_csr(unsigned long addr, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500329 outw(index, addr + PCNET32_WIO_RAP);
330 return inw(addr + PCNET32_WIO_RDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500333static void pcnet32_wio_write_csr(unsigned long addr, int index, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500335 outw(index, addr + PCNET32_WIO_RAP);
336 outw(val, addr + PCNET32_WIO_RDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500339static u16 pcnet32_wio_read_bcr(unsigned long addr, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500341 outw(index, addr + PCNET32_WIO_RAP);
342 return inw(addr + PCNET32_WIO_BDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500345static void pcnet32_wio_write_bcr(unsigned long addr, int index, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500347 outw(index, addr + PCNET32_WIO_RAP);
348 outw(val, addr + PCNET32_WIO_BDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500351static u16 pcnet32_wio_read_rap(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500353 return inw(addr + PCNET32_WIO_RAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500356static void pcnet32_wio_write_rap(unsigned long addr, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500358 outw(val, addr + PCNET32_WIO_RAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500361static void pcnet32_wio_reset(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500363 inw(addr + PCNET32_WIO_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500366static int pcnet32_wio_check(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500368 outw(88, addr + PCNET32_WIO_RAP);
369 return (inw(addr + PCNET32_WIO_RAP) == 88);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372static struct pcnet32_access pcnet32_wio = {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500373 .read_csr = pcnet32_wio_read_csr,
374 .write_csr = pcnet32_wio_write_csr,
375 .read_bcr = pcnet32_wio_read_bcr,
376 .write_bcr = pcnet32_wio_write_bcr,
377 .read_rap = pcnet32_wio_read_rap,
378 .write_rap = pcnet32_wio_write_rap,
379 .reset = pcnet32_wio_reset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380};
381
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500382static u16 pcnet32_dwio_read_csr(unsigned long addr, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500384 outl(index, addr + PCNET32_DWIO_RAP);
385 return (inl(addr + PCNET32_DWIO_RDP) & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500388static void pcnet32_dwio_write_csr(unsigned long addr, int index, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500390 outl(index, addr + PCNET32_DWIO_RAP);
391 outl(val, addr + PCNET32_DWIO_RDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500394static u16 pcnet32_dwio_read_bcr(unsigned long addr, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500396 outl(index, addr + PCNET32_DWIO_RAP);
397 return (inl(addr + PCNET32_DWIO_BDP) & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500400static void pcnet32_dwio_write_bcr(unsigned long addr, int index, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500402 outl(index, addr + PCNET32_DWIO_RAP);
403 outl(val, addr + PCNET32_DWIO_BDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500406static u16 pcnet32_dwio_read_rap(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500408 return (inl(addr + PCNET32_DWIO_RAP) & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500411static void pcnet32_dwio_write_rap(unsigned long addr, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500413 outl(val, addr + PCNET32_DWIO_RAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500416static void pcnet32_dwio_reset(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500418 inl(addr + PCNET32_DWIO_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500421static int pcnet32_dwio_check(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500423 outl(88, addr + PCNET32_DWIO_RAP);
424 return ((inl(addr + PCNET32_DWIO_RAP) & 0xffff) == 88);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
427static struct pcnet32_access pcnet32_dwio = {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500428 .read_csr = pcnet32_dwio_read_csr,
429 .write_csr = pcnet32_dwio_write_csr,
430 .read_bcr = pcnet32_dwio_read_bcr,
431 .write_bcr = pcnet32_dwio_write_bcr,
432 .read_rap = pcnet32_dwio_read_rap,
433 .write_rap = pcnet32_dwio_write_rap,
434 .reset = pcnet32_dwio_reset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435};
436
Don Fry06c87852006-06-29 13:54:38 -0700437static void pcnet32_netif_stop(struct net_device *dev)
438{
439 dev->trans_start = jiffies;
440 netif_poll_disable(dev);
441 netif_tx_disable(dev);
442}
443
444static void pcnet32_netif_start(struct net_device *dev)
445{
446 netif_wake_queue(dev);
447 netif_poll_enable(dev);
448}
449
450/*
451 * Allocate space for the new sized tx ring.
452 * Free old resources
453 * Save new resources.
454 * Any failure keeps old resources.
455 * Must be called with lp->lock held.
456 */
457static void pcnet32_realloc_tx_ring(struct net_device *dev,
458 struct pcnet32_private *lp,
459 unsigned int size)
460{
461 dma_addr_t new_ring_dma_addr;
462 dma_addr_t *new_dma_addr_list;
463 struct pcnet32_tx_head *new_tx_ring;
464 struct sk_buff **new_skb_list;
465
466 pcnet32_purge_tx_ring(dev);
467
468 new_tx_ring = pci_alloc_consistent(lp->pci_dev,
469 sizeof(struct pcnet32_tx_head) *
470 (1 << size),
471 &new_ring_dma_addr);
472 if (new_tx_ring == NULL) {
473 if (netif_msg_drv(lp))
474 printk("\n" KERN_ERR
475 "%s: Consistent memory allocation failed.\n",
476 dev->name);
477 return;
478 }
479 memset(new_tx_ring, 0, sizeof(struct pcnet32_tx_head) * (1 << size));
480
481 new_dma_addr_list = kcalloc((1 << size), sizeof(dma_addr_t),
482 GFP_ATOMIC);
483 if (!new_dma_addr_list) {
484 if (netif_msg_drv(lp))
485 printk("\n" KERN_ERR
486 "%s: Memory allocation failed.\n", dev->name);
487 goto free_new_tx_ring;
488 }
489
490 new_skb_list = kcalloc((1 << size), sizeof(struct sk_buff *),
491 GFP_ATOMIC);
492 if (!new_skb_list) {
493 if (netif_msg_drv(lp))
494 printk("\n" KERN_ERR
495 "%s: Memory allocation failed.\n", dev->name);
496 goto free_new_lists;
497 }
498
499 kfree(lp->tx_skbuff);
500 kfree(lp->tx_dma_addr);
501 pci_free_consistent(lp->pci_dev,
502 sizeof(struct pcnet32_tx_head) *
503 lp->tx_ring_size, lp->tx_ring,
504 lp->tx_ring_dma_addr);
505
506 lp->tx_ring_size = (1 << size);
507 lp->tx_mod_mask = lp->tx_ring_size - 1;
508 lp->tx_len_bits = (size << 12);
509 lp->tx_ring = new_tx_ring;
510 lp->tx_ring_dma_addr = new_ring_dma_addr;
511 lp->tx_dma_addr = new_dma_addr_list;
512 lp->tx_skbuff = new_skb_list;
513 return;
514
515 free_new_lists:
516 kfree(new_dma_addr_list);
517 free_new_tx_ring:
518 pci_free_consistent(lp->pci_dev,
519 sizeof(struct pcnet32_tx_head) *
520 (1 << size),
521 new_tx_ring,
522 new_ring_dma_addr);
523 return;
524}
525
526/*
527 * Allocate space for the new sized rx ring.
528 * Re-use old receive buffers.
529 * alloc extra buffers
530 * free unneeded buffers
531 * free unneeded buffers
532 * Save new resources.
533 * Any failure keeps old resources.
534 * Must be called with lp->lock held.
535 */
536static void pcnet32_realloc_rx_ring(struct net_device *dev,
537 struct pcnet32_private *lp,
538 unsigned int size)
539{
540 dma_addr_t new_ring_dma_addr;
541 dma_addr_t *new_dma_addr_list;
542 struct pcnet32_rx_head *new_rx_ring;
543 struct sk_buff **new_skb_list;
544 int new, overlap;
545
546 new_rx_ring = pci_alloc_consistent(lp->pci_dev,
547 sizeof(struct pcnet32_rx_head) *
548 (1 << size),
549 &new_ring_dma_addr);
550 if (new_rx_ring == NULL) {
551 if (netif_msg_drv(lp))
552 printk("\n" KERN_ERR
553 "%s: Consistent memory allocation failed.\n",
554 dev->name);
555 return;
556 }
557 memset(new_rx_ring, 0, sizeof(struct pcnet32_rx_head) * (1 << size));
558
559 new_dma_addr_list = kcalloc((1 << size), sizeof(dma_addr_t),
560 GFP_ATOMIC);
561 if (!new_dma_addr_list) {
562 if (netif_msg_drv(lp))
563 printk("\n" KERN_ERR
564 "%s: Memory allocation failed.\n", dev->name);
565 goto free_new_rx_ring;
566 }
567
568 new_skb_list = kcalloc((1 << size), sizeof(struct sk_buff *),
569 GFP_ATOMIC);
570 if (!new_skb_list) {
571 if (netif_msg_drv(lp))
572 printk("\n" KERN_ERR
573 "%s: Memory allocation failed.\n", dev->name);
574 goto free_new_lists;
575 }
576
577 /* first copy the current receive buffers */
578 overlap = min(size, lp->rx_ring_size);
579 for (new = 0; new < overlap; new++) {
580 new_rx_ring[new] = lp->rx_ring[new];
581 new_dma_addr_list[new] = lp->rx_dma_addr[new];
582 new_skb_list[new] = lp->rx_skbuff[new];
583 }
584 /* now allocate any new buffers needed */
585 for (; new < size; new++ ) {
586 struct sk_buff *rx_skbuff;
587 new_skb_list[new] = dev_alloc_skb(PKT_BUF_SZ);
588 if (!(rx_skbuff = new_skb_list[new])) {
589 /* keep the original lists and buffers */
590 if (netif_msg_drv(lp))
591 printk(KERN_ERR
592 "%s: pcnet32_realloc_rx_ring dev_alloc_skb failed.\n",
593 dev->name);
594 goto free_all_new;
595 }
596 skb_reserve(rx_skbuff, 2);
597
598 new_dma_addr_list[new] =
599 pci_map_single(lp->pci_dev, rx_skbuff->data,
600 PKT_BUF_SZ - 2, PCI_DMA_FROMDEVICE);
601 new_rx_ring[new].base = (u32) le32_to_cpu(new_dma_addr_list[new]);
602 new_rx_ring[new].buf_length = le16_to_cpu(2 - PKT_BUF_SZ);
603 new_rx_ring[new].status = le16_to_cpu(0x8000);
604 }
605 /* and free any unneeded buffers */
606 for (; new < lp->rx_ring_size; new++) {
607 if (lp->rx_skbuff[new]) {
608 pci_unmap_single(lp->pci_dev, lp->rx_dma_addr[new],
609 PKT_BUF_SZ - 2, PCI_DMA_FROMDEVICE);
610 dev_kfree_skb(lp->rx_skbuff[new]);
611 }
612 }
613
614 kfree(lp->rx_skbuff);
615 kfree(lp->rx_dma_addr);
616 pci_free_consistent(lp->pci_dev,
617 sizeof(struct pcnet32_rx_head) *
618 lp->rx_ring_size, lp->rx_ring,
619 lp->rx_ring_dma_addr);
620
621 lp->rx_ring_size = (1 << size);
622 lp->rx_mod_mask = lp->rx_ring_size - 1;
623 lp->rx_len_bits = (size << 4);
624 lp->rx_ring = new_rx_ring;
625 lp->rx_ring_dma_addr = new_ring_dma_addr;
626 lp->rx_dma_addr = new_dma_addr_list;
627 lp->rx_skbuff = new_skb_list;
628 return;
629
630 free_all_new:
631 for (; --new >= lp->rx_ring_size; ) {
632 if (new_skb_list[new]) {
633 pci_unmap_single(lp->pci_dev, new_dma_addr_list[new],
634 PKT_BUF_SZ - 2, PCI_DMA_FROMDEVICE);
635 dev_kfree_skb(new_skb_list[new]);
636 }
637 }
638 kfree(new_skb_list);
639 free_new_lists:
640 kfree(new_dma_addr_list);
641 free_new_rx_ring:
642 pci_free_consistent(lp->pci_dev,
643 sizeof(struct pcnet32_rx_head) *
644 (1 << size),
645 new_rx_ring,
646 new_ring_dma_addr);
647 return;
648}
649
Don Fryac5bfe42006-06-29 13:55:27 -0700650static void pcnet32_purge_rx_ring(struct net_device *dev)
651{
652 struct pcnet32_private *lp = dev->priv;
653 int i;
654
655 /* free all allocated skbuffs */
656 for (i = 0; i < lp->rx_ring_size; i++) {
657 lp->rx_ring[i].status = 0; /* CPU owns buffer */
658 wmb(); /* Make sure adapter sees owner change */
659 if (lp->rx_skbuff[i]) {
660 pci_unmap_single(lp->pci_dev, lp->rx_dma_addr[i],
661 PKT_BUF_SZ - 2, PCI_DMA_FROMDEVICE);
662 dev_kfree_skb_any(lp->rx_skbuff[i]);
663 }
664 lp->rx_skbuff[i] = NULL;
665 lp->rx_dma_addr[i] = 0;
666 }
667}
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669#ifdef CONFIG_NET_POLL_CONTROLLER
670static void pcnet32_poll_controller(struct net_device *dev)
671{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500672 disable_irq(dev->irq);
673 pcnet32_interrupt(0, dev, NULL);
674 enable_irq(dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676#endif
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678static int pcnet32_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
679{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500680 struct pcnet32_private *lp = dev->priv;
681 unsigned long flags;
682 int r = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500684 if (lp->mii) {
685 spin_lock_irqsave(&lp->lock, flags);
686 mii_ethtool_gset(&lp->mii_if, cmd);
687 spin_unlock_irqrestore(&lp->lock, flags);
688 r = 0;
689 }
690 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
693static int pcnet32_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
694{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500695 struct pcnet32_private *lp = dev->priv;
696 unsigned long flags;
697 int r = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500699 if (lp->mii) {
700 spin_lock_irqsave(&lp->lock, flags);
701 r = mii_ethtool_sset(&lp->mii_if, cmd);
702 spin_unlock_irqrestore(&lp->lock, flags);
703 }
704 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500707static void pcnet32_get_drvinfo(struct net_device *dev,
708 struct ethtool_drvinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500710 struct pcnet32_private *lp = dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500712 strcpy(info->driver, DRV_NAME);
713 strcpy(info->version, DRV_VERSION);
714 if (lp->pci_dev)
715 strcpy(info->bus_info, pci_name(lp->pci_dev));
716 else
717 sprintf(info->bus_info, "VLB 0x%lx", dev->base_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
720static u32 pcnet32_get_link(struct net_device *dev)
721{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500722 struct pcnet32_private *lp = dev->priv;
723 unsigned long flags;
724 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500726 spin_lock_irqsave(&lp->lock, flags);
727 if (lp->mii) {
728 r = mii_link_ok(&lp->mii_if);
Don Fry8d916262006-08-22 10:22:37 -0700729 } else if (lp->chip_version >= PCNET32_79C970A) {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500730 ulong ioaddr = dev->base_addr; /* card base I/O address */
731 r = (lp->a.read_bcr(ioaddr, 4) != 0xc0);
Don Fry8d916262006-08-22 10:22:37 -0700732 } else { /* can not detect link on really old chips */
733 r = 1;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500734 }
735 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500737 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
740static u32 pcnet32_get_msglevel(struct net_device *dev)
741{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500742 struct pcnet32_private *lp = dev->priv;
743 return lp->msg_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
746static void pcnet32_set_msglevel(struct net_device *dev, u32 value)
747{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500748 struct pcnet32_private *lp = dev->priv;
749 lp->msg_enable = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
752static int pcnet32_nway_reset(struct net_device *dev)
753{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500754 struct pcnet32_private *lp = dev->priv;
755 unsigned long flags;
756 int r = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500758 if (lp->mii) {
759 spin_lock_irqsave(&lp->lock, flags);
760 r = mii_nway_restart(&lp->mii_if);
761 spin_unlock_irqrestore(&lp->lock, flags);
762 }
763 return r;
764}
765
766static void pcnet32_get_ringparam(struct net_device *dev,
767 struct ethtool_ringparam *ering)
768{
769 struct pcnet32_private *lp = dev->priv;
770
Don Fry6dcd60c2006-06-29 13:53:23 -0700771 ering->tx_max_pending = TX_MAX_RING_SIZE;
772 ering->tx_pending = lp->tx_ring_size;
773 ering->rx_max_pending = RX_MAX_RING_SIZE;
774 ering->rx_pending = lp->rx_ring_size;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500775}
776
777static int pcnet32_set_ringparam(struct net_device *dev,
778 struct ethtool_ringparam *ering)
779{
780 struct pcnet32_private *lp = dev->priv;
781 unsigned long flags;
Don Fry06c87852006-06-29 13:54:38 -0700782 unsigned int size;
783 ulong ioaddr = dev->base_addr;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500784 int i;
785
786 if (ering->rx_mini_pending || ering->rx_jumbo_pending)
787 return -EINVAL;
788
789 if (netif_running(dev))
Don Fry06c87852006-06-29 13:54:38 -0700790 pcnet32_netif_stop(dev);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 spin_lock_irqsave(&lp->lock, flags);
Don Fry06c87852006-06-29 13:54:38 -0700793 lp->a.write_csr(ioaddr, CSR0, CSR0_STOP); /* stop the chip */
794
795 size = min(ering->tx_pending, (unsigned int)TX_MAX_RING_SIZE);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500796
797 /* set the minimum ring size to 4, to allow the loopback test to work
798 * unchanged.
799 */
800 for (i = 2; i <= PCNET32_LOG_MAX_TX_BUFFERS; i++) {
Don Fry06c87852006-06-29 13:54:38 -0700801 if (size <= (1 << i))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500802 break;
803 }
Don Fry06c87852006-06-29 13:54:38 -0700804 if ((1 << i) != lp->tx_ring_size)
805 pcnet32_realloc_tx_ring(dev, lp, i);
Don Fryb368a3f2006-09-13 10:16:07 -0700806
Don Fry06c87852006-06-29 13:54:38 -0700807 size = min(ering->rx_pending, (unsigned int)RX_MAX_RING_SIZE);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500808 for (i = 2; i <= PCNET32_LOG_MAX_RX_BUFFERS; i++) {
Don Fry06c87852006-06-29 13:54:38 -0700809 if (size <= (1 << i))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500810 break;
811 }
Don Fry06c87852006-06-29 13:54:38 -0700812 if ((1 << i) != lp->rx_ring_size)
813 pcnet32_realloc_rx_ring(dev, lp, i);
Don Fryb368a3f2006-09-13 10:16:07 -0700814
Don Fry06c87852006-06-29 13:54:38 -0700815 dev->weight = lp->rx_ring_size / 2;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500816
Don Fry06c87852006-06-29 13:54:38 -0700817 if (netif_running(dev)) {
818 pcnet32_netif_start(dev);
819 pcnet32_restart(dev, CSR0_NORMAL);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500820 }
821
Don Frya88c8442005-11-01 12:04:33 -0800822 spin_unlock_irqrestore(&lp->lock, flags);
Hubert WS Lineabf0412005-09-14 11:39:25 -0700823
Don Fry06c87852006-06-29 13:54:38 -0700824 if (netif_msg_drv(lp))
825 printk(KERN_INFO
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500826 "%s: Ring Param Settings: RX: %d, TX: %d\n", dev->name,
827 lp->rx_ring_size, lp->tx_ring_size);
Hubert WS Lineabf0412005-09-14 11:39:25 -0700828
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500829 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500832static void pcnet32_get_strings(struct net_device *dev, u32 stringset,
833 u8 * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500835 memcpy(data, pcnet32_gstrings_test, sizeof(pcnet32_gstrings_test));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
838static int pcnet32_self_test_count(struct net_device *dev)
839{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500840 return PCNET32_TEST_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
843static void pcnet32_ethtool_test(struct net_device *dev,
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500844 struct ethtool_test *test, u64 * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500846 struct pcnet32_private *lp = dev->priv;
847 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500849 if (test->flags == ETH_TEST_FL_OFFLINE) {
850 rc = pcnet32_loopback_test(dev, data);
851 if (rc) {
852 if (netif_msg_hw(lp))
853 printk(KERN_DEBUG "%s: Loopback test failed.\n",
854 dev->name);
855 test->flags |= ETH_TEST_FL_FAILED;
856 } else if (netif_msg_hw(lp))
857 printk(KERN_DEBUG "%s: Loopback test passed.\n",
858 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 } else if (netif_msg_hw(lp))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500860 printk(KERN_DEBUG
861 "%s: No tests to run (specify 'Offline' on ethtool).",
862 dev->name);
863} /* end pcnet32_ethtool_test */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500865static int pcnet32_loopback_test(struct net_device *dev, uint64_t * data1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500867 struct pcnet32_private *lp = dev->priv;
868 struct pcnet32_access *a = &lp->a; /* access to registers */
869 ulong ioaddr = dev->base_addr; /* card base I/O address */
870 struct sk_buff *skb; /* sk buff */
871 int x, i; /* counters */
872 int numbuffs = 4; /* number of TX/RX buffers and descs */
873 u16 status = 0x8300; /* TX ring status */
874 u16 teststatus; /* test of ring status */
875 int rc; /* return code */
876 int size; /* size of packets */
877 unsigned char *packet; /* source packet data */
878 static const int data_len = 60; /* length of source packets */
879 unsigned long flags;
880 unsigned long ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500882 rc = 1; /* default to fail */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500884 if (netif_running(dev))
885 pcnet32_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500887 spin_lock_irqsave(&lp->lock, flags);
Don Fryac5bfe42006-06-29 13:55:27 -0700888 lp->a.write_csr(ioaddr, CSR0, CSR0_STOP); /* stop the chip */
889
890 numbuffs = min(numbuffs, (int)min(lp->rx_ring_size, lp->tx_ring_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500892 /* Reset the PCNET32 */
893 lp->a.reset(ioaddr);
Don Fryb368a3f2006-09-13 10:16:07 -0700894 lp->a.write_csr(ioaddr, CSR4, 0x0915); /* auto tx pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500896 /* switch pcnet32 to 32bit mode */
897 lp->a.write_bcr(ioaddr, 20, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500899 /* purge & init rings but don't actually restart */
900 pcnet32_restart(dev, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Don Fryac5bfe42006-06-29 13:55:27 -0700902 lp->a.write_csr(ioaddr, CSR0, CSR0_STOP); /* Set STOP bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500904 /* Initialize Transmit buffers. */
905 size = data_len + 15;
906 for (x = 0; x < numbuffs; x++) {
907 if (!(skb = dev_alloc_skb(size))) {
908 if (netif_msg_hw(lp))
909 printk(KERN_DEBUG
910 "%s: Cannot allocate skb at line: %d!\n",
911 dev->name, __LINE__);
912 goto clean_up;
913 } else {
914 packet = skb->data;
915 skb_put(skb, size); /* create space for data */
916 lp->tx_skbuff[x] = skb;
917 lp->tx_ring[x].length = le16_to_cpu(-skb->len);
918 lp->tx_ring[x].misc = 0;
919
920 /* put DA and SA into the skb */
921 for (i = 0; i < 6; i++)
922 *packet++ = dev->dev_addr[i];
923 for (i = 0; i < 6; i++)
924 *packet++ = dev->dev_addr[i];
925 /* type */
926 *packet++ = 0x08;
927 *packet++ = 0x06;
928 /* packet number */
929 *packet++ = x;
930 /* fill packet with data */
931 for (i = 0; i < data_len; i++)
932 *packet++ = i;
933
934 lp->tx_dma_addr[x] =
935 pci_map_single(lp->pci_dev, skb->data, skb->len,
936 PCI_DMA_TODEVICE);
937 lp->tx_ring[x].base =
938 (u32) le32_to_cpu(lp->tx_dma_addr[x]);
939 wmb(); /* Make sure owner changes after all others are visible */
940 lp->tx_ring[x].status = le16_to_cpu(status);
941 }
942 }
943
Don Fryac5bfe42006-06-29 13:55:27 -0700944 x = a->read_bcr(ioaddr, 32); /* set internal loopback in BCR32 */
945 a->write_bcr(ioaddr, 32, x | 0x0002);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500946
Don Fryac5bfe42006-06-29 13:55:27 -0700947 /* set int loopback in CSR15 */
948 x = a->read_csr(ioaddr, CSR15) & 0xfffc;
949 lp->a.write_csr(ioaddr, CSR15, x | 0x0044);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500950
951 teststatus = le16_to_cpu(0x8000);
Don Fryac5bfe42006-06-29 13:55:27 -0700952 lp->a.write_csr(ioaddr, CSR0, CSR0_START); /* Set STRT bit */
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500953
954 /* Check status of descriptors */
955 for (x = 0; x < numbuffs; x++) {
956 ticks = 0;
957 rmb();
958 while ((lp->rx_ring[x].status & teststatus) && (ticks < 200)) {
959 spin_unlock_irqrestore(&lp->lock, flags);
Don Fryac5bfe42006-06-29 13:55:27 -0700960 msleep(1);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500961 spin_lock_irqsave(&lp->lock, flags);
962 rmb();
963 ticks++;
964 }
965 if (ticks == 200) {
966 if (netif_msg_hw(lp))
967 printk("%s: Desc %d failed to reset!\n",
968 dev->name, x);
969 break;
970 }
971 }
972
Don Fryac5bfe42006-06-29 13:55:27 -0700973 lp->a.write_csr(ioaddr, CSR0, CSR0_STOP); /* Set STOP bit */
Jeff Garzik4a5e8e22006-03-21 16:15:44 -0500974 wmb();
975 if (netif_msg_hw(lp) && netif_msg_pktdata(lp)) {
976 printk(KERN_DEBUG "%s: RX loopback packets:\n", dev->name);
977
978 for (x = 0; x < numbuffs; x++) {
979 printk(KERN_DEBUG "%s: Packet %d:\n", dev->name, x);
980 skb = lp->rx_skbuff[x];
981 for (i = 0; i < size; i++) {
982 printk("%02x ", *(skb->data + i));
983 }
984 printk("\n");
985 }
986 }
987
988 x = 0;
989 rc = 0;
990 while (x < numbuffs && !rc) {
991 skb = lp->rx_skbuff[x];
992 packet = lp->tx_skbuff[x]->data;
993 for (i = 0; i < size; i++) {
994 if (*(skb->data + i) != packet[i]) {
995 if (netif_msg_hw(lp))
996 printk(KERN_DEBUG
997 "%s: Error in compare! %2x - %02x %02x\n",
998 dev->name, i, *(skb->data + i),
999 packet[i]);
1000 rc = 1;
1001 break;
1002 }
1003 }
1004 x++;
1005 }
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001006
1007 clean_up:
Don Fryac5bfe42006-06-29 13:55:27 -07001008 *data1 = rc;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001009 pcnet32_purge_tx_ring(dev);
Don Fryac5bfe42006-06-29 13:55:27 -07001010
1011 x = a->read_csr(ioaddr, CSR15);
1012 a->write_csr(ioaddr, CSR15, (x & ~0x0044)); /* reset bits 6 and 2 */
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001013
1014 x = a->read_bcr(ioaddr, 32); /* reset internal loopback */
Don Fryac5bfe42006-06-29 13:55:27 -07001015 a->write_bcr(ioaddr, 32, (x & ~0x0002));
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001016
1017 if (netif_running(dev)) {
Don Fryac5bfe42006-06-29 13:55:27 -07001018 spin_unlock_irqrestore(&lp->lock, flags);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001019 pcnet32_open(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 } else {
Don Fryac5bfe42006-06-29 13:55:27 -07001021 pcnet32_purge_rx_ring(dev);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001022 lp->a.write_bcr(ioaddr, 20, 4); /* return to 16bit mode */
Don Fryac5bfe42006-06-29 13:55:27 -07001023 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001026 return (rc);
1027} /* end pcnet32_loopback_test */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029static void pcnet32_led_blink_callback(struct net_device *dev)
1030{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001031 struct pcnet32_private *lp = dev->priv;
1032 struct pcnet32_access *a = &lp->a;
1033 ulong ioaddr = dev->base_addr;
1034 unsigned long flags;
1035 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001037 spin_lock_irqsave(&lp->lock, flags);
1038 for (i = 4; i < 8; i++) {
1039 a->write_bcr(ioaddr, i, a->read_bcr(ioaddr, i) ^ 0x4000);
1040 }
1041 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001043 mod_timer(&lp->blink_timer, PCNET32_BLINK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
1046static int pcnet32_phys_id(struct net_device *dev, u32 data)
1047{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001048 struct pcnet32_private *lp = dev->priv;
1049 struct pcnet32_access *a = &lp->a;
1050 ulong ioaddr = dev->base_addr;
1051 unsigned long flags;
1052 int i, regs[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001054 if (!lp->blink_timer.function) {
1055 init_timer(&lp->blink_timer);
1056 lp->blink_timer.function = (void *)pcnet32_led_blink_callback;
1057 lp->blink_timer.data = (unsigned long)dev;
1058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001060 /* Save the current value of the bcrs */
1061 spin_lock_irqsave(&lp->lock, flags);
1062 for (i = 4; i < 8; i++) {
1063 regs[i - 4] = a->read_bcr(ioaddr, i);
1064 }
1065 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001067 mod_timer(&lp->blink_timer, jiffies);
1068 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001070 if ((!data) || (data > (u32) (MAX_SCHEDULE_TIMEOUT / HZ)))
1071 data = (u32) (MAX_SCHEDULE_TIMEOUT / HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001073 msleep_interruptible(data * 1000);
1074 del_timer_sync(&lp->blink_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001076 /* Restore the original value of the bcrs */
1077 spin_lock_irqsave(&lp->lock, flags);
1078 for (i = 4; i < 8; i++) {
1079 a->write_bcr(ioaddr, i, regs[i - 4]);
1080 }
1081 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
1085
Don Frydf27f4a2006-06-29 13:55:02 -07001086/*
1087 * lp->lock must be held.
1088 */
1089static int pcnet32_suspend(struct net_device *dev, unsigned long *flags,
1090 int can_sleep)
1091{
1092 int csr5;
1093 struct pcnet32_private *lp = dev->priv;
1094 struct pcnet32_access *a = &lp->a;
1095 ulong ioaddr = dev->base_addr;
1096 int ticks;
1097
Don Fry8d916262006-08-22 10:22:37 -07001098 /* really old chips have to be stopped. */
1099 if (lp->chip_version < PCNET32_79C970A)
1100 return 0;
1101
Don Frydf27f4a2006-06-29 13:55:02 -07001102 /* set SUSPEND (SPND) - CSR5 bit 0 */
1103 csr5 = a->read_csr(ioaddr, CSR5);
1104 a->write_csr(ioaddr, CSR5, csr5 | CSR5_SUSPEND);
1105
1106 /* poll waiting for bit to be set */
1107 ticks = 0;
1108 while (!(a->read_csr(ioaddr, CSR5) & CSR5_SUSPEND)) {
1109 spin_unlock_irqrestore(&lp->lock, *flags);
1110 if (can_sleep)
1111 msleep(1);
1112 else
1113 mdelay(1);
1114 spin_lock_irqsave(&lp->lock, *flags);
1115 ticks++;
1116 if (ticks > 200) {
1117 if (netif_msg_hw(lp))
1118 printk(KERN_DEBUG
1119 "%s: Error getting into suspend!\n",
1120 dev->name);
1121 return 0;
1122 }
1123 }
1124 return 1;
1125}
1126
Don Fry3904c322006-09-13 10:16:38 -07001127/*
1128 * process one receive descriptor entry
1129 */
Don Fry9691edd2006-09-13 10:16:21 -07001130
Don Fry3904c322006-09-13 10:16:38 -07001131static void pcnet32_rx_entry(struct net_device *dev,
1132 struct pcnet32_private *lp,
1133 struct pcnet32_rx_head *rxp,
1134 int entry)
1135{
1136 int status = (short)le16_to_cpu(rxp->status) >> 8;
1137 int rx_in_place = 0;
1138 struct sk_buff *skb;
1139 short pkt_len;
1140
1141 if (status != 0x03) { /* There was an error. */
1142 /*
1143 * There is a tricky error noted by John Murphy,
1144 * <murf@perftech.com> to Russ Nelson: Even with full-sized
1145 * buffers it's possible for a jabber packet to use two
1146 * buffers, with only the last correctly noting the error.
1147 */
1148 if (status & 0x01) /* Only count a general error at the */
1149 lp->stats.rx_errors++; /* end of a packet. */
1150 if (status & 0x20)
1151 lp->stats.rx_frame_errors++;
1152 if (status & 0x10)
1153 lp->stats.rx_over_errors++;
1154 if (status & 0x08)
1155 lp->stats.rx_crc_errors++;
1156 if (status & 0x04)
1157 lp->stats.rx_fifo_errors++;
1158 return;
1159 }
1160
1161 pkt_len = (le32_to_cpu(rxp->msg_length) & 0xfff) - 4;
1162
1163 /* Discard oversize frames. */
1164 if (unlikely(pkt_len > PKT_BUF_SZ - 2)) {
1165 if (netif_msg_drv(lp))
1166 printk(KERN_ERR "%s: Impossible packet size %d!\n",
1167 dev->name, pkt_len);
1168 lp->stats.rx_errors++;
1169 return;
1170 }
1171 if (pkt_len < 60) {
1172 if (netif_msg_rx_err(lp))
1173 printk(KERN_ERR "%s: Runt packet!\n", dev->name);
1174 lp->stats.rx_errors++;
1175 return;
1176 }
1177
1178 if (pkt_len > rx_copybreak) {
1179 struct sk_buff *newskb;
1180
1181 if ((newskb = dev_alloc_skb(PKT_BUF_SZ))) {
1182 skb_reserve(newskb, 2);
1183 skb = lp->rx_skbuff[entry];
1184 pci_unmap_single(lp->pci_dev,
1185 lp->rx_dma_addr[entry],
1186 PKT_BUF_SZ - 2,
1187 PCI_DMA_FROMDEVICE);
1188 skb_put(skb, pkt_len);
1189 lp->rx_skbuff[entry] = newskb;
1190 newskb->dev = dev;
1191 lp->rx_dma_addr[entry] =
1192 pci_map_single(lp->pci_dev,
1193 newskb->data,
1194 PKT_BUF_SZ - 2,
1195 PCI_DMA_FROMDEVICE);
1196 rxp->base = le32_to_cpu(lp->rx_dma_addr[entry]);
1197 rx_in_place = 1;
1198 } else
1199 skb = NULL;
1200 } else {
1201 skb = dev_alloc_skb(pkt_len + 2);
1202 }
1203
1204 if (skb == NULL) {
1205 if (netif_msg_drv(lp))
1206 printk(KERN_ERR
1207 "%s: Memory squeeze, dropping packet.\n",
1208 dev->name);
1209 lp->stats.rx_dropped++;
1210 return;
1211 }
1212 skb->dev = dev;
1213 if (!rx_in_place) {
1214 skb_reserve(skb, 2); /* 16 byte align */
1215 skb_put(skb, pkt_len); /* Make room */
1216 pci_dma_sync_single_for_cpu(lp->pci_dev,
1217 lp->rx_dma_addr[entry],
1218 PKT_BUF_SZ - 2,
1219 PCI_DMA_FROMDEVICE);
1220 eth_copy_and_sum(skb,
1221 (unsigned char *)(lp->rx_skbuff[entry]->data),
1222 pkt_len, 0);
1223 pci_dma_sync_single_for_device(lp->pci_dev,
1224 lp->rx_dma_addr[entry],
1225 PKT_BUF_SZ - 2,
1226 PCI_DMA_FROMDEVICE);
1227 }
1228 lp->stats.rx_bytes += skb->len;
1229 skb->protocol = eth_type_trans(skb, dev);
1230 netif_rx(skb);
1231 dev->last_rx = jiffies;
1232 lp->stats.rx_packets++;
1233 return;
1234}
1235
1236
1237static void pcnet32_rx(struct net_device *dev)
Don Fry9691edd2006-09-13 10:16:21 -07001238{
1239 struct pcnet32_private *lp = dev->priv;
1240 int entry = lp->cur_rx & lp->rx_mod_mask;
Don Fry3904c322006-09-13 10:16:38 -07001241 struct pcnet32_rx_head *rxp = &lp->rx_ring[entry];
1242 int npackets = 0;
Don Fry9691edd2006-09-13 10:16:21 -07001243 int boguscnt = lp->rx_ring_size / 2;
1244
1245 /* If we own the next entry, it's a new packet. Send it up. */
Don Fry3904c322006-09-13 10:16:38 -07001246 while (boguscnt > npackets && (short)le16_to_cpu(rxp->status) >= 0) {
1247 pcnet32_rx_entry(dev, lp, rxp, entry);
1248 npackets += 1;
Don Fry9691edd2006-09-13 10:16:21 -07001249 /*
Don Fry3904c322006-09-13 10:16:38 -07001250 * The docs say that the buffer length isn't touched, but Andrew
1251 * Boyd of QNX reports that some revs of the 79C965 clear it.
Don Fry9691edd2006-09-13 10:16:21 -07001252 */
Don Fry3904c322006-09-13 10:16:38 -07001253 rxp->buf_length = le16_to_cpu(2 - PKT_BUF_SZ);
1254 wmb(); /* Make sure owner changes after others are visible */
1255 rxp->status = le16_to_cpu(0x8000);
Don Fry9691edd2006-09-13 10:16:21 -07001256 entry = (++lp->cur_rx) & lp->rx_mod_mask;
Don Fry3904c322006-09-13 10:16:38 -07001257 rxp = &lp->rx_ring[entry];
Don Fry9691edd2006-09-13 10:16:21 -07001258 }
1259
Don Fry3904c322006-09-13 10:16:38 -07001260 return;
Don Fry9691edd2006-09-13 10:16:21 -07001261}
1262
1263static int pcnet32_tx(struct net_device *dev, u16 csr0)
1264{
1265 struct pcnet32_private *lp = dev->priv;
1266 unsigned int dirty_tx = lp->dirty_tx;
1267 int delta;
1268 int must_restart = 0;
1269
1270 while (dirty_tx != lp->cur_tx) {
1271 int entry = dirty_tx & lp->tx_mod_mask;
1272 int status = (short)le16_to_cpu(lp->tx_ring[entry].status);
1273
1274 if (status < 0)
1275 break; /* It still hasn't been Txed */
1276
1277 lp->tx_ring[entry].base = 0;
1278
1279 if (status & 0x4000) {
Don Fry3904c322006-09-13 10:16:38 -07001280 /* There was a major error, log it. */
Don Fry9691edd2006-09-13 10:16:21 -07001281 int err_status = le32_to_cpu(lp->tx_ring[entry].misc);
1282 lp->stats.tx_errors++;
1283 if (netif_msg_tx_err(lp))
1284 printk(KERN_ERR
1285 "%s: Tx error status=%04x err_status=%08x\n",
1286 dev->name, status,
1287 err_status);
1288 if (err_status & 0x04000000)
1289 lp->stats.tx_aborted_errors++;
1290 if (err_status & 0x08000000)
1291 lp->stats.tx_carrier_errors++;
1292 if (err_status & 0x10000000)
1293 lp->stats.tx_window_errors++;
1294#ifndef DO_DXSUFLO
1295 if (err_status & 0x40000000) {
1296 lp->stats.tx_fifo_errors++;
1297 /* Ackk! On FIFO errors the Tx unit is turned off! */
1298 /* Remove this verbosity later! */
1299 if (netif_msg_tx_err(lp))
1300 printk(KERN_ERR
1301 "%s: Tx FIFO error! CSR0=%4.4x\n",
1302 dev->name, csr0);
1303 must_restart = 1;
1304 }
1305#else
1306 if (err_status & 0x40000000) {
1307 lp->stats.tx_fifo_errors++;
1308 if (!lp->dxsuflo) { /* If controller doesn't recover ... */
1309 /* Ackk! On FIFO errors the Tx unit is turned off! */
1310 /* Remove this verbosity later! */
Don Fry3904c322006-09-13 10:16:38 -07001311 if (netif_msg_tx_err(lp))
Don Fry9691edd2006-09-13 10:16:21 -07001312 printk(KERN_ERR
1313 "%s: Tx FIFO error! CSR0=%4.4x\n",
1314 dev->name, csr0);
1315 must_restart = 1;
1316 }
1317 }
1318#endif
1319 } else {
1320 if (status & 0x1800)
1321 lp->stats.collisions++;
1322 lp->stats.tx_packets++;
1323 }
1324
1325 /* We must free the original skb */
1326 if (lp->tx_skbuff[entry]) {
1327 pci_unmap_single(lp->pci_dev,
1328 lp->tx_dma_addr[entry],
1329 lp->tx_skbuff[entry]->
1330 len, PCI_DMA_TODEVICE);
Don Fry3904c322006-09-13 10:16:38 -07001331 dev_kfree_skb_any(lp->tx_skbuff[entry]);
Don Fry9691edd2006-09-13 10:16:21 -07001332 lp->tx_skbuff[entry] = NULL;
1333 lp->tx_dma_addr[entry] = 0;
1334 }
1335 dirty_tx++;
1336 }
1337
Don Fry3904c322006-09-13 10:16:38 -07001338 delta = (lp->cur_tx - dirty_tx) & (lp->tx_mod_mask + lp->tx_ring_size);
Don Fry9691edd2006-09-13 10:16:21 -07001339 if (delta > lp->tx_ring_size) {
1340 if (netif_msg_drv(lp))
1341 printk(KERN_ERR
1342 "%s: out-of-sync dirty pointer, %d vs. %d, full=%d.\n",
1343 dev->name, dirty_tx, lp->cur_tx,
1344 lp->tx_full);
1345 dirty_tx += lp->tx_ring_size;
1346 delta -= lp->tx_ring_size;
1347 }
1348
1349 if (lp->tx_full &&
1350 netif_queue_stopped(dev) &&
1351 delta < lp->tx_ring_size - 2) {
1352 /* The ring is no longer full, clear tbusy. */
1353 lp->tx_full = 0;
1354 netif_wake_queue(dev);
1355 }
1356 lp->dirty_tx = dirty_tx;
1357
1358 return must_restart;
1359}
1360
Don Fryac62ef02006-03-20 15:26:03 -08001361#define PCNET32_REGS_PER_PHY 32
1362#define PCNET32_MAX_PHYS 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363static int pcnet32_get_regs_len(struct net_device *dev)
1364{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001365 struct pcnet32_private *lp = dev->priv;
1366 int j = lp->phycount * PCNET32_REGS_PER_PHY;
Don Fryac62ef02006-03-20 15:26:03 -08001367
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001368 return ((PCNET32_NUM_REGS + j) * sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369}
1370
1371static void pcnet32_get_regs(struct net_device *dev, struct ethtool_regs *regs,
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001372 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001374 int i, csr0;
1375 u16 *buff = ptr;
1376 struct pcnet32_private *lp = dev->priv;
1377 struct pcnet32_access *a = &lp->a;
1378 ulong ioaddr = dev->base_addr;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001379 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001381 spin_lock_irqsave(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Don Frydf27f4a2006-06-29 13:55:02 -07001383 csr0 = a->read_csr(ioaddr, CSR0);
1384 if (!(csr0 & CSR0_STOP)) /* If not stopped */
1385 pcnet32_suspend(dev, &flags, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001387 /* read address PROM */
1388 for (i = 0; i < 16; i += 2)
1389 *buff++ = inw(ioaddr + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001391 /* read control and status registers */
1392 for (i = 0; i < 90; i++) {
1393 *buff++ = a->read_csr(ioaddr, i);
1394 }
1395
1396 *buff++ = a->read_csr(ioaddr, 112);
1397 *buff++ = a->read_csr(ioaddr, 114);
1398
1399 /* read bus configuration registers */
1400 for (i = 0; i < 30; i++) {
1401 *buff++ = a->read_bcr(ioaddr, i);
1402 }
1403 *buff++ = 0; /* skip bcr30 so as not to hang 79C976 */
1404 for (i = 31; i < 36; i++) {
1405 *buff++ = a->read_bcr(ioaddr, i);
1406 }
1407
1408 /* read mii phy registers */
1409 if (lp->mii) {
1410 int j;
1411 for (j = 0; j < PCNET32_MAX_PHYS; j++) {
1412 if (lp->phymask & (1 << j)) {
1413 for (i = 0; i < PCNET32_REGS_PER_PHY; i++) {
1414 lp->a.write_bcr(ioaddr, 33,
1415 (j << 5) | i);
1416 *buff++ = lp->a.read_bcr(ioaddr, 34);
1417 }
1418 }
1419 }
1420 }
1421
Don Frydf27f4a2006-06-29 13:55:02 -07001422 if (!(csr0 & CSR0_STOP)) { /* If not stopped */
1423 int csr5;
1424
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001425 /* clear SUSPEND (SPND) - CSR5 bit 0 */
Don Frydf27f4a2006-06-29 13:55:02 -07001426 csr5 = a->read_csr(ioaddr, CSR5);
1427 a->write_csr(ioaddr, CSR5, csr5 & (~CSR5_SUSPEND));
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001428 }
1429
1430 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
1433static struct ethtool_ops pcnet32_ethtool_ops = {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001434 .get_settings = pcnet32_get_settings,
1435 .set_settings = pcnet32_set_settings,
1436 .get_drvinfo = pcnet32_get_drvinfo,
1437 .get_msglevel = pcnet32_get_msglevel,
1438 .set_msglevel = pcnet32_set_msglevel,
1439 .nway_reset = pcnet32_nway_reset,
1440 .get_link = pcnet32_get_link,
1441 .get_ringparam = pcnet32_get_ringparam,
1442 .set_ringparam = pcnet32_set_ringparam,
1443 .get_tx_csum = ethtool_op_get_tx_csum,
1444 .get_sg = ethtool_op_get_sg,
1445 .get_tso = ethtool_op_get_tso,
1446 .get_strings = pcnet32_get_strings,
1447 .self_test_count = pcnet32_self_test_count,
1448 .self_test = pcnet32_ethtool_test,
1449 .phys_id = pcnet32_phys_id,
1450 .get_regs_len = pcnet32_get_regs_len,
1451 .get_regs = pcnet32_get_regs,
1452 .get_perm_addr = ethtool_op_get_perm_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453};
1454
1455/* only probes for non-PCI devices, the rest are handled by
1456 * pci_register_driver via pcnet32_probe_pci */
1457
Don Frydcaf9762006-06-29 13:52:29 -07001458static void __devinit pcnet32_probe_vlbus(unsigned int *pcnet32_portlist)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001460 unsigned int *port, ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001462 /* search for PCnet32 VLB cards at known addresses */
1463 for (port = pcnet32_portlist; (ioaddr = *port); port++) {
1464 if (request_region
1465 (ioaddr, PCNET32_TOTAL_SIZE, "pcnet32_probe_vlbus")) {
1466 /* check if there is really a pcnet chip on that ioaddr */
1467 if ((inb(ioaddr + 14) == 0x57)
1468 && (inb(ioaddr + 15) == 0x57)) {
1469 pcnet32_probe1(ioaddr, 0, NULL);
1470 } else {
1471 release_region(ioaddr, PCNET32_TOTAL_SIZE);
1472 }
1473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477static int __devinit
1478pcnet32_probe_pci(struct pci_dev *pdev, const struct pci_device_id *ent)
1479{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001480 unsigned long ioaddr;
1481 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001483 err = pci_enable_device(pdev);
1484 if (err < 0) {
1485 if (pcnet32_debug & NETIF_MSG_PROBE)
1486 printk(KERN_ERR PFX
1487 "failed to enable device -- err=%d\n", err);
1488 return err;
1489 }
1490 pci_set_master(pdev);
1491
1492 ioaddr = pci_resource_start(pdev, 0);
1493 if (!ioaddr) {
1494 if (pcnet32_debug & NETIF_MSG_PROBE)
1495 printk(KERN_ERR PFX
1496 "card has no PCI IO resources, aborting\n");
1497 return -ENODEV;
1498 }
1499
1500 if (!pci_dma_supported(pdev, PCNET32_DMA_MASK)) {
1501 if (pcnet32_debug & NETIF_MSG_PROBE)
1502 printk(KERN_ERR PFX
1503 "architecture does not support 32bit PCI busmaster DMA\n");
1504 return -ENODEV;
1505 }
1506 if (request_region(ioaddr, PCNET32_TOTAL_SIZE, "pcnet32_probe_pci") ==
1507 NULL) {
1508 if (pcnet32_debug & NETIF_MSG_PROBE)
1509 printk(KERN_ERR PFX
1510 "io address range already allocated\n");
1511 return -EBUSY;
1512 }
1513
1514 err = pcnet32_probe1(ioaddr, 1, pdev);
1515 if (err < 0) {
1516 pci_disable_device(pdev);
1517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521/* pcnet32_probe1
1522 * Called from both pcnet32_probe_vlbus and pcnet_probe_pci.
1523 * pdev will be NULL when called from pcnet32_probe_vlbus.
1524 */
1525static int __devinit
1526pcnet32_probe1(unsigned long ioaddr, int shared, struct pci_dev *pdev)
1527{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001528 struct pcnet32_private *lp;
1529 dma_addr_t lp_dma_addr;
1530 int i, media;
1531 int fdx, mii, fset, dxsuflo;
1532 int chip_version;
1533 char *chipname;
1534 struct net_device *dev;
1535 struct pcnet32_access *a = NULL;
1536 u8 promaddr[6];
1537 int ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001539 /* reset the chip */
1540 pcnet32_wio_reset(ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001542 /* NOTE: 16-bit check is first, otherwise some older PCnet chips fail */
1543 if (pcnet32_wio_read_csr(ioaddr, 0) == 4 && pcnet32_wio_check(ioaddr)) {
1544 a = &pcnet32_wio;
1545 } else {
1546 pcnet32_dwio_reset(ioaddr);
1547 if (pcnet32_dwio_read_csr(ioaddr, 0) == 4
1548 && pcnet32_dwio_check(ioaddr)) {
1549 a = &pcnet32_dwio;
1550 } else
1551 goto err_release_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001554 chip_version =
1555 a->read_csr(ioaddr, 88) | (a->read_csr(ioaddr, 89) << 16);
1556 if ((pcnet32_debug & NETIF_MSG_PROBE) && (pcnet32_debug & NETIF_MSG_HW))
1557 printk(KERN_INFO " PCnet chip version is %#x.\n",
1558 chip_version);
1559 if ((chip_version & 0xfff) != 0x003) {
1560 if (pcnet32_debug & NETIF_MSG_PROBE)
1561 printk(KERN_INFO PFX "Unsupported chip version.\n");
1562 goto err_release_region;
1563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001565 /* initialize variables */
1566 fdx = mii = fset = dxsuflo = 0;
1567 chip_version = (chip_version >> 12) & 0xffff;
1568
1569 switch (chip_version) {
1570 case 0x2420:
1571 chipname = "PCnet/PCI 79C970"; /* PCI */
1572 break;
1573 case 0x2430:
1574 if (shared)
1575 chipname = "PCnet/PCI 79C970"; /* 970 gives the wrong chip id back */
1576 else
1577 chipname = "PCnet/32 79C965"; /* 486/VL bus */
1578 break;
1579 case 0x2621:
1580 chipname = "PCnet/PCI II 79C970A"; /* PCI */
1581 fdx = 1;
1582 break;
1583 case 0x2623:
1584 chipname = "PCnet/FAST 79C971"; /* PCI */
1585 fdx = 1;
1586 mii = 1;
1587 fset = 1;
1588 break;
1589 case 0x2624:
1590 chipname = "PCnet/FAST+ 79C972"; /* PCI */
1591 fdx = 1;
1592 mii = 1;
1593 fset = 1;
1594 break;
1595 case 0x2625:
1596 chipname = "PCnet/FAST III 79C973"; /* PCI */
1597 fdx = 1;
1598 mii = 1;
1599 break;
1600 case 0x2626:
1601 chipname = "PCnet/Home 79C978"; /* PCI */
1602 fdx = 1;
1603 /*
1604 * This is based on specs published at www.amd.com. This section
1605 * assumes that a card with a 79C978 wants to go into standard
1606 * ethernet mode. The 79C978 can also go into 1Mb HomePNA mode,
1607 * and the module option homepna=1 can select this instead.
1608 */
1609 media = a->read_bcr(ioaddr, 49);
1610 media &= ~3; /* default to 10Mb ethernet */
1611 if (cards_found < MAX_UNITS && homepna[cards_found])
1612 media |= 1; /* switch to home wiring mode */
1613 if (pcnet32_debug & NETIF_MSG_PROBE)
1614 printk(KERN_DEBUG PFX "media set to %sMbit mode.\n",
1615 (media & 1) ? "1" : "10");
1616 a->write_bcr(ioaddr, 49, media);
1617 break;
1618 case 0x2627:
1619 chipname = "PCnet/FAST III 79C975"; /* PCI */
1620 fdx = 1;
1621 mii = 1;
1622 break;
1623 case 0x2628:
1624 chipname = "PCnet/PRO 79C976";
1625 fdx = 1;
1626 mii = 1;
1627 break;
1628 default:
1629 if (pcnet32_debug & NETIF_MSG_PROBE)
1630 printk(KERN_INFO PFX
1631 "PCnet version %#x, no PCnet32 chip.\n",
1632 chip_version);
1633 goto err_release_region;
1634 }
1635
1636 /*
1637 * On selected chips turn on the BCR18:NOUFLO bit. This stops transmit
1638 * starting until the packet is loaded. Strike one for reliability, lose
1639 * one for latency - although on PCI this isnt a big loss. Older chips
1640 * have FIFO's smaller than a packet, so you can't do this.
1641 * Turn on BCR18:BurstRdEn and BCR18:BurstWrEn.
1642 */
1643
1644 if (fset) {
1645 a->write_bcr(ioaddr, 18, (a->read_bcr(ioaddr, 18) | 0x0860));
1646 a->write_csr(ioaddr, 80,
1647 (a->read_csr(ioaddr, 80) & 0x0C00) | 0x0c00);
1648 dxsuflo = 1;
1649 }
1650
1651 dev = alloc_etherdev(0);
1652 if (!dev) {
1653 if (pcnet32_debug & NETIF_MSG_PROBE)
1654 printk(KERN_ERR PFX "Memory allocation failed.\n");
1655 ret = -ENOMEM;
1656 goto err_release_region;
1657 }
1658 SET_NETDEV_DEV(dev, &pdev->dev);
1659
1660 if (pcnet32_debug & NETIF_MSG_PROBE)
1661 printk(KERN_INFO PFX "%s at %#3lx,", chipname, ioaddr);
1662
1663 /* In most chips, after a chip reset, the ethernet address is read from the
1664 * station address PROM at the base address and programmed into the
1665 * "Physical Address Registers" CSR12-14.
1666 * As a precautionary measure, we read the PROM values and complain if
Linas Vepstasbc0e1fc2006-03-28 16:36:23 -06001667 * they disagree with the CSRs. If they miscompare, and the PROM addr
1668 * is valid, then the PROM addr is used.
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001669 */
1670 for (i = 0; i < 3; i++) {
1671 unsigned int val;
1672 val = a->read_csr(ioaddr, i + 12) & 0x0ffff;
1673 /* There may be endianness issues here. */
1674 dev->dev_addr[2 * i] = val & 0x0ff;
1675 dev->dev_addr[2 * i + 1] = (val >> 8) & 0x0ff;
1676 }
1677
1678 /* read PROM address and compare with CSR address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 for (i = 0; i < 6; i++)
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001680 promaddr[i] = inb(ioaddr + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001682 if (memcmp(promaddr, dev->dev_addr, 6)
1683 || !is_valid_ether_addr(dev->dev_addr)) {
1684 if (is_valid_ether_addr(promaddr)) {
1685 if (pcnet32_debug & NETIF_MSG_PROBE) {
1686 printk(" warning: CSR address invalid,\n");
1687 printk(KERN_INFO
1688 " using instead PROM address of");
1689 }
1690 memcpy(dev->dev_addr, promaddr, 6);
1691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 }
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001693 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001695 /* if the ethernet address is not valid, force to 00:00:00:00:00:00 */
1696 if (!is_valid_ether_addr(dev->perm_addr))
1697 memset(dev->dev_addr, 0, sizeof(dev->dev_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001699 if (pcnet32_debug & NETIF_MSG_PROBE) {
1700 for (i = 0; i < 6; i++)
1701 printk(" %2.2x", dev->dev_addr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001703 /* Version 0x2623 and 0x2624 */
1704 if (((chip_version + 1) & 0xfffe) == 0x2624) {
1705 i = a->read_csr(ioaddr, 80) & 0x0C00; /* Check tx_start_pt */
1706 printk("\n" KERN_INFO " tx_start_pt(0x%04x):", i);
1707 switch (i >> 10) {
1708 case 0:
1709 printk(" 20 bytes,");
1710 break;
1711 case 1:
1712 printk(" 64 bytes,");
1713 break;
1714 case 2:
1715 printk(" 128 bytes,");
1716 break;
1717 case 3:
1718 printk("~220 bytes,");
1719 break;
1720 }
1721 i = a->read_bcr(ioaddr, 18); /* Check Burst/Bus control */
1722 printk(" BCR18(%x):", i & 0xffff);
1723 if (i & (1 << 5))
1724 printk("BurstWrEn ");
1725 if (i & (1 << 6))
1726 printk("BurstRdEn ");
1727 if (i & (1 << 7))
1728 printk("DWordIO ");
1729 if (i & (1 << 11))
1730 printk("NoUFlow ");
1731 i = a->read_bcr(ioaddr, 25);
1732 printk("\n" KERN_INFO " SRAMSIZE=0x%04x,", i << 8);
1733 i = a->read_bcr(ioaddr, 26);
1734 printk(" SRAM_BND=0x%04x,", i << 8);
1735 i = a->read_bcr(ioaddr, 27);
1736 if (i & (1 << 14))
1737 printk("LowLatRx");
1738 }
1739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001741 dev->base_addr = ioaddr;
1742 /* pci_alloc_consistent returns page-aligned memory, so we do not have to check the alignment */
1743 if ((lp =
1744 pci_alloc_consistent(pdev, sizeof(*lp), &lp_dma_addr)) == NULL) {
1745 if (pcnet32_debug & NETIF_MSG_PROBE)
1746 printk(KERN_ERR PFX
1747 "Consistent memory allocation failed.\n");
1748 ret = -ENOMEM;
1749 goto err_free_netdev;
1750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001752 memset(lp, 0, sizeof(*lp));
1753 lp->dma_addr = lp_dma_addr;
1754 lp->pci_dev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001756 spin_lock_init(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001758 SET_MODULE_OWNER(dev);
1759 SET_NETDEV_DEV(dev, &pdev->dev);
1760 dev->priv = lp;
1761 lp->name = chipname;
1762 lp->shared_irq = shared;
1763 lp->tx_ring_size = TX_RING_SIZE; /* default tx ring size */
1764 lp->rx_ring_size = RX_RING_SIZE; /* default rx ring size */
1765 lp->tx_mod_mask = lp->tx_ring_size - 1;
1766 lp->rx_mod_mask = lp->rx_ring_size - 1;
1767 lp->tx_len_bits = (PCNET32_LOG_TX_BUFFERS << 12);
1768 lp->rx_len_bits = (PCNET32_LOG_RX_BUFFERS << 4);
1769 lp->mii_if.full_duplex = fdx;
1770 lp->mii_if.phy_id_mask = 0x1f;
1771 lp->mii_if.reg_num_mask = 0x1f;
1772 lp->dxsuflo = dxsuflo;
1773 lp->mii = mii;
Don Fry8d916262006-08-22 10:22:37 -07001774 lp->chip_version = chip_version;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001775 lp->msg_enable = pcnet32_debug;
1776 if ((cards_found >= MAX_UNITS)
1777 || (options[cards_found] > sizeof(options_mapping)))
1778 lp->options = PCNET32_PORT_ASEL;
1779 else
1780 lp->options = options_mapping[options[cards_found]];
1781 lp->mii_if.dev = dev;
1782 lp->mii_if.mdio_read = mdio_read;
1783 lp->mii_if.mdio_write = mdio_write;
1784
1785 if (fdx && !(lp->options & PCNET32_PORT_ASEL) &&
1786 ((cards_found >= MAX_UNITS) || full_duplex[cards_found]))
1787 lp->options |= PCNET32_PORT_FD;
1788
1789 if (!a) {
1790 if (pcnet32_debug & NETIF_MSG_PROBE)
1791 printk(KERN_ERR PFX "No access methods\n");
1792 ret = -ENODEV;
1793 goto err_free_consistent;
1794 }
1795 lp->a = *a;
1796
1797 /* prior to register_netdev, dev->name is not yet correct */
1798 if (pcnet32_alloc_ring(dev, pci_name(lp->pci_dev))) {
1799 ret = -ENOMEM;
1800 goto err_free_ring;
1801 }
1802 /* detect special T1/E1 WAN card by checking for MAC address */
1803 if (dev->dev_addr[0] == 0x00 && dev->dev_addr[1] == 0xe0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 && dev->dev_addr[2] == 0x75)
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001805 lp->options = PCNET32_PORT_FD | PCNET32_PORT_GPSI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001807 lp->init_block.mode = le16_to_cpu(0x0003); /* Disable Rx and Tx. */
1808 lp->init_block.tlen_rlen =
1809 le16_to_cpu(lp->tx_len_bits | lp->rx_len_bits);
1810 for (i = 0; i < 6; i++)
1811 lp->init_block.phys_addr[i] = dev->dev_addr[i];
1812 lp->init_block.filter[0] = 0x00000000;
1813 lp->init_block.filter[1] = 0x00000000;
1814 lp->init_block.rx_ring = (u32) le32_to_cpu(lp->rx_ring_dma_addr);
1815 lp->init_block.tx_ring = (u32) le32_to_cpu(lp->tx_ring_dma_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001817 /* switch pcnet32 to 32bit mode */
1818 a->write_bcr(ioaddr, 20, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001820 a->write_csr(ioaddr, 1, (lp->dma_addr + offsetof(struct pcnet32_private,
1821 init_block)) & 0xffff);
1822 a->write_csr(ioaddr, 2, (lp->dma_addr + offsetof(struct pcnet32_private,
1823 init_block)) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001825 if (pdev) { /* use the IRQ provided by PCI */
1826 dev->irq = pdev->irq;
1827 if (pcnet32_debug & NETIF_MSG_PROBE)
1828 printk(" assigned IRQ %d.\n", dev->irq);
1829 } else {
1830 unsigned long irq_mask = probe_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001832 /*
1833 * To auto-IRQ we enable the initialization-done and DMA error
1834 * interrupts. For ISA boards we get a DMA error, but VLB and PCI
1835 * boards will work.
1836 */
1837 /* Trigger an initialization just for the interrupt. */
Don Fryb368a3f2006-09-13 10:16:07 -07001838 a->write_csr(ioaddr, CSR0, CSR0_INTEN | CSR0_INIT);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001839 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001841 dev->irq = probe_irq_off(irq_mask);
1842 if (!dev->irq) {
1843 if (pcnet32_debug & NETIF_MSG_PROBE)
1844 printk(", failed to detect IRQ line.\n");
1845 ret = -ENODEV;
1846 goto err_free_ring;
1847 }
1848 if (pcnet32_debug & NETIF_MSG_PROBE)
1849 printk(", probed IRQ %d.\n", dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001852 /* Set the mii phy_id so that we can query the link state */
1853 if (lp->mii) {
1854 /* lp->phycount and lp->phymask are set to 0 by memset above */
Don Fryac62ef02006-03-20 15:26:03 -08001855
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001856 lp->mii_if.phy_id = ((lp->a.read_bcr(ioaddr, 33)) >> 5) & 0x1f;
1857 /* scan for PHYs */
1858 for (i = 0; i < PCNET32_MAX_PHYS; i++) {
1859 unsigned short id1, id2;
Don Fryac62ef02006-03-20 15:26:03 -08001860
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001861 id1 = mdio_read(dev, i, MII_PHYSID1);
1862 if (id1 == 0xffff)
1863 continue;
1864 id2 = mdio_read(dev, i, MII_PHYSID2);
1865 if (id2 == 0xffff)
1866 continue;
1867 if (i == 31 && ((chip_version + 1) & 0xfffe) == 0x2624)
1868 continue; /* 79C971 & 79C972 have phantom phy at id 31 */
1869 lp->phycount++;
1870 lp->phymask |= (1 << i);
1871 lp->mii_if.phy_id = i;
1872 if (pcnet32_debug & NETIF_MSG_PROBE)
1873 printk(KERN_INFO PFX
1874 "Found PHY %04x:%04x at address %d.\n",
1875 id1, id2, i);
1876 }
1877 lp->a.write_bcr(ioaddr, 33, (lp->mii_if.phy_id) << 5);
1878 if (lp->phycount > 1) {
1879 lp->options |= PCNET32_PORT_MII;
1880 }
Don Fryac62ef02006-03-20 15:26:03 -08001881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001883 init_timer(&lp->watchdog_timer);
1884 lp->watchdog_timer.data = (unsigned long)dev;
1885 lp->watchdog_timer.function = (void *)&pcnet32_watchdog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001887 /* The PCNET32-specific entries in the device structure. */
1888 dev->open = &pcnet32_open;
1889 dev->hard_start_xmit = &pcnet32_start_xmit;
1890 dev->stop = &pcnet32_close;
1891 dev->get_stats = &pcnet32_get_stats;
1892 dev->set_multicast_list = &pcnet32_set_multicast_list;
1893 dev->do_ioctl = &pcnet32_ioctl;
1894 dev->ethtool_ops = &pcnet32_ethtool_ops;
1895 dev->tx_timeout = pcnet32_tx_timeout;
1896 dev->watchdog_timeo = (5 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898#ifdef CONFIG_NET_POLL_CONTROLLER
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001899 dev->poll_controller = pcnet32_poll_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900#endif
1901
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001902 /* Fill in the generic fields of the device structure. */
1903 if (register_netdev(dev))
1904 goto err_free_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001906 if (pdev) {
1907 pci_set_drvdata(pdev, dev);
1908 } else {
1909 lp->next = pcnet32_dev;
1910 pcnet32_dev = dev;
1911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001913 if (pcnet32_debug & NETIF_MSG_PROBE)
1914 printk(KERN_INFO "%s: registered as %s\n", dev->name, lp->name);
1915 cards_found++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001917 /* enable LED writes */
1918 a->write_bcr(ioaddr, 2, a->read_bcr(ioaddr, 2) | 0x1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001920 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001922 err_free_ring:
1923 pcnet32_free_ring(dev);
1924 err_free_consistent:
1925 pci_free_consistent(lp->pci_dev, sizeof(*lp), lp, lp->dma_addr);
1926 err_free_netdev:
1927 free_netdev(dev);
1928 err_release_region:
1929 release_region(ioaddr, PCNET32_TOTAL_SIZE);
1930 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
Don Frya88c8442005-11-01 12:04:33 -08001933/* if any allocation fails, caller must also call pcnet32_free_ring */
1934static int pcnet32_alloc_ring(struct net_device *dev, char *name)
Hubert WS Lineabf0412005-09-14 11:39:25 -07001935{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001936 struct pcnet32_private *lp = dev->priv;
Hubert WS Lineabf0412005-09-14 11:39:25 -07001937
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001938 lp->tx_ring = pci_alloc_consistent(lp->pci_dev,
1939 sizeof(struct pcnet32_tx_head) *
1940 lp->tx_ring_size,
1941 &lp->tx_ring_dma_addr);
1942 if (lp->tx_ring == NULL) {
Don Fry12fa30f2006-06-29 13:53:48 -07001943 if (netif_msg_drv(lp))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001944 printk("\n" KERN_ERR PFX
1945 "%s: Consistent memory allocation failed.\n",
1946 name);
1947 return -ENOMEM;
1948 }
Hubert WS Lineabf0412005-09-14 11:39:25 -07001949
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001950 lp->rx_ring = pci_alloc_consistent(lp->pci_dev,
1951 sizeof(struct pcnet32_rx_head) *
1952 lp->rx_ring_size,
1953 &lp->rx_ring_dma_addr);
1954 if (lp->rx_ring == NULL) {
Don Fry12fa30f2006-06-29 13:53:48 -07001955 if (netif_msg_drv(lp))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001956 printk("\n" KERN_ERR PFX
1957 "%s: Consistent memory allocation failed.\n",
1958 name);
1959 return -ENOMEM;
1960 }
Hubert WS Lineabf0412005-09-14 11:39:25 -07001961
Don Fry12fa30f2006-06-29 13:53:48 -07001962 lp->tx_dma_addr = kcalloc(lp->tx_ring_size, sizeof(dma_addr_t),
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001963 GFP_ATOMIC);
1964 if (!lp->tx_dma_addr) {
Don Fry12fa30f2006-06-29 13:53:48 -07001965 if (netif_msg_drv(lp))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001966 printk("\n" KERN_ERR PFX
1967 "%s: Memory allocation failed.\n", name);
1968 return -ENOMEM;
1969 }
Hubert WS Lineabf0412005-09-14 11:39:25 -07001970
Don Fry12fa30f2006-06-29 13:53:48 -07001971 lp->rx_dma_addr = kcalloc(lp->rx_ring_size, sizeof(dma_addr_t),
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001972 GFP_ATOMIC);
1973 if (!lp->rx_dma_addr) {
Don Fry12fa30f2006-06-29 13:53:48 -07001974 if (netif_msg_drv(lp))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001975 printk("\n" KERN_ERR PFX
1976 "%s: Memory allocation failed.\n", name);
1977 return -ENOMEM;
1978 }
Hubert WS Lineabf0412005-09-14 11:39:25 -07001979
Don Fry12fa30f2006-06-29 13:53:48 -07001980 lp->tx_skbuff = kcalloc(lp->tx_ring_size, sizeof(struct sk_buff *),
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001981 GFP_ATOMIC);
1982 if (!lp->tx_skbuff) {
Don Fry12fa30f2006-06-29 13:53:48 -07001983 if (netif_msg_drv(lp))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001984 printk("\n" KERN_ERR PFX
1985 "%s: Memory allocation failed.\n", name);
1986 return -ENOMEM;
1987 }
Hubert WS Lineabf0412005-09-14 11:39:25 -07001988
Don Fry12fa30f2006-06-29 13:53:48 -07001989 lp->rx_skbuff = kcalloc(lp->rx_ring_size, sizeof(struct sk_buff *),
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001990 GFP_ATOMIC);
1991 if (!lp->rx_skbuff) {
Don Fry12fa30f2006-06-29 13:53:48 -07001992 if (netif_msg_drv(lp))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001993 printk("\n" KERN_ERR PFX
1994 "%s: Memory allocation failed.\n", name);
1995 return -ENOMEM;
1996 }
Hubert WS Lineabf0412005-09-14 11:39:25 -07001997
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05001998 return 0;
Hubert WS Lineabf0412005-09-14 11:39:25 -07001999}
2000
Hubert WS Lineabf0412005-09-14 11:39:25 -07002001static void pcnet32_free_ring(struct net_device *dev)
2002{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002003 struct pcnet32_private *lp = dev->priv;
Hubert WS Lineabf0412005-09-14 11:39:25 -07002004
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002005 kfree(lp->tx_skbuff);
2006 lp->tx_skbuff = NULL;
Hubert WS Lineabf0412005-09-14 11:39:25 -07002007
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002008 kfree(lp->rx_skbuff);
2009 lp->rx_skbuff = NULL;
Hubert WS Lineabf0412005-09-14 11:39:25 -07002010
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002011 kfree(lp->tx_dma_addr);
2012 lp->tx_dma_addr = NULL;
Hubert WS Lineabf0412005-09-14 11:39:25 -07002013
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002014 kfree(lp->rx_dma_addr);
2015 lp->rx_dma_addr = NULL;
Hubert WS Lineabf0412005-09-14 11:39:25 -07002016
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002017 if (lp->tx_ring) {
2018 pci_free_consistent(lp->pci_dev,
2019 sizeof(struct pcnet32_tx_head) *
2020 lp->tx_ring_size, lp->tx_ring,
2021 lp->tx_ring_dma_addr);
2022 lp->tx_ring = NULL;
2023 }
Hubert WS Lineabf0412005-09-14 11:39:25 -07002024
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002025 if (lp->rx_ring) {
2026 pci_free_consistent(lp->pci_dev,
2027 sizeof(struct pcnet32_rx_head) *
2028 lp->rx_ring_size, lp->rx_ring,
2029 lp->rx_ring_dma_addr);
2030 lp->rx_ring = NULL;
2031 }
Hubert WS Lineabf0412005-09-14 11:39:25 -07002032}
2033
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002034static int pcnet32_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002036 struct pcnet32_private *lp = dev->priv;
2037 unsigned long ioaddr = dev->base_addr;
2038 u16 val;
2039 int i;
2040 int rc;
2041 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002043 if (request_irq(dev->irq, &pcnet32_interrupt,
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07002044 lp->shared_irq ? IRQF_SHARED : 0, dev->name,
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002045 (void *)dev)) {
2046 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002049 spin_lock_irqsave(&lp->lock, flags);
2050 /* Check for a valid station address */
2051 if (!is_valid_ether_addr(dev->dev_addr)) {
2052 rc = -EINVAL;
2053 goto err_free_irq;
2054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002056 /* Reset the PCNET32 */
2057 lp->a.reset(ioaddr);
2058
2059 /* switch pcnet32 to 32bit mode */
2060 lp->a.write_bcr(ioaddr, 20, 2);
2061
2062 if (netif_msg_ifup(lp))
2063 printk(KERN_DEBUG
2064 "%s: pcnet32_open() irq %d tx/rx rings %#x/%#x init %#x.\n",
2065 dev->name, dev->irq, (u32) (lp->tx_ring_dma_addr),
2066 (u32) (lp->rx_ring_dma_addr),
2067 (u32) (lp->dma_addr +
2068 offsetof(struct pcnet32_private, init_block)));
2069
2070 /* set/reset autoselect bit */
2071 val = lp->a.read_bcr(ioaddr, 2) & ~2;
2072 if (lp->options & PCNET32_PORT_ASEL)
2073 val |= 2;
2074 lp->a.write_bcr(ioaddr, 2, val);
2075
2076 /* handle full duplex setting */
2077 if (lp->mii_if.full_duplex) {
2078 val = lp->a.read_bcr(ioaddr, 9) & ~3;
2079 if (lp->options & PCNET32_PORT_FD) {
2080 val |= 1;
2081 if (lp->options == (PCNET32_PORT_FD | PCNET32_PORT_AUI))
2082 val |= 2;
2083 } else if (lp->options & PCNET32_PORT_ASEL) {
2084 /* workaround of xSeries250, turn on for 79C975 only */
Don Fry8d916262006-08-22 10:22:37 -07002085 if (lp->chip_version == 0x2627)
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002086 val |= 3;
2087 }
2088 lp->a.write_bcr(ioaddr, 9, val);
2089 }
2090
2091 /* set/reset GPSI bit in test register */
2092 val = lp->a.read_csr(ioaddr, 124) & ~0x10;
2093 if ((lp->options & PCNET32_PORT_PORTSEL) == PCNET32_PORT_GPSI)
2094 val |= 0x10;
2095 lp->a.write_csr(ioaddr, 124, val);
2096
2097 /* Allied Telesyn AT 2700/2701 FX are 100Mbit only and do not negotiate */
2098 if (lp->pci_dev->subsystem_vendor == PCI_VENDOR_ID_AT &&
Don Fry2964bbd2005-11-01 12:50:57 -08002099 (lp->pci_dev->subsystem_device == PCI_SUBDEVICE_ID_AT_2700FX ||
2100 lp->pci_dev->subsystem_device == PCI_SUBDEVICE_ID_AT_2701FX)) {
Don Fryac62ef02006-03-20 15:26:03 -08002101 if (lp->options & PCNET32_PORT_ASEL) {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002102 lp->options = PCNET32_PORT_FD | PCNET32_PORT_100;
2103 if (netif_msg_link(lp))
2104 printk(KERN_DEBUG
2105 "%s: Setting 100Mb-Full Duplex.\n",
2106 dev->name);
Don Fryac62ef02006-03-20 15:26:03 -08002107 }
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002108 }
2109 if (lp->phycount < 2) {
2110 /*
2111 * 24 Jun 2004 according AMD, in order to change the PHY,
2112 * DANAS (or DISPM for 79C976) must be set; then select the speed,
2113 * duplex, and/or enable auto negotiation, and clear DANAS
2114 */
2115 if (lp->mii && !(lp->options & PCNET32_PORT_ASEL)) {
2116 lp->a.write_bcr(ioaddr, 32,
2117 lp->a.read_bcr(ioaddr, 32) | 0x0080);
2118 /* disable Auto Negotiation, set 10Mpbs, HD */
2119 val = lp->a.read_bcr(ioaddr, 32) & ~0xb8;
2120 if (lp->options & PCNET32_PORT_FD)
2121 val |= 0x10;
2122 if (lp->options & PCNET32_PORT_100)
2123 val |= 0x08;
2124 lp->a.write_bcr(ioaddr, 32, val);
2125 } else {
2126 if (lp->options & PCNET32_PORT_ASEL) {
2127 lp->a.write_bcr(ioaddr, 32,
2128 lp->a.read_bcr(ioaddr,
2129 32) | 0x0080);
2130 /* enable auto negotiate, setup, disable fd */
2131 val = lp->a.read_bcr(ioaddr, 32) & ~0x98;
2132 val |= 0x20;
2133 lp->a.write_bcr(ioaddr, 32, val);
2134 }
2135 }
2136 } else {
2137 int first_phy = -1;
2138 u16 bmcr;
2139 u32 bcr9;
2140 struct ethtool_cmd ecmd;
2141
2142 /*
2143 * There is really no good other way to handle multiple PHYs
2144 * other than turning off all automatics
2145 */
2146 val = lp->a.read_bcr(ioaddr, 2);
2147 lp->a.write_bcr(ioaddr, 2, val & ~2);
2148 val = lp->a.read_bcr(ioaddr, 32);
2149 lp->a.write_bcr(ioaddr, 32, val & ~(1 << 7)); /* stop MII manager */
2150
2151 if (!(lp->options & PCNET32_PORT_ASEL)) {
2152 /* setup ecmd */
2153 ecmd.port = PORT_MII;
2154 ecmd.transceiver = XCVR_INTERNAL;
2155 ecmd.autoneg = AUTONEG_DISABLE;
2156 ecmd.speed =
2157 lp->
2158 options & PCNET32_PORT_100 ? SPEED_100 : SPEED_10;
2159 bcr9 = lp->a.read_bcr(ioaddr, 9);
2160
2161 if (lp->options & PCNET32_PORT_FD) {
2162 ecmd.duplex = DUPLEX_FULL;
2163 bcr9 |= (1 << 0);
2164 } else {
2165 ecmd.duplex = DUPLEX_HALF;
2166 bcr9 |= ~(1 << 0);
2167 }
2168 lp->a.write_bcr(ioaddr, 9, bcr9);
2169 }
2170
2171 for (i = 0; i < PCNET32_MAX_PHYS; i++) {
2172 if (lp->phymask & (1 << i)) {
2173 /* isolate all but the first PHY */
2174 bmcr = mdio_read(dev, i, MII_BMCR);
2175 if (first_phy == -1) {
2176 first_phy = i;
2177 mdio_write(dev, i, MII_BMCR,
2178 bmcr & ~BMCR_ISOLATE);
2179 } else {
2180 mdio_write(dev, i, MII_BMCR,
2181 bmcr | BMCR_ISOLATE);
2182 }
2183 /* use mii_ethtool_sset to setup PHY */
2184 lp->mii_if.phy_id = i;
2185 ecmd.phy_address = i;
2186 if (lp->options & PCNET32_PORT_ASEL) {
2187 mii_ethtool_gset(&lp->mii_if, &ecmd);
2188 ecmd.autoneg = AUTONEG_ENABLE;
2189 }
2190 mii_ethtool_sset(&lp->mii_if, &ecmd);
2191 }
2192 }
2193 lp->mii_if.phy_id = first_phy;
2194 if (netif_msg_link(lp))
2195 printk(KERN_INFO "%s: Using PHY number %d.\n",
2196 dev->name, first_phy);
2197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
2199#ifdef DO_DXSUFLO
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002200 if (lp->dxsuflo) { /* Disable transmit stop on underflow */
Don Fryb368a3f2006-09-13 10:16:07 -07002201 val = lp->a.read_csr(ioaddr, CSR3);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002202 val |= 0x40;
Don Fryb368a3f2006-09-13 10:16:07 -07002203 lp->a.write_csr(ioaddr, CSR3, val);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205#endif
2206
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002207 lp->init_block.mode =
2208 le16_to_cpu((lp->options & PCNET32_PORT_PORTSEL) << 7);
2209 pcnet32_load_multicast(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002211 if (pcnet32_init_ring(dev)) {
2212 rc = -ENOMEM;
2213 goto err_free_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
Hubert WS Lineabf0412005-09-14 11:39:25 -07002215
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002216 /* Re-initialize the PCNET32, and start it when done. */
2217 lp->a.write_csr(ioaddr, 1, (lp->dma_addr +
2218 offsetof(struct pcnet32_private,
2219 init_block)) & 0xffff);
2220 lp->a.write_csr(ioaddr, 2,
2221 (lp->dma_addr +
2222 offsetof(struct pcnet32_private, init_block)) >> 16);
Hubert WS Lineabf0412005-09-14 11:39:25 -07002223
Don Fryb368a3f2006-09-13 10:16:07 -07002224 lp->a.write_csr(ioaddr, CSR4, 0x0915); /* auto tx pad */
2225 lp->a.write_csr(ioaddr, CSR0, CSR0_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002227 netif_start_queue(dev);
2228
Don Fry8d916262006-08-22 10:22:37 -07002229 if (lp->chip_version >= PCNET32_79C970A) {
2230 /* Print the link status and start the watchdog */
2231 pcnet32_check_media(dev, 1);
2232 mod_timer(&(lp->watchdog_timer), PCNET32_WATCHDOG_TIMEOUT);
2233 }
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002234
2235 i = 0;
2236 while (i++ < 100)
Don Fryb368a3f2006-09-13 10:16:07 -07002237 if (lp->a.read_csr(ioaddr, CSR0) & CSR0_IDON)
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002238 break;
2239 /*
2240 * We used to clear the InitDone bit, 0x0100, here but Mark Stockton
2241 * reports that doing so triggers a bug in the '974.
2242 */
Don Fryb368a3f2006-09-13 10:16:07 -07002243 lp->a.write_csr(ioaddr, CSR0, CSR0_NORMAL);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002244
2245 if (netif_msg_ifup(lp))
2246 printk(KERN_DEBUG
2247 "%s: pcnet32 open after %d ticks, init block %#x csr0 %4.4x.\n",
2248 dev->name, i,
2249 (u32) (lp->dma_addr +
2250 offsetof(struct pcnet32_private, init_block)),
Don Fryb368a3f2006-09-13 10:16:07 -07002251 lp->a.read_csr(ioaddr, CSR0));
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002252
2253 spin_unlock_irqrestore(&lp->lock, flags);
2254
2255 return 0; /* Always succeed */
2256
2257 err_free_ring:
2258 /* free any allocated skbuffs */
Don Fryac5bfe42006-06-29 13:55:27 -07002259 pcnet32_purge_rx_ring(dev);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002260
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002261 /*
2262 * Switch back to 16bit mode to avoid problems with dumb
2263 * DOS packet driver after a warm reboot
2264 */
2265 lp->a.write_bcr(ioaddr, 20, 4);
2266
2267 err_free_irq:
2268 spin_unlock_irqrestore(&lp->lock, flags);
2269 free_irq(dev->irq, dev);
2270 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271}
2272
2273/*
2274 * The LANCE has been halted for one reason or another (busmaster memory
2275 * arbitration error, Tx FIFO underflow, driver stopped it to reconfigure,
2276 * etc.). Modern LANCE variants always reload their ring-buffer
2277 * configuration when restarted, so we must reinitialize our ring
2278 * context before restarting. As part of this reinitialization,
2279 * find all packets still on the Tx ring and pretend that they had been
2280 * sent (in effect, drop the packets on the floor) - the higher-level
2281 * protocols will time out and retransmit. It'd be better to shuffle
2282 * these skbs to a temp list and then actually re-Tx them after
2283 * restarting the chip, but I'm too lazy to do so right now. dplatt@3do.com
2284 */
2285
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002286static void pcnet32_purge_tx_ring(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002288 struct pcnet32_private *lp = dev->priv;
2289 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002291 for (i = 0; i < lp->tx_ring_size; i++) {
2292 lp->tx_ring[i].status = 0; /* CPU owns buffer */
2293 wmb(); /* Make sure adapter sees owner change */
2294 if (lp->tx_skbuff[i]) {
2295 pci_unmap_single(lp->pci_dev, lp->tx_dma_addr[i],
2296 lp->tx_skbuff[i]->len,
2297 PCI_DMA_TODEVICE);
2298 dev_kfree_skb_any(lp->tx_skbuff[i]);
2299 }
2300 lp->tx_skbuff[i] = NULL;
2301 lp->tx_dma_addr[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303}
2304
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305/* Initialize the PCNET32 Rx and Tx rings. */
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002306static int pcnet32_init_ring(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002308 struct pcnet32_private *lp = dev->priv;
2309 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002311 lp->tx_full = 0;
2312 lp->cur_rx = lp->cur_tx = 0;
2313 lp->dirty_rx = lp->dirty_tx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002315 for (i = 0; i < lp->rx_ring_size; i++) {
2316 struct sk_buff *rx_skbuff = lp->rx_skbuff[i];
2317 if (rx_skbuff == NULL) {
2318 if (!
2319 (rx_skbuff = lp->rx_skbuff[i] =
2320 dev_alloc_skb(PKT_BUF_SZ))) {
2321 /* there is not much, we can do at this point */
Don Fryb368a3f2006-09-13 10:16:07 -07002322 if (netif_msg_drv(lp))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002323 printk(KERN_ERR
2324 "%s: pcnet32_init_ring dev_alloc_skb failed.\n",
2325 dev->name);
2326 return -1;
2327 }
2328 skb_reserve(rx_skbuff, 2);
2329 }
2330
2331 rmb();
2332 if (lp->rx_dma_addr[i] == 0)
2333 lp->rx_dma_addr[i] =
2334 pci_map_single(lp->pci_dev, rx_skbuff->data,
2335 PKT_BUF_SZ - 2, PCI_DMA_FROMDEVICE);
2336 lp->rx_ring[i].base = (u32) le32_to_cpu(lp->rx_dma_addr[i]);
2337 lp->rx_ring[i].buf_length = le16_to_cpu(2 - PKT_BUF_SZ);
2338 wmb(); /* Make sure owner changes after all others are visible */
2339 lp->rx_ring[i].status = le16_to_cpu(0x8000);
2340 }
2341 /* The Tx buffer address is filled in as needed, but we do need to clear
2342 * the upper ownership bit. */
2343 for (i = 0; i < lp->tx_ring_size; i++) {
2344 lp->tx_ring[i].status = 0; /* CPU owns buffer */
2345 wmb(); /* Make sure adapter sees owner change */
2346 lp->tx_ring[i].base = 0;
2347 lp->tx_dma_addr[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
2349
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002350 lp->init_block.tlen_rlen =
2351 le16_to_cpu(lp->tx_len_bits | lp->rx_len_bits);
2352 for (i = 0; i < 6; i++)
2353 lp->init_block.phys_addr[i] = dev->dev_addr[i];
2354 lp->init_block.rx_ring = (u32) le32_to_cpu(lp->rx_ring_dma_addr);
2355 lp->init_block.tx_ring = (u32) le32_to_cpu(lp->tx_ring_dma_addr);
2356 wmb(); /* Make sure all changes are visible */
2357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358}
2359
2360/* the pcnet32 has been issued a stop or reset. Wait for the stop bit
2361 * then flush the pending transmit operations, re-initialize the ring,
2362 * and tell the chip to initialize.
2363 */
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002364static void pcnet32_restart(struct net_device *dev, unsigned int csr0_bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002366 struct pcnet32_private *lp = dev->priv;
2367 unsigned long ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002370 /* wait for stop */
2371 for (i = 0; i < 100; i++)
Don Fryb368a3f2006-09-13 10:16:07 -07002372 if (lp->a.read_csr(ioaddr, CSR0) & CSR0_STOP)
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002373 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002375 if (i >= 100 && netif_msg_drv(lp))
2376 printk(KERN_ERR
2377 "%s: pcnet32_restart timed out waiting for stop.\n",
2378 dev->name);
2379
2380 pcnet32_purge_tx_ring(dev);
2381 if (pcnet32_init_ring(dev))
2382 return;
2383
2384 /* ReInit Ring */
Don Fryb368a3f2006-09-13 10:16:07 -07002385 lp->a.write_csr(ioaddr, CSR0, CSR0_INIT);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002386 i = 0;
2387 while (i++ < 1000)
Don Fryb368a3f2006-09-13 10:16:07 -07002388 if (lp->a.read_csr(ioaddr, CSR0) & CSR0_IDON)
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002389 break;
2390
Don Fryb368a3f2006-09-13 10:16:07 -07002391 lp->a.write_csr(ioaddr, CSR0, csr0_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392}
2393
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002394static void pcnet32_tx_timeout(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002396 struct pcnet32_private *lp = dev->priv;
2397 unsigned long ioaddr = dev->base_addr, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002399 spin_lock_irqsave(&lp->lock, flags);
2400 /* Transmitter timeout, serious problems. */
2401 if (pcnet32_debug & NETIF_MSG_DRV)
2402 printk(KERN_ERR
2403 "%s: transmit timed out, status %4.4x, resetting.\n",
Don Fryb368a3f2006-09-13 10:16:07 -07002404 dev->name, lp->a.read_csr(ioaddr, CSR0));
2405 lp->a.write_csr(ioaddr, CSR0, CSR0_STOP);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002406 lp->stats.tx_errors++;
2407 if (netif_msg_tx_err(lp)) {
2408 int i;
2409 printk(KERN_DEBUG
2410 " Ring data dump: dirty_tx %d cur_tx %d%s cur_rx %d.",
2411 lp->dirty_tx, lp->cur_tx, lp->tx_full ? " (full)" : "",
2412 lp->cur_rx);
2413 for (i = 0; i < lp->rx_ring_size; i++)
2414 printk("%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
2415 le32_to_cpu(lp->rx_ring[i].base),
2416 (-le16_to_cpu(lp->rx_ring[i].buf_length)) &
2417 0xffff, le32_to_cpu(lp->rx_ring[i].msg_length),
2418 le16_to_cpu(lp->rx_ring[i].status));
2419 for (i = 0; i < lp->tx_ring_size; i++)
2420 printk("%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
2421 le32_to_cpu(lp->tx_ring[i].base),
2422 (-le16_to_cpu(lp->tx_ring[i].length)) & 0xffff,
2423 le32_to_cpu(lp->tx_ring[i].misc),
2424 le16_to_cpu(lp->tx_ring[i].status));
2425 printk("\n");
2426 }
Don Fryb368a3f2006-09-13 10:16:07 -07002427 pcnet32_restart(dev, CSR0_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002429 dev->trans_start = jiffies;
2430 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002432 spin_unlock_irqrestore(&lp->lock, flags);
2433}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002435static int pcnet32_start_xmit(struct sk_buff *skb, struct net_device *dev)
2436{
2437 struct pcnet32_private *lp = dev->priv;
2438 unsigned long ioaddr = dev->base_addr;
2439 u16 status;
2440 int entry;
2441 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002443 spin_lock_irqsave(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002445 if (netif_msg_tx_queued(lp)) {
2446 printk(KERN_DEBUG
2447 "%s: pcnet32_start_xmit() called, csr0 %4.4x.\n",
Don Fryb368a3f2006-09-13 10:16:07 -07002448 dev->name, lp->a.read_csr(ioaddr, CSR0));
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002451 /* Default status -- will not enable Successful-TxDone
2452 * interrupt when that option is available to us.
2453 */
2454 status = 0x8300;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002456 /* Fill in a Tx ring entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002458 /* Mask to ring buffer boundary. */
2459 entry = lp->cur_tx & lp->tx_mod_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002461 /* Caution: the write order is important here, set the status
2462 * with the "ownership" bits last. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002464 lp->tx_ring[entry].length = le16_to_cpu(-skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002466 lp->tx_ring[entry].misc = 0x00000000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002468 lp->tx_skbuff[entry] = skb;
2469 lp->tx_dma_addr[entry] =
2470 pci_map_single(lp->pci_dev, skb->data, skb->len, PCI_DMA_TODEVICE);
2471 lp->tx_ring[entry].base = (u32) le32_to_cpu(lp->tx_dma_addr[entry]);
2472 wmb(); /* Make sure owner changes after all others are visible */
2473 lp->tx_ring[entry].status = le16_to_cpu(status);
2474
2475 lp->cur_tx++;
2476 lp->stats.tx_bytes += skb->len;
2477
2478 /* Trigger an immediate send poll. */
Don Fryb368a3f2006-09-13 10:16:07 -07002479 lp->a.write_csr(ioaddr, CSR0, CSR0_INTEN | CSR0_TXPOLL);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002480
2481 dev->trans_start = jiffies;
2482
2483 if (lp->tx_ring[(entry + 1) & lp->tx_mod_mask].base != 0) {
2484 lp->tx_full = 1;
2485 netif_stop_queue(dev);
2486 }
2487 spin_unlock_irqrestore(&lp->lock, flags);
2488 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489}
2490
2491/* The PCNET32 interrupt handler. */
2492static irqreturn_t
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002493pcnet32_interrupt(int irq, void *dev_id, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002495 struct net_device *dev = dev_id;
2496 struct pcnet32_private *lp;
2497 unsigned long ioaddr;
Don Fry5c993462006-09-13 10:15:43 -07002498 u16 csr0;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002499 int boguscnt = max_interrupt_work;
2500 int must_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002502 if (!dev) {
2503 if (pcnet32_debug & NETIF_MSG_INTR)
2504 printk(KERN_DEBUG "%s(): irq %d for unknown device\n",
2505 __FUNCTION__, irq);
2506 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002509 ioaddr = dev->base_addr;
2510 lp = dev->priv;
2511
2512 spin_lock(&lp->lock);
2513
Don Fry3904c322006-09-13 10:16:38 -07002514 csr0 = lp->a.read_csr(ioaddr, CSR0);
2515 while ((csr0 & 0x8f00) && --boguscnt >= 0) {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002516 if (csr0 == 0xffff) {
2517 break; /* PCMCIA remove happened */
2518 }
2519 /* Acknowledge all of the current interrupt sources ASAP. */
Don Fry3904c322006-09-13 10:16:38 -07002520 lp->a.write_csr(ioaddr, CSR0, csr0 & ~0x004f);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002521
2522 must_restart = 0;
2523
2524 if (netif_msg_intr(lp))
2525 printk(KERN_DEBUG
2526 "%s: interrupt csr0=%#2.2x new csr=%#2.2x.\n",
Don Fry3904c322006-09-13 10:16:38 -07002527 dev->name, csr0, lp->a.read_csr(ioaddr, CSR0));
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002528
2529 if (csr0 & 0x0400) /* Rx interrupt */
2530 pcnet32_rx(dev);
2531
2532 if (csr0 & 0x0200) { /* Tx-done interrupt */
Don Fry9691edd2006-09-13 10:16:21 -07002533 must_restart = pcnet32_tx(dev, csr0);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002534 }
2535
2536 /* Log misc errors. */
2537 if (csr0 & 0x4000)
2538 lp->stats.tx_errors++; /* Tx babble. */
2539 if (csr0 & 0x1000) {
2540 /*
Don Fry3904c322006-09-13 10:16:38 -07002541 * This happens when our receive ring is full. This
2542 * shouldn't be a problem as we will see normal rx
2543 * interrupts for the frames in the receive ring. But
2544 * there are some PCI chipsets (I can reproduce this
2545 * on SP3G with Intel saturn chipset) which have
2546 * sometimes problems and will fill up the receive
2547 * ring with error descriptors. In this situation we
2548 * don't get a rx interrupt, but a missed frame
2549 * interrupt sooner or later. So we try to clean up
2550 * our receive ring here.
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002551 */
2552 pcnet32_rx(dev);
2553 lp->stats.rx_errors++; /* Missed a Rx frame. */
2554 }
2555 if (csr0 & 0x0800) {
2556 if (netif_msg_drv(lp))
2557 printk(KERN_ERR
2558 "%s: Bus master arbitration failure, status %4.4x.\n",
2559 dev->name, csr0);
2560 /* unlike for the lance, there is no restart needed */
2561 }
2562
2563 if (must_restart) {
2564 /* reset the chip to clear the error condition, then restart */
2565 lp->a.reset(ioaddr);
Don Fryb368a3f2006-09-13 10:16:07 -07002566 lp->a.write_csr(ioaddr, CSR4, 0x0915); /* auto tx pad */
2567 pcnet32_restart(dev, CSR0_START);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002568 netif_wake_queue(dev);
2569 }
Don Fry3904c322006-09-13 10:16:38 -07002570 csr0 = lp->a.read_csr(ioaddr, CSR0);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002571 }
2572
2573 /* Set interrupt enable. */
Don Fryb368a3f2006-09-13 10:16:07 -07002574 lp->a.write_csr(ioaddr, CSR0, CSR0_INTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
2576 if (netif_msg_intr(lp))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002577 printk(KERN_DEBUG "%s: exiting interrupt, csr0=%#4.4x.\n",
Don Fryb368a3f2006-09-13 10:16:07 -07002578 dev->name, lp->a.read_csr(ioaddr, CSR0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002580 spin_unlock(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002582 return IRQ_HANDLED;
2583}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002585static int pcnet32_close(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002587 unsigned long ioaddr = dev->base_addr;
2588 struct pcnet32_private *lp = dev->priv;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002589 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002591 del_timer_sync(&lp->watchdog_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002593 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002595 spin_lock_irqsave(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002597 lp->stats.rx_missed_errors = lp->a.read_csr(ioaddr, 112);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002599 if (netif_msg_ifdown(lp))
2600 printk(KERN_DEBUG
2601 "%s: Shutting down ethercard, status was %2.2x.\n",
Don Fryb368a3f2006-09-13 10:16:07 -07002602 dev->name, lp->a.read_csr(ioaddr, CSR0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002604 /* We stop the PCNET32 here -- it occasionally polls memory if we don't. */
Don Fryb368a3f2006-09-13 10:16:07 -07002605 lp->a.write_csr(ioaddr, CSR0, CSR0_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 /*
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002608 * Switch back to 16bit mode to avoid problems with dumb
2609 * DOS packet driver after a warm reboot
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 */
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002611 lp->a.write_bcr(ioaddr, 20, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002613 spin_unlock_irqrestore(&lp->lock, flags);
2614
2615 free_irq(dev->irq, dev);
2616
2617 spin_lock_irqsave(&lp->lock, flags);
2618
Don Fryac5bfe42006-06-29 13:55:27 -07002619 pcnet32_purge_rx_ring(dev);
2620 pcnet32_purge_tx_ring(dev);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002621
2622 spin_unlock_irqrestore(&lp->lock, flags);
2623
2624 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625}
2626
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002627static struct net_device_stats *pcnet32_get_stats(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002629 struct pcnet32_private *lp = dev->priv;
2630 unsigned long ioaddr = dev->base_addr;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002631 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002633 spin_lock_irqsave(&lp->lock, flags);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002634 lp->stats.rx_missed_errors = lp->a.read_csr(ioaddr, 112);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002635 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002637 return &lp->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638}
2639
2640/* taken from the sunlance driver, which it took from the depca driver */
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002641static void pcnet32_load_multicast(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002643 struct pcnet32_private *lp = dev->priv;
2644 volatile struct pcnet32_init_block *ib = &lp->init_block;
2645 volatile u16 *mcast_table = (u16 *) & ib->filter;
2646 struct dev_mc_list *dmi = dev->mc_list;
Don Frydf27f4a2006-06-29 13:55:02 -07002647 unsigned long ioaddr = dev->base_addr;
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002648 char *addrs;
2649 int i;
2650 u32 crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002652 /* set all multicast bits */
2653 if (dev->flags & IFF_ALLMULTI) {
2654 ib->filter[0] = 0xffffffff;
2655 ib->filter[1] = 0xffffffff;
Don Frydf27f4a2006-06-29 13:55:02 -07002656 lp->a.write_csr(ioaddr, PCNET32_MC_FILTER, 0xffff);
2657 lp->a.write_csr(ioaddr, PCNET32_MC_FILTER+1, 0xffff);
2658 lp->a.write_csr(ioaddr, PCNET32_MC_FILTER+2, 0xffff);
2659 lp->a.write_csr(ioaddr, PCNET32_MC_FILTER+3, 0xffff);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002660 return;
2661 }
2662 /* clear the multicast filter */
2663 ib->filter[0] = 0;
2664 ib->filter[1] = 0;
2665
2666 /* Add addresses */
2667 for (i = 0; i < dev->mc_count; i++) {
2668 addrs = dmi->dmi_addr;
2669 dmi = dmi->next;
2670
2671 /* multicast address? */
2672 if (!(*addrs & 1))
2673 continue;
2674
2675 crc = ether_crc_le(6, addrs);
2676 crc = crc >> 26;
2677 mcast_table[crc >> 4] =
2678 le16_to_cpu(le16_to_cpu(mcast_table[crc >> 4]) |
2679 (1 << (crc & 0xf)));
2680 }
Don Frydf27f4a2006-06-29 13:55:02 -07002681 for (i = 0; i < 4; i++)
2682 lp->a.write_csr(ioaddr, PCNET32_MC_FILTER + i,
2683 le16_to_cpu(mcast_table[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685}
2686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687/*
2688 * Set or clear the multicast filter for this adaptor.
2689 */
2690static void pcnet32_set_multicast_list(struct net_device *dev)
2691{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002692 unsigned long ioaddr = dev->base_addr, flags;
2693 struct pcnet32_private *lp = dev->priv;
Don Frydf27f4a2006-06-29 13:55:02 -07002694 int csr15, suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002696 spin_lock_irqsave(&lp->lock, flags);
Don Frydf27f4a2006-06-29 13:55:02 -07002697 suspended = pcnet32_suspend(dev, &flags, 0);
2698 csr15 = lp->a.read_csr(ioaddr, CSR15);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002699 if (dev->flags & IFF_PROMISC) {
2700 /* Log any net taps. */
2701 if (netif_msg_hw(lp))
2702 printk(KERN_INFO "%s: Promiscuous mode enabled.\n",
2703 dev->name);
2704 lp->init_block.mode =
2705 le16_to_cpu(0x8000 | (lp->options & PCNET32_PORT_PORTSEL) <<
2706 7);
Don Frydf27f4a2006-06-29 13:55:02 -07002707 lp->a.write_csr(ioaddr, CSR15, csr15 | 0x8000);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002708 } else {
2709 lp->init_block.mode =
2710 le16_to_cpu((lp->options & PCNET32_PORT_PORTSEL) << 7);
Don Frydf27f4a2006-06-29 13:55:02 -07002711 lp->a.write_csr(ioaddr, CSR15, csr15 & 0x7fff);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002712 pcnet32_load_multicast(dev);
2713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Don Frydf27f4a2006-06-29 13:55:02 -07002715 if (suspended) {
2716 int csr5;
2717 /* clear SUSPEND (SPND) - CSR5 bit 0 */
2718 csr5 = lp->a.read_csr(ioaddr, CSR5);
2719 lp->a.write_csr(ioaddr, CSR5, csr5 & (~CSR5_SUSPEND));
Don Fryb368a3f2006-09-13 10:16:07 -07002720 } else {
Don Frydf27f4a2006-06-29 13:55:02 -07002721 lp->a.write_csr(ioaddr, CSR0, CSR0_STOP);
2722 pcnet32_restart(dev, CSR0_NORMAL);
2723 netif_wake_queue(dev);
2724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002726 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727}
2728
2729/* This routine assumes that the lp->lock is held */
2730static int mdio_read(struct net_device *dev, int phy_id, int reg_num)
2731{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002732 struct pcnet32_private *lp = dev->priv;
2733 unsigned long ioaddr = dev->base_addr;
2734 u16 val_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002736 if (!lp->mii)
2737 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002739 lp->a.write_bcr(ioaddr, 33, ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
2740 val_out = lp->a.read_bcr(ioaddr, 34);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002742 return val_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743}
2744
2745/* This routine assumes that the lp->lock is held */
2746static void mdio_write(struct net_device *dev, int phy_id, int reg_num, int val)
2747{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002748 struct pcnet32_private *lp = dev->priv;
2749 unsigned long ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002751 if (!lp->mii)
2752 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002754 lp->a.write_bcr(ioaddr, 33, ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
2755 lp->a.write_bcr(ioaddr, 34, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756}
2757
2758static int pcnet32_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2759{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002760 struct pcnet32_private *lp = dev->priv;
2761 int rc;
2762 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002764 /* SIOC[GS]MIIxxx ioctls */
2765 if (lp->mii) {
2766 spin_lock_irqsave(&lp->lock, flags);
2767 rc = generic_mii_ioctl(&lp->mii_if, if_mii(rq), cmd, NULL);
2768 spin_unlock_irqrestore(&lp->lock, flags);
2769 } else {
2770 rc = -EOPNOTSUPP;
2771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002773 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774}
2775
Don Fryac62ef02006-03-20 15:26:03 -08002776static int pcnet32_check_otherphy(struct net_device *dev)
2777{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002778 struct pcnet32_private *lp = dev->priv;
2779 struct mii_if_info mii = lp->mii_if;
2780 u16 bmcr;
2781 int i;
Don Fryac62ef02006-03-20 15:26:03 -08002782
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002783 for (i = 0; i < PCNET32_MAX_PHYS; i++) {
2784 if (i == lp->mii_if.phy_id)
2785 continue; /* skip active phy */
2786 if (lp->phymask & (1 << i)) {
2787 mii.phy_id = i;
2788 if (mii_link_ok(&mii)) {
2789 /* found PHY with active link */
2790 if (netif_msg_link(lp))
2791 printk(KERN_INFO
2792 "%s: Using PHY number %d.\n",
2793 dev->name, i);
Don Fryac62ef02006-03-20 15:26:03 -08002794
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002795 /* isolate inactive phy */
2796 bmcr =
2797 mdio_read(dev, lp->mii_if.phy_id, MII_BMCR);
2798 mdio_write(dev, lp->mii_if.phy_id, MII_BMCR,
2799 bmcr | BMCR_ISOLATE);
Don Fryac62ef02006-03-20 15:26:03 -08002800
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002801 /* de-isolate new phy */
2802 bmcr = mdio_read(dev, i, MII_BMCR);
2803 mdio_write(dev, i, MII_BMCR,
2804 bmcr & ~BMCR_ISOLATE);
Don Fryac62ef02006-03-20 15:26:03 -08002805
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002806 /* set new phy address */
2807 lp->mii_if.phy_id = i;
2808 return 1;
2809 }
2810 }
Don Fryac62ef02006-03-20 15:26:03 -08002811 }
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002812 return 0;
Don Fryac62ef02006-03-20 15:26:03 -08002813}
2814
2815/*
2816 * Show the status of the media. Similar to mii_check_media however it
2817 * correctly shows the link speed for all (tested) pcnet32 variants.
2818 * Devices with no mii just report link state without speed.
2819 *
2820 * Caller is assumed to hold and release the lp->lock.
2821 */
2822
2823static void pcnet32_check_media(struct net_device *dev, int verbose)
2824{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002825 struct pcnet32_private *lp = dev->priv;
2826 int curr_link;
2827 int prev_link = netif_carrier_ok(dev) ? 1 : 0;
2828 u32 bcr9;
Don Fryac62ef02006-03-20 15:26:03 -08002829
Don Fryac62ef02006-03-20 15:26:03 -08002830 if (lp->mii) {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002831 curr_link = mii_link_ok(&lp->mii_if);
Don Fryac62ef02006-03-20 15:26:03 -08002832 } else {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002833 ulong ioaddr = dev->base_addr; /* card base I/O address */
2834 curr_link = (lp->a.read_bcr(ioaddr, 4) != 0xc0);
Don Fryac62ef02006-03-20 15:26:03 -08002835 }
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002836 if (!curr_link) {
2837 if (prev_link || verbose) {
2838 netif_carrier_off(dev);
2839 if (netif_msg_link(lp))
2840 printk(KERN_INFO "%s: link down\n", dev->name);
2841 }
2842 if (lp->phycount > 1) {
2843 curr_link = pcnet32_check_otherphy(dev);
2844 prev_link = 0;
2845 }
2846 } else if (verbose || !prev_link) {
2847 netif_carrier_on(dev);
2848 if (lp->mii) {
2849 if (netif_msg_link(lp)) {
2850 struct ethtool_cmd ecmd;
2851 mii_ethtool_gset(&lp->mii_if, &ecmd);
2852 printk(KERN_INFO
2853 "%s: link up, %sMbps, %s-duplex\n",
2854 dev->name,
2855 (ecmd.speed == SPEED_100) ? "100" : "10",
2856 (ecmd.duplex ==
2857 DUPLEX_FULL) ? "full" : "half");
2858 }
2859 bcr9 = lp->a.read_bcr(dev->base_addr, 9);
2860 if ((bcr9 & (1 << 0)) != lp->mii_if.full_duplex) {
2861 if (lp->mii_if.full_duplex)
2862 bcr9 |= (1 << 0);
2863 else
2864 bcr9 &= ~(1 << 0);
2865 lp->a.write_bcr(dev->base_addr, 9, bcr9);
2866 }
2867 } else {
2868 if (netif_msg_link(lp))
2869 printk(KERN_INFO "%s: link up\n", dev->name);
2870 }
2871 }
Don Fryac62ef02006-03-20 15:26:03 -08002872}
2873
2874/*
2875 * Check for loss of link and link establishment.
2876 * Can not use mii_check_media because it does nothing if mode is forced.
2877 */
2878
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879static void pcnet32_watchdog(struct net_device *dev)
2880{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002881 struct pcnet32_private *lp = dev->priv;
2882 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002884 /* Print the link status if it has changed */
2885 spin_lock_irqsave(&lp->lock, flags);
2886 pcnet32_check_media(dev, 0);
2887 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002889 mod_timer(&(lp->watchdog_timer), PCNET32_WATCHDOG_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890}
2891
2892static void __devexit pcnet32_remove_one(struct pci_dev *pdev)
2893{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002894 struct net_device *dev = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002896 if (dev) {
2897 struct pcnet32_private *lp = dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002899 unregister_netdev(dev);
2900 pcnet32_free_ring(dev);
2901 release_region(dev->base_addr, PCNET32_TOTAL_SIZE);
2902 pci_free_consistent(lp->pci_dev, sizeof(*lp), lp, lp->dma_addr);
2903 free_netdev(dev);
2904 pci_disable_device(pdev);
2905 pci_set_drvdata(pdev, NULL);
2906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907}
2908
2909static struct pci_driver pcnet32_driver = {
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002910 .name = DRV_NAME,
2911 .probe = pcnet32_probe_pci,
2912 .remove = __devexit_p(pcnet32_remove_one),
2913 .id_table = pcnet32_pci_tbl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914};
2915
2916/* An additional parameter that may be passed in... */
2917static int debug = -1;
2918static int tx_start_pt = -1;
2919static int pcnet32_have_pci;
2920
2921module_param(debug, int, 0);
2922MODULE_PARM_DESC(debug, DRV_NAME " debug level");
2923module_param(max_interrupt_work, int, 0);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002924MODULE_PARM_DESC(max_interrupt_work,
2925 DRV_NAME " maximum events handled per interrupt");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926module_param(rx_copybreak, int, 0);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002927MODULE_PARM_DESC(rx_copybreak,
2928 DRV_NAME " copy breakpoint for copy-only-tiny-frames");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929module_param(tx_start_pt, int, 0);
2930MODULE_PARM_DESC(tx_start_pt, DRV_NAME " transmit start point (0-3)");
2931module_param(pcnet32vlb, int, 0);
2932MODULE_PARM_DESC(pcnet32vlb, DRV_NAME " Vesa local bus (VLB) support (0/1)");
2933module_param_array(options, int, NULL, 0);
2934MODULE_PARM_DESC(options, DRV_NAME " initial option setting(s) (0-15)");
2935module_param_array(full_duplex, int, NULL, 0);
2936MODULE_PARM_DESC(full_duplex, DRV_NAME " full duplex setting(s) (1)");
2937/* Module Parameter for HomePNA cards added by Patrick Simmons, 2004 */
2938module_param_array(homepna, int, NULL, 0);
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002939MODULE_PARM_DESC(homepna,
2940 DRV_NAME
2941 " mode for 79C978 cards (1 for HomePNA, 0 for Ethernet, default Ethernet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
2943MODULE_AUTHOR("Thomas Bogendoerfer");
2944MODULE_DESCRIPTION("Driver for PCnet32 and PCnetPCI based ethercards");
2945MODULE_LICENSE("GPL");
2946
2947#define PCNET32_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
2948
2949static int __init pcnet32_init_module(void)
2950{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002951 printk(KERN_INFO "%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002953 pcnet32_debug = netif_msg_init(debug, PCNET32_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002955 if ((tx_start_pt >= 0) && (tx_start_pt <= 3))
2956 tx_start = tx_start_pt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002958 /* find the PCI devices */
Jeff Garzik29917622006-08-19 17:48:59 -04002959 if (!pci_register_driver(&pcnet32_driver))
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002960 pcnet32_have_pci = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002962 /* should we find any remaining VLbus devices ? */
2963 if (pcnet32vlb)
Don Frydcaf9762006-06-29 13:52:29 -07002964 pcnet32_probe_vlbus(pcnet32_portlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002966 if (cards_found && (pcnet32_debug & NETIF_MSG_PROBE))
2967 printk(KERN_INFO PFX "%d cards_found.\n", cards_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002969 return (pcnet32_have_pci + cards_found) ? 0 : -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970}
2971
2972static void __exit pcnet32_cleanup_module(void)
2973{
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002974 struct net_device *next_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002976 while (pcnet32_dev) {
2977 struct pcnet32_private *lp = pcnet32_dev->priv;
2978 next_dev = lp->next;
2979 unregister_netdev(pcnet32_dev);
2980 pcnet32_free_ring(pcnet32_dev);
2981 release_region(pcnet32_dev->base_addr, PCNET32_TOTAL_SIZE);
2982 pci_free_consistent(lp->pci_dev, sizeof(*lp), lp, lp->dma_addr);
2983 free_netdev(pcnet32_dev);
2984 pcnet32_dev = next_dev;
2985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Jeff Garzik4a5e8e22006-03-21 16:15:44 -05002987 if (pcnet32_have_pci)
2988 pci_unregister_driver(&pcnet32_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989}
2990
2991module_init(pcnet32_init_module);
2992module_exit(pcnet32_cleanup_module);
2993
2994/*
2995 * Local variables:
2996 * c-indent-level: 4
2997 * tab-width: 8
2998 * End:
2999 */