blob: c7b571be20e0693a7ba1dadc4f60da2b305bdbff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 3c527.c: 3Com Etherlink/MC32 driver for Linux 2.4 and 2.6.
2 *
3 * (c) Copyright 1998 Red Hat Software Inc
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004 * Written by Alan Cox.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Further debugging by Carl Drougge.
6 * Initial SMP support by Felipe W Damasio <felipewd@terra.com.br>
7 * Heavily modified by Richard Procter <rnp@paradise.net.nz>
8 *
9 * Based on skeleton.c written 1993-94 by Donald Becker and ne2.c
10 * (for the MCA stuff) written by Wim Dumon.
11 *
12 * Thanks to 3Com for making this possible by providing me with the
13 * documentation.
14 *
15 * This software may be used and distributed according to the terms
16 * of the GNU General Public License, incorporated herein by reference.
17 *
18 */
19
20#define DRV_NAME "3c527"
21#define DRV_VERSION "0.7-SMP"
22#define DRV_RELDATE "2003/09/21"
23
24static const char *version =
25DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " Richard Procter <rnp@paradise.net.nz>\n";
26
27/**
28 * DOC: Traps for the unwary
29 *
30 * The diagram (Figure 1-1) and the POS summary disagree with the
31 * "Interrupt Level" section in the manual.
32 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -040033 * The manual contradicts itself when describing the minimum number
34 * buffers in the 'configure lists' command.
35 * My card accepts a buffer config of 4/4.
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 *
37 * Setting the SAV BP bit does not save bad packets, but
Jeff Garzik6aa20a22006-09-13 13:24:59 -040038 * only enables RX on-card stats collection.
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 *
40 * The documentation in places seems to miss things. In actual fact
41 * I've always eventually found everything is documented, it just
42 * requires careful study.
43 *
44 * DOC: Theory Of Operation
45 *
46 * The 3com 3c527 is a 32bit MCA bus mastering adapter with a large
47 * amount of on board intelligence that housekeeps a somewhat dumber
48 * Intel NIC. For performance we want to keep the transmit queue deep
49 * as the card can transmit packets while fetching others from main
50 * memory by bus master DMA. Transmission and reception are driven by
51 * circular buffer queues.
52 *
53 * The mailboxes can be used for controlling how the card traverses
54 * its buffer rings, but are used only for inital setup in this
55 * implementation. The exec mailbox allows a variety of commands to
56 * be executed. Each command must complete before the next is
57 * executed. Primarily we use the exec mailbox for controlling the
58 * multicast lists. We have to do a certain amount of interesting
59 * hoop jumping as the multicast list changes can occur in interrupt
60 * state when the card has an exec command pending. We defer such
61 * events until the command completion interrupt.
62 *
63 * A copy break scheme (taken from 3c59x.c) is employed whereby
64 * received frames exceeding a configurable length are passed
65 * directly to the higher networking layers without incuring a copy,
66 * in what amounts to a time/space trade-off.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040067 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * The card also keeps a large amount of statistical information
69 * on-board. In a perfect world, these could be used safely at no
70 * cost. However, lacking information to the contrary, processing
71 * them without races would involve so much extra complexity as to
72 * make it unworthwhile to do so. In the end, a hybrid SW/HW
Jeff Garzik6aa20a22006-09-13 13:24:59 -040073 * implementation was made necessary --- see mc32_update_stats().
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 *
75 * DOC: Notes
Jeff Garzik6aa20a22006-09-13 13:24:59 -040076 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * It should be possible to use two or more cards, but at this stage
78 * only by loading two copies of the same module.
79 *
80 * The on-board 82586 NIC has trouble receiving multiple
81 * back-to-back frames and so is likely to drop packets from fast
82 * senders.
83**/
84
85#include <linux/module.h>
86
87#include <linux/errno.h>
88#include <linux/netdevice.h>
89#include <linux/etherdevice.h>
90#include <linux/if_ether.h>
91#include <linux/init.h>
92#include <linux/kernel.h>
93#include <linux/types.h>
94#include <linux/fcntl.h>
95#include <linux/interrupt.h>
96#include <linux/mca-legacy.h>
97#include <linux/ioport.h>
98#include <linux/in.h>
99#include <linux/skbuff.h>
100#include <linux/slab.h>
101#include <linux/string.h>
102#include <linux/wait.h>
103#include <linux/ethtool.h>
104#include <linux/completion.h>
105#include <linux/bitops.h>
106
107#include <asm/semaphore.h>
108#include <asm/uaccess.h>
109#include <asm/system.h>
110#include <asm/io.h>
111#include <asm/dma.h>
112
113#include "3c527.h"
114
115MODULE_LICENSE("GPL");
116
117/*
118 * The name of the card. Is used for messages and in the requests for
119 * io regions, irqs and dma channels
120 */
121static const char* cardname = DRV_NAME;
122
123/* use 0 for production, 1 for verification, >2 for debug */
124#ifndef NET_DEBUG
125#define NET_DEBUG 2
126#endif
127
128#undef DEBUG_IRQ
129
130static unsigned int mc32_debug = NET_DEBUG;
131
132/* The number of low I/O ports used by the ethercard. */
133#define MC32_IO_EXTENT 8
134
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400135/* As implemented, values must be a power-of-2 -- 4/8/16/32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#define TX_RING_LEN 32 /* Typically the card supports 37 */
137#define RX_RING_LEN 8 /* " " " */
138
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400139/* Copy break point, see above for details.
140 * Setting to > 1512 effectively disables this feature. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#define RX_COPYBREAK 200 /* Value from 3c59x.c */
142
143/* Issue the 82586 workaround command - this is for "busy lans", but
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400144 * basically means for all lans now days - has a performance (latency)
145 * cost, but best set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146static const int WORKAROUND_82586=1;
147
148/* Pointers to buffers and their on-card records */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400149struct mc32_ring_desc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400151 volatile struct skb_header *p;
152 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153};
154
155/* Information that needs to be kept for each board. */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400156struct mc32_local
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 int slot;
159
160 u32 base;
161 struct net_device_stats net_stats;
162 volatile struct mc32_mailbox *rx_box;
163 volatile struct mc32_mailbox *tx_box;
164 volatile struct mc32_mailbox *exec_box;
165 volatile struct mc32_stats *stats; /* Start of on-card statistics */
166 u16 tx_chain; /* Transmit list start offset */
167 u16 rx_chain; /* Receive list start offset */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400168 u16 tx_len; /* Transmit list count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 u16 rx_len; /* Receive list count */
170
171 u16 xceiver_desired_state; /* HALTED or RUNNING */
172 u16 cmd_nonblocking; /* Thread is uninterested in command result */
173 u16 mc_reload_wait; /* A multicast load request is pending */
174 u32 mc_list_valid; /* True when the mclist is set */
175
176 struct mc32_ring_desc tx_ring[TX_RING_LEN]; /* Host Transmit ring */
177 struct mc32_ring_desc rx_ring[RX_RING_LEN]; /* Host Receive ring */
178
179 atomic_t tx_count; /* buffers left */
180 atomic_t tx_ring_head; /* index to tx en-queue end */
181 u16 tx_ring_tail; /* index to tx de-queue end */
182
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400183 u16 rx_ring_tail; /* index to rx de-queue end */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 struct semaphore cmd_mutex; /* Serialises issuing of execute commands */
186 struct completion execution_cmd; /* Card has completed an execute command */
187 struct completion xceiver_cmd; /* Card has completed a tx or rx command */
188};
189
190/* The station (ethernet) address prefix, used for a sanity check. */
191#define SA_ADDR0 0x02
192#define SA_ADDR1 0x60
193#define SA_ADDR2 0xAC
194
195struct mca_adapters_t {
196 unsigned int id;
197 char *name;
198};
199
200static const struct mca_adapters_t mc32_adapters[] = {
201 { 0x0041, "3COM EtherLink MC/32" },
202 { 0x8EF5, "IBM High Performance Lan Adapter" },
203 { 0x0000, NULL }
204};
205
206
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400207/* Macros for ring index manipulations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208static inline u16 next_rx(u16 rx) { return (rx+1)&(RX_RING_LEN-1); };
209static inline u16 prev_rx(u16 rx) { return (rx-1)&(RX_RING_LEN-1); };
210
211static inline u16 next_tx(u16 tx) { return (tx+1)&(TX_RING_LEN-1); };
212
213
214/* Index to functions, as function prototypes. */
215static int mc32_probe1(struct net_device *dev, int ioaddr);
216static int mc32_command(struct net_device *dev, u16 cmd, void *data, int len);
217static int mc32_open(struct net_device *dev);
218static void mc32_timeout(struct net_device *dev);
219static int mc32_send_packet(struct sk_buff *skb, struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100220static irqreturn_t mc32_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221static int mc32_close(struct net_device *dev);
222static struct net_device_stats *mc32_get_stats(struct net_device *dev);
223static void mc32_set_multicast_list(struct net_device *dev);
224static void mc32_reset_multicast_list(struct net_device *dev);
Jeff Garzik7282d492006-09-13 14:30:00 -0400225static const struct ethtool_ops netdev_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227static void cleanup_card(struct net_device *dev)
228{
229 struct mc32_local *lp = netdev_priv(dev);
230 unsigned slot = lp->slot;
231 mca_mark_as_unused(slot);
232 mca_set_adapter_name(slot, NULL);
233 free_irq(dev->irq, dev);
234 release_region(dev->base_addr, MC32_IO_EXTENT);
235}
236
237/**
238 * mc32_probe - Search for supported boards
239 * @unit: interface number to use
240 *
241 * Because MCA bus is a real bus and we can scan for cards we could do a
242 * single scan for all boards here. Right now we use the passed in device
243 * structure and scan for only one board. This needs fixing for modules
244 * in particular.
245 */
246
247struct net_device *__init mc32_probe(int unit)
248{
249 struct net_device *dev = alloc_etherdev(sizeof(struct mc32_local));
250 static int current_mca_slot = -1;
251 int i;
252 int err;
253
254 if (!dev)
255 return ERR_PTR(-ENOMEM);
256
257 if (unit >= 0)
258 sprintf(dev->name, "eth%d", unit);
259
260 SET_MODULE_OWNER(dev);
261
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400262 /* Do not check any supplied i/o locations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 POS registers usually don't fail :) */
264
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400265 /* MCA cards have POS registers.
266 Autodetecting MCA cards is extremely simple.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 Just search for the card. */
268
269 for(i = 0; (mc32_adapters[i].name != NULL); i++) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400270 current_mca_slot =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 mca_find_unused_adapter(mc32_adapters[i].id, 0);
272
273 if(current_mca_slot != MCA_NOTFOUND) {
274 if(!mc32_probe1(dev, current_mca_slot))
275 {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400276 mca_set_adapter_name(current_mca_slot,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 mc32_adapters[i].name);
278 mca_mark_as_used(current_mca_slot);
279 err = register_netdev(dev);
280 if (err) {
281 cleanup_card(dev);
282 free_netdev(dev);
283 dev = ERR_PTR(err);
284 }
285 return dev;
286 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289 }
290 free_netdev(dev);
291 return ERR_PTR(-ENODEV);
292}
293
294/**
295 * mc32_probe1 - Check a given slot for a board and test the card
296 * @dev: Device structure to fill in
297 * @slot: The MCA bus slot being used by this card
298 *
299 * Decode the slot data and configure the card structures. Having done this we
300 * can reset the card and configure it. The card does a full self test cycle
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400301 * in firmware so we have to wait for it to return and post us either a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * failure case or some addresses we use to find the board internals.
303 */
304
305static int __init mc32_probe1(struct net_device *dev, int slot)
306{
307 static unsigned version_printed;
308 int i, err;
309 u8 POS;
310 u32 base;
311 struct mc32_local *lp = netdev_priv(dev);
312 static u16 mca_io_bases[]={
313 0x7280,0x7290,
314 0x7680,0x7690,
315 0x7A80,0x7A90,
316 0x7E80,0x7E90
317 };
318 static u32 mca_mem_bases[]={
319 0x00C0000,
320 0x00C4000,
321 0x00C8000,
322 0x00CC000,
323 0x00D0000,
324 0x00D4000,
325 0x00D8000,
326 0x00DC000
327 };
328 static char *failures[]={
329 "Processor instruction",
330 "Processor data bus",
331 "Processor data bus",
332 "Processor data bus",
333 "Adapter bus",
334 "ROM checksum",
335 "Base RAM",
336 "Extended RAM",
337 "82586 internal loopback",
338 "82586 initialisation failure",
339 "Adapter list configuration error"
340 };
341
342 /* Time to play MCA games */
343
344 if (mc32_debug && version_printed++ == 0)
345 printk(KERN_DEBUG "%s", version);
346
347 printk(KERN_INFO "%s: %s found in slot %d:", dev->name, cardname, slot);
348
349 POS = mca_read_stored_pos(slot, 2);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if(!(POS&1))
352 {
353 printk(" disabled.\n");
354 return -ENODEV;
355 }
356
357 /* Fill in the 'dev' fields. */
358 dev->base_addr = mca_io_bases[(POS>>1)&7];
359 dev->mem_start = mca_mem_bases[(POS>>4)&7];
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 POS = mca_read_stored_pos(slot, 4);
362 if(!(POS&1))
363 {
364 printk("memory window disabled.\n");
365 return -ENODEV;
366 }
367
368 POS = mca_read_stored_pos(slot, 5);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 i=(POS>>4)&3;
371 if(i==3)
372 {
373 printk("invalid memory window.\n");
374 return -ENODEV;
375 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 i*=16384;
378 i+=16384;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 dev->mem_end=dev->mem_start + i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 dev->irq = ((POS>>2)&3)+9;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if(!request_region(dev->base_addr, MC32_IO_EXTENT, cardname))
385 {
386 printk("io 0x%3lX, which is busy.\n", dev->base_addr);
387 return -EBUSY;
388 }
389
390 printk("io 0x%3lX irq %d mem 0x%lX (%dK)\n",
391 dev->base_addr, dev->irq, dev->mem_start, i/1024);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400392
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /* We ought to set the cache line size here.. */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400395
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /*
398 * Go PROM browsing
399 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 printk("%s: Address ", dev->name);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /* Retrieve and print the ethernet address. */
404 for (i = 0; i < 6; i++)
405 {
406 mca_write_pos(slot, 6, i+12);
407 mca_write_pos(slot, 7, 0);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 printk(" %2.2x", dev->dev_addr[i] = mca_read_pos(slot,3));
410 }
411
412 mca_write_pos(slot, 6, 0);
413 mca_write_pos(slot, 7, 0);
414
415 POS = mca_read_stored_pos(slot, 4);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if(POS&2)
418 printk(" : BNC port selected.\n");
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400419 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 printk(" : AUI port selected.\n");
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 POS=inb(dev->base_addr+HOST_CTRL);
423 POS|=HOST_CTRL_ATTN|HOST_CTRL_RESET;
424 POS&=~HOST_CTRL_INTE;
425 outb(POS, dev->base_addr+HOST_CTRL);
426 /* Reset adapter */
427 udelay(100);
428 /* Reset off */
429 POS&=~(HOST_CTRL_ATTN|HOST_CTRL_RESET);
430 outb(POS, dev->base_addr+HOST_CTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 udelay(300);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /*
435 * Grab the IRQ
436 */
437
Thomas Gleixner1fb9df52006-07-01 19:29:39 -0700438 err = request_irq(dev->irq, &mc32_interrupt, IRQF_SHARED | IRQF_SAMPLE_RANDOM, DRV_NAME, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (err) {
440 release_region(dev->base_addr, MC32_IO_EXTENT);
441 printk(KERN_ERR "%s: unable to get IRQ %d.\n", DRV_NAME, dev->irq);
442 goto err_exit_ports;
443 }
444
445 memset(lp, 0, sizeof(struct mc32_local));
446 lp->slot = slot;
447
448 i=0;
449
450 base = inb(dev->base_addr);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 while(base == 0xFF)
453 {
454 i++;
455 if(i == 1000)
456 {
457 printk(KERN_ERR "%s: failed to boot adapter.\n", dev->name);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400458 err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 goto err_exit_irq;
460 }
461 udelay(1000);
462 if(inb(dev->base_addr+2)&(1<<5))
463 base = inb(dev->base_addr);
464 }
465
466 if(base>0)
467 {
468 if(base < 0x0C)
469 printk(KERN_ERR "%s: %s%s.\n", dev->name, failures[base-1],
470 base<0x0A?" test failure":"");
471 else
472 printk(KERN_ERR "%s: unknown failure %d.\n", dev->name, base);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400473 err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 goto err_exit_irq;
475 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 base=0;
478 for(i=0;i<4;i++)
479 {
480 int n=0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 while(!(inb(dev->base_addr+2)&(1<<5)))
483 {
484 n++;
485 udelay(50);
486 if(n>100)
487 {
488 printk(KERN_ERR "%s: mailbox read fail (%d).\n", dev->name, i);
489 err = -ENODEV;
490 goto err_exit_irq;
491 }
492 }
493
494 base|=(inb(dev->base_addr)<<(8*i));
495 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 lp->exec_box=isa_bus_to_virt(dev->mem_start+base);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400498
499 base=lp->exec_box->data[1]<<16|lp->exec_box->data[0];
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 lp->base = dev->mem_start+base;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400502
503 lp->rx_box=isa_bus_to_virt(lp->base + lp->exec_box->data[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 lp->tx_box=isa_bus_to_virt(lp->base + lp->exec_box->data[3]);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 lp->stats = isa_bus_to_virt(lp->base + lp->exec_box->data[5]);
507
508 /*
509 * Descriptor chains (card relative)
510 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 lp->tx_chain = lp->exec_box->data[8]; /* Transmit list start offset */
513 lp->rx_chain = lp->exec_box->data[10]; /* Receive list start offset */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400514 lp->tx_len = lp->exec_box->data[9]; /* Transmit list count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 lp->rx_len = lp->exec_box->data[11]; /* Receive list count */
516
517 init_MUTEX_LOCKED(&lp->cmd_mutex);
518 init_completion(&lp->execution_cmd);
519 init_completion(&lp->xceiver_cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 printk("%s: Firmware Rev %d. %d RX buffers, %d TX buffers. Base of 0x%08X.\n",
522 dev->name, lp->exec_box->data[12], lp->rx_len, lp->tx_len, lp->base);
523
524 dev->open = mc32_open;
525 dev->stop = mc32_close;
526 dev->hard_start_xmit = mc32_send_packet;
527 dev->get_stats = mc32_get_stats;
528 dev->set_multicast_list = mc32_set_multicast_list;
529 dev->tx_timeout = mc32_timeout;
530 dev->watchdog_timeo = HZ*5; /* Board does all the work */
531 dev->ethtool_ops = &netdev_ethtool_ops;
532
533 return 0;
534
535err_exit_irq:
536 free_irq(dev->irq, dev);
537err_exit_ports:
538 release_region(dev->base_addr, MC32_IO_EXTENT);
539 return err;
540}
541
542
543/**
544 * mc32_ready_poll - wait until we can feed it a command
545 * @dev: The device to wait for
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400546 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 * Wait until the card becomes ready to accept a command via the
548 * command register. This tells us nothing about the completion
549 * status of any pending commands and takes very little time at all.
550 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552static inline void mc32_ready_poll(struct net_device *dev)
553{
554 int ioaddr = dev->base_addr;
555 while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
556}
557
558
559/**
560 * mc32_command_nowait - send a command non blocking
561 * @dev: The 3c527 to issue the command to
562 * @cmd: The command word to write to the mailbox
563 * @data: A data block if the command expects one
564 * @len: Length of the data block
565 *
566 * Send a command from interrupt state. If there is a command
567 * currently being executed then we return an error of -1. It
568 * simply isn't viable to wait around as commands may be
569 * slow. This can theoretically be starved on SMP, but it's hard
570 * to see a realistic situation. We do not wait for the command
571 * to complete --- we rely on the interrupt handler to tidy up
572 * after us.
573 */
574
575static int mc32_command_nowait(struct net_device *dev, u16 cmd, void *data, int len)
576{
577 struct mc32_local *lp = netdev_priv(dev);
578 int ioaddr = dev->base_addr;
579 int ret = -1;
580
581 if (down_trylock(&lp->cmd_mutex) == 0)
582 {
583 lp->cmd_nonblocking=1;
584 lp->exec_box->mbox=0;
585 lp->exec_box->mbox=cmd;
586 memcpy((void *)lp->exec_box->data, data, len);
587 barrier(); /* the memcpy forgot the volatile so be sure */
588
589 /* Send the command */
590 mc32_ready_poll(dev);
591 outb(1<<6, ioaddr+HOST_CMD);
592
593 ret = 0;
594
595 /* Interrupt handler will signal mutex on completion */
596 }
597
598 return ret;
599}
600
601
602/**
603 * mc32_command - send a command and sleep until completion
604 * @dev: The 3c527 card to issue the command to
605 * @cmd: The command word to write to the mailbox
606 * @data: A data block if the command expects one
607 * @len: Length of the data block
608 *
609 * Sends exec commands in a user context. This permits us to wait around
610 * for the replies and also to wait for the command buffer to complete
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400611 * from a previous command before we execute our command. After our
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 * command completes we will attempt any pending multicast reload
613 * we blocked off by hogging the exec buffer.
614 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400615 * You feed the card a command, you wait, it interrupts you get a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 * reply. All well and good. The complication arises because you use
617 * commands for filter list changes which come in at bh level from things
618 * like IPV6 group stuff.
619 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621static int mc32_command(struct net_device *dev, u16 cmd, void *data, int len)
622{
623 struct mc32_local *lp = netdev_priv(dev);
624 int ioaddr = dev->base_addr;
625 int ret = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 down(&lp->cmd_mutex);
628
629 /*
630 * My Turn
631 */
632
633 lp->cmd_nonblocking=0;
634 lp->exec_box->mbox=0;
635 lp->exec_box->mbox=cmd;
636 memcpy((void *)lp->exec_box->data, data, len);
637 barrier(); /* the memcpy forgot the volatile so be sure */
638
639 mc32_ready_poll(dev);
640 outb(1<<6, ioaddr+HOST_CMD);
641
642 wait_for_completion(&lp->execution_cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if(lp->exec_box->mbox&(1<<13))
645 ret = -1;
646
647 up(&lp->cmd_mutex);
648
649 /*
650 * A multicast set got blocked - try it now
651 */
652
653 if(lp->mc_reload_wait)
654 {
655 mc32_reset_multicast_list(dev);
656 }
657
658 return ret;
659}
660
661
662/**
663 * mc32_start_transceiver - tell board to restart tx/rx
664 * @dev: The 3c527 card to issue the command to
665 *
666 * This may be called from the interrupt state, where it is used
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400667 * to restart the rx ring if the card runs out of rx buffers.
668 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 * We must first check if it's ok to (re)start the transceiver. See
670 * mc32_close for details.
671 */
672
673static void mc32_start_transceiver(struct net_device *dev) {
674
675 struct mc32_local *lp = netdev_priv(dev);
676 int ioaddr = dev->base_addr;
677
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400678 /* Ignore RX overflow on device closure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (lp->xceiver_desired_state==HALTED)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400680 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 /* Give the card the offset to the post-EOL-bit RX descriptor */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400683 mc32_ready_poll(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 lp->rx_box->mbox=0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400685 lp->rx_box->data[0]=lp->rx_ring[prev_rx(lp->rx_ring_tail)].p->next;
686 outb(HOST_CMD_START_RX, ioaddr+HOST_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400688 mc32_ready_poll(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 lp->tx_box->mbox=0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400690 outb(HOST_CMD_RESTRT_TX, ioaddr+HOST_CMD); /* card ignores this on RX restart */
691
692 /* We are not interrupted on start completion */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
695
696/**
697 * mc32_halt_transceiver - tell board to stop tx/rx
698 * @dev: The 3c527 card to issue the command to
699 *
700 * We issue the commands to halt the card's transceiver. In fact,
701 * after some experimenting we now simply tell the card to
702 * suspend. When issuing aborts occasionally odd things happened.
703 *
704 * We then sleep until the card has notified us that both rx and
705 * tx have been suspended.
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400706 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400708static void mc32_halt_transceiver(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
710 struct mc32_local *lp = netdev_priv(dev);
711 int ioaddr = dev->base_addr;
712
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400713 mc32_ready_poll(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 lp->rx_box->mbox=0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400715 outb(HOST_CMD_SUSPND_RX, ioaddr+HOST_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 wait_for_completion(&lp->xceiver_cmd);
717
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400718 mc32_ready_poll(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 lp->tx_box->mbox=0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400720 outb(HOST_CMD_SUSPND_TX, ioaddr+HOST_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 wait_for_completion(&lp->xceiver_cmd);
722}
723
724
725/**
726 * mc32_load_rx_ring - load the ring of receive buffers
727 * @dev: 3c527 to build the ring for
728 *
729 * This initalises the on-card and driver datastructures to
730 * the point where mc32_start_transceiver() can be called.
731 *
732 * The card sets up the receive ring for us. We are required to use the
733 * ring it provides, although the size of the ring is configurable.
734 *
735 * We allocate an sk_buff for each ring entry in turn and
736 * initalise its house-keeping info. At the same time, we read
737 * each 'next' pointer in our rx_ring array. This reduces slow
738 * shared-memory reads and makes it easy to access predecessor
739 * descriptors.
740 *
741 * We then set the end-of-list bit for the last entry so that the
742 * card will know when it has run out of buffers.
743 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745static int mc32_load_rx_ring(struct net_device *dev)
746{
747 struct mc32_local *lp = netdev_priv(dev);
748 int i;
749 u16 rx_base;
750 volatile struct skb_header *p;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 rx_base=lp->rx_chain;
753
754 for(i=0; i<RX_RING_LEN; i++) {
755 lp->rx_ring[i].skb=alloc_skb(1532, GFP_KERNEL);
756 if (lp->rx_ring[i].skb==NULL) {
757 for (;i>=0;i--)
758 kfree_skb(lp->rx_ring[i].skb);
759 return -ENOBUFS;
760 }
761 skb_reserve(lp->rx_ring[i].skb, 18);
762
763 p=isa_bus_to_virt(lp->base+rx_base);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 p->control=0;
766 p->data=isa_virt_to_bus(lp->rx_ring[i].skb->data);
767 p->status=0;
768 p->length=1532;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400769
770 lp->rx_ring[i].p=p;
771 rx_base=p->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773
774 lp->rx_ring[i-1].p->control |= CONTROL_EOL;
775
776 lp->rx_ring_tail=0;
777
778 return 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400779}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781
782/**
783 * mc32_flush_rx_ring - free the ring of receive buffers
784 * @lp: Local data of 3c527 to flush the rx ring of
785 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400786 * Free the buffer for each ring slot. This may be called
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 * before mc32_load_rx_ring(), eg. on error in mc32_open().
788 * Requires rx skb pointers to point to a valid skb, or NULL.
789 */
790
791static void mc32_flush_rx_ring(struct net_device *dev)
792{
793 struct mc32_local *lp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400794 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400796 for(i=0; i < RX_RING_LEN; i++)
797 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (lp->rx_ring[i].skb) {
799 dev_kfree_skb(lp->rx_ring[i].skb);
800 lp->rx_ring[i].skb = NULL;
801 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400802 lp->rx_ring[i].p=NULL;
803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
806
807/**
808 * mc32_load_tx_ring - load transmit ring
809 * @dev: The 3c527 card to issue the command to
810 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400811 * This sets up the host transmit data-structures.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 *
813 * First, we obtain from the card it's current postion in the tx
814 * ring, so that we will know where to begin transmitting
815 * packets.
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400816 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 * Then, we read the 'next' pointers from the on-card tx ring into
818 * our tx_ring array to reduce slow shared-mem reads. Finally, we
819 * intitalise the tx house keeping variables.
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400820 *
821 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823static void mc32_load_tx_ring(struct net_device *dev)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400824{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 struct mc32_local *lp = netdev_priv(dev);
826 volatile struct skb_header *p;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400827 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 u16 tx_base;
829
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400830 tx_base=lp->tx_box->data[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 for(i=0 ; i<TX_RING_LEN ; i++)
833 {
834 p=isa_bus_to_virt(lp->base+tx_base);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400835 lp->tx_ring[i].p=p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 lp->tx_ring[i].skb=NULL;
837
838 tx_base=p->next;
839 }
840
841 /* -1 so that tx_ring_head cannot "lap" tx_ring_tail */
842 /* see mc32_tx_ring */
843
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400844 atomic_set(&lp->tx_count, TX_RING_LEN-1);
845 atomic_set(&lp->tx_ring_head, 0);
846 lp->tx_ring_tail=0;
847}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849
850/**
851 * mc32_flush_tx_ring - free transmit ring
852 * @lp: Local data of 3c527 to flush the tx ring of
853 *
854 * If the ring is non-empty, zip over the it, freeing any
855 * allocated skb_buffs. The tx ring house-keeping variables are
856 * then reset. Requires rx skb pointers to point to a valid skb,
857 * or NULL.
858 */
859
860static void mc32_flush_tx_ring(struct net_device *dev)
861{
862 struct mc32_local *lp = netdev_priv(dev);
863 int i;
864
865 for (i=0; i < TX_RING_LEN; i++)
866 {
867 if (lp->tx_ring[i].skb)
868 {
869 dev_kfree_skb(lp->tx_ring[i].skb);
870 lp->tx_ring[i].skb = NULL;
871 }
872 }
873
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400874 atomic_set(&lp->tx_count, 0);
875 atomic_set(&lp->tx_ring_head, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 lp->tx_ring_tail=0;
877}
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880/**
881 * mc32_open - handle 'up' of card
882 * @dev: device to open
883 *
884 * The user is trying to bring the card into ready state. This requires
885 * a brief dialogue with the card. Firstly we enable interrupts and then
886 * 'indications'. Without these enabled the card doesn't bother telling
887 * us what it has done. This had me puzzled for a week.
888 *
889 * We configure the number of card descriptors, then load the network
890 * address and multicast filters. Turn on the workaround mode. This
891 * works around a bug in the 82586 - it asks the firmware to do
892 * so. It has a performance (latency) hit but is needed on busy
893 * [read most] lans. We load the ring with buffers then we kick it
894 * all off.
895 */
896
897static int mc32_open(struct net_device *dev)
898{
899 int ioaddr = dev->base_addr;
900 struct mc32_local *lp = netdev_priv(dev);
901 u8 one=1;
902 u8 regs;
903 u16 descnumbuffs[2] = {TX_RING_LEN, RX_RING_LEN};
904
905 /*
906 * Interrupts enabled
907 */
908
909 regs=inb(ioaddr+HOST_CTRL);
910 regs|=HOST_CTRL_INTE;
911 outb(regs, ioaddr+HOST_CTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /*
914 * Allow ourselves to issue commands
915 */
916
917 up(&lp->cmd_mutex);
918
919
920 /*
921 * Send the indications on command
922 */
923
924 mc32_command(dev, 4, &one, 2);
925
926 /*
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400927 * Poke it to make sure it's really dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 */
929
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400930 mc32_halt_transceiver(dev);
931 mc32_flush_tx_ring(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400933 /*
934 * Ask card to set up on-card descriptors to our spec
935 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400937 if(mc32_command(dev, 8, descnumbuffs, 4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 printk("%s: %s rejected our buffer configuration!\n",
939 dev->name, cardname);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400940 mc32_close(dev);
941 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400943
944 /* Report new configuration */
945 mc32_command(dev, 6, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 lp->tx_chain = lp->exec_box->data[8]; /* Transmit list start offset */
948 lp->rx_chain = lp->exec_box->data[10]; /* Receive list start offset */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400949 lp->tx_len = lp->exec_box->data[9]; /* Transmit list count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 lp->rx_len = lp->exec_box->data[11]; /* Receive list count */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 /* Set Network Address */
953 mc32_command(dev, 1, dev->dev_addr, 6);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 /* Set the filters */
956 mc32_set_multicast_list(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400957
958 if (WORKAROUND_82586) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 u16 zero_word=0;
960 mc32_command(dev, 0x0D, &zero_word, 2); /* 82586 bug workaround on */
961 }
962
963 mc32_load_tx_ring(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400964
965 if(mc32_load_rx_ring(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 {
967 mc32_close(dev);
968 return -ENOBUFS;
969 }
970
971 lp->xceiver_desired_state = RUNNING;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 /* And finally, set the ball rolling... */
974 mc32_start_transceiver(dev);
975
976 netif_start_queue(dev);
977
978 return 0;
979}
980
981
982/**
983 * mc32_timeout - handle a timeout from the network layer
984 * @dev: 3c527 that timed out
985 *
986 * Handle a timeout on transmit from the 3c527. This normally means
987 * bad things as the hardware handles cable timeouts and mess for
988 * us.
989 *
990 */
991
992static void mc32_timeout(struct net_device *dev)
993{
994 printk(KERN_WARNING "%s: transmit timed out?\n", dev->name);
995 /* Try to restart the adaptor. */
996 netif_wake_queue(dev);
997}
998
999
1000/**
1001 * mc32_send_packet - queue a frame for transmit
1002 * @skb: buffer to transmit
1003 * @dev: 3c527 to send it out of
1004 *
1005 * Transmit a buffer. This normally means throwing the buffer onto
1006 * the transmit queue as the queue is quite large. If the queue is
1007 * full then we set tx_busy and return. Once the interrupt handler
1008 * gets messages telling it to reclaim transmit queue entries, we will
1009 * clear tx_busy and the kernel will start calling this again.
1010 *
1011 * We do not disable interrupts or acquire any locks; this can
1012 * run concurrently with mc32_tx_ring(), and the function itself
1013 * is serialised at a higher layer. However, similarly for the
1014 * card itself, we must ensure that we update tx_ring_head only
1015 * after we've established a valid packet on the tx ring (and
1016 * before we let the card "see" it, to prevent it racing with the
1017 * irq handler).
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001018 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 */
1020
1021static int mc32_send_packet(struct sk_buff *skb, struct net_device *dev)
1022{
1023 struct mc32_local *lp = netdev_priv(dev);
1024 u32 head = atomic_read(&lp->tx_ring_head);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 volatile struct skb_header *p, *np;
1027
1028 netif_stop_queue(dev);
1029
1030 if(atomic_read(&lp->tx_count)==0) {
1031 return 1;
1032 }
1033
Herbert Xu5b057c62006-06-23 02:06:41 -07001034 if (skb_padto(skb, ETH_ZLEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 netif_wake_queue(dev);
1036 return 0;
1037 }
1038
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001039 atomic_dec(&lp->tx_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 /* P is the last sending/sent buffer as a pointer */
1042 p=lp->tx_ring[head].p;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 head = next_tx(head);
1045
1046 /* NP is the buffer we will be loading */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001047 np=lp->tx_ring[head].p;
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /* We will need this to flush the buffer out */
1050 lp->tx_ring[head].skb=skb;
1051
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001052 np->length = unlikely(skb->len < ETH_ZLEN) ? ETH_ZLEN : skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 np->data = isa_virt_to_bus(skb->data);
1054 np->status = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001055 np->control = CONTROL_EOP | CONTROL_EOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 wmb();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 /*
1059 * The new frame has been setup; we can now
1060 * let the interrupt handler and card "see" it
1061 */
1062
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001063 atomic_set(&lp->tx_ring_head, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 p->control &= ~CONTROL_EOL;
1065
1066 netif_wake_queue(dev);
1067 return 0;
1068}
1069
1070
1071/**
1072 * mc32_update_stats - pull off the on board statistics
1073 * @dev: 3c527 to service
1074 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001075 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 * Query and reset the on-card stats. There's the small possibility
1077 * of a race here, which would result in an underestimation of
1078 * actual errors. As such, we'd prefer to keep all our stats
1079 * collection in software. As a rule, we do. However it can't be
1080 * used for rx errors and collisions as, by default, the card discards
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001081 * bad rx packets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 *
1083 * Setting the SAV BP in the rx filter command supposedly
1084 * stops this behaviour. However, testing shows that it only seems to
1085 * enable the collation of on-card rx statistics --- the driver
1086 * never sees an RX descriptor with an error status set.
1087 *
1088 */
1089
1090static void mc32_update_stats(struct net_device *dev)
1091{
1092 struct mc32_local *lp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001093 volatile struct mc32_stats *st = lp->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001095 u32 rx_errors=0;
1096
1097 rx_errors+=lp->net_stats.rx_crc_errors +=st->rx_crc_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 st->rx_crc_errors=0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001099 rx_errors+=lp->net_stats.rx_fifo_errors +=st->rx_overrun_errors;
1100 st->rx_overrun_errors=0;
1101 rx_errors+=lp->net_stats.rx_frame_errors +=st->rx_alignment_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 st->rx_alignment_errors=0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001103 rx_errors+=lp->net_stats.rx_length_errors+=st->rx_tooshort_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 st->rx_tooshort_errors=0;
1105 rx_errors+=lp->net_stats.rx_missed_errors+=st->rx_outofresource_errors;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001106 st->rx_outofresource_errors=0;
1107 lp->net_stats.rx_errors=rx_errors;
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 /* Number of packets which saw one collision */
1110 lp->net_stats.collisions+=st->dataC[10];
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001111 st->dataC[10]=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001113 /* Number of packets which saw 2--15 collisions */
1114 lp->net_stats.collisions+=st->dataC[11];
1115 st->dataC[11]=0;
1116}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118
1119/**
1120 * mc32_rx_ring - process the receive ring
1121 * @dev: 3c527 that needs its receive ring processing
1122 *
1123 *
1124 * We have received one or more indications from the card that a
1125 * receive has completed. The buffer ring thus contains dirty
1126 * entries. We walk the ring by iterating over the circular rx_ring
1127 * array, starting at the next dirty buffer (which happens to be the
1128 * one we finished up at last time around).
1129 *
1130 * For each completed packet, we will either copy it and pass it up
1131 * the stack or, if the packet is near MTU sized, we allocate
1132 * another buffer and flip the old one up the stack.
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001133 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 * We must succeed in keeping a buffer on the ring. If necessary we
1135 * will toss a received packet rather than lose a ring entry. Once
1136 * the first uncompleted descriptor is found, we move the
1137 * End-Of-List bit to include the buffers just processed.
1138 *
1139 */
1140
1141static void mc32_rx_ring(struct net_device *dev)
1142{
1143 struct mc32_local *lp = netdev_priv(dev);
1144 volatile struct skb_header *p;
1145 u16 rx_ring_tail;
1146 u16 rx_old_tail;
1147 int x=0;
1148
1149 rx_old_tail = rx_ring_tail = lp->rx_ring_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001151 do
1152 {
1153 p=lp->rx_ring[rx_ring_tail].p;
1154
1155 if(!(p->status & (1<<7))) { /* Not COMPLETED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if(p->status & (1<<6)) /* COMPLETED_OK */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001159 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 u16 length=p->length;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001162 struct sk_buff *skb;
1163 struct sk_buff *newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 /* Try to save time by avoiding a copy on big frames */
1166
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001167 if ((length > RX_COPYBREAK)
1168 && ((newskb=dev_alloc_skb(1532)) != NULL))
1169 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 skb=lp->rx_ring[rx_ring_tail].skb;
1171 skb_put(skb, length);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001172
1173 skb_reserve(newskb,18);
1174 lp->rx_ring[rx_ring_tail].skb=newskb;
1175 p->data=isa_virt_to_bus(newskb->data);
1176 }
1177 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001179 skb=dev_alloc_skb(length+2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 if(skb==NULL) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001182 lp->net_stats.rx_dropped++;
1183 goto dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
1185
1186 skb_reserve(skb,2);
1187 memcpy(skb_put(skb, length),
1188 lp->rx_ring[rx_ring_tail].skb->data, length);
1189 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001190
1191 skb->protocol=eth_type_trans(skb,dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 dev->last_rx = jiffies;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001193 lp->net_stats.rx_packets++;
1194 lp->net_stats.rx_bytes += length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 netif_rx(skb);
1196 }
1197
1198 dropped:
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001199 p->length = 1532;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 p->status = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001201
1202 rx_ring_tail=next_rx(rx_ring_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001204 while(x++<48);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001206 /* If there was actually a frame to be processed, place the EOL bit */
1207 /* at the descriptor prior to the one to be filled next */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001209 if (rx_ring_tail != rx_old_tail)
1210 {
1211 lp->rx_ring[prev_rx(rx_ring_tail)].p->control |= CONTROL_EOL;
1212 lp->rx_ring[prev_rx(rx_old_tail)].p->control &= ~CONTROL_EOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001214 lp->rx_ring_tail=rx_ring_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216}
1217
1218
1219/**
1220 * mc32_tx_ring - process completed transmits
1221 * @dev: 3c527 that needs its transmit ring processing
1222 *
1223 *
1224 * This operates in a similar fashion to mc32_rx_ring. We iterate
1225 * over the transmit ring. For each descriptor which has been
1226 * processed by the card, we free its associated buffer and note
1227 * any errors. This continues until the transmit ring is emptied
1228 * or we reach a descriptor that hasn't yet been processed by the
1229 * card.
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001230 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 */
1232
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001233static void mc32_tx_ring(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
1235 struct mc32_local *lp = netdev_priv(dev);
1236 volatile struct skb_header *np;
1237
1238 /*
1239 * We rely on head==tail to mean 'queue empty'.
1240 * This is why lp->tx_count=TX_RING_LEN-1: in order to prevent
1241 * tx_ring_head wrapping to tail and confusing a 'queue empty'
1242 * condition with 'queue full'
1243 */
1244
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001245 while (lp->tx_ring_tail != atomic_read(&lp->tx_ring_head))
1246 {
1247 u16 t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001249 t=next_tx(lp->tx_ring_tail);
1250 np=lp->tx_ring[t].p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001252 if(!(np->status & (1<<7)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001254 /* Not COMPLETED */
1255 break;
1256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 lp->net_stats.tx_packets++;
1258 if(!(np->status & (1<<6))) /* Not COMPLETED_OK */
1259 {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001260 lp->net_stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 switch(np->status&0x0F)
1263 {
1264 case 1:
1265 lp->net_stats.tx_aborted_errors++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001266 break; /* Max collisions */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 case 2:
1268 lp->net_stats.tx_fifo_errors++;
1269 break;
1270 case 3:
1271 lp->net_stats.tx_carrier_errors++;
1272 break;
1273 case 4:
1274 lp->net_stats.tx_window_errors++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001275 break; /* CTS Lost */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 case 5:
1277 lp->net_stats.tx_aborted_errors++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001278 break; /* Transmit timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280 }
1281 /* Packets are sent in order - this is
1282 basically a FIFO queue of buffers matching
1283 the card ring */
1284 lp->net_stats.tx_bytes+=lp->tx_ring[t].skb->len;
1285 dev_kfree_skb_irq(lp->tx_ring[t].skb);
1286 lp->tx_ring[t].skb=NULL;
1287 atomic_inc(&lp->tx_count);
1288 netif_wake_queue(dev);
1289
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001290 lp->tx_ring_tail=t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
1292
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001293}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295
1296/**
1297 * mc32_interrupt - handle an interrupt from a 3c527
1298 * @irq: Interrupt number
1299 * @dev_id: 3c527 that requires servicing
1300 * @regs: Registers (unused)
1301 *
1302 *
1303 * An interrupt is raised whenever the 3c527 writes to the command
1304 * register. This register contains the message it wishes to send us
1305 * packed into a single byte field. We keep reading status entries
1306 * until we have processed all the control items, but simply count
1307 * transmit and receive reports. When all reports are in we empty the
1308 * transceiver rings as appropriate. This saves the overhead of
1309 * multiple command requests.
1310 *
1311 * Because MCA is level-triggered, we shouldn't miss indications.
1312 * Therefore, we needn't ask the card to suspend interrupts within
1313 * this handler. The card receives an implicit acknowledgment of the
1314 * current interrupt when we read the command register.
1315 *
1316 */
1317
David Howells7d12e782006-10-05 14:55:46 +01001318static irqreturn_t mc32_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 struct net_device *dev = dev_id;
1321 struct mc32_local *lp;
1322 int ioaddr, status, boguscount = 0;
1323 int rx_event = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001324 int tx_event = 0;
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 ioaddr = dev->base_addr;
1327 lp = netdev_priv(dev);
1328
1329 /* See whats cooking */
1330
1331 while((inb(ioaddr+HOST_STATUS)&HOST_STATUS_CWR) && boguscount++<2000)
1332 {
1333 status=inb(ioaddr+HOST_CMD);
1334
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001335#ifdef DEBUG_IRQ
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 printk("Status TX%d RX%d EX%d OV%d BC%d\n",
1337 (status&7), (status>>3)&7, (status>>6)&1,
1338 (status>>7)&1, boguscount);
1339#endif
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 switch(status&7)
1342 {
1343 case 0:
1344 break;
1345 case 6: /* TX fail */
1346 case 2: /* TX ok */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001347 tx_event = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 break;
1349 case 3: /* Halt */
1350 case 4: /* Abort */
1351 complete(&lp->xceiver_cmd);
1352 break;
1353 default:
1354 printk("%s: strange tx ack %d\n", dev->name, status&7);
1355 }
1356 status>>=3;
1357 switch(status&7)
1358 {
1359 case 0:
1360 break;
1361 case 2: /* RX */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001362 rx_event=1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 break;
1364 case 3: /* Halt */
1365 case 4: /* Abort */
1366 complete(&lp->xceiver_cmd);
1367 break;
1368 case 6:
1369 /* Out of RX buffers stat */
1370 /* Must restart rx */
1371 lp->net_stats.rx_dropped++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001372 mc32_rx_ring(dev);
1373 mc32_start_transceiver(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 break;
1375 default:
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001376 printk("%s: strange rx ack %d\n",
1377 dev->name, status&7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
1379 status>>=3;
1380 if(status&1)
1381 {
1382 /*
1383 * No thread is waiting: we need to tidy
1384 * up ourself.
1385 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (lp->cmd_nonblocking) {
1388 up(&lp->cmd_mutex);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001389 if (lp->mc_reload_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 mc32_reset_multicast_list(dev);
1391 }
1392 else complete(&lp->execution_cmd);
1393 }
1394 if(status&2)
1395 {
1396 /*
1397 * We get interrupted once per
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001398 * counter that is about to overflow.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 */
1400
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001401 mc32_update_stats(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403 }
1404
1405
1406 /*
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001407 * Process the transmit and receive rings
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 */
1409
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001410 if(tx_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 mc32_tx_ring(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001412
1413 if(rx_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 mc32_rx_ring(dev);
1415
1416 return IRQ_HANDLED;
1417}
1418
1419
1420/**
1421 * mc32_close - user configuring the 3c527 down
1422 * @dev: 3c527 card to shut down
1423 *
1424 * The 3c527 is a bus mastering device. We must be careful how we
1425 * shut it down. It may also be running shared interrupt so we have
1426 * to be sure to silence it properly
1427 *
1428 * We indicate that the card is closing to the rest of the
1429 * driver. Otherwise, it is possible that the card may run out
1430 * of receive buffers and restart the transceiver while we're
1431 * trying to close it.
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001432 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 * We abort any receive and transmits going on and then wait until
1434 * any pending exec commands have completed in other code threads.
1435 * In theory we can't get here while that is true, in practice I am
1436 * paranoid
1437 *
1438 * We turn off the interrupt enable for the board to be sure it can't
1439 * intefere with other devices.
1440 */
1441
1442static int mc32_close(struct net_device *dev)
1443{
1444 struct mc32_local *lp = netdev_priv(dev);
1445 int ioaddr = dev->base_addr;
1446
1447 u8 regs;
1448 u16 one=1;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 lp->xceiver_desired_state = HALTED;
1451 netif_stop_queue(dev);
1452
1453 /*
1454 * Send the indications on command (handy debug check)
1455 */
1456
1457 mc32_command(dev, 4, &one, 2);
1458
1459 /* Shut down the transceiver */
1460
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001461 mc32_halt_transceiver(dev);
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 /* Ensure we issue no more commands beyond this point */
1464
1465 down(&lp->cmd_mutex);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001466
1467 /* Ok the card is now stopping */
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 regs=inb(ioaddr+HOST_CTRL);
1470 regs&=~HOST_CTRL_INTE;
1471 outb(regs, ioaddr+HOST_CTRL);
1472
1473 mc32_flush_rx_ring(dev);
1474 mc32_flush_tx_ring(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001475
1476 mc32_update_stats(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 return 0;
1479}
1480
1481
1482/**
1483 * mc32_get_stats - hand back stats to network layer
1484 * @dev: The 3c527 card to handle
1485 *
1486 * We've collected all the stats we can in software already. Now
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001487 * it's time to update those kept on-card and return the lot.
1488 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 */
1490
1491static struct net_device_stats *mc32_get_stats(struct net_device *dev)
1492{
1493 struct mc32_local *lp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001494
1495 mc32_update_stats(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 return &lp->net_stats;
1497}
1498
1499
1500/**
1501 * do_mc32_set_multicast_list - attempt to update multicasts
1502 * @dev: 3c527 device to load the list on
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001503 * @retry: indicates this is not the first call.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 *
1505 *
1506 * Actually set or clear the multicast filter for this adaptor. The
1507 * locking issues are handled by this routine. We have to track
1508 * state as it may take multiple calls to get the command sequence
1509 * completed. We just keep trying to schedule the loads until we
1510 * manage to process them all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001512 * num_addrs == -1 Promiscuous mode, receive all packets
1513 *
1514 * num_addrs == 0 Normal mode, clear multicast list
1515 *
1516 * num_addrs > 0 Multicast mode, receive normal and MC packets,
1517 * and do best-effort filtering.
1518 *
1519 * See mc32_update_stats() regards setting the SAV BP bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 *
1521 */
1522
1523static void do_mc32_set_multicast_list(struct net_device *dev, int retry)
1524{
1525 struct mc32_local *lp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001526 u16 filt = (1<<2); /* Save Bad Packets, for stats purposes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 if (dev->flags&IFF_PROMISC)
1529 /* Enable promiscuous mode */
1530 filt |= 1;
1531 else if((dev->flags&IFF_ALLMULTI) || dev->mc_count > 10)
1532 {
1533 dev->flags|=IFF_PROMISC;
1534 filt |= 1;
1535 }
1536 else if(dev->mc_count)
1537 {
1538 unsigned char block[62];
1539 unsigned char *bp;
1540 struct dev_mc_list *dmc=dev->mc_list;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if(retry==0)
1545 lp->mc_list_valid = 0;
1546 if(!lp->mc_list_valid)
1547 {
1548 block[1]=0;
1549 block[0]=dev->mc_count;
1550 bp=block+2;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 for(i=0;i<dev->mc_count;i++)
1553 {
1554 memcpy(bp, dmc->dmi_addr, 6);
1555 bp+=6;
1556 dmc=dmc->next;
1557 }
1558 if(mc32_command_nowait(dev, 2, block, 2+6*dev->mc_count)==-1)
1559 {
1560 lp->mc_reload_wait = 1;
1561 return;
1562 }
1563 lp->mc_list_valid=1;
1564 }
1565 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001566
1567 if(mc32_command_nowait(dev, 0, &filt, 2)==-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 {
1569 lp->mc_reload_wait = 1;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001570 }
1571 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 lp->mc_reload_wait = 0;
1573 }
1574}
1575
1576
1577/**
1578 * mc32_set_multicast_list - queue multicast list update
1579 * @dev: The 3c527 to use
1580 *
1581 * Commence loading the multicast list. This is called when the kernel
1582 * changes the lists. It will override any pending list we are trying to
1583 * load.
1584 */
1585
1586static void mc32_set_multicast_list(struct net_device *dev)
1587{
1588 do_mc32_set_multicast_list(dev,0);
1589}
1590
1591
1592/**
1593 * mc32_reset_multicast_list - reset multicast list
1594 * @dev: The 3c527 to use
1595 *
1596 * Attempt the next step in loading the multicast lists. If this attempt
1597 * fails to complete then it will be scheduled and this function called
1598 * again later from elsewhere.
1599 */
1600
1601static void mc32_reset_multicast_list(struct net_device *dev)
1602{
1603 do_mc32_set_multicast_list(dev,1);
1604}
1605
1606static void netdev_get_drvinfo(struct net_device *dev,
1607 struct ethtool_drvinfo *info)
1608{
1609 strcpy(info->driver, DRV_NAME);
1610 strcpy(info->version, DRV_VERSION);
1611 sprintf(info->bus_info, "MCA 0x%lx", dev->base_addr);
1612}
1613
1614static u32 netdev_get_msglevel(struct net_device *dev)
1615{
1616 return mc32_debug;
1617}
1618
1619static void netdev_set_msglevel(struct net_device *dev, u32 level)
1620{
1621 mc32_debug = level;
1622}
1623
Jeff Garzik7282d492006-09-13 14:30:00 -04001624static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 .get_drvinfo = netdev_get_drvinfo,
1626 .get_msglevel = netdev_get_msglevel,
1627 .set_msglevel = netdev_set_msglevel,
1628};
1629
1630#ifdef MODULE
1631
1632static struct net_device *this_device;
1633
1634/**
1635 * init_module - entry point
1636 *
1637 * Probe and locate a 3c527 card. This really should probe and locate
1638 * all the 3c527 cards in the machine not just one of them. Yes you can
1639 * insmod multiple modules for now but it's a hack.
1640 */
1641
Randy Dunlap96e672c2006-06-10 13:33:48 -07001642int __init init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
1644 this_device = mc32_probe(-1);
1645 if (IS_ERR(this_device))
1646 return PTR_ERR(this_device);
1647 return 0;
1648}
1649
1650/**
1651 * cleanup_module - free resources for an unload
1652 *
1653 * Unloading time. We release the MCA bus resources and the interrupt
1654 * at which point everything is ready to unload. The card must be stopped
1655 * at this point or we would not have been called. When we unload we
1656 * leave the card stopped but not totally shut down. When the card is
1657 * initialized it must be rebooted or the rings reloaded before any
1658 * transmit operations are allowed to start scribbling into memory.
1659 */
1660
Al Viroafc8eb42006-06-14 18:50:53 -04001661void __exit cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
1663 unregister_netdev(this_device);
1664 cleanup_card(this_device);
1665 free_netdev(this_device);
1666}
1667
1668#endif /* MODULE */