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