blob: 5996cee0ffa7d5e31ab357ad556763dea6da798a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This code is derived from the VIA reference driver (copyright message
3 * below) provided to Red Hat by VIA Networking Technologies, Inc. for
4 * addition to the Linux kernel.
5 *
6 * The code has been merged into one source file, cleaned up to follow
7 * Linux coding style, ported to the Linux 2.6 kernel tree and cleaned
8 * for 64bit hardware platforms.
9 *
10 * TODO
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * rx_copybreak/alignment
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * More testing
13 *
Alan Cox113aa832008-10-13 19:01:08 -070014 * The changes are (c) Copyright 2004, Red Hat Inc. <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Additional fixes and clean up: Francois Romieu
16 *
17 * This source has not been verified for use in safety critical systems.
18 *
19 * Please direct queries about the revamped driver to the linux-kernel
20 * list not VIA.
21 *
22 * Original code:
23 *
24 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
25 * All rights reserved.
26 *
27 * This software may be redistributed and/or modified under
28 * the terms of the GNU General Public License as published by the Free
29 * Software Foundation; either version 2 of the License, or
30 * any later version.
31 *
32 * This program is distributed in the hope that it will be useful, but
33 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
34 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
35 * for more details.
36 *
37 * Author: Chuang Liang-Shing, AJ Jiang
38 *
39 * Date: Jan 24, 2003
40 *
41 * MODULE_LICENSE("GPL");
42 *
43 */
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/module.h>
46#include <linux/types.h>
Jiri Pirko73b54682011-07-20 04:54:30 +000047#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/init.h>
Tony Priske2c41f12013-05-18 09:39:06 +000049#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/mm.h>
51#include <linux/errno.h>
52#include <linux/ioport.h>
53#include <linux/pci.h>
54#include <linux/kernel.h>
55#include <linux/netdevice.h>
56#include <linux/etherdevice.h>
57#include <linux/skbuff.h>
58#include <linux/delay.h>
59#include <linux/timer.h>
60#include <linux/slab.h>
61#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/string.h>
63#include <linux/wait.h>
Dave Jonesc4067402009-07-20 17:35:21 +000064#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/if.h>
Dave Jonesc4067402009-07-20 17:35:21 +000066#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/proc_fs.h>
68#include <linux/inetdevice.h>
69#include <linux/reboot.h>
70#include <linux/ethtool.h>
71#include <linux/mii.h>
72#include <linux/in.h>
73#include <linux/if_arp.h>
Stephen Hemminger501e4d22007-08-24 13:56:49 -070074#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <linux/ip.h>
76#include <linux/tcp.h>
77#include <linux/udp.h>
78#include <linux/crc-ccitt.h>
79#include <linux/crc32.h>
80
81#include "via-velocity.h"
82
83
Dave Jonesc4067402009-07-20 17:35:21 +000084static int velocity_nics;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static int msglevel = MSG_LEVEL_INFO;
86
Stephen Hemminger01faccb2007-08-24 14:40:45 -070087/**
88 * mac_get_cam_mask - Read a CAM mask
89 * @regs: register block for this velocity
90 * @mask: buffer to store mask
91 *
92 * Fetch the mask bits of the selected CAM and store them into the
93 * provided mask buffer.
94 */
Dave Jonesc4067402009-07-20 17:35:21 +000095static void mac_get_cam_mask(struct mac_regs __iomem *regs, u8 *mask)
Stephen Hemminger01faccb2007-08-24 14:40:45 -070096{
97 int i;
98
99 /* Select CAM mask */
100 BYTE_REG_BITS_SET(CAMCR_PS_CAM_MASK, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
101
102 writeb(0, &regs->CAMADDR);
103
104 /* read mask */
105 for (i = 0; i < 8; i++)
106 *mask++ = readb(&(regs->MARCAM[i]));
107
108 /* disable CAMEN */
109 writeb(0, &regs->CAMADDR);
110
111 /* Select mar */
112 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700113}
114
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700115/**
116 * mac_set_cam_mask - Set a CAM mask
117 * @regs: register block for this velocity
118 * @mask: CAM mask to load
119 *
120 * Store a new mask into a CAM
121 */
Dave Jonesc4067402009-07-20 17:35:21 +0000122static void mac_set_cam_mask(struct mac_regs __iomem *regs, u8 *mask)
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700123{
124 int i;
125 /* Select CAM mask */
126 BYTE_REG_BITS_SET(CAMCR_PS_CAM_MASK, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
127
128 writeb(CAMADDR_CAMEN, &regs->CAMADDR);
129
Dave Jonesc4067402009-07-20 17:35:21 +0000130 for (i = 0; i < 8; i++)
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700131 writeb(*mask++, &(regs->MARCAM[i]));
Dave Jonesc4067402009-07-20 17:35:21 +0000132
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700133 /* disable CAMEN */
134 writeb(0, &regs->CAMADDR);
135
136 /* Select mar */
137 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
138}
139
Dave Jonesc4067402009-07-20 17:35:21 +0000140static void mac_set_vlan_cam_mask(struct mac_regs __iomem *regs, u8 *mask)
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700141{
142 int i;
143 /* Select CAM mask */
144 BYTE_REG_BITS_SET(CAMCR_PS_CAM_MASK, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
145
146 writeb(CAMADDR_CAMEN | CAMADDR_VCAMSL, &regs->CAMADDR);
147
Dave Jonesc4067402009-07-20 17:35:21 +0000148 for (i = 0; i < 8; i++)
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700149 writeb(*mask++, &(regs->MARCAM[i]));
Dave Jonesc4067402009-07-20 17:35:21 +0000150
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700151 /* disable CAMEN */
152 writeb(0, &regs->CAMADDR);
153
154 /* Select mar */
155 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
156}
157
158/**
159 * mac_set_cam - set CAM data
160 * @regs: register block of this velocity
161 * @idx: Cam index
162 * @addr: 2 or 6 bytes of CAM data
163 *
164 * Load an address or vlan tag into a CAM
165 */
Dave Jonesc4067402009-07-20 17:35:21 +0000166static void mac_set_cam(struct mac_regs __iomem *regs, int idx, const u8 *addr)
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700167{
168 int i;
169
170 /* Select CAM mask */
171 BYTE_REG_BITS_SET(CAMCR_PS_CAM_DATA, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
172
173 idx &= (64 - 1);
174
175 writeb(CAMADDR_CAMEN | idx, &regs->CAMADDR);
176
Dave Jonesc4067402009-07-20 17:35:21 +0000177 for (i = 0; i < 6; i++)
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700178 writeb(*addr++, &(regs->MARCAM[i]));
Dave Jonesc4067402009-07-20 17:35:21 +0000179
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700180 BYTE_REG_BITS_ON(CAMCR_CAMWR, &regs->CAMCR);
181
182 udelay(10);
183
184 writeb(0, &regs->CAMADDR);
185
186 /* Select mar */
187 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
188}
189
Dave Jonesc4067402009-07-20 17:35:21 +0000190static void mac_set_vlan_cam(struct mac_regs __iomem *regs, int idx,
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700191 const u8 *addr)
192{
193
194 /* Select CAM mask */
195 BYTE_REG_BITS_SET(CAMCR_PS_CAM_DATA, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
196
197 idx &= (64 - 1);
198
199 writeb(CAMADDR_CAMEN | CAMADDR_VCAMSL | idx, &regs->CAMADDR);
200 writew(*((u16 *) addr), &regs->MARCAM[0]);
201
202 BYTE_REG_BITS_ON(CAMCR_CAMWR, &regs->CAMCR);
203
204 udelay(10);
205
206 writeb(0, &regs->CAMADDR);
207
208 /* Select mar */
209 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
210}
211
212
213/**
214 * mac_wol_reset - reset WOL after exiting low power
215 * @regs: register block of this velocity
216 *
217 * Called after we drop out of wake on lan mode in order to
218 * reset the Wake on lan features. This function doesn't restore
219 * the rest of the logic from the result of sleep/wakeup
220 */
Dave Jonesc4067402009-07-20 17:35:21 +0000221static void mac_wol_reset(struct mac_regs __iomem *regs)
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700222{
223
224 /* Turn off SWPTAG right after leaving power mode */
225 BYTE_REG_BITS_OFF(STICKHW_SWPTAG, &regs->STICKHW);
226 /* clear sticky bits */
227 BYTE_REG_BITS_OFF((STICKHW_DS1 | STICKHW_DS0), &regs->STICKHW);
228
229 BYTE_REG_BITS_OFF(CHIPGCR_FCGMII, &regs->CHIPGCR);
230 BYTE_REG_BITS_OFF(CHIPGCR_FCMODE, &regs->CHIPGCR);
231 /* disable force PME-enable */
232 writeb(WOLCFG_PMEOVR, &regs->WOLCFGClr);
233 /* disable power-event config bit */
234 writew(0xFFFF, &regs->WOLCRClr);
235 /* clear power status */
236 writew(0xFFFF, &regs->WOLSRClr);
237}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Jeff Garzik7282d492006-09-13 14:30:00 -0400239static const struct ethtool_ops velocity_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241/*
242 Define module options
243*/
244
245MODULE_AUTHOR("VIA Networking Technologies, Inc.");
246MODULE_LICENSE("GPL");
247MODULE_DESCRIPTION("VIA Networking Velocity Family Gigabit Ethernet Adapter Driver");
248
Dave Jonesc4067402009-07-20 17:35:21 +0000249#define VELOCITY_PARAM(N, D) \
250 static int N[MAX_UNITS] = OPTION_DEFAULT;\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 module_param_array(N, int, NULL, 0); \
Dave Jonesc4067402009-07-20 17:35:21 +0000252 MODULE_PARM_DESC(N, D);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254#define RX_DESC_MIN 64
255#define RX_DESC_MAX 255
256#define RX_DESC_DEF 64
257VELOCITY_PARAM(RxDescriptors, "Number of receive descriptors");
258
259#define TX_DESC_MIN 16
260#define TX_DESC_MAX 256
261#define TX_DESC_DEF 64
262VELOCITY_PARAM(TxDescriptors, "Number of transmit descriptors");
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264#define RX_THRESH_MIN 0
265#define RX_THRESH_MAX 3
266#define RX_THRESH_DEF 0
267/* rx_thresh[] is used for controlling the receive fifo threshold.
268 0: indicate the rxfifo threshold is 128 bytes.
269 1: indicate the rxfifo threshold is 512 bytes.
270 2: indicate the rxfifo threshold is 1024 bytes.
271 3: indicate the rxfifo threshold is store & forward.
272*/
273VELOCITY_PARAM(rx_thresh, "Receive fifo threshold");
274
275#define DMA_LENGTH_MIN 0
276#define DMA_LENGTH_MAX 7
Simon Kagstrom2a5774f2009-11-25 22:10:34 +0000277#define DMA_LENGTH_DEF 6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279/* DMA_length[] is used for controlling the DMA length
280 0: 8 DWORDs
281 1: 16 DWORDs
282 2: 32 DWORDs
283 3: 64 DWORDs
284 4: 128 DWORDs
285 5: 256 DWORDs
286 6: SF(flush till emply)
287 7: SF(flush till emply)
288*/
289VELOCITY_PARAM(DMA_length, "DMA length");
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291#define IP_ALIG_DEF 0
292/* IP_byte_align[] is used for IP header DWORD byte aligned
293 0: indicate the IP header won't be DWORD byte aligned.(Default) .
294 1: indicate the IP header will be DWORD byte aligned.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300295 In some environment, the IP header should be DWORD byte aligned,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 or the packet will be droped when we receive it. (eg: IPVS)
297*/
298VELOCITY_PARAM(IP_byte_align, "Enable IP header dword aligned");
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300#define FLOW_CNTL_DEF 1
301#define FLOW_CNTL_MIN 1
302#define FLOW_CNTL_MAX 5
303
304/* flow_control[] is used for setting the flow control ability of NIC.
305 1: hardware deafult - AUTO (default). Use Hardware default value in ANAR.
306 2: enable TX flow control.
307 3: enable RX flow control.
308 4: enable RX/TX flow control.
309 5: disable
310*/
311VELOCITY_PARAM(flow_control, "Enable flow control ability");
312
313#define MED_LNK_DEF 0
314#define MED_LNK_MIN 0
françois romieu15419222010-10-13 09:26:05 +0000315#define MED_LNK_MAX 5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316/* speed_duplex[] is used for setting the speed and duplex mode of NIC.
317 0: indicate autonegotiation for both speed and duplex mode
318 1: indicate 100Mbps half duplex mode
319 2: indicate 100Mbps full duplex mode
320 3: indicate 10Mbps half duplex mode
321 4: indicate 10Mbps full duplex mode
françois romieu15419222010-10-13 09:26:05 +0000322 5: indicate 1000Mbps full duplex mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 Note:
Dave Jonesc4067402009-07-20 17:35:21 +0000325 if EEPROM have been set to the force mode, this option is ignored
326 by driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327*/
328VELOCITY_PARAM(speed_duplex, "Setting the speed and duplex mode");
329
330#define VAL_PKT_LEN_DEF 0
331/* ValPktLen[] is used for setting the checksum offload ability of NIC.
332 0: Receive frame with invalid layer 2 length (Default)
333 1: Drop frame with invalid layer 2 length
334*/
335VELOCITY_PARAM(ValPktLen, "Receiving or Drop invalid 802.3 frame");
336
337#define WOL_OPT_DEF 0
338#define WOL_OPT_MIN 0
339#define WOL_OPT_MAX 7
340/* wol_opts[] is used for controlling wake on lan behavior.
341 0: Wake up if recevied a magic packet. (Default)
342 1: Wake up if link status is on/off.
343 2: Wake up if recevied an arp packet.
344 4: Wake up if recevied any unicast packet.
345 Those value can be sumed up to support more than one option.
346*/
347VELOCITY_PARAM(wol_opts, "Wake On Lan options");
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349static int rx_copybreak = 200;
350module_param(rx_copybreak, int, 0644);
351MODULE_PARM_DESC(rx_copybreak, "Copy breakpoint for copy-only-tiny-frames");
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353/*
354 * Internal board variants. At the moment we have only one
355 */
Andrew Morton4f14b922008-02-09 23:41:40 -0800356static struct velocity_info_tbl chip_info_table[] = {
Jeff Garzikcabb7662006-06-27 09:25:28 -0400357 {CHIP_TYPE_VT6110, "VIA Networking Velocity Family Gigabit Ethernet Adapter", 1, 0x00FFFFFFUL},
358 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359};
360
361/*
362 * Describe the PCI device identifiers that we support in this
363 * device driver. Used for hotplug autoloading.
364 */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000365static DEFINE_PCI_DEVICE_TABLE(velocity_id_table) = {
Jeff Garzike54f4892006-06-27 09:20:08 -0400366 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_612X) },
367 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368};
369
370MODULE_DEVICE_TABLE(pci, velocity_id_table);
371
372/**
373 * get_chip_name - identifier to name
374 * @id: chip identifier
375 *
376 * Given a chip identifier return a suitable description. Returns
377 * a pointer a static string valid while the driver is loaded.
378 */
Bill Pemberton27add002012-12-03 09:23:49 -0500379static const char *get_chip_name(enum chip_type chip_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 int i;
382 for (i = 0; chip_info_table[i].name != NULL; i++)
383 if (chip_info_table[i].chip_id == chip_id)
384 break;
385 return chip_info_table[i].name;
386}
387
388/**
389 * velocity_remove1 - device unplug
390 * @pdev: PCI device being removed
391 *
392 * Device unload callback. Called on an unplug or on module
393 * unload for each active device that is present. Disconnects
394 * the device from the network layer and frees all the resources
395 */
Bill Pemberton27add002012-12-03 09:23:49 -0500396static void velocity_remove1(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 struct net_device *dev = pci_get_drvdata(pdev);
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -0400399 struct velocity_info *vptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 unregister_netdev(dev);
402 iounmap(vptr->mac_regs);
403 pci_release_regions(pdev);
404 pci_disable_device(pdev);
405 pci_set_drvdata(pdev, NULL);
406 free_netdev(dev);
407
408 velocity_nics--;
409}
410
411/**
412 * velocity_set_int_opt - parser for integer options
413 * @opt: pointer to option value
414 * @val: value the user requested (or -1 for default)
415 * @min: lowest value allowed
416 * @max: highest value allowed
417 * @def: default value
418 * @name: property name
419 * @dev: device name
420 *
421 * Set an integer property in the module options. This function does
422 * all the verification and checking as well as reporting so that
423 * we don't duplicate code for each option.
424 */
Bill Pemberton27add002012-12-03 09:23:49 -0500425static void velocity_set_int_opt(int *opt, int val, int min, int max, int def,
426 char *name, const char *devname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 if (val == -1)
429 *opt = def;
430 else if (val < min || val > max) {
431 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: the value of parameter %s is invalid, the valid range is (%d-%d)\n",
432 devname, name, min, max);
433 *opt = def;
434 } else {
435 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_INFO "%s: set value of parameter %s to %d\n",
436 devname, name, val);
437 *opt = val;
438 }
439}
440
441/**
442 * velocity_set_bool_opt - parser for boolean options
443 * @opt: pointer to option value
444 * @val: value the user requested (or -1 for default)
445 * @def: default value (yes/no)
446 * @flag: numeric value to set for true.
447 * @name: property name
448 * @dev: device name
449 *
450 * Set a boolean property in the module options. This function does
451 * all the verification and checking as well as reporting so that
452 * we don't duplicate code for each option.
453 */
Bill Pemberton27add002012-12-03 09:23:49 -0500454static void velocity_set_bool_opt(u32 *opt, int val, int def, u32 flag,
455 char *name, const char *devname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 (*opt) &= (~flag);
458 if (val == -1)
459 *opt |= (def ? flag : 0);
460 else if (val < 0 || val > 1) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400461 printk(KERN_NOTICE "%s: the value of parameter %s is invalid, the valid range is (0-1)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 devname, name);
463 *opt |= (def ? flag : 0);
464 } else {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400465 printk(KERN_INFO "%s: set parameter %s to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 devname, name, val ? "TRUE" : "FALSE");
467 *opt |= (val ? flag : 0);
468 }
469}
470
471/**
472 * velocity_get_options - set options on device
473 * @opts: option structure for the device
474 * @index: index of option to use in module options array
475 * @devname: device name
476 *
477 * Turn the module and command options into a single structure
478 * for the current device
479 */
Bill Pemberton27add002012-12-03 09:23:49 -0500480static void velocity_get_options(struct velocity_opt *opts, int index,
481 const char *devname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483
484 velocity_set_int_opt(&opts->rx_thresh, rx_thresh[index], RX_THRESH_MIN, RX_THRESH_MAX, RX_THRESH_DEF, "rx_thresh", devname);
485 velocity_set_int_opt(&opts->DMA_length, DMA_length[index], DMA_LENGTH_MIN, DMA_LENGTH_MAX, DMA_LENGTH_DEF, "DMA_length", devname);
486 velocity_set_int_opt(&opts->numrx, RxDescriptors[index], RX_DESC_MIN, RX_DESC_MAX, RX_DESC_DEF, "RxDescriptors", devname);
487 velocity_set_int_opt(&opts->numtx, TxDescriptors[index], TX_DESC_MIN, TX_DESC_MAX, TX_DESC_DEF, "TxDescriptors", devname);
Stephen Hemminger501e4d22007-08-24 13:56:49 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 velocity_set_int_opt(&opts->flow_cntl, flow_control[index], FLOW_CNTL_MIN, FLOW_CNTL_MAX, FLOW_CNTL_DEF, "flow_control", devname);
490 velocity_set_bool_opt(&opts->flags, IP_byte_align[index], IP_ALIG_DEF, VELOCITY_FLAGS_IP_ALIGN, "IP_byte_align", devname);
491 velocity_set_bool_opt(&opts->flags, ValPktLen[index], VAL_PKT_LEN_DEF, VELOCITY_FLAGS_VAL_PKT_LEN, "ValPktLen", devname);
492 velocity_set_int_opt((int *) &opts->spd_dpx, speed_duplex[index], MED_LNK_MIN, MED_LNK_MAX, MED_LNK_DEF, "Media link mode", devname);
Joe Perches64699332012-06-04 12:44:16 +0000493 velocity_set_int_opt(&opts->wol_opts, wol_opts[index], WOL_OPT_MIN, WOL_OPT_MAX, WOL_OPT_DEF, "Wake On Lan options", devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 opts->numrx = (opts->numrx & ~3);
495}
496
497/**
498 * velocity_init_cam_filter - initialise CAM
499 * @vptr: velocity to program
500 *
501 * Initialize the content addressable memory used for filters. Load
502 * appropriately according to the presence of VLAN
503 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504static void velocity_init_cam_filter(struct velocity_info *vptr)
505{
Dave Jonesc4067402009-07-20 17:35:21 +0000506 struct mac_regs __iomem *regs = vptr->mac_regs;
Jiri Pirko73b54682011-07-20 04:54:30 +0000507 unsigned int vid, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 /* Turn on MCFG_PQEN, turn off MCFG_RTGOPT */
510 WORD_REG_BITS_SET(MCFG_PQEN, MCFG_RTGOPT, &regs->MCFG);
511 WORD_REG_BITS_ON(MCFG_VIDFR, &regs->MCFG);
512
513 /* Disable all CAMs */
514 memset(vptr->vCAMmask, 0, sizeof(u8) * 8);
515 memset(vptr->mCAMmask, 0, sizeof(u8) * 8);
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700516 mac_set_vlan_cam_mask(regs, vptr->vCAMmask);
517 mac_set_cam_mask(regs, vptr->mCAMmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Francois Romieud4f73c82008-04-24 23:32:33 +0200519 /* Enable VCAMs */
Jiri Pirko73b54682011-07-20 04:54:30 +0000520 for_each_set_bit(vid, vptr->active_vlans, VLAN_N_VID) {
521 mac_set_vlan_cam(regs, i, (u8 *) &vid);
522 vptr->vCAMmask[i / 8] |= 0x1 << (i % 8);
523 if (++i >= VCAM_SIZE)
524 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
Jiri Pirko73b54682011-07-20 04:54:30 +0000526 mac_set_vlan_cam_mask(regs, vptr->vCAMmask);
Francois Romieud4f73c82008-04-24 23:32:33 +0200527}
528
Patrick McHardy80d5c362013-04-19 02:04:28 +0000529static int velocity_vlan_rx_add_vid(struct net_device *dev,
530 __be16 proto, u16 vid)
Stephen Hemminger501e4d22007-08-24 13:56:49 -0700531{
532 struct velocity_info *vptr = netdev_priv(dev);
533
Dave Jonesc4067402009-07-20 17:35:21 +0000534 spin_lock_irq(&vptr->lock);
Jiri Pirko73b54682011-07-20 04:54:30 +0000535 set_bit(vid, vptr->active_vlans);
Stephen Hemminger501e4d22007-08-24 13:56:49 -0700536 velocity_init_cam_filter(vptr);
Dave Jonesc4067402009-07-20 17:35:21 +0000537 spin_unlock_irq(&vptr->lock);
Jiri Pirko8e586132011-12-08 19:52:37 -0500538 return 0;
Stephen Hemminger501e4d22007-08-24 13:56:49 -0700539}
540
Patrick McHardy80d5c362013-04-19 02:04:28 +0000541static int velocity_vlan_rx_kill_vid(struct net_device *dev,
542 __be16 proto, u16 vid)
Stephen Hemminger501e4d22007-08-24 13:56:49 -0700543{
544 struct velocity_info *vptr = netdev_priv(dev);
545
Dave Jonesc4067402009-07-20 17:35:21 +0000546 spin_lock_irq(&vptr->lock);
Jiri Pirko73b54682011-07-20 04:54:30 +0000547 clear_bit(vid, vptr->active_vlans);
Stephen Hemminger501e4d22007-08-24 13:56:49 -0700548 velocity_init_cam_filter(vptr);
Dave Jonesc4067402009-07-20 17:35:21 +0000549 spin_unlock_irq(&vptr->lock);
Jiri Pirko8e586132011-12-08 19:52:37 -0500550 return 0;
Stephen Hemminger501e4d22007-08-24 13:56:49 -0700551}
552
Francois Romieu3c4dc712008-07-31 22:51:18 +0200553static void velocity_init_rx_ring_indexes(struct velocity_info *vptr)
554{
555 vptr->rx.dirty = vptr->rx.filled = vptr->rx.curr = 0;
556}
Stephen Hemminger501e4d22007-08-24 13:56:49 -0700557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558/**
559 * velocity_rx_reset - handle a receive reset
560 * @vptr: velocity we are resetting
561 *
562 * Reset the ownership and status for the receive ring side.
563 * Hand all the receive queue to the NIC.
564 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565static void velocity_rx_reset(struct velocity_info *vptr)
566{
567
Dave Jonesc4067402009-07-20 17:35:21 +0000568 struct mac_regs __iomem *regs = vptr->mac_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 int i;
570
Francois Romieu3c4dc712008-07-31 22:51:18 +0200571 velocity_init_rx_ring_indexes(vptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 /*
574 * Init state, all RD entries belong to the NIC
575 */
576 for (i = 0; i < vptr->options.numrx; ++i)
Francois Romieu0fe9f152008-07-31 22:10:10 +0200577 vptr->rx.ring[i].rdesc0.len |= OWNED_BY_NIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 writew(vptr->options.numrx, &regs->RBRDU);
Francois Romieu0fe9f152008-07-31 22:10:10 +0200580 writel(vptr->rx.pool_dma, &regs->RDBaseLo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 writew(0, &regs->RDIdx);
582 writew(vptr->options.numrx - 1, &regs->RDCSize);
583}
584
585/**
Dave Jones2cf71d22009-07-23 18:11:12 -0700586 * velocity_get_opt_media_mode - get media selection
587 * @vptr: velocity adapter
588 *
589 * Get the media mode stored in EEPROM or module options and load
590 * mii_status accordingly. The requested link state information
591 * is also returned.
592 */
593static u32 velocity_get_opt_media_mode(struct velocity_info *vptr)
594{
595 u32 status = 0;
596
597 switch (vptr->options.spd_dpx) {
598 case SPD_DPX_AUTO:
599 status = VELOCITY_AUTONEG_ENABLE;
600 break;
601 case SPD_DPX_100_FULL:
602 status = VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL;
603 break;
604 case SPD_DPX_10_FULL:
605 status = VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL;
606 break;
607 case SPD_DPX_100_HALF:
608 status = VELOCITY_SPEED_100;
609 break;
610 case SPD_DPX_10_HALF:
611 status = VELOCITY_SPEED_10;
612 break;
françois romieu15419222010-10-13 09:26:05 +0000613 case SPD_DPX_1000_FULL:
614 status = VELOCITY_SPEED_1000 | VELOCITY_DUPLEX_FULL;
615 break;
Dave Jones2cf71d22009-07-23 18:11:12 -0700616 }
617 vptr->mii_status = status;
618 return status;
619}
620
621/**
622 * safe_disable_mii_autopoll - autopoll off
623 * @regs: velocity registers
624 *
625 * Turn off the autopoll and wait for it to disable on the chip
626 */
627static void safe_disable_mii_autopoll(struct mac_regs __iomem *regs)
628{
629 u16 ww;
630
631 /* turn off MAUTO */
632 writeb(0, &regs->MIICR);
633 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
634 udelay(1);
635 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
636 break;
637 }
638}
639
640/**
641 * enable_mii_autopoll - turn on autopolling
642 * @regs: velocity registers
643 *
644 * Enable the MII link status autopoll feature on the Velocity
645 * hardware. Wait for it to enable.
646 */
647static void enable_mii_autopoll(struct mac_regs __iomem *regs)
648{
649 int ii;
650
651 writeb(0, &(regs->MIICR));
652 writeb(MIIADR_SWMPL, &regs->MIIADR);
653
654 for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
655 udelay(1);
656 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
657 break;
658 }
659
660 writeb(MIICR_MAUTO, &regs->MIICR);
661
662 for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
663 udelay(1);
664 if (!BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
665 break;
666 }
667
668}
669
670/**
671 * velocity_mii_read - read MII data
672 * @regs: velocity registers
673 * @index: MII register index
674 * @data: buffer for received data
675 *
676 * Perform a single read of an MII 16bit register. Returns zero
677 * on success or -ETIMEDOUT if the PHY did not respond.
678 */
679static int velocity_mii_read(struct mac_regs __iomem *regs, u8 index, u16 *data)
680{
681 u16 ww;
682
683 /*
684 * Disable MIICR_MAUTO, so that mii addr can be set normally
685 */
686 safe_disable_mii_autopoll(regs);
687
688 writeb(index, &regs->MIIADR);
689
690 BYTE_REG_BITS_ON(MIICR_RCMD, &regs->MIICR);
691
692 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
693 if (!(readb(&regs->MIICR) & MIICR_RCMD))
694 break;
695 }
696
697 *data = readw(&regs->MIIDATA);
698
699 enable_mii_autopoll(regs);
700 if (ww == W_MAX_TIMEOUT)
701 return -ETIMEDOUT;
702 return 0;
703}
704
Dave Jones2cf71d22009-07-23 18:11:12 -0700705/**
706 * mii_check_media_mode - check media state
707 * @regs: velocity registers
708 *
709 * Check the current MII status and determine the link status
710 * accordingly
711 */
712static u32 mii_check_media_mode(struct mac_regs __iomem *regs)
713{
714 u32 status = 0;
715 u16 ANAR;
716
Francois Romieu3a7f8682010-04-06 14:24:53 -0700717 if (!MII_REG_BITS_IS_ON(BMSR_LSTATUS, MII_BMSR, regs))
Dave Jones2cf71d22009-07-23 18:11:12 -0700718 status |= VELOCITY_LINK_FAIL;
719
Francois Romieu3a7f8682010-04-06 14:24:53 -0700720 if (MII_REG_BITS_IS_ON(ADVERTISE_1000FULL, MII_CTRL1000, regs))
Dave Jones2cf71d22009-07-23 18:11:12 -0700721 status |= VELOCITY_SPEED_1000 | VELOCITY_DUPLEX_FULL;
Francois Romieu3a7f8682010-04-06 14:24:53 -0700722 else if (MII_REG_BITS_IS_ON(ADVERTISE_1000HALF, MII_CTRL1000, regs))
Dave Jones2cf71d22009-07-23 18:11:12 -0700723 status |= (VELOCITY_SPEED_1000);
724 else {
Francois Romieu3a7f8682010-04-06 14:24:53 -0700725 velocity_mii_read(regs, MII_ADVERTISE, &ANAR);
726 if (ANAR & ADVERTISE_100FULL)
Dave Jones2cf71d22009-07-23 18:11:12 -0700727 status |= (VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL);
Francois Romieu3a7f8682010-04-06 14:24:53 -0700728 else if (ANAR & ADVERTISE_100HALF)
Dave Jones2cf71d22009-07-23 18:11:12 -0700729 status |= VELOCITY_SPEED_100;
Francois Romieu3a7f8682010-04-06 14:24:53 -0700730 else if (ANAR & ADVERTISE_10FULL)
Dave Jones2cf71d22009-07-23 18:11:12 -0700731 status |= (VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL);
732 else
733 status |= (VELOCITY_SPEED_10);
734 }
735
Francois Romieu3a7f8682010-04-06 14:24:53 -0700736 if (MII_REG_BITS_IS_ON(BMCR_ANENABLE, MII_BMCR, regs)) {
737 velocity_mii_read(regs, MII_ADVERTISE, &ANAR);
738 if ((ANAR & (ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF))
739 == (ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF)) {
740 if (MII_REG_BITS_IS_ON(ADVERTISE_1000HALF | ADVERTISE_1000FULL, MII_CTRL1000, regs))
Dave Jones2cf71d22009-07-23 18:11:12 -0700741 status |= VELOCITY_AUTONEG_ENABLE;
742 }
743 }
744
745 return status;
746}
747
748/**
749 * velocity_mii_write - write MII data
750 * @regs: velocity registers
751 * @index: MII register index
752 * @data: 16bit data for the MII register
753 *
754 * Perform a single write to an MII 16bit register. Returns zero
755 * on success or -ETIMEDOUT if the PHY did not respond.
756 */
757static int velocity_mii_write(struct mac_regs __iomem *regs, u8 mii_addr, u16 data)
758{
759 u16 ww;
760
761 /*
762 * Disable MIICR_MAUTO, so that mii addr can be set normally
763 */
764 safe_disable_mii_autopoll(regs);
765
766 /* MII reg offset */
767 writeb(mii_addr, &regs->MIIADR);
768 /* set MII data */
769 writew(data, &regs->MIIDATA);
770
771 /* turn on MIICR_WCMD */
772 BYTE_REG_BITS_ON(MIICR_WCMD, &regs->MIICR);
773
774 /* W_MAX_TIMEOUT is the timeout period */
775 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
776 udelay(5);
777 if (!(readb(&regs->MIICR) & MIICR_WCMD))
778 break;
779 }
780 enable_mii_autopoll(regs);
781
782 if (ww == W_MAX_TIMEOUT)
783 return -ETIMEDOUT;
784 return 0;
785}
786
787/**
788 * set_mii_flow_control - flow control setup
789 * @vptr: velocity interface
790 *
791 * Set up the flow control on this interface according to
792 * the supplied user/eeprom options.
793 */
794static void set_mii_flow_control(struct velocity_info *vptr)
795{
796 /*Enable or Disable PAUSE in ANAR */
797 switch (vptr->options.flow_cntl) {
798 case FLOW_CNTL_TX:
Francois Romieu3a7f8682010-04-06 14:24:53 -0700799 MII_REG_BITS_OFF(ADVERTISE_PAUSE_CAP, MII_ADVERTISE, vptr->mac_regs);
800 MII_REG_BITS_ON(ADVERTISE_PAUSE_ASYM, MII_ADVERTISE, vptr->mac_regs);
Dave Jones2cf71d22009-07-23 18:11:12 -0700801 break;
802
803 case FLOW_CNTL_RX:
Francois Romieu3a7f8682010-04-06 14:24:53 -0700804 MII_REG_BITS_ON(ADVERTISE_PAUSE_CAP, MII_ADVERTISE, vptr->mac_regs);
805 MII_REG_BITS_ON(ADVERTISE_PAUSE_ASYM, MII_ADVERTISE, vptr->mac_regs);
Dave Jones2cf71d22009-07-23 18:11:12 -0700806 break;
807
808 case FLOW_CNTL_TX_RX:
Francois Romieu3a7f8682010-04-06 14:24:53 -0700809 MII_REG_BITS_ON(ADVERTISE_PAUSE_CAP, MII_ADVERTISE, vptr->mac_regs);
David S. Miller4a35ecf2010-04-06 23:53:30 -0700810 MII_REG_BITS_OFF(ADVERTISE_PAUSE_ASYM, MII_ADVERTISE, vptr->mac_regs);
Dave Jones2cf71d22009-07-23 18:11:12 -0700811 break;
812
813 case FLOW_CNTL_DISABLE:
Francois Romieu3a7f8682010-04-06 14:24:53 -0700814 MII_REG_BITS_OFF(ADVERTISE_PAUSE_CAP, MII_ADVERTISE, vptr->mac_regs);
815 MII_REG_BITS_OFF(ADVERTISE_PAUSE_ASYM, MII_ADVERTISE, vptr->mac_regs);
Dave Jones2cf71d22009-07-23 18:11:12 -0700816 break;
817 default:
818 break;
819 }
820}
821
822/**
823 * mii_set_auto_on - autonegotiate on
824 * @vptr: velocity
825 *
826 * Enable autonegotation on this interface
827 */
828static void mii_set_auto_on(struct velocity_info *vptr)
829{
Francois Romieu3a7f8682010-04-06 14:24:53 -0700830 if (MII_REG_BITS_IS_ON(BMCR_ANENABLE, MII_BMCR, vptr->mac_regs))
831 MII_REG_BITS_ON(BMCR_ANRESTART, MII_BMCR, vptr->mac_regs);
Dave Jones2cf71d22009-07-23 18:11:12 -0700832 else
Francois Romieu3a7f8682010-04-06 14:24:53 -0700833 MII_REG_BITS_ON(BMCR_ANENABLE, MII_BMCR, vptr->mac_regs);
Dave Jones2cf71d22009-07-23 18:11:12 -0700834}
835
836static u32 check_connection_type(struct mac_regs __iomem *regs)
837{
838 u32 status = 0;
839 u8 PHYSR0;
840 u16 ANAR;
841 PHYSR0 = readb(&regs->PHYSR0);
842
843 /*
844 if (!(PHYSR0 & PHYSR0_LINKGD))
845 status|=VELOCITY_LINK_FAIL;
846 */
847
848 if (PHYSR0 & PHYSR0_FDPX)
849 status |= VELOCITY_DUPLEX_FULL;
850
851 if (PHYSR0 & PHYSR0_SPDG)
852 status |= VELOCITY_SPEED_1000;
853 else if (PHYSR0 & PHYSR0_SPD10)
854 status |= VELOCITY_SPEED_10;
855 else
856 status |= VELOCITY_SPEED_100;
857
Francois Romieu3a7f8682010-04-06 14:24:53 -0700858 if (MII_REG_BITS_IS_ON(BMCR_ANENABLE, MII_BMCR, regs)) {
859 velocity_mii_read(regs, MII_ADVERTISE, &ANAR);
860 if ((ANAR & (ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF))
861 == (ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF)) {
862 if (MII_REG_BITS_IS_ON(ADVERTISE_1000HALF | ADVERTISE_1000FULL, MII_CTRL1000, regs))
Dave Jones2cf71d22009-07-23 18:11:12 -0700863 status |= VELOCITY_AUTONEG_ENABLE;
864 }
865 }
866
867 return status;
868}
869
Dave Jones2cf71d22009-07-23 18:11:12 -0700870/**
871 * velocity_set_media_mode - set media mode
872 * @mii_status: old MII link state
873 *
874 * Check the media link state and configure the flow control
875 * PHY and also velocity hardware setup accordingly. In particular
876 * we need to set up CD polling and frame bursting.
877 */
878static int velocity_set_media_mode(struct velocity_info *vptr, u32 mii_status)
879{
880 u32 curr_status;
881 struct mac_regs __iomem *regs = vptr->mac_regs;
882
883 vptr->mii_status = mii_check_media_mode(vptr->mac_regs);
884 curr_status = vptr->mii_status & (~VELOCITY_LINK_FAIL);
885
886 /* Set mii link status */
887 set_mii_flow_control(vptr);
888
889 /*
Uwe Kleine-Königa34f0b32010-12-10 14:55:42 +0100890 Check if new status is consistent with current status
Joe Perches8e95a202009-12-03 07:58:21 +0000891 if (((mii_status & curr_status) & VELOCITY_AUTONEG_ENABLE) ||
892 (mii_status==curr_status)) {
Dave Jones2cf71d22009-07-23 18:11:12 -0700893 vptr->mii_status=mii_check_media_mode(vptr->mac_regs);
894 vptr->mii_status=check_connection_type(vptr->mac_regs);
895 VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity link no change\n");
896 return 0;
897 }
898 */
899
900 if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201)
Francois Romieu3a7f8682010-04-06 14:24:53 -0700901 MII_REG_BITS_ON(AUXCR_MDPPS, MII_NCONFIG, vptr->mac_regs);
Dave Jones2cf71d22009-07-23 18:11:12 -0700902
903 /*
904 * If connection type is AUTO
905 */
906 if (mii_status & VELOCITY_AUTONEG_ENABLE) {
907 VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity is AUTO mode\n");
908 /* clear force MAC mode bit */
909 BYTE_REG_BITS_OFF(CHIPGCR_FCMODE, &regs->CHIPGCR);
910 /* set duplex mode of MAC according to duplex mode of MII */
Francois Romieu3a7f8682010-04-06 14:24:53 -0700911 MII_REG_BITS_ON(ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF, MII_ADVERTISE, vptr->mac_regs);
912 MII_REG_BITS_ON(ADVERTISE_1000FULL | ADVERTISE_1000HALF, MII_CTRL1000, vptr->mac_regs);
913 MII_REG_BITS_ON(BMCR_SPEED1000, MII_BMCR, vptr->mac_regs);
Dave Jones2cf71d22009-07-23 18:11:12 -0700914
915 /* enable AUTO-NEGO mode */
916 mii_set_auto_on(vptr);
917 } else {
françois romieu15419222010-10-13 09:26:05 +0000918 u16 CTRL1000;
Dave Jones2cf71d22009-07-23 18:11:12 -0700919 u16 ANAR;
920 u8 CHIPGCR;
921
922 /*
923 * 1. if it's 3119, disable frame bursting in halfduplex mode
924 * and enable it in fullduplex mode
925 * 2. set correct MII/GMII and half/full duplex mode in CHIPGCR
926 * 3. only enable CD heart beat counter in 10HD mode
927 */
928
929 /* set force MAC mode bit */
930 BYTE_REG_BITS_ON(CHIPGCR_FCMODE, &regs->CHIPGCR);
931
932 CHIPGCR = readb(&regs->CHIPGCR);
françois romieu15419222010-10-13 09:26:05 +0000933
934 if (mii_status & VELOCITY_SPEED_1000)
935 CHIPGCR |= CHIPGCR_FCGMII;
936 else
937 CHIPGCR &= ~CHIPGCR_FCGMII;
Dave Jones2cf71d22009-07-23 18:11:12 -0700938
939 if (mii_status & VELOCITY_DUPLEX_FULL) {
940 CHIPGCR |= CHIPGCR_FCFDX;
941 writeb(CHIPGCR, &regs->CHIPGCR);
942 VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced full mode\n");
943 if (vptr->rev_id < REV_ID_VT3216_A0)
944 BYTE_REG_BITS_OFF(TCR_TB2BDIS, &regs->TCR);
945 } else {
946 CHIPGCR &= ~CHIPGCR_FCFDX;
947 VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced half mode\n");
948 writeb(CHIPGCR, &regs->CHIPGCR);
949 if (vptr->rev_id < REV_ID_VT3216_A0)
950 BYTE_REG_BITS_ON(TCR_TB2BDIS, &regs->TCR);
951 }
952
françois romieu15419222010-10-13 09:26:05 +0000953 velocity_mii_read(vptr->mac_regs, MII_CTRL1000, &CTRL1000);
954 CTRL1000 &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
955 if ((mii_status & VELOCITY_SPEED_1000) &&
956 (mii_status & VELOCITY_DUPLEX_FULL)) {
957 CTRL1000 |= ADVERTISE_1000FULL;
958 }
959 velocity_mii_write(vptr->mac_regs, MII_CTRL1000, CTRL1000);
Dave Jones2cf71d22009-07-23 18:11:12 -0700960
961 if (!(mii_status & VELOCITY_DUPLEX_FULL) && (mii_status & VELOCITY_SPEED_10))
962 BYTE_REG_BITS_OFF(TESTCFG_HBDIS, &regs->TESTCFG);
963 else
964 BYTE_REG_BITS_ON(TESTCFG_HBDIS, &regs->TESTCFG);
965
Francois Romieu3a7f8682010-04-06 14:24:53 -0700966 /* MII_REG_BITS_OFF(BMCR_SPEED1000, MII_BMCR, vptr->mac_regs); */
967 velocity_mii_read(vptr->mac_regs, MII_ADVERTISE, &ANAR);
968 ANAR &= (~(ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF));
Dave Jones2cf71d22009-07-23 18:11:12 -0700969 if (mii_status & VELOCITY_SPEED_100) {
970 if (mii_status & VELOCITY_DUPLEX_FULL)
Francois Romieu3a7f8682010-04-06 14:24:53 -0700971 ANAR |= ADVERTISE_100FULL;
Dave Jones2cf71d22009-07-23 18:11:12 -0700972 else
Francois Romieu3a7f8682010-04-06 14:24:53 -0700973 ANAR |= ADVERTISE_100HALF;
françois romieu15419222010-10-13 09:26:05 +0000974 } else if (mii_status & VELOCITY_SPEED_10) {
Dave Jones2cf71d22009-07-23 18:11:12 -0700975 if (mii_status & VELOCITY_DUPLEX_FULL)
Francois Romieu3a7f8682010-04-06 14:24:53 -0700976 ANAR |= ADVERTISE_10FULL;
Dave Jones2cf71d22009-07-23 18:11:12 -0700977 else
Francois Romieu3a7f8682010-04-06 14:24:53 -0700978 ANAR |= ADVERTISE_10HALF;
Dave Jones2cf71d22009-07-23 18:11:12 -0700979 }
Francois Romieu3a7f8682010-04-06 14:24:53 -0700980 velocity_mii_write(vptr->mac_regs, MII_ADVERTISE, ANAR);
Dave Jones2cf71d22009-07-23 18:11:12 -0700981 /* enable AUTO-NEGO mode */
982 mii_set_auto_on(vptr);
Francois Romieu3a7f8682010-04-06 14:24:53 -0700983 /* MII_REG_BITS_ON(BMCR_ANENABLE, MII_BMCR, vptr->mac_regs); */
Dave Jones2cf71d22009-07-23 18:11:12 -0700984 }
985 /* vptr->mii_status=mii_check_media_mode(vptr->mac_regs); */
986 /* vptr->mii_status=check_connection_type(vptr->mac_regs); */
987 return VELOCITY_LINK_CHANGE;
988}
989
990/**
991 * velocity_print_link_status - link status reporting
992 * @vptr: velocity to report on
993 *
994 * Turn the link status of the velocity card into a kernel log
995 * description of the new link state, detailing speed and duplex
996 * status
997 */
998static void velocity_print_link_status(struct velocity_info *vptr)
999{
1000
1001 if (vptr->mii_status & VELOCITY_LINK_FAIL) {
Tony Priska9683c92013-05-18 09:39:05 +00001002 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: failed to detect cable link\n", vptr->netdev->name);
Dave Jones2cf71d22009-07-23 18:11:12 -07001003 } else if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
Tony Priska9683c92013-05-18 09:39:05 +00001004 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link auto-negotiation", vptr->netdev->name);
Dave Jones2cf71d22009-07-23 18:11:12 -07001005
1006 if (vptr->mii_status & VELOCITY_SPEED_1000)
1007 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 1000M bps");
1008 else if (vptr->mii_status & VELOCITY_SPEED_100)
1009 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps");
1010 else
1011 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps");
1012
1013 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
1014 VELOCITY_PRT(MSG_LEVEL_INFO, " full duplex\n");
1015 else
1016 VELOCITY_PRT(MSG_LEVEL_INFO, " half duplex\n");
1017 } else {
Tony Priska9683c92013-05-18 09:39:05 +00001018 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link forced", vptr->netdev->name);
Dave Jones2cf71d22009-07-23 18:11:12 -07001019 switch (vptr->options.spd_dpx) {
françois romieu15419222010-10-13 09:26:05 +00001020 case SPD_DPX_1000_FULL:
1021 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 1000M bps full duplex\n");
1022 break;
Dave Jones2cf71d22009-07-23 18:11:12 -07001023 case SPD_DPX_100_HALF:
1024 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps half duplex\n");
1025 break;
1026 case SPD_DPX_100_FULL:
1027 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps full duplex\n");
1028 break;
1029 case SPD_DPX_10_HALF:
1030 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps half duplex\n");
1031 break;
1032 case SPD_DPX_10_FULL:
1033 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps full duplex\n");
1034 break;
1035 default:
1036 break;
1037 }
1038 }
1039}
1040
1041/**
1042 * enable_flow_control_ability - flow control
1043 * @vptr: veloity to configure
1044 *
1045 * Set up flow control according to the flow control options
1046 * determined by the eeprom/configuration.
1047 */
1048static void enable_flow_control_ability(struct velocity_info *vptr)
1049{
1050
1051 struct mac_regs __iomem *regs = vptr->mac_regs;
1052
1053 switch (vptr->options.flow_cntl) {
1054
1055 case FLOW_CNTL_DEFAULT:
1056 if (BYTE_REG_BITS_IS_ON(PHYSR0_RXFLC, &regs->PHYSR0))
1057 writel(CR0_FDXRFCEN, &regs->CR0Set);
1058 else
1059 writel(CR0_FDXRFCEN, &regs->CR0Clr);
1060
1061 if (BYTE_REG_BITS_IS_ON(PHYSR0_TXFLC, &regs->PHYSR0))
1062 writel(CR0_FDXTFCEN, &regs->CR0Set);
1063 else
1064 writel(CR0_FDXTFCEN, &regs->CR0Clr);
1065 break;
1066
1067 case FLOW_CNTL_TX:
1068 writel(CR0_FDXTFCEN, &regs->CR0Set);
1069 writel(CR0_FDXRFCEN, &regs->CR0Clr);
1070 break;
1071
1072 case FLOW_CNTL_RX:
1073 writel(CR0_FDXRFCEN, &regs->CR0Set);
1074 writel(CR0_FDXTFCEN, &regs->CR0Clr);
1075 break;
1076
1077 case FLOW_CNTL_TX_RX:
1078 writel(CR0_FDXTFCEN, &regs->CR0Set);
1079 writel(CR0_FDXRFCEN, &regs->CR0Set);
1080 break;
1081
1082 case FLOW_CNTL_DISABLE:
1083 writel(CR0_FDXRFCEN, &regs->CR0Clr);
1084 writel(CR0_FDXTFCEN, &regs->CR0Clr);
1085 break;
1086
1087 default:
1088 break;
1089 }
1090
1091}
1092
1093/**
1094 * velocity_soft_reset - soft reset
1095 * @vptr: velocity to reset
1096 *
1097 * Kick off a soft reset of the velocity adapter and then poll
1098 * until the reset sequence has completed before returning.
1099 */
1100static int velocity_soft_reset(struct velocity_info *vptr)
1101{
1102 struct mac_regs __iomem *regs = vptr->mac_regs;
1103 int i = 0;
1104
1105 writel(CR0_SFRST, &regs->CR0Set);
1106
1107 for (i = 0; i < W_MAX_TIMEOUT; i++) {
1108 udelay(5);
1109 if (!DWORD_REG_BITS_IS_ON(CR0_SFRST, &regs->CR0Set))
1110 break;
1111 }
1112
1113 if (i == W_MAX_TIMEOUT) {
1114 writel(CR0_FORSRST, &regs->CR0Set);
1115 /* FIXME: PCI POSTING */
1116 /* delay 2ms */
1117 mdelay(2);
1118 }
1119 return 0;
1120}
1121
1122/**
1123 * velocity_set_multi - filter list change callback
1124 * @dev: network device
1125 *
1126 * Called by the network layer when the filter lists need to change
1127 * for a velocity adapter. Reload the CAMs with the new address
1128 * filter ruleset.
1129 */
1130static void velocity_set_multi(struct net_device *dev)
1131{
1132 struct velocity_info *vptr = netdev_priv(dev);
1133 struct mac_regs __iomem *regs = vptr->mac_regs;
1134 u8 rx_mode;
1135 int i;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001136 struct netdev_hw_addr *ha;
Dave Jones2cf71d22009-07-23 18:11:12 -07001137
1138 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
1139 writel(0xffffffff, &regs->MARCAM[0]);
1140 writel(0xffffffff, &regs->MARCAM[4]);
1141 rx_mode = (RCR_AM | RCR_AB | RCR_PROM);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001142 } else if ((netdev_mc_count(dev) > vptr->multicast_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00001143 (dev->flags & IFF_ALLMULTI)) {
Dave Jones2cf71d22009-07-23 18:11:12 -07001144 writel(0xffffffff, &regs->MARCAM[0]);
1145 writel(0xffffffff, &regs->MARCAM[4]);
1146 rx_mode = (RCR_AM | RCR_AB);
1147 } else {
1148 int offset = MCAM_SIZE - vptr->multicast_limit;
1149 mac_get_cam_mask(regs, vptr->mCAMmask);
1150
Jiri Pirko567ec872010-02-23 23:17:07 +00001151 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001152 netdev_for_each_mc_addr(ha, dev) {
1153 mac_set_cam(regs, i + offset, ha->addr);
Dave Jones2cf71d22009-07-23 18:11:12 -07001154 vptr->mCAMmask[(offset + i) / 8] |= 1 << ((offset + i) & 7);
Jiri Pirko567ec872010-02-23 23:17:07 +00001155 i++;
Dave Jones2cf71d22009-07-23 18:11:12 -07001156 }
1157
1158 mac_set_cam_mask(regs, vptr->mCAMmask);
1159 rx_mode = RCR_AM | RCR_AB | RCR_AP;
1160 }
1161 if (dev->mtu > 1500)
1162 rx_mode |= RCR_AL;
1163
1164 BYTE_REG_BITS_ON(rx_mode, &regs->RCR);
1165
1166}
1167
1168/*
1169 * MII access , media link mode setting functions
1170 */
1171
1172/**
1173 * mii_init - set up MII
1174 * @vptr: velocity adapter
1175 * @mii_status: links tatus
1176 *
1177 * Set up the PHY for the current link state.
1178 */
1179static void mii_init(struct velocity_info *vptr, u32 mii_status)
1180{
1181 u16 BMCR;
1182
1183 switch (PHYID_GET_PHY_ID(vptr->phy_id)) {
1184 case PHYID_CICADA_CS8201:
1185 /*
1186 * Reset to hardware default
1187 */
Francois Romieu3a7f8682010-04-06 14:24:53 -07001188 MII_REG_BITS_OFF((ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP), MII_ADVERTISE, vptr->mac_regs);
Dave Jones2cf71d22009-07-23 18:11:12 -07001189 /*
1190 * Turn on ECHODIS bit in NWay-forced full mode and turn it
1191 * off it in NWay-forced half mode for NWay-forced v.s.
1192 * legacy-forced issue.
1193 */
1194 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
Francois Romieu3a7f8682010-04-06 14:24:53 -07001195 MII_REG_BITS_ON(TCSR_ECHODIS, MII_SREVISION, vptr->mac_regs);
Dave Jones2cf71d22009-07-23 18:11:12 -07001196 else
Francois Romieu3a7f8682010-04-06 14:24:53 -07001197 MII_REG_BITS_OFF(TCSR_ECHODIS, MII_SREVISION, vptr->mac_regs);
Dave Jones2cf71d22009-07-23 18:11:12 -07001198 /*
1199 * Turn on Link/Activity LED enable bit for CIS8201
1200 */
Francois Romieu3a7f8682010-04-06 14:24:53 -07001201 MII_REG_BITS_ON(PLED_LALBE, MII_TPISTATUS, vptr->mac_regs);
Dave Jones2cf71d22009-07-23 18:11:12 -07001202 break;
1203 case PHYID_VT3216_32BIT:
1204 case PHYID_VT3216_64BIT:
1205 /*
1206 * Reset to hardware default
1207 */
Francois Romieu3a7f8682010-04-06 14:24:53 -07001208 MII_REG_BITS_ON((ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP), MII_ADVERTISE, vptr->mac_regs);
Dave Jones2cf71d22009-07-23 18:11:12 -07001209 /*
1210 * Turn on ECHODIS bit in NWay-forced full mode and turn it
1211 * off it in NWay-forced half mode for NWay-forced v.s.
1212 * legacy-forced issue
1213 */
1214 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
Francois Romieu3a7f8682010-04-06 14:24:53 -07001215 MII_REG_BITS_ON(TCSR_ECHODIS, MII_SREVISION, vptr->mac_regs);
Dave Jones2cf71d22009-07-23 18:11:12 -07001216 else
Francois Romieu3a7f8682010-04-06 14:24:53 -07001217 MII_REG_BITS_OFF(TCSR_ECHODIS, MII_SREVISION, vptr->mac_regs);
Dave Jones2cf71d22009-07-23 18:11:12 -07001218 break;
1219
1220 case PHYID_MARVELL_1000:
1221 case PHYID_MARVELL_1000S:
1222 /*
1223 * Assert CRS on Transmit
1224 */
1225 MII_REG_BITS_ON(PSCR_ACRSTX, MII_REG_PSCR, vptr->mac_regs);
1226 /*
1227 * Reset to hardware default
1228 */
Francois Romieu3a7f8682010-04-06 14:24:53 -07001229 MII_REG_BITS_ON((ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP), MII_ADVERTISE, vptr->mac_regs);
Dave Jones2cf71d22009-07-23 18:11:12 -07001230 break;
1231 default:
1232 ;
1233 }
Francois Romieu3a7f8682010-04-06 14:24:53 -07001234 velocity_mii_read(vptr->mac_regs, MII_BMCR, &BMCR);
1235 if (BMCR & BMCR_ISOLATE) {
1236 BMCR &= ~BMCR_ISOLATE;
1237 velocity_mii_write(vptr->mac_regs, MII_BMCR, BMCR);
Dave Jones2cf71d22009-07-23 18:11:12 -07001238 }
1239}
1240
Simon Kagstrom6dfc4b92009-11-25 22:10:12 +00001241/**
1242 * setup_queue_timers - Setup interrupt timers
1243 *
1244 * Setup interrupt frequency during suppression (timeout if the frame
1245 * count isn't filled).
1246 */
1247static void setup_queue_timers(struct velocity_info *vptr)
1248{
1249 /* Only for newer revisions */
1250 if (vptr->rev_id >= REV_ID_VT3216_A0) {
1251 u8 txqueue_timer = 0;
1252 u8 rxqueue_timer = 0;
1253
1254 if (vptr->mii_status & (VELOCITY_SPEED_1000 |
1255 VELOCITY_SPEED_100)) {
1256 txqueue_timer = vptr->options.txqueue_timer;
1257 rxqueue_timer = vptr->options.rxqueue_timer;
1258 }
1259
1260 writeb(txqueue_timer, &vptr->mac_regs->TQETMR);
1261 writeb(rxqueue_timer, &vptr->mac_regs->RQETMR);
1262 }
1263}
françois romieu5ae297b2011-08-04 02:39:03 +00001264
Simon Kagstrom6dfc4b92009-11-25 22:10:12 +00001265/**
1266 * setup_adaptive_interrupts - Setup interrupt suppression
1267 *
1268 * @vptr velocity adapter
1269 *
1270 * The velocity is able to suppress interrupt during high interrupt load.
1271 * This function turns on that feature.
1272 */
1273static void setup_adaptive_interrupts(struct velocity_info *vptr)
1274{
1275 struct mac_regs __iomem *regs = vptr->mac_regs;
1276 u16 tx_intsup = vptr->options.tx_intsup;
1277 u16 rx_intsup = vptr->options.rx_intsup;
1278
1279 /* Setup default interrupt mask (will be changed below) */
1280 vptr->int_mask = INT_MASK_DEF;
1281
1282 /* Set Tx Interrupt Suppression Threshold */
1283 writeb(CAMCR_PS0, &regs->CAMCR);
1284 if (tx_intsup != 0) {
1285 vptr->int_mask &= ~(ISR_PTXI | ISR_PTX0I | ISR_PTX1I |
1286 ISR_PTX2I | ISR_PTX3I);
1287 writew(tx_intsup, &regs->ISRCTL);
1288 } else
1289 writew(ISRCTL_TSUPDIS, &regs->ISRCTL);
1290
1291 /* Set Rx Interrupt Suppression Threshold */
1292 writeb(CAMCR_PS1, &regs->CAMCR);
1293 if (rx_intsup != 0) {
1294 vptr->int_mask &= ~ISR_PRXI;
1295 writew(rx_intsup, &regs->ISRCTL);
1296 } else
1297 writew(ISRCTL_RSUPDIS, &regs->ISRCTL);
1298
1299 /* Select page to interrupt hold timer */
1300 writeb(0, &regs->CAMCR);
1301}
Dave Jones2cf71d22009-07-23 18:11:12 -07001302
1303/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 * velocity_init_registers - initialise MAC registers
1305 * @vptr: velocity to init
1306 * @type: type of initialisation (hot or cold)
1307 *
1308 * Initialise the MAC on a reset or on first set up on the
1309 * hardware.
1310 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001311static void velocity_init_registers(struct velocity_info *vptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 enum velocity_init_type type)
1313{
Dave Jonesc4067402009-07-20 17:35:21 +00001314 struct mac_regs __iomem *regs = vptr->mac_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 int i, mii_status;
1316
1317 mac_wol_reset(regs);
1318
1319 switch (type) {
1320 case VELOCITY_INIT_RESET:
1321 case VELOCITY_INIT_WOL:
1322
Tony Priska9683c92013-05-18 09:39:05 +00001323 netif_stop_queue(vptr->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 /*
1326 * Reset RX to prevent RX pointer not on the 4X location
1327 */
1328 velocity_rx_reset(vptr);
1329 mac_rx_queue_run(regs);
1330 mac_rx_queue_wake(regs);
1331
1332 mii_status = velocity_get_opt_media_mode(vptr);
1333 if (velocity_set_media_mode(vptr, mii_status) != VELOCITY_LINK_CHANGE) {
1334 velocity_print_link_status(vptr);
1335 if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
Tony Priska9683c92013-05-18 09:39:05 +00001336 netif_wake_queue(vptr->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
1338
1339 enable_flow_control_ability(vptr);
1340
1341 mac_clear_isr(regs);
1342 writel(CR0_STOP, &regs->CR0Clr);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001343 writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 &regs->CR0Set);
1345
1346 break;
1347
1348 case VELOCITY_INIT_COLD:
1349 default:
1350 /*
1351 * Do reset
1352 */
1353 velocity_soft_reset(vptr);
1354 mdelay(5);
1355
1356 mac_eeprom_reload(regs);
Dave Jonesc4067402009-07-20 17:35:21 +00001357 for (i = 0; i < 6; i++)
Tony Priska9683c92013-05-18 09:39:05 +00001358 writeb(vptr->netdev->dev_addr[i], &(regs->PAR[i]));
Dave Jonesc4067402009-07-20 17:35:21 +00001359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 /*
1361 * clear Pre_ACPI bit.
1362 */
1363 BYTE_REG_BITS_OFF(CFGA_PACPI, &(regs->CFGA));
1364 mac_set_rx_thresh(regs, vptr->options.rx_thresh);
1365 mac_set_dma_length(regs, vptr->options.DMA_length);
1366
1367 writeb(WOLCFG_SAM | WOLCFG_SAB, &regs->WOLCFGSet);
1368 /*
1369 * Back off algorithm use original IEEE standard
1370 */
1371 BYTE_REG_BITS_SET(CFGB_OFSET, (CFGB_CRANDOM | CFGB_CAP | CFGB_MBA | CFGB_BAKOPT), &regs->CFGB);
1372
1373 /*
1374 * Init CAM filter
1375 */
1376 velocity_init_cam_filter(vptr);
1377
1378 /*
1379 * Set packet filter: Receive directed and broadcast address
1380 */
Tony Priska9683c92013-05-18 09:39:05 +00001381 velocity_set_multi(vptr->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 /*
1384 * Enable MII auto-polling
1385 */
1386 enable_mii_autopoll(regs);
1387
Simon Kagstrom6dfc4b92009-11-25 22:10:12 +00001388 setup_adaptive_interrupts(vptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Francois Romieu0fe9f152008-07-31 22:10:10 +02001390 writel(vptr->rx.pool_dma, &regs->RDBaseLo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 writew(vptr->options.numrx - 1, &regs->RDCSize);
1392 mac_rx_queue_run(regs);
1393 mac_rx_queue_wake(regs);
1394
1395 writew(vptr->options.numtx - 1, &regs->TDCSize);
1396
Francois Romieu0fe9f152008-07-31 22:10:10 +02001397 for (i = 0; i < vptr->tx.numq; i++) {
1398 writel(vptr->tx.pool_dma[i], &regs->TDBaseLo[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 mac_tx_queue_run(regs, i);
1400 }
1401
1402 init_flow_control_register(vptr);
1403
1404 writel(CR0_STOP, &regs->CR0Clr);
1405 writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT), &regs->CR0Set);
1406
1407 mii_status = velocity_get_opt_media_mode(vptr);
Tony Priska9683c92013-05-18 09:39:05 +00001408 netif_stop_queue(vptr->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 mii_init(vptr, mii_status);
1411
1412 if (velocity_set_media_mode(vptr, mii_status) != VELOCITY_LINK_CHANGE) {
1413 velocity_print_link_status(vptr);
1414 if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
Tony Priska9683c92013-05-18 09:39:05 +00001415 netif_wake_queue(vptr->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417
1418 enable_flow_control_ability(vptr);
1419 mac_hw_mibs_init(regs);
1420 mac_write_int_mask(vptr->int_mask, regs);
1421 mac_clear_isr(regs);
1422
1423 }
1424}
1425
Dave Jones2cf71d22009-07-23 18:11:12 -07001426static void velocity_give_many_rx_descs(struct velocity_info *vptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Dave Jonesc4067402009-07-20 17:35:21 +00001428 struct mac_regs __iomem *regs = vptr->mac_regs;
Dave Jones2cf71d22009-07-23 18:11:12 -07001429 int avail, dirty, unusable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Dave Jones2cf71d22009-07-23 18:11:12 -07001431 /*
1432 * RD number must be equal to 4X per hardware spec
1433 * (programming guide rev 1.20, p.13)
1434 */
1435 if (vptr->rx.filled < 4)
1436 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Dave Jones2cf71d22009-07-23 18:11:12 -07001438 wmb();
1439
1440 unusable = vptr->rx.filled & 0x0003;
1441 dirty = vptr->rx.dirty - unusable;
1442 for (avail = vptr->rx.filled & 0xfffc; avail; avail--) {
1443 dirty = (dirty > 0) ? dirty - 1 : vptr->options.numrx - 1;
1444 vptr->rx.ring[dirty].rdesc0.len |= OWNED_BY_NIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
1446
Dave Jones2cf71d22009-07-23 18:11:12 -07001447 writew(vptr->rx.filled & 0xfffc, &regs->RBRDU);
1448 vptr->rx.filled = unusable;
1449}
1450
1451/**
1452 * velocity_init_dma_rings - set up DMA rings
1453 * @vptr: Velocity to set up
1454 *
1455 * Allocate PCI mapped DMA rings for the receive and transmit layer
1456 * to use.
1457 */
1458static int velocity_init_dma_rings(struct velocity_info *vptr)
1459{
1460 struct velocity_opt *opt = &vptr->options;
1461 const unsigned int rx_ring_size = opt->numrx * sizeof(struct rx_desc);
1462 const unsigned int tx_ring_size = opt->numtx * sizeof(struct tx_desc);
Dave Jones2cf71d22009-07-23 18:11:12 -07001463 dma_addr_t pool_dma;
1464 void *pool;
1465 unsigned int i;
1466
1467 /*
1468 * Allocate all RD/TD rings a single pool.
1469 *
Tony Priske2c41f12013-05-18 09:39:06 +00001470 * dma_alloc_coherent() fulfills the requirement for 64 bytes
Dave Jones2cf71d22009-07-23 18:11:12 -07001471 * alignment
1472 */
Tony Priske2c41f12013-05-18 09:39:06 +00001473 pool = dma_alloc_coherent(vptr->dev, tx_ring_size * vptr->tx.numq +
1474 rx_ring_size, &pool_dma, GFP_ATOMIC);
Dave Jones2cf71d22009-07-23 18:11:12 -07001475 if (!pool) {
Tony Priske2c41f12013-05-18 09:39:06 +00001476 dev_err(vptr->dev, "%s : DMA memory allocation failed.\n",
Tony Priska9683c92013-05-18 09:39:05 +00001477 vptr->netdev->name);
Dave Jones2cf71d22009-07-23 18:11:12 -07001478 return -ENOMEM;
1479 }
1480
1481 vptr->rx.ring = pool;
1482 vptr->rx.pool_dma = pool_dma;
1483
1484 pool += rx_ring_size;
1485 pool_dma += rx_ring_size;
1486
1487 for (i = 0; i < vptr->tx.numq; i++) {
1488 vptr->tx.rings[i] = pool;
1489 vptr->tx.pool_dma[i] = pool_dma;
1490 pool += tx_ring_size;
1491 pool_dma += tx_ring_size;
1492 }
1493
1494 return 0;
1495}
1496
1497static void velocity_set_rxbufsize(struct velocity_info *vptr, int mtu)
1498{
1499 vptr->rx.buf_sz = (mtu <= ETH_DATA_LEN) ? PKT_BUF_SZ : mtu + 32;
1500}
1501
1502/**
1503 * velocity_alloc_rx_buf - allocate aligned receive buffer
1504 * @vptr: velocity
1505 * @idx: ring index
1506 *
1507 * Allocate a new full sized buffer for the reception of a frame and
1508 * map it into PCI space for the hardware to use. The hardware
1509 * requires *64* byte alignment of the buffer which makes life
1510 * less fun than would be ideal.
1511 */
1512static int velocity_alloc_rx_buf(struct velocity_info *vptr, int idx)
1513{
1514 struct rx_desc *rd = &(vptr->rx.ring[idx]);
1515 struct velocity_rd_info *rd_info = &(vptr->rx.info[idx]);
1516
Tony Priska9683c92013-05-18 09:39:05 +00001517 rd_info->skb = netdev_alloc_skb(vptr->netdev, vptr->rx.buf_sz + 64);
Dave Jones2cf71d22009-07-23 18:11:12 -07001518 if (rd_info->skb == NULL)
1519 return -ENOMEM;
1520
1521 /*
1522 * Do the gymnastics to get the buffer head for data at
1523 * 64byte alignment.
1524 */
Simon Kagstromda95b2d2009-11-25 22:09:53 +00001525 skb_reserve(rd_info->skb,
1526 64 - ((unsigned long) rd_info->skb->data & 63));
Tony Priske2c41f12013-05-18 09:39:06 +00001527 rd_info->skb_dma = dma_map_single(vptr->dev, rd_info->skb->data,
1528 vptr->rx.buf_sz, DMA_FROM_DEVICE);
Dave Jones2cf71d22009-07-23 18:11:12 -07001529
1530 /*
1531 * Fill in the descriptor to match
1532 */
1533
1534 *((u32 *) & (rd->rdesc0)) = 0;
1535 rd->size = cpu_to_le16(vptr->rx.buf_sz) | RX_INTEN;
1536 rd->pa_low = cpu_to_le32(rd_info->skb_dma);
1537 rd->pa_high = 0;
1538 return 0;
1539}
1540
1541
1542static int velocity_rx_refill(struct velocity_info *vptr)
1543{
1544 int dirty = vptr->rx.dirty, done = 0;
1545
1546 do {
1547 struct rx_desc *rd = vptr->rx.ring + dirty;
1548
1549 /* Fine for an all zero Rx desc at init time as well */
1550 if (rd->rdesc0.len & OWNED_BY_NIC)
1551 break;
1552
1553 if (!vptr->rx.info[dirty].skb) {
1554 if (velocity_alloc_rx_buf(vptr, dirty) < 0)
1555 break;
1556 }
1557 done++;
1558 dirty = (dirty < vptr->options.numrx - 1) ? dirty + 1 : 0;
1559 } while (dirty != vptr->rx.curr);
1560
1561 if (done) {
1562 vptr->rx.dirty = dirty;
1563 vptr->rx.filled += done;
1564 }
1565
1566 return done;
1567}
1568
1569/**
1570 * velocity_free_rd_ring - free receive ring
1571 * @vptr: velocity to clean up
1572 *
1573 * Free the receive buffers for each ring slot and any
1574 * attached socket buffers that need to go away.
1575 */
1576static void velocity_free_rd_ring(struct velocity_info *vptr)
1577{
1578 int i;
1579
1580 if (vptr->rx.info == NULL)
1581 return;
1582
1583 for (i = 0; i < vptr->options.numrx; i++) {
1584 struct velocity_rd_info *rd_info = &(vptr->rx.info[i]);
1585 struct rx_desc *rd = vptr->rx.ring + i;
1586
1587 memset(rd, 0, sizeof(*rd));
1588
1589 if (!rd_info->skb)
1590 continue;
Tony Priske2c41f12013-05-18 09:39:06 +00001591 dma_unmap_single(vptr->dev, rd_info->skb_dma, vptr->rx.buf_sz,
1592 DMA_FROM_DEVICE);
Dave Jones2cf71d22009-07-23 18:11:12 -07001593 rd_info->skb_dma = 0;
1594
1595 dev_kfree_skb(rd_info->skb);
1596 rd_info->skb = NULL;
1597 }
1598
1599 kfree(vptr->rx.info);
1600 vptr->rx.info = NULL;
1601}
1602
Dave Jones2cf71d22009-07-23 18:11:12 -07001603/**
1604 * velocity_init_rd_ring - set up receive ring
1605 * @vptr: velocity to configure
1606 *
1607 * Allocate and set up the receive buffers for each ring slot and
1608 * assign them to the network adapter.
1609 */
1610static int velocity_init_rd_ring(struct velocity_info *vptr)
1611{
1612 int ret = -ENOMEM;
1613
1614 vptr->rx.info = kcalloc(vptr->options.numrx,
1615 sizeof(struct velocity_rd_info), GFP_KERNEL);
1616 if (!vptr->rx.info)
1617 goto out;
1618
1619 velocity_init_rx_ring_indexes(vptr);
1620
1621 if (velocity_rx_refill(vptr) != vptr->options.numrx) {
1622 VELOCITY_PRT(MSG_LEVEL_ERR, KERN_ERR
Tony Priska9683c92013-05-18 09:39:05 +00001623 "%s: failed to allocate RX buffer.\n", vptr->netdev->name);
Dave Jones2cf71d22009-07-23 18:11:12 -07001624 velocity_free_rd_ring(vptr);
1625 goto out;
1626 }
1627
1628 ret = 0;
1629out:
1630 return ret;
1631}
1632
1633/**
1634 * velocity_init_td_ring - set up transmit ring
1635 * @vptr: velocity
1636 *
1637 * Set up the transmit ring and chain the ring pointers together.
1638 * Returns zero on success or a negative posix errno code for
1639 * failure.
1640 */
1641static int velocity_init_td_ring(struct velocity_info *vptr)
1642{
Dave Jones2cf71d22009-07-23 18:11:12 -07001643 int j;
1644
1645 /* Init the TD ring entries */
1646 for (j = 0; j < vptr->tx.numq; j++) {
Dave Jones2cf71d22009-07-23 18:11:12 -07001647
1648 vptr->tx.infos[j] = kcalloc(vptr->options.numtx,
1649 sizeof(struct velocity_td_info),
1650 GFP_KERNEL);
1651 if (!vptr->tx.infos[j]) {
1652 while (--j >= 0)
1653 kfree(vptr->tx.infos[j]);
1654 return -ENOMEM;
1655 }
1656
1657 vptr->tx.tail[j] = vptr->tx.curr[j] = vptr->tx.used[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
1659 return 0;
1660}
1661
Dave Jones2cf71d22009-07-23 18:11:12 -07001662/**
1663 * velocity_free_dma_rings - free PCI ring pointers
1664 * @vptr: Velocity to free from
1665 *
1666 * Clean up the PCI ring buffers allocated to this velocity.
1667 */
1668static void velocity_free_dma_rings(struct velocity_info *vptr)
1669{
1670 const int size = vptr->options.numrx * sizeof(struct rx_desc) +
1671 vptr->options.numtx * sizeof(struct tx_desc) * vptr->tx.numq;
1672
Tony Priske2c41f12013-05-18 09:39:06 +00001673 dma_free_coherent(vptr->dev, size, vptr->rx.ring, vptr->rx.pool_dma);
Dave Jones2cf71d22009-07-23 18:11:12 -07001674}
1675
Dave Jones2cf71d22009-07-23 18:11:12 -07001676static int velocity_init_rings(struct velocity_info *vptr, int mtu)
1677{
1678 int ret;
1679
1680 velocity_set_rxbufsize(vptr, mtu);
1681
1682 ret = velocity_init_dma_rings(vptr);
1683 if (ret < 0)
1684 goto out;
1685
1686 ret = velocity_init_rd_ring(vptr);
1687 if (ret < 0)
1688 goto err_free_dma_rings_0;
1689
1690 ret = velocity_init_td_ring(vptr);
1691 if (ret < 0)
1692 goto err_free_rd_ring_1;
1693out:
1694 return ret;
1695
1696err_free_rd_ring_1:
1697 velocity_free_rd_ring(vptr);
1698err_free_dma_rings_0:
1699 velocity_free_dma_rings(vptr);
1700 goto out;
1701}
1702
1703/**
1704 * velocity_free_tx_buf - free transmit buffer
1705 * @vptr: velocity
1706 * @tdinfo: buffer
1707 *
1708 * Release an transmit buffer. If the buffer was preallocated then
1709 * recycle it, if not then unmap the buffer.
1710 */
Simon Kagstromc79992f2009-11-25 22:10:43 +00001711static void velocity_free_tx_buf(struct velocity_info *vptr,
1712 struct velocity_td_info *tdinfo, struct tx_desc *td)
Dave Jones2cf71d22009-07-23 18:11:12 -07001713{
1714 struct sk_buff *skb = tdinfo->skb;
Dave Jones2cf71d22009-07-23 18:11:12 -07001715
1716 /*
1717 * Don't unmap the pre-allocated tx_bufs
1718 */
1719 if (tdinfo->skb_dma) {
Simon Kagstromc79992f2009-11-25 22:10:43 +00001720 int i;
Dave Jones2cf71d22009-07-23 18:11:12 -07001721
Dave Jones2cf71d22009-07-23 18:11:12 -07001722 for (i = 0; i < tdinfo->nskb_dma; i++) {
Simon Kagstromc79992f2009-11-25 22:10:43 +00001723 size_t pktlen = max_t(size_t, skb->len, ETH_ZLEN);
1724
1725 /* For scatter-gather */
1726 if (skb_shinfo(skb)->nr_frags > 0)
1727 pktlen = max_t(size_t, pktlen,
1728 td->td_buf[i].size & ~TD_QUEUE);
1729
Tony Priske2c41f12013-05-18 09:39:06 +00001730 dma_unmap_single(vptr->dev, tdinfo->skb_dma[i],
1731 le16_to_cpu(pktlen), DMA_TO_DEVICE);
Dave Jones2cf71d22009-07-23 18:11:12 -07001732 }
1733 }
1734 dev_kfree_skb_irq(skb);
1735 tdinfo->skb = NULL;
1736}
1737
Dave Jones2cf71d22009-07-23 18:11:12 -07001738/*
1739 * FIXME: could we merge this with velocity_free_tx_buf ?
1740 */
1741static void velocity_free_td_ring_entry(struct velocity_info *vptr,
1742 int q, int n)
1743{
1744 struct velocity_td_info *td_info = &(vptr->tx.infos[q][n]);
1745 int i;
1746
1747 if (td_info == NULL)
1748 return;
1749
1750 if (td_info->skb) {
1751 for (i = 0; i < td_info->nskb_dma; i++) {
1752 if (td_info->skb_dma[i]) {
Tony Priske2c41f12013-05-18 09:39:06 +00001753 dma_unmap_single(vptr->dev, td_info->skb_dma[i],
1754 td_info->skb->len, DMA_TO_DEVICE);
Dave Jones2cf71d22009-07-23 18:11:12 -07001755 td_info->skb_dma[i] = 0;
1756 }
1757 }
1758 dev_kfree_skb(td_info->skb);
1759 td_info->skb = NULL;
1760 }
1761}
1762
1763/**
1764 * velocity_free_td_ring - free td ring
1765 * @vptr: velocity
1766 *
1767 * Free up the transmit ring for this particular velocity adapter.
1768 * We free the ring contents but not the ring itself.
1769 */
1770static void velocity_free_td_ring(struct velocity_info *vptr)
1771{
1772 int i, j;
1773
1774 for (j = 0; j < vptr->tx.numq; j++) {
1775 if (vptr->tx.infos[j] == NULL)
1776 continue;
1777 for (i = 0; i < vptr->options.numtx; i++)
1778 velocity_free_td_ring_entry(vptr, j, i);
1779
1780 kfree(vptr->tx.infos[j]);
1781 vptr->tx.infos[j] = NULL;
1782 }
1783}
1784
Dave Jones2cf71d22009-07-23 18:11:12 -07001785static void velocity_free_rings(struct velocity_info *vptr)
1786{
1787 velocity_free_td_ring(vptr);
1788 velocity_free_rd_ring(vptr);
1789 velocity_free_dma_rings(vptr);
1790}
1791
1792/**
1793 * velocity_error - handle error from controller
1794 * @vptr: velocity
1795 * @status: card status
1796 *
1797 * Process an error report from the hardware and attempt to recover
1798 * the card itself. At the moment we cannot recover from some
1799 * theoretically impossible errors but this could be fixed using
1800 * the pci_device_failed logic to bounce the hardware
1801 *
1802 */
1803static void velocity_error(struct velocity_info *vptr, int status)
1804{
1805
1806 if (status & ISR_TXSTLI) {
1807 struct mac_regs __iomem *regs = vptr->mac_regs;
1808
1809 printk(KERN_ERR "TD structure error TDindex=%hx\n", readw(&regs->TDIdx[0]));
1810 BYTE_REG_BITS_ON(TXESR_TDSTR, &regs->TXESR);
1811 writew(TRDCSR_RUN, &regs->TDCSRClr);
Tony Priska9683c92013-05-18 09:39:05 +00001812 netif_stop_queue(vptr->netdev);
Dave Jones2cf71d22009-07-23 18:11:12 -07001813
1814 /* FIXME: port over the pci_device_failed code and use it
1815 here */
1816 }
1817
1818 if (status & ISR_SRCI) {
1819 struct mac_regs __iomem *regs = vptr->mac_regs;
1820 int linked;
1821
1822 if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
1823 vptr->mii_status = check_connection_type(regs);
1824
1825 /*
1826 * If it is a 3119, disable frame bursting in
1827 * halfduplex mode and enable it in fullduplex
1828 * mode
1829 */
1830 if (vptr->rev_id < REV_ID_VT3216_A0) {
David S. Miller6cdee2f2009-09-02 00:32:56 -07001831 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
Dave Jones2cf71d22009-07-23 18:11:12 -07001832 BYTE_REG_BITS_ON(TCR_TB2BDIS, &regs->TCR);
1833 else
1834 BYTE_REG_BITS_OFF(TCR_TB2BDIS, &regs->TCR);
1835 }
1836 /*
1837 * Only enable CD heart beat counter in 10HD mode
1838 */
1839 if (!(vptr->mii_status & VELOCITY_DUPLEX_FULL) && (vptr->mii_status & VELOCITY_SPEED_10))
1840 BYTE_REG_BITS_OFF(TESTCFG_HBDIS, &regs->TESTCFG);
1841 else
1842 BYTE_REG_BITS_ON(TESTCFG_HBDIS, &regs->TESTCFG);
Simon Kagstrom6dfc4b92009-11-25 22:10:12 +00001843
1844 setup_queue_timers(vptr);
Dave Jones2cf71d22009-07-23 18:11:12 -07001845 }
1846 /*
1847 * Get link status from PHYSR0
1848 */
1849 linked = readb(&regs->PHYSR0) & PHYSR0_LINKGD;
1850
1851 if (linked) {
1852 vptr->mii_status &= ~VELOCITY_LINK_FAIL;
Tony Priska9683c92013-05-18 09:39:05 +00001853 netif_carrier_on(vptr->netdev);
Dave Jones2cf71d22009-07-23 18:11:12 -07001854 } else {
1855 vptr->mii_status |= VELOCITY_LINK_FAIL;
Tony Priska9683c92013-05-18 09:39:05 +00001856 netif_carrier_off(vptr->netdev);
Dave Jones2cf71d22009-07-23 18:11:12 -07001857 }
1858
1859 velocity_print_link_status(vptr);
1860 enable_flow_control_ability(vptr);
1861
1862 /*
1863 * Re-enable auto-polling because SRCI will disable
1864 * auto-polling
1865 */
1866
1867 enable_mii_autopoll(regs);
1868
1869 if (vptr->mii_status & VELOCITY_LINK_FAIL)
Tony Priska9683c92013-05-18 09:39:05 +00001870 netif_stop_queue(vptr->netdev);
Dave Jones2cf71d22009-07-23 18:11:12 -07001871 else
Tony Priska9683c92013-05-18 09:39:05 +00001872 netif_wake_queue(vptr->netdev);
Dave Jones2cf71d22009-07-23 18:11:12 -07001873
Joe Perches6403eab2011-06-03 11:51:20 +00001874 }
Dave Jones2cf71d22009-07-23 18:11:12 -07001875 if (status & ISR_MIBFI)
1876 velocity_update_hw_mibs(vptr);
1877 if (status & ISR_LSTEI)
1878 mac_rx_queue_wake(vptr->mac_regs);
1879}
1880
1881/**
1882 * tx_srv - transmit interrupt service
1883 * @vptr; Velocity
Dave Jones2cf71d22009-07-23 18:11:12 -07001884 *
1885 * Scan the queues looking for transmitted packets that
1886 * we can complete and clean up. Update any statistics as
1887 * necessary/
1888 */
Simon Kagstromd6cade02010-02-09 23:37:54 +00001889static int velocity_tx_srv(struct velocity_info *vptr)
Dave Jones2cf71d22009-07-23 18:11:12 -07001890{
1891 struct tx_desc *td;
1892 int qnum;
1893 int full = 0;
1894 int idx;
1895 int works = 0;
1896 struct velocity_td_info *tdinfo;
Tony Priska9683c92013-05-18 09:39:05 +00001897 struct net_device_stats *stats = &vptr->netdev->stats;
Dave Jones2cf71d22009-07-23 18:11:12 -07001898
1899 for (qnum = 0; qnum < vptr->tx.numq; qnum++) {
1900 for (idx = vptr->tx.tail[qnum]; vptr->tx.used[qnum] > 0;
1901 idx = (idx + 1) % vptr->options.numtx) {
1902
1903 /*
1904 * Get Tx Descriptor
1905 */
1906 td = &(vptr->tx.rings[qnum][idx]);
1907 tdinfo = &(vptr->tx.infos[qnum][idx]);
1908
1909 if (td->tdesc0.len & OWNED_BY_NIC)
1910 break;
1911
1912 if ((works++ > 15))
1913 break;
1914
1915 if (td->tdesc0.TSR & TSR0_TERR) {
1916 stats->tx_errors++;
1917 stats->tx_dropped++;
1918 if (td->tdesc0.TSR & TSR0_CDH)
1919 stats->tx_heartbeat_errors++;
1920 if (td->tdesc0.TSR & TSR0_CRS)
1921 stats->tx_carrier_errors++;
1922 if (td->tdesc0.TSR & TSR0_ABT)
1923 stats->tx_aborted_errors++;
1924 if (td->tdesc0.TSR & TSR0_OWC)
1925 stats->tx_window_errors++;
1926 } else {
1927 stats->tx_packets++;
1928 stats->tx_bytes += tdinfo->skb->len;
1929 }
Simon Kagstromc79992f2009-11-25 22:10:43 +00001930 velocity_free_tx_buf(vptr, tdinfo, td);
Dave Jones2cf71d22009-07-23 18:11:12 -07001931 vptr->tx.used[qnum]--;
1932 }
1933 vptr->tx.tail[qnum] = idx;
1934
1935 if (AVAIL_TD(vptr, qnum) < 1)
1936 full = 1;
1937 }
1938 /*
1939 * Look to see if we should kick the transmit network
1940 * layer for more work.
1941 */
Tony Priska9683c92013-05-18 09:39:05 +00001942 if (netif_queue_stopped(vptr->netdev) && (full == 0) &&
Joe Perches8e95a202009-12-03 07:58:21 +00001943 (!(vptr->mii_status & VELOCITY_LINK_FAIL))) {
Tony Priska9683c92013-05-18 09:39:05 +00001944 netif_wake_queue(vptr->netdev);
Dave Jones2cf71d22009-07-23 18:11:12 -07001945 }
1946 return works;
1947}
1948
1949/**
1950 * velocity_rx_csum - checksum process
1951 * @rd: receive packet descriptor
1952 * @skb: network layer packet buffer
1953 *
1954 * Process the status bits for the received packet and determine
1955 * if the checksum was computed and verified by the hardware
1956 */
1957static inline void velocity_rx_csum(struct rx_desc *rd, struct sk_buff *skb)
1958{
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001959 skb_checksum_none_assert(skb);
Dave Jones2cf71d22009-07-23 18:11:12 -07001960
1961 if (rd->rdesc1.CSM & CSM_IPKT) {
1962 if (rd->rdesc1.CSM & CSM_IPOK) {
1963 if ((rd->rdesc1.CSM & CSM_TCPKT) ||
1964 (rd->rdesc1.CSM & CSM_UDPKT)) {
1965 if (!(rd->rdesc1.CSM & CSM_TUPOK))
1966 return;
1967 }
1968 skb->ip_summed = CHECKSUM_UNNECESSARY;
1969 }
1970 }
1971}
1972
1973/**
1974 * velocity_rx_copy - in place Rx copy for small packets
1975 * @rx_skb: network layer packet buffer candidate
1976 * @pkt_size: received data size
1977 * @rd: receive packet descriptor
1978 * @dev: network device
1979 *
1980 * Replace the current skb that is scheduled for Rx processing by a
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001981 * shorter, immediately allocated skb, if the received packet is small
Dave Jones2cf71d22009-07-23 18:11:12 -07001982 * enough. This function returns a negative value if the received
1983 * packet is too big or if memory is exhausted.
1984 */
1985static int velocity_rx_copy(struct sk_buff **rx_skb, int pkt_size,
1986 struct velocity_info *vptr)
1987{
1988 int ret = -1;
1989 if (pkt_size < rx_copybreak) {
1990 struct sk_buff *new_skb;
1991
Tony Priska9683c92013-05-18 09:39:05 +00001992 new_skb = netdev_alloc_skb_ip_align(vptr->netdev, pkt_size);
Dave Jones2cf71d22009-07-23 18:11:12 -07001993 if (new_skb) {
1994 new_skb->ip_summed = rx_skb[0]->ip_summed;
Dave Jones2cf71d22009-07-23 18:11:12 -07001995 skb_copy_from_linear_data(*rx_skb, new_skb->data, pkt_size);
1996 *rx_skb = new_skb;
1997 ret = 0;
1998 }
1999
2000 }
2001 return ret;
2002}
2003
2004/**
2005 * velocity_iph_realign - IP header alignment
2006 * @vptr: velocity we are handling
2007 * @skb: network layer packet buffer
2008 * @pkt_size: received data size
2009 *
2010 * Align IP header on a 2 bytes boundary. This behavior can be
2011 * configured by the user.
2012 */
2013static inline void velocity_iph_realign(struct velocity_info *vptr,
2014 struct sk_buff *skb, int pkt_size)
2015{
2016 if (vptr->flags & VELOCITY_FLAGS_IP_ALIGN) {
2017 memmove(skb->data + 2, skb->data, pkt_size);
2018 skb_reserve(skb, 2);
2019 }
2020}
2021
Dave Jones2cf71d22009-07-23 18:11:12 -07002022/**
2023 * velocity_receive_frame - received packet processor
2024 * @vptr: velocity we are handling
2025 * @idx: ring index
2026 *
2027 * A packet has arrived. We process the packet and if appropriate
2028 * pass the frame up the network stack
2029 */
2030static int velocity_receive_frame(struct velocity_info *vptr, int idx)
2031{
Tony Priska9683c92013-05-18 09:39:05 +00002032 struct net_device_stats *stats = &vptr->netdev->stats;
Dave Jones2cf71d22009-07-23 18:11:12 -07002033 struct velocity_rd_info *rd_info = &(vptr->rx.info[idx]);
2034 struct rx_desc *rd = &(vptr->rx.ring[idx]);
2035 int pkt_len = le16_to_cpu(rd->rdesc0.len) & 0x3fff;
2036 struct sk_buff *skb;
2037
2038 if (rd->rdesc0.RSR & (RSR_STP | RSR_EDP)) {
Tony Priska9683c92013-05-18 09:39:05 +00002039 VELOCITY_PRT(MSG_LEVEL_VERBOSE, KERN_ERR " %s : the received frame span multple RDs.\n", vptr->netdev->name);
Dave Jones2cf71d22009-07-23 18:11:12 -07002040 stats->rx_length_errors++;
2041 return -EINVAL;
2042 }
2043
2044 if (rd->rdesc0.RSR & RSR_MAR)
2045 stats->multicast++;
2046
2047 skb = rd_info->skb;
2048
Tony Priske2c41f12013-05-18 09:39:06 +00002049 dma_sync_single_for_cpu(vptr->dev, rd_info->skb_dma,
2050 vptr->rx.buf_sz, DMA_FROM_DEVICE);
Dave Jones2cf71d22009-07-23 18:11:12 -07002051
2052 /*
2053 * Drop frame not meeting IEEE 802.3
2054 */
2055
2056 if (vptr->flags & VELOCITY_FLAGS_VAL_PKT_LEN) {
2057 if (rd->rdesc0.RSR & RSR_RL) {
2058 stats->rx_length_errors++;
2059 return -EINVAL;
2060 }
2061 }
2062
Dave Jones2cf71d22009-07-23 18:11:12 -07002063 velocity_rx_csum(rd, skb);
2064
2065 if (velocity_rx_copy(&skb, pkt_len, vptr) < 0) {
2066 velocity_iph_realign(vptr, skb, pkt_len);
Dave Jones2cf71d22009-07-23 18:11:12 -07002067 rd_info->skb = NULL;
Tony Priske2c41f12013-05-18 09:39:06 +00002068 dma_unmap_single(vptr->dev, rd_info->skb_dma, vptr->rx.buf_sz,
2069 DMA_FROM_DEVICE);
2070 } else {
2071 dma_sync_single_for_device(vptr->dev, rd_info->skb_dma,
2072 vptr->rx.buf_sz, DMA_FROM_DEVICE);
Dave Jones2cf71d22009-07-23 18:11:12 -07002073 }
2074
Dave Jones2cf71d22009-07-23 18:11:12 -07002075 skb_put(skb, pkt_len - 4);
Tony Priska9683c92013-05-18 09:39:05 +00002076 skb->protocol = eth_type_trans(skb, vptr->netdev);
Dave Jones2cf71d22009-07-23 18:11:12 -07002077
Jiri Pirko73b54682011-07-20 04:54:30 +00002078 if (rd->rdesc0.RSR & RSR_DETAG) {
2079 u16 vid = swab16(le16_to_cpu(rd->rdesc1.PQTAG));
2080
Patrick McHardy86a9bad2013-04-19 02:04:30 +00002081 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
Jiri Pirko73b54682011-07-20 04:54:30 +00002082 }
2083 netif_rx(skb);
Dave Jones2cf71d22009-07-23 18:11:12 -07002084
2085 stats->rx_bytes += pkt_len;
françois romieu3cb7a792011-08-04 02:37:55 +00002086 stats->rx_packets++;
Dave Jones2cf71d22009-07-23 18:11:12 -07002087
2088 return 0;
2089}
2090
Dave Jones2cf71d22009-07-23 18:11:12 -07002091/**
2092 * velocity_rx_srv - service RX interrupt
2093 * @vptr: velocity
Dave Jones2cf71d22009-07-23 18:11:12 -07002094 *
2095 * Walk the receive ring of the velocity adapter and remove
2096 * any received packets from the receive queue. Hand the ring
2097 * slots back to the adapter for reuse.
2098 */
Simon Kagstromd6cade02010-02-09 23:37:54 +00002099static int velocity_rx_srv(struct velocity_info *vptr, int budget_left)
Dave Jones2cf71d22009-07-23 18:11:12 -07002100{
Tony Priska9683c92013-05-18 09:39:05 +00002101 struct net_device_stats *stats = &vptr->netdev->stats;
Dave Jones2cf71d22009-07-23 18:11:12 -07002102 int rd_curr = vptr->rx.curr;
2103 int works = 0;
2104
Simon Kagstromdfff7142009-11-25 22:10:26 +00002105 while (works < budget_left) {
Dave Jones2cf71d22009-07-23 18:11:12 -07002106 struct rx_desc *rd = vptr->rx.ring + rd_curr;
2107
2108 if (!vptr->rx.info[rd_curr].skb)
2109 break;
2110
2111 if (rd->rdesc0.len & OWNED_BY_NIC)
2112 break;
2113
2114 rmb();
2115
2116 /*
2117 * Don't drop CE or RL error frame although RXOK is off
2118 */
2119 if (rd->rdesc0.RSR & (RSR_RXOK | RSR_CE | RSR_RL)) {
2120 if (velocity_receive_frame(vptr, rd_curr) < 0)
2121 stats->rx_dropped++;
2122 } else {
2123 if (rd->rdesc0.RSR & RSR_CRC)
2124 stats->rx_crc_errors++;
2125 if (rd->rdesc0.RSR & RSR_FAE)
2126 stats->rx_frame_errors++;
2127
2128 stats->rx_dropped++;
2129 }
2130
2131 rd->size |= RX_INTEN;
2132
2133 rd_curr++;
2134 if (rd_curr >= vptr->options.numrx)
2135 rd_curr = 0;
Simon Kagstromdfff7142009-11-25 22:10:26 +00002136 works++;
2137 }
Dave Jones2cf71d22009-07-23 18:11:12 -07002138
2139 vptr->rx.curr = rd_curr;
2140
2141 if ((works > 0) && (velocity_rx_refill(vptr) > 0))
2142 velocity_give_many_rx_descs(vptr);
2143
2144 VAR_USED(stats);
2145 return works;
2146}
2147
Simon Kagstromdfff7142009-11-25 22:10:26 +00002148static int velocity_poll(struct napi_struct *napi, int budget)
2149{
2150 struct velocity_info *vptr = container_of(napi,
2151 struct velocity_info, napi);
2152 unsigned int rx_done;
Simon Kagstrom3f2e8d92010-02-09 23:38:25 +00002153 unsigned long flags;
Simon Kagstromdfff7142009-11-25 22:10:26 +00002154
Simon Kagstrom3f2e8d92010-02-09 23:38:25 +00002155 spin_lock_irqsave(&vptr->lock, flags);
Simon Kagstromdfff7142009-11-25 22:10:26 +00002156 /*
2157 * Do rx and tx twice for performance (taken from the VIA
2158 * out-of-tree driver).
2159 */
Simon Kagstromd6cade02010-02-09 23:37:54 +00002160 rx_done = velocity_rx_srv(vptr, budget / 2);
2161 velocity_tx_srv(vptr);
2162 rx_done += velocity_rx_srv(vptr, budget - rx_done);
2163 velocity_tx_srv(vptr);
Simon Kagstromdfff7142009-11-25 22:10:26 +00002164
2165 /* If budget not fully consumed, exit the polling mode */
2166 if (rx_done < budget) {
2167 napi_complete(napi);
2168 mac_enable_int(vptr->mac_regs);
2169 }
Simon Kagstrom3f2e8d92010-02-09 23:38:25 +00002170 spin_unlock_irqrestore(&vptr->lock, flags);
Simon Kagstromdfff7142009-11-25 22:10:26 +00002171
2172 return rx_done;
2173}
Dave Jones2cf71d22009-07-23 18:11:12 -07002174
2175/**
2176 * velocity_intr - interrupt callback
2177 * @irq: interrupt number
2178 * @dev_instance: interrupting device
2179 *
2180 * Called whenever an interrupt is generated by the velocity
2181 * adapter IRQ line. We may not be the source of the interrupt
2182 * and need to identify initially if we are, and if not exit as
2183 * efficiently as possible.
2184 */
2185static irqreturn_t velocity_intr(int irq, void *dev_instance)
2186{
2187 struct net_device *dev = dev_instance;
2188 struct velocity_info *vptr = netdev_priv(dev);
2189 u32 isr_status;
Dave Jones2cf71d22009-07-23 18:11:12 -07002190
2191 spin_lock(&vptr->lock);
2192 isr_status = mac_read_isr(vptr->mac_regs);
2193
2194 /* Not us ? */
2195 if (isr_status == 0) {
2196 spin_unlock(&vptr->lock);
2197 return IRQ_NONE;
2198 }
2199
Simon Kagstrom3f2e8d92010-02-09 23:38:25 +00002200 /* Ack the interrupt */
2201 mac_write_isr(vptr->mac_regs, isr_status);
2202
Simon Kagstromdfff7142009-11-25 22:10:26 +00002203 if (likely(napi_schedule_prep(&vptr->napi))) {
2204 mac_disable_int(vptr->mac_regs);
2205 __napi_schedule(&vptr->napi);
Dave Jones2cf71d22009-07-23 18:11:12 -07002206 }
Simon Kagstrom3f2e8d92010-02-09 23:38:25 +00002207
2208 if (isr_status & (~(ISR_PRXI | ISR_PPRXI | ISR_PTXI | ISR_PPTXI)))
2209 velocity_error(vptr, isr_status);
2210
Dave Jones2cf71d22009-07-23 18:11:12 -07002211 spin_unlock(&vptr->lock);
Dave Jones2cf71d22009-07-23 18:11:12 -07002212
Simon Kagstromdfff7142009-11-25 22:10:26 +00002213 return IRQ_HANDLED;
Dave Jones2cf71d22009-07-23 18:11:12 -07002214}
2215
2216/**
2217 * velocity_open - interface activation callback
2218 * @dev: network layer device to open
2219 *
2220 * Called when the network layer brings the interface up. Returns
2221 * a negative posix error code on failure, or zero on success.
2222 *
2223 * All the ring allocation and set up is done on open for this
2224 * adapter to minimise memory usage when inactive
2225 */
2226static int velocity_open(struct net_device *dev)
2227{
2228 struct velocity_info *vptr = netdev_priv(dev);
2229 int ret;
2230
2231 ret = velocity_init_rings(vptr, dev->mtu);
2232 if (ret < 0)
2233 goto out;
2234
2235 /* Ensure chip is running */
2236 pci_set_power_state(vptr->pdev, PCI_D0);
2237
Dave Jones2cf71d22009-07-23 18:11:12 -07002238 velocity_init_registers(vptr, VELOCITY_INIT_COLD);
2239
Julia Lawall1ede9b52009-11-18 08:24:13 +00002240 ret = request_irq(vptr->pdev->irq, velocity_intr, IRQF_SHARED,
Dave Jones2cf71d22009-07-23 18:11:12 -07002241 dev->name, dev);
2242 if (ret < 0) {
2243 /* Power down the chip */
2244 pci_set_power_state(vptr->pdev, PCI_D3hot);
2245 velocity_free_rings(vptr);
2246 goto out;
2247 }
2248
Ben Hutchings35bb5ca2009-12-14 16:05:09 +00002249 velocity_give_many_rx_descs(vptr);
2250
Dave Jones2cf71d22009-07-23 18:11:12 -07002251 mac_enable_int(vptr->mac_regs);
2252 netif_start_queue(dev);
Simon Kagstromdfff7142009-11-25 22:10:26 +00002253 napi_enable(&vptr->napi);
Dave Jones2cf71d22009-07-23 18:11:12 -07002254 vptr->flags |= VELOCITY_FLAGS_OPENED;
2255out:
2256 return ret;
2257}
2258
2259/**
2260 * velocity_shutdown - shut down the chip
2261 * @vptr: velocity to deactivate
2262 *
2263 * Shuts down the internal operations of the velocity and
2264 * disables interrupts, autopolling, transmit and receive
2265 */
2266static void velocity_shutdown(struct velocity_info *vptr)
2267{
2268 struct mac_regs __iomem *regs = vptr->mac_regs;
2269 mac_disable_int(regs);
2270 writel(CR0_STOP, &regs->CR0Set);
2271 writew(0xFFFF, &regs->TDCSRClr);
2272 writeb(0xFF, &regs->RDCSRClr);
2273 safe_disable_mii_autopoll(regs);
2274 mac_clear_isr(regs);
2275}
2276
2277/**
2278 * velocity_change_mtu - MTU change callback
2279 * @dev: network device
2280 * @new_mtu: desired MTU
2281 *
2282 * Handle requests from the networking layer for MTU change on
2283 * this interface. It gets called on a change by the network layer.
2284 * Return zero for success or negative posix error code.
2285 */
2286static int velocity_change_mtu(struct net_device *dev, int new_mtu)
2287{
2288 struct velocity_info *vptr = netdev_priv(dev);
2289 int ret = 0;
2290
2291 if ((new_mtu < VELOCITY_MIN_MTU) || new_mtu > (VELOCITY_MAX_MTU)) {
2292 VELOCITY_PRT(MSG_LEVEL_ERR, KERN_NOTICE "%s: Invalid MTU.\n",
Tony Priska9683c92013-05-18 09:39:05 +00002293 vptr->netdev->name);
Dave Jones2cf71d22009-07-23 18:11:12 -07002294 ret = -EINVAL;
2295 goto out_0;
2296 }
2297
2298 if (!netif_running(dev)) {
2299 dev->mtu = new_mtu;
2300 goto out_0;
2301 }
2302
2303 if (dev->mtu != new_mtu) {
2304 struct velocity_info *tmp_vptr;
2305 unsigned long flags;
2306 struct rx_info rx;
2307 struct tx_info tx;
2308
2309 tmp_vptr = kzalloc(sizeof(*tmp_vptr), GFP_KERNEL);
2310 if (!tmp_vptr) {
2311 ret = -ENOMEM;
2312 goto out_0;
2313 }
2314
Tony Priska9683c92013-05-18 09:39:05 +00002315 tmp_vptr->netdev = dev;
Dave Jones2cf71d22009-07-23 18:11:12 -07002316 tmp_vptr->pdev = vptr->pdev;
2317 tmp_vptr->options = vptr->options;
2318 tmp_vptr->tx.numq = vptr->tx.numq;
2319
2320 ret = velocity_init_rings(tmp_vptr, new_mtu);
2321 if (ret < 0)
2322 goto out_free_tmp_vptr_1;
2323
2324 spin_lock_irqsave(&vptr->lock, flags);
2325
2326 netif_stop_queue(dev);
2327 velocity_shutdown(vptr);
2328
2329 rx = vptr->rx;
2330 tx = vptr->tx;
2331
2332 vptr->rx = tmp_vptr->rx;
2333 vptr->tx = tmp_vptr->tx;
2334
2335 tmp_vptr->rx = rx;
2336 tmp_vptr->tx = tx;
2337
2338 dev->mtu = new_mtu;
2339
Dave Jones2cf71d22009-07-23 18:11:12 -07002340 velocity_init_registers(vptr, VELOCITY_INIT_COLD);
2341
Ben Hutchings35bb5ca2009-12-14 16:05:09 +00002342 velocity_give_many_rx_descs(vptr);
2343
Dave Jones2cf71d22009-07-23 18:11:12 -07002344 mac_enable_int(vptr->mac_regs);
2345 netif_start_queue(dev);
2346
2347 spin_unlock_irqrestore(&vptr->lock, flags);
2348
2349 velocity_free_rings(tmp_vptr);
2350
2351out_free_tmp_vptr_1:
2352 kfree(tmp_vptr);
2353 }
2354out_0:
2355 return ret;
2356}
2357
2358/**
2359 * velocity_mii_ioctl - MII ioctl handler
2360 * @dev: network device
2361 * @ifr: the ifreq block for the ioctl
2362 * @cmd: the command
2363 *
2364 * Process MII requests made via ioctl from the network layer. These
2365 * are used by tools like kudzu to interrogate the link state of the
2366 * hardware
2367 */
2368static int velocity_mii_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2369{
2370 struct velocity_info *vptr = netdev_priv(dev);
2371 struct mac_regs __iomem *regs = vptr->mac_regs;
2372 unsigned long flags;
2373 struct mii_ioctl_data *miidata = if_mii(ifr);
2374 int err;
2375
2376 switch (cmd) {
2377 case SIOCGMIIPHY:
2378 miidata->phy_id = readb(&regs->MIIADR) & 0x1f;
2379 break;
2380 case SIOCGMIIREG:
Dave Jones2cf71d22009-07-23 18:11:12 -07002381 if (velocity_mii_read(vptr->mac_regs, miidata->reg_num & 0x1f, &(miidata->val_out)) < 0)
2382 return -ETIMEDOUT;
2383 break;
2384 case SIOCSMIIREG:
Dave Jones2cf71d22009-07-23 18:11:12 -07002385 spin_lock_irqsave(&vptr->lock, flags);
2386 err = velocity_mii_write(vptr->mac_regs, miidata->reg_num & 0x1f, miidata->val_in);
2387 spin_unlock_irqrestore(&vptr->lock, flags);
2388 check_connection_type(vptr->mac_regs);
2389 if (err)
2390 return err;
2391 break;
2392 default:
2393 return -EOPNOTSUPP;
2394 }
2395 return 0;
2396}
2397
Dave Jones2cf71d22009-07-23 18:11:12 -07002398/**
2399 * velocity_ioctl - ioctl entry point
2400 * @dev: network device
2401 * @rq: interface request ioctl
2402 * @cmd: command code
2403 *
2404 * Called when the user issues an ioctl request to the network
2405 * device in question. The velocity interface supports MII.
2406 */
2407static int velocity_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2408{
2409 struct velocity_info *vptr = netdev_priv(dev);
2410 int ret;
2411
2412 /* If we are asked for information and the device is power
2413 saving then we need to bring the device back up to talk to it */
2414
2415 if (!netif_running(dev))
2416 pci_set_power_state(vptr->pdev, PCI_D0);
2417
2418 switch (cmd) {
2419 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
2420 case SIOCGMIIREG: /* Read MII PHY register. */
2421 case SIOCSMIIREG: /* Write to MII PHY register. */
2422 ret = velocity_mii_ioctl(dev, rq, cmd);
2423 break;
2424
2425 default:
2426 ret = -EOPNOTSUPP;
2427 }
2428 if (!netif_running(dev))
2429 pci_set_power_state(vptr->pdev, PCI_D3hot);
2430
2431
2432 return ret;
2433}
2434
2435/**
2436 * velocity_get_status - statistics callback
2437 * @dev: network device
2438 *
2439 * Callback from the network layer to allow driver statistics
2440 * to be resynchronized with hardware collected state. In the
2441 * case of the velocity we need to pull the MIB counters from
2442 * the hardware into the counters before letting the network
2443 * layer display them.
2444 */
2445static struct net_device_stats *velocity_get_stats(struct net_device *dev)
2446{
2447 struct velocity_info *vptr = netdev_priv(dev);
2448
2449 /* If the hardware is down, don't touch MII */
2450 if (!netif_running(dev))
2451 return &dev->stats;
2452
2453 spin_lock_irq(&vptr->lock);
2454 velocity_update_hw_mibs(vptr);
2455 spin_unlock_irq(&vptr->lock);
2456
2457 dev->stats.rx_packets = vptr->mib_counter[HW_MIB_ifRxAllPkts];
2458 dev->stats.rx_errors = vptr->mib_counter[HW_MIB_ifRxErrorPkts];
2459 dev->stats.rx_length_errors = vptr->mib_counter[HW_MIB_ifInRangeLengthErrors];
2460
2461// unsigned long rx_dropped; /* no space in linux buffers */
2462 dev->stats.collisions = vptr->mib_counter[HW_MIB_ifTxEtherCollisions];
2463 /* detailed rx_errors: */
2464// unsigned long rx_length_errors;
2465// unsigned long rx_over_errors; /* receiver ring buff overflow */
2466 dev->stats.rx_crc_errors = vptr->mib_counter[HW_MIB_ifRxPktCRCE];
2467// unsigned long rx_frame_errors; /* recv'd frame alignment error */
2468// unsigned long rx_fifo_errors; /* recv'r fifo overrun */
2469// unsigned long rx_missed_errors; /* receiver missed packet */
2470
2471 /* detailed tx_errors */
2472// unsigned long tx_fifo_errors;
2473
2474 return &dev->stats;
2475}
2476
2477/**
2478 * velocity_close - close adapter callback
2479 * @dev: network device
2480 *
2481 * Callback from the network layer when the velocity is being
2482 * deactivated by the network layer
2483 */
2484static int velocity_close(struct net_device *dev)
2485{
2486 struct velocity_info *vptr = netdev_priv(dev);
2487
Simon Kagstromdfff7142009-11-25 22:10:26 +00002488 napi_disable(&vptr->napi);
Dave Jones2cf71d22009-07-23 18:11:12 -07002489 netif_stop_queue(dev);
2490 velocity_shutdown(vptr);
2491
2492 if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED)
2493 velocity_get_ip(vptr);
Francois Romieudfda3572012-03-09 15:24:37 +01002494
2495 free_irq(vptr->pdev->irq, dev);
Dave Jones2cf71d22009-07-23 18:11:12 -07002496
Dave Jones2cf71d22009-07-23 18:11:12 -07002497 velocity_free_rings(vptr);
2498
2499 vptr->flags &= (~VELOCITY_FLAGS_OPENED);
2500 return 0;
2501}
2502
2503/**
2504 * velocity_xmit - transmit packet callback
2505 * @skb: buffer to transmit
2506 * @dev: network device
2507 *
2508 * Called by the networ layer to request a packet is queued to
2509 * the velocity. Returns zero on success.
2510 */
Stephen Hemminger613573252009-08-31 19:50:58 +00002511static netdev_tx_t velocity_xmit(struct sk_buff *skb,
2512 struct net_device *dev)
Dave Jones2cf71d22009-07-23 18:11:12 -07002513{
2514 struct velocity_info *vptr = netdev_priv(dev);
2515 int qnum = 0;
2516 struct tx_desc *td_ptr;
2517 struct velocity_td_info *tdinfo;
2518 unsigned long flags;
2519 int pktlen;
Simon Kagstromc79992f2009-11-25 22:10:43 +00002520 int index, prev;
2521 int i = 0;
Dave Jones2cf71d22009-07-23 18:11:12 -07002522
2523 if (skb_padto(skb, ETH_ZLEN))
2524 goto out;
Dave Jones2cf71d22009-07-23 18:11:12 -07002525
Simon Kagstromc79992f2009-11-25 22:10:43 +00002526 /* The hardware can handle at most 7 memory segments, so merge
2527 * the skb if there are more */
2528 if (skb_shinfo(skb)->nr_frags > 6 && __skb_linearize(skb)) {
2529 kfree_skb(skb);
2530 return NETDEV_TX_OK;
2531 }
2532
2533 pktlen = skb_shinfo(skb)->nr_frags == 0 ?
2534 max_t(unsigned int, skb->len, ETH_ZLEN) :
2535 skb_headlen(skb);
Dave Jones2cf71d22009-07-23 18:11:12 -07002536
2537 spin_lock_irqsave(&vptr->lock, flags);
2538
2539 index = vptr->tx.curr[qnum];
2540 td_ptr = &(vptr->tx.rings[qnum][index]);
2541 tdinfo = &(vptr->tx.infos[qnum][index]);
2542
2543 td_ptr->tdesc1.TCR = TCR0_TIC;
2544 td_ptr->td_buf[0].size &= ~TD_QUEUE;
2545
2546 /*
2547 * Map the linear network buffer into PCI space and
2548 * add it to the transmit ring.
2549 */
2550 tdinfo->skb = skb;
Tony Priske2c41f12013-05-18 09:39:06 +00002551 tdinfo->skb_dma[0] = dma_map_single(vptr->dev, skb->data, pktlen,
2552 DMA_TO_DEVICE);
Simon Kagstromc79992f2009-11-25 22:10:43 +00002553 td_ptr->tdesc0.len = cpu_to_le16(pktlen);
Dave Jones2cf71d22009-07-23 18:11:12 -07002554 td_ptr->td_buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]);
2555 td_ptr->td_buf[0].pa_high = 0;
Simon Kagstromc79992f2009-11-25 22:10:43 +00002556 td_ptr->td_buf[0].size = cpu_to_le16(pktlen);
2557
2558 /* Handle fragments */
2559 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002560 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Simon Kagstromc79992f2009-11-25 22:10:43 +00002561
Tony Priske2c41f12013-05-18 09:39:06 +00002562 tdinfo->skb_dma[i + 1] = skb_frag_dma_map(vptr->dev,
Ian Campbelle4cb1932011-09-21 21:53:26 +00002563 frag, 0,
Eric Dumazet9e903e02011-10-18 21:00:24 +00002564 skb_frag_size(frag),
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01002565 DMA_TO_DEVICE);
Simon Kagstromc79992f2009-11-25 22:10:43 +00002566
2567 td_ptr->td_buf[i + 1].pa_low = cpu_to_le32(tdinfo->skb_dma[i + 1]);
2568 td_ptr->td_buf[i + 1].pa_high = 0;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002569 td_ptr->td_buf[i + 1].size = cpu_to_le16(skb_frag_size(frag));
Simon Kagstromc79992f2009-11-25 22:10:43 +00002570 }
2571 tdinfo->nskb_dma = i + 1;
Dave Jones2cf71d22009-07-23 18:11:12 -07002572
2573 td_ptr->tdesc1.cmd = TCPLS_NORMAL + (tdinfo->nskb_dma + 1) * 16;
2574
Jesse Grosseab6d182010-10-20 13:56:03 +00002575 if (vlan_tx_tag_present(skb)) {
Dave Jones2cf71d22009-07-23 18:11:12 -07002576 td_ptr->tdesc1.vlan = cpu_to_le16(vlan_tx_tag_get(skb));
2577 td_ptr->tdesc1.TCR |= TCR0_VETAG;
2578 }
2579
2580 /*
2581 * Handle hardware checksum
2582 */
Michał Mirosławf593fe32011-04-10 03:13:21 +00002583 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Dave Jones2cf71d22009-07-23 18:11:12 -07002584 const struct iphdr *ip = ip_hdr(skb);
2585 if (ip->protocol == IPPROTO_TCP)
2586 td_ptr->tdesc1.TCR |= TCR0_TCPCK;
2587 else if (ip->protocol == IPPROTO_UDP)
2588 td_ptr->tdesc1.TCR |= (TCR0_UDPCK);
2589 td_ptr->tdesc1.TCR |= TCR0_IPCK;
2590 }
Dave Jones2cf71d22009-07-23 18:11:12 -07002591
Simon Kagstromc79992f2009-11-25 22:10:43 +00002592 prev = index - 1;
2593 if (prev < 0)
2594 prev = vptr->options.numtx - 1;
2595 td_ptr->tdesc0.len |= OWNED_BY_NIC;
2596 vptr->tx.used[qnum]++;
2597 vptr->tx.curr[qnum] = (index + 1) % vptr->options.numtx;
Dave Jones2cf71d22009-07-23 18:11:12 -07002598
Simon Kagstromc79992f2009-11-25 22:10:43 +00002599 if (AVAIL_TD(vptr, qnum) < 1)
2600 netif_stop_queue(dev);
Dave Jones2cf71d22009-07-23 18:11:12 -07002601
Simon Kagstromc79992f2009-11-25 22:10:43 +00002602 td_ptr = &(vptr->tx.rings[qnum][prev]);
2603 td_ptr->td_buf[0].size |= TD_QUEUE;
2604 mac_tx_queue_wake(vptr->mac_regs, qnum);
Dave Jones2cf71d22009-07-23 18:11:12 -07002605
Dave Jones2cf71d22009-07-23 18:11:12 -07002606 spin_unlock_irqrestore(&vptr->lock, flags);
2607out:
2608 return NETDEV_TX_OK;
2609}
2610
Stephen Hemminger39a11bd2008-11-19 22:19:33 -08002611static const struct net_device_ops velocity_netdev_ops = {
2612 .ndo_open = velocity_open,
2613 .ndo_stop = velocity_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08002614 .ndo_start_xmit = velocity_xmit,
Stephen Hemminger39a11bd2008-11-19 22:19:33 -08002615 .ndo_get_stats = velocity_get_stats,
2616 .ndo_validate_addr = eth_validate_addr,
françois romieu5ae297b2011-08-04 02:39:03 +00002617 .ndo_set_mac_address = eth_mac_addr,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002618 .ndo_set_rx_mode = velocity_set_multi,
Stephen Hemminger39a11bd2008-11-19 22:19:33 -08002619 .ndo_change_mtu = velocity_change_mtu,
2620 .ndo_do_ioctl = velocity_ioctl,
2621 .ndo_vlan_rx_add_vid = velocity_vlan_rx_add_vid,
2622 .ndo_vlan_rx_kill_vid = velocity_vlan_rx_kill_vid,
Stephen Hemminger39a11bd2008-11-19 22:19:33 -08002623};
2624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625/**
Dave Jones2cf71d22009-07-23 18:11:12 -07002626 * velocity_init_info - init private data
2627 * @pdev: PCI device
2628 * @vptr: Velocity info
2629 * @info: Board type
2630 *
2631 * Set up the initial velocity_info struct for the device that has been
2632 * discovered.
2633 */
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002634static void velocity_init_info(struct pci_dev *pdev, struct velocity_info *vptr,
2635 const struct velocity_info_tbl *info)
Dave Jones2cf71d22009-07-23 18:11:12 -07002636{
2637 memset(vptr, 0, sizeof(struct velocity_info));
2638
Tony Priske2c41f12013-05-18 09:39:06 +00002639 vptr->dev = &pdev->dev;
Dave Jones2cf71d22009-07-23 18:11:12 -07002640 vptr->pdev = pdev;
2641 vptr->chip_id = info->chip_id;
2642 vptr->tx.numq = info->txqueue;
2643 vptr->multicast_limit = MCAM_SIZE;
2644 spin_lock_init(&vptr->lock);
Dave Jones2cf71d22009-07-23 18:11:12 -07002645}
2646
2647/**
2648 * velocity_get_pci_info - retrieve PCI info for device
2649 * @vptr: velocity device
2650 * @pdev: PCI device it matches
2651 *
2652 * Retrieve the PCI configuration space data that interests us from
2653 * the kernel PCI layer
2654 */
Bill Pemberton27add002012-12-03 09:23:49 -05002655static int velocity_get_pci_info(struct velocity_info *vptr,
2656 struct pci_dev *pdev)
Dave Jones2cf71d22009-07-23 18:11:12 -07002657{
2658 vptr->rev_id = pdev->revision;
2659
2660 pci_set_master(pdev);
2661
2662 vptr->ioaddr = pci_resource_start(pdev, 0);
2663 vptr->memaddr = pci_resource_start(pdev, 1);
2664
2665 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_IO)) {
2666 dev_err(&pdev->dev,
2667 "region #0 is not an I/O resource, aborting.\n");
2668 return -EINVAL;
2669 }
2670
2671 if ((pci_resource_flags(pdev, 1) & IORESOURCE_IO)) {
2672 dev_err(&pdev->dev,
2673 "region #1 is an I/O resource, aborting.\n");
2674 return -EINVAL;
2675 }
2676
2677 if (pci_resource_len(pdev, 1) < VELOCITY_IO_SIZE) {
2678 dev_err(&pdev->dev, "region #1 is too small.\n");
2679 return -EINVAL;
2680 }
2681 vptr->pdev = pdev;
2682
2683 return 0;
2684}
2685
2686/**
2687 * velocity_print_info - per driver data
2688 * @vptr: velocity
2689 *
2690 * Print per driver data as the kernel driver finds Velocity
2691 * hardware
2692 */
Bill Pemberton27add002012-12-03 09:23:49 -05002693static void velocity_print_info(struct velocity_info *vptr)
Dave Jones2cf71d22009-07-23 18:11:12 -07002694{
Tony Priska9683c92013-05-18 09:39:05 +00002695 struct net_device *dev = vptr->netdev;
Dave Jones2cf71d22009-07-23 18:11:12 -07002696
2697 printk(KERN_INFO "%s: %s\n", dev->name, get_chip_name(vptr->chip_id));
H Hartley Sweetenaa7c68a2009-12-29 20:04:14 -08002698 printk(KERN_INFO "%s: Ethernet Address: %pM\n",
2699 dev->name, dev->dev_addr);
Dave Jones2cf71d22009-07-23 18:11:12 -07002700}
2701
2702static u32 velocity_get_link(struct net_device *dev)
2703{
2704 struct velocity_info *vptr = netdev_priv(dev);
2705 struct mac_regs __iomem *regs = vptr->mac_regs;
2706 return BYTE_REG_BITS_IS_ON(PHYSR0_LINKGD, &regs->PHYSR0) ? 1 : 0;
2707}
2708
Dave Jones2cf71d22009-07-23 18:11:12 -07002709/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 * velocity_found1 - set up discovered velocity card
2711 * @pdev: PCI device
2712 * @ent: PCI device table entry that matched
2713 *
2714 * Configure a discovered adapter from scratch. Return a negative
2715 * errno error code on failure paths.
2716 */
Bill Pemberton27add002012-12-03 09:23:49 -05002717static int velocity_found1(struct pci_dev *pdev,
2718 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
2720 static int first = 1;
2721 struct net_device *dev;
2722 int i;
Sven Hartge07b5f6a2008-10-23 13:03:44 +00002723 const char *drv_string;
Jeff Garzikcabb7662006-06-27 09:25:28 -04002724 const struct velocity_info_tbl *info = &chip_info_table[ent->driver_data];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 struct velocity_info *vptr;
Dave Jonesc4067402009-07-20 17:35:21 +00002726 struct mac_regs __iomem *regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 int ret = -ENOMEM;
2728
Jeff Garzike54f4892006-06-27 09:20:08 -04002729 /* FIXME: this driver, like almost all other ethernet drivers,
2730 * can support more than MAX_UNITS.
2731 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 if (velocity_nics >= MAX_UNITS) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002733 dev_notice(&pdev->dev, "already found %d NICs.\n",
Jeff Garzike54f4892006-06-27 09:20:08 -04002734 velocity_nics);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 return -ENODEV;
2736 }
2737
2738 dev = alloc_etherdev(sizeof(struct velocity_info));
Joe Perches41de8d42012-01-29 13:47:52 +00002739 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 goto out;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 /* Chain it all together */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002743
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 SET_NETDEV_DEV(dev, &pdev->dev);
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04002745 vptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 if (first) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002748 printk(KERN_INFO "%s Ver. %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 VELOCITY_FULL_DRV_NAM, VELOCITY_VERSION);
2750 printk(KERN_INFO "Copyright (c) 2002, 2003 VIA Networking Technologies, Inc.\n");
2751 printk(KERN_INFO "Copyright (c) 2004 Red Hat Inc.\n");
2752 first = 0;
2753 }
2754
2755 velocity_init_info(pdev, vptr, info);
2756
Tony Priska9683c92013-05-18 09:39:05 +00002757 vptr->netdev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 ret = pci_enable_device(pdev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002760 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 goto err_free_dev;
2762
2763 ret = velocity_get_pci_info(vptr, pdev);
2764 if (ret < 0) {
Jeff Garzike54f4892006-06-27 09:20:08 -04002765 /* error message already printed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 goto err_disable;
2767 }
2768
2769 ret = pci_request_regions(pdev, VELOCITY_NAME);
2770 if (ret < 0) {
Jeff Garzik9b91cf92006-06-27 11:39:50 -04002771 dev_err(&pdev->dev, "No PCI resources.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 goto err_disable;
2773 }
2774
Jeff Garzikcabb7662006-06-27 09:25:28 -04002775 regs = ioremap(vptr->memaddr, VELOCITY_IO_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 if (regs == NULL) {
2777 ret = -EIO;
2778 goto err_release_res;
2779 }
2780
2781 vptr->mac_regs = regs;
2782
2783 mac_wol_reset(regs);
2784
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 for (i = 0; i < 6; i++)
2786 dev->dev_addr[i] = readb(&regs->PAR[i]);
2787
2788
Sven Hartge07b5f6a2008-10-23 13:03:44 +00002789 drv_string = dev_driver_string(&pdev->dev);
2790
2791 velocity_get_options(&vptr->options, velocity_nics, drv_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002793 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 * Mask out the options cannot be set to the chip
2795 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002796
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 vptr->options.flags &= info->flags;
2798
2799 /*
2800 * Enable the chip specified capbilities
2801 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 vptr->flags = vptr->options.flags | (info->flags & 0xFF000000UL);
2804
2805 vptr->wol_opts = vptr->options.wol_opts;
2806 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
2807
2808 vptr->phy_id = MII_GET_PHY_ID(vptr->mac_regs);
2809
Stephen Hemminger39a11bd2008-11-19 22:19:33 -08002810 dev->netdev_ops = &velocity_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 dev->ethtool_ops = &velocity_ethtool_ops;
Simon Kagstromdfff7142009-11-25 22:10:26 +00002812 netif_napi_add(dev, &vptr->napi, velocity_poll, VELOCITY_NAPI_WEIGHT);
Stephen Hemminger501e4d22007-08-24 13:56:49 -07002813
Patrick McHardyf6469682013-04-19 02:04:27 +00002814 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
2815 NETIF_F_HW_VLAN_CTAG_TX;
2816 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_FILTER |
2817 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_IP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
2819 ret = register_netdev(dev);
2820 if (ret < 0)
2821 goto err_iounmap;
2822
Séguier Régisd3b238a2009-06-16 11:25:49 +00002823 if (!velocity_get_link(dev)) {
Francois Romieu8a22ddd2006-06-23 00:47:06 +02002824 netif_carrier_off(dev);
Séguier Régisd3b238a2009-06-16 11:25:49 +00002825 vptr->mii_status |= VELOCITY_LINK_FAIL;
2826 }
Francois Romieu8a22ddd2006-06-23 00:47:06 +02002827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 velocity_print_info(vptr);
2829 pci_set_drvdata(pdev, dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002830
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 /* and leave the chip powered down */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 pci_set_power_state(pdev, PCI_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 velocity_nics++;
2835out:
2836 return ret;
2837
2838err_iounmap:
2839 iounmap(regs);
2840err_release_res:
2841 pci_release_regions(pdev);
2842err_disable:
2843 pci_disable_device(pdev);
2844err_free_dev:
2845 free_netdev(dev);
2846 goto out;
2847}
2848
Dave Jones2cf71d22009-07-23 18:11:12 -07002849#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850/**
Dave Jones2cf71d22009-07-23 18:11:12 -07002851 * wol_calc_crc - WOL CRC
2852 * @pattern: data pattern
2853 * @mask_pattern: mask
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 *
Dave Jones2cf71d22009-07-23 18:11:12 -07002855 * Compute the wake on lan crc hashes for the packet header
2856 * we are interested in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 */
Dave Jones2cf71d22009-07-23 18:11:12 -07002858static u16 wol_calc_crc(int size, u8 *pattern, u8 *mask_pattern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859{
Dave Jones2cf71d22009-07-23 18:11:12 -07002860 u16 crc = 0xFFFF;
2861 u8 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 int i, j;
2863
Dave Jones2cf71d22009-07-23 18:11:12 -07002864 for (i = 0; i < size; i++) {
2865 mask = mask_pattern[i];
2866
2867 /* Skip this loop if the mask equals to zero */
2868 if (mask == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
Dave Jones2cf71d22009-07-23 18:11:12 -07002871 for (j = 0; j < 8; j++) {
2872 if ((mask & 0x01) == 0) {
2873 mask >>= 1;
2874 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 }
Dave Jones2cf71d22009-07-23 18:11:12 -07002876 mask >>= 1;
2877 crc = crc_ccitt(crc, &(pattern[i * 8 + j]), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 }
2879 }
Dave Jones2cf71d22009-07-23 18:11:12 -07002880 /* Finally, invert the result once to get the correct data */
2881 crc = ~crc;
2882 return bitrev32(crc) >> 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883}
2884
2885/**
Dave Jones2cf71d22009-07-23 18:11:12 -07002886 * velocity_set_wol - set up for wake on lan
2887 * @vptr: velocity to set WOL status on
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 *
Dave Jones2cf71d22009-07-23 18:11:12 -07002889 * Set a card up for wake on lan either by unicast or by
2890 * ARP packet.
2891 *
2892 * FIXME: check static buffer is safe here
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 */
Dave Jones2cf71d22009-07-23 18:11:12 -07002894static int velocity_set_wol(struct velocity_info *vptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895{
Dave Jonesc4067402009-07-20 17:35:21 +00002896 struct mac_regs __iomem *regs = vptr->mac_regs;
françois romieu2ffa0072011-01-20 04:59:33 +00002897 enum speed_opt spd_dpx = vptr->options.spd_dpx;
Dave Jones2cf71d22009-07-23 18:11:12 -07002898 static u8 buf[256];
2899 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
Dave Jones2cf71d22009-07-23 18:11:12 -07002901 static u32 mask_pattern[2][4] = {
2902 {0x00203000, 0x000003C0, 0x00000000, 0x0000000}, /* ARP */
2903 {0xfffff000, 0xffffffff, 0xffffffff, 0x000ffff} /* Magic Packet */
2904 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Dave Jones2cf71d22009-07-23 18:11:12 -07002906 writew(0xFFFF, &regs->WOLCRClr);
2907 writeb(WOLCFG_SAB | WOLCFG_SAM, &regs->WOLCFGSet);
2908 writew(WOLCR_MAGIC_EN, &regs->WOLCRSet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
Dave Jones2cf71d22009-07-23 18:11:12 -07002910 /*
2911 if (vptr->wol_opts & VELOCITY_WOL_PHY)
2912 writew((WOLCR_LINKON_EN|WOLCR_LINKOFF_EN), &regs->WOLCRSet);
2913 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Dave Jones2cf71d22009-07-23 18:11:12 -07002915 if (vptr->wol_opts & VELOCITY_WOL_UCAST)
2916 writew(WOLCR_UNICAST_EN, &regs->WOLCRSet);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002917
Dave Jones2cf71d22009-07-23 18:11:12 -07002918 if (vptr->wol_opts & VELOCITY_WOL_ARP) {
2919 struct arp_packet *arp = (struct arp_packet *) buf;
2920 u16 crc;
2921 memset(buf, 0, sizeof(struct arp_packet) + 7);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002922
Dave Jones2cf71d22009-07-23 18:11:12 -07002923 for (i = 0; i < 4; i++)
2924 writel(mask_pattern[0][i], &regs->ByteMask[0][i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
Dave Jones2cf71d22009-07-23 18:11:12 -07002926 arp->type = htons(ETH_P_ARP);
2927 arp->ar_op = htons(1);
2928
2929 memcpy(arp->ar_tip, vptr->ip_addr, 4);
2930
2931 crc = wol_calc_crc((sizeof(struct arp_packet) + 7) / 8, buf,
2932 (u8 *) & mask_pattern[0][0]);
2933
2934 writew(crc, &regs->PatternCRC[0]);
2935 writew(WOLCR_ARP_EN, &regs->WOLCRSet);
2936 }
2937
2938 BYTE_REG_BITS_ON(PWCFG_WOLTYPE, &regs->PWCFGSet);
2939 BYTE_REG_BITS_ON(PWCFG_LEGACY_WOLEN, &regs->PWCFGSet);
2940
2941 writew(0x0FFF, &regs->WOLSRClr);
2942
françois romieu2ffa0072011-01-20 04:59:33 +00002943 if (spd_dpx == SPD_DPX_1000_FULL)
2944 goto mac_done;
2945
2946 if (spd_dpx != SPD_DPX_AUTO)
2947 goto advertise_done;
2948
Dave Jones2cf71d22009-07-23 18:11:12 -07002949 if (vptr->mii_status & VELOCITY_AUTONEG_ENABLE) {
2950 if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201)
Francois Romieu3a7f8682010-04-06 14:24:53 -07002951 MII_REG_BITS_ON(AUXCR_MDPPS, MII_NCONFIG, vptr->mac_regs);
Dave Jones2cf71d22009-07-23 18:11:12 -07002952
Francois Romieu3a7f8682010-04-06 14:24:53 -07002953 MII_REG_BITS_OFF(ADVERTISE_1000FULL | ADVERTISE_1000HALF, MII_CTRL1000, vptr->mac_regs);
Dave Jones2cf71d22009-07-23 18:11:12 -07002954 }
2955
2956 if (vptr->mii_status & VELOCITY_SPEED_1000)
Francois Romieu3a7f8682010-04-06 14:24:53 -07002957 MII_REG_BITS_ON(BMCR_ANRESTART, MII_BMCR, vptr->mac_regs);
Dave Jones2cf71d22009-07-23 18:11:12 -07002958
françois romieu2ffa0072011-01-20 04:59:33 +00002959advertise_done:
Dave Jones2cf71d22009-07-23 18:11:12 -07002960 BYTE_REG_BITS_ON(CHIPGCR_FCMODE, &regs->CHIPGCR);
2961
2962 {
2963 u8 GCR;
2964 GCR = readb(&regs->CHIPGCR);
2965 GCR = (GCR & ~CHIPGCR_FCGMII) | CHIPGCR_FCFDX;
2966 writeb(GCR, &regs->CHIPGCR);
2967 }
2968
françois romieu2ffa0072011-01-20 04:59:33 +00002969mac_done:
Dave Jones2cf71d22009-07-23 18:11:12 -07002970 BYTE_REG_BITS_OFF(ISR_PWEI, &regs->ISR);
2971 /* Turn on SWPTAG just before entering power mode */
2972 BYTE_REG_BITS_ON(STICKHW_SWPTAG, &regs->STICKHW);
2973 /* Go to bed ..... */
2974 BYTE_REG_BITS_ON((STICKHW_DS1 | STICKHW_DS0), &regs->STICKHW);
2975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 return 0;
2977}
2978
2979/**
Dave Jones2cf71d22009-07-23 18:11:12 -07002980 * velocity_save_context - save registers
2981 * @vptr: velocity
2982 * @context: buffer for stored context
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 *
Dave Jones2cf71d22009-07-23 18:11:12 -07002984 * Retrieve the current configuration from the velocity hardware
2985 * and stash it in the context structure, for use by the context
2986 * restore functions. This allows us to save things we need across
2987 * power down states
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 */
Dave Jones2cf71d22009-07-23 18:11:12 -07002989static void velocity_save_context(struct velocity_info *vptr, struct velocity_context *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
Dave Jones2cf71d22009-07-23 18:11:12 -07002991 struct mac_regs __iomem *regs = vptr->mac_regs;
2992 u16 i;
2993 u8 __iomem *ptr = (u8 __iomem *)regs;
2994
2995 for (i = MAC_REG_PAR; i < MAC_REG_CR0_CLR; i += 4)
2996 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
2997
2998 for (i = MAC_REG_MAR; i < MAC_REG_TDCSR_CLR; i += 4)
2999 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3000
3001 for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4)
3002 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3003
3004}
3005
3006static int velocity_suspend(struct pci_dev *pdev, pm_message_t state)
3007{
3008 struct net_device *dev = pci_get_drvdata(pdev);
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04003009 struct velocity_info *vptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 unsigned long flags;
Francois Romieu580a6902008-07-11 00:03:44 +02003011
Tony Priska9683c92013-05-18 09:39:05 +00003012 if (!netif_running(vptr->netdev))
Dave Jones2cf71d22009-07-23 18:11:12 -07003013 return 0;
Francois Romieu580a6902008-07-11 00:03:44 +02003014
Tony Priska9683c92013-05-18 09:39:05 +00003015 netif_device_detach(vptr->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
3017 spin_lock_irqsave(&vptr->lock, flags);
Dave Jones2cf71d22009-07-23 18:11:12 -07003018 pci_save_state(pdev);
françois romieu5ae297b2011-08-04 02:39:03 +00003019
Dave Jones2cf71d22009-07-23 18:11:12 -07003020 if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED) {
3021 velocity_get_ip(vptr);
3022 velocity_save_context(vptr, &vptr->context);
3023 velocity_shutdown(vptr);
3024 velocity_set_wol(vptr);
3025 pci_enable_wake(pdev, PCI_D3hot, 1);
3026 pci_set_power_state(pdev, PCI_D3hot);
3027 } else {
3028 velocity_save_context(vptr, &vptr->context);
3029 velocity_shutdown(vptr);
3030 pci_disable_device(pdev);
3031 pci_set_power_state(pdev, pci_choose_state(pdev, state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 }
françois romieu5ae297b2011-08-04 02:39:03 +00003033
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 spin_unlock_irqrestore(&vptr->lock, flags);
Dave Jones2cf71d22009-07-23 18:11:12 -07003035 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036}
3037
3038/**
Dave Jones2cf71d22009-07-23 18:11:12 -07003039 * velocity_restore_context - restore registers
3040 * @vptr: velocity
3041 * @context: buffer for stored context
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 *
Dave Jones2cf71d22009-07-23 18:11:12 -07003043 * Reload the register configuration from the velocity context
3044 * created by velocity_save_context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 */
Dave Jones2cf71d22009-07-23 18:11:12 -07003046static void velocity_restore_context(struct velocity_info *vptr, struct velocity_context *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
Dave Jones2cf71d22009-07-23 18:11:12 -07003048 struct mac_regs __iomem *regs = vptr->mac_regs;
3049 int i;
3050 u8 __iomem *ptr = (u8 __iomem *)regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Dave Jones2cf71d22009-07-23 18:11:12 -07003052 for (i = MAC_REG_PAR; i < MAC_REG_CR0_SET; i += 4)
3053 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Dave Jones2cf71d22009-07-23 18:11:12 -07003055 /* Just skip cr0 */
3056 for (i = MAC_REG_CR1_SET; i < MAC_REG_CR0_CLR; i++) {
3057 /* Clear */
3058 writeb(~(*((u8 *) (context->mac_reg + i))), ptr + i + 4);
3059 /* Set */
3060 writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 }
3062
Dave Jones2cf71d22009-07-23 18:11:12 -07003063 for (i = MAC_REG_MAR; i < MAC_REG_IMR; i += 4)
3064 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3065
3066 for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4)
3067 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3068
3069 for (i = MAC_REG_TDCSR_SET; i <= MAC_REG_RDCSR_SET; i++)
3070 writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
3071}
3072
3073static int velocity_resume(struct pci_dev *pdev)
3074{
3075 struct net_device *dev = pci_get_drvdata(pdev);
3076 struct velocity_info *vptr = netdev_priv(dev);
3077 unsigned long flags;
3078 int i;
3079
Tony Priska9683c92013-05-18 09:39:05 +00003080 if (!netif_running(vptr->netdev))
Dave Jones2cf71d22009-07-23 18:11:12 -07003081 return 0;
3082
3083 pci_set_power_state(pdev, PCI_D0);
3084 pci_enable_wake(pdev, 0, 0);
3085 pci_restore_state(pdev);
3086
3087 mac_wol_reset(vptr->mac_regs);
3088
3089 spin_lock_irqsave(&vptr->lock, flags);
3090 velocity_restore_context(vptr, &vptr->context);
3091 velocity_init_registers(vptr, VELOCITY_INIT_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 mac_disable_int(vptr->mac_regs);
3093
Simon Kagstromd6cade02010-02-09 23:37:54 +00003094 velocity_tx_srv(vptr);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003095
Dave Jones2cf71d22009-07-23 18:11:12 -07003096 for (i = 0; i < vptr->tx.numq; i++) {
3097 if (vptr->tx.used[i])
3098 mac_tx_queue_wake(vptr->mac_regs, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 }
Dave Jones2cf71d22009-07-23 18:11:12 -07003100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 mac_enable_int(vptr->mac_regs);
Dave Jones2cf71d22009-07-23 18:11:12 -07003102 spin_unlock_irqrestore(&vptr->lock, flags);
Tony Priska9683c92013-05-18 09:39:05 +00003103 netif_device_attach(vptr->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Dave Jones2cf71d22009-07-23 18:11:12 -07003105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106}
Dave Jones2cf71d22009-07-23 18:11:12 -07003107#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
3109/*
3110 * Definition for our device driver. The PCI layer interface
3111 * uses this to handle all our card discover and plugging
3112 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113static struct pci_driver velocity_driver = {
françois romieu5ae297b2011-08-04 02:39:03 +00003114 .name = VELOCITY_NAME,
3115 .id_table = velocity_id_table,
3116 .probe = velocity_found1,
Bill Pemberton27add002012-12-03 09:23:49 -05003117 .remove = velocity_remove1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118#ifdef CONFIG_PM
françois romieu5ae297b2011-08-04 02:39:03 +00003119 .suspend = velocity_suspend,
3120 .resume = velocity_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121#endif
3122};
3123
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
3125/**
3126 * velocity_ethtool_up - pre hook for ethtool
3127 * @dev: network device
3128 *
3129 * Called before an ethtool operation. We need to make sure the
3130 * chip is out of D3 state before we poke at it.
3131 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132static int velocity_ethtool_up(struct net_device *dev)
3133{
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04003134 struct velocity_info *vptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 if (!netif_running(dev))
3136 pci_set_power_state(vptr->pdev, PCI_D0);
3137 return 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003138}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
3140/**
3141 * velocity_ethtool_down - post hook for ethtool
3142 * @dev: network device
3143 *
3144 * Called after an ethtool operation. Restore the chip back to D3
3145 * state if it isn't running.
3146 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147static void velocity_ethtool_down(struct net_device *dev)
3148{
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04003149 struct velocity_info *vptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 if (!netif_running(dev))
3151 pci_set_power_state(vptr->pdev, PCI_D3hot);
3152}
3153
David Decotigny70739492011-04-27 18:32:40 +00003154static int velocity_get_settings(struct net_device *dev,
3155 struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04003157 struct velocity_info *vptr = netdev_priv(dev);
Dave Jonesc4067402009-07-20 17:35:21 +00003158 struct mac_regs __iomem *regs = vptr->mac_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 u32 status;
3160 status = check_connection_type(vptr->mac_regs);
3161
Jay Cliburn59b693f2006-07-20 23:23:57 +02003162 cmd->supported = SUPPORTED_TP |
3163 SUPPORTED_Autoneg |
3164 SUPPORTED_10baseT_Half |
3165 SUPPORTED_10baseT_Full |
3166 SUPPORTED_100baseT_Half |
3167 SUPPORTED_100baseT_Full |
3168 SUPPORTED_1000baseT_Half |
3169 SUPPORTED_1000baseT_Full;
françois romieu15419222010-10-13 09:26:05 +00003170
3171 cmd->advertising = ADVERTISED_TP | ADVERTISED_Autoneg;
3172 if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
3173 cmd->advertising |=
3174 ADVERTISED_10baseT_Half |
3175 ADVERTISED_10baseT_Full |
3176 ADVERTISED_100baseT_Half |
3177 ADVERTISED_100baseT_Full |
3178 ADVERTISED_1000baseT_Half |
3179 ADVERTISED_1000baseT_Full;
3180 } else {
3181 switch (vptr->options.spd_dpx) {
3182 case SPD_DPX_1000_FULL:
3183 cmd->advertising |= ADVERTISED_1000baseT_Full;
3184 break;
3185 case SPD_DPX_100_HALF:
3186 cmd->advertising |= ADVERTISED_100baseT_Half;
3187 break;
3188 case SPD_DPX_100_FULL:
3189 cmd->advertising |= ADVERTISED_100baseT_Full;
3190 break;
3191 case SPD_DPX_10_HALF:
3192 cmd->advertising |= ADVERTISED_10baseT_Half;
3193 break;
3194 case SPD_DPX_10_FULL:
3195 cmd->advertising |= ADVERTISED_10baseT_Full;
3196 break;
3197 default:
3198 break;
3199 }
3200 }
David Decotigny70739492011-04-27 18:32:40 +00003201
Jay Cliburn59b693f2006-07-20 23:23:57 +02003202 if (status & VELOCITY_SPEED_1000)
David Decotigny70739492011-04-27 18:32:40 +00003203 ethtool_cmd_speed_set(cmd, SPEED_1000);
Jay Cliburn59b693f2006-07-20 23:23:57 +02003204 else if (status & VELOCITY_SPEED_100)
David Decotigny70739492011-04-27 18:32:40 +00003205 ethtool_cmd_speed_set(cmd, SPEED_100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 else
David Decotigny70739492011-04-27 18:32:40 +00003207 ethtool_cmd_speed_set(cmd, SPEED_10);
3208
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 cmd->autoneg = (status & VELOCITY_AUTONEG_ENABLE) ? AUTONEG_ENABLE : AUTONEG_DISABLE;
3210 cmd->port = PORT_TP;
3211 cmd->transceiver = XCVR_INTERNAL;
3212 cmd->phy_address = readb(&regs->MIIADR) & 0x1F;
3213
3214 if (status & VELOCITY_DUPLEX_FULL)
3215 cmd->duplex = DUPLEX_FULL;
3216 else
3217 cmd->duplex = DUPLEX_HALF;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003218
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 return 0;
3220}
3221
David Decotigny25db0332011-04-27 18:32:39 +00003222static int velocity_set_settings(struct net_device *dev,
3223 struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224{
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04003225 struct velocity_info *vptr = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +00003226 u32 speed = ethtool_cmd_speed(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 u32 curr_status;
3228 u32 new_status = 0;
3229 int ret = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003230
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 curr_status = check_connection_type(vptr->mac_regs);
3232 curr_status &= (~VELOCITY_LINK_FAIL);
3233
3234 new_status |= ((cmd->autoneg) ? VELOCITY_AUTONEG_ENABLE : 0);
David Decotigny25db0332011-04-27 18:32:39 +00003235 new_status |= ((speed == SPEED_1000) ? VELOCITY_SPEED_1000 : 0);
3236 new_status |= ((speed == SPEED_100) ? VELOCITY_SPEED_100 : 0);
3237 new_status |= ((speed == SPEED_10) ? VELOCITY_SPEED_10 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 new_status |= ((cmd->duplex == DUPLEX_FULL) ? VELOCITY_DUPLEX_FULL : 0);
3239
françois romieu15419222010-10-13 09:26:05 +00003240 if ((new_status & VELOCITY_AUTONEG_ENABLE) &&
3241 (new_status != (curr_status | VELOCITY_AUTONEG_ENABLE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 ret = -EINVAL;
françois romieu15419222010-10-13 09:26:05 +00003243 } else {
3244 enum speed_opt spd_dpx;
3245
3246 if (new_status & VELOCITY_AUTONEG_ENABLE)
3247 spd_dpx = SPD_DPX_AUTO;
3248 else if ((new_status & VELOCITY_SPEED_1000) &&
3249 (new_status & VELOCITY_DUPLEX_FULL)) {
3250 spd_dpx = SPD_DPX_1000_FULL;
3251 } else if (new_status & VELOCITY_SPEED_100)
3252 spd_dpx = (new_status & VELOCITY_DUPLEX_FULL) ?
3253 SPD_DPX_100_FULL : SPD_DPX_100_HALF;
3254 else if (new_status & VELOCITY_SPEED_10)
3255 spd_dpx = (new_status & VELOCITY_DUPLEX_FULL) ?
3256 SPD_DPX_10_FULL : SPD_DPX_10_HALF;
3257 else
3258 return -EOPNOTSUPP;
3259
3260 vptr->options.spd_dpx = spd_dpx;
3261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 velocity_set_media_mode(vptr, new_status);
françois romieu15419222010-10-13 09:26:05 +00003263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
3265 return ret;
3266}
3267
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268static void velocity_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
3269{
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04003270 struct velocity_info *vptr = netdev_priv(dev);
Rick Jones23020ab2011-11-09 09:58:07 +00003271 strlcpy(info->driver, VELOCITY_NAME, sizeof(info->driver));
3272 strlcpy(info->version, VELOCITY_VERSION, sizeof(info->version));
3273 strlcpy(info->bus_info, pci_name(vptr->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274}
3275
3276static void velocity_ethtool_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3277{
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04003278 struct velocity_info *vptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 wol->supported = WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP;
3280 wol->wolopts |= WAKE_MAGIC;
3281 /*
3282 if (vptr->wol_opts & VELOCITY_WOL_PHY)
3283 wol.wolopts|=WAKE_PHY;
3284 */
3285 if (vptr->wol_opts & VELOCITY_WOL_UCAST)
3286 wol->wolopts |= WAKE_UCAST;
3287 if (vptr->wol_opts & VELOCITY_WOL_ARP)
3288 wol->wolopts |= WAKE_ARP;
3289 memcpy(&wol->sopass, vptr->wol_passwd, 6);
3290}
3291
3292static int velocity_ethtool_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3293{
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04003294 struct velocity_info *vptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
3296 if (!(wol->wolopts & (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP)))
3297 return -EFAULT;
3298 vptr->wol_opts = VELOCITY_WOL_MAGIC;
3299
3300 /*
3301 if (wol.wolopts & WAKE_PHY) {
3302 vptr->wol_opts|=VELOCITY_WOL_PHY;
3303 vptr->flags |=VELOCITY_FLAGS_WOL_ENABLED;
3304 }
3305 */
3306
3307 if (wol->wolopts & WAKE_MAGIC) {
3308 vptr->wol_opts |= VELOCITY_WOL_MAGIC;
3309 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
3310 }
3311 if (wol->wolopts & WAKE_UCAST) {
3312 vptr->wol_opts |= VELOCITY_WOL_UCAST;
3313 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
3314 }
3315 if (wol->wolopts & WAKE_ARP) {
3316 vptr->wol_opts |= VELOCITY_WOL_ARP;
3317 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
3318 }
3319 memcpy(vptr->wol_passwd, wol->sopass, 6);
3320 return 0;
3321}
3322
3323static u32 velocity_get_msglevel(struct net_device *dev)
3324{
3325 return msglevel;
3326}
3327
3328static void velocity_set_msglevel(struct net_device *dev, u32 value)
3329{
3330 msglevel = value;
3331}
3332
Simon Kagstrom6dfc4b92009-11-25 22:10:12 +00003333static int get_pending_timer_val(int val)
3334{
3335 int mult_bits = val >> 6;
3336 int mult = 1;
3337
3338 switch (mult_bits)
3339 {
3340 case 1:
3341 mult = 4; break;
3342 case 2:
3343 mult = 16; break;
3344 case 3:
3345 mult = 64; break;
3346 case 0:
3347 default:
3348 break;
3349 }
3350
3351 return (val & 0x3f) * mult;
3352}
3353
3354static void set_pending_timer_val(int *val, u32 us)
3355{
3356 u8 mult = 0;
3357 u8 shift = 0;
3358
3359 if (us >= 0x3f) {
3360 mult = 1; /* mult with 4 */
3361 shift = 2;
3362 }
3363 if (us >= 0x3f * 4) {
3364 mult = 2; /* mult with 16 */
3365 shift = 4;
3366 }
3367 if (us >= 0x3f * 16) {
3368 mult = 3; /* mult with 64 */
3369 shift = 6;
3370 }
3371
3372 *val = (mult << 6) | ((us >> shift) & 0x3f);
3373}
3374
3375
3376static int velocity_get_coalesce(struct net_device *dev,
3377 struct ethtool_coalesce *ecmd)
3378{
3379 struct velocity_info *vptr = netdev_priv(dev);
3380
3381 ecmd->tx_max_coalesced_frames = vptr->options.tx_intsup;
3382 ecmd->rx_max_coalesced_frames = vptr->options.rx_intsup;
3383
3384 ecmd->rx_coalesce_usecs = get_pending_timer_val(vptr->options.rxqueue_timer);
3385 ecmd->tx_coalesce_usecs = get_pending_timer_val(vptr->options.txqueue_timer);
3386
3387 return 0;
3388}
3389
3390static int velocity_set_coalesce(struct net_device *dev,
3391 struct ethtool_coalesce *ecmd)
3392{
3393 struct velocity_info *vptr = netdev_priv(dev);
3394 int max_us = 0x3f * 64;
Simon Kagstrom39c2ff42010-02-09 23:38:07 +00003395 unsigned long flags;
Simon Kagstrom6dfc4b92009-11-25 22:10:12 +00003396
3397 /* 6 bits of */
3398 if (ecmd->tx_coalesce_usecs > max_us)
3399 return -EINVAL;
3400 if (ecmd->rx_coalesce_usecs > max_us)
3401 return -EINVAL;
3402
3403 if (ecmd->tx_max_coalesced_frames > 0xff)
3404 return -EINVAL;
3405 if (ecmd->rx_max_coalesced_frames > 0xff)
3406 return -EINVAL;
3407
3408 vptr->options.rx_intsup = ecmd->rx_max_coalesced_frames;
3409 vptr->options.tx_intsup = ecmd->tx_max_coalesced_frames;
3410
3411 set_pending_timer_val(&vptr->options.rxqueue_timer,
3412 ecmd->rx_coalesce_usecs);
3413 set_pending_timer_val(&vptr->options.txqueue_timer,
3414 ecmd->tx_coalesce_usecs);
3415
3416 /* Setup the interrupt suppression and queue timers */
Simon Kagstrom39c2ff42010-02-09 23:38:07 +00003417 spin_lock_irqsave(&vptr->lock, flags);
Simon Kagstrom6dfc4b92009-11-25 22:10:12 +00003418 mac_disable_int(vptr->mac_regs);
3419 setup_adaptive_interrupts(vptr);
3420 setup_queue_timers(vptr);
3421
3422 mac_write_int_mask(vptr->int_mask, vptr->mac_regs);
3423 mac_clear_isr(vptr->mac_regs);
3424 mac_enable_int(vptr->mac_regs);
Simon Kagstrom39c2ff42010-02-09 23:38:07 +00003425 spin_unlock_irqrestore(&vptr->lock, flags);
Simon Kagstrom6dfc4b92009-11-25 22:10:12 +00003426
3427 return 0;
3428}
3429
françois romieuad66fa72011-08-04 02:38:28 +00003430static const char velocity_gstrings[][ETH_GSTRING_LEN] = {
3431 "rx_all",
3432 "rx_ok",
3433 "tx_ok",
3434 "rx_error",
3435 "rx_runt_ok",
3436 "rx_runt_err",
3437 "rx_64",
3438 "tx_64",
3439 "rx_65_to_127",
3440 "tx_65_to_127",
3441 "rx_128_to_255",
3442 "tx_128_to_255",
3443 "rx_256_to_511",
3444 "tx_256_to_511",
3445 "rx_512_to_1023",
3446 "tx_512_to_1023",
3447 "rx_1024_to_1518",
3448 "tx_1024_to_1518",
3449 "tx_ether_collisions",
3450 "rx_crc_errors",
3451 "rx_jumbo",
3452 "tx_jumbo",
3453 "rx_mac_control_frames",
3454 "tx_mac_control_frames",
3455 "rx_frame_alignement_errors",
3456 "rx_long_ok",
3457 "rx_long_err",
3458 "tx_sqe_errors",
3459 "rx_no_buf",
3460 "rx_symbol_errors",
3461 "in_range_length_errors",
3462 "late_collisions"
3463};
3464
3465static void velocity_get_strings(struct net_device *dev, u32 sset, u8 *data)
3466{
3467 switch (sset) {
3468 case ETH_SS_STATS:
3469 memcpy(data, *velocity_gstrings, sizeof(velocity_gstrings));
3470 break;
3471 }
3472}
3473
3474static int velocity_get_sset_count(struct net_device *dev, int sset)
3475{
3476 switch (sset) {
3477 case ETH_SS_STATS:
3478 return ARRAY_SIZE(velocity_gstrings);
3479 default:
3480 return -EOPNOTSUPP;
3481 }
3482}
3483
3484static void velocity_get_ethtool_stats(struct net_device *dev,
3485 struct ethtool_stats *stats, u64 *data)
3486{
3487 if (netif_running(dev)) {
3488 struct velocity_info *vptr = netdev_priv(dev);
3489 u32 *p = vptr->mib_counter;
3490 int i;
3491
3492 spin_lock_irq(&vptr->lock);
3493 velocity_update_hw_mibs(vptr);
3494 spin_unlock_irq(&vptr->lock);
3495
3496 for (i = 0; i < ARRAY_SIZE(velocity_gstrings); i++)
3497 *data++ = *p++;
3498 }
3499}
3500
Jeff Garzik7282d492006-09-13 14:30:00 -04003501static const struct ethtool_ops velocity_ethtool_ops = {
françois romieu5ae297b2011-08-04 02:39:03 +00003502 .get_settings = velocity_get_settings,
3503 .set_settings = velocity_set_settings,
3504 .get_drvinfo = velocity_get_drvinfo,
3505 .get_wol = velocity_ethtool_get_wol,
3506 .set_wol = velocity_ethtool_set_wol,
3507 .get_msglevel = velocity_get_msglevel,
3508 .set_msglevel = velocity_set_msglevel,
3509 .get_link = velocity_get_link,
françois romieuad66fa72011-08-04 02:38:28 +00003510 .get_strings = velocity_get_strings,
3511 .get_sset_count = velocity_get_sset_count,
3512 .get_ethtool_stats = velocity_get_ethtool_stats,
françois romieu5ae297b2011-08-04 02:39:03 +00003513 .get_coalesce = velocity_get_coalesce,
3514 .set_coalesce = velocity_set_coalesce,
3515 .begin = velocity_ethtool_up,
3516 .complete = velocity_ethtool_down
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517};
3518
françois romieu5ae297b2011-08-04 02:39:03 +00003519#if defined(CONFIG_PM) && defined(CONFIG_INET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520static int velocity_netdev_event(struct notifier_block *nb, unsigned long notification, void *ptr)
3521{
françois romieu5ae297b2011-08-04 02:39:03 +00003522 struct in_ifaddr *ifa = ptr;
Denis V. Luneva337499f2008-02-28 20:44:27 -08003523 struct net_device *dev = ifa->ifa_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
Ben Hutchings516b4df2009-10-28 04:01:46 -07003525 if (dev_net(dev) == &init_net &&
3526 dev->netdev_ops == &velocity_netdev_ops)
3527 velocity_get_ip(netdev_priv(dev));
Denis V. Luneva337499f2008-02-28 20:44:27 -08003528
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 return NOTIFY_DONE;
3530}
Randy Dunlapce9f7fe2006-12-18 21:21:10 -08003531
Dave Jones2cf71d22009-07-23 18:11:12 -07003532static struct notifier_block velocity_inetaddr_notifier = {
françois romieu5ae297b2011-08-04 02:39:03 +00003533 .notifier_call = velocity_netdev_event,
Dave Jones2cf71d22009-07-23 18:11:12 -07003534};
3535
3536static void velocity_register_notifier(void)
3537{
3538 register_inetaddr_notifier(&velocity_inetaddr_notifier);
3539}
3540
3541static void velocity_unregister_notifier(void)
3542{
3543 unregister_inetaddr_notifier(&velocity_inetaddr_notifier);
3544}
3545
3546#else
3547
3548#define velocity_register_notifier() do {} while (0)
3549#define velocity_unregister_notifier() do {} while (0)
3550
3551#endif /* defined(CONFIG_PM) && defined(CONFIG_INET) */
3552
3553/**
3554 * velocity_init_module - load time function
3555 *
3556 * Called when the velocity module is loaded. The PCI driver
3557 * is registered with the PCI layer, and in turn will call
3558 * the probe functions for each velocity adapter installed
3559 * in the system.
3560 */
3561static int __init velocity_init_module(void)
3562{
3563 int ret;
3564
3565 velocity_register_notifier();
3566 ret = pci_register_driver(&velocity_driver);
3567 if (ret < 0)
3568 velocity_unregister_notifier();
3569 return ret;
3570}
3571
3572/**
3573 * velocity_cleanup - module unload
3574 *
3575 * When the velocity hardware is unloaded this function is called.
3576 * It will clean up the notifiers and the unregister the PCI
3577 * driver interface for this hardware. This in turn cleans up
3578 * all discovered interfaces before returning from the function
3579 */
3580static void __exit velocity_cleanup_module(void)
3581{
3582 velocity_unregister_notifier();
3583 pci_unregister_driver(&velocity_driver);
3584}
3585
3586module_init(velocity_init_module);
3587module_exit(velocity_cleanup_module);