blob: 4e9a5a20b6a671941c0f4dfb5d1a627bf50b0f52 [file] [log] [blame]
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001/*
2 * 7990.c -- LANCE ethernet IC generic routines.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * This is an attempt to separate out the bits of various ethernet
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004 * drivers that are common because they all use the AMD 7990 LANCE
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * (Local Area Network Controller for Ethernet) chip.
6 *
7 * Copyright (C) 05/1998 Peter Maydell <pmaydell@chiark.greenend.org.uk>
8 *
9 * Most of this stuff was obtained by looking at other LANCE drivers,
10 * in particular a2065.[ch]. The AMD C-LANCE datasheet was also helpful.
11 * NB: this was made easy by the fact that Jes Sorensen had cleaned up
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012 * most of a2025 and sunlance with the aim of merging them, so the
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * common code was pretty obvious.
14 */
15#include <linux/crc32.h>
16#include <linux/delay.h>
17#include <linux/errno.h>
18#include <linux/netdevice.h>
19#include <linux/etherdevice.h>
20#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/fcntl.h>
25#include <linux/interrupt.h>
26#include <linux/ioport.h>
27#include <linux/in.h>
28#include <linux/route.h>
29#include <linux/slab.h>
30#include <linux/string.h>
31#include <linux/skbuff.h>
Al Viro3c139582005-12-01 06:44:55 -050032#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/* Used for the temporal inet entries and routing */
34#include <linux/socket.h>
35#include <linux/bitops.h>
36
37#include <asm/system.h>
38#include <asm/io.h>
39#include <asm/dma.h>
40#include <asm/pgtable.h>
41#ifdef CONFIG_HP300
42#include <asm/blinken.h>
43#endif
44
45#include "7990.h"
46
47#define WRITERAP(lp,x) out_be16(lp->base + LANCE_RAP, (x))
48#define WRITERDP(lp,x) out_be16(lp->base + LANCE_RDP, (x))
49#define READRDP(lp) in_be16(lp->base + LANCE_RDP)
50
51#if defined(CONFIG_HPLANCE) || defined(CONFIG_HPLANCE_MODULE)
52#include "hplance.h"
53
54#undef WRITERAP
55#undef WRITERDP
56#undef READRDP
57
58#if defined(CONFIG_MVME147_NET) || defined(CONFIG_MVME147_NET_MODULE)
59
60/* Lossage Factor Nine, Mr Sulu. */
61#define WRITERAP(lp,x) (lp->writerap(lp,x))
62#define WRITERDP(lp,x) (lp->writerdp(lp,x))
63#define READRDP(lp) (lp->readrdp(lp))
64
65#else
66
67/* These inlines can be used if only CONFIG_HPLANCE is defined */
68static inline void WRITERAP(struct lance_private *lp, __u16 value)
69{
70 do {
71 out_be16(lp->base + HPLANCE_REGOFF + LANCE_RAP, value);
72 } while ((in_8(lp->base + HPLANCE_STATUS) & LE_ACK) == 0);
73}
74
75static inline void WRITERDP(struct lance_private *lp, __u16 value)
76{
77 do {
78 out_be16(lp->base + HPLANCE_REGOFF + LANCE_RDP, value);
79 } while ((in_8(lp->base + HPLANCE_STATUS) & LE_ACK) == 0);
80}
81
82static inline __u16 READRDP(struct lance_private *lp)
83{
84 __u16 value;
85 do {
86 value = in_be16(lp->base + HPLANCE_REGOFF + LANCE_RDP);
87 } while ((in_8(lp->base + HPLANCE_STATUS) & LE_ACK) == 0);
88 return value;
89}
90
91#endif
92#endif /* CONFIG_HPLANCE || CONFIG_HPLANCE_MODULE */
93
94/* debugging output macros, various flavours */
95/* #define TEST_HITS */
96#ifdef UNDEF
97#define PRINT_RINGS() \
98do { \
99 int t; \
100 for (t=0; t < RX_RING_SIZE; t++) { \
101 printk("R%d: @(%02X %04X) len %04X, mblen %04X, bits %02X\n",\
102 t, ib->brx_ring[t].rmd1_hadr, ib->brx_ring[t].rmd0,\
103 ib->brx_ring[t].length,\
104 ib->brx_ring[t].mblength, ib->brx_ring[t].rmd1_bits);\
105 }\
106 for (t=0; t < TX_RING_SIZE; t++) { \
107 printk("T%d: @(%02X %04X) len %04X, misc %04X, bits %02X\n",\
108 t, ib->btx_ring[t].tmd1_hadr, ib->btx_ring[t].tmd0,\
109 ib->btx_ring[t].length,\
110 ib->btx_ring[t].misc, ib->btx_ring[t].tmd1_bits);\
111 }\
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400112} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#else
114#define PRINT_RINGS()
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400115#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117/* Load the CSR registers. The LANCE has to be STOPped when we do this! */
118static void load_csrs (struct lance_private *lp)
119{
120 volatile struct lance_init_block *aib = lp->lance_init_block;
121 int leptr;
122
123 leptr = LANCE_ADDR (aib);
124
125 WRITERAP(lp, LE_CSR1); /* load address of init block */
126 WRITERDP(lp, leptr & 0xFFFF);
127 WRITERAP(lp, LE_CSR2);
128 WRITERDP(lp, leptr >> 16);
129 WRITERAP(lp, LE_CSR3);
130 WRITERDP(lp, lp->busmaster_regval); /* set byteswap/ALEctrl/byte ctrl */
131
132 /* Point back to csr0 */
133 WRITERAP(lp, LE_CSR0);
134}
135
136/* #define to 0 or 1 appropriately */
137#define DEBUG_IRING 0
138/* Set up the Lance Rx and Tx rings and the init block */
139static void lance_init_ring (struct net_device *dev)
140{
141 struct lance_private *lp = netdev_priv(dev);
142 volatile struct lance_init_block *ib = lp->init_block;
143 volatile struct lance_init_block *aib; /* for LANCE_ADDR computations */
144 int leptr;
145 int i;
146
147 aib = lp->lance_init_block;
148
149 lp->rx_new = lp->tx_new = 0;
150 lp->rx_old = lp->tx_old = 0;
151
152 ib->mode = LE_MO_PROM; /* normal, enable Tx & Rx */
153
154 /* Copy the ethernet address to the lance init block
155 * Notice that we do a byteswap if we're big endian.
156 * [I think this is the right criterion; at least, sunlance,
157 * a2065 and atarilance do the byteswap and lance.c (PC) doesn't.
158 * However, the datasheet says that the BSWAP bit doesn't affect
159 * the init block, so surely it should be low byte first for
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400160 * everybody? Um.]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 * We could define the ib->physaddr as three 16bit values and
162 * use (addr[1] << 8) | addr[0] & co, but this is more efficient.
163 */
164#ifdef __BIG_ENDIAN
165 ib->phys_addr [0] = dev->dev_addr [1];
166 ib->phys_addr [1] = dev->dev_addr [0];
167 ib->phys_addr [2] = dev->dev_addr [3];
168 ib->phys_addr [3] = dev->dev_addr [2];
169 ib->phys_addr [4] = dev->dev_addr [5];
170 ib->phys_addr [5] = dev->dev_addr [4];
171#else
172 for (i=0; i<6; i++)
173 ib->phys_addr[i] = dev->dev_addr[i];
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400174#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 if (DEBUG_IRING)
177 printk ("TX rings:\n");
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 lp->tx_full = 0;
180 /* Setup the Tx ring entries */
181 for (i = 0; i < (1<<lp->lance_log_tx_bufs); i++) {
182 leptr = LANCE_ADDR(&aib->tx_buf[i][0]);
183 ib->btx_ring [i].tmd0 = leptr;
184 ib->btx_ring [i].tmd1_hadr = leptr >> 16;
185 ib->btx_ring [i].tmd1_bits = 0;
186 ib->btx_ring [i].length = 0xf000; /* The ones required by tmd2 */
187 ib->btx_ring [i].misc = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400188 if (DEBUG_IRING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 printk ("%d: 0x%8.8x\n", i, leptr);
190 }
191
192 /* Setup the Rx ring entries */
193 if (DEBUG_IRING)
194 printk ("RX rings:\n");
195 for (i = 0; i < (1<<lp->lance_log_rx_bufs); i++) {
196 leptr = LANCE_ADDR(&aib->rx_buf[i][0]);
197
198 ib->brx_ring [i].rmd0 = leptr;
199 ib->brx_ring [i].rmd1_hadr = leptr >> 16;
200 ib->brx_ring [i].rmd1_bits = LE_R1_OWN;
201 /* 0xf000 == bits that must be one (reserved, presumably) */
202 ib->brx_ring [i].length = -RX_BUFF_SIZE | 0xf000;
203 ib->brx_ring [i].mblength = 0;
204 if (DEBUG_IRING)
205 printk ("%d: 0x%8.8x\n", i, leptr);
206 }
207
208 /* Setup the initialization block */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /* Setup rx descriptor pointer */
211 leptr = LANCE_ADDR(&aib->brx_ring);
212 ib->rx_len = (lp->lance_log_rx_bufs << 13) | (leptr >> 16);
213 ib->rx_ptr = leptr;
214 if (DEBUG_IRING)
215 printk ("RX ptr: %8.8x\n", leptr);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /* Setup tx descriptor pointer */
218 leptr = LANCE_ADDR(&aib->btx_ring);
219 ib->tx_len = (lp->lance_log_tx_bufs << 13) | (leptr >> 16);
220 ib->tx_ptr = leptr;
221 if (DEBUG_IRING)
222 printk ("TX ptr: %8.8x\n", leptr);
223
224 /* Clear the multicast filter */
225 ib->filter [0] = 0;
226 ib->filter [1] = 0;
227 PRINT_RINGS();
228}
229
230/* LANCE must be STOPped before we do this, too... */
231static int init_restart_lance (struct lance_private *lp)
232{
233 int i;
234
235 WRITERAP(lp, LE_CSR0);
236 WRITERDP(lp, LE_C0_INIT);
237
238 /* Need a hook here for sunlance ledma stuff */
239
240 /* Wait for the lance to complete initialization */
241 for (i = 0; (i < 100) && !(READRDP(lp) & (LE_C0_ERR | LE_C0_IDON)); i++)
242 barrier();
243 if ((i == 100) || (READRDP(lp) & LE_C0_ERR)) {
244 printk ("LANCE unopened after %d ticks, csr0=%4.4x.\n", i, READRDP(lp));
245 return -1;
246 }
247
248 /* Clear IDON by writing a "1", enable interrupts and start lance */
249 WRITERDP(lp, LE_C0_IDON);
250 WRITERDP(lp, LE_C0_INEA | LE_C0_STRT);
251
252 return 0;
253}
254
255static int lance_reset (struct net_device *dev)
256{
257 struct lance_private *lp = netdev_priv(dev);
258 int status;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 /* Stop the lance */
261 WRITERAP(lp, LE_CSR0);
262 WRITERDP(lp, LE_C0_STOP);
263
264 load_csrs (lp);
265 lance_init_ring (dev);
266 dev->trans_start = jiffies;
267 status = init_restart_lance (lp);
268#ifdef DEBUG_DRIVER
269 printk ("Lance restart=%d\n", status);
270#endif
271 return status;
272}
273
274static int lance_rx (struct net_device *dev)
275{
276 struct lance_private *lp = netdev_priv(dev);
277 volatile struct lance_init_block *ib = lp->init_block;
278 volatile struct lance_rx_desc *rd;
279 unsigned char bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280#ifdef TEST_HITS
281 int i;
282#endif
283
284#ifdef TEST_HITS
285 printk ("[");
286 for (i = 0; i < RX_RING_SIZE; i++) {
287 if (i == lp->rx_new)
288 printk ("%s",
289 ib->brx_ring [i].rmd1_bits & LE_R1_OWN ? "_" : "X");
290 else
291 printk ("%s",
292 ib->brx_ring [i].rmd1_bits & LE_R1_OWN ? "." : "1");
293 }
294 printk ("]");
295#endif
296#ifdef CONFIG_HP300
297 blinken_leds(0x40, 0);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400298#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 WRITERDP(lp, LE_C0_RINT | LE_C0_INEA); /* ack Rx int, reenable ints */
300 for (rd = &ib->brx_ring [lp->rx_new]; /* For each Rx ring we own... */
301 !((bits = rd->rmd1_bits) & LE_R1_OWN);
302 rd = &ib->brx_ring [lp->rx_new]) {
303
304 /* We got an incomplete frame? */
305 if ((bits & LE_R1_POK) != LE_R1_POK) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700306 dev->stats.rx_over_errors++;
307 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 continue;
309 } else if (bits & LE_R1_ERR) {
310 /* Count only the end frame as a rx error,
311 * not the beginning
312 */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700313 if (bits & LE_R1_BUF) dev->stats.rx_fifo_errors++;
314 if (bits & LE_R1_CRC) dev->stats.rx_crc_errors++;
315 if (bits & LE_R1_OFL) dev->stats.rx_over_errors++;
316 if (bits & LE_R1_FRA) dev->stats.rx_frame_errors++;
317 if (bits & LE_R1_EOP) dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 } else {
Al Viro79ea13c2008-01-24 02:06:46 -0800319 int len = (rd->mblength & 0xfff) - 4;
320 struct sk_buff *skb = dev_alloc_skb (len+2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Al Viro79ea13c2008-01-24 02:06:46 -0800322 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 printk ("%s: Memory squeeze, deferring packet.\n",
324 dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700325 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 rd->mblength = 0;
327 rd->rmd1_bits = LE_R1_OWN;
328 lp->rx_new = (lp->rx_new + 1) & lp->rx_ring_mod_mask;
329 return 0;
330 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 skb_reserve (skb, 2); /* 16 byte align */
333 skb_put (skb, len); /* make room */
David S. Miller8c7b7fa2007-07-10 22:08:12 -0700334 skb_copy_to_linear_data(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 (unsigned char *)&(ib->rx_buf [lp->rx_new][0]),
David S. Miller8c7b7fa2007-07-10 22:08:12 -0700336 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 skb->protocol = eth_type_trans (skb, dev);
338 netif_rx (skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700339 dev->stats.rx_packets++;
340 dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
343 /* Return the packet to the pool */
344 rd->mblength = 0;
345 rd->rmd1_bits = LE_R1_OWN;
346 lp->rx_new = (lp->rx_new + 1) & lp->rx_ring_mod_mask;
347 }
348 return 0;
349}
350
351static int lance_tx (struct net_device *dev)
352{
353 struct lance_private *lp = netdev_priv(dev);
354 volatile struct lance_init_block *ib = lp->init_block;
355 volatile struct lance_tx_desc *td;
356 int i, j;
357 int status;
358
359#ifdef CONFIG_HP300
360 blinken_leds(0x80, 0);
361#endif
362 /* csr0 is 2f3 */
363 WRITERDP(lp, LE_C0_TINT | LE_C0_INEA);
364 /* csr0 is 73 */
365
366 j = lp->tx_old;
367 for (i = j; i != lp->tx_new; i = j) {
368 td = &ib->btx_ring [i];
369
370 /* If we hit a packet not owned by us, stop */
371 if (td->tmd1_bits & LE_T1_OWN)
372 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (td->tmd1_bits & LE_T1_ERR) {
375 status = td->misc;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400376
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700377 dev->stats.tx_errors++;
378 if (status & LE_T3_RTY) dev->stats.tx_aborted_errors++;
379 if (status & LE_T3_LCOL) dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 if (status & LE_T3_CLOS) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700382 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (lp->auto_select) {
384 lp->tpe = 1 - lp->tpe;
385 printk("%s: Carrier Lost, trying %s\n",
386 dev->name, lp->tpe?"TPE":"AUI");
387 /* Stop the lance */
388 WRITERAP(lp, LE_CSR0);
389 WRITERDP(lp, LE_C0_STOP);
390 lance_init_ring (dev);
391 load_csrs (lp);
392 init_restart_lance (lp);
393 return 0;
394 }
395 }
396
397 /* buffer errors and underflows turn off the transmitter */
398 /* Restart the adapter */
399 if (status & (LE_T3_BUF|LE_T3_UFL)) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700400 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 printk ("%s: Tx: ERR_BUF|ERR_UFL, restarting\n",
403 dev->name);
404 /* Stop the lance */
405 WRITERAP(lp, LE_CSR0);
406 WRITERDP(lp, LE_C0_STOP);
407 lance_init_ring (dev);
408 load_csrs (lp);
409 init_restart_lance (lp);
410 return 0;
411 }
412 } else if ((td->tmd1_bits & LE_T1_POK) == LE_T1_POK) {
413 /*
414 * So we don't count the packet more than once.
415 */
416 td->tmd1_bits &= ~(LE_T1_POK);
417
418 /* One collision before packet was sent. */
419 if (td->tmd1_bits & LE_T1_EONE)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700420 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 /* More than one collision, be optimistic. */
423 if (td->tmd1_bits & LE_T1_EMORE)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700424 dev->stats.collisions += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700426 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 j = (j + 1) & lp->tx_ring_mod_mask;
430 }
431 lp->tx_old = j;
432 WRITERDP(lp, LE_C0_TINT | LE_C0_INEA);
433 return 0;
434}
435
436static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100437lance_interrupt (int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 struct net_device *dev = (struct net_device *)dev_id;
440 struct lance_private *lp = netdev_priv(dev);
441 int csr0;
442
443 spin_lock (&lp->devlock);
444
445 WRITERAP(lp, LE_CSR0); /* LANCE Controller Status */
446 csr0 = READRDP(lp);
447
448 PRINT_RINGS();
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (!(csr0 & LE_C0_INTR)) { /* Check if any interrupt has */
451 spin_unlock (&lp->devlock);
452 return IRQ_NONE; /* been generated by the Lance. */
453 }
454
455 /* Acknowledge all the interrupt sources ASAP */
456 WRITERDP(lp, csr0 & ~(LE_C0_INEA|LE_C0_TDMD|LE_C0_STOP|LE_C0_STRT|LE_C0_INIT));
457
458 if ((csr0 & LE_C0_ERR)) {
459 /* Clear the error condition */
460 WRITERDP(lp, LE_C0_BABL|LE_C0_ERR|LE_C0_MISS|LE_C0_INEA);
461 }
462
463 if (csr0 & LE_C0_RINT)
464 lance_rx (dev);
465
466 if (csr0 & LE_C0_TINT)
467 lance_tx (dev);
468
469 /* Log misc errors. */
470 if (csr0 & LE_C0_BABL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700471 dev->stats.tx_errors++; /* Tx babble. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (csr0 & LE_C0_MISS)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700473 dev->stats.rx_errors++; /* Missed a Rx frame. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (csr0 & LE_C0_MERR) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400475 printk("%s: Bus master arbitration failure, status %4.4x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 dev->name, csr0);
477 /* Restart the chip. */
478 WRITERDP(lp, LE_C0_STRT);
479 }
480
481 if (lp->tx_full && netif_queue_stopped(dev) && (TX_BUFFS_AVAIL >= 0)) {
482 lp->tx_full = 0;
483 netif_wake_queue (dev);
484 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 WRITERAP(lp, LE_CSR0);
487 WRITERDP(lp, LE_C0_BABL|LE_C0_CERR|LE_C0_MISS|LE_C0_MERR|LE_C0_IDON|LE_C0_INEA);
488
489 spin_unlock (&lp->devlock);
490 return IRQ_HANDLED;
491}
492
493int lance_open (struct net_device *dev)
494{
495 struct lance_private *lp = netdev_priv(dev);
496 int res;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 /* Install the Interrupt handler. Or we could shunt this out to specific drivers? */
Thomas Gleixner38515e92007-02-14 00:33:16 -0800499 if (request_irq(lp->irq, lance_interrupt, IRQF_SHARED, lp->name, dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return -EAGAIN;
501
502 res = lance_reset(dev);
503 spin_lock_init(&lp->devlock);
504 netif_start_queue (dev);
505
506 return res;
507}
Adrian Bunkbf4d5932008-06-10 01:22:16 +0300508EXPORT_SYMBOL_GPL(lance_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510int lance_close (struct net_device *dev)
511{
512 struct lance_private *lp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 netif_stop_queue (dev);
515
516 /* Stop the LANCE */
517 WRITERAP(lp, LE_CSR0);
518 WRITERDP(lp, LE_C0_STOP);
519
520 free_irq(lp->irq, dev);
521
522 return 0;
523}
Adrian Bunkbf4d5932008-06-10 01:22:16 +0300524EXPORT_SYMBOL_GPL(lance_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526void lance_tx_timeout(struct net_device *dev)
527{
528 printk("lance_tx_timeout\n");
529 lance_reset(dev);
530 dev->trans_start = jiffies;
531 netif_wake_queue (dev);
532}
Adrian Bunkbf4d5932008-06-10 01:22:16 +0300533EXPORT_SYMBOL_GPL(lance_tx_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535int lance_start_xmit (struct sk_buff *skb, struct net_device *dev)
536{
537 struct lance_private *lp = netdev_priv(dev);
538 volatile struct lance_init_block *ib = lp->init_block;
539 int entry, skblen, len;
540 static int outs;
541 unsigned long flags;
542
543 if (!TX_BUFFS_AVAIL)
Patrick McHardy5b548142009-06-12 06:22:29 +0000544 return NETDEV_TX_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 netif_stop_queue (dev);
547
548 skblen = skb->len;
549
550#ifdef DEBUG_DRIVER
551 /* dump the packet */
552 {
553 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 for (i = 0; i < 64; i++) {
556 if ((i % 16) == 0)
557 printk ("\n");
558 printk ("%2.2x ", skb->data [i]);
559 }
560 }
561#endif
562 len = (skblen <= ETH_ZLEN) ? ETH_ZLEN : skblen;
563 entry = lp->tx_new & lp->tx_ring_mod_mask;
564 ib->btx_ring [entry].length = (-len) | 0xf000;
565 ib->btx_ring [entry].misc = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400566
Geert Uytterhoevencfa08bb2007-05-01 22:33:05 +0200567 if (skb->len < ETH_ZLEN)
568 memset((void *)&ib->tx_buf[entry][0], 0, ETH_ZLEN);
569 skb_copy_from_linear_data(skb, (void *)&ib->tx_buf[entry][0], skblen);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* Now, give the packet to the lance */
572 ib->btx_ring [entry].tmd1_bits = (LE_T1_POK|LE_T1_OWN);
573 lp->tx_new = (lp->tx_new+1) & lp->tx_ring_mod_mask;
574
575 outs++;
576 /* Kick the lance: transmit now */
577 WRITERDP(lp, LE_C0_INEA | LE_C0_TDMD);
578 dev->trans_start = jiffies;
579 dev_kfree_skb (skb);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 spin_lock_irqsave (&lp->devlock, flags);
582 if (TX_BUFFS_AVAIL)
583 netif_start_queue (dev);
584 else
585 lp->tx_full = 1;
586 spin_unlock_irqrestore (&lp->devlock, flags);
587
Patrick McHardyec634fe2009-07-05 19:23:38 -0700588 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
Adrian Bunkbf4d5932008-06-10 01:22:16 +0300590EXPORT_SYMBOL_GPL(lance_start_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592/* taken from the depca driver via a2065.c */
593static void lance_load_multicast (struct net_device *dev)
594{
595 struct lance_private *lp = netdev_priv(dev);
596 volatile struct lance_init_block *ib = lp->init_block;
597 volatile u16 *mcast_table = (u16 *)&ib->filter;
Jiri Pirko0ddf4772010-02-20 00:13:58 +0000598 struct dev_mc_list *dmi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 char *addrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 u32 crc;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /* set all multicast bits */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400603 if (dev->flags & IFF_ALLMULTI){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 ib->filter [0] = 0xffffffff;
605 ib->filter [1] = 0xffffffff;
606 return;
607 }
608 /* clear the multicast filter */
609 ib->filter [0] = 0;
610 ib->filter [1] = 0;
611
612 /* Add addresses */
Jiri Pirko0ddf4772010-02-20 00:13:58 +0000613 netdev_for_each_mc_addr(dmi, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 addrs = dmi->dmi_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 /* multicast address? */
617 if (!(*addrs & 1))
618 continue;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 crc = ether_crc_le(6, addrs);
621 crc = crc >> 26;
622 mcast_table [crc >> 4] |= 1 << (crc & 0xf);
623 }
624 return;
625}
626
627
628void lance_set_multicast (struct net_device *dev)
629{
630 struct lance_private *lp = netdev_priv(dev);
631 volatile struct lance_init_block *ib = lp->init_block;
632 int stopped;
633
634 stopped = netif_queue_stopped(dev);
635 if (!stopped)
636 netif_stop_queue (dev);
637
638 while (lp->tx_old != lp->tx_new)
639 schedule();
640
641 WRITERAP(lp, LE_CSR0);
642 WRITERDP(lp, LE_C0_STOP);
643 lance_init_ring (dev);
644
645 if (dev->flags & IFF_PROMISC) {
646 ib->mode |= LE_MO_PROM;
647 } else {
648 ib->mode &= ~LE_MO_PROM;
649 lance_load_multicast (dev);
650 }
651 load_csrs (lp);
652 init_restart_lance (lp);
653
654 if (!stopped)
655 netif_start_queue (dev);
656}
Adrian Bunkbf4d5932008-06-10 01:22:16 +0300657EXPORT_SYMBOL_GPL(lance_set_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659#ifdef CONFIG_NET_POLL_CONTROLLER
660void lance_poll(struct net_device *dev)
661{
662 struct lance_private *lp = netdev_priv(dev);
663
664 spin_lock (&lp->devlock);
665 WRITERAP(lp, LE_CSR0);
666 WRITERDP(lp, LE_C0_STRT);
667 spin_unlock (&lp->devlock);
Al Viro2850bc22006-10-07 14:16:45 +0100668 lance_interrupt(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670#endif
671
672MODULE_LICENSE("GPL");