blob: 278e791afe0097ab834a4ebe5edeefd157778203 [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
Lucas De Marchi25985ed2011-03-30 22:57:33 -030054 * its buffer rings, but are used only for initial setup in this
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#include <asm/io.h>
110#include <asm/dma.h>
111
112#include "3c527.h"
113
114MODULE_LICENSE("GPL");
115
116/*
117 * The name of the card. Is used for messages and in the requests for
118 * io regions, irqs and dma channels
119 */
120static const char* cardname = DRV_NAME;
121
122/* use 0 for production, 1 for verification, >2 for debug */
123#ifndef NET_DEBUG
124#define NET_DEBUG 2
125#endif
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static unsigned int mc32_debug = NET_DEBUG;
128
129/* The number of low I/O ports used by the ethercard. */
130#define MC32_IO_EXTENT 8
131
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400132/* As implemented, values must be a power-of-2 -- 4/8/16/32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#define TX_RING_LEN 32 /* Typically the card supports 37 */
134#define RX_RING_LEN 8 /* " " " */
135
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400136/* Copy break point, see above for details.
137 * Setting to > 1512 effectively disables this feature. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#define RX_COPYBREAK 200 /* Value from 3c59x.c */
139
140/* Issue the 82586 workaround command - this is for "busy lans", but
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400141 * basically means for all lans now days - has a performance (latency)
142 * cost, but best set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static const int WORKAROUND_82586=1;
144
145/* Pointers to buffers and their on-card records */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400146struct mc32_ring_desc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400148 volatile struct skb_header *p;
149 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150};
151
152/* Information that needs to be kept for each board. */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400153struct mc32_local
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 int slot;
156
157 u32 base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 volatile struct mc32_mailbox *rx_box;
159 volatile struct mc32_mailbox *tx_box;
160 volatile struct mc32_mailbox *exec_box;
161 volatile struct mc32_stats *stats; /* Start of on-card statistics */
162 u16 tx_chain; /* Transmit list start offset */
163 u16 rx_chain; /* Receive list start offset */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400164 u16 tx_len; /* Transmit list count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 u16 rx_len; /* Receive list count */
166
167 u16 xceiver_desired_state; /* HALTED or RUNNING */
168 u16 cmd_nonblocking; /* Thread is uninterested in command result */
169 u16 mc_reload_wait; /* A multicast load request is pending */
170 u32 mc_list_valid; /* True when the mclist is set */
171
172 struct mc32_ring_desc tx_ring[TX_RING_LEN]; /* Host Transmit ring */
173 struct mc32_ring_desc rx_ring[RX_RING_LEN]; /* Host Receive ring */
174
175 atomic_t tx_count; /* buffers left */
176 atomic_t tx_ring_head; /* index to tx en-queue end */
177 u16 tx_ring_tail; /* index to tx de-queue end */
178
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400179 u16 rx_ring_tail; /* index to rx de-queue end */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 struct semaphore cmd_mutex; /* Serialises issuing of execute commands */
182 struct completion execution_cmd; /* Card has completed an execute command */
183 struct completion xceiver_cmd; /* Card has completed a tx or rx command */
184};
185
186/* The station (ethernet) address prefix, used for a sanity check. */
187#define SA_ADDR0 0x02
188#define SA_ADDR1 0x60
189#define SA_ADDR2 0xAC
190
191struct mca_adapters_t {
192 unsigned int id;
193 char *name;
194};
195
196static const struct mca_adapters_t mc32_adapters[] = {
197 { 0x0041, "3COM EtherLink MC/32" },
198 { 0x8EF5, "IBM High Performance Lan Adapter" },
199 { 0x0000, NULL }
200};
201
202
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400203/* Macros for ring index manipulations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204static inline u16 next_rx(u16 rx) { return (rx+1)&(RX_RING_LEN-1); };
205static inline u16 prev_rx(u16 rx) { return (rx-1)&(RX_RING_LEN-1); };
206
207static inline u16 next_tx(u16 tx) { return (tx+1)&(TX_RING_LEN-1); };
208
209
210/* Index to functions, as function prototypes. */
211static int mc32_probe1(struct net_device *dev, int ioaddr);
212static int mc32_command(struct net_device *dev, u16 cmd, void *data, int len);
213static int mc32_open(struct net_device *dev);
214static void mc32_timeout(struct net_device *dev);
Stephen Hemminger27a1de92009-08-31 19:50:54 +0000215static netdev_tx_t mc32_send_packet(struct sk_buff *skb,
216 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100217static irqreturn_t mc32_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218static int mc32_close(struct net_device *dev);
219static struct net_device_stats *mc32_get_stats(struct net_device *dev);
220static void mc32_set_multicast_list(struct net_device *dev);
221static void mc32_reset_multicast_list(struct net_device *dev);
Jeff Garzik7282d492006-09-13 14:30:00 -0400222static const struct ethtool_ops netdev_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224static void cleanup_card(struct net_device *dev)
225{
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);
232}
233
234/**
235 * mc32_probe - Search for supported boards
236 * @unit: interface number to use
237 *
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
241 * in particular.
242 */
243
244struct net_device *__init mc32_probe(int unit)
245{
246 struct net_device *dev = alloc_etherdev(sizeof(struct mc32_local));
247 static int current_mca_slot = -1;
248 int i;
249 int err;
250
251 if (!dev)
252 return ERR_PTR(-ENOMEM);
253
254 if (unit >= 0)
255 sprintf(dev->name, "eth%d", unit);
256
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400257 /* Do not check any supplied i/o locations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 POS registers usually don't fail :) */
259
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400260 /* MCA cards have POS registers.
261 Autodetecting MCA cards is extremely simple.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 Just search for the card. */
263
264 for(i = 0; (mc32_adapters[i].name != NULL); i++) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400265 current_mca_slot =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 mca_find_unused_adapter(mc32_adapters[i].id, 0);
267
268 if(current_mca_slot != MCA_NOTFOUND) {
269 if(!mc32_probe1(dev, current_mca_slot))
270 {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400271 mca_set_adapter_name(current_mca_slot,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 mc32_adapters[i].name);
273 mca_mark_as_used(current_mca_slot);
274 err = register_netdev(dev);
275 if (err) {
276 cleanup_card(dev);
277 free_netdev(dev);
278 dev = ERR_PTR(err);
279 }
280 return dev;
281 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284 }
285 free_netdev(dev);
286 return ERR_PTR(-ENODEV);
287}
288
Stephen Hemminger4394e652009-01-09 13:01:17 +0000289static 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,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000294 .ndo_set_rx_mode = mc32_set_multicast_list,
Stephen Hemminger4394e652009-01-09 13:01:17 +0000295 .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,
299};
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/**
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
305 *
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
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400308 * in firmware so we have to wait for it to return and post us either a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 * failure case or some addresses we use to find the board internals.
310 */
311
312static int __init mc32_probe1(struct net_device *dev, int slot)
313{
314 static unsigned version_printed;
315 int i, err;
316 u8 POS;
317 u32 base;
318 struct mc32_local *lp = netdev_priv(dev);
Joe Perchesb6bc7652010-12-21 02:16:08 -0800319 static const u16 mca_io_bases[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 0x7280,0x7290,
321 0x7680,0x7690,
322 0x7A80,0x7A90,
323 0x7E80,0x7E90
324 };
Joe Perchesb6bc7652010-12-21 02:16:08 -0800325 static const u32 mca_mem_bases[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 0x00C0000,
327 0x00C4000,
328 0x00C8000,
329 0x00CC000,
330 0x00D0000,
331 0x00D4000,
332 0x00D8000,
333 0x00DC000
334 };
Joe Perchesb6bc7652010-12-21 02:16:08 -0800335 static const char * const failures[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 "Processor instruction",
337 "Processor data bus",
338 "Processor data bus",
339 "Processor data bus",
340 "Adapter bus",
341 "ROM checksum",
342 "Base RAM",
343 "Extended RAM",
344 "82586 internal loopback",
345 "82586 initialisation failure",
346 "Adapter list configuration error"
347 };
348
349 /* Time to play MCA games */
350
351 if (mc32_debug && version_printed++ == 0)
Alexander Beregalov39738e12009-05-26 12:35:26 +0000352 pr_debug("%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Alexander Beregalov39738e12009-05-26 12:35:26 +0000354 pr_info("%s: %s found in slot %d: ", dev->name, cardname, slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 POS = mca_read_stored_pos(slot, 2);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if(!(POS&1))
359 {
Alexander Beregalov39738e12009-05-26 12:35:26 +0000360 pr_cont("disabled.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return -ENODEV;
362 }
363
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];
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 POS = mca_read_stored_pos(slot, 4);
369 if(!(POS&1))
370 {
Alexander Beregalov39738e12009-05-26 12:35:26 +0000371 pr_cont("memory window disabled.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return -ENODEV;
373 }
374
375 POS = mca_read_stored_pos(slot, 5);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 i=(POS>>4)&3;
378 if(i==3)
379 {
Alexander Beregalov39738e12009-05-26 12:35:26 +0000380 pr_cont("invalid memory window.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return -ENODEV;
382 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 i*=16384;
385 i+=16384;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 dev->mem_end=dev->mem_start + i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 dev->irq = ((POS>>2)&3)+9;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if(!request_region(dev->base_addr, MC32_IO_EXTENT, cardname))
392 {
Alexander Beregalov39738e12009-05-26 12:35:26 +0000393 pr_cont("io 0x%3lX, which is busy.\n", dev->base_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return -EBUSY;
395 }
396
Alexander Beregalov39738e12009-05-26 12:35:26 +0000397 pr_cont("io 0x%3lX irq %d mem 0x%lX (%dK)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 dev->base_addr, dev->irq, dev->mem_start, i/1024);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400399
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 /* We ought to set the cache line size here.. */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400402
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /*
405 * Go PROM browsing
406 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /* Retrieve and print the ethernet address. */
409 for (i = 0; i < 6; i++)
410 {
411 mca_write_pos(slot, 6, i+12);
412 mca_write_pos(slot, 7, 0);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400413
Joe Perches0795af52007-10-03 17:59:30 -0700414 dev->dev_addr[i] = mca_read_pos(slot,3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416
Alexander Beregalov39738e12009-05-26 12:35:26 +0000417 pr_info("%s: Address %pM ", dev->name, dev->dev_addr);
Joe Perches0795af52007-10-03 17:59:30 -0700418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 mca_write_pos(slot, 6, 0);
420 mca_write_pos(slot, 7, 0);
421
422 POS = mca_read_stored_pos(slot, 4);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if(POS&2)
Alexander Beregalov39738e12009-05-26 12:35:26 +0000425 pr_cont(": BNC port selected.\n");
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400426 else
Alexander Beregalov39738e12009-05-26 12:35:26 +0000427 pr_cont(": AUI port selected.\n");
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 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);
433 /* Reset adapter */
434 udelay(100);
435 /* Reset off */
436 POS&=~(HOST_CTRL_ATTN|HOST_CTRL_RESET);
437 outb(POS, dev->base_addr+HOST_CTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 udelay(300);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /*
442 * Grab the IRQ
443 */
444
Paul Gortmaker5d6076b2010-10-14 14:21:51 +0000445 err = request_irq(dev->irq, mc32_interrupt, IRQF_SHARED, DRV_NAME, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (err) {
447 release_region(dev->base_addr, MC32_IO_EXTENT);
Alexander Beregalov39738e12009-05-26 12:35:26 +0000448 pr_err("%s: unable to get IRQ %d.\n", DRV_NAME, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 goto err_exit_ports;
450 }
451
452 memset(lp, 0, sizeof(struct mc32_local));
453 lp->slot = slot;
454
455 i=0;
456
457 base = inb(dev->base_addr);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 while(base == 0xFF)
460 {
461 i++;
462 if(i == 1000)
463 {
Alexander Beregalov39738e12009-05-26 12:35:26 +0000464 pr_err("%s: failed to boot adapter.\n", dev->name);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400465 err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 goto err_exit_irq;
467 }
468 udelay(1000);
469 if(inb(dev->base_addr+2)&(1<<5))
470 base = inb(dev->base_addr);
471 }
472
473 if(base>0)
474 {
475 if(base < 0x0C)
Alexander Beregalov39738e12009-05-26 12:35:26 +0000476 pr_err("%s: %s%s.\n", dev->name, failures[base-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 base<0x0A?" test failure":"");
478 else
Alexander Beregalov39738e12009-05-26 12:35:26 +0000479 pr_err("%s: unknown failure %d.\n", dev->name, base);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400480 err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 goto err_exit_irq;
482 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 base=0;
485 for(i=0;i<4;i++)
486 {
487 int n=0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 while(!(inb(dev->base_addr+2)&(1<<5)))
490 {
491 n++;
492 udelay(50);
493 if(n>100)
494 {
Alexander Beregalov39738e12009-05-26 12:35:26 +0000495 pr_err("%s: mailbox read fail (%d).\n", dev->name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 err = -ENODEV;
497 goto err_exit_irq;
498 }
499 }
500
501 base|=(inb(dev->base_addr)<<(8*i));
502 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 lp->exec_box=isa_bus_to_virt(dev->mem_start+base);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400505
506 base=lp->exec_box->data[1]<<16|lp->exec_box->data[0];
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 lp->base = dev->mem_start+base;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400509
510 lp->rx_box=isa_bus_to_virt(lp->base + lp->exec_box->data[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 lp->tx_box=isa_bus_to_virt(lp->base + lp->exec_box->data[3]);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 lp->stats = isa_bus_to_virt(lp->base + lp->exec_box->data[5]);
514
515 /*
516 * Descriptor chains (card relative)
517 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 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 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400521 lp->tx_len = lp->exec_box->data[9]; /* Transmit list count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 lp->rx_len = lp->exec_box->data[11]; /* Receive list count */
523
Thomas Gleixner50948ee2010-09-07 14:32:10 +0000524 sema_init(&lp->cmd_mutex, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 init_completion(&lp->execution_cmd);
526 init_completion(&lp->xceiver_cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400527
Alexander Beregalov39738e12009-05-26 12:35:26 +0000528 pr_info("%s: Firmware Rev %d. %d RX buffers, %d TX buffers. Base of 0x%08X.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 dev->name, lp->exec_box->data[12], lp->rx_len, lp->tx_len, lp->base);
530
Stephen Hemminger4394e652009-01-09 13:01:17 +0000531 dev->netdev_ops = &netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 dev->watchdog_timeo = HZ*5; /* Board does all the work */
533 dev->ethtool_ops = &netdev_ethtool_ops;
534
535 return 0;
536
537err_exit_irq:
538 free_irq(dev->irq, dev);
539err_exit_ports:
540 release_region(dev->base_addr, MC32_IO_EXTENT);
541 return err;
542}
543
544
545/**
546 * mc32_ready_poll - wait until we can feed it a command
547 * @dev: The device to wait for
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400548 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 * 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.
552 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554static inline void mc32_ready_poll(struct net_device *dev)
555{
556 int ioaddr = dev->base_addr;
557 while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
558}
559
560
561/**
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
567 *
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
574 * after us.
575 */
576
577static int mc32_command_nowait(struct net_device *dev, u16 cmd, void *data, int len)
578{
579 struct mc32_local *lp = netdev_priv(dev);
580 int ioaddr = dev->base_addr;
581 int ret = -1;
582
583 if (down_trylock(&lp->cmd_mutex) == 0)
584 {
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 */
590
591 /* Send the command */
592 mc32_ready_poll(dev);
593 outb(1<<6, ioaddr+HOST_CMD);
594
595 ret = 0;
596
597 /* Interrupt handler will signal mutex on completion */
598 }
599
600 return ret;
601}
602
603
604/**
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
610 *
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
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400613 * from a previous command before we execute our command. After our
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 * command completes we will attempt any pending multicast reload
615 * we blocked off by hogging the exec buffer.
616 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400617 * You feed the card a command, you wait, it interrupts you get a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 * 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.
621 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623static int mc32_command(struct net_device *dev, u16 cmd, void *data, int len)
624{
625 struct mc32_local *lp = netdev_priv(dev);
626 int ioaddr = dev->base_addr;
627 int ret = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 down(&lp->cmd_mutex);
630
631 /*
632 * My Turn
633 */
634
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 */
640
641 mc32_ready_poll(dev);
642 outb(1<<6, ioaddr+HOST_CMD);
643
644 wait_for_completion(&lp->execution_cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if(lp->exec_box->mbox&(1<<13))
647 ret = -1;
648
649 up(&lp->cmd_mutex);
650
651 /*
652 * A multicast set got blocked - try it now
653 */
654
655 if(lp->mc_reload_wait)
656 {
657 mc32_reset_multicast_list(dev);
658 }
659
660 return ret;
661}
662
663
664/**
665 * mc32_start_transceiver - tell board to restart tx/rx
666 * @dev: The 3c527 card to issue the command to
667 *
668 * This may be called from the interrupt state, where it is used
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400669 * to restart the rx ring if the card runs out of rx buffers.
670 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 * We must first check if it's ok to (re)start the transceiver. See
672 * mc32_close for details.
673 */
674
675static void mc32_start_transceiver(struct net_device *dev) {
676
677 struct mc32_local *lp = netdev_priv(dev);
678 int ioaddr = dev->base_addr;
679
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400680 /* Ignore RX overflow on device closure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (lp->xceiver_desired_state==HALTED)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400682 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 /* Give the card the offset to the post-EOL-bit RX descriptor */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400685 mc32_ready_poll(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 lp->rx_box->mbox=0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400687 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400690 mc32_ready_poll(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 lp->tx_box->mbox=0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400692 outb(HOST_CMD_RESTRT_TX, ioaddr+HOST_CMD); /* card ignores this on RX restart */
693
694 /* We are not interrupted on start completion */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
697
698/**
699 * mc32_halt_transceiver - tell board to stop tx/rx
700 * @dev: The 3c527 card to issue the command to
701 *
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.
705 *
706 * We then sleep until the card has notified us that both rx and
707 * tx have been suspended.
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400708 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400710static void mc32_halt_transceiver(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 struct mc32_local *lp = netdev_priv(dev);
713 int ioaddr = dev->base_addr;
714
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400715 mc32_ready_poll(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 lp->rx_box->mbox=0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400717 outb(HOST_CMD_SUSPND_RX, ioaddr+HOST_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 wait_for_completion(&lp->xceiver_cmd);
719
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400720 mc32_ready_poll(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 lp->tx_box->mbox=0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400722 outb(HOST_CMD_SUSPND_TX, ioaddr+HOST_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 wait_for_completion(&lp->xceiver_cmd);
724}
725
726
727/**
728 * mc32_load_rx_ring - load the ring of receive buffers
729 * @dev: 3c527 to build the ring for
730 *
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200731 * This initialises the on-card and driver datastructures to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 * the point where mc32_start_transceiver() can be called.
733 *
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.
736 *
737 * We allocate an sk_buff for each ring entry in turn and
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200738 * initialise its house-keeping info. At the same time, we read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 * each 'next' pointer in our rx_ring array. This reduces slow
740 * shared-memory reads and makes it easy to access predecessor
741 * descriptors.
742 *
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.
745 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747static int mc32_load_rx_ring(struct net_device *dev)
748{
749 struct mc32_local *lp = netdev_priv(dev);
750 int i;
751 u16 rx_base;
752 volatile struct skb_header *p;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 rx_base=lp->rx_chain;
755
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) {
759 for (;i>=0;i--)
760 kfree_skb(lp->rx_ring[i].skb);
761 return -ENOBUFS;
762 }
763 skb_reserve(lp->rx_ring[i].skb, 18);
764
765 p=isa_bus_to_virt(lp->base+rx_base);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 p->control=0;
768 p->data=isa_virt_to_bus(lp->rx_ring[i].skb->data);
769 p->status=0;
770 p->length=1532;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400771
772 lp->rx_ring[i].p=p;
773 rx_base=p->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775
776 lp->rx_ring[i-1].p->control |= CONTROL_EOL;
777
778 lp->rx_ring_tail=0;
779
780 return 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400781}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783
784/**
785 * mc32_flush_rx_ring - free the ring of receive buffers
786 * @lp: Local data of 3c527 to flush the rx ring of
787 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400788 * Free the buffer for each ring slot. This may be called
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 * before mc32_load_rx_ring(), eg. on error in mc32_open().
790 * Requires rx skb pointers to point to a valid skb, or NULL.
791 */
792
793static void mc32_flush_rx_ring(struct net_device *dev)
794{
795 struct mc32_local *lp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400796 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400798 for(i=0; i < RX_RING_LEN; i++)
799 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (lp->rx_ring[i].skb) {
801 dev_kfree_skb(lp->rx_ring[i].skb);
802 lp->rx_ring[i].skb = NULL;
803 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400804 lp->rx_ring[i].p=NULL;
805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
808
809/**
810 * mc32_load_tx_ring - load transmit ring
811 * @dev: The 3c527 card to issue the command to
812 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400813 * This sets up the host transmit data-structures.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300815 * First, we obtain from the card it's current position in the tx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 * ring, so that we will know where to begin transmitting
817 * packets.
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400818 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 * 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.
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400822 *
823 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825static void mc32_load_tx_ring(struct net_device *dev)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400826{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 struct mc32_local *lp = netdev_priv(dev);
828 volatile struct skb_header *p;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400829 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 u16 tx_base;
831
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400832 tx_base=lp->tx_box->data[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 for(i=0 ; i<TX_RING_LEN ; i++)
835 {
836 p=isa_bus_to_virt(lp->base+tx_base);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400837 lp->tx_ring[i].p=p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 lp->tx_ring[i].skb=NULL;
839
840 tx_base=p->next;
841 }
842
843 /* -1 so that tx_ring_head cannot "lap" tx_ring_tail */
844 /* see mc32_tx_ring */
845
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400846 atomic_set(&lp->tx_count, TX_RING_LEN-1);
847 atomic_set(&lp->tx_ring_head, 0);
848 lp->tx_ring_tail=0;
849}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851
852/**
853 * mc32_flush_tx_ring - free transmit ring
854 * @lp: Local data of 3c527 to flush the tx ring of
855 *
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,
859 * or NULL.
860 */
861
862static void mc32_flush_tx_ring(struct net_device *dev)
863{
864 struct mc32_local *lp = netdev_priv(dev);
865 int i;
866
867 for (i=0; i < TX_RING_LEN; i++)
868 {
869 if (lp->tx_ring[i].skb)
870 {
871 dev_kfree_skb(lp->tx_ring[i].skb);
872 lp->tx_ring[i].skb = NULL;
873 }
874 }
875
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400876 atomic_set(&lp->tx_count, 0);
877 atomic_set(&lp->tx_ring_head, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 lp->tx_ring_tail=0;
879}
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882/**
883 * mc32_open - handle 'up' of card
884 * @dev: device to open
885 *
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.
890 *
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
896 * all off.
897 */
898
899static int mc32_open(struct net_device *dev)
900{
901 int ioaddr = dev->base_addr;
902 struct mc32_local *lp = netdev_priv(dev);
903 u8 one=1;
904 u8 regs;
905 u16 descnumbuffs[2] = {TX_RING_LEN, RX_RING_LEN};
906
907 /*
908 * Interrupts enabled
909 */
910
911 regs=inb(ioaddr+HOST_CTRL);
912 regs|=HOST_CTRL_INTE;
913 outb(regs, ioaddr+HOST_CTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 /*
916 * Allow ourselves to issue commands
917 */
918
919 up(&lp->cmd_mutex);
920
921
922 /*
923 * Send the indications on command
924 */
925
926 mc32_command(dev, 4, &one, 2);
927
928 /*
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400929 * Poke it to make sure it's really dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 */
931
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400932 mc32_halt_transceiver(dev);
933 mc32_flush_tx_ring(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400935 /*
936 * Ask card to set up on-card descriptors to our spec
937 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400939 if(mc32_command(dev, 8, descnumbuffs, 4)) {
Alexander Beregalov39738e12009-05-26 12:35:26 +0000940 pr_info("%s: %s rejected our buffer configuration!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 dev->name, cardname);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400942 mc32_close(dev);
943 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400945
946 /* Report new configuration */
947 mc32_command(dev, 6, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
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 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400951 lp->tx_len = lp->exec_box->data[9]; /* Transmit list count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 lp->rx_len = lp->exec_box->data[11]; /* Receive list count */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 /* Set Network Address */
955 mc32_command(dev, 1, dev->dev_addr, 6);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 /* Set the filters */
958 mc32_set_multicast_list(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400959
960 if (WORKAROUND_82586) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 u16 zero_word=0;
962 mc32_command(dev, 0x0D, &zero_word, 2); /* 82586 bug workaround on */
963 }
964
965 mc32_load_tx_ring(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400966
967 if(mc32_load_rx_ring(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 {
969 mc32_close(dev);
970 return -ENOBUFS;
971 }
972
973 lp->xceiver_desired_state = RUNNING;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /* And finally, set the ball rolling... */
976 mc32_start_transceiver(dev);
977
978 netif_start_queue(dev);
979
980 return 0;
981}
982
983
984/**
985 * mc32_timeout - handle a timeout from the network layer
986 * @dev: 3c527 that timed out
987 *
988 * Handle a timeout on transmit from the 3c527. This normally means
989 * bad things as the hardware handles cable timeouts and mess for
990 * us.
991 *
992 */
993
994static void mc32_timeout(struct net_device *dev)
995{
Alexander Beregalov39738e12009-05-26 12:35:26 +0000996 pr_warning("%s: transmit timed out?\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 /* Try to restart the adaptor. */
998 netif_wake_queue(dev);
999}
1000
1001
1002/**
1003 * mc32_send_packet - queue a frame for transmit
1004 * @skb: buffer to transmit
1005 * @dev: 3c527 to send it out of
1006 *
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.
1012 *
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
1019 * irq handler).
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001020 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 */
1022
Stephen Hemminger27a1de92009-08-31 19:50:54 +00001023static netdev_tx_t mc32_send_packet(struct sk_buff *skb,
1024 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
1026 struct mc32_local *lp = netdev_priv(dev);
1027 u32 head = atomic_read(&lp->tx_ring_head);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 volatile struct skb_header *p, *np;
1030
1031 netif_stop_queue(dev);
1032
1033 if(atomic_read(&lp->tx_count)==0) {
Patrick McHardy5b548142009-06-12 06:22:29 +00001034 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036
Herbert Xu5b057c62006-06-23 02:06:41 -07001037 if (skb_padto(skb, ETH_ZLEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 netif_wake_queue(dev);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001039 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
1041
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001042 atomic_dec(&lp->tx_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 /* P is the last sending/sent buffer as a pointer */
1045 p=lp->tx_ring[head].p;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 head = next_tx(head);
1048
1049 /* NP is the buffer we will be loading */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001050 np=lp->tx_ring[head].p;
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 /* We will need this to flush the buffer out */
1053 lp->tx_ring[head].skb=skb;
1054
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001055 np->length = unlikely(skb->len < ETH_ZLEN) ? ETH_ZLEN : skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 np->data = isa_virt_to_bus(skb->data);
1057 np->status = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001058 np->control = CONTROL_EOP | CONTROL_EOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 wmb();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 /*
1062 * The new frame has been setup; we can now
1063 * let the interrupt handler and card "see" it
1064 */
1065
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001066 atomic_set(&lp->tx_ring_head, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 p->control &= ~CONTROL_EOL;
1068
1069 netif_wake_queue(dev);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001070 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
1073
1074/**
1075 * mc32_update_stats - pull off the on board statistics
1076 * @dev: 3c527 to service
1077 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001078 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 * Query and reset the on-card stats. There's the small possibility
1080 * of a race here, which would result in an underestimation of
1081 * actual errors. As such, we'd prefer to keep all our stats
1082 * collection in software. As a rule, we do. However it can't be
1083 * used for rx errors and collisions as, by default, the card discards
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001084 * bad rx packets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 *
1086 * Setting the SAV BP in the rx filter command supposedly
1087 * stops this behaviour. However, testing shows that it only seems to
1088 * enable the collation of on-card rx statistics --- the driver
1089 * never sees an RX descriptor with an error status set.
1090 *
1091 */
1092
1093static void mc32_update_stats(struct net_device *dev)
1094{
1095 struct mc32_local *lp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001096 volatile struct mc32_stats *st = lp->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001098 u32 rx_errors=0;
1099
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001100 rx_errors+=dev->stats.rx_crc_errors +=st->rx_crc_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 st->rx_crc_errors=0;
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001102 rx_errors+=dev->stats.rx_fifo_errors +=st->rx_overrun_errors;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001103 st->rx_overrun_errors=0;
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001104 rx_errors+=dev->stats.rx_frame_errors +=st->rx_alignment_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 st->rx_alignment_errors=0;
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001106 rx_errors+=dev->stats.rx_length_errors+=st->rx_tooshort_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 st->rx_tooshort_errors=0;
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001108 rx_errors+=dev->stats.rx_missed_errors+=st->rx_outofresource_errors;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001109 st->rx_outofresource_errors=0;
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001110 dev->stats.rx_errors=rx_errors;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 /* Number of packets which saw one collision */
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001113 dev->stats.collisions+=st->dataC[10];
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001114 st->dataC[10]=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001116 /* Number of packets which saw 2--15 collisions */
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001117 dev->stats.collisions+=st->dataC[11];
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001118 st->dataC[11]=0;
1119}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121
1122/**
1123 * mc32_rx_ring - process the receive ring
1124 * @dev: 3c527 that needs its receive ring processing
1125 *
1126 *
1127 * We have received one or more indications from the card that a
1128 * receive has completed. The buffer ring thus contains dirty
1129 * entries. We walk the ring by iterating over the circular rx_ring
1130 * array, starting at the next dirty buffer (which happens to be the
1131 * one we finished up at last time around).
1132 *
1133 * For each completed packet, we will either copy it and pass it up
1134 * the stack or, if the packet is near MTU sized, we allocate
1135 * another buffer and flip the old one up the stack.
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001136 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 * We must succeed in keeping a buffer on the ring. If necessary we
1138 * will toss a received packet rather than lose a ring entry. Once
1139 * the first uncompleted descriptor is found, we move the
1140 * End-Of-List bit to include the buffers just processed.
1141 *
1142 */
1143
1144static void mc32_rx_ring(struct net_device *dev)
1145{
1146 struct mc32_local *lp = netdev_priv(dev);
1147 volatile struct skb_header *p;
1148 u16 rx_ring_tail;
1149 u16 rx_old_tail;
1150 int x=0;
1151
1152 rx_old_tail = rx_ring_tail = lp->rx_ring_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001154 do
1155 {
1156 p=lp->rx_ring[rx_ring_tail].p;
1157
1158 if(!(p->status & (1<<7))) { /* Not COMPLETED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if(p->status & (1<<6)) /* COMPLETED_OK */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001162 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 u16 length=p->length;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001165 struct sk_buff *skb;
1166 struct sk_buff *newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 /* Try to save time by avoiding a copy on big frames */
1169
Joe Perches8e95a202009-12-03 07:58:21 +00001170 if ((length > RX_COPYBREAK) &&
Pradeep A Dalvic056b732012-02-05 02:50:38 +00001171 ((newskb = netdev_alloc_skb(dev, 1532)) != NULL))
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001172 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 skb=lp->rx_ring[rx_ring_tail].skb;
1174 skb_put(skb, length);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001175
1176 skb_reserve(newskb,18);
1177 lp->rx_ring[rx_ring_tail].skb=newskb;
1178 p->data=isa_virt_to_bus(newskb->data);
1179 }
1180 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 {
Pradeep A Dalvic056b732012-02-05 02:50:38 +00001182 skb = netdev_alloc_skb(dev, length + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 if(skb==NULL) {
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001185 dev->stats.rx_dropped++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001186 goto dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188
1189 skb_reserve(skb,2);
1190 memcpy(skb_put(skb, length),
1191 lp->rx_ring[rx_ring_tail].skb->data, length);
1192 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001193
1194 skb->protocol=eth_type_trans(skb,dev);
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001195 dev->stats.rx_packets++;
1196 dev->stats.rx_bytes += length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 netif_rx(skb);
1198 }
1199
1200 dropped:
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001201 p->length = 1532;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 p->status = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001203
1204 rx_ring_tail=next_rx(rx_ring_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001206 while(x++<48);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001208 /* If there was actually a frame to be processed, place the EOL bit */
1209 /* at the descriptor prior to the one to be filled next */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001211 if (rx_ring_tail != rx_old_tail)
1212 {
1213 lp->rx_ring[prev_rx(rx_ring_tail)].p->control |= CONTROL_EOL;
1214 lp->rx_ring[prev_rx(rx_old_tail)].p->control &= ~CONTROL_EOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001216 lp->rx_ring_tail=rx_ring_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 }
1218}
1219
1220
1221/**
1222 * mc32_tx_ring - process completed transmits
1223 * @dev: 3c527 that needs its transmit ring processing
1224 *
1225 *
1226 * This operates in a similar fashion to mc32_rx_ring. We iterate
1227 * over the transmit ring. For each descriptor which has been
1228 * processed by the card, we free its associated buffer and note
1229 * any errors. This continues until the transmit ring is emptied
1230 * or we reach a descriptor that hasn't yet been processed by the
1231 * card.
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001232 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 */
1234
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001235static void mc32_tx_ring(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
1237 struct mc32_local *lp = netdev_priv(dev);
1238 volatile struct skb_header *np;
1239
1240 /*
1241 * We rely on head==tail to mean 'queue empty'.
1242 * This is why lp->tx_count=TX_RING_LEN-1: in order to prevent
1243 * tx_ring_head wrapping to tail and confusing a 'queue empty'
1244 * condition with 'queue full'
1245 */
1246
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001247 while (lp->tx_ring_tail != atomic_read(&lp->tx_ring_head))
1248 {
1249 u16 t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001251 t=next_tx(lp->tx_ring_tail);
1252 np=lp->tx_ring[t].p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001254 if(!(np->status & (1<<7)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001256 /* Not COMPLETED */
1257 break;
1258 }
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001259 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if(!(np->status & (1<<6))) /* Not COMPLETED_OK */
1261 {
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001262 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 switch(np->status&0x0F)
1265 {
1266 case 1:
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001267 dev->stats.tx_aborted_errors++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001268 break; /* Max collisions */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 case 2:
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001270 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 break;
1272 case 3:
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001273 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 break;
1275 case 4:
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001276 dev->stats.tx_window_errors++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001277 break; /* CTS Lost */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 case 5:
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001279 dev->stats.tx_aborted_errors++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001280 break; /* Transmit timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
1282 }
1283 /* Packets are sent in order - this is
1284 basically a FIFO queue of buffers matching
1285 the card ring */
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001286 dev->stats.tx_bytes+=lp->tx_ring[t].skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 dev_kfree_skb_irq(lp->tx_ring[t].skb);
1288 lp->tx_ring[t].skb=NULL;
1289 atomic_inc(&lp->tx_count);
1290 netif_wake_queue(dev);
1291
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001292 lp->tx_ring_tail=t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001295}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297
1298/**
1299 * mc32_interrupt - handle an interrupt from a 3c527
1300 * @irq: Interrupt number
1301 * @dev_id: 3c527 that requires servicing
1302 * @regs: Registers (unused)
1303 *
1304 *
1305 * An interrupt is raised whenever the 3c527 writes to the command
1306 * register. This register contains the message it wishes to send us
1307 * packed into a single byte field. We keep reading status entries
1308 * until we have processed all the control items, but simply count
1309 * transmit and receive reports. When all reports are in we empty the
1310 * transceiver rings as appropriate. This saves the overhead of
1311 * multiple command requests.
1312 *
1313 * Because MCA is level-triggered, we shouldn't miss indications.
1314 * Therefore, we needn't ask the card to suspend interrupts within
1315 * this handler. The card receives an implicit acknowledgment of the
1316 * current interrupt when we read the command register.
1317 *
1318 */
1319
David Howells7d12e782006-10-05 14:55:46 +01001320static irqreturn_t mc32_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
1322 struct net_device *dev = dev_id;
1323 struct mc32_local *lp;
1324 int ioaddr, status, boguscount = 0;
1325 int rx_event = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001326 int tx_event = 0;
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 ioaddr = dev->base_addr;
1329 lp = netdev_priv(dev);
1330
1331 /* See whats cooking */
1332
1333 while((inb(ioaddr+HOST_STATUS)&HOST_STATUS_CWR) && boguscount++<2000)
1334 {
1335 status=inb(ioaddr+HOST_CMD);
1336
Alexander Beregalov39738e12009-05-26 12:35:26 +00001337 pr_debug("Status TX%d RX%d EX%d OV%d BC%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 (status&7), (status>>3)&7, (status>>6)&1,
1339 (status>>7)&1, boguscount);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 switch(status&7)
1342 {
1343 case 0:
1344 break;
1345 case 6: /* TX fail */
1346 case 2: /* TX ok */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001347 tx_event = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 break;
1349 case 3: /* Halt */
1350 case 4: /* Abort */
1351 complete(&lp->xceiver_cmd);
1352 break;
1353 default:
Alexander Beregalov39738e12009-05-26 12:35:26 +00001354 pr_notice("%s: strange tx ack %d\n", dev->name, status&7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
1356 status>>=3;
1357 switch(status&7)
1358 {
1359 case 0:
1360 break;
1361 case 2: /* RX */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001362 rx_event=1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 break;
1364 case 3: /* Halt */
1365 case 4: /* Abort */
1366 complete(&lp->xceiver_cmd);
1367 break;
1368 case 6:
1369 /* Out of RX buffers stat */
1370 /* Must restart rx */
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001371 dev->stats.rx_dropped++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001372 mc32_rx_ring(dev);
1373 mc32_start_transceiver(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 break;
1375 default:
Alexander Beregalov39738e12009-05-26 12:35:26 +00001376 pr_notice("%s: strange rx ack %d\n",
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001377 dev->name, status&7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
1379 status>>=3;
1380 if(status&1)
1381 {
1382 /*
1383 * No thread is waiting: we need to tidy
1384 * up ourself.
1385 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (lp->cmd_nonblocking) {
1388 up(&lp->cmd_mutex);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001389 if (lp->mc_reload_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 mc32_reset_multicast_list(dev);
1391 }
1392 else complete(&lp->execution_cmd);
1393 }
1394 if(status&2)
1395 {
1396 /*
1397 * We get interrupted once per
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001398 * counter that is about to overflow.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 */
1400
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001401 mc32_update_stats(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403 }
1404
1405
1406 /*
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001407 * Process the transmit and receive rings
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 */
1409
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001410 if(tx_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 mc32_tx_ring(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001412
1413 if(rx_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 mc32_rx_ring(dev);
1415
1416 return IRQ_HANDLED;
1417}
1418
1419
1420/**
1421 * mc32_close - user configuring the 3c527 down
1422 * @dev: 3c527 card to shut down
1423 *
1424 * The 3c527 is a bus mastering device. We must be careful how we
1425 * shut it down. It may also be running shared interrupt so we have
1426 * to be sure to silence it properly
1427 *
1428 * We indicate that the card is closing to the rest of the
1429 * driver. Otherwise, it is possible that the card may run out
1430 * of receive buffers and restart the transceiver while we're
1431 * trying to close it.
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001432 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 * We abort any receive and transmits going on and then wait until
1434 * any pending exec commands have completed in other code threads.
1435 * In theory we can't get here while that is true, in practice I am
1436 * paranoid
1437 *
1438 * We turn off the interrupt enable for the board to be sure it can't
1439 * intefere with other devices.
1440 */
1441
1442static int mc32_close(struct net_device *dev)
1443{
1444 struct mc32_local *lp = netdev_priv(dev);
1445 int ioaddr = dev->base_addr;
1446
1447 u8 regs;
1448 u16 one=1;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 lp->xceiver_desired_state = HALTED;
1451 netif_stop_queue(dev);
1452
1453 /*
1454 * Send the indications on command (handy debug check)
1455 */
1456
1457 mc32_command(dev, 4, &one, 2);
1458
1459 /* Shut down the transceiver */
1460
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001461 mc32_halt_transceiver(dev);
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 /* Ensure we issue no more commands beyond this point */
1464
1465 down(&lp->cmd_mutex);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001466
1467 /* Ok the card is now stopping */
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 regs=inb(ioaddr+HOST_CTRL);
1470 regs&=~HOST_CTRL_INTE;
1471 outb(regs, ioaddr+HOST_CTRL);
1472
1473 mc32_flush_rx_ring(dev);
1474 mc32_flush_tx_ring(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001475
1476 mc32_update_stats(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 return 0;
1479}
1480
1481
1482/**
1483 * mc32_get_stats - hand back stats to network layer
1484 * @dev: The 3c527 card to handle
1485 *
1486 * We've collected all the stats we can in software already. Now
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001487 * it's time to update those kept on-card and return the lot.
1488 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 */
1490
1491static struct net_device_stats *mc32_get_stats(struct net_device *dev)
1492{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001493 mc32_update_stats(dev);
Paulius Zaleckas4711c842008-04-29 12:09:38 +03001494 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
1497
1498/**
1499 * do_mc32_set_multicast_list - attempt to update multicasts
1500 * @dev: 3c527 device to load the list on
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001501 * @retry: indicates this is not the first call.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 *
1503 *
1504 * Actually set or clear the multicast filter for this adaptor. The
1505 * locking issues are handled by this routine. We have to track
1506 * state as it may take multiple calls to get the command sequence
1507 * completed. We just keep trying to schedule the loads until we
1508 * manage to process them all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001510 * num_addrs == -1 Promiscuous mode, receive all packets
1511 *
1512 * num_addrs == 0 Normal mode, clear multicast list
1513 *
1514 * num_addrs > 0 Multicast mode, receive normal and MC packets,
1515 * and do best-effort filtering.
1516 *
1517 * See mc32_update_stats() regards setting the SAV BP bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 *
1519 */
1520
1521static void do_mc32_set_multicast_list(struct net_device *dev, int retry)
1522{
1523 struct mc32_local *lp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001524 u16 filt = (1<<2); /* Save Bad Packets, for stats purposes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Wang Chenc16d1182008-07-22 13:13:12 +08001526 if ((dev->flags&IFF_PROMISC) ||
1527 (dev->flags&IFF_ALLMULTI) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001528 netdev_mc_count(dev) > 10)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 /* Enable promiscuous mode */
1530 filt |= 1;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001531 else if (!netdev_mc_empty(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 {
1533 unsigned char block[62];
1534 unsigned char *bp;
Jiri Pirko22bedad2010-04-01 21:22:57 +00001535 struct netdev_hw_addr *ha;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if(retry==0)
1538 lp->mc_list_valid = 0;
1539 if(!lp->mc_list_valid)
1540 {
1541 block[1]=0;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001542 block[0]=netdev_mc_count(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 bp=block+2;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001544
Jiri Pirko22bedad2010-04-01 21:22:57 +00001545 netdev_for_each_mc_addr(ha, dev) {
1546 memcpy(bp, ha->addr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 bp+=6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001549 if(mc32_command_nowait(dev, 2, block,
1550 2+6*netdev_mc_count(dev))==-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 {
1552 lp->mc_reload_wait = 1;
1553 return;
1554 }
1555 lp->mc_list_valid=1;
1556 }
1557 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001558
1559 if(mc32_command_nowait(dev, 0, &filt, 2)==-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 {
1561 lp->mc_reload_wait = 1;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001562 }
1563 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 lp->mc_reload_wait = 0;
1565 }
1566}
1567
1568
1569/**
1570 * mc32_set_multicast_list - queue multicast list update
1571 * @dev: The 3c527 to use
1572 *
1573 * Commence loading the multicast list. This is called when the kernel
1574 * changes the lists. It will override any pending list we are trying to
1575 * load.
1576 */
1577
1578static void mc32_set_multicast_list(struct net_device *dev)
1579{
1580 do_mc32_set_multicast_list(dev,0);
1581}
1582
1583
1584/**
1585 * mc32_reset_multicast_list - reset multicast list
1586 * @dev: The 3c527 to use
1587 *
1588 * Attempt the next step in loading the multicast lists. If this attempt
1589 * fails to complete then it will be scheduled and this function called
1590 * again later from elsewhere.
1591 */
1592
1593static void mc32_reset_multicast_list(struct net_device *dev)
1594{
1595 do_mc32_set_multicast_list(dev,1);
1596}
1597
1598static void netdev_get_drvinfo(struct net_device *dev,
1599 struct ethtool_drvinfo *info)
1600{
1601 strcpy(info->driver, DRV_NAME);
1602 strcpy(info->version, DRV_VERSION);
1603 sprintf(info->bus_info, "MCA 0x%lx", dev->base_addr);
1604}
1605
1606static u32 netdev_get_msglevel(struct net_device *dev)
1607{
1608 return mc32_debug;
1609}
1610
1611static void netdev_set_msglevel(struct net_device *dev, u32 level)
1612{
1613 mc32_debug = level;
1614}
1615
Jeff Garzik7282d492006-09-13 14:30:00 -04001616static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 .get_drvinfo = netdev_get_drvinfo,
1618 .get_msglevel = netdev_get_msglevel,
1619 .set_msglevel = netdev_set_msglevel,
1620};
1621
1622#ifdef MODULE
1623
1624static struct net_device *this_device;
1625
1626/**
1627 * init_module - entry point
1628 *
1629 * Probe and locate a 3c527 card. This really should probe and locate
1630 * all the 3c527 cards in the machine not just one of them. Yes you can
1631 * insmod multiple modules for now but it's a hack.
1632 */
1633
Randy Dunlap96e672c2006-06-10 13:33:48 -07001634int __init init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
1636 this_device = mc32_probe(-1);
1637 if (IS_ERR(this_device))
1638 return PTR_ERR(this_device);
1639 return 0;
1640}
1641
1642/**
1643 * cleanup_module - free resources for an unload
1644 *
1645 * Unloading time. We release the MCA bus resources and the interrupt
1646 * at which point everything is ready to unload. The card must be stopped
1647 * at this point or we would not have been called. When we unload we
1648 * leave the card stopped but not totally shut down. When the card is
1649 * initialized it must be rebooted or the rings reloaded before any
1650 * transmit operations are allowed to start scribbling into memory.
1651 */
1652
Al Viroafc8eb42006-06-14 18:50:53 -04001653void __exit cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
1655 unregister_netdev(this_device);
1656 cleanup_card(this_device);
1657 free_netdev(this_device);
1658}
1659
1660#endif /* MODULE */