atl1.c 100.9 KB
Newer Older
J
Jay Cliburn 已提交
1 2
/*
 * Copyright(c) 2005 - 2006 Attansic Corporation. All rights reserved.
3
 * Copyright(c) 2006 - 2007 Chris Snook <csnook@redhat.com>
J
Jay Cliburn 已提交
4
 * Copyright(c) 2006 - 2008 Jay Cliburn <jcliburn@gmail.com>
J
Jay Cliburn 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 *
 * Derived from Intel e1000 driver
 * Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
 *
 * 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.
 *
 * This program 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., 59
 * Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * The full GNU General Public License is included in this distribution in the
 * file called COPYING.
 *
 * Contact Information:
 * Xiong Huang <xiong_huang@attansic.com>
 * Attansic Technology Corp. 3F 147, Xianzheng 9th Road, Zhubei,
 * Xinzhu  302, TAIWAN, REPUBLIC OF CHINA
 *
 * Chris Snook <csnook@redhat.com>
 * Jay Cliburn <jcliburn@gmail.com>
 *
 * This version is adapted from the Attansic reference driver for
 * inclusion in the Linux kernel.  It is currently under heavy development.
 * A very incomplete list of things that need to be dealt with:
 *
 * TODO:
J
Jay Cliburn 已提交
39
 * Add more ethtool functions.
J
Jay Cliburn 已提交
40 41 42 43 44 45 46 47 48 49 50
 * Fix abstruse irq enable/disable condition described here:
 *	http://marc.theaimsgroup.com/?l=linux-netdev&m=116398508500553&w=2
 *
 * NEEDS TESTING:
 * VLAN
 * multicast
 * promiscuous mode
 * interrupt coalescing
 * SMP torture testing
 */

51 52 53 54 55 56 57
#include <asm/atomic.h>
#include <asm/byteorder.h>

#include <linux/compiler.h>
#include <linux/crc32.h>
#include <linux/delay.h>
#include <linux/dma-mapping.h>
J
Jay Cliburn 已提交
58 59
#include <linux/etherdevice.h>
#include <linux/hardirq.h>
60 61 62
#include <linux/if_ether.h>
#include <linux/if_vlan.h>
#include <linux/in.h>
J
Jay Cliburn 已提交
63
#include <linux/interrupt.h>
64
#include <linux/ip.h>
J
Jay Cliburn 已提交
65
#include <linux/irqflags.h>
66 67 68 69 70
#include <linux/irqreturn.h>
#include <linux/jiffies.h>
#include <linux/mii.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
J
Jay Cliburn 已提交
71
#include <linux/net.h>
72 73 74
#include <linux/netdevice.h>
#include <linux/pci.h>
#include <linux/pci_ids.h>
J
Jay Cliburn 已提交
75
#include <linux/pm.h>
76 77 78 79
#include <linux/skbuff.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/string.h>
J
Jay Cliburn 已提交
80
#include <linux/tcp.h>
81 82 83
#include <linux/timer.h>
#include <linux/types.h>
#include <linux/workqueue.h>
J
Jay Cliburn 已提交
84 85 86 87 88

#include <net/checksum.h>

#include "atl1.h"

89 90
/* Temporary hack for merging atl1 and atl2 */
#include "atlx.c"
J
Jay Cliburn 已提交
91

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 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 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 227 228 229
/*
 * This is the only thing that needs to be changed to adjust the
 * maximum number of ports that the driver can manage.
 */
#define ATL1_MAX_NIC 4

#define OPTION_UNSET    -1
#define OPTION_DISABLED 0
#define OPTION_ENABLED  1

#define ATL1_PARAM_INIT { [0 ... ATL1_MAX_NIC] = OPTION_UNSET }

/*
 * Interrupt Moderate Timer in units of 2 us
 *
 * Valid Range: 10-65535
 *
 * Default Value: 100 (200us)
 */
static int __devinitdata int_mod_timer[ATL1_MAX_NIC+1] = ATL1_PARAM_INIT;
static int num_int_mod_timer;
module_param_array_named(int_mod_timer, int_mod_timer, int,
	&num_int_mod_timer, 0);
MODULE_PARM_DESC(int_mod_timer, "Interrupt moderator timer");

#define DEFAULT_INT_MOD_CNT	100	/* 200us */
#define MAX_INT_MOD_CNT		65000
#define MIN_INT_MOD_CNT		50

struct atl1_option {
	enum { enable_option, range_option, list_option } type;
	char *name;
	char *err;
	int def;
	union {
		struct {	/* range_option info */
			int min;
			int max;
		} r;
		struct {	/* list_option info */
			int nr;
			struct atl1_opt_list {
				int i;
				char *str;
			} *p;
		} l;
	} arg;
};

static int __devinit atl1_validate_option(int *value, struct atl1_option *opt,
	struct pci_dev *pdev)
{
	if (*value == OPTION_UNSET) {
		*value = opt->def;
		return 0;
	}

	switch (opt->type) {
	case enable_option:
		switch (*value) {
		case OPTION_ENABLED:
			dev_info(&pdev->dev, "%s enabled\n", opt->name);
			return 0;
		case OPTION_DISABLED:
			dev_info(&pdev->dev, "%s disabled\n", opt->name);
			return 0;
		}
		break;
	case range_option:
		if (*value >= opt->arg.r.min && *value <= opt->arg.r.max) {
			dev_info(&pdev->dev, "%s set to %i\n", opt->name,
				*value);
			return 0;
		}
		break;
	case list_option:{
			int i;
			struct atl1_opt_list *ent;

			for (i = 0; i < opt->arg.l.nr; i++) {
				ent = &opt->arg.l.p[i];
				if (*value == ent->i) {
					if (ent->str[0] != '\0')
						dev_info(&pdev->dev, "%s\n",
							ent->str);
					return 0;
				}
			}
		}
		break;

	default:
		break;
	}

	dev_info(&pdev->dev, "invalid %s specified (%i) %s\n",
		opt->name, *value, opt->err);
	*value = opt->def;
	return -1;
}

/*
 * atl1_check_options - Range Checking for Command Line Parameters
 * @adapter: board private structure
 *
 * This routine checks all command line parameters for valid user
 * input.  If an invalid value is given, or if no user specified
 * value exists, a default value is used.  The final value is stored
 * in a variable in the adapter structure.
 */
void __devinit atl1_check_options(struct atl1_adapter *adapter)
{
	struct pci_dev *pdev = adapter->pdev;
	int bd = adapter->bd_number;
	if (bd >= ATL1_MAX_NIC) {
		dev_notice(&pdev->dev, "no configuration for board#%i\n", bd);
		dev_notice(&pdev->dev, "using defaults for all values\n");
	}
	{			/* Interrupt Moderate Timer */
		struct atl1_option opt = {
			.type = range_option,
			.name = "Interrupt Moderator Timer",
			.err = "using default of "
				__MODULE_STRING(DEFAULT_INT_MOD_CNT),
			.def = DEFAULT_INT_MOD_CNT,
			.arg = {.r = {.min = MIN_INT_MOD_CNT,
					.max = MAX_INT_MOD_CNT} }
		};
		int val;
		if (num_int_mod_timer > bd) {
			val = int_mod_timer[bd];
			atl1_validate_option(&val, &opt, pdev);
			adapter->imt = (u16) val;
		} else
			adapter->imt = (u16) (opt.def);
	}
}

J
Jay Cliburn 已提交
230 231 232 233
/*
 * atl1_pci_tbl - PCI Device ID Table
 */
static const struct pci_device_id atl1_pci_tbl[] = {
234
	{PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATTANSIC_L1)},
J
Jay Cliburn 已提交
235 236 237 238 239
	/* required last entry */
	{0,}
};
MODULE_DEVICE_TABLE(pci, atl1_pci_tbl);

J
Jay Cliburn 已提交
240 241 242 243 244 245 246
static const u32 atl1_default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
	NETIF_MSG_LINK | NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP;

static int debug = -1;
module_param(debug, int, 0);
MODULE_PARM_DESC(debug, "Message level (0=none,...,16=all)");

J
Jay Cliburn 已提交
247
/*
J
Jay Cliburn 已提交
248 249 250
 * Reset the transmit and receive units; mask and clear all interrupts.
 * hw - Struct containing variables accessed by shared code
 * return : 0  or  idle status (if error)
J
Jay Cliburn 已提交
251
 */
J
Jay Cliburn 已提交
252
static s32 atl1_reset_hw(struct atl1_hw *hw)
J
Jay Cliburn 已提交
253
{
J
Jay Cliburn 已提交
254 255 256 257
	struct pci_dev *pdev = hw->back->pdev;
	struct atl1_adapter *adapter = hw->back;
	u32 icr;
	int i;
J
Jay Cliburn 已提交
258

J
Jay Cliburn 已提交
259 260 261 262 263 264 265 266
	/*
	 * Clear Interrupt mask to stop board from generating
	 * interrupts & Clear any pending interrupt events
	 */
	/*
	 * iowrite32(0, hw->hw_addr + REG_IMR);
	 * iowrite32(0xffffffff, hw->hw_addr + REG_ISR);
	 */
J
Jay Cliburn 已提交
267

J
Jay Cliburn 已提交
268 269 270 271 272 273 274 275
	/*
	 * Issue Soft Reset to the MAC.  This will reset the chip's
	 * transmit, receive, DMA.  It will not effect
	 * the current PCI configuration.  The global reset bit is self-
	 * clearing, and should clear within a microsecond.
	 */
	iowrite32(MASTER_CTRL_SOFT_RST, hw->hw_addr + REG_MASTER_CTRL);
	ioread32(hw->hw_addr + REG_MASTER_CTRL);
J
Jay Cliburn 已提交
276

J
Jay Cliburn 已提交
277 278
	iowrite16(1, hw->hw_addr + REG_PHY_ENABLE);
	ioread16(hw->hw_addr + REG_PHY_ENABLE);
J
Jay Cliburn 已提交
279

J
Jay Cliburn 已提交
280 281
	/* delay about 1ms */
	msleep(1);
J
Jay Cliburn 已提交
282

J
Jay Cliburn 已提交
283 284 285 286 287 288 289 290 291 292
	/* Wait at least 10ms for All module to be Idle */
	for (i = 0; i < 10; i++) {
		icr = ioread32(hw->hw_addr + REG_IDLE_STATUS);
		if (!icr)
			break;
		/* delay 1 ms */
		msleep(1);
		/* FIXME: still the right way to do this? */
		cpu_relax();
	}
J
Jay Cliburn 已提交
293

J
Jay Cliburn 已提交
294 295 296 297 298
	if (icr) {
		if (netif_msg_hw(adapter))
			dev_dbg(&pdev->dev, "ICR = 0x%x\n", icr);
		return icr;
	}
J
Jay Cliburn 已提交
299

J
Jay Cliburn 已提交
300
	return 0;
J
Jay Cliburn 已提交
301 302
}

J
Jay Cliburn 已提交
303 304 305 306 307 308
/* function about EEPROM
 *
 * check_eeprom_exist
 * return 0 if eeprom exist
 */
static int atl1_check_eeprom_exist(struct atl1_hw *hw)
J
Jay Cliburn 已提交
309
{
J
Jay Cliburn 已提交
310 311 312 313 314 315
	u32 value;
	value = ioread32(hw->hw_addr + REG_SPI_FLASH_CTRL);
	if (value & SPI_FLASH_CTRL_EN_VPD) {
		value &= ~SPI_FLASH_CTRL_EN_VPD;
		iowrite32(value, hw->hw_addr + REG_SPI_FLASH_CTRL);
	}
J
Jay Cliburn 已提交
316

J
Jay Cliburn 已提交
317 318
	value = ioread16(hw->hw_addr + REG_PCIE_CAP_LIST);
	return ((value & 0xFF00) == 0x6C00) ? 0 : 1;
J
Jay Cliburn 已提交
319 320
}

J
Jay Cliburn 已提交
321
static bool atl1_read_eeprom(struct atl1_hw *hw, u32 offset, u32 *p_value)
J
Jay Cliburn 已提交
322
{
J
Jay Cliburn 已提交
323 324
	int i;
	u32 control;
J
Jay Cliburn 已提交
325

J
Jay Cliburn 已提交
326 327 328
	if (offset & 3)
		/* address do not align */
		return false;
J
Jay Cliburn 已提交
329

J
Jay Cliburn 已提交
330 331 332 333
	iowrite32(0, hw->hw_addr + REG_VPD_DATA);
	control = (offset & VPD_CAP_VPD_ADDR_MASK) << VPD_CAP_VPD_ADDR_SHIFT;
	iowrite32(control, hw->hw_addr + REG_VPD_CAP);
	ioread32(hw->hw_addr + REG_VPD_CAP);
J
Jay Cliburn 已提交
334

J
Jay Cliburn 已提交
335 336 337 338 339 340 341 342 343 344 345 346
	for (i = 0; i < 10; i++) {
		msleep(2);
		control = ioread32(hw->hw_addr + REG_VPD_CAP);
		if (control & VPD_CAP_VPD_FLAG)
			break;
	}
	if (control & VPD_CAP_VPD_FLAG) {
		*p_value = ioread32(hw->hw_addr + REG_VPD_DATA);
		return true;
	}
	/* timeout */
	return false;
J
Jay Cliburn 已提交
347 348
}

J
Jay Cliburn 已提交
349
/*
J
Jay Cliburn 已提交
350 351 352
 * Reads the value from a PHY register
 * hw - Struct containing variables accessed by shared code
 * reg_addr - address of the PHY register to read
J
Jay Cliburn 已提交
353
 */
J
Jay Cliburn 已提交
354
s32 atl1_read_phy_reg(struct atl1_hw *hw, u16 reg_addr, u16 *phy_data)
J
Jay Cliburn 已提交
355
{
J
Jay Cliburn 已提交
356 357
	u32 val;
	int i;
J
Jay Cliburn 已提交
358

J
Jay Cliburn 已提交
359 360 361 362 363
	val = ((u32) (reg_addr & MDIO_REG_ADDR_MASK)) << MDIO_REG_ADDR_SHIFT |
		MDIO_START | MDIO_SUP_PREAMBLE | MDIO_RW | MDIO_CLK_25_4 <<
		MDIO_CLK_SEL_SHIFT;
	iowrite32(val, hw->hw_addr + REG_MDIO_CTRL);
	ioread32(hw->hw_addr + REG_MDIO_CTRL);
J
Jay Cliburn 已提交
364

J
Jay Cliburn 已提交
365 366 367 368 369 370 371 372 373
	for (i = 0; i < MDIO_WAIT_TIMES; i++) {
		udelay(2);
		val = ioread32(hw->hw_addr + REG_MDIO_CTRL);
		if (!(val & (MDIO_START | MDIO_BUSY)))
			break;
	}
	if (!(val & (MDIO_START | MDIO_BUSY))) {
		*phy_data = (u16) val;
		return 0;
J
Jay Cliburn 已提交
374
	}
J
Jay Cliburn 已提交
375 376
	return ATLX_ERR_PHY;
}
J
Jay Cliburn 已提交
377

J
Jay Cliburn 已提交
378 379 380 381 382
#define CUSTOM_SPI_CS_SETUP	2
#define CUSTOM_SPI_CLK_HI	2
#define CUSTOM_SPI_CLK_LO	2
#define CUSTOM_SPI_CS_HOLD	2
#define CUSTOM_SPI_CS_HI	3
J
Jay Cliburn 已提交
383

J
Jay Cliburn 已提交
384 385 386 387
static bool atl1_spi_read(struct atl1_hw *hw, u32 addr, u32 *buf)
{
	int i;
	u32 value;
J
Jay Cliburn 已提交
388

J
Jay Cliburn 已提交
389 390
	iowrite32(0, hw->hw_addr + REG_SPI_DATA);
	iowrite32(addr, hw->hw_addr + REG_SPI_ADDR);
391

J
Jay Cliburn 已提交
392 393 394 395 396 397 398 399 400 401 402 403
	value = SPI_FLASH_CTRL_WAIT_READY |
	    (CUSTOM_SPI_CS_SETUP & SPI_FLASH_CTRL_CS_SETUP_MASK) <<
	    SPI_FLASH_CTRL_CS_SETUP_SHIFT | (CUSTOM_SPI_CLK_HI &
					     SPI_FLASH_CTRL_CLK_HI_MASK) <<
	    SPI_FLASH_CTRL_CLK_HI_SHIFT | (CUSTOM_SPI_CLK_LO &
					   SPI_FLASH_CTRL_CLK_LO_MASK) <<
	    SPI_FLASH_CTRL_CLK_LO_SHIFT | (CUSTOM_SPI_CS_HOLD &
					   SPI_FLASH_CTRL_CS_HOLD_MASK) <<
	    SPI_FLASH_CTRL_CS_HOLD_SHIFT | (CUSTOM_SPI_CS_HI &
					    SPI_FLASH_CTRL_CS_HI_MASK) <<
	    SPI_FLASH_CTRL_CS_HI_SHIFT | (1 & SPI_FLASH_CTRL_INS_MASK) <<
	    SPI_FLASH_CTRL_INS_SHIFT;
J
Jay Cliburn 已提交
404

J
Jay Cliburn 已提交
405
	iowrite32(value, hw->hw_addr + REG_SPI_FLASH_CTRL);
J
Jay Cliburn 已提交
406

J
Jay Cliburn 已提交
407 408 409
	value |= SPI_FLASH_CTRL_START;
	iowrite32(value, hw->hw_addr + REG_SPI_FLASH_CTRL);
	ioread32(hw->hw_addr + REG_SPI_FLASH_CTRL);
J
Jay Cliburn 已提交
410

J
Jay Cliburn 已提交
411 412 413 414 415 416
	for (i = 0; i < 10; i++) {
		msleep(1);
		value = ioread32(hw->hw_addr + REG_SPI_FLASH_CTRL);
		if (!(value & SPI_FLASH_CTRL_START))
			break;
	}
J
Jay Cliburn 已提交
417

J
Jay Cliburn 已提交
418 419
	if (value & SPI_FLASH_CTRL_START)
		return false;
J
Jay Cliburn 已提交
420

J
Jay Cliburn 已提交
421
	*buf = ioread32(hw->hw_addr + REG_SPI_DATA);
422

J
Jay Cliburn 已提交
423
	return true;
J
Jay Cliburn 已提交
424 425 426
}

/*
J
Jay Cliburn 已提交
427 428
 * get_permanent_address
 * return 0 if get valid mac address,
J
Jay Cliburn 已提交
429
 */
J
Jay Cliburn 已提交
430
static int atl1_get_permanent_address(struct atl1_hw *hw)
J
Jay Cliburn 已提交
431
{
J
Jay Cliburn 已提交
432 433 434 435 436
	u32 addr[2];
	u32 i, control;
	u16 reg;
	u8 eth_addr[ETH_ALEN];
	bool key_valid;
J
Jay Cliburn 已提交
437

J
Jay Cliburn 已提交
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
	if (is_valid_ether_addr(hw->perm_mac_addr))
		return 0;

	/* init */
	addr[0] = addr[1] = 0;

	if (!atl1_check_eeprom_exist(hw)) {
		reg = 0;
		key_valid = false;
		/* Read out all EEPROM content */
		i = 0;
		while (1) {
			if (atl1_read_eeprom(hw, i + 0x100, &control)) {
				if (key_valid) {
					if (reg == REG_MAC_STA_ADDR)
						addr[0] = control;
					else if (reg == (REG_MAC_STA_ADDR + 4))
						addr[1] = control;
					key_valid = false;
				} else if ((control & 0xff) == 0x5A) {
					key_valid = true;
					reg = (u16) (control >> 16);
				} else
					break;
			} else
				/* read error */
				break;
			i += 4;
J
Jay Cliburn 已提交
466
		}
J
Jay Cliburn 已提交
467 468 469 470 471 472

		*(u32 *) &eth_addr[2] = swab32(addr[0]);
		*(u16 *) &eth_addr[0] = swab16(*(u16 *) &addr[1]);
		if (is_valid_ether_addr(eth_addr)) {
			memcpy(hw->perm_mac_addr, eth_addr, ETH_ALEN);
			return 0;
J
Jay Cliburn 已提交
473
		}
J
Jay Cliburn 已提交
474
		return 1;
J
Jay Cliburn 已提交
475
	}
J
Jay Cliburn 已提交
476

J
Jay Cliburn 已提交
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
	/* see if SPI FLAGS exist ? */
	addr[0] = addr[1] = 0;
	reg = 0;
	key_valid = false;
	i = 0;
	while (1) {
		if (atl1_spi_read(hw, i + 0x1f000, &control)) {
			if (key_valid) {
				if (reg == REG_MAC_STA_ADDR)
					addr[0] = control;
				else if (reg == (REG_MAC_STA_ADDR + 4))
					addr[1] = control;
				key_valid = false;
			} else if ((control & 0xff) == 0x5A) {
				key_valid = true;
				reg = (u16) (control >> 16);
			} else
				/* data end */
				break;
		} else
			/* read error */
			break;
		i += 4;
	}
J
Jay Cliburn 已提交
501

J
Jay Cliburn 已提交
502 503 504 505 506 507
	*(u32 *) &eth_addr[2] = swab32(addr[0]);
	*(u16 *) &eth_addr[0] = swab16(*(u16 *) &addr[1]);
	if (is_valid_ether_addr(eth_addr)) {
		memcpy(hw->perm_mac_addr, eth_addr, ETH_ALEN);
		return 0;
	}
J
Jay Cliburn 已提交
508

J
Jay Cliburn 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521 522
	/*
	 * On some motherboards, the MAC address is written by the
	 * BIOS directly to the MAC register during POST, and is
	 * not stored in eeprom.  If all else thus far has failed
	 * to fetch the permanent MAC address, try reading it directly.
	 */
	addr[0] = ioread32(hw->hw_addr + REG_MAC_STA_ADDR);
	addr[1] = ioread16(hw->hw_addr + (REG_MAC_STA_ADDR + 4));
	*(u32 *) &eth_addr[2] = swab32(addr[0]);
	*(u16 *) &eth_addr[0] = swab16(*(u16 *) &addr[1]);
	if (is_valid_ether_addr(eth_addr)) {
		memcpy(hw->perm_mac_addr, eth_addr, ETH_ALEN);
		return 0;
	}
J
Jay Cliburn 已提交
523

J
Jay Cliburn 已提交
524
	return 1;
J
Jay Cliburn 已提交
525 526
}

J
Jay Cliburn 已提交
527
/*
J
Jay Cliburn 已提交
528 529
 * Reads the adapter's MAC address from the EEPROM
 * hw - Struct containing variables accessed by shared code
J
Jay Cliburn 已提交
530
 */
J
Jay Cliburn 已提交
531
s32 atl1_read_mac_addr(struct atl1_hw *hw)
J
Jay Cliburn 已提交
532
{
J
Jay Cliburn 已提交
533
	u16 i;
J
Jay Cliburn 已提交
534

J
Jay Cliburn 已提交
535 536
	if (atl1_get_permanent_address(hw))
		random_ether_addr(hw->perm_mac_addr);
J
Jay Cliburn 已提交
537

J
Jay Cliburn 已提交
538 539 540
	for (i = 0; i < ETH_ALEN; i++)
		hw->mac_addr[i] = hw->perm_mac_addr[i];
	return 0;
J
Jay Cliburn 已提交
541 542 543
}

/*
J
Jay Cliburn 已提交
544 545 546
 * Hashes an address to determine its location in the multicast table
 * hw - Struct containing variables accessed by shared code
 * mc_addr - the multicast address to hash
J
Jay Cliburn 已提交
547
 *
J
Jay Cliburn 已提交
548 549 550 551 552 553
 * atl1_hash_mc_addr
 *  purpose
 *      set hash value for a multicast address
 *      hash calcu processing :
 *          1. calcu 32bit CRC for multicast address
 *          2. reverse crc with MSB to LSB
J
Jay Cliburn 已提交
554
 */
J
Jay Cliburn 已提交
555
u32 atl1_hash_mc_addr(struct atl1_hw *hw, u8 *mc_addr)
J
Jay Cliburn 已提交
556
{
J
Jay Cliburn 已提交
557 558
	u32 crc32, value = 0;
	int i;
J
Jay Cliburn 已提交
559

J
Jay Cliburn 已提交
560 561 562
	crc32 = ether_crc_le(6, mc_addr);
	for (i = 0; i < 32; i++)
		value |= (((crc32 >> i) & 1) << (31 - i));
J
Jay Cliburn 已提交
563

J
Jay Cliburn 已提交
564
	return value;
J
Jay Cliburn 已提交
565 566
}

J
Jay Cliburn 已提交
567 568 569 570 571 572
/*
 * Sets the bit in the multicast table corresponding to the hash value.
 * hw - Struct containing variables accessed by shared code
 * hash_value - Multicast address hash value
 */
void atl1_hash_set(struct atl1_hw *hw, u32 hash_value)
J
Jay Cliburn 已提交
573
{
J
Jay Cliburn 已提交
574 575 576 577 578 579 580 581 582 583 584
	u32 hash_bit, hash_reg;
	u32 mta;

	/*
	 * The HASH Table  is a register array of 2 32-bit registers.
	 * It is treated like an array of 64 bits.  We want to set
	 * bit BitArray[hash_value]. So we figure out what register
	 * the bit is in, read it, OR in the new bit, then write
	 * back the new value.  The register is determined by the
	 * upper 7 bits of the hash value and the bit within that
	 * register are determined by the lower 5 bits of the value.
J
Jay Cliburn 已提交
585
	 */
J
Jay Cliburn 已提交
586 587 588 589 590
	hash_reg = (hash_value >> 31) & 0x1;
	hash_bit = (hash_value >> 26) & 0x1F;
	mta = ioread32((hw->hw_addr + REG_RX_HASH_TABLE) + (hash_reg << 2));
	mta |= (1 << hash_bit);
	iowrite32(mta, (hw->hw_addr + REG_RX_HASH_TABLE) + (hash_reg << 2));
J
Jay Cliburn 已提交
591
}
J
Jay Cliburn 已提交
592

J
Jay Cliburn 已提交
593 594 595 596 597 598 599
/*
 * Writes a value to a PHY register
 * hw - Struct containing variables accessed by shared code
 * reg_addr - address of the PHY register to write
 * data - data to write to the PHY
 */
static s32 atl1_write_phy_reg(struct atl1_hw *hw, u32 reg_addr, u16 phy_data)
J
Jay Cliburn 已提交
600
{
J
Jay Cliburn 已提交
601 602
	int i;
	u32 val;
J
Jay Cliburn 已提交
603

J
Jay Cliburn 已提交
604 605 606 607 608 609
	val = ((u32) (phy_data & MDIO_DATA_MASK)) << MDIO_DATA_SHIFT |
	    (reg_addr & MDIO_REG_ADDR_MASK) << MDIO_REG_ADDR_SHIFT |
	    MDIO_SUP_PREAMBLE |
	    MDIO_START | MDIO_CLK_25_4 << MDIO_CLK_SEL_SHIFT;
	iowrite32(val, hw->hw_addr + REG_MDIO_CTRL);
	ioread32(hw->hw_addr + REG_MDIO_CTRL);
J
Jay Cliburn 已提交
610

J
Jay Cliburn 已提交
611 612 613 614 615
	for (i = 0; i < MDIO_WAIT_TIMES; i++) {
		udelay(2);
		val = ioread32(hw->hw_addr + REG_MDIO_CTRL);
		if (!(val & (MDIO_START | MDIO_BUSY)))
			break;
J
Jay Cliburn 已提交
616
	}
J
Jay Cliburn 已提交
617

J
Jay Cliburn 已提交
618
	if (!(val & (MDIO_START | MDIO_BUSY)))
619
		return 0;
J
Jay Cliburn 已提交
620

J
Jay Cliburn 已提交
621 622
	return ATLX_ERR_PHY;
}
J
Jay Cliburn 已提交
623

J
Jay Cliburn 已提交
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
/*
 * Make L001's PHY out of Power Saving State (bug)
 * hw - Struct containing variables accessed by shared code
 * when power on, L001's PHY always on Power saving State
 * (Gigabit Link forbidden)
 */
static s32 atl1_phy_leave_power_saving(struct atl1_hw *hw)
{
	s32 ret;
	ret = atl1_write_phy_reg(hw, 29, 0x0029);
	if (ret)
		return ret;
	return atl1_write_phy_reg(hw, 30, 0);
}

/*
J
Jay Cliburn 已提交
640
 * Force the PHY into power saving mode using vendor magic.
J
Jay Cliburn 已提交
641
 */
642
#ifdef CONFIG_PM
J
Jay Cliburn 已提交
643
static void atl1_phy_enter_power_saving(struct atl1_hw *hw)
J
Jay Cliburn 已提交
644
{
J
Jay Cliburn 已提交
645 646 647 648 649 650
	atl1_write_phy_reg(hw, MII_DBG_ADDR, 0);
	atl1_write_phy_reg(hw, MII_DBG_DATA, 0x124E);
	atl1_write_phy_reg(hw, MII_DBG_ADDR, 2);
	atl1_write_phy_reg(hw, MII_DBG_DATA, 0x3000);
	atl1_write_phy_reg(hw, MII_DBG_ADDR, 3);
	atl1_write_phy_reg(hw, MII_DBG_DATA, 0);
J
Jay Cliburn 已提交
651 652

}
653
#endif
J
Jay Cliburn 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671

/*
 * Resets the PHY and make all config validate
 * hw - Struct containing variables accessed by shared code
 *
 * Sets bit 15 and 12 of the MII Control regiser (for F001 bug)
 */
static s32 atl1_phy_reset(struct atl1_hw *hw)
{
	struct pci_dev *pdev = hw->back->pdev;
	struct atl1_adapter *adapter = hw->back;
	s32 ret_val;
	u16 phy_data;

	if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
	    hw->media_type == MEDIA_TYPE_1000M_FULL)
		phy_data = MII_CR_RESET | MII_CR_AUTO_NEG_EN;
	else {
J
Jay Cliburn 已提交
672 673
		switch (hw->media_type) {
		case MEDIA_TYPE_100M_FULL:
J
Jay Cliburn 已提交
674 675 676
			phy_data =
			    MII_CR_FULL_DUPLEX | MII_CR_SPEED_100 |
			    MII_CR_RESET;
J
Jay Cliburn 已提交
677 678 679 680 681 682 683 684
			break;
		case MEDIA_TYPE_100M_HALF:
			phy_data = MII_CR_SPEED_100 | MII_CR_RESET;
			break;
		case MEDIA_TYPE_10M_FULL:
			phy_data =
			    MII_CR_FULL_DUPLEX | MII_CR_SPEED_10 | MII_CR_RESET;
			break;
685 686
		default:
			/* MEDIA_TYPE_10M_HALF: */
J
Jay Cliburn 已提交
687 688
			phy_data = MII_CR_SPEED_10 | MII_CR_RESET;
			break;
J
Jay Cliburn 已提交
689 690 691
		}
	}

J
Jay Cliburn 已提交
692 693 694 695 696 697 698 699 700 701 702 703 704 705
	ret_val = atl1_write_phy_reg(hw, MII_BMCR, phy_data);
	if (ret_val) {
		u32 val;
		int i;
		/* pcie serdes link may be down! */
		if (netif_msg_hw(adapter))
			dev_dbg(&pdev->dev, "pcie phy link down\n");

		for (i = 0; i < 25; i++) {
			msleep(1);
			val = ioread32(hw->hw_addr + REG_MDIO_CTRL);
			if (!(val & (MDIO_START | MDIO_BUSY)))
				break;
		}
J
Jay Cliburn 已提交
706

J
Jay Cliburn 已提交
707 708 709 710 711 712 713
		if ((val & (MDIO_START | MDIO_BUSY)) != 0) {
			if (netif_msg_hw(adapter))
				dev_warn(&pdev->dev,
					"pcie link down at least 25ms\n");
			return ret_val;
		}
	}
714
	return 0;
715 716
}

J
Jay Cliburn 已提交
717 718 719 720 721
/*
 * Configures PHY autoneg and flow control advertisement settings
 * hw - Struct containing variables accessed by shared code
 */
static s32 atl1_phy_setup_autoneg_adv(struct atl1_hw *hw)
J
Jay Cliburn 已提交
722
{
J
Jay Cliburn 已提交
723 724 725
	s32 ret_val;
	s16 mii_autoneg_adv_reg;
	s16 mii_1000t_ctrl_reg;
J
Jay Cliburn 已提交
726

J
Jay Cliburn 已提交
727 728
	/* Read the MII Auto-Neg Advertisement Register (Address 4). */
	mii_autoneg_adv_reg = MII_AR_DEFAULT_CAP_MASK;
J
Jay Cliburn 已提交
729

J
Jay Cliburn 已提交
730 731
	/* Read the MII 1000Base-T Control Register (Address 9). */
	mii_1000t_ctrl_reg = MII_ATLX_CR_1000T_DEFAULT_CAP_MASK;
J
Jay Cliburn 已提交
732

J
Jay Cliburn 已提交
733 734 735 736 737 738 739
	/*
	 * First we clear all the 10/100 mb speed bits in the Auto-Neg
	 * Advertisement Register (Address 4) and the 1000 mb speed bits in
	 * the  1000Base-T Control Register (Address 9).
	 */
	mii_autoneg_adv_reg &= ~MII_AR_SPEED_MASK;
	mii_1000t_ctrl_reg &= ~MII_ATLX_CR_1000T_SPEED_MASK;
J
Jay Cliburn 已提交
740

J
Jay Cliburn 已提交
741 742 743 744 745 746 747 748 749 750 751 752
	/*
	 * Need to parse media_type  and set up
	 * the appropriate PHY registers.
	 */
	switch (hw->media_type) {
	case MEDIA_TYPE_AUTO_SENSOR:
		mii_autoneg_adv_reg |= (MII_AR_10T_HD_CAPS |
					MII_AR_10T_FD_CAPS |
					MII_AR_100TX_HD_CAPS |
					MII_AR_100TX_FD_CAPS);
		mii_1000t_ctrl_reg |= MII_ATLX_CR_1000T_FD_CAPS;
		break;
J
Jay Cliburn 已提交
753

J
Jay Cliburn 已提交
754 755 756
	case MEDIA_TYPE_1000M_FULL:
		mii_1000t_ctrl_reg |= MII_ATLX_CR_1000T_FD_CAPS;
		break;
J
Jay Cliburn 已提交
757

J
Jay Cliburn 已提交
758 759 760
	case MEDIA_TYPE_100M_FULL:
		mii_autoneg_adv_reg |= MII_AR_100TX_FD_CAPS;
		break;
J
Jay Cliburn 已提交
761

J
Jay Cliburn 已提交
762 763 764
	case MEDIA_TYPE_100M_HALF:
		mii_autoneg_adv_reg |= MII_AR_100TX_HD_CAPS;
		break;
J
Jay Cliburn 已提交
765

J
Jay Cliburn 已提交
766 767
	case MEDIA_TYPE_10M_FULL:
		mii_autoneg_adv_reg |= MII_AR_10T_FD_CAPS;
J
Jay Cliburn 已提交
768
		break;
J
Jay Cliburn 已提交
769

J
Jay Cliburn 已提交
770
	default:
J
Jay Cliburn 已提交
771
		mii_autoneg_adv_reg |= MII_AR_10T_HD_CAPS;
J
Jay Cliburn 已提交
772
		break;
J
Jay Cliburn 已提交
773 774
	}

J
Jay Cliburn 已提交
775 776
	/* flow control fixed to enable all */
	mii_autoneg_adv_reg |= (MII_AR_ASM_DIR | MII_AR_PAUSE);
J
Jay Cliburn 已提交
777

J
Jay Cliburn 已提交
778 779
	hw->mii_autoneg_adv_reg = mii_autoneg_adv_reg;
	hw->mii_1000t_ctrl_reg = mii_1000t_ctrl_reg;
J
Jay Cliburn 已提交
780

J
Jay Cliburn 已提交
781 782 783
	ret_val = atl1_write_phy_reg(hw, MII_ADVERTISE, mii_autoneg_adv_reg);
	if (ret_val)
		return ret_val;
J
Jay Cliburn 已提交
784

J
Jay Cliburn 已提交
785 786 787
	ret_val = atl1_write_phy_reg(hw, MII_ATLX_CR, mii_1000t_ctrl_reg);
	if (ret_val)
		return ret_val;
J
Jay Cliburn 已提交
788

J
Jay Cliburn 已提交
789
	return 0;
J
Jay Cliburn 已提交
790 791
}

J
Jay Cliburn 已提交
792
/*
J
Jay Cliburn 已提交
793 794 795 796
 * Configures link settings.
 * hw - Struct containing variables accessed by shared code
 * Assumes the hardware has previously been reset and the
 * transmitter and receiver are not enabled.
J
Jay Cliburn 已提交
797
 */
J
Jay Cliburn 已提交
798
static s32 atl1_setup_link(struct atl1_hw *hw)
J
Jay Cliburn 已提交
799
{
J
Jay Cliburn 已提交
800 801 802
	struct pci_dev *pdev = hw->back->pdev;
	struct atl1_adapter *adapter = hw->back;
	s32 ret_val;
J
Jay Cliburn 已提交
803

J
Jay Cliburn 已提交
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
	/*
	 * Options:
	 *  PHY will advertise value(s) parsed from
	 *  autoneg_advertised and fc
	 *  no matter what autoneg is , We will not wait link result.
	 */
	ret_val = atl1_phy_setup_autoneg_adv(hw);
	if (ret_val) {
		if (netif_msg_link(adapter))
			dev_dbg(&pdev->dev,
				"error setting up autonegotiation\n");
		return ret_val;
	}
	/* SW.Reset , En-Auto-Neg if needed */
	ret_val = atl1_phy_reset(hw);
	if (ret_val) {
		if (netif_msg_link(adapter))
			dev_dbg(&pdev->dev, "error resetting phy\n");
		return ret_val;
	}
	hw->phy_configured = true;
	return ret_val;
}
J
Jay Cliburn 已提交
827

J
Jay Cliburn 已提交
828
static void atl1_init_flash_opcode(struct atl1_hw *hw)
J
Jay Cliburn 已提交
829
{
J
Jay Cliburn 已提交
830 831 832
	if (hw->flash_vendor >= ARRAY_SIZE(flash_table))
		/* Atmel */
		hw->flash_vendor = 0;
J
Jay Cliburn 已提交
833

J
Jay Cliburn 已提交
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
	/* Init OP table */
	iowrite8(flash_table[hw->flash_vendor].cmd_program,
		hw->hw_addr + REG_SPI_FLASH_OP_PROGRAM);
	iowrite8(flash_table[hw->flash_vendor].cmd_sector_erase,
		hw->hw_addr + REG_SPI_FLASH_OP_SC_ERASE);
	iowrite8(flash_table[hw->flash_vendor].cmd_chip_erase,
		hw->hw_addr + REG_SPI_FLASH_OP_CHIP_ERASE);
	iowrite8(flash_table[hw->flash_vendor].cmd_rdid,
		hw->hw_addr + REG_SPI_FLASH_OP_RDID);
	iowrite8(flash_table[hw->flash_vendor].cmd_wren,
		hw->hw_addr + REG_SPI_FLASH_OP_WREN);
	iowrite8(flash_table[hw->flash_vendor].cmd_rdsr,
		hw->hw_addr + REG_SPI_FLASH_OP_RDSR);
	iowrite8(flash_table[hw->flash_vendor].cmd_wrsr,
		hw->hw_addr + REG_SPI_FLASH_OP_WRSR);
	iowrite8(flash_table[hw->flash_vendor].cmd_read,
		hw->hw_addr + REG_SPI_FLASH_OP_READ);
J
Jay Cliburn 已提交
851 852
}

J
Jay Cliburn 已提交
853 854 855 856 857 858 859 860 861
/*
 * Performs basic configuration of the adapter.
 * hw - Struct containing variables accessed by shared code
 * Assumes that the controller has previously been reset and is in a
 * post-reset uninitialized state. Initializes multicast table,
 * and  Calls routines to setup link
 * Leaves the transmit and receive units disabled and uninitialized.
 */
static s32 atl1_init_hw(struct atl1_hw *hw)
J
Jay Cliburn 已提交
862
{
J
Jay Cliburn 已提交
863
	u32 ret_val = 0;
J
Jay Cliburn 已提交
864

J
Jay Cliburn 已提交
865 866 867 868
	/* Zero out the Multicast HASH table */
	iowrite32(0, hw->hw_addr + REG_RX_HASH_TABLE);
	/* clear the old settings from the multicast hash table */
	iowrite32(0, (hw->hw_addr + REG_RX_HASH_TABLE) + (1 << 2));
J
Jay Cliburn 已提交
869

J
Jay Cliburn 已提交
870
	atl1_init_flash_opcode(hw);
J
Jay Cliburn 已提交
871

J
Jay Cliburn 已提交
872 873 874 875 876 877 878 879 880 881 882 883 884
	if (!hw->phy_configured) {
		/* enable GPHY LinkChange Interrrupt */
		ret_val = atl1_write_phy_reg(hw, 18, 0xC00);
		if (ret_val)
			return ret_val;
		/* make PHY out of power-saving state */
		ret_val = atl1_phy_leave_power_saving(hw);
		if (ret_val)
			return ret_val;
		/* Call a subroutine to configure the link */
		ret_val = atl1_setup_link(hw);
	}
	return ret_val;
J
Jay Cliburn 已提交
885 886 887
}

/*
J
Jay Cliburn 已提交
888 889 890 891
 * Detects the current speed and duplex settings of the hardware.
 * hw - Struct containing variables accessed by shared code
 * speed - Speed of the connection
 * duplex - Duplex setting of the connection
J
Jay Cliburn 已提交
892
 */
J
Jay Cliburn 已提交
893
static s32 atl1_get_speed_and_duplex(struct atl1_hw *hw, u16 *speed, u16 *duplex)
J
Jay Cliburn 已提交
894
{
J
Jay Cliburn 已提交
895 896 897 898
	struct pci_dev *pdev = hw->back->pdev;
	struct atl1_adapter *adapter = hw->back;
	s32 ret_val;
	u16 phy_data;
J
Jay Cliburn 已提交
899

J
Jay Cliburn 已提交
900 901 902 903
	/* ; --- Read   PHY Specific Status Register (17) */
	ret_val = atl1_read_phy_reg(hw, MII_ATLX_PSSR, &phy_data);
	if (ret_val)
		return ret_val;
J
Jay Cliburn 已提交
904

J
Jay Cliburn 已提交
905 906
	if (!(phy_data & MII_ATLX_PSSR_SPD_DPLX_RESOLVED))
		return ATLX_ERR_PHY_RES;
J
Jay Cliburn 已提交
907

J
Jay Cliburn 已提交
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
	switch (phy_data & MII_ATLX_PSSR_SPEED) {
	case MII_ATLX_PSSR_1000MBS:
		*speed = SPEED_1000;
		break;
	case MII_ATLX_PSSR_100MBS:
		*speed = SPEED_100;
		break;
	case MII_ATLX_PSSR_10MBS:
		*speed = SPEED_10;
		break;
	default:
		if (netif_msg_hw(adapter))
			dev_dbg(&pdev->dev, "error getting speed\n");
		return ATLX_ERR_PHY_SPEED;
		break;
J
Jay Cliburn 已提交
923
	}
J
Jay Cliburn 已提交
924 925 926 927 928 929
	if (phy_data & MII_ATLX_PSSR_DPLX)
		*duplex = FULL_DUPLEX;
	else
		*duplex = HALF_DUPLEX;

	return 0;
J
Jay Cliburn 已提交
930
}
J
Jay Cliburn 已提交
931

J
Jay Cliburn 已提交
932
void atl1_set_mac_addr(struct atl1_hw *hw)
J
Jay Cliburn 已提交
933
{
J
Jay Cliburn 已提交
934 935 936 937 938 939 940 941 942 943 944 945 946
	u32 value;
	/*
	 * 00-0B-6A-F6-00-DC
	 * 0:  6AF600DC   1: 000B
	 * low dword
	 */
	value = (((u32) hw->mac_addr[2]) << 24) |
	    (((u32) hw->mac_addr[3]) << 16) |
	    (((u32) hw->mac_addr[4]) << 8) | (((u32) hw->mac_addr[5]));
	iowrite32(value, hw->hw_addr + REG_MAC_STA_ADDR);
	/* high dword */
	value = (((u32) hw->mac_addr[0]) << 8) | (((u32) hw->mac_addr[1]));
	iowrite32(value, (hw->hw_addr + REG_MAC_STA_ADDR) + (1 << 2));
J
Jay Cliburn 已提交
947
}
J
Jay Cliburn 已提交
948 949 950 951 952 953 954 955 956 957 958 959 960 961

/*
 * atl1_sw_init - Initialize general software structures (struct atl1_adapter)
 * @adapter: board private structure to initialize
 *
 * atl1_sw_init initializes the Adapter private data structure.
 * Fields are initialized based on PCI device information and
 * OS network device settings (MTU size).
 */
static int __devinit atl1_sw_init(struct atl1_adapter *adapter)
{
	struct atl1_hw *hw = &adapter->hw;
	struct net_device *netdev = adapter->netdev;

J
Jay Cliburn 已提交
962
	hw->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
963
	hw->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
J
Jay Cliburn 已提交
964 965 966

	adapter->wol = 0;
	adapter->rx_buffer_len = (hw->max_frame_size + 7) & ~7;
J
Jay Cliburn 已提交
967
	adapter->ict = 50000;		/* 100ms */
J
Jay Cliburn 已提交
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 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
	adapter->link_speed = SPEED_0;	/* hardware init */
	adapter->link_duplex = FULL_DUPLEX;

	hw->phy_configured = false;
	hw->preamble_len = 7;
	hw->ipgt = 0x60;
	hw->min_ifg = 0x50;
	hw->ipgr1 = 0x40;
	hw->ipgr2 = 0x60;
	hw->max_retry = 0xf;
	hw->lcol = 0x37;
	hw->jam_ipg = 7;
	hw->rfd_burst = 8;
	hw->rrd_burst = 8;
	hw->rfd_fetch_gap = 1;
	hw->rx_jumbo_th = adapter->rx_buffer_len / 8;
	hw->rx_jumbo_lkah = 1;
	hw->rrd_ret_timer = 16;
	hw->tpd_burst = 4;
	hw->tpd_fetch_th = 16;
	hw->txf_burst = 0x100;
	hw->tx_jumbo_task_th = (hw->max_frame_size + 7) >> 3;
	hw->tpd_fetch_gap = 1;
	hw->rcb_value = atl1_rcb_64;
	hw->dma_ord = atl1_dma_ord_enh;
	hw->dmar_block = atl1_dma_req_256;
	hw->dmaw_block = atl1_dma_req_256;
	hw->cmb_rrd = 4;
	hw->cmb_tpd = 4;
	hw->cmb_rx_timer = 1;	/* about 2us */
	hw->cmb_tx_timer = 1;	/* about 2us */
	hw->smb_timer = 100000;	/* about 200ms */

	spin_lock_init(&adapter->lock);
	spin_lock_init(&adapter->mb_lock);

	return 0;
}

J
Jay Cliburn 已提交
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
static int mdio_read(struct net_device *netdev, int phy_id, int reg_num)
{
	struct atl1_adapter *adapter = netdev_priv(netdev);
	u16 result;

	atl1_read_phy_reg(&adapter->hw, reg_num & 0x1f, &result);

	return result;
}

static void mdio_write(struct net_device *netdev, int phy_id, int reg_num,
	int val)
{
	struct atl1_adapter *adapter = netdev_priv(netdev);

	atl1_write_phy_reg(&adapter->hw, reg_num, val);
}

/*
 * atl1_mii_ioctl -
 * @netdev:
 * @ifreq:
 * @cmd:
 */
static int atl1_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
{
	struct atl1_adapter *adapter = netdev_priv(netdev);
	unsigned long flags;
	int retval;

	if (!netif_running(netdev))
		return -EINVAL;

	spin_lock_irqsave(&adapter->lock, flags);
	retval = generic_mii_ioctl(&adapter->mii, if_mii(ifr), cmd, NULL);
	spin_unlock_irqrestore(&adapter->lock, flags);

	return retval;
}

J
Jay Cliburn 已提交
1047 1048 1049 1050 1051 1052
/*
 * atl1_setup_mem_resources - allocate Tx / RX descriptor resources
 * @adapter: board private structure
 *
 * Return 0 on success, negative on failure
 */
J
Jay Cliburn 已提交
1053
static s32 atl1_setup_ring_resources(struct atl1_adapter *adapter)
J
Jay Cliburn 已提交
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
{
	struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
	struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
	struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
	struct atl1_ring_header *ring_header = &adapter->ring_header;
	struct pci_dev *pdev = adapter->pdev;
	int size;
	u8 offset = 0;

	size = sizeof(struct atl1_buffer) * (tpd_ring->count + rfd_ring->count);
	tpd_ring->buffer_info = kzalloc(size, GFP_KERNEL);
	if (unlikely(!tpd_ring->buffer_info)) {
J
Jay Cliburn 已提交
1066 1067 1068
		if (netif_msg_drv(adapter))
			dev_err(&pdev->dev, "kzalloc failed , size = D%d\n",
				size);
J
Jay Cliburn 已提交
1069 1070 1071
		goto err_nomem;
	}
	rfd_ring->buffer_info =
J
Jay Cliburn 已提交
1072
		(struct atl1_buffer *)(tpd_ring->buffer_info + tpd_ring->count);
J
Jay Cliburn 已提交
1073

J
Jay Cliburn 已提交
1074 1075
	/*
	 * real ring DMA buffer
J
Jay Cliburn 已提交
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
	 * each ring/block may need up to 8 bytes for alignment, hence the
	 * additional 40 bytes tacked onto the end.
	 */
	ring_header->size = size =
		sizeof(struct tx_packet_desc) * tpd_ring->count
		+ sizeof(struct rx_free_desc) * rfd_ring->count
		+ sizeof(struct rx_return_desc) * rrd_ring->count
		+ sizeof(struct coals_msg_block)
		+ sizeof(struct stats_msg_block)
		+ 40;
J
Jay Cliburn 已提交
1086 1087

	ring_header->desc = pci_alloc_consistent(pdev, ring_header->size,
J
Jay Cliburn 已提交
1088
		&ring_header->dma);
J
Jay Cliburn 已提交
1089
	if (unlikely(!ring_header->desc)) {
J
Jay Cliburn 已提交
1090 1091
		if (netif_msg_drv(adapter))
			dev_err(&pdev->dev, "pci_alloc_consistent failed\n");
J
Jay Cliburn 已提交
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
		goto err_nomem;
	}

	memset(ring_header->desc, 0, ring_header->size);

	/* init TPD ring */
	tpd_ring->dma = ring_header->dma;
	offset = (tpd_ring->dma & 0x7) ? (8 - (ring_header->dma & 0x7)) : 0;
	tpd_ring->dma += offset;
	tpd_ring->desc = (u8 *) ring_header->desc + offset;
	tpd_ring->size = sizeof(struct tx_packet_desc) * tpd_ring->count;

	/* init RFD ring */
	rfd_ring->dma = tpd_ring->dma + tpd_ring->size;
	offset = (rfd_ring->dma & 0x7) ? (8 - (rfd_ring->dma & 0x7)) : 0;
	rfd_ring->dma += offset;
	rfd_ring->desc = (u8 *) tpd_ring->desc + (tpd_ring->size + offset);
	rfd_ring->size = sizeof(struct rx_free_desc) * rfd_ring->count;
1110

J
Jay Cliburn 已提交
1111 1112 1113 1114 1115 1116 1117

	/* init RRD ring */
	rrd_ring->dma = rfd_ring->dma + rfd_ring->size;
	offset = (rrd_ring->dma & 0x7) ? (8 - (rrd_ring->dma & 0x7)) : 0;
	rrd_ring->dma += offset;
	rrd_ring->desc = (u8 *) rfd_ring->desc + (rfd_ring->size + offset);
	rrd_ring->size = sizeof(struct rx_return_desc) * rrd_ring->count;
1118

J
Jay Cliburn 已提交
1119 1120 1121 1122 1123

	/* init CMB */
	adapter->cmb.dma = rrd_ring->dma + rrd_ring->size;
	offset = (adapter->cmb.dma & 0x7) ? (8 - (adapter->cmb.dma & 0x7)) : 0;
	adapter->cmb.dma += offset;
J
Jay Cliburn 已提交
1124 1125
	adapter->cmb.cmb = (struct coals_msg_block *)
		((u8 *) rrd_ring->desc + (rrd_ring->size + offset));
J
Jay Cliburn 已提交
1126 1127 1128 1129 1130 1131

	/* init SMB */
	adapter->smb.dma = adapter->cmb.dma + sizeof(struct coals_msg_block);
	offset = (adapter->smb.dma & 0x7) ? (8 - (adapter->smb.dma & 0x7)) : 0;
	adapter->smb.dma += offset;
	adapter->smb.smb = (struct stats_msg_block *)
J
Jay Cliburn 已提交
1132 1133
		((u8 *) adapter->cmb.cmb +
		(sizeof(struct coals_msg_block) + offset));
J
Jay Cliburn 已提交
1134

J
Jay Cliburn 已提交
1135
	return 0;
J
Jay Cliburn 已提交
1136 1137 1138 1139 1140 1141

err_nomem:
	kfree(tpd_ring->buffer_info);
	return -ENOMEM;
}

1142
static void atl1_init_ring_ptrs(struct atl1_adapter *adapter)
J
Jay Cliburn 已提交
1143
{
1144 1145 1146
	struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
	struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
	struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
J
Jay Cliburn 已提交
1147

1148 1149
	atomic_set(&tpd_ring->next_to_use, 0);
	atomic_set(&tpd_ring->next_to_clean, 0);
J
Jay Cliburn 已提交
1150

1151 1152 1153 1154 1155
	rfd_ring->next_to_clean = 0;
	atomic_set(&rfd_ring->next_to_use, 0);

	rrd_ring->next_to_use = 0;
	atomic_set(&rrd_ring->next_to_clean, 0);
J
Jay Cliburn 已提交
1156 1157 1158
}

/*
J
Jay Cliburn 已提交
1159
 * atl1_clean_rx_ring - Free RFD Buffers
J
Jay Cliburn 已提交
1160 1161
 * @adapter: board private structure
 */
J
Jay Cliburn 已提交
1162
static void atl1_clean_rx_ring(struct atl1_adapter *adapter)
J
Jay Cliburn 已提交
1163
{
J
Jay Cliburn 已提交
1164 1165 1166 1167 1168 1169
	struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
	struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
	struct atl1_buffer *buffer_info;
	struct pci_dev *pdev = adapter->pdev;
	unsigned long size;
	unsigned int i;
J
Jay Cliburn 已提交
1170

J
Jay Cliburn 已提交
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
	/* Free all the Rx ring sk_buffs */
	for (i = 0; i < rfd_ring->count; i++) {
		buffer_info = &rfd_ring->buffer_info[i];
		if (buffer_info->dma) {
			pci_unmap_page(pdev, buffer_info->dma,
				buffer_info->length, PCI_DMA_FROMDEVICE);
			buffer_info->dma = 0;
		}
		if (buffer_info->skb) {
			dev_kfree_skb(buffer_info->skb);
			buffer_info->skb = NULL;
		}
	}
J
Jay Cliburn 已提交
1184

J
Jay Cliburn 已提交
1185 1186
	size = sizeof(struct atl1_buffer) * rfd_ring->count;
	memset(rfd_ring->buffer_info, 0, size);
J
Jay Cliburn 已提交
1187

J
Jay Cliburn 已提交
1188 1189
	/* Zero out the descriptor ring */
	memset(rfd_ring->desc, 0, rfd_ring->size);
J
Jay Cliburn 已提交
1190

J
Jay Cliburn 已提交
1191 1192
	rfd_ring->next_to_clean = 0;
	atomic_set(&rfd_ring->next_to_use, 0);
J
Jay Cliburn 已提交
1193

J
Jay Cliburn 已提交
1194 1195
	rrd_ring->next_to_use = 0;
	atomic_set(&rrd_ring->next_to_clean, 0);
J
Jay Cliburn 已提交
1196 1197
}

J
Jay Cliburn 已提交
1198 1199 1200 1201 1202
/*
 * atl1_clean_tx_ring - Free Tx Buffers
 * @adapter: board private structure
 */
static void atl1_clean_tx_ring(struct atl1_adapter *adapter)
J
Jay Cliburn 已提交
1203
{
J
Jay Cliburn 已提交
1204 1205
	struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
	struct atl1_buffer *buffer_info;
J
Jay Cliburn 已提交
1206
	struct pci_dev *pdev = adapter->pdev;
J
Jay Cliburn 已提交
1207 1208
	unsigned long size;
	unsigned int i;
J
Jay Cliburn 已提交
1209

J
Jay Cliburn 已提交
1210 1211 1212 1213 1214 1215 1216
	/* Free all the Tx ring sk_buffs */
	for (i = 0; i < tpd_ring->count; i++) {
		buffer_info = &tpd_ring->buffer_info[i];
		if (buffer_info->dma) {
			pci_unmap_page(pdev, buffer_info->dma,
				buffer_info->length, PCI_DMA_TODEVICE);
			buffer_info->dma = 0;
J
Jay Cliburn 已提交
1217 1218 1219
		}
	}

J
Jay Cliburn 已提交
1220 1221 1222 1223 1224
	for (i = 0; i < tpd_ring->count; i++) {
		buffer_info = &tpd_ring->buffer_info[i];
		if (buffer_info->skb) {
			dev_kfree_skb_any(buffer_info->skb);
			buffer_info->skb = NULL;
J
Jay Cliburn 已提交
1225 1226 1227
		}
	}

J
Jay Cliburn 已提交
1228 1229
	size = sizeof(struct atl1_buffer) * tpd_ring->count;
	memset(tpd_ring->buffer_info, 0, size);
J
Jay Cliburn 已提交
1230

J
Jay Cliburn 已提交
1231 1232
	/* Zero out the descriptor ring */
	memset(tpd_ring->desc, 0, tpd_ring->size);
J
Jay Cliburn 已提交
1233

J
Jay Cliburn 已提交
1234 1235
	atomic_set(&tpd_ring->next_to_use, 0);
	atomic_set(&tpd_ring->next_to_clean, 0);
J
Jay Cliburn 已提交
1236 1237 1238
}

/*
J
Jay Cliburn 已提交
1239 1240 1241 1242
 * atl1_free_ring_resources - Free Tx / RX descriptor Resources
 * @adapter: board private structure
 *
 * Free all transmit software resources
J
Jay Cliburn 已提交
1243
 */
J
Jay Cliburn 已提交
1244
static void atl1_free_ring_resources(struct atl1_adapter *adapter)
J
Jay Cliburn 已提交
1245 1246
{
	struct pci_dev *pdev = adapter->pdev;
J
Jay Cliburn 已提交
1247 1248 1249 1250
	struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
	struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
	struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
	struct atl1_ring_header *ring_header = &adapter->ring_header;
J
Jay Cliburn 已提交
1251

J
Jay Cliburn 已提交
1252 1253
	atl1_clean_tx_ring(adapter);
	atl1_clean_rx_ring(adapter);
J
Jay Cliburn 已提交
1254

J
Jay Cliburn 已提交
1255 1256 1257
	kfree(tpd_ring->buffer_info);
	pci_free_consistent(pdev, ring_header->size, ring_header->desc,
		ring_header->dma);
J
Jay Cliburn 已提交
1258

J
Jay Cliburn 已提交
1259 1260 1261
	tpd_ring->buffer_info = NULL;
	tpd_ring->desc = NULL;
	tpd_ring->dma = 0;
J
Jay Cliburn 已提交
1262

J
Jay Cliburn 已提交
1263 1264 1265
	rfd_ring->buffer_info = NULL;
	rfd_ring->desc = NULL;
	rfd_ring->dma = 0;
J
Jay Cliburn 已提交
1266

J
Jay Cliburn 已提交
1267 1268
	rrd_ring->desc = NULL;
	rrd_ring->dma = 0;
J
Jay Cliburn 已提交
1269 1270
}

J
Jay Cliburn 已提交
1271
static void atl1_setup_mac_ctrl(struct atl1_adapter *adapter)
J
Jay Cliburn 已提交
1272 1273
{
	u32 value;
J
Jay Cliburn 已提交
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
	struct atl1_hw *hw = &adapter->hw;
	struct net_device *netdev = adapter->netdev;
	/* Config MAC CTRL Register */
	value = MAC_CTRL_TX_EN | MAC_CTRL_RX_EN;
	/* duplex */
	if (FULL_DUPLEX == adapter->link_duplex)
		value |= MAC_CTRL_DUPLX;
	/* speed */
	value |= ((u32) ((SPEED_1000 == adapter->link_speed) ?
			 MAC_CTRL_SPEED_1000 : MAC_CTRL_SPEED_10_100) <<
		  MAC_CTRL_SPEED_SHIFT);
	/* flow control */
	value |= (MAC_CTRL_TX_FLOW | MAC_CTRL_RX_FLOW);
	/* PAD & CRC */
	value |= (MAC_CTRL_ADD_CRC | MAC_CTRL_PAD);
	/* preamble length */
	value |= (((u32) adapter->hw.preamble_len
		   & MAC_CTRL_PRMLEN_MASK) << MAC_CTRL_PRMLEN_SHIFT);
	/* vlan */
	if (adapter->vlgrp)
		value |= MAC_CTRL_RMV_VLAN;
	/* rx checksum
	   if (adapter->rx_csum)
	   value |= MAC_CTRL_RX_CHKSUM_EN;
	 */
	/* filter mode */
	value |= MAC_CTRL_BC_EN;
	if (netdev->flags & IFF_PROMISC)
		value |= MAC_CTRL_PROMIS_EN;
	else if (netdev->flags & IFF_ALLMULTI)
		value |= MAC_CTRL_MC_ALL_EN;
	/* value |= MAC_CTRL_LOOPBACK; */
	iowrite32(value, hw->hw_addr + REG_MAC_CTRL);
}
J
Jay Cliburn 已提交
1308

J
Jay Cliburn 已提交
1309 1310 1311 1312 1313 1314 1315
static u32 atl1_check_link(struct atl1_adapter *adapter)
{
	struct atl1_hw *hw = &adapter->hw;
	struct net_device *netdev = adapter->netdev;
	u32 ret_val;
	u16 speed, duplex, phy_data;
	int reconfig = 0;
J
Jay Cliburn 已提交
1316

J
Jay Cliburn 已提交
1317 1318 1319
	/* MII_BMSR must read twice */
	atl1_read_phy_reg(hw, MII_BMSR, &phy_data);
	atl1_read_phy_reg(hw, MII_BMSR, &phy_data);
J
Jay Cliburn 已提交
1320 1321 1322 1323 1324 1325
	if (!(phy_data & BMSR_LSTATUS)) {
		/* link down */
		if (netif_carrier_ok(netdev)) {
			/* old link state: Up */
			if (netif_msg_link(adapter))
				dev_info(&adapter->pdev->dev, "link is down\n");
J
Jay Cliburn 已提交
1326 1327 1328
			adapter->link_speed = SPEED_0;
			netif_carrier_off(netdev);
			netif_stop_queue(netdev);
J
Jay Cliburn 已提交
1329
		}
J
Jay Cliburn 已提交
1330
		return 0;
J
Jay Cliburn 已提交
1331 1332
	}

J
Jay Cliburn 已提交
1333 1334 1335 1336
	/* Link Up */
	ret_val = atl1_get_speed_and_duplex(hw, &speed, &duplex);
	if (ret_val)
		return ret_val;
J
Jay Cliburn 已提交
1337

J
Jay Cliburn 已提交
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
	switch (hw->media_type) {
	case MEDIA_TYPE_1000M_FULL:
		if (speed != SPEED_1000 || duplex != FULL_DUPLEX)
			reconfig = 1;
		break;
	case MEDIA_TYPE_100M_FULL:
		if (speed != SPEED_100 || duplex != FULL_DUPLEX)
			reconfig = 1;
		break;
	case MEDIA_TYPE_100M_HALF:
		if (speed != SPEED_100 || duplex != HALF_DUPLEX)
			reconfig = 1;
		break;
	case MEDIA_TYPE_10M_FULL:
		if (speed != SPEED_10 || duplex != FULL_DUPLEX)
			reconfig = 1;
		break;
	case MEDIA_TYPE_10M_HALF:
		if (speed != SPEED_10 || duplex != HALF_DUPLEX)
			reconfig = 1;
		break;
	}
J
Jay Cliburn 已提交
1360

J
Jay Cliburn 已提交
1361 1362 1363 1364 1365 1366 1367
	/* link result is our setting */
	if (!reconfig) {
		if (adapter->link_speed != speed
		    || adapter->link_duplex != duplex) {
			adapter->link_speed = speed;
			adapter->link_duplex = duplex;
			atl1_setup_mac_ctrl(adapter);
J
Jay Cliburn 已提交
1368 1369 1370 1371 1372 1373
			if (netif_msg_link(adapter))
				dev_info(&adapter->pdev->dev,
					"%s link is up %d Mbps %s\n",
					netdev->name, adapter->link_speed,
					adapter->link_duplex == FULL_DUPLEX ?
					"full duplex" : "half duplex");
J
Jay Cliburn 已提交
1374
		}
J
Jay Cliburn 已提交
1375 1376
		if (!netif_carrier_ok(netdev)) {
			/* Link down -> Up */
J
Jay Cliburn 已提交
1377 1378 1379
			netif_carrier_on(netdev);
			netif_wake_queue(netdev);
		}
J
Jay Cliburn 已提交
1380
		return 0;
J
Jay Cliburn 已提交
1381 1382
	}

J
Jay Cliburn 已提交
1383
	/* change original link status */
J
Jay Cliburn 已提交
1384 1385 1386 1387
	if (netif_carrier_ok(netdev)) {
		adapter->link_speed = SPEED_0;
		netif_carrier_off(netdev);
		netif_stop_queue(netdev);
J
Jay Cliburn 已提交
1388 1389
	}

J
Jay Cliburn 已提交
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
	if (hw->media_type != MEDIA_TYPE_AUTO_SENSOR &&
	    hw->media_type != MEDIA_TYPE_1000M_FULL) {
		switch (hw->media_type) {
		case MEDIA_TYPE_100M_FULL:
			phy_data = MII_CR_FULL_DUPLEX | MII_CR_SPEED_100 |
			           MII_CR_RESET;
			break;
		case MEDIA_TYPE_100M_HALF:
			phy_data = MII_CR_SPEED_100 | MII_CR_RESET;
			break;
		case MEDIA_TYPE_10M_FULL:
			phy_data =
			    MII_CR_FULL_DUPLEX | MII_CR_SPEED_10 | MII_CR_RESET;
			break;
J
Jay Cliburn 已提交
1404 1405
		default:
			/* MEDIA_TYPE_10M_HALF: */
J
Jay Cliburn 已提交
1406 1407
			phy_data = MII_CR_SPEED_10 | MII_CR_RESET;
			break;
J
Jay Cliburn 已提交
1408
		}
J
Jay Cliburn 已提交
1409
		atl1_write_phy_reg(hw, MII_BMCR, phy_data);
J
Jay Cliburn 已提交
1410
		return 0;
J
Jay Cliburn 已提交
1411 1412
	}

J
Jay Cliburn 已提交
1413 1414 1415 1416
	/* auto-neg, insert timer to re-config phy */
	if (!adapter->phy_timer_pending) {
		adapter->phy_timer_pending = true;
		mod_timer(&adapter->phy_config_timer, jiffies + 3 * HZ);
J
Jay Cliburn 已提交
1417 1418
	}

J
Jay Cliburn 已提交
1419 1420
	return 0;
}
J
Jay Cliburn 已提交
1421

J
Jay Cliburn 已提交
1422 1423 1424
static void set_flow_ctrl_old(struct atl1_adapter *adapter)
{
	u32 hi, lo, value;
J
Jay Cliburn 已提交
1425

J
Jay Cliburn 已提交
1426 1427 1428 1429 1430 1431
	/* RFD Flow Control */
	value = adapter->rfd_ring.count;
	hi = value / 16;
	if (hi < 2)
		hi = 2;
	lo = value * 7 / 8;
J
Jay Cliburn 已提交
1432

J
Jay Cliburn 已提交
1433 1434 1435
	value = ((hi & RXQ_RXF_PAUSE_TH_HI_MASK) << RXQ_RXF_PAUSE_TH_HI_SHIFT) |
		((lo & RXQ_RXF_PAUSE_TH_LO_MASK) << RXQ_RXF_PAUSE_TH_LO_SHIFT);
	iowrite32(value, adapter->hw.hw_addr + REG_RXQ_RXF_PAUSE_THRESH);
J
Jay Cliburn 已提交
1436

J
Jay Cliburn 已提交
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
	/* RRD Flow Control */
	value = adapter->rrd_ring.count;
	lo = value / 16;
	hi = value * 7 / 8;
	if (lo < 2)
		lo = 2;
	value = ((hi & RXQ_RRD_PAUSE_TH_HI_MASK) << RXQ_RRD_PAUSE_TH_HI_SHIFT) |
		((lo & RXQ_RRD_PAUSE_TH_LO_MASK) << RXQ_RRD_PAUSE_TH_LO_SHIFT);
	iowrite32(value, adapter->hw.hw_addr + REG_RXQ_RRD_PAUSE_THRESH);
}
J
Jay Cliburn 已提交
1447

J
Jay Cliburn 已提交
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 1494 1495 1496 1497 1498 1499 1500
static void set_flow_ctrl_new(struct atl1_hw *hw)
{
	u32 hi, lo, value;

	/* RXF Flow Control */
	value = ioread32(hw->hw_addr + REG_SRAM_RXF_LEN);
	lo = value / 16;
	if (lo < 192)
		lo = 192;
	hi = value * 7 / 8;
	if (hi < lo)
		hi = lo + 16;
	value = ((hi & RXQ_RXF_PAUSE_TH_HI_MASK) << RXQ_RXF_PAUSE_TH_HI_SHIFT) |
		((lo & RXQ_RXF_PAUSE_TH_LO_MASK) << RXQ_RXF_PAUSE_TH_LO_SHIFT);
	iowrite32(value, hw->hw_addr + REG_RXQ_RXF_PAUSE_THRESH);

	/* RRD Flow Control */
	value = ioread32(hw->hw_addr + REG_SRAM_RRD_LEN);
	lo = value / 8;
	hi = value * 7 / 8;
	if (lo < 2)
		lo = 2;
	if (hi < lo)
		hi = lo + 3;
	value = ((hi & RXQ_RRD_PAUSE_TH_HI_MASK) << RXQ_RRD_PAUSE_TH_HI_SHIFT) |
		((lo & RXQ_RRD_PAUSE_TH_LO_MASK) << RXQ_RRD_PAUSE_TH_LO_SHIFT);
	iowrite32(value, hw->hw_addr + REG_RXQ_RRD_PAUSE_THRESH);
}

/*
 * atl1_configure - Configure Transmit&Receive Unit after Reset
 * @adapter: board private structure
 *
 * Configure the Tx /Rx unit of the MAC after a reset.
 */
static u32 atl1_configure(struct atl1_adapter *adapter)
{
	struct atl1_hw *hw = &adapter->hw;
	u32 value;

	/* clear interrupt status */
	iowrite32(0xffffffff, adapter->hw.hw_addr + REG_ISR);

	/* set MAC Address */
	value = (((u32) hw->mac_addr[2]) << 24) |
		(((u32) hw->mac_addr[3]) << 16) |
		(((u32) hw->mac_addr[4]) << 8) |
		(((u32) hw->mac_addr[5]));
	iowrite32(value, hw->hw_addr + REG_MAC_STA_ADDR);
	value = (((u32) hw->mac_addr[0]) << 8) | (((u32) hw->mac_addr[1]));
	iowrite32(value, hw->hw_addr + (REG_MAC_STA_ADDR + 4));

	/* tx / rx ring */
J
Jay Cliburn 已提交
1501

J
Jay Cliburn 已提交
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
	/* HI base address */
	iowrite32((u32) ((adapter->tpd_ring.dma & 0xffffffff00000000ULL) >> 32),
		hw->hw_addr + REG_DESC_BASE_ADDR_HI);
	/* LO base address */
	iowrite32((u32) (adapter->rfd_ring.dma & 0x00000000ffffffffULL),
		hw->hw_addr + REG_DESC_RFD_ADDR_LO);
	iowrite32((u32) (adapter->rrd_ring.dma & 0x00000000ffffffffULL),
		hw->hw_addr + REG_DESC_RRD_ADDR_LO);
	iowrite32((u32) (adapter->tpd_ring.dma & 0x00000000ffffffffULL),
		hw->hw_addr + REG_DESC_TPD_ADDR_LO);
	iowrite32((u32) (adapter->cmb.dma & 0x00000000ffffffffULL),
		hw->hw_addr + REG_DESC_CMB_ADDR_LO);
	iowrite32((u32) (adapter->smb.dma & 0x00000000ffffffffULL),
		hw->hw_addr + REG_DESC_SMB_ADDR_LO);
J
Jay Cliburn 已提交
1516

J
Jay Cliburn 已提交
1517 1518 1519 1520 1521 1522 1523
	/* element count */
	value = adapter->rrd_ring.count;
	value <<= 16;
	value += adapter->rfd_ring.count;
	iowrite32(value, hw->hw_addr + REG_DESC_RFD_RRD_RING_SIZE);
	iowrite32(adapter->tpd_ring.count, hw->hw_addr +
		REG_DESC_TPD_RING_SIZE);
J
Jay Cliburn 已提交
1524

J
Jay Cliburn 已提交
1525 1526
	/* Load Ptr */
	iowrite32(1, hw->hw_addr + REG_LOAD_PTR);
J
Jay Cliburn 已提交
1527

J
Jay Cliburn 已提交
1528 1529 1530 1531 1532 1533 1534 1535
	/* config Mailbox */
	value = ((atomic_read(&adapter->tpd_ring.next_to_use)
		  & MB_TPD_PROD_INDX_MASK) << MB_TPD_PROD_INDX_SHIFT) |
		((atomic_read(&adapter->rrd_ring.next_to_clean)
		& MB_RRD_CONS_INDX_MASK) << MB_RRD_CONS_INDX_SHIFT) |
		((atomic_read(&adapter->rfd_ring.next_to_use)
		& MB_RFD_PROD_INDX_MASK) << MB_RFD_PROD_INDX_SHIFT);
	iowrite32(value, hw->hw_addr + REG_MAILBOX);
J
Jay Cliburn 已提交
1536

J
Jay Cliburn 已提交
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
	/* config IPG/IFG */
	value = (((u32) hw->ipgt & MAC_IPG_IFG_IPGT_MASK)
		 << MAC_IPG_IFG_IPGT_SHIFT) |
		(((u32) hw->min_ifg & MAC_IPG_IFG_MIFG_MASK)
		<< MAC_IPG_IFG_MIFG_SHIFT) |
		(((u32) hw->ipgr1 & MAC_IPG_IFG_IPGR1_MASK)
		<< MAC_IPG_IFG_IPGR1_SHIFT) |
		(((u32) hw->ipgr2 & MAC_IPG_IFG_IPGR2_MASK)
		<< MAC_IPG_IFG_IPGR2_SHIFT);
	iowrite32(value, hw->hw_addr + REG_MAC_IPG_IFG);
J
Jay Cliburn 已提交
1547

J
Jay Cliburn 已提交
1548 1549 1550 1551 1552 1553 1554 1555 1556
	/* config  Half-Duplex Control */
	value = ((u32) hw->lcol & MAC_HALF_DUPLX_CTRL_LCOL_MASK) |
		(((u32) hw->max_retry & MAC_HALF_DUPLX_CTRL_RETRY_MASK)
		<< MAC_HALF_DUPLX_CTRL_RETRY_SHIFT) |
		MAC_HALF_DUPLX_CTRL_EXC_DEF_EN |
		(0xa << MAC_HALF_DUPLX_CTRL_ABEBT_SHIFT) |
		(((u32) hw->jam_ipg & MAC_HALF_DUPLX_CTRL_JAMIPG_MASK)
		<< MAC_HALF_DUPLX_CTRL_JAMIPG_SHIFT);
	iowrite32(value, hw->hw_addr + REG_MAC_HALF_DUPLX_CTRL);
J
Jay Cliburn 已提交
1557

J
Jay Cliburn 已提交
1558 1559 1560
	/* set Interrupt Moderator Timer */
	iowrite16(adapter->imt, hw->hw_addr + REG_IRQ_MODU_TIMER_INIT);
	iowrite32(MASTER_CTRL_ITIMER_EN, hw->hw_addr + REG_MASTER_CTRL);
J
Jay Cliburn 已提交
1561

J
Jay Cliburn 已提交
1562 1563
	/* set Interrupt Clear Timer */
	iowrite16(adapter->ict, hw->hw_addr + REG_CMBDISDMA_TIMER);
J
Jay Cliburn 已提交
1564

J
Jay Cliburn 已提交
1565 1566
	/* set max frame size hw will accept */
	iowrite32(hw->max_frame_size, hw->hw_addr + REG_MTU);
J
Jay Cliburn 已提交
1567

J
Jay Cliburn 已提交
1568 1569 1570 1571 1572 1573 1574 1575
	/* jumbo size & rrd retirement timer */
	value = (((u32) hw->rx_jumbo_th & RXQ_JMBOSZ_TH_MASK)
		 << RXQ_JMBOSZ_TH_SHIFT) |
		(((u32) hw->rx_jumbo_lkah & RXQ_JMBO_LKAH_MASK)
		<< RXQ_JMBO_LKAH_SHIFT) |
		(((u32) hw->rrd_ret_timer & RXQ_RRD_TIMER_MASK)
		<< RXQ_RRD_TIMER_SHIFT);
	iowrite32(value, hw->hw_addr + REG_RXQ_JMBOSZ_RRDTIM);
J
Jay Cliburn 已提交
1576

J
Jay Cliburn 已提交
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
	/* Flow Control */
	switch (hw->dev_rev) {
	case 0x8001:
	case 0x9001:
	case 0x9002:
	case 0x9003:
		set_flow_ctrl_old(adapter);
		break;
	default:
		set_flow_ctrl_new(hw);
		break;
J
Jay Cliburn 已提交
1588 1589
	}

J
Jay Cliburn 已提交
1590 1591 1592 1593 1594 1595 1596 1597 1598
	/* config TXQ */
	value = (((u32) hw->tpd_burst & TXQ_CTRL_TPD_BURST_NUM_MASK)
		 << TXQ_CTRL_TPD_BURST_NUM_SHIFT) |
		(((u32) hw->txf_burst & TXQ_CTRL_TXF_BURST_NUM_MASK)
		<< TXQ_CTRL_TXF_BURST_NUM_SHIFT) |
		(((u32) hw->tpd_fetch_th & TXQ_CTRL_TPD_FETCH_TH_MASK)
		<< TXQ_CTRL_TPD_FETCH_TH_SHIFT) | TXQ_CTRL_ENH_MODE |
		TXQ_CTRL_EN;
	iowrite32(value, hw->hw_addr + REG_TXQ_CTRL);
J
Jay Cliburn 已提交
1599

J
Jay Cliburn 已提交
1600 1601 1602 1603 1604 1605
	/* min tpd fetch gap & tx jumbo packet size threshold for taskoffload */
	value = (((u32) hw->tx_jumbo_task_th & TX_JUMBO_TASK_TH_MASK)
		<< TX_JUMBO_TASK_TH_SHIFT) |
		(((u32) hw->tpd_fetch_gap & TX_TPD_MIN_IPG_MASK)
		<< TX_TPD_MIN_IPG_SHIFT);
	iowrite32(value, hw->hw_addr + REG_TX_JUMBO_TASK_TH_TPD_IPG);
J
Jay Cliburn 已提交
1606

J
Jay Cliburn 已提交
1607 1608 1609 1610 1611 1612 1613 1614 1615
	/* config RXQ */
	value = (((u32) hw->rfd_burst & RXQ_CTRL_RFD_BURST_NUM_MASK)
		<< RXQ_CTRL_RFD_BURST_NUM_SHIFT) |
		(((u32) hw->rrd_burst & RXQ_CTRL_RRD_BURST_THRESH_MASK)
		<< RXQ_CTRL_RRD_BURST_THRESH_SHIFT) |
		(((u32) hw->rfd_fetch_gap & RXQ_CTRL_RFD_PREF_MIN_IPG_MASK)
		<< RXQ_CTRL_RFD_PREF_MIN_IPG_SHIFT) | RXQ_CTRL_CUT_THRU_EN |
		RXQ_CTRL_EN;
	iowrite32(value, hw->hw_addr + REG_RXQ_CTRL);
J
Jay Cliburn 已提交
1616

J
Jay Cliburn 已提交
1617 1618 1619
	/* config DMA Engine */
	value = ((((u32) hw->dmar_block) & DMA_CTRL_DMAR_BURST_LEN_MASK)
		<< DMA_CTRL_DMAR_BURST_LEN_SHIFT) |
J
Jay Cliburn 已提交
1620 1621
		((((u32) hw->dmaw_block) & DMA_CTRL_DMAW_BURST_LEN_MASK)
		<< DMA_CTRL_DMAW_BURST_LEN_SHIFT) | DMA_CTRL_DMAR_EN |
J
Jay Cliburn 已提交
1622 1623 1624 1625 1626
		DMA_CTRL_DMAW_EN;
	value |= (u32) hw->dma_ord;
	if (atl1_rcb_128 == hw->rcb_value)
		value |= DMA_CTRL_RCB_VALUE;
	iowrite32(value, hw->hw_addr + REG_DMA_CTRL);
J
Jay Cliburn 已提交
1627

J
Jay Cliburn 已提交
1628
	/* config CMB / SMB */
J
Jay Cliburn 已提交
1629 1630 1631 1632
	value = (hw->cmb_tpd > adapter->tpd_ring.count) ?
		hw->cmb_tpd : adapter->tpd_ring.count;
	value <<= 16;
	value |= hw->cmb_rrd;
J
Jay Cliburn 已提交
1633 1634 1635 1636
	iowrite32(value, hw->hw_addr + REG_CMB_WRITE_TH);
	value = hw->cmb_rx_timer | ((u32) hw->cmb_tx_timer << 16);
	iowrite32(value, hw->hw_addr + REG_CMB_WRITE_TIMER);
	iowrite32(hw->smb_timer, hw->hw_addr + REG_SMB_TIMER);
J
Jay Cliburn 已提交
1637

J
Jay Cliburn 已提交
1638 1639 1640
	/* --- enable CMB / SMB */
	value = CSMB_CTRL_CMB_EN | CSMB_CTRL_SMB_EN;
	iowrite32(value, hw->hw_addr + REG_CSMB_CTRL);
J
Jay Cliburn 已提交
1641

J
Jay Cliburn 已提交
1642 1643 1644 1645 1646
	value = ioread32(adapter->hw.hw_addr + REG_ISR);
	if (unlikely((value & ISR_PHY_LINKDOWN) != 0))
		value = 1;	/* config failed */
	else
		value = 0;
J
Jay Cliburn 已提交
1647

J
Jay Cliburn 已提交
1648 1649 1650 1651
	/* clear all interrupt status */
	iowrite32(0x3fffffff, adapter->hw.hw_addr + REG_ISR);
	iowrite32(0, adapter->hw.hw_addr + REG_ISR);
	return value;
J
Jay Cliburn 已提交
1652 1653
}

J
Jay Cliburn 已提交
1654 1655 1656 1657
/*
 * atl1_pcie_patch - Patch for PCIE module
 */
static void atl1_pcie_patch(struct atl1_adapter *adapter)
J
Jay Cliburn 已提交
1658
{
J
Jay Cliburn 已提交
1659
	u32 value;
J
Jay Cliburn 已提交
1660

J
Jay Cliburn 已提交
1661 1662 1663 1664 1665 1666 1667
	/* much vendor magic here */
	value = 0x6500;
	iowrite32(value, adapter->hw.hw_addr + 0x12FC);
	/* pcie flow control mode change */
	value = ioread32(adapter->hw.hw_addr + 0x1008);
	value |= 0x8000;
	iowrite32(value, adapter->hw.hw_addr + 0x1008);
J
Jay Cliburn 已提交
1668 1669 1670
}

/*
J
Jay Cliburn 已提交
1671 1672 1673 1674
 * When ACPI resume on some VIA MotherBoard, the Interrupt Disable bit/0x400
 * on PCI Command register is disable.
 * The function enable this bit.
 * Brackett, 2006/03/15
J
Jay Cliburn 已提交
1675
 */
J
Jay Cliburn 已提交
1676
static void atl1_via_workaround(struct atl1_adapter *adapter)
J
Jay Cliburn 已提交
1677
{
J
Jay Cliburn 已提交
1678
	unsigned long value;
J
Jay Cliburn 已提交
1679

J
Jay Cliburn 已提交
1680 1681 1682 1683
	value = ioread16(adapter->hw.hw_addr + PCI_COMMAND);
	if (value & PCI_COMMAND_INTX_DISABLE)
		value &= ~PCI_COMMAND_INTX_DISABLE;
	iowrite32(value, adapter->hw.hw_addr + PCI_COMMAND);
J
Jay Cliburn 已提交
1684 1685
}

J
Jay Cliburn 已提交
1686 1687 1688
static void atl1_inc_smb(struct atl1_adapter *adapter)
{
	struct stats_msg_block *smb = adapter->smb.smb;
J
Jay Cliburn 已提交
1689

J
Jay Cliburn 已提交
1690 1691 1692 1693 1694 1695 1696 1697
	/* Fill out the OS statistics structure */
	adapter->soft_stats.rx_packets += smb->rx_ok;
	adapter->soft_stats.tx_packets += smb->tx_ok;
	adapter->soft_stats.rx_bytes += smb->rx_byte_cnt;
	adapter->soft_stats.tx_bytes += smb->tx_byte_cnt;
	adapter->soft_stats.multicast += smb->rx_mcast;
	adapter->soft_stats.collisions += (smb->tx_1_col + smb->tx_2_col * 2 +
		smb->tx_late_col + smb->tx_abort_col * adapter->hw.max_retry);
J
Jay Cliburn 已提交
1698

J
Jay Cliburn 已提交
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
	/* Rx Errors */
	adapter->soft_stats.rx_errors += (smb->rx_frag + smb->rx_fcs_err +
		smb->rx_len_err + smb->rx_sz_ov + smb->rx_rxf_ov +
		smb->rx_rrd_ov + smb->rx_align_err);
	adapter->soft_stats.rx_fifo_errors += smb->rx_rxf_ov;
	adapter->soft_stats.rx_length_errors += smb->rx_len_err;
	adapter->soft_stats.rx_crc_errors += smb->rx_fcs_err;
	adapter->soft_stats.rx_frame_errors += smb->rx_align_err;
	adapter->soft_stats.rx_missed_errors += (smb->rx_rrd_ov +
		smb->rx_rxf_ov);
J
Jay Cliburn 已提交
1709

J
Jay Cliburn 已提交
1710 1711 1712
	adapter->soft_stats.rx_pause += smb->rx_pause;
	adapter->soft_stats.rx_rrd_ov += smb->rx_rrd_ov;
	adapter->soft_stats.rx_trunc += smb->rx_sz_ov;
J
Jay Cliburn 已提交
1713

J
Jay Cliburn 已提交
1714 1715 1716 1717 1718 1719
	/* Tx Errors */
	adapter->soft_stats.tx_errors += (smb->tx_late_col +
		smb->tx_abort_col + smb->tx_underrun + smb->tx_trunc);
	adapter->soft_stats.tx_fifo_errors += smb->tx_underrun;
	adapter->soft_stats.tx_aborted_errors += smb->tx_abort_col;
	adapter->soft_stats.tx_window_errors += smb->tx_late_col;
J
Jay Cliburn 已提交
1720

J
Jay Cliburn 已提交
1721 1722 1723 1724 1725 1726 1727 1728
	adapter->soft_stats.excecol += smb->tx_abort_col;
	adapter->soft_stats.deffer += smb->tx_defer;
	adapter->soft_stats.scc += smb->tx_1_col;
	adapter->soft_stats.mcc += smb->tx_2_col;
	adapter->soft_stats.latecol += smb->tx_late_col;
	adapter->soft_stats.tx_underun += smb->tx_underrun;
	adapter->soft_stats.tx_trunc += smb->tx_trunc;
	adapter->soft_stats.tx_pause += smb->tx_pause;
J
Jay Cliburn 已提交
1729

J
Jay Cliburn 已提交
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
	adapter->net_stats.rx_packets = adapter->soft_stats.rx_packets;
	adapter->net_stats.tx_packets = adapter->soft_stats.tx_packets;
	adapter->net_stats.rx_bytes = adapter->soft_stats.rx_bytes;
	adapter->net_stats.tx_bytes = adapter->soft_stats.tx_bytes;
	adapter->net_stats.multicast = adapter->soft_stats.multicast;
	adapter->net_stats.collisions = adapter->soft_stats.collisions;
	adapter->net_stats.rx_errors = adapter->soft_stats.rx_errors;
	adapter->net_stats.rx_over_errors =
		adapter->soft_stats.rx_missed_errors;
	adapter->net_stats.rx_length_errors =
		adapter->soft_stats.rx_length_errors;
	adapter->net_stats.rx_crc_errors = adapter->soft_stats.rx_crc_errors;
	adapter->net_stats.rx_frame_errors =
		adapter->soft_stats.rx_frame_errors;
	adapter->net_stats.rx_fifo_errors = adapter->soft_stats.rx_fifo_errors;
	adapter->net_stats.rx_missed_errors =
		adapter->soft_stats.rx_missed_errors;
	adapter->net_stats.tx_errors = adapter->soft_stats.tx_errors;
	adapter->net_stats.tx_fifo_errors = adapter->soft_stats.tx_fifo_errors;
	adapter->net_stats.tx_aborted_errors =
		adapter->soft_stats.tx_aborted_errors;
	adapter->net_stats.tx_window_errors =
		adapter->soft_stats.tx_window_errors;
	adapter->net_stats.tx_carrier_errors =
		adapter->soft_stats.tx_carrier_errors;
J
Jay Cliburn 已提交
1755 1756
}

J
Jay Cliburn 已提交
1757
static void atl1_update_mailbox(struct atl1_adapter *adapter)
J
Jay Cliburn 已提交
1758
{
J
Jay Cliburn 已提交
1759 1760 1761 1762
	unsigned long flags;
	u32 tpd_next_to_use;
	u32 rfd_next_to_use;
	u32 rrd_next_to_clean;
J
Jay Cliburn 已提交
1763 1764
	u32 value;

J
Jay Cliburn 已提交
1765
	spin_lock_irqsave(&adapter->mb_lock, flags);
J
Jay Cliburn 已提交
1766

J
Jay Cliburn 已提交
1767 1768 1769
	tpd_next_to_use = atomic_read(&adapter->tpd_ring.next_to_use);
	rfd_next_to_use = atomic_read(&adapter->rfd_ring.next_to_use);
	rrd_next_to_clean = atomic_read(&adapter->rrd_ring.next_to_clean);
J
Jay Cliburn 已提交
1770

J
Jay Cliburn 已提交
1771 1772 1773 1774 1775 1776 1777
	value = ((rfd_next_to_use & MB_RFD_PROD_INDX_MASK) <<
		MB_RFD_PROD_INDX_SHIFT) |
		((rrd_next_to_clean & MB_RRD_CONS_INDX_MASK) <<
		MB_RRD_CONS_INDX_SHIFT) |
		((tpd_next_to_use & MB_TPD_PROD_INDX_MASK) <<
		MB_TPD_PROD_INDX_SHIFT);
	iowrite32(value, adapter->hw.hw_addr + REG_MAILBOX);
J
Jay Cliburn 已提交
1778

J
Jay Cliburn 已提交
1779
	spin_unlock_irqrestore(&adapter->mb_lock, flags);
J
Jay Cliburn 已提交
1780 1781
}

J
Jay Cliburn 已提交
1782 1783
static void atl1_clean_alloc_flag(struct atl1_adapter *adapter,
	struct rx_return_desc *rrd, u16 offset)
J
Jay Cliburn 已提交
1784
{
J
Jay Cliburn 已提交
1785
	struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
J
Jay Cliburn 已提交
1786

J
Jay Cliburn 已提交
1787 1788 1789 1790
	while (rfd_ring->next_to_clean != (rrd->buf_indx + offset)) {
		rfd_ring->buffer_info[rfd_ring->next_to_clean].alloced = 0;
		if (++rfd_ring->next_to_clean == rfd_ring->count) {
			rfd_ring->next_to_clean = 0;
J
Jay Cliburn 已提交
1791 1792
		}
	}
J
Jay Cliburn 已提交
1793
}
J
Jay Cliburn 已提交
1794

J
Jay Cliburn 已提交
1795 1796 1797 1798
static void atl1_update_rfd_index(struct atl1_adapter *adapter,
	struct rx_return_desc *rrd)
{
	u16 num_buf;
J
Jay Cliburn 已提交
1799

J
Jay Cliburn 已提交
1800 1801 1802 1803 1804 1805
	num_buf = (rrd->xsz.xsum_sz.pkt_size + adapter->rx_buffer_len - 1) /
		adapter->rx_buffer_len;
	if (rrd->num_buf == num_buf)
		/* clean alloc flag for bad rrd */
		atl1_clean_alloc_flag(adapter, rrd, num_buf);
}
J
Jay Cliburn 已提交
1806

J
Jay Cliburn 已提交
1807 1808 1809 1810
static void atl1_rx_checksum(struct atl1_adapter *adapter,
	struct rx_return_desc *rrd, struct sk_buff *skb)
{
	struct pci_dev *pdev = adapter->pdev;
J
Jay Cliburn 已提交
1811

J
Jay Cliburn 已提交
1812
	skb->ip_summed = CHECKSUM_NONE;
J
Jay Cliburn 已提交
1813

J
Jay Cliburn 已提交
1814 1815 1816 1817
	if (unlikely(rrd->pkt_flg & PACKET_FLAG_ERR)) {
		if (rrd->err_flg & (ERR_FLAG_CRC | ERR_FLAG_TRUNC |
					ERR_FLAG_CODE | ERR_FLAG_OV)) {
			adapter->hw_csum_err++;
J
Jay Cliburn 已提交
1818 1819 1820
			if (netif_msg_rx_err(adapter))
				dev_printk(KERN_DEBUG, &pdev->dev,
					"rx checksum error\n");
J
Jay Cliburn 已提交
1821
			return;
J
Jay Cliburn 已提交
1822 1823 1824
		}
	}

J
Jay Cliburn 已提交
1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
	/* not IPv4 */
	if (!(rrd->pkt_flg & PACKET_FLAG_IPV4))
		/* checksum is invalid, but it's not an IPv4 pkt, so ok */
		return;

	/* IPv4 packet */
	if (likely(!(rrd->err_flg &
		(ERR_FLAG_IP_CHKSUM | ERR_FLAG_L4_CHKSUM)))) {
		skb->ip_summed = CHECKSUM_UNNECESSARY;
		adapter->hw_csum_good++;
		return;
J
Jay Cliburn 已提交
1836 1837
	}

J
Jay Cliburn 已提交
1838
	/* IPv4, but hardware thinks its checksum is wrong */
J
Jay Cliburn 已提交
1839 1840 1841 1842
	if (netif_msg_rx_err(adapter))
		dev_printk(KERN_DEBUG, &pdev->dev,
			"hw csum wrong, pkt_flag:%x, err_flag:%x\n",
			rrd->pkt_flg, rrd->err_flg);
J
Jay Cliburn 已提交
1843 1844 1845 1846
	skb->ip_summed = CHECKSUM_COMPLETE;
	skb->csum = htons(rrd->xsz.xsum_sz.rx_chksum);
	adapter->hw_csum_err++;
	return;
J
Jay Cliburn 已提交
1847 1848
}

J
Jay Cliburn 已提交
1849 1850 1851 1852 1853
/*
 * atl1_alloc_rx_buffers - Replace used receive buffers
 * @adapter: address of board private structure
 */
static u16 atl1_alloc_rx_buffers(struct atl1_adapter *adapter)
J
Jay Cliburn 已提交
1854
{
J
Jay Cliburn 已提交
1855 1856 1857 1858 1859 1860 1861 1862 1863
	struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
	struct pci_dev *pdev = adapter->pdev;
	struct page *page;
	unsigned long offset;
	struct atl1_buffer *buffer_info, *next_info;
	struct sk_buff *skb;
	u16 num_alloc = 0;
	u16 rfd_next_to_use, next_next;
	struct rx_free_desc *rfd_desc;
J
Jay Cliburn 已提交
1864

J
Jay Cliburn 已提交
1865 1866 1867 1868 1869
	next_next = rfd_next_to_use = atomic_read(&rfd_ring->next_to_use);
	if (++next_next == rfd_ring->count)
		next_next = 0;
	buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
	next_info = &rfd_ring->buffer_info[next_next];
J
Jay Cliburn 已提交
1870

J
Jay Cliburn 已提交
1871 1872 1873 1874 1875
	while (!buffer_info->alloced && !next_info->alloced) {
		if (buffer_info->skb) {
			buffer_info->alloced = 1;
			goto next;
		}
J
Jay Cliburn 已提交
1876

J
Jay Cliburn 已提交
1877
		rfd_desc = ATL1_RFD_DESC(rfd_ring, rfd_next_to_use);
J
Jay Cliburn 已提交
1878

J
Jay Cliburn 已提交
1879
		skb = dev_alloc_skb(adapter->rx_buffer_len + NET_IP_ALIGN);
J
Jay Cliburn 已提交
1880 1881
		if (unlikely(!skb)) {
			/* Better luck next round */
J
Jay Cliburn 已提交
1882 1883 1884
			adapter->net_stats.rx_dropped++;
			break;
		}
J
Jay Cliburn 已提交
1885

J
Jay Cliburn 已提交
1886 1887 1888 1889 1890 1891
		/*
		 * Make buffer alignment 2 beyond a 16 byte boundary
		 * this will result in a 16 byte aligned IP header after
		 * the 14 byte MAC header is removed
		 */
		skb_reserve(skb, NET_IP_ALIGN);
J
Jay Cliburn 已提交
1892

J
Jay Cliburn 已提交
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
		buffer_info->alloced = 1;
		buffer_info->skb = skb;
		buffer_info->length = (u16) adapter->rx_buffer_len;
		page = virt_to_page(skb->data);
		offset = (unsigned long)skb->data & ~PAGE_MASK;
		buffer_info->dma = pci_map_page(pdev, page, offset,
						adapter->rx_buffer_len,
						PCI_DMA_FROMDEVICE);
		rfd_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
		rfd_desc->buf_len = cpu_to_le16(adapter->rx_buffer_len);
		rfd_desc->coalese = 0;
J
Jay Cliburn 已提交
1904

J
Jay Cliburn 已提交
1905 1906 1907 1908
next:
		rfd_next_to_use = next_next;
		if (unlikely(++next_next == rfd_ring->count))
			next_next = 0;
J
Jay Cliburn 已提交
1909

J
Jay Cliburn 已提交
1910 1911 1912 1913
		buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
		next_info = &rfd_ring->buffer_info[next_next];
		num_alloc++;
	}
J
Jay Cliburn 已提交
1914

J
Jay Cliburn 已提交
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925
	if (num_alloc) {
		/*
		 * Force memory writes to complete before letting h/w
		 * know there are new descriptors to fetch.  (Only
		 * applicable for weak-ordered memory model archs,
		 * such as IA-64).
		 */
		wmb();
		atomic_set(&rfd_ring->next_to_use, (int)rfd_next_to_use);
	}
	return num_alloc;
J
Jay Cliburn 已提交
1926 1927
}

J
Jay Cliburn 已提交
1928
static void atl1_intr_rx(struct atl1_adapter *adapter)
J
Jay Cliburn 已提交
1929
{
J
Jay Cliburn 已提交
1930 1931 1932
	int i, count;
	u16 length;
	u16 rrd_next_to_clean;
J
Jay Cliburn 已提交
1933
	u32 value;
J
Jay Cliburn 已提交
1934 1935 1936 1937 1938
	struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
	struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
	struct atl1_buffer *buffer_info;
	struct rx_return_desc *rrd;
	struct sk_buff *skb;
J
Jay Cliburn 已提交
1939

J
Jay Cliburn 已提交
1940
	count = 0;
J
Jay Cliburn 已提交
1941

J
Jay Cliburn 已提交
1942
	rrd_next_to_clean = atomic_read(&rrd_ring->next_to_clean);
J
Jay Cliburn 已提交
1943

J
Jay Cliburn 已提交
1944 1945 1946 1947 1948 1949 1950 1951
	while (1) {
		rrd = ATL1_RRD_DESC(rrd_ring, rrd_next_to_clean);
		i = 1;
		if (likely(rrd->xsz.valid)) {	/* packet valid */
chk_rrd:
			/* check rrd status */
			if (likely(rrd->num_buf == 1))
				goto rrd_ok;
J
Jay Cliburn 已提交
1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973
			else if (netif_msg_rx_err(adapter)) {
				dev_printk(KERN_DEBUG, &adapter->pdev->dev,
					"unexpected RRD buffer count\n");
				dev_printk(KERN_DEBUG, &adapter->pdev->dev,
					"rx_buf_len = %d\n",
					adapter->rx_buffer_len);
				dev_printk(KERN_DEBUG, &adapter->pdev->dev,
					"RRD num_buf = %d\n",
					rrd->num_buf);
				dev_printk(KERN_DEBUG, &adapter->pdev->dev,
					"RRD pkt_len = %d\n",
					rrd->xsz.xsum_sz.pkt_size);
				dev_printk(KERN_DEBUG, &adapter->pdev->dev,
					"RRD pkt_flg = 0x%08X\n",
					rrd->pkt_flg);
				dev_printk(KERN_DEBUG, &adapter->pdev->dev,
					"RRD err_flg = 0x%08X\n",
					rrd->err_flg);
				dev_printk(KERN_DEBUG, &adapter->pdev->dev,
					"RRD vlan_tag = 0x%08X\n",
					rrd->vlan_tag);
			}
J
Jay Cliburn 已提交
1974

J
Jay Cliburn 已提交
1975 1976 1977 1978 1979 1980 1981
			/* rrd seems to be bad */
			if (unlikely(i-- > 0)) {
				/* rrd may not be DMAed completely */
				udelay(1);
				goto chk_rrd;
			}
			/* bad rrd */
J
Jay Cliburn 已提交
1982 1983 1984
			if (netif_msg_rx_err(adapter))
				dev_printk(KERN_DEBUG, &adapter->pdev->dev,
					"bad RRD\n");
J
Jay Cliburn 已提交
1985 1986 1987
			/* see if update RFD index */
			if (rrd->num_buf > 1)
				atl1_update_rfd_index(adapter, rrd);
J
Jay Cliburn 已提交
1988

J
Jay Cliburn 已提交
1989 1990 1991 1992 1993 1994 1995
			/* update rrd */
			rrd->xsz.valid = 0;
			if (++rrd_next_to_clean == rrd_ring->count)
				rrd_next_to_clean = 0;
			count++;
			continue;
		} else {	/* current rrd still not be updated */
J
Jay Cliburn 已提交
1996

J
Jay Cliburn 已提交
1997 1998 1999 2000 2001
			break;
		}
rrd_ok:
		/* clean alloc flag for bad rrd */
		atl1_clean_alloc_flag(adapter, rrd, 0);
J
Jay Cliburn 已提交
2002

J
Jay Cliburn 已提交
2003 2004 2005
		buffer_info = &rfd_ring->buffer_info[rrd->buf_indx];
		if (++rfd_ring->next_to_clean == rfd_ring->count)
			rfd_ring->next_to_clean = 0;
J
Jay Cliburn 已提交
2006

J
Jay Cliburn 已提交
2007 2008 2009 2010
		/* update rrd next to clean */
		if (++rrd_next_to_clean == rrd_ring->count)
			rrd_next_to_clean = 0;
		count++;
J
Jay Cliburn 已提交
2011

J
Jay Cliburn 已提交
2012 2013 2014 2015 2016 2017 2018 2019 2020 2021
		if (unlikely(rrd->pkt_flg & PACKET_FLAG_ERR)) {
			if (!(rrd->err_flg &
				(ERR_FLAG_IP_CHKSUM | ERR_FLAG_L4_CHKSUM
				| ERR_FLAG_LEN))) {
				/* packet error, don't need upstream */
				buffer_info->alloced = 0;
				rrd->xsz.valid = 0;
				continue;
			}
		}
J
Jay Cliburn 已提交
2022

J
Jay Cliburn 已提交
2023 2024 2025 2026 2027
		/* Good Receive */
		pci_unmap_page(adapter->pdev, buffer_info->dma,
			       buffer_info->length, PCI_DMA_FROMDEVICE);
		skb = buffer_info->skb;
		length = le16_to_cpu(rrd->xsz.xsum_sz.pkt_size);
J
Jay Cliburn 已提交
2028

2029
		skb_put(skb, length - ETH_FCS_LEN);
J
Jay Cliburn 已提交
2030

J
Jay Cliburn 已提交
2031 2032 2033
		/* Receive Checksum Offload */
		atl1_rx_checksum(adapter, rrd, skb);
		skb->protocol = eth_type_trans(skb, adapter->netdev);
J
Jay Cliburn 已提交
2034

J
Jay Cliburn 已提交
2035 2036 2037 2038 2039 2040 2041
		if (adapter->vlgrp && (rrd->pkt_flg & PACKET_FLAG_VLAN_INS)) {
			u16 vlan_tag = (rrd->vlan_tag >> 4) |
					((rrd->vlan_tag & 7) << 13) |
					((rrd->vlan_tag & 8) << 9);
			vlan_hwaccel_rx(skb, adapter->vlgrp, vlan_tag);
		} else
			netif_rx(skb);
J
Jay Cliburn 已提交
2042

J
Jay Cliburn 已提交
2043 2044 2045 2046
		/* let protocol layer free skb */
		buffer_info->skb = NULL;
		buffer_info->alloced = 0;
		rrd->xsz.valid = 0;
J
Jay Cliburn 已提交
2047

J
Jay Cliburn 已提交
2048 2049
		adapter->netdev->last_rx = jiffies;
	}
J
Jay Cliburn 已提交
2050

J
Jay Cliburn 已提交
2051
	atomic_set(&rrd_ring->next_to_clean, rrd_next_to_clean);
J
Jay Cliburn 已提交
2052

J
Jay Cliburn 已提交
2053
	atl1_alloc_rx_buffers(adapter);
J
Jay Cliburn 已提交
2054

J
Jay Cliburn 已提交
2055 2056 2057 2058
	/* update mailbox ? */
	if (count) {
		u32 tpd_next_to_use;
		u32 rfd_next_to_use;
J
Jay Cliburn 已提交
2059

J
Jay Cliburn 已提交
2060
		spin_lock(&adapter->mb_lock);
J
Jay Cliburn 已提交
2061

J
Jay Cliburn 已提交
2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
		tpd_next_to_use = atomic_read(&adapter->tpd_ring.next_to_use);
		rfd_next_to_use =
		    atomic_read(&adapter->rfd_ring.next_to_use);
		rrd_next_to_clean =
		    atomic_read(&adapter->rrd_ring.next_to_clean);
		value = ((rfd_next_to_use & MB_RFD_PROD_INDX_MASK) <<
			MB_RFD_PROD_INDX_SHIFT) |
                        ((rrd_next_to_clean & MB_RRD_CONS_INDX_MASK) <<
			MB_RRD_CONS_INDX_SHIFT) |
                        ((tpd_next_to_use & MB_TPD_PROD_INDX_MASK) <<
			MB_TPD_PROD_INDX_SHIFT);
		iowrite32(value, adapter->hw.hw_addr + REG_MAILBOX);
		spin_unlock(&adapter->mb_lock);
	}
J
Jay Cliburn 已提交
2076 2077
}

J
Jay Cliburn 已提交
2078
static void atl1_intr_tx(struct atl1_adapter *adapter)
J
Jay Cliburn 已提交
2079
{
J
Jay Cliburn 已提交
2080 2081 2082 2083
	struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
	struct atl1_buffer *buffer_info;
	u16 sw_tpd_next_to_clean;
	u16 cmb_tpd_next_to_clean;
J
Jay Cliburn 已提交
2084

J
Jay Cliburn 已提交
2085 2086
	sw_tpd_next_to_clean = atomic_read(&tpd_ring->next_to_clean);
	cmb_tpd_next_to_clean = le16_to_cpu(adapter->cmb.cmb->tpd_cons_idx);
J
Jay Cliburn 已提交
2087

J
Jay Cliburn 已提交
2088 2089
	while (cmb_tpd_next_to_clean != sw_tpd_next_to_clean) {
		struct tx_packet_desc *tpd;
J
Jay Cliburn 已提交
2090

J
Jay Cliburn 已提交
2091 2092 2093 2094 2095 2096 2097
		tpd = ATL1_TPD_DESC(tpd_ring, sw_tpd_next_to_clean);
		buffer_info = &tpd_ring->buffer_info[sw_tpd_next_to_clean];
		if (buffer_info->dma) {
			pci_unmap_page(adapter->pdev, buffer_info->dma,
				       buffer_info->length, PCI_DMA_TODEVICE);
			buffer_info->dma = 0;
		}
J
Jay Cliburn 已提交
2098

J
Jay Cliburn 已提交
2099 2100 2101 2102
		if (buffer_info->skb) {
			dev_kfree_skb_irq(buffer_info->skb);
			buffer_info->skb = NULL;
		}
J
Jay Cliburn 已提交
2103

J
Jay Cliburn 已提交
2104 2105 2106 2107 2108 2109 2110 2111
		if (++sw_tpd_next_to_clean == tpd_ring->count)
			sw_tpd_next_to_clean = 0;
	}
	atomic_set(&tpd_ring->next_to_clean, sw_tpd_next_to_clean);

	if (netif_queue_stopped(adapter->netdev)
	    && netif_carrier_ok(adapter->netdev))
		netif_wake_queue(adapter->netdev);
J
Jay Cliburn 已提交
2112 2113
}

2114
static u16 atl1_tpd_avail(struct atl1_tpd_ring *tpd_ring)
J
Jay Cliburn 已提交
2115 2116 2117
{
	u16 next_to_clean = atomic_read(&tpd_ring->next_to_clean);
	u16 next_to_use = atomic_read(&tpd_ring->next_to_use);
J
Jay Cliburn 已提交
2118 2119 2120
	return ((next_to_clean > next_to_use) ?
		next_to_clean - next_to_use - 1 :
		tpd_ring->count + next_to_clean - next_to_use - 1);
J
Jay Cliburn 已提交
2121 2122 2123
}

static int atl1_tso(struct atl1_adapter *adapter, struct sk_buff *skb,
J
Jay Cliburn 已提交
2124
	struct tx_packet_desc *ptpd)
J
Jay Cliburn 已提交
2125
{
J
Jay Cliburn 已提交
2126 2127 2128
	/* spinlock held */
	u8 hdr_len, ip_off;
	u32 real_len;
J
Jay Cliburn 已提交
2129 2130 2131 2132 2133 2134
	int err;

	if (skb_shinfo(skb)->gso_size) {
		if (skb_header_cloned(skb)) {
			err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
			if (unlikely(err))
J
Jay Cliburn 已提交
2135
				return -1;
J
Jay Cliburn 已提交
2136 2137 2138
		}

		if (skb->protocol == ntohs(ETH_P_IP)) {
2139 2140
			struct iphdr *iph = ip_hdr(skb);

J
Jay Cliburn 已提交
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161
			real_len = (((unsigned char *)iph - skb->data) +
				ntohs(iph->tot_len));
			if (real_len < skb->len)
				pskb_trim(skb, real_len);
			hdr_len = (skb_transport_offset(skb) + tcp_hdrlen(skb));
			if (skb->len == hdr_len) {
				iph->check = 0;
				tcp_hdr(skb)->check =
					~csum_tcpudp_magic(iph->saddr,
					iph->daddr, tcp_hdrlen(skb),
					IPPROTO_TCP, 0);
				ptpd->word3 |= (iph->ihl & TPD_IPHL_MASK) <<
					TPD_IPHL_SHIFT;
				ptpd->word3 |= ((tcp_hdrlen(skb) >> 2) &
					TPD_TCPHDRLEN_MASK) <<
					TPD_TCPHDRLEN_SHIFT;
				ptpd->word3 |= 1 << TPD_IP_CSUM_SHIFT;
				ptpd->word3 |= 1 << TPD_TCP_CSUM_SHIFT;
				return 1;
			}

2162
			iph->check = 0;
2163
			tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
J
Jay Cliburn 已提交
2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179
					iph->daddr, 0, IPPROTO_TCP, 0);
			ip_off = (unsigned char *)iph -
				(unsigned char *) skb_network_header(skb);
			if (ip_off == 8) /* 802.3-SNAP frame */
				ptpd->word3 |= 1 << TPD_ETHTYPE_SHIFT;
			else if (ip_off != 0)
				return -2;

			ptpd->word3 |= (iph->ihl & TPD_IPHL_MASK) <<
				TPD_IPHL_SHIFT;
			ptpd->word3 |= ((tcp_hdrlen(skb) >> 2) &
				TPD_TCPHDRLEN_MASK) << TPD_TCPHDRLEN_SHIFT;
			ptpd->word3 |= (skb_shinfo(skb)->gso_size &
				TPD_MSS_MASK) << TPD_MSS_SHIFT;
			ptpd->word3 |= 1 << TPD_SEGMENT_EN_SHIFT;
			return 3;
J
Jay Cliburn 已提交
2180 2181 2182 2183 2184 2185
		}
	}
	return false;
}

static int atl1_tx_csum(struct atl1_adapter *adapter, struct sk_buff *skb,
J
Jay Cliburn 已提交
2186
	struct tx_packet_desc *ptpd)
J
Jay Cliburn 已提交
2187 2188 2189 2190
{
	u8 css, cso;

	if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
J
Jay Cliburn 已提交
2191 2192 2193 2194 2195 2196 2197
		css = (u8) (skb->csum_start - skb_headroom(skb));
		cso = css + (u8) skb->csum_offset;
		if (unlikely(css & 0x1)) {
			/* L1 hardware requires an even number here */
			if (netif_msg_tx_err(adapter))
				dev_printk(KERN_DEBUG, &adapter->pdev->dev,
					"payload offset not an even number\n");
J
Jay Cliburn 已提交
2198 2199
			return -1;
		}
J
Jay Cliburn 已提交
2200 2201 2202 2203 2204
		ptpd->word3 |= (css & TPD_PLOADOFFSET_MASK) <<
			TPD_PLOADOFFSET_SHIFT;
		ptpd->word3 |= (cso & TPD_CCSUMOFFSET_MASK) <<
			TPD_CCSUMOFFSET_SHIFT;
		ptpd->word3 |= 1 << TPD_CUST_CSUM_EN_SHIFT;
J
Jay Cliburn 已提交
2205 2206
		return true;
	}
J
Jay Cliburn 已提交
2207
	return 0;
J
Jay Cliburn 已提交
2208 2209
}

J
Jay Cliburn 已提交
2210
static void atl1_tx_map(struct atl1_adapter *adapter, struct sk_buff *skb,
J
Jay Cliburn 已提交
2211
	struct tx_packet_desc *ptpd)
J
Jay Cliburn 已提交
2212
{
J
Jay Cliburn 已提交
2213
	/* spinlock held */
J
Jay Cliburn 已提交
2214 2215
	struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
	struct atl1_buffer *buffer_info;
J
Jay Cliburn 已提交
2216
	u16 buf_len = skb->len;
J
Jay Cliburn 已提交
2217 2218 2219 2220
	struct page *page;
	unsigned long offset;
	unsigned int nr_frags;
	unsigned int f;
J
Jay Cliburn 已提交
2221 2222 2223 2224
	int retval;
	u16 next_to_use;
	u16 data_len;
	u8 hdr_len;
J
Jay Cliburn 已提交
2225

J
Jay Cliburn 已提交
2226
	buf_len -= skb->data_len;
J
Jay Cliburn 已提交
2227
	nr_frags = skb_shinfo(skb)->nr_frags;
J
Jay Cliburn 已提交
2228 2229
	next_to_use = atomic_read(&tpd_ring->next_to_use);
	buffer_info = &tpd_ring->buffer_info[next_to_use];
J
Jay Cliburn 已提交
2230 2231
	if (unlikely(buffer_info->skb))
		BUG();
J
Jay Cliburn 已提交
2232 2233
	/* put skb in last TPD */
	buffer_info->skb = NULL;
J
Jay Cliburn 已提交
2234

J
Jay Cliburn 已提交
2235 2236 2237 2238 2239
	retval = (ptpd->word3 >> TPD_SEGMENT_EN_SHIFT) & TPD_SEGMENT_EN_MASK;
	if (retval) {
		/* TSO */
		hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
		buffer_info->length = hdr_len;
J
Jay Cliburn 已提交
2240 2241 2242
		page = virt_to_page(skb->data);
		offset = (unsigned long)skb->data & ~PAGE_MASK;
		buffer_info->dma = pci_map_page(adapter->pdev, page,
J
Jay Cliburn 已提交
2243
						offset, hdr_len,
J
Jay Cliburn 已提交
2244 2245
						PCI_DMA_TODEVICE);

J
Jay Cliburn 已提交
2246 2247
		if (++next_to_use == tpd_ring->count)
			next_to_use = 0;
J
Jay Cliburn 已提交
2248

J
Jay Cliburn 已提交
2249 2250
		if (buf_len > hdr_len) {
			int i, nseg;
2251

J
Jay Cliburn 已提交
2252 2253
			data_len = buf_len - hdr_len;
			nseg = (data_len + ATL1_MAX_TX_BUF_LEN - 1) /
J
Jay Cliburn 已提交
2254
				ATL1_MAX_TX_BUF_LEN;
J
Jay Cliburn 已提交
2255
			for (i = 0; i < nseg; i++) {
J
Jay Cliburn 已提交
2256
				buffer_info =
J
Jay Cliburn 已提交
2257
				    &tpd_ring->buffer_info[next_to_use];
J
Jay Cliburn 已提交
2258 2259
				buffer_info->skb = NULL;
				buffer_info->length =
J
Jay Cliburn 已提交
2260
				    (ATL1_MAX_TX_BUF_LEN >=
J
Jay Cliburn 已提交
2261 2262
				     data_len) ? ATL1_MAX_TX_BUF_LEN : data_len;
				data_len -= buffer_info->length;
J
Jay Cliburn 已提交
2263
				page = virt_to_page(skb->data +
J
Jay Cliburn 已提交
2264
					(hdr_len + i * ATL1_MAX_TX_BUF_LEN));
J
Jay Cliburn 已提交
2265
				offset = (unsigned long)(skb->data +
J
Jay Cliburn 已提交
2266 2267
					(hdr_len + i * ATL1_MAX_TX_BUF_LEN)) &
					~PAGE_MASK;
J
Jay Cliburn 已提交
2268 2269 2270
				buffer_info->dma = pci_map_page(adapter->pdev,
					page, offset, buffer_info->length,
					PCI_DMA_TODEVICE);
J
Jay Cliburn 已提交
2271 2272
				if (++next_to_use == tpd_ring->count)
					next_to_use = 0;
J
Jay Cliburn 已提交
2273 2274 2275
			}
		}
	} else {
J
Jay Cliburn 已提交
2276 2277
		/* not TSO */
		buffer_info->length = buf_len;
J
Jay Cliburn 已提交
2278 2279 2280
		page = virt_to_page(skb->data);
		offset = (unsigned long)skb->data & ~PAGE_MASK;
		buffer_info->dma = pci_map_page(adapter->pdev, page,
J
Jay Cliburn 已提交
2281 2282 2283
			offset, buf_len, PCI_DMA_TODEVICE);
		if (++next_to_use == tpd_ring->count)
			next_to_use = 0;
J
Jay Cliburn 已提交
2284 2285 2286 2287
	}

	for (f = 0; f < nr_frags; f++) {
		struct skb_frag_struct *frag;
J
Jay Cliburn 已提交
2288
		u16 i, nseg;
J
Jay Cliburn 已提交
2289 2290

		frag = &skb_shinfo(skb)->frags[f];
J
Jay Cliburn 已提交
2291
		buf_len = frag->size;
J
Jay Cliburn 已提交
2292

J
Jay Cliburn 已提交
2293 2294 2295 2296
		nseg = (buf_len + ATL1_MAX_TX_BUF_LEN - 1) /
			ATL1_MAX_TX_BUF_LEN;
		for (i = 0; i < nseg; i++) {
			buffer_info = &tpd_ring->buffer_info[next_to_use];
J
Jay Cliburn 已提交
2297 2298 2299
			if (unlikely(buffer_info->skb))
				BUG();
			buffer_info->skb = NULL;
J
Jay Cliburn 已提交
2300 2301 2302
			buffer_info->length = (buf_len > ATL1_MAX_TX_BUF_LEN) ?
				ATL1_MAX_TX_BUF_LEN : buf_len;
			buf_len -= buffer_info->length;
J
Jay Cliburn 已提交
2303 2304 2305 2306
			buffer_info->dma = pci_map_page(adapter->pdev,
				frag->page,
				frag->page_offset + (i * ATL1_MAX_TX_BUF_LEN),
				buffer_info->length, PCI_DMA_TODEVICE);
J
Jay Cliburn 已提交
2307

J
Jay Cliburn 已提交
2308 2309
			if (++next_to_use == tpd_ring->count)
				next_to_use = 0;
J
Jay Cliburn 已提交
2310 2311 2312 2313 2314 2315 2316
		}
	}

	/* last tpd's buffer-info */
	buffer_info->skb = skb;
}

J
Jay Cliburn 已提交
2317 2318
static void atl1_tx_queue(struct atl1_adapter *adapter, u16 count,
       struct tx_packet_desc *ptpd)
J
Jay Cliburn 已提交
2319
{
J
Jay Cliburn 已提交
2320
	/* spinlock held */
J
Jay Cliburn 已提交
2321 2322 2323
	struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
	struct atl1_buffer *buffer_info;
	struct tx_packet_desc *tpd;
J
Jay Cliburn 已提交
2324 2325 2326
	u16 j;
	u32 val;
	u16 next_to_use = (u16) atomic_read(&tpd_ring->next_to_use);
J
Jay Cliburn 已提交
2327 2328

	for (j = 0; j < count; j++) {
J
Jay Cliburn 已提交
2329 2330 2331 2332
		buffer_info = &tpd_ring->buffer_info[next_to_use];
		tpd = ATL1_TPD_DESC(&adapter->tpd_ring, next_to_use);
		if (tpd != ptpd)
			memcpy(tpd, ptpd, sizeof(struct tx_packet_desc));
J
Jay Cliburn 已提交
2333
		tpd->buffer_addr = cpu_to_le64(buffer_info->dma);
J
Jay Cliburn 已提交
2334 2335
		tpd->word2 = (cpu_to_le16(buffer_info->length) &
			TPD_BUFLEN_MASK) << TPD_BUFLEN_SHIFT;
J
Jay Cliburn 已提交
2336

J
Jay Cliburn 已提交
2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348
		/*
		 * if this is the first packet in a TSO chain, set
		 * TPD_HDRFLAG, otherwise, clear it.
		 */
		val = (tpd->word3 >> TPD_SEGMENT_EN_SHIFT) &
			TPD_SEGMENT_EN_MASK;
		if (val) {
			if (!j)
				tpd->word3 |= 1 << TPD_HDRFLAG_SHIFT;
			else
				tpd->word3 &= ~(1 << TPD_HDRFLAG_SHIFT);
		}
J
Jay Cliburn 已提交
2349 2350

		if (j == (count - 1))
J
Jay Cliburn 已提交
2351
			tpd->word3 |= 1 << TPD_EOP_SHIFT;
J
Jay Cliburn 已提交
2352

J
Jay Cliburn 已提交
2353 2354
		if (++next_to_use == tpd_ring->count)
			next_to_use = 0;
J
Jay Cliburn 已提交
2355 2356 2357 2358 2359 2360 2361 2362 2363
	}
	/*
	 * Force memory writes to complete before letting h/w
	 * know there are new descriptors to fetch.  (Only
	 * applicable for weak-ordered memory model archs,
	 * such as IA-64).
	 */
	wmb();

J
Jay Cliburn 已提交
2364
	atomic_set(&tpd_ring->next_to_use, next_to_use);
J
Jay Cliburn 已提交
2365 2366 2367 2368 2369
}

static int atl1_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
{
	struct atl1_adapter *adapter = netdev_priv(netdev);
J
Jay Cliburn 已提交
2370
	struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
J
Jay Cliburn 已提交
2371 2372 2373 2374
	int len = skb->len;
	int tso;
	int count = 1;
	int ret_val;
J
Jay Cliburn 已提交
2375
	struct tx_packet_desc *ptpd;
J
Jay Cliburn 已提交
2376 2377 2378 2379 2380 2381 2382 2383 2384 2385
	u16 frag_size;
	u16 vlan_tag;
	unsigned long flags;
	unsigned int nr_frags = 0;
	unsigned int mss = 0;
	unsigned int f;
	unsigned int proto_hdr_len;

	len -= skb->data_len;

J
Jay Cliburn 已提交
2386
	if (unlikely(skb->len <= 0)) {
J
Jay Cliburn 已提交
2387 2388 2389 2390 2391 2392 2393 2394
		dev_kfree_skb_any(skb);
		return NETDEV_TX_OK;
	}

	nr_frags = skb_shinfo(skb)->nr_frags;
	for (f = 0; f < nr_frags; f++) {
		frag_size = skb_shinfo(skb)->frags[f].size;
		if (frag_size)
J
Jay Cliburn 已提交
2395 2396
			count += (frag_size + ATL1_MAX_TX_BUF_LEN - 1) /
				ATL1_MAX_TX_BUF_LEN;
J
Jay Cliburn 已提交
2397 2398 2399 2400
	}

	mss = skb_shinfo(skb)->gso_size;
	if (mss) {
J
Jay Cliburn 已提交
2401
		if (skb->protocol == ntohs(ETH_P_IP)) {
2402
			proto_hdr_len = (skb_transport_offset(skb) +
2403
					 tcp_hdrlen(skb));
J
Jay Cliburn 已提交
2404 2405 2406 2407 2408 2409 2410
			if (unlikely(proto_hdr_len > len)) {
				dev_kfree_skb_any(skb);
				return NETDEV_TX_OK;
			}
			/* need additional TPD ? */
			if (proto_hdr_len != len)
				count += (len - proto_hdr_len +
J
Jay Cliburn 已提交
2411 2412
					ATL1_MAX_TX_BUF_LEN - 1) /
					ATL1_MAX_TX_BUF_LEN;
J
Jay Cliburn 已提交
2413 2414 2415
		}
	}

I
Ingo Molnar 已提交
2416
	if (!spin_trylock_irqsave(&adapter->lock, flags)) {
J
Jay Cliburn 已提交
2417
		/* Can't get lock - tell upper layer to requeue */
J
Jay Cliburn 已提交
2418 2419 2420
		if (netif_msg_tx_queued(adapter))
			dev_printk(KERN_DEBUG, &adapter->pdev->dev,
				"tx locked\n");
J
Jay Cliburn 已提交
2421 2422 2423
		return NETDEV_TX_LOCKED;
	}

2424
	if (atl1_tpd_avail(&adapter->tpd_ring) < count) {
J
Jay Cliburn 已提交
2425 2426 2427
		/* not enough descriptors */
		netif_stop_queue(netdev);
		spin_unlock_irqrestore(&adapter->lock, flags);
J
Jay Cliburn 已提交
2428 2429 2430
		if (netif_msg_tx_queued(adapter))
			dev_printk(KERN_DEBUG, &adapter->pdev->dev,
				"tx busy\n");
J
Jay Cliburn 已提交
2431 2432 2433
		return NETDEV_TX_BUSY;
	}

J
Jay Cliburn 已提交
2434 2435 2436
	ptpd = ATL1_TPD_DESC(tpd_ring,
		(u16) atomic_read(&tpd_ring->next_to_use));
	memset(ptpd, 0, sizeof(struct tx_packet_desc));
J
Jay Cliburn 已提交
2437 2438 2439 2440 2441

	if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
		vlan_tag = vlan_tx_tag_get(skb);
		vlan_tag = (vlan_tag << 4) | (vlan_tag >> 13) |
			((vlan_tag >> 9) & 0x8);
J
Jay Cliburn 已提交
2442 2443 2444
		ptpd->word3 |= 1 << TPD_INS_VL_TAG_SHIFT;
		ptpd->word3 |= (vlan_tag & TPD_VL_TAGGED_MASK) <<
			TPD_VL_TAGGED_SHIFT;
J
Jay Cliburn 已提交
2445 2446
	}

J
Jay Cliburn 已提交
2447
	tso = atl1_tso(adapter, skb, ptpd);
J
Jay Cliburn 已提交
2448 2449 2450 2451 2452 2453 2454
	if (tso < 0) {
		spin_unlock_irqrestore(&adapter->lock, flags);
		dev_kfree_skb_any(skb);
		return NETDEV_TX_OK;
	}

	if (!tso) {
J
Jay Cliburn 已提交
2455
		ret_val = atl1_tx_csum(adapter, skb, ptpd);
J
Jay Cliburn 已提交
2456 2457 2458 2459 2460 2461 2462
		if (ret_val < 0) {
			spin_unlock_irqrestore(&adapter->lock, flags);
			dev_kfree_skb_any(skb);
			return NETDEV_TX_OK;
		}
	}

J
Jay Cliburn 已提交
2463 2464
	atl1_tx_map(adapter, skb, ptpd);
	atl1_tx_queue(adapter, count, ptpd);
J
Jay Cliburn 已提交
2465
	atl1_update_mailbox(adapter);
J
Jay Cliburn 已提交
2466 2467
	spin_unlock_irqrestore(&adapter->lock, flags);
	netdev->trans_start = jiffies;
J
Jay Cliburn 已提交
2468 2469 2470 2471
	return NETDEV_TX_OK;
}

/*
J
Jay Cliburn 已提交
2472 2473 2474 2475
 * atl1_intr - Interrupt Handler
 * @irq: interrupt number
 * @data: pointer to a network interface device structure
 * @pt_regs: CPU registers structure
J
Jay Cliburn 已提交
2476
 */
J
Jay Cliburn 已提交
2477
static irqreturn_t atl1_intr(int irq, void *data)
J
Jay Cliburn 已提交
2478
{
J
Jay Cliburn 已提交
2479 2480 2481
	struct atl1_adapter *adapter = netdev_priv(data);
	u32 status;
	int max_ints = 10;
J
Jay Cliburn 已提交
2482

J
Jay Cliburn 已提交
2483 2484 2485
	status = adapter->cmb.cmb->int_stats;
	if (!status)
		return IRQ_NONE;
J
Jay Cliburn 已提交
2486

J
Jay Cliburn 已提交
2487 2488 2489 2490 2491
	do {
		/* clear CMB interrupt status at once */
		adapter->cmb.cmb->int_stats = 0;

		if (status & ISR_GPHY)	/* clear phy status */
J
Jay Cliburn 已提交
2492
			atlx_clear_phy_int(adapter);
J
Jay Cliburn 已提交
2493 2494 2495 2496 2497 2498 2499 2500 2501 2502

		/* clear ISR status, and Enable CMB DMA/Disable Interrupt */
		iowrite32(status | ISR_DIS_INT, adapter->hw.hw_addr + REG_ISR);

		/* check if SMB intr */
		if (status & ISR_SMB)
			atl1_inc_smb(adapter);

		/* check if PCIE PHY Link down */
		if (status & ISR_PHY_LINKDOWN) {
J
Jay Cliburn 已提交
2503 2504 2505
			if (netif_msg_intr(adapter))
				dev_printk(KERN_DEBUG, &adapter->pdev->dev,
					"pcie phy link down %x\n", status);
J
Jay Cliburn 已提交
2506 2507 2508 2509 2510
			if (netif_running(adapter->netdev)) {	/* reset MAC */
				iowrite32(0, adapter->hw.hw_addr + REG_IMR);
				schedule_work(&adapter->pcie_dma_to_rst_task);
				return IRQ_HANDLED;
			}
J
Jay Cliburn 已提交
2511
		}
J
Jay Cliburn 已提交
2512 2513 2514

		/* check if DMA read/write error ? */
		if (status & (ISR_DMAR_TO_RST | ISR_DMAW_TO_RST)) {
J
Jay Cliburn 已提交
2515 2516 2517 2518
			if (netif_msg_intr(adapter))
				dev_printk(KERN_DEBUG, &adapter->pdev->dev,
					"pcie DMA r/w error (status = 0x%x)\n",
					status);
J
Jay Cliburn 已提交
2519 2520 2521
			iowrite32(0, adapter->hw.hw_addr + REG_IMR);
			schedule_work(&adapter->pcie_dma_to_rst_task);
			return IRQ_HANDLED;
J
Jay Cliburn 已提交
2522 2523
		}

J
Jay Cliburn 已提交
2524 2525 2526 2527 2528
		/* link event */
		if (status & ISR_GPHY) {
			adapter->soft_stats.tx_carrier_errors++;
			atl1_check_for_link(adapter);
		}
J
Jay Cliburn 已提交
2529

J
Jay Cliburn 已提交
2530 2531 2532
		/* transmit event */
		if (status & ISR_CMB_TX)
			atl1_intr_tx(adapter);
J
Jay Cliburn 已提交
2533

J
Jay Cliburn 已提交
2534 2535 2536 2537 2538 2539 2540
		/* rx exception */
		if (unlikely(status & (ISR_RXF_OV | ISR_RFD_UNRUN |
			ISR_RRD_OV | ISR_HOST_RFD_UNRUN |
			ISR_HOST_RRD_OV | ISR_CMB_RX))) {
			if (status & (ISR_RXF_OV | ISR_RFD_UNRUN |
				ISR_RRD_OV | ISR_HOST_RFD_UNRUN |
				ISR_HOST_RRD_OV))
J
Jay Cliburn 已提交
2541 2542 2543 2544 2545
				if (netif_msg_intr(adapter))
					dev_printk(KERN_DEBUG,
						&adapter->pdev->dev,
						"rx exception, ISR = 0x%x\n",
						status);
J
Jay Cliburn 已提交
2546 2547
			atl1_intr_rx(adapter);
		}
J
Jay Cliburn 已提交
2548

J
Jay Cliburn 已提交
2549 2550 2551 2552 2553 2554 2555 2556
		if (--max_ints < 0)
			break;

	} while ((status = adapter->cmb.cmb->int_stats));

	/* re-enable Interrupt */
	iowrite32(ISR_DIS_SMB | ISR_DIS_DMA, adapter->hw.hw_addr + REG_ISR);
	return IRQ_HANDLED;
J
Jay Cliburn 已提交
2557 2558 2559
}

/*
J
Jay Cliburn 已提交
2560 2561
 * atl1_watchdog - Timer Call-back
 * @data: pointer to netdev cast into an unsigned long
J
Jay Cliburn 已提交
2562
 */
J
Jay Cliburn 已提交
2563
static void atl1_watchdog(unsigned long data)
J
Jay Cliburn 已提交
2564
{
J
Jay Cliburn 已提交
2565
	struct atl1_adapter *adapter = (struct atl1_adapter *)data;
J
Jay Cliburn 已提交
2566

J
Jay Cliburn 已提交
2567 2568 2569
	/* Reset the timer */
	mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
}
J
Jay Cliburn 已提交
2570

J
Jay Cliburn 已提交
2571 2572 2573 2574 2575 2576
/*
 * atl1_phy_config - Timer Call-back
 * @data: pointer to netdev cast into an unsigned long
 */
static void atl1_phy_config(unsigned long data)
{
J
Jay Cliburn 已提交
2577 2578
	struct atl1_adapter *adapter = (struct atl1_adapter *)data;
	struct atl1_hw *hw = &adapter->hw;
J
Jay Cliburn 已提交
2579
	unsigned long flags;
J
Jay Cliburn 已提交
2580

J
Jay Cliburn 已提交
2581
	spin_lock_irqsave(&adapter->lock, flags);
J
Jay Cliburn 已提交
2582 2583 2584 2585
	adapter->phy_timer_pending = false;
	atl1_write_phy_reg(hw, MII_ADVERTISE, hw->mii_autoneg_adv_reg);
	atl1_write_phy_reg(hw, MII_ATLX_CR, hw->mii_1000t_ctrl_reg);
	atl1_write_phy_reg(hw, MII_BMCR, MII_CR_RESET | MII_CR_AUTO_NEG_EN);
J
Jay Cliburn 已提交
2586 2587 2588
	spin_unlock_irqrestore(&adapter->lock, flags);
}

J
Jay Cliburn 已提交
2589 2590 2591 2592 2593 2594 2595 2596 2597 2598
/*
 * Orphaned vendor comment left intact here:
 * <vendor comment>
 * If TPD Buffer size equal to 0, PCIE DMAR_TO_INT
 * will assert. We do soft reset <0x1400=1> according
 * with the SPEC. BUT, it seemes that PCIE or DMA
 * state-machine will not be reset. DMAR_TO_INT will
 * assert again and again.
 * </vendor comment>
 */
J
Jay Cliburn 已提交
2599

J
Jay Cliburn 已提交
2600
static int atl1_reset(struct atl1_adapter *adapter)
J
Jay Cliburn 已提交
2601 2602 2603
{
	int ret;
	ret = atl1_reset_hw(&adapter->hw);
J
Jay Cliburn 已提交
2604
	if (ret)
J
Jay Cliburn 已提交
2605 2606
		return ret;
	return atl1_init_hw(&adapter->hw);
J
Jay Cliburn 已提交
2607 2608
}

J
Jay Cliburn 已提交
2609
static s32 atl1_up(struct atl1_adapter *adapter)
J
Jay Cliburn 已提交
2610 2611 2612 2613 2614 2615
{
	struct net_device *netdev = adapter->netdev;
	int err;
	int irq_flags = IRQF_SAMPLE_RANDOM;

	/* hardware has been reset, we need to reload some things */
J
Jay Cliburn 已提交
2616
	atlx_set_multi(netdev);
2617
	atl1_init_ring_ptrs(adapter);
J
Jay Cliburn 已提交
2618
	atlx_restore_vlan(adapter);
J
Jay Cliburn 已提交
2619
	err = atl1_alloc_rx_buffers(adapter);
J
Jay Cliburn 已提交
2620 2621
	if (unlikely(!err))
		/* no RX BUFFER allocated */
J
Jay Cliburn 已提交
2622 2623 2624 2625 2626 2627 2628 2629 2630
		return -ENOMEM;

	if (unlikely(atl1_configure(adapter))) {
		err = -EIO;
		goto err_up;
	}

	err = pci_enable_msi(adapter->pdev);
	if (err) {
J
Jay Cliburn 已提交
2631 2632 2633
		if (netif_msg_ifup(adapter))
			dev_info(&adapter->pdev->dev,
				"Unable to enable MSI: %d\n", err);
J
Jay Cliburn 已提交
2634 2635 2636 2637 2638 2639 2640 2641 2642
		irq_flags |= IRQF_SHARED;
	}

	err = request_irq(adapter->pdev->irq, &atl1_intr, irq_flags,
			netdev->name, netdev);
	if (unlikely(err))
		goto err_up;

	mod_timer(&adapter->watchdog_timer, jiffies);
J
Jay Cliburn 已提交
2643
	atlx_irq_enable(adapter);
J
Jay Cliburn 已提交
2644 2645 2646 2647 2648 2649 2650 2651 2652 2653
	atl1_check_link(adapter);
	return 0;

err_up:
	pci_disable_msi(adapter->pdev);
	/* free rx_buffers */
	atl1_clean_rx_ring(adapter);
	return err;
}

J
Jay Cliburn 已提交
2654
static void atl1_down(struct atl1_adapter *adapter)
J
Jay Cliburn 已提交
2655 2656 2657 2658 2659 2660 2661
{
	struct net_device *netdev = adapter->netdev;

	del_timer_sync(&adapter->watchdog_timer);
	del_timer_sync(&adapter->phy_config_timer);
	adapter->phy_timer_pending = false;

J
Jay Cliburn 已提交
2662
	atlx_irq_disable(adapter);
J
Jay Cliburn 已提交
2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676
	free_irq(adapter->pdev->irq, netdev);
	pci_disable_msi(adapter->pdev);
	atl1_reset_hw(&adapter->hw);
	adapter->cmb.cmb->int_stats = 0;

	adapter->link_speed = SPEED_0;
	adapter->link_duplex = -1;
	netif_carrier_off(netdev);
	netif_stop_queue(netdev);

	atl1_clean_tx_ring(adapter);
	atl1_clean_rx_ring(adapter);
}

J
Jay Cliburn 已提交
2677 2678 2679 2680 2681 2682 2683 2684
static void atl1_tx_timeout_task(struct work_struct *work)
{
	struct atl1_adapter *adapter =
		container_of(work, struct atl1_adapter, tx_timeout_task);
	struct net_device *netdev = adapter->netdev;

	netif_device_detach(netdev);
	atl1_down(adapter);
2685
	atl1_up(adapter);
J
Jay Cliburn 已提交
2686
	netif_device_attach(netdev);
2687 2688
}

J
Jay Cliburn 已提交
2689 2690 2691 2692 2693 2694 2695 2696
/*
 * atl1_change_mtu - Change the Maximum Transfer Unit
 * @netdev: network interface device structure
 * @new_mtu: new value for maximum frame size
 *
 * Returns 0 on success, negative on failure
 */
static int atl1_change_mtu(struct net_device *netdev, int new_mtu)
2697 2698
{
	struct atl1_adapter *adapter = netdev_priv(netdev);
J
Jay Cliburn 已提交
2699 2700
	int old_mtu = netdev->mtu;
	int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2701

J
Jay Cliburn 已提交
2702 2703 2704 2705 2706
	if ((max_frame < ETH_ZLEN + ETH_FCS_LEN) ||
	    (max_frame > MAX_JUMBO_FRAME_SIZE)) {
		if (netif_msg_link(adapter))
			dev_warn(&adapter->pdev->dev, "invalid MTU setting\n");
		return -EINVAL;
2707
	}
J
Jay Cliburn 已提交
2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720

	adapter->hw.max_frame_size = max_frame;
	adapter->hw.tx_jumbo_task_th = (max_frame + 7) >> 3;
	adapter->rx_buffer_len = (max_frame + 7) & ~7;
	adapter->hw.rx_jumbo_th = adapter->rx_buffer_len / 8;

	netdev->mtu = new_mtu;
	if ((old_mtu != new_mtu) && netif_running(netdev)) {
		atl1_down(adapter);
		atl1_up(adapter);
	}

	return 0;
2721 2722
}

J
Jay Cliburn 已提交
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 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774
/*
 * atl1_open - Called when a network interface is made active
 * @netdev: network interface device structure
 *
 * Returns 0 on success, negative value on failure
 *
 * The open entry point is called when a network interface is made
 * active by the system (IFF_UP).  At this point all resources needed
 * for transmit and receive operations are allocated, the interrupt
 * handler is registered with the OS, the watchdog timer is started,
 * and the stack is notified that the interface is ready.
 */
static int atl1_open(struct net_device *netdev)
{
	struct atl1_adapter *adapter = netdev_priv(netdev);
	int err;

	/* allocate transmit descriptors */
	err = atl1_setup_ring_resources(adapter);
	if (err)
		return err;

	err = atl1_up(adapter);
	if (err)
		goto err_up;

	return 0;

err_up:
	atl1_reset(adapter);
	return err;
}

/*
 * atl1_close - Disables a network interface
 * @netdev: network interface device structure
 *
 * Returns 0, this is not allowed to fail
 *
 * The close entry point is called when an interface is de-activated
 * by the OS.  The hardware is still under the drivers control, but
 * needs to be disabled.  A global MAC reset is issued to stop the
 * hardware, and all transmit and receive resources are freed.
 */
static int atl1_close(struct net_device *netdev)
{
	struct atl1_adapter *adapter = netdev_priv(netdev);
	atl1_down(adapter);
	atl1_free_ring_resources(adapter);
	return 0;
}

J
Jay Cliburn 已提交
2775 2776
#ifdef CONFIG_PM
static int atl1_suspend(struct pci_dev *pdev, pm_message_t state)
J
Jay Cliburn 已提交
2777
{
J
Jay Cliburn 已提交
2778 2779 2780 2781 2782
	struct net_device *netdev = pci_get_drvdata(pdev);
	struct atl1_adapter *adapter = netdev_priv(netdev);
	struct atl1_hw *hw = &adapter->hw;
	u32 ctrl = 0;
	u32 wufc = adapter->wol;
2783 2784 2785 2786
	u32 val;
	int retval;
	u16 speed;
	u16 duplex;
J
Jay Cliburn 已提交
2787 2788

	netif_device_detach(netdev);
J
Jay Cliburn 已提交
2789 2790
	if (netif_running(netdev))
		atl1_down(adapter);
J
Jay Cliburn 已提交
2791

2792 2793 2794 2795
	retval = pci_save_state(pdev);
	if (retval)
		return retval;

J
Jay Cliburn 已提交
2796 2797
	atl1_read_phy_reg(hw, MII_BMSR, (u16 *) & ctrl);
	atl1_read_phy_reg(hw, MII_BMSR, (u16 *) & ctrl);
2798 2799
	val = ctrl & BMSR_LSTATUS;
	if (val)
J
Jay Cliburn 已提交
2800
		wufc &= ~ATLX_WUFC_LNKC;
J
Jay Cliburn 已提交
2801

2802 2803 2804 2805 2806 2807 2808 2809
	if (val && wufc) {
		val = atl1_get_speed_and_duplex(hw, &speed, &duplex);
		if (val) {
			if (netif_msg_ifdown(adapter))
				dev_printk(KERN_DEBUG, &pdev->dev,
					"error getting speed/duplex\n");
			goto disable_wol;
		}
J
Jay Cliburn 已提交
2810 2811 2812

		ctrl = 0;

2813 2814 2815
		/* enable magic packet WOL */
		if (wufc & ATLX_WUFC_MAG)
			ctrl |= (WOL_MAGIC_EN | WOL_MAGIC_PME_EN);
J
Jay Cliburn 已提交
2816
		iowrite32(ctrl, hw->hw_addr + REG_WOL_CTRL);
2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829
		ioread32(hw->hw_addr + REG_WOL_CTRL);

		/* configure the mac */
		ctrl = MAC_CTRL_RX_EN;
		ctrl |= ((u32)((speed == SPEED_1000) ? MAC_CTRL_SPEED_1000 :
			MAC_CTRL_SPEED_10_100) << MAC_CTRL_SPEED_SHIFT);
		if (duplex == FULL_DUPLEX)
			ctrl |= MAC_CTRL_DUPLX;
		ctrl |= (((u32)adapter->hw.preamble_len &
			MAC_CTRL_PRMLEN_MASK) << MAC_CTRL_PRMLEN_SHIFT);
		if (adapter->vlgrp)
			ctrl |= MAC_CTRL_RMV_VLAN;
		if (wufc & ATLX_WUFC_MAG)
J
Jay Cliburn 已提交
2830 2831
			ctrl |= MAC_CTRL_BC_EN;
		iowrite32(ctrl, hw->hw_addr + REG_MAC_CTRL);
2832 2833 2834 2835 2836 2837 2838 2839 2840 2841
		ioread32(hw->hw_addr + REG_MAC_CTRL);

		/* poke the PHY */
		ctrl = ioread32(hw->hw_addr + REG_PCIE_PHYMISC);
		ctrl |= PCIE_PHYMISC_FORCE_RCV_DET;
		iowrite32(ctrl, hw->hw_addr + REG_PCIE_PHYMISC);
		ioread32(hw->hw_addr + REG_PCIE_PHYMISC);

		pci_enable_wake(pdev, pci_choose_state(pdev, state), 1);
		goto exit;
J
Jay Cliburn 已提交
2842 2843
	}

2844 2845 2846 2847 2848 2849 2850 2851 2852 2853
	if (!val && wufc) {
		ctrl |= (WOL_LINK_CHG_EN | WOL_LINK_CHG_PME_EN);
		iowrite32(ctrl, hw->hw_addr + REG_WOL_CTRL);
		ioread32(hw->hw_addr + REG_WOL_CTRL);
		iowrite32(0, hw->hw_addr + REG_MAC_CTRL);
		ioread32(hw->hw_addr + REG_MAC_CTRL);
		hw->phy_configured = false;
		pci_enable_wake(pdev, pci_choose_state(pdev, state), 1);
		goto exit;
	}
J
Jay Cliburn 已提交
2854

2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869
disable_wol:
	iowrite32(0, hw->hw_addr + REG_WOL_CTRL);
	ioread32(hw->hw_addr + REG_WOL_CTRL);
	ctrl = ioread32(hw->hw_addr + REG_PCIE_PHYMISC);
	ctrl |= PCIE_PHYMISC_FORCE_RCV_DET;
	iowrite32(ctrl, hw->hw_addr + REG_PCIE_PHYMISC);
	ioread32(hw->hw_addr + REG_PCIE_PHYMISC);
	atl1_phy_enter_power_saving(hw);
	hw->phy_configured = false;
	pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
exit:
	if (netif_running(netdev))
		pci_disable_msi(adapter->pdev);
	pci_disable_device(pdev);
	pci_set_power_state(pdev, pci_choose_state(pdev, state));
J
Jay Cliburn 已提交
2870 2871

	return 0;
J
Jay Cliburn 已提交
2872 2873
}

J
Jay Cliburn 已提交
2874
static int atl1_resume(struct pci_dev *pdev)
J
Jay Cliburn 已提交
2875
{
J
Jay Cliburn 已提交
2876 2877
	struct net_device *netdev = pci_get_drvdata(pdev);
	struct atl1_adapter *adapter = netdev_priv(netdev);
J
Jay Cliburn 已提交
2878
	u32 err;
J
Jay Cliburn 已提交
2879

J
Jay Cliburn 已提交
2880
	pci_set_power_state(pdev, PCI_D0);
J
Jay Cliburn 已提交
2881 2882
	pci_restore_state(pdev);

J
Jay Cliburn 已提交
2883
	err = pci_enable_device(pdev);
2884 2885 2886 2887 2888 2889 2890 2891 2892
	if (err) {
		if (netif_msg_ifup(adapter))
			dev_printk(KERN_DEBUG, &pdev->dev,
				"error enabling pci device\n");
		return err;
	}

	pci_set_master(pdev);
	iowrite32(0, adapter->hw.hw_addr + REG_WOL_CTRL);
J
Jay Cliburn 已提交
2893 2894 2895
	pci_enable_wake(pdev, PCI_D3hot, 0);
	pci_enable_wake(pdev, PCI_D3cold, 0);

2896 2897
	atl1_reset_hw(&adapter->hw);
	adapter->cmb.cmb->int_stats = 0;
J
Jay Cliburn 已提交
2898 2899 2900 2901 2902 2903

	if (netif_running(netdev))
		atl1_up(adapter);
	netif_device_attach(netdev);

	return 0;
J
Jay Cliburn 已提交
2904
}
J
Jay Cliburn 已提交
2905 2906 2907 2908
#else
#define atl1_suspend NULL
#define atl1_resume NULL
#endif
J
Jay Cliburn 已提交
2909

J
Jay Cliburn 已提交
2910 2911 2912 2913 2914 2915 2916
static void atl1_shutdown(struct pci_dev *pdev)
{
#ifdef CONFIG_PM
	atl1_suspend(pdev, PMSG_SUSPEND);
#endif
}

J
Jay Cliburn 已提交
2917 2918
#ifdef CONFIG_NET_POLL_CONTROLLER
static void atl1_poll_controller(struct net_device *netdev)
J
Jay Cliburn 已提交
2919
{
J
Jay Cliburn 已提交
2920 2921 2922
	disable_irq(netdev->irq);
	atl1_intr(netdev->irq, netdev);
	enable_irq(netdev->irq);
J
Jay Cliburn 已提交
2923
}
J
Jay Cliburn 已提交
2924
#endif
J
Jay Cliburn 已提交
2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937

/*
 * atl1_probe - Device Initialization Routine
 * @pdev: PCI device information struct
 * @ent: entry in atl1_pci_tbl
 *
 * Returns 0 on success, negative on failure
 *
 * atl1_probe initializes an adapter identified by a pci_dev structure.
 * The OS initialization, configuring of the adapter private structure,
 * and a hardware reset occur.
 */
static int __devinit atl1_probe(struct pci_dev *pdev,
J
Jay Cliburn 已提交
2938
	const struct pci_device_id *ent)
J
Jay Cliburn 已提交
2939 2940 2941 2942 2943 2944 2945 2946 2947 2948
{
	struct net_device *netdev;
	struct atl1_adapter *adapter;
	static int cards_found = 0;
	int err;

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

2949
	/*
C
Chris Snook 已提交
2950 2951 2952 2953 2954 2955 2956 2957
	 * The atl1 chip can DMA to 64-bit addresses, but it uses a single
	 * shared register for the high 32 bits, so only a single, aligned,
	 * 4 GB physical address range can be used at a time.
	 *
	 * Supporting 64-bit DMA on this hardware is more trouble than it's
	 * worth.  It is far easier to limit to 32-bit DMA than update
	 * various kernel subsystems to support the mechanics required by a
	 * fixed-high-32-bit system.
2958 2959
	 */
	err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
J
Jay Cliburn 已提交
2960
	if (err) {
2961 2962
		dev_err(&pdev->dev, "no usable DMA configuration\n");
		goto err_dma;
J
Jay Cliburn 已提交
2963
	}
J
Jay Cliburn 已提交
2964 2965
	/*
	 * Mark all PCI regions associated with PCI device
J
Jay Cliburn 已提交
2966 2967
	 * pdev as being reserved by owner atl1_driver_name
	 */
J
Jay Cliburn 已提交
2968
	err = pci_request_regions(pdev, ATLX_DRIVER_NAME);
J
Jay Cliburn 已提交
2969 2970 2971
	if (err)
		goto err_request_regions;

J
Jay Cliburn 已提交
2972 2973
	/*
	 * Enables bus-mastering on the device and calls
J
Jay Cliburn 已提交
2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989
	 * pcibios_set_master to do the needed arch specific settings
	 */
	pci_set_master(pdev);

	netdev = alloc_etherdev(sizeof(struct atl1_adapter));
	if (!netdev) {
		err = -ENOMEM;
		goto err_alloc_etherdev;
	}
	SET_NETDEV_DEV(netdev, &pdev->dev);

	pci_set_drvdata(pdev, netdev);
	adapter = netdev_priv(netdev);
	adapter->netdev = netdev;
	adapter->pdev = pdev;
	adapter->hw.back = adapter;
J
Jay Cliburn 已提交
2990
	adapter->msg_enable = netif_msg_init(debug, atl1_default_msg);
J
Jay Cliburn 已提交
2991 2992 2993 2994 2995 2996 2997

	adapter->hw.hw_addr = pci_iomap(pdev, 0, 0);
	if (!adapter->hw.hw_addr) {
		err = -EIO;
		goto err_pci_iomap;
	}
	/* get device revision number */
J
Jay Cliburn 已提交
2998
	adapter->hw.dev_rev = ioread16(adapter->hw.hw_addr +
J
Jay Cliburn 已提交
2999
		(REG_MASTER_CTRL + 2));
J
Jay Cliburn 已提交
3000 3001
	if (netif_msg_probe(adapter))
		dev_info(&pdev->dev, "version %s\n", ATLX_DRIVER_VERSION);
J
Jay Cliburn 已提交
3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015

	/* set default ring resource counts */
	adapter->rfd_ring.count = adapter->rrd_ring.count = ATL1_DEFAULT_RFD;
	adapter->tpd_ring.count = ATL1_DEFAULT_TPD;

	adapter->mii.dev = netdev;
	adapter->mii.mdio_read = mdio_read;
	adapter->mii.mdio_write = mdio_write;
	adapter->mii.phy_id_mask = 0x1f;
	adapter->mii.reg_num_mask = 0x1f;

	netdev->open = &atl1_open;
	netdev->stop = &atl1_close;
	netdev->hard_start_xmit = &atl1_xmit_frame;
J
Jay Cliburn 已提交
3016 3017
	netdev->get_stats = &atlx_get_stats;
	netdev->set_multicast_list = &atlx_set_multi;
J
Jay Cliburn 已提交
3018 3019
	netdev->set_mac_address = &atl1_set_mac;
	netdev->change_mtu = &atl1_change_mtu;
J
Jay Cliburn 已提交
3020 3021
	netdev->do_ioctl = &atlx_ioctl;
	netdev->tx_timeout = &atlx_tx_timeout;
J
Jay Cliburn 已提交
3022
	netdev->watchdog_timeo = 5 * HZ;
A
Alexey Dobriyan 已提交
3023 3024 3025
#ifdef CONFIG_NET_POLL_CONTROLLER
	netdev->poll_controller = atl1_poll_controller;
#endif
J
Jay Cliburn 已提交
3026
	netdev->vlan_rx_register = atlx_vlan_rx_register;
3027

J
Jay Cliburn 已提交
3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038
	netdev->ethtool_ops = &atl1_ethtool_ops;
	adapter->bd_number = cards_found;

	/* setup the private structure */
	err = atl1_sw_init(adapter);
	if (err)
		goto err_common;

	netdev->features = NETIF_F_HW_CSUM;
	netdev->features |= NETIF_F_SG;
	netdev->features |= (NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX);
J
Jay Cliburn 已提交
3039
	netdev->features |= NETIF_F_TSO;
J
Jay Cliburn 已提交
3040 3041 3042 3043 3044 3045 3046 3047 3048 3049
	netdev->features |= NETIF_F_LLTX;

	/*
	 * patch for some L1 of old version,
	 * the final version of L1 may not need these
	 * patches
	 */
	/* atl1_pcie_patch(adapter); */

	/* really reset GPHY core */
J
Jay Cliburn 已提交
3050
	iowrite16(0, adapter->hw.hw_addr + REG_PHY_ENABLE);
J
Jay Cliburn 已提交
3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094

	/*
	 * reset the controller to
	 * put the device in a known good starting state
	 */
	if (atl1_reset_hw(&adapter->hw)) {
		err = -EIO;
		goto err_common;
	}

	/* copy the MAC address out of the EEPROM */
	atl1_read_mac_addr(&adapter->hw);
	memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);

	if (!is_valid_ether_addr(netdev->dev_addr)) {
		err = -EIO;
		goto err_common;
	}

	atl1_check_options(adapter);

	/* pre-init the MAC, and setup link */
	err = atl1_init_hw(&adapter->hw);
	if (err) {
		err = -EIO;
		goto err_common;
	}

	atl1_pcie_patch(adapter);
	/* assume we have no link for now */
	netif_carrier_off(netdev);
	netif_stop_queue(netdev);

	init_timer(&adapter->watchdog_timer);
	adapter->watchdog_timer.function = &atl1_watchdog;
	adapter->watchdog_timer.data = (unsigned long)adapter;

	init_timer(&adapter->phy_config_timer);
	adapter->phy_config_timer.function = &atl1_phy_config;
	adapter->phy_config_timer.data = (unsigned long)adapter;
	adapter->phy_timer_pending = false;

	INIT_WORK(&adapter->tx_timeout_task, atl1_tx_timeout_task);

J
Jay Cliburn 已提交
3095
	INIT_WORK(&adapter->link_chg_task, atlx_link_chg_task);
J
Jay Cliburn 已提交
3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136

	INIT_WORK(&adapter->pcie_dma_to_rst_task, atl1_tx_timeout_task);

	err = register_netdev(netdev);
	if (err)
		goto err_common;

	cards_found++;
	atl1_via_workaround(adapter);
	return 0;

err_common:
	pci_iounmap(pdev, adapter->hw.hw_addr);
err_pci_iomap:
	free_netdev(netdev);
err_alloc_etherdev:
	pci_release_regions(pdev);
err_dma:
err_request_regions:
	pci_disable_device(pdev);
	return err;
}

/*
 * atl1_remove - Device Removal Routine
 * @pdev: PCI device information struct
 *
 * atl1_remove is called by the PCI subsystem to alert the driver
 * that it should release a PCI device.  The could be caused by a
 * Hot-Plug event, or because the driver is going to be removed from
 * memory.
 */
static void __devexit atl1_remove(struct pci_dev *pdev)
{
	struct net_device *netdev = pci_get_drvdata(pdev);
	struct atl1_adapter *adapter;
	/* Device not available. Return. */
	if (!netdev)
		return;

	adapter = netdev_priv(netdev);
C
Chris Snook 已提交
3137

J
Jay Cliburn 已提交
3138 3139
	/*
	 * Some atl1 boards lack persistent storage for their MAC, and get it
C
Chris Snook 已提交
3140 3141 3142 3143
	 * from the BIOS during POST.  If we've been messing with the MAC
	 * address, we need to save the permanent one.
	 */
	if (memcmp(adapter->hw.mac_addr, adapter->hw.perm_mac_addr, ETH_ALEN)) {
J
Jay Cliburn 已提交
3144 3145
		memcpy(adapter->hw.mac_addr, adapter->hw.perm_mac_addr,
			ETH_ALEN);
C
Chris Snook 已提交
3146 3147 3148
		atl1_set_mac_addr(&adapter->hw);
	}

J
Jay Cliburn 已提交
3149
	iowrite16(0, adapter->hw.hw_addr + REG_PHY_ENABLE);
J
Jay Cliburn 已提交
3150 3151 3152 3153 3154 3155 3156 3157
	unregister_netdev(netdev);
	pci_iounmap(pdev, adapter->hw.hw_addr);
	pci_release_regions(pdev);
	free_netdev(netdev);
	pci_disable_device(pdev);
}

static struct pci_driver atl1_driver = {
J
Jay Cliburn 已提交
3158
	.name = ATLX_DRIVER_NAME,
J
Jay Cliburn 已提交
3159 3160 3161 3162
	.id_table = atl1_pci_tbl,
	.probe = atl1_probe,
	.remove = __devexit_p(atl1_remove),
	.suspend = atl1_suspend,
J
Jay Cliburn 已提交
3163 3164
	.resume = atl1_resume,
	.shutdown = atl1_shutdown
J
Jay Cliburn 已提交
3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190
};

/*
 * atl1_exit_module - Driver Exit Cleanup Routine
 *
 * atl1_exit_module is called just before the driver is removed
 * from memory.
 */
static void __exit atl1_exit_module(void)
{
	pci_unregister_driver(&atl1_driver);
}

/*
 * atl1_init_module - Driver Registration Routine
 *
 * atl1_init_module is the first routine called when the driver is
 * loaded. All it does is register with the PCI subsystem.
 */
static int __init atl1_init_module(void)
{
	return pci_register_driver(&atl1_driver);
}

module_init(atl1_init_module);
module_exit(atl1_exit_module);
J
Jay Cliburn 已提交
3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236

struct atl1_stats {
	char stat_string[ETH_GSTRING_LEN];
	int sizeof_stat;
	int stat_offset;
};

#define ATL1_STAT(m) \
	sizeof(((struct atl1_adapter *)0)->m), offsetof(struct atl1_adapter, m)

static struct atl1_stats atl1_gstrings_stats[] = {
	{"rx_packets", ATL1_STAT(soft_stats.rx_packets)},
	{"tx_packets", ATL1_STAT(soft_stats.tx_packets)},
	{"rx_bytes", ATL1_STAT(soft_stats.rx_bytes)},
	{"tx_bytes", ATL1_STAT(soft_stats.tx_bytes)},
	{"rx_errors", ATL1_STAT(soft_stats.rx_errors)},
	{"tx_errors", ATL1_STAT(soft_stats.tx_errors)},
	{"rx_dropped", ATL1_STAT(net_stats.rx_dropped)},
	{"tx_dropped", ATL1_STAT(net_stats.tx_dropped)},
	{"multicast", ATL1_STAT(soft_stats.multicast)},
	{"collisions", ATL1_STAT(soft_stats.collisions)},
	{"rx_length_errors", ATL1_STAT(soft_stats.rx_length_errors)},
	{"rx_over_errors", ATL1_STAT(soft_stats.rx_missed_errors)},
	{"rx_crc_errors", ATL1_STAT(soft_stats.rx_crc_errors)},
	{"rx_frame_errors", ATL1_STAT(soft_stats.rx_frame_errors)},
	{"rx_fifo_errors", ATL1_STAT(soft_stats.rx_fifo_errors)},
	{"rx_missed_errors", ATL1_STAT(soft_stats.rx_missed_errors)},
	{"tx_aborted_errors", ATL1_STAT(soft_stats.tx_aborted_errors)},
	{"tx_carrier_errors", ATL1_STAT(soft_stats.tx_carrier_errors)},
	{"tx_fifo_errors", ATL1_STAT(soft_stats.tx_fifo_errors)},
	{"tx_window_errors", ATL1_STAT(soft_stats.tx_window_errors)},
	{"tx_abort_exce_coll", ATL1_STAT(soft_stats.excecol)},
	{"tx_abort_late_coll", ATL1_STAT(soft_stats.latecol)},
	{"tx_deferred_ok", ATL1_STAT(soft_stats.deffer)},
	{"tx_single_coll_ok", ATL1_STAT(soft_stats.scc)},
	{"tx_multi_coll_ok", ATL1_STAT(soft_stats.mcc)},
	{"tx_underun", ATL1_STAT(soft_stats.tx_underun)},
	{"tx_trunc", ATL1_STAT(soft_stats.tx_trunc)},
	{"tx_pause", ATL1_STAT(soft_stats.tx_pause)},
	{"rx_pause", ATL1_STAT(soft_stats.rx_pause)},
	{"rx_rrd_ov", ATL1_STAT(soft_stats.rx_rrd_ov)},
	{"rx_trunc", ATL1_STAT(soft_stats.rx_trunc)}
};

static void atl1_get_ethtool_stats(struct net_device *netdev,
	struct ethtool_stats *stats, u64 *data)
3237
{
J
Jay Cliburn 已提交
3238
	struct atl1_adapter *adapter = netdev_priv(netdev);
3239
	int i;
J
Jay Cliburn 已提交
3240
	char *p;
3241

J
Jay Cliburn 已提交
3242 3243 3244 3245
	for (i = 0; i < ARRAY_SIZE(atl1_gstrings_stats); i++) {
		p = (char *)adapter+atl1_gstrings_stats[i].stat_offset;
		data[i] = (atl1_gstrings_stats[i].sizeof_stat ==
			sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
3246 3247 3248 3249
	}

}

J
Jay Cliburn 已提交
3250
static int atl1_get_sset_count(struct net_device *netdev, int sset)
3251
{
J
Jay Cliburn 已提交
3252 3253 3254 3255 3256 3257
	switch (sset) {
	case ETH_SS_STATS:
		return ARRAY_SIZE(atl1_gstrings_stats);
	default:
		return -EOPNOTSUPP;
	}
3258 3259
}

J
Jay Cliburn 已提交
3260 3261
static int atl1_get_settings(struct net_device *netdev,
	struct ethtool_cmd *ecmd)
3262
{
J
Jay Cliburn 已提交
3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307
	struct atl1_adapter *adapter = netdev_priv(netdev);
	struct atl1_hw *hw = &adapter->hw;

	ecmd->supported = (SUPPORTED_10baseT_Half |
			   SUPPORTED_10baseT_Full |
			   SUPPORTED_100baseT_Half |
			   SUPPORTED_100baseT_Full |
			   SUPPORTED_1000baseT_Full |
			   SUPPORTED_Autoneg | SUPPORTED_TP);
	ecmd->advertising = ADVERTISED_TP;
	if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
	    hw->media_type == MEDIA_TYPE_1000M_FULL) {
		ecmd->advertising |= ADVERTISED_Autoneg;
		if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR) {
			ecmd->advertising |= ADVERTISED_Autoneg;
			ecmd->advertising |=
			    (ADVERTISED_10baseT_Half |
			     ADVERTISED_10baseT_Full |
			     ADVERTISED_100baseT_Half |
			     ADVERTISED_100baseT_Full |
			     ADVERTISED_1000baseT_Full);
		} else
			ecmd->advertising |= (ADVERTISED_1000baseT_Full);
	}
	ecmd->port = PORT_TP;
	ecmd->phy_address = 0;
	ecmd->transceiver = XCVR_INTERNAL;

	if (netif_carrier_ok(adapter->netdev)) {
		u16 link_speed, link_duplex;
		atl1_get_speed_and_duplex(hw, &link_speed, &link_duplex);
		ecmd->speed = link_speed;
		if (link_duplex == FULL_DUPLEX)
			ecmd->duplex = DUPLEX_FULL;
		else
			ecmd->duplex = DUPLEX_HALF;
	} else {
		ecmd->speed = -1;
		ecmd->duplex = -1;
	}
	if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
	    hw->media_type == MEDIA_TYPE_1000M_FULL)
		ecmd->autoneg = AUTONEG_ENABLE;
	else
		ecmd->autoneg = AUTONEG_DISABLE;
3308 3309 3310 3311

	return 0;
}

J
Jay Cliburn 已提交
3312 3313
static int atl1_set_settings(struct net_device *netdev,
	struct ethtool_cmd *ecmd)
3314
{
J
Jay Cliburn 已提交
3315 3316
	struct atl1_adapter *adapter = netdev_priv(netdev);
	struct atl1_hw *hw = &adapter->hw;
3317
	u16 phy_data;
J
Jay Cliburn 已提交
3318 3319
	int ret_val = 0;
	u16 old_media_type = hw->media_type;
3320

J
Jay Cliburn 已提交
3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377
	if (netif_running(adapter->netdev)) {
		if (netif_msg_link(adapter))
			dev_dbg(&adapter->pdev->dev,
				"ethtool shutting down adapter\n");
		atl1_down(adapter);
	}

	if (ecmd->autoneg == AUTONEG_ENABLE)
		hw->media_type = MEDIA_TYPE_AUTO_SENSOR;
	else {
		if (ecmd->speed == SPEED_1000) {
			if (ecmd->duplex != DUPLEX_FULL) {
				if (netif_msg_link(adapter))
					dev_warn(&adapter->pdev->dev,
						"1000M half is invalid\n");
				ret_val = -EINVAL;
				goto exit_sset;
			}
			hw->media_type = MEDIA_TYPE_1000M_FULL;
		} else if (ecmd->speed == SPEED_100) {
			if (ecmd->duplex == DUPLEX_FULL)
				hw->media_type = MEDIA_TYPE_100M_FULL;
			else
				hw->media_type = MEDIA_TYPE_100M_HALF;
		} else {
			if (ecmd->duplex == DUPLEX_FULL)
				hw->media_type = MEDIA_TYPE_10M_FULL;
			else
				hw->media_type = MEDIA_TYPE_10M_HALF;
		}
	}
	switch (hw->media_type) {
	case MEDIA_TYPE_AUTO_SENSOR:
		ecmd->advertising =
		    ADVERTISED_10baseT_Half |
		    ADVERTISED_10baseT_Full |
		    ADVERTISED_100baseT_Half |
		    ADVERTISED_100baseT_Full |
		    ADVERTISED_1000baseT_Full |
		    ADVERTISED_Autoneg | ADVERTISED_TP;
		break;
	case MEDIA_TYPE_1000M_FULL:
		ecmd->advertising =
		    ADVERTISED_1000baseT_Full |
		    ADVERTISED_Autoneg | ADVERTISED_TP;
		break;
	default:
		ecmd->advertising = 0;
		break;
	}
	if (atl1_phy_setup_autoneg_adv(hw)) {
		ret_val = -EINVAL;
		if (netif_msg_link(adapter))
			dev_warn(&adapter->pdev->dev,
				"invalid ethtool speed/duplex setting\n");
		goto exit_sset;
	}
3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400
	if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
	    hw->media_type == MEDIA_TYPE_1000M_FULL)
		phy_data = MII_CR_RESET | MII_CR_AUTO_NEG_EN;
	else {
		switch (hw->media_type) {
		case MEDIA_TYPE_100M_FULL:
			phy_data =
			    MII_CR_FULL_DUPLEX | MII_CR_SPEED_100 |
			    MII_CR_RESET;
			break;
		case MEDIA_TYPE_100M_HALF:
			phy_data = MII_CR_SPEED_100 | MII_CR_RESET;
			break;
		case MEDIA_TYPE_10M_FULL:
			phy_data =
			    MII_CR_FULL_DUPLEX | MII_CR_SPEED_10 | MII_CR_RESET;
			break;
		default:
			/* MEDIA_TYPE_10M_HALF: */
			phy_data = MII_CR_SPEED_10 | MII_CR_RESET;
			break;
		}
	}
J
Jay Cliburn 已提交
3401 3402 3403 3404
	atl1_write_phy_reg(hw, MII_BMCR, phy_data);
exit_sset:
	if (ret_val)
		hw->media_type = old_media_type;
3405

J
Jay Cliburn 已提交
3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418
	if (netif_running(adapter->netdev)) {
		if (netif_msg_link(adapter))
			dev_dbg(&adapter->pdev->dev,
				"ethtool starting adapter\n");
		atl1_up(adapter);
	} else if (!ret_val) {
		if (netif_msg_link(adapter))
			dev_dbg(&adapter->pdev->dev,
				"ethtool resetting adapter\n");
		atl1_reset(adapter);
	}
	return ret_val;
}
3419

J
Jay Cliburn 已提交
3420 3421 3422 3423
static void atl1_get_drvinfo(struct net_device *netdev,
	struct ethtool_drvinfo *drvinfo)
{
	struct atl1_adapter *adapter = netdev_priv(netdev);
3424

J
Jay Cliburn 已提交
3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467
	strncpy(drvinfo->driver, ATLX_DRIVER_NAME, sizeof(drvinfo->driver));
	strncpy(drvinfo->version, ATLX_DRIVER_VERSION,
		sizeof(drvinfo->version));
	strncpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
	strncpy(drvinfo->bus_info, pci_name(adapter->pdev),
		sizeof(drvinfo->bus_info));
	drvinfo->eedump_len = ATL1_EEDUMP_LEN;
}

static void atl1_get_wol(struct net_device *netdev,
	struct ethtool_wolinfo *wol)
{
	struct atl1_adapter *adapter = netdev_priv(netdev);

	wol->supported = WAKE_UCAST | WAKE_MCAST | WAKE_BCAST | WAKE_MAGIC;
	wol->wolopts = 0;
	if (adapter->wol & ATLX_WUFC_EX)
		wol->wolopts |= WAKE_UCAST;
	if (adapter->wol & ATLX_WUFC_MC)
		wol->wolopts |= WAKE_MCAST;
	if (adapter->wol & ATLX_WUFC_BC)
		wol->wolopts |= WAKE_BCAST;
	if (adapter->wol & ATLX_WUFC_MAG)
		wol->wolopts |= WAKE_MAGIC;
	return;
}

static int atl1_set_wol(struct net_device *netdev,
	struct ethtool_wolinfo *wol)
{
	struct atl1_adapter *adapter = netdev_priv(netdev);

	if (wol->wolopts & (WAKE_PHY | WAKE_ARP | WAKE_MAGICSECURE))
		return -EOPNOTSUPP;
	adapter->wol = 0;
	if (wol->wolopts & WAKE_UCAST)
		adapter->wol |= ATLX_WUFC_EX;
	if (wol->wolopts & WAKE_MCAST)
		adapter->wol |= ATLX_WUFC_MC;
	if (wol->wolopts & WAKE_BCAST)
		adapter->wol |= ATLX_WUFC_BC;
	if (wol->wolopts & WAKE_MAGIC)
		adapter->wol |= ATLX_WUFC_MAG;
3468 3469 3470
	return 0;
}

J
Jay Cliburn 已提交
3471
static u32 atl1_get_msglevel(struct net_device *netdev)
3472
{
J
Jay Cliburn 已提交
3473 3474 3475
	struct atl1_adapter *adapter = netdev_priv(netdev);
	return adapter->msg_enable;
}
3476

J
Jay Cliburn 已提交
3477 3478 3479 3480 3481
static void atl1_set_msglevel(struct net_device *netdev, u32 value)
{
	struct atl1_adapter *adapter = netdev_priv(netdev);
	adapter->msg_enable = value;
}
3482

J
Jay Cliburn 已提交
3483 3484 3485 3486
static int atl1_get_regs_len(struct net_device *netdev)
{
	return ATL1_REG_COUNT * sizeof(u32);
}
3487

J
Jay Cliburn 已提交
3488 3489 3490 3491 3492 3493 3494
static void atl1_get_regs(struct net_device *netdev, struct ethtool_regs *regs,
	void *p)
{
	struct atl1_adapter *adapter = netdev_priv(netdev);
	struct atl1_hw *hw = &adapter->hw;
	unsigned int i;
	u32 *regbuf = p;
3495

J
Jay Cliburn 已提交
3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532
	for (i = 0; i < ATL1_REG_COUNT; i++) {
		/*
		 * This switch statement avoids reserved regions
		 * of register space.
		 */
		switch (i) {
		case 6 ... 9:
		case 14:
		case 29 ... 31:
		case 34 ... 63:
		case 75 ... 127:
		case 136 ... 1023:
		case 1027 ... 1087:
		case 1091 ... 1151:
		case 1194 ... 1195:
		case 1200 ... 1201:
		case 1206 ... 1213:
		case 1216 ... 1279:
		case 1290 ... 1311:
		case 1323 ... 1343:
		case 1358 ... 1359:
		case 1368 ... 1375:
		case 1378 ... 1383:
		case 1388 ... 1391:
		case 1393 ... 1395:
		case 1402 ... 1403:
		case 1410 ... 1471:
		case 1522 ... 1535:
			/* reserved region; don't read it */
			regbuf[i] = 0;
			break;
		default:
			/* unreserved region */
			regbuf[i] = ioread32(hw->hw_addr + (i * sizeof(u32)));
		}
	}
}
3533

J
Jay Cliburn 已提交
3534 3535 3536 3537 3538 3539
static void atl1_get_ringparam(struct net_device *netdev,
	struct ethtool_ringparam *ring)
{
	struct atl1_adapter *adapter = netdev_priv(netdev);
	struct atl1_tpd_ring *txdr = &adapter->tpd_ring;
	struct atl1_rfd_ring *rxdr = &adapter->rfd_ring;
3540

J
Jay Cliburn 已提交
3541 3542 3543 3544 3545 3546 3547 3548 3549
	ring->rx_max_pending = ATL1_MAX_RFD;
	ring->tx_max_pending = ATL1_MAX_TPD;
	ring->rx_mini_max_pending = 0;
	ring->rx_jumbo_max_pending = 0;
	ring->rx_pending = rxdr->count;
	ring->tx_pending = txdr->count;
	ring->rx_mini_pending = 0;
	ring->rx_jumbo_pending = 0;
}
3550

J
Jay Cliburn 已提交
3551 3552 3553 3554 3555 3556 3557
static int atl1_set_ringparam(struct net_device *netdev,
	struct ethtool_ringparam *ring)
{
	struct atl1_adapter *adapter = netdev_priv(netdev);
	struct atl1_tpd_ring *tpdr = &adapter->tpd_ring;
	struct atl1_rrd_ring *rrdr = &adapter->rrd_ring;
	struct atl1_rfd_ring *rfdr = &adapter->rfd_ring;
3558

J
Jay Cliburn 已提交
3559 3560 3561 3562 3563
	struct atl1_tpd_ring tpd_old, tpd_new;
	struct atl1_rfd_ring rfd_old, rfd_new;
	struct atl1_rrd_ring rrd_old, rrd_new;
	struct atl1_ring_header rhdr_old, rhdr_new;
	int err;
3564

J
Jay Cliburn 已提交
3565 3566 3567 3568
	tpd_old = adapter->tpd_ring;
	rfd_old = adapter->rfd_ring;
	rrd_old = adapter->rrd_ring;
	rhdr_old = adapter->ring_header;
3569

J
Jay Cliburn 已提交
3570 3571
	if (netif_running(adapter->netdev))
		atl1_down(adapter);
3572

J
Jay Cliburn 已提交
3573 3574 3575 3576 3577
	rfdr->count = (u16) max(ring->rx_pending, (u32) ATL1_MIN_RFD);
	rfdr->count = rfdr->count > ATL1_MAX_RFD ? ATL1_MAX_RFD :
			rfdr->count;
	rfdr->count = (rfdr->count + 3) & ~3;
	rrdr->count = rfdr->count;
3578

J
Jay Cliburn 已提交
3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593
	tpdr->count = (u16) max(ring->tx_pending, (u32) ATL1_MIN_TPD);
	tpdr->count = tpdr->count > ATL1_MAX_TPD ? ATL1_MAX_TPD :
			tpdr->count;
	tpdr->count = (tpdr->count + 3) & ~3;

	if (netif_running(adapter->netdev)) {
		/* try to get new resources before deleting old */
		err = atl1_setup_ring_resources(adapter);
		if (err)
			goto err_setup_ring;

		/*
		 * save the new, restore the old in order to free it,
		 * then restore the new back again
		 */
3594

J
Jay Cliburn 已提交
3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607
		rfd_new = adapter->rfd_ring;
		rrd_new = adapter->rrd_ring;
		tpd_new = adapter->tpd_ring;
		rhdr_new = adapter->ring_header;
		adapter->rfd_ring = rfd_old;
		adapter->rrd_ring = rrd_old;
		adapter->tpd_ring = tpd_old;
		adapter->ring_header = rhdr_old;
		atl1_free_ring_resources(adapter);
		adapter->rfd_ring = rfd_new;
		adapter->rrd_ring = rrd_new;
		adapter->tpd_ring = tpd_new;
		adapter->ring_header = rhdr_new;
3608

J
Jay Cliburn 已提交
3609 3610 3611 3612
		err = atl1_up(adapter);
		if (err)
			return err;
	}
3613
	return 0;
J
Jay Cliburn 已提交
3614 3615 3616 3617 3618 3619 3620 3621

err_setup_ring:
	adapter->rfd_ring = rfd_old;
	adapter->rrd_ring = rrd_old;
	adapter->tpd_ring = tpd_old;
	adapter->ring_header = rhdr_old;
	atl1_up(adapter);
	return err;
3622 3623
}

J
Jay Cliburn 已提交
3624 3625
static void atl1_get_pauseparam(struct net_device *netdev,
	struct ethtool_pauseparam *epause)
3626
{
J
Jay Cliburn 已提交
3627 3628
	struct atl1_adapter *adapter = netdev_priv(netdev);
	struct atl1_hw *hw = &adapter->hw;
3629

J
Jay Cliburn 已提交
3630 3631 3632 3633 3634
	if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
	    hw->media_type == MEDIA_TYPE_1000M_FULL) {
		epause->autoneg = AUTONEG_ENABLE;
	} else {
		epause->autoneg = AUTONEG_DISABLE;
3635
	}
J
Jay Cliburn 已提交
3636 3637
	epause->rx_pause = 1;
	epause->tx_pause = 1;
3638 3639
}

J
Jay Cliburn 已提交
3640 3641
static int atl1_set_pauseparam(struct net_device *netdev,
	struct ethtool_pauseparam *epause)
3642
{
J
Jay Cliburn 已提交
3643 3644
	struct atl1_adapter *adapter = netdev_priv(netdev);
	struct atl1_hw *hw = &adapter->hw;
3645

J
Jay Cliburn 已提交
3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656
	if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
	    hw->media_type == MEDIA_TYPE_1000M_FULL) {
		epause->autoneg = AUTONEG_ENABLE;
	} else {
		epause->autoneg = AUTONEG_DISABLE;
	}

	epause->rx_pause = 1;
	epause->tx_pause = 1;

	return 0;
3657 3658
}

J
Jay Cliburn 已提交
3659 3660
/* FIXME: is this right? -- CHS */
static u32 atl1_get_rx_csum(struct net_device *netdev)
3661
{
J
Jay Cliburn 已提交
3662 3663
	return 1;
}
3664

J
Jay Cliburn 已提交
3665 3666 3667 3668 3669
static void atl1_get_strings(struct net_device *netdev, u32 stringset,
	u8 *data)
{
	u8 *p = data;
	int i;
3670

J
Jay Cliburn 已提交
3671 3672 3673 3674 3675 3676 3677 3678
	switch (stringset) {
	case ETH_SS_STATS:
		for (i = 0; i < ARRAY_SIZE(atl1_gstrings_stats); i++) {
			memcpy(p, atl1_gstrings_stats[i].stat_string,
				ETH_GSTRING_LEN);
			p += ETH_GSTRING_LEN;
		}
		break;
3679 3680 3681
	}
}

J
Jay Cliburn 已提交
3682
static int atl1_nway_reset(struct net_device *netdev)
3683
{
J
Jay Cliburn 已提交
3684 3685
	struct atl1_adapter *adapter = netdev_priv(netdev);
	struct atl1_hw *hw = &adapter->hw;
3686

J
Jay Cliburn 已提交
3687 3688 3689
	if (netif_running(netdev)) {
		u16 phy_data;
		atl1_down(adapter);
3690

J
Jay Cliburn 已提交
3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713
		if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
			hw->media_type == MEDIA_TYPE_1000M_FULL) {
			phy_data = MII_CR_RESET | MII_CR_AUTO_NEG_EN;
		} else {
			switch (hw->media_type) {
			case MEDIA_TYPE_100M_FULL:
				phy_data = MII_CR_FULL_DUPLEX |
					MII_CR_SPEED_100 | MII_CR_RESET;
				break;
			case MEDIA_TYPE_100M_HALF:
				phy_data = MII_CR_SPEED_100 | MII_CR_RESET;
				break;
			case MEDIA_TYPE_10M_FULL:
				phy_data = MII_CR_FULL_DUPLEX |
					MII_CR_SPEED_10 | MII_CR_RESET;
				break;
			default:
				/* MEDIA_TYPE_10M_HALF */
				phy_data = MII_CR_SPEED_10 | MII_CR_RESET;
			}
		}
		atl1_write_phy_reg(hw, MII_BMCR, phy_data);
		atl1_up(adapter);
3714 3715 3716 3717
	}
	return 0;
}

J
Jay Cliburn 已提交
3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741
const struct ethtool_ops atl1_ethtool_ops = {
	.get_settings		= atl1_get_settings,
	.set_settings		= atl1_set_settings,
	.get_drvinfo		= atl1_get_drvinfo,
	.get_wol		= atl1_get_wol,
	.set_wol		= atl1_set_wol,
	.get_msglevel		= atl1_get_msglevel,
	.set_msglevel		= atl1_set_msglevel,
	.get_regs_len		= atl1_get_regs_len,
	.get_regs		= atl1_get_regs,
	.get_ringparam		= atl1_get_ringparam,
	.set_ringparam		= atl1_set_ringparam,
	.get_pauseparam		= atl1_get_pauseparam,
	.set_pauseparam		= atl1_set_pauseparam,
	.get_rx_csum		= atl1_get_rx_csum,
	.set_tx_csum		= ethtool_op_set_tx_hw_csum,
	.get_link		= ethtool_op_get_link,
	.set_sg			= ethtool_op_set_sg,
	.get_strings		= atl1_get_strings,
	.nway_reset		= atl1_nway_reset,
	.get_ethtool_stats	= atl1_get_ethtool_stats,
	.get_sset_count		= atl1_get_sset_count,
	.set_tso		= ethtool_op_set_tso,
};