via-velocity.c 95.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * This code is derived from the VIA reference driver (copyright message
 * below) provided to Red Hat by VIA Networking Technologies, Inc. for
 * addition to the Linux kernel.
 *
 * The code has been merged into one source file, cleaned up to follow
 * Linux coding style,  ported to the Linux 2.6 kernel tree and cleaned
 * for 64bit hardware platforms.
 *
 * TODO
 *	rx_copybreak/alignment
 *	More testing
 *
14
 * The changes are (c) Copyright 2004, Red Hat Inc. <alan@lxorguk.ukuu.org.uk>
L
Linus Torvalds 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
 * Additional fixes and clean up: Francois Romieu
 *
 * This source has not been verified for use in safety critical systems.
 *
 * Please direct queries about the revamped driver to the linux-kernel
 * list not VIA.
 *
 * Original code:
 *
 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
 * All rights reserved.
 *
 * This software may be redistributed and/or modified under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or
 * 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.
 *
 * Author: Chuang Liang-Shing, AJ Jiang
 *
 * Date: Jan 24, 2003
 *
 * MODULE_LICENSE("GPL");
 *
 */

#include <linux/module.h>
#include <linux/types.h>
J
Jiri Pirko 已提交
47
#include <linux/bitops.h>
L
Linus Torvalds 已提交
48
#include <linux/init.h>
49
#include <linux/dma-mapping.h>
L
Linus Torvalds 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63
#include <linux/mm.h>
#include <linux/errno.h>
#include <linux/ioport.h>
#include <linux/pci.h>
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/delay.h>
#include <linux/timer.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/string.h>
#include <linux/wait.h>
64
#include <linux/io.h>
L
Linus Torvalds 已提交
65
#include <linux/if.h>
66
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
67
#include <linux/proc_fs.h>
68 69 70
#include <linux/of_address.h>
#include <linux/of_device.h>
#include <linux/of_irq.h>
L
Linus Torvalds 已提交
71
#include <linux/inetdevice.h>
72
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
73 74 75 76 77
#include <linux/reboot.h>
#include <linux/ethtool.h>
#include <linux/mii.h>
#include <linux/in.h>
#include <linux/if_arp.h>
78
#include <linux/if_vlan.h>
L
Linus Torvalds 已提交
79 80 81 82 83 84 85 86
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/crc-ccitt.h>
#include <linux/crc32.h>

#include "via-velocity.h"

87 88 89 90
enum velocity_bus_type {
	BUS_PCI,
	BUS_PLATFORM,
};
L
Linus Torvalds 已提交
91

92
static int velocity_nics;
L
Linus Torvalds 已提交
93 94
static int msglevel = MSG_LEVEL_INFO;

95 96 97 98 99 100 101 102 103 104
static void velocity_set_power_state(struct velocity_info *vptr, char state)
{
	void *addr = vptr->mac_regs;

	if (vptr->pdev)
		pci_set_power_state(vptr->pdev, state);
	else
		writeb(state, addr + 0x154);
}

S
Stephen Hemminger 已提交
105 106 107 108 109 110 111 112
/**
 *	mac_get_cam_mask	-	Read a CAM mask
 *	@regs: register block for this velocity
 *	@mask: buffer to store mask
 *
 *	Fetch the mask bits of the selected CAM and store them into the
 *	provided mask buffer.
 */
113
static void mac_get_cam_mask(struct mac_regs __iomem *regs, u8 *mask)
S
Stephen Hemminger 已提交
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
{
	int i;

	/* Select CAM mask */
	BYTE_REG_BITS_SET(CAMCR_PS_CAM_MASK, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);

	writeb(0, &regs->CAMADDR);

	/* read mask */
	for (i = 0; i < 8; i++)
		*mask++ = readb(&(regs->MARCAM[i]));

	/* disable CAMEN */
	writeb(0, &regs->CAMADDR);

	/* Select mar */
	BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
}

/**
 *	mac_set_cam_mask	-	Set a CAM mask
 *	@regs: register block for this velocity
 *	@mask: CAM mask to load
 *
 *	Store a new mask into a CAM
 */
140
static void mac_set_cam_mask(struct mac_regs __iomem *regs, u8 *mask)
S
Stephen Hemminger 已提交
141 142 143 144 145 146 147
{
	int i;
	/* Select CAM mask */
	BYTE_REG_BITS_SET(CAMCR_PS_CAM_MASK, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);

	writeb(CAMADDR_CAMEN, &regs->CAMADDR);

148
	for (i = 0; i < 8; i++)
S
Stephen Hemminger 已提交
149
		writeb(*mask++, &(regs->MARCAM[i]));
150

S
Stephen Hemminger 已提交
151 152 153 154 155 156 157
	/* disable CAMEN */
	writeb(0, &regs->CAMADDR);

	/* Select mar */
	BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
}

158
static void mac_set_vlan_cam_mask(struct mac_regs __iomem *regs, u8 *mask)
S
Stephen Hemminger 已提交
159 160 161 162 163 164 165
{
	int i;
	/* Select CAM mask */
	BYTE_REG_BITS_SET(CAMCR_PS_CAM_MASK, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);

	writeb(CAMADDR_CAMEN | CAMADDR_VCAMSL, &regs->CAMADDR);

166
	for (i = 0; i < 8; i++)
S
Stephen Hemminger 已提交
167
		writeb(*mask++, &(regs->MARCAM[i]));
168

S
Stephen Hemminger 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
	/* disable CAMEN */
	writeb(0, &regs->CAMADDR);

	/* Select mar */
	BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
}

/**
 *	mac_set_cam	-	set CAM data
 *	@regs: register block of this velocity
 *	@idx: Cam index
 *	@addr: 2 or 6 bytes of CAM data
 *
 *	Load an address or vlan tag into a CAM
 */
184
static void mac_set_cam(struct mac_regs __iomem *regs, int idx, const u8 *addr)
S
Stephen Hemminger 已提交
185 186 187 188 189 190 191 192 193 194
{
	int i;

	/* Select CAM mask */
	BYTE_REG_BITS_SET(CAMCR_PS_CAM_DATA, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);

	idx &= (64 - 1);

	writeb(CAMADDR_CAMEN | idx, &regs->CAMADDR);

195
	for (i = 0; i < 6; i++)
S
Stephen Hemminger 已提交
196
		writeb(*addr++, &(regs->MARCAM[i]));
197

S
Stephen Hemminger 已提交
198 199 200 201 202 203 204 205 206 207
	BYTE_REG_BITS_ON(CAMCR_CAMWR, &regs->CAMCR);

	udelay(10);

	writeb(0, &regs->CAMADDR);

	/* Select mar */
	BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
}

208
static void mac_set_vlan_cam(struct mac_regs __iomem *regs, int idx,
S
Stephen Hemminger 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
			     const u8 *addr)
{

	/* Select CAM mask */
	BYTE_REG_BITS_SET(CAMCR_PS_CAM_DATA, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);

	idx &= (64 - 1);

	writeb(CAMADDR_CAMEN | CAMADDR_VCAMSL | idx, &regs->CAMADDR);
	writew(*((u16 *) addr), &regs->MARCAM[0]);

	BYTE_REG_BITS_ON(CAMCR_CAMWR, &regs->CAMCR);

	udelay(10);

	writeb(0, &regs->CAMADDR);

	/* Select mar */
	BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
}


/**
 *	mac_wol_reset	-	reset WOL after exiting low power
 *	@regs: register block of this velocity
 *
 *	Called after we drop out of wake on lan mode in order to
 *	reset the Wake on lan features. This function doesn't restore
 *	the rest of the logic from the result of sleep/wakeup
 */
239
static void mac_wol_reset(struct mac_regs __iomem *regs)
S
Stephen Hemminger 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
{

	/* Turn off SWPTAG right after leaving power mode */
	BYTE_REG_BITS_OFF(STICKHW_SWPTAG, &regs->STICKHW);
	/* clear sticky bits */
	BYTE_REG_BITS_OFF((STICKHW_DS1 | STICKHW_DS0), &regs->STICKHW);

	BYTE_REG_BITS_OFF(CHIPGCR_FCGMII, &regs->CHIPGCR);
	BYTE_REG_BITS_OFF(CHIPGCR_FCMODE, &regs->CHIPGCR);
	/* disable force PME-enable */
	writeb(WOLCFG_PMEOVR, &regs->WOLCFGClr);
	/* disable power-event config bit */
	writew(0xFFFF, &regs->WOLCRClr);
	/* clear power status */
	writew(0xFFFF, &regs->WOLSRClr);
}
L
Linus Torvalds 已提交
256

257
static const struct ethtool_ops velocity_ethtool_ops;
L
Linus Torvalds 已提交
258 259 260 261 262 263 264 265 266

/*
    Define module options
*/

MODULE_AUTHOR("VIA Networking Technologies, Inc.");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("VIA Networking Velocity Family Gigabit Ethernet Adapter Driver");

267 268
#define VELOCITY_PARAM(N, D) \
	static int N[MAX_UNITS] = OPTION_DEFAULT;\
L
Linus Torvalds 已提交
269
	module_param_array(N, int, NULL, 0); \
270
	MODULE_PARM_DESC(N, D);
L
Linus Torvalds 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294

#define RX_DESC_MIN     64
#define RX_DESC_MAX     255
#define RX_DESC_DEF     64
VELOCITY_PARAM(RxDescriptors, "Number of receive descriptors");

#define TX_DESC_MIN     16
#define TX_DESC_MAX     256
#define TX_DESC_DEF     64
VELOCITY_PARAM(TxDescriptors, "Number of transmit descriptors");

#define RX_THRESH_MIN   0
#define RX_THRESH_MAX   3
#define RX_THRESH_DEF   0
/* rx_thresh[] is used for controlling the receive fifo threshold.
   0: indicate the rxfifo threshold is 128 bytes.
   1: indicate the rxfifo threshold is 512 bytes.
   2: indicate the rxfifo threshold is 1024 bytes.
   3: indicate the rxfifo threshold is store & forward.
*/
VELOCITY_PARAM(rx_thresh, "Receive fifo threshold");

#define DMA_LENGTH_MIN  0
#define DMA_LENGTH_MAX  7
295
#define DMA_LENGTH_DEF  6
L
Linus Torvalds 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312

/* DMA_length[] is used for controlling the DMA length
   0: 8 DWORDs
   1: 16 DWORDs
   2: 32 DWORDs
   3: 64 DWORDs
   4: 128 DWORDs
   5: 256 DWORDs
   6: SF(flush till emply)
   7: SF(flush till emply)
*/
VELOCITY_PARAM(DMA_length, "DMA length");

#define IP_ALIG_DEF     0
/* IP_byte_align[] is used for IP header DWORD byte aligned
   0: indicate the IP header won't be DWORD byte aligned.(Default) .
   1: indicate the IP header will be DWORD byte aligned.
L
Lucas De Marchi 已提交
313
      In some environment, the IP header should be DWORD byte aligned,
L
Linus Torvalds 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
      or the packet will be droped when we receive it. (eg: IPVS)
*/
VELOCITY_PARAM(IP_byte_align, "Enable IP header dword aligned");

#define FLOW_CNTL_DEF   1
#define FLOW_CNTL_MIN   1
#define FLOW_CNTL_MAX   5

/* flow_control[] is used for setting the flow control ability of NIC.
   1: hardware deafult - AUTO (default). Use Hardware default value in ANAR.
   2: enable TX flow control.
   3: enable RX flow control.
   4: enable RX/TX flow control.
   5: disable
*/
VELOCITY_PARAM(flow_control, "Enable flow control ability");

#define MED_LNK_DEF 0
#define MED_LNK_MIN 0
333
#define MED_LNK_MAX 5
L
Linus Torvalds 已提交
334 335 336 337 338 339
/* speed_duplex[] is used for setting the speed and duplex mode of NIC.
   0: indicate autonegotiation for both speed and duplex mode
   1: indicate 100Mbps half duplex mode
   2: indicate 100Mbps full duplex mode
   3: indicate 10Mbps half duplex mode
   4: indicate 10Mbps full duplex mode
340
   5: indicate 1000Mbps full duplex mode
L
Linus Torvalds 已提交
341 342

   Note:
343 344
   if EEPROM have been set to the force mode, this option is ignored
   by driver.
L
Linus Torvalds 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
*/
VELOCITY_PARAM(speed_duplex, "Setting the speed and duplex mode");

#define WOL_OPT_DEF     0
#define WOL_OPT_MIN     0
#define WOL_OPT_MAX     7
/* wol_opts[] is used for controlling wake on lan behavior.
   0: Wake up if recevied a magic packet. (Default)
   1: Wake up if link status is on/off.
   2: Wake up if recevied an arp packet.
   4: Wake up if recevied any unicast packet.
   Those value can be sumed up to support more than one option.
*/
VELOCITY_PARAM(wol_opts, "Wake On Lan options");

static int rx_copybreak = 200;
module_param(rx_copybreak, int, 0644);
MODULE_PARM_DESC(rx_copybreak, "Copy breakpoint for copy-only-tiny-frames");

/*
 *	Internal board variants. At the moment we have only one
 */
A
Andrew Morton 已提交
367
static struct velocity_info_tbl chip_info_table[] = {
368 369
	{CHIP_TYPE_VT6110, "VIA Networking Velocity Family Gigabit Ethernet Adapter", 1, 0x00FFFFFFUL},
	{ }
L
Linus Torvalds 已提交
370 371 372 373 374 375
};

/*
 *	Describe the PCI device identifiers that we support in this
 *	device driver. Used for hotplug autoloading.
 */
376

377
static const struct pci_device_id velocity_pci_id_table[] = {
378 379
	{ PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_612X) },
	{ }
L
Linus Torvalds 已提交
380 381
};

382 383 384 385 386 387
MODULE_DEVICE_TABLE(pci, velocity_pci_id_table);

/**
 *	Describe the OF device identifiers that we support in this
 *	device driver. Used for devicetree nodes.
 */
388
static const struct of_device_id velocity_of_ids[] = {
389 390 391 392
	{ .compatible = "via,velocity-vt6110", .data = &chip_info_table[0] },
	{ /* Sentinel */ },
};
MODULE_DEVICE_TABLE(of, velocity_of_ids);
L
Linus Torvalds 已提交
393 394 395 396 397 398 399 400

/**
 *	get_chip_name	- 	identifier to name
 *	@id: chip identifier
 *
 *	Given a chip identifier return a suitable description. Returns
 *	a pointer a static string valid while the driver is loaded.
 */
401
static const char *get_chip_name(enum chip_type chip_id)
L
Linus Torvalds 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
{
	int i;
	for (i = 0; chip_info_table[i].name != NULL; i++)
		if (chip_info_table[i].chip_id == chip_id)
			break;
	return chip_info_table[i].name;
}

/**
 *	velocity_set_int_opt	-	parser for integer options
 *	@opt: pointer to option value
 *	@val: value the user requested (or -1 for default)
 *	@min: lowest value allowed
 *	@max: highest value allowed
 *	@def: default value
 *	@name: property name
 *	@dev: device name
 *
 *	Set an integer property in the module options. This function does
 *	all the verification and checking as well as reporting so that
 *	we don't duplicate code for each option.
 */
424 425
static void velocity_set_int_opt(int *opt, int val, int min, int max, int def,
				 char *name, const char *devname)
L
Linus Torvalds 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
{
	if (val == -1)
		*opt = def;
	else if (val < min || val > max) {
		VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: the value of parameter %s is invalid, the valid range is (%d-%d)\n",
					devname, name, min, max);
		*opt = def;
	} else {
		VELOCITY_PRT(MSG_LEVEL_INFO, KERN_INFO "%s: set value of parameter %s to %d\n",
					devname, name, val);
		*opt = val;
	}
}

/**
 *	velocity_set_bool_opt	-	parser for boolean options
 *	@opt: pointer to option value
 *	@val: value the user requested (or -1 for default)
 *	@def: default value (yes/no)
 *	@flag: numeric value to set for true.
 *	@name: property name
 *	@dev: device name
 *
 *	Set a boolean property in the module options. This function does
 *	all the verification and checking as well as reporting so that
 *	we don't duplicate code for each option.
 */
453 454
static void velocity_set_bool_opt(u32 *opt, int val, int def, u32 flag,
				  char *name, const char *devname)
L
Linus Torvalds 已提交
455 456 457 458 459
{
	(*opt) &= (~flag);
	if (val == -1)
		*opt |= (def ? flag : 0);
	else if (val < 0 || val > 1) {
460
		printk(KERN_NOTICE "%s: the value of parameter %s is invalid, the valid range is (0-1)\n",
L
Linus Torvalds 已提交
461 462 463
			devname, name);
		*opt |= (def ? flag : 0);
	} else {
464
		printk(KERN_INFO "%s: set parameter %s to %s\n",
L
Linus Torvalds 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477 478
			devname, name, val ? "TRUE" : "FALSE");
		*opt |= (val ? flag : 0);
	}
}

/**
 *	velocity_get_options	-	set options on device
 *	@opts: option structure for the device
 *	@index: index of option to use in module options array
 *	@devname: device name
 *
 *	Turn the module and command options into a single structure
 *	for the current device
 */
479 480
static void velocity_get_options(struct velocity_opt *opts, int index,
				 const char *devname)
L
Linus Torvalds 已提交
481 482 483 484 485 486
{

	velocity_set_int_opt(&opts->rx_thresh, rx_thresh[index], RX_THRESH_MIN, RX_THRESH_MAX, RX_THRESH_DEF, "rx_thresh", devname);
	velocity_set_int_opt(&opts->DMA_length, DMA_length[index], DMA_LENGTH_MIN, DMA_LENGTH_MAX, DMA_LENGTH_DEF, "DMA_length", devname);
	velocity_set_int_opt(&opts->numrx, RxDescriptors[index], RX_DESC_MIN, RX_DESC_MAX, RX_DESC_DEF, "RxDescriptors", devname);
	velocity_set_int_opt(&opts->numtx, TxDescriptors[index], TX_DESC_MIN, TX_DESC_MAX, TX_DESC_DEF, "TxDescriptors", devname);
487

L
Linus Torvalds 已提交
488 489 490
	velocity_set_int_opt(&opts->flow_cntl, flow_control[index], FLOW_CNTL_MIN, FLOW_CNTL_MAX, FLOW_CNTL_DEF, "flow_control", devname);
	velocity_set_bool_opt(&opts->flags, IP_byte_align[index], IP_ALIG_DEF, VELOCITY_FLAGS_IP_ALIGN, "IP_byte_align", devname);
	velocity_set_int_opt((int *) &opts->spd_dpx, speed_duplex[index], MED_LNK_MIN, MED_LNK_MAX, MED_LNK_DEF, "Media link mode", devname);
491
	velocity_set_int_opt(&opts->wol_opts, wol_opts[index], WOL_OPT_MIN, WOL_OPT_MAX, WOL_OPT_DEF, "Wake On Lan options", devname);
L
Linus Torvalds 已提交
492 493 494 495 496 497 498 499 500 501 502 503
	opts->numrx = (opts->numrx & ~3);
}

/**
 *	velocity_init_cam_filter	-	initialise CAM
 *	@vptr: velocity to program
 *
 *	Initialize the content addressable memory used for filters. Load
 *	appropriately according to the presence of VLAN
 */
static void velocity_init_cam_filter(struct velocity_info *vptr)
{
504
	struct mac_regs __iomem *regs = vptr->mac_regs;
J
Jiri Pirko 已提交
505
	unsigned int vid, i = 0;
L
Linus Torvalds 已提交
506 507 508 509 510 511 512 513

	/* Turn on MCFG_PQEN, turn off MCFG_RTGOPT */
	WORD_REG_BITS_SET(MCFG_PQEN, MCFG_RTGOPT, &regs->MCFG);
	WORD_REG_BITS_ON(MCFG_VIDFR, &regs->MCFG);

	/* Disable all CAMs */
	memset(vptr->vCAMmask, 0, sizeof(u8) * 8);
	memset(vptr->mCAMmask, 0, sizeof(u8) * 8);
S
Stephen Hemminger 已提交
514 515
	mac_set_vlan_cam_mask(regs, vptr->vCAMmask);
	mac_set_cam_mask(regs, vptr->mCAMmask);
L
Linus Torvalds 已提交
516

F
Francois Romieu 已提交
517
	/* Enable VCAMs */
J
Jiri Pirko 已提交
518 519 520 521 522 523 524
	for_each_set_bit(vid, vptr->active_vlans, VLAN_N_VID) {
		mac_set_vlan_cam(regs, i, (u8 *) &vid);
		vptr->vCAMmask[i / 8] |= 0x1 << (i % 8);
		if (++i >= VCAM_SIZE)
			break;
	}
	mac_set_vlan_cam_mask(regs, vptr->vCAMmask);
F
Francois Romieu 已提交
525 526
}

527 528
static int velocity_vlan_rx_add_vid(struct net_device *dev,
				    __be16 proto, u16 vid)
529 530 531
{
	struct velocity_info *vptr = netdev_priv(dev);

532
	spin_lock_irq(&vptr->lock);
J
Jiri Pirko 已提交
533
	set_bit(vid, vptr->active_vlans);
534
	velocity_init_cam_filter(vptr);
535
	spin_unlock_irq(&vptr->lock);
536
	return 0;
537 538
}

539 540
static int velocity_vlan_rx_kill_vid(struct net_device *dev,
				     __be16 proto, u16 vid)
541 542 543
{
	struct velocity_info *vptr = netdev_priv(dev);

544
	spin_lock_irq(&vptr->lock);
J
Jiri Pirko 已提交
545
	clear_bit(vid, vptr->active_vlans);
546
	velocity_init_cam_filter(vptr);
547
	spin_unlock_irq(&vptr->lock);
548
	return 0;
549 550
}

551 552 553 554
static void velocity_init_rx_ring_indexes(struct velocity_info *vptr)
{
	vptr->rx.dirty = vptr->rx.filled = vptr->rx.curr = 0;
}
555

L
Linus Torvalds 已提交
556 557 558 559 560 561 562 563 564 565
/**
 *	velocity_rx_reset	-	handle a receive reset
 *	@vptr: velocity we are resetting
 *
 *	Reset the ownership and status for the receive ring side.
 *	Hand all the receive queue to the NIC.
 */
static void velocity_rx_reset(struct velocity_info *vptr)
{

566
	struct mac_regs __iomem *regs = vptr->mac_regs;
L
Linus Torvalds 已提交
567 568
	int i;

569
	velocity_init_rx_ring_indexes(vptr);
L
Linus Torvalds 已提交
570 571 572 573 574

	/*
	 *	Init state, all RD entries belong to the NIC
	 */
	for (i = 0; i < vptr->options.numrx; ++i)
575
		vptr->rx.ring[i].rdesc0.len |= OWNED_BY_NIC;
L
Linus Torvalds 已提交
576 577

	writew(vptr->options.numrx, &regs->RBRDU);
578
	writel(vptr->rx.pool_dma, &regs->RDBaseLo);
L
Linus Torvalds 已提交
579 580 581 582 583
	writew(0, &regs->RDIdx);
	writew(vptr->options.numrx - 1, &regs->RDCSize);
}

/**
584 585
 *	velocity_get_opt_media_mode	-	get media selection
 *	@vptr: velocity adapter
L
Linus Torvalds 已提交
586
 *
587 588 589
 *	Get the media mode stored in EEPROM or module options and load
 *	mii_status accordingly. The requested link state information
 *	is also returned.
L
Linus Torvalds 已提交
590
 */
591
static u32 velocity_get_opt_media_mode(struct velocity_info *vptr)
L
Linus Torvalds 已提交
592
{
593
	u32 status = 0;
L
Linus Torvalds 已提交
594

595 596 597
	switch (vptr->options.spd_dpx) {
	case SPD_DPX_AUTO:
		status = VELOCITY_AUTONEG_ENABLE;
L
Linus Torvalds 已提交
598
		break;
599 600 601 602 603 604 605 606 607 608 609 610
	case SPD_DPX_100_FULL:
		status = VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL;
		break;
	case SPD_DPX_10_FULL:
		status = VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL;
		break;
	case SPD_DPX_100_HALF:
		status = VELOCITY_SPEED_100;
		break;
	case SPD_DPX_10_HALF:
		status = VELOCITY_SPEED_10;
		break;
611 612 613
	case SPD_DPX_1000_FULL:
		status = VELOCITY_SPEED_1000 | VELOCITY_DUPLEX_FULL;
		break;
614 615 616 617
	}
	vptr->mii_status = status;
	return status;
}
L
Linus Torvalds 已提交
618

619 620 621 622 623 624 625 626 627
/**
 *	safe_disable_mii_autopoll	-	autopoll off
 *	@regs: velocity registers
 *
 *	Turn off the autopoll and wait for it to disable on the chip
 */
static void safe_disable_mii_autopoll(struct mac_regs __iomem *regs)
{
	u16 ww;
L
Linus Torvalds 已提交
628

629 630 631 632 633 634 635 636
	/*  turn off MAUTO */
	writeb(0, &regs->MIICR);
	for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
		udelay(1);
		if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
			break;
	}
}
L
Linus Torvalds 已提交
637

638 639 640 641 642 643 644 645 646 647
/**
 *	enable_mii_autopoll	-	turn on autopolling
 *	@regs: velocity registers
 *
 *	Enable the MII link status autopoll feature on the Velocity
 *	hardware. Wait for it to enable.
 */
static void enable_mii_autopoll(struct mac_regs __iomem *regs)
{
	int ii;
L
Linus Torvalds 已提交
648

649 650
	writeb(0, &(regs->MIICR));
	writeb(MIIADR_SWMPL, &regs->MIIADR);
L
Linus Torvalds 已提交
651

652 653 654 655 656
	for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
		udelay(1);
		if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
			break;
	}
L
Linus Torvalds 已提交
657

658
	writeb(MIICR_MAUTO, &regs->MIICR);
L
Linus Torvalds 已提交
659

660 661 662 663 664
	for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
		udelay(1);
		if (!BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
			break;
	}
L
Linus Torvalds 已提交
665

666
}
L
Linus Torvalds 已提交
667

668 669 670 671 672 673 674 675 676 677 678 679
/**
 *	velocity_mii_read	-	read MII data
 *	@regs: velocity registers
 *	@index: MII register index
 *	@data: buffer for received data
 *
 *	Perform a single read of an MII 16bit register. Returns zero
 *	on success or -ETIMEDOUT if the PHY did not respond.
 */
static int velocity_mii_read(struct mac_regs __iomem *regs, u8 index, u16 *data)
{
	u16 ww;
L
Linus Torvalds 已提交
680

681 682 683 684
	/*
	 *	Disable MIICR_MAUTO, so that mii addr can be set normally
	 */
	safe_disable_mii_autopoll(regs);
L
Linus Torvalds 已提交
685

686
	writeb(index, &regs->MIIADR);
L
Linus Torvalds 已提交
687

688
	BYTE_REG_BITS_ON(MIICR_RCMD, &regs->MIICR);
L
Linus Torvalds 已提交
689

690 691 692 693
	for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
		if (!(readb(&regs->MIICR) & MIICR_RCMD))
			break;
	}
L
Linus Torvalds 已提交
694

695
	*data = readw(&regs->MIIDATA);
L
Linus Torvalds 已提交
696

697 698 699 700
	enable_mii_autopoll(regs);
	if (ww == W_MAX_TIMEOUT)
		return -ETIMEDOUT;
	return 0;
L
Linus Torvalds 已提交
701 702 703
}

/**
704 705
 *	mii_check_media_mode	-	check media state
 *	@regs: velocity registers
L
Linus Torvalds 已提交
706
 *
707 708
 *	Check the current MII status and determine the link status
 *	accordingly
L
Linus Torvalds 已提交
709
 */
710
static u32 mii_check_media_mode(struct mac_regs __iomem *regs)
L
Linus Torvalds 已提交
711
{
712 713
	u32 status = 0;
	u16 ANAR;
L
Linus Torvalds 已提交
714

715
	if (!MII_REG_BITS_IS_ON(BMSR_LSTATUS, MII_BMSR, regs))
716
		status |= VELOCITY_LINK_FAIL;
L
Linus Torvalds 已提交
717

718
	if (MII_REG_BITS_IS_ON(ADVERTISE_1000FULL, MII_CTRL1000, regs))
719
		status |= VELOCITY_SPEED_1000 | VELOCITY_DUPLEX_FULL;
720
	else if (MII_REG_BITS_IS_ON(ADVERTISE_1000HALF, MII_CTRL1000, regs))
721 722
		status |= (VELOCITY_SPEED_1000);
	else {
723 724
		velocity_mii_read(regs, MII_ADVERTISE, &ANAR);
		if (ANAR & ADVERTISE_100FULL)
725
			status |= (VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL);
726
		else if (ANAR & ADVERTISE_100HALF)
727
			status |= VELOCITY_SPEED_100;
728
		else if (ANAR & ADVERTISE_10FULL)
729 730 731
			status |= (VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL);
		else
			status |= (VELOCITY_SPEED_10);
L
Linus Torvalds 已提交
732 733
	}

734 735 736 737 738
	if (MII_REG_BITS_IS_ON(BMCR_ANENABLE, MII_BMCR, regs)) {
		velocity_mii_read(regs, MII_ADVERTISE, &ANAR);
		if ((ANAR & (ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF))
		    == (ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF)) {
			if (MII_REG_BITS_IS_ON(ADVERTISE_1000HALF | ADVERTISE_1000FULL, MII_CTRL1000, regs))
739 740
				status |= VELOCITY_AUTONEG_ENABLE;
		}
L
Linus Torvalds 已提交
741 742
	}

743 744
	return status;
}
745

L
Linus Torvalds 已提交
746
/**
747 748 749 750
 *	velocity_mii_write	-	write MII data
 *	@regs: velocity registers
 *	@index: MII register index
 *	@data: 16bit data for the MII register
L
Linus Torvalds 已提交
751
 *
752 753
 *	Perform a single write to an MII 16bit register. Returns zero
 *	on success or -ETIMEDOUT if the PHY did not respond.
L
Linus Torvalds 已提交
754
 */
755
static int velocity_mii_write(struct mac_regs __iomem *regs, u8 mii_addr, u16 data)
L
Linus Torvalds 已提交
756
{
757
	u16 ww;
L
Linus Torvalds 已提交
758

759 760
	/*
	 *	Disable MIICR_MAUTO, so that mii addr can be set normally
761
	 */
762
	safe_disable_mii_autopoll(regs);
L
Linus Torvalds 已提交
763

764 765 766 767 768 769 770 771 772 773 774 775 776
	/* MII reg offset */
	writeb(mii_addr, &regs->MIIADR);
	/* set MII data */
	writew(data, &regs->MIIDATA);

	/* turn on MIICR_WCMD */
	BYTE_REG_BITS_ON(MIICR_WCMD, &regs->MIICR);

	/* W_MAX_TIMEOUT is the timeout period */
	for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
		udelay(5);
		if (!(readb(&regs->MIICR) & MIICR_WCMD))
			break;
L
Linus Torvalds 已提交
777
	}
778
	enable_mii_autopoll(regs);
779

780 781 782 783
	if (ww == W_MAX_TIMEOUT)
		return -ETIMEDOUT;
	return 0;
}
784

785 786 787 788 789 790 791 792 793 794 795 796
/**
 *	set_mii_flow_control	-	flow control setup
 *	@vptr: velocity interface
 *
 *	Set up the flow control on this interface according to
 *	the supplied user/eeprom options.
 */
static void set_mii_flow_control(struct velocity_info *vptr)
{
	/*Enable or Disable PAUSE in ANAR */
	switch (vptr->options.flow_cntl) {
	case FLOW_CNTL_TX:
797 798
		MII_REG_BITS_OFF(ADVERTISE_PAUSE_CAP, MII_ADVERTISE, vptr->mac_regs);
		MII_REG_BITS_ON(ADVERTISE_PAUSE_ASYM, MII_ADVERTISE, vptr->mac_regs);
799
		break;
L
Linus Torvalds 已提交
800

801
	case FLOW_CNTL_RX:
802 803
		MII_REG_BITS_ON(ADVERTISE_PAUSE_CAP, MII_ADVERTISE, vptr->mac_regs);
		MII_REG_BITS_ON(ADVERTISE_PAUSE_ASYM, MII_ADVERTISE, vptr->mac_regs);
804
		break;
L
Linus Torvalds 已提交
805

806
	case FLOW_CNTL_TX_RX:
807
		MII_REG_BITS_ON(ADVERTISE_PAUSE_CAP, MII_ADVERTISE, vptr->mac_regs);
808
		MII_REG_BITS_OFF(ADVERTISE_PAUSE_ASYM, MII_ADVERTISE, vptr->mac_regs);
809
		break;
L
Linus Torvalds 已提交
810

811
	case FLOW_CNTL_DISABLE:
812 813
		MII_REG_BITS_OFF(ADVERTISE_PAUSE_CAP, MII_ADVERTISE, vptr->mac_regs);
		MII_REG_BITS_OFF(ADVERTISE_PAUSE_ASYM, MII_ADVERTISE, vptr->mac_regs);
814 815 816 817 818
		break;
	default:
		break;
	}
}
L
Linus Torvalds 已提交
819

820 821 822 823 824 825 826 827
/**
 *	mii_set_auto_on		-	autonegotiate on
 *	@vptr: velocity
 *
 *	Enable autonegotation on this interface
 */
static void mii_set_auto_on(struct velocity_info *vptr)
{
828 829
	if (MII_REG_BITS_IS_ON(BMCR_ANENABLE, MII_BMCR, vptr->mac_regs))
		MII_REG_BITS_ON(BMCR_ANRESTART, MII_BMCR, vptr->mac_regs);
830
	else
831
		MII_REG_BITS_ON(BMCR_ANENABLE, MII_BMCR, vptr->mac_regs);
832
}
L
Linus Torvalds 已提交
833

834 835 836 837 838 839
static u32 check_connection_type(struct mac_regs __iomem *regs)
{
	u32 status = 0;
	u8 PHYSR0;
	u16 ANAR;
	PHYSR0 = readb(&regs->PHYSR0);
L
Linus Torvalds 已提交
840

841 842 843 844
	/*
	   if (!(PHYSR0 & PHYSR0_LINKGD))
	   status|=VELOCITY_LINK_FAIL;
	 */
L
Linus Torvalds 已提交
845

846 847
	if (PHYSR0 & PHYSR0_FDPX)
		status |= VELOCITY_DUPLEX_FULL;
L
Linus Torvalds 已提交
848

849 850 851 852 853 854
	if (PHYSR0 & PHYSR0_SPDG)
		status |= VELOCITY_SPEED_1000;
	else if (PHYSR0 & PHYSR0_SPD10)
		status |= VELOCITY_SPEED_10;
	else
		status |= VELOCITY_SPEED_100;
L
Linus Torvalds 已提交
855

856 857 858 859 860
	if (MII_REG_BITS_IS_ON(BMCR_ANENABLE, MII_BMCR, regs)) {
		velocity_mii_read(regs, MII_ADVERTISE, &ANAR);
		if ((ANAR & (ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF))
		    == (ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF)) {
			if (MII_REG_BITS_IS_ON(ADVERTISE_1000HALF | ADVERTISE_1000FULL, MII_CTRL1000, regs))
861 862
				status |= VELOCITY_AUTONEG_ENABLE;
		}
L
Linus Torvalds 已提交
863 864
	}

865 866
	return status;
}
L
Linus Torvalds 已提交
867

868 869 870 871 872 873 874 875 876 877 878 879
/**
 *	velocity_set_media_mode		-	set media mode
 *	@mii_status: old MII link state
 *
 *	Check the media link state and configure the flow control
 *	PHY and also velocity hardware setup accordingly. In particular
 *	we need to set up CD polling and frame bursting.
 */
static int velocity_set_media_mode(struct velocity_info *vptr, u32 mii_status)
{
	u32 curr_status;
	struct mac_regs __iomem *regs = vptr->mac_regs;
L
Linus Torvalds 已提交
880

881 882
	vptr->mii_status = mii_check_media_mode(vptr->mac_regs);
	curr_status = vptr->mii_status & (~VELOCITY_LINK_FAIL);
883

884 885
	/* Set mii link status */
	set_mii_flow_control(vptr);
L
Linus Torvalds 已提交
886

887
	/*
888
	   Check if new status is consistent with current status
889 890
	   if (((mii_status & curr_status) & VELOCITY_AUTONEG_ENABLE) ||
	       (mii_status==curr_status)) {
891 892 893 894 895
	   vptr->mii_status=mii_check_media_mode(vptr->mac_regs);
	   vptr->mii_status=check_connection_type(vptr->mac_regs);
	   VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity link no change\n");
	   return 0;
	   }
L
Linus Torvalds 已提交
896
	 */
897

898
	if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201)
899
		MII_REG_BITS_ON(AUXCR_MDPPS, MII_NCONFIG, vptr->mac_regs);
L
Linus Torvalds 已提交
900 901

	/*
902
	 *	If connection type is AUTO
L
Linus Torvalds 已提交
903
	 */
904 905 906 907 908
	if (mii_status & VELOCITY_AUTONEG_ENABLE) {
		VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity is AUTO mode\n");
		/* clear force MAC mode bit */
		BYTE_REG_BITS_OFF(CHIPGCR_FCMODE, &regs->CHIPGCR);
		/* set duplex mode of MAC according to duplex mode of MII */
909 910 911
		MII_REG_BITS_ON(ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF, MII_ADVERTISE, vptr->mac_regs);
		MII_REG_BITS_ON(ADVERTISE_1000FULL | ADVERTISE_1000HALF, MII_CTRL1000, vptr->mac_regs);
		MII_REG_BITS_ON(BMCR_SPEED1000, MII_BMCR, vptr->mac_regs);
912

913 914 915
		/* enable AUTO-NEGO mode */
		mii_set_auto_on(vptr);
	} else {
916
		u16 CTRL1000;
917 918
		u16 ANAR;
		u8 CHIPGCR;
L
Linus Torvalds 已提交
919

920 921 922 923 924 925
		/*
		 * 1. if it's 3119, disable frame bursting in halfduplex mode
		 *    and enable it in fullduplex mode
		 * 2. set correct MII/GMII and half/full duplex mode in CHIPGCR
		 * 3. only enable CD heart beat counter in 10HD mode
		 */
L
Linus Torvalds 已提交
926

927 928
		/* set force MAC mode bit */
		BYTE_REG_BITS_ON(CHIPGCR_FCMODE, &regs->CHIPGCR);
L
Linus Torvalds 已提交
929

930
		CHIPGCR = readb(&regs->CHIPGCR);
931 932 933 934 935

		if (mii_status & VELOCITY_SPEED_1000)
			CHIPGCR |= CHIPGCR_FCGMII;
		else
			CHIPGCR &= ~CHIPGCR_FCGMII;
936

937 938 939 940 941 942 943 944 945 946 947 948 949
		if (mii_status & VELOCITY_DUPLEX_FULL) {
			CHIPGCR |= CHIPGCR_FCFDX;
			writeb(CHIPGCR, &regs->CHIPGCR);
			VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced full mode\n");
			if (vptr->rev_id < REV_ID_VT3216_A0)
				BYTE_REG_BITS_OFF(TCR_TB2BDIS, &regs->TCR);
		} else {
			CHIPGCR &= ~CHIPGCR_FCFDX;
			VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced half mode\n");
			writeb(CHIPGCR, &regs->CHIPGCR);
			if (vptr->rev_id < REV_ID_VT3216_A0)
				BYTE_REG_BITS_ON(TCR_TB2BDIS, &regs->TCR);
		}
L
Linus Torvalds 已提交
950

951 952 953 954 955 956 957
		velocity_mii_read(vptr->mac_regs, MII_CTRL1000, &CTRL1000);
		CTRL1000 &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
		if ((mii_status & VELOCITY_SPEED_1000) &&
		    (mii_status & VELOCITY_DUPLEX_FULL)) {
			CTRL1000 |= ADVERTISE_1000FULL;
		}
		velocity_mii_write(vptr->mac_regs, MII_CTRL1000, CTRL1000);
L
Linus Torvalds 已提交
958

959 960 961 962
		if (!(mii_status & VELOCITY_DUPLEX_FULL) && (mii_status & VELOCITY_SPEED_10))
			BYTE_REG_BITS_OFF(TESTCFG_HBDIS, &regs->TESTCFG);
		else
			BYTE_REG_BITS_ON(TESTCFG_HBDIS, &regs->TESTCFG);
L
Linus Torvalds 已提交
963

964 965 966
		/* MII_REG_BITS_OFF(BMCR_SPEED1000, MII_BMCR, vptr->mac_regs); */
		velocity_mii_read(vptr->mac_regs, MII_ADVERTISE, &ANAR);
		ANAR &= (~(ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF));
967 968
		if (mii_status & VELOCITY_SPEED_100) {
			if (mii_status & VELOCITY_DUPLEX_FULL)
969
				ANAR |= ADVERTISE_100FULL;
970
			else
971
				ANAR |= ADVERTISE_100HALF;
972
		} else if (mii_status & VELOCITY_SPEED_10) {
973
			if (mii_status & VELOCITY_DUPLEX_FULL)
974
				ANAR |= ADVERTISE_10FULL;
975
			else
976
				ANAR |= ADVERTISE_10HALF;
977
		}
978
		velocity_mii_write(vptr->mac_regs, MII_ADVERTISE, ANAR);
979 980
		/* enable AUTO-NEGO mode */
		mii_set_auto_on(vptr);
981
		/* MII_REG_BITS_ON(BMCR_ANENABLE, MII_BMCR, vptr->mac_regs); */
982
	}
983 984 985 986
	/* vptr->mii_status=mii_check_media_mode(vptr->mac_regs); */
	/* vptr->mii_status=check_connection_type(vptr->mac_regs); */
	return VELOCITY_LINK_CHANGE;
}
987

988 989 990 991 992 993 994 995 996 997
/**
 *	velocity_print_link_status	-	link status reporting
 *	@vptr: velocity to report on
 *
 *	Turn the link status of the velocity card into a kernel log
 *	description of the new link state, detailing speed and duplex
 *	status
 */
static void velocity_print_link_status(struct velocity_info *vptr)
{
998

999
	if (vptr->mii_status & VELOCITY_LINK_FAIL) {
1000
		VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: failed to detect cable link\n", vptr->netdev->name);
1001
	} else if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
1002
		VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link auto-negotiation", vptr->netdev->name);
1003

1004 1005 1006 1007 1008 1009
		if (vptr->mii_status & VELOCITY_SPEED_1000)
			VELOCITY_PRT(MSG_LEVEL_INFO, " speed 1000M bps");
		else if (vptr->mii_status & VELOCITY_SPEED_100)
			VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps");
		else
			VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps");
L
Linus Torvalds 已提交
1010

1011 1012 1013 1014 1015
		if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
			VELOCITY_PRT(MSG_LEVEL_INFO, " full duplex\n");
		else
			VELOCITY_PRT(MSG_LEVEL_INFO, " half duplex\n");
	} else {
1016
		VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link forced", vptr->netdev->name);
1017
		switch (vptr->options.spd_dpx) {
1018 1019 1020
		case SPD_DPX_1000_FULL:
			VELOCITY_PRT(MSG_LEVEL_INFO, " speed 1000M bps full duplex\n");
			break;
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
		case SPD_DPX_100_HALF:
			VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps half duplex\n");
			break;
		case SPD_DPX_100_FULL:
			VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps full duplex\n");
			break;
		case SPD_DPX_10_HALF:
			VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps half duplex\n");
			break;
		case SPD_DPX_10_FULL:
			VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps full duplex\n");
			break;
		default:
			break;
		}
L
Linus Torvalds 已提交
1036 1037 1038 1039
	}
}

/**
1040 1041
 *	enable_flow_control_ability	-	flow control
 *	@vptr: veloity to configure
L
Linus Torvalds 已提交
1042
 *
1043 1044
 *	Set up flow control according to the flow control options
 *	determined by the eeprom/configuration.
L
Linus Torvalds 已提交
1045
 */
1046
static void enable_flow_control_ability(struct velocity_info *vptr)
L
Linus Torvalds 已提交
1047 1048
{

1049
	struct mac_regs __iomem *regs = vptr->mac_regs;
L
Linus Torvalds 已提交
1050

1051
	switch (vptr->options.flow_cntl) {
L
Linus Torvalds 已提交
1052

1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
	case FLOW_CNTL_DEFAULT:
		if (BYTE_REG_BITS_IS_ON(PHYSR0_RXFLC, &regs->PHYSR0))
			writel(CR0_FDXRFCEN, &regs->CR0Set);
		else
			writel(CR0_FDXRFCEN, &regs->CR0Clr);

		if (BYTE_REG_BITS_IS_ON(PHYSR0_TXFLC, &regs->PHYSR0))
			writel(CR0_FDXTFCEN, &regs->CR0Set);
		else
			writel(CR0_FDXTFCEN, &regs->CR0Clr);
		break;

	case FLOW_CNTL_TX:
		writel(CR0_FDXTFCEN, &regs->CR0Set);
		writel(CR0_FDXRFCEN, &regs->CR0Clr);
		break;

	case FLOW_CNTL_RX:
		writel(CR0_FDXRFCEN, &regs->CR0Set);
		writel(CR0_FDXTFCEN, &regs->CR0Clr);
		break;

	case FLOW_CNTL_TX_RX:
		writel(CR0_FDXTFCEN, &regs->CR0Set);
		writel(CR0_FDXRFCEN, &regs->CR0Set);
		break;

	case FLOW_CNTL_DISABLE:
		writel(CR0_FDXRFCEN, &regs->CR0Clr);
		writel(CR0_FDXTFCEN, &regs->CR0Clr);
		break;

	default:
		break;
	}
L
Linus Torvalds 已提交
1088 1089 1090 1091

}

/**
1092 1093
 *	velocity_soft_reset	-	soft reset
 *	@vptr: velocity to reset
L
Linus Torvalds 已提交
1094
 *
1095 1096
 *	Kick off a soft reset of the velocity adapter and then poll
 *	until the reset sequence has completed before returning.
L
Linus Torvalds 已提交
1097
 */
1098
static int velocity_soft_reset(struct velocity_info *vptr)
L
Linus Torvalds 已提交
1099
{
1100 1101
	struct mac_regs __iomem *regs = vptr->mac_regs;
	int i = 0;
1102

1103
	writel(CR0_SFRST, &regs->CR0Set);
L
Linus Torvalds 已提交
1104

1105 1106 1107 1108
	for (i = 0; i < W_MAX_TIMEOUT; i++) {
		udelay(5);
		if (!DWORD_REG_BITS_IS_ON(CR0_SFRST, &regs->CR0Set))
			break;
L
Linus Torvalds 已提交
1109 1110
	}

1111 1112 1113 1114 1115
	if (i == W_MAX_TIMEOUT) {
		writel(CR0_FORSRST, &regs->CR0Set);
		/* FIXME: PCI POSTING */
		/* delay 2ms */
		mdelay(2);
L
Linus Torvalds 已提交
1116 1117 1118 1119 1120
	}
	return 0;
}

/**
1121 1122
 *	velocity_set_multi	-	filter list change callback
 *	@dev: network device
L
Linus Torvalds 已提交
1123
 *
1124 1125 1126
 *	Called by the network layer when the filter lists need to change
 *	for a velocity adapter. Reload the CAMs with the new address
 *	filter ruleset.
L
Linus Torvalds 已提交
1127
 */
1128
static void velocity_set_multi(struct net_device *dev)
L
Linus Torvalds 已提交
1129
{
1130
	struct velocity_info *vptr = netdev_priv(dev);
L
Linus Torvalds 已提交
1131
	struct mac_regs __iomem *regs = vptr->mac_regs;
1132 1133
	u8 rx_mode;
	int i;
1134
	struct netdev_hw_addr *ha;
L
Linus Torvalds 已提交
1135

1136 1137 1138 1139
	if (dev->flags & IFF_PROMISC) {	/* Set promiscuous. */
		writel(0xffffffff, &regs->MARCAM[0]);
		writel(0xffffffff, &regs->MARCAM[4]);
		rx_mode = (RCR_AM | RCR_AB | RCR_PROM);
1140
	} else if ((netdev_mc_count(dev) > vptr->multicast_limit) ||
1141
		   (dev->flags & IFF_ALLMULTI)) {
1142 1143 1144 1145 1146 1147
		writel(0xffffffff, &regs->MARCAM[0]);
		writel(0xffffffff, &regs->MARCAM[4]);
		rx_mode = (RCR_AM | RCR_AB);
	} else {
		int offset = MCAM_SIZE - vptr->multicast_limit;
		mac_get_cam_mask(regs, vptr->mCAMmask);
L
Linus Torvalds 已提交
1148

1149
		i = 0;
1150 1151
		netdev_for_each_mc_addr(ha, dev) {
			mac_set_cam(regs, i + offset, ha->addr);
1152
			vptr->mCAMmask[(offset + i) / 8] |= 1 << ((offset + i) & 7);
1153
			i++;
L
Linus Torvalds 已提交
1154 1155
		}

1156 1157
		mac_set_cam_mask(regs, vptr->mCAMmask);
		rx_mode = RCR_AM | RCR_AB | RCR_AP;
L
Linus Torvalds 已提交
1158
	}
1159 1160
	if (dev->mtu > 1500)
		rx_mode |= RCR_AL;
L
Linus Torvalds 已提交
1161

1162
	BYTE_REG_BITS_ON(rx_mode, &regs->RCR);
L
Linus Torvalds 已提交
1163

1164 1165
}

1166 1167
/*
 * MII access , media link mode setting functions
L
Linus Torvalds 已提交
1168 1169 1170
 */

/**
1171 1172 1173
 *	mii_init	-	set up MII
 *	@vptr: velocity adapter
 *	@mii_status:  links tatus
L
Linus Torvalds 已提交
1174
 *
1175
 *	Set up the PHY for the current link state.
L
Linus Torvalds 已提交
1176
 */
1177
static void mii_init(struct velocity_info *vptr, u32 mii_status)
L
Linus Torvalds 已提交
1178
{
1179
	u16 BMCR;
L
Linus Torvalds 已提交
1180

1181
	switch (PHYID_GET_PHY_ID(vptr->phy_id)) {
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
	case PHYID_ICPLUS_IP101A:
		MII_REG_BITS_ON((ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP),
						MII_ADVERTISE, vptr->mac_regs);
		if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
			MII_REG_BITS_ON(TCSR_ECHODIS, MII_SREVISION,
								vptr->mac_regs);
		else
			MII_REG_BITS_OFF(TCSR_ECHODIS, MII_SREVISION,
								vptr->mac_regs);
		MII_REG_BITS_ON(PLED_LALBE, MII_TPISTATUS, vptr->mac_regs);
		break;
1193 1194 1195 1196
	case PHYID_CICADA_CS8201:
		/*
		 *	Reset to hardware default
		 */
1197
		MII_REG_BITS_OFF((ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP), MII_ADVERTISE, vptr->mac_regs);
1198 1199 1200 1201 1202 1203
		/*
		 *	Turn on ECHODIS bit in NWay-forced full mode and turn it
		 *	off it in NWay-forced half mode for NWay-forced v.s.
		 *	legacy-forced issue.
		 */
		if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
1204
			MII_REG_BITS_ON(TCSR_ECHODIS, MII_SREVISION, vptr->mac_regs);
1205
		else
1206
			MII_REG_BITS_OFF(TCSR_ECHODIS, MII_SREVISION, vptr->mac_regs);
1207 1208 1209
		/*
		 *	Turn on Link/Activity LED enable bit for CIS8201
		 */
1210
		MII_REG_BITS_ON(PLED_LALBE, MII_TPISTATUS, vptr->mac_regs);
1211 1212 1213 1214 1215 1216
		break;
	case PHYID_VT3216_32BIT:
	case PHYID_VT3216_64BIT:
		/*
		 *	Reset to hardware default
		 */
1217
		MII_REG_BITS_ON((ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP), MII_ADVERTISE, vptr->mac_regs);
1218 1219 1220 1221 1222 1223
		/*
		 *	Turn on ECHODIS bit in NWay-forced full mode and turn it
		 *	off it in NWay-forced half mode for NWay-forced v.s.
		 *	legacy-forced issue
		 */
		if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
1224
			MII_REG_BITS_ON(TCSR_ECHODIS, MII_SREVISION, vptr->mac_regs);
1225
		else
1226
			MII_REG_BITS_OFF(TCSR_ECHODIS, MII_SREVISION, vptr->mac_regs);
1227
		break;
L
Linus Torvalds 已提交
1228

1229 1230 1231 1232 1233 1234 1235 1236 1237
	case PHYID_MARVELL_1000:
	case PHYID_MARVELL_1000S:
		/*
		 *	Assert CRS on Transmit
		 */
		MII_REG_BITS_ON(PSCR_ACRSTX, MII_REG_PSCR, vptr->mac_regs);
		/*
		 *	Reset to hardware default
		 */
1238
		MII_REG_BITS_ON((ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP), MII_ADVERTISE, vptr->mac_regs);
1239 1240 1241 1242
		break;
	default:
		;
	}
1243 1244 1245 1246
	velocity_mii_read(vptr->mac_regs, MII_BMCR, &BMCR);
	if (BMCR & BMCR_ISOLATE) {
		BMCR &= ~BMCR_ISOLATE;
		velocity_mii_write(vptr->mac_regs, MII_BMCR, BMCR);
L
Linus Torvalds 已提交
1247 1248 1249
	}
}

1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
/**
 * setup_queue_timers	-	Setup interrupt timers
 *
 * Setup interrupt frequency during suppression (timeout if the frame
 * count isn't filled).
 */
static void setup_queue_timers(struct velocity_info *vptr)
{
	/* Only for newer revisions */
	if (vptr->rev_id >= REV_ID_VT3216_A0) {
		u8 txqueue_timer = 0;
		u8 rxqueue_timer = 0;

		if (vptr->mii_status & (VELOCITY_SPEED_1000 |
				VELOCITY_SPEED_100)) {
			txqueue_timer = vptr->options.txqueue_timer;
			rxqueue_timer = vptr->options.rxqueue_timer;
		}

		writeb(txqueue_timer, &vptr->mac_regs->TQETMR);
		writeb(rxqueue_timer, &vptr->mac_regs->RQETMR);
	}
}
F
françois romieu 已提交
1273

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 1308 1309 1310
/**
 * setup_adaptive_interrupts  -  Setup interrupt suppression
 *
 * @vptr velocity adapter
 *
 * The velocity is able to suppress interrupt during high interrupt load.
 * This function turns on that feature.
 */
static void setup_adaptive_interrupts(struct velocity_info *vptr)
{
	struct mac_regs __iomem *regs = vptr->mac_regs;
	u16 tx_intsup = vptr->options.tx_intsup;
	u16 rx_intsup = vptr->options.rx_intsup;

	/* Setup default interrupt mask (will be changed below) */
	vptr->int_mask = INT_MASK_DEF;

	/* Set Tx Interrupt Suppression Threshold */
	writeb(CAMCR_PS0, &regs->CAMCR);
	if (tx_intsup != 0) {
		vptr->int_mask &= ~(ISR_PTXI | ISR_PTX0I | ISR_PTX1I |
				ISR_PTX2I | ISR_PTX3I);
		writew(tx_intsup, &regs->ISRCTL);
	} else
		writew(ISRCTL_TSUPDIS, &regs->ISRCTL);

	/* Set Rx Interrupt Suppression Threshold */
	writeb(CAMCR_PS1, &regs->CAMCR);
	if (rx_intsup != 0) {
		vptr->int_mask &= ~ISR_PRXI;
		writew(rx_intsup, &regs->ISRCTL);
	} else
		writew(ISRCTL_RSUPDIS, &regs->ISRCTL);

	/* Select page to interrupt hold timer */
	writeb(0, &regs->CAMCR);
}
1311

L
Linus Torvalds 已提交
1312
/**
1313 1314 1315
 *	velocity_init_registers	-	initialise MAC registers
 *	@vptr: velocity to init
 *	@type: type of initialisation (hot or cold)
L
Linus Torvalds 已提交
1316
 *
1317 1318
 *	Initialise the MAC on a reset or on first set up on the
 *	hardware.
L
Linus Torvalds 已提交
1319
 */
1320 1321
static void velocity_init_registers(struct velocity_info *vptr,
				    enum velocity_init_type type)
L
Linus Torvalds 已提交
1322
{
1323
	struct mac_regs __iomem *regs = vptr->mac_regs;
1324
	struct net_device *netdev = vptr->netdev;
1325
	int i, mii_status;
L
Linus Torvalds 已提交
1326

1327
	mac_wol_reset(regs);
L
Linus Torvalds 已提交
1328

1329 1330 1331
	switch (type) {
	case VELOCITY_INIT_RESET:
	case VELOCITY_INIT_WOL:
L
Linus Torvalds 已提交
1332

1333
		netif_stop_queue(netdev);
1334

1335 1336 1337 1338 1339 1340
		/*
		 *	Reset RX to prevent RX pointer not on the 4X location
		 */
		velocity_rx_reset(vptr);
		mac_rx_queue_run(regs);
		mac_rx_queue_wake(regs);
1341

1342 1343 1344 1345
		mii_status = velocity_get_opt_media_mode(vptr);
		if (velocity_set_media_mode(vptr, mii_status) != VELOCITY_LINK_CHANGE) {
			velocity_print_link_status(vptr);
			if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
1346
				netif_wake_queue(netdev);
L
Linus Torvalds 已提交
1347 1348
		}

1349
		enable_flow_control_ability(vptr);
L
Linus Torvalds 已提交
1350

1351 1352 1353 1354
		mac_clear_isr(regs);
		writel(CR0_STOP, &regs->CR0Clr);
		writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT),
							&regs->CR0Set);
1355

1356
		break;
L
Linus Torvalds 已提交
1357

1358 1359 1360 1361 1362 1363 1364
	case VELOCITY_INIT_COLD:
	default:
		/*
		 *	Do reset
		 */
		velocity_soft_reset(vptr);
		mdelay(5);
L
Linus Torvalds 已提交
1365

1366 1367 1368 1369 1370
		if (!vptr->no_eeprom) {
			mac_eeprom_reload(regs);
			for (i = 0; i < 6; i++)
				writeb(netdev->dev_addr[i], regs->PAR + i);
		}
L
Linus Torvalds 已提交
1371

1372 1373 1374 1375 1376 1377
		/*
		 *	clear Pre_ACPI bit.
		 */
		BYTE_REG_BITS_OFF(CFGA_PACPI, &(regs->CFGA));
		mac_set_rx_thresh(regs, vptr->options.rx_thresh);
		mac_set_dma_length(regs, vptr->options.DMA_length);
L
Linus Torvalds 已提交
1378

1379 1380 1381 1382 1383
		writeb(WOLCFG_SAM | WOLCFG_SAB, &regs->WOLCFGSet);
		/*
		 *	Back off algorithm use original IEEE standard
		 */
		BYTE_REG_BITS_SET(CFGB_OFSET, (CFGB_CRANDOM | CFGB_CAP | CFGB_MBA | CFGB_BAKOPT), &regs->CFGB);
L
Linus Torvalds 已提交
1384 1385

		/*
1386
		 *	Init CAM filter
L
Linus Torvalds 已提交
1387
		 */
1388
		velocity_init_cam_filter(vptr);
L
Linus Torvalds 已提交
1389

1390 1391 1392
		/*
		 *	Set packet filter: Receive directed and broadcast address
		 */
1393
		velocity_set_multi(netdev);
L
Linus Torvalds 已提交
1394

1395 1396 1397 1398
		/*
		 *	Enable MII auto-polling
		 */
		enable_mii_autopoll(regs);
L
Linus Torvalds 已提交
1399

1400
		setup_adaptive_interrupts(vptr);
L
Linus Torvalds 已提交
1401

1402 1403 1404 1405
		writel(vptr->rx.pool_dma, &regs->RDBaseLo);
		writew(vptr->options.numrx - 1, &regs->RDCSize);
		mac_rx_queue_run(regs);
		mac_rx_queue_wake(regs);
L
Linus Torvalds 已提交
1406

1407
		writew(vptr->options.numtx - 1, &regs->TDCSize);
L
Linus Torvalds 已提交
1408

1409 1410 1411 1412
		for (i = 0; i < vptr->tx.numq; i++) {
			writel(vptr->tx.pool_dma[i], &regs->TDBaseLo[i]);
			mac_tx_queue_run(regs, i);
		}
L
Linus Torvalds 已提交
1413

1414
		init_flow_control_register(vptr);
1415

1416 1417
		writel(CR0_STOP, &regs->CR0Clr);
		writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT), &regs->CR0Set);
L
Linus Torvalds 已提交
1418

1419
		mii_status = velocity_get_opt_media_mode(vptr);
1420
		netif_stop_queue(netdev);
L
Linus Torvalds 已提交
1421

1422
		mii_init(vptr, mii_status);
L
Linus Torvalds 已提交
1423

1424 1425 1426
		if (velocity_set_media_mode(vptr, mii_status) != VELOCITY_LINK_CHANGE) {
			velocity_print_link_status(vptr);
			if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
1427
				netif_wake_queue(netdev);
L
Linus Torvalds 已提交
1428
		}
1429

1430 1431 1432 1433 1434
		enable_flow_control_ability(vptr);
		mac_hw_mibs_init(regs);
		mac_write_int_mask(vptr->int_mask, regs);
		mac_clear_isr(regs);

L
Linus Torvalds 已提交
1435 1436 1437
	}
}

1438
static void velocity_give_many_rx_descs(struct velocity_info *vptr)
L
Linus Torvalds 已提交
1439
{
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
	struct mac_regs __iomem *regs = vptr->mac_regs;
	int avail, dirty, unusable;

	/*
	 * RD number must be equal to 4X per hardware spec
	 * (programming guide rev 1.20, p.13)
	 */
	if (vptr->rx.filled < 4)
		return;

	wmb();

	unusable = vptr->rx.filled & 0x0003;
	dirty = vptr->rx.dirty - unusable;
	for (avail = vptr->rx.filled & 0xfffc; avail; avail--) {
		dirty = (dirty > 0) ? dirty - 1 : vptr->options.numrx - 1;
		vptr->rx.ring[dirty].rdesc0.len |= OWNED_BY_NIC;
L
Linus Torvalds 已提交
1457
	}
1458 1459 1460

	writew(vptr->rx.filled & 0xfffc, &regs->RBRDU);
	vptr->rx.filled = unusable;
L
Linus Torvalds 已提交
1461 1462 1463
}

/**
1464 1465
 *	velocity_init_dma_rings	-	set up DMA rings
 *	@vptr: Velocity to set up
1466
 *
1467 1468
 *	Allocate PCI mapped DMA rings for the receive and transmit layer
 *	to use.
L
Linus Torvalds 已提交
1469
 */
1470
static int velocity_init_dma_rings(struct velocity_info *vptr)
L
Linus Torvalds 已提交
1471
{
1472 1473 1474 1475 1476 1477
	struct velocity_opt *opt = &vptr->options;
	const unsigned int rx_ring_size = opt->numrx * sizeof(struct rx_desc);
	const unsigned int tx_ring_size = opt->numtx * sizeof(struct tx_desc);
	dma_addr_t pool_dma;
	void *pool;
	unsigned int i;
L
Linus Torvalds 已提交
1478 1479

	/*
1480 1481
	 * Allocate all RD/TD rings a single pool.
	 *
1482
	 * dma_alloc_coherent() fulfills the requirement for 64 bytes
1483
	 * alignment
L
Linus Torvalds 已提交
1484
	 */
1485 1486
	pool = dma_alloc_coherent(vptr->dev, tx_ring_size * vptr->tx.numq +
				    rx_ring_size, &pool_dma, GFP_ATOMIC);
1487
	if (!pool) {
1488
		dev_err(vptr->dev, "%s : DMA memory allocation failed.\n",
1489
			vptr->netdev->name);
1490
		return -ENOMEM;
L
Linus Torvalds 已提交
1491 1492
	}

1493 1494
	vptr->rx.ring = pool;
	vptr->rx.pool_dma = pool_dma;
L
Linus Torvalds 已提交
1495

1496 1497
	pool += rx_ring_size;
	pool_dma += rx_ring_size;
F
Francois Romieu 已提交
1498

1499 1500 1501 1502 1503 1504
	for (i = 0; i < vptr->tx.numq; i++) {
		vptr->tx.rings[i] = pool;
		vptr->tx.pool_dma[i] = pool_dma;
		pool += tx_ring_size;
		pool_dma += tx_ring_size;
	}
L
Linus Torvalds 已提交
1505 1506 1507 1508

	return 0;
}

1509 1510 1511 1512 1513
static void velocity_set_rxbufsize(struct velocity_info *vptr, int mtu)
{
	vptr->rx.buf_sz = (mtu <= ETH_DATA_LEN) ? PKT_BUF_SZ : mtu + 32;
}

L
Linus Torvalds 已提交
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
/**
 *	velocity_alloc_rx_buf	-	allocate aligned receive buffer
 *	@vptr: velocity
 *	@idx: ring index
 *
 *	Allocate a new full sized buffer for the reception of a frame and
 *	map it into PCI space for the hardware to use. The hardware
 *	requires *64* byte alignment of the buffer which makes life
 *	less fun than would be ideal.
 */
static int velocity_alloc_rx_buf(struct velocity_info *vptr, int idx)
{
1526 1527
	struct rx_desc *rd = &(vptr->rx.ring[idx]);
	struct velocity_rd_info *rd_info = &(vptr->rx.info[idx]);
L
Linus Torvalds 已提交
1528

1529
	rd_info->skb = netdev_alloc_skb(vptr->netdev, vptr->rx.buf_sz + 64);
L
Linus Torvalds 已提交
1530 1531 1532 1533 1534 1535 1536
	if (rd_info->skb == NULL)
		return -ENOMEM;

	/*
	 *	Do the gymnastics to get the buffer head for data at
	 *	64byte alignment.
	 */
1537 1538
	skb_reserve(rd_info->skb,
			64 - ((unsigned long) rd_info->skb->data & 63));
1539 1540
	rd_info->skb_dma = dma_map_single(vptr->dev, rd_info->skb->data,
					vptr->rx.buf_sz, DMA_FROM_DEVICE);
1541

L
Linus Torvalds 已提交
1542 1543
	/*
	 *	Fill in the descriptor to match
1544
	 */
1545

L
Linus Torvalds 已提交
1546
	*((u32 *) & (rd->rdesc0)) = 0;
1547
	rd->size = cpu_to_le16(vptr->rx.buf_sz) | RX_INTEN;
L
Linus Torvalds 已提交
1548 1549 1550 1551 1552
	rd->pa_low = cpu_to_le32(rd_info->skb_dma);
	rd->pa_high = 0;
	return 0;
}

1553

1554
static int velocity_rx_refill(struct velocity_info *vptr)
L
Linus Torvalds 已提交
1555
{
1556
	int dirty = vptr->rx.dirty, done = 0;
L
Linus Torvalds 已提交
1557

1558 1559
	do {
		struct rx_desc *rd = vptr->rx.ring + dirty;
L
Linus Torvalds 已提交
1560

1561 1562 1563
		/* Fine for an all zero Rx desc at init time as well */
		if (rd->rdesc0.len & OWNED_BY_NIC)
			break;
L
Linus Torvalds 已提交
1564

1565 1566
		if (!vptr->rx.info[dirty].skb) {
			if (velocity_alloc_rx_buf(vptr, dirty) < 0)
L
Linus Torvalds 已提交
1567 1568
				break;
		}
1569 1570 1571
		done++;
		dirty = (dirty < vptr->options.numrx - 1) ? dirty + 1 : 0;
	} while (dirty != vptr->rx.curr);
L
Linus Torvalds 已提交
1572

1573 1574 1575
	if (done) {
		vptr->rx.dirty = dirty;
		vptr->rx.filled += done;
L
Linus Torvalds 已提交
1576
	}
1577 1578

	return done;
L
Linus Torvalds 已提交
1579 1580 1581
}

/**
1582 1583
 *	velocity_free_rd_ring	-	free receive ring
 *	@vptr: velocity to clean up
L
Linus Torvalds 已提交
1584
 *
1585 1586
 *	Free the receive buffers for each ring slot and any
 *	attached socket buffers that need to go away.
L
Linus Torvalds 已提交
1587
 */
1588
static void velocity_free_rd_ring(struct velocity_info *vptr)
L
Linus Torvalds 已提交
1589
{
1590
	int i;
L
Linus Torvalds 已提交
1591

1592 1593
	if (vptr->rx.info == NULL)
		return;
1594

1595 1596 1597
	for (i = 0; i < vptr->options.numrx; i++) {
		struct velocity_rd_info *rd_info = &(vptr->rx.info[i]);
		struct rx_desc *rd = vptr->rx.ring + i;
L
Linus Torvalds 已提交
1598

1599
		memset(rd, 0, sizeof(*rd));
L
Linus Torvalds 已提交
1600

1601 1602
		if (!rd_info->skb)
			continue;
1603 1604
		dma_unmap_single(vptr->dev, rd_info->skb_dma, vptr->rx.buf_sz,
				 DMA_FROM_DEVICE);
1605
		rd_info->skb_dma = 0;
1606

1607 1608
		dev_kfree_skb(rd_info->skb);
		rd_info->skb = NULL;
L
Linus Torvalds 已提交
1609 1610
	}

1611 1612 1613
	kfree(vptr->rx.info);
	vptr->rx.info = NULL;
}
L
Linus Torvalds 已提交
1614

1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
/**
 *	velocity_init_rd_ring	-	set up receive ring
 *	@vptr: velocity to configure
 *
 *	Allocate and set up the receive buffers for each ring slot and
 *	assign them to the network adapter.
 */
static int velocity_init_rd_ring(struct velocity_info *vptr)
{
	int ret = -ENOMEM;
L
Linus Torvalds 已提交
1625

1626 1627 1628 1629
	vptr->rx.info = kcalloc(vptr->options.numrx,
				sizeof(struct velocity_rd_info), GFP_KERNEL);
	if (!vptr->rx.info)
		goto out;
1630

1631
	velocity_init_rx_ring_indexes(vptr);
L
Linus Torvalds 已提交
1632

1633 1634
	if (velocity_rx_refill(vptr) != vptr->options.numrx) {
		VELOCITY_PRT(MSG_LEVEL_ERR, KERN_ERR
1635
			"%s: failed to allocate RX buffer.\n", vptr->netdev->name);
1636 1637 1638
		velocity_free_rd_ring(vptr);
		goto out;
	}
L
Linus Torvalds 已提交
1639

1640 1641 1642
	ret = 0;
out:
	return ret;
L
Linus Torvalds 已提交
1643 1644 1645
}

/**
1646 1647
 *	velocity_init_td_ring	-	set up transmit ring
 *	@vptr:	velocity
L
Linus Torvalds 已提交
1648
 *
1649 1650 1651
 *	Set up the transmit ring and chain the ring pointers together.
 *	Returns zero on success or a negative posix errno code for
 *	failure.
L
Linus Torvalds 已提交
1652
 */
1653
static int velocity_init_td_ring(struct velocity_info *vptr)
L
Linus Torvalds 已提交
1654
{
1655
	int j;
L
Linus Torvalds 已提交
1656

1657 1658
	/* Init the TD ring entries */
	for (j = 0; j < vptr->tx.numq; j++) {
L
Linus Torvalds 已提交
1659

1660 1661 1662 1663 1664 1665 1666
		vptr->tx.infos[j] = kcalloc(vptr->options.numtx,
					    sizeof(struct velocity_td_info),
					    GFP_KERNEL);
		if (!vptr->tx.infos[j])	{
			while (--j >= 0)
				kfree(vptr->tx.infos[j]);
			return -ENOMEM;
L
Linus Torvalds 已提交
1667
		}
1668 1669

		vptr->tx.tail[j] = vptr->tx.curr[j] = vptr->tx.used[j] = 0;
L
Linus Torvalds 已提交
1670
	}
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
	return 0;
}

/**
 *	velocity_free_dma_rings	-	free PCI ring pointers
 *	@vptr: Velocity to free from
 *
 *	Clean up the PCI ring buffers allocated to this velocity.
 */
static void velocity_free_dma_rings(struct velocity_info *vptr)
{
	const int size = vptr->options.numrx * sizeof(struct rx_desc) +
		vptr->options.numtx * sizeof(struct tx_desc) * vptr->tx.numq;

1685
	dma_free_coherent(vptr->dev, size, vptr->rx.ring, vptr->rx.pool_dma);
L
Linus Torvalds 已提交
1686 1687
}

1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
static int velocity_init_rings(struct velocity_info *vptr, int mtu)
{
	int ret;

	velocity_set_rxbufsize(vptr, mtu);

	ret = velocity_init_dma_rings(vptr);
	if (ret < 0)
		goto out;

	ret = velocity_init_rd_ring(vptr);
	if (ret < 0)
		goto err_free_dma_rings_0;

	ret = velocity_init_td_ring(vptr);
	if (ret < 0)
		goto err_free_rd_ring_1;
out:
	return ret;

err_free_rd_ring_1:
	velocity_free_rd_ring(vptr);
err_free_dma_rings_0:
	velocity_free_dma_rings(vptr);
	goto out;
}

L
Linus Torvalds 已提交
1715
/**
1716 1717 1718
 *	velocity_free_tx_buf	-	free transmit buffer
 *	@vptr: velocity
 *	@tdinfo: buffer
L
Linus Torvalds 已提交
1719
 *
1720 1721
 *	Release an transmit buffer. If the buffer was preallocated then
 *	recycle it, if not then unmap the buffer.
L
Linus Torvalds 已提交
1722
 */
1723 1724
static void velocity_free_tx_buf(struct velocity_info *vptr,
		struct velocity_td_info *tdinfo, struct tx_desc *td)
L
Linus Torvalds 已提交
1725
{
1726
	struct sk_buff *skb = tdinfo->skb;
1727

1728 1729 1730 1731
	/*
	 *	Don't unmap the pre-allocated tx_bufs
	 */
	if (tdinfo->skb_dma) {
1732
		int i;
L
Linus Torvalds 已提交
1733

1734
		for (i = 0; i < tdinfo->nskb_dma; i++) {
1735 1736 1737 1738 1739 1740 1741
			size_t pktlen = max_t(size_t, skb->len, ETH_ZLEN);

			/* For scatter-gather */
			if (skb_shinfo(skb)->nr_frags > 0)
				pktlen = max_t(size_t, pktlen,
						td->td_buf[i].size & ~TD_QUEUE);

1742 1743
			dma_unmap_single(vptr->dev, tdinfo->skb_dma[i],
					le16_to_cpu(pktlen), DMA_TO_DEVICE);
1744
		}
L
Linus Torvalds 已提交
1745
	}
1746 1747
	dev_kfree_skb_irq(skb);
	tdinfo->skb = NULL;
L
Linus Torvalds 已提交
1748 1749
}

1750 1751 1752 1753 1754
/*
 *	FIXME: could we merge this with velocity_free_tx_buf ?
 */
static void velocity_free_td_ring_entry(struct velocity_info *vptr,
							 int q, int n)
L
Linus Torvalds 已提交
1755
{
1756 1757
	struct velocity_td_info *td_info = &(vptr->tx.infos[q][n]);
	int i;
1758

1759 1760
	if (td_info == NULL)
		return;
1761

1762 1763 1764
	if (td_info->skb) {
		for (i = 0; i < td_info->nskb_dma; i++) {
			if (td_info->skb_dma[i]) {
1765 1766
				dma_unmap_single(vptr->dev, td_info->skb_dma[i],
					td_info->skb->len, DMA_TO_DEVICE);
1767 1768
				td_info->skb_dma[i] = 0;
			}
1769
		}
1770 1771
		dev_kfree_skb(td_info->skb);
		td_info->skb = NULL;
1772
	}
L
Linus Torvalds 已提交
1773 1774 1775
}

/**
1776 1777
 *	velocity_free_td_ring	-	free td ring
 *	@vptr: velocity
L
Linus Torvalds 已提交
1778
 *
1779 1780 1781 1782 1783 1784
 *	Free up the transmit ring for this particular velocity adapter.
 *	We free the ring contents but not the ring itself.
 */
static void velocity_free_td_ring(struct velocity_info *vptr)
{
	int i, j;
L
Linus Torvalds 已提交
1785

1786 1787 1788 1789 1790
	for (j = 0; j < vptr->tx.numq; j++) {
		if (vptr->tx.infos[j] == NULL)
			continue;
		for (i = 0; i < vptr->options.numtx; i++)
			velocity_free_td_ring_entry(vptr, j, i);
1791

1792 1793 1794 1795
		kfree(vptr->tx.infos[j]);
		vptr->tx.infos[j] = NULL;
	}
}
1796

1797 1798 1799 1800 1801
static void velocity_free_rings(struct velocity_info *vptr)
{
	velocity_free_td_ring(vptr);
	velocity_free_rd_ring(vptr);
	velocity_free_dma_rings(vptr);
L
Linus Torvalds 已提交
1802 1803 1804
}

/**
1805 1806 1807 1808 1809 1810 1811 1812
 *	velocity_error	-	handle error from controller
 *	@vptr: velocity
 *	@status: card status
 *
 *	Process an error report from the hardware and attempt to recover
 *	the card itself. At the moment we cannot recover from some
 *	theoretically impossible errors but this could be fixed using
 *	the pci_device_failed logic to bounce the hardware
L
Linus Torvalds 已提交
1813 1814
 *
 */
1815
static void velocity_error(struct velocity_info *vptr, int status)
L
Linus Torvalds 已提交
1816
{
1817

1818 1819
	if (status & ISR_TXSTLI) {
		struct mac_regs __iomem *regs = vptr->mac_regs;
L
Linus Torvalds 已提交
1820

1821 1822 1823
		printk(KERN_ERR "TD structure error TDindex=%hx\n", readw(&regs->TDIdx[0]));
		BYTE_REG_BITS_ON(TXESR_TDSTR, &regs->TXESR);
		writew(TRDCSR_RUN, &regs->TDCSRClr);
1824
		netif_stop_queue(vptr->netdev);
L
Linus Torvalds 已提交
1825

1826 1827 1828
		/* FIXME: port over the pci_device_failed code and use it
		   here */
	}
L
Linus Torvalds 已提交
1829

1830 1831 1832
	if (status & ISR_SRCI) {
		struct mac_regs __iomem *regs = vptr->mac_regs;
		int linked;
L
Linus Torvalds 已提交
1833

1834 1835
		if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
			vptr->mii_status = check_connection_type(regs);
L
Linus Torvalds 已提交
1836

1837 1838 1839 1840 1841 1842
			/*
			 *	If it is a 3119, disable frame bursting in
			 *	halfduplex mode and enable it in fullduplex
			 *	 mode
			 */
			if (vptr->rev_id < REV_ID_VT3216_A0) {
1843
				if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854
					BYTE_REG_BITS_ON(TCR_TB2BDIS, &regs->TCR);
				else
					BYTE_REG_BITS_OFF(TCR_TB2BDIS, &regs->TCR);
			}
			/*
			 *	Only enable CD heart beat counter in 10HD mode
			 */
			if (!(vptr->mii_status & VELOCITY_DUPLEX_FULL) && (vptr->mii_status & VELOCITY_SPEED_10))
				BYTE_REG_BITS_OFF(TESTCFG_HBDIS, &regs->TESTCFG);
			else
				BYTE_REG_BITS_ON(TESTCFG_HBDIS, &regs->TESTCFG);
1855 1856

			setup_queue_timers(vptr);
1857 1858 1859 1860 1861
		}
		/*
		 *	Get link status from PHYSR0
		 */
		linked = readb(&regs->PHYSR0) & PHYSR0_LINKGD;
L
Linus Torvalds 已提交
1862

1863 1864
		if (linked) {
			vptr->mii_status &= ~VELOCITY_LINK_FAIL;
1865
			netif_carrier_on(vptr->netdev);
1866 1867
		} else {
			vptr->mii_status |= VELOCITY_LINK_FAIL;
1868
			netif_carrier_off(vptr->netdev);
1869
		}
L
Linus Torvalds 已提交
1870

1871 1872
		velocity_print_link_status(vptr);
		enable_flow_control_ability(vptr);
L
Linus Torvalds 已提交
1873

1874 1875 1876 1877
		/*
		 *	Re-enable auto-polling because SRCI will disable
		 *	auto-polling
		 */
L
Linus Torvalds 已提交
1878

1879
		enable_mii_autopoll(regs);
L
Linus Torvalds 已提交
1880

1881
		if (vptr->mii_status & VELOCITY_LINK_FAIL)
1882
			netif_stop_queue(vptr->netdev);
1883
		else
1884
			netif_wake_queue(vptr->netdev);
L
Linus Torvalds 已提交
1885

1886
	}
1887 1888 1889 1890
	if (status & ISR_MIBFI)
		velocity_update_hw_mibs(vptr);
	if (status & ISR_LSTEI)
		mac_rx_queue_wake(vptr->mac_regs);
L
Linus Torvalds 已提交
1891 1892 1893
}

/**
1894 1895
 *	tx_srv		-	transmit interrupt service
 *	@vptr; Velocity
L
Linus Torvalds 已提交
1896
 *
1897 1898 1899
 *	Scan the queues looking for transmitted packets that
 *	we can complete and clean up. Update any statistics as
 *	necessary/
L
Linus Torvalds 已提交
1900
 */
1901
static int velocity_tx_srv(struct velocity_info *vptr)
L
Linus Torvalds 已提交
1902
{
1903 1904 1905 1906 1907 1908
	struct tx_desc *td;
	int qnum;
	int full = 0;
	int idx;
	int works = 0;
	struct velocity_td_info *tdinfo;
1909
	struct net_device_stats *stats = &vptr->netdev->stats;
L
Linus Torvalds 已提交
1910

1911 1912 1913
	for (qnum = 0; qnum < vptr->tx.numq; qnum++) {
		for (idx = vptr->tx.tail[qnum]; vptr->tx.used[qnum] > 0;
			idx = (idx + 1) % vptr->options.numtx) {
L
Linus Torvalds 已提交
1914

1915 1916 1917 1918 1919
			/*
			 *	Get Tx Descriptor
			 */
			td = &(vptr->tx.rings[qnum][idx]);
			tdinfo = &(vptr->tx.infos[qnum][idx]);
L
Linus Torvalds 已提交
1920

1921 1922
			if (td->tdesc0.len & OWNED_BY_NIC)
				break;
L
Linus Torvalds 已提交
1923

1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
			if ((works++ > 15))
				break;

			if (td->tdesc0.TSR & TSR0_TERR) {
				stats->tx_errors++;
				stats->tx_dropped++;
				if (td->tdesc0.TSR & TSR0_CDH)
					stats->tx_heartbeat_errors++;
				if (td->tdesc0.TSR & TSR0_CRS)
					stats->tx_carrier_errors++;
				if (td->tdesc0.TSR & TSR0_ABT)
					stats->tx_aborted_errors++;
				if (td->tdesc0.TSR & TSR0_OWC)
					stats->tx_window_errors++;
			} else {
				stats->tx_packets++;
				stats->tx_bytes += tdinfo->skb->len;
			}
1942
			velocity_free_tx_buf(vptr, tdinfo, td);
1943 1944 1945
			vptr->tx.used[qnum]--;
		}
		vptr->tx.tail[qnum] = idx;
L
Linus Torvalds 已提交
1946

1947 1948 1949
		if (AVAIL_TD(vptr, qnum) < 1)
			full = 1;
	}
L
Linus Torvalds 已提交
1950
	/*
1951 1952
	 *	Look to see if we should kick the transmit network
	 *	layer for more work.
L
Linus Torvalds 已提交
1953
	 */
1954
	if (netif_queue_stopped(vptr->netdev) && (full == 0) &&
1955
	    (!(vptr->mii_status & VELOCITY_LINK_FAIL))) {
1956
		netif_wake_queue(vptr->netdev);
1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
	}
	return works;
}

/**
 *	velocity_rx_csum	-	checksum process
 *	@rd: receive packet descriptor
 *	@skb: network layer packet buffer
 *
 *	Process the status bits for the received packet and determine
 *	if the checksum was computed and verified by the hardware
 */
static inline void velocity_rx_csum(struct rx_desc *rd, struct sk_buff *skb)
{
1971
	skb_checksum_none_assert(skb);
1972

1973 1974 1975 1976 1977 1978 1979 1980
	if (rd->rdesc1.CSM & CSM_IPKT) {
		if (rd->rdesc1.CSM & CSM_IPOK) {
			if ((rd->rdesc1.CSM & CSM_TCPKT) ||
					(rd->rdesc1.CSM & CSM_UDPKT)) {
				if (!(rd->rdesc1.CSM & CSM_TUPOK))
					return;
			}
			skb->ip_summed = CHECKSUM_UNNECESSARY;
L
Linus Torvalds 已提交
1981 1982 1983 1984 1985
		}
	}
}

/**
1986 1987 1988 1989
 *	velocity_rx_copy	-	in place Rx copy for small packets
 *	@rx_skb: network layer packet buffer candidate
 *	@pkt_size: received data size
 *	@rd: receive packet descriptor
L
Linus Torvalds 已提交
1990 1991
 *	@dev: network device
 *
1992
 *	Replace the current skb that is scheduled for Rx processing by a
L
Lucas De Marchi 已提交
1993
 *	shorter, immediately allocated skb, if the received packet is small
1994 1995
 *	enough. This function returns a negative value if the received
 *	packet is too big or if memory is exhausted.
L
Linus Torvalds 已提交
1996
 */
1997 1998
static int velocity_rx_copy(struct sk_buff **rx_skb, int pkt_size,
			    struct velocity_info *vptr)
L
Linus Torvalds 已提交
1999
{
2000 2001 2002
	int ret = -1;
	if (pkt_size < rx_copybreak) {
		struct sk_buff *new_skb;
L
Linus Torvalds 已提交
2003

2004
		new_skb = netdev_alloc_skb_ip_align(vptr->netdev, pkt_size);
2005 2006 2007 2008 2009
		if (new_skb) {
			new_skb->ip_summed = rx_skb[0]->ip_summed;
			skb_copy_from_linear_data(*rx_skb, new_skb->data, pkt_size);
			*rx_skb = new_skb;
			ret = 0;
L
Linus Torvalds 已提交
2010 2011 2012
		}

	}
2013
	return ret;
L
Linus Torvalds 已提交
2014 2015 2016
}

/**
2017 2018 2019 2020
 *	velocity_iph_realign	-	IP header alignment
 *	@vptr: velocity we are handling
 *	@skb: network layer packet buffer
 *	@pkt_size: received data size
L
Linus Torvalds 已提交
2021
 *
2022 2023
 *	Align IP header on a 2 bytes boundary. This behavior can be
 *	configured by the user.
L
Linus Torvalds 已提交
2024
 */
2025 2026
static inline void velocity_iph_realign(struct velocity_info *vptr,
					struct sk_buff *skb, int pkt_size)
L
Linus Torvalds 已提交
2027
{
2028 2029 2030 2031 2032
	if (vptr->flags & VELOCITY_FLAGS_IP_ALIGN) {
		memmove(skb->data + 2, skb->data, pkt_size);
		skb_reserve(skb, 2);
	}
}
2033

2034 2035 2036 2037 2038 2039 2040 2041 2042 2043
/**
 *	velocity_receive_frame	-	received packet processor
 *	@vptr: velocity we are handling
 *	@idx: ring index
 *
 *	A packet has arrived. We process the packet and if appropriate
 *	pass the frame up the network stack
 */
static int velocity_receive_frame(struct velocity_info *vptr, int idx)
{
2044
	struct net_device_stats *stats = &vptr->netdev->stats;
2045 2046 2047 2048
	struct velocity_rd_info *rd_info = &(vptr->rx.info[idx]);
	struct rx_desc *rd = &(vptr->rx.ring[idx]);
	int pkt_len = le16_to_cpu(rd->rdesc0.len) & 0x3fff;
	struct sk_buff *skb;
L
Linus Torvalds 已提交
2049

2050 2051 2052
	if (unlikely(rd->rdesc0.RSR & (RSR_STP | RSR_EDP | RSR_RL))) {
		if (rd->rdesc0.RSR & (RSR_STP | RSR_EDP))
			VELOCITY_PRT(MSG_LEVEL_VERBOSE, KERN_ERR " %s : the received frame spans multiple RDs.\n", vptr->netdev->name);
2053 2054 2055
		stats->rx_length_errors++;
		return -EINVAL;
	}
L
Linus Torvalds 已提交
2056

2057 2058
	if (rd->rdesc0.RSR & RSR_MAR)
		stats->multicast++;
L
Linus Torvalds 已提交
2059

2060
	skb = rd_info->skb;
L
Linus Torvalds 已提交
2061

2062 2063
	dma_sync_single_for_cpu(vptr->dev, rd_info->skb_dma,
				    vptr->rx.buf_sz, DMA_FROM_DEVICE);
L
Linus Torvalds 已提交
2064

2065
	velocity_rx_csum(rd, skb);
2066

2067 2068 2069
	if (velocity_rx_copy(&skb, pkt_len, vptr) < 0) {
		velocity_iph_realign(vptr, skb, pkt_len);
		rd_info->skb = NULL;
2070 2071 2072 2073 2074
		dma_unmap_single(vptr->dev, rd_info->skb_dma, vptr->rx.buf_sz,
				 DMA_FROM_DEVICE);
	} else {
		dma_sync_single_for_device(vptr->dev, rd_info->skb_dma,
					   vptr->rx.buf_sz, DMA_FROM_DEVICE);
2075
	}
2076

2077
	skb_put(skb, pkt_len - 4);
2078
	skb->protocol = eth_type_trans(skb, vptr->netdev);
2079

J
Jiri Pirko 已提交
2080 2081 2082
	if (rd->rdesc0.RSR & RSR_DETAG) {
		u16 vid = swab16(le16_to_cpu(rd->rdesc1.PQTAG));

2083
		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
J
Jiri Pirko 已提交
2084
	}
2085
	netif_receive_skb(skb);
2086

2087
	stats->rx_bytes += pkt_len;
2088
	stats->rx_packets++;
2089

2090
	return 0;
L
Linus Torvalds 已提交
2091 2092 2093
}

/**
2094 2095
 *	velocity_rx_srv		-	service RX interrupt
 *	@vptr: velocity
L
Linus Torvalds 已提交
2096
 *
2097 2098 2099
 *	Walk the receive ring of the velocity adapter and remove
 *	any received packets from the receive queue. Hand the ring
 *	slots back to the adapter for reuse.
L
Linus Torvalds 已提交
2100
 */
2101
static int velocity_rx_srv(struct velocity_info *vptr, int budget_left)
L
Linus Torvalds 已提交
2102
{
2103
	struct net_device_stats *stats = &vptr->netdev->stats;
2104 2105 2106
	int rd_curr = vptr->rx.curr;
	int works = 0;

2107
	while (works < budget_left) {
2108 2109 2110 2111 2112 2113 2114 2115 2116
		struct rx_desc *rd = vptr->rx.ring + rd_curr;

		if (!vptr->rx.info[rd_curr].skb)
			break;

		if (rd->rdesc0.len & OWNED_BY_NIC)
			break;

		rmb();
L
Linus Torvalds 已提交
2117

2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
		/*
		 *	Don't drop CE or RL error frame although RXOK is off
		 */
		if (rd->rdesc0.RSR & (RSR_RXOK | RSR_CE | RSR_RL)) {
			if (velocity_receive_frame(vptr, rd_curr) < 0)
				stats->rx_dropped++;
		} else {
			if (rd->rdesc0.RSR & RSR_CRC)
				stats->rx_crc_errors++;
			if (rd->rdesc0.RSR & RSR_FAE)
				stats->rx_frame_errors++;
L
Linus Torvalds 已提交
2129

2130 2131
			stats->rx_dropped++;
		}
2132

2133
		rd->size |= RX_INTEN;
L
Linus Torvalds 已提交
2134

2135 2136 2137
		rd_curr++;
		if (rd_curr >= vptr->options.numrx)
			rd_curr = 0;
2138 2139
		works++;
	}
L
Linus Torvalds 已提交
2140

2141
	vptr->rx.curr = rd_curr;
L
Linus Torvalds 已提交
2142

2143 2144 2145 2146 2147 2148
	if ((works > 0) && (velocity_rx_refill(vptr) > 0))
		velocity_give_many_rx_descs(vptr);

	VAR_USED(stats);
	return works;
}
2149

2150 2151 2152 2153 2154
static int velocity_poll(struct napi_struct *napi, int budget)
{
	struct velocity_info *vptr = container_of(napi,
			struct velocity_info, napi);
	unsigned int rx_done;
2155
	unsigned long flags;
2156 2157 2158 2159 2160

	/*
	 * Do rx and tx twice for performance (taken from the VIA
	 * out-of-tree driver).
	 */
2161 2162
	rx_done = velocity_rx_srv(vptr, budget);
	spin_lock_irqsave(&vptr->lock, flags);
2163
	velocity_tx_srv(vptr);
2164 2165 2166 2167 2168
	/* If budget not fully consumed, exit the polling mode */
	if (rx_done < budget) {
		napi_complete(napi);
		mac_enable_int(vptr->mac_regs);
	}
2169
	spin_unlock_irqrestore(&vptr->lock, flags);
2170 2171 2172

	return rx_done;
}
2173

L
Linus Torvalds 已提交
2174
/**
2175 2176 2177
 *	velocity_intr		-	interrupt callback
 *	@irq: interrupt number
 *	@dev_instance: interrupting device
L
Linus Torvalds 已提交
2178
 *
2179 2180 2181 2182
 *	Called whenever an interrupt is generated by the velocity
 *	adapter IRQ line. We may not be the source of the interrupt
 *	and need to identify initially if we are, and if not exit as
 *	efficiently as possible.
L
Linus Torvalds 已提交
2183
 */
2184
static irqreturn_t velocity_intr(int irq, void *dev_instance)
L
Linus Torvalds 已提交
2185
{
2186 2187 2188
	struct net_device *dev = dev_instance;
	struct velocity_info *vptr = netdev_priv(dev);
	u32 isr_status;
L
Linus Torvalds 已提交
2189

2190 2191 2192 2193 2194 2195 2196
	spin_lock(&vptr->lock);
	isr_status = mac_read_isr(vptr->mac_regs);

	/* Not us ? */
	if (isr_status == 0) {
		spin_unlock(&vptr->lock);
		return IRQ_NONE;
L
Linus Torvalds 已提交
2197 2198
	}

2199 2200 2201
	/* Ack the interrupt */
	mac_write_isr(vptr->mac_regs, isr_status);

2202 2203 2204
	if (likely(napi_schedule_prep(&vptr->napi))) {
		mac_disable_int(vptr->mac_regs);
		__napi_schedule(&vptr->napi);
L
Linus Torvalds 已提交
2205
	}
2206 2207 2208 2209

	if (isr_status & (~(ISR_PRXI | ISR_PPRXI | ISR_PTXI | ISR_PPTXI)))
		velocity_error(vptr, isr_status);

2210 2211
	spin_unlock(&vptr->lock);

2212
	return IRQ_HANDLED;
L
Linus Torvalds 已提交
2213 2214 2215
}

/**
2216 2217
 *	velocity_open		-	interface activation callback
 *	@dev: network layer device to open
L
Linus Torvalds 已提交
2218
 *
2219 2220 2221 2222 2223
 *	Called when the network layer brings the interface up. Returns
 *	a negative posix error code on failure, or zero on success.
 *
 *	All the ring allocation and set up is done on open for this
 *	adapter to minimise memory usage when inactive
L
Linus Torvalds 已提交
2224
 */
2225
static int velocity_open(struct net_device *dev)
L
Linus Torvalds 已提交
2226
{
2227 2228
	struct velocity_info *vptr = netdev_priv(dev);
	int ret;
L
Linus Torvalds 已提交
2229

2230 2231 2232
	ret = velocity_init_rings(vptr, dev->mtu);
	if (ret < 0)
		goto out;
L
Linus Torvalds 已提交
2233

2234
	/* Ensure chip is running */
2235
	velocity_set_power_state(vptr, PCI_D0);
L
Linus Torvalds 已提交
2236

2237 2238
	velocity_init_registers(vptr, VELOCITY_INIT_COLD);

2239
	ret = request_irq(dev->irq, velocity_intr, IRQF_SHARED,
2240 2241 2242
			  dev->name, dev);
	if (ret < 0) {
		/* Power down the chip */
2243
		velocity_set_power_state(vptr, PCI_D3hot);
2244 2245
		velocity_free_rings(vptr);
		goto out;
L
Linus Torvalds 已提交
2246 2247
	}

2248 2249
	velocity_give_many_rx_descs(vptr);

2250 2251
	mac_enable_int(vptr->mac_regs);
	netif_start_queue(dev);
2252
	napi_enable(&vptr->napi);
2253 2254 2255
	vptr->flags |= VELOCITY_FLAGS_OPENED;
out:
	return ret;
L
Linus Torvalds 已提交
2256 2257 2258
}

/**
2259 2260
 *	velocity_shutdown	-	shut down the chip
 *	@vptr: velocity to deactivate
L
Linus Torvalds 已提交
2261
 *
2262 2263
 *	Shuts down the internal operations of the velocity and
 *	disables interrupts, autopolling, transmit and receive
L
Linus Torvalds 已提交
2264
 */
2265
static void velocity_shutdown(struct velocity_info *vptr)
L
Linus Torvalds 已提交
2266
{
2267 2268 2269 2270 2271
	struct mac_regs __iomem *regs = vptr->mac_regs;
	mac_disable_int(regs);
	writel(CR0_STOP, &regs->CR0Set);
	writew(0xFFFF, &regs->TDCSRClr);
	writeb(0xFF, &regs->RDCSRClr);
L
Linus Torvalds 已提交
2272
	safe_disable_mii_autopoll(regs);
2273 2274
	mac_clear_isr(regs);
}
L
Linus Torvalds 已提交
2275

2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288
/**
 *	velocity_change_mtu	-	MTU change callback
 *	@dev: network device
 *	@new_mtu: desired MTU
 *
 *	Handle requests from the networking layer for MTU change on
 *	this interface. It gets called on a change by the network layer.
 *	Return zero for success or negative posix error code.
 */
static int velocity_change_mtu(struct net_device *dev, int new_mtu)
{
	struct velocity_info *vptr = netdev_priv(dev);
	int ret = 0;
L
Linus Torvalds 已提交
2289

2290 2291
	if ((new_mtu < VELOCITY_MIN_MTU) || new_mtu > (VELOCITY_MAX_MTU)) {
		VELOCITY_PRT(MSG_LEVEL_ERR, KERN_NOTICE "%s: Invalid MTU.\n",
2292
				vptr->netdev->name);
2293 2294 2295
		ret = -EINVAL;
		goto out_0;
	}
L
Linus Torvalds 已提交
2296

2297 2298 2299
	if (!netif_running(dev)) {
		dev->mtu = new_mtu;
		goto out_0;
L
Linus Torvalds 已提交
2300 2301
	}

2302 2303 2304 2305 2306
	if (dev->mtu != new_mtu) {
		struct velocity_info *tmp_vptr;
		unsigned long flags;
		struct rx_info rx;
		struct tx_info tx;
L
Linus Torvalds 已提交
2307

2308 2309 2310 2311 2312
		tmp_vptr = kzalloc(sizeof(*tmp_vptr), GFP_KERNEL);
		if (!tmp_vptr) {
			ret = -ENOMEM;
			goto out_0;
		}
L
Linus Torvalds 已提交
2313

2314
		tmp_vptr->netdev = dev;
2315
		tmp_vptr->pdev = vptr->pdev;
2316
		tmp_vptr->dev = vptr->dev;
2317 2318
		tmp_vptr->options = vptr->options;
		tmp_vptr->tx.numq = vptr->tx.numq;
2319

2320 2321 2322
		ret = velocity_init_rings(tmp_vptr, new_mtu);
		if (ret < 0)
			goto out_free_tmp_vptr_1;
L
Linus Torvalds 已提交
2323

2324 2325
		napi_disable(&vptr->napi);

2326
		spin_lock_irqsave(&vptr->lock, flags);
L
Linus Torvalds 已提交
2327

2328 2329
		netif_stop_queue(dev);
		velocity_shutdown(vptr);
L
Linus Torvalds 已提交
2330

2331 2332
		rx = vptr->rx;
		tx = vptr->tx;
L
Linus Torvalds 已提交
2333

2334 2335
		vptr->rx = tmp_vptr->rx;
		vptr->tx = tmp_vptr->tx;
L
Linus Torvalds 已提交
2336

2337 2338
		tmp_vptr->rx = rx;
		tmp_vptr->tx = tx;
L
Linus Torvalds 已提交
2339

2340
		dev->mtu = new_mtu;
2341

2342
		velocity_init_registers(vptr, VELOCITY_INIT_COLD);
L
Linus Torvalds 已提交
2343

2344 2345
		velocity_give_many_rx_descs(vptr);

2346 2347
		napi_enable(&vptr->napi);

2348 2349
		mac_enable_int(vptr->mac_regs);
		netif_start_queue(dev);
2350

2351
		spin_unlock_irqrestore(&vptr->lock, flags);
L
Linus Torvalds 已提交
2352

2353
		velocity_free_rings(tmp_vptr);
L
Linus Torvalds 已提交
2354

2355 2356 2357 2358 2359
out_free_tmp_vptr_1:
		kfree(tmp_vptr);
	}
out_0:
	return ret;
L
Linus Torvalds 已提交
2360 2361
}

2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378
#ifdef CONFIG_NET_POLL_CONTROLLER
/**
 *  velocity_poll_controller		-	Velocity Poll controller function
 *  @dev: network device
 *
 *
 *  Used by NETCONSOLE and other diagnostic tools to allow network I/P
 *  with interrupts disabled.
 */
static void velocity_poll_controller(struct net_device *dev)
{
	disable_irq(dev->irq);
	velocity_intr(dev->irq, dev);
	enable_irq(dev->irq);
}
#endif

L
Linus Torvalds 已提交
2379
/**
2380 2381 2382 2383
 *	velocity_mii_ioctl		-	MII ioctl handler
 *	@dev: network device
 *	@ifr: the ifreq block for the ioctl
 *	@cmd: the command
L
Linus Torvalds 已提交
2384
 *
2385 2386 2387
 *	Process MII requests made via ioctl from the network layer. These
 *	are used by tools like kudzu to interrogate the link state of the
 *	hardware
L
Linus Torvalds 已提交
2388
 */
2389
static int velocity_mii_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
L
Linus Torvalds 已提交
2390
{
2391 2392 2393 2394 2395
	struct velocity_info *vptr = netdev_priv(dev);
	struct mac_regs __iomem *regs = vptr->mac_regs;
	unsigned long flags;
	struct mii_ioctl_data *miidata = if_mii(ifr);
	int err;
L
Linus Torvalds 已提交
2396

2397 2398 2399
	switch (cmd) {
	case SIOCGMIIPHY:
		miidata->phy_id = readb(&regs->MIIADR) & 0x1f;
L
Linus Torvalds 已提交
2400
		break;
2401 2402 2403
	case SIOCGMIIREG:
		if (velocity_mii_read(vptr->mac_regs, miidata->reg_num & 0x1f, &(miidata->val_out)) < 0)
			return -ETIMEDOUT;
L
Linus Torvalds 已提交
2404
		break;
2405 2406 2407 2408 2409 2410 2411
	case SIOCSMIIREG:
		spin_lock_irqsave(&vptr->lock, flags);
		err = velocity_mii_write(vptr->mac_regs, miidata->reg_num & 0x1f, miidata->val_in);
		spin_unlock_irqrestore(&vptr->lock, flags);
		check_connection_type(vptr->mac_regs);
		if (err)
			return err;
L
Linus Torvalds 已提交
2412 2413
		break;
	default:
2414
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
2415
	}
2416
	return 0;
L
Linus Torvalds 已提交
2417 2418 2419
}

/**
2420 2421 2422 2423
 *	velocity_ioctl		-	ioctl entry point
 *	@dev: network device
 *	@rq: interface request ioctl
 *	@cmd: command code
L
Linus Torvalds 已提交
2424
 *
2425 2426
 *	Called when the user issues an ioctl request to the network
 *	device in question. The velocity interface supports MII.
L
Linus Torvalds 已提交
2427
 */
2428 2429 2430 2431
static int velocity_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
{
	struct velocity_info *vptr = netdev_priv(dev);
	int ret;
2432

2433 2434
	/* If we are asked for information and the device is power
	   saving then we need to bring the device back up to talk to it */
L
Linus Torvalds 已提交
2435

2436
	if (!netif_running(dev))
2437
		velocity_set_power_state(vptr, PCI_D0);
L
Linus Torvalds 已提交
2438

2439 2440 2441 2442 2443 2444
	switch (cmd) {
	case SIOCGMIIPHY:	/* Get address of MII PHY in use. */
	case SIOCGMIIREG:	/* Read MII PHY register. */
	case SIOCSMIIREG:	/* Write to MII PHY register. */
		ret = velocity_mii_ioctl(dev, rq, cmd);
		break;
L
Linus Torvalds 已提交
2445

2446 2447 2448 2449
	default:
		ret = -EOPNOTSUPP;
	}
	if (!netif_running(dev))
2450
		velocity_set_power_state(vptr, PCI_D3hot);
L
Linus Torvalds 已提交
2451

2452

2453
	return ret;
L
Linus Torvalds 已提交
2454 2455 2456
}

/**
2457 2458
 *	velocity_get_status	-	statistics callback
 *	@dev: network device
L
Linus Torvalds 已提交
2459
 *
2460 2461 2462 2463 2464
 *	Callback from the network layer to allow driver statistics
 *	to be resynchronized with hardware collected state. In the
 *	case of the velocity we need to pull the MIB counters from
 *	the hardware into the counters before letting the network
 *	layer display them.
L
Linus Torvalds 已提交
2465
 */
2466
static struct net_device_stats *velocity_get_stats(struct net_device *dev)
L
Linus Torvalds 已提交
2467
{
2468
	struct velocity_info *vptr = netdev_priv(dev);
L
Linus Torvalds 已提交
2469

2470 2471 2472
	/* If the hardware is down, don't touch MII */
	if (!netif_running(dev))
		return &dev->stats;
L
Linus Torvalds 已提交
2473

2474 2475 2476
	spin_lock_irq(&vptr->lock);
	velocity_update_hw_mibs(vptr);
	spin_unlock_irq(&vptr->lock);
L
Linus Torvalds 已提交
2477

2478 2479 2480
	dev->stats.rx_packets = vptr->mib_counter[HW_MIB_ifRxAllPkts];
	dev->stats.rx_errors = vptr->mib_counter[HW_MIB_ifRxErrorPkts];
	dev->stats.rx_length_errors = vptr->mib_counter[HW_MIB_ifInRangeLengthErrors];
L
Linus Torvalds 已提交
2481

2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495
//  unsigned long   rx_dropped;     /* no space in linux buffers    */
	dev->stats.collisions = vptr->mib_counter[HW_MIB_ifTxEtherCollisions];
	/* detailed rx_errors: */
//  unsigned long   rx_length_errors;
//  unsigned long   rx_over_errors;     /* receiver ring buff overflow  */
	dev->stats.rx_crc_errors = vptr->mib_counter[HW_MIB_ifRxPktCRCE];
//  unsigned long   rx_frame_errors;    /* recv'd frame alignment error */
//  unsigned long   rx_fifo_errors;     /* recv'r fifo overrun      */
//  unsigned long   rx_missed_errors;   /* receiver missed packet   */

	/* detailed tx_errors */
//  unsigned long   tx_fifo_errors;

	return &dev->stats;
L
Linus Torvalds 已提交
2496 2497
}

2498 2499 2500 2501 2502 2503 2504 2505
/**
 *	velocity_close		-	close adapter callback
 *	@dev: network device
 *
 *	Callback from the network layer when the velocity is being
 *	deactivated by the network layer
 */
static int velocity_close(struct net_device *dev)
L
Linus Torvalds 已提交
2506
{
2507
	struct velocity_info *vptr = netdev_priv(dev);
L
Linus Torvalds 已提交
2508

2509
	napi_disable(&vptr->napi);
2510 2511
	netif_stop_queue(dev);
	velocity_shutdown(vptr);
L
Linus Torvalds 已提交
2512

2513 2514
	if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED)
		velocity_get_ip(vptr);
2515

2516
	free_irq(dev->irq, dev);
L
Linus Torvalds 已提交
2517

2518
	velocity_free_rings(vptr);
L
Linus Torvalds 已提交
2519

2520 2521
	vptr->flags &= (~VELOCITY_FLAGS_OPENED);
	return 0;
L
Linus Torvalds 已提交
2522 2523 2524
}

/**
2525 2526 2527
 *	velocity_xmit		-	transmit packet callback
 *	@skb: buffer to transmit
 *	@dev: network device
L
Linus Torvalds 已提交
2528
 *
2529 2530
 *	Called by the networ layer to request a packet is queued to
 *	the velocity. Returns zero on success.
L
Linus Torvalds 已提交
2531
 */
2532 2533
static netdev_tx_t velocity_xmit(struct sk_buff *skb,
				 struct net_device *dev)
L
Linus Torvalds 已提交
2534
{
2535 2536 2537 2538 2539 2540
	struct velocity_info *vptr = netdev_priv(dev);
	int qnum = 0;
	struct tx_desc *td_ptr;
	struct velocity_td_info *tdinfo;
	unsigned long flags;
	int pktlen;
2541 2542
	int index, prev;
	int i = 0;
L
Linus Torvalds 已提交
2543

2544 2545
	if (skb_padto(skb, ETH_ZLEN))
		goto out;
L
Linus Torvalds 已提交
2546

2547 2548 2549
	/* The hardware can handle at most 7 memory segments, so merge
	 * the skb if there are more */
	if (skb_shinfo(skb)->nr_frags > 6 && __skb_linearize(skb)) {
2550
		dev_kfree_skb_any(skb);
2551 2552 2553 2554 2555 2556
		return NETDEV_TX_OK;
	}

	pktlen = skb_shinfo(skb)->nr_frags == 0 ?
			max_t(unsigned int, skb->len, ETH_ZLEN) :
				skb_headlen(skb);
L
Linus Torvalds 已提交
2557

2558
	spin_lock_irqsave(&vptr->lock, flags);
L
Linus Torvalds 已提交
2559

2560 2561 2562
	index = vptr->tx.curr[qnum];
	td_ptr = &(vptr->tx.rings[qnum][index]);
	tdinfo = &(vptr->tx.infos[qnum][index]);
L
Linus Torvalds 已提交
2563

2564 2565
	td_ptr->tdesc1.TCR = TCR0_TIC;
	td_ptr->td_buf[0].size &= ~TD_QUEUE;
L
Linus Torvalds 已提交
2566

2567 2568 2569 2570 2571
	/*
	 *	Map the linear network buffer into PCI space and
	 *	add it to the transmit ring.
	 */
	tdinfo->skb = skb;
2572 2573
	tdinfo->skb_dma[0] = dma_map_single(vptr->dev, skb->data, pktlen,
								DMA_TO_DEVICE);
2574
	td_ptr->tdesc0.len = cpu_to_le16(pktlen);
2575 2576
	td_ptr->td_buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]);
	td_ptr->td_buf[0].pa_high = 0;
2577 2578 2579 2580
	td_ptr->td_buf[0].size = cpu_to_le16(pktlen);

	/* Handle fragments */
	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
E
Eric Dumazet 已提交
2581
		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2582

2583
		tdinfo->skb_dma[i + 1] = skb_frag_dma_map(vptr->dev,
2584
							  frag, 0,
E
Eric Dumazet 已提交
2585
							  skb_frag_size(frag),
2586
							  DMA_TO_DEVICE);
2587 2588 2589

		td_ptr->td_buf[i + 1].pa_low = cpu_to_le32(tdinfo->skb_dma[i + 1]);
		td_ptr->td_buf[i + 1].pa_high = 0;
E
Eric Dumazet 已提交
2590
		td_ptr->td_buf[i + 1].size = cpu_to_le16(skb_frag_size(frag));
2591 2592
	}
	tdinfo->nskb_dma = i + 1;
2593 2594 2595

	td_ptr->tdesc1.cmd = TCPLS_NORMAL + (tdinfo->nskb_dma + 1) * 16;

2596 2597
	if (skb_vlan_tag_present(skb)) {
		td_ptr->tdesc1.vlan = cpu_to_le16(skb_vlan_tag_get(skb));
2598 2599 2600 2601 2602 2603
		td_ptr->tdesc1.TCR |= TCR0_VETAG;
	}

	/*
	 *	Handle hardware checksum
	 */
2604
	if (skb->ip_summed == CHECKSUM_PARTIAL) {
2605 2606 2607 2608 2609 2610 2611
		const struct iphdr *ip = ip_hdr(skb);
		if (ip->protocol == IPPROTO_TCP)
			td_ptr->tdesc1.TCR |= TCR0_TCPCK;
		else if (ip->protocol == IPPROTO_UDP)
			td_ptr->tdesc1.TCR |= (TCR0_UDPCK);
		td_ptr->tdesc1.TCR |= TCR0_IPCK;
	}
L
Linus Torvalds 已提交
2612

2613 2614 2615 2616 2617 2618
	prev = index - 1;
	if (prev < 0)
		prev = vptr->options.numtx - 1;
	td_ptr->tdesc0.len |= OWNED_BY_NIC;
	vptr->tx.used[qnum]++;
	vptr->tx.curr[qnum] = (index + 1) % vptr->options.numtx;
L
Linus Torvalds 已提交
2619

2620 2621
	if (AVAIL_TD(vptr, qnum) < 1)
		netif_stop_queue(dev);
L
Linus Torvalds 已提交
2622

2623 2624 2625
	td_ptr = &(vptr->tx.rings[qnum][prev]);
	td_ptr->td_buf[0].size |= TD_QUEUE;
	mac_tx_queue_wake(vptr->mac_regs, qnum);
L
Linus Torvalds 已提交
2626

2627 2628 2629
	spin_unlock_irqrestore(&vptr->lock, flags);
out:
	return NETDEV_TX_OK;
L
Linus Torvalds 已提交
2630 2631
}

2632 2633 2634 2635 2636 2637
static const struct net_device_ops velocity_netdev_ops = {
	.ndo_open		= velocity_open,
	.ndo_stop		= velocity_close,
	.ndo_start_xmit		= velocity_xmit,
	.ndo_get_stats		= velocity_get_stats,
	.ndo_validate_addr	= eth_validate_addr,
F
françois romieu 已提交
2638
	.ndo_set_mac_address	= eth_mac_addr,
2639
	.ndo_set_rx_mode	= velocity_set_multi,
2640 2641 2642 2643
	.ndo_change_mtu		= velocity_change_mtu,
	.ndo_do_ioctl		= velocity_ioctl,
	.ndo_vlan_rx_add_vid	= velocity_vlan_rx_add_vid,
	.ndo_vlan_rx_kill_vid	= velocity_vlan_rx_kill_vid,
2644 2645 2646
#ifdef CONFIG_NET_POLL_CONTROLLER
	.ndo_poll_controller = velocity_poll_controller,
#endif
2647 2648
};

L
Linus Torvalds 已提交
2649
/**
2650 2651 2652 2653
 *	velocity_init_info	-	init private data
 *	@pdev: PCI device
 *	@vptr: Velocity info
 *	@info: Board type
L
Linus Torvalds 已提交
2654
 *
2655 2656
 *	Set up the initial velocity_info struct for the device that has been
 *	discovered.
L
Linus Torvalds 已提交
2657
 */
2658 2659
static void velocity_init_info(struct velocity_info *vptr,
				const struct velocity_info_tbl *info)
L
Linus Torvalds 已提交
2660
{
2661 2662 2663 2664
	vptr->chip_id = info->chip_id;
	vptr->tx.numq = info->txqueue;
	vptr->multicast_limit = MCAM_SIZE;
	spin_lock_init(&vptr->lock);
2665
}
L
Linus Torvalds 已提交
2666 2667

/**
2668 2669 2670
 *	velocity_get_pci_info	-	retrieve PCI info for device
 *	@vptr: velocity device
 *	@pdev: PCI device it matches
L
Linus Torvalds 已提交
2671
 *
2672 2673
 *	Retrieve the PCI configuration space data that interests us from
 *	the kernel PCI layer
L
Linus Torvalds 已提交
2674
 */
2675
static int velocity_get_pci_info(struct velocity_info *vptr)
L
Linus Torvalds 已提交
2676
{
2677
	struct pci_dev *pdev = vptr->pdev;
L
Linus Torvalds 已提交
2678

2679
	pci_set_master(pdev);
L
Linus Torvalds 已提交
2680

2681 2682
	vptr->ioaddr = pci_resource_start(pdev, 0);
	vptr->memaddr = pci_resource_start(pdev, 1);
L
Linus Torvalds 已提交
2683

2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699
	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_IO)) {
		dev_err(&pdev->dev,
			   "region #0 is not an I/O resource, aborting.\n");
		return -EINVAL;
	}

	if ((pci_resource_flags(pdev, 1) & IORESOURCE_IO)) {
		dev_err(&pdev->dev,
			   "region #1 is an I/O resource, aborting.\n");
		return -EINVAL;
	}

	if (pci_resource_len(pdev, 1) < VELOCITY_IO_SIZE) {
		dev_err(&pdev->dev, "region #1 is too small.\n");
		return -EINVAL;
	}
2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730

	return 0;
}

/**
 *	velocity_get_platform_info - retrieve platform info for device
 *	@vptr: velocity device
 *	@pdev: platform device it matches
 *
 *	Retrieve the Platform configuration data that interests us
 */
static int velocity_get_platform_info(struct velocity_info *vptr)
{
	struct resource res;
	int ret;

	if (of_get_property(vptr->dev->of_node, "no-eeprom", NULL))
		vptr->no_eeprom = 1;

	ret = of_address_to_resource(vptr->dev->of_node, 0, &res);
	if (ret) {
		dev_err(vptr->dev, "unable to find memory address\n");
		return ret;
	}

	vptr->memaddr = res.start;

	if (resource_size(&res) < VELOCITY_IO_SIZE) {
		dev_err(vptr->dev, "memory region is too small.\n");
		return -EINVAL;
	}
2731

L
Linus Torvalds 已提交
2732 2733 2734
	return 0;
}

2735 2736 2737 2738 2739 2740 2741
/**
 *	velocity_print_info	-	per driver data
 *	@vptr: velocity
 *
 *	Print per driver data as the kernel driver finds Velocity
 *	hardware
 */
2742
static void velocity_print_info(struct velocity_info *vptr)
L
Linus Torvalds 已提交
2743
{
2744
	struct net_device *dev = vptr->netdev;
L
Linus Torvalds 已提交
2745

2746
	printk(KERN_INFO "%s: %s\n", dev->name, get_chip_name(vptr->chip_id));
2747 2748
	printk(KERN_INFO "%s: Ethernet Address: %pM\n",
		dev->name, dev->dev_addr);
L
Linus Torvalds 已提交
2749 2750 2751 2752
}

static u32 velocity_get_link(struct net_device *dev)
{
2753
	struct velocity_info *vptr = netdev_priv(dev);
2754
	struct mac_regs __iomem *regs = vptr->mac_regs;
2755
	return BYTE_REG_BITS_IS_ON(PHYSR0_LINKGD, &regs->PHYSR0) ? 1 : 0;
L
Linus Torvalds 已提交
2756 2757
}

2758
/**
2759
 *	velocity_probe - set up discovered velocity device
2760 2761
 *	@pdev: PCI device
 *	@ent: PCI device table entry that matched
2762
 *	@bustype: bus that device is connected to
2763 2764 2765 2766
 *
 *	Configure a discovered adapter from scratch. Return a negative
 *	errno error code on failure paths.
 */
2767 2768 2769
static int velocity_probe(struct device *dev, int irq,
			   const struct velocity_info_tbl *info,
			   enum velocity_bus_type bustype)
L
Linus Torvalds 已提交
2770
{
2771
	static int first = 1;
2772
	struct net_device *netdev;
2773 2774 2775 2776 2777
	int i;
	const char *drv_string;
	struct velocity_info *vptr;
	struct mac_regs __iomem *regs;
	int ret = -ENOMEM;
L
Linus Torvalds 已提交
2778

2779 2780 2781 2782
	/* FIXME: this driver, like almost all other ethernet drivers,
	 * can support more than MAX_UNITS.
	 */
	if (velocity_nics >= MAX_UNITS) {
2783
		dev_notice(dev, "already found %d NICs.\n", velocity_nics);
2784 2785
		return -ENODEV;
	}
L
Linus Torvalds 已提交
2786

2787 2788
	netdev = alloc_etherdev(sizeof(struct velocity_info));
	if (!netdev)
2789
		goto out;
L
Linus Torvalds 已提交
2790

2791
	/* Chain it all together */
L
Linus Torvalds 已提交
2792

2793 2794
	SET_NETDEV_DEV(netdev, dev);
	vptr = netdev_priv(netdev);
2795 2796 2797 2798 2799 2800 2801 2802 2803

	if (first) {
		printk(KERN_INFO "%s Ver. %s\n",
			VELOCITY_FULL_DRV_NAM, VELOCITY_VERSION);
		printk(KERN_INFO "Copyright (c) 2002, 2003 VIA Networking Technologies, Inc.\n");
		printk(KERN_INFO "Copyright (c) 2004 Red Hat Inc.\n");
		first = 0;
	}

2804 2805 2806
	netdev->irq = irq;
	vptr->netdev = netdev;
	vptr->dev = dev;
2807

2808
	velocity_init_info(vptr, info);
2809

2810 2811
	if (bustype == BUS_PCI) {
		vptr->pdev = to_pci_dev(dev);
2812

2813 2814 2815 2816 2817 2818 2819 2820
		ret = velocity_get_pci_info(vptr);
		if (ret < 0)
			goto err_free_dev;
	} else {
		vptr->pdev = NULL;
		ret = velocity_get_platform_info(vptr);
		if (ret < 0)
			goto err_free_dev;
L
Linus Torvalds 已提交
2821
	}
2822 2823 2824 2825

	regs = ioremap(vptr->memaddr, VELOCITY_IO_SIZE);
	if (regs == NULL) {
		ret = -EIO;
2826
		goto err_free_dev;
L
Linus Torvalds 已提交
2827 2828
	}

2829
	vptr->mac_regs = regs;
2830
	vptr->rev_id = readb(&regs->rev_id);
L
Linus Torvalds 已提交
2831

2832
	mac_wol_reset(regs);
L
Linus Torvalds 已提交
2833

2834
	for (i = 0; i < 6; i++)
2835
		netdev->dev_addr[i] = readb(&regs->PAR[i]);
2836 2837


2838
	drv_string = dev_driver_string(dev);
L
Linus Torvalds 已提交
2839

2840
	velocity_get_options(&vptr->options, velocity_nics, drv_string);
L
Linus Torvalds 已提交
2841

2842 2843 2844
	/*
	 *	Mask out the options cannot be set to the chip
	 */
2845

2846
	vptr->options.flags &= info->flags;
L
Linus Torvalds 已提交
2847

2848 2849 2850
	/*
	 *	Enable the chip specified capbilities
	 */
L
Linus Torvalds 已提交
2851

2852
	vptr->flags = vptr->options.flags | (info->flags & 0xFF000000UL);
L
Linus Torvalds 已提交
2853

2854 2855
	vptr->wol_opts = vptr->options.wol_opts;
	vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
L
Linus Torvalds 已提交
2856

2857
	vptr->phy_id = MII_GET_PHY_ID(vptr->mac_regs);
L
Linus Torvalds 已提交
2858

2859 2860 2861 2862
	netdev->netdev_ops = &velocity_netdev_ops;
	netdev->ethtool_ops = &velocity_ethtool_ops;
	netif_napi_add(netdev, &vptr->napi, velocity_poll,
							VELOCITY_NAPI_WEIGHT);
2863

2864
	netdev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
2865
			   NETIF_F_HW_VLAN_CTAG_TX;
2866 2867 2868
	netdev->features |= NETIF_F_HW_VLAN_CTAG_TX |
			NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX |
			NETIF_F_IP_CSUM;
L
Linus Torvalds 已提交
2869

2870
	ret = register_netdev(netdev);
2871 2872 2873
	if (ret < 0)
		goto err_iounmap;

2874 2875
	if (!velocity_get_link(netdev)) {
		netif_carrier_off(netdev);
2876
		vptr->mii_status |= VELOCITY_LINK_FAIL;
L
Linus Torvalds 已提交
2877 2878
	}

2879
	velocity_print_info(vptr);
2880
	dev_set_drvdata(vptr->dev, netdev);
L
Linus Torvalds 已提交
2881

2882
	/* and leave the chip powered down */
L
Linus Torvalds 已提交
2883

2884
	velocity_set_power_state(vptr, PCI_D3hot);
2885 2886 2887 2888 2889
	velocity_nics++;
out:
	return ret;

err_iounmap:
2890
	netif_napi_del(&vptr->napi);
2891 2892
	iounmap(regs);
err_free_dev:
2893
	free_netdev(netdev);
2894
	goto out;
L
Linus Torvalds 已提交
2895 2896
}

2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910
/**
 *	velocity_remove	- device unplug
 *	@dev: device being removed
 *
 *	Device unload callback. Called on an unplug or on module
 *	unload for each active device that is present. Disconnects
 *	the device from the network layer and frees all the resources
 */
static int velocity_remove(struct device *dev)
{
	struct net_device *netdev = dev_get_drvdata(dev);
	struct velocity_info *vptr = netdev_priv(netdev);

	unregister_netdev(netdev);
2911
	netif_napi_del(&vptr->napi);
2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979
	iounmap(vptr->mac_regs);
	free_netdev(netdev);
	velocity_nics--;

	return 0;
}

static int velocity_pci_probe(struct pci_dev *pdev,
			       const struct pci_device_id *ent)
{
	const struct velocity_info_tbl *info =
					&chip_info_table[ent->driver_data];
	int ret;

	ret = pci_enable_device(pdev);
	if (ret < 0)
		return ret;

	ret = pci_request_regions(pdev, VELOCITY_NAME);
	if (ret < 0) {
		dev_err(&pdev->dev, "No PCI resources.\n");
		goto fail1;
	}

	ret = velocity_probe(&pdev->dev, pdev->irq, info, BUS_PCI);
	if (ret == 0)
		return 0;

	pci_release_regions(pdev);
fail1:
	pci_disable_device(pdev);
	return ret;
}

static void velocity_pci_remove(struct pci_dev *pdev)
{
	velocity_remove(&pdev->dev);

	pci_release_regions(pdev);
	pci_disable_device(pdev);
}

static int velocity_platform_probe(struct platform_device *pdev)
{
	const struct of_device_id *of_id;
	const struct velocity_info_tbl *info;
	int irq;

	of_id = of_match_device(velocity_of_ids, &pdev->dev);
	if (!of_id)
		return -EINVAL;
	info = of_id->data;

	irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
	if (!irq)
		return -EINVAL;

	return velocity_probe(&pdev->dev, irq, info, BUS_PLATFORM);
}

static int velocity_platform_remove(struct platform_device *pdev)
{
	velocity_remove(&pdev->dev);

	return 0;
}

#ifdef CONFIG_PM_SLEEP
L
Linus Torvalds 已提交
2980 2981 2982 2983 2984 2985 2986 2987
/**
 *	wol_calc_crc		-	WOL CRC
 *	@pattern: data pattern
 *	@mask_pattern: mask
 *
 *	Compute the wake on lan crc hashes for the packet header
 *	we are interested in.
 */
2988
static u16 wol_calc_crc(int size, u8 *pattern, u8 *mask_pattern)
L
Linus Torvalds 已提交
2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011
{
	u16 crc = 0xFFFF;
	u8 mask;
	int i, j;

	for (i = 0; i < size; i++) {
		mask = mask_pattern[i];

		/* Skip this loop if the mask equals to zero */
		if (mask == 0x00)
			continue;

		for (j = 0; j < 8; j++) {
			if ((mask & 0x01) == 0) {
				mask >>= 1;
				continue;
			}
			mask >>= 1;
			crc = crc_ccitt(crc, &(pattern[i * 8 + j]), 1);
		}
	}
	/*	Finally, invert the result once to get the correct data */
	crc = ~crc;
3012
	return bitrev32(crc) >> 16;
L
Linus Torvalds 已提交
3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025
}

/**
 *	velocity_set_wol	-	set up for wake on lan
 *	@vptr: velocity to set WOL status on
 *
 *	Set a card up for wake on lan either by unicast or by
 *	ARP packet.
 *
 *	FIXME: check static buffer is safe here
 */
static int velocity_set_wol(struct velocity_info *vptr)
{
3026
	struct mac_regs __iomem *regs = vptr->mac_regs;
3027
	enum speed_opt spd_dpx = vptr->options.spd_dpx;
L
Linus Torvalds 已提交
3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044
	static u8 buf[256];
	int i;

	static u32 mask_pattern[2][4] = {
		{0x00203000, 0x000003C0, 0x00000000, 0x0000000}, /* ARP */
		{0xfffff000, 0xffffffff, 0xffffffff, 0x000ffff}	 /* Magic Packet */
	};

	writew(0xFFFF, &regs->WOLCRClr);
	writeb(WOLCFG_SAB | WOLCFG_SAM, &regs->WOLCFGSet);
	writew(WOLCR_MAGIC_EN, &regs->WOLCRSet);

	/*
	   if (vptr->wol_opts & VELOCITY_WOL_PHY)
	   writew((WOLCR_LINKON_EN|WOLCR_LINKOFF_EN), &regs->WOLCRSet);
	 */

3045
	if (vptr->wol_opts & VELOCITY_WOL_UCAST)
L
Linus Torvalds 已提交
3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072
		writew(WOLCR_UNICAST_EN, &regs->WOLCRSet);

	if (vptr->wol_opts & VELOCITY_WOL_ARP) {
		struct arp_packet *arp = (struct arp_packet *) buf;
		u16 crc;
		memset(buf, 0, sizeof(struct arp_packet) + 7);

		for (i = 0; i < 4; i++)
			writel(mask_pattern[0][i], &regs->ByteMask[0][i]);

		arp->type = htons(ETH_P_ARP);
		arp->ar_op = htons(1);

		memcpy(arp->ar_tip, vptr->ip_addr, 4);

		crc = wol_calc_crc((sizeof(struct arp_packet) + 7) / 8, buf,
				(u8 *) & mask_pattern[0][0]);

		writew(crc, &regs->PatternCRC[0]);
		writew(WOLCR_ARP_EN, &regs->WOLCRSet);
	}

	BYTE_REG_BITS_ON(PWCFG_WOLTYPE, &regs->PWCFGSet);
	BYTE_REG_BITS_ON(PWCFG_LEGACY_WOLEN, &regs->PWCFGSet);

	writew(0x0FFF, &regs->WOLSRClr);

3073 3074 3075 3076 3077 3078
	if (spd_dpx == SPD_DPX_1000_FULL)
		goto mac_done;

	if (spd_dpx != SPD_DPX_AUTO)
		goto advertise_done;

L
Linus Torvalds 已提交
3079 3080
	if (vptr->mii_status & VELOCITY_AUTONEG_ENABLE) {
		if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201)
3081
			MII_REG_BITS_ON(AUXCR_MDPPS, MII_NCONFIG, vptr->mac_regs);
L
Linus Torvalds 已提交
3082

3083
		MII_REG_BITS_OFF(ADVERTISE_1000FULL | ADVERTISE_1000HALF, MII_CTRL1000, vptr->mac_regs);
L
Linus Torvalds 已提交
3084 3085 3086
	}

	if (vptr->mii_status & VELOCITY_SPEED_1000)
3087
		MII_REG_BITS_ON(BMCR_ANRESTART, MII_BMCR, vptr->mac_regs);
L
Linus Torvalds 已提交
3088

3089
advertise_done:
L
Linus Torvalds 已提交
3090 3091 3092 3093 3094 3095 3096 3097 3098
	BYTE_REG_BITS_ON(CHIPGCR_FCMODE, &regs->CHIPGCR);

	{
		u8 GCR;
		GCR = readb(&regs->CHIPGCR);
		GCR = (GCR & ~CHIPGCR_FCGMII) | CHIPGCR_FCFDX;
		writeb(GCR, &regs->CHIPGCR);
	}

3099
mac_done:
L
Linus Torvalds 已提交
3100 3101 3102 3103 3104 3105 3106 3107 3108
	BYTE_REG_BITS_OFF(ISR_PWEI, &regs->ISR);
	/* Turn on SWPTAG just before entering power mode */
	BYTE_REG_BITS_ON(STICKHW_SWPTAG, &regs->STICKHW);
	/* Go to bed ..... */
	BYTE_REG_BITS_ON((STICKHW_DS1 | STICKHW_DS0), &regs->STICKHW);

	return 0;
}

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
/**
 *	velocity_save_context	-	save registers
 *	@vptr: velocity
 *	@context: buffer for stored context
 *
 *	Retrieve the current configuration from the velocity hardware
 *	and stash it in the context structure, for use by the context
 *	restore functions. This allows us to save things we need across
 *	power down states
 */
static void velocity_save_context(struct velocity_info *vptr, struct velocity_context *context)
{
	struct mac_regs __iomem *regs = vptr->mac_regs;
	u16 i;
	u8 __iomem *ptr = (u8 __iomem *)regs;

	for (i = MAC_REG_PAR; i < MAC_REG_CR0_CLR; i += 4)
		*((u32 *) (context->mac_reg + i)) = readl(ptr + i);

	for (i = MAC_REG_MAR; i < MAC_REG_TDCSR_CLR; i += 4)
		*((u32 *) (context->mac_reg + i)) = readl(ptr + i);

	for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4)
		*((u32 *) (context->mac_reg + i)) = readl(ptr + i);

}

3136
static int velocity_suspend(struct device *dev)
L
Linus Torvalds 已提交
3137
{
3138 3139
	struct net_device *netdev = dev_get_drvdata(dev);
	struct velocity_info *vptr = netdev_priv(netdev);
L
Linus Torvalds 已提交
3140 3141
	unsigned long flags;

3142
	if (!netif_running(vptr->netdev))
L
Linus Torvalds 已提交
3143 3144
		return 0;

3145
	netif_device_detach(vptr->netdev);
L
Linus Torvalds 已提交
3146 3147

	spin_lock_irqsave(&vptr->lock, flags);
3148 3149
	if (vptr->pdev)
		pci_save_state(vptr->pdev);
F
françois romieu 已提交
3150

L
Linus Torvalds 已提交
3151 3152 3153 3154 3155
	if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED) {
		velocity_get_ip(vptr);
		velocity_save_context(vptr, &vptr->context);
		velocity_shutdown(vptr);
		velocity_set_wol(vptr);
3156 3157 3158
		if (vptr->pdev)
			pci_enable_wake(vptr->pdev, PCI_D3hot, 1);
		velocity_set_power_state(vptr, PCI_D3hot);
L
Linus Torvalds 已提交
3159 3160 3161
	} else {
		velocity_save_context(vptr, &vptr->context);
		velocity_shutdown(vptr);
3162 3163 3164
		if (vptr->pdev)
			pci_disable_device(vptr->pdev);
		velocity_set_power_state(vptr, PCI_D3hot);
L
Linus Torvalds 已提交
3165
	}
F
françois romieu 已提交
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 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203
	spin_unlock_irqrestore(&vptr->lock, flags);
	return 0;
}

/**
 *	velocity_restore_context	-	restore registers
 *	@vptr: velocity
 *	@context: buffer for stored context
 *
 *	Reload the register configuration from the velocity context
 *	created by velocity_save_context.
 */
static void velocity_restore_context(struct velocity_info *vptr, struct velocity_context *context)
{
	struct mac_regs __iomem *regs = vptr->mac_regs;
	int i;
	u8 __iomem *ptr = (u8 __iomem *)regs;

	for (i = MAC_REG_PAR; i < MAC_REG_CR0_SET; i += 4)
		writel(*((u32 *) (context->mac_reg + i)), ptr + i);

	/* Just skip cr0 */
	for (i = MAC_REG_CR1_SET; i < MAC_REG_CR0_CLR; i++) {
		/* Clear */
		writeb(~(*((u8 *) (context->mac_reg + i))), ptr + i + 4);
		/* Set */
		writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
	}

	for (i = MAC_REG_MAR; i < MAC_REG_IMR; i += 4)
		writel(*((u32 *) (context->mac_reg + i)), ptr + i);

	for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4)
		writel(*((u32 *) (context->mac_reg + i)), ptr + i);

	for (i = MAC_REG_TDCSR_SET; i <= MAC_REG_RDCSR_SET; i++)
		writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
L
Linus Torvalds 已提交
3204 3205
}

3206
static int velocity_resume(struct device *dev)
L
Linus Torvalds 已提交
3207
{
3208 3209
	struct net_device *netdev = dev_get_drvdata(dev);
	struct velocity_info *vptr = netdev_priv(netdev);
L
Linus Torvalds 已提交
3210 3211 3212
	unsigned long flags;
	int i;

3213
	if (!netif_running(vptr->netdev))
L
Linus Torvalds 已提交
3214 3215
		return 0;

3216 3217 3218
	velocity_set_power_state(vptr, PCI_D0);

	if (vptr->pdev) {
3219
		pci_enable_wake(vptr->pdev, PCI_D0, 0);
3220 3221
		pci_restore_state(vptr->pdev);
	}
L
Linus Torvalds 已提交
3222 3223 3224 3225 3226 3227 3228 3229

	mac_wol_reset(vptr->mac_regs);

	spin_lock_irqsave(&vptr->lock, flags);
	velocity_restore_context(vptr, &vptr->context);
	velocity_init_registers(vptr, VELOCITY_INIT_WOL);
	mac_disable_int(vptr->mac_regs);

3230
	velocity_tx_srv(vptr);
L
Linus Torvalds 已提交
3231

3232
	for (i = 0; i < vptr->tx.numq; i++) {
3233
		if (vptr->tx.used[i])
L
Linus Torvalds 已提交
3234 3235 3236 3237 3238
			mac_tx_queue_wake(vptr->mac_regs, i);
	}

	mac_enable_int(vptr->mac_regs);
	spin_unlock_irqrestore(&vptr->lock, flags);
3239
	netif_device_attach(vptr->netdev);
L
Linus Torvalds 已提交
3240 3241 3242

	return 0;
}
3243 3244 3245
#endif	/* CONFIG_PM_SLEEP */

static SIMPLE_DEV_PM_OPS(velocity_pm_ops, velocity_suspend, velocity_resume);
L
Linus Torvalds 已提交
3246

3247 3248 3249 3250
/*
 *	Definition for our device driver. The PCI layer interface
 *	uses this to handle all our card discover and plugging
 */
3251
static struct pci_driver velocity_pci_driver = {
F
françois romieu 已提交
3252
	.name		= VELOCITY_NAME,
3253 3254 3255 3256 3257 3258
	.id_table	= velocity_pci_id_table,
	.probe		= velocity_pci_probe,
	.remove		= velocity_pci_remove,
	.driver = {
		.pm = &velocity_pm_ops,
	},
3259 3260
};

3261 3262 3263 3264 3265 3266 3267 3268 3269
static struct platform_driver velocity_platform_driver = {
	.probe		= velocity_platform_probe,
	.remove		= velocity_platform_remove,
	.driver = {
		.name = "via-velocity",
		.of_match_table = velocity_of_ids,
		.pm = &velocity_pm_ops,
	},
};
3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281

/**
 *	velocity_ethtool_up	-	pre hook for ethtool
 *	@dev: network device
 *
 *	Called before an ethtool operation. We need to make sure the
 *	chip is out of D3 state before we poke at it.
 */
static int velocity_ethtool_up(struct net_device *dev)
{
	struct velocity_info *vptr = netdev_priv(dev);
	if (!netif_running(dev))
3282
		velocity_set_power_state(vptr, PCI_D0);
3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296
	return 0;
}

/**
 *	velocity_ethtool_down	-	post hook for ethtool
 *	@dev: network device
 *
 *	Called after an ethtool operation. Restore the chip back to D3
 *	state if it isn't running.
 */
static void velocity_ethtool_down(struct net_device *dev)
{
	struct velocity_info *vptr = netdev_priv(dev);
	if (!netif_running(dev))
3297
		velocity_set_power_state(vptr, PCI_D3hot);
3298 3299
}

3300 3301
static int velocity_get_settings(struct net_device *dev,
				 struct ethtool_cmd *cmd)
3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315
{
	struct velocity_info *vptr = netdev_priv(dev);
	struct mac_regs __iomem *regs = vptr->mac_regs;
	u32 status;
	status = check_connection_type(vptr->mac_regs);

	cmd->supported = SUPPORTED_TP |
			SUPPORTED_Autoneg |
			SUPPORTED_10baseT_Half |
			SUPPORTED_10baseT_Full |
			SUPPORTED_100baseT_Half |
			SUPPORTED_100baseT_Full |
			SUPPORTED_1000baseT_Half |
			SUPPORTED_1000baseT_Full;
3316 3317 3318 3319 3320 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

	cmd->advertising = ADVERTISED_TP | ADVERTISED_Autoneg;
	if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
		cmd->advertising |=
			ADVERTISED_10baseT_Half |
			ADVERTISED_10baseT_Full |
			ADVERTISED_100baseT_Half |
			ADVERTISED_100baseT_Full |
			ADVERTISED_1000baseT_Half |
			ADVERTISED_1000baseT_Full;
	} else {
		switch (vptr->options.spd_dpx) {
		case SPD_DPX_1000_FULL:
			cmd->advertising |= ADVERTISED_1000baseT_Full;
			break;
		case SPD_DPX_100_HALF:
			cmd->advertising |= ADVERTISED_100baseT_Half;
			break;
		case SPD_DPX_100_FULL:
			cmd->advertising |= ADVERTISED_100baseT_Full;
			break;
		case SPD_DPX_10_HALF:
			cmd->advertising |= ADVERTISED_10baseT_Half;
			break;
		case SPD_DPX_10_FULL:
			cmd->advertising |= ADVERTISED_10baseT_Full;
			break;
		default:
			break;
		}
	}
3347

3348
	if (status & VELOCITY_SPEED_1000)
3349
		ethtool_cmd_speed_set(cmd, SPEED_1000);
3350
	else if (status & VELOCITY_SPEED_100)
3351
		ethtool_cmd_speed_set(cmd, SPEED_100);
3352
	else
3353 3354
		ethtool_cmd_speed_set(cmd, SPEED_10);

3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367
	cmd->autoneg = (status & VELOCITY_AUTONEG_ENABLE) ? AUTONEG_ENABLE : AUTONEG_DISABLE;
	cmd->port = PORT_TP;
	cmd->transceiver = XCVR_INTERNAL;
	cmd->phy_address = readb(&regs->MIIADR) & 0x1F;

	if (status & VELOCITY_DUPLEX_FULL)
		cmd->duplex = DUPLEX_FULL;
	else
		cmd->duplex = DUPLEX_HALF;

	return 0;
}

3368 3369
static int velocity_set_settings(struct net_device *dev,
				 struct ethtool_cmd *cmd)
3370 3371
{
	struct velocity_info *vptr = netdev_priv(dev);
3372
	u32 speed = ethtool_cmd_speed(cmd);
3373 3374 3375 3376 3377 3378 3379 3380
	u32 curr_status;
	u32 new_status = 0;
	int ret = 0;

	curr_status = check_connection_type(vptr->mac_regs);
	curr_status &= (~VELOCITY_LINK_FAIL);

	new_status |= ((cmd->autoneg) ? VELOCITY_AUTONEG_ENABLE : 0);
3381 3382 3383
	new_status |= ((speed == SPEED_1000) ? VELOCITY_SPEED_1000 : 0);
	new_status |= ((speed == SPEED_100) ? VELOCITY_SPEED_100 : 0);
	new_status |= ((speed == SPEED_10) ? VELOCITY_SPEED_10 : 0);
3384 3385
	new_status |= ((cmd->duplex == DUPLEX_FULL) ? VELOCITY_DUPLEX_FULL : 0);

3386 3387
	if ((new_status & VELOCITY_AUTONEG_ENABLE) &&
	    (new_status != (curr_status | VELOCITY_AUTONEG_ENABLE))) {
3388
		ret = -EINVAL;
3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407
	} else {
		enum speed_opt spd_dpx;

		if (new_status & VELOCITY_AUTONEG_ENABLE)
			spd_dpx = SPD_DPX_AUTO;
		else if ((new_status & VELOCITY_SPEED_1000) &&
			 (new_status & VELOCITY_DUPLEX_FULL)) {
			spd_dpx = SPD_DPX_1000_FULL;
		} else if (new_status & VELOCITY_SPEED_100)
			spd_dpx = (new_status & VELOCITY_DUPLEX_FULL) ?
				SPD_DPX_100_FULL : SPD_DPX_100_HALF;
		else if (new_status & VELOCITY_SPEED_10)
			spd_dpx = (new_status & VELOCITY_DUPLEX_FULL) ?
				SPD_DPX_10_FULL : SPD_DPX_10_HALF;
		else
			return -EOPNOTSUPP;

		vptr->options.spd_dpx = spd_dpx;

3408
		velocity_set_media_mode(vptr, new_status);
3409
	}
3410 3411 3412 3413 3414 3415 3416

	return ret;
}

static void velocity_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
{
	struct velocity_info *vptr = netdev_priv(dev);
3417

3418 3419
	strlcpy(info->driver, VELOCITY_NAME, sizeof(info->driver));
	strlcpy(info->version, VELOCITY_VERSION, sizeof(info->version));
3420 3421 3422 3423 3424
	if (vptr->pdev)
		strlcpy(info->bus_info, pci_name(vptr->pdev),
						sizeof(info->bus_info));
	else
		strlcpy(info->bus_info, "platform", sizeof(info->bus_info));
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 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483
}

static void velocity_ethtool_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
{
	struct velocity_info *vptr = netdev_priv(dev);
	wol->supported = WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP;
	wol->wolopts |= WAKE_MAGIC;
	/*
	   if (vptr->wol_opts & VELOCITY_WOL_PHY)
		   wol.wolopts|=WAKE_PHY;
			 */
	if (vptr->wol_opts & VELOCITY_WOL_UCAST)
		wol->wolopts |= WAKE_UCAST;
	if (vptr->wol_opts & VELOCITY_WOL_ARP)
		wol->wolopts |= WAKE_ARP;
	memcpy(&wol->sopass, vptr->wol_passwd, 6);
}

static int velocity_ethtool_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
{
	struct velocity_info *vptr = netdev_priv(dev);

	if (!(wol->wolopts & (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP)))
		return -EFAULT;
	vptr->wol_opts = VELOCITY_WOL_MAGIC;

	/*
	   if (wol.wolopts & WAKE_PHY) {
	   vptr->wol_opts|=VELOCITY_WOL_PHY;
	   vptr->flags |=VELOCITY_FLAGS_WOL_ENABLED;
	   }
	 */

	if (wol->wolopts & WAKE_MAGIC) {
		vptr->wol_opts |= VELOCITY_WOL_MAGIC;
		vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
	}
	if (wol->wolopts & WAKE_UCAST) {
		vptr->wol_opts |= VELOCITY_WOL_UCAST;
		vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
	}
	if (wol->wolopts & WAKE_ARP) {
		vptr->wol_opts |= VELOCITY_WOL_ARP;
		vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
	}
	memcpy(vptr->wol_passwd, wol->sopass, 6);
	return 0;
}

static u32 velocity_get_msglevel(struct net_device *dev)
{
	return msglevel;
}

static void velocity_set_msglevel(struct net_device *dev, u32 value)
{
	 msglevel = value;
}

3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 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 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545
static int get_pending_timer_val(int val)
{
	int mult_bits = val >> 6;
	int mult = 1;

	switch (mult_bits)
	{
	case 1:
		mult = 4; break;
	case 2:
		mult = 16; break;
	case 3:
		mult = 64; break;
	case 0:
	default:
		break;
	}

	return (val & 0x3f) * mult;
}

static void set_pending_timer_val(int *val, u32 us)
{
	u8 mult = 0;
	u8 shift = 0;

	if (us >= 0x3f) {
		mult = 1; /* mult with 4 */
		shift = 2;
	}
	if (us >= 0x3f * 4) {
		mult = 2; /* mult with 16 */
		shift = 4;
	}
	if (us >= 0x3f * 16) {
		mult = 3; /* mult with 64 */
		shift = 6;
	}

	*val = (mult << 6) | ((us >> shift) & 0x3f);
}


static int velocity_get_coalesce(struct net_device *dev,
		struct ethtool_coalesce *ecmd)
{
	struct velocity_info *vptr = netdev_priv(dev);

	ecmd->tx_max_coalesced_frames = vptr->options.tx_intsup;
	ecmd->rx_max_coalesced_frames = vptr->options.rx_intsup;

	ecmd->rx_coalesce_usecs = get_pending_timer_val(vptr->options.rxqueue_timer);
	ecmd->tx_coalesce_usecs = get_pending_timer_val(vptr->options.txqueue_timer);

	return 0;
}

static int velocity_set_coalesce(struct net_device *dev,
		struct ethtool_coalesce *ecmd)
{
	struct velocity_info *vptr = netdev_priv(dev);
	int max_us = 0x3f * 64;
3546
	unsigned long flags;
3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567

	/* 6 bits of  */
	if (ecmd->tx_coalesce_usecs > max_us)
		return -EINVAL;
	if (ecmd->rx_coalesce_usecs > max_us)
		return -EINVAL;

	if (ecmd->tx_max_coalesced_frames > 0xff)
		return -EINVAL;
	if (ecmd->rx_max_coalesced_frames > 0xff)
		return -EINVAL;

	vptr->options.rx_intsup = ecmd->rx_max_coalesced_frames;
	vptr->options.tx_intsup = ecmd->tx_max_coalesced_frames;

	set_pending_timer_val(&vptr->options.rxqueue_timer,
			ecmd->rx_coalesce_usecs);
	set_pending_timer_val(&vptr->options.txqueue_timer,
			ecmd->tx_coalesce_usecs);

	/* Setup the interrupt suppression and queue timers */
3568
	spin_lock_irqsave(&vptr->lock, flags);
3569 3570 3571 3572 3573 3574 3575
	mac_disable_int(vptr->mac_regs);
	setup_adaptive_interrupts(vptr);
	setup_queue_timers(vptr);

	mac_write_int_mask(vptr->int_mask, vptr->mac_regs);
	mac_clear_isr(vptr->mac_regs);
	mac_enable_int(vptr->mac_regs);
3576
	spin_unlock_irqrestore(&vptr->lock, flags);
3577 3578 3579 3580

	return 0;
}

3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651
static const char velocity_gstrings[][ETH_GSTRING_LEN] = {
	"rx_all",
	"rx_ok",
	"tx_ok",
	"rx_error",
	"rx_runt_ok",
	"rx_runt_err",
	"rx_64",
	"tx_64",
	"rx_65_to_127",
	"tx_65_to_127",
	"rx_128_to_255",
	"tx_128_to_255",
	"rx_256_to_511",
	"tx_256_to_511",
	"rx_512_to_1023",
	"tx_512_to_1023",
	"rx_1024_to_1518",
	"tx_1024_to_1518",
	"tx_ether_collisions",
	"rx_crc_errors",
	"rx_jumbo",
	"tx_jumbo",
	"rx_mac_control_frames",
	"tx_mac_control_frames",
	"rx_frame_alignement_errors",
	"rx_long_ok",
	"rx_long_err",
	"tx_sqe_errors",
	"rx_no_buf",
	"rx_symbol_errors",
	"in_range_length_errors",
	"late_collisions"
};

static void velocity_get_strings(struct net_device *dev, u32 sset, u8 *data)
{
	switch (sset) {
	case ETH_SS_STATS:
		memcpy(data, *velocity_gstrings, sizeof(velocity_gstrings));
		break;
	}
}

static int velocity_get_sset_count(struct net_device *dev, int sset)
{
	switch (sset) {
	case ETH_SS_STATS:
		return ARRAY_SIZE(velocity_gstrings);
	default:
		return -EOPNOTSUPP;
	}
}

static void velocity_get_ethtool_stats(struct net_device *dev,
				       struct ethtool_stats *stats, u64 *data)
{
	if (netif_running(dev)) {
		struct velocity_info *vptr = netdev_priv(dev);
		u32 *p = vptr->mib_counter;
		int i;

		spin_lock_irq(&vptr->lock);
		velocity_update_hw_mibs(vptr);
		spin_unlock_irq(&vptr->lock);

		for (i = 0; i < ARRAY_SIZE(velocity_gstrings); i++)
			*data++ = *p++;
	}
}

3652
static const struct ethtool_ops velocity_ethtool_ops = {
F
françois romieu 已提交
3653 3654 3655 3656 3657 3658 3659 3660
	.get_settings		= velocity_get_settings,
	.set_settings		= velocity_set_settings,
	.get_drvinfo		= velocity_get_drvinfo,
	.get_wol		= velocity_ethtool_get_wol,
	.set_wol		= velocity_ethtool_set_wol,
	.get_msglevel		= velocity_get_msglevel,
	.set_msglevel		= velocity_set_msglevel,
	.get_link		= velocity_get_link,
3661 3662 3663
	.get_strings		= velocity_get_strings,
	.get_sset_count		= velocity_get_sset_count,
	.get_ethtool_stats	= velocity_get_ethtool_stats,
F
françois romieu 已提交
3664 3665 3666 3667
	.get_coalesce		= velocity_get_coalesce,
	.set_coalesce		= velocity_set_coalesce,
	.begin			= velocity_ethtool_up,
	.complete		= velocity_ethtool_down
3668
};
R
Randy Dunlap 已提交
3669

F
françois romieu 已提交
3670
#if defined(CONFIG_PM) && defined(CONFIG_INET)
L
Linus Torvalds 已提交
3671 3672
static int velocity_netdev_event(struct notifier_block *nb, unsigned long notification, void *ptr)
{
F
françois romieu 已提交
3673
	struct in_ifaddr *ifa = ptr;
3674
	struct net_device *dev = ifa->ifa_dev->dev;
L
Linus Torvalds 已提交
3675

3676 3677 3678
	if (dev_net(dev) == &init_net &&
	    dev->netdev_ops == &velocity_netdev_ops)
		velocity_get_ip(netdev_priv(dev));
3679

L
Linus Torvalds 已提交
3680 3681
	return NOTIFY_DONE;
}
R
Randy Dunlap 已提交
3682

3683
static struct notifier_block velocity_inetaddr_notifier = {
F
françois romieu 已提交
3684
	.notifier_call	= velocity_netdev_event,
3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713
};

static void velocity_register_notifier(void)
{
	register_inetaddr_notifier(&velocity_inetaddr_notifier);
}

static void velocity_unregister_notifier(void)
{
	unregister_inetaddr_notifier(&velocity_inetaddr_notifier);
}

#else

#define velocity_register_notifier()	do {} while (0)
#define velocity_unregister_notifier()	do {} while (0)

#endif	/* defined(CONFIG_PM) && defined(CONFIG_INET) */

/**
 *	velocity_init_module	-	load time function
 *
 *	Called when the velocity module is loaded. The PCI driver
 *	is registered with the PCI layer, and in turn will call
 *	the probe functions for each velocity adapter installed
 *	in the system.
 */
static int __init velocity_init_module(void)
{
3714
	int ret_pci, ret_platform;
3715 3716

	velocity_register_notifier();
3717 3718 3719 3720 3721 3722

	ret_pci = pci_register_driver(&velocity_pci_driver);
	ret_platform = platform_driver_register(&velocity_platform_driver);

	/* if both_registers failed, remove the notifier */
	if ((ret_pci < 0) && (ret_platform < 0)) {
3723
		velocity_unregister_notifier();
3724 3725 3726 3727
		return ret_pci;
	}

	return 0;
3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740
}

/**
 *	velocity_cleanup	-	module unload
 *
 *	When the velocity hardware is unloaded this function is called.
 *	It will clean up the notifiers and the unregister the PCI
 *	driver interface for this hardware. This in turn cleans up
 *	all discovered interfaces before returning from the function
 */
static void __exit velocity_cleanup_module(void)
{
	velocity_unregister_notifier();
3741 3742 3743

	pci_unregister_driver(&velocity_pci_driver);
	platform_driver_unregister(&velocity_platform_driver);
3744 3745 3746 3747
}

module_init(velocity_init_module);
module_exit(velocity_cleanup_module);