dl2k.c 47.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*  D-Link DL2000-based Gigabit Ethernet Adapter Linux driver */
/*
    Copyright (c) 2001, 2002 by D-Link Corporation
    Written by Edward Peng.<edward_peng@dlink.com.tw>
    Created 03-May-2001, base on Linux' sundance.c.

    This program 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.
*/

13 14 15
#define DRV_NAME	"DL2000/TC902x-based linux driver"
#define DRV_VERSION	"v1.19"
#define DRV_RELDATE	"2007/08/12"
L
Linus Torvalds 已提交
16
#include "dl2k.h"
A
Andrew Morton 已提交
17
#include <linux/dma-mapping.h>
L
Linus Torvalds 已提交
18

19 20 21 22 23 24 25
#define dw32(reg, val)	iowrite32(val, ioaddr + (reg))
#define dw16(reg, val)	iowrite16(val, ioaddr + (reg))
#define dw8(reg, val)	iowrite8(val, ioaddr + (reg))
#define dr32(reg)	ioread32(ioaddr + (reg))
#define dr16(reg)	ioread16(ioaddr + (reg))
#define dr8(reg)	ioread8(ioaddr + (reg))

B
Bill Pemberton 已提交
26
static char version[] =
27
      KERN_INFO DRV_NAME " " DRV_VERSION " " DRV_RELDATE "\n";
L
Linus Torvalds 已提交
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
#define MAX_UNITS 8
static int mtu[MAX_UNITS];
static int vlan[MAX_UNITS];
static int jumbo[MAX_UNITS];
static char *media[MAX_UNITS];
static int tx_flow=-1;
static int rx_flow=-1;
static int copy_thresh;
static int rx_coalesce=10;	/* Rx frame count each interrupt */
static int rx_timeout=200;	/* Rx DMA wait time in 640ns increments */
static int tx_coalesce=16;	/* HW xmit count each TxDMAComplete */


MODULE_AUTHOR ("Edward Peng");
MODULE_DESCRIPTION ("D-Link DL2000-based Gigabit Ethernet Adapter");
MODULE_LICENSE("GPL");
module_param_array(mtu, int, NULL, 0);
module_param_array(media, charp, NULL, 0);
module_param_array(vlan, int, NULL, 0);
module_param_array(jumbo, int, NULL, 0);
module_param(tx_flow, int, 0);
module_param(rx_flow, int, 0);
module_param(copy_thresh, int, 0);
module_param(rx_coalesce, int, 0);	/* Rx frame count each interrupt */
module_param(rx_timeout, int, 0);	/* Rx DMA wait time in 64ns increments */
module_param(tx_coalesce, int, 0); /* HW xmit count each TxDMAComplete */


/* Enable the default interrupts */
#define DEFAULT_INTR (RxDMAComplete | HostError | IntRequested | TxDMAComplete| \
       UpdateStats | LinkEvent)
59 60 61 62 63 64 65

static void dl2k_enable_int(struct netdev_private *np)
{
	void __iomem *ioaddr = np->ioaddr;

	dw16(IntEnable, DEFAULT_INTR);
}
L
Linus Torvalds 已提交
66

67 68
static const int max_intrloop = 50;
static const int multicast_filter_limit = 0x40;
L
Linus Torvalds 已提交
69 70 71 72

static int rio_open (struct net_device *dev);
static void rio_timer (unsigned long data);
static void rio_tx_timeout (struct net_device *dev);
73
static netdev_tx_t start_xmit (struct sk_buff *skb, struct net_device *dev);
74
static irqreturn_t rio_interrupt (int irq, void *dev_instance);
L
Linus Torvalds 已提交
75 76 77 78 79 80 81 82 83 84 85 86
static void rio_free_tx (struct net_device *dev, int irq);
static void tx_error (struct net_device *dev, int tx_status);
static int receive_packet (struct net_device *dev);
static void rio_error (struct net_device *dev, int int_status);
static int change_mtu (struct net_device *dev, int new_mtu);
static void set_multicast (struct net_device *dev);
static struct net_device_stats *get_stats (struct net_device *dev);
static int clear_stats (struct net_device *dev);
static int rio_ioctl (struct net_device *dev, struct ifreq *rq, int cmd);
static int rio_close (struct net_device *dev);
static int find_miiphy (struct net_device *dev);
static int parse_eeprom (struct net_device *dev);
87
static int read_eeprom (struct netdev_private *, int eep_addr);
L
Linus Torvalds 已提交
88 89 90 91 92 93 94 95 96
static int mii_wait_link (struct net_device *dev, int wait);
static int mii_set_media (struct net_device *dev);
static int mii_get_media (struct net_device *dev);
static int mii_set_media_pcs (struct net_device *dev);
static int mii_get_media_pcs (struct net_device *dev);
static int mii_read (struct net_device *dev, int phy_addr, int reg_num);
static int mii_write (struct net_device *dev, int phy_addr, int reg_num,
		      u16 data);

97
static const struct ethtool_ops ethtool_ops;
L
Linus Torvalds 已提交
98

99 100 101 102 103 104 105
static const struct net_device_ops netdev_ops = {
	.ndo_open		= rio_open,
	.ndo_start_xmit	= start_xmit,
	.ndo_stop		= rio_close,
	.ndo_get_stats		= get_stats,
	.ndo_validate_addr	= eth_validate_addr,
	.ndo_set_mac_address 	= eth_mac_addr,
106
	.ndo_set_rx_mode	= set_multicast,
107 108 109 110 111
	.ndo_do_ioctl		= rio_ioctl,
	.ndo_tx_timeout		= rio_tx_timeout,
	.ndo_change_mtu		= change_mtu,
};

B
Bill Pemberton 已提交
112
static int
L
Linus Torvalds 已提交
113 114 115 116 117 118 119
rio_probe1 (struct pci_dev *pdev, const struct pci_device_id *ent)
{
	struct net_device *dev;
	struct netdev_private *np;
	static int card_idx;
	int chip_idx = ent->driver_data;
	int err, irq;
120
	void __iomem *ioaddr;
L
Linus Torvalds 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	static int version_printed;
	void *ring_space;
	dma_addr_t ring_dma;

	if (!version_printed++)
		printk ("%s", version);

	err = pci_enable_device (pdev);
	if (err)
		return err;

	irq = pdev->irq;
	err = pci_request_regions (pdev, "dl2k");
	if (err)
		goto err_out_disable;

	pci_set_master (pdev);
138 139 140

	err = -ENOMEM;

L
Linus Torvalds 已提交
141
	dev = alloc_etherdev (sizeof (*np));
142
	if (!dev)
L
Linus Torvalds 已提交
143 144 145
		goto err_out_res;
	SET_NETDEV_DEV(dev, &pdev->dev);

146 147 148 149 150
	np = netdev_priv(dev);

	/* IO registers range. */
	ioaddr = pci_iomap(pdev, 0, 0);
	if (!ioaddr)
L
Linus Torvalds 已提交
151
		goto err_out_dev;
152 153 154 155 156 157 158
	np->eeprom_addr = ioaddr;

#ifdef MEM_MAPPING
	/* MM registers range. */
	ioaddr = pci_iomap(pdev, 1, 0);
	if (!ioaddr)
		goto err_out_iounmap;
L
Linus Torvalds 已提交
159
#endif
160
	np->ioaddr = ioaddr;
L
Linus Torvalds 已提交
161 162 163 164 165 166 167 168 169 170 171 172
	np->chip_id = chip_idx;
	np->pdev = pdev;
	spin_lock_init (&np->tx_lock);
	spin_lock_init (&np->rx_lock);

	/* Parse manual configuration */
	np->an_enable = 1;
	np->tx_coalesce = 1;
	if (card_idx < MAX_UNITS) {
		if (media[card_idx] != NULL) {
			np->an_enable = 0;
			if (strcmp (media[card_idx], "auto") == 0 ||
173
			    strcmp (media[card_idx], "autosense") == 0 ||
L
Linus Torvalds 已提交
174
			    strcmp (media[card_idx], "0") == 0 ) {
175
				np->an_enable = 2;
L
Linus Torvalds 已提交
176 177 178 179
			} else if (strcmp (media[card_idx], "100mbps_fd") == 0 ||
			    strcmp (media[card_idx], "4") == 0) {
				np->speed = 100;
				np->full_duplex = 1;
180 181
			} else if (strcmp (media[card_idx], "100mbps_hd") == 0 ||
				   strcmp (media[card_idx], "3") == 0) {
L
Linus Torvalds 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
				np->speed = 100;
				np->full_duplex = 0;
			} else if (strcmp (media[card_idx], "10mbps_fd") == 0 ||
				   strcmp (media[card_idx], "2") == 0) {
				np->speed = 10;
				np->full_duplex = 1;
			} else if (strcmp (media[card_idx], "10mbps_hd") == 0 ||
				   strcmp (media[card_idx], "1") == 0) {
				np->speed = 10;
				np->full_duplex = 0;
			} else if (strcmp (media[card_idx], "1000mbps_fd") == 0 ||
				 strcmp (media[card_idx], "6") == 0) {
				np->speed=1000;
				np->full_duplex=1;
			} else if (strcmp (media[card_idx], "1000mbps_hd") == 0 ||
				 strcmp (media[card_idx], "5") == 0) {
				np->speed = 1000;
				np->full_duplex = 0;
			} else {
				np->an_enable = 1;
			}
		}
		if (jumbo[card_idx] != 0) {
			np->jumbo = 1;
			dev->mtu = MAX_JUMBO;
		} else {
			np->jumbo = 0;
			if (mtu[card_idx] > 0 && mtu[card_idx] < PACKET_SIZE)
				dev->mtu = mtu[card_idx];
		}
		np->vlan = (vlan[card_idx] > 0 && vlan[card_idx] < 4096) ?
		    vlan[card_idx] : 0;
		if (rx_coalesce > 0 && rx_timeout > 0) {
			np->rx_coalesce = rx_coalesce;
			np->rx_timeout = rx_timeout;
			np->coalesce = 1;
		}
		np->tx_flow = (tx_flow == 0) ? 0 : 1;
		np->rx_flow = (rx_flow == 0) ? 0 : 1;

		if (tx_coalesce < 1)
			tx_coalesce = 1;
		else if (tx_coalesce > TX_RING_SIZE-1)
			tx_coalesce = TX_RING_SIZE - 1;
	}
227
	dev->netdev_ops = &netdev_ops;
L
Linus Torvalds 已提交
228
	dev->watchdog_timeo = TX_TIMEOUT;
229
	dev->ethtool_ops = &ethtool_ops;
L
Linus Torvalds 已提交
230 231 232 233 234 235 236 237
#if 0
	dev->features = NETIF_F_IP_CSUM;
#endif
	pci_set_drvdata (pdev, dev);

	ring_space = pci_alloc_consistent (pdev, TX_TOTAL_SIZE, &ring_dma);
	if (!ring_space)
		goto err_out_iounmap;
238
	np->tx_ring = ring_space;
L
Linus Torvalds 已提交
239 240 241 242 243
	np->tx_ring_dma = ring_dma;

	ring_space = pci_alloc_consistent (pdev, RX_TOTAL_SIZE, &ring_dma);
	if (!ring_space)
		goto err_out_unmap_tx;
244
	np->rx_ring = ring_space;
L
Linus Torvalds 已提交
245 246 247 248 249 250 251 252 253
	np->rx_ring_dma = ring_dma;

	/* Parse eeprom data */
	parse_eeprom (dev);

	/* Find PHY address */
	err = find_miiphy (dev);
	if (err)
		goto err_out_unmap_rx;
254

L
Linus Torvalds 已提交
255
	/* Fiber device? */
256
	np->phy_media = (dr16(ASICCtrl) & PhyMedia) ? 1 : 0;
L
Linus Torvalds 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
	np->link_status = 0;
	/* Set media and reset PHY */
	if (np->phy_media) {
		/* default Auto-Negotiation for fiber deivices */
	 	if (np->an_enable == 2) {
			np->an_enable = 1;
		}
	} else {
		/* Auto-Negotiation is mandatory for 1000BASE-T,
		   IEEE 802.3ab Annex 28D page 14 */
		if (np->speed == 1000)
			np->an_enable = 1;
	}

	err = register_netdev (dev);
	if (err)
		goto err_out_unmap_rx;

	card_idx++;

J
Johannes Berg 已提交
277 278
	printk (KERN_INFO "%s: %s, %pM, IRQ %d\n",
		dev->name, np->name, dev->dev_addr, irq);
L
Linus Torvalds 已提交
279
	if (tx_coalesce > 1)
280
		printk(KERN_INFO "tx_coalesce:\t%d packets\n",
L
Linus Torvalds 已提交
281 282
				tx_coalesce);
	if (np->coalesce)
283 284 285
		printk(KERN_INFO
		       "rx_coalesce:\t%d packets\n"
		       "rx_timeout: \t%d ns\n",
L
Linus Torvalds 已提交
286 287 288 289 290
				np->rx_coalesce, np->rx_timeout*640);
	if (np->vlan)
		printk(KERN_INFO "vlan(id):\t%d\n", np->vlan);
	return 0;

291
err_out_unmap_rx:
L
Linus Torvalds 已提交
292
	pci_free_consistent (pdev, RX_TOTAL_SIZE, np->rx_ring, np->rx_ring_dma);
293
err_out_unmap_tx:
L
Linus Torvalds 已提交
294
	pci_free_consistent (pdev, TX_TOTAL_SIZE, np->tx_ring, np->tx_ring_dma);
295
err_out_iounmap:
L
Linus Torvalds 已提交
296
#ifdef MEM_MAPPING
297
	pci_iounmap(pdev, np->ioaddr);
L
Linus Torvalds 已提交
298
#endif
299 300
	pci_iounmap(pdev, np->eeprom_addr);
err_out_dev:
L
Linus Torvalds 已提交
301
	free_netdev (dev);
302
err_out_res:
L
Linus Torvalds 已提交
303
	pci_release_regions (pdev);
304
err_out_disable:
L
Linus Torvalds 已提交
305 306 307 308
	pci_disable_device (pdev);
	return err;
}

309
static int
L
Linus Torvalds 已提交
310 311
find_miiphy (struct net_device *dev)
{
312
	struct netdev_private *np = netdev_priv(dev);
L
Linus Torvalds 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
	int i, phy_found = 0;
	np = netdev_priv(dev);
	np->phy_addr = 1;

	for (i = 31; i >= 0; i--) {
		int mii_status = mii_read (dev, i, 1);
		if (mii_status != 0xffff && mii_status != 0x0000) {
			np->phy_addr = i;
			phy_found++;
		}
	}
	if (!phy_found) {
		printk (KERN_ERR "%s: No MII PHY found!\n", dev->name);
		return -ENODEV;
	}
	return 0;
}

331
static int
L
Linus Torvalds 已提交
332 333
parse_eeprom (struct net_device *dev)
{
334 335
	struct netdev_private *np = netdev_priv(dev);
	void __iomem *ioaddr = np->ioaddr;
L
Linus Torvalds 已提交
336 337 338 339 340 341 342 343
	int i, j;
	u8 sromdata[256];
	u8 *psib;
	u32 crc;
	PSROM_t psrom = (PSROM_t) sromdata;

	int cid, next;

344 345 346
	for (i = 0; i < 128; i++)
		((__le16 *) sromdata)[i] = cpu_to_le16(read_eeprom(np, i));

347 348 349
	if (np->pdev->vendor == PCI_VENDOR_ID_DLINK) {	/* D-Link Only */
		/* Check CRC */
		crc = ~ether_crc_le (256 - 4, sromdata);
350
		if (psrom->crc != cpu_to_le32(crc)) {
351 352 353 354
			printk (KERN_ERR "%s: EEPROM data CRC error.\n",
					dev->name);
			return -1;
		}
L
Linus Torvalds 已提交
355 356 357 358 359 360
	}

	/* Set MAC address */
	for (i = 0; i < 6; i++)
		dev->dev_addr[i] = psrom->mac_addr[i];

361 362 363 364 365
	if (np->chip_id == CHIP_IP1000A) {
		np->led_mode = psrom->led_mode;
		return 0;
	}

366 367 368 369
	if (np->pdev->vendor != PCI_VENDOR_ID_DLINK) {
		return 0;
	}

370
	/* Parse Software Information Block */
L
Linus Torvalds 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
	i = 0x30;
	psib = (u8 *) sromdata;
	do {
		cid = psib[i++];
		next = psib[i++];
		if ((cid == 0 && next == 0) || (cid == 0xff && next == 0xff)) {
			printk (KERN_ERR "Cell data error\n");
			return -1;
		}
		switch (cid) {
		case 0:	/* Format version */
			break;
		case 1:	/* End of cell */
			return 0;
		case 2:	/* Duplex Polarity */
			np->duplex_polarity = psib[i];
387
			dw8(PhyCtrl, dr8(PhyCtrl) | psib[i]);
L
Linus Torvalds 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
			break;
		case 3:	/* Wake Polarity */
			np->wake_polarity = psib[i];
			break;
		case 9:	/* Adapter description */
			j = (next - i > 255) ? 255 : next - i;
			memcpy (np->name, &(psib[i]), j);
			break;
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:	/* Reversed */
			break;
		default:	/* Unknown cell */
			return -1;
		}
		i = next;
	} while (1);

	return 0;
}

411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
static void rio_set_led_mode(struct net_device *dev)
{
	struct netdev_private *np = netdev_priv(dev);
	void __iomem *ioaddr = np->ioaddr;
	u32 mode;

	if (np->chip_id != CHIP_IP1000A)
		return;

	mode = dr32(ASICCtrl);
	mode &= ~(IPG_AC_LED_MODE_BIT_1 | IPG_AC_LED_MODE | IPG_AC_LED_SPEED);

	if (np->led_mode & 0x01)
		mode |= IPG_AC_LED_MODE;
	if (np->led_mode & 0x02)
		mode |= IPG_AC_LED_MODE_BIT_1;
	if (np->led_mode & 0x08)
		mode |= IPG_AC_LED_SPEED;

	dw32(ASICCtrl, mode);
}

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
static inline dma_addr_t desc_to_dma(struct netdev_desc *desc)
{
	return le64_to_cpu(desc->fraginfo) & DMA_BIT_MASK(48);
}

static void free_list(struct net_device *dev)
{
	struct netdev_private *np = netdev_priv(dev);
	struct sk_buff *skb;
	int i;

	/* Free all the skbuffs in the queue. */
	for (i = 0; i < RX_RING_SIZE; i++) {
		skb = np->rx_skbuff[i];
		if (skb) {
			pci_unmap_single(np->pdev, desc_to_dma(&np->rx_ring[i]),
					 skb->len, PCI_DMA_FROMDEVICE);
			dev_kfree_skb(skb);
			np->rx_skbuff[i] = NULL;
		}
		np->rx_ring[i].status = 0;
		np->rx_ring[i].fraginfo = 0;
	}
	for (i = 0; i < TX_RING_SIZE; i++) {
		skb = np->tx_skbuff[i];
		if (skb) {
			pci_unmap_single(np->pdev, desc_to_dma(&np->tx_ring[i]),
					 skb->len, PCI_DMA_TODEVICE);
			dev_kfree_skb(skb);
			np->tx_skbuff[i] = NULL;
		}
	}
}

O
Ondrej Zary 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
static void rio_reset_ring(struct netdev_private *np)
{
	int i;

	np->cur_rx = 0;
	np->cur_tx = 0;
	np->old_rx = 0;
	np->old_tx = 0;

	for (i = 0; i < TX_RING_SIZE; i++)
		np->tx_ring[i].status = cpu_to_le64(TFDDone);

	for (i = 0; i < RX_RING_SIZE; i++)
		np->rx_ring[i].status = 0;
}

483 484 485 486 487 488
 /* allocate and initialize Tx and Rx descriptors */
static int alloc_list(struct net_device *dev)
{
	struct netdev_private *np = netdev_priv(dev);
	int i;

O
Ondrej Zary 已提交
489
	rio_reset_ring(np);
490 491 492 493 494 495 496 497 498 499
	np->rx_buf_sz = (dev->mtu <= 1500 ? PACKET_SIZE : dev->mtu + 32);

	/* Initialize Tx descriptors, TFDListPtr leaves in start_xmit(). */
	for (i = 0; i < TX_RING_SIZE; i++) {
		np->tx_skbuff[i] = NULL;
		np->tx_ring[i].next_desc = cpu_to_le64(np->tx_ring_dma +
					      ((i + 1) % TX_RING_SIZE) *
					      sizeof(struct netdev_desc));
	}

O
Ondrej Zary 已提交
500
	/* Initialize Rx descriptors & allocate buffers */
501 502 503 504 505 506 507 508 509 510 511
	for (i = 0; i < RX_RING_SIZE; i++) {
		/* Allocated fixed size of skbuff */
		struct sk_buff *skb;

		skb = netdev_alloc_skb_ip_align(dev, np->rx_buf_sz);
		np->rx_skbuff[i] = skb;
		if (!skb) {
			free_list(dev);
			return -ENOMEM;
		}

O
Ondrej Zary 已提交
512 513 514
		np->rx_ring[i].next_desc = cpu_to_le64(np->rx_ring_dma +
						((i + 1) % RX_RING_SIZE) *
						sizeof(struct netdev_desc));
515 516 517 518 519 520 521 522 523 524 525
		/* Rubicon now supports 40 bits of addressing space. */
		np->rx_ring[i].fraginfo =
		    cpu_to_le64(pci_map_single(
				  np->pdev, skb->data, np->rx_buf_sz,
				  PCI_DMA_FROMDEVICE));
		np->rx_ring[i].fraginfo |= cpu_to_le64((u64)np->rx_buf_sz << 48);
	}

	return 0;
}

526
static void rio_hw_init(struct net_device *dev)
L
Linus Torvalds 已提交
527 528
{
	struct netdev_private *np = netdev_priv(dev);
529
	void __iomem *ioaddr = np->ioaddr;
L
Linus Torvalds 已提交
530 531
	int i;
	u16 macctrl;
532

L
Linus Torvalds 已提交
533
	/* Reset all logic functions */
534 535
	dw16(ASICCtrl + 2,
	     GlobalReset | DMAReset | FIFOReset | NetworkReset | HostReset);
L
Linus Torvalds 已提交
536
	mdelay(10);
537

538 539
	rio_set_led_mode(dev);

L
Linus Torvalds 已提交
540
	/* DebugCtrl bit 4, 5, 9 must set */
541
	dw32(DebugCtrl, dr32(DebugCtrl) | 0x0230);
L
Linus Torvalds 已提交
542

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
	if (np->chip_id == CHIP_IP1000A &&
	    (np->pdev->revision == 0x40 || np->pdev->revision == 0x41)) {
		/* PHY magic taken from ipg driver, undocumented registers */
		mii_write(dev, np->phy_addr, 31, 0x0001);
		mii_write(dev, np->phy_addr, 27, 0x01e0);
		mii_write(dev, np->phy_addr, 31, 0x0002);
		mii_write(dev, np->phy_addr, 27, 0xeb8e);
		mii_write(dev, np->phy_addr, 31, 0x0000);
		mii_write(dev, np->phy_addr, 30, 0x005e);
		/* advertise 1000BASE-T half & full duplex, prefer MASTER */
		mii_write(dev, np->phy_addr, MII_CTRL1000, 0x0700);
	}

	if (np->phy_media)
		mii_set_media_pcs(dev);
	else
		mii_set_media(dev);

L
Linus Torvalds 已提交
561 562
	/* Jumbo frame */
	if (np->jumbo != 0)
563
		dw16(MaxFrameSize, MAX_JUMBO+14);
L
Linus Torvalds 已提交
564

565 566 567
	/* Set RFDListPtr */
	dw32(RFDListPtr0, np->rx_ring_dma);
	dw32(RFDListPtr1, 0);
L
Linus Torvalds 已提交
568

569 570 571 572 573 574 575
	/* Set station address */
	/* 16 or 32-bit access is required by TC9020 datasheet but 8-bit works
	 * too. However, it doesn't work on IP1000A so we use 16-bit access.
	 */
	for (i = 0; i < 3; i++)
		dw16(StationAddr0 + 2 * i,
		     cpu_to_le16(((u16 *)dev->dev_addr)[i]));
L
Linus Torvalds 已提交
576 577 578

	set_multicast (dev);
	if (np->coalesce) {
579
		dw32(RxDMAIntCtrl, np->rx_coalesce | np->rx_timeout << 16);
L
Linus Torvalds 已提交
580 581
	}
	/* Set RIO to poll every N*320nsec. */
582 583 584 585 586
	dw8(RxDMAPollPeriod, 0x20);
	dw8(TxDMAPollPeriod, 0xff);
	dw8(RxDMABurstThresh, 0x30);
	dw8(RxDMAUrgentThresh, 0x30);
	dw32(RmonStatMask, 0x0007ffff);
L
Linus Torvalds 已提交
587 588 589 590 591 592
	/* clear statistics */
	clear_stats (dev);

	/* VLAN supported */
	if (np->vlan) {
		/* priority field in RxDMAIntCtrl  */
593
		dw32(RxDMAIntCtrl, dr32(RxDMAIntCtrl) | 0x7 << 10);
L
Linus Torvalds 已提交
594
		/* VLANId */
595
		dw16(VLANId, np->vlan);
L
Linus Torvalds 已提交
596
		/* Length/Type should be 0x8100 */
597
		dw32(VLANTag, 0x8100 << 16 | np->vlan);
L
Linus Torvalds 已提交
598 599
		/* Enable AutoVLANuntagging, but disable AutoVLANtagging.
		   VLAN information tagged by TFC' VID, CFI fields. */
600
		dw32(MACCtrl, dr32(MACCtrl) | AutoVLANuntagging);
L
Linus Torvalds 已提交
601 602 603
	}

	/* Start Tx/Rx */
604
	dw32(MACCtrl, dr32(MACCtrl) | StatsEnable | RxEnable | TxEnable);
605

L
Linus Torvalds 已提交
606 607 608 609 610
	macctrl = 0;
	macctrl |= (np->vlan) ? AutoVLANuntagging : 0;
	macctrl |= (np->full_duplex) ? DuplexSelect : 0;
	macctrl |= (np->tx_flow) ? TxFlowControlEnable : 0;
	macctrl |= (np->rx_flow) ? RxFlowControlEnable : 0;
611
	dw16(MACCtrl, macctrl);
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
}

static void rio_hw_stop(struct net_device *dev)
{
	struct netdev_private *np = netdev_priv(dev);
	void __iomem *ioaddr = np->ioaddr;

	/* Disable interrupts */
	dw16(IntEnable, 0);

	/* Stop Tx and Rx logics */
	dw32(MACCtrl, TxDisable | RxDisable | StatsDisable);
}

static int rio_open(struct net_device *dev)
{
	struct netdev_private *np = netdev_priv(dev);
	const int irq = np->pdev->irq;
	int i;

	i = alloc_list(dev);
	if (i)
		return i;

	rio_hw_init(dev);

	i = request_irq(irq, rio_interrupt, IRQF_SHARED, dev->name, dev);
	if (i) {
		rio_hw_stop(dev);
		free_list(dev);
		return i;
	}

	setup_timer(&np->timer, rio_timer, (unsigned long)dev);
	np->timer.expires = jiffies + 1 * HZ;
	add_timer(&np->timer);
L
Linus Torvalds 已提交
648 649

	netif_start_queue (dev);
650

651
	dl2k_enable_int(np);
L
Linus Torvalds 已提交
652 653 654
	return 0;
}

655
static void
L
Linus Torvalds 已提交
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
rio_timer (unsigned long data)
{
	struct net_device *dev = (struct net_device *)data;
	struct netdev_private *np = netdev_priv(dev);
	unsigned int entry;
	int next_tick = 1*HZ;
	unsigned long flags;

	spin_lock_irqsave(&np->rx_lock, flags);
	/* Recover rx ring exhausted error */
	if (np->cur_rx - np->old_rx >= RX_RING_SIZE) {
		printk(KERN_INFO "Try to recover rx ring exhausted...\n");
		/* Re-allocate skbuffs to fill the descriptor ring */
		for (; np->cur_rx - np->old_rx > 0; np->old_rx++) {
			struct sk_buff *skb;
			entry = np->old_rx % RX_RING_SIZE;
			/* Dropped packets don't need to re-allocate */
			if (np->rx_skbuff[entry] == NULL) {
674 675
				skb = netdev_alloc_skb_ip_align(dev,
								np->rx_buf_sz);
L
Linus Torvalds 已提交
676 677 678 679 680 681 682 683 684 685
				if (skb == NULL) {
					np->rx_ring[entry].fraginfo = 0;
					printk (KERN_INFO
						"%s: Still unable to re-allocate Rx skbuff.#%d\n",
						dev->name, entry);
					break;
				}
				np->rx_skbuff[entry] = skb;
				np->rx_ring[entry].fraginfo =
				    cpu_to_le64 (pci_map_single
686
					 (np->pdev, skb->data, np->rx_buf_sz,
L
Linus Torvalds 已提交
687 688 689
					  PCI_DMA_FROMDEVICE));
			}
			np->rx_ring[entry].fraginfo |=
A
Al Viro 已提交
690
			    cpu_to_le64((u64)np->rx_buf_sz << 48);
L
Linus Torvalds 已提交
691 692 693 694 695 696 697
			np->rx_ring[entry].status = 0;
		} /* end for */
	} /* end if */
	spin_unlock_irqrestore (&np->rx_lock, flags);
	np->timer.expires = jiffies + next_tick;
	add_timer(&np->timer);
}
698

L
Linus Torvalds 已提交
699 700 701
static void
rio_tx_timeout (struct net_device *dev)
{
702 703
	struct netdev_private *np = netdev_priv(dev);
	void __iomem *ioaddr = np->ioaddr;
L
Linus Torvalds 已提交
704 705

	printk (KERN_INFO "%s: Tx timed out (%4.4x), is buffer full?\n",
706
		dev->name, dr32(TxStatus));
L
Linus Torvalds 已提交
707 708
	rio_free_tx(dev, 0);
	dev->if_port = 0;
E
Eric Dumazet 已提交
709
	dev->trans_start = jiffies; /* prevent tx timeout */
L
Linus Torvalds 已提交
710 711
}

712
static netdev_tx_t
L
Linus Torvalds 已提交
713 714 715
start_xmit (struct sk_buff *skb, struct net_device *dev)
{
	struct netdev_private *np = netdev_priv(dev);
716
	void __iomem *ioaddr = np->ioaddr;
L
Linus Torvalds 已提交
717 718 719 720 721 722
	struct netdev_desc *txdesc;
	unsigned entry;
	u64 tfc_vlan_tag = 0;

	if (np->link_status == 0) {	/* Link Down */
		dev_kfree_skb(skb);
E
Eric Dumazet 已提交
723
		return NETDEV_TX_OK;
L
Linus Torvalds 已提交
724 725 726 727 728 729
	}
	entry = np->cur_tx % TX_RING_SIZE;
	np->tx_skbuff[entry] = skb;
	txdesc = &np->tx_ring[entry];

#if 0
730
	if (skb->ip_summed == CHECKSUM_PARTIAL) {
L
Linus Torvalds 已提交
731 732 733 734 735 736
		txdesc->status |=
		    cpu_to_le64 (TCPChecksumEnable | UDPChecksumEnable |
				 IPChecksumEnable);
	}
#endif
	if (np->vlan) {
A
Al Viro 已提交
737 738 739
		tfc_vlan_tag = VLANTagInsert |
		    ((u64)np->vlan << 32) |
		    ((u64)skb->priority << 45);
L
Linus Torvalds 已提交
740 741 742 743
	}
	txdesc->fraginfo = cpu_to_le64 (pci_map_single (np->pdev, skb->data,
							skb->len,
							PCI_DMA_TODEVICE));
A
Al Viro 已提交
744
	txdesc->fraginfo |= cpu_to_le64((u64)skb->len << 48);
L
Linus Torvalds 已提交
745 746 747 748 749

	/* DL2K bug: DMA fails to get next descriptor ptr in 10Mbps mode
	 * Work around: Always use 1 descriptor in 10Mbps mode */
	if (entry % np->tx_coalesce == 0 || np->speed == 10)
		txdesc->status = cpu_to_le64 (entry | tfc_vlan_tag |
750
					      WordAlignDisable |
L
Linus Torvalds 已提交
751 752 753 754
					      TxDMAIndicate |
					      (1 << FragCountShift));
	else
		txdesc->status = cpu_to_le64 (entry | tfc_vlan_tag |
755
					      WordAlignDisable |
L
Linus Torvalds 已提交
756 757 758
					      (1 << FragCountShift));

	/* TxDMAPollNow */
759
	dw32(DMACtrl, dr32(DMACtrl) | 0x00001000);
L
Linus Torvalds 已提交
760
	/* Schedule ISR */
761
	dw32(CountDown, 10000);
L
Linus Torvalds 已提交
762 763 764 765 766 767 768 769 770
	np->cur_tx = (np->cur_tx + 1) % TX_RING_SIZE;
	if ((np->cur_tx - np->old_tx + TX_RING_SIZE) % TX_RING_SIZE
			< TX_QUEUE_LEN - 1 && np->speed != 10) {
		/* do nothing */
	} else if (!netif_queue_stopped(dev)) {
		netif_stop_queue (dev);
	}

	/* The first TFDListPtr */
771 772 773 774
	if (!dr32(TFDListPtr0)) {
		dw32(TFDListPtr0, np->tx_ring_dma +
		     entry * sizeof (struct netdev_desc));
		dw32(TFDListPtr1, 0);
L
Linus Torvalds 已提交
775
	}
776

E
Eric Dumazet 已提交
777
	return NETDEV_TX_OK;
L
Linus Torvalds 已提交
778 779 780
}

static irqreturn_t
781
rio_interrupt (int irq, void *dev_instance)
L
Linus Torvalds 已提交
782 783
{
	struct net_device *dev = dev_instance;
784 785
	struct netdev_private *np = netdev_priv(dev);
	void __iomem *ioaddr = np->ioaddr;
L
Linus Torvalds 已提交
786 787 788 789 790
	unsigned int_status;
	int cnt = max_intrloop;
	int handled = 0;

	while (1) {
791 792
		int_status = dr16(IntStatus);
		dw16(IntStatus, int_status);
L
Linus Torvalds 已提交
793 794 795 796 797 798 799 800 801 802
		int_status &= DEFAULT_INTR;
		if (int_status == 0 || --cnt < 0)
			break;
		handled = 1;
		/* Processing received packets */
		if (int_status & RxDMAComplete)
			receive_packet (dev);
		/* TxDMAComplete interrupt */
		if ((int_status & (TxDMAComplete|IntRequested))) {
			int tx_status;
803
			tx_status = dr32(TxStatus);
L
Linus Torvalds 已提交
804 805 806
			if (tx_status & 0x01)
				tx_error (dev, tx_status);
			/* Free used tx skbuffs */
807
			rio_free_tx (dev, 1);
L
Linus Torvalds 已提交
808 809 810 811 812 813 814 815
		}

		/* Handle uncommon events */
		if (int_status &
		    (HostError | LinkEvent | UpdateStats))
			rio_error (dev, int_status);
	}
	if (np->cur_tx != np->old_tx)
816
		dw32(CountDown, 100);
L
Linus Torvalds 已提交
817 818 819
	return IRQ_RETVAL(handled);
}

820 821
static void
rio_free_tx (struct net_device *dev, int irq)
L
Linus Torvalds 已提交
822 823 824 825 826
{
	struct netdev_private *np = netdev_priv(dev);
	int entry = np->old_tx % TX_RING_SIZE;
	int tx_use = 0;
	unsigned long flag = 0;
827

L
Linus Torvalds 已提交
828 829 830 831
	if (irq)
		spin_lock(&np->tx_lock);
	else
		spin_lock_irqsave(&np->tx_lock, flag);
832

L
Linus Torvalds 已提交
833 834 835 836
	/* Free used tx skbuffs */
	while (entry != np->cur_tx) {
		struct sk_buff *skb;

A
Al Viro 已提交
837
		if (!(np->tx_ring[entry].status & cpu_to_le64(TFDDone)))
L
Linus Torvalds 已提交
838 839 840
			break;
		skb = np->tx_skbuff[entry];
		pci_unmap_single (np->pdev,
A
Al Viro 已提交
841
				  desc_to_dma(&np->tx_ring[entry]),
L
Linus Torvalds 已提交
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
				  skb->len, PCI_DMA_TODEVICE);
		if (irq)
			dev_kfree_skb_irq (skb);
		else
			dev_kfree_skb (skb);

		np->tx_skbuff[entry] = NULL;
		entry = (entry + 1) % TX_RING_SIZE;
		tx_use++;
	}
	if (irq)
		spin_unlock(&np->tx_lock);
	else
		spin_unlock_irqrestore(&np->tx_lock, flag);
	np->old_tx = entry;

858
	/* If the ring is no longer full, clear tx_full and
L
Linus Torvalds 已提交
859 860 861
	   call netif_wake_queue() */

	if (netif_queue_stopped(dev) &&
862
	    ((np->cur_tx - np->old_tx + TX_RING_SIZE) % TX_RING_SIZE
L
Linus Torvalds 已提交
863 864 865 866 867 868 869 870
	    < TX_QUEUE_LEN - 1 || np->speed == 10)) {
		netif_wake_queue (dev);
	}
}

static void
tx_error (struct net_device *dev, int tx_status)
{
871 872
	struct netdev_private *np = netdev_priv(dev);
	void __iomem *ioaddr = np->ioaddr;
L
Linus Torvalds 已提交
873 874 875 876 877 878 879 880 881 882
	int frame_id;
	int i;

	frame_id = (tx_status & 0xffff0000);
	printk (KERN_ERR "%s: Transmit error, TxStatus %4.4x, FrameId %d.\n",
		dev->name, tx_status, frame_id);
	np->stats.tx_errors++;
	/* Ttransmit Underrun */
	if (tx_status & 0x10) {
		np->stats.tx_fifo_errors++;
883
		dw16(TxStartThresh, dr16(TxStartThresh) + 0x10);
L
Linus Torvalds 已提交
884
		/* Transmit Underrun need to set TxReset, DMARest, FIFOReset */
885 886
		dw16(ASICCtrl + 2,
		     TxReset | DMAReset | FIFOReset | NetworkReset);
L
Linus Torvalds 已提交
887 888
		/* Wait for ResetBusy bit clear */
		for (i = 50; i > 0; i--) {
889
			if (!(dr16(ASICCtrl + 2) & ResetBusy))
L
Linus Torvalds 已提交
890 891 892
				break;
			mdelay (1);
		}
893
		rio_set_led_mode(dev);
L
Linus Torvalds 已提交
894 895
		rio_free_tx (dev, 1);
		/* Reset TFDListPtr */
896 897 898
		dw32(TFDListPtr0, np->tx_ring_dma +
		     np->old_tx * sizeof (struct netdev_desc));
		dw32(TFDListPtr1, 0);
L
Linus Torvalds 已提交
899 900 901 902 903 904 905

		/* Let TxStartThresh stay default value */
	}
	/* Late Collision */
	if (tx_status & 0x04) {
		np->stats.tx_fifo_errors++;
		/* TxReset and clear FIFO */
906
		dw16(ASICCtrl + 2, TxReset | FIFOReset);
L
Linus Torvalds 已提交
907 908
		/* Wait reset done */
		for (i = 50; i > 0; i--) {
909
			if (!(dr16(ASICCtrl + 2) & ResetBusy))
L
Linus Torvalds 已提交
910 911 912
				break;
			mdelay (1);
		}
913
		rio_set_led_mode(dev);
L
Linus Torvalds 已提交
914 915 916
		/* Let TxStartThresh stay default value */
	}
	/* Maximum Collisions */
917 918
#ifdef ETHER_STATS
	if (tx_status & 0x08)
L
Linus Torvalds 已提交
919 920
		np->stats.collisions16++;
#else
921
	if (tx_status & 0x08)
L
Linus Torvalds 已提交
922 923 924
		np->stats.collisions++;
#endif
	/* Restart the Tx */
925
	dw32(MACCtrl, dr16(MACCtrl) | TxEnable);
L
Linus Torvalds 已提交
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
}

static int
receive_packet (struct net_device *dev)
{
	struct netdev_private *np = netdev_priv(dev);
	int entry = np->cur_rx % RX_RING_SIZE;
	int cnt = 30;

	/* If RFDDone, FrameStart and FrameEnd set, there is a new packet in. */
	while (1) {
		struct netdev_desc *desc = &np->rx_ring[entry];
		int pkt_len;
		u64 frame_status;

A
Al Viro 已提交
941 942 943
		if (!(desc->status & cpu_to_le64(RFDDone)) ||
		    !(desc->status & cpu_to_le64(FrameStart)) ||
		    !(desc->status & cpu_to_le64(FrameEnd)))
L
Linus Torvalds 已提交
944 945 946
			break;

		/* Chip omits the CRC. */
A
Al Viro 已提交
947 948
		frame_status = le64_to_cpu(desc->status);
		pkt_len = frame_status & 0xffff;
L
Linus Torvalds 已提交
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
		if (--cnt < 0)
			break;
		/* Update rx error statistics, drop packet. */
		if (frame_status & RFS_Errors) {
			np->stats.rx_errors++;
			if (frame_status & (RxRuntFrame | RxLengthError))
				np->stats.rx_length_errors++;
			if (frame_status & RxFCSError)
				np->stats.rx_crc_errors++;
			if (frame_status & RxAlignmentError && np->speed != 1000)
				np->stats.rx_frame_errors++;
			if (frame_status & RxFIFOOverrun)
	 			np->stats.rx_fifo_errors++;
		} else {
			struct sk_buff *skb;

			/* Small skbuffs for short packets */
			if (pkt_len > copy_thresh) {
J
Jon Mason 已提交
967
				pci_unmap_single (np->pdev,
A
Al Viro 已提交
968
						  desc_to_dma(desc),
L
Linus Torvalds 已提交
969 970 971 972
						  np->rx_buf_sz,
						  PCI_DMA_FROMDEVICE);
				skb_put (skb = np->rx_skbuff[entry], pkt_len);
				np->rx_skbuff[entry] = NULL;
973
			} else if ((skb = netdev_alloc_skb_ip_align(dev, pkt_len))) {
L
Linus Torvalds 已提交
974
				pci_dma_sync_single_for_cpu(np->pdev,
A
Al Viro 已提交
975
							    desc_to_dma(desc),
L
Linus Torvalds 已提交
976 977
							    np->rx_buf_sz,
							    PCI_DMA_FROMDEVICE);
978
				skb_copy_to_linear_data (skb,
979
						  np->rx_skbuff[entry]->data,
980
						  pkt_len);
L
Linus Torvalds 已提交
981 982
				skb_put (skb, pkt_len);
				pci_dma_sync_single_for_device(np->pdev,
A
Al Viro 已提交
983
							       desc_to_dma(desc),
L
Linus Torvalds 已提交
984 985 986 987
							       np->rx_buf_sz,
							       PCI_DMA_FROMDEVICE);
			}
			skb->protocol = eth_type_trans (skb, dev);
988
#if 0
L
Linus Torvalds 已提交
989
			/* Checksum done by hw, but csum value unavailable. */
990
			if (np->pdev->pci_rev_id >= 0x0c &&
L
Linus Torvalds 已提交
991 992
				!(frame_status & (TCPError | UDPError | IPError))) {
				skb->ip_summed = CHECKSUM_UNNECESSARY;
993
			}
L
Linus Torvalds 已提交
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
#endif
			netif_rx (skb);
		}
		entry = (entry + 1) % RX_RING_SIZE;
	}
	spin_lock(&np->rx_lock);
	np->cur_rx = entry;
	/* Re-allocate skbuffs to fill the descriptor ring */
	entry = np->old_rx;
	while (entry != np->cur_rx) {
		struct sk_buff *skb;
		/* Dropped packets don't need to re-allocate */
		if (np->rx_skbuff[entry] == NULL) {
1007
			skb = netdev_alloc_skb_ip_align(dev, np->rx_buf_sz);
L
Linus Torvalds 已提交
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
			if (skb == NULL) {
				np->rx_ring[entry].fraginfo = 0;
				printk (KERN_INFO
					"%s: receive_packet: "
					"Unable to re-allocate Rx skbuff.#%d\n",
					dev->name, entry);
				break;
			}
			np->rx_skbuff[entry] = skb;
			np->rx_ring[entry].fraginfo =
			    cpu_to_le64 (pci_map_single
1019
					 (np->pdev, skb->data, np->rx_buf_sz,
L
Linus Torvalds 已提交
1020 1021 1022
					  PCI_DMA_FROMDEVICE));
		}
		np->rx_ring[entry].fraginfo |=
A
Al Viro 已提交
1023
		    cpu_to_le64((u64)np->rx_buf_sz << 48);
L
Linus Torvalds 已提交
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
		np->rx_ring[entry].status = 0;
		entry = (entry + 1) % RX_RING_SIZE;
	}
	np->old_rx = entry;
	spin_unlock(&np->rx_lock);
	return 0;
}

static void
rio_error (struct net_device *dev, int int_status)
{
	struct netdev_private *np = netdev_priv(dev);
1036
	void __iomem *ioaddr = np->ioaddr;
L
Linus Torvalds 已提交
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
	u16 macctrl;

	/* Link change event */
	if (int_status & LinkEvent) {
		if (mii_wait_link (dev, 10) == 0) {
			printk (KERN_INFO "%s: Link up\n", dev->name);
			if (np->phy_media)
				mii_get_media_pcs (dev);
			else
				mii_get_media (dev);
			if (np->speed == 1000)
				np->tx_coalesce = tx_coalesce;
1049
			else
L
Linus Torvalds 已提交
1050 1051 1052 1053
				np->tx_coalesce = 1;
			macctrl = 0;
			macctrl |= (np->vlan) ? AutoVLANuntagging : 0;
			macctrl |= (np->full_duplex) ? DuplexSelect : 0;
1054
			macctrl |= (np->tx_flow) ?
L
Linus Torvalds 已提交
1055
				TxFlowControlEnable : 0;
1056
			macctrl |= (np->rx_flow) ?
L
Linus Torvalds 已提交
1057
				RxFlowControlEnable : 0;
1058
			dw16(MACCtrl, macctrl);
L
Linus Torvalds 已提交
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
			np->link_status = 1;
			netif_carrier_on(dev);
		} else {
			printk (KERN_INFO "%s: Link off\n", dev->name);
			np->link_status = 0;
			netif_carrier_off(dev);
		}
	}

	/* UpdateStats statistics registers */
	if (int_status & UpdateStats) {
		get_stats (dev);
	}

1073
	/* PCI Error, a catastronphic error related to the bus interface
L
Linus Torvalds 已提交
1074 1075 1076 1077
	   occurs, set GlobalReset and HostReset to reset. */
	if (int_status & HostError) {
		printk (KERN_ERR "%s: HostError! IntStatus %4.4x.\n",
			dev->name, int_status);
1078
		dw16(ASICCtrl + 2, GlobalReset | HostReset);
L
Linus Torvalds 已提交
1079
		mdelay (500);
1080
		rio_set_led_mode(dev);
L
Linus Torvalds 已提交
1081 1082 1083 1084 1085 1086 1087
	}
}

static struct net_device_stats *
get_stats (struct net_device *dev)
{
	struct netdev_private *np = netdev_priv(dev);
1088
	void __iomem *ioaddr = np->ioaddr;
L
Linus Torvalds 已提交
1089 1090 1091 1092 1093 1094 1095
#ifdef MEM_MAPPING
	int i;
#endif
	unsigned int stat_reg;

	/* All statistics registers need to be acknowledged,
	   else statistic overflow could cause problems */
1096

1097 1098 1099 1100
	np->stats.rx_packets += dr32(FramesRcvOk);
	np->stats.tx_packets += dr32(FramesXmtOk);
	np->stats.rx_bytes += dr32(OctetRcvOk);
	np->stats.tx_bytes += dr32(OctetXmtOk);
L
Linus Torvalds 已提交
1101

1102 1103 1104
	np->stats.multicast = dr32(McstFramesRcvdOk);
	np->stats.collisions += dr32(SingleColFrames)
			     +  dr32(MultiColFrames);
1105

L
Linus Torvalds 已提交
1106
	/* detailed tx errors */
1107
	stat_reg = dr16(FramesAbortXSColls);
L
Linus Torvalds 已提交
1108 1109 1110
	np->stats.tx_aborted_errors += stat_reg;
	np->stats.tx_errors += stat_reg;

1111
	stat_reg = dr16(CarrierSenseErrors);
L
Linus Torvalds 已提交
1112 1113 1114 1115
	np->stats.tx_carrier_errors += stat_reg;
	np->stats.tx_errors += stat_reg;

	/* Clear all other statistic register. */
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
	dr32(McstOctetXmtOk);
	dr16(BcstFramesXmtdOk);
	dr32(McstFramesXmtdOk);
	dr16(BcstFramesRcvdOk);
	dr16(MacControlFramesRcvd);
	dr16(FrameTooLongErrors);
	dr16(InRangeLengthErrors);
	dr16(FramesCheckSeqErrors);
	dr16(FramesLostRxErrors);
	dr32(McstOctetXmtOk);
	dr32(BcstOctetXmtOk);
	dr32(McstFramesXmtdOk);
	dr32(FramesWDeferredXmt);
	dr32(LateCollisions);
	dr16(BcstFramesXmtdOk);
	dr16(MacControlFramesXmtd);
	dr16(FramesWEXDeferal);
L
Linus Torvalds 已提交
1133 1134 1135

#ifdef MEM_MAPPING
	for (i = 0x100; i <= 0x150; i += 4)
1136
		dr32(i);
L
Linus Torvalds 已提交
1137
#endif
1138 1139 1140 1141 1142
	dr16(TxJumboFrames);
	dr16(RxJumboFrames);
	dr16(TCPCheckSumErrors);
	dr16(UDPCheckSumErrors);
	dr16(IPCheckSumErrors);
L
Linus Torvalds 已提交
1143 1144 1145 1146 1147 1148
	return &np->stats;
}

static int
clear_stats (struct net_device *dev)
{
1149 1150
	struct netdev_private *np = netdev_priv(dev);
	void __iomem *ioaddr = np->ioaddr;
L
Linus Torvalds 已提交
1151 1152
#ifdef MEM_MAPPING
	int i;
1153
#endif
L
Linus Torvalds 已提交
1154 1155 1156

	/* All statistics registers need to be acknowledged,
	   else statistic overflow could cause problems */
1157 1158 1159 1160 1161 1162 1163 1164 1165
	dr32(FramesRcvOk);
	dr32(FramesXmtOk);
	dr32(OctetRcvOk);
	dr32(OctetXmtOk);

	dr32(McstFramesRcvdOk);
	dr32(SingleColFrames);
	dr32(MultiColFrames);
	dr32(LateCollisions);
1166
	/* detailed rx errors */
1167 1168 1169 1170
	dr16(FrameTooLongErrors);
	dr16(InRangeLengthErrors);
	dr16(FramesCheckSeqErrors);
	dr16(FramesLostRxErrors);
L
Linus Torvalds 已提交
1171 1172

	/* detailed tx errors */
1173 1174
	dr16(FramesAbortXSColls);
	dr16(CarrierSenseErrors);
L
Linus Torvalds 已提交
1175 1176

	/* Clear all other statistic register. */
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
	dr32(McstOctetXmtOk);
	dr16(BcstFramesXmtdOk);
	dr32(McstFramesXmtdOk);
	dr16(BcstFramesRcvdOk);
	dr16(MacControlFramesRcvd);
	dr32(McstOctetXmtOk);
	dr32(BcstOctetXmtOk);
	dr32(McstFramesXmtdOk);
	dr32(FramesWDeferredXmt);
	dr16(BcstFramesXmtdOk);
	dr16(MacControlFramesXmtd);
	dr16(FramesWEXDeferal);
L
Linus Torvalds 已提交
1189 1190
#ifdef MEM_MAPPING
	for (i = 0x100; i <= 0x150; i += 4)
1191
		dr32(i);
1192
#endif
1193 1194 1195 1196 1197
	dr16(TxJumboFrames);
	dr16(RxJumboFrames);
	dr16(TCPCheckSumErrors);
	dr16(UDPCheckSumErrors);
	dr16(IPCheckSumErrors);
L
Linus Torvalds 已提交
1198 1199 1200 1201
	return 0;
}


1202
static int
L
Linus Torvalds 已提交
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
change_mtu (struct net_device *dev, int new_mtu)
{
	struct netdev_private *np = netdev_priv(dev);
	int max = (np->jumbo) ? MAX_JUMBO : 1536;

	if ((new_mtu < 68) || (new_mtu > max)) {
		return -EINVAL;
	}

	dev->mtu = new_mtu;

	return 0;
}

static void
set_multicast (struct net_device *dev)
{
1220 1221
	struct netdev_private *np = netdev_priv(dev);
	void __iomem *ioaddr = np->ioaddr;
L
Linus Torvalds 已提交
1222 1223
	u32 hash_table[2];
	u16 rx_mode = 0;
1224

L
Linus Torvalds 已提交
1225 1226
	hash_table[0] = hash_table[1] = 0;
	/* RxFlowcontrol DA: 01-80-C2-00-00-01. Hash index=0x39 */
A
Al Viro 已提交
1227
	hash_table[1] |= 0x02000000;
L
Linus Torvalds 已提交
1228 1229 1230
	if (dev->flags & IFF_PROMISC) {
		/* Receive all frames promiscuously. */
		rx_mode = ReceiveAllFrames;
1231
	} else if ((dev->flags & IFF_ALLMULTI) ||
1232
			(netdev_mc_count(dev) > multicast_filter_limit)) {
L
Linus Torvalds 已提交
1233 1234
		/* Receive broadcast and multicast frames */
		rx_mode = ReceiveBroadcast | ReceiveMulticast | ReceiveUnicast;
1235
	} else if (!netdev_mc_empty(dev)) {
1236
		struct netdev_hw_addr *ha;
1237
		/* Receive broadcast frames and multicast frames filtering
L
Linus Torvalds 已提交
1238 1239 1240
		   by Hashtable */
		rx_mode =
		    ReceiveBroadcast | ReceiveMulticastHash | ReceiveUnicast;
1241
		netdev_for_each_mc_addr(ha, dev) {
L
Linus Torvalds 已提交
1242
			int bit, index = 0;
1243
			int crc = ether_crc_le(ETH_ALEN, ha->addr);
L
Linus Torvalds 已提交
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
			/* The inverted high significant 6 bits of CRC are
			   used as an index to hashtable */
			for (bit = 0; bit < 6; bit++)
				if (crc & (1 << (31 - bit)))
					index |= (1 << bit);
			hash_table[index / 32] |= (1 << (index % 32));
		}
	} else {
		rx_mode = ReceiveBroadcast | ReceiveUnicast;
	}
	if (np->vlan) {
		/* ReceiveVLANMatch field in ReceiveMode */
		rx_mode |= ReceiveVLANMatch;
	}

1259 1260 1261
	dw32(HashTable0, hash_table[0]);
	dw32(HashTable1, hash_table[1]);
	dw16(ReceiveMode, rx_mode);
L
Linus Torvalds 已提交
1262 1263 1264 1265 1266
}

static void rio_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
{
	struct netdev_private *np = netdev_priv(dev);
1267 1268 1269 1270

	strlcpy(info->driver, "dl2k", sizeof(info->driver));
	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
	strlcpy(info->bus_info, pci_name(np->pdev), sizeof(info->bus_info));
1271
}
L
Linus Torvalds 已提交
1272 1273 1274 1275 1276 1277 1278 1279 1280

static int rio_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
{
	struct netdev_private *np = netdev_priv(dev);
	if (np->phy_media) {
		/* fiber device */
		cmd->supported = SUPPORTED_Autoneg | SUPPORTED_FIBRE;
		cmd->advertising= ADVERTISED_Autoneg | ADVERTISED_FIBRE;
		cmd->port = PORT_FIBRE;
1281
		cmd->transceiver = XCVR_INTERNAL;
L
Linus Torvalds 已提交
1282 1283
	} else {
		/* copper device */
1284
		cmd->supported = SUPPORTED_10baseT_Half |
L
Linus Torvalds 已提交
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
			SUPPORTED_10baseT_Full | SUPPORTED_100baseT_Half
			| SUPPORTED_100baseT_Full | SUPPORTED_1000baseT_Full |
			SUPPORTED_Autoneg | SUPPORTED_MII;
		cmd->advertising = ADVERTISED_10baseT_Half |
			ADVERTISED_10baseT_Full | ADVERTISED_100baseT_Half |
			ADVERTISED_100baseT_Full | ADVERTISED_1000baseT_Full|
			ADVERTISED_Autoneg | ADVERTISED_MII;
		cmd->port = PORT_MII;
		cmd->transceiver = XCVR_INTERNAL;
	}
1295
	if ( np->link_status ) {
1296
		ethtool_cmd_speed_set(cmd, np->speed);
L
Linus Torvalds 已提交
1297 1298
		cmd->duplex = np->full_duplex ? DUPLEX_FULL : DUPLEX_HALF;
	} else {
1299 1300
		ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
		cmd->duplex = DUPLEX_UNKNOWN;
L
Linus Torvalds 已提交
1301 1302 1303 1304 1305
	}
	if ( np->an_enable)
		cmd->autoneg = AUTONEG_ENABLE;
	else
		cmd->autoneg = AUTONEG_DISABLE;
1306

L
Linus Torvalds 已提交
1307
	cmd->phy_address = np->phy_addr;
1308
	return 0;
L
Linus Torvalds 已提交
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
}

static int rio_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
{
	struct netdev_private *np = netdev_priv(dev);
	netif_carrier_off(dev);
	if (cmd->autoneg == AUTONEG_ENABLE) {
		if (np->an_enable)
			return 0;
		else {
			np->an_enable = 1;
			mii_set_media(dev);
1321 1322
			return 0;
		}
L
Linus Torvalds 已提交
1323 1324 1325
	} else {
		np->an_enable = 0;
		if (np->speed == 1000) {
1326
			ethtool_cmd_speed_set(cmd, SPEED_100);
L
Linus Torvalds 已提交
1327 1328 1329
			cmd->duplex = DUPLEX_FULL;
			printk("Warning!! Can't disable Auto negotiation in 1000Mbps, change to Manual 100Mbps, Full duplex.\n");
		}
1330 1331
		switch (ethtool_cmd_speed(cmd)) {
		case SPEED_10:
L
Linus Torvalds 已提交
1332
			np->speed = 10;
1333
			np->full_duplex = (cmd->duplex == DUPLEX_FULL);
L
Linus Torvalds 已提交
1334
			break;
1335
		case SPEED_100:
L
Linus Torvalds 已提交
1336
			np->speed = 100;
1337
			np->full_duplex = (cmd->duplex == DUPLEX_FULL);
L
Linus Torvalds 已提交
1338
			break;
1339
		case SPEED_1000: /* not supported */
L
Linus Torvalds 已提交
1340
		default:
1341
			return -EINVAL;
L
Linus Torvalds 已提交
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
		}
		mii_set_media(dev);
	}
	return 0;
}

static u32 rio_get_link(struct net_device *dev)
{
	struct netdev_private *np = netdev_priv(dev);
	return np->link_status;
}

1354
static const struct ethtool_ops ethtool_ops = {
L
Linus Torvalds 已提交
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
	.get_drvinfo = rio_get_drvinfo,
	.get_settings = rio_get_settings,
	.set_settings = rio_set_settings,
	.get_link = rio_get_link,
};

static int
rio_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
{
	int phy_addr;
	struct netdev_private *np = netdev_priv(dev);
J
Jeff Mahoney 已提交
1366
	struct mii_ioctl_data *miidata = if_mii(rq);
L
Linus Torvalds 已提交
1367 1368 1369

	phy_addr = np->phy_addr;
	switch (cmd) {
J
Jeff Mahoney 已提交
1370 1371
	case SIOCGMIIPHY:
		miidata->phy_id = phy_addr;
L
Linus Torvalds 已提交
1372
		break;
J
Jeff Mahoney 已提交
1373 1374
	case SIOCGMIIREG:
		miidata->val_out = mii_read (dev, phy_addr, miidata->reg_num);
L
Linus Torvalds 已提交
1375
		break;
J
Jeff Mahoney 已提交
1376 1377 1378 1379
	case SIOCSMIIREG:
		if (!capable(CAP_NET_ADMIN))
			return -EPERM;
		mii_write (dev, phy_addr, miidata->reg_num, miidata->val_in);
L
Linus Torvalds 已提交
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
		break;
	default:
		return -EOPNOTSUPP;
	}
	return 0;
}

#define EEP_READ 0x0200
#define EEP_BUSY 0x8000
/* Read the EEPROM word */
/* We use I/O instruction to read/write eeprom to avoid fail on some machines */
1391
static int read_eeprom(struct netdev_private *np, int eep_addr)
L
Linus Torvalds 已提交
1392
{
1393
	void __iomem *ioaddr = np->eeprom_addr;
L
Linus Torvalds 已提交
1394
	int i = 1000;
1395 1396

	dw16(EepromCtrl, EEP_READ | (eep_addr & 0xff));
L
Linus Torvalds 已提交
1397
	while (i-- > 0) {
1398 1399
		if (!(dr16(EepromCtrl) & EEP_BUSY))
			return dr16(EepromData);
L
Linus Torvalds 已提交
1400 1401 1402 1403 1404 1405 1406 1407 1408
	}
	return 0;
}

enum phy_ctrl_bits {
	MII_READ = 0x00, MII_CLK = 0x01, MII_DATA1 = 0x02, MII_WRITE = 0x04,
	MII_DUPLEX = 0x08,
};

1409
#define mii_delay() dr8(PhyCtrl)
L
Linus Torvalds 已提交
1410 1411 1412
static void
mii_sendbit (struct net_device *dev, u32 data)
{
1413 1414 1415 1416 1417
	struct netdev_private *np = netdev_priv(dev);
	void __iomem *ioaddr = np->ioaddr;

	data = ((data) ? MII_DATA1 : 0) | (dr8(PhyCtrl) & 0xf8) | MII_WRITE;
	dw8(PhyCtrl, data);
L
Linus Torvalds 已提交
1418
	mii_delay ();
1419
	dw8(PhyCtrl, data | MII_CLK);
L
Linus Torvalds 已提交
1420 1421 1422 1423 1424 1425
	mii_delay ();
}

static int
mii_getbit (struct net_device *dev)
{
1426 1427
	struct netdev_private *np = netdev_priv(dev);
	void __iomem *ioaddr = np->ioaddr;
L
Linus Torvalds 已提交
1428 1429
	u8 data;

1430 1431
	data = (dr8(PhyCtrl) & 0xf8) | MII_READ;
	dw8(PhyCtrl, data);
L
Linus Torvalds 已提交
1432
	mii_delay ();
1433
	dw8(PhyCtrl, data | MII_CLK);
L
Linus Torvalds 已提交
1434
	mii_delay ();
1435
	return (dr8(PhyCtrl) >> 1) & 1;
L
Linus Torvalds 已提交
1436 1437 1438 1439 1440 1441
}

static void
mii_send_bits (struct net_device *dev, u32 data, int len)
{
	int i;
1442

L
Linus Torvalds 已提交
1443 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 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
	for (i = len - 1; i >= 0; i--) {
		mii_sendbit (dev, data & (1 << i));
	}
}

static int
mii_read (struct net_device *dev, int phy_addr, int reg_num)
{
	u32 cmd;
	int i;
	u32 retval = 0;

	/* Preamble */
	mii_send_bits (dev, 0xffffffff, 32);
	/* ST(2), OP(2), ADDR(5), REG#(5), TA(2), Data(16) total 32 bits */
	/* ST,OP = 0110'b for read operation */
	cmd = (0x06 << 10 | phy_addr << 5 | reg_num);
	mii_send_bits (dev, cmd, 14);
	/* Turnaround */
	if (mii_getbit (dev))
		goto err_out;
	/* Read data */
	for (i = 0; i < 16; i++) {
		retval |= mii_getbit (dev);
		retval <<= 1;
	}
	/* End cycle */
	mii_getbit (dev);
	return (retval >> 1) & 0xffff;

      err_out:
	return 0;
}
static int
mii_write (struct net_device *dev, int phy_addr, int reg_num, u16 data)
{
	u32 cmd;

	/* Preamble */
	mii_send_bits (dev, 0xffffffff, 32);
	/* ST(2), OP(2), ADDR(5), REG#(5), TA(2), Data(16) total 32 bits */
	/* ST,OP,AAAAA,RRRRR,TA = 0101xxxxxxxxxx10'b = 0x5002 for write */
	cmd = (0x5002 << 16) | (phy_addr << 23) | (reg_num << 18) | data;
	mii_send_bits (dev, cmd, 32);
	/* End cycle */
	mii_getbit (dev);
	return 0;
}
static int
mii_wait_link (struct net_device *dev, int wait)
{
A
Al Viro 已提交
1494
	__u16 bmsr;
L
Linus Torvalds 已提交
1495 1496 1497 1498 1499 1500 1501
	int phy_addr;
	struct netdev_private *np;

	np = netdev_priv(dev);
	phy_addr = np->phy_addr;

	do {
A
Al Viro 已提交
1502
		bmsr = mii_read (dev, phy_addr, MII_BMSR);
1503
		if (bmsr & BMSR_LSTATUS)
L
Linus Torvalds 已提交
1504 1505 1506 1507 1508 1509 1510 1511
			return 0;
		mdelay (1);
	} while (--wait > 0);
	return -1;
}
static int
mii_get_media (struct net_device *dev)
{
A
Al Viro 已提交
1512
	__u16 negotiate;
A
Al Viro 已提交
1513
	__u16 bmsr;
A
Al Viro 已提交
1514 1515
	__u16 mscr;
	__u16 mssr;
L
Linus Torvalds 已提交
1516 1517 1518 1519 1520 1521
	int phy_addr;
	struct netdev_private *np;

	np = netdev_priv(dev);
	phy_addr = np->phy_addr;

A
Al Viro 已提交
1522
	bmsr = mii_read (dev, phy_addr, MII_BMSR);
L
Linus Torvalds 已提交
1523
	if (np->an_enable) {
1524
		if (!(bmsr & BMSR_ANEGCOMPLETE)) {
L
Linus Torvalds 已提交
1525 1526 1527
			/* Auto-Negotiation not completed */
			return -1;
		}
1528 1529 1530 1531 1532
		negotiate = mii_read (dev, phy_addr, MII_ADVERTISE) &
			mii_read (dev, phy_addr, MII_LPA);
		mscr = mii_read (dev, phy_addr, MII_CTRL1000);
		mssr = mii_read (dev, phy_addr, MII_STAT1000);
		if (mscr & ADVERTISE_1000FULL && mssr & LPA_1000FULL) {
L
Linus Torvalds 已提交
1533 1534 1535
			np->speed = 1000;
			np->full_duplex = 1;
			printk (KERN_INFO "Auto 1000 Mbps, Full duplex\n");
1536
		} else if (mscr & ADVERTISE_1000HALF && mssr & LPA_1000HALF) {
L
Linus Torvalds 已提交
1537 1538 1539
			np->speed = 1000;
			np->full_duplex = 0;
			printk (KERN_INFO "Auto 1000 Mbps, Half duplex\n");
1540
		} else if (negotiate & ADVERTISE_100FULL) {
L
Linus Torvalds 已提交
1541 1542 1543
			np->speed = 100;
			np->full_duplex = 1;
			printk (KERN_INFO "Auto 100 Mbps, Full duplex\n");
1544
		} else if (negotiate & ADVERTISE_100HALF) {
L
Linus Torvalds 已提交
1545 1546 1547
			np->speed = 100;
			np->full_duplex = 0;
			printk (KERN_INFO "Auto 100 Mbps, Half duplex\n");
1548
		} else if (negotiate & ADVERTISE_10FULL) {
L
Linus Torvalds 已提交
1549 1550 1551
			np->speed = 10;
			np->full_duplex = 1;
			printk (KERN_INFO "Auto 10 Mbps, Full duplex\n");
1552
		} else if (negotiate & ADVERTISE_10HALF) {
L
Linus Torvalds 已提交
1553 1554 1555 1556
			np->speed = 10;
			np->full_duplex = 0;
			printk (KERN_INFO "Auto 10 Mbps, Half duplex\n");
		}
1557
		if (negotiate & ADVERTISE_PAUSE_CAP) {
L
Linus Torvalds 已提交
1558 1559
			np->tx_flow &= 1;
			np->rx_flow &= 1;
1560
		} else if (negotiate & ADVERTISE_PAUSE_ASYM) {
L
Linus Torvalds 已提交
1561 1562 1563 1564 1565
			np->tx_flow = 0;
			np->rx_flow &= 1;
		}
		/* else tx_flow, rx_flow = user select  */
	} else {
A
Al Viro 已提交
1566
		__u16 bmcr = mii_read (dev, phy_addr, MII_BMCR);
1567 1568
		switch (bmcr & (BMCR_SPEED100 | BMCR_SPEED1000)) {
		case BMCR_SPEED1000:
A
Al Viro 已提交
1569 1570
			printk (KERN_INFO "Operating at 1000 Mbps, ");
			break;
1571
		case BMCR_SPEED100:
L
Linus Torvalds 已提交
1572
			printk (KERN_INFO "Operating at 100 Mbps, ");
A
Al Viro 已提交
1573 1574
			break;
		case 0:
L
Linus Torvalds 已提交
1575 1576
			printk (KERN_INFO "Operating at 10 Mbps, ");
		}
1577
		if (bmcr & BMCR_FULLDPLX) {
1578
			printk (KERN_CONT "Full duplex\n");
L
Linus Torvalds 已提交
1579
		} else {
1580
			printk (KERN_CONT "Half duplex\n");
L
Linus Torvalds 已提交
1581 1582
		}
	}
1583
	if (np->tx_flow)
L
Linus Torvalds 已提交
1584
		printk(KERN_INFO "Enable Tx Flow Control\n");
1585
	else
L
Linus Torvalds 已提交
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
		printk(KERN_INFO "Disable Tx Flow Control\n");
	if (np->rx_flow)
		printk(KERN_INFO "Enable Rx Flow Control\n");
	else
		printk(KERN_INFO "Disable Rx Flow Control\n");

	return 0;
}

static int
mii_set_media (struct net_device *dev)
{
A
Al Viro 已提交
1598
	__u16 pscr;
A
Al Viro 已提交
1599
	__u16 bmcr;
A
Al Viro 已提交
1600
	__u16 bmsr;
A
Al Viro 已提交
1601
	__u16 anar;
L
Linus Torvalds 已提交
1602 1603 1604 1605 1606 1607 1608 1609
	int phy_addr;
	struct netdev_private *np;
	np = netdev_priv(dev);
	phy_addr = np->phy_addr;

	/* Does user set speed? */
	if (np->an_enable) {
		/* Advertise capabilities */
A
Al Viro 已提交
1610
		bmsr = mii_read (dev, phy_addr, MII_BMSR);
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
		anar = mii_read (dev, phy_addr, MII_ADVERTISE) &
			~(ADVERTISE_100FULL | ADVERTISE_10FULL |
			  ADVERTISE_100HALF | ADVERTISE_10HALF |
			  ADVERTISE_100BASE4);
		if (bmsr & BMSR_100FULL)
			anar |= ADVERTISE_100FULL;
		if (bmsr & BMSR_100HALF)
			anar |= ADVERTISE_100HALF;
		if (bmsr & BMSR_100BASE4)
			anar |= ADVERTISE_100BASE4;
		if (bmsr & BMSR_10FULL)
			anar |= ADVERTISE_10FULL;
		if (bmsr & BMSR_10HALF)
			anar |= ADVERTISE_10HALF;
		anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
		mii_write (dev, phy_addr, MII_ADVERTISE, anar);
L
Linus Torvalds 已提交
1627 1628

		/* Enable Auto crossover */
A
Al Viro 已提交
1629 1630 1631
		pscr = mii_read (dev, phy_addr, MII_PHY_SCR);
		pscr |= 3 << 5;	/* 11'b */
		mii_write (dev, phy_addr, MII_PHY_SCR, pscr);
1632

L
Linus Torvalds 已提交
1633
		/* Soft reset PHY */
1634 1635
		mii_write (dev, phy_addr, MII_BMCR, BMCR_RESET);
		bmcr = BMCR_ANENABLE | BMCR_ANRESTART | BMCR_RESET;
A
Al Viro 已提交
1636
		mii_write (dev, phy_addr, MII_BMCR, bmcr);
L
Linus Torvalds 已提交
1637 1638 1639 1640
		mdelay(1);
	} else {
		/* Force speed setting */
		/* 1) Disable Auto crossover */
A
Al Viro 已提交
1641 1642 1643
		pscr = mii_read (dev, phy_addr, MII_PHY_SCR);
		pscr &= ~(3 << 5);
		mii_write (dev, phy_addr, MII_PHY_SCR, pscr);
L
Linus Torvalds 已提交
1644 1645

		/* 2) PHY Reset */
A
Al Viro 已提交
1646
		bmcr = mii_read (dev, phy_addr, MII_BMCR);
1647
		bmcr |= BMCR_RESET;
A
Al Viro 已提交
1648
		mii_write (dev, phy_addr, MII_BMCR, bmcr);
L
Linus Torvalds 已提交
1649 1650

		/* 3) Power Down */
A
Al Viro 已提交
1651 1652
		bmcr = 0x1940;	/* must be 0x1940 */
		mii_write (dev, phy_addr, MII_BMCR, bmcr);
L
Linus Torvalds 已提交
1653 1654 1655
		mdelay (100);	/* wait a certain time */

		/* 4) Advertise nothing */
1656
		mii_write (dev, phy_addr, MII_ADVERTISE, 0);
L
Linus Torvalds 已提交
1657 1658

		/* 5) Set media and Power Up */
1659
		bmcr = BMCR_PDOWN;
L
Linus Torvalds 已提交
1660
		if (np->speed == 100) {
1661
			bmcr |= BMCR_SPEED100;
L
Linus Torvalds 已提交
1662 1663 1664 1665 1666
			printk (KERN_INFO "Manual 100 Mbps, ");
		} else if (np->speed == 10) {
			printk (KERN_INFO "Manual 10 Mbps, ");
		}
		if (np->full_duplex) {
1667
			bmcr |= BMCR_FULLDPLX;
1668
			printk (KERN_CONT "Full duplex\n");
L
Linus Torvalds 已提交
1669
		} else {
1670
			printk (KERN_CONT "Half duplex\n");
L
Linus Torvalds 已提交
1671 1672 1673
		}
#if 0
		/* Set 1000BaseT Master/Slave setting */
1674
		mscr = mii_read (dev, phy_addr, MII_CTRL1000);
A
Al Viro 已提交
1675 1676
		mscr |= MII_MSCR_CFG_ENABLE;
		mscr &= ~MII_MSCR_CFG_VALUE = 0;
L
Linus Torvalds 已提交
1677
#endif
A
Al Viro 已提交
1678
		mii_write (dev, phy_addr, MII_BMCR, bmcr);
L
Linus Torvalds 已提交
1679 1680 1681 1682 1683 1684 1685 1686
		mdelay(10);
	}
	return 0;
}

static int
mii_get_media_pcs (struct net_device *dev)
{
A
Al Viro 已提交
1687
	__u16 negotiate;
A
Al Viro 已提交
1688
	__u16 bmsr;
L
Linus Torvalds 已提交
1689 1690 1691 1692 1693 1694
	int phy_addr;
	struct netdev_private *np;

	np = netdev_priv(dev);
	phy_addr = np->phy_addr;

A
Al Viro 已提交
1695
	bmsr = mii_read (dev, phy_addr, PCS_BMSR);
L
Linus Torvalds 已提交
1696
	if (np->an_enable) {
1697
		if (!(bmsr & BMSR_ANEGCOMPLETE)) {
L
Linus Torvalds 已提交
1698 1699 1700
			/* Auto-Negotiation not completed */
			return -1;
		}
A
Al Viro 已提交
1701
		negotiate = mii_read (dev, phy_addr, PCS_ANAR) &
L
Linus Torvalds 已提交
1702 1703
			mii_read (dev, phy_addr, PCS_ANLPAR);
		np->speed = 1000;
A
Al Viro 已提交
1704
		if (negotiate & PCS_ANAR_FULL_DUPLEX) {
L
Linus Torvalds 已提交
1705 1706 1707 1708 1709 1710
			printk (KERN_INFO "Auto 1000 Mbps, Full duplex\n");
			np->full_duplex = 1;
		} else {
			printk (KERN_INFO "Auto 1000 Mbps, half duplex\n");
			np->full_duplex = 0;
		}
A
Al Viro 已提交
1711
		if (negotiate & PCS_ANAR_PAUSE) {
L
Linus Torvalds 已提交
1712 1713
			np->tx_flow &= 1;
			np->rx_flow &= 1;
A
Al Viro 已提交
1714
		} else if (negotiate & PCS_ANAR_ASYMMETRIC) {
L
Linus Torvalds 已提交
1715 1716 1717 1718 1719
			np->tx_flow = 0;
			np->rx_flow &= 1;
		}
		/* else tx_flow, rx_flow = user select  */
	} else {
A
Al Viro 已提交
1720
		__u16 bmcr = mii_read (dev, phy_addr, PCS_BMCR);
L
Linus Torvalds 已提交
1721
		printk (KERN_INFO "Operating at 1000 Mbps, ");
1722
		if (bmcr & BMCR_FULLDPLX) {
1723
			printk (KERN_CONT "Full duplex\n");
L
Linus Torvalds 已提交
1724
		} else {
1725
			printk (KERN_CONT "Half duplex\n");
L
Linus Torvalds 已提交
1726 1727
		}
	}
1728
	if (np->tx_flow)
L
Linus Torvalds 已提交
1729
		printk(KERN_INFO "Enable Tx Flow Control\n");
1730
	else
L
Linus Torvalds 已提交
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
		printk(KERN_INFO "Disable Tx Flow Control\n");
	if (np->rx_flow)
		printk(KERN_INFO "Enable Rx Flow Control\n");
	else
		printk(KERN_INFO "Disable Rx Flow Control\n");

	return 0;
}

static int
mii_set_media_pcs (struct net_device *dev)
{
A
Al Viro 已提交
1743
	__u16 bmcr;
A
Al Viro 已提交
1744
	__u16 esr;
A
Al Viro 已提交
1745
	__u16 anar;
L
Linus Torvalds 已提交
1746 1747 1748 1749 1750 1751 1752 1753
	int phy_addr;
	struct netdev_private *np;
	np = netdev_priv(dev);
	phy_addr = np->phy_addr;

	/* Auto-Negotiation? */
	if (np->an_enable) {
		/* Advertise capabilities */
A
Al Viro 已提交
1754
		esr = mii_read (dev, phy_addr, PCS_ESR);
1755
		anar = mii_read (dev, phy_addr, MII_ADVERTISE) &
A
Al Viro 已提交
1756 1757
			~PCS_ANAR_HALF_DUPLEX &
			~PCS_ANAR_FULL_DUPLEX;
A
Al Viro 已提交
1758
		if (esr & (MII_ESR_1000BT_HD | MII_ESR_1000BX_HD))
A
Al Viro 已提交
1759
			anar |= PCS_ANAR_HALF_DUPLEX;
A
Al Viro 已提交
1760
		if (esr & (MII_ESR_1000BT_FD | MII_ESR_1000BX_FD))
A
Al Viro 已提交
1761 1762
			anar |= PCS_ANAR_FULL_DUPLEX;
		anar |= PCS_ANAR_PAUSE | PCS_ANAR_ASYMMETRIC;
1763
		mii_write (dev, phy_addr, MII_ADVERTISE, anar);
L
Linus Torvalds 已提交
1764 1765

		/* Soft reset PHY */
1766 1767
		mii_write (dev, phy_addr, MII_BMCR, BMCR_RESET);
		bmcr = BMCR_ANENABLE | BMCR_ANRESTART | BMCR_RESET;
A
Al Viro 已提交
1768
		mii_write (dev, phy_addr, MII_BMCR, bmcr);
L
Linus Torvalds 已提交
1769 1770 1771 1772
		mdelay(1);
	} else {
		/* Force speed setting */
		/* PHY Reset */
1773
		bmcr = BMCR_RESET;
A
Al Viro 已提交
1774
		mii_write (dev, phy_addr, MII_BMCR, bmcr);
L
Linus Torvalds 已提交
1775 1776
		mdelay(10);
		if (np->full_duplex) {
1777
			bmcr = BMCR_FULLDPLX;
L
Linus Torvalds 已提交
1778 1779
			printk (KERN_INFO "Manual full duplex\n");
		} else {
A
Al Viro 已提交
1780
			bmcr = 0;
L
Linus Torvalds 已提交
1781 1782
			printk (KERN_INFO "Manual half duplex\n");
		}
A
Al Viro 已提交
1783
		mii_write (dev, phy_addr, MII_BMCR, bmcr);
L
Linus Torvalds 已提交
1784 1785 1786
		mdelay(10);

		/*  Advertise nothing */
1787
		mii_write (dev, phy_addr, MII_ADVERTISE, 0);
L
Linus Torvalds 已提交
1788 1789 1790 1791 1792 1793 1794 1795 1796
	}
	return 0;
}


static int
rio_close (struct net_device *dev)
{
	struct netdev_private *np = netdev_priv(dev);
1797
	struct pci_dev *pdev = np->pdev;
L
Linus Torvalds 已提交
1798 1799 1800

	netif_stop_queue (dev);

1801
	rio_hw_stop(dev);
1802

1803
	free_irq(pdev->irq, dev);
L
Linus Torvalds 已提交
1804
	del_timer_sync (&np->timer);
1805

1806
	free_list(dev);
L
Linus Torvalds 已提交
1807 1808 1809 1810

	return 0;
}

B
Bill Pemberton 已提交
1811
static void
L
Linus Torvalds 已提交
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824
rio_remove1 (struct pci_dev *pdev)
{
	struct net_device *dev = pci_get_drvdata (pdev);

	if (dev) {
		struct netdev_private *np = netdev_priv(dev);

		unregister_netdev (dev);
		pci_free_consistent (pdev, RX_TOTAL_SIZE, np->rx_ring,
				     np->rx_ring_dma);
		pci_free_consistent (pdev, TX_TOTAL_SIZE, np->tx_ring,
				     np->tx_ring_dma);
#ifdef MEM_MAPPING
1825
		pci_iounmap(pdev, np->ioaddr);
L
Linus Torvalds 已提交
1826
#endif
1827
		pci_iounmap(pdev, np->eeprom_addr);
L
Linus Torvalds 已提交
1828 1829 1830 1831 1832 1833
		free_netdev (dev);
		pci_release_regions (pdev);
		pci_disable_device (pdev);
	}
}

O
Ondrej Zary 已提交
1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876
#ifdef CONFIG_PM_SLEEP
static int rio_suspend(struct device *device)
{
	struct net_device *dev = dev_get_drvdata(device);
	struct netdev_private *np = netdev_priv(dev);

	if (!netif_running(dev))
		return 0;

	netif_device_detach(dev);
	del_timer_sync(&np->timer);
	rio_hw_stop(dev);

	return 0;
}

static int rio_resume(struct device *device)
{
	struct net_device *dev = dev_get_drvdata(device);
	struct netdev_private *np = netdev_priv(dev);

	if (!netif_running(dev))
		return 0;

	rio_reset_ring(np);
	rio_hw_init(dev);
	np->timer.expires = jiffies + 1 * HZ;
	add_timer(&np->timer);
	netif_device_attach(dev);
	dl2k_enable_int(np);

	return 0;
}

static SIMPLE_DEV_PM_OPS(rio_pm_ops, rio_suspend, rio_resume);
#define RIO_PM_OPS    (&rio_pm_ops)

#else

#define RIO_PM_OPS	NULL

#endif /* CONFIG_PM_SLEEP */

L
Linus Torvalds 已提交
1877 1878 1879 1880
static struct pci_driver rio_driver = {
	.name		= "dl2k",
	.id_table	= rio_pci_tbl,
	.probe		= rio_probe1,
B
Bill Pemberton 已提交
1881
	.remove		= rio_remove1,
O
Ondrej Zary 已提交
1882
	.driver.pm	= RIO_PM_OPS,
L
Linus Torvalds 已提交
1883 1884
};

1885
module_pci_driver(rio_driver);
L
Linus Torvalds 已提交
1886
/*
1887 1888 1889

Compile command:

L
Linus Torvalds 已提交
1890 1891 1892 1893 1894 1895
gcc -D__KERNEL__ -DMODULE -I/usr/src/linux/include -Wall -Wstrict-prototypes -O2 -c dl2k.c

Read Documentation/networking/dl2k.txt for details.

*/