blob: 3d94797c8f9ba6efd583d5fe7060c64db981e0b8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* at1700.c: A network device driver for the Allied Telesis AT1700.
2
3 Written 1993-98 by Donald Becker.
4
5 Copyright 1993 United States Government as represented by the
6 Director, National Security Agency.
7
8 This software may be used and distributed according to the terms
9 of the GNU General Public License, incorporated herein by reference.
10
11 The author may be reached as becker@scyld.com, or C/O
12 Scyld Computing Corporation
13 410 Severn Ave., Suite 210
14 Annapolis MD 21403
15
16 This is a device driver for the Allied Telesis AT1700, and
17 Fujitsu FMV-181/182/181A/182A/183/184/183A/184A, which are
18 straight-forward Fujitsu MB86965 implementations.
19
20 Modification for Fujitsu FMV-18X cards is done by Yutaka Tamiya
Jeff Garzik6aa20a22006-09-13 13:24:59 -040021 (tamy@flab.fujitsu.co.jp).
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23 Sources:
24 The Fujitsu MB86965 datasheet.
25
26 After the initial version of this driver was written Gerry Sawkins of
27 ATI provided their EEPROM configuration code header file.
28 Thanks to NIIBE Yutaka <gniibe@mri.co.jp> for bug fixes.
29
30 MCA bus (AT1720) support by Rene Schmit <rene@bss.lu>
31
32 Bugs:
33 The MB86965 has a design flaw that makes all probes unreliable. Not
34 only is it difficult to detect, it also moves around in I/O space in
35 response to inb()s from other device probes!
36*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/errno.h>
39#include <linux/netdevice.h>
40#include <linux/etherdevice.h>
41#include <linux/mca-legacy.h>
42#include <linux/module.h>
43#include <linux/kernel.h>
44#include <linux/types.h>
45#include <linux/fcntl.h>
46#include <linux/interrupt.h>
47#include <linux/ioport.h>
48#include <linux/in.h>
49#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/string.h>
51#include <linux/init.h>
52#include <linux/crc32.h>
53#include <linux/bitops.h>
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/io.h>
56#include <asm/dma.h>
57
58static char version[] __initdata =
Andy Gospodarekd5b20692006-09-11 17:39:18 -040059 "at1700.c:v1.16 9/11/06 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61#define DRV_NAME "at1700"
62
63/* Tunable parameters. */
64
65/* When to switch from the 64-entry multicast filter to Rx-all-multicast. */
66#define MC_FILTERBREAK 64
67
68/* These unusual address orders are used to verify the CONFIG register. */
69
70static int fmv18x_probe_list[] __initdata = {
71 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x300, 0x340, 0
72};
73
74/*
75 * ISA
76 */
77
78static unsigned at1700_probe_list[] __initdata = {
79 0x260, 0x280, 0x2a0, 0x240, 0x340, 0x320, 0x380, 0x300, 0
80};
81
82/*
83 * MCA
84 */
85#ifdef CONFIG_MCA_LEGACY
86static int at1700_ioaddr_pattern[] __initdata = {
87 0x00, 0x04, 0x01, 0x05, 0x02, 0x06, 0x03, 0x07
88};
89
90static int at1700_mca_probe_list[] __initdata = {
91 0x400, 0x1400, 0x2400, 0x3400, 0x4400, 0x5400, 0x6400, 0x7400, 0
92};
93
94static int at1700_irq_pattern[] __initdata = {
95 0x00, 0x00, 0x00, 0x30, 0x70, 0xb0, 0x00, 0x00,
96 0x00, 0xf0, 0x34, 0x74, 0xb4, 0x00, 0x00, 0xf4, 0x00
97};
98#endif
99
100/* use 0 for production, 1 for verification, >2 for debug */
101#ifndef NET_DEBUG
102#define NET_DEBUG 1
103#endif
104static unsigned int net_debug = NET_DEBUG;
105
106typedef unsigned char uchar;
107
108/* Information that need to be kept for each board. */
109struct net_local {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 spinlock_t lock;
111 unsigned char mc_filter[8];
112 uint jumpered:1; /* Set iff the board has jumper config. */
113 uint tx_started:1; /* Packets are on the Tx queue. */
114 uint tx_queue_ready:1; /* Tx queue is ready to be sent. */
115 uint rx_started:1; /* Packets are Rxing. */
116 uchar tx_queue; /* Number of packet on the Tx queue. */
117 char mca_slot; /* -1 means ISA */
118 ushort tx_queue_len; /* Current length of the Tx queue. */
119};
120
121
122/* Offsets from the base address. */
123#define STATUS 0
124#define TX_STATUS 0
125#define RX_STATUS 1
126#define TX_INTR 2 /* Bit-mapped interrupt enable registers. */
127#define RX_INTR 3
128#define TX_MODE 4
129#define RX_MODE 5
130#define CONFIG_0 6 /* Misc. configuration settings. */
131#define CONFIG_1 7
132/* Run-time register bank 2 definitions. */
133#define DATAPORT 8 /* Word-wide DMA or programmed-I/O dataport. */
134#define TX_START 10
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300135#define COL16CNTL 11 /* Control Reg for 16 collisions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#define MODE13 13
137#define RX_CTRL 14
138/* Configuration registers only on the '865A/B chips. */
139#define EEPROM_Ctrl 16
140#define EEPROM_Data 17
141#define CARDSTATUS 16 /* FMV-18x Card Status */
142#define CARDSTATUS1 17 /* FMV-18x Card Status */
143#define IOCONFIG 18 /* Either read the jumper, or move the I/O. */
144#define IOCONFIG1 19
145#define SAPROM 20 /* The station address PROM, if no EEPROM. */
146#define MODE24 24
147#define RESET 31 /* Write to reset some parts of the chip. */
148#define AT1700_IO_EXTENT 32
149#define PORT_OFFSET(o) (o)
150
151
Eric Dumazetc63fdf42010-11-03 22:49:35 +0000152#define TX_TIMEOUT (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154
155/* Index to functions, as function prototypes. */
156
157static int at1700_probe1(struct net_device *dev, int ioaddr);
158static int read_eeprom(long ioaddr, int location);
159static int net_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000160static netdev_tx_t net_send_packet(struct sk_buff *skb,
161 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100162static irqreturn_t net_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163static void net_rx(struct net_device *dev);
164static int net_close(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static void set_rx_mode(struct net_device *dev);
166static void net_tx_timeout (struct net_device *dev);
167
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169#ifdef CONFIG_MCA_LEGACY
170struct at1720_mca_adapters_struct {
171 char* name;
172 int id;
173};
174/* rEnE : maybe there are others I don't know off... */
175
176static struct at1720_mca_adapters_struct at1720_mca_adapters[] __initdata = {
177 { "Allied Telesys AT1720AT", 0x6410 },
178 { "Allied Telesys AT1720BT", 0x6413 },
179 { "Allied Telesys AT1720T", 0x6416 },
180 { NULL, 0 },
181};
182#endif
183
184/* Check for a network adaptor of this type, and return '0' iff one exists.
185 If dev->base_addr == 0, probe all likely locations.
186 If dev->base_addr == 1, always return failure.
187 If dev->base_addr == 2, allocate space for the device and return success
188 (detachable devices only).
189 */
190
191static int io = 0x260;
192
193static int irq;
194
195static void cleanup_card(struct net_device *dev)
196{
197#ifdef CONFIG_MCA_LEGACY
198 struct net_local *lp = netdev_priv(dev);
199 if (lp->mca_slot >= 0)
200 mca_mark_as_unused(lp->mca_slot);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400201#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 free_irq(dev->irq, NULL);
203 release_region(dev->base_addr, AT1700_IO_EXTENT);
204}
205
206struct net_device * __init at1700_probe(int unit)
207{
208 struct net_device *dev = alloc_etherdev(sizeof(struct net_local));
209 unsigned *port;
210 int err = 0;
211
212 if (!dev)
213 return ERR_PTR(-ENODEV);
214
215 if (unit >= 0) {
216 sprintf(dev->name, "eth%d", unit);
217 netdev_boot_setup_check(dev);
218 io = dev->base_addr;
219 irq = dev->irq;
220 } else {
221 dev->base_addr = io;
222 dev->irq = irq;
223 }
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (io > 0x1ff) { /* Check a single specified location. */
226 err = at1700_probe1(dev, io);
227 } else if (io != 0) { /* Don't probe at all. */
228 err = -ENXIO;
229 } else {
230 for (port = at1700_probe_list; *port; port++) {
231 if (at1700_probe1(dev, *port) == 0)
232 break;
233 dev->irq = irq;
234 }
235 if (!*port)
236 err = -ENODEV;
237 }
238 if (err)
239 goto out;
240 err = register_netdev(dev);
241 if (err)
242 goto out1;
243 return dev;
244out1:
245 cleanup_card(dev);
246out:
247 free_netdev(dev);
248 return ERR_PTR(err);
249}
250
Stephen Hemmingercb0c7002009-03-26 15:11:36 +0000251static const struct net_device_ops at1700_netdev_ops = {
252 .ndo_open = net_open,
253 .ndo_stop = net_close,
254 .ndo_start_xmit = net_send_packet,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000255 .ndo_set_rx_mode = set_rx_mode,
Stephen Hemmingercb0c7002009-03-26 15:11:36 +0000256 .ndo_tx_timeout = net_tx_timeout,
257 .ndo_change_mtu = eth_change_mtu,
258 .ndo_set_mac_address = eth_mac_addr,
259 .ndo_validate_addr = eth_validate_addr,
260};
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262/* The Fujitsu datasheet suggests that the NIC be probed for by checking its
263 "signature", the default bit pattern after a reset. This *doesn't* work --
264 there is no way to reset the bus interface without a complete power-cycle!
265
266 It turns out that ATI came to the same conclusion I did: the only thing
267 that can be done is checking a few bits and then diving right into an
268 EEPROM read. */
269
270static int __init at1700_probe1(struct net_device *dev, int ioaddr)
271{
Joe Perchesb6bc7652010-12-21 02:16:08 -0800272 static const char fmv_irqmap[4] = {3, 7, 10, 15};
273 static const char fmv_irqmap_pnp[8] = {3, 4, 5, 7, 9, 10, 11, 15};
274 static const char at1700_irqmap[8] = {3, 4, 5, 9, 10, 11, 14, 15};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 unsigned int i, irq, is_fmv18x = 0, is_at1700 = 0;
276 int slot, ret = -ENODEV;
277 struct net_local *lp = netdev_priv(dev);
278
279 if (!request_region(ioaddr, AT1700_IO_EXTENT, DRV_NAME))
280 return -EBUSY;
281
282 /* Resetting the chip doesn't reset the ISA interface, so don't bother.
283 That means we have to be careful with the register values we probe
284 for.
285 */
286#ifdef notdef
287 printk("at1700 probe at %#x, eeprom is %4.4x %4.4x %4.4x ctrl %4.4x.\n",
288 ioaddr, read_eeprom(ioaddr, 4), read_eeprom(ioaddr, 5),
289 read_eeprom(ioaddr, 6), inw(ioaddr + EEPROM_Ctrl));
290#endif
291
292#ifdef CONFIG_MCA_LEGACY
293 /* rEnE (rene@bss.lu): got this from 3c509 driver source , adapted for AT1720 */
294
295 /* Based on Erik Nygren's (nygren@mit.edu) 3c529 patch, heavily
296 modified by Chris Beauregard (cpbeaure@csclub.uwaterloo.ca)
297 to support standard MCA probing. */
298
299 /* redone for multi-card detection by ZP Gu (zpg@castle.net) */
300 /* now works as a module */
301
302 if (MCA_bus) {
303 int j;
304 int l_i;
305 u_char pos3, pos4;
306
307 for (j = 0; at1720_mca_adapters[j].name != NULL; j ++) {
308 slot = 0;
309 while (slot != MCA_NOTFOUND) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 slot = mca_find_unused_adapter( at1720_mca_adapters[j].id, slot );
312 if (slot == MCA_NOTFOUND) break;
313
314 /* if we get this far, an adapter has been detected and is
315 enabled */
316
317 pos3 = mca_read_stored_pos( slot, 3 );
318 pos4 = mca_read_stored_pos( slot, 4 );
319
roel kluin3b73e792009-07-25 12:01:50 +0000320 for (l_i = 0; l_i < 8; l_i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (( pos3 & 0x07) == at1700_ioaddr_pattern[l_i])
322 break;
323 ioaddr = at1700_mca_probe_list[l_i];
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 for (irq = 0; irq < 0x10; irq++)
326 if (((((pos4>>4) & 0x0f) | (pos3 & 0xf0)) & 0xff) == at1700_irq_pattern[irq])
327 break;
328
329 /* probing for a card at a particular IO/IRQ */
330 if ((dev->irq && dev->irq != irq) ||
331 (dev->base_addr && dev->base_addr != ioaddr)) {
332 slot++; /* probing next slot */
333 continue;
334 }
335
336 dev->irq = irq;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 /* claim the slot */
339 mca_set_adapter_name( slot, at1720_mca_adapters[j].name );
340 mca_mark_as_used(slot);
341
342 goto found;
343 }
344 }
345 /* if we get here, we didn't find an MCA adapter - try ISA */
346 }
347#endif
348 slot = -1;
349 /* We must check for the EEPROM-config boards first, else accessing
350 IOCONFIG0 will move the board! */
Joe Perches8e95a202009-12-03 07:58:21 +0000351 if (at1700_probe_list[inb(ioaddr + IOCONFIG1) & 0x07] == ioaddr &&
352 read_eeprom(ioaddr, 4) == 0x0000 &&
353 (read_eeprom(ioaddr, 5) & 0xff00) == 0xF400)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 is_at1700 = 1;
Joe Perches8e95a202009-12-03 07:58:21 +0000355 else if (inb(ioaddr + SAPROM ) == 0x00 &&
356 inb(ioaddr + SAPROM + 1) == 0x00 &&
357 inb(ioaddr + SAPROM + 2) == 0x0e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 is_fmv18x = 1;
359 else {
360 goto err_out;
361 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363#ifdef CONFIG_MCA_LEGACY
364found:
365#endif
366
367 /* Reset the internal state machines. */
368 outb(0, ioaddr + RESET);
369
370 if (is_at1700) {
371 irq = at1700_irqmap[(read_eeprom(ioaddr, 12)&0x04)
372 | (read_eeprom(ioaddr, 0)>>14)];
373 } else {
374 /* Check PnP mode for FMV-183/184/183A/184A. */
375 /* This PnP routine is very poor. IO and IRQ should be known. */
376 if (inb(ioaddr + CARDSTATUS1) & 0x20) {
377 irq = dev->irq;
378 for (i = 0; i < 8; i++) {
379 if (irq == fmv_irqmap_pnp[i])
380 break;
381 }
382 if (i == 8) {
383 goto err_mca;
384 }
385 } else {
386 if (fmv18x_probe_list[inb(ioaddr + IOCONFIG) & 0x07] != ioaddr)
387 goto err_mca;
388 irq = fmv_irqmap[(inb(ioaddr + IOCONFIG)>>6) & 0x03];
389 }
390 }
391
392 printk("%s: %s found at %#3x, IRQ %d, address ", dev->name,
393 is_at1700 ? "AT1700" : "FMV-18X", ioaddr, irq);
394
395 dev->base_addr = ioaddr;
396 dev->irq = irq;
397
398 if (is_at1700) {
399 for(i = 0; i < 3; i++) {
400 unsigned short eeprom_val = read_eeprom(ioaddr, 4+i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 ((unsigned short *)dev->dev_addr)[i] = ntohs(eeprom_val);
402 }
403 } else {
404 for(i = 0; i < 6; i++) {
405 unsigned char val = inb(ioaddr + SAPROM + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 dev->dev_addr[i] = val;
407 }
408 }
Johannes Berge1749612008-10-27 15:59:26 -0700409 printk("%pM", dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 /* The EEPROM word 12 bit 0x0400 means use regular 100 ohm 10baseT signals,
412 rather than 150 ohm shielded twisted pair compensation.
413 0x0000 == auto-sense the interface
414 0x0800 == use TP interface
415 0x1800 == use coax interface
416 */
417 {
418 const char *porttype[] = {"auto-sense", "10baseT", "auto-sense", "10base2"};
419 if (is_at1700) {
420 ushort setup_value = read_eeprom(ioaddr, 12);
421 dev->if_port = setup_value >> 8;
422 } else {
423 ushort setup_value = inb(ioaddr + CARDSTATUS);
424 switch (setup_value & 0x07) {
425 case 0x01: /* 10base5 */
426 case 0x02: /* 10base2 */
427 dev->if_port = 0x18; break;
428 case 0x04: /* 10baseT */
429 dev->if_port = 0x08; break;
430 default: /* auto-sense */
431 dev->if_port = 0x00; break;
432 }
433 }
434 printk(" %s interface.\n", porttype[(dev->if_port>>3) & 3]);
435 }
436
437 /* Set the configuration register 0 to 32K 100ns. byte-wide memory, 16 bit
438 bus access, two 4K Tx queues, and disabled Tx and Rx. */
439 outb(0xda, ioaddr + CONFIG_0);
440
441 /* Set the station address in bank zero. */
442 outb(0x00, ioaddr + CONFIG_1);
443 for (i = 0; i < 6; i++)
444 outb(dev->dev_addr[i], ioaddr + PORT_OFFSET(8 + i));
445
446 /* Switch to bank 1 and set the multicast table to accept none. */
447 outb(0x04, ioaddr + CONFIG_1);
448 for (i = 0; i < 8; i++)
449 outb(0x00, ioaddr + PORT_OFFSET(8 + i));
450
451
452 /* Switch to bank 2 */
453 /* Lock our I/O address, and set manual processing mode for 16 collisions. */
454 outb(0x08, ioaddr + CONFIG_1);
455 outb(dev->if_port, ioaddr + MODE13);
456 outb(0x00, ioaddr + COL16CNTL);
457
458 if (net_debug)
459 printk(version);
460
Stephen Hemmingercb0c7002009-03-26 15:11:36 +0000461 dev->netdev_ops = &at1700_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 dev->watchdog_timeo = TX_TIMEOUT;
463
464 spin_lock_init(&lp->lock);
465
466 lp->jumpered = is_fmv18x;
467 lp->mca_slot = slot;
468 /* Snarf the interrupt vector now. */
Joe Perchesa0607fd2009-11-18 23:29:17 -0800469 ret = request_irq(irq, net_interrupt, 0, DRV_NAME, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (ret) {
Jeff Garzikcba05162007-11-23 21:50:34 -0500471 printk(KERN_ERR "AT1700 at %#3x is unusable due to a "
472 "conflict on IRQ %d.\n",
473 ioaddr, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 goto err_mca;
475 }
476
477 return 0;
478
479err_mca:
480#ifdef CONFIG_MCA_LEGACY
481 if (slot >= 0)
482 mca_mark_as_unused(slot);
483#endif
484err_out:
485 release_region(ioaddr, AT1700_IO_EXTENT);
486 return ret;
487}
488
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490/* EEPROM_Ctrl bits. */
491#define EE_SHIFT_CLK 0x40 /* EEPROM shift clock, in reg. 16. */
492#define EE_CS 0x20 /* EEPROM chip select, in reg. 16. */
493#define EE_DATA_WRITE 0x80 /* EEPROM chip data in, in reg. 17. */
494#define EE_DATA_READ 0x80 /* EEPROM chip data out, in reg. 17. */
495
496/* The EEPROM commands include the alway-set leading bit. */
497#define EE_WRITE_CMD (5 << 6)
498#define EE_READ_CMD (6 << 6)
499#define EE_ERASE_CMD (7 << 6)
500
501static int __init read_eeprom(long ioaddr, int location)
502{
503 int i;
504 unsigned short retval = 0;
505 long ee_addr = ioaddr + EEPROM_Ctrl;
506 long ee_daddr = ioaddr + EEPROM_Data;
507 int read_cmd = location | EE_READ_CMD;
508
509 /* Shift the read command bits out. */
510 for (i = 9; i >= 0; i--) {
511 short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
512 outb(EE_CS, ee_addr);
513 outb(dataval, ee_daddr);
514 outb(EE_CS | EE_SHIFT_CLK, ee_addr); /* EEPROM clock tick. */
515 }
516 outb(EE_DATA_WRITE, ee_daddr);
517 for (i = 16; i > 0; i--) {
518 outb(EE_CS, ee_addr);
519 outb(EE_CS | EE_SHIFT_CLK, ee_addr);
520 retval = (retval << 1) | ((inb(ee_daddr) & EE_DATA_READ) ? 1 : 0);
521 }
522
523 /* Terminate the EEPROM access. */
524 outb(EE_CS, ee_addr);
525 outb(EE_SHIFT_CLK, ee_addr);
526 outb(0, ee_addr);
527 return retval;
528}
529
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532static int net_open(struct net_device *dev)
533{
534 struct net_local *lp = netdev_priv(dev);
535 int ioaddr = dev->base_addr;
536
537 /* Set the configuration register 0 to 32K 100ns. byte-wide memory, 16 bit
538 bus access, and two 4K Tx queues. */
539 outb(0x5a, ioaddr + CONFIG_0);
540
541 /* Powerup, switch to register bank 2, and enable the Rx and Tx. */
542 outb(0xe8, ioaddr + CONFIG_1);
543
544 lp->tx_started = 0;
545 lp->tx_queue_ready = 1;
546 lp->rx_started = 0;
547 lp->tx_queue = 0;
548 lp->tx_queue_len = 0;
549
550 /* Turn on hardware Tx and Rx interrupts. */
551 outb(0x82, ioaddr + TX_INTR);
552 outb(0x81, ioaddr + RX_INTR);
553
554 /* Enable the IRQ on boards of fmv18x it is feasible. */
555 if (lp->jumpered) {
556 outb(0x80, ioaddr + IOCONFIG1);
557 }
558
559 netif_start_queue(dev);
560 return 0;
561}
562
563static void net_tx_timeout (struct net_device *dev)
564{
565 struct net_local *lp = netdev_priv(dev);
566 int ioaddr = dev->base_addr;
567
568 printk ("%s: transmit timed out with status %04x, %s?\n", dev->name,
569 inw (ioaddr + STATUS), inb (ioaddr + TX_STATUS) & 0x80
570 ? "IRQ conflict" : "network cable problem");
571 printk ("%s: timeout registers: %04x %04x %04x %04x %04x %04x %04x %04x.\n",
572 dev->name, inw(ioaddr + TX_STATUS), inw(ioaddr + TX_INTR), inw(ioaddr + TX_MODE),
573 inw(ioaddr + CONFIG_0), inw(ioaddr + DATAPORT), inw(ioaddr + TX_START),
574 inw(ioaddr + MODE13 - 1), inw(ioaddr + RX_CTRL));
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700575 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 /* ToDo: We should try to restart the adaptor... */
577 outw(0xffff, ioaddr + MODE24);
578 outw (0xffff, ioaddr + TX_STATUS);
579 outb (0x5a, ioaddr + CONFIG_0);
580 outb (0xe8, ioaddr + CONFIG_1);
581 outw (0x8182, ioaddr + TX_INTR);
582 outb (0x00, ioaddr + TX_START);
583 outb (0x03, ioaddr + COL16CNTL);
584
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700585 dev->trans_start = jiffies; /* prevent tx timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 lp->tx_started = 0;
588 lp->tx_queue_ready = 1;
589 lp->rx_started = 0;
590 lp->tx_queue = 0;
591 lp->tx_queue_len = 0;
592
593 netif_wake_queue(dev);
594}
595
596
Stephen Hemminger613573252009-08-31 19:50:58 +0000597static netdev_tx_t net_send_packet (struct sk_buff *skb,
598 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 struct net_local *lp = netdev_priv(dev);
601 int ioaddr = dev->base_addr;
602 short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
603 short len = skb->len;
604 unsigned char *buf = skb->data;
605 static u8 pad[ETH_ZLEN];
606
607 netif_stop_queue (dev);
608
609 /* We may not start transmitting unless we finish transferring
610 a packet into the Tx queue. During executing the following
611 codes we possibly catch a Tx interrupt. Thus we flag off
612 tx_queue_ready, so that we prevent the interrupt routine
613 (net_interrupt) to start transmitting. */
614 lp->tx_queue_ready = 0;
615 {
616 outw (length, ioaddr + DATAPORT);
617 /* Packet data */
618 outsw (ioaddr + DATAPORT, buf, len >> 1);
619 /* Check for dribble byte */
620 if (len & 1) {
621 outw(skb->data[skb->len-1], ioaddr + DATAPORT);
622 len++;
623 }
624 /* Check for packet padding */
625 if (length != skb->len)
626 outsw(ioaddr + DATAPORT, pad, (length - len + 1) >> 1);
627
628 lp->tx_queue++;
629 lp->tx_queue_len += length + 2;
630 }
631 lp->tx_queue_ready = 1;
632
633 if (lp->tx_started == 0) {
634 /* If the Tx is idle, always trigger a transmit. */
635 outb (0x80 | lp->tx_queue, ioaddr + TX_START);
636 lp->tx_queue = 0;
637 lp->tx_queue_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 lp->tx_started = 1;
639 netif_start_queue (dev);
640 } else if (lp->tx_queue_len < 4096 - 1502)
641 /* Yes, there is room for one more packet. */
642 netif_start_queue (dev);
643 dev_kfree_skb (skb);
644
Patrick McHardy6ed10652009-06-23 06:03:08 +0000645 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648/* The typical workload of the driver:
649 Handle the network interface interrupts. */
David Howells7d12e782006-10-05 14:55:46 +0100650static irqreturn_t net_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 struct net_device *dev = dev_id;
653 struct net_local *lp;
654 int ioaddr, status;
655 int handled = 0;
656
657 if (dev == NULL) {
658 printk ("at1700_interrupt(): irq %d for unknown device.\n", irq);
659 return IRQ_NONE;
660 }
661
662 ioaddr = dev->base_addr;
663 lp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 spin_lock (&lp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 status = inw(ioaddr + TX_STATUS);
668 outw(status, ioaddr + TX_STATUS);
669
670 if (net_debug > 4)
671 printk("%s: Interrupt with status %04x.\n", dev->name, status);
672 if (lp->rx_started == 0 &&
673 (status & 0xff00 || (inb(ioaddr + RX_MODE) & 0x40) == 0)) {
674 /* Got a packet(s).
675 We cannot execute net_rx more than once at the same time for
676 the same device. During executing net_rx, we possibly catch a
677 Tx interrupt. Thus we flag on rx_started, so that we prevent
678 the interrupt routine (net_interrupt) to dive into net_rx
679 again. */
680 handled = 1;
681 lp->rx_started = 1;
682 outb(0x00, ioaddr + RX_INTR); /* Disable RX intr. */
683 net_rx(dev);
684 outb(0x81, ioaddr + RX_INTR); /* Enable RX intr. */
685 lp->rx_started = 0;
686 }
687 if (status & 0x00ff) {
688 handled = 1;
689 if (status & 0x02) {
690 /* More than 16 collisions occurred */
691 if (net_debug > 4)
692 printk("%s: 16 Collision occur during Txing.\n", dev->name);
693 /* Cancel sending a packet. */
694 outb(0x03, ioaddr + COL16CNTL);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700695 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697 if (status & 0x82) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700698 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 /* The Tx queue has any packets and is not being
700 transferred a packet from the host, start
701 transmitting. */
702 if (lp->tx_queue && lp->tx_queue_ready) {
703 outb(0x80 | lp->tx_queue, ioaddr + TX_START);
704 lp->tx_queue = 0;
705 lp->tx_queue_len = 0;
706 dev->trans_start = jiffies;
707 netif_wake_queue (dev);
708 } else {
709 lp->tx_started = 0;
710 netif_wake_queue (dev);
711 }
712 }
713 }
714
715 spin_unlock (&lp->lock);
716 return IRQ_RETVAL(handled);
717}
718
719/* We have a good packet(s), get it/them out of the buffers. */
720static void
721net_rx(struct net_device *dev)
722{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 int ioaddr = dev->base_addr;
724 int boguscount = 5;
725
726 while ((inb(ioaddr + RX_MODE) & 0x40) == 0) {
727 ushort status = inw(ioaddr + DATAPORT);
728 ushort pkt_len = inw(ioaddr + DATAPORT);
729
730 if (net_debug > 4)
731 printk("%s: Rxing packet mode %02x status %04x.\n",
732 dev->name, inb(ioaddr + RX_MODE), status);
733#ifndef final_version
734 if (status == 0) {
735 outb(0x05, ioaddr + RX_CTRL);
736 break;
737 }
738#endif
739
740 if ((status & 0xF0) != 0x20) { /* There was an error. */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700741 dev->stats.rx_errors++;
742 if (status & 0x08) dev->stats.rx_length_errors++;
743 if (status & 0x04) dev->stats.rx_frame_errors++;
744 if (status & 0x02) dev->stats.rx_crc_errors++;
745 if (status & 0x01) dev->stats.rx_over_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 } else {
747 /* Malloc up new buffer. */
748 struct sk_buff *skb;
749
750 if (pkt_len > 1550) {
751 printk("%s: The AT1700 claimed a very large packet, size %d.\n",
752 dev->name, pkt_len);
753 /* Prime the FIFO and then flush the packet. */
754 inw(ioaddr + DATAPORT); inw(ioaddr + DATAPORT);
755 outb(0x05, ioaddr + RX_CTRL);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700756 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 break;
758 }
Pradeep A Dalvi21a4e462012-02-05 02:50:10 +0000759 skb = netdev_alloc_skb(dev, pkt_len + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (skb == NULL) {
761 printk("%s: Memory squeeze, dropping packet (len %d).\n",
762 dev->name, pkt_len);
763 /* Prime the FIFO and then flush the packet. */
764 inw(ioaddr + DATAPORT); inw(ioaddr + DATAPORT);
765 outb(0x05, ioaddr + RX_CTRL);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700766 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 break;
768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 skb_reserve(skb,2);
770
771 insw(ioaddr + DATAPORT, skb_put(skb,pkt_len), (pkt_len + 1) >> 1);
772 skb->protocol=eth_type_trans(skb, dev);
773 netif_rx(skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700774 dev->stats.rx_packets++;
775 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777 if (--boguscount <= 0)
778 break;
779 }
780
781 /* If any worth-while packets have been received, dev_rint()
782 has done a mark_bh(NET_BH) for us and will work on them
783 when we get to the bottom-half routine. */
784 {
785 int i;
786 for (i = 0; i < 20; i++) {
787 if ((inb(ioaddr + RX_MODE) & 0x40) == 0x40)
788 break;
789 inw(ioaddr + DATAPORT); /* dummy status read */
790 outb(0x05, ioaddr + RX_CTRL);
791 }
792
793 if (net_debug > 5)
794 printk("%s: Exint Rx packet with mode %02x after %d ticks.\n",
795 dev->name, inb(ioaddr + RX_MODE), i);
796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
799/* The inverse routine to net_open(). */
800static int net_close(struct net_device *dev)
801{
802 struct net_local *lp = netdev_priv(dev);
803 int ioaddr = dev->base_addr;
804
805 netif_stop_queue(dev);
806
807 /* Set configuration register 0 to disable Tx and Rx. */
808 outb(0xda, ioaddr + CONFIG_0);
809
810 /* No statistic counters on the chip to update. */
811
812 /* Disable the IRQ on boards of fmv18x where it is feasible. */
Kulikov Vasiliy451d33dd2010-07-09 02:31:26 +0000813 if (lp->jumpered)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 outb(0x00, ioaddr + IOCONFIG1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 /* Power-down the chip. Green, green, green! */
817 outb(0x00, ioaddr + CONFIG_1);
818 return 0;
819}
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821/*
822 Set the multicast/promiscuous mode for this adaptor.
823*/
824
825static void
826set_rx_mode(struct net_device *dev)
827{
828 int ioaddr = dev->base_addr;
829 struct net_local *lp = netdev_priv(dev);
830 unsigned char mc_filter[8]; /* Multicast hash filter */
831 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 memset(mc_filter, 0xff, sizeof(mc_filter));
835 outb(3, ioaddr + RX_MODE); /* Enable promiscuous mode */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000836 } else if (netdev_mc_count(dev) > MC_FILTERBREAK ||
Joe Perches8e95a202009-12-03 07:58:21 +0000837 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /* Too many to filter perfectly -- accept all multicasts. */
839 memset(mc_filter, 0xff, sizeof(mc_filter));
840 outb(2, ioaddr + RX_MODE); /* Use normal mode. */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000841 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 memset(mc_filter, 0x00, sizeof(mc_filter));
843 outb(1, ioaddr + RX_MODE); /* Ignore almost all multicasts. */
844 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +0000845 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 memset(mc_filter, 0, sizeof(mc_filter));
Jiri Pirko22bedad2010-04-01 21:22:57 +0000848 netdev_for_each_mc_addr(ha, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 unsigned int bit =
Jiri Pirko22bedad2010-04-01 21:22:57 +0000850 ether_crc_le(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 mc_filter[bit >> 3] |= (1 << bit);
852 }
853 outb(0x02, ioaddr + RX_MODE); /* Use normal mode. */
854 }
855
856 spin_lock_irqsave (&lp->lock, flags);
857 if (memcmp(mc_filter, lp->mc_filter, sizeof(mc_filter))) {
Hannes Eder1256f732009-02-14 11:11:55 +0000858 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 int saved_bank = inw(ioaddr + CONFIG_0);
860 /* Switch to bank 1 and set the multicast table. */
861 outw((saved_bank & ~0x0C00) | 0x0480, ioaddr + CONFIG_0);
862 for (i = 0; i < 8; i++)
863 outb(mc_filter[i], ioaddr + PORT_OFFSET(8 + i));
864 memcpy(lp->mc_filter, mc_filter, sizeof(mc_filter));
865 outw(saved_bank, ioaddr + CONFIG_0);
866 }
867 spin_unlock_irqrestore (&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
870#ifdef MODULE
871static struct net_device *dev_at1700;
872
873module_param(io, int, 0);
874module_param(irq, int, 0);
875module_param(net_debug, int, 0);
876MODULE_PARM_DESC(io, "AT1700/FMV18X I/O base address");
877MODULE_PARM_DESC(irq, "AT1700/FMV18X IRQ number");
878MODULE_PARM_DESC(net_debug, "AT1700/FMV18X debug level (0-6)");
879
Jon Schindler5250f332008-02-28 01:31:12 -0600880static int __init at1700_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 if (io == 0)
883 printk("at1700: You should not use auto-probing with insmod!\n");
884 dev_at1700 = at1700_probe(-1);
885 if (IS_ERR(dev_at1700))
886 return PTR_ERR(dev_at1700);
887 return 0;
888}
889
Jon Schindler5250f332008-02-28 01:31:12 -0600890static void __exit at1700_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
892 unregister_netdev(dev_at1700);
893 cleanup_card(dev_at1700);
894 free_netdev(dev_at1700);
895}
Jon Schindler5250f332008-02-28 01:31:12 -0600896module_init(at1700_module_init);
897module_exit(at1700_module_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898#endif /* MODULE */
899MODULE_LICENSE("GPL");