blob: d973fc6c6b88a68095bcd12632d981f1ad946e3f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net-3-driver for the NI5210 card (i82586 Ethernet chip)
3 *
4 * This is an extension to the Linux operating system, and is covered by the
5 * same GNU General Public License that covers that work.
6 *
7 * Alphacode 0.82 (96/09/29) for Linux 2.0.0 (or later)
8 * Copyrights (c) 1994,1995,1996 by M.Hipp (hippm@informatik.uni-tuebingen.de)
9 * [feel free to mail ....]
10 *
11 * when using as module: (no autoprobing!)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * run with e.g:
13 * insmod ni52.o io=0x360 irq=9 memstart=0xd0000 memend=0xd4000
14 *
15 * CAN YOU PLEASE REPORT ME YOUR PERFORMANCE EXPERIENCES !!.
16 *
17 * If you find a bug, please report me:
18 * The kernel panic output and any kmsg from the ni52 driver
19 * the ni5210-driver-version and the linux-kernel version
20 * how many shared memory (memsize) on the netcard,
21 * bootprom: yes/no, base_addr, mem_start
22 * maybe the ni5210-card revision and the i82586 version
23 *
24 * autoprobe for: base_addr: 0x300,0x280,0x360,0x320,0x340
25 * mem_start: 0xd0000,0xd2000,0xc8000,0xca000,0xd4000,0xd6000,
26 * 0xd8000,0xcc000,0xce000,0xda000,0xdc000
27 *
28 * sources:
29 * skeleton.c from Donald Becker
30 *
31 * I have also done a look in the following sources: (mail me if you need them)
32 * crynwr-packet-driver by Russ Nelson
33 * Garret A. Wollman's (fourth) i82586-driver for BSD
Alan Coxa197f692008-02-08 15:27:38 +000034 * (before getting an i82596 (yes 596 not 586) manual, the existing drivers
35 * helped me a lot to understand this tricky chip.)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 *
37 * Known Problems:
38 * The internal sysbus seems to be slow. So we often lose packets because of
39 * overruns while receiving from a fast remote host.
Alan Coxa197f692008-02-08 15:27:38 +000040 * This can slow down TCP connections. Maybe the newer ni5210 cards are
41 * better. My experience is, that if a machine sends with more than about
42 * 500-600K/s the fifo/sysbus overflows.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 *
44 * IMPORTANT NOTE:
45 * On fast networks, it's a (very) good idea to have 16K shared memory. With
Alan Coxa197f692008-02-08 15:27:38 +000046 * 8K, we can store only 4 receive frames, so it can (easily) happen that a
47 * remote machine 'overruns' our system.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 *
49 * Known i82586/card problems (I'm sure, there are many more!):
50 * Running the NOP-mode, the i82586 sometimes seems to forget to report
51 * every xmit-interrupt until we restart the CU.
52 * Another MAJOR bug is, that the RU sometimes seems to ignore the EL-Bit
53 * in the RBD-Struct which indicates an end of the RBD queue.
54 * Instead, the RU fetches another (randomly selected and
55 * usually used) RBD and begins to fill it. (Maybe, this happens only if
56 * the last buffer from the previous RFD fits exact into the queue and
57 * the next RFD can't fetch an initial RBD. Anyone knows more? )
58 *
59 * results from ftp performance tests with Linux 1.2.5
60 * send and receive about 350-400 KByte/s (peak up to 460 kbytes/s)
Alan Coxa197f692008-02-08 15:27:38 +000061 * sending in NOP-mode: peak performance up to 530K/s (but better don't
62 * run this mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 */
64
65/*
66 * 29.Sept.96: virt_to_bus changes for new memory scheme
67 * 19.Feb.96: more Mcast changes, module support (MH)
68 *
69 * 18.Nov.95: Mcast changes (AC).
70 *
71 * 23.April.95: fixed(?) receiving problems by configuring a RFD more
72 * than the number of RBD's. Can maybe cause other problems.
73 * 18.April.95: Added MODULE support (MH)
74 * 17.April.95: MC related changes in init586() and set_multicast_list().
75 * removed use of 'jiffies' in init586() (MH)
76 *
77 * 19.Sep.94: Added Multicast support (not tested yet) (MH)
78 *
79 * 18.Sep.94: Workaround for 'EL-Bug'. Removed flexible RBD-handling.
80 * Now, every RFD has exact one RBD. (MH)
81 *
82 * 14.Sep.94: added promiscuous mode, a few cleanups (MH)
83 *
84 * 19.Aug.94: changed request_irq() parameter (MH)
85 *
86 * 20.July.94: removed cleanup bugs, removed a 16K-mem-probe-bug (MH)
87 *
88 * 19.July.94: lotsa cleanups .. (MH)
89 *
90 * 17.July.94: some patches ... verified to run with 1.1.29 (MH)
91 *
92 * 4.July.94: patches for Linux 1.1.24 (MH)
93 *
94 * 26.March.94: patches for Linux 1.0 and iomem-auto-probe (MH)
95 *
Alan Coxa197f692008-02-08 15:27:38 +000096 * 30.Sep.93: Added nop-chain .. driver now runs with only one Xmit-Buff,
97 * too (MH)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 *
99 * < 30.Sep.93: first versions
100 */
101
102static int debuglevel; /* debug-printk 0: off 1: a few 2: more */
103static int automatic_resume; /* experimental .. better should be zero */
104static int rfdadd; /* rfdadd=1 may be better for 8K MEM cards */
Alan Coxa197f692008-02-08 15:27:38 +0000105static int fifo = 0x8; /* don't change */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#include <linux/module.h>
108#include <linux/kernel.h>
109#include <linux/string.h>
110#include <linux/errno.h>
111#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#include <linux/interrupt.h>
113#include <linux/delay.h>
114#include <linux/init.h>
115#include <linux/bitops.h>
116#include <asm/io.h>
117
118#include <linux/netdevice.h>
119#include <linux/etherdevice.h>
120#include <linux/skbuff.h>
121
122#include "ni52.h"
123
124#define DRV_NAME "ni52"
125
126#define DEBUG /* debug on */
127#define SYSBUSVAL 1 /* 8 Bit */
128
Alan Coxa197f692008-02-08 15:27:38 +0000129#define ni_attn586() { outb(0, dev->base_addr + NI52_ATTENTION); }
130#define ni_reset586() { outb(0, dev->base_addr + NI52_RESET); }
131#define ni_disint() { outb(0, dev->base_addr + NI52_INTDIS); }
132#define ni_enaint() { outb(0, dev->base_addr + NI52_INTENA); }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Al Viro05bd8312008-03-19 09:44:39 +0000134#define make32(ptr16) ((void __iomem *)(p->memtop + (short) (ptr16)))
Al Viroe06ee2b2008-03-19 09:43:59 +0000135#define make24(ptr32) ((char __iomem *)(ptr32)) - p->base
136#define make16(ptr32) ((unsigned short) ((char __iomem *)(ptr32)\
137 - p->memtop))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139/******************* how to calculate the buffers *****************************
140
141 * IMPORTANT NOTE: if you configure only one NUM_XMIT_BUFFS, the driver works
142 * --------------- in a different (more stable?) mode. Only in this mode it's
143 * possible to configure the driver with 'NO_NOPCOMMANDS'
144
145sizeof(scp)=12; sizeof(scb)=16; sizeof(iscp)=8;
146sizeof(scp)+sizeof(iscp)+sizeof(scb) = 36 = INIT
147sizeof(rfd) = 24; sizeof(rbd) = 12;
148sizeof(tbd) = 8; sizeof(transmit_cmd) = 16;
149sizeof(nop_cmd) = 8;
150
151 * if you don't know the driver, better do not change these values: */
152
153#define RECV_BUFF_SIZE 1524 /* slightly oversized */
154#define XMIT_BUFF_SIZE 1524 /* slightly oversized */
155#define NUM_XMIT_BUFFS 1 /* config for both, 8K and 16K shmem */
156#define NUM_RECV_BUFFS_8 4 /* config for 8K shared mem */
157#define NUM_RECV_BUFFS_16 9 /* config for 16K shared mem */
158#define NO_NOPCOMMANDS /* only possible with NUM_XMIT_BUFFS=1 */
159
160/**************************************************************************/
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163#define NI52_TOTAL_SIZE 16
164#define NI52_ADDR0 0x02
165#define NI52_ADDR1 0x07
166#define NI52_ADDR2 0x01
167
Alan Coxa197f692008-02-08 15:27:38 +0000168static int ni52_probe1(struct net_device *dev, int ioaddr);
169static irqreturn_t ni52_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static int ni52_open(struct net_device *dev);
171static int ni52_close(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000172static netdev_tx_t ni52_send_packet(struct sk_buff *, struct net_device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static struct net_device_stats *ni52_get_stats(struct net_device *dev);
174static void set_multicast_list(struct net_device *dev);
175static void ni52_timeout(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177/* helper-functions */
178static int init586(struct net_device *dev);
Al Virode597142008-03-19 09:43:49 +0000179static int check586(struct net_device *dev, unsigned size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static void alloc586(struct net_device *dev);
181static void startrecv586(struct net_device *dev);
Al Viro2d76c262008-03-19 09:43:29 +0000182static void __iomem *alloc_rfa(struct net_device *dev, void __iomem *ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183static void ni52_rcv_int(struct net_device *dev);
184static void ni52_xmt_int(struct net_device *dev);
185static void ni52_rnr_int(struct net_device *dev);
186
Alan Coxa197f692008-02-08 15:27:38 +0000187struct priv {
Al Viroe06ee2b2008-03-19 09:43:59 +0000188 char __iomem *base;
189 char __iomem *mapped;
Al Viro2d76c262008-03-19 09:43:29 +0000190 char __iomem *memtop;
Alan Coxa197f692008-02-08 15:27:38 +0000191 spinlock_t spinlock;
192 int reset;
Al Viro2d76c262008-03-19 09:43:29 +0000193 struct rfd_struct __iomem *rfd_last, *rfd_top, *rfd_first;
194 struct scp_struct __iomem *scp;
195 struct iscp_struct __iomem *iscp;
196 struct scb_struct __iomem *scb;
197 struct tbd_struct __iomem *xmit_buffs[NUM_XMIT_BUFFS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198#if (NUM_XMIT_BUFFS == 1)
Al Viro2d76c262008-03-19 09:43:29 +0000199 struct transmit_cmd_struct __iomem *xmit_cmds[2];
200 struct nop_cmd_struct __iomem *nop_cmds[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201#else
Al Viro2d76c262008-03-19 09:43:29 +0000202 struct transmit_cmd_struct __iomem *xmit_cmds[NUM_XMIT_BUFFS];
203 struct nop_cmd_struct __iomem *nop_cmds[NUM_XMIT_BUFFS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204#endif
Alan Coxa197f692008-02-08 15:27:38 +0000205 int nop_point, num_recv_buffs;
Al Viro2d76c262008-03-19 09:43:29 +0000206 char __iomem *xmit_cbuffs[NUM_XMIT_BUFFS];
Alan Coxa197f692008-02-08 15:27:38 +0000207 int xmit_count, xmit_last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
209
Alan Coxa197f692008-02-08 15:27:38 +0000210/* wait for command with timeout: */
211static void wait_for_scb_cmd(struct net_device *dev)
212{
Wang Chen4cf16532008-11-12 23:38:14 -0800213 struct priv *p = netdev_priv(dev);
Alan Coxa197f692008-02-08 15:27:38 +0000214 int i;
215 for (i = 0; i < 16384; i++) {
216 if (readb(&p->scb->cmd_cuc) == 0)
217 break;
218 udelay(4);
219 if (i == 16383) {
220 printk(KERN_ERR "%s: scb_cmd timed out: %04x,%04x .. disabling i82586!!\n",
221 dev->name, readb(&p->scb->cmd_cuc), readb(&p->scb->cus));
222 if (!p->reset) {
223 p->reset = 1;
224 ni_reset586();
225 }
226 }
227 }
228}
229
230static void wait_for_scb_cmd_ruc(struct net_device *dev)
231{
Wang Chen4cf16532008-11-12 23:38:14 -0800232 struct priv *p = netdev_priv(dev);
Alan Coxa197f692008-02-08 15:27:38 +0000233 int i;
234 for (i = 0; i < 16384; i++) {
235 if (readb(&p->scb->cmd_ruc) == 0)
236 break;
237 udelay(4);
238 if (i == 16383) {
239 printk(KERN_ERR "%s: scb_cmd (ruc) timed out: %04x,%04x .. disabling i82586!!\n",
Al Viro05bd8312008-03-19 09:44:39 +0000240 dev->name, readb(&p->scb->cmd_ruc),
241 readb(&p->scb->rus));
Alan Coxa197f692008-02-08 15:27:38 +0000242 if (!p->reset) {
243 p->reset = 1;
244 ni_reset586();
245 }
246 }
247 }
248}
249
Al Viro2d76c262008-03-19 09:43:29 +0000250static void wait_for_stat_compl(void __iomem *p)
Alan Coxa197f692008-02-08 15:27:38 +0000251{
Al Viro2d76c262008-03-19 09:43:29 +0000252 struct nop_cmd_struct __iomem *addr = p;
Alan Coxa197f692008-02-08 15:27:38 +0000253 int i;
254 for (i = 0; i < 32767; i++) {
255 if (readw(&((addr)->cmd_status)) & STAT_COMPL)
256 break;
257 udelay(32);
258 }
259}
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261/**********************************************
262 * close device
263 */
264static int ni52_close(struct net_device *dev)
265{
266 free_irq(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 ni_reset586(); /* the hard way to stop the receiver */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return 0;
270}
271
272/**********************************************
273 * open device
274 */
275static int ni52_open(struct net_device *dev)
276{
277 int ret;
278
279 ni_disint();
280 alloc586(dev);
281 init586(dev);
282 startrecv586(dev);
283 ni_enaint();
284
Joe Perchesa0607fd2009-11-18 23:29:17 -0800285 ret = request_irq(dev->irq, ni52_interrupt, 0, dev->name, dev);
Alan Coxa197f692008-02-08 15:27:38 +0000286 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 ni_reset586();
288 return ret;
289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 netif_start_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return 0; /* most done by init */
292}
293
Al Viro76af5692008-03-19 09:44:19 +0000294static int check_iscp(struct net_device *dev, void __iomem *addr)
295{
296 struct iscp_struct __iomem *iscp = addr;
Wang Chen4cf16532008-11-12 23:38:14 -0800297 struct priv *p = netdev_priv(dev);
Al Viro76af5692008-03-19 09:44:19 +0000298 memset_io(iscp, 0, sizeof(struct iscp_struct));
299
300 writel(make24(iscp), &p->scp->iscp);
301 writeb(1, &iscp->busy);
302
303 ni_reset586();
304 ni_attn586();
305 mdelay(32); /* wait a while... */
306 /* i82586 clears 'busy' after successful init */
307 if (readb(&iscp->busy))
308 return 0;
309 return 1;
310}
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312/**********************************************
313 * Check to see if there's an 82586 out there.
314 */
Al Virode597142008-03-19 09:43:49 +0000315static int check586(struct net_device *dev, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Wang Chen4cf16532008-11-12 23:38:14 -0800317 struct priv *p = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 int i;
319
Al Viro176f65f2008-03-19 09:44:29 +0000320 p->mapped = ioremap(dev->mem_start, size);
321 if (!p->mapped)
322 return 0;
323
Al Viroe06ee2b2008-03-19 09:43:59 +0000324 p->base = p->mapped + size - 0x01000000;
325 p->memtop = p->mapped + size;
Al Viro2d76c262008-03-19 09:43:29 +0000326 p->scp = (struct scp_struct __iomem *)(p->base + SCP_DEFAULT_ADDRESS);
Al Viro7f8cfd52008-03-19 09:44:09 +0000327 p->scb = (struct scb_struct __iomem *) p->mapped;
Al Viro76af5692008-03-19 09:44:19 +0000328 p->iscp = (struct iscp_struct __iomem *)p->scp - 1;
Al Viro2d76c262008-03-19 09:43:29 +0000329 memset_io(p->scp, 0, sizeof(struct scp_struct));
Alan Coxa197f692008-02-08 15:27:38 +0000330 for (i = 0; i < sizeof(struct scp_struct); i++)
331 /* memory was writeable? */
Al Viro2d76c262008-03-19 09:43:29 +0000332 if (readb((char __iomem *)p->scp + i))
Al Viro176f65f2008-03-19 09:44:29 +0000333 goto Enodev;
Alan Coxa197f692008-02-08 15:27:38 +0000334 writeb(SYSBUSVAL, &p->scp->sysbus); /* 1 = 8Bit-Bus, 0 = 16 Bit */
335 if (readb(&p->scp->sysbus) != SYSBUSVAL)
Al Viro176f65f2008-03-19 09:44:29 +0000336 goto Enodev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Al Viro76af5692008-03-19 09:44:19 +0000338 if (!check_iscp(dev, p->mapped))
Al Viro176f65f2008-03-19 09:44:29 +0000339 goto Enodev;
Al Viro76af5692008-03-19 09:44:19 +0000340 if (!check_iscp(dev, p->iscp))
Al Viro176f65f2008-03-19 09:44:29 +0000341 goto Enodev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return 1;
Al Viro176f65f2008-03-19 09:44:29 +0000343Enodev:
344 iounmap(p->mapped);
345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
348/******************************************************************
349 * set iscp at the right place, called by ni52_probe1 and open586.
350 */
351static void alloc586(struct net_device *dev)
352{
Wang Chen4cf16532008-11-12 23:38:14 -0800353 struct priv *p = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 ni_reset586();
Alan Coxa197f692008-02-08 15:27:38 +0000356 mdelay(32);
357
Alan Coxa197f692008-02-08 15:27:38 +0000358 memset_io(p->iscp, 0, sizeof(struct iscp_struct));
359 memset_io(p->scp , 0, sizeof(struct scp_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Alan Coxa197f692008-02-08 15:27:38 +0000361 writel(make24(p->iscp), &p->scp->iscp);
362 writeb(SYSBUSVAL, &p->scp->sysbus);
363 writew(make16(p->scb), &p->iscp->scb_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Alan Coxa197f692008-02-08 15:27:38 +0000365 writeb(1, &p->iscp->busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 ni_reset586();
367 ni_attn586();
368
Alan Coxa197f692008-02-08 15:27:38 +0000369 mdelay(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Alan Coxa197f692008-02-08 15:27:38 +0000371 if (readb(&p->iscp->busy))
372 printk(KERN_ERR "%s: Init-Problems (alloc).\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Alan Coxa197f692008-02-08 15:27:38 +0000374 p->reset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Al Viro2d76c262008-03-19 09:43:29 +0000376 memset_io(p->scb, 0, sizeof(struct scb_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
379/* set: io,irq,memstart,memend or set it when calling insmod */
Alan Coxa197f692008-02-08 15:27:38 +0000380static int irq = 9;
381static int io = 0x300;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382static long memstart; /* e.g 0xd0000 */
383static long memend; /* e.g 0xd4000 */
384
385/**********************************************
386 * probe the ni5210-card
387 */
388struct net_device * __init ni52_probe(int unit)
389{
390 struct net_device *dev = alloc_etherdev(sizeof(struct priv));
Joe Perchesb6bc7652010-12-21 02:16:08 -0800391 static const int ports[] = {0x300, 0x280, 0x360, 0x320, 0x340, 0};
392 const int *port;
Al Viro176f65f2008-03-19 09:44:29 +0000393 struct priv *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 int err = 0;
395
396 if (!dev)
397 return ERR_PTR(-ENOMEM);
398
Wang Chen4cf16532008-11-12 23:38:14 -0800399 p = netdev_priv(dev);
Al Viro176f65f2008-03-19 09:44:29 +0000400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (unit >= 0) {
402 sprintf(dev->name, "eth%d", unit);
403 netdev_boot_setup_check(dev);
404 io = dev->base_addr;
405 irq = dev->irq;
406 memstart = dev->mem_start;
407 memend = dev->mem_end;
408 }
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (io > 0x1ff) { /* Check a single specified location. */
411 err = ni52_probe1(dev, io);
412 } else if (io > 0) { /* Don't probe at all. */
413 err = -ENXIO;
414 } else {
415 for (port = ports; *port && ni52_probe1(dev, *port) ; port++)
416 ;
417 if (*port)
418 goto got_it;
419#ifdef FULL_IO_PROBE
420 for (io = 0x200; io < 0x400 && ni52_probe1(dev, io); io += 8)
421 ;
422 if (io < 0x400)
423 goto got_it;
424#endif
425 err = -ENODEV;
426 }
427 if (err)
428 goto out;
429got_it:
430 err = register_netdev(dev);
431 if (err)
432 goto out1;
433 return dev;
434out1:
Al Viro176f65f2008-03-19 09:44:29 +0000435 iounmap(p->mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 release_region(dev->base_addr, NI52_TOTAL_SIZE);
437out:
438 free_netdev(dev);
439 return ERR_PTR(err);
440}
441
Stephen Hemminger2c7669e2009-03-26 15:11:39 +0000442static const struct net_device_ops ni52_netdev_ops = {
443 .ndo_open = ni52_open,
444 .ndo_stop = ni52_close,
445 .ndo_get_stats = ni52_get_stats,
446 .ndo_tx_timeout = ni52_timeout,
447 .ndo_start_xmit = ni52_send_packet,
448 .ndo_set_multicast_list = set_multicast_list,
449 .ndo_change_mtu = eth_change_mtu,
450 .ndo_set_mac_address = eth_mac_addr,
451 .ndo_validate_addr = eth_validate_addr,
452};
453
Alan Coxa197f692008-02-08 15:27:38 +0000454static int __init ni52_probe1(struct net_device *dev, int ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 int i, size, retval;
Wang Chen4cf16532008-11-12 23:38:14 -0800457 struct priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 dev->base_addr = ioaddr;
460 dev->irq = irq;
461 dev->mem_start = memstart;
462 dev->mem_end = memend;
463
Al Viro7f8cfd52008-03-19 09:44:09 +0000464 spin_lock_init(&priv->spinlock);
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (!request_region(ioaddr, NI52_TOTAL_SIZE, DRV_NAME))
467 return -EBUSY;
468
Alan Coxa197f692008-02-08 15:27:38 +0000469 if (!(inb(ioaddr+NI52_MAGIC1) == NI52_MAGICVAL1) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 !(inb(ioaddr+NI52_MAGIC2) == NI52_MAGICVAL2)) {
471 retval = -ENODEV;
472 goto out;
473 }
474
Alan Coxa197f692008-02-08 15:27:38 +0000475 for (i = 0; i < ETH_ALEN; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 dev->dev_addr[i] = inb(dev->base_addr+i);
477
Joe Perches8e95a202009-12-03 07:58:21 +0000478 if (dev->dev_addr[0] != NI52_ADDR0 || dev->dev_addr[1] != NI52_ADDR1 ||
479 dev->dev_addr[2] != NI52_ADDR2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 retval = -ENODEV;
481 goto out;
482 }
483
Alan Coxa197f692008-02-08 15:27:38 +0000484 printk(KERN_INFO "%s: NI5210 found at %#3lx, ",
485 dev->name, dev->base_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 /*
488 * check (or search) IO-Memory, 8K and 16K
489 */
490#ifdef MODULE
491 size = dev->mem_end - dev->mem_start;
Alan Coxa197f692008-02-08 15:27:38 +0000492 if (size != 0x2000 && size != 0x4000) {
493 printk("\n");
494 printk(KERN_ERR "%s: Invalid memory size %d. Allowed is 0x2000 or 0x4000 bytes.\n", dev->name, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 retval = -ENODEV;
496 goto out;
497 }
Al Virode597142008-03-19 09:43:49 +0000498 if (!check586(dev, size)) {
Alan Coxa197f692008-02-08 15:27:38 +0000499 printk(KERN_ERR "?memcheck, Can't find memory at 0x%lx with size %d!\n", dev->mem_start, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 retval = -ENODEV;
501 goto out;
502 }
503#else
Alan Coxa197f692008-02-08 15:27:38 +0000504 if (dev->mem_start != 0) {
505 /* no auto-mem-probe */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 size = 0x4000; /* check for 16K mem */
Al Virode597142008-03-19 09:43:49 +0000507 if (!check586(dev, size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 size = 0x2000; /* check for 8K mem */
Al Virode597142008-03-19 09:43:49 +0000509 if (!check586(dev, size)) {
Alan Coxa197f692008-02-08 15:27:38 +0000510 printk(KERN_ERR "?memprobe, Can't find memory at 0x%lx!\n", dev->mem_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 retval = -ENODEV;
512 goto out;
513 }
514 }
Alan Coxa197f692008-02-08 15:27:38 +0000515 } else {
516 static const unsigned long memaddrs[] = {
517 0xc8000, 0xca000, 0xcc000, 0xce000, 0xd0000, 0xd2000,
518 0xd4000, 0xd6000, 0xd8000, 0xda000, 0xdc000, 0
519 };
520 for (i = 0;; i++) {
521 if (!memaddrs[i]) {
522 printk(KERN_ERR "?memprobe, Can't find io-memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 retval = -ENODEV;
524 goto out;
525 }
526 dev->mem_start = memaddrs[i];
527 size = 0x2000; /* check for 8K mem */
Al Virode597142008-03-19 09:43:49 +0000528 if (check586(dev, size))
Alan Coxa197f692008-02-08 15:27:38 +0000529 /* 8K-check */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 break;
531 size = 0x4000; /* check for 16K mem */
Al Virode597142008-03-19 09:43:49 +0000532 if (check586(dev, size))
Alan Coxa197f692008-02-08 15:27:38 +0000533 /* 16K-check */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 break;
535 }
536 }
Alan Coxa197f692008-02-08 15:27:38 +0000537 /* set mem_end showed by 'ifconfig' */
538 dev->mem_end = dev->mem_start + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539#endif
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 alloc586(dev);
542
543 /* set number of receive-buffs according to memsize */
Alan Coxa197f692008-02-08 15:27:38 +0000544 if (size == 0x2000)
Al Viroe06ee2b2008-03-19 09:43:59 +0000545 priv->num_recv_buffs = NUM_RECV_BUFFS_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 else
Al Viroe06ee2b2008-03-19 09:43:59 +0000547 priv->num_recv_buffs = NUM_RECV_BUFFS_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Alan Coxa197f692008-02-08 15:27:38 +0000549 printk(KERN_DEBUG "Memaddr: 0x%lx, Memsize: %d, ",
550 dev->mem_start, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Alan Coxa197f692008-02-08 15:27:38 +0000552 if (dev->irq < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 unsigned long irq_mask;
554
555 irq_mask = probe_irq_on();
556 ni_reset586();
557 ni_attn586();
558
559 mdelay(20);
560 dev->irq = probe_irq_off(irq_mask);
Alan Coxa197f692008-02-08 15:27:38 +0000561 if (!dev->irq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 printk("?autoirq, Failed to detect IRQ line!\n");
563 retval = -EAGAIN;
Al Viro176f65f2008-03-19 09:44:29 +0000564 iounmap(priv->mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 goto out;
566 }
Alan Coxa197f692008-02-08 15:27:38 +0000567 printk("IRQ %d (autodetected).\n", dev->irq);
568 } else {
569 if (dev->irq == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 dev->irq = 9;
Alan Coxa197f692008-02-08 15:27:38 +0000571 printk("IRQ %d (assigned and not checked!).\n", dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573
Stephen Hemminger2c7669e2009-03-26 15:11:39 +0000574 dev->netdev_ops = &ni52_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 dev->watchdog_timeo = HZ/20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 return 0;
578out:
579 release_region(ioaddr, NI52_TOTAL_SIZE);
580 return retval;
581}
582
583/**********************************************
584 * init the chip (ni52-interrupt should be disabled?!)
585 * needs a correct 'allocated' memory
586 */
587
588static int init586(struct net_device *dev)
589{
Al Viro2d76c262008-03-19 09:43:29 +0000590 void __iomem *ptr;
Alan Coxa197f692008-02-08 15:27:38 +0000591 int i, result = 0;
Wang Chen4cf16532008-11-12 23:38:14 -0800592 struct priv *p = netdev_priv(dev);
Al Viro2d76c262008-03-19 09:43:29 +0000593 struct configure_cmd_struct __iomem *cfg_cmd;
594 struct iasetup_cmd_struct __iomem *ias_cmd;
595 struct tdr_cmd_struct __iomem *tdr_cmd;
596 struct mcsetup_cmd_struct __iomem *mc_cmd;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000597 struct netdev_hw_addr *ha;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000598 int num_addrs = netdev_mc_count(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Al Viro2d76c262008-03-19 09:43:29 +0000600 ptr = p->scb + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Al Viro2d76c262008-03-19 09:43:29 +0000602 cfg_cmd = ptr; /* configure-command */
Alan Coxa197f692008-02-08 15:27:38 +0000603 writew(0, &cfg_cmd->cmd_status);
604 writew(CMD_CONFIGURE | CMD_LAST, &cfg_cmd->cmd_cmd);
605 writew(0xFFFF, &cfg_cmd->cmd_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Alan Coxa197f692008-02-08 15:27:38 +0000607 /* number of cfg bytes */
608 writeb(0x0a, &cfg_cmd->byte_cnt);
609 /* fifo-limit (8=tx:32/rx:64) */
610 writeb(fifo, &cfg_cmd->fifo);
611 /* hold or discard bad recv frames (bit 7) */
612 writeb(0x40, &cfg_cmd->sav_bf);
613 /* addr_len |!src_insert |pre-len |loopback */
614 writeb(0x2e, &cfg_cmd->adr_len);
615 writeb(0x00, &cfg_cmd->priority);
Joe Perchesa419aef2009-08-18 11:18:35 -0700616 writeb(0x60, &cfg_cmd->ifs);
Alan Coxa197f692008-02-08 15:27:38 +0000617 writeb(0x00, &cfg_cmd->time_low);
618 writeb(0xf2, &cfg_cmd->time_high);
Joe Perchesa419aef2009-08-18 11:18:35 -0700619 writeb(0x00, &cfg_cmd->promisc);
Alan Coxa197f692008-02-08 15:27:38 +0000620 if (dev->flags & IFF_ALLMULTI) {
Al Viro2d76c262008-03-19 09:43:29 +0000621 int len = ((char __iomem *)p->iscp - (char __iomem *)ptr - 8) / 6;
Alan Coxa197f692008-02-08 15:27:38 +0000622 if (num_addrs > len) {
623 printk(KERN_ERR "%s: switching to promisc. mode\n",
624 dev->name);
Wang Chenc16d1182008-07-22 13:13:12 +0800625 writeb(0x01, &cfg_cmd->promisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627 }
Alan Coxa197f692008-02-08 15:27:38 +0000628 if (dev->flags & IFF_PROMISC)
629 writeb(0x01, &cfg_cmd->promisc);
630 writeb(0x00, &cfg_cmd->carr_coll);
631 writew(make16(cfg_cmd), &p->scb->cbl_offset);
Al Viro05bd8312008-03-19 09:44:39 +0000632 writeb(0, &p->scb->cmd_ruc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Alan Coxa197f692008-02-08 15:27:38 +0000634 writeb(CUC_START, &p->scb->cmd_cuc); /* cmd.-unit start */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 ni_attn586();
636
Alan Coxa197f692008-02-08 15:27:38 +0000637 wait_for_stat_compl(cfg_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Alan Coxa197f692008-02-08 15:27:38 +0000639 if ((readw(&cfg_cmd->cmd_status) & (STAT_OK|STAT_COMPL)) !=
640 (STAT_COMPL|STAT_OK)) {
641 printk(KERN_ERR "%s: configure command failed: %x\n",
642 dev->name, readw(&cfg_cmd->cmd_status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return 1;
644 }
645
646 /*
647 * individual address setup
648 */
649
Al Viro2d76c262008-03-19 09:43:29 +0000650 ias_cmd = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Alan Coxa197f692008-02-08 15:27:38 +0000652 writew(0, &ias_cmd->cmd_status);
653 writew(CMD_IASETUP | CMD_LAST, &ias_cmd->cmd_cmd);
654 writew(0xffff, &ias_cmd->cmd_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Al Viro2d76c262008-03-19 09:43:29 +0000656 memcpy_toio(&ias_cmd->iaddr, (char *)dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Alan Coxa197f692008-02-08 15:27:38 +0000658 writew(make16(ias_cmd), &p->scb->cbl_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Alan Coxa197f692008-02-08 15:27:38 +0000660 writeb(CUC_START, &p->scb->cmd_cuc); /* cmd.-unit start */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 ni_attn586();
662
Alan Coxa197f692008-02-08 15:27:38 +0000663 wait_for_stat_compl(ias_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Alan Coxa197f692008-02-08 15:27:38 +0000665 if ((readw(&ias_cmd->cmd_status) & (STAT_OK|STAT_COMPL)) !=
666 (STAT_OK|STAT_COMPL)) {
667 printk(KERN_ERR "%s (ni52): individual address setup command failed: %04x\n", dev->name, readw(&ias_cmd->cmd_status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return 1;
669 }
670
671 /*
672 * TDR, wire check .. e.g. no resistor e.t.c
673 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400674
Al Viro2d76c262008-03-19 09:43:29 +0000675 tdr_cmd = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Alan Coxa197f692008-02-08 15:27:38 +0000677 writew(0, &tdr_cmd->cmd_status);
678 writew(CMD_TDR | CMD_LAST, &tdr_cmd->cmd_cmd);
679 writew(0xffff, &tdr_cmd->cmd_link);
680 writew(0, &tdr_cmd->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Alan Coxa197f692008-02-08 15:27:38 +0000682 writew(make16(tdr_cmd), &p->scb->cbl_offset);
683 writeb(CUC_START, &p->scb->cmd_cuc); /* cmd.-unit start */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 ni_attn586();
685
Alan Coxa197f692008-02-08 15:27:38 +0000686 wait_for_stat_compl(tdr_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Alan Coxa197f692008-02-08 15:27:38 +0000688 if (!(readw(&tdr_cmd->cmd_status) & STAT_COMPL))
689 printk(KERN_ERR "%s: Problems while running the TDR.\n",
690 dev->name);
691 else {
692 udelay(16);
693 result = readw(&tdr_cmd->status);
694 writeb(readb(&p->scb->cus) & STAT_MASK, &p->scb->cmd_cuc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 ni_attn586(); /* ack the interrupts */
696
Alan Coxa197f692008-02-08 15:27:38 +0000697 if (result & TDR_LNK_OK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 ;
Alan Coxa197f692008-02-08 15:27:38 +0000699 else if (result & TDR_XCVR_PRB)
700 printk(KERN_ERR "%s: TDR: Transceiver problem. Check the cable(s)!\n",
701 dev->name);
702 else if (result & TDR_ET_OPN)
703 printk(KERN_ERR "%s: TDR: No correct termination %d clocks away.\n",
704 dev->name, result & TDR_TIMEMASK);
705 else if (result & TDR_ET_SRT) {
706 /* time == 0 -> strange :-) */
707 if (result & TDR_TIMEMASK)
708 printk(KERN_ERR "%s: TDR: Detected a short circuit %d clocks away.\n",
709 dev->name, result & TDR_TIMEMASK);
710 } else
711 printk(KERN_ERR "%s: TDR: Unknown status %04x\n",
712 dev->name, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714
715 /*
716 * Multicast setup
717 */
Alan Coxa197f692008-02-08 15:27:38 +0000718 if (num_addrs && !(dev->flags & IFF_PROMISC)) {
Al Viro2d76c262008-03-19 09:43:29 +0000719 mc_cmd = ptr;
Alan Coxa197f692008-02-08 15:27:38 +0000720 writew(0, &mc_cmd->cmd_status);
721 writew(CMD_MCSETUP | CMD_LAST, &mc_cmd->cmd_cmd);
722 writew(0xffff, &mc_cmd->cmd_link);
723 writew(num_addrs * 6, &mc_cmd->mc_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +0000725 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000726 netdev_for_each_mc_addr(ha, dev)
727 memcpy_toio(mc_cmd->mc_list[i++], ha->addr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Alan Coxa197f692008-02-08 15:27:38 +0000729 writew(make16(mc_cmd), &p->scb->cbl_offset);
730 writeb(CUC_START, &p->scb->cmd_cuc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 ni_attn586();
732
Alan Coxa197f692008-02-08 15:27:38 +0000733 wait_for_stat_compl(mc_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Alan Coxa197f692008-02-08 15:27:38 +0000735 if ((readw(&mc_cmd->cmd_status) & (STAT_COMPL|STAT_OK))
736 != (STAT_COMPL|STAT_OK))
737 printk(KERN_ERR "%s: Can't apply multicast-address-list.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739
740 /*
741 * alloc nop/xmit-cmds
742 */
743#if (NUM_XMIT_BUFFS == 1)
Alan Coxa197f692008-02-08 15:27:38 +0000744 for (i = 0; i < 2; i++) {
Al Viro2d76c262008-03-19 09:43:29 +0000745 p->nop_cmds[i] = ptr;
Alan Coxa197f692008-02-08 15:27:38 +0000746 writew(CMD_NOP, &p->nop_cmds[i]->cmd_cmd);
747 writew(0, &p->nop_cmds[i]->cmd_status);
748 writew(make16(p->nop_cmds[i]), &p->nop_cmds[i]->cmd_link);
Al Viro2d76c262008-03-19 09:43:29 +0000749 ptr = ptr + sizeof(struct nop_cmd_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
751#else
Alan Coxa197f692008-02-08 15:27:38 +0000752 for (i = 0; i < NUM_XMIT_BUFFS; i++) {
Al Viro2d76c262008-03-19 09:43:29 +0000753 p->nop_cmds[i] = ptr;
Alan Coxa197f692008-02-08 15:27:38 +0000754 writew(CMD_NOP, &p->nop_cmds[i]->cmd_cmd);
755 writew(0, &p->nop_cmds[i]->cmd_status);
756 writew(make16(p->nop_cmds[i]), &p->nop_cmds[i]->cmd_link);
Al Viro2d76c262008-03-19 09:43:29 +0000757 ptr = ptr + sizeof(struct nop_cmd_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759#endif
760
Al Viro2d76c262008-03-19 09:43:29 +0000761 ptr = alloc_rfa(dev, ptr); /* init receive-frame-area */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 /*
764 * alloc xmit-buffs / init xmit_cmds
765 */
Alan Coxa197f692008-02-08 15:27:38 +0000766 for (i = 0; i < NUM_XMIT_BUFFS; i++) {
767 /* Transmit cmd/buff 0 */
Al Viro2d76c262008-03-19 09:43:29 +0000768 p->xmit_cmds[i] = ptr;
769 ptr = ptr + sizeof(struct transmit_cmd_struct);
770 p->xmit_cbuffs[i] = ptr; /* char-buffs */
771 ptr = ptr + XMIT_BUFF_SIZE;
772 p->xmit_buffs[i] = ptr; /* TBD */
773 ptr = ptr + sizeof(struct tbd_struct);
774 if ((void __iomem *)ptr > (void __iomem *)p->iscp) {
Alan Coxa197f692008-02-08 15:27:38 +0000775 printk(KERN_ERR "%s: not enough shared-mem for your configuration!\n",
776 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return 1;
778 }
Al Viro2d76c262008-03-19 09:43:29 +0000779 memset_io(p->xmit_cmds[i], 0,
Alan Coxa197f692008-02-08 15:27:38 +0000780 sizeof(struct transmit_cmd_struct));
Al Viro2d76c262008-03-19 09:43:29 +0000781 memset_io(p->xmit_buffs[i], 0,
Alan Coxa197f692008-02-08 15:27:38 +0000782 sizeof(struct tbd_struct));
783 writew(make16(p->nop_cmds[(i+1)%NUM_XMIT_BUFFS]),
784 &p->xmit_cmds[i]->cmd_link);
785 writew(STAT_COMPL, &p->xmit_cmds[i]->cmd_status);
786 writew(CMD_XMIT|CMD_INT, &p->xmit_cmds[i]->cmd_cmd);
787 writew(make16(p->xmit_buffs[i]), &p->xmit_cmds[i]->tbd_offset);
788 writew(0xffff, &p->xmit_buffs[i]->next);
789 writel(make24(p->xmit_cbuffs[i]), &p->xmit_buffs[i]->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791
792 p->xmit_count = 0;
793 p->xmit_last = 0;
794#ifndef NO_NOPCOMMANDS
795 p->nop_point = 0;
796#endif
797
798 /*
799 * 'start transmitter'
800 */
801#ifndef NO_NOPCOMMANDS
Alan Coxa197f692008-02-08 15:27:38 +0000802 writew(make16(p->nop_cmds[0]), &p->scb->cbl_offset);
803 writeb(CUC_START, &p->scb->cmd_cuc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 ni_attn586();
Alan Coxa197f692008-02-08 15:27:38 +0000805 wait_for_scb_cmd(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806#else
Alan Coxa197f692008-02-08 15:27:38 +0000807 writew(make16(p->xmit_cmds[0]), &p->xmit_cmds[0]->cmd_link);
808 writew(CMD_XMIT | CMD_SUSPEND | CMD_INT, &p->xmit_cmds[0]->cmd_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809#endif
810
811 /*
812 * ack. interrupts
813 */
Alan Coxa197f692008-02-08 15:27:38 +0000814 writeb(readb(&p->scb->cus) & STAT_MASK, &p->scb->cmd_cuc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 ni_attn586();
Alan Coxa197f692008-02-08 15:27:38 +0000816 udelay(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 ni_enaint();
819
820 return 0;
821}
822
823/******************************************************
824 * This is a helper routine for ni52_rnr_int() and init586().
825 * It sets up the Receive Frame Area (RFA).
826 */
827
Al Viro2d76c262008-03-19 09:43:29 +0000828static void __iomem *alloc_rfa(struct net_device *dev, void __iomem *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Al Viro2d76c262008-03-19 09:43:29 +0000830 struct rfd_struct __iomem *rfd = ptr;
831 struct rbd_struct __iomem *rbd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 int i;
Wang Chen4cf16532008-11-12 23:38:14 -0800833 struct priv *p = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Al Viro2d76c262008-03-19 09:43:29 +0000835 memset_io(rfd, 0,
Alan Coxa197f692008-02-08 15:27:38 +0000836 sizeof(struct rfd_struct) * (p->num_recv_buffs + rfdadd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 p->rfd_first = rfd;
838
Alan Coxa197f692008-02-08 15:27:38 +0000839 for (i = 0; i < (p->num_recv_buffs + rfdadd); i++) {
840 writew(make16(rfd + (i+1) % (p->num_recv_buffs+rfdadd)),
841 &rfd[i].next);
842 writew(0xffff, &rfd[i].rbd_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
Alan Coxa197f692008-02-08 15:27:38 +0000844 /* RU suspend */
845 writeb(RFD_SUSP, &rfd[p->num_recv_buffs-1+rfdadd].last);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Al Viro2d76c262008-03-19 09:43:29 +0000847 ptr = rfd + (p->num_recv_buffs + rfdadd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Al Viro2d76c262008-03-19 09:43:29 +0000849 rbd = ptr;
850 ptr = rbd + p->num_recv_buffs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 /* clr descriptors */
Al Viro2d76c262008-03-19 09:43:29 +0000853 memset_io(rbd, 0, sizeof(struct rbd_struct) * (p->num_recv_buffs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Alan Coxa197f692008-02-08 15:27:38 +0000855 for (i = 0; i < p->num_recv_buffs; i++) {
856 writew(make16(rbd + (i+1) % p->num_recv_buffs), &rbd[i].next);
857 writew(RECV_BUFF_SIZE, &rbd[i].size);
858 writel(make24(ptr), &rbd[i].buffer);
Al Viro2d76c262008-03-19 09:43:29 +0000859 ptr = ptr + RECV_BUFF_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 p->rfd_top = p->rfd_first;
862 p->rfd_last = p->rfd_first + (p->num_recv_buffs - 1 + rfdadd);
863
Alan Coxa197f692008-02-08 15:27:38 +0000864 writew(make16(p->rfd_first), &p->scb->rfa_offset);
865 writew(make16(rbd), &p->rfd_first->rbd_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 return ptr;
868}
869
870
871/**************************************************
872 * Interrupt Handler ...
873 */
874
Alan Coxa197f692008-02-08 15:27:38 +0000875static irqreturn_t ni52_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
877 struct net_device *dev = dev_id;
Alan Coxa197f692008-02-08 15:27:38 +0000878 unsigned int stat;
879 int cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 struct priv *p;
881
Wang Chen4cf16532008-11-12 23:38:14 -0800882 p = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Alan Coxa197f692008-02-08 15:27:38 +0000884 if (debuglevel > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 printk("I");
886
Alan Coxa197f692008-02-08 15:27:38 +0000887 spin_lock(&p->spinlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Alan Coxa197f692008-02-08 15:27:38 +0000889 wait_for_scb_cmd(dev); /* wait for last command */
890
891 while ((stat = readb(&p->scb->cus) & STAT_MASK)) {
892 writeb(stat, &p->scb->cmd_cuc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 ni_attn586();
894
Alan Coxa197f692008-02-08 15:27:38 +0000895 if (stat & STAT_FR) /* received a frame */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 ni52_rcv_int(dev);
897
Alan Coxa197f692008-02-08 15:27:38 +0000898 if (stat & STAT_RNR) { /* RU went 'not ready' */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 printk("(R)");
Alan Coxa197f692008-02-08 15:27:38 +0000900 if (readb(&p->scb->rus) & RU_SUSPEND) {
901 /* special case: RU_SUSPEND */
902 wait_for_scb_cmd(dev);
Al Viro05bd8312008-03-19 09:44:39 +0000903 writeb(RUC_RESUME, &p->scb->cmd_ruc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 ni_attn586();
Alan Coxa197f692008-02-08 15:27:38 +0000905 wait_for_scb_cmd_ruc(dev);
906 } else {
907 printk(KERN_ERR "%s: Receiver-Unit went 'NOT READY': %04x/%02x.\n",
908 dev->name, stat, readb(&p->scb->rus));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 ni52_rnr_int(dev);
910 }
911 }
912
Alan Coxa197f692008-02-08 15:27:38 +0000913 /* Command with I-bit set complete */
914 if (stat & STAT_CX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 ni52_xmt_int(dev);
916
917#ifndef NO_NOPCOMMANDS
Alan Coxa197f692008-02-08 15:27:38 +0000918 if (stat & STAT_CNA) { /* CU went 'not ready' */
919 if (netif_running(dev))
920 printk(KERN_ERR "%s: oops! CU has left active state. stat: %04x/%02x.\n",
921 dev->name, stat, readb(&p->scb->cus));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923#endif
924
Alan Coxa197f692008-02-08 15:27:38 +0000925 if (debuglevel > 1)
926 printk("%d", cnt++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Alan Coxa197f692008-02-08 15:27:38 +0000928 /* Wait for ack. (ni52_xmt_int can be faster than ack!!) */
929 wait_for_scb_cmd(dev);
Al Viro05bd8312008-03-19 09:44:39 +0000930 if (readb(&p->scb->cmd_cuc)) { /* timed out? */
Alan Coxa197f692008-02-08 15:27:38 +0000931 printk(KERN_ERR "%s: Acknowledge timed out.\n",
932 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 ni_disint();
934 break;
935 }
936 }
Alan Coxa197f692008-02-08 15:27:38 +0000937 spin_unlock(&p->spinlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Alan Coxa197f692008-02-08 15:27:38 +0000939 if (debuglevel > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 printk("i");
941 return IRQ_HANDLED;
942}
943
944/*******************************************************
945 * receive-interrupt
946 */
947
948static void ni52_rcv_int(struct net_device *dev)
949{
Alan Coxa197f692008-02-08 15:27:38 +0000950 int status, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 unsigned short totlen;
952 struct sk_buff *skb;
Al Viro2d76c262008-03-19 09:43:29 +0000953 struct rbd_struct __iomem *rbd;
Wang Chen4cf16532008-11-12 23:38:14 -0800954 struct priv *p = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Alan Coxa197f692008-02-08 15:27:38 +0000956 if (debuglevel > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 printk("R");
958
Alan Coxa197f692008-02-08 15:27:38 +0000959 for (; (status = readb(&p->rfd_top->stat_high)) & RFD_COMPL;) {
Al Viro05bd8312008-03-19 09:44:39 +0000960 rbd = make32(readw(&p->rfd_top->rbd_offset));
Alan Coxa197f692008-02-08 15:27:38 +0000961 if (status & RFD_OK) { /* frame received without error? */
962 totlen = readw(&rbd->status);
963 if (totlen & RBD_LAST) {
964 /* the first and the last buffer? */
965 totlen &= RBD_MASK; /* length of this frame */
966 writew(0x00, &rbd->status);
967 skb = (struct sk_buff *)dev_alloc_skb(totlen+2);
968 if (skb != NULL) {
969 skb_reserve(skb, 2);
970 skb_put(skb, totlen);
Al Viro05bd8312008-03-19 09:44:39 +0000971 memcpy_fromio(skb->data, p->base + readl(&rbd->buffer), totlen);
Alan Coxa197f692008-02-08 15:27:38 +0000972 skb->protocol = eth_type_trans(skb, dev);
973 netif_rx(skb);
Kulikov Vasiliy4265e662010-07-05 02:14:23 +0000974 dev->stats.rx_packets++;
975 dev->stats.rx_bytes += totlen;
Alan Coxa197f692008-02-08 15:27:38 +0000976 } else
Kulikov Vasiliy4265e662010-07-05 02:14:23 +0000977 dev->stats.rx_dropped++;
Alan Coxa197f692008-02-08 15:27:38 +0000978 } else {
979 int rstat;
980 /* free all RBD's until RBD_LAST is set */
981 totlen = 0;
982 while (!((rstat = readw(&rbd->status)) & RBD_LAST)) {
983 totlen += rstat & RBD_MASK;
984 if (!rstat) {
985 printk(KERN_ERR "%s: Whoops .. no end mark in RBD list\n", dev->name);
986 break;
987 }
988 writew(0, &rbd->status);
Al Viro05bd8312008-03-19 09:44:39 +0000989 rbd = make32(readw(&rbd->next));
Alan Coxa197f692008-02-08 15:27:38 +0000990 }
991 totlen += rstat & RBD_MASK;
992 writew(0, &rbd->status);
993 printk(KERN_ERR "%s: received oversized frame! length: %d\n",
994 dev->name, totlen);
Kulikov Vasiliy4265e662010-07-05 02:14:23 +0000995 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
Alan Coxa197f692008-02-08 15:27:38 +0000997 } else {/* frame !(ok), only with 'save-bad-frames' */
998 printk(KERN_ERR "%s: oops! rfd-error-status: %04x\n",
999 dev->name, status);
Kulikov Vasiliy4265e662010-07-05 02:14:23 +00001000 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
Alan Coxa197f692008-02-08 15:27:38 +00001002 writeb(0, &p->rfd_top->stat_high);
1003 writeb(RFD_SUSP, &p->rfd_top->last); /* maybe exchange by RFD_LAST */
1004 writew(0xffff, &p->rfd_top->rbd_offset);
1005 writeb(0, &p->rfd_last->last); /* delete RFD_SUSP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 p->rfd_last = p->rfd_top;
Al Viro05bd8312008-03-19 09:44:39 +00001007 p->rfd_top = make32(readw(&p->rfd_top->next)); /* step to next RFD */
Alan Coxa197f692008-02-08 15:27:38 +00001008 writew(make16(p->rfd_top), &p->scb->rfa_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Alan Coxa197f692008-02-08 15:27:38 +00001010 if (debuglevel > 0)
1011 printk("%d", cnt++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
1013
Alan Coxa197f692008-02-08 15:27:38 +00001014 if (automatic_resume) {
1015 wait_for_scb_cmd(dev);
1016 writeb(RUC_RESUME, &p->scb->cmd_ruc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 ni_attn586();
Alan Coxa197f692008-02-08 15:27:38 +00001018 wait_for_scb_cmd_ruc(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020
1021#ifdef WAIT_4_BUSY
1022 {
1023 int i;
Alan Coxa197f692008-02-08 15:27:38 +00001024 for (i = 0; i < 1024; i++) {
1025 if (p->rfd_top->status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 break;
Alan Coxa197f692008-02-08 15:27:38 +00001027 udelay(16);
1028 if (i == 1023)
1029 printk(KERN_ERR "%s: RU hasn't fetched next RFD (not busy/complete)\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031 }
1032#endif
Alan Coxa197f692008-02-08 15:27:38 +00001033 if (debuglevel > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 printk("r");
1035}
1036
1037/**********************************************************
1038 * handle 'Receiver went not ready'.
1039 */
1040
1041static void ni52_rnr_int(struct net_device *dev)
1042{
Wang Chen4cf16532008-11-12 23:38:14 -08001043 struct priv *p = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Kulikov Vasiliy4265e662010-07-05 02:14:23 +00001045 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Alan Coxa197f692008-02-08 15:27:38 +00001047 wait_for_scb_cmd(dev); /* wait for the last cmd, WAIT_4_FULLSTAT?? */
1048 writeb(RUC_ABORT, &p->scb->cmd_ruc); /* usually the RU is in the 'no resource'-state .. abort it now. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 ni_attn586();
Alan Coxa197f692008-02-08 15:27:38 +00001050 wait_for_scb_cmd_ruc(dev); /* wait for accept cmd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Al Viro2d76c262008-03-19 09:43:29 +00001052 alloc_rfa(dev, p->rfd_first);
Alan Coxa197f692008-02-08 15:27:38 +00001053 /* maybe add a check here, before restarting the RU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 startrecv586(dev); /* restart RU */
1055
Al Viro05bd8312008-03-19 09:44:39 +00001056 printk(KERN_ERR "%s: Receive-Unit restarted. Status: %04x\n",
1057 dev->name, readb(&p->scb->rus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059}
1060
1061/**********************************************************
1062 * handle xmit - interrupt
1063 */
1064
1065static void ni52_xmt_int(struct net_device *dev)
1066{
1067 int status;
Wang Chen4cf16532008-11-12 23:38:14 -08001068 struct priv *p = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Alan Coxa197f692008-02-08 15:27:38 +00001070 if (debuglevel > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 printk("X");
1072
Alan Coxa197f692008-02-08 15:27:38 +00001073 status = readw(&p->xmit_cmds[p->xmit_last]->cmd_status);
1074 if (!(status & STAT_COMPL))
1075 printk(KERN_ERR "%s: strange .. xmit-int without a 'COMPLETE'\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Alan Coxa197f692008-02-08 15:27:38 +00001077 if (status & STAT_OK) {
Kulikov Vasiliy4265e662010-07-05 02:14:23 +00001078 dev->stats.tx_packets++;
1079 dev->stats.collisions += (status & TCMD_MAXCOLLMASK);
Alan Coxa197f692008-02-08 15:27:38 +00001080 } else {
Kulikov Vasiliy4265e662010-07-05 02:14:23 +00001081 dev->stats.tx_errors++;
Alan Coxa197f692008-02-08 15:27:38 +00001082 if (status & TCMD_LATECOLL) {
1083 printk(KERN_ERR "%s: late collision detected.\n",
1084 dev->name);
Kulikov Vasiliy4265e662010-07-05 02:14:23 +00001085 dev->stats.collisions++;
Alan Coxa197f692008-02-08 15:27:38 +00001086 } else if (status & TCMD_NOCARRIER) {
Kulikov Vasiliy4265e662010-07-05 02:14:23 +00001087 dev->stats.tx_carrier_errors++;
Alan Coxa197f692008-02-08 15:27:38 +00001088 printk(KERN_ERR "%s: no carrier detected.\n",
1089 dev->name);
1090 } else if (status & TCMD_LOSTCTS)
1091 printk(KERN_ERR "%s: loss of CTS detected.\n",
1092 dev->name);
1093 else if (status & TCMD_UNDERRUN) {
Kulikov Vasiliy4265e662010-07-05 02:14:23 +00001094 dev->stats.tx_fifo_errors++;
Alan Coxa197f692008-02-08 15:27:38 +00001095 printk(KERN_ERR "%s: DMA underrun detected.\n",
1096 dev->name);
1097 } else if (status & TCMD_MAXCOLL) {
1098 printk(KERN_ERR "%s: Max. collisions exceeded.\n",
1099 dev->name);
Kulikov Vasiliy4265e662010-07-05 02:14:23 +00001100 dev->stats.collisions += 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
1102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103#if (NUM_XMIT_BUFFS > 1)
Alan Coxa197f692008-02-08 15:27:38 +00001104 if ((++p->xmit_last) == NUM_XMIT_BUFFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 p->xmit_last = 0;
1106#endif
1107 netif_wake_queue(dev);
1108}
1109
1110/***********************************************************
1111 * (re)start the receiver
1112 */
1113
1114static void startrecv586(struct net_device *dev)
1115{
Wang Chen4cf16532008-11-12 23:38:14 -08001116 struct priv *p = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Alan Coxa197f692008-02-08 15:27:38 +00001118 wait_for_scb_cmd(dev);
1119 wait_for_scb_cmd_ruc(dev);
1120 writew(make16(p->rfd_first), &p->scb->rfa_offset);
1121 writeb(RUC_START, &p->scb->cmd_ruc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 ni_attn586(); /* start cmd. */
Alan Coxa197f692008-02-08 15:27:38 +00001123 wait_for_scb_cmd_ruc(dev);
1124 /* wait for accept cmd. (no timeout!!) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
1127static void ni52_timeout(struct net_device *dev)
1128{
Wang Chen4cf16532008-11-12 23:38:14 -08001129 struct priv *p = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130#ifndef NO_NOPCOMMANDS
Alan Coxa197f692008-02-08 15:27:38 +00001131 if (readb(&p->scb->cus) & CU_ACTIVE) { /* COMMAND-UNIT active? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 netif_wake_queue(dev);
1133#ifdef DEBUG
Alan Coxa197f692008-02-08 15:27:38 +00001134 printk(KERN_ERR "%s: strange ... timeout with CU active?!?\n",
1135 dev->name);
1136 printk(KERN_ERR "%s: X0: %04x N0: %04x N1: %04x %d\n",
1137 dev->name, (int)p->xmit_cmds[0]->cmd_status,
1138 readw(&p->nop_cmds[0]->cmd_status),
1139 readw(&p->nop_cmds[1]->cmd_status),
1140 p->nop_point);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141#endif
Alan Coxa197f692008-02-08 15:27:38 +00001142 writeb(CUC_ABORT, &p->scb->cmd_cuc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 ni_attn586();
Alan Coxa197f692008-02-08 15:27:38 +00001144 wait_for_scb_cmd(dev);
1145 writew(make16(p->nop_cmds[p->nop_point]), &p->scb->cbl_offset);
1146 writeb(CUC_START, &p->scb->cmd_cuc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 ni_attn586();
Alan Coxa197f692008-02-08 15:27:38 +00001148 wait_for_scb_cmd(dev);
Eric Dumazet1ae5dc32010-05-10 05:01:31 -07001149 dev->trans_start = jiffies; /* prevent tx timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return 0;
1151 }
1152#endif
1153 {
1154#ifdef DEBUG
Alan Coxa197f692008-02-08 15:27:38 +00001155 printk(KERN_ERR "%s: xmitter timed out, try to restart! stat: %02x\n",
1156 dev->name, readb(&p->scb->cus));
1157 printk(KERN_ERR "%s: command-stats: %04x %04x\n",
1158 dev->name,
1159 readw(&p->xmit_cmds[0]->cmd_status),
1160 readw(&p->xmit_cmds[1]->cmd_status));
1161 printk(KERN_ERR "%s: check, whether you set the right interrupt number!\n",
1162 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163#endif
1164 ni52_close(dev);
1165 ni52_open(dev);
1166 }
Eric Dumazet1ae5dc32010-05-10 05:01:31 -07001167 dev->trans_start = jiffies; /* prevent tx timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
1170/******************************************************
1171 * send frame
1172 */
1173
Stephen Hemminger613573252009-08-31 19:50:58 +00001174static netdev_tx_t ni52_send_packet(struct sk_buff *skb,
1175 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Alan Coxa197f692008-02-08 15:27:38 +00001177 int len, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178#ifndef NO_NOPCOMMANDS
1179 int next_nop;
1180#endif
Wang Chen4cf16532008-11-12 23:38:14 -08001181 struct priv *p = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Alan Coxa197f692008-02-08 15:27:38 +00001183 if (skb->len > XMIT_BUFF_SIZE) {
1184 printk(KERN_ERR "%s: Sorry, max. framelength is %d bytes. The length of your frame is %d bytes.\n", dev->name, XMIT_BUFF_SIZE, skb->len);
Patrick McHardyec634fe2009-07-05 19:23:38 -07001185 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187
1188 netif_stop_queue(dev);
1189
Al Viro05bd8312008-03-19 09:44:39 +00001190 memcpy_toio(p->xmit_cbuffs[p->xmit_count], skb->data, skb->len);
Alan Coxa197f692008-02-08 15:27:38 +00001191 len = skb->len;
1192 if (len < ETH_ZLEN) {
1193 len = ETH_ZLEN;
Al Viro05bd8312008-03-19 09:44:39 +00001194 memset_io(p->xmit_cbuffs[p->xmit_count]+skb->len, 0,
Alan Coxa197f692008-02-08 15:27:38 +00001195 len - skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198#if (NUM_XMIT_BUFFS == 1)
1199# ifdef NO_NOPCOMMANDS
1200
1201#ifdef DEBUG
Al Viro05bd8312008-03-19 09:44:39 +00001202 if (readb(&p->scb->cus) & CU_ACTIVE) {
Alan Coxa197f692008-02-08 15:27:38 +00001203 printk(KERN_ERR "%s: Hmmm .. CU is still running and we wanna send a new packet.\n", dev->name);
1204 printk(KERN_ERR "%s: stat: %04x %04x\n",
1205 dev->name, readb(&p->scb->cus),
1206 readw(&p->xmit_cmds[0]->cmd_status));
1207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208#endif
Al Viro05bd8312008-03-19 09:44:39 +00001209 writew(TBD_LAST | len, &p->xmit_buffs[0]->size);
Alan Coxa197f692008-02-08 15:27:38 +00001210 for (i = 0; i < 16; i++) {
1211 writew(0, &p->xmit_cmds[0]->cmd_status);
1212 wait_for_scb_cmd(dev);
1213 if ((readb(&p->scb->cus) & CU_STATUS) == CU_SUSPEND)
1214 writeb(CUC_RESUME, &p->scb->cmd_cuc);
1215 else {
1216 writew(make16(p->xmit_cmds[0]), &p->scb->cbl_offset);
1217 writeb(CUC_START, &p->scb->cmd_cuc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
Alan Coxa197f692008-02-08 15:27:38 +00001219 ni_attn586();
Alan Coxa197f692008-02-08 15:27:38 +00001220 if (!i)
1221 dev_kfree_skb(skb);
1222 wait_for_scb_cmd(dev);
1223 /* test it, because CU sometimes doesn't start immediately */
1224 if (readb(&p->scb->cus) & CU_ACTIVE)
1225 break;
1226 if (readw(&p->xmit_cmds[0]->cmd_status))
1227 break;
1228 if (i == 15)
1229 printk(KERN_WARNING "%s: Can't start transmit-command.\n", dev->name);
1230 }
1231# else
1232 next_nop = (p->nop_point + 1) & 0x1;
1233 writew(TBD_LAST | len, &p->xmit_buffs[0]->size);
1234 writew(make16(p->nop_cmds[next_nop]), &p->xmit_cmds[0]->cmd_link);
1235 writew(make16(p->nop_cmds[next_nop]),
1236 &p->nop_cmds[next_nop]->cmd_link);
1237 writew(0, &p->xmit_cmds[0]->cmd_status);
1238 writew(0, &p->nop_cmds[next_nop]->cmd_status);
1239
1240 writew(make16(p->xmit_cmds[0]), &p->nop_cmds[p->nop_point]->cmd_link);
Alan Coxa197f692008-02-08 15:27:38 +00001241 p->nop_point = next_nop;
1242 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243# endif
1244#else
Alan Coxa197f692008-02-08 15:27:38 +00001245 writew(TBD_LAST | len, &p->xmit_buffs[p->xmit_count]->size);
1246 next_nop = p->xmit_count + 1
1247 if (next_nop == NUM_XMIT_BUFFS)
1248 next_nop = 0;
1249 writew(0, &p->xmit_cmds[p->xmit_count]->cmd_status);
1250 /* linkpointer of xmit-command already points to next nop cmd */
1251 writew(make16(p->nop_cmds[next_nop]),
1252 &p->nop_cmds[next_nop]->cmd_link);
1253 writew(0, &p->nop_cmds[next_nop]->cmd_status);
1254 writew(make16(p->xmit_cmds[p->xmit_count]),
1255 &p->nop_cmds[p->xmit_count]->cmd_link);
Alan Coxa197f692008-02-08 15:27:38 +00001256 p->xmit_count = next_nop;
1257 {
1258 unsigned long flags;
1259 spin_lock_irqsave(&p->spinlock);
1260 if (p->xmit_count != p->xmit_last)
1261 netif_wake_queue(dev);
1262 spin_unlock_irqrestore(&p->spinlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
Alan Coxa197f692008-02-08 15:27:38 +00001264 dev_kfree_skb(skb);
1265#endif
Patrick McHardyec634fe2009-07-05 19:23:38 -07001266 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
1269/*******************************************
1270 * Someone wanna have the statistics
1271 */
1272
1273static struct net_device_stats *ni52_get_stats(struct net_device *dev)
1274{
Wang Chen4cf16532008-11-12 23:38:14 -08001275 struct priv *p = netdev_priv(dev);
Alan Coxa197f692008-02-08 15:27:38 +00001276 unsigned short crc, aln, rsc, ovrn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Alan Coxa197f692008-02-08 15:27:38 +00001278 /* Get error-statistics from the ni82586 */
1279 crc = readw(&p->scb->crc_errs);
1280 writew(0, &p->scb->crc_errs);
1281 aln = readw(&p->scb->aln_errs);
1282 writew(0, &p->scb->aln_errs);
1283 rsc = readw(&p->scb->rsc_errs);
1284 writew(0, &p->scb->rsc_errs);
1285 ovrn = readw(&p->scb->ovrn_errs);
1286 writew(0, &p->scb->ovrn_errs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Kulikov Vasiliy4265e662010-07-05 02:14:23 +00001288 dev->stats.rx_crc_errors += crc;
1289 dev->stats.rx_fifo_errors += ovrn;
1290 dev->stats.rx_frame_errors += aln;
1291 dev->stats.rx_dropped += rsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Kulikov Vasiliy4265e662010-07-05 02:14:23 +00001293 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
1296/********************************************************
1297 * Set MC list ..
1298 */
1299
1300static void set_multicast_list(struct net_device *dev)
1301{
1302 netif_stop_queue(dev);
1303 ni_disint();
1304 alloc586(dev);
1305 init586(dev);
1306 startrecv586(dev);
1307 ni_enaint();
1308 netif_wake_queue(dev);
1309}
1310
1311#ifdef MODULE
1312static struct net_device *dev_ni52;
1313
1314module_param(io, int, 0);
1315module_param(irq, int, 0);
1316module_param(memstart, long, 0);
1317module_param(memend, long, 0);
1318MODULE_PARM_DESC(io, "NI5210 I/O base address,required");
1319MODULE_PARM_DESC(irq, "NI5210 IRQ number,required");
1320MODULE_PARM_DESC(memstart, "NI5210 memory base address,required");
1321MODULE_PARM_DESC(memend, "NI5210 memory end address,required");
1322
Andrew Morton19a86642006-08-14 23:00:05 -07001323int __init init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Alan Coxa197f692008-02-08 15:27:38 +00001325 if (io <= 0x0 || !memend || !memstart || irq < 2) {
1326 printk(KERN_ERR "ni52: Autoprobing not allowed for modules.\n");
1327 printk(KERN_ERR "ni52: Set symbols 'io' 'irq' 'memstart' and 'memend'\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return -ENODEV;
1329 }
1330 dev_ni52 = ni52_probe(-1);
1331 if (IS_ERR(dev_ni52))
1332 return PTR_ERR(dev_ni52);
1333 return 0;
1334}
1335
Al Viroafc8eb42006-06-14 18:50:53 -04001336void __exit cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Wang Chen4cf16532008-11-12 23:38:14 -08001338 struct priv *p = netdev_priv(dev_ni52);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 unregister_netdev(dev_ni52);
Al Viro176f65f2008-03-19 09:44:29 +00001340 iounmap(p->mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 release_region(dev_ni52->base_addr, NI52_TOTAL_SIZE);
1342 free_netdev(dev_ni52);
1343}
1344#endif /* MODULE */
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346MODULE_LICENSE("GPL");