hp100.c 88.2 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
** hp100.c
L
Linus Torvalds 已提交
3 4 5 6 7
** HP CASCADE Architecture Driver for 100VG-AnyLan Network Adapters
**
** $Id: hp100.c,v 1.58 2001/09/24 18:03:01 perex Exp perex $
**
** Based on the HP100 driver written by Jaroslav Kysela <perex@jcu.cz>
8
** Extended for new busmaster capable chipsets by
L
Linus Torvalds 已提交
9 10
** Siegfried "Frieder" Loeffler (dg1sek) <floeff@mathematik.uni-stuttgart.de>
**
11
** Maintained by: Jaroslav Kysela <perex@perex.cz>
12
**
L
Linus Torvalds 已提交
13 14
** This driver has only been tested with
** -- HP J2585B 10/100 Mbit/s PCI Busmaster
15
** -- HP J2585A 10/100 Mbit/s PCI
P
Pavel Machek 已提交
16 17
** -- HP J2970A 10 Mbit/s PCI Combo 10base-T/BNC
** -- HP J2973A 10 Mbit/s PCI 10base-T
L
Linus Torvalds 已提交
18 19 20
** -- HP J2573  10/100 ISA
** -- Compex ReadyLink ENET100-VG4  10/100 Mbit/s PCI / EISA
** -- Compex FreedomLine 100/VG  10/100 Mbit/s ISA / EISA / PCI
21
**
L
Linus Torvalds 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
** but it should also work with the other CASCADE based adapters.
**
** TODO:
**       -  J2573 seems to hang sometimes when in shared memory mode.
**       -  Mode for Priority TX
**       -  Check PCI registers, performance might be improved?
**       -  To reduce interrupt load in busmaster, one could switch off
**          the interrupts that are used to refill the queues whenever the
**          queues are filled up to more than a certain threshold.
**       -  some updates for EISA version of card
**
**
**   This code is free software; you can redistribute it and/or modify
**   it under the terms of the GNU General Public License as published by
**   the Free Software Foundation; either version 2 of the License, or
**   (at your option) any later version.
**
**   This code is distributed in the hope that it will be useful,
**   but WITHOUT ANY WARRANTY; without even the implied warranty of
**   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**   GNU General Public License for more details.
**
**   You should have received a copy of the GNU General Public License
**   along with this program; if not, write to the Free Software
**   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
** 1.57c -> 1.58
**   - used indent to change coding-style
**   - added KTI DP-200 EISA ID
**   - ioremap is also used for low (<1MB) memory (multi-architecture support)
**
** 1.57b -> 1.57c - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
**   - release resources on failure in init_module
**
** 1.57 -> 1.57b - Jean II
**   - fix spinlocks, SMP is now working !
**
** 1.56 -> 1.57
**   - updates for new PCI interface for 2.1 kernels
**
** 1.55 -> 1.56
**   - removed printk in misc. interrupt and update statistics to allow
**     monitoring of card status
**   - timing changes in xmit routines, relogin to 100VG hub added when
**     driver does reset
**   - included fix for Compex FreedomLine PCI adapter
68
**
L
Linus Torvalds 已提交
69 70 71 72 73 74 75
** 1.54 -> 1.55
**   - fixed bad initialization in init_module
**   - added Compex FreedomLine adapter
**   - some fixes in card initialization
**
** 1.53 -> 1.54
**   - added hardware multicast filter support (doesn't work)
76
**   - little changes in hp100_sense_lan routine
L
Linus Torvalds 已提交
77 78
**     - added support for Coax and AUI (J2970)
**   - fix for multiple cards and hp100_mode parameter (insmod)
79
**   - fix for shared IRQ
L
Linus Torvalds 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
**
** 1.52 -> 1.53
**   - fixed bug in multicast support
**
*/

#define HP100_DEFAULT_PRIORITY_TX 0

#undef HP100_DEBUG
#undef HP100_DEBUG_B		/* Trace  */
#undef HP100_DEBUG_BM		/* Debug busmaster code (PDL stuff) */

#undef HP100_DEBUG_TRAINING	/* Debug login-to-hub procedure */
#undef HP100_DEBUG_TX
#undef HP100_DEBUG_IRQ
#undef HP100_DEBUG_RX

#undef HP100_MULTICAST_FILTER	/* Need to be debugged... */

#include <linux/module.h>
#include <linux/kernel.h>
101
#include <linux/sched.h>
L
Linus Torvalds 已提交
102 103 104 105 106 107
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/ioport.h>
#include <linux/interrupt.h>
#include <linux/eisa.h>
#include <linux/pci.h>
108
#include <linux/dma-mapping.h>
L
Linus Torvalds 已提交
109 110 111 112 113 114 115 116
#include <linux/spinlock.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/bitops.h>
117
#include <linux/jiffies.h>
L
Linus Torvalds 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

#include <asm/io.h>

#include "hp100.h"

/*
 *  defines
 */

#define HP100_BUS_ISA     0
#define HP100_BUS_EISA    1
#define HP100_BUS_PCI     2

#define HP100_REGION_SIZE	0x20	/* for ioports */
#define HP100_SIG_LEN		8	/* same as EISA_SIG_LEN */

#define HP100_MAX_PACKET_SIZE	(1536+4)
#define HP100_MIN_PACKET_SIZE	60

#ifndef HP100_DEFAULT_RX_RATIO
/* default - 75% onboard memory on the card are used for RX packets */
#define HP100_DEFAULT_RX_RATIO	75
#endif

#ifndef HP100_DEFAULT_PRIORITY_TX
/* default - don't enable transmit outgoing packets as priority */
#define HP100_DEFAULT_PRIORITY_TX 0
#endif

/*
 *  structures
 */

struct hp100_private {
	spinlock_t lock;
	char id[HP100_SIG_LEN];
	u_short chip;
	u_short soft_model;
	u_int memory_size;
	u_int virt_memory_size;
	u_short rx_ratio;	/* 1 - 99 */
	u_short priority_tx;	/* != 0 - priority tx */
	u_short mode;		/* PIO, Shared Mem or Busmaster */
	u_char bus;
	struct pci_dev *pci_dev;
	short mem_mapped;	/* memory mapped access */
	void __iomem *mem_ptr_virt;	/* virtual memory mapped area, maybe NULL */
	unsigned long mem_ptr_phys;	/* physical memory mapped area */
	short lan_type;		/* 10Mb/s, 100Mb/s or -1 (error) */
	int hub_status;		/* was login to hub successful? */
	u_char mac1_mode;
	u_char mac2_mode;
	u_char hash_bytes[8];

	/* Rings for busmaster mode: */
	hp100_ring_t *rxrhead;	/* Head (oldest) index into rxring */
	hp100_ring_t *rxrtail;	/* Tail (newest) index into rxring */
	hp100_ring_t *txrhead;	/* Head (oldest) index into txring */
	hp100_ring_t *txrtail;	/* Tail (newest) index into txring */

	hp100_ring_t rxring[MAX_RX_PDL];
	hp100_ring_t txring[MAX_TX_PDL];

	u_int *page_vaddr_algn;	/* Aligned virtual address of allocated page */
	u_long whatever_offset;	/* Offset to bus/phys/dma address */
L
Lucas De Marchi 已提交
183 184
	int rxrcommit;		/* # Rx PDLs committed to adapter */
	int txrcommit;		/* # Tx PDLs committed to adapter */
L
Linus Torvalds 已提交
185 186 187 188 189
};

/*
 *  variables
 */
190
#ifdef CONFIG_ISA
191
static const char *hp100_isa_tbl[] = {
L
Linus Torvalds 已提交
192 193 194
	"HWPF150", /* HP J2573 rev A */
	"HWP1950", /* HP J2573 */
};
195
#endif
L
Linus Torvalds 已提交
196 197

#ifdef CONFIG_EISA
198
static struct eisa_device_id hp100_eisa_tbl[] = {
L
Linus Torvalds 已提交
199 200 201 202 203 204 205 206 207 208 209 210
	{ "HWPF180" }, /* HP J2577 rev A */
	{ "HWP1920" }, /* HP 27248B */
	{ "HWP1940" }, /* HP J2577 */
	{ "HWP1990" }, /* HP J2577 */
	{ "CPX0301" }, /* ReadyLink ENET100-VG4 */
	{ "CPX0401" }, /* FreedomLine 100/VG */
	{ "" }	       /* Mandatory final entry ! */
};
MODULE_DEVICE_TABLE(eisa, hp100_eisa_tbl);
#endif

#ifdef CONFIG_PCI
211
static DEFINE_PCI_DEVICE_TABLE(hp100_pci_tbl) = {
L
Linus Torvalds 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	{PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_J2585A, PCI_ANY_ID, PCI_ANY_ID,},
	{PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_J2585B, PCI_ANY_ID, PCI_ANY_ID,},
	{PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_J2970A, PCI_ANY_ID, PCI_ANY_ID,},
	{PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_J2973A, PCI_ANY_ID, PCI_ANY_ID,},
	{PCI_VENDOR_ID_COMPEX, PCI_DEVICE_ID_COMPEX_ENET100VG4, PCI_ANY_ID, PCI_ANY_ID,},
	{PCI_VENDOR_ID_COMPEX2, PCI_DEVICE_ID_COMPEX2_100VG, PCI_ANY_ID, PCI_ANY_ID,},
/*	{PCI_VENDOR_ID_KTI, PCI_DEVICE_ID_KTI_DP200, PCI_ANY_ID, PCI_ANY_ID }, */
	{}			/* Terminating entry */
};
MODULE_DEVICE_TABLE(pci, hp100_pci_tbl);
#endif

static int hp100_rx_ratio = HP100_DEFAULT_RX_RATIO;
static int hp100_priority_tx = HP100_DEFAULT_PRIORITY_TX;
static int hp100_mode = 1;

module_param(hp100_rx_ratio, int, 0);
module_param(hp100_priority_tx, int, 0);
module_param(hp100_mode, int, 0);

/*
 *  prototypes
 */

static int hp100_probe1(struct net_device *dev, int ioaddr, u_char bus,
			struct pci_dev *pci_dev);


static int hp100_open(struct net_device *dev);
static int hp100_close(struct net_device *dev);
242 243 244 245
static netdev_tx_t hp100_start_xmit(struct sk_buff *skb,
				    struct net_device *dev);
static netdev_tx_t hp100_start_xmit_bm(struct sk_buff *skb,
				       struct net_device *dev);
L
Linus Torvalds 已提交
246 247 248 249 250 251
static void hp100_rx(struct net_device *dev);
static struct net_device_stats *hp100_get_stats(struct net_device *dev);
static void hp100_misc_interrupt(struct net_device *dev);
static void hp100_update_stats(struct net_device *dev);
static void hp100_clear_stats(struct hp100_private *lp, int ioaddr);
static void hp100_set_multicast_list(struct net_device *dev);
252
static irqreturn_t hp100_interrupt(int irq, void *dev_id);
L
Linus Torvalds 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
static void hp100_start_interface(struct net_device *dev);
static void hp100_stop_interface(struct net_device *dev);
static void hp100_load_eeprom(struct net_device *dev, u_short ioaddr);
static int hp100_sense_lan(struct net_device *dev);
static int hp100_login_to_vg_hub(struct net_device *dev,
				 u_short force_relogin);
static int hp100_down_vg_link(struct net_device *dev);
static void hp100_cascade_reset(struct net_device *dev, u_short enable);
static void hp100_BM_shutdown(struct net_device *dev);
static void hp100_mmuinit(struct net_device *dev);
static void hp100_init_pdls(struct net_device *dev);
static int hp100_init_rxpdl(struct net_device *dev,
			    register hp100_ring_t * ringptr,
			    register u_int * pdlptr);
static int hp100_init_txpdl(struct net_device *dev,
			    register hp100_ring_t * ringptr,
			    register u_int * pdlptr);
static void hp100_rxfill(struct net_device *dev);
static void hp100_hwinit(struct net_device *dev);
static void hp100_clean_txring(struct net_device *dev);
#ifdef HP100_DEBUG
static void hp100_RegisterDump(struct net_device *dev);
#endif

/* Conversion to new PCI API :
 * Convert an address in a kernel buffer to a bus/phys/dma address.
 * This work *only* for memory fragments part of lp->page_vaddr,
 * because it was properly DMA allocated via pci_alloc_consistent(),
A
Adrian Bunk 已提交
281
 * so we just need to "retrieve" the original mapping to bus/phys/dma
L
Linus Torvalds 已提交
282 283 284 285 286 287 288 289 290
 * address - Jean II */
static inline dma_addr_t virt_to_whatever(struct net_device *dev, u32 * ptr)
{
	struct hp100_private *lp = netdev_priv(dev);
	return ((u_long) ptr) + lp->whatever_offset;
}

static inline u_int pdl_map_data(struct hp100_private *lp, void *data)
{
291
	return pci_map_single(lp->pci_dev, data,
L
Linus Torvalds 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
			      MAX_ETHER_SIZE, PCI_DMA_FROMDEVICE);
}

/* TODO: This function should not really be needed in a good design... */
static void wait(void)
{
	mdelay(1);
}

/*
 *  probe functions
 *  These functions should - if possible - avoid doing write operations
 *  since this could cause problems when the card is not installed.
 */

/*
 * Read board id and convert to string.
 * Effectively same code as decode_eisa_sig
 */
B
Bill Pemberton 已提交
311
static const char *hp100_read_id(int ioaddr)
L
Linus Torvalds 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
{
	int i;
	static char str[HP100_SIG_LEN];
	unsigned char sig[4], sum;
        unsigned short rev;

	hp100_page(ID_MAC_ADDR);
	sum = 0;
	for (i = 0; i < 4; i++) {
		sig[i] = hp100_inb(BOARD_ID + i);
		sum += sig[i];
	}

	sum += hp100_inb(BOARD_ID + i);
	if (sum != 0xff)
		return NULL;	/* bad checksum */

        str[0] = ((sig[0] >> 2) & 0x1f) + ('A' - 1);
        str[1] = (((sig[0] & 3) << 3) | (sig[1] >> 5)) + ('A' - 1);
        str[2] = (sig[1] & 0x1f) + ('A' - 1);
        rev = (sig[2] << 8) | sig[3];
        sprintf(str + 3, "%04X", rev);

	return str;
}

338
#ifdef CONFIG_ISA
339
static __init int hp100_isa_probe1(struct net_device *dev, int ioaddr)
L
Linus Torvalds 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
{
	const char *sig;
	int i;

	if (!request_region(ioaddr, HP100_REGION_SIZE, "hp100"))
		goto err;

	if (hp100_inw(HW_ID) != HP100_HW_ID_CASCADE) {
		release_region(ioaddr, HP100_REGION_SIZE);
		goto err;
	}

	sig = hp100_read_id(ioaddr);
	release_region(ioaddr, HP100_REGION_SIZE);

	if (sig == NULL)
		goto err;

	for (i = 0; i < ARRAY_SIZE(hp100_isa_tbl); i++) {
359
		if (!strcmp(hp100_isa_tbl[i], sig))
L
Linus Torvalds 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
			break;

	}

	if (i < ARRAY_SIZE(hp100_isa_tbl))
		return hp100_probe1(dev, ioaddr, HP100_BUS_ISA, NULL);
 err:
	return -ENODEV;

}
/*
 * Probe for ISA board.
 * EISA and PCI are handled by device infrastructure.
 */

375
static int  __init hp100_isa_probe(struct net_device *dev, int addr)
L
Linus Torvalds 已提交
376 377 378
{
	int err = -ENODEV;

379
	/* Probe for a specific ISA address */
L
Linus Torvalds 已提交
380 381 382
	if (addr > 0xff && addr < 0x400)
		err = hp100_isa_probe1(dev, addr);

383
	else if (addr != 0)
L
Linus Torvalds 已提交
384 385 386 387 388 389 390 391 392 393 394 395
		err = -ENXIO;

	else {
		/* Probe all ISA possible port regions */
		for (addr = 0x100; addr < 0x400; addr += 0x20) {
			err = hp100_isa_probe1(dev, addr);
			if (!err)
				break;
		}
	}
	return err;
}
396
#endif /* CONFIG_ISA */
L
Linus Torvalds 已提交
397

398
#if !defined(MODULE) && defined(CONFIG_ISA)
399
struct net_device * __init hp100_probe(int unit)
L
Linus Torvalds 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
{
	struct net_device *dev = alloc_etherdev(sizeof(struct hp100_private));
	int err;

	if (!dev)
		return ERR_PTR(-ENODEV);

#ifdef HP100_DEBUG_B
	hp100_outw(0x4200, TRACE);
	printk("hp100: %s: probe\n", dev->name);
#endif

	if (unit >= 0) {
		sprintf(dev->name, "eth%d", unit);
		netdev_boot_setup_check(dev);
	}

	err = hp100_isa_probe(dev, dev->base_addr);
	if (err)
		goto out;

	return dev;
 out:
	free_netdev(dev);
	return ERR_PTR(err);
}
426
#endif /* !MODULE && CONFIG_ISA */
L
Linus Torvalds 已提交
427

428 429 430 431 432
static const struct net_device_ops hp100_bm_netdev_ops = {
	.ndo_open		= hp100_open,
	.ndo_stop		= hp100_close,
	.ndo_start_xmit		= hp100_start_xmit_bm,
	.ndo_get_stats 		= hp100_get_stats,
433
	.ndo_set_rx_mode	= hp100_set_multicast_list,
434 435 436 437 438 439 440 441 442 443
	.ndo_change_mtu		= eth_change_mtu,
	.ndo_set_mac_address 	= eth_mac_addr,
	.ndo_validate_addr	= eth_validate_addr,
};

static const struct net_device_ops hp100_netdev_ops = {
	.ndo_open		= hp100_open,
	.ndo_stop		= hp100_close,
	.ndo_start_xmit		= hp100_start_xmit,
	.ndo_get_stats 		= hp100_get_stats,
444
	.ndo_set_rx_mode	= hp100_set_multicast_list,
445 446 447 448 449
	.ndo_change_mtu		= eth_change_mtu,
	.ndo_set_mac_address 	= eth_mac_addr,
	.ndo_validate_addr	= eth_validate_addr,
};

450 451
static int hp100_probe1(struct net_device *dev, int ioaddr, u_char bus,
			struct pci_dev *pci_dev)
L
Linus Torvalds 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
{
	int i;
	int err = -ENODEV;
	const char *eid;
	u_int chip;
	u_char uc;
	u_int memory_size = 0, virt_memory_size = 0;
	u_short local_mode, lsw;
	short mem_mapped;
	unsigned long mem_ptr_phys;
	void __iomem *mem_ptr_virt;
	struct hp100_private *lp;

#ifdef HP100_DEBUG_B
	hp100_outw(0x4201, TRACE);
	printk("hp100: %s: probe1\n", dev->name);
#endif

	/* memory region for programmed i/o */
	if (!request_region(ioaddr, HP100_REGION_SIZE, "hp100"))
		goto out1;

474
	if (hp100_inw(HW_ID) != HP100_HW_ID_CASCADE)
L
Linus Torvalds 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
		goto out2;

	chip = hp100_inw(PAGING) & HP100_CHIPID_MASK;
#ifdef HP100_DEBUG
	if (chip == HP100_CHIPID_SHASTA)
		printk("hp100: %s: Shasta Chip detected. (This is a pre 802.12 chip)\n", dev->name);
	else if (chip == HP100_CHIPID_RAINIER)
		printk("hp100: %s: Rainier Chip detected. (This is a pre 802.12 chip)\n", dev->name);
	else if (chip == HP100_CHIPID_LASSEN)
		printk("hp100: %s: Lassen Chip detected.\n", dev->name);
	else
		printk("hp100: %s: Warning: Unknown CASCADE chip (id=0x%.4x).\n", dev->name, chip);
#endif

	dev->base_addr = ioaddr;

	eid = hp100_read_id(ioaddr);
	if (eid == NULL) {	/* bad checksum? */
		printk(KERN_WARNING "hp100_probe: bad ID checksum at base port 0x%x\n", ioaddr);
		goto out2;
	}

	hp100_page(ID_MAC_ADDR);
	for (i = uc = 0; i < 7; i++)
		uc += hp100_inb(LAN_ADDR + i);
	if (uc != 0xff) {
		printk(KERN_WARNING "hp100_probe: bad lan address checksum at port 0x%x)\n", ioaddr);
		err = -EIO;
		goto out2;
	}

	/* Make sure, that all registers are correctly updated... */

	hp100_load_eeprom(dev, ioaddr);
	wait();

	/*
	 * Determine driver operation mode
	 *
	 * Use the variable "hp100_mode" upon insmod or as kernel parameter to
	 * force driver modes:
	 * hp100_mode=1 -> default, use busmaster mode if configured.
517
	 * hp100_mode=2 -> enable shared memory mode
L
Linus Torvalds 已提交
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
	 * hp100_mode=3 -> force use of i/o mapped mode.
	 * hp100_mode=4 -> same as 1, but re-set the enable bit on the card.
	 */

	/*
	 * LSW values:
	 *   0x2278 -> J2585B, PnP shared memory mode
	 *   0x2270 -> J2585B, shared memory mode, 0xdc000
	 *   0xa23c -> J2585B, I/O mapped mode
	 *   0x2240 -> EISA COMPEX, BusMaster (Shasta Chip)
	 *   0x2220 -> EISA HP, I/O (Shasta Chip)
	 *   0x2260 -> EISA HP, BusMaster (Shasta Chip)
	 */

#if 0
	local_mode = 0x2270;
	hp100_outw(0xfefe, OPTION_LSW);
	hp100_outw(local_mode | HP100_SET_LB | HP100_SET_HB, OPTION_LSW);
#endif

	/* hp100_mode value maybe used in future by another card */
	local_mode = hp100_mode;
	if (local_mode < 1 || local_mode > 4)
		local_mode = 1;	/* default */
#ifdef HP100_DEBUG
	printk("hp100: %s: original LSW = 0x%x\n", dev->name,
	       hp100_inw(OPTION_LSW));
#endif

	if (local_mode == 3) {
		hp100_outw(HP100_MEM_EN | HP100_RESET_LB, OPTION_LSW);
		hp100_outw(HP100_IO_EN | HP100_SET_LB, OPTION_LSW);
		hp100_outw(HP100_BM_WRITE | HP100_BM_READ | HP100_RESET_HB, OPTION_LSW);
		printk("hp100: IO mapped mode forced.\n");
	} else if (local_mode == 2) {
		hp100_outw(HP100_MEM_EN | HP100_SET_LB, OPTION_LSW);
		hp100_outw(HP100_IO_EN | HP100_SET_LB, OPTION_LSW);
		hp100_outw(HP100_BM_WRITE | HP100_BM_READ | HP100_RESET_HB, OPTION_LSW);
		printk("hp100: Shared memory mode requested.\n");
	} else if (local_mode == 4) {
		if (chip == HP100_CHIPID_LASSEN) {
			hp100_outw(HP100_BM_WRITE | HP100_BM_READ | HP100_SET_HB, OPTION_LSW);
			hp100_outw(HP100_IO_EN | HP100_MEM_EN | HP100_RESET_LB, OPTION_LSW);
			printk("hp100: Busmaster mode requested.\n");
		}
		local_mode = 1;
	}

	if (local_mode == 1) {	/* default behaviour */
		lsw = hp100_inw(OPTION_LSW);

		if ((lsw & HP100_IO_EN) && (~lsw & HP100_MEM_EN) &&
		    (~lsw & (HP100_BM_WRITE | HP100_BM_READ))) {
#ifdef HP100_DEBUG
			printk("hp100: %s: IO_EN bit is set on card.\n", dev->name);
#endif
			local_mode = 3;
		} else if (chip == HP100_CHIPID_LASSEN &&
			   (lsw & (HP100_BM_WRITE | HP100_BM_READ)) == (HP100_BM_WRITE | HP100_BM_READ)) {
			/* Conversion to new PCI API :
			 * I don't have the doc, but I assume that the card
			 * can map the full 32bit address space.
			 * Also, we can have EISA Busmaster cards (not tested),
			 * so beware !!! - Jean II */
			if((bus == HP100_BUS_PCI) &&
583
			   (pci_set_dma_mask(pci_dev, DMA_BIT_MASK(32)))) {
L
Linus Torvalds 已提交
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
				/* Gracefully fallback to shared memory */
				goto busmasterfail;
			}
			printk("hp100: Busmaster mode enabled.\n");
			hp100_outw(HP100_MEM_EN | HP100_IO_EN | HP100_RESET_LB, OPTION_LSW);
		} else {
		busmasterfail:
#ifdef HP100_DEBUG
			printk("hp100: %s: Card not configured for BM or BM not supported with this card.\n", dev->name);
			printk("hp100: %s: Trying shared memory mode.\n", dev->name);
#endif
			/* In this case, try shared memory mode */
			local_mode = 2;
			hp100_outw(HP100_MEM_EN | HP100_SET_LB, OPTION_LSW);
			/* hp100_outw(HP100_IO_EN|HP100_RESET_LB, OPTION_LSW); */
		}
	}
#ifdef HP100_DEBUG
	printk("hp100: %s: new LSW = 0x%x\n", dev->name, hp100_inw(OPTION_LSW));
#endif

	/* Check for shared memory on the card, eventually remap it */
	hp100_page(HW_MAP);
	mem_mapped = ((hp100_inw(OPTION_LSW) & (HP100_MEM_EN)) != 0);
	mem_ptr_phys = 0UL;
	mem_ptr_virt = NULL;
	memory_size = (8192 << ((hp100_inb(SRAM) >> 5) & 0x07));
	virt_memory_size = 0;

	/* For memory mapped or busmaster mode, we want the memory address */
	if (mem_mapped || (local_mode == 1)) {
		mem_ptr_phys = (hp100_inw(MEM_MAP_LSW) | (hp100_inw(MEM_MAP_MSW) << 16));
		mem_ptr_phys &= ~0x1fff;	/* 8k alignment */

		if (bus == HP100_BUS_ISA && (mem_ptr_phys & ~0xfffff) != 0) {
			printk("hp100: Can only use programmed i/o mode.\n");
			mem_ptr_phys = 0;
			mem_mapped = 0;
			local_mode = 3;	/* Use programmed i/o */
		}

		/* We do not need access to shared memory in busmaster mode */
		/* However in slave mode we need to remap high (>1GB) card memory  */
		if (local_mode != 1) {	/* = not busmaster */
			/* We try with smaller memory sizes, if ioremap fails */
			for (virt_memory_size = memory_size; virt_memory_size > 16383; virt_memory_size >>= 1) {
				if ((mem_ptr_virt = ioremap((u_long) mem_ptr_phys, virt_memory_size)) == NULL) {
#ifdef HP100_DEBUG
					printk("hp100: %s: ioremap for 0x%x bytes high PCI memory at 0x%lx failed\n", dev->name, virt_memory_size, mem_ptr_phys);
#endif
				} else {
#ifdef HP100_DEBUG
					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);
#endif
					break;
				}
			}

			if (mem_ptr_virt == NULL) {	/* all ioremap tries failed */
				printk("hp100: Failed to ioremap the PCI card memory. Will have to use i/o mapped mode.\n");
				local_mode = 3;
				virt_memory_size = 0;
			}
		}
	}

	if (local_mode == 3) {	/* io mapped forced */
		mem_mapped = 0;
		mem_ptr_phys = 0;
		mem_ptr_virt = NULL;
		printk("hp100: Using (slow) programmed i/o mode.\n");
	}

	/* Initialise the "private" data structure for this card. */
	lp = netdev_priv(dev);

	spin_lock_init(&lp->lock);
	strlcpy(lp->id, eid, HP100_SIG_LEN);
	lp->chip = chip;
	lp->mode = local_mode;
	lp->bus = bus;
	lp->pci_dev = pci_dev;
	lp->priority_tx = hp100_priority_tx;
	lp->rx_ratio = hp100_rx_ratio;
	lp->mem_ptr_phys = mem_ptr_phys;
	lp->mem_ptr_virt = mem_ptr_virt;
	hp100_page(ID_MAC_ADDR);
	lp->soft_model = hp100_inb(SOFT_MODEL);
	lp->mac1_mode = HP100_MAC1MODE3;
	lp->mac2_mode = HP100_MAC2MODE3;
	memset(&lp->hash_bytes, 0x00, 8);

	dev->base_addr = ioaddr;

	lp->memory_size = memory_size;
	lp->virt_memory_size = virt_memory_size;
	lp->rx_ratio = hp100_rx_ratio;	/* can be conf'd with insmod */

	if (lp->mode == 1)	/* busmaster */
683
		dev->netdev_ops = &hp100_bm_netdev_ops;
L
Linus Torvalds 已提交
684
	else
685
		dev->netdev_ops = &hp100_netdev_ops;
L
Linus Torvalds 已提交
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708

	/* Ask the card for which IRQ line it is configured */
	if (bus == HP100_BUS_PCI) {
		dev->irq = pci_dev->irq;
	} else {
		hp100_page(HW_MAP);
		dev->irq = hp100_inb(IRQ_CHANNEL) & HP100_IRQMASK;
		if (dev->irq == 2)
			dev->irq = 9;
	}

	if (lp->mode == 1)	/* busmaster */
		dev->dma = 4;

	/* Ask the card for its MAC address and store it for later use. */
	hp100_page(ID_MAC_ADDR);
	for (i = uc = 0; i < 6; i++)
		dev->dev_addr[i] = hp100_inb(LAN_ADDR + i);

	/* Reset statistics (counters) */
	hp100_clear_stats(lp, ioaddr);

	/* If busmaster mode is wanted, a dma-capable memory area is needed for
709
	 * the rx and tx PDLs
L
Linus Torvalds 已提交
710
	 * PCI cards can access the whole PC memory. Therefore GFP_DMA is not
711
	 * needed for the allocation of the memory area.
L
Linus Torvalds 已提交
712 713 714 715 716 717 718
	 */

	/* TODO: We do not need this with old cards, where PDLs are stored
	 * in the cards shared memory area. But currently, busmaster has been
	 * implemented/tested only with the lassen chip anyway... */
	if (lp->mode == 1) {	/* busmaster */
		dma_addr_t page_baddr;
L
Lucas De Marchi 已提交
719
		/* Get physically continuous memory for TX & RX PDLs    */
L
Linus Torvalds 已提交
720 721 722
		/* Conversion to new PCI API :
		 * Pages are always aligned and zeroed, no need to it ourself.
		 * Doc says should be OK for EISA bus as well - Jean II */
723 724
		lp->page_vaddr_algn = pci_alloc_consistent(lp->pci_dev, MAX_RINGSIZE, &page_baddr);
		if (!lp->page_vaddr_algn) {
L
Linus Torvalds 已提交
725
			err = -ENOMEM;
726
			goto out_mem_ptr;
L
Linus Torvalds 已提交
727 728 729 730 731 732 733 734 735 736 737 738
		}
		lp->whatever_offset = ((u_long) page_baddr) - ((u_long) lp->page_vaddr_algn);

#ifdef HP100_DEBUG_BM
		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);
#endif
		lp->rxrcommit = lp->txrcommit = 0;
		lp->rxrhead = lp->rxrtail = &(lp->rxring[0]);
		lp->txrhead = lp->txrtail = &(lp->txring[0]);
	}

	/* Initialise the card. */
739
	/* (I'm not really sure if it's a good idea to do this during probing, but
L
Linus Torvalds 已提交
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
	 * like this it's assured that the lan connection type can be sensed
	 * correctly)
	 */
	hp100_hwinit(dev);

	/* Try to find out which kind of LAN the card is connected to. */
	lp->lan_type = hp100_sense_lan(dev);

	/* Print out a message what about what we think we have probed. */
	printk("hp100: at 0x%x, IRQ %d, ", ioaddr, dev->irq);
	switch (bus) {
	case HP100_BUS_EISA:
		printk("EISA");
		break;
	case HP100_BUS_PCI:
		printk("PCI");
		break;
	default:
		printk("ISA");
		break;
	}
	printk(" bus, %dk SRAM (rx/tx %d%%).\n", lp->memory_size >> 10, lp->rx_ratio);

	if (lp->mode == 2) {	/* memory mapped */
		printk("hp100: Memory area at 0x%lx-0x%lx", mem_ptr_phys,
				(mem_ptr_phys + (mem_ptr_phys > 0x100000 ? (u_long) lp->memory_size : 16 * 1024)) - 1);
		if (mem_ptr_virt)
			printk(" (virtual base %p)", mem_ptr_virt);
		printk(".\n");

		/* Set for info when doing ifconfig */
		dev->mem_start = mem_ptr_phys;
		dev->mem_end = mem_ptr_phys + lp->memory_size;
	}

	printk("hp100: ");
	if (lp->lan_type != HP100_LAN_ERR)
		printk("Adapter is attached to ");
	switch (lp->lan_type) {
	case HP100_LAN_100:
		printk("100Mb/s Voice Grade AnyLAN network.\n");
		break;
	case HP100_LAN_10:
		printk("10Mb/s network (10baseT).\n");
		break;
	case HP100_LAN_COAX:
		printk("10Mb/s network (coax).\n");
		break;
	default:
		printk("Warning! Link down.\n");
	}

792 793 794 795
	err = register_netdev(dev);
	if (err)
		goto out3;

L
Linus Torvalds 已提交
796
	return 0;
797 798
out3:
	if (local_mode == 1)
799 800
		pci_free_consistent(lp->pci_dev, MAX_RINGSIZE + 0x0f,
				    lp->page_vaddr_algn,
801
				    virt_to_whatever(dev, lp->page_vaddr_algn));
802
out_mem_ptr:
803 804
	if (mem_ptr_virt)
		iounmap(mem_ptr_virt);
L
Linus Torvalds 已提交
805 806 807
out2:
	release_region(ioaddr, HP100_REGION_SIZE);
out1:
808
	return err;
L
Linus Torvalds 已提交
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
}

/* This procedure puts the card into a stable init state */
static void hp100_hwinit(struct net_device *dev)
{
	int ioaddr = dev->base_addr;
	struct hp100_private *lp = netdev_priv(dev);

#ifdef HP100_DEBUG_B
	hp100_outw(0x4202, TRACE);
	printk("hp100: %s: hwinit\n", dev->name);
#endif

	/* Initialise the card. -------------------------------------------- */

	/* Clear all pending Ints and disable Ints */
	hp100_page(PERFORMANCE);
	hp100_outw(0xfefe, IRQ_MASK);	/* mask off all ints */
	hp100_outw(0xffff, IRQ_STATUS);	/* clear all pending ints */

	hp100_outw(HP100_INT_EN | HP100_RESET_LB, OPTION_LSW);
	hp100_outw(HP100_TRI_INT | HP100_SET_HB, OPTION_LSW);

	if (lp->mode == 1) {
		hp100_BM_shutdown(dev);	/* disables BM, puts cascade in reset */
		wait();
	} else {
		hp100_outw(HP100_INT_EN | HP100_RESET_LB, OPTION_LSW);
		hp100_cascade_reset(dev, 1);
		hp100_page(MAC_CTRL);
		hp100_andb(~(HP100_RX_EN | HP100_TX_EN), MAC_CFG_1);
	}

	/* Initiate EEPROM reload */
	hp100_load_eeprom(dev, 0);

	wait();

	/* Go into reset again. */
	hp100_cascade_reset(dev, 1);

	/* Set Option Registers to a safe state  */
	hp100_outw(HP100_DEBUG_EN |
		   HP100_RX_HDR |
		   HP100_EE_EN |
		   HP100_BM_WRITE |
		   HP100_BM_READ | HP100_RESET_HB |
		   HP100_FAKE_INT |
		   HP100_INT_EN |
		   HP100_MEM_EN |
		   HP100_IO_EN | HP100_RESET_LB, OPTION_LSW);

	hp100_outw(HP100_TRI_INT |
		   HP100_MMAP_DIS | HP100_SET_HB, OPTION_LSW);

	hp100_outb(HP100_PRIORITY_TX |
		   HP100_ADV_NXT_PKT |
		   HP100_TX_CMD | HP100_RESET_LB, OPTION_MSW);

	/* TODO: Configure MMU for Ram Test. */
	/* TODO: Ram Test. */

	/* Re-check if adapter is still at same i/o location      */
	/* (If the base i/o in eeprom has been changed but the    */
	/* registers had not been changed, a reload of the eeprom */
	/* would move the adapter to the address stored in eeprom */

	/* TODO: Code to implement. */

	/* Until here it was code from HWdiscover procedure. */
	/* Next comes code from mmuinit procedure of SCO BM driver which is
	 * called from HWconfigure in the SCO driver.  */

882
	/* Initialise MMU, eventually switch on Busmaster Mode, initialise
L
Linus Torvalds 已提交
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
	 * multicast filter...
	 */
	hp100_mmuinit(dev);

	/* We don't turn the interrupts on here - this is done by start_interface. */
	wait();			/* TODO: Do we really need this? */

	/* Enable Hardware (e.g. unreset) */
	hp100_cascade_reset(dev, 0);

	/* ------- initialisation complete ----------- */

	/* Finally try to log in the Hub if there may be a VG connection. */
	if ((lp->lan_type == HP100_LAN_100) || (lp->lan_type == HP100_LAN_ERR))
		hp100_login_to_vg_hub(dev, 0);	/* relogin */

}

901 902

/*
L
Linus Torvalds 已提交
903
 * mmuinit - Reinitialise Cascade MMU and MAC settings.
904
 * Note: Must already be in reset and leaves card in reset.
L
Linus Torvalds 已提交
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
 */
static void hp100_mmuinit(struct net_device *dev)
{
	int ioaddr = dev->base_addr;
	struct hp100_private *lp = netdev_priv(dev);
	int i;

#ifdef HP100_DEBUG_B
	hp100_outw(0x4203, TRACE);
	printk("hp100: %s: mmuinit\n", dev->name);
#endif

#ifdef HP100_DEBUG
	if (0 != (hp100_inw(OPTION_LSW) & HP100_HW_RST)) {
		printk("hp100: %s: Not in reset when entering mmuinit. Fix me.\n", dev->name);
		return;
	}
#endif

	/* Make sure IRQs are masked off and ack'ed. */
	hp100_page(PERFORMANCE);
	hp100_outw(0xfefe, IRQ_MASK);	/* mask off all ints */
	hp100_outw(0xffff, IRQ_STATUS);	/* ack IRQ */

	/*
930
	 * Enable Hardware
L
Linus Torvalds 已提交
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
	 * - Clear Debug En, Rx Hdr Pipe, EE En, I/O En, Fake Int and Intr En
	 * - Set Tri-State Int, Bus Master Rd/Wr, and Mem Map Disable
	 * - Clear Priority, Advance Pkt and Xmit Cmd
	 */

	hp100_outw(HP100_DEBUG_EN |
		   HP100_RX_HDR |
		   HP100_EE_EN | HP100_RESET_HB |
		   HP100_IO_EN |
		   HP100_FAKE_INT |
		   HP100_INT_EN | HP100_RESET_LB, OPTION_LSW);

	hp100_outw(HP100_TRI_INT | HP100_SET_HB, OPTION_LSW);

	if (lp->mode == 1) {	/* busmaster */
		hp100_outw(HP100_BM_WRITE |
			   HP100_BM_READ |
			   HP100_MMAP_DIS | HP100_SET_HB, OPTION_LSW);
	} else if (lp->mode == 2) {	/* memory mapped */
		hp100_outw(HP100_BM_WRITE |
			   HP100_BM_READ | HP100_RESET_HB, OPTION_LSW);
		hp100_outw(HP100_MMAP_DIS | HP100_RESET_HB, OPTION_LSW);
		hp100_outw(HP100_MEM_EN | HP100_SET_LB, OPTION_LSW);
		hp100_outw(HP100_IO_EN | HP100_SET_LB, OPTION_LSW);
	} else if (lp->mode == 3) {	/* i/o mapped mode */
		hp100_outw(HP100_MMAP_DIS | HP100_SET_HB |
			   HP100_IO_EN | HP100_SET_LB, OPTION_LSW);
	}

	hp100_page(HW_MAP);
	hp100_outb(0, EARLYRXCFG);
	hp100_outw(0, EARLYTXCFG);

	/*
	 * Enable Bus Master mode
	 */
	if (lp->mode == 1) {	/* busmaster */
		/* Experimental: Set some PCI configuration bits */
		hp100_page(HW_MAP);
		hp100_andb(~HP100_PDL_USE3, MODECTRL1);	/* BM engine read maximum */
		hp100_andb(~HP100_TX_DUALQ, MODECTRL1);	/* No Queue for Priority TX */

		/* PCI Bus failures should result in a Misc. Interrupt */
		hp100_orb(HP100_EN_BUS_FAIL, MODECTRL2);

		hp100_outw(HP100_BM_READ | HP100_BM_WRITE | HP100_SET_HB, OPTION_LSW);
		hp100_page(HW_MAP);
		/* Use Burst Mode and switch on PAGE_CK */
		hp100_orb(HP100_BM_BURST_RD | HP100_BM_BURST_WR, BM);
		if ((lp->chip == HP100_CHIPID_RAINIER) || (lp->chip == HP100_CHIPID_SHASTA))
			hp100_orb(HP100_BM_PAGE_CK, BM);
		hp100_orb(HP100_BM_MASTER, BM);
	} else {		/* not busmaster */

		hp100_page(HW_MAP);
		hp100_andb(~HP100_BM_MASTER, BM);
	}

	/*
	 * Divide card memory into regions for Rx, Tx and, if non-ETR chip, PDLs
	 */
	hp100_page(MMU_CFG);
	if (lp->mode == 1) {	/* only needed for Busmaster */
		int xmit_stop, recv_stop;

996 997
		if ((lp->chip == HP100_CHIPID_RAINIER) ||
		    (lp->chip == HP100_CHIPID_SHASTA)) {
L
Linus Torvalds 已提交
998 999 1000 1001 1002 1003 1004
			int pdl_stop;

			/*
			 * Each pdl is 508 bytes long. (63 frags * 4 bytes for address and
			 * 4 bytes for header). We will leave NUM_RXPDLS * 508 (rounded
			 * to the next higher 1k boundary) bytes for the rx-pdl's
			 * Note: For non-etr chips the transmit stop register must be
1005
			 * programmed on a 1k boundary, i.e. bits 9:0 must be zero.
L
Linus Torvalds 已提交
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
			 */
			pdl_stop = lp->memory_size;
			xmit_stop = (pdl_stop - 508 * (MAX_RX_PDL) - 16) & ~(0x03ff);
			recv_stop = (xmit_stop * (lp->rx_ratio) / 100) & ~(0x03ff);
			hp100_outw((pdl_stop >> 4) - 1, PDL_MEM_STOP);
#ifdef HP100_DEBUG_BM
			printk("hp100: %s: PDL_STOP = 0x%x\n", dev->name, pdl_stop);
#endif
		} else {
			/* ETR chip (Lassen) in busmaster mode */
			xmit_stop = (lp->memory_size) - 1;
			recv_stop = ((lp->memory_size * lp->rx_ratio) / 100) & ~(0x03ff);
		}

		hp100_outw(xmit_stop >> 4, TX_MEM_STOP);
		hp100_outw(recv_stop >> 4, RX_MEM_STOP);
#ifdef HP100_DEBUG_BM
		printk("hp100: %s: TX_STOP  = 0x%x\n", dev->name, xmit_stop >> 4);
		printk("hp100: %s: RX_STOP  = 0x%x\n", dev->name, recv_stop >> 4);
#endif
	} else {
		/* Slave modes (memory mapped and programmed io)  */
		hp100_outw((((lp->memory_size * lp->rx_ratio) / 100) >> 4), RX_MEM_STOP);
		hp100_outw(((lp->memory_size - 1) >> 4), TX_MEM_STOP);
#ifdef HP100_DEBUG
		printk("hp100: %s: TX_MEM_STOP: 0x%x\n", dev->name, hp100_inw(TX_MEM_STOP));
		printk("hp100: %s: RX_MEM_STOP: 0x%x\n", dev->name, hp100_inw(RX_MEM_STOP));
#endif
	}

	/* Write MAC address into page 1 */
	hp100_page(MAC_ADDRESS);
	for (i = 0; i < 6; i++)
		hp100_outb(dev->dev_addr[i], MAC_ADDR + i);

	/* Zero the multicast hash registers */
	for (i = 0; i < 8; i++)
		hp100_outb(0x0, HASH_BYTE0 + i);

	/* Set up MAC defaults */
	hp100_page(MAC_CTRL);

	/* Go to LAN Page and zero all filter bits */
	/* Zero accept error, accept multicast, accept broadcast and accept */
	/* all directed packet bits */
	hp100_andb(~(HP100_RX_EN |
		     HP100_TX_EN |
		     HP100_ACC_ERRORED |
		     HP100_ACC_MC |
		     HP100_ACC_BC | HP100_ACC_PHY), MAC_CFG_1);

	hp100_outb(0x00, MAC_CFG_2);

	/* Zero the frame format bit. This works around a training bug in the */
	/* new hubs. */
	hp100_outb(0x00, VG_LAN_CFG_2);	/* (use 802.3) */

	if (lp->priority_tx)
		hp100_outb(HP100_PRIORITY_TX | HP100_SET_LB, OPTION_MSW);
	else
		hp100_outb(HP100_PRIORITY_TX | HP100_RESET_LB, OPTION_MSW);

	hp100_outb(HP100_ADV_NXT_PKT |
		   HP100_TX_CMD | HP100_RESET_LB, OPTION_MSW);

	/* If busmaster, initialize the PDLs */
	if (lp->mode == 1)
		hp100_init_pdls(dev);

1075
	/* Go to performance page and initialize isr and imr registers */
L
Linus Torvalds 已提交
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
	hp100_page(PERFORMANCE);
	hp100_outw(0xfefe, IRQ_MASK);	/* mask off all ints */
	hp100_outw(0xffff, IRQ_STATUS);	/* ack IRQ */
}

/*
 *  open/close functions
 */

static int hp100_open(struct net_device *dev)
{
	struct hp100_private *lp = netdev_priv(dev);
#ifdef HP100_DEBUG_B
	int ioaddr = dev->base_addr;
#endif

#ifdef HP100_DEBUG_B
	hp100_outw(0x4204, TRACE);
	printk("hp100: %s: open\n", dev->name);
#endif

	/* New: if bus is PCI or EISA, interrupts might be shared interrupts */
	if (request_irq(dev->irq, hp100_interrupt,
			lp->bus == HP100_BUS_PCI || lp->bus ==
1100
			HP100_BUS_EISA ? IRQF_SHARED : 0,
1101
			dev->name, dev)) {
L
Linus Torvalds 已提交
1102 1103 1104 1105
		printk("hp100: %s: unable to get IRQ %d\n", dev->name, dev->irq);
		return -EAGAIN;
	}

E
Eric Dumazet 已提交
1106
	dev->trans_start = jiffies; /* prevent tx timeout */
L
Linus Torvalds 已提交
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
	netif_start_queue(dev);

	lp->lan_type = hp100_sense_lan(dev);
	lp->mac1_mode = HP100_MAC1MODE3;
	lp->mac2_mode = HP100_MAC2MODE3;
	memset(&lp->hash_bytes, 0x00, 8);

	hp100_stop_interface(dev);

	hp100_hwinit(dev);

	hp100_start_interface(dev);	/* sets mac modes, enables interrupts */

	return 0;
}

/* The close function is called when the interface is to be brought down */
static int hp100_close(struct net_device *dev)
{
	int ioaddr = dev->base_addr;
	struct hp100_private *lp = netdev_priv(dev);

#ifdef HP100_DEBUG_B
	hp100_outw(0x4205, TRACE);
	printk("hp100: %s: close\n", dev->name);
#endif

	hp100_page(PERFORMANCE);
	hp100_outw(0xfefe, IRQ_MASK);	/* mask off all IRQs */

	hp100_stop_interface(dev);

	if (lp->lan_type == HP100_LAN_100)
		lp->hub_status = hp100_login_to_vg_hub(dev, 0);

	netif_stop_queue(dev);

	free_irq(dev->irq, dev);

#ifdef HP100_DEBUG
	printk("hp100: %s: close LSW = 0x%x\n", dev->name,
	       hp100_inw(OPTION_LSW));
#endif

	return 0;
}
1153

L
Linus Torvalds 已提交
1154 1155

/*
1156
 * Configure the PDL Rx rings and LAN
L
Linus Torvalds 已提交
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
 */
static void hp100_init_pdls(struct net_device *dev)
{
	struct hp100_private *lp = netdev_priv(dev);
	hp100_ring_t *ringptr;
	u_int *pageptr;		/* Warning : increment by 4 - Jean II */
	int i;

#ifdef HP100_DEBUG_B
	int ioaddr = dev->base_addr;
#endif

#ifdef HP100_DEBUG_B
	hp100_outw(0x4206, TRACE);
	printk("hp100: %s: init pdls\n", dev->name);
#endif

A
Al Viro 已提交
1174
	if (!lp->page_vaddr_algn)
L
Linus Torvalds 已提交
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
		printk("hp100: %s: Warning: lp->page_vaddr_algn not initialised!\n", dev->name);
	else {
		/* pageptr shall point into the DMA accessible memory region  */
		/* we use this pointer to status the upper limit of allocated */
		/* memory in the allocated page. */
		/* note: align the pointers to the pci cache line size */
		memset(lp->page_vaddr_algn, 0, MAX_RINGSIZE);	/* Zero  Rx/Tx ring page */
		pageptr = lp->page_vaddr_algn;

		lp->rxrcommit = 0;
		ringptr = lp->rxrhead = lp->rxrtail = &(lp->rxring[0]);

		/* Initialise Rx Ring */
		for (i = MAX_RX_PDL - 1; i >= 0; i--) {
			lp->rxring[i].next = ringptr;
			ringptr = &(lp->rxring[i]);
			pageptr += hp100_init_rxpdl(dev, ringptr, pageptr);
		}

		/* Initialise Tx Ring */
		lp->txrcommit = 0;
		ringptr = lp->txrhead = lp->txrtail = &(lp->txring[0]);
		for (i = MAX_TX_PDL - 1; i >= 0; i--) {
			lp->txring[i].next = ringptr;
			ringptr = &(lp->txring[i]);
			pageptr += hp100_init_txpdl(dev, ringptr, pageptr);
		}
	}
}
1204

L
Linus Torvalds 已提交
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219

/* These functions "format" the entries in the pdl structure   */
/* They return how much memory the fragments need.            */
static int hp100_init_rxpdl(struct net_device *dev,
			    register hp100_ring_t * ringptr,
			    register u32 * pdlptr)
{
	/* pdlptr is starting address for this pdl */

	if (0 != (((unsigned long) pdlptr) & 0xf))
		printk("hp100: %s: Init rxpdl: Unaligned pdlptr 0x%lx.\n",
		       dev->name, (unsigned long) pdlptr);

	ringptr->pdl = pdlptr + 1;
	ringptr->pdl_paddr = virt_to_whatever(dev, pdlptr + 1);
1220
	ringptr->skb = NULL;
L
Linus Torvalds 已提交
1221

1222
	/*
L
Linus Torvalds 已提交
1223 1224
	 * Write address and length of first PDL Fragment (which is used for
	 * storing the RX-Header
1225
	 * We use the 4 bytes _before_ the PDH in the pdl memory area to
L
Linus Torvalds 已提交
1226 1227 1228 1229 1230 1231 1232
	 * store this information. (PDH is at offset 0x04)
	 */
	/* Note that pdlptr+1 and not pdlptr is the pointer to the PDH */

	*(pdlptr + 2) = (u_int) virt_to_whatever(dev, pdlptr);	/* Address Frag 1 */
	*(pdlptr + 3) = 4;	/* Length  Frag 1 */

1233
	return roundup(MAX_RX_FRAG * 2 + 2, 4);
L
Linus Torvalds 已提交
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
}


static int hp100_init_txpdl(struct net_device *dev,
			    register hp100_ring_t * ringptr,
			    register u32 * pdlptr)
{
	if (0 != (((unsigned long) pdlptr) & 0xf))
		printk("hp100: %s: Init txpdl: Unaligned pdlptr 0x%lx.\n", dev->name, (unsigned long) pdlptr);

	ringptr->pdl = pdlptr;	/* +1; */
	ringptr->pdl_paddr = virt_to_whatever(dev, pdlptr);	/* +1 */
1246
	ringptr->skb = NULL;
L
Linus Torvalds 已提交
1247

1248
	return roundup(MAX_TX_FRAG * 2 + 2, 4);
L
Linus Torvalds 已提交
1249 1250 1251
}

/*
1252
 * hp100_build_rx_pdl allocates an skb_buff of maximum size plus two bytes
L
Linus Torvalds 已提交
1253
 * for possible odd word alignment rounding up to next dword and set PDL
1254
 * address for fragment#2
L
Linus Torvalds 已提交
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
 * Returns: 0 if unable to allocate skb_buff
 *          1 if successful
 */
static int hp100_build_rx_pdl(hp100_ring_t * ringptr,
			      struct net_device *dev)
{
#ifdef HP100_DEBUG_B
	int ioaddr = dev->base_addr;
#endif
#ifdef HP100_DEBUG_BM
	u_int *p;
#endif

#ifdef HP100_DEBUG_B
	hp100_outw(0x4207, TRACE);
	printk("hp100: %s: build rx pdl\n", dev->name);
#endif

	/* Allocate skb buffer of maximum size */
1274
	/* Note: This depends on the alloc_skb functions allocating more
L
Linus Torvalds 已提交
1275 1276
	 * space than requested, i.e. aligning to 16bytes */

1277
	ringptr->skb = netdev_alloc_skb(dev, roundup(MAX_ETHER_SIZE + 2, 4));
L
Linus Torvalds 已提交
1278 1279

	if (NULL != ringptr->skb) {
1280
		/*
L
Linus Torvalds 已提交
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
		 * Reserve 2 bytes at the head of the buffer to land the IP header
		 * on a long word boundary (According to the Network Driver section
		 * in the Linux KHG, this should help to increase performance.)
		 */
		skb_reserve(ringptr->skb, 2);

		ringptr->skb->data = (u_char *) skb_put(ringptr->skb, MAX_ETHER_SIZE);

		/* ringptr->pdl points to the beginning of the PDL, i.e. the PDH */
		/* Note: 1st Fragment is used for the 4 byte packet status
1291
		 * (receive header). Its PDL entries are set up by init_rxpdl. So
L
Linus Torvalds 已提交
1292
		 * here we only have to set up the PDL fragment entries for the data
1293 1294
		 * part. Those 4 bytes will be stored in the DMA memory region
		 * directly before the PDL.
L
Linus Torvalds 已提交
1295 1296 1297 1298
		 */
#ifdef HP100_DEBUG_BM
		printk("hp100: %s: build_rx_pdl: PDH@0x%x, skb->data (len %d) at 0x%x\n",
				     dev->name, (u_int) ringptr->pdl,
1299
				     roundup(MAX_ETHER_SIZE + 2, 4),
L
Linus Torvalds 已提交
1300 1301 1302 1303 1304 1305
				     (unsigned int) ringptr->skb->data);
#endif

		/* Conversion to new PCI API : map skbuf data to PCI bus.
		 * Doc says it's OK for EISA as well - Jean II */
		ringptr->pdl[0] = 0x00020000;	/* Write PDH */
1306
		ringptr->pdl[3] = pdl_map_data(netdev_priv(dev),
L
Linus Torvalds 已提交
1307 1308 1309 1310 1311 1312 1313
					       ringptr->skb->data);
		ringptr->pdl[4] = MAX_ETHER_SIZE;	/* Length of Data */

#ifdef HP100_DEBUG_BM
		for (p = (ringptr->pdl); p < (ringptr->pdl + 5); p++)
			printk("hp100: %s: Adr 0x%.8x = 0x%.8x\n", dev->name, (u_int) p, (u_int) * p);
#endif
1314
		return 1;
L
Linus Torvalds 已提交
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
	}
	/* else: */
	/* alloc_skb failed (no memory) -> still can receive the header
	 * fragment into PDL memory. make PDL safe by clearing msgptr and
	 * making the PDL only 1 fragment (i.e. the 4 byte packet status)
	 */
#ifdef HP100_DEBUG_BM
	printk("hp100: %s: build_rx_pdl: PDH@0x%x, No space for skb.\n", dev->name, (u_int) ringptr->pdl);
#endif

	ringptr->pdl[0] = 0x00010000;	/* PDH: Count=1 Fragment */

1327
	return 0;
L
Linus Torvalds 已提交
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
}

/*
 *  hp100_rxfill - attempt to fill the Rx Ring will empty skb's
 *
 * Makes assumption that skb's are always contiguous memory areas and
 * therefore PDLs contain only 2 physical fragments.
 * -  While the number of Rx PDLs with buffers is less than maximum
 *      a.  Get a maximum packet size skb
 *      b.  Put the physical address of the buffer into the PDL.
 *      c.  Output physical address of PDL to adapter.
 */
static void hp100_rxfill(struct net_device *dev)
{
	int ioaddr = dev->base_addr;

	struct hp100_private *lp = netdev_priv(dev);
	hp100_ring_t *ringptr;

#ifdef HP100_DEBUG_B
	hp100_outw(0x4208, TRACE);
	printk("hp100: %s: rxfill\n", dev->name);
#endif

	hp100_page(PERFORMANCE);

	while (lp->rxrcommit < MAX_RX_PDL) {
		/*
		   ** Attempt to get a buffer and build a Rx PDL.
		 */
		ringptr = lp->rxrtail;
		if (0 == hp100_build_rx_pdl(ringptr, dev)) {
			return;	/* None available, return */
		}

		/* Hand this PDL over to the card */
		/* Note: This needs performance page selected! */
#ifdef HP100_DEBUG_BM
		printk("hp100: %s: rxfill: Hand to card: pdl #%d @0x%x phys:0x%x, buffer: 0x%x\n",
				     dev->name, lp->rxrcommit, (u_int) ringptr->pdl,
				     (u_int) ringptr->pdl_paddr, (u_int) ringptr->pdl[3]);
#endif

		hp100_outl((u32) ringptr->pdl_paddr, RX_PDA);

		lp->rxrcommit += 1;
		lp->rxrtail = ringptr->next;
	}
}

/*
 * BM_shutdown - shutdown bus mastering and leave chip in reset state
 */

static void hp100_BM_shutdown(struct net_device *dev)
{
	int ioaddr = dev->base_addr;
	struct hp100_private *lp = netdev_priv(dev);
	unsigned long time;

#ifdef HP100_DEBUG_B
	hp100_outw(0x4209, TRACE);
	printk("hp100: %s: bm shutdown\n", dev->name);
#endif

	hp100_page(PERFORMANCE);
	hp100_outw(0xfefe, IRQ_MASK);	/* mask off all ints */
	hp100_outw(0xffff, IRQ_STATUS);	/* Ack all ints */

	/* Ensure Interrupts are off */
	hp100_outw(HP100_INT_EN | HP100_RESET_LB, OPTION_LSW);

	/* Disable all MAC activity */
	hp100_page(MAC_CTRL);
	hp100_andb(~(HP100_RX_EN | HP100_TX_EN), MAC_CFG_1);	/* stop rx/tx */

	/* If cascade MMU is not already in reset */
	if (0 != (hp100_inw(OPTION_LSW) & HP100_HW_RST)) {
		/* Wait 1.3ms (10Mb max packet time) to ensure MAC is idle so
		 * MMU pointers will not be reset out from underneath
		 */
		hp100_page(MAC_CTRL);
		for (time = 0; time < 5000; time++) {
			if ((hp100_inb(MAC_CFG_1) & (HP100_TX_IDLE | HP100_RX_IDLE)) == (HP100_TX_IDLE | HP100_RX_IDLE))
				break;
		}

		/* Shutdown algorithm depends on the generation of Cascade */
		if (lp->chip == HP100_CHIPID_LASSEN) {	/* ETR shutdown/reset */
			/* Disable Busmaster mode and wait for bit to go to zero. */
			hp100_page(HW_MAP);
			hp100_andb(~HP100_BM_MASTER, BM);
			/* 100 ms timeout */
			for (time = 0; time < 32000; time++) {
				if (0 == (hp100_inb(BM) & HP100_BM_MASTER))
					break;
			}
		} else {	/* Shasta or Rainier Shutdown/Reset */
			/* To ensure all bus master inloading activity has ceased,
1427
			 * wait for no Rx PDAs or no Rx packets on card.
L
Linus Torvalds 已提交
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
			 */
			hp100_page(PERFORMANCE);
			/* 100 ms timeout */
			for (time = 0; time < 10000; time++) {
				/* RX_PDL: PDLs not executed. */
				/* RX_PKT_CNT: RX'd packets on card. */
				if ((hp100_inb(RX_PDL) == 0) && (hp100_inb(RX_PKT_CNT) == 0))
					break;
			}

			if (time >= 10000)
				printk("hp100: %s: BM shutdown error.\n", dev->name);

			/* To ensure all bus master outloading activity has ceased,
			 * wait until the Tx PDA count goes to zero or no more Tx space
1443
			 * available in the Tx region of the card.
L
Linus Torvalds 已提交
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
			 */
			/* 100 ms timeout */
			for (time = 0; time < 10000; time++) {
				if ((0 == hp100_inb(TX_PKT_CNT)) &&
				    (0 != (hp100_inb(TX_MEM_FREE) & HP100_AUTO_COMPARE)))
					break;
			}

			/* Disable Busmaster mode */
			hp100_page(HW_MAP);
			hp100_andb(~HP100_BM_MASTER, BM);
		}	/* end of shutdown procedure for non-etr parts */

		hp100_cascade_reset(dev, 1);
	}
	hp100_page(PERFORMANCE);
	/* hp100_outw( HP100_BM_READ | HP100_BM_WRITE | HP100_RESET_HB, OPTION_LSW ); */
	/* Busmaster mode should be shut down now. */
}

static int hp100_check_lan(struct net_device *dev)
{
	struct hp100_private *lp = netdev_priv(dev);

	if (lp->lan_type < 0) {	/* no LAN type detected yet? */
		hp100_stop_interface(dev);
		if ((lp->lan_type = hp100_sense_lan(dev)) < 0) {
			printk("hp100: %s: no connection found - check wire\n", dev->name);
			hp100_start_interface(dev);	/* 10Mb/s RX packets maybe handled */
			return -EIO;
		}
		if (lp->lan_type == HP100_LAN_100)
			lp->hub_status = hp100_login_to_vg_hub(dev, 0);	/* relogin */
		hp100_start_interface(dev);
	}
	return 0;
}

1482
/*
L
Linus Torvalds 已提交
1483 1484 1485 1486
 *  transmit functions
 */

/* tx function for busmaster mode */
1487 1488
static netdev_tx_t hp100_start_xmit_bm(struct sk_buff *skb,
				       struct net_device *dev)
L
Linus Torvalds 已提交
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500
{
	unsigned long flags;
	int i, ok_flag;
	int ioaddr = dev->base_addr;
	struct hp100_private *lp = netdev_priv(dev);
	hp100_ring_t *ringptr;

#ifdef HP100_DEBUG_B
	hp100_outw(0x4210, TRACE);
	printk("hp100: %s: start_xmit_bm\n", dev->name);
#endif
	if (skb->len <= 0)
1501
		goto drop;
1502

1503
	if (lp->chip == HP100_CHIPID_SHASTA && skb_padto(skb, ETH_ZLEN))
1504
		return NETDEV_TX_OK;
L
Linus Torvalds 已提交
1505 1506 1507 1508 1509 1510 1511 1512

	/* Get Tx ring tail pointer */
	if (lp->txrtail->next == lp->txrhead) {
		/* No memory. */
#ifdef HP100_DEBUG
		printk("hp100: %s: start_xmit_bm: No TX PDL available.\n", dev->name);
#endif
		/* not waited long enough since last tx? */
E
Eric Dumazet 已提交
1513
		if (time_before(jiffies, dev_trans_start(dev) + HZ))
1514
			goto drop;
L
Linus Torvalds 已提交
1515 1516

		if (hp100_check_lan(dev))
1517
			goto drop;
L
Linus Torvalds 已提交
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549

		if (lp->lan_type == HP100_LAN_100 && lp->hub_status < 0) {
			/* we have a 100Mb/s adapter but it isn't connected to hub */
			printk("hp100: %s: login to 100Mb/s hub retry\n", dev->name);
			hp100_stop_interface(dev);
			lp->hub_status = hp100_login_to_vg_hub(dev, 0);
			hp100_start_interface(dev);
		} else {
			spin_lock_irqsave(&lp->lock, flags);
			hp100_ints_off();	/* Useful ? Jean II */
			i = hp100_sense_lan(dev);
			hp100_ints_on();
			spin_unlock_irqrestore(&lp->lock, flags);
			if (i == HP100_LAN_ERR)
				printk("hp100: %s: link down detected\n", dev->name);
			else if (lp->lan_type != i) {	/* cable change! */
				/* it's very hard - all network settings must be changed!!! */
				printk("hp100: %s: cable change 10Mb/s <-> 100Mb/s detected\n", dev->name);
				lp->lan_type = i;
				hp100_stop_interface(dev);
				if (lp->lan_type == HP100_LAN_100)
					lp->hub_status = hp100_login_to_vg_hub(dev, 0);
				hp100_start_interface(dev);
			} else {
				printk("hp100: %s: interface reset\n", dev->name);
				hp100_stop_interface(dev);
				if (lp->lan_type == HP100_LAN_100)
					lp->hub_status = hp100_login_to_vg_hub(dev, 0);
				hp100_start_interface(dev);
			}
		}

1550
		goto drop;
L
Linus Torvalds 已提交
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
	}

	/*
	 * we have to turn int's off before modifying this, otherwise
	 * a tx_pdl_cleanup could occur at the same time
	 */
	spin_lock_irqsave(&lp->lock, flags);
	ringptr = lp->txrtail;
	lp->txrtail = ringptr->next;

	/* Check whether packet has minimal packet size */
	ok_flag = skb->len >= HP100_MIN_PACKET_SIZE;
	i = ok_flag ? skb->len : HP100_MIN_PACKET_SIZE;

	ringptr->skb = skb;
	ringptr->pdl[0] = ((1 << 16) | i);	/* PDH: 1 Fragment & length */
	if (lp->chip == HP100_CHIPID_SHASTA) {
		/* TODO:Could someone who has the EISA card please check if this works? */
		ringptr->pdl[2] = i;
	} else {		/* Lassen */
		/* In the PDL, don't use the padded size but the real packet size: */
		ringptr->pdl[2] = skb->len;	/* 1st Frag: Length of frag */
	}
	/* Conversion to new PCI API : map skbuf data to PCI bus.
	 * Doc says it's OK for EISA as well - Jean II */
	ringptr->pdl[1] = ((u32) pci_map_single(lp->pci_dev, skb->data, ringptr->pdl[2], PCI_DMA_TODEVICE));	/* 1st Frag: Adr. of data */

	/* Hand this PDL to the card. */
	hp100_outl(ringptr->pdl_paddr, TX_PDA_L);	/* Low Prio. Queue */

	lp->txrcommit++;

1583 1584
	dev->stats.tx_packets++;
	dev->stats.tx_bytes += skb->len;
L
Linus Torvalds 已提交
1585

E
Eric Dumazet 已提交
1586 1587
	spin_unlock_irqrestore(&lp->lock, flags);

1588
	return NETDEV_TX_OK;
1589 1590 1591 1592

drop:
	dev_kfree_skb(skb);
	return NETDEV_TX_OK;
L
Linus Torvalds 已提交
1593
}
1594

L
Linus Torvalds 已提交
1595 1596 1597

/* clean_txring checks if packets have been sent by the card by reading
 * the TX_PDL register from the performance page and comparing it to the
L
Lucas De Marchi 已提交
1598
 * number of committed packets. It then frees the skb's of the packets that
L
Linus Torvalds 已提交
1599 1600
 * obviously have been sent to the network.
 *
1601
 * Needs the PERFORMANCE page selected.
L
Linus Torvalds 已提交
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
 */
static void hp100_clean_txring(struct net_device *dev)
{
	struct hp100_private *lp = netdev_priv(dev);
	int ioaddr = dev->base_addr;
	int donecount;

#ifdef HP100_DEBUG_B
	hp100_outw(0x4211, TRACE);
	printk("hp100: %s: clean txring\n", dev->name);
#endif

	/* How many PDLs have been transmitted? */
	donecount = (lp->txrcommit) - hp100_inb(TX_PDL);

#ifdef HP100_DEBUG
	if (donecount > MAX_TX_PDL)
L
Lucas De Marchi 已提交
1619
		printk("hp100: %s: Warning: More PDLs transmitted than committed to card???\n", dev->name);
L
Linus Torvalds 已提交
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
#endif

	for (; 0 != donecount; donecount--) {
#ifdef HP100_DEBUG_BM
		printk("hp100: %s: Free skb: data @0x%.8x txrcommit=0x%x TXPDL=0x%x, done=0x%x\n",
				dev->name, (u_int) lp->txrhead->skb->data,
				lp->txrcommit, hp100_inb(TX_PDL), donecount);
#endif
		/* Conversion to new PCI API : NOP */
		pci_unmap_single(lp->pci_dev, (dma_addr_t) lp->txrhead->pdl[1], lp->txrhead->pdl[2], PCI_DMA_TODEVICE);
		dev_kfree_skb_any(lp->txrhead->skb);
1631
		lp->txrhead->skb = NULL;
L
Linus Torvalds 已提交
1632 1633 1634 1635 1636 1637
		lp->txrhead = lp->txrhead->next;
		lp->txrcommit--;
	}
}

/* tx function for slave modes */
1638 1639
static netdev_tx_t hp100_start_xmit(struct sk_buff *skb,
				    struct net_device *dev)
L
Linus Torvalds 已提交
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
{
	unsigned long flags;
	int i, ok_flag;
	int ioaddr = dev->base_addr;
	u_short val;
	struct hp100_private *lp = netdev_priv(dev);

#ifdef HP100_DEBUG_B
	hp100_outw(0x4212, TRACE);
	printk("hp100: %s: start_xmit\n", dev->name);
#endif
	if (skb->len <= 0)
1652
		goto drop;
L
Linus Torvalds 已提交
1653 1654

	if (hp100_check_lan(dev))
1655
		goto drop;
L
Linus Torvalds 已提交
1656 1657 1658 1659 1660 1661 1662 1663

	/* If there is not enough free memory on the card... */
	i = hp100_inl(TX_MEM_FREE) & 0x7fffffff;
	if (!(((i / 2) - 539) > (skb->len + 16) && (hp100_inb(TX_PKT_CNT) < 255))) {
#ifdef HP100_DEBUG
		printk("hp100: %s: start_xmit: tx free mem = 0x%x\n", dev->name, i);
#endif
		/* not waited long enough since last failed tx try? */
E
Eric Dumazet 已提交
1664
		if (time_before(jiffies, dev_trans_start(dev) + HZ)) {
L
Linus Torvalds 已提交
1665 1666 1667 1668
#ifdef HP100_DEBUG
			printk("hp100: %s: trans_start timing problem\n",
			       dev->name);
#endif
1669
			goto drop;
L
Linus Torvalds 已提交
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
		}
		if (lp->lan_type == HP100_LAN_100 && lp->hub_status < 0) {
			/* we have a 100Mb/s adapter but it isn't connected to hub */
			printk("hp100: %s: login to 100Mb/s hub retry\n", dev->name);
			hp100_stop_interface(dev);
			lp->hub_status = hp100_login_to_vg_hub(dev, 0);
			hp100_start_interface(dev);
		} else {
			spin_lock_irqsave(&lp->lock, flags);
			hp100_ints_off();	/* Useful ? Jean II */
			i = hp100_sense_lan(dev);
			hp100_ints_on();
			spin_unlock_irqrestore(&lp->lock, flags);
			if (i == HP100_LAN_ERR)
				printk("hp100: %s: link down detected\n", dev->name);
			else if (lp->lan_type != i) {	/* cable change! */
				/* it's very hard - all network setting must be changed!!! */
				printk("hp100: %s: cable change 10Mb/s <-> 100Mb/s detected\n", dev->name);
				lp->lan_type = i;
				hp100_stop_interface(dev);
				if (lp->lan_type == HP100_LAN_100)
					lp->hub_status = hp100_login_to_vg_hub(dev, 0);
				hp100_start_interface(dev);
			} else {
				printk("hp100: %s: interface reset\n", dev->name);
				hp100_stop_interface(dev);
				if (lp->lan_type == HP100_LAN_100)
					lp->hub_status = hp100_login_to_vg_hub(dev, 0);
				hp100_start_interface(dev);
				mdelay(1);
			}
		}
1702
		goto drop;
L
Linus Torvalds 已提交
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
	}

	for (i = 0; i < 6000 && (hp100_inb(OPTION_MSW) & HP100_TX_CMD); i++) {
#ifdef HP100_DEBUG_TX
		printk("hp100: %s: start_xmit: busy\n", dev->name);
#endif
	}

	spin_lock_irqsave(&lp->lock, flags);
	hp100_ints_off();
	val = hp100_inw(IRQ_STATUS);
	/* Ack / clear the interrupt TX_COMPLETE interrupt - this interrupt is set
	 * when the current packet being transmitted on the wire is completed. */
	hp100_outw(HP100_TX_COMPLETE, IRQ_STATUS);
#ifdef HP100_DEBUG_TX
	printk("hp100: %s: start_xmit: irq_status=0x%.4x, irqmask=0x%.4x, len=%d\n",
			dev->name, val, hp100_inw(IRQ_MASK), (int) skb->len);
#endif

	ok_flag = skb->len >= HP100_MIN_PACKET_SIZE;
	i = ok_flag ? skb->len : HP100_MIN_PACKET_SIZE;

	hp100_outw(i, DATA32);	/* tell card the total packet length */
	hp100_outw(i, FRAGMENT_LEN);	/* and first/only fragment length    */

	if (lp->mode == 2) {	/* memory mapped */
A
Al Viro 已提交
1729 1730 1731 1732
		/* Note: The J2585B needs alignment to 32bits here!  */
		memcpy_toio(lp->mem_ptr_virt, skb->data, (skb->len + 3) & ~3);
		if (!ok_flag)
			memset_io(lp->mem_ptr_virt, 0, HP100_MIN_PACKET_SIZE - skb->len);
L
Linus Torvalds 已提交
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
	} else {		/* programmed i/o */
		outsl(ioaddr + HP100_REG_DATA32, skb->data,
		      (skb->len + 3) >> 2);
		if (!ok_flag)
			for (i = (skb->len + 3) & ~3; i < HP100_MIN_PACKET_SIZE; i += 4)
				hp100_outl(0, DATA32);
	}

	hp100_outb(HP100_TX_CMD | HP100_SET_LB, OPTION_MSW);	/* send packet */

1743 1744
	dev->stats.tx_packets++;
	dev->stats.tx_bytes += skb->len;
L
Linus Torvalds 已提交
1745 1746 1747 1748 1749 1750 1751 1752 1753
	hp100_ints_on();
	spin_unlock_irqrestore(&lp->lock, flags);

	dev_kfree_skb_any(skb);

#ifdef HP100_DEBUG_TX
	printk("hp100: %s: start_xmit: end\n", dev->name);
#endif

1754
	return NETDEV_TX_OK;
1755 1756 1757 1758 1759

drop:
	dev_kfree_skb(skb);
	return NETDEV_TX_OK;

L
Linus Torvalds 已提交
1760
}
1761

L
Linus Torvalds 已提交
1762 1763 1764

/*
 * Receive Function (Non-Busmaster mode)
1765
 * Called when an "Receive Packet" interrupt occurs, i.e. the receive
L
Linus Torvalds 已提交
1766
 * packet counter is non-zero.
L
Lucas De Marchi 已提交
1767
 * For non-busmaster, this function does the whole work of transferring
L
Linus Torvalds 已提交
1768
 * the packet to the host memory and then up to higher layers via skb
1769
 * and netif_rx.
L
Linus Torvalds 已提交
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
 */

static void hp100_rx(struct net_device *dev)
{
	int packets, pkt_len;
	int ioaddr = dev->base_addr;
	struct hp100_private *lp = netdev_priv(dev);
	u_int header;
	struct sk_buff *skb;

#ifdef DEBUG_B
	hp100_outw(0x4213, TRACE);
	printk("hp100: %s: rx\n", dev->name);
#endif

	/* First get indication of received lan packet */
	/* RX_PKT_CND indicates the number of packets which have been fully */
	/* received onto the card but have not been fully transferred of the card */
	packets = hp100_inb(RX_PKT_CNT);
#ifdef HP100_DEBUG_RX
	if (packets > 1)
		printk("hp100: %s: rx: waiting packets = %d\n", dev->name, packets);
#endif

	while (packets-- > 0) {
		/* If ADV_NXT_PKT is still set, we have to wait until the card has */
		/* really advanced to the next packet. */
		for (pkt_len = 0; pkt_len < 6000 && (hp100_inb(OPTION_MSW) & HP100_ADV_NXT_PKT); pkt_len++) {
#ifdef HP100_DEBUG_RX
			printk ("hp100: %s: rx: busy, remaining packets = %d\n", dev->name, packets);
#endif
		}

		/* First we get the header, which contains information about the */
		/* actual length of the received packet. */
		if (lp->mode == 2) {	/* memory mapped mode */
A
Al Viro 已提交
1806
			header = readl(lp->mem_ptr_virt);
L
Linus Torvalds 已提交
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818
		} else		/* programmed i/o */
			header = hp100_inl(DATA32);

		pkt_len = ((header & HP100_PKT_LEN_MASK) + 3) & ~3;

#ifdef HP100_DEBUG_RX
		printk("hp100: %s: rx: new packet - length=%d, errors=0x%x, dest=0x%x\n",
				     dev->name, header & HP100_PKT_LEN_MASK,
				     (header >> 16) & 0xfff8, (header >> 16) & 7);
#endif

		/* Now we allocate the skb and transfer the data into it. */
1819
		skb = netdev_alloc_skb(dev, pkt_len + 2);
L
Linus Torvalds 已提交
1820 1821 1822 1823 1824
		if (skb == NULL) {	/* Not enough memory->drop packet */
#ifdef HP100_DEBUG
			printk("hp100: %s: rx: couldn't allocate a sk_buff of size %d\n",
					     dev->name, pkt_len);
#endif
1825
			dev->stats.rx_dropped++;
L
Linus Torvalds 已提交
1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836
		} else {	/* skb successfully allocated */

			u_char *ptr;

			skb_reserve(skb,2);

			/* ptr to start of the sk_buff data area */
			skb_put(skb, pkt_len);
			ptr = skb->data;

			/* Now transfer the data from the card into that area */
A
Al Viro 已提交
1837 1838 1839
			if (lp->mode == 2)
				memcpy_fromio(ptr, lp->mem_ptr_virt,pkt_len);
			else	/* io mapped */
L
Linus Torvalds 已提交
1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
				insl(ioaddr + HP100_REG_DATA32, ptr, pkt_len >> 2);

			skb->protocol = eth_type_trans(skb, dev);

#ifdef HP100_DEBUG_RX
			printk("hp100: %s: rx: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
					dev->name, ptr[0], ptr[1], ptr[2], ptr[3],
		 			ptr[4], ptr[5], ptr[6], ptr[7], ptr[8],
					ptr[9], ptr[10], ptr[11]);
#endif
			netif_rx(skb);
1851 1852
			dev->stats.rx_packets++;
			dev->stats.rx_bytes += pkt_len;
L
Linus Torvalds 已提交
1853 1854 1855 1856 1857 1858 1859 1860
		}

		/* Indicate the card that we have got the packet */
		hp100_outb(HP100_ADV_NXT_PKT | HP100_SET_LB, OPTION_MSW);

		switch (header & 0x00070000) {
		case (HP100_MULTI_ADDR_HASH << 16):
		case (HP100_MULTI_ADDR_NO_HASH << 16):
1861
			dev->stats.multicast++;
L
Linus Torvalds 已提交
1862 1863 1864 1865 1866 1867 1868 1869
			break;
		}
	}			/* end of while(there are packets) loop */
#ifdef HP100_DEBUG_RX
	printk("hp100_rx: %s: end\n", dev->name);
#endif
}

1870
/*
L
Linus Torvalds 已提交
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890
 * Receive Function for Busmaster Mode
 */
static void hp100_rx_bm(struct net_device *dev)
{
	int ioaddr = dev->base_addr;
	struct hp100_private *lp = netdev_priv(dev);
	hp100_ring_t *ptr;
	u_int header;
	int pkt_len;

#ifdef HP100_DEBUG_B
	hp100_outw(0x4214, TRACE);
	printk("hp100: %s: rx_bm\n", dev->name);
#endif

#ifdef HP100_DEBUG
	if (0 == lp->rxrcommit) {
		printk("hp100: %s: rx_bm called although no PDLs were committed to adapter?\n", dev->name);
		return;
	} else
1891
		/* RX_PKT_CNT states how many PDLs are currently formatted and available to
L
Linus Torvalds 已提交
1892 1893
		 * the cards BM engine */
	if ((hp100_inw(RX_PKT_CNT) & 0x00ff) >= lp->rxrcommit) {
L
Lucas De Marchi 已提交
1894
		printk("hp100: %s: More packets received than committed? RX_PKT_CNT=0x%x, commit=0x%x\n",
L
Linus Torvalds 已提交
1895 1896 1897 1898 1899 1900 1901 1902 1903
				     dev->name, hp100_inw(RX_PKT_CNT) & 0x00ff,
				     lp->rxrcommit);
		return;
	}
#endif

	while ((lp->rxrcommit > hp100_inb(RX_PDL))) {
		/*
		 * The packet was received into the pdl pointed to by lp->rxrhead (
1904
		 * the oldest pdl in the ring
L
Linus Torvalds 已提交
1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
		 */

		/* First we get the header, which contains information about the */
		/* actual length of the received packet. */

		ptr = lp->rxrhead;

		header = *(ptr->pdl - 1);
		pkt_len = (header & HP100_PKT_LEN_MASK);

		/* Conversion to new PCI API : NOP */
		pci_unmap_single(lp->pci_dev, (dma_addr_t) ptr->pdl[3], MAX_ETHER_SIZE, PCI_DMA_FROMDEVICE);

#ifdef HP100_DEBUG_BM
		printk("hp100: %s: rx_bm: header@0x%x=0x%x length=%d, errors=0x%x, dest=0x%x\n",
				dev->name, (u_int) (ptr->pdl - 1), (u_int) header,
				pkt_len, (header >> 16) & 0xfff8, (header >> 16) & 7);
		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",
		   		dev->name, hp100_inb(RX_PDL), hp100_inb(TX_PDL),
				hp100_inb(RX_PKT_CNT), (u_int) * (ptr->pdl),
				(u_int) * (ptr->pdl + 3), (u_int) * (ptr->pdl + 4));
#endif

		if ((pkt_len >= MIN_ETHER_SIZE) &&
		    (pkt_len <= MAX_ETHER_SIZE)) {
			if (ptr->skb == NULL) {
				printk("hp100: %s: rx_bm: skb null\n", dev->name);
				/* can happen if we only allocated room for the pdh due to memory shortage. */
1933
				dev->stats.rx_dropped++;
L
Linus Torvalds 已提交
1934 1935 1936 1937 1938 1939 1940
			} else {
				skb_trim(ptr->skb, pkt_len);	/* Shorten it */
				ptr->skb->protocol =
				    eth_type_trans(ptr->skb, dev);

				netif_rx(ptr->skb);	/* Up and away... */

1941 1942
				dev->stats.rx_packets++;
				dev->stats.rx_bytes += pkt_len;
L
Linus Torvalds 已提交
1943 1944 1945 1946 1947
			}

			switch (header & 0x00070000) {
			case (HP100_MULTI_ADDR_HASH << 16):
			case (HP100_MULTI_ADDR_NO_HASH << 16):
1948
				dev->stats.multicast++;
L
Linus Torvalds 已提交
1949 1950 1951 1952 1953 1954 1955 1956
				break;
			}
		} else {
#ifdef HP100_DEBUG
			printk("hp100: %s: rx_bm: Received bad packet (length=%d)\n", dev->name, pkt_len);
#endif
			if (ptr->skb != NULL)
				dev_kfree_skb_any(ptr->skb);
1957
			dev->stats.rx_errors++;
L
Linus Torvalds 已提交
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994
		}

		lp->rxrhead = lp->rxrhead->next;

		/* Allocate a new rx PDL (so lp->rxrcommit stays the same) */
		if (0 == hp100_build_rx_pdl(lp->rxrtail, dev)) {
			/* No space for skb, header can still be received. */
#ifdef HP100_DEBUG
			printk("hp100: %s: rx_bm: No space for new PDL.\n", dev->name);
#endif
			return;
		} else {	/* successfully allocated new PDL - put it in ringlist at tail. */
			hp100_outl((u32) lp->rxrtail->pdl_paddr, RX_PDA);
			lp->rxrtail = lp->rxrtail->next;
		}

	}
}

/*
 *  statistics
 */
static struct net_device_stats *hp100_get_stats(struct net_device *dev)
{
	unsigned long flags;
	int ioaddr = dev->base_addr;
	struct hp100_private *lp = netdev_priv(dev);

#ifdef HP100_DEBUG_B
	hp100_outw(0x4215, TRACE);
#endif

	spin_lock_irqsave(&lp->lock, flags);
	hp100_ints_off();	/* Useful ? Jean II */
	hp100_update_stats(dev);
	hp100_ints_on();
	spin_unlock_irqrestore(&lp->lock, flags);
1995
	return &(dev->stats);
L
Linus Torvalds 已提交
1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
}

static void hp100_update_stats(struct net_device *dev)
{
	int ioaddr = dev->base_addr;
	u_short val;

#ifdef HP100_DEBUG_B
	hp100_outw(0x4216, TRACE);
	printk("hp100: %s: update-stats\n", dev->name);
#endif

	/* Note: Statistics counters clear when read. */
	hp100_page(MAC_CTRL);
	val = hp100_inw(DROPPED) & 0x0fff;
2011 2012
	dev->stats.rx_errors += val;
	dev->stats.rx_over_errors += val;
L
Linus Torvalds 已提交
2013
	val = hp100_inb(CRC);
2014 2015
	dev->stats.rx_errors += val;
	dev->stats.rx_crc_errors += val;
L
Linus Torvalds 已提交
2016
	val = hp100_inb(ABORT);
2017 2018
	dev->stats.tx_errors += val;
	dev->stats.tx_aborted_errors += val;
L
Linus Torvalds 已提交
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
	hp100_page(PERFORMANCE);
}

static void hp100_misc_interrupt(struct net_device *dev)
{
#ifdef HP100_DEBUG_B
	int ioaddr = dev->base_addr;
#endif

#ifdef HP100_DEBUG_B
	int ioaddr = dev->base_addr;
	hp100_outw(0x4216, TRACE);
	printk("hp100: %s: misc_interrupt\n", dev->name);
#endif

	/* Note: Statistics counters clear when read. */
2035 2036
	dev->stats.rx_errors++;
	dev->stats.tx_errors++;
L
Linus Torvalds 已提交
2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055
}

static void hp100_clear_stats(struct hp100_private *lp, int ioaddr)
{
	unsigned long flags;

#ifdef HP100_DEBUG_B
	hp100_outw(0x4217, TRACE);
	printk("hp100: %s: clear_stats\n", dev->name);
#endif

	spin_lock_irqsave(&lp->lock, flags);
	hp100_page(MAC_CTRL);	/* get all statistics bytes */
	hp100_inw(DROPPED);
	hp100_inb(CRC);
	hp100_inb(ABORT);
	hp100_page(PERFORMANCE);
	spin_unlock_irqrestore(&lp->lock, flags);
}
2056

L
Linus Torvalds 已提交
2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085

/*
 *  multicast setup
 */

/*
 *  Set or clear the multicast filter for this adapter.
 */

static void hp100_set_multicast_list(struct net_device *dev)
{
	unsigned long flags;
	int ioaddr = dev->base_addr;
	struct hp100_private *lp = netdev_priv(dev);

#ifdef HP100_DEBUG_B
	hp100_outw(0x4218, TRACE);
	printk("hp100: %s: set_mc_list\n", dev->name);
#endif

	spin_lock_irqsave(&lp->lock, flags);
	hp100_ints_off();
	hp100_page(MAC_CTRL);
	hp100_andb(~(HP100_RX_EN | HP100_TX_EN), MAC_CFG_1);	/* stop rx/tx */

	if (dev->flags & IFF_PROMISC) {
		lp->mac2_mode = HP100_MAC2MODE6;	/* promiscuous mode = get all good */
		lp->mac1_mode = HP100_MAC1MODE6;	/* packets on the net */
		memset(&lp->hash_bytes, 0xff, 8);
2086
	} else if (!netdev_mc_empty(dev) || (dev->flags & IFF_ALLMULTI)) {
L
Linus Torvalds 已提交
2087 2088 2089 2090 2091 2092 2093
		lp->mac2_mode = HP100_MAC2MODE5;	/* multicast mode = get packets for */
		lp->mac1_mode = HP100_MAC1MODE5;	/* me, broadcasts and all multicasts */
#ifdef HP100_MULTICAST_FILTER	/* doesn't work!!! */
		if (dev->flags & IFF_ALLMULTI) {
			/* set hash filter to receive all multicast packets */
			memset(&lp->hash_bytes, 0xff, 8);
		} else {
2094
			int i, idx;
L
Linus Torvalds 已提交
2095
			u_char *addrs;
2096
			struct netdev_hw_addr *ha;
L
Linus Torvalds 已提交
2097 2098 2099

			memset(&lp->hash_bytes, 0x00, 8);
#ifdef HP100_DEBUG
2100 2101
			printk("hp100: %s: computing hash filter - mc_count = %i\n",
			       dev->name, netdev_mc_count(dev));
L
Linus Torvalds 已提交
2102
#endif
2103 2104
			netdev_for_each_mc_addr(ha, dev) {
				addrs = ha->addr;
L
Linus Torvalds 已提交
2105
#ifdef HP100_DEBUG
2106 2107
				printk("hp100: %s: multicast = %pM, ",
					     dev->name, addrs);
L
Linus Torvalds 已提交
2108
#endif
2109 2110 2111 2112
				for (i = idx = 0; i < 6; i++) {
					idx ^= *addrs++ & 0x3f;
					printk(":%02x:", idx);
				}
L
Linus Torvalds 已提交
2113
#ifdef HP100_DEBUG
2114
				printk("idx = %i\n", idx);
L
Linus Torvalds 已提交
2115
#endif
2116
				lp->hash_bytes[idx >> 3] |= (1 << (idx & 7));
L
Linus Torvalds 已提交
2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194
			}
		}
#else
		memset(&lp->hash_bytes, 0xff, 8);
#endif
	} else {
		lp->mac2_mode = HP100_MAC2MODE3;	/* normal mode = get packets for me */
		lp->mac1_mode = HP100_MAC1MODE3;	/* and broadcasts */
		memset(&lp->hash_bytes, 0x00, 8);
	}

	if (((hp100_inb(MAC_CFG_1) & 0x0f) != lp->mac1_mode) ||
	    (hp100_inb(MAC_CFG_2) != lp->mac2_mode)) {
		int i;

		hp100_outb(lp->mac2_mode, MAC_CFG_2);
		hp100_andb(HP100_MAC1MODEMASK, MAC_CFG_1);	/* clear mac1 mode bits */
		hp100_orb(lp->mac1_mode, MAC_CFG_1);	/* and set the new mode */

		hp100_page(MAC_ADDRESS);
		for (i = 0; i < 8; i++)
			hp100_outb(lp->hash_bytes[i], HASH_BYTE0 + i);
#ifdef HP100_DEBUG
		printk("hp100: %s: mac1 = 0x%x, mac2 = 0x%x, multicast hash = %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
				     dev->name, lp->mac1_mode, lp->mac2_mode,
				     lp->hash_bytes[0], lp->hash_bytes[1],
				     lp->hash_bytes[2], lp->hash_bytes[3],
				     lp->hash_bytes[4], lp->hash_bytes[5],
				     lp->hash_bytes[6], lp->hash_bytes[7]);
#endif

		if (lp->lan_type == HP100_LAN_100) {
#ifdef HP100_DEBUG
			printk("hp100: %s: 100VG MAC settings have changed - relogin.\n", dev->name);
#endif
			lp->hub_status = hp100_login_to_vg_hub(dev, 1);	/* force a relogin to the hub */
		}
	} else {
		int i;
		u_char old_hash_bytes[8];

		hp100_page(MAC_ADDRESS);
		for (i = 0; i < 8; i++)
			old_hash_bytes[i] = hp100_inb(HASH_BYTE0 + i);
		if (memcmp(old_hash_bytes, &lp->hash_bytes, 8)) {
			for (i = 0; i < 8; i++)
				hp100_outb(lp->hash_bytes[i], HASH_BYTE0 + i);
#ifdef HP100_DEBUG
			printk("hp100: %s: multicast hash = %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
					dev->name, lp->hash_bytes[0],
					lp->hash_bytes[1], lp->hash_bytes[2],
					lp->hash_bytes[3], lp->hash_bytes[4],
					lp->hash_bytes[5], lp->hash_bytes[6],
					lp->hash_bytes[7]);
#endif

			if (lp->lan_type == HP100_LAN_100) {
#ifdef HP100_DEBUG
				printk("hp100: %s: 100VG MAC settings have changed - relogin.\n", dev->name);
#endif
				lp->hub_status = hp100_login_to_vg_hub(dev, 1);	/* force a relogin to the hub */
			}
		}
	}

	hp100_page(MAC_CTRL);
	hp100_orb(HP100_RX_EN | HP100_RX_IDLE |	/* enable rx */
		  HP100_TX_EN | HP100_TX_IDLE, MAC_CFG_1);	/* enable tx */

	hp100_page(PERFORMANCE);
	hp100_ints_on();
	spin_unlock_irqrestore(&lp->lock, flags);
}

/*
 *  hardware interrupt handling
 */

2195
static irqreturn_t hp100_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230
{
	struct net_device *dev = (struct net_device *) dev_id;
	struct hp100_private *lp = netdev_priv(dev);

	int ioaddr;
	u_int val;

	if (dev == NULL)
		return IRQ_NONE;
	ioaddr = dev->base_addr;

	spin_lock(&lp->lock);

	hp100_ints_off();

#ifdef HP100_DEBUG_B
	hp100_outw(0x4219, TRACE);
#endif

	/*  hp100_page( PERFORMANCE ); */
	val = hp100_inw(IRQ_STATUS);
#ifdef HP100_DEBUG_IRQ
	printk("hp100: %s: mode=%x,IRQ_STAT=0x%.4x,RXPKTCNT=0x%.2x RXPDL=0x%.2x TXPKTCNT=0x%.2x TXPDL=0x%.2x\n",
			     dev->name, lp->mode, (u_int) val, hp100_inb(RX_PKT_CNT),
			     hp100_inb(RX_PDL), hp100_inb(TX_PKT_CNT), hp100_inb(TX_PDL));
#endif

	if (val == 0) {		/* might be a shared interrupt */
		spin_unlock(&lp->lock);
		hp100_ints_on();
		return IRQ_NONE;
	}
	/* We're only interested in those interrupts we really enabled. */
	/* val &= hp100_inw( IRQ_MASK ); */

2231 2232 2233
	/*
	 * RX_PDL_FILL_COMPL is set whenever a RX_PDL has been executed. A RX_PDL
	 * is considered executed whenever the RX_PDL data structure is no longer
L
Linus Torvalds 已提交
2234 2235 2236 2237 2238 2239 2240 2241 2242 2243
	 * needed.
	 */
	if (val & HP100_RX_PDL_FILL_COMPL) {
		if (lp->mode == 1)
			hp100_rx_bm(dev);
		else {
			printk("hp100: %s: rx_pdl_fill_compl interrupt although not busmaster?\n", dev->name);
		}
	}

2244
	/*
L
Linus Torvalds 已提交
2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
	 * The RX_PACKET interrupt is set, when the receive packet counter is
	 * non zero. We use this interrupt for receiving in slave mode. In
	 * busmaster mode, we use it to make sure we did not miss any rx_pdl_fill
	 * interrupts. If rx_pdl_fill_compl is not set and rx_packet is set, then
	 * we somehow have missed a rx_pdl_fill_compl interrupt.
	 */

	if (val & HP100_RX_PACKET) {	/* Receive Packet Counter is non zero */
		if (lp->mode != 1)	/* non busmaster */
			hp100_rx(dev);
		else if (!(val & HP100_RX_PDL_FILL_COMPL)) {
L
Lucas De Marchi 已提交
2256
			/* Shouldn't happen - maybe we missed a RX_PDL_FILL Interrupt?  */
L
Linus Torvalds 已提交
2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269
			hp100_rx_bm(dev);
		}
	}

	/*
	 * Ack. that we have noticed the interrupt and thereby allow next one.
	 * Note that this is now done after the slave rx function, since first
	 * acknowledging and then setting ADV_NXT_PKT caused an extra interrupt
	 * on the J2573.
	 */
	hp100_outw(val, IRQ_STATUS);

	/*
2270 2271 2272 2273
	 * RX_ERROR is set when a packet is dropped due to no memory resources on
	 * the card or when a RCV_ERR occurs.
	 * TX_ERROR is set when a TX_ABORT condition occurs in the MAC->exists
	 * only in the 802.3 MAC and happens when 16 collisions occur during a TX
L
Linus Torvalds 已提交
2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285
	 */
	if (val & (HP100_TX_ERROR | HP100_RX_ERROR)) {
#ifdef HP100_DEBUG_IRQ
		printk("hp100: %s: TX/RX Error IRQ\n", dev->name);
#endif
		hp100_update_stats(dev);
		if (lp->mode == 1) {
			hp100_rxfill(dev);
			hp100_clean_txring(dev);
		}
	}

2286 2287
	/*
	 * RX_PDA_ZERO is set when the PDA count goes from non-zero to zero.
L
Linus Torvalds 已提交
2288 2289 2290 2291
	 */
	if ((lp->mode == 1) && (val & (HP100_RX_PDA_ZERO)))
		hp100_rxfill(dev);

2292 2293 2294
	/*
	 * HP100_TX_COMPLETE interrupt occurs when packet transmitted on wire
	 * is completed
L
Linus Torvalds 已提交
2295 2296 2297 2298
	 */
	if ((lp->mode == 1) && (val & (HP100_TX_COMPLETE)))
		hp100_clean_txring(dev);

2299
	/*
L
Linus Torvalds 已提交
2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481
	 * MISC_ERROR is set when either the LAN link goes down or a detected
	 * bus error occurs.
	 */
	if (val & HP100_MISC_ERROR) {	/* New for J2585B */
#ifdef HP100_DEBUG_IRQ
		printk
		    ("hp100: %s: Misc. Error Interrupt - Check cabling.\n",
		     dev->name);
#endif
		if (lp->mode == 1) {
			hp100_clean_txring(dev);
			hp100_rxfill(dev);
		}
		hp100_misc_interrupt(dev);
	}

	spin_unlock(&lp->lock);
	hp100_ints_on();
	return IRQ_HANDLED;
}

/*
 *  some misc functions
 */

static void hp100_start_interface(struct net_device *dev)
{
	unsigned long flags;
	int ioaddr = dev->base_addr;
	struct hp100_private *lp = netdev_priv(dev);

#ifdef HP100_DEBUG_B
	hp100_outw(0x4220, TRACE);
	printk("hp100: %s: hp100_start_interface\n", dev->name);
#endif

	spin_lock_irqsave(&lp->lock, flags);

	/* Ensure the adapter does not want to request an interrupt when */
	/* enabling the IRQ line to be active on the bus (i.e. not tri-stated) */
	hp100_page(PERFORMANCE);
	hp100_outw(0xfefe, IRQ_MASK);	/* mask off all ints */
	hp100_outw(0xffff, IRQ_STATUS);	/* ack all IRQs */
	hp100_outw(HP100_FAKE_INT | HP100_INT_EN | HP100_RESET_LB,
		   OPTION_LSW);
	/* Un Tri-state int. TODO: Check if shared interrupts can be realised? */
	hp100_outw(HP100_TRI_INT | HP100_RESET_HB, OPTION_LSW);

	if (lp->mode == 1) {
		/* Make sure BM bit is set... */
		hp100_page(HW_MAP);
		hp100_orb(HP100_BM_MASTER, BM);
		hp100_rxfill(dev);
	} else if (lp->mode == 2) {
		/* Enable memory mapping. Note: Don't do this when busmaster. */
		hp100_outw(HP100_MMAP_DIS | HP100_RESET_HB, OPTION_LSW);
	}

	hp100_page(PERFORMANCE);
	hp100_outw(0xfefe, IRQ_MASK);	/* mask off all ints */
	hp100_outw(0xffff, IRQ_STATUS);	/* ack IRQ */

	/* enable a few interrupts: */
	if (lp->mode == 1) {	/* busmaster mode */
		hp100_outw(HP100_RX_PDL_FILL_COMPL |
			   HP100_RX_PDA_ZERO | HP100_RX_ERROR |
			   /* HP100_RX_PACKET    | */
			   /* HP100_RX_EARLY_INT |  */ HP100_SET_HB |
			   /* HP100_TX_PDA_ZERO  |  */
			   HP100_TX_COMPLETE |
			   /* HP100_MISC_ERROR   |  */
			   HP100_TX_ERROR | HP100_SET_LB, IRQ_MASK);
	} else {
		hp100_outw(HP100_RX_PACKET |
			   HP100_RX_ERROR | HP100_SET_HB |
			   HP100_TX_ERROR | HP100_SET_LB, IRQ_MASK);
	}

	/* Note : before hp100_set_multicast_list(), because it will play with
	 * spinlock itself... Jean II */
	spin_unlock_irqrestore(&lp->lock, flags);

	/* Enable MAC Tx and RX, set MAC modes, ... */
	hp100_set_multicast_list(dev);
}

static void hp100_stop_interface(struct net_device *dev)
{
	struct hp100_private *lp = netdev_priv(dev);
	int ioaddr = dev->base_addr;
	u_int val;

#ifdef HP100_DEBUG_B
	printk("hp100: %s: hp100_stop_interface\n", dev->name);
	hp100_outw(0x4221, TRACE);
#endif

	if (lp->mode == 1)
		hp100_BM_shutdown(dev);
	else {
		/* Note: MMAP_DIS will be reenabled by start_interface */
		hp100_outw(HP100_INT_EN | HP100_RESET_LB |
			   HP100_TRI_INT | HP100_MMAP_DIS | HP100_SET_HB,
			   OPTION_LSW);
		val = hp100_inw(OPTION_LSW);

		hp100_page(MAC_CTRL);
		hp100_andb(~(HP100_RX_EN | HP100_TX_EN), MAC_CFG_1);

		if (!(val & HP100_HW_RST))
			return;	/* If reset, imm. return ... */
		/* ... else: busy wait until idle */
		for (val = 0; val < 6000; val++)
			if ((hp100_inb(MAC_CFG_1) & (HP100_TX_IDLE | HP100_RX_IDLE)) == (HP100_TX_IDLE | HP100_RX_IDLE)) {
				hp100_page(PERFORMANCE);
				return;
			}
		printk("hp100: %s: hp100_stop_interface - timeout\n", dev->name);
		hp100_page(PERFORMANCE);
	}
}

static void hp100_load_eeprom(struct net_device *dev, u_short probe_ioaddr)
{
	int i;
	int ioaddr = probe_ioaddr > 0 ? probe_ioaddr : dev->base_addr;

#ifdef HP100_DEBUG_B
	hp100_outw(0x4222, TRACE);
#endif

	hp100_page(EEPROM_CTRL);
	hp100_andw(~HP100_EEPROM_LOAD, EEPROM_CTRL);
	hp100_orw(HP100_EEPROM_LOAD, EEPROM_CTRL);
	for (i = 0; i < 10000; i++)
		if (!(hp100_inb(OPTION_MSW) & HP100_EE_LOAD))
			return;
	printk("hp100: %s: hp100_load_eeprom - timeout\n", dev->name);
}

/*  Sense connection status.
 *  return values: LAN_10  - Connected to 10Mbit/s network
 *                 LAN_100 - Connected to 100Mbit/s network
 *                 LAN_ERR - not connected or 100Mbit/s Hub down
 */
static int hp100_sense_lan(struct net_device *dev)
{
	int ioaddr = dev->base_addr;
	u_short val_VG, val_10;
	struct hp100_private *lp = netdev_priv(dev);

#ifdef HP100_DEBUG_B
	hp100_outw(0x4223, TRACE);
#endif

	hp100_page(MAC_CTRL);
	val_10 = hp100_inb(10_LAN_CFG_1);
	val_VG = hp100_inb(VG_LAN_CFG_1);
	hp100_page(PERFORMANCE);
#ifdef HP100_DEBUG
	printk("hp100: %s: sense_lan: val_VG = 0x%04x, val_10 = 0x%04x\n",
	       dev->name, val_VG, val_10);
#endif

	if (val_10 & HP100_LINK_BEAT_ST)	/* 10Mb connection is active */
		return HP100_LAN_10;

	if (val_10 & HP100_AUI_ST) {	/* have we BNC or AUI onboard? */
		/*
		 * This can be overriden by dos utility, so if this has no effect,
		 * perhaps you need to download that utility from HP and set card
		 * back to "auto detect".
		 */
		val_10 |= HP100_AUI_SEL | HP100_LOW_TH;
		hp100_page(MAC_CTRL);
		hp100_outb(val_10, 10_LAN_CFG_1);
		hp100_page(PERFORMANCE);
		return HP100_LAN_COAX;
	}

	/* Those cards don't have a 100 Mbit connector */
	if ( !strcmp(lp->id, "HWP1920")  ||
2482 2483
	     (lp->pci_dev &&
	      lp->pci_dev->vendor == PCI_VENDOR_ID &&
L
Linus Torvalds 已提交
2484 2485 2486
	      (lp->pci_dev->device == PCI_DEVICE_ID_HP_J2970A ||
	       lp->pci_dev->device == PCI_DEVICE_ID_HP_J2973A)))
		return HP100_LAN_ERR;
2487

L
Linus Torvalds 已提交
2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509
	if (val_VG & HP100_LINK_CABLE_ST)	/* Can hear the HUBs tone. */
		return HP100_LAN_100;
	return HP100_LAN_ERR;
}

static int hp100_down_vg_link(struct net_device *dev)
{
	struct hp100_private *lp = netdev_priv(dev);
	int ioaddr = dev->base_addr;
	unsigned long time;
	long savelan, newlan;

#ifdef HP100_DEBUG_B
	hp100_outw(0x4224, TRACE);
	printk("hp100: %s: down_vg_link\n", dev->name);
#endif

	hp100_page(MAC_CTRL);
	time = jiffies + (HZ / 4);
	do {
		if (hp100_inb(VG_LAN_CFG_1) & HP100_LINK_CABLE_ST)
			break;
2510 2511
		if (!in_interrupt())
			schedule_timeout_interruptible(1);
L
Linus Torvalds 已提交
2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526
	} while (time_after(time, jiffies));

	if (time_after_eq(jiffies, time))	/* no signal->no logout */
		return 0;

	/* Drop the VG Link by clearing the link up cmd and load addr. */

	hp100_andb(~(HP100_LOAD_ADDR | HP100_LINK_CMD), VG_LAN_CFG_1);
	hp100_orb(HP100_VG_SEL, VG_LAN_CFG_1);

	/* Conditionally stall for >250ms on Link-Up Status (to go down) */
	time = jiffies + (HZ / 2);
	do {
		if (!(hp100_inb(VG_LAN_CFG_1) & HP100_LINK_UP_ST))
			break;
2527 2528
		if (!in_interrupt())
			schedule_timeout_interruptible(1);
L
Linus Torvalds 已提交
2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565
	} while (time_after(time, jiffies));

#ifdef HP100_DEBUG
	if (time_after_eq(jiffies, time))
		printk("hp100: %s: down_vg_link: Link does not go down?\n", dev->name);
#endif

	/* To prevent condition where Rev 1 VG MAC and old hubs do not complete */
	/* logout under traffic (even though all the status bits are cleared),  */
	/* do this workaround to get the Rev 1 MAC in its idle state */
	if (lp->chip == HP100_CHIPID_LASSEN) {
		/* Reset VG MAC to insure it leaves the logoff state even if */
		/* the Hub is still emitting tones */
		hp100_andb(~HP100_VG_RESET, VG_LAN_CFG_1);
		udelay(1500);	/* wait for >1ms */
		hp100_orb(HP100_VG_RESET, VG_LAN_CFG_1);	/* Release Reset */
		udelay(1500);
	}

	/* New: For lassen, switch to 10 Mbps mac briefly to clear training ACK */
	/* to get the VG mac to full reset. This is not req.d with later chips */
	/* Note: It will take the between 1 and 2 seconds for the VG mac to be */
	/* selected again! This will be left to the connect hub function to */
	/* perform if desired.  */
	if (lp->chip == HP100_CHIPID_LASSEN) {
		/* Have to write to 10 and 100VG control registers simultaneously */
		savelan = newlan = hp100_inl(10_LAN_CFG_1);	/* read 10+100 LAN_CFG regs */
		newlan &= ~(HP100_VG_SEL << 16);
		newlan |= (HP100_DOT3_MAC) << 8;
		hp100_andb(~HP100_AUTO_MODE, MAC_CFG_3);	/* Autosel off */
		hp100_outl(newlan, 10_LAN_CFG_1);

		/* Conditionally stall for 5sec on VG selected. */
		time = jiffies + (HZ * 5);
		do {
			if (!(hp100_inb(MAC_CFG_4) & HP100_MAC_SEL_ST))
				break;
2566 2567
			if (!in_interrupt())
				schedule_timeout_interruptible(1);
L
Linus Torvalds 已提交
2568 2569 2570 2571 2572 2573 2574 2575 2576 2577
		} while (time_after(time, jiffies));

		hp100_orb(HP100_AUTO_MODE, MAC_CFG_3);	/* Autosel back on */
		hp100_outl(savelan, 10_LAN_CFG_1);
	}

	time = jiffies + (3 * HZ);	/* Timeout 3s */
	do {
		if ((hp100_inb(VG_LAN_CFG_1) & HP100_LINK_CABLE_ST) == 0)
			break;
2578 2579
		if (!in_interrupt())
			schedule_timeout_interruptible(1);
L
Linus Torvalds 已提交
2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590
	} while (time_after(time, jiffies));

	if (time_before_eq(time, jiffies)) {
#ifdef HP100_DEBUG
		printk("hp100: %s: down_vg_link: timeout\n", dev->name);
#endif
		return -EIO;
	}

	time = jiffies + (2 * HZ);	/* This seems to take a while.... */
	do {
2591 2592
		if (!in_interrupt())
			schedule_timeout_interruptible(1);
L
Linus Torvalds 已提交
2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641
	} while (time_after(time, jiffies));

	return 0;
}

static int hp100_login_to_vg_hub(struct net_device *dev, u_short force_relogin)
{
	int ioaddr = dev->base_addr;
	struct hp100_private *lp = netdev_priv(dev);
	u_short val = 0;
	unsigned long time;
	int startst;

#ifdef HP100_DEBUG_B
	hp100_outw(0x4225, TRACE);
	printk("hp100: %s: login_to_vg_hub\n", dev->name);
#endif

	/* Initiate a login sequence iff VG MAC is enabled and either Load Address
	 * bit is zero or the force relogin flag is set (e.g. due to MAC address or
	 * promiscuous mode change)
	 */
	hp100_page(MAC_CTRL);
	startst = hp100_inb(VG_LAN_CFG_1);
	if ((force_relogin == 1) || (hp100_inb(MAC_CFG_4) & HP100_MAC_SEL_ST)) {
#ifdef HP100_DEBUG_TRAINING
		printk("hp100: %s: Start training\n", dev->name);
#endif

		/* Ensure VG Reset bit is 1 (i.e., do not reset) */
		hp100_orb(HP100_VG_RESET, VG_LAN_CFG_1);

		/* If Lassen AND auto-select-mode AND VG tones were sensed on */
		/* entry then temporarily put them into force 100Mbit mode */
		if ((lp->chip == HP100_CHIPID_LASSEN) && (startst & HP100_LINK_CABLE_ST))
			hp100_andb(~HP100_DOT3_MAC, 10_LAN_CFG_2);

		/* Drop the VG link by zeroing Link Up Command and Load Address  */
		hp100_andb(~(HP100_LINK_CMD /* |HP100_LOAD_ADDR */ ), VG_LAN_CFG_1);

#ifdef HP100_DEBUG_TRAINING
		printk("hp100: %s: Bring down the link\n", dev->name);
#endif

		/* Wait for link to drop */
		time = jiffies + (HZ / 10);
		do {
			if (~(hp100_inb(VG_LAN_CFG_1) & HP100_LINK_UP_ST))
				break;
2642 2643
			if (!in_interrupt())
				schedule_timeout_interruptible(1);
L
Linus Torvalds 已提交
2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677
		} while (time_after(time, jiffies));

		/* Start an addressed training and optionally request promiscuous port */
		if ((dev->flags) & IFF_PROMISC) {
			hp100_orb(HP100_PROM_MODE, VG_LAN_CFG_2);
			if (lp->chip == HP100_CHIPID_LASSEN)
				hp100_orw(HP100_MACRQ_PROMSC, TRAIN_REQUEST);
		} else {
			hp100_andb(~HP100_PROM_MODE, VG_LAN_CFG_2);
			/* For ETR parts we need to reset the prom. bit in the training
			 * register, otherwise promiscious mode won't be disabled.
			 */
			if (lp->chip == HP100_CHIPID_LASSEN) {
				hp100_andw(~HP100_MACRQ_PROMSC, TRAIN_REQUEST);
			}
		}

		/* With ETR parts, frame format request bits can be set. */
		if (lp->chip == HP100_CHIPID_LASSEN)
			hp100_orb(HP100_MACRQ_FRAMEFMT_EITHER, TRAIN_REQUEST);

		hp100_orb(HP100_LINK_CMD | HP100_LOAD_ADDR | HP100_VG_RESET, VG_LAN_CFG_1);

		/* Note: Next wait could be omitted for Hood and earlier chips under */
		/* certain circumstances */
		/* TODO: check if hood/earlier and skip wait. */

		/* Wait for either short timeout for VG tones or long for login    */
		/* Wait for the card hardware to signalise link cable status ok... */
		hp100_page(MAC_CTRL);
		time = jiffies + (1 * HZ);	/* 1 sec timeout for cable st */
		do {
			if (hp100_inb(VG_LAN_CFG_1) & HP100_LINK_CABLE_ST)
				break;
2678 2679
			if (!in_interrupt())
				schedule_timeout_interruptible(1);
L
Linus Torvalds 已提交
2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701
		} while (time_before(jiffies, time));

		if (time_after_eq(jiffies, time)) {
#ifdef HP100_DEBUG_TRAINING
			printk("hp100: %s: Link cable status not ok? Training aborted.\n", dev->name);
#endif
		} else {
#ifdef HP100_DEBUG_TRAINING
			printk
			    ("hp100: %s: HUB tones detected. Trying to train.\n",
			     dev->name);
#endif

			time = jiffies + (2 * HZ);	/* again a timeout */
			do {
				val = hp100_inb(VG_LAN_CFG_1);
				if ((val & (HP100_LINK_UP_ST))) {
#ifdef HP100_DEBUG_TRAINING
					printk("hp100: %s: Passed training.\n", dev->name);
#endif
					break;
				}
2702 2703
				if (!in_interrupt())
					schedule_timeout_interruptible(1);
L
Linus Torvalds 已提交
2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751
			} while (time_after(time, jiffies));
		}

		/* If LINK_UP_ST is set, then we are logged into the hub. */
		if (time_before_eq(jiffies, time) && (val & HP100_LINK_UP_ST)) {
#ifdef HP100_DEBUG_TRAINING
			printk("hp100: %s: Successfully logged into the HUB.\n", dev->name);
			if (lp->chip == HP100_CHIPID_LASSEN) {
				val = hp100_inw(TRAIN_ALLOW);
				printk("hp100: %s: Card supports 100VG MAC Version \"%s\" ",
					     dev->name, (hp100_inw(TRAIN_REQUEST) & HP100_CARD_MACVER) ? "802.12" : "Pre");
				printk("Driver will use MAC Version \"%s\"\n", (val & HP100_HUB_MACVER) ? "802.12" : "Pre");
				printk("hp100: %s: Frame format is %s.\n", dev->name, (val & HP100_MALLOW_FRAMEFMT) ? "802.5" : "802.3");
			}
#endif
		} else {
			/* If LINK_UP_ST is not set, login was not successful */
			printk("hp100: %s: Problem logging into the HUB.\n", dev->name);
			if (lp->chip == HP100_CHIPID_LASSEN) {
				/* Check allowed Register to find out why there is a problem. */
				val = hp100_inw(TRAIN_ALLOW);	/* won't work on non-ETR card */
#ifdef HP100_DEBUG_TRAINING
				printk("hp100: %s: MAC Configuration requested: 0x%04x, HUB allowed: 0x%04x\n", dev->name, hp100_inw(TRAIN_REQUEST), val);
#endif
				if (val & HP100_MALLOW_ACCDENIED)
					printk("hp100: %s: HUB access denied.\n", dev->name);
				if (val & HP100_MALLOW_CONFIGURE)
					printk("hp100: %s: MAC Configuration is incompatible with the Network.\n", dev->name);
				if (val & HP100_MALLOW_DUPADDR)
					printk("hp100: %s: Duplicate MAC Address on the Network.\n", dev->name);
			}
		}

		/* If we have put the chip into forced 100 Mbit mode earlier, go back */
		/* to auto-select mode */

		if ((lp->chip == HP100_CHIPID_LASSEN) && (startst & HP100_LINK_CABLE_ST)) {
			hp100_page(MAC_CTRL);
			hp100_orb(HP100_DOT3_MAC, 10_LAN_CFG_2);
		}

		val = hp100_inb(VG_LAN_CFG_1);

		/* Clear the MISC_ERROR Interrupt, which might be generated when doing the relogin */
		hp100_page(PERFORMANCE);
		hp100_outw(HP100_MISC_ERROR, IRQ_STATUS);

		if (val & HP100_LINK_UP_ST)
2752
			return 0;	/* login was ok */
L
Linus Torvalds 已提交
2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832
		else {
			printk("hp100: %s: Training failed.\n", dev->name);
			hp100_down_vg_link(dev);
			return -EIO;
		}
	}
	/* no forced relogin & already link there->no training. */
	return -EIO;
}

static void hp100_cascade_reset(struct net_device *dev, u_short enable)
{
	int ioaddr = dev->base_addr;
	struct hp100_private *lp = netdev_priv(dev);

#ifdef HP100_DEBUG_B
	hp100_outw(0x4226, TRACE);
	printk("hp100: %s: cascade_reset\n", dev->name);
#endif

	if (enable) {
		hp100_outw(HP100_HW_RST | HP100_RESET_LB, OPTION_LSW);
		if (lp->chip == HP100_CHIPID_LASSEN) {
			/* Lassen requires a PCI transmit fifo reset */
			hp100_page(HW_MAP);
			hp100_andb(~HP100_PCI_RESET, PCICTRL2);
			hp100_orb(HP100_PCI_RESET, PCICTRL2);
			/* Wait for min. 300 ns */
			/* we can't use jiffies here, because it may be */
			/* that we have disabled the timer... */
			udelay(400);
			hp100_andb(~HP100_PCI_RESET, PCICTRL2);
			hp100_page(PERFORMANCE);
		}
	} else {		/* bring out of reset */
		hp100_outw(HP100_HW_RST | HP100_SET_LB, OPTION_LSW);
		udelay(400);
		hp100_page(PERFORMANCE);
	}
}

#ifdef HP100_DEBUG
void hp100_RegisterDump(struct net_device *dev)
{
	int ioaddr = dev->base_addr;
	int Page;
	int Register;

	/* Dump common registers */
	printk("hp100: %s: Cascade Register Dump\n", dev->name);
	printk("hardware id #1: 0x%.2x\n", hp100_inb(HW_ID));
	printk("hardware id #2/paging: 0x%.2x\n", hp100_inb(PAGING));
	printk("option #1: 0x%.4x\n", hp100_inw(OPTION_LSW));
	printk("option #2: 0x%.4x\n", hp100_inw(OPTION_MSW));

	/* Dump paged registers */
	for (Page = 0; Page < 8; Page++) {
		/* Dump registers */
		printk("page: 0x%.2x\n", Page);
		outw(Page, ioaddr + 0x02);
		for (Register = 0x8; Register < 0x22; Register += 2) {
			/* Display Register contents except data port */
			if (((Register != 0x10) && (Register != 0x12)) || (Page > 0)) {
				printk("0x%.2x = 0x%.4x\n", Register, inw(ioaddr + Register));
			}
		}
	}
	hp100_page(PERFORMANCE);
}
#endif


static void cleanup_dev(struct net_device *d)
{
	struct hp100_private *p = netdev_priv(d);

	unregister_netdev(d);
	release_region(d->base_addr, HP100_REGION_SIZE);

	if (p->mode == 1)	/* busmaster */
2833 2834
		pci_free_consistent(p->pci_dev, MAX_RINGSIZE + 0x0f,
				    p->page_vaddr_algn,
L
Linus Torvalds 已提交
2835 2836 2837 2838 2839 2840 2841 2842
				    virt_to_whatever(d, p->page_vaddr_algn));
	if (p->mem_ptr_virt)
		iounmap(p->mem_ptr_virt);

	free_netdev(d);
}

#ifdef CONFIG_EISA
2843
static int __init hp100_eisa_probe (struct device *gendev)
L
Linus Torvalds 已提交
2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858
{
	struct net_device *dev = alloc_etherdev(sizeof(struct hp100_private));
	struct eisa_device *edev = to_eisa_device(gendev);
	int err;

	if (!dev)
		return -ENOMEM;

	SET_NETDEV_DEV(dev, &edev->dev);

	err = hp100_probe1(dev, edev->base_addr + 0xC38, HP100_BUS_EISA, NULL);
	if (err)
		goto out1;

#ifdef HP100_DEBUG
2859
	printk("hp100: %s: EISA adapter found at 0x%x\n", dev->name,
L
Linus Torvalds 已提交
2860 2861
	       dev->base_addr);
#endif
2862
	dev_set_drvdata(gendev, dev);
L
Linus Torvalds 已提交
2863 2864 2865 2866 2867 2868
	return 0;
 out1:
	free_netdev(dev);
	return err;
}

B
Bill Pemberton 已提交
2869
static int hp100_eisa_remove(struct device *gendev)
L
Linus Torvalds 已提交
2870
{
2871
	struct net_device *dev = dev_get_drvdata(gendev);
L
Linus Torvalds 已提交
2872 2873 2874 2875 2876 2877 2878 2879 2880
	cleanup_dev(dev);
	return 0;
}

static struct eisa_driver hp100_eisa_driver = {
        .id_table = hp100_eisa_tbl,
        .driver   = {
                .name    = "hp100",
                .probe   = hp100_eisa_probe,
2881
		.remove  = hp100_eisa_remove,
L
Linus Torvalds 已提交
2882 2883 2884 2885 2886
        }
};
#endif

#ifdef CONFIG_PCI
B
Bill Pemberton 已提交
2887 2888
static int hp100_pci_probe(struct pci_dev *pdev,
			   const struct pci_device_id *ent)
L
Linus Torvalds 已提交
2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921
{
	struct net_device *dev;
	int ioaddr;
	u_short pci_command;
	int err;

	if (pci_enable_device(pdev))
		return -ENODEV;

	dev = alloc_etherdev(sizeof(struct hp100_private));
	if (!dev) {
		err = -ENOMEM;
		goto out0;
	}

	SET_NETDEV_DEV(dev, &pdev->dev);

	pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
	if (!(pci_command & PCI_COMMAND_IO)) {
#ifdef HP100_DEBUG
		printk("hp100: %s: PCI I/O Bit has not been set. Setting...\n", dev->name);
#endif
		pci_command |= PCI_COMMAND_IO;
		pci_write_config_word(pdev, PCI_COMMAND, pci_command);
	}

	if (!(pci_command & PCI_COMMAND_MASTER)) {
#ifdef HP100_DEBUG
		printk("hp100: %s: PCI Master Bit has not been set. Setting...\n", dev->name);
#endif
		pci_command |= PCI_COMMAND_MASTER;
		pci_write_config_word(pdev, PCI_COMMAND, pci_command);
	}
2922

L
Linus Torvalds 已提交
2923 2924
	ioaddr = pci_resource_start(pdev, 0);
	err = hp100_probe1(dev, ioaddr, HP100_BUS_PCI, pdev);
2925
	if (err)
L
Linus Torvalds 已提交
2926
		goto out1;
2927

L
Linus Torvalds 已提交
2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939
#ifdef HP100_DEBUG
	printk("hp100: %s: PCI adapter found at 0x%x\n", dev->name, ioaddr);
#endif
	pci_set_drvdata(pdev, dev);
	return 0;
 out1:
	free_netdev(dev);
 out0:
	pci_disable_device(pdev);
	return err;
}

B
Bill Pemberton 已提交
2940
static void hp100_pci_remove(struct pci_dev *pdev)
L
Linus Torvalds 已提交
2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952
{
	struct net_device *dev = pci_get_drvdata(pdev);

	cleanup_dev(dev);
	pci_disable_device(pdev);
}


static struct pci_driver hp100_pci_driver = {
	.name		= "hp100",
	.id_table	= hp100_pci_tbl,
	.probe		= hp100_pci_probe,
B
Bill Pemberton 已提交
2953
	.remove		= hp100_pci_remove,
L
Linus Torvalds 已提交
2954 2955 2956 2957 2958 2959 2960 2961
};
#endif

/*
 *  module section
 */

MODULE_LICENSE("GPL");
2962
MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, "
L
Linus Torvalds 已提交
2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000
              "Siegfried \"Frieder\" Loeffler (dg1sek) <floeff@mathematik.uni-stuttgart.de>");
MODULE_DESCRIPTION("HP CASCADE Architecture Driver for 100VG-AnyLan Network Adapters");

/*
 * Note: to register three isa devices, use:
 * option hp100 hp100_port=0,0,0
 *        to register one card at io 0x280 as eth239, use:
 * option hp100 hp100_port=0x280
 */
#if defined(MODULE) && defined(CONFIG_ISA)
#define HP100_DEVICES 5
/* Parameters set by insmod */
static int hp100_port[HP100_DEVICES] = { 0, [1 ... (HP100_DEVICES-1)] = -1 };
module_param_array(hp100_port, int, NULL, 0);

/* List of devices */
static struct net_device *hp100_devlist[HP100_DEVICES];

static int __init hp100_isa_init(void)
{
	struct net_device *dev;
	int i, err, cards = 0;

	/* Don't autoprobe ISA bus */
	if (hp100_port[0] == 0)
		return -ENODEV;

	/* Loop on all possible base addresses */
	for (i = 0; i < HP100_DEVICES && hp100_port[i] != -1; ++i) {
		dev = alloc_etherdev(sizeof(struct hp100_private));
		if (!dev) {
			while (cards > 0)
				cleanup_dev(hp100_devlist[--cards]);

			return -ENOMEM;
		}

		err = hp100_isa_probe(dev, hp100_port[i]);
3001 3002 3003
		if (!err)
			hp100_devlist[cards++] = dev;
		else
L
Linus Torvalds 已提交
3004 3005 3006 3007 3008 3009
			free_netdev(dev);
	}

	return cards > 0 ? 0 : -ENODEV;
}

3010
static void hp100_isa_cleanup(void)
L
Linus Torvalds 已提交
3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033
{
	int i;

	for (i = 0; i < HP100_DEVICES; i++) {
		struct net_device *dev = hp100_devlist[i];
		if (dev)
			cleanup_dev(dev);
	}
}
#else
#define hp100_isa_init()	(0)
#define hp100_isa_cleanup()	do { } while(0)
#endif

static int __init hp100_module_init(void)
{
	int err;

	err = hp100_isa_init();
	if (err && err != -ENODEV)
		goto out;
#ifdef CONFIG_EISA
	err = eisa_driver_register(&hp100_eisa_driver);
3034
	if (err && err != -ENODEV)
L
Linus Torvalds 已提交
3035 3036 3037
		goto out2;
#endif
#ifdef CONFIG_PCI
3038
	err = pci_register_driver(&hp100_pci_driver);
3039
	if (err && err != -ENODEV)
L
Linus Torvalds 已提交
3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066
		goto out3;
#endif
 out:
	return err;
 out3:
#ifdef CONFIG_EISA
	eisa_driver_unregister (&hp100_eisa_driver);
 out2:
#endif
	hp100_isa_cleanup();
	goto out;
}


static void __exit hp100_module_exit(void)
{
	hp100_isa_cleanup();
#ifdef CONFIG_EISA
	eisa_driver_unregister (&hp100_eisa_driver);
#endif
#ifdef CONFIG_PCI
	pci_unregister_driver (&hp100_pci_driver);
#endif
}

module_init(hp100_module_init)
module_exit(hp100_module_exit)