make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / drivers / net / hp100.c
1 /*
2 ** hp100.c 
3 ** HP CASCADE Architecture Driver for 100VG-AnyLan Network Adapters
4 **
5 ** $Id: hp100.c,v 1.1.1.1 2005/04/11 02:50:27 jack Exp $
6 **
7 ** Based on the HP100 driver written by Jaroslav Kysela <perex@jcu.cz>
8 ** Extended for new busmaster capable chipsets by 
9 ** Siegfried "Frieder" Loeffler (dg1sek) <floeff@mathematik.uni-stuttgart.de>
10 **
11 ** Maintained by: Jaroslav Kysela <perex@suse.cz>
12 ** 
13 ** This driver has only been tested with
14 ** -- HP J2585B 10/100 Mbit/s PCI Busmaster
15 ** -- HP J2585A 10/100 Mbit/s PCI 
16 ** -- HP J2970  10 Mbit/s PCI Combo 10base-T/BNC
17 ** -- HP J2973  10 Mbit/s PCI 10base-T
18 ** -- HP J2573  10/100 ISA
19 ** -- Compex ReadyLink ENET100-VG4  10/100 Mbit/s PCI / EISA
20 ** -- Compex FreedomLine 100/VG  10/100 Mbit/s ISA / EISA / PCI
21 ** 
22 ** but it should also work with the other CASCADE based adapters.
23 **
24 ** TODO:
25 **       -  J2573 seems to hang sometimes when in shared memory mode.
26 **       -  Mode for Priority TX
27 **       -  Check PCI registers, performance might be improved?
28 **       -  To reduce interrupt load in busmaster, one could switch off
29 **          the interrupts that are used to refill the queues whenever the
30 **          queues are filled up to more than a certain threshold.
31 **       -  some updates for EISA version of card
32 **
33 **
34 **   This code is free software; you can redistribute it and/or modify
35 **   it under the terms of the GNU General Public License as published by
36 **   the Free Software Foundation; either version 2 of the License, or
37 **   (at your option) any later version.
38 **
39 **   This code is distributed in the hope that it will be useful,
40 **   but WITHOUT ANY WARRANTY; without even the implied warranty of
41 **   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42 **   GNU General Public License for more details.
43 **
44 **   You should have received a copy of the GNU General Public License
45 **   along with this program; if not, write to the Free Software
46 **   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
47 **
48 ** 1.57c -> 1.58
49 **   - used indent to change coding-style
50 **   - added KTI DP-200 EISA ID
51 **   - ioremap is also used for low (<1MB) memory (multi-architecture support)
52 **
53 ** 1.57b -> 1.57c - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
54 **   - release resources on failure in init_module
55 **
56 ** 1.57 -> 1.57b - Jean II
57 **   - fix spinlocks, SMP is now working !
58 **
59 ** 1.56 -> 1.57
60 **   - updates for new PCI interface for 2.1 kernels
61 **
62 ** 1.55 -> 1.56
63 **   - removed printk in misc. interrupt and update statistics to allow
64 **     monitoring of card status
65 **   - timing changes in xmit routines, relogin to 100VG hub added when
66 **     driver does reset
67 **   - included fix for Compex FreedomLine PCI adapter
68 ** 
69 ** 1.54 -> 1.55
70 **   - fixed bad initialization in init_module
71 **   - added Compex FreedomLine adapter
72 **   - some fixes in card initialization
73 **
74 ** 1.53 -> 1.54
75 **   - added hardware multicast filter support (doesn't work)
76 **   - little changes in hp100_sense_lan routine 
77 **     - added support for Coax and AUI (J2970)
78 **   - fix for multiple cards and hp100_mode parameter (insmod)
79 **   - fix for shared IRQ 
80 **
81 ** 1.52 -> 1.53
82 **   - fixed bug in multicast support
83 **
84 */
85
86 #define HP100_DEFAULT_PRIORITY_TX 0
87
88 #undef HP100_DEBUG
89 #undef HP100_DEBUG_B            /* Trace  */
90 #undef HP100_DEBUG_BM           /* Debug busmaster code (PDL stuff) */
91
92 #undef HP100_DEBUG_TRAINING     /* Debug login-to-hub procedure */
93 #undef HP100_DEBUG_TX
94 #undef HP100_DEBUG_IRQ
95 #undef HP100_DEBUG_RX
96
97 #undef HP100_MULTICAST_FILTER   /* Need to be debugged... */
98
99 #include <linux/version.h>
100 #include <linux/module.h>
101 #include <linux/kernel.h>
102 #include <linux/sched.h>
103 #include <linux/string.h>
104 #include <linux/errno.h>
105 #include <linux/ioport.h>
106 #include <linux/slab.h>
107 #include <linux/interrupt.h>
108 #include <linux/pci.h>
109 #include <linux/spinlock.h>
110 #include <asm/bitops.h>
111 #include <asm/io.h>
112
113 #include <linux/netdevice.h>
114 #include <linux/etherdevice.h>
115 #include <linux/skbuff.h>
116
117 #include <linux/types.h>
118 #include <linux/config.h>       /* for CONFIG_PCI */
119 #include <linux/delay.h>
120 #include <linux/init.h>
121
122 #define LINUX_2_1
123 typedef struct net_device_stats hp100_stats_t;
124 EXPORT_NO_SYMBOLS;
125
126 #include "hp100.h"
127
128 /*
129  *  defines
130  */
131
132 #define HP100_BUS_ISA     0
133 #define HP100_BUS_EISA    1
134 #define HP100_BUS_PCI     2
135
136 #ifndef PCI_DEVICE_ID_HP_J2585B
137 #define PCI_DEVICE_ID_HP_J2585B 0x1031
138 #endif
139 #ifndef PCI_VENDOR_ID_COMPEX
140 #define PCI_VENDOR_ID_COMPEX 0x11f6
141 #endif
142 #ifndef PCI_DEVICE_ID_COMPEX_ENET100VG4
143 #define PCI_DEVICE_ID_COMPEX_ENET100VG4 0x0112
144 #endif
145 #ifndef PCI_VENDOR_ID_COMPEX2
146 #define PCI_VENDOR_ID_COMPEX2 0x101a
147 #endif
148 #ifndef PCI_DEVICE_ID_COMPEX2_100VG
149 #define PCI_DEVICE_ID_COMPEX2_100VG 0x0005
150 #endif
151
152 #define HP100_REGION_SIZE       0x20    /* for ioports */
153
154 #define HP100_MAX_PACKET_SIZE   (1536+4)
155 #define HP100_MIN_PACKET_SIZE   60
156
157 #ifndef HP100_DEFAULT_RX_RATIO
158 /* default - 75% onboard memory on the card are used for RX packets */
159 #define HP100_DEFAULT_RX_RATIO  75
160 #endif
161
162 #ifndef HP100_DEFAULT_PRIORITY_TX
163 /* default - don't enable transmit outgoing packets as priority */
164 #define HP100_DEFAULT_PRIORITY_TX 0
165 #endif
166
167 /*
168  *  structures
169  */
170
171 struct hp100_eisa_id {
172         u_int id;
173         const char *name;
174         u_char bus;
175 };
176
177 struct hp100_pci_id {
178         u_short vendor;
179         u_short device;
180 };
181
182 struct hp100_private {
183         struct hp100_eisa_id *id;
184         spinlock_t lock;
185         u_short chip;
186         u_short soft_model;
187         u_int memory_size;
188         u_int virt_memory_size;
189         u_short rx_ratio;       /* 1 - 99 */
190         u_short priority_tx;    /* != 0 - priority tx */
191         u_short mode;           /* PIO, Shared Mem or Busmaster */
192         u_char bus;
193         struct pci_dev *pci_dev;
194         short mem_mapped;       /* memory mapped access */
195         void *mem_ptr_virt;     /* virtual memory mapped area, maybe NULL */
196         unsigned long mem_ptr_phys;     /* physical memory mapped area */
197         short lan_type;         /* 10Mb/s, 100Mb/s or -1 (error) */
198         int hub_status;         /* was login to hub successful? */
199         u_char mac1_mode;
200         u_char mac2_mode;
201         u_char hash_bytes[8];
202         hp100_stats_t stats;
203
204         /* Rings for busmaster mode: */
205         hp100_ring_t *rxrhead;  /* Head (oldest) index into rxring */
206         hp100_ring_t *rxrtail;  /* Tail (newest) index into rxring */
207         hp100_ring_t *txrhead;  /* Head (oldest) index into txring */
208         hp100_ring_t *txrtail;  /* Tail (newest) index into txring */
209
210         hp100_ring_t rxring[MAX_RX_PDL];
211         hp100_ring_t txring[MAX_TX_PDL];
212
213         u_int *page_vaddr_algn; /* Aligned virtual address of allocated page */
214         u_long whatever_offset; /* Offset to bus/phys/dma address */
215         int rxrcommit;          /* # Rx PDLs commited to adapter */
216         int txrcommit;          /* # Tx PDLs commited to adapter */
217 };
218
219 /*
220  *  variables
221  */
222
223 static struct hp100_eisa_id hp100_eisa_ids[] = {
224
225         /* 10/100 EISA card with revision A Cascade chip */
226         {0x80F1F022, "HP J2577 rev A", HP100_BUS_EISA},
227
228         /* 10/100 ISA card with revision A Cascade chip */
229         {0x50F1F022, "HP J2573 rev A", HP100_BUS_ISA},
230
231         /* 10 only EISA card with Cascade chip */
232         {0x2019F022, "HP 27248B", HP100_BUS_EISA},
233
234         /* 10/100 EISA card with Cascade chip */
235         {0x4019F022, "HP J2577", HP100_BUS_EISA},
236
237         /* 10/100 ISA card with Cascade chip */
238         {0x5019F022, "HP J2573", HP100_BUS_ISA},
239
240         /* 10/100 EISA card with AT&T chip */
241         {0x9019f022, "HP J2577", HP100_BUS_EISA },
242
243         /* 10/100 PCI card - old J2585A */
244         {0x1030103c, "HP J2585A", HP100_BUS_PCI},
245
246         /* 10/100 PCI card - new J2585B - master capable */
247         {0x1041103c, "HP J2585B", HP100_BUS_PCI},
248
249         /* 10 Mbit Combo Adapter */
250         {0x1042103c, "HP J2970", HP100_BUS_PCI},
251
252         /* 10 Mbit 10baseT Adapter */
253         {0x1040103c, "HP J2973", HP100_BUS_PCI},
254
255         /* 10/100 EISA card from Compex */
256         {0x0103180e, "ReadyLink ENET100-VG4", HP100_BUS_EISA},
257
258         /* 10/100 EISA card from Compex - FreedomLine (sq5bpf) */
259         /* Note: plhbrod@mbox.vol.cz reported that same ID have ISA */
260         /*       version of adapter, too... */
261         {0x0104180e, "FreedomLine 100/VG", HP100_BUS_EISA},
262
263         /* 10/100 PCI card from Compex - FreedomLine
264          *
265          * I think this card doesn't like aic7178 scsi controller, but
266          * I haven't tested this much. It works fine on diskless machines.
267          *                            Jacek Lipkowski <sq5bpf@acid.ch.pw.edu.pl>
268          */
269         {0x021211f6, "FreedomLine 100/VG", HP100_BUS_PCI},
270
271         /* 10/100 PCI card from Compex (J2585A compatible) */
272         {0x011211f6, "ReadyLink ENET100-VG4", HP100_BUS_PCI},
273         
274         /* 10/100 PCI card from KTI */
275         {0x40008e2e, "KTI DP-200", HP100_BUS_PCI }
276 };
277
278 #define HP100_EISA_IDS_SIZE     (sizeof(hp100_eisa_ids)/sizeof(struct hp100_eisa_id))
279
280 #ifdef CONFIG_PCI
281 static struct hp100_pci_id hp100_pci_ids[] = {
282         {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_J2585A},
283         {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_J2585B},
284         {PCI_VENDOR_ID_COMPEX, PCI_DEVICE_ID_COMPEX_ENET100VG4},
285         {PCI_VENDOR_ID_COMPEX2, PCI_DEVICE_ID_COMPEX2_100VG}
286 };
287 #endif
288
289 #define HP100_PCI_IDS_SIZE      (sizeof(hp100_pci_ids)/sizeof(struct hp100_pci_id))
290
291 #if LINUX_VERSION_CODE >= 0x20400
292 static struct pci_device_id hp100_pci_tbl[] __initdata = {
293         {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_J2585A, PCI_ANY_ID, PCI_ANY_ID,},
294         {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_J2585B, PCI_ANY_ID, PCI_ANY_ID,},
295         {PCI_VENDOR_ID_COMPEX, PCI_DEVICE_ID_COMPEX_ENET100VG4, PCI_ANY_ID, PCI_ANY_ID,},
296         {PCI_VENDOR_ID_COMPEX2, PCI_DEVICE_ID_COMPEX2_100VG, PCI_ANY_ID, PCI_ANY_ID,},
297         {}                      /* Terminating entry */
298 };
299 MODULE_DEVICE_TABLE(pci, hp100_pci_tbl);
300 #endif                          /* LINUX_VERSION_CODE >= 0x20400 */
301
302 static int hp100_rx_ratio = HP100_DEFAULT_RX_RATIO;
303 static int hp100_priority_tx = HP100_DEFAULT_PRIORITY_TX;
304 static int hp100_mode = 1;
305
306 MODULE_PARM(hp100_rx_ratio, "1i");
307 MODULE_PARM(hp100_priority_tx, "1i");
308 MODULE_PARM(hp100_mode, "1i");
309
310 /*
311  *  prototypes
312  */
313
314 static int hp100_probe1(struct net_device *dev, int ioaddr, u_char bus,
315                         struct pci_dev *pci_dev);
316
317
318 static int hp100_open(struct net_device *dev);
319 static int hp100_close(struct net_device *dev);
320 static int hp100_start_xmit(struct sk_buff *skb, struct net_device *dev);
321 static int hp100_start_xmit_bm(struct sk_buff *skb,
322                                struct net_device *dev);
323 static void hp100_rx(struct net_device *dev);
324 static hp100_stats_t *hp100_get_stats(struct net_device *dev);
325 static void hp100_misc_interrupt(struct net_device *dev);
326 static void hp100_update_stats(struct net_device *dev);
327 static void hp100_clear_stats(struct hp100_private *lp, int ioaddr);
328 static void hp100_set_multicast_list(struct net_device *dev);
329 static void hp100_interrupt(int irq, void *dev_id, struct pt_regs *regs);
330 static void hp100_start_interface(struct net_device *dev);
331 static void hp100_stop_interface(struct net_device *dev);
332 static void hp100_load_eeprom(struct net_device *dev, u_short ioaddr);
333 static int hp100_sense_lan(struct net_device *dev);
334 static int hp100_login_to_vg_hub(struct net_device *dev,
335                                  u_short force_relogin);
336 static int hp100_down_vg_link(struct net_device *dev);
337 static void hp100_cascade_reset(struct net_device *dev, u_short enable);
338 static void hp100_BM_shutdown(struct net_device *dev);
339 static void hp100_mmuinit(struct net_device *dev);
340 static void hp100_init_pdls(struct net_device *dev);
341 static int hp100_init_rxpdl(struct net_device *dev,
342                             register hp100_ring_t * ringptr,
343                             register u_int * pdlptr);
344 static int hp100_init_txpdl(struct net_device *dev,
345                             register hp100_ring_t * ringptr,
346                             register u_int * pdlptr);
347 static void hp100_rxfill(struct net_device *dev);
348 static void hp100_hwinit(struct net_device *dev);
349 static void hp100_clean_txring(struct net_device *dev);
350 #ifdef HP100_DEBUG
351 static void hp100_RegisterDump(struct net_device *dev);
352 #endif
353
354 /* Conversion to new PCI API :
355  * Convert an address in a kernel buffer to a bus/phys/dma address.
356  * This work *only* for memory fragments part of lp->page_vaddr,
357  * because it was properly DMA allocated via pci_alloc_consistent(),
358  * so we just need to "retreive" the original mapping to bus/phys/dma
359  * address - Jean II */
360 static inline dma_addr_t virt_to_whatever(struct net_device *dev, u32 * ptr)
361 {
362   return ((u_long) ptr) +
363     ((struct hp100_private *) (dev->priv))->whatever_offset;
364 }
365
366 /* TODO: This function should not really be needed in a good design... */
367 static void wait(void)
368 {
369         mdelay(1);
370 }
371
372 /*
373  *  probe functions
374  *  These functions should - if possible - avoid doing write operations
375  *  since this could cause problems when the card is not installed.
376  */
377
378 int __init hp100_probe(struct net_device *dev)
379 {
380         int base_addr = dev ? dev->base_addr : 0;
381         int ioaddr = 0;
382 #ifdef CONFIG_PCI
383         int pci_start_index = 0;
384 #endif
385
386 #ifdef HP100_DEBUG_B
387         hp100_outw(0x4200, TRACE);
388         printk("hp100: %s: probe\n", dev->name);
389 #endif
390
391         if (base_addr > 0xff) { /* Check a single specified location. */
392                 if (check_region(base_addr, HP100_REGION_SIZE))
393                         return -EINVAL;
394                 if (base_addr < 0x400)
395                         return hp100_probe1(dev, base_addr, HP100_BUS_ISA,
396                                             NULL);
397                 if (EISA_bus && base_addr >= 0x1c38 && ((base_addr - 0x1c38) & 0x3ff) == 0)
398                         return hp100_probe1(dev, base_addr, HP100_BUS_EISA, NULL);
399 #ifdef CONFIG_PCI
400                 printk("hp100: %s: You must specify card # in i/o address parameter for PCI bus...", dev->name);
401 #else
402                 return -ENODEV;
403 #endif
404         } else
405 #ifdef CONFIG_PCI
406                 if (base_addr > 0 && base_addr < 8 + 1)
407                         pci_start_index = 0x100 | (base_addr - 1);
408           else
409 #endif
410                 if (base_addr != 0)
411                         return -ENXIO;
412
413         /* First: scan PCI bus(es) */
414
415 #ifdef CONFIG_PCI
416         if (pcibios_present()) {
417                 int pci_index;
418                 struct pci_dev *pci_dev = NULL;
419                 int pci_id_index;
420                 u_short pci_command;
421
422 #ifdef HP100_DEBUG_PCI
423                 printk("hp100: %s: PCI BIOS is present, checking for devices..\n", dev->name);
424 #endif
425                 pci_index = 0;
426                 for (pci_id_index = 0; pci_id_index < HP100_PCI_IDS_SIZE;
427                      pci_id_index++) {
428                         while ((pci_dev = pci_find_device(hp100_pci_ids[pci_id_index].vendor,
429                                                           hp100_pci_ids[pci_id_index].device,
430                                                           pci_dev)) != NULL) {
431                                 if (pci_index < (pci_start_index & 7)) {
432                                         pci_index++;
433                                         continue;
434                                 }
435                                 if (pci_enable_device(pci_dev))
436                                         continue;
437                                 /* found... */
438                                 ioaddr = pci_resource_start(pci_dev, 0);
439                                 if (check_region(ioaddr, HP100_REGION_SIZE))
440                                         continue;
441                                 pci_read_config_word(pci_dev, PCI_COMMAND, &pci_command);
442                                 if (!(pci_command & PCI_COMMAND_IO)) {
443 #ifdef HP100_DEBUG
444                                         printk("hp100: %s: PCI I/O Bit has not been set. Setting...\n", dev->name);
445 #endif
446                                         pci_command |= PCI_COMMAND_IO;
447                                         pci_write_config_word(pci_dev, PCI_COMMAND, pci_command);
448                                 }
449                                 if (!(pci_command & PCI_COMMAND_MASTER)) {
450 #ifdef HP100_DEBUG
451                                         printk("hp100: %s: PCI Master Bit has not been set. Setting...\n", dev->name);
452 #endif
453                                         pci_command |= PCI_COMMAND_MASTER;
454                                         pci_write_config_word(pci_dev, PCI_COMMAND, pci_command);
455                                 }
456 #ifdef HP100_DEBUG
457                                 printk("hp100: %s: PCI adapter found at 0x%x\n", dev->name, ioaddr);
458 #endif
459                                 if (hp100_probe1(dev, ioaddr, HP100_BUS_PCI, pci_dev) == 0)
460                                         return 0;
461                         }
462                 }
463         }
464         if (pci_start_index > 0)
465                 return -ENODEV;
466 #endif /* CONFIG_PCI */
467
468         /* Second: Probe all EISA possible port regions (if EISA bus present) */
469         for (ioaddr = 0x1c38; EISA_bus && ioaddr < 0x10000; ioaddr += 0x400) {
470                 if (check_region(ioaddr, HP100_REGION_SIZE))
471                         continue;
472                 if (hp100_probe1(dev, ioaddr, HP100_BUS_EISA, NULL) == 0)
473                         return 0;
474         }
475
476         /* Third: Probe all ISA possible port regions */
477         for (ioaddr = 0x100; ioaddr < 0x400; ioaddr += 0x20) {
478                 if (check_region(ioaddr, HP100_REGION_SIZE))
479                         continue;
480                 if (hp100_probe1(dev, ioaddr, HP100_BUS_ISA, NULL) == 0)
481                         return 0;
482         }
483
484         return -ENODEV;
485 }
486
487 static int __init hp100_probe1(struct net_device *dev, int ioaddr,
488                                u_char bus, struct pci_dev *pci_dev)
489 {
490         int i;
491
492         u_char uc, uc_1;
493         u_int eisa_id;
494         u_int chip;
495         u_int memory_size = 0, virt_memory_size = 0;
496         u_short local_mode, lsw;
497         short mem_mapped;
498         unsigned long mem_ptr_phys;
499         void **mem_ptr_virt;
500         struct hp100_private *lp;
501         struct hp100_eisa_id *eid;
502
503 #ifdef HP100_DEBUG_B
504         hp100_outw(0x4201, TRACE);
505         printk("hp100: %s: probe1\n", dev->name);
506 #endif
507
508         if (dev == NULL) {
509 #ifdef HP100_DEBUG
510                 printk("hp100_probe1: %s: dev == NULL ?\n", dev->name);
511 #endif
512                 return -EIO;
513         }
514
515         if (hp100_inw(HW_ID) != HP100_HW_ID_CASCADE) {
516                 return -ENODEV;
517         } else {
518                 chip = hp100_inw(PAGING) & HP100_CHIPID_MASK;
519 #ifdef HP100_DEBUG
520                 if (chip == HP100_CHIPID_SHASTA)
521                         printk("hp100: %s: Shasta Chip detected. (This is a pre 802.12 chip)\n", dev->name);
522                 else if (chip == HP100_CHIPID_RAINIER)
523                         printk("hp100: %s: Rainier Chip detected. (This is a pre 802.12 chip)\n", dev->name);
524                 else if (chip == HP100_CHIPID_LASSEN)
525                         printk("hp100: %s: Lassen Chip detected.\n", dev->name);
526                 else
527                         printk("hp100: %s: Warning: Unknown CASCADE chip (id=0x%.4x).\n", dev->name, chip);
528 #endif
529         }
530
531         dev->base_addr = ioaddr;
532
533         hp100_page(ID_MAC_ADDR);
534         for (i = uc = eisa_id = 0; i < 4; i++) {
535                 eisa_id >>= 8;
536                 uc_1 = hp100_inb(BOARD_ID + i);
537                 eisa_id |= uc_1 << 24;
538                 uc += uc_1;
539         }
540         uc += hp100_inb(BOARD_ID + 4);
541
542         if (uc != 0xff) {       /* bad checksum? */
543                 printk("hp100_probe: %s: bad EISA ID checksum at base port 0x%x\n", dev->name, ioaddr);
544                 return -ENODEV;
545         }
546
547         for (i = 0; i < HP100_EISA_IDS_SIZE; i++)
548                 if (hp100_eisa_ids[i].id == eisa_id)
549                         break;
550         if (i >= HP100_EISA_IDS_SIZE) {
551                 for (i = 0; i < HP100_EISA_IDS_SIZE; i++)
552                         if ((hp100_eisa_ids[i].id & 0xf0ffffff) == (eisa_id & 0xf0ffffff))
553                                 break;
554                 if (i >= HP100_EISA_IDS_SIZE) {
555                         printk ("hp100_probe: %s: card at port 0x%x isn't known (id = 0x%x)\n", dev->name, ioaddr, eisa_id);
556                         return -ENODEV;
557                 }
558         }
559         eid = &hp100_eisa_ids[i];
560         if ((eid->id & 0x0f000000) < (eisa_id & 0x0f000000)) {
561                 printk("hp100_probe: %s: newer version of card %s at port 0x%x - unsupported\n", dev->name, eid->name, ioaddr);
562                 return -ENODEV;
563         }
564
565         for (i = uc = 0; i < 7; i++)
566                 uc += hp100_inb(LAN_ADDR + i);
567         if (uc != 0xff) {
568                 printk("hp100_probe: %s: bad lan address checksum (card %s at port 0x%x)\n", dev->name, eid->name, ioaddr);
569                 return -EIO;
570         }
571
572         /* Make sure, that all registers are correctly updated... */
573
574         hp100_load_eeprom(dev, ioaddr);
575         wait();
576
577         /*
578          * Determine driver operation mode
579          *
580          * Use the variable "hp100_mode" upon insmod or as kernel parameter to
581          * force driver modes:
582          * hp100_mode=1 -> default, use busmaster mode if configured.
583          * hp100_mode=2 -> enable shared memory mode 
584          * hp100_mode=3 -> force use of i/o mapped mode.
585          * hp100_mode=4 -> same as 1, but re-set the enable bit on the card.
586          */
587
588         /*
589          * LSW values:
590          *   0x2278 -> J2585B, PnP shared memory mode
591          *   0x2270 -> J2585B, shared memory mode, 0xdc000
592          *   0xa23c -> J2585B, I/O mapped mode
593          *   0x2240 -> EISA COMPEX, BusMaster (Shasta Chip)
594          *   0x2220 -> EISA HP, I/O (Shasta Chip)
595          *   0x2260 -> EISA HP, BusMaster (Shasta Chip)
596          */
597
598 #if 0
599         local_mode = 0x2270;
600         hp100_outw(0xfefe, OPTION_LSW);
601         hp100_outw(local_mode | HP100_SET_LB | HP100_SET_HB, OPTION_LSW);
602 #endif
603
604         /* hp100_mode value maybe used in future by another card */
605         local_mode = hp100_mode;
606         if (local_mode < 1 || local_mode > 4)
607                 local_mode = 1; /* default */
608 #ifdef HP100_DEBUG
609         printk("hp100: %s: original LSW = 0x%x\n", dev->name,
610                hp100_inw(OPTION_LSW));
611 #endif
612
613         if (local_mode == 3) {
614                 hp100_outw(HP100_MEM_EN | HP100_RESET_LB, OPTION_LSW);
615                 hp100_outw(HP100_IO_EN | HP100_SET_LB, OPTION_LSW);
616                 hp100_outw(HP100_BM_WRITE | HP100_BM_READ | HP100_RESET_HB, OPTION_LSW);
617                 printk("hp100: %s: IO mapped mode forced.\n", dev->name);
618         } else if (local_mode == 2) {
619                 hp100_outw(HP100_MEM_EN | HP100_SET_LB, OPTION_LSW);
620                 hp100_outw(HP100_IO_EN | HP100_SET_LB, OPTION_LSW);
621                 hp100_outw(HP100_BM_WRITE | HP100_BM_READ | HP100_RESET_HB, OPTION_LSW);
622                 printk("hp100: %s: Shared memory mode requested.\n", dev->name);
623         } else if (local_mode == 4) {
624                 if (chip == HP100_CHIPID_LASSEN) {
625                         hp100_outw(HP100_BM_WRITE | HP100_BM_READ | HP100_SET_HB, OPTION_LSW);
626                         hp100_outw(HP100_IO_EN | HP100_MEM_EN | HP100_RESET_LB, OPTION_LSW);
627                         printk("hp100: %s: Busmaster mode requested.\n", dev->name);
628                 }
629                 local_mode = 1;
630         }
631
632         if (local_mode == 1) {  /* default behaviour */
633                 lsw = hp100_inw(OPTION_LSW);
634
635                 if ((lsw & HP100_IO_EN) && (~lsw & HP100_MEM_EN) &&
636                     (~lsw & (HP100_BM_WRITE | HP100_BM_READ))) {
637 #ifdef HP100_DEBUG
638                         printk("hp100: %s: IO_EN bit is set on card.\n", dev->name);
639 #endif
640                         local_mode = 3;
641                 } else if (chip == HP100_CHIPID_LASSEN &&
642                            (lsw & (HP100_BM_WRITE | HP100_BM_READ)) == (HP100_BM_WRITE | HP100_BM_READ)) {
643                         /* Conversion to new PCI API :
644                          * I don't have the doc, but I assume that the card
645                          * can map the full 32bit address space.
646                          * Also, we can have EISA Busmaster cards (not tested),
647                          * so beware !!! - Jean II */
648                         if((bus == HP100_BUS_PCI) &&
649                            (pci_set_dma_mask(pci_dev, 0xffffffff))) {
650                                 /* Gracefully fallback to shared memory */
651                                 goto busmasterfail;
652                         }
653                         printk("hp100: %s: Busmaster mode enabled.\n", dev->name);
654                         hp100_outw(HP100_MEM_EN | HP100_IO_EN | HP100_RESET_LB, OPTION_LSW);
655                 } else {
656                 busmasterfail:
657 #ifdef HP100_DEBUG
658                         printk("hp100: %s: Card not configured for BM or BM not supported with this card.\n", dev->name);
659                         printk("hp100: %s: Trying shared memory mode.\n", dev->name);
660 #endif
661                         /* In this case, try shared memory mode */
662                         local_mode = 2;
663                         hp100_outw(HP100_MEM_EN | HP100_SET_LB, OPTION_LSW);
664                         /* hp100_outw(HP100_IO_EN|HP100_RESET_LB, OPTION_LSW); */
665                 }
666         }
667 #ifdef HP100_DEBUG
668         printk("hp100: %s: new LSW = 0x%x\n", dev->name, hp100_inw(OPTION_LSW));
669 #endif
670
671         /* Check for shared memory on the card, eventually remap it */
672         hp100_page(HW_MAP);
673         mem_mapped = ((hp100_inw(OPTION_LSW) & (HP100_MEM_EN)) != 0);
674         mem_ptr_phys = 0UL;
675         mem_ptr_virt = NULL;
676         memory_size = (8192 << ((hp100_inb(SRAM) >> 5) & 0x07));
677         virt_memory_size = 0;
678
679         /* For memory mapped or busmaster mode, we want the memory address */
680         if (mem_mapped || (local_mode == 1)) {
681                 mem_ptr_phys = (hp100_inw(MEM_MAP_LSW) | (hp100_inw(MEM_MAP_MSW) << 16));
682                 mem_ptr_phys &= ~0x1fff;        /* 8k alignment */
683
684                 if (bus == HP100_BUS_ISA && (mem_ptr_phys & ~0xfffff) != 0) {
685                         printk("hp100: %s: Can only use programmed i/o mode.\n", dev->name);
686                         mem_ptr_phys = 0;
687                         mem_mapped = 0;
688                         local_mode = 3; /* Use programmed i/o */
689                 }
690
691                 /* We do not need access to shared memory in busmaster mode */
692                 /* However in slave mode we need to remap high (>1GB) card memory  */
693                 if (local_mode != 1) {  /* = not busmaster */
694                         /* We try with smaller memory sizes, if ioremap fails */
695                         for (virt_memory_size = memory_size; virt_memory_size > 16383; virt_memory_size >>= 1) {
696                                 if ((mem_ptr_virt = ioremap((u_long) mem_ptr_phys, virt_memory_size)) == NULL) {
697 #ifdef HP100_DEBUG
698                                         printk("hp100: %s: ioremap for 0x%x bytes high PCI memory at 0x%lx failed\n", dev->name, virt_memory_size, mem_ptr_phys);
699 #endif
700                                 } else {
701 #ifdef HP100_DEBUG
702                                         printk("hp100: %s: remapped 0x%x bytes high PCI memory at 0x%lx to %p.\n", dev->name, virt_memory_size, mem_ptr_phys, mem_ptr_virt);
703 #endif
704                                         break;
705                                 }
706                         }
707
708                         if (mem_ptr_virt == NULL) {     /* all ioremap tries failed */
709                                 printk("hp100: %s: Failed to ioremap the PCI card memory. Will have to use i/o mapped mode.\n", dev->name);
710                                 local_mode = 3;
711                                 virt_memory_size = 0;
712                         }
713                 }
714         }
715
716         if (local_mode == 3) {  /* io mapped forced */
717                 mem_mapped = 0;
718                 mem_ptr_phys = 0;
719                 mem_ptr_virt = NULL;
720                 printk("hp100: %s: Using (slow) programmed i/o mode.\n", dev->name);
721         }
722
723         /* Initialise the "private" data structure for this card. */
724         if ((dev->priv = kmalloc(sizeof(struct hp100_private), GFP_KERNEL)) == NULL)
725                 return -ENOMEM;
726
727         lp = (struct hp100_private *) dev->priv;
728         memset(lp, 0, sizeof(struct hp100_private));
729         spin_lock_init(&lp->lock);
730         lp->id = eid;
731         lp->chip = chip;
732         lp->mode = local_mode;
733         lp->bus = bus;
734         lp->pci_dev = pci_dev;
735         lp->priority_tx = hp100_priority_tx;
736         lp->rx_ratio = hp100_rx_ratio;
737         lp->mem_ptr_phys = mem_ptr_phys;
738         lp->mem_ptr_virt = mem_ptr_virt;
739         hp100_page(ID_MAC_ADDR);
740         lp->soft_model = hp100_inb(SOFT_MODEL);
741         lp->mac1_mode = HP100_MAC1MODE3;
742         lp->mac2_mode = HP100_MAC2MODE3;
743         memset(&lp->hash_bytes, 0x00, 8);
744
745         dev->base_addr = ioaddr;
746
747         lp->memory_size = memory_size;
748         lp->virt_memory_size = virt_memory_size;
749         lp->rx_ratio = hp100_rx_ratio;  /* can be conf'd with insmod */
750
751         /* memory region for programmed i/o */
752         request_region(dev->base_addr, HP100_REGION_SIZE, eid->name);
753
754         dev->open = hp100_open;
755         dev->stop = hp100_close;
756
757         if (lp->mode == 1)      /* busmaster */
758                 dev->hard_start_xmit = hp100_start_xmit_bm;
759         else
760                 dev->hard_start_xmit = hp100_start_xmit;
761
762         dev->get_stats = hp100_get_stats;
763         dev->set_multicast_list = &hp100_set_multicast_list;
764
765         /* Ask the card for which IRQ line it is configured */
766         if (bus == HP100_BUS_PCI) {
767                 dev->irq = pci_dev->irq;
768         } else {
769                 hp100_page(HW_MAP);
770                 dev->irq = hp100_inb(IRQ_CHANNEL) & HP100_IRQMASK;
771                 if (dev->irq == 2)
772                         dev->irq = 9;
773         }
774
775         if (lp->mode == 1)      /* busmaster */
776                 dev->dma = 4;
777
778         /* Ask the card for its MAC address and store it for later use. */
779         hp100_page(ID_MAC_ADDR);
780         for (i = uc = 0; i < 6; i++)
781                 dev->dev_addr[i] = hp100_inb(LAN_ADDR + i);
782
783         /* Reset statistics (counters) */
784         hp100_clear_stats(lp, ioaddr);
785
786         SET_MODULE_OWNER(dev);
787         ether_setup(dev);
788
789         /* If busmaster mode is wanted, a dma-capable memory area is needed for
790          * the rx and tx PDLs 
791          * PCI cards can access the whole PC memory. Therefore GFP_DMA is not
792          * needed for the allocation of the memory area. 
793          */
794
795         /* TODO: We do not need this with old cards, where PDLs are stored
796          * in the cards shared memory area. But currently, busmaster has been
797          * implemented/tested only with the lassen chip anyway... */
798         if (lp->mode == 1) {    /* busmaster */
799                 dma_addr_t page_baddr;
800                 /* Get physically continous memory for TX & RX PDLs    */
801                 /* Conversion to new PCI API :
802                  * Pages are always aligned and zeroed, no need to it ourself.
803                  * Doc says should be OK for EISA bus as well - Jean II */
804                 if ((lp->page_vaddr_algn = pci_alloc_consistent(lp->pci_dev, MAX_RINGSIZE, &page_baddr)) == NULL)
805                         return -ENOMEM;
806                 lp->whatever_offset = ((u_long) page_baddr) - ((u_long) lp->page_vaddr_algn);
807
808 #ifdef HP100_DEBUG_BM
809                 printk("hp100: %s: Reserved DMA memory from 0x%x to 0x%x\n", dev->name, (u_int) lp->page_vaddr_algn, (u_int) lp->page_vaddr_algn + MAX_RINGSIZE);
810 #endif
811                 lp->rxrcommit = lp->txrcommit = 0;
812                 lp->rxrhead = lp->rxrtail = &(lp->rxring[0]);
813                 lp->txrhead = lp->txrtail = &(lp->txring[0]);
814         }
815
816         /* Initialise the card. */
817         /* (I'm not really sure if it's a good idea to do this during probing, but 
818          * like this it's assured that the lan connection type can be sensed
819          * correctly)
820          */
821         hp100_hwinit(dev);
822
823         /* Try to find out which kind of LAN the card is connected to. */
824         lp->lan_type = hp100_sense_lan(dev);
825
826         /* Print out a message what about what we think we have probed. */
827         printk("hp100: %s: %s at 0x%x, IRQ %d, ", dev->name, lp->id->name, ioaddr, dev->irq);
828         switch (bus) {
829         case HP100_BUS_EISA:
830                 printk("EISA");
831                 break;
832         case HP100_BUS_PCI:
833                 printk("PCI");
834                 break;
835         default:
836                 printk("ISA");
837                 break;
838         }
839         printk(" bus, %dk SRAM (rx/tx %d%%).\n", lp->memory_size >> 10, lp->rx_ratio);
840
841         if (lp->mode == 2) {    /* memory mapped */
842                 printk("hp100: %s: Memory area at 0x%lx-0x%lx", dev->name, mem_ptr_phys,
843                                 (mem_ptr_phys + (mem_ptr_phys > 0x100000 ? (u_long) lp->memory_size : 16 * 1024)) - 1);
844                 if (mem_ptr_virt)
845                         printk(" (virtual base %p)", mem_ptr_virt);
846                 printk(".\n");
847
848                 /* Set for info when doing ifconfig */
849                 dev->mem_start = mem_ptr_phys;
850                 dev->mem_end = mem_ptr_phys + lp->memory_size;
851         }
852         printk("hp100: %s: ", dev->name);
853         if (lp->lan_type != HP100_LAN_ERR)
854                 printk("Adapter is attached to ");
855         switch (lp->lan_type) {
856         case HP100_LAN_100:
857                 printk("100Mb/s Voice Grade AnyLAN network.\n");
858                 break;
859         case HP100_LAN_10:
860                 printk("10Mb/s network.\n");
861                 break;
862         default:
863                 printk("Warning! Link down.\n");
864         }
865
866         return 0;
867 }
868
869 /* This procedure puts the card into a stable init state */
870 static void hp100_hwinit(struct net_device *dev)
871 {
872         int ioaddr = dev->base_addr;
873         struct hp100_private *lp = (struct hp100_private *) dev->priv;
874
875 #ifdef HP100_DEBUG_B
876         hp100_outw(0x4202, TRACE);
877         printk("hp100: %s: hwinit\n", dev->name);
878 #endif
879
880         /* Initialise the card. -------------------------------------------- */
881
882         /* Clear all pending Ints and disable Ints */
883         hp100_page(PERFORMANCE);
884         hp100_outw(0xfefe, IRQ_MASK);   /* mask off all ints */
885         hp100_outw(0xffff, IRQ_STATUS); /* clear all pending ints */
886
887         hp100_outw(HP100_INT_EN | HP100_RESET_LB, OPTION_LSW);
888         hp100_outw(HP100_TRI_INT | HP100_SET_HB, OPTION_LSW);
889
890         if (lp->mode == 1) {
891                 hp100_BM_shutdown(dev); /* disables BM, puts cascade in reset */
892                 wait();
893         } else {
894                 hp100_outw(HP100_INT_EN | HP100_RESET_LB, OPTION_LSW);
895                 hp100_cascade_reset(dev, TRUE);
896                 hp100_page(MAC_CTRL);
897                 hp100_andb(~(HP100_RX_EN | HP100_TX_EN), MAC_CFG_1);
898         }
899
900         /* Initiate EEPROM reload */
901         hp100_load_eeprom(dev, 0);
902
903         wait();
904
905         /* Go into reset again. */
906         hp100_cascade_reset(dev, TRUE);
907
908         /* Set Option Registers to a safe state  */
909         hp100_outw(HP100_DEBUG_EN |
910                    HP100_RX_HDR |
911                    HP100_EE_EN |
912                    HP100_BM_WRITE |
913                    HP100_BM_READ | HP100_RESET_HB |
914                    HP100_FAKE_INT |
915                    HP100_INT_EN |
916                    HP100_MEM_EN |
917                    HP100_IO_EN | HP100_RESET_LB, OPTION_LSW);
918
919         hp100_outw(HP100_TRI_INT |
920                    HP100_MMAP_DIS | HP100_SET_HB, OPTION_LSW);
921
922         hp100_outb(HP100_PRIORITY_TX |
923                    HP100_ADV_NXT_PKT |
924                    HP100_TX_CMD | HP100_RESET_LB, OPTION_MSW);
925
926         /* TODO: Configure MMU for Ram Test. */
927         /* TODO: Ram Test. */
928
929         /* Re-check if adapter is still at same i/o location      */
930         /* (If the base i/o in eeprom has been changed but the    */
931         /* registers had not been changed, a reload of the eeprom */
932         /* would move the adapter to the address stored in eeprom */
933
934         /* TODO: Code to implement. */
935
936         /* Until here it was code from HWdiscover procedure. */
937         /* Next comes code from mmuinit procedure of SCO BM driver which is
938          * called from HWconfigure in the SCO driver.  */
939
940         /* Initialise MMU, eventually switch on Busmaster Mode, initialise 
941          * multicast filter...
942          */
943         hp100_mmuinit(dev);
944
945         /* We don't turn the interrupts on here - this is done by start_interface. */
946         wait();                 /* TODO: Do we really need this? */
947
948         /* Enable Hardware (e.g. unreset) */
949         hp100_cascade_reset(dev, FALSE);
950
951         /* ------- initialisation complete ----------- */
952
953         /* Finally try to log in the Hub if there may be a VG connection. */
954         if (lp->lan_type != HP100_LAN_10)
955                 hp100_login_to_vg_hub(dev, FALSE);      /* relogin */
956 }
957 \f
958
959 /* 
960  * mmuinit - Reinitialise Cascade MMU and MAC settings.
961  * Note: Must already be in reset and leaves card in reset. 
962  */
963 static void hp100_mmuinit(struct net_device *dev)
964 {
965         int ioaddr = dev->base_addr;
966         struct hp100_private *lp = (struct hp100_private *) dev->priv;
967         int i;
968
969 #ifdef HP100_DEBUG_B
970         hp100_outw(0x4203, TRACE);
971         printk("hp100: %s: mmuinit\n", dev->name);
972 #endif
973
974 #ifdef HP100_DEBUG
975         if (0 != (hp100_inw(OPTION_LSW) & HP100_HW_RST)) {
976                 printk("hp100: %s: Not in reset when entering mmuinit. Fix me.\n", dev->name);
977                 return;
978         }
979 #endif
980
981         /* Make sure IRQs are masked off and ack'ed. */
982         hp100_page(PERFORMANCE);
983         hp100_outw(0xfefe, IRQ_MASK);   /* mask off all ints */
984         hp100_outw(0xffff, IRQ_STATUS); /* ack IRQ */
985
986         /*
987          * Enable Hardware 
988          * - Clear Debug En, Rx Hdr Pipe, EE En, I/O En, Fake Int and Intr En
989          * - Set Tri-State Int, Bus Master Rd/Wr, and Mem Map Disable
990          * - Clear Priority, Advance Pkt and Xmit Cmd
991          */
992
993         hp100_outw(HP100_DEBUG_EN |
994                    HP100_RX_HDR |
995                    HP100_EE_EN | HP100_RESET_HB |
996                    HP100_IO_EN |
997                    HP100_FAKE_INT |
998                    HP100_INT_EN | HP100_RESET_LB, OPTION_LSW);
999
1000         hp100_outw(HP100_TRI_INT | HP100_SET_HB, OPTION_LSW);
1001
1002         if (lp->mode == 1) {    /* busmaster */
1003                 hp100_outw(HP100_BM_WRITE |
1004                            HP100_BM_READ |
1005                            HP100_MMAP_DIS | HP100_SET_HB, OPTION_LSW);
1006         } else if (lp->mode == 2) {     /* memory mapped */
1007                 hp100_outw(HP100_BM_WRITE |
1008                            HP100_BM_READ | HP100_RESET_HB, OPTION_LSW);
1009                 hp100_outw(HP100_MMAP_DIS | HP100_RESET_HB, OPTION_LSW);
1010                 hp100_outw(HP100_MEM_EN | HP100_SET_LB, OPTION_LSW);
1011                 hp100_outw(HP100_IO_EN | HP100_SET_LB, OPTION_LSW);
1012         } else if (lp->mode == 3) {     /* i/o mapped mode */
1013                 hp100_outw(HP100_MMAP_DIS | HP100_SET_HB |
1014                            HP100_IO_EN | HP100_SET_LB, OPTION_LSW);
1015         }
1016
1017         hp100_page(HW_MAP);
1018         hp100_outb(0, EARLYRXCFG);
1019         hp100_outw(0, EARLYTXCFG);
1020
1021         /*
1022          * Enable Bus Master mode
1023          */
1024         if (lp->mode == 1) {    /* busmaster */
1025                 /* Experimental: Set some PCI configuration bits */
1026                 hp100_page(HW_MAP);
1027                 hp100_andb(~HP100_PDL_USE3, MODECTRL1); /* BM engine read maximum */
1028                 hp100_andb(~HP100_TX_DUALQ, MODECTRL1); /* No Queue for Priority TX */
1029
1030                 /* PCI Bus failures should result in a Misc. Interrupt */
1031                 hp100_orb(HP100_EN_BUS_FAIL, MODECTRL2);
1032
1033                 hp100_outw(HP100_BM_READ | HP100_BM_WRITE | HP100_SET_HB, OPTION_LSW);
1034                 hp100_page(HW_MAP);
1035                 /* Use Burst Mode and switch on PAGE_CK */
1036                 hp100_orb(HP100_BM_BURST_RD | HP100_BM_BURST_WR, BM);
1037                 if ((lp->chip == HP100_CHIPID_RAINIER) || (lp->chip == HP100_CHIPID_SHASTA))
1038                         hp100_orb(HP100_BM_PAGE_CK, BM);
1039                 hp100_orb(HP100_BM_MASTER, BM);
1040         } else {                /* not busmaster */
1041
1042                 hp100_page(HW_MAP);
1043                 hp100_andb(~HP100_BM_MASTER, BM);
1044         }
1045
1046         /*
1047          * Divide card memory into regions for Rx, Tx and, if non-ETR chip, PDLs
1048          */
1049         hp100_page(MMU_CFG);
1050         if (lp->mode == 1) {    /* only needed for Busmaster */
1051                 int xmit_stop, recv_stop;
1052
1053                 if ((lp->chip == HP100_CHIPID_RAINIER)
1054                     || (lp->chip == HP100_CHIPID_SHASTA)) {
1055                         int pdl_stop;
1056
1057                         /*
1058                          * Each pdl is 508 bytes long. (63 frags * 4 bytes for address and
1059                          * 4 bytes for header). We will leave NUM_RXPDLS * 508 (rounded
1060                          * to the next higher 1k boundary) bytes for the rx-pdl's
1061                          * Note: For non-etr chips the transmit stop register must be
1062                          * programmed on a 1k boundary, i.e. bits 9:0 must be zero. 
1063                          */
1064                         pdl_stop = lp->memory_size;
1065                         xmit_stop = (pdl_stop - 508 * (MAX_RX_PDL) - 16) & ~(0x03ff);
1066                         recv_stop = (xmit_stop * (lp->rx_ratio) / 100) & ~(0x03ff);
1067                         hp100_outw((pdl_stop >> 4) - 1, PDL_MEM_STOP);
1068 #ifdef HP100_DEBUG_BM
1069                         printk("hp100: %s: PDL_STOP = 0x%x\n", dev->name, pdl_stop);
1070 #endif
1071                 } else {
1072                         /* ETR chip (Lassen) in busmaster mode */
1073                         xmit_stop = (lp->memory_size) - 1;
1074                         recv_stop = ((lp->memory_size * lp->rx_ratio) / 100) & ~(0x03ff);
1075                 }
1076
1077                 hp100_outw(xmit_stop >> 4, TX_MEM_STOP);
1078                 hp100_outw(recv_stop >> 4, RX_MEM_STOP);
1079 #ifdef HP100_DEBUG_BM
1080                 printk("hp100: %s: TX_STOP  = 0x%x\n", dev->name, xmit_stop >> 4);
1081                 printk("hp100: %s: RX_STOP  = 0x%x\n", dev->name, recv_stop >> 4);
1082 #endif
1083         } else {
1084                 /* Slave modes (memory mapped and programmed io)  */
1085                 hp100_outw((((lp->memory_size * lp->rx_ratio) / 100) >> 4), RX_MEM_STOP);
1086                 hp100_outw(((lp->memory_size - 1) >> 4), TX_MEM_STOP);
1087 #ifdef HP100_DEBUG
1088                 printk("hp100: %s: TX_MEM_STOP: 0x%x\n", dev->name, hp100_inw(TX_MEM_STOP));
1089                 printk("hp100: %s: RX_MEM_STOP: 0x%x\n", dev->name, hp100_inw(RX_MEM_STOP));
1090 #endif
1091         }
1092
1093         /* Write MAC address into page 1 */
1094         hp100_page(MAC_ADDRESS);
1095         for (i = 0; i < 6; i++)
1096                 hp100_outb(dev->dev_addr[i], MAC_ADDR + i);
1097
1098         /* Zero the multicast hash registers */
1099         for (i = 0; i < 8; i++)
1100                 hp100_outb(0x0, HASH_BYTE0 + i);
1101
1102         /* Set up MAC defaults */
1103         hp100_page(MAC_CTRL);
1104
1105         /* Go to LAN Page and zero all filter bits */
1106         /* Zero accept error, accept multicast, accept broadcast and accept */
1107         /* all directed packet bits */
1108         hp100_andb(~(HP100_RX_EN |
1109                      HP100_TX_EN |
1110                      HP100_ACC_ERRORED |
1111                      HP100_ACC_MC |
1112                      HP100_ACC_BC | HP100_ACC_PHY), MAC_CFG_1);
1113
1114         hp100_outb(0x00, MAC_CFG_2);
1115
1116         /* Zero the frame format bit. This works around a training bug in the */
1117         /* new hubs. */
1118         hp100_outb(0x00, VG_LAN_CFG_2); /* (use 802.3) */
1119
1120         if (lp->priority_tx)
1121                 hp100_outb(HP100_PRIORITY_TX | HP100_SET_LB, OPTION_MSW);
1122         else
1123                 hp100_outb(HP100_PRIORITY_TX | HP100_RESET_LB, OPTION_MSW);
1124
1125         hp100_outb(HP100_ADV_NXT_PKT |
1126                    HP100_TX_CMD | HP100_RESET_LB, OPTION_MSW);
1127
1128         /* If busmaster, initialize the PDLs */
1129         if (lp->mode == 1)
1130                 hp100_init_pdls(dev);
1131
1132         /* Go to performance page and initalize isr and imr registers */
1133         hp100_page(PERFORMANCE);
1134         hp100_outw(0xfefe, IRQ_MASK);   /* mask off all ints */
1135         hp100_outw(0xffff, IRQ_STATUS); /* ack IRQ */
1136 }
1137
1138 /*
1139  *  open/close functions
1140  */
1141
1142 static int hp100_open(struct net_device *dev)
1143 {
1144         struct hp100_private *lp = (struct hp100_private *) dev->priv;
1145 #ifdef HP100_DEBUG_B
1146         int ioaddr = dev->base_addr;
1147 #endif
1148
1149 #ifdef HP100_DEBUG_B
1150         hp100_outw(0x4204, TRACE);
1151         printk("hp100: %s: open\n", dev->name);
1152 #endif
1153
1154         /* New: if bus is PCI or EISA, interrupts might be shared interrupts */
1155         if (request_irq(dev->irq, hp100_interrupt,
1156                         lp->bus == HP100_BUS_PCI || lp->bus ==
1157                         HP100_BUS_EISA ? SA_SHIRQ : SA_INTERRUPT,
1158                         lp->id->name, dev)) {
1159                 printk("hp100: %s: unable to get IRQ %d\n", dev->name, dev->irq);
1160                 return -EAGAIN;
1161         }
1162
1163         dev->trans_start = jiffies;
1164         netif_start_queue(dev);
1165
1166         lp->lan_type = hp100_sense_lan(dev);
1167         lp->mac1_mode = HP100_MAC1MODE3;
1168         lp->mac2_mode = HP100_MAC2MODE3;
1169         memset(&lp->hash_bytes, 0x00, 8);
1170
1171         hp100_stop_interface(dev);
1172
1173         hp100_hwinit(dev);
1174
1175         hp100_start_interface(dev);     /* sets mac modes, enables interrupts */
1176
1177         return 0;
1178 }
1179
1180 /* The close function is called when the interface is to be brought down */
1181 static int hp100_close(struct net_device *dev)
1182 {
1183         int ioaddr = dev->base_addr;
1184         struct hp100_private *lp = (struct hp100_private *) dev->priv;
1185
1186 #ifdef HP100_DEBUG_B
1187         hp100_outw(0x4205, TRACE);
1188         printk("hp100: %s: close\n", dev->name);
1189 #endif
1190
1191         hp100_page(PERFORMANCE);
1192         hp100_outw(0xfefe, IRQ_MASK);   /* mask off all IRQs */
1193
1194         hp100_stop_interface(dev);
1195
1196         if (lp->lan_type == HP100_LAN_100)
1197                 lp->hub_status = hp100_login_to_vg_hub(dev, FALSE);
1198
1199         netif_stop_queue(dev);
1200
1201         free_irq(dev->irq, dev);
1202
1203 #ifdef HP100_DEBUG
1204         printk("hp100: %s: close LSW = 0x%x\n", dev->name,
1205                hp100_inw(OPTION_LSW));
1206 #endif
1207
1208         return 0;
1209 }
1210 \f
1211
1212 /*
1213  * Configure the PDL Rx rings and LAN 
1214  */
1215 static void hp100_init_pdls(struct net_device *dev)
1216 {
1217         struct hp100_private *lp = (struct hp100_private *) dev->priv;
1218         hp100_ring_t *ringptr;
1219         u_int *pageptr;         /* Warning : increment by 4 - Jean II */
1220         int i;
1221
1222 #ifdef HP100_DEBUG_B
1223         int ioaddr = dev->base_addr;
1224 #endif
1225
1226 #ifdef HP100_DEBUG_B
1227         hp100_outw(0x4206, TRACE);
1228         printk("hp100: %s: init pdls\n", dev->name);
1229 #endif
1230
1231         if (0 == lp->page_vaddr_algn)
1232                 printk("hp100: %s: Warning: lp->page_vaddr_algn not initialised!\n", dev->name);
1233         else {
1234                 /* pageptr shall point into the DMA accessible memory region  */
1235                 /* we use this pointer to status the upper limit of allocated */
1236                 /* memory in the allocated page. */
1237                 /* note: align the pointers to the pci cache line size */
1238                 memset(lp->page_vaddr_algn, 0, MAX_RINGSIZE);   /* Zero  Rx/Tx ring page */
1239                 pageptr = lp->page_vaddr_algn;
1240
1241                 lp->rxrcommit = 0;
1242                 ringptr = lp->rxrhead = lp->rxrtail = &(lp->rxring[0]);
1243
1244                 /* Initialise Rx Ring */
1245                 for (i = MAX_RX_PDL - 1; i >= 0; i--) {
1246                         lp->rxring[i].next = ringptr;
1247                         ringptr = &(lp->rxring[i]);
1248                         pageptr += hp100_init_rxpdl(dev, ringptr, pageptr);
1249                 }
1250
1251                 /* Initialise Tx Ring */
1252                 lp->txrcommit = 0;
1253                 ringptr = lp->txrhead = lp->txrtail = &(lp->txring[0]);
1254                 for (i = MAX_TX_PDL - 1; i >= 0; i--) {
1255                         lp->txring[i].next = ringptr;
1256                         ringptr = &(lp->txring[i]);
1257                         pageptr += hp100_init_txpdl(dev, ringptr, pageptr);
1258                 }
1259         }
1260 }
1261 \f
1262
1263 /* These functions "format" the entries in the pdl structure   */
1264 /* They return how much memory the fragments need.            */
1265 static int hp100_init_rxpdl(struct net_device *dev,
1266                             register hp100_ring_t * ringptr,
1267                             register u32 * pdlptr)
1268 {
1269         /* pdlptr is starting address for this pdl */
1270
1271         if (0 != (((unsigned) pdlptr) & 0xf))
1272                 printk("hp100: %s: Init rxpdl: Unaligned pdlptr 0x%x.\n",
1273                        dev->name, (unsigned) pdlptr);
1274
1275         ringptr->pdl = pdlptr + 1;
1276         ringptr->pdl_paddr = virt_to_whatever(dev, pdlptr + 1);
1277         ringptr->skb = (void *) NULL;
1278
1279         /* 
1280          * Write address and length of first PDL Fragment (which is used for
1281          * storing the RX-Header
1282          * We use the 4 bytes _before_ the PDH in the pdl memory area to 
1283          * store this information. (PDH is at offset 0x04)
1284          */
1285         /* Note that pdlptr+1 and not pdlptr is the pointer to the PDH */
1286
1287         *(pdlptr + 2) = (u_int) virt_to_whatever(dev, pdlptr);  /* Address Frag 1 */
1288         *(pdlptr + 3) = 4;      /* Length  Frag 1 */
1289
1290         return ((((MAX_RX_FRAG * 2 + 2) + 3) / 4) * 4);
1291 }
1292
1293
1294 static int hp100_init_txpdl(struct net_device *dev,
1295                             register hp100_ring_t * ringptr,
1296                             register u32 * pdlptr)
1297 {
1298         if (0 != (((unsigned) pdlptr) & 0xf))
1299                 printk("hp100: %s: Init txpdl: Unaligned pdlptr 0x%x.\n", dev->name, (unsigned) pdlptr);
1300
1301         ringptr->pdl = pdlptr;  /* +1; */
1302         ringptr->pdl_paddr = virt_to_whatever(dev, pdlptr);     /* +1 */
1303         ringptr->skb = (void *) NULL;
1304
1305         return ((((MAX_TX_FRAG * 2 + 2) + 3) / 4) * 4);
1306 }
1307
1308 /*
1309  * hp100_build_rx_pdl allocates an skb_buff of maximum size plus two bytes 
1310  * for possible odd word alignment rounding up to next dword and set PDL
1311  * address for fragment#2 
1312  * Returns: 0 if unable to allocate skb_buff
1313  *          1 if successful
1314  */
1315 static int hp100_build_rx_pdl(hp100_ring_t * ringptr,
1316                               struct net_device *dev)
1317 {
1318 #ifdef HP100_DEBUG_B
1319         int ioaddr = dev->base_addr;
1320 #endif
1321 #ifdef HP100_DEBUG_BM
1322         u_int *p;
1323 #endif
1324
1325 #ifdef HP100_DEBUG_B
1326         hp100_outw(0x4207, TRACE);
1327         printk("hp100: %s: build rx pdl\n", dev->name);
1328 #endif
1329
1330         /* Allocate skb buffer of maximum size */
1331         /* Note: This depends on the alloc_skb functions allocating more 
1332          * space than requested, i.e. aligning to 16bytes */
1333
1334         ringptr->skb = dev_alloc_skb(((MAX_ETHER_SIZE + 2 + 3) / 4) * 4);
1335
1336         if (NULL != ringptr->skb) {
1337                 /* 
1338                  * Reserve 2 bytes at the head of the buffer to land the IP header
1339                  * on a long word boundary (According to the Network Driver section
1340                  * in the Linux KHG, this should help to increase performance.)
1341                  */
1342                 skb_reserve(ringptr->skb, 2);
1343
1344                 ringptr->skb->dev = dev;
1345                 ringptr->skb->data = (u_char *) skb_put(ringptr->skb, MAX_ETHER_SIZE);
1346
1347                 /* ringptr->pdl points to the beginning of the PDL, i.e. the PDH */
1348                 /* Note: 1st Fragment is used for the 4 byte packet status
1349                  * (receive header). Its PDL entries are set up by init_rxpdl. So 
1350                  * here we only have to set up the PDL fragment entries for the data
1351                  * part. Those 4 bytes will be stored in the DMA memory region 
1352                  * directly before the PDL. 
1353                  */
1354 #ifdef HP100_DEBUG_BM
1355                 printk("hp100: %s: build_rx_pdl: PDH@0x%x, skb->data (len %d) at 0x%x\n",
1356                                      dev->name, (u_int) ringptr->pdl,
1357                                      ((MAX_ETHER_SIZE + 2 + 3) / 4) * 4,
1358                                      (unsigned int) ringptr->skb->data);
1359 #endif
1360
1361                 /* Conversion to new PCI API : map skbuf data to PCI bus.
1362                  * Doc says it's OK for EISA as well - Jean II */
1363                 ringptr->pdl[0] = 0x00020000;   /* Write PDH */
1364                 ringptr->pdl[3] = ((u_int) pci_map_single(((struct hp100_private *) (dev->priv))->pci_dev, ringptr->skb->data, MAX_ETHER_SIZE, PCI_DMA_FROMDEVICE));
1365                 ringptr->pdl[4] = MAX_ETHER_SIZE;       /* Length of Data */
1366
1367 #ifdef HP100_DEBUG_BM
1368                 for (p = (ringptr->pdl); p < (ringptr->pdl + 5); p++)
1369                         printk("hp100: %s: Adr 0x%.8x = 0x%.8x\n", dev->name, (u_int) p, (u_int) * p);
1370 #endif
1371                 return (1);
1372         }
1373         /* else: */
1374         /* alloc_skb failed (no memory) -> still can receive the header
1375          * fragment into PDL memory. make PDL safe by clearing msgptr and
1376          * making the PDL only 1 fragment (i.e. the 4 byte packet status)
1377          */
1378 #ifdef HP100_DEBUG_BM
1379         printk("hp100: %s: build_rx_pdl: PDH@0x%x, No space for skb.\n", dev->name, (u_int) ringptr->pdl);
1380 #endif
1381
1382         ringptr->pdl[0] = 0x00010000;   /* PDH: Count=1 Fragment */
1383
1384         return (0);
1385 }
1386
1387 /*
1388  *  hp100_rxfill - attempt to fill the Rx Ring will empty skb's
1389  *
1390  * Makes assumption that skb's are always contiguous memory areas and
1391  * therefore PDLs contain only 2 physical fragments.
1392  * -  While the number of Rx PDLs with buffers is less than maximum
1393  *      a.  Get a maximum packet size skb
1394  *      b.  Put the physical address of the buffer into the PDL.
1395  *      c.  Output physical address of PDL to adapter.
1396  */
1397 static void hp100_rxfill(struct net_device *dev)
1398 {
1399         int ioaddr = dev->base_addr;
1400
1401         struct hp100_private *lp = (struct hp100_private *) dev->priv;
1402         hp100_ring_t *ringptr;
1403
1404 #ifdef HP100_DEBUG_B
1405         hp100_outw(0x4208, TRACE);
1406         printk("hp100: %s: rxfill\n", dev->name);
1407 #endif
1408
1409         hp100_page(PERFORMANCE);
1410
1411         while (lp->rxrcommit < MAX_RX_PDL) {
1412                 /*
1413                    ** Attempt to get a buffer and build a Rx PDL.
1414                  */
1415                 ringptr = lp->rxrtail;
1416                 if (0 == hp100_build_rx_pdl(ringptr, dev)) {
1417                         return; /* None available, return */
1418                 }
1419
1420                 /* Hand this PDL over to the card */
1421                 /* Note: This needs performance page selected! */
1422 #ifdef HP100_DEBUG_BM
1423                 printk("hp100: %s: rxfill: Hand to card: pdl #%d @0x%x phys:0x%x, buffer: 0x%x\n",
1424                                      dev->name, lp->rxrcommit, (u_int) ringptr->pdl,
1425                                      (u_int) ringptr->pdl_paddr, (u_int) ringptr->pdl[3]);
1426 #endif
1427
1428                 hp100_outl((u32) ringptr->pdl_paddr, RX_PDA);
1429
1430                 lp->rxrcommit += 1;
1431                 lp->rxrtail = ringptr->next;
1432         }
1433 }
1434
1435 /*
1436  * BM_shutdown - shutdown bus mastering and leave chip in reset state
1437  */
1438
1439 static void hp100_BM_shutdown(struct net_device *dev)
1440 {
1441         int ioaddr = dev->base_addr;
1442         struct hp100_private *lp = (struct hp100_private *) dev->priv;
1443         unsigned long time;
1444
1445 #ifdef HP100_DEBUG_B
1446         hp100_outw(0x4209, TRACE);
1447         printk("hp100: %s: bm shutdown\n", dev->name);
1448 #endif
1449
1450         hp100_page(PERFORMANCE);
1451         hp100_outw(0xfefe, IRQ_MASK);   /* mask off all ints */
1452         hp100_outw(0xffff, IRQ_STATUS); /* Ack all ints */
1453
1454         /* Ensure Interrupts are off */
1455         hp100_outw(HP100_INT_EN | HP100_RESET_LB, OPTION_LSW);
1456
1457         /* Disable all MAC activity */
1458         hp100_page(MAC_CTRL);
1459         hp100_andb(~(HP100_RX_EN | HP100_TX_EN), MAC_CFG_1);    /* stop rx/tx */
1460
1461         /* If cascade MMU is not already in reset */
1462         if (0 != (hp100_inw(OPTION_LSW) & HP100_HW_RST)) {
1463                 /* Wait 1.3ms (10Mb max packet time) to ensure MAC is idle so
1464                  * MMU pointers will not be reset out from underneath
1465                  */
1466                 hp100_page(MAC_CTRL);
1467                 for (time = 0; time < 5000; time++) {
1468                         if ((hp100_inb(MAC_CFG_1) & (HP100_TX_IDLE | HP100_RX_IDLE)) == (HP100_TX_IDLE | HP100_RX_IDLE))
1469                                 break;
1470                 }
1471
1472                 /* Shutdown algorithm depends on the generation of Cascade */
1473                 if (lp->chip == HP100_CHIPID_LASSEN) {  /* ETR shutdown/reset */
1474                         /* Disable Busmaster mode and wait for bit to go to zero. */
1475                         hp100_page(HW_MAP);
1476                         hp100_andb(~HP100_BM_MASTER, BM);
1477                         /* 100 ms timeout */
1478                         for (time = 0; time < 32000; time++) {
1479                                 if (0 == (hp100_inb(BM) & HP100_BM_MASTER))
1480                                         break;
1481                         }
1482                 } else {        /* Shasta or Rainier Shutdown/Reset */
1483                         /* To ensure all bus master inloading activity has ceased,
1484                          * wait for no Rx PDAs or no Rx packets on card. 
1485                          */
1486                         hp100_page(PERFORMANCE);
1487                         /* 100 ms timeout */
1488                         for (time = 0; time < 10000; time++) {
1489                                 /* RX_PDL: PDLs not executed. */
1490                                 /* RX_PKT_CNT: RX'd packets on card. */
1491                                 if ((hp100_inb(RX_PDL) == 0) && (hp100_inb(RX_PKT_CNT) == 0))
1492                                         break;
1493                         }
1494
1495                         if (time >= 10000)
1496                                 printk("hp100: %s: BM shutdown error.\n", dev->name);
1497
1498                         /* To ensure all bus master outloading activity has ceased,
1499                          * wait until the Tx PDA count goes to zero or no more Tx space
1500                          * available in the Tx region of the card. 
1501                          */
1502                         /* 100 ms timeout */
1503                         for (time = 0; time < 10000; time++) {
1504                                 if ((0 == hp100_inb(TX_PKT_CNT)) &&
1505                                     (0 != (hp100_inb(TX_MEM_FREE) & HP100_AUTO_COMPARE)))
1506                                         break;
1507                         }
1508
1509                         /* Disable Busmaster mode */
1510                         hp100_page(HW_MAP);
1511                         hp100_andb(~HP100_BM_MASTER, BM);
1512                 }       /* end of shutdown procedure for non-etr parts */
1513
1514                 hp100_cascade_reset(dev, TRUE);
1515         }
1516         hp100_page(PERFORMANCE);
1517         /* hp100_outw( HP100_BM_READ | HP100_BM_WRITE | HP100_RESET_HB, OPTION_LSW ); */
1518         /* Busmaster mode should be shut down now. */
1519 }
1520
1521 /* 
1522  *  transmit functions
1523  */
1524
1525 /* tx function for busmaster mode */
1526 static int hp100_start_xmit_bm(struct sk_buff *skb, struct net_device *dev)
1527 {
1528         unsigned long flags;
1529         int i, ok_flag;
1530         int ioaddr = dev->base_addr;
1531         struct hp100_private *lp = (struct hp100_private *) dev->priv;
1532         hp100_ring_t *ringptr;
1533
1534 #ifdef HP100_DEBUG_B
1535         hp100_outw(0x4210, TRACE);
1536         printk("hp100: %s: start_xmit_bm\n", dev->name);
1537 #endif
1538
1539         if (skb == NULL) {
1540                 return 0;
1541         }
1542
1543         if (skb->len <= 0)
1544                 return 0;
1545
1546         /* Get Tx ring tail pointer */
1547         if (lp->txrtail->next == lp->txrhead) {
1548                 /* No memory. */
1549 #ifdef HP100_DEBUG
1550                 printk("hp100: %s: start_xmit_bm: No TX PDL available.\n", dev->name);
1551 #endif
1552                 /* not waited long enough since last tx? */
1553                 if (jiffies - dev->trans_start < HZ)
1554                         return -EAGAIN;
1555
1556                 if (lp->lan_type < 0) { /* no LAN type detected yet? */
1557                         hp100_stop_interface(dev);
1558                         if ((lp->lan_type = hp100_sense_lan(dev)) < 0) {
1559                                 printk("hp100: %s: no connection found - check wire\n", dev->name);
1560                                 hp100_start_interface(dev);     /* 10Mb/s RX pkts maybe handled */
1561                                 return -EIO;
1562                         }
1563                         if (lp->lan_type == HP100_LAN_100)
1564                                 lp->hub_status = hp100_login_to_vg_hub(dev, FALSE);     /* relogin */
1565                         hp100_start_interface(dev);
1566                 }
1567
1568                 if (lp->lan_type == HP100_LAN_100 && lp->hub_status < 0) {
1569                         /* we have a 100Mb/s adapter but it isn't connected to hub */
1570                         printk("hp100: %s: login to 100Mb/s hub retry\n", dev->name);
1571                         hp100_stop_interface(dev);
1572                         lp->hub_status = hp100_login_to_vg_hub(dev, FALSE);
1573                         hp100_start_interface(dev);
1574                 } else {
1575                         spin_lock_irqsave(&lp->lock, flags);
1576                         hp100_ints_off();       /* Useful ? Jean II */
1577                         i = hp100_sense_lan(dev);
1578                         hp100_ints_on();
1579                         spin_unlock_irqrestore(&lp->lock, flags);
1580                         if (i == HP100_LAN_ERR)
1581                                 printk("hp100: %s: link down detected\n", dev->name);
1582                         else if (lp->lan_type != i) {   /* cable change! */
1583                                 /* it's very hard - all network setting must be changed!!! */
1584                                 printk("hp100: %s: cable change 10Mb/s <-> 100Mb/s detected\n", dev->name);
1585                                 lp->lan_type = i;
1586                                 hp100_stop_interface(dev);
1587                                 if (lp->lan_type == HP100_LAN_100)
1588                                         lp->hub_status = hp100_login_to_vg_hub(dev, FALSE);
1589                                 hp100_start_interface(dev);
1590                         } else {
1591                                 printk("hp100: %s: interface reset\n", dev->name);
1592                                 hp100_stop_interface(dev);
1593                                 if (lp->lan_type == HP100_LAN_100)
1594                                         lp->hub_status = hp100_login_to_vg_hub(dev, FALSE);
1595                                 hp100_start_interface(dev);
1596                         }
1597                 }
1598
1599                 dev->trans_start = jiffies;
1600                 return -EAGAIN;
1601         }
1602
1603         /*
1604          * we have to turn int's off before modifying this, otherwise
1605          * a tx_pdl_cleanup could occur at the same time
1606          */
1607         spin_lock_irqsave(&lp->lock, flags);
1608         ringptr = lp->txrtail;
1609         lp->txrtail = ringptr->next;
1610
1611         /* Check whether packet has minimal packet size */
1612         ok_flag = skb->len >= HP100_MIN_PACKET_SIZE;
1613         i = ok_flag ? skb->len : HP100_MIN_PACKET_SIZE;
1614
1615         ringptr->skb = skb;
1616         ringptr->pdl[0] = ((1 << 16) | i);      /* PDH: 1 Fragment & length */
1617         if (lp->chip == HP100_CHIPID_SHASTA) {
1618                 /* TODO:Could someone who has the EISA card please check if this works? */
1619                 ringptr->pdl[2] = i;
1620         } else {                /* Lassen */
1621                 /* In the PDL, don't use the padded size but the real packet size: */
1622                 ringptr->pdl[2] = skb->len;     /* 1st Frag: Length of frag */
1623         }
1624         /* Conversion to new PCI API : map skbuf data to PCI bus.
1625          * Doc says it's OK for EISA as well - Jean II */
1626         ringptr->pdl[1] = ((u32) pci_map_single(lp->pci_dev, skb->data, ringptr->pdl[2], PCI_DMA_TODEVICE));    /* 1st Frag: Adr. of data */
1627
1628         /* Hand this PDL to the card. */
1629         hp100_outl(ringptr->pdl_paddr, TX_PDA_L);       /* Low Prio. Queue */
1630
1631         lp->txrcommit++;
1632         spin_unlock_irqrestore(&lp->lock, flags);
1633
1634         /* Update statistics */
1635         lp->stats.tx_packets++;
1636         lp->stats.tx_bytes += skb->len;
1637         dev->trans_start = jiffies;
1638
1639         return 0;
1640 }
1641 \f
1642
1643 /* clean_txring checks if packets have been sent by the card by reading
1644  * the TX_PDL register from the performance page and comparing it to the
1645  * number of commited packets. It then frees the skb's of the packets that
1646  * obviously have been sent to the network.
1647  *
1648  * Needs the PERFORMANCE page selected. 
1649  */
1650 static void hp100_clean_txring(struct net_device *dev)
1651 {
1652         struct hp100_private *lp = (struct hp100_private *) dev->priv;
1653         int ioaddr = dev->base_addr;
1654         int donecount;
1655
1656 #ifdef HP100_DEBUG_B
1657         hp100_outw(0x4211, TRACE);
1658         printk("hp100: %s: clean txring\n", dev->name);
1659 #endif
1660
1661         /* How many PDLs have been transmitted? */
1662         donecount = (lp->txrcommit) - hp100_inb(TX_PDL);
1663
1664 #ifdef HP100_DEBUG
1665         if (donecount > MAX_TX_PDL)
1666                 printk("hp100: %s: Warning: More PDLs transmitted than commited to card???\n", dev->name);
1667 #endif
1668
1669         for (; 0 != donecount; donecount--) {
1670 #ifdef HP100_DEBUG_BM
1671                 printk("hp100: %s: Free skb: data @0x%.8x txrcommit=0x%x TXPDL=0x%x, done=0x%x\n",
1672                                 dev->name, (u_int) lp->txrhead->skb->data,
1673                                 lp->txrcommit, hp100_inb(TX_PDL), donecount);
1674 #endif
1675                 /* Conversion to new PCI API : NOP */
1676                 pci_unmap_single(lp->pci_dev, (dma_addr_t) lp->txrhead->pdl[1], lp->txrhead->pdl[2], PCI_DMA_TODEVICE);
1677                 dev_kfree_skb_any(lp->txrhead->skb);
1678                 lp->txrhead->skb = (void *) NULL;
1679                 lp->txrhead = lp->txrhead->next;
1680                 lp->txrcommit--;
1681         }
1682 }
1683
1684 /* tx function for slave modes */
1685 static int hp100_start_xmit(struct sk_buff *skb, struct net_device *dev)
1686 {
1687         unsigned long flags;
1688         int i, ok_flag;
1689         int ioaddr = dev->base_addr;
1690         u_short val;
1691         struct hp100_private *lp = (struct hp100_private *) dev->priv;
1692
1693 #ifdef HP100_DEBUG_B
1694         hp100_outw(0x4212, TRACE);
1695         printk("hp100: %s: start_xmit\n", dev->name);
1696 #endif
1697
1698         if (skb == NULL) {
1699                 return 0;
1700         }
1701
1702         if (skb->len <= 0)
1703                 return 0;
1704
1705         if (lp->lan_type < 0) { /* no LAN type detected yet? */
1706                 hp100_stop_interface(dev);
1707                 if ((lp->lan_type = hp100_sense_lan(dev)) < 0) {
1708                         printk("hp100: %s: no connection found - check wire\n", dev->name);
1709                         hp100_start_interface(dev);     /* 10Mb/s RX packets maybe handled */
1710                         return -EIO;
1711                 }
1712                 if (lp->lan_type == HP100_LAN_100)
1713                         lp->hub_status = hp100_login_to_vg_hub(dev, FALSE);     /* relogin */
1714                 hp100_start_interface(dev);
1715         }
1716
1717         /* If there is not enough free memory on the card... */
1718         i = hp100_inl(TX_MEM_FREE) & 0x7fffffff;
1719         if (!(((i / 2) - 539) > (skb->len + 16) && (hp100_inb(TX_PKT_CNT) < 255))) {
1720 #ifdef HP100_DEBUG
1721                 printk("hp100: %s: start_xmit: tx free mem = 0x%x\n", dev->name, i);
1722 #endif
1723                 /* not waited long enough since last failed tx try? */
1724                 if (jiffies - dev->trans_start < HZ) {
1725 #ifdef HP100_DEBUG
1726                         printk("hp100: %s: trans_start timing problem\n",
1727                                dev->name);
1728 #endif
1729                         return -EAGAIN;
1730                 }
1731                 if (lp->lan_type == HP100_LAN_100 && lp->hub_status < 0) {
1732                         /* we have a 100Mb/s adapter but it isn't connected to hub */
1733                         printk("hp100: %s: login to 100Mb/s hub retry\n", dev->name);
1734                         hp100_stop_interface(dev);
1735                         lp->hub_status = hp100_login_to_vg_hub(dev, FALSE);
1736                         hp100_start_interface(dev);
1737                 } else {
1738                         spin_lock_irqsave(&lp->lock, flags);
1739                         hp100_ints_off();       /* Useful ? Jean II */
1740                         i = hp100_sense_lan(dev);
1741                         hp100_ints_on();
1742                         spin_unlock_irqrestore(&lp->lock, flags);
1743                         if (i == HP100_LAN_ERR)
1744                                 printk("hp100: %s: link down detected\n", dev->name);
1745                         else if (lp->lan_type != i) {   /* cable change! */
1746                                 /* it's very hard - all network setting must be changed!!! */
1747                                 printk("hp100: %s: cable change 10Mb/s <-> 100Mb/s detected\n", dev->name);
1748                                 lp->lan_type = i;
1749                                 hp100_stop_interface(dev);
1750                                 if (lp->lan_type == HP100_LAN_100)
1751                                         lp->hub_status = hp100_login_to_vg_hub(dev, FALSE);
1752                                 hp100_start_interface(dev);
1753                         } else {
1754                                 printk("hp100: %s: interface reset\n", dev->name);
1755                                 hp100_stop_interface(dev);
1756                                 if (lp->lan_type == HP100_LAN_100)
1757                                         lp->hub_status = hp100_login_to_vg_hub(dev, FALSE);
1758                                 hp100_start_interface(dev);
1759                                 mdelay(1);
1760                         }
1761                 }
1762                 dev->trans_start = jiffies;
1763                 return -EAGAIN;
1764         }
1765
1766         for (i = 0; i < 6000 && (hp100_inb(OPTION_MSW) & HP100_TX_CMD); i++) {
1767 #ifdef HP100_DEBUG_TX
1768                 printk("hp100: %s: start_xmit: busy\n", dev->name);
1769 #endif
1770         }
1771
1772         spin_lock_irqsave(&lp->lock, flags);
1773         hp100_ints_off();
1774         val = hp100_inw(IRQ_STATUS);
1775         /* Ack / clear the interrupt TX_COMPLETE interrupt - this interrupt is set
1776          * when the current packet being transmitted on the wire is completed. */
1777         hp100_outw(HP100_TX_COMPLETE, IRQ_STATUS);
1778 #ifdef HP100_DEBUG_TX
1779         printk("hp100: %s: start_xmit: irq_status=0x%.4x, irqmask=0x%.4x, len=%d\n",
1780                         dev->name, val, hp100_inw(IRQ_MASK), (int) skb->len);
1781 #endif
1782
1783         ok_flag = skb->len >= HP100_MIN_PACKET_SIZE;
1784         i = ok_flag ? skb->len : HP100_MIN_PACKET_SIZE;
1785
1786         hp100_outw(i, DATA32);  /* tell card the total packet length */
1787         hp100_outw(i, FRAGMENT_LEN);    /* and first/only fragment length    */
1788
1789         if (lp->mode == 2) {    /* memory mapped */
1790                 if (lp->mem_ptr_virt) { /* high pci memory was remapped */
1791                         /* Note: The J2585B needs alignment to 32bits here!  */
1792                         memcpy_toio(lp->mem_ptr_virt, skb->data, (skb->len + 3) & ~3);
1793                         if (!ok_flag)
1794                                 memset_io(lp->mem_ptr_virt, 0, HP100_MIN_PACKET_SIZE - skb->len);
1795                 } else {
1796                         /* Note: The J2585B needs alignment to 32bits here!  */
1797                         isa_memcpy_toio(lp->mem_ptr_phys, skb->data, (skb->len + 3) & ~3);
1798                         if (!ok_flag)
1799                                 isa_memset_io(lp->mem_ptr_phys, 0, HP100_MIN_PACKET_SIZE - skb->len);
1800                 }
1801         } else {                /* programmed i/o */
1802                 outsl(ioaddr + HP100_REG_DATA32, skb->data,
1803                       (skb->len + 3) >> 2);
1804                 if (!ok_flag)
1805                         for (i = (skb->len + 3) & ~3; i < HP100_MIN_PACKET_SIZE; i += 4)
1806                                 hp100_outl(0, DATA32);
1807         }
1808
1809         hp100_outb(HP100_TX_CMD | HP100_SET_LB, OPTION_MSW);    /* send packet */
1810
1811         lp->stats.tx_packets++;
1812         lp->stats.tx_bytes += skb->len;
1813         dev->trans_start = jiffies;
1814         hp100_ints_on();
1815         spin_unlock_irqrestore(&lp->lock, flags);
1816
1817         dev_kfree_skb_any(skb);
1818
1819 #ifdef HP100_DEBUG_TX
1820         printk("hp100: %s: start_xmit: end\n", dev->name);
1821 #endif
1822
1823         return 0;
1824 }
1825 \f
1826
1827 /*
1828  * Receive Function (Non-Busmaster mode)
1829  * Called when an "Receive Packet" interrupt occurs, i.e. the receive 
1830  * packet counter is non-zero.
1831  * For non-busmaster, this function does the whole work of transfering
1832  * the packet to the host memory and then up to higher layers via skb
1833  * and netif_rx. 
1834  */
1835
1836 static void hp100_rx(struct net_device *dev)
1837 {
1838         int packets, pkt_len;
1839         int ioaddr = dev->base_addr;
1840         struct hp100_private *lp = (struct hp100_private *) dev->priv;
1841         u_int header;
1842         struct sk_buff *skb;
1843
1844 #ifdef DEBUG_B
1845         hp100_outw(0x4213, TRACE);
1846         printk("hp100: %s: rx\n", dev->name);
1847 #endif
1848
1849         /* First get indication of received lan packet */
1850         /* RX_PKT_CND indicates the number of packets which have been fully */
1851         /* received onto the card but have not been fully transferred of the card */
1852         packets = hp100_inb(RX_PKT_CNT);
1853 #ifdef HP100_DEBUG_RX
1854         if (packets > 1)
1855                 printk("hp100: %s: rx: waiting packets = %d\n", dev->name, packets);
1856 #endif
1857
1858         while (packets-- > 0) {
1859                 /* If ADV_NXT_PKT is still set, we have to wait until the card has */
1860                 /* really advanced to the next packet. */
1861                 for (pkt_len = 0; pkt_len < 6000 && (hp100_inb(OPTION_MSW) & HP100_ADV_NXT_PKT); pkt_len++) {
1862 #ifdef HP100_DEBUG_RX
1863                         printk ("hp100: %s: rx: busy, remaining packets = %d\n", dev->name, packets);
1864 #endif
1865                 }
1866
1867                 /* First we get the header, which contains information about the */
1868                 /* actual length of the received packet. */
1869                 if (lp->mode == 2) {    /* memory mapped mode */
1870                         if (lp->mem_ptr_virt)   /* if memory was remapped */
1871                                 header = readl(lp->mem_ptr_virt);
1872                         else
1873                                 header = isa_readl(lp->mem_ptr_phys);
1874                 } else          /* programmed i/o */
1875                         header = hp100_inl(DATA32);
1876
1877                 pkt_len = ((header & HP100_PKT_LEN_MASK) + 3) & ~3;
1878
1879 #ifdef HP100_DEBUG_RX
1880                 printk("hp100: %s: rx: new packet - length=%d, errors=0x%x, dest=0x%x\n",
1881                                      dev->name, header & HP100_PKT_LEN_MASK,
1882                                      (header >> 16) & 0xfff8, (header >> 16) & 7);
1883 #endif
1884
1885                 /* Now we allocate the skb and transfer the data into it. */
1886                 skb = dev_alloc_skb(pkt_len+2);
1887                 if (skb == NULL) {      /* Not enough memory->drop packet */
1888 #ifdef HP100_DEBUG
1889                         printk("hp100: %s: rx: couldn't allocate a sk_buff of size %d\n",
1890                                              dev->name, pkt_len);
1891 #endif
1892                         lp->stats.rx_dropped++;
1893                 } else {        /* skb successfully allocated */
1894
1895                         u_char *ptr;
1896
1897                         skb_reserve(skb,2);
1898                         skb->dev = dev;
1899
1900                         /* ptr to start of the sk_buff data area */
1901                         skb_put(skb, pkt_len);
1902                         ptr = skb->data;
1903
1904                         /* Now transfer the data from the card into that area */
1905                         if (lp->mode == 2) {
1906                                 if (lp->mem_ptr_virt)
1907                                         memcpy_fromio(ptr, lp->mem_ptr_virt,pkt_len);
1908                                 /* Note alignment to 32bit transfers */
1909                                 else
1910                                         isa_memcpy_fromio(ptr, lp->mem_ptr_phys, pkt_len);
1911                         } else  /* io mapped */
1912                                 insl(ioaddr + HP100_REG_DATA32, ptr, pkt_len >> 2);
1913
1914                         skb->protocol = eth_type_trans(skb, dev);
1915
1916 #ifdef HP100_DEBUG_RX
1917                         printk("hp100: %s: rx: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
1918                                         dev->name, ptr[0], ptr[1], ptr[2], ptr[3],
1919                                         ptr[4], ptr[5], ptr[6], ptr[7], ptr[8],
1920                                         ptr[9], ptr[10], ptr[11]);
1921 #endif
1922                         netif_rx(skb);
1923                         dev->last_rx = jiffies;
1924                         lp->stats.rx_packets++;
1925                         lp->stats.rx_bytes += pkt_len;
1926                 }
1927
1928                 /* Indicate the card that we have got the packet */
1929                 hp100_outb(HP100_ADV_NXT_PKT | HP100_SET_LB, OPTION_MSW);
1930
1931                 switch (header & 0x00070000) {
1932                 case (HP100_MULTI_ADDR_HASH << 16):
1933                 case (HP100_MULTI_ADDR_NO_HASH << 16):
1934                         lp->stats.multicast++;
1935                         break;
1936                 }
1937         }                       /* end of while(there are packets) loop */
1938 #ifdef HP100_DEBUG_RX
1939         printk("hp100_rx: %s: end\n", dev->name);
1940 #endif
1941 }
1942
1943 /* 
1944  * Receive Function for Busmaster Mode
1945  */
1946 static void hp100_rx_bm(struct net_device *dev)
1947 {
1948         int ioaddr = dev->base_addr;
1949         struct hp100_private *lp = (struct hp100_private *) dev->priv;
1950         hp100_ring_t *ptr;
1951         u_int header;
1952         int pkt_len;
1953
1954 #ifdef HP100_DEBUG_B
1955         hp100_outw(0x4214, TRACE);
1956         printk("hp100: %s: rx_bm\n", dev->name);
1957 #endif
1958
1959 #ifdef HP100_DEBUG
1960         if (0 == lp->rxrcommit) {
1961                 printk("hp100: %s: rx_bm called although no PDLs were committed to adapter?\n", dev->name);
1962                 return;
1963         } else
1964                 /* RX_PKT_CNT states how many PDLs are currently formatted and available to 
1965                  * the cards BM engine */
1966         if ((hp100_inw(RX_PKT_CNT) & 0x00ff) >= lp->rxrcommit) {
1967                 printk("hp100: %s: More packets received than commited? RX_PKT_CNT=0x%x, commit=0x%x\n",
1968                                      dev->name, hp100_inw(RX_PKT_CNT) & 0x00ff,
1969                                      lp->rxrcommit);
1970                 return;
1971         }
1972 #endif
1973
1974         while ((lp->rxrcommit > hp100_inb(RX_PDL))) {
1975                 /*
1976                  * The packet was received into the pdl pointed to by lp->rxrhead (
1977                  * the oldest pdl in the ring 
1978                  */
1979
1980                 /* First we get the header, which contains information about the */
1981                 /* actual length of the received packet. */
1982
1983                 ptr = lp->rxrhead;
1984
1985                 header = *(ptr->pdl - 1);
1986                 pkt_len = (header & HP100_PKT_LEN_MASK);
1987
1988                 /* Conversion to new PCI API : NOP */
1989                 pci_unmap_single(lp->pci_dev, (dma_addr_t) ptr->pdl[3], MAX_ETHER_SIZE, PCI_DMA_FROMDEVICE);
1990
1991 #ifdef HP100_DEBUG_BM
1992                 printk("hp100: %s: rx_bm: header@0x%x=0x%x length=%d, errors=0x%x, dest=0x%x\n",
1993                                 dev->name, (u_int) (ptr->pdl - 1), (u_int) header,
1994                                 pkt_len, (header >> 16) & 0xfff8, (header >> 16) & 7);
1995                 printk("hp100: %s: RX_PDL_COUNT:0x%x TX_PDL_COUNT:0x%x, RX_PKT_CNT=0x%x PDH=0x%x, Data@0x%x len=0x%x\n",
1996                                 dev->name, hp100_inb(RX_PDL), hp100_inb(TX_PDL),
1997                                 hp100_inb(RX_PKT_CNT), (u_int) * (ptr->pdl),
1998                                 (u_int) * (ptr->pdl + 3), (u_int) * (ptr->pdl + 4));
1999 #endif
2000
2001                 if ((pkt_len >= MIN_ETHER_SIZE) &&
2002                     (pkt_len <= MAX_ETHER_SIZE)) {
2003                         if (ptr->skb == NULL) {
2004                                 printk("hp100: %s: rx_bm: skb null\n", dev->name);
2005                                 /* can happen if we only allocated room for the pdh due to memory shortage. */
2006                                 lp->stats.rx_dropped++;
2007                         } else {
2008                                 skb_trim(ptr->skb, pkt_len);    /* Shorten it */
2009                                 ptr->skb->protocol =
2010                                     eth_type_trans(ptr->skb, dev);
2011
2012                                 netif_rx(ptr->skb);     /* Up and away... */
2013
2014                                 dev->last_rx = jiffies;
2015                                 lp->stats.rx_packets++;
2016                                 lp->stats.rx_bytes += pkt_len;
2017                         }
2018
2019                         switch (header & 0x00070000) {
2020                         case (HP100_MULTI_ADDR_HASH << 16):
2021                         case (HP100_MULTI_ADDR_NO_HASH << 16):
2022                                 lp->stats.multicast++;
2023                                 break;
2024                         }
2025                 } else {
2026 #ifdef HP100_DEBUG
2027                         printk("hp100: %s: rx_bm: Received bad packet (length=%d)\n", dev->name, pkt_len);
2028 #endif
2029                         if (ptr->skb != NULL)
2030                                 dev_kfree_skb_any(ptr->skb);
2031                         lp->stats.rx_errors++;
2032                 }
2033
2034                 lp->rxrhead = lp->rxrhead->next;
2035
2036                 /* Allocate a new rx PDL (so lp->rxrcommit stays the same) */
2037                 if (0 == hp100_build_rx_pdl(lp->rxrtail, dev)) {
2038                         /* No space for skb, header can still be received. */
2039 #ifdef HP100_DEBUG
2040                         printk("hp100: %s: rx_bm: No space for new PDL.\n", dev->name);
2041 #endif
2042                         return;
2043                 } else {        /* successfully allocated new PDL - put it in ringlist at tail. */
2044                         hp100_outl((u32) lp->rxrtail->pdl_paddr, RX_PDA);
2045                         lp->rxrtail = lp->rxrtail->next;
2046                 }
2047
2048         }
2049 }
2050
2051 /*
2052  *  statistics
2053  */
2054 static hp100_stats_t *hp100_get_stats(struct net_device *dev)
2055 {
2056         unsigned long flags;
2057         int ioaddr = dev->base_addr;
2058         struct hp100_private *lp = (struct hp100_private *) dev->priv;
2059
2060 #ifdef HP100_DEBUG_B
2061         hp100_outw(0x4215, TRACE);
2062 #endif
2063
2064         spin_lock_irqsave(&lp->lock, flags);
2065         hp100_ints_off();       /* Useful ? Jean II */
2066         hp100_update_stats(dev);
2067         hp100_ints_on();
2068         spin_unlock_irqrestore(&lp->lock, flags);
2069         return &(lp->stats);
2070 }
2071
2072 static void hp100_update_stats(struct net_device *dev)
2073 {
2074         int ioaddr = dev->base_addr;
2075         u_short val;
2076         struct hp100_private *lp = (struct hp100_private *) dev->priv;
2077
2078 #ifdef HP100_DEBUG_B
2079         hp100_outw(0x4216, TRACE);
2080         printk("hp100: %s: update-stats\n", dev->name);
2081 #endif
2082
2083         /* Note: Statistics counters clear when read. */
2084         hp100_page(MAC_CTRL);
2085         val = hp100_inw(DROPPED) & 0x0fff;
2086         lp->stats.rx_errors += val;
2087         lp->stats.rx_over_errors += val;
2088         val = hp100_inb(CRC);
2089         lp->stats.rx_errors += val;
2090         lp->stats.rx_crc_errors += val;
2091         val = hp100_inb(ABORT);
2092         lp->stats.tx_errors += val;
2093         lp->stats.tx_aborted_errors += val;
2094         hp100_page(PERFORMANCE);
2095 }
2096
2097 static void hp100_misc_interrupt(struct net_device *dev)
2098 {
2099         struct hp100_private *lp = (struct hp100_private *) dev->priv;
2100
2101 #ifdef HP100_DEBUG_B
2102         hp100_outw(0x4216, TRACE);
2103         printk("hp100: %s: misc_interrupt\n", dev->name);
2104 #endif
2105
2106         /* Note: Statistics counters clear when read. */
2107         lp->stats.rx_errors++;
2108         lp->stats.tx_errors++;
2109 }
2110
2111 static void hp100_clear_stats(struct hp100_private *lp, int ioaddr)
2112 {
2113         unsigned long flags;
2114
2115 #ifdef HP100_DEBUG_B
2116         hp100_outw(0x4217, TRACE);
2117         printk("hp100: %s: clear_stats\n", dev->name);
2118 #endif
2119
2120         spin_lock_irqsave(&lp->lock, flags);
2121         hp100_page(MAC_CTRL);   /* get all statistics bytes */
2122         hp100_inw(DROPPED);
2123         hp100_inb(CRC);
2124         hp100_inb(ABORT);
2125         hp100_page(PERFORMANCE);
2126         spin_unlock_irqrestore(&lp->lock, flags);
2127 }
2128 \f
2129
2130 /*
2131  *  multicast setup
2132  */
2133
2134 /*
2135  *  Set or clear the multicast filter for this adapter.
2136  */
2137
2138 static void hp100_set_multicast_list(struct net_device *dev)
2139 {
2140         unsigned long flags;
2141         int ioaddr = dev->base_addr;
2142         struct hp100_private *lp = (struct hp100_private *) dev->priv;
2143
2144 #ifdef HP100_DEBUG_B
2145         hp100_outw(0x4218, TRACE);
2146         printk("hp100: %s: set_mc_list\n", dev->name);
2147 #endif
2148
2149         spin_lock_irqsave(&lp->lock, flags);
2150         hp100_ints_off();
2151         hp100_page(MAC_CTRL);
2152         hp100_andb(~(HP100_RX_EN | HP100_TX_EN), MAC_CFG_1);    /* stop rx/tx */
2153
2154         if (dev->flags & IFF_PROMISC) {
2155                 lp->mac2_mode = HP100_MAC2MODE6;        /* promiscuous mode = get all good */
2156                 lp->mac1_mode = HP100_MAC1MODE6;        /* packets on the net */
2157                 memset(&lp->hash_bytes, 0xff, 8);
2158         } else if (dev->mc_count || (dev->flags & IFF_ALLMULTI)) {
2159                 lp->mac2_mode = HP100_MAC2MODE5;        /* multicast mode = get packets for */
2160                 lp->mac1_mode = HP100_MAC1MODE5;        /* me, broadcasts and all multicasts */
2161 #ifdef HP100_MULTICAST_FILTER   /* doesn't work!!! */
2162                 if (dev->flags & IFF_ALLMULTI) {
2163                         /* set hash filter to receive all multicast packets */
2164                         memset(&lp->hash_bytes, 0xff, 8);
2165                 } else {
2166                         int i, j, idx;
2167                         u_char *addrs;
2168                         struct dev_mc_list *dmi;
2169
2170                         memset(&lp->hash_bytes, 0x00, 8);
2171 #ifdef HP100_DEBUG
2172                         printk("hp100: %s: computing hash filter - mc_count = %i\n", dev->name, dev->mc_count);
2173 #endif
2174                         for (i = 0, dmi = dev->mc_list; i < dev->mc_count; i++, dmi = dmi->next) {
2175                                 addrs = dmi->dmi_addr;
2176                                 if ((*addrs & 0x01) == 0x01) {  /* multicast address? */
2177 #ifdef HP100_DEBUG
2178                                         printk("hp100: %s: multicast = %02x:%02x:%02x:%02x:%02x:%02x, ",
2179                                                      dev->name, addrs[0], addrs[1], addrs[2],
2180                                                      addrs[3], addrs[4], addrs[5]);
2181 #endif
2182                                         for (j = idx = 0; j < 6; j++) {
2183                                                 idx ^= *addrs++ & 0x3f;
2184                                                 printk(":%02x:", idx);
2185                                         }
2186 #ifdef HP100_DEBUG
2187                                         printk("idx = %i\n", idx);
2188 #endif
2189                                         lp->hash_bytes[idx >> 3] |= (1 << (idx & 7));
2190                                 }
2191                         }
2192                 }
2193 #else
2194                 memset(&lp->hash_bytes, 0xff, 8);
2195 #endif
2196         } else {
2197                 lp->mac2_mode = HP100_MAC2MODE3;        /* normal mode = get packets for me */
2198                 lp->mac1_mode = HP100_MAC1MODE3;        /* and broadcasts */
2199                 memset(&lp->hash_bytes, 0x00, 8);
2200         }
2201
2202         if (((hp100_inb(MAC_CFG_1) & 0x0f) != lp->mac1_mode) ||
2203             (hp100_inb(MAC_CFG_2) != lp->mac2_mode)) {
2204                 int i;
2205
2206                 hp100_outb(lp->mac2_mode, MAC_CFG_2);
2207                 hp100_andb(HP100_MAC1MODEMASK, MAC_CFG_1);      /* clear mac1 mode bits */
2208                 hp100_orb(lp->mac1_mode, MAC_CFG_1);    /* and set the new mode */
2209
2210                 hp100_page(MAC_ADDRESS);
2211                 for (i = 0; i < 8; i++)
2212                         hp100_outb(lp->hash_bytes[i], HASH_BYTE0 + i);
2213 #ifdef HP100_DEBUG
2214                 printk("hp100: %s: mac1 = 0x%x, mac2 = 0x%x, multicast hash = %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
2215                                      dev->name, lp->mac1_mode, lp->mac2_mode,
2216                                      lp->hash_bytes[0], lp->hash_bytes[1],
2217                                      lp->hash_bytes[2], lp->hash_bytes[3],
2218                                      lp->hash_bytes[4], lp->hash_bytes[5],
2219                                      lp->hash_bytes[6], lp->hash_bytes[7]);
2220 #endif
2221
2222                 if (lp->lan_type == HP100_LAN_100) {
2223 #ifdef HP100_DEBUG
2224                         printk("hp100: %s: 100VG MAC settings have changed - relogin.\n", dev->name);
2225 #endif
2226                         lp->hub_status = hp100_login_to_vg_hub(dev, TRUE);      /* force a relogin to the hub */
2227                 }
2228         } else {
2229                 int i;
2230                 u_char old_hash_bytes[8];
2231
2232                 hp100_page(MAC_ADDRESS);
2233                 for (i = 0; i < 8; i++)
2234                         old_hash_bytes[i] = hp100_inb(HASH_BYTE0 + i);
2235                 if (memcmp(old_hash_bytes, &lp->hash_bytes, 8)) {
2236                         for (i = 0; i < 8; i++)
2237                                 hp100_outb(lp->hash_bytes[i], HASH_BYTE0 + i);
2238 #ifdef HP100_DEBUG
2239                         printk("hp100: %s: multicast hash = %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
2240                                         dev->name, lp->hash_bytes[0],
2241                                         lp->hash_bytes[1], lp->hash_bytes[2],
2242                                         lp->hash_bytes[3], lp->hash_bytes[4],
2243                                         lp->hash_bytes[5], lp->hash_bytes[6],
2244                                         lp->hash_bytes[7]);
2245 #endif
2246
2247                         if (lp->lan_type == HP100_LAN_100) {
2248 #ifdef HP100_DEBUG
2249                                 printk("hp100: %s: 100VG MAC settings have changed - relogin.\n", dev->name);
2250 #endif
2251                                 lp->hub_status = hp100_login_to_vg_hub(dev, TRUE);      /* force a relogin to the hub */
2252                         }
2253                 }
2254         }
2255
2256         hp100_page(MAC_CTRL);
2257         hp100_orb(HP100_RX_EN | HP100_RX_IDLE | /* enable rx */
2258                   HP100_TX_EN | HP100_TX_IDLE, MAC_CFG_1);      /* enable tx */
2259
2260         hp100_page(PERFORMANCE);
2261         hp100_ints_on();
2262         spin_unlock_irqrestore(&lp->lock, flags);
2263 }
2264
2265 /*
2266  *  hardware interrupt handling
2267  */
2268
2269 static void hp100_interrupt(int irq, void *dev_id, struct pt_regs *regs)
2270 {
2271         struct net_device *dev = (struct net_device *) dev_id;
2272         struct hp100_private *lp = (struct hp100_private *) dev->priv;
2273
2274         int ioaddr;
2275         u_int val;
2276
2277         if (dev == NULL)
2278                 return;
2279         ioaddr = dev->base_addr;
2280
2281         spin_lock(&lp->lock);
2282
2283         hp100_ints_off();
2284
2285 #ifdef HP100_DEBUG_B
2286         hp100_outw(0x4219, TRACE);
2287 #endif
2288
2289         /*  hp100_page( PERFORMANCE ); */
2290         val = hp100_inw(IRQ_STATUS);
2291 #ifdef HP100_DEBUG_IRQ
2292         printk("hp100: %s: mode=%x,IRQ_STAT=0x%.4x,RXPKTCNT=0x%.2x RXPDL=0x%.2x TXPKTCNT=0x%.2x TXPDL=0x%.2x\n",
2293                              dev->name, lp->mode, (u_int) val, hp100_inb(RX_PKT_CNT),
2294                              hp100_inb(RX_PDL), hp100_inb(TX_PKT_CNT), hp100_inb(TX_PDL));
2295 #endif
2296
2297         if (val == 0) {         /* might be a shared interrupt */
2298                 spin_unlock(&lp->lock);
2299                 hp100_ints_on();
2300                 return;
2301         }
2302         /* We're only interested in those interrupts we really enabled. */
2303         /* val &= hp100_inw( IRQ_MASK ); */
2304
2305         /* 
2306          * RX_PDL_FILL_COMPL is set whenever a RX_PDL has been executed. A RX_PDL 
2307          * is considered executed whenever the RX_PDL data structure is no longer 
2308          * needed.
2309          */
2310         if (val & HP100_RX_PDL_FILL_COMPL) {
2311                 if (lp->mode == 1)
2312                         hp100_rx_bm(dev);
2313                 else {
2314                         printk("hp100: %s: rx_pdl_fill_compl interrupt although not busmaster?\n", dev->name);
2315                 }
2316         }
2317
2318         /* 
2319          * The RX_PACKET interrupt is set, when the receive packet counter is
2320          * non zero. We use this interrupt for receiving in slave mode. In
2321          * busmaster mode, we use it to make sure we did not miss any rx_pdl_fill
2322          * interrupts. If rx_pdl_fill_compl is not set and rx_packet is set, then
2323          * we somehow have missed a rx_pdl_fill_compl interrupt.
2324          */
2325
2326         if (val & HP100_RX_PACKET) {    /* Receive Packet Counter is non zero */
2327                 if (lp->mode != 1)      /* non busmaster */
2328                         hp100_rx(dev);
2329                 else if (!(val & HP100_RX_PDL_FILL_COMPL)) {
2330                         /* Shouldnt happen - maybe we missed a RX_PDL_FILL Interrupt?  */
2331                         hp100_rx_bm(dev);
2332                 }
2333         }
2334
2335         /*
2336          * Ack. that we have noticed the interrupt and thereby allow next one.
2337          * Note that this is now done after the slave rx function, since first
2338          * acknowledging and then setting ADV_NXT_PKT caused an extra interrupt
2339          * on the J2573.
2340          */
2341         hp100_outw(val, IRQ_STATUS);
2342
2343         /*
2344          * RX_ERROR is set when a packet is dropped due to no memory resources on 
2345          * the card or when a RCV_ERR occurs. 
2346          * TX_ERROR is set when a TX_ABORT condition occurs in the MAC->exists  
2347          * only in the 802.3 MAC and happens when 16 collisions occur during a TX 
2348          */
2349         if (val & (HP100_TX_ERROR | HP100_RX_ERROR)) {
2350 #ifdef HP100_DEBUG_IRQ
2351                 printk("hp100: %s: TX/RX Error IRQ\n", dev->name);
2352 #endif
2353                 hp100_update_stats(dev);
2354                 if (lp->mode == 1) {
2355                         hp100_rxfill(dev);
2356                         hp100_clean_txring(dev);
2357                 }
2358         }
2359
2360         /* 
2361          * RX_PDA_ZERO is set when the PDA count goes from non-zero to zero. 
2362          */
2363         if ((lp->mode == 1) && (val & (HP100_RX_PDA_ZERO)))
2364                 hp100_rxfill(dev);
2365
2366         /* 
2367          * HP100_TX_COMPLETE interrupt occurs when packet transmitted on wire 
2368          * is completed 
2369          */
2370         if ((lp->mode == 1) && (val & (HP100_TX_COMPLETE)))
2371                 hp100_clean_txring(dev);
2372
2373         /* 
2374          * MISC_ERROR is set when either the LAN link goes down or a detected
2375          * bus error occurs.
2376          */
2377         if (val & HP100_MISC_ERROR) {   /* New for J2585B */
2378 #ifdef HP100_DEBUG_IRQ
2379                 printk
2380                     ("hp100: %s: Misc. Error Interrupt - Check cabling.\n",
2381                      dev->name);
2382 #endif
2383                 if (lp->mode == 1) {
2384                         hp100_clean_txring(dev);
2385                         hp100_rxfill(dev);
2386                 }
2387                 hp100_misc_interrupt(dev);
2388         }
2389
2390         spin_unlock(&lp->lock);
2391         hp100_ints_on();
2392 }
2393
2394 /*
2395  *  some misc functions
2396  */
2397
2398 static void hp100_start_interface(struct net_device *dev)
2399 {
2400         unsigned long flags;
2401         int ioaddr = dev->base_addr;
2402         struct hp100_private *lp = (struct hp100_private *) dev->priv;
2403
2404 #ifdef HP100_DEBUG_B
2405         hp100_outw(0x4220, TRACE);
2406         printk("hp100: %s: hp100_start_interface\n", dev->name);
2407 #endif
2408
2409         spin_lock_irqsave(&lp->lock, flags);
2410
2411         /* Ensure the adapter does not want to request an interrupt when */
2412         /* enabling the IRQ line to be active on the bus (i.e. not tri-stated) */
2413         hp100_page(PERFORMANCE);
2414         hp100_outw(0xfefe, IRQ_MASK);   /* mask off all ints */
2415         hp100_outw(0xffff, IRQ_STATUS); /* ack all IRQs */
2416         hp100_outw(HP100_FAKE_INT | HP100_INT_EN | HP100_RESET_LB,
2417                    OPTION_LSW);
2418         /* Un Tri-state int. TODO: Check if shared interrupts can be realised? */
2419         hp100_outw(HP100_TRI_INT | HP100_RESET_HB, OPTION_LSW);
2420
2421         if (lp->mode == 1) {
2422                 /* Make sure BM bit is set... */
2423                 hp100_page(HW_MAP);
2424                 hp100_orb(HP100_BM_MASTER, BM);
2425                 hp100_rxfill(dev);
2426         } else if (lp->mode == 2) {
2427                 /* Enable memory mapping. Note: Don't do this when busmaster. */
2428                 hp100_outw(HP100_MMAP_DIS | HP100_RESET_HB, OPTION_LSW);
2429         }
2430
2431         hp100_page(PERFORMANCE);
2432         hp100_outw(0xfefe, IRQ_MASK);   /* mask off all ints */
2433         hp100_outw(0xffff, IRQ_STATUS); /* ack IRQ */
2434
2435         /* enable a few interrupts: */
2436         if (lp->mode == 1) {    /* busmaster mode */
2437                 hp100_outw(HP100_RX_PDL_FILL_COMPL |
2438                            HP100_RX_PDA_ZERO | HP100_RX_ERROR |
2439                            /* HP100_RX_PACKET    | */
2440                            /* HP100_RX_EARLY_INT |  */ HP100_SET_HB |
2441                            /* HP100_TX_PDA_ZERO  |  */
2442                            HP100_TX_COMPLETE |
2443                            /* HP100_MISC_ERROR   |  */
2444                            HP100_TX_ERROR | HP100_SET_LB, IRQ_MASK);
2445         } else {
2446                 hp100_outw(HP100_RX_PACKET |
2447                            HP100_RX_ERROR | HP100_SET_HB |
2448                            HP100_TX_ERROR | HP100_SET_LB, IRQ_MASK);
2449         }
2450
2451         /* Note : before hp100_set_multicast_list(), because it will play with
2452          * spinlock itself... Jean II */
2453         spin_unlock_irqrestore(&lp->lock, flags);
2454
2455         /* Enable MAC Tx and RX, set MAC modes, ... */
2456         hp100_set_multicast_list(dev);
2457 }
2458
2459 static void hp100_stop_interface(struct net_device *dev)
2460 {
2461         struct hp100_private *lp = (struct hp100_private *) dev->priv;
2462         int ioaddr = dev->base_addr;
2463         u_int val;
2464
2465 #ifdef HP100_DEBUG_B
2466         printk("hp100: %s: hp100_stop_interface\n", dev->name);
2467         hp100_outw(0x4221, TRACE);
2468 #endif
2469
2470         if (lp->mode == 1)
2471                 hp100_BM_shutdown(dev);
2472         else {
2473                 /* Note: MMAP_DIS will be reenabled by start_interface */
2474                 hp100_outw(HP100_INT_EN | HP100_RESET_LB |
2475                            HP100_TRI_INT | HP100_MMAP_DIS | HP100_SET_HB,
2476                            OPTION_LSW);
2477                 val = hp100_inw(OPTION_LSW);
2478
2479                 hp100_page(MAC_CTRL);
2480                 hp100_andb(~(HP100_RX_EN | HP100_TX_EN), MAC_CFG_1);
2481
2482                 if (!(val & HP100_HW_RST))
2483                         return; /* If reset, imm. return ... */
2484                 /* ... else: busy wait until idle */
2485                 for (val = 0; val < 6000; val++)
2486                         if ((hp100_inb(MAC_CFG_1) & (HP100_TX_IDLE | HP100_RX_IDLE)) == (HP100_TX_IDLE | HP100_RX_IDLE)) {
2487                                 hp100_page(PERFORMANCE);
2488                                 return;
2489                         }
2490                 printk("hp100: %s: hp100_stop_interface - timeout\n", dev->name);
2491                 hp100_page(PERFORMANCE);
2492         }
2493 }
2494
2495 static void hp100_load_eeprom(struct net_device *dev, u_short probe_ioaddr)
2496 {
2497         int i;
2498         int ioaddr = probe_ioaddr > 0 ? probe_ioaddr : dev->base_addr;
2499
2500 #ifdef HP100_DEBUG_B
2501         hp100_outw(0x4222, TRACE);
2502 #endif
2503
2504         hp100_page(EEPROM_CTRL);
2505         hp100_andw(~HP100_EEPROM_LOAD, EEPROM_CTRL);
2506         hp100_orw(HP100_EEPROM_LOAD, EEPROM_CTRL);
2507         for (i = 0; i < 10000; i++)
2508                 if (!(hp100_inb(OPTION_MSW) & HP100_EE_LOAD))
2509                         return;
2510         printk("hp100: %s: hp100_load_eeprom - timeout\n", dev->name);
2511 }
2512
2513 /*  Sense connection status.
2514  *  return values: LAN_10  - Connected to 10Mbit/s network
2515  *                 LAN_100 - Connected to 100Mbit/s network
2516  *                 LAN_ERR - not connected or 100Mbit/s Hub down
2517  */
2518 static int hp100_sense_lan(struct net_device *dev)
2519 {
2520         int ioaddr = dev->base_addr;
2521         u_short val_VG, val_10;
2522         struct hp100_private *lp = (struct hp100_private *) dev->priv;
2523
2524 #ifdef HP100_DEBUG_B
2525         hp100_outw(0x4223, TRACE);
2526 #endif
2527
2528         hp100_page(MAC_CTRL);
2529         val_10 = hp100_inb(10_LAN_CFG_1);
2530         val_VG = hp100_inb(VG_LAN_CFG_1);
2531         hp100_page(PERFORMANCE);
2532 #ifdef HP100_DEBUG
2533         printk("hp100: %s: sense_lan: val_VG = 0x%04x, val_10 = 0x%04x\n",
2534                dev->name, val_VG, val_10);
2535 #endif
2536
2537         if (val_10 & HP100_LINK_BEAT_ST)        /* 10Mb connection is active */
2538                 return HP100_LAN_10;
2539
2540         if (val_10 & HP100_AUI_ST) {    /* have we BNC or AUI onboard? */
2541                 val_10 |= HP100_AUI_SEL | HP100_LOW_TH;
2542                 hp100_page(MAC_CTRL);
2543                 hp100_outb(val_10, 10_LAN_CFG_1);
2544                 hp100_page(PERFORMANCE);
2545                 return HP100_LAN_10;
2546         }
2547
2548         if ((lp->id->id == 0x02019F022) ||
2549             (lp->id->id == 0x01042103c) || (lp->id->id == 0x01040103c))
2550                 return HP100_LAN_ERR;   /* Those cards don't have a 100 Mbit connector */
2551
2552         if (val_VG & HP100_LINK_CABLE_ST)       /* Can hear the HUBs tone. */
2553                 return HP100_LAN_100;
2554         return HP100_LAN_ERR;
2555 }
2556
2557 static int hp100_down_vg_link(struct net_device *dev)
2558 {
2559         struct hp100_private *lp = (struct hp100_private *) dev->priv;
2560         int ioaddr = dev->base_addr;
2561         unsigned long time;
2562         long savelan, newlan;
2563
2564 #ifdef HP100_DEBUG_B
2565         hp100_outw(0x4224, TRACE);
2566         printk("hp100: %s: down_vg_link\n", dev->name);
2567 #endif
2568
2569         hp100_page(MAC_CTRL);
2570         time = jiffies + (HZ / 4);
2571         do {
2572                 if (hp100_inb(VG_LAN_CFG_1) & HP100_LINK_CABLE_ST)
2573                         break;
2574                 if (!in_interrupt()) {
2575                         set_current_state(TASK_INTERRUPTIBLE);
2576                         schedule_timeout(1);
2577                 }
2578         } while (time_after(time, jiffies));
2579
2580         if (time_after_eq(jiffies, time))       /* no signal->no logout */
2581                 return 0;
2582
2583         /* Drop the VG Link by clearing the link up cmd and load addr. */
2584
2585         hp100_andb(~(HP100_LOAD_ADDR | HP100_LINK_CMD), VG_LAN_CFG_1);
2586         hp100_orb(HP100_VG_SEL, VG_LAN_CFG_1);
2587
2588         /* Conditionally stall for >250ms on Link-Up Status (to go down) */
2589         time = jiffies + (HZ / 2);
2590         do {
2591                 if (!(hp100_inb(VG_LAN_CFG_1) & HP100_LINK_UP_ST))
2592                         break;
2593                 if (!in_interrupt()) {
2594                         set_current_state(TASK_INTERRUPTIBLE);
2595                         schedule_timeout(1);
2596                 }
2597         } while (time_after(time, jiffies));
2598
2599 #ifdef HP100_DEBUG
2600         if (time_after_eq(jiffies, time))
2601                 printk("hp100: %s: down_vg_link: Link does not go down?\n", dev->name);
2602 #endif
2603
2604         /* To prevent condition where Rev 1 VG MAC and old hubs do not complete */
2605         /* logout under traffic (even though all the status bits are cleared),  */
2606         /* do this workaround to get the Rev 1 MAC in its idle state */
2607         if (lp->chip == HP100_CHIPID_LASSEN) {
2608                 /* Reset VG MAC to insure it leaves the logoff state even if */
2609                 /* the Hub is still emitting tones */
2610                 hp100_andb(~HP100_VG_RESET, VG_LAN_CFG_1);
2611                 udelay(1500);   /* wait for >1ms */
2612                 hp100_orb(HP100_VG_RESET, VG_LAN_CFG_1);        /* Release Reset */
2613                 udelay(1500);
2614         }
2615
2616         /* New: For lassen, switch to 10 Mbps mac briefly to clear training ACK */
2617         /* to get the VG mac to full reset. This is not req.d with later chips */
2618         /* Note: It will take the between 1 and 2 seconds for the VG mac to be */
2619         /* selected again! This will be left to the connect hub function to */
2620         /* perform if desired.  */
2621         if (lp->chip == HP100_CHIPID_LASSEN) {
2622                 /* Have to write to 10 and 100VG control registers simultaneously */
2623                 savelan = newlan = hp100_inl(10_LAN_CFG_1);     /* read 10+100 LAN_CFG regs */
2624                 newlan &= ~(HP100_VG_SEL << 16);
2625                 newlan |= (HP100_DOT3_MAC) << 8;
2626                 hp100_andb(~HP100_AUTO_MODE, MAC_CFG_3);        /* Autosel off */
2627                 hp100_outl(newlan, 10_LAN_CFG_1);
2628
2629                 /* Conditionally stall for 5sec on VG selected. */
2630                 time = jiffies + (HZ * 5);
2631                 do {
2632                         if (!(hp100_inb(MAC_CFG_4) & HP100_MAC_SEL_ST))
2633                                 break;
2634                         if (!in_interrupt()) {
2635                                 set_current_state(TASK_INTERRUPTIBLE);
2636                                 schedule_timeout(1);
2637                         }
2638                 } while (time_after(time, jiffies));
2639
2640                 hp100_orb(HP100_AUTO_MODE, MAC_CFG_3);  /* Autosel back on */
2641                 hp100_outl(savelan, 10_LAN_CFG_1);
2642         }
2643
2644         time = jiffies + (3 * HZ);      /* Timeout 3s */
2645         do {
2646                 if ((hp100_inb(VG_LAN_CFG_1) & HP100_LINK_CABLE_ST) == 0)
2647                         break;
2648                 if (!in_interrupt()) {
2649                         set_current_state(TASK_INTERRUPTIBLE);
2650                         schedule_timeout(1);
2651                 }
2652         } while (time_after(time, jiffies));
2653
2654         if (time_before_eq(time, jiffies)) {
2655 #ifdef HP100_DEBUG
2656                 printk("hp100: %s: down_vg_link: timeout\n", dev->name);
2657 #endif
2658                 return -EIO;
2659         }
2660
2661         time = jiffies + (2 * HZ);      /* This seems to take a while.... */
2662         do {
2663                 if (!in_interrupt()) {
2664                         set_current_state(TASK_INTERRUPTIBLE);
2665                         schedule_timeout(1);
2666                 }
2667         } while (time_after(time, jiffies));
2668
2669         return 0;
2670 }
2671
2672 static int hp100_login_to_vg_hub(struct net_device *dev, u_short force_relogin)
2673 {
2674         int ioaddr = dev->base_addr;
2675         struct hp100_private *lp = (struct hp100_private *) dev->priv;
2676         u_short val = 0;
2677         unsigned long time;
2678         int startst;
2679
2680 #ifdef HP100_DEBUG_B
2681         hp100_outw(0x4225, TRACE);
2682         printk("hp100: %s: login_to_vg_hub\n", dev->name);
2683 #endif
2684
2685         /* Initiate a login sequence iff VG MAC is enabled and either Load Address
2686          * bit is zero or the force relogin flag is set (e.g. due to MAC address or
2687          * promiscuous mode change)
2688          */
2689         hp100_page(MAC_CTRL);
2690         startst = hp100_inb(VG_LAN_CFG_1);
2691         if ((force_relogin == TRUE) || (hp100_inb(MAC_CFG_4) & HP100_MAC_SEL_ST)) {
2692 #ifdef HP100_DEBUG_TRAINING
2693                 printk("hp100: %s: Start training\n", dev->name);
2694 #endif
2695
2696                 /* Ensure VG Reset bit is 1 (i.e., do not reset) */
2697                 hp100_orb(HP100_VG_RESET, VG_LAN_CFG_1);
2698
2699                 /* If Lassen AND auto-select-mode AND VG tones were sensed on */
2700                 /* entry then temporarily put them into force 100Mbit mode */
2701                 if ((lp->chip == HP100_CHIPID_LASSEN) && (startst & HP100_LINK_CABLE_ST))
2702                         hp100_andb(~HP100_DOT3_MAC, 10_LAN_CFG_2);
2703
2704                 /* Drop the VG link by zeroing Link Up Command and Load Address  */
2705                 hp100_andb(~(HP100_LINK_CMD /* |HP100_LOAD_ADDR */ ), VG_LAN_CFG_1);
2706
2707 #ifdef HP100_DEBUG_TRAINING
2708                 printk("hp100: %s: Bring down the link\n", dev->name);
2709 #endif
2710
2711                 /* Wait for link to drop */
2712                 time = jiffies + (HZ / 10);
2713                 do {
2714                         if (~(hp100_inb(VG_LAN_CFG_1) & HP100_LINK_UP_ST))
2715                                 break;
2716                         if (!in_interrupt()) {
2717                                 set_current_state(TASK_INTERRUPTIBLE);
2718                                 schedule_timeout(1);
2719                         }
2720                 } while (time_after(time, jiffies));
2721
2722                 /* Start an addressed training and optionally request promiscuous port */
2723                 if ((dev->flags) & IFF_PROMISC) {
2724                         hp100_orb(HP100_PROM_MODE, VG_LAN_CFG_2);
2725                         if (lp->chip == HP100_CHIPID_LASSEN)
2726                                 hp100_orw(HP100_MACRQ_PROMSC, TRAIN_REQUEST);
2727                 } else {
2728                         hp100_andb(~HP100_PROM_MODE, VG_LAN_CFG_2);
2729                         /* For ETR parts we need to reset the prom. bit in the training
2730                          * register, otherwise promiscious mode won't be disabled.
2731                          */
2732                         if (lp->chip == HP100_CHIPID_LASSEN) {
2733                                 hp100_andw(~HP100_MACRQ_PROMSC, TRAIN_REQUEST);
2734                         }
2735                 }
2736
2737                 /* With ETR parts, frame format request bits can be set. */
2738                 if (lp->chip == HP100_CHIPID_LASSEN)
2739                         hp100_orb(HP100_MACRQ_FRAMEFMT_EITHER, TRAIN_REQUEST);
2740
2741                 hp100_orb(HP100_LINK_CMD | HP100_LOAD_ADDR | HP100_VG_RESET, VG_LAN_CFG_1);
2742
2743                 /* Note: Next wait could be omitted for Hood and earlier chips under */
2744                 /* certain circumstances */
2745                 /* TODO: check if hood/earlier and skip wait. */
2746
2747                 /* Wait for either short timeout for VG tones or long for login    */
2748                 /* Wait for the card hardware to signalise link cable status ok... */
2749                 hp100_page(MAC_CTRL);
2750                 time = jiffies + (1 * HZ);      /* 1 sec timeout for cable st */
2751                 do {
2752                         if (hp100_inb(VG_LAN_CFG_1) & HP100_LINK_CABLE_ST)
2753                                 break;
2754                         if (!in_interrupt()) {
2755                                 set_current_state(TASK_INTERRUPTIBLE);
2756                                 schedule_timeout(1);
2757                         }
2758                 } while (time_before(jiffies, time));
2759
2760                 if (time_after_eq(jiffies, time)) {
2761 #ifdef HP100_DEBUG_TRAINING
2762                         printk("hp100: %s: Link cable status not ok? Training aborted.\n", dev->name);
2763 #endif
2764                 } else {
2765 #ifdef HP100_DEBUG_TRAINING
2766                         printk
2767                             ("hp100: %s: HUB tones detected. Trying to train.\n",
2768                              dev->name);
2769 #endif
2770
2771                         time = jiffies + (2 * HZ);      /* again a timeout */
2772                         do {
2773                                 val = hp100_inb(VG_LAN_CFG_1);
2774                                 if ((val & (HP100_LINK_UP_ST))) {
2775 #ifdef HP100_DEBUG_TRAINING
2776                                         printk("hp100: %s: Passed training.\n", dev->name);
2777 #endif
2778                                         break;
2779                                 }
2780                                 if (!in_interrupt()) {
2781                                         set_current_state(TASK_INTERRUPTIBLE);
2782                                         schedule_timeout(1);
2783                                 }
2784                         } while (time_after(time, jiffies));
2785                 }
2786
2787                 /* If LINK_UP_ST is set, then we are logged into the hub. */
2788                 if (time_before_eq(jiffies, time) && (val & HP100_LINK_UP_ST)) {
2789 #ifdef HP100_DEBUG_TRAINING
2790                         printk("hp100: %s: Successfully logged into the HUB.\n", dev->name);
2791                         if (lp->chip == HP100_CHIPID_LASSEN) {
2792                                 val = hp100_inw(TRAIN_ALLOW);
2793                                 printk("hp100: %s: Card supports 100VG MAC Version \"%s\" ",
2794                                              dev->name, (hp100_inw(TRAIN_REQUEST) & HP100_CARD_MACVER) ? "802.12" : "Pre");
2795                                 printk("Driver will use MAC Version \"%s\"\n", (val & HP100_HUB_MACVER) ? "802.12" : "Pre");
2796                                 printk("hp100: %s: Frame format is %s.\n", dev->name, (val & HP100_MALLOW_FRAMEFMT) ? "802.5" : "802.3");
2797                         }
2798 #endif
2799                 } else {
2800                         /* If LINK_UP_ST is not set, login was not successful */
2801                         printk("hp100: %s: Problem logging into the HUB.\n", dev->name);
2802                         if (lp->chip == HP100_CHIPID_LASSEN) {
2803                                 /* Check allowed Register to find out why there is a problem. */
2804                                 val = hp100_inw(TRAIN_ALLOW);   /* wont work on non-ETR card */
2805 #ifdef HP100_DEBUG_TRAINING
2806                                 printk("hp100: %s: MAC Configuration requested: 0x%04x, HUB allowed: 0x%04x\n", dev->name, hp100_inw(TRAIN_REQUEST), val);
2807 #endif
2808                                 if (val & HP100_MALLOW_ACCDENIED)
2809                                         printk("hp100: %s: HUB access denied.\n", dev->name);
2810                                 if (val & HP100_MALLOW_CONFIGURE)
2811                                         printk("hp100: %s: MAC Configuration is incompatible with the Network.\n", dev->name);
2812                                 if (val & HP100_MALLOW_DUPADDR)
2813                                         printk("hp100: %s: Duplicate MAC Address on the Network.\n", dev->name);
2814                         }
2815                 }
2816
2817                 /* If we have put the chip into forced 100 Mbit mode earlier, go back */
2818                 /* to auto-select mode */
2819
2820                 if ((lp->chip == HP100_CHIPID_LASSEN) && (startst & HP100_LINK_CABLE_ST)) {
2821                         hp100_page(MAC_CTRL);
2822                         hp100_orb(HP100_DOT3_MAC, 10_LAN_CFG_2);
2823                 }
2824
2825                 val = hp100_inb(VG_LAN_CFG_1);
2826
2827                 /* Clear the MISC_ERROR Interrupt, which might be generated when doing the relogin */
2828                 hp100_page(PERFORMANCE);
2829                 hp100_outw(HP100_MISC_ERROR, IRQ_STATUS);
2830
2831                 if (val & HP100_LINK_UP_ST)
2832                         return (0);     /* login was ok */
2833                 else {
2834                         printk("hp100: %s: Training failed.\n", dev->name);
2835                         hp100_down_vg_link(dev);
2836                         return -EIO;
2837                 }
2838         }
2839         /* no forced relogin & already link there->no training. */
2840         return -EIO;
2841 }
2842
2843 static void hp100_cascade_reset(struct net_device *dev, u_short enable)
2844 {
2845         int ioaddr = dev->base_addr;
2846         struct hp100_private *lp = (struct hp100_private *) dev->priv;
2847
2848 #ifdef HP100_DEBUG_B
2849         hp100_outw(0x4226, TRACE);
2850         printk("hp100: %s: cascade_reset\n", dev->name);
2851 #endif
2852
2853         if (enable == TRUE) {
2854                 hp100_outw(HP100_HW_RST | HP100_RESET_LB, OPTION_LSW);
2855                 if (lp->chip == HP100_CHIPID_LASSEN) {
2856                         /* Lassen requires a PCI transmit fifo reset */
2857                         hp100_page(HW_MAP);
2858                         hp100_andb(~HP100_PCI_RESET, PCICTRL2);
2859                         hp100_orb(HP100_PCI_RESET, PCICTRL2);
2860                         /* Wait for min. 300 ns */
2861                         /* we cant use jiffies here, because it may be */
2862                         /* that we have disabled the timer... */
2863                         udelay(400);
2864                         hp100_andb(~HP100_PCI_RESET, PCICTRL2);
2865                         hp100_page(PERFORMANCE);
2866                 }
2867         } else {                /* bring out of reset */
2868                 hp100_outw(HP100_HW_RST | HP100_SET_LB, OPTION_LSW);
2869                 udelay(400);
2870                 hp100_page(PERFORMANCE);
2871         }
2872 }
2873
2874 #ifdef HP100_DEBUG
2875 void hp100_RegisterDump(struct net_device *dev)
2876 {
2877         int ioaddr = dev->base_addr;
2878         int Page;
2879         int Register;
2880
2881         /* Dump common registers */
2882         printk("hp100: %s: Cascade Register Dump\n", dev->name);
2883         printk("hardware id #1: 0x%.2x\n", hp100_inb(HW_ID));
2884         printk("hardware id #2/paging: 0x%.2x\n", hp100_inb(PAGING));
2885         printk("option #1: 0x%.4x\n", hp100_inw(OPTION_LSW));
2886         printk("option #2: 0x%.4x\n", hp100_inw(OPTION_MSW));
2887
2888         /* Dump paged registers */
2889         for (Page = 0; Page < 8; Page++) {
2890                 /* Dump registers */
2891                 printk("page: 0x%.2x\n", Page);
2892                 outw(Page, ioaddr + 0x02);
2893                 for (Register = 0x8; Register < 0x22; Register += 2) {
2894                         /* Display Register contents except data port */
2895                         if (((Register != 0x10) && (Register != 0x12)) || (Page > 0)) {
2896                                 printk("0x%.2x = 0x%.4x\n", Register, inw(ioaddr + Register));
2897                         }
2898                 }
2899         }
2900         hp100_page(PERFORMANCE);
2901 }
2902 #endif
2903
2904
2905 /*
2906  *  module section
2907  */
2908
2909 #ifdef MODULE
2910
2911 MODULE_LICENSE("GPL");
2912 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, "
2913               "Siegfried \"Frieder\" Loeffler (dg1sek) <floeff@mathematik.uni-stuttgart.de>");
2914 MODULE_DESCRIPTION("HP CASCADE Architecture Driver for 100VG-AnyLan Network Adapters");
2915
2916 /*
2917  * Note: if you have more than five 100vg cards in your pc, feel free to
2918  * increase this value 
2919  */
2920
2921 #define HP100_DEVICES 5
2922
2923 /*
2924  * Note: to register three eisa or pci devices, use:
2925  * option hp100 hp100_port=0,0,0
2926  *        to register one card at io 0x280 as eth239, use:
2927  * option hp100 hp100_port=0x280 hp100_name=eth239
2928  */
2929
2930 /* Parameters set by insmod */
2931 static int hp100_port[HP100_DEVICES] = { 0, [1 ... (HP100_DEVICES-1)] = -1 };
2932 MODULE_PARM(hp100_port, "1-" __MODULE_STRING(HP100_DEVICES) "i");
2933
2934 /* Allocate HP100_DEVICES strings of length IFNAMSIZ, one string for each device */
2935 static char hp100_name[HP100_DEVICES][IFNAMSIZ] = { "", "", "", "", "" };
2936 /* Allow insmod to write those HP100_DEVICES strings individually */
2937 MODULE_PARM(hp100_name, "1-" __MODULE_STRING(HP100_DEVICES) "c" __MODULE_STRING(IFNAMSIZ));
2938
2939 /* List of devices */
2940 static struct net_device *hp100_devlist[HP100_DEVICES];
2941
2942 static void release_dev(int i)
2943 {
2944         struct net_device *d = hp100_devlist[i];
2945         struct hp100_private *p = (struct hp100_private *) d->priv;
2946
2947         unregister_netdev(d);
2948         release_region(d->base_addr, HP100_REGION_SIZE);
2949
2950         if (p->mode == 1)       /* busmaster */
2951                 pci_free_consistent(p->pci_dev, MAX_RINGSIZE + 0x0f, p->page_vaddr_algn, virt_to_whatever(d, p->page_vaddr_algn));
2952         if (p->mem_ptr_virt)
2953                 iounmap(p->mem_ptr_virt);
2954         kfree(d->priv);
2955         d->priv = NULL;
2956         kfree(d);
2957         hp100_devlist[i] = NULL;
2958 }
2959
2960 static int __init hp100_module_init(void)
2961 {
2962         int i, cards;
2963
2964         if (hp100_port == 0 && !EISA_bus && !pcibios_present())
2965                 printk("hp100: You should not use auto-probing with insmod!\n");
2966
2967         /* Loop on all possible base addresses */
2968         i = -1;
2969         cards = 0;
2970         while ((hp100_port[++i] != -1) && (i < HP100_DEVICES)) {
2971                 /* Create device and set basics args */
2972                 hp100_devlist[i] = kmalloc(sizeof(struct net_device), GFP_KERNEL);
2973                 if (!hp100_devlist[i])
2974                         goto fail;
2975                 memset(hp100_devlist[i], 0x00, sizeof(struct net_device));
2976 #if LINUX_VERSION_CODE >= 0x020362      /* 2.3.99-pre7 */
2977                 memcpy(hp100_devlist[i]->name, hp100_name[i], IFNAMSIZ);        /* Copy name */
2978 #else
2979                 hp100_devlist[i]->name = hp100_name[i];
2980 #endif                          /* LINUX_VERSION_CODE >= 0x020362 */
2981                 hp100_devlist[i]->base_addr = hp100_port[i];
2982                 hp100_devlist[i]->init = &hp100_probe;
2983
2984                 /* Try to create the device */
2985                 if (register_netdev(hp100_devlist[i]) != 0) {
2986                         /* DeAllocate everything */
2987                         /* Note: if dev->priv is mallocated, there is no way to fail */
2988                         kfree(hp100_devlist[i]);
2989                         hp100_devlist[i] = (struct net_device *) NULL;
2990                 } else
2991                         cards++;
2992         }                       /* Loop over all devices */
2993
2994         return cards > 0 ? 0 : -ENODEV;
2995       fail:
2996         while (cards && --i)
2997                 if (hp100_devlist[i]) {
2998                         release_dev(i);
2999                         --cards;
3000                 }
3001         return -ENOMEM;
3002 }
3003
3004 static void __exit hp100_module_exit(void)
3005 {
3006         int i;
3007
3008         /* TODO: Check if all skb's are released/freed. */
3009         for (i = 0; i < HP100_DEVICES; i++)
3010                 if (hp100_devlist[i] != (struct net_device *) NULL)
3011                         release_dev(i);
3012 }
3013
3014 module_init(hp100_module_init)
3015 module_exit(hp100_module_exit)
3016
3017 #endif                          /* MODULE */
3018
3019
3020 /*
3021  * Local variables:
3022  *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c hp100.c"
3023  *  c-indent-level: 2
3024  *  tab-width: 8
3025  * End:
3026  */