Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
583 | mauro | 1 | /* 3c509.c: A 3c509 EtherLink3 ethernet driver for linux. */ |
2 | /* |
||
3 | Written 1993-1998 by Donald Becker. |
||
4 | |||
5 | Copyright 1994-1998 by Donald Becker. |
||
6 | Copyright 1993 United States Government as represented by the |
||
7 | Director, National Security Agency. This software may be used and |
||
8 | distributed according to the terms of the GNU Public License, |
||
9 | incorporated herein by reference. |
||
10 | |||
11 | This driver is for the 3Com EtherLinkIII series. |
||
12 | |||
13 | The author may be reached as becker@cesdis.gsfc.nasa.gov or |
||
14 | C/O Center of Excellence in Space Data and Information Sciences |
||
15 | Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771 |
||
16 | |||
17 | Known limitations: |
||
18 | Because of the way 3c509 ISA detection works it's difficult to predict |
||
19 | a priori which of several ISA-mode cards will be detected first. |
||
20 | |||
21 | This driver does not use predictive interrupt mode, resulting in higher |
||
22 | packet latency but lower overhead. If interrupts are disabled for an |
||
23 | unusually long time it could also result in missed packets, but in |
||
24 | practice this rarely happens. |
||
25 | |||
26 | |||
27 | FIXES: |
||
28 | Alan Cox: Removed the 'Unexpected interrupt' bug. |
||
29 | Michael Meskes: Upgraded to Donald Becker's version 1.07. |
||
30 | Alan Cox: Increased the eeprom delay. Regardless of |
||
31 | what the docs say some people definitely |
||
32 | get problems with lower (but in card spec) |
||
33 | delays |
||
34 | v1.10 4/21/97 Fixed module code so that multiple cards may be detected, |
||
35 | other cleanups. -djb |
||
36 | Andrea Arcangeli: Upgraded to Donald Becker's version 1.12. |
||
37 | Rick Payne: Fixed SMP race condition |
||
38 | v1.13 9/8/97 Made 'max_interrupt_work' an insmod-settable variable -djb |
||
39 | v1.14 10/15/97 Avoided waiting..discard message for fast machines -djb |
||
40 | v1.15 1/31/98 Faster recovery for Tx errors. -djb |
||
41 | v1.16 2/3/98 Different ID port handling to avoid sound cards. -djb |
||
42 | */ |
||
43 | |||
44 | static char *version = "3c509.c:1.16 (2.2) 2/3/98 becker@cesdis.gsfc.nasa.gov.\n"; |
||
45 | /* A few values that may be tweaked. */ |
||
46 | |||
47 | /* Time in jiffies before concluding the transmitter is hung. */ |
||
48 | #define TX_TIMEOUT (400*HZ/1000) |
||
49 | /* Maximum events (Rx packets, etc.) to handle at each interrupt. */ |
||
50 | static int max_interrupt_work = 10; |
||
51 | |||
52 | #include <linux/config.h> |
||
53 | #include <linux/module.h> |
||
54 | |||
55 | #include <linux/mca.h> |
||
56 | #include <linux/sched.h> |
||
57 | #include <linux/string.h> |
||
58 | #include <linux/interrupt.h> |
||
59 | #include <linux/errno.h> |
||
60 | #include <linux/in.h> |
||
61 | #include <linux/malloc.h> |
||
62 | #include <linux/ioport.h> |
||
63 | #include <linux/netdevice.h> |
||
64 | #include <linux/etherdevice.h> |
||
65 | #include <linux/skbuff.h> |
||
66 | #include <linux/delay.h> /* for udelay() */ |
||
67 | |||
68 | #include <asm/spinlock.h> |
||
69 | #include <asm/bitops.h> |
||
70 | #include <asm/io.h> |
||
71 | #include <asm/irq.h> |
||
72 | |||
73 | #ifdef EL3_DEBUG |
||
74 | int el3_debug = EL3_DEBUG; |
||
75 | #else |
||
76 | int el3_debug = 2; |
||
77 | #endif |
||
78 | |||
79 | /* To minimize the size of the driver source I only define operating |
||
80 | constants if they are used several times. You'll need the manual |
||
81 | anyway if you want to understand driver details. */ |
||
82 | /* Offsets from base I/O address. */ |
||
83 | #define EL3_DATA 0x00 |
||
84 | #define EL3_CMD 0x0e |
||
85 | #define EL3_STATUS 0x0e |
||
86 | #define EEPROM_READ 0x80 |
||
87 | |||
88 | #define EL3_IO_EXTENT 16 |
||
89 | |||
90 | #define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD) |
||
91 | |||
92 | /* The top five bits written to EL3_CMD are a command, the lower |
||
93 | 11 bits are the parameter, if applicable. */ |
||
94 | enum c509cmd { |
||
95 | TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11, |
||
96 | RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11, RxDiscard = 8<<11, |
||
97 | TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11, |
||
98 | FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11, |
||
99 | SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11, |
||
100 | SetTxThreshold = 18<<11, SetTxStart = 19<<11, StatsEnable = 21<<11, |
||
101 | StatsDisable = 22<<11, StopCoax = 23<<11,}; |
||
102 | |||
103 | enum c509status { |
||
104 | IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004, |
||
105 | TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020, |
||
106 | IntReq = 0x0040, StatsFull = 0x0080, CmdBusy = 0x1000, }; |
||
107 | |||
108 | /* The SetRxFilter command accepts the following classes: */ |
||
109 | enum RxFilter { |
||
110 | RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8 }; |
||
111 | |||
112 | /* Register window 1 offsets, the window used in normal operation. */ |
||
113 | #define TX_FIFO 0x00 |
||
114 | #define RX_FIFO 0x00 |
||
115 | #define RX_STATUS 0x08 |
||
116 | #define TX_STATUS 0x0B |
||
117 | #define TX_FREE 0x0C /* Remaining free bytes in Tx buffer. */ |
||
118 | |||
119 | #define WN0_IRQ 0x08 /* Window 0: Set IRQ line in bits 12-15. */ |
||
120 | #define WN4_MEDIA 0x0A /* Window 4: Various transcvr/media bits. */ |
||
121 | #define MEDIA_TP 0x00C0 /* Enable link beat and jabber for 10baseT. */ |
||
122 | |||
123 | /* |
||
124 | * Must be a power of two (we use a binary and in the |
||
125 | * circular queue) |
||
126 | */ |
||
127 | #define SKB_QUEUE_SIZE 64 |
||
128 | |||
129 | struct el3_private { |
||
130 | struct enet_statistics stats; |
||
131 | struct device *next_dev; |
||
132 | spinlock_t lock; |
||
133 | /* skb send-queue */ |
||
134 | int head, size; |
||
135 | struct sk_buff *queue[SKB_QUEUE_SIZE]; |
||
136 | char mca_slot; |
||
137 | }; |
||
138 | static int id_port = 0x110; /* Start with 0x110 to avoid new sound cards.*/ |
||
139 | static struct device *el3_root_dev = NULL; |
||
140 | |||
141 | static ushort id_read_eeprom(int index); |
||
142 | static ushort read_eeprom(int ioaddr, int index); |
||
143 | static int el3_open(struct device *dev); |
||
144 | static int el3_start_xmit(struct sk_buff *skb, struct device *dev); |
||
145 | static void el3_interrupt(int irq, void *dev_id, struct pt_regs *regs); |
||
146 | static void update_stats(struct device *dev); |
||
147 | static struct enet_statistics *el3_get_stats(struct device *dev); |
||
148 | static int el3_rx(struct device *dev); |
||
149 | static int el3_close(struct device *dev); |
||
150 | static void set_multicast_list(struct device *dev); |
||
151 | |||
152 | #ifdef CONFIG_MCA |
||
153 | struct el3_mca_adapters_struct { |
||
154 | char* name; |
||
155 | int id; |
||
156 | }; |
||
157 | |||
158 | struct el3_mca_adapters_struct el3_mca_adapters[] = { |
||
159 | { "3Com 3c529 EtherLink III (10base2)", 0x627c }, |
||
160 | { "3Com 3c529 EtherLink III (10baseT)", 0x627d }, |
||
161 | { "3Com 3c529 EtherLink III (test mode)", 0x62db }, |
||
162 | { "3Com 3c529 EtherLink III (TP or coax)", 0x62f6 }, |
||
163 | { "3Com 3c529 EtherLink III (TP)", 0x62f7 }, |
||
164 | { NULL, 0 }, |
||
165 | }; |
||
166 | #endif |
||
167 | |||
168 | int el3_probe(struct device *dev) |
||
169 | { |
||
170 | short lrs_state = 0xff, i; |
||
171 | int ioaddr, irq, if_port; |
||
172 | u16 phys_addr[3]; |
||
173 | static int current_tag = 0; |
||
174 | int mca_slot = -1; |
||
175 | |||
176 | /* First check all slots of the EISA bus. The next slot address to |
||
177 | probe is kept in 'eisa_addr' to support multiple probe() calls. */ |
||
178 | if (EISA_bus) { |
||
179 | static int eisa_addr = 0x1000; |
||
180 | while (eisa_addr < 0x9000) { |
||
181 | ioaddr = eisa_addr; |
||
182 | eisa_addr += 0x1000; |
||
183 | |||
184 | /* Check the standard EISA ID register for an encoded '3Com'. */ |
||
185 | if (inw(ioaddr + 0xC80) != 0x6d50) |
||
186 | continue; |
||
187 | |||
188 | /* Change the register set to the configuration window 0. */ |
||
189 | outw(SelectWindow | 0, ioaddr + 0xC80 + EL3_CMD); |
||
190 | |||
191 | irq = inw(ioaddr + WN0_IRQ) >> 12; |
||
192 | if_port = inw(ioaddr + 6)>>14; |
||
193 | for (i = 0; i < 3; i++) |
||
194 | phys_addr[i] = htons(read_eeprom(ioaddr, i)); |
||
195 | |||
196 | /* Restore the "Product ID" to the EEPROM read register. */ |
||
197 | read_eeprom(ioaddr, 3); |
||
198 | |||
199 | /* Was the EISA code an add-on hack? Nahhhhh... */ |
||
200 | goto found; |
||
201 | } |
||
202 | } |
||
203 | |||
204 | #ifdef CONFIG_MCA |
||
205 | /* Based on Erik Nygren's (nygren@mit.edu) 3c529 patch, heavily |
||
206 | * modified by Chris Beauregard (cpbeaure@csclub.uwaterloo.ca) |
||
207 | * to support standard MCA probing. |
||
208 | * |
||
209 | * redone for multi-card detection by ZP Gu (zpg@castle.net) |
||
210 | * now works as a module |
||
211 | */ |
||
212 | |||
213 | if( MCA_bus ) { |
||
214 | int slot, j; |
||
215 | u_char pos4, pos5; |
||
216 | |||
217 | for( j = 0; el3_mca_adapters[j].name != NULL; j ++ ) { |
||
218 | slot = 0; |
||
219 | while( slot != MCA_NOTFOUND ) { |
||
220 | slot = mca_find_unused_adapter( |
||
221 | el3_mca_adapters[j].id, slot ); |
||
222 | if( slot == MCA_NOTFOUND ) break; |
||
223 | |||
224 | /* if we get this far, an adapter has been |
||
225 | * detected and is enabled |
||
226 | */ |
||
227 | |||
228 | pos4 = mca_read_stored_pos( slot, 4 ); |
||
229 | pos5 = mca_read_stored_pos( slot, 5 ); |
||
230 | |||
231 | ioaddr = ((short)((pos4&0xfc)|0x02)) << 8; |
||
232 | irq = pos5 & 0x0f; |
||
233 | |||
234 | /* probing for a card at a particular IO/IRQ */ |
||
235 | if(dev && ((dev->irq >= 1 && dev->irq != irq) || |
||
236 | (dev->base_addr >= 1 && dev->base_addr != ioaddr))) { |
||
237 | slot++; /* probing next slot */ |
||
238 | continue; |
||
239 | } |
||
240 | |||
241 | printk("3c509: found %s at slot %d\n", |
||
242 | el3_mca_adapters[j].name, slot + 1 ); |
||
243 | |||
244 | /* claim the slot */ |
||
245 | mca_set_adapter_name(slot, el3_mca_adapters[j].name); |
||
246 | mca_set_adapter_procfn(slot, NULL, NULL); |
||
247 | mca_mark_as_used(slot); |
||
248 | |||
249 | if_port = pos4 & 0x03; |
||
250 | if (el3_debug > 2) { |
||
251 | printk("3c529: irq %d ioaddr 0x%x ifport %d\n", irq, ioaddr, if_port); |
||
252 | } |
||
253 | for (i = 0; i < 3; i++) { |
||
254 | phys_addr[i] = htons(read_eeprom(ioaddr, i)); |
||
255 | } |
||
256 | |||
257 | mca_slot = slot; |
||
258 | |||
259 | goto found; |
||
260 | } |
||
261 | } |
||
262 | /* if we get here, we didn't find an MCA adapter */ |
||
263 | return -ENODEV; |
||
264 | } |
||
265 | #endif |
||
266 | /* Reset the ISA PnP mechanism on 3c509b. */ |
||
267 | outb(0x02, 0x279); /* Select PnP config control register. */ |
||
268 | outb(0x02, 0xA79); /* Return to WaitForKey state. */ |
||
269 | /* Select an open I/O location at 0x1*0 to do contention select. */ |
||
270 | for ( ; id_port < 0x200; id_port += 0x10) { |
||
271 | if (check_region(id_port, 1)) |
||
272 | continue; |
||
273 | outb(0x00, id_port); |
||
274 | outb(0xff, id_port); |
||
275 | if (inb(id_port) & 0x01) |
||
276 | break; |
||
277 | } |
||
278 | if (id_port >= 0x200) { |
||
279 | /* Rare -- do we really need a warning? */ |
||
280 | printk(" WARNING: No I/O port available for 3c509 activation.\n"); |
||
281 | return -ENODEV; |
||
282 | } |
||
283 | /* Next check for all ISA bus boards by sending the ID sequence to the |
||
284 | ID_PORT. We find cards past the first by setting the 'current_tag' |
||
285 | on cards as they are found. Cards with their tag set will not |
||
286 | respond to subsequent ID sequences. */ |
||
287 | |||
288 | outb(0x00, id_port); |
||
289 | outb(0x00, id_port); |
||
290 | for(i = 0; i < 255; i++) { |
||
291 | outb(lrs_state, id_port); |
||
292 | lrs_state <<= 1; |
||
293 | lrs_state = lrs_state & 0x100 ? lrs_state ^ 0xcf : lrs_state; |
||
294 | } |
||
295 | |||
296 | /* For the first probe, clear all board's tag registers. */ |
||
297 | if (current_tag == 0) |
||
298 | outb(0xd0, id_port); |
||
299 | else /* Otherwise kill off already-found boards. */ |
||
300 | outb(0xd8, id_port); |
||
301 | |||
302 | if (id_read_eeprom(7) != 0x6d50) { |
||
303 | return -ENODEV; |
||
304 | } |
||
305 | |||
306 | /* Read in EEPROM data, which does contention-select. |
||
307 | Only the lowest address board will stay "on-line". |
||
308 | 3Com got the byte order backwards. */ |
||
309 | for (i = 0; i < 3; i++) { |
||
310 | phys_addr[i] = htons(id_read_eeprom(i)); |
||
311 | } |
||
312 | |||
313 | { |
||
314 | unsigned int iobase = id_read_eeprom(8); |
||
315 | if_port = iobase >> 14; |
||
316 | ioaddr = 0x200 + ((iobase & 0x1f) << 4); |
||
317 | } |
||
318 | irq = id_read_eeprom(9) >> 12; |
||
319 | |||
320 | if (dev) { /* Set passed-in IRQ or I/O Addr. */ |
||
321 | if (dev->irq > 1 && dev->irq < 16) |
||
322 | irq = dev->irq; |
||
323 | |||
324 | if (dev->base_addr) { |
||
325 | if (dev->mem_end == 0x3c509 /* Magic key */ |
||
326 | && dev->base_addr >= 0x200 && dev->base_addr <= 0x3e0) |
||
327 | ioaddr = dev->base_addr & 0x3f0; |
||
328 | else if (dev->base_addr != ioaddr) |
||
329 | return -ENODEV; |
||
330 | } |
||
331 | } |
||
332 | |||
333 | /* Set the adaptor tag so that the next card can be found. */ |
||
334 | outb(0xd0 + ++current_tag, id_port); |
||
335 | |||
336 | /* Activate the adaptor at the EEPROM location. */ |
||
337 | outb((ioaddr >> 4) | 0xe0, id_port); |
||
338 | |||
339 | EL3WINDOW(0); |
||
340 | if (inw(ioaddr) != 0x6d50) |
||
341 | return -ENODEV; |
||
342 | |||
343 | /* Free the interrupt so that some other card can use it. */ |
||
344 | outw(0x0f00, ioaddr + WN0_IRQ); |
||
345 | found: |
||
346 | if (dev == NULL) { |
||
347 | dev = init_etherdev(dev, sizeof(struct el3_private)); |
||
348 | } |
||
349 | memcpy(dev->dev_addr, phys_addr, sizeof(phys_addr)); |
||
350 | dev->base_addr = ioaddr; |
||
351 | dev->irq = irq; |
||
352 | dev->if_port = (dev->mem_start & 0x1f) ? dev->mem_start & 3 : if_port; |
||
353 | |||
354 | request_region(dev->base_addr, EL3_IO_EXTENT, "3c509"); |
||
355 | |||
356 | { |
||
357 | const char *if_names[] = {"10baseT", "AUI", "undefined", "BNC"}; |
||
358 | printk("%s: 3c509 at %#3.3lx tag %d, %s port, address ", |
||
359 | dev->name, dev->base_addr, current_tag, if_names[dev->if_port]); |
||
360 | } |
||
361 | |||
362 | /* Read in the station address. */ |
||
363 | for (i = 0; i < 6; i++) |
||
364 | printk(" %2.2x", dev->dev_addr[i]); |
||
365 | printk(", IRQ %d.\n", dev->irq); |
||
366 | |||
367 | /* Make up a EL3-specific-data structure. */ |
||
368 | if (dev->priv == NULL) |
||
369 | dev->priv = kmalloc(sizeof(struct el3_private), GFP_KERNEL); |
||
370 | if (dev->priv == NULL) |
||
371 | return -ENOMEM; |
||
372 | memset(dev->priv, 0, sizeof(struct el3_private)); |
||
373 | |||
374 | ((struct el3_private *)dev->priv)->mca_slot = mca_slot; |
||
375 | ((struct el3_private *)dev->priv)->next_dev = el3_root_dev; |
||
376 | el3_root_dev = dev; |
||
377 | |||
378 | if (el3_debug > 0) |
||
379 | printk(version); |
||
380 | |||
381 | /* The EL3-specific entries in the device structure. */ |
||
382 | dev->open = &el3_open; |
||
383 | dev->hard_start_xmit = &el3_start_xmit; |
||
384 | dev->stop = &el3_close; |
||
385 | dev->get_stats = &el3_get_stats; |
||
386 | dev->set_multicast_list = &set_multicast_list; |
||
387 | |||
388 | /* Fill in the generic fields of the device structure. */ |
||
389 | ether_setup(dev); |
||
390 | return 0; |
||
391 | } |
||
392 | |||
393 | /* Read a word from the EEPROM using the regular EEPROM access register. |
||
394 | Assume that we are in register window zero. |
||
395 | */ |
||
396 | static ushort read_eeprom(int ioaddr, int index) |
||
397 | { |
||
398 | outw(EEPROM_READ + index, ioaddr + 10); |
||
399 | /* Pause for at least 162 us. for the read to take place. */ |
||
400 | udelay (500); |
||
401 | return inw(ioaddr + 12); |
||
402 | } |
||
403 | |||
404 | /* Read a word from the EEPROM when in the ISA ID probe state. */ |
||
405 | static ushort id_read_eeprom(int index) |
||
406 | { |
||
407 | int bit, word = 0; |
||
408 | |||
409 | /* Issue read command, and pause for at least 162 us. for it to complete. |
||
410 | Assume extra-fast 16Mhz bus. */ |
||
411 | outb(EEPROM_READ + index, id_port); |
||
412 | |||
413 | /* Pause for at least 162 us. for the read to take place. */ |
||
414 | udelay (500); |
||
415 | |||
416 | for (bit = 15; bit >= 0; bit--) |
||
417 | word = (word << 1) + (inb(id_port) & 0x01); |
||
418 | |||
419 | if (el3_debug > 3) |
||
420 | printk(" 3c509 EEPROM word %d %#4.4x.\n", index, word); |
||
421 | |||
422 | return word; |
||
423 | } |
||
424 | |||
425 | |||
426 | static int |
||
427 | el3_open(struct device *dev) |
||
428 | { |
||
429 | int ioaddr = dev->base_addr; |
||
430 | int i; |
||
431 | |||
432 | outw(TxReset, ioaddr + EL3_CMD); |
||
433 | outw(RxReset, ioaddr + EL3_CMD); |
||
434 | outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD); |
||
435 | |||
436 | /* Set the spinlock before grabbing IRQ! */ |
||
437 | ((struct el3_private *)dev->priv)->lock = (spinlock_t) SPIN_LOCK_UNLOCKED; |
||
438 | |||
439 | if (request_irq(dev->irq, &el3_interrupt, 0, dev->name, dev)) { |
||
440 | return -EAGAIN; |
||
441 | } |
||
442 | |||
443 | EL3WINDOW(0); |
||
444 | if (el3_debug > 3) |
||
445 | printk("%s: Opening, IRQ %d status@%x %4.4x.\n", dev->name, |
||
446 | dev->irq, ioaddr + EL3_STATUS, inw(ioaddr + EL3_STATUS)); |
||
447 | |||
448 | /* Activate board: this is probably unnecessary. */ |
||
449 | outw(0x0001, ioaddr + 4); |
||
450 | |||
451 | /* Set the IRQ line. */ |
||
452 | outw((dev->irq << 12) | 0x0f00, ioaddr + WN0_IRQ); |
||
453 | |||
454 | /* Set the station address in window 2 each time opened. */ |
||
455 | EL3WINDOW(2); |
||
456 | |||
457 | for (i = 0; i < 6; i++) |
||
458 | outb(dev->dev_addr[i], ioaddr + i); |
||
459 | |||
460 | if (dev->if_port == 3) |
||
461 | /* Start the thinnet transceiver. We should really wait 50ms...*/ |
||
462 | outw(StartCoax, ioaddr + EL3_CMD); |
||
463 | else if (dev->if_port == 0) { |
||
464 | /* 10baseT interface, enabled link beat and jabber check. */ |
||
465 | EL3WINDOW(4); |
||
466 | outw(inw(ioaddr + WN4_MEDIA) | MEDIA_TP, ioaddr + WN4_MEDIA); |
||
467 | } |
||
468 | |||
469 | /* Switch to the stats window, and clear all stats by reading. */ |
||
470 | outw(StatsDisable, ioaddr + EL3_CMD); |
||
471 | EL3WINDOW(6); |
||
472 | for (i = 0; i < 9; i++) |
||
473 | inb(ioaddr + i); |
||
474 | inw(ioaddr + 10); |
||
475 | inw(ioaddr + 12); |
||
476 | |||
477 | /* Switch to register set 1 for normal use. */ |
||
478 | EL3WINDOW(1); |
||
479 | |||
480 | /* Accept b-case and phys addr only. */ |
||
481 | outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD); |
||
482 | outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */ |
||
483 | |||
484 | dev->interrupt = 0; |
||
485 | dev->tbusy = 0; |
||
486 | dev->start = 1; |
||
487 | |||
488 | outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */ |
||
489 | outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */ |
||
490 | /* Allow status bits to be seen. */ |
||
491 | outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD); |
||
492 | /* Ack all pending events, and set active indicator mask. */ |
||
493 | outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq, |
||
494 | ioaddr + EL3_CMD); |
||
495 | outw(SetIntrEnb | IntLatch|TxAvailable|TxComplete|RxComplete|StatsFull, |
||
496 | ioaddr + EL3_CMD); |
||
497 | |||
498 | if (el3_debug > 3) |
||
499 | printk("%s: Opened 3c509 IRQ %d status %4.4x.\n", |
||
500 | dev->name, dev->irq, inw(ioaddr + EL3_STATUS)); |
||
501 | |||
502 | MOD_INC_USE_COUNT; |
||
503 | return 0; /* Always succeed */ |
||
504 | } |
||
505 | |||
506 | static int |
||
507 | el3_start_xmit(struct sk_buff *skb, struct device *dev) |
||
508 | { |
||
509 | struct el3_private *lp = (struct el3_private *)dev->priv; |
||
510 | int ioaddr = dev->base_addr; |
||
511 | |||
512 | /* Transmitter timeout, serious problems. */ |
||
513 | if (dev->tbusy) { |
||
514 | int tickssofar = jiffies - dev->trans_start; |
||
515 | if (tickssofar < TX_TIMEOUT) |
||
516 | return 1; |
||
517 | printk("%s: transmit timed out, Tx_status %2.2x status %4.4x " |
||
518 | "Tx FIFO room %d.\n", |
||
519 | dev->name, inb(ioaddr + TX_STATUS), inw(ioaddr + EL3_STATUS), |
||
520 | inw(ioaddr + TX_FREE)); |
||
521 | lp->stats.tx_errors++; |
||
522 | dev->trans_start = jiffies; |
||
523 | /* Issue TX_RESET and TX_START commands. */ |
||
524 | outw(TxReset, ioaddr + EL3_CMD); |
||
525 | outw(TxEnable, ioaddr + EL3_CMD); |
||
526 | dev->tbusy = 0; |
||
527 | } |
||
528 | |||
529 | lp->stats.tx_bytes += skb->len; |
||
530 | |||
531 | if (el3_debug > 4) { |
||
532 | printk("%s: el3_start_xmit(length = %u) called, status %4.4x.\n", |
||
533 | dev->name, skb->len, inw(ioaddr + EL3_STATUS)); |
||
534 | } |
||
535 | #if 0 |
||
536 | #ifndef final_version |
||
537 | { /* Error-checking code, delete someday. */ |
||
538 | ushort status = inw(ioaddr + EL3_STATUS); |
||
539 | if (status & 0x0001 /* IRQ line active, missed one. */ |
||
540 | && inw(ioaddr + EL3_STATUS) & 1) { /* Make sure. */ |
||
541 | printk("%s: Missed interrupt, status then %04x now %04x" |
||
542 | " Tx %2.2x Rx %4.4x.\n", dev->name, status, |
||
543 | inw(ioaddr + EL3_STATUS), inb(ioaddr + TX_STATUS), |
||
544 | inw(ioaddr + RX_STATUS)); |
||
545 | /* Fake interrupt trigger by masking, acknowledge interrupts. */ |
||
546 | outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD); |
||
547 | outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq, |
||
548 | ioaddr + EL3_CMD); |
||
549 | outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD); |
||
550 | } |
||
551 | } |
||
552 | #endif |
||
553 | #endif |
||
554 | /* Avoid timer-based retransmission conflicts. */ |
||
555 | if (test_and_set_bit(0, (void*)&dev->tbusy) != 0) |
||
556 | printk("%s: Transmitter access conflict.\n", dev->name); |
||
557 | else { |
||
558 | /* |
||
559 | * We lock the driver against other processors. Note |
||
560 | * we don't need to lock versus the IRQ as we suspended |
||
561 | * that. This means that we lose the ability to take |
||
562 | * an RX during a TX upload. That sucks a bit with SMP |
||
563 | * on an original 3c509 (2K buffer) |
||
564 | * |
||
565 | * Using disable_irq stops us crapping on other |
||
566 | * time sensitive devices. |
||
567 | */ |
||
568 | |||
569 | #ifdef __SMP__ |
||
570 | disable_irq_nosync(dev->irq); |
||
571 | spin_lock(&lp->lock); |
||
572 | #endif |
||
573 | |||
574 | /* Put out the doubleword header... */ |
||
575 | outw(skb->len, ioaddr + TX_FIFO); |
||
576 | outw(0x00, ioaddr + TX_FIFO); |
||
577 | /* ... and the packet rounded to a doubleword. */ |
||
578 | #ifdef __powerpc__ |
||
579 | outsl_unswapped(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2); |
||
580 | #else |
||
581 | outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2); |
||
582 | #endif |
||
583 | |||
584 | dev->trans_start = jiffies; |
||
585 | if (inw(ioaddr + TX_FREE) > 1536) { |
||
586 | dev->tbusy = 0; |
||
587 | } else |
||
588 | /* Interrupt us when the FIFO has room for max-sized packet. */ |
||
589 | outw(SetTxThreshold + 1536, ioaddr + EL3_CMD); |
||
590 | #ifdef __SMP__ |
||
591 | spin_unlock(&lp->lock); |
||
592 | enable_irq(dev->irq); |
||
593 | #endif |
||
594 | } |
||
595 | |||
596 | dev_kfree_skb (skb); |
||
597 | |||
598 | /* Clear the Tx status stack. */ |
||
599 | { |
||
600 | short tx_status; |
||
601 | int i = 4; |
||
602 | |||
603 | while (--i > 0 && (tx_status = inb(ioaddr + TX_STATUS)) > 0) { |
||
604 | if (tx_status & 0x38) lp->stats.tx_aborted_errors++; |
||
605 | if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD); |
||
606 | if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD); |
||
607 | outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */ |
||
608 | } |
||
609 | } |
||
610 | return 0; |
||
611 | } |
||
612 | |||
613 | /* The EL3 interrupt handler. */ |
||
614 | static void |
||
615 | el3_interrupt(int irq, void *dev_id, struct pt_regs *regs) |
||
616 | { |
||
617 | struct device *dev = (struct device *)dev_id; |
||
618 | struct el3_private *lp; |
||
619 | int ioaddr, status; |
||
620 | int i = max_interrupt_work; |
||
621 | |||
622 | if (dev == NULL) { |
||
623 | printk ("el3_interrupt(): irq %d for unknown device.\n", irq); |
||
624 | return; |
||
625 | } |
||
626 | |||
627 | lp = (struct el3_private *)dev->priv; |
||
628 | spin_lock(&lp->lock); |
||
629 | |||
630 | if (dev->interrupt) |
||
631 | printk("%s: Re-entering the interrupt handler.\n", dev->name); |
||
632 | dev->interrupt = 1; |
||
633 | |||
634 | ioaddr = dev->base_addr; |
||
635 | |||
636 | if (el3_debug > 4) { |
||
637 | status = inw(ioaddr + EL3_STATUS); |
||
638 | printk("%s: interrupt, status %4.4x.\n", dev->name, status); |
||
639 | } |
||
640 | |||
641 | while ((status = inw(ioaddr + EL3_STATUS)) & |
||
642 | (IntLatch | RxComplete | StatsFull)) { |
||
643 | |||
644 | if (status & RxComplete) |
||
645 | el3_rx(dev); |
||
646 | |||
647 | if (status & TxAvailable) { |
||
648 | if (el3_debug > 5) |
||
649 | printk(" TX room bit was handled.\n"); |
||
650 | /* There's room in the FIFO for a full-sized packet. */ |
||
651 | outw(AckIntr | TxAvailable, ioaddr + EL3_CMD); |
||
652 | dev->tbusy = 0; |
||
653 | mark_bh(NET_BH); |
||
654 | } |
||
655 | if (status & (AdapterFailure | RxEarly | StatsFull | TxComplete)) { |
||
656 | /* Handle all uncommon interrupts. */ |
||
657 | if (status & StatsFull) /* Empty statistics. */ |
||
658 | update_stats(dev); |
||
659 | if (status & RxEarly) { /* Rx early is unused. */ |
||
660 | el3_rx(dev); |
||
661 | outw(AckIntr | RxEarly, ioaddr + EL3_CMD); |
||
662 | } |
||
663 | if (status & TxComplete) { /* Really Tx error. */ |
||
664 | struct el3_private *lp = (struct el3_private *)dev->priv; |
||
665 | short tx_status; |
||
666 | int i = 4; |
||
667 | |||
668 | while (--i>0 && (tx_status = inb(ioaddr + TX_STATUS)) > 0) { |
||
669 | if (tx_status & 0x38) lp->stats.tx_aborted_errors++; |
||
670 | if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD); |
||
671 | if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD); |
||
672 | outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */ |
||
673 | } |
||
674 | } |
||
675 | if (status & AdapterFailure) { |
||
676 | /* Adapter failure requires Rx reset and reinit. */ |
||
677 | outw(RxReset, ioaddr + EL3_CMD); |
||
678 | /* Set the Rx filter to the current state. */ |
||
679 | outw(SetRxFilter | RxStation | RxBroadcast |
||
680 | | (dev->flags & IFF_ALLMULTI ? RxMulticast : 0) |
||
681 | | (dev->flags & IFF_PROMISC ? RxProm : 0), |
||
682 | ioaddr + EL3_CMD); |
||
683 | outw(RxEnable, ioaddr + EL3_CMD); /* Re-enable the receiver. */ |
||
684 | outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD); |
||
685 | } |
||
686 | } |
||
687 | |||
688 | if (--i < 0) { |
||
689 | printk("%s: Infinite loop in interrupt, status %4.4x.\n", |
||
690 | dev->name, status); |
||
691 | /* Clear all interrupts. */ |
||
692 | outw(AckIntr | 0xFF, ioaddr + EL3_CMD); |
||
693 | break; |
||
694 | } |
||
695 | /* Acknowledge the IRQ. */ |
||
696 | outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD); /* Ack IRQ */ |
||
697 | } |
||
698 | |||
699 | if (el3_debug > 4) { |
||
700 | printk("%s: exiting interrupt, status %4.4x.\n", dev->name, |
||
701 | inw(ioaddr + EL3_STATUS)); |
||
702 | } |
||
703 | spin_unlock(&lp->lock); |
||
704 | dev->interrupt = 0; |
||
705 | return; |
||
706 | } |
||
707 | |||
708 | |||
709 | static struct enet_statistics * |
||
710 | el3_get_stats(struct device *dev) |
||
711 | { |
||
712 | struct el3_private *lp = (struct el3_private *)dev->priv; |
||
713 | unsigned long flags; |
||
714 | |||
715 | /* |
||
716 | * This is fast enough not to bother with disable IRQ |
||
717 | * stuff. |
||
718 | */ |
||
719 | |||
720 | spin_lock_irqsave(&lp->lock, flags); |
||
721 | update_stats(dev); |
||
722 | spin_unlock_irqrestore(&lp->lock, flags); |
||
723 | return &lp->stats; |
||
724 | } |
||
725 | |||
726 | /* Update statistics. We change to register window 6, so this should be run |
||
727 | single-threaded if the device is active. This is expected to be a rare |
||
728 | operation, and it's simpler for the rest of the driver to assume that |
||
729 | window 1 is always valid rather than use a special window-state variable. |
||
730 | */ |
||
731 | static void update_stats(struct device *dev) |
||
732 | { |
||
733 | struct el3_private *lp = (struct el3_private *)dev->priv; |
||
734 | int ioaddr = dev->base_addr; |
||
735 | |||
736 | if (el3_debug > 5) |
||
737 | printk(" Updating the statistics.\n"); |
||
738 | /* Turn off statistics updates while reading. */ |
||
739 | outw(StatsDisable, ioaddr + EL3_CMD); |
||
740 | /* Switch to the stats window, and read everything. */ |
||
741 | EL3WINDOW(6); |
||
742 | lp->stats.tx_carrier_errors += inb(ioaddr + 0); |
||
743 | lp->stats.tx_heartbeat_errors += inb(ioaddr + 1); |
||
744 | /* Multiple collisions. */ inb(ioaddr + 2); |
||
745 | lp->stats.collisions += inb(ioaddr + 3); |
||
746 | lp->stats.tx_window_errors += inb(ioaddr + 4); |
||
747 | lp->stats.rx_fifo_errors += inb(ioaddr + 5); |
||
748 | lp->stats.tx_packets += inb(ioaddr + 6); |
||
749 | /* Rx packets */ inb(ioaddr + 7); |
||
750 | /* Tx deferrals */ inb(ioaddr + 8); |
||
751 | inw(ioaddr + 10); /* Total Rx and Tx octets. */ |
||
752 | inw(ioaddr + 12); |
||
753 | |||
754 | /* Back to window 1, and turn statistics back on. */ |
||
755 | EL3WINDOW(1); |
||
756 | outw(StatsEnable, ioaddr + EL3_CMD); |
||
757 | return; |
||
758 | } |
||
759 | |||
760 | static int |
||
761 | el3_rx(struct device *dev) |
||
762 | { |
||
763 | struct el3_private *lp = (struct el3_private *)dev->priv; |
||
764 | int ioaddr = dev->base_addr; |
||
765 | short rx_status; |
||
766 | |||
767 | if (el3_debug > 5) |
||
768 | printk(" In rx_packet(), status %4.4x, rx_status %4.4x.\n", |
||
769 | inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS)); |
||
770 | while ((rx_status = inw(ioaddr + RX_STATUS)) > 0) { |
||
771 | if (rx_status & 0x4000) { /* Error, update stats. */ |
||
772 | short error = rx_status & 0x3800; |
||
773 | |||
774 | outw(RxDiscard, ioaddr + EL3_CMD); |
||
775 | lp->stats.rx_errors++; |
||
776 | switch (error) { |
||
777 | case 0x0000: lp->stats.rx_over_errors++; break; |
||
778 | case 0x0800: lp->stats.rx_length_errors++; break; |
||
779 | case 0x1000: lp->stats.rx_frame_errors++; break; |
||
780 | case 0x1800: lp->stats.rx_length_errors++; break; |
||
781 | case 0x2000: lp->stats.rx_frame_errors++; break; |
||
782 | case 0x2800: lp->stats.rx_crc_errors++; break; |
||
783 | } |
||
784 | } else { |
||
785 | short pkt_len = rx_status & 0x7ff; |
||
786 | struct sk_buff *skb; |
||
787 | |||
788 | skb = dev_alloc_skb(pkt_len+5); |
||
789 | lp->stats.rx_bytes += pkt_len; |
||
790 | if (el3_debug > 4) |
||
791 | printk("Receiving packet size %d status %4.4x.\n", |
||
792 | pkt_len, rx_status); |
||
793 | if (skb != NULL) { |
||
794 | skb->dev = dev; |
||
795 | skb_reserve(skb, 2); /* Align IP on 16 byte */ |
||
796 | |||
797 | /* 'skb->data' points to the start of sk_buff data area. */ |
||
798 | #ifdef __powerpc__ |
||
799 | insl_unswapped(ioaddr+RX_FIFO, skb_put(skb,pkt_len), |
||
800 | (pkt_len + 3) >> 2); |
||
801 | #else |
||
802 | insl(ioaddr + RX_FIFO, skb_put(skb,pkt_len), |
||
803 | (pkt_len + 3) >> 2); |
||
804 | #endif |
||
805 | |||
806 | outw(RxDiscard, ioaddr + EL3_CMD); /* Pop top Rx packet. */ |
||
807 | skb->protocol = eth_type_trans(skb,dev); |
||
808 | netif_rx(skb); |
||
809 | lp->stats.rx_packets++; |
||
810 | continue; |
||
811 | } |
||
812 | outw(RxDiscard, ioaddr + EL3_CMD); |
||
813 | lp->stats.rx_dropped++; |
||
814 | if (el3_debug) |
||
815 | printk("%s: Couldn't allocate a sk_buff of size %d.\n", |
||
816 | dev->name, pkt_len); |
||
817 | } |
||
818 | inw(ioaddr + EL3_STATUS); /* Delay. */ |
||
819 | while (inw(ioaddr + EL3_STATUS) & 0x1000) |
||
820 | printk(KERN_DEBUG " Waiting for 3c509 to discard packet, status %x.\n", |
||
821 | inw(ioaddr + EL3_STATUS) ); |
||
822 | } |
||
823 | |||
824 | return 0; |
||
825 | } |
||
826 | |||
827 | /* |
||
828 | * Set or clear the multicast filter for this adaptor. |
||
829 | */ |
||
830 | static void |
||
831 | set_multicast_list(struct device *dev) |
||
832 | { |
||
833 | unsigned long flags; |
||
834 | struct el3_private *lp = (struct el3_private *)dev->priv; |
||
835 | int ioaddr = dev->base_addr; |
||
836 | |||
837 | if (el3_debug > 1) { |
||
838 | static int old = 0; |
||
839 | if (old != dev->mc_count) { |
||
840 | old = dev->mc_count; |
||
841 | printk("%s: Setting Rx mode to %d addresses.\n", dev->name, dev->mc_count); |
||
842 | } |
||
843 | } |
||
844 | spin_lock_irqsave(&lp->lock, flags); |
||
845 | if (dev->flags&IFF_PROMISC) { |
||
846 | outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast | RxProm, |
||
847 | ioaddr + EL3_CMD); |
||
848 | } |
||
849 | else if (dev->mc_count || (dev->flags&IFF_ALLMULTI)) { |
||
850 | outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast, ioaddr + EL3_CMD); |
||
851 | } |
||
852 | else |
||
853 | outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD); |
||
854 | spin_unlock_irqrestore(&lp->lock, flags); |
||
855 | } |
||
856 | |||
857 | static int |
||
858 | el3_close(struct device *dev) |
||
859 | { |
||
860 | int ioaddr = dev->base_addr; |
||
861 | |||
862 | if (el3_debug > 2) |
||
863 | printk("%s: Shutting down ethercard.\n", dev->name); |
||
864 | |||
865 | dev->tbusy = 1; |
||
866 | dev->start = 0; |
||
867 | |||
868 | /* Turn off statistics ASAP. We update lp->stats below. */ |
||
869 | outw(StatsDisable, ioaddr + EL3_CMD); |
||
870 | |||
871 | /* Disable the receiver and transmitter. */ |
||
872 | outw(RxDisable, ioaddr + EL3_CMD); |
||
873 | outw(TxDisable, ioaddr + EL3_CMD); |
||
874 | |||
875 | if (dev->if_port == 3) |
||
876 | /* Turn off thinnet power. Green! */ |
||
877 | outw(StopCoax, ioaddr + EL3_CMD); |
||
878 | else if (dev->if_port == 0) { |
||
879 | /* Disable link beat and jabber, if_port may change ere next open(). */ |
||
880 | EL3WINDOW(4); |
||
881 | outw(inw(ioaddr + WN4_MEDIA) & ~MEDIA_TP, ioaddr + WN4_MEDIA); |
||
882 | } |
||
883 | |||
884 | free_irq(dev->irq, dev); |
||
885 | /* Switching back to window 0 disables the IRQ. */ |
||
886 | EL3WINDOW(0); |
||
887 | /* But we explicitly zero the IRQ line select anyway. */ |
||
888 | outw(0x0f00, ioaddr + WN0_IRQ); |
||
889 | |||
890 | update_stats(dev); |
||
891 | MOD_DEC_USE_COUNT; |
||
892 | return 0; |
||
893 | } |
||
894 | |||
895 | #ifdef MODULE |
||
896 | /* Parameters that may be passed into the module. */ |
||
897 | static int debug = -1; |
||
898 | static int irq[] = {-1, -1, -1, -1, -1, -1, -1, -1}; |
||
899 | static int xcvr[] = {-1, -1, -1, -1, -1, -1, -1, -1}; |
||
900 | |||
901 | MODULE_PARM(debug,"i"); |
||
902 | MODULE_PARM(irq,"1-8i"); |
||
903 | MODULE_PARM(xcvr,"1-8i"); |
||
904 | |||
905 | int |
||
906 | init_module(void) |
||
907 | { |
||
908 | int el3_cards = 0; |
||
909 | |||
910 | if (debug >= 0) |
||
911 | el3_debug = debug; |
||
912 | |||
913 | el3_root_dev = NULL; |
||
914 | while (el3_probe(0) == 0) { |
||
915 | if (irq[el3_cards] > 1) |
||
916 | el3_root_dev->irq = irq[el3_cards]; |
||
917 | if (xcvr[el3_cards] >= 0) |
||
918 | el3_root_dev->if_port = xcvr[el3_cards]; |
||
919 | el3_cards++; |
||
920 | } |
||
921 | |||
922 | return el3_cards ? 0 : -ENODEV; |
||
923 | } |
||
924 | |||
925 | void |
||
926 | cleanup_module(void) |
||
927 | { |
||
928 | struct device *next_dev; |
||
929 | |||
930 | /* No need to check MOD_IN_USE, as sys_delete_module() checks. */ |
||
931 | while (el3_root_dev) { |
||
932 | struct el3_private *lp = (struct el3_private *)el3_root_dev->priv; |
||
933 | #ifdef CONFIG_MCA |
||
934 | if(lp->mca_slot!=-1) |
||
935 | mca_mark_as_unused(lp->mca_slot); |
||
936 | #endif |
||
937 | next_dev = lp->next_dev; |
||
938 | unregister_netdev(el3_root_dev); |
||
939 | release_region(el3_root_dev->base_addr, EL3_IO_EXTENT); |
||
940 | kfree(el3_root_dev); |
||
941 | el3_root_dev = next_dev; |
||
942 | } |
||
943 | } |
||
944 | #endif /* MODULE */ |
||
945 | |||
946 | /* |
||
947 | * Local variables: |
||
948 | * compile-command: "gcc -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -c 3c509.c" |
||
949 | * version-control: t |
||
950 | * kept-new-versions: 5 |
||
951 | * tab-width: 4 |
||
952 | * End: |
||
953 | */ |