blob: 04b5bba19021c8c93f340b0aa814dc93e3305a60 [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
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000129#define filename __FILE__
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000131#define timeout_msg "*** timeout at %s:%s (line %d) ***\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#define TIMEOUT_MSG(lineno) \
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000133 pr_notice(timeout_msg, filename, __func__, (lineno))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000135#define invalid_pcb_msg "*** invalid pcb length %d at %s:%s (line %d) ***\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#define INVALID_PCB_MSG(len) \
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000137 pr_notice(invalid_pcb_msg, (len), filename, __func__, __LINE__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000139#define search_msg "%s: Looking for 3c505 adapter at address %#x..."
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000141#define stilllooking_msg "still looking..."
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000143#define found_msg "found.\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000145#define notfound_msg "not found (reason = %d)\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000147#define couldnot_msg "%s: 3c505 not found\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149/*********************************************************
150 *
151 * various other debug stuff
152 *
153 *********************************************************/
154
155#ifdef ELP_DEBUG
156static int elp_debug = ELP_DEBUG;
157#else
158static int elp_debug;
159#endif
160#define debug elp_debug
161
162/*
163 * 0 = no messages (well, some)
164 * 1 = messages when high level commands performed
165 * 2 = messages when low level commands performed
166 * 3 = messages when interrupts received
167 */
168
169/*****************************************************************
170 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * List of I/O-addresses we try to auto-sense
172 * Last element MUST BE 0!
173 *****************************************************************/
174
175static int addr_list[] __initdata = {0x300, 0x280, 0x310, 0};
176
177/* Dma Memory related stuff */
178
179static unsigned long dma_mem_alloc(int size)
180{
181 int order = get_order(size);
182 return __get_dma_pages(GFP_KERNEL, order);
183}
184
185
186/*****************************************************************
187 *
188 * Functions for I/O (note the inline !)
189 *
190 *****************************************************************/
191
192static inline unsigned char inb_status(unsigned int base_addr)
193{
194 return inb(base_addr + PORT_STATUS);
195}
196
197static inline int inb_command(unsigned int base_addr)
198{
199 return inb(base_addr + PORT_COMMAND);
200}
201
202static inline void outb_control(unsigned char val, struct net_device *dev)
203{
204 outb(val, dev->base_addr + PORT_CONTROL);
Wang Chen454d7c92008-11-12 23:37:49 -0800205 ((elp_device *)(netdev_priv(dev)))->hcr_val = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Wang Chen454d7c92008-11-12 23:37:49 -0800208#define HCR_VAL(x) (((elp_device *)(netdev_priv(x)))->hcr_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210static inline void outb_command(unsigned char val, unsigned int base_addr)
211{
212 outb(val, base_addr + PORT_COMMAND);
213}
214
215static inline unsigned int backlog_next(unsigned int n)
216{
217 return (n + 1) % BACKLOG_SIZE;
218}
219
220/*****************************************************************
221 *
222 * useful functions for accessing the adapter
223 *
224 *****************************************************************/
225
226/*
227 * use this routine when accessing the ASF bits as they are
228 * changed asynchronously by the adapter
229 */
230
231/* get adapter PCB status */
232#define GET_ASF(addr) \
233 (get_status(addr)&ASF_PCB_MASK)
234
235static inline int get_status(unsigned int base_addr)
236{
237 unsigned long timeout = jiffies + 10*HZ/100;
238 register int stat1;
239 do {
240 stat1 = inb_status(base_addr);
241 } while (stat1 != inb_status(base_addr) && time_before(jiffies, timeout));
242 if (time_after_eq(jiffies, timeout))
243 TIMEOUT_MSG(__LINE__);
244 return stat1;
245}
246
247static inline void set_hsf(struct net_device *dev, int hsf)
248{
Wang Chen454d7c92008-11-12 23:37:49 -0800249 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 unsigned long flags;
251
252 spin_lock_irqsave(&adapter->lock, flags);
253 outb_control((HCR_VAL(dev) & ~HSF_PCB_MASK) | hsf, dev);
254 spin_unlock_irqrestore(&adapter->lock, flags);
255}
256
Richard Knutssondf570f92007-08-14 01:49:40 +0200257static bool start_receive(struct net_device *, pcb_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Jesper Juhl77933d72005-07-27 11:46:09 -0700259static inline void adapter_reset(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 unsigned long timeout;
Wang Chen454d7c92008-11-12 23:37:49 -0800262 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 unsigned char orig_hcr = adapter->hcr_val;
264
265 outb_control(0, dev);
266
267 if (inb_status(dev->base_addr) & ACRF) {
268 do {
269 inb_command(dev->base_addr);
270 timeout = jiffies + 2*HZ/100;
271 while (time_before_eq(jiffies, timeout) && !(inb_status(dev->base_addr) & ACRF));
272 } while (inb_status(dev->base_addr) & ACRF);
273 set_hsf(dev, HSF_PCB_NAK);
274 }
275 outb_control(adapter->hcr_val | ATTN | DIR, dev);
276 mdelay(10);
277 outb_control(adapter->hcr_val & ~ATTN, dev);
278 mdelay(10);
279 outb_control(adapter->hcr_val | FLSH, dev);
280 mdelay(10);
281 outb_control(adapter->hcr_val & ~FLSH, dev);
282 mdelay(10);
283
284 outb_control(orig_hcr, dev);
285 if (!start_receive(dev, &adapter->tx_pcb))
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000286 pr_err("%s: start receive command failed\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289/* Check to make sure that a DMA transfer hasn't timed out. This should
290 * never happen in theory, but seems to occur occasionally if the card gets
291 * prodded at the wrong time.
292 */
293static inline void check_3c505_dma(struct net_device *dev)
294{
Wang Chen454d7c92008-11-12 23:37:49 -0800295 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (adapter->dmaing && time_after(jiffies, adapter->current_dma.start_time + 10)) {
297 unsigned long flags, f;
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000298 pr_err("%s: DMA %s timed out, %d bytes left\n", dev->name,
299 adapter->current_dma.direction ? "download" : "upload",
300 get_dma_residue(dev->dma));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 spin_lock_irqsave(&adapter->lock, flags);
302 adapter->dmaing = 0;
303 adapter->busy = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 f=claim_dma_lock();
306 disable_dma(dev->dma);
307 release_dma_lock(f);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (adapter->rx_active)
310 adapter->rx_active--;
311 outb_control(adapter->hcr_val & ~(DMAE | TCEN | DIR), dev);
312 spin_unlock_irqrestore(&adapter->lock, flags);
313 }
314}
315
316/* Primitive functions used by send_pcb() */
Richard Knutssondf570f92007-08-14 01:49:40 +0200317static inline bool send_pcb_slow(unsigned int base_addr, unsigned char byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 unsigned long timeout;
320 outb_command(byte, base_addr);
321 for (timeout = jiffies + 5*HZ/100; time_before(jiffies, timeout);) {
322 if (inb_status(base_addr) & HCRE)
Richard Knutssondf570f92007-08-14 01:49:40 +0200323 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000325 pr_warning("3c505: send_pcb_slow timed out\n");
Richard Knutssondf570f92007-08-14 01:49:40 +0200326 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Richard Knutssondf570f92007-08-14 01:49:40 +0200329static inline bool send_pcb_fast(unsigned int base_addr, unsigned char byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 unsigned int timeout;
332 outb_command(byte, base_addr);
333 for (timeout = 0; timeout < 40000; timeout++) {
334 if (inb_status(base_addr) & HCRE)
Richard Knutssondf570f92007-08-14 01:49:40 +0200335 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000337 pr_warning("3c505: send_pcb_fast timed out\n");
Richard Knutssondf570f92007-08-14 01:49:40 +0200338 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341/* Check to see if the receiver needs restarting, and kick it if so */
342static inline void prime_rx(struct net_device *dev)
343{
Wang Chen454d7c92008-11-12 23:37:49 -0800344 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 while (adapter->rx_active < ELP_RX_PCBS && netif_running(dev)) {
346 if (!start_receive(dev, &adapter->itx_pcb))
347 break;
348 }
349}
350
351/*****************************************************************
352 *
353 * send_pcb
354 * Send a PCB to the adapter.
355 *
356 * output byte to command reg --<--+
357 * wait until HCRE is non zero |
358 * loop until all bytes sent -->--+
359 * set HSF1 and HSF2 to 1
360 * output pcb length
361 * wait until ASF give ACK or NAK
362 * set HSF1 and HSF2 to 0
363 *
364 *****************************************************************/
365
366/* This can be quite slow -- the adapter is allowed to take up to 40ms
367 * to respond to the initial interrupt.
368 *
369 * We run initially with interrupts turned on, but with a semaphore set
370 * so that nobody tries to re-enter this code. Once the first byte has
371 * gone through, we turn interrupts off and then send the others (the
372 * timeout is reduced to 500us).
373 */
374
Richard Knutssondf570f92007-08-14 01:49:40 +0200375static bool send_pcb(struct net_device *dev, pcb_struct * pcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 int i;
378 unsigned long timeout;
Wang Chen454d7c92008-11-12 23:37:49 -0800379 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 unsigned long flags;
381
382 check_3c505_dma(dev);
383
384 if (adapter->dmaing && adapter->current_dma.direction == 0)
Richard Knutssondf570f92007-08-14 01:49:40 +0200385 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 /* Avoid contention */
388 if (test_and_set_bit(1, &adapter->send_pcb_semaphore)) {
389 if (elp_debug >= 3) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000390 pr_debug("%s: send_pcb entered while threaded\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
Richard Knutssondf570f92007-08-14 01:49:40 +0200392 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394 /*
395 * load each byte into the command register and
396 * wait for the HCRE bit to indicate the adapter
397 * had read the byte
398 */
399 set_hsf(dev, 0);
400
401 if (send_pcb_slow(dev->base_addr, pcb->command))
402 goto abort;
403
404 spin_lock_irqsave(&adapter->lock, flags);
405
406 if (send_pcb_fast(dev->base_addr, pcb->length))
407 goto sti_abort;
408
409 for (i = 0; i < pcb->length; i++) {
410 if (send_pcb_fast(dev->base_addr, pcb->data.raw[i]))
411 goto sti_abort;
412 }
413
414 outb_control(adapter->hcr_val | 3, dev); /* signal end of PCB */
415 outb_command(2 + pcb->length, dev->base_addr);
416
417 /* now wait for the acknowledgement */
418 spin_unlock_irqrestore(&adapter->lock, flags);
419
420 for (timeout = jiffies + 5*HZ/100; time_before(jiffies, timeout);) {
421 switch (GET_ASF(dev->base_addr)) {
422 case ASF_PCB_ACK:
423 adapter->send_pcb_semaphore = 0;
Richard Knutssondf570f92007-08-14 01:49:40 +0200424 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 case ASF_PCB_NAK:
427#ifdef ELP_DEBUG
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000428 pr_debug("%s: send_pcb got NAK\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429#endif
430 goto abort;
431 }
432 }
433
434 if (elp_debug >= 1)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000435 pr_debug("%s: timeout waiting for PCB acknowledge (status %02x)\n",
436 dev->name, inb_status(dev->base_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 goto abort;
438
439 sti_abort:
440 spin_unlock_irqrestore(&adapter->lock, flags);
441 abort:
442 adapter->send_pcb_semaphore = 0;
Richard Knutssondf570f92007-08-14 01:49:40 +0200443 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446
447/*****************************************************************
448 *
449 * receive_pcb
450 * Read a PCB from the adapter
451 *
452 * wait for ACRF to be non-zero ---<---+
453 * input a byte |
454 * if ASF1 and ASF2 were not both one |
455 * before byte was read, loop --->---+
456 * set HSF1 and HSF2 for ack
457 *
458 *****************************************************************/
459
Richard Knutssondf570f92007-08-14 01:49:40 +0200460static bool receive_pcb(struct net_device *dev, pcb_struct * pcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 int i, j;
463 int total_length;
464 int stat;
465 unsigned long timeout;
466 unsigned long flags;
467
Wang Chen454d7c92008-11-12 23:37:49 -0800468 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 set_hsf(dev, 0);
471
472 /* get the command code */
473 timeout = jiffies + 2*HZ/100;
474 while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && time_before(jiffies, timeout));
475 if (time_after_eq(jiffies, timeout)) {
476 TIMEOUT_MSG(__LINE__);
Richard Knutssondf570f92007-08-14 01:49:40 +0200477 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479 pcb->command = inb_command(dev->base_addr);
480
481 /* read the data length */
482 timeout = jiffies + 3*HZ/100;
483 while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && time_before(jiffies, timeout));
484 if (time_after_eq(jiffies, timeout)) {
485 TIMEOUT_MSG(__LINE__);
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000486 pr_info("%s: status %02x\n", dev->name, stat);
Richard Knutssondf570f92007-08-14 01:49:40 +0200487 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489 pcb->length = inb_command(dev->base_addr);
490
491 if (pcb->length > MAX_PCB_DATA) {
492 INVALID_PCB_MSG(pcb->length);
493 adapter_reset(dev);
Richard Knutssondf570f92007-08-14 01:49:40 +0200494 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496 /* read the data */
497 spin_lock_irqsave(&adapter->lock, flags);
Roel Kluin501aa062009-02-12 16:52:31 -0800498 for (i = 0; i < MAX_PCB_DATA; i++) {
499 for (j = 0; j < 20000; j++) {
500 stat = get_status(dev->base_addr);
501 if (stat & ACRF)
502 break;
503 }
504 pcb->data.raw[i] = inb_command(dev->base_addr);
505 if ((stat & ASF_PCB_MASK) == ASF_PCB_END || j >= 20000)
506 break;
507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 spin_unlock_irqrestore(&adapter->lock, flags);
Roel Kluin501aa062009-02-12 16:52:31 -0800509 if (i >= MAX_PCB_DATA) {
510 INVALID_PCB_MSG(i);
511 return false;
512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (j >= 20000) {
514 TIMEOUT_MSG(__LINE__);
Richard Knutssondf570f92007-08-14 01:49:40 +0200515 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
Roel Kluin501aa062009-02-12 16:52:31 -0800517 /* the last "data" byte was really the length! */
518 total_length = pcb->data.raw[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 /* safety check total length vs data length */
521 if (total_length != (pcb->length + 2)) {
522 if (elp_debug >= 2)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000523 pr_warning("%s: mangled PCB received\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 set_hsf(dev, HSF_PCB_NAK);
Richard Knutssondf570f92007-08-14 01:49:40 +0200525 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527
528 if (pcb->command == CMD_RECEIVE_PACKET_COMPLETE) {
529 if (test_and_set_bit(0, (void *) &adapter->busy)) {
530 if (backlog_next(adapter->rx_backlog.in) == adapter->rx_backlog.out) {
531 set_hsf(dev, HSF_PCB_NAK);
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000532 pr_warning("%s: PCB rejected, transfer in progress and backlog full\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 pcb->command = 0;
Richard Knutssondf570f92007-08-14 01:49:40 +0200534 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 } else {
536 pcb->command = 0xff;
537 }
538 }
539 }
540 set_hsf(dev, HSF_PCB_ACK);
Richard Knutssondf570f92007-08-14 01:49:40 +0200541 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544/******************************************************
545 *
546 * queue a receive command on the adapter so we will get an
547 * interrupt when a packet is received.
548 *
549 ******************************************************/
550
Richard Knutssondf570f92007-08-14 01:49:40 +0200551static bool start_receive(struct net_device *dev, pcb_struct * tx_pcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Richard Knutssondf570f92007-08-14 01:49:40 +0200553 bool status;
Wang Chen454d7c92008-11-12 23:37:49 -0800554 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 if (elp_debug >= 3)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000557 pr_debug("%s: restarting receiver\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 tx_pcb->command = CMD_RECEIVE_PACKET;
559 tx_pcb->length = sizeof(struct Rcv_pkt);
560 tx_pcb->data.rcv_pkt.buf_seg
561 = tx_pcb->data.rcv_pkt.buf_ofs = 0; /* Unused */
562 tx_pcb->data.rcv_pkt.buf_len = 1600;
563 tx_pcb->data.rcv_pkt.timeout = 0; /* set timeout to zero */
564 status = send_pcb(dev, tx_pcb);
565 if (status)
566 adapter->rx_active++;
567 return status;
568}
569
570/******************************************************
571 *
572 * extract a packet from the adapter
573 * this routine is only called from within the interrupt
574 * service routine, so no cli/sti calls are needed
575 * note that the length is always assumed to be even
576 *
577 ******************************************************/
578
579static void receive_packet(struct net_device *dev, int len)
580{
581 int rlen;
Wang Chen454d7c92008-11-12 23:37:49 -0800582 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 void *target;
584 struct sk_buff *skb;
585 unsigned long flags;
586
587 rlen = (len + 1) & ~1;
588 skb = dev_alloc_skb(rlen + 2);
589
590 if (!skb) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000591 pr_warning("%s: memory squeeze, dropping packet\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 target = adapter->dma_buffer;
593 adapter->current_dma.target = NULL;
594 /* FIXME: stats */
595 return;
596 }
597
598 skb_reserve(skb, 2);
599 target = skb_put(skb, rlen);
600 if ((unsigned long)(target + rlen) >= MAX_DMA_ADDRESS) {
601 adapter->current_dma.target = target;
602 target = adapter->dma_buffer;
603 } else {
604 adapter->current_dma.target = NULL;
605 }
606
607 /* if this happens, we die */
608 if (test_and_set_bit(0, (void *) &adapter->dmaing))
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000609 pr_err("%s: rx blocked, DMA in progress, dir %d\n",
610 dev->name, adapter->current_dma.direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 adapter->current_dma.direction = 0;
613 adapter->current_dma.length = rlen;
614 adapter->current_dma.skb = skb;
615 adapter->current_dma.start_time = jiffies;
616
617 outb_control(adapter->hcr_val | DIR | TCEN | DMAE, dev);
618
619 flags=claim_dma_lock();
620 disable_dma(dev->dma);
621 clear_dma_ff(dev->dma);
622 set_dma_mode(dev->dma, 0x04); /* dma read */
623 set_dma_addr(dev->dma, isa_virt_to_bus(target));
624 set_dma_count(dev->dma, rlen);
625 enable_dma(dev->dma);
626 release_dma_lock(flags);
627
628 if (elp_debug >= 3) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000629 pr_debug("%s: rx DMA transfer started\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631
632 if (adapter->rx_active)
633 adapter->rx_active--;
634
635 if (!adapter->busy)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000636 pr_warning("%s: receive_packet called, busy not set.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
639/******************************************************
640 *
641 * interrupt handler
642 *
643 ******************************************************/
644
David Howells7d12e782006-10-05 14:55:46 +0100645static irqreturn_t elp_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 int len;
648 int dlen;
649 int icount = 0;
Wang Chen454d7c92008-11-12 23:37:49 -0800650 struct net_device *dev = dev_id;
651 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 unsigned long timeout;
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 spin_lock(&adapter->lock);
655
656 do {
657 /*
658 * has a DMA transfer finished?
659 */
660 if (inb_status(dev->base_addr) & DONE) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000661 if (!adapter->dmaing)
662 pr_warning("%s: phantom DMA completed\n", dev->name);
663
664 if (elp_debug >= 3)
665 pr_debug("%s: %s DMA complete, status %02x\n", dev->name,
666 adapter->current_dma.direction ? "tx" : "rx",
667 inb_status(dev->base_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 outb_control(adapter->hcr_val & ~(DMAE | TCEN | DIR), dev);
670 if (adapter->current_dma.direction) {
671 dev_kfree_skb_irq(adapter->current_dma.skb);
672 } else {
673 struct sk_buff *skb = adapter->current_dma.skb;
674 if (skb) {
675 if (adapter->current_dma.target) {
676 /* have already done the skb_put() */
677 memcpy(adapter->current_dma.target, adapter->dma_buffer, adapter->current_dma.length);
678 }
679 skb->protocol = eth_type_trans(skb,dev);
Paulius Zaleckasba0f6ca2008-04-29 02:27:37 +0300680 dev->stats.rx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683 }
684 adapter->dmaing = 0;
685 if (adapter->rx_backlog.in != adapter->rx_backlog.out) {
686 int t = adapter->rx_backlog.length[adapter->rx_backlog.out];
687 adapter->rx_backlog.out = backlog_next(adapter->rx_backlog.out);
688 if (elp_debug >= 2)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000689 pr_debug("%s: receiving backlogged packet (%d)\n", dev->name, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 receive_packet(dev, t);
691 } else {
692 adapter->busy = 0;
693 }
694 } else {
695 /* has one timed out? */
696 check_3c505_dma(dev);
697 }
698
699 /*
700 * receive a PCB from the adapter
701 */
702 timeout = jiffies + 3*HZ/100;
703 while ((inb_status(dev->base_addr) & ACRF) != 0 && time_before(jiffies, timeout)) {
704 if (receive_pcb(dev, &adapter->irx_pcb)) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400705 switch (adapter->irx_pcb.command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 {
707 case 0:
708 break;
709 /*
710 * received a packet - this must be handled fast
711 */
712 case 0xff:
713 case CMD_RECEIVE_PACKET_COMPLETE:
714 /* if the device isn't open, don't pass packets up the stack */
715 if (!netif_running(dev))
716 break;
717 len = adapter->irx_pcb.data.rcv_resp.pkt_len;
718 dlen = adapter->irx_pcb.data.rcv_resp.buf_len;
719 if (adapter->irx_pcb.data.rcv_resp.timeout != 0) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000720 pr_err("%s: interrupt - packet not received correctly\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 } else {
722 if (elp_debug >= 3) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000723 pr_debug("%s: interrupt - packet received of length %i (%i)\n",
724 dev->name, len, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726 if (adapter->irx_pcb.command == 0xff) {
727 if (elp_debug >= 2)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000728 pr_debug("%s: adding packet to backlog (len = %d)\n",
729 dev->name, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 adapter->rx_backlog.length[adapter->rx_backlog.in] = dlen;
731 adapter->rx_backlog.in = backlog_next(adapter->rx_backlog.in);
732 } else {
733 receive_packet(dev, dlen);
734 }
735 if (elp_debug >= 3)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000736 pr_debug("%s: packet received\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738 break;
739
740 /*
741 * 82586 configured correctly
742 */
743 case CMD_CONFIGURE_82586_RESPONSE:
744 adapter->got[CMD_CONFIGURE_82586] = 1;
745 if (elp_debug >= 3)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000746 pr_debug("%s: interrupt - configure response received\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 break;
748
749 /*
750 * Adapter memory configuration
751 */
752 case CMD_CONFIGURE_ADAPTER_RESPONSE:
753 adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] = 1;
754 if (elp_debug >= 3)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000755 pr_debug("%s: Adapter memory configuration %s.\n", dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 adapter->irx_pcb.data.failed ? "failed" : "succeeded");
757 break;
758
759 /*
760 * Multicast list loading
761 */
762 case CMD_LOAD_MULTICAST_RESPONSE:
763 adapter->got[CMD_LOAD_MULTICAST_LIST] = 1;
764 if (elp_debug >= 3)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000765 pr_debug("%s: Multicast address list loading %s.\n", dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 adapter->irx_pcb.data.failed ? "failed" : "succeeded");
767 break;
768
769 /*
770 * Station address setting
771 */
772 case CMD_SET_ADDRESS_RESPONSE:
773 adapter->got[CMD_SET_STATION_ADDRESS] = 1;
774 if (elp_debug >= 3)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000775 pr_debug("%s: Ethernet address setting %s.\n", dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 adapter->irx_pcb.data.failed ? "failed" : "succeeded");
777 break;
778
779
780 /*
781 * received board statistics
782 */
783 case CMD_NETWORK_STATISTICS_RESPONSE:
Paulius Zaleckasba0f6ca2008-04-29 02:27:37 +0300784 dev->stats.rx_packets += adapter->irx_pcb.data.netstat.tot_recv;
785 dev->stats.tx_packets += adapter->irx_pcb.data.netstat.tot_xmit;
786 dev->stats.rx_crc_errors += adapter->irx_pcb.data.netstat.err_CRC;
787 dev->stats.rx_frame_errors += adapter->irx_pcb.data.netstat.err_align;
788 dev->stats.rx_fifo_errors += adapter->irx_pcb.data.netstat.err_ovrrun;
789 dev->stats.rx_over_errors += adapter->irx_pcb.data.netstat.err_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 adapter->got[CMD_NETWORK_STATISTICS] = 1;
791 if (elp_debug >= 3)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000792 pr_debug("%s: interrupt - statistics response received\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 break;
794
795 /*
796 * sent a packet
797 */
798 case CMD_TRANSMIT_PACKET_COMPLETE:
799 if (elp_debug >= 3)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000800 pr_debug("%s: interrupt - packet sent\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (!netif_running(dev))
802 break;
803 switch (adapter->irx_pcb.data.xmit_resp.c_stat) {
804 case 0xffff:
Paulius Zaleckasba0f6ca2008-04-29 02:27:37 +0300805 dev->stats.tx_aborted_errors++;
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000806 pr_info("%s: transmit timed out, network cable problem?\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 break;
808 case 0xfffe:
Paulius Zaleckasba0f6ca2008-04-29 02:27:37 +0300809 dev->stats.tx_fifo_errors++;
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000810 pr_info("%s: transmit timed out, FIFO underrun\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 break;
812 }
813 netif_wake_queue(dev);
814 break;
815
816 /*
817 * some unknown PCB
818 */
819 default:
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000820 pr_debug("%s: unknown PCB received - %2.2x\n",
821 dev->name, adapter->irx_pcb.command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 break;
823 }
824 } else {
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000825 pr_warning("%s: failed to read PCB on interrupt\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 adapter_reset(dev);
827 }
828 }
829
830 } while (icount++ < 5 && (inb_status(dev->base_addr) & (ACRF | DONE)));
831
832 prime_rx(dev);
833
834 /*
835 * indicate no longer in interrupt routine
836 */
837 spin_unlock(&adapter->lock);
838 return IRQ_HANDLED;
839}
840
841
842/******************************************************
843 *
844 * open the board
845 *
846 ******************************************************/
847
848static int elp_open(struct net_device *dev)
849{
Wang Chen454d7c92008-11-12 23:37:49 -0800850 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 int retval;
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (elp_debug >= 3)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000854 pr_debug("%s: request to open device\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 /*
857 * make sure we actually found the device
858 */
859 if (adapter == NULL) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000860 pr_err("%s: Opening a non-existent physical device\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return -EAGAIN;
862 }
863 /*
864 * disable interrupts on the board
865 */
866 outb_control(0, dev);
867
868 /*
869 * clear any pending interrupts
870 */
871 inb_command(dev->base_addr);
872 adapter_reset(dev);
873
874 /*
875 * no receive PCBs active
876 */
877 adapter->rx_active = 0;
878
879 adapter->busy = 0;
880 adapter->send_pcb_semaphore = 0;
881 adapter->rx_backlog.in = 0;
882 adapter->rx_backlog.out = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 spin_lock_init(&adapter->lock);
885
886 /*
887 * install our interrupt service routine
888 */
Joe Perchesa0607fd2009-11-18 23:29:17 -0800889 if ((retval = request_irq(dev->irq, elp_interrupt, 0, dev->name, dev))) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000890 pr_err("%s: could not allocate IRQ%d\n", dev->name, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return retval;
892 }
893 if ((retval = request_dma(dev->dma, dev->name))) {
894 free_irq(dev->irq, dev);
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000895 pr_err("%s: could not allocate DMA%d channel\n", dev->name, dev->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return retval;
897 }
898 adapter->dma_buffer = (void *) dma_mem_alloc(DMA_BUFFER_SIZE);
899 if (!adapter->dma_buffer) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000900 pr_err("%s: could not allocate DMA buffer\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 free_dma(dev->dma);
902 free_irq(dev->irq, dev);
903 return -ENOMEM;
904 }
905 adapter->dmaing = 0;
906
907 /*
908 * enable interrupts on the board
909 */
910 outb_control(CMDE, dev);
911
912 /*
913 * configure adapter memory: we need 10 multicast addresses, default==0
914 */
915 if (elp_debug >= 3)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000916 pr_debug("%s: sending 3c505 memory configuration command\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
918 adapter->tx_pcb.data.memconf.cmd_q = 10;
919 adapter->tx_pcb.data.memconf.rcv_q = 20;
920 adapter->tx_pcb.data.memconf.mcast = 10;
921 adapter->tx_pcb.data.memconf.frame = 20;
922 adapter->tx_pcb.data.memconf.rcv_b = 20;
923 adapter->tx_pcb.data.memconf.progs = 0;
924 adapter->tx_pcb.length = sizeof(struct Memconf);
925 adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] = 0;
926 if (!send_pcb(dev, &adapter->tx_pcb))
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000927 pr_err("%s: couldn't send memory configuration command\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 else {
929 unsigned long timeout = jiffies + TIMEOUT;
930 while (adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] == 0 && time_before(jiffies, timeout));
931 if (time_after_eq(jiffies, timeout))
932 TIMEOUT_MSG(__LINE__);
933 }
934
935
936 /*
937 * configure adapter to receive broadcast messages and wait for response
938 */
939 if (elp_debug >= 3)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000940 pr_debug("%s: sending 82586 configure command\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 adapter->tx_pcb.command = CMD_CONFIGURE_82586;
942 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
943 adapter->tx_pcb.length = 2;
944 adapter->got[CMD_CONFIGURE_82586] = 0;
945 if (!send_pcb(dev, &adapter->tx_pcb))
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000946 pr_err("%s: couldn't send 82586 configure command\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 else {
948 unsigned long timeout = jiffies + TIMEOUT;
949 while (adapter->got[CMD_CONFIGURE_82586] == 0 && time_before(jiffies, timeout));
950 if (time_after_eq(jiffies, timeout))
951 TIMEOUT_MSG(__LINE__);
952 }
953
954 /* enable burst-mode DMA */
955 /* outb(0x1, dev->base_addr + PORT_AUXDMA); */
956
957 /*
958 * queue receive commands to provide buffering
959 */
960 prime_rx(dev);
961 if (elp_debug >= 3)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000962 pr_debug("%s: %d receive PCBs active\n", dev->name, adapter->rx_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 /*
965 * device is now officially open!
966 */
967
968 netif_start_queue(dev);
969 return 0;
970}
971
972
973/******************************************************
974 *
975 * send a packet to the adapter
976 *
977 ******************************************************/
978
Stephen Hemminger27a1de92009-08-31 19:50:54 +0000979static netdev_tx_t send_packet(struct net_device *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Wang Chen454d7c92008-11-12 23:37:49 -0800981 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 unsigned long target;
983 unsigned long flags;
984
985 /*
986 * make sure the length is even and no shorter than 60 bytes
987 */
988 unsigned int nlen = (((skb->len < 60) ? 60 : skb->len) + 1) & (~1);
989
990 if (test_and_set_bit(0, (void *) &adapter->busy)) {
991 if (elp_debug >= 2)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000992 pr_debug("%s: transmit blocked\n", dev->name);
Richard Knutssondf570f92007-08-14 01:49:40 +0200993 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995
Paulius Zaleckasba0f6ca2008-04-29 02:27:37 +0300996 dev->stats.tx_bytes += nlen;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 /*
999 * send the adapter a transmit packet command. Ignore segment and offset
1000 * and make sure the length is even
1001 */
1002 adapter->tx_pcb.command = CMD_TRANSMIT_PACKET;
1003 adapter->tx_pcb.length = sizeof(struct Xmit_pkt);
1004 adapter->tx_pcb.data.xmit_pkt.buf_ofs
1005 = adapter->tx_pcb.data.xmit_pkt.buf_seg = 0; /* Unused */
1006 adapter->tx_pcb.data.xmit_pkt.pkt_len = nlen;
1007
1008 if (!send_pcb(dev, &adapter->tx_pcb)) {
1009 adapter->busy = 0;
Richard Knutssondf570f92007-08-14 01:49:40 +02001010 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012 /* if this happens, we die */
1013 if (test_and_set_bit(0, (void *) &adapter->dmaing))
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001014 pr_debug("%s: tx: DMA %d in progress\n", dev->name, adapter->current_dma.direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 adapter->current_dma.direction = 1;
1017 adapter->current_dma.start_time = jiffies;
1018
1019 if ((unsigned long)(skb->data + nlen) >= MAX_DMA_ADDRESS || nlen != skb->len) {
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001020 skb_copy_from_linear_data(skb, adapter->dma_buffer, nlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 memset(adapter->dma_buffer+skb->len, 0, nlen-skb->len);
1022 target = isa_virt_to_bus(adapter->dma_buffer);
1023 }
1024 else {
1025 target = isa_virt_to_bus(skb->data);
1026 }
1027 adapter->current_dma.skb = skb;
1028
1029 flags=claim_dma_lock();
1030 disable_dma(dev->dma);
1031 clear_dma_ff(dev->dma);
1032 set_dma_mode(dev->dma, 0x48); /* dma memory -> io */
1033 set_dma_addr(dev->dma, target);
1034 set_dma_count(dev->dma, nlen);
1035 outb_control(adapter->hcr_val | DMAE | TCEN, dev);
1036 enable_dma(dev->dma);
1037 release_dma_lock(flags);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (elp_debug >= 3)
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001040 pr_debug("%s: DMA transfer started\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Richard Knutssondf570f92007-08-14 01:49:40 +02001042 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
1045/*
1046 * The upper layer thinks we timed out
1047 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049static void elp_timeout(struct net_device *dev)
1050{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 int stat;
1052
1053 stat = inb_status(dev->base_addr);
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001054 pr_warning("%s: transmit timed out, lost %s?\n", dev->name,
1055 (stat & ACRF) ? "interrupt" : "command");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (elp_debug >= 1)
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001057 pr_debug("%s: status %#02x\n", dev->name, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 dev->trans_start = jiffies;
Paulius Zaleckasba0f6ca2008-04-29 02:27:37 +03001059 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 netif_wake_queue(dev);
1061}
1062
1063/******************************************************
1064 *
1065 * start the transmitter
1066 * return 0 if sent OK, else return 1
1067 *
1068 ******************************************************/
1069
Stephen Hemminger27a1de92009-08-31 19:50:54 +00001070static netdev_tx_t elp_start_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
1072 unsigned long flags;
Wang Chen454d7c92008-11-12 23:37:49 -08001073 elp_device *adapter = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 spin_lock_irqsave(&adapter->lock, flags);
1076 check_3c505_dma(dev);
1077
1078 if (elp_debug >= 3)
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001079 pr_debug("%s: request to send packet of length %d\n", dev->name, (int) skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 netif_stop_queue(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /*
1084 * send the packet at skb->data for skb->len
1085 */
1086 if (!send_packet(dev, skb)) {
1087 if (elp_debug >= 2) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001088 pr_debug("%s: failed to transmit packet\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090 spin_unlock_irqrestore(&adapter->lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +00001091 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093 if (elp_debug >= 3)
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001094 pr_debug("%s: packet of length %d sent\n", dev->name, (int) skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 /*
1097 * start the transmit timeout
1098 */
1099 dev->trans_start = jiffies;
1100
1101 prime_rx(dev);
1102 spin_unlock_irqrestore(&adapter->lock, flags);
1103 netif_start_queue(dev);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001104 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
1107/******************************************************
1108 *
1109 * return statistics on the board
1110 *
1111 ******************************************************/
1112
1113static struct net_device_stats *elp_get_stats(struct net_device *dev)
1114{
Wang Chen454d7c92008-11-12 23:37:49 -08001115 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 if (elp_debug >= 3)
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001118 pr_debug("%s: request for stats\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 /* If the device is closed, just return the latest stats we have,
1121 - we cannot ask from the adapter without interrupts */
1122 if (!netif_running(dev))
Paulius Zaleckasba0f6ca2008-04-29 02:27:37 +03001123 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 /* send a get statistics command to the board */
1126 adapter->tx_pcb.command = CMD_NETWORK_STATISTICS;
1127 adapter->tx_pcb.length = 0;
1128 adapter->got[CMD_NETWORK_STATISTICS] = 0;
1129 if (!send_pcb(dev, &adapter->tx_pcb))
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001130 pr_err("%s: couldn't send get statistics command\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 else {
1132 unsigned long timeout = jiffies + TIMEOUT;
1133 while (adapter->got[CMD_NETWORK_STATISTICS] == 0 && time_before(jiffies, timeout));
1134 if (time_after_eq(jiffies, timeout)) {
1135 TIMEOUT_MSG(__LINE__);
Paulius Zaleckasba0f6ca2008-04-29 02:27:37 +03001136 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138 }
1139
1140 /* statistics are now up to date */
Paulius Zaleckasba0f6ca2008-04-29 02:27:37 +03001141 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
1144
1145static void netdev_get_drvinfo(struct net_device *dev,
1146 struct ethtool_drvinfo *info)
1147{
1148 strcpy(info->driver, DRV_NAME);
1149 strcpy(info->version, DRV_VERSION);
1150 sprintf(info->bus_info, "ISA 0x%lx", dev->base_addr);
1151}
1152
1153static u32 netdev_get_msglevel(struct net_device *dev)
1154{
1155 return debug;
1156}
1157
1158static void netdev_set_msglevel(struct net_device *dev, u32 level)
1159{
1160 debug = level;
1161}
1162
Jeff Garzik7282d492006-09-13 14:30:00 -04001163static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 .get_drvinfo = netdev_get_drvinfo,
1165 .get_msglevel = netdev_get_msglevel,
1166 .set_msglevel = netdev_set_msglevel,
1167};
1168
1169/******************************************************
1170 *
1171 * close the board
1172 *
1173 ******************************************************/
1174
1175static int elp_close(struct net_device *dev)
1176{
Wang Chen454d7c92008-11-12 23:37:49 -08001177 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 if (elp_debug >= 3)
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001180 pr_debug("%s: request to close device\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 netif_stop_queue(dev);
1183
1184 /* Someone may request the device statistic information even when
1185 * the interface is closed. The following will update the statistics
1186 * structure in the driver, so we'll be able to give current statistics.
1187 */
1188 (void) elp_get_stats(dev);
1189
1190 /*
1191 * disable interrupts on the board
1192 */
1193 outb_control(0, dev);
1194
1195 /*
1196 * release the IRQ
1197 */
1198 free_irq(dev->irq, dev);
1199
1200 free_dma(dev->dma);
1201 free_pages((unsigned long) adapter->dma_buffer, get_order(DMA_BUFFER_SIZE));
1202
1203 return 0;
1204}
1205
1206
1207/************************************************************
1208 *
1209 * Set multicast list
1210 * num_addrs==0: clear mc_list
1211 * num_addrs==-1: set promiscuous mode
1212 * num_addrs>0: set mc_list
1213 *
1214 ************************************************************/
1215
1216static void elp_set_mc_list(struct net_device *dev)
1217{
Wang Chen454d7c92008-11-12 23:37:49 -08001218 elp_device *adapter = netdev_priv(dev);
Jiri Pirko59ce25d2010-02-17 23:12:15 +00001219 struct dev_mc_list *dmi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 int i;
1221 unsigned long flags;
1222
1223 if (elp_debug >= 3)
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001224 pr_debug("%s: request to set multicast list\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 spin_lock_irqsave(&adapter->lock, flags);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (!(dev->flags & (IFF_PROMISC | IFF_ALLMULTI))) {
1229 /* send a "load multicast list" command to the board, max 10 addrs/cmd */
1230 /* if num_addrs==0 the list will be cleared */
1231 adapter->tx_pcb.command = CMD_LOAD_MULTICAST_LIST;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001232 adapter->tx_pcb.length = 6 * netdev_mc_count(dev);
Jiri Pirko59ce25d2010-02-17 23:12:15 +00001233 i = 0;
1234 netdev_for_each_mc_addr(dmi, dev)
1235 memcpy(adapter->tx_pcb.data.multicast[i++], dmi->dmi_addr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 adapter->got[CMD_LOAD_MULTICAST_LIST] = 0;
1237 if (!send_pcb(dev, &adapter->tx_pcb))
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001238 pr_err("%s: couldn't send set_multicast command\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 else {
1240 unsigned long timeout = jiffies + TIMEOUT;
1241 while (adapter->got[CMD_LOAD_MULTICAST_LIST] == 0 && time_before(jiffies, timeout));
1242 if (time_after_eq(jiffies, timeout)) {
1243 TIMEOUT_MSG(__LINE__);
1244 }
1245 }
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001246 if (!netdev_mc_empty(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD | RECV_MULTI;
1248 else /* num_addrs == 0 */
1249 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
1250 } else
1251 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_PROMISC;
1252 /*
1253 * configure adapter to receive messages (as specified above)
1254 * and wait for response
1255 */
1256 if (elp_debug >= 3)
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001257 pr_debug("%s: sending 82586 configure command\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 adapter->tx_pcb.command = CMD_CONFIGURE_82586;
1259 adapter->tx_pcb.length = 2;
1260 adapter->got[CMD_CONFIGURE_82586] = 0;
1261 if (!send_pcb(dev, &adapter->tx_pcb))
1262 {
1263 spin_unlock_irqrestore(&adapter->lock, flags);
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001264 pr_err("%s: couldn't send 82586 configure command\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266 else {
1267 unsigned long timeout = jiffies + TIMEOUT;
1268 spin_unlock_irqrestore(&adapter->lock, flags);
1269 while (adapter->got[CMD_CONFIGURE_82586] == 0 && time_before(jiffies, timeout));
1270 if (time_after_eq(jiffies, timeout))
1271 TIMEOUT_MSG(__LINE__);
1272 }
1273}
1274
1275/************************************************************
1276 *
1277 * A couple of tests to see if there's 3C505 or not
1278 * Called only by elp_autodetect
1279 ************************************************************/
1280
1281static int __init elp_sense(struct net_device *dev)
1282{
1283 int addr = dev->base_addr;
1284 const char *name = dev->name;
1285 byte orig_HSR;
1286
1287 if (!request_region(addr, ELP_IO_EXTENT, "3c505"))
1288 return -ENODEV;
1289
1290 orig_HSR = inb_status(addr);
1291
1292 if (elp_debug > 0)
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001293 pr_debug(search_msg, name, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 if (orig_HSR == 0xff) {
1296 if (elp_debug > 0)
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001297 pr_cont(notfound_msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 goto out;
1299 }
1300
1301 /* Wait for a while; the adapter may still be booting up */
1302 if (elp_debug > 0)
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001303 pr_cont(stilllooking_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 if (orig_HSR & DIR) {
1306 /* If HCR.DIR is up, we pull it down. HSR.DIR should follow. */
1307 outb(0, dev->base_addr + PORT_CONTROL);
Domen Puncerc56943e2005-05-12 22:21:51 -04001308 msleep(300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if (inb_status(addr) & DIR) {
1310 if (elp_debug > 0)
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001311 pr_cont(notfound_msg, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 goto out;
1313 }
1314 } else {
1315 /* If HCR.DIR is down, we pull it up. HSR.DIR should follow. */
1316 outb(DIR, dev->base_addr + PORT_CONTROL);
Domen Puncerc56943e2005-05-12 22:21:51 -04001317 msleep(300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (!(inb_status(addr) & DIR)) {
1319 if (elp_debug > 0)
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001320 pr_cont(notfound_msg, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 goto out;
1322 }
1323 }
1324 /*
1325 * It certainly looks like a 3c505.
1326 */
1327 if (elp_debug > 0)
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001328 pr_cont(found_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 return 0;
1331out:
1332 release_region(addr, ELP_IO_EXTENT);
1333 return -ENODEV;
1334}
1335
1336/*************************************************************
1337 *
1338 * Search through addr_list[] and try to find a 3C505
1339 * Called only by eplus_probe
1340 *************************************************************/
1341
1342static int __init elp_autodetect(struct net_device *dev)
1343{
1344 int idx = 0;
1345
1346 /* if base address set, then only check that address
1347 otherwise, run through the table */
1348 if (dev->base_addr != 0) { /* dev->base_addr == 0 ==> plain autodetect */
1349 if (elp_sense(dev) == 0)
1350 return dev->base_addr;
1351 } else
1352 while ((dev->base_addr = addr_list[idx++])) {
1353 if (elp_sense(dev) == 0)
1354 return dev->base_addr;
1355 }
1356
1357 /* could not find an adapter */
1358 if (elp_debug > 0)
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001359 pr_debug(couldnot_msg, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 return 0; /* Because of this, the layer above will return -ENODEV */
1362}
1363
Stephen Hemmingere6c42b72009-01-09 13:01:12 +00001364static const struct net_device_ops elp_netdev_ops = {
1365 .ndo_open = elp_open,
1366 .ndo_stop = elp_close,
1367 .ndo_get_stats = elp_get_stats,
1368 .ndo_start_xmit = elp_start_xmit,
1369 .ndo_tx_timeout = elp_timeout,
1370 .ndo_set_multicast_list = elp_set_mc_list,
1371 .ndo_change_mtu = eth_change_mtu,
1372 .ndo_set_mac_address = eth_mac_addr,
1373 .ndo_validate_addr = eth_validate_addr,
1374};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376/******************************************************
1377 *
1378 * probe for an Etherlink Plus board at the specified address
1379 *
1380 ******************************************************/
1381
1382/* There are three situations we need to be able to detect here:
1383
1384 * a) the card is idle
1385 * b) the card is still booting up
1386 * c) the card is stuck in a strange state (some DOS drivers do this)
1387 *
1388 * In case (a), all is well. In case (b), we wait 10 seconds to see if the
1389 * card finishes booting, and carry on if so. In case (c), we do a hard reset,
1390 * loop round, and hope for the best.
1391 *
1392 * This is all very unpleasant, but hopefully avoids the problems with the old
1393 * probe code (which had a 15-second delay if the card was idle, and didn't
1394 * work at all if it was in a weird state).
1395 */
1396
1397static int __init elplus_setup(struct net_device *dev)
1398{
Wang Chen454d7c92008-11-12 23:37:49 -08001399 elp_device *adapter = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 int i, tries, tries1, okay;
1401 unsigned long timeout;
1402 unsigned long cookie = 0;
1403 int err = -ENODEV;
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 /*
1406 * setup adapter structure
1407 */
1408
1409 dev->base_addr = elp_autodetect(dev);
1410 if (!dev->base_addr)
1411 return -ENODEV;
1412
1413 adapter->send_pcb_semaphore = 0;
1414
1415 for (tries1 = 0; tries1 < 3; tries1++) {
1416 outb_control((adapter->hcr_val | CMDE) & ~DIR, dev);
1417 /* First try to write just one byte, to see if the card is
1418 * responding at all normally.
1419 */
1420 timeout = jiffies + 5*HZ/100;
1421 okay = 0;
1422 while (time_before(jiffies, timeout) && !(inb_status(dev->base_addr) & HCRE));
1423 if ((inb_status(dev->base_addr) & HCRE)) {
1424 outb_command(0, dev->base_addr); /* send a spurious byte */
1425 timeout = jiffies + 5*HZ/100;
1426 while (time_before(jiffies, timeout) && !(inb_status(dev->base_addr) & HCRE));
1427 if (inb_status(dev->base_addr) & HCRE)
1428 okay = 1;
1429 }
1430 if (!okay) {
1431 /* Nope, it's ignoring the command register. This means that
1432 * either it's still booting up, or it's died.
1433 */
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001434 pr_err("%s: command register wouldn't drain, ", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if ((inb_status(dev->base_addr) & 7) == 3) {
1436 /* If the adapter status is 3, it *could* still be booting.
1437 * Give it the benefit of the doubt for 10 seconds.
1438 */
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001439 pr_cont("assuming 3c505 still starting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 timeout = jiffies + 10*HZ;
1441 while (time_before(jiffies, timeout) && (inb_status(dev->base_addr) & 7));
1442 if (inb_status(dev->base_addr) & 7) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001443 pr_err("%s: 3c505 failed to start\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 } else {
1445 okay = 1; /* It started */
1446 }
1447 } else {
1448 /* Otherwise, it must just be in a strange
1449 * state. We probably need to kick it.
1450 */
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001451 pr_cont("3c505 is sulking\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453 }
1454 for (tries = 0; tries < 5 && okay; tries++) {
1455
1456 /*
1457 * Try to set the Ethernet address, to make sure that the board
1458 * is working.
1459 */
1460 adapter->tx_pcb.command = CMD_STATION_ADDRESS;
1461 adapter->tx_pcb.length = 0;
1462 cookie = probe_irq_on();
1463 if (!send_pcb(dev, &adapter->tx_pcb)) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001464 pr_err("%s: could not send first PCB\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 probe_irq_off(cookie);
1466 continue;
1467 }
1468 if (!receive_pcb(dev, &adapter->rx_pcb)) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001469 pr_err("%s: could not read first PCB\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 probe_irq_off(cookie);
1471 continue;
1472 }
1473 if ((adapter->rx_pcb.command != CMD_ADDRESS_RESPONSE) ||
1474 (adapter->rx_pcb.length != 6)) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001475 pr_err("%s: first PCB wrong (%d, %d)\n", dev->name,
1476 adapter->rx_pcb.command, adapter->rx_pcb.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 probe_irq_off(cookie);
1478 continue;
1479 }
1480 goto okay;
1481 }
1482 /* It's broken. Do a hard reset to re-initialise the board,
1483 * and try again.
1484 */
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001485 pr_info("%s: resetting adapter\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 outb_control(adapter->hcr_val | FLSH | ATTN, dev);
1487 outb_control(adapter->hcr_val & ~(FLSH | ATTN), dev);
1488 }
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001489 pr_err("%s: failed to initialise 3c505\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 goto out;
1491
1492 okay:
1493 if (dev->irq) { /* Is there a preset IRQ? */
1494 int rpt = probe_irq_off(cookie);
1495 if (dev->irq != rpt) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001496 pr_warning("%s: warning, irq %d configured but %d detected\n", dev->name, dev->irq, rpt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
1498 /* if dev->irq == probe_irq_off(cookie), all is well */
1499 } else /* No preset IRQ; just use what we can detect */
1500 dev->irq = probe_irq_off(cookie);
1501 switch (dev->irq) { /* Legal, sane? */
1502 case 0:
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001503 pr_err("%s: IRQ probe failed: check 3c505 jumpers.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 dev->name);
1505 goto out;
1506 case 1:
1507 case 6:
1508 case 8:
1509 case 13:
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001510 pr_err("%s: Impossible IRQ %d reported by probe_irq_off().\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 dev->name, dev->irq);
1512 goto out;
1513 }
1514 /*
1515 * Now we have the IRQ number so we can disable the interrupts from
1516 * the board until the board is opened.
1517 */
1518 outb_control(adapter->hcr_val & ~CMDE, dev);
1519
1520 /*
1521 * copy Ethernet address into structure
1522 */
1523 for (i = 0; i < 6; i++)
1524 dev->dev_addr[i] = adapter->rx_pcb.data.eth_addr[i];
1525
1526 /* find a DMA channel */
1527 if (!dev->dma) {
1528 if (dev->mem_start) {
1529 dev->dma = dev->mem_start & 7;
1530 }
1531 else {
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001532 pr_warning("%s: warning, DMA channel not specified, using default\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 dev->dma = ELP_DMA;
1534 }
1535 }
1536
1537 /*
1538 * print remainder of startup message
1539 */
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001540 pr_info("%s: 3c505 at %#lx, irq %d, dma %d, addr %pM, ",
1541 dev->name, dev->base_addr, dev->irq, dev->dma, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 /*
1543 * read more information from the adapter
1544 */
1545
1546 adapter->tx_pcb.command = CMD_ADAPTER_INFO;
1547 adapter->tx_pcb.length = 0;
1548 if (!send_pcb(dev, &adapter->tx_pcb) ||
1549 !receive_pcb(dev, &adapter->rx_pcb) ||
1550 (adapter->rx_pcb.command != CMD_ADAPTER_INFO_RESPONSE) ||
1551 (adapter->rx_pcb.length != 10)) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001552 pr_cont("not responding to second PCB\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 }
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001554 pr_cont("rev %d.%d, %dk\n", adapter->rx_pcb.data.info.major_vers,
1555 adapter->rx_pcb.data.info.minor_vers, adapter->rx_pcb.data.info.RAM_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 /*
1558 * reconfigure the adapter memory to better suit our purposes
1559 */
1560 adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
1561 adapter->tx_pcb.length = 12;
1562 adapter->tx_pcb.data.memconf.cmd_q = 8;
1563 adapter->tx_pcb.data.memconf.rcv_q = 8;
1564 adapter->tx_pcb.data.memconf.mcast = 10;
1565 adapter->tx_pcb.data.memconf.frame = 10;
1566 adapter->tx_pcb.data.memconf.rcv_b = 10;
1567 adapter->tx_pcb.data.memconf.progs = 0;
1568 if (!send_pcb(dev, &adapter->tx_pcb) ||
1569 !receive_pcb(dev, &adapter->rx_pcb) ||
1570 (adapter->rx_pcb.command != CMD_CONFIGURE_ADAPTER_RESPONSE) ||
1571 (adapter->rx_pcb.length != 2)) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001572 pr_err("%s: could not configure adapter memory\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574 if (adapter->rx_pcb.data.configure) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001575 pr_err("%s: adapter configuration failed\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
1577
Stephen Hemmingere6c42b72009-01-09 13:01:12 +00001578 dev->netdev_ops = &elp_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 dev->watchdog_timeo = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 dev->ethtool_ops = &netdev_ethtool_ops; /* local */
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 dev->mem_start = dev->mem_end = 0;
1583
1584 err = register_netdev(dev);
1585 if (err)
1586 goto out;
1587
1588 return 0;
1589out:
1590 release_region(dev->base_addr, ELP_IO_EXTENT);
1591 return err;
1592}
1593
1594#ifndef MODULE
1595struct net_device * __init elplus_probe(int unit)
1596{
1597 struct net_device *dev = alloc_etherdev(sizeof(elp_device));
1598 int err;
1599 if (!dev)
1600 return ERR_PTR(-ENOMEM);
1601
1602 sprintf(dev->name, "eth%d", unit);
1603 netdev_boot_setup_check(dev);
1604
1605 err = elplus_setup(dev);
1606 if (err) {
1607 free_netdev(dev);
1608 return ERR_PTR(err);
1609 }
1610 return dev;
1611}
1612
1613#else
1614static struct net_device *dev_3c505[ELP_MAX_CARDS];
1615static int io[ELP_MAX_CARDS];
1616static int irq[ELP_MAX_CARDS];
1617static int dma[ELP_MAX_CARDS];
1618module_param_array(io, int, NULL, 0);
1619module_param_array(irq, int, NULL, 0);
1620module_param_array(dma, int, NULL, 0);
1621MODULE_PARM_DESC(io, "EtherLink Plus I/O base address(es)");
1622MODULE_PARM_DESC(irq, "EtherLink Plus IRQ number(s) (assigned)");
1623MODULE_PARM_DESC(dma, "EtherLink Plus DMA channel(s)");
1624
Randy Dunlap96e672c2006-06-10 13:33:48 -07001625int __init init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
1627 int this_dev, found = 0;
1628
1629 for (this_dev = 0; this_dev < ELP_MAX_CARDS; this_dev++) {
1630 struct net_device *dev = alloc_etherdev(sizeof(elp_device));
1631 if (!dev)
1632 break;
1633
1634 dev->irq = irq[this_dev];
1635 dev->base_addr = io[this_dev];
1636 if (dma[this_dev]) {
1637 dev->dma = dma[this_dev];
1638 } else {
1639 dev->dma = ELP_DMA;
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001640 pr_warning("3c505.c: warning, using default DMA channel,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
1642 if (io[this_dev] == 0) {
1643 if (this_dev) {
1644 free_netdev(dev);
1645 break;
1646 }
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001647 pr_notice("3c505.c: module autoprobe not recommended, give io=xx.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
1649 if (elplus_setup(dev) != 0) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +00001650 pr_warning("3c505.c: Failed to register card at 0x%x.\n", io[this_dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 free_netdev(dev);
1652 break;
1653 }
1654 dev_3c505[this_dev] = dev;
1655 found++;
1656 }
1657 if (!found)
1658 return -ENODEV;
1659 return 0;
1660}
1661
Al Viroafc8eb42006-06-14 18:50:53 -04001662void __exit cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
1664 int this_dev;
1665
1666 for (this_dev = 0; this_dev < ELP_MAX_CARDS; this_dev++) {
1667 struct net_device *dev = dev_3c505[this_dev];
1668 if (dev) {
1669 unregister_netdev(dev);
1670 release_region(dev->base_addr, ELP_IO_EXTENT);
1671 free_netdev(dev);
1672 }
1673 }
1674}
1675
1676#endif /* MODULE */
1677MODULE_LICENSE("GPL");