blob: d6a92b794f352bfbaf2dba555e9f280b4cdea537 [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
12 * Scatter gather
13 * More testing
14 *
Alan Cox113aa832008-10-13 19:01:08 -070015 * The changes are (c) Copyright 2004, Red Hat Inc. <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * Additional fixes and clean up: Francois Romieu
17 *
18 * This source has not been verified for use in safety critical systems.
19 *
20 * Please direct queries about the revamped driver to the linux-kernel
21 * list not VIA.
22 *
23 * Original code:
24 *
25 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
26 * All rights reserved.
27 *
28 * This software may be redistributed and/or modified under
29 * the terms of the GNU General Public License as published by the Free
30 * Software Foundation; either version 2 of the License, or
31 * any later version.
32 *
33 * This program is distributed in the hope that it will be useful, but
34 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
35 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
36 * for more details.
37 *
38 * Author: Chuang Liang-Shing, AJ Jiang
39 *
40 * Date: Jan 24, 2003
41 *
42 * MODULE_LICENSE("GPL");
43 *
44 */
45
46
47#include <linux/module.h>
48#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/init.h>
50#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 */
95
Dave Jonesc4067402009-07-20 17:35:21 +000096static void mac_get_cam_mask(struct mac_regs __iomem *regs, u8 *mask)
Stephen Hemminger01faccb2007-08-24 14:40:45 -070097{
98 int i;
99
100 /* Select CAM mask */
101 BYTE_REG_BITS_SET(CAMCR_PS_CAM_MASK, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
102
103 writeb(0, &regs->CAMADDR);
104
105 /* read mask */
106 for (i = 0; i < 8; i++)
107 *mask++ = readb(&(regs->MARCAM[i]));
108
109 /* disable CAMEN */
110 writeb(0, &regs->CAMADDR);
111
112 /* Select mar */
113 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700114}
115
116
117/**
118 * mac_set_cam_mask - Set a CAM mask
119 * @regs: register block for this velocity
120 * @mask: CAM mask to load
121 *
122 * Store a new mask into a CAM
123 */
124
Dave Jonesc4067402009-07-20 17:35:21 +0000125static void mac_set_cam_mask(struct mac_regs __iomem *regs, u8 *mask)
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700126{
127 int i;
128 /* Select CAM mask */
129 BYTE_REG_BITS_SET(CAMCR_PS_CAM_MASK, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
130
131 writeb(CAMADDR_CAMEN, &regs->CAMADDR);
132
Dave Jonesc4067402009-07-20 17:35:21 +0000133 for (i = 0; i < 8; i++)
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700134 writeb(*mask++, &(regs->MARCAM[i]));
Dave Jonesc4067402009-07-20 17:35:21 +0000135
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700136 /* disable CAMEN */
137 writeb(0, &regs->CAMADDR);
138
139 /* Select mar */
140 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
141}
142
Dave Jonesc4067402009-07-20 17:35:21 +0000143static void mac_set_vlan_cam_mask(struct mac_regs __iomem *regs, u8 *mask)
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700144{
145 int i;
146 /* Select CAM mask */
147 BYTE_REG_BITS_SET(CAMCR_PS_CAM_MASK, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
148
149 writeb(CAMADDR_CAMEN | CAMADDR_VCAMSL, &regs->CAMADDR);
150
Dave Jonesc4067402009-07-20 17:35:21 +0000151 for (i = 0; i < 8; i++)
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700152 writeb(*mask++, &(regs->MARCAM[i]));
Dave Jonesc4067402009-07-20 17:35:21 +0000153
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700154 /* disable CAMEN */
155 writeb(0, &regs->CAMADDR);
156
157 /* Select mar */
158 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
159}
160
161/**
162 * mac_set_cam - set CAM data
163 * @regs: register block of this velocity
164 * @idx: Cam index
165 * @addr: 2 or 6 bytes of CAM data
166 *
167 * Load an address or vlan tag into a CAM
168 */
169
Dave Jonesc4067402009-07-20 17:35:21 +0000170static void mac_set_cam(struct mac_regs __iomem *regs, int idx, const u8 *addr)
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700171{
172 int i;
173
174 /* Select CAM mask */
175 BYTE_REG_BITS_SET(CAMCR_PS_CAM_DATA, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
176
177 idx &= (64 - 1);
178
179 writeb(CAMADDR_CAMEN | idx, &regs->CAMADDR);
180
Dave Jonesc4067402009-07-20 17:35:21 +0000181 for (i = 0; i < 6; i++)
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700182 writeb(*addr++, &(regs->MARCAM[i]));
Dave Jonesc4067402009-07-20 17:35:21 +0000183
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700184 BYTE_REG_BITS_ON(CAMCR_CAMWR, &regs->CAMCR);
185
186 udelay(10);
187
188 writeb(0, &regs->CAMADDR);
189
190 /* Select mar */
191 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
192}
193
Dave Jonesc4067402009-07-20 17:35:21 +0000194static void mac_set_vlan_cam(struct mac_regs __iomem *regs, int idx,
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700195 const u8 *addr)
196{
197
198 /* Select CAM mask */
199 BYTE_REG_BITS_SET(CAMCR_PS_CAM_DATA, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
200
201 idx &= (64 - 1);
202
203 writeb(CAMADDR_CAMEN | CAMADDR_VCAMSL | idx, &regs->CAMADDR);
204 writew(*((u16 *) addr), &regs->MARCAM[0]);
205
206 BYTE_REG_BITS_ON(CAMCR_CAMWR, &regs->CAMCR);
207
208 udelay(10);
209
210 writeb(0, &regs->CAMADDR);
211
212 /* Select mar */
213 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
214}
215
216
217/**
218 * mac_wol_reset - reset WOL after exiting low power
219 * @regs: register block of this velocity
220 *
221 * Called after we drop out of wake on lan mode in order to
222 * reset the Wake on lan features. This function doesn't restore
223 * the rest of the logic from the result of sleep/wakeup
224 */
225
Dave Jonesc4067402009-07-20 17:35:21 +0000226static void mac_wol_reset(struct mac_regs __iomem *regs)
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700227{
228
229 /* Turn off SWPTAG right after leaving power mode */
230 BYTE_REG_BITS_OFF(STICKHW_SWPTAG, &regs->STICKHW);
231 /* clear sticky bits */
232 BYTE_REG_BITS_OFF((STICKHW_DS1 | STICKHW_DS0), &regs->STICKHW);
233
234 BYTE_REG_BITS_OFF(CHIPGCR_FCGMII, &regs->CHIPGCR);
235 BYTE_REG_BITS_OFF(CHIPGCR_FCMODE, &regs->CHIPGCR);
236 /* disable force PME-enable */
237 writeb(WOLCFG_PMEOVR, &regs->WOLCFGClr);
238 /* disable power-event config bit */
239 writew(0xFFFF, &regs->WOLCRClr);
240 /* clear power status */
241 writew(0xFFFF, &regs->WOLSRClr);
242}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244static int velocity_mii_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
Jeff Garzik7282d492006-09-13 14:30:00 -0400245static const struct ethtool_ops velocity_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247/*
248 Define module options
249*/
250
251MODULE_AUTHOR("VIA Networking Technologies, Inc.");
252MODULE_LICENSE("GPL");
253MODULE_DESCRIPTION("VIA Networking Velocity Family Gigabit Ethernet Adapter Driver");
254
Dave Jonesc4067402009-07-20 17:35:21 +0000255#define VELOCITY_PARAM(N, D) \
256 static int N[MAX_UNITS] = OPTION_DEFAULT;\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 module_param_array(N, int, NULL, 0); \
Dave Jonesc4067402009-07-20 17:35:21 +0000258 MODULE_PARM_DESC(N, D);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260#define RX_DESC_MIN 64
261#define RX_DESC_MAX 255
262#define RX_DESC_DEF 64
263VELOCITY_PARAM(RxDescriptors, "Number of receive descriptors");
264
265#define TX_DESC_MIN 16
266#define TX_DESC_MAX 256
267#define TX_DESC_DEF 64
268VELOCITY_PARAM(TxDescriptors, "Number of transmit descriptors");
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270#define RX_THRESH_MIN 0
271#define RX_THRESH_MAX 3
272#define RX_THRESH_DEF 0
273/* rx_thresh[] is used for controlling the receive fifo threshold.
274 0: indicate the rxfifo threshold is 128 bytes.
275 1: indicate the rxfifo threshold is 512 bytes.
276 2: indicate the rxfifo threshold is 1024 bytes.
277 3: indicate the rxfifo threshold is store & forward.
278*/
279VELOCITY_PARAM(rx_thresh, "Receive fifo threshold");
280
281#define DMA_LENGTH_MIN 0
282#define DMA_LENGTH_MAX 7
283#define DMA_LENGTH_DEF 0
284
285/* DMA_length[] is used for controlling the DMA length
286 0: 8 DWORDs
287 1: 16 DWORDs
288 2: 32 DWORDs
289 3: 64 DWORDs
290 4: 128 DWORDs
291 5: 256 DWORDs
292 6: SF(flush till emply)
293 7: SF(flush till emply)
294*/
295VELOCITY_PARAM(DMA_length, "DMA length");
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297#define IP_ALIG_DEF 0
298/* IP_byte_align[] is used for IP header DWORD byte aligned
299 0: indicate the IP header won't be DWORD byte aligned.(Default) .
300 1: indicate the IP header will be DWORD byte aligned.
301 In some enviroment, the IP header should be DWORD byte aligned,
302 or the packet will be droped when we receive it. (eg: IPVS)
303*/
304VELOCITY_PARAM(IP_byte_align, "Enable IP header dword aligned");
305
306#define TX_CSUM_DEF 1
307/* txcsum_offload[] is used for setting the checksum offload ability of NIC.
308 (We only support RX checksum offload now)
309 0: disable csum_offload[checksum offload
310 1: enable checksum offload. (Default)
311*/
312VELOCITY_PARAM(txcsum_offload, "Enable transmit packet checksum offload");
313
314#define FLOW_CNTL_DEF 1
315#define FLOW_CNTL_MIN 1
316#define FLOW_CNTL_MAX 5
317
318/* flow_control[] is used for setting the flow control ability of NIC.
319 1: hardware deafult - AUTO (default). Use Hardware default value in ANAR.
320 2: enable TX flow control.
321 3: enable RX flow control.
322 4: enable RX/TX flow control.
323 5: disable
324*/
325VELOCITY_PARAM(flow_control, "Enable flow control ability");
326
327#define MED_LNK_DEF 0
328#define MED_LNK_MIN 0
329#define MED_LNK_MAX 4
330/* speed_duplex[] is used for setting the speed and duplex mode of NIC.
331 0: indicate autonegotiation for both speed and duplex mode
332 1: indicate 100Mbps half duplex mode
333 2: indicate 100Mbps full duplex mode
334 3: indicate 10Mbps half duplex mode
335 4: indicate 10Mbps full duplex mode
336
337 Note:
Dave Jonesc4067402009-07-20 17:35:21 +0000338 if EEPROM have been set to the force mode, this option is ignored
339 by driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340*/
341VELOCITY_PARAM(speed_duplex, "Setting the speed and duplex mode");
342
343#define VAL_PKT_LEN_DEF 0
344/* ValPktLen[] is used for setting the checksum offload ability of NIC.
345 0: Receive frame with invalid layer 2 length (Default)
346 1: Drop frame with invalid layer 2 length
347*/
348VELOCITY_PARAM(ValPktLen, "Receiving or Drop invalid 802.3 frame");
349
350#define WOL_OPT_DEF 0
351#define WOL_OPT_MIN 0
352#define WOL_OPT_MAX 7
353/* wol_opts[] is used for controlling wake on lan behavior.
354 0: Wake up if recevied a magic packet. (Default)
355 1: Wake up if link status is on/off.
356 2: Wake up if recevied an arp packet.
357 4: Wake up if recevied any unicast packet.
358 Those value can be sumed up to support more than one option.
359*/
360VELOCITY_PARAM(wol_opts, "Wake On Lan options");
361
362#define INT_WORKS_DEF 20
363#define INT_WORKS_MIN 10
364#define INT_WORKS_MAX 64
365
366VELOCITY_PARAM(int_works, "Number of packets per interrupt services");
367
368static int rx_copybreak = 200;
369module_param(rx_copybreak, int, 0644);
370MODULE_PARM_DESC(rx_copybreak, "Copy breakpoint for copy-only-tiny-frames");
371
Jeff Garzikcabb7662006-06-27 09:25:28 -0400372static void velocity_init_info(struct pci_dev *pdev, struct velocity_info *vptr,
373 const struct velocity_info_tbl *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374static int velocity_get_pci_info(struct velocity_info *, struct pci_dev *pdev);
375static void velocity_print_info(struct velocity_info *vptr);
376static int velocity_open(struct net_device *dev);
377static int velocity_change_mtu(struct net_device *dev, int mtu);
378static int velocity_xmit(struct sk_buff *skb, struct net_device *dev);
Séguier Régis3ca17df2009-04-09 01:33:57 +0000379static irqreturn_t velocity_intr(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380static void velocity_set_multi(struct net_device *dev);
381static struct net_device_stats *velocity_get_stats(struct net_device *dev);
382static int velocity_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
383static int velocity_close(struct net_device *dev);
384static int velocity_receive_frame(struct velocity_info *, int idx);
385static int velocity_alloc_rx_buf(struct velocity_info *, int idx);
386static void velocity_free_rd_ring(struct velocity_info *vptr);
387static void velocity_free_tx_buf(struct velocity_info *vptr, struct velocity_td_info *);
388static int velocity_soft_reset(struct velocity_info *vptr);
389static void mii_init(struct velocity_info *vptr, u32 mii_status);
Francois Romieu8a22ddd2006-06-23 00:47:06 +0200390static u32 velocity_get_link(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391static u32 velocity_get_opt_media_mode(struct velocity_info *vptr);
392static void velocity_print_link_status(struct velocity_info *vptr);
Dave Jonesc4067402009-07-20 17:35:21 +0000393static void safe_disable_mii_autopoll(struct mac_regs __iomem *regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394static void velocity_shutdown(struct velocity_info *vptr);
395static void enable_flow_control_ability(struct velocity_info *vptr);
Dave Jonesc4067402009-07-20 17:35:21 +0000396static void enable_mii_autopoll(struct mac_regs __iomem *regs);
397static int velocity_mii_read(struct mac_regs __iomem *, u8 byIdx, u16 *pdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398static int velocity_mii_write(struct mac_regs __iomem *, u8 byMiiAddr, u16 data);
Dave Jonesc4067402009-07-20 17:35:21 +0000399static u32 mii_check_media_mode(struct mac_regs __iomem *regs);
400static u32 check_connection_type(struct mac_regs __iomem *regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401static int velocity_set_media_mode(struct velocity_info *vptr, u32 mii_status);
402
403#ifdef CONFIG_PM
404
405static int velocity_suspend(struct pci_dev *pdev, pm_message_t state);
406static int velocity_resume(struct pci_dev *pdev);
407
Randy Dunlapce9f7fe2006-12-18 21:21:10 -0800408static DEFINE_SPINLOCK(velocity_dev_list_lock);
409static LIST_HEAD(velocity_dev_list);
410
411#endif
412
413#if defined(CONFIG_PM) && defined(CONFIG_INET)
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415static int velocity_netdev_event(struct notifier_block *nb, unsigned long notification, void *ptr);
416
417static struct notifier_block velocity_inetaddr_notifier = {
418 .notifier_call = velocity_netdev_event,
419};
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421static void velocity_register_notifier(void)
422{
423 register_inetaddr_notifier(&velocity_inetaddr_notifier);
424}
425
426static void velocity_unregister_notifier(void)
427{
428 unregister_inetaddr_notifier(&velocity_inetaddr_notifier);
429}
430
Randy Dunlapce9f7fe2006-12-18 21:21:10 -0800431#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433#define velocity_register_notifier() do {} while (0)
434#define velocity_unregister_notifier() do {} while (0)
435
Randy Dunlapce9f7fe2006-12-18 21:21:10 -0800436#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438/*
439 * Internal board variants. At the moment we have only one
440 */
441
Andrew Morton4f14b922008-02-09 23:41:40 -0800442static struct velocity_info_tbl chip_info_table[] = {
Jeff Garzikcabb7662006-06-27 09:25:28 -0400443 {CHIP_TYPE_VT6110, "VIA Networking Velocity Family Gigabit Ethernet Adapter", 1, 0x00FFFFFFUL},
444 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445};
446
447/*
448 * Describe the PCI device identifiers that we support in this
449 * device driver. Used for hotplug autoloading.
450 */
451
Jeff Garzike54f4892006-06-27 09:20:08 -0400452static const struct pci_device_id velocity_id_table[] __devinitdata = {
453 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_612X) },
454 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455};
456
457MODULE_DEVICE_TABLE(pci, velocity_id_table);
458
459/**
460 * get_chip_name - identifier to name
461 * @id: chip identifier
462 *
463 * Given a chip identifier return a suitable description. Returns
464 * a pointer a static string valid while the driver is loaded.
465 */
466
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700467static const char __devinit *get_chip_name(enum chip_type chip_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 int i;
470 for (i = 0; chip_info_table[i].name != NULL; i++)
471 if (chip_info_table[i].chip_id == chip_id)
472 break;
473 return chip_info_table[i].name;
474}
475
476/**
477 * velocity_remove1 - device unplug
478 * @pdev: PCI device being removed
479 *
480 * Device unload callback. Called on an unplug or on module
481 * unload for each active device that is present. Disconnects
482 * the device from the network layer and frees all the resources
483 */
484
485static void __devexit velocity_remove1(struct pci_dev *pdev)
486{
487 struct net_device *dev = pci_get_drvdata(pdev);
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -0400488 struct velocity_info *vptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490#ifdef CONFIG_PM
491 unsigned long flags;
492
493 spin_lock_irqsave(&velocity_dev_list_lock, flags);
494 if (!list_empty(&velocity_dev_list))
495 list_del(&vptr->list);
496 spin_unlock_irqrestore(&velocity_dev_list_lock, flags);
497#endif
498 unregister_netdev(dev);
499 iounmap(vptr->mac_regs);
500 pci_release_regions(pdev);
501 pci_disable_device(pdev);
502 pci_set_drvdata(pdev, NULL);
503 free_netdev(dev);
504
505 velocity_nics--;
506}
507
508/**
509 * velocity_set_int_opt - parser for integer options
510 * @opt: pointer to option value
511 * @val: value the user requested (or -1 for default)
512 * @min: lowest value allowed
513 * @max: highest value allowed
514 * @def: default value
515 * @name: property name
516 * @dev: device name
517 *
518 * Set an integer property in the module options. This function does
519 * all the verification and checking as well as reporting so that
520 * we don't duplicate code for each option.
521 */
522
Sven Hartge07b5f6a2008-10-23 13:03:44 +0000523static void __devinit velocity_set_int_opt(int *opt, int val, int min, int max, int def, char *name, const char *devname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 if (val == -1)
526 *opt = def;
527 else if (val < min || val > max) {
528 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: the value of parameter %s is invalid, the valid range is (%d-%d)\n",
529 devname, name, min, max);
530 *opt = def;
531 } else {
532 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_INFO "%s: set value of parameter %s to %d\n",
533 devname, name, val);
534 *opt = val;
535 }
536}
537
538/**
539 * velocity_set_bool_opt - parser for boolean options
540 * @opt: pointer to option value
541 * @val: value the user requested (or -1 for default)
542 * @def: default value (yes/no)
543 * @flag: numeric value to set for true.
544 * @name: property name
545 * @dev: device name
546 *
547 * Set a boolean property in the module options. This function does
548 * all the verification and checking as well as reporting so that
549 * we don't duplicate code for each option.
550 */
551
Dave Jonesc4067402009-07-20 17:35:21 +0000552static void __devinit velocity_set_bool_opt(u32 *opt, int val, int def, u32 flag, char *name, const char *devname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 (*opt) &= (~flag);
555 if (val == -1)
556 *opt |= (def ? flag : 0);
557 else if (val < 0 || val > 1) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400558 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 -0700559 devname, name);
560 *opt |= (def ? flag : 0);
561 } else {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400562 printk(KERN_INFO "%s: set parameter %s to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 devname, name, val ? "TRUE" : "FALSE");
564 *opt |= (val ? flag : 0);
565 }
566}
567
568/**
569 * velocity_get_options - set options on device
570 * @opts: option structure for the device
571 * @index: index of option to use in module options array
572 * @devname: device name
573 *
574 * Turn the module and command options into a single structure
575 * for the current device
576 */
577
Sven Hartge07b5f6a2008-10-23 13:03:44 +0000578static void __devinit velocity_get_options(struct velocity_opt *opts, int index, const char *devname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580
581 velocity_set_int_opt(&opts->rx_thresh, rx_thresh[index], RX_THRESH_MIN, RX_THRESH_MAX, RX_THRESH_DEF, "rx_thresh", devname);
582 velocity_set_int_opt(&opts->DMA_length, DMA_length[index], DMA_LENGTH_MIN, DMA_LENGTH_MAX, DMA_LENGTH_DEF, "DMA_length", devname);
583 velocity_set_int_opt(&opts->numrx, RxDescriptors[index], RX_DESC_MIN, RX_DESC_MAX, RX_DESC_DEF, "RxDescriptors", devname);
584 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 -0700585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 velocity_set_bool_opt(&opts->flags, txcsum_offload[index], TX_CSUM_DEF, VELOCITY_FLAGS_TX_CSUM, "txcsum_offload", devname);
587 velocity_set_int_opt(&opts->flow_cntl, flow_control[index], FLOW_CNTL_MIN, FLOW_CNTL_MAX, FLOW_CNTL_DEF, "flow_control", devname);
588 velocity_set_bool_opt(&opts->flags, IP_byte_align[index], IP_ALIG_DEF, VELOCITY_FLAGS_IP_ALIGN, "IP_byte_align", devname);
589 velocity_set_bool_opt(&opts->flags, ValPktLen[index], VAL_PKT_LEN_DEF, VELOCITY_FLAGS_VAL_PKT_LEN, "ValPktLen", devname);
590 velocity_set_int_opt((int *) &opts->spd_dpx, speed_duplex[index], MED_LNK_MIN, MED_LNK_MAX, MED_LNK_DEF, "Media link mode", devname);
591 velocity_set_int_opt((int *) &opts->wol_opts, wol_opts[index], WOL_OPT_MIN, WOL_OPT_MAX, WOL_OPT_DEF, "Wake On Lan options", devname);
592 velocity_set_int_opt((int *) &opts->int_works, int_works[index], INT_WORKS_MIN, INT_WORKS_MAX, INT_WORKS_DEF, "Interrupt service works", devname);
593 opts->numrx = (opts->numrx & ~3);
594}
595
596/**
597 * velocity_init_cam_filter - initialise CAM
598 * @vptr: velocity to program
599 *
600 * Initialize the content addressable memory used for filters. Load
601 * appropriately according to the presence of VLAN
602 */
603
604static void velocity_init_cam_filter(struct velocity_info *vptr)
605{
Dave Jonesc4067402009-07-20 17:35:21 +0000606 struct mac_regs __iomem *regs = vptr->mac_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 /* Turn on MCFG_PQEN, turn off MCFG_RTGOPT */
609 WORD_REG_BITS_SET(MCFG_PQEN, MCFG_RTGOPT, &regs->MCFG);
610 WORD_REG_BITS_ON(MCFG_VIDFR, &regs->MCFG);
611
612 /* Disable all CAMs */
613 memset(vptr->vCAMmask, 0, sizeof(u8) * 8);
614 memset(vptr->mCAMmask, 0, sizeof(u8) * 8);
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700615 mac_set_vlan_cam_mask(regs, vptr->vCAMmask);
616 mac_set_cam_mask(regs, vptr->mCAMmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Francois Romieud4f73c82008-04-24 23:32:33 +0200618 /* Enable VCAMs */
Stephen Hemminger501e4d22007-08-24 13:56:49 -0700619 if (vptr->vlgrp) {
Francois Romieud4f73c82008-04-24 23:32:33 +0200620 unsigned int vid, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Francois Romieud4f73c82008-04-24 23:32:33 +0200622 if (!vlan_group_get_device(vptr->vlgrp, 0))
623 WORD_REG_BITS_ON(MCFG_RTGOPT, &regs->MCFG);
624
625 for (vid = 1; (vid < VLAN_VID_MASK); vid++) {
626 if (vlan_group_get_device(vptr->vlgrp, vid)) {
627 mac_set_vlan_cam(regs, i, (u8 *) &vid);
628 vptr->vCAMmask[i / 8] |= 0x1 << (i % 8);
629 if (++i >= VCAM_SIZE)
630 break;
Stephen Hemminger501e4d22007-08-24 13:56:49 -0700631 }
632 }
Stephen Hemminger01faccb2007-08-24 14:40:45 -0700633 mac_set_vlan_cam_mask(regs, vptr->vCAMmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635}
636
Francois Romieud4f73c82008-04-24 23:32:33 +0200637static void velocity_vlan_rx_register(struct net_device *dev,
638 struct vlan_group *grp)
639{
640 struct velocity_info *vptr = netdev_priv(dev);
641
642 vptr->vlgrp = grp;
643}
644
Stephen Hemminger501e4d22007-08-24 13:56:49 -0700645static void velocity_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
646{
647 struct velocity_info *vptr = netdev_priv(dev);
648
Dave Jonesc4067402009-07-20 17:35:21 +0000649 spin_lock_irq(&vptr->lock);
Stephen Hemminger501e4d22007-08-24 13:56:49 -0700650 velocity_init_cam_filter(vptr);
Dave Jonesc4067402009-07-20 17:35:21 +0000651 spin_unlock_irq(&vptr->lock);
Stephen Hemminger501e4d22007-08-24 13:56:49 -0700652}
653
654static void velocity_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
655{
656 struct velocity_info *vptr = netdev_priv(dev);
657
Dave Jonesc4067402009-07-20 17:35:21 +0000658 spin_lock_irq(&vptr->lock);
Stephen Hemminger501e4d22007-08-24 13:56:49 -0700659 vlan_group_set_device(vptr->vlgrp, vid, NULL);
660 velocity_init_cam_filter(vptr);
Dave Jonesc4067402009-07-20 17:35:21 +0000661 spin_unlock_irq(&vptr->lock);
Stephen Hemminger501e4d22007-08-24 13:56:49 -0700662}
663
Francois Romieu3c4dc712008-07-31 22:51:18 +0200664static void velocity_init_rx_ring_indexes(struct velocity_info *vptr)
665{
666 vptr->rx.dirty = vptr->rx.filled = vptr->rx.curr = 0;
667}
Stephen Hemminger501e4d22007-08-24 13:56:49 -0700668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669/**
670 * velocity_rx_reset - handle a receive reset
671 * @vptr: velocity we are resetting
672 *
673 * Reset the ownership and status for the receive ring side.
674 * Hand all the receive queue to the NIC.
675 */
676
677static void velocity_rx_reset(struct velocity_info *vptr)
678{
679
Dave Jonesc4067402009-07-20 17:35:21 +0000680 struct mac_regs __iomem *regs = vptr->mac_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 int i;
682
Francois Romieu3c4dc712008-07-31 22:51:18 +0200683 velocity_init_rx_ring_indexes(vptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 /*
686 * Init state, all RD entries belong to the NIC
687 */
688 for (i = 0; i < vptr->options.numrx; ++i)
Francois Romieu0fe9f152008-07-31 22:10:10 +0200689 vptr->rx.ring[i].rdesc0.len |= OWNED_BY_NIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 writew(vptr->options.numrx, &regs->RBRDU);
Francois Romieu0fe9f152008-07-31 22:10:10 +0200692 writel(vptr->rx.pool_dma, &regs->RDBaseLo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 writew(0, &regs->RDIdx);
694 writew(vptr->options.numrx - 1, &regs->RDCSize);
695}
696
697/**
698 * velocity_init_registers - initialise MAC registers
699 * @vptr: velocity to init
700 * @type: type of initialisation (hot or cold)
701 *
702 * Initialise the MAC on a reset or on first set up on the
703 * hardware.
704 */
705
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400706static void velocity_init_registers(struct velocity_info *vptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 enum velocity_init_type type)
708{
Dave Jonesc4067402009-07-20 17:35:21 +0000709 struct mac_regs __iomem *regs = vptr->mac_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 int i, mii_status;
711
712 mac_wol_reset(regs);
713
714 switch (type) {
715 case VELOCITY_INIT_RESET:
716 case VELOCITY_INIT_WOL:
717
718 netif_stop_queue(vptr->dev);
719
720 /*
721 * Reset RX to prevent RX pointer not on the 4X location
722 */
723 velocity_rx_reset(vptr);
724 mac_rx_queue_run(regs);
725 mac_rx_queue_wake(regs);
726
727 mii_status = velocity_get_opt_media_mode(vptr);
728 if (velocity_set_media_mode(vptr, mii_status) != VELOCITY_LINK_CHANGE) {
729 velocity_print_link_status(vptr);
730 if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
731 netif_wake_queue(vptr->dev);
732 }
733
734 enable_flow_control_ability(vptr);
735
736 mac_clear_isr(regs);
737 writel(CR0_STOP, &regs->CR0Clr);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400738 writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 &regs->CR0Set);
740
741 break;
742
743 case VELOCITY_INIT_COLD:
744 default:
745 /*
746 * Do reset
747 */
748 velocity_soft_reset(vptr);
749 mdelay(5);
750
751 mac_eeprom_reload(regs);
Dave Jonesc4067402009-07-20 17:35:21 +0000752 for (i = 0; i < 6; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 writeb(vptr->dev->dev_addr[i], &(regs->PAR[i]));
Dave Jonesc4067402009-07-20 17:35:21 +0000754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 /*
756 * clear Pre_ACPI bit.
757 */
758 BYTE_REG_BITS_OFF(CFGA_PACPI, &(regs->CFGA));
759 mac_set_rx_thresh(regs, vptr->options.rx_thresh);
760 mac_set_dma_length(regs, vptr->options.DMA_length);
761
762 writeb(WOLCFG_SAM | WOLCFG_SAB, &regs->WOLCFGSet);
763 /*
764 * Back off algorithm use original IEEE standard
765 */
766 BYTE_REG_BITS_SET(CFGB_OFSET, (CFGB_CRANDOM | CFGB_CAP | CFGB_MBA | CFGB_BAKOPT), &regs->CFGB);
767
768 /*
769 * Init CAM filter
770 */
771 velocity_init_cam_filter(vptr);
772
773 /*
774 * Set packet filter: Receive directed and broadcast address
775 */
776 velocity_set_multi(vptr->dev);
777
778 /*
779 * Enable MII auto-polling
780 */
781 enable_mii_autopoll(regs);
782
783 vptr->int_mask = INT_MASK_DEF;
784
Francois Romieu0fe9f152008-07-31 22:10:10 +0200785 writel(vptr->rx.pool_dma, &regs->RDBaseLo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 writew(vptr->options.numrx - 1, &regs->RDCSize);
787 mac_rx_queue_run(regs);
788 mac_rx_queue_wake(regs);
789
790 writew(vptr->options.numtx - 1, &regs->TDCSize);
791
Francois Romieu0fe9f152008-07-31 22:10:10 +0200792 for (i = 0; i < vptr->tx.numq; i++) {
793 writel(vptr->tx.pool_dma[i], &regs->TDBaseLo[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 mac_tx_queue_run(regs, i);
795 }
796
797 init_flow_control_register(vptr);
798
799 writel(CR0_STOP, &regs->CR0Clr);
800 writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT), &regs->CR0Set);
801
802 mii_status = velocity_get_opt_media_mode(vptr);
803 netif_stop_queue(vptr->dev);
804
805 mii_init(vptr, mii_status);
806
807 if (velocity_set_media_mode(vptr, mii_status) != VELOCITY_LINK_CHANGE) {
808 velocity_print_link_status(vptr);
809 if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
810 netif_wake_queue(vptr->dev);
811 }
812
813 enable_flow_control_ability(vptr);
814 mac_hw_mibs_init(regs);
815 mac_write_int_mask(vptr->int_mask, regs);
816 mac_clear_isr(regs);
817
818 }
819}
820
821/**
822 * velocity_soft_reset - soft reset
823 * @vptr: velocity to reset
824 *
825 * Kick off a soft reset of the velocity adapter and then poll
826 * until the reset sequence has completed before returning.
827 */
828
829static int velocity_soft_reset(struct velocity_info *vptr)
830{
Dave Jonesc4067402009-07-20 17:35:21 +0000831 struct mac_regs __iomem *regs = vptr->mac_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 int i = 0;
833
834 writel(CR0_SFRST, &regs->CR0Set);
835
836 for (i = 0; i < W_MAX_TIMEOUT; i++) {
837 udelay(5);
838 if (!DWORD_REG_BITS_IS_ON(CR0_SFRST, &regs->CR0Set))
839 break;
840 }
841
842 if (i == W_MAX_TIMEOUT) {
843 writel(CR0_FORSRST, &regs->CR0Set);
844 /* FIXME: PCI POSTING */
845 /* delay 2ms */
846 mdelay(2);
847 }
848 return 0;
849}
850
Stephen Hemminger39a11bd2008-11-19 22:19:33 -0800851static const struct net_device_ops velocity_netdev_ops = {
852 .ndo_open = velocity_open,
853 .ndo_stop = velocity_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800854 .ndo_start_xmit = velocity_xmit,
Stephen Hemminger39a11bd2008-11-19 22:19:33 -0800855 .ndo_get_stats = velocity_get_stats,
856 .ndo_validate_addr = eth_validate_addr,
Stephen Hemmingerfe96aaa2009-01-09 11:13:14 +0000857 .ndo_set_mac_address = eth_mac_addr,
Stephen Hemminger39a11bd2008-11-19 22:19:33 -0800858 .ndo_set_multicast_list = velocity_set_multi,
859 .ndo_change_mtu = velocity_change_mtu,
860 .ndo_do_ioctl = velocity_ioctl,
861 .ndo_vlan_rx_add_vid = velocity_vlan_rx_add_vid,
862 .ndo_vlan_rx_kill_vid = velocity_vlan_rx_kill_vid,
863 .ndo_vlan_rx_register = velocity_vlan_rx_register,
864};
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866/**
867 * velocity_found1 - set up discovered velocity card
868 * @pdev: PCI device
869 * @ent: PCI device table entry that matched
870 *
871 * Configure a discovered adapter from scratch. Return a negative
872 * errno error code on failure paths.
873 */
874
875static int __devinit velocity_found1(struct pci_dev *pdev, const struct pci_device_id *ent)
876{
877 static int first = 1;
878 struct net_device *dev;
879 int i;
Sven Hartge07b5f6a2008-10-23 13:03:44 +0000880 const char *drv_string;
Jeff Garzikcabb7662006-06-27 09:25:28 -0400881 const struct velocity_info_tbl *info = &chip_info_table[ent->driver_data];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct velocity_info *vptr;
Dave Jonesc4067402009-07-20 17:35:21 +0000883 struct mac_regs __iomem *regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 int ret = -ENOMEM;
885
Jeff Garzike54f4892006-06-27 09:20:08 -0400886 /* FIXME: this driver, like almost all other ethernet drivers,
887 * can support more than MAX_UNITS.
888 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (velocity_nics >= MAX_UNITS) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400890 dev_notice(&pdev->dev, "already found %d NICs.\n",
Jeff Garzike54f4892006-06-27 09:20:08 -0400891 velocity_nics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return -ENODEV;
893 }
894
895 dev = alloc_etherdev(sizeof(struct velocity_info));
Jeff Garzike54f4892006-06-27 09:20:08 -0400896 if (!dev) {
Jeff Garzik9b91cf92006-06-27 11:39:50 -0400897 dev_err(&pdev->dev, "allocate net device failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 goto out;
899 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 /* Chain it all together */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 SET_NETDEV_DEV(dev, &pdev->dev);
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -0400904 vptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906
907 if (first) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400908 printk(KERN_INFO "%s Ver. %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 VELOCITY_FULL_DRV_NAM, VELOCITY_VERSION);
910 printk(KERN_INFO "Copyright (c) 2002, 2003 VIA Networking Technologies, Inc.\n");
911 printk(KERN_INFO "Copyright (c) 2004 Red Hat Inc.\n");
912 first = 0;
913 }
914
915 velocity_init_info(pdev, vptr, info);
916
917 vptr->dev = dev;
918
919 dev->irq = pdev->irq;
920
921 ret = pci_enable_device(pdev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400922 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 goto err_free_dev;
924
925 ret = velocity_get_pci_info(vptr, pdev);
926 if (ret < 0) {
Jeff Garzike54f4892006-06-27 09:20:08 -0400927 /* error message already printed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 goto err_disable;
929 }
930
931 ret = pci_request_regions(pdev, VELOCITY_NAME);
932 if (ret < 0) {
Jeff Garzik9b91cf92006-06-27 11:39:50 -0400933 dev_err(&pdev->dev, "No PCI resources.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 goto err_disable;
935 }
936
Jeff Garzikcabb7662006-06-27 09:25:28 -0400937 regs = ioremap(vptr->memaddr, VELOCITY_IO_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (regs == NULL) {
939 ret = -EIO;
940 goto err_release_res;
941 }
942
943 vptr->mac_regs = regs;
944
945 mac_wol_reset(regs);
946
947 dev->base_addr = vptr->ioaddr;
948
949 for (i = 0; i < 6; i++)
950 dev->dev_addr[i] = readb(&regs->PAR[i]);
951
952
Sven Hartge07b5f6a2008-10-23 13:03:44 +0000953 drv_string = dev_driver_string(&pdev->dev);
954
955 velocity_get_options(&vptr->options, velocity_nics, drv_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400957 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 * Mask out the options cannot be set to the chip
959 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 vptr->options.flags &= info->flags;
962
963 /*
964 * Enable the chip specified capbilities
965 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 vptr->flags = vptr->options.flags | (info->flags & 0xFF000000UL);
968
969 vptr->wol_opts = vptr->options.wol_opts;
970 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
971
972 vptr->phy_id = MII_GET_PHY_ID(vptr->mac_regs);
973
974 dev->irq = pdev->irq;
Stephen Hemminger39a11bd2008-11-19 22:19:33 -0800975 dev->netdev_ops = &velocity_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 dev->ethtool_ops = &velocity_ethtool_ops;
Stephen Hemminger501e4d22007-08-24 13:56:49 -0700977
Francois Romieud4f73c82008-04-24 23:32:33 +0200978 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_FILTER |
979 NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Stephen Hemminger501e4d22007-08-24 13:56:49 -0700981 if (vptr->flags & VELOCITY_FLAGS_TX_CSUM)
John W. Linville9f3f46b2005-12-09 10:36:09 -0500982 dev->features |= NETIF_F_IP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 ret = register_netdev(dev);
985 if (ret < 0)
986 goto err_iounmap;
987
Séguier Régisd3b238a2009-06-16 11:25:49 +0000988 if (!velocity_get_link(dev)) {
Francois Romieu8a22ddd2006-06-23 00:47:06 +0200989 netif_carrier_off(dev);
Séguier Régisd3b238a2009-06-16 11:25:49 +0000990 vptr->mii_status |= VELOCITY_LINK_FAIL;
991 }
Francois Romieu8a22ddd2006-06-23 00:47:06 +0200992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 velocity_print_info(vptr);
994 pci_set_drvdata(pdev, dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 /* and leave the chip powered down */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 pci_set_power_state(pdev, PCI_D3hot);
999#ifdef CONFIG_PM
1000 {
1001 unsigned long flags;
1002
1003 spin_lock_irqsave(&velocity_dev_list_lock, flags);
1004 list_add(&vptr->list, &velocity_dev_list);
1005 spin_unlock_irqrestore(&velocity_dev_list_lock, flags);
1006 }
1007#endif
1008 velocity_nics++;
1009out:
1010 return ret;
1011
1012err_iounmap:
1013 iounmap(regs);
1014err_release_res:
1015 pci_release_regions(pdev);
1016err_disable:
1017 pci_disable_device(pdev);
1018err_free_dev:
1019 free_netdev(dev);
1020 goto out;
1021}
1022
1023/**
1024 * velocity_print_info - per driver data
1025 * @vptr: velocity
1026 *
1027 * Print per driver data as the kernel driver finds Velocity
1028 * hardware
1029 */
1030
1031static void __devinit velocity_print_info(struct velocity_info *vptr)
1032{
1033 struct net_device *dev = vptr->dev;
1034
1035 printk(KERN_INFO "%s: %s\n", dev->name, get_chip_name(vptr->chip_id));
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001036 printk(KERN_INFO "%s: Ethernet Address: %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n",
1037 dev->name,
1038 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1040}
1041
1042/**
1043 * velocity_init_info - init private data
1044 * @pdev: PCI device
1045 * @vptr: Velocity info
1046 * @info: Board type
1047 *
1048 * Set up the initial velocity_info struct for the device that has been
1049 * discovered.
1050 */
1051
Jeff Garzikcabb7662006-06-27 09:25:28 -04001052static void __devinit velocity_init_info(struct pci_dev *pdev,
1053 struct velocity_info *vptr,
1054 const struct velocity_info_tbl *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
1056 memset(vptr, 0, sizeof(struct velocity_info));
1057
1058 vptr->pdev = pdev;
1059 vptr->chip_id = info->chip_id;
Francois Romieu0fe9f152008-07-31 22:10:10 +02001060 vptr->tx.numq = info->txqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 vptr->multicast_limit = MCAM_SIZE;
1062 spin_lock_init(&vptr->lock);
1063 INIT_LIST_HEAD(&vptr->list);
1064}
1065
1066/**
1067 * velocity_get_pci_info - retrieve PCI info for device
1068 * @vptr: velocity device
1069 * @pdev: PCI device it matches
1070 *
1071 * Retrieve the PCI configuration space data that interests us from
1072 * the kernel PCI layer
1073 */
1074
1075static int __devinit velocity_get_pci_info(struct velocity_info *vptr, struct pci_dev *pdev)
1076{
Auke Kok44c10132007-06-08 15:46:36 -07001077 vptr->rev_id = pdev->revision;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 pci_set_master(pdev);
1080
1081 vptr->ioaddr = pci_resource_start(pdev, 0);
1082 vptr->memaddr = pci_resource_start(pdev, 1);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001083
Jeff Garzike54f4892006-06-27 09:20:08 -04001084 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_IO)) {
Jeff Garzik9b91cf92006-06-27 11:39:50 -04001085 dev_err(&pdev->dev,
Jeff Garzike54f4892006-06-27 09:20:08 -04001086 "region #0 is not an I/O resource, aborting.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return -EINVAL;
1088 }
1089
Jeff Garzike54f4892006-06-27 09:20:08 -04001090 if ((pci_resource_flags(pdev, 1) & IORESOURCE_IO)) {
Jeff Garzik9b91cf92006-06-27 11:39:50 -04001091 dev_err(&pdev->dev,
Jeff Garzike54f4892006-06-27 09:20:08 -04001092 "region #1 is an I/O resource, aborting.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return -EINVAL;
1094 }
1095
Jeff Garzikcabb7662006-06-27 09:25:28 -04001096 if (pci_resource_len(pdev, 1) < VELOCITY_IO_SIZE) {
Jeff Garzik9b91cf92006-06-27 11:39:50 -04001097 dev_err(&pdev->dev, "region #1 is too small.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return -EINVAL;
1099 }
1100 vptr->pdev = pdev;
1101
1102 return 0;
1103}
1104
1105/**
Francois Romieu3c4dc712008-07-31 22:51:18 +02001106 * velocity_init_dma_rings - set up DMA rings
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 * @vptr: Velocity to set up
1108 *
1109 * Allocate PCI mapped DMA rings for the receive and transmit layer
1110 * to use.
1111 */
1112
Francois Romieu3c4dc712008-07-31 22:51:18 +02001113static int velocity_init_dma_rings(struct velocity_info *vptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Francois Romieu8ac53af2008-07-11 00:04:33 +02001115 struct velocity_opt *opt = &vptr->options;
1116 const unsigned int rx_ring_size = opt->numrx * sizeof(struct rx_desc);
1117 const unsigned int tx_ring_size = opt->numtx * sizeof(struct tx_desc);
1118 struct pci_dev *pdev = vptr->pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 dma_addr_t pool_dma;
Francois Romieu8ac53af2008-07-11 00:04:33 +02001120 void *pool;
1121 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 /*
Francois Romieu8ac53af2008-07-11 00:04:33 +02001124 * Allocate all RD/TD rings a single pool.
1125 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 * pci_alloc_consistent() fulfills the requirement for 64 bytes
1127 * alignment
1128 */
Francois Romieu0fe9f152008-07-31 22:10:10 +02001129 pool = pci_alloc_consistent(pdev, tx_ring_size * vptr->tx.numq +
Francois Romieu8ac53af2008-07-11 00:04:33 +02001130 rx_ring_size, &pool_dma);
1131 if (!pool) {
1132 dev_err(&pdev->dev, "%s : DMA memory allocation failed.\n",
1133 vptr->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return -ENOMEM;
1135 }
1136
Francois Romieu0fe9f152008-07-31 22:10:10 +02001137 vptr->rx.ring = pool;
1138 vptr->rx.pool_dma = pool_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Francois Romieu8ac53af2008-07-11 00:04:33 +02001140 pool += rx_ring_size;
1141 pool_dma += rx_ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Francois Romieu0fe9f152008-07-31 22:10:10 +02001143 for (i = 0; i < vptr->tx.numq; i++) {
1144 vptr->tx.rings[i] = pool;
1145 vptr->tx.pool_dma[i] = pool_dma;
Francois Romieu8ac53af2008-07-11 00:04:33 +02001146 pool += tx_ring_size;
1147 pool_dma += tx_ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
Francois Romieu8ac53af2008-07-11 00:04:33 +02001149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return 0;
1151}
1152
1153/**
Francois Romieu3c4dc712008-07-31 22:51:18 +02001154 * velocity_free_dma_rings - free PCI ring pointers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 * @vptr: Velocity to free from
1156 *
1157 * Clean up the PCI ring buffers allocated to this velocity.
1158 */
1159
Francois Romieu3c4dc712008-07-31 22:51:18 +02001160static void velocity_free_dma_rings(struct velocity_info *vptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Francois Romieu580a6902008-07-11 00:03:44 +02001162 const int size = vptr->options.numrx * sizeof(struct rx_desc) +
Francois Romieu0fe9f152008-07-31 22:10:10 +02001163 vptr->options.numtx * sizeof(struct tx_desc) * vptr->tx.numq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Francois Romieu0fe9f152008-07-31 22:10:10 +02001165 pci_free_consistent(vptr->pdev, size, vptr->rx.ring, vptr->rx.pool_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
Francois Romieu28133172008-07-11 00:05:17 +02001168static void velocity_give_many_rx_descs(struct velocity_info *vptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
1170 struct mac_regs __iomem *regs = vptr->mac_regs;
1171 int avail, dirty, unusable;
1172
1173 /*
1174 * RD number must be equal to 4X per hardware spec
1175 * (programming guide rev 1.20, p.13)
1176 */
Francois Romieu0fe9f152008-07-31 22:10:10 +02001177 if (vptr->rx.filled < 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 return;
1179
1180 wmb();
1181
Francois Romieu0fe9f152008-07-31 22:10:10 +02001182 unusable = vptr->rx.filled & 0x0003;
1183 dirty = vptr->rx.dirty - unusable;
1184 for (avail = vptr->rx.filled & 0xfffc; avail; avail--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 dirty = (dirty > 0) ? dirty - 1 : vptr->options.numrx - 1;
Francois Romieu0fe9f152008-07-31 22:10:10 +02001186 vptr->rx.ring[dirty].rdesc0.len |= OWNED_BY_NIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188
Francois Romieu0fe9f152008-07-31 22:10:10 +02001189 writew(vptr->rx.filled & 0xfffc, &regs->RBRDU);
1190 vptr->rx.filled = unusable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
1192
1193static int velocity_rx_refill(struct velocity_info *vptr)
1194{
Francois Romieu0fe9f152008-07-31 22:10:10 +02001195 int dirty = vptr->rx.dirty, done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 do {
Francois Romieu0fe9f152008-07-31 22:10:10 +02001198 struct rx_desc *rd = vptr->rx.ring + dirty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 /* Fine for an all zero Rx desc at init time as well */
Al Viro4a51c0d2008-01-30 19:10:13 +00001201 if (rd->rdesc0.len & OWNED_BY_NIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 break;
1203
Francois Romieu0fe9f152008-07-31 22:10:10 +02001204 if (!vptr->rx.info[dirty].skb) {
Francois Romieu28133172008-07-11 00:05:17 +02001205 if (velocity_alloc_rx_buf(vptr, dirty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 break;
1207 }
1208 done++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001209 dirty = (dirty < vptr->options.numrx - 1) ? dirty + 1 : 0;
Francois Romieu0fe9f152008-07-31 22:10:10 +02001210 } while (dirty != vptr->rx.curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 if (done) {
Francois Romieu0fe9f152008-07-31 22:10:10 +02001213 vptr->rx.dirty = dirty;
1214 vptr->rx.filled += done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216
Francois Romieu28133172008-07-11 00:05:17 +02001217 return done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
Francois Romieu9088d9a2008-07-11 00:05:57 +02001220static void velocity_set_rxbufsize(struct velocity_info *vptr, int mtu)
1221{
Francois Romieu0fe9f152008-07-31 22:10:10 +02001222 vptr->rx.buf_sz = (mtu <= ETH_DATA_LEN) ? PKT_BUF_SZ : mtu + 32;
Francois Romieu9088d9a2008-07-11 00:05:57 +02001223}
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225/**
1226 * velocity_init_rd_ring - set up receive ring
1227 * @vptr: velocity to configure
1228 *
1229 * Allocate and set up the receive buffers for each ring slot and
1230 * assign them to the network adapter.
1231 */
1232
1233static int velocity_init_rd_ring(struct velocity_info *vptr)
1234{
Francois Romieu28133172008-07-11 00:05:17 +02001235 int ret = -ENOMEM;
Stephen Hemminger48f6b052007-11-28 14:20:16 -08001236
Francois Romieu0fe9f152008-07-31 22:10:10 +02001237 vptr->rx.info = kcalloc(vptr->options.numrx,
Mariusz Kozlowskiae946072007-08-01 00:11:50 +02001238 sizeof(struct velocity_rd_info), GFP_KERNEL);
Francois Romieu0fe9f152008-07-31 22:10:10 +02001239 if (!vptr->rx.info)
Francois Romieu28133172008-07-11 00:05:17 +02001240 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Francois Romieu3c4dc712008-07-31 22:51:18 +02001242 velocity_init_rx_ring_indexes(vptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Francois Romieu28133172008-07-11 00:05:17 +02001244 if (velocity_rx_refill(vptr) != vptr->options.numrx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 VELOCITY_PRT(MSG_LEVEL_ERR, KERN_ERR
1246 "%s: failed to allocate RX buffer.\n", vptr->dev->name);
1247 velocity_free_rd_ring(vptr);
Francois Romieu28133172008-07-11 00:05:17 +02001248 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
Mariusz Kozlowskiae946072007-08-01 00:11:50 +02001250
Francois Romieu28133172008-07-11 00:05:17 +02001251 ret = 0;
1252out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return ret;
1254}
1255
1256/**
1257 * velocity_free_rd_ring - free receive ring
1258 * @vptr: velocity to clean up
1259 *
1260 * Free the receive buffers for each ring slot and any
1261 * attached socket buffers that need to go away.
1262 */
1263
1264static void velocity_free_rd_ring(struct velocity_info *vptr)
1265{
1266 int i;
1267
Francois Romieu0fe9f152008-07-31 22:10:10 +02001268 if (vptr->rx.info == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return;
1270
1271 for (i = 0; i < vptr->options.numrx; i++) {
Francois Romieu0fe9f152008-07-31 22:10:10 +02001272 struct velocity_rd_info *rd_info = &(vptr->rx.info[i]);
1273 struct rx_desc *rd = vptr->rx.ring + i;
Francois Romieub3c3e7d2006-02-27 23:11:08 +01001274
1275 memset(rd, 0, sizeof(*rd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 if (!rd_info->skb)
1278 continue;
Francois Romieu0fe9f152008-07-31 22:10:10 +02001279 pci_unmap_single(vptr->pdev, rd_info->skb_dma, vptr->rx.buf_sz,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 PCI_DMA_FROMDEVICE);
Francois Romieu822f1a52008-10-08 15:55:15 -07001281 rd_info->skb_dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 dev_kfree_skb(rd_info->skb);
1284 rd_info->skb = NULL;
1285 }
1286
Francois Romieu0fe9f152008-07-31 22:10:10 +02001287 kfree(vptr->rx.info);
1288 vptr->rx.info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
1291/**
1292 * velocity_init_td_ring - set up transmit ring
1293 * @vptr: velocity
1294 *
1295 * Set up the transmit ring and chain the ring pointers together.
1296 * Returns zero on success or a negative posix errno code for
1297 * failure.
1298 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300static int velocity_init_td_ring(struct velocity_info *vptr)
1301{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 dma_addr_t curr;
roel kluin0d1cfd22009-01-17 11:14:31 +00001303 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 /* Init the TD ring entries */
Francois Romieu0fe9f152008-07-31 22:10:10 +02001306 for (j = 0; j < vptr->tx.numq; j++) {
1307 curr = vptr->tx.pool_dma[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Francois Romieu0fe9f152008-07-31 22:10:10 +02001309 vptr->tx.infos[j] = kcalloc(vptr->options.numtx,
Mariusz Kozlowskiae946072007-08-01 00:11:50 +02001310 sizeof(struct velocity_td_info),
1311 GFP_KERNEL);
Francois Romieu0fe9f152008-07-31 22:10:10 +02001312 if (!vptr->tx.infos[j]) {
Dave Jonesc4067402009-07-20 17:35:21 +00001313 while (--j >= 0)
Francois Romieu0fe9f152008-07-31 22:10:10 +02001314 kfree(vptr->tx.infos[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return -ENOMEM;
1316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Francois Romieu0fe9f152008-07-31 22:10:10 +02001318 vptr->tx.tail[j] = vptr->tx.curr[j] = vptr->tx.used[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320 return 0;
1321}
1322
1323/*
1324 * FIXME: could we merge this with velocity_free_tx_buf ?
1325 */
1326
1327static void velocity_free_td_ring_entry(struct velocity_info *vptr,
1328 int q, int n)
1329{
Dave Jonesc4067402009-07-20 17:35:21 +00001330 struct velocity_td_info *td_info = &(vptr->tx.infos[q][n]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 if (td_info == NULL)
1334 return;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (td_info->skb) {
Dave Jonesc4067402009-07-20 17:35:21 +00001337 for (i = 0; i < td_info->nskb_dma; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (td_info->skb_dma[i]) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001339 pci_unmap_single(vptr->pdev, td_info->skb_dma[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 td_info->skb->len, PCI_DMA_TODEVICE);
Francois Romieu822f1a52008-10-08 15:55:15 -07001341 td_info->skb_dma[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343 }
1344 dev_kfree_skb(td_info->skb);
1345 td_info->skb = NULL;
1346 }
1347}
1348
1349/**
1350 * velocity_free_td_ring - free td ring
1351 * @vptr: velocity
1352 *
1353 * Free up the transmit ring for this particular velocity adapter.
1354 * We free the ring contents but not the ring itself.
1355 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357static void velocity_free_td_ring(struct velocity_info *vptr)
1358{
1359 int i, j;
1360
Francois Romieu0fe9f152008-07-31 22:10:10 +02001361 for (j = 0; j < vptr->tx.numq; j++) {
1362 if (vptr->tx.infos[j] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 continue;
Dave Jonesc4067402009-07-20 17:35:21 +00001364 for (i = 0; i < vptr->options.numtx; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 velocity_free_td_ring_entry(vptr, j, i);
1366
Francois Romieu0fe9f152008-07-31 22:10:10 +02001367 kfree(vptr->tx.infos[j]);
1368 vptr->tx.infos[j] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370}
1371
1372/**
1373 * velocity_rx_srv - service RX interrupt
1374 * @vptr: velocity
1375 * @status: adapter status (unused)
1376 *
1377 * Walk the receive ring of the velocity adapter and remove
1378 * any received packets from the receive queue. Hand the ring
1379 * slots back to the adapter for reuse.
1380 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382static int velocity_rx_srv(struct velocity_info *vptr, int status)
1383{
Eric Dumazet553e2332009-05-27 10:34:50 +00001384 struct net_device_stats *stats = &vptr->dev->stats;
Francois Romieu0fe9f152008-07-31 22:10:10 +02001385 int rd_curr = vptr->rx.curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 int works = 0;
1387
1388 do {
Francois Romieu0fe9f152008-07-31 22:10:10 +02001389 struct rx_desc *rd = vptr->rx.ring + rd_curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Francois Romieu0fe9f152008-07-31 22:10:10 +02001391 if (!vptr->rx.info[rd_curr].skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 break;
1393
Al Viro4a51c0d2008-01-30 19:10:13 +00001394 if (rd->rdesc0.len & OWNED_BY_NIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 break;
1396
1397 rmb();
1398
1399 /*
1400 * Don't drop CE or RL error frame although RXOK is off
1401 */
Al Viro4a51c0d2008-01-30 19:10:13 +00001402 if (rd->rdesc0.RSR & (RSR_RXOK | RSR_CE | RSR_RL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (velocity_receive_frame(vptr, rd_curr) < 0)
1404 stats->rx_dropped++;
1405 } else {
1406 if (rd->rdesc0.RSR & RSR_CRC)
1407 stats->rx_crc_errors++;
1408 if (rd->rdesc0.RSR & RSR_FAE)
1409 stats->rx_frame_errors++;
1410
1411 stats->rx_dropped++;
1412 }
1413
Al Viro4a51c0d2008-01-30 19:10:13 +00001414 rd->size |= RX_INTEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 rd_curr++;
1417 if (rd_curr >= vptr->options.numrx)
1418 rd_curr = 0;
1419 } while (++works <= 15);
1420
Francois Romieu0fe9f152008-07-31 22:10:10 +02001421 vptr->rx.curr = rd_curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Francois Romieu28133172008-07-11 00:05:17 +02001423 if ((works > 0) && (velocity_rx_refill(vptr) > 0))
1424 velocity_give_many_rx_descs(vptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 VAR_USED(stats);
1427 return works;
1428}
1429
1430/**
1431 * velocity_rx_csum - checksum process
1432 * @rd: receive packet descriptor
1433 * @skb: network layer packet buffer
1434 *
1435 * Process the status bits for the received packet and determine
1436 * if the checksum was computed and verified by the hardware
1437 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439static inline void velocity_rx_csum(struct rx_desc *rd, struct sk_buff *skb)
1440{
1441 skb->ip_summed = CHECKSUM_NONE;
1442
1443 if (rd->rdesc1.CSM & CSM_IPKT) {
1444 if (rd->rdesc1.CSM & CSM_IPOK) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001445 if ((rd->rdesc1.CSM & CSM_TCPKT) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 (rd->rdesc1.CSM & CSM_UDPKT)) {
Dave Jonesc4067402009-07-20 17:35:21 +00001447 if (!(rd->rdesc1.CSM & CSM_TUPOK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450 skb->ip_summed = CHECKSUM_UNNECESSARY;
1451 }
1452 }
1453}
1454
1455/**
1456 * velocity_rx_copy - in place Rx copy for small packets
1457 * @rx_skb: network layer packet buffer candidate
1458 * @pkt_size: received data size
1459 * @rd: receive packet descriptor
1460 * @dev: network device
1461 *
1462 * Replace the current skb that is scheduled for Rx processing by a
1463 * shorter, immediatly allocated skb, if the received packet is small
1464 * enough. This function returns a negative value if the received
1465 * packet is too big or if memory is exhausted.
1466 */
Stephen Hemmingerc73d2582008-04-16 16:37:31 -07001467static int velocity_rx_copy(struct sk_buff **rx_skb, int pkt_size,
1468 struct velocity_info *vptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
1470 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 if (pkt_size < rx_copybreak) {
1472 struct sk_buff *new_skb;
1473
Stephen Hemmingerc73d2582008-04-16 16:37:31 -07001474 new_skb = netdev_alloc_skb(vptr->dev, pkt_size + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 if (new_skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 new_skb->ip_summed = rx_skb[0]->ip_summed;
Stephen Hemmingerc73d2582008-04-16 16:37:31 -07001477 skb_reserve(new_skb, 2);
1478 skb_copy_from_linear_data(*rx_skb, new_skb->data, pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 *rx_skb = new_skb;
1480 ret = 0;
1481 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
1484 return ret;
1485}
1486
1487/**
1488 * velocity_iph_realign - IP header alignment
1489 * @vptr: velocity we are handling
1490 * @skb: network layer packet buffer
1491 * @pkt_size: received data size
1492 *
1493 * Align IP header on a 2 bytes boundary. This behavior can be
1494 * configured by the user.
1495 */
1496static inline void velocity_iph_realign(struct velocity_info *vptr,
1497 struct sk_buff *skb, int pkt_size)
1498{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (vptr->flags & VELOCITY_FLAGS_IP_ALIGN) {
Stephen Hemmingerc03571a2008-04-16 16:37:32 -07001500 memmove(skb->data + 2, skb->data, pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 skb_reserve(skb, 2);
1502 }
1503}
1504
1505/**
1506 * velocity_receive_frame - received packet processor
1507 * @vptr: velocity we are handling
1508 * @idx: ring index
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001509 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 * A packet has arrived. We process the packet and if appropriate
1511 * pass the frame up the network stack
1512 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514static int velocity_receive_frame(struct velocity_info *vptr, int idx)
1515{
1516 void (*pci_action)(struct pci_dev *, dma_addr_t, size_t, int);
Eric Dumazet553e2332009-05-27 10:34:50 +00001517 struct net_device_stats *stats = &vptr->dev->stats;
Francois Romieu0fe9f152008-07-31 22:10:10 +02001518 struct velocity_rd_info *rd_info = &(vptr->rx.info[idx]);
1519 struct rx_desc *rd = &(vptr->rx.ring[idx]);
Al Viro4a51c0d2008-01-30 19:10:13 +00001520 int pkt_len = le16_to_cpu(rd->rdesc0.len) & 0x3fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 struct sk_buff *skb;
1522
1523 if (rd->rdesc0.RSR & (RSR_STP | RSR_EDP)) {
1524 VELOCITY_PRT(MSG_LEVEL_VERBOSE, KERN_ERR " %s : the received frame span multple RDs.\n", vptr->dev->name);
1525 stats->rx_length_errors++;
1526 return -EINVAL;
1527 }
1528
1529 if (rd->rdesc0.RSR & RSR_MAR)
Eric Dumazet553e2332009-05-27 10:34:50 +00001530 stats->multicast++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532 skb = rd_info->skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 pci_dma_sync_single_for_cpu(vptr->pdev, rd_info->skb_dma,
Francois Romieu0fe9f152008-07-31 22:10:10 +02001535 vptr->rx.buf_sz, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 /*
1538 * Drop frame not meeting IEEE 802.3
1539 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (vptr->flags & VELOCITY_FLAGS_VAL_PKT_LEN) {
1542 if (rd->rdesc0.RSR & RSR_RL) {
1543 stats->rx_length_errors++;
1544 return -EINVAL;
1545 }
1546 }
1547
1548 pci_action = pci_dma_sync_single_for_device;
1549
1550 velocity_rx_csum(rd, skb);
1551
1552 if (velocity_rx_copy(&skb, pkt_len, vptr) < 0) {
1553 velocity_iph_realign(vptr, skb, pkt_len);
1554 pci_action = pci_unmap_single;
1555 rd_info->skb = NULL;
1556 }
1557
Francois Romieu0fe9f152008-07-31 22:10:10 +02001558 pci_action(vptr->pdev, rd_info->skb_dma, vptr->rx.buf_sz,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 PCI_DMA_FROMDEVICE);
1560
1561 skb_put(skb, pkt_len - 4);
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001562 skb->protocol = eth_type_trans(skb, vptr->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Francois Romieud4f73c82008-04-24 23:32:33 +02001564 if (vptr->vlgrp && (rd->rdesc0.RSR & RSR_DETAG)) {
1565 vlan_hwaccel_rx(skb, vptr->vlgrp,
1566 swab16(le16_to_cpu(rd->rdesc1.PQTAG)));
1567 } else
1568 netif_rx(skb);
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 stats->rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 return 0;
1573}
1574
1575/**
1576 * velocity_alloc_rx_buf - allocate aligned receive buffer
1577 * @vptr: velocity
1578 * @idx: ring index
1579 *
1580 * Allocate a new full sized buffer for the reception of a frame and
1581 * map it into PCI space for the hardware to use. The hardware
1582 * requires *64* byte alignment of the buffer which makes life
1583 * less fun than would be ideal.
1584 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586static int velocity_alloc_rx_buf(struct velocity_info *vptr, int idx)
1587{
Francois Romieu0fe9f152008-07-31 22:10:10 +02001588 struct rx_desc *rd = &(vptr->rx.ring[idx]);
1589 struct velocity_rd_info *rd_info = &(vptr->rx.info[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Francois Romieu0fe9f152008-07-31 22:10:10 +02001591 rd_info->skb = dev_alloc_skb(vptr->rx.buf_sz + 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if (rd_info->skb == NULL)
1593 return -ENOMEM;
1594
1595 /*
1596 * Do the gymnastics to get the buffer head for data at
1597 * 64byte alignment.
1598 */
David S. Miller689be432005-06-28 15:25:31 -07001599 skb_reserve(rd_info->skb, (unsigned long) rd_info->skb->data & 63);
Francois Romieu0fe9f152008-07-31 22:10:10 +02001600 rd_info->skb_dma = pci_map_single(vptr->pdev, rd_info->skb->data,
1601 vptr->rx.buf_sz, PCI_DMA_FROMDEVICE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 /*
1604 * Fill in the descriptor to match
Francois Romieu0fe9f152008-07-31 22:10:10 +02001605 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 *((u32 *) & (rd->rdesc0)) = 0;
Francois Romieu0fe9f152008-07-31 22:10:10 +02001608 rd->size = cpu_to_le16(vptr->rx.buf_sz) | RX_INTEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 rd->pa_low = cpu_to_le32(rd_info->skb_dma);
1610 rd->pa_high = 0;
1611 return 0;
1612}
1613
1614/**
1615 * tx_srv - transmit interrupt service
1616 * @vptr; Velocity
1617 * @status:
1618 *
1619 * Scan the queues looking for transmitted packets that
1620 * we can complete and clean up. Update any statistics as
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001621 * necessary/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624static int velocity_tx_srv(struct velocity_info *vptr, u32 status)
1625{
1626 struct tx_desc *td;
1627 int qnum;
1628 int full = 0;
1629 int idx;
1630 int works = 0;
1631 struct velocity_td_info *tdinfo;
Eric Dumazet553e2332009-05-27 10:34:50 +00001632 struct net_device_stats *stats = &vptr->dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Francois Romieu0fe9f152008-07-31 22:10:10 +02001634 for (qnum = 0; qnum < vptr->tx.numq; qnum++) {
1635 for (idx = vptr->tx.tail[qnum]; vptr->tx.used[qnum] > 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 idx = (idx + 1) % vptr->options.numtx) {
1637
1638 /*
1639 * Get Tx Descriptor
1640 */
Francois Romieu0fe9f152008-07-31 22:10:10 +02001641 td = &(vptr->tx.rings[qnum][idx]);
1642 tdinfo = &(vptr->tx.infos[qnum][idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Al Viro4a51c0d2008-01-30 19:10:13 +00001644 if (td->tdesc0.len & OWNED_BY_NIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 break;
1646
1647 if ((works++ > 15))
1648 break;
1649
1650 if (td->tdesc0.TSR & TSR0_TERR) {
1651 stats->tx_errors++;
1652 stats->tx_dropped++;
1653 if (td->tdesc0.TSR & TSR0_CDH)
1654 stats->tx_heartbeat_errors++;
1655 if (td->tdesc0.TSR & TSR0_CRS)
1656 stats->tx_carrier_errors++;
1657 if (td->tdesc0.TSR & TSR0_ABT)
1658 stats->tx_aborted_errors++;
1659 if (td->tdesc0.TSR & TSR0_OWC)
1660 stats->tx_window_errors++;
1661 } else {
1662 stats->tx_packets++;
1663 stats->tx_bytes += tdinfo->skb->len;
1664 }
1665 velocity_free_tx_buf(vptr, tdinfo);
Francois Romieu0fe9f152008-07-31 22:10:10 +02001666 vptr->tx.used[qnum]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
Francois Romieu0fe9f152008-07-31 22:10:10 +02001668 vptr->tx.tail[qnum] = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Dave Jonesc4067402009-07-20 17:35:21 +00001670 if (AVAIL_TD(vptr, qnum) < 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 full = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673 /*
1674 * Look to see if we should kick the transmit network
1675 * layer for more work.
1676 */
1677 if (netif_queue_stopped(vptr->dev) && (full == 0)
1678 && (!(vptr->mii_status & VELOCITY_LINK_FAIL))) {
1679 netif_wake_queue(vptr->dev);
1680 }
1681 return works;
1682}
1683
1684/**
1685 * velocity_print_link_status - link status reporting
1686 * @vptr: velocity to report on
1687 *
1688 * Turn the link status of the velocity card into a kernel log
1689 * description of the new link state, detailing speed and duplex
1690 * status
1691 */
1692
1693static void velocity_print_link_status(struct velocity_info *vptr)
1694{
1695
1696 if (vptr->mii_status & VELOCITY_LINK_FAIL) {
1697 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: failed to detect cable link\n", vptr->dev->name);
1698 } else if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
Dave Jonesb4fea612007-06-06 03:07:52 -04001699 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link auto-negotiation", vptr->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 if (vptr->mii_status & VELOCITY_SPEED_1000)
1702 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 1000M bps");
1703 else if (vptr->mii_status & VELOCITY_SPEED_100)
1704 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps");
1705 else
1706 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps");
1707
1708 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
1709 VELOCITY_PRT(MSG_LEVEL_INFO, " full duplex\n");
1710 else
1711 VELOCITY_PRT(MSG_LEVEL_INFO, " half duplex\n");
1712 } else {
1713 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link forced", vptr->dev->name);
1714 switch (vptr->options.spd_dpx) {
1715 case SPD_DPX_100_HALF:
1716 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps half duplex\n");
1717 break;
1718 case SPD_DPX_100_FULL:
1719 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps full duplex\n");
1720 break;
1721 case SPD_DPX_10_HALF:
1722 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps half duplex\n");
1723 break;
1724 case SPD_DPX_10_FULL:
1725 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps full duplex\n");
1726 break;
1727 default:
1728 break;
1729 }
1730 }
1731}
1732
1733/**
1734 * velocity_error - handle error from controller
1735 * @vptr: velocity
1736 * @status: card status
1737 *
1738 * Process an error report from the hardware and attempt to recover
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001739 * the card itself. At the moment we cannot recover from some
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 * theoretically impossible errors but this could be fixed using
1741 * the pci_device_failed logic to bounce the hardware
1742 *
1743 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745static void velocity_error(struct velocity_info *vptr, int status)
1746{
1747
1748 if (status & ISR_TXSTLI) {
Dave Jonesc4067402009-07-20 17:35:21 +00001749 struct mac_regs __iomem *regs = vptr->mac_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Eddy L O Jansson0e6ff152007-07-31 00:38:53 -07001751 printk(KERN_ERR "TD structure error TDindex=%hx\n", readw(&regs->TDIdx[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 BYTE_REG_BITS_ON(TXESR_TDSTR, &regs->TXESR);
1753 writew(TRDCSR_RUN, &regs->TDCSRClr);
1754 netif_stop_queue(vptr->dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 /* FIXME: port over the pci_device_failed code and use it
1757 here */
1758 }
1759
1760 if (status & ISR_SRCI) {
Dave Jonesc4067402009-07-20 17:35:21 +00001761 struct mac_regs __iomem *regs = vptr->mac_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 int linked;
1763
1764 if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
1765 vptr->mii_status = check_connection_type(regs);
1766
1767 /*
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001768 * If it is a 3119, disable frame bursting in
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 * halfduplex mode and enable it in fullduplex
1770 * mode
1771 */
1772 if (vptr->rev_id < REV_ID_VT3216_A0) {
1773 if (vptr->mii_status | VELOCITY_DUPLEX_FULL)
1774 BYTE_REG_BITS_ON(TCR_TB2BDIS, &regs->TCR);
1775 else
1776 BYTE_REG_BITS_OFF(TCR_TB2BDIS, &regs->TCR);
1777 }
1778 /*
1779 * Only enable CD heart beat counter in 10HD mode
1780 */
Dave Jonesc4067402009-07-20 17:35:21 +00001781 if (!(vptr->mii_status & VELOCITY_DUPLEX_FULL) && (vptr->mii_status & VELOCITY_SPEED_10))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 BYTE_REG_BITS_OFF(TESTCFG_HBDIS, &regs->TESTCFG);
Dave Jonesc4067402009-07-20 17:35:21 +00001783 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 BYTE_REG_BITS_ON(TESTCFG_HBDIS, &regs->TESTCFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 }
1786 /*
1787 * Get link status from PHYSR0
1788 */
1789 linked = readb(&regs->PHYSR0) & PHYSR0_LINKGD;
1790
1791 if (linked) {
1792 vptr->mii_status &= ~VELOCITY_LINK_FAIL;
Francois Romieu8a22ddd2006-06-23 00:47:06 +02001793 netif_carrier_on(vptr->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 } else {
1795 vptr->mii_status |= VELOCITY_LINK_FAIL;
Francois Romieu8a22ddd2006-06-23 00:47:06 +02001796 netif_carrier_off(vptr->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
1798
1799 velocity_print_link_status(vptr);
1800 enable_flow_control_ability(vptr);
1801
1802 /*
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001803 * Re-enable auto-polling because SRCI will disable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 * auto-polling
1805 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 enable_mii_autopoll(regs);
1808
1809 if (vptr->mii_status & VELOCITY_LINK_FAIL)
1810 netif_stop_queue(vptr->dev);
1811 else
1812 netif_wake_queue(vptr->dev);
1813
1814 };
1815 if (status & ISR_MIBFI)
1816 velocity_update_hw_mibs(vptr);
1817 if (status & ISR_LSTEI)
1818 mac_rx_queue_wake(vptr->mac_regs);
1819}
1820
1821/**
1822 * velocity_free_tx_buf - free transmit buffer
1823 * @vptr: velocity
1824 * @tdinfo: buffer
1825 *
1826 * Release an transmit buffer. If the buffer was preallocated then
1827 * recycle it, if not then unmap the buffer.
1828 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830static void velocity_free_tx_buf(struct velocity_info *vptr, struct velocity_td_info *tdinfo)
1831{
1832 struct sk_buff *skb = tdinfo->skb;
1833 int i;
Dave Jones59f8e162009-03-13 13:37:46 -07001834 int pktlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836 /*
1837 * Don't unmap the pre-allocated tx_bufs
1838 */
Francois Romieu580a6902008-07-11 00:03:44 +02001839 if (tdinfo->skb_dma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Dave Jonesf6b24ca2009-06-21 22:42:30 -07001841 pktlen = max_t(unsigned int, skb->len, ETH_ZLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 for (i = 0; i < tdinfo->nskb_dma; i++) {
Dave Jones59f8e162009-03-13 13:37:46 -07001843 pci_unmap_single(vptr->pdev, tdinfo->skb_dma[i], pktlen, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 tdinfo->skb_dma[i] = 0;
1845 }
1846 }
1847 dev_kfree_skb_irq(skb);
1848 tdinfo->skb = NULL;
1849}
1850
Francois Romieu3c4dc712008-07-31 22:51:18 +02001851static int velocity_init_rings(struct velocity_info *vptr, int mtu)
1852{
1853 int ret;
1854
1855 velocity_set_rxbufsize(vptr, mtu);
1856
1857 ret = velocity_init_dma_rings(vptr);
1858 if (ret < 0)
1859 goto out;
1860
1861 ret = velocity_init_rd_ring(vptr);
1862 if (ret < 0)
1863 goto err_free_dma_rings_0;
1864
1865 ret = velocity_init_td_ring(vptr);
1866 if (ret < 0)
1867 goto err_free_rd_ring_1;
1868out:
1869 return ret;
1870
1871err_free_rd_ring_1:
1872 velocity_free_rd_ring(vptr);
1873err_free_dma_rings_0:
1874 velocity_free_dma_rings(vptr);
1875 goto out;
1876}
1877
1878static void velocity_free_rings(struct velocity_info *vptr)
1879{
1880 velocity_free_td_ring(vptr);
1881 velocity_free_rd_ring(vptr);
1882 velocity_free_dma_rings(vptr);
1883}
1884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885/**
1886 * velocity_open - interface activation callback
1887 * @dev: network layer device to open
1888 *
1889 * Called when the network layer brings the interface up. Returns
1890 * a negative posix error code on failure, or zero on success.
1891 *
1892 * All the ring allocation and set up is done on open for this
1893 * adapter to minimise memory usage when inactive
1894 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896static int velocity_open(struct net_device *dev)
1897{
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04001898 struct velocity_info *vptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 int ret;
1900
Francois Romieu3c4dc712008-07-31 22:51:18 +02001901 ret = velocity_init_rings(vptr, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 if (ret < 0)
1903 goto out;
1904
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001905 /* Ensure chip is running */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 pci_set_power_state(vptr->pdev, PCI_D0);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001907
Francois Romieu28133172008-07-11 00:05:17 +02001908 velocity_give_many_rx_descs(vptr);
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 velocity_init_registers(vptr, VELOCITY_INIT_COLD);
1911
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07001912 ret = request_irq(vptr->pdev->irq, &velocity_intr, IRQF_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 dev->name, dev);
1914 if (ret < 0) {
1915 /* Power down the chip */
1916 pci_set_power_state(vptr->pdev, PCI_D3hot);
Francois Romieu3c4dc712008-07-31 22:51:18 +02001917 velocity_free_rings(vptr);
1918 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
1920
1921 mac_enable_int(vptr->mac_regs);
1922 netif_start_queue(dev);
1923 vptr->flags |= VELOCITY_FLAGS_OPENED;
1924out:
1925 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001928/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 * velocity_change_mtu - MTU change callback
1930 * @dev: network device
1931 * @new_mtu: desired MTU
1932 *
1933 * Handle requests from the networking layer for MTU change on
1934 * this interface. It gets called on a change by the network layer.
1935 * Return zero for success or negative posix error code.
1936 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938static int velocity_change_mtu(struct net_device *dev, int new_mtu)
1939{
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04001940 struct velocity_info *vptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 int ret = 0;
1942
1943 if ((new_mtu < VELOCITY_MIN_MTU) || new_mtu > (VELOCITY_MAX_MTU)) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001944 VELOCITY_PRT(MSG_LEVEL_ERR, KERN_NOTICE "%s: Invalid MTU.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 vptr->dev->name);
Francois Romieu3c4dc712008-07-31 22:51:18 +02001946 ret = -EINVAL;
1947 goto out_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 }
1949
Stephen Hemmingerbd7b3f32007-11-14 19:47:27 -08001950 if (!netif_running(dev)) {
1951 dev->mtu = new_mtu;
Francois Romieu3c4dc712008-07-31 22:51:18 +02001952 goto out_0;
Stephen Hemmingerbd7b3f32007-11-14 19:47:27 -08001953 }
1954
Francois Romieu3c4dc712008-07-31 22:51:18 +02001955 if (dev->mtu != new_mtu) {
1956 struct velocity_info *tmp_vptr;
1957 unsigned long flags;
1958 struct rx_info rx;
1959 struct tx_info tx;
1960
1961 tmp_vptr = kzalloc(sizeof(*tmp_vptr), GFP_KERNEL);
1962 if (!tmp_vptr) {
1963 ret = -ENOMEM;
1964 goto out_0;
1965 }
1966
1967 tmp_vptr->dev = dev;
1968 tmp_vptr->pdev = vptr->pdev;
1969 tmp_vptr->options = vptr->options;
1970 tmp_vptr->tx.numq = vptr->tx.numq;
1971
1972 ret = velocity_init_rings(tmp_vptr, new_mtu);
1973 if (ret < 0)
1974 goto out_free_tmp_vptr_1;
1975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 spin_lock_irqsave(&vptr->lock, flags);
1977
1978 netif_stop_queue(dev);
1979 velocity_shutdown(vptr);
1980
Francois Romieu3c4dc712008-07-31 22:51:18 +02001981 rx = vptr->rx;
1982 tx = vptr->tx;
1983
1984 vptr->rx = tmp_vptr->rx;
1985 vptr->tx = tmp_vptr->tx;
1986
1987 tmp_vptr->rx = rx;
1988 tmp_vptr->tx = tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 dev->mtu = new_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Francois Romieu3c4dc712008-07-31 22:51:18 +02001992 velocity_give_many_rx_descs(vptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994 velocity_init_registers(vptr, VELOCITY_INIT_COLD);
1995
1996 mac_enable_int(vptr->mac_regs);
1997 netif_start_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Francois Romieu3c4dc712008-07-31 22:51:18 +02001999 spin_unlock_irqrestore(&vptr->lock, flags);
2000
2001 velocity_free_rings(tmp_vptr);
2002
2003out_free_tmp_vptr_1:
2004 kfree(tmp_vptr);
2005 }
2006out_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 return ret;
2008}
2009
2010/**
2011 * velocity_shutdown - shut down the chip
2012 * @vptr: velocity to deactivate
2013 *
2014 * Shuts down the internal operations of the velocity and
2015 * disables interrupts, autopolling, transmit and receive
2016 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018static void velocity_shutdown(struct velocity_info *vptr)
2019{
Dave Jonesc4067402009-07-20 17:35:21 +00002020 struct mac_regs __iomem *regs = vptr->mac_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 mac_disable_int(regs);
2022 writel(CR0_STOP, &regs->CR0Set);
2023 writew(0xFFFF, &regs->TDCSRClr);
2024 writeb(0xFF, &regs->RDCSRClr);
2025 safe_disable_mii_autopoll(regs);
2026 mac_clear_isr(regs);
2027}
2028
2029/**
2030 * velocity_close - close adapter callback
2031 * @dev: network device
2032 *
2033 * Callback from the network layer when the velocity is being
2034 * deactivated by the network layer
2035 */
2036
2037static int velocity_close(struct net_device *dev)
2038{
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04002039 struct velocity_info *vptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
2041 netif_stop_queue(dev);
2042 velocity_shutdown(vptr);
2043
2044 if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED)
2045 velocity_get_ip(vptr);
2046 if (dev->irq != 0)
2047 free_irq(dev->irq, dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 /* Power down the chip */
2050 pci_set_power_state(vptr->pdev, PCI_D3hot);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 velocity_free_rings(vptr);
2053
2054 vptr->flags &= (~VELOCITY_FLAGS_OPENED);
2055 return 0;
2056}
2057
2058/**
2059 * velocity_xmit - transmit packet callback
2060 * @skb: buffer to transmit
2061 * @dev: network device
2062 *
2063 * Called by the networ layer to request a packet is queued to
2064 * the velocity. Returns zero on success.
2065 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067static int velocity_xmit(struct sk_buff *skb, struct net_device *dev)
2068{
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04002069 struct velocity_info *vptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 int qnum = 0;
2071 struct tx_desc *td_ptr;
2072 struct velocity_td_info *tdinfo;
2073 unsigned long flags;
Dave Jones59f8e162009-03-13 13:37:46 -07002074 int pktlen;
Francois Romieu580a6902008-07-11 00:03:44 +02002075 __le16 len;
2076 int index;
2077
2078
Dave Jones59f8e162009-03-13 13:37:46 -07002079 if (skb_padto(skb, ETH_ZLEN))
2080 goto out;
2081 pktlen = max_t(unsigned int, skb->len, ETH_ZLEN);
Francois Romieu580a6902008-07-11 00:03:44 +02002082
2083 len = cpu_to_le16(pktlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 spin_lock_irqsave(&vptr->lock, flags);
2086
Francois Romieu0fe9f152008-07-31 22:10:10 +02002087 index = vptr->tx.curr[qnum];
2088 td_ptr = &(vptr->tx.rings[qnum][index]);
2089 tdinfo = &(vptr->tx.infos[qnum][index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 td_ptr->tdesc1.TCR = TCR0_TIC;
Al Viro4a51c0d2008-01-30 19:10:13 +00002092 td_ptr->td_buf[0].size &= ~TD_QUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Dave Jones83c98a82009-07-21 09:15:49 +00002094 /*
2095 * Map the linear network buffer into PCI space and
2096 * add it to the transmit ring.
2097 */
2098 tdinfo->skb = skb;
2099 tdinfo->skb_dma[0] = pci_map_single(vptr->pdev, skb->data, pktlen, PCI_DMA_TODEVICE);
2100 td_ptr->tdesc0.len = len;
2101 td_ptr->td_buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]);
2102 td_ptr->td_buf[0].pa_high = 0;
2103 td_ptr->td_buf[0].size = len;
2104 tdinfo->nskb_dma = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Al Viro4a51c0d2008-01-30 19:10:13 +00002106 td_ptr->tdesc1.cmd = TCPLS_NORMAL + (tdinfo->nskb_dma + 1) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Stephen Hemminger501e4d22007-08-24 13:56:49 -07002108 if (vptr->vlgrp && vlan_tx_tag_present(skb)) {
Al Viro4a51c0d2008-01-30 19:10:13 +00002109 td_ptr->tdesc1.vlan = cpu_to_le16(vlan_tx_tag_get(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 td_ptr->tdesc1.TCR |= TCR0_VETAG;
2111 }
2112
2113 /*
2114 * Handle hardware checksum
2115 */
2116 if ((vptr->flags & VELOCITY_FLAGS_TX_CSUM)
Patrick McHardy84fa7932006-08-29 16:44:56 -07002117 && (skb->ip_summed == CHECKSUM_PARTIAL)) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002118 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 if (ip->protocol == IPPROTO_TCP)
2120 td_ptr->tdesc1.TCR |= TCR0_TCPCK;
2121 else if (ip->protocol == IPPROTO_UDP)
2122 td_ptr->tdesc1.TCR |= (TCR0_UDPCK);
2123 td_ptr->tdesc1.TCR |= TCR0_IPCK;
2124 }
2125 {
2126
2127 int prev = index - 1;
2128
2129 if (prev < 0)
2130 prev = vptr->options.numtx - 1;
Al Viro4a51c0d2008-01-30 19:10:13 +00002131 td_ptr->tdesc0.len |= OWNED_BY_NIC;
Francois Romieu0fe9f152008-07-31 22:10:10 +02002132 vptr->tx.used[qnum]++;
2133 vptr->tx.curr[qnum] = (index + 1) % vptr->options.numtx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
2135 if (AVAIL_TD(vptr, qnum) < 1)
2136 netif_stop_queue(dev);
2137
Francois Romieu0fe9f152008-07-31 22:10:10 +02002138 td_ptr = &(vptr->tx.rings[qnum][prev]);
Al Viro4a51c0d2008-01-30 19:10:13 +00002139 td_ptr->td_buf[0].size |= TD_QUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 mac_tx_queue_wake(vptr->mac_regs, qnum);
2141 }
2142 dev->trans_start = jiffies;
2143 spin_unlock_irqrestore(&vptr->lock, flags);
Francois Romieu580a6902008-07-11 00:03:44 +02002144out:
2145 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
2147
2148/**
2149 * velocity_intr - interrupt callback
2150 * @irq: interrupt number
2151 * @dev_instance: interrupting device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 *
2153 * Called whenever an interrupt is generated by the velocity
2154 * adapter IRQ line. We may not be the source of the interrupt
2155 * and need to identify initially if we are, and if not exit as
2156 * efficiently as possible.
2157 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002158
Séguier Régis3ca17df2009-04-09 01:33:57 +00002159static irqreturn_t velocity_intr(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
2161 struct net_device *dev = dev_instance;
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04002162 struct velocity_info *vptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 u32 isr_status;
2164 int max_count = 0;
2165
2166
2167 spin_lock(&vptr->lock);
2168 isr_status = mac_read_isr(vptr->mac_regs);
2169
2170 /* Not us ? */
2171 if (isr_status == 0) {
2172 spin_unlock(&vptr->lock);
2173 return IRQ_NONE;
2174 }
2175
2176 mac_disable_int(vptr->mac_regs);
2177
2178 /*
2179 * Keep processing the ISR until we have completed
2180 * processing and the isr_status becomes zero
2181 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 while (isr_status != 0) {
2184 mac_write_isr(vptr->mac_regs, isr_status);
2185 if (isr_status & (~(ISR_PRXI | ISR_PPRXI | ISR_PTXI | ISR_PPTXI)))
2186 velocity_error(vptr, isr_status);
2187 if (isr_status & (ISR_PRXI | ISR_PPRXI))
2188 max_count += velocity_rx_srv(vptr, isr_status);
2189 if (isr_status & (ISR_PTXI | ISR_PPTXI))
2190 max_count += velocity_tx_srv(vptr, isr_status);
2191 isr_status = mac_read_isr(vptr->mac_regs);
Dave Jonesc4067402009-07-20 17:35:21 +00002192 if (max_count > vptr->options.int_works) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002193 printk(KERN_WARNING "%s: excessive work at interrupt.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 dev->name);
2195 max_count = 0;
2196 }
2197 }
2198 spin_unlock(&vptr->lock);
2199 mac_enable_int(vptr->mac_regs);
2200 return IRQ_HANDLED;
2201
2202}
2203
2204
2205/**
2206 * velocity_set_multi - filter list change callback
2207 * @dev: network device
2208 *
2209 * Called by the network layer when the filter lists need to change
2210 * for a velocity adapter. Reload the CAMs with the new address
2211 * filter ruleset.
2212 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002213
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214static void velocity_set_multi(struct net_device *dev)
2215{
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04002216 struct velocity_info *vptr = netdev_priv(dev);
Dave Jonesc4067402009-07-20 17:35:21 +00002217 struct mac_regs __iomem *regs = vptr->mac_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 u8 rx_mode;
2219 int i;
2220 struct dev_mc_list *mclist;
2221
2222 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 writel(0xffffffff, &regs->MARCAM[0]);
2224 writel(0xffffffff, &regs->MARCAM[4]);
2225 rx_mode = (RCR_AM | RCR_AB | RCR_PROM);
2226 } else if ((dev->mc_count > vptr->multicast_limit)
2227 || (dev->flags & IFF_ALLMULTI)) {
2228 writel(0xffffffff, &regs->MARCAM[0]);
2229 writel(0xffffffff, &regs->MARCAM[4]);
2230 rx_mode = (RCR_AM | RCR_AB);
2231 } else {
2232 int offset = MCAM_SIZE - vptr->multicast_limit;
Stephen Hemminger01faccb2007-08-24 14:40:45 -07002233 mac_get_cam_mask(regs, vptr->mCAMmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
2235 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count; i++, mclist = mclist->next) {
Stephen Hemminger01faccb2007-08-24 14:40:45 -07002236 mac_set_cam(regs, i + offset, mclist->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 vptr->mCAMmask[(offset + i) / 8] |= 1 << ((offset + i) & 7);
2238 }
2239
Stephen Hemminger01faccb2007-08-24 14:40:45 -07002240 mac_set_cam_mask(regs, vptr->mCAMmask);
Joey Zhuo5f5c4bd2008-11-16 00:39:35 -08002241 rx_mode = RCR_AM | RCR_AB | RCR_AP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 }
2243 if (dev->mtu > 1500)
2244 rx_mode |= RCR_AL;
2245
2246 BYTE_REG_BITS_ON(rx_mode, &regs->RCR);
2247
2248}
2249
2250/**
2251 * velocity_get_status - statistics callback
2252 * @dev: network device
2253 *
2254 * Callback from the network layer to allow driver statistics
2255 * to be resynchronized with hardware collected state. In the
2256 * case of the velocity we need to pull the MIB counters from
2257 * the hardware into the counters before letting the network
2258 * layer display them.
2259 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002260
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261static struct net_device_stats *velocity_get_stats(struct net_device *dev)
2262{
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04002263 struct velocity_info *vptr = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 /* If the hardware is down, don't touch MII */
Dave Jonesc4067402009-07-20 17:35:21 +00002266 if (!netif_running(dev))
Eric Dumazet553e2332009-05-27 10:34:50 +00002267 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269 spin_lock_irq(&vptr->lock);
2270 velocity_update_hw_mibs(vptr);
2271 spin_unlock_irq(&vptr->lock);
2272
Eric Dumazet553e2332009-05-27 10:34:50 +00002273 dev->stats.rx_packets = vptr->mib_counter[HW_MIB_ifRxAllPkts];
2274 dev->stats.rx_errors = vptr->mib_counter[HW_MIB_ifRxErrorPkts];
2275 dev->stats.rx_length_errors = vptr->mib_counter[HW_MIB_ifInRangeLengthErrors];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
2277// unsigned long rx_dropped; /* no space in linux buffers */
Eric Dumazet553e2332009-05-27 10:34:50 +00002278 dev->stats.collisions = vptr->mib_counter[HW_MIB_ifTxEtherCollisions];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 /* detailed rx_errors: */
2280// unsigned long rx_length_errors;
2281// unsigned long rx_over_errors; /* receiver ring buff overflow */
Eric Dumazet553e2332009-05-27 10:34:50 +00002282 dev->stats.rx_crc_errors = vptr->mib_counter[HW_MIB_ifRxPktCRCE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283// unsigned long rx_frame_errors; /* recv'd frame alignment error */
2284// unsigned long rx_fifo_errors; /* recv'r fifo overrun */
2285// unsigned long rx_missed_errors; /* receiver missed packet */
2286
2287 /* detailed tx_errors */
2288// unsigned long tx_fifo_errors;
2289
Eric Dumazet553e2332009-05-27 10:34:50 +00002290 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291}
2292
2293
2294/**
2295 * velocity_ioctl - ioctl entry point
2296 * @dev: network device
2297 * @rq: interface request ioctl
2298 * @cmd: command code
2299 *
2300 * Called when the user issues an ioctl request to the network
2301 * device in question. The velocity interface supports MII.
2302 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304static int velocity_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2305{
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04002306 struct velocity_info *vptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 int ret;
2308
2309 /* If we are asked for information and the device is power
2310 saving then we need to bring the device back up to talk to it */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 if (!netif_running(dev))
2313 pci_set_power_state(vptr->pdev, PCI_D0);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 switch (cmd) {
2316 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
2317 case SIOCGMIIREG: /* Read MII PHY register. */
2318 case SIOCSMIIREG: /* Write to MII PHY register. */
2319 ret = velocity_mii_ioctl(dev, rq, cmd);
2320 break;
2321
2322 default:
2323 ret = -EOPNOTSUPP;
2324 }
2325 if (!netif_running(dev))
2326 pci_set_power_state(vptr->pdev, PCI_D3hot);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002327
2328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return ret;
2330}
2331
2332/*
2333 * Definition for our device driver. The PCI layer interface
2334 * uses this to handle all our card discover and plugging
2335 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337static struct pci_driver velocity_driver = {
2338 .name = VELOCITY_NAME,
2339 .id_table = velocity_id_table,
2340 .probe = velocity_found1,
2341 .remove = __devexit_p(velocity_remove1),
2342#ifdef CONFIG_PM
2343 .suspend = velocity_suspend,
2344 .resume = velocity_resume,
2345#endif
2346};
2347
2348/**
2349 * velocity_init_module - load time function
2350 *
2351 * Called when the velocity module is loaded. The PCI driver
2352 * is registered with the PCI layer, and in turn will call
2353 * the probe functions for each velocity adapter installed
2354 * in the system.
2355 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002356
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357static int __init velocity_init_module(void)
2358{
2359 int ret;
2360
2361 velocity_register_notifier();
Jeff Garzik29917622006-08-19 17:48:59 -04002362 ret = pci_register_driver(&velocity_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 if (ret < 0)
2364 velocity_unregister_notifier();
2365 return ret;
2366}
2367
2368/**
2369 * velocity_cleanup - module unload
2370 *
2371 * When the velocity hardware is unloaded this function is called.
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002372 * It will clean up the notifiers and the unregister the PCI
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 * driver interface for this hardware. This in turn cleans up
2374 * all discovered interfaces before returning from the function
2375 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377static void __exit velocity_cleanup_module(void)
2378{
2379 velocity_unregister_notifier();
2380 pci_unregister_driver(&velocity_driver);
2381}
2382
2383module_init(velocity_init_module);
2384module_exit(velocity_cleanup_module);
2385
2386
2387/*
2388 * MII access , media link mode setting functions
2389 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002390
2391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392/**
2393 * mii_init - set up MII
2394 * @vptr: velocity adapter
2395 * @mii_status: links tatus
2396 *
2397 * Set up the PHY for the current link state.
2398 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400static void mii_init(struct velocity_info *vptr, u32 mii_status)
2401{
2402 u16 BMCR;
2403
2404 switch (PHYID_GET_PHY_ID(vptr->phy_id)) {
2405 case PHYID_CICADA_CS8201:
2406 /*
2407 * Reset to hardware default
2408 */
2409 MII_REG_BITS_OFF((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
2410 /*
2411 * Turn on ECHODIS bit in NWay-forced full mode and turn it
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002412 * off it in NWay-forced half mode for NWay-forced v.s.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 * legacy-forced issue.
2414 */
2415 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
2416 MII_REG_BITS_ON(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2417 else
2418 MII_REG_BITS_OFF(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2419 /*
2420 * Turn on Link/Activity LED enable bit for CIS8201
2421 */
2422 MII_REG_BITS_ON(PLED_LALBE, MII_REG_PLED, vptr->mac_regs);
2423 break;
2424 case PHYID_VT3216_32BIT:
2425 case PHYID_VT3216_64BIT:
2426 /*
2427 * Reset to hardware default
2428 */
2429 MII_REG_BITS_ON((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
2430 /*
2431 * Turn on ECHODIS bit in NWay-forced full mode and turn it
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002432 * off it in NWay-forced half mode for NWay-forced v.s.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 * legacy-forced issue
2434 */
2435 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
2436 MII_REG_BITS_ON(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2437 else
2438 MII_REG_BITS_OFF(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2439 break;
2440
2441 case PHYID_MARVELL_1000:
2442 case PHYID_MARVELL_1000S:
2443 /*
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002444 * Assert CRS on Transmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 */
2446 MII_REG_BITS_ON(PSCR_ACRSTX, MII_REG_PSCR, vptr->mac_regs);
2447 /*
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002448 * Reset to hardware default
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 */
2450 MII_REG_BITS_ON((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
2451 break;
2452 default:
2453 ;
2454 }
2455 velocity_mii_read(vptr->mac_regs, MII_REG_BMCR, &BMCR);
2456 if (BMCR & BMCR_ISO) {
2457 BMCR &= ~BMCR_ISO;
2458 velocity_mii_write(vptr->mac_regs, MII_REG_BMCR, BMCR);
2459 }
2460}
2461
2462/**
2463 * safe_disable_mii_autopoll - autopoll off
2464 * @regs: velocity registers
2465 *
2466 * Turn off the autopoll and wait for it to disable on the chip
2467 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002468
Dave Jonesc4067402009-07-20 17:35:21 +00002469static void safe_disable_mii_autopoll(struct mac_regs __iomem *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
2471 u16 ww;
2472
2473 /* turn off MAUTO */
2474 writeb(0, &regs->MIICR);
2475 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
2476 udelay(1);
2477 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
2478 break;
2479 }
2480}
2481
2482/**
2483 * enable_mii_autopoll - turn on autopolling
2484 * @regs: velocity registers
2485 *
2486 * Enable the MII link status autopoll feature on the Velocity
2487 * hardware. Wait for it to enable.
2488 */
2489
Dave Jonesc4067402009-07-20 17:35:21 +00002490static void enable_mii_autopoll(struct mac_regs __iomem *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491{
2492 int ii;
2493
2494 writeb(0, &(regs->MIICR));
2495 writeb(MIIADR_SWMPL, &regs->MIIADR);
2496
2497 for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
2498 udelay(1);
2499 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
2500 break;
2501 }
2502
2503 writeb(MIICR_MAUTO, &regs->MIICR);
2504
2505 for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
2506 udelay(1);
2507 if (!BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
2508 break;
2509 }
2510
2511}
2512
2513/**
2514 * velocity_mii_read - read MII data
2515 * @regs: velocity registers
2516 * @index: MII register index
2517 * @data: buffer for received data
2518 *
2519 * Perform a single read of an MII 16bit register. Returns zero
2520 * on success or -ETIMEDOUT if the PHY did not respond.
2521 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523static int velocity_mii_read(struct mac_regs __iomem *regs, u8 index, u16 *data)
2524{
2525 u16 ww;
2526
2527 /*
2528 * Disable MIICR_MAUTO, so that mii addr can be set normally
2529 */
2530 safe_disable_mii_autopoll(regs);
2531
2532 writeb(index, &regs->MIIADR);
2533
2534 BYTE_REG_BITS_ON(MIICR_RCMD, &regs->MIICR);
2535
2536 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
2537 if (!(readb(&regs->MIICR) & MIICR_RCMD))
2538 break;
2539 }
2540
2541 *data = readw(&regs->MIIDATA);
2542
2543 enable_mii_autopoll(regs);
2544 if (ww == W_MAX_TIMEOUT)
2545 return -ETIMEDOUT;
2546 return 0;
2547}
2548
2549/**
2550 * velocity_mii_write - write MII data
2551 * @regs: velocity registers
2552 * @index: MII register index
2553 * @data: 16bit data for the MII register
2554 *
2555 * Perform a single write to an MII 16bit register. Returns zero
2556 * on success or -ETIMEDOUT if the PHY did not respond.
2557 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559static int velocity_mii_write(struct mac_regs __iomem *regs, u8 mii_addr, u16 data)
2560{
2561 u16 ww;
2562
2563 /*
2564 * Disable MIICR_MAUTO, so that mii addr can be set normally
2565 */
2566 safe_disable_mii_autopoll(regs);
2567
2568 /* MII reg offset */
2569 writeb(mii_addr, &regs->MIIADR);
2570 /* set MII data */
2571 writew(data, &regs->MIIDATA);
2572
2573 /* turn on MIICR_WCMD */
2574 BYTE_REG_BITS_ON(MIICR_WCMD, &regs->MIICR);
2575
2576 /* W_MAX_TIMEOUT is the timeout period */
2577 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
2578 udelay(5);
2579 if (!(readb(&regs->MIICR) & MIICR_WCMD))
2580 break;
2581 }
2582 enable_mii_autopoll(regs);
2583
2584 if (ww == W_MAX_TIMEOUT)
2585 return -ETIMEDOUT;
2586 return 0;
2587}
2588
2589/**
2590 * velocity_get_opt_media_mode - get media selection
2591 * @vptr: velocity adapter
2592 *
2593 * Get the media mode stored in EEPROM or module options and load
2594 * mii_status accordingly. The requested link state information
2595 * is also returned.
2596 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598static u32 velocity_get_opt_media_mode(struct velocity_info *vptr)
2599{
2600 u32 status = 0;
2601
2602 switch (vptr->options.spd_dpx) {
2603 case SPD_DPX_AUTO:
2604 status = VELOCITY_AUTONEG_ENABLE;
2605 break;
2606 case SPD_DPX_100_FULL:
2607 status = VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL;
2608 break;
2609 case SPD_DPX_10_FULL:
2610 status = VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL;
2611 break;
2612 case SPD_DPX_100_HALF:
2613 status = VELOCITY_SPEED_100;
2614 break;
2615 case SPD_DPX_10_HALF:
2616 status = VELOCITY_SPEED_10;
2617 break;
2618 }
2619 vptr->mii_status = status;
2620 return status;
2621}
2622
2623/**
2624 * mii_set_auto_on - autonegotiate on
2625 * @vptr: velocity
2626 *
2627 * Enable autonegotation on this interface
2628 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630static void mii_set_auto_on(struct velocity_info *vptr)
2631{
2632 if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs))
2633 MII_REG_BITS_ON(BMCR_REAUTO, MII_REG_BMCR, vptr->mac_regs);
2634 else
2635 MII_REG_BITS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs);
2636}
2637
2638
2639/*
Dave Jonesc4067402009-07-20 17:35:21 +00002640static void mii_set_auto_off(struct velocity_info *vptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641{
2642 MII_REG_BITS_OFF(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs);
2643}
2644*/
2645
2646/**
2647 * set_mii_flow_control - flow control setup
2648 * @vptr: velocity interface
2649 *
2650 * Set up the flow control on this interface according to
2651 * the supplied user/eeprom options.
2652 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654static void set_mii_flow_control(struct velocity_info *vptr)
2655{
2656 /*Enable or Disable PAUSE in ANAR */
2657 switch (vptr->options.flow_cntl) {
2658 case FLOW_CNTL_TX:
2659 MII_REG_BITS_OFF(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2660 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2661 break;
2662
2663 case FLOW_CNTL_RX:
2664 MII_REG_BITS_ON(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2665 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2666 break;
2667
2668 case FLOW_CNTL_TX_RX:
2669 MII_REG_BITS_ON(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2670 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2671 break;
2672
2673 case FLOW_CNTL_DISABLE:
2674 MII_REG_BITS_OFF(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2675 MII_REG_BITS_OFF(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2676 break;
2677 default:
2678 break;
2679 }
2680}
2681
2682/**
2683 * velocity_set_media_mode - set media mode
2684 * @mii_status: old MII link state
2685 *
2686 * Check the media link state and configure the flow control
2687 * PHY and also velocity hardware setup accordingly. In particular
2688 * we need to set up CD polling and frame bursting.
2689 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691static int velocity_set_media_mode(struct velocity_info *vptr, u32 mii_status)
2692{
2693 u32 curr_status;
Dave Jonesc4067402009-07-20 17:35:21 +00002694 struct mac_regs __iomem *regs = vptr->mac_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
2696 vptr->mii_status = mii_check_media_mode(vptr->mac_regs);
2697 curr_status = vptr->mii_status & (~VELOCITY_LINK_FAIL);
2698
2699 /* Set mii link status */
2700 set_mii_flow_control(vptr);
2701
2702 /*
2703 Check if new status is consisent with current status
2704 if (((mii_status & curr_status) & VELOCITY_AUTONEG_ENABLE)
2705 || (mii_status==curr_status)) {
2706 vptr->mii_status=mii_check_media_mode(vptr->mac_regs);
2707 vptr->mii_status=check_connection_type(vptr->mac_regs);
2708 VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity link no change\n");
2709 return 0;
2710 }
2711 */
2712
Dave Jonesc4067402009-07-20 17:35:21 +00002713 if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 MII_REG_BITS_ON(AUXCR_MDPPS, MII_REG_AUXCR, vptr->mac_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
2716 /*
2717 * If connection type is AUTO
2718 */
2719 if (mii_status & VELOCITY_AUTONEG_ENABLE) {
2720 VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity is AUTO mode\n");
2721 /* clear force MAC mode bit */
2722 BYTE_REG_BITS_OFF(CHIPGCR_FCMODE, &regs->CHIPGCR);
2723 /* set duplex mode of MAC according to duplex mode of MII */
2724 MII_REG_BITS_ON(ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10, MII_REG_ANAR, vptr->mac_regs);
2725 MII_REG_BITS_ON(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
2726 MII_REG_BITS_ON(BMCR_SPEED1G, MII_REG_BMCR, vptr->mac_regs);
2727
2728 /* enable AUTO-NEGO mode */
2729 mii_set_auto_on(vptr);
2730 } else {
2731 u16 ANAR;
2732 u8 CHIPGCR;
2733
2734 /*
2735 * 1. if it's 3119, disable frame bursting in halfduplex mode
2736 * and enable it in fullduplex mode
2737 * 2. set correct MII/GMII and half/full duplex mode in CHIPGCR
2738 * 3. only enable CD heart beat counter in 10HD mode
2739 */
2740
2741 /* set force MAC mode bit */
2742 BYTE_REG_BITS_ON(CHIPGCR_FCMODE, &regs->CHIPGCR);
2743
2744 CHIPGCR = readb(&regs->CHIPGCR);
2745 CHIPGCR &= ~CHIPGCR_FCGMII;
2746
2747 if (mii_status & VELOCITY_DUPLEX_FULL) {
2748 CHIPGCR |= CHIPGCR_FCFDX;
2749 writeb(CHIPGCR, &regs->CHIPGCR);
2750 VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced full mode\n");
2751 if (vptr->rev_id < REV_ID_VT3216_A0)
2752 BYTE_REG_BITS_OFF(TCR_TB2BDIS, &regs->TCR);
2753 } else {
2754 CHIPGCR &= ~CHIPGCR_FCFDX;
2755 VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced half mode\n");
2756 writeb(CHIPGCR, &regs->CHIPGCR);
2757 if (vptr->rev_id < REV_ID_VT3216_A0)
2758 BYTE_REG_BITS_ON(TCR_TB2BDIS, &regs->TCR);
2759 }
2760
2761 MII_REG_BITS_OFF(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
2762
Dave Jonesc4067402009-07-20 17:35:21 +00002763 if (!(mii_status & VELOCITY_DUPLEX_FULL) && (mii_status & VELOCITY_SPEED_10))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 BYTE_REG_BITS_OFF(TESTCFG_HBDIS, &regs->TESTCFG);
Dave Jonesc4067402009-07-20 17:35:21 +00002765 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 BYTE_REG_BITS_ON(TESTCFG_HBDIS, &regs->TESTCFG);
Dave Jonesc4067402009-07-20 17:35:21 +00002767
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 /* MII_REG_BITS_OFF(BMCR_SPEED1G, MII_REG_BMCR, vptr->mac_regs); */
2769 velocity_mii_read(vptr->mac_regs, MII_REG_ANAR, &ANAR);
2770 ANAR &= (~(ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10));
2771 if (mii_status & VELOCITY_SPEED_100) {
2772 if (mii_status & VELOCITY_DUPLEX_FULL)
2773 ANAR |= ANAR_TXFD;
2774 else
2775 ANAR |= ANAR_TX;
2776 } else {
2777 if (mii_status & VELOCITY_DUPLEX_FULL)
2778 ANAR |= ANAR_10FD;
2779 else
2780 ANAR |= ANAR_10;
2781 }
2782 velocity_mii_write(vptr->mac_regs, MII_REG_ANAR, ANAR);
2783 /* enable AUTO-NEGO mode */
2784 mii_set_auto_on(vptr);
2785 /* MII_REG_BITS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs); */
2786 }
2787 /* vptr->mii_status=mii_check_media_mode(vptr->mac_regs); */
2788 /* vptr->mii_status=check_connection_type(vptr->mac_regs); */
2789 return VELOCITY_LINK_CHANGE;
2790}
2791
2792/**
2793 * mii_check_media_mode - check media state
2794 * @regs: velocity registers
2795 *
2796 * Check the current MII status and determine the link status
2797 * accordingly
2798 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002799
Dave Jonesc4067402009-07-20 17:35:21 +00002800static u32 mii_check_media_mode(struct mac_regs __iomem *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801{
2802 u32 status = 0;
2803 u16 ANAR;
2804
2805 if (!MII_REG_BITS_IS_ON(BMSR_LNK, MII_REG_BMSR, regs))
2806 status |= VELOCITY_LINK_FAIL;
2807
2808 if (MII_REG_BITS_IS_ON(G1000CR_1000FD, MII_REG_G1000CR, regs))
2809 status |= VELOCITY_SPEED_1000 | VELOCITY_DUPLEX_FULL;
2810 else if (MII_REG_BITS_IS_ON(G1000CR_1000, MII_REG_G1000CR, regs))
2811 status |= (VELOCITY_SPEED_1000);
2812 else {
2813 velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
2814 if (ANAR & ANAR_TXFD)
2815 status |= (VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL);
2816 else if (ANAR & ANAR_TX)
2817 status |= VELOCITY_SPEED_100;
2818 else if (ANAR & ANAR_10FD)
2819 status |= (VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL);
2820 else
2821 status |= (VELOCITY_SPEED_10);
2822 }
2823
2824 if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, regs)) {
2825 velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
2826 if ((ANAR & (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10))
2827 == (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10)) {
2828 if (MII_REG_BITS_IS_ON(G1000CR_1000 | G1000CR_1000FD, MII_REG_G1000CR, regs))
2829 status |= VELOCITY_AUTONEG_ENABLE;
2830 }
2831 }
2832
2833 return status;
2834}
2835
Dave Jonesc4067402009-07-20 17:35:21 +00002836static u32 check_connection_type(struct mac_regs __iomem *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837{
2838 u32 status = 0;
2839 u8 PHYSR0;
2840 u16 ANAR;
2841 PHYSR0 = readb(&regs->PHYSR0);
2842
2843 /*
2844 if (!(PHYSR0 & PHYSR0_LINKGD))
2845 status|=VELOCITY_LINK_FAIL;
2846 */
2847
2848 if (PHYSR0 & PHYSR0_FDPX)
2849 status |= VELOCITY_DUPLEX_FULL;
2850
2851 if (PHYSR0 & PHYSR0_SPDG)
2852 status |= VELOCITY_SPEED_1000;
Jay Cliburn59b693f2006-07-20 23:23:57 +02002853 else if (PHYSR0 & PHYSR0_SPD10)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 status |= VELOCITY_SPEED_10;
2855 else
2856 status |= VELOCITY_SPEED_100;
2857
2858 if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, regs)) {
2859 velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
2860 if ((ANAR & (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10))
2861 == (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10)) {
2862 if (MII_REG_BITS_IS_ON(G1000CR_1000 | G1000CR_1000FD, MII_REG_G1000CR, regs))
2863 status |= VELOCITY_AUTONEG_ENABLE;
2864 }
2865 }
2866
2867 return status;
2868}
2869
2870/**
2871 * enable_flow_control_ability - flow control
2872 * @vptr: veloity to configure
2873 *
2874 * Set up flow control according to the flow control options
2875 * determined by the eeprom/configuration.
2876 */
2877
2878static void enable_flow_control_ability(struct velocity_info *vptr)
2879{
2880
Dave Jonesc4067402009-07-20 17:35:21 +00002881 struct mac_regs __iomem *regs = vptr->mac_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883 switch (vptr->options.flow_cntl) {
2884
2885 case FLOW_CNTL_DEFAULT:
2886 if (BYTE_REG_BITS_IS_ON(PHYSR0_RXFLC, &regs->PHYSR0))
2887 writel(CR0_FDXRFCEN, &regs->CR0Set);
2888 else
2889 writel(CR0_FDXRFCEN, &regs->CR0Clr);
2890
2891 if (BYTE_REG_BITS_IS_ON(PHYSR0_TXFLC, &regs->PHYSR0))
2892 writel(CR0_FDXTFCEN, &regs->CR0Set);
2893 else
2894 writel(CR0_FDXTFCEN, &regs->CR0Clr);
2895 break;
2896
2897 case FLOW_CNTL_TX:
2898 writel(CR0_FDXTFCEN, &regs->CR0Set);
2899 writel(CR0_FDXRFCEN, &regs->CR0Clr);
2900 break;
2901
2902 case FLOW_CNTL_RX:
2903 writel(CR0_FDXRFCEN, &regs->CR0Set);
2904 writel(CR0_FDXTFCEN, &regs->CR0Clr);
2905 break;
2906
2907 case FLOW_CNTL_TX_RX:
2908 writel(CR0_FDXTFCEN, &regs->CR0Set);
2909 writel(CR0_FDXRFCEN, &regs->CR0Set);
2910 break;
2911
2912 case FLOW_CNTL_DISABLE:
2913 writel(CR0_FDXRFCEN, &regs->CR0Clr);
2914 writel(CR0_FDXTFCEN, &regs->CR0Clr);
2915 break;
2916
2917 default:
2918 break;
2919 }
2920
2921}
2922
2923
2924/**
2925 * velocity_ethtool_up - pre hook for ethtool
2926 * @dev: network device
2927 *
2928 * Called before an ethtool operation. We need to make sure the
2929 * chip is out of D3 state before we poke at it.
2930 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002931
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932static int velocity_ethtool_up(struct net_device *dev)
2933{
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04002934 struct velocity_info *vptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 if (!netif_running(dev))
2936 pci_set_power_state(vptr->pdev, PCI_D0);
2937 return 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002938}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
2940/**
2941 * velocity_ethtool_down - post hook for ethtool
2942 * @dev: network device
2943 *
2944 * Called after an ethtool operation. Restore the chip back to D3
2945 * state if it isn't running.
2946 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948static void velocity_ethtool_down(struct net_device *dev)
2949{
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04002950 struct velocity_info *vptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 if (!netif_running(dev))
2952 pci_set_power_state(vptr->pdev, PCI_D3hot);
2953}
2954
2955static int velocity_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2956{
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04002957 struct velocity_info *vptr = netdev_priv(dev);
Dave Jonesc4067402009-07-20 17:35:21 +00002958 struct mac_regs __iomem *regs = vptr->mac_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 u32 status;
2960 status = check_connection_type(vptr->mac_regs);
2961
Jay Cliburn59b693f2006-07-20 23:23:57 +02002962 cmd->supported = SUPPORTED_TP |
2963 SUPPORTED_Autoneg |
2964 SUPPORTED_10baseT_Half |
2965 SUPPORTED_10baseT_Full |
2966 SUPPORTED_100baseT_Half |
2967 SUPPORTED_100baseT_Full |
2968 SUPPORTED_1000baseT_Half |
2969 SUPPORTED_1000baseT_Full;
2970 if (status & VELOCITY_SPEED_1000)
2971 cmd->speed = SPEED_1000;
2972 else if (status & VELOCITY_SPEED_100)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 cmd->speed = SPEED_100;
2974 else
2975 cmd->speed = SPEED_10;
2976 cmd->autoneg = (status & VELOCITY_AUTONEG_ENABLE) ? AUTONEG_ENABLE : AUTONEG_DISABLE;
2977 cmd->port = PORT_TP;
2978 cmd->transceiver = XCVR_INTERNAL;
2979 cmd->phy_address = readb(&regs->MIIADR) & 0x1F;
2980
2981 if (status & VELOCITY_DUPLEX_FULL)
2982 cmd->duplex = DUPLEX_FULL;
2983 else
2984 cmd->duplex = DUPLEX_HALF;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002985
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 return 0;
2987}
2988
2989static int velocity_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2990{
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04002991 struct velocity_info *vptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 u32 curr_status;
2993 u32 new_status = 0;
2994 int ret = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002995
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 curr_status = check_connection_type(vptr->mac_regs);
2997 curr_status &= (~VELOCITY_LINK_FAIL);
2998
2999 new_status |= ((cmd->autoneg) ? VELOCITY_AUTONEG_ENABLE : 0);
3000 new_status |= ((cmd->speed == SPEED_100) ? VELOCITY_SPEED_100 : 0);
3001 new_status |= ((cmd->speed == SPEED_10) ? VELOCITY_SPEED_10 : 0);
3002 new_status |= ((cmd->duplex == DUPLEX_FULL) ? VELOCITY_DUPLEX_FULL : 0);
3003
3004 if ((new_status & VELOCITY_AUTONEG_ENABLE) && (new_status != (curr_status | VELOCITY_AUTONEG_ENABLE)))
3005 ret = -EINVAL;
3006 else
3007 velocity_set_media_mode(vptr, new_status);
3008
3009 return ret;
3010}
3011
3012static u32 velocity_get_link(struct net_device *dev)
3013{
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04003014 struct velocity_info *vptr = netdev_priv(dev);
Dave Jonesc4067402009-07-20 17:35:21 +00003015 struct mac_regs __iomem *regs = vptr->mac_regs;
Jay Cliburn59b693f2006-07-20 23:23:57 +02003016 return BYTE_REG_BITS_IS_ON(PHYSR0_LINKGD, &regs->PHYSR0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017}
3018
3019static void velocity_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
3020{
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04003021 struct velocity_info *vptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 strcpy(info->driver, VELOCITY_NAME);
3023 strcpy(info->version, VELOCITY_VERSION);
3024 strcpy(info->bus_info, pci_name(vptr->pdev));
3025}
3026
3027static void velocity_ethtool_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3028{
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04003029 struct velocity_info *vptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 wol->supported = WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP;
3031 wol->wolopts |= WAKE_MAGIC;
3032 /*
3033 if (vptr->wol_opts & VELOCITY_WOL_PHY)
3034 wol.wolopts|=WAKE_PHY;
3035 */
3036 if (vptr->wol_opts & VELOCITY_WOL_UCAST)
3037 wol->wolopts |= WAKE_UCAST;
3038 if (vptr->wol_opts & VELOCITY_WOL_ARP)
3039 wol->wolopts |= WAKE_ARP;
3040 memcpy(&wol->sopass, vptr->wol_passwd, 6);
3041}
3042
3043static int velocity_ethtool_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3044{
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04003045 struct velocity_info *vptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
3047 if (!(wol->wolopts & (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP)))
3048 return -EFAULT;
3049 vptr->wol_opts = VELOCITY_WOL_MAGIC;
3050
3051 /*
3052 if (wol.wolopts & WAKE_PHY) {
3053 vptr->wol_opts|=VELOCITY_WOL_PHY;
3054 vptr->flags |=VELOCITY_FLAGS_WOL_ENABLED;
3055 }
3056 */
3057
3058 if (wol->wolopts & WAKE_MAGIC) {
3059 vptr->wol_opts |= VELOCITY_WOL_MAGIC;
3060 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
3061 }
3062 if (wol->wolopts & WAKE_UCAST) {
3063 vptr->wol_opts |= VELOCITY_WOL_UCAST;
3064 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
3065 }
3066 if (wol->wolopts & WAKE_ARP) {
3067 vptr->wol_opts |= VELOCITY_WOL_ARP;
3068 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
3069 }
3070 memcpy(vptr->wol_passwd, wol->sopass, 6);
3071 return 0;
3072}
3073
3074static u32 velocity_get_msglevel(struct net_device *dev)
3075{
3076 return msglevel;
3077}
3078
3079static void velocity_set_msglevel(struct net_device *dev, u32 value)
3080{
3081 msglevel = value;
3082}
3083
Jeff Garzik7282d492006-09-13 14:30:00 -04003084static const struct ethtool_ops velocity_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 .get_settings = velocity_get_settings,
3086 .set_settings = velocity_set_settings,
3087 .get_drvinfo = velocity_get_drvinfo,
3088 .get_wol = velocity_ethtool_get_wol,
3089 .set_wol = velocity_ethtool_set_wol,
3090 .get_msglevel = velocity_get_msglevel,
3091 .set_msglevel = velocity_set_msglevel,
3092 .get_link = velocity_get_link,
3093 .begin = velocity_ethtool_up,
3094 .complete = velocity_ethtool_down
3095};
3096
3097/**
3098 * velocity_mii_ioctl - MII ioctl handler
3099 * @dev: network device
3100 * @ifr: the ifreq block for the ioctl
3101 * @cmd: the command
3102 *
3103 * Process MII requests made via ioctl from the network layer. These
3104 * are used by tools like kudzu to interrogate the link state of the
3105 * hardware
3106 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003107
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108static int velocity_mii_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3109{
Jeff Garzik8ab6f3f2006-06-27 08:56:23 -04003110 struct velocity_info *vptr = netdev_priv(dev);
Dave Jonesc4067402009-07-20 17:35:21 +00003111 struct mac_regs __iomem *regs = vptr->mac_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 unsigned long flags;
3113 struct mii_ioctl_data *miidata = if_mii(ifr);
3114 int err;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003115
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 switch (cmd) {
3117 case SIOCGMIIPHY:
3118 miidata->phy_id = readb(&regs->MIIADR) & 0x1f;
3119 break;
3120 case SIOCGMIIREG:
3121 if (!capable(CAP_NET_ADMIN))
3122 return -EPERM;
Dave Jonesc4067402009-07-20 17:35:21 +00003123 if (velocity_mii_read(vptr->mac_regs, miidata->reg_num & 0x1f, &(miidata->val_out)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 return -ETIMEDOUT;
3125 break;
3126 case SIOCSMIIREG:
3127 if (!capable(CAP_NET_ADMIN))
3128 return -EPERM;
3129 spin_lock_irqsave(&vptr->lock, flags);
3130 err = velocity_mii_write(vptr->mac_regs, miidata->reg_num & 0x1f, miidata->val_in);
3131 spin_unlock_irqrestore(&vptr->lock, flags);
3132 check_connection_type(vptr->mac_regs);
Dave Jonesc4067402009-07-20 17:35:21 +00003133 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 return err;
3135 break;
3136 default:
3137 return -EOPNOTSUPP;
3138 }
3139 return 0;
3140}
3141
3142#ifdef CONFIG_PM
3143
3144/**
3145 * velocity_save_context - save registers
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003146 * @vptr: velocity
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 * @context: buffer for stored context
3148 *
3149 * Retrieve the current configuration from the velocity hardware
3150 * and stash it in the context structure, for use by the context
3151 * restore functions. This allows us to save things we need across
3152 * power down states
3153 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003154
Dave Jonesc4067402009-07-20 17:35:21 +00003155static void velocity_save_context(struct velocity_info *vptr, struct velocity_context *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
Dave Jonesc4067402009-07-20 17:35:21 +00003157 struct mac_regs __iomem *regs = vptr->mac_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 u16 i;
3159 u8 __iomem *ptr = (u8 __iomem *)regs;
3160
3161 for (i = MAC_REG_PAR; i < MAC_REG_CR0_CLR; i += 4)
3162 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3163
3164 for (i = MAC_REG_MAR; i < MAC_REG_TDCSR_CLR; i += 4)
3165 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3166
3167 for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4)
3168 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3169
3170}
3171
3172/**
3173 * velocity_restore_context - restore registers
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003174 * @vptr: velocity
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 * @context: buffer for stored context
3176 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003177 * Reload the register configuration from the velocity context
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 * created by velocity_save_context.
3179 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003180
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181static void velocity_restore_context(struct velocity_info *vptr, struct velocity_context *context)
3182{
Dave Jonesc4067402009-07-20 17:35:21 +00003183 struct mac_regs __iomem *regs = vptr->mac_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 int i;
3185 u8 __iomem *ptr = (u8 __iomem *)regs;
3186
Dave Jonesc4067402009-07-20 17:35:21 +00003187 for (i = MAC_REG_PAR; i < MAC_REG_CR0_SET; i += 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
3190 /* Just skip cr0 */
3191 for (i = MAC_REG_CR1_SET; i < MAC_REG_CR0_CLR; i++) {
3192 /* Clear */
3193 writeb(~(*((u8 *) (context->mac_reg + i))), ptr + i + 4);
3194 /* Set */
3195 writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
3196 }
3197
Dave Jonesc4067402009-07-20 17:35:21 +00003198 for (i = MAC_REG_MAR; i < MAC_REG_IMR; i += 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
Dave Jonesc4067402009-07-20 17:35:21 +00003201 for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Dave Jonesc4067402009-07-20 17:35:21 +00003204 for (i = MAC_REG_TDCSR_SET; i <= MAC_REG_RDCSR_SET; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206}
3207
3208/**
3209 * wol_calc_crc - WOL CRC
3210 * @pattern: data pattern
3211 * @mask_pattern: mask
3212 *
3213 * Compute the wake on lan crc hashes for the packet header
3214 * we are interested in.
3215 */
3216
Dave Jonesc4067402009-07-20 17:35:21 +00003217static u16 wol_calc_crc(int size, u8 *pattern, u8 *mask_pattern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218{
3219 u16 crc = 0xFFFF;
3220 u8 mask;
3221 int i, j;
3222
3223 for (i = 0; i < size; i++) {
3224 mask = mask_pattern[i];
3225
3226 /* Skip this loop if the mask equals to zero */
3227 if (mask == 0x00)
3228 continue;
3229
3230 for (j = 0; j < 8; j++) {
3231 if ((mask & 0x01) == 0) {
3232 mask >>= 1;
3233 continue;
3234 }
3235 mask >>= 1;
3236 crc = crc_ccitt(crc, &(pattern[i * 8 + j]), 1);
3237 }
3238 }
3239 /* Finally, invert the result once to get the correct data */
3240 crc = ~crc;
Akinobu Mita906d66d2006-12-08 02:36:25 -08003241 return bitrev32(crc) >> 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
3243
3244/**
3245 * velocity_set_wol - set up for wake on lan
3246 * @vptr: velocity to set WOL status on
3247 *
3248 * Set a card up for wake on lan either by unicast or by
3249 * ARP packet.
3250 *
3251 * FIXME: check static buffer is safe here
3252 */
3253
3254static int velocity_set_wol(struct velocity_info *vptr)
3255{
Dave Jonesc4067402009-07-20 17:35:21 +00003256 struct mac_regs __iomem *regs = vptr->mac_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 static u8 buf[256];
3258 int i;
3259
3260 static u32 mask_pattern[2][4] = {
3261 {0x00203000, 0x000003C0, 0x00000000, 0x0000000}, /* ARP */
3262 {0xfffff000, 0xffffffff, 0xffffffff, 0x000ffff} /* Magic Packet */
3263 };
3264
3265 writew(0xFFFF, &regs->WOLCRClr);
3266 writeb(WOLCFG_SAB | WOLCFG_SAM, &regs->WOLCFGSet);
3267 writew(WOLCR_MAGIC_EN, &regs->WOLCRSet);
3268
3269 /*
3270 if (vptr->wol_opts & VELOCITY_WOL_PHY)
3271 writew((WOLCR_LINKON_EN|WOLCR_LINKOFF_EN), &regs->WOLCRSet);
3272 */
3273
Dave Jonesc4067402009-07-20 17:35:21 +00003274 if (vptr->wol_opts & VELOCITY_WOL_UCAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 writew(WOLCR_UNICAST_EN, &regs->WOLCRSet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
3277 if (vptr->wol_opts & VELOCITY_WOL_ARP) {
3278 struct arp_packet *arp = (struct arp_packet *) buf;
3279 u16 crc;
3280 memset(buf, 0, sizeof(struct arp_packet) + 7);
3281
3282 for (i = 0; i < 4; i++)
3283 writel(mask_pattern[0][i], &regs->ByteMask[0][i]);
3284
3285 arp->type = htons(ETH_P_ARP);
3286 arp->ar_op = htons(1);
3287
3288 memcpy(arp->ar_tip, vptr->ip_addr, 4);
3289
3290 crc = wol_calc_crc((sizeof(struct arp_packet) + 7) / 8, buf,
3291 (u8 *) & mask_pattern[0][0]);
3292
3293 writew(crc, &regs->PatternCRC[0]);
3294 writew(WOLCR_ARP_EN, &regs->WOLCRSet);
3295 }
3296
3297 BYTE_REG_BITS_ON(PWCFG_WOLTYPE, &regs->PWCFGSet);
3298 BYTE_REG_BITS_ON(PWCFG_LEGACY_WOLEN, &regs->PWCFGSet);
3299
3300 writew(0x0FFF, &regs->WOLSRClr);
3301
3302 if (vptr->mii_status & VELOCITY_AUTONEG_ENABLE) {
3303 if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201)
3304 MII_REG_BITS_ON(AUXCR_MDPPS, MII_REG_AUXCR, vptr->mac_regs);
3305
3306 MII_REG_BITS_OFF(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
3307 }
3308
3309 if (vptr->mii_status & VELOCITY_SPEED_1000)
3310 MII_REG_BITS_ON(BMCR_REAUTO, MII_REG_BMCR, vptr->mac_regs);
3311
3312 BYTE_REG_BITS_ON(CHIPGCR_FCMODE, &regs->CHIPGCR);
3313
3314 {
3315 u8 GCR;
3316 GCR = readb(&regs->CHIPGCR);
3317 GCR = (GCR & ~CHIPGCR_FCGMII) | CHIPGCR_FCFDX;
3318 writeb(GCR, &regs->CHIPGCR);
3319 }
3320
3321 BYTE_REG_BITS_OFF(ISR_PWEI, &regs->ISR);
3322 /* Turn on SWPTAG just before entering power mode */
3323 BYTE_REG_BITS_ON(STICKHW_SWPTAG, &regs->STICKHW);
3324 /* Go to bed ..... */
3325 BYTE_REG_BITS_ON((STICKHW_DS1 | STICKHW_DS0), &regs->STICKHW);
3326
3327 return 0;
3328}
3329
3330static int velocity_suspend(struct pci_dev *pdev, pm_message_t state)
3331{
3332 struct net_device *dev = pci_get_drvdata(pdev);
3333 struct velocity_info *vptr = netdev_priv(dev);
3334 unsigned long flags;
3335
Dave Jonesc4067402009-07-20 17:35:21 +00003336 if (!netif_running(vptr->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 return 0;
3338
3339 netif_device_detach(vptr->dev);
3340
3341 spin_lock_irqsave(&vptr->lock, flags);
3342 pci_save_state(pdev);
3343#ifdef ETHTOOL_GWOL
3344 if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED) {
3345 velocity_get_ip(vptr);
3346 velocity_save_context(vptr, &vptr->context);
3347 velocity_shutdown(vptr);
3348 velocity_set_wol(vptr);
Al Viro4a51c0d2008-01-30 19:10:13 +00003349 pci_enable_wake(pdev, PCI_D3hot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 pci_set_power_state(pdev, PCI_D3hot);
3351 } else {
3352 velocity_save_context(vptr, &vptr->context);
3353 velocity_shutdown(vptr);
3354 pci_disable_device(pdev);
3355 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3356 }
3357#else
3358 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3359#endif
3360 spin_unlock_irqrestore(&vptr->lock, flags);
3361 return 0;
3362}
3363
3364static int velocity_resume(struct pci_dev *pdev)
3365{
3366 struct net_device *dev = pci_get_drvdata(pdev);
3367 struct velocity_info *vptr = netdev_priv(dev);
3368 unsigned long flags;
3369 int i;
3370
Dave Jonesc4067402009-07-20 17:35:21 +00003371 if (!netif_running(vptr->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 return 0;
3373
3374 pci_set_power_state(pdev, PCI_D0);
3375 pci_enable_wake(pdev, 0, 0);
3376 pci_restore_state(pdev);
3377
3378 mac_wol_reset(vptr->mac_regs);
3379
3380 spin_lock_irqsave(&vptr->lock, flags);
3381 velocity_restore_context(vptr, &vptr->context);
3382 velocity_init_registers(vptr, VELOCITY_INIT_WOL);
3383 mac_disable_int(vptr->mac_regs);
3384
3385 velocity_tx_srv(vptr, 0);
3386
Francois Romieu0fe9f152008-07-31 22:10:10 +02003387 for (i = 0; i < vptr->tx.numq; i++) {
Dave Jonesc4067402009-07-20 17:35:21 +00003388 if (vptr->tx.used[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 mac_tx_queue_wake(vptr->mac_regs, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 }
3391
3392 mac_enable_int(vptr->mac_regs);
3393 spin_unlock_irqrestore(&vptr->lock, flags);
3394 netif_device_attach(vptr->dev);
3395
3396 return 0;
3397}
3398
Randy Dunlapce9f7fe2006-12-18 21:21:10 -08003399#ifdef CONFIG_INET
3400
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401static int velocity_netdev_event(struct notifier_block *nb, unsigned long notification, void *ptr)
3402{
3403 struct in_ifaddr *ifa = (struct in_ifaddr *) ptr;
Denis V. Luneva337499f2008-02-28 20:44:27 -08003404 struct net_device *dev = ifa->ifa_dev->dev;
3405 struct velocity_info *vptr;
3406 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003408 if (dev_net(dev) != &init_net)
Denis V. Lunev6133fb12008-02-28 20:46:17 -08003409 return NOTIFY_DONE;
3410
Denis V. Luneva337499f2008-02-28 20:44:27 -08003411 spin_lock_irqsave(&velocity_dev_list_lock, flags);
3412 list_for_each_entry(vptr, &velocity_dev_list, list) {
3413 if (vptr->dev == dev) {
3414 velocity_get_ip(vptr);
3415 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 }
Denis V. Luneva337499f2008-02-28 20:44:27 -08003418 spin_unlock_irqrestore(&velocity_dev_list_lock, flags);
3419
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 return NOTIFY_DONE;
3421}
Randy Dunlapce9f7fe2006-12-18 21:21:10 -08003422
3423#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424#endif