1 /* 3c527.c: 3Com Etherlink/MC32 driver for Linux 2.4 and 2.6.
3 * (c) Copyright 1998 Red Hat Software Inc
5 * 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>
9 * Based on skeleton.c written 1993-94 by Donald Becker and ne2.c
10 * (for the MCA stuff) written by Wim Dumon.
12 * Thanks to 3Com for making this possible by providing me with the
15 * This software may be used and distributed according to the terms
16 * of the GNU General Public License, incorporated herein by reference.
20 #define DRV_NAME "3c527"
21 #define DRV_VERSION "0.7-SMP"
22 #define DRV_RELDATE "2003/09/21"
24 static const char *version =
25 DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " Richard Procter <rnp@paradise.net.nz>\n";
28 * DOC: Traps for the unwary
30 * The diagram (Figure 1-1) and the POS summary disagree with the
31 * "Interrupt Level" section in the manual.
33 * 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.
37 * Setting the SAV BP bit does not save bad packets, but
38 * only enables RX on-card stats collection.
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.
44 * DOC: Theory Of Operation
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.
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.
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.
68 * 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
73 * implementation was made necessary --- see mc32_update_stats().
77 * It should be possible to use two or more cards, but at this stage
78 * only by loading two copies of the same module.
80 * The on-board 82586 NIC has trouble receiving multiple
81 * back-to-back frames and so is likely to drop packets from fast
85 #include <linux/module.h>
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>
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 #include <linux/semaphore.h>
108 #include <asm/uaccess.h>
109 #include <asm/system.h>
115 MODULE_LICENSE("GPL");
118 * The name of the card. Is used for messages and in the requests for
119 * io regions, irqs and dma channels
121 static const char* cardname = DRV_NAME;
123 /* use 0 for production, 1 for verification, >2 for debug */
128 static unsigned int mc32_debug = NET_DEBUG;
130 /* The number of low I/O ports used by the ethercard. */
131 #define MC32_IO_EXTENT 8
133 /* As implemented, values must be a power-of-2 -- 4/8/16/32 */
134 #define TX_RING_LEN 32 /* Typically the card supports 37 */
135 #define RX_RING_LEN 8 /* " " " */
137 /* Copy break point, see above for details.
138 * Setting to > 1512 effectively disables this feature. */
139 #define RX_COPYBREAK 200 /* Value from 3c59x.c */
141 /* Issue the 82586 workaround command - this is for "busy lans", but
142 * basically means for all lans now days - has a performance (latency)
143 * cost, but best set. */
144 static const int WORKAROUND_82586=1;
146 /* Pointers to buffers and their on-card records */
147 struct mc32_ring_desc
149 volatile struct skb_header *p;
153 /* Information that needs to be kept for each board. */
159 volatile struct mc32_mailbox *rx_box;
160 volatile struct mc32_mailbox *tx_box;
161 volatile struct mc32_mailbox *exec_box;
162 volatile struct mc32_stats *stats; /* Start of on-card statistics */
163 u16 tx_chain; /* Transmit list start offset */
164 u16 rx_chain; /* Receive list start offset */
165 u16 tx_len; /* Transmit list count */
166 u16 rx_len; /* Receive list count */
168 u16 xceiver_desired_state; /* HALTED or RUNNING */
169 u16 cmd_nonblocking; /* Thread is uninterested in command result */
170 u16 mc_reload_wait; /* A multicast load request is pending */
171 u32 mc_list_valid; /* True when the mclist is set */
173 struct mc32_ring_desc tx_ring[TX_RING_LEN]; /* Host Transmit ring */
174 struct mc32_ring_desc rx_ring[RX_RING_LEN]; /* Host Receive ring */
176 atomic_t tx_count; /* buffers left */
177 atomic_t tx_ring_head; /* index to tx en-queue end */
178 u16 tx_ring_tail; /* index to tx de-queue end */
180 u16 rx_ring_tail; /* index to rx de-queue end */
182 struct semaphore cmd_mutex; /* Serialises issuing of execute commands */
183 struct completion execution_cmd; /* Card has completed an execute command */
184 struct completion xceiver_cmd; /* Card has completed a tx or rx command */
187 /* The station (ethernet) address prefix, used for a sanity check. */
188 #define SA_ADDR0 0x02
189 #define SA_ADDR1 0x60
190 #define SA_ADDR2 0xAC
192 struct mca_adapters_t {
197 static const struct mca_adapters_t mc32_adapters[] = {
198 { 0x0041, "3COM EtherLink MC/32" },
199 { 0x8EF5, "IBM High Performance Lan Adapter" },
204 /* Macros for ring index manipulations */
205 static inline u16 next_rx(u16 rx) { return (rx+1)&(RX_RING_LEN-1); };
206 static inline u16 prev_rx(u16 rx) { return (rx-1)&(RX_RING_LEN-1); };
208 static inline u16 next_tx(u16 tx) { return (tx+1)&(TX_RING_LEN-1); };
211 /* Index to functions, as function prototypes. */
212 static int mc32_probe1(struct net_device *dev, int ioaddr);
213 static int mc32_command(struct net_device *dev, u16 cmd, void *data, int len);
214 static int mc32_open(struct net_device *dev);
215 static void mc32_timeout(struct net_device *dev);
216 static int mc32_send_packet(struct sk_buff *skb, struct net_device *dev);
217 static irqreturn_t mc32_interrupt(int irq, void *dev_id);
218 static int mc32_close(struct net_device *dev);
219 static struct net_device_stats *mc32_get_stats(struct net_device *dev);
220 static void mc32_set_multicast_list(struct net_device *dev);
221 static void mc32_reset_multicast_list(struct net_device *dev);
222 static const struct ethtool_ops netdev_ethtool_ops;
224 static void cleanup_card(struct net_device *dev)
226 struct mc32_local *lp = netdev_priv(dev);
227 unsigned slot = lp->slot;
228 mca_mark_as_unused(slot);
229 mca_set_adapter_name(slot, NULL);
230 free_irq(dev->irq, dev);
231 release_region(dev->base_addr, MC32_IO_EXTENT);
235 * mc32_probe - Search for supported boards
236 * @unit: interface number to use
238 * Because MCA bus is a real bus and we can scan for cards we could do a
239 * single scan for all boards here. Right now we use the passed in device
240 * structure and scan for only one board. This needs fixing for modules
244 struct net_device *__init mc32_probe(int unit)
246 struct net_device *dev = alloc_etherdev(sizeof(struct mc32_local));
247 static int current_mca_slot = -1;
252 return ERR_PTR(-ENOMEM);
255 sprintf(dev->name, "eth%d", unit);
257 /* Do not check any supplied i/o locations.
258 POS registers usually don't fail :) */
260 /* MCA cards have POS registers.
261 Autodetecting MCA cards is extremely simple.
262 Just search for the card. */
264 for(i = 0; (mc32_adapters[i].name != NULL); i++) {
266 mca_find_unused_adapter(mc32_adapters[i].id, 0);
268 if(current_mca_slot != MCA_NOTFOUND) {
269 if(!mc32_probe1(dev, current_mca_slot))
271 mca_set_adapter_name(current_mca_slot,
272 mc32_adapters[i].name);
273 mca_mark_as_used(current_mca_slot);
274 err = register_netdev(dev);
286 return ERR_PTR(-ENODEV);
289 static const struct net_device_ops netdev_ops = {
290 .ndo_open = mc32_open,
291 .ndo_stop = mc32_close,
292 .ndo_start_xmit = mc32_send_packet,
293 .ndo_get_stats = mc32_get_stats,
294 .ndo_set_multicast_list = mc32_set_multicast_list,
295 .ndo_tx_timeout = mc32_timeout,
296 .ndo_change_mtu = eth_change_mtu,
297 .ndo_set_mac_address = eth_mac_addr,
298 .ndo_validate_addr = eth_validate_addr,
302 * mc32_probe1 - Check a given slot for a board and test the card
303 * @dev: Device structure to fill in
304 * @slot: The MCA bus slot being used by this card
306 * Decode the slot data and configure the card structures. Having done this we
307 * can reset the card and configure it. The card does a full self test cycle
308 * in firmware so we have to wait for it to return and post us either a
309 * failure case or some addresses we use to find the board internals.
312 static int __init mc32_probe1(struct net_device *dev, int slot)
314 static unsigned version_printed;
318 struct mc32_local *lp = netdev_priv(dev);
319 static u16 mca_io_bases[]={
325 static u32 mca_mem_bases[]={
335 static char *failures[]={
336 "Processor instruction",
337 "Processor data bus",
338 "Processor data bus",
339 "Processor data bus",
344 "82586 internal loopback",
345 "82586 initialisation failure",
346 "Adapter list configuration error"
349 /* Time to play MCA games */
351 if (mc32_debug && version_printed++ == 0)
352 pr_debug("%s", version);
354 pr_info("%s: %s found in slot %d: ", dev->name, cardname, slot);
356 POS = mca_read_stored_pos(slot, 2);
360 pr_cont("disabled.\n");
364 /* Fill in the 'dev' fields. */
365 dev->base_addr = mca_io_bases[(POS>>1)&7];
366 dev->mem_start = mca_mem_bases[(POS>>4)&7];
368 POS = mca_read_stored_pos(slot, 4);
371 pr_cont("memory window disabled.\n");
375 POS = mca_read_stored_pos(slot, 5);
380 pr_cont("invalid memory window.\n");
387 dev->mem_end=dev->mem_start + i;
389 dev->irq = ((POS>>2)&3)+9;
391 if(!request_region(dev->base_addr, MC32_IO_EXTENT, cardname))
393 pr_cont("io 0x%3lX, which is busy.\n", dev->base_addr);
397 pr_cont("io 0x%3lX irq %d mem 0x%lX (%dK)\n",
398 dev->base_addr, dev->irq, dev->mem_start, i/1024);
401 /* We ought to set the cache line size here.. */
408 /* Retrieve and print the ethernet address. */
409 for (i = 0; i < 6; i++)
411 mca_write_pos(slot, 6, i+12);
412 mca_write_pos(slot, 7, 0);
414 dev->dev_addr[i] = mca_read_pos(slot,3);
417 pr_info("%s: Address %pM ", dev->name, dev->dev_addr);
419 mca_write_pos(slot, 6, 0);
420 mca_write_pos(slot, 7, 0);
422 POS = mca_read_stored_pos(slot, 4);
425 pr_cont(": BNC port selected.\n");
427 pr_cont(": AUI port selected.\n");
429 POS=inb(dev->base_addr+HOST_CTRL);
430 POS|=HOST_CTRL_ATTN|HOST_CTRL_RESET;
431 POS&=~HOST_CTRL_INTE;
432 outb(POS, dev->base_addr+HOST_CTRL);
436 POS&=~(HOST_CTRL_ATTN|HOST_CTRL_RESET);
437 outb(POS, dev->base_addr+HOST_CTRL);
445 err = request_irq(dev->irq, &mc32_interrupt, IRQF_SHARED | IRQF_SAMPLE_RANDOM, DRV_NAME, dev);
447 release_region(dev->base_addr, MC32_IO_EXTENT);
448 pr_err("%s: unable to get IRQ %d.\n", DRV_NAME, dev->irq);
452 memset(lp, 0, sizeof(struct mc32_local));
457 base = inb(dev->base_addr);
464 pr_err("%s: failed to boot adapter.\n", dev->name);
469 if(inb(dev->base_addr+2)&(1<<5))
470 base = inb(dev->base_addr);
476 pr_err("%s: %s%s.\n", dev->name, failures[base-1],
477 base<0x0A?" test failure":"");
479 pr_err("%s: unknown failure %d.\n", dev->name, base);
489 while(!(inb(dev->base_addr+2)&(1<<5)))
495 pr_err("%s: mailbox read fail (%d).\n", dev->name, i);
501 base|=(inb(dev->base_addr)<<(8*i));
504 lp->exec_box=isa_bus_to_virt(dev->mem_start+base);
506 base=lp->exec_box->data[1]<<16|lp->exec_box->data[0];
508 lp->base = dev->mem_start+base;
510 lp->rx_box=isa_bus_to_virt(lp->base + lp->exec_box->data[2]);
511 lp->tx_box=isa_bus_to_virt(lp->base + lp->exec_box->data[3]);
513 lp->stats = isa_bus_to_virt(lp->base + lp->exec_box->data[5]);
516 * Descriptor chains (card relative)
519 lp->tx_chain = lp->exec_box->data[8]; /* Transmit list start offset */
520 lp->rx_chain = lp->exec_box->data[10]; /* Receive list start offset */
521 lp->tx_len = lp->exec_box->data[9]; /* Transmit list count */
522 lp->rx_len = lp->exec_box->data[11]; /* Receive list count */
524 init_MUTEX_LOCKED(&lp->cmd_mutex);
525 init_completion(&lp->execution_cmd);
526 init_completion(&lp->xceiver_cmd);
528 pr_info("%s: Firmware Rev %d. %d RX buffers, %d TX buffers. Base of 0x%08X.\n",
529 dev->name, lp->exec_box->data[12], lp->rx_len, lp->tx_len, lp->base);
531 dev->netdev_ops = &netdev_ops;
532 dev->watchdog_timeo = HZ*5; /* Board does all the work */
533 dev->ethtool_ops = &netdev_ethtool_ops;
538 free_irq(dev->irq, dev);
540 release_region(dev->base_addr, MC32_IO_EXTENT);
546 * mc32_ready_poll - wait until we can feed it a command
547 * @dev: The device to wait for
549 * Wait until the card becomes ready to accept a command via the
550 * command register. This tells us nothing about the completion
551 * status of any pending commands and takes very little time at all.
554 static inline void mc32_ready_poll(struct net_device *dev)
556 int ioaddr = dev->base_addr;
557 while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
562 * mc32_command_nowait - send a command non blocking
563 * @dev: The 3c527 to issue the command to
564 * @cmd: The command word to write to the mailbox
565 * @data: A data block if the command expects one
566 * @len: Length of the data block
568 * Send a command from interrupt state. If there is a command
569 * currently being executed then we return an error of -1. It
570 * simply isn't viable to wait around as commands may be
571 * slow. This can theoretically be starved on SMP, but it's hard
572 * to see a realistic situation. We do not wait for the command
573 * to complete --- we rely on the interrupt handler to tidy up
577 static int mc32_command_nowait(struct net_device *dev, u16 cmd, void *data, int len)
579 struct mc32_local *lp = netdev_priv(dev);
580 int ioaddr = dev->base_addr;
583 if (down_trylock(&lp->cmd_mutex) == 0)
585 lp->cmd_nonblocking=1;
586 lp->exec_box->mbox=0;
587 lp->exec_box->mbox=cmd;
588 memcpy((void *)lp->exec_box->data, data, len);
589 barrier(); /* the memcpy forgot the volatile so be sure */
591 /* Send the command */
592 mc32_ready_poll(dev);
593 outb(1<<6, ioaddr+HOST_CMD);
597 /* Interrupt handler will signal mutex on completion */
605 * mc32_command - send a command and sleep until completion
606 * @dev: The 3c527 card to issue the command to
607 * @cmd: The command word to write to the mailbox
608 * @data: A data block if the command expects one
609 * @len: Length of the data block
611 * Sends exec commands in a user context. This permits us to wait around
612 * for the replies and also to wait for the command buffer to complete
613 * from a previous command before we execute our command. After our
614 * command completes we will attempt any pending multicast reload
615 * we blocked off by hogging the exec buffer.
617 * You feed the card a command, you wait, it interrupts you get a
618 * reply. All well and good. The complication arises because you use
619 * commands for filter list changes which come in at bh level from things
620 * like IPV6 group stuff.
623 static int mc32_command(struct net_device *dev, u16 cmd, void *data, int len)
625 struct mc32_local *lp = netdev_priv(dev);
626 int ioaddr = dev->base_addr;
629 down(&lp->cmd_mutex);
635 lp->cmd_nonblocking=0;
636 lp->exec_box->mbox=0;
637 lp->exec_box->mbox=cmd;
638 memcpy((void *)lp->exec_box->data, data, len);
639 barrier(); /* the memcpy forgot the volatile so be sure */
641 mc32_ready_poll(dev);
642 outb(1<<6, ioaddr+HOST_CMD);
644 wait_for_completion(&lp->execution_cmd);
646 if(lp->exec_box->mbox&(1<<13))
652 * A multicast set got blocked - try it now
655 if(lp->mc_reload_wait)
657 mc32_reset_multicast_list(dev);
665 * mc32_start_transceiver - tell board to restart tx/rx
666 * @dev: The 3c527 card to issue the command to
668 * This may be called from the interrupt state, where it is used
669 * to restart the rx ring if the card runs out of rx buffers.
671 * We must first check if it's ok to (re)start the transceiver. See
672 * mc32_close for details.
675 static void mc32_start_transceiver(struct net_device *dev) {
677 struct mc32_local *lp = netdev_priv(dev);
678 int ioaddr = dev->base_addr;
680 /* Ignore RX overflow on device closure */
681 if (lp->xceiver_desired_state==HALTED)
684 /* Give the card the offset to the post-EOL-bit RX descriptor */
685 mc32_ready_poll(dev);
687 lp->rx_box->data[0]=lp->rx_ring[prev_rx(lp->rx_ring_tail)].p->next;
688 outb(HOST_CMD_START_RX, ioaddr+HOST_CMD);
690 mc32_ready_poll(dev);
692 outb(HOST_CMD_RESTRT_TX, ioaddr+HOST_CMD); /* card ignores this on RX restart */
694 /* We are not interrupted on start completion */
699 * mc32_halt_transceiver - tell board to stop tx/rx
700 * @dev: The 3c527 card to issue the command to
702 * We issue the commands to halt the card's transceiver. In fact,
703 * after some experimenting we now simply tell the card to
704 * suspend. When issuing aborts occasionally odd things happened.
706 * We then sleep until the card has notified us that both rx and
707 * tx have been suspended.
710 static void mc32_halt_transceiver(struct net_device *dev)
712 struct mc32_local *lp = netdev_priv(dev);
713 int ioaddr = dev->base_addr;
715 mc32_ready_poll(dev);
717 outb(HOST_CMD_SUSPND_RX, ioaddr+HOST_CMD);
718 wait_for_completion(&lp->xceiver_cmd);
720 mc32_ready_poll(dev);
722 outb(HOST_CMD_SUSPND_TX, ioaddr+HOST_CMD);
723 wait_for_completion(&lp->xceiver_cmd);
728 * mc32_load_rx_ring - load the ring of receive buffers
729 * @dev: 3c527 to build the ring for
731 * This initalises the on-card and driver datastructures to
732 * the point where mc32_start_transceiver() can be called.
734 * The card sets up the receive ring for us. We are required to use the
735 * ring it provides, although the size of the ring is configurable.
737 * We allocate an sk_buff for each ring entry in turn and
738 * initalise its house-keeping info. At the same time, we read
739 * each 'next' pointer in our rx_ring array. This reduces slow
740 * shared-memory reads and makes it easy to access predecessor
743 * We then set the end-of-list bit for the last entry so that the
744 * card will know when it has run out of buffers.
747 static int mc32_load_rx_ring(struct net_device *dev)
749 struct mc32_local *lp = netdev_priv(dev);
752 volatile struct skb_header *p;
754 rx_base=lp->rx_chain;
756 for(i=0; i<RX_RING_LEN; i++) {
757 lp->rx_ring[i].skb=alloc_skb(1532, GFP_KERNEL);
758 if (lp->rx_ring[i].skb==NULL) {
760 kfree_skb(lp->rx_ring[i].skb);
763 skb_reserve(lp->rx_ring[i].skb, 18);
765 p=isa_bus_to_virt(lp->base+rx_base);
768 p->data=isa_virt_to_bus(lp->rx_ring[i].skb->data);
776 lp->rx_ring[i-1].p->control |= CONTROL_EOL;
785 * mc32_flush_rx_ring - free the ring of receive buffers
786 * @lp: Local data of 3c527 to flush the rx ring of
788 * Free the buffer for each ring slot. This may be called
789 * before mc32_load_rx_ring(), eg. on error in mc32_open().
790 * Requires rx skb pointers to point to a valid skb, or NULL.
793 static void mc32_flush_rx_ring(struct net_device *dev)
795 struct mc32_local *lp = netdev_priv(dev);
798 for(i=0; i < RX_RING_LEN; i++)
800 if (lp->rx_ring[i].skb) {
801 dev_kfree_skb(lp->rx_ring[i].skb);
802 lp->rx_ring[i].skb = NULL;
804 lp->rx_ring[i].p=NULL;
810 * mc32_load_tx_ring - load transmit ring
811 * @dev: The 3c527 card to issue the command to
813 * This sets up the host transmit data-structures.
815 * First, we obtain from the card it's current postion in the tx
816 * ring, so that we will know where to begin transmitting
819 * Then, we read the 'next' pointers from the on-card tx ring into
820 * our tx_ring array to reduce slow shared-mem reads. Finally, we
821 * intitalise the tx house keeping variables.
825 static void mc32_load_tx_ring(struct net_device *dev)
827 struct mc32_local *lp = netdev_priv(dev);
828 volatile struct skb_header *p;
832 tx_base=lp->tx_box->data[0];
834 for(i=0 ; i<TX_RING_LEN ; i++)
836 p=isa_bus_to_virt(lp->base+tx_base);
838 lp->tx_ring[i].skb=NULL;
843 /* -1 so that tx_ring_head cannot "lap" tx_ring_tail */
844 /* see mc32_tx_ring */
846 atomic_set(&lp->tx_count, TX_RING_LEN-1);
847 atomic_set(&lp->tx_ring_head, 0);
853 * mc32_flush_tx_ring - free transmit ring
854 * @lp: Local data of 3c527 to flush the tx ring of
856 * If the ring is non-empty, zip over the it, freeing any
857 * allocated skb_buffs. The tx ring house-keeping variables are
858 * then reset. Requires rx skb pointers to point to a valid skb,
862 static void mc32_flush_tx_ring(struct net_device *dev)
864 struct mc32_local *lp = netdev_priv(dev);
867 for (i=0; i < TX_RING_LEN; i++)
869 if (lp->tx_ring[i].skb)
871 dev_kfree_skb(lp->tx_ring[i].skb);
872 lp->tx_ring[i].skb = NULL;
876 atomic_set(&lp->tx_count, 0);
877 atomic_set(&lp->tx_ring_head, 0);
883 * mc32_open - handle 'up' of card
884 * @dev: device to open
886 * The user is trying to bring the card into ready state. This requires
887 * a brief dialogue with the card. Firstly we enable interrupts and then
888 * 'indications'. Without these enabled the card doesn't bother telling
889 * us what it has done. This had me puzzled for a week.
891 * We configure the number of card descriptors, then load the network
892 * address and multicast filters. Turn on the workaround mode. This
893 * works around a bug in the 82586 - it asks the firmware to do
894 * so. It has a performance (latency) hit but is needed on busy
895 * [read most] lans. We load the ring with buffers then we kick it
899 static int mc32_open(struct net_device *dev)
901 int ioaddr = dev->base_addr;
902 struct mc32_local *lp = netdev_priv(dev);
905 u16 descnumbuffs[2] = {TX_RING_LEN, RX_RING_LEN};
911 regs=inb(ioaddr+HOST_CTRL);
912 regs|=HOST_CTRL_INTE;
913 outb(regs, ioaddr+HOST_CTRL);
916 * Allow ourselves to issue commands
923 * Send the indications on command
926 mc32_command(dev, 4, &one, 2);
929 * Poke it to make sure it's really dead.
932 mc32_halt_transceiver(dev);
933 mc32_flush_tx_ring(dev);
936 * Ask card to set up on-card descriptors to our spec
939 if(mc32_command(dev, 8, descnumbuffs, 4)) {
940 pr_info("%s: %s rejected our buffer configuration!\n",
941 dev->name, cardname);
946 /* Report new configuration */
947 mc32_command(dev, 6, NULL, 0);
949 lp->tx_chain = lp->exec_box->data[8]; /* Transmit list start offset */
950 lp->rx_chain = lp->exec_box->data[10]; /* Receive list start offset */
951 lp->tx_len = lp->exec_box->data[9]; /* Transmit list count */
952 lp->rx_len = lp->exec_box->data[11]; /* Receive list count */
954 /* Set Network Address */
955 mc32_command(dev, 1, dev->dev_addr, 6);
957 /* Set the filters */
958 mc32_set_multicast_list(dev);
960 if (WORKAROUND_82586) {
962 mc32_command(dev, 0x0D, &zero_word, 2); /* 82586 bug workaround on */
965 mc32_load_tx_ring(dev);
967 if(mc32_load_rx_ring(dev))
973 lp->xceiver_desired_state = RUNNING;
975 /* And finally, set the ball rolling... */
976 mc32_start_transceiver(dev);
978 netif_start_queue(dev);
985 * mc32_timeout - handle a timeout from the network layer
986 * @dev: 3c527 that timed out
988 * Handle a timeout on transmit from the 3c527. This normally means
989 * bad things as the hardware handles cable timeouts and mess for
994 static void mc32_timeout(struct net_device *dev)
996 pr_warning("%s: transmit timed out?\n", dev->name);
997 /* Try to restart the adaptor. */
998 netif_wake_queue(dev);
1003 * mc32_send_packet - queue a frame for transmit
1004 * @skb: buffer to transmit
1005 * @dev: 3c527 to send it out of
1007 * Transmit a buffer. This normally means throwing the buffer onto
1008 * the transmit queue as the queue is quite large. If the queue is
1009 * full then we set tx_busy and return. Once the interrupt handler
1010 * gets messages telling it to reclaim transmit queue entries, we will
1011 * clear tx_busy and the kernel will start calling this again.
1013 * We do not disable interrupts or acquire any locks; this can
1014 * run concurrently with mc32_tx_ring(), and the function itself
1015 * is serialised at a higher layer. However, similarly for the
1016 * card itself, we must ensure that we update tx_ring_head only
1017 * after we've established a valid packet on the tx ring (and
1018 * before we let the card "see" it, to prevent it racing with the
1023 static int mc32_send_packet(struct sk_buff *skb, struct net_device *dev)
1025 struct mc32_local *lp = netdev_priv(dev);
1026 u32 head = atomic_read(&lp->tx_ring_head);
1028 volatile struct skb_header *p, *np;
1030 netif_stop_queue(dev);
1032 if(atomic_read(&lp->tx_count)==0) {
1033 return NETDEV_TX_BUSY;
1036 if (skb_padto(skb, ETH_ZLEN)) {
1037 netif_wake_queue(dev);
1041 atomic_dec(&lp->tx_count);
1043 /* P is the last sending/sent buffer as a pointer */
1044 p=lp->tx_ring[head].p;
1046 head = next_tx(head);
1048 /* NP is the buffer we will be loading */
1049 np=lp->tx_ring[head].p;
1051 /* We will need this to flush the buffer out */
1052 lp->tx_ring[head].skb=skb;
1054 np->length = unlikely(skb->len < ETH_ZLEN) ? ETH_ZLEN : skb->len;
1055 np->data = isa_virt_to_bus(skb->data);
1057 np->control = CONTROL_EOP | CONTROL_EOL;
1061 * The new frame has been setup; we can now
1062 * let the interrupt handler and card "see" it
1065 atomic_set(&lp->tx_ring_head, head);
1066 p->control &= ~CONTROL_EOL;
1068 netif_wake_queue(dev);
1074 * mc32_update_stats - pull off the on board statistics
1075 * @dev: 3c527 to service
1078 * Query and reset the on-card stats. There's the small possibility
1079 * of a race here, which would result in an underestimation of
1080 * actual errors. As such, we'd prefer to keep all our stats
1081 * collection in software. As a rule, we do. However it can't be
1082 * used for rx errors and collisions as, by default, the card discards
1085 * Setting the SAV BP in the rx filter command supposedly
1086 * stops this behaviour. However, testing shows that it only seems to
1087 * enable the collation of on-card rx statistics --- the driver
1088 * never sees an RX descriptor with an error status set.
1092 static void mc32_update_stats(struct net_device *dev)
1094 struct mc32_local *lp = netdev_priv(dev);
1095 volatile struct mc32_stats *st = lp->stats;
1099 rx_errors+=dev->stats.rx_crc_errors +=st->rx_crc_errors;
1100 st->rx_crc_errors=0;
1101 rx_errors+=dev->stats.rx_fifo_errors +=st->rx_overrun_errors;
1102 st->rx_overrun_errors=0;
1103 rx_errors+=dev->stats.rx_frame_errors +=st->rx_alignment_errors;
1104 st->rx_alignment_errors=0;
1105 rx_errors+=dev->stats.rx_length_errors+=st->rx_tooshort_errors;
1106 st->rx_tooshort_errors=0;
1107 rx_errors+=dev->stats.rx_missed_errors+=st->rx_outofresource_errors;
1108 st->rx_outofresource_errors=0;
1109 dev->stats.rx_errors=rx_errors;
1111 /* Number of packets which saw one collision */
1112 dev->stats.collisions+=st->dataC[10];
1115 /* Number of packets which saw 2--15 collisions */
1116 dev->stats.collisions+=st->dataC[11];
1122 * mc32_rx_ring - process the receive ring
1123 * @dev: 3c527 that needs its receive ring processing
1126 * We have received one or more indications from the card that a
1127 * receive has completed. The buffer ring thus contains dirty
1128 * entries. We walk the ring by iterating over the circular rx_ring
1129 * array, starting at the next dirty buffer (which happens to be the
1130 * one we finished up at last time around).
1132 * For each completed packet, we will either copy it and pass it up
1133 * the stack or, if the packet is near MTU sized, we allocate
1134 * another buffer and flip the old one up the stack.
1136 * We must succeed in keeping a buffer on the ring. If necessary we
1137 * will toss a received packet rather than lose a ring entry. Once
1138 * the first uncompleted descriptor is found, we move the
1139 * End-Of-List bit to include the buffers just processed.
1143 static void mc32_rx_ring(struct net_device *dev)
1145 struct mc32_local *lp = netdev_priv(dev);
1146 volatile struct skb_header *p;
1151 rx_old_tail = rx_ring_tail = lp->rx_ring_tail;
1155 p=lp->rx_ring[rx_ring_tail].p;
1157 if(!(p->status & (1<<7))) { /* Not COMPLETED */
1160 if(p->status & (1<<6)) /* COMPLETED_OK */
1163 u16 length=p->length;
1164 struct sk_buff *skb;
1165 struct sk_buff *newskb;
1167 /* Try to save time by avoiding a copy on big frames */
1169 if ((length > RX_COPYBREAK)
1170 && ((newskb=dev_alloc_skb(1532)) != NULL))
1172 skb=lp->rx_ring[rx_ring_tail].skb;
1173 skb_put(skb, length);
1175 skb_reserve(newskb,18);
1176 lp->rx_ring[rx_ring_tail].skb=newskb;
1177 p->data=isa_virt_to_bus(newskb->data);
1181 skb=dev_alloc_skb(length+2);
1184 dev->stats.rx_dropped++;
1189 memcpy(skb_put(skb, length),
1190 lp->rx_ring[rx_ring_tail].skb->data, length);
1193 skb->protocol=eth_type_trans(skb,dev);
1194 dev->stats.rx_packets++;
1195 dev->stats.rx_bytes += length;
1203 rx_ring_tail=next_rx(rx_ring_tail);
1207 /* If there was actually a frame to be processed, place the EOL bit */
1208 /* at the descriptor prior to the one to be filled next */
1210 if (rx_ring_tail != rx_old_tail)
1212 lp->rx_ring[prev_rx(rx_ring_tail)].p->control |= CONTROL_EOL;
1213 lp->rx_ring[prev_rx(rx_old_tail)].p->control &= ~CONTROL_EOL;
1215 lp->rx_ring_tail=rx_ring_tail;
1221 * mc32_tx_ring - process completed transmits
1222 * @dev: 3c527 that needs its transmit ring processing
1225 * This operates in a similar fashion to mc32_rx_ring. We iterate
1226 * over the transmit ring. For each descriptor which has been
1227 * processed by the card, we free its associated buffer and note
1228 * any errors. This continues until the transmit ring is emptied
1229 * or we reach a descriptor that hasn't yet been processed by the
1234 static void mc32_tx_ring(struct net_device *dev)
1236 struct mc32_local *lp = netdev_priv(dev);
1237 volatile struct skb_header *np;
1240 * We rely on head==tail to mean 'queue empty'.
1241 * This is why lp->tx_count=TX_RING_LEN-1: in order to prevent
1242 * tx_ring_head wrapping to tail and confusing a 'queue empty'
1243 * condition with 'queue full'
1246 while (lp->tx_ring_tail != atomic_read(&lp->tx_ring_head))
1250 t=next_tx(lp->tx_ring_tail);
1251 np=lp->tx_ring[t].p;
1253 if(!(np->status & (1<<7)))
1258 dev->stats.tx_packets++;
1259 if(!(np->status & (1<<6))) /* Not COMPLETED_OK */
1261 dev->stats.tx_errors++;
1263 switch(np->status&0x0F)
1266 dev->stats.tx_aborted_errors++;
1267 break; /* Max collisions */
1269 dev->stats.tx_fifo_errors++;
1272 dev->stats.tx_carrier_errors++;
1275 dev->stats.tx_window_errors++;
1276 break; /* CTS Lost */
1278 dev->stats.tx_aborted_errors++;
1279 break; /* Transmit timeout */
1282 /* Packets are sent in order - this is
1283 basically a FIFO queue of buffers matching
1285 dev->stats.tx_bytes+=lp->tx_ring[t].skb->len;
1286 dev_kfree_skb_irq(lp->tx_ring[t].skb);
1287 lp->tx_ring[t].skb=NULL;
1288 atomic_inc(&lp->tx_count);
1289 netif_wake_queue(dev);
1298 * mc32_interrupt - handle an interrupt from a 3c527
1299 * @irq: Interrupt number
1300 * @dev_id: 3c527 that requires servicing
1301 * @regs: Registers (unused)
1304 * An interrupt is raised whenever the 3c527 writes to the command
1305 * register. This register contains the message it wishes to send us
1306 * packed into a single byte field. We keep reading status entries
1307 * until we have processed all the control items, but simply count
1308 * transmit and receive reports. When all reports are in we empty the
1309 * transceiver rings as appropriate. This saves the overhead of
1310 * multiple command requests.
1312 * Because MCA is level-triggered, we shouldn't miss indications.
1313 * Therefore, we needn't ask the card to suspend interrupts within
1314 * this handler. The card receives an implicit acknowledgment of the
1315 * current interrupt when we read the command register.
1319 static irqreturn_t mc32_interrupt(int irq, void *dev_id)
1321 struct net_device *dev = dev_id;
1322 struct mc32_local *lp;
1323 int ioaddr, status, boguscount = 0;
1327 ioaddr = dev->base_addr;
1328 lp = netdev_priv(dev);
1330 /* See whats cooking */
1332 while((inb(ioaddr+HOST_STATUS)&HOST_STATUS_CWR) && boguscount++<2000)
1334 status=inb(ioaddr+HOST_CMD);
1336 pr_debug("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);
1344 case 6: /* TX fail */
1350 complete(&lp->xceiver_cmd);
1353 pr_notice("%s: strange tx ack %d\n", dev->name, status&7);
1365 complete(&lp->xceiver_cmd);
1368 /* Out of RX buffers stat */
1369 /* Must restart rx */
1370 dev->stats.rx_dropped++;
1372 mc32_start_transceiver(dev);
1375 pr_notice("%s: strange rx ack %d\n",
1376 dev->name, status&7);
1382 * No thread is waiting: we need to tidy
1386 if (lp->cmd_nonblocking) {
1388 if (lp->mc_reload_wait)
1389 mc32_reset_multicast_list(dev);
1391 else complete(&lp->execution_cmd);
1396 * We get interrupted once per
1397 * counter that is about to overflow.
1400 mc32_update_stats(dev);
1406 * Process the transmit and receive rings
1420 * mc32_close - user configuring the 3c527 down
1421 * @dev: 3c527 card to shut down
1423 * The 3c527 is a bus mastering device. We must be careful how we
1424 * shut it down. It may also be running shared interrupt so we have
1425 * to be sure to silence it properly
1427 * We indicate that the card is closing to the rest of the
1428 * driver. Otherwise, it is possible that the card may run out
1429 * of receive buffers and restart the transceiver while we're
1430 * trying to close it.
1432 * We abort any receive and transmits going on and then wait until
1433 * any pending exec commands have completed in other code threads.
1434 * In theory we can't get here while that is true, in practice I am
1437 * We turn off the interrupt enable for the board to be sure it can't
1438 * intefere with other devices.
1441 static int mc32_close(struct net_device *dev)
1443 struct mc32_local *lp = netdev_priv(dev);
1444 int ioaddr = dev->base_addr;
1449 lp->xceiver_desired_state = HALTED;
1450 netif_stop_queue(dev);
1453 * Send the indications on command (handy debug check)
1456 mc32_command(dev, 4, &one, 2);
1458 /* Shut down the transceiver */
1460 mc32_halt_transceiver(dev);
1462 /* Ensure we issue no more commands beyond this point */
1464 down(&lp->cmd_mutex);
1466 /* Ok the card is now stopping */
1468 regs=inb(ioaddr+HOST_CTRL);
1469 regs&=~HOST_CTRL_INTE;
1470 outb(regs, ioaddr+HOST_CTRL);
1472 mc32_flush_rx_ring(dev);
1473 mc32_flush_tx_ring(dev);
1475 mc32_update_stats(dev);
1482 * mc32_get_stats - hand back stats to network layer
1483 * @dev: The 3c527 card to handle
1485 * We've collected all the stats we can in software already. Now
1486 * it's time to update those kept on-card and return the lot.
1490 static struct net_device_stats *mc32_get_stats(struct net_device *dev)
1492 mc32_update_stats(dev);
1498 * do_mc32_set_multicast_list - attempt to update multicasts
1499 * @dev: 3c527 device to load the list on
1500 * @retry: indicates this is not the first call.
1503 * Actually set or clear the multicast filter for this adaptor. The
1504 * locking issues are handled by this routine. We have to track
1505 * state as it may take multiple calls to get the command sequence
1506 * completed. We just keep trying to schedule the loads until we
1507 * manage to process them all.
1509 * num_addrs == -1 Promiscuous mode, receive all packets
1511 * num_addrs == 0 Normal mode, clear multicast list
1513 * num_addrs > 0 Multicast mode, receive normal and MC packets,
1514 * and do best-effort filtering.
1516 * See mc32_update_stats() regards setting the SAV BP bit.
1520 static void do_mc32_set_multicast_list(struct net_device *dev, int retry)
1522 struct mc32_local *lp = netdev_priv(dev);
1523 u16 filt = (1<<2); /* Save Bad Packets, for stats purposes */
1525 if ((dev->flags&IFF_PROMISC) ||
1526 (dev->flags&IFF_ALLMULTI) ||
1528 /* Enable promiscuous mode */
1530 else if(dev->mc_count)
1532 unsigned char block[62];
1534 struct dev_mc_list *dmc=dev->mc_list;
1539 lp->mc_list_valid = 0;
1540 if(!lp->mc_list_valid)
1543 block[0]=dev->mc_count;
1546 for(i=0;i<dev->mc_count;i++)
1548 memcpy(bp, dmc->dmi_addr, 6);
1552 if(mc32_command_nowait(dev, 2, block, 2+6*dev->mc_count)==-1)
1554 lp->mc_reload_wait = 1;
1557 lp->mc_list_valid=1;
1561 if(mc32_command_nowait(dev, 0, &filt, 2)==-1)
1563 lp->mc_reload_wait = 1;
1566 lp->mc_reload_wait = 0;
1572 * mc32_set_multicast_list - queue multicast list update
1573 * @dev: The 3c527 to use
1575 * Commence loading the multicast list. This is called when the kernel
1576 * changes the lists. It will override any pending list we are trying to
1580 static void mc32_set_multicast_list(struct net_device *dev)
1582 do_mc32_set_multicast_list(dev,0);
1587 * mc32_reset_multicast_list - reset multicast list
1588 * @dev: The 3c527 to use
1590 * Attempt the next step in loading the multicast lists. If this attempt
1591 * fails to complete then it will be scheduled and this function called
1592 * again later from elsewhere.
1595 static void mc32_reset_multicast_list(struct net_device *dev)
1597 do_mc32_set_multicast_list(dev,1);
1600 static void netdev_get_drvinfo(struct net_device *dev,
1601 struct ethtool_drvinfo *info)
1603 strcpy(info->driver, DRV_NAME);
1604 strcpy(info->version, DRV_VERSION);
1605 sprintf(info->bus_info, "MCA 0x%lx", dev->base_addr);
1608 static u32 netdev_get_msglevel(struct net_device *dev)
1613 static void netdev_set_msglevel(struct net_device *dev, u32 level)
1618 static const struct ethtool_ops netdev_ethtool_ops = {
1619 .get_drvinfo = netdev_get_drvinfo,
1620 .get_msglevel = netdev_get_msglevel,
1621 .set_msglevel = netdev_set_msglevel,
1626 static struct net_device *this_device;
1629 * init_module - entry point
1631 * Probe and locate a 3c527 card. This really should probe and locate
1632 * all the 3c527 cards in the machine not just one of them. Yes you can
1633 * insmod multiple modules for now but it's a hack.
1636 int __init init_module(void)
1638 this_device = mc32_probe(-1);
1639 if (IS_ERR(this_device))
1640 return PTR_ERR(this_device);
1645 * cleanup_module - free resources for an unload
1647 * Unloading time. We release the MCA bus resources and the interrupt
1648 * at which point everything is ready to unload. The card must be stopped
1649 * at this point or we would not have been called. When we unload we
1650 * leave the card stopped but not totally shut down. When the card is
1651 * initialized it must be rebooted or the rings reloaded before any
1652 * transmit operations are allowed to start scribbling into memory.
1655 void __exit cleanup_module(void)
1657 unregister_netdev(this_device);
1658 cleanup_card(this_device);
1659 free_netdev(this_device);