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.
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.
12 * rx_copybreak/alignment
16 * The changes are (c) Copyright 2004, Red Hat Inc. <alan@redhat.com>
17 * Additional fixes and clean up: Francois Romieu
19 * This source has not been verified for use in safety critical systems.
21 * Please direct queries about the revamped driver to the linux-kernel
26 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
27 * All rights reserved.
29 * This software may be redistributed and/or modified under
30 * the terms of the GNU General Public License as published by the Free
31 * Software Foundation; either version 2 of the License, or
34 * This program is distributed in the hope that it will be useful, but
35 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
36 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
39 * Author: Chuang Liang-Shing, AJ Jiang
43 * MODULE_LICENSE("GPL");
48 #include <linux/module.h>
49 #include <linux/types.h>
50 #include <linux/init.h>
52 #include <linux/errno.h>
53 #include <linux/ioport.h>
54 #include <linux/pci.h>
55 #include <linux/kernel.h>
56 #include <linux/netdevice.h>
57 #include <linux/etherdevice.h>
58 #include <linux/skbuff.h>
59 #include <linux/delay.h>
60 #include <linux/timer.h>
61 #include <linux/slab.h>
62 #include <linux/interrupt.h>
63 #include <linux/string.h>
64 #include <linux/wait.h>
67 #include <asm/uaccess.h>
68 #include <linux/proc_fs.h>
69 #include <linux/inetdevice.h>
70 #include <linux/reboot.h>
71 #include <linux/ethtool.h>
72 #include <linux/mii.h>
74 #include <linux/if_arp.h>
75 #include <linux/if_vlan.h>
77 #include <linux/tcp.h>
78 #include <linux/udp.h>
79 #include <linux/crc-ccitt.h>
80 #include <linux/crc32.h>
82 #include "via-velocity.h"
85 static int velocity_nics
= 0;
86 static int msglevel
= MSG_LEVEL_INFO
;
89 static int velocity_mii_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
);
90 static const struct ethtool_ops velocity_ethtool_ops
;
96 MODULE_AUTHOR("VIA Networking Technologies, Inc.");
97 MODULE_LICENSE("GPL");
98 MODULE_DESCRIPTION("VIA Networking Velocity Family Gigabit Ethernet Adapter Driver");
100 #define VELOCITY_PARAM(N,D) \
101 static int N[MAX_UNITS]=OPTION_DEFAULT;\
102 module_param_array(N, int, NULL, 0); \
103 MODULE_PARM_DESC(N, D);
105 #define RX_DESC_MIN 64
106 #define RX_DESC_MAX 255
107 #define RX_DESC_DEF 64
108 VELOCITY_PARAM(RxDescriptors
, "Number of receive descriptors");
110 #define TX_DESC_MIN 16
111 #define TX_DESC_MAX 256
112 #define TX_DESC_DEF 64
113 VELOCITY_PARAM(TxDescriptors
, "Number of transmit descriptors");
115 #define RX_THRESH_MIN 0
116 #define RX_THRESH_MAX 3
117 #define RX_THRESH_DEF 0
118 /* rx_thresh[] is used for controlling the receive fifo threshold.
119 0: indicate the rxfifo threshold is 128 bytes.
120 1: indicate the rxfifo threshold is 512 bytes.
121 2: indicate the rxfifo threshold is 1024 bytes.
122 3: indicate the rxfifo threshold is store & forward.
124 VELOCITY_PARAM(rx_thresh
, "Receive fifo threshold");
126 #define DMA_LENGTH_MIN 0
127 #define DMA_LENGTH_MAX 7
128 #define DMA_LENGTH_DEF 0
130 /* DMA_length[] is used for controlling the DMA length
137 6: SF(flush till emply)
138 7: SF(flush till emply)
140 VELOCITY_PARAM(DMA_length
, "DMA length");
142 #define IP_ALIG_DEF 0
143 /* IP_byte_align[] is used for IP header DWORD byte aligned
144 0: indicate the IP header won't be DWORD byte aligned.(Default) .
145 1: indicate the IP header will be DWORD byte aligned.
146 In some enviroment, the IP header should be DWORD byte aligned,
147 or the packet will be droped when we receive it. (eg: IPVS)
149 VELOCITY_PARAM(IP_byte_align
, "Enable IP header dword aligned");
151 #define TX_CSUM_DEF 1
152 /* txcsum_offload[] is used for setting the checksum offload ability of NIC.
153 (We only support RX checksum offload now)
154 0: disable csum_offload[checksum offload
155 1: enable checksum offload. (Default)
157 VELOCITY_PARAM(txcsum_offload
, "Enable transmit packet checksum offload");
159 #define FLOW_CNTL_DEF 1
160 #define FLOW_CNTL_MIN 1
161 #define FLOW_CNTL_MAX 5
163 /* flow_control[] is used for setting the flow control ability of NIC.
164 1: hardware deafult - AUTO (default). Use Hardware default value in ANAR.
165 2: enable TX flow control.
166 3: enable RX flow control.
167 4: enable RX/TX flow control.
170 VELOCITY_PARAM(flow_control
, "Enable flow control ability");
172 #define MED_LNK_DEF 0
173 #define MED_LNK_MIN 0
174 #define MED_LNK_MAX 4
175 /* speed_duplex[] is used for setting the speed and duplex mode of NIC.
176 0: indicate autonegotiation for both speed and duplex mode
177 1: indicate 100Mbps half duplex mode
178 2: indicate 100Mbps full duplex mode
179 3: indicate 10Mbps half duplex mode
180 4: indicate 10Mbps full duplex mode
183 if EEPROM have been set to the force mode, this option is ignored
186 VELOCITY_PARAM(speed_duplex
, "Setting the speed and duplex mode");
188 #define VAL_PKT_LEN_DEF 0
189 /* ValPktLen[] is used for setting the checksum offload ability of NIC.
190 0: Receive frame with invalid layer 2 length (Default)
191 1: Drop frame with invalid layer 2 length
193 VELOCITY_PARAM(ValPktLen
, "Receiving or Drop invalid 802.3 frame");
195 #define WOL_OPT_DEF 0
196 #define WOL_OPT_MIN 0
197 #define WOL_OPT_MAX 7
198 /* wol_opts[] is used for controlling wake on lan behavior.
199 0: Wake up if recevied a magic packet. (Default)
200 1: Wake up if link status is on/off.
201 2: Wake up if recevied an arp packet.
202 4: Wake up if recevied any unicast packet.
203 Those value can be sumed up to support more than one option.
205 VELOCITY_PARAM(wol_opts
, "Wake On Lan options");
207 #define INT_WORKS_DEF 20
208 #define INT_WORKS_MIN 10
209 #define INT_WORKS_MAX 64
211 VELOCITY_PARAM(int_works
, "Number of packets per interrupt services");
213 static int rx_copybreak
= 200;
214 module_param(rx_copybreak
, int, 0644);
215 MODULE_PARM_DESC(rx_copybreak
, "Copy breakpoint for copy-only-tiny-frames");
217 static void velocity_init_info(struct pci_dev
*pdev
, struct velocity_info
*vptr
,
218 const struct velocity_info_tbl
*info
);
219 static int velocity_get_pci_info(struct velocity_info
*, struct pci_dev
*pdev
);
220 static void velocity_print_info(struct velocity_info
*vptr
);
221 static int velocity_open(struct net_device
*dev
);
222 static int velocity_change_mtu(struct net_device
*dev
, int mtu
);
223 static int velocity_xmit(struct sk_buff
*skb
, struct net_device
*dev
);
224 static int velocity_intr(int irq
, void *dev_instance
);
225 static void velocity_set_multi(struct net_device
*dev
);
226 static struct net_device_stats
*velocity_get_stats(struct net_device
*dev
);
227 static int velocity_ioctl(struct net_device
*dev
, struct ifreq
*rq
, int cmd
);
228 static int velocity_close(struct net_device
*dev
);
229 static int velocity_receive_frame(struct velocity_info
*, int idx
);
230 static int velocity_alloc_rx_buf(struct velocity_info
*, int idx
);
231 static void velocity_free_rd_ring(struct velocity_info
*vptr
);
232 static void velocity_free_tx_buf(struct velocity_info
*vptr
, struct velocity_td_info
*);
233 static int velocity_soft_reset(struct velocity_info
*vptr
);
234 static void mii_init(struct velocity_info
*vptr
, u32 mii_status
);
235 static u32
velocity_get_link(struct net_device
*dev
);
236 static u32
velocity_get_opt_media_mode(struct velocity_info
*vptr
);
237 static void velocity_print_link_status(struct velocity_info
*vptr
);
238 static void safe_disable_mii_autopoll(struct mac_regs __iomem
* regs
);
239 static void velocity_shutdown(struct velocity_info
*vptr
);
240 static void enable_flow_control_ability(struct velocity_info
*vptr
);
241 static void enable_mii_autopoll(struct mac_regs __iomem
* regs
);
242 static int velocity_mii_read(struct mac_regs __iomem
*, u8 byIdx
, u16
* pdata
);
243 static int velocity_mii_write(struct mac_regs __iomem
*, u8 byMiiAddr
, u16 data
);
244 static u32
mii_check_media_mode(struct mac_regs __iomem
* regs
);
245 static u32
check_connection_type(struct mac_regs __iomem
* regs
);
246 static int velocity_set_media_mode(struct velocity_info
*vptr
, u32 mii_status
);
250 static int velocity_suspend(struct pci_dev
*pdev
, pm_message_t state
);
251 static int velocity_resume(struct pci_dev
*pdev
);
253 static DEFINE_SPINLOCK(velocity_dev_list_lock
);
254 static LIST_HEAD(velocity_dev_list
);
258 #if defined(CONFIG_PM) && defined(CONFIG_INET)
260 static int velocity_netdev_event(struct notifier_block
*nb
, unsigned long notification
, void *ptr
);
262 static struct notifier_block velocity_inetaddr_notifier
= {
263 .notifier_call
= velocity_netdev_event
,
266 static void velocity_register_notifier(void)
268 register_inetaddr_notifier(&velocity_inetaddr_notifier
);
271 static void velocity_unregister_notifier(void)
273 unregister_inetaddr_notifier(&velocity_inetaddr_notifier
);
278 #define velocity_register_notifier() do {} while (0)
279 #define velocity_unregister_notifier() do {} while (0)
284 * Internal board variants. At the moment we have only one
287 static const struct velocity_info_tbl chip_info_table
[] __devinitdata
= {
288 {CHIP_TYPE_VT6110
, "VIA Networking Velocity Family Gigabit Ethernet Adapter", 1, 0x00FFFFFFUL
},
293 * Describe the PCI device identifiers that we support in this
294 * device driver. Used for hotplug autoloading.
297 static const struct pci_device_id velocity_id_table
[] __devinitdata
= {
298 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, PCI_DEVICE_ID_VIA_612X
) },
302 MODULE_DEVICE_TABLE(pci
, velocity_id_table
);
305 * get_chip_name - identifier to name
306 * @id: chip identifier
308 * Given a chip identifier return a suitable description. Returns
309 * a pointer a static string valid while the driver is loaded.
312 static char __devinit
*get_chip_name(enum chip_type chip_id
)
315 for (i
= 0; chip_info_table
[i
].name
!= NULL
; i
++)
316 if (chip_info_table
[i
].chip_id
== chip_id
)
318 return chip_info_table
[i
].name
;
322 * velocity_remove1 - device unplug
323 * @pdev: PCI device being removed
325 * Device unload callback. Called on an unplug or on module
326 * unload for each active device that is present. Disconnects
327 * the device from the network layer and frees all the resources
330 static void __devexit
velocity_remove1(struct pci_dev
*pdev
)
332 struct net_device
*dev
= pci_get_drvdata(pdev
);
333 struct velocity_info
*vptr
= netdev_priv(dev
);
338 spin_lock_irqsave(&velocity_dev_list_lock
, flags
);
339 if (!list_empty(&velocity_dev_list
))
340 list_del(&vptr
->list
);
341 spin_unlock_irqrestore(&velocity_dev_list_lock
, flags
);
343 unregister_netdev(dev
);
344 iounmap(vptr
->mac_regs
);
345 pci_release_regions(pdev
);
346 pci_disable_device(pdev
);
347 pci_set_drvdata(pdev
, NULL
);
354 * velocity_set_int_opt - parser for integer options
355 * @opt: pointer to option value
356 * @val: value the user requested (or -1 for default)
357 * @min: lowest value allowed
358 * @max: highest value allowed
359 * @def: default value
360 * @name: property name
363 * Set an integer property in the module options. This function does
364 * all the verification and checking as well as reporting so that
365 * we don't duplicate code for each option.
368 static void __devinit
velocity_set_int_opt(int *opt
, int val
, int min
, int max
, int def
, char *name
, char *devname
)
372 else if (val
< min
|| val
> max
) {
373 VELOCITY_PRT(MSG_LEVEL_INFO
, KERN_NOTICE
"%s: the value of parameter %s is invalid, the valid range is (%d-%d)\n",
374 devname
, name
, min
, max
);
377 VELOCITY_PRT(MSG_LEVEL_INFO
, KERN_INFO
"%s: set value of parameter %s to %d\n",
384 * velocity_set_bool_opt - parser for boolean options
385 * @opt: pointer to option value
386 * @val: value the user requested (or -1 for default)
387 * @def: default value (yes/no)
388 * @flag: numeric value to set for true.
389 * @name: property name
392 * Set a boolean property in the module options. This function does
393 * all the verification and checking as well as reporting so that
394 * we don't duplicate code for each option.
397 static void __devinit
velocity_set_bool_opt(u32
* opt
, int val
, int def
, u32 flag
, char *name
, char *devname
)
401 *opt
|= (def
? flag
: 0);
402 else if (val
< 0 || val
> 1) {
403 printk(KERN_NOTICE
"%s: the value of parameter %s is invalid, the valid range is (0-1)\n",
405 *opt
|= (def
? flag
: 0);
407 printk(KERN_INFO
"%s: set parameter %s to %s\n",
408 devname
, name
, val
? "TRUE" : "FALSE");
409 *opt
|= (val
? flag
: 0);
414 * velocity_get_options - set options on device
415 * @opts: option structure for the device
416 * @index: index of option to use in module options array
417 * @devname: device name
419 * Turn the module and command options into a single structure
420 * for the current device
423 static void __devinit
velocity_get_options(struct velocity_opt
*opts
, int index
, char *devname
)
426 velocity_set_int_opt(&opts
->rx_thresh
, rx_thresh
[index
], RX_THRESH_MIN
, RX_THRESH_MAX
, RX_THRESH_DEF
, "rx_thresh", devname
);
427 velocity_set_int_opt(&opts
->DMA_length
, DMA_length
[index
], DMA_LENGTH_MIN
, DMA_LENGTH_MAX
, DMA_LENGTH_DEF
, "DMA_length", devname
);
428 velocity_set_int_opt(&opts
->numrx
, RxDescriptors
[index
], RX_DESC_MIN
, RX_DESC_MAX
, RX_DESC_DEF
, "RxDescriptors", devname
);
429 velocity_set_int_opt(&opts
->numtx
, TxDescriptors
[index
], TX_DESC_MIN
, TX_DESC_MAX
, TX_DESC_DEF
, "TxDescriptors", devname
);
431 velocity_set_bool_opt(&opts
->flags
, txcsum_offload
[index
], TX_CSUM_DEF
, VELOCITY_FLAGS_TX_CSUM
, "txcsum_offload", devname
);
432 velocity_set_int_opt(&opts
->flow_cntl
, flow_control
[index
], FLOW_CNTL_MIN
, FLOW_CNTL_MAX
, FLOW_CNTL_DEF
, "flow_control", devname
);
433 velocity_set_bool_opt(&opts
->flags
, IP_byte_align
[index
], IP_ALIG_DEF
, VELOCITY_FLAGS_IP_ALIGN
, "IP_byte_align", devname
);
434 velocity_set_bool_opt(&opts
->flags
, ValPktLen
[index
], VAL_PKT_LEN_DEF
, VELOCITY_FLAGS_VAL_PKT_LEN
, "ValPktLen", devname
);
435 velocity_set_int_opt((int *) &opts
->spd_dpx
, speed_duplex
[index
], MED_LNK_MIN
, MED_LNK_MAX
, MED_LNK_DEF
, "Media link mode", devname
);
436 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
);
437 velocity_set_int_opt((int *) &opts
->int_works
, int_works
[index
], INT_WORKS_MIN
, INT_WORKS_MAX
, INT_WORKS_DEF
, "Interrupt service works", devname
);
438 opts
->numrx
= (opts
->numrx
& ~3);
442 * velocity_init_cam_filter - initialise CAM
443 * @vptr: velocity to program
445 * Initialize the content addressable memory used for filters. Load
446 * appropriately according to the presence of VLAN
449 static void velocity_init_cam_filter(struct velocity_info
*vptr
)
451 struct mac_regs __iomem
* regs
= vptr
->mac_regs
;
454 /* Turn on MCFG_PQEN, turn off MCFG_RTGOPT */
455 WORD_REG_BITS_SET(MCFG_PQEN
, MCFG_RTGOPT
, ®s
->MCFG
);
456 WORD_REG_BITS_ON(MCFG_VIDFR
, ®s
->MCFG
);
458 /* Disable all CAMs */
459 memset(vptr
->vCAMmask
, 0, sizeof(u8
) * 8);
460 memset(vptr
->mCAMmask
, 0, sizeof(u8
) * 8);
461 mac_set_cam_mask(regs
, vptr
->vCAMmask
, VELOCITY_VLAN_ID_CAM
);
462 mac_set_cam_mask(regs
, vptr
->mCAMmask
, VELOCITY_MULTICAST_CAM
);
464 /* Enable first VCAM */
466 for (vid
= 0; vid
< VLAN_VID_MASK
; vid
++) {
467 if (vlan_group_get_device(vptr
->vlgrp
, vid
)) {
468 /* If Tagging option is enabled and
469 VLAN ID is not zero, then
470 turn on MCFG_RTGOPT also */
472 WORD_REG_BITS_ON(MCFG_RTGOPT
, ®s
->MCFG
);
474 mac_set_cam(regs
, 0, (u8
*) &vid
,
475 VELOCITY_VLAN_ID_CAM
);
478 vptr
->vCAMmask
[0] |= 1;
479 mac_set_cam_mask(regs
, vptr
->vCAMmask
, VELOCITY_VLAN_ID_CAM
);
482 mac_set_cam(regs
, 0, (u8
*) &temp
, VELOCITY_VLAN_ID_CAM
);
484 mac_set_cam_mask(regs
, (u8
*) &temp
, VELOCITY_VLAN_ID_CAM
);
488 static void velocity_vlan_rx_add_vid(struct net_device
*dev
, unsigned short vid
)
490 struct velocity_info
*vptr
= netdev_priv(dev
);
492 spin_lock_irq(&vptr
->lock
);
493 velocity_init_cam_filter(vptr
);
494 spin_unlock_irq(&vptr
->lock
);
497 static void velocity_vlan_rx_kill_vid(struct net_device
*dev
, unsigned short vid
)
499 struct velocity_info
*vptr
= netdev_priv(dev
);
501 spin_lock_irq(&vptr
->lock
);
502 vlan_group_set_device(vptr
->vlgrp
, vid
, NULL
);
503 velocity_init_cam_filter(vptr
);
504 spin_unlock_irq(&vptr
->lock
);
509 * velocity_rx_reset - handle a receive reset
510 * @vptr: velocity we are resetting
512 * Reset the ownership and status for the receive ring side.
513 * Hand all the receive queue to the NIC.
516 static void velocity_rx_reset(struct velocity_info
*vptr
)
519 struct mac_regs __iomem
* regs
= vptr
->mac_regs
;
522 vptr
->rd_dirty
= vptr
->rd_filled
= vptr
->rd_curr
= 0;
525 * Init state, all RD entries belong to the NIC
527 for (i
= 0; i
< vptr
->options
.numrx
; ++i
)
528 vptr
->rd_ring
[i
].rdesc0
.owner
= OWNED_BY_NIC
;
530 writew(vptr
->options
.numrx
, ®s
->RBRDU
);
531 writel(vptr
->rd_pool_dma
, ®s
->RDBaseLo
);
532 writew(0, ®s
->RDIdx
);
533 writew(vptr
->options
.numrx
- 1, ®s
->RDCSize
);
537 * velocity_init_registers - initialise MAC registers
538 * @vptr: velocity to init
539 * @type: type of initialisation (hot or cold)
541 * Initialise the MAC on a reset or on first set up on the
545 static void velocity_init_registers(struct velocity_info
*vptr
,
546 enum velocity_init_type type
)
548 struct mac_regs __iomem
* regs
= vptr
->mac_regs
;
554 case VELOCITY_INIT_RESET
:
555 case VELOCITY_INIT_WOL
:
557 netif_stop_queue(vptr
->dev
);
560 * Reset RX to prevent RX pointer not on the 4X location
562 velocity_rx_reset(vptr
);
563 mac_rx_queue_run(regs
);
564 mac_rx_queue_wake(regs
);
566 mii_status
= velocity_get_opt_media_mode(vptr
);
567 if (velocity_set_media_mode(vptr
, mii_status
) != VELOCITY_LINK_CHANGE
) {
568 velocity_print_link_status(vptr
);
569 if (!(vptr
->mii_status
& VELOCITY_LINK_FAIL
))
570 netif_wake_queue(vptr
->dev
);
573 enable_flow_control_ability(vptr
);
576 writel(CR0_STOP
, ®s
->CR0Clr
);
577 writel((CR0_DPOLL
| CR0_TXON
| CR0_RXON
| CR0_STRT
),
582 case VELOCITY_INIT_COLD
:
587 velocity_soft_reset(vptr
);
590 mac_eeprom_reload(regs
);
591 for (i
= 0; i
< 6; i
++) {
592 writeb(vptr
->dev
->dev_addr
[i
], &(regs
->PAR
[i
]));
595 * clear Pre_ACPI bit.
597 BYTE_REG_BITS_OFF(CFGA_PACPI
, &(regs
->CFGA
));
598 mac_set_rx_thresh(regs
, vptr
->options
.rx_thresh
);
599 mac_set_dma_length(regs
, vptr
->options
.DMA_length
);
601 writeb(WOLCFG_SAM
| WOLCFG_SAB
, ®s
->WOLCFGSet
);
603 * Back off algorithm use original IEEE standard
605 BYTE_REG_BITS_SET(CFGB_OFSET
, (CFGB_CRANDOM
| CFGB_CAP
| CFGB_MBA
| CFGB_BAKOPT
), ®s
->CFGB
);
610 velocity_init_cam_filter(vptr
);
613 * Set packet filter: Receive directed and broadcast address
615 velocity_set_multi(vptr
->dev
);
618 * Enable MII auto-polling
620 enable_mii_autopoll(regs
);
622 vptr
->int_mask
= INT_MASK_DEF
;
624 writel(cpu_to_le32(vptr
->rd_pool_dma
), ®s
->RDBaseLo
);
625 writew(vptr
->options
.numrx
- 1, ®s
->RDCSize
);
626 mac_rx_queue_run(regs
);
627 mac_rx_queue_wake(regs
);
629 writew(vptr
->options
.numtx
- 1, ®s
->TDCSize
);
631 for (i
= 0; i
< vptr
->num_txq
; i
++) {
632 writel(cpu_to_le32(vptr
->td_pool_dma
[i
]), &(regs
->TDBaseLo
[i
]));
633 mac_tx_queue_run(regs
, i
);
636 init_flow_control_register(vptr
);
638 writel(CR0_STOP
, ®s
->CR0Clr
);
639 writel((CR0_DPOLL
| CR0_TXON
| CR0_RXON
| CR0_STRT
), ®s
->CR0Set
);
641 mii_status
= velocity_get_opt_media_mode(vptr
);
642 netif_stop_queue(vptr
->dev
);
644 mii_init(vptr
, mii_status
);
646 if (velocity_set_media_mode(vptr
, mii_status
) != VELOCITY_LINK_CHANGE
) {
647 velocity_print_link_status(vptr
);
648 if (!(vptr
->mii_status
& VELOCITY_LINK_FAIL
))
649 netif_wake_queue(vptr
->dev
);
652 enable_flow_control_ability(vptr
);
653 mac_hw_mibs_init(regs
);
654 mac_write_int_mask(vptr
->int_mask
, regs
);
661 * velocity_soft_reset - soft reset
662 * @vptr: velocity to reset
664 * Kick off a soft reset of the velocity adapter and then poll
665 * until the reset sequence has completed before returning.
668 static int velocity_soft_reset(struct velocity_info
*vptr
)
670 struct mac_regs __iomem
* regs
= vptr
->mac_regs
;
673 writel(CR0_SFRST
, ®s
->CR0Set
);
675 for (i
= 0; i
< W_MAX_TIMEOUT
; i
++) {
677 if (!DWORD_REG_BITS_IS_ON(CR0_SFRST
, ®s
->CR0Set
))
681 if (i
== W_MAX_TIMEOUT
) {
682 writel(CR0_FORSRST
, ®s
->CR0Set
);
683 /* FIXME: PCI POSTING */
691 * velocity_found1 - set up discovered velocity card
693 * @ent: PCI device table entry that matched
695 * Configure a discovered adapter from scratch. Return a negative
696 * errno error code on failure paths.
699 static int __devinit
velocity_found1(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
701 static int first
= 1;
702 struct net_device
*dev
;
704 const struct velocity_info_tbl
*info
= &chip_info_table
[ent
->driver_data
];
705 struct velocity_info
*vptr
;
706 struct mac_regs __iomem
* regs
;
709 /* FIXME: this driver, like almost all other ethernet drivers,
710 * can support more than MAX_UNITS.
712 if (velocity_nics
>= MAX_UNITS
) {
713 dev_notice(&pdev
->dev
, "already found %d NICs.\n",
718 dev
= alloc_etherdev(sizeof(struct velocity_info
));
720 dev_err(&pdev
->dev
, "allocate net device failed.\n");
724 /* Chain it all together */
726 SET_NETDEV_DEV(dev
, &pdev
->dev
);
727 vptr
= netdev_priv(dev
);
731 printk(KERN_INFO
"%s Ver. %s\n",
732 VELOCITY_FULL_DRV_NAM
, VELOCITY_VERSION
);
733 printk(KERN_INFO
"Copyright (c) 2002, 2003 VIA Networking Technologies, Inc.\n");
734 printk(KERN_INFO
"Copyright (c) 2004 Red Hat Inc.\n");
738 velocity_init_info(pdev
, vptr
, info
);
742 dev
->irq
= pdev
->irq
;
744 ret
= pci_enable_device(pdev
);
748 ret
= velocity_get_pci_info(vptr
, pdev
);
750 /* error message already printed */
754 ret
= pci_request_regions(pdev
, VELOCITY_NAME
);
756 dev_err(&pdev
->dev
, "No PCI resources.\n");
760 regs
= ioremap(vptr
->memaddr
, VELOCITY_IO_SIZE
);
763 goto err_release_res
;
766 vptr
->mac_regs
= regs
;
770 dev
->base_addr
= vptr
->ioaddr
;
772 for (i
= 0; i
< 6; i
++)
773 dev
->dev_addr
[i
] = readb(®s
->PAR
[i
]);
776 velocity_get_options(&vptr
->options
, velocity_nics
, dev
->name
);
779 * Mask out the options cannot be set to the chip
782 vptr
->options
.flags
&= info
->flags
;
785 * Enable the chip specified capbilities
788 vptr
->flags
= vptr
->options
.flags
| (info
->flags
& 0xFF000000UL
);
790 vptr
->wol_opts
= vptr
->options
.wol_opts
;
791 vptr
->flags
|= VELOCITY_FLAGS_WOL_ENABLED
;
793 vptr
->phy_id
= MII_GET_PHY_ID(vptr
->mac_regs
);
795 dev
->irq
= pdev
->irq
;
796 dev
->open
= velocity_open
;
797 dev
->hard_start_xmit
= velocity_xmit
;
798 dev
->stop
= velocity_close
;
799 dev
->get_stats
= velocity_get_stats
;
800 dev
->set_multicast_list
= velocity_set_multi
;
801 dev
->do_ioctl
= velocity_ioctl
;
802 dev
->ethtool_ops
= &velocity_ethtool_ops
;
803 dev
->change_mtu
= velocity_change_mtu
;
805 dev
->vlan_rx_add_vid
= velocity_vlan_rx_add_vid
;
806 dev
->vlan_rx_kill_vid
= velocity_vlan_rx_kill_vid
;
808 #ifdef VELOCITY_ZERO_COPY_SUPPORT
809 dev
->features
|= NETIF_F_SG
;
811 dev
->features
|= NETIF_F_HW_VLAN_TX
| NETIF_F_HW_VLAN_FILTER
;
813 if (vptr
->flags
& VELOCITY_FLAGS_TX_CSUM
)
814 dev
->features
|= NETIF_F_IP_CSUM
;
816 ret
= register_netdev(dev
);
820 if (velocity_get_link(dev
))
821 netif_carrier_off(dev
);
823 velocity_print_info(vptr
);
824 pci_set_drvdata(pdev
, dev
);
826 /* and leave the chip powered down */
828 pci_set_power_state(pdev
, PCI_D3hot
);
833 spin_lock_irqsave(&velocity_dev_list_lock
, flags
);
834 list_add(&vptr
->list
, &velocity_dev_list
);
835 spin_unlock_irqrestore(&velocity_dev_list_lock
, flags
);
845 pci_release_regions(pdev
);
847 pci_disable_device(pdev
);
854 * velocity_print_info - per driver data
857 * Print per driver data as the kernel driver finds Velocity
861 static void __devinit
velocity_print_info(struct velocity_info
*vptr
)
863 struct net_device
*dev
= vptr
->dev
;
865 printk(KERN_INFO
"%s: %s\n", dev
->name
, get_chip_name(vptr
->chip_id
));
866 printk(KERN_INFO
"%s: Ethernet Address: %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n",
868 dev
->dev_addr
[0], dev
->dev_addr
[1], dev
->dev_addr
[2],
869 dev
->dev_addr
[3], dev
->dev_addr
[4], dev
->dev_addr
[5]);
873 * velocity_init_info - init private data
875 * @vptr: Velocity info
878 * Set up the initial velocity_info struct for the device that has been
882 static void __devinit
velocity_init_info(struct pci_dev
*pdev
,
883 struct velocity_info
*vptr
,
884 const struct velocity_info_tbl
*info
)
886 memset(vptr
, 0, sizeof(struct velocity_info
));
889 vptr
->chip_id
= info
->chip_id
;
890 vptr
->num_txq
= info
->txqueue
;
891 vptr
->multicast_limit
= MCAM_SIZE
;
892 spin_lock_init(&vptr
->lock
);
893 INIT_LIST_HEAD(&vptr
->list
);
897 * velocity_get_pci_info - retrieve PCI info for device
898 * @vptr: velocity device
899 * @pdev: PCI device it matches
901 * Retrieve the PCI configuration space data that interests us from
902 * the kernel PCI layer
905 static int __devinit
velocity_get_pci_info(struct velocity_info
*vptr
, struct pci_dev
*pdev
)
907 vptr
->rev_id
= pdev
->revision
;
909 pci_set_master(pdev
);
911 vptr
->ioaddr
= pci_resource_start(pdev
, 0);
912 vptr
->memaddr
= pci_resource_start(pdev
, 1);
914 if (!(pci_resource_flags(pdev
, 0) & IORESOURCE_IO
)) {
916 "region #0 is not an I/O resource, aborting.\n");
920 if ((pci_resource_flags(pdev
, 1) & IORESOURCE_IO
)) {
922 "region #1 is an I/O resource, aborting.\n");
926 if (pci_resource_len(pdev
, 1) < VELOCITY_IO_SIZE
) {
927 dev_err(&pdev
->dev
, "region #1 is too small.\n");
936 * velocity_init_rings - set up DMA rings
937 * @vptr: Velocity to set up
939 * Allocate PCI mapped DMA rings for the receive and transmit layer
943 static int velocity_init_rings(struct velocity_info
*vptr
)
952 * Allocate all RD/TD rings a single pool
955 psize
= vptr
->options
.numrx
* sizeof(struct rx_desc
) +
956 vptr
->options
.numtx
* sizeof(struct tx_desc
) * vptr
->num_txq
;
959 * pci_alloc_consistent() fulfills the requirement for 64 bytes
962 pool
= pci_alloc_consistent(vptr
->pdev
, psize
, &pool_dma
);
965 printk(KERN_ERR
"%s : DMA memory allocation failed.\n",
970 memset(pool
, 0, psize
);
972 vptr
->rd_ring
= (struct rx_desc
*) pool
;
974 vptr
->rd_pool_dma
= pool_dma
;
976 tsize
= vptr
->options
.numtx
* PKT_BUF_SZ
* vptr
->num_txq
;
977 vptr
->tx_bufs
= pci_alloc_consistent(vptr
->pdev
, tsize
,
980 if (vptr
->tx_bufs
== NULL
) {
981 printk(KERN_ERR
"%s: DMA memory allocation failed.\n",
983 pci_free_consistent(vptr
->pdev
, psize
, pool
, pool_dma
);
987 memset(vptr
->tx_bufs
, 0, vptr
->options
.numtx
* PKT_BUF_SZ
* vptr
->num_txq
);
989 i
= vptr
->options
.numrx
* sizeof(struct rx_desc
);
992 for (i
= 0; i
< vptr
->num_txq
; i
++) {
993 int offset
= vptr
->options
.numtx
* sizeof(struct tx_desc
);
995 vptr
->td_pool_dma
[i
] = pool_dma
;
996 vptr
->td_rings
[i
] = (struct tx_desc
*) pool
;
1004 * velocity_free_rings - free PCI ring pointers
1005 * @vptr: Velocity to free from
1007 * Clean up the PCI ring buffers allocated to this velocity.
1010 static void velocity_free_rings(struct velocity_info
*vptr
)
1014 size
= vptr
->options
.numrx
* sizeof(struct rx_desc
) +
1015 vptr
->options
.numtx
* sizeof(struct tx_desc
) * vptr
->num_txq
;
1017 pci_free_consistent(vptr
->pdev
, size
, vptr
->rd_ring
, vptr
->rd_pool_dma
);
1019 size
= vptr
->options
.numtx
* PKT_BUF_SZ
* vptr
->num_txq
;
1021 pci_free_consistent(vptr
->pdev
, size
, vptr
->tx_bufs
, vptr
->tx_bufs_dma
);
1024 static inline void velocity_give_many_rx_descs(struct velocity_info
*vptr
)
1026 struct mac_regs __iomem
*regs
= vptr
->mac_regs
;
1027 int avail
, dirty
, unusable
;
1030 * RD number must be equal to 4X per hardware spec
1031 * (programming guide rev 1.20, p.13)
1033 if (vptr
->rd_filled
< 4)
1038 unusable
= vptr
->rd_filled
& 0x0003;
1039 dirty
= vptr
->rd_dirty
- unusable
;
1040 for (avail
= vptr
->rd_filled
& 0xfffc; avail
; avail
--) {
1041 dirty
= (dirty
> 0) ? dirty
- 1 : vptr
->options
.numrx
- 1;
1042 vptr
->rd_ring
[dirty
].rdesc0
.owner
= OWNED_BY_NIC
;
1045 writew(vptr
->rd_filled
& 0xfffc, ®s
->RBRDU
);
1046 vptr
->rd_filled
= unusable
;
1049 static int velocity_rx_refill(struct velocity_info
*vptr
)
1051 int dirty
= vptr
->rd_dirty
, done
= 0, ret
= 0;
1054 struct rx_desc
*rd
= vptr
->rd_ring
+ dirty
;
1056 /* Fine for an all zero Rx desc at init time as well */
1057 if (rd
->rdesc0
.owner
== OWNED_BY_NIC
)
1060 if (!vptr
->rd_info
[dirty
].skb
) {
1061 ret
= velocity_alloc_rx_buf(vptr
, dirty
);
1066 dirty
= (dirty
< vptr
->options
.numrx
- 1) ? dirty
+ 1 : 0;
1067 } while (dirty
!= vptr
->rd_curr
);
1070 vptr
->rd_dirty
= dirty
;
1071 vptr
->rd_filled
+= done
;
1072 velocity_give_many_rx_descs(vptr
);
1079 * velocity_init_rd_ring - set up receive ring
1080 * @vptr: velocity to configure
1082 * Allocate and set up the receive buffers for each ring slot and
1083 * assign them to the network adapter.
1086 static int velocity_init_rd_ring(struct velocity_info
*vptr
)
1090 vptr
->rd_info
= kcalloc(vptr
->options
.numrx
,
1091 sizeof(struct velocity_rd_info
), GFP_KERNEL
);
1095 vptr
->rd_filled
= vptr
->rd_dirty
= vptr
->rd_curr
= 0;
1097 ret
= velocity_rx_refill(vptr
);
1099 VELOCITY_PRT(MSG_LEVEL_ERR
, KERN_ERR
1100 "%s: failed to allocate RX buffer.\n", vptr
->dev
->name
);
1101 velocity_free_rd_ring(vptr
);
1108 * velocity_free_rd_ring - free receive ring
1109 * @vptr: velocity to clean up
1111 * Free the receive buffers for each ring slot and any
1112 * attached socket buffers that need to go away.
1115 static void velocity_free_rd_ring(struct velocity_info
*vptr
)
1119 if (vptr
->rd_info
== NULL
)
1122 for (i
= 0; i
< vptr
->options
.numrx
; i
++) {
1123 struct velocity_rd_info
*rd_info
= &(vptr
->rd_info
[i
]);
1124 struct rx_desc
*rd
= vptr
->rd_ring
+ i
;
1126 memset(rd
, 0, sizeof(*rd
));
1130 pci_unmap_single(vptr
->pdev
, rd_info
->skb_dma
, vptr
->rx_buf_sz
,
1131 PCI_DMA_FROMDEVICE
);
1132 rd_info
->skb_dma
= (dma_addr_t
) NULL
;
1134 dev_kfree_skb(rd_info
->skb
);
1135 rd_info
->skb
= NULL
;
1138 kfree(vptr
->rd_info
);
1139 vptr
->rd_info
= NULL
;
1143 * velocity_init_td_ring - set up transmit ring
1146 * Set up the transmit ring and chain the ring pointers together.
1147 * Returns zero on success or a negative posix errno code for
1151 static int velocity_init_td_ring(struct velocity_info
*vptr
)
1156 struct velocity_td_info
*td_info
;
1158 /* Init the TD ring entries */
1159 for (j
= 0; j
< vptr
->num_txq
; j
++) {
1160 curr
= vptr
->td_pool_dma
[j
];
1162 vptr
->td_infos
[j
] = kcalloc(vptr
->options
.numtx
,
1163 sizeof(struct velocity_td_info
),
1165 if (!vptr
->td_infos
[j
]) {
1167 kfree(vptr
->td_infos
[j
]);
1171 for (i
= 0; i
< vptr
->options
.numtx
; i
++, curr
+= sizeof(struct tx_desc
)) {
1172 td
= &(vptr
->td_rings
[j
][i
]);
1173 td_info
= &(vptr
->td_infos
[j
][i
]);
1174 td_info
->buf
= vptr
->tx_bufs
+
1175 (j
* vptr
->options
.numtx
+ i
) * PKT_BUF_SZ
;
1176 td_info
->buf_dma
= vptr
->tx_bufs_dma
+
1177 (j
* vptr
->options
.numtx
+ i
) * PKT_BUF_SZ
;
1179 vptr
->td_tail
[j
] = vptr
->td_curr
[j
] = vptr
->td_used
[j
] = 0;
1185 * FIXME: could we merge this with velocity_free_tx_buf ?
1188 static void velocity_free_td_ring_entry(struct velocity_info
*vptr
,
1191 struct velocity_td_info
* td_info
= &(vptr
->td_infos
[q
][n
]);
1194 if (td_info
== NULL
)
1198 for (i
= 0; i
< td_info
->nskb_dma
; i
++)
1200 if (td_info
->skb_dma
[i
]) {
1201 pci_unmap_single(vptr
->pdev
, td_info
->skb_dma
[i
],
1202 td_info
->skb
->len
, PCI_DMA_TODEVICE
);
1203 td_info
->skb_dma
[i
] = (dma_addr_t
) NULL
;
1206 dev_kfree_skb(td_info
->skb
);
1207 td_info
->skb
= NULL
;
1212 * velocity_free_td_ring - free td ring
1215 * Free up the transmit ring for this particular velocity adapter.
1216 * We free the ring contents but not the ring itself.
1219 static void velocity_free_td_ring(struct velocity_info
*vptr
)
1223 for (j
= 0; j
< vptr
->num_txq
; j
++) {
1224 if (vptr
->td_infos
[j
] == NULL
)
1226 for (i
= 0; i
< vptr
->options
.numtx
; i
++) {
1227 velocity_free_td_ring_entry(vptr
, j
, i
);
1230 kfree(vptr
->td_infos
[j
]);
1231 vptr
->td_infos
[j
] = NULL
;
1236 * velocity_rx_srv - service RX interrupt
1238 * @status: adapter status (unused)
1240 * Walk the receive ring of the velocity adapter and remove
1241 * any received packets from the receive queue. Hand the ring
1242 * slots back to the adapter for reuse.
1245 static int velocity_rx_srv(struct velocity_info
*vptr
, int status
)
1247 struct net_device_stats
*stats
= &vptr
->stats
;
1248 int rd_curr
= vptr
->rd_curr
;
1252 struct rx_desc
*rd
= vptr
->rd_ring
+ rd_curr
;
1254 if (!vptr
->rd_info
[rd_curr
].skb
)
1257 if (rd
->rdesc0
.owner
== OWNED_BY_NIC
)
1263 * Don't drop CE or RL error frame although RXOK is off
1265 if ((rd
->rdesc0
.RSR
& RSR_RXOK
) || (!(rd
->rdesc0
.RSR
& RSR_RXOK
) && (rd
->rdesc0
.RSR
& (RSR_CE
| RSR_RL
)))) {
1266 if (velocity_receive_frame(vptr
, rd_curr
) < 0)
1267 stats
->rx_dropped
++;
1269 if (rd
->rdesc0
.RSR
& RSR_CRC
)
1270 stats
->rx_crc_errors
++;
1271 if (rd
->rdesc0
.RSR
& RSR_FAE
)
1272 stats
->rx_frame_errors
++;
1274 stats
->rx_dropped
++;
1279 vptr
->dev
->last_rx
= jiffies
;
1282 if (rd_curr
>= vptr
->options
.numrx
)
1284 } while (++works
<= 15);
1286 vptr
->rd_curr
= rd_curr
;
1288 if (works
> 0 && velocity_rx_refill(vptr
) < 0) {
1289 VELOCITY_PRT(MSG_LEVEL_ERR
, KERN_ERR
1290 "%s: rx buf allocation failure\n", vptr
->dev
->name
);
1298 * velocity_rx_csum - checksum process
1299 * @rd: receive packet descriptor
1300 * @skb: network layer packet buffer
1302 * Process the status bits for the received packet and determine
1303 * if the checksum was computed and verified by the hardware
1306 static inline void velocity_rx_csum(struct rx_desc
*rd
, struct sk_buff
*skb
)
1308 skb
->ip_summed
= CHECKSUM_NONE
;
1310 if (rd
->rdesc1
.CSM
& CSM_IPKT
) {
1311 if (rd
->rdesc1
.CSM
& CSM_IPOK
) {
1312 if ((rd
->rdesc1
.CSM
& CSM_TCPKT
) ||
1313 (rd
->rdesc1
.CSM
& CSM_UDPKT
)) {
1314 if (!(rd
->rdesc1
.CSM
& CSM_TUPOK
)) {
1318 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1324 * velocity_rx_copy - in place Rx copy for small packets
1325 * @rx_skb: network layer packet buffer candidate
1326 * @pkt_size: received data size
1327 * @rd: receive packet descriptor
1328 * @dev: network device
1330 * Replace the current skb that is scheduled for Rx processing by a
1331 * shorter, immediatly allocated skb, if the received packet is small
1332 * enough. This function returns a negative value if the received
1333 * packet is too big or if memory is exhausted.
1335 static inline int velocity_rx_copy(struct sk_buff
**rx_skb
, int pkt_size
,
1336 struct velocity_info
*vptr
)
1340 if (pkt_size
< rx_copybreak
) {
1341 struct sk_buff
*new_skb
;
1343 new_skb
= dev_alloc_skb(pkt_size
+ 2);
1345 new_skb
->dev
= vptr
->dev
;
1346 new_skb
->ip_summed
= rx_skb
[0]->ip_summed
;
1348 if (vptr
->flags
& VELOCITY_FLAGS_IP_ALIGN
)
1349 skb_reserve(new_skb
, 2);
1351 skb_copy_from_linear_data(rx_skb
[0], new_skb
->data
,
1362 * velocity_iph_realign - IP header alignment
1363 * @vptr: velocity we are handling
1364 * @skb: network layer packet buffer
1365 * @pkt_size: received data size
1367 * Align IP header on a 2 bytes boundary. This behavior can be
1368 * configured by the user.
1370 static inline void velocity_iph_realign(struct velocity_info
*vptr
,
1371 struct sk_buff
*skb
, int pkt_size
)
1373 /* FIXME - memmove ? */
1374 if (vptr
->flags
& VELOCITY_FLAGS_IP_ALIGN
) {
1377 for (i
= pkt_size
; i
>= 0; i
--)
1378 *(skb
->data
+ i
+ 2) = *(skb
->data
+ i
);
1379 skb_reserve(skb
, 2);
1384 * velocity_receive_frame - received packet processor
1385 * @vptr: velocity we are handling
1388 * A packet has arrived. We process the packet and if appropriate
1389 * pass the frame up the network stack
1392 static int velocity_receive_frame(struct velocity_info
*vptr
, int idx
)
1394 void (*pci_action
)(struct pci_dev
*, dma_addr_t
, size_t, int);
1395 struct net_device_stats
*stats
= &vptr
->stats
;
1396 struct velocity_rd_info
*rd_info
= &(vptr
->rd_info
[idx
]);
1397 struct rx_desc
*rd
= &(vptr
->rd_ring
[idx
]);
1398 int pkt_len
= rd
->rdesc0
.len
;
1399 struct sk_buff
*skb
;
1401 if (rd
->rdesc0
.RSR
& (RSR_STP
| RSR_EDP
)) {
1402 VELOCITY_PRT(MSG_LEVEL_VERBOSE
, KERN_ERR
" %s : the received frame span multple RDs.\n", vptr
->dev
->name
);
1403 stats
->rx_length_errors
++;
1407 if (rd
->rdesc0
.RSR
& RSR_MAR
)
1408 vptr
->stats
.multicast
++;
1412 pci_dma_sync_single_for_cpu(vptr
->pdev
, rd_info
->skb_dma
,
1413 vptr
->rx_buf_sz
, PCI_DMA_FROMDEVICE
);
1416 * Drop frame not meeting IEEE 802.3
1419 if (vptr
->flags
& VELOCITY_FLAGS_VAL_PKT_LEN
) {
1420 if (rd
->rdesc0
.RSR
& RSR_RL
) {
1421 stats
->rx_length_errors
++;
1426 pci_action
= pci_dma_sync_single_for_device
;
1428 velocity_rx_csum(rd
, skb
);
1430 if (velocity_rx_copy(&skb
, pkt_len
, vptr
) < 0) {
1431 velocity_iph_realign(vptr
, skb
, pkt_len
);
1432 pci_action
= pci_unmap_single
;
1433 rd_info
->skb
= NULL
;
1436 pci_action(vptr
->pdev
, rd_info
->skb_dma
, vptr
->rx_buf_sz
,
1437 PCI_DMA_FROMDEVICE
);
1439 skb_put(skb
, pkt_len
- 4);
1440 skb
->protocol
= eth_type_trans(skb
, vptr
->dev
);
1442 stats
->rx_bytes
+= pkt_len
;
1449 * velocity_alloc_rx_buf - allocate aligned receive buffer
1453 * Allocate a new full sized buffer for the reception of a frame and
1454 * map it into PCI space for the hardware to use. The hardware
1455 * requires *64* byte alignment of the buffer which makes life
1456 * less fun than would be ideal.
1459 static int velocity_alloc_rx_buf(struct velocity_info
*vptr
, int idx
)
1461 struct rx_desc
*rd
= &(vptr
->rd_ring
[idx
]);
1462 struct velocity_rd_info
*rd_info
= &(vptr
->rd_info
[idx
]);
1464 rd_info
->skb
= dev_alloc_skb(vptr
->rx_buf_sz
+ 64);
1465 if (rd_info
->skb
== NULL
)
1469 * Do the gymnastics to get the buffer head for data at
1472 skb_reserve(rd_info
->skb
, (unsigned long) rd_info
->skb
->data
& 63);
1473 rd_info
->skb
->dev
= vptr
->dev
;
1474 rd_info
->skb_dma
= pci_map_single(vptr
->pdev
, rd_info
->skb
->data
, vptr
->rx_buf_sz
, PCI_DMA_FROMDEVICE
);
1477 * Fill in the descriptor to match
1480 *((u32
*) & (rd
->rdesc0
)) = 0;
1481 rd
->len
= cpu_to_le32(vptr
->rx_buf_sz
);
1483 rd
->pa_low
= cpu_to_le32(rd_info
->skb_dma
);
1489 * tx_srv - transmit interrupt service
1493 * Scan the queues looking for transmitted packets that
1494 * we can complete and clean up. Update any statistics as
1498 static int velocity_tx_srv(struct velocity_info
*vptr
, u32 status
)
1505 struct velocity_td_info
*tdinfo
;
1506 struct net_device_stats
*stats
= &vptr
->stats
;
1508 for (qnum
= 0; qnum
< vptr
->num_txq
; qnum
++) {
1509 for (idx
= vptr
->td_tail
[qnum
]; vptr
->td_used
[qnum
] > 0;
1510 idx
= (idx
+ 1) % vptr
->options
.numtx
) {
1515 td
= &(vptr
->td_rings
[qnum
][idx
]);
1516 tdinfo
= &(vptr
->td_infos
[qnum
][idx
]);
1518 if (td
->tdesc0
.owner
== OWNED_BY_NIC
)
1524 if (td
->tdesc0
.TSR
& TSR0_TERR
) {
1526 stats
->tx_dropped
++;
1527 if (td
->tdesc0
.TSR
& TSR0_CDH
)
1528 stats
->tx_heartbeat_errors
++;
1529 if (td
->tdesc0
.TSR
& TSR0_CRS
)
1530 stats
->tx_carrier_errors
++;
1531 if (td
->tdesc0
.TSR
& TSR0_ABT
)
1532 stats
->tx_aborted_errors
++;
1533 if (td
->tdesc0
.TSR
& TSR0_OWC
)
1534 stats
->tx_window_errors
++;
1536 stats
->tx_packets
++;
1537 stats
->tx_bytes
+= tdinfo
->skb
->len
;
1539 velocity_free_tx_buf(vptr
, tdinfo
);
1540 vptr
->td_used
[qnum
]--;
1542 vptr
->td_tail
[qnum
] = idx
;
1544 if (AVAIL_TD(vptr
, qnum
) < 1) {
1549 * Look to see if we should kick the transmit network
1550 * layer for more work.
1552 if (netif_queue_stopped(vptr
->dev
) && (full
== 0)
1553 && (!(vptr
->mii_status
& VELOCITY_LINK_FAIL
))) {
1554 netif_wake_queue(vptr
->dev
);
1560 * velocity_print_link_status - link status reporting
1561 * @vptr: velocity to report on
1563 * Turn the link status of the velocity card into a kernel log
1564 * description of the new link state, detailing speed and duplex
1568 static void velocity_print_link_status(struct velocity_info
*vptr
)
1571 if (vptr
->mii_status
& VELOCITY_LINK_FAIL
) {
1572 VELOCITY_PRT(MSG_LEVEL_INFO
, KERN_NOTICE
"%s: failed to detect cable link\n", vptr
->dev
->name
);
1573 } else if (vptr
->options
.spd_dpx
== SPD_DPX_AUTO
) {
1574 VELOCITY_PRT(MSG_LEVEL_INFO
, KERN_NOTICE
"%s: Link auto-negotiation", vptr
->dev
->name
);
1576 if (vptr
->mii_status
& VELOCITY_SPEED_1000
)
1577 VELOCITY_PRT(MSG_LEVEL_INFO
, " speed 1000M bps");
1578 else if (vptr
->mii_status
& VELOCITY_SPEED_100
)
1579 VELOCITY_PRT(MSG_LEVEL_INFO
, " speed 100M bps");
1581 VELOCITY_PRT(MSG_LEVEL_INFO
, " speed 10M bps");
1583 if (vptr
->mii_status
& VELOCITY_DUPLEX_FULL
)
1584 VELOCITY_PRT(MSG_LEVEL_INFO
, " full duplex\n");
1586 VELOCITY_PRT(MSG_LEVEL_INFO
, " half duplex\n");
1588 VELOCITY_PRT(MSG_LEVEL_INFO
, KERN_NOTICE
"%s: Link forced", vptr
->dev
->name
);
1589 switch (vptr
->options
.spd_dpx
) {
1590 case SPD_DPX_100_HALF
:
1591 VELOCITY_PRT(MSG_LEVEL_INFO
, " speed 100M bps half duplex\n");
1593 case SPD_DPX_100_FULL
:
1594 VELOCITY_PRT(MSG_LEVEL_INFO
, " speed 100M bps full duplex\n");
1596 case SPD_DPX_10_HALF
:
1597 VELOCITY_PRT(MSG_LEVEL_INFO
, " speed 10M bps half duplex\n");
1599 case SPD_DPX_10_FULL
:
1600 VELOCITY_PRT(MSG_LEVEL_INFO
, " speed 10M bps full duplex\n");
1609 * velocity_error - handle error from controller
1611 * @status: card status
1613 * Process an error report from the hardware and attempt to recover
1614 * the card itself. At the moment we cannot recover from some
1615 * theoretically impossible errors but this could be fixed using
1616 * the pci_device_failed logic to bounce the hardware
1620 static void velocity_error(struct velocity_info
*vptr
, int status
)
1623 if (status
& ISR_TXSTLI
) {
1624 struct mac_regs __iomem
* regs
= vptr
->mac_regs
;
1626 printk(KERN_ERR
"TD structure error TDindex=%hx\n", readw(®s
->TDIdx
[0]));
1627 BYTE_REG_BITS_ON(TXESR_TDSTR
, ®s
->TXESR
);
1628 writew(TRDCSR_RUN
, ®s
->TDCSRClr
);
1629 netif_stop_queue(vptr
->dev
);
1631 /* FIXME: port over the pci_device_failed code and use it
1635 if (status
& ISR_SRCI
) {
1636 struct mac_regs __iomem
* regs
= vptr
->mac_regs
;
1639 if (vptr
->options
.spd_dpx
== SPD_DPX_AUTO
) {
1640 vptr
->mii_status
= check_connection_type(regs
);
1643 * If it is a 3119, disable frame bursting in
1644 * halfduplex mode and enable it in fullduplex
1647 if (vptr
->rev_id
< REV_ID_VT3216_A0
) {
1648 if (vptr
->mii_status
| VELOCITY_DUPLEX_FULL
)
1649 BYTE_REG_BITS_ON(TCR_TB2BDIS
, ®s
->TCR
);
1651 BYTE_REG_BITS_OFF(TCR_TB2BDIS
, ®s
->TCR
);
1654 * Only enable CD heart beat counter in 10HD mode
1656 if (!(vptr
->mii_status
& VELOCITY_DUPLEX_FULL
) && (vptr
->mii_status
& VELOCITY_SPEED_10
)) {
1657 BYTE_REG_BITS_OFF(TESTCFG_HBDIS
, ®s
->TESTCFG
);
1659 BYTE_REG_BITS_ON(TESTCFG_HBDIS
, ®s
->TESTCFG
);
1663 * Get link status from PHYSR0
1665 linked
= readb(®s
->PHYSR0
) & PHYSR0_LINKGD
;
1668 vptr
->mii_status
&= ~VELOCITY_LINK_FAIL
;
1669 netif_carrier_on(vptr
->dev
);
1671 vptr
->mii_status
|= VELOCITY_LINK_FAIL
;
1672 netif_carrier_off(vptr
->dev
);
1675 velocity_print_link_status(vptr
);
1676 enable_flow_control_ability(vptr
);
1679 * Re-enable auto-polling because SRCI will disable
1683 enable_mii_autopoll(regs
);
1685 if (vptr
->mii_status
& VELOCITY_LINK_FAIL
)
1686 netif_stop_queue(vptr
->dev
);
1688 netif_wake_queue(vptr
->dev
);
1691 if (status
& ISR_MIBFI
)
1692 velocity_update_hw_mibs(vptr
);
1693 if (status
& ISR_LSTEI
)
1694 mac_rx_queue_wake(vptr
->mac_regs
);
1698 * velocity_free_tx_buf - free transmit buffer
1702 * Release an transmit buffer. If the buffer was preallocated then
1703 * recycle it, if not then unmap the buffer.
1706 static void velocity_free_tx_buf(struct velocity_info
*vptr
, struct velocity_td_info
*tdinfo
)
1708 struct sk_buff
*skb
= tdinfo
->skb
;
1712 * Don't unmap the pre-allocated tx_bufs
1714 if (tdinfo
->skb_dma
&& (tdinfo
->skb_dma
[0] != tdinfo
->buf_dma
)) {
1716 for (i
= 0; i
< tdinfo
->nskb_dma
; i
++) {
1717 #ifdef VELOCITY_ZERO_COPY_SUPPORT
1718 pci_unmap_single(vptr
->pdev
, tdinfo
->skb_dma
[i
], td
->tdesc1
.len
, PCI_DMA_TODEVICE
);
1720 pci_unmap_single(vptr
->pdev
, tdinfo
->skb_dma
[i
], skb
->len
, PCI_DMA_TODEVICE
);
1722 tdinfo
->skb_dma
[i
] = 0;
1725 dev_kfree_skb_irq(skb
);
1730 * velocity_open - interface activation callback
1731 * @dev: network layer device to open
1733 * Called when the network layer brings the interface up. Returns
1734 * a negative posix error code on failure, or zero on success.
1736 * All the ring allocation and set up is done on open for this
1737 * adapter to minimise memory usage when inactive
1740 static int velocity_open(struct net_device
*dev
)
1742 struct velocity_info
*vptr
= netdev_priv(dev
);
1745 vptr
->rx_buf_sz
= (dev
->mtu
<= 1504 ? PKT_BUF_SZ
: dev
->mtu
+ 32);
1747 ret
= velocity_init_rings(vptr
);
1751 ret
= velocity_init_rd_ring(vptr
);
1753 goto err_free_desc_rings
;
1755 ret
= velocity_init_td_ring(vptr
);
1757 goto err_free_rd_ring
;
1759 /* Ensure chip is running */
1760 pci_set_power_state(vptr
->pdev
, PCI_D0
);
1762 velocity_init_registers(vptr
, VELOCITY_INIT_COLD
);
1764 ret
= request_irq(vptr
->pdev
->irq
, &velocity_intr
, IRQF_SHARED
,
1767 /* Power down the chip */
1768 pci_set_power_state(vptr
->pdev
, PCI_D3hot
);
1769 goto err_free_td_ring
;
1772 mac_enable_int(vptr
->mac_regs
);
1773 netif_start_queue(dev
);
1774 vptr
->flags
|= VELOCITY_FLAGS_OPENED
;
1779 velocity_free_td_ring(vptr
);
1781 velocity_free_rd_ring(vptr
);
1782 err_free_desc_rings
:
1783 velocity_free_rings(vptr
);
1788 * velocity_change_mtu - MTU change callback
1789 * @dev: network device
1790 * @new_mtu: desired MTU
1792 * Handle requests from the networking layer for MTU change on
1793 * this interface. It gets called on a change by the network layer.
1794 * Return zero for success or negative posix error code.
1797 static int velocity_change_mtu(struct net_device
*dev
, int new_mtu
)
1799 struct velocity_info
*vptr
= netdev_priv(dev
);
1800 unsigned long flags
;
1801 int oldmtu
= dev
->mtu
;
1804 if ((new_mtu
< VELOCITY_MIN_MTU
) || new_mtu
> (VELOCITY_MAX_MTU
)) {
1805 VELOCITY_PRT(MSG_LEVEL_ERR
, KERN_NOTICE
"%s: Invalid MTU.\n",
1810 if (new_mtu
!= oldmtu
) {
1811 spin_lock_irqsave(&vptr
->lock
, flags
);
1813 netif_stop_queue(dev
);
1814 velocity_shutdown(vptr
);
1816 velocity_free_td_ring(vptr
);
1817 velocity_free_rd_ring(vptr
);
1821 vptr
->rx_buf_sz
= 9 * 1024;
1822 else if (new_mtu
> 4096)
1823 vptr
->rx_buf_sz
= 8192;
1825 vptr
->rx_buf_sz
= 4 * 1024;
1827 ret
= velocity_init_rd_ring(vptr
);
1831 ret
= velocity_init_td_ring(vptr
);
1835 velocity_init_registers(vptr
, VELOCITY_INIT_COLD
);
1837 mac_enable_int(vptr
->mac_regs
);
1838 netif_start_queue(dev
);
1840 spin_unlock_irqrestore(&vptr
->lock
, flags
);
1847 * velocity_shutdown - shut down the chip
1848 * @vptr: velocity to deactivate
1850 * Shuts down the internal operations of the velocity and
1851 * disables interrupts, autopolling, transmit and receive
1854 static void velocity_shutdown(struct velocity_info
*vptr
)
1856 struct mac_regs __iomem
* regs
= vptr
->mac_regs
;
1857 mac_disable_int(regs
);
1858 writel(CR0_STOP
, ®s
->CR0Set
);
1859 writew(0xFFFF, ®s
->TDCSRClr
);
1860 writeb(0xFF, ®s
->RDCSRClr
);
1861 safe_disable_mii_autopoll(regs
);
1862 mac_clear_isr(regs
);
1866 * velocity_close - close adapter callback
1867 * @dev: network device
1869 * Callback from the network layer when the velocity is being
1870 * deactivated by the network layer
1873 static int velocity_close(struct net_device
*dev
)
1875 struct velocity_info
*vptr
= netdev_priv(dev
);
1877 netif_stop_queue(dev
);
1878 velocity_shutdown(vptr
);
1880 if (vptr
->flags
& VELOCITY_FLAGS_WOL_ENABLED
)
1881 velocity_get_ip(vptr
);
1883 free_irq(dev
->irq
, dev
);
1885 /* Power down the chip */
1886 pci_set_power_state(vptr
->pdev
, PCI_D3hot
);
1888 /* Free the resources */
1889 velocity_free_td_ring(vptr
);
1890 velocity_free_rd_ring(vptr
);
1891 velocity_free_rings(vptr
);
1893 vptr
->flags
&= (~VELOCITY_FLAGS_OPENED
);
1898 * velocity_xmit - transmit packet callback
1899 * @skb: buffer to transmit
1900 * @dev: network device
1902 * Called by the networ layer to request a packet is queued to
1903 * the velocity. Returns zero on success.
1906 static int velocity_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1908 struct velocity_info
*vptr
= netdev_priv(dev
);
1910 struct tx_desc
*td_ptr
;
1911 struct velocity_td_info
*tdinfo
;
1912 unsigned long flags
;
1915 int pktlen
= skb
->len
;
1917 #ifdef VELOCITY_ZERO_COPY_SUPPORT
1918 if (skb_shinfo(skb
)->nr_frags
> 6 && __skb_linearize(skb
)) {
1924 spin_lock_irqsave(&vptr
->lock
, flags
);
1926 index
= vptr
->td_curr
[qnum
];
1927 td_ptr
= &(vptr
->td_rings
[qnum
][index
]);
1928 tdinfo
= &(vptr
->td_infos
[qnum
][index
]);
1930 td_ptr
->tdesc1
.TCPLS
= TCPLS_NORMAL
;
1931 td_ptr
->tdesc1
.TCR
= TCR0_TIC
;
1932 td_ptr
->td_buf
[0].queue
= 0;
1937 if (pktlen
< ETH_ZLEN
) {
1938 /* Cannot occur until ZC support */
1940 skb_copy_from_linear_data(skb
, tdinfo
->buf
, skb
->len
);
1941 memset(tdinfo
->buf
+ skb
->len
, 0, ETH_ZLEN
- skb
->len
);
1943 tdinfo
->skb_dma
[0] = tdinfo
->buf_dma
;
1944 td_ptr
->tdesc0
.pktsize
= pktlen
;
1945 td_ptr
->td_buf
[0].pa_low
= cpu_to_le32(tdinfo
->skb_dma
[0]);
1946 td_ptr
->td_buf
[0].pa_high
= 0;
1947 td_ptr
->td_buf
[0].bufsize
= td_ptr
->tdesc0
.pktsize
;
1948 tdinfo
->nskb_dma
= 1;
1949 td_ptr
->tdesc1
.CMDZ
= 2;
1951 #ifdef VELOCITY_ZERO_COPY_SUPPORT
1952 if (skb_shinfo(skb
)->nr_frags
> 0) {
1953 int nfrags
= skb_shinfo(skb
)->nr_frags
;
1956 skb_copy_from_linear_data(skb
, tdinfo
->buf
, skb
->len
);
1957 tdinfo
->skb_dma
[0] = tdinfo
->buf_dma
;
1958 td_ptr
->tdesc0
.pktsize
=
1959 td_ptr
->td_buf
[0].pa_low
= cpu_to_le32(tdinfo
->skb_dma
[0]);
1960 td_ptr
->td_buf
[0].pa_high
= 0;
1961 td_ptr
->td_buf
[0].bufsize
= td_ptr
->tdesc0
.pktsize
;
1962 tdinfo
->nskb_dma
= 1;
1963 td_ptr
->tdesc1
.CMDZ
= 2;
1966 tdinfo
->nskb_dma
= 0;
1967 tdinfo
->skb_dma
[i
] = pci_map_single(vptr
->pdev
, skb
->data
, skb
->len
- skb
->data_len
, PCI_DMA_TODEVICE
);
1969 td_ptr
->tdesc0
.pktsize
= pktlen
;
1971 /* FIXME: support 48bit DMA later */
1972 td_ptr
->td_buf
[i
].pa_low
= cpu_to_le32(tdinfo
->skb_dma
);
1973 td_ptr
->td_buf
[i
].pa_high
= 0;
1974 td_ptr
->td_buf
[i
].bufsize
= skb
->len
->skb
->data_len
;
1976 for (i
= 0; i
< nfrags
; i
++) {
1977 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1978 void *addr
= ((void *) page_address(frag
->page
+ frag
->page_offset
));
1980 tdinfo
->skb_dma
[i
+ 1] = pci_map_single(vptr
->pdev
, addr
, frag
->size
, PCI_DMA_TODEVICE
);
1982 td_ptr
->td_buf
[i
+ 1].pa_low
= cpu_to_le32(tdinfo
->skb_dma
[i
+ 1]);
1983 td_ptr
->td_buf
[i
+ 1].pa_high
= 0;
1984 td_ptr
->td_buf
[i
+ 1].bufsize
= frag
->size
;
1986 tdinfo
->nskb_dma
= i
- 1;
1987 td_ptr
->tdesc1
.CMDZ
= i
;
1994 * Map the linear network buffer into PCI space and
1995 * add it to the transmit ring.
1998 tdinfo
->skb_dma
[0] = pci_map_single(vptr
->pdev
, skb
->data
, pktlen
, PCI_DMA_TODEVICE
);
1999 td_ptr
->tdesc0
.pktsize
= pktlen
;
2000 td_ptr
->td_buf
[0].pa_low
= cpu_to_le32(tdinfo
->skb_dma
[0]);
2001 td_ptr
->td_buf
[0].pa_high
= 0;
2002 td_ptr
->td_buf
[0].bufsize
= td_ptr
->tdesc0
.pktsize
;
2003 tdinfo
->nskb_dma
= 1;
2004 td_ptr
->tdesc1
.CMDZ
= 2;
2007 if (vptr
->vlgrp
&& vlan_tx_tag_present(skb
)) {
2008 td_ptr
->tdesc1
.pqinf
.VID
= vlan_tx_tag_get(skb
);
2009 td_ptr
->tdesc1
.pqinf
.priority
= 0;
2010 td_ptr
->tdesc1
.pqinf
.CFI
= 0;
2011 td_ptr
->tdesc1
.TCR
|= TCR0_VETAG
;
2015 * Handle hardware checksum
2017 if ((vptr
->flags
& VELOCITY_FLAGS_TX_CSUM
)
2018 && (skb
->ip_summed
== CHECKSUM_PARTIAL
)) {
2019 const struct iphdr
*ip
= ip_hdr(skb
);
2020 if (ip
->protocol
== IPPROTO_TCP
)
2021 td_ptr
->tdesc1
.TCR
|= TCR0_TCPCK
;
2022 else if (ip
->protocol
== IPPROTO_UDP
)
2023 td_ptr
->tdesc1
.TCR
|= (TCR0_UDPCK
);
2024 td_ptr
->tdesc1
.TCR
|= TCR0_IPCK
;
2028 int prev
= index
- 1;
2031 prev
= vptr
->options
.numtx
- 1;
2032 td_ptr
->tdesc0
.owner
= OWNED_BY_NIC
;
2033 vptr
->td_used
[qnum
]++;
2034 vptr
->td_curr
[qnum
] = (index
+ 1) % vptr
->options
.numtx
;
2036 if (AVAIL_TD(vptr
, qnum
) < 1)
2037 netif_stop_queue(dev
);
2039 td_ptr
= &(vptr
->td_rings
[qnum
][prev
]);
2040 td_ptr
->td_buf
[0].queue
= 1;
2041 mac_tx_queue_wake(vptr
->mac_regs
, qnum
);
2043 dev
->trans_start
= jiffies
;
2044 spin_unlock_irqrestore(&vptr
->lock
, flags
);
2049 * velocity_intr - interrupt callback
2050 * @irq: interrupt number
2051 * @dev_instance: interrupting device
2053 * Called whenever an interrupt is generated by the velocity
2054 * adapter IRQ line. We may not be the source of the interrupt
2055 * and need to identify initially if we are, and if not exit as
2056 * efficiently as possible.
2059 static int velocity_intr(int irq
, void *dev_instance
)
2061 struct net_device
*dev
= dev_instance
;
2062 struct velocity_info
*vptr
= netdev_priv(dev
);
2067 spin_lock(&vptr
->lock
);
2068 isr_status
= mac_read_isr(vptr
->mac_regs
);
2071 if (isr_status
== 0) {
2072 spin_unlock(&vptr
->lock
);
2076 mac_disable_int(vptr
->mac_regs
);
2079 * Keep processing the ISR until we have completed
2080 * processing and the isr_status becomes zero
2083 while (isr_status
!= 0) {
2084 mac_write_isr(vptr
->mac_regs
, isr_status
);
2085 if (isr_status
& (~(ISR_PRXI
| ISR_PPRXI
| ISR_PTXI
| ISR_PPTXI
)))
2086 velocity_error(vptr
, isr_status
);
2087 if (isr_status
& (ISR_PRXI
| ISR_PPRXI
))
2088 max_count
+= velocity_rx_srv(vptr
, isr_status
);
2089 if (isr_status
& (ISR_PTXI
| ISR_PPTXI
))
2090 max_count
+= velocity_tx_srv(vptr
, isr_status
);
2091 isr_status
= mac_read_isr(vptr
->mac_regs
);
2092 if (max_count
> vptr
->options
.int_works
)
2094 printk(KERN_WARNING
"%s: excessive work at interrupt.\n",
2099 spin_unlock(&vptr
->lock
);
2100 mac_enable_int(vptr
->mac_regs
);
2107 * velocity_set_multi - filter list change callback
2108 * @dev: network device
2110 * Called by the network layer when the filter lists need to change
2111 * for a velocity adapter. Reload the CAMs with the new address
2115 static void velocity_set_multi(struct net_device
*dev
)
2117 struct velocity_info
*vptr
= netdev_priv(dev
);
2118 struct mac_regs __iomem
* regs
= vptr
->mac_regs
;
2121 struct dev_mc_list
*mclist
;
2123 if (dev
->flags
& IFF_PROMISC
) { /* Set promiscuous. */
2124 writel(0xffffffff, ®s
->MARCAM
[0]);
2125 writel(0xffffffff, ®s
->MARCAM
[4]);
2126 rx_mode
= (RCR_AM
| RCR_AB
| RCR_PROM
);
2127 } else if ((dev
->mc_count
> vptr
->multicast_limit
)
2128 || (dev
->flags
& IFF_ALLMULTI
)) {
2129 writel(0xffffffff, ®s
->MARCAM
[0]);
2130 writel(0xffffffff, ®s
->MARCAM
[4]);
2131 rx_mode
= (RCR_AM
| RCR_AB
);
2133 int offset
= MCAM_SIZE
- vptr
->multicast_limit
;
2134 mac_get_cam_mask(regs
, vptr
->mCAMmask
, VELOCITY_MULTICAST_CAM
);
2136 for (i
= 0, mclist
= dev
->mc_list
; mclist
&& i
< dev
->mc_count
; i
++, mclist
= mclist
->next
) {
2137 mac_set_cam(regs
, i
+ offset
, mclist
->dmi_addr
, VELOCITY_MULTICAST_CAM
);
2138 vptr
->mCAMmask
[(offset
+ i
) / 8] |= 1 << ((offset
+ i
) & 7);
2141 mac_set_cam_mask(regs
, vptr
->mCAMmask
, VELOCITY_MULTICAST_CAM
);
2142 rx_mode
= (RCR_AM
| RCR_AB
);
2144 if (dev
->mtu
> 1500)
2147 BYTE_REG_BITS_ON(rx_mode
, ®s
->RCR
);
2152 * velocity_get_status - statistics callback
2153 * @dev: network device
2155 * Callback from the network layer to allow driver statistics
2156 * to be resynchronized with hardware collected state. In the
2157 * case of the velocity we need to pull the MIB counters from
2158 * the hardware into the counters before letting the network
2159 * layer display them.
2162 static struct net_device_stats
*velocity_get_stats(struct net_device
*dev
)
2164 struct velocity_info
*vptr
= netdev_priv(dev
);
2166 /* If the hardware is down, don't touch MII */
2167 if(!netif_running(dev
))
2168 return &vptr
->stats
;
2170 spin_lock_irq(&vptr
->lock
);
2171 velocity_update_hw_mibs(vptr
);
2172 spin_unlock_irq(&vptr
->lock
);
2174 vptr
->stats
.rx_packets
= vptr
->mib_counter
[HW_MIB_ifRxAllPkts
];
2175 vptr
->stats
.rx_errors
= vptr
->mib_counter
[HW_MIB_ifRxErrorPkts
];
2176 vptr
->stats
.rx_length_errors
= vptr
->mib_counter
[HW_MIB_ifInRangeLengthErrors
];
2178 // unsigned long rx_dropped; /* no space in linux buffers */
2179 vptr
->stats
.collisions
= vptr
->mib_counter
[HW_MIB_ifTxEtherCollisions
];
2180 /* detailed rx_errors: */
2181 // unsigned long rx_length_errors;
2182 // unsigned long rx_over_errors; /* receiver ring buff overflow */
2183 vptr
->stats
.rx_crc_errors
= vptr
->mib_counter
[HW_MIB_ifRxPktCRCE
];
2184 // unsigned long rx_frame_errors; /* recv'd frame alignment error */
2185 // unsigned long rx_fifo_errors; /* recv'r fifo overrun */
2186 // unsigned long rx_missed_errors; /* receiver missed packet */
2188 /* detailed tx_errors */
2189 // unsigned long tx_fifo_errors;
2191 return &vptr
->stats
;
2196 * velocity_ioctl - ioctl entry point
2197 * @dev: network device
2198 * @rq: interface request ioctl
2199 * @cmd: command code
2201 * Called when the user issues an ioctl request to the network
2202 * device in question. The velocity interface supports MII.
2205 static int velocity_ioctl(struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
2207 struct velocity_info
*vptr
= netdev_priv(dev
);
2210 /* If we are asked for information and the device is power
2211 saving then we need to bring the device back up to talk to it */
2213 if (!netif_running(dev
))
2214 pci_set_power_state(vptr
->pdev
, PCI_D0
);
2217 case SIOCGMIIPHY
: /* Get address of MII PHY in use. */
2218 case SIOCGMIIREG
: /* Read MII PHY register. */
2219 case SIOCSMIIREG
: /* Write to MII PHY register. */
2220 ret
= velocity_mii_ioctl(dev
, rq
, cmd
);
2226 if (!netif_running(dev
))
2227 pci_set_power_state(vptr
->pdev
, PCI_D3hot
);
2234 * Definition for our device driver. The PCI layer interface
2235 * uses this to handle all our card discover and plugging
2238 static struct pci_driver velocity_driver
= {
2239 .name
= VELOCITY_NAME
,
2240 .id_table
= velocity_id_table
,
2241 .probe
= velocity_found1
,
2242 .remove
= __devexit_p(velocity_remove1
),
2244 .suspend
= velocity_suspend
,
2245 .resume
= velocity_resume
,
2250 * velocity_init_module - load time function
2252 * Called when the velocity module is loaded. The PCI driver
2253 * is registered with the PCI layer, and in turn will call
2254 * the probe functions for each velocity adapter installed
2258 static int __init
velocity_init_module(void)
2262 velocity_register_notifier();
2263 ret
= pci_register_driver(&velocity_driver
);
2265 velocity_unregister_notifier();
2270 * velocity_cleanup - module unload
2272 * When the velocity hardware is unloaded this function is called.
2273 * It will clean up the notifiers and the unregister the PCI
2274 * driver interface for this hardware. This in turn cleans up
2275 * all discovered interfaces before returning from the function
2278 static void __exit
velocity_cleanup_module(void)
2280 velocity_unregister_notifier();
2281 pci_unregister_driver(&velocity_driver
);
2284 module_init(velocity_init_module
);
2285 module_exit(velocity_cleanup_module
);
2289 * MII access , media link mode setting functions
2294 * mii_init - set up MII
2295 * @vptr: velocity adapter
2296 * @mii_status: links tatus
2298 * Set up the PHY for the current link state.
2301 static void mii_init(struct velocity_info
*vptr
, u32 mii_status
)
2305 switch (PHYID_GET_PHY_ID(vptr
->phy_id
)) {
2306 case PHYID_CICADA_CS8201
:
2308 * Reset to hardware default
2310 MII_REG_BITS_OFF((ANAR_ASMDIR
| ANAR_PAUSE
), MII_REG_ANAR
, vptr
->mac_regs
);
2312 * Turn on ECHODIS bit in NWay-forced full mode and turn it
2313 * off it in NWay-forced half mode for NWay-forced v.s.
2314 * legacy-forced issue.
2316 if (vptr
->mii_status
& VELOCITY_DUPLEX_FULL
)
2317 MII_REG_BITS_ON(TCSR_ECHODIS
, MII_REG_TCSR
, vptr
->mac_regs
);
2319 MII_REG_BITS_OFF(TCSR_ECHODIS
, MII_REG_TCSR
, vptr
->mac_regs
);
2321 * Turn on Link/Activity LED enable bit for CIS8201
2323 MII_REG_BITS_ON(PLED_LALBE
, MII_REG_PLED
, vptr
->mac_regs
);
2325 case PHYID_VT3216_32BIT
:
2326 case PHYID_VT3216_64BIT
:
2328 * Reset to hardware default
2330 MII_REG_BITS_ON((ANAR_ASMDIR
| ANAR_PAUSE
), MII_REG_ANAR
, vptr
->mac_regs
);
2332 * Turn on ECHODIS bit in NWay-forced full mode and turn it
2333 * off it in NWay-forced half mode for NWay-forced v.s.
2334 * legacy-forced issue
2336 if (vptr
->mii_status
& VELOCITY_DUPLEX_FULL
)
2337 MII_REG_BITS_ON(TCSR_ECHODIS
, MII_REG_TCSR
, vptr
->mac_regs
);
2339 MII_REG_BITS_OFF(TCSR_ECHODIS
, MII_REG_TCSR
, vptr
->mac_regs
);
2342 case PHYID_MARVELL_1000
:
2343 case PHYID_MARVELL_1000S
:
2345 * Assert CRS on Transmit
2347 MII_REG_BITS_ON(PSCR_ACRSTX
, MII_REG_PSCR
, vptr
->mac_regs
);
2349 * Reset to hardware default
2351 MII_REG_BITS_ON((ANAR_ASMDIR
| ANAR_PAUSE
), MII_REG_ANAR
, vptr
->mac_regs
);
2356 velocity_mii_read(vptr
->mac_regs
, MII_REG_BMCR
, &BMCR
);
2357 if (BMCR
& BMCR_ISO
) {
2359 velocity_mii_write(vptr
->mac_regs
, MII_REG_BMCR
, BMCR
);
2364 * safe_disable_mii_autopoll - autopoll off
2365 * @regs: velocity registers
2367 * Turn off the autopoll and wait for it to disable on the chip
2370 static void safe_disable_mii_autopoll(struct mac_regs __iomem
* regs
)
2374 /* turn off MAUTO */
2375 writeb(0, ®s
->MIICR
);
2376 for (ww
= 0; ww
< W_MAX_TIMEOUT
; ww
++) {
2378 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE
, ®s
->MIISR
))
2384 * enable_mii_autopoll - turn on autopolling
2385 * @regs: velocity registers
2387 * Enable the MII link status autopoll feature on the Velocity
2388 * hardware. Wait for it to enable.
2391 static void enable_mii_autopoll(struct mac_regs __iomem
* regs
)
2395 writeb(0, &(regs
->MIICR
));
2396 writeb(MIIADR_SWMPL
, ®s
->MIIADR
);
2398 for (ii
= 0; ii
< W_MAX_TIMEOUT
; ii
++) {
2400 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE
, ®s
->MIISR
))
2404 writeb(MIICR_MAUTO
, ®s
->MIICR
);
2406 for (ii
= 0; ii
< W_MAX_TIMEOUT
; ii
++) {
2408 if (!BYTE_REG_BITS_IS_ON(MIISR_MIDLE
, ®s
->MIISR
))
2415 * velocity_mii_read - read MII data
2416 * @regs: velocity registers
2417 * @index: MII register index
2418 * @data: buffer for received data
2420 * Perform a single read of an MII 16bit register. Returns zero
2421 * on success or -ETIMEDOUT if the PHY did not respond.
2424 static int velocity_mii_read(struct mac_regs __iomem
*regs
, u8 index
, u16
*data
)
2429 * Disable MIICR_MAUTO, so that mii addr can be set normally
2431 safe_disable_mii_autopoll(regs
);
2433 writeb(index
, ®s
->MIIADR
);
2435 BYTE_REG_BITS_ON(MIICR_RCMD
, ®s
->MIICR
);
2437 for (ww
= 0; ww
< W_MAX_TIMEOUT
; ww
++) {
2438 if (!(readb(®s
->MIICR
) & MIICR_RCMD
))
2442 *data
= readw(®s
->MIIDATA
);
2444 enable_mii_autopoll(regs
);
2445 if (ww
== W_MAX_TIMEOUT
)
2451 * velocity_mii_write - write MII data
2452 * @regs: velocity registers
2453 * @index: MII register index
2454 * @data: 16bit data for the MII register
2456 * Perform a single write to an MII 16bit register. Returns zero
2457 * on success or -ETIMEDOUT if the PHY did not respond.
2460 static int velocity_mii_write(struct mac_regs __iomem
*regs
, u8 mii_addr
, u16 data
)
2465 * Disable MIICR_MAUTO, so that mii addr can be set normally
2467 safe_disable_mii_autopoll(regs
);
2469 /* MII reg offset */
2470 writeb(mii_addr
, ®s
->MIIADR
);
2472 writew(data
, ®s
->MIIDATA
);
2474 /* turn on MIICR_WCMD */
2475 BYTE_REG_BITS_ON(MIICR_WCMD
, ®s
->MIICR
);
2477 /* W_MAX_TIMEOUT is the timeout period */
2478 for (ww
= 0; ww
< W_MAX_TIMEOUT
; ww
++) {
2480 if (!(readb(®s
->MIICR
) & MIICR_WCMD
))
2483 enable_mii_autopoll(regs
);
2485 if (ww
== W_MAX_TIMEOUT
)
2491 * velocity_get_opt_media_mode - get media selection
2492 * @vptr: velocity adapter
2494 * Get the media mode stored in EEPROM or module options and load
2495 * mii_status accordingly. The requested link state information
2499 static u32
velocity_get_opt_media_mode(struct velocity_info
*vptr
)
2503 switch (vptr
->options
.spd_dpx
) {
2505 status
= VELOCITY_AUTONEG_ENABLE
;
2507 case SPD_DPX_100_FULL
:
2508 status
= VELOCITY_SPEED_100
| VELOCITY_DUPLEX_FULL
;
2510 case SPD_DPX_10_FULL
:
2511 status
= VELOCITY_SPEED_10
| VELOCITY_DUPLEX_FULL
;
2513 case SPD_DPX_100_HALF
:
2514 status
= VELOCITY_SPEED_100
;
2516 case SPD_DPX_10_HALF
:
2517 status
= VELOCITY_SPEED_10
;
2520 vptr
->mii_status
= status
;
2525 * mii_set_auto_on - autonegotiate on
2528 * Enable autonegotation on this interface
2531 static void mii_set_auto_on(struct velocity_info
*vptr
)
2533 if (MII_REG_BITS_IS_ON(BMCR_AUTO
, MII_REG_BMCR
, vptr
->mac_regs
))
2534 MII_REG_BITS_ON(BMCR_REAUTO
, MII_REG_BMCR
, vptr
->mac_regs
);
2536 MII_REG_BITS_ON(BMCR_AUTO
, MII_REG_BMCR
, vptr
->mac_regs
);
2541 static void mii_set_auto_off(struct velocity_info * vptr)
2543 MII_REG_BITS_OFF(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs);
2548 * set_mii_flow_control - flow control setup
2549 * @vptr: velocity interface
2551 * Set up the flow control on this interface according to
2552 * the supplied user/eeprom options.
2555 static void set_mii_flow_control(struct velocity_info
*vptr
)
2557 /*Enable or Disable PAUSE in ANAR */
2558 switch (vptr
->options
.flow_cntl
) {
2560 MII_REG_BITS_OFF(ANAR_PAUSE
, MII_REG_ANAR
, vptr
->mac_regs
);
2561 MII_REG_BITS_ON(ANAR_ASMDIR
, MII_REG_ANAR
, vptr
->mac_regs
);
2565 MII_REG_BITS_ON(ANAR_PAUSE
, MII_REG_ANAR
, vptr
->mac_regs
);
2566 MII_REG_BITS_ON(ANAR_ASMDIR
, MII_REG_ANAR
, vptr
->mac_regs
);
2569 case FLOW_CNTL_TX_RX
:
2570 MII_REG_BITS_ON(ANAR_PAUSE
, MII_REG_ANAR
, vptr
->mac_regs
);
2571 MII_REG_BITS_ON(ANAR_ASMDIR
, MII_REG_ANAR
, vptr
->mac_regs
);
2574 case FLOW_CNTL_DISABLE
:
2575 MII_REG_BITS_OFF(ANAR_PAUSE
, MII_REG_ANAR
, vptr
->mac_regs
);
2576 MII_REG_BITS_OFF(ANAR_ASMDIR
, MII_REG_ANAR
, vptr
->mac_regs
);
2584 * velocity_set_media_mode - set media mode
2585 * @mii_status: old MII link state
2587 * Check the media link state and configure the flow control
2588 * PHY and also velocity hardware setup accordingly. In particular
2589 * we need to set up CD polling and frame bursting.
2592 static int velocity_set_media_mode(struct velocity_info
*vptr
, u32 mii_status
)
2595 struct mac_regs __iomem
* regs
= vptr
->mac_regs
;
2597 vptr
->mii_status
= mii_check_media_mode(vptr
->mac_regs
);
2598 curr_status
= vptr
->mii_status
& (~VELOCITY_LINK_FAIL
);
2600 /* Set mii link status */
2601 set_mii_flow_control(vptr
);
2604 Check if new status is consisent with current status
2605 if (((mii_status & curr_status) & VELOCITY_AUTONEG_ENABLE)
2606 || (mii_status==curr_status)) {
2607 vptr->mii_status=mii_check_media_mode(vptr->mac_regs);
2608 vptr->mii_status=check_connection_type(vptr->mac_regs);
2609 VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity link no change\n");
2614 if (PHYID_GET_PHY_ID(vptr
->phy_id
) == PHYID_CICADA_CS8201
) {
2615 MII_REG_BITS_ON(AUXCR_MDPPS
, MII_REG_AUXCR
, vptr
->mac_regs
);
2619 * If connection type is AUTO
2621 if (mii_status
& VELOCITY_AUTONEG_ENABLE
) {
2622 VELOCITY_PRT(MSG_LEVEL_INFO
, "Velocity is AUTO mode\n");
2623 /* clear force MAC mode bit */
2624 BYTE_REG_BITS_OFF(CHIPGCR_FCMODE
, ®s
->CHIPGCR
);
2625 /* set duplex mode of MAC according to duplex mode of MII */
2626 MII_REG_BITS_ON(ANAR_TXFD
| ANAR_TX
| ANAR_10FD
| ANAR_10
, MII_REG_ANAR
, vptr
->mac_regs
);
2627 MII_REG_BITS_ON(G1000CR_1000FD
| G1000CR_1000
, MII_REG_G1000CR
, vptr
->mac_regs
);
2628 MII_REG_BITS_ON(BMCR_SPEED1G
, MII_REG_BMCR
, vptr
->mac_regs
);
2630 /* enable AUTO-NEGO mode */
2631 mii_set_auto_on(vptr
);
2637 * 1. if it's 3119, disable frame bursting in halfduplex mode
2638 * and enable it in fullduplex mode
2639 * 2. set correct MII/GMII and half/full duplex mode in CHIPGCR
2640 * 3. only enable CD heart beat counter in 10HD mode
2643 /* set force MAC mode bit */
2644 BYTE_REG_BITS_ON(CHIPGCR_FCMODE
, ®s
->CHIPGCR
);
2646 CHIPGCR
= readb(®s
->CHIPGCR
);
2647 CHIPGCR
&= ~CHIPGCR_FCGMII
;
2649 if (mii_status
& VELOCITY_DUPLEX_FULL
) {
2650 CHIPGCR
|= CHIPGCR_FCFDX
;
2651 writeb(CHIPGCR
, ®s
->CHIPGCR
);
2652 VELOCITY_PRT(MSG_LEVEL_INFO
, "set Velocity to forced full mode\n");
2653 if (vptr
->rev_id
< REV_ID_VT3216_A0
)
2654 BYTE_REG_BITS_OFF(TCR_TB2BDIS
, ®s
->TCR
);
2656 CHIPGCR
&= ~CHIPGCR_FCFDX
;
2657 VELOCITY_PRT(MSG_LEVEL_INFO
, "set Velocity to forced half mode\n");
2658 writeb(CHIPGCR
, ®s
->CHIPGCR
);
2659 if (vptr
->rev_id
< REV_ID_VT3216_A0
)
2660 BYTE_REG_BITS_ON(TCR_TB2BDIS
, ®s
->TCR
);
2663 MII_REG_BITS_OFF(G1000CR_1000FD
| G1000CR_1000
, MII_REG_G1000CR
, vptr
->mac_regs
);
2665 if (!(mii_status
& VELOCITY_DUPLEX_FULL
) && (mii_status
& VELOCITY_SPEED_10
)) {
2666 BYTE_REG_BITS_OFF(TESTCFG_HBDIS
, ®s
->TESTCFG
);
2668 BYTE_REG_BITS_ON(TESTCFG_HBDIS
, ®s
->TESTCFG
);
2670 /* MII_REG_BITS_OFF(BMCR_SPEED1G, MII_REG_BMCR, vptr->mac_regs); */
2671 velocity_mii_read(vptr
->mac_regs
, MII_REG_ANAR
, &ANAR
);
2672 ANAR
&= (~(ANAR_TXFD
| ANAR_TX
| ANAR_10FD
| ANAR_10
));
2673 if (mii_status
& VELOCITY_SPEED_100
) {
2674 if (mii_status
& VELOCITY_DUPLEX_FULL
)
2679 if (mii_status
& VELOCITY_DUPLEX_FULL
)
2684 velocity_mii_write(vptr
->mac_regs
, MII_REG_ANAR
, ANAR
);
2685 /* enable AUTO-NEGO mode */
2686 mii_set_auto_on(vptr
);
2687 /* MII_REG_BITS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs); */
2689 /* vptr->mii_status=mii_check_media_mode(vptr->mac_regs); */
2690 /* vptr->mii_status=check_connection_type(vptr->mac_regs); */
2691 return VELOCITY_LINK_CHANGE
;
2695 * mii_check_media_mode - check media state
2696 * @regs: velocity registers
2698 * Check the current MII status and determine the link status
2702 static u32
mii_check_media_mode(struct mac_regs __iomem
* regs
)
2707 if (!MII_REG_BITS_IS_ON(BMSR_LNK
, MII_REG_BMSR
, regs
))
2708 status
|= VELOCITY_LINK_FAIL
;
2710 if (MII_REG_BITS_IS_ON(G1000CR_1000FD
, MII_REG_G1000CR
, regs
))
2711 status
|= VELOCITY_SPEED_1000
| VELOCITY_DUPLEX_FULL
;
2712 else if (MII_REG_BITS_IS_ON(G1000CR_1000
, MII_REG_G1000CR
, regs
))
2713 status
|= (VELOCITY_SPEED_1000
);
2715 velocity_mii_read(regs
, MII_REG_ANAR
, &ANAR
);
2716 if (ANAR
& ANAR_TXFD
)
2717 status
|= (VELOCITY_SPEED_100
| VELOCITY_DUPLEX_FULL
);
2718 else if (ANAR
& ANAR_TX
)
2719 status
|= VELOCITY_SPEED_100
;
2720 else if (ANAR
& ANAR_10FD
)
2721 status
|= (VELOCITY_SPEED_10
| VELOCITY_DUPLEX_FULL
);
2723 status
|= (VELOCITY_SPEED_10
);
2726 if (MII_REG_BITS_IS_ON(BMCR_AUTO
, MII_REG_BMCR
, regs
)) {
2727 velocity_mii_read(regs
, MII_REG_ANAR
, &ANAR
);
2728 if ((ANAR
& (ANAR_TXFD
| ANAR_TX
| ANAR_10FD
| ANAR_10
))
2729 == (ANAR_TXFD
| ANAR_TX
| ANAR_10FD
| ANAR_10
)) {
2730 if (MII_REG_BITS_IS_ON(G1000CR_1000
| G1000CR_1000FD
, MII_REG_G1000CR
, regs
))
2731 status
|= VELOCITY_AUTONEG_ENABLE
;
2738 static u32
check_connection_type(struct mac_regs __iomem
* regs
)
2743 PHYSR0
= readb(®s
->PHYSR0
);
2746 if (!(PHYSR0 & PHYSR0_LINKGD))
2747 status|=VELOCITY_LINK_FAIL;
2750 if (PHYSR0
& PHYSR0_FDPX
)
2751 status
|= VELOCITY_DUPLEX_FULL
;
2753 if (PHYSR0
& PHYSR0_SPDG
)
2754 status
|= VELOCITY_SPEED_1000
;
2755 else if (PHYSR0
& PHYSR0_SPD10
)
2756 status
|= VELOCITY_SPEED_10
;
2758 status
|= VELOCITY_SPEED_100
;
2760 if (MII_REG_BITS_IS_ON(BMCR_AUTO
, MII_REG_BMCR
, regs
)) {
2761 velocity_mii_read(regs
, MII_REG_ANAR
, &ANAR
);
2762 if ((ANAR
& (ANAR_TXFD
| ANAR_TX
| ANAR_10FD
| ANAR_10
))
2763 == (ANAR_TXFD
| ANAR_TX
| ANAR_10FD
| ANAR_10
)) {
2764 if (MII_REG_BITS_IS_ON(G1000CR_1000
| G1000CR_1000FD
, MII_REG_G1000CR
, regs
))
2765 status
|= VELOCITY_AUTONEG_ENABLE
;
2773 * enable_flow_control_ability - flow control
2774 * @vptr: veloity to configure
2776 * Set up flow control according to the flow control options
2777 * determined by the eeprom/configuration.
2780 static void enable_flow_control_ability(struct velocity_info
*vptr
)
2783 struct mac_regs __iomem
* regs
= vptr
->mac_regs
;
2785 switch (vptr
->options
.flow_cntl
) {
2787 case FLOW_CNTL_DEFAULT
:
2788 if (BYTE_REG_BITS_IS_ON(PHYSR0_RXFLC
, ®s
->PHYSR0
))
2789 writel(CR0_FDXRFCEN
, ®s
->CR0Set
);
2791 writel(CR0_FDXRFCEN
, ®s
->CR0Clr
);
2793 if (BYTE_REG_BITS_IS_ON(PHYSR0_TXFLC
, ®s
->PHYSR0
))
2794 writel(CR0_FDXTFCEN
, ®s
->CR0Set
);
2796 writel(CR0_FDXTFCEN
, ®s
->CR0Clr
);
2800 writel(CR0_FDXTFCEN
, ®s
->CR0Set
);
2801 writel(CR0_FDXRFCEN
, ®s
->CR0Clr
);
2805 writel(CR0_FDXRFCEN
, ®s
->CR0Set
);
2806 writel(CR0_FDXTFCEN
, ®s
->CR0Clr
);
2809 case FLOW_CNTL_TX_RX
:
2810 writel(CR0_FDXTFCEN
, ®s
->CR0Set
);
2811 writel(CR0_FDXRFCEN
, ®s
->CR0Set
);
2814 case FLOW_CNTL_DISABLE
:
2815 writel(CR0_FDXRFCEN
, ®s
->CR0Clr
);
2816 writel(CR0_FDXTFCEN
, ®s
->CR0Clr
);
2827 * velocity_ethtool_up - pre hook for ethtool
2828 * @dev: network device
2830 * Called before an ethtool operation. We need to make sure the
2831 * chip is out of D3 state before we poke at it.
2834 static int velocity_ethtool_up(struct net_device
*dev
)
2836 struct velocity_info
*vptr
= netdev_priv(dev
);
2837 if (!netif_running(dev
))
2838 pci_set_power_state(vptr
->pdev
, PCI_D0
);
2843 * velocity_ethtool_down - post hook for ethtool
2844 * @dev: network device
2846 * Called after an ethtool operation. Restore the chip back to D3
2847 * state if it isn't running.
2850 static void velocity_ethtool_down(struct net_device
*dev
)
2852 struct velocity_info
*vptr
= netdev_priv(dev
);
2853 if (!netif_running(dev
))
2854 pci_set_power_state(vptr
->pdev
, PCI_D3hot
);
2857 static int velocity_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
2859 struct velocity_info
*vptr
= netdev_priv(dev
);
2860 struct mac_regs __iomem
* regs
= vptr
->mac_regs
;
2862 status
= check_connection_type(vptr
->mac_regs
);
2864 cmd
->supported
= SUPPORTED_TP
|
2866 SUPPORTED_10baseT_Half
|
2867 SUPPORTED_10baseT_Full
|
2868 SUPPORTED_100baseT_Half
|
2869 SUPPORTED_100baseT_Full
|
2870 SUPPORTED_1000baseT_Half
|
2871 SUPPORTED_1000baseT_Full
;
2872 if (status
& VELOCITY_SPEED_1000
)
2873 cmd
->speed
= SPEED_1000
;
2874 else if (status
& VELOCITY_SPEED_100
)
2875 cmd
->speed
= SPEED_100
;
2877 cmd
->speed
= SPEED_10
;
2878 cmd
->autoneg
= (status
& VELOCITY_AUTONEG_ENABLE
) ? AUTONEG_ENABLE
: AUTONEG_DISABLE
;
2879 cmd
->port
= PORT_TP
;
2880 cmd
->transceiver
= XCVR_INTERNAL
;
2881 cmd
->phy_address
= readb(®s
->MIIADR
) & 0x1F;
2883 if (status
& VELOCITY_DUPLEX_FULL
)
2884 cmd
->duplex
= DUPLEX_FULL
;
2886 cmd
->duplex
= DUPLEX_HALF
;
2891 static int velocity_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
2893 struct velocity_info
*vptr
= netdev_priv(dev
);
2898 curr_status
= check_connection_type(vptr
->mac_regs
);
2899 curr_status
&= (~VELOCITY_LINK_FAIL
);
2901 new_status
|= ((cmd
->autoneg
) ? VELOCITY_AUTONEG_ENABLE
: 0);
2902 new_status
|= ((cmd
->speed
== SPEED_100
) ? VELOCITY_SPEED_100
: 0);
2903 new_status
|= ((cmd
->speed
== SPEED_10
) ? VELOCITY_SPEED_10
: 0);
2904 new_status
|= ((cmd
->duplex
== DUPLEX_FULL
) ? VELOCITY_DUPLEX_FULL
: 0);
2906 if ((new_status
& VELOCITY_AUTONEG_ENABLE
) && (new_status
!= (curr_status
| VELOCITY_AUTONEG_ENABLE
)))
2909 velocity_set_media_mode(vptr
, new_status
);
2914 static u32
velocity_get_link(struct net_device
*dev
)
2916 struct velocity_info
*vptr
= netdev_priv(dev
);
2917 struct mac_regs __iomem
* regs
= vptr
->mac_regs
;
2918 return BYTE_REG_BITS_IS_ON(PHYSR0_LINKGD
, ®s
->PHYSR0
) ? 1 : 0;
2921 static void velocity_get_drvinfo(struct net_device
*dev
, struct ethtool_drvinfo
*info
)
2923 struct velocity_info
*vptr
= netdev_priv(dev
);
2924 strcpy(info
->driver
, VELOCITY_NAME
);
2925 strcpy(info
->version
, VELOCITY_VERSION
);
2926 strcpy(info
->bus_info
, pci_name(vptr
->pdev
));
2929 static void velocity_ethtool_get_wol(struct net_device
*dev
, struct ethtool_wolinfo
*wol
)
2931 struct velocity_info
*vptr
= netdev_priv(dev
);
2932 wol
->supported
= WAKE_PHY
| WAKE_MAGIC
| WAKE_UCAST
| WAKE_ARP
;
2933 wol
->wolopts
|= WAKE_MAGIC
;
2935 if (vptr->wol_opts & VELOCITY_WOL_PHY)
2936 wol.wolopts|=WAKE_PHY;
2938 if (vptr
->wol_opts
& VELOCITY_WOL_UCAST
)
2939 wol
->wolopts
|= WAKE_UCAST
;
2940 if (vptr
->wol_opts
& VELOCITY_WOL_ARP
)
2941 wol
->wolopts
|= WAKE_ARP
;
2942 memcpy(&wol
->sopass
, vptr
->wol_passwd
, 6);
2945 static int velocity_ethtool_set_wol(struct net_device
*dev
, struct ethtool_wolinfo
*wol
)
2947 struct velocity_info
*vptr
= netdev_priv(dev
);
2949 if (!(wol
->wolopts
& (WAKE_PHY
| WAKE_MAGIC
| WAKE_UCAST
| WAKE_ARP
)))
2951 vptr
->wol_opts
= VELOCITY_WOL_MAGIC
;
2954 if (wol.wolopts & WAKE_PHY) {
2955 vptr->wol_opts|=VELOCITY_WOL_PHY;
2956 vptr->flags |=VELOCITY_FLAGS_WOL_ENABLED;
2960 if (wol
->wolopts
& WAKE_MAGIC
) {
2961 vptr
->wol_opts
|= VELOCITY_WOL_MAGIC
;
2962 vptr
->flags
|= VELOCITY_FLAGS_WOL_ENABLED
;
2964 if (wol
->wolopts
& WAKE_UCAST
) {
2965 vptr
->wol_opts
|= VELOCITY_WOL_UCAST
;
2966 vptr
->flags
|= VELOCITY_FLAGS_WOL_ENABLED
;
2968 if (wol
->wolopts
& WAKE_ARP
) {
2969 vptr
->wol_opts
|= VELOCITY_WOL_ARP
;
2970 vptr
->flags
|= VELOCITY_FLAGS_WOL_ENABLED
;
2972 memcpy(vptr
->wol_passwd
, wol
->sopass
, 6);
2976 static u32
velocity_get_msglevel(struct net_device
*dev
)
2981 static void velocity_set_msglevel(struct net_device
*dev
, u32 value
)
2986 static const struct ethtool_ops velocity_ethtool_ops
= {
2987 .get_settings
= velocity_get_settings
,
2988 .set_settings
= velocity_set_settings
,
2989 .get_drvinfo
= velocity_get_drvinfo
,
2990 .get_wol
= velocity_ethtool_get_wol
,
2991 .set_wol
= velocity_ethtool_set_wol
,
2992 .get_msglevel
= velocity_get_msglevel
,
2993 .set_msglevel
= velocity_set_msglevel
,
2994 .get_link
= velocity_get_link
,
2995 .begin
= velocity_ethtool_up
,
2996 .complete
= velocity_ethtool_down
3000 * velocity_mii_ioctl - MII ioctl handler
3001 * @dev: network device
3002 * @ifr: the ifreq block for the ioctl
3005 * Process MII requests made via ioctl from the network layer. These
3006 * are used by tools like kudzu to interrogate the link state of the
3010 static int velocity_mii_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
3012 struct velocity_info
*vptr
= netdev_priv(dev
);
3013 struct mac_regs __iomem
* regs
= vptr
->mac_regs
;
3014 unsigned long flags
;
3015 struct mii_ioctl_data
*miidata
= if_mii(ifr
);
3020 miidata
->phy_id
= readb(®s
->MIIADR
) & 0x1f;
3023 if (!capable(CAP_NET_ADMIN
))
3025 if(velocity_mii_read(vptr
->mac_regs
, miidata
->reg_num
& 0x1f, &(miidata
->val_out
)) < 0)
3029 if (!capable(CAP_NET_ADMIN
))
3031 spin_lock_irqsave(&vptr
->lock
, flags
);
3032 err
= velocity_mii_write(vptr
->mac_regs
, miidata
->reg_num
& 0x1f, miidata
->val_in
);
3033 spin_unlock_irqrestore(&vptr
->lock
, flags
);
3034 check_connection_type(vptr
->mac_regs
);
3047 * velocity_save_context - save registers
3049 * @context: buffer for stored context
3051 * Retrieve the current configuration from the velocity hardware
3052 * and stash it in the context structure, for use by the context
3053 * restore functions. This allows us to save things we need across
3057 static void velocity_save_context(struct velocity_info
*vptr
, struct velocity_context
* context
)
3059 struct mac_regs __iomem
* regs
= vptr
->mac_regs
;
3061 u8 __iomem
*ptr
= (u8 __iomem
*)regs
;
3063 for (i
= MAC_REG_PAR
; i
< MAC_REG_CR0_CLR
; i
+= 4)
3064 *((u32
*) (context
->mac_reg
+ i
)) = readl(ptr
+ i
);
3066 for (i
= MAC_REG_MAR
; i
< MAC_REG_TDCSR_CLR
; i
+= 4)
3067 *((u32
*) (context
->mac_reg
+ i
)) = readl(ptr
+ i
);
3069 for (i
= MAC_REG_RDBASE_LO
; i
< MAC_REG_FIFO_TEST0
; i
+= 4)
3070 *((u32
*) (context
->mac_reg
+ i
)) = readl(ptr
+ i
);
3075 * velocity_restore_context - restore registers
3077 * @context: buffer for stored context
3079 * Reload the register configuration from the velocity context
3080 * created by velocity_save_context.
3083 static void velocity_restore_context(struct velocity_info
*vptr
, struct velocity_context
*context
)
3085 struct mac_regs __iomem
* regs
= vptr
->mac_regs
;
3087 u8 __iomem
*ptr
= (u8 __iomem
*)regs
;
3089 for (i
= MAC_REG_PAR
; i
< MAC_REG_CR0_SET
; i
+= 4) {
3090 writel(*((u32
*) (context
->mac_reg
+ i
)), ptr
+ i
);
3094 for (i
= MAC_REG_CR1_SET
; i
< MAC_REG_CR0_CLR
; i
++) {
3096 writeb(~(*((u8
*) (context
->mac_reg
+ i
))), ptr
+ i
+ 4);
3098 writeb(*((u8
*) (context
->mac_reg
+ i
)), ptr
+ i
);
3101 for (i
= MAC_REG_MAR
; i
< MAC_REG_IMR
; i
+= 4) {
3102 writel(*((u32
*) (context
->mac_reg
+ i
)), ptr
+ i
);
3105 for (i
= MAC_REG_RDBASE_LO
; i
< MAC_REG_FIFO_TEST0
; i
+= 4) {
3106 writel(*((u32
*) (context
->mac_reg
+ i
)), ptr
+ i
);
3109 for (i
= MAC_REG_TDCSR_SET
; i
<= MAC_REG_RDCSR_SET
; i
++) {
3110 writeb(*((u8
*) (context
->mac_reg
+ i
)), ptr
+ i
);
3116 * wol_calc_crc - WOL CRC
3117 * @pattern: data pattern
3118 * @mask_pattern: mask
3120 * Compute the wake on lan crc hashes for the packet header
3121 * we are interested in.
3124 static u16
wol_calc_crc(int size
, u8
* pattern
, u8
*mask_pattern
)
3130 for (i
= 0; i
< size
; i
++) {
3131 mask
= mask_pattern
[i
];
3133 /* Skip this loop if the mask equals to zero */
3137 for (j
= 0; j
< 8; j
++) {
3138 if ((mask
& 0x01) == 0) {
3143 crc
= crc_ccitt(crc
, &(pattern
[i
* 8 + j
]), 1);
3146 /* Finally, invert the result once to get the correct data */
3148 return bitrev32(crc
) >> 16;
3152 * velocity_set_wol - set up for wake on lan
3153 * @vptr: velocity to set WOL status on
3155 * Set a card up for wake on lan either by unicast or by
3158 * FIXME: check static buffer is safe here
3161 static int velocity_set_wol(struct velocity_info
*vptr
)
3163 struct mac_regs __iomem
* regs
= vptr
->mac_regs
;
3167 static u32 mask_pattern
[2][4] = {
3168 {0x00203000, 0x000003C0, 0x00000000, 0x0000000}, /* ARP */
3169 {0xfffff000, 0xffffffff, 0xffffffff, 0x000ffff} /* Magic Packet */
3172 writew(0xFFFF, ®s
->WOLCRClr
);
3173 writeb(WOLCFG_SAB
| WOLCFG_SAM
, ®s
->WOLCFGSet
);
3174 writew(WOLCR_MAGIC_EN
, ®s
->WOLCRSet
);
3177 if (vptr->wol_opts & VELOCITY_WOL_PHY)
3178 writew((WOLCR_LINKON_EN|WOLCR_LINKOFF_EN), ®s->WOLCRSet);
3181 if (vptr
->wol_opts
& VELOCITY_WOL_UCAST
) {
3182 writew(WOLCR_UNICAST_EN
, ®s
->WOLCRSet
);
3185 if (vptr
->wol_opts
& VELOCITY_WOL_ARP
) {
3186 struct arp_packet
*arp
= (struct arp_packet
*) buf
;
3188 memset(buf
, 0, sizeof(struct arp_packet
) + 7);
3190 for (i
= 0; i
< 4; i
++)
3191 writel(mask_pattern
[0][i
], ®s
->ByteMask
[0][i
]);
3193 arp
->type
= htons(ETH_P_ARP
);
3194 arp
->ar_op
= htons(1);
3196 memcpy(arp
->ar_tip
, vptr
->ip_addr
, 4);
3198 crc
= wol_calc_crc((sizeof(struct arp_packet
) + 7) / 8, buf
,
3199 (u8
*) & mask_pattern
[0][0]);
3201 writew(crc
, ®s
->PatternCRC
[0]);
3202 writew(WOLCR_ARP_EN
, ®s
->WOLCRSet
);
3205 BYTE_REG_BITS_ON(PWCFG_WOLTYPE
, ®s
->PWCFGSet
);
3206 BYTE_REG_BITS_ON(PWCFG_LEGACY_WOLEN
, ®s
->PWCFGSet
);
3208 writew(0x0FFF, ®s
->WOLSRClr
);
3210 if (vptr
->mii_status
& VELOCITY_AUTONEG_ENABLE
) {
3211 if (PHYID_GET_PHY_ID(vptr
->phy_id
) == PHYID_CICADA_CS8201
)
3212 MII_REG_BITS_ON(AUXCR_MDPPS
, MII_REG_AUXCR
, vptr
->mac_regs
);
3214 MII_REG_BITS_OFF(G1000CR_1000FD
| G1000CR_1000
, MII_REG_G1000CR
, vptr
->mac_regs
);
3217 if (vptr
->mii_status
& VELOCITY_SPEED_1000
)
3218 MII_REG_BITS_ON(BMCR_REAUTO
, MII_REG_BMCR
, vptr
->mac_regs
);
3220 BYTE_REG_BITS_ON(CHIPGCR_FCMODE
, ®s
->CHIPGCR
);
3224 GCR
= readb(®s
->CHIPGCR
);
3225 GCR
= (GCR
& ~CHIPGCR_FCGMII
) | CHIPGCR_FCFDX
;
3226 writeb(GCR
, ®s
->CHIPGCR
);
3229 BYTE_REG_BITS_OFF(ISR_PWEI
, ®s
->ISR
);
3230 /* Turn on SWPTAG just before entering power mode */
3231 BYTE_REG_BITS_ON(STICKHW_SWPTAG
, ®s
->STICKHW
);
3232 /* Go to bed ..... */
3233 BYTE_REG_BITS_ON((STICKHW_DS1
| STICKHW_DS0
), ®s
->STICKHW
);
3238 static int velocity_suspend(struct pci_dev
*pdev
, pm_message_t state
)
3240 struct net_device
*dev
= pci_get_drvdata(pdev
);
3241 struct velocity_info
*vptr
= netdev_priv(dev
);
3242 unsigned long flags
;
3244 if(!netif_running(vptr
->dev
))
3247 netif_device_detach(vptr
->dev
);
3249 spin_lock_irqsave(&vptr
->lock
, flags
);
3250 pci_save_state(pdev
);
3252 if (vptr
->flags
& VELOCITY_FLAGS_WOL_ENABLED
) {
3253 velocity_get_ip(vptr
);
3254 velocity_save_context(vptr
, &vptr
->context
);
3255 velocity_shutdown(vptr
);
3256 velocity_set_wol(vptr
);
3257 pci_enable_wake(pdev
, 3, 1);
3258 pci_set_power_state(pdev
, PCI_D3hot
);
3260 velocity_save_context(vptr
, &vptr
->context
);
3261 velocity_shutdown(vptr
);
3262 pci_disable_device(pdev
);
3263 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
3266 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
3268 spin_unlock_irqrestore(&vptr
->lock
, flags
);
3272 static int velocity_resume(struct pci_dev
*pdev
)
3274 struct net_device
*dev
= pci_get_drvdata(pdev
);
3275 struct velocity_info
*vptr
= netdev_priv(dev
);
3276 unsigned long flags
;
3279 if(!netif_running(vptr
->dev
))
3282 pci_set_power_state(pdev
, PCI_D0
);
3283 pci_enable_wake(pdev
, 0, 0);
3284 pci_restore_state(pdev
);
3286 mac_wol_reset(vptr
->mac_regs
);
3288 spin_lock_irqsave(&vptr
->lock
, flags
);
3289 velocity_restore_context(vptr
, &vptr
->context
);
3290 velocity_init_registers(vptr
, VELOCITY_INIT_WOL
);
3291 mac_disable_int(vptr
->mac_regs
);
3293 velocity_tx_srv(vptr
, 0);
3295 for (i
= 0; i
< vptr
->num_txq
; i
++) {
3296 if (vptr
->td_used
[i
]) {
3297 mac_tx_queue_wake(vptr
->mac_regs
, i
);
3301 mac_enable_int(vptr
->mac_regs
);
3302 spin_unlock_irqrestore(&vptr
->lock
, flags
);
3303 netif_device_attach(vptr
->dev
);
3310 static int velocity_netdev_event(struct notifier_block
*nb
, unsigned long notification
, void *ptr
)
3312 struct in_ifaddr
*ifa
= (struct in_ifaddr
*) ptr
;
3315 struct net_device
*dev
= ifa
->ifa_dev
->dev
;
3316 struct velocity_info
*vptr
;
3317 unsigned long flags
;
3319 spin_lock_irqsave(&velocity_dev_list_lock
, flags
);
3320 list_for_each_entry(vptr
, &velocity_dev_list
, list
) {
3321 if (vptr
->dev
== dev
) {
3322 velocity_get_ip(vptr
);
3326 spin_unlock_irqrestore(&velocity_dev_list_lock
, flags
);