blob: 68e4f660367d5e1795ffbef7a4fb2fe6e75339ac [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: sunlance.c,v 1.112 2002/01/15 06:48:55 davem Exp $
2 * lance.c: Linux/Sparc/Lance driver
3 *
4 * Written 1995, 1996 by Miguel de Icaza
5 * Sources:
6 * The Linux depca driver
7 * The Linux lance driver.
8 * The Linux skeleton driver.
9 * The NetBSD Sparc/Lance driver.
10 * Theo de Raadt (deraadt@openbsd.org)
11 * NCR92C990 Lan Controller manual
12 *
13 * 1.4:
14 * Added support to run with a ledma on the Sun4m
15 *
16 * 1.5:
17 * Added multiple card detection.
18 *
19 * 4/17/96: Burst sizes and tpe selection on sun4m by Eddie C. Dost
20 * (ecd@skynet.be)
21 *
22 * 5/15/96: auto carrier detection on sun4m by Eddie C. Dost
23 * (ecd@skynet.be)
24 *
25 * 5/17/96: lebuffer on scsi/ether cards now work David S. Miller
26 * (davem@caip.rutgers.edu)
27 *
28 * 5/29/96: override option 'tpe-link-test?', if it is 'false', as
29 * this disables auto carrier detection on sun4m. Eddie C. Dost
30 * (ecd@skynet.be)
31 *
32 * 1.7:
33 * 6/26/96: Bug fix for multiple ledmas, miguel.
34 *
35 * 1.8:
36 * Stole multicast code from depca.c, fixed lance_tx.
37 *
38 * 1.9:
39 * 8/21/96: Fixed the multicast code (Pedro Roque)
40 *
41 * 8/28/96: Send fake packet in lance_open() if auto_select is true,
42 * so we can detect the carrier loss condition in time.
43 * Eddie C. Dost (ecd@skynet.be)
44 *
45 * 9/15/96: Align rx_buf so that eth_copy_and_sum() won't cause an
46 * MNA trap during chksum_partial_copy(). (ecd@skynet.be)
47 *
48 * 11/17/96: Handle LE_C0_MERR in lance_interrupt(). (ecd@skynet.be)
49 *
50 * 12/22/96: Don't loop forever in lance_rx() on incomplete packets.
51 * This was the sun4c killer. Shit, stupid bug.
52 * (ecd@skynet.be)
53 *
54 * 1.10:
55 * 1/26/97: Modularize driver. (ecd@skynet.be)
56 *
57 * 1.11:
58 * 12/27/97: Added sun4d support. (jj@sunsite.mff.cuni.cz)
59 *
60 * 1.12:
61 * 11/3/99: Fixed SMP race in lance_start_xmit found by davem.
62 * Anton Blanchard (anton@progsoc.uts.edu.au)
63 * 2.00: 11/9/99: Massive overhaul and port to new SBUS driver interfaces.
64 * David S. Miller (davem@redhat.com)
65 * 2.01:
66 * 11/08/01: Use library crc32 functions (Matt_Domsch@dell.com)
Jeff Garzik6aa20a22006-09-13 13:24:59 -040067 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 */
69
70#undef DEBUG_DRIVER
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static char lancestr[] = "LANCE";
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <linux/module.h>
75#include <linux/kernel.h>
76#include <linux/types.h>
77#include <linux/fcntl.h>
78#include <linux/interrupt.h>
79#include <linux/ioport.h>
80#include <linux/in.h>
81#include <linux/slab.h>
82#include <linux/string.h>
83#include <linux/delay.h>
84#include <linux/init.h>
85#include <linux/crc32.h>
86#include <linux/errno.h>
87#include <linux/socket.h> /* Used for the temporal inet entries and routing */
88#include <linux/route.h>
89#include <linux/netdevice.h>
90#include <linux/etherdevice.h>
91#include <linux/skbuff.h>
92#include <linux/ethtool.h>
93#include <linux/bitops.h>
94
95#include <asm/system.h>
96#include <asm/io.h>
97#include <asm/dma.h>
98#include <asm/pgtable.h>
99#include <asm/byteorder.h> /* Used by the checksum routines */
100#include <asm/idprom.h>
101#include <asm/sbus.h>
David S. Miller76fcdb32007-07-30 18:23:39 -0700102#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#include <asm/auxio.h> /* For tpe-link-test? setting */
104#include <asm/irq.h>
105
Tom 'spot' Callaway10158282005-04-24 20:35:20 -0700106#define DRV_NAME "sunlance"
107#define DRV_VERSION "2.02"
108#define DRV_RELDATE "8/24/03"
109#define DRV_AUTHOR "Miguel de Icaza (miguel@nuclecu.unam.mx)"
110
111static char version[] =
112 DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " " DRV_AUTHOR "\n";
113
114MODULE_VERSION(DRV_VERSION);
115MODULE_AUTHOR(DRV_AUTHOR);
116MODULE_DESCRIPTION("Sun Lance ethernet driver");
117MODULE_LICENSE("GPL");
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/* Define: 2^4 Tx buffers and 2^4 Rx buffers */
120#ifndef LANCE_LOG_TX_BUFFERS
121#define LANCE_LOG_TX_BUFFERS 4
122#define LANCE_LOG_RX_BUFFERS 4
123#endif
124
125#define LE_CSR0 0
126#define LE_CSR1 1
127#define LE_CSR2 2
128#define LE_CSR3 3
129
130#define LE_MO_PROM 0x8000 /* Enable promiscuous mode */
131
132#define LE_C0_ERR 0x8000 /* Error: set if BAB, SQE, MISS or ME is set */
133#define LE_C0_BABL 0x4000 /* BAB: Babble: tx timeout. */
134#define LE_C0_CERR 0x2000 /* SQE: Signal quality error */
135#define LE_C0_MISS 0x1000 /* MISS: Missed a packet */
136#define LE_C0_MERR 0x0800 /* ME: Memory error */
137#define LE_C0_RINT 0x0400 /* Received interrupt */
138#define LE_C0_TINT 0x0200 /* Transmitter Interrupt */
139#define LE_C0_IDON 0x0100 /* IFIN: Init finished. */
140#define LE_C0_INTR 0x0080 /* Interrupt or error */
141#define LE_C0_INEA 0x0040 /* Interrupt enable */
142#define LE_C0_RXON 0x0020 /* Receiver on */
143#define LE_C0_TXON 0x0010 /* Transmitter on */
144#define LE_C0_TDMD 0x0008 /* Transmitter demand */
145#define LE_C0_STOP 0x0004 /* Stop the card */
146#define LE_C0_STRT 0x0002 /* Start the card */
147#define LE_C0_INIT 0x0001 /* Init the card */
148
149#define LE_C3_BSWP 0x4 /* SWAP */
150#define LE_C3_ACON 0x2 /* ALE Control */
151#define LE_C3_BCON 0x1 /* Byte control */
152
153/* Receive message descriptor 1 */
154#define LE_R1_OWN 0x80 /* Who owns the entry */
155#define LE_R1_ERR 0x40 /* Error: if FRA, OFL, CRC or BUF is set */
156#define LE_R1_FRA 0x20 /* FRA: Frame error */
157#define LE_R1_OFL 0x10 /* OFL: Frame overflow */
158#define LE_R1_CRC 0x08 /* CRC error */
159#define LE_R1_BUF 0x04 /* BUF: Buffer error */
160#define LE_R1_SOP 0x02 /* Start of packet */
161#define LE_R1_EOP 0x01 /* End of packet */
162#define LE_R1_POK 0x03 /* Packet is complete: SOP + EOP */
163
164#define LE_T1_OWN 0x80 /* Lance owns the packet */
165#define LE_T1_ERR 0x40 /* Error summary */
166#define LE_T1_EMORE 0x10 /* Error: more than one retry needed */
167#define LE_T1_EONE 0x08 /* Error: one retry needed */
168#define LE_T1_EDEF 0x04 /* Error: deferred */
169#define LE_T1_SOP 0x02 /* Start of packet */
170#define LE_T1_EOP 0x01 /* End of packet */
171#define LE_T1_POK 0x03 /* Packet is complete: SOP + EOP */
172
173#define LE_T3_BUF 0x8000 /* Buffer error */
174#define LE_T3_UFL 0x4000 /* Error underflow */
175#define LE_T3_LCOL 0x1000 /* Error late collision */
176#define LE_T3_CLOS 0x0800 /* Error carrier loss */
177#define LE_T3_RTY 0x0400 /* Error retry */
178#define LE_T3_TDR 0x03ff /* Time Domain Reflectometry counter */
179
180#define TX_RING_SIZE (1 << (LANCE_LOG_TX_BUFFERS))
181#define TX_RING_MOD_MASK (TX_RING_SIZE - 1)
182#define TX_RING_LEN_BITS ((LANCE_LOG_TX_BUFFERS) << 29)
183#define TX_NEXT(__x) (((__x)+1) & TX_RING_MOD_MASK)
184
185#define RX_RING_SIZE (1 << (LANCE_LOG_RX_BUFFERS))
186#define RX_RING_MOD_MASK (RX_RING_SIZE - 1)
187#define RX_RING_LEN_BITS ((LANCE_LOG_RX_BUFFERS) << 29)
188#define RX_NEXT(__x) (((__x)+1) & RX_RING_MOD_MASK)
189
190#define PKT_BUF_SZ 1544
191#define RX_BUFF_SIZE PKT_BUF_SZ
192#define TX_BUFF_SIZE PKT_BUF_SZ
193
194struct lance_rx_desc {
195 u16 rmd0; /* low address of packet */
196 u8 rmd1_bits; /* descriptor bits */
197 u8 rmd1_hadr; /* high address of packet */
198 s16 length; /* This length is 2s complement (negative)!
199 * Buffer length
200 */
201 u16 mblength; /* This is the actual number of bytes received */
202};
203
204struct lance_tx_desc {
205 u16 tmd0; /* low address of packet */
206 u8 tmd1_bits; /* descriptor bits */
207 u8 tmd1_hadr; /* high address of packet */
208 s16 length; /* Length is 2s complement (negative)! */
209 u16 misc;
210};
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/* The LANCE initialization block, described in databook. */
213/* On the Sparc, this block should be on a DMA region */
214struct lance_init_block {
215 u16 mode; /* Pre-set mode (reg. 15) */
216 u8 phys_addr[6]; /* Physical ethernet address */
217 u32 filter[2]; /* Multicast filter. */
218
219 /* Receive and transmit ring base, along with extra bits. */
220 u16 rx_ptr; /* receive descriptor addr */
221 u16 rx_len; /* receive len and high addr */
222 u16 tx_ptr; /* transmit descriptor addr */
223 u16 tx_len; /* transmit len and high addr */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /* The Tx and Rx ring entries must aligned on 8-byte boundaries. */
226 struct lance_rx_desc brx_ring[RX_RING_SIZE];
227 struct lance_tx_desc btx_ring[TX_RING_SIZE];
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 u8 tx_buf [TX_RING_SIZE][TX_BUFF_SIZE];
230 u8 pad[2]; /* align rx_buf for copy_and_sum(). */
231 u8 rx_buf [RX_RING_SIZE][RX_BUFF_SIZE];
232};
233
234#define libdesc_offset(rt, elem) \
235((__u32)(((unsigned long)(&(((struct lance_init_block *)0)->rt[elem])))))
236
237#define libbuff_offset(rt, elem) \
238((__u32)(((unsigned long)(&(((struct lance_init_block *)0)->rt[elem][0])))))
239
240struct lance_private {
241 void __iomem *lregs; /* Lance RAP/RDP regs. */
242 void __iomem *dregs; /* DMA controller regs. */
243 struct lance_init_block __iomem *init_block_iomem;
244 struct lance_init_block *init_block_mem;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 spinlock_t lock;
247
248 int rx_new, tx_new;
249 int rx_old, tx_old;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 struct net_device_stats stats;
252 struct sbus_dma *ledma; /* If set this points to ledma */
253 char tpe; /* cable-selection is TPE */
254 char auto_select; /* cable-selection by carrier */
255 char burst_sizes; /* ledma SBus burst sizes */
256 char pio_buffer; /* init block in PIO space? */
257
258 unsigned short busmaster_regval;
259
260 void (*init_ring)(struct net_device *);
261 void (*rx)(struct net_device *);
262 void (*tx)(struct net_device *);
263
264 char *name;
265 dma_addr_t init_block_dvma;
266 struct net_device *dev; /* Backpointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct sbus_dev *sdev;
268 struct timer_list multicast_timer;
269};
270
271#define TX_BUFFS_AVAIL ((lp->tx_old<=lp->tx_new)?\
272 lp->tx_old+TX_RING_MOD_MASK-lp->tx_new:\
273 lp->tx_old - lp->tx_new-1)
274
275/* Lance registers. */
276#define RDP 0x00UL /* register data port */
277#define RAP 0x02UL /* register address port */
278#define LANCE_REG_SIZE 0x04UL
279
280#define STOP_LANCE(__lp) \
281do { void __iomem *__base = (__lp)->lregs; \
282 sbus_writew(LE_CSR0, __base + RAP); \
283 sbus_writew(LE_C0_STOP, __base + RDP); \
284} while (0)
285
286int sparc_lance_debug = 2;
287
288/* The Lance uses 24 bit addresses */
289/* On the Sun4c the DVMA will provide the remaining bytes for us */
290/* On the Sun4m we have to instruct the ledma to provide them */
291/* Even worse, on scsi/ether SBUS cards, the init block and the
292 * transmit/receive buffers are addresses as offsets from absolute
293 * zero on the lebuffer PIO area. -DaveM
294 */
295
296#define LANCE_ADDR(x) ((long)(x) & ~0xff000000)
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298/* Load the CSR registers */
299static void load_csrs(struct lance_private *lp)
300{
301 u32 leptr;
302
303 if (lp->pio_buffer)
304 leptr = 0;
305 else
306 leptr = LANCE_ADDR(lp->init_block_dvma);
307
308 sbus_writew(LE_CSR1, lp->lregs + RAP);
309 sbus_writew(leptr & 0xffff, lp->lregs + RDP);
310 sbus_writew(LE_CSR2, lp->lregs + RAP);
311 sbus_writew(leptr >> 16, lp->lregs + RDP);
312 sbus_writew(LE_CSR3, lp->lregs + RAP);
313 sbus_writew(lp->busmaster_regval, lp->lregs + RDP);
314
315 /* Point back to csr0 */
316 sbus_writew(LE_CSR0, lp->lregs + RAP);
317}
318
319/* Setup the Lance Rx and Tx rings */
320static void lance_init_ring_dvma(struct net_device *dev)
321{
322 struct lance_private *lp = netdev_priv(dev);
323 struct lance_init_block *ib = lp->init_block_mem;
324 dma_addr_t aib = lp->init_block_dvma;
325 __u32 leptr;
326 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /* Lock out other processes while setting up hardware */
329 netif_stop_queue(dev);
330 lp->rx_new = lp->tx_new = 0;
331 lp->rx_old = lp->tx_old = 0;
332
333 /* Copy the ethernet address to the lance init block
334 * Note that on the sparc you need to swap the ethernet address.
335 */
336 ib->phys_addr [0] = dev->dev_addr [1];
337 ib->phys_addr [1] = dev->dev_addr [0];
338 ib->phys_addr [2] = dev->dev_addr [3];
339 ib->phys_addr [3] = dev->dev_addr [2];
340 ib->phys_addr [4] = dev->dev_addr [5];
341 ib->phys_addr [5] = dev->dev_addr [4];
342
343 /* Setup the Tx ring entries */
344 for (i = 0; i <= TX_RING_SIZE; i++) {
345 leptr = LANCE_ADDR(aib + libbuff_offset(tx_buf, i));
346 ib->btx_ring [i].tmd0 = leptr;
347 ib->btx_ring [i].tmd1_hadr = leptr >> 16;
348 ib->btx_ring [i].tmd1_bits = 0;
349 ib->btx_ring [i].length = 0xf000; /* The ones required by tmd2 */
350 ib->btx_ring [i].misc = 0;
351 }
352
353 /* Setup the Rx ring entries */
354 for (i = 0; i < RX_RING_SIZE; i++) {
355 leptr = LANCE_ADDR(aib + libbuff_offset(rx_buf, i));
356
357 ib->brx_ring [i].rmd0 = leptr;
358 ib->brx_ring [i].rmd1_hadr = leptr >> 16;
359 ib->brx_ring [i].rmd1_bits = LE_R1_OWN;
360 ib->brx_ring [i].length = -RX_BUFF_SIZE | 0xf000;
361 ib->brx_ring [i].mblength = 0;
362 }
363
364 /* Setup the initialization block */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /* Setup rx descriptor pointer */
367 leptr = LANCE_ADDR(aib + libdesc_offset(brx_ring, 0));
368 ib->rx_len = (LANCE_LOG_RX_BUFFERS << 13) | (leptr >> 16);
369 ib->rx_ptr = leptr;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 /* Setup tx descriptor pointer */
372 leptr = LANCE_ADDR(aib + libdesc_offset(btx_ring, 0));
373 ib->tx_len = (LANCE_LOG_TX_BUFFERS << 13) | (leptr >> 16);
374 ib->tx_ptr = leptr;
375}
376
377static void lance_init_ring_pio(struct net_device *dev)
378{
379 struct lance_private *lp = netdev_priv(dev);
380 struct lance_init_block __iomem *ib = lp->init_block_iomem;
381 u32 leptr;
382 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 /* Lock out other processes while setting up hardware */
385 netif_stop_queue(dev);
386 lp->rx_new = lp->tx_new = 0;
387 lp->rx_old = lp->tx_old = 0;
388
389 /* Copy the ethernet address to the lance init block
390 * Note that on the sparc you need to swap the ethernet address.
391 */
392 sbus_writeb(dev->dev_addr[1], &ib->phys_addr[0]);
393 sbus_writeb(dev->dev_addr[0], &ib->phys_addr[1]);
394 sbus_writeb(dev->dev_addr[3], &ib->phys_addr[2]);
395 sbus_writeb(dev->dev_addr[2], &ib->phys_addr[3]);
396 sbus_writeb(dev->dev_addr[5], &ib->phys_addr[4]);
397 sbus_writeb(dev->dev_addr[4], &ib->phys_addr[5]);
398
399 /* Setup the Tx ring entries */
400 for (i = 0; i <= TX_RING_SIZE; i++) {
401 leptr = libbuff_offset(tx_buf, i);
402 sbus_writew(leptr, &ib->btx_ring [i].tmd0);
403 sbus_writeb(leptr >> 16,&ib->btx_ring [i].tmd1_hadr);
404 sbus_writeb(0, &ib->btx_ring [i].tmd1_bits);
405
406 /* The ones required by tmd2 */
407 sbus_writew(0xf000, &ib->btx_ring [i].length);
408 sbus_writew(0, &ib->btx_ring [i].misc);
409 }
410
411 /* Setup the Rx ring entries */
412 for (i = 0; i < RX_RING_SIZE; i++) {
413 leptr = libbuff_offset(rx_buf, i);
414
415 sbus_writew(leptr, &ib->brx_ring [i].rmd0);
416 sbus_writeb(leptr >> 16,&ib->brx_ring [i].rmd1_hadr);
417 sbus_writeb(LE_R1_OWN, &ib->brx_ring [i].rmd1_bits);
418 sbus_writew(-RX_BUFF_SIZE|0xf000,
419 &ib->brx_ring [i].length);
420 sbus_writew(0, &ib->brx_ring [i].mblength);
421 }
422
423 /* Setup the initialization block */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 /* Setup rx descriptor pointer */
426 leptr = libdesc_offset(brx_ring, 0);
427 sbus_writew((LANCE_LOG_RX_BUFFERS << 13) | (leptr >> 16),
428 &ib->rx_len);
429 sbus_writew(leptr, &ib->rx_ptr);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /* Setup tx descriptor pointer */
432 leptr = libdesc_offset(btx_ring, 0);
433 sbus_writew((LANCE_LOG_TX_BUFFERS << 13) | (leptr >> 16),
434 &ib->tx_len);
435 sbus_writew(leptr, &ib->tx_ptr);
436}
437
438static void init_restart_ledma(struct lance_private *lp)
439{
440 u32 csr = sbus_readl(lp->dregs + DMA_CSR);
441
442 if (!(csr & DMA_HNDL_ERROR)) {
443 /* E-Cache draining */
444 while (sbus_readl(lp->dregs + DMA_CSR) & DMA_FIFO_ISDRAIN)
445 barrier();
446 }
447
448 csr = sbus_readl(lp->dregs + DMA_CSR);
449 csr &= ~DMA_E_BURSTS;
450 if (lp->burst_sizes & DMA_BURST32)
451 csr |= DMA_E_BURST32;
452 else
453 csr |= DMA_E_BURST16;
454
455 csr |= (DMA_DSBL_RD_DRN | DMA_DSBL_WR_INV | DMA_FIFO_INV);
456
457 if (lp->tpe)
458 csr |= DMA_EN_ENETAUI;
459 else
460 csr &= ~DMA_EN_ENETAUI;
461 udelay(20);
462 sbus_writel(csr, lp->dregs + DMA_CSR);
463 udelay(200);
464}
465
466static int init_restart_lance(struct lance_private *lp)
467{
468 u16 regval = 0;
469 int i;
470
471 if (lp->dregs)
472 init_restart_ledma(lp);
473
474 sbus_writew(LE_CSR0, lp->lregs + RAP);
475 sbus_writew(LE_C0_INIT, lp->lregs + RDP);
476
477 /* Wait for the lance to complete initialization */
478 for (i = 0; i < 100; i++) {
479 regval = sbus_readw(lp->lregs + RDP);
480
481 if (regval & (LE_C0_ERR | LE_C0_IDON))
482 break;
483 barrier();
484 }
485 if (i == 100 || (regval & LE_C0_ERR)) {
486 printk(KERN_ERR "LANCE unopened after %d ticks, csr0=%4.4x.\n",
487 i, regval);
488 if (lp->dregs)
489 printk("dcsr=%8.8x\n", sbus_readl(lp->dregs + DMA_CSR));
490 return -1;
491 }
492
493 /* Clear IDON by writing a "1", enable interrupts and start lance */
494 sbus_writew(LE_C0_IDON, lp->lregs + RDP);
495 sbus_writew(LE_C0_INEA | LE_C0_STRT, lp->lregs + RDP);
496
497 if (lp->dregs) {
498 u32 csr = sbus_readl(lp->dregs + DMA_CSR);
499
500 csr |= DMA_INT_ENAB;
501 sbus_writel(csr, lp->dregs + DMA_CSR);
502 }
503
504 return 0;
505}
506
507static void lance_rx_dvma(struct net_device *dev)
508{
509 struct lance_private *lp = netdev_priv(dev);
510 struct lance_init_block *ib = lp->init_block_mem;
511 struct lance_rx_desc *rd;
512 u8 bits;
513 int len, entry = lp->rx_new;
514 struct sk_buff *skb;
515
516 for (rd = &ib->brx_ring [entry];
517 !((bits = rd->rmd1_bits) & LE_R1_OWN);
518 rd = &ib->brx_ring [entry]) {
519
520 /* We got an incomplete frame? */
521 if ((bits & LE_R1_POK) != LE_R1_POK) {
522 lp->stats.rx_over_errors++;
523 lp->stats.rx_errors++;
524 } else if (bits & LE_R1_ERR) {
525 /* Count only the end frame as a rx error,
526 * not the beginning
527 */
528 if (bits & LE_R1_BUF) lp->stats.rx_fifo_errors++;
529 if (bits & LE_R1_CRC) lp->stats.rx_crc_errors++;
530 if (bits & LE_R1_OFL) lp->stats.rx_over_errors++;
531 if (bits & LE_R1_FRA) lp->stats.rx_frame_errors++;
532 if (bits & LE_R1_EOP) lp->stats.rx_errors++;
533 } else {
534 len = (rd->mblength & 0xfff) - 4;
535 skb = dev_alloc_skb(len + 2);
536
537 if (skb == NULL) {
538 printk(KERN_INFO "%s: Memory squeeze, deferring packet.\n",
539 dev->name);
540 lp->stats.rx_dropped++;
541 rd->mblength = 0;
542 rd->rmd1_bits = LE_R1_OWN;
543 lp->rx_new = RX_NEXT(entry);
544 return;
545 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 lp->stats.rx_bytes += len;
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 skb_reserve(skb, 2); /* 16 byte align */
550 skb_put(skb, len); /* make room */
David S. Miller8c7b7fa2007-07-10 22:08:12 -0700551 skb_copy_to_linear_data(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 (unsigned char *)&(ib->rx_buf [entry][0]),
David S. Miller8c7b7fa2007-07-10 22:08:12 -0700553 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 skb->protocol = eth_type_trans(skb, dev);
555 netif_rx(skb);
556 dev->last_rx = jiffies;
557 lp->stats.rx_packets++;
558 }
559
560 /* Return the packet to the pool */
561 rd->mblength = 0;
562 rd->rmd1_bits = LE_R1_OWN;
563 entry = RX_NEXT(entry);
564 }
565
566 lp->rx_new = entry;
567}
568
569static void lance_tx_dvma(struct net_device *dev)
570{
571 struct lance_private *lp = netdev_priv(dev);
572 struct lance_init_block *ib = lp->init_block_mem;
573 int i, j;
574
575 spin_lock(&lp->lock);
576
577 j = lp->tx_old;
578 for (i = j; i != lp->tx_new; i = j) {
579 struct lance_tx_desc *td = &ib->btx_ring [i];
580 u8 bits = td->tmd1_bits;
581
582 /* If we hit a packet not owned by us, stop */
583 if (bits & LE_T1_OWN)
584 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (bits & LE_T1_ERR) {
587 u16 status = td->misc;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 lp->stats.tx_errors++;
590 if (status & LE_T3_RTY) lp->stats.tx_aborted_errors++;
591 if (status & LE_T3_LCOL) lp->stats.tx_window_errors++;
592
593 if (status & LE_T3_CLOS) {
594 lp->stats.tx_carrier_errors++;
595 if (lp->auto_select) {
596 lp->tpe = 1 - lp->tpe;
597 printk(KERN_NOTICE "%s: Carrier Lost, trying %s\n",
598 dev->name, lp->tpe?"TPE":"AUI");
599 STOP_LANCE(lp);
600 lp->init_ring(dev);
601 load_csrs(lp);
602 init_restart_lance(lp);
603 goto out;
604 }
605 }
606
607 /* Buffer errors and underflows turn off the
608 * transmitter, restart the adapter.
609 */
610 if (status & (LE_T3_BUF|LE_T3_UFL)) {
611 lp->stats.tx_fifo_errors++;
612
613 printk(KERN_ERR "%s: Tx: ERR_BUF|ERR_UFL, restarting\n",
614 dev->name);
615 STOP_LANCE(lp);
616 lp->init_ring(dev);
617 load_csrs(lp);
618 init_restart_lance(lp);
619 goto out;
620 }
621 } else if ((bits & LE_T1_POK) == LE_T1_POK) {
622 /*
623 * So we don't count the packet more than once.
624 */
625 td->tmd1_bits = bits & ~(LE_T1_POK);
626
627 /* One collision before packet was sent. */
628 if (bits & LE_T1_EONE)
629 lp->stats.collisions++;
630
631 /* More than one collision, be optimistic. */
632 if (bits & LE_T1_EMORE)
633 lp->stats.collisions += 2;
634
635 lp->stats.tx_packets++;
636 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 j = TX_NEXT(j);
639 }
640 lp->tx_old = j;
641out:
642 if (netif_queue_stopped(dev) &&
643 TX_BUFFS_AVAIL > 0)
644 netif_wake_queue(dev);
645
646 spin_unlock(&lp->lock);
647}
648
649static void lance_piocopy_to_skb(struct sk_buff *skb, void __iomem *piobuf, int len)
650{
651 u16 *p16 = (u16 *) skb->data;
652 u32 *p32;
653 u8 *p8;
654 void __iomem *pbuf = piobuf;
655
656 /* We know here that both src and dest are on a 16bit boundary. */
657 *p16++ = sbus_readw(pbuf);
658 p32 = (u32 *) p16;
659 pbuf += 2;
660 len -= 2;
661
662 while (len >= 4) {
663 *p32++ = sbus_readl(pbuf);
664 pbuf += 4;
665 len -= 4;
666 }
667 p8 = (u8 *) p32;
668 if (len >= 2) {
669 p16 = (u16 *) p32;
670 *p16++ = sbus_readw(pbuf);
671 pbuf += 2;
672 len -= 2;
673 p8 = (u8 *) p16;
674 }
675 if (len >= 1)
676 *p8 = sbus_readb(pbuf);
677}
678
679static void lance_rx_pio(struct net_device *dev)
680{
681 struct lance_private *lp = netdev_priv(dev);
682 struct lance_init_block __iomem *ib = lp->init_block_iomem;
683 struct lance_rx_desc __iomem *rd;
684 unsigned char bits;
685 int len, entry;
686 struct sk_buff *skb;
687
688 entry = lp->rx_new;
689 for (rd = &ib->brx_ring [entry];
690 !((bits = sbus_readb(&rd->rmd1_bits)) & LE_R1_OWN);
691 rd = &ib->brx_ring [entry]) {
692
693 /* We got an incomplete frame? */
694 if ((bits & LE_R1_POK) != LE_R1_POK) {
695 lp->stats.rx_over_errors++;
696 lp->stats.rx_errors++;
697 } else if (bits & LE_R1_ERR) {
698 /* Count only the end frame as a rx error,
699 * not the beginning
700 */
701 if (bits & LE_R1_BUF) lp->stats.rx_fifo_errors++;
702 if (bits & LE_R1_CRC) lp->stats.rx_crc_errors++;
703 if (bits & LE_R1_OFL) lp->stats.rx_over_errors++;
704 if (bits & LE_R1_FRA) lp->stats.rx_frame_errors++;
705 if (bits & LE_R1_EOP) lp->stats.rx_errors++;
706 } else {
707 len = (sbus_readw(&rd->mblength) & 0xfff) - 4;
708 skb = dev_alloc_skb(len + 2);
709
710 if (skb == NULL) {
711 printk(KERN_INFO "%s: Memory squeeze, deferring packet.\n",
712 dev->name);
713 lp->stats.rx_dropped++;
714 sbus_writew(0, &rd->mblength);
715 sbus_writeb(LE_R1_OWN, &rd->rmd1_bits);
716 lp->rx_new = RX_NEXT(entry);
717 return;
718 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 lp->stats.rx_bytes += len;
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 skb_reserve (skb, 2); /* 16 byte align */
723 skb_put(skb, len); /* make room */
724 lance_piocopy_to_skb(skb, &(ib->rx_buf[entry][0]), len);
725 skb->protocol = eth_type_trans(skb, dev);
726 netif_rx(skb);
727 dev->last_rx = jiffies;
728 lp->stats.rx_packets++;
729 }
730
731 /* Return the packet to the pool */
732 sbus_writew(0, &rd->mblength);
733 sbus_writeb(LE_R1_OWN, &rd->rmd1_bits);
734 entry = RX_NEXT(entry);
735 }
736
737 lp->rx_new = entry;
738}
739
740static void lance_tx_pio(struct net_device *dev)
741{
742 struct lance_private *lp = netdev_priv(dev);
743 struct lance_init_block __iomem *ib = lp->init_block_iomem;
744 int i, j;
745
746 spin_lock(&lp->lock);
747
748 j = lp->tx_old;
749 for (i = j; i != lp->tx_new; i = j) {
750 struct lance_tx_desc __iomem *td = &ib->btx_ring [i];
751 u8 bits = sbus_readb(&td->tmd1_bits);
752
753 /* If we hit a packet not owned by us, stop */
754 if (bits & LE_T1_OWN)
755 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (bits & LE_T1_ERR) {
758 u16 status = sbus_readw(&td->misc);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 lp->stats.tx_errors++;
761 if (status & LE_T3_RTY) lp->stats.tx_aborted_errors++;
762 if (status & LE_T3_LCOL) lp->stats.tx_window_errors++;
763
764 if (status & LE_T3_CLOS) {
765 lp->stats.tx_carrier_errors++;
766 if (lp->auto_select) {
767 lp->tpe = 1 - lp->tpe;
768 printk(KERN_NOTICE "%s: Carrier Lost, trying %s\n",
769 dev->name, lp->tpe?"TPE":"AUI");
770 STOP_LANCE(lp);
771 lp->init_ring(dev);
772 load_csrs(lp);
773 init_restart_lance(lp);
774 goto out;
775 }
776 }
777
778 /* Buffer errors and underflows turn off the
779 * transmitter, restart the adapter.
780 */
781 if (status & (LE_T3_BUF|LE_T3_UFL)) {
782 lp->stats.tx_fifo_errors++;
783
784 printk(KERN_ERR "%s: Tx: ERR_BUF|ERR_UFL, restarting\n",
785 dev->name);
786 STOP_LANCE(lp);
787 lp->init_ring(dev);
788 load_csrs(lp);
789 init_restart_lance(lp);
790 goto out;
791 }
792 } else if ((bits & LE_T1_POK) == LE_T1_POK) {
793 /*
794 * So we don't count the packet more than once.
795 */
796 sbus_writeb(bits & ~(LE_T1_POK), &td->tmd1_bits);
797
798 /* One collision before packet was sent. */
799 if (bits & LE_T1_EONE)
800 lp->stats.collisions++;
801
802 /* More than one collision, be optimistic. */
803 if (bits & LE_T1_EMORE)
804 lp->stats.collisions += 2;
805
806 lp->stats.tx_packets++;
807 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 j = TX_NEXT(j);
810 }
811 lp->tx_old = j;
812
813 if (netif_queue_stopped(dev) &&
814 TX_BUFFS_AVAIL > 0)
815 netif_wake_queue(dev);
816out:
817 spin_unlock(&lp->lock);
818}
819
David Howells7d12e782006-10-05 14:55:46 +0100820static irqreturn_t lance_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Jeff Garzikc31f28e2006-10-06 14:56:04 -0400822 struct net_device *dev = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 struct lance_private *lp = netdev_priv(dev);
824 int csr0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 sbus_writew(LE_CSR0, lp->lregs + RAP);
827 csr0 = sbus_readw(lp->lregs + RDP);
828
829 /* Acknowledge all the interrupt sources ASAP */
830 sbus_writew(csr0 & (LE_C0_INTR | LE_C0_TINT | LE_C0_RINT),
831 lp->lregs + RDP);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if ((csr0 & LE_C0_ERR) != 0) {
834 /* Clear the error condition */
835 sbus_writew((LE_C0_BABL | LE_C0_ERR | LE_C0_MISS |
836 LE_C0_CERR | LE_C0_MERR),
837 lp->lregs + RDP);
838 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (csr0 & LE_C0_RINT)
841 lp->rx(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (csr0 & LE_C0_TINT)
844 lp->tx(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (csr0 & LE_C0_BABL)
847 lp->stats.tx_errors++;
848
849 if (csr0 & LE_C0_MISS)
850 lp->stats.rx_errors++;
851
852 if (csr0 & LE_C0_MERR) {
853 if (lp->dregs) {
854 u32 addr = sbus_readl(lp->dregs + DMA_ADDR);
855
856 printk(KERN_ERR "%s: Memory error, status %04x, addr %06x\n",
857 dev->name, csr0, addr & 0xffffff);
858 } else {
859 printk(KERN_ERR "%s: Memory error, status %04x\n",
860 dev->name, csr0);
861 }
862
863 sbus_writew(LE_C0_STOP, lp->lregs + RDP);
864
865 if (lp->dregs) {
866 u32 dma_csr = sbus_readl(lp->dregs + DMA_CSR);
867
868 dma_csr |= DMA_FIFO_INV;
869 sbus_writel(dma_csr, lp->dregs + DMA_CSR);
870 }
871
872 lp->init_ring(dev);
873 load_csrs(lp);
874 init_restart_lance(lp);
875 netif_wake_queue(dev);
876 }
877
878 sbus_writew(LE_C0_INEA, lp->lregs + RDP);
879
880 return IRQ_HANDLED;
881}
882
883/* Build a fake network packet and send it to ourselves. */
884static void build_fake_packet(struct lance_private *lp)
885{
886 struct net_device *dev = lp->dev;
887 int i, entry;
888
889 entry = lp->tx_new & TX_RING_MOD_MASK;
890 if (lp->pio_buffer) {
891 struct lance_init_block __iomem *ib = lp->init_block_iomem;
892 u16 __iomem *packet = (u16 __iomem *) &(ib->tx_buf[entry][0]);
893 struct ethhdr __iomem *eth = (struct ethhdr __iomem *) packet;
894 for (i = 0; i < (ETH_ZLEN / sizeof(u16)); i++)
895 sbus_writew(0, &packet[i]);
896 for (i = 0; i < 6; i++) {
897 sbus_writeb(dev->dev_addr[i], &eth->h_dest[i]);
898 sbus_writeb(dev->dev_addr[i], &eth->h_source[i]);
899 }
900 sbus_writew((-ETH_ZLEN) | 0xf000, &ib->btx_ring[entry].length);
901 sbus_writew(0, &ib->btx_ring[entry].misc);
902 sbus_writeb(LE_T1_POK|LE_T1_OWN, &ib->btx_ring[entry].tmd1_bits);
903 } else {
904 struct lance_init_block *ib = lp->init_block_mem;
905 u16 *packet = (u16 *) &(ib->tx_buf[entry][0]);
906 struct ethhdr *eth = (struct ethhdr *) packet;
907 memset(packet, 0, ETH_ZLEN);
908 for (i = 0; i < 6; i++) {
909 eth->h_dest[i] = dev->dev_addr[i];
910 eth->h_source[i] = dev->dev_addr[i];
911 }
912 ib->btx_ring[entry].length = (-ETH_ZLEN) | 0xf000;
913 ib->btx_ring[entry].misc = 0;
914 ib->btx_ring[entry].tmd1_bits = (LE_T1_POK|LE_T1_OWN);
915 }
916 lp->tx_new = TX_NEXT(entry);
917}
918
919struct net_device *last_dev;
920
921static int lance_open(struct net_device *dev)
922{
923 struct lance_private *lp = netdev_priv(dev);
924 int status = 0;
925
926 last_dev = dev;
927
928 STOP_LANCE(lp);
929
Thomas Gleixner1fb9df52006-07-01 19:29:39 -0700930 if (request_irq(dev->irq, &lance_interrupt, IRQF_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 lancestr, (void *) dev)) {
David S. Millerc6387a42006-06-20 01:21:29 -0700932 printk(KERN_ERR "Lance: Can't get irq %d\n", dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return -EAGAIN;
934 }
935
936 /* On the 4m, setup the ledma to provide the upper bits for buffers */
937 if (lp->dregs) {
938 u32 regval = lp->init_block_dvma & 0xff000000;
939
940 sbus_writel(regval, lp->dregs + DMA_TEST);
941 }
942
943 /* Set mode and clear multicast filter only at device open,
944 * so that lance_init_ring() called at any error will not
945 * forget multicast filters.
946 *
947 * BTW it is common bug in all lance drivers! --ANK
948 */
949 if (lp->pio_buffer) {
950 struct lance_init_block __iomem *ib = lp->init_block_iomem;
951 sbus_writew(0, &ib->mode);
952 sbus_writel(0, &ib->filter[0]);
953 sbus_writel(0, &ib->filter[1]);
954 } else {
955 struct lance_init_block *ib = lp->init_block_mem;
956 ib->mode = 0;
957 ib->filter [0] = 0;
958 ib->filter [1] = 0;
959 }
960
961 lp->init_ring(dev);
962 load_csrs(lp);
963
964 netif_start_queue(dev);
965
966 status = init_restart_lance(lp);
967 if (!status && lp->auto_select) {
968 build_fake_packet(lp);
969 sbus_writew(LE_C0_INEA | LE_C0_TDMD, lp->lregs + RDP);
970 }
971
972 return status;
973}
974
975static int lance_close(struct net_device *dev)
976{
977 struct lance_private *lp = netdev_priv(dev);
978
979 netif_stop_queue(dev);
980 del_timer_sync(&lp->multicast_timer);
981
982 STOP_LANCE(lp);
983
984 free_irq(dev->irq, (void *) dev);
985 return 0;
986}
987
988static int lance_reset(struct net_device *dev)
989{
990 struct lance_private *lp = netdev_priv(dev);
991 int status;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 STOP_LANCE(lp);
994
995 /* On the 4m, reset the dma too */
996 if (lp->dregs) {
997 u32 csr, addr;
998
999 printk(KERN_ERR "resetting ledma\n");
1000 csr = sbus_readl(lp->dregs + DMA_CSR);
1001 sbus_writel(csr | DMA_RST_ENET, lp->dregs + DMA_CSR);
1002 udelay(200);
1003 sbus_writel(csr & ~DMA_RST_ENET, lp->dregs + DMA_CSR);
1004
1005 addr = lp->init_block_dvma & 0xff000000;
1006 sbus_writel(addr, lp->dregs + DMA_TEST);
1007 }
1008 lp->init_ring(dev);
1009 load_csrs(lp);
1010 dev->trans_start = jiffies;
1011 status = init_restart_lance(lp);
1012 return status;
1013}
1014
1015static void lance_piocopy_from_skb(void __iomem *dest, unsigned char *src, int len)
1016{
1017 void __iomem *piobuf = dest;
1018 u32 *p32;
1019 u16 *p16;
1020 u8 *p8;
1021
1022 switch ((unsigned long)src & 0x3) {
1023 case 0:
1024 p32 = (u32 *) src;
1025 while (len >= 4) {
1026 sbus_writel(*p32, piobuf);
1027 p32++;
1028 piobuf += 4;
1029 len -= 4;
1030 }
1031 src = (char *) p32;
1032 break;
1033 case 1:
1034 case 3:
1035 p8 = (u8 *) src;
1036 while (len >= 4) {
1037 u32 val;
1038
1039 val = p8[0] << 24;
1040 val |= p8[1] << 16;
1041 val |= p8[2] << 8;
1042 val |= p8[3];
1043 sbus_writel(val, piobuf);
1044 p8 += 4;
1045 piobuf += 4;
1046 len -= 4;
1047 }
1048 src = (char *) p8;
1049 break;
1050 case 2:
1051 p16 = (u16 *) src;
1052 while (len >= 4) {
1053 u32 val = p16[0]<<16 | p16[1];
1054 sbus_writel(val, piobuf);
1055 p16 += 2;
1056 piobuf += 4;
1057 len -= 4;
1058 }
1059 src = (char *) p16;
1060 break;
1061 };
1062 if (len >= 2) {
1063 u16 val = src[0] << 8 | src[1];
1064 sbus_writew(val, piobuf);
1065 src += 2;
1066 piobuf += 2;
1067 len -= 2;
1068 }
1069 if (len >= 1)
1070 sbus_writeb(src[0], piobuf);
1071}
1072
1073static void lance_piozero(void __iomem *dest, int len)
1074{
1075 void __iomem *piobuf = dest;
1076
1077 if ((unsigned long)piobuf & 1) {
1078 sbus_writeb(0, piobuf);
1079 piobuf += 1;
1080 len -= 1;
1081 if (len == 0)
1082 return;
1083 }
1084 if (len == 1) {
1085 sbus_writeb(0, piobuf);
1086 return;
1087 }
1088 if ((unsigned long)piobuf & 2) {
1089 sbus_writew(0, piobuf);
1090 piobuf += 2;
1091 len -= 2;
1092 if (len == 0)
1093 return;
1094 }
1095 while (len >= 4) {
1096 sbus_writel(0, piobuf);
1097 piobuf += 4;
1098 len -= 4;
1099 }
1100 if (len >= 2) {
1101 sbus_writew(0, piobuf);
1102 piobuf += 2;
1103 len -= 2;
1104 }
1105 if (len >= 1)
1106 sbus_writeb(0, piobuf);
1107}
1108
1109static void lance_tx_timeout(struct net_device *dev)
1110{
1111 struct lance_private *lp = netdev_priv(dev);
1112
1113 printk(KERN_ERR "%s: transmit timed out, status %04x, reset\n",
1114 dev->name, sbus_readw(lp->lregs + RDP));
1115 lance_reset(dev);
1116 netif_wake_queue(dev);
1117}
1118
1119static int lance_start_xmit(struct sk_buff *skb, struct net_device *dev)
1120{
1121 struct lance_private *lp = netdev_priv(dev);
1122 int entry, skblen, len;
1123
1124 skblen = skb->len;
1125
1126 len = (skblen <= ETH_ZLEN) ? ETH_ZLEN : skblen;
1127
1128 spin_lock_irq(&lp->lock);
1129
1130 lp->stats.tx_bytes += len;
1131
1132 entry = lp->tx_new & TX_RING_MOD_MASK;
1133 if (lp->pio_buffer) {
1134 struct lance_init_block __iomem *ib = lp->init_block_iomem;
1135 sbus_writew((-len) | 0xf000, &ib->btx_ring[entry].length);
1136 sbus_writew(0, &ib->btx_ring[entry].misc);
1137 lance_piocopy_from_skb(&ib->tx_buf[entry][0], skb->data, skblen);
1138 if (len != skblen)
1139 lance_piozero(&ib->tx_buf[entry][skblen], len - skblen);
1140 sbus_writeb(LE_T1_POK | LE_T1_OWN, &ib->btx_ring[entry].tmd1_bits);
1141 } else {
1142 struct lance_init_block *ib = lp->init_block_mem;
1143 ib->btx_ring [entry].length = (-len) | 0xf000;
1144 ib->btx_ring [entry].misc = 0;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001145 skb_copy_from_linear_data(skb, &ib->tx_buf [entry][0], skblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (len != skblen)
1147 memset((char *) &ib->tx_buf [entry][skblen], 0, len - skblen);
1148 ib->btx_ring [entry].tmd1_bits = (LE_T1_POK | LE_T1_OWN);
1149 }
1150
1151 lp->tx_new = TX_NEXT(entry);
1152
1153 if (TX_BUFFS_AVAIL <= 0)
1154 netif_stop_queue(dev);
1155
1156 /* Kick the lance: transmit now */
1157 sbus_writew(LE_C0_INEA | LE_C0_TDMD, lp->lregs + RDP);
1158
1159 /* Read back CSR to invalidate the E-Cache.
1160 * This is needed, because DMA_DSBL_WR_INV is set.
1161 */
1162 if (lp->dregs)
1163 sbus_readw(lp->lregs + RDP);
1164
1165 spin_unlock_irq(&lp->lock);
1166
1167 dev->trans_start = jiffies;
1168 dev_kfree_skb(skb);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return 0;
1171}
1172
1173static struct net_device_stats *lance_get_stats(struct net_device *dev)
1174{
1175 struct lance_private *lp = netdev_priv(dev);
1176
1177 return &lp->stats;
1178}
1179
1180/* taken from the depca driver */
1181static void lance_load_multicast(struct net_device *dev)
1182{
1183 struct lance_private *lp = netdev_priv(dev);
1184 struct dev_mc_list *dmi = dev->mc_list;
1185 char *addrs;
1186 int i;
1187 u32 crc;
1188 u32 val;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 /* set all multicast bits */
1191 if (dev->flags & IFF_ALLMULTI)
1192 val = ~0;
1193 else
1194 val = 0;
1195
1196 if (lp->pio_buffer) {
1197 struct lance_init_block __iomem *ib = lp->init_block_iomem;
1198 sbus_writel(val, &ib->filter[0]);
1199 sbus_writel(val, &ib->filter[1]);
1200 } else {
1201 struct lance_init_block *ib = lp->init_block_mem;
1202 ib->filter [0] = val;
1203 ib->filter [1] = val;
1204 }
1205
1206 if (dev->flags & IFF_ALLMULTI)
1207 return;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 /* Add addresses */
1210 for (i = 0; i < dev->mc_count; i++) {
1211 addrs = dmi->dmi_addr;
1212 dmi = dmi->next;
1213
1214 /* multicast address? */
1215 if (!(*addrs & 1))
1216 continue;
1217 crc = ether_crc_le(6, addrs);
1218 crc = crc >> 26;
1219 if (lp->pio_buffer) {
1220 struct lance_init_block __iomem *ib = lp->init_block_iomem;
1221 u16 __iomem *mcast_table = (u16 __iomem *) &ib->filter;
1222 u16 tmp = sbus_readw(&mcast_table[crc>>4]);
1223 tmp |= 1 << (crc & 0xf);
1224 sbus_writew(tmp, &mcast_table[crc>>4]);
1225 } else {
1226 struct lance_init_block *ib = lp->init_block_mem;
1227 u16 *mcast_table = (u16 *) &ib->filter;
1228 mcast_table [crc >> 4] |= 1 << (crc & 0xf);
1229 }
1230 }
1231}
1232
1233static void lance_set_multicast(struct net_device *dev)
1234{
1235 struct lance_private *lp = netdev_priv(dev);
1236 struct lance_init_block *ib_mem = lp->init_block_mem;
1237 struct lance_init_block __iomem *ib_iomem = lp->init_block_iomem;
1238 u16 mode;
1239
1240 if (!netif_running(dev))
1241 return;
1242
1243 if (lp->tx_old != lp->tx_new) {
1244 mod_timer(&lp->multicast_timer, jiffies + 4);
1245 netif_wake_queue(dev);
1246 return;
1247 }
1248
1249 netif_stop_queue(dev);
1250
1251 STOP_LANCE(lp);
1252 lp->init_ring(dev);
1253
1254 if (lp->pio_buffer)
1255 mode = sbus_readw(&ib_iomem->mode);
1256 else
1257 mode = ib_mem->mode;
1258 if (dev->flags & IFF_PROMISC) {
1259 mode |= LE_MO_PROM;
1260 if (lp->pio_buffer)
1261 sbus_writew(mode, &ib_iomem->mode);
1262 else
1263 ib_mem->mode = mode;
1264 } else {
1265 mode &= ~LE_MO_PROM;
1266 if (lp->pio_buffer)
1267 sbus_writew(mode, &ib_iomem->mode);
1268 else
1269 ib_mem->mode = mode;
1270 lance_load_multicast(dev);
1271 }
1272 load_csrs(lp);
1273 init_restart_lance(lp);
1274 netif_wake_queue(dev);
1275}
1276
1277static void lance_set_multicast_retry(unsigned long _opaque)
1278{
1279 struct net_device *dev = (struct net_device *) _opaque;
1280
1281 lance_set_multicast(dev);
1282}
1283
1284static void lance_free_hwresources(struct lance_private *lp)
1285{
1286 if (lp->lregs)
1287 sbus_iounmap(lp->lregs, LANCE_REG_SIZE);
1288 if (lp->init_block_iomem) {
1289 sbus_iounmap(lp->init_block_iomem,
1290 sizeof(struct lance_init_block));
1291 } else if (lp->init_block_mem) {
1292 sbus_free_consistent(lp->sdev,
1293 sizeof(struct lance_init_block),
1294 lp->init_block_mem,
1295 lp->init_block_dvma);
1296 }
1297}
1298
1299/* Ethtool support... */
1300static void sparc_lance_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1301{
1302 struct lance_private *lp = netdev_priv(dev);
1303
1304 strcpy(info->driver, "sunlance");
1305 strcpy(info->version, "2.02");
1306 sprintf(info->bus_info, "SBUS:%d",
1307 lp->sdev->slot);
1308}
1309
1310static u32 sparc_lance_get_link(struct net_device *dev)
1311{
1312 /* We really do not keep track of this, but this
1313 * is better than not reporting anything at all.
1314 */
1315 return 1;
1316}
1317
Jeff Garzik7282d492006-09-13 14:30:00 -04001318static const struct ethtool_ops sparc_lance_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 .get_drvinfo = sparc_lance_get_drvinfo,
1320 .get_link = sparc_lance_get_link,
1321};
1322
David S. Millerefdbc1a2006-09-25 14:04:49 -07001323static int __devinit sparc_lance_probe_one(struct sbus_dev *sdev,
1324 struct sbus_dma *ledma,
1325 struct sbus_dev *lebuffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 static unsigned version_printed;
David S. Miller76fcdb32007-07-30 18:23:39 -07001328 struct device_node *dp = sdev->ofdev.node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 struct net_device *dev;
1330 struct lance_private *lp;
1331 int i;
1332
1333 dev = alloc_etherdev(sizeof(struct lance_private) + 8);
1334 if (!dev)
1335 return -ENOMEM;
1336
1337 lp = netdev_priv(dev);
1338 memset(lp, 0, sizeof(*lp));
1339
1340 if (sparc_lance_debug && version_printed++ == 0)
1341 printk (KERN_INFO "%s", version);
1342
1343 spin_lock_init(&lp->lock);
1344
1345 /* Copy the IDPROM ethernet address to the device structure, later we
1346 * will copy the address in the device structure to the lance
1347 * initialization block.
1348 */
1349 for (i = 0; i < 6; i++)
1350 dev->dev_addr[i] = idprom->id_ethaddr[i];
1351
1352 /* Get the IO region */
1353 lp->lregs = sbus_ioremap(&sdev->resource[0], 0,
1354 LANCE_REG_SIZE, lancestr);
1355 if (!lp->lregs) {
1356 printk(KERN_ERR "SunLance: Cannot map registers.\n");
1357 goto fail;
1358 }
1359
1360 lp->sdev = sdev;
1361 if (lebuffer) {
1362 /* sanity check */
1363 if (lebuffer->resource[0].start & 7) {
1364 printk(KERN_ERR "SunLance: ERROR: Rx and Tx rings not on even boundary.\n");
1365 goto fail;
1366 }
1367 lp->init_block_iomem =
1368 sbus_ioremap(&lebuffer->resource[0], 0,
1369 sizeof(struct lance_init_block), "lebuffer");
1370 if (!lp->init_block_iomem) {
1371 printk(KERN_ERR "SunLance: Cannot map PIO buffer.\n");
1372 goto fail;
1373 }
1374 lp->init_block_dvma = 0;
1375 lp->pio_buffer = 1;
1376 lp->init_ring = lance_init_ring_pio;
1377 lp->rx = lance_rx_pio;
1378 lp->tx = lance_tx_pio;
1379 } else {
1380 lp->init_block_mem =
1381 sbus_alloc_consistent(sdev, sizeof(struct lance_init_block),
1382 &lp->init_block_dvma);
1383 if (!lp->init_block_mem || lp->init_block_dvma == 0) {
1384 printk(KERN_ERR "SunLance: Cannot allocate consistent DMA memory.\n");
1385 goto fail;
1386 }
1387 lp->pio_buffer = 0;
1388 lp->init_ring = lance_init_ring_dvma;
1389 lp->rx = lance_rx_dvma;
1390 lp->tx = lance_tx_dvma;
1391 }
David S. Miller76fcdb32007-07-30 18:23:39 -07001392 lp->busmaster_regval = of_getintprop_default(dp, "busmaster-regval",
1393 (LE_C3_BSWP |
1394 LE_C3_ACON |
1395 LE_C3_BCON));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 lp->name = lancestr;
1398 lp->ledma = ledma;
1399
1400 lp->burst_sizes = 0;
1401 if (lp->ledma) {
David S. Miller76fcdb32007-07-30 18:23:39 -07001402 struct device_node *ledma_dp = ledma->sdev->ofdev.node;
1403 const char *prop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 unsigned int sbmask;
1405 u32 csr;
1406
1407 /* Find burst-size property for ledma */
David S. Miller76fcdb32007-07-30 18:23:39 -07001408 lp->burst_sizes = of_getintprop_default(ledma_dp,
1409 "burst-sizes", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 /* ledma may be capable of fast bursts, but sbus may not. */
David S. Miller76fcdb32007-07-30 18:23:39 -07001412 sbmask = of_getintprop_default(ledma_dp, "burst-sizes",
1413 DMA_BURSTBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 lp->burst_sizes &= sbmask;
1415
1416 /* Get the cable-selection property */
David S. Miller76fcdb32007-07-30 18:23:39 -07001417 prop = of_get_property(ledma_dp, "cable-selection", NULL);
1418 if (!prop || prop[0] == '\0') {
1419 struct device_node *nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
David S. Miller76fcdb32007-07-30 18:23:39 -07001421 printk(KERN_INFO "SunLance: using "
1422 "auto-carrier-detection.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
David S. Miller76fcdb32007-07-30 18:23:39 -07001424 nd = of_find_node_by_path("/options");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (!nd)
1426 goto no_link_test;
1427
David S. Miller76fcdb32007-07-30 18:23:39 -07001428 prop = of_get_property(nd, "tpe-link-test?", NULL);
1429 if (!prop)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 goto no_link_test;
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (strcmp(prop, "true")) {
1433 printk(KERN_NOTICE "SunLance: warning: overriding option "
1434 "'tpe-link-test?'\n");
1435 printk(KERN_NOTICE "SunLance: warning: mail any problems "
1436 "to ecd@skynet.be\n");
1437 auxio_set_lte(AUXIO_LTE_ON);
1438 }
1439no_link_test:
1440 lp->auto_select = 1;
1441 lp->tpe = 0;
1442 } else if (!strcmp(prop, "aui")) {
1443 lp->auto_select = 0;
1444 lp->tpe = 0;
1445 } else {
1446 lp->auto_select = 0;
1447 lp->tpe = 1;
1448 }
1449
1450 lp->dregs = ledma->regs;
1451
1452 /* Reset ledma */
1453 csr = sbus_readl(lp->dregs + DMA_CSR);
1454 sbus_writel(csr | DMA_RST_ENET, lp->dregs + DMA_CSR);
1455 udelay(200);
1456 sbus_writel(csr & ~DMA_RST_ENET, lp->dregs + DMA_CSR);
1457 } else
1458 lp->dregs = NULL;
1459
1460 lp->dev = dev;
1461 SET_MODULE_OWNER(dev);
David S. Millerc2d81e62006-06-23 18:39:40 -07001462 SET_NETDEV_DEV(dev, &sdev->ofdev.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 dev->open = &lance_open;
1464 dev->stop = &lance_close;
1465 dev->hard_start_xmit = &lance_start_xmit;
1466 dev->tx_timeout = &lance_tx_timeout;
1467 dev->watchdog_timeo = 5*HZ;
1468 dev->get_stats = &lance_get_stats;
1469 dev->set_multicast_list = &lance_set_multicast;
1470 dev->ethtool_ops = &sparc_lance_ethtool_ops;
1471
1472 dev->irq = sdev->irqs[0];
1473
1474 dev->dma = 0;
1475
1476 /* We cannot sleep if the chip is busy during a
1477 * multicast list update event, because such events
1478 * can occur from interrupts (ex. IPv6). So we
1479 * use a timer to try again later when necessary. -DaveM
1480 */
1481 init_timer(&lp->multicast_timer);
1482 lp->multicast_timer.data = (unsigned long) dev;
1483 lp->multicast_timer.function = &lance_set_multicast_retry;
1484
1485 if (register_netdev(dev)) {
1486 printk(KERN_ERR "SunLance: Cannot register device.\n");
1487 goto fail;
1488 }
1489
David S. Millerc2d81e62006-06-23 18:39:40 -07001490 dev_set_drvdata(&sdev->ofdev.dev, lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 printk(KERN_INFO "%s: LANCE ", dev->name);
1493
1494 for (i = 0; i < 6; i++)
1495 printk("%2.2x%c", dev->dev_addr[i],
1496 i == 5 ? ' ': ':');
1497 printk("\n");
1498
1499 return 0;
1500
1501fail:
1502 lance_free_hwresources(lp);
1503 free_netdev(dev);
1504 return -ENODEV;
1505}
1506
1507/* On 4m, find the associated dma for the lance chip */
David S. Millerefdbc1a2006-09-25 14:04:49 -07001508static struct sbus_dma * __devinit find_ledma(struct sbus_dev *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 struct sbus_dma *p;
1511
1512 for_each_dvma(p) {
1513 if (p->sdev == sdev)
1514 return p;
1515 }
1516 return NULL;
1517}
1518
1519#ifdef CONFIG_SUN4
1520
1521#include <asm/sun4paddr.h>
1522#include <asm/machines.h>
1523
1524/* Find all the lance cards on the system and initialize them */
David S. Millerc2d81e62006-06-23 18:39:40 -07001525static struct sbus_dev sun4_sdev;
David S. Millerefdbc1a2006-09-25 14:04:49 -07001526static int __devinit sparc_lance_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 if ((idprom->id_machtype == (SM_SUN4|SM_4_330)) ||
1529 (idprom->id_machtype == (SM_SUN4|SM_4_470))) {
Alexey Dobriyan93853fd2006-07-28 01:09:40 -07001530 memset(&sun4_sdev, 0, sizeof(struct sbus_dev));
David S. Millerc2d81e62006-06-23 18:39:40 -07001531 sun4_sdev.reg_addrs[0].phys_addr = sun4_eth_physaddr;
1532 sun4_sdev.irqs[0] = 6;
1533 return sparc_lance_probe_one(&sun4_sdev, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
1535 return -ENODEV;
1536}
1537
David S. Millerc2d81e62006-06-23 18:39:40 -07001538static int __exit sunlance_sun4_remove(void)
1539{
Alexey Dobriyan93853fd2006-07-28 01:09:40 -07001540 struct lance_private *lp = dev_get_drvdata(&sun4_sdev.ofdev.dev);
David S. Millerc2d81e62006-06-23 18:39:40 -07001541 struct net_device *net_dev = lp->dev;
1542
Marcel van Nies9f9b6692007-04-21 15:34:10 -07001543 unregister_netdev(net_dev);
David S. Millerc2d81e62006-06-23 18:39:40 -07001544
Alexey Dobriyan93853fd2006-07-28 01:09:40 -07001545 lance_free_hwresources(lp);
David S. Millerc2d81e62006-06-23 18:39:40 -07001546
1547 free_netdev(net_dev);
1548
Alexey Dobriyan93853fd2006-07-28 01:09:40 -07001549 dev_set_drvdata(&sun4_sdev.ofdev.dev, NULL);
David S. Millerc2d81e62006-06-23 18:39:40 -07001550
1551 return 0;
1552}
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554#else /* !CONFIG_SUN4 */
1555
David S. Millerc2d81e62006-06-23 18:39:40 -07001556static int __devinit sunlance_sbus_probe(struct of_device *dev, const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
David S. Millerc2d81e62006-06-23 18:39:40 -07001558 struct sbus_dev *sdev = to_sbus_device(&dev->dev);
David S. Millerc2d81e62006-06-23 18:39:40 -07001559 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Krzysztof Helt404dda82006-08-28 23:28:16 -07001561 if (sdev->parent) {
1562 struct of_device *parent = &sdev->parent->ofdev;
1563
1564 if (!strcmp(parent->node->name, "ledma")) {
1565 struct sbus_dma *ledma = find_ledma(to_sbus_device(&parent->dev));
1566
1567 err = sparc_lance_probe_one(sdev, ledma, NULL);
1568 } else if (!strcmp(parent->node->name, "lebuffer")) {
1569 err = sparc_lance_probe_one(sdev, NULL, to_sbus_device(&parent->dev));
1570 } else
1571 err = sparc_lance_probe_one(sdev, NULL, NULL);
1572 } else
David S. Millerc2d81e62006-06-23 18:39:40 -07001573 err = sparc_lance_probe_one(sdev, NULL, NULL);
David S. Millerc2d81e62006-06-23 18:39:40 -07001574
1575 return err;
1576}
1577
1578static int __devexit sunlance_sbus_remove(struct of_device *dev)
1579{
1580 struct lance_private *lp = dev_get_drvdata(&dev->dev);
1581 struct net_device *net_dev = lp->dev;
1582
Marcel van Nies9f9b6692007-04-21 15:34:10 -07001583 unregister_netdev(net_dev);
David S. Millerc2d81e62006-06-23 18:39:40 -07001584
1585 lance_free_hwresources(lp);
1586
1587 free_netdev(net_dev);
1588
1589 dev_set_drvdata(&dev->dev, NULL);
1590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return 0;
1592}
David S. Millerc2d81e62006-06-23 18:39:40 -07001593
1594static struct of_device_id sunlance_sbus_match[] = {
1595 {
1596 .name = "le",
1597 },
David S. Millerc2d81e62006-06-23 18:39:40 -07001598 {},
1599};
1600
1601MODULE_DEVICE_TABLE(of, sunlance_sbus_match);
1602
1603static struct of_platform_driver sunlance_sbus_driver = {
1604 .name = "sunlance",
1605 .match_table = sunlance_sbus_match,
1606 .probe = sunlance_sbus_probe,
1607 .remove = __devexit_p(sunlance_sbus_remove),
1608};
1609
1610
1611/* Find all the lance cards on the system and initialize them */
1612static int __init sparc_lance_init(void)
1613{
1614 return of_register_driver(&sunlance_sbus_driver, &sbus_bus_type);
1615}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616#endif /* !CONFIG_SUN4 */
1617
David S. Millerc2d81e62006-06-23 18:39:40 -07001618static void __exit sparc_lance_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
David S. Millerc2d81e62006-06-23 18:39:40 -07001620#ifdef CONFIG_SUN4
1621 sunlance_sun4_remove();
1622#else
1623 of_unregister_driver(&sunlance_sbus_driver);
1624#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
David S. Millerc2d81e62006-06-23 18:39:40 -07001627module_init(sparc_lance_init);
1628module_exit(sparc_lance_exit);