blob: ea1ad8ce883640cf73e9ddfcbcff00e491c6f670 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux Ethernet device driver for the 3Com Etherlink Plus (3C505)
3 * By Craig Southeren, Juha Laiho and Philip Blundell
4 *
5 * 3c505.c This module implements an interface to the 3Com
6 * Etherlink Plus (3c505) Ethernet card. Linux device
7 * driver interface reverse engineered from the Linux 3C509
8 * device drivers. Some 3C505 information gleaned from
9 * the Crynwr packet driver. Still this driver would not
10 * be here without 3C505 technical reference provided by
11 * 3Com.
12 *
13 * $Id: 3c505.c,v 1.10 1996/04/16 13:06:27 phil Exp $
14 *
15 * Authors: Linux 3c505 device driver by
16 * Craig Southeren, <craigs@ineluki.apana.org.au>
17 * Final debugging by
18 * Andrew Tridgell, <tridge@nimbus.anu.edu.au>
19 * Auto irq/address, tuning, cleanup and v1.1.4+ kernel mods by
20 * Juha Laiho, <jlaiho@ichaos.nullnet.fi>
21 * Linux 3C509 driver by
22 * Donald Becker, <becker@super.org>
23 * (Now at <becker@scyld.com>)
24 * Crynwr packet driver by
25 * Krishnan Gopalan and Gregg Stefancik,
26 * Clemson University Engineering Computer Operations.
27 * Portions of the code have been adapted from the 3c505
28 * driver for NCSA Telnet by Bruce Orchard and later
29 * modified by Warren Van Houten and krus@diku.dk.
30 * 3C505 technical information provided by
31 * Terry Murphy, of 3Com Network Adapter Division
32 * Linux 1.3.0 changes by
33 * Alan Cox <Alan.Cox@linux.org>
34 * More debugging, DMA support, currently maintained by
35 * Philip Blundell <philb@gnu.org>
36 * Multicard/soft configurable dma channel/rev 2 hardware support
37 * by Christopher Collins <ccollins@pcug.org.au>
38 * Ethtool support (jgarzik), 11/17/2001
39 */
40
41#define DRV_NAME "3c505"
42#define DRV_VERSION "1.10a"
43
44
45/* Theory of operation:
46 *
47 * The 3c505 is quite an intelligent board. All communication with it is done
48 * by means of Primary Command Blocks (PCBs); these are transferred using PIO
49 * through the command register. The card has 256k of on-board RAM, which is
50 * used to buffer received packets. It might seem at first that more buffers
51 * are better, but in fact this isn't true. From my tests, it seems that
52 * more than about 10 buffers are unnecessary, and there is a noticeable
53 * performance hit in having more active on the card. So the majority of the
54 * card's memory isn't, in fact, used. Sadly, the card only has one transmit
55 * buffer and, short of loading our own firmware into it (which is what some
56 * drivers resort to) there's nothing we can do about this.
57 *
58 * We keep up to 4 "receive packet" commands active on the board at a time.
59 * When a packet comes in, so long as there is a receive command active, the
60 * board will send us a "packet received" PCB and then add the data for that
61 * packet to the DMA queue. If a DMA transfer is not already in progress, we
62 * set one up to start uploading the data. We have to maintain a list of
63 * backlogged receive packets, because the card may decide to tell us about
64 * a newly-arrived packet at any time, and we may not be able to start a DMA
65 * transfer immediately (ie one may already be going on). We can't NAK the
66 * PCB, because then it would throw the packet away.
67 *
68 * Trying to send a PCB to the card at the wrong moment seems to have bad
69 * effects. If we send it a transmit PCB while a receive DMA is happening,
70 * it will just NAK the PCB and so we will have wasted our time. Worse, it
71 * sometimes seems to interrupt the transfer. The majority of the low-level
72 * code is protected by one huge semaphore -- "busy" -- which is set whenever
73 * it probably isn't safe to do anything to the card. The receive routine
74 * must gain a lock on "busy" before it can start a DMA transfer, and the
75 * transmit routine must gain a lock before it sends the first PCB to the card.
76 * The send_pcb() routine also has an internal semaphore to protect it against
77 * being re-entered (which would be disastrous) -- this is needed because
78 * several things can happen asynchronously (re-priming the receiver and
79 * asking the card for statistics, for example). send_pcb() will also refuse
80 * to talk to the card at all if a DMA upload is happening. The higher-level
81 * networking code will reschedule a later retry if some part of the driver
82 * is blocked. In practice, this doesn't seem to happen very often.
83 */
84
85/* This driver may now work with revision 2.x hardware, since all the read
86 * operations on the HCR have been removed (we now keep our own softcopy).
87 * But I don't have an old card to test it on.
88 *
89 * This has had the bad effect that the autoprobe routine is now a bit
90 * less friendly to other devices. However, it was never very good.
91 * before, so I doubt it will hurt anybody.
92 */
93
94/* The driver is a mess. I took Craig's and Juha's code, and hacked it firstly
95 * to make it more reliable, and secondly to add DMA mode. Many things could
96 * probably be done better; the concurrency protection is particularly awful.
97 */
98
99#include <linux/module.h>
100#include <linux/kernel.h>
101#include <linux/string.h>
102#include <linux/interrupt.h>
103#include <linux/errno.h>
104#include <linux/in.h>
105#include <linux/slab.h>
106#include <linux/ioport.h>
107#include <linux/spinlock.h>
108#include <linux/ethtool.h>
109#include <linux/delay.h>
110#include <linux/bitops.h>
111
112#include <asm/uaccess.h>
113#include <asm/io.h>
114#include <asm/dma.h>
115
116#include <linux/netdevice.h>
117#include <linux/etherdevice.h>
118#include <linux/skbuff.h>
119#include <linux/init.h>
120
121#include "3c505.h"
122
123/*********************************************************
124 *
125 * define debug messages here as common strings to reduce space
126 *
127 *********************************************************/
128
129static const char filename[] = __FILE__;
130
131static const char timeout_msg[] = "*** timeout at %s:%s (line %d) ***\n";
132#define TIMEOUT_MSG(lineno) \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -0700133 printk(timeout_msg, filename,__func__,(lineno))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135static const char invalid_pcb_msg[] =
136"*** invalid pcb length %d at %s:%s (line %d) ***\n";
137#define INVALID_PCB_MSG(len) \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -0700138 printk(invalid_pcb_msg, (len),filename,__func__,__LINE__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140static char search_msg[] __initdata = KERN_INFO "%s: Looking for 3c505 adapter at address %#x...";
141
142static char stilllooking_msg[] __initdata = "still looking...";
143
144static char found_msg[] __initdata = "found.\n";
145
146static char notfound_msg[] __initdata = "not found (reason = %d)\n";
147
148static char couldnot_msg[] __initdata = KERN_INFO "%s: 3c505 not found\n";
149
150/*********************************************************
151 *
152 * various other debug stuff
153 *
154 *********************************************************/
155
156#ifdef ELP_DEBUG
157static int elp_debug = ELP_DEBUG;
158#else
159static int elp_debug;
160#endif
161#define debug elp_debug
162
163/*
164 * 0 = no messages (well, some)
165 * 1 = messages when high level commands performed
166 * 2 = messages when low level commands performed
167 * 3 = messages when interrupts received
168 */
169
170/*****************************************************************
171 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * List of I/O-addresses we try to auto-sense
173 * Last element MUST BE 0!
174 *****************************************************************/
175
176static int addr_list[] __initdata = {0x300, 0x280, 0x310, 0};
177
178/* Dma Memory related stuff */
179
180static unsigned long dma_mem_alloc(int size)
181{
182 int order = get_order(size);
183 return __get_dma_pages(GFP_KERNEL, order);
184}
185
186
187/*****************************************************************
188 *
189 * Functions for I/O (note the inline !)
190 *
191 *****************************************************************/
192
193static inline unsigned char inb_status(unsigned int base_addr)
194{
195 return inb(base_addr + PORT_STATUS);
196}
197
198static inline int inb_command(unsigned int base_addr)
199{
200 return inb(base_addr + PORT_COMMAND);
201}
202
203static inline void outb_control(unsigned char val, struct net_device *dev)
204{
205 outb(val, dev->base_addr + PORT_CONTROL);
Wang Chen454d7c92008-11-12 23:37:49 -0800206 ((elp_device *)(netdev_priv(dev)))->hcr_val = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Wang Chen454d7c92008-11-12 23:37:49 -0800209#define HCR_VAL(x) (((elp_device *)(netdev_priv(x)))->hcr_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211static inline void outb_command(unsigned char val, unsigned int base_addr)
212{
213 outb(val, base_addr + PORT_COMMAND);
214}
215
216static inline unsigned int backlog_next(unsigned int n)
217{
218 return (n + 1) % BACKLOG_SIZE;
219}
220
221/*****************************************************************
222 *
223 * useful functions for accessing the adapter
224 *
225 *****************************************************************/
226
227/*
228 * use this routine when accessing the ASF bits as they are
229 * changed asynchronously by the adapter
230 */
231
232/* get adapter PCB status */
233#define GET_ASF(addr) \
234 (get_status(addr)&ASF_PCB_MASK)
235
236static inline int get_status(unsigned int base_addr)
237{
238 unsigned long timeout = jiffies + 10*HZ/100;
239 register int stat1;
240 do {
241 stat1 = inb_status(base_addr);
242 } while (stat1 != inb_status(base_addr) && time_before(jiffies, timeout));
243 if (time_after_eq(jiffies, timeout))
244 TIMEOUT_MSG(__LINE__);
245 return stat1;
246}
247
248static inline void set_hsf(struct net_device *dev, int hsf)
249{
Wang Chen454d7c92008-11-12 23:37:49 -0800250 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 unsigned long flags;
252
253 spin_lock_irqsave(&adapter->lock, flags);
254 outb_control((HCR_VAL(dev) & ~HSF_PCB_MASK) | hsf, dev);
255 spin_unlock_irqrestore(&adapter->lock, flags);
256}
257
Richard Knutssondf570f92007-08-14 01:49:40 +0200258static bool start_receive(struct net_device *, pcb_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Jesper Juhl77933d72005-07-27 11:46:09 -0700260static inline void adapter_reset(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 unsigned long timeout;
Wang Chen454d7c92008-11-12 23:37:49 -0800263 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 unsigned char orig_hcr = adapter->hcr_val;
265
266 outb_control(0, dev);
267
268 if (inb_status(dev->base_addr) & ACRF) {
269 do {
270 inb_command(dev->base_addr);
271 timeout = jiffies + 2*HZ/100;
272 while (time_before_eq(jiffies, timeout) && !(inb_status(dev->base_addr) & ACRF));
273 } while (inb_status(dev->base_addr) & ACRF);
274 set_hsf(dev, HSF_PCB_NAK);
275 }
276 outb_control(adapter->hcr_val | ATTN | DIR, dev);
277 mdelay(10);
278 outb_control(adapter->hcr_val & ~ATTN, dev);
279 mdelay(10);
280 outb_control(adapter->hcr_val | FLSH, dev);
281 mdelay(10);
282 outb_control(adapter->hcr_val & ~FLSH, dev);
283 mdelay(10);
284
285 outb_control(orig_hcr, dev);
286 if (!start_receive(dev, &adapter->tx_pcb))
287 printk(KERN_ERR "%s: start receive command failed \n", dev->name);
288}
289
290/* Check to make sure that a DMA transfer hasn't timed out. This should
291 * never happen in theory, but seems to occur occasionally if the card gets
292 * prodded at the wrong time.
293 */
294static inline void check_3c505_dma(struct net_device *dev)
295{
Wang Chen454d7c92008-11-12 23:37:49 -0800296 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (adapter->dmaing && time_after(jiffies, adapter->current_dma.start_time + 10)) {
298 unsigned long flags, f;
299 printk(KERN_ERR "%s: DMA %s timed out, %d bytes left\n", dev->name, adapter->current_dma.direction ? "download" : "upload", get_dma_residue(dev->dma));
300 spin_lock_irqsave(&adapter->lock, flags);
301 adapter->dmaing = 0;
302 adapter->busy = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 f=claim_dma_lock();
305 disable_dma(dev->dma);
306 release_dma_lock(f);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (adapter->rx_active)
309 adapter->rx_active--;
310 outb_control(adapter->hcr_val & ~(DMAE | TCEN | DIR), dev);
311 spin_unlock_irqrestore(&adapter->lock, flags);
312 }
313}
314
315/* Primitive functions used by send_pcb() */
Richard Knutssondf570f92007-08-14 01:49:40 +0200316static inline bool send_pcb_slow(unsigned int base_addr, unsigned char byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 unsigned long timeout;
319 outb_command(byte, base_addr);
320 for (timeout = jiffies + 5*HZ/100; time_before(jiffies, timeout);) {
321 if (inb_status(base_addr) & HCRE)
Richard Knutssondf570f92007-08-14 01:49:40 +0200322 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324 printk(KERN_WARNING "3c505: send_pcb_slow timed out\n");
Richard Knutssondf570f92007-08-14 01:49:40 +0200325 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Richard Knutssondf570f92007-08-14 01:49:40 +0200328static inline bool send_pcb_fast(unsigned int base_addr, unsigned char byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 unsigned int timeout;
331 outb_command(byte, base_addr);
332 for (timeout = 0; timeout < 40000; timeout++) {
333 if (inb_status(base_addr) & HCRE)
Richard Knutssondf570f92007-08-14 01:49:40 +0200334 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336 printk(KERN_WARNING "3c505: send_pcb_fast timed out\n");
Richard Knutssondf570f92007-08-14 01:49:40 +0200337 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340/* Check to see if the receiver needs restarting, and kick it if so */
341static inline void prime_rx(struct net_device *dev)
342{
Wang Chen454d7c92008-11-12 23:37:49 -0800343 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 while (adapter->rx_active < ELP_RX_PCBS && netif_running(dev)) {
345 if (!start_receive(dev, &adapter->itx_pcb))
346 break;
347 }
348}
349
350/*****************************************************************
351 *
352 * send_pcb
353 * Send a PCB to the adapter.
354 *
355 * output byte to command reg --<--+
356 * wait until HCRE is non zero |
357 * loop until all bytes sent -->--+
358 * set HSF1 and HSF2 to 1
359 * output pcb length
360 * wait until ASF give ACK or NAK
361 * set HSF1 and HSF2 to 0
362 *
363 *****************************************************************/
364
365/* This can be quite slow -- the adapter is allowed to take up to 40ms
366 * to respond to the initial interrupt.
367 *
368 * We run initially with interrupts turned on, but with a semaphore set
369 * so that nobody tries to re-enter this code. Once the first byte has
370 * gone through, we turn interrupts off and then send the others (the
371 * timeout is reduced to 500us).
372 */
373
Richard Knutssondf570f92007-08-14 01:49:40 +0200374static bool send_pcb(struct net_device *dev, pcb_struct * pcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 int i;
377 unsigned long timeout;
Wang Chen454d7c92008-11-12 23:37:49 -0800378 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 unsigned long flags;
380
381 check_3c505_dma(dev);
382
383 if (adapter->dmaing && adapter->current_dma.direction == 0)
Richard Knutssondf570f92007-08-14 01:49:40 +0200384 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 /* Avoid contention */
387 if (test_and_set_bit(1, &adapter->send_pcb_semaphore)) {
388 if (elp_debug >= 3) {
389 printk(KERN_DEBUG "%s: send_pcb entered while threaded\n", dev->name);
390 }
Richard Knutssondf570f92007-08-14 01:49:40 +0200391 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393 /*
394 * load each byte into the command register and
395 * wait for the HCRE bit to indicate the adapter
396 * had read the byte
397 */
398 set_hsf(dev, 0);
399
400 if (send_pcb_slow(dev->base_addr, pcb->command))
401 goto abort;
402
403 spin_lock_irqsave(&adapter->lock, flags);
404
405 if (send_pcb_fast(dev->base_addr, pcb->length))
406 goto sti_abort;
407
408 for (i = 0; i < pcb->length; i++) {
409 if (send_pcb_fast(dev->base_addr, pcb->data.raw[i]))
410 goto sti_abort;
411 }
412
413 outb_control(adapter->hcr_val | 3, dev); /* signal end of PCB */
414 outb_command(2 + pcb->length, dev->base_addr);
415
416 /* now wait for the acknowledgement */
417 spin_unlock_irqrestore(&adapter->lock, flags);
418
419 for (timeout = jiffies + 5*HZ/100; time_before(jiffies, timeout);) {
420 switch (GET_ASF(dev->base_addr)) {
421 case ASF_PCB_ACK:
422 adapter->send_pcb_semaphore = 0;
Richard Knutssondf570f92007-08-14 01:49:40 +0200423 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 case ASF_PCB_NAK:
426#ifdef ELP_DEBUG
427 printk(KERN_DEBUG "%s: send_pcb got NAK\n", dev->name);
428#endif
429 goto abort;
430 }
431 }
432
433 if (elp_debug >= 1)
434 printk(KERN_DEBUG "%s: timeout waiting for PCB acknowledge (status %02x)\n", dev->name, inb_status(dev->base_addr));
435 goto abort;
436
437 sti_abort:
438 spin_unlock_irqrestore(&adapter->lock, flags);
439 abort:
440 adapter->send_pcb_semaphore = 0;
Richard Knutssondf570f92007-08-14 01:49:40 +0200441 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444
445/*****************************************************************
446 *
447 * receive_pcb
448 * Read a PCB from the adapter
449 *
450 * wait for ACRF to be non-zero ---<---+
451 * input a byte |
452 * if ASF1 and ASF2 were not both one |
453 * before byte was read, loop --->---+
454 * set HSF1 and HSF2 for ack
455 *
456 *****************************************************************/
457
Richard Knutssondf570f92007-08-14 01:49:40 +0200458static bool receive_pcb(struct net_device *dev, pcb_struct * pcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 int i, j;
461 int total_length;
462 int stat;
463 unsigned long timeout;
464 unsigned long flags;
465
Wang Chen454d7c92008-11-12 23:37:49 -0800466 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 set_hsf(dev, 0);
469
470 /* get the command code */
471 timeout = jiffies + 2*HZ/100;
472 while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && time_before(jiffies, timeout));
473 if (time_after_eq(jiffies, timeout)) {
474 TIMEOUT_MSG(__LINE__);
Richard Knutssondf570f92007-08-14 01:49:40 +0200475 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477 pcb->command = inb_command(dev->base_addr);
478
479 /* read the data length */
480 timeout = jiffies + 3*HZ/100;
481 while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && time_before(jiffies, timeout));
482 if (time_after_eq(jiffies, timeout)) {
483 TIMEOUT_MSG(__LINE__);
484 printk(KERN_INFO "%s: status %02x\n", dev->name, stat);
Richard Knutssondf570f92007-08-14 01:49:40 +0200485 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487 pcb->length = inb_command(dev->base_addr);
488
489 if (pcb->length > MAX_PCB_DATA) {
490 INVALID_PCB_MSG(pcb->length);
491 adapter_reset(dev);
Richard Knutssondf570f92007-08-14 01:49:40 +0200492 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494 /* read the data */
495 spin_lock_irqsave(&adapter->lock, flags);
496 i = 0;
497 do {
498 j = 0;
499 while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && j++ < 20000);
500 pcb->data.raw[i++] = inb_command(dev->base_addr);
501 if (i > MAX_PCB_DATA)
502 INVALID_PCB_MSG(i);
503 } while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j < 20000);
504 spin_unlock_irqrestore(&adapter->lock, flags);
505 if (j >= 20000) {
506 TIMEOUT_MSG(__LINE__);
Richard Knutssondf570f92007-08-14 01:49:40 +0200507 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509 /* woops, the last "data" byte was really the length! */
510 total_length = pcb->data.raw[--i];
511
512 /* safety check total length vs data length */
513 if (total_length != (pcb->length + 2)) {
514 if (elp_debug >= 2)
515 printk(KERN_WARNING "%s: mangled PCB received\n", dev->name);
516 set_hsf(dev, HSF_PCB_NAK);
Richard Knutssondf570f92007-08-14 01:49:40 +0200517 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519
520 if (pcb->command == CMD_RECEIVE_PACKET_COMPLETE) {
521 if (test_and_set_bit(0, (void *) &adapter->busy)) {
522 if (backlog_next(adapter->rx_backlog.in) == adapter->rx_backlog.out) {
523 set_hsf(dev, HSF_PCB_NAK);
524 printk(KERN_WARNING "%s: PCB rejected, transfer in progress and backlog full\n", dev->name);
525 pcb->command = 0;
Richard Knutssondf570f92007-08-14 01:49:40 +0200526 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 } else {
528 pcb->command = 0xff;
529 }
530 }
531 }
532 set_hsf(dev, HSF_PCB_ACK);
Richard Knutssondf570f92007-08-14 01:49:40 +0200533 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536/******************************************************
537 *
538 * queue a receive command on the adapter so we will get an
539 * interrupt when a packet is received.
540 *
541 ******************************************************/
542
Richard Knutssondf570f92007-08-14 01:49:40 +0200543static bool start_receive(struct net_device *dev, pcb_struct * tx_pcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Richard Knutssondf570f92007-08-14 01:49:40 +0200545 bool status;
Wang Chen454d7c92008-11-12 23:37:49 -0800546 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 if (elp_debug >= 3)
549 printk(KERN_DEBUG "%s: restarting receiver\n", dev->name);
550 tx_pcb->command = CMD_RECEIVE_PACKET;
551 tx_pcb->length = sizeof(struct Rcv_pkt);
552 tx_pcb->data.rcv_pkt.buf_seg
553 = tx_pcb->data.rcv_pkt.buf_ofs = 0; /* Unused */
554 tx_pcb->data.rcv_pkt.buf_len = 1600;
555 tx_pcb->data.rcv_pkt.timeout = 0; /* set timeout to zero */
556 status = send_pcb(dev, tx_pcb);
557 if (status)
558 adapter->rx_active++;
559 return status;
560}
561
562/******************************************************
563 *
564 * extract a packet from the adapter
565 * this routine is only called from within the interrupt
566 * service routine, so no cli/sti calls are needed
567 * note that the length is always assumed to be even
568 *
569 ******************************************************/
570
571static void receive_packet(struct net_device *dev, int len)
572{
573 int rlen;
Wang Chen454d7c92008-11-12 23:37:49 -0800574 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 void *target;
576 struct sk_buff *skb;
577 unsigned long flags;
578
579 rlen = (len + 1) & ~1;
580 skb = dev_alloc_skb(rlen + 2);
581
582 if (!skb) {
583 printk(KERN_WARNING "%s: memory squeeze, dropping packet\n", dev->name);
584 target = adapter->dma_buffer;
585 adapter->current_dma.target = NULL;
586 /* FIXME: stats */
587 return;
588 }
589
590 skb_reserve(skb, 2);
591 target = skb_put(skb, rlen);
592 if ((unsigned long)(target + rlen) >= MAX_DMA_ADDRESS) {
593 adapter->current_dma.target = target;
594 target = adapter->dma_buffer;
595 } else {
596 adapter->current_dma.target = NULL;
597 }
598
599 /* if this happens, we die */
600 if (test_and_set_bit(0, (void *) &adapter->dmaing))
601 printk(KERN_ERR "%s: rx blocked, DMA in progress, dir %d\n", dev->name, adapter->current_dma.direction);
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 adapter->current_dma.direction = 0;
604 adapter->current_dma.length = rlen;
605 adapter->current_dma.skb = skb;
606 adapter->current_dma.start_time = jiffies;
607
608 outb_control(adapter->hcr_val | DIR | TCEN | DMAE, dev);
609
610 flags=claim_dma_lock();
611 disable_dma(dev->dma);
612 clear_dma_ff(dev->dma);
613 set_dma_mode(dev->dma, 0x04); /* dma read */
614 set_dma_addr(dev->dma, isa_virt_to_bus(target));
615 set_dma_count(dev->dma, rlen);
616 enable_dma(dev->dma);
617 release_dma_lock(flags);
618
619 if (elp_debug >= 3) {
620 printk(KERN_DEBUG "%s: rx DMA transfer started\n", dev->name);
621 }
622
623 if (adapter->rx_active)
624 adapter->rx_active--;
625
626 if (!adapter->busy)
627 printk(KERN_WARNING "%s: receive_packet called, busy not set.\n", dev->name);
628}
629
630/******************************************************
631 *
632 * interrupt handler
633 *
634 ******************************************************/
635
David Howells7d12e782006-10-05 14:55:46 +0100636static irqreturn_t elp_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 int len;
639 int dlen;
640 int icount = 0;
Wang Chen454d7c92008-11-12 23:37:49 -0800641 struct net_device *dev = dev_id;
642 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 unsigned long timeout;
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 spin_lock(&adapter->lock);
646
647 do {
648 /*
649 * has a DMA transfer finished?
650 */
651 if (inb_status(dev->base_addr) & DONE) {
652 if (!adapter->dmaing) {
653 printk(KERN_WARNING "%s: phantom DMA completed\n", dev->name);
654 }
655 if (elp_debug >= 3) {
656 printk(KERN_DEBUG "%s: %s DMA complete, status %02x\n", dev->name, adapter->current_dma.direction ? "tx" : "rx", inb_status(dev->base_addr));
657 }
658
659 outb_control(adapter->hcr_val & ~(DMAE | TCEN | DIR), dev);
660 if (adapter->current_dma.direction) {
661 dev_kfree_skb_irq(adapter->current_dma.skb);
662 } else {
663 struct sk_buff *skb = adapter->current_dma.skb;
664 if (skb) {
665 if (adapter->current_dma.target) {
666 /* have already done the skb_put() */
667 memcpy(adapter->current_dma.target, adapter->dma_buffer, adapter->current_dma.length);
668 }
669 skb->protocol = eth_type_trans(skb,dev);
Paulius Zaleckasba0f6ca2008-04-29 02:27:37 +0300670 dev->stats.rx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673 }
674 adapter->dmaing = 0;
675 if (adapter->rx_backlog.in != adapter->rx_backlog.out) {
676 int t = adapter->rx_backlog.length[adapter->rx_backlog.out];
677 adapter->rx_backlog.out = backlog_next(adapter->rx_backlog.out);
678 if (elp_debug >= 2)
679 printk(KERN_DEBUG "%s: receiving backlogged packet (%d)\n", dev->name, t);
680 receive_packet(dev, t);
681 } else {
682 adapter->busy = 0;
683 }
684 } else {
685 /* has one timed out? */
686 check_3c505_dma(dev);
687 }
688
689 /*
690 * receive a PCB from the adapter
691 */
692 timeout = jiffies + 3*HZ/100;
693 while ((inb_status(dev->base_addr) & ACRF) != 0 && time_before(jiffies, timeout)) {
694 if (receive_pcb(dev, &adapter->irx_pcb)) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400695 switch (adapter->irx_pcb.command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 {
697 case 0:
698 break;
699 /*
700 * received a packet - this must be handled fast
701 */
702 case 0xff:
703 case CMD_RECEIVE_PACKET_COMPLETE:
704 /* if the device isn't open, don't pass packets up the stack */
705 if (!netif_running(dev))
706 break;
707 len = adapter->irx_pcb.data.rcv_resp.pkt_len;
708 dlen = adapter->irx_pcb.data.rcv_resp.buf_len;
709 if (adapter->irx_pcb.data.rcv_resp.timeout != 0) {
710 printk(KERN_ERR "%s: interrupt - packet not received correctly\n", dev->name);
711 } else {
712 if (elp_debug >= 3) {
713 printk(KERN_DEBUG "%s: interrupt - packet received of length %i (%i)\n", dev->name, len, dlen);
714 }
715 if (adapter->irx_pcb.command == 0xff) {
716 if (elp_debug >= 2)
717 printk(KERN_DEBUG "%s: adding packet to backlog (len = %d)\n", dev->name, dlen);
718 adapter->rx_backlog.length[adapter->rx_backlog.in] = dlen;
719 adapter->rx_backlog.in = backlog_next(adapter->rx_backlog.in);
720 } else {
721 receive_packet(dev, dlen);
722 }
723 if (elp_debug >= 3)
724 printk(KERN_DEBUG "%s: packet received\n", dev->name);
725 }
726 break;
727
728 /*
729 * 82586 configured correctly
730 */
731 case CMD_CONFIGURE_82586_RESPONSE:
732 adapter->got[CMD_CONFIGURE_82586] = 1;
733 if (elp_debug >= 3)
734 printk(KERN_DEBUG "%s: interrupt - configure response received\n", dev->name);
735 break;
736
737 /*
738 * Adapter memory configuration
739 */
740 case CMD_CONFIGURE_ADAPTER_RESPONSE:
741 adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] = 1;
742 if (elp_debug >= 3)
743 printk(KERN_DEBUG "%s: Adapter memory configuration %s.\n", dev->name,
744 adapter->irx_pcb.data.failed ? "failed" : "succeeded");
745 break;
746
747 /*
748 * Multicast list loading
749 */
750 case CMD_LOAD_MULTICAST_RESPONSE:
751 adapter->got[CMD_LOAD_MULTICAST_LIST] = 1;
752 if (elp_debug >= 3)
753 printk(KERN_DEBUG "%s: Multicast address list loading %s.\n", dev->name,
754 adapter->irx_pcb.data.failed ? "failed" : "succeeded");
755 break;
756
757 /*
758 * Station address setting
759 */
760 case CMD_SET_ADDRESS_RESPONSE:
761 adapter->got[CMD_SET_STATION_ADDRESS] = 1;
762 if (elp_debug >= 3)
763 printk(KERN_DEBUG "%s: Ethernet address setting %s.\n", dev->name,
764 adapter->irx_pcb.data.failed ? "failed" : "succeeded");
765 break;
766
767
768 /*
769 * received board statistics
770 */
771 case CMD_NETWORK_STATISTICS_RESPONSE:
Paulius Zaleckasba0f6ca2008-04-29 02:27:37 +0300772 dev->stats.rx_packets += adapter->irx_pcb.data.netstat.tot_recv;
773 dev->stats.tx_packets += adapter->irx_pcb.data.netstat.tot_xmit;
774 dev->stats.rx_crc_errors += adapter->irx_pcb.data.netstat.err_CRC;
775 dev->stats.rx_frame_errors += adapter->irx_pcb.data.netstat.err_align;
776 dev->stats.rx_fifo_errors += adapter->irx_pcb.data.netstat.err_ovrrun;
777 dev->stats.rx_over_errors += adapter->irx_pcb.data.netstat.err_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 adapter->got[CMD_NETWORK_STATISTICS] = 1;
779 if (elp_debug >= 3)
780 printk(KERN_DEBUG "%s: interrupt - statistics response received\n", dev->name);
781 break;
782
783 /*
784 * sent a packet
785 */
786 case CMD_TRANSMIT_PACKET_COMPLETE:
787 if (elp_debug >= 3)
788 printk(KERN_DEBUG "%s: interrupt - packet sent\n", dev->name);
789 if (!netif_running(dev))
790 break;
791 switch (adapter->irx_pcb.data.xmit_resp.c_stat) {
792 case 0xffff:
Paulius Zaleckasba0f6ca2008-04-29 02:27:37 +0300793 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 printk(KERN_INFO "%s: transmit timed out, network cable problem?\n", dev->name);
795 break;
796 case 0xfffe:
Paulius Zaleckasba0f6ca2008-04-29 02:27:37 +0300797 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 printk(KERN_INFO "%s: transmit timed out, FIFO underrun\n", dev->name);
799 break;
800 }
801 netif_wake_queue(dev);
802 break;
803
804 /*
805 * some unknown PCB
806 */
807 default:
808 printk(KERN_DEBUG "%s: unknown PCB received - %2.2x\n", dev->name, adapter->irx_pcb.command);
809 break;
810 }
811 } else {
812 printk(KERN_WARNING "%s: failed to read PCB on interrupt\n", dev->name);
813 adapter_reset(dev);
814 }
815 }
816
817 } while (icount++ < 5 && (inb_status(dev->base_addr) & (ACRF | DONE)));
818
819 prime_rx(dev);
820
821 /*
822 * indicate no longer in interrupt routine
823 */
824 spin_unlock(&adapter->lock);
825 return IRQ_HANDLED;
826}
827
828
829/******************************************************
830 *
831 * open the board
832 *
833 ******************************************************/
834
835static int elp_open(struct net_device *dev)
836{
Wang Chen454d7c92008-11-12 23:37:49 -0800837 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 int retval;
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (elp_debug >= 3)
841 printk(KERN_DEBUG "%s: request to open device\n", dev->name);
842
843 /*
844 * make sure we actually found the device
845 */
846 if (adapter == NULL) {
847 printk(KERN_ERR "%s: Opening a non-existent physical device\n", dev->name);
848 return -EAGAIN;
849 }
850 /*
851 * disable interrupts on the board
852 */
853 outb_control(0, dev);
854
855 /*
856 * clear any pending interrupts
857 */
858 inb_command(dev->base_addr);
859 adapter_reset(dev);
860
861 /*
862 * no receive PCBs active
863 */
864 adapter->rx_active = 0;
865
866 adapter->busy = 0;
867 adapter->send_pcb_semaphore = 0;
868 adapter->rx_backlog.in = 0;
869 adapter->rx_backlog.out = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 spin_lock_init(&adapter->lock);
872
873 /*
874 * install our interrupt service routine
875 */
876 if ((retval = request_irq(dev->irq, &elp_interrupt, 0, dev->name, dev))) {
877 printk(KERN_ERR "%s: could not allocate IRQ%d\n", dev->name, dev->irq);
878 return retval;
879 }
880 if ((retval = request_dma(dev->dma, dev->name))) {
881 free_irq(dev->irq, dev);
882 printk(KERN_ERR "%s: could not allocate DMA%d channel\n", dev->name, dev->dma);
883 return retval;
884 }
885 adapter->dma_buffer = (void *) dma_mem_alloc(DMA_BUFFER_SIZE);
886 if (!adapter->dma_buffer) {
887 printk(KERN_ERR "%s: could not allocate DMA buffer\n", dev->name);
888 free_dma(dev->dma);
889 free_irq(dev->irq, dev);
890 return -ENOMEM;
891 }
892 adapter->dmaing = 0;
893
894 /*
895 * enable interrupts on the board
896 */
897 outb_control(CMDE, dev);
898
899 /*
900 * configure adapter memory: we need 10 multicast addresses, default==0
901 */
902 if (elp_debug >= 3)
903 printk(KERN_DEBUG "%s: sending 3c505 memory configuration command\n", dev->name);
904 adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
905 adapter->tx_pcb.data.memconf.cmd_q = 10;
906 adapter->tx_pcb.data.memconf.rcv_q = 20;
907 adapter->tx_pcb.data.memconf.mcast = 10;
908 adapter->tx_pcb.data.memconf.frame = 20;
909 adapter->tx_pcb.data.memconf.rcv_b = 20;
910 adapter->tx_pcb.data.memconf.progs = 0;
911 adapter->tx_pcb.length = sizeof(struct Memconf);
912 adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] = 0;
913 if (!send_pcb(dev, &adapter->tx_pcb))
914 printk(KERN_ERR "%s: couldn't send memory configuration command\n", dev->name);
915 else {
916 unsigned long timeout = jiffies + TIMEOUT;
917 while (adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] == 0 && time_before(jiffies, timeout));
918 if (time_after_eq(jiffies, timeout))
919 TIMEOUT_MSG(__LINE__);
920 }
921
922
923 /*
924 * configure adapter to receive broadcast messages and wait for response
925 */
926 if (elp_debug >= 3)
927 printk(KERN_DEBUG "%s: sending 82586 configure command\n", dev->name);
928 adapter->tx_pcb.command = CMD_CONFIGURE_82586;
929 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
930 adapter->tx_pcb.length = 2;
931 adapter->got[CMD_CONFIGURE_82586] = 0;
932 if (!send_pcb(dev, &adapter->tx_pcb))
933 printk(KERN_ERR "%s: couldn't send 82586 configure command\n", dev->name);
934 else {
935 unsigned long timeout = jiffies + TIMEOUT;
936 while (adapter->got[CMD_CONFIGURE_82586] == 0 && time_before(jiffies, timeout));
937 if (time_after_eq(jiffies, timeout))
938 TIMEOUT_MSG(__LINE__);
939 }
940
941 /* enable burst-mode DMA */
942 /* outb(0x1, dev->base_addr + PORT_AUXDMA); */
943
944 /*
945 * queue receive commands to provide buffering
946 */
947 prime_rx(dev);
948 if (elp_debug >= 3)
949 printk(KERN_DEBUG "%s: %d receive PCBs active\n", dev->name, adapter->rx_active);
950
951 /*
952 * device is now officially open!
953 */
954
955 netif_start_queue(dev);
956 return 0;
957}
958
959
960/******************************************************
961 *
962 * send a packet to the adapter
963 *
964 ******************************************************/
965
Richard Knutssondf570f92007-08-14 01:49:40 +0200966static bool send_packet(struct net_device *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Wang Chen454d7c92008-11-12 23:37:49 -0800968 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 unsigned long target;
970 unsigned long flags;
971
972 /*
973 * make sure the length is even and no shorter than 60 bytes
974 */
975 unsigned int nlen = (((skb->len < 60) ? 60 : skb->len) + 1) & (~1);
976
977 if (test_and_set_bit(0, (void *) &adapter->busy)) {
978 if (elp_debug >= 2)
979 printk(KERN_DEBUG "%s: transmit blocked\n", dev->name);
Richard Knutssondf570f92007-08-14 01:49:40 +0200980 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982
Paulius Zaleckasba0f6ca2008-04-29 02:27:37 +0300983 dev->stats.tx_bytes += nlen;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 /*
986 * send the adapter a transmit packet command. Ignore segment and offset
987 * and make sure the length is even
988 */
989 adapter->tx_pcb.command = CMD_TRANSMIT_PACKET;
990 adapter->tx_pcb.length = sizeof(struct Xmit_pkt);
991 adapter->tx_pcb.data.xmit_pkt.buf_ofs
992 = adapter->tx_pcb.data.xmit_pkt.buf_seg = 0; /* Unused */
993 adapter->tx_pcb.data.xmit_pkt.pkt_len = nlen;
994
995 if (!send_pcb(dev, &adapter->tx_pcb)) {
996 adapter->busy = 0;
Richard Knutssondf570f92007-08-14 01:49:40 +0200997 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999 /* if this happens, we die */
1000 if (test_and_set_bit(0, (void *) &adapter->dmaing))
1001 printk(KERN_DEBUG "%s: tx: DMA %d in progress\n", dev->name, adapter->current_dma.direction);
1002
1003 adapter->current_dma.direction = 1;
1004 adapter->current_dma.start_time = jiffies;
1005
1006 if ((unsigned long)(skb->data + nlen) >= MAX_DMA_ADDRESS || nlen != skb->len) {
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001007 skb_copy_from_linear_data(skb, adapter->dma_buffer, nlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 memset(adapter->dma_buffer+skb->len, 0, nlen-skb->len);
1009 target = isa_virt_to_bus(adapter->dma_buffer);
1010 }
1011 else {
1012 target = isa_virt_to_bus(skb->data);
1013 }
1014 adapter->current_dma.skb = skb;
1015
1016 flags=claim_dma_lock();
1017 disable_dma(dev->dma);
1018 clear_dma_ff(dev->dma);
1019 set_dma_mode(dev->dma, 0x48); /* dma memory -> io */
1020 set_dma_addr(dev->dma, target);
1021 set_dma_count(dev->dma, nlen);
1022 outb_control(adapter->hcr_val | DMAE | TCEN, dev);
1023 enable_dma(dev->dma);
1024 release_dma_lock(flags);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (elp_debug >= 3)
1027 printk(KERN_DEBUG "%s: DMA transfer started\n", dev->name);
1028
Richard Knutssondf570f92007-08-14 01:49:40 +02001029 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
1032/*
1033 * The upper layer thinks we timed out
1034 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036static void elp_timeout(struct net_device *dev)
1037{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 int stat;
1039
1040 stat = inb_status(dev->base_addr);
1041 printk(KERN_WARNING "%s: transmit timed out, lost %s?\n", dev->name, (stat & ACRF) ? "interrupt" : "command");
1042 if (elp_debug >= 1)
1043 printk(KERN_DEBUG "%s: status %#02x\n", dev->name, stat);
1044 dev->trans_start = jiffies;
Paulius Zaleckasba0f6ca2008-04-29 02:27:37 +03001045 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 netif_wake_queue(dev);
1047}
1048
1049/******************************************************
1050 *
1051 * start the transmitter
1052 * return 0 if sent OK, else return 1
1053 *
1054 ******************************************************/
1055
1056static int elp_start_xmit(struct sk_buff *skb, struct net_device *dev)
1057{
1058 unsigned long flags;
Wang Chen454d7c92008-11-12 23:37:49 -08001059 elp_device *adapter = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 spin_lock_irqsave(&adapter->lock, flags);
1062 check_3c505_dma(dev);
1063
1064 if (elp_debug >= 3)
1065 printk(KERN_DEBUG "%s: request to send packet of length %d\n", dev->name, (int) skb->len);
1066
1067 netif_stop_queue(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /*
1070 * send the packet at skb->data for skb->len
1071 */
1072 if (!send_packet(dev, skb)) {
1073 if (elp_debug >= 2) {
1074 printk(KERN_DEBUG "%s: failed to transmit packet\n", dev->name);
1075 }
1076 spin_unlock_irqrestore(&adapter->lock, flags);
1077 return 1;
1078 }
1079 if (elp_debug >= 3)
1080 printk(KERN_DEBUG "%s: packet of length %d sent\n", dev->name, (int) skb->len);
1081
1082 /*
1083 * start the transmit timeout
1084 */
1085 dev->trans_start = jiffies;
1086
1087 prime_rx(dev);
1088 spin_unlock_irqrestore(&adapter->lock, flags);
1089 netif_start_queue(dev);
1090 return 0;
1091}
1092
1093/******************************************************
1094 *
1095 * return statistics on the board
1096 *
1097 ******************************************************/
1098
1099static struct net_device_stats *elp_get_stats(struct net_device *dev)
1100{
Wang Chen454d7c92008-11-12 23:37:49 -08001101 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 if (elp_debug >= 3)
1104 printk(KERN_DEBUG "%s: request for stats\n", dev->name);
1105
1106 /* If the device is closed, just return the latest stats we have,
1107 - we cannot ask from the adapter without interrupts */
1108 if (!netif_running(dev))
Paulius Zaleckasba0f6ca2008-04-29 02:27:37 +03001109 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 /* send a get statistics command to the board */
1112 adapter->tx_pcb.command = CMD_NETWORK_STATISTICS;
1113 adapter->tx_pcb.length = 0;
1114 adapter->got[CMD_NETWORK_STATISTICS] = 0;
1115 if (!send_pcb(dev, &adapter->tx_pcb))
1116 printk(KERN_ERR "%s: couldn't send get statistics command\n", dev->name);
1117 else {
1118 unsigned long timeout = jiffies + TIMEOUT;
1119 while (adapter->got[CMD_NETWORK_STATISTICS] == 0 && time_before(jiffies, timeout));
1120 if (time_after_eq(jiffies, timeout)) {
1121 TIMEOUT_MSG(__LINE__);
Paulius Zaleckasba0f6ca2008-04-29 02:27:37 +03001122 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124 }
1125
1126 /* statistics are now up to date */
Paulius Zaleckasba0f6ca2008-04-29 02:27:37 +03001127 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
1130
1131static void netdev_get_drvinfo(struct net_device *dev,
1132 struct ethtool_drvinfo *info)
1133{
1134 strcpy(info->driver, DRV_NAME);
1135 strcpy(info->version, DRV_VERSION);
1136 sprintf(info->bus_info, "ISA 0x%lx", dev->base_addr);
1137}
1138
1139static u32 netdev_get_msglevel(struct net_device *dev)
1140{
1141 return debug;
1142}
1143
1144static void netdev_set_msglevel(struct net_device *dev, u32 level)
1145{
1146 debug = level;
1147}
1148
Jeff Garzik7282d492006-09-13 14:30:00 -04001149static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 .get_drvinfo = netdev_get_drvinfo,
1151 .get_msglevel = netdev_get_msglevel,
1152 .set_msglevel = netdev_set_msglevel,
1153};
1154
1155/******************************************************
1156 *
1157 * close the board
1158 *
1159 ******************************************************/
1160
1161static int elp_close(struct net_device *dev)
1162{
Wang Chen454d7c92008-11-12 23:37:49 -08001163 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 if (elp_debug >= 3)
1166 printk(KERN_DEBUG "%s: request to close device\n", dev->name);
1167
1168 netif_stop_queue(dev);
1169
1170 /* Someone may request the device statistic information even when
1171 * the interface is closed. The following will update the statistics
1172 * structure in the driver, so we'll be able to give current statistics.
1173 */
1174 (void) elp_get_stats(dev);
1175
1176 /*
1177 * disable interrupts on the board
1178 */
1179 outb_control(0, dev);
1180
1181 /*
1182 * release the IRQ
1183 */
1184 free_irq(dev->irq, dev);
1185
1186 free_dma(dev->dma);
1187 free_pages((unsigned long) adapter->dma_buffer, get_order(DMA_BUFFER_SIZE));
1188
1189 return 0;
1190}
1191
1192
1193/************************************************************
1194 *
1195 * Set multicast list
1196 * num_addrs==0: clear mc_list
1197 * num_addrs==-1: set promiscuous mode
1198 * num_addrs>0: set mc_list
1199 *
1200 ************************************************************/
1201
1202static void elp_set_mc_list(struct net_device *dev)
1203{
Wang Chen454d7c92008-11-12 23:37:49 -08001204 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 struct dev_mc_list *dmi = dev->mc_list;
1206 int i;
1207 unsigned long flags;
1208
1209 if (elp_debug >= 3)
1210 printk(KERN_DEBUG "%s: request to set multicast list\n", dev->name);
1211
1212 spin_lock_irqsave(&adapter->lock, flags);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (!(dev->flags & (IFF_PROMISC | IFF_ALLMULTI))) {
1215 /* send a "load multicast list" command to the board, max 10 addrs/cmd */
1216 /* if num_addrs==0 the list will be cleared */
1217 adapter->tx_pcb.command = CMD_LOAD_MULTICAST_LIST;
1218 adapter->tx_pcb.length = 6 * dev->mc_count;
1219 for (i = 0; i < dev->mc_count; i++) {
1220 memcpy(adapter->tx_pcb.data.multicast[i], dmi->dmi_addr, 6);
1221 dmi = dmi->next;
1222 }
1223 adapter->got[CMD_LOAD_MULTICAST_LIST] = 0;
1224 if (!send_pcb(dev, &adapter->tx_pcb))
1225 printk(KERN_ERR "%s: couldn't send set_multicast command\n", dev->name);
1226 else {
1227 unsigned long timeout = jiffies + TIMEOUT;
1228 while (adapter->got[CMD_LOAD_MULTICAST_LIST] == 0 && time_before(jiffies, timeout));
1229 if (time_after_eq(jiffies, timeout)) {
1230 TIMEOUT_MSG(__LINE__);
1231 }
1232 }
1233 if (dev->mc_count)
1234 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD | RECV_MULTI;
1235 else /* num_addrs == 0 */
1236 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
1237 } else
1238 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_PROMISC;
1239 /*
1240 * configure adapter to receive messages (as specified above)
1241 * and wait for response
1242 */
1243 if (elp_debug >= 3)
1244 printk(KERN_DEBUG "%s: sending 82586 configure command\n", dev->name);
1245 adapter->tx_pcb.command = CMD_CONFIGURE_82586;
1246 adapter->tx_pcb.length = 2;
1247 adapter->got[CMD_CONFIGURE_82586] = 0;
1248 if (!send_pcb(dev, &adapter->tx_pcb))
1249 {
1250 spin_unlock_irqrestore(&adapter->lock, flags);
1251 printk(KERN_ERR "%s: couldn't send 82586 configure command\n", dev->name);
1252 }
1253 else {
1254 unsigned long timeout = jiffies + TIMEOUT;
1255 spin_unlock_irqrestore(&adapter->lock, flags);
1256 while (adapter->got[CMD_CONFIGURE_82586] == 0 && time_before(jiffies, timeout));
1257 if (time_after_eq(jiffies, timeout))
1258 TIMEOUT_MSG(__LINE__);
1259 }
1260}
1261
1262/************************************************************
1263 *
1264 * A couple of tests to see if there's 3C505 or not
1265 * Called only by elp_autodetect
1266 ************************************************************/
1267
1268static int __init elp_sense(struct net_device *dev)
1269{
1270 int addr = dev->base_addr;
1271 const char *name = dev->name;
1272 byte orig_HSR;
1273
1274 if (!request_region(addr, ELP_IO_EXTENT, "3c505"))
1275 return -ENODEV;
1276
1277 orig_HSR = inb_status(addr);
1278
1279 if (elp_debug > 0)
1280 printk(search_msg, name, addr);
1281
1282 if (orig_HSR == 0xff) {
1283 if (elp_debug > 0)
1284 printk(notfound_msg, 1);
1285 goto out;
1286 }
1287
1288 /* Wait for a while; the adapter may still be booting up */
1289 if (elp_debug > 0)
1290 printk(stilllooking_msg);
1291
1292 if (orig_HSR & DIR) {
1293 /* If HCR.DIR is up, we pull it down. HSR.DIR should follow. */
1294 outb(0, dev->base_addr + PORT_CONTROL);
Domen Puncerc56943e2005-05-12 22:21:51 -04001295 msleep(300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (inb_status(addr) & DIR) {
1297 if (elp_debug > 0)
1298 printk(notfound_msg, 2);
1299 goto out;
1300 }
1301 } else {
1302 /* If HCR.DIR is down, we pull it up. HSR.DIR should follow. */
1303 outb(DIR, dev->base_addr + PORT_CONTROL);
Domen Puncerc56943e2005-05-12 22:21:51 -04001304 msleep(300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (!(inb_status(addr) & DIR)) {
1306 if (elp_debug > 0)
1307 printk(notfound_msg, 3);
1308 goto out;
1309 }
1310 }
1311 /*
1312 * It certainly looks like a 3c505.
1313 */
1314 if (elp_debug > 0)
1315 printk(found_msg);
1316
1317 return 0;
1318out:
1319 release_region(addr, ELP_IO_EXTENT);
1320 return -ENODEV;
1321}
1322
1323/*************************************************************
1324 *
1325 * Search through addr_list[] and try to find a 3C505
1326 * Called only by eplus_probe
1327 *************************************************************/
1328
1329static int __init elp_autodetect(struct net_device *dev)
1330{
1331 int idx = 0;
1332
1333 /* if base address set, then only check that address
1334 otherwise, run through the table */
1335 if (dev->base_addr != 0) { /* dev->base_addr == 0 ==> plain autodetect */
1336 if (elp_sense(dev) == 0)
1337 return dev->base_addr;
1338 } else
1339 while ((dev->base_addr = addr_list[idx++])) {
1340 if (elp_sense(dev) == 0)
1341 return dev->base_addr;
1342 }
1343
1344 /* could not find an adapter */
1345 if (elp_debug > 0)
1346 printk(couldnot_msg, dev->name);
1347
1348 return 0; /* Because of this, the layer above will return -ENODEV */
1349}
1350
Stephen Hemmingere6c42b72009-01-09 13:01:12 +00001351static const struct net_device_ops elp_netdev_ops = {
1352 .ndo_open = elp_open,
1353 .ndo_stop = elp_close,
1354 .ndo_get_stats = elp_get_stats,
1355 .ndo_start_xmit = elp_start_xmit,
1356 .ndo_tx_timeout = elp_timeout,
1357 .ndo_set_multicast_list = elp_set_mc_list,
1358 .ndo_change_mtu = eth_change_mtu,
1359 .ndo_set_mac_address = eth_mac_addr,
1360 .ndo_validate_addr = eth_validate_addr,
1361};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363/******************************************************
1364 *
1365 * probe for an Etherlink Plus board at the specified address
1366 *
1367 ******************************************************/
1368
1369/* There are three situations we need to be able to detect here:
1370
1371 * a) the card is idle
1372 * b) the card is still booting up
1373 * c) the card is stuck in a strange state (some DOS drivers do this)
1374 *
1375 * In case (a), all is well. In case (b), we wait 10 seconds to see if the
1376 * card finishes booting, and carry on if so. In case (c), we do a hard reset,
1377 * loop round, and hope for the best.
1378 *
1379 * This is all very unpleasant, but hopefully avoids the problems with the old
1380 * probe code (which had a 15-second delay if the card was idle, and didn't
1381 * work at all if it was in a weird state).
1382 */
1383
1384static int __init elplus_setup(struct net_device *dev)
1385{
Wang Chen454d7c92008-11-12 23:37:49 -08001386 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 int i, tries, tries1, okay;
1388 unsigned long timeout;
1389 unsigned long cookie = 0;
1390 int err = -ENODEV;
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 /*
1393 * setup adapter structure
1394 */
1395
1396 dev->base_addr = elp_autodetect(dev);
1397 if (!dev->base_addr)
1398 return -ENODEV;
1399
1400 adapter->send_pcb_semaphore = 0;
1401
1402 for (tries1 = 0; tries1 < 3; tries1++) {
1403 outb_control((adapter->hcr_val | CMDE) & ~DIR, dev);
1404 /* First try to write just one byte, to see if the card is
1405 * responding at all normally.
1406 */
1407 timeout = jiffies + 5*HZ/100;
1408 okay = 0;
1409 while (time_before(jiffies, timeout) && !(inb_status(dev->base_addr) & HCRE));
1410 if ((inb_status(dev->base_addr) & HCRE)) {
1411 outb_command(0, dev->base_addr); /* send a spurious byte */
1412 timeout = jiffies + 5*HZ/100;
1413 while (time_before(jiffies, timeout) && !(inb_status(dev->base_addr) & HCRE));
1414 if (inb_status(dev->base_addr) & HCRE)
1415 okay = 1;
1416 }
1417 if (!okay) {
1418 /* Nope, it's ignoring the command register. This means that
1419 * either it's still booting up, or it's died.
1420 */
1421 printk(KERN_ERR "%s: command register wouldn't drain, ", dev->name);
1422 if ((inb_status(dev->base_addr) & 7) == 3) {
1423 /* If the adapter status is 3, it *could* still be booting.
1424 * Give it the benefit of the doubt for 10 seconds.
1425 */
1426 printk("assuming 3c505 still starting\n");
1427 timeout = jiffies + 10*HZ;
1428 while (time_before(jiffies, timeout) && (inb_status(dev->base_addr) & 7));
1429 if (inb_status(dev->base_addr) & 7) {
1430 printk(KERN_ERR "%s: 3c505 failed to start\n", dev->name);
1431 } else {
1432 okay = 1; /* It started */
1433 }
1434 } else {
1435 /* Otherwise, it must just be in a strange
1436 * state. We probably need to kick it.
1437 */
1438 printk("3c505 is sulking\n");
1439 }
1440 }
1441 for (tries = 0; tries < 5 && okay; tries++) {
1442
1443 /*
1444 * Try to set the Ethernet address, to make sure that the board
1445 * is working.
1446 */
1447 adapter->tx_pcb.command = CMD_STATION_ADDRESS;
1448 adapter->tx_pcb.length = 0;
1449 cookie = probe_irq_on();
1450 if (!send_pcb(dev, &adapter->tx_pcb)) {
1451 printk(KERN_ERR "%s: could not send first PCB\n", dev->name);
1452 probe_irq_off(cookie);
1453 continue;
1454 }
1455 if (!receive_pcb(dev, &adapter->rx_pcb)) {
1456 printk(KERN_ERR "%s: could not read first PCB\n", dev->name);
1457 probe_irq_off(cookie);
1458 continue;
1459 }
1460 if ((adapter->rx_pcb.command != CMD_ADDRESS_RESPONSE) ||
1461 (adapter->rx_pcb.length != 6)) {
1462 printk(KERN_ERR "%s: first PCB wrong (%d, %d)\n", dev->name, adapter->rx_pcb.command, adapter->rx_pcb.length);
1463 probe_irq_off(cookie);
1464 continue;
1465 }
1466 goto okay;
1467 }
1468 /* It's broken. Do a hard reset to re-initialise the board,
1469 * and try again.
1470 */
1471 printk(KERN_INFO "%s: resetting adapter\n", dev->name);
1472 outb_control(adapter->hcr_val | FLSH | ATTN, dev);
1473 outb_control(adapter->hcr_val & ~(FLSH | ATTN), dev);
1474 }
1475 printk(KERN_ERR "%s: failed to initialise 3c505\n", dev->name);
1476 goto out;
1477
1478 okay:
1479 if (dev->irq) { /* Is there a preset IRQ? */
1480 int rpt = probe_irq_off(cookie);
1481 if (dev->irq != rpt) {
1482 printk(KERN_WARNING "%s: warning, irq %d configured but %d detected\n", dev->name, dev->irq, rpt);
1483 }
1484 /* if dev->irq == probe_irq_off(cookie), all is well */
1485 } else /* No preset IRQ; just use what we can detect */
1486 dev->irq = probe_irq_off(cookie);
1487 switch (dev->irq) { /* Legal, sane? */
1488 case 0:
1489 printk(KERN_ERR "%s: IRQ probe failed: check 3c505 jumpers.\n",
1490 dev->name);
1491 goto out;
1492 case 1:
1493 case 6:
1494 case 8:
1495 case 13:
1496 printk(KERN_ERR "%s: Impossible IRQ %d reported by probe_irq_off().\n",
1497 dev->name, dev->irq);
1498 goto out;
1499 }
1500 /*
1501 * Now we have the IRQ number so we can disable the interrupts from
1502 * the board until the board is opened.
1503 */
1504 outb_control(adapter->hcr_val & ~CMDE, dev);
1505
1506 /*
1507 * copy Ethernet address into structure
1508 */
1509 for (i = 0; i < 6; i++)
1510 dev->dev_addr[i] = adapter->rx_pcb.data.eth_addr[i];
1511
1512 /* find a DMA channel */
1513 if (!dev->dma) {
1514 if (dev->mem_start) {
1515 dev->dma = dev->mem_start & 7;
1516 }
1517 else {
1518 printk(KERN_WARNING "%s: warning, DMA channel not specified, using default\n", dev->name);
1519 dev->dma = ELP_DMA;
1520 }
1521 }
1522
1523 /*
1524 * print remainder of startup message
1525 */
Joe Perches0795af52007-10-03 17:59:30 -07001526 printk(KERN_INFO "%s: 3c505 at %#lx, irq %d, dma %d, "
Johannes Berge1749612008-10-27 15:59:26 -07001527 "addr %pM, ",
Joe Perches0795af52007-10-03 17:59:30 -07001528 dev->name, dev->base_addr, dev->irq, dev->dma,
Johannes Berge1749612008-10-27 15:59:26 -07001529 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 /*
1532 * read more information from the adapter
1533 */
1534
1535 adapter->tx_pcb.command = CMD_ADAPTER_INFO;
1536 adapter->tx_pcb.length = 0;
1537 if (!send_pcb(dev, &adapter->tx_pcb) ||
1538 !receive_pcb(dev, &adapter->rx_pcb) ||
1539 (adapter->rx_pcb.command != CMD_ADAPTER_INFO_RESPONSE) ||
1540 (adapter->rx_pcb.length != 10)) {
1541 printk("not responding to second PCB\n");
1542 }
1543 printk("rev %d.%d, %dk\n", adapter->rx_pcb.data.info.major_vers, adapter->rx_pcb.data.info.minor_vers, adapter->rx_pcb.data.info.RAM_sz);
1544
1545 /*
1546 * reconfigure the adapter memory to better suit our purposes
1547 */
1548 adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
1549 adapter->tx_pcb.length = 12;
1550 adapter->tx_pcb.data.memconf.cmd_q = 8;
1551 adapter->tx_pcb.data.memconf.rcv_q = 8;
1552 adapter->tx_pcb.data.memconf.mcast = 10;
1553 adapter->tx_pcb.data.memconf.frame = 10;
1554 adapter->tx_pcb.data.memconf.rcv_b = 10;
1555 adapter->tx_pcb.data.memconf.progs = 0;
1556 if (!send_pcb(dev, &adapter->tx_pcb) ||
1557 !receive_pcb(dev, &adapter->rx_pcb) ||
1558 (adapter->rx_pcb.command != CMD_CONFIGURE_ADAPTER_RESPONSE) ||
1559 (adapter->rx_pcb.length != 2)) {
1560 printk(KERN_ERR "%s: could not configure adapter memory\n", dev->name);
1561 }
1562 if (adapter->rx_pcb.data.configure) {
1563 printk(KERN_ERR "%s: adapter configuration failed\n", dev->name);
1564 }
1565
Stephen Hemmingere6c42b72009-01-09 13:01:12 +00001566 dev->netdev_ops = &elp_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 dev->watchdog_timeo = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 dev->ethtool_ops = &netdev_ethtool_ops; /* local */
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 dev->mem_start = dev->mem_end = 0;
1571
1572 err = register_netdev(dev);
1573 if (err)
1574 goto out;
1575
1576 return 0;
1577out:
1578 release_region(dev->base_addr, ELP_IO_EXTENT);
1579 return err;
1580}
1581
1582#ifndef MODULE
1583struct net_device * __init elplus_probe(int unit)
1584{
1585 struct net_device *dev = alloc_etherdev(sizeof(elp_device));
1586 int err;
1587 if (!dev)
1588 return ERR_PTR(-ENOMEM);
1589
1590 sprintf(dev->name, "eth%d", unit);
1591 netdev_boot_setup_check(dev);
1592
1593 err = elplus_setup(dev);
1594 if (err) {
1595 free_netdev(dev);
1596 return ERR_PTR(err);
1597 }
1598 return dev;
1599}
1600
1601#else
1602static struct net_device *dev_3c505[ELP_MAX_CARDS];
1603static int io[ELP_MAX_CARDS];
1604static int irq[ELP_MAX_CARDS];
1605static int dma[ELP_MAX_CARDS];
1606module_param_array(io, int, NULL, 0);
1607module_param_array(irq, int, NULL, 0);
1608module_param_array(dma, int, NULL, 0);
1609MODULE_PARM_DESC(io, "EtherLink Plus I/O base address(es)");
1610MODULE_PARM_DESC(irq, "EtherLink Plus IRQ number(s) (assigned)");
1611MODULE_PARM_DESC(dma, "EtherLink Plus DMA channel(s)");
1612
Randy Dunlap96e672c2006-06-10 13:33:48 -07001613int __init init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
1615 int this_dev, found = 0;
1616
1617 for (this_dev = 0; this_dev < ELP_MAX_CARDS; this_dev++) {
1618 struct net_device *dev = alloc_etherdev(sizeof(elp_device));
1619 if (!dev)
1620 break;
1621
1622 dev->irq = irq[this_dev];
1623 dev->base_addr = io[this_dev];
1624 if (dma[this_dev]) {
1625 dev->dma = dma[this_dev];
1626 } else {
1627 dev->dma = ELP_DMA;
1628 printk(KERN_WARNING "3c505.c: warning, using default DMA channel,\n");
1629 }
1630 if (io[this_dev] == 0) {
1631 if (this_dev) {
1632 free_netdev(dev);
1633 break;
1634 }
1635 printk(KERN_NOTICE "3c505.c: module autoprobe not recommended, give io=xx.\n");
1636 }
1637 if (elplus_setup(dev) != 0) {
1638 printk(KERN_WARNING "3c505.c: Failed to register card at 0x%x.\n", io[this_dev]);
1639 free_netdev(dev);
1640 break;
1641 }
1642 dev_3c505[this_dev] = dev;
1643 found++;
1644 }
1645 if (!found)
1646 return -ENODEV;
1647 return 0;
1648}
1649
Al Viroafc8eb42006-06-14 18:50:53 -04001650void __exit cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
1652 int this_dev;
1653
1654 for (this_dev = 0; this_dev < ELP_MAX_CARDS; this_dev++) {
1655 struct net_device *dev = dev_3c505[this_dev];
1656 if (dev) {
1657 unregister_netdev(dev);
1658 release_region(dev->base_addr, ELP_IO_EXTENT);
1659 free_netdev(dev);
1660 }
1661 }
1662}
1663
1664#endif /* MODULE */
1665MODULE_LICENSE("GPL");