1 /* eexpress.c: Intel EtherExpress device driver for Linux. */
2 /*
3 Written 1993 by Donald Becker.
4 Copyright 1993 United States Government as represented by the Director,
5 National Security Agency. This software may only be used and distributed
6 according to the terms of the GNU Public License as modified by SRC,
7 incorported herein by reference.
8
9 The author may be reached as becker@super.org or
10 C/O Supercomputing Research Ctr., 17100 Science Dr., Bowie MD 20715
11
12 Things remaining to do:
13 Check that the 586 and ASIC are reset/unreset at the right times.
14 Check tx and rx buffer setup.
15 The current Tx is single-buffer-only.
16 Move the theory of operation and memory map documentation.
17 Rework the board error reset
18 The statistics need to be updated correctly.
19 */
20
21 static char *version =
22 "eexpress.c:v0.06 10/27/93 Donald Becker (becker@super.org)\n";
23
24 #include <linux/config.h>
25
26 /*
27 Sources:
28 This driver wouldn't have been written with the availability of the
29 Crynwr driver source code. It provided a known-working implementation
30 that filled in the gaping holes of the Intel documention. Three cheers
31 for Russ Nelson.
32
33 Intel Microcommunications Databook, Vol. 1, 1990. It provides just enough
34 info that the casual reader might think that it documents the i82586.
35 */
36
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/types.h>
40 #include <linux/fcntl.h>
41 #include <linux/interrupt.h>
42 #include <linux/ptrace.h>
43 #include <linux/ioport.h>
44 #include <linux/in.h>
45 #include <asm/system.h>
46 #include <asm/bitops.h>
47 #include <asm/io.h>
48 #include <asm/dma.h>
49 #include <errno.h>
50 #include <memory.h>
51
52 #include "dev.h"
53 #include "iow.h"
54 #include "eth.h"
55 #include "skbuff.h"
56 #include "arp.h"
57
58 #ifndef HAVE_ALLOC_SKB
59 #define alloc_skb(size, priority) (struct sk_buff *) kmalloc(size,priority)
60 #else
61 /* This isn't quite right, but it's the best version define I can find right now. */
62 #include <linux/malloc.h>
63 #endif
64
65 /* use 0 for production, 1 for verification, 2..7 for debug */
66 #ifndef NET_DEBUG
67 #define NET_DEBUG 2
68 #endif
69 static unsigned int net_debug = NET_DEBUG;
70
71 /*
72 Details of the i82586.
73
74 You'll really need the databook to understand the details of this part,
75 but the outline is that the i82586 has two seperate processing units.
76
77 The Rx unit uses a list of frame descriptors and a list of data buffer
78 descriptors. We use full-sized (1518 byte) data buffers, so there is
79 a one-to-one pairing of frame descriptors to buffer descriptors.
80
81 The Tx ("command") unit executes a list of commands that look like:
82 Status word Written by the 82586 when the command is done.
83 Command word Command in lower 3 bits, post-command action in upper 3
84 Link word The address of the next command.
85 Parameters (as needed).
86
87 Some definitions related to the Command Word are:
88 */
89 #define CMD_EOL 0x8000 /* The last command of the list, stop. */
90 #define CMD_SUSP 0x4000 /* Suspend after doing cmd. */
91 #define CMD_INTR 0x2000 /* Interrupt after doing cmd. */
92
93 enum commands {
94 CmdNOp = 0, CmdSASetup = 1, CmdConfigure = 2, CmdMulticastList = 3,
95 CmdTx = 4, CmdTDR = 5, CmdDump = 6, CmdDiagnose = 7};
96
97 /* Information that need to be kept for each board. */
98 struct net_local {
99 struct enet_statistics stats;
100 int last_restart;
101 short rx_head;
102 short rx_tail;
103 short tx_head;
104 short tx_cmd_link;
105 short tx_reap;
106 };
107
108 /*
109 Details of the EtherExpress Implementation
110 The EtherExpress takes an unusual approach to host access to packet buffer
111 memory. The host can use either the Dataport, with independent
112 autoincrementing read and write pointers, or it can I/O map 32 bytes of the
113 memory using the "Shadow Memory Pointer" (SMB) as follows:
114 ioaddr Normal EtherExpress registers
115 ioaddr+0x4000...0x400f Buffer Memory at SMB...SMB+15
116 ioaddr+0x8000...0x800f Buffer Memory at SMB+16...SMB+31
117 ioaddr+0xC000...0xC007 "" SMB+16...SMB+23 (hardware flaw?)
118 ioaddr+0xC008...0xC00f Buffer Memory at 0x0008...0x000f
119 The last I/O map set is useful if you put the i82586 System Command Block
120 (the command mailbox) exactly at 0x0008. (There seems to be some
121 undocumented init structure at 0x0000-7, so I had to use the Crywnr memory
122 setup verbatim for those four words anyway.)
123
124 A problem with using either one of these mechanisms is that you must run
125 single-threaded, or the interrupt handler must restore a changed value of
126 the read, write, or SMB pointers.
127
128 Unlike the Crynwr driver, my driver mostly ignores the I/O mapped "feature"
129 and relies heavily on the dataport for buffer memory access. To minimize
130 switching, the read_pointer is dedicated to the Rx interrupt handler, and
131 the write_pointer is used by the send_packet() routine (it's carefully saved
132 and restored when it's needed by the interrupt handler).
133 */
134
135 /* Offsets from the base I/O address. */
136 #define DATAPORT 0 /* Data Transfer Register. */
137 #define WRITE_PTR 2 /* Write Address Pointer. */
138 #define READ_PTR 4 /* Read Address Pointer. */
139 #define SIGNAL_CA 6 /* Frob the 82586 Channel Attention line. */
140 #define SET_IRQ 7 /* IRQ Select. */
141 #define SHADOW_PTR 8 /* Shadow Memory Bank Pointer. */
142 #define MEM_Ctrl 11
143 #define MEM_Page_Ctrl 12
144 #define Config 13
145 #define EEPROM_Ctrl 14
146 #define ID_PORT 15
147
148 /* EEPROM_Ctrl bits. */
149
150 #define EE_SHIFT_CLK 0x01 /* EEPROM shift clock. */
151 #define EE_CS 0x02 /* EEPROM chip select. */
152 #define EE_DATA_WRITE 0x04 /* EEPROM chip data in. */
153 #define EE_DATA_READ 0x08 /* EEPROM chip data out. */
154 #define EE_CTRL_BITS (EE_SHIFT_CLK | EE_CS | EE_DATA_WRITE | EE_DATA_READ)
155 #define ASIC_RESET 0x40
156 #define _586_RESET 0x80
157
158 /* Offsets into the System Control Block structure. */
159 #define SCB_STATUS 0xc008
160 #define SCB_CMD 0xc00A
161 #define CUC_START 0x0100
162 #define CUC_RESUME 0x0200
163 #define CUC_SUSPEND 0x0300
164 #define RX_START 0x0010
165 #define RX_RESUME 0x0020
166 #define RX_SUSPEND 0x0030
167 #define SCB_CBL 0xc00C /* Command BLock offset. */
168 #define SCB_RFA 0xc00E /* Rx Frame Area offset. */
169
170 /*
171 What follows in 'init_words[]' is the "program" that is downloaded to the
172 82586 memory. It's mostly tables and command blocks, and starts at the
173 reset address 0xfffff6.
174
175 Even with the additional "don't care" values, doing it this way takes less
176 program space than initializing the individual tables, and I feel it's much
177 cleaner.
178
179 The databook is particularly useless for the first two structures, I had
180 to use the Crynwr driver as an example.
181
182 The memory setup is as follows:
183 */
184
185 #define CONFIG_CMD 0x0018
186 #define SET_SA_CMD 0x0024
187 #define SA_OFFSET 0x002A
188 #define IDLELOOP 0x30
189 #define TDR_CMD 0x38
190 #define TDR_TIME 0x3C
191 #define DUMP_CMD 0x40
192 #define DIAG_CMD 0x48
193 #define SET_MC_CMD 0x4E
194 #define DUMP_DATA 0x56 /* A 170 byte buffer for dump and Set-MC into. */
195
196 #define TX_BUF_START 0x0100
197 #define NUM_TX_BUFS 4
198 #define TX_BUF_SIZE (1518+14+20+16) /* packet+header+TBD */
199
200 #define RX_BUF_START 0x2000
201 #define RX_BUF_SIZE (0x640) /* packet+header+RBD+extra */
202 #define RX_BUF_END 0x8000
203
204 /*
205 That's it: only 86 bytes to set up the beast, including every extra
206 command available. The 170 byte buffer at DUMP_DATA is shared between the
207 Dump command (called only by the diagnostic program) and the SetMulticastList
208 command.
209
210 To complete the memory setup you only have to write the station address at
211 SA_OFFSET and create the Tx & Rx buffer lists.
212
213 The Tx command chain and buffer list is setup as follows:
214 A Tx command table, with the data buffer pointing to...
215 A Tx data buffer descriptor. The packet is in a single buffer, rather than
216 chaining together several smaller buffers.
217 A NoOp command, which initially points to itself,
218 And the packet data.
219
220 A transmit is done by filling in the Tx command table and data buffer,
221 re-writing the NoOp command, and finally changing the offset of the last
222 command to point to the current Tx command. When the Tx command is finished,
223 it jumps to the NoOp, when it loops until the next Tx command changes the
224 "link offset" in the NoOp. This way the 82586 never has to go through the
225 slow restart sequence.
226
227 The Rx buffer list is set up in the obvious ring structure. We have enough
228 memory (and low enough interrupt latency) that we can avoid the complicated
229 Rx buffer linked lists by alway associating a full-size Rx data buffer with
230 each Rx data frame.
231
232 I current use four transmit buffers starting at TX_BUF_START (0x0100), and
233 use the rest of memory, from RX_BUF_START to RX_BUF_END, for Rx buffers.
234
235 */
236
237 static short init_words[] = {
238 0x0000, /* Set bus size to 16 bits. */
239 0x0000,0x0000, /* Set control mailbox (SCB) addr. */
240 0,0, /* pad to 0x000000. */
241 0x0001, /* Status word that's cleared when init is done. */
242 0x0008,0,0, /* SCB offset, (skip, skip) */
243
244 0,0xf000|RX_START|CUC_START, /* SCB status and cmd. */
245 CONFIG_CMD, /* Command list pointer, points to Configure. */
246 RX_BUF_START, /* Rx block list. */
247 0,0,0,0, /* Error count: CRC, align, buffer, overrun. */
248
249 /* 0x0018: Configure command. Change to put MAC data with packet. */
250 0, CmdConfigure, /* Status, command. */
251 SET_SA_CMD, /* Next command is Set Station Addr. */
252 0x0804, /* "4" bytes of config data, 8 byte FIFO. */
253 0x2e40, /* Magic values, including MAC data location. */
254 0, /* Unused pad word. */
255
256 /* 0x0024: Setup station address command. */
257 0, CmdSASetup,
258 SET_MC_CMD, /* Next command. */
259 0xaa00,0xb000,0x0bad, /* Station address (to be filled in) */
260
261 /* 0x0030: NOP, looping back to itself. Point to first Tx buffer to Tx. */
262 0, CmdNOp, IDLELOOP, 0 /* pad */,
263
264 /* 0x0038: A unused Time-Domain Reflectometer command. */
265 0, CmdTDR, IDLELOOP, 0,
266
267 /* 0x0040: An unused Dump State command. */
268 0, CmdDump, IDLELOOP, DUMP_DATA,
269
270 /* 0x0048: An unused Diagnose command. */
271 0, CmdDiagnose, IDLELOOP,
272
273 /* 0x004E: An empty set-multicast-list command. */
274 #ifdef initial_text_tx
275 0, CmdMulticastList, DUMP_DATA, 0,
276 #else
277 0, CmdMulticastList, IDLELOOP, 0,
278 #endif
279
280 /* 0x0056: A continuous transmit command, only here for testing. */
281 0, CmdTx, DUMP_DATA, DUMP_DATA+8, 0x803ff, -1, DUMP_DATA, 0,
282 };
283
284 /* Index to functions, as function prototypes. */
285
286 extern int express_probe(struct device *dev); /* Called from Space.c */
287
288 static int eexp_probe1(struct device *dev, short ioaddr);
289 static int eexp_open(struct device *dev);
290 static int eexp_send_packet(struct sk_buff *skb, struct device *dev);
291 static void eexp_interrupt(int reg_ptr);
292 static void eexp_rx(struct device *dev);
293 static int eexp_close(struct device *dev);
294 static struct enet_statistics *eexp_get_stats(struct device *dev);
295 #ifdef HAVE_MULTICAST
296 static void set_multicast_list(struct device *dev, int num_addrs, void *addrs);
297 #endif
298
299 static int read_eeprom(int ioaddr, int location);
300 static void hardware_send_packet(struct device *dev, void *buf, short length);
301 static void init_82586_mem(struct device *dev);
302 static void init_rx_bufs(struct device *dev);
303
304
305 /* Check for a network adaptor of this type, and return '0' iff one exists.
306 If dev->base_addr == 0, probe all likely locations.
307 If dev->base_addr == 1, always return failure.
308 If dev->base_addr == 2, (detachable devices only) alloate space for the
309 device and return success.
310 */
311 int
312 express_probe(struct device *dev)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
313 {
314 /* Don't probe all settable addresses, 0x[23][0-7]0, just common ones. */
315 int *port, ports[] = {0x300, 0x270, 0x320, 0x340, 0};
316 int base_addr = dev->base_addr;
317
318 if (base_addr > 0x1ff) /* Check a single specified location. */
319 return eexp_probe1(dev, base_addr);
320 else if (base_addr > 0)
321 return ENXIO; /* Don't probe at all. */
322
323 for (port = &ports[0]; *port; port++) {
324 short id_addr = *port + ID_PORT;
325 unsigned short sum = 0;
326 int i;
327 #ifdef notdef
328 for (i = 16; i > 0; i--)
329 sum += inb(id_addr);
330 #else
331 for (i = 4; i > 0; i--) {
332 short id_val = inb(id_addr);
333 sum |= (id_val >> 4) << ((id_val & 3) << 2);
334 }
335 #endif
336 if (sum == 0xbaba
337 && eexp_probe1(dev, *port) == 0)
338 return 0;
339 }
340
341 return ENODEV; /* ENODEV would be more accurate. */
342 }
343
344 int eexp_probe1(struct device *dev, short ioaddr)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
345 {
346 unsigned short station_addr[3];
347 int i;
348
349 printk("%s: EtherExpress at %#x,", dev->name, ioaddr);
350
351 /* The station address is stored !backwards! in the EEPROM, reverse
352 after reading. (Hmmm, a little brain-damage there at Intel, eh?) */
353 station_addr[0] = read_eeprom(ioaddr, 2);
354 station_addr[1] = read_eeprom(ioaddr, 3);
355 station_addr[2] = read_eeprom(ioaddr, 4);
356
357 /* Check the first three octets of the S.A. for the manufactor's code. */
358 if (station_addr[2] != 0x00aa || (station_addr[1] & 0xff00) != 0x0000) {
359 printk(" rejected (invalid address %04x%04x%04x).\n",
360 station_addr[2], station_addr[1], station_addr[0]);
361 return ENODEV;
362 }
363
364 /* We've committed to using the board, and can start filling in *dev. */
365 snarf_region(ioaddr, 16);
366 dev->base_addr = ioaddr;
367
368 for (i = 0; i < 6; i++) {
369 dev->dev_addr[i] = ((unsigned char*)station_addr)[5-i];
370 printk(" %02x", dev->dev_addr[i]);
371 }
372
373 /* There is no reason for the driver to care, but I print out the
374 interface to minimize bogus bug reports. */
375 {
376 char irqmap[] = {0, 9, 3, 4, 5, 10, 11, 0};
377 char *ifmap[] = {"AUI", "BNC", "10baseT"};
378 enum iftype {AUI=0, BNC=1, TP=2};
379 unsigned short setupval = read_eeprom(ioaddr, 0);
380
381 dev->irq = irqmap[setupval >> 13];
382 dev->if_port = (setupval & 0x1000) == 0 ? AUI :
383 read_eeprom(ioaddr, 5) & 0x1 ? TP : BNC;
384 printk(", IRQ %d, Interface %s.\n", dev->irq, ifmap[dev->if_port]);
385 /* Release the IRQ line so that it can be shared if we don't use the
386 ethercard. */
387 outb(0x00, ioaddr + SET_IRQ);
388 }
389
390 /* It's now OK to leave the board in reset, pending the open(). */
391 outb(ASIC_RESET, ioaddr + EEPROM_Ctrl);
392
393 if ((dev->mem_start & 0xf) > 0)
394 net_debug = dev->mem_start & 7;
395
396 if (net_debug)
397 printk(version);
398
399 /* Initialize the device structure. */
400 dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
401 memset(dev->priv, 0, sizeof(struct net_local));
402
403 dev->open = eexp_open;
404 dev->stop = eexp_close;
405 dev->hard_start_xmit = eexp_send_packet;
406 dev->get_stats = eexp_get_stats;
407 #ifdef HAVE_MULTICAST
408 dev->set_multicast_list = &set_multicast_list;
409 #endif
410
411 /* Fill in the fields of the device structure with ethernet-generic values.
412 This should be in a common file instead of per-driver. */
413 for (i = 0; i < DEV_NUMBUFFS; i++)
414 dev->buffs[i] = NULL;
415
416 dev->hard_header = eth_header;
417 dev->add_arp = eth_add_arp;
418 dev->queue_xmit = dev_queue_xmit;
419 dev->rebuild_header = eth_rebuild_header;
420 dev->type_trans = eth_type_trans;
421
422 dev->type = ARPHRD_ETHER;
423 dev->hard_header_len = ETH_HLEN;
424 dev->mtu = 1500; /* eth_mtu */
425 dev->addr_len = ETH_ALEN;
426 for (i = 0; i < ETH_ALEN; i++) {
427 dev->broadcast[i]=0xff;
428 }
429
430 /* New-style flags. */
431 dev->flags = IFF_BROADCAST;
432 dev->family = AF_INET;
433 dev->pa_addr = 0;
434 dev->pa_brdaddr = 0;
435 dev->pa_mask = 0;
436 dev->pa_alen = sizeof(unsigned long);
437
438 return 0;
439 }
440
441
442 /* Reverse IRQ map: the value to put in the SET_IRQ reg. for IRQ<index>. */
443 static char irqrmap[]={0,0,1,2,3,4,0,0,0,1,5,6,0,0,0,0};
444
445 static int
446 eexp_open(struct device *dev)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
447 {
448 int ioaddr = dev->base_addr;
449
450 if (dev->irq == 0 || irqrmap[dev->irq] == 0)
451 return -ENXIO;
452
453 if (irq2dev_map[dev->irq] != 0
454 /* This is always true, but avoid the false IRQ. */
455 || (irq2dev_map[dev->irq] = dev) == 0
456 || request_irq(dev->irq, &eexp_interrupt)) {
457 return -EAGAIN;
458 }
459
460 /* Initialize the 82586 memory and start it. */
461 init_82586_mem(dev);
462
463 /* Enable the interrupt line. */
464 outb(irqrmap[dev->irq] | 0x08, ioaddr + SET_IRQ);
465
466 dev->tbusy = 0;
467 dev->interrupt = 0;
468 dev->start = 1;
469 return 0;
470 }
471
472 static int
473 eexp_send_packet(struct sk_buff *skb, struct device *dev)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
474 {
475 struct net_local *lp = (struct net_local *)dev->priv;
476 int ioaddr = dev->base_addr;
477
478 if (dev->tbusy) {
479 /* If we get here, some higher level has decided we are broken.
480 There should really be a "kick me" function call instead. */
481 int tickssofar = jiffies - dev->trans_start;
482 if (tickssofar < 5)
483 return 1;
484 if (net_debug > 1)
485 printk("%s: transmit timed out, %s? ", dev->name,
486 inw(ioaddr+SCB_STATUS) & 0x8000 ? "IRQ conflict" :
487 "network cable problem");
488 lp->stats.tx_errors++;
489 /* Try to restart the adaptor. */
490 if (lp->last_restart == lp->stats.tx_packets) {
491 if (net_debug > 1) printk("Resetting board.\n");
492 /* Completely reset the adaptor. */
493 init_82586_mem(dev);
494 } else {
495 /* Issue the channel attention signal and hope it "gets better". */
496 if (net_debug > 1) printk("Kicking board.\n");
497 outw(0xf000|CUC_START|RX_START, ioaddr + SCB_CMD);
498 outb(0, ioaddr + SIGNAL_CA);
499 lp->last_restart = lp->stats.tx_packets;
500 }
501 dev->tbusy=0;
502 dev->trans_start = jiffies;
503 }
504
505 /* If some higher layer thinks we've missed an tx-done interrupt
506 we are passed NULL. Caution: dev_tint() handles the cli()/sti()
507 itself. */
508 if (skb == NULL) {
509 dev_tint(dev);
510 return 0;
511 }
512
513 /* For ethernet, fill in the header. This should really be done by a
514 higher level, rather than duplicated for each ethernet adaptor. */
515 if (!skb->arp && dev->rebuild_header(skb+1, dev)) {
516 skb->dev = dev;
517 arp_queue (skb);
518 return 0;
519 }
520 skb->arp=1;
521
522 /* Block a timer-based transmit from overlapping. */
523 if (set_bit(0, (void*)&dev->tbusy) != 0)
524 printk("%s: Transmitter access conflict.\n", dev->name);
525 else {
526 short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
527 unsigned char *buf = (void *)(skb+1);
528
529 /* Disable the 82586's input to the interrupt line. */
530 outb(irqrmap[dev->irq], ioaddr + SET_IRQ);
531 hardware_send_packet(dev, buf, length);
532 dev->trans_start = jiffies;
533 /* Enable the 82586 interrupt input. */
534 outb(0x08 | irqrmap[dev->irq], ioaddr + SET_IRQ);
535 }
536
537 if (skb->free)
538 kfree_skb (skb, FREE_WRITE);
539
540 /* You might need to clean up and record Tx statistics here. */
541 lp->stats.tx_aborted_errors++;
542
543 return 0;
544 }
545
546 /* The typical workload of the driver:
547 Handle the network interface interrupts. */
548 static void
549 eexp_interrupt(int reg_ptr)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
550 {
551 int irq = -(((struct pt_regs *)reg_ptr)->orig_eax+2);
552 struct device *dev = (struct device *)(irq2dev_map[irq]);
553 struct net_local *lp;
554 int ioaddr, status, boguscount = 0;
555 short ack_cmd = 0;
556
557 if (dev == NULL) {
558 printk ("net_interrupt(): irq %d for unknown device.\n", irq);
559 return;
560 }
561 dev->interrupt = 1;
562
563 ioaddr = dev->base_addr;
564 lp = (struct net_local *)dev->priv;
565
566 status = inw(ioaddr + SCB_STATUS);
567
568 if (net_debug > 4) {
569 printk("%s: EExp interrupt, status %4.4x.\n", dev->name, status);
570 }
571
572 /* Disable the 82586's input to the interrupt line. */
573 outb(irqrmap[dev->irq], ioaddr + SET_IRQ);
574
575 /* Reap the Tx packet buffers. */
576 while (lp->tx_reap != lp->tx_head) { /* if (status & 0x8000) */
577 unsigned short tx_status;
578 outw(lp->tx_reap, ioaddr + READ_PTR);
579 tx_status = inw(ioaddr);
580 if (tx_status == 0) {
581 if (net_debug > 5) printk("Couldn't reap %#x.\n", lp->tx_reap);
582 break;
583 }
584 if (tx_status & 0x2000) {
585 lp->stats.tx_packets++;
586 lp->stats.collisions += tx_status & 0xf;
587 dev->tbusy = 0;
588 mark_bh(INET_BH); /* Inform upper layers. */
589 } else {
590 lp->stats.tx_errors++;
591 if (tx_status & 0x0600) lp->stats.tx_carrier_errors++;
592 if (tx_status & 0x0100) lp->stats.tx_fifo_errors++;
593 if (!(tx_status & 0x0040)) lp->stats.tx_heartbeat_errors++;
594 if (tx_status & 0x0020) lp->stats.tx_aborted_errors++;
595 }
596 if (net_debug > 5)
597 printk("Reaped %x, Tx status %04x.\n" , lp->tx_reap, tx_status);
598 lp->tx_reap += TX_BUF_SIZE;
599 if (lp->tx_reap > RX_BUF_START - TX_BUF_SIZE)
600 lp->tx_reap = TX_BUF_START;
601 if (++boguscount > 4)
602 break;
603 }
604
605 if (status & 0x4000) { /* Packet received. */
606 if (net_debug > 5)
607 printk("Received packet, rx_head %04x.\n", lp->rx_head);
608 eexp_rx(dev);
609 }
610
611 /* Acknowledge the interrupt sources. */
612 ack_cmd = status & 0xf000;
613
614 if ((status & 0x0700) != 0x0200 && dev->start) {
615 if (net_debug)
616 printk("%s: Command unit stopped, status %04x, restarting.\n",
617 dev->name, status);
618 /* If this ever occurs we should really re-write the idle loop, reset
619 the Tx list, and do a complete restart of the command unit.
620 For now we rely on the Tx timeout if the resume doesn't work. */
621 ack_cmd |= CUC_RESUME;
622 }
623
624 if ((status & 0x0070) != 0x0040 && dev->start) {
625 short saved_write_ptr = inw(ioaddr + WRITE_PTR);
626 /* The Rx unit is not ready, it must be hung. Restart the receiver by
627 initializing the rx buffers, and issuing an Rx start command. */
628 if (net_debug)
629 printk("%s: Rx unit stopped, status %04x, restarting.\n",
630 dev->name, status);
631 init_rx_bufs(dev);
632 outw(RX_BUF_START, SCB_RFA);
633 outw(saved_write_ptr, ioaddr + WRITE_PTR);
634 ack_cmd |= RX_START;
635 }
636
637 outw(ack_cmd, ioaddr + SCB_CMD);
638 outb(0, ioaddr + SIGNAL_CA);
639
640 if (net_debug > 5) {
641 printk("%s: EExp exiting interrupt, status %4.4x.\n", dev->name,
642 inw(ioaddr + SCB_CMD));
643 }
644 /* Enable the 82586's input to the interrupt line. */
645 outb(irqrmap[dev->irq] | 0x08, ioaddr + SET_IRQ);
646 return;
647 }
648
649 static int
650 eexp_close(struct device *dev)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
651 {
652 int ioaddr = dev->base_addr;
653
654 dev->tbusy = 1;
655 dev->start = 0;
656
657 /* Flush the Tx and disable Rx. */
658 outw(RX_SUSPEND | CUC_SUSPEND, ioaddr + SCB_CMD);
659 outb(0, ioaddr + SIGNAL_CA);
660
661 /* Disable the physical interrupt line. */
662 outb(0, ioaddr + SET_IRQ);
663
664 free_irq(dev->irq);
665
666 irq2dev_map[dev->irq] = 0;
667
668 /* Update the statistics here. */
669
670 return 0;
671 }
672
673 /* Get the current statistics. This may be called with the card open or
674 closed. */
675 static struct enet_statistics *
676 eexp_get_stats(struct device *dev)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
677 {
678 struct net_local *lp = (struct net_local *)dev->priv;
679
680 /* ToDo: decide if there are any useful statistics from the SCB. */
681
682 return &lp->stats;
683 }
684
685 #ifdef HAVE_MULTICAST
686 /* Set or clear the multicast filter for this adaptor.
687 num_addrs == -1 Promiscuous mode, receive all packets
688 num_addrs == 0 Normal mode, clear multicast list
689 num_addrs > 0 Multicast mode, receive normal and MC packets, and do
690 best-effort filtering.
691 */
692 static void
693 set_multicast_list(struct device *dev, int num_addrs, void *addrs)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
694 {
695 short ioaddr = dev->base_addr;
696 if (num_addrs < 0) {
697 /* Not written yet, this requires expanding the init_words config
698 cmd. */
699 } else if (num_addrs > 0) {
700 /* Fill in the SET_MC_CMD with the number of address bytes, followed
701 by the list of multicast addresses to be accepted. */
702 outw(SET_MC_CMD + 6, ioaddr + WRITE_PTR);
703 outw(num_addrs * 6, ioaddr);
704 port_write(ioaddr, addrs, num_addrs*3); /* 3 = addr len in words */
705 /* We must trigger a whole 586 reset due to a bug. */
706 } else {
707 /* Not written yet, this requires expanding the init_words config
708 cmd. */
709 outw(99, ioaddr); /* Disable promiscuous mode, use normal mode */
710 }
711 }
712 #endif
713
714 /* The horrible routine to read a word from the serial EEPROM. */
715
716 /* The delay between EEPROM clock transitions. */
717 #define eeprom_delay() { int _i = 40; while (--_i > 0) { __SLOW_DOWN_IO; }}
718 #define EE_READ_CMD (6 << 6)
719
720 int
721 read_eeprom(int ioaddr, int location)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
722 {
723 int i;
724 unsigned short retval = 0;
725 short ee_addr = ioaddr + EEPROM_Ctrl;
726 int read_cmd = location | EE_READ_CMD;
727 short ctrl_val = EE_CS | _586_RESET;
728
729 outb(ctrl_val, ee_addr);
730
731 /* Shift the read command bits out. */
732 for (i = 8; i >= 0; i--) {
733 short outval = (read_cmd & (1 << i)) ? ctrl_val | EE_DATA_WRITE
734 : ctrl_val;
735 outb(outval, ee_addr);
736 outb(outval | EE_SHIFT_CLK, ee_addr); /* EEPROM clock tick. */
737 eeprom_delay();
738 outb(outval, ee_addr); /* Finish EEPROM a clock tick. */
739 eeprom_delay();
740 }
741 outb(ctrl_val, ee_addr);
742
743 for (i = 16; i > 0; i--) {
744 outb(ctrl_val | EE_SHIFT_CLK, ee_addr); eeprom_delay();
745 retval = (retval << 1) | ((inb(ee_addr) & EE_DATA_READ) ? 1 : 0);
746 outb(ctrl_val, ee_addr); eeprom_delay();
747 }
748
749 /* Terminate the EEPROM access. */
750 ctrl_val &= ~EE_CS;
751 outb(ctrl_val | EE_SHIFT_CLK, ee_addr);
752 eeprom_delay();
753 outb(ctrl_val, ee_addr);
754 eeprom_delay();
755 return retval;
756 }
757
758 static void
759 init_82586_mem(struct device *dev)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
760 {
761 struct net_local *lp = (struct net_local *)dev->priv;
762 short ioaddr = dev->base_addr;
763
764 /* Enable loopback to protect the wire while starting up.
765 This is Superstition From Crynwr. */
766 outb(inb(ioaddr + Config) | 0x02, ioaddr + Config);
767
768 /* Hold the 586 in reset during the memory initialization. */
769 outb(_586_RESET, ioaddr + EEPROM_Ctrl);
770
771 /* Place the write pointer at 0xfff6 (address-aliased to 0xfffff6). */
772 outw(0xfff6, ioaddr + WRITE_PTR);
773 port_write(ioaddr, init_words, sizeof(init_words)>>1);
774
775 /* Fill in the station address. */
776 outw(SA_OFFSET, ioaddr + WRITE_PTR);
777 port_write(ioaddr, dev->dev_addr, 3);
778
779 /* The Tx-block list is written as needed. We just set up the values. */
780 #ifdef initial_text_tx
781 lp->tx_cmd_link = DUMP_DATA + 4;
782 #else
783 lp->tx_cmd_link = IDLELOOP + 4;
784 #endif
785 lp->tx_head = lp->tx_reap = TX_BUF_START;
786
787 init_rx_bufs(dev);
788
789 /* Start the 586 by releasing the reset line. */
790 outb(0x00, ioaddr + EEPROM_Ctrl);
791
792 /* This was time consuming to track down: you need to give two channel
793 attention signals to reliably start up the i82586. */
794 outb(0, ioaddr + SIGNAL_CA);
795
796 {
797 int boguscnt = 50;
798 while (inw(ioaddr + SCB_STATUS) == 0)
799 if (--boguscnt == 0) {
800 printk("%s: i82586 initialization timed out with status %04x, cmd %04x.\n",
801 dev->name, inw(ioaddr + SCB_STATUS), inw(ioaddr + SCB_CMD));
802 break;
803 }
804 /* Issue channel-attn -- the 82586 won't start without it. */
805 outb(0, ioaddr + SIGNAL_CA);
806 }
807
808 /* Disable loopback. */
809 outb(inb(ioaddr + Config) & ~0x02, ioaddr + Config);
810 if (net_debug > 4)
811 printk("%s: Initialized 82586, status %04x.\n", dev->name,
812 inw(ioaddr + SCB_STATUS));
813 return;
814 }
815
816 /* Initialize the Rx-block list. */
817 static void
818 init_rx_bufs(struct device *dev)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
819 {
820 struct net_local *lp = (struct net_local *)dev->priv;
821 short ioaddr = dev->base_addr;
822
823 int cur_rxbuf = lp->rx_head = RX_BUF_START;
824
825 /* Initialize each Rx frame + data buffer. */
826 do { /* While there is room for one more. */
827 outw(cur_rxbuf, ioaddr + WRITE_PTR);
828 outw(0x0000, ioaddr); /* Status */
829 outw(0x0000, ioaddr); /* Command */
830 outw(cur_rxbuf + RX_BUF_SIZE, ioaddr); /* Link */
831 outw(cur_rxbuf + 22, ioaddr); /* Buffer offset */
832 outw(0x0000, ioaddr); /* Pad for dest addr. */
833 outw(0x0000, ioaddr);
834 outw(0x0000, ioaddr);
835 outw(0x0000, ioaddr); /* Pad for source addr. */
836 outw(0x0000, ioaddr);
837 outw(0x0000, ioaddr);
838 outw(0x0000, ioaddr); /* Pad for protocol. */
839
840 outw(0x0000, ioaddr); /* Buffer: Actual count */
841 outw(-1, ioaddr); /* Buffer: Next (none). */
842 outw(cur_rxbuf + 0x20, ioaddr); /* Buffer: Address low */
843 outw(0x0000, ioaddr);
844 /* Finally, the number of bytes in the buffer. */
845 outw(0x8000 + RX_BUF_SIZE-0x20, ioaddr);
846
847 lp->rx_tail = cur_rxbuf;
848 cur_rxbuf += RX_BUF_SIZE;
849 } while (cur_rxbuf <= RX_BUF_END - RX_BUF_SIZE);
850
851 /* Terminate the list by setting the EOL bit, and wrap the pointer to make
852 the list a ring. */
853 outw(lp->rx_tail + 2, ioaddr + WRITE_PTR);
854 outw(0xC000, ioaddr); /* Command, mark as last. */
855 outw(lp->rx_head, ioaddr); /* Link */
856 }
857
858 static void
859 hardware_send_packet(struct device *dev, void *buf, short length)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
860 {
861 struct net_local *lp = (struct net_local *)dev->priv;
862 short ioaddr = dev->base_addr;
863 short tx_block = lp->tx_head;
864
865 /* Set the write pointer to the Tx block, and put out the header. */
866 outw(tx_block, ioaddr + WRITE_PTR);
867 outw(0x0000, ioaddr); /* Tx status */
868 outw(CMD_INTR|CmdTx, ioaddr); /* Tx command */
869 outw(tx_block+16, ioaddr); /* Next command is a NoOp. */
870 outw(tx_block+8, ioaddr); /* Data Buffer offset. */
871
872 /* Output the data buffer descriptor. */
873 outw(length | 0x8000, ioaddr); /* Byte count parameter. */
874 outw(-1, ioaddr); /* No next data buffer. */
875 outw(tx_block+22, ioaddr); /* Buffer follows the NoOp command. */
876 outw(0x0000, ioaddr); /* Buffer address high bits (always zero). */
877
878 /* Output the Loop-back NoOp command. */
879 outw(0x0000, ioaddr); /* Tx status */
880 outw(CmdNOp, ioaddr); /* Tx command */
881 outw(tx_block+16, ioaddr); /* Next is myself. */
882
883 /* Output the packet using the write pointer.
884 Hmmm, it feels a little like a 3c501! */
885 port_write(ioaddr + DATAPORT, buf, (length + 1) >> 1);
886
887 /* Set the old command link pointing to this send packet. */
888 outw(lp->tx_cmd_link, ioaddr + WRITE_PTR);
889 outw(tx_block, ioaddr);
890 lp->tx_cmd_link = tx_block + 20;
891
892 /* Set the next free tx region. */
893 lp->tx_head = tx_block + TX_BUF_SIZE;
894 if (lp->tx_head > RX_BUF_START - TX_BUF_SIZE)
895 lp->tx_head = TX_BUF_START;
896
897 if (net_debug > 4) {
898 printk("%s: EExp @%x send length = %d, tx_block %3x, next %3x, "
899 "reap %4x status %4.4x.\n", dev->name, ioaddr, length,
900 tx_block, lp->tx_head, lp->tx_reap, inw(ioaddr + SCB_STATUS));
901 }
902
903 if (lp->tx_head != lp->tx_reap)
904 dev->tbusy = 0;
905 }
906
907 static void
908 eexp_rx(struct device *dev)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
909 {
910 struct net_local *lp = (struct net_local *)dev->priv;
911 short ioaddr = dev->base_addr;
912 short saved_write_ptr = inw(ioaddr + WRITE_PTR);
913 short rx_head = lp->rx_head;
914 short rx_tail = lp->rx_tail;
915 short boguscount = 10;
916 short frame_status;
917
918 /* Set the read pointer to the Rx frame. */
919 outw(rx_head, ioaddr + READ_PTR);
920 while ((frame_status = inw(ioaddr)) < 0) { /* Command complete */
921 short rfd_cmd = inw(ioaddr);
922 short next_rx_frame = inw(ioaddr);
923 short data_buffer_addr = inw(ioaddr);
924 short pkt_len;
925
926 /* Set the read pointer the data buffer. */
927 outw(data_buffer_addr, ioaddr + READ_PTR);
928 pkt_len = inw(ioaddr);
929
930 if (rfd_cmd != 0 || data_buffer_addr != rx_head + 22
931 || pkt_len & 0xC000 != 0xC000) {
932 printk("%s: Rx frame at %#x corrupted, status %04x cmd %04x"
933 "next %04x data-buf @%04x %04x.\n", dev->name, rx_head,
934 frame_status, rfd_cmd, next_rx_frame, data_buffer_addr,
935 pkt_len);
936 } else if ((frame_status & 0x2000) == 0) {
937 /* Frame Rxed, but with error. */
938 lp->stats.rx_errors++;
939 if (frame_status & 0x0800) lp->stats.rx_crc_errors++;
940 if (frame_status & 0x0400) lp->stats.rx_frame_errors++;
941 if (frame_status & 0x0200) lp->stats.rx_fifo_errors++;
942 if (frame_status & 0x0100) lp->stats.rx_over_errors++;
943 if (frame_status & 0x0080) lp->stats.rx_length_errors++;
944 } else {
945 /* Malloc up new buffer. */
946 int sksize;
947 struct sk_buff *skb;
948
949 pkt_len &= 0x3fff;
950 sksize = sizeof(struct sk_buff) + pkt_len;
951 skb = alloc_skb(sksize, GFP_ATOMIC);
952 if (skb == NULL) {
953 printk("%s: Memory squeeze, dropping packet.\n", dev->name);
954 lp->stats.rx_dropped++;
955 break;
956 }
957 skb->mem_len = sksize;
958 skb->mem_addr = skb;
959 skb->len = pkt_len;
960 skb->dev = dev;
961
962 outw(data_buffer_addr + 10, ioaddr + READ_PTR);
963
964 port_read(ioaddr, (void *)(skb+1), (pkt_len + 1) >> 1);
965
966 #ifdef HAVE_NETIF_RX
967 netif_rx(skb);
968 #else
969 skb->lock = 0;
970 if (dev_rint((unsigned char*)skb, pkt_len, IN_SKBUFF, dev) != 0) {
971 kfree_s(skb, sksize);
972 lp->stats.rx_dropped++;
973 break;
974 }
975 #endif
976 lp->stats.rx_packets++;
977 }
978
979 /* Clear the status word and set End-of-List on the rx frame. */
980 outw(rx_head, ioaddr + WRITE_PTR);
981 outw(0x0000, ioaddr);
982 outw(0xC000, ioaddr);
983 #ifndef final_version
984 if (next_rx_frame != rx_head + RX_BUF_SIZE
985 && next_rx_frame != RX_BUF_START) {
986 printk("%s: Rx next frame at %#x is %#x instead of %#x.\n", dev->name,
987 rx_head, next_rx_frame, rx_head + RX_BUF_SIZE);
988 next_rx_frame = rx_head + RX_BUF_SIZE;
989 if (next_rx_frame >= RX_BUF_END - RX_BUF_SIZE)
990 next_rx_frame = RX_BUF_START;
991 }
992 #endif
993 outw(rx_tail+2, ioaddr + WRITE_PTR);
994 outw(0x0000, ioaddr); /* Clear the end-of-list on the prev. RFD. */
995
996 #ifndef final_version
997 outw(rx_tail+4, ioaddr + READ_PTR);
998 if (inw(ioaddr) != rx_head) {
999 printk("%s: Rx buf link mismatch, at %04x link %04x instead of %04x.\n",
1000 dev->name, rx_tail, (outw(rx_tail+4, ioaddr + READ_PTR),inw(ioaddr)),
1001 rx_head);
1002 outw(rx_head, ioaddr);
1003 }
1004 #endif
1005
1006 rx_tail = rx_head;
1007 rx_head = next_rx_frame;
1008 if (--boguscount == 0)
1009 break;
1010 outw(rx_head, ioaddr + READ_PTR);
1011 }
1012
1013 lp->rx_head = rx_head;
1014 lp->rx_tail = rx_tail;
1015
1016 /* Restore the original write pointer. */
1017 outw(saved_write_ptr, ioaddr + WRITE_PTR);
1018 }
1019
1020 /*
1021 * Local variables:
1022 * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -I/usr/src/linux/drivers/net -Wall -Wstrict-prototypes -O6 -m486 -c eexpress.c"
1023 * version-control: t
1024 * kept-new-versions: 5
1025 * tab-width: 4
1026 * End:
1027 */