Blame view

drivers/net/epic100.c 45 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  /* epic100.c: A SMC 83c170 EPIC/100 Fast Ethernet driver for Linux. */
  /*
  	Written/copyright 1997-2001 by Donald Becker.
  
  	This software may be used and distributed according to the terms of
  	the GNU General Public License (GPL), incorporated herein by reference.
  	Drivers based on or derived from this code fall under the GPL and must
  	retain the authorship, copyright and license notice.  This file is not
  	a complete program and may only be used when the entire operating
  	system is licensed under the GPL.
  
  	This driver is for the SMC83c170/175 "EPIC" series, as used on the
  	SMC EtherPower II 9432 PCI adapter, and several CardBus cards.
  
  	The author may be reached as becker@scyld.com, or C/O
  	Scyld Computing Corporation
  	410 Severn Ave., Suite 210
  	Annapolis MD 21403
  
  	Information and updates available at
  	http://www.scyld.com/network/epic100.html
36e1e8476   Jeff Garzik   [netdrvr] epic100...
22
  	[this link no longer provides anything useful -jgarzik]
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
24
  
  	---------------------------------------------------------------------
f3b197ac2   Jeff Garzik   [netdrvr] trim tr...
25

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
27
28
  */
  
  #define DRV_NAME        "epic100"
d5b20697c   Andy Gospodarek   [PATCH] Remove mo...
29
30
  #define DRV_VERSION     "2.1"
  #define DRV_RELDATE     "Sept 11, 2006"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  
  /* The user-configurable values.
     These may be modified when a driver module is loaded.*/
  
  static int debug = 1;			/* 1 normal messages, 0 quiet .. 7 verbose. */
  
  /* Used to pass the full-duplex flag, etc. */
  #define MAX_UNITS 8		/* More are supported, limit only on options */
  static int options[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
  static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
  
  /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
     Setting to > 1518 effectively disables this feature. */
  static int rx_copybreak;
  
  /* Operational parameters that are set at compile time. */
  
  /* Keep the ring sizes a power of two for operational efficiency.
     The compiler will convert <unsigned>'%'<2^N> into a bit mask.
     Making the Tx ring too large decreases the effectiveness of channel
     bonding and packet priority.
     There are no ill effects from too-large receive rings. */
  #define TX_RING_SIZE	256
  #define TX_QUEUE_LEN	240		/* Limit ring entries actually used.  */
  #define RX_RING_SIZE	256
  #define TX_TOTAL_SIZE	TX_RING_SIZE*sizeof(struct epic_tx_desc)
  #define RX_TOTAL_SIZE	RX_RING_SIZE*sizeof(struct epic_rx_desc)
  
  /* Operational parameters that usually are not changed. */
  /* Time in jiffies before concluding the transmitter is hung. */
  #define TX_TIMEOUT  (2*HZ)
  
  #define PKT_BUF_SZ		1536			/* Size of each temporary Rx buffer.*/
  
  /* Bytes transferred to chip before transmission starts. */
  /* Initial threshold, increased on underflow, rounded down to 4 byte units. */
  #define TX_FIFO_THRESH 256
  #define RX_FIFO_THRESH 1		/* 0-3, 0==32, 64,96, or 3==128 bytes  */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
69
70
71
72
73
74
  #include <linux/module.h>
  #include <linux/kernel.h>
  #include <linux/string.h>
  #include <linux/timer.h>
  #include <linux/errno.h>
  #include <linux/ioport.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  #include <linux/interrupt.h>
  #include <linux/pci.h>
  #include <linux/delay.h>
  #include <linux/netdevice.h>
  #include <linux/etherdevice.h>
  #include <linux/skbuff.h>
  #include <linux/init.h>
  #include <linux/spinlock.h>
  #include <linux/ethtool.h>
  #include <linux/mii.h>
  #include <linux/crc32.h>
  #include <linux/bitops.h>
  #include <asm/io.h>
  #include <asm/uaccess.h>
9e2d11b92   Roland Dreier   epic100: Test __B...
89
  #include <asm/byteorder.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
90
91
92
93
94
95
  
  /* These identify the driver base version and may not be removed. */
  static char version[] __devinitdata =
  DRV_NAME ".c:v1.11 1/7/2001 Written by Donald Becker <becker@scyld.com>
  ";
  static char version2[] __devinitdata =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
  "  (unofficial 2.4.x kernel port, version " DRV_VERSION ", " DRV_RELDATE ")
  ";
  
  MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
  MODULE_DESCRIPTION("SMC 83c170 EPIC series Ethernet driver");
  MODULE_LICENSE("GPL");
  
  module_param(debug, int, 0);
  module_param(rx_copybreak, int, 0);
  module_param_array(options, int, NULL, 0);
  module_param_array(full_duplex, int, NULL, 0);
  MODULE_PARM_DESC(debug, "EPIC/100 debug level (0-5)");
  MODULE_PARM_DESC(options, "EPIC/100: Bits 0-3: media type, bit 4: full duplex");
  MODULE_PARM_DESC(rx_copybreak, "EPIC/100 copy breakpoint for copy-only-tiny-frames");
  MODULE_PARM_DESC(full_duplex, "EPIC/100 full duplex setting(s) (1)");
  
  /*
  				Theory of Operation
  
  I. Board Compatibility
  
  This device driver is designed for the SMC "EPIC/100", the SMC
  single-chip Ethernet controllers for PCI.  This chip is used on
  the SMC EtherPower II boards.
  
  II. Board-specific settings
  
  PCI bus devices are configured by the system at boot time, so no jumpers
  need to be set on the board.  The system BIOS will assign the
  PCI INTA signal to a (preferably otherwise unused) system IRQ line.
  Note: Kernel versions earlier than 1.3.73 do not support shared PCI
  interrupt lines.
  
  III. Driver operation
  
  IIIa. Ring buffers
  
  IVb. References
631dd1a88   Justin P. Mattock   Update broken web...
134
135
  http://www.smsc.com/media/Downloads_Public/discontinued/83c171.pdf
  http://www.smsc.com/media/Downloads_Public/discontinued/83c175.pdf
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136
137
138
139
140
141
  http://scyld.com/expert/NWay.html
  http://www.national.com/pf/DP/DP83840A.html
  
  IVc. Errata
  
  */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
142
143
144
145
  enum chip_capability_flags { MII_PWRDWN=1, TYPE2_INTR=2, NO_MII=4 };
  
  #define EPIC_TOTAL_SIZE 0x100
  #define USE_IO_OPS 1
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
146
147
148
149
150
151
152
153
154
155
  
  typedef enum {
  	SMSC_83C170_0,
  	SMSC_83C170,
  	SMSC_83C175,
  } chip_t;
  
  
  struct epic_chip_info {
  	const char *name;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
157
158
159
160
          int drv_flags;                          /* Driver use, intended as capability flags. */
  };
  
  
  /* indexed by chip_t */
f71e13096   Arjan van de Ven   Massive net drive...
161
  static const struct epic_chip_info pci_id_tbl[] = {
36e1e8476   Jeff Garzik   [netdrvr] epic100...
162
163
164
  	{ "SMSC EPIC/100 83c170",	TYPE2_INTR | NO_MII | MII_PWRDWN },
  	{ "SMSC EPIC/100 83c170",	TYPE2_INTR },
  	{ "SMSC EPIC/C 83c175",		TYPE2_INTR | MII_PWRDWN },
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
165
  };
a3aa18842   Alexey Dobriyan   drivers/net/: use...
166
  static DEFINE_PCI_DEVICE_TABLE(epic_pci_tbl) = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
167
168
169
170
171
172
173
  	{ 0x10B8, 0x0005, 0x1092, 0x0AB4, 0, 0, SMSC_83C170_0 },
  	{ 0x10B8, 0x0005, PCI_ANY_ID, PCI_ANY_ID, 0, 0, SMSC_83C170 },
  	{ 0x10B8, 0x0006, PCI_ANY_ID, PCI_ANY_ID,
  	  PCI_CLASS_NETWORK_ETHERNET << 8, 0xffff00, SMSC_83C175 },
  	{ 0,}
  };
  MODULE_DEVICE_TABLE (pci, epic_pci_tbl);
f3b197ac2   Jeff Garzik   [netdrvr] trim tr...
174

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
  #ifndef USE_IO_OPS
  #undef inb
  #undef inw
  #undef inl
  #undef outb
  #undef outw
  #undef outl
  #define inb readb
  #define inw readw
  #define inl readl
  #define outb writeb
  #define outw writew
  #define outl writel
  #endif
  
  /* Offsets to registers, using the (ugh) SMC names. */
  enum epic_registers {
    COMMAND=0, INTSTAT=4, INTMASK=8, GENCTL=0x0C, NVCTL=0x10, EECTL=0x14,
    PCIBurstCnt=0x18,
    TEST1=0x1C, CRCCNT=0x20, ALICNT=0x24, MPCNT=0x28,	/* Rx error counters. */
    MIICtrl=0x30, MIIData=0x34, MIICfg=0x38,
    LAN0=64,						/* MAC address. */
    MC0=80,						/* Multicast filter table. */
    RxCtrl=96, TxCtrl=112, TxSTAT=0x74,
    PRxCDAR=0x84, RxSTAT=0xA4, EarlyRx=0xB0, PTxCDAR=0xC4, TxThresh=0xDC,
  };
  
  /* Interrupt register bits, using my own meaningful names. */
  enum IntrStatus {
  	TxIdle=0x40000, RxIdle=0x20000, IntrSummary=0x010000,
  	PCIBusErr170=0x7000, PCIBusErr175=0x1000, PhyEvent175=0x8000,
  	RxStarted=0x0800, RxEarlyWarn=0x0400, CntFull=0x0200, TxUnderrun=0x0100,
  	TxEmpty=0x0080, TxDone=0x0020, RxError=0x0010,
  	RxOverflow=0x0008, RxFull=0x0004, RxHeader=0x0002, RxDone=0x0001,
  };
  enum CommandBits {
  	StopRx=1, StartRx=2, TxQueued=4, RxQueued=8,
  	StopTxDMA=0x20, StopRxDMA=0x40, RestartTx=0x80,
  };
  
  #define EpicRemoved	0xffffffff	/* Chip failed or removed (CardBus) */
  
  #define EpicNapiEvent	(TxEmpty | TxDone | \
  			 RxDone | RxStarted | RxEarlyWarn | RxOverflow | RxFull)
  #define EpicNormalEvent	(0x0000ffff & ~EpicNapiEvent)
f71e13096   Arjan van de Ven   Massive net drive...
220
  static const u16 media2miictl[16] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
221
222
  	0, 0x0C00, 0x0C00, 0x2000,  0x0100, 0x2100, 0, 0,
  	0, 0, 0, 0,  0, 0, 0, 0 };
9ebfd4929   Al Viro   epic100 endiannes...
223
224
225
226
  /*
   * The EPIC100 Rx and Tx buffer descriptors.  Note that these
   * really ARE host-endian; it's not a misannotation.  We tell
   * the card to byteswap them internally on big-endian hosts -
9e2d11b92   Roland Dreier   epic100: Test __B...
227
   * look for #ifdef __BIG_ENDIAN in epic_open().
9ebfd4929   Al Viro   epic100 endiannes...
228
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
  
  struct epic_tx_desc {
  	u32 txstatus;
  	u32 bufaddr;
  	u32 buflength;
  	u32 next;
  };
  
  struct epic_rx_desc {
  	u32 rxstatus;
  	u32 bufaddr;
  	u32 buflength;
  	u32 next;
  };
  
  enum desc_status_bits {
  	DescOwn=0x8000,
  };
  
  #define PRIV_ALIGN	15 	/* Required alignment mask */
  struct epic_private {
  	struct epic_rx_desc *rx_ring;
  	struct epic_tx_desc *tx_ring;
  	/* The saved address of a sent-in-place packet/buffer, for skfree(). */
  	struct sk_buff* tx_skbuff[TX_RING_SIZE];
  	/* The addresses of receive-in-place skbuffs. */
  	struct sk_buff* rx_skbuff[RX_RING_SIZE];
  
  	dma_addr_t tx_ring_dma;
  	dma_addr_t rx_ring_dma;
  
  	/* Ring pointers. */
  	spinlock_t lock;				/* Group with Tx control cache line. */
  	spinlock_t napi_lock;
bea3348ee   Stephen Hemminger   [NET]: Make NAPI ...
263
  	struct napi_struct napi;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
264
265
266
267
268
269
270
271
272
  	unsigned int reschedule_in_poll;
  	unsigned int cur_tx, dirty_tx;
  
  	unsigned int cur_rx, dirty_rx;
  	u32 irq_mask;
  	unsigned int rx_buf_sz;				/* Based on MTU+slack. */
  
  	struct pci_dev *pci_dev;			/* PCI bus location. */
  	int chip_id, chip_flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
  	struct timer_list timer;			/* Media selection timer. */
  	int tx_threshold;
  	unsigned char mc_filter[8];
  	signed char phys[4];				/* MII device addresses. */
  	u16 advertising;					/* NWay media advertisement */
  	int mii_phy_cnt;
  	struct mii_if_info mii;
  	unsigned int tx_full:1;				/* The Tx queue is full. */
  	unsigned int default_port:4;		/* Last dev->if_port value. */
  };
  
  static int epic_open(struct net_device *dev);
  static int read_eeprom(long ioaddr, int location);
  static int mdio_read(struct net_device *dev, int phy_id, int location);
  static void mdio_write(struct net_device *dev, int phy_id, int loc, int val);
  static void epic_restart(struct net_device *dev);
  static void epic_timer(unsigned long data);
  static void epic_tx_timeout(struct net_device *dev);
  static void epic_init_ring(struct net_device *dev);
61357325f   Stephen Hemminger   netdev: convert b...
292
293
  static netdev_tx_t epic_start_xmit(struct sk_buff *skb,
  				   struct net_device *dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
294
  static int epic_rx(struct net_device *dev, int budget);
bea3348ee   Stephen Hemminger   [NET]: Make NAPI ...
295
  static int epic_poll(struct napi_struct *napi, int budget);
7d12e780e   David Howells   IRQ: Maintain reg...
296
  static irqreturn_t epic_interrupt(int irq, void *dev_instance);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
297
  static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
7282d491e   Jeff Garzik   drivers/net: cons...
298
  static const struct ethtool_ops netdev_ethtool_ops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
299
300
301
  static int epic_close(struct net_device *dev);
  static struct net_device_stats *epic_get_stats(struct net_device *dev);
  static void set_rx_mode(struct net_device *dev);
805524cbc   Stephen Hemminger   epic100: update t...
302
303
304
305
306
307
308
309
310
311
312
313
  static const struct net_device_ops epic_netdev_ops = {
  	.ndo_open		= epic_open,
  	.ndo_stop		= epic_close,
  	.ndo_start_xmit		= epic_start_xmit,
  	.ndo_tx_timeout 	= epic_tx_timeout,
  	.ndo_get_stats		= epic_get_stats,
  	.ndo_set_multicast_list = set_rx_mode,
  	.ndo_do_ioctl 		= netdev_ioctl,
  	.ndo_change_mtu		= eth_change_mtu,
  	.ndo_set_mac_address 	= eth_mac_addr,
  	.ndo_validate_addr	= eth_validate_addr,
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
  
  static int __devinit epic_init_one (struct pci_dev *pdev,
  				    const struct pci_device_id *ent)
  {
  	static int card_idx = -1;
  	long ioaddr;
  	int chip_idx = (int) ent->driver_data;
  	int irq;
  	struct net_device *dev;
  	struct epic_private *ep;
  	int i, ret, option = 0, duplex = 0;
  	void *ring_space;
  	dma_addr_t ring_dma;
  
  /* when built into the kernel, we only print version if device is found */
  #ifndef MODULE
  	static int printed_version;
  	if (!printed_version++)
ad361c988   Joe Perches   Remove multiple K...
332
  		printk(KERN_INFO "%s%s", version, version2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
333
  #endif
f3b197ac2   Jeff Garzik   [netdrvr] trim tr...
334

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
335
  	card_idx++;
f3b197ac2   Jeff Garzik   [netdrvr] trim tr...
336

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
337
338
339
340
  	ret = pci_enable_device(pdev);
  	if (ret)
  		goto out;
  	irq = pdev->irq;
36e1e8476   Jeff Garzik   [netdrvr] epic100...
341
  	if (pci_resource_len(pdev, 0) < EPIC_TOTAL_SIZE) {
9b91cf9da   Jeff Garzik   [netdrvr] use dev...
342
343
  		dev_err(&pdev->dev, "no PCI region space
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
344
345
346
  		ret = -ENODEV;
  		goto err_out_disable;
  	}
f3b197ac2   Jeff Garzik   [netdrvr] trim tr...
347

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
348
349
350
351
352
353
354
355
356
357
  	pci_set_master(pdev);
  
  	ret = pci_request_regions(pdev, DRV_NAME);
  	if (ret < 0)
  		goto err_out_disable;
  
  	ret = -ENOMEM;
  
  	dev = alloc_etherdev(sizeof (*ep));
  	if (!dev) {
9b91cf9da   Jeff Garzik   [netdrvr] use dev...
358
359
  		dev_err(&pdev->dev, "no memory for eth device
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
360
361
  		goto err_out_free_res;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
362
363
364
365
366
367
  	SET_NETDEV_DEV(dev, &pdev->dev);
  
  #ifdef USE_IO_OPS
  	ioaddr = pci_resource_start (pdev, 0);
  #else
  	ioaddr = pci_resource_start (pdev, 1);
275f165fa   Arjan van de Ven   pci: use pci_iore...
368
  	ioaddr = (long) pci_ioremap_bar(pdev, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
369
  	if (!ioaddr) {
9b91cf9da   Jeff Garzik   [netdrvr] use dev...
370
371
  		dev_err(&pdev->dev, "ioremap failed
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
372
373
374
375
376
  		goto err_out_free_netdev;
  	}
  #endif
  
  	pci_set_drvdata(pdev, dev);
4cf1653aa   Wang Chen   netdevice: safe c...
377
  	ep = netdev_priv(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
378
379
380
381
382
383
384
385
386
  	ep->mii.dev = dev;
  	ep->mii.mdio_read = mdio_read;
  	ep->mii.mdio_write = mdio_write;
  	ep->mii.phy_id_mask = 0x1f;
  	ep->mii.reg_num_mask = 0x1f;
  
  	ring_space = pci_alloc_consistent(pdev, TX_TOTAL_SIZE, &ring_dma);
  	if (!ring_space)
  		goto err_out_iounmap;
43d620c82   Joe Perches   drivers/net: Remo...
387
  	ep->tx_ring = ring_space;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
388
389
390
391
392
  	ep->tx_ring_dma = ring_dma;
  
  	ring_space = pci_alloc_consistent(pdev, RX_TOTAL_SIZE, &ring_dma);
  	if (!ring_space)
  		goto err_out_unmap_tx;
43d620c82   Joe Perches   drivers/net: Remo...
393
  	ep->rx_ring = ring_space;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
  	ep->rx_ring_dma = ring_dma;
  
  	if (dev->mem_start) {
  		option = dev->mem_start;
  		duplex = (dev->mem_start & 16) ? 1 : 0;
  	} else if (card_idx >= 0  &&  card_idx < MAX_UNITS) {
  		if (options[card_idx] >= 0)
  			option = options[card_idx];
  		if (full_duplex[card_idx] >= 0)
  			duplex = full_duplex[card_idx];
  	}
  
  	dev->base_addr = ioaddr;
  	dev->irq = irq;
  
  	spin_lock_init(&ep->lock);
  	spin_lock_init(&ep->napi_lock);
  	ep->reschedule_in_poll = 0;
  
  	/* Bring the chip out of low-power mode. */
  	outl(0x4200, ioaddr + GENCTL);
  	/* Magic?!  If we don't set this bit the MII interface won't work. */
  	/* This magic is documented in SMSC app note 7.15 */
  	for (i = 16; i > 0; i--)
  		outl(0x0008, ioaddr + TEST1);
  
  	/* Turn on the MII transceiver. */
  	outl(0x12, ioaddr + MIICfg);
  	if (chip_idx == 1)
  		outl((inl(ioaddr + NVCTL) & ~0x003C) | 0x4800, ioaddr + NVCTL);
  	outl(0x0200, ioaddr + GENCTL);
  
  	/* Note: the '175 does not have a serial EEPROM. */
  	for (i = 0; i < 3; i++)
9ebfd4929   Al Viro   epic100 endiannes...
428
  		((__le16 *)dev->dev_addr)[i] = cpu_to_le16(inw(ioaddr + LAN0 + i*4));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
429
430
  
  	if (debug > 2) {
2e8a538d8   Jeff Garzik   [netdrvr] Use dev...
431
432
  		dev_printk(KERN_DEBUG, &pdev->dev, "EEPROM contents:
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
433
434
435
436
437
438
439
440
441
  		for (i = 0; i < 64; i++)
  			printk(" %4.4x%s", read_eeprom(ioaddr, i),
  				   i % 16 == 15 ? "
  " : "");
  	}
  
  	ep->pci_dev = pdev;
  	ep->chip_id = chip_idx;
  	ep->chip_flags = pci_id_tbl[chip_idx].drv_flags;
f3b197ac2   Jeff Garzik   [netdrvr] trim tr...
442
  	ep->irq_mask =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
443
444
445
446
447
448
449
450
451
452
453
454
  		(ep->chip_flags & TYPE2_INTR ?  PCIBusErr175 : PCIBusErr170)
  		 | CntFull | TxUnderrun | EpicNapiEvent;
  
  	/* Find the connected MII xcvrs.
  	   Doing this in open() would allow detecting external xcvrs later, but
  	   takes much time and no cards have external MII. */
  	{
  		int phy, phy_idx = 0;
  		for (phy = 1; phy < 32 && phy_idx < sizeof(ep->phys); phy++) {
  			int mii_status = mdio_read(dev, phy, MII_BMSR);
  			if (mii_status != 0xffff  &&  mii_status != 0x0000) {
  				ep->phys[phy_idx++] = phy;
9b91cf9da   Jeff Garzik   [netdrvr] use dev...
455
  				dev_info(&pdev->dev,
2e8a538d8   Jeff Garzik   [netdrvr] Use dev...
456
457
458
459
  					"MII transceiver #%d control "
  					"%4.4x status %4.4x.
  ",
  					phy, mdio_read(dev, phy, 0), mii_status);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
460
461
462
463
464
465
  			}
  		}
  		ep->mii_phy_cnt = phy_idx;
  		if (phy_idx != 0) {
  			phy = ep->phys[0];
  			ep->mii.advertising = mdio_read(dev, phy, MII_ADVERTISE);
9b91cf9da   Jeff Garzik   [netdrvr] use dev...
466
  			dev_info(&pdev->dev,
2e8a538d8   Jeff Garzik   [netdrvr] Use dev...
467
  				"Autonegotiation advertising %4.4x link "
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
468
469
  				   "partner %4.4x.
  ",
2e8a538d8   Jeff Garzik   [netdrvr] Use dev...
470
  				   ep->mii.advertising, mdio_read(dev, phy, 5));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
471
  		} else if ( ! (ep->chip_flags & NO_MII)) {
9b91cf9da   Jeff Garzik   [netdrvr] use dev...
472
  			dev_warn(&pdev->dev,
2e8a538d8   Jeff Garzik   [netdrvr] Use dev...
473
474
  				"***WARNING***: No MII transceiver found!
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
475
476
477
478
479
480
481
482
483
484
485
486
487
488
  			/* Use the known PHY address of the EPII. */
  			ep->phys[0] = 3;
  		}
  		ep->mii.phy_id = ep->phys[0];
  	}
  
  	/* Turn off the MII xcvr (175 only!), leave the chip in low-power mode. */
  	if (ep->chip_flags & MII_PWRDWN)
  		outl(inl(ioaddr + NVCTL) & ~0x483C, ioaddr + NVCTL);
  	outl(0x0008, ioaddr + GENCTL);
  
  	/* The lower four bits are the media type. */
  	if (duplex) {
  		ep->mii.force_media = ep->mii.full_duplex = 1;
9b91cf9da   Jeff Garzik   [netdrvr] use dev...
489
490
  		dev_info(&pdev->dev, "Forced full duplex requested.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
491
492
493
494
  	}
  	dev->if_port = ep->default_port = option;
  
  	/* The Epic-specific entries in the device structure. */
805524cbc   Stephen Hemminger   epic100: update t...
495
  	dev->netdev_ops = &epic_netdev_ops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
496
497
  	dev->ethtool_ops = &netdev_ethtool_ops;
  	dev->watchdog_timeo = TX_TIMEOUT;
bea3348ee   Stephen Hemminger   [NET]: Make NAPI ...
498
  	netif_napi_add(dev, &ep->napi, epic_poll, 64);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
499
500
501
502
  
  	ret = register_netdev(dev);
  	if (ret < 0)
  		goto err_out_unmap_rx;
e174961ca   Johannes Berg   net: convert prin...
503
504
  	printk(KERN_INFO "%s: %s at %#lx, IRQ %d, %pM
  ",
0795af572   Joe Perches   [NET]: Introduce ...
505
  	       dev->name, pci_id_tbl[chip_idx].name, ioaddr, dev->irq,
e174961ca   Johannes Berg   net: convert prin...
506
  	       dev->dev_addr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
  
  out:
  	return ret;
  
  err_out_unmap_rx:
  	pci_free_consistent(pdev, RX_TOTAL_SIZE, ep->rx_ring, ep->rx_ring_dma);
  err_out_unmap_tx:
  	pci_free_consistent(pdev, TX_TOTAL_SIZE, ep->tx_ring, ep->tx_ring_dma);
  err_out_iounmap:
  #ifndef USE_IO_OPS
  	iounmap(ioaddr);
  err_out_free_netdev:
  #endif
  	free_netdev(dev);
  err_out_free_res:
  	pci_release_regions(pdev);
  err_out_disable:
  	pci_disable_device(pdev);
  	goto out;
  }
f3b197ac2   Jeff Garzik   [netdrvr] trim tr...
527

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
  /* Serial EEPROM section. */
  
  /*  EEPROM_Ctrl bits. */
  #define EE_SHIFT_CLK	0x04	/* EEPROM shift clock. */
  #define EE_CS			0x02	/* EEPROM chip select. */
  #define EE_DATA_WRITE	0x08	/* EEPROM chip data in. */
  #define EE_WRITE_0		0x01
  #define EE_WRITE_1		0x09
  #define EE_DATA_READ	0x10	/* EEPROM chip data out. */
  #define EE_ENB			(0x0001 | EE_CS)
  
  /* Delay between EEPROM clock transitions.
     This serves to flush the operation to the PCI bus.
   */
  
  #define eeprom_delay()	inl(ee_addr)
  
  /* The EEPROM commands include the alway-set leading bit. */
  #define EE_WRITE_CMD	(5 << 6)
  #define EE_READ64_CMD	(6 << 6)
  #define EE_READ256_CMD	(6 << 8)
  #define EE_ERASE_CMD	(7 << 6)
  
  static void epic_disable_int(struct net_device *dev, struct epic_private *ep)
  {
  	long ioaddr = dev->base_addr;
  
  	outl(0x00000000, ioaddr + INTMASK);
  }
  
  static inline void __epic_pci_commit(long ioaddr)
  {
  #ifndef USE_IO_OPS
  	inl(ioaddr + INTMASK);
  #endif
  }
  
  static inline void epic_napi_irq_off(struct net_device *dev,
  				     struct epic_private *ep)
  {
  	long ioaddr = dev->base_addr;
  
  	outl(ep->irq_mask & ~EpicNapiEvent, ioaddr + INTMASK);
  	__epic_pci_commit(ioaddr);
  }
  
  static inline void epic_napi_irq_on(struct net_device *dev,
  				    struct epic_private *ep)
  {
  	long ioaddr = dev->base_addr;
  
  	/* No need to commit possible posted write */
  	outl(ep->irq_mask | EpicNapiEvent, ioaddr + INTMASK);
  }
  
  static int __devinit read_eeprom(long ioaddr, int location)
  {
  	int i;
  	int retval = 0;
  	long ee_addr = ioaddr + EECTL;
  	int read_cmd = location |
  		(inl(ee_addr) & 0x40 ? EE_READ64_CMD : EE_READ256_CMD);
  
  	outl(EE_ENB & ~EE_CS, ee_addr);
  	outl(EE_ENB, ee_addr);
  
  	/* Shift the read command bits out. */
  	for (i = 12; i >= 0; i--) {
  		short dataval = (read_cmd & (1 << i)) ? EE_WRITE_1 : EE_WRITE_0;
  		outl(EE_ENB | dataval, ee_addr);
  		eeprom_delay();
  		outl(EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
  		eeprom_delay();
  	}
  	outl(EE_ENB, ee_addr);
  
  	for (i = 16; i > 0; i--) {
  		outl(EE_ENB | EE_SHIFT_CLK, ee_addr);
  		eeprom_delay();
  		retval = (retval << 1) | ((inl(ee_addr) & EE_DATA_READ) ? 1 : 0);
  		outl(EE_ENB, ee_addr);
  		eeprom_delay();
  	}
  
  	/* Terminate the EEPROM access. */
  	outl(EE_ENB & ~EE_CS, ee_addr);
  	return retval;
  }
  
  #define MII_READOP		1
  #define MII_WRITEOP		2
  static int mdio_read(struct net_device *dev, int phy_id, int location)
  {
  	long ioaddr = dev->base_addr;
  	int read_cmd = (phy_id << 9) | (location << 4) | MII_READOP;
  	int i;
  
  	outl(read_cmd, ioaddr + MIICtrl);
  	/* Typical operation takes 25 loops. */
  	for (i = 400; i > 0; i--) {
  		barrier();
  		if ((inl(ioaddr + MIICtrl) & MII_READOP) == 0) {
  			/* Work around read failure bug. */
8e95a2026   Joe Perches   drivers/net: Move...
631
632
  			if (phy_id == 1 && location < 6 &&
  			    inw(ioaddr + MIIData) == 0xffff) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
  				outl(read_cmd, ioaddr + MIICtrl);
  				continue;
  			}
  			return inw(ioaddr + MIIData);
  		}
  	}
  	return 0xffff;
  }
  
  static void mdio_write(struct net_device *dev, int phy_id, int loc, int value)
  {
  	long ioaddr = dev->base_addr;
  	int i;
  
  	outw(value, ioaddr + MIIData);
  	outl((phy_id << 9) | (loc << 4) | MII_WRITEOP, ioaddr + MIICtrl);
f3b197ac2   Jeff Garzik   [netdrvr] trim tr...
649
  	for (i = 10000; i > 0; i--) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
650
651
652
653
  		barrier();
  		if ((inl(ioaddr + MIICtrl) & MII_WRITEOP) == 0)
  			break;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
654
  }
f3b197ac2   Jeff Garzik   [netdrvr] trim tr...
655

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
656
657
  static int epic_open(struct net_device *dev)
  {
4cf1653aa   Wang Chen   netdevice: safe c...
658
  	struct epic_private *ep = netdev_priv(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
659
660
661
662
663
664
  	long ioaddr = dev->base_addr;
  	int i;
  	int retval;
  
  	/* Soft reset the chip. */
  	outl(0x4001, ioaddr + GENCTL);
bea3348ee   Stephen Hemminger   [NET]: Make NAPI ...
665
  	napi_enable(&ep->napi);
a0607fd3a   Joe Perches   drivers/net: requ...
666
  	if ((retval = request_irq(dev->irq, epic_interrupt, IRQF_SHARED, dev->name, dev))) {
bea3348ee   Stephen Hemminger   [NET]: Make NAPI ...
667
  		napi_disable(&ep->napi);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
668
  		return retval;
bea3348ee   Stephen Hemminger   [NET]: Make NAPI ...
669
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
  
  	epic_init_ring(dev);
  
  	outl(0x4000, ioaddr + GENCTL);
  	/* This magic is documented in SMSC app note 7.15 */
  	for (i = 16; i > 0; i--)
  		outl(0x0008, ioaddr + TEST1);
  
  	/* Pull the chip out of low-power mode, enable interrupts, and set for
  	   PCI read multiple.  The MIIcfg setting and strange write order are
  	   required by the details of which bits are reset and the transceiver
  	   wiring on the Ositech CardBus card.
  	*/
  #if 0
  	outl(dev->if_port == 1 ? 0x13 : 0x12, ioaddr + MIICfg);
  #endif
  	if (ep->chip_flags & MII_PWRDWN)
  		outl((inl(ioaddr + NVCTL) & ~0x003C) | 0x4800, ioaddr + NVCTL);
9ebfd4929   Al Viro   epic100 endiannes...
688
  	/* Tell the chip to byteswap descriptors on big-endian hosts */
9e2d11b92   Roland Dreier   epic100: Test __B...
689
  #ifdef __BIG_ENDIAN
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
690
691
692
693
694
695
696
697
698
699
  	outl(0x4432 | (RX_FIFO_THRESH<<8), ioaddr + GENCTL);
  	inl(ioaddr + GENCTL);
  	outl(0x0432 | (RX_FIFO_THRESH<<8), ioaddr + GENCTL);
  #else
  	outl(0x4412 | (RX_FIFO_THRESH<<8), ioaddr + GENCTL);
  	inl(ioaddr + GENCTL);
  	outl(0x0412 | (RX_FIFO_THRESH<<8), ioaddr + GENCTL);
  #endif
  
  	udelay(20); /* Looks like EPII needs that if you want reliable RX init. FIXME: pci posting bug? */
f3b197ac2   Jeff Garzik   [netdrvr] trim tr...
700

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
701
  	for (i = 0; i < 3; i++)
9ebfd4929   Al Viro   epic100 endiannes...
702
  		outl(le16_to_cpu(((__le16*)dev->dev_addr)[i]), ioaddr + LAN0 + i*4);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
  
  	ep->tx_threshold = TX_FIFO_THRESH;
  	outl(ep->tx_threshold, ioaddr + TxThresh);
  
  	if (media2miictl[dev->if_port & 15]) {
  		if (ep->mii_phy_cnt)
  			mdio_write(dev, ep->phys[0], MII_BMCR, media2miictl[dev->if_port&15]);
  		if (dev->if_port == 1) {
  			if (debug > 1)
  				printk(KERN_INFO "%s: Using the 10base2 transceiver, MII "
  					   "status %4.4x.
  ",
  					   dev->name, mdio_read(dev, ep->phys[0], MII_BMSR));
  		}
  	} else {
  		int mii_lpa = mdio_read(dev, ep->phys[0], MII_LPA);
  		if (mii_lpa != 0xffff) {
  			if ((mii_lpa & LPA_100FULL) || (mii_lpa & 0x01C0) == LPA_10FULL)
  				ep->mii.full_duplex = 1;
  			else if (! (mii_lpa & LPA_LPACK))
  				mdio_write(dev, ep->phys[0], MII_BMCR, BMCR_ANENABLE|BMCR_ANRESTART);
  			if (debug > 1)
  				printk(KERN_INFO "%s: Setting %s-duplex based on MII xcvr %d"
  					   " register read of %4.4x.
  ", dev->name,
  					   ep->mii.full_duplex ? "full" : "half",
  					   ep->phys[0], mii_lpa);
  		}
  	}
  
  	outl(ep->mii.full_duplex ? 0x7F : 0x79, ioaddr + TxCtrl);
  	outl(ep->rx_ring_dma, ioaddr + PRxCDAR);
  	outl(ep->tx_ring_dma, ioaddr + PTxCDAR);
  
  	/* Start the chip's Rx process. */
  	set_rx_mode(dev);
  	outl(StartRx | RxQueued, ioaddr + COMMAND);
  
  	netif_start_queue(dev);
  
  	/* Enable interrupts by setting the interrupt mask. */
  	outl((ep->chip_flags & TYPE2_INTR ? PCIBusErr175 : PCIBusErr170)
f3b197ac2   Jeff Garzik   [netdrvr] trim tr...
745
  		 | CntFull | TxUnderrun
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
746
747
748
749
750
751
752
753
754
755
756
757
758
759
  		 | RxError | RxHeader | EpicNapiEvent, ioaddr + INTMASK);
  
  	if (debug > 1)
  		printk(KERN_DEBUG "%s: epic_open() ioaddr %lx IRQ %d status %4.4x "
  			   "%s-duplex.
  ",
  			   dev->name, ioaddr, dev->irq, (int)inl(ioaddr + GENCTL),
  			   ep->mii.full_duplex ? "full" : "half");
  
  	/* Set the timer to switch to check for link beat and perhaps switch
  	   to an alternate media type. */
  	init_timer(&ep->timer);
  	ep->timer.expires = jiffies + 3*HZ;
  	ep->timer.data = (unsigned long)dev;
c061b18df   Joe Perches   drivers/net: Remo...
760
  	ep->timer.function = epic_timer;				/* timer handler */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
761
762
763
764
765
766
767
768
769
770
  	add_timer(&ep->timer);
  
  	return 0;
  }
  
  /* Reset the chip to recover from a PCI transaction error.
     This may occur at interrupt time. */
  static void epic_pause(struct net_device *dev)
  {
  	long ioaddr = dev->base_addr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
771
772
  
  	netif_stop_queue (dev);
f3b197ac2   Jeff Garzik   [netdrvr] trim tr...
773

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
774
775
776
777
778
779
780
  	/* Disable interrupts by clearing the interrupt mask. */
  	outl(0x00000000, ioaddr + INTMASK);
  	/* Stop the chip's Tx and Rx DMA processes. */
  	outw(StopRx | StopTxDMA | StopRxDMA, ioaddr + COMMAND);
  
  	/* Update the error counts. */
  	if (inw(ioaddr + COMMAND) != 0xffff) {
275defc95   Kulikov Vasiliy   epic100: Use the ...
781
782
783
  		dev->stats.rx_missed_errors += inb(ioaddr + MPCNT);
  		dev->stats.rx_frame_errors += inb(ioaddr + ALICNT);
  		dev->stats.rx_crc_errors += inb(ioaddr + CRCCNT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
784
785
786
787
788
789
790
791
792
  	}
  
  	/* Remove the packets on the Rx queue. */
  	epic_rx(dev, RX_RING_SIZE);
  }
  
  static void epic_restart(struct net_device *dev)
  {
  	long ioaddr = dev->base_addr;
4cf1653aa   Wang Chen   netdevice: safe c...
793
  	struct epic_private *ep = netdev_priv(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
794
795
796
797
798
799
800
801
802
803
804
805
806
  	int i;
  
  	/* Soft reset the chip. */
  	outl(0x4001, ioaddr + GENCTL);
  
  	printk(KERN_DEBUG "%s: Restarting the EPIC chip, Rx %d/%d Tx %d/%d.
  ",
  		   dev->name, ep->cur_rx, ep->dirty_rx, ep->dirty_tx, ep->cur_tx);
  	udelay(1);
  
  	/* This magic is documented in SMSC app note 7.15 */
  	for (i = 16; i > 0; i--)
  		outl(0x0008, ioaddr + TEST1);
9e2d11b92   Roland Dreier   epic100: Test __B...
807
  #ifdef __BIG_ENDIAN
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
808
809
810
811
812
813
814
815
816
  	outl(0x0432 | (RX_FIFO_THRESH<<8), ioaddr + GENCTL);
  #else
  	outl(0x0412 | (RX_FIFO_THRESH<<8), ioaddr + GENCTL);
  #endif
  	outl(dev->if_port == 1 ? 0x13 : 0x12, ioaddr + MIICfg);
  	if (ep->chip_flags & MII_PWRDWN)
  		outl((inl(ioaddr + NVCTL) & ~0x003C) | 0x4800, ioaddr + NVCTL);
  
  	for (i = 0; i < 3; i++)
9ebfd4929   Al Viro   epic100 endiannes...
817
  		outl(le16_to_cpu(((__le16*)dev->dev_addr)[i]), ioaddr + LAN0 + i*4);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
  
  	ep->tx_threshold = TX_FIFO_THRESH;
  	outl(ep->tx_threshold, ioaddr + TxThresh);
  	outl(ep->mii.full_duplex ? 0x7F : 0x79, ioaddr + TxCtrl);
  	outl(ep->rx_ring_dma + (ep->cur_rx%RX_RING_SIZE)*
  		sizeof(struct epic_rx_desc), ioaddr + PRxCDAR);
  	outl(ep->tx_ring_dma + (ep->dirty_tx%TX_RING_SIZE)*
  		 sizeof(struct epic_tx_desc), ioaddr + PTxCDAR);
  
  	/* Start the chip's Rx process. */
  	set_rx_mode(dev);
  	outl(StartRx | RxQueued, ioaddr + COMMAND);
  
  	/* Enable interrupts by setting the interrupt mask. */
  	outl((ep->chip_flags & TYPE2_INTR ? PCIBusErr175 : PCIBusErr170)
  		 | CntFull | TxUnderrun
  		 | RxError | RxHeader | EpicNapiEvent, ioaddr + INTMASK);
  
  	printk(KERN_DEBUG "%s: epic_restart() done, cmd status %4.4x, ctl %4.4x"
  		   " interrupt %4.4x.
  ",
  		   dev->name, (int)inl(ioaddr + COMMAND), (int)inl(ioaddr + GENCTL),
  		   (int)inl(ioaddr + INTSTAT));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
841
842
843
844
  }
  
  static void check_media(struct net_device *dev)
  {
4cf1653aa   Wang Chen   netdevice: safe c...
845
  	struct epic_private *ep = netdev_priv(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
  	long ioaddr = dev->base_addr;
  	int mii_lpa = ep->mii_phy_cnt ? mdio_read(dev, ep->phys[0], MII_LPA) : 0;
  	int negotiated = mii_lpa & ep->mii.advertising;
  	int duplex = (negotiated & 0x0100) || (negotiated & 0x01C0) == 0x0040;
  
  	if (ep->mii.force_media)
  		return;
  	if (mii_lpa == 0xffff)		/* Bogus read */
  		return;
  	if (ep->mii.full_duplex != duplex) {
  		ep->mii.full_duplex = duplex;
  		printk(KERN_INFO "%s: Setting %s-duplex based on MII #%d link"
  			   " partner capability of %4.4x.
  ", dev->name,
  			   ep->mii.full_duplex ? "full" : "half", ep->phys[0], mii_lpa);
  		outl(ep->mii.full_duplex ? 0x7F : 0x79, ioaddr + TxCtrl);
  	}
  }
  
  static void epic_timer(unsigned long data)
  {
  	struct net_device *dev = (struct net_device *)data;
4cf1653aa   Wang Chen   netdevice: safe c...
868
  	struct epic_private *ep = netdev_priv(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
  	long ioaddr = dev->base_addr;
  	int next_tick = 5*HZ;
  
  	if (debug > 3) {
  		printk(KERN_DEBUG "%s: Media monitor tick, Tx status %8.8x.
  ",
  			   dev->name, (int)inl(ioaddr + TxSTAT));
  		printk(KERN_DEBUG "%s: Other registers are IntMask %4.4x "
  			   "IntStatus %4.4x RxStatus %4.4x.
  ",
  			   dev->name, (int)inl(ioaddr + INTMASK),
  			   (int)inl(ioaddr + INTSTAT), (int)inl(ioaddr + RxSTAT));
  	}
  
  	check_media(dev);
  
  	ep->timer.expires = jiffies + next_tick;
  	add_timer(&ep->timer);
  }
  
  static void epic_tx_timeout(struct net_device *dev)
  {
4cf1653aa   Wang Chen   netdevice: safe c...
891
  	struct epic_private *ep = netdev_priv(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
892
893
894
895
896
897
898
899
900
901
902
903
904
905
  	long ioaddr = dev->base_addr;
  
  	if (debug > 0) {
  		printk(KERN_WARNING "%s: Transmit timeout using MII device, "
  			   "Tx status %4.4x.
  ",
  			   dev->name, (int)inw(ioaddr + TxSTAT));
  		if (debug > 1) {
  			printk(KERN_DEBUG "%s: Tx indices: dirty_tx %d, cur_tx %d.
  ",
  				   dev->name, ep->dirty_tx, ep->cur_tx);
  		}
  	}
  	if (inw(ioaddr + TxSTAT) & 0x10) {		/* Tx FIFO underflow. */
275defc95   Kulikov Vasiliy   epic100: Use the ...
906
  		dev->stats.tx_fifo_errors++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
907
908
909
910
911
  		outl(RestartTx, ioaddr + COMMAND);
  	} else {
  		epic_restart(dev);
  		outl(TxQueued, dev->base_addr + COMMAND);
  	}
1ae5dc342   Eric Dumazet   net: trans_start ...
912
  	dev->trans_start = jiffies; /* prevent tx timeout */
275defc95   Kulikov Vasiliy   epic100: Use the ...
913
  	dev->stats.tx_errors++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
914
915
916
917
918
919
920
  	if (!ep->tx_full)
  		netif_wake_queue(dev);
  }
  
  /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
  static void epic_init_ring(struct net_device *dev)
  {
4cf1653aa   Wang Chen   netdevice: safe c...
921
  	struct epic_private *ep = netdev_priv(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
922
923
924
925
926
927
928
929
930
931
  	int i;
  
  	ep->tx_full = 0;
  	ep->dirty_tx = ep->cur_tx = 0;
  	ep->cur_rx = ep->dirty_rx = 0;
  	ep->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
  
  	/* Initialize all Rx descriptors. */
  	for (i = 0; i < RX_RING_SIZE; i++) {
  		ep->rx_ring[i].rxstatus = 0;
9ebfd4929   Al Viro   epic100 endiannes...
932
  		ep->rx_ring[i].buflength = ep->rx_buf_sz;
f3b197ac2   Jeff Garzik   [netdrvr] trim tr...
933
  		ep->rx_ring[i].next = ep->rx_ring_dma +
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
934
935
936
937
938
939
940
941
  				      (i+1)*sizeof(struct epic_rx_desc);
  		ep->rx_skbuff[i] = NULL;
  	}
  	/* Mark the last entry as wrapping the ring. */
  	ep->rx_ring[i-1].next = ep->rx_ring_dma;
  
  	/* Fill in the Rx buffers.  Handle allocation failure gracefully. */
  	for (i = 0; i < RX_RING_SIZE; i++) {
7a36df8a8   Jarek Poplawski   epic100: hamachi:...
942
  		struct sk_buff *skb = dev_alloc_skb(ep->rx_buf_sz + 2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
943
944
945
  		ep->rx_skbuff[i] = skb;
  		if (skb == NULL)
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
946
  		skb_reserve(skb, 2);	/* 16 byte align the IP header. */
f3b197ac2   Jeff Garzik   [netdrvr] trim tr...
947
  		ep->rx_ring[i].bufaddr = pci_map_single(ep->pci_dev,
689be4394   David S. Miller   [NET]: Remove gra...
948
  			skb->data, ep->rx_buf_sz, PCI_DMA_FROMDEVICE);
9ebfd4929   Al Viro   epic100 endiannes...
949
  		ep->rx_ring[i].rxstatus = DescOwn;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
950
951
952
953
954
955
956
957
  	}
  	ep->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
  
  	/* The Tx buffer descriptor is filled in as needed, but we
  	   do need to clear the ownership bit. */
  	for (i = 0; i < TX_RING_SIZE; i++) {
  		ep->tx_skbuff[i] = NULL;
  		ep->tx_ring[i].txstatus = 0x0000;
f3b197ac2   Jeff Garzik   [netdrvr] trim tr...
958
  		ep->tx_ring[i].next = ep->tx_ring_dma +
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
959
960
961
  			(i+1)*sizeof(struct epic_tx_desc);
  	}
  	ep->tx_ring[i-1].next = ep->tx_ring_dma;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
962
  }
61357325f   Stephen Hemminger   netdev: convert b...
963
  static netdev_tx_t epic_start_xmit(struct sk_buff *skb, struct net_device *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
964
  {
4cf1653aa   Wang Chen   netdevice: safe c...
965
  	struct epic_private *ep = netdev_priv(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
966
967
968
  	int entry, free_count;
  	u32 ctrl_word;
  	unsigned long flags;
f3b197ac2   Jeff Garzik   [netdrvr] trim tr...
969

5b057c6b1   Herbert Xu   [NET]: Avoid allo...
970
  	if (skb_padto(skb, ETH_ZLEN))
6ed106549   Patrick McHardy   net: use NETDEV_T...
971
  		return NETDEV_TX_OK;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
972
973
974
975
976
977
978
979
980
981
  
  	/* Caution: the write order is important here, set the field with the
  	   "ownership" bit last. */
  
  	/* Calculate the next Tx descriptor entry. */
  	spin_lock_irqsave(&ep->lock, flags);
  	free_count = ep->cur_tx - ep->dirty_tx;
  	entry = ep->cur_tx % TX_RING_SIZE;
  
  	ep->tx_skbuff[entry] = skb;
f3b197ac2   Jeff Garzik   [netdrvr] trim tr...
982
  	ep->tx_ring[entry].bufaddr = pci_map_single(ep->pci_dev, skb->data,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
983
984
  		 			            skb->len, PCI_DMA_TODEVICE);
  	if (free_count < TX_QUEUE_LEN/2) {/* Typical path */
9ebfd4929   Al Viro   epic100 endiannes...
985
  		ctrl_word = 0x100000; /* No interrupt */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
986
  	} else if (free_count == TX_QUEUE_LEN/2) {
9ebfd4929   Al Viro   epic100 endiannes...
987
  		ctrl_word = 0x140000; /* Tx-done intr. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
988
  	} else if (free_count < TX_QUEUE_LEN - 1) {
9ebfd4929   Al Viro   epic100 endiannes...
989
  		ctrl_word = 0x100000; /* No Tx-done intr. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
990
991
  	} else {
  		/* Leave room for an additional entry. */
9ebfd4929   Al Viro   epic100 endiannes...
992
  		ctrl_word = 0x140000; /* Tx-done intr. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
993
994
  		ep->tx_full = 1;
  	}
9ebfd4929   Al Viro   epic100 endiannes...
995
  	ep->tx_ring[entry].buflength = ctrl_word | skb->len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
996
997
  	ep->tx_ring[entry].txstatus =
  		((skb->len >= ETH_ZLEN ? skb->len : ETH_ZLEN) << 16)
9ebfd4929   Al Viro   epic100 endiannes...
998
  			    | DescOwn;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
999
1000
1001
1002
1003
1004
1005
1006
  
  	ep->cur_tx++;
  	if (ep->tx_full)
  		netif_stop_queue(dev);
  
  	spin_unlock_irqrestore(&ep->lock, flags);
  	/* Trigger an immediate transmit demand. */
  	outl(TxQueued, dev->base_addr + COMMAND);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1007
1008
1009
1010
1011
1012
  	if (debug > 4)
  		printk(KERN_DEBUG "%s: Queued Tx packet size %d to slot %d, "
  			   "flag %2.2x Tx status %8.8x.
  ",
  			   dev->name, (int)skb->len, entry, ctrl_word,
  			   (int)inl(dev->base_addr + TxSTAT));
6ed106549   Patrick McHardy   net: use NETDEV_T...
1013
  	return NETDEV_TX_OK;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1014
1015
1016
1017
1018
  }
  
  static void epic_tx_error(struct net_device *dev, struct epic_private *ep,
  			  int status)
  {
275defc95   Kulikov Vasiliy   epic100: Use the ...
1019
  	struct net_device_stats *stats = &dev->stats;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
  
  #ifndef final_version
  	/* There was an major error, log it. */
  	if (debug > 1)
  		printk(KERN_DEBUG "%s: Transmit error, Tx status %8.8x.
  ",
  		       dev->name, status);
  #endif
  	stats->tx_errors++;
  	if (status & 0x1050)
  		stats->tx_aborted_errors++;
  	if (status & 0x0008)
  		stats->tx_carrier_errors++;
  	if (status & 0x0040)
  		stats->tx_window_errors++;
  	if (status & 0x0010)
  		stats->tx_fifo_errors++;
  }
  
  static void epic_tx(struct net_device *dev, struct epic_private *ep)
  {
  	unsigned int dirty_tx, cur_tx;
  
  	/*
  	 * Note: if this lock becomes a problem we can narrow the locked
  	 * region at the cost of occasionally grabbing the lock more times.
  	 */
  	cur_tx = ep->cur_tx;
  	for (dirty_tx = ep->dirty_tx; cur_tx - dirty_tx > 0; dirty_tx++) {
  		struct sk_buff *skb;
  		int entry = dirty_tx % TX_RING_SIZE;
9ebfd4929   Al Viro   epic100 endiannes...
1051
  		int txstatus = ep->tx_ring[entry].txstatus;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1052
1053
1054
1055
1056
  
  		if (txstatus & DescOwn)
  			break;	/* It still hasn't been Txed */
  
  		if (likely(txstatus & 0x0001)) {
275defc95   Kulikov Vasiliy   epic100: Use the ...
1057
1058
1059
  			dev->stats.collisions += (txstatus >> 8) & 15;
  			dev->stats.tx_packets++;
  			dev->stats.tx_bytes += ep->tx_skbuff[entry]->len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1060
1061
1062
1063
1064
  		} else
  			epic_tx_error(dev, ep, txstatus);
  
  		/* Free the original skb. */
  		skb = ep->tx_skbuff[entry];
f3b197ac2   Jeff Garzik   [netdrvr] trim tr...
1065
  		pci_unmap_single(ep->pci_dev, ep->tx_ring[entry].bufaddr,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
  				 skb->len, PCI_DMA_TODEVICE);
  		dev_kfree_skb_irq(skb);
  		ep->tx_skbuff[entry] = NULL;
  	}
  
  #ifndef final_version
  	if (cur_tx - dirty_tx > TX_RING_SIZE) {
  		printk(KERN_WARNING
  		       "%s: Out-of-sync dirty pointer, %d vs. %d, full=%d.
  ",
  		       dev->name, dirty_tx, cur_tx, ep->tx_full);
  		dirty_tx += TX_RING_SIZE;
  	}
  #endif
  	ep->dirty_tx = dirty_tx;
  	if (ep->tx_full && cur_tx - dirty_tx < TX_QUEUE_LEN - 4) {
  		/* The ring is no longer full, allow new TX entries. */
  		ep->tx_full = 0;
  		netif_wake_queue(dev);
  	}
  }
  
  /* The interrupt handler does all of the Rx thread work and cleans up
     after the Tx thread. */
7d12e780e   David Howells   IRQ: Maintain reg...
1090
  static irqreturn_t epic_interrupt(int irq, void *dev_instance)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1091
1092
  {
  	struct net_device *dev = dev_instance;
4cf1653aa   Wang Chen   netdevice: safe c...
1093
  	struct epic_private *ep = netdev_priv(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
  	long ioaddr = dev->base_addr;
  	unsigned int handled = 0;
  	int status;
  
  	status = inl(ioaddr + INTSTAT);
  	/* Acknowledge all of the current interrupt sources ASAP. */
  	outl(status & EpicNormalEvent, ioaddr + INTSTAT);
  
  	if (debug > 4) {
  		printk(KERN_DEBUG "%s: Interrupt, status=%#8.8x new "
  				   "intstat=%#8.8x.
  ", dev->name, status,
  				   (int)inl(ioaddr + INTSTAT));
  	}
  
  	if ((status & IntrSummary) == 0)
  		goto out;
  
  	handled = 1;
  
  	if ((status & EpicNapiEvent) && !ep->reschedule_in_poll) {
  		spin_lock(&ep->napi_lock);
288379f05   Ben Hutchings   net: Remove redun...
1116
  		if (napi_schedule_prep(&ep->napi)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1117
  			epic_napi_irq_off(dev, ep);
288379f05   Ben Hutchings   net: Remove redun...
1118
  			__napi_schedule(&ep->napi);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
  		} else
  			ep->reschedule_in_poll++;
  		spin_unlock(&ep->napi_lock);
  	}
  	status &= ~EpicNapiEvent;
  
  	/* Check uncommon events all at once. */
  	if (status & (CntFull | TxUnderrun | PCIBusErr170 | PCIBusErr175)) {
  		if (status == EpicRemoved)
  			goto out;
  
  		/* Always update the error counts to avoid overhead later. */
275defc95   Kulikov Vasiliy   epic100: Use the ...
1131
1132
1133
  		dev->stats.rx_missed_errors += inb(ioaddr + MPCNT);
  		dev->stats.rx_frame_errors += inb(ioaddr + ALICNT);
  		dev->stats.rx_crc_errors += inb(ioaddr + CRCCNT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1134
1135
  
  		if (status & TxUnderrun) { /* Tx FIFO underflow. */
275defc95   Kulikov Vasiliy   epic100: Use the ...
1136
  			dev->stats.tx_fifo_errors++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
  			outl(ep->tx_threshold += 128, ioaddr + TxThresh);
  			/* Restart the transmit process. */
  			outl(RestartTx, ioaddr + COMMAND);
  		}
  		if (status & PCIBusErr170) {
  			printk(KERN_ERR "%s: PCI Bus Error! status %4.4x.
  ",
  					 dev->name, status);
  			epic_pause(dev);
  			epic_restart(dev);
  		}
  		/* Clear all error sources. */
  		outl(status & 0x7f18, ioaddr + INTSTAT);
  	}
  
  out:
  	if (debug > 3) {
  		printk(KERN_DEBUG "%s: exit interrupt, intr_status=%#4.4x.
  ",
  				   dev->name, status);
  	}
  
  	return IRQ_RETVAL(handled);
  }
  
  static int epic_rx(struct net_device *dev, int budget)
  {
4cf1653aa   Wang Chen   netdevice: safe c...
1164
  	struct epic_private *ep = netdev_priv(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
  	int entry = ep->cur_rx % RX_RING_SIZE;
  	int rx_work_limit = ep->dirty_rx + RX_RING_SIZE - ep->cur_rx;
  	int work_done = 0;
  
  	if (debug > 4)
  		printk(KERN_DEBUG " In epic_rx(), entry %d %8.8x.
  ", entry,
  			   ep->rx_ring[entry].rxstatus);
  
  	if (rx_work_limit > budget)
  		rx_work_limit = budget;
  
  	/* If we own the next entry, it's a new packet. Send it up. */
9ebfd4929   Al Viro   epic100 endiannes...
1178
1179
  	while ((ep->rx_ring[entry].rxstatus & DescOwn) == 0) {
  		int status = ep->rx_ring[entry].rxstatus;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
  
  		if (debug > 4)
  			printk(KERN_DEBUG "  epic_rx() status was %8.8x.
  ", status);
  		if (--rx_work_limit < 0)
  			break;
  		if (status & 0x2006) {
  			if (debug > 2)
  				printk(KERN_DEBUG "%s: epic_rx() error status was %8.8x.
  ",
  					   dev->name, status);
  			if (status & 0x2000) {
  				printk(KERN_WARNING "%s: Oversized Ethernet frame spanned "
  					   "multiple buffers, status %4.4x!
  ", dev->name, status);
275defc95   Kulikov Vasiliy   epic100: Use the ...
1195
  				dev->stats.rx_length_errors++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1196
1197
  			} else if (status & 0x0006)
  				/* Rx Frame errors are counted in hardware. */
275defc95   Kulikov Vasiliy   epic100: Use the ...
1198
  				dev->stats.rx_errors++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
  		} else {
  			/* Malloc up new buffer, compatible with net-2e. */
  			/* Omit the four octet CRC from the length. */
  			short pkt_len = (status >> 16) - 4;
  			struct sk_buff *skb;
  
  			if (pkt_len > PKT_BUF_SZ - 4) {
  				printk(KERN_ERR "%s: Oversized Ethernet frame, status %x "
  					   "%d bytes.
  ",
  					   dev->name, status, pkt_len);
  				pkt_len = 1514;
  			}
  			/* Check if the packet is long enough to accept without copying
  			   to a minimally-sized skbuff. */
8e95a2026   Joe Perches   drivers/net: Move...
1214
1215
  			if (pkt_len < rx_copybreak &&
  			    (skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1216
1217
1218
1219
1220
  				skb_reserve(skb, 2);	/* 16 byte align the IP header */
  				pci_dma_sync_single_for_cpu(ep->pci_dev,
  							    ep->rx_ring[entry].bufaddr,
  							    ep->rx_buf_sz,
  							    PCI_DMA_FROMDEVICE);
8c7b7faaa   David S. Miller   [NET]: Kill eth_c...
1221
  				skb_copy_to_linear_data(skb, ep->rx_skbuff[entry]->data, pkt_len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1222
1223
1224
1225
1226
1227
  				skb_put(skb, pkt_len);
  				pci_dma_sync_single_for_device(ep->pci_dev,
  							       ep->rx_ring[entry].bufaddr,
  							       ep->rx_buf_sz,
  							       PCI_DMA_FROMDEVICE);
  			} else {
f3b197ac2   Jeff Garzik   [netdrvr] trim tr...
1228
1229
  				pci_unmap_single(ep->pci_dev,
  					ep->rx_ring[entry].bufaddr,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1230
1231
1232
1233
1234
1235
  					ep->rx_buf_sz, PCI_DMA_FROMDEVICE);
  				skb_put(skb = ep->rx_skbuff[entry], pkt_len);
  				ep->rx_skbuff[entry] = NULL;
  			}
  			skb->protocol = eth_type_trans(skb, dev);
  			netif_receive_skb(skb);
275defc95   Kulikov Vasiliy   epic100: Use the ...
1236
1237
  			dev->stats.rx_packets++;
  			dev->stats.rx_bytes += pkt_len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
  		}
  		work_done++;
  		entry = (++ep->cur_rx) % RX_RING_SIZE;
  	}
  
  	/* Refill the Rx ring buffers. */
  	for (; ep->cur_rx - ep->dirty_rx > 0; ep->dirty_rx++) {
  		entry = ep->dirty_rx % RX_RING_SIZE;
  		if (ep->rx_skbuff[entry] == NULL) {
  			struct sk_buff *skb;
7a36df8a8   Jarek Poplawski   epic100: hamachi:...
1248
  			skb = ep->rx_skbuff[entry] = dev_alloc_skb(ep->rx_buf_sz + 2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1249
1250
  			if (skb == NULL)
  				break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1251
  			skb_reserve(skb, 2);	/* Align IP on 16 byte boundaries */
f3b197ac2   Jeff Garzik   [netdrvr] trim tr...
1252
  			ep->rx_ring[entry].bufaddr = pci_map_single(ep->pci_dev,
689be4394   David S. Miller   [NET]: Remove gra...
1253
  				skb->data, ep->rx_buf_sz, PCI_DMA_FROMDEVICE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1254
1255
  			work_done++;
  		}
9ebfd4929   Al Viro   epic100 endiannes...
1256
1257
  		/* AV: shouldn't we add a barrier here? */
  		ep->rx_ring[entry].rxstatus = DescOwn;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
  	}
  	return work_done;
  }
  
  static void epic_rx_err(struct net_device *dev, struct epic_private *ep)
  {
  	long ioaddr = dev->base_addr;
  	int status;
  
  	status = inl(ioaddr + INTSTAT);
  
  	if (status == EpicRemoved)
  		return;
  	if (status & RxOverflow) 	/* Missed a Rx frame. */
275defc95   Kulikov Vasiliy   epic100: Use the ...
1272
  		dev->stats.rx_errors++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1273
1274
1275
  	if (status & (RxOverflow | RxFull))
  		outw(RxQueued, ioaddr + COMMAND);
  }
bea3348ee   Stephen Hemminger   [NET]: Make NAPI ...
1276
  static int epic_poll(struct napi_struct *napi, int budget)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1277
  {
bea3348ee   Stephen Hemminger   [NET]: Make NAPI ...
1278
1279
1280
  	struct epic_private *ep = container_of(napi, struct epic_private, napi);
  	struct net_device *dev = ep->mii.dev;
  	int work_done = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1281
  	long ioaddr = dev->base_addr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1282
1283
1284
  rx_action:
  
  	epic_tx(dev, ep);
bea3348ee   Stephen Hemminger   [NET]: Make NAPI ...
1285
  	work_done += epic_rx(dev, budget);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1286
1287
  
  	epic_rx_err(dev, ep);
4ec241198   David S. Miller   [NET]: Do not che...
1288
  	if (work_done < budget) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1289
1290
1291
1292
1293
1294
1295
1296
1297
  		unsigned long flags;
  		int more;
  
  		/* A bit baroque but it avoids a (space hungry) spin_unlock */
  
  		spin_lock_irqsave(&ep->napi_lock, flags);
  
  		more = ep->reschedule_in_poll;
  		if (!more) {
288379f05   Ben Hutchings   net: Remove redun...
1298
  			__napi_complete(napi);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
  			outl(EpicNapiEvent, ioaddr + INTSTAT);
  			epic_napi_irq_on(dev, ep);
  		} else
  			ep->reschedule_in_poll--;
  
  		spin_unlock_irqrestore(&ep->napi_lock, flags);
  
  		if (more)
  			goto rx_action;
  	}
bea3348ee   Stephen Hemminger   [NET]: Make NAPI ...
1309
  	return work_done;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1310
1311
1312
1313
1314
  }
  
  static int epic_close(struct net_device *dev)
  {
  	long ioaddr = dev->base_addr;
4cf1653aa   Wang Chen   netdevice: safe c...
1315
  	struct epic_private *ep = netdev_priv(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1316
1317
1318
1319
  	struct sk_buff *skb;
  	int i;
  
  	netif_stop_queue(dev);
bea3348ee   Stephen Hemminger   [NET]: Make NAPI ...
1320
  	napi_disable(&ep->napi);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
  
  	if (debug > 1)
  		printk(KERN_DEBUG "%s: Shutting down ethercard, status was %2.2x.
  ",
  			   dev->name, (int)inl(ioaddr + INTSTAT));
  
  	del_timer_sync(&ep->timer);
  
  	epic_disable_int(dev, ep);
  
  	free_irq(dev->irq, dev);
  
  	epic_pause(dev);
  
  	/* Free all the skbuffs in the Rx queue. */
  	for (i = 0; i < RX_RING_SIZE; i++) {
  		skb = ep->rx_skbuff[i];
  		ep->rx_skbuff[i] = NULL;
  		ep->rx_ring[i].rxstatus = 0;		/* Not owned by Epic chip. */
  		ep->rx_ring[i].buflength = 0;
  		if (skb) {
f3b197ac2   Jeff Garzik   [netdrvr] trim tr...
1342
  			pci_unmap_single(ep->pci_dev, ep->rx_ring[i].bufaddr,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
  				 	 ep->rx_buf_sz, PCI_DMA_FROMDEVICE);
  			dev_kfree_skb(skb);
  		}
  		ep->rx_ring[i].bufaddr = 0xBADF00D0; /* An invalid address. */
  	}
  	for (i = 0; i < TX_RING_SIZE; i++) {
  		skb = ep->tx_skbuff[i];
  		ep->tx_skbuff[i] = NULL;
  		if (!skb)
  			continue;
f3b197ac2   Jeff Garzik   [netdrvr] trim tr...
1353
  		pci_unmap_single(ep->pci_dev, ep->tx_ring[i].bufaddr,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
  				 skb->len, PCI_DMA_TODEVICE);
  		dev_kfree_skb(skb);
  	}
  
  	/* Green! Leave the chip in low-power mode. */
  	outl(0x0008, ioaddr + GENCTL);
  
  	return 0;
  }
  
  static struct net_device_stats *epic_get_stats(struct net_device *dev)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1366
1367
1368
1369
  	long ioaddr = dev->base_addr;
  
  	if (netif_running(dev)) {
  		/* Update the error counts. */
275defc95   Kulikov Vasiliy   epic100: Use the ...
1370
1371
1372
  		dev->stats.rx_missed_errors += inb(ioaddr + MPCNT);
  		dev->stats.rx_frame_errors += inb(ioaddr + ALICNT);
  		dev->stats.rx_crc_errors += inb(ioaddr + CRCCNT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1373
  	}
275defc95   Kulikov Vasiliy   epic100: Use the ...
1374
  	return &dev->stats;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
  }
  
  /* Set or clear the multicast filter for this adaptor.
     Note that we only use exclusion around actually queueing the
     new frame, not around filling ep->setup_frame.  This is non-deterministic
     when re-entered but still correct. */
  
  static void set_rx_mode(struct net_device *dev)
  {
  	long ioaddr = dev->base_addr;
4cf1653aa   Wang Chen   netdevice: safe c...
1385
  	struct epic_private *ep = netdev_priv(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1386
1387
1388
1389
1390
1391
  	unsigned char mc_filter[8];		 /* Multicast hash filter */
  	int i;
  
  	if (dev->flags & IFF_PROMISC) {			/* Set promiscuous. */
  		outl(0x002C, ioaddr + RxCtrl);
  		/* Unconditionally log net taps. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1392
  		memset(mc_filter, 0xff, sizeof(mc_filter));
4cd24eaf0   Jiri Pirko   net: use netdev_m...
1393
  	} else if ((!netdev_mc_empty(dev)) || (dev->flags & IFF_ALLMULTI)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1394
1395
1396
1397
1398
  		/* There is apparently a chip bug, so the multicast filter
  		   is never enabled. */
  		/* Too many to filter perfectly -- accept all multicasts. */
  		memset(mc_filter, 0xff, sizeof(mc_filter));
  		outl(0x000C, ioaddr + RxCtrl);
4cd24eaf0   Jiri Pirko   net: use netdev_m...
1399
  	} else if (netdev_mc_empty(dev)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1400
1401
1402
  		outl(0x0004, ioaddr + RxCtrl);
  		return;
  	} else {					/* Never executed, for now. */
22bedad3c   Jiri Pirko   net: convert mult...
1403
  		struct netdev_hw_addr *ha;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1404
1405
  
  		memset(mc_filter, 0, sizeof(mc_filter));
22bedad3c   Jiri Pirko   net: convert mult...
1406
  		netdev_for_each_mc_addr(ha, dev) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1407
  			unsigned int bit_nr =
22bedad3c   Jiri Pirko   net: convert mult...
1408
  				ether_crc_le(ETH_ALEN, ha->addr) & 0x3f;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1409
1410
1411
1412
1413
1414
1415
1416
1417
  			mc_filter[bit_nr >> 3] |= (1 << bit_nr);
  		}
  	}
  	/* ToDo: perhaps we need to stop the Tx and Rx process here? */
  	if (memcmp(mc_filter, ep->mc_filter, sizeof(mc_filter))) {
  		for (i = 0; i < 4; i++)
  			outw(((u16 *)mc_filter)[i], ioaddr + MC0 + i*4);
  		memcpy(ep->mc_filter, mc_filter, sizeof(mc_filter));
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1418
1419
1420
1421
  }
  
  static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info)
  {
4cf1653aa   Wang Chen   netdevice: safe c...
1422
  	struct epic_private *np = netdev_priv(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1423
1424
1425
1426
1427
1428
1429
1430
  
  	strcpy (info->driver, DRV_NAME);
  	strcpy (info->version, DRV_VERSION);
  	strcpy (info->bus_info, pci_name(np->pci_dev));
  }
  
  static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  {
4cf1653aa   Wang Chen   netdevice: safe c...
1431
  	struct epic_private *np = netdev_priv(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
  	int rc;
  
  	spin_lock_irq(&np->lock);
  	rc = mii_ethtool_gset(&np->mii, cmd);
  	spin_unlock_irq(&np->lock);
  
  	return rc;
  }
  
  static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  {
4cf1653aa   Wang Chen   netdevice: safe c...
1443
  	struct epic_private *np = netdev_priv(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
  	int rc;
  
  	spin_lock_irq(&np->lock);
  	rc = mii_ethtool_sset(&np->mii, cmd);
  	spin_unlock_irq(&np->lock);
  
  	return rc;
  }
  
  static int netdev_nway_reset(struct net_device *dev)
  {
4cf1653aa   Wang Chen   netdevice: safe c...
1455
  	struct epic_private *np = netdev_priv(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1456
1457
1458
1459
1460
  	return mii_nway_restart(&np->mii);
  }
  
  static u32 netdev_get_link(struct net_device *dev)
  {
4cf1653aa   Wang Chen   netdevice: safe c...
1461
  	struct epic_private *np = netdev_priv(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
  	return mii_link_ok(&np->mii);
  }
  
  static u32 netdev_get_msglevel(struct net_device *dev)
  {
  	return debug;
  }
  
  static void netdev_set_msglevel(struct net_device *dev, u32 value)
  {
  	debug = value;
  }
  
  static int ethtool_begin(struct net_device *dev)
  {
  	unsigned long ioaddr = dev->base_addr;
  	/* power-up, if interface is down */
  	if (! netif_running(dev)) {
  		outl(0x0200, ioaddr + GENCTL);
  		outl((inl(ioaddr + NVCTL) & ~0x003C) | 0x4800, ioaddr + NVCTL);
  	}
  	return 0;
  }
  
  static void ethtool_complete(struct net_device *dev)
  {
  	unsigned long ioaddr = dev->base_addr;
  	/* power-down, if interface is down */
  	if (! netif_running(dev)) {
  		outl(0x0008, ioaddr + GENCTL);
  		outl((inl(ioaddr + NVCTL) & ~0x483C) | 0x0000, ioaddr + NVCTL);
  	}
  }
7282d491e   Jeff Garzik   drivers/net: cons...
1495
  static const struct ethtool_ops netdev_ethtool_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1496
1497
1498
1499
1500
1501
1502
  	.get_drvinfo		= netdev_get_drvinfo,
  	.get_settings		= netdev_get_settings,
  	.set_settings		= netdev_set_settings,
  	.nway_reset		= netdev_nway_reset,
  	.get_link		= netdev_get_link,
  	.get_msglevel		= netdev_get_msglevel,
  	.set_msglevel		= netdev_set_msglevel,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1503
1504
1505
1506
1507
1508
  	.begin			= ethtool_begin,
  	.complete		= ethtool_complete
  };
  
  static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  {
4cf1653aa   Wang Chen   netdevice: safe c...
1509
  	struct epic_private *np = netdev_priv(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
  	long ioaddr = dev->base_addr;
  	struct mii_ioctl_data *data = if_mii(rq);
  	int rc;
  
  	/* power-up, if interface is down */
  	if (! netif_running(dev)) {
  		outl(0x0200, ioaddr + GENCTL);
  		outl((inl(ioaddr + NVCTL) & ~0x003C) | 0x4800, ioaddr + NVCTL);
  	}
  
  	/* all non-ethtool ioctls (the SIOC[GS]MIIxxx ioctls) */
  	spin_lock_irq(&np->lock);
  	rc = generic_mii_ioctl(&np->mii, data, cmd, NULL);
  	spin_unlock_irq(&np->lock);
  
  	/* power-down, if interface is down */
  	if (! netif_running(dev)) {
  		outl(0x0008, ioaddr + GENCTL);
  		outl((inl(ioaddr + NVCTL) & ~0x483C) | 0x0000, ioaddr + NVCTL);
  	}
  	return rc;
  }
  
  
  static void __devexit epic_remove_one (struct pci_dev *pdev)
  {
  	struct net_device *dev = pci_get_drvdata(pdev);
4cf1653aa   Wang Chen   netdevice: safe c...
1537
  	struct epic_private *ep = netdev_priv(dev);
f3b197ac2   Jeff Garzik   [netdrvr] trim tr...
1538

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
  	pci_free_consistent(pdev, TX_TOTAL_SIZE, ep->tx_ring, ep->tx_ring_dma);
  	pci_free_consistent(pdev, RX_TOTAL_SIZE, ep->rx_ring, ep->rx_ring_dma);
  	unregister_netdev(dev);
  #ifndef USE_IO_OPS
  	iounmap((void*) dev->base_addr);
  #endif
  	pci_release_regions(pdev);
  	free_netdev(dev);
  	pci_disable_device(pdev);
  	pci_set_drvdata(pdev, NULL);
  	/* pci_power_off(pdev, -1); */
  }
  
  
  #ifdef CONFIG_PM
  
  static int epic_suspend (struct pci_dev *pdev, pm_message_t state)
  {
  	struct net_device *dev = pci_get_drvdata(pdev);
  	long ioaddr = dev->base_addr;
  
  	if (!netif_running(dev))
  		return 0;
  	epic_pause(dev);
  	/* Put the chip into low-power mode. */
  	outl(0x0008, ioaddr + GENCTL);
  	/* pci_power_off(pdev, -1); */
  	return 0;
  }
  
  
  static int epic_resume (struct pci_dev *pdev)
  {
  	struct net_device *dev = pci_get_drvdata(pdev);
  
  	if (!netif_running(dev))
  		return 0;
  	epic_restart(dev);
  	/* pci_power_on(pdev); */
  	return 0;
  }
  
  #endif /* CONFIG_PM */
  
  
  static struct pci_driver epic_driver = {
  	.name		= DRV_NAME,
  	.id_table	= epic_pci_tbl,
  	.probe		= epic_init_one,
  	.remove		= __devexit_p(epic_remove_one),
  #ifdef CONFIG_PM
  	.suspend	= epic_suspend,
  	.resume		= epic_resume,
  #endif /* CONFIG_PM */
  };
  
  
  static int __init epic_init (void)
  {
  /* when a module, this is printed whether or not devices are found in probe */
  #ifdef MODULE
ad361c988   Joe Perches   Remove multiple K...
1600
  	printk (KERN_INFO "%s%s",
2c2a8c531   Markus Dahms   remove broken URL...
1601
  		version, version2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1602
  #endif
299176206   Jeff Garzik   drivers/net: Remo...
1603
  	return pci_register_driver(&epic_driver);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
  }
  
  
  static void __exit epic_cleanup (void)
  {
  	pci_unregister_driver (&epic_driver);
  }
  
  
  module_init(epic_init);
  module_exit(epic_cleanup);