blob: 2df3af3b9b20130ead927f93db05730029430d9c [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>
Matthew Wilcox6188e102008-04-18 22:21:05 -0400106#include <linux/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 volatile struct mc32_mailbox *rx_box;
162 volatile struct mc32_mailbox *tx_box;
163 volatile struct mc32_mailbox *exec_box;
164 volatile struct mc32_stats *stats; /* Start of on-card statistics */
165 u16 tx_chain; /* Transmit list start offset */
166 u16 rx_chain; /* Receive list start offset */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400167 u16 tx_len; /* Transmit list count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 u16 rx_len; /* Receive list count */
169
170 u16 xceiver_desired_state; /* HALTED or RUNNING */
171 u16 cmd_nonblocking; /* Thread is uninterested in command result */
172 u16 mc_reload_wait; /* A multicast load request is pending */
173 u32 mc_list_valid; /* True when the mclist is set */
174
175 struct mc32_ring_desc tx_ring[TX_RING_LEN]; /* Host Transmit ring */
176 struct mc32_ring_desc rx_ring[RX_RING_LEN]; /* Host Receive ring */
177
178 atomic_t tx_count; /* buffers left */
179 atomic_t tx_ring_head; /* index to tx en-queue end */
180 u16 tx_ring_tail; /* index to tx de-queue end */
181
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400182 u16 rx_ring_tail; /* index to rx de-queue end */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 struct semaphore cmd_mutex; /* Serialises issuing of execute commands */
185 struct completion execution_cmd; /* Card has completed an execute command */
186 struct completion xceiver_cmd; /* Card has completed a tx or rx command */
187};
188
189/* The station (ethernet) address prefix, used for a sanity check. */
190#define SA_ADDR0 0x02
191#define SA_ADDR1 0x60
192#define SA_ADDR2 0xAC
193
194struct mca_adapters_t {
195 unsigned int id;
196 char *name;
197};
198
199static const struct mca_adapters_t mc32_adapters[] = {
200 { 0x0041, "3COM EtherLink MC/32" },
201 { 0x8EF5, "IBM High Performance Lan Adapter" },
202 { 0x0000, NULL }
203};
204
205
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400206/* Macros for ring index manipulations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207static inline u16 next_rx(u16 rx) { return (rx+1)&(RX_RING_LEN-1); };
208static inline u16 prev_rx(u16 rx) { return (rx-1)&(RX_RING_LEN-1); };
209
210static inline u16 next_tx(u16 tx) { return (tx+1)&(TX_RING_LEN-1); };
211
212
213/* Index to functions, as function prototypes. */
214static int mc32_probe1(struct net_device *dev, int ioaddr);
215static int mc32_command(struct net_device *dev, u16 cmd, void *data, int len);
216static int mc32_open(struct net_device *dev);
217static void mc32_timeout(struct net_device *dev);
218static int mc32_send_packet(struct sk_buff *skb, struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100219static irqreturn_t mc32_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220static int mc32_close(struct net_device *dev);
221static struct net_device_stats *mc32_get_stats(struct net_device *dev);
222static void mc32_set_multicast_list(struct net_device *dev);
223static void mc32_reset_multicast_list(struct net_device *dev);
Jeff Garzik7282d492006-09-13 14:30:00 -0400224static const struct ethtool_ops netdev_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226static void cleanup_card(struct net_device *dev)
227{
228 struct mc32_local *lp = netdev_priv(dev);
229 unsigned slot = lp->slot;
230 mca_mark_as_unused(slot);
231 mca_set_adapter_name(slot, NULL);
232 free_irq(dev->irq, dev);
233 release_region(dev->base_addr, MC32_IO_EXTENT);
234}
235
236/**
237 * mc32_probe - Search for supported boards
238 * @unit: interface number to use
239 *
240 * Because MCA bus is a real bus and we can scan for cards we could do a
241 * single scan for all boards here. Right now we use the passed in device
242 * structure and scan for only one board. This needs fixing for modules
243 * in particular.
244 */
245
246struct net_device *__init mc32_probe(int unit)
247{
248 struct net_device *dev = alloc_etherdev(sizeof(struct mc32_local));
249 static int current_mca_slot = -1;
250 int i;
251 int err;
252
253 if (!dev)
254 return ERR_PTR(-ENOMEM);
255
256 if (unit >= 0)
257 sprintf(dev->name, "eth%d", unit);
258
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400259 /* Do not check any supplied i/o locations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 POS registers usually don't fail :) */
261
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400262 /* MCA cards have POS registers.
263 Autodetecting MCA cards is extremely simple.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 Just search for the card. */
265
266 for(i = 0; (mc32_adapters[i].name != NULL); i++) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400267 current_mca_slot =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 mca_find_unused_adapter(mc32_adapters[i].id, 0);
269
270 if(current_mca_slot != MCA_NOTFOUND) {
271 if(!mc32_probe1(dev, current_mca_slot))
272 {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400273 mca_set_adapter_name(current_mca_slot,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 mc32_adapters[i].name);
275 mca_mark_as_used(current_mca_slot);
276 err = register_netdev(dev);
277 if (err) {
278 cleanup_card(dev);
279 free_netdev(dev);
280 dev = ERR_PTR(err);
281 }
282 return dev;
283 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286 }
287 free_netdev(dev);
288 return ERR_PTR(-ENODEV);
289}
290
291/**
292 * mc32_probe1 - Check a given slot for a board and test the card
293 * @dev: Device structure to fill in
294 * @slot: The MCA bus slot being used by this card
295 *
296 * Decode the slot data and configure the card structures. Having done this we
297 * can reset the card and configure it. The card does a full self test cycle
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400298 * in firmware so we have to wait for it to return and post us either a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * failure case or some addresses we use to find the board internals.
300 */
301
302static int __init mc32_probe1(struct net_device *dev, int slot)
303{
304 static unsigned version_printed;
305 int i, err;
306 u8 POS;
307 u32 base;
308 struct mc32_local *lp = netdev_priv(dev);
309 static u16 mca_io_bases[]={
310 0x7280,0x7290,
311 0x7680,0x7690,
312 0x7A80,0x7A90,
313 0x7E80,0x7E90
314 };
315 static u32 mca_mem_bases[]={
316 0x00C0000,
317 0x00C4000,
318 0x00C8000,
319 0x00CC000,
320 0x00D0000,
321 0x00D4000,
322 0x00D8000,
323 0x00DC000
324 };
325 static char *failures[]={
326 "Processor instruction",
327 "Processor data bus",
328 "Processor data bus",
329 "Processor data bus",
330 "Adapter bus",
331 "ROM checksum",
332 "Base RAM",
333 "Extended RAM",
334 "82586 internal loopback",
335 "82586 initialisation failure",
336 "Adapter list configuration error"
337 };
338
339 /* Time to play MCA games */
340
341 if (mc32_debug && version_printed++ == 0)
342 printk(KERN_DEBUG "%s", version);
343
344 printk(KERN_INFO "%s: %s found in slot %d:", dev->name, cardname, slot);
345
346 POS = mca_read_stored_pos(slot, 2);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if(!(POS&1))
349 {
350 printk(" disabled.\n");
351 return -ENODEV;
352 }
353
354 /* Fill in the 'dev' fields. */
355 dev->base_addr = mca_io_bases[(POS>>1)&7];
356 dev->mem_start = mca_mem_bases[(POS>>4)&7];
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 POS = mca_read_stored_pos(slot, 4);
359 if(!(POS&1))
360 {
361 printk("memory window disabled.\n");
362 return -ENODEV;
363 }
364
365 POS = mca_read_stored_pos(slot, 5);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 i=(POS>>4)&3;
368 if(i==3)
369 {
370 printk("invalid memory window.\n");
371 return -ENODEV;
372 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 i*=16384;
375 i+=16384;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 dev->mem_end=dev->mem_start + i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 dev->irq = ((POS>>2)&3)+9;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if(!request_region(dev->base_addr, MC32_IO_EXTENT, cardname))
382 {
383 printk("io 0x%3lX, which is busy.\n", dev->base_addr);
384 return -EBUSY;
385 }
386
387 printk("io 0x%3lX irq %d mem 0x%lX (%dK)\n",
388 dev->base_addr, dev->irq, dev->mem_start, i/1024);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400389
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /* We ought to set the cache line size here.. */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400392
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /*
395 * Go PROM browsing
396 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* Retrieve and print the ethernet address. */
399 for (i = 0; i < 6; i++)
400 {
401 mca_write_pos(slot, 6, i+12);
402 mca_write_pos(slot, 7, 0);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400403
Joe Perches0795af52007-10-03 17:59:30 -0700404 dev->dev_addr[i] = mca_read_pos(slot,3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406
Johannes Berge1749612008-10-27 15:59:26 -0700407 printk("%s: Address %pM", dev->name, dev->dev_addr);
Joe Perches0795af52007-10-03 17:59:30 -0700408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 mca_write_pos(slot, 6, 0);
410 mca_write_pos(slot, 7, 0);
411
412 POS = mca_read_stored_pos(slot, 4);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if(POS&2)
415 printk(" : BNC port selected.\n");
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400416 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 printk(" : AUI port selected.\n");
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 POS=inb(dev->base_addr+HOST_CTRL);
420 POS|=HOST_CTRL_ATTN|HOST_CTRL_RESET;
421 POS&=~HOST_CTRL_INTE;
422 outb(POS, dev->base_addr+HOST_CTRL);
423 /* Reset adapter */
424 udelay(100);
425 /* Reset off */
426 POS&=~(HOST_CTRL_ATTN|HOST_CTRL_RESET);
427 outb(POS, dev->base_addr+HOST_CTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 udelay(300);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /*
432 * Grab the IRQ
433 */
434
Thomas Gleixner1fb9df52006-07-01 19:29:39 -0700435 err = request_irq(dev->irq, &mc32_interrupt, IRQF_SHARED | IRQF_SAMPLE_RANDOM, DRV_NAME, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (err) {
437 release_region(dev->base_addr, MC32_IO_EXTENT);
438 printk(KERN_ERR "%s: unable to get IRQ %d.\n", DRV_NAME, dev->irq);
439 goto err_exit_ports;
440 }
441
442 memset(lp, 0, sizeof(struct mc32_local));
443 lp->slot = slot;
444
445 i=0;
446
447 base = inb(dev->base_addr);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 while(base == 0xFF)
450 {
451 i++;
452 if(i == 1000)
453 {
454 printk(KERN_ERR "%s: failed to boot adapter.\n", dev->name);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400455 err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 goto err_exit_irq;
457 }
458 udelay(1000);
459 if(inb(dev->base_addr+2)&(1<<5))
460 base = inb(dev->base_addr);
461 }
462
463 if(base>0)
464 {
465 if(base < 0x0C)
466 printk(KERN_ERR "%s: %s%s.\n", dev->name, failures[base-1],
467 base<0x0A?" test failure":"");
468 else
469 printk(KERN_ERR "%s: unknown failure %d.\n", dev->name, base);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400470 err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 goto err_exit_irq;
472 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 base=0;
475 for(i=0;i<4;i++)
476 {
477 int n=0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 while(!(inb(dev->base_addr+2)&(1<<5)))
480 {
481 n++;
482 udelay(50);
483 if(n>100)
484 {
485 printk(KERN_ERR "%s: mailbox read fail (%d).\n", dev->name, i);
486 err = -ENODEV;
487 goto err_exit_irq;
488 }
489 }
490
491 base|=(inb(dev->base_addr)<<(8*i));
492 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 lp->exec_box=isa_bus_to_virt(dev->mem_start+base);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400495
496 base=lp->exec_box->data[1]<<16|lp->exec_box->data[0];
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 lp->base = dev->mem_start+base;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400499
500 lp->rx_box=isa_bus_to_virt(lp->base + lp->exec_box->data[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 lp->tx_box=isa_bus_to_virt(lp->base + lp->exec_box->data[3]);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 lp->stats = isa_bus_to_virt(lp->base + lp->exec_box->data[5]);
504
505 /*
506 * Descriptor chains (card relative)
507 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 lp->tx_chain = lp->exec_box->data[8]; /* Transmit list start offset */
510 lp->rx_chain = lp->exec_box->data[10]; /* Receive list start offset */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400511 lp->tx_len = lp->exec_box->data[9]; /* Transmit list count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 lp->rx_len = lp->exec_box->data[11]; /* Receive list count */
513
514 init_MUTEX_LOCKED(&lp->cmd_mutex);
515 init_completion(&lp->execution_cmd);
516 init_completion(&lp->xceiver_cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 printk("%s: Firmware Rev %d. %d RX buffers, %d TX buffers. Base of 0x%08X.\n",
519 dev->name, lp->exec_box->data[12], lp->rx_len, lp->tx_len, lp->base);
520
521 dev->open = mc32_open;
522 dev->stop = mc32_close;
523 dev->hard_start_xmit = mc32_send_packet;
524 dev->get_stats = mc32_get_stats;
525 dev->set_multicast_list = mc32_set_multicast_list;
526 dev->tx_timeout = mc32_timeout;
527 dev->watchdog_timeo = HZ*5; /* Board does all the work */
528 dev->ethtool_ops = &netdev_ethtool_ops;
529
530 return 0;
531
532err_exit_irq:
533 free_irq(dev->irq, dev);
534err_exit_ports:
535 release_region(dev->base_addr, MC32_IO_EXTENT);
536 return err;
537}
538
539
540/**
541 * mc32_ready_poll - wait until we can feed it a command
542 * @dev: The device to wait for
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400543 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 * Wait until the card becomes ready to accept a command via the
545 * command register. This tells us nothing about the completion
546 * status of any pending commands and takes very little time at all.
547 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549static inline void mc32_ready_poll(struct net_device *dev)
550{
551 int ioaddr = dev->base_addr;
552 while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
553}
554
555
556/**
557 * mc32_command_nowait - send a command non blocking
558 * @dev: The 3c527 to issue the command to
559 * @cmd: The command word to write to the mailbox
560 * @data: A data block if the command expects one
561 * @len: Length of the data block
562 *
563 * Send a command from interrupt state. If there is a command
564 * currently being executed then we return an error of -1. It
565 * simply isn't viable to wait around as commands may be
566 * slow. This can theoretically be starved on SMP, but it's hard
567 * to see a realistic situation. We do not wait for the command
568 * to complete --- we rely on the interrupt handler to tidy up
569 * after us.
570 */
571
572static int mc32_command_nowait(struct net_device *dev, u16 cmd, void *data, int len)
573{
574 struct mc32_local *lp = netdev_priv(dev);
575 int ioaddr = dev->base_addr;
576 int ret = -1;
577
578 if (down_trylock(&lp->cmd_mutex) == 0)
579 {
580 lp->cmd_nonblocking=1;
581 lp->exec_box->mbox=0;
582 lp->exec_box->mbox=cmd;
583 memcpy((void *)lp->exec_box->data, data, len);
584 barrier(); /* the memcpy forgot the volatile so be sure */
585
586 /* Send the command */
587 mc32_ready_poll(dev);
588 outb(1<<6, ioaddr+HOST_CMD);
589
590 ret = 0;
591
592 /* Interrupt handler will signal mutex on completion */
593 }
594
595 return ret;
596}
597
598
599/**
600 * mc32_command - send a command and sleep until completion
601 * @dev: The 3c527 card to issue the command to
602 * @cmd: The command word to write to the mailbox
603 * @data: A data block if the command expects one
604 * @len: Length of the data block
605 *
606 * Sends exec commands in a user context. This permits us to wait around
607 * for the replies and also to wait for the command buffer to complete
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400608 * from a previous command before we execute our command. After our
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 * command completes we will attempt any pending multicast reload
610 * we blocked off by hogging the exec buffer.
611 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400612 * You feed the card a command, you wait, it interrupts you get a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 * reply. All well and good. The complication arises because you use
614 * commands for filter list changes which come in at bh level from things
615 * like IPV6 group stuff.
616 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618static int mc32_command(struct net_device *dev, u16 cmd, void *data, int len)
619{
620 struct mc32_local *lp = netdev_priv(dev);
621 int ioaddr = dev->base_addr;
622 int ret = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 down(&lp->cmd_mutex);
625
626 /*
627 * My Turn
628 */
629
630 lp->cmd_nonblocking=0;
631 lp->exec_box->mbox=0;
632 lp->exec_box->mbox=cmd;
633 memcpy((void *)lp->exec_box->data, data, len);
634 barrier(); /* the memcpy forgot the volatile so be sure */
635
636 mc32_ready_poll(dev);
637 outb(1<<6, ioaddr+HOST_CMD);
638
639 wait_for_completion(&lp->execution_cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if(lp->exec_box->mbox&(1<<13))
642 ret = -1;
643
644 up(&lp->cmd_mutex);
645
646 /*
647 * A multicast set got blocked - try it now
648 */
649
650 if(lp->mc_reload_wait)
651 {
652 mc32_reset_multicast_list(dev);
653 }
654
655 return ret;
656}
657
658
659/**
660 * mc32_start_transceiver - tell board to restart tx/rx
661 * @dev: The 3c527 card to issue the command to
662 *
663 * This may be called from the interrupt state, where it is used
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400664 * to restart the rx ring if the card runs out of rx buffers.
665 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 * We must first check if it's ok to (re)start the transceiver. See
667 * mc32_close for details.
668 */
669
670static void mc32_start_transceiver(struct net_device *dev) {
671
672 struct mc32_local *lp = netdev_priv(dev);
673 int ioaddr = dev->base_addr;
674
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400675 /* Ignore RX overflow on device closure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (lp->xceiver_desired_state==HALTED)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400677 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 /* Give the card the offset to the post-EOL-bit RX descriptor */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400680 mc32_ready_poll(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 lp->rx_box->mbox=0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400682 lp->rx_box->data[0]=lp->rx_ring[prev_rx(lp->rx_ring_tail)].p->next;
683 outb(HOST_CMD_START_RX, ioaddr+HOST_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400685 mc32_ready_poll(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 lp->tx_box->mbox=0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400687 outb(HOST_CMD_RESTRT_TX, ioaddr+HOST_CMD); /* card ignores this on RX restart */
688
689 /* We are not interrupted on start completion */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
692
693/**
694 * mc32_halt_transceiver - tell board to stop tx/rx
695 * @dev: The 3c527 card to issue the command to
696 *
697 * We issue the commands to halt the card's transceiver. In fact,
698 * after some experimenting we now simply tell the card to
699 * suspend. When issuing aborts occasionally odd things happened.
700 *
701 * We then sleep until the card has notified us that both rx and
702 * tx have been suspended.
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400703 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400705static void mc32_halt_transceiver(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 struct mc32_local *lp = netdev_priv(dev);
708 int ioaddr = dev->base_addr;
709
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400710 mc32_ready_poll(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 lp->rx_box->mbox=0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400712 outb(HOST_CMD_SUSPND_RX, ioaddr+HOST_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 wait_for_completion(&lp->xceiver_cmd);
714
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400715 mc32_ready_poll(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 lp->tx_box->mbox=0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400717 outb(HOST_CMD_SUSPND_TX, ioaddr+HOST_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 wait_for_completion(&lp->xceiver_cmd);
719}
720
721
722/**
723 * mc32_load_rx_ring - load the ring of receive buffers
724 * @dev: 3c527 to build the ring for
725 *
726 * This initalises the on-card and driver datastructures to
727 * the point where mc32_start_transceiver() can be called.
728 *
729 * The card sets up the receive ring for us. We are required to use the
730 * ring it provides, although the size of the ring is configurable.
731 *
732 * We allocate an sk_buff for each ring entry in turn and
733 * initalise its house-keeping info. At the same time, we read
734 * each 'next' pointer in our rx_ring array. This reduces slow
735 * shared-memory reads and makes it easy to access predecessor
736 * descriptors.
737 *
738 * We then set the end-of-list bit for the last entry so that the
739 * card will know when it has run out of buffers.
740 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742static int mc32_load_rx_ring(struct net_device *dev)
743{
744 struct mc32_local *lp = netdev_priv(dev);
745 int i;
746 u16 rx_base;
747 volatile struct skb_header *p;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 rx_base=lp->rx_chain;
750
751 for(i=0; i<RX_RING_LEN; i++) {
752 lp->rx_ring[i].skb=alloc_skb(1532, GFP_KERNEL);
753 if (lp->rx_ring[i].skb==NULL) {
754 for (;i>=0;i--)
755 kfree_skb(lp->rx_ring[i].skb);
756 return -ENOBUFS;
757 }
758 skb_reserve(lp->rx_ring[i].skb, 18);
759
760 p=isa_bus_to_virt(lp->base+rx_base);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 p->control=0;
763 p->data=isa_virt_to_bus(lp->rx_ring[i].skb->data);
764 p->status=0;
765 p->length=1532;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400766
767 lp->rx_ring[i].p=p;
768 rx_base=p->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770
771 lp->rx_ring[i-1].p->control |= CONTROL_EOL;
772
773 lp->rx_ring_tail=0;
774
775 return 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400776}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778
779/**
780 * mc32_flush_rx_ring - free the ring of receive buffers
781 * @lp: Local data of 3c527 to flush the rx ring of
782 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400783 * Free the buffer for each ring slot. This may be called
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 * before mc32_load_rx_ring(), eg. on error in mc32_open().
785 * Requires rx skb pointers to point to a valid skb, or NULL.
786 */
787
788static void mc32_flush_rx_ring(struct net_device *dev)
789{
790 struct mc32_local *lp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400791 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400793 for(i=0; i < RX_RING_LEN; i++)
794 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (lp->rx_ring[i].skb) {
796 dev_kfree_skb(lp->rx_ring[i].skb);
797 lp->rx_ring[i].skb = NULL;
798 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400799 lp->rx_ring[i].p=NULL;
800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
803
804/**
805 * mc32_load_tx_ring - load transmit ring
806 * @dev: The 3c527 card to issue the command to
807 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400808 * This sets up the host transmit data-structures.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 *
810 * First, we obtain from the card it's current postion in the tx
811 * ring, so that we will know where to begin transmitting
812 * packets.
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400813 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 * Then, we read the 'next' pointers from the on-card tx ring into
815 * our tx_ring array to reduce slow shared-mem reads. Finally, we
816 * intitalise the tx house keeping variables.
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400817 *
818 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820static void mc32_load_tx_ring(struct net_device *dev)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400821{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 struct mc32_local *lp = netdev_priv(dev);
823 volatile struct skb_header *p;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400824 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 u16 tx_base;
826
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400827 tx_base=lp->tx_box->data[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 for(i=0 ; i<TX_RING_LEN ; i++)
830 {
831 p=isa_bus_to_virt(lp->base+tx_base);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400832 lp->tx_ring[i].p=p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 lp->tx_ring[i].skb=NULL;
834
835 tx_base=p->next;
836 }
837
838 /* -1 so that tx_ring_head cannot "lap" tx_ring_tail */
839 /* see mc32_tx_ring */
840
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400841 atomic_set(&lp->tx_count, TX_RING_LEN-1);
842 atomic_set(&lp->tx_ring_head, 0);
843 lp->tx_ring_tail=0;
844}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846
847/**
848 * mc32_flush_tx_ring - free transmit ring
849 * @lp: Local data of 3c527 to flush the tx ring of
850 *
851 * If the ring is non-empty, zip over the it, freeing any
852 * allocated skb_buffs. The tx ring house-keeping variables are
853 * then reset. Requires rx skb pointers to point to a valid skb,
854 * or NULL.
855 */
856
857static void mc32_flush_tx_ring(struct net_device *dev)
858{
859 struct mc32_local *lp = netdev_priv(dev);
860 int i;
861
862 for (i=0; i < TX_RING_LEN; i++)
863 {
864 if (lp->tx_ring[i].skb)
865 {
866 dev_kfree_skb(lp->tx_ring[i].skb);
867 lp->tx_ring[i].skb = NULL;
868 }
869 }
870
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400871 atomic_set(&lp->tx_count, 0);
872 atomic_set(&lp->tx_ring_head, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 lp->tx_ring_tail=0;
874}
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877/**
878 * mc32_open - handle 'up' of card
879 * @dev: device to open
880 *
881 * The user is trying to bring the card into ready state. This requires
882 * a brief dialogue with the card. Firstly we enable interrupts and then
883 * 'indications'. Without these enabled the card doesn't bother telling
884 * us what it has done. This had me puzzled for a week.
885 *
886 * We configure the number of card descriptors, then load the network
887 * address and multicast filters. Turn on the workaround mode. This
888 * works around a bug in the 82586 - it asks the firmware to do
889 * so. It has a performance (latency) hit but is needed on busy
890 * [read most] lans. We load the ring with buffers then we kick it
891 * all off.
892 */
893
894static int mc32_open(struct net_device *dev)
895{
896 int ioaddr = dev->base_addr;
897 struct mc32_local *lp = netdev_priv(dev);
898 u8 one=1;
899 u8 regs;
900 u16 descnumbuffs[2] = {TX_RING_LEN, RX_RING_LEN};
901
902 /*
903 * Interrupts enabled
904 */
905
906 regs=inb(ioaddr+HOST_CTRL);
907 regs|=HOST_CTRL_INTE;
908 outb(regs, ioaddr+HOST_CTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 /*
911 * Allow ourselves to issue commands
912 */
913
914 up(&lp->cmd_mutex);
915
916
917 /*
918 * Send the indications on command
919 */
920
921 mc32_command(dev, 4, &one, 2);
922
923 /*
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400924 * Poke it to make sure it's really dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 */
926
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400927 mc32_halt_transceiver(dev);
928 mc32_flush_tx_ring(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400930 /*
931 * Ask card to set up on-card descriptors to our spec
932 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400934 if(mc32_command(dev, 8, descnumbuffs, 4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 printk("%s: %s rejected our buffer configuration!\n",
936 dev->name, cardname);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400937 mc32_close(dev);
938 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400940
941 /* Report new configuration */
942 mc32_command(dev, 6, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 lp->tx_chain = lp->exec_box->data[8]; /* Transmit list start offset */
945 lp->rx_chain = lp->exec_box->data[10]; /* Receive list start offset */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400946 lp->tx_len = lp->exec_box->data[9]; /* Transmit list count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 lp->rx_len = lp->exec_box->data[11]; /* Receive list count */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 /* Set Network Address */
950 mc32_command(dev, 1, dev->dev_addr, 6);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 /* Set the filters */
953 mc32_set_multicast_list(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400954
955 if (WORKAROUND_82586) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 u16 zero_word=0;
957 mc32_command(dev, 0x0D, &zero_word, 2); /* 82586 bug workaround on */
958 }
959
960 mc32_load_tx_ring(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400961
962 if(mc32_load_rx_ring(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 {
964 mc32_close(dev);
965 return -ENOBUFS;
966 }
967
968 lp->xceiver_desired_state = RUNNING;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* And finally, set the ball rolling... */
971 mc32_start_transceiver(dev);
972
973 netif_start_queue(dev);
974
975 return 0;
976}
977
978
979/**
980 * mc32_timeout - handle a timeout from the network layer
981 * @dev: 3c527 that timed out
982 *
983 * Handle a timeout on transmit from the 3c527. This normally means
984 * bad things as the hardware handles cable timeouts and mess for
985 * us.
986 *
987 */
988
989static void mc32_timeout(struct net_device *dev)
990{
991 printk(KERN_WARNING "%s: transmit timed out?\n", dev->name);
992 /* Try to restart the adaptor. */
993 netif_wake_queue(dev);
994}
995
996
997/**
998 * mc32_send_packet - queue a frame for transmit
999 * @skb: buffer to transmit
1000 * @dev: 3c527 to send it out of
1001 *
1002 * Transmit a buffer. This normally means throwing the buffer onto
1003 * the transmit queue as the queue is quite large. If the queue is
1004 * full then we set tx_busy and return. Once the interrupt handler
1005 * gets messages telling it to reclaim transmit queue entries, we will
1006 * clear tx_busy and the kernel will start calling this again.
1007 *
1008 * We do not disable interrupts or acquire any locks; this can
1009 * run concurrently with mc32_tx_ring(), and the function itself
1010 * is serialised at a higher layer. However, similarly for the
1011 * card itself, we must ensure that we update tx_ring_head only
1012 * after we've established a valid packet on the tx ring (and
1013 * before we let the card "see" it, to prevent it racing with the
1014 * irq handler).
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 */
1017
1018static int mc32_send_packet(struct sk_buff *skb, struct net_device *dev)
1019{
1020 struct mc32_local *lp = netdev_priv(dev);
1021 u32 head = atomic_read(&lp->tx_ring_head);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 volatile struct skb_header *p, *np;
1024
1025 netif_stop_queue(dev);
1026
1027 if(atomic_read(&lp->tx_count)==0) {
1028 return 1;
1029 }
1030
Herbert Xu5b057c62006-06-23 02:06:41 -07001031 if (skb_padto(skb, ETH_ZLEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 netif_wake_queue(dev);
1033 return 0;
1034 }
1035
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001036 atomic_dec(&lp->tx_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 /* P is the last sending/sent buffer as a pointer */
1039 p=lp->tx_ring[head].p;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 head = next_tx(head);
1042
1043 /* NP is the buffer we will be loading */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001044 np=lp->tx_ring[head].p;
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 /* We will need this to flush the buffer out */
1047 lp->tx_ring[head].skb=skb;
1048
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001049 np->length = unlikely(skb->len < ETH_ZLEN) ? ETH_ZLEN : skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 np->data = isa_virt_to_bus(skb->data);
1051 np->status = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001052 np->control = CONTROL_EOP | CONTROL_EOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 wmb();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /*
1056 * The new frame has been setup; we can now
1057 * let the interrupt handler and card "see" it
1058 */
1059
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001060 atomic_set(&lp->tx_ring_head, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 p->control &= ~CONTROL_EOL;
1062
1063 netif_wake_queue(dev);
1064 return 0;
1065}
1066
1067
1068/**
1069 * mc32_update_stats - pull off the on board statistics
1070 * @dev: 3c527 to service
1071 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001072 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 * Query and reset the on-card stats. There's the small possibility
1074 * of a race here, which would result in an underestimation of
1075 * actual errors. As such, we'd prefer to keep all our stats
1076 * collection in software. As a rule, we do. However it can't be
1077 * used for rx errors and collisions as, by default, the card discards
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001078 * bad rx packets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 *
1080 * Setting the SAV BP in the rx filter command supposedly
1081 * stops this behaviour. However, testing shows that it only seems to
1082 * enable the collation of on-card rx statistics --- the driver
1083 * never sees an RX descriptor with an error status set.
1084 *
1085 */
1086
1087static void mc32_update_stats(struct net_device *dev)
1088{
1089 struct mc32_local *lp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001090 volatile struct mc32_stats *st = lp->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001092 u32 rx_errors=0;
1093
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001094 rx_errors+=dev->stats.rx_crc_errors +=st->rx_crc_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 st->rx_crc_errors=0;
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001096 rx_errors+=dev->stats.rx_fifo_errors +=st->rx_overrun_errors;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001097 st->rx_overrun_errors=0;
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001098 rx_errors+=dev->stats.rx_frame_errors +=st->rx_alignment_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 st->rx_alignment_errors=0;
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001100 rx_errors+=dev->stats.rx_length_errors+=st->rx_tooshort_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 st->rx_tooshort_errors=0;
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001102 rx_errors+=dev->stats.rx_missed_errors+=st->rx_outofresource_errors;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001103 st->rx_outofresource_errors=0;
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001104 dev->stats.rx_errors=rx_errors;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 /* Number of packets which saw one collision */
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001107 dev->stats.collisions+=st->dataC[10];
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001108 st->dataC[10]=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001110 /* Number of packets which saw 2--15 collisions */
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001111 dev->stats.collisions+=st->dataC[11];
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001112 st->dataC[11]=0;
1113}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115
1116/**
1117 * mc32_rx_ring - process the receive ring
1118 * @dev: 3c527 that needs its receive ring processing
1119 *
1120 *
1121 * We have received one or more indications from the card that a
1122 * receive has completed. The buffer ring thus contains dirty
1123 * entries. We walk the ring by iterating over the circular rx_ring
1124 * array, starting at the next dirty buffer (which happens to be the
1125 * one we finished up at last time around).
1126 *
1127 * For each completed packet, we will either copy it and pass it up
1128 * the stack or, if the packet is near MTU sized, we allocate
1129 * another buffer and flip the old one up the stack.
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001130 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 * We must succeed in keeping a buffer on the ring. If necessary we
1132 * will toss a received packet rather than lose a ring entry. Once
1133 * the first uncompleted descriptor is found, we move the
1134 * End-Of-List bit to include the buffers just processed.
1135 *
1136 */
1137
1138static void mc32_rx_ring(struct net_device *dev)
1139{
1140 struct mc32_local *lp = netdev_priv(dev);
1141 volatile struct skb_header *p;
1142 u16 rx_ring_tail;
1143 u16 rx_old_tail;
1144 int x=0;
1145
1146 rx_old_tail = rx_ring_tail = lp->rx_ring_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001148 do
1149 {
1150 p=lp->rx_ring[rx_ring_tail].p;
1151
1152 if(!(p->status & (1<<7))) { /* Not COMPLETED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if(p->status & (1<<6)) /* COMPLETED_OK */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001156 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 u16 length=p->length;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001159 struct sk_buff *skb;
1160 struct sk_buff *newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 /* Try to save time by avoiding a copy on big frames */
1163
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001164 if ((length > RX_COPYBREAK)
1165 && ((newskb=dev_alloc_skb(1532)) != NULL))
1166 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 skb=lp->rx_ring[rx_ring_tail].skb;
1168 skb_put(skb, length);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001169
1170 skb_reserve(newskb,18);
1171 lp->rx_ring[rx_ring_tail].skb=newskb;
1172 p->data=isa_virt_to_bus(newskb->data);
1173 }
1174 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001176 skb=dev_alloc_skb(length+2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 if(skb==NULL) {
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001179 dev->stats.rx_dropped++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001180 goto dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182
1183 skb_reserve(skb,2);
1184 memcpy(skb_put(skb, length),
1185 lp->rx_ring[rx_ring_tail].skb->data, length);
1186 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001187
1188 skb->protocol=eth_type_trans(skb,dev);
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001189 dev->stats.rx_packets++;
1190 dev->stats.rx_bytes += length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 netif_rx(skb);
1192 }
1193
1194 dropped:
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001195 p->length = 1532;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 p->status = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001197
1198 rx_ring_tail=next_rx(rx_ring_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001200 while(x++<48);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001202 /* If there was actually a frame to be processed, place the EOL bit */
1203 /* at the descriptor prior to the one to be filled next */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001205 if (rx_ring_tail != rx_old_tail)
1206 {
1207 lp->rx_ring[prev_rx(rx_ring_tail)].p->control |= CONTROL_EOL;
1208 lp->rx_ring[prev_rx(rx_old_tail)].p->control &= ~CONTROL_EOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001210 lp->rx_ring_tail=rx_ring_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212}
1213
1214
1215/**
1216 * mc32_tx_ring - process completed transmits
1217 * @dev: 3c527 that needs its transmit ring processing
1218 *
1219 *
1220 * This operates in a similar fashion to mc32_rx_ring. We iterate
1221 * over the transmit ring. For each descriptor which has been
1222 * processed by the card, we free its associated buffer and note
1223 * any errors. This continues until the transmit ring is emptied
1224 * or we reach a descriptor that hasn't yet been processed by the
1225 * card.
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001226 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 */
1228
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001229static void mc32_tx_ring(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 struct mc32_local *lp = netdev_priv(dev);
1232 volatile struct skb_header *np;
1233
1234 /*
1235 * We rely on head==tail to mean 'queue empty'.
1236 * This is why lp->tx_count=TX_RING_LEN-1: in order to prevent
1237 * tx_ring_head wrapping to tail and confusing a 'queue empty'
1238 * condition with 'queue full'
1239 */
1240
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001241 while (lp->tx_ring_tail != atomic_read(&lp->tx_ring_head))
1242 {
1243 u16 t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001245 t=next_tx(lp->tx_ring_tail);
1246 np=lp->tx_ring[t].p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001248 if(!(np->status & (1<<7)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001250 /* Not COMPLETED */
1251 break;
1252 }
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001253 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if(!(np->status & (1<<6))) /* Not COMPLETED_OK */
1255 {
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001256 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 switch(np->status&0x0F)
1259 {
1260 case 1:
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001261 dev->stats.tx_aborted_errors++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001262 break; /* Max collisions */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 case 2:
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001264 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 break;
1266 case 3:
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001267 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 break;
1269 case 4:
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001270 dev->stats.tx_window_errors++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001271 break; /* CTS Lost */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 case 5:
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001273 dev->stats.tx_aborted_errors++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001274 break; /* Transmit timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
1276 }
1277 /* Packets are sent in order - this is
1278 basically a FIFO queue of buffers matching
1279 the card ring */
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001280 dev->stats.tx_bytes+=lp->tx_ring[t].skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 dev_kfree_skb_irq(lp->tx_ring[t].skb);
1282 lp->tx_ring[t].skb=NULL;
1283 atomic_inc(&lp->tx_count);
1284 netif_wake_queue(dev);
1285
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001286 lp->tx_ring_tail=t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
1288
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001289}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291
1292/**
1293 * mc32_interrupt - handle an interrupt from a 3c527
1294 * @irq: Interrupt number
1295 * @dev_id: 3c527 that requires servicing
1296 * @regs: Registers (unused)
1297 *
1298 *
1299 * An interrupt is raised whenever the 3c527 writes to the command
1300 * register. This register contains the message it wishes to send us
1301 * packed into a single byte field. We keep reading status entries
1302 * until we have processed all the control items, but simply count
1303 * transmit and receive reports. When all reports are in we empty the
1304 * transceiver rings as appropriate. This saves the overhead of
1305 * multiple command requests.
1306 *
1307 * Because MCA is level-triggered, we shouldn't miss indications.
1308 * Therefore, we needn't ask the card to suspend interrupts within
1309 * this handler. The card receives an implicit acknowledgment of the
1310 * current interrupt when we read the command register.
1311 *
1312 */
1313
David Howells7d12e782006-10-05 14:55:46 +01001314static irqreturn_t mc32_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
1316 struct net_device *dev = dev_id;
1317 struct mc32_local *lp;
1318 int ioaddr, status, boguscount = 0;
1319 int rx_event = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001320 int tx_event = 0;
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 ioaddr = dev->base_addr;
1323 lp = netdev_priv(dev);
1324
1325 /* See whats cooking */
1326
1327 while((inb(ioaddr+HOST_STATUS)&HOST_STATUS_CWR) && boguscount++<2000)
1328 {
1329 status=inb(ioaddr+HOST_CMD);
1330
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001331#ifdef DEBUG_IRQ
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 printk("Status TX%d RX%d EX%d OV%d BC%d\n",
1333 (status&7), (status>>3)&7, (status>>6)&1,
1334 (status>>7)&1, boguscount);
1335#endif
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 switch(status&7)
1338 {
1339 case 0:
1340 break;
1341 case 6: /* TX fail */
1342 case 2: /* TX ok */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001343 tx_event = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 break;
1345 case 3: /* Halt */
1346 case 4: /* Abort */
1347 complete(&lp->xceiver_cmd);
1348 break;
1349 default:
1350 printk("%s: strange tx ack %d\n", dev->name, status&7);
1351 }
1352 status>>=3;
1353 switch(status&7)
1354 {
1355 case 0:
1356 break;
1357 case 2: /* RX */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001358 rx_event=1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 break;
1360 case 3: /* Halt */
1361 case 4: /* Abort */
1362 complete(&lp->xceiver_cmd);
1363 break;
1364 case 6:
1365 /* Out of RX buffers stat */
1366 /* Must restart rx */
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001367 dev->stats.rx_dropped++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001368 mc32_rx_ring(dev);
1369 mc32_start_transceiver(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 break;
1371 default:
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001372 printk("%s: strange rx ack %d\n",
1373 dev->name, status&7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
1375 status>>=3;
1376 if(status&1)
1377 {
1378 /*
1379 * No thread is waiting: we need to tidy
1380 * up ourself.
1381 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (lp->cmd_nonblocking) {
1384 up(&lp->cmd_mutex);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001385 if (lp->mc_reload_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 mc32_reset_multicast_list(dev);
1387 }
1388 else complete(&lp->execution_cmd);
1389 }
1390 if(status&2)
1391 {
1392 /*
1393 * We get interrupted once per
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001394 * counter that is about to overflow.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 */
1396
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001397 mc32_update_stats(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399 }
1400
1401
1402 /*
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001403 * Process the transmit and receive rings
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 */
1405
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001406 if(tx_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 mc32_tx_ring(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001408
1409 if(rx_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 mc32_rx_ring(dev);
1411
1412 return IRQ_HANDLED;
1413}
1414
1415
1416/**
1417 * mc32_close - user configuring the 3c527 down
1418 * @dev: 3c527 card to shut down
1419 *
1420 * The 3c527 is a bus mastering device. We must be careful how we
1421 * shut it down. It may also be running shared interrupt so we have
1422 * to be sure to silence it properly
1423 *
1424 * We indicate that the card is closing to the rest of the
1425 * driver. Otherwise, it is possible that the card may run out
1426 * of receive buffers and restart the transceiver while we're
1427 * trying to close it.
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001428 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 * We abort any receive and transmits going on and then wait until
1430 * any pending exec commands have completed in other code threads.
1431 * In theory we can't get here while that is true, in practice I am
1432 * paranoid
1433 *
1434 * We turn off the interrupt enable for the board to be sure it can't
1435 * intefere with other devices.
1436 */
1437
1438static int mc32_close(struct net_device *dev)
1439{
1440 struct mc32_local *lp = netdev_priv(dev);
1441 int ioaddr = dev->base_addr;
1442
1443 u8 regs;
1444 u16 one=1;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 lp->xceiver_desired_state = HALTED;
1447 netif_stop_queue(dev);
1448
1449 /*
1450 * Send the indications on command (handy debug check)
1451 */
1452
1453 mc32_command(dev, 4, &one, 2);
1454
1455 /* Shut down the transceiver */
1456
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001457 mc32_halt_transceiver(dev);
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 /* Ensure we issue no more commands beyond this point */
1460
1461 down(&lp->cmd_mutex);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001462
1463 /* Ok the card is now stopping */
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 regs=inb(ioaddr+HOST_CTRL);
1466 regs&=~HOST_CTRL_INTE;
1467 outb(regs, ioaddr+HOST_CTRL);
1468
1469 mc32_flush_rx_ring(dev);
1470 mc32_flush_tx_ring(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001471
1472 mc32_update_stats(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 return 0;
1475}
1476
1477
1478/**
1479 * mc32_get_stats - hand back stats to network layer
1480 * @dev: The 3c527 card to handle
1481 *
1482 * We've collected all the stats we can in software already. Now
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001483 * it's time to update those kept on-card and return the lot.
1484 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 */
1486
1487static struct net_device_stats *mc32_get_stats(struct net_device *dev)
1488{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001489 mc32_update_stats(dev);
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001490 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
1493
1494/**
1495 * do_mc32_set_multicast_list - attempt to update multicasts
1496 * @dev: 3c527 device to load the list on
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001497 * @retry: indicates this is not the first call.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 *
1499 *
1500 * Actually set or clear the multicast filter for this adaptor. The
1501 * locking issues are handled by this routine. We have to track
1502 * state as it may take multiple calls to get the command sequence
1503 * completed. We just keep trying to schedule the loads until we
1504 * manage to process them all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001506 * num_addrs == -1 Promiscuous mode, receive all packets
1507 *
1508 * num_addrs == 0 Normal mode, clear multicast list
1509 *
1510 * num_addrs > 0 Multicast mode, receive normal and MC packets,
1511 * and do best-effort filtering.
1512 *
1513 * See mc32_update_stats() regards setting the SAV BP bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 *
1515 */
1516
1517static void do_mc32_set_multicast_list(struct net_device *dev, int retry)
1518{
1519 struct mc32_local *lp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001520 u16 filt = (1<<2); /* Save Bad Packets, for stats purposes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Wang Chenc16d1182008-07-22 13:13:12 +08001522 if ((dev->flags&IFF_PROMISC) ||
1523 (dev->flags&IFF_ALLMULTI) ||
1524 dev->mc_count > 10)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 /* Enable promiscuous mode */
1526 filt |= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 else if(dev->mc_count)
1528 {
1529 unsigned char block[62];
1530 unsigned char *bp;
1531 struct dev_mc_list *dmc=dev->mc_list;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if(retry==0)
1536 lp->mc_list_valid = 0;
1537 if(!lp->mc_list_valid)
1538 {
1539 block[1]=0;
1540 block[0]=dev->mc_count;
1541 bp=block+2;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 for(i=0;i<dev->mc_count;i++)
1544 {
1545 memcpy(bp, dmc->dmi_addr, 6);
1546 bp+=6;
1547 dmc=dmc->next;
1548 }
1549 if(mc32_command_nowait(dev, 2, block, 2+6*dev->mc_count)==-1)
1550 {
1551 lp->mc_reload_wait = 1;
1552 return;
1553 }
1554 lp->mc_list_valid=1;
1555 }
1556 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001557
1558 if(mc32_command_nowait(dev, 0, &filt, 2)==-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 {
1560 lp->mc_reload_wait = 1;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001561 }
1562 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 lp->mc_reload_wait = 0;
1564 }
1565}
1566
1567
1568/**
1569 * mc32_set_multicast_list - queue multicast list update
1570 * @dev: The 3c527 to use
1571 *
1572 * Commence loading the multicast list. This is called when the kernel
1573 * changes the lists. It will override any pending list we are trying to
1574 * load.
1575 */
1576
1577static void mc32_set_multicast_list(struct net_device *dev)
1578{
1579 do_mc32_set_multicast_list(dev,0);
1580}
1581
1582
1583/**
1584 * mc32_reset_multicast_list - reset multicast list
1585 * @dev: The 3c527 to use
1586 *
1587 * Attempt the next step in loading the multicast lists. If this attempt
1588 * fails to complete then it will be scheduled and this function called
1589 * again later from elsewhere.
1590 */
1591
1592static void mc32_reset_multicast_list(struct net_device *dev)
1593{
1594 do_mc32_set_multicast_list(dev,1);
1595}
1596
1597static void netdev_get_drvinfo(struct net_device *dev,
1598 struct ethtool_drvinfo *info)
1599{
1600 strcpy(info->driver, DRV_NAME);
1601 strcpy(info->version, DRV_VERSION);
1602 sprintf(info->bus_info, "MCA 0x%lx", dev->base_addr);
1603}
1604
1605static u32 netdev_get_msglevel(struct net_device *dev)
1606{
1607 return mc32_debug;
1608}
1609
1610static void netdev_set_msglevel(struct net_device *dev, u32 level)
1611{
1612 mc32_debug = level;
1613}
1614
Jeff Garzik7282d492006-09-13 14:30:00 -04001615static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 .get_drvinfo = netdev_get_drvinfo,
1617 .get_msglevel = netdev_get_msglevel,
1618 .set_msglevel = netdev_set_msglevel,
1619};
1620
1621#ifdef MODULE
1622
1623static struct net_device *this_device;
1624
1625/**
1626 * init_module - entry point
1627 *
1628 * Probe and locate a 3c527 card. This really should probe and locate
1629 * all the 3c527 cards in the machine not just one of them. Yes you can
1630 * insmod multiple modules for now but it's a hack.
1631 */
1632
Randy Dunlap96e672c2006-06-10 13:33:48 -07001633int __init init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
1635 this_device = mc32_probe(-1);
1636 if (IS_ERR(this_device))
1637 return PTR_ERR(this_device);
1638 return 0;
1639}
1640
1641/**
1642 * cleanup_module - free resources for an unload
1643 *
1644 * Unloading time. We release the MCA bus resources and the interrupt
1645 * at which point everything is ready to unload. The card must be stopped
1646 * at this point or we would not have been called. When we unload we
1647 * leave the card stopped but not totally shut down. When the card is
1648 * initialized it must be rebooted or the rings reloaded before any
1649 * transmit operations are allowed to start scribbling into memory.
1650 */
1651
Al Viroafc8eb42006-06-14 18:50:53 -04001652void __exit cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
1654 unregister_netdev(this_device);
1655 cleanup_card(this_device);
1656 free_netdev(this_device);
1657}
1658
1659#endif /* MODULE */